xref: /freebsd/sys/dev/cxgbe/t4_main.c (revision d381a6b4a552305de48027c51919a7cf28d52d02)
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 #include <net/rss_config.h>
61 #include <netinet/in.h>
62 #include <netinet/ip.h>
63 #ifdef KERN_TLS
64 #include <netinet/tcp_seq.h>
65 #endif
66 #if defined(__i386__) || defined(__amd64__)
67 #include <machine/md_var.h>
68 #include <machine/cputypes.h>
69 #include <vm/vm.h>
70 #include <vm/pmap.h>
71 #endif
72 #ifdef DDB
73 #include <ddb/ddb.h>
74 #include <ddb/db_lex.h>
75 #endif
76 
77 #include "common/common.h"
78 #include "common/t4_msg.h"
79 #include "common/t4_regs.h"
80 #include "common/t4_regs_values.h"
81 #include "cudbg/cudbg.h"
82 #include "t4_clip.h"
83 #include "t4_ioctl.h"
84 #include "t4_l2t.h"
85 #include "t4_mp_ring.h"
86 #include "t4_if.h"
87 #include "t4_smt.h"
88 
89 /* T4 bus driver interface */
90 static int t4_probe(device_t);
91 static int t4_attach(device_t);
92 static int t4_detach(device_t);
93 static int t4_child_location(device_t, device_t, struct sbuf *);
94 static int t4_ready(device_t);
95 static int t4_read_port_device(device_t, int, device_t *);
96 static int t4_suspend(device_t);
97 static int t4_resume(device_t);
98 static int t4_reset_prepare(device_t, device_t);
99 static int t4_reset_post(device_t, device_t);
100 static device_method_t t4_methods[] = {
101 	DEVMETHOD(device_probe,		t4_probe),
102 	DEVMETHOD(device_attach,	t4_attach),
103 	DEVMETHOD(device_detach,	t4_detach),
104 	DEVMETHOD(device_suspend,	t4_suspend),
105 	DEVMETHOD(device_resume,	t4_resume),
106 
107 	DEVMETHOD(bus_child_location,	t4_child_location),
108 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
109 	DEVMETHOD(bus_reset_post,	t4_reset_post),
110 
111 	DEVMETHOD(t4_is_main_ready,	t4_ready),
112 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
113 
114 	DEVMETHOD_END
115 };
116 static driver_t t4_driver = {
117 	"t4nex",
118 	t4_methods,
119 	sizeof(struct adapter)
120 };
121 
122 
123 /* T4 port (cxgbe) interface */
124 static int cxgbe_probe(device_t);
125 static int cxgbe_attach(device_t);
126 static int cxgbe_detach(device_t);
127 device_method_t cxgbe_methods[] = {
128 	DEVMETHOD(device_probe,		cxgbe_probe),
129 	DEVMETHOD(device_attach,	cxgbe_attach),
130 	DEVMETHOD(device_detach,	cxgbe_detach),
131 	{ 0, 0 }
132 };
133 static driver_t cxgbe_driver = {
134 	"cxgbe",
135 	cxgbe_methods,
136 	sizeof(struct port_info)
137 };
138 
139 /* T4 VI (vcxgbe) interface */
140 static int vcxgbe_probe(device_t);
141 static int vcxgbe_attach(device_t);
142 static int vcxgbe_detach(device_t);
143 static device_method_t vcxgbe_methods[] = {
144 	DEVMETHOD(device_probe,		vcxgbe_probe),
145 	DEVMETHOD(device_attach,	vcxgbe_attach),
146 	DEVMETHOD(device_detach,	vcxgbe_detach),
147 	{ 0, 0 }
148 };
149 static driver_t vcxgbe_driver = {
150 	"vcxgbe",
151 	vcxgbe_methods,
152 	sizeof(struct vi_info)
153 };
154 
155 static d_ioctl_t t4_ioctl;
156 
157 static struct cdevsw t4_cdevsw = {
158        .d_version = D_VERSION,
159        .d_ioctl = t4_ioctl,
160        .d_name = "t4nex",
161 };
162 
163 /* T5 bus driver interface */
164 static int t5_probe(device_t);
165 static device_method_t t5_methods[] = {
166 	DEVMETHOD(device_probe,		t5_probe),
167 	DEVMETHOD(device_attach,	t4_attach),
168 	DEVMETHOD(device_detach,	t4_detach),
169 	DEVMETHOD(device_suspend,	t4_suspend),
170 	DEVMETHOD(device_resume,	t4_resume),
171 
172 	DEVMETHOD(bus_child_location,	t4_child_location),
173 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
174 	DEVMETHOD(bus_reset_post,	t4_reset_post),
175 
176 	DEVMETHOD(t4_is_main_ready,	t4_ready),
177 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
178 
179 	DEVMETHOD_END
180 };
181 static driver_t t5_driver = {
182 	"t5nex",
183 	t5_methods,
184 	sizeof(struct adapter)
185 };
186 
187 
188 /* T5 port (cxl) interface */
189 static driver_t cxl_driver = {
190 	"cxl",
191 	cxgbe_methods,
192 	sizeof(struct port_info)
193 };
194 
195 /* T5 VI (vcxl) interface */
196 static driver_t vcxl_driver = {
197 	"vcxl",
198 	vcxgbe_methods,
199 	sizeof(struct vi_info)
200 };
201 
202 /* T6 bus driver interface */
203 static int t6_probe(device_t);
204 static device_method_t t6_methods[] = {
205 	DEVMETHOD(device_probe,		t6_probe),
206 	DEVMETHOD(device_attach,	t4_attach),
207 	DEVMETHOD(device_detach,	t4_detach),
208 	DEVMETHOD(device_suspend,	t4_suspend),
209 	DEVMETHOD(device_resume,	t4_resume),
210 
211 	DEVMETHOD(bus_child_location,	t4_child_location),
212 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
213 	DEVMETHOD(bus_reset_post,	t4_reset_post),
214 
215 	DEVMETHOD(t4_is_main_ready,	t4_ready),
216 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
217 
218 	DEVMETHOD_END
219 };
220 static driver_t t6_driver = {
221 	"t6nex",
222 	t6_methods,
223 	sizeof(struct adapter)
224 };
225 
226 
227 /* T6 port (cc) interface */
228 static driver_t cc_driver = {
229 	"cc",
230 	cxgbe_methods,
231 	sizeof(struct port_info)
232 };
233 
234 /* T6 VI (vcc) interface */
235 static driver_t vcc_driver = {
236 	"vcc",
237 	vcxgbe_methods,
238 	sizeof(struct vi_info)
239 };
240 
241 /* T7+ bus driver interface */
242 static int ch_probe(device_t);
243 static device_method_t ch_methods[] = {
244 	DEVMETHOD(device_probe,		ch_probe),
245 	DEVMETHOD(device_attach,	t4_attach),
246 	DEVMETHOD(device_detach,	t4_detach),
247 	DEVMETHOD(device_suspend,	t4_suspend),
248 	DEVMETHOD(device_resume,	t4_resume),
249 
250 	DEVMETHOD(bus_child_location,	t4_child_location),
251 	DEVMETHOD(bus_reset_prepare, 	t4_reset_prepare),
252 	DEVMETHOD(bus_reset_post, 	t4_reset_post),
253 
254 	DEVMETHOD(t4_is_main_ready,	t4_ready),
255 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
256 
257 	DEVMETHOD_END
258 };
259 static driver_t ch_driver = {
260 	"chnex",
261 	ch_methods,
262 	sizeof(struct adapter)
263 };
264 
265 
266 /* T7+ port (che) interface */
267 static driver_t che_driver = {
268 	"che",
269 	cxgbe_methods,
270 	sizeof(struct port_info)
271 };
272 
273 /* T7+ VI (vche) interface */
274 static driver_t vche_driver = {
275 	"vche",
276 	vcxgbe_methods,
277 	sizeof(struct vi_info)
278 };
279 
280 /* ifnet interface */
281 static void cxgbe_init(void *);
282 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
283 static int cxgbe_transmit(if_t, struct mbuf *);
284 static void cxgbe_qflush(if_t);
285 #if defined(KERN_TLS) || defined(RATELIMIT)
286 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
287     struct m_snd_tag **);
288 #endif
289 
290 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
291 
292 /*
293  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
294  * then ADAPTER_LOCK, then t4_uld_list_lock.
295  */
296 static struct sx t4_list_lock;
297 SLIST_HEAD(, adapter) t4_list;
298 #ifdef TCP_OFFLOAD
299 static struct sx t4_uld_list_lock;
300 struct uld_info *t4_uld_list[ULD_MAX + 1];
301 #endif
302 
303 /*
304  * Tunables.  See tweak_tunables() too.
305  *
306  * Each tunable is set to a default value here if it's known at compile-time.
307  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
308  * provide a reasonable default (upto n) when the driver is loaded.
309  *
310  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
311  * T5 are under hw.cxl.
312  */
313 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
314     "cxgbe(4) parameters");
315 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
316     "cxgbe(4) T5+ parameters");
317 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
318     "cxgbe(4) TOE parameters");
319 
320 /*
321  * Number of queues for tx and rx, NIC and offload.
322  */
323 #define NTXQ 16
324 int t4_ntxq = -NTXQ;
325 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
326     "Number of TX queues per port");
327 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
328 
329 #define NRXQ 8
330 int t4_nrxq = -NRXQ;
331 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
332     "Number of RX queues per port");
333 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
334 
335 #define NTXQ_VI 1
336 static int t4_ntxq_vi = -NTXQ_VI;
337 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
338     "Number of TX queues per VI");
339 
340 #define NRXQ_VI 1
341 static int t4_nrxq_vi = -NRXQ_VI;
342 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
343     "Number of RX queues per VI");
344 
345 static int t4_rsrv_noflowq = 0;
346 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
347     0, "Reserve TX queue 0 of each VI for non-flowid packets");
348 
349 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
350 #define NOFLDTXQ 8
351 static int t4_nofldtxq = -NOFLDTXQ;
352 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
353     "Number of offload TX queues per port");
354 
355 #define NOFLDTXQ_VI 1
356 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
357 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
358     "Number of offload TX queues per VI");
359 #endif
360 
361 #if defined(TCP_OFFLOAD)
362 #define NOFLDRXQ 2
363 static int t4_nofldrxq = -NOFLDRXQ;
364 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
365     "Number of offload RX queues per port");
366 
367 #define NOFLDRXQ_VI 1
368 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
369 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
370     "Number of offload RX queues per VI");
371 
372 #define TMR_IDX_OFLD 1
373 static int t4_tmr_idx_ofld = TMR_IDX_OFLD;
374 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
375     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
376 
377 #define PKTC_IDX_OFLD (-1)
378 static int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
379 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
380     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
381 
382 /* 0 means chip/fw default, non-zero number is value in microseconds */
383 static u_long t4_toe_keepalive_idle = 0;
384 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
385     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
386 
387 /* 0 means chip/fw default, non-zero number is value in microseconds */
388 static u_long t4_toe_keepalive_interval = 0;
389 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
390     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
391 
392 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
393 static int t4_toe_keepalive_count = 0;
394 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
395     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
396 
397 /* 0 means chip/fw default, non-zero number is value in microseconds */
398 static u_long t4_toe_rexmt_min = 0;
399 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
400     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
401 
402 /* 0 means chip/fw default, non-zero number is value in microseconds */
403 static u_long t4_toe_rexmt_max = 0;
404 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
405     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
406 
407 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
408 static int t4_toe_rexmt_count = 0;
409 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
410     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
411 
412 /* -1 means chip/fw default, other values are raw backoff values to use */
413 static int t4_toe_rexmt_backoff[16] = {
414 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
415 };
416 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
417     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
418     "cxgbe(4) TOE retransmit backoff values");
419 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
420     &t4_toe_rexmt_backoff[0], 0, "");
421 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
422     &t4_toe_rexmt_backoff[1], 0, "");
423 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
424     &t4_toe_rexmt_backoff[2], 0, "");
425 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
426     &t4_toe_rexmt_backoff[3], 0, "");
427 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
428     &t4_toe_rexmt_backoff[4], 0, "");
429 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
430     &t4_toe_rexmt_backoff[5], 0, "");
431 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
432     &t4_toe_rexmt_backoff[6], 0, "");
433 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
434     &t4_toe_rexmt_backoff[7], 0, "");
435 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
436     &t4_toe_rexmt_backoff[8], 0, "");
437 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
438     &t4_toe_rexmt_backoff[9], 0, "");
439 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
440     &t4_toe_rexmt_backoff[10], 0, "");
441 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
442     &t4_toe_rexmt_backoff[11], 0, "");
443 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
444     &t4_toe_rexmt_backoff[12], 0, "");
445 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
446     &t4_toe_rexmt_backoff[13], 0, "");
447 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
448     &t4_toe_rexmt_backoff[14], 0, "");
449 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
450     &t4_toe_rexmt_backoff[15], 0, "");
451 
452 int t4_ddp_rcvbuf_len = 256 * 1024;
453 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_len, CTLFLAG_RWTUN,
454     &t4_ddp_rcvbuf_len, 0, "length of each DDP RX buffer");
455 
456 unsigned int t4_ddp_rcvbuf_cache = 4;
457 SYSCTL_UINT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_cache, CTLFLAG_RWTUN,
458     &t4_ddp_rcvbuf_cache, 0,
459     "maximum number of free DDP RX buffers to cache per connection");
460 #endif
461 
462 #ifdef DEV_NETMAP
463 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
464 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
465 static int t4_native_netmap = NN_EXTRA_VI;
466 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
467     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
468 
469 #define NNMTXQ 8
470 static int t4_nnmtxq = -NNMTXQ;
471 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
472     "Number of netmap TX queues");
473 
474 #define NNMRXQ 8
475 static int t4_nnmrxq = -NNMRXQ;
476 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
477     "Number of netmap RX queues");
478 
479 #define NNMTXQ_VI 2
480 static int t4_nnmtxq_vi = -NNMTXQ_VI;
481 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
482     "Number of netmap TX queues per VI");
483 
484 #define NNMRXQ_VI 2
485 static int t4_nnmrxq_vi = -NNMRXQ_VI;
486 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
487     "Number of netmap RX queues per VI");
488 #endif
489 
490 /*
491  * Holdoff parameters for ports.
492  */
493 #define TMR_IDX 1
494 int t4_tmr_idx = TMR_IDX;
495 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
496     0, "Holdoff timer index");
497 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
498 
499 #define PKTC_IDX (-1)
500 int t4_pktc_idx = PKTC_IDX;
501 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
502     0, "Holdoff packet counter index");
503 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
504 
505 /*
506  * Size (# of entries) of each tx and rx queue.
507  */
508 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
509 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
510     "Number of descriptors in each TX queue");
511 
512 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
513 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
514     "Number of descriptors in each RX queue");
515 
516 /*
517  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
518  */
519 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
520 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
521     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
522 
523 /*
524  * Configuration file.  All the _CF names here are special.
525  */
526 #define DEFAULT_CF	"default"
527 #define BUILTIN_CF	"built-in"
528 #define FLASH_CF	"flash"
529 #define UWIRE_CF	"uwire"
530 #define FPGA_CF		"fpga"
531 static char t4_cfg_file[32] = DEFAULT_CF;
532 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
533     sizeof(t4_cfg_file), "Firmware configuration file");
534 
535 /*
536  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
537  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
538  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
539  *            mark or when signalled to do so, 0 to never emit PAUSE.
540  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
541  *                 negotiated settings will override rx_pause/tx_pause.
542  *                 Otherwise rx_pause/tx_pause are applied forcibly.
543  */
544 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
545 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
546     &t4_pause_settings, 0,
547     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
548 
549 /*
550  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
551  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
552  *  0 to disable FEC.
553  */
554 static int t4_fec = -1;
555 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
556     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
557 
558 static const char *
559 t4_fec_bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2\6auto\7module";
560 
561 /*
562  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
563  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
564  * driver runs as if this is set to 0.
565  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
566  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
567  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
568  *    the firmware anyway (may result in l1cfg errors with old firmwares).
569  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
570  *    means set all FEC bits that are valid for the speed.
571  */
572 static int t4_force_fec = -1;
573 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
574     "Controls the use of FORCE_FEC bit in L1 configuration.");
575 
576 /*
577  * Link autonegotiation.
578  * -1 to run with the firmware default.
579  *  0 to disable.
580  *  1 to enable.
581  */
582 static int t4_autoneg = -1;
583 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
584     "Link autonegotiation");
585 
586 /*
587  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
588  * encouraged respectively).  '-n' is the same as 'n' except the firmware
589  * version used in the checks is read from the firmware bundled with the driver.
590  */
591 static int t4_fw_install = 1;
592 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
593     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
594 
595 /*
596  * ASIC features that will be used.  Disable the ones you don't want so that the
597  * chip resources aren't wasted on features that will not be used.
598  */
599 static int t4_nbmcaps_allowed = 0;
600 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
601     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
602 
603 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
604 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
605     &t4_linkcaps_allowed, 0, "Default link capabilities");
606 
607 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
608     FW_CAPS_CONFIG_SWITCH_EGRESS;
609 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
610     &t4_switchcaps_allowed, 0, "Default switch capabilities");
611 
612 static int t4_nvmecaps_allowed = -1;
613 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nvmecaps_allowed, CTLFLAG_RDTUN,
614     &t4_nvmecaps_allowed, 0, "Default NVMe capabilities");
615 
616 #ifdef RATELIMIT
617 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
618 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
619 #else
620 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
621 	FW_CAPS_CONFIG_NIC_HASHFILTER;
622 #endif
623 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
624     &t4_niccaps_allowed, 0, "Default NIC capabilities");
625 
626 static int t4_toecaps_allowed = -1;
627 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
628     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
629 
630 static int t4_rdmacaps_allowed = -1;
631 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
632     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
633 
634 static int t4_cryptocaps_allowed = -1;
635 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
636     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
637 
638 static int t4_iscsicaps_allowed = -1;
639 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
640     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
641 
642 static int t4_fcoecaps_allowed = 0;
643 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
644     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
645 
646 static int t5_write_combine = 0;
647 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
648     0, "Use WC instead of UC for BAR2");
649 
650 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
651 static int t4_doorbells_allowed = 0xf;
652 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
653 	   &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
654 
655 static int t4_num_vis = 1;
656 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
657     "Number of VIs per port");
658 
659 /*
660  * PCIe Relaxed Ordering.
661  * -1: driver should figure out a good value.
662  * 0: disable RO.
663  * 1: enable RO.
664  * 2: leave RO alone.
665  */
666 static int pcie_relaxed_ordering = -1;
667 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
668     &pcie_relaxed_ordering, 0,
669     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
670 
671 static int t4_panic_on_fatal_err = 0;
672 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
673     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
674 
675 static int t4_reset_on_fatal_err = 0;
676 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
677     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
678 
679 static int t4_reset_method = 1;
680 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_method, CTLFLAG_RWTUN, &t4_reset_method,
681     0, "reset method: 0 = PL_RST, 1 = PCIe secondary bus reset, 2 = PCIe link bounce");
682 
683 static int t4_clock_gate_on_suspend = 0;
684 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
685     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
686 
687 static int t4_tx_vm_wr = 0;
688 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
689     "Use VM work requests to transmit packets.");
690 
691 /*
692  * Set to non-zero to enable the attack filter.  A packet that matches any of
693  * these conditions will get dropped on ingress:
694  * 1) IP && source address == destination address.
695  * 2) TCP/IP && source address is not a unicast address.
696  * 3) TCP/IP && destination address is not a unicast address.
697  * 4) IP && source address is loopback (127.x.y.z).
698  * 5) IP && destination address is loopback (127.x.y.z).
699  * 6) IPv6 && source address == destination address.
700  * 7) IPv6 && source address is not a unicast address.
701  * 8) IPv6 && source address is loopback (::1/128).
702  * 9) IPv6 && destination address is loopback (::1/128).
703  * 10) IPv6 && source address is unspecified (::/128).
704  * 11) IPv6 && destination address is unspecified (::/128).
705  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
706  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
707  */
708 static int t4_attack_filter = 0;
709 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
710     &t4_attack_filter, 0, "Drop suspicious traffic");
711 
712 static int t4_drop_ip_fragments = 0;
713 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
714     &t4_drop_ip_fragments, 0, "Drop IP fragments");
715 
716 static int t4_drop_pkts_with_l2_errors = 1;
717 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
718     &t4_drop_pkts_with_l2_errors, 0,
719     "Drop all frames with Layer 2 length or checksum errors");
720 
721 static int t4_drop_pkts_with_l3_errors = 0;
722 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
723     &t4_drop_pkts_with_l3_errors, 0,
724     "Drop all frames with IP version, length, or checksum errors");
725 
726 static int t4_drop_pkts_with_l4_errors = 0;
727 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
728     &t4_drop_pkts_with_l4_errors, 0,
729     "Drop all frames with Layer 4 length, checksum, or other errors");
730 
731 #ifdef TCP_OFFLOAD
732 /*
733  * TOE tunables.
734  */
735 static int t4_cop_managed_offloading = 0;
736 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
737     &t4_cop_managed_offloading, 0,
738     "COP (Connection Offload Policy) controls all TOE offload");
739 TUNABLE_INT("hw.cxgbe.cop_managed_offloading", &t4_cop_managed_offloading);
740 #endif
741 
742 #ifdef KERN_TLS
743 /*
744  * This enables KERN_TLS for all adapters if set.
745  */
746 static int t4_kern_tls = 0;
747 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
748     "Enable KERN_TLS mode for T6 adapters");
749 
750 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
751     "cxgbe(4) KERN_TLS parameters");
752 
753 static int t4_tls_inline_keys = 0;
754 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
755     &t4_tls_inline_keys, 0,
756     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
757     "in card memory.");
758 
759 static int t4_tls_combo_wrs = 0;
760 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
761     0, "Attempt to combine TCB field updates with TLS record work requests.");
762 
763 static int t4_tls_short_records = 1;
764 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, short_records, CTLFLAG_RDTUN,
765     &t4_tls_short_records, 0, "Use cipher-only mode for short records.");
766 
767 static int t4_tls_partial_ghash = 1;
768 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, partial_ghash, CTLFLAG_RDTUN,
769     &t4_tls_partial_ghash, 0, "Use partial GHASH for AES-GCM records.");
770 #endif
771 
772 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
773 static int vi_mac_funcs[] = {
774 	FW_VI_FUNC_ETH,
775 	FW_VI_FUNC_OFLD,
776 	FW_VI_FUNC_IWARP,
777 	FW_VI_FUNC_OPENISCSI,
778 	FW_VI_FUNC_OPENFCOE,
779 	FW_VI_FUNC_FOISCSI,
780 	FW_VI_FUNC_FOFCOE,
781 };
782 
783 struct intrs_and_queues {
784 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
785 	uint16_t num_vis;	/* number of VIs for each port */
786 	uint16_t nirq;		/* Total # of vectors */
787 	uint16_t ntxq;		/* # of NIC txq's for each port */
788 	uint16_t nrxq;		/* # of NIC rxq's for each port */
789 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
790 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
791 	uint16_t nnmtxq;	/* # of netmap txq's */
792 	uint16_t nnmrxq;	/* # of netmap rxq's */
793 
794 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
795 	uint16_t ntxq_vi;	/* # of NIC txq's */
796 	uint16_t nrxq_vi;	/* # of NIC rxq's */
797 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
798 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
799 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
800 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
801 };
802 
803 static void setup_memwin(struct adapter *);
804 static void position_memwin(struct adapter *, int, uint32_t);
805 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
806 static int fwmtype_to_hwmtype(int);
807 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
808     uint32_t *);
809 static int fixup_devlog_params(struct adapter *);
810 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
811 static int contact_firmware(struct adapter *);
812 static int partition_resources(struct adapter *);
813 static int get_params__pre_init(struct adapter *);
814 static int set_params__pre_init(struct adapter *);
815 static int get_params__post_init(struct adapter *);
816 static int set_params__post_init(struct adapter *);
817 static void t4_set_desc(struct adapter *);
818 static bool fixed_ifmedia(struct port_info *);
819 static void build_medialist(struct port_info *);
820 static void init_link_config(struct port_info *);
821 static int fixup_link_config(struct port_info *);
822 static int apply_link_config(struct port_info *);
823 static int cxgbe_init_synchronized(struct vi_info *);
824 static int cxgbe_uninit_synchronized(struct vi_info *);
825 static int adapter_full_init(struct adapter *);
826 static void adapter_full_uninit(struct adapter *);
827 static int vi_full_init(struct vi_info *);
828 static void vi_full_uninit(struct vi_info *);
829 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
830 static void quiesce_txq(struct sge_txq *);
831 static void quiesce_wrq(struct sge_wrq *);
832 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
833 static void quiesce_vi(struct vi_info *);
834 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
835     driver_intr_t *, void *, char *);
836 static int t4_free_irq(struct adapter *, struct irq *);
837 static void t4_init_atid_table(struct adapter *);
838 static void t4_free_atid_table(struct adapter *);
839 static void stop_atid_allocator(struct adapter *);
840 static void restart_atid_allocator(struct adapter *);
841 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
842 static void vi_refresh_stats(struct vi_info *);
843 static void cxgbe_refresh_stats(struct vi_info *);
844 static void cxgbe_tick(void *);
845 static void vi_tick(void *);
846 static void cxgbe_sysctls(struct port_info *);
847 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
848 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
849 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
850 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
851 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
852 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
853 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
854 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
855 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
856 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
857 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
858 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
859 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
860 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
861 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
862 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
863 static int sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS);
864 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
865 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
866 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
867 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
868 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
869 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
870 static int sysctl_cim_ibq(SYSCTL_HANDLER_ARGS);
871 static int sysctl_cim_obq(SYSCTL_HANDLER_ARGS);
872 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
873 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
874 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
875 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
876 static int sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS);
877 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
878 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
879 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
880 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
881 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
882 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
883 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
884 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
885 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
886 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
887 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
888 static int sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS);
889 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
890 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
891 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
892 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
893 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
894 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
895 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
896 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
897 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
898 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
899 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
900 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
901 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
902 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
903 #ifdef TCP_OFFLOAD
904 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
905 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
906 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
907 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
908 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
909 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
910 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
911 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
912 #endif
913 static int get_sge_context(struct adapter *, int, uint32_t, int, uint32_t *);
914 static int load_fw(struct adapter *, struct t4_data *);
915 static int load_cfg(struct adapter *, struct t4_data *);
916 static int load_boot(struct adapter *, struct t4_bootrom *);
917 static int load_bootcfg(struct adapter *, struct t4_data *);
918 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
919 static void free_offload_policy(struct t4_offload_policy *);
920 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
921 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
922 static int read_i2c(struct adapter *, struct t4_i2c_data *);
923 static int clear_stats(struct adapter *, u_int);
924 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
925 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
926 static inline int stop_adapter(struct adapter *);
927 static inline void set_adapter_hwstatus(struct adapter *, const bool);
928 static int stop_lld(struct adapter *);
929 static inline int restart_adapter(struct adapter *);
930 static int restart_lld(struct adapter *);
931 #ifdef TCP_OFFLOAD
932 static int deactivate_all_uld(struct adapter *);
933 static void stop_all_uld(struct adapter *);
934 static void restart_all_uld(struct adapter *);
935 #endif
936 #ifdef KERN_TLS
937 static int ktls_capability(struct adapter *, bool);
938 #endif
939 static int mod_event(module_t, int, void *);
940 static int notify_siblings(device_t, int);
941 static uint64_t vi_get_counter(if_t, ift_counter);
942 static uint64_t cxgbe_get_counter(if_t, ift_counter);
943 static void enable_vxlan_rx(struct adapter *);
944 static void reset_adapter_task(void *, int);
945 static void fatal_error_task(void *, int);
946 static void dump_devlog(struct adapter *);
947 static void dump_cim_regs(struct adapter *);
948 static void dump_cimla(struct adapter *);
949 
950 struct {
951 	uint16_t device;
952 	char *desc;
953 } t4_pciids[] = {
954 	{0xa000, "Chelsio Terminator 4 FPGA"},
955 	{0x4400, "Chelsio T440-dbg"},
956 	{0x4401, "Chelsio T420-CR"},
957 	{0x4402, "Chelsio T422-CR"},
958 	{0x4403, "Chelsio T440-CR"},
959 	{0x4404, "Chelsio T420-BCH"},
960 	{0x4405, "Chelsio T440-BCH"},
961 	{0x4406, "Chelsio T440-CH"},
962 	{0x4407, "Chelsio T420-SO"},
963 	{0x4408, "Chelsio T420-CX"},
964 	{0x4409, "Chelsio T420-BT"},
965 	{0x440a, "Chelsio T404-BT"},
966 	{0x440e, "Chelsio T440-LP-CR"},
967 }, t5_pciids[] = {
968 	{0xb000, "Chelsio Terminator 5 FPGA"},
969 	{0x5400, "Chelsio T580-dbg"},
970 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
971 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
972 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
973 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
974 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
975 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
976 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
977 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
978 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
979 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
980 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
981 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
982 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
983 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
984 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
985 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
986 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
987 
988 	/* Custom */
989 	{0x5483, "Custom T540-CR"},
990 	{0x5484, "Custom T540-BT"},
991 }, t6_pciids[] = {
992 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
993 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
994 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
995 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
996 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
997 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
998 	{0x6405, "Chelsio T6225-SO-OCP3"},	/* 2 x 10/25G, nomem */
999 	{0x6406, "Chelsio T6225-OCP3"},		/* 2 x 10/25G */
1000 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
1001 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
1002 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
1003 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
1004 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
1005 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
1006 	{0x6414, "Chelsio T62100-SO-OCP3"},	/* 2 x 40/50/100G, nomem */
1007 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
1008 
1009 	/* Custom */
1010 	{0x6480, "Custom T6225-CR"},
1011 	{0x6481, "Custom T62100-CR"},
1012 	{0x6482, "Custom T6225-CR"},
1013 	{0x6483, "Custom T62100-CR"},
1014 	{0x6484, "Custom T64100-CR"},
1015 	{0x6485, "Custom T6240-SO"},
1016 	{0x6486, "Custom T6225-SO-CR"},
1017 	{0x6487, "Custom T6225-CR"},
1018 }, t7_pciids[] = {
1019 	{0xd000, "Chelsio Terminator 7 FPGA"},	/* T7 PE12K FPGA */
1020 	{0x7400, "Chelsio T72200-DBG"},		/* 2 x 200G, debug */
1021 	{0x7401, "Chelsio T7250"},		/* 2 x 10/25/50G, 1 mem */
1022 	{0x7402, "Chelsio S7250"},		/* 2 x 10/25/50G, nomem */
1023 	{0x7403, "Chelsio T7450"},		/* 4 x 10/25/50G, 1 mem */
1024 	{0x7404, "Chelsio S7450"},		/* 4 x 10/25/50G, nomem */
1025 	{0x7405, "Chelsio T72200"},		/* 2 x 40/100/200G, 1 mem */
1026 	{0x7406, "Chelsio S72200"},		/* 2 x 40/100/200G, nomem */
1027 	{0x7407, "Chelsio T72200-FH"},		/* 2 x 40/100/200G, 2 mem */
1028 	{0x7408, "Chelsio S71400"},		/* 1 x 400G, nomem */
1029 	{0x7409, "Chelsio S7210-BT"},		/* 2 x 10GBASE-T, nomem */
1030 	{0x740a, "Chelsio T7450-RC"},		/* 4 x 10/25/50G, 1 mem, RC */
1031 	{0x740b, "Chelsio T72200-RC"},		/* 2 x 40/100/200G, 1 mem, RC */
1032 	{0x740c, "Chelsio T72200-FH-RC"},	/* 2 x 40/100/200G, 2 mem, RC */
1033 	{0x740d, "Chelsio S72200-OCP3"},	/* 2 x 40/100/200G OCP3 */
1034 	{0x740e, "Chelsio S7450-OCP3"},		/* 4 x 1/20/25/50G OCP3 */
1035 	{0x740f, "Chelsio S7410-BT-OCP3"},	/* 4 x 10GBASE-T OCP3 */
1036 	{0x7410, "Chelsio S7210-BT-A"},		/* 2 x 10GBASE-T */
1037 	{0x7411, "Chelsio T7_MAYRA_7"},		/* Motherboard */
1038 
1039 	/* Custom */
1040 	{0x7480, "Custom T7"},
1041 };
1042 
1043 #ifdef TCP_OFFLOAD
1044 /*
1045  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
1046  * be exactly the same for both rxq and ofld_rxq.
1047  */
1048 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
1049 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
1050 #endif
1051 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
1052 
1053 static int
t4_probe(device_t dev)1054 t4_probe(device_t dev)
1055 {
1056 	int i;
1057 	uint16_t v = pci_get_vendor(dev);
1058 	uint16_t d = pci_get_device(dev);
1059 	uint8_t f = pci_get_function(dev);
1060 
1061 	if (v != PCI_VENDOR_ID_CHELSIO)
1062 		return (ENXIO);
1063 
1064 	/* Attach only to PF0 of the FPGA */
1065 	if (d == 0xa000 && f != 0)
1066 		return (ENXIO);
1067 
1068 	for (i = 0; i < nitems(t4_pciids); i++) {
1069 		if (d == t4_pciids[i].device) {
1070 			device_set_desc(dev, t4_pciids[i].desc);
1071 			return (BUS_PROBE_DEFAULT);
1072 		}
1073 	}
1074 
1075 	return (ENXIO);
1076 }
1077 
1078 static int
t5_probe(device_t dev)1079 t5_probe(device_t dev)
1080 {
1081 	int i;
1082 	uint16_t v = pci_get_vendor(dev);
1083 	uint16_t d = pci_get_device(dev);
1084 	uint8_t f = pci_get_function(dev);
1085 
1086 	if (v != PCI_VENDOR_ID_CHELSIO)
1087 		return (ENXIO);
1088 
1089 	/* Attach only to PF0 of the FPGA */
1090 	if (d == 0xb000 && f != 0)
1091 		return (ENXIO);
1092 
1093 	for (i = 0; i < nitems(t5_pciids); i++) {
1094 		if (d == t5_pciids[i].device) {
1095 			device_set_desc(dev, t5_pciids[i].desc);
1096 			return (BUS_PROBE_DEFAULT);
1097 		}
1098 	}
1099 
1100 	return (ENXIO);
1101 }
1102 
1103 static int
t6_probe(device_t dev)1104 t6_probe(device_t dev)
1105 {
1106 	int i;
1107 	uint16_t v = pci_get_vendor(dev);
1108 	uint16_t d = pci_get_device(dev);
1109 
1110 	if (v != PCI_VENDOR_ID_CHELSIO)
1111 		return (ENXIO);
1112 
1113 	for (i = 0; i < nitems(t6_pciids); i++) {
1114 		if (d == t6_pciids[i].device) {
1115 			device_set_desc(dev, t6_pciids[i].desc);
1116 			return (BUS_PROBE_DEFAULT);
1117 		}
1118 	}
1119 
1120 	return (ENXIO);
1121 }
1122 
1123 static int
ch_probe(device_t dev)1124 ch_probe(device_t dev)
1125 {
1126 	int i;
1127 	uint16_t v = pci_get_vendor(dev);
1128 	uint16_t d = pci_get_device(dev);
1129 	uint8_t f = pci_get_function(dev);
1130 
1131 	if (v != PCI_VENDOR_ID_CHELSIO)
1132 		return (ENXIO);
1133 
1134 	/* Attach only to PF0 of the FPGA */
1135 	if (d == 0xd000 && f != 0)
1136 		return (ENXIO);
1137 
1138 	for (i = 0; i < nitems(t7_pciids); i++) {
1139 		if (d == t7_pciids[i].device) {
1140 			device_set_desc(dev, t7_pciids[i].desc);
1141 			return (BUS_PROBE_DEFAULT);
1142 		}
1143 	}
1144 
1145 	return (ENXIO);
1146 }
1147 
1148 static void
t5_attribute_workaround(device_t dev)1149 t5_attribute_workaround(device_t dev)
1150 {
1151 	device_t root_port;
1152 	uint32_t v;
1153 
1154 	/*
1155 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1156 	 * Ordering attributes when replying to a TLP from a Root
1157 	 * Port.  As a workaround, find the parent Root Port and
1158 	 * disable No Snoop and Relaxed Ordering.  Note that this
1159 	 * affects all devices under this root port.
1160 	 */
1161 	root_port = pci_find_pcie_root_port(dev);
1162 	if (root_port == NULL) {
1163 		device_printf(dev, "Unable to find parent root port\n");
1164 		return;
1165 	}
1166 
1167 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1168 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1169 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1170 	    0)
1171 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1172 		    device_get_nameunit(root_port));
1173 }
1174 
1175 static const struct devnames devnames[] = {
1176 	{
1177 		.nexus_name = "t4nex",
1178 		.ifnet_name = "cxgbe",
1179 		.vi_ifnet_name = "vcxgbe",
1180 		.pf03_drv_name = "t4iov",
1181 		.vf_nexus_name = "t4vf",
1182 		.vf_ifnet_name = "cxgbev"
1183 	}, {
1184 		.nexus_name = "t5nex",
1185 		.ifnet_name = "cxl",
1186 		.vi_ifnet_name = "vcxl",
1187 		.pf03_drv_name = "t5iov",
1188 		.vf_nexus_name = "t5vf",
1189 		.vf_ifnet_name = "cxlv"
1190 	}, {
1191 		.nexus_name = "t6nex",
1192 		.ifnet_name = "cc",
1193 		.vi_ifnet_name = "vcc",
1194 		.pf03_drv_name = "t6iov",
1195 		.vf_nexus_name = "t6vf",
1196 		.vf_ifnet_name = "ccv"
1197 	}, {
1198 		.nexus_name = "chnex",
1199 		.ifnet_name = "che",
1200 		.vi_ifnet_name = "vche",
1201 		.pf03_drv_name = "chiov",
1202 		.vf_nexus_name = "chvf",
1203 		.vf_ifnet_name = "chev"
1204 	}
1205 };
1206 
1207 void
t4_init_devnames(struct adapter * sc)1208 t4_init_devnames(struct adapter *sc)
1209 {
1210 	int id;
1211 
1212 	id = chip_id(sc);
1213 	if (id < CHELSIO_T4) {
1214 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1215 		sc->names = NULL;
1216 	} else if (id - CHELSIO_T4 < nitems(devnames))
1217 		sc->names = &devnames[id - CHELSIO_T4];
1218 	else
1219 		sc->names = &devnames[nitems(devnames) - 1];
1220 }
1221 
1222 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1223 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1224 {
1225 	const char *parent, *name;
1226 	long value;
1227 	int line, unit;
1228 
1229 	line = 0;
1230 	parent = device_get_nameunit(sc->dev);
1231 	name = sc->names->ifnet_name;
1232 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1233 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1234 		    value == pi->port_id)
1235 			return (unit);
1236 	}
1237 	return (-1);
1238 }
1239 
1240 static void
t4_calibration(void * arg)1241 t4_calibration(void *arg)
1242 {
1243 	struct adapter *sc;
1244 	struct clock_sync *cur, *nex;
1245 	uint64_t hw;
1246 	sbintime_t sbt;
1247 	int next_up;
1248 
1249 	sc = (struct adapter *)arg;
1250 
1251 	KASSERT(hw_all_ok(sc), ("!hw_all_ok at t4_calibration"));
1252 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1253 	sbt = sbinuptime();
1254 
1255 	cur = &sc->cal_info[sc->cal_current];
1256 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1257 	nex = &sc->cal_info[next_up];
1258 	if (__predict_false(sc->cal_count == 0)) {
1259 		/* First time in, just get the values in */
1260 		cur->hw_cur = hw;
1261 		cur->sbt_cur = sbt;
1262 		sc->cal_count++;
1263 		goto done;
1264 	}
1265 
1266 	if (cur->hw_cur == hw) {
1267 		/* The clock is not advancing? */
1268 		sc->cal_count = 0;
1269 		atomic_store_rel_int(&cur->gen, 0);
1270 		goto done;
1271 	}
1272 
1273 	seqc_write_begin(&nex->gen);
1274 	nex->hw_prev = cur->hw_cur;
1275 	nex->sbt_prev = cur->sbt_cur;
1276 	nex->hw_cur = hw;
1277 	nex->sbt_cur = sbt;
1278 	seqc_write_end(&nex->gen);
1279 	sc->cal_current = next_up;
1280 done:
1281 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1282 	    sc, C_DIRECT_EXEC);
1283 }
1284 
1285 static void
t4_calibration_start(struct adapter * sc)1286 t4_calibration_start(struct adapter *sc)
1287 {
1288 	/*
1289 	 * Here if we have not done a calibration
1290 	 * then do so otherwise start the appropriate
1291 	 * timer.
1292 	 */
1293 	int i;
1294 
1295 	for (i = 0; i < CNT_CAL_INFO; i++) {
1296 		sc->cal_info[i].gen = 0;
1297 	}
1298 	sc->cal_current = 0;
1299 	sc->cal_count = 0;
1300 	sc->cal_gen = 0;
1301 	t4_calibration(sc);
1302 }
1303 
1304 static int
t4_attach(device_t dev)1305 t4_attach(device_t dev)
1306 {
1307 	struct adapter *sc;
1308 	int rc = 0, i, j, rqidx, tqidx, nports;
1309 	struct make_dev_args mda;
1310 	struct intrs_and_queues iaq;
1311 	struct sge *s;
1312 	uint32_t *buf;
1313 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1314 	int ofld_tqidx;
1315 #endif
1316 #ifdef TCP_OFFLOAD
1317 	int ofld_rqidx;
1318 #endif
1319 #ifdef DEV_NETMAP
1320 	int nm_rqidx, nm_tqidx;
1321 #endif
1322 	int num_vis;
1323 
1324 	sc = device_get_softc(dev);
1325 	sc->dev = dev;
1326 	sysctl_ctx_init(&sc->ctx);
1327 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1328 	if (TUNABLE_INT_FETCH("hw.cxgbe.iflags", &sc->intr_flags) == 0)
1329 		sc->intr_flags = IHF_INTR_CLEAR_ON_INIT | IHF_CLR_ALL_UNIGNORED;
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 	stop_adapter(sc);
3947 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3948 		return;
3949 	if (fw_error) {
3950 		/*
3951 		 * We are here because of a firmware error/timeout and not
3952 		 * because of a hardware interrupt.  It is possible (although
3953 		 * not very likely) that an error interrupt was also raised but
3954 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3955 		 * main INT_CAUSE registers here to make sure we haven't missed
3956 		 * anything interesting.
3957 		 */
3958 		t4_slow_intr_handler(sc, sc->intr_flags);
3959 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3960 	}
3961 	t4_report_fw_error(sc);
3962 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3963 	    device_get_nameunit(sc->dev), fw_error);
3964 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3965 }
3966 
3967 void
t4_add_adapter(struct adapter * sc)3968 t4_add_adapter(struct adapter *sc)
3969 {
3970 	sx_xlock(&t4_list_lock);
3971 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3972 	sx_xunlock(&t4_list_lock);
3973 }
3974 
3975 int
t4_map_bars_0_and_4(struct adapter * sc)3976 t4_map_bars_0_and_4(struct adapter *sc)
3977 {
3978 	sc->regs_rid = PCIR_BAR(0);
3979 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3980 	    &sc->regs_rid, RF_ACTIVE);
3981 	if (sc->regs_res == NULL) {
3982 		device_printf(sc->dev, "cannot map registers.\n");
3983 		return (ENXIO);
3984 	}
3985 	sc->bt = rman_get_bustag(sc->regs_res);
3986 	sc->bh = rman_get_bushandle(sc->regs_res);
3987 	sc->mmio_len = rman_get_size(sc->regs_res);
3988 	setbit(&sc->doorbells, DOORBELL_KDB);
3989 
3990 	sc->msix_rid = PCIR_BAR(4);
3991 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3992 	    &sc->msix_rid, RF_ACTIVE);
3993 	if (sc->msix_res == NULL) {
3994 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3995 		return (ENXIO);
3996 	}
3997 
3998 	return (0);
3999 }
4000 
4001 int
t4_map_bar_2(struct adapter * sc)4002 t4_map_bar_2(struct adapter *sc)
4003 {
4004 
4005 	/*
4006 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
4007 	 * to map it if RDMA is disabled.
4008 	 */
4009 	if (is_t4(sc) && sc->rdmacaps == 0)
4010 		return (0);
4011 
4012 	sc->udbs_rid = PCIR_BAR(2);
4013 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
4014 	    &sc->udbs_rid, RF_ACTIVE);
4015 	if (sc->udbs_res == NULL) {
4016 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
4017 		return (ENXIO);
4018 	}
4019 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
4020 
4021 	if (chip_id(sc) >= CHELSIO_T5) {
4022 		setbit(&sc->doorbells, DOORBELL_UDB);
4023 #if defined(__i386__) || defined(__amd64__)
4024 		if (t5_write_combine) {
4025 			int rc, mode;
4026 
4027 			/*
4028 			 * Enable write combining on BAR2.  This is the
4029 			 * userspace doorbell BAR and is split into 128B
4030 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
4031 			 * with an egress queue.  The first 64B has the doorbell
4032 			 * and the second 64B can be used to submit a tx work
4033 			 * request with an implicit doorbell.
4034 			 */
4035 
4036 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
4037 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
4038 			if (rc == 0) {
4039 				clrbit(&sc->doorbells, DOORBELL_UDB);
4040 				setbit(&sc->doorbells, DOORBELL_WCWR);
4041 				setbit(&sc->doorbells, DOORBELL_UDBWC);
4042 			} else {
4043 				device_printf(sc->dev,
4044 				    "couldn't enable write combining: %d\n",
4045 				    rc);
4046 			}
4047 
4048 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
4049 			t4_write_reg(sc, A_SGE_STAT_CFG,
4050 			    V_STATSOURCE_T5(7) | mode);
4051 		}
4052 #endif
4053 	}
4054 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
4055 
4056 	return (0);
4057 }
4058 
4059 int
t4_adj_doorbells(struct adapter * sc)4060 t4_adj_doorbells(struct adapter *sc)
4061 {
4062 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
4063 		sc->doorbells &= t4_doorbells_allowed;
4064 		return (0);
4065 	}
4066 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
4067 	       sc->doorbells, t4_doorbells_allowed);
4068 	return (EINVAL);
4069 }
4070 
4071 struct memwin_init {
4072 	uint32_t base;
4073 	uint32_t aperture;
4074 };
4075 
4076 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
4077 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
4078 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
4079 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
4080 };
4081 
4082 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
4083 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
4084 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
4085 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
4086 };
4087 
4088 static void
setup_memwin(struct adapter * sc)4089 setup_memwin(struct adapter *sc)
4090 {
4091 	const struct memwin_init *mw_init;
4092 	struct memwin *mw;
4093 	int i;
4094 	uint32_t bar0, reg;
4095 
4096 	if (is_t4(sc)) {
4097 		/*
4098 		 * Read low 32b of bar0 indirectly via the hardware backdoor
4099 		 * mechanism.  Works from within PCI passthrough environments
4100 		 * too, where rman_get_start() can return a different value.  We
4101 		 * need to program the T4 memory window decoders with the actual
4102 		 * addresses that will be coming across the PCIe link.
4103 		 */
4104 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
4105 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
4106 
4107 		mw_init = &t4_memwin[0];
4108 	} else {
4109 		/* T5+ use the relative offset inside the PCIe BAR */
4110 		bar0 = 0;
4111 
4112 		mw_init = &t5_memwin[0];
4113 	}
4114 
4115 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
4116 		if (!rw_initialized(&mw->mw_lock)) {
4117 			rw_init(&mw->mw_lock, "memory window access");
4118 			mw->mw_base = mw_init->base;
4119 			mw->mw_aperture = mw_init->aperture;
4120 			mw->mw_curpos = 0;
4121 		}
4122 		reg = chip_id(sc) > CHELSIO_T6 ?
4123 		    PCIE_MEM_ACCESS_T7_REG(A_T7_PCIE_MEM_ACCESS_BASE_WIN, i) :
4124 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i);
4125 		t4_write_reg(sc, reg, (mw->mw_base + bar0) | V_BIR(0) |
4126 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
4127 		rw_wlock(&mw->mw_lock);
4128 		position_memwin(sc, i, mw->mw_curpos);
4129 		rw_wunlock(&mw->mw_lock);
4130 	}
4131 
4132 	/* flush */
4133 	t4_read_reg(sc, reg);
4134 }
4135 
4136 /*
4137  * Positions the memory window at the given address in the card's address space.
4138  * There are some alignment requirements and the actual position may be at an
4139  * address prior to the requested address.  mw->mw_curpos always has the actual
4140  * position of the window.
4141  */
4142 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)4143 position_memwin(struct adapter *sc, int idx, uint32_t addr)
4144 {
4145 	struct memwin *mw;
4146 	uint32_t pf, reg, val;
4147 
4148 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
4149 	mw = &sc->memwin[idx];
4150 	rw_assert(&mw->mw_lock, RA_WLOCKED);
4151 
4152 	if (is_t4(sc)) {
4153 		pf = 0;
4154 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
4155 	} else {
4156 		pf = V_PFNUM(sc->pf);
4157 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
4158 	}
4159 	if (chip_id(sc) > CHELSIO_T6) {
4160 		reg = PCIE_MEM_ACCESS_T7_REG(A_PCIE_MEM_ACCESS_OFFSET0, idx);
4161 		val = (mw->mw_curpos >> X_T7_MEMOFST_SHIFT) | pf;
4162 	} else {
4163 		reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
4164 		val = mw->mw_curpos | pf;
4165 	}
4166 	t4_write_reg(sc, reg, val);
4167 	t4_read_reg(sc, reg);	/* flush */
4168 }
4169 
4170 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)4171 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
4172     int len, int rw)
4173 {
4174 	struct memwin *mw;
4175 	uint32_t mw_end, v;
4176 
4177 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
4178 
4179 	/* Memory can only be accessed in naturally aligned 4 byte units */
4180 	if (addr & 3 || len & 3 || len <= 0)
4181 		return (EINVAL);
4182 
4183 	mw = &sc->memwin[idx];
4184 	while (len > 0) {
4185 		rw_rlock(&mw->mw_lock);
4186 		mw_end = mw->mw_curpos + mw->mw_aperture;
4187 		if (addr >= mw_end || addr < mw->mw_curpos) {
4188 			/* Will need to reposition the window */
4189 			if (!rw_try_upgrade(&mw->mw_lock)) {
4190 				rw_runlock(&mw->mw_lock);
4191 				rw_wlock(&mw->mw_lock);
4192 			}
4193 			rw_assert(&mw->mw_lock, RA_WLOCKED);
4194 			position_memwin(sc, idx, addr);
4195 			rw_downgrade(&mw->mw_lock);
4196 			mw_end = mw->mw_curpos + mw->mw_aperture;
4197 		}
4198 		rw_assert(&mw->mw_lock, RA_RLOCKED);
4199 		while (addr < mw_end && len > 0) {
4200 			if (rw == 0) {
4201 				v = t4_read_reg(sc, mw->mw_base + addr -
4202 				    mw->mw_curpos);
4203 				*val++ = le32toh(v);
4204 			} else {
4205 				v = *val++;
4206 				t4_write_reg(sc, mw->mw_base + addr -
4207 				    mw->mw_curpos, htole32(v));
4208 			}
4209 			addr += 4;
4210 			len -= 4;
4211 		}
4212 		rw_runlock(&mw->mw_lock);
4213 	}
4214 
4215 	return (0);
4216 }
4217 
4218 CTASSERT(M_TID_COOKIE == M_COOKIE);
4219 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
4220 
4221 static void
t4_init_atid_table(struct adapter * sc)4222 t4_init_atid_table(struct adapter *sc)
4223 {
4224 	struct tid_info *t;
4225 	int i;
4226 
4227 	t = &sc->tids;
4228 	if (t->natids == 0)
4229 		return;
4230 
4231 	MPASS(t->atid_tab == NULL);
4232 
4233 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
4234 	    M_ZERO | M_WAITOK);
4235 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
4236 	t->afree = t->atid_tab;
4237 	t->atids_in_use = 0;
4238 	t->atid_alloc_stopped = false;
4239 	for (i = 1; i < t->natids; i++)
4240 		t->atid_tab[i - 1].next = &t->atid_tab[i];
4241 	t->atid_tab[t->natids - 1].next = NULL;
4242 }
4243 
4244 static void
t4_free_atid_table(struct adapter * sc)4245 t4_free_atid_table(struct adapter *sc)
4246 {
4247 	struct tid_info *t;
4248 
4249 	t = &sc->tids;
4250 
4251 	KASSERT(t->atids_in_use == 0,
4252 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4253 
4254 	if (mtx_initialized(&t->atid_lock))
4255 		mtx_destroy(&t->atid_lock);
4256 	free(t->atid_tab, M_CXGBE);
4257 	t->atid_tab = NULL;
4258 }
4259 
4260 static void
stop_atid_allocator(struct adapter * sc)4261 stop_atid_allocator(struct adapter *sc)
4262 {
4263 	struct tid_info *t = &sc->tids;
4264 
4265 	if (t->natids == 0)
4266 		return;
4267 	mtx_lock(&t->atid_lock);
4268 	t->atid_alloc_stopped = true;
4269 	mtx_unlock(&t->atid_lock);
4270 }
4271 
4272 static void
restart_atid_allocator(struct adapter * sc)4273 restart_atid_allocator(struct adapter *sc)
4274 {
4275 	struct tid_info *t = &sc->tids;
4276 
4277 	if (t->natids == 0)
4278 		return;
4279 	mtx_lock(&t->atid_lock);
4280 	KASSERT(t->atids_in_use == 0,
4281 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4282 	t->atid_alloc_stopped = false;
4283 	mtx_unlock(&t->atid_lock);
4284 }
4285 
4286 int
alloc_atid(struct adapter * sc,void * ctx)4287 alloc_atid(struct adapter *sc, void *ctx)
4288 {
4289 	struct tid_info *t = &sc->tids;
4290 	int atid = -1;
4291 
4292 	mtx_lock(&t->atid_lock);
4293 	if (t->afree && !t->atid_alloc_stopped) {
4294 		union aopen_entry *p = t->afree;
4295 
4296 		atid = p - t->atid_tab;
4297 		MPASS(atid <= M_TID_TID);
4298 		t->afree = p->next;
4299 		p->data = ctx;
4300 		t->atids_in_use++;
4301 	}
4302 	mtx_unlock(&t->atid_lock);
4303 	return (atid);
4304 }
4305 
4306 void *
lookup_atid(struct adapter * sc,int atid)4307 lookup_atid(struct adapter *sc, int atid)
4308 {
4309 	struct tid_info *t = &sc->tids;
4310 
4311 	return (t->atid_tab[atid].data);
4312 }
4313 
4314 void
free_atid(struct adapter * sc,int atid)4315 free_atid(struct adapter *sc, int atid)
4316 {
4317 	struct tid_info *t = &sc->tids;
4318 	union aopen_entry *p = &t->atid_tab[atid];
4319 
4320 	mtx_lock(&t->atid_lock);
4321 	p->next = t->afree;
4322 	t->afree = p;
4323 	t->atids_in_use--;
4324 	mtx_unlock(&t->atid_lock);
4325 }
4326 
4327 static void
queue_tid_release(struct adapter * sc,int tid)4328 queue_tid_release(struct adapter *sc, int tid)
4329 {
4330 
4331 	CXGBE_UNIMPLEMENTED("deferred tid release");
4332 }
4333 
4334 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4335 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4336 {
4337 	struct wrqe *wr;
4338 	struct cpl_tid_release *req;
4339 
4340 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4341 	if (wr == NULL) {
4342 		queue_tid_release(sc, tid);	/* defer */
4343 		return;
4344 	}
4345 	req = wrtod(wr);
4346 
4347 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4348 
4349 	t4_wrq_tx(sc, wr);
4350 }
4351 
4352 static int
t4_range_cmp(const void * a,const void * b)4353 t4_range_cmp(const void *a, const void *b)
4354 {
4355 	return ((const struct t4_range *)a)->start -
4356 	       ((const struct t4_range *)b)->start;
4357 }
4358 
4359 /*
4360  * Verify that the memory range specified by the addr/len pair is valid within
4361  * the card's address space.
4362  */
4363 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4364 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4365 {
4366 	struct t4_range mem_ranges[4], *r, *next;
4367 	uint32_t em, addr_len;
4368 	int i, n, remaining;
4369 
4370 	/* Memory can only be accessed in naturally aligned 4 byte units */
4371 	if (addr & 3 || len & 3 || len == 0)
4372 		return (EINVAL);
4373 
4374 	/* Enabled memories */
4375 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4376 
4377 	r = &mem_ranges[0];
4378 	n = 0;
4379 	bzero(r, sizeof(mem_ranges));
4380 	if (em & F_EDRAM0_ENABLE) {
4381 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4382 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4383 		if (r->size > 0) {
4384 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4385 			if (addr >= r->start &&
4386 			    addr + len <= r->start + r->size)
4387 				return (0);
4388 			r++;
4389 			n++;
4390 		}
4391 	}
4392 	if (em & F_EDRAM1_ENABLE) {
4393 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4394 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4395 		if (r->size > 0) {
4396 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4397 			if (addr >= r->start &&
4398 			    addr + len <= r->start + r->size)
4399 				return (0);
4400 			r++;
4401 			n++;
4402 		}
4403 	}
4404 	if (em & F_EXT_MEM_ENABLE) {
4405 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4406 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4407 		if (r->size > 0) {
4408 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4409 			if (addr >= r->start &&
4410 			    addr + len <= r->start + r->size)
4411 				return (0);
4412 			r++;
4413 			n++;
4414 		}
4415 	}
4416 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4417 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4418 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4419 		if (r->size > 0) {
4420 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4421 			if (addr >= r->start &&
4422 			    addr + len <= r->start + r->size)
4423 				return (0);
4424 			r++;
4425 			n++;
4426 		}
4427 	}
4428 	MPASS(n <= nitems(mem_ranges));
4429 
4430 	if (n > 1) {
4431 		/* Sort and merge the ranges. */
4432 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4433 
4434 		/* Start from index 0 and examine the next n - 1 entries. */
4435 		r = &mem_ranges[0];
4436 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4437 
4438 			MPASS(r->size > 0);	/* r is a valid entry. */
4439 			next = r + 1;
4440 			MPASS(next->size > 0);	/* and so is the next one. */
4441 
4442 			while (r->start + r->size >= next->start) {
4443 				/* Merge the next one into the current entry. */
4444 				r->size = max(r->start + r->size,
4445 				    next->start + next->size) - r->start;
4446 				n--;	/* One fewer entry in total. */
4447 				if (--remaining == 0)
4448 					goto done;	/* short circuit */
4449 				next++;
4450 			}
4451 			if (next != r + 1) {
4452 				/*
4453 				 * Some entries were merged into r and next
4454 				 * points to the first valid entry that couldn't
4455 				 * be merged.
4456 				 */
4457 				MPASS(next->size > 0);	/* must be valid */
4458 				memcpy(r + 1, next, remaining * sizeof(*r));
4459 #ifdef INVARIANTS
4460 				/*
4461 				 * This so that the foo->size assertion in the
4462 				 * next iteration of the loop do the right
4463 				 * thing for entries that were pulled up and are
4464 				 * no longer valid.
4465 				 */
4466 				MPASS(n < nitems(mem_ranges));
4467 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4468 				    sizeof(struct t4_range));
4469 #endif
4470 			}
4471 		}
4472 done:
4473 		/* Done merging the ranges. */
4474 		MPASS(n > 0);
4475 		r = &mem_ranges[0];
4476 		for (i = 0; i < n; i++, r++) {
4477 			if (addr >= r->start &&
4478 			    addr + len <= r->start + r->size)
4479 				return (0);
4480 		}
4481 	}
4482 
4483 	return (EFAULT);
4484 }
4485 
4486 static int
fwmtype_to_hwmtype(int mtype)4487 fwmtype_to_hwmtype(int mtype)
4488 {
4489 
4490 	switch (mtype) {
4491 	case FW_MEMTYPE_EDC0:
4492 		return (MEM_EDC0);
4493 	case FW_MEMTYPE_EDC1:
4494 		return (MEM_EDC1);
4495 	case FW_MEMTYPE_EXTMEM:
4496 		return (MEM_MC0);
4497 	case FW_MEMTYPE_EXTMEM1:
4498 		return (MEM_MC1);
4499 	default:
4500 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4501 	}
4502 }
4503 
4504 /*
4505  * Verify that the memory range specified by the memtype/offset/len pair is
4506  * valid and lies entirely within the memtype specified.  The global address of
4507  * the start of the range is returned in addr.
4508  */
4509 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4510 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4511     uint32_t *addr)
4512 {
4513 	uint32_t em, addr_len, maddr;
4514 
4515 	/* Memory can only be accessed in naturally aligned 4 byte units */
4516 	if (off & 3 || len & 3 || len == 0)
4517 		return (EINVAL);
4518 
4519 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4520 	switch (fwmtype_to_hwmtype(mtype)) {
4521 	case MEM_EDC0:
4522 		if (!(em & F_EDRAM0_ENABLE))
4523 			return (EINVAL);
4524 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4525 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4526 		break;
4527 	case MEM_EDC1:
4528 		if (!(em & F_EDRAM1_ENABLE))
4529 			return (EINVAL);
4530 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4531 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4532 		break;
4533 	case MEM_MC:
4534 		if (!(em & F_EXT_MEM_ENABLE))
4535 			return (EINVAL);
4536 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4537 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4538 		break;
4539 	case MEM_MC1:
4540 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4541 			return (EINVAL);
4542 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4543 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4544 		break;
4545 	default:
4546 		return (EINVAL);
4547 	}
4548 
4549 	*addr = maddr + off;	/* global address */
4550 	return (validate_mem_range(sc, *addr, len));
4551 }
4552 
4553 static int
fixup_devlog_params(struct adapter * sc)4554 fixup_devlog_params(struct adapter *sc)
4555 {
4556 	struct devlog_params *dparams = &sc->params.devlog;
4557 	int rc;
4558 
4559 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4560 	    dparams->size, &dparams->addr);
4561 
4562 	return (rc);
4563 }
4564 
4565 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4566 update_nirq(struct intrs_and_queues *iaq, int nports)
4567 {
4568 
4569 	iaq->nirq = T4_EXTRA_INTR;
4570 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4571 	iaq->nirq += nports * iaq->nofldrxq;
4572 	iaq->nirq += nports * (iaq->num_vis - 1) *
4573 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4574 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4575 }
4576 
4577 /*
4578  * Adjust requirements to fit the number of interrupts available.
4579  */
4580 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4581 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4582     int navail)
4583 {
4584 	int old_nirq;
4585 	const int nports = sc->params.nports;
4586 
4587 	MPASS(nports > 0);
4588 	MPASS(navail > 0);
4589 
4590 	bzero(iaq, sizeof(*iaq));
4591 	iaq->intr_type = itype;
4592 	iaq->num_vis = t4_num_vis;
4593 	iaq->ntxq = t4_ntxq;
4594 	iaq->ntxq_vi = t4_ntxq_vi;
4595 	iaq->nrxq = t4_nrxq;
4596 	iaq->nrxq_vi = t4_nrxq_vi;
4597 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4598 	if (is_offload(sc) || is_ethoffload(sc)) {
4599 		if (sc->params.tid_qid_sel_mask == 0) {
4600 			iaq->nofldtxq = t4_nofldtxq;
4601 			iaq->nofldtxq_vi = t4_nofldtxq_vi;
4602 		} else {
4603 			iaq->nofldtxq = roundup(t4_nofldtxq, sc->params.ncores);
4604 			iaq->nofldtxq_vi = roundup(t4_nofldtxq_vi,
4605 			    sc->params.ncores);
4606 			if (iaq->nofldtxq != t4_nofldtxq)
4607 				device_printf(sc->dev,
4608 				    "nofldtxq updated (%d -> %d) for correct"
4609 				    " operation with %d firmware cores.\n",
4610 				    t4_nofldtxq, iaq->nofldtxq,
4611 				    sc->params.ncores);
4612 			if (iaq->num_vis > 1 &&
4613 			    iaq->nofldtxq_vi != t4_nofldtxq_vi)
4614 				device_printf(sc->dev,
4615 				    "nofldtxq_vi updated (%d -> %d) for correct"
4616 				    " operation with %d firmware cores.\n",
4617 				    t4_nofldtxq_vi, iaq->nofldtxq_vi,
4618 				    sc->params.ncores);
4619 		}
4620 	}
4621 #endif
4622 #ifdef TCP_OFFLOAD
4623 	if (is_offload(sc)) {
4624 		iaq->nofldrxq = t4_nofldrxq;
4625 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4626 	}
4627 #endif
4628 #ifdef DEV_NETMAP
4629 	if (t4_native_netmap & NN_MAIN_VI) {
4630 		iaq->nnmtxq = t4_nnmtxq;
4631 		iaq->nnmrxq = t4_nnmrxq;
4632 	}
4633 	if (t4_native_netmap & NN_EXTRA_VI) {
4634 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4635 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4636 	}
4637 #endif
4638 
4639 	update_nirq(iaq, nports);
4640 	if (iaq->nirq <= navail &&
4641 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4642 		/*
4643 		 * This is the normal case -- there are enough interrupts for
4644 		 * everything.
4645 		 */
4646 		goto done;
4647 	}
4648 
4649 	/*
4650 	 * If extra VIs have been configured try reducing their count and see if
4651 	 * that works.
4652 	 */
4653 	while (iaq->num_vis > 1) {
4654 		iaq->num_vis--;
4655 		update_nirq(iaq, nports);
4656 		if (iaq->nirq <= navail &&
4657 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4658 			device_printf(sc->dev, "virtual interfaces per port "
4659 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4660 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4661 			    "itype %d, navail %u, nirq %d.\n",
4662 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4663 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4664 			    itype, navail, iaq->nirq);
4665 			goto done;
4666 		}
4667 	}
4668 
4669 	/*
4670 	 * Extra VIs will not be created.  Log a message if they were requested.
4671 	 */
4672 	MPASS(iaq->num_vis == 1);
4673 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4674 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4675 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4676 	if (iaq->num_vis != t4_num_vis) {
4677 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4678 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4679 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4680 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4681 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4682 	}
4683 
4684 	/*
4685 	 * Keep reducing the number of NIC rx queues to the next lower power of
4686 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4687 	 * if that works.
4688 	 */
4689 	do {
4690 		if (iaq->nrxq > 1) {
4691 			iaq->nrxq = rounddown_pow_of_two(iaq->nrxq - 1);
4692 			if (iaq->nnmrxq > iaq->nrxq)
4693 				iaq->nnmrxq = iaq->nrxq;
4694 		}
4695 		if (iaq->nofldrxq > 1)
4696 			iaq->nofldrxq >>= 1;
4697 
4698 		old_nirq = iaq->nirq;
4699 		update_nirq(iaq, nports);
4700 		if (iaq->nirq <= navail &&
4701 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4702 			device_printf(sc->dev, "running with reduced number of "
4703 			    "rx queues because of shortage of interrupts.  "
4704 			    "nrxq=%u, nofldrxq=%u.  "
4705 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4706 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4707 			goto done;
4708 		}
4709 	} while (old_nirq != iaq->nirq);
4710 
4711 	/* One interrupt for everything.  Ugh. */
4712 	device_printf(sc->dev, "running with minimal number of queues.  "
4713 	    "itype %d, navail %u.\n", itype, navail);
4714 	iaq->nirq = 1;
4715 	iaq->nrxq = 1;
4716 	iaq->ntxq = 1;
4717 	if (iaq->nofldrxq > 0) {
4718 		iaq->nofldrxq = 1;
4719 		iaq->nofldtxq = 1;
4720 		if (sc->params.tid_qid_sel_mask == 0)
4721 			iaq->nofldtxq = 1;
4722 		else
4723 			iaq->nofldtxq = sc->params.ncores;
4724 	}
4725 	iaq->nnmtxq = 0;
4726 	iaq->nnmrxq = 0;
4727 done:
4728 	MPASS(iaq->num_vis > 0);
4729 	if (iaq->num_vis > 1) {
4730 		MPASS(iaq->nrxq_vi > 0);
4731 		MPASS(iaq->ntxq_vi > 0);
4732 	}
4733 	MPASS(iaq->nirq > 0);
4734 	MPASS(iaq->nrxq > 0);
4735 	MPASS(iaq->ntxq > 0);
4736 	if (itype == INTR_MSI)
4737 		MPASS(powerof2(iaq->nirq));
4738 	if (sc->params.tid_qid_sel_mask != 0)
4739 		MPASS(iaq->nofldtxq % sc->params.ncores == 0);
4740 }
4741 
4742 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4743 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4744 {
4745 	int rc, itype, navail, nalloc;
4746 
4747 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4748 
4749 		if ((itype & t4_intr_types) == 0)
4750 			continue;	/* not allowed */
4751 
4752 		if (itype == INTR_MSIX)
4753 			navail = pci_msix_count(sc->dev);
4754 		else if (itype == INTR_MSI)
4755 			navail = pci_msi_count(sc->dev);
4756 		else
4757 			navail = 1;
4758 restart:
4759 		if (navail == 0)
4760 			continue;
4761 
4762 		calculate_iaq(sc, iaq, itype, navail);
4763 		nalloc = iaq->nirq;
4764 		rc = 0;
4765 		if (itype == INTR_MSIX)
4766 			rc = pci_alloc_msix(sc->dev, &nalloc);
4767 		else if (itype == INTR_MSI)
4768 			rc = pci_alloc_msi(sc->dev, &nalloc);
4769 
4770 		if (rc == 0 && nalloc > 0) {
4771 			if (nalloc == iaq->nirq)
4772 				return (0);
4773 
4774 			/*
4775 			 * Didn't get the number requested.  Use whatever number
4776 			 * the kernel is willing to allocate.
4777 			 */
4778 			device_printf(sc->dev, "fewer vectors than requested, "
4779 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4780 			    itype, iaq->nirq, nalloc);
4781 			pci_release_msi(sc->dev);
4782 			navail = nalloc;
4783 			goto restart;
4784 		}
4785 
4786 		device_printf(sc->dev,
4787 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4788 		    itype, rc, iaq->nirq, nalloc);
4789 	}
4790 
4791 	device_printf(sc->dev,
4792 	    "failed to find a usable interrupt type.  "
4793 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4794 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4795 
4796 	return (ENXIO);
4797 }
4798 
4799 #define FW_VERSION(chip) ( \
4800     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4801     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4802     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4803     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4804 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4805 
4806 /* Just enough of fw_hdr to cover all version info. */
4807 struct fw_h {
4808 	__u8	ver;
4809 	__u8	chip;
4810 	__be16	len512;
4811 	__be32	fw_ver;
4812 	__be32	tp_microcode_ver;
4813 	__u8	intfver_nic;
4814 	__u8	intfver_vnic;
4815 	__u8	intfver_ofld;
4816 	__u8	intfver_ri;
4817 	__u8	intfver_iscsipdu;
4818 	__u8	intfver_iscsi;
4819 	__u8	intfver_fcoepdu;
4820 	__u8	intfver_fcoe;
4821 };
4822 /* Spot check a couple of fields. */
4823 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4824 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4825 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4826 
4827 struct fw_info {
4828 	uint8_t chip;
4829 	char *kld_name;
4830 	char *fw_mod_name;
4831 	struct fw_h fw_h;
4832 } fw_info[] = {
4833 	{
4834 		.chip = CHELSIO_T4,
4835 		.kld_name = "t4fw_cfg",
4836 		.fw_mod_name = "t4fw",
4837 		.fw_h = {
4838 			.chip = FW_HDR_CHIP_T4,
4839 			.fw_ver = htobe32(FW_VERSION(T4)),
4840 			.intfver_nic = FW_INTFVER(T4, NIC),
4841 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4842 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4843 			.intfver_ri = FW_INTFVER(T4, RI),
4844 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4845 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4846 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4847 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4848 		},
4849 	}, {
4850 		.chip = CHELSIO_T5,
4851 		.kld_name = "t5fw_cfg",
4852 		.fw_mod_name = "t5fw",
4853 		.fw_h = {
4854 			.chip = FW_HDR_CHIP_T5,
4855 			.fw_ver = htobe32(FW_VERSION(T5)),
4856 			.intfver_nic = FW_INTFVER(T5, NIC),
4857 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4858 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4859 			.intfver_ri = FW_INTFVER(T5, RI),
4860 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4861 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4862 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4863 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4864 		},
4865 	}, {
4866 		.chip = CHELSIO_T6,
4867 		.kld_name = "t6fw_cfg",
4868 		.fw_mod_name = "t6fw",
4869 		.fw_h = {
4870 			.chip = FW_HDR_CHIP_T6,
4871 			.fw_ver = htobe32(FW_VERSION(T6)),
4872 			.intfver_nic = FW_INTFVER(T6, NIC),
4873 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4874 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4875 			.intfver_ri = FW_INTFVER(T6, RI),
4876 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4877 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4878 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4879 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4880 		},
4881 	}, {
4882 		.chip = CHELSIO_T7,
4883 		.kld_name = "t7fw_cfg",
4884 		.fw_mod_name = "t7fw",
4885 		.fw_h = {
4886 			.chip = FW_HDR_CHIP_T7,
4887 			.fw_ver = htobe32(FW_VERSION(T7)),
4888 			.intfver_nic = FW_INTFVER(T7, NIC),
4889 			.intfver_vnic = FW_INTFVER(T7, VNIC),
4890 			.intfver_ofld = FW_INTFVER(T7, OFLD),
4891 			.intfver_ri = FW_INTFVER(T7, RI),
4892 			.intfver_iscsipdu = FW_INTFVER(T7, ISCSIPDU),
4893 			.intfver_iscsi = FW_INTFVER(T7, ISCSI),
4894 			.intfver_fcoepdu = FW_INTFVER(T7, FCOEPDU),
4895 			.intfver_fcoe = FW_INTFVER(T7, FCOE),
4896 		},
4897 	}
4898 };
4899 
4900 static struct fw_info *
find_fw_info(int chip)4901 find_fw_info(int chip)
4902 {
4903 	int i;
4904 
4905 	for (i = 0; i < nitems(fw_info); i++) {
4906 		if (fw_info[i].chip == chip)
4907 			return (&fw_info[i]);
4908 	}
4909 	return (NULL);
4910 }
4911 
4912 /*
4913  * Is the given firmware API compatible with the one the driver was compiled
4914  * with?
4915  */
4916 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4917 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4918 {
4919 
4920 	/* short circuit if it's the exact same firmware version */
4921 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4922 		return (1);
4923 
4924 	/*
4925 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4926 	 * features that are supported in the driver.
4927 	 */
4928 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4929 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4930 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4931 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4932 		return (1);
4933 #undef SAME_INTF
4934 
4935 	return (0);
4936 }
4937 
4938 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4939 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4940     const struct firmware **fw)
4941 {
4942 	struct fw_info *fw_info;
4943 
4944 	*dcfg = NULL;
4945 	if (fw != NULL)
4946 		*fw = NULL;
4947 
4948 	fw_info = find_fw_info(chip_id(sc));
4949 	if (fw_info == NULL) {
4950 		device_printf(sc->dev,
4951 		    "unable to look up firmware information for chip %d.\n",
4952 		    chip_id(sc));
4953 		return (EINVAL);
4954 	}
4955 
4956 	*dcfg = firmware_get(fw_info->kld_name);
4957 	if (*dcfg != NULL) {
4958 		if (fw != NULL)
4959 			*fw = firmware_get(fw_info->fw_mod_name);
4960 		return (0);
4961 	}
4962 
4963 	return (ENOENT);
4964 }
4965 
4966 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4967 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4968     const struct firmware *fw)
4969 {
4970 
4971 	if (fw != NULL)
4972 		firmware_put(fw, FIRMWARE_UNLOAD);
4973 	if (dcfg != NULL)
4974 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4975 }
4976 
4977 /*
4978  * Return values:
4979  * 0 means no firmware install attempted.
4980  * ERESTART means a firmware install was attempted and was successful.
4981  * +ve errno means a firmware install was attempted but failed.
4982  */
4983 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4984 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4985     const struct fw_h *drv_fw, const char *reason, int *already)
4986 {
4987 	const struct firmware *cfg, *fw;
4988 	const uint32_t c = be32toh(card_fw->fw_ver);
4989 	uint32_t d, k;
4990 	int rc, fw_install;
4991 	struct fw_h bundled_fw;
4992 	bool load_attempted;
4993 
4994 	cfg = fw = NULL;
4995 	load_attempted = false;
4996 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4997 
4998 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4999 	if (t4_fw_install < 0) {
5000 		rc = load_fw_module(sc, &cfg, &fw);
5001 		if (rc != 0 || fw == NULL) {
5002 			device_printf(sc->dev,
5003 			    "failed to load firmware module: %d. cfg %p, fw %p;"
5004 			    " will use compiled-in firmware version for"
5005 			    "hw.cxgbe.fw_install checks.\n",
5006 			    rc, cfg, fw);
5007 		} else {
5008 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
5009 		}
5010 		load_attempted = true;
5011 	}
5012 	d = be32toh(bundled_fw.fw_ver);
5013 
5014 	if (reason != NULL)
5015 		goto install;
5016 
5017 	if ((sc->flags & FW_OK) == 0) {
5018 
5019 		if (c == 0xffffffff) {
5020 			reason = "missing";
5021 			goto install;
5022 		}
5023 
5024 		rc = 0;
5025 		goto done;
5026 	}
5027 
5028 	if (!fw_compatible(card_fw, &bundled_fw)) {
5029 		reason = "incompatible or unusable";
5030 		goto install;
5031 	}
5032 
5033 	if (d > c) {
5034 		reason = "older than the version bundled with this driver";
5035 		goto install;
5036 	}
5037 
5038 	if (fw_install == 2 && d != c) {
5039 		reason = "different than the version bundled with this driver";
5040 		goto install;
5041 	}
5042 
5043 	/* No reason to do anything to the firmware already on the card. */
5044 	rc = 0;
5045 	goto done;
5046 
5047 install:
5048 	rc = 0;
5049 	if ((*already)++)
5050 		goto done;
5051 
5052 	if (fw_install == 0) {
5053 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5054 		    "but the driver is prohibited from installing a firmware "
5055 		    "on the card.\n",
5056 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5057 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
5058 
5059 		goto done;
5060 	}
5061 
5062 	/*
5063 	 * We'll attempt to install a firmware.  Load the module first (if it
5064 	 * hasn't been loaded already).
5065 	 */
5066 	if (!load_attempted) {
5067 		rc = load_fw_module(sc, &cfg, &fw);
5068 		if (rc != 0 || fw == NULL) {
5069 			device_printf(sc->dev,
5070 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
5071 			    rc, cfg, fw);
5072 			/* carry on */
5073 		}
5074 	}
5075 	if (fw == NULL) {
5076 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5077 		    "but the driver cannot take corrective action because it "
5078 		    "is unable to load the firmware module.\n",
5079 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5080 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
5081 		rc = sc->flags & FW_OK ? 0 : ENOENT;
5082 		goto done;
5083 	}
5084 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
5085 	if (k != d) {
5086 		MPASS(t4_fw_install > 0);
5087 		device_printf(sc->dev,
5088 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
5089 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
5090 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
5091 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
5092 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
5093 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
5094 		rc = sc->flags & FW_OK ? 0 : EINVAL;
5095 		goto done;
5096 	}
5097 
5098 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5099 	    "installing firmware %u.%u.%u.%u on card.\n",
5100 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5101 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
5102 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
5103 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
5104 
5105 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
5106 	if (rc != 0) {
5107 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
5108 	} else {
5109 		/* Installed successfully, update the cached header too. */
5110 		rc = ERESTART;
5111 		memcpy(card_fw, fw->data, sizeof(*card_fw));
5112 	}
5113 done:
5114 	unload_fw_module(sc, cfg, fw);
5115 
5116 	return (rc);
5117 }
5118 
5119 /*
5120  * Establish contact with the firmware and attempt to become the master driver.
5121  *
5122  * A firmware will be installed to the card if needed (if the driver is allowed
5123  * to do so).
5124  */
5125 static int
contact_firmware(struct adapter * sc)5126 contact_firmware(struct adapter *sc)
5127 {
5128 	int rc, already = 0;
5129 	enum dev_state state;
5130 	struct fw_info *fw_info;
5131 	struct fw_hdr *card_fw;		/* fw on the card */
5132 	const struct fw_h *drv_fw;
5133 
5134 	fw_info = find_fw_info(chip_id(sc));
5135 	if (fw_info == NULL) {
5136 		device_printf(sc->dev,
5137 		    "unable to look up firmware information for chip %d.\n",
5138 		    chip_id(sc));
5139 		return (EINVAL);
5140 	}
5141 	drv_fw = &fw_info->fw_h;
5142 
5143 	/* Read the header of the firmware on the card */
5144 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
5145 restart:
5146 	rc = -t4_get_fw_hdr(sc, card_fw);
5147 	if (rc != 0) {
5148 		device_printf(sc->dev,
5149 		    "unable to read firmware header from card's flash: %d\n",
5150 		    rc);
5151 		goto done;
5152 	}
5153 
5154 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
5155 	    &already);
5156 	if (rc == ERESTART)
5157 		goto restart;
5158 	if (rc != 0)
5159 		goto done;
5160 
5161 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
5162 	if (rc < 0 || state == DEV_STATE_ERR) {
5163 		rc = -rc;
5164 		device_printf(sc->dev,
5165 		    "failed to connect to the firmware: %d, %d.  "
5166 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5167 #if 0
5168 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
5169 		    "not responding properly to HELLO", &already) == ERESTART)
5170 			goto restart;
5171 #endif
5172 		goto done;
5173 	}
5174 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
5175 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
5176 
5177 	if (rc == sc->pf) {
5178 		sc->flags |= MASTER_PF;
5179 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
5180 		    NULL, &already);
5181 		if (rc == ERESTART)
5182 			rc = 0;
5183 		else if (rc != 0)
5184 			goto done;
5185 	} else if (state == DEV_STATE_UNINIT) {
5186 		/*
5187 		 * We didn't get to be the master so we definitely won't be
5188 		 * configuring the chip.  It's a bug if someone else hasn't
5189 		 * configured it already.
5190 		 */
5191 		device_printf(sc->dev, "couldn't be master(%d), "
5192 		    "device not already initialized either(%d).  "
5193 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5194 		rc = EPROTO;
5195 		goto done;
5196 	} else {
5197 		/*
5198 		 * Some other PF is the master and has configured the chip.
5199 		 * This is allowed but untested.
5200 		 */
5201 		device_printf(sc->dev, "PF%d is master, device state %d.  "
5202 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5203 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
5204 		sc->cfcsum = 0;
5205 		rc = 0;
5206 	}
5207 done:
5208 	if (rc != 0 && sc->flags & FW_OK) {
5209 		t4_fw_bye(sc, sc->mbox);
5210 		sc->flags &= ~FW_OK;
5211 	}
5212 	free(card_fw, M_CXGBE);
5213 	return (rc);
5214 }
5215 
5216 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff,u_int maxlen)5217 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
5218     uint32_t mtype, uint32_t moff, u_int maxlen)
5219 {
5220 	struct fw_info *fw_info;
5221 	const struct firmware *dcfg, *rcfg = NULL;
5222 	const uint32_t *cfdata;
5223 	uint32_t cflen, addr;
5224 	int rc;
5225 
5226 	load_fw_module(sc, &dcfg, NULL);
5227 
5228 	/* Card specific interpretation of "default". */
5229 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5230 		if (pci_get_device(sc->dev) == 0x440a)
5231 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
5232 		if (is_fpga(sc))
5233 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
5234 	}
5235 
5236 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5237 		if (dcfg == NULL) {
5238 			device_printf(sc->dev,
5239 			    "KLD with default config is not available.\n");
5240 			rc = ENOENT;
5241 			goto done;
5242 		}
5243 		cfdata = dcfg->data;
5244 		cflen = dcfg->datasize & ~3;
5245 	} else {
5246 		char s[32];
5247 
5248 		fw_info = find_fw_info(chip_id(sc));
5249 		if (fw_info == NULL) {
5250 			device_printf(sc->dev,
5251 			    "unable to look up firmware information for chip %d.\n",
5252 			    chip_id(sc));
5253 			rc = EINVAL;
5254 			goto done;
5255 		}
5256 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
5257 
5258 		rcfg = firmware_get(s);
5259 		if (rcfg == NULL) {
5260 			device_printf(sc->dev,
5261 			    "unable to load module \"%s\" for configuration "
5262 			    "profile \"%s\".\n", s, cfg_file);
5263 			rc = ENOENT;
5264 			goto done;
5265 		}
5266 		cfdata = rcfg->data;
5267 		cflen = rcfg->datasize & ~3;
5268 	}
5269 
5270 	if (cflen > maxlen) {
5271 		device_printf(sc->dev,
5272 		    "config file too long (%d, max allowed is %d).\n",
5273 		    cflen, maxlen);
5274 		rc = EINVAL;
5275 		goto done;
5276 	}
5277 
5278 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
5279 	if (rc != 0) {
5280 		device_printf(sc->dev,
5281 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
5282 		    __func__, mtype, moff, cflen, rc);
5283 		rc = EINVAL;
5284 		goto done;
5285 	}
5286 	write_via_memwin(sc, 2, addr, cfdata, cflen);
5287 done:
5288 	if (rcfg != NULL)
5289 		firmware_put(rcfg, FIRMWARE_UNLOAD);
5290 	unload_fw_module(sc, dcfg, NULL);
5291 	return (rc);
5292 }
5293 
5294 struct caps_allowed {
5295 	uint16_t nbmcaps;
5296 	uint16_t linkcaps;
5297 	uint16_t switchcaps;
5298 	uint16_t nvmecaps;
5299 	uint16_t niccaps;
5300 	uint16_t toecaps;
5301 	uint16_t rdmacaps;
5302 	uint16_t cryptocaps;
5303 	uint16_t iscsicaps;
5304 	uint16_t fcoecaps;
5305 };
5306 
5307 #define FW_PARAM_DEV(param) \
5308 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5309 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5310 #define FW_PARAM_PFVF(param) \
5311 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5312 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5313 
5314 /*
5315  * Provide a configuration profile to the firmware and have it initialize the
5316  * chip accordingly.  This may involve uploading a configuration file to the
5317  * card.
5318  */
5319 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5320 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5321     const struct caps_allowed *caps_allowed)
5322 {
5323 	int rc;
5324 	struct fw_caps_config_cmd caps;
5325 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5326 	unsigned int maxlen = 0;
5327 	const int cfg_addr = t4_flash_cfg_addr(sc, &maxlen);
5328 
5329 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5330 	if (rc != 0) {
5331 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5332 		return (rc);
5333 	}
5334 
5335 	bzero(&caps, sizeof(caps));
5336 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5337 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5338 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5339 		mtype = 0;
5340 		moff = 0;
5341 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5342 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5343 		mtype = FW_MEMTYPE_FLASH;
5344 		moff = cfg_addr;
5345 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5346 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5347 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5348 		    FW_LEN16(caps));
5349 	} else {
5350 		/*
5351 		 * Ask the firmware where it wants us to upload the config file.
5352 		 */
5353 		param = FW_PARAM_DEV(CF);
5354 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5355 		if (rc != 0) {
5356 			/* No support for config file?  Shouldn't happen. */
5357 			device_printf(sc->dev,
5358 			    "failed to query config file location: %d.\n", rc);
5359 			goto done;
5360 		}
5361 		mtype = G_FW_PARAMS_PARAM_Y(val);
5362 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5363 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5364 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5365 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5366 		    FW_LEN16(caps));
5367 
5368 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff, maxlen);
5369 		if (rc != 0) {
5370 			device_printf(sc->dev,
5371 			    "failed to upload config file to card: %d.\n", rc);
5372 			goto done;
5373 		}
5374 	}
5375 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5376 	if (rc != 0) {
5377 		device_printf(sc->dev, "failed to pre-process config file: %d "
5378 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5379 		goto done;
5380 	}
5381 
5382 	finicsum = be32toh(caps.finicsum);
5383 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5384 	if (finicsum != cfcsum) {
5385 		device_printf(sc->dev,
5386 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5387 		    finicsum, cfcsum);
5388 	}
5389 	sc->cfcsum = cfcsum;
5390 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5391 
5392 	/*
5393 	 * Let the firmware know what features will (not) be used so it can tune
5394 	 * things accordingly.
5395 	 */
5396 #define LIMIT_CAPS(x) do { \
5397 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5398 } while (0)
5399 	LIMIT_CAPS(nbm);
5400 	LIMIT_CAPS(link);
5401 	LIMIT_CAPS(switch);
5402 	LIMIT_CAPS(nvme);
5403 	LIMIT_CAPS(nic);
5404 	LIMIT_CAPS(toe);
5405 	LIMIT_CAPS(rdma);
5406 	LIMIT_CAPS(crypto);
5407 	LIMIT_CAPS(iscsi);
5408 	LIMIT_CAPS(fcoe);
5409 #undef LIMIT_CAPS
5410 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5411 		/*
5412 		 * TOE and hashfilters are mutually exclusive.  It is a config
5413 		 * file or firmware bug if both are reported as available.  Try
5414 		 * to cope with the situation in non-debug builds by disabling
5415 		 * TOE.
5416 		 */
5417 		MPASS(caps.toecaps == 0);
5418 
5419 		caps.toecaps = 0;
5420 		caps.rdmacaps = 0;
5421 		caps.iscsicaps = 0;
5422 		caps.nvmecaps = 0;
5423 	}
5424 
5425 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5426 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5427 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5428 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5429 	if (rc != 0) {
5430 		device_printf(sc->dev,
5431 		    "failed to process config file: %d.\n", rc);
5432 		goto done;
5433 	}
5434 
5435 	t4_tweak_chip_settings(sc);
5436 	set_params__pre_init(sc);
5437 
5438 	/* get basic stuff going */
5439 	rc = -t4_fw_initialize(sc, sc->mbox);
5440 	if (rc != 0) {
5441 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5442 		goto done;
5443 	}
5444 done:
5445 	return (rc);
5446 }
5447 
5448 /*
5449  * Partition chip resources for use between various PFs, VFs, etc.
5450  */
5451 static int
partition_resources(struct adapter * sc)5452 partition_resources(struct adapter *sc)
5453 {
5454 	char cfg_file[sizeof(t4_cfg_file)];
5455 	struct caps_allowed caps_allowed;
5456 	int rc;
5457 	bool fallback;
5458 
5459 	/* Only the master driver gets to configure the chip resources. */
5460 	MPASS(sc->flags & MASTER_PF);
5461 
5462 #define COPY_CAPS(x) do { \
5463 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5464 } while (0)
5465 	bzero(&caps_allowed, sizeof(caps_allowed));
5466 	COPY_CAPS(nbm);
5467 	COPY_CAPS(link);
5468 	COPY_CAPS(switch);
5469 	COPY_CAPS(nvme);
5470 	COPY_CAPS(nic);
5471 	COPY_CAPS(toe);
5472 	COPY_CAPS(rdma);
5473 	COPY_CAPS(crypto);
5474 	COPY_CAPS(iscsi);
5475 	COPY_CAPS(fcoe);
5476 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5477 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5478 retry:
5479 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5480 	if (rc != 0 && fallback) {
5481 		dump_devlog(sc);
5482 		device_printf(sc->dev,
5483 		    "failed (%d) to configure card with \"%s\" profile, "
5484 		    "will fall back to a basic configuration and retry.\n",
5485 		    rc, cfg_file);
5486 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5487 		bzero(&caps_allowed, sizeof(caps_allowed));
5488 		COPY_CAPS(switch);
5489 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5490 		fallback = false;
5491 		goto retry;
5492 	}
5493 #undef COPY_CAPS
5494 	return (rc);
5495 }
5496 
5497 /*
5498  * Retrieve parameters that are needed (or nice to have) very early.
5499  */
5500 static int
get_params__pre_init(struct adapter * sc)5501 get_params__pre_init(struct adapter *sc)
5502 {
5503 	int rc;
5504 	uint32_t param[2], val[2];
5505 
5506 	t4_get_version_info(sc);
5507 
5508 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5509 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5510 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5511 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5512 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5513 
5514 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5515 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5516 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5517 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5518 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5519 
5520 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5521 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5522 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5523 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5524 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5525 
5526 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5527 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5528 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5529 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5530 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5531 
5532 	param[0] = FW_PARAM_DEV(PORTVEC);
5533 	param[1] = FW_PARAM_DEV(CCLK);
5534 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5535 	if (rc != 0) {
5536 		device_printf(sc->dev,
5537 		    "failed to query parameters (pre_init): %d.\n", rc);
5538 		return (rc);
5539 	}
5540 
5541 	sc->params.portvec = val[0];
5542 	sc->params.nports = bitcount32(val[0]);
5543 	sc->params.vpd.cclk = val[1];
5544 
5545 	/* Read device log parameters. */
5546 	rc = -t4_init_devlog_ncores_params(sc, 1);
5547 	if (rc == 0)
5548 		fixup_devlog_params(sc);
5549 	else {
5550 		device_printf(sc->dev,
5551 		    "failed to get devlog parameters: %d.\n", rc);
5552 		rc = 0;	/* devlog isn't critical for device operation */
5553 	}
5554 
5555 	return (rc);
5556 }
5557 
5558 /*
5559  * Any params that need to be set before FW_INITIALIZE.
5560  */
5561 static int
set_params__pre_init(struct adapter * sc)5562 set_params__pre_init(struct adapter *sc)
5563 {
5564 	int rc = 0;
5565 	uint32_t param, val;
5566 
5567 	if (chip_id(sc) >= CHELSIO_T6) {
5568 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5569 		val = 1;
5570 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5571 		/* firmwares < 1.20.1.0 do not have this param. */
5572 		if (rc == FW_EINVAL &&
5573 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5574 			rc = 0;
5575 		}
5576 		if (rc != 0) {
5577 			device_printf(sc->dev,
5578 			    "failed to enable high priority filters :%d.\n",
5579 			    rc);
5580 		}
5581 
5582 		param = FW_PARAM_DEV(PPOD_EDRAM);
5583 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5584 		if (rc == 0 && val == 1) {
5585 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5586 			    &val);
5587 			if (rc != 0) {
5588 				device_printf(sc->dev,
5589 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5590 			}
5591 		}
5592 	}
5593 
5594 	/* Enable opaque VIIDs with firmwares that support it. */
5595 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5596 	val = 1;
5597 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5598 	if (rc == 0 && val == 1)
5599 		sc->params.viid_smt_extn_support = true;
5600 	else
5601 		sc->params.viid_smt_extn_support = false;
5602 
5603 	return (rc);
5604 }
5605 
5606 /*
5607  * Retrieve various parameters that are of interest to the driver.  The device
5608  * has been initialized by the firmware at this point.
5609  */
5610 static int
get_params__post_init(struct adapter * sc)5611 get_params__post_init(struct adapter *sc)
5612 {
5613 	int rc;
5614 	uint32_t param[7], val[7];
5615 	struct fw_caps_config_cmd caps;
5616 
5617 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5618 	param[1] = FW_PARAM_PFVF(EQ_START);
5619 	param[2] = FW_PARAM_PFVF(FILTER_START);
5620 	param[3] = FW_PARAM_PFVF(FILTER_END);
5621 	param[4] = FW_PARAM_PFVF(L2T_START);
5622 	param[5] = FW_PARAM_PFVF(L2T_END);
5623 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5624 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5625 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5626 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5627 	if (rc != 0) {
5628 		device_printf(sc->dev,
5629 		    "failed to query parameters (post_init): %d.\n", rc);
5630 		return (rc);
5631 	}
5632 
5633 	sc->sge.iq_start = val[0];
5634 	sc->sge.eq_start = val[1];
5635 	if ((int)val[3] > (int)val[2]) {
5636 		sc->tids.ftid_base = val[2];
5637 		sc->tids.ftid_end = val[3];
5638 		sc->tids.nftids = val[3] - val[2] + 1;
5639 	}
5640 	sc->vres.l2t.start = val[4];
5641 	sc->vres.l2t.size = val[5] - val[4] + 1;
5642 	/* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5643 	if (sc->vres.l2t.size > 0)
5644 		MPASS(fls(val[5]) <= S_SYNC_WR);
5645 	sc->params.core_vdd = val[6];
5646 
5647 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5648 	param[1] = FW_PARAM_PFVF(EQ_END);
5649 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5650 	if (rc != 0) {
5651 		device_printf(sc->dev,
5652 		    "failed to query parameters (post_init2): %d.\n", rc);
5653 		return (rc);
5654 	}
5655 	MPASS((int)val[0] >= sc->sge.iq_start);
5656 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5657 	MPASS((int)val[1] >= sc->sge.eq_start);
5658 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5659 
5660 	if (chip_id(sc) >= CHELSIO_T6) {
5661 
5662 		sc->tids.tid_base = t4_read_reg(sc,
5663 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5664 
5665 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5666 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5667 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5668 		if (rc != 0) {
5669 			device_printf(sc->dev,
5670 			   "failed to query hpfilter parameters: %d.\n", rc);
5671 			return (rc);
5672 		}
5673 		if ((int)val[1] > (int)val[0]) {
5674 			sc->tids.hpftid_base = val[0];
5675 			sc->tids.hpftid_end = val[1];
5676 			sc->tids.nhpftids = val[1] - val[0] + 1;
5677 
5678 			/*
5679 			 * These should go off if the layout changes and the
5680 			 * driver needs to catch up.
5681 			 */
5682 			MPASS(sc->tids.hpftid_base == 0);
5683 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5684 		}
5685 
5686 		param[0] = FW_PARAM_PFVF(RAWF_START);
5687 		param[1] = FW_PARAM_PFVF(RAWF_END);
5688 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5689 		if (rc != 0) {
5690 			device_printf(sc->dev,
5691 			   "failed to query rawf parameters: %d.\n", rc);
5692 			return (rc);
5693 		}
5694 		if ((int)val[1] > (int)val[0]) {
5695 			sc->rawf_base = val[0];
5696 			sc->nrawf = val[1] - val[0] + 1;
5697 		}
5698 	}
5699 
5700 	if (sc->params.ncores > 1) {
5701 		MPASS(chip_id(sc) >= CHELSIO_T7);
5702 
5703 		param[0] = FW_PARAM_DEV(TID_QID_SEL_MASK);
5704 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5705 		sc->params.tid_qid_sel_mask = rc == 0 ? val[0] : 0;
5706 	}
5707 
5708 	/*
5709 	 * The parameters that follow may not be available on all firmwares.  We
5710 	 * query them individually rather than in a compound query because old
5711 	 * firmwares fail the entire query if an unknown parameter is queried.
5712 	 */
5713 
5714 	/*
5715 	 * MPS buffer group configuration.
5716 	 */
5717 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5718 	val[0] = 0;
5719 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5720 	if (rc == 0)
5721 		sc->params.mps_bg_map = val[0];
5722 	else
5723 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5724 
5725 	param[0] = FW_PARAM_DEV(TPCHMAP);
5726 	val[0] = 0;
5727 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5728 	if (rc == 0)
5729 		sc->params.tp_ch_map = val[0];
5730 	else
5731 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5732 
5733 	param[0] = FW_PARAM_DEV(TX_TPCHMAP);
5734 	val[0] = 0;
5735 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5736 	if (rc == 0)
5737 		sc->params.tx_tp_ch_map = val[0];
5738 	else
5739 		sc->params.tx_tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5740 
5741 	/*
5742 	 * Determine whether the firmware supports the filter2 work request.
5743 	 */
5744 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5745 	val[0] = 0;
5746 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5747 	if (rc == 0)
5748 		sc->params.filter2_wr_support = val[0] != 0;
5749 	else
5750 		sc->params.filter2_wr_support = 0;
5751 
5752 	/*
5753 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5754 	 */
5755 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5756 	val[0] = 0;
5757 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5758 	if (rc == 0)
5759 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5760 	else
5761 		sc->params.ulptx_memwrite_dsgl = false;
5762 
5763 	/* FW_RI_FR_NSMR_TPTE_WR support */
5764 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5765 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5766 	if (rc == 0)
5767 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5768 	else
5769 		sc->params.fr_nsmr_tpte_wr_support = false;
5770 
5771 	/* Support for 512 SGL entries per FR MR. */
5772 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5773 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5774 	if (rc == 0)
5775 		sc->params.dev_512sgl_mr = val[0] != 0;
5776 	else
5777 		sc->params.dev_512sgl_mr = false;
5778 
5779 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5780 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5781 	if (rc == 0)
5782 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5783 	else
5784 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5785 
5786 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5787 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5788 	if (rc == 0) {
5789 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5790 		sc->params.nsched_cls = val[0];
5791 	} else
5792 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5793 
5794 	/* get capabilites */
5795 	bzero(&caps, sizeof(caps));
5796 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5797 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5798 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5799 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5800 	if (rc != 0) {
5801 		device_printf(sc->dev,
5802 		    "failed to get card capabilities: %d.\n", rc);
5803 		return (rc);
5804 	}
5805 
5806 #define READ_CAPS(x) do { \
5807 	sc->x = htobe16(caps.x); \
5808 } while (0)
5809 	READ_CAPS(nbmcaps);
5810 	READ_CAPS(linkcaps);
5811 	READ_CAPS(switchcaps);
5812 	READ_CAPS(nvmecaps);
5813 	READ_CAPS(niccaps);
5814 	READ_CAPS(toecaps);
5815 	READ_CAPS(rdmacaps);
5816 	READ_CAPS(cryptocaps);
5817 	READ_CAPS(iscsicaps);
5818 	READ_CAPS(fcoecaps);
5819 
5820 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5821 		MPASS(chip_id(sc) > CHELSIO_T4);
5822 		MPASS(sc->toecaps == 0);
5823 		sc->toecaps = 0;
5824 
5825 		param[0] = FW_PARAM_DEV(NTID);
5826 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5827 		if (rc != 0) {
5828 			device_printf(sc->dev,
5829 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5830 			return (rc);
5831 		}
5832 		sc->tids.ntids = val[0];
5833 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5834 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5835 			sc->tids.ntids -= sc->tids.nhpftids;
5836 		}
5837 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5838 		sc->params.hash_filter = 1;
5839 	}
5840 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5841 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5842 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5843 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5844 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5845 		if (rc != 0) {
5846 			device_printf(sc->dev,
5847 			    "failed to query NIC parameters: %d.\n", rc);
5848 			return (rc);
5849 		}
5850 		if ((int)val[1] > (int)val[0]) {
5851 			sc->tids.etid_base = val[0];
5852 			sc->tids.etid_end = val[1];
5853 			sc->tids.netids = val[1] - val[0] + 1;
5854 			sc->params.eo_wr_cred = val[2];
5855 			sc->params.ethoffload = 1;
5856 		}
5857 	}
5858 	if (sc->toecaps) {
5859 		/* query offload-related parameters */
5860 		param[0] = FW_PARAM_DEV(NTID);
5861 		param[1] = FW_PARAM_PFVF(SERVER_START);
5862 		param[2] = FW_PARAM_PFVF(SERVER_END);
5863 		param[3] = FW_PARAM_PFVF(TDDP_START);
5864 		param[4] = FW_PARAM_PFVF(TDDP_END);
5865 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5866 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5867 		if (rc != 0) {
5868 			device_printf(sc->dev,
5869 			    "failed to query TOE parameters: %d.\n", rc);
5870 			return (rc);
5871 		}
5872 		sc->tids.ntids = val[0];
5873 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5874 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5875 			sc->tids.ntids -= sc->tids.nhpftids;
5876 		}
5877 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5878 		if ((int)val[2] > (int)val[1]) {
5879 			sc->tids.stid_base = val[1];
5880 			sc->tids.nstids = val[2] - val[1] + 1;
5881 		}
5882 		sc->vres.ddp.start = val[3];
5883 		sc->vres.ddp.size = val[4] - val[3] + 1;
5884 		sc->params.ofldq_wr_cred = val[5];
5885 		sc->params.offload = 1;
5886 	} else {
5887 		/*
5888 		 * The firmware attempts memfree TOE configuration for -SO cards
5889 		 * and will report toecaps=0 if it runs out of resources (this
5890 		 * depends on the config file).  It may not report 0 for other
5891 		 * capabilities dependent on the TOE in this case.  Set them to
5892 		 * 0 here so that the driver doesn't bother tracking resources
5893 		 * that will never be used.
5894 		 */
5895 		sc->iscsicaps = 0;
5896 		sc->nvmecaps = 0;
5897 		sc->rdmacaps = 0;
5898 	}
5899 	if (sc->nvmecaps || sc->rdmacaps) {
5900 		param[0] = FW_PARAM_PFVF(STAG_START);
5901 		param[1] = FW_PARAM_PFVF(STAG_END);
5902 		param[2] = FW_PARAM_PFVF(PBL_START);
5903 		param[3] = FW_PARAM_PFVF(PBL_END);
5904 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5905 		if (rc != 0) {
5906 			device_printf(sc->dev,
5907 			    "failed to query NVMe/RDMA parameters: %d.\n", rc);
5908 			return (rc);
5909 		}
5910 		sc->vres.stag.start = val[0];
5911 		sc->vres.stag.size = val[1] - val[0] + 1;
5912 		sc->vres.pbl.start = val[2];
5913 		sc->vres.pbl.size = val[3] - val[2] + 1;
5914 	}
5915 	if (sc->rdmacaps) {
5916 		param[0] = FW_PARAM_PFVF(RQ_START);
5917 		param[1] = FW_PARAM_PFVF(RQ_END);
5918 		param[2] = FW_PARAM_PFVF(SQRQ_START);
5919 		param[3] = FW_PARAM_PFVF(SQRQ_END);
5920 		param[4] = FW_PARAM_PFVF(CQ_START);
5921 		param[5] = FW_PARAM_PFVF(CQ_END);
5922 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5923 		if (rc != 0) {
5924 			device_printf(sc->dev,
5925 			    "failed to query RDMA parameters(1): %d.\n", rc);
5926 			return (rc);
5927 		}
5928 		sc->vres.rq.start = val[0];
5929 		sc->vres.rq.size = val[1] - val[0] + 1;
5930 		sc->vres.qp.start = val[2];
5931 		sc->vres.qp.size = val[3] - val[2] + 1;
5932 		sc->vres.cq.start = val[4];
5933 		sc->vres.cq.size = val[5] - val[4] + 1;
5934 
5935 		param[0] = FW_PARAM_PFVF(OCQ_START);
5936 		param[1] = FW_PARAM_PFVF(OCQ_END);
5937 		param[2] = FW_PARAM_PFVF(SRQ_START);
5938 		param[3] = FW_PARAM_PFVF(SRQ_END);
5939 		param[4] = FW_PARAM_DEV(MAXORDIRD_QP);
5940 		param[5] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5941 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5942 		if (rc != 0) {
5943 			device_printf(sc->dev,
5944 			    "failed to query RDMA parameters(2): %d.\n", rc);
5945 			return (rc);
5946 		}
5947 		sc->vres.ocq.start = val[0];
5948 		sc->vres.ocq.size = val[1] - val[0] + 1;
5949 		sc->vres.srq.start = val[2];
5950 		sc->vres.srq.size = val[3] - val[2] + 1;
5951 		sc->params.max_ordird_qp = val[4];
5952 		sc->params.max_ird_adapter = val[5];
5953 	}
5954 	if (sc->iscsicaps) {
5955 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5956 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5957 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5958 		if (rc != 0) {
5959 			device_printf(sc->dev,
5960 			    "failed to query iSCSI parameters: %d.\n", rc);
5961 			return (rc);
5962 		}
5963 		sc->vres.iscsi.start = val[0];
5964 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5965 	}
5966 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5967 		param[0] = FW_PARAM_PFVF(TLS_START);
5968 		param[1] = FW_PARAM_PFVF(TLS_END);
5969 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5970 		if (rc != 0) {
5971 			device_printf(sc->dev,
5972 			    "failed to query TLS parameters: %d.\n", rc);
5973 			return (rc);
5974 		}
5975 		sc->vres.key.start = val[0];
5976 		sc->vres.key.size = val[1] - val[0] + 1;
5977 	}
5978 
5979 	/*
5980 	 * We've got the params we wanted to query directly from the firmware.
5981 	 * Grab some others via other means.
5982 	 */
5983 	t4_init_sge_params(sc);
5984 	t4_init_tp_params(sc);
5985 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5986 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5987 
5988 	rc = t4_verify_chip_settings(sc);
5989 	if (rc != 0)
5990 		return (rc);
5991 	t4_init_rx_buf_info(sc);
5992 
5993 	return (rc);
5994 }
5995 
5996 #ifdef KERN_TLS
5997 static void
ktls_tick(void * arg)5998 ktls_tick(void *arg)
5999 {
6000 	struct adapter *sc;
6001 	uint32_t tstamp;
6002 
6003 	sc = arg;
6004 	tstamp = tcp_ts_getticks();
6005 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
6006 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
6007 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
6008 }
6009 
6010 static int
t6_config_kern_tls(struct adapter * sc,bool enable)6011 t6_config_kern_tls(struct adapter *sc, bool enable)
6012 {
6013 	int rc;
6014 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
6015 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
6016 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
6017 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
6018 
6019 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
6020 	if (rc != 0) {
6021 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
6022 		    enable ?  "enable" : "disable", rc);
6023 		return (rc);
6024 	}
6025 
6026 	if (enable) {
6027 		sc->flags |= KERN_TLS_ON;
6028 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
6029 		    C_HARDCLOCK);
6030 	} else {
6031 		sc->flags &= ~KERN_TLS_ON;
6032 		callout_stop(&sc->ktls_tick);
6033 	}
6034 
6035 	return (rc);
6036 }
6037 #endif
6038 
6039 static int
set_params__post_init(struct adapter * sc)6040 set_params__post_init(struct adapter *sc)
6041 {
6042 	uint32_t mask, param, val;
6043 #ifdef TCP_OFFLOAD
6044 	int i, v, shift;
6045 #endif
6046 
6047 	/* ask for encapsulated CPLs */
6048 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
6049 	val = 1;
6050 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
6051 
6052 	/* Enable 32b port caps if the firmware supports it. */
6053 	param = FW_PARAM_PFVF(PORT_CAPS32);
6054 	val = 1;
6055 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
6056 		sc->params.port_caps32 = 1;
6057 
6058 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
6059 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
6060 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
6061 	    V_MASKFILTER(val - 1));
6062 
6063 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
6064 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
6065 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
6066 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
6067 	val = 0;
6068 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
6069 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
6070 		    F_ATTACKFILTERENABLE);
6071 		val |= F_DROPERRORATTACK;
6072 	}
6073 	if (t4_drop_ip_fragments != 0) {
6074 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
6075 		    F_FRAGMENTDROP);
6076 		val |= F_DROPERRORFRAG;
6077 	}
6078 	if (t4_drop_pkts_with_l2_errors != 0)
6079 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
6080 	if (t4_drop_pkts_with_l3_errors != 0) {
6081 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
6082 		    F_DROPERRORCSUMIP;
6083 	}
6084 	if (t4_drop_pkts_with_l4_errors != 0) {
6085 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
6086 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
6087 	}
6088 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
6089 
6090 #ifdef TCP_OFFLOAD
6091 	/*
6092 	 * Override the TOE timers with user provided tunables.  This is not the
6093 	 * recommended way to change the timers (the firmware config file is) so
6094 	 * these tunables are not documented.
6095 	 *
6096 	 * All the timer tunables are in microseconds.
6097 	 */
6098 	if (t4_toe_keepalive_idle != 0) {
6099 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
6100 		v &= M_KEEPALIVEIDLE;
6101 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
6102 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
6103 	}
6104 	if (t4_toe_keepalive_interval != 0) {
6105 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
6106 		v &= M_KEEPALIVEINTVL;
6107 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
6108 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
6109 	}
6110 	if (t4_toe_keepalive_count != 0) {
6111 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
6112 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
6113 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
6114 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
6115 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
6116 	}
6117 	if (t4_toe_rexmt_min != 0) {
6118 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
6119 		v &= M_RXTMIN;
6120 		t4_set_reg_field(sc, A_TP_RXT_MIN,
6121 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
6122 	}
6123 	if (t4_toe_rexmt_max != 0) {
6124 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
6125 		v &= M_RXTMAX;
6126 		t4_set_reg_field(sc, A_TP_RXT_MAX,
6127 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
6128 	}
6129 	if (t4_toe_rexmt_count != 0) {
6130 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
6131 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
6132 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
6133 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
6134 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
6135 	}
6136 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
6137 		if (t4_toe_rexmt_backoff[i] != -1) {
6138 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
6139 			shift = (i & 3) << 3;
6140 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
6141 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
6142 		}
6143 	}
6144 #endif
6145 
6146 	/*
6147 	 * Limit TOE connections to 2 reassembly "islands".  This is
6148 	 * required to permit migrating TOE connections to either
6149 	 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
6150 	 */
6151 	t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
6152 	    V_PASSMODE(2));
6153 
6154 #ifdef KERN_TLS
6155 	if (is_ktls(sc)) {
6156 		sc->tlst.inline_keys = t4_tls_inline_keys;
6157 		if (t4_kern_tls != 0 && is_t6(sc)) {
6158 			sc->tlst.combo_wrs = t4_tls_combo_wrs;
6159 			t6_config_kern_tls(sc, true);
6160 		} else {
6161 			sc->tlst.short_records = t4_tls_short_records;
6162 			sc->tlst.partial_ghash = t4_tls_partial_ghash;
6163 		}
6164 	}
6165 #endif
6166 	return (0);
6167 }
6168 
6169 #undef FW_PARAM_PFVF
6170 #undef FW_PARAM_DEV
6171 
6172 static void
t4_set_desc(struct adapter * sc)6173 t4_set_desc(struct adapter *sc)
6174 {
6175 	struct adapter_params *p = &sc->params;
6176 
6177 	device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
6178 }
6179 
6180 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)6181 ifmedia_add4(struct ifmedia *ifm, int m)
6182 {
6183 
6184 	ifmedia_add(ifm, m, 0, NULL);
6185 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
6186 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
6187 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
6188 }
6189 
6190 /*
6191  * This is the selected media, which is not quite the same as the active media.
6192  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
6193  * and active are not the same, and "media: Ethernet selected" otherwise.
6194  */
6195 static void
set_current_media(struct port_info * pi)6196 set_current_media(struct port_info *pi)
6197 {
6198 	struct link_config *lc;
6199 	struct ifmedia *ifm;
6200 	int mword;
6201 	u_int speed;
6202 
6203 	PORT_LOCK_ASSERT_OWNED(pi);
6204 
6205 	/* Leave current media alone if it's already set to IFM_NONE. */
6206 	ifm = &pi->media;
6207 	if (ifm->ifm_cur != NULL &&
6208 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
6209 		return;
6210 
6211 	lc = &pi->link_cfg;
6212 	if (lc->requested_aneg != AUTONEG_DISABLE &&
6213 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
6214 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
6215 		return;
6216 	}
6217 	mword = IFM_ETHER | IFM_FDX;
6218 	if (lc->requested_fc & PAUSE_TX)
6219 		mword |= IFM_ETH_TXPAUSE;
6220 	if (lc->requested_fc & PAUSE_RX)
6221 		mword |= IFM_ETH_RXPAUSE;
6222 	if (lc->requested_speed == 0)
6223 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
6224 	else
6225 		speed = lc->requested_speed;
6226 	mword |= port_mword(pi, speed_to_fwcap(speed));
6227 	ifmedia_set(ifm, mword);
6228 }
6229 
6230 /*
6231  * Returns true if the ifmedia list for the port cannot change.
6232  */
6233 static bool
fixed_ifmedia(struct port_info * pi)6234 fixed_ifmedia(struct port_info *pi)
6235 {
6236 
6237 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
6238 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
6239 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
6240 	    pi->port_type == FW_PORT_TYPE_KX4 ||
6241 	    pi->port_type == FW_PORT_TYPE_KX ||
6242 	    pi->port_type == FW_PORT_TYPE_KR ||
6243 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
6244 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
6245 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
6246 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
6247 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
6248 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
6249 }
6250 
6251 static void
build_medialist(struct port_info * pi)6252 build_medialist(struct port_info *pi)
6253 {
6254 	uint32_t ss, speed;
6255 	int unknown, mword, bit;
6256 	struct link_config *lc;
6257 	struct ifmedia *ifm;
6258 
6259 	PORT_LOCK_ASSERT_OWNED(pi);
6260 
6261 	if (pi->flags & FIXED_IFMEDIA)
6262 		return;
6263 
6264 	/*
6265 	 * Rebuild the ifmedia list.
6266 	 */
6267 	ifm = &pi->media;
6268 	ifmedia_removeall(ifm);
6269 	lc = &pi->link_cfg;
6270 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
6271 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
6272 		MPASS(ss != 0);
6273 no_media:
6274 		MPASS(LIST_EMPTY(&ifm->ifm_list));
6275 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
6276 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
6277 		return;
6278 	}
6279 
6280 	unknown = 0;
6281 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
6282 		speed = 1 << bit;
6283 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
6284 		if (ss & speed) {
6285 			mword = port_mword(pi, speed);
6286 			if (mword == IFM_NONE) {
6287 				goto no_media;
6288 			} else if (mword == IFM_UNKNOWN)
6289 				unknown++;
6290 			else
6291 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
6292 		}
6293 	}
6294 	if (unknown > 0) /* Add one unknown for all unknown media types. */
6295 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
6296 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
6297 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
6298 
6299 	set_current_media(pi);
6300 }
6301 
6302 /*
6303  * Initialize the requested fields in the link config based on driver tunables.
6304  */
6305 static void
init_link_config(struct port_info * pi)6306 init_link_config(struct port_info *pi)
6307 {
6308 	struct link_config *lc = &pi->link_cfg;
6309 
6310 	PORT_LOCK_ASSERT_OWNED(pi);
6311 
6312 	lc->requested_caps = 0;
6313 	lc->requested_speed = 0;
6314 
6315 	if (t4_autoneg == 0)
6316 		lc->requested_aneg = AUTONEG_DISABLE;
6317 	else if (t4_autoneg == 1)
6318 		lc->requested_aneg = AUTONEG_ENABLE;
6319 	else
6320 		lc->requested_aneg = AUTONEG_AUTO;
6321 
6322 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
6323 	    PAUSE_AUTONEG);
6324 
6325 	if (t4_fec & FEC_AUTO)
6326 		lc->requested_fec = FEC_AUTO;
6327 	else if (t4_fec == 0)
6328 		lc->requested_fec = FEC_NONE;
6329 	else {
6330 		/* -1 is handled by the FEC_AUTO block above and not here. */
6331 		lc->requested_fec = t4_fec &
6332 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6333 		if (lc->requested_fec == 0)
6334 			lc->requested_fec = FEC_AUTO;
6335 	}
6336 	if (t4_force_fec < 0)
6337 		lc->force_fec = -1;
6338 	else if (t4_force_fec > 0)
6339 		lc->force_fec = 1;
6340 	else
6341 		lc->force_fec = 0;
6342 }
6343 
6344 /*
6345  * Makes sure that all requested settings comply with what's supported by the
6346  * port.  Returns the number of settings that were invalid and had to be fixed.
6347  */
6348 static int
fixup_link_config(struct port_info * pi)6349 fixup_link_config(struct port_info *pi)
6350 {
6351 	int n = 0;
6352 	struct link_config *lc = &pi->link_cfg;
6353 	uint32_t fwspeed;
6354 
6355 	PORT_LOCK_ASSERT_OWNED(pi);
6356 
6357 	/* Speed (when not autonegotiating) */
6358 	if (lc->requested_speed != 0) {
6359 		fwspeed = speed_to_fwcap(lc->requested_speed);
6360 		if ((fwspeed & lc->pcaps) == 0) {
6361 			n++;
6362 			lc->requested_speed = 0;
6363 		}
6364 	}
6365 
6366 	/* Link autonegotiation */
6367 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6368 	    lc->requested_aneg == AUTONEG_DISABLE ||
6369 	    lc->requested_aneg == AUTONEG_AUTO);
6370 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6371 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6372 		n++;
6373 		lc->requested_aneg = AUTONEG_AUTO;
6374 	}
6375 
6376 	/* Flow control */
6377 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6378 	if (lc->requested_fc & PAUSE_TX &&
6379 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6380 		n++;
6381 		lc->requested_fc &= ~PAUSE_TX;
6382 	}
6383 	if (lc->requested_fc & PAUSE_RX &&
6384 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6385 		n++;
6386 		lc->requested_fc &= ~PAUSE_RX;
6387 	}
6388 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6389 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6390 		n++;
6391 		lc->requested_fc |= PAUSE_AUTONEG;
6392 	}
6393 
6394 	/* FEC */
6395 	if ((lc->requested_fec & FEC_RS &&
6396 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6397 	    (lc->requested_fec & FEC_BASER_RS &&
6398 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6399 		n++;
6400 		lc->requested_fec = FEC_AUTO;
6401 	}
6402 
6403 	return (n);
6404 }
6405 
6406 /*
6407  * Apply the requested L1 settings, which are expected to be valid, to the
6408  * hardware.
6409  */
6410 static int
apply_link_config(struct port_info * pi)6411 apply_link_config(struct port_info *pi)
6412 {
6413 	struct adapter *sc = pi->adapter;
6414 	struct link_config *lc = &pi->link_cfg;
6415 	int rc;
6416 
6417 #ifdef INVARIANTS
6418 	ASSERT_SYNCHRONIZED_OP(sc);
6419 	PORT_LOCK_ASSERT_OWNED(pi);
6420 
6421 	if (lc->requested_aneg == AUTONEG_ENABLE)
6422 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6423 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6424 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6425 	if (lc->requested_fc & PAUSE_TX)
6426 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6427 	if (lc->requested_fc & PAUSE_RX)
6428 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6429 	if (lc->requested_fec & FEC_RS)
6430 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6431 	if (lc->requested_fec & FEC_BASER_RS)
6432 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6433 #endif
6434 	if (!(sc->flags & IS_VF)) {
6435 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->hw_port, lc);
6436 		if (rc != 0) {
6437 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6438 			return (rc);
6439 		}
6440 	}
6441 
6442 	/*
6443 	 * An L1_CFG will almost always result in a link-change event if the
6444 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6445 	 * the notification is processed.  If the link is down then the actual
6446 	 * settings are meaningless.
6447 	 *
6448 	 * This takes care of the case where a change in the L1 settings may not
6449 	 * result in a notification.
6450 	 */
6451 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6452 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6453 
6454 	return (0);
6455 }
6456 
6457 #define FW_MAC_EXACT_CHUNK	7
6458 struct mcaddr_ctx {
6459 	if_t ifp;
6460 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6461 	uint64_t hash;
6462 	int i;
6463 	int del;
6464 	int rc;
6465 };
6466 
6467 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6468 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6469 {
6470 	struct mcaddr_ctx *ctx = arg;
6471 	struct vi_info *vi = if_getsoftc(ctx->ifp);
6472 	struct port_info *pi = vi->pi;
6473 	struct adapter *sc = pi->adapter;
6474 
6475 	if (ctx->rc < 0)
6476 		return (0);
6477 
6478 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6479 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6480 	ctx->i++;
6481 
6482 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6483 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6484 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6485 		if (ctx->rc < 0) {
6486 			int j;
6487 
6488 			for (j = 0; j < ctx->i; j++) {
6489 				if_printf(ctx->ifp,
6490 				    "failed to add mc address"
6491 				    " %02x:%02x:%02x:"
6492 				    "%02x:%02x:%02x rc=%d\n",
6493 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6494 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6495 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6496 				    -ctx->rc);
6497 			}
6498 			return (0);
6499 		}
6500 		ctx->del = 0;
6501 		ctx->i = 0;
6502 	}
6503 
6504 	return (1);
6505 }
6506 
6507 /*
6508  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6509  * indicates which parameters should be programmed (the rest are left alone).
6510  */
6511 int
update_mac_settings(if_t ifp,int flags)6512 update_mac_settings(if_t ifp, int flags)
6513 {
6514 	int rc = 0;
6515 	struct vi_info *vi = if_getsoftc(ifp);
6516 	struct port_info *pi = vi->pi;
6517 	struct adapter *sc = pi->adapter;
6518 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6519 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6520 
6521 	ASSERT_SYNCHRONIZED_OP(sc);
6522 	KASSERT(flags, ("%s: not told what to update.", __func__));
6523 
6524 	if (flags & XGMAC_MTU)
6525 		mtu = if_getmtu(ifp);
6526 
6527 	if (flags & XGMAC_PROMISC)
6528 		promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6529 
6530 	if (flags & XGMAC_ALLMULTI)
6531 		allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6532 
6533 	if (flags & XGMAC_VLANEX)
6534 		vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6535 
6536 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6537 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6538 		    allmulti, 1, vlanex, false);
6539 		if (rc) {
6540 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6541 			    rc);
6542 			return (rc);
6543 		}
6544 	}
6545 
6546 	if (flags & XGMAC_UCADDR) {
6547 		uint8_t ucaddr[ETHER_ADDR_LEN];
6548 
6549 		bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6550 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6551 		    ucaddr, true, &vi->smt_idx);
6552 		if (rc < 0) {
6553 			rc = -rc;
6554 			if_printf(ifp, "change_mac failed: %d\n", rc);
6555 			return (rc);
6556 		} else {
6557 			vi->xact_addr_filt = rc;
6558 			rc = 0;
6559 		}
6560 	}
6561 
6562 	if (flags & XGMAC_MCADDRS) {
6563 		struct epoch_tracker et;
6564 		struct mcaddr_ctx ctx;
6565 		int j;
6566 
6567 		ctx.ifp = ifp;
6568 		ctx.hash = 0;
6569 		ctx.i = 0;
6570 		ctx.del = 1;
6571 		ctx.rc = 0;
6572 		/*
6573 		 * Unlike other drivers, we accumulate list of pointers into
6574 		 * interface address lists and we need to keep it safe even
6575 		 * after if_foreach_llmaddr() returns, thus we must enter the
6576 		 * network epoch.
6577 		 */
6578 		NET_EPOCH_ENTER(et);
6579 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6580 		if (ctx.rc < 0) {
6581 			NET_EPOCH_EXIT(et);
6582 			rc = -ctx.rc;
6583 			return (rc);
6584 		}
6585 		if (ctx.i > 0) {
6586 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6587 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6588 			NET_EPOCH_EXIT(et);
6589 			if (rc < 0) {
6590 				rc = -rc;
6591 				for (j = 0; j < ctx.i; j++) {
6592 					if_printf(ifp,
6593 					    "failed to add mcast address"
6594 					    " %02x:%02x:%02x:"
6595 					    "%02x:%02x:%02x rc=%d\n",
6596 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6597 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6598 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6599 					    rc);
6600 				}
6601 				return (rc);
6602 			}
6603 			ctx.del = 0;
6604 		} else
6605 			NET_EPOCH_EXIT(et);
6606 
6607 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6608 		if (rc != 0)
6609 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6610 			    rc);
6611 		if (ctx.del == 0) {
6612 			/* We clobbered the VXLAN entry if there was one. */
6613 			pi->vxlan_tcam_entry = false;
6614 		}
6615 	}
6616 
6617 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6618 	    pi->vxlan_tcam_entry == false) {
6619 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6620 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6621 		    true);
6622 		if (rc < 0) {
6623 			rc = -rc;
6624 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6625 			    rc);
6626 		} else {
6627 			MPASS(rc == sc->rawf_base + pi->port_id);
6628 			rc = 0;
6629 			pi->vxlan_tcam_entry = true;
6630 		}
6631 	}
6632 
6633 	return (rc);
6634 }
6635 
6636 /*
6637  * {begin|end}_synchronized_op must be called from the same thread.
6638  */
6639 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6640 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6641     char *wmesg)
6642 {
6643 	int rc;
6644 
6645 #ifdef WITNESS
6646 	/* the caller thinks it's ok to sleep, but is it really? */
6647 	if (flags & SLEEP_OK)
6648 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
6649 #endif
6650 	ADAPTER_LOCK(sc);
6651 	for (;;) {
6652 
6653 		if (vi && IS_DETACHING(vi)) {
6654 			rc = ENXIO;
6655 			goto done;
6656 		}
6657 
6658 		if (!IS_BUSY(sc)) {
6659 			rc = 0;
6660 			break;
6661 		}
6662 
6663 		if (!(flags & SLEEP_OK)) {
6664 			rc = EBUSY;
6665 			goto done;
6666 		}
6667 
6668 		if (mtx_sleep(&sc->flags, &sc->sc_lock,
6669 		    flags & INTR_OK ? PCATCH : 0, wmesg, 0)) {
6670 			rc = EINTR;
6671 			goto done;
6672 		}
6673 	}
6674 
6675 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6676 	SET_BUSY(sc);
6677 #ifdef INVARIANTS
6678 	sc->last_op = wmesg;
6679 	sc->last_op_thr = curthread;
6680 	sc->last_op_flags = flags;
6681 #endif
6682 
6683 done:
6684 	if (!(flags & HOLD_LOCK) || rc)
6685 		ADAPTER_UNLOCK(sc);
6686 
6687 	return (rc);
6688 }
6689 
6690 /*
6691  * Tell if_ioctl and if_init that the VI is going away.  This is
6692  * special variant of begin_synchronized_op and must be paired with a
6693  * call to end_vi_detach.
6694  */
6695 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6696 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6697 {
6698 	ADAPTER_LOCK(sc);
6699 	SET_DETACHING(vi);
6700 	wakeup(&sc->flags);
6701 	while (IS_BUSY(sc))
6702 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6703 	SET_BUSY(sc);
6704 #ifdef INVARIANTS
6705 	sc->last_op = "t4detach";
6706 	sc->last_op_thr = curthread;
6707 	sc->last_op_flags = 0;
6708 #endif
6709 	ADAPTER_UNLOCK(sc);
6710 }
6711 
6712 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6713 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6714 {
6715 	ADAPTER_LOCK(sc);
6716 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6717 	CLR_BUSY(sc);
6718 	CLR_DETACHING(vi);
6719 	wakeup(&sc->flags);
6720 	ADAPTER_UNLOCK(sc);
6721 }
6722 
6723 /*
6724  * {begin|end}_synchronized_op must be called from the same thread.
6725  */
6726 void
end_synchronized_op(struct adapter * sc,int flags)6727 end_synchronized_op(struct adapter *sc, int flags)
6728 {
6729 
6730 	if (flags & LOCK_HELD)
6731 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6732 	else
6733 		ADAPTER_LOCK(sc);
6734 
6735 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6736 	CLR_BUSY(sc);
6737 	wakeup(&sc->flags);
6738 	ADAPTER_UNLOCK(sc);
6739 }
6740 
6741 static int
cxgbe_init_synchronized(struct vi_info * vi)6742 cxgbe_init_synchronized(struct vi_info *vi)
6743 {
6744 	struct port_info *pi = vi->pi;
6745 	struct adapter *sc = pi->adapter;
6746 	if_t ifp = vi->ifp;
6747 	int rc = 0, i;
6748 	struct sge_txq *txq;
6749 
6750 	ASSERT_SYNCHRONIZED_OP(sc);
6751 
6752 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6753 		return (0);	/* already running */
6754 
6755 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6756 		return (rc);	/* error message displayed already */
6757 
6758 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6759 		return (rc); /* error message displayed already */
6760 
6761 	rc = update_mac_settings(ifp, XGMAC_ALL);
6762 	if (rc)
6763 		goto done;	/* error message displayed already */
6764 
6765 	PORT_LOCK(pi);
6766 	if (pi->up_vis == 0) {
6767 		t4_update_port_info(pi);
6768 		fixup_link_config(pi);
6769 		build_medialist(pi);
6770 		apply_link_config(pi);
6771 	}
6772 
6773 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6774 	if (rc != 0) {
6775 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6776 		PORT_UNLOCK(pi);
6777 		goto done;
6778 	}
6779 
6780 	/*
6781 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6782 	 * if this changes.
6783 	 */
6784 
6785 	for_each_txq(vi, i, txq) {
6786 		TXQ_LOCK(txq);
6787 		txq->eq.flags |= EQ_ENABLED;
6788 		TXQ_UNLOCK(txq);
6789 	}
6790 
6791 	/*
6792 	 * The first iq of the first port to come up is used for tracing.
6793 	 */
6794 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6795 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6796 		t4_set_trace_rss_control(sc, pi->tx_chan, sc->traceq);
6797 		pi->flags |= HAS_TRACEQ;
6798 	}
6799 
6800 	/* all ok */
6801 	pi->up_vis++;
6802 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6803 	if (pi->link_cfg.link_ok)
6804 		t4_os_link_changed(pi);
6805 	PORT_UNLOCK(pi);
6806 
6807 	mtx_lock(&vi->tick_mtx);
6808 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6809 		callout_reset(&vi->tick, hz, vi_tick, vi);
6810 	else
6811 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6812 	mtx_unlock(&vi->tick_mtx);
6813 done:
6814 	if (rc != 0)
6815 		cxgbe_uninit_synchronized(vi);
6816 
6817 	return (rc);
6818 }
6819 
6820 /*
6821  * Idempotent.
6822  */
6823 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6824 cxgbe_uninit_synchronized(struct vi_info *vi)
6825 {
6826 	struct port_info *pi = vi->pi;
6827 	struct adapter *sc = pi->adapter;
6828 	if_t ifp = vi->ifp;
6829 	int rc, i;
6830 	struct sge_txq *txq;
6831 
6832 	ASSERT_SYNCHRONIZED_OP(sc);
6833 
6834 	if (!(vi->flags & VI_INIT_DONE)) {
6835 		if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6836 			KASSERT(0, ("uninited VI is running"));
6837 			if_printf(ifp, "uninited VI with running ifnet.  "
6838 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6839 			    "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6840 			    if_getdrvflags(ifp));
6841 		}
6842 		return (0);
6843 	}
6844 
6845 	/*
6846 	 * Disable the VI so that all its data in either direction is discarded
6847 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6848 	 * tick) intact as the TP can deliver negative advice or data that it's
6849 	 * holding in its RAM (for an offloaded connection) even after the VI is
6850 	 * disabled.
6851 	 */
6852 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6853 	if (rc) {
6854 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6855 		return (rc);
6856 	}
6857 
6858 	for_each_txq(vi, i, txq) {
6859 		TXQ_LOCK(txq);
6860 		txq->eq.flags &= ~EQ_ENABLED;
6861 		TXQ_UNLOCK(txq);
6862 	}
6863 
6864 	mtx_lock(&vi->tick_mtx);
6865 	callout_stop(&vi->tick);
6866 	mtx_unlock(&vi->tick_mtx);
6867 
6868 	PORT_LOCK(pi);
6869 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6870 		PORT_UNLOCK(pi);
6871 		return (0);
6872 	}
6873 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6874 	pi->up_vis--;
6875 	if (pi->up_vis > 0) {
6876 		PORT_UNLOCK(pi);
6877 		return (0);
6878 	}
6879 
6880 	pi->link_cfg.link_ok = false;
6881 	pi->link_cfg.speed = 0;
6882 	pi->link_cfg.link_down_rc = 255;
6883 	t4_os_link_changed(pi);
6884 	PORT_UNLOCK(pi);
6885 
6886 	return (0);
6887 }
6888 
6889 /*
6890  * It is ok for this function to fail midway and return right away.  t4_detach
6891  * will walk the entire sc->irq list and clean up whatever is valid.
6892  */
6893 int
t4_setup_intr_handlers(struct adapter * sc)6894 t4_setup_intr_handlers(struct adapter *sc)
6895 {
6896 	int rc, rid, p, q, v;
6897 	char s[8];
6898 	struct irq *irq;
6899 	struct port_info *pi;
6900 	struct vi_info *vi;
6901 	struct sge *sge = &sc->sge;
6902 	struct sge_rxq *rxq;
6903 #ifdef TCP_OFFLOAD
6904 	struct sge_ofld_rxq *ofld_rxq;
6905 #endif
6906 #ifdef DEV_NETMAP
6907 	struct sge_nm_rxq *nm_rxq;
6908 #endif
6909 #ifdef RSS
6910 	int nbuckets = rss_getnumbuckets();
6911 #endif
6912 
6913 	/*
6914 	 * Setup interrupts.
6915 	 */
6916 	irq = &sc->irq[0];
6917 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6918 	if (forwarding_intr_to_fwq(sc))
6919 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6920 
6921 	/* Multiple interrupts. */
6922 	if (sc->flags & IS_VF)
6923 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6924 		    ("%s: too few intr.", __func__));
6925 	else
6926 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6927 		    ("%s: too few intr.", __func__));
6928 
6929 	/* The first one is always error intr on PFs */
6930 	if (!(sc->flags & IS_VF)) {
6931 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6932 		if (rc != 0)
6933 			return (rc);
6934 		irq++;
6935 		rid++;
6936 	}
6937 
6938 	/* The second one is always the firmware event queue (first on VFs) */
6939 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6940 	if (rc != 0)
6941 		return (rc);
6942 	irq++;
6943 	rid++;
6944 
6945 	for_each_port(sc, p) {
6946 		pi = sc->port[p];
6947 		for_each_vi(pi, v, vi) {
6948 			vi->first_intr = rid - 1;
6949 
6950 			if (vi->nnmrxq > 0) {
6951 				int n = max(vi->nrxq, vi->nnmrxq);
6952 
6953 				rxq = &sge->rxq[vi->first_rxq];
6954 #ifdef DEV_NETMAP
6955 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6956 #endif
6957 				for (q = 0; q < n; q++) {
6958 					snprintf(s, sizeof(s), "%x%c%x", p,
6959 					    'a' + v, q);
6960 					if (q < vi->nrxq)
6961 						irq->rxq = rxq++;
6962 #ifdef DEV_NETMAP
6963 					if (q < vi->nnmrxq)
6964 						irq->nm_rxq = nm_rxq++;
6965 
6966 					if (irq->nm_rxq != NULL &&
6967 					    irq->rxq == NULL) {
6968 						/* Netmap rx only */
6969 						rc = t4_alloc_irq(sc, irq, rid,
6970 						    t4_nm_intr, irq->nm_rxq, s);
6971 					}
6972 					if (irq->nm_rxq != NULL &&
6973 					    irq->rxq != NULL) {
6974 						/* NIC and Netmap rx */
6975 						rc = t4_alloc_irq(sc, irq, rid,
6976 						    t4_vi_intr, irq, s);
6977 					}
6978 #endif
6979 					if (irq->rxq != NULL &&
6980 					    irq->nm_rxq == NULL) {
6981 						/* NIC rx only */
6982 						rc = t4_alloc_irq(sc, irq, rid,
6983 						    t4_intr, irq->rxq, s);
6984 					}
6985 					if (rc != 0)
6986 						return (rc);
6987 #ifdef RSS
6988 					if (q < vi->nrxq) {
6989 						bus_bind_intr(sc->dev, irq->res,
6990 						    rss_getcpu(q % nbuckets));
6991 					}
6992 #endif
6993 					irq++;
6994 					rid++;
6995 					vi->nintr++;
6996 				}
6997 			} else {
6998 				for_each_rxq(vi, q, rxq) {
6999 					snprintf(s, sizeof(s), "%x%c%x", p,
7000 					    'a' + v, q);
7001 					rc = t4_alloc_irq(sc, irq, rid,
7002 					    t4_intr, rxq, s);
7003 					if (rc != 0)
7004 						return (rc);
7005 #ifdef RSS
7006 					bus_bind_intr(sc->dev, irq->res,
7007 					    rss_getcpu(q % nbuckets));
7008 #endif
7009 					irq++;
7010 					rid++;
7011 					vi->nintr++;
7012 				}
7013 			}
7014 #ifdef TCP_OFFLOAD
7015 			for_each_ofld_rxq(vi, q, ofld_rxq) {
7016 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
7017 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
7018 				    ofld_rxq, s);
7019 				if (rc != 0)
7020 					return (rc);
7021 				irq++;
7022 				rid++;
7023 				vi->nintr++;
7024 			}
7025 #endif
7026 		}
7027 	}
7028 	MPASS(irq == &sc->irq[sc->intr_count]);
7029 
7030 	return (0);
7031 }
7032 
7033 static void
write_global_rss_key(struct adapter * sc)7034 write_global_rss_key(struct adapter *sc)
7035 {
7036 	int i;
7037 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
7038 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
7039 
7040 	CTASSERT(RSS_KEYSIZE == 40);
7041 
7042 	rss_getkey((void *)&raw_rss_key[0]);
7043 	for (i = 0; i < nitems(rss_key); i++) {
7044 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
7045 	}
7046 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
7047 }
7048 
7049 /*
7050  * Idempotent.
7051  */
7052 static int
adapter_full_init(struct adapter * sc)7053 adapter_full_init(struct adapter *sc)
7054 {
7055 	int rc, i;
7056 
7057 	ASSERT_SYNCHRONIZED_OP(sc);
7058 
7059 	/*
7060 	 * queues that belong to the adapter (not any particular port).
7061 	 */
7062 	rc = t4_setup_adapter_queues(sc);
7063 	if (rc != 0)
7064 		return (rc);
7065 
7066 	MPASS(sc->params.nports <= nitems(sc->tq));
7067 	for (i = 0; i < sc->params.nports; i++) {
7068 		if (sc->tq[i] != NULL)
7069 			continue;
7070 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
7071 		    taskqueue_thread_enqueue, &sc->tq[i]);
7072 		if (sc->tq[i] == NULL) {
7073 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
7074 			return (ENOMEM);
7075 		}
7076 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
7077 		    device_get_nameunit(sc->dev), i);
7078 	}
7079 
7080 	if (!(sc->flags & IS_VF)) {
7081 		write_global_rss_key(sc);
7082 		t4_intr_enable(sc);
7083 	}
7084 	return (0);
7085 }
7086 
7087 int
adapter_init(struct adapter * sc)7088 adapter_init(struct adapter *sc)
7089 {
7090 	int rc;
7091 
7092 	ASSERT_SYNCHRONIZED_OP(sc);
7093 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
7094 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
7095 	    ("%s: FULL_INIT_DONE already", __func__));
7096 
7097 	rc = adapter_full_init(sc);
7098 	if (rc != 0)
7099 		adapter_full_uninit(sc);
7100 	else
7101 		sc->flags |= FULL_INIT_DONE;
7102 
7103 	return (rc);
7104 }
7105 
7106 /*
7107  * Idempotent.
7108  */
7109 static void
adapter_full_uninit(struct adapter * sc)7110 adapter_full_uninit(struct adapter *sc)
7111 {
7112 	int i;
7113 
7114 	t4_teardown_adapter_queues(sc);
7115 
7116 	for (i = 0; i < nitems(sc->tq); i++) {
7117 		if (sc->tq[i] == NULL)
7118 			continue;
7119 		taskqueue_free(sc->tq[i]);
7120 		sc->tq[i] = NULL;
7121 	}
7122 
7123 	sc->flags &= ~FULL_INIT_DONE;
7124 }
7125 
7126 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
7127     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
7128     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
7129     RSS_HASHTYPE_RSS_UDP_IPV6)
7130 
7131 /* Translates kernel hash types to hardware. */
7132 static int
hashconfig_to_hashen(int hashconfig)7133 hashconfig_to_hashen(int hashconfig)
7134 {
7135 	int hashen = 0;
7136 
7137 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
7138 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
7139 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
7140 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
7141 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
7142 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
7143 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
7144 	}
7145 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
7146 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
7147 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
7148 	}
7149 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
7150 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
7151 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
7152 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
7153 
7154 	return (hashen);
7155 }
7156 
7157 /* Translates hardware hash types to kernel. */
7158 static int
hashen_to_hashconfig(int hashen)7159 hashen_to_hashconfig(int hashen)
7160 {
7161 	int hashconfig = 0;
7162 
7163 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
7164 		/*
7165 		 * If UDP hashing was enabled it must have been enabled for
7166 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
7167 		 * enabling any 4-tuple hash is nonsense configuration.
7168 		 */
7169 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
7170 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
7171 
7172 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
7173 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
7174 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
7175 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
7176 	}
7177 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
7178 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
7179 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
7180 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
7181 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
7182 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
7183 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
7184 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
7185 
7186 	return (hashconfig);
7187 }
7188 
7189 /*
7190  * Idempotent.
7191  */
7192 static int
vi_full_init(struct vi_info * vi)7193 vi_full_init(struct vi_info *vi)
7194 {
7195 	struct adapter *sc = vi->adapter;
7196 	struct sge_rxq *rxq;
7197 	int rc, i, j;
7198 	int hashconfig = rss_gethashconfig();
7199 #ifdef RSS
7200 	int nbuckets = rss_getnumbuckets();
7201 	int extra;
7202 #endif
7203 
7204 	ASSERT_SYNCHRONIZED_OP(sc);
7205 
7206 	/*
7207 	 * Allocate tx/rx/fl queues for this VI.
7208 	 */
7209 	rc = t4_setup_vi_queues(vi);
7210 	if (rc != 0)
7211 		return (rc);
7212 
7213 	/*
7214 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
7215 	 */
7216 	if (vi->nrxq > vi->rss_size) {
7217 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
7218 		    "some queues will never receive traffic.\n", vi->nrxq,
7219 		    vi->rss_size);
7220 	} else if (vi->rss_size % vi->nrxq) {
7221 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
7222 		    "expect uneven traffic distribution.\n", vi->nrxq,
7223 		    vi->rss_size);
7224 	}
7225 #ifdef RSS
7226 	if (vi->nrxq != nbuckets) {
7227 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
7228 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
7229 	}
7230 #endif
7231 	if (vi->rss == NULL)
7232 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
7233 		    M_ZERO | M_WAITOK);
7234 	for (i = 0; i < vi->rss_size;) {
7235 #ifdef RSS
7236 		j = rss_get_indirection_to_bucket(i);
7237 		j %= vi->nrxq;
7238 		rxq = &sc->sge.rxq[vi->first_rxq + j];
7239 		vi->rss[i++] = rxq->iq.abs_id;
7240 #else
7241 		for_each_rxq(vi, j, rxq) {
7242 			vi->rss[i++] = rxq->iq.abs_id;
7243 			if (i == vi->rss_size)
7244 				break;
7245 		}
7246 #endif
7247 	}
7248 
7249 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
7250 	    vi->rss, vi->rss_size);
7251 	if (rc != 0) {
7252 		CH_ERR(vi, "rss_config failed: %d\n", rc);
7253 		return (rc);
7254 	}
7255 
7256 	vi->hashen = hashconfig_to_hashen(hashconfig);
7257 
7258 #ifdef RSS
7259 	/*
7260 	 * We may have had to enable some hashes even though the global config
7261 	 * wants them disabled.  This is a potential problem that must be
7262 	 * reported to the user.
7263 	 */
7264 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
7265 
7266 	/*
7267 	 * If we consider only the supported hash types, then the enabled hashes
7268 	 * are a superset of the requested hashes.  In other words, there cannot
7269 	 * be any supported hash that was requested but not enabled, but there
7270 	 * can be hashes that were not requested but had to be enabled.
7271 	 */
7272 	extra &= SUPPORTED_RSS_HASHTYPES;
7273 	MPASS((extra & hashconfig) == 0);
7274 
7275 	if (extra) {
7276 		CH_ALERT(vi,
7277 		    "global RSS config (0x%x) cannot be accommodated.\n",
7278 		    hashconfig);
7279 	}
7280 	if (extra & RSS_HASHTYPE_RSS_IPV4)
7281 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
7282 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
7283 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
7284 	if (extra & RSS_HASHTYPE_RSS_IPV6)
7285 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
7286 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
7287 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
7288 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
7289 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
7290 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
7291 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
7292 #endif
7293 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
7294 	    0, 0);
7295 	if (rc != 0) {
7296 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
7297 		return (rc);
7298 	}
7299 
7300 	return (0);
7301 }
7302 
7303 int
vi_init(struct vi_info * vi)7304 vi_init(struct vi_info *vi)
7305 {
7306 	int rc;
7307 
7308 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
7309 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
7310 	    ("%s: VI_INIT_DONE already", __func__));
7311 
7312 	rc = vi_full_init(vi);
7313 	if (rc != 0)
7314 		vi_full_uninit(vi);
7315 	else
7316 		vi->flags |= VI_INIT_DONE;
7317 
7318 	return (rc);
7319 }
7320 
7321 /*
7322  * Idempotent.
7323  */
7324 static void
vi_full_uninit(struct vi_info * vi)7325 vi_full_uninit(struct vi_info *vi)
7326 {
7327 
7328 	if (vi->flags & VI_INIT_DONE) {
7329 		quiesce_vi(vi);
7330 		free(vi->rss, M_CXGBE);
7331 		free(vi->nm_rss, M_CXGBE);
7332 	}
7333 
7334 	t4_teardown_vi_queues(vi);
7335 	vi->flags &= ~VI_INIT_DONE;
7336 }
7337 
7338 static void
quiesce_txq(struct sge_txq * txq)7339 quiesce_txq(struct sge_txq *txq)
7340 {
7341 	struct sge_eq *eq = &txq->eq;
7342 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7343 
7344 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7345 	MPASS(!(eq->flags & EQ_ENABLED));
7346 
7347 	/* Wait for the mp_ring to empty. */
7348 	while (!mp_ring_is_idle(txq->r)) {
7349 		mp_ring_check_drainage(txq->r, 4096);
7350 		pause("rquiesce", 1);
7351 	}
7352 	MPASS(txq->txp.npkt == 0);
7353 
7354 	if (eq->flags & EQ_HW_ALLOCATED) {
7355 		/*
7356 		 * Hardware is alive and working normally.  Wait for it to
7357 		 * finish and then wait for the driver to catch up and reclaim
7358 		 * all descriptors.
7359 		 */
7360 		while (spg->cidx != htobe16(eq->pidx))
7361 			pause("equiesce", 1);
7362 		while (eq->cidx != eq->pidx)
7363 			pause("dquiesce", 1);
7364 	} else {
7365 		/*
7366 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7367 		 * descriptors directly.
7368 		 */
7369 		TXQ_LOCK(txq);
7370 		while (eq->cidx != eq->pidx) {
7371 			struct mbuf *m, *nextpkt;
7372 			struct tx_sdesc *txsd;
7373 
7374 			txsd = &txq->sdesc[eq->cidx];
7375 			for (m = txsd->m; m != NULL; m = nextpkt) {
7376 				nextpkt = m->m_nextpkt;
7377 				m->m_nextpkt = NULL;
7378 				m_freem(m);
7379 			}
7380 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7381 		}
7382 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7383 		TXQ_UNLOCK(txq);
7384 	}
7385 }
7386 
7387 static void
quiesce_wrq(struct sge_wrq * wrq)7388 quiesce_wrq(struct sge_wrq *wrq)
7389 {
7390 	struct wrqe *wr;
7391 
7392 	TXQ_LOCK(wrq);
7393 	while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7394 		STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7395 #ifdef INVARIANTS
7396 		wrq->nwr_pending--;
7397 		wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7398 #endif
7399 		free(wr, M_CXGBE);
7400 	}
7401 	MPASS(wrq->nwr_pending == 0);
7402 	MPASS(wrq->ndesc_needed == 0);
7403 	wrq->nwr_pending = 0;
7404 	wrq->ndesc_needed = 0;
7405 	TXQ_UNLOCK(wrq);
7406 }
7407 
7408 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7409 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7410 {
7411 	/* Synchronize with the interrupt handler */
7412 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7413 		pause("iqfree", 1);
7414 
7415 	if (fl != NULL) {
7416 		MPASS(iq->flags & IQ_HAS_FL);
7417 
7418 		mtx_lock(&sc->sfl_lock);
7419 		FL_LOCK(fl);
7420 		fl->flags |= FL_DOOMED;
7421 		FL_UNLOCK(fl);
7422 		callout_stop(&sc->sfl_callout);
7423 		mtx_unlock(&sc->sfl_lock);
7424 
7425 		KASSERT((fl->flags & FL_STARVING) == 0,
7426 		    ("%s: still starving", __func__));
7427 
7428 		/* Release all buffers if hardware is no longer available. */
7429 		if (!(iq->flags & IQ_HW_ALLOCATED))
7430 			free_fl_buffers(sc, fl);
7431 	}
7432 }
7433 
7434 /*
7435  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7436  * that no new work is being enqueued by the hardware or the driver.  That part
7437  * should be arranged before calling this function.
7438  */
7439 static void
quiesce_vi(struct vi_info * vi)7440 quiesce_vi(struct vi_info *vi)
7441 {
7442 	int i;
7443 	struct adapter *sc = vi->adapter;
7444 	struct sge_rxq *rxq;
7445 	struct sge_txq *txq;
7446 #ifdef TCP_OFFLOAD
7447 	struct sge_ofld_rxq *ofld_rxq;
7448 #endif
7449 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7450 	struct sge_ofld_txq *ofld_txq;
7451 #endif
7452 
7453 	if (!(vi->flags & VI_INIT_DONE))
7454 		return;
7455 
7456 	for_each_txq(vi, i, txq) {
7457 		quiesce_txq(txq);
7458 	}
7459 
7460 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7461 	for_each_ofld_txq(vi, i, ofld_txq) {
7462 		quiesce_wrq(&ofld_txq->wrq);
7463 	}
7464 #endif
7465 
7466 	for_each_rxq(vi, i, rxq) {
7467 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7468 	}
7469 
7470 #ifdef TCP_OFFLOAD
7471 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7472 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7473 	}
7474 #endif
7475 }
7476 
7477 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7478 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7479     driver_intr_t *handler, void *arg, char *name)
7480 {
7481 	int rc;
7482 
7483 	irq->rid = rid;
7484 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7485 	    RF_SHAREABLE | RF_ACTIVE);
7486 	if (irq->res == NULL) {
7487 		device_printf(sc->dev,
7488 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7489 		return (ENOMEM);
7490 	}
7491 
7492 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7493 	    NULL, handler, arg, &irq->tag);
7494 	if (rc != 0) {
7495 		device_printf(sc->dev,
7496 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7497 		    rid, name, rc);
7498 	} else if (name)
7499 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7500 
7501 	return (rc);
7502 }
7503 
7504 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7505 t4_free_irq(struct adapter *sc, struct irq *irq)
7506 {
7507 	if (irq->tag)
7508 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7509 	if (irq->res)
7510 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7511 
7512 	bzero(irq, sizeof(*irq));
7513 
7514 	return (0);
7515 }
7516 
7517 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7518 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7519 {
7520 
7521 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7522 	t4_get_regs(sc, buf, regs->len);
7523 }
7524 
7525 #define	A_PL_INDIR_CMD	0x1f8
7526 
7527 #define	S_PL_AUTOINC	31
7528 #define	M_PL_AUTOINC	0x1U
7529 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7530 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7531 
7532 #define	S_PL_VFID	20
7533 #define	M_PL_VFID	0xffU
7534 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7535 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7536 
7537 #define	S_PL_ADDR	0
7538 #define	M_PL_ADDR	0xfffffU
7539 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7540 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7541 
7542 #define	A_PL_INDIR_DATA	0x1fc
7543 
7544 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7545 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7546 {
7547 	u32 stats[2];
7548 
7549 	if (sc->flags & IS_VF) {
7550 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7551 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7552 	} else {
7553 		mtx_assert(&sc->reg_lock, MA_OWNED);
7554 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7555 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7556 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7557 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7558 	}
7559 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7560 }
7561 
7562 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7563 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7564 {
7565 
7566 #define GET_STAT(name) \
7567 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7568 
7569 	if (!(sc->flags & IS_VF))
7570 		mtx_lock(&sc->reg_lock);
7571 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7572 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7573 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7574 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7575 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7576 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7577 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7578 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7579 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7580 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7581 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7582 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7583 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7584 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7585 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7586 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7587 	if (!(sc->flags & IS_VF))
7588 		mtx_unlock(&sc->reg_lock);
7589 
7590 #undef GET_STAT
7591 }
7592 
7593 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7594 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7595 {
7596 	int reg;
7597 
7598 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7599 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7600 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7601 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7602 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7603 }
7604 
7605 static void
vi_refresh_stats(struct vi_info * vi)7606 vi_refresh_stats(struct vi_info *vi)
7607 {
7608 	struct timeval tv;
7609 	const struct timeval interval = {0, 250000};	/* 250ms */
7610 
7611 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7612 
7613 	if (vi->flags & VI_SKIP_STATS)
7614 		return;
7615 
7616 	getmicrotime(&tv);
7617 	timevalsub(&tv, &interval);
7618 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7619 		return;
7620 
7621 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7622 	getmicrotime(&vi->last_refreshed);
7623 }
7624 
7625 static void
cxgbe_refresh_stats(struct vi_info * vi)7626 cxgbe_refresh_stats(struct vi_info *vi)
7627 {
7628 	u_int i, v, tnl_cong_drops, chan_map;
7629 	struct timeval tv;
7630 	const struct timeval interval = {0, 250000};	/* 250ms */
7631 	struct port_info *pi;
7632 	struct adapter *sc;
7633 
7634 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7635 
7636 	if (vi->flags & VI_SKIP_STATS)
7637 		return;
7638 
7639 	getmicrotime(&tv);
7640 	timevalsub(&tv, &interval);
7641 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7642 		return;
7643 
7644 	pi = vi->pi;
7645 	sc = vi->adapter;
7646 	tnl_cong_drops = 0;
7647 	t4_get_port_stats(sc, pi->hw_port, &pi->stats);
7648 	chan_map = pi->rx_e_chan_map;
7649 	while (chan_map) {
7650 		i = ffs(chan_map) - 1;
7651 		mtx_lock(&sc->reg_lock);
7652 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7653 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7654 		mtx_unlock(&sc->reg_lock);
7655 		tnl_cong_drops += v;
7656 		chan_map &= ~(1 << i);
7657 	}
7658 	pi->tnl_cong_drops = tnl_cong_drops;
7659 	getmicrotime(&vi->last_refreshed);
7660 }
7661 
7662 static void
cxgbe_tick(void * arg)7663 cxgbe_tick(void *arg)
7664 {
7665 	struct vi_info *vi = arg;
7666 
7667 	MPASS(IS_MAIN_VI(vi));
7668 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7669 
7670 	cxgbe_refresh_stats(vi);
7671 	callout_schedule(&vi->tick, hz);
7672 }
7673 
7674 static void
vi_tick(void * arg)7675 vi_tick(void *arg)
7676 {
7677 	struct vi_info *vi = arg;
7678 
7679 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7680 
7681 	vi_refresh_stats(vi);
7682 	callout_schedule(&vi->tick, hz);
7683 }
7684 
7685 /* CIM inbound queues */
7686 static const char *t4_ibq[CIM_NUM_IBQ] = {
7687 	"ibq_tp0", "ibq_tp1", "ibq_ulp", "ibq_sge0", "ibq_sge1", "ibq_ncsi"
7688 };
7689 static const char *t7_ibq[CIM_NUM_IBQ_T7] = {
7690 	"ibq_tp0", "ibq_tp1", "ibq_tp2", "ibq_tp3", "ibq_ulp", "ibq_sge0",
7691 	"ibq_sge1", "ibq_ncsi", NULL, "ibq_ipc1", "ibq_ipc2", "ibq_ipc3",
7692 	"ibq_ipc4", "ibq_ipc5", "ibq_ipc6", "ibq_ipc7"
7693 };
7694 static const char *t7_ibq_sec[] = {
7695 	"ibq_tp0", "ibq_tp1", "ibq_tp2", "ibq_tp3", "ibq_ulp", "ibq_sge0",
7696 	NULL, NULL, NULL, "ibq_ipc0"
7697 };
7698 
7699 /* CIM outbound queues */
7700 static const char *t4_obq[CIM_NUM_OBQ_T5] = {
7701 	"obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", "obq_ncsi",
7702 	"obq_sge_rx_q0", "obq_sge_rx_q1" /* These two are T5/T6 only */
7703 };
7704 static const char *t7_obq[CIM_NUM_OBQ_T7] = {
7705 	"obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", "obq_ncsi",
7706 	"obq_sge_rx_q0", NULL, NULL, "obq_ipc1", "obq_ipc2", "obq_ipc3",
7707 	"obq_ipc4", "obq_ipc5", "obq_ipc6", "obq_ipc7"
7708 };
7709 static const char *t7_obq_sec[] = {
7710 	"obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", NULL,
7711 	"obq_sge_rx_q0", NULL, NULL, "obq_ipc0"
7712 };
7713 
7714 static void
cim_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * c0)7715 cim_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
7716     struct sysctl_oid_list *c0)
7717 {
7718 	struct sysctl_oid *oid;
7719 	struct sysctl_oid_list *children1;
7720 	int i, j, qcount;
7721 	char s[16];
7722 	const char **qname;
7723 
7724 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "cim",
7725 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "CIM block");
7726 	c0 = SYSCTL_CHILDREN(oid);
7727 
7728 	SYSCTL_ADD_U8(ctx, c0, OID_AUTO, "ncores", CTLFLAG_RD, NULL,
7729 	    sc->params.ncores, "# of active CIM cores");
7730 
7731 	for (i = 0; i < sc->params.ncores; i++) {
7732 		snprintf(s, sizeof(s), "%u", i);
7733 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, s,
7734 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "CIM core");
7735 		children1 = SYSCTL_CHILDREN(oid);
7736 
7737 		/*
7738 		 * CTLFLAG_SKIP because the misc.devlog sysctl already displays
7739 		 * the log for all cores.  Use this sysctl to get the log for a
7740 		 * particular core only.
7741 		 */
7742 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "devlog",
7743 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_SKIP,
7744 		    sc, i, sysctl_devlog, "A", "firmware's device log");
7745 
7746 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "loadavg",
7747 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7748 		    sysctl_loadavg, "A",
7749 		    "microprocessor load averages (select firmwares only)");
7750 
7751 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "qcfg",
7752 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7753 		    chip_id(sc) > CHELSIO_T6 ? sysctl_cim_qcfg_t7 : sysctl_cim_qcfg,
7754 		    "A", "Queue configuration");
7755 
7756 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "la",
7757 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7758 		    sysctl_cim_la, "A", "Logic analyzer");
7759 
7760 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "ma_la",
7761 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7762 		    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7763 
7764 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "pif_la",
7765 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7766 		    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7767 
7768 		/* IBQs */
7769 		switch (chip_id(sc)) {
7770 		case CHELSIO_T4:
7771 		case CHELSIO_T5:
7772 		case CHELSIO_T6:
7773 			qname = &t4_ibq[0];
7774 			qcount = nitems(t4_ibq);
7775 			break;
7776 		case CHELSIO_T7:
7777 		default:
7778 			if (i == 0) {
7779 				qname = &t7_ibq[0];
7780 				qcount = nitems(t7_ibq);
7781 			} else {
7782 				qname = &t7_ibq_sec[0];
7783 				qcount = nitems(t7_ibq_sec);
7784 			}
7785 			break;
7786 		}
7787 		MPASS(qcount <= sc->chip_params->cim_num_ibq);
7788 		for (j = 0; j < qcount; j++) {
7789 			if (qname[j] == NULL)
7790 				continue;
7791 			SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, qname[j],
7792 			    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7793 			    (i << 16) | j, sysctl_cim_ibq, "A", NULL);
7794 		}
7795 
7796 		/* OBQs */
7797 		switch (chip_id(sc)) {
7798 		case CHELSIO_T4:
7799 			qname = t4_obq;
7800 			qcount = CIM_NUM_OBQ;
7801 			break;
7802 		case CHELSIO_T5:
7803 		case CHELSIO_T6:
7804 			qname = t4_obq;
7805 			qcount = nitems(t4_obq);
7806 			break;
7807 		case CHELSIO_T7:
7808 		default:
7809 			if (i == 0) {
7810 				qname = t7_obq;
7811 				qcount = nitems(t7_obq);
7812 			} else {
7813 				qname = t7_obq_sec;
7814 				qcount = nitems(t7_obq_sec);
7815 			}
7816 			break;
7817 		}
7818 		MPASS(qcount <= sc->chip_params->cim_num_obq);
7819 		for (j = 0; j < qcount; j++) {
7820 			if (qname[j] == NULL)
7821 				continue;
7822 			SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, qname[j],
7823 			    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7824 			    (i << 16) | j, sysctl_cim_obq, "A", NULL);
7825 		}
7826 	}
7827 }
7828 
7829 /*
7830  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7831  */
7832 static char *caps_decoder[] = {
7833 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7834 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7835 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7836 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7837 	    "\006HASHFILTER\007ETHOFLD",
7838 	"\20\001TOE\002SENDPATH",			/* 4: TOE */
7839 	"\20\001RDDP\002RDMAC\003ROCEv2",		/* 5: RDMA */
7840 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7841 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7842 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7843 	    "\007T10DIF"
7844 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7845 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7846 	    "\004TLS_HW,\005TOE_IPSEC",
7847 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7848 		    "\004PO_INITIATOR\005PO_TARGET",
7849 	"\20\001NVMe_TCP",				/* 9: NVMe */
7850 };
7851 
7852 void
t4_sysctls(struct adapter * sc)7853 t4_sysctls(struct adapter *sc)
7854 {
7855 	struct sysctl_ctx_list *ctx = &sc->ctx;
7856 	struct sysctl_oid *oid;
7857 	struct sysctl_oid_list *children, *c0;
7858 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7859 
7860 	/*
7861 	 * dev.t4nex.X.
7862 	 */
7863 	oid = device_get_sysctl_tree(sc->dev);
7864 	c0 = children = SYSCTL_CHILDREN(oid);
7865 
7866 	sc->sc_do_rxcopy = 1;
7867 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7868 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7869 
7870 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7871 	    sc->params.nports, "# of ports");
7872 
7873 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7874 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7875 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7876 	    "available doorbells");
7877 
7878 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7879 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7880 
7881 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7882 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7883 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7884 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7885 
7886 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7887 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7888 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7889 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7890 
7891 	t4_sge_sysctls(sc, ctx, children);
7892 
7893 	sc->lro_timeout = 100;
7894 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7895 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7896 
7897 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7898 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7899 
7900 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iflags", CTLFLAG_RW,
7901 	    &sc->intr_flags, 0, "flags for the slow interrupt handler");
7902 
7903 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7904 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7905 
7906 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7907 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7908 
7909 	if (sc->flags & IS_VF)
7910 		return;
7911 
7912 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7913 	    NULL, chip_rev(sc), "chip hardware revision");
7914 
7915 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7916 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7917 
7918 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7919 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7920 
7921 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7922 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7923 
7924 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7925 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7926 
7927 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7928 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7929 
7930 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7931 	    sc->er_version, 0, "expansion ROM version");
7932 
7933 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7934 	    sc->bs_version, 0, "bootstrap firmware version");
7935 
7936 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7937 	    NULL, sc->params.scfg_vers, "serial config version");
7938 
7939 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7940 	    NULL, sc->params.vpd_vers, "VPD version");
7941 
7942 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7943 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7944 
7945 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7946 	    sc->cfcsum, "config file checksum");
7947 
7948 #define SYSCTL_CAP(name, n, text) \
7949 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7950 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7951 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7952 	    "available " text " capabilities")
7953 
7954 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7955 	SYSCTL_CAP(linkcaps, 1, "link");
7956 	SYSCTL_CAP(switchcaps, 2, "switch");
7957 	SYSCTL_CAP(nvmecaps, 9, "NVMe");
7958 	SYSCTL_CAP(niccaps, 3, "NIC");
7959 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7960 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7961 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7962 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7963 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7964 #undef SYSCTL_CAP
7965 
7966 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7967 	    NULL, sc->tids.nftids, "number of filters");
7968 
7969 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7970 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7971 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7972 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7973 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7974 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7975 
7976 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7977 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7978 	    "I", "core Vdd (in mV)");
7979 
7980 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7981 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7982 	    sysctl_cpus, "A", "local CPUs");
7983 
7984 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7985 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7986 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7987 
7988 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7989 	    &sc->swintr, 0, "software triggered interrupts");
7990 
7991 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7992 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7993 	    "1 = reset adapter, 0 = zero reset counter");
7994 
7995 	/*
7996 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7997 	 */
7998 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7999 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
8000 	    "logs and miscellaneous information");
8001 	children = SYSCTL_CHILDREN(oid);
8002 
8003 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
8004 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8005 	    sysctl_cctrl, "A", "congestion control");
8006 
8007 	cim_sysctls(sc, ctx, children);
8008 
8009 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
8010 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8011 	    sysctl_cpl_stats, "A", "CPL statistics");
8012 
8013 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
8014 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8015 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
8016 
8017 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
8018 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8019 	    sysctl_tid_stats, "A", "tid stats");
8020 
8021 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
8022 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, -1,
8023 	    sysctl_devlog, "A", "firmware's device log (all cores)");
8024 
8025 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
8026 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8027 	    sysctl_fcoe_stats, "A", "FCoE statistics");
8028 
8029 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
8030 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8031 	    sysctl_hw_sched, "A", "hardware scheduler ");
8032 
8033 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
8034 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8035 	    sysctl_l2t, "A", "hardware L2 table");
8036 
8037 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
8038 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8039 	    sysctl_smt, "A", "hardware source MAC table");
8040 
8041 #ifdef INET6
8042 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
8043 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8044 	    sysctl_clip, "A", "active CLIP table entries");
8045 #endif
8046 
8047 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
8048 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8049 	    sysctl_lb_stats, "A", "loopback statistics");
8050 
8051 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
8052 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8053 	    sysctl_meminfo, "A", "memory regions");
8054 
8055 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
8056 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8057 	    chip_id(sc) >= CHELSIO_T7 ? sysctl_mps_tcam_t7 :
8058 	    (chip_id(sc) >= CHELSIO_T6 ? sysctl_mps_tcam_t6 : sysctl_mps_tcam),
8059 	    "A", "MPS TCAM entries");
8060 
8061 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
8062 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8063 	    sysctl_path_mtus, "A", "path MTUs");
8064 
8065 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
8066 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8067 	    sysctl_pm_stats, "A", "PM statistics");
8068 
8069 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
8070 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8071 	    sysctl_rdma_stats, "A", "RDMA statistics");
8072 
8073 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
8074 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8075 	    sysctl_tcp_stats, "A", "TCP statistics");
8076 
8077 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
8078 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8079 	    sysctl_tids, "A", "TID information");
8080 
8081 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
8082 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8083 	    sysctl_tp_err_stats, "A", "TP error statistics");
8084 
8085 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
8086 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8087 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
8088 
8089 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
8090 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
8091 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
8092 
8093 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
8094 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8095 	    sysctl_tp_la, "A", "TP logic analyzer");
8096 
8097 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
8098 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8099 	    sysctl_tx_rate, "A", "Tx rate");
8100 
8101 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
8102 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8103 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
8104 
8105 	if (chip_id(sc) >= CHELSIO_T5) {
8106 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
8107 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8108 		    sysctl_wcwr_stats, "A", "write combined work requests");
8109 	}
8110 
8111 #ifdef KERN_TLS
8112 	if (is_ktls(sc)) {
8113 		/*
8114 		 * dev.t4nex.0.tls.
8115 		 */
8116 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
8117 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
8118 		children = SYSCTL_CHILDREN(oid);
8119 
8120 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
8121 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
8122 		    "keys in work requests (1) or attempt to store TLS keys "
8123 		    "in card memory.");
8124 
8125 		if (is_t6(sc))
8126 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
8127 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
8128 			    "combine TCB field updates with TLS record work "
8129 			    "requests.");
8130 		else {
8131 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "short_records",
8132 			    CTLFLAG_RW, &sc->tlst.short_records, 0,
8133 			    "Use cipher-only mode for short records.");
8134 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "partial_ghash",
8135 			    CTLFLAG_RW, &sc->tlst.partial_ghash, 0,
8136 			    "Use partial GHASH for AES-GCM records.");
8137 		}
8138 	}
8139 #endif
8140 
8141 #ifdef TCP_OFFLOAD
8142 	if (is_offload(sc)) {
8143 		int i;
8144 		char s[4];
8145 
8146 		/*
8147 		 * dev.t4nex.X.toe.
8148 		 */
8149 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
8150 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
8151 		children = SYSCTL_CHILDREN(oid);
8152 
8153 		sc->tt.cong_algorithm = -1;
8154 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
8155 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
8156 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
8157 		    "3 = highspeed)");
8158 
8159 		sc->tt.sndbuf = -1;
8160 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
8161 		    &sc->tt.sndbuf, 0, "hardware send buffer");
8162 
8163 		sc->tt.ddp = 0;
8164 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
8165 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
8166 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
8167 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
8168 
8169 		sc->tt.rx_coalesce = -1;
8170 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
8171 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
8172 
8173 		sc->tt.tls = 1;
8174 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
8175 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
8176 		    "Inline TLS allowed");
8177 
8178 		sc->tt.tx_align = -1;
8179 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
8180 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
8181 
8182 		sc->tt.tx_zcopy = 0;
8183 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
8184 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
8185 		    "Enable zero-copy aio_write(2)");
8186 
8187 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
8188 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
8189 		    "cop_managed_offloading", CTLFLAG_RW,
8190 		    &sc->tt.cop_managed_offloading, 0,
8191 		    "COP (Connection Offload Policy) controls all TOE offload");
8192 
8193 		sc->tt.autorcvbuf_inc = 16 * 1024;
8194 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
8195 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
8196 		    "autorcvbuf increment");
8197 
8198 		sc->tt.update_hc_on_pmtu_change = 1;
8199 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
8200 		    "update_hc_on_pmtu_change", CTLFLAG_RW,
8201 		    &sc->tt.update_hc_on_pmtu_change, 0,
8202 		    "Update hostcache entry if the PMTU changes");
8203 
8204 		sc->tt.iso = 1;
8205 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
8206 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
8207 
8208 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
8209 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8210 		    sysctl_tp_tick, "A", "TP timer tick (us)");
8211 
8212 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
8213 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
8214 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
8215 
8216 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
8217 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
8218 		    sysctl_tp_tick, "A", "DACK tick (us)");
8219 
8220 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
8221 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8222 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
8223 
8224 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
8225 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8226 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
8227 		    "Minimum retransmit interval (us)");
8228 
8229 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
8230 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8231 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
8232 		    "Maximum retransmit interval (us)");
8233 
8234 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
8235 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8236 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
8237 		    "Persist timer min (us)");
8238 
8239 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
8240 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8241 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
8242 		    "Persist timer max (us)");
8243 
8244 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
8245 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8246 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
8247 		    "Keepalive idle timer (us)");
8248 
8249 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
8250 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8251 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
8252 		    "Keepalive interval timer (us)");
8253 
8254 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
8255 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8256 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
8257 
8258 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
8259 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8260 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
8261 		    "FINWAIT2 timer (us)");
8262 
8263 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
8264 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8265 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
8266 		    "Number of SYN retransmissions before abort");
8267 
8268 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
8269 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8270 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
8271 		    "Number of retransmissions before abort");
8272 
8273 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
8274 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8275 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
8276 		    "Number of keepalive probes before abort");
8277 
8278 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
8279 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8280 		    "TOE retransmit backoffs");
8281 		children = SYSCTL_CHILDREN(oid);
8282 		for (i = 0; i < 16; i++) {
8283 			snprintf(s, sizeof(s), "%u", i);
8284 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
8285 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8286 			    i, sysctl_tp_backoff, "IU",
8287 			    "TOE retransmit backoff");
8288 		}
8289 	}
8290 #endif
8291 }
8292 
8293 void
vi_sysctls(struct vi_info * vi)8294 vi_sysctls(struct vi_info *vi)
8295 {
8296 	struct sysctl_ctx_list *ctx = &vi->ctx;
8297 	struct sysctl_oid *oid;
8298 	struct sysctl_oid_list *children;
8299 
8300 	/*
8301 	 * dev.v?(cxgbe|cxl).X.
8302 	 */
8303 	oid = device_get_sysctl_tree(vi->dev);
8304 	children = SYSCTL_CHILDREN(oid);
8305 
8306 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
8307 	    vi->viid, "VI identifer");
8308 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
8309 	    &vi->nrxq, 0, "# of rx queues");
8310 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
8311 	    &vi->ntxq, 0, "# of tx queues");
8312 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
8313 	    &vi->first_rxq, 0, "index of first rx queue");
8314 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
8315 	    &vi->first_txq, 0, "index of first tx queue");
8316 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
8317 	    vi->rss_base, "start of RSS indirection table");
8318 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
8319 	    vi->rss_size, "size of RSS indirection table");
8320 
8321 	if (IS_MAIN_VI(vi)) {
8322 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
8323 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8324 		    sysctl_noflowq, "IU",
8325 		    "Reserve queue 0 for non-flowid packets");
8326 	}
8327 
8328 	if (vi->adapter->flags & IS_VF) {
8329 		MPASS(vi->flags & TX_USES_VM_WR);
8330 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
8331 		    NULL, 1, "use VM work requests for transmit");
8332 	} else {
8333 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
8334 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8335 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
8336 	}
8337 
8338 #ifdef TCP_OFFLOAD
8339 	if (vi->nofldrxq != 0) {
8340 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
8341 		    &vi->nofldrxq, 0,
8342 		    "# of rx queues for offloaded TCP connections");
8343 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
8344 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
8345 		    "index of first TOE rx queue");
8346 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
8347 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8348 		    sysctl_holdoff_tmr_idx_ofld, "I",
8349 		    "holdoff timer index for TOE queues");
8350 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
8351 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8352 		    sysctl_holdoff_pktc_idx_ofld, "I",
8353 		    "holdoff packet counter index for TOE queues");
8354 	}
8355 #endif
8356 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
8357 	if (vi->nofldtxq != 0) {
8358 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
8359 		    &vi->nofldtxq, 0,
8360 		    "# of tx queues for TOE/ETHOFLD");
8361 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
8362 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
8363 		    "index of first TOE/ETHOFLD tx queue");
8364 	}
8365 #endif
8366 #ifdef DEV_NETMAP
8367 	if (vi->nnmrxq != 0) {
8368 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
8369 		    &vi->nnmrxq, 0, "# of netmap rx queues");
8370 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
8371 		    &vi->nnmtxq, 0, "# of netmap tx queues");
8372 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
8373 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
8374 		    "index of first netmap rx queue");
8375 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
8376 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
8377 		    "index of first netmap tx queue");
8378 	}
8379 #endif
8380 
8381 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
8382 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8383 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
8384 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
8385 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8386 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
8387 
8388 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
8389 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8390 	    sysctl_qsize_rxq, "I", "rx queue size");
8391 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
8392 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8393 	    sysctl_qsize_txq, "I", "tx queue size");
8394 }
8395 
8396 static void
cxgbe_sysctls(struct port_info * pi)8397 cxgbe_sysctls(struct port_info *pi)
8398 {
8399 	struct sysctl_ctx_list *ctx = &pi->ctx;
8400 	struct sysctl_oid *oid;
8401 	struct sysctl_oid_list *children, *children2;
8402 	struct adapter *sc = pi->adapter;
8403 	int i;
8404 	char name[16];
8405 	static char *tc_flags = {"\20\1USER"};
8406 
8407 	/*
8408 	 * dev.cxgbe.X.
8409 	 */
8410 	oid = device_get_sysctl_tree(pi->dev);
8411 	children = SYSCTL_CHILDREN(oid);
8412 
8413 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8414 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8415 	    sysctl_linkdnrc, "A", "reason why link is down");
8416 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8417 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8418 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8419 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8420 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8421 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8422 		    sysctl_btphy, "I", "PHY firmware version");
8423 	}
8424 
8425 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8426 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8427 	    sysctl_pause_settings, "A",
8428 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8429 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8430 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8431 	    "FEC in use on the link");
8432 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8433 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8434 	    sysctl_requested_fec, "A",
8435 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8436 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8437 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8438 	    "FEC recommended by the cable/transceiver");
8439 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8440 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8441 	    sysctl_autoneg, "I",
8442 	    "autonegotiation (-1 = not supported)");
8443 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8444 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8445 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8446 
8447 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8448 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8449 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8450 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8451 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8452 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8453 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8454 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8455 
8456 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8457 	    port_top_speed(pi), "max speed (in Gbps)");
8458 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8459 	    pi->mps_bg_map, "MPS buffer group map");
8460 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8461 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8462 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8463 	    pi->tx_chan, "TP tx c-channel");
8464 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8465 	    pi->rx_chan, "TP rx c-channel");
8466 
8467 	if (sc->flags & IS_VF)
8468 		return;
8469 
8470 	/*
8471 	 * dev.(cxgbe|cxl).X.tc.
8472 	 */
8473 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8474 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8475 	    "Tx scheduler traffic classes (cl_rl)");
8476 	children2 = SYSCTL_CHILDREN(oid);
8477 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8478 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8479 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8480 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8481 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8482 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8483 	for (i = 0; i < sc->params.nsched_cls; i++) {
8484 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8485 
8486 		snprintf(name, sizeof(name), "%d", i);
8487 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8488 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8489 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8490 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8491 		    CTLFLAG_RD, &tc->state, 0, "current state");
8492 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8493 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8494 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8495 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8496 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8497 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8498 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8499 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8500 		    "traffic class parameters");
8501 	}
8502 
8503 	/*
8504 	 * dev.cxgbe.X.stats.
8505 	 */
8506 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8507 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8508 	children = SYSCTL_CHILDREN(oid);
8509 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8510 	    &pi->tx_parse_error, 0,
8511 	    "# of tx packets with invalid length or # of segments");
8512 
8513 #define T4_LBSTAT(name, stat, desc) do { \
8514 	if (sc->params.tp.lb_mode) { \
8515 		SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8516 		    CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, \
8517 		    A_MPS_PORT_STAT_##stat##_L, \
8518 		    sysctl_handle_t4_portstat64, "QU", desc); \
8519 	} else { \
8520 		SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8521 		    CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8522 		    t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8523 		    sysctl_handle_t4_reg64, "QU", desc); \
8524 	} \
8525 } while (0)
8526 
8527 	T4_LBSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8528 	T4_LBSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8529 	T4_LBSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8530 	T4_LBSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8531 	T4_LBSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8532 	T4_LBSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8533 	T4_LBSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8534 	T4_LBSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8535 	T4_LBSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8536 	T4_LBSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8537 	T4_LBSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8538 	T4_LBSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8539 	T4_LBSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8540 	T4_LBSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8541 	T4_LBSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8542 	T4_LBSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8543 	T4_LBSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8544 	T4_LBSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8545 	T4_LBSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8546 	T4_LBSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8547 	T4_LBSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8548 	T4_LBSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8549 	T4_LBSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8550 
8551 	T4_LBSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8552 	T4_LBSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8553 	T4_LBSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8554 	T4_LBSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8555 	T4_LBSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8556 	T4_LBSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8557 	T4_LBSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8558 	if (is_t6(sc)) {
8559 		/* Read from port_stats and may be stale by up to 1s */
8560 		SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "rx_fcs_err",
8561 		    CTLFLAG_RD, &pi->stats.rx_fcs_err,
8562 		    "# of frames received with bad FCS since last link up");
8563 	} else {
8564 		T4_LBSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8565 		    "# of frames received with bad FCS");
8566 	}
8567 	T4_LBSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8568 	T4_LBSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8569 	T4_LBSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8570 	T4_LBSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8571 	T4_LBSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8572 	T4_LBSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8573 	T4_LBSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8574 	T4_LBSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8575 	T4_LBSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8576 	T4_LBSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8577 	T4_LBSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8578 	T4_LBSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8579 	T4_LBSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8580 	T4_LBSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8581 	T4_LBSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8582 	T4_LBSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8583 	T4_LBSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8584 	T4_LBSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8585 	T4_LBSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8586 #undef T4_LBSTAT
8587 
8588 #define T4_REGSTAT(name, stat, desc) do { \
8589 	SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8590 	    CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8591 	    A_MPS_STAT_##stat##_L, sysctl_handle_t4_reg64, "QU", desc); \
8592 } while (0)
8593 
8594 	if (pi->mps_bg_map & 1) {
8595 		T4_REGSTAT(rx_ovflow0, RX_BG_0_MAC_DROP_FRAME,
8596 		    "# drops due to buffer-group 0 overflows");
8597 		T4_REGSTAT(rx_trunc0, RX_BG_0_MAC_TRUNC_FRAME,
8598 		    "# of buffer-group 0 truncated packets");
8599 	}
8600 	if (pi->mps_bg_map & 2) {
8601 		T4_REGSTAT(rx_ovflow1, RX_BG_1_MAC_DROP_FRAME,
8602 		    "# drops due to buffer-group 1 overflows");
8603 		T4_REGSTAT(rx_trunc1, RX_BG_1_MAC_TRUNC_FRAME,
8604 		    "# of buffer-group 1 truncated packets");
8605 	}
8606 	if (pi->mps_bg_map & 4) {
8607 		T4_REGSTAT(rx_ovflow2, RX_BG_2_MAC_DROP_FRAME,
8608 		    "# drops due to buffer-group 2 overflows");
8609 		T4_REGSTAT(rx_trunc2, RX_BG_2_MAC_TRUNC_FRAME,
8610 		    "# of buffer-group 2 truncated packets");
8611 	}
8612 	if (pi->mps_bg_map & 8) {
8613 		T4_REGSTAT(rx_ovflow3, RX_BG_3_MAC_DROP_FRAME,
8614 		    "# drops due to buffer-group 3 overflows");
8615 		T4_REGSTAT(rx_trunc3, RX_BG_3_MAC_TRUNC_FRAME,
8616 		    "# of buffer-group 3 truncated packets");
8617 	}
8618 #undef T4_REGSTAT
8619 }
8620 
8621 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8622 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8623 {
8624 	int rc, *i, space = 0;
8625 	struct sbuf sb;
8626 
8627 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8628 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8629 		if (space)
8630 			sbuf_printf(&sb, " ");
8631 		sbuf_printf(&sb, "%d", *i);
8632 		space = 1;
8633 	}
8634 	rc = sbuf_finish(&sb);
8635 	sbuf_delete(&sb);
8636 	return (rc);
8637 }
8638 
8639 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8640 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8641 {
8642 	int rc;
8643 	struct sbuf *sb;
8644 
8645 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8646 	if (sb == NULL)
8647 		return (ENOMEM);
8648 
8649 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8650 	rc = sbuf_finish(sb);
8651 	sbuf_delete(sb);
8652 
8653 	return (rc);
8654 }
8655 
8656 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8657 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8658 {
8659 	int rc;
8660 	struct sbuf *sb;
8661 
8662 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8663 	if (sb == NULL)
8664 		return (ENOMEM);
8665 
8666 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8667 	rc = sbuf_finish(sb);
8668 	sbuf_delete(sb);
8669 
8670 	return (rc);
8671 }
8672 
8673 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8674 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8675 {
8676 	struct port_info *pi = arg1;
8677 	int op = arg2;
8678 	struct adapter *sc = pi->adapter;
8679 	u_int v;
8680 	int rc;
8681 
8682 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8683 	if (rc)
8684 		return (rc);
8685 	if (!hw_all_ok(sc))
8686 		rc = ENXIO;
8687 	else {
8688 		/* XXX: magic numbers */
8689 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8690 		    op ? 0x20 : 0xc820, &v);
8691 	}
8692 	end_synchronized_op(sc, 0);
8693 	if (rc)
8694 		return (rc);
8695 	if (op == 0)
8696 		v /= 256;
8697 
8698 	rc = sysctl_handle_int(oidp, &v, 0, req);
8699 	return (rc);
8700 }
8701 
8702 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8703 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8704 {
8705 	struct vi_info *vi = arg1;
8706 	int rc, val;
8707 
8708 	val = vi->rsrv_noflowq;
8709 	rc = sysctl_handle_int(oidp, &val, 0, req);
8710 	if (rc != 0 || req->newptr == NULL)
8711 		return (rc);
8712 
8713 	if ((val >= 1) && (vi->ntxq > 1))
8714 		vi->rsrv_noflowq = 1;
8715 	else
8716 		vi->rsrv_noflowq = 0;
8717 
8718 	return (rc);
8719 }
8720 
8721 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8722 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8723 {
8724 	struct vi_info *vi = arg1;
8725 	struct adapter *sc = vi->adapter;
8726 	int rc, val, i;
8727 
8728 	MPASS(!(sc->flags & IS_VF));
8729 
8730 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8731 	rc = sysctl_handle_int(oidp, &val, 0, req);
8732 	if (rc != 0 || req->newptr == NULL)
8733 		return (rc);
8734 
8735 	if (val != 0 && val != 1)
8736 		return (EINVAL);
8737 
8738 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8739 	    "t4txvm");
8740 	if (rc)
8741 		return (rc);
8742 	if (!hw_all_ok(sc))
8743 		rc = ENXIO;
8744 	else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8745 		/*
8746 		 * We don't want parse_pkt to run with one setting (VF or PF)
8747 		 * and then eth_tx to see a different setting but still use
8748 		 * stale information calculated by parse_pkt.
8749 		 */
8750 		rc = EBUSY;
8751 	} else {
8752 		struct port_info *pi = vi->pi;
8753 		struct sge_txq *txq;
8754 		uint32_t ctrl0;
8755 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8756 
8757 		if (val) {
8758 			vi->flags |= TX_USES_VM_WR;
8759 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8760 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8761 			    V_TXPKT_INTF(pi->hw_port));
8762 			if (!(sc->flags & IS_VF))
8763 				npkt--;
8764 		} else {
8765 			vi->flags &= ~TX_USES_VM_WR;
8766 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8767 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8768 			    V_TXPKT_INTF(pi->hw_port) | V_TXPKT_PF(sc->pf) |
8769 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8770 		}
8771 		for_each_txq(vi, i, txq) {
8772 			txq->cpl_ctrl0 = ctrl0;
8773 			txq->txp.max_npkt = npkt;
8774 		}
8775 	}
8776 	end_synchronized_op(sc, LOCK_HELD);
8777 	return (rc);
8778 }
8779 
8780 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8781 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8782 {
8783 	struct vi_info *vi = arg1;
8784 	struct adapter *sc = vi->adapter;
8785 	int idx, rc, i;
8786 	struct sge_rxq *rxq;
8787 	uint8_t v;
8788 
8789 	idx = vi->tmr_idx;
8790 
8791 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8792 	if (rc != 0 || req->newptr == NULL)
8793 		return (rc);
8794 
8795 	if (idx < 0 || idx >= SGE_NTIMERS)
8796 		return (EINVAL);
8797 
8798 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8799 	    "t4tmr");
8800 	if (rc)
8801 		return (rc);
8802 
8803 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8804 	for_each_rxq(vi, i, rxq) {
8805 #ifdef atomic_store_rel_8
8806 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8807 #else
8808 		rxq->iq.intr_params = v;
8809 #endif
8810 	}
8811 	vi->tmr_idx = idx;
8812 
8813 	end_synchronized_op(sc, LOCK_HELD);
8814 	return (0);
8815 }
8816 
8817 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8818 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8819 {
8820 	struct vi_info *vi = arg1;
8821 	struct adapter *sc = vi->adapter;
8822 	int idx, rc;
8823 
8824 	idx = vi->pktc_idx;
8825 
8826 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8827 	if (rc != 0 || req->newptr == NULL)
8828 		return (rc);
8829 
8830 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8831 		return (EINVAL);
8832 
8833 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8834 	    "t4pktc");
8835 	if (rc)
8836 		return (rc);
8837 
8838 	if (vi->flags & VI_INIT_DONE)
8839 		rc = EBUSY; /* cannot be changed once the queues are created */
8840 	else
8841 		vi->pktc_idx = idx;
8842 
8843 	end_synchronized_op(sc, LOCK_HELD);
8844 	return (rc);
8845 }
8846 
8847 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8848 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8849 {
8850 	struct vi_info *vi = arg1;
8851 	struct adapter *sc = vi->adapter;
8852 	int qsize, rc;
8853 
8854 	qsize = vi->qsize_rxq;
8855 
8856 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8857 	if (rc != 0 || req->newptr == NULL)
8858 		return (rc);
8859 
8860 	if (qsize < 128 || (qsize & 7))
8861 		return (EINVAL);
8862 
8863 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8864 	    "t4rxqs");
8865 	if (rc)
8866 		return (rc);
8867 
8868 	if (vi->flags & VI_INIT_DONE)
8869 		rc = EBUSY; /* cannot be changed once the queues are created */
8870 	else
8871 		vi->qsize_rxq = qsize;
8872 
8873 	end_synchronized_op(sc, LOCK_HELD);
8874 	return (rc);
8875 }
8876 
8877 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8878 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8879 {
8880 	struct vi_info *vi = arg1;
8881 	struct adapter *sc = vi->adapter;
8882 	int qsize, rc;
8883 
8884 	qsize = vi->qsize_txq;
8885 
8886 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8887 	if (rc != 0 || req->newptr == NULL)
8888 		return (rc);
8889 
8890 	if (qsize < 128 || qsize > 65536)
8891 		return (EINVAL);
8892 
8893 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8894 	    "t4txqs");
8895 	if (rc)
8896 		return (rc);
8897 
8898 	if (vi->flags & VI_INIT_DONE)
8899 		rc = EBUSY; /* cannot be changed once the queues are created */
8900 	else
8901 		vi->qsize_txq = qsize;
8902 
8903 	end_synchronized_op(sc, LOCK_HELD);
8904 	return (rc);
8905 }
8906 
8907 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8908 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8909 {
8910 	struct port_info *pi = arg1;
8911 	struct adapter *sc = pi->adapter;
8912 	struct link_config *lc = &pi->link_cfg;
8913 	int rc;
8914 
8915 	if (req->newptr == NULL) {
8916 		struct sbuf *sb;
8917 		static char *bits = "\20\1RX\2TX\3AUTO";
8918 
8919 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8920 		if (sb == NULL)
8921 			return (ENOMEM);
8922 
8923 		if (lc->link_ok) {
8924 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8925 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8926 		} else {
8927 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8928 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8929 		}
8930 		rc = sbuf_finish(sb);
8931 		sbuf_delete(sb);
8932 	} else {
8933 		char s[2];
8934 		int n;
8935 
8936 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8937 		    PAUSE_AUTONEG));
8938 		s[1] = 0;
8939 
8940 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8941 		if (rc != 0)
8942 			return(rc);
8943 
8944 		if (s[1] != 0)
8945 			return (EINVAL);
8946 		if (s[0] < '0' || s[0] > '9')
8947 			return (EINVAL);	/* not a number */
8948 		n = s[0] - '0';
8949 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8950 			return (EINVAL);	/* some other bit is set too */
8951 
8952 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8953 		    "t4PAUSE");
8954 		if (rc)
8955 			return (rc);
8956 		if (hw_all_ok(sc)) {
8957 			PORT_LOCK(pi);
8958 			lc->requested_fc = n;
8959 			fixup_link_config(pi);
8960 			if (pi->up_vis > 0)
8961 				rc = apply_link_config(pi);
8962 			set_current_media(pi);
8963 			PORT_UNLOCK(pi);
8964 		}
8965 		end_synchronized_op(sc, 0);
8966 	}
8967 
8968 	return (rc);
8969 }
8970 
8971 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8972 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8973 {
8974 	struct port_info *pi = arg1;
8975 	struct link_config *lc = &pi->link_cfg;
8976 	int rc;
8977 	struct sbuf *sb;
8978 
8979 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8980 	if (sb == NULL)
8981 		return (ENOMEM);
8982 	if (lc->link_ok)
8983 		sbuf_printf(sb, "%b", lc->fec, t4_fec_bits);
8984 	else
8985 		sbuf_printf(sb, "no link");
8986 	rc = sbuf_finish(sb);
8987 	sbuf_delete(sb);
8988 
8989 	return (rc);
8990 }
8991 
8992 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)8993 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8994 {
8995 	struct port_info *pi = arg1;
8996 	struct adapter *sc = pi->adapter;
8997 	struct link_config *lc = &pi->link_cfg;
8998 	int rc;
8999 	int8_t old = lc->requested_fec;
9000 
9001 	if (req->newptr == NULL) {
9002 		struct sbuf *sb;
9003 
9004 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
9005 		if (sb == NULL)
9006 			return (ENOMEM);
9007 
9008 		sbuf_printf(sb, "%b", old, t4_fec_bits);
9009 		rc = sbuf_finish(sb);
9010 		sbuf_delete(sb);
9011 	} else {
9012 		char s[8];
9013 		int n;
9014 
9015 		snprintf(s, sizeof(s), "%d", old == FEC_AUTO ? -1 :
9016 		    old & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
9017 
9018 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
9019 		if (rc != 0)
9020 			return(rc);
9021 
9022 		n = strtol(&s[0], NULL, 0);
9023 		if (n < 0 || n & FEC_AUTO)
9024 			n = FEC_AUTO;
9025 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
9026 			return (EINVAL);/* some other bit is set too */
9027 
9028 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
9029 		    "t4reqf");
9030 		if (rc)
9031 			return (rc);
9032 		PORT_LOCK(pi);
9033 		if (lc->requested_fec != old) {
9034 			rc = EBUSY;
9035 			goto done;
9036 		}
9037 		if (n == FEC_AUTO)
9038 			lc->requested_fec = FEC_AUTO;
9039 		else if (n == 0 || n == FEC_NONE)
9040 			lc->requested_fec = FEC_NONE;
9041 		else {
9042 			if ((lc->pcaps |
9043 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
9044 			    lc->pcaps) {
9045 				rc = ENOTSUP;
9046 				goto done;
9047 			}
9048 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
9049 			    FEC_MODULE);
9050 		}
9051 		if (hw_all_ok(sc)) {
9052 			fixup_link_config(pi);
9053 			if (pi->up_vis > 0) {
9054 				rc = apply_link_config(pi);
9055 				if (rc != 0) {
9056 					lc->requested_fec = old;
9057 					if (rc == FW_EPROTO)
9058 						rc = ENOTSUP;
9059 				}
9060 			}
9061 		}
9062 done:
9063 		PORT_UNLOCK(pi);
9064 		end_synchronized_op(sc, 0);
9065 	}
9066 
9067 	return (rc);
9068 }
9069 
9070 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)9071 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
9072 {
9073 	struct port_info *pi = arg1;
9074 	struct adapter *sc = pi->adapter;
9075 	struct link_config *lc = &pi->link_cfg;
9076 	int rc;
9077 	int8_t fec;
9078 	struct sbuf *sb;
9079 
9080 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
9081 	if (sb == NULL)
9082 		return (ENOMEM);
9083 
9084 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
9085 		rc = EBUSY;
9086 		goto done;
9087 	}
9088 	if (!hw_all_ok(sc)) {
9089 		rc = ENXIO;
9090 		goto done;
9091 	}
9092 	PORT_LOCK(pi);
9093 	if (pi->up_vis == 0) {
9094 		/*
9095 		 * If all the interfaces are administratively down the firmware
9096 		 * does not report transceiver changes.  Refresh port info here.
9097 		 * This is the only reason we have a synchronized op in this
9098 		 * function.  Just PORT_LOCK would have been enough otherwise.
9099 		 */
9100 		t4_update_port_info(pi);
9101 	}
9102 
9103 	fec = lc->fec_hint;
9104 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
9105 	    !fec_supported(lc->pcaps)) {
9106 		PORT_UNLOCK(pi);
9107 		sbuf_printf(sb, "n/a");
9108 	} else {
9109 		if (fec == 0)
9110 			fec = FEC_NONE;
9111 		PORT_UNLOCK(pi);
9112 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, t4_fec_bits);
9113 	}
9114 	rc = sbuf_finish(sb);
9115 done:
9116 	sbuf_delete(sb);
9117 	end_synchronized_op(sc, 0);
9118 
9119 	return (rc);
9120 }
9121 
9122 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)9123 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
9124 {
9125 	struct port_info *pi = arg1;
9126 	struct adapter *sc = pi->adapter;
9127 	struct link_config *lc = &pi->link_cfg;
9128 	int rc, val;
9129 
9130 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
9131 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
9132 	else
9133 		val = -1;
9134 	rc = sysctl_handle_int(oidp, &val, 0, req);
9135 	if (rc != 0 || req->newptr == NULL)
9136 		return (rc);
9137 	if (val == 0)
9138 		val = AUTONEG_DISABLE;
9139 	else if (val == 1)
9140 		val = AUTONEG_ENABLE;
9141 	else
9142 		val = AUTONEG_AUTO;
9143 
9144 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
9145 	    "t4aneg");
9146 	if (rc)
9147 		return (rc);
9148 	PORT_LOCK(pi);
9149 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
9150 		rc = ENOTSUP;
9151 		goto done;
9152 	}
9153 	lc->requested_aneg = val;
9154 	if (hw_all_ok(sc)) {
9155 		fixup_link_config(pi);
9156 		if (pi->up_vis > 0)
9157 			rc = apply_link_config(pi);
9158 		set_current_media(pi);
9159 	}
9160 done:
9161 	PORT_UNLOCK(pi);
9162 	end_synchronized_op(sc, 0);
9163 	return (rc);
9164 }
9165 
9166 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)9167 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
9168 {
9169 	struct port_info *pi = arg1;
9170 	struct adapter *sc = pi->adapter;
9171 	struct link_config *lc = &pi->link_cfg;
9172 	int rc, val;
9173 
9174 	val = lc->force_fec;
9175 	MPASS(val >= -1 && val <= 1);
9176 	rc = sysctl_handle_int(oidp, &val, 0, req);
9177 	if (rc != 0 || req->newptr == NULL)
9178 		return (rc);
9179 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
9180 		return (ENOTSUP);
9181 	if (val < -1 || val > 1)
9182 		return (EINVAL);
9183 
9184 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
9185 	if (rc)
9186 		return (rc);
9187 	PORT_LOCK(pi);
9188 	lc->force_fec = val;
9189 	if (hw_all_ok(sc)) {
9190 		fixup_link_config(pi);
9191 		if (pi->up_vis > 0)
9192 			rc = apply_link_config(pi);
9193 	}
9194 	PORT_UNLOCK(pi);
9195 	end_synchronized_op(sc, 0);
9196 	return (rc);
9197 }
9198 
9199 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)9200 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
9201 {
9202 	struct adapter *sc = arg1;
9203 	int rc, reg = arg2;
9204 	uint64_t val;
9205 
9206 	mtx_lock(&sc->reg_lock);
9207 	if (hw_off_limits(sc))
9208 		rc = ENXIO;
9209 	else {
9210 		rc = 0;
9211 		val = t4_read_reg64(sc, reg);
9212 	}
9213 	mtx_unlock(&sc->reg_lock);
9214 	if (rc == 0)
9215 		rc = sysctl_handle_64(oidp, &val, 0, req);
9216 	return (rc);
9217 }
9218 
9219 static int
sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS)9220 sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS)
9221 {
9222 	struct port_info *pi = arg1;
9223 	struct adapter *sc = pi->adapter;
9224 	int rc, i, reg = arg2;
9225 	uint64_t val;
9226 
9227 	mtx_lock(&sc->reg_lock);
9228 	if (hw_off_limits(sc))
9229 		rc = ENXIO;
9230 	else {
9231 		val = 0;
9232 		for (i = 0; i < sc->params.tp.lb_nchan; i++) {
9233 			val += t4_read_reg64(sc,
9234 			    t4_port_reg(sc, pi->tx_chan + i, reg));
9235 		}
9236 		rc = 0;
9237 	}
9238 	mtx_unlock(&sc->reg_lock);
9239 	if (rc == 0)
9240 		rc = sysctl_handle_64(oidp, &val, 0, req);
9241 	return (rc);
9242 }
9243 
9244 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)9245 sysctl_temperature(SYSCTL_HANDLER_ARGS)
9246 {
9247 	struct adapter *sc = arg1;
9248 	int rc, t;
9249 	uint32_t param, val;
9250 
9251 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
9252 	if (rc)
9253 		return (rc);
9254 	if (!hw_all_ok(sc))
9255 		rc = ENXIO;
9256 	else {
9257 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9258 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9259 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
9260 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9261 	}
9262 	end_synchronized_op(sc, 0);
9263 	if (rc)
9264 		return (rc);
9265 
9266 	/* unknown is returned as 0 but we display -1 in that case */
9267 	t = val == 0 ? -1 : val;
9268 
9269 	rc = sysctl_handle_int(oidp, &t, 0, req);
9270 	return (rc);
9271 }
9272 
9273 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)9274 sysctl_vdd(SYSCTL_HANDLER_ARGS)
9275 {
9276 	struct adapter *sc = arg1;
9277 	int rc;
9278 	uint32_t param, val;
9279 
9280 	if (sc->params.core_vdd == 0) {
9281 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
9282 		    "t4vdd");
9283 		if (rc)
9284 			return (rc);
9285 		if (!hw_all_ok(sc))
9286 			rc = ENXIO;
9287 		else {
9288 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9289 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9290 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
9291 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
9292 			    &param, &val);
9293 		}
9294 		end_synchronized_op(sc, 0);
9295 		if (rc)
9296 			return (rc);
9297 		sc->params.core_vdd = val;
9298 	}
9299 
9300 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
9301 }
9302 
9303 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)9304 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
9305 {
9306 	struct adapter *sc = arg1;
9307 	int rc, v;
9308 	uint32_t param, val;
9309 
9310 	v = sc->sensor_resets;
9311 	rc = sysctl_handle_int(oidp, &v, 0, req);
9312 	if (rc != 0 || req->newptr == NULL || v <= 0)
9313 		return (rc);
9314 
9315 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
9316 	    chip_id(sc) < CHELSIO_T5)
9317 		return (ENOTSUP);
9318 
9319 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
9320 	if (rc)
9321 		return (rc);
9322 	if (!hw_all_ok(sc))
9323 		rc = ENXIO;
9324 	else {
9325 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9326 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9327 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
9328 		val = 1;
9329 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9330 	}
9331 	end_synchronized_op(sc, 0);
9332 	if (rc == 0)
9333 		sc->sensor_resets++;
9334 	return (rc);
9335 }
9336 
9337 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)9338 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
9339 {
9340 	struct adapter *sc = arg1;
9341 	struct sbuf *sb;
9342 	int rc;
9343 	uint32_t param, val;
9344 	uint8_t coreid = (uint8_t)arg2;
9345 
9346 	KASSERT(coreid < sc->params.ncores,
9347 	    ("%s: bad coreid %u\n", __func__, coreid));
9348 
9349 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
9350 	if (rc)
9351 		return (rc);
9352 	if (!hw_all_ok(sc))
9353 		rc = ENXIO;
9354 	else {
9355 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9356 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD) |
9357 		    V_FW_PARAMS_PARAM_Y(coreid);
9358 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9359 	}
9360 	end_synchronized_op(sc, 0);
9361 	if (rc)
9362 		return (rc);
9363 
9364 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9365 	if (sb == NULL)
9366 		return (ENOMEM);
9367 
9368 	if (val == 0xffffffff) {
9369 		/* Only debug and custom firmwares report load averages. */
9370 		sbuf_printf(sb, "not available");
9371 	} else {
9372 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
9373 		    (val >> 16) & 0xff);
9374 	}
9375 	rc = sbuf_finish(sb);
9376 	sbuf_delete(sb);
9377 
9378 	return (rc);
9379 }
9380 
9381 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)9382 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
9383 {
9384 	struct adapter *sc = arg1;
9385 	struct sbuf *sb;
9386 	int rc, i;
9387 	uint16_t incr[NMTUS][NCCTRL_WIN];
9388 	static const char *dec_fac[] = {
9389 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
9390 		"0.9375"
9391 	};
9392 
9393 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9394 	if (sb == NULL)
9395 		return (ENOMEM);
9396 
9397 	rc = 0;
9398 	mtx_lock(&sc->reg_lock);
9399 	if (hw_off_limits(sc))
9400 		rc = ENXIO;
9401 	else
9402 		t4_read_cong_tbl(sc, incr);
9403 	mtx_unlock(&sc->reg_lock);
9404 	if (rc)
9405 		goto done;
9406 
9407 	for (i = 0; i < NCCTRL_WIN; ++i) {
9408 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
9409 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
9410 		    incr[5][i], incr[6][i], incr[7][i]);
9411 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
9412 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
9413 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
9414 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
9415 	}
9416 
9417 	rc = sbuf_finish(sb);
9418 done:
9419 	sbuf_delete(sb);
9420 	return (rc);
9421 }
9422 
9423 static int
sysctl_cim_ibq(SYSCTL_HANDLER_ARGS)9424 sysctl_cim_ibq(SYSCTL_HANDLER_ARGS)
9425 {
9426 	struct adapter *sc = arg1;
9427 	struct sbuf *sb;
9428 	int rc, i, n, qid, coreid;
9429 	uint32_t *buf, *p;
9430 
9431 	qid = arg2 & 0xffff;
9432 	coreid = arg2 >> 16;
9433 
9434 	KASSERT(qid >= 0 && qid < sc->chip_params->cim_num_ibq,
9435 	    ("%s: bad ibq qid %d\n", __func__, qid));
9436 	KASSERT(coreid >= 0 && coreid < sc->params.ncores,
9437 	    ("%s: bad coreid %d\n", __func__, coreid));
9438 
9439 	n = 4 * CIM_IBQ_SIZE;
9440 	buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9441 	mtx_lock(&sc->reg_lock);
9442 	if (hw_off_limits(sc))
9443 		rc = -ENXIO;
9444 	else
9445 		rc = t4_read_cim_ibq_core(sc, coreid, qid, buf, n);
9446 	mtx_unlock(&sc->reg_lock);
9447 	if (rc < 0) {
9448 		rc = -rc;
9449 		goto done;
9450 	}
9451 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9452 
9453 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9454 	if (sb == NULL) {
9455 		rc = ENOMEM;
9456 		goto done;
9457 	}
9458 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9459 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9460 		    p[2], p[3]);
9461 	rc = sbuf_finish(sb);
9462 	sbuf_delete(sb);
9463 done:
9464 	free(buf, M_CXGBE);
9465 	return (rc);
9466 }
9467 
9468 static int
sysctl_cim_obq(SYSCTL_HANDLER_ARGS)9469 sysctl_cim_obq(SYSCTL_HANDLER_ARGS)
9470 {
9471 	struct adapter *sc = arg1;
9472 	struct sbuf *sb;
9473 	int rc, i, n, qid, coreid;
9474 	uint32_t *buf, *p;
9475 
9476 	qid = arg2 & 0xffff;
9477 	coreid = arg2 >> 16;
9478 
9479 	KASSERT(qid >= 0 && qid < sc->chip_params->cim_num_obq,
9480 	    ("%s: bad obq qid %d\n", __func__, qid));
9481 	KASSERT(coreid >= 0 && coreid < sc->params.ncores,
9482 	    ("%s: bad coreid %d\n", __func__, coreid));
9483 
9484 	n = 6 * CIM_OBQ_SIZE * 4;
9485 	buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9486 	mtx_lock(&sc->reg_lock);
9487 	if (hw_off_limits(sc))
9488 		rc = -ENXIO;
9489 	else
9490 		rc = t4_read_cim_obq_core(sc, coreid, qid, buf, n);
9491 	mtx_unlock(&sc->reg_lock);
9492 	if (rc < 0) {
9493 		rc = -rc;
9494 		goto done;
9495 	}
9496 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9497 
9498 	rc = sysctl_wire_old_buffer(req, 0);
9499 	if (rc != 0)
9500 		goto done;
9501 
9502 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9503 	if (sb == NULL) {
9504 		rc = ENOMEM;
9505 		goto done;
9506 	}
9507 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9508 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9509 		    p[2], p[3]);
9510 	rc = sbuf_finish(sb);
9511 	sbuf_delete(sb);
9512 done:
9513 	free(buf, M_CXGBE);
9514 	return (rc);
9515 }
9516 
9517 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9518 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9519 {
9520 	uint32_t *p;
9521 
9522 	sbuf_printf(sb, "Status   Data      PC%s",
9523 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9524 	    "     LS0Stat  LS0Addr             LS0Data");
9525 
9526 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9527 		if (cfg & F_UPDBGLACAPTPCONLY) {
9528 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9529 			    p[6], p[7]);
9530 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9531 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9532 			    p[4] & 0xff, p[5] >> 8);
9533 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9534 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9535 			    p[1] & 0xf, p[2] >> 4);
9536 		} else {
9537 			sbuf_printf(sb,
9538 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9539 			    "%08x%08x%08x%08x",
9540 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9541 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9542 			    p[6], p[7]);
9543 		}
9544 	}
9545 }
9546 
9547 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9548 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9549 {
9550 	uint32_t *p;
9551 
9552 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9553 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9554 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9555 
9556 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9557 		if (cfg & F_UPDBGLACAPTPCONLY) {
9558 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9559 			    p[3] & 0xff, p[2], p[1], p[0]);
9560 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9561 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9562 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9563 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9564 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9565 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9566 			    p[6] >> 16);
9567 		} else {
9568 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9569 			    "%08x %08x %08x %08x %08x %08x",
9570 			    (p[9] >> 16) & 0xff,
9571 			    p[9] & 0xffff, p[8] >> 16,
9572 			    p[8] & 0xffff, p[7] >> 16,
9573 			    p[7] & 0xffff, p[6] >> 16,
9574 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9575 		}
9576 	}
9577 }
9578 
9579 static int
sbuf_cim_la(struct adapter * sc,int coreid,struct sbuf * sb,int flags)9580 sbuf_cim_la(struct adapter *sc, int coreid, struct sbuf *sb, int flags)
9581 {
9582 	uint32_t cfg, *buf;
9583 	int rc;
9584 
9585 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9586 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9587 	    M_ZERO | flags);
9588 	if (buf == NULL)
9589 		return (ENOMEM);
9590 
9591 	mtx_lock(&sc->reg_lock);
9592 	if (hw_off_limits(sc))
9593 		rc = ENXIO;
9594 	else {
9595 		rc = -t4_cim_read_core(sc, 1, coreid, A_UP_UP_DBG_LA_CFG, 1,
9596 		    &cfg);
9597 		if (rc == 0)
9598 			rc = -t4_cim_read_la_core(sc, coreid, buf, NULL);
9599 	}
9600 	mtx_unlock(&sc->reg_lock);
9601 	if (rc == 0) {
9602 		if (chip_id(sc) < CHELSIO_T6)
9603 			sbuf_cim_la4(sc, sb, buf, cfg);
9604 		else
9605 			sbuf_cim_la6(sc, sb, buf, cfg);
9606 	}
9607 	free(buf, M_CXGBE);
9608 	return (rc);
9609 }
9610 
9611 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9612 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9613 {
9614 	struct adapter *sc = arg1;
9615 	int coreid = arg2;
9616 	struct sbuf *sb;
9617 	int rc;
9618 
9619 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9620 	if (sb == NULL)
9621 		return (ENOMEM);
9622 
9623 	rc = sbuf_cim_la(sc, coreid, sb, M_WAITOK);
9624 	if (rc == 0)
9625 		rc = sbuf_finish(sb);
9626 	sbuf_delete(sb);
9627 	return (rc);
9628 }
9629 
9630 static void
dump_cim_regs(struct adapter * sc)9631 dump_cim_regs(struct adapter *sc)
9632 {
9633 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9634 	    device_get_nameunit(sc->dev),
9635 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9636 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9637 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9638 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9639 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9640 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9641 	    device_get_nameunit(sc->dev),
9642 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9643 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9644 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9645 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9646 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9647 }
9648 
9649 static void
dump_cimla(struct adapter * sc)9650 dump_cimla(struct adapter *sc)
9651 {
9652 	struct sbuf sb;
9653 	int rc;
9654 
9655 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9656 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9657 		    device_get_nameunit(sc->dev));
9658 		return;
9659 	}
9660 	rc = sbuf_cim_la(sc, 0, &sb, M_WAITOK);
9661 	if (rc == 0) {
9662 		rc = sbuf_finish(&sb);
9663 		if (rc == 0) {
9664 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9665 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9666 		}
9667 	}
9668 	sbuf_delete(&sb);
9669 }
9670 
9671 void
t4_os_cim_err(struct adapter * sc)9672 t4_os_cim_err(struct adapter *sc)
9673 {
9674 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9675 }
9676 
9677 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9678 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9679 {
9680 	struct adapter *sc = arg1;
9681 	u_int i;
9682 	struct sbuf *sb;
9683 	uint32_t *buf, *p;
9684 	int rc;
9685 
9686 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9687 	if (sb == NULL)
9688 		return (ENOMEM);
9689 
9690 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9691 	    M_ZERO | M_WAITOK);
9692 
9693 	rc = 0;
9694 	mtx_lock(&sc->reg_lock);
9695 	if (hw_off_limits(sc))
9696 		rc = ENXIO;
9697 	else
9698 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9699 	mtx_unlock(&sc->reg_lock);
9700 	if (rc)
9701 		goto done;
9702 
9703 	p = buf;
9704 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9705 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9706 		    p[1], p[0]);
9707 	}
9708 
9709 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9710 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9711 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9712 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9713 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9714 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9715 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9716 		    p[0] & 1);
9717 	}
9718 	rc = sbuf_finish(sb);
9719 done:
9720 	sbuf_delete(sb);
9721 	free(buf, M_CXGBE);
9722 	return (rc);
9723 }
9724 
9725 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9726 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9727 {
9728 	struct adapter *sc = arg1;
9729 	u_int i;
9730 	struct sbuf *sb;
9731 	uint32_t *buf, *p;
9732 	int rc;
9733 
9734 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9735 	if (sb == NULL)
9736 		return (ENOMEM);
9737 
9738 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9739 	    M_ZERO | M_WAITOK);
9740 
9741 	rc = 0;
9742 	mtx_lock(&sc->reg_lock);
9743 	if (hw_off_limits(sc))
9744 		rc = ENXIO;
9745 	else
9746 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9747 	mtx_unlock(&sc->reg_lock);
9748 	if (rc)
9749 		goto done;
9750 
9751 	p = buf;
9752 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9753 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9754 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9755 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9756 		    p[4], p[3], p[2], p[1], p[0]);
9757 	}
9758 
9759 	sbuf_printf(sb, "\n\nCntl ID               Data");
9760 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9761 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9762 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9763 	}
9764 
9765 	rc = sbuf_finish(sb);
9766 done:
9767 	sbuf_delete(sb);
9768 	free(buf, M_CXGBE);
9769 	return (rc);
9770 }
9771 
9772 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9773 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9774 {
9775 	struct adapter *sc = arg1;
9776 	struct sbuf *sb;
9777 	int rc, i;
9778 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9779 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9780 	uint16_t thres[CIM_NUM_IBQ];
9781 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9782 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9783 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9784 	static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
9785 		"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
9786 		"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
9787 		"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
9788 	};
9789 
9790 	MPASS(chip_id(sc) < CHELSIO_T7);
9791 
9792 	cim_num_obq = sc->chip_params->cim_num_obq;
9793 	if (is_t4(sc)) {
9794 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9795 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9796 	} else {
9797 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9798 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9799 	}
9800 	nq = CIM_NUM_IBQ + cim_num_obq;
9801 
9802 	mtx_lock(&sc->reg_lock);
9803 	if (hw_off_limits(sc))
9804 		rc = ENXIO;
9805 	else {
9806 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9807 		if (rc == 0) {
9808 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9809 			    obq_wr);
9810 			if (rc == 0)
9811 				t4_read_cimq_cfg(sc, base, size, thres);
9812 		}
9813 	}
9814 	mtx_unlock(&sc->reg_lock);
9815 	if (rc)
9816 		return (rc);
9817 
9818 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9819 	if (sb == NULL)
9820 		return (ENOMEM);
9821 
9822 	sbuf_printf(sb,
9823 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9824 
9825 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9826 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9827 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9828 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9829 		    G_QUEREMFLITS(p[2]) * 16);
9830 	for ( ; i < nq; i++, p += 4, wr += 2)
9831 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9832 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9833 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9834 		    G_QUEREMFLITS(p[2]) * 16);
9835 
9836 	rc = sbuf_finish(sb);
9837 	sbuf_delete(sb);
9838 
9839 	return (rc);
9840 }
9841 
9842 static int
sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS)9843 sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS)
9844 {
9845 	struct adapter *sc = arg1;
9846 	u_int coreid = arg2;
9847 	struct sbuf *sb;
9848 	int rc, i;
9849 	u_int addr;
9850 	uint16_t base[CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7];
9851 	uint16_t size[CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7];
9852 	uint16_t thres[CIM_NUM_IBQ_T7];
9853 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T7], *wr = obq_wr;
9854 	uint32_t stat[4 * (CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7)], *p = stat;
9855 	static const char * const qname_ibq_t7[] = {
9856 		"TP0", "TP1", "TP2", "TP3", "ULP", "SGE0", "SGE1", "NC-SI",
9857 		"RSVD", "IPC1", "IPC2", "IPC3", "IPC4", "IPC5", "IPC6", "IPC7",
9858 	};
9859 	static const char * const qname_obq_t7[] = {
9860 		"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI", "SGE0-RX",
9861 		"RSVD", "RSVD", "IPC1", "IPC2", "IPC3", "IPC4", "IPC5",
9862 		"IPC6", "IPC7"
9863 	};
9864 	static const char * const qname_ibq_sec_t7[] = {
9865 		"TP0", "TP1", "TP2", "TP3", "ULP", "SGE0", "RSVD", "RSVD",
9866 		"RSVD", "IPC0", "RSVD", "RSVD", "RSVD", "RSVD",	"RSVD", "RSVD",
9867 	};
9868 	static const char * const qname_obq_sec_t7[] = {
9869 		"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "RSVD", "SGE0-RX",
9870 		"RSVD", "RSVD", "IPC0", "RSVD", "RSVD", "RSVD", "RSVD",
9871 		"RSVD", "RSVD",
9872 	};
9873 
9874 	MPASS(chip_id(sc) >= CHELSIO_T7);
9875 
9876 	mtx_lock(&sc->reg_lock);
9877 	if (hw_off_limits(sc))
9878 		rc = ENXIO;
9879 	else {
9880 		rc = -t4_cim_read_core(sc, 1, coreid,
9881 		    A_T7_UP_IBQ_0_SHADOW_RDADDR, 4 * CIM_NUM_IBQ_T7, stat);
9882 		if (rc != 0)
9883 			goto unlock;
9884 
9885 		rc = -t4_cim_read_core(sc, 1, coreid,
9886 		    A_T7_UP_OBQ_0_SHADOW_RDADDR, 4 * CIM_NUM_OBQ_T7,
9887 		    &stat[4 * CIM_NUM_IBQ_T7]);
9888 		if (rc != 0)
9889 			goto unlock;
9890 
9891 		addr = A_T7_UP_OBQ_0_SHADOW_REALADDR;
9892 		for (i = 0; i < CIM_NUM_OBQ_T7 * 2; i++, addr += 8) {
9893 			rc = -t4_cim_read_core(sc, 1, coreid, addr, 1,
9894 			    &obq_wr[i]);
9895 			if (rc != 0)
9896 				goto unlock;
9897 		}
9898 		t4_read_cimq_cfg_core(sc, coreid, base, size, thres);
9899 	}
9900 unlock:
9901 	mtx_unlock(&sc->reg_lock);
9902 	if (rc)
9903 		return (rc);
9904 
9905 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9906 	if (sb == NULL)
9907 		return (ENOMEM);
9908 
9909 	sbuf_printf(sb,
9910 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9911 
9912 	for (i = 0; i < CIM_NUM_IBQ_T7; i++, p += 4) {
9913 		if (!size[i])
9914 			continue;
9915 
9916 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9917 		    coreid == 0 ? qname_ibq_t7[i] : qname_ibq_sec_t7[i],
9918 		    base[i], size[i], thres[i], G_IBQRDADDR(p[0]) & 0xfff,
9919 		    G_IBQWRADDR(p[1]) & 0xfff, G_QUESOPCNT(p[3]),
9920 		    G_QUEEOPCNT(p[3]), G_T7_QUEREMFLITS(p[2]) * 16);
9921 	}
9922 
9923 	for ( ; i < CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7; i++, p += 4, wr += 2) {
9924 		if (!size[i])
9925 			continue;
9926 
9927 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u",
9928 		    coreid == 0 ? qname_obq_t7[i - CIM_NUM_IBQ_T7] :
9929 		    qname_obq_sec_t7[i - CIM_NUM_IBQ_T7],
9930 		    base[i], size[i], G_QUERDADDR(p[0]) & 0xfff,
9931 		    wr[0] << 1, G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9932 		    G_T7_QUEREMFLITS(p[2]) * 16);
9933 	}
9934 
9935 	rc = sbuf_finish(sb);
9936 	sbuf_delete(sb);
9937 	return (rc);
9938 }
9939 
9940 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9941 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9942 {
9943 	struct adapter *sc = arg1;
9944 	struct sbuf *sb;
9945 	int rc;
9946 	struct tp_cpl_stats stats;
9947 
9948 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9949 	if (sb == NULL)
9950 		return (ENOMEM);
9951 
9952 	rc = 0;
9953 	mtx_lock(&sc->reg_lock);
9954 	if (hw_off_limits(sc))
9955 		rc = ENXIO;
9956 	else
9957 		t4_tp_get_cpl_stats(sc, &stats, 0);
9958 	mtx_unlock(&sc->reg_lock);
9959 	if (rc)
9960 		goto done;
9961 
9962 	if (sc->chip_params->nchan > 2) {
9963 		sbuf_printf(sb, "                 channel 0  channel 1"
9964 		    "  channel 2  channel 3");
9965 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9966 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9967 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9968 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9969 	} else {
9970 		sbuf_printf(sb, "                 channel 0  channel 1");
9971 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9972 		    stats.req[0], stats.req[1]);
9973 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9974 		    stats.rsp[0], stats.rsp[1]);
9975 	}
9976 
9977 	rc = sbuf_finish(sb);
9978 done:
9979 	sbuf_delete(sb);
9980 	return (rc);
9981 }
9982 
9983 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9984 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9985 {
9986 	struct adapter *sc = arg1;
9987 	struct sbuf *sb;
9988 	int rc;
9989 	struct tp_usm_stats stats;
9990 
9991 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9992 	if (sb == NULL)
9993 		return (ENOMEM);
9994 
9995 	rc = 0;
9996 	mtx_lock(&sc->reg_lock);
9997 	if (hw_off_limits(sc))
9998 		rc = ENXIO;
9999 	else
10000 		t4_get_usm_stats(sc, &stats, 1);
10001 	mtx_unlock(&sc->reg_lock);
10002 	if (rc == 0) {
10003 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
10004 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
10005 		sbuf_printf(sb, "Drops:  %u", stats.drops);
10006 		rc = sbuf_finish(sb);
10007 	}
10008 	sbuf_delete(sb);
10009 
10010 	return (rc);
10011 }
10012 
10013 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)10014 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
10015 {
10016 	struct adapter *sc = arg1;
10017 	struct sbuf *sb;
10018 	int rc;
10019 	struct tp_tid_stats stats;
10020 
10021 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10022 	if (sb == NULL)
10023 		return (ENOMEM);
10024 
10025 	rc = 0;
10026 	mtx_lock(&sc->reg_lock);
10027 	if (hw_off_limits(sc))
10028 		rc = ENXIO;
10029 	else
10030 		t4_tp_get_tid_stats(sc, &stats, 1);
10031 	mtx_unlock(&sc->reg_lock);
10032 	if (rc == 0) {
10033 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
10034 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
10035 		sbuf_printf(sb, "Active:     %u\n", stats.act);
10036 		sbuf_printf(sb, "Passive:    %u", stats.pas);
10037 		rc = sbuf_finish(sb);
10038 	}
10039 	sbuf_delete(sb);
10040 
10041 	return (rc);
10042 }
10043 
10044 static const char * const devlog_level_strings[] = {
10045 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
10046 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
10047 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
10048 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
10049 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
10050 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
10051 };
10052 
10053 static const char * const devlog_facility_strings[] = {
10054 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
10055 	[FW_DEVLOG_FACILITY_CF]		= "CF",
10056 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
10057 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
10058 	[FW_DEVLOG_FACILITY_RES]	= "RES",
10059 	[FW_DEVLOG_FACILITY_HW]		= "HW",
10060 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
10061 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
10062 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
10063 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
10064 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
10065 	[FW_DEVLOG_FACILITY_VI]		= "VI",
10066 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
10067 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
10068 	[FW_DEVLOG_FACILITY_TM]		= "TM",
10069 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
10070 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
10071 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
10072 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
10073 	[FW_DEVLOG_FACILITY_RI]		= "RI",
10074 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
10075 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
10076 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
10077 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
10078 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
10079 };
10080 
10081 static int
sbuf_devlog(struct adapter * sc,int coreid,struct sbuf * sb,int flags)10082 sbuf_devlog(struct adapter *sc, int coreid, struct sbuf *sb, int flags)
10083 {
10084 	int i, j, rc, nentries, first = 0;
10085 	struct devlog_params *dparams = &sc->params.devlog;
10086 	struct fw_devlog_e *buf, *e;
10087 	uint32_t addr, size;
10088 	uint64_t ftstamp = UINT64_MAX;
10089 
10090 	KASSERT(coreid >= 0 && coreid < sc->params.ncores,
10091 	    ("%s: bad coreid %d\n", __func__, coreid));
10092 
10093 	if (dparams->addr == 0)
10094 		return (ENXIO);
10095 
10096 	size = dparams->size / sc->params.ncores;
10097 	addr = dparams->addr + coreid * size;
10098 
10099 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
10100 	buf = malloc(size, M_CXGBE, M_ZERO | flags);
10101 	if (buf == NULL)
10102 		return (ENOMEM);
10103 
10104 	mtx_lock(&sc->reg_lock);
10105 	if (hw_off_limits(sc))
10106 		rc = ENXIO;
10107 	else
10108 		rc = read_via_memwin(sc, 1, addr, (void *)buf, size);
10109 	mtx_unlock(&sc->reg_lock);
10110 	if (rc != 0)
10111 		goto done;
10112 
10113 	nentries = size / sizeof(struct fw_devlog_e);
10114 	for (i = 0; i < nentries; i++) {
10115 		e = &buf[i];
10116 
10117 		if (e->timestamp == 0)
10118 			break;	/* end */
10119 
10120 		e->timestamp = be64toh(e->timestamp);
10121 		e->seqno = be32toh(e->seqno);
10122 		for (j = 0; j < 8; j++)
10123 			e->params[j] = be32toh(e->params[j]);
10124 
10125 		if (e->timestamp < ftstamp) {
10126 			ftstamp = e->timestamp;
10127 			first = i;
10128 		}
10129 	}
10130 
10131 	if (buf[first].timestamp == 0)
10132 		goto done;	/* nothing in the log */
10133 
10134 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
10135 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
10136 
10137 	i = first;
10138 	do {
10139 		e = &buf[i];
10140 		if (e->timestamp == 0)
10141 			break;	/* end */
10142 
10143 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
10144 		    e->seqno, e->timestamp,
10145 		    (e->level < nitems(devlog_level_strings) ?
10146 			devlog_level_strings[e->level] : "UNKNOWN"),
10147 		    (e->facility < nitems(devlog_facility_strings) ?
10148 			devlog_facility_strings[e->facility] : "UNKNOWN"));
10149 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
10150 		    e->params[2], e->params[3], e->params[4],
10151 		    e->params[5], e->params[6], e->params[7]);
10152 
10153 		if (++i == nentries)
10154 			i = 0;
10155 	} while (i != first);
10156 done:
10157 	free(buf, M_CXGBE);
10158 	return (rc);
10159 }
10160 
10161 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)10162 sysctl_devlog(SYSCTL_HANDLER_ARGS)
10163 {
10164 	struct adapter *sc = arg1;
10165 	int rc, i, coreid = arg2;
10166 	struct sbuf *sb;
10167 
10168 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10169 	if (sb == NULL)
10170 		return (ENOMEM);
10171 	if (coreid == -1) {
10172 		/* -1 means all cores */
10173 		for (i = rc = 0; i < sc->params.ncores && rc == 0; i++) {
10174 			if (sc->params.ncores > 0)
10175 				sbuf_printf(sb, "=== CIM core %u ===\n", i);
10176 			rc = sbuf_devlog(sc, i, sb, M_WAITOK);
10177 		}
10178 	} else {
10179 		KASSERT(coreid >= 0 && coreid < sc->params.ncores,
10180 		    ("%s: bad coreid %d\n", __func__, coreid));
10181 		rc = sbuf_devlog(sc, coreid, sb, M_WAITOK);
10182 	}
10183 	if (rc == 0)
10184 		rc = sbuf_finish(sb);
10185 	sbuf_delete(sb);
10186 	return (rc);
10187 }
10188 
10189 static void
dump_devlog(struct adapter * sc)10190 dump_devlog(struct adapter *sc)
10191 {
10192 	int rc, i;
10193 	struct sbuf sb;
10194 
10195 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
10196 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
10197 		    device_get_nameunit(sc->dev));
10198 		return;
10199 	}
10200 	for (i = rc = 0; i < sc->params.ncores && rc == 0; i++) {
10201 		if (sc->params.ncores > 0)
10202 			sbuf_printf(&sb, "=== CIM core %u ===\n", i);
10203 		rc = sbuf_devlog(sc, i, &sb, M_WAITOK);
10204 	}
10205 	if (rc == 0) {
10206 		sbuf_finish(&sb);
10207 		log(LOG_DEBUG, "%s: device log follows.\n%s",
10208 		    device_get_nameunit(sc->dev), sbuf_data(&sb));
10209 	}
10210 	sbuf_delete(&sb);
10211 }
10212 
10213 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)10214 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
10215 {
10216 	struct adapter *sc = arg1;
10217 	struct sbuf *sb;
10218 	int rc;
10219 	struct tp_fcoe_stats stats[MAX_NCHAN];
10220 	int i, nchan = sc->chip_params->nchan;
10221 
10222 	rc = 0;
10223 	mtx_lock(&sc->reg_lock);
10224 	if (hw_off_limits(sc))
10225 		rc = ENXIO;
10226 	else {
10227 		for (i = 0; i < nchan; i++)
10228 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
10229 	}
10230 	mtx_unlock(&sc->reg_lock);
10231 	if (rc != 0)
10232 		return (rc);
10233 
10234 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10235 	if (sb == NULL)
10236 		return (ENOMEM);
10237 
10238 	if (nchan > 2) {
10239 		sbuf_printf(sb, "                   channel 0        channel 1"
10240 		    "        channel 2        channel 3");
10241 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
10242 		    stats[0].octets_ddp, stats[1].octets_ddp,
10243 		    stats[2].octets_ddp, stats[3].octets_ddp);
10244 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
10245 		    stats[0].frames_ddp, stats[1].frames_ddp,
10246 		    stats[2].frames_ddp, stats[3].frames_ddp);
10247 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
10248 		    stats[0].frames_drop, stats[1].frames_drop,
10249 		    stats[2].frames_drop, stats[3].frames_drop);
10250 	} else {
10251 		sbuf_printf(sb, "                   channel 0        channel 1");
10252 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
10253 		    stats[0].octets_ddp, stats[1].octets_ddp);
10254 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
10255 		    stats[0].frames_ddp, stats[1].frames_ddp);
10256 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
10257 		    stats[0].frames_drop, stats[1].frames_drop);
10258 	}
10259 
10260 	rc = sbuf_finish(sb);
10261 	sbuf_delete(sb);
10262 
10263 	return (rc);
10264 }
10265 
10266 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)10267 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
10268 {
10269 	struct adapter *sc = arg1;
10270 	struct sbuf *sb;
10271 	int rc, i;
10272 	unsigned int map, kbps, ipg, mode;
10273 	unsigned int pace_tab[NTX_SCHED];
10274 
10275 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
10276 	if (sb == NULL)
10277 		return (ENOMEM);
10278 
10279 	mtx_lock(&sc->reg_lock);
10280 	if (hw_off_limits(sc)) {
10281 		mtx_unlock(&sc->reg_lock);
10282 		rc = ENXIO;
10283 		goto done;
10284 	}
10285 
10286 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
10287 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
10288 	t4_read_pace_tbl(sc, pace_tab);
10289 	mtx_unlock(&sc->reg_lock);
10290 
10291 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
10292 	    "Class IPG (0.1 ns)   Flow IPG (us)");
10293 
10294 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
10295 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
10296 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
10297 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
10298 		if (kbps)
10299 			sbuf_printf(sb, "%9u     ", kbps);
10300 		else
10301 			sbuf_printf(sb, " disabled     ");
10302 
10303 		if (ipg)
10304 			sbuf_printf(sb, "%13u        ", ipg);
10305 		else
10306 			sbuf_printf(sb, "     disabled        ");
10307 
10308 		if (pace_tab[i])
10309 			sbuf_printf(sb, "%10u", pace_tab[i]);
10310 		else
10311 			sbuf_printf(sb, "  disabled");
10312 	}
10313 	rc = sbuf_finish(sb);
10314 done:
10315 	sbuf_delete(sb);
10316 	return (rc);
10317 }
10318 
10319 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)10320 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
10321 {
10322 	struct adapter *sc = arg1;
10323 	struct sbuf *sb;
10324 	int rc, i, j;
10325 	uint64_t *p0, *p1;
10326 	struct lb_port_stats s[2];
10327 	static const char *stat_name[] = {
10328 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
10329 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
10330 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
10331 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
10332 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
10333 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
10334 		"BG2FramesTrunc:", "BG3FramesTrunc:"
10335 	};
10336 
10337 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10338 	if (sb == NULL)
10339 		return (ENOMEM);
10340 
10341 	memset(s, 0, sizeof(s));
10342 
10343 	rc = 0;
10344 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
10345 		mtx_lock(&sc->reg_lock);
10346 		if (hw_off_limits(sc))
10347 			rc = ENXIO;
10348 		else {
10349 			t4_get_lb_stats(sc, i, &s[0]);
10350 			t4_get_lb_stats(sc, i + 1, &s[1]);
10351 		}
10352 		mtx_unlock(&sc->reg_lock);
10353 		if (rc != 0)
10354 			break;
10355 
10356 		p0 = &s[0].octets;
10357 		p1 = &s[1].octets;
10358 		sbuf_printf(sb, "%s                       Loopback %u"
10359 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
10360 
10361 		for (j = 0; j < nitems(stat_name); j++)
10362 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
10363 				   *p0++, *p1++);
10364 	}
10365 
10366 	if (rc == 0)
10367 		rc = sbuf_finish(sb);
10368 	sbuf_delete(sb);
10369 
10370 	return (rc);
10371 }
10372 
10373 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)10374 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
10375 {
10376 	int rc = 0;
10377 	struct port_info *pi = arg1;
10378 	struct link_config *lc = &pi->link_cfg;
10379 	struct sbuf *sb;
10380 
10381 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
10382 	if (sb == NULL)
10383 		return (ENOMEM);
10384 
10385 	if (lc->link_ok || lc->link_down_rc == 255)
10386 		sbuf_printf(sb, "n/a");
10387 	else
10388 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
10389 
10390 	rc = sbuf_finish(sb);
10391 	sbuf_delete(sb);
10392 
10393 	return (rc);
10394 }
10395 
10396 struct mem_desc {
10397 	uint64_t base;
10398 	uint64_t limit;
10399 	u_int idx;
10400 };
10401 
10402 static int
mem_desc_cmp(const void * a,const void * b)10403 mem_desc_cmp(const void *a, const void *b)
10404 {
10405 	const uint64_t v1 = ((const struct mem_desc *)a)->base;
10406 	const uint64_t v2 = ((const struct mem_desc *)b)->base;
10407 
10408 	if (v1 < v2)
10409 		return (-1);
10410 	else if (v1 > v2)
10411 		return (1);
10412 
10413 	return (0);
10414 }
10415 
10416 static void
mem_region_show(struct sbuf * sb,const char * name,uint64_t from,uint64_t to)10417 mem_region_show(struct sbuf *sb, const char *name, uint64_t from, uint64_t to)
10418 {
10419 	uintmax_t size;
10420 
10421 	if (from == to)
10422 		return;
10423 
10424 	size = to - from + 1;
10425 	if (size == 0)
10426 		return;
10427 
10428 	if (from > UINT32_MAX || to > UINT32_MAX)
10429 		sbuf_printf(sb, "%-18s 0x%012jx-0x%012jx [%ju]\n", name,
10430 		    (uintmax_t)from, (uintmax_t)to, size);
10431 	else
10432 		sbuf_printf(sb, "%-18s 0x%08jx-0x%08jx [%ju]\n", name,
10433 		    (uintmax_t)from, (uintmax_t)to, size);
10434 }
10435 
10436 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)10437 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
10438 {
10439 	struct adapter *sc = arg1;
10440 	struct sbuf *sb;
10441 	int rc, i, n, nchan;
10442 	uint32_t lo, hi, used, free, alloc;
10443 	static const char *memory[] = {
10444 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
10445 	};
10446 	static const char *region[] = {
10447 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
10448 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
10449 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
10450 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
10451 		"RQUDP region:", "PBL region:", "TXPBL region:",
10452 		"TLSKey region:", "RRQ region:", "NVMe STAG region:",
10453 		"NVMe RQ region:", "NVMe RXPBL region:", "NVMe TPT region:",
10454 		"NVMe TXPBL region:", "DBVFIFO region:", "ULPRX state:",
10455 		"ULPTX state:", "RoCE RRQ region:", "On-chip queues:",
10456 	};
10457 	struct mem_desc avail[4];
10458 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
10459 	struct mem_desc *md;
10460 
10461 	rc = sysctl_wire_old_buffer(req, 0);
10462 	if (rc != 0)
10463 		return (rc);
10464 
10465 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10466 	if (sb == NULL)
10467 		return (ENOMEM);
10468 
10469 	for (i = 0; i < nitems(mem); i++) {
10470 		mem[i].limit = 0;
10471 		mem[i].idx = i;
10472 	}
10473 
10474 	mtx_lock(&sc->reg_lock);
10475 	if (hw_off_limits(sc)) {
10476 		rc = ENXIO;
10477 		goto done;
10478 	}
10479 
10480 	/* Find and sort the populated memory ranges */
10481 	i = 0;
10482 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
10483 	if (lo & F_EDRAM0_ENABLE) {
10484 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
10485 		if (chip_id(sc) >= CHELSIO_T7) {
10486 			avail[i].base = (uint64_t)G_T7_EDRAM0_BASE(hi) << 20;
10487 			avail[i].limit = avail[i].base +
10488 			    (G_T7_EDRAM0_SIZE(hi) << 20);
10489 		} else {
10490 			avail[i].base = (uint64_t)G_EDRAM0_BASE(hi) << 20;
10491 			avail[i].limit = avail[i].base +
10492 			    (G_EDRAM0_SIZE(hi) << 20);
10493 		}
10494 		avail[i].idx = 0;
10495 		i++;
10496 	}
10497 	if (lo & F_EDRAM1_ENABLE) {
10498 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
10499 		if (chip_id(sc) >= CHELSIO_T7) {
10500 			avail[i].base = (uint64_t)G_T7_EDRAM1_BASE(hi) << 20;
10501 			avail[i].limit = avail[i].base +
10502 			    (G_T7_EDRAM1_SIZE(hi) << 20);
10503 		} else {
10504 			avail[i].base = (uint64_t)G_EDRAM1_BASE(hi) << 20;
10505 			avail[i].limit = avail[i].base +
10506 			    (G_EDRAM1_SIZE(hi) << 20);
10507 		}
10508 		avail[i].idx = 1;
10509 		i++;
10510 	}
10511 	if (lo & F_EXT_MEM_ENABLE) {
10512 		switch (chip_id(sc)) {
10513 		case CHELSIO_T4:
10514 		case CHELSIO_T6:
10515 			hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
10516 			avail[i].base = (uint64_t)G_EXT_MEM_BASE(hi) << 20;
10517 			avail[i].limit = avail[i].base +
10518 			    (G_EXT_MEM_SIZE(hi) << 20);
10519 			avail[i].idx = 2;
10520 			break;
10521 		case CHELSIO_T5:
10522 			hi = t4_read_reg(sc, A_MA_EXT_MEMORY0_BAR);
10523 			avail[i].base = (uint64_t)G_EXT_MEM0_BASE(hi) << 20;
10524 			avail[i].limit = avail[i].base +
10525 			    (G_EXT_MEM0_SIZE(hi) << 20);
10526 			avail[i].idx = 3;	/* Call it MC0 for T5 */
10527 			break;
10528 		default:
10529 			hi = t4_read_reg(sc, A_MA_EXT_MEMORY0_BAR);
10530 			avail[i].base = (uint64_t)G_T7_EXT_MEM0_BASE(hi) << 20;
10531 			avail[i].limit = avail[i].base +
10532 			    (G_T7_EXT_MEM0_SIZE(hi) << 20);
10533 			avail[i].idx = 3;	/* Call it MC0 for T7+ */
10534 			break;
10535 		}
10536 		i++;
10537 	}
10538 	if (lo & F_EXT_MEM1_ENABLE && !(lo & F_MC_SPLIT)) {
10539 		/* Only T5 and T7+ have 2 MCs. */
10540 		MPASS(is_t5(sc) || chip_id(sc) >= CHELSIO_T7);
10541 
10542 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10543 		if (chip_id(sc) >= CHELSIO_T7) {
10544 			avail[i].base = (uint64_t)G_T7_EXT_MEM1_BASE(hi) << 20;
10545 			avail[i].limit = avail[i].base +
10546 			    (G_T7_EXT_MEM1_SIZE(hi) << 20);
10547 		} else {
10548 			avail[i].base = (uint64_t)G_EXT_MEM1_BASE(hi) << 20;
10549 			avail[i].limit = avail[i].base +
10550 			    (G_EXT_MEM1_SIZE(hi) << 20);
10551 		}
10552 		avail[i].idx = 4;
10553 		i++;
10554 	}
10555 	if (lo & F_HMA_MUX) {
10556 		/* Only T6+ have HMA. */
10557 		MPASS(chip_id(sc) >= CHELSIO_T6);
10558 
10559 		if (chip_id(sc) >= CHELSIO_T7) {
10560 			hi = t4_read_reg(sc, A_MA_HOST_MEMORY_BAR);
10561 			avail[i].base = (uint64_t)G_HMATARGETBASE(hi) << 20;
10562 			avail[i].limit = avail[i].base +
10563 			    (G_T7_HMA_SIZE(hi) << 20);
10564 		} else {
10565 			hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10566 			avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
10567 			avail[i].limit = avail[i].base +
10568 			    (G_EXT_MEM1_SIZE(hi) << 20);
10569 		}
10570 		avail[i].idx = 5;
10571 		i++;
10572 	}
10573 	MPASS(i <= nitems(avail));
10574 	if (!i)                                    /* no memory available */
10575 		goto done;
10576 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
10577 
10578 	md = &mem[0];
10579 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
10580 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
10581 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
10582 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
10583 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
10584 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
10585 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
10586 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
10587 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
10588 
10589 	/* the next few have explicit upper bounds */
10590 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
10591 	md->limit = md->base - 1 +
10592 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
10593 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
10594 	md++;
10595 
10596 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
10597 	md->limit = md->base - 1 +
10598 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
10599 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
10600 	md++;
10601 
10602 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10603 		if (chip_id(sc) <= CHELSIO_T5)
10604 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
10605 		else
10606 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
10607 		md->limit = 0;
10608 	} else {
10609 		md->base = 0;
10610 		md->idx = nitems(region);  /* hide it */
10611 	}
10612 	md++;
10613 
10614 #define ulp_region(reg) do {\
10615 		const u_int shift = chip_id(sc) >= CHELSIO_T7 ? 4 : 0; \
10616 		md->base = (uint64_t)t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT) << shift; \
10617 		md->limit = (uint64_t)t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT) << shift; \
10618 		md->limit += (1 << shift) - 1; \
10619 		md++; \
10620 	} while (0)
10621 
10622 #define	hide_ulp_region() do { \
10623 		md->base = 0; \
10624 		md->idx = nitems(region); \
10625 		md++; \
10626 	} while (0)
10627 
10628 	ulp_region(RX_ISCSI);
10629 	ulp_region(RX_TDDP);
10630 	ulp_region(TX_TPT);
10631 	ulp_region(RX_STAG);
10632 	ulp_region(RX_RQ);
10633 	if (chip_id(sc) < CHELSIO_T7)
10634 		ulp_region(RX_RQUDP);
10635 	else
10636 		hide_ulp_region();
10637 	ulp_region(RX_PBL);
10638 	ulp_region(TX_PBL);
10639 	if (chip_id(sc) >= CHELSIO_T6)
10640 		ulp_region(RX_TLS_KEY);
10641 	else
10642 		hide_ulp_region();
10643 	if (chip_id(sc) >= CHELSIO_T7) {
10644 		ulp_region(RX_RRQ);
10645 		ulp_region(RX_NVME_TCP_STAG);
10646 		ulp_region(RX_NVME_TCP_RQ);
10647 		ulp_region(RX_NVME_TCP_PBL);
10648 		ulp_region(TX_NVME_TCP_TPT);
10649 		ulp_region(TX_NVME_TCP_PBL);
10650 	} else {
10651 		hide_ulp_region();
10652 		hide_ulp_region();
10653 		hide_ulp_region();
10654 		hide_ulp_region();
10655 		hide_ulp_region();
10656 		hide_ulp_region();
10657 	}
10658 #undef ulp_region
10659 #undef hide_ulp_region
10660 
10661 	md->base = 0;
10662 	if (is_t4(sc))
10663 		md->idx = nitems(region);
10664 	else {
10665 		uint32_t size = 0;
10666 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
10667 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
10668 
10669 		if (is_t5(sc)) {
10670 			if (sge_ctrl & F_VFIFO_ENABLE)
10671 				size = fifo_size << 2;
10672 		} else
10673 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
10674 
10675 		if (size) {
10676 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
10677 			md->limit = md->base + size - 1;
10678 		} else
10679 			md->idx = nitems(region);
10680 	}
10681 	md++;
10682 
10683 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
10684 	md->limit = 0;
10685 	md++;
10686 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
10687 	md->limit = 0;
10688 	md++;
10689 
10690 	if (chip_id(sc) >= CHELSIO_T7) {
10691 		t4_tp_pio_read(sc, &lo, 1, A_TP_ROCE_RRQ_BASE, false);
10692 		md->base = lo;
10693 	} else {
10694 		md->base = 0;
10695 		md->idx = nitems(region);
10696 	}
10697 	md++;
10698 
10699 	md->base = sc->vres.ocq.start;
10700 	if (sc->vres.ocq.size)
10701 		md->limit = md->base + sc->vres.ocq.size - 1;
10702 	else
10703 		md->idx = nitems(region);  /* hide it */
10704 	md++;
10705 
10706 	/* add any address-space holes, there can be up to 3 */
10707 	for (n = 0; n < i - 1; n++)
10708 		if (avail[n].limit < avail[n + 1].base)
10709 			(md++)->base = avail[n].limit;
10710 	if (avail[n].limit)
10711 		(md++)->base = avail[n].limit;
10712 
10713 	n = md - mem;
10714 	MPASS(n <= nitems(mem));
10715 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10716 
10717 	for (lo = 0; lo < i; lo++)
10718 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10719 				avail[lo].limit - 1);
10720 
10721 	sbuf_printf(sb, "\n");
10722 	for (i = 0; i < n; i++) {
10723 		if (mem[i].idx >= nitems(region))
10724 			continue;                        /* skip holes */
10725 		if (!mem[i].limit)
10726 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10727 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10728 				mem[i].limit);
10729 	}
10730 
10731 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10732 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10733 	if (hi != lo  - 1) {
10734 		sbuf_printf(sb, "\n");
10735 		mem_region_show(sb, "uP RAM:", lo, hi);
10736 	}
10737 
10738 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10739 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10740 	if (hi != lo  - 1)
10741 		mem_region_show(sb, "uP Extmem2:", lo, hi);
10742 
10743 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10744 	if (chip_id(sc) >= CHELSIO_T7)
10745 		nchan = 1 << G_T7_PMRXNUMCHN(lo);
10746 	else
10747 		nchan = lo & F_PMRXNUMCHN ? 2 : 1;
10748 	for (i = 0, free = 0; i < nchan; i++)
10749 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10750 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10751 		   G_PMRXMAXPAGE(lo), free,
10752 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10, nchan);
10753 
10754 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10755 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10756 	if (chip_id(sc) >= CHELSIO_T7)
10757 		nchan = 1 << G_T7_PMTXNUMCHN(lo);
10758 	else
10759 		nchan = 1 << G_PMTXNUMCHN(lo);
10760 	for (i = 0, free = 0; i < nchan; i++)
10761 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10762 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10763 		   G_PMTXMAXPAGE(lo), free,
10764 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10765 		   hi >= (1 << 20) ? 'M' : 'K', nchan);
10766 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10767 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10768 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10769 
10770 	for (i = 0; i < 4; i++) {
10771 		if (chip_id(sc) > CHELSIO_T5)
10772 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10773 		else
10774 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10775 		if (is_t5(sc)) {
10776 			used = G_T5_USED(lo);
10777 			alloc = G_T5_ALLOC(lo);
10778 		} else {
10779 			used = G_USED(lo);
10780 			alloc = G_ALLOC(lo);
10781 		}
10782 		/* For T6+ these are MAC buffer groups */
10783 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10784 		    i, used, alloc);
10785 	}
10786 	for (i = 0; i < sc->chip_params->nchan; i++) {
10787 		if (chip_id(sc) > CHELSIO_T5)
10788 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10789 		else
10790 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10791 		if (is_t5(sc)) {
10792 			used = G_T5_USED(lo);
10793 			alloc = G_T5_ALLOC(lo);
10794 		} else {
10795 			used = G_USED(lo);
10796 			alloc = G_ALLOC(lo);
10797 		}
10798 		/* For T6+ these are MAC buffer groups */
10799 		sbuf_printf(sb,
10800 		    "\nLoopback %d using %u pages out of %u allocated",
10801 		    i, used, alloc);
10802 	}
10803 done:
10804 	mtx_unlock(&sc->reg_lock);
10805 	if (rc == 0)
10806 		rc = sbuf_finish(sb);
10807 	sbuf_delete(sb);
10808 	return (rc);
10809 }
10810 
10811 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10812 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10813 {
10814 	*mask = x | y;
10815 	y = htobe64(y);
10816 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10817 }
10818 
10819 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10820 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10821 {
10822 	struct adapter *sc = arg1;
10823 	struct sbuf *sb;
10824 	int rc, i;
10825 
10826 	MPASS(chip_id(sc) <= CHELSIO_T5);
10827 
10828 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10829 	if (sb == NULL)
10830 		return (ENOMEM);
10831 
10832 	sbuf_printf(sb,
10833 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10834 	    "  VF              Replication             P0 P1 P2 P3  ML");
10835 	rc = 0;
10836 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10837 		uint64_t tcamx, tcamy, mask;
10838 		uint32_t cls_lo, cls_hi;
10839 		uint8_t addr[ETHER_ADDR_LEN];
10840 
10841 		mtx_lock(&sc->reg_lock);
10842 		if (hw_off_limits(sc))
10843 			rc = ENXIO;
10844 		else {
10845 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10846 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10847 		}
10848 		mtx_unlock(&sc->reg_lock);
10849 		if (rc != 0)
10850 			break;
10851 		if (tcamx & tcamy)
10852 			continue;
10853 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10854 		mtx_lock(&sc->reg_lock);
10855 		if (hw_off_limits(sc))
10856 			rc = ENXIO;
10857 		else {
10858 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10859 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10860 		}
10861 		mtx_unlock(&sc->reg_lock);
10862 		if (rc != 0)
10863 			break;
10864 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10865 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10866 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10867 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10868 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10869 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10870 
10871 		if (cls_lo & F_REPLICATE) {
10872 			struct fw_ldst_cmd ldst_cmd;
10873 
10874 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10875 			ldst_cmd.op_to_addrspace =
10876 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10877 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10878 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10879 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10880 			ldst_cmd.u.mps.rplc.fid_idx =
10881 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10882 				V_FW_LDST_CMD_IDX(i));
10883 
10884 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10885 			    "t4mps");
10886 			if (rc)
10887 				break;
10888 			if (hw_off_limits(sc))
10889 				rc = ENXIO;
10890 			else
10891 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10892 				    sizeof(ldst_cmd), &ldst_cmd);
10893 			end_synchronized_op(sc, 0);
10894 			if (rc != 0)
10895 				break;
10896 			else {
10897 				sbuf_printf(sb, " %08x %08x %08x %08x",
10898 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10899 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10900 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10901 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10902 			}
10903 		} else
10904 			sbuf_printf(sb, "%36s", "");
10905 
10906 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10907 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10908 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10909 	}
10910 
10911 	if (rc)
10912 		(void) sbuf_finish(sb);
10913 	else
10914 		rc = sbuf_finish(sb);
10915 	sbuf_delete(sb);
10916 
10917 	return (rc);
10918 }
10919 
10920 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10921 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10922 {
10923 	struct adapter *sc = arg1;
10924 	struct sbuf *sb;
10925 	int rc, i;
10926 
10927 	MPASS(chip_id(sc) == CHELSIO_T6);
10928 
10929 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10930 	if (sb == NULL)
10931 		return (ENOMEM);
10932 
10933 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10934 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10935 	    "                           Replication"
10936 	    "                                    P0 P1 P2 P3  ML");
10937 
10938 	rc = 0;
10939 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10940 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10941 		uint16_t ivlan;
10942 		uint64_t tcamx, tcamy, val, mask;
10943 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10944 		uint8_t addr[ETHER_ADDR_LEN];
10945 
10946 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10947 		if (i < 256)
10948 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10949 		else
10950 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10951 		mtx_lock(&sc->reg_lock);
10952 		if (hw_off_limits(sc))
10953 			rc = ENXIO;
10954 		else {
10955 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10956 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10957 			tcamy = G_DMACH(val) << 32;
10958 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10959 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10960 		}
10961 		mtx_unlock(&sc->reg_lock);
10962 		if (rc != 0)
10963 			break;
10964 
10965 		lookup_type = G_DATALKPTYPE(data2);
10966 		port_num = G_DATAPORTNUM(data2);
10967 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10968 			/* Inner header VNI */
10969 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10970 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10971 			dip_hit = data2 & F_DATADIPHIT;
10972 			vlan_vld = 0;
10973 		} else {
10974 			vniy = 0;
10975 			dip_hit = 0;
10976 			vlan_vld = data2 & F_DATAVIDH2;
10977 			ivlan = G_VIDL(val);
10978 		}
10979 
10980 		ctl |= V_CTLXYBITSEL(1);
10981 		mtx_lock(&sc->reg_lock);
10982 		if (hw_off_limits(sc))
10983 			rc = ENXIO;
10984 		else {
10985 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10986 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10987 			tcamx = G_DMACH(val) << 32;
10988 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10989 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10990 		}
10991 		mtx_unlock(&sc->reg_lock);
10992 		if (rc != 0)
10993 			break;
10994 
10995 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10996 			/* Inner header VNI mask */
10997 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10998 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10999 		} else
11000 			vnix = 0;
11001 
11002 		if (tcamx & tcamy)
11003 			continue;
11004 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
11005 
11006 		mtx_lock(&sc->reg_lock);
11007 		if (hw_off_limits(sc))
11008 			rc = ENXIO;
11009 		else {
11010 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
11011 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
11012 		}
11013 		mtx_unlock(&sc->reg_lock);
11014 		if (rc != 0)
11015 			break;
11016 
11017 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11018 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11019 			    "%012jx %06x %06x    -    -   %3c"
11020 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
11021 			    addr[1], addr[2], addr[3], addr[4], addr[5],
11022 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
11023 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11024 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11025 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11026 		} else {
11027 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11028 			    "%012jx    -       -   ", i, addr[0], addr[1],
11029 			    addr[2], addr[3], addr[4], addr[5],
11030 			    (uintmax_t)mask);
11031 
11032 			if (vlan_vld)
11033 				sbuf_printf(sb, "%4u   Y     ", ivlan);
11034 			else
11035 				sbuf_printf(sb, "  -    N     ");
11036 
11037 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
11038 			    lookup_type ? 'I' : 'O', port_num,
11039 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11040 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11041 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11042 		}
11043 
11044 
11045 		if (cls_lo & F_T6_REPLICATE) {
11046 			struct fw_ldst_cmd ldst_cmd;
11047 
11048 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
11049 			ldst_cmd.op_to_addrspace =
11050 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
11051 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
11052 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
11053 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
11054 			ldst_cmd.u.mps.rplc.fid_idx =
11055 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
11056 				V_FW_LDST_CMD_IDX(i));
11057 
11058 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
11059 			    "t6mps");
11060 			if (rc)
11061 				break;
11062 			if (hw_off_limits(sc))
11063 				rc = ENXIO;
11064 			else
11065 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
11066 				    sizeof(ldst_cmd), &ldst_cmd);
11067 			end_synchronized_op(sc, 0);
11068 			if (rc != 0)
11069 				break;
11070 			else {
11071 				sbuf_printf(sb, " %08x %08x %08x %08x"
11072 				    " %08x %08x %08x %08x",
11073 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
11074 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
11075 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
11076 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
11077 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
11078 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
11079 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
11080 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
11081 			}
11082 		} else
11083 			sbuf_printf(sb, "%72s", "");
11084 
11085 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
11086 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
11087 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
11088 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
11089 	}
11090 
11091 	if (rc)
11092 		(void) sbuf_finish(sb);
11093 	else
11094 		rc = sbuf_finish(sb);
11095 	sbuf_delete(sb);
11096 
11097 	return (rc);
11098 }
11099 
11100 static int
sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS)11101 sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS)
11102 {
11103 	struct adapter *sc = arg1;
11104 	struct sbuf *sb;
11105 	int rc, i;
11106 
11107 	MPASS(chip_id(sc) >= CHELSIO_T7);
11108 
11109 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11110 	if (sb == NULL)
11111 		return (ENOMEM);
11112 
11113 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
11114 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
11115 	    "                           Replication"
11116 	    "                                    P0 P1 P2 P3  ML");
11117 
11118 	rc = 0;
11119 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
11120 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
11121 		uint16_t ivlan;
11122 		uint64_t tcamx, tcamy, val, mask;
11123 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
11124 		uint8_t addr[ETHER_ADDR_LEN];
11125 
11126 		/* Read tcamy */
11127 		ctl = (V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0));
11128 		if (chip_rev(sc) == 0) {
11129 			if (i < 256)
11130 				ctl |= V_CTLTCAMINDEX(i) | V_T7_CTLTCAMSEL(0);
11131 			else
11132 				ctl |= V_CTLTCAMINDEX(i - 256) | V_T7_CTLTCAMSEL(1);
11133 		} else {
11134 #if 0
11135 			ctl = (V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0));
11136 #endif
11137 			if (i < 512)
11138 				ctl |= V_CTLTCAMINDEX(i) | V_T7_CTLTCAMSEL(0);
11139 			else if (i < 1024)
11140 				ctl |= V_CTLTCAMINDEX(i - 512) | V_T7_CTLTCAMSEL(1);
11141 			else
11142 				ctl |= V_CTLTCAMINDEX(i - 1024) | V_T7_CTLTCAMSEL(2);
11143 		}
11144 
11145 		mtx_lock(&sc->reg_lock);
11146 		if (hw_off_limits(sc))
11147 			rc = ENXIO;
11148 		else {
11149 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
11150 			val = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA1_REQ_ID1);
11151 			tcamy = G_DMACH(val) << 32;
11152 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA0_REQ_ID1);
11153 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA2_REQ_ID1);
11154 		}
11155 		mtx_unlock(&sc->reg_lock);
11156 		if (rc != 0)
11157 			break;
11158 
11159 		lookup_type = G_DATALKPTYPE(data2);
11160 		port_num = G_DATAPORTNUM(data2);
11161 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11162 			/* Inner header VNI */
11163 			vniy = (((data2 & F_DATAVIDH2) |
11164 			    G_DATAVIDH1(data2)) << 16) | G_VIDL(val);
11165 			dip_hit = data2 & F_DATADIPHIT;
11166 			vlan_vld = 0;
11167 		} else {
11168 			vniy = 0;
11169 			dip_hit = 0;
11170 			vlan_vld = data2 & F_DATAVIDH2;
11171 			ivlan = G_VIDL(val);
11172 		}
11173 
11174 		ctl |= V_CTLXYBITSEL(1);
11175 		mtx_lock(&sc->reg_lock);
11176 		if (hw_off_limits(sc))
11177 			rc = ENXIO;
11178 		else {
11179 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
11180 			val = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA1_REQ_ID1);
11181 			tcamx = G_DMACH(val) << 32;
11182 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA0_REQ_ID1);
11183 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA2_REQ_ID1);
11184 		}
11185 		mtx_unlock(&sc->reg_lock);
11186 		if (rc != 0)
11187 			break;
11188 
11189 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11190 			/* Inner header VNI mask */
11191 			vnix = (((data2 & F_DATAVIDH2) |
11192 			    G_DATAVIDH1(data2)) << 16) | G_VIDL(val);
11193 		} else
11194 			vnix = 0;
11195 
11196 		if (tcamx & tcamy)
11197 			continue;
11198 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
11199 
11200 		mtx_lock(&sc->reg_lock);
11201 		if (hw_off_limits(sc))
11202 			rc = ENXIO;
11203 		else {
11204 			if (chip_rev(sc) == 0) {
11205 				cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
11206 				cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
11207 			} else {
11208 				t4_write_reg(sc, A_MPS_CLS_SRAM_H,
11209 				    V_SRAMWRN(0) | V_SRAMINDEX(i));
11210 				cls_lo = t4_read_reg(sc, A_MPS_CLS_SRAM_L);
11211 				cls_hi = t4_read_reg(sc, A_MPS_CLS_SRAM_H);
11212 			}
11213 		}
11214 		mtx_unlock(&sc->reg_lock);
11215 		if (rc != 0)
11216 			break;
11217 
11218 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11219 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11220 			    "%012jx %06x %06x    -    -   %3c"
11221 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
11222 			    addr[1], addr[2], addr[3], addr[4], addr[5],
11223 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
11224 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11225 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11226 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11227 		} else {
11228 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11229 			    "%012jx    -       -   ", i, addr[0], addr[1],
11230 			    addr[2], addr[3], addr[4], addr[5],
11231 			    (uintmax_t)mask);
11232 
11233 			if (vlan_vld)
11234 				sbuf_printf(sb, "%4u   Y     ", ivlan);
11235 			else
11236 				sbuf_printf(sb, "  -    N     ");
11237 
11238 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
11239 			    lookup_type ? 'I' : 'O', port_num,
11240 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11241 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11242 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11243 		}
11244 
11245 		if (cls_lo & F_T6_REPLICATE) {
11246 			struct fw_ldst_cmd ldst_cmd;
11247 
11248 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
11249 			ldst_cmd.op_to_addrspace =
11250 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
11251 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
11252 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
11253 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
11254 			ldst_cmd.u.mps.rplc.fid_idx =
11255 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
11256 				V_FW_LDST_CMD_IDX(i));
11257 
11258 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
11259 			    "t6mps");
11260 			if (rc)
11261 				break;
11262 			if (hw_off_limits(sc))
11263 				rc = ENXIO;
11264 			else
11265 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
11266 				    sizeof(ldst_cmd), &ldst_cmd);
11267 			end_synchronized_op(sc, 0);
11268 			if (rc != 0)
11269 				break;
11270 			else {
11271 				sbuf_printf(sb, " %08x %08x %08x %08x"
11272 				    " %08x %08x %08x %08x",
11273 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
11274 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
11275 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
11276 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
11277 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
11278 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
11279 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
11280 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
11281 			}
11282 		} else
11283 			sbuf_printf(sb, "%72s", "");
11284 
11285 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
11286 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
11287 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
11288 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
11289 	}
11290 
11291 	if (rc)
11292 		(void) sbuf_finish(sb);
11293 	else
11294 		rc = sbuf_finish(sb);
11295 	sbuf_delete(sb);
11296 
11297 	return (rc);
11298 }
11299 
11300 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)11301 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
11302 {
11303 	struct adapter *sc = arg1;
11304 	struct sbuf *sb;
11305 	int rc;
11306 	uint16_t mtus[NMTUS];
11307 
11308 	rc = 0;
11309 	mtx_lock(&sc->reg_lock);
11310 	if (hw_off_limits(sc))
11311 		rc = ENXIO;
11312 	else
11313 		t4_read_mtu_tbl(sc, mtus, NULL);
11314 	mtx_unlock(&sc->reg_lock);
11315 	if (rc != 0)
11316 		return (rc);
11317 
11318 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11319 	if (sb == NULL)
11320 		return (ENOMEM);
11321 
11322 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
11323 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
11324 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
11325 	    mtus[14], mtus[15]);
11326 
11327 	rc = sbuf_finish(sb);
11328 	sbuf_delete(sb);
11329 
11330 	return (rc);
11331 }
11332 
11333 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)11334 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
11335 {
11336 	struct adapter *sc = arg1;
11337 	struct sbuf *sb;
11338 	int rc, i;
11339 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
11340 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
11341 	uint32_t stats[T7_PM_RX_CACHE_NSTATS];
11342 	static const char *tx_stats[MAX_PM_NSTATS] = {
11343 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
11344 		"Tx FIFO wait", NULL, "Tx latency"
11345 	};
11346 	static const char *rx_stats[MAX_PM_NSTATS] = {
11347 		"Read:", "Write bypass:", "Write mem:", "Flush:",
11348 		"Rx FIFO wait", NULL, "Rx latency"
11349 	};
11350 
11351 	rc = 0;
11352 	mtx_lock(&sc->reg_lock);
11353 	if (hw_off_limits(sc))
11354 		rc = ENXIO;
11355 	else {
11356 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
11357 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
11358 		if (chip_id(sc) >= CHELSIO_T7)
11359 			t4_pmrx_cache_get_stats(sc, stats);
11360 	}
11361 	mtx_unlock(&sc->reg_lock);
11362 	if (rc != 0)
11363 		return (rc);
11364 
11365 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11366 	if (sb == NULL)
11367 		return (ENOMEM);
11368 
11369 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
11370 	for (i = 0; i < 4; i++) {
11371 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11372 		    tx_cyc[i]);
11373 	}
11374 
11375 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
11376 	for (i = 0; i < 4; i++) {
11377 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11378 		    rx_cyc[i]);
11379 	}
11380 
11381 	if (chip_id(sc) > CHELSIO_T5) {
11382 		sbuf_printf(sb,
11383 		    "\n              Total wait      Total occupancy");
11384 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11385 		    tx_cyc[i]);
11386 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11387 		    rx_cyc[i]);
11388 
11389 		i += 2;
11390 		MPASS(i < nitems(tx_stats));
11391 
11392 		sbuf_printf(sb,
11393 		    "\n                   Reads           Total wait");
11394 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11395 		    tx_cyc[i]);
11396 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11397 		    rx_cyc[i]);
11398 	}
11399 
11400 	if (chip_id(sc) >= CHELSIO_T7) {
11401 		i = 0;
11402 		sbuf_printf(sb, "\n\nPM RX Cache Stats\n");
11403 		sbuf_printf(sb, "%-40s    %u\n", "ReqWrite", stats[i++]);
11404 		sbuf_printf(sb, "%-40s    %u\n", "ReqReadInv", stats[i++]);
11405 		sbuf_printf(sb, "%-40s    %u\n", "ReqReadNoInv", stats[i++]);
11406 		sbuf_printf(sb, "%-40s    %u\n", "Write Split Request",
11407 			   stats[i++]);
11408 		sbuf_printf(sb, "%-40s    %u\n",
11409 			   "Normal Read Split (Read Invalidate)", stats[i++]);
11410 		sbuf_printf(sb, "%-40s    %u\n",
11411 			   "Feedback Read Split (Read NoInvalidate)",
11412 			   stats[i++]);
11413 		sbuf_printf(sb, "%-40s    %u\n", "Write Hit", stats[i++]);
11414 		sbuf_printf(sb, "%-40s    %u\n", "Normal Read Hit",
11415 			   stats[i++]);
11416 		sbuf_printf(sb, "%-40s    %u\n", "Feedback Read Hit",
11417 			   stats[i++]);
11418 		sbuf_printf(sb, "%-40s    %u\n", "Normal Read Hit Full Avail",
11419 			   stats[i++]);
11420 		sbuf_printf(sb, "%-40s    %u\n", "Normal Read Hit Full UnAvail",
11421 			   stats[i++]);
11422 		sbuf_printf(sb, "%-40s    %u\n",
11423 			   "Normal Read Hit Partial Avail",
11424 			   stats[i++]);
11425 		sbuf_printf(sb, "%-40s    %u\n", "FB Read Hit Full Avail",
11426 			   stats[i++]);
11427 		sbuf_printf(sb, "%-40s    %u\n", "FB Read Hit Full UnAvail",
11428 			   stats[i++]);
11429 		sbuf_printf(sb, "%-40s    %u\n", "FB Read Hit Partial Avail",
11430 			   stats[i++]);
11431 		sbuf_printf(sb, "%-40s    %u\n", "Normal Read Full Free",
11432 			   stats[i++]);
11433 		sbuf_printf(sb, "%-40s    %u\n",
11434 			   "Normal Read Part-avail Mul-Regions",
11435 			   stats[i++]);
11436 		sbuf_printf(sb, "%-40s    %u\n",
11437 			   "FB Read Part-avail Mul-Regions",
11438 			   stats[i++]);
11439 		sbuf_printf(sb, "%-40s    %u\n", "Write Miss FL Used",
11440 			   stats[i++]);
11441 		sbuf_printf(sb, "%-40s    %u\n", "Write Miss LRU Used",
11442 			   stats[i++]);
11443 		sbuf_printf(sb, "%-40s    %u\n",
11444 			   "Write Miss LRU-Multiple Evict", stats[i++]);
11445 		sbuf_printf(sb, "%-40s    %u\n",
11446 			   "Write Hit Increasing Islands", stats[i++]);
11447 		sbuf_printf(sb, "%-40s    %u\n",
11448 			   "Normal Read Island Read split", stats[i++]);
11449 		sbuf_printf(sb, "%-40s    %u\n", "Write Overflow Eviction",
11450 			   stats[i++]);
11451 		sbuf_printf(sb, "%-40s    %u", "Read Overflow Eviction",
11452 			   stats[i++]);
11453 	}
11454 
11455 	rc = sbuf_finish(sb);
11456 	sbuf_delete(sb);
11457 
11458 	return (rc);
11459 }
11460 
11461 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)11462 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
11463 {
11464 	struct adapter *sc = arg1;
11465 	struct sbuf *sb;
11466 	int rc;
11467 	struct tp_rdma_stats stats;
11468 
11469 	rc = 0;
11470 	mtx_lock(&sc->reg_lock);
11471 	if (hw_off_limits(sc))
11472 		rc = ENXIO;
11473 	else
11474 		t4_tp_get_rdma_stats(sc, &stats, 0);
11475 	mtx_unlock(&sc->reg_lock);
11476 	if (rc != 0)
11477 		return (rc);
11478 
11479 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11480 	if (sb == NULL)
11481 		return (ENOMEM);
11482 
11483 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
11484 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
11485 
11486 	rc = sbuf_finish(sb);
11487 	sbuf_delete(sb);
11488 
11489 	return (rc);
11490 }
11491 
11492 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)11493 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
11494 {
11495 	struct adapter *sc = arg1;
11496 	struct sbuf *sb;
11497 	int rc;
11498 	struct tp_tcp_stats v4, v6;
11499 
11500 	rc = 0;
11501 	mtx_lock(&sc->reg_lock);
11502 	if (hw_off_limits(sc))
11503 		rc = ENXIO;
11504 	else
11505 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
11506 	mtx_unlock(&sc->reg_lock);
11507 	if (rc != 0)
11508 		return (rc);
11509 
11510 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11511 	if (sb == NULL)
11512 		return (ENOMEM);
11513 
11514 	sbuf_printf(sb,
11515 	    "                                IP                 IPv6\n");
11516 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
11517 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
11518 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
11519 	    v4.tcp_in_segs, v6.tcp_in_segs);
11520 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
11521 	    v4.tcp_out_segs, v6.tcp_out_segs);
11522 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
11523 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
11524 
11525 	rc = sbuf_finish(sb);
11526 	sbuf_delete(sb);
11527 
11528 	return (rc);
11529 }
11530 
11531 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)11532 sysctl_tids(SYSCTL_HANDLER_ARGS)
11533 {
11534 	struct adapter *sc = arg1;
11535 	struct sbuf *sb;
11536 	int rc;
11537 	uint32_t x, y;
11538 	struct tid_info *t = &sc->tids;
11539 
11540 	rc = 0;
11541 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11542 	if (sb == NULL)
11543 		return (ENOMEM);
11544 
11545 	if (t->natids) {
11546 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
11547 		    t->atids_in_use);
11548 	}
11549 
11550 	if (t->nhpftids) {
11551 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
11552 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
11553 	}
11554 
11555 	if (t->ntids) {
11556 		bool hashen = false;
11557 
11558 		mtx_lock(&sc->reg_lock);
11559 		if (hw_off_limits(sc))
11560 			rc = ENXIO;
11561 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
11562 			hashen = true;
11563 			if (chip_id(sc) <= CHELSIO_T5) {
11564 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
11565 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
11566 			} else {
11567 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
11568 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
11569 			}
11570 		}
11571 		mtx_unlock(&sc->reg_lock);
11572 		if (rc != 0)
11573 			goto done;
11574 
11575 		sbuf_printf(sb, "TID range: ");
11576 		if (hashen) {
11577 			if (x)
11578 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
11579 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
11580 		} else {
11581 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
11582 			    t->ntids - 1);
11583 		}
11584 		sbuf_printf(sb, ", in use: %u\n",
11585 		    atomic_load_acq_int(&t->tids_in_use));
11586 	}
11587 
11588 	if (t->nstids) {
11589 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
11590 		    t->stid_base + t->nstids - 1, t->stids_in_use);
11591 	}
11592 
11593 	if (t->nftids) {
11594 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
11595 		    t->ftid_end, t->ftids_in_use);
11596 	}
11597 
11598 	if (t->netids) {
11599 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
11600 		    t->etid_base + t->netids - 1, t->etids_in_use);
11601 	}
11602 
11603 	mtx_lock(&sc->reg_lock);
11604 	if (hw_off_limits(sc))
11605 		rc = ENXIO;
11606 	else {
11607 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
11608 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
11609 	}
11610 	mtx_unlock(&sc->reg_lock);
11611 	if (rc != 0)
11612 		goto done;
11613 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
11614 done:
11615 	if (rc == 0)
11616 		rc = sbuf_finish(sb);
11617 	else
11618 		(void)sbuf_finish(sb);
11619 	sbuf_delete(sb);
11620 
11621 	return (rc);
11622 }
11623 
11624 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)11625 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
11626 {
11627 	struct adapter *sc = arg1;
11628 	struct sbuf *sb;
11629 	int rc;
11630 	struct tp_err_stats stats;
11631 
11632 	rc = 0;
11633 	mtx_lock(&sc->reg_lock);
11634 	if (hw_off_limits(sc))
11635 		rc = ENXIO;
11636 	else
11637 		t4_tp_get_err_stats(sc, &stats, 0);
11638 	mtx_unlock(&sc->reg_lock);
11639 	if (rc != 0)
11640 		return (rc);
11641 
11642 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11643 	if (sb == NULL)
11644 		return (ENOMEM);
11645 
11646 	if (sc->chip_params->nchan > 2) {
11647 		sbuf_printf(sb, "                 channel 0  channel 1"
11648 		    "  channel 2  channel 3\n");
11649 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
11650 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
11651 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
11652 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
11653 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
11654 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
11655 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
11656 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
11657 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
11658 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
11659 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
11660 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
11661 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
11662 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
11663 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
11664 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
11665 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
11666 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
11667 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
11668 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
11669 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
11670 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
11671 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
11672 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
11673 	} else {
11674 		sbuf_printf(sb, "                 channel 0  channel 1\n");
11675 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
11676 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
11677 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
11678 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
11679 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
11680 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
11681 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
11682 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
11683 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
11684 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
11685 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
11686 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
11687 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
11688 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
11689 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
11690 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
11691 	}
11692 
11693 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
11694 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
11695 
11696 	rc = sbuf_finish(sb);
11697 	sbuf_delete(sb);
11698 
11699 	return (rc);
11700 }
11701 
11702 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)11703 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
11704 {
11705 	struct adapter *sc = arg1;
11706 	struct sbuf *sb;
11707 	int rc;
11708 	struct tp_tnl_stats stats;
11709 
11710 	rc = 0;
11711 	mtx_lock(&sc->reg_lock);
11712 	if (hw_off_limits(sc))
11713 		rc = ENXIO;
11714 	else
11715 		t4_tp_get_tnl_stats(sc, &stats, 1);
11716 	mtx_unlock(&sc->reg_lock);
11717 	if (rc != 0)
11718 		return (rc);
11719 
11720 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11721 	if (sb == NULL)
11722 		return (ENOMEM);
11723 
11724 	if (sc->chip_params->nchan > 2) {
11725 		sbuf_printf(sb, "           channel 0  channel 1"
11726 		    "  channel 2  channel 3\n");
11727 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
11728 		    stats.out_pkt[0], stats.out_pkt[1],
11729 		    stats.out_pkt[2], stats.out_pkt[3]);
11730 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
11731 		    stats.in_pkt[0], stats.in_pkt[1],
11732 		    stats.in_pkt[2], stats.in_pkt[3]);
11733 	} else {
11734 		sbuf_printf(sb, "           channel 0  channel 1\n");
11735 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
11736 		    stats.out_pkt[0], stats.out_pkt[1]);
11737 		sbuf_printf(sb, "InPkts:   %10u %10u",
11738 		    stats.in_pkt[0], stats.in_pkt[1]);
11739 	}
11740 
11741 	rc = sbuf_finish(sb);
11742 	sbuf_delete(sb);
11743 
11744 	return (rc);
11745 }
11746 
11747 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)11748 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
11749 {
11750 	struct adapter *sc = arg1;
11751 	struct tp_params *tpp = &sc->params.tp;
11752 	u_int mask;
11753 	int rc;
11754 
11755 	mask = tpp->la_mask >> 16;
11756 	rc = sysctl_handle_int(oidp, &mask, 0, req);
11757 	if (rc != 0 || req->newptr == NULL)
11758 		return (rc);
11759 	if (mask > 0xffff)
11760 		return (EINVAL);
11761 	mtx_lock(&sc->reg_lock);
11762 	if (hw_off_limits(sc))
11763 		rc = ENXIO;
11764 	else {
11765 		tpp->la_mask = mask << 16;
11766 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
11767 		    tpp->la_mask);
11768 	}
11769 	mtx_unlock(&sc->reg_lock);
11770 
11771 	return (rc);
11772 }
11773 
11774 struct field_desc {
11775 	const char *name;
11776 	u_int start;
11777 	u_int width;
11778 };
11779 
11780 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)11781 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
11782 {
11783 	char buf[32];
11784 	int line_size = 0;
11785 
11786 	while (f->name) {
11787 		uint64_t mask = (1ULL << f->width) - 1;
11788 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
11789 		    ((uintmax_t)v >> f->start) & mask);
11790 
11791 		if (line_size + len >= 79) {
11792 			line_size = 8;
11793 			sbuf_printf(sb, "\n        ");
11794 		}
11795 		sbuf_printf(sb, "%s ", buf);
11796 		line_size += len + 1;
11797 		f++;
11798 	}
11799 	sbuf_printf(sb, "\n");
11800 }
11801 
11802 static const struct field_desc tp_la0[] = {
11803 	{ "RcfOpCodeOut", 60, 4 },
11804 	{ "State", 56, 4 },
11805 	{ "WcfState", 52, 4 },
11806 	{ "RcfOpcSrcOut", 50, 2 },
11807 	{ "CRxError", 49, 1 },
11808 	{ "ERxError", 48, 1 },
11809 	{ "SanityFailed", 47, 1 },
11810 	{ "SpuriousMsg", 46, 1 },
11811 	{ "FlushInputMsg", 45, 1 },
11812 	{ "FlushInputCpl", 44, 1 },
11813 	{ "RssUpBit", 43, 1 },
11814 	{ "RssFilterHit", 42, 1 },
11815 	{ "Tid", 32, 10 },
11816 	{ "InitTcb", 31, 1 },
11817 	{ "LineNumber", 24, 7 },
11818 	{ "Emsg", 23, 1 },
11819 	{ "EdataOut", 22, 1 },
11820 	{ "Cmsg", 21, 1 },
11821 	{ "CdataOut", 20, 1 },
11822 	{ "EreadPdu", 19, 1 },
11823 	{ "CreadPdu", 18, 1 },
11824 	{ "TunnelPkt", 17, 1 },
11825 	{ "RcfPeerFin", 16, 1 },
11826 	{ "RcfReasonOut", 12, 4 },
11827 	{ "TxCchannel", 10, 2 },
11828 	{ "RcfTxChannel", 8, 2 },
11829 	{ "RxEchannel", 6, 2 },
11830 	{ "RcfRxChannel", 5, 1 },
11831 	{ "RcfDataOutSrdy", 4, 1 },
11832 	{ "RxDvld", 3, 1 },
11833 	{ "RxOoDvld", 2, 1 },
11834 	{ "RxCongestion", 1, 1 },
11835 	{ "TxCongestion", 0, 1 },
11836 	{ NULL }
11837 };
11838 
11839 static const struct field_desc tp_la1[] = {
11840 	{ "CplCmdIn", 56, 8 },
11841 	{ "CplCmdOut", 48, 8 },
11842 	{ "ESynOut", 47, 1 },
11843 	{ "EAckOut", 46, 1 },
11844 	{ "EFinOut", 45, 1 },
11845 	{ "ERstOut", 44, 1 },
11846 	{ "SynIn", 43, 1 },
11847 	{ "AckIn", 42, 1 },
11848 	{ "FinIn", 41, 1 },
11849 	{ "RstIn", 40, 1 },
11850 	{ "DataIn", 39, 1 },
11851 	{ "DataInVld", 38, 1 },
11852 	{ "PadIn", 37, 1 },
11853 	{ "RxBufEmpty", 36, 1 },
11854 	{ "RxDdp", 35, 1 },
11855 	{ "RxFbCongestion", 34, 1 },
11856 	{ "TxFbCongestion", 33, 1 },
11857 	{ "TxPktSumSrdy", 32, 1 },
11858 	{ "RcfUlpType", 28, 4 },
11859 	{ "Eread", 27, 1 },
11860 	{ "Ebypass", 26, 1 },
11861 	{ "Esave", 25, 1 },
11862 	{ "Static0", 24, 1 },
11863 	{ "Cread", 23, 1 },
11864 	{ "Cbypass", 22, 1 },
11865 	{ "Csave", 21, 1 },
11866 	{ "CPktOut", 20, 1 },
11867 	{ "RxPagePoolFull", 18, 2 },
11868 	{ "RxLpbkPkt", 17, 1 },
11869 	{ "TxLpbkPkt", 16, 1 },
11870 	{ "RxVfValid", 15, 1 },
11871 	{ "SynLearned", 14, 1 },
11872 	{ "SetDelEntry", 13, 1 },
11873 	{ "SetInvEntry", 12, 1 },
11874 	{ "CpcmdDvld", 11, 1 },
11875 	{ "CpcmdSave", 10, 1 },
11876 	{ "RxPstructsFull", 8, 2 },
11877 	{ "EpcmdDvld", 7, 1 },
11878 	{ "EpcmdFlush", 6, 1 },
11879 	{ "EpcmdTrimPrefix", 5, 1 },
11880 	{ "EpcmdTrimPostfix", 4, 1 },
11881 	{ "ERssIp4Pkt", 3, 1 },
11882 	{ "ERssIp6Pkt", 2, 1 },
11883 	{ "ERssTcpUdpPkt", 1, 1 },
11884 	{ "ERssFceFipPkt", 0, 1 },
11885 	{ NULL }
11886 };
11887 
11888 static const struct field_desc tp_la2[] = {
11889 	{ "CplCmdIn", 56, 8 },
11890 	{ "MpsVfVld", 55, 1 },
11891 	{ "MpsPf", 52, 3 },
11892 	{ "MpsVf", 44, 8 },
11893 	{ "SynIn", 43, 1 },
11894 	{ "AckIn", 42, 1 },
11895 	{ "FinIn", 41, 1 },
11896 	{ "RstIn", 40, 1 },
11897 	{ "DataIn", 39, 1 },
11898 	{ "DataInVld", 38, 1 },
11899 	{ "PadIn", 37, 1 },
11900 	{ "RxBufEmpty", 36, 1 },
11901 	{ "RxDdp", 35, 1 },
11902 	{ "RxFbCongestion", 34, 1 },
11903 	{ "TxFbCongestion", 33, 1 },
11904 	{ "TxPktSumSrdy", 32, 1 },
11905 	{ "RcfUlpType", 28, 4 },
11906 	{ "Eread", 27, 1 },
11907 	{ "Ebypass", 26, 1 },
11908 	{ "Esave", 25, 1 },
11909 	{ "Static0", 24, 1 },
11910 	{ "Cread", 23, 1 },
11911 	{ "Cbypass", 22, 1 },
11912 	{ "Csave", 21, 1 },
11913 	{ "CPktOut", 20, 1 },
11914 	{ "RxPagePoolFull", 18, 2 },
11915 	{ "RxLpbkPkt", 17, 1 },
11916 	{ "TxLpbkPkt", 16, 1 },
11917 	{ "RxVfValid", 15, 1 },
11918 	{ "SynLearned", 14, 1 },
11919 	{ "SetDelEntry", 13, 1 },
11920 	{ "SetInvEntry", 12, 1 },
11921 	{ "CpcmdDvld", 11, 1 },
11922 	{ "CpcmdSave", 10, 1 },
11923 	{ "RxPstructsFull", 8, 2 },
11924 	{ "EpcmdDvld", 7, 1 },
11925 	{ "EpcmdFlush", 6, 1 },
11926 	{ "EpcmdTrimPrefix", 5, 1 },
11927 	{ "EpcmdTrimPostfix", 4, 1 },
11928 	{ "ERssIp4Pkt", 3, 1 },
11929 	{ "ERssIp6Pkt", 2, 1 },
11930 	{ "ERssTcpUdpPkt", 1, 1 },
11931 	{ "ERssFceFipPkt", 0, 1 },
11932 	{ NULL }
11933 };
11934 
11935 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)11936 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
11937 {
11938 
11939 	field_desc_show(sb, *p, tp_la0);
11940 }
11941 
11942 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)11943 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
11944 {
11945 
11946 	if (idx)
11947 		sbuf_printf(sb, "\n");
11948 	field_desc_show(sb, p[0], tp_la0);
11949 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11950 		field_desc_show(sb, p[1], tp_la0);
11951 }
11952 
11953 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)11954 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
11955 {
11956 
11957 	if (idx)
11958 		sbuf_printf(sb, "\n");
11959 	field_desc_show(sb, p[0], tp_la0);
11960 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11961 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
11962 }
11963 
11964 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)11965 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
11966 {
11967 	struct adapter *sc = arg1;
11968 	struct sbuf *sb;
11969 	uint64_t *buf, *p;
11970 	int rc;
11971 	u_int i, inc;
11972 	void (*show_func)(struct sbuf *, uint64_t *, int);
11973 
11974 	rc = 0;
11975 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11976 	if (sb == NULL)
11977 		return (ENOMEM);
11978 
11979 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11980 
11981 	mtx_lock(&sc->reg_lock);
11982 	if (hw_off_limits(sc))
11983 		rc = ENXIO;
11984 	else {
11985 		t4_tp_read_la(sc, buf, NULL);
11986 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11987 		case 2:
11988 			inc = 2;
11989 			show_func = tp_la_show2;
11990 			break;
11991 		case 3:
11992 			inc = 2;
11993 			show_func = tp_la_show3;
11994 			break;
11995 		default:
11996 			inc = 1;
11997 			show_func = tp_la_show;
11998 		}
11999 	}
12000 	mtx_unlock(&sc->reg_lock);
12001 	if (rc != 0)
12002 		goto done;
12003 
12004 	p = buf;
12005 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
12006 		(*show_func)(sb, p, i);
12007 	rc = sbuf_finish(sb);
12008 done:
12009 	sbuf_delete(sb);
12010 	free(buf, M_CXGBE);
12011 	return (rc);
12012 }
12013 
12014 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)12015 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
12016 {
12017 	struct adapter *sc = arg1;
12018 	struct sbuf *sb;
12019 	int rc;
12020 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
12021 
12022 	rc = 0;
12023 	mtx_lock(&sc->reg_lock);
12024 	if (hw_off_limits(sc))
12025 		rc = ENXIO;
12026 	else
12027 		t4_get_chan_txrate(sc, nrate, orate);
12028 	mtx_unlock(&sc->reg_lock);
12029 	if (rc != 0)
12030 		return (rc);
12031 
12032 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
12033 	if (sb == NULL)
12034 		return (ENOMEM);
12035 
12036 	if (sc->chip_params->nchan > 2) {
12037 		sbuf_printf(sb, "              channel 0   channel 1"
12038 		    "   channel 2   channel 3\n");
12039 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
12040 		    nrate[0], nrate[1], nrate[2], nrate[3]);
12041 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
12042 		    orate[0], orate[1], orate[2], orate[3]);
12043 	} else {
12044 		sbuf_printf(sb, "              channel 0   channel 1\n");
12045 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
12046 		    nrate[0], nrate[1]);
12047 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
12048 		    orate[0], orate[1]);
12049 	}
12050 
12051 	rc = sbuf_finish(sb);
12052 	sbuf_delete(sb);
12053 
12054 	return (rc);
12055 }
12056 
12057 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)12058 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
12059 {
12060 	struct adapter *sc = arg1;
12061 	struct sbuf *sb;
12062 	uint32_t *buf, *p;
12063 	int rc, i;
12064 
12065 	rc = 0;
12066 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12067 	if (sb == NULL)
12068 		return (ENOMEM);
12069 
12070 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
12071 	    M_ZERO | M_WAITOK);
12072 
12073 	mtx_lock(&sc->reg_lock);
12074 	if (hw_off_limits(sc))
12075 		rc = ENXIO;
12076 	else
12077 		t4_ulprx_read_la(sc, buf);
12078 	mtx_unlock(&sc->reg_lock);
12079 	if (rc != 0)
12080 		goto done;
12081 
12082 	p = buf;
12083 	sbuf_printf(sb, "      Pcmd        Type   Message"
12084 	    "                Data");
12085 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
12086 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
12087 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
12088 	}
12089 	rc = sbuf_finish(sb);
12090 done:
12091 	sbuf_delete(sb);
12092 	free(buf, M_CXGBE);
12093 	return (rc);
12094 }
12095 
12096 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)12097 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
12098 {
12099 	struct adapter *sc = arg1;
12100 	struct sbuf *sb;
12101 	int rc;
12102 	uint32_t cfg, s1, s2;
12103 
12104 	MPASS(chip_id(sc) >= CHELSIO_T5);
12105 
12106 	rc = 0;
12107 	mtx_lock(&sc->reg_lock);
12108 	if (hw_off_limits(sc))
12109 		rc = ENXIO;
12110 	else {
12111 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
12112 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
12113 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
12114 	}
12115 	mtx_unlock(&sc->reg_lock);
12116 	if (rc != 0)
12117 		return (rc);
12118 
12119 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12120 	if (sb == NULL)
12121 		return (ENOMEM);
12122 
12123 	if (G_STATSOURCE_T5(cfg) == 7) {
12124 		int mode;
12125 
12126 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
12127 		if (mode == 0)
12128 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
12129 		else if (mode == 1)
12130 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
12131 		else
12132 			sbuf_printf(sb, "unknown mode %d", mode);
12133 	}
12134 	rc = sbuf_finish(sb);
12135 	sbuf_delete(sb);
12136 
12137 	return (rc);
12138 }
12139 
12140 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)12141 sysctl_cpus(SYSCTL_HANDLER_ARGS)
12142 {
12143 	struct adapter *sc = arg1;
12144 	enum cpu_sets op = arg2;
12145 	cpuset_t cpuset;
12146 	struct sbuf *sb;
12147 	int i, rc;
12148 
12149 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
12150 
12151 	CPU_ZERO(&cpuset);
12152 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
12153 	if (rc != 0)
12154 		return (rc);
12155 
12156 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12157 	if (sb == NULL)
12158 		return (ENOMEM);
12159 
12160 	CPU_FOREACH(i)
12161 		sbuf_printf(sb, "%d ", i);
12162 	rc = sbuf_finish(sb);
12163 	sbuf_delete(sb);
12164 
12165 	return (rc);
12166 }
12167 
12168 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)12169 sysctl_reset(SYSCTL_HANDLER_ARGS)
12170 {
12171 	struct adapter *sc = arg1;
12172 	u_int val;
12173 	int rc;
12174 
12175 	val = atomic_load_int(&sc->num_resets);
12176 	rc = sysctl_handle_int(oidp, &val, 0, req);
12177 	if (rc != 0 || req->newptr == NULL)
12178 		return (rc);
12179 
12180 	if (val == 0) {
12181 		/* Zero out the counter that tracks reset. */
12182 		atomic_store_int(&sc->num_resets, 0);
12183 		return (0);
12184 	}
12185 
12186 	if (val != 1)
12187 		return (EINVAL);	/* 0 or 1 are the only legal values */
12188 
12189 	if (hw_off_limits(sc))		/* harmless race */
12190 		return (EALREADY);
12191 
12192 	taskqueue_enqueue(reset_tq, &sc->reset_task);
12193 	return (0);
12194 }
12195 
12196 #ifdef TCP_OFFLOAD
12197 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)12198 sysctl_tls(SYSCTL_HANDLER_ARGS)
12199 {
12200 	struct adapter *sc = arg1;
12201 	int i, j, v, rc;
12202 	struct vi_info *vi;
12203 
12204 	v = sc->tt.tls;
12205 	rc = sysctl_handle_int(oidp, &v, 0, req);
12206 	if (rc != 0 || req->newptr == NULL)
12207 		return (rc);
12208 
12209 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
12210 		return (ENOTSUP);
12211 
12212 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
12213 	if (rc)
12214 		return (rc);
12215 	if (hw_off_limits(sc))
12216 		rc = ENXIO;
12217 	else {
12218 		sc->tt.tls = !!v;
12219 		for_each_port(sc, i) {
12220 			for_each_vi(sc->port[i], j, vi) {
12221 				if (vi->flags & VI_INIT_DONE)
12222 					t4_update_fl_bufsize(vi->ifp);
12223 			}
12224 		}
12225 	}
12226 	end_synchronized_op(sc, 0);
12227 
12228 	return (rc);
12229 
12230 }
12231 
12232 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)12233 unit_conv(char *buf, size_t len, u_int val, u_int factor)
12234 {
12235 	u_int rem = val % factor;
12236 
12237 	if (rem == 0)
12238 		snprintf(buf, len, "%u", val / factor);
12239 	else {
12240 		while (rem % 10 == 0)
12241 			rem /= 10;
12242 		snprintf(buf, len, "%u.%u", val / factor, rem);
12243 	}
12244 }
12245 
12246 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)12247 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
12248 {
12249 	struct adapter *sc = arg1;
12250 	char buf[16];
12251 	u_int res, re;
12252 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12253 
12254 	mtx_lock(&sc->reg_lock);
12255 	if (hw_off_limits(sc))
12256 		res = (u_int)-1;
12257 	else
12258 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
12259 	mtx_unlock(&sc->reg_lock);
12260 	if (res == (u_int)-1)
12261 		return (ENXIO);
12262 
12263 	switch (arg2) {
12264 	case 0:
12265 		/* timer_tick */
12266 		re = G_TIMERRESOLUTION(res);
12267 		break;
12268 	case 1:
12269 		/* TCP timestamp tick */
12270 		re = G_TIMESTAMPRESOLUTION(res);
12271 		break;
12272 	case 2:
12273 		/* DACK tick */
12274 		re = G_DELAYEDACKRESOLUTION(res);
12275 		break;
12276 	default:
12277 		return (EDOOFUS);
12278 	}
12279 
12280 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
12281 
12282 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
12283 }
12284 
12285 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)12286 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
12287 {
12288 	struct adapter *sc = arg1;
12289 	int rc;
12290 	u_int dack_tmr, dack_re, v;
12291 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12292 
12293 	mtx_lock(&sc->reg_lock);
12294 	if (hw_off_limits(sc))
12295 		rc = ENXIO;
12296 	else {
12297 		rc = 0;
12298 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
12299 		    A_TP_TIMER_RESOLUTION));
12300 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
12301 	}
12302 	mtx_unlock(&sc->reg_lock);
12303 	if (rc != 0)
12304 		return (rc);
12305 
12306 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
12307 
12308 	return (sysctl_handle_int(oidp, &v, 0, req));
12309 }
12310 
12311 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)12312 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
12313 {
12314 	struct adapter *sc = arg1;
12315 	int rc, reg = arg2;
12316 	u_int tre;
12317 	u_long tp_tick_us, v;
12318 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12319 
12320 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
12321 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
12322 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
12323 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
12324 
12325 	mtx_lock(&sc->reg_lock);
12326 	if (hw_off_limits(sc))
12327 		rc = ENXIO;
12328 	else {
12329 		rc = 0;
12330 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
12331 		tp_tick_us = (cclk_ps << tre) / 1000000;
12332 		if (reg == A_TP_INIT_SRTT)
12333 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
12334 		else
12335 			v = tp_tick_us * t4_read_reg(sc, reg);
12336 	}
12337 	mtx_unlock(&sc->reg_lock);
12338 	if (rc != 0)
12339 		return (rc);
12340 	else
12341 		return (sysctl_handle_long(oidp, &v, 0, req));
12342 }
12343 
12344 /*
12345  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
12346  * passed to this function.
12347  */
12348 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)12349 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
12350 {
12351 	struct adapter *sc = arg1;
12352 	int rc, idx = arg2;
12353 	u_int v;
12354 
12355 	MPASS(idx >= 0 && idx <= 24);
12356 
12357 	mtx_lock(&sc->reg_lock);
12358 	if (hw_off_limits(sc))
12359 		rc = ENXIO;
12360 	else {
12361 		rc = 0;
12362 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
12363 	}
12364 	mtx_unlock(&sc->reg_lock);
12365 	if (rc != 0)
12366 		return (rc);
12367 	else
12368 		return (sysctl_handle_int(oidp, &v, 0, req));
12369 }
12370 
12371 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)12372 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
12373 {
12374 	struct adapter *sc = arg1;
12375 	int rc, idx = arg2;
12376 	u_int shift, v, r;
12377 
12378 	MPASS(idx >= 0 && idx < 16);
12379 
12380 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
12381 	shift = (idx & 3) << 3;
12382 	mtx_lock(&sc->reg_lock);
12383 	if (hw_off_limits(sc))
12384 		rc = ENXIO;
12385 	else {
12386 		rc = 0;
12387 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
12388 	}
12389 	mtx_unlock(&sc->reg_lock);
12390 	if (rc != 0)
12391 		return (rc);
12392 	else
12393 		return (sysctl_handle_int(oidp, &v, 0, req));
12394 }
12395 
12396 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)12397 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
12398 {
12399 	struct vi_info *vi = arg1;
12400 	struct adapter *sc = vi->adapter;
12401 	int idx, rc, i;
12402 	struct sge_ofld_rxq *ofld_rxq;
12403 	uint8_t v;
12404 
12405 	idx = vi->ofld_tmr_idx;
12406 
12407 	rc = sysctl_handle_int(oidp, &idx, 0, req);
12408 	if (rc != 0 || req->newptr == NULL)
12409 		return (rc);
12410 
12411 	if (idx < 0 || idx >= SGE_NTIMERS)
12412 		return (EINVAL);
12413 
12414 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
12415 	    "t4otmr");
12416 	if (rc)
12417 		return (rc);
12418 
12419 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
12420 	for_each_ofld_rxq(vi, i, ofld_rxq) {
12421 #ifdef atomic_store_rel_8
12422 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
12423 #else
12424 		ofld_rxq->iq.intr_params = v;
12425 #endif
12426 	}
12427 	vi->ofld_tmr_idx = idx;
12428 
12429 	end_synchronized_op(sc, LOCK_HELD);
12430 	return (0);
12431 }
12432 
12433 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)12434 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
12435 {
12436 	struct vi_info *vi = arg1;
12437 	struct adapter *sc = vi->adapter;
12438 	int idx, rc;
12439 
12440 	idx = vi->ofld_pktc_idx;
12441 
12442 	rc = sysctl_handle_int(oidp, &idx, 0, req);
12443 	if (rc != 0 || req->newptr == NULL)
12444 		return (rc);
12445 
12446 	if (idx < -1 || idx >= SGE_NCOUNTERS)
12447 		return (EINVAL);
12448 
12449 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
12450 	    "t4opktc");
12451 	if (rc)
12452 		return (rc);
12453 
12454 	if (vi->flags & VI_INIT_DONE)
12455 		rc = EBUSY; /* cannot be changed once the queues are created */
12456 	else
12457 		vi->ofld_pktc_idx = idx;
12458 
12459 	end_synchronized_op(sc, LOCK_HELD);
12460 	return (rc);
12461 }
12462 #endif
12463 
12464 static int
get_sge_context(struct adapter * sc,int mem_id,uint32_t cid,int len,uint32_t * data)12465 get_sge_context(struct adapter *sc, int mem_id, uint32_t cid, int len,
12466     uint32_t *data)
12467 {
12468 	int rc;
12469 
12470 	if (len < sc->chip_params->sge_ctxt_size)
12471 		return (ENOBUFS);
12472 	if (cid > M_CTXTQID)
12473 		return (EINVAL);
12474 	if (mem_id != CTXT_EGRESS && mem_id != CTXT_INGRESS &&
12475 	    mem_id != CTXT_FLM && mem_id != CTXT_CNM)
12476 		return (EINVAL);
12477 
12478 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
12479 	if (rc)
12480 		return (rc);
12481 
12482 	if (hw_off_limits(sc)) {
12483 		rc = ENXIO;
12484 		goto done;
12485 	}
12486 
12487 	if (sc->flags & FW_OK) {
12488 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cid, mem_id, data);
12489 		if (rc == 0)
12490 			goto done;
12491 	}
12492 
12493 	/*
12494 	 * Read via firmware failed or wasn't even attempted.  Read directly via
12495 	 * the backdoor.
12496 	 */
12497 	rc = -t4_sge_ctxt_rd_bd(sc, cid, mem_id, data);
12498 done:
12499 	end_synchronized_op(sc, 0);
12500 	return (rc);
12501 }
12502 
12503 static int
load_fw(struct adapter * sc,struct t4_data * fw)12504 load_fw(struct adapter *sc, struct t4_data *fw)
12505 {
12506 	int rc;
12507 	uint8_t *fw_data;
12508 
12509 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
12510 	if (rc)
12511 		return (rc);
12512 
12513 	if (hw_off_limits(sc)) {
12514 		rc = ENXIO;
12515 		goto done;
12516 	}
12517 
12518 	/*
12519 	 * The firmware, with the sole exception of the memory parity error
12520 	 * handler, runs from memory and not flash.  It is almost always safe to
12521 	 * install a new firmware on a running system.  Just set bit 1 in
12522 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
12523 	 */
12524 	if (sc->flags & FULL_INIT_DONE &&
12525 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
12526 		rc = EBUSY;
12527 		goto done;
12528 	}
12529 
12530 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
12531 
12532 	rc = copyin(fw->data, fw_data, fw->len);
12533 	if (rc == 0)
12534 		rc = -t4_load_fw(sc, fw_data, fw->len);
12535 
12536 	free(fw_data, M_CXGBE);
12537 done:
12538 	end_synchronized_op(sc, 0);
12539 	return (rc);
12540 }
12541 
12542 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)12543 load_cfg(struct adapter *sc, struct t4_data *cfg)
12544 {
12545 	int rc;
12546 	uint8_t *cfg_data = NULL;
12547 
12548 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
12549 	if (rc)
12550 		return (rc);
12551 
12552 	if (hw_off_limits(sc)) {
12553 		rc = ENXIO;
12554 		goto done;
12555 	}
12556 
12557 	if (cfg->len == 0) {
12558 		/* clear */
12559 		rc = -t4_load_cfg(sc, NULL, 0);
12560 		goto done;
12561 	}
12562 
12563 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
12564 
12565 	rc = copyin(cfg->data, cfg_data, cfg->len);
12566 	if (rc == 0)
12567 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
12568 
12569 	free(cfg_data, M_CXGBE);
12570 done:
12571 	end_synchronized_op(sc, 0);
12572 	return (rc);
12573 }
12574 
12575 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)12576 load_boot(struct adapter *sc, struct t4_bootrom *br)
12577 {
12578 	int rc;
12579 	uint8_t *br_data = NULL;
12580 	u_int offset;
12581 
12582 	if (br->len > 1024 * 1024)
12583 		return (EFBIG);
12584 
12585 	if (br->pf_offset == 0) {
12586 		/* pfidx */
12587 		if (br->pfidx_addr > 7)
12588 			return (EINVAL);
12589 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
12590 		    A_PCIE_PF_EXPROM_OFST)));
12591 	} else if (br->pf_offset == 1) {
12592 		/* offset */
12593 		offset = G_OFFSET(br->pfidx_addr);
12594 	} else {
12595 		return (EINVAL);
12596 	}
12597 
12598 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
12599 	if (rc)
12600 		return (rc);
12601 
12602 	if (hw_off_limits(sc)) {
12603 		rc = ENXIO;
12604 		goto done;
12605 	}
12606 
12607 	if (br->len == 0) {
12608 		/* clear */
12609 		rc = -t4_load_boot(sc, NULL, offset, 0);
12610 		goto done;
12611 	}
12612 
12613 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
12614 
12615 	rc = copyin(br->data, br_data, br->len);
12616 	if (rc == 0)
12617 		rc = -t4_load_boot(sc, br_data, offset, br->len);
12618 
12619 	free(br_data, M_CXGBE);
12620 done:
12621 	end_synchronized_op(sc, 0);
12622 	return (rc);
12623 }
12624 
12625 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)12626 load_bootcfg(struct adapter *sc, struct t4_data *bc)
12627 {
12628 	int rc;
12629 	uint8_t *bc_data = NULL;
12630 
12631 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
12632 	if (rc)
12633 		return (rc);
12634 
12635 	if (hw_off_limits(sc)) {
12636 		rc = ENXIO;
12637 		goto done;
12638 	}
12639 
12640 	if (bc->len == 0) {
12641 		/* clear */
12642 		rc = -t4_load_bootcfg(sc, NULL, 0);
12643 		goto done;
12644 	}
12645 
12646 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
12647 
12648 	rc = copyin(bc->data, bc_data, bc->len);
12649 	if (rc == 0)
12650 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
12651 
12652 	free(bc_data, M_CXGBE);
12653 done:
12654 	end_synchronized_op(sc, 0);
12655 	return (rc);
12656 }
12657 
12658 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)12659 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
12660 {
12661 	int rc;
12662 	struct cudbg_init *cudbg;
12663 	void *handle, *buf;
12664 
12665 	/* buf is large, don't block if no memory is available */
12666 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
12667 	if (buf == NULL)
12668 		return (ENOMEM);
12669 
12670 	handle = cudbg_alloc_handle();
12671 	if (handle == NULL) {
12672 		rc = ENOMEM;
12673 		goto done;
12674 	}
12675 
12676 	cudbg = cudbg_get_init(handle);
12677 	cudbg->adap = sc;
12678 	cudbg->print = (cudbg_print_cb)printf;
12679 
12680 #ifndef notyet
12681 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
12682 	    __func__, dump->wr_flash, dump->len, dump->data);
12683 #endif
12684 
12685 	if (dump->wr_flash)
12686 		cudbg->use_flash = 1;
12687 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
12688 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
12689 
12690 	rc = cudbg_collect(handle, buf, &dump->len);
12691 	if (rc != 0)
12692 		goto done;
12693 
12694 	rc = copyout(buf, dump->data, dump->len);
12695 done:
12696 	cudbg_free_handle(handle);
12697 	free(buf, M_CXGBE);
12698 	return (rc);
12699 }
12700 
12701 static void
free_offload_policy(struct t4_offload_policy * op)12702 free_offload_policy(struct t4_offload_policy *op)
12703 {
12704 	struct offload_rule *r;
12705 	int i;
12706 
12707 	if (op == NULL)
12708 		return;
12709 
12710 	r = &op->rule[0];
12711 	for (i = 0; i < op->nrules; i++, r++) {
12712 		free(r->bpf_prog.bf_insns, M_CXGBE);
12713 	}
12714 	free(op->rule, M_CXGBE);
12715 	free(op, M_CXGBE);
12716 }
12717 
12718 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)12719 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
12720 {
12721 	int i, rc, len;
12722 	struct t4_offload_policy *op, *old;
12723 	struct bpf_program *bf;
12724 	const struct offload_settings *s;
12725 	struct offload_rule *r;
12726 	void *u;
12727 
12728 	if (!is_offload(sc))
12729 		return (ENODEV);
12730 
12731 	if (uop->nrules == 0) {
12732 		/* Delete installed policies. */
12733 		op = NULL;
12734 		goto set_policy;
12735 	} else if (uop->nrules > 256) { /* arbitrary */
12736 		return (E2BIG);
12737 	}
12738 
12739 	/* Copy userspace offload policy to kernel */
12740 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
12741 	op->nrules = uop->nrules;
12742 	len = op->nrules * sizeof(struct offload_rule);
12743 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
12744 	rc = copyin(uop->rule, op->rule, len);
12745 	if (rc) {
12746 		free(op->rule, M_CXGBE);
12747 		free(op, M_CXGBE);
12748 		return (rc);
12749 	}
12750 
12751 	r = &op->rule[0];
12752 	for (i = 0; i < op->nrules; i++, r++) {
12753 
12754 		/* Validate open_type */
12755 		if (r->open_type != OPEN_TYPE_LISTEN &&
12756 		    r->open_type != OPEN_TYPE_ACTIVE &&
12757 		    r->open_type != OPEN_TYPE_PASSIVE &&
12758 		    r->open_type != OPEN_TYPE_DONTCARE) {
12759 error:
12760 			/*
12761 			 * Rules 0 to i have malloc'd filters that need to be
12762 			 * freed.  Rules i+1 to nrules have userspace pointers
12763 			 * and should be left alone.
12764 			 */
12765 			op->nrules = i;
12766 			free_offload_policy(op);
12767 			return (rc);
12768 		}
12769 
12770 		/* Validate settings */
12771 		s = &r->settings;
12772 		if ((s->offload != 0 && s->offload != 1) ||
12773 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
12774 		    s->sched_class < -1 ||
12775 		    s->sched_class >= sc->params.nsched_cls) {
12776 			rc = EINVAL;
12777 			goto error;
12778 		}
12779 
12780 		bf = &r->bpf_prog;
12781 		u = bf->bf_insns;	/* userspace ptr */
12782 		bf->bf_insns = NULL;
12783 		if (bf->bf_len == 0) {
12784 			/* legal, matches everything */
12785 			continue;
12786 		}
12787 		len = bf->bf_len * sizeof(*bf->bf_insns);
12788 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
12789 		rc = copyin(u, bf->bf_insns, len);
12790 		if (rc != 0)
12791 			goto error;
12792 
12793 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
12794 			rc = EINVAL;
12795 			goto error;
12796 		}
12797 	}
12798 set_policy:
12799 	rw_wlock(&sc->policy_lock);
12800 	old = sc->policy;
12801 	sc->policy = op;
12802 	rw_wunlock(&sc->policy_lock);
12803 	free_offload_policy(old);
12804 
12805 	return (0);
12806 }
12807 
12808 #define MAX_READ_BUF_SIZE (128 * 1024)
12809 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)12810 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
12811 {
12812 	uint32_t addr, remaining, n;
12813 	uint32_t *buf;
12814 	int rc;
12815 	uint8_t *dst;
12816 
12817 	mtx_lock(&sc->reg_lock);
12818 	if (hw_off_limits(sc))
12819 		rc = ENXIO;
12820 	else
12821 		rc = validate_mem_range(sc, mr->addr, mr->len);
12822 	mtx_unlock(&sc->reg_lock);
12823 	if (rc != 0)
12824 		return (rc);
12825 
12826 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
12827 	addr = mr->addr;
12828 	remaining = mr->len;
12829 	dst = (void *)mr->data;
12830 
12831 	while (remaining) {
12832 		n = min(remaining, MAX_READ_BUF_SIZE);
12833 		mtx_lock(&sc->reg_lock);
12834 		if (hw_off_limits(sc))
12835 			rc = ENXIO;
12836 		else
12837 			read_via_memwin(sc, 2, addr, buf, n);
12838 		mtx_unlock(&sc->reg_lock);
12839 		if (rc != 0)
12840 			break;
12841 
12842 		rc = copyout(buf, dst, n);
12843 		if (rc != 0)
12844 			break;
12845 
12846 		dst += n;
12847 		remaining -= n;
12848 		addr += n;
12849 	}
12850 
12851 	free(buf, M_CXGBE);
12852 	return (rc);
12853 }
12854 #undef MAX_READ_BUF_SIZE
12855 
12856 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)12857 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
12858 {
12859 	int rc;
12860 
12861 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
12862 		return (EINVAL);
12863 
12864 	if (i2cd->len > sizeof(i2cd->data))
12865 		return (EFBIG);
12866 
12867 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
12868 	if (rc)
12869 		return (rc);
12870 	if (hw_off_limits(sc))
12871 		rc = ENXIO;
12872 	else
12873 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
12874 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
12875 	end_synchronized_op(sc, 0);
12876 
12877 	return (rc);
12878 }
12879 
12880 static int
clear_stats(struct adapter * sc,u_int port_id)12881 clear_stats(struct adapter *sc, u_int port_id)
12882 {
12883 	int i, v, chan_map;
12884 	struct port_info *pi;
12885 	struct vi_info *vi;
12886 	struct sge_rxq *rxq;
12887 	struct sge_txq *txq;
12888 	struct sge_wrq *wrq;
12889 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12890 	struct sge_ofld_txq *ofld_txq;
12891 #endif
12892 #ifdef TCP_OFFLOAD
12893 	struct sge_ofld_rxq *ofld_rxq;
12894 #endif
12895 
12896 	if (port_id >= sc->params.nports)
12897 		return (EINVAL);
12898 	pi = sc->port[port_id];
12899 	if (pi == NULL)
12900 		return (EIO);
12901 
12902 	mtx_lock(&sc->reg_lock);
12903 	if (!hw_off_limits(sc)) {
12904 		/* MAC stats */
12905 		t4_clr_port_stats(sc, pi->hw_port);
12906 		if (is_t6(sc)) {
12907 			if (pi->fcs_reg != -1)
12908 				pi->fcs_base = t4_read_reg64(sc,
12909 				    t4_port_reg(sc, pi->tx_chan, pi->fcs_reg));
12910 			else
12911 				pi->stats.rx_fcs_err = 0;
12912 		}
12913 		for_each_vi(pi, v, vi) {
12914 			if (vi->flags & VI_INIT_DONE)
12915 				t4_clr_vi_stats(sc, vi->vin);
12916 		}
12917 		chan_map = pi->rx_e_chan_map;
12918 		v = 0;	/* reuse */
12919 		while (chan_map) {
12920 			i = ffs(chan_map) - 1;
12921 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
12922 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
12923 			chan_map &= ~(1 << i);
12924 		}
12925 	}
12926 	mtx_unlock(&sc->reg_lock);
12927 	pi->tx_parse_error = 0;
12928 	pi->tnl_cong_drops = 0;
12929 
12930 	/*
12931 	 * Since this command accepts a port, clear stats for
12932 	 * all VIs on this port.
12933 	 */
12934 	for_each_vi(pi, v, vi) {
12935 		if (vi->flags & VI_INIT_DONE) {
12936 
12937 			for_each_rxq(vi, i, rxq) {
12938 #if defined(INET) || defined(INET6)
12939 				rxq->lro.lro_queued = 0;
12940 				rxq->lro.lro_flushed = 0;
12941 #endif
12942 				rxq->rxcsum = 0;
12943 				rxq->vlan_extraction = 0;
12944 				rxq->vxlan_rxcsum = 0;
12945 
12946 				rxq->fl.cl_allocated = 0;
12947 				rxq->fl.cl_recycled = 0;
12948 				rxq->fl.cl_fast_recycled = 0;
12949 			}
12950 
12951 			for_each_txq(vi, i, txq) {
12952 				txq->txcsum = 0;
12953 				txq->tso_wrs = 0;
12954 				txq->vlan_insertion = 0;
12955 				txq->imm_wrs = 0;
12956 				txq->sgl_wrs = 0;
12957 				txq->txpkt_wrs = 0;
12958 				txq->txpkts0_wrs = 0;
12959 				txq->txpkts1_wrs = 0;
12960 				txq->txpkts0_pkts = 0;
12961 				txq->txpkts1_pkts = 0;
12962 				txq->txpkts_flush = 0;
12963 				txq->raw_wrs = 0;
12964 				txq->vxlan_tso_wrs = 0;
12965 				txq->vxlan_txcsum = 0;
12966 				txq->kern_tls_records = 0;
12967 				txq->kern_tls_short = 0;
12968 				txq->kern_tls_partial = 0;
12969 				txq->kern_tls_full = 0;
12970 				txq->kern_tls_octets = 0;
12971 				txq->kern_tls_waste = 0;
12972 				txq->kern_tls_header = 0;
12973 				txq->kern_tls_fin_short = 0;
12974 				txq->kern_tls_cbc = 0;
12975 				txq->kern_tls_gcm = 0;
12976 				if (is_t6(sc)) {
12977 					txq->kern_tls_options = 0;
12978 					txq->kern_tls_fin = 0;
12979 				} else {
12980 					txq->kern_tls_ghash_received = 0;
12981 					txq->kern_tls_ghash_requested = 0;
12982 					txq->kern_tls_lso = 0;
12983 					txq->kern_tls_partial_ghash = 0;
12984 					txq->kern_tls_splitmode = 0;
12985 					txq->kern_tls_trailer = 0;
12986 				}
12987 				mp_ring_reset_stats(txq->r);
12988 			}
12989 
12990 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12991 			for_each_ofld_txq(vi, i, ofld_txq) {
12992 				ofld_txq->wrq.tx_wrs_direct = 0;
12993 				ofld_txq->wrq.tx_wrs_copied = 0;
12994 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12995 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12996 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12997 				counter_u64_zero(ofld_txq->tx_nvme_pdus);
12998 				counter_u64_zero(ofld_txq->tx_nvme_octets);
12999 				counter_u64_zero(ofld_txq->tx_nvme_iso_wrs);
13000 				counter_u64_zero(ofld_txq->tx_aio_jobs);
13001 				counter_u64_zero(ofld_txq->tx_aio_octets);
13002 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
13003 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
13004 			}
13005 #endif
13006 #ifdef TCP_OFFLOAD
13007 			for_each_ofld_rxq(vi, i, ofld_rxq) {
13008 				ofld_rxq->fl.cl_allocated = 0;
13009 				ofld_rxq->fl.cl_recycled = 0;
13010 				ofld_rxq->fl.cl_fast_recycled = 0;
13011 				counter_u64_zero(
13012 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
13013 				counter_u64_zero(
13014 				    ofld_rxq->rx_iscsi_ddp_setup_error);
13015 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
13016 				ofld_rxq->rx_iscsi_ddp_octets = 0;
13017 				ofld_rxq->rx_iscsi_fl_pdus = 0;
13018 				ofld_rxq->rx_iscsi_fl_octets = 0;
13019 				counter_u64_zero(
13020 				    ofld_rxq->rx_nvme_ddp_setup_ok);
13021 				counter_u64_zero(
13022 				    ofld_rxq->rx_nvme_ddp_setup_no_stag);
13023 				counter_u64_zero(
13024 				    ofld_rxq->rx_nvme_ddp_setup_error);
13025 				counter_u64_zero(ofld_rxq->rx_nvme_ddp_pdus);
13026 				counter_u64_zero(ofld_rxq->rx_nvme_ddp_octets);
13027 				counter_u64_zero(ofld_rxq->rx_nvme_fl_pdus);
13028 				counter_u64_zero(ofld_rxq->rx_nvme_fl_octets);
13029 				counter_u64_zero(
13030 				    ofld_rxq->rx_nvme_invalid_headers);
13031 				counter_u64_zero(
13032 				    ofld_rxq->rx_nvme_header_digest_errors);
13033 				counter_u64_zero(
13034 				    ofld_rxq->rx_nvme_data_digest_errors);
13035 				ofld_rxq->rx_aio_ddp_jobs = 0;
13036 				ofld_rxq->rx_aio_ddp_octets = 0;
13037 				ofld_rxq->rx_toe_tls_records = 0;
13038 				ofld_rxq->rx_toe_tls_octets = 0;
13039 				ofld_rxq->rx_toe_ddp_octets = 0;
13040 				counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
13041 				counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
13042 				counter_u64_zero(ofld_rxq->ddp_buffer_free);
13043 			}
13044 #endif
13045 
13046 			if (IS_MAIN_VI(vi)) {
13047 				wrq = &sc->sge.ctrlq[pi->port_id];
13048 				wrq->tx_wrs_direct = 0;
13049 				wrq->tx_wrs_copied = 0;
13050 			}
13051 		}
13052 	}
13053 
13054 	return (0);
13055 }
13056 
13057 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)13058 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
13059 {
13060 #ifdef INET6
13061 	struct in6_addr in6;
13062 
13063 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
13064 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
13065 		return (0);
13066 	else
13067 		return (EIO);
13068 #else
13069 	return (ENOTSUP);
13070 #endif
13071 }
13072 
13073 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)13074 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
13075 {
13076 #ifdef INET6
13077 	struct in6_addr in6;
13078 
13079 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
13080 	return (t4_release_clip_addr(sc, &in6));
13081 #else
13082 	return (ENOTSUP);
13083 #endif
13084 }
13085 
13086 int
t4_os_find_pci_capability(struct adapter * sc,int cap)13087 t4_os_find_pci_capability(struct adapter *sc, int cap)
13088 {
13089 	int i;
13090 
13091 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
13092 }
13093 
13094 void
t4_os_portmod_changed(struct port_info * pi)13095 t4_os_portmod_changed(struct port_info *pi)
13096 {
13097 	struct adapter *sc = pi->adapter;
13098 	struct vi_info *vi;
13099 	if_t ifp;
13100 	static const char *mod_str[] = {
13101 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM",
13102 		"LR_SIMPLEX", "DR"
13103 	};
13104 
13105 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
13106 	    ("%s: port_type %u", __func__, pi->port_type));
13107 
13108 	vi = &pi->vi[0];
13109 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
13110 		PORT_LOCK(pi);
13111 		build_medialist(pi);
13112 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
13113 			fixup_link_config(pi);
13114 			apply_link_config(pi);
13115 		}
13116 		PORT_UNLOCK(pi);
13117 		end_synchronized_op(sc, LOCK_HELD);
13118 	}
13119 
13120 	ifp = vi->ifp;
13121 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
13122 		if_printf(ifp, "transceiver unplugged.\n");
13123 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
13124 		if_printf(ifp, "unknown transceiver inserted.\n");
13125 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
13126 		if_printf(ifp, "unsupported transceiver inserted.\n");
13127 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
13128 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
13129 		    port_top_speed(pi), mod_str[pi->mod_type]);
13130 	} else {
13131 		if_printf(ifp, "transceiver (type %d) inserted.\n",
13132 		    pi->mod_type);
13133 	}
13134 }
13135 
13136 void
t4_os_link_changed(struct port_info * pi)13137 t4_os_link_changed(struct port_info *pi)
13138 {
13139 	struct vi_info *vi;
13140 	if_t ifp;
13141 	struct link_config *lc = &pi->link_cfg;
13142 	struct adapter *sc = pi->adapter;
13143 	int v;
13144 
13145 	PORT_LOCK_ASSERT_OWNED(pi);
13146 
13147 	if (is_t6(sc)) {
13148 		if (lc->link_ok) {
13149 			if (lc->speed > 25000 ||
13150 			    (lc->speed == 25000 && lc->fec == FEC_RS))
13151 				pi->fcs_reg = A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS;
13152 			else
13153 				pi->fcs_reg = A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS;
13154 			pi->fcs_base = t4_read_reg64(sc,
13155 			    t4_port_reg(sc, pi->tx_chan, pi->fcs_reg));
13156 			pi->stats.rx_fcs_err = 0;
13157 		} else {
13158 			pi->fcs_reg = -1;
13159 		}
13160 	} else {
13161 		MPASS(pi->fcs_reg != -1);
13162 		MPASS(pi->fcs_base == 0);
13163 	}
13164 
13165 	for_each_vi(pi, v, vi) {
13166 		ifp = vi->ifp;
13167 		if (ifp == NULL || IS_DETACHING(vi))
13168 			continue;
13169 
13170 		if (lc->link_ok) {
13171 			if_setbaudrate(ifp, IF_Mbps(lc->speed));
13172 			if_link_state_change(ifp, LINK_STATE_UP);
13173 		} else {
13174 			if_link_state_change(ifp, LINK_STATE_DOWN);
13175 		}
13176 	}
13177 }
13178 
13179 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)13180 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
13181 {
13182 	struct adapter *sc;
13183 
13184 	sx_slock(&t4_list_lock);
13185 	SLIST_FOREACH(sc, &t4_list, link) {
13186 		/*
13187 		 * func should not make any assumptions about what state sc is
13188 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
13189 		 */
13190 		func(sc, arg);
13191 	}
13192 	sx_sunlock(&t4_list_lock);
13193 }
13194 
13195 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)13196 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
13197     struct thread *td)
13198 {
13199 	int rc;
13200 	struct adapter *sc = dev->si_drv1;
13201 
13202 	rc = priv_check(td, PRIV_DRIVER);
13203 	if (rc != 0)
13204 		return (rc);
13205 
13206 	switch (cmd) {
13207 	case CHELSIO_T4_GETREG: {
13208 		struct t4_reg *edata = (struct t4_reg *)data;
13209 
13210 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
13211 			return (EFAULT);
13212 
13213 		mtx_lock(&sc->reg_lock);
13214 		if (hw_off_limits(sc))
13215 			rc = ENXIO;
13216 		else if (edata->size == 4)
13217 			edata->val = t4_read_reg(sc, edata->addr);
13218 		else if (edata->size == 8)
13219 			edata->val = t4_read_reg64(sc, edata->addr);
13220 		else
13221 			rc = EINVAL;
13222 		mtx_unlock(&sc->reg_lock);
13223 
13224 		break;
13225 	}
13226 	case CHELSIO_T4_SETREG: {
13227 		struct t4_reg *edata = (struct t4_reg *)data;
13228 
13229 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
13230 			return (EFAULT);
13231 
13232 		mtx_lock(&sc->reg_lock);
13233 		if (hw_off_limits(sc))
13234 			rc = ENXIO;
13235 		else if (edata->size == 4) {
13236 			if (edata->val & 0xffffffff00000000)
13237 				rc = EINVAL;
13238 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
13239 		} else if (edata->size == 8)
13240 			t4_write_reg64(sc, edata->addr, edata->val);
13241 		else
13242 			rc = EINVAL;
13243 		mtx_unlock(&sc->reg_lock);
13244 
13245 		break;
13246 	}
13247 	case CHELSIO_T4_REGDUMP: {
13248 		struct t4_regdump *regs = (struct t4_regdump *)data;
13249 		int reglen = t4_get_regs_len(sc);
13250 		uint8_t *buf;
13251 
13252 		if (regs->len < reglen) {
13253 			regs->len = reglen; /* hint to the caller */
13254 			return (ENOBUFS);
13255 		}
13256 
13257 		regs->len = reglen;
13258 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
13259 		mtx_lock(&sc->reg_lock);
13260 		if (hw_off_limits(sc))
13261 			rc = ENXIO;
13262 		else
13263 			get_regs(sc, regs, buf);
13264 		mtx_unlock(&sc->reg_lock);
13265 		if (rc == 0)
13266 			rc = copyout(buf, regs->data, reglen);
13267 		free(buf, M_CXGBE);
13268 		break;
13269 	}
13270 	case CHELSIO_T4_GET_FILTER_MODE:
13271 		rc = get_filter_mode(sc, (uint32_t *)data);
13272 		break;
13273 	case CHELSIO_T4_SET_FILTER_MODE:
13274 		rc = set_filter_mode(sc, *(uint32_t *)data);
13275 		break;
13276 	case CHELSIO_T4_SET_FILTER_MASK:
13277 		rc = set_filter_mask(sc, *(uint32_t *)data);
13278 		break;
13279 	case CHELSIO_T4_GET_FILTER:
13280 		rc = get_filter(sc, (struct t4_filter *)data);
13281 		break;
13282 	case CHELSIO_T4_SET_FILTER:
13283 		rc = set_filter(sc, (struct t4_filter *)data);
13284 		break;
13285 	case CHELSIO_T4_DEL_FILTER:
13286 		rc = del_filter(sc, (struct t4_filter *)data);
13287 		break;
13288 	case CHELSIO_T4_GET_SGE_CONTEXT: {
13289 		struct t4_sge_context *ctxt = (struct t4_sge_context *)data;
13290 
13291 		rc = get_sge_context(sc, ctxt->mem_id, ctxt->cid,
13292 		    sizeof(ctxt->data), &ctxt->data[0]);
13293 		break;
13294 	}
13295 	case CHELSIO_T4_LOAD_FW:
13296 		rc = load_fw(sc, (struct t4_data *)data);
13297 		break;
13298 	case CHELSIO_T4_GET_MEM:
13299 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
13300 		break;
13301 	case CHELSIO_T4_GET_I2C:
13302 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
13303 		break;
13304 	case CHELSIO_T4_CLEAR_STATS:
13305 		rc = clear_stats(sc, *(uint32_t *)data);
13306 		break;
13307 	case CHELSIO_T4_SCHED_CLASS:
13308 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
13309 		break;
13310 	case CHELSIO_T4_SCHED_QUEUE:
13311 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
13312 		break;
13313 	case CHELSIO_T4_GET_TRACER:
13314 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
13315 		break;
13316 	case CHELSIO_T4_SET_TRACER:
13317 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
13318 		break;
13319 	case CHELSIO_T4_LOAD_CFG:
13320 		rc = load_cfg(sc, (struct t4_data *)data);
13321 		break;
13322 	case CHELSIO_T4_LOAD_BOOT:
13323 		rc = load_boot(sc, (struct t4_bootrom *)data);
13324 		break;
13325 	case CHELSIO_T4_LOAD_BOOTCFG:
13326 		rc = load_bootcfg(sc, (struct t4_data *)data);
13327 		break;
13328 	case CHELSIO_T4_CUDBG_DUMP:
13329 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
13330 		break;
13331 	case CHELSIO_T4_SET_OFLD_POLICY:
13332 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
13333 		break;
13334 	case CHELSIO_T4_HOLD_CLIP_ADDR:
13335 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
13336 		break;
13337 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
13338 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
13339 		break;
13340 	case CHELSIO_T4_GET_SGE_CTXT: {
13341 		struct t4_sge_ctxt *ctxt = (struct t4_sge_ctxt *)data;
13342 
13343 		rc = get_sge_context(sc, ctxt->mem_id, ctxt->cid,
13344 		    sizeof(ctxt->data), &ctxt->data[0]);
13345 		break;
13346 	}
13347 	default:
13348 		rc = ENOTTY;
13349 	}
13350 
13351 	return (rc);
13352 }
13353 
13354 #ifdef TCP_OFFLOAD
13355 int
toe_capability(struct vi_info * vi,bool enable)13356 toe_capability(struct vi_info *vi, bool enable)
13357 {
13358 	int rc;
13359 	struct port_info *pi = vi->pi;
13360 	struct adapter *sc = pi->adapter;
13361 
13362 	ASSERT_SYNCHRONIZED_OP(sc);
13363 
13364 	if (!is_offload(sc))
13365 		return (ENODEV);
13366 	if (!hw_all_ok(sc))
13367 		return (ENXIO);
13368 
13369 	if (enable) {
13370 #ifdef KERN_TLS
13371 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
13372 			int i, j, n;
13373 			struct port_info *p;
13374 			struct vi_info *v;
13375 
13376 			/*
13377 			 * Reconfigure hardware for TOE if TXTLS is not enabled
13378 			 * on any ifnet.
13379 			 */
13380 			n = 0;
13381 			for_each_port(sc, i) {
13382 				p = sc->port[i];
13383 				for_each_vi(p, j, v) {
13384 					if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
13385 						CH_WARN(sc,
13386 						    "%s has NIC TLS enabled.\n",
13387 						    device_get_nameunit(v->dev));
13388 						n++;
13389 					}
13390 				}
13391 			}
13392 			if (n > 0) {
13393 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
13394 				    "associated with this adapter before "
13395 				    "trying to enable TOE.\n");
13396 				return (EAGAIN);
13397 			}
13398 			rc = t6_config_kern_tls(sc, false);
13399 			if (rc)
13400 				return (rc);
13401 		}
13402 #endif
13403 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
13404 			/* TOE is already enabled. */
13405 			return (0);
13406 		}
13407 
13408 		/*
13409 		 * We need the port's queues around so that we're able to send
13410 		 * and receive CPLs to/from the TOE even if the ifnet for this
13411 		 * port has never been UP'd administratively.
13412 		 */
13413 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
13414 			return (rc);
13415 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
13416 		    ((rc = vi_init(&pi->vi[0])) != 0))
13417 			return (rc);
13418 
13419 		if (isset(&sc->offload_map, pi->port_id)) {
13420 			/* TOE is enabled on another VI of this port. */
13421 			MPASS(pi->uld_vis > 0);
13422 			pi->uld_vis++;
13423 			return (0);
13424 		}
13425 
13426 		if (!uld_active(sc, ULD_TOM)) {
13427 			rc = t4_activate_uld(sc, ULD_TOM);
13428 			if (rc == EAGAIN) {
13429 				log(LOG_WARNING,
13430 				    "You must kldload t4_tom.ko before trying "
13431 				    "to enable TOE on a cxgbe interface.\n");
13432 			}
13433 			if (rc != 0)
13434 				return (rc);
13435 			KASSERT(sc->tom_softc != NULL,
13436 			    ("%s: TOM activated but softc NULL", __func__));
13437 			KASSERT(uld_active(sc, ULD_TOM),
13438 			    ("%s: TOM activated but flag not set", __func__));
13439 		}
13440 
13441 		/*
13442 		 * Activate iWARP, iSCSI, and NVMe too, if the modules
13443 		 * are loaded.
13444 		 */
13445 		if (!uld_active(sc, ULD_IWARP))
13446 			(void) t4_activate_uld(sc, ULD_IWARP);
13447 		if (!uld_active(sc, ULD_ISCSI))
13448 			(void) t4_activate_uld(sc, ULD_ISCSI);
13449 		if (!uld_active(sc, ULD_NVME))
13450 			(void) t4_activate_uld(sc, ULD_NVME);
13451 
13452 		if (pi->uld_vis++ == 0)
13453 			setbit(&sc->offload_map, pi->port_id);
13454 	} else {
13455 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) == 0) {
13456 			/* TOE is already disabled. */
13457 			return (0);
13458 		}
13459 		MPASS(isset(&sc->offload_map, pi->port_id));
13460 		MPASS(pi->uld_vis > 0);
13461 		if (--pi->uld_vis == 0)
13462 			clrbit(&sc->offload_map, pi->port_id);
13463 	}
13464 
13465 	return (0);
13466 }
13467 
13468 /*
13469  * Add an upper layer driver to the global list.
13470  */
13471 int
t4_register_uld(struct uld_info * ui,int id)13472 t4_register_uld(struct uld_info *ui, int id)
13473 {
13474 	int rc;
13475 
13476 	if (id < 0 || id > ULD_MAX)
13477 		return (EINVAL);
13478 	sx_xlock(&t4_uld_list_lock);
13479 	if (t4_uld_list[id] != NULL)
13480 		rc = EEXIST;
13481 	else {
13482 		t4_uld_list[id] = ui;
13483 		rc = 0;
13484 	}
13485 	sx_xunlock(&t4_uld_list_lock);
13486 	return (rc);
13487 }
13488 
13489 int
t4_unregister_uld(struct uld_info * ui,int id)13490 t4_unregister_uld(struct uld_info *ui, int id)
13491 {
13492 
13493 	if (id < 0 || id > ULD_MAX)
13494 		return (EINVAL);
13495 	sx_xlock(&t4_uld_list_lock);
13496 	MPASS(t4_uld_list[id] == ui);
13497 	t4_uld_list[id] = NULL;
13498 	sx_xunlock(&t4_uld_list_lock);
13499 	return (0);
13500 }
13501 
13502 int
t4_activate_uld(struct adapter * sc,int id)13503 t4_activate_uld(struct adapter *sc, int id)
13504 {
13505 	int rc;
13506 
13507 	ASSERT_SYNCHRONIZED_OP(sc);
13508 
13509 	if (id < 0 || id > ULD_MAX)
13510 		return (EINVAL);
13511 
13512 	/* Adapter needs to be initialized before any ULD can be activated. */
13513 	if (!(sc->flags & FULL_INIT_DONE)) {
13514 		rc = adapter_init(sc);
13515 		if (rc != 0)
13516 			return (rc);
13517 	}
13518 
13519 	sx_slock(&t4_uld_list_lock);
13520 	if (t4_uld_list[id] == NULL)
13521 		rc = EAGAIN;	/* load the KLD with this ULD and try again. */
13522 	else {
13523 		rc = t4_uld_list[id]->uld_activate(sc);
13524 		if (rc == 0)
13525 			setbit(&sc->active_ulds, id);
13526 	}
13527 	sx_sunlock(&t4_uld_list_lock);
13528 
13529 	return (rc);
13530 }
13531 
13532 int
t4_deactivate_uld(struct adapter * sc,int id)13533 t4_deactivate_uld(struct adapter *sc, int id)
13534 {
13535 	int rc;
13536 
13537 	ASSERT_SYNCHRONIZED_OP(sc);
13538 
13539 	if (id < 0 || id > ULD_MAX)
13540 		return (EINVAL);
13541 
13542 	sx_slock(&t4_uld_list_lock);
13543 	if (t4_uld_list[id] == NULL)
13544 		rc = ENXIO;
13545 	else {
13546 		rc = t4_uld_list[id]->uld_deactivate(sc);
13547 		if (rc == 0)
13548 			clrbit(&sc->active_ulds, id);
13549 	}
13550 	sx_sunlock(&t4_uld_list_lock);
13551 
13552 	return (rc);
13553 }
13554 
13555 static int
deactivate_all_uld(struct adapter * sc)13556 deactivate_all_uld(struct adapter *sc)
13557 {
13558 	int i, rc;
13559 
13560 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
13561 	if (rc != 0)
13562 		return (ENXIO);
13563 	sx_slock(&t4_uld_list_lock);
13564 	for (i = 0; i <= ULD_MAX; i++) {
13565 		if (t4_uld_list[i] == NULL || !uld_active(sc, i))
13566 			continue;
13567 		rc = t4_uld_list[i]->uld_deactivate(sc);
13568 		if (rc != 0)
13569 			break;
13570 		clrbit(&sc->active_ulds, i);
13571 	}
13572 	sx_sunlock(&t4_uld_list_lock);
13573 	end_synchronized_op(sc, 0);
13574 
13575 	return (rc);
13576 }
13577 
13578 static void
stop_all_uld(struct adapter * sc)13579 stop_all_uld(struct adapter *sc)
13580 {
13581 	int i;
13582 
13583 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
13584 		return;
13585 	sx_slock(&t4_uld_list_lock);
13586 	for (i = 0; i <= ULD_MAX; i++) {
13587 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
13588 		    t4_uld_list[i]->uld_stop == NULL)
13589 			continue;
13590 		(void) t4_uld_list[i]->uld_stop(sc);
13591 	}
13592 	sx_sunlock(&t4_uld_list_lock);
13593 	end_synchronized_op(sc, 0);
13594 }
13595 
13596 static void
restart_all_uld(struct adapter * sc)13597 restart_all_uld(struct adapter *sc)
13598 {
13599 	int i;
13600 
13601 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
13602 		return;
13603 	sx_slock(&t4_uld_list_lock);
13604 	for (i = 0; i <= ULD_MAX; i++) {
13605 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
13606 		    t4_uld_list[i]->uld_restart == NULL)
13607 			continue;
13608 		(void) t4_uld_list[i]->uld_restart(sc);
13609 	}
13610 	sx_sunlock(&t4_uld_list_lock);
13611 	end_synchronized_op(sc, 0);
13612 }
13613 
13614 int
uld_active(struct adapter * sc,int id)13615 uld_active(struct adapter *sc, int id)
13616 {
13617 
13618 	MPASS(id >= 0 && id <= ULD_MAX);
13619 
13620 	return (isset(&sc->active_ulds, id));
13621 }
13622 #endif
13623 
13624 #ifdef KERN_TLS
13625 static int
ktls_capability(struct adapter * sc,bool enable)13626 ktls_capability(struct adapter *sc, bool enable)
13627 {
13628 	ASSERT_SYNCHRONIZED_OP(sc);
13629 
13630 	if (!is_ktls(sc))
13631 		return (ENODEV);
13632 	if (!is_t6(sc))
13633 		return (0);
13634 	if (!hw_all_ok(sc))
13635 		return (ENXIO);
13636 
13637 	if (enable) {
13638 		if (sc->flags & KERN_TLS_ON)
13639 			return (0);	/* already on */
13640 		if (sc->offload_map != 0) {
13641 			CH_WARN(sc,
13642 			    "Disable TOE on all interfaces associated with "
13643 			    "this adapter before trying to enable NIC TLS.\n");
13644 			return (EAGAIN);
13645 		}
13646 		return (t6_config_kern_tls(sc, true));
13647 	} else {
13648 		/*
13649 		 * Nothing to do for disable.  If TOE is enabled sometime later
13650 		 * then toe_capability will reconfigure the hardware.
13651 		 */
13652 		return (0);
13653 	}
13654 }
13655 #endif
13656 
13657 /*
13658  * t  = ptr to tunable.
13659  * nc = number of CPUs.
13660  * c  = compiled in default for that tunable.
13661  */
13662 static void
calculate_nqueues(int * t,int nc,const int c)13663 calculate_nqueues(int *t, int nc, const int c)
13664 {
13665 	int nq;
13666 
13667 	if (*t > 0)
13668 		return;
13669 	nq = *t < 0 ? -*t : c;
13670 	*t = min(nc, nq);
13671 }
13672 
13673 /*
13674  * Come up with reasonable defaults for some of the tunables, provided they're
13675  * not set by the user (in which case we'll use the values as is).
13676  */
13677 static void
tweak_tunables(void)13678 tweak_tunables(void)
13679 {
13680 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
13681 
13682 	if (t4_ntxq < 1) {
13683 #ifdef RSS
13684 		t4_ntxq = rss_getnumbuckets();
13685 #else
13686 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
13687 #endif
13688 	}
13689 
13690 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
13691 
13692 	if (t4_nrxq < 1) {
13693 #ifdef RSS
13694 		t4_nrxq = rss_getnumbuckets();
13695 #else
13696 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
13697 #endif
13698 	}
13699 
13700 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
13701 
13702 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
13703 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
13704 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
13705 #endif
13706 #ifdef TCP_OFFLOAD
13707 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
13708 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
13709 #endif
13710 
13711 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
13712 	if (t4_toecaps_allowed == -1)
13713 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
13714 #else
13715 	if (t4_toecaps_allowed == -1)
13716 		t4_toecaps_allowed = 0;
13717 #endif
13718 
13719 #ifdef TCP_OFFLOAD
13720 	if (t4_rdmacaps_allowed == -1) {
13721 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
13722 		    FW_CAPS_CONFIG_RDMA_RDMAC;
13723 	}
13724 
13725 	if (t4_iscsicaps_allowed == -1) {
13726 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
13727 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
13728 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
13729 	}
13730 
13731 	if (t4_nvmecaps_allowed == -1)
13732 		t4_nvmecaps_allowed = FW_CAPS_CONFIG_NVME_TCP;
13733 
13734 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
13735 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
13736 
13737 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
13738 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
13739 #else
13740 	if (t4_rdmacaps_allowed == -1)
13741 		t4_rdmacaps_allowed = 0;
13742 
13743 	if (t4_iscsicaps_allowed == -1)
13744 		t4_iscsicaps_allowed = 0;
13745 
13746 	if (t4_nvmecaps_allowed == -1)
13747 		t4_nvmecaps_allowed = 0;
13748 #endif
13749 
13750 #ifdef DEV_NETMAP
13751 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
13752 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
13753 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
13754 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
13755 #endif
13756 
13757 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
13758 		t4_tmr_idx = TMR_IDX;
13759 
13760 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
13761 		t4_pktc_idx = PKTC_IDX;
13762 
13763 	if (t4_qsize_txq < 128)
13764 		t4_qsize_txq = 128;
13765 
13766 	if (t4_qsize_rxq < 128)
13767 		t4_qsize_rxq = 128;
13768 	while (t4_qsize_rxq & 7)
13769 		t4_qsize_rxq++;
13770 
13771 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
13772 
13773 	/*
13774 	 * Number of VIs to create per-port.  The first VI is the "main" regular
13775 	 * VI for the port.  The rest are additional virtual interfaces on the
13776 	 * same physical port.  Note that the main VI does not have native
13777 	 * netmap support but the extra VIs do.
13778 	 *
13779 	 * Limit the number of VIs per port to the number of available
13780 	 * MAC addresses per port.
13781 	 */
13782 	if (t4_num_vis < 1)
13783 		t4_num_vis = 1;
13784 	if (t4_num_vis > nitems(vi_mac_funcs)) {
13785 		t4_num_vis = nitems(vi_mac_funcs);
13786 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
13787 	}
13788 
13789 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
13790 		pcie_relaxed_ordering = 1;
13791 #if defined(__i386__) || defined(__amd64__)
13792 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
13793 			pcie_relaxed_ordering = 0;
13794 #endif
13795 	}
13796 }
13797 
13798 #ifdef DDB
13799 static void
t4_dump_mem(struct adapter * sc,u_int addr,u_int len)13800 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
13801 {
13802 	uint32_t base, j, off, pf, reg, save, win_pos;
13803 
13804 	reg = chip_id(sc) > CHELSIO_T6 ?
13805 	    PCIE_MEM_ACCESS_T7_REG(A_PCIE_MEM_ACCESS_OFFSET0, 2) :
13806 	    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
13807 	save = t4_read_reg(sc, reg);
13808 	base = sc->memwin[2].mw_base;
13809 
13810 	if (is_t4(sc)) {
13811 		pf = 0;
13812 		win_pos = addr & ~0xf;	/* start must be 16B aligned */
13813 	} else {
13814 		pf = V_PFNUM(sc->pf);
13815 		win_pos = addr & ~0x7f;	/* start must be 128B aligned */
13816 	}
13817 	off = addr - win_pos;
13818 	if (chip_id(sc) > CHELSIO_T6)
13819 		win_pos >>= X_T7_MEMOFST_SHIFT;
13820 	t4_write_reg(sc, reg, win_pos | pf);
13821 	t4_read_reg(sc, reg);
13822 
13823 	while (len > 0 && !db_pager_quit) {
13824 		uint32_t buf[8];
13825 		for (j = 0; j < 8; j++, off += 4)
13826 			buf[j] = htonl(t4_read_reg(sc, base + off));
13827 
13828 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
13829 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
13830 		    buf[7]);
13831 		if (len <= sizeof(buf))
13832 			len = 0;
13833 		else
13834 			len -= sizeof(buf);
13835 	}
13836 
13837 	t4_write_reg(sc, reg, save);
13838 	t4_read_reg(sc, reg);
13839 }
13840 
13841 static void
t4_dump_tcb(struct adapter * sc,int tid)13842 t4_dump_tcb(struct adapter *sc, int tid)
13843 {
13844 	uint32_t tcb_addr;
13845 
13846 	/* Dump TCB for the tid */
13847 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
13848 	tcb_addr += tid * TCB_SIZE;
13849 	t4_dump_mem(sc, tcb_addr, TCB_SIZE);
13850 }
13851 
13852 static void
t4_dump_devlog(struct adapter * sc)13853 t4_dump_devlog(struct adapter *sc)
13854 {
13855 	struct devlog_params *dparams = &sc->params.devlog;
13856 	struct fw_devlog_e e;
13857 	int i, first, j, m, nentries, rc;
13858 	uint64_t ftstamp = UINT64_MAX;
13859 
13860 	if (dparams->start == 0) {
13861 		db_printf("devlog params not valid\n");
13862 		return;
13863 	}
13864 
13865 	nentries = dparams->size / sizeof(struct fw_devlog_e);
13866 	m = fwmtype_to_hwmtype(dparams->memtype);
13867 
13868 	/* Find the first entry. */
13869 	first = -1;
13870 	for (i = 0; i < nentries && !db_pager_quit; i++) {
13871 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
13872 		    sizeof(e), (void *)&e);
13873 		if (rc != 0)
13874 			break;
13875 
13876 		if (e.timestamp == 0)
13877 			break;
13878 
13879 		e.timestamp = be64toh(e.timestamp);
13880 		if (e.timestamp < ftstamp) {
13881 			ftstamp = e.timestamp;
13882 			first = i;
13883 		}
13884 	}
13885 
13886 	if (first == -1)
13887 		return;
13888 
13889 	i = first;
13890 	do {
13891 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
13892 		    sizeof(e), (void *)&e);
13893 		if (rc != 0)
13894 			return;
13895 
13896 		if (e.timestamp == 0)
13897 			return;
13898 
13899 		e.timestamp = be64toh(e.timestamp);
13900 		e.seqno = be32toh(e.seqno);
13901 		for (j = 0; j < 8; j++)
13902 			e.params[j] = be32toh(e.params[j]);
13903 
13904 		db_printf("%10d  %15ju  %8s  %8s  ",
13905 		    e.seqno, e.timestamp,
13906 		    (e.level < nitems(devlog_level_strings) ?
13907 			devlog_level_strings[e.level] : "UNKNOWN"),
13908 		    (e.facility < nitems(devlog_facility_strings) ?
13909 			devlog_facility_strings[e.facility] : "UNKNOWN"));
13910 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
13911 		    e.params[3], e.params[4], e.params[5], e.params[6],
13912 		    e.params[7]);
13913 
13914 		if (++i == nentries)
13915 			i = 0;
13916 	} while (i != first && !db_pager_quit);
13917 }
13918 
13919 static DB_DEFINE_TABLE(show, t4, show_t4);
13920 
DB_TABLE_COMMAND_FLAGS(show_t4,devlog,db_show_devlog,CS_OWN)13921 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
13922 {
13923 	device_t dev;
13924 	int t;
13925 	bool valid;
13926 
13927 	valid = false;
13928 	t = db_read_token();
13929 	if (t == tIDENT) {
13930 		dev = device_lookup_by_name(db_tok_string);
13931 		valid = true;
13932 	}
13933 	db_skip_to_eol();
13934 	if (!valid) {
13935 		db_printf("usage: show t4 devlog <nexus>\n");
13936 		return;
13937 	}
13938 
13939 	if (dev == NULL) {
13940 		db_printf("device not found\n");
13941 		return;
13942 	}
13943 
13944 	t4_dump_devlog(device_get_softc(dev));
13945 }
13946 
DB_TABLE_COMMAND_FLAGS(show_t4,tcb,db_show_t4tcb,CS_OWN)13947 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
13948 {
13949 	device_t dev;
13950 	int radix, tid, t;
13951 	bool valid;
13952 
13953 	valid = false;
13954 	radix = db_radix;
13955 	db_radix = 10;
13956 	t = db_read_token();
13957 	if (t == tIDENT) {
13958 		dev = device_lookup_by_name(db_tok_string);
13959 		t = db_read_token();
13960 		if (t == tNUMBER) {
13961 			tid = db_tok_number;
13962 			valid = true;
13963 		}
13964 	}
13965 	db_radix = radix;
13966 	db_skip_to_eol();
13967 	if (!valid) {
13968 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
13969 		return;
13970 	}
13971 
13972 	if (dev == NULL) {
13973 		db_printf("device not found\n");
13974 		return;
13975 	}
13976 	if (tid < 0) {
13977 		db_printf("invalid tid\n");
13978 		return;
13979 	}
13980 
13981 	t4_dump_tcb(device_get_softc(dev), tid);
13982 }
13983 
DB_TABLE_COMMAND_FLAGS(show_t4,memdump,db_show_memdump,CS_OWN)13984 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
13985 {
13986 	device_t dev;
13987 	int radix, t;
13988 	bool valid;
13989 
13990 	valid = false;
13991 	radix = db_radix;
13992 	db_radix = 10;
13993 	t = db_read_token();
13994 	if (t == tIDENT) {
13995 		dev = device_lookup_by_name(db_tok_string);
13996 		t = db_read_token();
13997 		if (t == tNUMBER) {
13998 			addr = db_tok_number;
13999 			t = db_read_token();
14000 			if (t == tNUMBER) {
14001 				count = db_tok_number;
14002 				valid = true;
14003 			}
14004 		}
14005 	}
14006 	db_radix = radix;
14007 	db_skip_to_eol();
14008 	if (!valid) {
14009 		db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
14010 		return;
14011 	}
14012 
14013 	if (dev == NULL) {
14014 		db_printf("device not found\n");
14015 		return;
14016 	}
14017 	if (addr < 0) {
14018 		db_printf("invalid address\n");
14019 		return;
14020 	}
14021 	if (count <= 0) {
14022 		db_printf("invalid length\n");
14023 		return;
14024 	}
14025 
14026 	t4_dump_mem(device_get_softc(dev), addr, count);
14027 }
14028 #endif
14029 
14030 static eventhandler_tag vxlan_start_evtag;
14031 static eventhandler_tag vxlan_stop_evtag;
14032 
14033 struct vxlan_evargs {
14034 	if_t ifp;
14035 	uint16_t port;
14036 };
14037 
14038 static void
enable_vxlan_rx(struct adapter * sc)14039 enable_vxlan_rx(struct adapter *sc)
14040 {
14041 	int i, rc;
14042 	struct port_info *pi;
14043 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
14044 
14045 	ASSERT_SYNCHRONIZED_OP(sc);
14046 
14047 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
14048 	    F_VXLAN_EN);
14049 	for_each_port(sc, i) {
14050 		pi = sc->port[i];
14051 		if (pi->vxlan_tcam_entry == true)
14052 			continue;
14053 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
14054 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
14055 		    true);
14056 		if (rc < 0) {
14057 			rc = -rc;
14058 			CH_ERR(&pi->vi[0],
14059 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
14060 		} else {
14061 			MPASS(rc == sc->rawf_base + pi->port_id);
14062 			pi->vxlan_tcam_entry = true;
14063 		}
14064 	}
14065 }
14066 
14067 static void
t4_vxlan_start(struct adapter * sc,void * arg)14068 t4_vxlan_start(struct adapter *sc, void *arg)
14069 {
14070 	struct vxlan_evargs *v = arg;
14071 
14072 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
14073 		return;
14074 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
14075 		return;
14076 
14077 	if (sc->vxlan_refcount == 0) {
14078 		sc->vxlan_port = v->port;
14079 		sc->vxlan_refcount = 1;
14080 		if (!hw_off_limits(sc))
14081 			enable_vxlan_rx(sc);
14082 	} else if (sc->vxlan_port == v->port) {
14083 		sc->vxlan_refcount++;
14084 	} else {
14085 		CH_ERR(sc, "VXLAN already configured on port  %d; "
14086 		    "ignoring attempt to configure it on port %d\n",
14087 		    sc->vxlan_port, v->port);
14088 	}
14089 	end_synchronized_op(sc, 0);
14090 }
14091 
14092 static void
t4_vxlan_stop(struct adapter * sc,void * arg)14093 t4_vxlan_stop(struct adapter *sc, void *arg)
14094 {
14095 	struct vxlan_evargs *v = arg;
14096 
14097 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
14098 		return;
14099 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
14100 		return;
14101 
14102 	/*
14103 	 * VXLANs may have been configured before the driver was loaded so we
14104 	 * may see more stops than starts.  This is not handled cleanly but at
14105 	 * least we keep the refcount sane.
14106 	 */
14107 	if (sc->vxlan_port != v->port)
14108 		goto done;
14109 	if (sc->vxlan_refcount == 0) {
14110 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
14111 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
14112 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
14113 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
14114 done:
14115 	end_synchronized_op(sc, 0);
14116 }
14117 
14118 static void
t4_vxlan_start_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)14119 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
14120     sa_family_t family, u_int port)
14121 {
14122 	struct vxlan_evargs v;
14123 
14124 	MPASS(family == AF_INET || family == AF_INET6);
14125 	v.ifp = ifp;
14126 	v.port = port;
14127 
14128 	t4_iterate(t4_vxlan_start, &v);
14129 }
14130 
14131 static void
t4_vxlan_stop_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)14132 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
14133     u_int port)
14134 {
14135 	struct vxlan_evargs v;
14136 
14137 	MPASS(family == AF_INET || family == AF_INET6);
14138 	v.ifp = ifp;
14139 	v.port = port;
14140 
14141 	t4_iterate(t4_vxlan_stop, &v);
14142 }
14143 
14144 
14145 static struct sx mlu;	/* mod load unload */
14146 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
14147 
14148 static int
mod_event(module_t mod,int cmd,void * arg)14149 mod_event(module_t mod, int cmd, void *arg)
14150 {
14151 	int rc = 0;
14152 	static int loaded = 0;
14153 
14154 	switch (cmd) {
14155 	case MOD_LOAD:
14156 		sx_xlock(&mlu);
14157 		if (loaded++ == 0) {
14158 			t4_sge_modload();
14159 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
14160 			    t4_filter_rpl, CPL_COOKIE_FILTER);
14161 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
14162 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
14163 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
14164 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
14165 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
14166 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
14167 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
14168 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
14169 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
14170 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
14171 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
14172 			    do_smt_write_rpl);
14173 			sx_init(&t4_list_lock, "T4/T5 adapters");
14174 			SLIST_INIT(&t4_list);
14175 			callout_init(&fatal_callout, 1);
14176 #ifdef TCP_OFFLOAD
14177 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
14178 #endif
14179 #ifdef INET6
14180 			t4_clip_modload();
14181 #endif
14182 #ifdef KERN_TLS
14183 			t6_ktls_modload();
14184 			t7_ktls_modload();
14185 #endif
14186 			t4_tracer_modload();
14187 			tweak_tunables();
14188 			vxlan_start_evtag =
14189 			    EVENTHANDLER_REGISTER(vxlan_start,
14190 				t4_vxlan_start_handler, NULL,
14191 				EVENTHANDLER_PRI_ANY);
14192 			vxlan_stop_evtag =
14193 			    EVENTHANDLER_REGISTER(vxlan_stop,
14194 				t4_vxlan_stop_handler, NULL,
14195 				EVENTHANDLER_PRI_ANY);
14196 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
14197 			    taskqueue_thread_enqueue, &reset_tq);
14198 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
14199 			    "t4_rst_thr");
14200 		}
14201 		sx_xunlock(&mlu);
14202 		break;
14203 
14204 	case MOD_UNLOAD:
14205 		sx_xlock(&mlu);
14206 		if (--loaded == 0) {
14207 #ifdef TCP_OFFLOAD
14208 			int i;
14209 #endif
14210 			int tries;
14211 
14212 			taskqueue_free(reset_tq);
14213 
14214 			tries = 0;
14215 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
14216 				uprintf("%ju clusters with custom free routine "
14217 				    "still is use.\n", t4_sge_extfree_refs());
14218 				pause("t4unload", 2 * hz);
14219 			}
14220 
14221 			sx_slock(&t4_list_lock);
14222 			if (!SLIST_EMPTY(&t4_list)) {
14223 				rc = EBUSY;
14224 				sx_sunlock(&t4_list_lock);
14225 				goto done_unload;
14226 			}
14227 #ifdef TCP_OFFLOAD
14228 			sx_slock(&t4_uld_list_lock);
14229 			for (i = 0; i <= ULD_MAX; i++) {
14230 				if (t4_uld_list[i] != NULL) {
14231 					rc = EBUSY;
14232 					sx_sunlock(&t4_uld_list_lock);
14233 					sx_sunlock(&t4_list_lock);
14234 					goto done_unload;
14235 				}
14236 			}
14237 			sx_sunlock(&t4_uld_list_lock);
14238 #endif
14239 			sx_sunlock(&t4_list_lock);
14240 
14241 			if (t4_sge_extfree_refs() == 0) {
14242 				EVENTHANDLER_DEREGISTER(vxlan_start,
14243 				    vxlan_start_evtag);
14244 				EVENTHANDLER_DEREGISTER(vxlan_stop,
14245 				    vxlan_stop_evtag);
14246 				t4_tracer_modunload();
14247 #ifdef KERN_TLS
14248 				t7_ktls_modunload();
14249 				t6_ktls_modunload();
14250 #endif
14251 #ifdef INET6
14252 				t4_clip_modunload();
14253 #endif
14254 #ifdef TCP_OFFLOAD
14255 				sx_destroy(&t4_uld_list_lock);
14256 #endif
14257 				sx_destroy(&t4_list_lock);
14258 				t4_sge_modunload();
14259 				loaded = 0;
14260 			} else {
14261 				rc = EBUSY;
14262 				loaded++;	/* undo earlier decrement */
14263 			}
14264 		}
14265 done_unload:
14266 		sx_xunlock(&mlu);
14267 		break;
14268 	}
14269 
14270 	return (rc);
14271 }
14272 
14273 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
14274 MODULE_VERSION(t4nex, 1);
14275 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
14276 #ifdef DEV_NETMAP
14277 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
14278 #endif /* DEV_NETMAP */
14279 
14280 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
14281 MODULE_VERSION(t5nex, 1);
14282 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
14283 #ifdef DEV_NETMAP
14284 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
14285 #endif /* DEV_NETMAP */
14286 
14287 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
14288 MODULE_VERSION(t6nex, 1);
14289 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
14290 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
14291 #ifdef DEV_NETMAP
14292 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
14293 #endif /* DEV_NETMAP */
14294 
14295 DRIVER_MODULE(chnex, pci, ch_driver, mod_event, 0);
14296 MODULE_VERSION(chnex, 1);
14297 MODULE_DEPEND(chnex, crypto, 1, 1, 1);
14298 MODULE_DEPEND(chnex, firmware, 1, 1, 1);
14299 #ifdef DEV_NETMAP
14300 MODULE_DEPEND(chnex, netmap, 1, 1, 1);
14301 #endif /* DEV_NETMAP */
14302 
14303 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
14304 MODULE_VERSION(cxgbe, 1);
14305 
14306 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
14307 MODULE_VERSION(cxl, 1);
14308 
14309 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
14310 MODULE_VERSION(cc, 1);
14311 
14312 DRIVER_MODULE(che, chnex, che_driver, 0, 0);
14313 MODULE_VERSION(che, 1);
14314 
14315 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
14316 MODULE_VERSION(vcxgbe, 1);
14317 
14318 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
14319 MODULE_VERSION(vcxl, 1);
14320 
14321 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
14322 MODULE_VERSION(vcc, 1);
14323 
14324 DRIVER_MODULE(vche, che, vche_driver, 0, 0);
14325 MODULE_VERSION(vche, 1);
14326