xref: /freebsd-13-stable/sys/dev/cxgbe/t4_main.c (revision a7494467d1763b295c8fadde174ca954b4ac2bc9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_ddb.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_kern_tls.h"
35 #include "opt_ratelimit.h"
36 #include "opt_rss.h"
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/priv.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/eventhandler.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/taskqueue.h>
48 #include <sys/pciio.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pci_private.h>
52 #include <sys/firmware.h>
53 #include <sys/sbuf.h>
54 #include <sys/smp.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <net/if_dl.h>
62 #include <net/if_vlan_var.h>
63 #ifdef RSS
64 #include <net/rss_config.h>
65 #endif
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 #ifdef KERN_TLS
69 #include <netinet/tcp_seq.h>
70 #endif
71 #if defined(__i386__) || defined(__amd64__)
72 #include <machine/md_var.h>
73 #include <machine/cputypes.h>
74 #include <vm/vm.h>
75 #include <vm/pmap.h>
76 #endif
77 #ifdef DDB
78 #include <ddb/ddb.h>
79 #include <ddb/db_lex.h>
80 #endif
81 
82 #include "common/common.h"
83 #include "common/t4_msg.h"
84 #include "common/t4_regs.h"
85 #include "common/t4_regs_values.h"
86 #include "cudbg/cudbg.h"
87 #include "t4_clip.h"
88 #include "t4_ioctl.h"
89 #include "t4_l2t.h"
90 #include "t4_mp_ring.h"
91 #include "t4_if.h"
92 #include "t4_smt.h"
93 
94 /* T4 bus driver interface */
95 static int t4_probe(device_t);
96 static int t4_attach(device_t);
97 static int t4_detach(device_t);
98 static int t4_child_location_str(device_t, device_t, char *, size_t);
99 static int t4_ready(device_t);
100 static int t4_read_port_device(device_t, int, device_t *);
101 static int t4_suspend(device_t);
102 static int t4_resume(device_t);
103 static int t4_reset_prepare(device_t, device_t);
104 static int t4_reset_post(device_t, device_t);
105 static device_method_t t4_methods[] = {
106 	DEVMETHOD(device_probe,		t4_probe),
107 	DEVMETHOD(device_attach,	t4_attach),
108 	DEVMETHOD(device_detach,	t4_detach),
109 	DEVMETHOD(device_suspend,	t4_suspend),
110 	DEVMETHOD(device_resume,	t4_resume),
111 
112 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
113 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
114 	DEVMETHOD(bus_reset_post,	t4_reset_post),
115 
116 	DEVMETHOD(t4_is_main_ready,	t4_ready),
117 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
118 
119 	DEVMETHOD_END
120 };
121 static driver_t t4_driver = {
122 	"t4nex",
123 	t4_methods,
124 	sizeof(struct adapter)
125 };
126 
127 
128 /* T4 port (cxgbe) interface */
129 static int cxgbe_probe(device_t);
130 static int cxgbe_attach(device_t);
131 static int cxgbe_detach(device_t);
132 device_method_t cxgbe_methods[] = {
133 	DEVMETHOD(device_probe,		cxgbe_probe),
134 	DEVMETHOD(device_attach,	cxgbe_attach),
135 	DEVMETHOD(device_detach,	cxgbe_detach),
136 	{ 0, 0 }
137 };
138 static driver_t cxgbe_driver = {
139 	"cxgbe",
140 	cxgbe_methods,
141 	sizeof(struct port_info)
142 };
143 
144 /* T4 VI (vcxgbe) interface */
145 static int vcxgbe_probe(device_t);
146 static int vcxgbe_attach(device_t);
147 static int vcxgbe_detach(device_t);
148 static device_method_t vcxgbe_methods[] = {
149 	DEVMETHOD(device_probe,		vcxgbe_probe),
150 	DEVMETHOD(device_attach,	vcxgbe_attach),
151 	DEVMETHOD(device_detach,	vcxgbe_detach),
152 	{ 0, 0 }
153 };
154 static driver_t vcxgbe_driver = {
155 	"vcxgbe",
156 	vcxgbe_methods,
157 	sizeof(struct vi_info)
158 };
159 
160 static d_ioctl_t t4_ioctl;
161 
162 static struct cdevsw t4_cdevsw = {
163        .d_version = D_VERSION,
164        .d_ioctl = t4_ioctl,
165        .d_name = "t4nex",
166 };
167 
168 /* T5 bus driver interface */
169 static int t5_probe(device_t);
170 static device_method_t t5_methods[] = {
171 	DEVMETHOD(device_probe,		t5_probe),
172 	DEVMETHOD(device_attach,	t4_attach),
173 	DEVMETHOD(device_detach,	t4_detach),
174 	DEVMETHOD(device_suspend,	t4_suspend),
175 	DEVMETHOD(device_resume,	t4_resume),
176 
177 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
178 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
179 	DEVMETHOD(bus_reset_post,	t4_reset_post),
180 
181 	DEVMETHOD(t4_is_main_ready,	t4_ready),
182 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
183 
184 	DEVMETHOD_END
185 };
186 static driver_t t5_driver = {
187 	"t5nex",
188 	t5_methods,
189 	sizeof(struct adapter)
190 };
191 
192 
193 /* T5 port (cxl) interface */
194 static driver_t cxl_driver = {
195 	"cxl",
196 	cxgbe_methods,
197 	sizeof(struct port_info)
198 };
199 
200 /* T5 VI (vcxl) interface */
201 static driver_t vcxl_driver = {
202 	"vcxl",
203 	vcxgbe_methods,
204 	sizeof(struct vi_info)
205 };
206 
207 /* T6 bus driver interface */
208 static int t6_probe(device_t);
209 static device_method_t t6_methods[] = {
210 	DEVMETHOD(device_probe,		t6_probe),
211 	DEVMETHOD(device_attach,	t4_attach),
212 	DEVMETHOD(device_detach,	t4_detach),
213 	DEVMETHOD(device_suspend,	t4_suspend),
214 	DEVMETHOD(device_resume,	t4_resume),
215 
216 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
217 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
218 	DEVMETHOD(bus_reset_post,	t4_reset_post),
219 
220 	DEVMETHOD(t4_is_main_ready,	t4_ready),
221 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
222 
223 	DEVMETHOD_END
224 };
225 static driver_t t6_driver = {
226 	"t6nex",
227 	t6_methods,
228 	sizeof(struct adapter)
229 };
230 
231 
232 /* T6 port (cc) interface */
233 static driver_t cc_driver = {
234 	"cc",
235 	cxgbe_methods,
236 	sizeof(struct port_info)
237 };
238 
239 /* T6 VI (vcc) interface */
240 static driver_t vcc_driver = {
241 	"vcc",
242 	vcxgbe_methods,
243 	sizeof(struct vi_info)
244 };
245 
246 /* ifnet interface */
247 static void cxgbe_init(void *);
248 static int cxgbe_ioctl(struct ifnet *, unsigned long, caddr_t);
249 static int cxgbe_transmit(struct ifnet *, struct mbuf *);
250 static void cxgbe_qflush(struct ifnet *);
251 #if defined(KERN_TLS) || defined(RATELIMIT)
252 static int cxgbe_snd_tag_alloc(struct ifnet *, union if_snd_tag_alloc_params *,
253     struct m_snd_tag **);
254 static int cxgbe_snd_tag_modify(struct m_snd_tag *,
255     union if_snd_tag_modify_params *);
256 static int cxgbe_snd_tag_query(struct m_snd_tag *,
257     union if_snd_tag_query_params *);
258 static void cxgbe_snd_tag_free(struct m_snd_tag *);
259 #endif
260 
261 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
262 
263 /*
264  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
265  * then ADAPTER_LOCK, then t4_uld_list_lock.
266  */
267 static struct sx t4_list_lock;
268 SLIST_HEAD(, adapter) t4_list;
269 #ifdef TCP_OFFLOAD
270 static struct sx t4_uld_list_lock;
271 SLIST_HEAD(, uld_info) t4_uld_list;
272 #endif
273 
274 /*
275  * Tunables.  See tweak_tunables() too.
276  *
277  * Each tunable is set to a default value here if it's known at compile-time.
278  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
279  * provide a reasonable default (upto n) when the driver is loaded.
280  *
281  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
282  * T5 are under hw.cxl.
283  */
284 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
285     "cxgbe(4) parameters");
286 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
287     "cxgbe(4) T5+ parameters");
288 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
289     "cxgbe(4) TOE parameters");
290 
291 /*
292  * Number of queues for tx and rx, NIC and offload.
293  */
294 #define NTXQ 16
295 int t4_ntxq = -NTXQ;
296 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
297     "Number of TX queues per port");
298 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
299 
300 #define NRXQ 8
301 int t4_nrxq = -NRXQ;
302 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
303     "Number of RX queues per port");
304 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
305 
306 #define NTXQ_VI 1
307 static int t4_ntxq_vi = -NTXQ_VI;
308 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
309     "Number of TX queues per VI");
310 
311 #define NRXQ_VI 1
312 static int t4_nrxq_vi = -NRXQ_VI;
313 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
314     "Number of RX queues per VI");
315 
316 static int t4_rsrv_noflowq = 0;
317 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
318     0, "Reserve TX queue 0 of each VI for non-flowid packets");
319 
320 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
321 #define NOFLDTXQ 8
322 static int t4_nofldtxq = -NOFLDTXQ;
323 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
324     "Number of offload TX queues per port");
325 
326 #define NOFLDRXQ 2
327 static int t4_nofldrxq = -NOFLDRXQ;
328 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
329     "Number of offload RX queues per port");
330 
331 #define NOFLDTXQ_VI 1
332 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
333 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
334     "Number of offload TX queues per VI");
335 
336 #define NOFLDRXQ_VI 1
337 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
338 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
339     "Number of offload RX queues per VI");
340 
341 #define TMR_IDX_OFLD 1
342 int t4_tmr_idx_ofld = TMR_IDX_OFLD;
343 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
344     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
345 
346 #define PKTC_IDX_OFLD (-1)
347 int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
348 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
349     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
350 
351 /* 0 means chip/fw default, non-zero number is value in microseconds */
352 static u_long t4_toe_keepalive_idle = 0;
353 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
354     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
355 
356 /* 0 means chip/fw default, non-zero number is value in microseconds */
357 static u_long t4_toe_keepalive_interval = 0;
358 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
359     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
360 
361 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
362 static int t4_toe_keepalive_count = 0;
363 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
364     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
365 
366 /* 0 means chip/fw default, non-zero number is value in microseconds */
367 static u_long t4_toe_rexmt_min = 0;
368 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
369     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
370 
371 /* 0 means chip/fw default, non-zero number is value in microseconds */
372 static u_long t4_toe_rexmt_max = 0;
373 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
374     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
375 
376 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
377 static int t4_toe_rexmt_count = 0;
378 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
379     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
380 
381 /* -1 means chip/fw default, other values are raw backoff values to use */
382 static int t4_toe_rexmt_backoff[16] = {
383 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
384 };
385 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
386     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
387     "cxgbe(4) TOE retransmit backoff values");
388 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
389     &t4_toe_rexmt_backoff[0], 0, "");
390 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
391     &t4_toe_rexmt_backoff[1], 0, "");
392 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
393     &t4_toe_rexmt_backoff[2], 0, "");
394 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
395     &t4_toe_rexmt_backoff[3], 0, "");
396 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
397     &t4_toe_rexmt_backoff[4], 0, "");
398 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
399     &t4_toe_rexmt_backoff[5], 0, "");
400 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
401     &t4_toe_rexmt_backoff[6], 0, "");
402 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
403     &t4_toe_rexmt_backoff[7], 0, "");
404 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
405     &t4_toe_rexmt_backoff[8], 0, "");
406 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
407     &t4_toe_rexmt_backoff[9], 0, "");
408 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
409     &t4_toe_rexmt_backoff[10], 0, "");
410 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
411     &t4_toe_rexmt_backoff[11], 0, "");
412 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
413     &t4_toe_rexmt_backoff[12], 0, "");
414 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
415     &t4_toe_rexmt_backoff[13], 0, "");
416 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
417     &t4_toe_rexmt_backoff[14], 0, "");
418 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
419     &t4_toe_rexmt_backoff[15], 0, "");
420 
421 static int t4_toe_tls_rx_timeout = 5;
422 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, tls_rx_timeout, CTLFLAG_RDTUN,
423     &t4_toe_tls_rx_timeout, 0,
424     "Timeout in seconds to downgrade TLS sockets to plain TOE");
425 #endif
426 
427 #ifdef DEV_NETMAP
428 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
429 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
430 static int t4_native_netmap = NN_EXTRA_VI;
431 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
432     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
433 
434 #define NNMTXQ 8
435 static int t4_nnmtxq = -NNMTXQ;
436 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
437     "Number of netmap TX queues");
438 
439 #define NNMRXQ 8
440 static int t4_nnmrxq = -NNMRXQ;
441 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
442     "Number of netmap RX queues");
443 
444 #define NNMTXQ_VI 2
445 static int t4_nnmtxq_vi = -NNMTXQ_VI;
446 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
447     "Number of netmap TX queues per VI");
448 
449 #define NNMRXQ_VI 2
450 static int t4_nnmrxq_vi = -NNMRXQ_VI;
451 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
452     "Number of netmap RX queues per VI");
453 #endif
454 
455 /*
456  * Holdoff parameters for ports.
457  */
458 #define TMR_IDX 1
459 int t4_tmr_idx = TMR_IDX;
460 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
461     0, "Holdoff timer index");
462 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
463 
464 #define PKTC_IDX (-1)
465 int t4_pktc_idx = PKTC_IDX;
466 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
467     0, "Holdoff packet counter index");
468 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
469 
470 /*
471  * Size (# of entries) of each tx and rx queue.
472  */
473 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
474 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
475     "Number of descriptors in each TX queue");
476 
477 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
478 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
479     "Number of descriptors in each RX queue");
480 
481 /*
482  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
483  */
484 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
485 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
486     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
487 
488 /*
489  * Configuration file.  All the _CF names here are special.
490  */
491 #define DEFAULT_CF	"default"
492 #define BUILTIN_CF	"built-in"
493 #define FLASH_CF	"flash"
494 #define UWIRE_CF	"uwire"
495 #define FPGA_CF		"fpga"
496 static char t4_cfg_file[32] = DEFAULT_CF;
497 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
498     sizeof(t4_cfg_file), "Firmware configuration file");
499 
500 /*
501  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
502  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
503  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
504  *            mark or when signalled to do so, 0 to never emit PAUSE.
505  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
506  *                 negotiated settings will override rx_pause/tx_pause.
507  *                 Otherwise rx_pause/tx_pause are applied forcibly.
508  */
509 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
510 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
511     &t4_pause_settings, 0,
512     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
513 
514 /*
515  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
516  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
517  *  0 to disable FEC.
518  */
519 static int t4_fec = -1;
520 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
521     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
522 
523 /*
524  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
525  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
526  * driver runs as if this is set to 0.
527  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
528  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
529  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
530  *    the firmware anyway (may result in l1cfg errors with old firmwares).
531  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
532  *    means set all FEC bits that are valid for the speed.
533  */
534 static int t4_force_fec = -1;
535 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
536     "Controls the use of FORCE_FEC bit in L1 configuration.");
537 
538 /*
539  * Link autonegotiation.
540  * -1 to run with the firmware default.
541  *  0 to disable.
542  *  1 to enable.
543  */
544 static int t4_autoneg = -1;
545 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
546     "Link autonegotiation");
547 
548 /*
549  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
550  * encouraged respectively).  '-n' is the same as 'n' except the firmware
551  * version used in the checks is read from the firmware bundled with the driver.
552  */
553 static int t4_fw_install = 1;
554 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
555     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
556 
557 /*
558  * ASIC features that will be used.  Disable the ones you don't want so that the
559  * chip resources aren't wasted on features that will not be used.
560  */
561 static int t4_nbmcaps_allowed = 0;
562 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
563     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
564 
565 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
566 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
567     &t4_linkcaps_allowed, 0, "Default link capabilities");
568 
569 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
570     FW_CAPS_CONFIG_SWITCH_EGRESS;
571 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
572     &t4_switchcaps_allowed, 0, "Default switch capabilities");
573 
574 #ifdef RATELIMIT
575 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
576 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
577 #else
578 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
579 	FW_CAPS_CONFIG_NIC_HASHFILTER;
580 #endif
581 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
582     &t4_niccaps_allowed, 0, "Default NIC capabilities");
583 
584 static int t4_toecaps_allowed = -1;
585 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
586     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
587 
588 static int t4_rdmacaps_allowed = -1;
589 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
590     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
591 
592 static int t4_cryptocaps_allowed = -1;
593 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
594     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
595 
596 static int t4_iscsicaps_allowed = -1;
597 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
598     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
599 
600 static int t4_fcoecaps_allowed = 0;
601 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
602     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
603 
604 static int t5_write_combine = 0;
605 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
606     0, "Use WC instead of UC for BAR2");
607 
608 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
609 static int t4_doorbells_allowed = 0xf;
610 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
611 	   &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
612 
613 static int t4_num_vis = 1;
614 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
615     "Number of VIs per port");
616 
617 /*
618  * PCIe Relaxed Ordering.
619  * -1: driver should figure out a good value.
620  * 0: disable RO.
621  * 1: enable RO.
622  * 2: leave RO alone.
623  */
624 static int pcie_relaxed_ordering = -1;
625 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
626     &pcie_relaxed_ordering, 0,
627     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
628 
629 static int t4_panic_on_fatal_err = 0;
630 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
631     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
632 
633 static int t4_reset_on_fatal_err = 0;
634 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
635     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
636 
637 static int t4_clock_gate_on_suspend = 0;
638 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
639     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
640 
641 static int t4_tx_vm_wr = 0;
642 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
643     "Use VM work requests to transmit packets.");
644 
645 /*
646  * Set to non-zero to enable the attack filter.  A packet that matches any of
647  * these conditions will get dropped on ingress:
648  * 1) IP && source address == destination address.
649  * 2) TCP/IP && source address is not a unicast address.
650  * 3) TCP/IP && destination address is not a unicast address.
651  * 4) IP && source address is loopback (127.x.y.z).
652  * 5) IP && destination address is loopback (127.x.y.z).
653  * 6) IPv6 && source address == destination address.
654  * 7) IPv6 && source address is not a unicast address.
655  * 8) IPv6 && source address is loopback (::1/128).
656  * 9) IPv6 && destination address is loopback (::1/128).
657  * 10) IPv6 && source address is unspecified (::/128).
658  * 11) IPv6 && destination address is unspecified (::/128).
659  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
660  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
661  */
662 static int t4_attack_filter = 0;
663 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
664     &t4_attack_filter, 0, "Drop suspicious traffic");
665 
666 static int t4_drop_ip_fragments = 0;
667 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
668     &t4_drop_ip_fragments, 0, "Drop IP fragments");
669 
670 static int t4_drop_pkts_with_l2_errors = 1;
671 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
672     &t4_drop_pkts_with_l2_errors, 0,
673     "Drop all frames with Layer 2 length or checksum errors");
674 
675 static int t4_drop_pkts_with_l3_errors = 0;
676 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
677     &t4_drop_pkts_with_l3_errors, 0,
678     "Drop all frames with IP version, length, or checksum errors");
679 
680 static int t4_drop_pkts_with_l4_errors = 0;
681 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
682     &t4_drop_pkts_with_l4_errors, 0,
683     "Drop all frames with Layer 4 length, checksum, or other errors");
684 
685 #ifdef TCP_OFFLOAD
686 /*
687  * TOE tunables.
688  */
689 static int t4_cop_managed_offloading = 0;
690 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
691     &t4_cop_managed_offloading, 0,
692     "COP (Connection Offload Policy) controls all TOE offload");
693 #endif
694 
695 #ifdef KERN_TLS
696 /*
697  * This enables KERN_TLS for all adapters if set.
698  */
699 static int t4_kern_tls = 0;
700 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
701     "Enable KERN_TLS mode for T6 adapters");
702 
703 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
704     "cxgbe(4) KERN_TLS parameters");
705 
706 static int t4_tls_inline_keys = 0;
707 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
708     &t4_tls_inline_keys, 0,
709     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
710     "in card memory.");
711 
712 static int t4_tls_combo_wrs = 0;
713 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
714     0, "Attempt to combine TCB field updates with TLS record work requests.");
715 #endif
716 
717 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
718 static int vi_mac_funcs[] = {
719 	FW_VI_FUNC_ETH,
720 	FW_VI_FUNC_OFLD,
721 	FW_VI_FUNC_IWARP,
722 	FW_VI_FUNC_OPENISCSI,
723 	FW_VI_FUNC_OPENFCOE,
724 	FW_VI_FUNC_FOISCSI,
725 	FW_VI_FUNC_FOFCOE,
726 };
727 
728 struct intrs_and_queues {
729 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
730 	uint16_t num_vis;	/* number of VIs for each port */
731 	uint16_t nirq;		/* Total # of vectors */
732 	uint16_t ntxq;		/* # of NIC txq's for each port */
733 	uint16_t nrxq;		/* # of NIC rxq's for each port */
734 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
735 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
736 	uint16_t nnmtxq;	/* # of netmap txq's */
737 	uint16_t nnmrxq;	/* # of netmap rxq's */
738 
739 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
740 	uint16_t ntxq_vi;	/* # of NIC txq's */
741 	uint16_t nrxq_vi;	/* # of NIC rxq's */
742 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
743 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
744 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
745 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
746 };
747 
748 static void setup_memwin(struct adapter *);
749 static void position_memwin(struct adapter *, int, uint32_t);
750 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
751 static int fwmtype_to_hwmtype(int);
752 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
753     uint32_t *);
754 static int fixup_devlog_params(struct adapter *);
755 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
756 static int contact_firmware(struct adapter *);
757 static int partition_resources(struct adapter *);
758 static int get_params__pre_init(struct adapter *);
759 static int set_params__pre_init(struct adapter *);
760 static int get_params__post_init(struct adapter *);
761 static int set_params__post_init(struct adapter *);
762 static void t4_set_desc(struct adapter *);
763 static bool fixed_ifmedia(struct port_info *);
764 static void build_medialist(struct port_info *);
765 static void init_link_config(struct port_info *);
766 static int fixup_link_config(struct port_info *);
767 static int apply_link_config(struct port_info *);
768 static int cxgbe_init_synchronized(struct vi_info *);
769 static int cxgbe_uninit_synchronized(struct vi_info *);
770 static int adapter_full_init(struct adapter *);
771 static void adapter_full_uninit(struct adapter *);
772 static int vi_full_init(struct vi_info *);
773 static void vi_full_uninit(struct vi_info *);
774 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
775 static void quiesce_txq(struct sge_txq *);
776 static void quiesce_wrq(struct sge_wrq *);
777 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
778 static void quiesce_vi(struct vi_info *);
779 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
780     driver_intr_t *, void *, char *);
781 static int t4_free_irq(struct adapter *, struct irq *);
782 static void t4_init_atid_table(struct adapter *);
783 static void t4_free_atid_table(struct adapter *);
784 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
785 static void vi_refresh_stats(struct vi_info *);
786 static void cxgbe_refresh_stats(struct vi_info *);
787 static void cxgbe_tick(void *);
788 static void vi_tick(void *);
789 static void cxgbe_sysctls(struct port_info *);
790 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
791 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
792 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
793 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
794 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
795 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
796 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
797 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
798 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
799 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
800 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
801 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
802 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
803 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
804 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
805 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
806 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
807 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
808 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
809 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
810 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
811 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
812 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
813 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
814 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
815 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
816 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
817 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
818 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
819 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
820 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
821 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
822 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
823 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
824 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
825 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
826 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
827 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
828 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
829 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
830 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
831 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
832 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
833 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
834 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
835 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
836 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
837 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
838 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
839 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
840 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
841 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
842 #ifdef TCP_OFFLOAD
843 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
844 static int sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS);
845 static int sysctl_tls_rx_timeout(SYSCTL_HANDLER_ARGS);
846 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
847 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
848 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
849 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
850 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
851 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
852 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
853 #endif
854 static int get_sge_context(struct adapter *, struct t4_sge_context *);
855 static int load_fw(struct adapter *, struct t4_data *);
856 static int load_cfg(struct adapter *, struct t4_data *);
857 static int load_boot(struct adapter *, struct t4_bootrom *);
858 static int load_bootcfg(struct adapter *, struct t4_data *);
859 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
860 static void free_offload_policy(struct t4_offload_policy *);
861 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
862 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
863 static int read_i2c(struct adapter *, struct t4_i2c_data *);
864 static int clear_stats(struct adapter *, u_int);
865 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
866 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
867 #ifdef TCP_OFFLOAD
868 static int toe_capability(struct vi_info *, bool);
869 static int t4_deactivate_all_uld(struct adapter *);
870 static void t4_async_event(struct adapter *);
871 #endif
872 #ifdef KERN_TLS
873 static int ktls_capability(struct adapter *, bool);
874 #endif
875 static int mod_event(module_t, int, void *);
876 static int notify_siblings(device_t, int);
877 static uint64_t vi_get_counter(struct ifnet *, ift_counter);
878 static uint64_t cxgbe_get_counter(struct ifnet *, ift_counter);
879 static void enable_vxlan_rx(struct adapter *);
880 static void reset_adapter_task(void *, int);
881 static void fatal_error_task(void *, int);
882 static void dump_devlog(struct adapter *);
883 static void dump_cim_regs(struct adapter *);
884 static void dump_cimla(struct adapter *);
885 
886 struct {
887 	uint16_t device;
888 	char *desc;
889 } t4_pciids[] = {
890 	{0xa000, "Chelsio Terminator 4 FPGA"},
891 	{0x4400, "Chelsio T440-dbg"},
892 	{0x4401, "Chelsio T420-CR"},
893 	{0x4402, "Chelsio T422-CR"},
894 	{0x4403, "Chelsio T440-CR"},
895 	{0x4404, "Chelsio T420-BCH"},
896 	{0x4405, "Chelsio T440-BCH"},
897 	{0x4406, "Chelsio T440-CH"},
898 	{0x4407, "Chelsio T420-SO"},
899 	{0x4408, "Chelsio T420-CX"},
900 	{0x4409, "Chelsio T420-BT"},
901 	{0x440a, "Chelsio T404-BT"},
902 	{0x440e, "Chelsio T440-LP-CR"},
903 }, t5_pciids[] = {
904 	{0xb000, "Chelsio Terminator 5 FPGA"},
905 	{0x5400, "Chelsio T580-dbg"},
906 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
907 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
908 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
909 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
910 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
911 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
912 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
913 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
914 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
915 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
916 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
917 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
918 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
919 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
920 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
921 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
922 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
923 
924 	/* Custom */
925 	{0x5483, "Custom T540-CR"},
926 	{0x5484, "Custom T540-BT"},
927 }, t6_pciids[] = {
928 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
929 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
930 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
931 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
932 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
933 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
934 	{0x6405, "Chelsio T6225-OCP-SO"},	/* 2 x 10/25G, nomem */
935 	{0x6406, "Chelsio T62100-OCP-SO"},	/* 2 x 40/50/100G, nomem */
936 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
937 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
938 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
939 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
940 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
941 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
942 	{0x6414, "Chelsio T61100-OCP-SO"},	/* 1 x 40/50/100G, nomem */
943 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
944 
945 	/* Custom */
946 	{0x6480, "Custom T6225-CR"},
947 	{0x6481, "Custom T62100-CR"},
948 	{0x6482, "Custom T6225-CR"},
949 	{0x6483, "Custom T62100-CR"},
950 	{0x6484, "Custom T64100-CR"},
951 	{0x6485, "Custom T6240-SO"},
952 	{0x6486, "Custom T6225-SO-CR"},
953 	{0x6487, "Custom T6225-CR"},
954 };
955 
956 #ifdef TCP_OFFLOAD
957 /*
958  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
959  * be exactly the same for both rxq and ofld_rxq.
960  */
961 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
962 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
963 #endif
964 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
965 
966 static int
t4_probe(device_t dev)967 t4_probe(device_t dev)
968 {
969 	int i;
970 	uint16_t v = pci_get_vendor(dev);
971 	uint16_t d = pci_get_device(dev);
972 	uint8_t f = pci_get_function(dev);
973 
974 	if (v != PCI_VENDOR_ID_CHELSIO)
975 		return (ENXIO);
976 
977 	/* Attach only to PF0 of the FPGA */
978 	if (d == 0xa000 && f != 0)
979 		return (ENXIO);
980 
981 	for (i = 0; i < nitems(t4_pciids); i++) {
982 		if (d == t4_pciids[i].device) {
983 			device_set_desc(dev, t4_pciids[i].desc);
984 			return (BUS_PROBE_DEFAULT);
985 		}
986 	}
987 
988 	return (ENXIO);
989 }
990 
991 static int
t5_probe(device_t dev)992 t5_probe(device_t dev)
993 {
994 	int i;
995 	uint16_t v = pci_get_vendor(dev);
996 	uint16_t d = pci_get_device(dev);
997 	uint8_t f = pci_get_function(dev);
998 
999 	if (v != PCI_VENDOR_ID_CHELSIO)
1000 		return (ENXIO);
1001 
1002 	/* Attach only to PF0 of the FPGA */
1003 	if (d == 0xb000 && f != 0)
1004 		return (ENXIO);
1005 
1006 	for (i = 0; i < nitems(t5_pciids); i++) {
1007 		if (d == t5_pciids[i].device) {
1008 			device_set_desc(dev, t5_pciids[i].desc);
1009 			return (BUS_PROBE_DEFAULT);
1010 		}
1011 	}
1012 
1013 	return (ENXIO);
1014 }
1015 
1016 static int
t6_probe(device_t dev)1017 t6_probe(device_t dev)
1018 {
1019 	int i;
1020 	uint16_t v = pci_get_vendor(dev);
1021 	uint16_t d = pci_get_device(dev);
1022 
1023 	if (v != PCI_VENDOR_ID_CHELSIO)
1024 		return (ENXIO);
1025 
1026 	for (i = 0; i < nitems(t6_pciids); i++) {
1027 		if (d == t6_pciids[i].device) {
1028 			device_set_desc(dev, t6_pciids[i].desc);
1029 			return (BUS_PROBE_DEFAULT);
1030 		}
1031 	}
1032 
1033 	return (ENXIO);
1034 }
1035 
1036 static void
t5_attribute_workaround(device_t dev)1037 t5_attribute_workaround(device_t dev)
1038 {
1039 	device_t root_port;
1040 	uint32_t v;
1041 
1042 	/*
1043 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1044 	 * Ordering attributes when replying to a TLP from a Root
1045 	 * Port.  As a workaround, find the parent Root Port and
1046 	 * disable No Snoop and Relaxed Ordering.  Note that this
1047 	 * affects all devices under this root port.
1048 	 */
1049 	root_port = pci_find_pcie_root_port(dev);
1050 	if (root_port == NULL) {
1051 		device_printf(dev, "Unable to find parent root port\n");
1052 		return;
1053 	}
1054 
1055 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1056 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1057 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1058 	    0)
1059 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1060 		    device_get_nameunit(root_port));
1061 }
1062 
1063 static const struct devnames devnames[] = {
1064 	{
1065 		.nexus_name = "t4nex",
1066 		.ifnet_name = "cxgbe",
1067 		.vi_ifnet_name = "vcxgbe",
1068 		.pf03_drv_name = "t4iov",
1069 		.vf_nexus_name = "t4vf",
1070 		.vf_ifnet_name = "cxgbev"
1071 	}, {
1072 		.nexus_name = "t5nex",
1073 		.ifnet_name = "cxl",
1074 		.vi_ifnet_name = "vcxl",
1075 		.pf03_drv_name = "t5iov",
1076 		.vf_nexus_name = "t5vf",
1077 		.vf_ifnet_name = "cxlv"
1078 	}, {
1079 		.nexus_name = "t6nex",
1080 		.ifnet_name = "cc",
1081 		.vi_ifnet_name = "vcc",
1082 		.pf03_drv_name = "t6iov",
1083 		.vf_nexus_name = "t6vf",
1084 		.vf_ifnet_name = "ccv"
1085 	}
1086 };
1087 
1088 void
t4_init_devnames(struct adapter * sc)1089 t4_init_devnames(struct adapter *sc)
1090 {
1091 	int id;
1092 
1093 	id = chip_id(sc);
1094 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
1095 		sc->names = &devnames[id - CHELSIO_T4];
1096 	else {
1097 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1098 		sc->names = NULL;
1099 	}
1100 }
1101 
1102 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1103 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1104 {
1105 	const char *parent, *name;
1106 	long value;
1107 	int line, unit;
1108 
1109 	line = 0;
1110 	parent = device_get_nameunit(sc->dev);
1111 	name = sc->names->ifnet_name;
1112 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1113 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1114 		    value == pi->port_id)
1115 			return (unit);
1116 	}
1117 	return (-1);
1118 }
1119 
1120 static void
t4_calibration(void * arg)1121 t4_calibration(void *arg)
1122 {
1123 	struct adapter *sc;
1124 	struct clock_sync *cur, *nex;
1125 	uint64_t hw;
1126 	sbintime_t sbt;
1127 	int next_up;
1128 
1129 	sc = (struct adapter *)arg;
1130 
1131 	KASSERT((hw_off_limits(sc) == 0), ("hw_off_limits at t4_calibration"));
1132 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1133 	sbt = sbinuptime();
1134 
1135 	cur = &sc->cal_info[sc->cal_current];
1136 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1137 	nex = &sc->cal_info[next_up];
1138 	if (__predict_false(sc->cal_count == 0)) {
1139 		/* First time in, just get the values in */
1140 		cur->hw_cur = hw;
1141 		cur->sbt_cur = sbt;
1142 		sc->cal_count++;
1143 		goto done;
1144 	}
1145 
1146 	if (cur->hw_cur == hw) {
1147 		/* The clock is not advancing? */
1148 		sc->cal_count = 0;
1149 		atomic_store_rel_int(&cur->gen, 0);
1150 		goto done;
1151 	}
1152 
1153 	seqc_write_begin(&nex->gen);
1154 	nex->hw_prev = cur->hw_cur;
1155 	nex->sbt_prev = cur->sbt_cur;
1156 	nex->hw_cur = hw;
1157 	nex->sbt_cur = sbt;
1158 	seqc_write_end(&nex->gen);
1159 	sc->cal_current = next_up;
1160 done:
1161 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1162 	    sc, C_DIRECT_EXEC);
1163 }
1164 
1165 static void
t4_calibration_start(struct adapter * sc)1166 t4_calibration_start(struct adapter *sc)
1167 {
1168 	/*
1169 	 * Here if we have not done a calibration
1170 	 * then do so otherwise start the appropriate
1171 	 * timer.
1172 	 */
1173 	int i;
1174 
1175 	for (i = 0; i < CNT_CAL_INFO; i++) {
1176 		sc->cal_info[i].gen = 0;
1177 	}
1178 	sc->cal_current = 0;
1179 	sc->cal_count = 0;
1180 	sc->cal_gen = 0;
1181 	t4_calibration(sc);
1182 }
1183 
1184 static int
t4_attach(device_t dev)1185 t4_attach(device_t dev)
1186 {
1187 	struct adapter *sc;
1188 	int rc = 0, i, j, rqidx, tqidx, nports;
1189 	struct make_dev_args mda;
1190 	struct intrs_and_queues iaq;
1191 	struct sge *s;
1192 	uint32_t *buf;
1193 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1194 	int ofld_tqidx;
1195 #endif
1196 #ifdef TCP_OFFLOAD
1197 	int ofld_rqidx;
1198 #endif
1199 #ifdef DEV_NETMAP
1200 	int nm_rqidx, nm_tqidx;
1201 #endif
1202 	int num_vis;
1203 
1204 	sc = device_get_softc(dev);
1205 	sc->dev = dev;
1206 	sysctl_ctx_init(&sc->ctx);
1207 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1208 
1209 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1210 		t5_attribute_workaround(dev);
1211 	pci_enable_busmaster(dev);
1212 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1213 		uint32_t v;
1214 
1215 		pci_set_max_read_req(dev, 4096);
1216 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1217 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1218 		if (pcie_relaxed_ordering == 0 &&
1219 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1220 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1221 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1222 		} else if (pcie_relaxed_ordering == 1 &&
1223 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1224 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1225 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1226 		}
1227 	}
1228 
1229 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1230 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1231 	sc->traceq = -1;
1232 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1233 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1234 	    device_get_nameunit(dev));
1235 
1236 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1237 	    device_get_nameunit(dev));
1238 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1239 	t4_add_adapter(sc);
1240 
1241 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1242 	TAILQ_INIT(&sc->sfl);
1243 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1244 
1245 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1246 
1247 	sc->policy = NULL;
1248 	rw_init(&sc->policy_lock, "connection offload policy");
1249 
1250 	callout_init(&sc->ktls_tick, 1);
1251 
1252 	callout_init(&sc->cal_callout, 1);
1253 
1254 	refcount_init(&sc->vxlan_refcount, 0);
1255 
1256 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1257 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1258 
1259 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1260 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1261 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1262 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1263 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1264 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1265 
1266 	rc = t4_map_bars_0_and_4(sc);
1267 	if (rc != 0)
1268 		goto done; /* error message displayed already */
1269 
1270 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1271 
1272 	/* Prepare the adapter for operation. */
1273 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1274 	rc = -t4_prep_adapter(sc, buf);
1275 	free(buf, M_CXGBE);
1276 	if (rc != 0) {
1277 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1278 		goto done;
1279 	}
1280 
1281 	/*
1282 	 * This is the real PF# to which we're attaching.  Works from within PCI
1283 	 * passthrough environments too, where pci_get_function() could return a
1284 	 * different PF# depending on the passthrough configuration.  We need to
1285 	 * use the real PF# in all our communication with the firmware.
1286 	 */
1287 	j = t4_read_reg(sc, A_PL_WHOAMI);
1288 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1289 	sc->mbox = sc->pf;
1290 
1291 	t4_init_devnames(sc);
1292 	if (sc->names == NULL) {
1293 		rc = ENOTSUP;
1294 		goto done; /* error message displayed already */
1295 	}
1296 
1297 	/*
1298 	 * Do this really early, with the memory windows set up even before the
1299 	 * character device.  The userland tool's register i/o and mem read
1300 	 * will work even in "recovery mode".
1301 	 */
1302 	setup_memwin(sc);
1303 	if (t4_init_devlog_params(sc, 0) == 0)
1304 		fixup_devlog_params(sc);
1305 	make_dev_args_init(&mda);
1306 	mda.mda_devsw = &t4_cdevsw;
1307 	mda.mda_uid = UID_ROOT;
1308 	mda.mda_gid = GID_WHEEL;
1309 	mda.mda_mode = 0600;
1310 	mda.mda_si_drv1 = sc;
1311 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1312 	if (rc != 0)
1313 		device_printf(dev, "failed to create nexus char device: %d.\n",
1314 		    rc);
1315 
1316 	/* Go no further if recovery mode has been requested. */
1317 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1318 		device_printf(dev, "recovery mode.\n");
1319 		goto done;
1320 	}
1321 
1322 #if defined(__i386__)
1323 	if ((cpu_feature & CPUID_CX8) == 0) {
1324 		device_printf(dev, "64 bit atomics not available.\n");
1325 		rc = ENOTSUP;
1326 		goto done;
1327 	}
1328 #endif
1329 
1330 	/* Contact the firmware and try to become the master driver. */
1331 	rc = contact_firmware(sc);
1332 	if (rc != 0)
1333 		goto done; /* error message displayed already */
1334 	MPASS(sc->flags & FW_OK);
1335 
1336 	rc = get_params__pre_init(sc);
1337 	if (rc != 0)
1338 		goto done; /* error message displayed already */
1339 
1340 	if (sc->flags & MASTER_PF) {
1341 		rc = partition_resources(sc);
1342 		if (rc != 0)
1343 			goto done; /* error message displayed already */
1344 	}
1345 
1346 	rc = get_params__post_init(sc);
1347 	if (rc != 0)
1348 		goto done; /* error message displayed already */
1349 
1350 	rc = set_params__post_init(sc);
1351 	if (rc != 0)
1352 		goto done; /* error message displayed already */
1353 
1354 	rc = t4_map_bar_2(sc);
1355 	if (rc != 0)
1356 		goto done; /* error message displayed already */
1357 
1358 	rc = t4_adj_doorbells(sc);
1359 	if (rc != 0)
1360 		goto done; /* error message displayed already */
1361 
1362 	rc = t4_create_dma_tag(sc);
1363 	if (rc != 0)
1364 		goto done; /* error message displayed already */
1365 
1366 	/*
1367 	 * First pass over all the ports - allocate VIs and initialize some
1368 	 * basic parameters like mac address, port type, etc.
1369 	 */
1370 	for_each_port(sc, i) {
1371 		struct port_info *pi;
1372 
1373 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1374 		sc->port[i] = pi;
1375 
1376 		/* These must be set before t4_port_init */
1377 		pi->adapter = sc;
1378 		pi->port_id = i;
1379 		/*
1380 		 * XXX: vi[0] is special so we can't delay this allocation until
1381 		 * pi->nvi's final value is known.
1382 		 */
1383 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1384 		    M_ZERO | M_WAITOK);
1385 
1386 		/*
1387 		 * Allocate the "main" VI and initialize parameters
1388 		 * like mac addr.
1389 		 */
1390 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1391 		if (rc != 0) {
1392 			device_printf(dev, "unable to initialize port %d: %d\n",
1393 			    i, rc);
1394 			free(pi->vi, M_CXGBE);
1395 			free(pi, M_CXGBE);
1396 			sc->port[i] = NULL;
1397 			goto done;
1398 		}
1399 
1400 		if (is_bt(pi->port_type))
1401 			setbit(&sc->bt_map, pi->tx_chan);
1402 		else
1403 			MPASS(!isset(&sc->bt_map, pi->tx_chan));
1404 
1405 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1406 		    device_get_nameunit(dev), i);
1407 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1408 		sc->chan_map[pi->tx_chan] = i;
1409 
1410 		/*
1411 		 * The MPS counter for FCS errors doesn't work correctly on the
1412 		 * T6 so we use the MAC counter here.  Which MAC is in use
1413 		 * depends on the link settings which will be known when the
1414 		 * link comes up.
1415 		 */
1416 		if (is_t6(sc))
1417 			pi->fcs_reg = -1;
1418 		else {
1419 			pi->fcs_reg = t4_port_reg(sc, pi->tx_chan,
1420 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1421 		}
1422 		pi->fcs_base = 0;
1423 
1424 		/* All VIs on this port share this media. */
1425 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1426 		    cxgbe_media_status);
1427 
1428 		PORT_LOCK(pi);
1429 		init_link_config(pi);
1430 		fixup_link_config(pi);
1431 		build_medialist(pi);
1432 		if (fixed_ifmedia(pi))
1433 			pi->flags |= FIXED_IFMEDIA;
1434 		PORT_UNLOCK(pi);
1435 
1436 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1437 		    t4_ifnet_unit(sc, pi));
1438 		if (pi->dev == NULL) {
1439 			device_printf(dev,
1440 			    "failed to add device for port %d.\n", i);
1441 			rc = ENXIO;
1442 			goto done;
1443 		}
1444 		pi->vi[0].dev = pi->dev;
1445 		device_set_softc(pi->dev, pi);
1446 	}
1447 
1448 	/*
1449 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1450 	 */
1451 	nports = sc->params.nports;
1452 	rc = cfg_itype_and_nqueues(sc, &iaq);
1453 	if (rc != 0)
1454 		goto done; /* error message displayed already */
1455 
1456 	num_vis = iaq.num_vis;
1457 	sc->intr_type = iaq.intr_type;
1458 	sc->intr_count = iaq.nirq;
1459 
1460 	s = &sc->sge;
1461 	s->nrxq = nports * iaq.nrxq;
1462 	s->ntxq = nports * iaq.ntxq;
1463 	if (num_vis > 1) {
1464 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1465 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1466 	}
1467 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1468 	s->neq += nports;		/* ctrl queues: 1 per port */
1469 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1470 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1471 	if (is_offload(sc) || is_ethoffload(sc)) {
1472 		s->nofldtxq = nports * iaq.nofldtxq;
1473 		if (num_vis > 1)
1474 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1475 		s->neq += s->nofldtxq;
1476 
1477 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1478 		    M_CXGBE, M_ZERO | M_WAITOK);
1479 	}
1480 #endif
1481 #ifdef TCP_OFFLOAD
1482 	if (is_offload(sc)) {
1483 		s->nofldrxq = nports * iaq.nofldrxq;
1484 		if (num_vis > 1)
1485 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1486 		s->neq += s->nofldrxq;	/* free list */
1487 		s->niq += s->nofldrxq;
1488 
1489 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1490 		    M_CXGBE, M_ZERO | M_WAITOK);
1491 	}
1492 #endif
1493 #ifdef DEV_NETMAP
1494 	s->nnmrxq = 0;
1495 	s->nnmtxq = 0;
1496 	if (t4_native_netmap & NN_MAIN_VI) {
1497 		s->nnmrxq += nports * iaq.nnmrxq;
1498 		s->nnmtxq += nports * iaq.nnmtxq;
1499 	}
1500 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1501 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1502 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1503 	}
1504 	s->neq += s->nnmtxq + s->nnmrxq;
1505 	s->niq += s->nnmrxq;
1506 
1507 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1508 	    M_CXGBE, M_ZERO | M_WAITOK);
1509 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1510 	    M_CXGBE, M_ZERO | M_WAITOK);
1511 #endif
1512 	MPASS(s->niq <= s->iqmap_sz);
1513 	MPASS(s->neq <= s->eqmap_sz);
1514 
1515 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1516 	    M_ZERO | M_WAITOK);
1517 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1518 	    M_ZERO | M_WAITOK);
1519 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1520 	    M_ZERO | M_WAITOK);
1521 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1522 	    M_ZERO | M_WAITOK);
1523 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1524 	    M_ZERO | M_WAITOK);
1525 
1526 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1527 	    M_ZERO | M_WAITOK);
1528 
1529 	t4_init_l2t(sc, M_WAITOK);
1530 	t4_init_smt(sc, M_WAITOK);
1531 	t4_init_tx_sched(sc);
1532 	t4_init_atid_table(sc);
1533 #ifdef RATELIMIT
1534 	t4_init_etid_table(sc);
1535 #endif
1536 #ifdef INET6
1537 	t4_init_clip_table(sc);
1538 #endif
1539 	if (sc->vres.key.size != 0)
1540 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1541 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1542 
1543 	/*
1544 	 * Second pass over the ports.  This time we know the number of rx and
1545 	 * tx queues that each port should get.
1546 	 */
1547 	rqidx = tqidx = 0;
1548 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1549 	ofld_tqidx = 0;
1550 #endif
1551 #ifdef TCP_OFFLOAD
1552 	ofld_rqidx = 0;
1553 #endif
1554 #ifdef DEV_NETMAP
1555 	nm_rqidx = nm_tqidx = 0;
1556 #endif
1557 	for_each_port(sc, i) {
1558 		struct port_info *pi = sc->port[i];
1559 		struct vi_info *vi;
1560 
1561 		if (pi == NULL)
1562 			continue;
1563 
1564 		pi->nvi = num_vis;
1565 		for_each_vi(pi, j, vi) {
1566 			vi->pi = pi;
1567 			vi->adapter = sc;
1568 			vi->first_intr = -1;
1569 			vi->qsize_rxq = t4_qsize_rxq;
1570 			vi->qsize_txq = t4_qsize_txq;
1571 
1572 			vi->first_rxq = rqidx;
1573 			vi->first_txq = tqidx;
1574 			vi->tmr_idx = t4_tmr_idx;
1575 			vi->pktc_idx = t4_pktc_idx;
1576 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1577 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1578 
1579 			rqidx += vi->nrxq;
1580 			tqidx += vi->ntxq;
1581 
1582 			if (j == 0 && vi->ntxq > 1)
1583 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1584 			else
1585 				vi->rsrv_noflowq = 0;
1586 
1587 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1588 			vi->first_ofld_txq = ofld_tqidx;
1589 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1590 			ofld_tqidx += vi->nofldtxq;
1591 #endif
1592 #ifdef TCP_OFFLOAD
1593 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1594 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1595 			vi->first_ofld_rxq = ofld_rqidx;
1596 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1597 
1598 			ofld_rqidx += vi->nofldrxq;
1599 #endif
1600 #ifdef DEV_NETMAP
1601 			vi->first_nm_rxq = nm_rqidx;
1602 			vi->first_nm_txq = nm_tqidx;
1603 			if (j == 0) {
1604 				vi->nnmrxq = iaq.nnmrxq;
1605 				vi->nnmtxq = iaq.nnmtxq;
1606 			} else {
1607 				vi->nnmrxq = iaq.nnmrxq_vi;
1608 				vi->nnmtxq = iaq.nnmtxq_vi;
1609 			}
1610 			nm_rqidx += vi->nnmrxq;
1611 			nm_tqidx += vi->nnmtxq;
1612 #endif
1613 		}
1614 	}
1615 
1616 	rc = t4_setup_intr_handlers(sc);
1617 	if (rc != 0) {
1618 		device_printf(dev,
1619 		    "failed to setup interrupt handlers: %d\n", rc);
1620 		goto done;
1621 	}
1622 
1623 	rc = bus_generic_probe(dev);
1624 	if (rc != 0) {
1625 		device_printf(dev, "failed to probe child drivers: %d\n", rc);
1626 		goto done;
1627 	}
1628 
1629 	/*
1630 	 * Ensure thread-safe mailbox access (in debug builds).
1631 	 *
1632 	 * So far this was the only thread accessing the mailbox but various
1633 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1634 	 * will access the mailbox from different threads.
1635 	 */
1636 	sc->flags |= CHK_MBOX_ACCESS;
1637 
1638 	rc = bus_generic_attach(dev);
1639 	if (rc != 0) {
1640 		device_printf(dev,
1641 		    "failed to attach all child ports: %d\n", rc);
1642 		goto done;
1643 	}
1644 	t4_calibration_start(sc);
1645 
1646 	device_printf(dev,
1647 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1648 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1649 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1650 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1651 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1652 
1653 	t4_set_desc(sc);
1654 
1655 	notify_siblings(dev, 0);
1656 
1657 done:
1658 	if (rc != 0 && sc->cdev) {
1659 		/* cdev was created and so cxgbetool works; recover that way. */
1660 		device_printf(dev,
1661 		    "error during attach, adapter is now in recovery mode.\n");
1662 		rc = 0;
1663 	}
1664 
1665 	if (rc != 0)
1666 		t4_detach_common(dev);
1667 	else
1668 		t4_sysctls(sc);
1669 
1670 	return (rc);
1671 }
1672 
1673 static int
t4_child_location_str(device_t bus,device_t dev,char * buf,size_t buflen)1674 t4_child_location_str(device_t bus, device_t dev, char *buf, size_t buflen)
1675 {
1676 	struct adapter *sc;
1677 	struct port_info *pi;
1678 	int i;
1679 
1680 	sc = device_get_softc(bus);
1681 	buf[0] = '\0';
1682 	for_each_port(sc, i) {
1683 		pi = sc->port[i];
1684 		if (pi != NULL && pi->dev == dev) {
1685 			snprintf(buf, buflen, "port=%d", pi->port_id);
1686 			break;
1687 		}
1688 	}
1689 	return (0);
1690 }
1691 
1692 static int
t4_ready(device_t dev)1693 t4_ready(device_t dev)
1694 {
1695 	struct adapter *sc;
1696 
1697 	sc = device_get_softc(dev);
1698 	if (sc->flags & FW_OK)
1699 		return (0);
1700 	return (ENXIO);
1701 }
1702 
1703 static int
t4_read_port_device(device_t dev,int port,device_t * child)1704 t4_read_port_device(device_t dev, int port, device_t *child)
1705 {
1706 	struct adapter *sc;
1707 	struct port_info *pi;
1708 
1709 	sc = device_get_softc(dev);
1710 	if (port < 0 || port >= MAX_NPORTS)
1711 		return (EINVAL);
1712 	pi = sc->port[port];
1713 	if (pi == NULL || pi->dev == NULL)
1714 		return (ENXIO);
1715 	*child = pi->dev;
1716 	return (0);
1717 }
1718 
1719 static int
notify_siblings(device_t dev,int detaching)1720 notify_siblings(device_t dev, int detaching)
1721 {
1722 	device_t sibling;
1723 	int error, i;
1724 
1725 	error = 0;
1726 	for (i = 0; i < PCI_FUNCMAX; i++) {
1727 		if (i == pci_get_function(dev))
1728 			continue;
1729 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1730 		    pci_get_slot(dev), i);
1731 		if (sibling == NULL || !device_is_attached(sibling))
1732 			continue;
1733 		if (detaching)
1734 			error = T4_DETACH_CHILD(sibling);
1735 		else
1736 			(void)T4_ATTACH_CHILD(sibling);
1737 		if (error)
1738 			break;
1739 	}
1740 	return (error);
1741 }
1742 
1743 /*
1744  * Idempotent
1745  */
1746 static int
t4_detach(device_t dev)1747 t4_detach(device_t dev)
1748 {
1749 	int rc;
1750 
1751 	rc = notify_siblings(dev, 1);
1752 	if (rc) {
1753 		device_printf(dev,
1754 		    "failed to detach sibling devices: %d\n", rc);
1755 		return (rc);
1756 	}
1757 
1758 	return (t4_detach_common(dev));
1759 }
1760 
1761 int
t4_detach_common(device_t dev)1762 t4_detach_common(device_t dev)
1763 {
1764 	struct adapter *sc;
1765 	struct port_info *pi;
1766 	int i, rc;
1767 
1768 	sc = device_get_softc(dev);
1769 
1770 #ifdef TCP_OFFLOAD
1771 	rc = t4_deactivate_all_uld(sc);
1772 	if (rc) {
1773 		device_printf(dev,
1774 		    "failed to detach upper layer drivers: %d\n", rc);
1775 		return (rc);
1776 	}
1777 #endif
1778 
1779 	if (sc->cdev) {
1780 		destroy_dev(sc->cdev);
1781 		sc->cdev = NULL;
1782 	}
1783 
1784 	sx_xlock(&t4_list_lock);
1785 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1786 	sx_xunlock(&t4_list_lock);
1787 
1788 	sc->flags &= ~CHK_MBOX_ACCESS;
1789 	if (sc->flags & FULL_INIT_DONE) {
1790 		if (!(sc->flags & IS_VF))
1791 			t4_intr_disable(sc);
1792 	}
1793 
1794 	if (device_is_attached(dev)) {
1795 		rc = bus_generic_detach(dev);
1796 		if (rc) {
1797 			device_printf(dev,
1798 			    "failed to detach child devices: %d\n", rc);
1799 			return (rc);
1800 		}
1801 	}
1802 
1803 	for (i = 0; i < sc->intr_count; i++)
1804 		t4_free_irq(sc, &sc->irq[i]);
1805 
1806 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1807 		t4_free_tx_sched(sc);
1808 
1809 	for (i = 0; i < MAX_NPORTS; i++) {
1810 		pi = sc->port[i];
1811 		if (pi) {
1812 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1813 			if (pi->dev)
1814 				device_delete_child(dev, pi->dev);
1815 
1816 			mtx_destroy(&pi->pi_lock);
1817 			free(pi->vi, M_CXGBE);
1818 			free(pi, M_CXGBE);
1819 		}
1820 	}
1821 	callout_stop(&sc->cal_callout);
1822 	callout_drain(&sc->cal_callout);
1823 	device_delete_children(dev);
1824 	sysctl_ctx_free(&sc->ctx);
1825 	adapter_full_uninit(sc);
1826 
1827 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1828 		t4_fw_bye(sc, sc->mbox);
1829 
1830 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1831 		pci_release_msi(dev);
1832 
1833 	if (sc->regs_res)
1834 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1835 		    sc->regs_res);
1836 
1837 	if (sc->udbs_res)
1838 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1839 		    sc->udbs_res);
1840 
1841 	if (sc->msix_res)
1842 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1843 		    sc->msix_res);
1844 
1845 	if (sc->l2t)
1846 		t4_free_l2t(sc->l2t);
1847 	if (sc->smt)
1848 		t4_free_smt(sc->smt);
1849 	t4_free_atid_table(sc);
1850 #ifdef RATELIMIT
1851 	t4_free_etid_table(sc);
1852 #endif
1853 	if (sc->key_map)
1854 		vmem_destroy(sc->key_map);
1855 #ifdef INET6
1856 	t4_destroy_clip_table(sc);
1857 #endif
1858 
1859 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1860 	free(sc->sge.ofld_txq, M_CXGBE);
1861 #endif
1862 #ifdef TCP_OFFLOAD
1863 	free(sc->sge.ofld_rxq, M_CXGBE);
1864 #endif
1865 #ifdef DEV_NETMAP
1866 	free(sc->sge.nm_rxq, M_CXGBE);
1867 	free(sc->sge.nm_txq, M_CXGBE);
1868 #endif
1869 	free(sc->irq, M_CXGBE);
1870 	free(sc->sge.rxq, M_CXGBE);
1871 	free(sc->sge.txq, M_CXGBE);
1872 	free(sc->sge.ctrlq, M_CXGBE);
1873 	free(sc->sge.iqmap, M_CXGBE);
1874 	free(sc->sge.eqmap, M_CXGBE);
1875 	free(sc->tids.ftid_tab, M_CXGBE);
1876 	free(sc->tids.hpftid_tab, M_CXGBE);
1877 	free_hftid_hash(&sc->tids);
1878 	free(sc->tids.tid_tab, M_CXGBE);
1879 	free(sc->tt.tls_rx_ports, M_CXGBE);
1880 	t4_destroy_dma_tag(sc);
1881 
1882 	callout_drain(&sc->ktls_tick);
1883 	callout_drain(&sc->sfl_callout);
1884 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1885 		mtx_destroy(&sc->tids.ftid_lock);
1886 		cv_destroy(&sc->tids.ftid_cv);
1887 	}
1888 	if (mtx_initialized(&sc->tids.atid_lock))
1889 		mtx_destroy(&sc->tids.atid_lock);
1890 	if (mtx_initialized(&sc->ifp_lock))
1891 		mtx_destroy(&sc->ifp_lock);
1892 
1893 	if (rw_initialized(&sc->policy_lock)) {
1894 		rw_destroy(&sc->policy_lock);
1895 #ifdef TCP_OFFLOAD
1896 		if (sc->policy != NULL)
1897 			free_offload_policy(sc->policy);
1898 #endif
1899 	}
1900 
1901 	for (i = 0; i < NUM_MEMWIN; i++) {
1902 		struct memwin *mw = &sc->memwin[i];
1903 
1904 		if (rw_initialized(&mw->mw_lock))
1905 			rw_destroy(&mw->mw_lock);
1906 	}
1907 
1908 	mtx_destroy(&sc->sfl_lock);
1909 	mtx_destroy(&sc->reg_lock);
1910 	mtx_destroy(&sc->sc_lock);
1911 
1912 	bzero(sc, sizeof(*sc));
1913 
1914 	return (0);
1915 }
1916 
1917 static inline bool
ok_to_reset(struct adapter * sc)1918 ok_to_reset(struct adapter *sc)
1919 {
1920 	struct tid_info *t = &sc->tids;
1921 	struct port_info *pi;
1922 	struct vi_info *vi;
1923 	int i, j;
1924 	int caps = IFCAP_TOE | IFCAP_NETMAP | IFCAP_TXRTLMT;
1925 
1926 	if (is_t6(sc))
1927 		caps |= IFCAP_TXTLS;
1928 
1929 	ASSERT_SYNCHRONIZED_OP(sc);
1930 	MPASS(!(sc->flags & IS_VF));
1931 
1932 	for_each_port(sc, i) {
1933 		pi = sc->port[i];
1934 		for_each_vi(pi, j, vi) {
1935 			if (vi->ifp->if_capenable & caps)
1936 				return (false);
1937 		}
1938 	}
1939 
1940 	if (atomic_load_int(&t->tids_in_use) > 0)
1941 		return (false);
1942 	if (atomic_load_int(&t->stids_in_use) > 0)
1943 		return (false);
1944 	if (atomic_load_int(&t->atids_in_use) > 0)
1945 		return (false);
1946 	if (atomic_load_int(&t->ftids_in_use) > 0)
1947 		return (false);
1948 	if (atomic_load_int(&t->hpftids_in_use) > 0)
1949 		return (false);
1950 	if (atomic_load_int(&t->etids_in_use) > 0)
1951 		return (false);
1952 
1953 	return (true);
1954 }
1955 
1956 static inline int
stop_adapter(struct adapter * sc)1957 stop_adapter(struct adapter *sc)
1958 {
1959 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED)))
1960 		return (1);		/* Already stopped. */
1961 	return (t4_shutdown_adapter(sc));
1962 }
1963 
1964 static int
t4_suspend(device_t dev)1965 t4_suspend(device_t dev)
1966 {
1967 	struct adapter *sc = device_get_softc(dev);
1968 	struct port_info *pi;
1969 	struct vi_info *vi;
1970 	struct ifnet *ifp;
1971 	struct sge_rxq *rxq;
1972 	struct sge_txq *txq;
1973 	struct sge_wrq *wrq;
1974 #ifdef TCP_OFFLOAD
1975 	struct sge_ofld_rxq *ofld_rxq;
1976 #endif
1977 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1978 	struct sge_ofld_txq *ofld_txq;
1979 #endif
1980 	int rc, i, j, k;
1981 
1982 	CH_ALERT(sc, "suspend requested\n");
1983 
1984 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4sus");
1985 	if (rc != 0)
1986 		return (ENXIO);
1987 
1988 	/* XXX: Can the kernel call suspend repeatedly without resume? */
1989 	MPASS(!hw_off_limits(sc));
1990 
1991 	if (!ok_to_reset(sc)) {
1992 		/* XXX: should list what resource is preventing suspend. */
1993 		CH_ERR(sc, "not safe to suspend.\n");
1994 		rc = EBUSY;
1995 		goto done;
1996 	}
1997 
1998 	/* No more DMA or interrupts. */
1999 	stop_adapter(sc);
2000 
2001 	/* Quiesce all activity. */
2002 	for_each_port(sc, i) {
2003 		pi = sc->port[i];
2004 		pi->vxlan_tcam_entry = false;
2005 
2006 		PORT_LOCK(pi);
2007 		if (pi->up_vis > 0) {
2008 			/*
2009 			 * t4_shutdown_adapter has already shut down all the
2010 			 * PHYs but it also disables interrupts and DMA so there
2011 			 * won't be a link interrupt.  So we update the state
2012 			 * manually and inform the kernel.
2013 			 */
2014 			pi->link_cfg.link_ok = false;
2015 			t4_os_link_changed(pi);
2016 		}
2017 		PORT_UNLOCK(pi);
2018 
2019 		for_each_vi(pi, j, vi) {
2020 			vi->xact_addr_filt = -1;
2021 			mtx_lock(&vi->tick_mtx);
2022 			vi->flags |= VI_SKIP_STATS;
2023 			mtx_unlock(&vi->tick_mtx);
2024 			if (!(vi->flags & VI_INIT_DONE))
2025 				continue;
2026 
2027 			ifp = vi->ifp;
2028 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2029 				mtx_lock(&vi->tick_mtx);
2030 				callout_stop(&vi->tick);
2031 				mtx_unlock(&vi->tick_mtx);
2032 				callout_drain(&vi->tick);
2033 			}
2034 
2035 			/*
2036 			 * Note that the HW is not available.
2037 			 */
2038 			for_each_txq(vi, k, txq) {
2039 				TXQ_LOCK(txq);
2040 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2041 				TXQ_UNLOCK(txq);
2042 			}
2043 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2044 			for_each_ofld_txq(vi, k, ofld_txq) {
2045 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2046 			}
2047 #endif
2048 			for_each_rxq(vi, k, rxq) {
2049 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2050 			}
2051 #if defined(TCP_OFFLOAD)
2052 			for_each_ofld_rxq(vi, k, ofld_rxq) {
2053 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2054 			}
2055 #endif
2056 
2057 			quiesce_vi(vi);
2058 		}
2059 
2060 		if (sc->flags & FULL_INIT_DONE) {
2061 			/* Control queue */
2062 			wrq = &sc->sge.ctrlq[i];
2063 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2064 			quiesce_wrq(wrq);
2065 		}
2066 	}
2067 	if (sc->flags & FULL_INIT_DONE) {
2068 		/* Firmware event queue */
2069 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2070 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2071 	}
2072 
2073 	/* Stop calibration */
2074 	callout_stop(&sc->cal_callout);
2075 	callout_drain(&sc->cal_callout);
2076 
2077 	/* Mark the adapter totally off limits. */
2078 	mtx_lock(&sc->reg_lock);
2079 	atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
2080 	sc->flags &= ~(FW_OK | MASTER_PF);
2081 	sc->reset_thread = NULL;
2082 	mtx_unlock(&sc->reg_lock);
2083 
2084 	if (t4_clock_gate_on_suspend) {
2085 		t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2086 		    F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2087 		    F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2088 	}
2089 
2090 	CH_ALERT(sc, "suspend completed.\n");
2091 done:
2092 	end_synchronized_op(sc, 0);
2093 	return (rc);
2094 }
2095 
2096 struct adapter_pre_reset_state {
2097 	u_int flags;
2098 	uint16_t nbmcaps;
2099 	uint16_t linkcaps;
2100 	uint16_t switchcaps;
2101 	uint16_t niccaps;
2102 	uint16_t toecaps;
2103 	uint16_t rdmacaps;
2104 	uint16_t cryptocaps;
2105 	uint16_t iscsicaps;
2106 	uint16_t fcoecaps;
2107 
2108 	u_int cfcsum;
2109 	char cfg_file[32];
2110 
2111 	struct adapter_params params;
2112 	struct t4_virt_res vres;
2113 	struct tid_info tids;
2114 	struct sge sge;
2115 
2116 	int rawf_base;
2117 	int nrawf;
2118 
2119 };
2120 
2121 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2122 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2123 {
2124 
2125 	ASSERT_SYNCHRONIZED_OP(sc);
2126 
2127 	o->flags = sc->flags;
2128 
2129 	o->nbmcaps =  sc->nbmcaps;
2130 	o->linkcaps = sc->linkcaps;
2131 	o->switchcaps = sc->switchcaps;
2132 	o->niccaps = sc->niccaps;
2133 	o->toecaps = sc->toecaps;
2134 	o->rdmacaps = sc->rdmacaps;
2135 	o->cryptocaps = sc->cryptocaps;
2136 	o->iscsicaps = sc->iscsicaps;
2137 	o->fcoecaps = sc->fcoecaps;
2138 
2139 	o->cfcsum = sc->cfcsum;
2140 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2141 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2142 
2143 	o->params = sc->params;
2144 	o->vres = sc->vres;
2145 	o->tids = sc->tids;
2146 	o->sge = sc->sge;
2147 
2148 	o->rawf_base = sc->rawf_base;
2149 	o->nrawf = sc->nrawf;
2150 }
2151 
2152 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2153 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2154 {
2155 	int rc = 0;
2156 
2157 	ASSERT_SYNCHRONIZED_OP(sc);
2158 
2159 	/* Capabilities */
2160 #define COMPARE_CAPS(c) do { \
2161 	if (o->c##caps != sc->c##caps) { \
2162 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2163 		    sc->c##caps); \
2164 		rc = EINVAL; \
2165 	} \
2166 } while (0)
2167 	COMPARE_CAPS(nbm);
2168 	COMPARE_CAPS(link);
2169 	COMPARE_CAPS(switch);
2170 	COMPARE_CAPS(nic);
2171 	COMPARE_CAPS(toe);
2172 	COMPARE_CAPS(rdma);
2173 	COMPARE_CAPS(crypto);
2174 	COMPARE_CAPS(iscsi);
2175 	COMPARE_CAPS(fcoe);
2176 #undef COMPARE_CAPS
2177 
2178 	/* Firmware config file */
2179 	if (o->cfcsum != sc->cfcsum) {
2180 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2181 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2182 		rc = EINVAL;
2183 	}
2184 
2185 #define COMPARE_PARAM(p, name) do { \
2186 	if (o->p != sc->p) { \
2187 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2188 		rc = EINVAL; \
2189 	} \
2190 } while (0)
2191 	COMPARE_PARAM(sge.iq_start, iq_start);
2192 	COMPARE_PARAM(sge.eq_start, eq_start);
2193 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2194 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2195 	COMPARE_PARAM(tids.nftids, nftids);
2196 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2197 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2198 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2199 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2200 	COMPARE_PARAM(tids.tid_base, tid_base);
2201 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2202 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2203 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2204 	COMPARE_PARAM(rawf_base, rawf_base);
2205 	COMPARE_PARAM(nrawf, nrawf);
2206 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2207 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2208 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2209 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2210 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2211 	COMPARE_PARAM(tids.ntids, ntids);
2212 	COMPARE_PARAM(tids.etid_base, etid_base);
2213 	COMPARE_PARAM(tids.etid_end, etid_end);
2214 	COMPARE_PARAM(tids.netids, netids);
2215 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2216 	COMPARE_PARAM(params.ethoffload, ethoffload);
2217 	COMPARE_PARAM(tids.natids, natids);
2218 	COMPARE_PARAM(tids.stid_base, stid_base);
2219 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2220 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2221 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2222 	COMPARE_PARAM(vres.stag.start, stag_start);
2223 	COMPARE_PARAM(vres.stag.size, stag_size);
2224 	COMPARE_PARAM(vres.rq.start, rq_start);
2225 	COMPARE_PARAM(vres.rq.size, rq_size);
2226 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2227 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2228 	COMPARE_PARAM(vres.qp.start, qp_start);
2229 	COMPARE_PARAM(vres.qp.size, qp_size);
2230 	COMPARE_PARAM(vres.cq.start, cq_start);
2231 	COMPARE_PARAM(vres.cq.size, cq_size);
2232 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2233 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2234 	COMPARE_PARAM(vres.srq.start, srq_start);
2235 	COMPARE_PARAM(vres.srq.size, srq_size);
2236 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2237 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2238 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2239 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2240 	COMPARE_PARAM(vres.key.start, key_start);
2241 	COMPARE_PARAM(vres.key.size, key_size);
2242 #undef COMPARE_PARAM
2243 
2244 	return (rc);
2245 }
2246 
2247 static int
t4_resume(device_t dev)2248 t4_resume(device_t dev)
2249 {
2250 	struct adapter *sc = device_get_softc(dev);
2251 	struct adapter_pre_reset_state *old_state = NULL;
2252 	struct port_info *pi;
2253 	struct vi_info *vi;
2254 	struct ifnet *ifp;
2255 	struct sge_txq *txq;
2256 	int rc, i, j, k;
2257 
2258 	CH_ALERT(sc, "resume requested.\n");
2259 
2260 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4res");
2261 	if (rc != 0)
2262 		return (ENXIO);
2263 	MPASS(hw_off_limits(sc));
2264 	MPASS((sc->flags & FW_OK) == 0);
2265 	MPASS((sc->flags & MASTER_PF) == 0);
2266 	MPASS(sc->reset_thread == NULL);
2267 	sc->reset_thread = curthread;
2268 
2269 	/* Register access is expected to work by the time we're here. */
2270 	if (t4_read_reg(sc, A_PL_WHOAMI) == 0xffffffff) {
2271 		CH_ERR(sc, "%s: can't read device registers\n", __func__);
2272 		rc = ENXIO;
2273 		goto done;
2274 	}
2275 
2276 	/* Note that HW_OFF_LIMITS is cleared a bit later. */
2277 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR | ADAP_STOPPED);
2278 
2279 	/* Restore memory window. */
2280 	setup_memwin(sc);
2281 
2282 	/* Go no further if recovery mode has been requested. */
2283 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2284 		CH_ALERT(sc, "recovery mode on resume.\n");
2285 		rc = 0;
2286 		mtx_lock(&sc->reg_lock);
2287 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
2288 		mtx_unlock(&sc->reg_lock);
2289 		goto done;
2290 	}
2291 
2292 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2293 	save_caps_and_params(sc, old_state);
2294 
2295 	/* Reestablish contact with firmware and become the primary PF. */
2296 	rc = contact_firmware(sc);
2297 	if (rc != 0)
2298 		goto done; /* error message displayed already */
2299 	MPASS(sc->flags & FW_OK);
2300 
2301 	if (sc->flags & MASTER_PF) {
2302 		rc = partition_resources(sc);
2303 		if (rc != 0)
2304 			goto done; /* error message displayed already */
2305 	}
2306 
2307 	rc = get_params__post_init(sc);
2308 	if (rc != 0)
2309 		goto done; /* error message displayed already */
2310 
2311 	rc = set_params__post_init(sc);
2312 	if (rc != 0)
2313 		goto done; /* error message displayed already */
2314 
2315 	rc = compare_caps_and_params(sc, old_state);
2316 	if (rc != 0)
2317 		goto done; /* error message displayed already */
2318 
2319 	for_each_port(sc, i) {
2320 		pi = sc->port[i];
2321 		MPASS(pi != NULL);
2322 		MPASS(pi->vi != NULL);
2323 		MPASS(pi->vi[0].dev == pi->dev);
2324 
2325 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2326 		if (rc != 0) {
2327 			CH_ERR(sc,
2328 			    "failed to re-initialize port %d: %d\n", i, rc);
2329 			goto done;
2330 		}
2331 		MPASS(sc->chan_map[pi->tx_chan] == i);
2332 
2333 		PORT_LOCK(pi);
2334 		fixup_link_config(pi);
2335 		build_medialist(pi);
2336 		PORT_UNLOCK(pi);
2337 		for_each_vi(pi, j, vi) {
2338 			if (IS_MAIN_VI(vi))
2339 				continue;
2340 			rc = alloc_extra_vi(sc, pi, vi);
2341 			if (rc != 0) {
2342 				CH_ERR(vi,
2343 				    "failed to re-allocate extra VI: %d\n", rc);
2344 				goto done;
2345 			}
2346 		}
2347 	}
2348 
2349 	/*
2350 	 * Interrupts and queues are about to be enabled and other threads will
2351 	 * want to access the hardware too.  It is safe to do so.  Note that
2352 	 * this thread is still in the middle of a synchronized_op.
2353 	 */
2354 	mtx_lock(&sc->reg_lock);
2355 	atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
2356 	mtx_unlock(&sc->reg_lock);
2357 
2358 	if (sc->flags & FULL_INIT_DONE) {
2359 		rc = adapter_full_init(sc);
2360 		if (rc != 0) {
2361 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2362 			goto done;
2363 		}
2364 
2365 		if (sc->vxlan_refcount > 0)
2366 			enable_vxlan_rx(sc);
2367 
2368 		for_each_port(sc, i) {
2369 			pi = sc->port[i];
2370 			for_each_vi(pi, j, vi) {
2371 				mtx_lock(&vi->tick_mtx);
2372 				vi->flags &= ~VI_SKIP_STATS;
2373 				mtx_unlock(&vi->tick_mtx);
2374 				if (!(vi->flags & VI_INIT_DONE))
2375 					continue;
2376 				rc = vi_full_init(vi);
2377 				if (rc != 0) {
2378 					CH_ERR(vi, "failed to re-initialize "
2379 					    "interface: %d\n", rc);
2380 					goto done;
2381 				}
2382 
2383 				ifp = vi->ifp;
2384 				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2385 					continue;
2386 				/*
2387 				 * Note that we do not setup multicast addresses
2388 				 * in the first pass.  This ensures that the
2389 				 * unicast DMACs for all VIs on all ports get an
2390 				 * MPS TCAM entry.
2391 				 */
2392 				rc = update_mac_settings(ifp, XGMAC_ALL &
2393 				    ~XGMAC_MCADDRS);
2394 				if (rc != 0) {
2395 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2396 					goto done;
2397 				}
2398 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2399 				    true);
2400 				if (rc != 0) {
2401 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2402 					goto done;
2403 				}
2404 				for_each_txq(vi, k, txq) {
2405 					TXQ_LOCK(txq);
2406 					txq->eq.flags |= EQ_ENABLED;
2407 					TXQ_UNLOCK(txq);
2408 				}
2409 				mtx_lock(&vi->tick_mtx);
2410 				callout_schedule(&vi->tick, hz);
2411 				mtx_unlock(&vi->tick_mtx);
2412 			}
2413 			PORT_LOCK(pi);
2414 			if (pi->up_vis > 0) {
2415 				t4_update_port_info(pi);
2416 				fixup_link_config(pi);
2417 				build_medialist(pi);
2418 				apply_link_config(pi);
2419 				if (pi->link_cfg.link_ok)
2420 					t4_os_link_changed(pi);
2421 			}
2422 			PORT_UNLOCK(pi);
2423 		}
2424 
2425 		/* Now reprogram the L2 multicast addresses. */
2426 		for_each_port(sc, i) {
2427 			pi = sc->port[i];
2428 			for_each_vi(pi, j, vi) {
2429 				if (!(vi->flags & VI_INIT_DONE))
2430 					continue;
2431 				ifp = vi->ifp;
2432 				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2433 					continue;
2434 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2435 				if (rc != 0) {
2436 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2437 					rc = 0;	/* carry on */
2438 				}
2439 			}
2440 		}
2441 	}
2442 
2443 	/* Reset all calibration */
2444 	t4_calibration_start(sc);
2445 
2446 done:
2447 	if (rc == 0) {
2448 		sc->incarnation++;
2449 		CH_ALERT(sc, "resume completed.\n");
2450 	}
2451 	end_synchronized_op(sc, 0);
2452 	free(old_state, M_CXGBE);
2453 	return (rc);
2454 }
2455 
2456 static int
t4_reset_prepare(device_t dev,device_t child)2457 t4_reset_prepare(device_t dev, device_t child)
2458 {
2459 	struct adapter *sc = device_get_softc(dev);
2460 
2461 	CH_ALERT(sc, "reset_prepare.\n");
2462 	return (0);
2463 }
2464 
2465 static int
t4_reset_post(device_t dev,device_t child)2466 t4_reset_post(device_t dev, device_t child)
2467 {
2468 	struct adapter *sc = device_get_softc(dev);
2469 
2470 	CH_ALERT(sc, "reset_post.\n");
2471 	return (0);
2472 }
2473 
2474 static int
reset_adapter(struct adapter * sc)2475 reset_adapter(struct adapter *sc)
2476 {
2477 	int rc, oldinc, error_flags;
2478 
2479 	CH_ALERT(sc, "reset requested.\n");
2480 
2481 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rst1");
2482 	if (rc != 0)
2483 		return (EBUSY);
2484 
2485 	if (hw_off_limits(sc)) {
2486 		CH_ERR(sc, "adapter is suspended, use resume (not reset).\n");
2487 		rc = ENXIO;
2488 		goto done;
2489 	}
2490 
2491 	if (!ok_to_reset(sc)) {
2492 		/* XXX: should list what resource is preventing reset. */
2493 		CH_ERR(sc, "not safe to reset.\n");
2494 		rc = EBUSY;
2495 		goto done;
2496 	}
2497 
2498 done:
2499 	oldinc = sc->incarnation;
2500 	end_synchronized_op(sc, 0);
2501 	if (rc != 0)
2502 		return (rc);	/* Error logged already. */
2503 
2504 	atomic_add_int(&sc->num_resets, 1);
2505 	mtx_lock(&Giant);
2506 	rc = BUS_RESET_CHILD(device_get_parent(sc->dev), sc->dev, 0);
2507 	mtx_unlock(&Giant);
2508 	if (rc != 0)
2509 		CH_ERR(sc, "bus_reset_child failed: %d.\n", rc);
2510 	else {
2511 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rst2");
2512 		if (rc != 0)
2513 			return (EBUSY);
2514 		error_flags = atomic_load_int(&sc->error_flags);
2515 		if (sc->incarnation > oldinc && error_flags == 0) {
2516 			CH_ALERT(sc, "bus_reset_child succeeded.\n");
2517 		} else {
2518 			CH_ERR(sc, "adapter did not reset properly, flags "
2519 			    "0x%08x, error_flags 0x%08x.\n", sc->flags,
2520 			    error_flags);
2521 			rc = ENXIO;
2522 		}
2523 		end_synchronized_op(sc, 0);
2524 	}
2525 
2526 	return (rc);
2527 }
2528 
2529 static void
reset_adapter_task(void * arg,int pending)2530 reset_adapter_task(void *arg, int pending)
2531 {
2532 	/* XXX: t4_async_event here? */
2533 	reset_adapter(arg);
2534 }
2535 
2536 static int
cxgbe_probe(device_t dev)2537 cxgbe_probe(device_t dev)
2538 {
2539 	char buf[128];
2540 	struct port_info *pi = device_get_softc(dev);
2541 
2542 	snprintf(buf, sizeof(buf), "port %d", pi->port_id);
2543 	device_set_desc_copy(dev, buf);
2544 
2545 	return (BUS_PROBE_DEFAULT);
2546 }
2547 
2548 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2549     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2550     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2551     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2552 #define T4_CAP_ENABLE (T4_CAP)
2553 
2554 static void
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2555 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2556 {
2557 	struct ifnet *ifp;
2558 	struct sbuf *sb;
2559 	struct sysctl_ctx_list *ctx = &vi->ctx;
2560 	struct sysctl_oid_list *children;
2561 	struct pfil_head_args pa;
2562 	struct adapter *sc = vi->adapter;
2563 
2564 	sysctl_ctx_init(ctx);
2565 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2566 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2567 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2568 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2569 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2570 #ifdef DEV_NETMAP
2571 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2572 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2573 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2574 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2575 #endif
2576 #ifdef TCP_OFFLOAD
2577 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2578 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2579 #endif
2580 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2581 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2582 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2583 #endif
2584 
2585 	vi->xact_addr_filt = -1;
2586 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2587 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2588 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2589 		vi->flags |= TX_USES_VM_WR;
2590 
2591 	/* Allocate an ifnet and set it up */
2592 	ifp = if_alloc_dev(IFT_ETHER, dev);
2593 	vi->ifp = ifp;
2594 	ifp->if_softc = vi;
2595 
2596 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2597 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2598 
2599 	ifp->if_init = cxgbe_init;
2600 	ifp->if_ioctl = cxgbe_ioctl;
2601 	ifp->if_transmit = cxgbe_transmit;
2602 	ifp->if_qflush = cxgbe_qflush;
2603 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2604 		ifp->if_get_counter = vi_get_counter;
2605 	else
2606 		ifp->if_get_counter = cxgbe_get_counter;
2607 #if defined(KERN_TLS) || defined(RATELIMIT)
2608 	ifp->if_snd_tag_alloc = cxgbe_snd_tag_alloc;
2609 	ifp->if_snd_tag_modify = cxgbe_snd_tag_modify;
2610 	ifp->if_snd_tag_query = cxgbe_snd_tag_query;
2611 	ifp->if_snd_tag_free = cxgbe_snd_tag_free;
2612 #endif
2613 #ifdef RATELIMIT
2614 	ifp->if_ratelimit_query = cxgbe_ratelimit_query;
2615 #endif
2616 
2617 	ifp->if_capabilities = T4_CAP;
2618 	ifp->if_capenable = T4_CAP_ENABLE;
2619 	ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2620 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6;
2621 	if (chip_id(sc) >= CHELSIO_T6) {
2622 		ifp->if_capabilities |= IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO;
2623 		ifp->if_capenable |= IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO;
2624 		ifp->if_hwassist |= CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2625 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2626 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN;
2627 	}
2628 
2629 #ifdef TCP_OFFLOAD
2630 	if (vi->nofldrxq != 0)
2631 		ifp->if_capabilities |= IFCAP_TOE;
2632 #endif
2633 #ifdef RATELIMIT
2634 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2635 		ifp->if_capabilities |= IFCAP_TXRTLMT;
2636 		ifp->if_capenable |= IFCAP_TXRTLMT;
2637 	}
2638 #endif
2639 
2640 	ifp->if_hw_tsomax = IP_MAXPACKET;
2641 	if (vi->flags & TX_USES_VM_WR)
2642 		ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_VM_TSO;
2643 	else
2644 		ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_TSO;
2645 #ifdef RATELIMIT
2646 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2647 		ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_EO_TSO;
2648 #endif
2649 	ifp->if_hw_tsomaxsegsize = 65536;
2650 #ifdef KERN_TLS
2651 	if (is_ktls(sc)) {
2652 		ifp->if_capabilities |= IFCAP_TXTLS;
2653 		if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2654 			ifp->if_capenable |= IFCAP_TXTLS;
2655 	}
2656 #endif
2657 
2658 	ether_ifattach(ifp, vi->hw_addr);
2659 #ifdef DEV_NETMAP
2660 	if (vi->nnmrxq != 0)
2661 		cxgbe_nm_attach(vi);
2662 #endif
2663 	sb = sbuf_new_auto();
2664 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2665 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2666 	switch (ifp->if_capabilities & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2667 	case IFCAP_TOE:
2668 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2669 		break;
2670 	case IFCAP_TOE | IFCAP_TXRTLMT:
2671 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2672 		break;
2673 	case IFCAP_TXRTLMT:
2674 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2675 		break;
2676 	}
2677 #endif
2678 #ifdef TCP_OFFLOAD
2679 	if (ifp->if_capabilities & IFCAP_TOE)
2680 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2681 #endif
2682 #ifdef DEV_NETMAP
2683 	if (ifp->if_capabilities & IFCAP_NETMAP)
2684 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2685 		    vi->nnmtxq, vi->nnmrxq);
2686 #endif
2687 	sbuf_finish(sb);
2688 	device_printf(dev, "%s\n", sbuf_data(sb));
2689 	sbuf_delete(sb);
2690 
2691 	vi_sysctls(vi);
2692 
2693 	pa.pa_version = PFIL_VERSION;
2694 	pa.pa_flags = PFIL_IN;
2695 	pa.pa_type = PFIL_TYPE_ETHERNET;
2696 	pa.pa_headname = ifp->if_xname;
2697 	vi->pfil = pfil_head_register(&pa);
2698 }
2699 
2700 static int
cxgbe_attach(device_t dev)2701 cxgbe_attach(device_t dev)
2702 {
2703 	struct port_info *pi = device_get_softc(dev);
2704 	struct adapter *sc = pi->adapter;
2705 	struct vi_info *vi;
2706 	int i;
2707 
2708 	sysctl_ctx_init(&pi->ctx);
2709 
2710 	cxgbe_vi_attach(dev, &pi->vi[0]);
2711 
2712 	for_each_vi(pi, i, vi) {
2713 		if (i == 0)
2714 			continue;
2715 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, -1);
2716 		if (vi->dev == NULL) {
2717 			device_printf(dev, "failed to add VI %d\n", i);
2718 			continue;
2719 		}
2720 		device_set_softc(vi->dev, vi);
2721 	}
2722 
2723 	cxgbe_sysctls(pi);
2724 
2725 	bus_generic_attach(dev);
2726 
2727 	return (0);
2728 }
2729 
2730 static void
cxgbe_vi_detach(struct vi_info * vi)2731 cxgbe_vi_detach(struct vi_info *vi)
2732 {
2733 	struct ifnet *ifp = vi->ifp;
2734 
2735 	if (vi->pfil != NULL) {
2736 		pfil_head_unregister(vi->pfil);
2737 		vi->pfil = NULL;
2738 	}
2739 
2740 	ether_ifdetach(ifp);
2741 
2742 	/* Let detach proceed even if these fail. */
2743 #ifdef DEV_NETMAP
2744 	if (ifp->if_capabilities & IFCAP_NETMAP)
2745 		cxgbe_nm_detach(vi);
2746 #endif
2747 	cxgbe_uninit_synchronized(vi);
2748 	callout_drain(&vi->tick);
2749 	mtx_destroy(&vi->tick_mtx);
2750 	sysctl_ctx_free(&vi->ctx);
2751 	vi_full_uninit(vi);
2752 
2753 	if_free(vi->ifp);
2754 	vi->ifp = NULL;
2755 }
2756 
2757 static int
cxgbe_detach(device_t dev)2758 cxgbe_detach(device_t dev)
2759 {
2760 	struct port_info *pi = device_get_softc(dev);
2761 	struct adapter *sc = pi->adapter;
2762 	int rc;
2763 
2764 	/* Detach the extra VIs first. */
2765 	rc = bus_generic_detach(dev);
2766 	if (rc)
2767 		return (rc);
2768 	device_delete_children(dev);
2769 
2770 	sysctl_ctx_free(&pi->ctx);
2771 	begin_vi_detach(sc, &pi->vi[0]);
2772 	if (pi->flags & HAS_TRACEQ) {
2773 		sc->traceq = -1;	/* cloner should not create ifnet */
2774 		t4_tracer_port_detach(sc);
2775 	}
2776 	cxgbe_vi_detach(&pi->vi[0]);
2777 	ifmedia_removeall(&pi->media);
2778 	end_vi_detach(sc, &pi->vi[0]);
2779 
2780 	return (0);
2781 }
2782 
2783 static void
cxgbe_init(void * arg)2784 cxgbe_init(void *arg)
2785 {
2786 	struct vi_info *vi = arg;
2787 	struct adapter *sc = vi->adapter;
2788 
2789 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
2790 		return;
2791 	cxgbe_init_synchronized(vi);
2792 	end_synchronized_op(sc, 0);
2793 }
2794 
2795 static int
cxgbe_ioctl(struct ifnet * ifp,unsigned long cmd,caddr_t data)2796 cxgbe_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
2797 {
2798 	int rc = 0, mtu, flags;
2799 	struct vi_info *vi = ifp->if_softc;
2800 	struct port_info *pi = vi->pi;
2801 	struct adapter *sc = pi->adapter;
2802 	struct ifreq *ifr = (struct ifreq *)data;
2803 	uint32_t mask;
2804 
2805 	switch (cmd) {
2806 	case SIOCSIFMTU:
2807 		mtu = ifr->ifr_mtu;
2808 		if (mtu < ETHERMIN || mtu > MAX_MTU)
2809 			return (EINVAL);
2810 
2811 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
2812 		if (rc)
2813 			return (rc);
2814 		ifp->if_mtu = mtu;
2815 		if (vi->flags & VI_INIT_DONE) {
2816 			t4_update_fl_bufsize(ifp);
2817 			if (!hw_off_limits(sc) &&
2818 			    ifp->if_drv_flags & IFF_DRV_RUNNING)
2819 				rc = update_mac_settings(ifp, XGMAC_MTU);
2820 		}
2821 		end_synchronized_op(sc, 0);
2822 		break;
2823 
2824 	case SIOCSIFFLAGS:
2825 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
2826 		if (rc)
2827 			return (rc);
2828 
2829 		if (hw_off_limits(sc)) {
2830 			rc = ENXIO;
2831 			goto fail;
2832 		}
2833 
2834 		if (ifp->if_flags & IFF_UP) {
2835 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2836 				flags = vi->if_flags;
2837 				if ((ifp->if_flags ^ flags) &
2838 				    (IFF_PROMISC | IFF_ALLMULTI)) {
2839 					rc = update_mac_settings(ifp,
2840 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
2841 				}
2842 			} else {
2843 				rc = cxgbe_init_synchronized(vi);
2844 			}
2845 			vi->if_flags = ifp->if_flags;
2846 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2847 			rc = cxgbe_uninit_synchronized(vi);
2848 		}
2849 		end_synchronized_op(sc, 0);
2850 		break;
2851 
2852 	case SIOCADDMULTI:
2853 	case SIOCDELMULTI:
2854 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
2855 		if (rc)
2856 			return (rc);
2857 		if (!hw_off_limits(sc) && ifp->if_drv_flags & IFF_DRV_RUNNING)
2858 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2859 		end_synchronized_op(sc, 0);
2860 		break;
2861 
2862 	case SIOCSIFCAP:
2863 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
2864 		if (rc)
2865 			return (rc);
2866 
2867 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2868 		if (mask & IFCAP_TXCSUM) {
2869 			ifp->if_capenable ^= IFCAP_TXCSUM;
2870 			ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2871 
2872 			if (IFCAP_TSO4 & ifp->if_capenable &&
2873 			    !(IFCAP_TXCSUM & ifp->if_capenable)) {
2874 				mask &= ~IFCAP_TSO4;
2875 				ifp->if_capenable &= ~IFCAP_TSO4;
2876 				if_printf(ifp,
2877 				    "tso4 disabled due to -txcsum.\n");
2878 			}
2879 		}
2880 		if (mask & IFCAP_TXCSUM_IPV6) {
2881 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
2882 			ifp->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2883 
2884 			if (IFCAP_TSO6 & ifp->if_capenable &&
2885 			    !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2886 				mask &= ~IFCAP_TSO6;
2887 				ifp->if_capenable &= ~IFCAP_TSO6;
2888 				if_printf(ifp,
2889 				    "tso6 disabled due to -txcsum6.\n");
2890 			}
2891 		}
2892 		if (mask & IFCAP_RXCSUM)
2893 			ifp->if_capenable ^= IFCAP_RXCSUM;
2894 		if (mask & IFCAP_RXCSUM_IPV6)
2895 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
2896 
2897 		/*
2898 		 * Note that we leave CSUM_TSO alone (it is always set).  The
2899 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
2900 		 * sending a TSO request our way, so it's sufficient to toggle
2901 		 * IFCAP_TSOx only.
2902 		 */
2903 		if (mask & IFCAP_TSO4) {
2904 			if (!(IFCAP_TSO4 & ifp->if_capenable) &&
2905 			    !(IFCAP_TXCSUM & ifp->if_capenable)) {
2906 				if_printf(ifp, "enable txcsum first.\n");
2907 				rc = EAGAIN;
2908 				goto fail;
2909 			}
2910 			ifp->if_capenable ^= IFCAP_TSO4;
2911 		}
2912 		if (mask & IFCAP_TSO6) {
2913 			if (!(IFCAP_TSO6 & ifp->if_capenable) &&
2914 			    !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2915 				if_printf(ifp, "enable txcsum6 first.\n");
2916 				rc = EAGAIN;
2917 				goto fail;
2918 			}
2919 			ifp->if_capenable ^= IFCAP_TSO6;
2920 		}
2921 		if (mask & IFCAP_LRO) {
2922 #if defined(INET) || defined(INET6)
2923 			int i;
2924 			struct sge_rxq *rxq;
2925 
2926 			ifp->if_capenable ^= IFCAP_LRO;
2927 			for_each_rxq(vi, i, rxq) {
2928 				if (ifp->if_capenable & IFCAP_LRO)
2929 					rxq->iq.flags |= IQ_LRO_ENABLED;
2930 				else
2931 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
2932 			}
2933 #endif
2934 		}
2935 #ifdef TCP_OFFLOAD
2936 		if (mask & IFCAP_TOE) {
2937 			int enable = (ifp->if_capenable ^ mask) & IFCAP_TOE;
2938 
2939 			rc = toe_capability(vi, enable);
2940 			if (rc != 0)
2941 				goto fail;
2942 
2943 			ifp->if_capenable ^= mask;
2944 		}
2945 #endif
2946 		if (mask & IFCAP_VLAN_HWTAGGING) {
2947 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2948 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2949 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
2950 		}
2951 		if (mask & IFCAP_VLAN_MTU) {
2952 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
2953 
2954 			/* Need to find out how to disable auto-mtu-inflation */
2955 		}
2956 		if (mask & IFCAP_VLAN_HWTSO)
2957 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
2958 		if (mask & IFCAP_VLAN_HWCSUM)
2959 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2960 #ifdef RATELIMIT
2961 		if (mask & IFCAP_TXRTLMT)
2962 			ifp->if_capenable ^= IFCAP_TXRTLMT;
2963 #endif
2964 		if (mask & IFCAP_HWRXTSTMP) {
2965 			int i;
2966 			struct sge_rxq *rxq;
2967 
2968 			ifp->if_capenable ^= IFCAP_HWRXTSTMP;
2969 			for_each_rxq(vi, i, rxq) {
2970 				if (ifp->if_capenable & IFCAP_HWRXTSTMP)
2971 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
2972 				else
2973 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
2974 			}
2975 		}
2976 		if (mask & IFCAP_MEXTPG)
2977 			ifp->if_capenable ^= IFCAP_MEXTPG;
2978 
2979 #ifdef KERN_TLS
2980 		if (mask & IFCAP_TXTLS) {
2981 			int enable = (ifp->if_capenable ^ mask) & IFCAP_TXTLS;
2982 
2983 			rc = ktls_capability(sc, enable);
2984 			if (rc != 0)
2985 				goto fail;
2986 
2987 			ifp->if_capenable ^= (mask & IFCAP_TXTLS);
2988 		}
2989 #endif
2990 		if (mask & IFCAP_VXLAN_HWCSUM) {
2991 			ifp->if_capenable ^= IFCAP_VXLAN_HWCSUM;
2992 			ifp->if_hwassist ^= CSUM_INNER_IP6_UDP |
2993 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
2994 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP;
2995 		}
2996 		if (mask & IFCAP_VXLAN_HWTSO) {
2997 			ifp->if_capenable ^= IFCAP_VXLAN_HWTSO;
2998 			ifp->if_hwassist ^= CSUM_INNER_IP6_TSO |
2999 			    CSUM_INNER_IP_TSO;
3000 		}
3001 
3002 #ifdef VLAN_CAPABILITIES
3003 		VLAN_CAPABILITIES(ifp);
3004 #endif
3005 fail:
3006 		end_synchronized_op(sc, 0);
3007 		break;
3008 
3009 	case SIOCSIFMEDIA:
3010 	case SIOCGIFMEDIA:
3011 	case SIOCGIFXMEDIA:
3012 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3013 		break;
3014 
3015 	case SIOCGI2C: {
3016 		struct ifi2creq i2c;
3017 
3018 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3019 		if (rc != 0)
3020 			break;
3021 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3022 			rc = EPERM;
3023 			break;
3024 		}
3025 		if (i2c.len > sizeof(i2c.data)) {
3026 			rc = EINVAL;
3027 			break;
3028 		}
3029 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3030 		if (rc)
3031 			return (rc);
3032 		if (hw_off_limits(sc))
3033 			rc = ENXIO;
3034 		else
3035 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3036 			    i2c.offset, i2c.len, &i2c.data[0]);
3037 		end_synchronized_op(sc, 0);
3038 		if (rc == 0)
3039 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3040 		break;
3041 	}
3042 
3043 	default:
3044 		rc = ether_ioctl(ifp, cmd, data);
3045 	}
3046 
3047 	return (rc);
3048 }
3049 
3050 static int
cxgbe_transmit(struct ifnet * ifp,struct mbuf * m)3051 cxgbe_transmit(struct ifnet *ifp, struct mbuf *m)
3052 {
3053 	struct vi_info *vi = ifp->if_softc;
3054 	struct port_info *pi = vi->pi;
3055 	struct adapter *sc;
3056 	struct sge_txq *txq;
3057 	void *items[1];
3058 	int rc;
3059 
3060 	M_ASSERTPKTHDR(m);
3061 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
3062 #if defined(KERN_TLS) || defined(RATELIMIT)
3063 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3064 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3065 #endif
3066 
3067 	if (__predict_false(pi->link_cfg.link_ok == false)) {
3068 		m_freem(m);
3069 		return (ENETDOWN);
3070 	}
3071 
3072 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3073 	if (__predict_false(rc != 0)) {
3074 		MPASS(m == NULL);			/* was freed already */
3075 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
3076 		return (rc);
3077 	}
3078 #ifdef RATELIMIT
3079 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
3080 		if (m->m_pkthdr.snd_tag->type == IF_SND_TAG_TYPE_RATE_LIMIT)
3081 			return (ethofld_transmit(ifp, m));
3082 	}
3083 #endif
3084 
3085 	/* Select a txq. */
3086 	sc = vi->adapter;
3087 	txq = &sc->sge.txq[vi->first_txq];
3088 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3089 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3090 		    vi->rsrv_noflowq);
3091 
3092 	items[0] = m;
3093 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3094 	if (__predict_false(rc != 0))
3095 		m_freem(m);
3096 
3097 	return (rc);
3098 }
3099 
3100 static void
cxgbe_qflush(struct ifnet * ifp)3101 cxgbe_qflush(struct ifnet *ifp)
3102 {
3103 	struct vi_info *vi = ifp->if_softc;
3104 	struct sge_txq *txq;
3105 	int i;
3106 
3107 	/* queues do not exist if !VI_INIT_DONE. */
3108 	if (vi->flags & VI_INIT_DONE) {
3109 		for_each_txq(vi, i, txq) {
3110 			TXQ_LOCK(txq);
3111 			txq->eq.flags |= EQ_QFLUSH;
3112 			TXQ_UNLOCK(txq);
3113 			while (!mp_ring_is_idle(txq->r)) {
3114 				mp_ring_check_drainage(txq->r, 4096);
3115 				pause("qflush", 1);
3116 			}
3117 			TXQ_LOCK(txq);
3118 			txq->eq.flags &= ~EQ_QFLUSH;
3119 			TXQ_UNLOCK(txq);
3120 		}
3121 	}
3122 	if_qflush(ifp);
3123 }
3124 
3125 static uint64_t
vi_get_counter(struct ifnet * ifp,ift_counter c)3126 vi_get_counter(struct ifnet *ifp, ift_counter c)
3127 {
3128 	struct vi_info *vi = ifp->if_softc;
3129 	struct fw_vi_stats_vf *s = &vi->stats;
3130 
3131 	mtx_lock(&vi->tick_mtx);
3132 	vi_refresh_stats(vi);
3133 	mtx_unlock(&vi->tick_mtx);
3134 
3135 	switch (c) {
3136 	case IFCOUNTER_IPACKETS:
3137 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3138 		    s->rx_ucast_frames);
3139 	case IFCOUNTER_IERRORS:
3140 		return (s->rx_err_frames);
3141 	case IFCOUNTER_OPACKETS:
3142 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3143 		    s->tx_ucast_frames + s->tx_offload_frames);
3144 	case IFCOUNTER_OERRORS:
3145 		return (s->tx_drop_frames);
3146 	case IFCOUNTER_IBYTES:
3147 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3148 		    s->rx_ucast_bytes);
3149 	case IFCOUNTER_OBYTES:
3150 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3151 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3152 	case IFCOUNTER_IMCASTS:
3153 		return (s->rx_mcast_frames);
3154 	case IFCOUNTER_OMCASTS:
3155 		return (s->tx_mcast_frames);
3156 	case IFCOUNTER_OQDROPS: {
3157 		uint64_t drops;
3158 
3159 		drops = 0;
3160 		if (vi->flags & VI_INIT_DONE) {
3161 			int i;
3162 			struct sge_txq *txq;
3163 
3164 			for_each_txq(vi, i, txq)
3165 				drops += counter_u64_fetch(txq->r->dropped);
3166 		}
3167 
3168 		return (drops);
3169 
3170 	}
3171 
3172 	default:
3173 		return (if_get_counter_default(ifp, c));
3174 	}
3175 }
3176 
3177 static uint64_t
cxgbe_get_counter(struct ifnet * ifp,ift_counter c)3178 cxgbe_get_counter(struct ifnet *ifp, ift_counter c)
3179 {
3180 	struct vi_info *vi = ifp->if_softc;
3181 	struct port_info *pi = vi->pi;
3182 	struct port_stats *s = &pi->stats;
3183 
3184 	mtx_lock(&vi->tick_mtx);
3185 	cxgbe_refresh_stats(vi);
3186 	mtx_unlock(&vi->tick_mtx);
3187 
3188 	switch (c) {
3189 	case IFCOUNTER_IPACKETS:
3190 		return (s->rx_frames);
3191 
3192 	case IFCOUNTER_IERRORS:
3193 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3194 		    s->rx_fcs_err + s->rx_len_err);
3195 
3196 	case IFCOUNTER_OPACKETS:
3197 		return (s->tx_frames);
3198 
3199 	case IFCOUNTER_OERRORS:
3200 		return (s->tx_error_frames);
3201 
3202 	case IFCOUNTER_IBYTES:
3203 		return (s->rx_octets);
3204 
3205 	case IFCOUNTER_OBYTES:
3206 		return (s->tx_octets);
3207 
3208 	case IFCOUNTER_IMCASTS:
3209 		return (s->rx_mcast_frames);
3210 
3211 	case IFCOUNTER_OMCASTS:
3212 		return (s->tx_mcast_frames);
3213 
3214 	case IFCOUNTER_IQDROPS:
3215 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3216 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3217 		    s->rx_trunc3 + pi->tnl_cong_drops);
3218 
3219 	case IFCOUNTER_OQDROPS: {
3220 		uint64_t drops;
3221 
3222 		drops = s->tx_drop;
3223 		if (vi->flags & VI_INIT_DONE) {
3224 			int i;
3225 			struct sge_txq *txq;
3226 
3227 			for_each_txq(vi, i, txq)
3228 				drops += counter_u64_fetch(txq->r->dropped);
3229 		}
3230 
3231 		return (drops);
3232 
3233 	}
3234 
3235 	default:
3236 		return (if_get_counter_default(ifp, c));
3237 	}
3238 }
3239 
3240 #if defined(KERN_TLS) || defined(RATELIMIT)
3241 static int
cxgbe_snd_tag_alloc(struct ifnet * ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3242 cxgbe_snd_tag_alloc(struct ifnet *ifp, union if_snd_tag_alloc_params *params,
3243     struct m_snd_tag **pt)
3244 {
3245 	int error;
3246 
3247 	switch (params->hdr.type) {
3248 #ifdef RATELIMIT
3249 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3250 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3251 		break;
3252 #endif
3253 #ifdef KERN_TLS
3254 	case IF_SND_TAG_TYPE_TLS:
3255 	{
3256 		struct vi_info *vi = ifp->if_softc;
3257 
3258 		if (is_t6(vi->pi->adapter))
3259 			error = t6_tls_tag_alloc(ifp, params, pt);
3260 		else
3261 			error = EOPNOTSUPP;
3262 		break;
3263 	}
3264 #endif
3265 	default:
3266 		error = EOPNOTSUPP;
3267 	}
3268 	return (error);
3269 }
3270 
3271 static int
cxgbe_snd_tag_modify(struct m_snd_tag * mst,union if_snd_tag_modify_params * params)3272 cxgbe_snd_tag_modify(struct m_snd_tag *mst,
3273     union if_snd_tag_modify_params *params)
3274 {
3275 
3276 	switch (mst->type) {
3277 #ifdef RATELIMIT
3278 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3279 		return (cxgbe_rate_tag_modify(mst, params));
3280 #endif
3281 	default:
3282 		return (EOPNOTSUPP);
3283 	}
3284 }
3285 
3286 static int
cxgbe_snd_tag_query(struct m_snd_tag * mst,union if_snd_tag_query_params * params)3287 cxgbe_snd_tag_query(struct m_snd_tag *mst,
3288     union if_snd_tag_query_params *params)
3289 {
3290 
3291 	switch (mst->type) {
3292 #ifdef RATELIMIT
3293 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3294 		return (cxgbe_rate_tag_query(mst, params));
3295 #endif
3296 	default:
3297 		return (EOPNOTSUPP);
3298 	}
3299 }
3300 
3301 static void
cxgbe_snd_tag_free(struct m_snd_tag * mst)3302 cxgbe_snd_tag_free(struct m_snd_tag *mst)
3303 {
3304 
3305 	switch (mst->type) {
3306 #ifdef RATELIMIT
3307 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3308 		cxgbe_rate_tag_free(mst);
3309 		return;
3310 #endif
3311 #ifdef KERN_TLS
3312 	case IF_SND_TAG_TYPE_TLS:
3313 		t6_tls_tag_free(mst);
3314 		return;
3315 #endif
3316 	default:
3317 		panic("shouldn't get here");
3318 	}
3319 }
3320 #endif
3321 
3322 /*
3323  * The kernel picks a media from the list we had provided but we still validate
3324  * the requeste.
3325  */
3326 int
cxgbe_media_change(struct ifnet * ifp)3327 cxgbe_media_change(struct ifnet *ifp)
3328 {
3329 	struct vi_info *vi = ifp->if_softc;
3330 	struct port_info *pi = vi->pi;
3331 	struct ifmedia *ifm = &pi->media;
3332 	struct link_config *lc = &pi->link_cfg;
3333 	struct adapter *sc = pi->adapter;
3334 	int rc;
3335 
3336 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3337 	if (rc != 0)
3338 		return (rc);
3339 	PORT_LOCK(pi);
3340 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3341 		/* ifconfig .. media autoselect */
3342 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3343 			rc = ENOTSUP; /* AN not supported by transceiver */
3344 			goto done;
3345 		}
3346 		lc->requested_aneg = AUTONEG_ENABLE;
3347 		lc->requested_speed = 0;
3348 		lc->requested_fc |= PAUSE_AUTONEG;
3349 	} else {
3350 		lc->requested_aneg = AUTONEG_DISABLE;
3351 		lc->requested_speed =
3352 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3353 		lc->requested_fc = 0;
3354 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3355 			lc->requested_fc |= PAUSE_RX;
3356 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3357 			lc->requested_fc |= PAUSE_TX;
3358 	}
3359 	if (pi->up_vis > 0 && !hw_off_limits(sc)) {
3360 		fixup_link_config(pi);
3361 		rc = apply_link_config(pi);
3362 	}
3363 done:
3364 	PORT_UNLOCK(pi);
3365 	end_synchronized_op(sc, 0);
3366 	return (rc);
3367 }
3368 
3369 /*
3370  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3371  * given speed.
3372  */
3373 static int
port_mword(struct port_info * pi,uint32_t speed)3374 port_mword(struct port_info *pi, uint32_t speed)
3375 {
3376 
3377 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3378 	MPASS(powerof2(speed));
3379 
3380 	switch(pi->port_type) {
3381 	case FW_PORT_TYPE_BT_SGMII:
3382 	case FW_PORT_TYPE_BT_XFI:
3383 	case FW_PORT_TYPE_BT_XAUI:
3384 		/* BaseT */
3385 		switch (speed) {
3386 		case FW_PORT_CAP32_SPEED_100M:
3387 			return (IFM_100_T);
3388 		case FW_PORT_CAP32_SPEED_1G:
3389 			return (IFM_1000_T);
3390 		case FW_PORT_CAP32_SPEED_10G:
3391 			return (IFM_10G_T);
3392 		}
3393 		break;
3394 	case FW_PORT_TYPE_KX4:
3395 		if (speed == FW_PORT_CAP32_SPEED_10G)
3396 			return (IFM_10G_KX4);
3397 		break;
3398 	case FW_PORT_TYPE_CX4:
3399 		if (speed == FW_PORT_CAP32_SPEED_10G)
3400 			return (IFM_10G_CX4);
3401 		break;
3402 	case FW_PORT_TYPE_KX:
3403 		if (speed == FW_PORT_CAP32_SPEED_1G)
3404 			return (IFM_1000_KX);
3405 		break;
3406 	case FW_PORT_TYPE_KR:
3407 	case FW_PORT_TYPE_BP_AP:
3408 	case FW_PORT_TYPE_BP4_AP:
3409 	case FW_PORT_TYPE_BP40_BA:
3410 	case FW_PORT_TYPE_KR4_100G:
3411 	case FW_PORT_TYPE_KR_SFP28:
3412 	case FW_PORT_TYPE_KR_XLAUI:
3413 		switch (speed) {
3414 		case FW_PORT_CAP32_SPEED_1G:
3415 			return (IFM_1000_KX);
3416 		case FW_PORT_CAP32_SPEED_10G:
3417 			return (IFM_10G_KR);
3418 		case FW_PORT_CAP32_SPEED_25G:
3419 			return (IFM_25G_KR);
3420 		case FW_PORT_CAP32_SPEED_40G:
3421 			return (IFM_40G_KR4);
3422 		case FW_PORT_CAP32_SPEED_50G:
3423 			return (IFM_50G_KR2);
3424 		case FW_PORT_CAP32_SPEED_100G:
3425 			return (IFM_100G_KR4);
3426 		}
3427 		break;
3428 	case FW_PORT_TYPE_FIBER_XFI:
3429 	case FW_PORT_TYPE_FIBER_XAUI:
3430 	case FW_PORT_TYPE_SFP:
3431 	case FW_PORT_TYPE_QSFP_10G:
3432 	case FW_PORT_TYPE_QSA:
3433 	case FW_PORT_TYPE_QSFP:
3434 	case FW_PORT_TYPE_CR4_QSFP:
3435 	case FW_PORT_TYPE_CR_QSFP:
3436 	case FW_PORT_TYPE_CR2_QSFP:
3437 	case FW_PORT_TYPE_SFP28:
3438 		/* Pluggable transceiver */
3439 		switch (pi->mod_type) {
3440 		case FW_PORT_MOD_TYPE_LR:
3441 			switch (speed) {
3442 			case FW_PORT_CAP32_SPEED_1G:
3443 				return (IFM_1000_LX);
3444 			case FW_PORT_CAP32_SPEED_10G:
3445 				return (IFM_10G_LR);
3446 			case FW_PORT_CAP32_SPEED_25G:
3447 				return (IFM_25G_LR);
3448 			case FW_PORT_CAP32_SPEED_40G:
3449 				return (IFM_40G_LR4);
3450 			case FW_PORT_CAP32_SPEED_50G:
3451 				return (IFM_50G_LR2);
3452 			case FW_PORT_CAP32_SPEED_100G:
3453 				return (IFM_100G_LR4);
3454 			}
3455 			break;
3456 		case FW_PORT_MOD_TYPE_SR:
3457 			switch (speed) {
3458 			case FW_PORT_CAP32_SPEED_1G:
3459 				return (IFM_1000_SX);
3460 			case FW_PORT_CAP32_SPEED_10G:
3461 				return (IFM_10G_SR);
3462 			case FW_PORT_CAP32_SPEED_25G:
3463 				return (IFM_25G_SR);
3464 			case FW_PORT_CAP32_SPEED_40G:
3465 				return (IFM_40G_SR4);
3466 			case FW_PORT_CAP32_SPEED_50G:
3467 				return (IFM_50G_SR2);
3468 			case FW_PORT_CAP32_SPEED_100G:
3469 				return (IFM_100G_SR4);
3470 			}
3471 			break;
3472 		case FW_PORT_MOD_TYPE_ER:
3473 			if (speed == FW_PORT_CAP32_SPEED_10G)
3474 				return (IFM_10G_ER);
3475 			break;
3476 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3477 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3478 			switch (speed) {
3479 			case FW_PORT_CAP32_SPEED_1G:
3480 				return (IFM_1000_CX);
3481 			case FW_PORT_CAP32_SPEED_10G:
3482 				return (IFM_10G_TWINAX);
3483 			case FW_PORT_CAP32_SPEED_25G:
3484 				return (IFM_25G_CR);
3485 			case FW_PORT_CAP32_SPEED_40G:
3486 				return (IFM_40G_CR4);
3487 			case FW_PORT_CAP32_SPEED_50G:
3488 				return (IFM_50G_CR2);
3489 			case FW_PORT_CAP32_SPEED_100G:
3490 				return (IFM_100G_CR4);
3491 			}
3492 			break;
3493 		case FW_PORT_MOD_TYPE_LRM:
3494 			if (speed == FW_PORT_CAP32_SPEED_10G)
3495 				return (IFM_10G_LRM);
3496 			break;
3497 		case FW_PORT_MOD_TYPE_NA:
3498 			MPASS(0);	/* Not pluggable? */
3499 			/* fall throough */
3500 		case FW_PORT_MOD_TYPE_ERROR:
3501 		case FW_PORT_MOD_TYPE_UNKNOWN:
3502 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3503 			break;
3504 		case FW_PORT_MOD_TYPE_NONE:
3505 			return (IFM_NONE);
3506 		}
3507 		break;
3508 	case FW_PORT_TYPE_NONE:
3509 		return (IFM_NONE);
3510 	}
3511 
3512 	return (IFM_UNKNOWN);
3513 }
3514 
3515 void
cxgbe_media_status(struct ifnet * ifp,struct ifmediareq * ifmr)3516 cxgbe_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
3517 {
3518 	struct vi_info *vi = ifp->if_softc;
3519 	struct port_info *pi = vi->pi;
3520 	struct adapter *sc = pi->adapter;
3521 	struct link_config *lc = &pi->link_cfg;
3522 
3523 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4med") != 0)
3524 		return;
3525 	PORT_LOCK(pi);
3526 
3527 	if (pi->up_vis == 0 && !hw_off_limits(sc)) {
3528 		/*
3529 		 * If all the interfaces are administratively down the firmware
3530 		 * does not report transceiver changes.  Refresh port info here
3531 		 * so that ifconfig displays accurate ifmedia at all times.
3532 		 * This is the only reason we have a synchronized op in this
3533 		 * function.  Just PORT_LOCK would have been enough otherwise.
3534 		 */
3535 		t4_update_port_info(pi);
3536 		build_medialist(pi);
3537 	}
3538 
3539 	/* ifm_status */
3540 	ifmr->ifm_status = IFM_AVALID;
3541 	if (lc->link_ok == false)
3542 		goto done;
3543 	ifmr->ifm_status |= IFM_ACTIVE;
3544 
3545 	/* ifm_active */
3546 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3547 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3548 	if (lc->fc & PAUSE_RX)
3549 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3550 	if (lc->fc & PAUSE_TX)
3551 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3552 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3553 done:
3554 	PORT_UNLOCK(pi);
3555 	end_synchronized_op(sc, 0);
3556 }
3557 
3558 static int
vcxgbe_probe(device_t dev)3559 vcxgbe_probe(device_t dev)
3560 {
3561 	char buf[128];
3562 	struct vi_info *vi = device_get_softc(dev);
3563 
3564 	snprintf(buf, sizeof(buf), "port %d vi %td", vi->pi->port_id,
3565 	    vi - vi->pi->vi);
3566 	device_set_desc_copy(dev, buf);
3567 
3568 	return (BUS_PROBE_DEFAULT);
3569 }
3570 
3571 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3572 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3573 {
3574 	int func, index, rc;
3575 	uint32_t param, val;
3576 
3577 	ASSERT_SYNCHRONIZED_OP(sc);
3578 
3579 	index = vi - pi->vi;
3580 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3581 	KASSERT(index < nitems(vi_mac_funcs),
3582 	    ("%s: VI %s doesn't have a MAC func", __func__,
3583 	    device_get_nameunit(vi->dev)));
3584 	func = vi_mac_funcs[index];
3585 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
3586 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3587 	if (rc < 0) {
3588 		CH_ERR(vi, "failed to allocate virtual interface %d"
3589 		    "for port %d: %d\n", index, pi->port_id, -rc);
3590 		return (-rc);
3591 	}
3592 	vi->viid = rc;
3593 
3594 	if (vi->rss_size == 1) {
3595 		/*
3596 		 * This VI didn't get a slice of the RSS table.  Reduce the
3597 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3598 		 * configuration file (nvi, rssnvi for this PF) if this is a
3599 		 * problem.
3600 		 */
3601 		device_printf(vi->dev, "RSS table not available.\n");
3602 		vi->rss_base = 0xffff;
3603 
3604 		return (0);
3605 	}
3606 
3607 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3608 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3609 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3610 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3611 	if (rc)
3612 		vi->rss_base = 0xffff;
3613 	else {
3614 		MPASS((val >> 16) == vi->rss_size);
3615 		vi->rss_base = val & 0xffff;
3616 	}
3617 
3618 	return (0);
3619 }
3620 
3621 static int
vcxgbe_attach(device_t dev)3622 vcxgbe_attach(device_t dev)
3623 {
3624 	struct vi_info *vi;
3625 	struct port_info *pi;
3626 	struct adapter *sc;
3627 	int rc;
3628 
3629 	vi = device_get_softc(dev);
3630 	pi = vi->pi;
3631 	sc = pi->adapter;
3632 
3633 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3634 	if (rc)
3635 		return (rc);
3636 	rc = alloc_extra_vi(sc, pi, vi);
3637 	end_synchronized_op(sc, 0);
3638 	if (rc)
3639 		return (rc);
3640 
3641 	cxgbe_vi_attach(dev, vi);
3642 
3643 	return (0);
3644 }
3645 
3646 static int
vcxgbe_detach(device_t dev)3647 vcxgbe_detach(device_t dev)
3648 {
3649 	struct vi_info *vi;
3650 	struct adapter *sc;
3651 
3652 	vi = device_get_softc(dev);
3653 	sc = vi->adapter;
3654 
3655 	begin_vi_detach(sc, vi);
3656 	cxgbe_vi_detach(vi);
3657 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3658 	end_vi_detach(sc, vi);
3659 
3660 	return (0);
3661 }
3662 
3663 static struct callout fatal_callout;
3664 static struct taskqueue *reset_tq;
3665 
3666 static void
delayed_panic(void * arg)3667 delayed_panic(void *arg)
3668 {
3669 	struct adapter *sc = arg;
3670 
3671 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3672 }
3673 
3674 static void
fatal_error_task(void * arg,int pending)3675 fatal_error_task(void *arg, int pending)
3676 {
3677 	struct adapter *sc = arg;
3678 	int rc;
3679 
3680 #ifdef TCP_OFFLOAD
3681 	t4_async_event(sc);
3682 #endif
3683 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3684 		dump_cim_regs(sc);
3685 		dump_cimla(sc);
3686 		dump_devlog(sc);
3687 	}
3688 
3689 	if (t4_reset_on_fatal_err) {
3690 		CH_ALERT(sc, "resetting on fatal error.\n");
3691 		rc = reset_adapter(sc);
3692 		if (rc == 0 && t4_panic_on_fatal_err) {
3693 			CH_ALERT(sc, "reset was successful, "
3694 			    "system will NOT panic.\n");
3695 			return;
3696 		}
3697 	}
3698 
3699 	if (t4_panic_on_fatal_err) {
3700 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3701 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3702 	}
3703 }
3704 
3705 void
t4_fatal_err(struct adapter * sc,bool fw_error)3706 t4_fatal_err(struct adapter *sc, bool fw_error)
3707 {
3708 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3709 
3710 	stop_adapter(sc);
3711 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3712 		return;
3713 	if (fw_error) {
3714 		/*
3715 		 * We are here because of a firmware error/timeout and not
3716 		 * because of a hardware interrupt.  It is possible (although
3717 		 * not very likely) that an error interrupt was also raised but
3718 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3719 		 * main INT_CAUSE registers here to make sure we haven't missed
3720 		 * anything interesting.
3721 		 */
3722 		t4_slow_intr_handler(sc, verbose);
3723 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3724 	}
3725 	t4_report_fw_error(sc);
3726 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3727 	    device_get_nameunit(sc->dev), fw_error);
3728 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3729 }
3730 
3731 void
t4_add_adapter(struct adapter * sc)3732 t4_add_adapter(struct adapter *sc)
3733 {
3734 	sx_xlock(&t4_list_lock);
3735 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3736 	sx_xunlock(&t4_list_lock);
3737 }
3738 
3739 int
t4_map_bars_0_and_4(struct adapter * sc)3740 t4_map_bars_0_and_4(struct adapter *sc)
3741 {
3742 	sc->regs_rid = PCIR_BAR(0);
3743 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3744 	    &sc->regs_rid, RF_ACTIVE);
3745 	if (sc->regs_res == NULL) {
3746 		device_printf(sc->dev, "cannot map registers.\n");
3747 		return (ENXIO);
3748 	}
3749 	sc->bt = rman_get_bustag(sc->regs_res);
3750 	sc->bh = rman_get_bushandle(sc->regs_res);
3751 	sc->mmio_len = rman_get_size(sc->regs_res);
3752 	setbit(&sc->doorbells, DOORBELL_KDB);
3753 
3754 	sc->msix_rid = PCIR_BAR(4);
3755 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3756 	    &sc->msix_rid, RF_ACTIVE);
3757 	if (sc->msix_res == NULL) {
3758 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3759 		return (ENXIO);
3760 	}
3761 
3762 	return (0);
3763 }
3764 
3765 int
t4_map_bar_2(struct adapter * sc)3766 t4_map_bar_2(struct adapter *sc)
3767 {
3768 
3769 	/*
3770 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
3771 	 * to map it if RDMA is disabled.
3772 	 */
3773 	if (is_t4(sc) && sc->rdmacaps == 0)
3774 		return (0);
3775 
3776 	sc->udbs_rid = PCIR_BAR(2);
3777 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3778 	    &sc->udbs_rid, RF_ACTIVE);
3779 	if (sc->udbs_res == NULL) {
3780 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
3781 		return (ENXIO);
3782 	}
3783 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
3784 
3785 	if (chip_id(sc) >= CHELSIO_T5) {
3786 		setbit(&sc->doorbells, DOORBELL_UDB);
3787 #if defined(__i386__) || defined(__amd64__)
3788 		if (t5_write_combine) {
3789 			int rc, mode;
3790 
3791 			/*
3792 			 * Enable write combining on BAR2.  This is the
3793 			 * userspace doorbell BAR and is split into 128B
3794 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
3795 			 * with an egress queue.  The first 64B has the doorbell
3796 			 * and the second 64B can be used to submit a tx work
3797 			 * request with an implicit doorbell.
3798 			 */
3799 
3800 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
3801 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
3802 			if (rc == 0) {
3803 				clrbit(&sc->doorbells, DOORBELL_UDB);
3804 				setbit(&sc->doorbells, DOORBELL_WCWR);
3805 				setbit(&sc->doorbells, DOORBELL_UDBWC);
3806 			} else {
3807 				device_printf(sc->dev,
3808 				    "couldn't enable write combining: %d\n",
3809 				    rc);
3810 			}
3811 
3812 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
3813 			t4_write_reg(sc, A_SGE_STAT_CFG,
3814 			    V_STATSOURCE_T5(7) | mode);
3815 		}
3816 #endif
3817 	}
3818 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
3819 
3820 	return (0);
3821 }
3822 
3823 int
t4_adj_doorbells(struct adapter * sc)3824 t4_adj_doorbells(struct adapter *sc)
3825 {
3826 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
3827 		sc->doorbells &= t4_doorbells_allowed;
3828 		return (0);
3829 	}
3830 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
3831 	       sc->doorbells, t4_doorbells_allowed);
3832 	return (EINVAL);
3833 }
3834 
3835 struct memwin_init {
3836 	uint32_t base;
3837 	uint32_t aperture;
3838 };
3839 
3840 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
3841 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3842 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3843 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
3844 };
3845 
3846 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
3847 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3848 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3849 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
3850 };
3851 
3852 static void
setup_memwin(struct adapter * sc)3853 setup_memwin(struct adapter *sc)
3854 {
3855 	const struct memwin_init *mw_init;
3856 	struct memwin *mw;
3857 	int i;
3858 	uint32_t bar0;
3859 
3860 	if (is_t4(sc)) {
3861 		/*
3862 		 * Read low 32b of bar0 indirectly via the hardware backdoor
3863 		 * mechanism.  Works from within PCI passthrough environments
3864 		 * too, where rman_get_start() can return a different value.  We
3865 		 * need to program the T4 memory window decoders with the actual
3866 		 * addresses that will be coming across the PCIe link.
3867 		 */
3868 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
3869 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
3870 
3871 		mw_init = &t4_memwin[0];
3872 	} else {
3873 		/* T5+ use the relative offset inside the PCIe BAR */
3874 		bar0 = 0;
3875 
3876 		mw_init = &t5_memwin[0];
3877 	}
3878 
3879 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
3880 		if (!rw_initialized(&mw->mw_lock)) {
3881 			rw_init(&mw->mw_lock, "memory window access");
3882 			mw->mw_base = mw_init->base;
3883 			mw->mw_aperture = mw_init->aperture;
3884 			mw->mw_curpos = 0;
3885 		}
3886 		t4_write_reg(sc,
3887 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
3888 		    (mw->mw_base + bar0) | V_BIR(0) |
3889 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
3890 		rw_wlock(&mw->mw_lock);
3891 		position_memwin(sc, i, mw->mw_curpos);
3892 		rw_wunlock(&mw->mw_lock);
3893 	}
3894 
3895 	/* flush */
3896 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
3897 }
3898 
3899 /*
3900  * Positions the memory window at the given address in the card's address space.
3901  * There are some alignment requirements and the actual position may be at an
3902  * address prior to the requested address.  mw->mw_curpos always has the actual
3903  * position of the window.
3904  */
3905 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)3906 position_memwin(struct adapter *sc, int idx, uint32_t addr)
3907 {
3908 	struct memwin *mw;
3909 	uint32_t pf;
3910 	uint32_t reg;
3911 
3912 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3913 	mw = &sc->memwin[idx];
3914 	rw_assert(&mw->mw_lock, RA_WLOCKED);
3915 
3916 	if (is_t4(sc)) {
3917 		pf = 0;
3918 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
3919 	} else {
3920 		pf = V_PFNUM(sc->pf);
3921 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
3922 	}
3923 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
3924 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
3925 	t4_read_reg(sc, reg);	/* flush */
3926 }
3927 
3928 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)3929 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
3930     int len, int rw)
3931 {
3932 	struct memwin *mw;
3933 	uint32_t mw_end, v;
3934 
3935 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3936 
3937 	/* Memory can only be accessed in naturally aligned 4 byte units */
3938 	if (addr & 3 || len & 3 || len <= 0)
3939 		return (EINVAL);
3940 
3941 	mw = &sc->memwin[idx];
3942 	while (len > 0) {
3943 		rw_rlock(&mw->mw_lock);
3944 		mw_end = mw->mw_curpos + mw->mw_aperture;
3945 		if (addr >= mw_end || addr < mw->mw_curpos) {
3946 			/* Will need to reposition the window */
3947 			if (!rw_try_upgrade(&mw->mw_lock)) {
3948 				rw_runlock(&mw->mw_lock);
3949 				rw_wlock(&mw->mw_lock);
3950 			}
3951 			rw_assert(&mw->mw_lock, RA_WLOCKED);
3952 			position_memwin(sc, idx, addr);
3953 			rw_downgrade(&mw->mw_lock);
3954 			mw_end = mw->mw_curpos + mw->mw_aperture;
3955 		}
3956 		rw_assert(&mw->mw_lock, RA_RLOCKED);
3957 		while (addr < mw_end && len > 0) {
3958 			if (rw == 0) {
3959 				v = t4_read_reg(sc, mw->mw_base + addr -
3960 				    mw->mw_curpos);
3961 				*val++ = le32toh(v);
3962 			} else {
3963 				v = *val++;
3964 				t4_write_reg(sc, mw->mw_base + addr -
3965 				    mw->mw_curpos, htole32(v));
3966 			}
3967 			addr += 4;
3968 			len -= 4;
3969 		}
3970 		rw_runlock(&mw->mw_lock);
3971 	}
3972 
3973 	return (0);
3974 }
3975 
3976 CTASSERT(M_TID_COOKIE == M_COOKIE);
3977 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
3978 
3979 static void
t4_init_atid_table(struct adapter * sc)3980 t4_init_atid_table(struct adapter *sc)
3981 {
3982 	struct tid_info *t;
3983 	int i;
3984 
3985 	t = &sc->tids;
3986 	if (t->natids == 0)
3987 		return;
3988 
3989 	MPASS(t->atid_tab == NULL);
3990 
3991 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
3992 	    M_ZERO | M_WAITOK);
3993 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
3994 	t->afree = t->atid_tab;
3995 	t->atids_in_use = 0;
3996 	for (i = 1; i < t->natids; i++)
3997 		t->atid_tab[i - 1].next = &t->atid_tab[i];
3998 	t->atid_tab[t->natids - 1].next = NULL;
3999 }
4000 
4001 static void
t4_free_atid_table(struct adapter * sc)4002 t4_free_atid_table(struct adapter *sc)
4003 {
4004 	struct tid_info *t;
4005 
4006 	t = &sc->tids;
4007 
4008 	KASSERT(t->atids_in_use == 0,
4009 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4010 
4011 	if (mtx_initialized(&t->atid_lock))
4012 		mtx_destroy(&t->atid_lock);
4013 	free(t->atid_tab, M_CXGBE);
4014 	t->atid_tab = NULL;
4015 }
4016 
4017 int
alloc_atid(struct adapter * sc,void * ctx)4018 alloc_atid(struct adapter *sc, void *ctx)
4019 {
4020 	struct tid_info *t = &sc->tids;
4021 	int atid = -1;
4022 
4023 	mtx_lock(&t->atid_lock);
4024 	if (t->afree) {
4025 		union aopen_entry *p = t->afree;
4026 
4027 		atid = p - t->atid_tab;
4028 		MPASS(atid <= M_TID_TID);
4029 		t->afree = p->next;
4030 		p->data = ctx;
4031 		t->atids_in_use++;
4032 	}
4033 	mtx_unlock(&t->atid_lock);
4034 	return (atid);
4035 }
4036 
4037 void *
lookup_atid(struct adapter * sc,int atid)4038 lookup_atid(struct adapter *sc, int atid)
4039 {
4040 	struct tid_info *t = &sc->tids;
4041 
4042 	return (t->atid_tab[atid].data);
4043 }
4044 
4045 void
free_atid(struct adapter * sc,int atid)4046 free_atid(struct adapter *sc, int atid)
4047 {
4048 	struct tid_info *t = &sc->tids;
4049 	union aopen_entry *p = &t->atid_tab[atid];
4050 
4051 	mtx_lock(&t->atid_lock);
4052 	p->next = t->afree;
4053 	t->afree = p;
4054 	t->atids_in_use--;
4055 	mtx_unlock(&t->atid_lock);
4056 }
4057 
4058 static void
queue_tid_release(struct adapter * sc,int tid)4059 queue_tid_release(struct adapter *sc, int tid)
4060 {
4061 
4062 	CXGBE_UNIMPLEMENTED("deferred tid release");
4063 }
4064 
4065 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4066 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4067 {
4068 	struct wrqe *wr;
4069 	struct cpl_tid_release *req;
4070 
4071 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4072 	if (wr == NULL) {
4073 		queue_tid_release(sc, tid);	/* defer */
4074 		return;
4075 	}
4076 	req = wrtod(wr);
4077 
4078 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4079 
4080 	t4_wrq_tx(sc, wr);
4081 }
4082 
4083 static int
t4_range_cmp(const void * a,const void * b)4084 t4_range_cmp(const void *a, const void *b)
4085 {
4086 	return ((const struct t4_range *)a)->start -
4087 	       ((const struct t4_range *)b)->start;
4088 }
4089 
4090 /*
4091  * Verify that the memory range specified by the addr/len pair is valid within
4092  * the card's address space.
4093  */
4094 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4095 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4096 {
4097 	struct t4_range mem_ranges[4], *r, *next;
4098 	uint32_t em, addr_len;
4099 	int i, n, remaining;
4100 
4101 	/* Memory can only be accessed in naturally aligned 4 byte units */
4102 	if (addr & 3 || len & 3 || len == 0)
4103 		return (EINVAL);
4104 
4105 	/* Enabled memories */
4106 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4107 
4108 	r = &mem_ranges[0];
4109 	n = 0;
4110 	bzero(r, sizeof(mem_ranges));
4111 	if (em & F_EDRAM0_ENABLE) {
4112 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4113 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4114 		if (r->size > 0) {
4115 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4116 			if (addr >= r->start &&
4117 			    addr + len <= r->start + r->size)
4118 				return (0);
4119 			r++;
4120 			n++;
4121 		}
4122 	}
4123 	if (em & F_EDRAM1_ENABLE) {
4124 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4125 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4126 		if (r->size > 0) {
4127 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4128 			if (addr >= r->start &&
4129 			    addr + len <= r->start + r->size)
4130 				return (0);
4131 			r++;
4132 			n++;
4133 		}
4134 	}
4135 	if (em & F_EXT_MEM_ENABLE) {
4136 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4137 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4138 		if (r->size > 0) {
4139 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4140 			if (addr >= r->start &&
4141 			    addr + len <= r->start + r->size)
4142 				return (0);
4143 			r++;
4144 			n++;
4145 		}
4146 	}
4147 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4148 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4149 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4150 		if (r->size > 0) {
4151 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4152 			if (addr >= r->start &&
4153 			    addr + len <= r->start + r->size)
4154 				return (0);
4155 			r++;
4156 			n++;
4157 		}
4158 	}
4159 	MPASS(n <= nitems(mem_ranges));
4160 
4161 	if (n > 1) {
4162 		/* Sort and merge the ranges. */
4163 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4164 
4165 		/* Start from index 0 and examine the next n - 1 entries. */
4166 		r = &mem_ranges[0];
4167 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4168 
4169 			MPASS(r->size > 0);	/* r is a valid entry. */
4170 			next = r + 1;
4171 			MPASS(next->size > 0);	/* and so is the next one. */
4172 
4173 			while (r->start + r->size >= next->start) {
4174 				/* Merge the next one into the current entry. */
4175 				r->size = max(r->start + r->size,
4176 				    next->start + next->size) - r->start;
4177 				n--;	/* One fewer entry in total. */
4178 				if (--remaining == 0)
4179 					goto done;	/* short circuit */
4180 				next++;
4181 			}
4182 			if (next != r + 1) {
4183 				/*
4184 				 * Some entries were merged into r and next
4185 				 * points to the first valid entry that couldn't
4186 				 * be merged.
4187 				 */
4188 				MPASS(next->size > 0);	/* must be valid */
4189 				memcpy(r + 1, next, remaining * sizeof(*r));
4190 #ifdef INVARIANTS
4191 				/*
4192 				 * This so that the foo->size assertion in the
4193 				 * next iteration of the loop do the right
4194 				 * thing for entries that were pulled up and are
4195 				 * no longer valid.
4196 				 */
4197 				MPASS(n < nitems(mem_ranges));
4198 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4199 				    sizeof(struct t4_range));
4200 #endif
4201 			}
4202 		}
4203 done:
4204 		/* Done merging the ranges. */
4205 		MPASS(n > 0);
4206 		r = &mem_ranges[0];
4207 		for (i = 0; i < n; i++, r++) {
4208 			if (addr >= r->start &&
4209 			    addr + len <= r->start + r->size)
4210 				return (0);
4211 		}
4212 	}
4213 
4214 	return (EFAULT);
4215 }
4216 
4217 static int
fwmtype_to_hwmtype(int mtype)4218 fwmtype_to_hwmtype(int mtype)
4219 {
4220 
4221 	switch (mtype) {
4222 	case FW_MEMTYPE_EDC0:
4223 		return (MEM_EDC0);
4224 	case FW_MEMTYPE_EDC1:
4225 		return (MEM_EDC1);
4226 	case FW_MEMTYPE_EXTMEM:
4227 		return (MEM_MC0);
4228 	case FW_MEMTYPE_EXTMEM1:
4229 		return (MEM_MC1);
4230 	default:
4231 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4232 	}
4233 }
4234 
4235 /*
4236  * Verify that the memory range specified by the memtype/offset/len pair is
4237  * valid and lies entirely within the memtype specified.  The global address of
4238  * the start of the range is returned in addr.
4239  */
4240 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4241 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4242     uint32_t *addr)
4243 {
4244 	uint32_t em, addr_len, maddr;
4245 
4246 	/* Memory can only be accessed in naturally aligned 4 byte units */
4247 	if (off & 3 || len & 3 || len == 0)
4248 		return (EINVAL);
4249 
4250 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4251 	switch (fwmtype_to_hwmtype(mtype)) {
4252 	case MEM_EDC0:
4253 		if (!(em & F_EDRAM0_ENABLE))
4254 			return (EINVAL);
4255 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4256 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4257 		break;
4258 	case MEM_EDC1:
4259 		if (!(em & F_EDRAM1_ENABLE))
4260 			return (EINVAL);
4261 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4262 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4263 		break;
4264 	case MEM_MC:
4265 		if (!(em & F_EXT_MEM_ENABLE))
4266 			return (EINVAL);
4267 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4268 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4269 		break;
4270 	case MEM_MC1:
4271 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4272 			return (EINVAL);
4273 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4274 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4275 		break;
4276 	default:
4277 		return (EINVAL);
4278 	}
4279 
4280 	*addr = maddr + off;	/* global address */
4281 	return (validate_mem_range(sc, *addr, len));
4282 }
4283 
4284 static int
fixup_devlog_params(struct adapter * sc)4285 fixup_devlog_params(struct adapter *sc)
4286 {
4287 	struct devlog_params *dparams = &sc->params.devlog;
4288 	int rc;
4289 
4290 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4291 	    dparams->size, &dparams->addr);
4292 
4293 	return (rc);
4294 }
4295 
4296 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4297 update_nirq(struct intrs_and_queues *iaq, int nports)
4298 {
4299 
4300 	iaq->nirq = T4_EXTRA_INTR;
4301 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4302 	iaq->nirq += nports * iaq->nofldrxq;
4303 	iaq->nirq += nports * (iaq->num_vis - 1) *
4304 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4305 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4306 }
4307 
4308 /*
4309  * Adjust requirements to fit the number of interrupts available.
4310  */
4311 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4312 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4313     int navail)
4314 {
4315 	int old_nirq;
4316 	const int nports = sc->params.nports;
4317 
4318 	MPASS(nports > 0);
4319 	MPASS(navail > 0);
4320 
4321 	bzero(iaq, sizeof(*iaq));
4322 	iaq->intr_type = itype;
4323 	iaq->num_vis = t4_num_vis;
4324 	iaq->ntxq = t4_ntxq;
4325 	iaq->ntxq_vi = t4_ntxq_vi;
4326 	iaq->nrxq = t4_nrxq;
4327 	iaq->nrxq_vi = t4_nrxq_vi;
4328 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4329 	if (is_offload(sc) || is_ethoffload(sc)) {
4330 		iaq->nofldtxq = t4_nofldtxq;
4331 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
4332 	}
4333 #endif
4334 #ifdef TCP_OFFLOAD
4335 	if (is_offload(sc)) {
4336 		iaq->nofldrxq = t4_nofldrxq;
4337 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4338 	}
4339 #endif
4340 #ifdef DEV_NETMAP
4341 	if (t4_native_netmap & NN_MAIN_VI) {
4342 		iaq->nnmtxq = t4_nnmtxq;
4343 		iaq->nnmrxq = t4_nnmrxq;
4344 	}
4345 	if (t4_native_netmap & NN_EXTRA_VI) {
4346 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4347 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4348 	}
4349 #endif
4350 
4351 	update_nirq(iaq, nports);
4352 	if (iaq->nirq <= navail &&
4353 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4354 		/*
4355 		 * This is the normal case -- there are enough interrupts for
4356 		 * everything.
4357 		 */
4358 		goto done;
4359 	}
4360 
4361 	/*
4362 	 * If extra VIs have been configured try reducing their count and see if
4363 	 * that works.
4364 	 */
4365 	while (iaq->num_vis > 1) {
4366 		iaq->num_vis--;
4367 		update_nirq(iaq, nports);
4368 		if (iaq->nirq <= navail &&
4369 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4370 			device_printf(sc->dev, "virtual interfaces per port "
4371 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4372 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4373 			    "itype %d, navail %u, nirq %d.\n",
4374 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4375 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4376 			    itype, navail, iaq->nirq);
4377 			goto done;
4378 		}
4379 	}
4380 
4381 	/*
4382 	 * Extra VIs will not be created.  Log a message if they were requested.
4383 	 */
4384 	MPASS(iaq->num_vis == 1);
4385 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4386 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4387 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4388 	if (iaq->num_vis != t4_num_vis) {
4389 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4390 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4391 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4392 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4393 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4394 	}
4395 
4396 	/*
4397 	 * Keep reducing the number of NIC rx queues to the next lower power of
4398 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4399 	 * if that works.
4400 	 */
4401 	do {
4402 		if (iaq->nrxq > 1) {
4403 			do {
4404 				iaq->nrxq--;
4405 			} while (!powerof2(iaq->nrxq));
4406 			if (iaq->nnmrxq > iaq->nrxq)
4407 				iaq->nnmrxq = iaq->nrxq;
4408 		}
4409 		if (iaq->nofldrxq > 1)
4410 			iaq->nofldrxq >>= 1;
4411 
4412 		old_nirq = iaq->nirq;
4413 		update_nirq(iaq, nports);
4414 		if (iaq->nirq <= navail &&
4415 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4416 			device_printf(sc->dev, "running with reduced number of "
4417 			    "rx queues because of shortage of interrupts.  "
4418 			    "nrxq=%u, nofldrxq=%u.  "
4419 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4420 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4421 			goto done;
4422 		}
4423 	} while (old_nirq != iaq->nirq);
4424 
4425 	/* One interrupt for everything.  Ugh. */
4426 	device_printf(sc->dev, "running with minimal number of queues.  "
4427 	    "itype %d, navail %u.\n", itype, navail);
4428 	iaq->nirq = 1;
4429 	iaq->nrxq = 1;
4430 	iaq->ntxq = 1;
4431 	if (iaq->nofldrxq > 0) {
4432 		iaq->nofldrxq = 1;
4433 		iaq->nofldtxq = 1;
4434 	}
4435 	iaq->nnmtxq = 0;
4436 	iaq->nnmrxq = 0;
4437 done:
4438 	MPASS(iaq->num_vis > 0);
4439 	if (iaq->num_vis > 1) {
4440 		MPASS(iaq->nrxq_vi > 0);
4441 		MPASS(iaq->ntxq_vi > 0);
4442 	}
4443 	MPASS(iaq->nirq > 0);
4444 	MPASS(iaq->nrxq > 0);
4445 	MPASS(iaq->ntxq > 0);
4446 	if (itype == INTR_MSI) {
4447 		MPASS(powerof2(iaq->nirq));
4448 	}
4449 }
4450 
4451 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4452 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4453 {
4454 	int rc, itype, navail, nalloc;
4455 
4456 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4457 
4458 		if ((itype & t4_intr_types) == 0)
4459 			continue;	/* not allowed */
4460 
4461 		if (itype == INTR_MSIX)
4462 			navail = pci_msix_count(sc->dev);
4463 		else if (itype == INTR_MSI)
4464 			navail = pci_msi_count(sc->dev);
4465 		else
4466 			navail = 1;
4467 restart:
4468 		if (navail == 0)
4469 			continue;
4470 
4471 		calculate_iaq(sc, iaq, itype, navail);
4472 		nalloc = iaq->nirq;
4473 		rc = 0;
4474 		if (itype == INTR_MSIX)
4475 			rc = pci_alloc_msix(sc->dev, &nalloc);
4476 		else if (itype == INTR_MSI)
4477 			rc = pci_alloc_msi(sc->dev, &nalloc);
4478 
4479 		if (rc == 0 && nalloc > 0) {
4480 			if (nalloc == iaq->nirq)
4481 				return (0);
4482 
4483 			/*
4484 			 * Didn't get the number requested.  Use whatever number
4485 			 * the kernel is willing to allocate.
4486 			 */
4487 			device_printf(sc->dev, "fewer vectors than requested, "
4488 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4489 			    itype, iaq->nirq, nalloc);
4490 			pci_release_msi(sc->dev);
4491 			navail = nalloc;
4492 			goto restart;
4493 		}
4494 
4495 		device_printf(sc->dev,
4496 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4497 		    itype, rc, iaq->nirq, nalloc);
4498 	}
4499 
4500 	device_printf(sc->dev,
4501 	    "failed to find a usable interrupt type.  "
4502 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4503 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4504 
4505 	return (ENXIO);
4506 }
4507 
4508 #define FW_VERSION(chip) ( \
4509     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4510     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4511     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4512     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4513 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4514 
4515 /* Just enough of fw_hdr to cover all version info. */
4516 struct fw_h {
4517 	__u8	ver;
4518 	__u8	chip;
4519 	__be16	len512;
4520 	__be32	fw_ver;
4521 	__be32	tp_microcode_ver;
4522 	__u8	intfver_nic;
4523 	__u8	intfver_vnic;
4524 	__u8	intfver_ofld;
4525 	__u8	intfver_ri;
4526 	__u8	intfver_iscsipdu;
4527 	__u8	intfver_iscsi;
4528 	__u8	intfver_fcoepdu;
4529 	__u8	intfver_fcoe;
4530 };
4531 /* Spot check a couple of fields. */
4532 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4533 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4534 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4535 
4536 struct fw_info {
4537 	uint8_t chip;
4538 	char *kld_name;
4539 	char *fw_mod_name;
4540 	struct fw_h fw_h;
4541 } fw_info[] = {
4542 	{
4543 		.chip = CHELSIO_T4,
4544 		.kld_name = "t4fw_cfg",
4545 		.fw_mod_name = "t4fw",
4546 		.fw_h = {
4547 			.chip = FW_HDR_CHIP_T4,
4548 			.fw_ver = htobe32(FW_VERSION(T4)),
4549 			.intfver_nic = FW_INTFVER(T4, NIC),
4550 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4551 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4552 			.intfver_ri = FW_INTFVER(T4, RI),
4553 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4554 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4555 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4556 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4557 		},
4558 	}, {
4559 		.chip = CHELSIO_T5,
4560 		.kld_name = "t5fw_cfg",
4561 		.fw_mod_name = "t5fw",
4562 		.fw_h = {
4563 			.chip = FW_HDR_CHIP_T5,
4564 			.fw_ver = htobe32(FW_VERSION(T5)),
4565 			.intfver_nic = FW_INTFVER(T5, NIC),
4566 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4567 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4568 			.intfver_ri = FW_INTFVER(T5, RI),
4569 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4570 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4571 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4572 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4573 		},
4574 	}, {
4575 		.chip = CHELSIO_T6,
4576 		.kld_name = "t6fw_cfg",
4577 		.fw_mod_name = "t6fw",
4578 		.fw_h = {
4579 			.chip = FW_HDR_CHIP_T6,
4580 			.fw_ver = htobe32(FW_VERSION(T6)),
4581 			.intfver_nic = FW_INTFVER(T6, NIC),
4582 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4583 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4584 			.intfver_ri = FW_INTFVER(T6, RI),
4585 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4586 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4587 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4588 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4589 		},
4590 	}
4591 };
4592 
4593 static struct fw_info *
find_fw_info(int chip)4594 find_fw_info(int chip)
4595 {
4596 	int i;
4597 
4598 	for (i = 0; i < nitems(fw_info); i++) {
4599 		if (fw_info[i].chip == chip)
4600 			return (&fw_info[i]);
4601 	}
4602 	return (NULL);
4603 }
4604 
4605 /*
4606  * Is the given firmware API compatible with the one the driver was compiled
4607  * with?
4608  */
4609 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4610 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4611 {
4612 
4613 	/* short circuit if it's the exact same firmware version */
4614 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4615 		return (1);
4616 
4617 	/*
4618 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4619 	 * features that are supported in the driver.
4620 	 */
4621 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4622 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4623 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4624 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4625 		return (1);
4626 #undef SAME_INTF
4627 
4628 	return (0);
4629 }
4630 
4631 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4632 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4633     const struct firmware **fw)
4634 {
4635 	struct fw_info *fw_info;
4636 
4637 	*dcfg = NULL;
4638 	if (fw != NULL)
4639 		*fw = NULL;
4640 
4641 	fw_info = find_fw_info(chip_id(sc));
4642 	if (fw_info == NULL) {
4643 		device_printf(sc->dev,
4644 		    "unable to look up firmware information for chip %d.\n",
4645 		    chip_id(sc));
4646 		return (EINVAL);
4647 	}
4648 
4649 	*dcfg = firmware_get(fw_info->kld_name);
4650 	if (*dcfg != NULL) {
4651 		if (fw != NULL)
4652 			*fw = firmware_get(fw_info->fw_mod_name);
4653 		return (0);
4654 	}
4655 
4656 	return (ENOENT);
4657 }
4658 
4659 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4660 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4661     const struct firmware *fw)
4662 {
4663 
4664 	if (fw != NULL)
4665 		firmware_put(fw, FIRMWARE_UNLOAD);
4666 	if (dcfg != NULL)
4667 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4668 }
4669 
4670 /*
4671  * Return values:
4672  * 0 means no firmware install attempted.
4673  * ERESTART means a firmware install was attempted and was successful.
4674  * +ve errno means a firmware install was attempted but failed.
4675  */
4676 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4677 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4678     const struct fw_h *drv_fw, const char *reason, int *already)
4679 {
4680 	const struct firmware *cfg, *fw;
4681 	const uint32_t c = be32toh(card_fw->fw_ver);
4682 	uint32_t d, k;
4683 	int rc, fw_install;
4684 	struct fw_h bundled_fw;
4685 	bool load_attempted;
4686 
4687 	cfg = fw = NULL;
4688 	load_attempted = false;
4689 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4690 
4691 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4692 	if (t4_fw_install < 0) {
4693 		rc = load_fw_module(sc, &cfg, &fw);
4694 		if (rc != 0 || fw == NULL) {
4695 			device_printf(sc->dev,
4696 			    "failed to load firmware module: %d. cfg %p, fw %p;"
4697 			    " will use compiled-in firmware version for"
4698 			    "hw.cxgbe.fw_install checks.\n",
4699 			    rc, cfg, fw);
4700 		} else {
4701 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
4702 		}
4703 		load_attempted = true;
4704 	}
4705 	d = be32toh(bundled_fw.fw_ver);
4706 
4707 	if (reason != NULL)
4708 		goto install;
4709 
4710 	if ((sc->flags & FW_OK) == 0) {
4711 
4712 		if (c == 0xffffffff) {
4713 			reason = "missing";
4714 			goto install;
4715 		}
4716 
4717 		rc = 0;
4718 		goto done;
4719 	}
4720 
4721 	if (!fw_compatible(card_fw, &bundled_fw)) {
4722 		reason = "incompatible or unusable";
4723 		goto install;
4724 	}
4725 
4726 	if (d > c) {
4727 		reason = "older than the version bundled with this driver";
4728 		goto install;
4729 	}
4730 
4731 	if (fw_install == 2 && d != c) {
4732 		reason = "different than the version bundled with this driver";
4733 		goto install;
4734 	}
4735 
4736 	/* No reason to do anything to the firmware already on the card. */
4737 	rc = 0;
4738 	goto done;
4739 
4740 install:
4741 	rc = 0;
4742 	if ((*already)++)
4743 		goto done;
4744 
4745 	if (fw_install == 0) {
4746 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4747 		    "but the driver is prohibited from installing a firmware "
4748 		    "on the card.\n",
4749 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4750 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4751 
4752 		goto done;
4753 	}
4754 
4755 	/*
4756 	 * We'll attempt to install a firmware.  Load the module first (if it
4757 	 * hasn't been loaded already).
4758 	 */
4759 	if (!load_attempted) {
4760 		rc = load_fw_module(sc, &cfg, &fw);
4761 		if (rc != 0 || fw == NULL) {
4762 			device_printf(sc->dev,
4763 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
4764 			    rc, cfg, fw);
4765 			/* carry on */
4766 		}
4767 	}
4768 	if (fw == NULL) {
4769 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4770 		    "but the driver cannot take corrective action because it "
4771 		    "is unable to load the firmware module.\n",
4772 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4773 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4774 		rc = sc->flags & FW_OK ? 0 : ENOENT;
4775 		goto done;
4776 	}
4777 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
4778 	if (k != d) {
4779 		MPASS(t4_fw_install > 0);
4780 		device_printf(sc->dev,
4781 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
4782 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
4783 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
4784 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
4785 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4786 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4787 		rc = sc->flags & FW_OK ? 0 : EINVAL;
4788 		goto done;
4789 	}
4790 
4791 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4792 	    "installing firmware %u.%u.%u.%u on card.\n",
4793 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4794 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
4795 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4796 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4797 
4798 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
4799 	if (rc != 0) {
4800 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
4801 	} else {
4802 		/* Installed successfully, update the cached header too. */
4803 		rc = ERESTART;
4804 		memcpy(card_fw, fw->data, sizeof(*card_fw));
4805 	}
4806 done:
4807 	unload_fw_module(sc, cfg, fw);
4808 
4809 	return (rc);
4810 }
4811 
4812 /*
4813  * Establish contact with the firmware and attempt to become the master driver.
4814  *
4815  * A firmware will be installed to the card if needed (if the driver is allowed
4816  * to do so).
4817  */
4818 static int
contact_firmware(struct adapter * sc)4819 contact_firmware(struct adapter *sc)
4820 {
4821 	int rc, already = 0;
4822 	enum dev_state state;
4823 	struct fw_info *fw_info;
4824 	struct fw_hdr *card_fw;		/* fw on the card */
4825 	const struct fw_h *drv_fw;
4826 
4827 	fw_info = find_fw_info(chip_id(sc));
4828 	if (fw_info == NULL) {
4829 		device_printf(sc->dev,
4830 		    "unable to look up firmware information for chip %d.\n",
4831 		    chip_id(sc));
4832 		return (EINVAL);
4833 	}
4834 	drv_fw = &fw_info->fw_h;
4835 
4836 	/* Read the header of the firmware on the card */
4837 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
4838 restart:
4839 	rc = -t4_get_fw_hdr(sc, card_fw);
4840 	if (rc != 0) {
4841 		device_printf(sc->dev,
4842 		    "unable to read firmware header from card's flash: %d\n",
4843 		    rc);
4844 		goto done;
4845 	}
4846 
4847 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
4848 	    &already);
4849 	if (rc == ERESTART)
4850 		goto restart;
4851 	if (rc != 0)
4852 		goto done;
4853 
4854 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
4855 	if (rc < 0 || state == DEV_STATE_ERR) {
4856 		rc = -rc;
4857 		device_printf(sc->dev,
4858 		    "failed to connect to the firmware: %d, %d.  "
4859 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4860 #if 0
4861 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4862 		    "not responding properly to HELLO", &already) == ERESTART)
4863 			goto restart;
4864 #endif
4865 		goto done;
4866 	}
4867 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
4868 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
4869 
4870 	if (rc == sc->pf) {
4871 		sc->flags |= MASTER_PF;
4872 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4873 		    NULL, &already);
4874 		if (rc == ERESTART)
4875 			rc = 0;
4876 		else if (rc != 0)
4877 			goto done;
4878 	} else if (state == DEV_STATE_UNINIT) {
4879 		/*
4880 		 * We didn't get to be the master so we definitely won't be
4881 		 * configuring the chip.  It's a bug if someone else hasn't
4882 		 * configured it already.
4883 		 */
4884 		device_printf(sc->dev, "couldn't be master(%d), "
4885 		    "device not already initialized either(%d).  "
4886 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4887 		rc = EPROTO;
4888 		goto done;
4889 	} else {
4890 		/*
4891 		 * Some other PF is the master and has configured the chip.
4892 		 * This is allowed but untested.
4893 		 */
4894 		device_printf(sc->dev, "PF%d is master, device state %d.  "
4895 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4896 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
4897 		sc->cfcsum = 0;
4898 		rc = 0;
4899 	}
4900 done:
4901 	if (rc != 0 && sc->flags & FW_OK) {
4902 		t4_fw_bye(sc, sc->mbox);
4903 		sc->flags &= ~FW_OK;
4904 	}
4905 	free(card_fw, M_CXGBE);
4906 	return (rc);
4907 }
4908 
4909 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff)4910 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
4911     uint32_t mtype, uint32_t moff)
4912 {
4913 	struct fw_info *fw_info;
4914 	const struct firmware *dcfg, *rcfg = NULL;
4915 	const uint32_t *cfdata;
4916 	uint32_t cflen, addr;
4917 	int rc;
4918 
4919 	load_fw_module(sc, &dcfg, NULL);
4920 
4921 	/* Card specific interpretation of "default". */
4922 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4923 		if (pci_get_device(sc->dev) == 0x440a)
4924 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
4925 		if (is_fpga(sc))
4926 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
4927 	}
4928 
4929 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4930 		if (dcfg == NULL) {
4931 			device_printf(sc->dev,
4932 			    "KLD with default config is not available.\n");
4933 			rc = ENOENT;
4934 			goto done;
4935 		}
4936 		cfdata = dcfg->data;
4937 		cflen = dcfg->datasize & ~3;
4938 	} else {
4939 		char s[32];
4940 
4941 		fw_info = find_fw_info(chip_id(sc));
4942 		if (fw_info == NULL) {
4943 			device_printf(sc->dev,
4944 			    "unable to look up firmware information for chip %d.\n",
4945 			    chip_id(sc));
4946 			rc = EINVAL;
4947 			goto done;
4948 		}
4949 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
4950 
4951 		rcfg = firmware_get(s);
4952 		if (rcfg == NULL) {
4953 			device_printf(sc->dev,
4954 			    "unable to load module \"%s\" for configuration "
4955 			    "profile \"%s\".\n", s, cfg_file);
4956 			rc = ENOENT;
4957 			goto done;
4958 		}
4959 		cfdata = rcfg->data;
4960 		cflen = rcfg->datasize & ~3;
4961 	}
4962 
4963 	if (cflen > FLASH_CFG_MAX_SIZE) {
4964 		device_printf(sc->dev,
4965 		    "config file too long (%d, max allowed is %d).\n",
4966 		    cflen, FLASH_CFG_MAX_SIZE);
4967 		rc = EINVAL;
4968 		goto done;
4969 	}
4970 
4971 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
4972 	if (rc != 0) {
4973 		device_printf(sc->dev,
4974 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
4975 		    __func__, mtype, moff, cflen, rc);
4976 		rc = EINVAL;
4977 		goto done;
4978 	}
4979 	write_via_memwin(sc, 2, addr, cfdata, cflen);
4980 done:
4981 	if (rcfg != NULL)
4982 		firmware_put(rcfg, FIRMWARE_UNLOAD);
4983 	unload_fw_module(sc, dcfg, NULL);
4984 	return (rc);
4985 }
4986 
4987 struct caps_allowed {
4988 	uint16_t nbmcaps;
4989 	uint16_t linkcaps;
4990 	uint16_t switchcaps;
4991 	uint16_t niccaps;
4992 	uint16_t toecaps;
4993 	uint16_t rdmacaps;
4994 	uint16_t cryptocaps;
4995 	uint16_t iscsicaps;
4996 	uint16_t fcoecaps;
4997 };
4998 
4999 #define FW_PARAM_DEV(param) \
5000 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5001 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5002 #define FW_PARAM_PFVF(param) \
5003 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5004 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5005 
5006 /*
5007  * Provide a configuration profile to the firmware and have it initialize the
5008  * chip accordingly.  This may involve uploading a configuration file to the
5009  * card.
5010  */
5011 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5012 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5013     const struct caps_allowed *caps_allowed)
5014 {
5015 	int rc;
5016 	struct fw_caps_config_cmd caps;
5017 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5018 
5019 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5020 	if (rc != 0) {
5021 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5022 		return (rc);
5023 	}
5024 
5025 	bzero(&caps, sizeof(caps));
5026 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5027 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5028 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5029 		mtype = 0;
5030 		moff = 0;
5031 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5032 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5033 		mtype = FW_MEMTYPE_FLASH;
5034 		moff = t4_flash_cfg_addr(sc);
5035 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5036 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5037 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5038 		    FW_LEN16(caps));
5039 	} else {
5040 		/*
5041 		 * Ask the firmware where it wants us to upload the config file.
5042 		 */
5043 		param = FW_PARAM_DEV(CF);
5044 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5045 		if (rc != 0) {
5046 			/* No support for config file?  Shouldn't happen. */
5047 			device_printf(sc->dev,
5048 			    "failed to query config file location: %d.\n", rc);
5049 			goto done;
5050 		}
5051 		mtype = G_FW_PARAMS_PARAM_Y(val);
5052 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5053 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5054 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5055 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5056 		    FW_LEN16(caps));
5057 
5058 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
5059 		if (rc != 0) {
5060 			device_printf(sc->dev,
5061 			    "failed to upload config file to card: %d.\n", rc);
5062 			goto done;
5063 		}
5064 	}
5065 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5066 	if (rc != 0) {
5067 		device_printf(sc->dev, "failed to pre-process config file: %d "
5068 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5069 		goto done;
5070 	}
5071 
5072 	finicsum = be32toh(caps.finicsum);
5073 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5074 	if (finicsum != cfcsum) {
5075 		device_printf(sc->dev,
5076 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5077 		    finicsum, cfcsum);
5078 	}
5079 	sc->cfcsum = cfcsum;
5080 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5081 
5082 	/*
5083 	 * Let the firmware know what features will (not) be used so it can tune
5084 	 * things accordingly.
5085 	 */
5086 #define LIMIT_CAPS(x) do { \
5087 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5088 } while (0)
5089 	LIMIT_CAPS(nbm);
5090 	LIMIT_CAPS(link);
5091 	LIMIT_CAPS(switch);
5092 	LIMIT_CAPS(nic);
5093 	LIMIT_CAPS(toe);
5094 	LIMIT_CAPS(rdma);
5095 	LIMIT_CAPS(crypto);
5096 	LIMIT_CAPS(iscsi);
5097 	LIMIT_CAPS(fcoe);
5098 #undef LIMIT_CAPS
5099 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5100 		/*
5101 		 * TOE and hashfilters are mutually exclusive.  It is a config
5102 		 * file or firmware bug if both are reported as available.  Try
5103 		 * to cope with the situation in non-debug builds by disabling
5104 		 * TOE.
5105 		 */
5106 		MPASS(caps.toecaps == 0);
5107 
5108 		caps.toecaps = 0;
5109 		caps.rdmacaps = 0;
5110 		caps.iscsicaps = 0;
5111 	}
5112 
5113 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5114 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5115 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5116 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5117 	if (rc != 0) {
5118 		device_printf(sc->dev,
5119 		    "failed to process config file: %d.\n", rc);
5120 		goto done;
5121 	}
5122 
5123 	t4_tweak_chip_settings(sc);
5124 	set_params__pre_init(sc);
5125 
5126 	/* get basic stuff going */
5127 	rc = -t4_fw_initialize(sc, sc->mbox);
5128 	if (rc != 0) {
5129 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5130 		goto done;
5131 	}
5132 done:
5133 	return (rc);
5134 }
5135 
5136 /*
5137  * Partition chip resources for use between various PFs, VFs, etc.
5138  */
5139 static int
partition_resources(struct adapter * sc)5140 partition_resources(struct adapter *sc)
5141 {
5142 	char cfg_file[sizeof(t4_cfg_file)];
5143 	struct caps_allowed caps_allowed;
5144 	int rc;
5145 	bool fallback;
5146 
5147 	/* Only the master driver gets to configure the chip resources. */
5148 	MPASS(sc->flags & MASTER_PF);
5149 
5150 #define COPY_CAPS(x) do { \
5151 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5152 } while (0)
5153 	bzero(&caps_allowed, sizeof(caps_allowed));
5154 	COPY_CAPS(nbm);
5155 	COPY_CAPS(link);
5156 	COPY_CAPS(switch);
5157 	COPY_CAPS(nic);
5158 	COPY_CAPS(toe);
5159 	COPY_CAPS(rdma);
5160 	COPY_CAPS(crypto);
5161 	COPY_CAPS(iscsi);
5162 	COPY_CAPS(fcoe);
5163 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5164 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5165 retry:
5166 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5167 	if (rc != 0 && fallback) {
5168 		dump_devlog(sc);
5169 		device_printf(sc->dev,
5170 		    "failed (%d) to configure card with \"%s\" profile, "
5171 		    "will fall back to a basic configuration and retry.\n",
5172 		    rc, cfg_file);
5173 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5174 		bzero(&caps_allowed, sizeof(caps_allowed));
5175 		COPY_CAPS(switch);
5176 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5177 		fallback = false;
5178 		goto retry;
5179 	}
5180 #undef COPY_CAPS
5181 	return (rc);
5182 }
5183 
5184 /*
5185  * Retrieve parameters that are needed (or nice to have) very early.
5186  */
5187 static int
get_params__pre_init(struct adapter * sc)5188 get_params__pre_init(struct adapter *sc)
5189 {
5190 	int rc;
5191 	uint32_t param[2], val[2];
5192 
5193 	t4_get_version_info(sc);
5194 
5195 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5196 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5197 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5198 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5199 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5200 
5201 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5202 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5203 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5204 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5205 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5206 
5207 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5208 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5209 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5210 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5211 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5212 
5213 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5214 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5215 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5216 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5217 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5218 
5219 	param[0] = FW_PARAM_DEV(PORTVEC);
5220 	param[1] = FW_PARAM_DEV(CCLK);
5221 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5222 	if (rc != 0) {
5223 		device_printf(sc->dev,
5224 		    "failed to query parameters (pre_init): %d.\n", rc);
5225 		return (rc);
5226 	}
5227 
5228 	sc->params.portvec = val[0];
5229 	sc->params.nports = bitcount32(val[0]);
5230 	sc->params.vpd.cclk = val[1];
5231 
5232 	/* Read device log parameters. */
5233 	rc = -t4_init_devlog_params(sc, 1);
5234 	if (rc == 0)
5235 		fixup_devlog_params(sc);
5236 	else {
5237 		device_printf(sc->dev,
5238 		    "failed to get devlog parameters: %d.\n", rc);
5239 		rc = 0;	/* devlog isn't critical for device operation */
5240 	}
5241 
5242 	return (rc);
5243 }
5244 
5245 /*
5246  * Any params that need to be set before FW_INITIALIZE.
5247  */
5248 static int
set_params__pre_init(struct adapter * sc)5249 set_params__pre_init(struct adapter *sc)
5250 {
5251 	int rc = 0;
5252 	uint32_t param, val;
5253 
5254 	if (chip_id(sc) >= CHELSIO_T6) {
5255 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5256 		val = 1;
5257 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5258 		/* firmwares < 1.20.1.0 do not have this param. */
5259 		if (rc == FW_EINVAL &&
5260 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5261 			rc = 0;
5262 		}
5263 		if (rc != 0) {
5264 			device_printf(sc->dev,
5265 			    "failed to enable high priority filters :%d.\n",
5266 			    rc);
5267 		}
5268 
5269 		param = FW_PARAM_DEV(PPOD_EDRAM);
5270 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5271 		if (rc == 0 && val == 1) {
5272 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5273 			    &val);
5274 			if (rc != 0) {
5275 				device_printf(sc->dev,
5276 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5277 			}
5278 		}
5279 	}
5280 
5281 	/* Enable opaque VIIDs with firmwares that support it. */
5282 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5283 	val = 1;
5284 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5285 	if (rc == 0 && val == 1)
5286 		sc->params.viid_smt_extn_support = true;
5287 	else
5288 		sc->params.viid_smt_extn_support = false;
5289 
5290 	return (rc);
5291 }
5292 
5293 /*
5294  * Retrieve various parameters that are of interest to the driver.  The device
5295  * has been initialized by the firmware at this point.
5296  */
5297 static int
get_params__post_init(struct adapter * sc)5298 get_params__post_init(struct adapter *sc)
5299 {
5300 	int rc;
5301 	uint32_t param[7], val[7];
5302 	struct fw_caps_config_cmd caps;
5303 
5304 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5305 	param[1] = FW_PARAM_PFVF(EQ_START);
5306 	param[2] = FW_PARAM_PFVF(FILTER_START);
5307 	param[3] = FW_PARAM_PFVF(FILTER_END);
5308 	param[4] = FW_PARAM_PFVF(L2T_START);
5309 	param[5] = FW_PARAM_PFVF(L2T_END);
5310 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5311 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5312 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5313 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5314 	if (rc != 0) {
5315 		device_printf(sc->dev,
5316 		    "failed to query parameters (post_init): %d.\n", rc);
5317 		return (rc);
5318 	}
5319 
5320 	sc->sge.iq_start = val[0];
5321 	sc->sge.eq_start = val[1];
5322 	if ((int)val[3] > (int)val[2]) {
5323 		sc->tids.ftid_base = val[2];
5324 		sc->tids.ftid_end = val[3];
5325 		sc->tids.nftids = val[3] - val[2] + 1;
5326 	}
5327 	sc->vres.l2t.start = val[4];
5328 	sc->vres.l2t.size = val[5] - val[4] + 1;
5329 	KASSERT(sc->vres.l2t.size <= L2T_SIZE,
5330 	    ("%s: L2 table size (%u) larger than expected (%u)",
5331 	    __func__, sc->vres.l2t.size, L2T_SIZE));
5332 	sc->params.core_vdd = val[6];
5333 
5334 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5335 	param[1] = FW_PARAM_PFVF(EQ_END);
5336 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5337 	if (rc != 0) {
5338 		device_printf(sc->dev,
5339 		    "failed to query parameters (post_init2): %d.\n", rc);
5340 		return (rc);
5341 	}
5342 	MPASS((int)val[0] >= sc->sge.iq_start);
5343 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5344 	MPASS((int)val[1] >= sc->sge.eq_start);
5345 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5346 
5347 	if (chip_id(sc) >= CHELSIO_T6) {
5348 
5349 		sc->tids.tid_base = t4_read_reg(sc,
5350 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5351 
5352 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5353 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5354 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5355 		if (rc != 0) {
5356 			device_printf(sc->dev,
5357 			   "failed to query hpfilter parameters: %d.\n", rc);
5358 			return (rc);
5359 		}
5360 		if ((int)val[1] > (int)val[0]) {
5361 			sc->tids.hpftid_base = val[0];
5362 			sc->tids.hpftid_end = val[1];
5363 			sc->tids.nhpftids = val[1] - val[0] + 1;
5364 
5365 			/*
5366 			 * These should go off if the layout changes and the
5367 			 * driver needs to catch up.
5368 			 */
5369 			MPASS(sc->tids.hpftid_base == 0);
5370 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5371 		}
5372 
5373 		param[0] = FW_PARAM_PFVF(RAWF_START);
5374 		param[1] = FW_PARAM_PFVF(RAWF_END);
5375 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5376 		if (rc != 0) {
5377 			device_printf(sc->dev,
5378 			   "failed to query rawf parameters: %d.\n", rc);
5379 			return (rc);
5380 		}
5381 		if ((int)val[1] > (int)val[0]) {
5382 			sc->rawf_base = val[0];
5383 			sc->nrawf = val[1] - val[0] + 1;
5384 		}
5385 	}
5386 
5387 	/*
5388 	 * The parameters that follow may not be available on all firmwares.  We
5389 	 * query them individually rather than in a compound query because old
5390 	 * firmwares fail the entire query if an unknown parameter is queried.
5391 	 */
5392 
5393 	/*
5394 	 * MPS buffer group configuration.
5395 	 */
5396 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5397 	val[0] = 0;
5398 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5399 	if (rc == 0)
5400 		sc->params.mps_bg_map = val[0];
5401 	else
5402 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5403 
5404 	param[0] = FW_PARAM_DEV(TPCHMAP);
5405 	val[0] = 0;
5406 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5407 	if (rc == 0)
5408 		sc->params.tp_ch_map = val[0];
5409 	else
5410 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5411 
5412 	/*
5413 	 * Determine whether the firmware supports the filter2 work request.
5414 	 */
5415 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5416 	val[0] = 0;
5417 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5418 	if (rc == 0)
5419 		sc->params.filter2_wr_support = val[0] != 0;
5420 	else
5421 		sc->params.filter2_wr_support = 0;
5422 
5423 	/*
5424 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5425 	 */
5426 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5427 	val[0] = 0;
5428 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5429 	if (rc == 0)
5430 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5431 	else
5432 		sc->params.ulptx_memwrite_dsgl = false;
5433 
5434 	/* FW_RI_FR_NSMR_TPTE_WR support */
5435 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5436 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5437 	if (rc == 0)
5438 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5439 	else
5440 		sc->params.fr_nsmr_tpte_wr_support = false;
5441 
5442 	/* Support for 512 SGL entries per FR MR. */
5443 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5444 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5445 	if (rc == 0)
5446 		sc->params.dev_512sgl_mr = val[0] != 0;
5447 	else
5448 		sc->params.dev_512sgl_mr = false;
5449 
5450 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5451 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5452 	if (rc == 0)
5453 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5454 	else
5455 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5456 
5457 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5458 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5459 	if (rc == 0) {
5460 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5461 		sc->params.nsched_cls = val[0];
5462 	} else
5463 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5464 
5465 	/* get capabilites */
5466 	bzero(&caps, sizeof(caps));
5467 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5468 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5469 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5470 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5471 	if (rc != 0) {
5472 		device_printf(sc->dev,
5473 		    "failed to get card capabilities: %d.\n", rc);
5474 		return (rc);
5475 	}
5476 
5477 #define READ_CAPS(x) do { \
5478 	sc->x = htobe16(caps.x); \
5479 } while (0)
5480 	READ_CAPS(nbmcaps);
5481 	READ_CAPS(linkcaps);
5482 	READ_CAPS(switchcaps);
5483 	READ_CAPS(niccaps);
5484 	READ_CAPS(toecaps);
5485 	READ_CAPS(rdmacaps);
5486 	READ_CAPS(cryptocaps);
5487 	READ_CAPS(iscsicaps);
5488 	READ_CAPS(fcoecaps);
5489 
5490 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5491 		MPASS(chip_id(sc) > CHELSIO_T4);
5492 		MPASS(sc->toecaps == 0);
5493 		sc->toecaps = 0;
5494 
5495 		param[0] = FW_PARAM_DEV(NTID);
5496 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5497 		if (rc != 0) {
5498 			device_printf(sc->dev,
5499 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5500 			return (rc);
5501 		}
5502 		sc->tids.ntids = val[0];
5503 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5504 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5505 			sc->tids.ntids -= sc->tids.nhpftids;
5506 		}
5507 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5508 		sc->params.hash_filter = 1;
5509 	}
5510 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5511 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5512 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5513 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5514 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5515 		if (rc != 0) {
5516 			device_printf(sc->dev,
5517 			    "failed to query NIC parameters: %d.\n", rc);
5518 			return (rc);
5519 		}
5520 		if ((int)val[1] > (int)val[0]) {
5521 			sc->tids.etid_base = val[0];
5522 			sc->tids.etid_end = val[1];
5523 			sc->tids.netids = val[1] - val[0] + 1;
5524 			sc->params.eo_wr_cred = val[2];
5525 			sc->params.ethoffload = 1;
5526 		}
5527 	}
5528 	if (sc->toecaps) {
5529 		/* query offload-related parameters */
5530 		param[0] = FW_PARAM_DEV(NTID);
5531 		param[1] = FW_PARAM_PFVF(SERVER_START);
5532 		param[2] = FW_PARAM_PFVF(SERVER_END);
5533 		param[3] = FW_PARAM_PFVF(TDDP_START);
5534 		param[4] = FW_PARAM_PFVF(TDDP_END);
5535 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5536 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5537 		if (rc != 0) {
5538 			device_printf(sc->dev,
5539 			    "failed to query TOE parameters: %d.\n", rc);
5540 			return (rc);
5541 		}
5542 		sc->tids.ntids = val[0];
5543 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5544 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5545 			sc->tids.ntids -= sc->tids.nhpftids;
5546 		}
5547 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5548 		if ((int)val[2] > (int)val[1]) {
5549 			sc->tids.stid_base = val[1];
5550 			sc->tids.nstids = val[2] - val[1] + 1;
5551 		}
5552 		sc->vres.ddp.start = val[3];
5553 		sc->vres.ddp.size = val[4] - val[3] + 1;
5554 		sc->params.ofldq_wr_cred = val[5];
5555 		sc->params.offload = 1;
5556 	} else {
5557 		/*
5558 		 * The firmware attempts memfree TOE configuration for -SO cards
5559 		 * and will report toecaps=0 if it runs out of resources (this
5560 		 * depends on the config file).  It may not report 0 for other
5561 		 * capabilities dependent on the TOE in this case.  Set them to
5562 		 * 0 here so that the driver doesn't bother tracking resources
5563 		 * that will never be used.
5564 		 */
5565 		sc->iscsicaps = 0;
5566 		sc->rdmacaps = 0;
5567 	}
5568 	if (sc->rdmacaps) {
5569 		param[0] = FW_PARAM_PFVF(STAG_START);
5570 		param[1] = FW_PARAM_PFVF(STAG_END);
5571 		param[2] = FW_PARAM_PFVF(RQ_START);
5572 		param[3] = FW_PARAM_PFVF(RQ_END);
5573 		param[4] = FW_PARAM_PFVF(PBL_START);
5574 		param[5] = FW_PARAM_PFVF(PBL_END);
5575 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5576 		if (rc != 0) {
5577 			device_printf(sc->dev,
5578 			    "failed to query RDMA parameters(1): %d.\n", rc);
5579 			return (rc);
5580 		}
5581 		sc->vres.stag.start = val[0];
5582 		sc->vres.stag.size = val[1] - val[0] + 1;
5583 		sc->vres.rq.start = val[2];
5584 		sc->vres.rq.size = val[3] - val[2] + 1;
5585 		sc->vres.pbl.start = val[4];
5586 		sc->vres.pbl.size = val[5] - val[4] + 1;
5587 
5588 		param[0] = FW_PARAM_PFVF(SQRQ_START);
5589 		param[1] = FW_PARAM_PFVF(SQRQ_END);
5590 		param[2] = FW_PARAM_PFVF(CQ_START);
5591 		param[3] = FW_PARAM_PFVF(CQ_END);
5592 		param[4] = FW_PARAM_PFVF(OCQ_START);
5593 		param[5] = FW_PARAM_PFVF(OCQ_END);
5594 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5595 		if (rc != 0) {
5596 			device_printf(sc->dev,
5597 			    "failed to query RDMA parameters(2): %d.\n", rc);
5598 			return (rc);
5599 		}
5600 		sc->vres.qp.start = val[0];
5601 		sc->vres.qp.size = val[1] - val[0] + 1;
5602 		sc->vres.cq.start = val[2];
5603 		sc->vres.cq.size = val[3] - val[2] + 1;
5604 		sc->vres.ocq.start = val[4];
5605 		sc->vres.ocq.size = val[5] - val[4] + 1;
5606 
5607 		param[0] = FW_PARAM_PFVF(SRQ_START);
5608 		param[1] = FW_PARAM_PFVF(SRQ_END);
5609 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
5610 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5611 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5612 		if (rc != 0) {
5613 			device_printf(sc->dev,
5614 			    "failed to query RDMA parameters(3): %d.\n", rc);
5615 			return (rc);
5616 		}
5617 		sc->vres.srq.start = val[0];
5618 		sc->vres.srq.size = val[1] - val[0] + 1;
5619 		sc->params.max_ordird_qp = val[2];
5620 		sc->params.max_ird_adapter = val[3];
5621 	}
5622 	if (sc->iscsicaps) {
5623 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5624 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5625 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5626 		if (rc != 0) {
5627 			device_printf(sc->dev,
5628 			    "failed to query iSCSI parameters: %d.\n", rc);
5629 			return (rc);
5630 		}
5631 		sc->vres.iscsi.start = val[0];
5632 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5633 	}
5634 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5635 		param[0] = FW_PARAM_PFVF(TLS_START);
5636 		param[1] = FW_PARAM_PFVF(TLS_END);
5637 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5638 		if (rc != 0) {
5639 			device_printf(sc->dev,
5640 			    "failed to query TLS parameters: %d.\n", rc);
5641 			return (rc);
5642 		}
5643 		sc->vres.key.start = val[0];
5644 		sc->vres.key.size = val[1] - val[0] + 1;
5645 	}
5646 
5647 	/*
5648 	 * We've got the params we wanted to query directly from the firmware.
5649 	 * Grab some others via other means.
5650 	 */
5651 	t4_init_sge_params(sc);
5652 	t4_init_tp_params(sc);
5653 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5654 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5655 
5656 	rc = t4_verify_chip_settings(sc);
5657 	if (rc != 0)
5658 		return (rc);
5659 	t4_init_rx_buf_info(sc);
5660 
5661 	return (rc);
5662 }
5663 
5664 #ifdef KERN_TLS
5665 static void
ktls_tick(void * arg)5666 ktls_tick(void *arg)
5667 {
5668 	struct adapter *sc;
5669 	uint32_t tstamp;
5670 
5671 	sc = arg;
5672 	tstamp = tcp_ts_getticks();
5673 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
5674 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
5675 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
5676 }
5677 
5678 static int
t6_config_kern_tls(struct adapter * sc,bool enable)5679 t6_config_kern_tls(struct adapter *sc, bool enable)
5680 {
5681 	int rc;
5682 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5683 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
5684 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
5685 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
5686 
5687 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
5688 	if (rc != 0) {
5689 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
5690 		    enable ?  "enable" : "disable", rc);
5691 		return (rc);
5692 	}
5693 
5694 	if (enable) {
5695 		sc->flags |= KERN_TLS_ON;
5696 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
5697 		    C_HARDCLOCK);
5698 	} else {
5699 		sc->flags &= ~KERN_TLS_ON;
5700 		callout_stop(&sc->ktls_tick);
5701 	}
5702 
5703 	return (rc);
5704 }
5705 #endif
5706 
5707 static int
set_params__post_init(struct adapter * sc)5708 set_params__post_init(struct adapter *sc)
5709 {
5710 	uint32_t mask, param, val;
5711 #ifdef TCP_OFFLOAD
5712 	int i, v, shift;
5713 #endif
5714 
5715 	/* ask for encapsulated CPLs */
5716 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5717 	val = 1;
5718 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5719 
5720 	/* Enable 32b port caps if the firmware supports it. */
5721 	param = FW_PARAM_PFVF(PORT_CAPS32);
5722 	val = 1;
5723 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
5724 		sc->params.port_caps32 = 1;
5725 
5726 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
5727 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
5728 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
5729 	    V_MASKFILTER(val - 1));
5730 
5731 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
5732 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
5733 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5734 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
5735 	val = 0;
5736 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
5737 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
5738 		    F_ATTACKFILTERENABLE);
5739 		val |= F_DROPERRORATTACK;
5740 	}
5741 	if (t4_drop_ip_fragments != 0) {
5742 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
5743 		    F_FRAGMENTDROP);
5744 		val |= F_DROPERRORFRAG;
5745 	}
5746 	if (t4_drop_pkts_with_l2_errors != 0)
5747 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
5748 	if (t4_drop_pkts_with_l3_errors != 0) {
5749 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
5750 		    F_DROPERRORCSUMIP;
5751 	}
5752 	if (t4_drop_pkts_with_l4_errors != 0) {
5753 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5754 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
5755 	}
5756 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
5757 
5758 #ifdef TCP_OFFLOAD
5759 	/*
5760 	 * Override the TOE timers with user provided tunables.  This is not the
5761 	 * recommended way to change the timers (the firmware config file is) so
5762 	 * these tunables are not documented.
5763 	 *
5764 	 * All the timer tunables are in microseconds.
5765 	 */
5766 	if (t4_toe_keepalive_idle != 0) {
5767 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
5768 		v &= M_KEEPALIVEIDLE;
5769 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
5770 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
5771 	}
5772 	if (t4_toe_keepalive_interval != 0) {
5773 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
5774 		v &= M_KEEPALIVEINTVL;
5775 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
5776 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
5777 	}
5778 	if (t4_toe_keepalive_count != 0) {
5779 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
5780 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5781 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
5782 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
5783 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
5784 	}
5785 	if (t4_toe_rexmt_min != 0) {
5786 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
5787 		v &= M_RXTMIN;
5788 		t4_set_reg_field(sc, A_TP_RXT_MIN,
5789 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
5790 	}
5791 	if (t4_toe_rexmt_max != 0) {
5792 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
5793 		v &= M_RXTMAX;
5794 		t4_set_reg_field(sc, A_TP_RXT_MAX,
5795 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
5796 	}
5797 	if (t4_toe_rexmt_count != 0) {
5798 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
5799 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5800 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
5801 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
5802 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
5803 	}
5804 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
5805 		if (t4_toe_rexmt_backoff[i] != -1) {
5806 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
5807 			shift = (i & 3) << 3;
5808 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
5809 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
5810 		}
5811 	}
5812 #endif
5813 
5814 #ifdef KERN_TLS
5815 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS &&
5816 	    sc->toecaps & FW_CAPS_CONFIG_TOE) {
5817 		/*
5818 		 * Limit TOE connections to 2 reassembly "islands".  This is
5819 		 * required for TOE TLS connections to downgrade to plain TOE
5820 		 * connections if an unsupported TLS version or ciphersuite is
5821 		 * used.
5822 		 */
5823 		t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG,
5824 		    V_PASSMODE(M_PASSMODE), V_PASSMODE(2));
5825 	}
5826 
5827 	if (is_ktls(sc)) {
5828 		sc->tlst.inline_keys = t4_tls_inline_keys;
5829 		sc->tlst.combo_wrs = t4_tls_combo_wrs;
5830 		if (t4_kern_tls != 0 && is_t6(sc))
5831 			t6_config_kern_tls(sc, true);
5832 	}
5833 #endif
5834 	return (0);
5835 }
5836 
5837 #undef FW_PARAM_PFVF
5838 #undef FW_PARAM_DEV
5839 
5840 static void
t4_set_desc(struct adapter * sc)5841 t4_set_desc(struct adapter *sc)
5842 {
5843 	char buf[128];
5844 	struct adapter_params *p = &sc->params;
5845 
5846 	snprintf(buf, sizeof(buf), "Chelsio %s", p->vpd.id);
5847 
5848 	device_set_desc_copy(sc->dev, buf);
5849 }
5850 
5851 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)5852 ifmedia_add4(struct ifmedia *ifm, int m)
5853 {
5854 
5855 	ifmedia_add(ifm, m, 0, NULL);
5856 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
5857 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
5858 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
5859 }
5860 
5861 /*
5862  * This is the selected media, which is not quite the same as the active media.
5863  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
5864  * and active are not the same, and "media: Ethernet selected" otherwise.
5865  */
5866 static void
set_current_media(struct port_info * pi)5867 set_current_media(struct port_info *pi)
5868 {
5869 	struct link_config *lc;
5870 	struct ifmedia *ifm;
5871 	int mword;
5872 	u_int speed;
5873 
5874 	PORT_LOCK_ASSERT_OWNED(pi);
5875 
5876 	/* Leave current media alone if it's already set to IFM_NONE. */
5877 	ifm = &pi->media;
5878 	if (ifm->ifm_cur != NULL &&
5879 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
5880 		return;
5881 
5882 	lc = &pi->link_cfg;
5883 	if (lc->requested_aneg != AUTONEG_DISABLE &&
5884 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
5885 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
5886 		return;
5887 	}
5888 	mword = IFM_ETHER | IFM_FDX;
5889 	if (lc->requested_fc & PAUSE_TX)
5890 		mword |= IFM_ETH_TXPAUSE;
5891 	if (lc->requested_fc & PAUSE_RX)
5892 		mword |= IFM_ETH_RXPAUSE;
5893 	if (lc->requested_speed == 0)
5894 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
5895 	else
5896 		speed = lc->requested_speed;
5897 	mword |= port_mword(pi, speed_to_fwcap(speed));
5898 	ifmedia_set(ifm, mword);
5899 }
5900 
5901 /*
5902  * Returns true if the ifmedia list for the port cannot change.
5903  */
5904 static bool
fixed_ifmedia(struct port_info * pi)5905 fixed_ifmedia(struct port_info *pi)
5906 {
5907 
5908 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
5909 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
5910 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
5911 	    pi->port_type == FW_PORT_TYPE_KX4 ||
5912 	    pi->port_type == FW_PORT_TYPE_KX ||
5913 	    pi->port_type == FW_PORT_TYPE_KR ||
5914 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
5915 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
5916 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
5917 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
5918 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
5919 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
5920 }
5921 
5922 static void
build_medialist(struct port_info * pi)5923 build_medialist(struct port_info *pi)
5924 {
5925 	uint32_t ss, speed;
5926 	int unknown, mword, bit;
5927 	struct link_config *lc;
5928 	struct ifmedia *ifm;
5929 
5930 	PORT_LOCK_ASSERT_OWNED(pi);
5931 
5932 	if (pi->flags & FIXED_IFMEDIA)
5933 		return;
5934 
5935 	/*
5936 	 * Rebuild the ifmedia list.
5937 	 */
5938 	ifm = &pi->media;
5939 	ifmedia_removeall(ifm);
5940 	lc = &pi->link_cfg;
5941 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
5942 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
5943 		MPASS(ss != 0);
5944 no_media:
5945 		MPASS(LIST_EMPTY(&ifm->ifm_list));
5946 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
5947 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
5948 		return;
5949 	}
5950 
5951 	unknown = 0;
5952 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
5953 		speed = 1 << bit;
5954 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
5955 		if (ss & speed) {
5956 			mword = port_mword(pi, speed);
5957 			if (mword == IFM_NONE) {
5958 				goto no_media;
5959 			} else if (mword == IFM_UNKNOWN)
5960 				unknown++;
5961 			else
5962 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
5963 		}
5964 	}
5965 	if (unknown > 0) /* Add one unknown for all unknown media types. */
5966 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
5967 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
5968 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
5969 
5970 	set_current_media(pi);
5971 }
5972 
5973 /*
5974  * Initialize the requested fields in the link config based on driver tunables.
5975  */
5976 static void
init_link_config(struct port_info * pi)5977 init_link_config(struct port_info *pi)
5978 {
5979 	struct link_config *lc = &pi->link_cfg;
5980 
5981 	PORT_LOCK_ASSERT_OWNED(pi);
5982 
5983 	lc->requested_caps = 0;
5984 	lc->requested_speed = 0;
5985 
5986 	if (t4_autoneg == 0)
5987 		lc->requested_aneg = AUTONEG_DISABLE;
5988 	else if (t4_autoneg == 1)
5989 		lc->requested_aneg = AUTONEG_ENABLE;
5990 	else
5991 		lc->requested_aneg = AUTONEG_AUTO;
5992 
5993 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
5994 	    PAUSE_AUTONEG);
5995 
5996 	if (t4_fec & FEC_AUTO)
5997 		lc->requested_fec = FEC_AUTO;
5998 	else if (t4_fec == 0)
5999 		lc->requested_fec = FEC_NONE;
6000 	else {
6001 		/* -1 is handled by the FEC_AUTO block above and not here. */
6002 		lc->requested_fec = t4_fec &
6003 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6004 		if (lc->requested_fec == 0)
6005 			lc->requested_fec = FEC_AUTO;
6006 	}
6007 	if (t4_force_fec < 0)
6008 		lc->force_fec = -1;
6009 	else if (t4_force_fec > 0)
6010 		lc->force_fec = 1;
6011 	else
6012 		lc->force_fec = 0;
6013 }
6014 
6015 /*
6016  * Makes sure that all requested settings comply with what's supported by the
6017  * port.  Returns the number of settings that were invalid and had to be fixed.
6018  */
6019 static int
fixup_link_config(struct port_info * pi)6020 fixup_link_config(struct port_info *pi)
6021 {
6022 	int n = 0;
6023 	struct link_config *lc = &pi->link_cfg;
6024 	uint32_t fwspeed;
6025 
6026 	PORT_LOCK_ASSERT_OWNED(pi);
6027 
6028 	/* Speed (when not autonegotiating) */
6029 	if (lc->requested_speed != 0) {
6030 		fwspeed = speed_to_fwcap(lc->requested_speed);
6031 		if ((fwspeed & lc->pcaps) == 0) {
6032 			n++;
6033 			lc->requested_speed = 0;
6034 		}
6035 	}
6036 
6037 	/* Link autonegotiation */
6038 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6039 	    lc->requested_aneg == AUTONEG_DISABLE ||
6040 	    lc->requested_aneg == AUTONEG_AUTO);
6041 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6042 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6043 		n++;
6044 		lc->requested_aneg = AUTONEG_AUTO;
6045 	}
6046 
6047 	/* Flow control */
6048 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6049 	if (lc->requested_fc & PAUSE_TX &&
6050 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6051 		n++;
6052 		lc->requested_fc &= ~PAUSE_TX;
6053 	}
6054 	if (lc->requested_fc & PAUSE_RX &&
6055 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6056 		n++;
6057 		lc->requested_fc &= ~PAUSE_RX;
6058 	}
6059 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6060 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6061 		n++;
6062 		lc->requested_fc |= PAUSE_AUTONEG;
6063 	}
6064 
6065 	/* FEC */
6066 	if ((lc->requested_fec & FEC_RS &&
6067 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6068 	    (lc->requested_fec & FEC_BASER_RS &&
6069 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6070 		n++;
6071 		lc->requested_fec = FEC_AUTO;
6072 	}
6073 
6074 	return (n);
6075 }
6076 
6077 /*
6078  * Apply the requested L1 settings, which are expected to be valid, to the
6079  * hardware.
6080  */
6081 static int
apply_link_config(struct port_info * pi)6082 apply_link_config(struct port_info *pi)
6083 {
6084 	struct adapter *sc = pi->adapter;
6085 	struct link_config *lc = &pi->link_cfg;
6086 	int rc;
6087 
6088 #ifdef INVARIANTS
6089 	ASSERT_SYNCHRONIZED_OP(sc);
6090 	PORT_LOCK_ASSERT_OWNED(pi);
6091 
6092 	if (lc->requested_aneg == AUTONEG_ENABLE)
6093 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6094 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6095 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6096 	if (lc->requested_fc & PAUSE_TX)
6097 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6098 	if (lc->requested_fc & PAUSE_RX)
6099 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6100 	if (lc->requested_fec & FEC_RS)
6101 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6102 	if (lc->requested_fec & FEC_BASER_RS)
6103 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6104 #endif
6105 	if (!(sc->flags & IS_VF)) {
6106 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
6107 		if (rc != 0) {
6108 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6109 			return (rc);
6110 		}
6111 	}
6112 
6113 	/*
6114 	 * An L1_CFG will almost always result in a link-change event if the
6115 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6116 	 * the notification is processed.  If the link is down then the actual
6117 	 * settings are meaningless.
6118 	 *
6119 	 * This takes care of the case where a change in the L1 settings may not
6120 	 * result in a notification.
6121 	 */
6122 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6123 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6124 
6125 	return (0);
6126 }
6127 
6128 #define FW_MAC_EXACT_CHUNK	7
6129 struct mcaddr_ctx {
6130 	struct ifnet *ifp;
6131 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6132 	uint64_t hash;
6133 	int i;
6134 	int del;
6135 	int rc;
6136 };
6137 
6138 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6139 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6140 {
6141 	struct mcaddr_ctx *ctx = arg;
6142 	struct vi_info *vi = ctx->ifp->if_softc;
6143 	struct port_info *pi = vi->pi;
6144 	struct adapter *sc = pi->adapter;
6145 
6146 	if (ctx->rc < 0)
6147 		return (0);
6148 
6149 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6150 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6151 	ctx->i++;
6152 
6153 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6154 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6155 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6156 		if (ctx->rc < 0) {
6157 			int j;
6158 
6159 			for (j = 0; j < ctx->i; j++) {
6160 				if_printf(ctx->ifp,
6161 				    "failed to add mc address"
6162 				    " %02x:%02x:%02x:"
6163 				    "%02x:%02x:%02x rc=%d\n",
6164 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6165 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6166 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6167 				    -ctx->rc);
6168 			}
6169 			return (0);
6170 		}
6171 		ctx->del = 0;
6172 		ctx->i = 0;
6173 	}
6174 
6175 	return (1);
6176 }
6177 
6178 /*
6179  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6180  * indicates which parameters should be programmed (the rest are left alone).
6181  */
6182 int
update_mac_settings(struct ifnet * ifp,int flags)6183 update_mac_settings(struct ifnet *ifp, int flags)
6184 {
6185 	int rc = 0;
6186 	struct vi_info *vi = ifp->if_softc;
6187 	struct port_info *pi = vi->pi;
6188 	struct adapter *sc = pi->adapter;
6189 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6190 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6191 
6192 	ASSERT_SYNCHRONIZED_OP(sc);
6193 	KASSERT(flags, ("%s: not told what to update.", __func__));
6194 
6195 	if (flags & XGMAC_MTU)
6196 		mtu = ifp->if_mtu;
6197 
6198 	if (flags & XGMAC_PROMISC)
6199 		promisc = ifp->if_flags & IFF_PROMISC ? 1 : 0;
6200 
6201 	if (flags & XGMAC_ALLMULTI)
6202 		allmulti = ifp->if_flags & IFF_ALLMULTI ? 1 : 0;
6203 
6204 	if (flags & XGMAC_VLANEX)
6205 		vlanex = ifp->if_capenable & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6206 
6207 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6208 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6209 		    allmulti, 1, vlanex, false);
6210 		if (rc) {
6211 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6212 			    rc);
6213 			return (rc);
6214 		}
6215 	}
6216 
6217 	if (flags & XGMAC_UCADDR) {
6218 		uint8_t ucaddr[ETHER_ADDR_LEN];
6219 
6220 		bcopy(IF_LLADDR(ifp), ucaddr, sizeof(ucaddr));
6221 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6222 		    ucaddr, true, &vi->smt_idx);
6223 		if (rc < 0) {
6224 			rc = -rc;
6225 			if_printf(ifp, "change_mac failed: %d\n", rc);
6226 			return (rc);
6227 		} else {
6228 			vi->xact_addr_filt = rc;
6229 			rc = 0;
6230 		}
6231 	}
6232 
6233 	if (flags & XGMAC_MCADDRS) {
6234 		struct epoch_tracker et;
6235 		struct mcaddr_ctx ctx;
6236 		int j;
6237 
6238 		ctx.ifp = ifp;
6239 		ctx.hash = 0;
6240 		ctx.i = 0;
6241 		ctx.del = 1;
6242 		ctx.rc = 0;
6243 		/*
6244 		 * Unlike other drivers, we accumulate list of pointers into
6245 		 * interface address lists and we need to keep it safe even
6246 		 * after if_foreach_llmaddr() returns, thus we must enter the
6247 		 * network epoch.
6248 		 */
6249 		NET_EPOCH_ENTER(et);
6250 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6251 		if (ctx.rc < 0) {
6252 			NET_EPOCH_EXIT(et);
6253 			rc = -ctx.rc;
6254 			return (rc);
6255 		}
6256 		if (ctx.i > 0) {
6257 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6258 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6259 			NET_EPOCH_EXIT(et);
6260 			if (rc < 0) {
6261 				rc = -rc;
6262 				for (j = 0; j < ctx.i; j++) {
6263 					if_printf(ifp,
6264 					    "failed to add mcast address"
6265 					    " %02x:%02x:%02x:"
6266 					    "%02x:%02x:%02x rc=%d\n",
6267 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6268 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6269 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6270 					    rc);
6271 				}
6272 				return (rc);
6273 			}
6274 			ctx.del = 0;
6275 		} else
6276 			NET_EPOCH_EXIT(et);
6277 
6278 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6279 		if (rc != 0)
6280 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6281 			    rc);
6282 		if (ctx.del == 0) {
6283 			/* We clobbered the VXLAN entry if there was one. */
6284 			pi->vxlan_tcam_entry = false;
6285 		}
6286 	}
6287 
6288 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6289 	    pi->vxlan_tcam_entry == false) {
6290 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6291 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6292 		    true);
6293 		if (rc < 0) {
6294 			rc = -rc;
6295 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6296 			    rc);
6297 		} else {
6298 			MPASS(rc == sc->rawf_base + pi->port_id);
6299 			rc = 0;
6300 			pi->vxlan_tcam_entry = true;
6301 		}
6302 	}
6303 
6304 	return (rc);
6305 }
6306 
6307 /*
6308  * {begin|end}_synchronized_op must be called from the same thread.
6309  */
6310 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6311 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6312     char *wmesg)
6313 {
6314 	int rc, pri;
6315 
6316 #ifdef WITNESS
6317 	/* the caller thinks it's ok to sleep, but is it really? */
6318 	if (flags & SLEEP_OK)
6319 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
6320 		    "begin_synchronized_op");
6321 #endif
6322 
6323 	if (INTR_OK)
6324 		pri = PCATCH;
6325 	else
6326 		pri = 0;
6327 
6328 	ADAPTER_LOCK(sc);
6329 	for (;;) {
6330 
6331 		if (vi && IS_DETACHING(vi)) {
6332 			rc = ENXIO;
6333 			goto done;
6334 		}
6335 
6336 		if (!IS_BUSY(sc)) {
6337 			rc = 0;
6338 			break;
6339 		}
6340 
6341 		if (!(flags & SLEEP_OK)) {
6342 			rc = EBUSY;
6343 			goto done;
6344 		}
6345 
6346 		if (mtx_sleep(&sc->flags, &sc->sc_lock, pri, wmesg, 0)) {
6347 			rc = EINTR;
6348 			goto done;
6349 		}
6350 	}
6351 
6352 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6353 	SET_BUSY(sc);
6354 #ifdef INVARIANTS
6355 	sc->last_op = wmesg;
6356 	sc->last_op_thr = curthread;
6357 	sc->last_op_flags = flags;
6358 #endif
6359 
6360 done:
6361 	if (!(flags & HOLD_LOCK) || rc)
6362 		ADAPTER_UNLOCK(sc);
6363 
6364 	return (rc);
6365 }
6366 
6367 /*
6368  * Tell if_ioctl and if_init that the VI is going away.  This is
6369  * special variant of begin_synchronized_op and must be paired with a
6370  * call to end_vi_detach.
6371  */
6372 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6373 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6374 {
6375 	ADAPTER_LOCK(sc);
6376 	SET_DETACHING(vi);
6377 	wakeup(&sc->flags);
6378 	while (IS_BUSY(sc))
6379 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6380 	SET_BUSY(sc);
6381 #ifdef INVARIANTS
6382 	sc->last_op = "t4detach";
6383 	sc->last_op_thr = curthread;
6384 	sc->last_op_flags = 0;
6385 #endif
6386 	ADAPTER_UNLOCK(sc);
6387 }
6388 
6389 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6390 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6391 {
6392 	ADAPTER_LOCK(sc);
6393 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6394 	CLR_BUSY(sc);
6395 	CLR_DETACHING(vi);
6396 	wakeup(&sc->flags);
6397 	ADAPTER_UNLOCK(sc);
6398 }
6399 
6400 /*
6401  * {begin|end}_synchronized_op must be called from the same thread.
6402  */
6403 void
end_synchronized_op(struct adapter * sc,int flags)6404 end_synchronized_op(struct adapter *sc, int flags)
6405 {
6406 
6407 	if (flags & LOCK_HELD)
6408 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6409 	else
6410 		ADAPTER_LOCK(sc);
6411 
6412 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6413 	CLR_BUSY(sc);
6414 	wakeup(&sc->flags);
6415 	ADAPTER_UNLOCK(sc);
6416 }
6417 
6418 static int
cxgbe_init_synchronized(struct vi_info * vi)6419 cxgbe_init_synchronized(struct vi_info *vi)
6420 {
6421 	struct port_info *pi = vi->pi;
6422 	struct adapter *sc = pi->adapter;
6423 	struct ifnet *ifp = vi->ifp;
6424 	int rc = 0, i;
6425 	struct sge_txq *txq;
6426 
6427 	ASSERT_SYNCHRONIZED_OP(sc);
6428 
6429 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6430 		return (0);	/* already running */
6431 
6432 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6433 		return (rc);	/* error message displayed already */
6434 
6435 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6436 		return (rc); /* error message displayed already */
6437 
6438 	rc = update_mac_settings(ifp, XGMAC_ALL);
6439 	if (rc)
6440 		goto done;	/* error message displayed already */
6441 
6442 	PORT_LOCK(pi);
6443 	if (pi->up_vis == 0) {
6444 		t4_update_port_info(pi);
6445 		fixup_link_config(pi);
6446 		build_medialist(pi);
6447 		apply_link_config(pi);
6448 	}
6449 
6450 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6451 	if (rc != 0) {
6452 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6453 		PORT_UNLOCK(pi);
6454 		goto done;
6455 	}
6456 
6457 	/*
6458 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6459 	 * if this changes.
6460 	 */
6461 
6462 	for_each_txq(vi, i, txq) {
6463 		TXQ_LOCK(txq);
6464 		txq->eq.flags |= EQ_ENABLED;
6465 		TXQ_UNLOCK(txq);
6466 	}
6467 
6468 	/*
6469 	 * The first iq of the first port to come up is used for tracing.
6470 	 */
6471 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6472 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6473 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
6474 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
6475 		    V_QUEUENUMBER(sc->traceq));
6476 		pi->flags |= HAS_TRACEQ;
6477 	}
6478 
6479 	/* all ok */
6480 	pi->up_vis++;
6481 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
6482 	if (pi->link_cfg.link_ok)
6483 		t4_os_link_changed(pi);
6484 	PORT_UNLOCK(pi);
6485 
6486 	mtx_lock(&vi->tick_mtx);
6487 	if (ifp->if_get_counter == vi_get_counter)
6488 		callout_reset(&vi->tick, hz, vi_tick, vi);
6489 	else
6490 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6491 	mtx_unlock(&vi->tick_mtx);
6492 done:
6493 	if (rc != 0)
6494 		cxgbe_uninit_synchronized(vi);
6495 
6496 	return (rc);
6497 }
6498 
6499 /*
6500  * Idempotent.
6501  */
6502 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6503 cxgbe_uninit_synchronized(struct vi_info *vi)
6504 {
6505 	struct port_info *pi = vi->pi;
6506 	struct adapter *sc = pi->adapter;
6507 	struct ifnet *ifp = vi->ifp;
6508 	int rc, i;
6509 	struct sge_txq *txq;
6510 
6511 	ASSERT_SYNCHRONIZED_OP(sc);
6512 
6513 	if (!(vi->flags & VI_INIT_DONE)) {
6514 		if (__predict_false(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
6515 			KASSERT(0, ("uninited VI is running"));
6516 			if_printf(ifp, "uninited VI with running ifnet.  "
6517 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6518 			    "if_drv_flags 0x%08x\n", vi->flags, ifp->if_flags,
6519 			    ifp->if_drv_flags);
6520 		}
6521 		return (0);
6522 	}
6523 
6524 	/*
6525 	 * Disable the VI so that all its data in either direction is discarded
6526 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6527 	 * tick) intact as the TP can deliver negative advice or data that it's
6528 	 * holding in its RAM (for an offloaded connection) even after the VI is
6529 	 * disabled.
6530 	 */
6531 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6532 	if (rc) {
6533 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6534 		return (rc);
6535 	}
6536 
6537 	for_each_txq(vi, i, txq) {
6538 		TXQ_LOCK(txq);
6539 		txq->eq.flags &= ~EQ_ENABLED;
6540 		TXQ_UNLOCK(txq);
6541 	}
6542 
6543 	mtx_lock(&vi->tick_mtx);
6544 	callout_stop(&vi->tick);
6545 	mtx_unlock(&vi->tick_mtx);
6546 
6547 	PORT_LOCK(pi);
6548 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
6549 		PORT_UNLOCK(pi);
6550 		return (0);
6551 	}
6552 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
6553 	pi->up_vis--;
6554 	if (pi->up_vis > 0) {
6555 		PORT_UNLOCK(pi);
6556 		return (0);
6557 	}
6558 
6559 	pi->link_cfg.link_ok = false;
6560 	pi->link_cfg.speed = 0;
6561 	pi->link_cfg.link_down_rc = 255;
6562 	t4_os_link_changed(pi);
6563 	PORT_UNLOCK(pi);
6564 
6565 	return (0);
6566 }
6567 
6568 /*
6569  * It is ok for this function to fail midway and return right away.  t4_detach
6570  * will walk the entire sc->irq list and clean up whatever is valid.
6571  */
6572 int
t4_setup_intr_handlers(struct adapter * sc)6573 t4_setup_intr_handlers(struct adapter *sc)
6574 {
6575 	int rc, rid, p, q, v;
6576 	char s[8];
6577 	struct irq *irq;
6578 	struct port_info *pi;
6579 	struct vi_info *vi;
6580 	struct sge *sge = &sc->sge;
6581 	struct sge_rxq *rxq;
6582 #ifdef TCP_OFFLOAD
6583 	struct sge_ofld_rxq *ofld_rxq;
6584 #endif
6585 #ifdef DEV_NETMAP
6586 	struct sge_nm_rxq *nm_rxq;
6587 #endif
6588 #ifdef RSS
6589 	int nbuckets = rss_getnumbuckets();
6590 #endif
6591 
6592 	/*
6593 	 * Setup interrupts.
6594 	 */
6595 	irq = &sc->irq[0];
6596 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6597 	if (forwarding_intr_to_fwq(sc))
6598 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6599 
6600 	/* Multiple interrupts. */
6601 	if (sc->flags & IS_VF)
6602 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6603 		    ("%s: too few intr.", __func__));
6604 	else
6605 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6606 		    ("%s: too few intr.", __func__));
6607 
6608 	/* The first one is always error intr on PFs */
6609 	if (!(sc->flags & IS_VF)) {
6610 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6611 		if (rc != 0)
6612 			return (rc);
6613 		irq++;
6614 		rid++;
6615 	}
6616 
6617 	/* The second one is always the firmware event queue (first on VFs) */
6618 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6619 	if (rc != 0)
6620 		return (rc);
6621 	irq++;
6622 	rid++;
6623 
6624 	for_each_port(sc, p) {
6625 		pi = sc->port[p];
6626 		for_each_vi(pi, v, vi) {
6627 			vi->first_intr = rid - 1;
6628 
6629 			if (vi->nnmrxq > 0) {
6630 				int n = max(vi->nrxq, vi->nnmrxq);
6631 
6632 				rxq = &sge->rxq[vi->first_rxq];
6633 #ifdef DEV_NETMAP
6634 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6635 #endif
6636 				for (q = 0; q < n; q++) {
6637 					snprintf(s, sizeof(s), "%x%c%x", p,
6638 					    'a' + v, q);
6639 					if (q < vi->nrxq)
6640 						irq->rxq = rxq++;
6641 #ifdef DEV_NETMAP
6642 					if (q < vi->nnmrxq)
6643 						irq->nm_rxq = nm_rxq++;
6644 
6645 					if (irq->nm_rxq != NULL &&
6646 					    irq->rxq == NULL) {
6647 						/* Netmap rx only */
6648 						rc = t4_alloc_irq(sc, irq, rid,
6649 						    t4_nm_intr, irq->nm_rxq, s);
6650 					}
6651 					if (irq->nm_rxq != NULL &&
6652 					    irq->rxq != NULL) {
6653 						/* NIC and Netmap rx */
6654 						rc = t4_alloc_irq(sc, irq, rid,
6655 						    t4_vi_intr, irq, s);
6656 					}
6657 #endif
6658 					if (irq->rxq != NULL &&
6659 					    irq->nm_rxq == NULL) {
6660 						/* NIC rx only */
6661 						rc = t4_alloc_irq(sc, irq, rid,
6662 						    t4_intr, irq->rxq, s);
6663 					}
6664 					if (rc != 0)
6665 						return (rc);
6666 #ifdef RSS
6667 					if (q < vi->nrxq) {
6668 						bus_bind_intr(sc->dev, irq->res,
6669 						    rss_getcpu(q % nbuckets));
6670 					}
6671 #endif
6672 					irq++;
6673 					rid++;
6674 					vi->nintr++;
6675 				}
6676 			} else {
6677 				for_each_rxq(vi, q, rxq) {
6678 					snprintf(s, sizeof(s), "%x%c%x", p,
6679 					    'a' + v, q);
6680 					rc = t4_alloc_irq(sc, irq, rid,
6681 					    t4_intr, rxq, s);
6682 					if (rc != 0)
6683 						return (rc);
6684 #ifdef RSS
6685 					bus_bind_intr(sc->dev, irq->res,
6686 					    rss_getcpu(q % nbuckets));
6687 #endif
6688 					irq++;
6689 					rid++;
6690 					vi->nintr++;
6691 				}
6692 			}
6693 #ifdef TCP_OFFLOAD
6694 			for_each_ofld_rxq(vi, q, ofld_rxq) {
6695 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
6696 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
6697 				    ofld_rxq, s);
6698 				if (rc != 0)
6699 					return (rc);
6700 				irq++;
6701 				rid++;
6702 				vi->nintr++;
6703 			}
6704 #endif
6705 		}
6706 	}
6707 	MPASS(irq == &sc->irq[sc->intr_count]);
6708 
6709 	return (0);
6710 }
6711 
6712 static void
write_global_rss_key(struct adapter * sc)6713 write_global_rss_key(struct adapter *sc)
6714 {
6715 #ifdef RSS
6716 	int i;
6717 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6718 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6719 
6720 	CTASSERT(RSS_KEYSIZE == 40);
6721 
6722 	rss_getkey((void *)&raw_rss_key[0]);
6723 	for (i = 0; i < nitems(rss_key); i++) {
6724 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
6725 	}
6726 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
6727 #endif
6728 }
6729 
6730 /*
6731  * Idempotent.
6732  */
6733 static int
adapter_full_init(struct adapter * sc)6734 adapter_full_init(struct adapter *sc)
6735 {
6736 	int rc, i;
6737 
6738 	ASSERT_SYNCHRONIZED_OP(sc);
6739 
6740 	/*
6741 	 * queues that belong to the adapter (not any particular port).
6742 	 */
6743 	rc = t4_setup_adapter_queues(sc);
6744 	if (rc != 0)
6745 		return (rc);
6746 
6747 	MPASS(sc->params.nports <= nitems(sc->tq));
6748 	for (i = 0; i < sc->params.nports; i++) {
6749 		if (sc->tq[i] != NULL)
6750 			continue;
6751 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
6752 		    taskqueue_thread_enqueue, &sc->tq[i]);
6753 		if (sc->tq[i] == NULL) {
6754 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
6755 			return (ENOMEM);
6756 		}
6757 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
6758 		    device_get_nameunit(sc->dev), i);
6759 	}
6760 
6761 	if (!(sc->flags & IS_VF)) {
6762 		write_global_rss_key(sc);
6763 		t4_intr_enable(sc);
6764 	}
6765 	return (0);
6766 }
6767 
6768 int
adapter_init(struct adapter * sc)6769 adapter_init(struct adapter *sc)
6770 {
6771 	int rc;
6772 
6773 	ASSERT_SYNCHRONIZED_OP(sc);
6774 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
6775 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
6776 	    ("%s: FULL_INIT_DONE already", __func__));
6777 
6778 	rc = adapter_full_init(sc);
6779 	if (rc != 0)
6780 		adapter_full_uninit(sc);
6781 	else
6782 		sc->flags |= FULL_INIT_DONE;
6783 
6784 	return (rc);
6785 }
6786 
6787 /*
6788  * Idempotent.
6789  */
6790 static void
adapter_full_uninit(struct adapter * sc)6791 adapter_full_uninit(struct adapter *sc)
6792 {
6793 	int i;
6794 
6795 	t4_teardown_adapter_queues(sc);
6796 
6797 	for (i = 0; i < nitems(sc->tq); i++) {
6798 		if (sc->tq[i] == NULL)
6799 			continue;
6800 		taskqueue_free(sc->tq[i]);
6801 		sc->tq[i] = NULL;
6802 	}
6803 
6804 	sc->flags &= ~FULL_INIT_DONE;
6805 }
6806 
6807 #ifdef RSS
6808 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
6809     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
6810     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
6811     RSS_HASHTYPE_RSS_UDP_IPV6)
6812 
6813 /* Translates kernel hash types to hardware. */
6814 static int
hashconfig_to_hashen(int hashconfig)6815 hashconfig_to_hashen(int hashconfig)
6816 {
6817 	int hashen = 0;
6818 
6819 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
6820 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
6821 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
6822 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
6823 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
6824 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6825 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6826 	}
6827 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
6828 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6829 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6830 	}
6831 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
6832 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6833 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
6834 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6835 
6836 	return (hashen);
6837 }
6838 
6839 /* Translates hardware hash types to kernel. */
6840 static int
hashen_to_hashconfig(int hashen)6841 hashen_to_hashconfig(int hashen)
6842 {
6843 	int hashconfig = 0;
6844 
6845 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
6846 		/*
6847 		 * If UDP hashing was enabled it must have been enabled for
6848 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
6849 		 * enabling any 4-tuple hash is nonsense configuration.
6850 		 */
6851 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6852 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
6853 
6854 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6855 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
6856 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6857 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
6858 	}
6859 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6860 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
6861 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6862 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
6863 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
6864 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
6865 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
6866 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
6867 
6868 	return (hashconfig);
6869 }
6870 #endif
6871 
6872 /*
6873  * Idempotent.
6874  */
6875 static int
vi_full_init(struct vi_info * vi)6876 vi_full_init(struct vi_info *vi)
6877 {
6878 	struct adapter *sc = vi->adapter;
6879 	struct sge_rxq *rxq;
6880 	int rc, i, j;
6881 #ifdef RSS
6882 	int nbuckets = rss_getnumbuckets();
6883 	int hashconfig = rss_gethashconfig();
6884 	int extra;
6885 #endif
6886 
6887 	ASSERT_SYNCHRONIZED_OP(sc);
6888 
6889 	/*
6890 	 * Allocate tx/rx/fl queues for this VI.
6891 	 */
6892 	rc = t4_setup_vi_queues(vi);
6893 	if (rc != 0)
6894 		return (rc);
6895 
6896 	/*
6897 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
6898 	 */
6899 	if (vi->nrxq > vi->rss_size) {
6900 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
6901 		    "some queues will never receive traffic.\n", vi->nrxq,
6902 		    vi->rss_size);
6903 	} else if (vi->rss_size % vi->nrxq) {
6904 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
6905 		    "expect uneven traffic distribution.\n", vi->nrxq,
6906 		    vi->rss_size);
6907 	}
6908 #ifdef RSS
6909 	if (vi->nrxq != nbuckets) {
6910 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
6911 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
6912 	}
6913 #endif
6914 	if (vi->rss == NULL)
6915 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
6916 		    M_ZERO | M_WAITOK);
6917 	for (i = 0; i < vi->rss_size;) {
6918 #ifdef RSS
6919 		j = rss_get_indirection_to_bucket(i);
6920 		j %= vi->nrxq;
6921 		rxq = &sc->sge.rxq[vi->first_rxq + j];
6922 		vi->rss[i++] = rxq->iq.abs_id;
6923 #else
6924 		for_each_rxq(vi, j, rxq) {
6925 			vi->rss[i++] = rxq->iq.abs_id;
6926 			if (i == vi->rss_size)
6927 				break;
6928 		}
6929 #endif
6930 	}
6931 
6932 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
6933 	    vi->rss, vi->rss_size);
6934 	if (rc != 0) {
6935 		CH_ERR(vi, "rss_config failed: %d\n", rc);
6936 		return (rc);
6937 	}
6938 
6939 #ifdef RSS
6940 	vi->hashen = hashconfig_to_hashen(hashconfig);
6941 
6942 	/*
6943 	 * We may have had to enable some hashes even though the global config
6944 	 * wants them disabled.  This is a potential problem that must be
6945 	 * reported to the user.
6946 	 */
6947 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
6948 
6949 	/*
6950 	 * If we consider only the supported hash types, then the enabled hashes
6951 	 * are a superset of the requested hashes.  In other words, there cannot
6952 	 * be any supported hash that was requested but not enabled, but there
6953 	 * can be hashes that were not requested but had to be enabled.
6954 	 */
6955 	extra &= SUPPORTED_RSS_HASHTYPES;
6956 	MPASS((extra & hashconfig) == 0);
6957 
6958 	if (extra) {
6959 		CH_ALERT(vi,
6960 		    "global RSS config (0x%x) cannot be accommodated.\n",
6961 		    hashconfig);
6962 	}
6963 	if (extra & RSS_HASHTYPE_RSS_IPV4)
6964 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
6965 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
6966 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
6967 	if (extra & RSS_HASHTYPE_RSS_IPV6)
6968 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
6969 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
6970 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
6971 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
6972 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
6973 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
6974 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
6975 #else
6976 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
6977 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
6978 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6979 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
6980 #endif
6981 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
6982 	    0, 0);
6983 	if (rc != 0) {
6984 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
6985 		return (rc);
6986 	}
6987 
6988 	return (0);
6989 }
6990 
6991 int
vi_init(struct vi_info * vi)6992 vi_init(struct vi_info *vi)
6993 {
6994 	int rc;
6995 
6996 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
6997 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
6998 	    ("%s: VI_INIT_DONE already", __func__));
6999 
7000 	rc = vi_full_init(vi);
7001 	if (rc != 0)
7002 		vi_full_uninit(vi);
7003 	else
7004 		vi->flags |= VI_INIT_DONE;
7005 
7006 	return (rc);
7007 }
7008 
7009 /*
7010  * Idempotent.
7011  */
7012 static void
vi_full_uninit(struct vi_info * vi)7013 vi_full_uninit(struct vi_info *vi)
7014 {
7015 
7016 	if (vi->flags & VI_INIT_DONE) {
7017 		quiesce_vi(vi);
7018 		free(vi->rss, M_CXGBE);
7019 		free(vi->nm_rss, M_CXGBE);
7020 	}
7021 
7022 	t4_teardown_vi_queues(vi);
7023 	vi->flags &= ~VI_INIT_DONE;
7024 }
7025 
7026 static void
quiesce_txq(struct sge_txq * txq)7027 quiesce_txq(struct sge_txq *txq)
7028 {
7029 	struct sge_eq *eq = &txq->eq;
7030 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7031 
7032 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7033 	MPASS(!(eq->flags & EQ_ENABLED));
7034 
7035 	/* Wait for the mp_ring to empty. */
7036 	while (!mp_ring_is_idle(txq->r)) {
7037 		mp_ring_check_drainage(txq->r, 4096);
7038 		pause("rquiesce", 1);
7039 	}
7040 	MPASS(txq->txp.npkt == 0);
7041 
7042 	if (eq->flags & EQ_HW_ALLOCATED) {
7043 		/*
7044 		 * Hardware is alive and working normally.  Wait for it to
7045 		 * finish and then wait for the driver to catch up and reclaim
7046 		 * all descriptors.
7047 		 */
7048 		while (spg->cidx != htobe16(eq->pidx))
7049 			pause("equiesce", 1);
7050 		while (eq->cidx != eq->pidx)
7051 			pause("dquiesce", 1);
7052 	} else {
7053 		/*
7054 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7055 		 * descriptors directly.
7056 		 */
7057 		TXQ_LOCK(txq);
7058 		while (eq->cidx != eq->pidx) {
7059 			struct mbuf *m, *nextpkt;
7060 			struct tx_sdesc *txsd;
7061 
7062 			txsd = &txq->sdesc[eq->cidx];
7063 			for (m = txsd->m; m != NULL; m = nextpkt) {
7064 				nextpkt = m->m_nextpkt;
7065 				m->m_nextpkt = NULL;
7066 				m_freem(m);
7067 			}
7068 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7069 		}
7070 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7071 		TXQ_UNLOCK(txq);
7072 	}
7073 }
7074 
7075 static void
quiesce_wrq(struct sge_wrq * wrq)7076 quiesce_wrq(struct sge_wrq *wrq)
7077 {
7078 
7079 	/* XXXTX */
7080 }
7081 
7082 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7083 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7084 {
7085 	/* Synchronize with the interrupt handler */
7086 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7087 		pause("iqfree", 1);
7088 
7089 	if (fl != NULL) {
7090 		MPASS(iq->flags & IQ_HAS_FL);
7091 
7092 		mtx_lock(&sc->sfl_lock);
7093 		FL_LOCK(fl);
7094 		fl->flags |= FL_DOOMED;
7095 		FL_UNLOCK(fl);
7096 		callout_stop(&sc->sfl_callout);
7097 		mtx_unlock(&sc->sfl_lock);
7098 
7099 		KASSERT((fl->flags & FL_STARVING) == 0,
7100 		    ("%s: still starving", __func__));
7101 
7102 		/* Release all buffers if hardware is no longer available. */
7103 		if (!(iq->flags & IQ_HW_ALLOCATED))
7104 			free_fl_buffers(sc, fl);
7105 	}
7106 }
7107 
7108 /*
7109  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7110  * that no new work is being enqueued by the hardware or the driver.  That part
7111  * should be arranged before calling this function.
7112  */
7113 static void
quiesce_vi(struct vi_info * vi)7114 quiesce_vi(struct vi_info *vi)
7115 {
7116 	int i;
7117 	struct adapter *sc = vi->adapter;
7118 	struct sge_rxq *rxq;
7119 	struct sge_txq *txq;
7120 #ifdef TCP_OFFLOAD
7121 	struct sge_ofld_rxq *ofld_rxq;
7122 #endif
7123 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7124 	struct sge_ofld_txq *ofld_txq;
7125 #endif
7126 
7127 	if (!(vi->flags & VI_INIT_DONE))
7128 		return;
7129 
7130 	for_each_txq(vi, i, txq) {
7131 		quiesce_txq(txq);
7132 	}
7133 
7134 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7135 	for_each_ofld_txq(vi, i, ofld_txq) {
7136 		quiesce_wrq(&ofld_txq->wrq);
7137 	}
7138 #endif
7139 
7140 	for_each_rxq(vi, i, rxq) {
7141 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7142 	}
7143 
7144 #ifdef TCP_OFFLOAD
7145 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7146 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7147 	}
7148 #endif
7149 }
7150 
7151 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7152 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7153     driver_intr_t *handler, void *arg, char *name)
7154 {
7155 	int rc;
7156 
7157 	irq->rid = rid;
7158 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7159 	    RF_SHAREABLE | RF_ACTIVE);
7160 	if (irq->res == NULL) {
7161 		device_printf(sc->dev,
7162 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7163 		return (ENOMEM);
7164 	}
7165 
7166 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7167 	    NULL, handler, arg, &irq->tag);
7168 	if (rc != 0) {
7169 		device_printf(sc->dev,
7170 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7171 		    rid, name, rc);
7172 	} else if (name)
7173 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7174 
7175 	return (rc);
7176 }
7177 
7178 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7179 t4_free_irq(struct adapter *sc, struct irq *irq)
7180 {
7181 	if (irq->tag)
7182 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7183 	if (irq->res)
7184 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7185 
7186 	bzero(irq, sizeof(*irq));
7187 
7188 	return (0);
7189 }
7190 
7191 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7192 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7193 {
7194 
7195 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7196 	t4_get_regs(sc, buf, regs->len);
7197 }
7198 
7199 #define	A_PL_INDIR_CMD	0x1f8
7200 
7201 #define	S_PL_AUTOINC	31
7202 #define	M_PL_AUTOINC	0x1U
7203 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7204 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7205 
7206 #define	S_PL_VFID	20
7207 #define	M_PL_VFID	0xffU
7208 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7209 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7210 
7211 #define	S_PL_ADDR	0
7212 #define	M_PL_ADDR	0xfffffU
7213 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7214 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7215 
7216 #define	A_PL_INDIR_DATA	0x1fc
7217 
7218 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7219 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7220 {
7221 	u32 stats[2];
7222 
7223 	if (sc->flags & IS_VF) {
7224 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7225 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7226 	} else {
7227 		mtx_assert(&sc->reg_lock, MA_OWNED);
7228 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7229 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7230 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7231 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7232 	}
7233 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7234 }
7235 
7236 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7237 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7238 {
7239 
7240 #define GET_STAT(name) \
7241 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7242 
7243 	if (!(sc->flags & IS_VF))
7244 		mtx_lock(&sc->reg_lock);
7245 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7246 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7247 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7248 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7249 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7250 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7251 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7252 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7253 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7254 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7255 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7256 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7257 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7258 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7259 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7260 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7261 	if (!(sc->flags & IS_VF))
7262 		mtx_unlock(&sc->reg_lock);
7263 
7264 #undef GET_STAT
7265 }
7266 
7267 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7268 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7269 {
7270 	int reg;
7271 
7272 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7273 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7274 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7275 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7276 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7277 }
7278 
7279 static void
vi_refresh_stats(struct vi_info * vi)7280 vi_refresh_stats(struct vi_info *vi)
7281 {
7282 	struct timeval tv;
7283 	const struct timeval interval = {0, 250000};	/* 250ms */
7284 
7285 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7286 
7287 	if (vi->flags & VI_SKIP_STATS)
7288 		return;
7289 
7290 	getmicrotime(&tv);
7291 	timevalsub(&tv, &interval);
7292 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7293 		return;
7294 
7295 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7296 	getmicrotime(&vi->last_refreshed);
7297 }
7298 
7299 static void
cxgbe_refresh_stats(struct vi_info * vi)7300 cxgbe_refresh_stats(struct vi_info *vi)
7301 {
7302 	u_int i, v, tnl_cong_drops, chan_map;
7303 	struct timeval tv;
7304 	const struct timeval interval = {0, 250000};	/* 250ms */
7305 	struct port_info *pi;
7306 	struct adapter *sc;
7307 
7308 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7309 
7310 	if (vi->flags & VI_SKIP_STATS)
7311 		return;
7312 
7313 	getmicrotime(&tv);
7314 	timevalsub(&tv, &interval);
7315 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7316 		return;
7317 
7318 	pi = vi->pi;
7319 	sc = vi->adapter;
7320 	tnl_cong_drops = 0;
7321 	t4_get_port_stats(sc, pi->port_id, &pi->stats);
7322 	chan_map = pi->rx_e_chan_map;
7323 	while (chan_map) {
7324 		i = ffs(chan_map) - 1;
7325 		mtx_lock(&sc->reg_lock);
7326 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7327 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7328 		mtx_unlock(&sc->reg_lock);
7329 		tnl_cong_drops += v;
7330 		chan_map &= ~(1 << i);
7331 	}
7332 	pi->tnl_cong_drops = tnl_cong_drops;
7333 	getmicrotime(&vi->last_refreshed);
7334 }
7335 
7336 static void
cxgbe_tick(void * arg)7337 cxgbe_tick(void *arg)
7338 {
7339 	struct vi_info *vi = arg;
7340 
7341 	MPASS(IS_MAIN_VI(vi));
7342 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7343 
7344 	cxgbe_refresh_stats(vi);
7345 	callout_schedule(&vi->tick, hz);
7346 }
7347 
7348 static void
vi_tick(void * arg)7349 vi_tick(void *arg)
7350 {
7351 	struct vi_info *vi = arg;
7352 
7353 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7354 
7355 	vi_refresh_stats(vi);
7356 	callout_schedule(&vi->tick, hz);
7357 }
7358 
7359 /*
7360  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7361  */
7362 static char *caps_decoder[] = {
7363 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7364 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7365 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7366 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7367 	    "\006HASHFILTER\007ETHOFLD",
7368 	"\20\001TOE",					/* 4: TOE */
7369 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
7370 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7371 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7372 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7373 	    "\007T10DIF"
7374 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7375 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7376 	    "\004TLS_HW",
7377 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7378 		    "\004PO_INITIATOR\005PO_TARGET",
7379 };
7380 
7381 void
t4_sysctls(struct adapter * sc)7382 t4_sysctls(struct adapter *sc)
7383 {
7384 	struct sysctl_ctx_list *ctx = &sc->ctx;
7385 	struct sysctl_oid *oid;
7386 	struct sysctl_oid_list *children, *c0;
7387 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7388 
7389 	/*
7390 	 * dev.t4nex.X.
7391 	 */
7392 	oid = device_get_sysctl_tree(sc->dev);
7393 	c0 = children = SYSCTL_CHILDREN(oid);
7394 
7395 	sc->sc_do_rxcopy = 1;
7396 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7397 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7398 
7399 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7400 	    sc->params.nports, "# of ports");
7401 
7402 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7403 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7404 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7405 	    "available doorbells");
7406 
7407 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7408 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7409 
7410 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7411 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7412 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7413 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7414 
7415 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7416 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7417 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7418 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7419 
7420 	t4_sge_sysctls(sc, ctx, children);
7421 
7422 	sc->lro_timeout = 100;
7423 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7424 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7425 
7426 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7427 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7428 
7429 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7430 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7431 
7432 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7433 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7434 
7435 	if (sc->flags & IS_VF)
7436 		return;
7437 
7438 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7439 	    NULL, chip_rev(sc), "chip hardware revision");
7440 
7441 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7442 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7443 
7444 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7445 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7446 
7447 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7448 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7449 
7450 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7451 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7452 
7453 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7454 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7455 
7456 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7457 	    sc->er_version, 0, "expansion ROM version");
7458 
7459 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7460 	    sc->bs_version, 0, "bootstrap firmware version");
7461 
7462 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7463 	    NULL, sc->params.scfg_vers, "serial config version");
7464 
7465 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7466 	    NULL, sc->params.vpd_vers, "VPD version");
7467 
7468 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7469 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7470 
7471 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7472 	    sc->cfcsum, "config file checksum");
7473 
7474 #define SYSCTL_CAP(name, n, text) \
7475 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7476 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7477 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7478 	    "available " text " capabilities")
7479 
7480 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7481 	SYSCTL_CAP(linkcaps, 1, "link");
7482 	SYSCTL_CAP(switchcaps, 2, "switch");
7483 	SYSCTL_CAP(niccaps, 3, "NIC");
7484 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7485 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7486 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7487 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7488 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7489 #undef SYSCTL_CAP
7490 
7491 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7492 	    NULL, sc->tids.nftids, "number of filters");
7493 
7494 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7495 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7496 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7497 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7498 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7499 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7500 
7501 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg",
7502 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7503 	    sysctl_loadavg, "A",
7504 	    "microprocessor load averages (debug firmwares only)");
7505 
7506 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7507 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7508 	    "I", "core Vdd (in mV)");
7509 
7510 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7511 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7512 	    sysctl_cpus, "A", "local CPUs");
7513 
7514 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7515 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7516 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7517 
7518 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7519 	    &sc->swintr, 0, "software triggered interrupts");
7520 
7521 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7522 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7523 	    "1 = reset adapter, 0 = zero reset counter");
7524 
7525 	/*
7526 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7527 	 */
7528 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7529 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
7530 	    "logs and miscellaneous information");
7531 	children = SYSCTL_CHILDREN(oid);
7532 
7533 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
7534 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7535 	    sysctl_cctrl, "A", "congestion control");
7536 
7537 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
7538 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7539 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
7540 
7541 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
7542 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7543 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
7544 
7545 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
7546 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7547 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
7548 
7549 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
7550 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 3,
7551 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
7552 
7553 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
7554 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 4,
7555 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
7556 
7557 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
7558 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 5,
7559 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
7560 
7561 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
7562 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7563 	    sysctl_cim_la, "A", "CIM logic analyzer");
7564 
7565 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
7566 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7567 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7568 
7569 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
7570 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7571 	    0 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
7572 
7573 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
7574 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7575 	    1 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
7576 
7577 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
7578 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7579 	    2 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
7580 
7581 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
7582 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7583 	    3 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
7584 
7585 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
7586 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7587 	    4 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
7588 
7589 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
7590 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7591 	    5 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
7592 
7593 	if (chip_id(sc) > CHELSIO_T4) {
7594 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
7595 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7596 		    6 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7597 		    "CIM OBQ 6 (SGE0-RX)");
7598 
7599 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
7600 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7601 		    7 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7602 		    "CIM OBQ 7 (SGE1-RX)");
7603 	}
7604 
7605 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
7606 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7607 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7608 
7609 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
7610 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7611 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
7612 
7613 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
7614 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7615 	    sysctl_cpl_stats, "A", "CPL statistics");
7616 
7617 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
7618 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7619 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
7620 
7621 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
7622 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7623 	    sysctl_tid_stats, "A", "tid stats");
7624 
7625 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
7626 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7627 	    sysctl_devlog, "A", "firmware's device log");
7628 
7629 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
7630 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7631 	    sysctl_fcoe_stats, "A", "FCoE statistics");
7632 
7633 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
7634 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7635 	    sysctl_hw_sched, "A", "hardware scheduler ");
7636 
7637 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
7638 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7639 	    sysctl_l2t, "A", "hardware L2 table");
7640 
7641 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
7642 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7643 	    sysctl_smt, "A", "hardware source MAC table");
7644 
7645 #ifdef INET6
7646 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
7647 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7648 	    sysctl_clip, "A", "active CLIP table entries");
7649 #endif
7650 
7651 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
7652 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7653 	    sysctl_lb_stats, "A", "loopback statistics");
7654 
7655 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
7656 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7657 	    sysctl_meminfo, "A", "memory regions");
7658 
7659 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
7660 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7661 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
7662 	    "A", "MPS TCAM entries");
7663 
7664 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
7665 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7666 	    sysctl_path_mtus, "A", "path MTUs");
7667 
7668 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
7669 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7670 	    sysctl_pm_stats, "A", "PM statistics");
7671 
7672 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
7673 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7674 	    sysctl_rdma_stats, "A", "RDMA statistics");
7675 
7676 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
7677 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7678 	    sysctl_tcp_stats, "A", "TCP statistics");
7679 
7680 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
7681 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7682 	    sysctl_tids, "A", "TID information");
7683 
7684 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
7685 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7686 	    sysctl_tp_err_stats, "A", "TP error statistics");
7687 
7688 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
7689 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7690 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
7691 
7692 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
7693 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7694 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
7695 
7696 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
7697 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7698 	    sysctl_tp_la, "A", "TP logic analyzer");
7699 
7700 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
7701 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7702 	    sysctl_tx_rate, "A", "Tx rate");
7703 
7704 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
7705 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7706 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
7707 
7708 	if (chip_id(sc) >= CHELSIO_T5) {
7709 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
7710 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7711 		    sysctl_wcwr_stats, "A", "write combined work requests");
7712 	}
7713 
7714 #ifdef KERN_TLS
7715 	if (is_ktls(sc)) {
7716 		/*
7717 		 * dev.t4nex.0.tls.
7718 		 */
7719 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
7720 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
7721 		children = SYSCTL_CHILDREN(oid);
7722 
7723 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
7724 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
7725 		    "keys in work requests (1) or attempt to store TLS keys "
7726 		    "in card memory.");
7727 
7728 		if (is_t6(sc))
7729 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
7730 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
7731 			    "combine TCB field updates with TLS record work "
7732 			    "requests.");
7733 	}
7734 #endif
7735 
7736 #ifdef TCP_OFFLOAD
7737 	if (is_offload(sc)) {
7738 		int i;
7739 		char s[4];
7740 
7741 		/*
7742 		 * dev.t4nex.X.toe.
7743 		 */
7744 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
7745 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
7746 		children = SYSCTL_CHILDREN(oid);
7747 
7748 		sc->tt.cong_algorithm = -1;
7749 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
7750 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
7751 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
7752 		    "3 = highspeed)");
7753 
7754 		sc->tt.sndbuf = -1;
7755 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
7756 		    &sc->tt.sndbuf, 0, "hardware send buffer");
7757 
7758 		sc->tt.ddp = 0;
7759 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
7760 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
7761 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
7762 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
7763 
7764 		sc->tt.rx_coalesce = -1;
7765 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
7766 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
7767 
7768 		sc->tt.tls = 0;
7769 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
7770 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
7771 		    "Inline TLS allowed");
7772 
7773 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls_rx_ports",
7774 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7775 		    sysctl_tls_rx_ports, "I",
7776 		    "TCP ports that use inline TLS+TOE RX");
7777 
7778 		sc->tt.tls_rx_timeout = t4_toe_tls_rx_timeout;
7779 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls_rx_timeout",
7780 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7781 		    sysctl_tls_rx_timeout, "I",
7782 		    "Timeout in seconds to downgrade TLS sockets to plain TOE");
7783 
7784 		sc->tt.tx_align = -1;
7785 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
7786 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
7787 
7788 		sc->tt.tx_zcopy = 0;
7789 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
7790 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
7791 		    "Enable zero-copy aio_write(2)");
7792 
7793 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
7794 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7795 		    "cop_managed_offloading", CTLFLAG_RW,
7796 		    &sc->tt.cop_managed_offloading, 0,
7797 		    "COP (Connection Offload Policy) controls all TOE offload");
7798 
7799 		sc->tt.autorcvbuf_inc = 16 * 1024;
7800 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
7801 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
7802 		    "autorcvbuf increment");
7803 
7804 		sc->tt.iso = 1;
7805 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
7806 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
7807 
7808 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
7809 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7810 		    sysctl_tp_tick, "A", "TP timer tick (us)");
7811 
7812 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
7813 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7814 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
7815 
7816 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
7817 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7818 		    sysctl_tp_tick, "A", "DACK tick (us)");
7819 
7820 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
7821 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7822 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
7823 
7824 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
7825 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7826 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
7827 		    "Minimum retransmit interval (us)");
7828 
7829 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
7830 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7831 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
7832 		    "Maximum retransmit interval (us)");
7833 
7834 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
7835 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7836 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
7837 		    "Persist timer min (us)");
7838 
7839 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
7840 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7841 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
7842 		    "Persist timer max (us)");
7843 
7844 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
7845 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7846 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
7847 		    "Keepalive idle timer (us)");
7848 
7849 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
7850 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7851 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
7852 		    "Keepalive interval timer (us)");
7853 
7854 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
7855 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7856 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
7857 
7858 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
7859 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7860 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
7861 		    "FINWAIT2 timer (us)");
7862 
7863 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
7864 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7865 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
7866 		    "Number of SYN retransmissions before abort");
7867 
7868 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
7869 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7870 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
7871 		    "Number of retransmissions before abort");
7872 
7873 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
7874 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7875 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
7876 		    "Number of keepalive probes before abort");
7877 
7878 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
7879 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7880 		    "TOE retransmit backoffs");
7881 		children = SYSCTL_CHILDREN(oid);
7882 		for (i = 0; i < 16; i++) {
7883 			snprintf(s, sizeof(s), "%u", i);
7884 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
7885 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7886 			    i, sysctl_tp_backoff, "IU",
7887 			    "TOE retransmit backoff");
7888 		}
7889 	}
7890 #endif
7891 }
7892 
7893 void
vi_sysctls(struct vi_info * vi)7894 vi_sysctls(struct vi_info *vi)
7895 {
7896 	struct sysctl_ctx_list *ctx = &vi->ctx;
7897 	struct sysctl_oid *oid;
7898 	struct sysctl_oid_list *children;
7899 
7900 	/*
7901 	 * dev.v?(cxgbe|cxl).X.
7902 	 */
7903 	oid = device_get_sysctl_tree(vi->dev);
7904 	children = SYSCTL_CHILDREN(oid);
7905 
7906 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
7907 	    vi->viid, "VI identifer");
7908 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
7909 	    &vi->nrxq, 0, "# of rx queues");
7910 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
7911 	    &vi->ntxq, 0, "# of tx queues");
7912 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
7913 	    &vi->first_rxq, 0, "index of first rx queue");
7914 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
7915 	    &vi->first_txq, 0, "index of first tx queue");
7916 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
7917 	    vi->rss_base, "start of RSS indirection table");
7918 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
7919 	    vi->rss_size, "size of RSS indirection table");
7920 
7921 	if (IS_MAIN_VI(vi)) {
7922 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
7923 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7924 		    sysctl_noflowq, "IU",
7925 		    "Reserve queue 0 for non-flowid packets");
7926 	}
7927 
7928 	if (vi->adapter->flags & IS_VF) {
7929 		MPASS(vi->flags & TX_USES_VM_WR);
7930 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
7931 		    NULL, 1, "use VM work requests for transmit");
7932 	} else {
7933 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
7934 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7935 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
7936 	}
7937 
7938 #ifdef TCP_OFFLOAD
7939 	if (vi->nofldrxq != 0) {
7940 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
7941 		    &vi->nofldrxq, 0,
7942 		    "# of rx queues for offloaded TCP connections");
7943 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
7944 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
7945 		    "index of first TOE rx queue");
7946 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
7947 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7948 		    sysctl_holdoff_tmr_idx_ofld, "I",
7949 		    "holdoff timer index for TOE queues");
7950 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
7951 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7952 		    sysctl_holdoff_pktc_idx_ofld, "I",
7953 		    "holdoff packet counter index for TOE queues");
7954 	}
7955 #endif
7956 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7957 	if (vi->nofldtxq != 0) {
7958 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
7959 		    &vi->nofldtxq, 0,
7960 		    "# of tx queues for TOE/ETHOFLD");
7961 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
7962 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
7963 		    "index of first TOE/ETHOFLD tx queue");
7964 	}
7965 #endif
7966 #ifdef DEV_NETMAP
7967 	if (vi->nnmrxq != 0) {
7968 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
7969 		    &vi->nnmrxq, 0, "# of netmap rx queues");
7970 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
7971 		    &vi->nnmtxq, 0, "# of netmap tx queues");
7972 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
7973 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
7974 		    "index of first netmap rx queue");
7975 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
7976 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
7977 		    "index of first netmap tx queue");
7978 	}
7979 #endif
7980 
7981 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
7982 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7983 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
7984 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
7985 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7986 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
7987 
7988 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
7989 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7990 	    sysctl_qsize_rxq, "I", "rx queue size");
7991 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
7992 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7993 	    sysctl_qsize_txq, "I", "tx queue size");
7994 }
7995 
7996 static void
cxgbe_sysctls(struct port_info * pi)7997 cxgbe_sysctls(struct port_info *pi)
7998 {
7999 	struct sysctl_ctx_list *ctx = &pi->ctx;
8000 	struct sysctl_oid *oid;
8001 	struct sysctl_oid_list *children, *children2;
8002 	struct adapter *sc = pi->adapter;
8003 	int i;
8004 	char name[16];
8005 	static char *tc_flags = {"\20\1USER"};
8006 
8007 	/*
8008 	 * dev.cxgbe.X.
8009 	 */
8010 	oid = device_get_sysctl_tree(pi->dev);
8011 	children = SYSCTL_CHILDREN(oid);
8012 
8013 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8014 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8015 	    sysctl_linkdnrc, "A", "reason why link is down");
8016 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8017 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8018 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8019 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8020 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8021 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8022 		    sysctl_btphy, "I", "PHY firmware version");
8023 	}
8024 
8025 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8026 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8027 	    sysctl_pause_settings, "A",
8028 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8029 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8030 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8031 	    "FEC in use on the link");
8032 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8033 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8034 	    sysctl_requested_fec, "A",
8035 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8036 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8037 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8038 	    "FEC recommended by the cable/transceiver");
8039 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8040 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8041 	    sysctl_autoneg, "I",
8042 	    "autonegotiation (-1 = not supported)");
8043 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8044 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8045 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8046 
8047 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8048 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8049 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8050 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8051 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8052 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8053 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8054 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8055 
8056 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8057 	    port_top_speed(pi), "max speed (in Gbps)");
8058 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8059 	    pi->mps_bg_map, "MPS buffer group map");
8060 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8061 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8062 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8063 	    pi->tx_chan, "TP tx c-channel");
8064 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8065 	    pi->rx_chan, "TP rx c-channel");
8066 
8067 	if (sc->flags & IS_VF)
8068 		return;
8069 
8070 	/*
8071 	 * dev.(cxgbe|cxl).X.tc.
8072 	 */
8073 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8074 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8075 	    "Tx scheduler traffic classes (cl_rl)");
8076 	children2 = SYSCTL_CHILDREN(oid);
8077 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8078 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8079 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8080 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8081 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8082 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8083 	for (i = 0; i < sc->params.nsched_cls; i++) {
8084 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8085 
8086 		snprintf(name, sizeof(name), "%d", i);
8087 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8088 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8089 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8090 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8091 		    CTLFLAG_RD, &tc->state, 0, "current state");
8092 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8093 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8094 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8095 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8096 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8097 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8098 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8099 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8100 		    "traffic class parameters");
8101 	}
8102 
8103 	/*
8104 	 * dev.cxgbe.X.stats.
8105 	 */
8106 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8107 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8108 	children = SYSCTL_CHILDREN(oid);
8109 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8110 	    &pi->tx_parse_error, 0,
8111 	    "# of tx packets with invalid length or # of segments");
8112 
8113 #define T4_REGSTAT(name, stat, desc) \
8114     SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8115 	CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8116 	t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8117         sysctl_handle_t4_reg64, "QU", desc)
8118 
8119 /* We get these from port_stats and they may be stale by up to 1s */
8120 #define T4_PORTSTAT(name, desc) \
8121 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
8122 	    &pi->stats.name, desc)
8123 
8124 	T4_REGSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8125 	T4_REGSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8126 	T4_REGSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8127 	T4_REGSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8128 	T4_REGSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8129 	T4_REGSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8130 	T4_REGSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8131 	T4_REGSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8132 	T4_REGSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8133 	T4_REGSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8134 	T4_REGSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8135 	T4_REGSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8136 	T4_REGSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8137 	T4_REGSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8138 	T4_REGSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8139 	T4_REGSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8140 	T4_REGSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8141 	T4_REGSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8142 	T4_REGSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8143 	T4_REGSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8144 	T4_REGSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8145 	T4_REGSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8146 	T4_REGSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8147 
8148 	T4_REGSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8149 	T4_REGSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8150 	T4_REGSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8151 	T4_REGSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8152 	T4_REGSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8153 	T4_REGSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8154 	T4_REGSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8155 	if (is_t6(sc)) {
8156 		T4_PORTSTAT(rx_fcs_err,
8157 		    "# of frames received with bad FCS since last link up");
8158 	} else {
8159 		T4_REGSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8160 		    "# of frames received with bad FCS");
8161 	}
8162 	T4_REGSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8163 	T4_REGSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8164 	T4_REGSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8165 	T4_REGSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8166 	T4_REGSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8167 	T4_REGSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8168 	T4_REGSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8169 	T4_REGSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8170 	T4_REGSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8171 	T4_REGSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8172 	T4_REGSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8173 	T4_REGSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8174 	T4_REGSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8175 	T4_REGSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8176 	T4_REGSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8177 	T4_REGSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8178 	T4_REGSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8179 	T4_REGSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8180 	T4_REGSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8181 
8182 	T4_PORTSTAT(rx_ovflow0, "# drops due to buffer-group 0 overflows");
8183 	T4_PORTSTAT(rx_ovflow1, "# drops due to buffer-group 1 overflows");
8184 	T4_PORTSTAT(rx_ovflow2, "# drops due to buffer-group 2 overflows");
8185 	T4_PORTSTAT(rx_ovflow3, "# drops due to buffer-group 3 overflows");
8186 	T4_PORTSTAT(rx_trunc0, "# of buffer-group 0 truncated packets");
8187 	T4_PORTSTAT(rx_trunc1, "# of buffer-group 1 truncated packets");
8188 	T4_PORTSTAT(rx_trunc2, "# of buffer-group 2 truncated packets");
8189 	T4_PORTSTAT(rx_trunc3, "# of buffer-group 3 truncated packets");
8190 
8191 #undef T4_REGSTAT
8192 #undef T4_PORTSTAT
8193 }
8194 
8195 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8196 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8197 {
8198 	int rc, *i, space = 0;
8199 	struct sbuf sb;
8200 
8201 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8202 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8203 		if (space)
8204 			sbuf_printf(&sb, " ");
8205 		sbuf_printf(&sb, "%d", *i);
8206 		space = 1;
8207 	}
8208 	rc = sbuf_finish(&sb);
8209 	sbuf_delete(&sb);
8210 	return (rc);
8211 }
8212 
8213 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8214 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8215 {
8216 	int rc;
8217 	struct sbuf *sb;
8218 
8219 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8220 	if (sb == NULL)
8221 		return (ENOMEM);
8222 
8223 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8224 	rc = sbuf_finish(sb);
8225 	sbuf_delete(sb);
8226 
8227 	return (rc);
8228 }
8229 
8230 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8231 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8232 {
8233 	int rc;
8234 	struct sbuf *sb;
8235 
8236 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8237 	if (sb == NULL)
8238 		return (ENOMEM);
8239 
8240 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8241 	rc = sbuf_finish(sb);
8242 	sbuf_delete(sb);
8243 
8244 	return (rc);
8245 }
8246 
8247 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8248 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8249 {
8250 	struct port_info *pi = arg1;
8251 	int op = arg2;
8252 	struct adapter *sc = pi->adapter;
8253 	u_int v;
8254 	int rc;
8255 
8256 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8257 	if (rc)
8258 		return (rc);
8259 	if (hw_off_limits(sc))
8260 		rc = ENXIO;
8261 	else {
8262 		/* XXX: magic numbers */
8263 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8264 		    op ? 0x20 : 0xc820, &v);
8265 	}
8266 	end_synchronized_op(sc, 0);
8267 	if (rc)
8268 		return (rc);
8269 	if (op == 0)
8270 		v /= 256;
8271 
8272 	rc = sysctl_handle_int(oidp, &v, 0, req);
8273 	return (rc);
8274 }
8275 
8276 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8277 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8278 {
8279 	struct vi_info *vi = arg1;
8280 	int rc, val;
8281 
8282 	val = vi->rsrv_noflowq;
8283 	rc = sysctl_handle_int(oidp, &val, 0, req);
8284 	if (rc != 0 || req->newptr == NULL)
8285 		return (rc);
8286 
8287 	if ((val >= 1) && (vi->ntxq > 1))
8288 		vi->rsrv_noflowq = 1;
8289 	else
8290 		vi->rsrv_noflowq = 0;
8291 
8292 	return (rc);
8293 }
8294 
8295 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8296 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8297 {
8298 	struct vi_info *vi = arg1;
8299 	struct adapter *sc = vi->adapter;
8300 	int rc, val, i;
8301 
8302 	MPASS(!(sc->flags & IS_VF));
8303 
8304 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8305 	rc = sysctl_handle_int(oidp, &val, 0, req);
8306 	if (rc != 0 || req->newptr == NULL)
8307 		return (rc);
8308 
8309 	if (val != 0 && val != 1)
8310 		return (EINVAL);
8311 
8312 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8313 	    "t4txvm");
8314 	if (rc)
8315 		return (rc);
8316 	if (hw_off_limits(sc))
8317 		rc = ENXIO;
8318 	else if (vi->ifp->if_drv_flags & IFF_DRV_RUNNING) {
8319 		/*
8320 		 * We don't want parse_pkt to run with one setting (VF or PF)
8321 		 * and then eth_tx to see a different setting but still use
8322 		 * stale information calculated by parse_pkt.
8323 		 */
8324 		rc = EBUSY;
8325 	} else {
8326 		struct port_info *pi = vi->pi;
8327 		struct sge_txq *txq;
8328 		uint32_t ctrl0;
8329 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8330 
8331 		if (val) {
8332 			vi->flags |= TX_USES_VM_WR;
8333 			vi->ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_VM_TSO;
8334 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8335 			    V_TXPKT_INTF(pi->tx_chan));
8336 			if (!(sc->flags & IS_VF))
8337 				npkt--;
8338 		} else {
8339 			vi->flags &= ~TX_USES_VM_WR;
8340 			vi->ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_TSO;
8341 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8342 			    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
8343 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8344 		}
8345 		for_each_txq(vi, i, txq) {
8346 			txq->cpl_ctrl0 = ctrl0;
8347 			txq->txp.max_npkt = npkt;
8348 		}
8349 	}
8350 	end_synchronized_op(sc, LOCK_HELD);
8351 	return (rc);
8352 }
8353 
8354 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8355 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8356 {
8357 	struct vi_info *vi = arg1;
8358 	struct adapter *sc = vi->adapter;
8359 	int idx, rc, i;
8360 	struct sge_rxq *rxq;
8361 	uint8_t v;
8362 
8363 	idx = vi->tmr_idx;
8364 
8365 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8366 	if (rc != 0 || req->newptr == NULL)
8367 		return (rc);
8368 
8369 	if (idx < 0 || idx >= SGE_NTIMERS)
8370 		return (EINVAL);
8371 
8372 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8373 	    "t4tmr");
8374 	if (rc)
8375 		return (rc);
8376 
8377 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8378 	for_each_rxq(vi, i, rxq) {
8379 #ifdef atomic_store_rel_8
8380 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8381 #else
8382 		rxq->iq.intr_params = v;
8383 #endif
8384 	}
8385 	vi->tmr_idx = idx;
8386 
8387 	end_synchronized_op(sc, LOCK_HELD);
8388 	return (0);
8389 }
8390 
8391 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8392 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8393 {
8394 	struct vi_info *vi = arg1;
8395 	struct adapter *sc = vi->adapter;
8396 	int idx, rc;
8397 
8398 	idx = vi->pktc_idx;
8399 
8400 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8401 	if (rc != 0 || req->newptr == NULL)
8402 		return (rc);
8403 
8404 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8405 		return (EINVAL);
8406 
8407 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8408 	    "t4pktc");
8409 	if (rc)
8410 		return (rc);
8411 
8412 	if (vi->flags & VI_INIT_DONE)
8413 		rc = EBUSY; /* cannot be changed once the queues are created */
8414 	else
8415 		vi->pktc_idx = idx;
8416 
8417 	end_synchronized_op(sc, LOCK_HELD);
8418 	return (rc);
8419 }
8420 
8421 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8422 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8423 {
8424 	struct vi_info *vi = arg1;
8425 	struct adapter *sc = vi->adapter;
8426 	int qsize, rc;
8427 
8428 	qsize = vi->qsize_rxq;
8429 
8430 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8431 	if (rc != 0 || req->newptr == NULL)
8432 		return (rc);
8433 
8434 	if (qsize < 128 || (qsize & 7))
8435 		return (EINVAL);
8436 
8437 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8438 	    "t4rxqs");
8439 	if (rc)
8440 		return (rc);
8441 
8442 	if (vi->flags & VI_INIT_DONE)
8443 		rc = EBUSY; /* cannot be changed once the queues are created */
8444 	else
8445 		vi->qsize_rxq = qsize;
8446 
8447 	end_synchronized_op(sc, LOCK_HELD);
8448 	return (rc);
8449 }
8450 
8451 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8452 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8453 {
8454 	struct vi_info *vi = arg1;
8455 	struct adapter *sc = vi->adapter;
8456 	int qsize, rc;
8457 
8458 	qsize = vi->qsize_txq;
8459 
8460 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8461 	if (rc != 0 || req->newptr == NULL)
8462 		return (rc);
8463 
8464 	if (qsize < 128 || qsize > 65536)
8465 		return (EINVAL);
8466 
8467 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8468 	    "t4txqs");
8469 	if (rc)
8470 		return (rc);
8471 
8472 	if (vi->flags & VI_INIT_DONE)
8473 		rc = EBUSY; /* cannot be changed once the queues are created */
8474 	else
8475 		vi->qsize_txq = qsize;
8476 
8477 	end_synchronized_op(sc, LOCK_HELD);
8478 	return (rc);
8479 }
8480 
8481 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8482 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8483 {
8484 	struct port_info *pi = arg1;
8485 	struct adapter *sc = pi->adapter;
8486 	struct link_config *lc = &pi->link_cfg;
8487 	int rc;
8488 
8489 	if (req->newptr == NULL) {
8490 		struct sbuf *sb;
8491 		static char *bits = "\20\1RX\2TX\3AUTO";
8492 
8493 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8494 		if (sb == NULL)
8495 			return (ENOMEM);
8496 
8497 		if (lc->link_ok) {
8498 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8499 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8500 		} else {
8501 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8502 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8503 		}
8504 		rc = sbuf_finish(sb);
8505 		sbuf_delete(sb);
8506 	} else {
8507 		char s[2];
8508 		int n;
8509 
8510 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8511 		    PAUSE_AUTONEG));
8512 		s[1] = 0;
8513 
8514 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8515 		if (rc != 0)
8516 			return(rc);
8517 
8518 		if (s[1] != 0)
8519 			return (EINVAL);
8520 		if (s[0] < '0' || s[0] > '9')
8521 			return (EINVAL);	/* not a number */
8522 		n = s[0] - '0';
8523 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8524 			return (EINVAL);	/* some other bit is set too */
8525 
8526 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8527 		    "t4PAUSE");
8528 		if (rc)
8529 			return (rc);
8530 		if (!hw_off_limits(sc)) {
8531 			PORT_LOCK(pi);
8532 			lc->requested_fc = n;
8533 			fixup_link_config(pi);
8534 			if (pi->up_vis > 0)
8535 				rc = apply_link_config(pi);
8536 			set_current_media(pi);
8537 			PORT_UNLOCK(pi);
8538 		}
8539 		end_synchronized_op(sc, 0);
8540 	}
8541 
8542 	return (rc);
8543 }
8544 
8545 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8546 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8547 {
8548 	struct port_info *pi = arg1;
8549 	struct link_config *lc = &pi->link_cfg;
8550 	int rc;
8551 	struct sbuf *sb;
8552 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2";
8553 
8554 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8555 	if (sb == NULL)
8556 		return (ENOMEM);
8557 	if (lc->link_ok)
8558 		sbuf_printf(sb, "%b", lc->fec, bits);
8559 	else
8560 		sbuf_printf(sb, "no link");
8561 	rc = sbuf_finish(sb);
8562 	sbuf_delete(sb);
8563 
8564 	return (rc);
8565 }
8566 
8567 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)8568 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8569 {
8570 	struct port_info *pi = arg1;
8571 	struct adapter *sc = pi->adapter;
8572 	struct link_config *lc = &pi->link_cfg;
8573 	int rc;
8574 	int8_t old;
8575 
8576 	if (req->newptr == NULL) {
8577 		struct sbuf *sb;
8578 		static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2"
8579 		    "\5RSVD3\6auto\7module";
8580 
8581 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8582 		if (sb == NULL)
8583 			return (ENOMEM);
8584 
8585 		sbuf_printf(sb, "%b", lc->requested_fec, bits);
8586 		rc = sbuf_finish(sb);
8587 		sbuf_delete(sb);
8588 	} else {
8589 		char s[8];
8590 		int n;
8591 
8592 		snprintf(s, sizeof(s), "%d",
8593 		    lc->requested_fec == FEC_AUTO ? -1 :
8594 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
8595 
8596 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8597 		if (rc != 0)
8598 			return(rc);
8599 
8600 		n = strtol(&s[0], NULL, 0);
8601 		if (n < 0 || n & FEC_AUTO)
8602 			n = FEC_AUTO;
8603 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
8604 			return (EINVAL);/* some other bit is set too */
8605 
8606 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8607 		    "t4reqf");
8608 		if (rc)
8609 			return (rc);
8610 		PORT_LOCK(pi);
8611 		old = lc->requested_fec;
8612 		if (n == FEC_AUTO)
8613 			lc->requested_fec = FEC_AUTO;
8614 		else if (n == 0 || n == FEC_NONE)
8615 			lc->requested_fec = FEC_NONE;
8616 		else {
8617 			if ((lc->pcaps |
8618 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
8619 			    lc->pcaps) {
8620 				rc = ENOTSUP;
8621 				goto done;
8622 			}
8623 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
8624 			    FEC_MODULE);
8625 		}
8626 		if (!hw_off_limits(sc)) {
8627 			fixup_link_config(pi);
8628 			if (pi->up_vis > 0) {
8629 				rc = apply_link_config(pi);
8630 				if (rc != 0) {
8631 					lc->requested_fec = old;
8632 					if (rc == FW_EPROTO)
8633 						rc = ENOTSUP;
8634 				}
8635 			}
8636 		}
8637 done:
8638 		PORT_UNLOCK(pi);
8639 		end_synchronized_op(sc, 0);
8640 	}
8641 
8642 	return (rc);
8643 }
8644 
8645 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)8646 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
8647 {
8648 	struct port_info *pi = arg1;
8649 	struct adapter *sc = pi->adapter;
8650 	struct link_config *lc = &pi->link_cfg;
8651 	int rc;
8652 	int8_t fec;
8653 	struct sbuf *sb;
8654 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2\5RSVD3";
8655 
8656 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8657 	if (sb == NULL)
8658 		return (ENOMEM);
8659 
8660 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
8661 		rc = EBUSY;
8662 		goto done;
8663 	}
8664 	if (hw_off_limits(sc)) {
8665 		rc = ENXIO;
8666 		goto done;
8667 	}
8668 	PORT_LOCK(pi);
8669 	if (pi->up_vis == 0) {
8670 		/*
8671 		 * If all the interfaces are administratively down the firmware
8672 		 * does not report transceiver changes.  Refresh port info here.
8673 		 * This is the only reason we have a synchronized op in this
8674 		 * function.  Just PORT_LOCK would have been enough otherwise.
8675 		 */
8676 		t4_update_port_info(pi);
8677 	}
8678 
8679 	fec = lc->fec_hint;
8680 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
8681 	    !fec_supported(lc->pcaps)) {
8682 		PORT_UNLOCK(pi);
8683 		sbuf_printf(sb, "n/a");
8684 	} else {
8685 		if (fec == 0)
8686 			fec = FEC_NONE;
8687 		PORT_UNLOCK(pi);
8688 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, bits);
8689 	}
8690 	rc = sbuf_finish(sb);
8691 done:
8692 	sbuf_delete(sb);
8693 	end_synchronized_op(sc, 0);
8694 
8695 	return (rc);
8696 }
8697 
8698 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)8699 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
8700 {
8701 	struct port_info *pi = arg1;
8702 	struct adapter *sc = pi->adapter;
8703 	struct link_config *lc = &pi->link_cfg;
8704 	int rc, val;
8705 
8706 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
8707 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
8708 	else
8709 		val = -1;
8710 	rc = sysctl_handle_int(oidp, &val, 0, req);
8711 	if (rc != 0 || req->newptr == NULL)
8712 		return (rc);
8713 	if (val == 0)
8714 		val = AUTONEG_DISABLE;
8715 	else if (val == 1)
8716 		val = AUTONEG_ENABLE;
8717 	else
8718 		val = AUTONEG_AUTO;
8719 
8720 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8721 	    "t4aneg");
8722 	if (rc)
8723 		return (rc);
8724 	PORT_LOCK(pi);
8725 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
8726 		rc = ENOTSUP;
8727 		goto done;
8728 	}
8729 	lc->requested_aneg = val;
8730 	if (!hw_off_limits(sc)) {
8731 		fixup_link_config(pi);
8732 		if (pi->up_vis > 0)
8733 			rc = apply_link_config(pi);
8734 		set_current_media(pi);
8735 	}
8736 done:
8737 	PORT_UNLOCK(pi);
8738 	end_synchronized_op(sc, 0);
8739 	return (rc);
8740 }
8741 
8742 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)8743 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
8744 {
8745 	struct port_info *pi = arg1;
8746 	struct adapter *sc = pi->adapter;
8747 	struct link_config *lc = &pi->link_cfg;
8748 	int rc, val;
8749 
8750 	val = lc->force_fec;
8751 	MPASS(val >= -1 && val <= 1);
8752 	rc = sysctl_handle_int(oidp, &val, 0, req);
8753 	if (rc != 0 || req->newptr == NULL)
8754 		return (rc);
8755 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
8756 		return (ENOTSUP);
8757 	if (val < -1 || val > 1)
8758 		return (EINVAL);
8759 
8760 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
8761 	if (rc)
8762 		return (rc);
8763 	PORT_LOCK(pi);
8764 	lc->force_fec = val;
8765 	if (!hw_off_limits(sc)) {
8766 		fixup_link_config(pi);
8767 		if (pi->up_vis > 0)
8768 			rc = apply_link_config(pi);
8769 	}
8770 	PORT_UNLOCK(pi);
8771 	end_synchronized_op(sc, 0);
8772 	return (rc);
8773 }
8774 
8775 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)8776 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
8777 {
8778 	struct adapter *sc = arg1;
8779 	int rc, reg = arg2;
8780 	uint64_t val;
8781 
8782 	mtx_lock(&sc->reg_lock);
8783 	if (hw_off_limits(sc))
8784 		rc = ENXIO;
8785 	else {
8786 		rc = 0;
8787 		val = t4_read_reg64(sc, reg);
8788 	}
8789 	mtx_unlock(&sc->reg_lock);
8790 	if (rc == 0)
8791 		rc = sysctl_handle_64(oidp, &val, 0, req);
8792 	return (rc);
8793 }
8794 
8795 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)8796 sysctl_temperature(SYSCTL_HANDLER_ARGS)
8797 {
8798 	struct adapter *sc = arg1;
8799 	int rc, t;
8800 	uint32_t param, val;
8801 
8802 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
8803 	if (rc)
8804 		return (rc);
8805 	if (hw_off_limits(sc))
8806 		rc = ENXIO;
8807 	else {
8808 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8809 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8810 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
8811 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8812 	}
8813 	end_synchronized_op(sc, 0);
8814 	if (rc)
8815 		return (rc);
8816 
8817 	/* unknown is returned as 0 but we display -1 in that case */
8818 	t = val == 0 ? -1 : val;
8819 
8820 	rc = sysctl_handle_int(oidp, &t, 0, req);
8821 	return (rc);
8822 }
8823 
8824 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)8825 sysctl_vdd(SYSCTL_HANDLER_ARGS)
8826 {
8827 	struct adapter *sc = arg1;
8828 	int rc;
8829 	uint32_t param, val;
8830 
8831 	if (sc->params.core_vdd == 0) {
8832 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8833 		    "t4vdd");
8834 		if (rc)
8835 			return (rc);
8836 		if (hw_off_limits(sc))
8837 			rc = ENXIO;
8838 		else {
8839 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8840 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8841 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
8842 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
8843 			    &param, &val);
8844 		}
8845 		end_synchronized_op(sc, 0);
8846 		if (rc)
8847 			return (rc);
8848 		sc->params.core_vdd = val;
8849 	}
8850 
8851 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
8852 }
8853 
8854 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)8855 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
8856 {
8857 	struct adapter *sc = arg1;
8858 	int rc, v;
8859 	uint32_t param, val;
8860 
8861 	v = sc->sensor_resets;
8862 	rc = sysctl_handle_int(oidp, &v, 0, req);
8863 	if (rc != 0 || req->newptr == NULL || v <= 0)
8864 		return (rc);
8865 
8866 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
8867 	    chip_id(sc) < CHELSIO_T5)
8868 		return (ENOTSUP);
8869 
8870 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
8871 	if (rc)
8872 		return (rc);
8873 	if (hw_off_limits(sc))
8874 		rc = ENXIO;
8875 	else {
8876 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8877 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8878 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
8879 		val = 1;
8880 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8881 	}
8882 	end_synchronized_op(sc, 0);
8883 	if (rc == 0)
8884 		sc->sensor_resets++;
8885 	return (rc);
8886 }
8887 
8888 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)8889 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
8890 {
8891 	struct adapter *sc = arg1;
8892 	struct sbuf *sb;
8893 	int rc;
8894 	uint32_t param, val;
8895 
8896 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
8897 	if (rc)
8898 		return (rc);
8899 	if (hw_off_limits(sc))
8900 		rc = ENXIO;
8901 	else {
8902 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8903 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
8904 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8905 	}
8906 	end_synchronized_op(sc, 0);
8907 	if (rc)
8908 		return (rc);
8909 
8910 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8911 	if (sb == NULL)
8912 		return (ENOMEM);
8913 
8914 	if (val == 0xffffffff) {
8915 		/* Only debug and custom firmwares report load averages. */
8916 		sbuf_printf(sb, "not available");
8917 	} else {
8918 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
8919 		    (val >> 16) & 0xff);
8920 	}
8921 	rc = sbuf_finish(sb);
8922 	sbuf_delete(sb);
8923 
8924 	return (rc);
8925 }
8926 
8927 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)8928 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
8929 {
8930 	struct adapter *sc = arg1;
8931 	struct sbuf *sb;
8932 	int rc, i;
8933 	uint16_t incr[NMTUS][NCCTRL_WIN];
8934 	static const char *dec_fac[] = {
8935 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
8936 		"0.9375"
8937 	};
8938 
8939 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8940 	if (sb == NULL)
8941 		return (ENOMEM);
8942 
8943 	rc = 0;
8944 	mtx_lock(&sc->reg_lock);
8945 	if (hw_off_limits(sc))
8946 		rc = ENXIO;
8947 	else
8948 		t4_read_cong_tbl(sc, incr);
8949 	mtx_unlock(&sc->reg_lock);
8950 	if (rc)
8951 		goto done;
8952 
8953 	for (i = 0; i < NCCTRL_WIN; ++i) {
8954 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
8955 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
8956 		    incr[5][i], incr[6][i], incr[7][i]);
8957 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
8958 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
8959 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
8960 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
8961 	}
8962 
8963 	rc = sbuf_finish(sb);
8964 done:
8965 	sbuf_delete(sb);
8966 	return (rc);
8967 }
8968 
8969 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
8970 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
8971 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
8972 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
8973 };
8974 
8975 static int
sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)8976 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
8977 {
8978 	struct adapter *sc = arg1;
8979 	struct sbuf *sb;
8980 	int rc, i, n, qid = arg2;
8981 	uint32_t *buf, *p;
8982 	char *qtype;
8983 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
8984 
8985 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
8986 	    ("%s: bad qid %d\n", __func__, qid));
8987 
8988 	if (qid < CIM_NUM_IBQ) {
8989 		/* inbound queue */
8990 		qtype = "IBQ";
8991 		n = 4 * CIM_IBQ_SIZE;
8992 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
8993 		mtx_lock(&sc->reg_lock);
8994 		if (hw_off_limits(sc))
8995 			rc = -ENXIO;
8996 		else
8997 			rc = t4_read_cim_ibq(sc, qid, buf, n);
8998 		mtx_unlock(&sc->reg_lock);
8999 	} else {
9000 		/* outbound queue */
9001 		qtype = "OBQ";
9002 		qid -= CIM_NUM_IBQ;
9003 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
9004 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9005 		mtx_lock(&sc->reg_lock);
9006 		if (hw_off_limits(sc))
9007 			rc = -ENXIO;
9008 		else
9009 			rc = t4_read_cim_obq(sc, qid, buf, n);
9010 		mtx_unlock(&sc->reg_lock);
9011 	}
9012 
9013 	if (rc < 0) {
9014 		rc = -rc;
9015 		goto done;
9016 	}
9017 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9018 
9019 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9020 	if (sb == NULL) {
9021 		rc = ENOMEM;
9022 		goto done;
9023 	}
9024 
9025 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
9026 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9027 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9028 		    p[2], p[3]);
9029 
9030 	rc = sbuf_finish(sb);
9031 	sbuf_delete(sb);
9032 done:
9033 	free(buf, M_CXGBE);
9034 	return (rc);
9035 }
9036 
9037 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9038 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9039 {
9040 	uint32_t *p;
9041 
9042 	sbuf_printf(sb, "Status   Data      PC%s",
9043 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9044 	    "     LS0Stat  LS0Addr             LS0Data");
9045 
9046 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9047 		if (cfg & F_UPDBGLACAPTPCONLY) {
9048 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9049 			    p[6], p[7]);
9050 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9051 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9052 			    p[4] & 0xff, p[5] >> 8);
9053 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9054 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9055 			    p[1] & 0xf, p[2] >> 4);
9056 		} else {
9057 			sbuf_printf(sb,
9058 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9059 			    "%08x%08x%08x%08x",
9060 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9061 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9062 			    p[6], p[7]);
9063 		}
9064 	}
9065 }
9066 
9067 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9068 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9069 {
9070 	uint32_t *p;
9071 
9072 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9073 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9074 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9075 
9076 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9077 		if (cfg & F_UPDBGLACAPTPCONLY) {
9078 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9079 			    p[3] & 0xff, p[2], p[1], p[0]);
9080 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9081 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9082 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9083 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9084 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9085 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9086 			    p[6] >> 16);
9087 		} else {
9088 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9089 			    "%08x %08x %08x %08x %08x %08x",
9090 			    (p[9] >> 16) & 0xff,
9091 			    p[9] & 0xffff, p[8] >> 16,
9092 			    p[8] & 0xffff, p[7] >> 16,
9093 			    p[7] & 0xffff, p[6] >> 16,
9094 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9095 		}
9096 	}
9097 }
9098 
9099 static int
sbuf_cim_la(struct adapter * sc,struct sbuf * sb,int flags)9100 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
9101 {
9102 	uint32_t cfg, *buf;
9103 	int rc;
9104 
9105 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9106 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9107 	    M_ZERO | flags);
9108 	if (buf == NULL)
9109 		return (ENOMEM);
9110 
9111 	mtx_lock(&sc->reg_lock);
9112 	if (hw_off_limits(sc))
9113 		rc = ENXIO;
9114 	else {
9115 		rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
9116 		if (rc == 0)
9117 			rc = -t4_cim_read_la(sc, buf, NULL);
9118 	}
9119 	mtx_unlock(&sc->reg_lock);
9120 	if (rc == 0) {
9121 		if (chip_id(sc) < CHELSIO_T6)
9122 			sbuf_cim_la4(sc, sb, buf, cfg);
9123 		else
9124 			sbuf_cim_la6(sc, sb, buf, cfg);
9125 	}
9126 	free(buf, M_CXGBE);
9127 	return (rc);
9128 }
9129 
9130 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9131 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9132 {
9133 	struct adapter *sc = arg1;
9134 	struct sbuf *sb;
9135 	int rc;
9136 
9137 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9138 	if (sb == NULL)
9139 		return (ENOMEM);
9140 
9141 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
9142 	if (rc == 0)
9143 		rc = sbuf_finish(sb);
9144 	sbuf_delete(sb);
9145 	return (rc);
9146 }
9147 
9148 static void
dump_cim_regs(struct adapter * sc)9149 dump_cim_regs(struct adapter *sc)
9150 {
9151 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9152 	    device_get_nameunit(sc->dev),
9153 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9154 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9155 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9156 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9157 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9158 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9159 	    device_get_nameunit(sc->dev),
9160 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9161 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9162 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9163 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9164 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9165 }
9166 
9167 static void
dump_cimla(struct adapter * sc)9168 dump_cimla(struct adapter *sc)
9169 {
9170 	struct sbuf sb;
9171 	int rc;
9172 
9173 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9174 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9175 		    device_get_nameunit(sc->dev));
9176 		return;
9177 	}
9178 	rc = sbuf_cim_la(sc, &sb, M_WAITOK);
9179 	if (rc == 0) {
9180 		rc = sbuf_finish(&sb);
9181 		if (rc == 0) {
9182 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9183 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9184 		}
9185 	}
9186 	sbuf_delete(&sb);
9187 }
9188 
9189 void
t4_os_cim_err(struct adapter * sc)9190 t4_os_cim_err(struct adapter *sc)
9191 {
9192 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9193 }
9194 
9195 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9196 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9197 {
9198 	struct adapter *sc = arg1;
9199 	u_int i;
9200 	struct sbuf *sb;
9201 	uint32_t *buf, *p;
9202 	int rc;
9203 
9204 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9205 	if (sb == NULL)
9206 		return (ENOMEM);
9207 
9208 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9209 	    M_ZERO | M_WAITOK);
9210 
9211 	rc = 0;
9212 	mtx_lock(&sc->reg_lock);
9213 	if (hw_off_limits(sc))
9214 		rc = ENXIO;
9215 	else
9216 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9217 	mtx_unlock(&sc->reg_lock);
9218 	if (rc)
9219 		goto done;
9220 
9221 	p = buf;
9222 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9223 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9224 		    p[1], p[0]);
9225 	}
9226 
9227 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9228 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9229 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9230 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9231 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9232 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9233 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9234 		    p[0] & 1);
9235 	}
9236 	rc = sbuf_finish(sb);
9237 done:
9238 	sbuf_delete(sb);
9239 	free(buf, M_CXGBE);
9240 	return (rc);
9241 }
9242 
9243 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9244 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9245 {
9246 	struct adapter *sc = arg1;
9247 	u_int i;
9248 	struct sbuf *sb;
9249 	uint32_t *buf, *p;
9250 	int rc;
9251 
9252 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9253 	if (sb == NULL)
9254 		return (ENOMEM);
9255 
9256 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9257 	    M_ZERO | M_WAITOK);
9258 
9259 	rc = 0;
9260 	mtx_lock(&sc->reg_lock);
9261 	if (hw_off_limits(sc))
9262 		rc = ENXIO;
9263 	else
9264 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9265 	mtx_unlock(&sc->reg_lock);
9266 	if (rc)
9267 		goto done;
9268 
9269 	p = buf;
9270 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9271 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9272 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9273 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9274 		    p[4], p[3], p[2], p[1], p[0]);
9275 	}
9276 
9277 	sbuf_printf(sb, "\n\nCntl ID               Data");
9278 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9279 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9280 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9281 	}
9282 
9283 	rc = sbuf_finish(sb);
9284 done:
9285 	sbuf_delete(sb);
9286 	free(buf, M_CXGBE);
9287 	return (rc);
9288 }
9289 
9290 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9291 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9292 {
9293 	struct adapter *sc = arg1;
9294 	struct sbuf *sb;
9295 	int rc, i;
9296 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9297 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9298 	uint16_t thres[CIM_NUM_IBQ];
9299 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9300 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9301 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9302 
9303 	cim_num_obq = sc->chip_params->cim_num_obq;
9304 	if (is_t4(sc)) {
9305 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9306 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9307 	} else {
9308 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9309 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9310 	}
9311 	nq = CIM_NUM_IBQ + cim_num_obq;
9312 
9313 	mtx_lock(&sc->reg_lock);
9314 	if (hw_off_limits(sc))
9315 		rc = ENXIO;
9316 	else {
9317 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9318 		if (rc == 0) {
9319 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9320 			    obq_wr);
9321 			if (rc == 0)
9322 				t4_read_cimq_cfg(sc, base, size, thres);
9323 		}
9324 	}
9325 	mtx_unlock(&sc->reg_lock);
9326 	if (rc)
9327 		return (rc);
9328 
9329 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9330 	if (sb == NULL)
9331 		return (ENOMEM);
9332 
9333 	sbuf_printf(sb,
9334 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9335 
9336 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9337 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9338 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9339 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9340 		    G_QUEREMFLITS(p[2]) * 16);
9341 	for ( ; i < nq; i++, p += 4, wr += 2)
9342 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9343 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9344 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9345 		    G_QUEREMFLITS(p[2]) * 16);
9346 
9347 	rc = sbuf_finish(sb);
9348 	sbuf_delete(sb);
9349 
9350 	return (rc);
9351 }
9352 
9353 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9354 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9355 {
9356 	struct adapter *sc = arg1;
9357 	struct sbuf *sb;
9358 	int rc;
9359 	struct tp_cpl_stats stats;
9360 
9361 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9362 	if (sb == NULL)
9363 		return (ENOMEM);
9364 
9365 	rc = 0;
9366 	mtx_lock(&sc->reg_lock);
9367 	if (hw_off_limits(sc))
9368 		rc = ENXIO;
9369 	else
9370 		t4_tp_get_cpl_stats(sc, &stats, 0);
9371 	mtx_unlock(&sc->reg_lock);
9372 	if (rc)
9373 		goto done;
9374 
9375 	if (sc->chip_params->nchan > 2) {
9376 		sbuf_printf(sb, "                 channel 0  channel 1"
9377 		    "  channel 2  channel 3");
9378 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9379 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9380 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9381 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9382 	} else {
9383 		sbuf_printf(sb, "                 channel 0  channel 1");
9384 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9385 		    stats.req[0], stats.req[1]);
9386 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9387 		    stats.rsp[0], stats.rsp[1]);
9388 	}
9389 
9390 	rc = sbuf_finish(sb);
9391 done:
9392 	sbuf_delete(sb);
9393 	return (rc);
9394 }
9395 
9396 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9397 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9398 {
9399 	struct adapter *sc = arg1;
9400 	struct sbuf *sb;
9401 	int rc;
9402 	struct tp_usm_stats stats;
9403 
9404 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9405 	if (sb == NULL)
9406 		return (ENOMEM);
9407 
9408 	rc = 0;
9409 	mtx_lock(&sc->reg_lock);
9410 	if (hw_off_limits(sc))
9411 		rc = ENXIO;
9412 	else
9413 		t4_get_usm_stats(sc, &stats, 1);
9414 	mtx_unlock(&sc->reg_lock);
9415 	if (rc == 0) {
9416 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
9417 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
9418 		sbuf_printf(sb, "Drops:  %u", stats.drops);
9419 		rc = sbuf_finish(sb);
9420 	}
9421 	sbuf_delete(sb);
9422 
9423 	return (rc);
9424 }
9425 
9426 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)9427 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
9428 {
9429 	struct adapter *sc = arg1;
9430 	struct sbuf *sb;
9431 	int rc;
9432 	struct tp_tid_stats stats;
9433 
9434 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9435 	if (sb == NULL)
9436 		return (ENOMEM);
9437 
9438 	rc = 0;
9439 	mtx_lock(&sc->reg_lock);
9440 	if (hw_off_limits(sc))
9441 		rc = ENXIO;
9442 	else
9443 		t4_tp_get_tid_stats(sc, &stats, 1);
9444 	mtx_unlock(&sc->reg_lock);
9445 	if (rc == 0) {
9446 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
9447 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
9448 		sbuf_printf(sb, "Active:     %u\n", stats.act);
9449 		sbuf_printf(sb, "Passive:    %u", stats.pas);
9450 		rc = sbuf_finish(sb);
9451 	}
9452 	sbuf_delete(sb);
9453 
9454 	return (rc);
9455 }
9456 
9457 static const char * const devlog_level_strings[] = {
9458 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
9459 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
9460 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
9461 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
9462 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
9463 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
9464 };
9465 
9466 static const char * const devlog_facility_strings[] = {
9467 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
9468 	[FW_DEVLOG_FACILITY_CF]		= "CF",
9469 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
9470 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
9471 	[FW_DEVLOG_FACILITY_RES]	= "RES",
9472 	[FW_DEVLOG_FACILITY_HW]		= "HW",
9473 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
9474 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
9475 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
9476 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
9477 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
9478 	[FW_DEVLOG_FACILITY_VI]		= "VI",
9479 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
9480 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
9481 	[FW_DEVLOG_FACILITY_TM]		= "TM",
9482 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
9483 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
9484 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
9485 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
9486 	[FW_DEVLOG_FACILITY_RI]		= "RI",
9487 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
9488 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
9489 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
9490 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
9491 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
9492 };
9493 
9494 static int
sbuf_devlog(struct adapter * sc,struct sbuf * sb,int flags)9495 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
9496 {
9497 	int i, j, rc, nentries, first = 0;
9498 	struct devlog_params *dparams = &sc->params.devlog;
9499 	struct fw_devlog_e *buf, *e;
9500 	uint64_t ftstamp = UINT64_MAX;
9501 
9502 	if (dparams->addr == 0)
9503 		return (ENXIO);
9504 
9505 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9506 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
9507 	if (buf == NULL)
9508 		return (ENOMEM);
9509 
9510 	mtx_lock(&sc->reg_lock);
9511 	if (hw_off_limits(sc))
9512 		rc = ENXIO;
9513 	else
9514 		rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf,
9515 		    dparams->size);
9516 	mtx_unlock(&sc->reg_lock);
9517 	if (rc != 0)
9518 		goto done;
9519 
9520 	nentries = dparams->size / sizeof(struct fw_devlog_e);
9521 	for (i = 0; i < nentries; i++) {
9522 		e = &buf[i];
9523 
9524 		if (e->timestamp == 0)
9525 			break;	/* end */
9526 
9527 		e->timestamp = be64toh(e->timestamp);
9528 		e->seqno = be32toh(e->seqno);
9529 		for (j = 0; j < 8; j++)
9530 			e->params[j] = be32toh(e->params[j]);
9531 
9532 		if (e->timestamp < ftstamp) {
9533 			ftstamp = e->timestamp;
9534 			first = i;
9535 		}
9536 	}
9537 
9538 	if (buf[first].timestamp == 0)
9539 		goto done;	/* nothing in the log */
9540 
9541 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
9542 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
9543 
9544 	i = first;
9545 	do {
9546 		e = &buf[i];
9547 		if (e->timestamp == 0)
9548 			break;	/* end */
9549 
9550 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
9551 		    e->seqno, e->timestamp,
9552 		    (e->level < nitems(devlog_level_strings) ?
9553 			devlog_level_strings[e->level] : "UNKNOWN"),
9554 		    (e->facility < nitems(devlog_facility_strings) ?
9555 			devlog_facility_strings[e->facility] : "UNKNOWN"));
9556 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
9557 		    e->params[2], e->params[3], e->params[4],
9558 		    e->params[5], e->params[6], e->params[7]);
9559 
9560 		if (++i == nentries)
9561 			i = 0;
9562 	} while (i != first);
9563 done:
9564 	free(buf, M_CXGBE);
9565 	return (rc);
9566 }
9567 
9568 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)9569 sysctl_devlog(SYSCTL_HANDLER_ARGS)
9570 {
9571 	struct adapter *sc = arg1;
9572 	int rc;
9573 	struct sbuf *sb;
9574 
9575 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9576 	if (sb == NULL)
9577 		return (ENOMEM);
9578 
9579 	rc = sbuf_devlog(sc, sb, M_WAITOK);
9580 	if (rc == 0)
9581 		rc = sbuf_finish(sb);
9582 	sbuf_delete(sb);
9583 	return (rc);
9584 }
9585 
9586 static void
dump_devlog(struct adapter * sc)9587 dump_devlog(struct adapter *sc)
9588 {
9589 	int rc;
9590 	struct sbuf sb;
9591 
9592 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9593 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
9594 		    device_get_nameunit(sc->dev));
9595 		return;
9596 	}
9597 	rc = sbuf_devlog(sc, &sb, M_WAITOK);
9598 	if (rc == 0) {
9599 		rc = sbuf_finish(&sb);
9600 		if (rc == 0) {
9601 			log(LOG_DEBUG, "%s: device log follows.\n%s",
9602 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9603 		}
9604 	}
9605 	sbuf_delete(&sb);
9606 }
9607 
9608 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)9609 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
9610 {
9611 	struct adapter *sc = arg1;
9612 	struct sbuf *sb;
9613 	int rc;
9614 	struct tp_fcoe_stats stats[MAX_NCHAN];
9615 	int i, nchan = sc->chip_params->nchan;
9616 
9617 	rc = 0;
9618 	mtx_lock(&sc->reg_lock);
9619 	if (hw_off_limits(sc))
9620 		rc = ENXIO;
9621 	else {
9622 		for (i = 0; i < nchan; i++)
9623 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
9624 	}
9625 	mtx_unlock(&sc->reg_lock);
9626 	if (rc != 0)
9627 		return (rc);
9628 
9629 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9630 	if (sb == NULL)
9631 		return (ENOMEM);
9632 
9633 	if (nchan > 2) {
9634 		sbuf_printf(sb, "                   channel 0        channel 1"
9635 		    "        channel 2        channel 3");
9636 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
9637 		    stats[0].octets_ddp, stats[1].octets_ddp,
9638 		    stats[2].octets_ddp, stats[3].octets_ddp);
9639 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
9640 		    stats[0].frames_ddp, stats[1].frames_ddp,
9641 		    stats[2].frames_ddp, stats[3].frames_ddp);
9642 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
9643 		    stats[0].frames_drop, stats[1].frames_drop,
9644 		    stats[2].frames_drop, stats[3].frames_drop);
9645 	} else {
9646 		sbuf_printf(sb, "                   channel 0        channel 1");
9647 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
9648 		    stats[0].octets_ddp, stats[1].octets_ddp);
9649 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
9650 		    stats[0].frames_ddp, stats[1].frames_ddp);
9651 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
9652 		    stats[0].frames_drop, stats[1].frames_drop);
9653 	}
9654 
9655 	rc = sbuf_finish(sb);
9656 	sbuf_delete(sb);
9657 
9658 	return (rc);
9659 }
9660 
9661 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)9662 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
9663 {
9664 	struct adapter *sc = arg1;
9665 	struct sbuf *sb;
9666 	int rc, i;
9667 	unsigned int map, kbps, ipg, mode;
9668 	unsigned int pace_tab[NTX_SCHED];
9669 
9670 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
9671 	if (sb == NULL)
9672 		return (ENOMEM);
9673 
9674 	mtx_lock(&sc->reg_lock);
9675 	if (hw_off_limits(sc)) {
9676 		mtx_unlock(&sc->reg_lock);
9677 		rc = ENXIO;
9678 		goto done;
9679 	}
9680 
9681 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
9682 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
9683 	t4_read_pace_tbl(sc, pace_tab);
9684 	mtx_unlock(&sc->reg_lock);
9685 
9686 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
9687 	    "Class IPG (0.1 ns)   Flow IPG (us)");
9688 
9689 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
9690 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
9691 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
9692 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
9693 		if (kbps)
9694 			sbuf_printf(sb, "%9u     ", kbps);
9695 		else
9696 			sbuf_printf(sb, " disabled     ");
9697 
9698 		if (ipg)
9699 			sbuf_printf(sb, "%13u        ", ipg);
9700 		else
9701 			sbuf_printf(sb, "     disabled        ");
9702 
9703 		if (pace_tab[i])
9704 			sbuf_printf(sb, "%10u", pace_tab[i]);
9705 		else
9706 			sbuf_printf(sb, "  disabled");
9707 	}
9708 	rc = sbuf_finish(sb);
9709 done:
9710 	sbuf_delete(sb);
9711 	return (rc);
9712 }
9713 
9714 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)9715 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
9716 {
9717 	struct adapter *sc = arg1;
9718 	struct sbuf *sb;
9719 	int rc, i, j;
9720 	uint64_t *p0, *p1;
9721 	struct lb_port_stats s[2];
9722 	static const char *stat_name[] = {
9723 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
9724 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
9725 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
9726 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
9727 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
9728 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
9729 		"BG2FramesTrunc:", "BG3FramesTrunc:"
9730 	};
9731 
9732 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9733 	if (sb == NULL)
9734 		return (ENOMEM);
9735 
9736 	memset(s, 0, sizeof(s));
9737 
9738 	rc = 0;
9739 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
9740 		mtx_lock(&sc->reg_lock);
9741 		if (hw_off_limits(sc))
9742 			rc = ENXIO;
9743 		else {
9744 			t4_get_lb_stats(sc, i, &s[0]);
9745 			t4_get_lb_stats(sc, i + 1, &s[1]);
9746 		}
9747 		mtx_unlock(&sc->reg_lock);
9748 		if (rc != 0)
9749 			break;
9750 
9751 		p0 = &s[0].octets;
9752 		p1 = &s[1].octets;
9753 		sbuf_printf(sb, "%s                       Loopback %u"
9754 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
9755 
9756 		for (j = 0; j < nitems(stat_name); j++)
9757 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
9758 				   *p0++, *p1++);
9759 	}
9760 
9761 	if (rc == 0)
9762 		rc = sbuf_finish(sb);
9763 	sbuf_delete(sb);
9764 
9765 	return (rc);
9766 }
9767 
9768 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)9769 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
9770 {
9771 	int rc = 0;
9772 	struct port_info *pi = arg1;
9773 	struct link_config *lc = &pi->link_cfg;
9774 	struct sbuf *sb;
9775 
9776 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
9777 	if (sb == NULL)
9778 		return (ENOMEM);
9779 
9780 	if (lc->link_ok || lc->link_down_rc == 255)
9781 		sbuf_printf(sb, "n/a");
9782 	else
9783 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
9784 
9785 	rc = sbuf_finish(sb);
9786 	sbuf_delete(sb);
9787 
9788 	return (rc);
9789 }
9790 
9791 struct mem_desc {
9792 	u_int base;
9793 	u_int limit;
9794 	u_int idx;
9795 };
9796 
9797 static int
mem_desc_cmp(const void * a,const void * b)9798 mem_desc_cmp(const void *a, const void *b)
9799 {
9800 	const u_int v1 = ((const struct mem_desc *)a)->base;
9801 	const u_int v2 = ((const struct mem_desc *)b)->base;
9802 
9803 	if (v1 < v2)
9804 		return (-1);
9805 	else if (v1 > v2)
9806 		return (1);
9807 
9808 	return (0);
9809 }
9810 
9811 static void
mem_region_show(struct sbuf * sb,const char * name,unsigned int from,unsigned int to)9812 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
9813     unsigned int to)
9814 {
9815 	unsigned int size;
9816 
9817 	if (from == to)
9818 		return;
9819 
9820 	size = to - from + 1;
9821 	if (size == 0)
9822 		return;
9823 
9824 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
9825 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
9826 }
9827 
9828 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)9829 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
9830 {
9831 	struct adapter *sc = arg1;
9832 	struct sbuf *sb;
9833 	int rc, i, n;
9834 	uint32_t lo, hi, used, free, alloc;
9835 	static const char *memory[] = {
9836 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
9837 	};
9838 	static const char *region[] = {
9839 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
9840 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
9841 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
9842 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
9843 		"RQUDP region:", "PBL region:", "TXPBL region:",
9844 		"TLSKey region:", "DBVFIFO region:", "ULPRX state:",
9845 		"ULPTX state:", "On-chip queues:",
9846 	};
9847 	struct mem_desc avail[4];
9848 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
9849 	struct mem_desc *md = mem;
9850 
9851 	rc = sysctl_wire_old_buffer(req, 0);
9852 	if (rc != 0)
9853 		return (rc);
9854 
9855 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9856 	if (sb == NULL)
9857 		return (ENOMEM);
9858 
9859 	for (i = 0; i < nitems(mem); i++) {
9860 		mem[i].limit = 0;
9861 		mem[i].idx = i;
9862 	}
9863 
9864 	mtx_lock(&sc->reg_lock);
9865 	if (hw_off_limits(sc)) {
9866 		rc = ENXIO;
9867 		goto done;
9868 	}
9869 
9870 	/* Find and sort the populated memory ranges */
9871 	i = 0;
9872 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
9873 	if (lo & F_EDRAM0_ENABLE) {
9874 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
9875 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
9876 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
9877 		avail[i].idx = 0;
9878 		i++;
9879 	}
9880 	if (lo & F_EDRAM1_ENABLE) {
9881 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
9882 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
9883 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
9884 		avail[i].idx = 1;
9885 		i++;
9886 	}
9887 	if (lo & F_EXT_MEM_ENABLE) {
9888 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
9889 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
9890 		avail[i].limit = avail[i].base + (G_EXT_MEM_SIZE(hi) << 20);
9891 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
9892 		i++;
9893 	}
9894 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
9895 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9896 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9897 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9898 		avail[i].idx = 4;
9899 		i++;
9900 	}
9901 	if (is_t6(sc) && lo & F_HMA_MUX) {
9902 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9903 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9904 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9905 		avail[i].idx = 5;
9906 		i++;
9907 	}
9908 	MPASS(i <= nitems(avail));
9909 	if (!i)                                    /* no memory available */
9910 		goto done;
9911 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
9912 
9913 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
9914 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
9915 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
9916 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
9917 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
9918 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
9919 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
9920 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
9921 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
9922 
9923 	/* the next few have explicit upper bounds */
9924 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
9925 	md->limit = md->base - 1 +
9926 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
9927 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
9928 	md++;
9929 
9930 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
9931 	md->limit = md->base - 1 +
9932 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
9933 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
9934 	md++;
9935 
9936 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
9937 		if (chip_id(sc) <= CHELSIO_T5)
9938 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
9939 		else
9940 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
9941 		md->limit = 0;
9942 	} else {
9943 		md->base = 0;
9944 		md->idx = nitems(region);  /* hide it */
9945 	}
9946 	md++;
9947 
9948 #define ulp_region(reg) \
9949 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
9950 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
9951 
9952 	ulp_region(RX_ISCSI);
9953 	ulp_region(RX_TDDP);
9954 	ulp_region(TX_TPT);
9955 	ulp_region(RX_STAG);
9956 	ulp_region(RX_RQ);
9957 	ulp_region(RX_RQUDP);
9958 	ulp_region(RX_PBL);
9959 	ulp_region(TX_PBL);
9960 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
9961 		ulp_region(RX_TLS_KEY);
9962 	}
9963 #undef ulp_region
9964 
9965 	md->base = 0;
9966 	if (is_t4(sc))
9967 		md->idx = nitems(region);
9968 	else {
9969 		uint32_t size = 0;
9970 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
9971 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
9972 
9973 		if (is_t5(sc)) {
9974 			if (sge_ctrl & F_VFIFO_ENABLE)
9975 				size = fifo_size << 2;
9976 		} else
9977 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
9978 
9979 		if (size) {
9980 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
9981 			md->limit = md->base + size - 1;
9982 		} else
9983 			md->idx = nitems(region);
9984 	}
9985 	md++;
9986 
9987 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
9988 	md->limit = 0;
9989 	md++;
9990 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
9991 	md->limit = 0;
9992 	md++;
9993 
9994 	md->base = sc->vres.ocq.start;
9995 	if (sc->vres.ocq.size)
9996 		md->limit = md->base + sc->vres.ocq.size - 1;
9997 	else
9998 		md->idx = nitems(region);  /* hide it */
9999 	md++;
10000 
10001 	/* add any address-space holes, there can be up to 3 */
10002 	for (n = 0; n < i - 1; n++)
10003 		if (avail[n].limit < avail[n + 1].base)
10004 			(md++)->base = avail[n].limit;
10005 	if (avail[n].limit)
10006 		(md++)->base = avail[n].limit;
10007 
10008 	n = md - mem;
10009 	MPASS(n <= nitems(mem));
10010 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10011 
10012 	for (lo = 0; lo < i; lo++)
10013 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10014 				avail[lo].limit - 1);
10015 
10016 	sbuf_printf(sb, "\n");
10017 	for (i = 0; i < n; i++) {
10018 		if (mem[i].idx >= nitems(region))
10019 			continue;                        /* skip holes */
10020 		if (!mem[i].limit)
10021 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10022 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10023 				mem[i].limit);
10024 	}
10025 
10026 	sbuf_printf(sb, "\n");
10027 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10028 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10029 	mem_region_show(sb, "uP RAM:", lo, hi);
10030 
10031 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10032 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10033 	mem_region_show(sb, "uP Extmem2:", lo, hi);
10034 
10035 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10036 	for (i = 0, free = 0; i < 2; i++)
10037 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10038 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10039 		   G_PMRXMAXPAGE(lo), free,
10040 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
10041 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
10042 
10043 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10044 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10045 	for (i = 0, free = 0; i < 4; i++)
10046 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10047 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10048 		   G_PMTXMAXPAGE(lo), free,
10049 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10050 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
10051 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10052 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10053 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10054 
10055 	for (i = 0; i < 4; i++) {
10056 		if (chip_id(sc) > CHELSIO_T5)
10057 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10058 		else
10059 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10060 		if (is_t5(sc)) {
10061 			used = G_T5_USED(lo);
10062 			alloc = G_T5_ALLOC(lo);
10063 		} else {
10064 			used = G_USED(lo);
10065 			alloc = G_ALLOC(lo);
10066 		}
10067 		/* For T6 these are MAC buffer groups */
10068 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10069 		    i, used, alloc);
10070 	}
10071 	for (i = 0; i < sc->chip_params->nchan; i++) {
10072 		if (chip_id(sc) > CHELSIO_T5)
10073 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10074 		else
10075 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10076 		if (is_t5(sc)) {
10077 			used = G_T5_USED(lo);
10078 			alloc = G_T5_ALLOC(lo);
10079 		} else {
10080 			used = G_USED(lo);
10081 			alloc = G_ALLOC(lo);
10082 		}
10083 		/* For T6 these are MAC buffer groups */
10084 		sbuf_printf(sb,
10085 		    "\nLoopback %d using %u pages out of %u allocated",
10086 		    i, used, alloc);
10087 	}
10088 done:
10089 	mtx_unlock(&sc->reg_lock);
10090 	if (rc == 0)
10091 		rc = sbuf_finish(sb);
10092 	sbuf_delete(sb);
10093 	return (rc);
10094 }
10095 
10096 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10097 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10098 {
10099 	*mask = x | y;
10100 	y = htobe64(y);
10101 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10102 }
10103 
10104 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10105 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10106 {
10107 	struct adapter *sc = arg1;
10108 	struct sbuf *sb;
10109 	int rc, i;
10110 
10111 	MPASS(chip_id(sc) <= CHELSIO_T5);
10112 
10113 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10114 	if (sb == NULL)
10115 		return (ENOMEM);
10116 
10117 	sbuf_printf(sb,
10118 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10119 	    "  VF              Replication             P0 P1 P2 P3  ML");
10120 	rc = 0;
10121 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10122 		uint64_t tcamx, tcamy, mask;
10123 		uint32_t cls_lo, cls_hi;
10124 		uint8_t addr[ETHER_ADDR_LEN];
10125 
10126 		mtx_lock(&sc->reg_lock);
10127 		if (hw_off_limits(sc))
10128 			rc = ENXIO;
10129 		else {
10130 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10131 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10132 		}
10133 		mtx_unlock(&sc->reg_lock);
10134 		if (rc != 0)
10135 			break;
10136 		if (tcamx & tcamy)
10137 			continue;
10138 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10139 		mtx_lock(&sc->reg_lock);
10140 		if (hw_off_limits(sc))
10141 			rc = ENXIO;
10142 		else {
10143 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10144 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10145 		}
10146 		mtx_unlock(&sc->reg_lock);
10147 		if (rc != 0)
10148 			break;
10149 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10150 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10151 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10152 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10153 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10154 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10155 
10156 		if (cls_lo & F_REPLICATE) {
10157 			struct fw_ldst_cmd ldst_cmd;
10158 
10159 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10160 			ldst_cmd.op_to_addrspace =
10161 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10162 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10163 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10164 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10165 			ldst_cmd.u.mps.rplc.fid_idx =
10166 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10167 				V_FW_LDST_CMD_IDX(i));
10168 
10169 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10170 			    "t4mps");
10171 			if (rc)
10172 				break;
10173 			if (hw_off_limits(sc))
10174 				rc = ENXIO;
10175 			else
10176 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10177 				    sizeof(ldst_cmd), &ldst_cmd);
10178 			end_synchronized_op(sc, 0);
10179 			if (rc != 0)
10180 				break;
10181 			else {
10182 				sbuf_printf(sb, " %08x %08x %08x %08x",
10183 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10184 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10185 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10186 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10187 			}
10188 		} else
10189 			sbuf_printf(sb, "%36s", "");
10190 
10191 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10192 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10193 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10194 	}
10195 
10196 	if (rc)
10197 		(void) sbuf_finish(sb);
10198 	else
10199 		rc = sbuf_finish(sb);
10200 	sbuf_delete(sb);
10201 
10202 	return (rc);
10203 }
10204 
10205 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10206 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10207 {
10208 	struct adapter *sc = arg1;
10209 	struct sbuf *sb;
10210 	int rc, i;
10211 
10212 	MPASS(chip_id(sc) > CHELSIO_T5);
10213 
10214 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10215 	if (sb == NULL)
10216 		return (ENOMEM);
10217 
10218 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10219 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10220 	    "                           Replication"
10221 	    "                                    P0 P1 P2 P3  ML\n");
10222 
10223 	rc = 0;
10224 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10225 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10226 		uint16_t ivlan;
10227 		uint64_t tcamx, tcamy, val, mask;
10228 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10229 		uint8_t addr[ETHER_ADDR_LEN];
10230 
10231 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10232 		if (i < 256)
10233 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10234 		else
10235 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10236 		mtx_lock(&sc->reg_lock);
10237 		if (hw_off_limits(sc))
10238 			rc = ENXIO;
10239 		else {
10240 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10241 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10242 			tcamy = G_DMACH(val) << 32;
10243 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10244 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10245 		}
10246 		mtx_unlock(&sc->reg_lock);
10247 		if (rc != 0)
10248 			break;
10249 
10250 		lookup_type = G_DATALKPTYPE(data2);
10251 		port_num = G_DATAPORTNUM(data2);
10252 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10253 			/* Inner header VNI */
10254 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10255 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10256 			dip_hit = data2 & F_DATADIPHIT;
10257 			vlan_vld = 0;
10258 		} else {
10259 			vniy = 0;
10260 			dip_hit = 0;
10261 			vlan_vld = data2 & F_DATAVIDH2;
10262 			ivlan = G_VIDL(val);
10263 		}
10264 
10265 		ctl |= V_CTLXYBITSEL(1);
10266 		mtx_lock(&sc->reg_lock);
10267 		if (hw_off_limits(sc))
10268 			rc = ENXIO;
10269 		else {
10270 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10271 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10272 			tcamx = G_DMACH(val) << 32;
10273 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10274 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10275 		}
10276 		mtx_unlock(&sc->reg_lock);
10277 		if (rc != 0)
10278 			break;
10279 
10280 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10281 			/* Inner header VNI mask */
10282 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10283 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10284 		} else
10285 			vnix = 0;
10286 
10287 		if (tcamx & tcamy)
10288 			continue;
10289 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10290 
10291 		mtx_lock(&sc->reg_lock);
10292 		if (hw_off_limits(sc))
10293 			rc = ENXIO;
10294 		else {
10295 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10296 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10297 		}
10298 		mtx_unlock(&sc->reg_lock);
10299 		if (rc != 0)
10300 			break;
10301 
10302 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10303 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10304 			    "%012jx %06x %06x    -    -   %3c"
10305 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
10306 			    addr[1], addr[2], addr[3], addr[4], addr[5],
10307 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
10308 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10309 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10310 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10311 		} else {
10312 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10313 			    "%012jx    -       -   ", i, addr[0], addr[1],
10314 			    addr[2], addr[3], addr[4], addr[5],
10315 			    (uintmax_t)mask);
10316 
10317 			if (vlan_vld)
10318 				sbuf_printf(sb, "%4u   Y     ", ivlan);
10319 			else
10320 				sbuf_printf(sb, "  -    N     ");
10321 
10322 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
10323 			    lookup_type ? 'I' : 'O', port_num,
10324 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10325 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10326 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10327 		}
10328 
10329 
10330 		if (cls_lo & F_T6_REPLICATE) {
10331 			struct fw_ldst_cmd ldst_cmd;
10332 
10333 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10334 			ldst_cmd.op_to_addrspace =
10335 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10336 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10337 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10338 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10339 			ldst_cmd.u.mps.rplc.fid_idx =
10340 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10341 				V_FW_LDST_CMD_IDX(i));
10342 
10343 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10344 			    "t6mps");
10345 			if (rc)
10346 				break;
10347 			if (hw_off_limits(sc))
10348 				rc = ENXIO;
10349 			else
10350 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10351 				    sizeof(ldst_cmd), &ldst_cmd);
10352 			end_synchronized_op(sc, 0);
10353 			if (rc != 0)
10354 				break;
10355 			else {
10356 				sbuf_printf(sb, " %08x %08x %08x %08x"
10357 				    " %08x %08x %08x %08x",
10358 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
10359 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
10360 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
10361 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
10362 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10363 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10364 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10365 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10366 			}
10367 		} else
10368 			sbuf_printf(sb, "%72s", "");
10369 
10370 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
10371 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
10372 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
10373 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
10374 	}
10375 
10376 	if (rc)
10377 		(void) sbuf_finish(sb);
10378 	else
10379 		rc = sbuf_finish(sb);
10380 	sbuf_delete(sb);
10381 
10382 	return (rc);
10383 }
10384 
10385 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)10386 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
10387 {
10388 	struct adapter *sc = arg1;
10389 	struct sbuf *sb;
10390 	int rc;
10391 	uint16_t mtus[NMTUS];
10392 
10393 	rc = 0;
10394 	mtx_lock(&sc->reg_lock);
10395 	if (hw_off_limits(sc))
10396 		rc = ENXIO;
10397 	else
10398 		t4_read_mtu_tbl(sc, mtus, NULL);
10399 	mtx_unlock(&sc->reg_lock);
10400 	if (rc != 0)
10401 		return (rc);
10402 
10403 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10404 	if (sb == NULL)
10405 		return (ENOMEM);
10406 
10407 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
10408 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
10409 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
10410 	    mtus[14], mtus[15]);
10411 
10412 	rc = sbuf_finish(sb);
10413 	sbuf_delete(sb);
10414 
10415 	return (rc);
10416 }
10417 
10418 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)10419 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
10420 {
10421 	struct adapter *sc = arg1;
10422 	struct sbuf *sb;
10423 	int rc, i;
10424 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
10425 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
10426 	static const char *tx_stats[MAX_PM_NSTATS] = {
10427 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
10428 		"Tx FIFO wait", NULL, "Tx latency"
10429 	};
10430 	static const char *rx_stats[MAX_PM_NSTATS] = {
10431 		"Read:", "Write bypass:", "Write mem:", "Flush:",
10432 		"Rx FIFO wait", NULL, "Rx latency"
10433 	};
10434 
10435 	rc = 0;
10436 	mtx_lock(&sc->reg_lock);
10437 	if (hw_off_limits(sc))
10438 		rc = ENXIO;
10439 	else {
10440 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
10441 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
10442 	}
10443 	mtx_unlock(&sc->reg_lock);
10444 	if (rc != 0)
10445 		return (rc);
10446 
10447 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10448 	if (sb == NULL)
10449 		return (ENOMEM);
10450 
10451 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
10452 	for (i = 0; i < 4; i++) {
10453 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10454 		    tx_cyc[i]);
10455 	}
10456 
10457 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
10458 	for (i = 0; i < 4; i++) {
10459 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10460 		    rx_cyc[i]);
10461 	}
10462 
10463 	if (chip_id(sc) > CHELSIO_T5) {
10464 		sbuf_printf(sb,
10465 		    "\n              Total wait      Total occupancy");
10466 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10467 		    tx_cyc[i]);
10468 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10469 		    rx_cyc[i]);
10470 
10471 		i += 2;
10472 		MPASS(i < nitems(tx_stats));
10473 
10474 		sbuf_printf(sb,
10475 		    "\n                   Reads           Total wait");
10476 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10477 		    tx_cyc[i]);
10478 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10479 		    rx_cyc[i]);
10480 	}
10481 
10482 	rc = sbuf_finish(sb);
10483 	sbuf_delete(sb);
10484 
10485 	return (rc);
10486 }
10487 
10488 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)10489 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
10490 {
10491 	struct adapter *sc = arg1;
10492 	struct sbuf *sb;
10493 	int rc;
10494 	struct tp_rdma_stats stats;
10495 
10496 	rc = 0;
10497 	mtx_lock(&sc->reg_lock);
10498 	if (hw_off_limits(sc))
10499 		rc = ENXIO;
10500 	else
10501 		t4_tp_get_rdma_stats(sc, &stats, 0);
10502 	mtx_unlock(&sc->reg_lock);
10503 	if (rc != 0)
10504 		return (rc);
10505 
10506 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10507 	if (sb == NULL)
10508 		return (ENOMEM);
10509 
10510 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
10511 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
10512 
10513 	rc = sbuf_finish(sb);
10514 	sbuf_delete(sb);
10515 
10516 	return (rc);
10517 }
10518 
10519 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)10520 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
10521 {
10522 	struct adapter *sc = arg1;
10523 	struct sbuf *sb;
10524 	int rc;
10525 	struct tp_tcp_stats v4, v6;
10526 
10527 	rc = 0;
10528 	mtx_lock(&sc->reg_lock);
10529 	if (hw_off_limits(sc))
10530 		rc = ENXIO;
10531 	else
10532 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
10533 	mtx_unlock(&sc->reg_lock);
10534 	if (rc != 0)
10535 		return (rc);
10536 
10537 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10538 	if (sb == NULL)
10539 		return (ENOMEM);
10540 
10541 	sbuf_printf(sb,
10542 	    "                                IP                 IPv6\n");
10543 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
10544 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
10545 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
10546 	    v4.tcp_in_segs, v6.tcp_in_segs);
10547 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
10548 	    v4.tcp_out_segs, v6.tcp_out_segs);
10549 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
10550 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
10551 
10552 	rc = sbuf_finish(sb);
10553 	sbuf_delete(sb);
10554 
10555 	return (rc);
10556 }
10557 
10558 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)10559 sysctl_tids(SYSCTL_HANDLER_ARGS)
10560 {
10561 	struct adapter *sc = arg1;
10562 	struct sbuf *sb;
10563 	int rc;
10564 	uint32_t x, y;
10565 	struct tid_info *t = &sc->tids;
10566 
10567 	rc = 0;
10568 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10569 	if (sb == NULL)
10570 		return (ENOMEM);
10571 
10572 	if (t->natids) {
10573 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
10574 		    t->atids_in_use);
10575 	}
10576 
10577 	if (t->nhpftids) {
10578 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
10579 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
10580 	}
10581 
10582 	if (t->ntids) {
10583 		bool hashen = false;
10584 
10585 		mtx_lock(&sc->reg_lock);
10586 		if (hw_off_limits(sc))
10587 			rc = ENXIO;
10588 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10589 			hashen = true;
10590 			if (chip_id(sc) <= CHELSIO_T5) {
10591 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
10592 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
10593 			} else {
10594 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
10595 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
10596 			}
10597 		}
10598 		mtx_unlock(&sc->reg_lock);
10599 		if (rc != 0)
10600 			goto done;
10601 
10602 		sbuf_printf(sb, "TID range: ");
10603 		if (hashen) {
10604 			if (x)
10605 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
10606 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
10607 		} else {
10608 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
10609 			    t->ntids - 1);
10610 		}
10611 		sbuf_printf(sb, ", in use: %u\n",
10612 		    atomic_load_acq_int(&t->tids_in_use));
10613 	}
10614 
10615 	if (t->nstids) {
10616 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
10617 		    t->stid_base + t->nstids - 1, t->stids_in_use);
10618 	}
10619 
10620 	if (t->nftids) {
10621 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
10622 		    t->ftid_end, t->ftids_in_use);
10623 	}
10624 
10625 	if (t->netids) {
10626 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
10627 		    t->etid_base + t->netids - 1, t->etids_in_use);
10628 	}
10629 
10630 	mtx_lock(&sc->reg_lock);
10631 	if (hw_off_limits(sc))
10632 		rc = ENXIO;
10633 	else {
10634 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
10635 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
10636 	}
10637 	mtx_unlock(&sc->reg_lock);
10638 	if (rc != 0)
10639 		goto done;
10640 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
10641 done:
10642 	if (rc == 0)
10643 		rc = sbuf_finish(sb);
10644 	else
10645 		(void)sbuf_finish(sb);
10646 	sbuf_delete(sb);
10647 
10648 	return (rc);
10649 }
10650 
10651 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)10652 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
10653 {
10654 	struct adapter *sc = arg1;
10655 	struct sbuf *sb;
10656 	int rc;
10657 	struct tp_err_stats stats;
10658 
10659 	rc = 0;
10660 	mtx_lock(&sc->reg_lock);
10661 	if (hw_off_limits(sc))
10662 		rc = ENXIO;
10663 	else
10664 		t4_tp_get_err_stats(sc, &stats, 0);
10665 	mtx_unlock(&sc->reg_lock);
10666 	if (rc != 0)
10667 		return (rc);
10668 
10669 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10670 	if (sb == NULL)
10671 		return (ENOMEM);
10672 
10673 	if (sc->chip_params->nchan > 2) {
10674 		sbuf_printf(sb, "                 channel 0  channel 1"
10675 		    "  channel 2  channel 3\n");
10676 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
10677 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
10678 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
10679 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
10680 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
10681 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
10682 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
10683 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
10684 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
10685 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
10686 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
10687 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
10688 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
10689 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
10690 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
10691 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
10692 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
10693 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
10694 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
10695 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
10696 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
10697 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
10698 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
10699 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
10700 	} else {
10701 		sbuf_printf(sb, "                 channel 0  channel 1\n");
10702 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
10703 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
10704 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
10705 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
10706 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
10707 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
10708 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
10709 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
10710 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
10711 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
10712 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
10713 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
10714 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
10715 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
10716 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
10717 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
10718 	}
10719 
10720 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
10721 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
10722 
10723 	rc = sbuf_finish(sb);
10724 	sbuf_delete(sb);
10725 
10726 	return (rc);
10727 }
10728 
10729 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)10730 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
10731 {
10732 	struct adapter *sc = arg1;
10733 	struct sbuf *sb;
10734 	int rc;
10735 	struct tp_tnl_stats stats;
10736 
10737 	rc = 0;
10738 	mtx_lock(&sc->reg_lock);
10739 	if (hw_off_limits(sc))
10740 		rc = ENXIO;
10741 	else
10742 		t4_tp_get_tnl_stats(sc, &stats, 1);
10743 	mtx_unlock(&sc->reg_lock);
10744 	if (rc != 0)
10745 		return (rc);
10746 
10747 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10748 	if (sb == NULL)
10749 		return (ENOMEM);
10750 
10751 	if (sc->chip_params->nchan > 2) {
10752 		sbuf_printf(sb, "           channel 0  channel 1"
10753 		    "  channel 2  channel 3\n");
10754 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
10755 		    stats.out_pkt[0], stats.out_pkt[1],
10756 		    stats.out_pkt[2], stats.out_pkt[3]);
10757 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
10758 		    stats.in_pkt[0], stats.in_pkt[1],
10759 		    stats.in_pkt[2], stats.in_pkt[3]);
10760 	} else {
10761 		sbuf_printf(sb, "           channel 0  channel 1\n");
10762 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
10763 		    stats.out_pkt[0], stats.out_pkt[1]);
10764 		sbuf_printf(sb, "InPkts:   %10u %10u",
10765 		    stats.in_pkt[0], stats.in_pkt[1]);
10766 	}
10767 
10768 	rc = sbuf_finish(sb);
10769 	sbuf_delete(sb);
10770 
10771 	return (rc);
10772 }
10773 
10774 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)10775 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
10776 {
10777 	struct adapter *sc = arg1;
10778 	struct tp_params *tpp = &sc->params.tp;
10779 	u_int mask;
10780 	int rc;
10781 
10782 	mask = tpp->la_mask >> 16;
10783 	rc = sysctl_handle_int(oidp, &mask, 0, req);
10784 	if (rc != 0 || req->newptr == NULL)
10785 		return (rc);
10786 	if (mask > 0xffff)
10787 		return (EINVAL);
10788 	mtx_lock(&sc->reg_lock);
10789 	if (hw_off_limits(sc))
10790 		rc = ENXIO;
10791 	else {
10792 		tpp->la_mask = mask << 16;
10793 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
10794 		    tpp->la_mask);
10795 	}
10796 	mtx_unlock(&sc->reg_lock);
10797 
10798 	return (rc);
10799 }
10800 
10801 struct field_desc {
10802 	const char *name;
10803 	u_int start;
10804 	u_int width;
10805 };
10806 
10807 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)10808 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
10809 {
10810 	char buf[32];
10811 	int line_size = 0;
10812 
10813 	while (f->name) {
10814 		uint64_t mask = (1ULL << f->width) - 1;
10815 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
10816 		    ((uintmax_t)v >> f->start) & mask);
10817 
10818 		if (line_size + len >= 79) {
10819 			line_size = 8;
10820 			sbuf_printf(sb, "\n        ");
10821 		}
10822 		sbuf_printf(sb, "%s ", buf);
10823 		line_size += len + 1;
10824 		f++;
10825 	}
10826 	sbuf_printf(sb, "\n");
10827 }
10828 
10829 static const struct field_desc tp_la0[] = {
10830 	{ "RcfOpCodeOut", 60, 4 },
10831 	{ "State", 56, 4 },
10832 	{ "WcfState", 52, 4 },
10833 	{ "RcfOpcSrcOut", 50, 2 },
10834 	{ "CRxError", 49, 1 },
10835 	{ "ERxError", 48, 1 },
10836 	{ "SanityFailed", 47, 1 },
10837 	{ "SpuriousMsg", 46, 1 },
10838 	{ "FlushInputMsg", 45, 1 },
10839 	{ "FlushInputCpl", 44, 1 },
10840 	{ "RssUpBit", 43, 1 },
10841 	{ "RssFilterHit", 42, 1 },
10842 	{ "Tid", 32, 10 },
10843 	{ "InitTcb", 31, 1 },
10844 	{ "LineNumber", 24, 7 },
10845 	{ "Emsg", 23, 1 },
10846 	{ "EdataOut", 22, 1 },
10847 	{ "Cmsg", 21, 1 },
10848 	{ "CdataOut", 20, 1 },
10849 	{ "EreadPdu", 19, 1 },
10850 	{ "CreadPdu", 18, 1 },
10851 	{ "TunnelPkt", 17, 1 },
10852 	{ "RcfPeerFin", 16, 1 },
10853 	{ "RcfReasonOut", 12, 4 },
10854 	{ "TxCchannel", 10, 2 },
10855 	{ "RcfTxChannel", 8, 2 },
10856 	{ "RxEchannel", 6, 2 },
10857 	{ "RcfRxChannel", 5, 1 },
10858 	{ "RcfDataOutSrdy", 4, 1 },
10859 	{ "RxDvld", 3, 1 },
10860 	{ "RxOoDvld", 2, 1 },
10861 	{ "RxCongestion", 1, 1 },
10862 	{ "TxCongestion", 0, 1 },
10863 	{ NULL }
10864 };
10865 
10866 static const struct field_desc tp_la1[] = {
10867 	{ "CplCmdIn", 56, 8 },
10868 	{ "CplCmdOut", 48, 8 },
10869 	{ "ESynOut", 47, 1 },
10870 	{ "EAckOut", 46, 1 },
10871 	{ "EFinOut", 45, 1 },
10872 	{ "ERstOut", 44, 1 },
10873 	{ "SynIn", 43, 1 },
10874 	{ "AckIn", 42, 1 },
10875 	{ "FinIn", 41, 1 },
10876 	{ "RstIn", 40, 1 },
10877 	{ "DataIn", 39, 1 },
10878 	{ "DataInVld", 38, 1 },
10879 	{ "PadIn", 37, 1 },
10880 	{ "RxBufEmpty", 36, 1 },
10881 	{ "RxDdp", 35, 1 },
10882 	{ "RxFbCongestion", 34, 1 },
10883 	{ "TxFbCongestion", 33, 1 },
10884 	{ "TxPktSumSrdy", 32, 1 },
10885 	{ "RcfUlpType", 28, 4 },
10886 	{ "Eread", 27, 1 },
10887 	{ "Ebypass", 26, 1 },
10888 	{ "Esave", 25, 1 },
10889 	{ "Static0", 24, 1 },
10890 	{ "Cread", 23, 1 },
10891 	{ "Cbypass", 22, 1 },
10892 	{ "Csave", 21, 1 },
10893 	{ "CPktOut", 20, 1 },
10894 	{ "RxPagePoolFull", 18, 2 },
10895 	{ "RxLpbkPkt", 17, 1 },
10896 	{ "TxLpbkPkt", 16, 1 },
10897 	{ "RxVfValid", 15, 1 },
10898 	{ "SynLearned", 14, 1 },
10899 	{ "SetDelEntry", 13, 1 },
10900 	{ "SetInvEntry", 12, 1 },
10901 	{ "CpcmdDvld", 11, 1 },
10902 	{ "CpcmdSave", 10, 1 },
10903 	{ "RxPstructsFull", 8, 2 },
10904 	{ "EpcmdDvld", 7, 1 },
10905 	{ "EpcmdFlush", 6, 1 },
10906 	{ "EpcmdTrimPrefix", 5, 1 },
10907 	{ "EpcmdTrimPostfix", 4, 1 },
10908 	{ "ERssIp4Pkt", 3, 1 },
10909 	{ "ERssIp6Pkt", 2, 1 },
10910 	{ "ERssTcpUdpPkt", 1, 1 },
10911 	{ "ERssFceFipPkt", 0, 1 },
10912 	{ NULL }
10913 };
10914 
10915 static const struct field_desc tp_la2[] = {
10916 	{ "CplCmdIn", 56, 8 },
10917 	{ "MpsVfVld", 55, 1 },
10918 	{ "MpsPf", 52, 3 },
10919 	{ "MpsVf", 44, 8 },
10920 	{ "SynIn", 43, 1 },
10921 	{ "AckIn", 42, 1 },
10922 	{ "FinIn", 41, 1 },
10923 	{ "RstIn", 40, 1 },
10924 	{ "DataIn", 39, 1 },
10925 	{ "DataInVld", 38, 1 },
10926 	{ "PadIn", 37, 1 },
10927 	{ "RxBufEmpty", 36, 1 },
10928 	{ "RxDdp", 35, 1 },
10929 	{ "RxFbCongestion", 34, 1 },
10930 	{ "TxFbCongestion", 33, 1 },
10931 	{ "TxPktSumSrdy", 32, 1 },
10932 	{ "RcfUlpType", 28, 4 },
10933 	{ "Eread", 27, 1 },
10934 	{ "Ebypass", 26, 1 },
10935 	{ "Esave", 25, 1 },
10936 	{ "Static0", 24, 1 },
10937 	{ "Cread", 23, 1 },
10938 	{ "Cbypass", 22, 1 },
10939 	{ "Csave", 21, 1 },
10940 	{ "CPktOut", 20, 1 },
10941 	{ "RxPagePoolFull", 18, 2 },
10942 	{ "RxLpbkPkt", 17, 1 },
10943 	{ "TxLpbkPkt", 16, 1 },
10944 	{ "RxVfValid", 15, 1 },
10945 	{ "SynLearned", 14, 1 },
10946 	{ "SetDelEntry", 13, 1 },
10947 	{ "SetInvEntry", 12, 1 },
10948 	{ "CpcmdDvld", 11, 1 },
10949 	{ "CpcmdSave", 10, 1 },
10950 	{ "RxPstructsFull", 8, 2 },
10951 	{ "EpcmdDvld", 7, 1 },
10952 	{ "EpcmdFlush", 6, 1 },
10953 	{ "EpcmdTrimPrefix", 5, 1 },
10954 	{ "EpcmdTrimPostfix", 4, 1 },
10955 	{ "ERssIp4Pkt", 3, 1 },
10956 	{ "ERssIp6Pkt", 2, 1 },
10957 	{ "ERssTcpUdpPkt", 1, 1 },
10958 	{ "ERssFceFipPkt", 0, 1 },
10959 	{ NULL }
10960 };
10961 
10962 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)10963 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
10964 {
10965 
10966 	field_desc_show(sb, *p, tp_la0);
10967 }
10968 
10969 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)10970 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
10971 {
10972 
10973 	if (idx)
10974 		sbuf_printf(sb, "\n");
10975 	field_desc_show(sb, p[0], tp_la0);
10976 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
10977 		field_desc_show(sb, p[1], tp_la0);
10978 }
10979 
10980 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)10981 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
10982 {
10983 
10984 	if (idx)
10985 		sbuf_printf(sb, "\n");
10986 	field_desc_show(sb, p[0], tp_la0);
10987 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
10988 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
10989 }
10990 
10991 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)10992 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
10993 {
10994 	struct adapter *sc = arg1;
10995 	struct sbuf *sb;
10996 	uint64_t *buf, *p;
10997 	int rc;
10998 	u_int i, inc;
10999 	void (*show_func)(struct sbuf *, uint64_t *, int);
11000 
11001 	rc = 0;
11002 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11003 	if (sb == NULL)
11004 		return (ENOMEM);
11005 
11006 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11007 
11008 	mtx_lock(&sc->reg_lock);
11009 	if (hw_off_limits(sc))
11010 		rc = ENXIO;
11011 	else {
11012 		t4_tp_read_la(sc, buf, NULL);
11013 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11014 		case 2:
11015 			inc = 2;
11016 			show_func = tp_la_show2;
11017 			break;
11018 		case 3:
11019 			inc = 2;
11020 			show_func = tp_la_show3;
11021 			break;
11022 		default:
11023 			inc = 1;
11024 			show_func = tp_la_show;
11025 		}
11026 	}
11027 	mtx_unlock(&sc->reg_lock);
11028 	if (rc != 0)
11029 		goto done;
11030 
11031 	p = buf;
11032 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
11033 		(*show_func)(sb, p, i);
11034 	rc = sbuf_finish(sb);
11035 done:
11036 	sbuf_delete(sb);
11037 	free(buf, M_CXGBE);
11038 	return (rc);
11039 }
11040 
11041 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)11042 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
11043 {
11044 	struct adapter *sc = arg1;
11045 	struct sbuf *sb;
11046 	int rc;
11047 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
11048 
11049 	rc = 0;
11050 	mtx_lock(&sc->reg_lock);
11051 	if (hw_off_limits(sc))
11052 		rc = ENXIO;
11053 	else
11054 		t4_get_chan_txrate(sc, nrate, orate);
11055 	mtx_unlock(&sc->reg_lock);
11056 	if (rc != 0)
11057 		return (rc);
11058 
11059 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11060 	if (sb == NULL)
11061 		return (ENOMEM);
11062 
11063 	if (sc->chip_params->nchan > 2) {
11064 		sbuf_printf(sb, "              channel 0   channel 1"
11065 		    "   channel 2   channel 3\n");
11066 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
11067 		    nrate[0], nrate[1], nrate[2], nrate[3]);
11068 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
11069 		    orate[0], orate[1], orate[2], orate[3]);
11070 	} else {
11071 		sbuf_printf(sb, "              channel 0   channel 1\n");
11072 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
11073 		    nrate[0], nrate[1]);
11074 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
11075 		    orate[0], orate[1]);
11076 	}
11077 
11078 	rc = sbuf_finish(sb);
11079 	sbuf_delete(sb);
11080 
11081 	return (rc);
11082 }
11083 
11084 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)11085 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
11086 {
11087 	struct adapter *sc = arg1;
11088 	struct sbuf *sb;
11089 	uint32_t *buf, *p;
11090 	int rc, i;
11091 
11092 	rc = 0;
11093 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11094 	if (sb == NULL)
11095 		return (ENOMEM);
11096 
11097 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
11098 	    M_ZERO | M_WAITOK);
11099 
11100 	mtx_lock(&sc->reg_lock);
11101 	if (hw_off_limits(sc))
11102 		rc = ENXIO;
11103 	else
11104 		t4_ulprx_read_la(sc, buf);
11105 	mtx_unlock(&sc->reg_lock);
11106 	if (rc != 0)
11107 		goto done;
11108 
11109 	p = buf;
11110 	sbuf_printf(sb, "      Pcmd        Type   Message"
11111 	    "                Data");
11112 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
11113 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
11114 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
11115 	}
11116 	rc = sbuf_finish(sb);
11117 done:
11118 	sbuf_delete(sb);
11119 	free(buf, M_CXGBE);
11120 	return (rc);
11121 }
11122 
11123 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)11124 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
11125 {
11126 	struct adapter *sc = arg1;
11127 	struct sbuf *sb;
11128 	int rc;
11129 	uint32_t cfg, s1, s2;
11130 
11131 	MPASS(chip_id(sc) >= CHELSIO_T5);
11132 
11133 	rc = 0;
11134 	mtx_lock(&sc->reg_lock);
11135 	if (hw_off_limits(sc))
11136 		rc = ENXIO;
11137 	else {
11138 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
11139 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
11140 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
11141 	}
11142 	mtx_unlock(&sc->reg_lock);
11143 	if (rc != 0)
11144 		return (rc);
11145 
11146 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11147 	if (sb == NULL)
11148 		return (ENOMEM);
11149 
11150 	if (G_STATSOURCE_T5(cfg) == 7) {
11151 		int mode;
11152 
11153 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
11154 		if (mode == 0)
11155 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
11156 		else if (mode == 1)
11157 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
11158 		else
11159 			sbuf_printf(sb, "unknown mode %d", mode);
11160 	}
11161 	rc = sbuf_finish(sb);
11162 	sbuf_delete(sb);
11163 
11164 	return (rc);
11165 }
11166 
11167 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)11168 sysctl_cpus(SYSCTL_HANDLER_ARGS)
11169 {
11170 	struct adapter *sc = arg1;
11171 	enum cpu_sets op = arg2;
11172 	cpuset_t cpuset;
11173 	struct sbuf *sb;
11174 	int i, rc;
11175 
11176 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
11177 
11178 	CPU_ZERO(&cpuset);
11179 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
11180 	if (rc != 0)
11181 		return (rc);
11182 
11183 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11184 	if (sb == NULL)
11185 		return (ENOMEM);
11186 
11187 	CPU_FOREACH(i)
11188 		sbuf_printf(sb, "%d ", i);
11189 	rc = sbuf_finish(sb);
11190 	sbuf_delete(sb);
11191 
11192 	return (rc);
11193 }
11194 
11195 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)11196 sysctl_reset(SYSCTL_HANDLER_ARGS)
11197 {
11198 	struct adapter *sc = arg1;
11199 	u_int val;
11200 	int rc;
11201 
11202 	val = atomic_load_int(&sc->num_resets);
11203 	rc = sysctl_handle_int(oidp, &val, 0, req);
11204 	if (rc != 0 || req->newptr == NULL)
11205 		return (rc);
11206 
11207 	if (val == 0) {
11208 		/* Zero out the counter that tracks reset. */
11209 		atomic_store_int(&sc->num_resets, 0);
11210 		return (0);
11211 	}
11212 
11213 	if (val != 1)
11214 		return (EINVAL);	/* 0 or 1 are the only legal values */
11215 
11216 	if (hw_off_limits(sc))		/* harmless race */
11217 		return (EALREADY);
11218 
11219 	taskqueue_enqueue(reset_tq, &sc->reset_task);
11220 	return (0);
11221 }
11222 
11223 #ifdef TCP_OFFLOAD
11224 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)11225 sysctl_tls(SYSCTL_HANDLER_ARGS)
11226 {
11227 	struct adapter *sc = arg1;
11228 	int i, j, v, rc;
11229 	struct vi_info *vi;
11230 
11231 	v = sc->tt.tls;
11232 	rc = sysctl_handle_int(oidp, &v, 0, req);
11233 	if (rc != 0 || req->newptr == NULL)
11234 		return (rc);
11235 
11236 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11237 		return (ENOTSUP);
11238 
11239 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
11240 	if (rc)
11241 		return (rc);
11242 	if (hw_off_limits(sc))
11243 		rc = ENXIO;
11244 	else {
11245 		sc->tt.tls = !!v;
11246 		for_each_port(sc, i) {
11247 			for_each_vi(sc->port[i], j, vi) {
11248 				if (vi->flags & VI_INIT_DONE)
11249 					t4_update_fl_bufsize(vi->ifp);
11250 			}
11251 		}
11252 	}
11253 	end_synchronized_op(sc, 0);
11254 
11255 	return (rc);
11256 
11257 }
11258 
11259 static int
sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS)11260 sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS)
11261 {
11262 	struct adapter *sc = arg1;
11263 	int *old_ports, *new_ports;
11264 	int i, new_count, rc;
11265 
11266 	if (req->newptr == NULL && req->oldptr == NULL)
11267 		return (SYSCTL_OUT(req, NULL, imax(sc->tt.num_tls_rx_ports, 1) *
11268 		    sizeof(sc->tt.tls_rx_ports[0])));
11269 
11270 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tlsrx");
11271 	if (rc)
11272 		return (rc);
11273 
11274 	if (hw_off_limits(sc)) {
11275 		rc = ENXIO;
11276 		goto done;
11277 	}
11278 
11279 	if (sc->tt.num_tls_rx_ports == 0) {
11280 		i = -1;
11281 		rc = SYSCTL_OUT(req, &i, sizeof(i));
11282 	} else
11283 		rc = SYSCTL_OUT(req, sc->tt.tls_rx_ports,
11284 		    sc->tt.num_tls_rx_ports * sizeof(sc->tt.tls_rx_ports[0]));
11285 	if (rc == 0 && req->newptr != NULL) {
11286 		new_count = req->newlen / sizeof(new_ports[0]);
11287 		new_ports = malloc(new_count * sizeof(new_ports[0]), M_CXGBE,
11288 		    M_WAITOK);
11289 		rc = SYSCTL_IN(req, new_ports, new_count *
11290 		    sizeof(new_ports[0]));
11291 		if (rc)
11292 			goto err;
11293 
11294 		/* Allow setting to a single '-1' to clear the list. */
11295 		if (new_count == 1 && new_ports[0] == -1) {
11296 			ADAPTER_LOCK(sc);
11297 			old_ports = sc->tt.tls_rx_ports;
11298 			sc->tt.tls_rx_ports = NULL;
11299 			sc->tt.num_tls_rx_ports = 0;
11300 			ADAPTER_UNLOCK(sc);
11301 			free(old_ports, M_CXGBE);
11302 		} else {
11303 			for (i = 0; i < new_count; i++) {
11304 				if (new_ports[i] < 1 ||
11305 				    new_ports[i] > IPPORT_MAX) {
11306 					rc = EINVAL;
11307 					goto err;
11308 				}
11309 			}
11310 
11311 			ADAPTER_LOCK(sc);
11312 			old_ports = sc->tt.tls_rx_ports;
11313 			sc->tt.tls_rx_ports = new_ports;
11314 			sc->tt.num_tls_rx_ports = new_count;
11315 			ADAPTER_UNLOCK(sc);
11316 			free(old_ports, M_CXGBE);
11317 			new_ports = NULL;
11318 		}
11319 	err:
11320 		free(new_ports, M_CXGBE);
11321 	}
11322 done:
11323 	end_synchronized_op(sc, 0);
11324 	return (rc);
11325 }
11326 
11327 static int
sysctl_tls_rx_timeout(SYSCTL_HANDLER_ARGS)11328 sysctl_tls_rx_timeout(SYSCTL_HANDLER_ARGS)
11329 {
11330 	struct adapter *sc = arg1;
11331 	int v, rc;
11332 
11333 	v = sc->tt.tls_rx_timeout;
11334 	rc = sysctl_handle_int(oidp, &v, 0, req);
11335 	if (rc != 0 || req->newptr == NULL)
11336 		return (rc);
11337 
11338 	if (v < 0)
11339 		return (EINVAL);
11340 
11341 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11342 		return (ENOTSUP);
11343 
11344 	sc->tt.tls_rx_timeout = v;
11345 
11346 	return (0);
11347 
11348 }
11349 
11350 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)11351 unit_conv(char *buf, size_t len, u_int val, u_int factor)
11352 {
11353 	u_int rem = val % factor;
11354 
11355 	if (rem == 0)
11356 		snprintf(buf, len, "%u", val / factor);
11357 	else {
11358 		while (rem % 10 == 0)
11359 			rem /= 10;
11360 		snprintf(buf, len, "%u.%u", val / factor, rem);
11361 	}
11362 }
11363 
11364 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)11365 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
11366 {
11367 	struct adapter *sc = arg1;
11368 	char buf[16];
11369 	u_int res, re;
11370 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11371 
11372 	mtx_lock(&sc->reg_lock);
11373 	if (hw_off_limits(sc))
11374 		res = (u_int)-1;
11375 	else
11376 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
11377 	mtx_unlock(&sc->reg_lock);
11378 	if (res == (u_int)-1)
11379 		return (ENXIO);
11380 
11381 	switch (arg2) {
11382 	case 0:
11383 		/* timer_tick */
11384 		re = G_TIMERRESOLUTION(res);
11385 		break;
11386 	case 1:
11387 		/* TCP timestamp tick */
11388 		re = G_TIMESTAMPRESOLUTION(res);
11389 		break;
11390 	case 2:
11391 		/* DACK tick */
11392 		re = G_DELAYEDACKRESOLUTION(res);
11393 		break;
11394 	default:
11395 		return (EDOOFUS);
11396 	}
11397 
11398 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
11399 
11400 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
11401 }
11402 
11403 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)11404 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
11405 {
11406 	struct adapter *sc = arg1;
11407 	int rc;
11408 	u_int dack_tmr, dack_re, v;
11409 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11410 
11411 	mtx_lock(&sc->reg_lock);
11412 	if (hw_off_limits(sc))
11413 		rc = ENXIO;
11414 	else {
11415 		rc = 0;
11416 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
11417 		    A_TP_TIMER_RESOLUTION));
11418 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
11419 	}
11420 	mtx_unlock(&sc->reg_lock);
11421 	if (rc != 0)
11422 		return (rc);
11423 
11424 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
11425 
11426 	return (sysctl_handle_int(oidp, &v, 0, req));
11427 }
11428 
11429 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)11430 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
11431 {
11432 	struct adapter *sc = arg1;
11433 	int rc, reg = arg2;
11434 	u_int tre;
11435 	u_long tp_tick_us, v;
11436 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11437 
11438 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
11439 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
11440 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
11441 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
11442 
11443 	mtx_lock(&sc->reg_lock);
11444 	if (hw_off_limits(sc))
11445 		rc = ENXIO;
11446 	else {
11447 		rc = 0;
11448 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
11449 		tp_tick_us = (cclk_ps << tre) / 1000000;
11450 		if (reg == A_TP_INIT_SRTT)
11451 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
11452 		else
11453 			v = tp_tick_us * t4_read_reg(sc, reg);
11454 	}
11455 	mtx_unlock(&sc->reg_lock);
11456 	if (rc != 0)
11457 		return (rc);
11458 	else
11459 		return (sysctl_handle_long(oidp, &v, 0, req));
11460 }
11461 
11462 /*
11463  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
11464  * passed to this function.
11465  */
11466 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)11467 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
11468 {
11469 	struct adapter *sc = arg1;
11470 	int rc, idx = arg2;
11471 	u_int v;
11472 
11473 	MPASS(idx >= 0 && idx <= 24);
11474 
11475 	mtx_lock(&sc->reg_lock);
11476 	if (hw_off_limits(sc))
11477 		rc = ENXIO;
11478 	else {
11479 		rc = 0;
11480 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
11481 	}
11482 	mtx_unlock(&sc->reg_lock);
11483 	if (rc != 0)
11484 		return (rc);
11485 	else
11486 		return (sysctl_handle_int(oidp, &v, 0, req));
11487 }
11488 
11489 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)11490 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
11491 {
11492 	struct adapter *sc = arg1;
11493 	int rc, idx = arg2;
11494 	u_int shift, v, r;
11495 
11496 	MPASS(idx >= 0 && idx < 16);
11497 
11498 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
11499 	shift = (idx & 3) << 3;
11500 	mtx_lock(&sc->reg_lock);
11501 	if (hw_off_limits(sc))
11502 		rc = ENXIO;
11503 	else {
11504 		rc = 0;
11505 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
11506 	}
11507 	mtx_unlock(&sc->reg_lock);
11508 	if (rc != 0)
11509 		return (rc);
11510 	else
11511 		return (sysctl_handle_int(oidp, &v, 0, req));
11512 }
11513 
11514 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)11515 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
11516 {
11517 	struct vi_info *vi = arg1;
11518 	struct adapter *sc = vi->adapter;
11519 	int idx, rc, i;
11520 	struct sge_ofld_rxq *ofld_rxq;
11521 	uint8_t v;
11522 
11523 	idx = vi->ofld_tmr_idx;
11524 
11525 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11526 	if (rc != 0 || req->newptr == NULL)
11527 		return (rc);
11528 
11529 	if (idx < 0 || idx >= SGE_NTIMERS)
11530 		return (EINVAL);
11531 
11532 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11533 	    "t4otmr");
11534 	if (rc)
11535 		return (rc);
11536 
11537 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
11538 	for_each_ofld_rxq(vi, i, ofld_rxq) {
11539 #ifdef atomic_store_rel_8
11540 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
11541 #else
11542 		ofld_rxq->iq.intr_params = v;
11543 #endif
11544 	}
11545 	vi->ofld_tmr_idx = idx;
11546 
11547 	end_synchronized_op(sc, LOCK_HELD);
11548 	return (0);
11549 }
11550 
11551 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)11552 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
11553 {
11554 	struct vi_info *vi = arg1;
11555 	struct adapter *sc = vi->adapter;
11556 	int idx, rc;
11557 
11558 	idx = vi->ofld_pktc_idx;
11559 
11560 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11561 	if (rc != 0 || req->newptr == NULL)
11562 		return (rc);
11563 
11564 	if (idx < -1 || idx >= SGE_NCOUNTERS)
11565 		return (EINVAL);
11566 
11567 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11568 	    "t4opktc");
11569 	if (rc)
11570 		return (rc);
11571 
11572 	if (vi->flags & VI_INIT_DONE)
11573 		rc = EBUSY; /* cannot be changed once the queues are created */
11574 	else
11575 		vi->ofld_pktc_idx = idx;
11576 
11577 	end_synchronized_op(sc, LOCK_HELD);
11578 	return (rc);
11579 }
11580 #endif
11581 
11582 static int
get_sge_context(struct adapter * sc,struct t4_sge_context * cntxt)11583 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
11584 {
11585 	int rc;
11586 
11587 	if (cntxt->cid > M_CTXTQID)
11588 		return (EINVAL);
11589 
11590 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
11591 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
11592 		return (EINVAL);
11593 
11594 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
11595 	if (rc)
11596 		return (rc);
11597 
11598 	if (hw_off_limits(sc)) {
11599 		rc = ENXIO;
11600 		goto done;
11601 	}
11602 
11603 	if (sc->flags & FW_OK) {
11604 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
11605 		    &cntxt->data[0]);
11606 		if (rc == 0)
11607 			goto done;
11608 	}
11609 
11610 	/*
11611 	 * Read via firmware failed or wasn't even attempted.  Read directly via
11612 	 * the backdoor.
11613 	 */
11614 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
11615 done:
11616 	end_synchronized_op(sc, 0);
11617 	return (rc);
11618 }
11619 
11620 static int
load_fw(struct adapter * sc,struct t4_data * fw)11621 load_fw(struct adapter *sc, struct t4_data *fw)
11622 {
11623 	int rc;
11624 	uint8_t *fw_data;
11625 
11626 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
11627 	if (rc)
11628 		return (rc);
11629 
11630 	if (hw_off_limits(sc)) {
11631 		rc = ENXIO;
11632 		goto done;
11633 	}
11634 
11635 	/*
11636 	 * The firmware, with the sole exception of the memory parity error
11637 	 * handler, runs from memory and not flash.  It is almost always safe to
11638 	 * install a new firmware on a running system.  Just set bit 1 in
11639 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
11640 	 */
11641 	if (sc->flags & FULL_INIT_DONE &&
11642 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
11643 		rc = EBUSY;
11644 		goto done;
11645 	}
11646 
11647 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
11648 
11649 	rc = copyin(fw->data, fw_data, fw->len);
11650 	if (rc == 0)
11651 		rc = -t4_load_fw(sc, fw_data, fw->len);
11652 
11653 	free(fw_data, M_CXGBE);
11654 done:
11655 	end_synchronized_op(sc, 0);
11656 	return (rc);
11657 }
11658 
11659 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)11660 load_cfg(struct adapter *sc, struct t4_data *cfg)
11661 {
11662 	int rc;
11663 	uint8_t *cfg_data = NULL;
11664 
11665 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11666 	if (rc)
11667 		return (rc);
11668 
11669 	if (hw_off_limits(sc)) {
11670 		rc = ENXIO;
11671 		goto done;
11672 	}
11673 
11674 	if (cfg->len == 0) {
11675 		/* clear */
11676 		rc = -t4_load_cfg(sc, NULL, 0);
11677 		goto done;
11678 	}
11679 
11680 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
11681 
11682 	rc = copyin(cfg->data, cfg_data, cfg->len);
11683 	if (rc == 0)
11684 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
11685 
11686 	free(cfg_data, M_CXGBE);
11687 done:
11688 	end_synchronized_op(sc, 0);
11689 	return (rc);
11690 }
11691 
11692 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)11693 load_boot(struct adapter *sc, struct t4_bootrom *br)
11694 {
11695 	int rc;
11696 	uint8_t *br_data = NULL;
11697 	u_int offset;
11698 
11699 	if (br->len > 1024 * 1024)
11700 		return (EFBIG);
11701 
11702 	if (br->pf_offset == 0) {
11703 		/* pfidx */
11704 		if (br->pfidx_addr > 7)
11705 			return (EINVAL);
11706 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
11707 		    A_PCIE_PF_EXPROM_OFST)));
11708 	} else if (br->pf_offset == 1) {
11709 		/* offset */
11710 		offset = G_OFFSET(br->pfidx_addr);
11711 	} else {
11712 		return (EINVAL);
11713 	}
11714 
11715 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
11716 	if (rc)
11717 		return (rc);
11718 
11719 	if (hw_off_limits(sc)) {
11720 		rc = ENXIO;
11721 		goto done;
11722 	}
11723 
11724 	if (br->len == 0) {
11725 		/* clear */
11726 		rc = -t4_load_boot(sc, NULL, offset, 0);
11727 		goto done;
11728 	}
11729 
11730 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
11731 
11732 	rc = copyin(br->data, br_data, br->len);
11733 	if (rc == 0)
11734 		rc = -t4_load_boot(sc, br_data, offset, br->len);
11735 
11736 	free(br_data, M_CXGBE);
11737 done:
11738 	end_synchronized_op(sc, 0);
11739 	return (rc);
11740 }
11741 
11742 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)11743 load_bootcfg(struct adapter *sc, struct t4_data *bc)
11744 {
11745 	int rc;
11746 	uint8_t *bc_data = NULL;
11747 
11748 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11749 	if (rc)
11750 		return (rc);
11751 
11752 	if (hw_off_limits(sc)) {
11753 		rc = ENXIO;
11754 		goto done;
11755 	}
11756 
11757 	if (bc->len == 0) {
11758 		/* clear */
11759 		rc = -t4_load_bootcfg(sc, NULL, 0);
11760 		goto done;
11761 	}
11762 
11763 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
11764 
11765 	rc = copyin(bc->data, bc_data, bc->len);
11766 	if (rc == 0)
11767 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
11768 
11769 	free(bc_data, M_CXGBE);
11770 done:
11771 	end_synchronized_op(sc, 0);
11772 	return (rc);
11773 }
11774 
11775 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)11776 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
11777 {
11778 	int rc;
11779 	struct cudbg_init *cudbg;
11780 	void *handle, *buf;
11781 
11782 	/* buf is large, don't block if no memory is available */
11783 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
11784 	if (buf == NULL)
11785 		return (ENOMEM);
11786 
11787 	handle = cudbg_alloc_handle();
11788 	if (handle == NULL) {
11789 		rc = ENOMEM;
11790 		goto done;
11791 	}
11792 
11793 	cudbg = cudbg_get_init(handle);
11794 	cudbg->adap = sc;
11795 	cudbg->print = (cudbg_print_cb)printf;
11796 
11797 #ifndef notyet
11798 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
11799 	    __func__, dump->wr_flash, dump->len, dump->data);
11800 #endif
11801 
11802 	if (dump->wr_flash)
11803 		cudbg->use_flash = 1;
11804 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
11805 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
11806 
11807 	rc = cudbg_collect(handle, buf, &dump->len);
11808 	if (rc != 0)
11809 		goto done;
11810 
11811 	rc = copyout(buf, dump->data, dump->len);
11812 done:
11813 	cudbg_free_handle(handle);
11814 	free(buf, M_CXGBE);
11815 	return (rc);
11816 }
11817 
11818 static void
free_offload_policy(struct t4_offload_policy * op)11819 free_offload_policy(struct t4_offload_policy *op)
11820 {
11821 	struct offload_rule *r;
11822 	int i;
11823 
11824 	if (op == NULL)
11825 		return;
11826 
11827 	r = &op->rule[0];
11828 	for (i = 0; i < op->nrules; i++, r++) {
11829 		free(r->bpf_prog.bf_insns, M_CXGBE);
11830 	}
11831 	free(op->rule, M_CXGBE);
11832 	free(op, M_CXGBE);
11833 }
11834 
11835 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)11836 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
11837 {
11838 	int i, rc, len;
11839 	struct t4_offload_policy *op, *old;
11840 	struct bpf_program *bf;
11841 	const struct offload_settings *s;
11842 	struct offload_rule *r;
11843 	void *u;
11844 
11845 	if (!is_offload(sc))
11846 		return (ENODEV);
11847 
11848 	if (uop->nrules == 0) {
11849 		/* Delete installed policies. */
11850 		op = NULL;
11851 		goto set_policy;
11852 	} else if (uop->nrules > 256) { /* arbitrary */
11853 		return (E2BIG);
11854 	}
11855 
11856 	/* Copy userspace offload policy to kernel */
11857 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
11858 	op->nrules = uop->nrules;
11859 	len = op->nrules * sizeof(struct offload_rule);
11860 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11861 	rc = copyin(uop->rule, op->rule, len);
11862 	if (rc) {
11863 		free(op->rule, M_CXGBE);
11864 		free(op, M_CXGBE);
11865 		return (rc);
11866 	}
11867 
11868 	r = &op->rule[0];
11869 	for (i = 0; i < op->nrules; i++, r++) {
11870 
11871 		/* Validate open_type */
11872 		if (r->open_type != OPEN_TYPE_LISTEN &&
11873 		    r->open_type != OPEN_TYPE_ACTIVE &&
11874 		    r->open_type != OPEN_TYPE_PASSIVE &&
11875 		    r->open_type != OPEN_TYPE_DONTCARE) {
11876 error:
11877 			/*
11878 			 * Rules 0 to i have malloc'd filters that need to be
11879 			 * freed.  Rules i+1 to nrules have userspace pointers
11880 			 * and should be left alone.
11881 			 */
11882 			op->nrules = i;
11883 			free_offload_policy(op);
11884 			return (rc);
11885 		}
11886 
11887 		/* Validate settings */
11888 		s = &r->settings;
11889 		if ((s->offload != 0 && s->offload != 1) ||
11890 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
11891 		    s->sched_class < -1 ||
11892 		    s->sched_class >= sc->params.nsched_cls) {
11893 			rc = EINVAL;
11894 			goto error;
11895 		}
11896 
11897 		bf = &r->bpf_prog;
11898 		u = bf->bf_insns;	/* userspace ptr */
11899 		bf->bf_insns = NULL;
11900 		if (bf->bf_len == 0) {
11901 			/* legal, matches everything */
11902 			continue;
11903 		}
11904 		len = bf->bf_len * sizeof(*bf->bf_insns);
11905 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11906 		rc = copyin(u, bf->bf_insns, len);
11907 		if (rc != 0)
11908 			goto error;
11909 
11910 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
11911 			rc = EINVAL;
11912 			goto error;
11913 		}
11914 	}
11915 set_policy:
11916 	rw_wlock(&sc->policy_lock);
11917 	old = sc->policy;
11918 	sc->policy = op;
11919 	rw_wunlock(&sc->policy_lock);
11920 	free_offload_policy(old);
11921 
11922 	return (0);
11923 }
11924 
11925 #define MAX_READ_BUF_SIZE (128 * 1024)
11926 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)11927 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
11928 {
11929 	uint32_t addr, remaining, n;
11930 	uint32_t *buf;
11931 	int rc;
11932 	uint8_t *dst;
11933 
11934 	mtx_lock(&sc->reg_lock);
11935 	if (hw_off_limits(sc))
11936 		rc = ENXIO;
11937 	else
11938 		rc = validate_mem_range(sc, mr->addr, mr->len);
11939 	mtx_unlock(&sc->reg_lock);
11940 	if (rc != 0)
11941 		return (rc);
11942 
11943 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
11944 	addr = mr->addr;
11945 	remaining = mr->len;
11946 	dst = (void *)mr->data;
11947 
11948 	while (remaining) {
11949 		n = min(remaining, MAX_READ_BUF_SIZE);
11950 		mtx_lock(&sc->reg_lock);
11951 		if (hw_off_limits(sc))
11952 			rc = ENXIO;
11953 		else
11954 			read_via_memwin(sc, 2, addr, buf, n);
11955 		mtx_unlock(&sc->reg_lock);
11956 		if (rc != 0)
11957 			break;
11958 
11959 		rc = copyout(buf, dst, n);
11960 		if (rc != 0)
11961 			break;
11962 
11963 		dst += n;
11964 		remaining -= n;
11965 		addr += n;
11966 	}
11967 
11968 	free(buf, M_CXGBE);
11969 	return (rc);
11970 }
11971 #undef MAX_READ_BUF_SIZE
11972 
11973 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)11974 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
11975 {
11976 	int rc;
11977 
11978 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
11979 		return (EINVAL);
11980 
11981 	if (i2cd->len > sizeof(i2cd->data))
11982 		return (EFBIG);
11983 
11984 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
11985 	if (rc)
11986 		return (rc);
11987 	if (hw_off_limits(sc))
11988 		rc = ENXIO;
11989 	else
11990 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
11991 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
11992 	end_synchronized_op(sc, 0);
11993 
11994 	return (rc);
11995 }
11996 
11997 static int
clear_stats(struct adapter * sc,u_int port_id)11998 clear_stats(struct adapter *sc, u_int port_id)
11999 {
12000 	int i, v, chan_map;
12001 	struct port_info *pi;
12002 	struct vi_info *vi;
12003 	struct sge_rxq *rxq;
12004 	struct sge_txq *txq;
12005 	struct sge_wrq *wrq;
12006 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12007 	struct sge_ofld_txq *ofld_txq;
12008 #endif
12009 #ifdef TCP_OFFLOAD
12010 	struct sge_ofld_rxq *ofld_rxq;
12011 #endif
12012 
12013 	if (port_id >= sc->params.nports)
12014 		return (EINVAL);
12015 	pi = sc->port[port_id];
12016 	if (pi == NULL)
12017 		return (EIO);
12018 
12019 	mtx_lock(&sc->reg_lock);
12020 	if (!hw_off_limits(sc)) {
12021 		/* MAC stats */
12022 		t4_clr_port_stats(sc, pi->tx_chan);
12023 		if (is_t6(sc)) {
12024 			if (pi->fcs_reg != -1)
12025 				pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12026 			else
12027 				pi->stats.rx_fcs_err = 0;
12028 		}
12029 		for_each_vi(pi, v, vi) {
12030 			if (vi->flags & VI_INIT_DONE)
12031 				t4_clr_vi_stats(sc, vi->vin);
12032 		}
12033 		chan_map = pi->rx_e_chan_map;
12034 		v = 0;	/* reuse */
12035 		while (chan_map) {
12036 			i = ffs(chan_map) - 1;
12037 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
12038 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
12039 			chan_map &= ~(1 << i);
12040 		}
12041 	}
12042 	mtx_unlock(&sc->reg_lock);
12043 	pi->tx_parse_error = 0;
12044 	pi->tnl_cong_drops = 0;
12045 
12046 	/*
12047 	 * Since this command accepts a port, clear stats for
12048 	 * all VIs on this port.
12049 	 */
12050 	for_each_vi(pi, v, vi) {
12051 		if (vi->flags & VI_INIT_DONE) {
12052 
12053 			for_each_rxq(vi, i, rxq) {
12054 #if defined(INET) || defined(INET6)
12055 				rxq->lro.lro_queued = 0;
12056 				rxq->lro.lro_flushed = 0;
12057 #endif
12058 				rxq->rxcsum = 0;
12059 				rxq->vlan_extraction = 0;
12060 				rxq->vxlan_rxcsum = 0;
12061 
12062 				rxq->fl.cl_allocated = 0;
12063 				rxq->fl.cl_recycled = 0;
12064 				rxq->fl.cl_fast_recycled = 0;
12065 			}
12066 
12067 			for_each_txq(vi, i, txq) {
12068 				txq->txcsum = 0;
12069 				txq->tso_wrs = 0;
12070 				txq->vlan_insertion = 0;
12071 				txq->imm_wrs = 0;
12072 				txq->sgl_wrs = 0;
12073 				txq->txpkt_wrs = 0;
12074 				txq->txpkts0_wrs = 0;
12075 				txq->txpkts1_wrs = 0;
12076 				txq->txpkts0_pkts = 0;
12077 				txq->txpkts1_pkts = 0;
12078 				txq->txpkts_flush = 0;
12079 				txq->raw_wrs = 0;
12080 				txq->vxlan_tso_wrs = 0;
12081 				txq->vxlan_txcsum = 0;
12082 				txq->kern_tls_records = 0;
12083 				txq->kern_tls_short = 0;
12084 				txq->kern_tls_partial = 0;
12085 				txq->kern_tls_full = 0;
12086 				txq->kern_tls_octets = 0;
12087 				txq->kern_tls_waste = 0;
12088 				txq->kern_tls_options = 0;
12089 				txq->kern_tls_header = 0;
12090 				txq->kern_tls_fin = 0;
12091 				txq->kern_tls_fin_short = 0;
12092 				txq->kern_tls_cbc = 0;
12093 				txq->kern_tls_gcm = 0;
12094 				mp_ring_reset_stats(txq->r);
12095 			}
12096 
12097 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12098 			for_each_ofld_txq(vi, i, ofld_txq) {
12099 				ofld_txq->wrq.tx_wrs_direct = 0;
12100 				ofld_txq->wrq.tx_wrs_copied = 0;
12101 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12102 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12103 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12104 				counter_u64_zero(ofld_txq->tx_aio_jobs);
12105 				counter_u64_zero(ofld_txq->tx_aio_octets);
12106 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
12107 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
12108 			}
12109 #endif
12110 #ifdef TCP_OFFLOAD
12111 			for_each_ofld_rxq(vi, i, ofld_rxq) {
12112 				ofld_rxq->fl.cl_allocated = 0;
12113 				ofld_rxq->fl.cl_recycled = 0;
12114 				ofld_rxq->fl.cl_fast_recycled = 0;
12115 				counter_u64_zero(
12116 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
12117 				counter_u64_zero(
12118 				    ofld_rxq->rx_iscsi_ddp_setup_error);
12119 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
12120 				ofld_rxq->rx_iscsi_ddp_octets = 0;
12121 				ofld_rxq->rx_iscsi_fl_pdus = 0;
12122 				ofld_rxq->rx_iscsi_fl_octets = 0;
12123 				ofld_rxq->rx_aio_ddp_jobs = 0;
12124 				ofld_rxq->rx_aio_ddp_octets = 0;
12125 				ofld_rxq->rx_toe_tls_records = 0;
12126 				ofld_rxq->rx_toe_tls_octets = 0;
12127 			}
12128 #endif
12129 
12130 			if (IS_MAIN_VI(vi)) {
12131 				wrq = &sc->sge.ctrlq[pi->port_id];
12132 				wrq->tx_wrs_direct = 0;
12133 				wrq->tx_wrs_copied = 0;
12134 			}
12135 		}
12136 	}
12137 
12138 	return (0);
12139 }
12140 
12141 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12142 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12143 {
12144 #ifdef INET6
12145 	struct in6_addr in6;
12146 
12147 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12148 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
12149 		return (0);
12150 	else
12151 		return (EIO);
12152 #else
12153 	return (ENOTSUP);
12154 #endif
12155 }
12156 
12157 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12158 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12159 {
12160 #ifdef INET6
12161 	struct in6_addr in6;
12162 
12163 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12164 	return (t4_release_clip_addr(sc, &in6));
12165 #else
12166 	return (ENOTSUP);
12167 #endif
12168 }
12169 
12170 int
t4_os_find_pci_capability(struct adapter * sc,int cap)12171 t4_os_find_pci_capability(struct adapter *sc, int cap)
12172 {
12173 	int i;
12174 
12175 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
12176 }
12177 
12178 int
t4_os_pci_save_state(struct adapter * sc)12179 t4_os_pci_save_state(struct adapter *sc)
12180 {
12181 	device_t dev;
12182 	struct pci_devinfo *dinfo;
12183 
12184 	dev = sc->dev;
12185 	dinfo = device_get_ivars(dev);
12186 
12187 	pci_cfg_save(dev, dinfo, 0);
12188 	return (0);
12189 }
12190 
12191 int
t4_os_pci_restore_state(struct adapter * sc)12192 t4_os_pci_restore_state(struct adapter *sc)
12193 {
12194 	device_t dev;
12195 	struct pci_devinfo *dinfo;
12196 
12197 	dev = sc->dev;
12198 	dinfo = device_get_ivars(dev);
12199 
12200 	pci_cfg_restore(dev, dinfo);
12201 	return (0);
12202 }
12203 
12204 void
t4_os_portmod_changed(struct port_info * pi)12205 t4_os_portmod_changed(struct port_info *pi)
12206 {
12207 	struct adapter *sc = pi->adapter;
12208 	struct vi_info *vi;
12209 	struct ifnet *ifp;
12210 	static const char *mod_str[] = {
12211 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
12212 	};
12213 
12214 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
12215 	    ("%s: port_type %u", __func__, pi->port_type));
12216 
12217 	vi = &pi->vi[0];
12218 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
12219 		PORT_LOCK(pi);
12220 		build_medialist(pi);
12221 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
12222 			fixup_link_config(pi);
12223 			apply_link_config(pi);
12224 		}
12225 		PORT_UNLOCK(pi);
12226 		end_synchronized_op(sc, LOCK_HELD);
12227 	}
12228 
12229 	ifp = vi->ifp;
12230 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
12231 		if_printf(ifp, "transceiver unplugged.\n");
12232 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
12233 		if_printf(ifp, "unknown transceiver inserted.\n");
12234 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
12235 		if_printf(ifp, "unsupported transceiver inserted.\n");
12236 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
12237 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
12238 		    port_top_speed(pi), mod_str[pi->mod_type]);
12239 	} else {
12240 		if_printf(ifp, "transceiver (type %d) inserted.\n",
12241 		    pi->mod_type);
12242 	}
12243 }
12244 
12245 void
t4_os_link_changed(struct port_info * pi)12246 t4_os_link_changed(struct port_info *pi)
12247 {
12248 	struct vi_info *vi;
12249 	struct ifnet *ifp;
12250 	struct link_config *lc = &pi->link_cfg;
12251 	struct adapter *sc = pi->adapter;
12252 	int v;
12253 
12254 	PORT_LOCK_ASSERT_OWNED(pi);
12255 
12256 	if (is_t6(sc)) {
12257 		if (lc->link_ok) {
12258 			if (lc->speed > 25000 ||
12259 			    (lc->speed == 25000 && lc->fec == FEC_RS)) {
12260 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12261 				    A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS);
12262 			} else {
12263 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12264 				    A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS);
12265 			}
12266 			pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12267 			pi->stats.rx_fcs_err = 0;
12268 		} else {
12269 			pi->fcs_reg = -1;
12270 		}
12271 	} else {
12272 		MPASS(pi->fcs_reg != -1);
12273 		MPASS(pi->fcs_base == 0);
12274 	}
12275 
12276 	for_each_vi(pi, v, vi) {
12277 		ifp = vi->ifp;
12278 		if (ifp == NULL || IS_DETACHING(vi))
12279 			continue;
12280 
12281 		if (lc->link_ok) {
12282 			ifp->if_baudrate = IF_Mbps(lc->speed);
12283 			if_link_state_change(ifp, LINK_STATE_UP);
12284 		} else {
12285 			if_link_state_change(ifp, LINK_STATE_DOWN);
12286 		}
12287 	}
12288 }
12289 
12290 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)12291 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
12292 {
12293 	struct adapter *sc;
12294 
12295 	sx_slock(&t4_list_lock);
12296 	SLIST_FOREACH(sc, &t4_list, link) {
12297 		/*
12298 		 * func should not make any assumptions about what state sc is
12299 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
12300 		 */
12301 		func(sc, arg);
12302 	}
12303 	sx_sunlock(&t4_list_lock);
12304 }
12305 
12306 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)12307 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
12308     struct thread *td)
12309 {
12310 	int rc;
12311 	struct adapter *sc = dev->si_drv1;
12312 
12313 	rc = priv_check(td, PRIV_DRIVER);
12314 	if (rc != 0)
12315 		return (rc);
12316 
12317 	switch (cmd) {
12318 	case CHELSIO_T4_GETREG: {
12319 		struct t4_reg *edata = (struct t4_reg *)data;
12320 
12321 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12322 			return (EFAULT);
12323 
12324 		mtx_lock(&sc->reg_lock);
12325 		if (hw_off_limits(sc))
12326 			rc = ENXIO;
12327 		else if (edata->size == 4)
12328 			edata->val = t4_read_reg(sc, edata->addr);
12329 		else if (edata->size == 8)
12330 			edata->val = t4_read_reg64(sc, edata->addr);
12331 		else
12332 			rc = EINVAL;
12333 		mtx_unlock(&sc->reg_lock);
12334 
12335 		break;
12336 	}
12337 	case CHELSIO_T4_SETREG: {
12338 		struct t4_reg *edata = (struct t4_reg *)data;
12339 
12340 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12341 			return (EFAULT);
12342 
12343 		mtx_lock(&sc->reg_lock);
12344 		if (hw_off_limits(sc))
12345 			rc = ENXIO;
12346 		else if (edata->size == 4) {
12347 			if (edata->val & 0xffffffff00000000)
12348 				rc = EINVAL;
12349 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
12350 		} else if (edata->size == 8)
12351 			t4_write_reg64(sc, edata->addr, edata->val);
12352 		else
12353 			rc = EINVAL;
12354 		mtx_unlock(&sc->reg_lock);
12355 
12356 		break;
12357 	}
12358 	case CHELSIO_T4_REGDUMP: {
12359 		struct t4_regdump *regs = (struct t4_regdump *)data;
12360 		int reglen = t4_get_regs_len(sc);
12361 		uint8_t *buf;
12362 
12363 		if (regs->len < reglen) {
12364 			regs->len = reglen; /* hint to the caller */
12365 			return (ENOBUFS);
12366 		}
12367 
12368 		regs->len = reglen;
12369 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
12370 		mtx_lock(&sc->reg_lock);
12371 		if (hw_off_limits(sc))
12372 			rc = ENXIO;
12373 		else
12374 			get_regs(sc, regs, buf);
12375 		mtx_unlock(&sc->reg_lock);
12376 		if (rc == 0)
12377 			rc = copyout(buf, regs->data, reglen);
12378 		free(buf, M_CXGBE);
12379 		break;
12380 	}
12381 	case CHELSIO_T4_GET_FILTER_MODE:
12382 		rc = get_filter_mode(sc, (uint32_t *)data);
12383 		break;
12384 	case CHELSIO_T4_SET_FILTER_MODE:
12385 		rc = set_filter_mode(sc, *(uint32_t *)data);
12386 		break;
12387 	case CHELSIO_T4_SET_FILTER_MASK:
12388 		rc = set_filter_mask(sc, *(uint32_t *)data);
12389 		break;
12390 	case CHELSIO_T4_GET_FILTER:
12391 		rc = get_filter(sc, (struct t4_filter *)data);
12392 		break;
12393 	case CHELSIO_T4_SET_FILTER:
12394 		rc = set_filter(sc, (struct t4_filter *)data);
12395 		break;
12396 	case CHELSIO_T4_DEL_FILTER:
12397 		rc = del_filter(sc, (struct t4_filter *)data);
12398 		break;
12399 	case CHELSIO_T4_GET_SGE_CONTEXT:
12400 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
12401 		break;
12402 	case CHELSIO_T4_LOAD_FW:
12403 		rc = load_fw(sc, (struct t4_data *)data);
12404 		break;
12405 	case CHELSIO_T4_GET_MEM:
12406 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
12407 		break;
12408 	case CHELSIO_T4_GET_I2C:
12409 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
12410 		break;
12411 	case CHELSIO_T4_CLEAR_STATS:
12412 		rc = clear_stats(sc, *(uint32_t *)data);
12413 		break;
12414 	case CHELSIO_T4_SCHED_CLASS:
12415 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
12416 		break;
12417 	case CHELSIO_T4_SCHED_QUEUE:
12418 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
12419 		break;
12420 	case CHELSIO_T4_GET_TRACER:
12421 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
12422 		break;
12423 	case CHELSIO_T4_SET_TRACER:
12424 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
12425 		break;
12426 	case CHELSIO_T4_LOAD_CFG:
12427 		rc = load_cfg(sc, (struct t4_data *)data);
12428 		break;
12429 	case CHELSIO_T4_LOAD_BOOT:
12430 		rc = load_boot(sc, (struct t4_bootrom *)data);
12431 		break;
12432 	case CHELSIO_T4_LOAD_BOOTCFG:
12433 		rc = load_bootcfg(sc, (struct t4_data *)data);
12434 		break;
12435 	case CHELSIO_T4_CUDBG_DUMP:
12436 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
12437 		break;
12438 	case CHELSIO_T4_SET_OFLD_POLICY:
12439 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
12440 		break;
12441 	case CHELSIO_T4_HOLD_CLIP_ADDR:
12442 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
12443 		break;
12444 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
12445 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
12446 		break;
12447 	default:
12448 		rc = ENOTTY;
12449 	}
12450 
12451 	return (rc);
12452 }
12453 
12454 #ifdef TCP_OFFLOAD
12455 static int
toe_capability(struct vi_info * vi,bool enable)12456 toe_capability(struct vi_info *vi, bool enable)
12457 {
12458 	int rc;
12459 	struct port_info *pi = vi->pi;
12460 	struct adapter *sc = pi->adapter;
12461 
12462 	ASSERT_SYNCHRONIZED_OP(sc);
12463 
12464 	if (!is_offload(sc))
12465 		return (ENODEV);
12466 	if (hw_off_limits(sc))
12467 		return (ENXIO);
12468 
12469 	if (enable) {
12470 #ifdef KERN_TLS
12471 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
12472 			int i, j, n;
12473 			struct port_info *p;
12474 			struct vi_info *v;
12475 
12476 			/*
12477 			 * Reconfigure hardware for TOE if TXTLS is not enabled
12478 			 * on any ifnet.
12479 			 */
12480 			n = 0;
12481 			for_each_port(sc, i) {
12482 				p = sc->port[i];
12483 				for_each_vi(p, j, v) {
12484 					if (v->ifp->if_capenable & IFCAP_TXTLS) {
12485 						CH_WARN(sc,
12486 						    "%s has NIC TLS enabled.\n",
12487 						    device_get_nameunit(v->dev));
12488 						n++;
12489 					}
12490 				}
12491 			}
12492 			if (n > 0) {
12493 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
12494 				    "associated with this adapter before "
12495 				    "trying to enable TOE.\n");
12496 				return (EAGAIN);
12497 			}
12498 			rc = t6_config_kern_tls(sc, false);
12499 			if (rc)
12500 				return (rc);
12501 		}
12502 #endif
12503 		if ((vi->ifp->if_capenable & IFCAP_TOE) != 0) {
12504 			/* TOE is already enabled. */
12505 			return (0);
12506 		}
12507 
12508 		/*
12509 		 * We need the port's queues around so that we're able to send
12510 		 * and receive CPLs to/from the TOE even if the ifnet for this
12511 		 * port has never been UP'd administratively.
12512 		 */
12513 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
12514 			return (rc);
12515 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
12516 		    ((rc = vi_init(&pi->vi[0])) != 0))
12517 			return (rc);
12518 
12519 		if (isset(&sc->offload_map, pi->port_id)) {
12520 			/* TOE is enabled on another VI of this port. */
12521 			pi->uld_vis++;
12522 			return (0);
12523 		}
12524 
12525 		if (!uld_active(sc, ULD_TOM)) {
12526 			rc = t4_activate_uld(sc, ULD_TOM);
12527 			if (rc == EAGAIN) {
12528 				log(LOG_WARNING,
12529 				    "You must kldload t4_tom.ko before trying "
12530 				    "to enable TOE on a cxgbe interface.\n");
12531 			}
12532 			if (rc != 0)
12533 				return (rc);
12534 			KASSERT(sc->tom_softc != NULL,
12535 			    ("%s: TOM activated but softc NULL", __func__));
12536 			KASSERT(uld_active(sc, ULD_TOM),
12537 			    ("%s: TOM activated but flag not set", __func__));
12538 		}
12539 
12540 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
12541 		if (!uld_active(sc, ULD_IWARP))
12542 			(void) t4_activate_uld(sc, ULD_IWARP);
12543 		if (!uld_active(sc, ULD_ISCSI))
12544 			(void) t4_activate_uld(sc, ULD_ISCSI);
12545 
12546 		pi->uld_vis++;
12547 		setbit(&sc->offload_map, pi->port_id);
12548 	} else {
12549 		pi->uld_vis--;
12550 
12551 		if (!isset(&sc->offload_map, pi->port_id) || pi->uld_vis > 0)
12552 			return (0);
12553 
12554 		KASSERT(uld_active(sc, ULD_TOM),
12555 		    ("%s: TOM never initialized?", __func__));
12556 		clrbit(&sc->offload_map, pi->port_id);
12557 	}
12558 
12559 	return (0);
12560 }
12561 
12562 /*
12563  * Add an upper layer driver to the global list.
12564  */
12565 int
t4_register_uld(struct uld_info * ui)12566 t4_register_uld(struct uld_info *ui)
12567 {
12568 	int rc = 0;
12569 	struct uld_info *u;
12570 
12571 	sx_xlock(&t4_uld_list_lock);
12572 	SLIST_FOREACH(u, &t4_uld_list, link) {
12573 	    if (u->uld_id == ui->uld_id) {
12574 		    rc = EEXIST;
12575 		    goto done;
12576 	    }
12577 	}
12578 
12579 	SLIST_INSERT_HEAD(&t4_uld_list, ui, link);
12580 	ui->refcount = 0;
12581 done:
12582 	sx_xunlock(&t4_uld_list_lock);
12583 	return (rc);
12584 }
12585 
12586 int
t4_unregister_uld(struct uld_info * ui)12587 t4_unregister_uld(struct uld_info *ui)
12588 {
12589 	int rc = EINVAL;
12590 	struct uld_info *u;
12591 
12592 	sx_xlock(&t4_uld_list_lock);
12593 
12594 	SLIST_FOREACH(u, &t4_uld_list, link) {
12595 	    if (u == ui) {
12596 		    if (ui->refcount > 0) {
12597 			    rc = EBUSY;
12598 			    goto done;
12599 		    }
12600 
12601 		    SLIST_REMOVE(&t4_uld_list, ui, uld_info, link);
12602 		    rc = 0;
12603 		    goto done;
12604 	    }
12605 	}
12606 done:
12607 	sx_xunlock(&t4_uld_list_lock);
12608 	return (rc);
12609 }
12610 
12611 int
t4_activate_uld(struct adapter * sc,int id)12612 t4_activate_uld(struct adapter *sc, int id)
12613 {
12614 	int rc;
12615 	struct uld_info *ui;
12616 
12617 	ASSERT_SYNCHRONIZED_OP(sc);
12618 
12619 	if (id < 0 || id > ULD_MAX)
12620 		return (EINVAL);
12621 	rc = EAGAIN;	/* kldoad the module with this ULD and try again. */
12622 
12623 	sx_slock(&t4_uld_list_lock);
12624 
12625 	SLIST_FOREACH(ui, &t4_uld_list, link) {
12626 		if (ui->uld_id == id) {
12627 			if (!(sc->flags & FULL_INIT_DONE)) {
12628 				rc = adapter_init(sc);
12629 				if (rc != 0)
12630 					break;
12631 			}
12632 
12633 			rc = ui->activate(sc);
12634 			if (rc == 0) {
12635 				setbit(&sc->active_ulds, id);
12636 				ui->refcount++;
12637 			}
12638 			break;
12639 		}
12640 	}
12641 
12642 	sx_sunlock(&t4_uld_list_lock);
12643 
12644 	return (rc);
12645 }
12646 
12647 int
t4_deactivate_uld(struct adapter * sc,int id)12648 t4_deactivate_uld(struct adapter *sc, int id)
12649 {
12650 	int rc;
12651 	struct uld_info *ui;
12652 
12653 	ASSERT_SYNCHRONIZED_OP(sc);
12654 
12655 	if (id < 0 || id > ULD_MAX)
12656 		return (EINVAL);
12657 	rc = ENXIO;
12658 
12659 	sx_slock(&t4_uld_list_lock);
12660 
12661 	SLIST_FOREACH(ui, &t4_uld_list, link) {
12662 		if (ui->uld_id == id) {
12663 			rc = ui->deactivate(sc);
12664 			if (rc == 0) {
12665 				clrbit(&sc->active_ulds, id);
12666 				ui->refcount--;
12667 			}
12668 			break;
12669 		}
12670 	}
12671 
12672 	sx_sunlock(&t4_uld_list_lock);
12673 
12674 	return (rc);
12675 }
12676 
12677 static int
t4_deactivate_all_uld(struct adapter * sc)12678 t4_deactivate_all_uld(struct adapter *sc)
12679 {
12680 	int rc;
12681 	struct uld_info *ui;
12682 
12683 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
12684 	if (rc != 0)
12685 		return (ENXIO);
12686 
12687 	sx_slock(&t4_uld_list_lock);
12688 
12689 	SLIST_FOREACH(ui, &t4_uld_list, link) {
12690 		if (isset(&sc->active_ulds, ui->uld_id)) {
12691 			rc = ui->deactivate(sc);
12692 			if (rc != 0)
12693 				break;
12694 			clrbit(&sc->active_ulds, ui->uld_id);
12695 			ui->refcount--;
12696 		}
12697 	}
12698 
12699 	sx_sunlock(&t4_uld_list_lock);
12700 	end_synchronized_op(sc, 0);
12701 
12702 	return (rc);
12703 }
12704 
12705 static void
t4_async_event(struct adapter * sc)12706 t4_async_event(struct adapter *sc)
12707 {
12708 	struct uld_info *ui;
12709 
12710 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4async") != 0)
12711 		return;
12712 	sx_slock(&t4_uld_list_lock);
12713 	SLIST_FOREACH(ui, &t4_uld_list, link) {
12714 		if (ui->uld_id == ULD_IWARP) {
12715 			ui->async_event(sc);
12716 			break;
12717 		}
12718 	}
12719 	sx_sunlock(&t4_uld_list_lock);
12720 	end_synchronized_op(sc, 0);
12721 }
12722 
12723 int
uld_active(struct adapter * sc,int uld_id)12724 uld_active(struct adapter *sc, int uld_id)
12725 {
12726 
12727 	MPASS(uld_id >= 0 && uld_id <= ULD_MAX);
12728 
12729 	return (isset(&sc->active_ulds, uld_id));
12730 }
12731 #endif
12732 
12733 #ifdef KERN_TLS
12734 static int
ktls_capability(struct adapter * sc,bool enable)12735 ktls_capability(struct adapter *sc, bool enable)
12736 {
12737 	ASSERT_SYNCHRONIZED_OP(sc);
12738 
12739 	if (!is_ktls(sc))
12740 		return (ENODEV);
12741 	if (!is_t6(sc))
12742 		return (0);
12743 	if (hw_off_limits(sc))
12744 		return (ENXIO);
12745 
12746 	if (enable) {
12747 		if (sc->flags & KERN_TLS_ON)
12748 			return (0);	/* already on */
12749 		if (sc->offload_map != 0) {
12750 			CH_WARN(sc,
12751 			    "Disable TOE on all interfaces associated with "
12752 			    "this adapter before trying to enable NIC TLS.\n");
12753 			return (EAGAIN);
12754 		}
12755 		return (t6_config_kern_tls(sc, true));
12756 	} else {
12757 		/*
12758 		 * Nothing to do for disable.  If TOE is enabled sometime later
12759 		 * then toe_capability will reconfigure the hardware.
12760 		 */
12761 		return (0);
12762 	}
12763 }
12764 #endif
12765 
12766 /*
12767  * t  = ptr to tunable.
12768  * nc = number of CPUs.
12769  * c  = compiled in default for that tunable.
12770  */
12771 static void
calculate_nqueues(int * t,int nc,const int c)12772 calculate_nqueues(int *t, int nc, const int c)
12773 {
12774 	int nq;
12775 
12776 	if (*t > 0)
12777 		return;
12778 	nq = *t < 0 ? -*t : c;
12779 	*t = min(nc, nq);
12780 }
12781 
12782 /*
12783  * Come up with reasonable defaults for some of the tunables, provided they're
12784  * not set by the user (in which case we'll use the values as is).
12785  */
12786 static void
tweak_tunables(void)12787 tweak_tunables(void)
12788 {
12789 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
12790 
12791 	if (t4_ntxq < 1) {
12792 #ifdef RSS
12793 		t4_ntxq = rss_getnumbuckets();
12794 #else
12795 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
12796 #endif
12797 	}
12798 
12799 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
12800 
12801 	if (t4_nrxq < 1) {
12802 #ifdef RSS
12803 		t4_nrxq = rss_getnumbuckets();
12804 #else
12805 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
12806 #endif
12807 	}
12808 
12809 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
12810 
12811 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12812 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
12813 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
12814 #endif
12815 #ifdef TCP_OFFLOAD
12816 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
12817 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
12818 #endif
12819 
12820 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
12821 	if (t4_toecaps_allowed == -1)
12822 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
12823 #else
12824 	if (t4_toecaps_allowed == -1)
12825 		t4_toecaps_allowed = 0;
12826 #endif
12827 
12828 #ifdef TCP_OFFLOAD
12829 	if (t4_rdmacaps_allowed == -1) {
12830 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
12831 		    FW_CAPS_CONFIG_RDMA_RDMAC;
12832 	}
12833 
12834 	if (t4_iscsicaps_allowed == -1) {
12835 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
12836 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
12837 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
12838 	}
12839 
12840 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
12841 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
12842 
12843 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
12844 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
12845 
12846 	if (t4_toe_tls_rx_timeout < 0)
12847 		t4_toe_tls_rx_timeout = 0;
12848 #else
12849 	if (t4_rdmacaps_allowed == -1)
12850 		t4_rdmacaps_allowed = 0;
12851 
12852 	if (t4_iscsicaps_allowed == -1)
12853 		t4_iscsicaps_allowed = 0;
12854 #endif
12855 
12856 #ifdef DEV_NETMAP
12857 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
12858 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
12859 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
12860 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
12861 #endif
12862 
12863 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
12864 		t4_tmr_idx = TMR_IDX;
12865 
12866 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
12867 		t4_pktc_idx = PKTC_IDX;
12868 
12869 	if (t4_qsize_txq < 128)
12870 		t4_qsize_txq = 128;
12871 
12872 	if (t4_qsize_rxq < 128)
12873 		t4_qsize_rxq = 128;
12874 	while (t4_qsize_rxq & 7)
12875 		t4_qsize_rxq++;
12876 
12877 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
12878 
12879 	/*
12880 	 * Number of VIs to create per-port.  The first VI is the "main" regular
12881 	 * VI for the port.  The rest are additional virtual interfaces on the
12882 	 * same physical port.  Note that the main VI does not have native
12883 	 * netmap support but the extra VIs do.
12884 	 *
12885 	 * Limit the number of VIs per port to the number of available
12886 	 * MAC addresses per port.
12887 	 */
12888 	if (t4_num_vis < 1)
12889 		t4_num_vis = 1;
12890 	if (t4_num_vis > nitems(vi_mac_funcs)) {
12891 		t4_num_vis = nitems(vi_mac_funcs);
12892 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
12893 	}
12894 
12895 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
12896 		pcie_relaxed_ordering = 1;
12897 #if defined(__i386__) || defined(__amd64__)
12898 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
12899 			pcie_relaxed_ordering = 0;
12900 #endif
12901 	}
12902 }
12903 
12904 #ifdef DDB
12905 static void
t4_dump_tcb(struct adapter * sc,int tid)12906 t4_dump_tcb(struct adapter *sc, int tid)
12907 {
12908 	uint32_t base, i, j, off, pf, reg, save, tcb_addr, win_pos;
12909 
12910 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
12911 	save = t4_read_reg(sc, reg);
12912 	base = sc->memwin[2].mw_base;
12913 
12914 	/* Dump TCB for the tid */
12915 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
12916 	tcb_addr += tid * TCB_SIZE;
12917 
12918 	if (is_t4(sc)) {
12919 		pf = 0;
12920 		win_pos = tcb_addr & ~0xf;	/* start must be 16B aligned */
12921 	} else {
12922 		pf = V_PFNUM(sc->pf);
12923 		win_pos = tcb_addr & ~0x7f;	/* start must be 128B aligned */
12924 	}
12925 	t4_write_reg(sc, reg, win_pos | pf);
12926 	t4_read_reg(sc, reg);
12927 
12928 	off = tcb_addr - win_pos;
12929 	for (i = 0; i < 4; i++) {
12930 		uint32_t buf[8];
12931 		for (j = 0; j < 8; j++, off += 4)
12932 			buf[j] = htonl(t4_read_reg(sc, base + off));
12933 
12934 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
12935 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12936 		    buf[7]);
12937 	}
12938 
12939 	t4_write_reg(sc, reg, save);
12940 	t4_read_reg(sc, reg);
12941 }
12942 
12943 static void
t4_dump_devlog(struct adapter * sc)12944 t4_dump_devlog(struct adapter *sc)
12945 {
12946 	struct devlog_params *dparams = &sc->params.devlog;
12947 	struct fw_devlog_e e;
12948 	int i, first, j, m, nentries, rc;
12949 	uint64_t ftstamp = UINT64_MAX;
12950 
12951 	if (dparams->start == 0) {
12952 		db_printf("devlog params not valid\n");
12953 		return;
12954 	}
12955 
12956 	nentries = dparams->size / sizeof(struct fw_devlog_e);
12957 	m = fwmtype_to_hwmtype(dparams->memtype);
12958 
12959 	/* Find the first entry. */
12960 	first = -1;
12961 	for (i = 0; i < nentries && !db_pager_quit; i++) {
12962 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12963 		    sizeof(e), (void *)&e);
12964 		if (rc != 0)
12965 			break;
12966 
12967 		if (e.timestamp == 0)
12968 			break;
12969 
12970 		e.timestamp = be64toh(e.timestamp);
12971 		if (e.timestamp < ftstamp) {
12972 			ftstamp = e.timestamp;
12973 			first = i;
12974 		}
12975 	}
12976 
12977 	if (first == -1)
12978 		return;
12979 
12980 	i = first;
12981 	do {
12982 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12983 		    sizeof(e), (void *)&e);
12984 		if (rc != 0)
12985 			return;
12986 
12987 		if (e.timestamp == 0)
12988 			return;
12989 
12990 		e.timestamp = be64toh(e.timestamp);
12991 		e.seqno = be32toh(e.seqno);
12992 		for (j = 0; j < 8; j++)
12993 			e.params[j] = be32toh(e.params[j]);
12994 
12995 		db_printf("%10d  %15ju  %8s  %8s  ",
12996 		    e.seqno, e.timestamp,
12997 		    (e.level < nitems(devlog_level_strings) ?
12998 			devlog_level_strings[e.level] : "UNKNOWN"),
12999 		    (e.facility < nitems(devlog_facility_strings) ?
13000 			devlog_facility_strings[e.facility] : "UNKNOWN"));
13001 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
13002 		    e.params[3], e.params[4], e.params[5], e.params[6],
13003 		    e.params[7]);
13004 
13005 		if (++i == nentries)
13006 			i = 0;
13007 	} while (i != first && !db_pager_quit);
13008 }
13009 
13010 static struct db_command_table db_t4_table = LIST_HEAD_INITIALIZER(db_t4_table);
13011 _DB_SET(_show, t4, NULL, db_show_table, 0, &db_t4_table);
13012 
DB_FUNC(devlog,db_show_devlog,db_t4_table,CS_OWN,NULL)13013 DB_FUNC(devlog, db_show_devlog, db_t4_table, CS_OWN, NULL)
13014 {
13015 	device_t dev;
13016 	int t;
13017 	bool valid;
13018 
13019 	valid = false;
13020 	t = db_read_token();
13021 	if (t == tIDENT) {
13022 		dev = device_lookup_by_name(db_tok_string);
13023 		valid = true;
13024 	}
13025 	db_skip_to_eol();
13026 	if (!valid) {
13027 		db_printf("usage: show t4 devlog <nexus>\n");
13028 		return;
13029 	}
13030 
13031 	if (dev == NULL) {
13032 		db_printf("device not found\n");
13033 		return;
13034 	}
13035 
13036 	t4_dump_devlog(device_get_softc(dev));
13037 }
13038 
DB_FUNC(tcb,db_show_t4tcb,db_t4_table,CS_OWN,NULL)13039 DB_FUNC(tcb, db_show_t4tcb, db_t4_table, CS_OWN, NULL)
13040 {
13041 	device_t dev;
13042 	int radix, tid, t;
13043 	bool valid;
13044 
13045 	valid = false;
13046 	radix = db_radix;
13047 	db_radix = 10;
13048 	t = db_read_token();
13049 	if (t == tIDENT) {
13050 		dev = device_lookup_by_name(db_tok_string);
13051 		t = db_read_token();
13052 		if (t == tNUMBER) {
13053 			tid = db_tok_number;
13054 			valid = true;
13055 		}
13056 	}
13057 	db_radix = radix;
13058 	db_skip_to_eol();
13059 	if (!valid) {
13060 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
13061 		return;
13062 	}
13063 
13064 	if (dev == NULL) {
13065 		db_printf("device not found\n");
13066 		return;
13067 	}
13068 	if (tid < 0) {
13069 		db_printf("invalid tid\n");
13070 		return;
13071 	}
13072 
13073 	t4_dump_tcb(device_get_softc(dev), tid);
13074 }
13075 #endif
13076 
13077 static eventhandler_tag vxlan_start_evtag;
13078 static eventhandler_tag vxlan_stop_evtag;
13079 
13080 struct vxlan_evargs {
13081 	struct ifnet *ifp;
13082 	uint16_t port;
13083 };
13084 
13085 static void
enable_vxlan_rx(struct adapter * sc)13086 enable_vxlan_rx(struct adapter *sc)
13087 {
13088 	int i, rc;
13089 	struct port_info *pi;
13090 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
13091 
13092 	ASSERT_SYNCHRONIZED_OP(sc);
13093 
13094 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
13095 	    F_VXLAN_EN);
13096 	for_each_port(sc, i) {
13097 		pi = sc->port[i];
13098 		if (pi->vxlan_tcam_entry == true)
13099 			continue;
13100 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
13101 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
13102 		    true);
13103 		if (rc < 0) {
13104 			rc = -rc;
13105 			CH_ERR(&pi->vi[0],
13106 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
13107 		} else {
13108 			MPASS(rc == sc->rawf_base + pi->port_id);
13109 			pi->vxlan_tcam_entry = true;
13110 		}
13111 	}
13112 }
13113 
13114 static void
t4_vxlan_start(struct adapter * sc,void * arg)13115 t4_vxlan_start(struct adapter *sc, void *arg)
13116 {
13117 	struct vxlan_evargs *v = arg;
13118 
13119 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13120 		return;
13121 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
13122 		return;
13123 
13124 	if (sc->vxlan_refcount == 0) {
13125 		sc->vxlan_port = v->port;
13126 		sc->vxlan_refcount = 1;
13127 		if (!hw_off_limits(sc))
13128 			enable_vxlan_rx(sc);
13129 	} else if (sc->vxlan_port == v->port) {
13130 		sc->vxlan_refcount++;
13131 	} else {
13132 		CH_ERR(sc, "VXLAN already configured on port  %d; "
13133 		    "ignoring attempt to configure it on port %d\n",
13134 		    sc->vxlan_port, v->port);
13135 	}
13136 	end_synchronized_op(sc, 0);
13137 }
13138 
13139 static void
t4_vxlan_stop(struct adapter * sc,void * arg)13140 t4_vxlan_stop(struct adapter *sc, void *arg)
13141 {
13142 	struct vxlan_evargs *v = arg;
13143 
13144 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13145 		return;
13146 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
13147 		return;
13148 
13149 	/*
13150 	 * VXLANs may have been configured before the driver was loaded so we
13151 	 * may see more stops than starts.  This is not handled cleanly but at
13152 	 * least we keep the refcount sane.
13153 	 */
13154 	if (sc->vxlan_port != v->port)
13155 		goto done;
13156 	if (sc->vxlan_refcount == 0) {
13157 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
13158 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
13159 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
13160 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
13161 done:
13162 	end_synchronized_op(sc, 0);
13163 }
13164 
13165 static void
t4_vxlan_start_handler(void * arg __unused,struct ifnet * ifp,sa_family_t family,u_int port)13166 t4_vxlan_start_handler(void *arg __unused, struct ifnet *ifp,
13167     sa_family_t family, u_int port)
13168 {
13169 	struct vxlan_evargs v;
13170 
13171 	MPASS(family == AF_INET || family == AF_INET6);
13172 	v.ifp = ifp;
13173 	v.port = port;
13174 
13175 	t4_iterate(t4_vxlan_start, &v);
13176 }
13177 
13178 static void
t4_vxlan_stop_handler(void * arg __unused,struct ifnet * ifp,sa_family_t family,u_int port)13179 t4_vxlan_stop_handler(void *arg __unused, struct ifnet *ifp, sa_family_t family,
13180     u_int port)
13181 {
13182 	struct vxlan_evargs v;
13183 
13184 	MPASS(family == AF_INET || family == AF_INET6);
13185 	v.ifp = ifp;
13186 	v.port = port;
13187 
13188 	t4_iterate(t4_vxlan_stop, &v);
13189 }
13190 
13191 
13192 static struct sx mlu;	/* mod load unload */
13193 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
13194 
13195 static int
mod_event(module_t mod,int cmd,void * arg)13196 mod_event(module_t mod, int cmd, void *arg)
13197 {
13198 	int rc = 0;
13199 	static int loaded = 0;
13200 
13201 	switch (cmd) {
13202 	case MOD_LOAD:
13203 		sx_xlock(&mlu);
13204 		if (loaded++ == 0) {
13205 			t4_sge_modload();
13206 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13207 			    t4_filter_rpl, CPL_COOKIE_FILTER);
13208 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
13209 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
13210 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
13211 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
13212 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13213 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
13214 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
13215 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
13216 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
13217 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
13218 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
13219 			    do_smt_write_rpl);
13220 			sx_init(&t4_list_lock, "T4/T5 adapters");
13221 			SLIST_INIT(&t4_list);
13222 			callout_init(&fatal_callout, 1);
13223 #ifdef TCP_OFFLOAD
13224 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
13225 			SLIST_INIT(&t4_uld_list);
13226 #endif
13227 #ifdef INET6
13228 			t4_clip_modload();
13229 #endif
13230 #ifdef KERN_TLS
13231 			t6_ktls_modload();
13232 #endif
13233 			t4_tracer_modload();
13234 			tweak_tunables();
13235 			vxlan_start_evtag =
13236 			    EVENTHANDLER_REGISTER(vxlan_start,
13237 				t4_vxlan_start_handler, NULL,
13238 				EVENTHANDLER_PRI_ANY);
13239 			vxlan_stop_evtag =
13240 			    EVENTHANDLER_REGISTER(vxlan_stop,
13241 				t4_vxlan_stop_handler, NULL,
13242 				EVENTHANDLER_PRI_ANY);
13243 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
13244 			    taskqueue_thread_enqueue, &reset_tq);
13245 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
13246 			    "t4_rst_thr");
13247 		}
13248 		sx_xunlock(&mlu);
13249 		break;
13250 
13251 	case MOD_UNLOAD:
13252 		sx_xlock(&mlu);
13253 		if (--loaded == 0) {
13254 			int tries;
13255 
13256 			taskqueue_free(reset_tq);
13257 			sx_slock(&t4_list_lock);
13258 			if (!SLIST_EMPTY(&t4_list)) {
13259 				rc = EBUSY;
13260 				sx_sunlock(&t4_list_lock);
13261 				goto done_unload;
13262 			}
13263 #ifdef TCP_OFFLOAD
13264 			sx_slock(&t4_uld_list_lock);
13265 			if (!SLIST_EMPTY(&t4_uld_list)) {
13266 				rc = EBUSY;
13267 				sx_sunlock(&t4_uld_list_lock);
13268 				sx_sunlock(&t4_list_lock);
13269 				goto done_unload;
13270 			}
13271 #endif
13272 			tries = 0;
13273 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
13274 				uprintf("%ju clusters with custom free routine "
13275 				    "still is use.\n", t4_sge_extfree_refs());
13276 				pause("t4unload", 2 * hz);
13277 			}
13278 #ifdef TCP_OFFLOAD
13279 			sx_sunlock(&t4_uld_list_lock);
13280 #endif
13281 			sx_sunlock(&t4_list_lock);
13282 
13283 			if (t4_sge_extfree_refs() == 0) {
13284 				EVENTHANDLER_DEREGISTER(vxlan_start,
13285 				    vxlan_start_evtag);
13286 				EVENTHANDLER_DEREGISTER(vxlan_stop,
13287 				    vxlan_stop_evtag);
13288 				t4_tracer_modunload();
13289 #ifdef KERN_TLS
13290 				t6_ktls_modunload();
13291 #endif
13292 #ifdef INET6
13293 				t4_clip_modunload();
13294 #endif
13295 #ifdef TCP_OFFLOAD
13296 				sx_destroy(&t4_uld_list_lock);
13297 #endif
13298 				sx_destroy(&t4_list_lock);
13299 				t4_sge_modunload();
13300 				loaded = 0;
13301 			} else {
13302 				rc = EBUSY;
13303 				loaded++;	/* undo earlier decrement */
13304 			}
13305 		}
13306 done_unload:
13307 		sx_xunlock(&mlu);
13308 		break;
13309 	}
13310 
13311 	return (rc);
13312 }
13313 
13314 static devclass_t t4_devclass, t5_devclass, t6_devclass;
13315 static devclass_t cxgbe_devclass, cxl_devclass, cc_devclass;
13316 static devclass_t vcxgbe_devclass, vcxl_devclass, vcc_devclass;
13317 
13318 DRIVER_MODULE(t4nex, pci, t4_driver, t4_devclass, mod_event, 0);
13319 MODULE_VERSION(t4nex, 1);
13320 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
13321 #ifdef DEV_NETMAP
13322 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
13323 #endif /* DEV_NETMAP */
13324 
13325 DRIVER_MODULE(t5nex, pci, t5_driver, t5_devclass, mod_event, 0);
13326 MODULE_VERSION(t5nex, 1);
13327 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
13328 #ifdef DEV_NETMAP
13329 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
13330 #endif /* DEV_NETMAP */
13331 
13332 DRIVER_MODULE(t6nex, pci, t6_driver, t6_devclass, mod_event, 0);
13333 MODULE_VERSION(t6nex, 1);
13334 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
13335 #ifdef DEV_NETMAP
13336 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
13337 #endif /* DEV_NETMAP */
13338 
13339 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, cxgbe_devclass, 0, 0);
13340 MODULE_VERSION(cxgbe, 1);
13341 
13342 DRIVER_MODULE(cxl, t5nex, cxl_driver, cxl_devclass, 0, 0);
13343 MODULE_VERSION(cxl, 1);
13344 
13345 DRIVER_MODULE(cc, t6nex, cc_driver, cc_devclass, 0, 0);
13346 MODULE_VERSION(cc, 1);
13347 
13348 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, vcxgbe_devclass, 0, 0);
13349 MODULE_VERSION(vcxgbe, 1);
13350 
13351 DRIVER_MODULE(vcxl, cxl, vcxl_driver, vcxl_devclass, 0, 0);
13352 MODULE_VERSION(vcxl, 1);
13353 
13354 DRIVER_MODULE(vcc, cc, vcc_driver, vcc_devclass, 0, 0);
13355 MODULE_VERSION(vcc, 1);
13356