xref: /freebsd-13.1/sys/dev/cxgbe/t4_main.c (revision 8824cbac)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <[email protected]>
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 __FBSDID("$FreeBSD$");
32 
33 #include "opt_ddb.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_kern_tls.h"
37 #include "opt_ratelimit.h"
38 #include "opt_rss.h"
39 
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/priv.h>
43 #include <sys/kernel.h>
44 #include <sys/bus.h>
45 #include <sys/eventhandler.h>
46 #include <sys/module.h>
47 #include <sys/malloc.h>
48 #include <sys/queue.h>
49 #include <sys/taskqueue.h>
50 #include <sys/pciio.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/pci_private.h>
54 #include <sys/firmware.h>
55 #include <sys/sbuf.h>
56 #include <sys/smp.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/sysctl.h>
60 #include <net/ethernet.h>
61 #include <net/if.h>
62 #include <net/if_types.h>
63 #include <net/if_dl.h>
64 #include <net/if_vlan_var.h>
65 #ifdef RSS
66 #include <net/rss_config.h>
67 #endif
68 #include <netinet/in.h>
69 #include <netinet/ip.h>
70 #ifdef KERN_TLS
71 #include <netinet/tcp_seq.h>
72 #endif
73 #if defined(__i386__) || defined(__amd64__)
74 #include <machine/md_var.h>
75 #include <machine/cputypes.h>
76 #include <vm/vm.h>
77 #include <vm/pmap.h>
78 #endif
79 #ifdef DDB
80 #include <ddb/ddb.h>
81 #include <ddb/db_lex.h>
82 #endif
83 
84 #include "common/common.h"
85 #include "common/t4_msg.h"
86 #include "common/t4_regs.h"
87 #include "common/t4_regs_values.h"
88 #include "cudbg/cudbg.h"
89 #include "t4_clip.h"
90 #include "t4_ioctl.h"
91 #include "t4_l2t.h"
92 #include "t4_mp_ring.h"
93 #include "t4_if.h"
94 #include "t4_smt.h"
95 
96 /* T4 bus driver interface */
97 static int t4_probe(device_t);
98 static int t4_attach(device_t);
99 static int t4_detach(device_t);
100 static int t4_child_location_str(device_t, device_t, char *, size_t);
101 static int t4_ready(device_t);
102 static int t4_read_port_device(device_t, int, device_t *);
103 static int t4_suspend(device_t);
104 static int t4_resume(device_t);
105 static int t4_reset_prepare(device_t, device_t);
106 static int t4_reset_post(device_t, device_t);
107 static device_method_t t4_methods[] = {
108 	DEVMETHOD(device_probe,		t4_probe),
109 	DEVMETHOD(device_attach,	t4_attach),
110 	DEVMETHOD(device_detach,	t4_detach),
111 	DEVMETHOD(device_suspend,	t4_suspend),
112 	DEVMETHOD(device_resume,	t4_resume),
113 
114 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
115 	DEVMETHOD(bus_reset_prepare, 	t4_reset_prepare),
116 	DEVMETHOD(bus_reset_post, 	t4_reset_post),
117 
118 	DEVMETHOD(t4_is_main_ready,	t4_ready),
119 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
120 
121 	DEVMETHOD_END
122 };
123 static driver_t t4_driver = {
124 	"t4nex",
125 	t4_methods,
126 	sizeof(struct adapter)
127 };
128 
129 
130 /* T4 port (cxgbe) interface */
131 static int cxgbe_probe(device_t);
132 static int cxgbe_attach(device_t);
133 static int cxgbe_detach(device_t);
134 device_method_t cxgbe_methods[] = {
135 	DEVMETHOD(device_probe,		cxgbe_probe),
136 	DEVMETHOD(device_attach,	cxgbe_attach),
137 	DEVMETHOD(device_detach,	cxgbe_detach),
138 	{ 0, 0 }
139 };
140 static driver_t cxgbe_driver = {
141 	"cxgbe",
142 	cxgbe_methods,
143 	sizeof(struct port_info)
144 };
145 
146 /* T4 VI (vcxgbe) interface */
147 static int vcxgbe_probe(device_t);
148 static int vcxgbe_attach(device_t);
149 static int vcxgbe_detach(device_t);
150 static device_method_t vcxgbe_methods[] = {
151 	DEVMETHOD(device_probe,		vcxgbe_probe),
152 	DEVMETHOD(device_attach,	vcxgbe_attach),
153 	DEVMETHOD(device_detach,	vcxgbe_detach),
154 	{ 0, 0 }
155 };
156 static driver_t vcxgbe_driver = {
157 	"vcxgbe",
158 	vcxgbe_methods,
159 	sizeof(struct vi_info)
160 };
161 
162 static d_ioctl_t t4_ioctl;
163 
164 static struct cdevsw t4_cdevsw = {
165        .d_version = D_VERSION,
166        .d_ioctl = t4_ioctl,
167        .d_name = "t4nex",
168 };
169 
170 /* T5 bus driver interface */
171 static int t5_probe(device_t);
172 static device_method_t t5_methods[] = {
173 	DEVMETHOD(device_probe,		t5_probe),
174 	DEVMETHOD(device_attach,	t4_attach),
175 	DEVMETHOD(device_detach,	t4_detach),
176 	DEVMETHOD(device_suspend,	t4_suspend),
177 	DEVMETHOD(device_resume,	t4_resume),
178 
179 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
180 	DEVMETHOD(bus_reset_prepare, 	t4_reset_prepare),
181 	DEVMETHOD(bus_reset_post, 	t4_reset_post),
182 
183 	DEVMETHOD(t4_is_main_ready,	t4_ready),
184 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
185 
186 	DEVMETHOD_END
187 };
188 static driver_t t5_driver = {
189 	"t5nex",
190 	t5_methods,
191 	sizeof(struct adapter)
192 };
193 
194 
195 /* T5 port (cxl) interface */
196 static driver_t cxl_driver = {
197 	"cxl",
198 	cxgbe_methods,
199 	sizeof(struct port_info)
200 };
201 
202 /* T5 VI (vcxl) interface */
203 static driver_t vcxl_driver = {
204 	"vcxl",
205 	vcxgbe_methods,
206 	sizeof(struct vi_info)
207 };
208 
209 /* T6 bus driver interface */
210 static int t6_probe(device_t);
211 static device_method_t t6_methods[] = {
212 	DEVMETHOD(device_probe,		t6_probe),
213 	DEVMETHOD(device_attach,	t4_attach),
214 	DEVMETHOD(device_detach,	t4_detach),
215 	DEVMETHOD(device_suspend,	t4_suspend),
216 	DEVMETHOD(device_resume,	t4_resume),
217 
218 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
219 	DEVMETHOD(bus_reset_prepare, 	t4_reset_prepare),
220 	DEVMETHOD(bus_reset_post, 	t4_reset_post),
221 
222 	DEVMETHOD(t4_is_main_ready,	t4_ready),
223 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
224 
225 	DEVMETHOD_END
226 };
227 static driver_t t6_driver = {
228 	"t6nex",
229 	t6_methods,
230 	sizeof(struct adapter)
231 };
232 
233 
234 /* T6 port (cc) interface */
235 static driver_t cc_driver = {
236 	"cc",
237 	cxgbe_methods,
238 	sizeof(struct port_info)
239 };
240 
241 /* T6 VI (vcc) interface */
242 static driver_t vcc_driver = {
243 	"vcc",
244 	vcxgbe_methods,
245 	sizeof(struct vi_info)
246 };
247 
248 /* ifnet interface */
249 static void cxgbe_init(void *);
250 static int cxgbe_ioctl(struct ifnet *, unsigned long, caddr_t);
251 static int cxgbe_transmit(struct ifnet *, struct mbuf *);
252 static void cxgbe_qflush(struct ifnet *);
253 #if defined(KERN_TLS) || defined(RATELIMIT)
254 static int cxgbe_snd_tag_alloc(struct ifnet *, union if_snd_tag_alloc_params *,
255     struct m_snd_tag **);
256 static int cxgbe_snd_tag_modify(struct m_snd_tag *,
257     union if_snd_tag_modify_params *);
258 static int cxgbe_snd_tag_query(struct m_snd_tag *,
259     union if_snd_tag_query_params *);
260 static void cxgbe_snd_tag_free(struct m_snd_tag *);
261 #endif
262 
263 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
264 
265 /*
266  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
267  * then ADAPTER_LOCK, then t4_uld_list_lock.
268  */
269 static struct sx t4_list_lock;
270 SLIST_HEAD(, adapter) t4_list;
271 #ifdef TCP_OFFLOAD
272 static struct sx t4_uld_list_lock;
273 SLIST_HEAD(, uld_info) t4_uld_list;
274 #endif
275 
276 /*
277  * Tunables.  See tweak_tunables() too.
278  *
279  * Each tunable is set to a default value here if it's known at compile-time.
280  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
281  * provide a reasonable default (upto n) when the driver is loaded.
282  *
283  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
284  * T5 are under hw.cxl.
285  */
286 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
287     "cxgbe(4) parameters");
288 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
289     "cxgbe(4) T5+ parameters");
290 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
291     "cxgbe(4) TOE parameters");
292 
293 /*
294  * Number of queues for tx and rx, NIC and offload.
295  */
296 #define NTXQ 16
297 int t4_ntxq = -NTXQ;
298 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
299     "Number of TX queues per port");
300 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
301 
302 #define NRXQ 8
303 int t4_nrxq = -NRXQ;
304 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
305     "Number of RX queues per port");
306 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
307 
308 #define NTXQ_VI 1
309 static int t4_ntxq_vi = -NTXQ_VI;
310 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
311     "Number of TX queues per VI");
312 
313 #define NRXQ_VI 1
314 static int t4_nrxq_vi = -NRXQ_VI;
315 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
316     "Number of RX queues per VI");
317 
318 static int t4_rsrv_noflowq = 0;
319 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
320     0, "Reserve TX queue 0 of each VI for non-flowid packets");
321 
322 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
323 #define NOFLDTXQ 8
324 static int t4_nofldtxq = -NOFLDTXQ;
325 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
326     "Number of offload TX queues per port");
327 
328 #define NOFLDRXQ 2
329 static int t4_nofldrxq = -NOFLDRXQ;
330 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
331     "Number of offload RX queues per port");
332 
333 #define NOFLDTXQ_VI 1
334 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
335 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
336     "Number of offload TX queues per VI");
337 
338 #define NOFLDRXQ_VI 1
339 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
340 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
341     "Number of offload RX queues per VI");
342 
343 #define TMR_IDX_OFLD 1
344 int t4_tmr_idx_ofld = TMR_IDX_OFLD;
345 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
346     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
347 
348 #define PKTC_IDX_OFLD (-1)
349 int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
350 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
351     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
352 
353 /* 0 means chip/fw default, non-zero number is value in microseconds */
354 static u_long t4_toe_keepalive_idle = 0;
355 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
356     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
357 
358 /* 0 means chip/fw default, non-zero number is value in microseconds */
359 static u_long t4_toe_keepalive_interval = 0;
360 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
361     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
362 
363 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
364 static int t4_toe_keepalive_count = 0;
365 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
366     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
367 
368 /* 0 means chip/fw default, non-zero number is value in microseconds */
369 static u_long t4_toe_rexmt_min = 0;
370 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
371     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
372 
373 /* 0 means chip/fw default, non-zero number is value in microseconds */
374 static u_long t4_toe_rexmt_max = 0;
375 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
376     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
377 
378 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
379 static int t4_toe_rexmt_count = 0;
380 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
381     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
382 
383 /* -1 means chip/fw default, other values are raw backoff values to use */
384 static int t4_toe_rexmt_backoff[16] = {
385 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
386 };
387 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
388     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
389     "cxgbe(4) TOE retransmit backoff values");
390 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
391     &t4_toe_rexmt_backoff[0], 0, "");
392 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
393     &t4_toe_rexmt_backoff[1], 0, "");
394 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
395     &t4_toe_rexmt_backoff[2], 0, "");
396 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
397     &t4_toe_rexmt_backoff[3], 0, "");
398 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
399     &t4_toe_rexmt_backoff[4], 0, "");
400 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
401     &t4_toe_rexmt_backoff[5], 0, "");
402 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
403     &t4_toe_rexmt_backoff[6], 0, "");
404 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
405     &t4_toe_rexmt_backoff[7], 0, "");
406 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
407     &t4_toe_rexmt_backoff[8], 0, "");
408 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
409     &t4_toe_rexmt_backoff[9], 0, "");
410 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
411     &t4_toe_rexmt_backoff[10], 0, "");
412 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
413     &t4_toe_rexmt_backoff[11], 0, "");
414 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
415     &t4_toe_rexmt_backoff[12], 0, "");
416 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
417     &t4_toe_rexmt_backoff[13], 0, "");
418 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
419     &t4_toe_rexmt_backoff[14], 0, "");
420 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
421     &t4_toe_rexmt_backoff[15], 0, "");
422 
423 static int t4_toe_tls_rx_timeout = 5;
424 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, tls_rx_timeout, CTLFLAG_RDTUN,
425     &t4_toe_tls_rx_timeout, 0,
426     "Timeout in seconds to downgrade TLS sockets to plain TOE");
427 #endif
428 
429 #ifdef DEV_NETMAP
430 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
431 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
432 static int t4_native_netmap = NN_EXTRA_VI;
433 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
434     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
435 
436 #define NNMTXQ 8
437 static int t4_nnmtxq = -NNMTXQ;
438 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
439     "Number of netmap TX queues");
440 
441 #define NNMRXQ 8
442 static int t4_nnmrxq = -NNMRXQ;
443 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
444     "Number of netmap RX queues");
445 
446 #define NNMTXQ_VI 2
447 static int t4_nnmtxq_vi = -NNMTXQ_VI;
448 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
449     "Number of netmap TX queues per VI");
450 
451 #define NNMRXQ_VI 2
452 static int t4_nnmrxq_vi = -NNMRXQ_VI;
453 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
454     "Number of netmap RX queues per VI");
455 #endif
456 
457 /*
458  * Holdoff parameters for ports.
459  */
460 #define TMR_IDX 1
461 int t4_tmr_idx = TMR_IDX;
462 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
463     0, "Holdoff timer index");
464 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
465 
466 #define PKTC_IDX (-1)
467 int t4_pktc_idx = PKTC_IDX;
468 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
469     0, "Holdoff packet counter index");
470 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
471 
472 /*
473  * Size (# of entries) of each tx and rx queue.
474  */
475 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
476 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
477     "Number of descriptors in each TX queue");
478 
479 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
480 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
481     "Number of descriptors in each RX queue");
482 
483 /*
484  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
485  */
486 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
487 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
488     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
489 
490 /*
491  * Configuration file.  All the _CF names here are special.
492  */
493 #define DEFAULT_CF	"default"
494 #define BUILTIN_CF	"built-in"
495 #define FLASH_CF	"flash"
496 #define UWIRE_CF	"uwire"
497 #define FPGA_CF		"fpga"
498 static char t4_cfg_file[32] = DEFAULT_CF;
499 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
500     sizeof(t4_cfg_file), "Firmware configuration file");
501 
502 /*
503  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
504  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
505  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
506  *            mark or when signalled to do so, 0 to never emit PAUSE.
507  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
508  *                 negotiated settings will override rx_pause/tx_pause.
509  *                 Otherwise rx_pause/tx_pause are applied forcibly.
510  */
511 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
512 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
513     &t4_pause_settings, 0,
514     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
515 
516 /*
517  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
518  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
519  *  0 to disable FEC.
520  */
521 static int t4_fec = -1;
522 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
523     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
524 
525 /*
526  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
527  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
528  * driver runs as if this is set to 0.
529  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
530  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
531  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
532  *    the firmware anyway (may result in l1cfg errors with old firmwares).
533  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
534  *    means set all FEC bits that are valid for the speed.
535  */
536 static int t4_force_fec = -1;
537 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
538     "Controls the use of FORCE_FEC bit in L1 configuration.");
539 
540 /*
541  * Link autonegotiation.
542  * -1 to run with the firmware default.
543  *  0 to disable.
544  *  1 to enable.
545  */
546 static int t4_autoneg = -1;
547 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
548     "Link autonegotiation");
549 
550 /*
551  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
552  * encouraged respectively).  '-n' is the same as 'n' except the firmware
553  * version used in the checks is read from the firmware bundled with the driver.
554  */
555 static int t4_fw_install = 1;
556 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
557     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
558 
559 /*
560  * ASIC features that will be used.  Disable the ones you don't want so that the
561  * chip resources aren't wasted on features that will not be used.
562  */
563 static int t4_nbmcaps_allowed = 0;
564 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
565     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
566 
567 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
568 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
569     &t4_linkcaps_allowed, 0, "Default link capabilities");
570 
571 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
572     FW_CAPS_CONFIG_SWITCH_EGRESS;
573 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
574     &t4_switchcaps_allowed, 0, "Default switch capabilities");
575 
576 #ifdef RATELIMIT
577 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
578 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
579 #else
580 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
581 	FW_CAPS_CONFIG_NIC_HASHFILTER;
582 #endif
583 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
584     &t4_niccaps_allowed, 0, "Default NIC capabilities");
585 
586 static int t4_toecaps_allowed = -1;
587 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
588     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
589 
590 static int t4_rdmacaps_allowed = -1;
591 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
592     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
593 
594 static int t4_cryptocaps_allowed = -1;
595 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
596     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
597 
598 static int t4_iscsicaps_allowed = -1;
599 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
600     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
601 
602 static int t4_fcoecaps_allowed = 0;
603 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
604     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
605 
606 static int t5_write_combine = 0;
607 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
608     0, "Use WC instead of UC for BAR2");
609 
610 static int t4_num_vis = 1;
611 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
612     "Number of VIs per port");
613 
614 /*
615  * PCIe Relaxed Ordering.
616  * -1: driver should figure out a good value.
617  * 0: disable RO.
618  * 1: enable RO.
619  * 2: leave RO alone.
620  */
621 static int pcie_relaxed_ordering = -1;
622 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
623     &pcie_relaxed_ordering, 0,
624     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
625 
626 static int t4_panic_on_fatal_err = 0;
627 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
628     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
629 
630 static int t4_reset_on_fatal_err = 0;
631 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
632     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
633 
634 static int t4_tx_vm_wr = 0;
635 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
636     "Use VM work requests to transmit packets.");
637 
638 /*
639  * Set to non-zero to enable the attack filter.  A packet that matches any of
640  * these conditions will get dropped on ingress:
641  * 1) IP && source address == destination address.
642  * 2) TCP/IP && source address is not a unicast address.
643  * 3) TCP/IP && destination address is not a unicast address.
644  * 4) IP && source address is loopback (127.x.y.z).
645  * 5) IP && destination address is loopback (127.x.y.z).
646  * 6) IPv6 && source address == destination address.
647  * 7) IPv6 && source address is not a unicast address.
648  * 8) IPv6 && source address is loopback (::1/128).
649  * 9) IPv6 && destination address is loopback (::1/128).
650  * 10) IPv6 && source address is unspecified (::/128).
651  * 11) IPv6 && destination address is unspecified (::/128).
652  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
653  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
654  */
655 static int t4_attack_filter = 0;
656 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
657     &t4_attack_filter, 0, "Drop suspicious traffic");
658 
659 static int t4_drop_ip_fragments = 0;
660 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
661     &t4_drop_ip_fragments, 0, "Drop IP fragments");
662 
663 static int t4_drop_pkts_with_l2_errors = 1;
664 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
665     &t4_drop_pkts_with_l2_errors, 0,
666     "Drop all frames with Layer 2 length or checksum errors");
667 
668 static int t4_drop_pkts_with_l3_errors = 0;
669 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
670     &t4_drop_pkts_with_l3_errors, 0,
671     "Drop all frames with IP version, length, or checksum errors");
672 
673 static int t4_drop_pkts_with_l4_errors = 0;
674 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
675     &t4_drop_pkts_with_l4_errors, 0,
676     "Drop all frames with Layer 4 length, checksum, or other errors");
677 
678 #ifdef TCP_OFFLOAD
679 /*
680  * TOE tunables.
681  */
682 static int t4_cop_managed_offloading = 0;
683 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
684     &t4_cop_managed_offloading, 0,
685     "COP (Connection Offload Policy) controls all TOE offload");
686 #endif
687 
688 #ifdef KERN_TLS
689 /*
690  * This enables KERN_TLS for all adapters if set.
691  */
692 static int t4_kern_tls = 0;
693 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
694     "Enable KERN_TLS mode for all supported adapters");
695 
696 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
697     "cxgbe(4) KERN_TLS parameters");
698 
699 static int t4_tls_inline_keys = 0;
700 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
701     &t4_tls_inline_keys, 0,
702     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
703     "in card memory.");
704 
705 static int t4_tls_combo_wrs = 0;
706 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
707     0, "Attempt to combine TCB field updates with TLS record work requests.");
708 #endif
709 
710 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
711 static int vi_mac_funcs[] = {
712 	FW_VI_FUNC_ETH,
713 	FW_VI_FUNC_OFLD,
714 	FW_VI_FUNC_IWARP,
715 	FW_VI_FUNC_OPENISCSI,
716 	FW_VI_FUNC_OPENFCOE,
717 	FW_VI_FUNC_FOISCSI,
718 	FW_VI_FUNC_FOFCOE,
719 };
720 
721 struct intrs_and_queues {
722 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
723 	uint16_t num_vis;	/* number of VIs for each port */
724 	uint16_t nirq;		/* Total # of vectors */
725 	uint16_t ntxq;		/* # of NIC txq's for each port */
726 	uint16_t nrxq;		/* # of NIC rxq's for each port */
727 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
728 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
729 	uint16_t nnmtxq;	/* # of netmap txq's */
730 	uint16_t nnmrxq;	/* # of netmap rxq's */
731 
732 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
733 	uint16_t ntxq_vi;	/* # of NIC txq's */
734 	uint16_t nrxq_vi;	/* # of NIC rxq's */
735 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
736 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
737 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
738 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
739 };
740 
741 static void setup_memwin(struct adapter *);
742 static void position_memwin(struct adapter *, int, uint32_t);
743 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
744 static int fwmtype_to_hwmtype(int);
745 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
746     uint32_t *);
747 static int fixup_devlog_params(struct adapter *);
748 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
749 static int contact_firmware(struct adapter *);
750 static int partition_resources(struct adapter *);
751 static int get_params__pre_init(struct adapter *);
752 static int set_params__pre_init(struct adapter *);
753 static int get_params__post_init(struct adapter *);
754 static int set_params__post_init(struct adapter *);
755 static void t4_set_desc(struct adapter *);
756 static bool fixed_ifmedia(struct port_info *);
757 static void build_medialist(struct port_info *);
758 static void init_link_config(struct port_info *);
759 static int fixup_link_config(struct port_info *);
760 static int apply_link_config(struct port_info *);
761 static int cxgbe_init_synchronized(struct vi_info *);
762 static int cxgbe_uninit_synchronized(struct vi_info *);
763 static int adapter_full_init(struct adapter *);
764 static void adapter_full_uninit(struct adapter *);
765 static int vi_full_init(struct vi_info *);
766 static void vi_full_uninit(struct vi_info *);
767 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
768 static void quiesce_txq(struct sge_txq *);
769 static void quiesce_wrq(struct sge_wrq *);
770 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
771 static void quiesce_vi(struct vi_info *);
772 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
773     driver_intr_t *, void *, char *);
774 static int t4_free_irq(struct adapter *, struct irq *);
775 static void t4_init_atid_table(struct adapter *);
776 static void t4_free_atid_table(struct adapter *);
777 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
778 static void vi_refresh_stats(struct vi_info *);
779 static void cxgbe_refresh_stats(struct vi_info *);
780 static void cxgbe_tick(void *);
781 static void vi_tick(void *);
782 static void cxgbe_sysctls(struct port_info *);
783 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
784 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
785 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
786 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
787 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
788 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
789 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
790 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
791 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
792 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
793 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
794 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
795 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
796 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
797 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
798 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
799 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
800 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
801 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
802 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
803 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
804 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
805 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
806 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
807 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
808 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
809 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
810 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
811 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
812 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
813 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
814 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
815 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
816 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
817 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
818 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
819 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
820 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
821 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
822 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
823 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
824 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
825 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
826 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
827 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
828 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
829 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
830 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
831 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
832 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
833 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
834 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
835 #ifdef TCP_OFFLOAD
836 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
837 static int sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS);
838 static int sysctl_tls_rx_timeout(SYSCTL_HANDLER_ARGS);
839 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
840 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
841 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
842 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
843 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
844 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
845 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
846 #endif
847 static int get_sge_context(struct adapter *, struct t4_sge_context *);
848 static int load_fw(struct adapter *, struct t4_data *);
849 static int load_cfg(struct adapter *, struct t4_data *);
850 static int load_boot(struct adapter *, struct t4_bootrom *);
851 static int load_bootcfg(struct adapter *, struct t4_data *);
852 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
853 static void free_offload_policy(struct t4_offload_policy *);
854 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
855 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
856 static int read_i2c(struct adapter *, struct t4_i2c_data *);
857 static int clear_stats(struct adapter *, u_int);
858 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
859 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
860 #ifdef TCP_OFFLOAD
861 static int toe_capability(struct vi_info *, bool);
862 static void t4_async_event(struct adapter *);
863 #endif
864 #ifdef KERN_TLS
865 static int ktls_capability(struct adapter *, bool);
866 #endif
867 static int mod_event(module_t, int, void *);
868 static int notify_siblings(device_t, int);
869 static uint64_t vi_get_counter(struct ifnet *, ift_counter);
870 static uint64_t cxgbe_get_counter(struct ifnet *, ift_counter);
871 static void enable_vxlan_rx(struct adapter *);
872 static void reset_adapter_task(void *, int);
873 static void fatal_error_task(void *, int);
874 static void dump_devlog(struct adapter *);
875 static void dump_cim_regs(struct adapter *);
876 static void dump_cimla(struct adapter *);
877 
878 struct {
879 	uint16_t device;
880 	char *desc;
881 } t4_pciids[] = {
882 	{0xa000, "Chelsio Terminator 4 FPGA"},
883 	{0x4400, "Chelsio T440-dbg"},
884 	{0x4401, "Chelsio T420-CR"},
885 	{0x4402, "Chelsio T422-CR"},
886 	{0x4403, "Chelsio T440-CR"},
887 	{0x4404, "Chelsio T420-BCH"},
888 	{0x4405, "Chelsio T440-BCH"},
889 	{0x4406, "Chelsio T440-CH"},
890 	{0x4407, "Chelsio T420-SO"},
891 	{0x4408, "Chelsio T420-CX"},
892 	{0x4409, "Chelsio T420-BT"},
893 	{0x440a, "Chelsio T404-BT"},
894 	{0x440e, "Chelsio T440-LP-CR"},
895 }, t5_pciids[] = {
896 	{0xb000, "Chelsio Terminator 5 FPGA"},
897 	{0x5400, "Chelsio T580-dbg"},
898 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
899 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
900 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
901 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
902 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
903 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
904 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
905 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
906 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
907 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
908 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
909 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
910 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
911 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
912 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
913 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
914 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
915 
916 	/* Custom */
917 	{0x5483, "Custom T540-CR"},
918 	{0x5484, "Custom T540-BT"},
919 }, t6_pciids[] = {
920 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
921 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
922 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
923 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
924 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
925 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
926 	{0x6405, "Chelsio T6225-OCP-SO"},	/* 2 x 10/25G, nomem */
927 	{0x6406, "Chelsio T62100-OCP-SO"},	/* 2 x 40/50/100G, nomem */
928 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
929 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
930 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
931 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
932 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
933 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
934 	{0x6414, "Chelsio T61100-OCP-SO"},	/* 1 x 40/50/100G, nomem */
935 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
936 
937 	/* Custom */
938 	{0x6480, "Custom T6225-CR"},
939 	{0x6481, "Custom T62100-CR"},
940 	{0x6482, "Custom T6225-CR"},
941 	{0x6483, "Custom T62100-CR"},
942 	{0x6484, "Custom T64100-CR"},
943 	{0x6485, "Custom T6240-SO"},
944 	{0x6486, "Custom T6225-SO-CR"},
945 	{0x6487, "Custom T6225-CR"},
946 };
947 
948 #ifdef TCP_OFFLOAD
949 /*
950  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
951  * be exactly the same for both rxq and ofld_rxq.
952  */
953 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
954 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
955 #endif
956 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
957 
958 static int
t4_probe(device_t dev)959 t4_probe(device_t dev)
960 {
961 	int i;
962 	uint16_t v = pci_get_vendor(dev);
963 	uint16_t d = pci_get_device(dev);
964 	uint8_t f = pci_get_function(dev);
965 
966 	if (v != PCI_VENDOR_ID_CHELSIO)
967 		return (ENXIO);
968 
969 	/* Attach only to PF0 of the FPGA */
970 	if (d == 0xa000 && f != 0)
971 		return (ENXIO);
972 
973 	for (i = 0; i < nitems(t4_pciids); i++) {
974 		if (d == t4_pciids[i].device) {
975 			device_set_desc(dev, t4_pciids[i].desc);
976 			return (BUS_PROBE_DEFAULT);
977 		}
978 	}
979 
980 	return (ENXIO);
981 }
982 
983 static int
t5_probe(device_t dev)984 t5_probe(device_t dev)
985 {
986 	int i;
987 	uint16_t v = pci_get_vendor(dev);
988 	uint16_t d = pci_get_device(dev);
989 	uint8_t f = pci_get_function(dev);
990 
991 	if (v != PCI_VENDOR_ID_CHELSIO)
992 		return (ENXIO);
993 
994 	/* Attach only to PF0 of the FPGA */
995 	if (d == 0xb000 && f != 0)
996 		return (ENXIO);
997 
998 	for (i = 0; i < nitems(t5_pciids); i++) {
999 		if (d == t5_pciids[i].device) {
1000 			device_set_desc(dev, t5_pciids[i].desc);
1001 			return (BUS_PROBE_DEFAULT);
1002 		}
1003 	}
1004 
1005 	return (ENXIO);
1006 }
1007 
1008 static int
t6_probe(device_t dev)1009 t6_probe(device_t dev)
1010 {
1011 	int i;
1012 	uint16_t v = pci_get_vendor(dev);
1013 	uint16_t d = pci_get_device(dev);
1014 
1015 	if (v != PCI_VENDOR_ID_CHELSIO)
1016 		return (ENXIO);
1017 
1018 	for (i = 0; i < nitems(t6_pciids); i++) {
1019 		if (d == t6_pciids[i].device) {
1020 			device_set_desc(dev, t6_pciids[i].desc);
1021 			return (BUS_PROBE_DEFAULT);
1022 		}
1023 	}
1024 
1025 	return (ENXIO);
1026 }
1027 
1028 static void
t5_attribute_workaround(device_t dev)1029 t5_attribute_workaround(device_t dev)
1030 {
1031 	device_t root_port;
1032 	uint32_t v;
1033 
1034 	/*
1035 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1036 	 * Ordering attributes when replying to a TLP from a Root
1037 	 * Port.  As a workaround, find the parent Root Port and
1038 	 * disable No Snoop and Relaxed Ordering.  Note that this
1039 	 * affects all devices under this root port.
1040 	 */
1041 	root_port = pci_find_pcie_root_port(dev);
1042 	if (root_port == NULL) {
1043 		device_printf(dev, "Unable to find parent root port\n");
1044 		return;
1045 	}
1046 
1047 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1048 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1049 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1050 	    0)
1051 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1052 		    device_get_nameunit(root_port));
1053 }
1054 
1055 static const struct devnames devnames[] = {
1056 	{
1057 		.nexus_name = "t4nex",
1058 		.ifnet_name = "cxgbe",
1059 		.vi_ifnet_name = "vcxgbe",
1060 		.pf03_drv_name = "t4iov",
1061 		.vf_nexus_name = "t4vf",
1062 		.vf_ifnet_name = "cxgbev"
1063 	}, {
1064 		.nexus_name = "t5nex",
1065 		.ifnet_name = "cxl",
1066 		.vi_ifnet_name = "vcxl",
1067 		.pf03_drv_name = "t5iov",
1068 		.vf_nexus_name = "t5vf",
1069 		.vf_ifnet_name = "cxlv"
1070 	}, {
1071 		.nexus_name = "t6nex",
1072 		.ifnet_name = "cc",
1073 		.vi_ifnet_name = "vcc",
1074 		.pf03_drv_name = "t6iov",
1075 		.vf_nexus_name = "t6vf",
1076 		.vf_ifnet_name = "ccv"
1077 	}
1078 };
1079 
1080 void
t4_init_devnames(struct adapter * sc)1081 t4_init_devnames(struct adapter *sc)
1082 {
1083 	int id;
1084 
1085 	id = chip_id(sc);
1086 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
1087 		sc->names = &devnames[id - CHELSIO_T4];
1088 	else {
1089 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1090 		sc->names = NULL;
1091 	}
1092 }
1093 
1094 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1095 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1096 {
1097 	const char *parent, *name;
1098 	long value;
1099 	int line, unit;
1100 
1101 	line = 0;
1102 	parent = device_get_nameunit(sc->dev);
1103 	name = sc->names->ifnet_name;
1104 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1105 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1106 		    value == pi->port_id)
1107 			return (unit);
1108 	}
1109 	return (-1);
1110 }
1111 
1112 static int
t4_attach(device_t dev)1113 t4_attach(device_t dev)
1114 {
1115 	struct adapter *sc;
1116 	int rc = 0, i, j, rqidx, tqidx, nports;
1117 	struct make_dev_args mda;
1118 	struct intrs_and_queues iaq;
1119 	struct sge *s;
1120 	uint32_t *buf;
1121 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1122 	int ofld_tqidx;
1123 #endif
1124 #ifdef TCP_OFFLOAD
1125 	int ofld_rqidx;
1126 #endif
1127 #ifdef DEV_NETMAP
1128 	int nm_rqidx, nm_tqidx;
1129 #endif
1130 	int num_vis;
1131 
1132 	sc = device_get_softc(dev);
1133 	sc->dev = dev;
1134 	sysctl_ctx_init(&sc->ctx);
1135 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1136 
1137 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1138 		t5_attribute_workaround(dev);
1139 	pci_enable_busmaster(dev);
1140 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1141 		uint32_t v;
1142 
1143 		pci_set_max_read_req(dev, 4096);
1144 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1145 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1146 		if (pcie_relaxed_ordering == 0 &&
1147 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1148 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1149 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1150 		} else if (pcie_relaxed_ordering == 1 &&
1151 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1152 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1153 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1154 		}
1155 	}
1156 
1157 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1158 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1159 	sc->traceq = -1;
1160 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1161 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1162 	    device_get_nameunit(dev));
1163 
1164 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1165 	    device_get_nameunit(dev));
1166 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1167 	t4_add_adapter(sc);
1168 
1169 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1170 	TAILQ_INIT(&sc->sfl);
1171 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1172 
1173 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1174 
1175 	sc->policy = NULL;
1176 	rw_init(&sc->policy_lock, "connection offload policy");
1177 
1178 	callout_init(&sc->ktls_tick, 1);
1179 
1180 	refcount_init(&sc->vxlan_refcount, 0);
1181 
1182 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1183 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1184 
1185 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1186 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1187 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1188 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1189 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1190 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1191 
1192 	rc = t4_map_bars_0_and_4(sc);
1193 	if (rc != 0)
1194 		goto done; /* error message displayed already */
1195 
1196 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1197 
1198 	/* Prepare the adapter for operation. */
1199 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1200 	rc = -t4_prep_adapter(sc, buf);
1201 	free(buf, M_CXGBE);
1202 	if (rc != 0) {
1203 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1204 		goto done;
1205 	}
1206 
1207 	/*
1208 	 * This is the real PF# to which we're attaching.  Works from within PCI
1209 	 * passthrough environments too, where pci_get_function() could return a
1210 	 * different PF# depending on the passthrough configuration.  We need to
1211 	 * use the real PF# in all our communication with the firmware.
1212 	 */
1213 	j = t4_read_reg(sc, A_PL_WHOAMI);
1214 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1215 	sc->mbox = sc->pf;
1216 
1217 	t4_init_devnames(sc);
1218 	if (sc->names == NULL) {
1219 		rc = ENOTSUP;
1220 		goto done; /* error message displayed already */
1221 	}
1222 
1223 	/*
1224 	 * Do this really early, with the memory windows set up even before the
1225 	 * character device.  The userland tool's register i/o and mem read
1226 	 * will work even in "recovery mode".
1227 	 */
1228 	setup_memwin(sc);
1229 	if (t4_init_devlog_params(sc, 0) == 0)
1230 		fixup_devlog_params(sc);
1231 	make_dev_args_init(&mda);
1232 	mda.mda_devsw = &t4_cdevsw;
1233 	mda.mda_uid = UID_ROOT;
1234 	mda.mda_gid = GID_WHEEL;
1235 	mda.mda_mode = 0600;
1236 	mda.mda_si_drv1 = sc;
1237 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1238 	if (rc != 0)
1239 		device_printf(dev, "failed to create nexus char device: %d.\n",
1240 		    rc);
1241 
1242 	/* Go no further if recovery mode has been requested. */
1243 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1244 		device_printf(dev, "recovery mode.\n");
1245 		goto done;
1246 	}
1247 
1248 #if defined(__i386__)
1249 	if ((cpu_feature & CPUID_CX8) == 0) {
1250 		device_printf(dev, "64 bit atomics not available.\n");
1251 		rc = ENOTSUP;
1252 		goto done;
1253 	}
1254 #endif
1255 
1256 	/* Contact the firmware and try to become the master driver. */
1257 	rc = contact_firmware(sc);
1258 	if (rc != 0)
1259 		goto done; /* error message displayed already */
1260 	MPASS(sc->flags & FW_OK);
1261 
1262 	rc = get_params__pre_init(sc);
1263 	if (rc != 0)
1264 		goto done; /* error message displayed already */
1265 
1266 	if (sc->flags & MASTER_PF) {
1267 		rc = partition_resources(sc);
1268 		if (rc != 0)
1269 			goto done; /* error message displayed already */
1270 		t4_intr_clear(sc);
1271 	}
1272 
1273 	rc = get_params__post_init(sc);
1274 	if (rc != 0)
1275 		goto done; /* error message displayed already */
1276 
1277 	rc = set_params__post_init(sc);
1278 	if (rc != 0)
1279 		goto done; /* error message displayed already */
1280 
1281 	rc = t4_map_bar_2(sc);
1282 	if (rc != 0)
1283 		goto done; /* error message displayed already */
1284 
1285 	rc = t4_create_dma_tag(sc);
1286 	if (rc != 0)
1287 		goto done; /* error message displayed already */
1288 
1289 	/*
1290 	 * First pass over all the ports - allocate VIs and initialize some
1291 	 * basic parameters like mac address, port type, etc.
1292 	 */
1293 	for_each_port(sc, i) {
1294 		struct port_info *pi;
1295 
1296 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1297 		sc->port[i] = pi;
1298 
1299 		/* These must be set before t4_port_init */
1300 		pi->adapter = sc;
1301 		pi->port_id = i;
1302 		/*
1303 		 * XXX: vi[0] is special so we can't delay this allocation until
1304 		 * pi->nvi's final value is known.
1305 		 */
1306 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1307 		    M_ZERO | M_WAITOK);
1308 
1309 		/*
1310 		 * Allocate the "main" VI and initialize parameters
1311 		 * like mac addr.
1312 		 */
1313 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1314 		if (rc != 0) {
1315 			device_printf(dev, "unable to initialize port %d: %d\n",
1316 			    i, rc);
1317 			free(pi->vi, M_CXGBE);
1318 			free(pi, M_CXGBE);
1319 			sc->port[i] = NULL;
1320 			goto done;
1321 		}
1322 
1323 		if (is_bt(pi->port_type))
1324 			setbit(&sc->bt_map, pi->tx_chan);
1325 		else
1326 			MPASS(!isset(&sc->bt_map, pi->tx_chan));
1327 
1328 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1329 		    device_get_nameunit(dev), i);
1330 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1331 		sc->chan_map[pi->tx_chan] = i;
1332 
1333 		/*
1334 		 * The MPS counter for FCS errors doesn't work correctly on the
1335 		 * T6 so we use the MAC counter here.  Which MAC is in use
1336 		 * depends on the link settings which will be known when the
1337 		 * link comes up.
1338 		 */
1339 		if (is_t6(sc)) {
1340 			pi->fcs_reg = -1;
1341 		} else if (is_t4(sc)) {
1342 			pi->fcs_reg = PORT_REG(pi->tx_chan,
1343 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1344 		} else {
1345 			pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
1346 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1347 		}
1348 		pi->fcs_base = 0;
1349 
1350 		/* All VIs on this port share this media. */
1351 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1352 		    cxgbe_media_status);
1353 
1354 		PORT_LOCK(pi);
1355 		init_link_config(pi);
1356 		fixup_link_config(pi);
1357 		build_medialist(pi);
1358 		if (fixed_ifmedia(pi))
1359 			pi->flags |= FIXED_IFMEDIA;
1360 		PORT_UNLOCK(pi);
1361 
1362 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1363 		    t4_ifnet_unit(sc, pi));
1364 		if (pi->dev == NULL) {
1365 			device_printf(dev,
1366 			    "failed to add device for port %d.\n", i);
1367 			rc = ENXIO;
1368 			goto done;
1369 		}
1370 		pi->vi[0].dev = pi->dev;
1371 		device_set_softc(pi->dev, pi);
1372 	}
1373 
1374 	/*
1375 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1376 	 */
1377 	nports = sc->params.nports;
1378 	rc = cfg_itype_and_nqueues(sc, &iaq);
1379 	if (rc != 0)
1380 		goto done; /* error message displayed already */
1381 
1382 	num_vis = iaq.num_vis;
1383 	sc->intr_type = iaq.intr_type;
1384 	sc->intr_count = iaq.nirq;
1385 
1386 	s = &sc->sge;
1387 	s->nrxq = nports * iaq.nrxq;
1388 	s->ntxq = nports * iaq.ntxq;
1389 	if (num_vis > 1) {
1390 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1391 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1392 	}
1393 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1394 	s->neq += nports;		/* ctrl queues: 1 per port */
1395 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1396 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1397 	if (is_offload(sc) || is_ethoffload(sc)) {
1398 		s->nofldtxq = nports * iaq.nofldtxq;
1399 		if (num_vis > 1)
1400 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1401 		s->neq += s->nofldtxq;
1402 
1403 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1404 		    M_CXGBE, M_ZERO | M_WAITOK);
1405 	}
1406 #endif
1407 #ifdef TCP_OFFLOAD
1408 	if (is_offload(sc)) {
1409 		s->nofldrxq = nports * iaq.nofldrxq;
1410 		if (num_vis > 1)
1411 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1412 		s->neq += s->nofldrxq;	/* free list */
1413 		s->niq += s->nofldrxq;
1414 
1415 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1416 		    M_CXGBE, M_ZERO | M_WAITOK);
1417 	}
1418 #endif
1419 #ifdef DEV_NETMAP
1420 	s->nnmrxq = 0;
1421 	s->nnmtxq = 0;
1422 	if (t4_native_netmap & NN_MAIN_VI) {
1423 		s->nnmrxq += nports * iaq.nnmrxq;
1424 		s->nnmtxq += nports * iaq.nnmtxq;
1425 	}
1426 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1427 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1428 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1429 	}
1430 	s->neq += s->nnmtxq + s->nnmrxq;
1431 	s->niq += s->nnmrxq;
1432 
1433 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1434 	    M_CXGBE, M_ZERO | M_WAITOK);
1435 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1436 	    M_CXGBE, M_ZERO | M_WAITOK);
1437 #endif
1438 	MPASS(s->niq <= s->iqmap_sz);
1439 	MPASS(s->neq <= s->eqmap_sz);
1440 
1441 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1442 	    M_ZERO | M_WAITOK);
1443 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1444 	    M_ZERO | M_WAITOK);
1445 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1446 	    M_ZERO | M_WAITOK);
1447 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1448 	    M_ZERO | M_WAITOK);
1449 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1450 	    M_ZERO | M_WAITOK);
1451 
1452 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1453 	    M_ZERO | M_WAITOK);
1454 
1455 	t4_init_l2t(sc, M_WAITOK);
1456 	t4_init_smt(sc, M_WAITOK);
1457 	t4_init_tx_sched(sc);
1458 	t4_init_atid_table(sc);
1459 #ifdef RATELIMIT
1460 	t4_init_etid_table(sc);
1461 #endif
1462 #ifdef INET6
1463 	t4_init_clip_table(sc);
1464 #endif
1465 	if (sc->vres.key.size != 0)
1466 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1467 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1468 
1469 	/*
1470 	 * Second pass over the ports.  This time we know the number of rx and
1471 	 * tx queues that each port should get.
1472 	 */
1473 	rqidx = tqidx = 0;
1474 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1475 	ofld_tqidx = 0;
1476 #endif
1477 #ifdef TCP_OFFLOAD
1478 	ofld_rqidx = 0;
1479 #endif
1480 #ifdef DEV_NETMAP
1481 	nm_rqidx = nm_tqidx = 0;
1482 #endif
1483 	for_each_port(sc, i) {
1484 		struct port_info *pi = sc->port[i];
1485 		struct vi_info *vi;
1486 
1487 		if (pi == NULL)
1488 			continue;
1489 
1490 		pi->nvi = num_vis;
1491 		for_each_vi(pi, j, vi) {
1492 			vi->pi = pi;
1493 			vi->adapter = sc;
1494 			vi->first_intr = -1;
1495 			vi->qsize_rxq = t4_qsize_rxq;
1496 			vi->qsize_txq = t4_qsize_txq;
1497 
1498 			vi->first_rxq = rqidx;
1499 			vi->first_txq = tqidx;
1500 			vi->tmr_idx = t4_tmr_idx;
1501 			vi->pktc_idx = t4_pktc_idx;
1502 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1503 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1504 
1505 			rqidx += vi->nrxq;
1506 			tqidx += vi->ntxq;
1507 
1508 			if (j == 0 && vi->ntxq > 1)
1509 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1510 			else
1511 				vi->rsrv_noflowq = 0;
1512 
1513 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1514 			vi->first_ofld_txq = ofld_tqidx;
1515 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1516 			ofld_tqidx += vi->nofldtxq;
1517 #endif
1518 #ifdef TCP_OFFLOAD
1519 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1520 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1521 			vi->first_ofld_rxq = ofld_rqidx;
1522 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1523 
1524 			ofld_rqidx += vi->nofldrxq;
1525 #endif
1526 #ifdef DEV_NETMAP
1527 			vi->first_nm_rxq = nm_rqidx;
1528 			vi->first_nm_txq = nm_tqidx;
1529 			if (j == 0) {
1530 				vi->nnmrxq = iaq.nnmrxq;
1531 				vi->nnmtxq = iaq.nnmtxq;
1532 			} else {
1533 				vi->nnmrxq = iaq.nnmrxq_vi;
1534 				vi->nnmtxq = iaq.nnmtxq_vi;
1535 			}
1536 			nm_rqidx += vi->nnmrxq;
1537 			nm_tqidx += vi->nnmtxq;
1538 #endif
1539 		}
1540 	}
1541 
1542 	rc = t4_setup_intr_handlers(sc);
1543 	if (rc != 0) {
1544 		device_printf(dev,
1545 		    "failed to setup interrupt handlers: %d\n", rc);
1546 		goto done;
1547 	}
1548 
1549 	rc = bus_generic_probe(dev);
1550 	if (rc != 0) {
1551 		device_printf(dev, "failed to probe child drivers: %d\n", rc);
1552 		goto done;
1553 	}
1554 
1555 	/*
1556 	 * Ensure thread-safe mailbox access (in debug builds).
1557 	 *
1558 	 * So far this was the only thread accessing the mailbox but various
1559 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1560 	 * will access the mailbox from different threads.
1561 	 */
1562 	sc->flags |= CHK_MBOX_ACCESS;
1563 
1564 	rc = bus_generic_attach(dev);
1565 	if (rc != 0) {
1566 		device_printf(dev,
1567 		    "failed to attach all child ports: %d\n", rc);
1568 		goto done;
1569 	}
1570 
1571 	device_printf(dev,
1572 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1573 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1574 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1575 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1576 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1577 
1578 	t4_set_desc(sc);
1579 
1580 	notify_siblings(dev, 0);
1581 
1582 done:
1583 	if (rc != 0 && sc->cdev) {
1584 		/* cdev was created and so cxgbetool works; recover that way. */
1585 		device_printf(dev,
1586 		    "error during attach, adapter is now in recovery mode.\n");
1587 		rc = 0;
1588 	}
1589 
1590 	if (rc != 0)
1591 		t4_detach_common(dev);
1592 	else
1593 		t4_sysctls(sc);
1594 
1595 	return (rc);
1596 }
1597 
1598 static int
t4_child_location_str(device_t bus,device_t dev,char * buf,size_t buflen)1599 t4_child_location_str(device_t bus, device_t dev, char *buf, size_t buflen)
1600 {
1601 	struct adapter *sc;
1602 	struct port_info *pi;
1603 	int i;
1604 
1605 	sc = device_get_softc(bus);
1606 	buf[0] = '\0';
1607 	for_each_port(sc, i) {
1608 		pi = sc->port[i];
1609 		if (pi != NULL && pi->dev == dev) {
1610 			snprintf(buf, buflen, "port=%d", pi->port_id);
1611 			break;
1612 		}
1613 	}
1614 	return (0);
1615 }
1616 
1617 static int
t4_ready(device_t dev)1618 t4_ready(device_t dev)
1619 {
1620 	struct adapter *sc;
1621 
1622 	sc = device_get_softc(dev);
1623 	if (sc->flags & FW_OK)
1624 		return (0);
1625 	return (ENXIO);
1626 }
1627 
1628 static int
t4_read_port_device(device_t dev,int port,device_t * child)1629 t4_read_port_device(device_t dev, int port, device_t *child)
1630 {
1631 	struct adapter *sc;
1632 	struct port_info *pi;
1633 
1634 	sc = device_get_softc(dev);
1635 	if (port < 0 || port >= MAX_NPORTS)
1636 		return (EINVAL);
1637 	pi = sc->port[port];
1638 	if (pi == NULL || pi->dev == NULL)
1639 		return (ENXIO);
1640 	*child = pi->dev;
1641 	return (0);
1642 }
1643 
1644 static int
notify_siblings(device_t dev,int detaching)1645 notify_siblings(device_t dev, int detaching)
1646 {
1647 	device_t sibling;
1648 	int error, i;
1649 
1650 	error = 0;
1651 	for (i = 0; i < PCI_FUNCMAX; i++) {
1652 		if (i == pci_get_function(dev))
1653 			continue;
1654 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1655 		    pci_get_slot(dev), i);
1656 		if (sibling == NULL || !device_is_attached(sibling))
1657 			continue;
1658 		if (detaching)
1659 			error = T4_DETACH_CHILD(sibling);
1660 		else
1661 			(void)T4_ATTACH_CHILD(sibling);
1662 		if (error)
1663 			break;
1664 	}
1665 	return (error);
1666 }
1667 
1668 /*
1669  * Idempotent
1670  */
1671 static int
t4_detach(device_t dev)1672 t4_detach(device_t dev)
1673 {
1674 	int rc;
1675 
1676 	rc = notify_siblings(dev, 1);
1677 	if (rc) {
1678 		device_printf(dev,
1679 		    "failed to detach sibling devices: %d\n", rc);
1680 		return (rc);
1681 	}
1682 
1683 	return (t4_detach_common(dev));
1684 }
1685 
1686 int
t4_detach_common(device_t dev)1687 t4_detach_common(device_t dev)
1688 {
1689 	struct adapter *sc;
1690 	struct port_info *pi;
1691 	int i, rc;
1692 
1693 	sc = device_get_softc(dev);
1694 
1695 	if (sc->cdev) {
1696 		destroy_dev(sc->cdev);
1697 		sc->cdev = NULL;
1698 	}
1699 
1700 	sx_xlock(&t4_list_lock);
1701 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1702 	sx_xunlock(&t4_list_lock);
1703 
1704 	sc->flags &= ~CHK_MBOX_ACCESS;
1705 	if (sc->flags & FULL_INIT_DONE) {
1706 		if (!(sc->flags & IS_VF))
1707 			t4_intr_disable(sc);
1708 	}
1709 
1710 	if (device_is_attached(dev)) {
1711 		rc = bus_generic_detach(dev);
1712 		if (rc) {
1713 			device_printf(dev,
1714 			    "failed to detach child devices: %d\n", rc);
1715 			return (rc);
1716 		}
1717 	}
1718 
1719 	for (i = 0; i < sc->intr_count; i++)
1720 		t4_free_irq(sc, &sc->irq[i]);
1721 
1722 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1723 		t4_free_tx_sched(sc);
1724 
1725 	for (i = 0; i < MAX_NPORTS; i++) {
1726 		pi = sc->port[i];
1727 		if (pi) {
1728 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1729 			if (pi->dev)
1730 				device_delete_child(dev, pi->dev);
1731 
1732 			mtx_destroy(&pi->pi_lock);
1733 			free(pi->vi, M_CXGBE);
1734 			free(pi, M_CXGBE);
1735 		}
1736 	}
1737 
1738 	device_delete_children(dev);
1739 	sysctl_ctx_free(&sc->ctx);
1740 	adapter_full_uninit(sc);
1741 
1742 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1743 		t4_fw_bye(sc, sc->mbox);
1744 
1745 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1746 		pci_release_msi(dev);
1747 
1748 	if (sc->regs_res)
1749 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1750 		    sc->regs_res);
1751 
1752 	if (sc->udbs_res)
1753 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1754 		    sc->udbs_res);
1755 
1756 	if (sc->msix_res)
1757 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1758 		    sc->msix_res);
1759 
1760 	if (sc->l2t)
1761 		t4_free_l2t(sc->l2t);
1762 	if (sc->smt)
1763 		t4_free_smt(sc->smt);
1764 	t4_free_atid_table(sc);
1765 #ifdef RATELIMIT
1766 	t4_free_etid_table(sc);
1767 #endif
1768 	if (sc->key_map)
1769 		vmem_destroy(sc->key_map);
1770 #ifdef INET6
1771 	t4_destroy_clip_table(sc);
1772 #endif
1773 
1774 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1775 	free(sc->sge.ofld_txq, M_CXGBE);
1776 #endif
1777 #ifdef TCP_OFFLOAD
1778 	free(sc->sge.ofld_rxq, M_CXGBE);
1779 #endif
1780 #ifdef DEV_NETMAP
1781 	free(sc->sge.nm_rxq, M_CXGBE);
1782 	free(sc->sge.nm_txq, M_CXGBE);
1783 #endif
1784 	free(sc->irq, M_CXGBE);
1785 	free(sc->sge.rxq, M_CXGBE);
1786 	free(sc->sge.txq, M_CXGBE);
1787 	free(sc->sge.ctrlq, M_CXGBE);
1788 	free(sc->sge.iqmap, M_CXGBE);
1789 	free(sc->sge.eqmap, M_CXGBE);
1790 	free(sc->tids.ftid_tab, M_CXGBE);
1791 	free(sc->tids.hpftid_tab, M_CXGBE);
1792 	free_hftid_hash(&sc->tids);
1793 	free(sc->tids.tid_tab, M_CXGBE);
1794 	free(sc->tt.tls_rx_ports, M_CXGBE);
1795 	t4_destroy_dma_tag(sc);
1796 
1797 	callout_drain(&sc->ktls_tick);
1798 	callout_drain(&sc->sfl_callout);
1799 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1800 		mtx_destroy(&sc->tids.ftid_lock);
1801 		cv_destroy(&sc->tids.ftid_cv);
1802 	}
1803 	if (mtx_initialized(&sc->tids.atid_lock))
1804 		mtx_destroy(&sc->tids.atid_lock);
1805 	if (mtx_initialized(&sc->ifp_lock))
1806 		mtx_destroy(&sc->ifp_lock);
1807 
1808 	if (rw_initialized(&sc->policy_lock)) {
1809 		rw_destroy(&sc->policy_lock);
1810 #ifdef TCP_OFFLOAD
1811 		if (sc->policy != NULL)
1812 			free_offload_policy(sc->policy);
1813 #endif
1814 	}
1815 
1816 	for (i = 0; i < NUM_MEMWIN; i++) {
1817 		struct memwin *mw = &sc->memwin[i];
1818 
1819 		if (rw_initialized(&mw->mw_lock))
1820 			rw_destroy(&mw->mw_lock);
1821 	}
1822 
1823 	mtx_destroy(&sc->sfl_lock);
1824 	mtx_destroy(&sc->reg_lock);
1825 	mtx_destroy(&sc->sc_lock);
1826 
1827 	bzero(sc, sizeof(*sc));
1828 
1829 	return (0);
1830 }
1831 
1832 static inline bool
ok_to_reset(struct adapter * sc)1833 ok_to_reset(struct adapter *sc)
1834 {
1835 	struct tid_info *t = &sc->tids;
1836 	struct port_info *pi;
1837 	struct vi_info *vi;
1838 	int i, j;
1839 	const int caps = IFCAP_TOE | IFCAP_TXTLS | IFCAP_NETMAP | IFCAP_TXRTLMT;
1840 
1841 	ASSERT_SYNCHRONIZED_OP(sc);
1842 	MPASS(!(sc->flags & IS_VF));
1843 
1844 	for_each_port(sc, i) {
1845 		pi = sc->port[i];
1846 		for_each_vi(pi, j, vi) {
1847 			if (vi->ifp->if_capenable & caps)
1848 				return (false);
1849 		}
1850 	}
1851 
1852 	if (atomic_load_int(&t->tids_in_use) > 0)
1853 		return (false);
1854 	if (atomic_load_int(&t->stids_in_use) > 0)
1855 		return (false);
1856 	if (atomic_load_int(&t->atids_in_use) > 0)
1857 		return (false);
1858 	if (atomic_load_int(&t->ftids_in_use) > 0)
1859 		return (false);
1860 	if (atomic_load_int(&t->hpftids_in_use) > 0)
1861 		return (false);
1862 	if (atomic_load_int(&t->etids_in_use) > 0)
1863 		return (false);
1864 
1865 	return (true);
1866 }
1867 
1868 static inline int
stop_adapter(struct adapter * sc)1869 stop_adapter(struct adapter *sc)
1870 {
1871 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED)))
1872 		return (1);		/* Already stopped. */
1873 	return (t4_shutdown_adapter(sc));
1874 }
1875 
1876 static int
t4_suspend(device_t dev)1877 t4_suspend(device_t dev)
1878 {
1879 	struct adapter *sc = device_get_softc(dev);
1880 	struct port_info *pi;
1881 	struct vi_info *vi;
1882 	struct ifnet *ifp;
1883 	struct sge_rxq *rxq;
1884 	struct sge_txq *txq;
1885 	struct sge_wrq *wrq;
1886 #ifdef TCP_OFFLOAD
1887 	struct sge_ofld_rxq *ofld_rxq;
1888 #endif
1889 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1890 	struct sge_ofld_txq *ofld_txq;
1891 #endif
1892 	int rc, i, j, k;
1893 
1894 	CH_ALERT(sc, "suspend requested\n");
1895 
1896 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4sus");
1897 	if (rc != 0)
1898 		return (ENXIO);
1899 
1900 	/* XXX: Can the kernel call suspend repeatedly without resume? */
1901 	MPASS(!hw_off_limits(sc));
1902 
1903 	if (!ok_to_reset(sc)) {
1904 		/* XXX: should list what resource is preventing suspend. */
1905 		CH_ERR(sc, "not safe to suspend.\n");
1906 		rc = EBUSY;
1907 		goto done;
1908 	}
1909 
1910 	/* No more DMA or interrupts. */
1911 	stop_adapter(sc);
1912 
1913 	/* Quiesce all activity. */
1914 	for_each_port(sc, i) {
1915 		pi = sc->port[i];
1916 		pi->vxlan_tcam_entry = false;
1917 
1918 		PORT_LOCK(pi);
1919 		if (pi->up_vis > 0) {
1920 			/*
1921 			 * t4_shutdown_adapter has already shut down all the
1922 			 * PHYs but it also disables interrupts and DMA so there
1923 			 * won't be a link interrupt.  So we update the state
1924 			 * manually and inform the kernel.
1925 			 */
1926 			pi->link_cfg.link_ok = false;
1927 			t4_os_link_changed(pi);
1928 		}
1929 		PORT_UNLOCK(pi);
1930 
1931 		for_each_vi(pi, j, vi) {
1932 			vi->xact_addr_filt = -1;
1933 			mtx_lock(&vi->tick_mtx);
1934 			vi->flags |= VI_SKIP_STATS;
1935 			mtx_unlock(&vi->tick_mtx);
1936 			if (!(vi->flags & VI_INIT_DONE))
1937 				continue;
1938 
1939 			ifp = vi->ifp;
1940 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1941 				mtx_lock(&vi->tick_mtx);
1942 				callout_stop(&vi->tick);
1943 				mtx_unlock(&vi->tick_mtx);
1944 				callout_drain(&vi->tick);
1945 			}
1946 
1947 			/*
1948 			 * Note that the HW is not available.
1949 			 */
1950 			for_each_txq(vi, k, txq) {
1951 				TXQ_LOCK(txq);
1952 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
1953 				TXQ_UNLOCK(txq);
1954 			}
1955 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1956 			for_each_ofld_txq(vi, k, ofld_txq) {
1957 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
1958 			}
1959 #endif
1960 			for_each_rxq(vi, k, rxq) {
1961 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
1962 			}
1963 #if defined(TCP_OFFLOAD)
1964 			for_each_ofld_rxq(vi, k, ofld_rxq) {
1965 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
1966 			}
1967 #endif
1968 
1969 			quiesce_vi(vi);
1970 		}
1971 
1972 		if (sc->flags & FULL_INIT_DONE) {
1973 			/* Control queue */
1974 			wrq = &sc->sge.ctrlq[i];
1975 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
1976 			quiesce_wrq(wrq);
1977 		}
1978 	}
1979 	if (sc->flags & FULL_INIT_DONE) {
1980 		/* Firmware event queue */
1981 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
1982 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
1983 	}
1984 
1985 	/* Mark the adapter totally off limits. */
1986 	mtx_lock(&sc->reg_lock);
1987 	atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
1988 	sc->flags &= ~(FW_OK | MASTER_PF);
1989 	sc->reset_thread = NULL;
1990 	mtx_unlock(&sc->reg_lock);
1991 
1992 	CH_ALERT(sc, "suspend completed.\n");
1993 done:
1994 	end_synchronized_op(sc, 0);
1995 	return (rc);
1996 }
1997 
1998 struct adapter_pre_reset_state {
1999 	u_int flags;
2000 	uint16_t nbmcaps;
2001 	uint16_t linkcaps;
2002 	uint16_t switchcaps;
2003 	uint16_t niccaps;
2004 	uint16_t toecaps;
2005 	uint16_t rdmacaps;
2006 	uint16_t cryptocaps;
2007 	uint16_t iscsicaps;
2008 	uint16_t fcoecaps;
2009 
2010 	u_int cfcsum;
2011 	char cfg_file[32];
2012 
2013 	struct adapter_params params;
2014 	struct t4_virt_res vres;
2015 	struct tid_info tids;
2016 	struct sge sge;
2017 
2018 	int rawf_base;
2019 	int nrawf;
2020 
2021 };
2022 
2023 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2024 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2025 {
2026 
2027 	ASSERT_SYNCHRONIZED_OP(sc);
2028 
2029 	o->flags = sc->flags;
2030 
2031 	o->nbmcaps =  sc->nbmcaps;
2032 	o->linkcaps = sc->linkcaps;
2033 	o->switchcaps = sc->switchcaps;
2034 	o->niccaps = sc->niccaps;
2035 	o->toecaps = sc->toecaps;
2036 	o->rdmacaps = sc->rdmacaps;
2037 	o->cryptocaps = sc->cryptocaps;
2038 	o->iscsicaps = sc->iscsicaps;
2039 	o->fcoecaps = sc->fcoecaps;
2040 
2041 	o->cfcsum = sc->cfcsum;
2042 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2043 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2044 
2045 	o->params = sc->params;
2046 	o->vres = sc->vres;
2047 	o->tids = sc->tids;
2048 	o->sge = sc->sge;
2049 
2050 	o->rawf_base = sc->rawf_base;
2051 	o->nrawf = sc->nrawf;
2052 }
2053 
2054 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2055 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2056 {
2057 	int rc = 0;
2058 
2059 	ASSERT_SYNCHRONIZED_OP(sc);
2060 
2061 	/* Capabilities */
2062 #define COMPARE_CAPS(c) do { \
2063 	if (o->c##caps != sc->c##caps) { \
2064 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2065 		    sc->c##caps); \
2066 		rc = EINVAL; \
2067 	} \
2068 } while (0)
2069 	COMPARE_CAPS(nbm);
2070 	COMPARE_CAPS(link);
2071 	COMPARE_CAPS(switch);
2072 	COMPARE_CAPS(nic);
2073 	COMPARE_CAPS(toe);
2074 	COMPARE_CAPS(rdma);
2075 	COMPARE_CAPS(crypto);
2076 	COMPARE_CAPS(iscsi);
2077 	COMPARE_CAPS(fcoe);
2078 #undef COMPARE_CAPS
2079 
2080 	/* Firmware config file */
2081 	if (o->cfcsum != sc->cfcsum) {
2082 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2083 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2084 		rc = EINVAL;
2085 	}
2086 
2087 #define COMPARE_PARAM(p, name) do { \
2088 	if (o->p != sc->p) { \
2089 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2090 		rc = EINVAL; \
2091 	} \
2092 } while (0)
2093 	COMPARE_PARAM(sge.iq_start, iq_start);
2094 	COMPARE_PARAM(sge.eq_start, eq_start);
2095 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2096 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2097 	COMPARE_PARAM(tids.nftids, nftids);
2098 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2099 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2100 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2101 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2102 	COMPARE_PARAM(tids.tid_base, tid_base);
2103 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2104 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2105 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2106 	COMPARE_PARAM(rawf_base, rawf_base);
2107 	COMPARE_PARAM(nrawf, nrawf);
2108 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2109 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2110 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2111 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2112 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2113 	COMPARE_PARAM(tids.ntids, ntids);
2114 	COMPARE_PARAM(tids.etid_base, etid_base);
2115 	COMPARE_PARAM(tids.etid_end, etid_end);
2116 	COMPARE_PARAM(tids.netids, netids);
2117 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2118 	COMPARE_PARAM(params.ethoffload, ethoffload);
2119 	COMPARE_PARAM(tids.natids, natids);
2120 	COMPARE_PARAM(tids.stid_base, stid_base);
2121 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2122 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2123 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2124 	COMPARE_PARAM(vres.stag.start, stag_start);
2125 	COMPARE_PARAM(vres.stag.size, stag_size);
2126 	COMPARE_PARAM(vres.rq.start, rq_start);
2127 	COMPARE_PARAM(vres.rq.size, rq_size);
2128 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2129 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2130 	COMPARE_PARAM(vres.qp.start, qp_start);
2131 	COMPARE_PARAM(vres.qp.size, qp_size);
2132 	COMPARE_PARAM(vres.cq.start, cq_start);
2133 	COMPARE_PARAM(vres.cq.size, cq_size);
2134 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2135 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2136 	COMPARE_PARAM(vres.srq.start, srq_start);
2137 	COMPARE_PARAM(vres.srq.size, srq_size);
2138 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2139 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2140 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2141 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2142 	COMPARE_PARAM(vres.key.start, key_start);
2143 	COMPARE_PARAM(vres.key.size, key_size);
2144 #undef COMPARE_PARAM
2145 
2146 	return (rc);
2147 }
2148 
2149 static int
t4_resume(device_t dev)2150 t4_resume(device_t dev)
2151 {
2152 	struct adapter *sc = device_get_softc(dev);
2153 	struct adapter_pre_reset_state *old_state = NULL;
2154 	struct port_info *pi;
2155 	struct vi_info *vi;
2156 	struct ifnet *ifp;
2157 	struct sge_txq *txq;
2158 	int rc, i, j, k;
2159 
2160 	CH_ALERT(sc, "resume requested.\n");
2161 
2162 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4res");
2163 	if (rc != 0)
2164 		return (ENXIO);
2165 	MPASS(hw_off_limits(sc));
2166 	MPASS((sc->flags & FW_OK) == 0);
2167 	MPASS((sc->flags & MASTER_PF) == 0);
2168 	MPASS(sc->reset_thread == NULL);
2169 	sc->reset_thread = curthread;
2170 
2171 	/* Register access is expected to work by the time we're here. */
2172 	if (t4_read_reg(sc, A_PL_WHOAMI) == 0xffffffff) {
2173 		CH_ERR(sc, "%s: can't read device registers\n", __func__);
2174 		rc = ENXIO;
2175 		goto done;
2176 	}
2177 
2178 	/* Note that HW_OFF_LIMITS is cleared a bit later. */
2179 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR | ADAP_STOPPED);
2180 
2181 	/* Restore memory window. */
2182 	setup_memwin(sc);
2183 
2184 	/* Go no further if recovery mode has been requested. */
2185 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2186 		CH_ALERT(sc, "recovery mode on resume.\n");
2187 		rc = 0;
2188 		mtx_lock(&sc->reg_lock);
2189 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
2190 		mtx_unlock(&sc->reg_lock);
2191 		goto done;
2192 	}
2193 
2194 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2195 	save_caps_and_params(sc, old_state);
2196 
2197 	/* Reestablish contact with firmware and become the primary PF. */
2198 	rc = contact_firmware(sc);
2199 	if (rc != 0)
2200 		goto done; /* error message displayed already */
2201 	MPASS(sc->flags & FW_OK);
2202 
2203 	if (sc->flags & MASTER_PF) {
2204 		rc = partition_resources(sc);
2205 		if (rc != 0)
2206 			goto done; /* error message displayed already */
2207 		t4_intr_clear(sc);
2208 	}
2209 
2210 	rc = get_params__post_init(sc);
2211 	if (rc != 0)
2212 		goto done; /* error message displayed already */
2213 
2214 	rc = set_params__post_init(sc);
2215 	if (rc != 0)
2216 		goto done; /* error message displayed already */
2217 
2218 	rc = compare_caps_and_params(sc, old_state);
2219 	if (rc != 0)
2220 		goto done; /* error message displayed already */
2221 
2222 	for_each_port(sc, i) {
2223 		pi = sc->port[i];
2224 		MPASS(pi != NULL);
2225 		MPASS(pi->vi != NULL);
2226 		MPASS(pi->vi[0].dev == pi->dev);
2227 
2228 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2229 		if (rc != 0) {
2230 			CH_ERR(sc,
2231 			    "failed to re-initialize port %d: %d\n", i, rc);
2232 			goto done;
2233 		}
2234 		MPASS(sc->chan_map[pi->tx_chan] == i);
2235 
2236 		PORT_LOCK(pi);
2237 		fixup_link_config(pi);
2238 		build_medialist(pi);
2239 		PORT_UNLOCK(pi);
2240 		for_each_vi(pi, j, vi) {
2241 			if (IS_MAIN_VI(vi))
2242 				continue;
2243 			rc = alloc_extra_vi(sc, pi, vi);
2244 			if (rc != 0) {
2245 				CH_ERR(vi,
2246 				    "failed to re-allocate extra VI: %d\n", rc);
2247 				goto done;
2248 			}
2249 		}
2250 	}
2251 
2252 	/*
2253 	 * Interrupts and queues are about to be enabled and other threads will
2254 	 * want to access the hardware too.  It is safe to do so.  Note that
2255 	 * this thread is still in the middle of a synchronized_op.
2256 	 */
2257 	mtx_lock(&sc->reg_lock);
2258 	atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
2259 	mtx_unlock(&sc->reg_lock);
2260 
2261 	if (sc->flags & FULL_INIT_DONE) {
2262 		rc = adapter_full_init(sc);
2263 		if (rc != 0) {
2264 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2265 			goto done;
2266 		}
2267 
2268 		if (sc->vxlan_refcount > 0)
2269 			enable_vxlan_rx(sc);
2270 
2271 		for_each_port(sc, i) {
2272 			pi = sc->port[i];
2273 			for_each_vi(pi, j, vi) {
2274 				mtx_lock(&vi->tick_mtx);
2275 				vi->flags &= ~VI_SKIP_STATS;
2276 				mtx_unlock(&vi->tick_mtx);
2277 				if (!(vi->flags & VI_INIT_DONE))
2278 					continue;
2279 				rc = vi_full_init(vi);
2280 				if (rc != 0) {
2281 					CH_ERR(vi, "failed to re-initialize "
2282 					    "interface: %d\n", rc);
2283 					goto done;
2284 				}
2285 
2286 				ifp = vi->ifp;
2287 				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2288 					continue;
2289 				/*
2290 				 * Note that we do not setup multicast addresses
2291 				 * in the first pass.  This ensures that the
2292 				 * unicast DMACs for all VIs on all ports get an
2293 				 * MPS TCAM entry.
2294 				 */
2295 				rc = update_mac_settings(ifp, XGMAC_ALL &
2296 				    ~XGMAC_MCADDRS);
2297 				if (rc != 0) {
2298 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2299 					goto done;
2300 				}
2301 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2302 				    true);
2303 				if (rc != 0) {
2304 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2305 					goto done;
2306 				}
2307 				for_each_txq(vi, k, txq) {
2308 					TXQ_LOCK(txq);
2309 					txq->eq.flags |= EQ_ENABLED;
2310 					TXQ_UNLOCK(txq);
2311 				}
2312 				mtx_lock(&vi->tick_mtx);
2313 				callout_schedule(&vi->tick, hz);
2314 				mtx_unlock(&vi->tick_mtx);
2315 			}
2316 			PORT_LOCK(pi);
2317 			if (pi->up_vis > 0) {
2318 				t4_update_port_info(pi);
2319 				fixup_link_config(pi);
2320 				build_medialist(pi);
2321 				apply_link_config(pi);
2322 				if (pi->link_cfg.link_ok)
2323 					t4_os_link_changed(pi);
2324 			}
2325 			PORT_UNLOCK(pi);
2326 		}
2327 
2328 		/* Now reprogram the L2 multicast addresses. */
2329 		for_each_port(sc, i) {
2330 			pi = sc->port[i];
2331 			for_each_vi(pi, j, vi) {
2332 				if (!(vi->flags & VI_INIT_DONE))
2333 					continue;
2334 				ifp = vi->ifp;
2335 				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2336 					continue;
2337 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2338 				if (rc != 0) {
2339 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2340 					rc = 0;	/* carry on */
2341 				}
2342 			}
2343 		}
2344 	}
2345 done:
2346 	if (rc == 0) {
2347 		sc->incarnation++;
2348 		CH_ALERT(sc, "resume completed.\n");
2349 	}
2350 	end_synchronized_op(sc, 0);
2351 	free(old_state, M_CXGBE);
2352 	return (rc);
2353 }
2354 
2355 static int
t4_reset_prepare(device_t dev,device_t child)2356 t4_reset_prepare(device_t dev, device_t child)
2357 {
2358 	struct adapter *sc = device_get_softc(dev);
2359 
2360 	CH_ALERT(sc, "reset_prepare.\n");
2361 	return (0);
2362 }
2363 
2364 static int
t4_reset_post(device_t dev,device_t child)2365 t4_reset_post(device_t dev, device_t child)
2366 {
2367 	struct adapter *sc = device_get_softc(dev);
2368 
2369 	CH_ALERT(sc, "reset_post.\n");
2370 	return (0);
2371 }
2372 
2373 static int
reset_adapter(struct adapter * sc)2374 reset_adapter(struct adapter *sc)
2375 {
2376 	int rc, oldinc, error_flags;
2377 
2378 	CH_ALERT(sc, "reset requested.\n");
2379 
2380 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rst1");
2381 	if (rc != 0)
2382 		return (EBUSY);
2383 
2384 	if (hw_off_limits(sc)) {
2385 		CH_ERR(sc, "adapter is suspended, use resume (not reset).\n");
2386 		rc = ENXIO;
2387 		goto done;
2388 	}
2389 
2390 	if (!ok_to_reset(sc)) {
2391 		/* XXX: should list what resource is preventing reset. */
2392 		CH_ERR(sc, "not safe to reset.\n");
2393 		rc = EBUSY;
2394 		goto done;
2395 	}
2396 
2397 done:
2398 	oldinc = sc->incarnation;
2399 	end_synchronized_op(sc, 0);
2400 	if (rc != 0)
2401 		return (rc);	/* Error logged already. */
2402 
2403 	atomic_add_int(&sc->num_resets, 1);
2404 	mtx_lock(&Giant);
2405 	rc = BUS_RESET_CHILD(device_get_parent(sc->dev), sc->dev, 0);
2406 	mtx_unlock(&Giant);
2407 	if (rc != 0)
2408 		CH_ERR(sc, "bus_reset_child failed: %d.\n", rc);
2409 	else {
2410 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rst2");
2411 		if (rc != 0)
2412 			return (EBUSY);
2413 		error_flags = atomic_load_int(&sc->error_flags);
2414 		if (sc->incarnation > oldinc && error_flags == 0) {
2415 			CH_ALERT(sc, "bus_reset_child succeeded.\n");
2416 		} else {
2417 			CH_ERR(sc, "adapter did not reset properly, flags "
2418 			    "0x%08x, error_flags 0x%08x.\n", sc->flags,
2419 			    error_flags);
2420 			rc = ENXIO;
2421 		}
2422 		end_synchronized_op(sc, 0);
2423 	}
2424 
2425 	return (rc);
2426 }
2427 
2428 static void
reset_adapter_task(void * arg,int pending)2429 reset_adapter_task(void *arg, int pending)
2430 {
2431 	/* XXX: t4_async_event here? */
2432 	reset_adapter(arg);
2433 }
2434 
2435 static int
cxgbe_probe(device_t dev)2436 cxgbe_probe(device_t dev)
2437 {
2438 	char buf[128];
2439 	struct port_info *pi = device_get_softc(dev);
2440 
2441 	snprintf(buf, sizeof(buf), "port %d", pi->port_id);
2442 	device_set_desc_copy(dev, buf);
2443 
2444 	return (BUS_PROBE_DEFAULT);
2445 }
2446 
2447 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2448     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2449     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2450     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2451 #define T4_CAP_ENABLE (T4_CAP)
2452 
2453 static int
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2454 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2455 {
2456 	struct ifnet *ifp;
2457 	struct sbuf *sb;
2458 	struct sysctl_ctx_list *ctx = &vi->ctx;
2459 	struct sysctl_oid_list *children;
2460 	struct pfil_head_args pa;
2461 	struct adapter *sc = vi->adapter;
2462 
2463 	sysctl_ctx_init(ctx);
2464 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2465 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2466 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2467 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2468 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2469 #ifdef DEV_NETMAP
2470 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2471 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2472 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2473 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2474 #endif
2475 #ifdef TCP_OFFLOAD
2476 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2477 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2478 #endif
2479 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2480 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2481 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2482 #endif
2483 
2484 	vi->xact_addr_filt = -1;
2485 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2486 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2487 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2488 		vi->flags |= TX_USES_VM_WR;
2489 
2490 	/* Allocate an ifnet and set it up */
2491 	ifp = if_alloc_dev(IFT_ETHER, dev);
2492 	if (ifp == NULL) {
2493 		device_printf(dev, "Cannot allocate ifnet\n");
2494 		return (ENOMEM);
2495 	}
2496 	vi->ifp = ifp;
2497 	ifp->if_softc = vi;
2498 
2499 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2500 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2501 
2502 	ifp->if_init = cxgbe_init;
2503 	ifp->if_ioctl = cxgbe_ioctl;
2504 	ifp->if_transmit = cxgbe_transmit;
2505 	ifp->if_qflush = cxgbe_qflush;
2506 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2507 		ifp->if_get_counter = vi_get_counter;
2508 	else
2509 		ifp->if_get_counter = cxgbe_get_counter;
2510 #if defined(KERN_TLS) || defined(RATELIMIT)
2511 	ifp->if_snd_tag_alloc = cxgbe_snd_tag_alloc;
2512 	ifp->if_snd_tag_modify = cxgbe_snd_tag_modify;
2513 	ifp->if_snd_tag_query = cxgbe_snd_tag_query;
2514 	ifp->if_snd_tag_free = cxgbe_snd_tag_free;
2515 #endif
2516 #ifdef RATELIMIT
2517 	ifp->if_ratelimit_query = cxgbe_ratelimit_query;
2518 #endif
2519 
2520 	ifp->if_capabilities = T4_CAP;
2521 	ifp->if_capenable = T4_CAP_ENABLE;
2522 	ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2523 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6;
2524 	if (chip_id(sc) >= CHELSIO_T6) {
2525 		ifp->if_capabilities |= IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO;
2526 		ifp->if_capenable |= IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO;
2527 		ifp->if_hwassist |= CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2528 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2529 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN;
2530 	}
2531 
2532 #ifdef TCP_OFFLOAD
2533 	if (vi->nofldrxq != 0)
2534 		ifp->if_capabilities |= IFCAP_TOE;
2535 #endif
2536 #ifdef RATELIMIT
2537 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2538 		ifp->if_capabilities |= IFCAP_TXRTLMT;
2539 		ifp->if_capenable |= IFCAP_TXRTLMT;
2540 	}
2541 #endif
2542 
2543 	ifp->if_hw_tsomax = IP_MAXPACKET;
2544 	if (vi->flags & TX_USES_VM_WR)
2545 		ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_VM_TSO;
2546 	else
2547 		ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_TSO;
2548 #ifdef RATELIMIT
2549 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2550 		ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_EO_TSO;
2551 #endif
2552 	ifp->if_hw_tsomaxsegsize = 65536;
2553 #ifdef KERN_TLS
2554 	if (is_ktls(sc)) {
2555 		ifp->if_capabilities |= IFCAP_TXTLS;
2556 		if (sc->flags & KERN_TLS_ON)
2557 			ifp->if_capenable |= IFCAP_TXTLS;
2558 	}
2559 #endif
2560 
2561 	ether_ifattach(ifp, vi->hw_addr);
2562 #ifdef DEV_NETMAP
2563 	if (vi->nnmrxq != 0)
2564 		cxgbe_nm_attach(vi);
2565 #endif
2566 	sb = sbuf_new_auto();
2567 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2568 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2569 	switch (ifp->if_capabilities & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2570 	case IFCAP_TOE:
2571 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2572 		break;
2573 	case IFCAP_TOE | IFCAP_TXRTLMT:
2574 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2575 		break;
2576 	case IFCAP_TXRTLMT:
2577 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2578 		break;
2579 	}
2580 #endif
2581 #ifdef TCP_OFFLOAD
2582 	if (ifp->if_capabilities & IFCAP_TOE)
2583 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2584 #endif
2585 #ifdef DEV_NETMAP
2586 	if (ifp->if_capabilities & IFCAP_NETMAP)
2587 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2588 		    vi->nnmtxq, vi->nnmrxq);
2589 #endif
2590 	sbuf_finish(sb);
2591 	device_printf(dev, "%s\n", sbuf_data(sb));
2592 	sbuf_delete(sb);
2593 
2594 	vi_sysctls(vi);
2595 
2596 	pa.pa_version = PFIL_VERSION;
2597 	pa.pa_flags = PFIL_IN;
2598 	pa.pa_type = PFIL_TYPE_ETHERNET;
2599 	pa.pa_headname = ifp->if_xname;
2600 	vi->pfil = pfil_head_register(&pa);
2601 
2602 	return (0);
2603 }
2604 
2605 static int
cxgbe_attach(device_t dev)2606 cxgbe_attach(device_t dev)
2607 {
2608 	struct port_info *pi = device_get_softc(dev);
2609 	struct adapter *sc = pi->adapter;
2610 	struct vi_info *vi;
2611 	int i, rc;
2612 
2613 	sysctl_ctx_init(&pi->ctx);
2614 
2615 	rc = cxgbe_vi_attach(dev, &pi->vi[0]);
2616 	if (rc)
2617 		return (rc);
2618 
2619 	for_each_vi(pi, i, vi) {
2620 		if (i == 0)
2621 			continue;
2622 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, -1);
2623 		if (vi->dev == NULL) {
2624 			device_printf(dev, "failed to add VI %d\n", i);
2625 			continue;
2626 		}
2627 		device_set_softc(vi->dev, vi);
2628 	}
2629 
2630 	cxgbe_sysctls(pi);
2631 
2632 	bus_generic_attach(dev);
2633 
2634 	return (0);
2635 }
2636 
2637 static void
cxgbe_vi_detach(struct vi_info * vi)2638 cxgbe_vi_detach(struct vi_info *vi)
2639 {
2640 	struct ifnet *ifp = vi->ifp;
2641 
2642 	if (vi->pfil != NULL) {
2643 		pfil_head_unregister(vi->pfil);
2644 		vi->pfil = NULL;
2645 	}
2646 
2647 	ether_ifdetach(ifp);
2648 
2649 	/* Let detach proceed even if these fail. */
2650 #ifdef DEV_NETMAP
2651 	if (ifp->if_capabilities & IFCAP_NETMAP)
2652 		cxgbe_nm_detach(vi);
2653 #endif
2654 	cxgbe_uninit_synchronized(vi);
2655 	callout_drain(&vi->tick);
2656 	sysctl_ctx_free(&vi->ctx);
2657 	vi_full_uninit(vi);
2658 
2659 	if_free(vi->ifp);
2660 	vi->ifp = NULL;
2661 }
2662 
2663 static int
cxgbe_detach(device_t dev)2664 cxgbe_detach(device_t dev)
2665 {
2666 	struct port_info *pi = device_get_softc(dev);
2667 	struct adapter *sc = pi->adapter;
2668 	int rc;
2669 
2670 	/* Detach the extra VIs first. */
2671 	rc = bus_generic_detach(dev);
2672 	if (rc)
2673 		return (rc);
2674 	device_delete_children(dev);
2675 
2676 	sysctl_ctx_free(&pi->ctx);
2677 	doom_vi(sc, &pi->vi[0]);
2678 
2679 	if (pi->flags & HAS_TRACEQ) {
2680 		sc->traceq = -1;	/* cloner should not create ifnet */
2681 		t4_tracer_port_detach(sc);
2682 	}
2683 
2684 	cxgbe_vi_detach(&pi->vi[0]);
2685 	ifmedia_removeall(&pi->media);
2686 
2687 	end_synchronized_op(sc, 0);
2688 
2689 	return (0);
2690 }
2691 
2692 static void
cxgbe_init(void * arg)2693 cxgbe_init(void *arg)
2694 {
2695 	struct vi_info *vi = arg;
2696 	struct adapter *sc = vi->adapter;
2697 
2698 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
2699 		return;
2700 	cxgbe_init_synchronized(vi);
2701 	end_synchronized_op(sc, 0);
2702 }
2703 
2704 static int
cxgbe_ioctl(struct ifnet * ifp,unsigned long cmd,caddr_t data)2705 cxgbe_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
2706 {
2707 	int rc = 0, mtu, flags;
2708 	struct vi_info *vi = ifp->if_softc;
2709 	struct port_info *pi = vi->pi;
2710 	struct adapter *sc = pi->adapter;
2711 	struct ifreq *ifr = (struct ifreq *)data;
2712 	uint32_t mask;
2713 
2714 	switch (cmd) {
2715 	case SIOCSIFMTU:
2716 		mtu = ifr->ifr_mtu;
2717 		if (mtu < ETHERMIN || mtu > MAX_MTU)
2718 			return (EINVAL);
2719 
2720 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
2721 		if (rc)
2722 			return (rc);
2723 		ifp->if_mtu = mtu;
2724 		if (vi->flags & VI_INIT_DONE) {
2725 			t4_update_fl_bufsize(ifp);
2726 			if (!hw_off_limits(sc) &&
2727 			    ifp->if_drv_flags & IFF_DRV_RUNNING)
2728 				rc = update_mac_settings(ifp, XGMAC_MTU);
2729 		}
2730 		end_synchronized_op(sc, 0);
2731 		break;
2732 
2733 	case SIOCSIFFLAGS:
2734 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
2735 		if (rc)
2736 			return (rc);
2737 
2738 		if (hw_off_limits(sc)) {
2739 			rc = ENXIO;
2740 			goto fail;
2741 		}
2742 
2743 		if (ifp->if_flags & IFF_UP) {
2744 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2745 				flags = vi->if_flags;
2746 				if ((ifp->if_flags ^ flags) &
2747 				    (IFF_PROMISC | IFF_ALLMULTI)) {
2748 					rc = update_mac_settings(ifp,
2749 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
2750 				}
2751 			} else {
2752 				rc = cxgbe_init_synchronized(vi);
2753 			}
2754 			vi->if_flags = ifp->if_flags;
2755 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2756 			rc = cxgbe_uninit_synchronized(vi);
2757 		}
2758 		end_synchronized_op(sc, 0);
2759 		break;
2760 
2761 	case SIOCADDMULTI:
2762 	case SIOCDELMULTI:
2763 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
2764 		if (rc)
2765 			return (rc);
2766 		if (!hw_off_limits(sc) && ifp->if_drv_flags & IFF_DRV_RUNNING)
2767 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2768 		end_synchronized_op(sc, 0);
2769 		break;
2770 
2771 	case SIOCSIFCAP:
2772 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
2773 		if (rc)
2774 			return (rc);
2775 
2776 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2777 		if (mask & IFCAP_TXCSUM) {
2778 			ifp->if_capenable ^= IFCAP_TXCSUM;
2779 			ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2780 
2781 			if (IFCAP_TSO4 & ifp->if_capenable &&
2782 			    !(IFCAP_TXCSUM & ifp->if_capenable)) {
2783 				mask &= ~IFCAP_TSO4;
2784 				ifp->if_capenable &= ~IFCAP_TSO4;
2785 				if_printf(ifp,
2786 				    "tso4 disabled due to -txcsum.\n");
2787 			}
2788 		}
2789 		if (mask & IFCAP_TXCSUM_IPV6) {
2790 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
2791 			ifp->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2792 
2793 			if (IFCAP_TSO6 & ifp->if_capenable &&
2794 			    !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2795 				mask &= ~IFCAP_TSO6;
2796 				ifp->if_capenable &= ~IFCAP_TSO6;
2797 				if_printf(ifp,
2798 				    "tso6 disabled due to -txcsum6.\n");
2799 			}
2800 		}
2801 		if (mask & IFCAP_RXCSUM)
2802 			ifp->if_capenable ^= IFCAP_RXCSUM;
2803 		if (mask & IFCAP_RXCSUM_IPV6)
2804 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
2805 
2806 		/*
2807 		 * Note that we leave CSUM_TSO alone (it is always set).  The
2808 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
2809 		 * sending a TSO request our way, so it's sufficient to toggle
2810 		 * IFCAP_TSOx only.
2811 		 */
2812 		if (mask & IFCAP_TSO4) {
2813 			if (!(IFCAP_TSO4 & ifp->if_capenable) &&
2814 			    !(IFCAP_TXCSUM & ifp->if_capenable)) {
2815 				if_printf(ifp, "enable txcsum first.\n");
2816 				rc = EAGAIN;
2817 				goto fail;
2818 			}
2819 			ifp->if_capenable ^= IFCAP_TSO4;
2820 		}
2821 		if (mask & IFCAP_TSO6) {
2822 			if (!(IFCAP_TSO6 & ifp->if_capenable) &&
2823 			    !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2824 				if_printf(ifp, "enable txcsum6 first.\n");
2825 				rc = EAGAIN;
2826 				goto fail;
2827 			}
2828 			ifp->if_capenable ^= IFCAP_TSO6;
2829 		}
2830 		if (mask & IFCAP_LRO) {
2831 #if defined(INET) || defined(INET6)
2832 			int i;
2833 			struct sge_rxq *rxq;
2834 
2835 			ifp->if_capenable ^= IFCAP_LRO;
2836 			for_each_rxq(vi, i, rxq) {
2837 				if (ifp->if_capenable & IFCAP_LRO)
2838 					rxq->iq.flags |= IQ_LRO_ENABLED;
2839 				else
2840 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
2841 			}
2842 #endif
2843 		}
2844 #ifdef TCP_OFFLOAD
2845 		if (mask & IFCAP_TOE) {
2846 			int enable = (ifp->if_capenable ^ mask) & IFCAP_TOE;
2847 
2848 			rc = toe_capability(vi, enable);
2849 			if (rc != 0)
2850 				goto fail;
2851 
2852 			ifp->if_capenable ^= mask;
2853 		}
2854 #endif
2855 		if (mask & IFCAP_VLAN_HWTAGGING) {
2856 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2857 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2858 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
2859 		}
2860 		if (mask & IFCAP_VLAN_MTU) {
2861 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
2862 
2863 			/* Need to find out how to disable auto-mtu-inflation */
2864 		}
2865 		if (mask & IFCAP_VLAN_HWTSO)
2866 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
2867 		if (mask & IFCAP_VLAN_HWCSUM)
2868 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2869 #ifdef RATELIMIT
2870 		if (mask & IFCAP_TXRTLMT)
2871 			ifp->if_capenable ^= IFCAP_TXRTLMT;
2872 #endif
2873 		if (mask & IFCAP_HWRXTSTMP) {
2874 			int i;
2875 			struct sge_rxq *rxq;
2876 
2877 			ifp->if_capenable ^= IFCAP_HWRXTSTMP;
2878 			for_each_rxq(vi, i, rxq) {
2879 				if (ifp->if_capenable & IFCAP_HWRXTSTMP)
2880 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
2881 				else
2882 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
2883 			}
2884 		}
2885 		if (mask & IFCAP_MEXTPG)
2886 			ifp->if_capenable ^= IFCAP_MEXTPG;
2887 
2888 #ifdef KERN_TLS
2889 		if (mask & IFCAP_TXTLS) {
2890 			int enable = (ifp->if_capenable ^ mask) & IFCAP_TXTLS;
2891 
2892 			rc = ktls_capability(sc, enable);
2893 			if (rc != 0)
2894 				goto fail;
2895 
2896 			ifp->if_capenable ^= (mask & IFCAP_TXTLS);
2897 		}
2898 #endif
2899 		if (mask & IFCAP_VXLAN_HWCSUM) {
2900 			ifp->if_capenable ^= IFCAP_VXLAN_HWCSUM;
2901 			ifp->if_hwassist ^= CSUM_INNER_IP6_UDP |
2902 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
2903 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP;
2904 		}
2905 		if (mask & IFCAP_VXLAN_HWTSO) {
2906 			ifp->if_capenable ^= IFCAP_VXLAN_HWTSO;
2907 			ifp->if_hwassist ^= CSUM_INNER_IP6_TSO |
2908 			    CSUM_INNER_IP_TSO;
2909 		}
2910 
2911 #ifdef VLAN_CAPABILITIES
2912 		VLAN_CAPABILITIES(ifp);
2913 #endif
2914 fail:
2915 		end_synchronized_op(sc, 0);
2916 		break;
2917 
2918 	case SIOCSIFMEDIA:
2919 	case SIOCGIFMEDIA:
2920 	case SIOCGIFXMEDIA:
2921 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
2922 		break;
2923 
2924 	case SIOCGI2C: {
2925 		struct ifi2creq i2c;
2926 
2927 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
2928 		if (rc != 0)
2929 			break;
2930 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
2931 			rc = EPERM;
2932 			break;
2933 		}
2934 		if (i2c.len > sizeof(i2c.data)) {
2935 			rc = EINVAL;
2936 			break;
2937 		}
2938 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
2939 		if (rc)
2940 			return (rc);
2941 		if (hw_off_limits(sc))
2942 			rc = ENXIO;
2943 		else
2944 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
2945 			    i2c.offset, i2c.len, &i2c.data[0]);
2946 		end_synchronized_op(sc, 0);
2947 		if (rc == 0)
2948 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
2949 		break;
2950 	}
2951 
2952 	default:
2953 		rc = ether_ioctl(ifp, cmd, data);
2954 	}
2955 
2956 	return (rc);
2957 }
2958 
2959 static int
cxgbe_transmit(struct ifnet * ifp,struct mbuf * m)2960 cxgbe_transmit(struct ifnet *ifp, struct mbuf *m)
2961 {
2962 	struct vi_info *vi = ifp->if_softc;
2963 	struct port_info *pi = vi->pi;
2964 	struct adapter *sc;
2965 	struct sge_txq *txq;
2966 	void *items[1];
2967 	int rc;
2968 
2969 	M_ASSERTPKTHDR(m);
2970 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
2971 #if defined(KERN_TLS) || defined(RATELIMIT)
2972 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2973 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2974 #endif
2975 
2976 	if (__predict_false(pi->link_cfg.link_ok == false)) {
2977 		m_freem(m);
2978 		return (ENETDOWN);
2979 	}
2980 
2981 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
2982 	if (__predict_false(rc != 0)) {
2983 		MPASS(m == NULL);			/* was freed already */
2984 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
2985 		return (rc);
2986 	}
2987 #ifdef RATELIMIT
2988 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
2989 		if (m->m_pkthdr.snd_tag->type == IF_SND_TAG_TYPE_RATE_LIMIT)
2990 			return (ethofld_transmit(ifp, m));
2991 	}
2992 #endif
2993 
2994 	/* Select a txq. */
2995 	sc = vi->adapter;
2996 	txq = &sc->sge.txq[vi->first_txq];
2997 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2998 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
2999 		    vi->rsrv_noflowq);
3000 
3001 	items[0] = m;
3002 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3003 	if (__predict_false(rc != 0))
3004 		m_freem(m);
3005 
3006 	return (rc);
3007 }
3008 
3009 static void
cxgbe_qflush(struct ifnet * ifp)3010 cxgbe_qflush(struct ifnet *ifp)
3011 {
3012 	struct vi_info *vi = ifp->if_softc;
3013 	struct sge_txq *txq;
3014 	int i;
3015 
3016 	/* queues do not exist if !VI_INIT_DONE. */
3017 	if (vi->flags & VI_INIT_DONE) {
3018 		for_each_txq(vi, i, txq) {
3019 			TXQ_LOCK(txq);
3020 			txq->eq.flags |= EQ_QFLUSH;
3021 			TXQ_UNLOCK(txq);
3022 			while (!mp_ring_is_idle(txq->r)) {
3023 				mp_ring_check_drainage(txq->r, 4096);
3024 				pause("qflush", 1);
3025 			}
3026 			TXQ_LOCK(txq);
3027 			txq->eq.flags &= ~EQ_QFLUSH;
3028 			TXQ_UNLOCK(txq);
3029 		}
3030 	}
3031 	if_qflush(ifp);
3032 }
3033 
3034 static uint64_t
vi_get_counter(struct ifnet * ifp,ift_counter c)3035 vi_get_counter(struct ifnet *ifp, ift_counter c)
3036 {
3037 	struct vi_info *vi = ifp->if_softc;
3038 	struct fw_vi_stats_vf *s = &vi->stats;
3039 
3040 	mtx_lock(&vi->tick_mtx);
3041 	vi_refresh_stats(vi);
3042 	mtx_unlock(&vi->tick_mtx);
3043 
3044 	switch (c) {
3045 	case IFCOUNTER_IPACKETS:
3046 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3047 		    s->rx_ucast_frames);
3048 	case IFCOUNTER_IERRORS:
3049 		return (s->rx_err_frames);
3050 	case IFCOUNTER_OPACKETS:
3051 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3052 		    s->tx_ucast_frames + s->tx_offload_frames);
3053 	case IFCOUNTER_OERRORS:
3054 		return (s->tx_drop_frames);
3055 	case IFCOUNTER_IBYTES:
3056 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3057 		    s->rx_ucast_bytes);
3058 	case IFCOUNTER_OBYTES:
3059 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3060 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3061 	case IFCOUNTER_IMCASTS:
3062 		return (s->rx_mcast_frames);
3063 	case IFCOUNTER_OMCASTS:
3064 		return (s->tx_mcast_frames);
3065 	case IFCOUNTER_OQDROPS: {
3066 		uint64_t drops;
3067 
3068 		drops = 0;
3069 		if (vi->flags & VI_INIT_DONE) {
3070 			int i;
3071 			struct sge_txq *txq;
3072 
3073 			for_each_txq(vi, i, txq)
3074 				drops += counter_u64_fetch(txq->r->dropped);
3075 		}
3076 
3077 		return (drops);
3078 
3079 	}
3080 
3081 	default:
3082 		return (if_get_counter_default(ifp, c));
3083 	}
3084 }
3085 
3086 static uint64_t
cxgbe_get_counter(struct ifnet * ifp,ift_counter c)3087 cxgbe_get_counter(struct ifnet *ifp, ift_counter c)
3088 {
3089 	struct vi_info *vi = ifp->if_softc;
3090 	struct port_info *pi = vi->pi;
3091 	struct port_stats *s = &pi->stats;
3092 
3093 	mtx_lock(&vi->tick_mtx);
3094 	cxgbe_refresh_stats(vi);
3095 	mtx_unlock(&vi->tick_mtx);
3096 
3097 	switch (c) {
3098 	case IFCOUNTER_IPACKETS:
3099 		return (s->rx_frames);
3100 
3101 	case IFCOUNTER_IERRORS:
3102 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3103 		    s->rx_fcs_err + s->rx_len_err);
3104 
3105 	case IFCOUNTER_OPACKETS:
3106 		return (s->tx_frames);
3107 
3108 	case IFCOUNTER_OERRORS:
3109 		return (s->tx_error_frames);
3110 
3111 	case IFCOUNTER_IBYTES:
3112 		return (s->rx_octets);
3113 
3114 	case IFCOUNTER_OBYTES:
3115 		return (s->tx_octets);
3116 
3117 	case IFCOUNTER_IMCASTS:
3118 		return (s->rx_mcast_frames);
3119 
3120 	case IFCOUNTER_OMCASTS:
3121 		return (s->tx_mcast_frames);
3122 
3123 	case IFCOUNTER_IQDROPS:
3124 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3125 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3126 		    s->rx_trunc3 + pi->tnl_cong_drops);
3127 
3128 	case IFCOUNTER_OQDROPS: {
3129 		uint64_t drops;
3130 
3131 		drops = s->tx_drop;
3132 		if (vi->flags & VI_INIT_DONE) {
3133 			int i;
3134 			struct sge_txq *txq;
3135 
3136 			for_each_txq(vi, i, txq)
3137 				drops += counter_u64_fetch(txq->r->dropped);
3138 		}
3139 
3140 		return (drops);
3141 
3142 	}
3143 
3144 	default:
3145 		return (if_get_counter_default(ifp, c));
3146 	}
3147 }
3148 
3149 #if defined(KERN_TLS) || defined(RATELIMIT)
3150 static int
cxgbe_snd_tag_alloc(struct ifnet * ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3151 cxgbe_snd_tag_alloc(struct ifnet *ifp, union if_snd_tag_alloc_params *params,
3152     struct m_snd_tag **pt)
3153 {
3154 	int error;
3155 
3156 	switch (params->hdr.type) {
3157 #ifdef RATELIMIT
3158 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3159 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3160 		break;
3161 #endif
3162 #ifdef KERN_TLS
3163 	case IF_SND_TAG_TYPE_TLS:
3164 		error = cxgbe_tls_tag_alloc(ifp, params, pt);
3165 		break;
3166 #endif
3167 	default:
3168 		error = EOPNOTSUPP;
3169 	}
3170 	return (error);
3171 }
3172 
3173 static int
cxgbe_snd_tag_modify(struct m_snd_tag * mst,union if_snd_tag_modify_params * params)3174 cxgbe_snd_tag_modify(struct m_snd_tag *mst,
3175     union if_snd_tag_modify_params *params)
3176 {
3177 
3178 	switch (mst->type) {
3179 #ifdef RATELIMIT
3180 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3181 		return (cxgbe_rate_tag_modify(mst, params));
3182 #endif
3183 	default:
3184 		return (EOPNOTSUPP);
3185 	}
3186 }
3187 
3188 static int
cxgbe_snd_tag_query(struct m_snd_tag * mst,union if_snd_tag_query_params * params)3189 cxgbe_snd_tag_query(struct m_snd_tag *mst,
3190     union if_snd_tag_query_params *params)
3191 {
3192 
3193 	switch (mst->type) {
3194 #ifdef RATELIMIT
3195 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3196 		return (cxgbe_rate_tag_query(mst, params));
3197 #endif
3198 	default:
3199 		return (EOPNOTSUPP);
3200 	}
3201 }
3202 
3203 static void
cxgbe_snd_tag_free(struct m_snd_tag * mst)3204 cxgbe_snd_tag_free(struct m_snd_tag *mst)
3205 {
3206 
3207 	switch (mst->type) {
3208 #ifdef RATELIMIT
3209 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3210 		cxgbe_rate_tag_free(mst);
3211 		return;
3212 #endif
3213 #ifdef KERN_TLS
3214 	case IF_SND_TAG_TYPE_TLS:
3215 		cxgbe_tls_tag_free(mst);
3216 		return;
3217 #endif
3218 	default:
3219 		panic("shouldn't get here");
3220 	}
3221 }
3222 #endif
3223 
3224 /*
3225  * The kernel picks a media from the list we had provided but we still validate
3226  * the requeste.
3227  */
3228 int
cxgbe_media_change(struct ifnet * ifp)3229 cxgbe_media_change(struct ifnet *ifp)
3230 {
3231 	struct vi_info *vi = ifp->if_softc;
3232 	struct port_info *pi = vi->pi;
3233 	struct ifmedia *ifm = &pi->media;
3234 	struct link_config *lc = &pi->link_cfg;
3235 	struct adapter *sc = pi->adapter;
3236 	int rc;
3237 
3238 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3239 	if (rc != 0)
3240 		return (rc);
3241 	PORT_LOCK(pi);
3242 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3243 		/* ifconfig .. media autoselect */
3244 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3245 			rc = ENOTSUP; /* AN not supported by transceiver */
3246 			goto done;
3247 		}
3248 		lc->requested_aneg = AUTONEG_ENABLE;
3249 		lc->requested_speed = 0;
3250 		lc->requested_fc |= PAUSE_AUTONEG;
3251 	} else {
3252 		lc->requested_aneg = AUTONEG_DISABLE;
3253 		lc->requested_speed =
3254 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3255 		lc->requested_fc = 0;
3256 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3257 			lc->requested_fc |= PAUSE_RX;
3258 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3259 			lc->requested_fc |= PAUSE_TX;
3260 	}
3261 	if (pi->up_vis > 0 && !hw_off_limits(sc)) {
3262 		fixup_link_config(pi);
3263 		rc = apply_link_config(pi);
3264 	}
3265 done:
3266 	PORT_UNLOCK(pi);
3267 	end_synchronized_op(sc, 0);
3268 	return (rc);
3269 }
3270 
3271 /*
3272  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3273  * given speed.
3274  */
3275 static int
port_mword(struct port_info * pi,uint32_t speed)3276 port_mword(struct port_info *pi, uint32_t speed)
3277 {
3278 
3279 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3280 	MPASS(powerof2(speed));
3281 
3282 	switch(pi->port_type) {
3283 	case FW_PORT_TYPE_BT_SGMII:
3284 	case FW_PORT_TYPE_BT_XFI:
3285 	case FW_PORT_TYPE_BT_XAUI:
3286 		/* BaseT */
3287 		switch (speed) {
3288 		case FW_PORT_CAP32_SPEED_100M:
3289 			return (IFM_100_T);
3290 		case FW_PORT_CAP32_SPEED_1G:
3291 			return (IFM_1000_T);
3292 		case FW_PORT_CAP32_SPEED_10G:
3293 			return (IFM_10G_T);
3294 		}
3295 		break;
3296 	case FW_PORT_TYPE_KX4:
3297 		if (speed == FW_PORT_CAP32_SPEED_10G)
3298 			return (IFM_10G_KX4);
3299 		break;
3300 	case FW_PORT_TYPE_CX4:
3301 		if (speed == FW_PORT_CAP32_SPEED_10G)
3302 			return (IFM_10G_CX4);
3303 		break;
3304 	case FW_PORT_TYPE_KX:
3305 		if (speed == FW_PORT_CAP32_SPEED_1G)
3306 			return (IFM_1000_KX);
3307 		break;
3308 	case FW_PORT_TYPE_KR:
3309 	case FW_PORT_TYPE_BP_AP:
3310 	case FW_PORT_TYPE_BP4_AP:
3311 	case FW_PORT_TYPE_BP40_BA:
3312 	case FW_PORT_TYPE_KR4_100G:
3313 	case FW_PORT_TYPE_KR_SFP28:
3314 	case FW_PORT_TYPE_KR_XLAUI:
3315 		switch (speed) {
3316 		case FW_PORT_CAP32_SPEED_1G:
3317 			return (IFM_1000_KX);
3318 		case FW_PORT_CAP32_SPEED_10G:
3319 			return (IFM_10G_KR);
3320 		case FW_PORT_CAP32_SPEED_25G:
3321 			return (IFM_25G_KR);
3322 		case FW_PORT_CAP32_SPEED_40G:
3323 			return (IFM_40G_KR4);
3324 		case FW_PORT_CAP32_SPEED_50G:
3325 			return (IFM_50G_KR2);
3326 		case FW_PORT_CAP32_SPEED_100G:
3327 			return (IFM_100G_KR4);
3328 		}
3329 		break;
3330 	case FW_PORT_TYPE_FIBER_XFI:
3331 	case FW_PORT_TYPE_FIBER_XAUI:
3332 	case FW_PORT_TYPE_SFP:
3333 	case FW_PORT_TYPE_QSFP_10G:
3334 	case FW_PORT_TYPE_QSA:
3335 	case FW_PORT_TYPE_QSFP:
3336 	case FW_PORT_TYPE_CR4_QSFP:
3337 	case FW_PORT_TYPE_CR_QSFP:
3338 	case FW_PORT_TYPE_CR2_QSFP:
3339 	case FW_PORT_TYPE_SFP28:
3340 		/* Pluggable transceiver */
3341 		switch (pi->mod_type) {
3342 		case FW_PORT_MOD_TYPE_LR:
3343 			switch (speed) {
3344 			case FW_PORT_CAP32_SPEED_1G:
3345 				return (IFM_1000_LX);
3346 			case FW_PORT_CAP32_SPEED_10G:
3347 				return (IFM_10G_LR);
3348 			case FW_PORT_CAP32_SPEED_25G:
3349 				return (IFM_25G_LR);
3350 			case FW_PORT_CAP32_SPEED_40G:
3351 				return (IFM_40G_LR4);
3352 			case FW_PORT_CAP32_SPEED_50G:
3353 				return (IFM_50G_LR2);
3354 			case FW_PORT_CAP32_SPEED_100G:
3355 				return (IFM_100G_LR4);
3356 			}
3357 			break;
3358 		case FW_PORT_MOD_TYPE_SR:
3359 			switch (speed) {
3360 			case FW_PORT_CAP32_SPEED_1G:
3361 				return (IFM_1000_SX);
3362 			case FW_PORT_CAP32_SPEED_10G:
3363 				return (IFM_10G_SR);
3364 			case FW_PORT_CAP32_SPEED_25G:
3365 				return (IFM_25G_SR);
3366 			case FW_PORT_CAP32_SPEED_40G:
3367 				return (IFM_40G_SR4);
3368 			case FW_PORT_CAP32_SPEED_50G:
3369 				return (IFM_50G_SR2);
3370 			case FW_PORT_CAP32_SPEED_100G:
3371 				return (IFM_100G_SR4);
3372 			}
3373 			break;
3374 		case FW_PORT_MOD_TYPE_ER:
3375 			if (speed == FW_PORT_CAP32_SPEED_10G)
3376 				return (IFM_10G_ER);
3377 			break;
3378 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3379 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3380 			switch (speed) {
3381 			case FW_PORT_CAP32_SPEED_1G:
3382 				return (IFM_1000_CX);
3383 			case FW_PORT_CAP32_SPEED_10G:
3384 				return (IFM_10G_TWINAX);
3385 			case FW_PORT_CAP32_SPEED_25G:
3386 				return (IFM_25G_CR);
3387 			case FW_PORT_CAP32_SPEED_40G:
3388 				return (IFM_40G_CR4);
3389 			case FW_PORT_CAP32_SPEED_50G:
3390 				return (IFM_50G_CR2);
3391 			case FW_PORT_CAP32_SPEED_100G:
3392 				return (IFM_100G_CR4);
3393 			}
3394 			break;
3395 		case FW_PORT_MOD_TYPE_LRM:
3396 			if (speed == FW_PORT_CAP32_SPEED_10G)
3397 				return (IFM_10G_LRM);
3398 			break;
3399 		case FW_PORT_MOD_TYPE_NA:
3400 			MPASS(0);	/* Not pluggable? */
3401 			/* fall throough */
3402 		case FW_PORT_MOD_TYPE_ERROR:
3403 		case FW_PORT_MOD_TYPE_UNKNOWN:
3404 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3405 			break;
3406 		case FW_PORT_MOD_TYPE_NONE:
3407 			return (IFM_NONE);
3408 		}
3409 		break;
3410 	case FW_PORT_TYPE_NONE:
3411 		return (IFM_NONE);
3412 	}
3413 
3414 	return (IFM_UNKNOWN);
3415 }
3416 
3417 void
cxgbe_media_status(struct ifnet * ifp,struct ifmediareq * ifmr)3418 cxgbe_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
3419 {
3420 	struct vi_info *vi = ifp->if_softc;
3421 	struct port_info *pi = vi->pi;
3422 	struct adapter *sc = pi->adapter;
3423 	struct link_config *lc = &pi->link_cfg;
3424 
3425 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4med") != 0)
3426 		return;
3427 	PORT_LOCK(pi);
3428 
3429 	if (pi->up_vis == 0 && !hw_off_limits(sc)) {
3430 		/*
3431 		 * If all the interfaces are administratively down the firmware
3432 		 * does not report transceiver changes.  Refresh port info here
3433 		 * so that ifconfig displays accurate ifmedia at all times.
3434 		 * This is the only reason we have a synchronized op in this
3435 		 * function.  Just PORT_LOCK would have been enough otherwise.
3436 		 */
3437 		t4_update_port_info(pi);
3438 		build_medialist(pi);
3439 	}
3440 
3441 	/* ifm_status */
3442 	ifmr->ifm_status = IFM_AVALID;
3443 	if (lc->link_ok == false)
3444 		goto done;
3445 	ifmr->ifm_status |= IFM_ACTIVE;
3446 
3447 	/* ifm_active */
3448 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3449 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3450 	if (lc->fc & PAUSE_RX)
3451 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3452 	if (lc->fc & PAUSE_TX)
3453 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3454 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3455 done:
3456 	PORT_UNLOCK(pi);
3457 	end_synchronized_op(sc, 0);
3458 }
3459 
3460 static int
vcxgbe_probe(device_t dev)3461 vcxgbe_probe(device_t dev)
3462 {
3463 	char buf[128];
3464 	struct vi_info *vi = device_get_softc(dev);
3465 
3466 	snprintf(buf, sizeof(buf), "port %d vi %td", vi->pi->port_id,
3467 	    vi - vi->pi->vi);
3468 	device_set_desc_copy(dev, buf);
3469 
3470 	return (BUS_PROBE_DEFAULT);
3471 }
3472 
3473 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3474 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3475 {
3476 	int func, index, rc;
3477 	uint32_t param, val;
3478 
3479 	ASSERT_SYNCHRONIZED_OP(sc);
3480 
3481 	index = vi - pi->vi;
3482 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3483 	KASSERT(index < nitems(vi_mac_funcs),
3484 	    ("%s: VI %s doesn't have a MAC func", __func__,
3485 	    device_get_nameunit(vi->dev)));
3486 	func = vi_mac_funcs[index];
3487 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
3488 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3489 	if (rc < 0) {
3490 		CH_ERR(vi, "failed to allocate virtual interface %d"
3491 		    "for port %d: %d\n", index, pi->port_id, -rc);
3492 		return (-rc);
3493 	}
3494 	vi->viid = rc;
3495 
3496 	if (vi->rss_size == 1) {
3497 		/*
3498 		 * This VI didn't get a slice of the RSS table.  Reduce the
3499 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3500 		 * configuration file (nvi, rssnvi for this PF) if this is a
3501 		 * problem.
3502 		 */
3503 		device_printf(vi->dev, "RSS table not available.\n");
3504 		vi->rss_base = 0xffff;
3505 
3506 		return (0);
3507 	}
3508 
3509 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3510 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3511 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3512 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3513 	if (rc)
3514 		vi->rss_base = 0xffff;
3515 	else {
3516 		MPASS((val >> 16) == vi->rss_size);
3517 		vi->rss_base = val & 0xffff;
3518 	}
3519 
3520 	return (0);
3521 }
3522 
3523 static int
vcxgbe_attach(device_t dev)3524 vcxgbe_attach(device_t dev)
3525 {
3526 	struct vi_info *vi;
3527 	struct port_info *pi;
3528 	struct adapter *sc;
3529 	int rc;
3530 
3531 	vi = device_get_softc(dev);
3532 	pi = vi->pi;
3533 	sc = pi->adapter;
3534 
3535 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3536 	if (rc)
3537 		return (rc);
3538 	rc = alloc_extra_vi(sc, pi, vi);
3539 	end_synchronized_op(sc, 0);
3540 	if (rc)
3541 		return (rc);
3542 
3543 	rc = cxgbe_vi_attach(dev, vi);
3544 	if (rc) {
3545 		t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3546 		return (rc);
3547 	}
3548 	return (0);
3549 }
3550 
3551 static int
vcxgbe_detach(device_t dev)3552 vcxgbe_detach(device_t dev)
3553 {
3554 	struct vi_info *vi;
3555 	struct adapter *sc;
3556 
3557 	vi = device_get_softc(dev);
3558 	sc = vi->adapter;
3559 
3560 	doom_vi(sc, vi);
3561 
3562 	cxgbe_vi_detach(vi);
3563 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3564 
3565 	end_synchronized_op(sc, 0);
3566 
3567 	return (0);
3568 }
3569 
3570 static struct callout fatal_callout;
3571 static struct taskqueue *reset_tq;
3572 
3573 static void
delayed_panic(void * arg)3574 delayed_panic(void *arg)
3575 {
3576 	struct adapter *sc = arg;
3577 
3578 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3579 }
3580 
3581 static void
fatal_error_task(void * arg,int pending)3582 fatal_error_task(void *arg, int pending)
3583 {
3584 	struct adapter *sc = arg;
3585 	int rc;
3586 
3587 #ifdef TCP_OFFLOAD
3588 	t4_async_event(sc);
3589 #endif
3590 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3591 		dump_cim_regs(sc);
3592 		dump_cimla(sc);
3593 		dump_devlog(sc);
3594 	}
3595 
3596 	if (t4_reset_on_fatal_err) {
3597 		CH_ALERT(sc, "resetting on fatal error.\n");
3598 		rc = reset_adapter(sc);
3599 		if (rc == 0 && t4_panic_on_fatal_err) {
3600 			CH_ALERT(sc, "reset was successful, "
3601 			    "system will NOT panic.\n");
3602 			return;
3603 		}
3604 	}
3605 
3606 	if (t4_panic_on_fatal_err) {
3607 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3608 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3609 	}
3610 }
3611 
3612 void
t4_fatal_err(struct adapter * sc,bool fw_error)3613 t4_fatal_err(struct adapter *sc, bool fw_error)
3614 {
3615 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3616 
3617 	stop_adapter(sc);
3618 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3619 		return;
3620 	if (fw_error) {
3621 		/*
3622 		 * We are here because of a firmware error/timeout and not
3623 		 * because of a hardware interrupt.  It is possible (although
3624 		 * not very likely) that an error interrupt was also raised but
3625 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3626 		 * main INT_CAUSE registers here to make sure we haven't missed
3627 		 * anything interesting.
3628 		 */
3629 		t4_slow_intr_handler(sc, verbose);
3630 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3631 	}
3632 	t4_report_fw_error(sc);
3633 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3634 	    device_get_nameunit(sc->dev), fw_error);
3635 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3636 }
3637 
3638 void
t4_add_adapter(struct adapter * sc)3639 t4_add_adapter(struct adapter *sc)
3640 {
3641 	sx_xlock(&t4_list_lock);
3642 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3643 	sx_xunlock(&t4_list_lock);
3644 }
3645 
3646 int
t4_map_bars_0_and_4(struct adapter * sc)3647 t4_map_bars_0_and_4(struct adapter *sc)
3648 {
3649 	sc->regs_rid = PCIR_BAR(0);
3650 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3651 	    &sc->regs_rid, RF_ACTIVE);
3652 	if (sc->regs_res == NULL) {
3653 		device_printf(sc->dev, "cannot map registers.\n");
3654 		return (ENXIO);
3655 	}
3656 	sc->bt = rman_get_bustag(sc->regs_res);
3657 	sc->bh = rman_get_bushandle(sc->regs_res);
3658 	sc->mmio_len = rman_get_size(sc->regs_res);
3659 	setbit(&sc->doorbells, DOORBELL_KDB);
3660 
3661 	sc->msix_rid = PCIR_BAR(4);
3662 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3663 	    &sc->msix_rid, RF_ACTIVE);
3664 	if (sc->msix_res == NULL) {
3665 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3666 		return (ENXIO);
3667 	}
3668 
3669 	return (0);
3670 }
3671 
3672 int
t4_map_bar_2(struct adapter * sc)3673 t4_map_bar_2(struct adapter *sc)
3674 {
3675 
3676 	/*
3677 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
3678 	 * to map it if RDMA is disabled.
3679 	 */
3680 	if (is_t4(sc) && sc->rdmacaps == 0)
3681 		return (0);
3682 
3683 	sc->udbs_rid = PCIR_BAR(2);
3684 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3685 	    &sc->udbs_rid, RF_ACTIVE);
3686 	if (sc->udbs_res == NULL) {
3687 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
3688 		return (ENXIO);
3689 	}
3690 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
3691 
3692 	if (chip_id(sc) >= CHELSIO_T5) {
3693 		setbit(&sc->doorbells, DOORBELL_UDB);
3694 #if defined(__i386__) || defined(__amd64__)
3695 		if (t5_write_combine) {
3696 			int rc, mode;
3697 
3698 			/*
3699 			 * Enable write combining on BAR2.  This is the
3700 			 * userspace doorbell BAR and is split into 128B
3701 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
3702 			 * with an egress queue.  The first 64B has the doorbell
3703 			 * and the second 64B can be used to submit a tx work
3704 			 * request with an implicit doorbell.
3705 			 */
3706 
3707 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
3708 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
3709 			if (rc == 0) {
3710 				clrbit(&sc->doorbells, DOORBELL_UDB);
3711 				setbit(&sc->doorbells, DOORBELL_WCWR);
3712 				setbit(&sc->doorbells, DOORBELL_UDBWC);
3713 			} else {
3714 				device_printf(sc->dev,
3715 				    "couldn't enable write combining: %d\n",
3716 				    rc);
3717 			}
3718 
3719 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
3720 			t4_write_reg(sc, A_SGE_STAT_CFG,
3721 			    V_STATSOURCE_T5(7) | mode);
3722 		}
3723 #endif
3724 	}
3725 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
3726 
3727 	return (0);
3728 }
3729 
3730 struct memwin_init {
3731 	uint32_t base;
3732 	uint32_t aperture;
3733 };
3734 
3735 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
3736 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3737 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3738 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
3739 };
3740 
3741 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
3742 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3743 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3744 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
3745 };
3746 
3747 static void
setup_memwin(struct adapter * sc)3748 setup_memwin(struct adapter *sc)
3749 {
3750 	const struct memwin_init *mw_init;
3751 	struct memwin *mw;
3752 	int i;
3753 	uint32_t bar0;
3754 
3755 	if (is_t4(sc)) {
3756 		/*
3757 		 * Read low 32b of bar0 indirectly via the hardware backdoor
3758 		 * mechanism.  Works from within PCI passthrough environments
3759 		 * too, where rman_get_start() can return a different value.  We
3760 		 * need to program the T4 memory window decoders with the actual
3761 		 * addresses that will be coming across the PCIe link.
3762 		 */
3763 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
3764 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
3765 
3766 		mw_init = &t4_memwin[0];
3767 	} else {
3768 		/* T5+ use the relative offset inside the PCIe BAR */
3769 		bar0 = 0;
3770 
3771 		mw_init = &t5_memwin[0];
3772 	}
3773 
3774 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
3775 		if (!rw_initialized(&mw->mw_lock)) {
3776 			rw_init(&mw->mw_lock, "memory window access");
3777 			mw->mw_base = mw_init->base;
3778 			mw->mw_aperture = mw_init->aperture;
3779 			mw->mw_curpos = 0;
3780 		}
3781 		t4_write_reg(sc,
3782 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
3783 		    (mw->mw_base + bar0) | V_BIR(0) |
3784 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
3785 		rw_wlock(&mw->mw_lock);
3786 		position_memwin(sc, i, mw->mw_curpos);
3787 		rw_wunlock(&mw->mw_lock);
3788 	}
3789 
3790 	/* flush */
3791 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
3792 }
3793 
3794 /*
3795  * Positions the memory window at the given address in the card's address space.
3796  * There are some alignment requirements and the actual position may be at an
3797  * address prior to the requested address.  mw->mw_curpos always has the actual
3798  * position of the window.
3799  */
3800 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)3801 position_memwin(struct adapter *sc, int idx, uint32_t addr)
3802 {
3803 	struct memwin *mw;
3804 	uint32_t pf;
3805 	uint32_t reg;
3806 
3807 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3808 	mw = &sc->memwin[idx];
3809 	rw_assert(&mw->mw_lock, RA_WLOCKED);
3810 
3811 	if (is_t4(sc)) {
3812 		pf = 0;
3813 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
3814 	} else {
3815 		pf = V_PFNUM(sc->pf);
3816 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
3817 	}
3818 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
3819 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
3820 	t4_read_reg(sc, reg);	/* flush */
3821 }
3822 
3823 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)3824 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
3825     int len, int rw)
3826 {
3827 	struct memwin *mw;
3828 	uint32_t mw_end, v;
3829 
3830 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3831 
3832 	/* Memory can only be accessed in naturally aligned 4 byte units */
3833 	if (addr & 3 || len & 3 || len <= 0)
3834 		return (EINVAL);
3835 
3836 	mw = &sc->memwin[idx];
3837 	while (len > 0) {
3838 		rw_rlock(&mw->mw_lock);
3839 		mw_end = mw->mw_curpos + mw->mw_aperture;
3840 		if (addr >= mw_end || addr < mw->mw_curpos) {
3841 			/* Will need to reposition the window */
3842 			if (!rw_try_upgrade(&mw->mw_lock)) {
3843 				rw_runlock(&mw->mw_lock);
3844 				rw_wlock(&mw->mw_lock);
3845 			}
3846 			rw_assert(&mw->mw_lock, RA_WLOCKED);
3847 			position_memwin(sc, idx, addr);
3848 			rw_downgrade(&mw->mw_lock);
3849 			mw_end = mw->mw_curpos + mw->mw_aperture;
3850 		}
3851 		rw_assert(&mw->mw_lock, RA_RLOCKED);
3852 		while (addr < mw_end && len > 0) {
3853 			if (rw == 0) {
3854 				v = t4_read_reg(sc, mw->mw_base + addr -
3855 				    mw->mw_curpos);
3856 				*val++ = le32toh(v);
3857 			} else {
3858 				v = *val++;
3859 				t4_write_reg(sc, mw->mw_base + addr -
3860 				    mw->mw_curpos, htole32(v));
3861 			}
3862 			addr += 4;
3863 			len -= 4;
3864 		}
3865 		rw_runlock(&mw->mw_lock);
3866 	}
3867 
3868 	return (0);
3869 }
3870 
3871 static void
t4_init_atid_table(struct adapter * sc)3872 t4_init_atid_table(struct adapter *sc)
3873 {
3874 	struct tid_info *t;
3875 	int i;
3876 
3877 	t = &sc->tids;
3878 	if (t->natids == 0)
3879 		return;
3880 
3881 	MPASS(t->atid_tab == NULL);
3882 
3883 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
3884 	    M_ZERO | M_WAITOK);
3885 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
3886 	t->afree = t->atid_tab;
3887 	t->atids_in_use = 0;
3888 	for (i = 1; i < t->natids; i++)
3889 		t->atid_tab[i - 1].next = &t->atid_tab[i];
3890 	t->atid_tab[t->natids - 1].next = NULL;
3891 }
3892 
3893 static void
t4_free_atid_table(struct adapter * sc)3894 t4_free_atid_table(struct adapter *sc)
3895 {
3896 	struct tid_info *t;
3897 
3898 	t = &sc->tids;
3899 
3900 	KASSERT(t->atids_in_use == 0,
3901 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
3902 
3903 	if (mtx_initialized(&t->atid_lock))
3904 		mtx_destroy(&t->atid_lock);
3905 	free(t->atid_tab, M_CXGBE);
3906 	t->atid_tab = NULL;
3907 }
3908 
3909 int
alloc_atid(struct adapter * sc,void * ctx)3910 alloc_atid(struct adapter *sc, void *ctx)
3911 {
3912 	struct tid_info *t = &sc->tids;
3913 	int atid = -1;
3914 
3915 	mtx_lock(&t->atid_lock);
3916 	if (t->afree) {
3917 		union aopen_entry *p = t->afree;
3918 
3919 		atid = p - t->atid_tab;
3920 		MPASS(atid <= M_TID_TID);
3921 		t->afree = p->next;
3922 		p->data = ctx;
3923 		t->atids_in_use++;
3924 	}
3925 	mtx_unlock(&t->atid_lock);
3926 	return (atid);
3927 }
3928 
3929 void *
lookup_atid(struct adapter * sc,int atid)3930 lookup_atid(struct adapter *sc, int atid)
3931 {
3932 	struct tid_info *t = &sc->tids;
3933 
3934 	return (t->atid_tab[atid].data);
3935 }
3936 
3937 void
free_atid(struct adapter * sc,int atid)3938 free_atid(struct adapter *sc, int atid)
3939 {
3940 	struct tid_info *t = &sc->tids;
3941 	union aopen_entry *p = &t->atid_tab[atid];
3942 
3943 	mtx_lock(&t->atid_lock);
3944 	p->next = t->afree;
3945 	t->afree = p;
3946 	t->atids_in_use--;
3947 	mtx_unlock(&t->atid_lock);
3948 }
3949 
3950 static void
queue_tid_release(struct adapter * sc,int tid)3951 queue_tid_release(struct adapter *sc, int tid)
3952 {
3953 
3954 	CXGBE_UNIMPLEMENTED("deferred tid release");
3955 }
3956 
3957 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)3958 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
3959 {
3960 	struct wrqe *wr;
3961 	struct cpl_tid_release *req;
3962 
3963 	wr = alloc_wrqe(sizeof(*req), ctrlq);
3964 	if (wr == NULL) {
3965 		queue_tid_release(sc, tid);	/* defer */
3966 		return;
3967 	}
3968 	req = wrtod(wr);
3969 
3970 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
3971 
3972 	t4_wrq_tx(sc, wr);
3973 }
3974 
3975 static int
t4_range_cmp(const void * a,const void * b)3976 t4_range_cmp(const void *a, const void *b)
3977 {
3978 	return ((const struct t4_range *)a)->start -
3979 	       ((const struct t4_range *)b)->start;
3980 }
3981 
3982 /*
3983  * Verify that the memory range specified by the addr/len pair is valid within
3984  * the card's address space.
3985  */
3986 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)3987 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
3988 {
3989 	struct t4_range mem_ranges[4], *r, *next;
3990 	uint32_t em, addr_len;
3991 	int i, n, remaining;
3992 
3993 	/* Memory can only be accessed in naturally aligned 4 byte units */
3994 	if (addr & 3 || len & 3 || len == 0)
3995 		return (EINVAL);
3996 
3997 	/* Enabled memories */
3998 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
3999 
4000 	r = &mem_ranges[0];
4001 	n = 0;
4002 	bzero(r, sizeof(mem_ranges));
4003 	if (em & F_EDRAM0_ENABLE) {
4004 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4005 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4006 		if (r->size > 0) {
4007 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4008 			if (addr >= r->start &&
4009 			    addr + len <= r->start + r->size)
4010 				return (0);
4011 			r++;
4012 			n++;
4013 		}
4014 	}
4015 	if (em & F_EDRAM1_ENABLE) {
4016 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4017 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4018 		if (r->size > 0) {
4019 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4020 			if (addr >= r->start &&
4021 			    addr + len <= r->start + r->size)
4022 				return (0);
4023 			r++;
4024 			n++;
4025 		}
4026 	}
4027 	if (em & F_EXT_MEM_ENABLE) {
4028 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4029 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4030 		if (r->size > 0) {
4031 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4032 			if (addr >= r->start &&
4033 			    addr + len <= r->start + r->size)
4034 				return (0);
4035 			r++;
4036 			n++;
4037 		}
4038 	}
4039 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4040 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4041 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4042 		if (r->size > 0) {
4043 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4044 			if (addr >= r->start &&
4045 			    addr + len <= r->start + r->size)
4046 				return (0);
4047 			r++;
4048 			n++;
4049 		}
4050 	}
4051 	MPASS(n <= nitems(mem_ranges));
4052 
4053 	if (n > 1) {
4054 		/* Sort and merge the ranges. */
4055 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4056 
4057 		/* Start from index 0 and examine the next n - 1 entries. */
4058 		r = &mem_ranges[0];
4059 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4060 
4061 			MPASS(r->size > 0);	/* r is a valid entry. */
4062 			next = r + 1;
4063 			MPASS(next->size > 0);	/* and so is the next one. */
4064 
4065 			while (r->start + r->size >= next->start) {
4066 				/* Merge the next one into the current entry. */
4067 				r->size = max(r->start + r->size,
4068 				    next->start + next->size) - r->start;
4069 				n--;	/* One fewer entry in total. */
4070 				if (--remaining == 0)
4071 					goto done;	/* short circuit */
4072 				next++;
4073 			}
4074 			if (next != r + 1) {
4075 				/*
4076 				 * Some entries were merged into r and next
4077 				 * points to the first valid entry that couldn't
4078 				 * be merged.
4079 				 */
4080 				MPASS(next->size > 0);	/* must be valid */
4081 				memcpy(r + 1, next, remaining * sizeof(*r));
4082 #ifdef INVARIANTS
4083 				/*
4084 				 * This so that the foo->size assertion in the
4085 				 * next iteration of the loop do the right
4086 				 * thing for entries that were pulled up and are
4087 				 * no longer valid.
4088 				 */
4089 				MPASS(n < nitems(mem_ranges));
4090 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4091 				    sizeof(struct t4_range));
4092 #endif
4093 			}
4094 		}
4095 done:
4096 		/* Done merging the ranges. */
4097 		MPASS(n > 0);
4098 		r = &mem_ranges[0];
4099 		for (i = 0; i < n; i++, r++) {
4100 			if (addr >= r->start &&
4101 			    addr + len <= r->start + r->size)
4102 				return (0);
4103 		}
4104 	}
4105 
4106 	return (EFAULT);
4107 }
4108 
4109 static int
fwmtype_to_hwmtype(int mtype)4110 fwmtype_to_hwmtype(int mtype)
4111 {
4112 
4113 	switch (mtype) {
4114 	case FW_MEMTYPE_EDC0:
4115 		return (MEM_EDC0);
4116 	case FW_MEMTYPE_EDC1:
4117 		return (MEM_EDC1);
4118 	case FW_MEMTYPE_EXTMEM:
4119 		return (MEM_MC0);
4120 	case FW_MEMTYPE_EXTMEM1:
4121 		return (MEM_MC1);
4122 	default:
4123 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4124 	}
4125 }
4126 
4127 /*
4128  * Verify that the memory range specified by the memtype/offset/len pair is
4129  * valid and lies entirely within the memtype specified.  The global address of
4130  * the start of the range is returned in addr.
4131  */
4132 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4133 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4134     uint32_t *addr)
4135 {
4136 	uint32_t em, addr_len, maddr;
4137 
4138 	/* Memory can only be accessed in naturally aligned 4 byte units */
4139 	if (off & 3 || len & 3 || len == 0)
4140 		return (EINVAL);
4141 
4142 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4143 	switch (fwmtype_to_hwmtype(mtype)) {
4144 	case MEM_EDC0:
4145 		if (!(em & F_EDRAM0_ENABLE))
4146 			return (EINVAL);
4147 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4148 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4149 		break;
4150 	case MEM_EDC1:
4151 		if (!(em & F_EDRAM1_ENABLE))
4152 			return (EINVAL);
4153 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4154 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4155 		break;
4156 	case MEM_MC:
4157 		if (!(em & F_EXT_MEM_ENABLE))
4158 			return (EINVAL);
4159 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4160 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4161 		break;
4162 	case MEM_MC1:
4163 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4164 			return (EINVAL);
4165 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4166 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4167 		break;
4168 	default:
4169 		return (EINVAL);
4170 	}
4171 
4172 	*addr = maddr + off;	/* global address */
4173 	return (validate_mem_range(sc, *addr, len));
4174 }
4175 
4176 static int
fixup_devlog_params(struct adapter * sc)4177 fixup_devlog_params(struct adapter *sc)
4178 {
4179 	struct devlog_params *dparams = &sc->params.devlog;
4180 	int rc;
4181 
4182 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4183 	    dparams->size, &dparams->addr);
4184 
4185 	return (rc);
4186 }
4187 
4188 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4189 update_nirq(struct intrs_and_queues *iaq, int nports)
4190 {
4191 
4192 	iaq->nirq = T4_EXTRA_INTR;
4193 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4194 	iaq->nirq += nports * iaq->nofldrxq;
4195 	iaq->nirq += nports * (iaq->num_vis - 1) *
4196 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4197 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4198 }
4199 
4200 /*
4201  * Adjust requirements to fit the number of interrupts available.
4202  */
4203 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4204 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4205     int navail)
4206 {
4207 	int old_nirq;
4208 	const int nports = sc->params.nports;
4209 
4210 	MPASS(nports > 0);
4211 	MPASS(navail > 0);
4212 
4213 	bzero(iaq, sizeof(*iaq));
4214 	iaq->intr_type = itype;
4215 	iaq->num_vis = t4_num_vis;
4216 	iaq->ntxq = t4_ntxq;
4217 	iaq->ntxq_vi = t4_ntxq_vi;
4218 	iaq->nrxq = t4_nrxq;
4219 	iaq->nrxq_vi = t4_nrxq_vi;
4220 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4221 	if (is_offload(sc) || is_ethoffload(sc)) {
4222 		iaq->nofldtxq = t4_nofldtxq;
4223 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
4224 	}
4225 #endif
4226 #ifdef TCP_OFFLOAD
4227 	if (is_offload(sc)) {
4228 		iaq->nofldrxq = t4_nofldrxq;
4229 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4230 	}
4231 #endif
4232 #ifdef DEV_NETMAP
4233 	if (t4_native_netmap & NN_MAIN_VI) {
4234 		iaq->nnmtxq = t4_nnmtxq;
4235 		iaq->nnmrxq = t4_nnmrxq;
4236 	}
4237 	if (t4_native_netmap & NN_EXTRA_VI) {
4238 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4239 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4240 	}
4241 #endif
4242 
4243 	update_nirq(iaq, nports);
4244 	if (iaq->nirq <= navail &&
4245 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4246 		/*
4247 		 * This is the normal case -- there are enough interrupts for
4248 		 * everything.
4249 		 */
4250 		goto done;
4251 	}
4252 
4253 	/*
4254 	 * If extra VIs have been configured try reducing their count and see if
4255 	 * that works.
4256 	 */
4257 	while (iaq->num_vis > 1) {
4258 		iaq->num_vis--;
4259 		update_nirq(iaq, nports);
4260 		if (iaq->nirq <= navail &&
4261 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4262 			device_printf(sc->dev, "virtual interfaces per port "
4263 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4264 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4265 			    "itype %d, navail %u, nirq %d.\n",
4266 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4267 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4268 			    itype, navail, iaq->nirq);
4269 			goto done;
4270 		}
4271 	}
4272 
4273 	/*
4274 	 * Extra VIs will not be created.  Log a message if they were requested.
4275 	 */
4276 	MPASS(iaq->num_vis == 1);
4277 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4278 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4279 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4280 	if (iaq->num_vis != t4_num_vis) {
4281 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4282 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4283 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4284 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4285 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4286 	}
4287 
4288 	/*
4289 	 * Keep reducing the number of NIC rx queues to the next lower power of
4290 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4291 	 * if that works.
4292 	 */
4293 	do {
4294 		if (iaq->nrxq > 1) {
4295 			do {
4296 				iaq->nrxq--;
4297 			} while (!powerof2(iaq->nrxq));
4298 			if (iaq->nnmrxq > iaq->nrxq)
4299 				iaq->nnmrxq = iaq->nrxq;
4300 		}
4301 		if (iaq->nofldrxq > 1)
4302 			iaq->nofldrxq >>= 1;
4303 
4304 		old_nirq = iaq->nirq;
4305 		update_nirq(iaq, nports);
4306 		if (iaq->nirq <= navail &&
4307 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4308 			device_printf(sc->dev, "running with reduced number of "
4309 			    "rx queues because of shortage of interrupts.  "
4310 			    "nrxq=%u, nofldrxq=%u.  "
4311 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4312 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4313 			goto done;
4314 		}
4315 	} while (old_nirq != iaq->nirq);
4316 
4317 	/* One interrupt for everything.  Ugh. */
4318 	device_printf(sc->dev, "running with minimal number of queues.  "
4319 	    "itype %d, navail %u.\n", itype, navail);
4320 	iaq->nirq = 1;
4321 	iaq->nrxq = 1;
4322 	iaq->ntxq = 1;
4323 	if (iaq->nofldrxq > 0) {
4324 		iaq->nofldrxq = 1;
4325 		iaq->nofldtxq = 1;
4326 	}
4327 	iaq->nnmtxq = 0;
4328 	iaq->nnmrxq = 0;
4329 done:
4330 	MPASS(iaq->num_vis > 0);
4331 	if (iaq->num_vis > 1) {
4332 		MPASS(iaq->nrxq_vi > 0);
4333 		MPASS(iaq->ntxq_vi > 0);
4334 	}
4335 	MPASS(iaq->nirq > 0);
4336 	MPASS(iaq->nrxq > 0);
4337 	MPASS(iaq->ntxq > 0);
4338 	if (itype == INTR_MSI) {
4339 		MPASS(powerof2(iaq->nirq));
4340 	}
4341 }
4342 
4343 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4344 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4345 {
4346 	int rc, itype, navail, nalloc;
4347 
4348 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4349 
4350 		if ((itype & t4_intr_types) == 0)
4351 			continue;	/* not allowed */
4352 
4353 		if (itype == INTR_MSIX)
4354 			navail = pci_msix_count(sc->dev);
4355 		else if (itype == INTR_MSI)
4356 			navail = pci_msi_count(sc->dev);
4357 		else
4358 			navail = 1;
4359 restart:
4360 		if (navail == 0)
4361 			continue;
4362 
4363 		calculate_iaq(sc, iaq, itype, navail);
4364 		nalloc = iaq->nirq;
4365 		rc = 0;
4366 		if (itype == INTR_MSIX)
4367 			rc = pci_alloc_msix(sc->dev, &nalloc);
4368 		else if (itype == INTR_MSI)
4369 			rc = pci_alloc_msi(sc->dev, &nalloc);
4370 
4371 		if (rc == 0 && nalloc > 0) {
4372 			if (nalloc == iaq->nirq)
4373 				return (0);
4374 
4375 			/*
4376 			 * Didn't get the number requested.  Use whatever number
4377 			 * the kernel is willing to allocate.
4378 			 */
4379 			device_printf(sc->dev, "fewer vectors than requested, "
4380 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4381 			    itype, iaq->nirq, nalloc);
4382 			pci_release_msi(sc->dev);
4383 			navail = nalloc;
4384 			goto restart;
4385 		}
4386 
4387 		device_printf(sc->dev,
4388 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4389 		    itype, rc, iaq->nirq, nalloc);
4390 	}
4391 
4392 	device_printf(sc->dev,
4393 	    "failed to find a usable interrupt type.  "
4394 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4395 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4396 
4397 	return (ENXIO);
4398 }
4399 
4400 #define FW_VERSION(chip) ( \
4401     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4402     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4403     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4404     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4405 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4406 
4407 /* Just enough of fw_hdr to cover all version info. */
4408 struct fw_h {
4409 	__u8	ver;
4410 	__u8	chip;
4411 	__be16	len512;
4412 	__be32	fw_ver;
4413 	__be32	tp_microcode_ver;
4414 	__u8	intfver_nic;
4415 	__u8	intfver_vnic;
4416 	__u8	intfver_ofld;
4417 	__u8	intfver_ri;
4418 	__u8	intfver_iscsipdu;
4419 	__u8	intfver_iscsi;
4420 	__u8	intfver_fcoepdu;
4421 	__u8	intfver_fcoe;
4422 };
4423 /* Spot check a couple of fields. */
4424 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4425 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4426 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4427 
4428 struct fw_info {
4429 	uint8_t chip;
4430 	char *kld_name;
4431 	char *fw_mod_name;
4432 	struct fw_h fw_h;
4433 } fw_info[] = {
4434 	{
4435 		.chip = CHELSIO_T4,
4436 		.kld_name = "t4fw_cfg",
4437 		.fw_mod_name = "t4fw",
4438 		.fw_h = {
4439 			.chip = FW_HDR_CHIP_T4,
4440 			.fw_ver = htobe32(FW_VERSION(T4)),
4441 			.intfver_nic = FW_INTFVER(T4, NIC),
4442 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4443 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4444 			.intfver_ri = FW_INTFVER(T4, RI),
4445 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4446 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4447 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4448 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4449 		},
4450 	}, {
4451 		.chip = CHELSIO_T5,
4452 		.kld_name = "t5fw_cfg",
4453 		.fw_mod_name = "t5fw",
4454 		.fw_h = {
4455 			.chip = FW_HDR_CHIP_T5,
4456 			.fw_ver = htobe32(FW_VERSION(T5)),
4457 			.intfver_nic = FW_INTFVER(T5, NIC),
4458 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4459 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4460 			.intfver_ri = FW_INTFVER(T5, RI),
4461 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4462 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4463 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4464 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4465 		},
4466 	}, {
4467 		.chip = CHELSIO_T6,
4468 		.kld_name = "t6fw_cfg",
4469 		.fw_mod_name = "t6fw",
4470 		.fw_h = {
4471 			.chip = FW_HDR_CHIP_T6,
4472 			.fw_ver = htobe32(FW_VERSION(T6)),
4473 			.intfver_nic = FW_INTFVER(T6, NIC),
4474 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4475 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4476 			.intfver_ri = FW_INTFVER(T6, RI),
4477 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4478 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4479 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4480 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4481 		},
4482 	}
4483 };
4484 
4485 static struct fw_info *
find_fw_info(int chip)4486 find_fw_info(int chip)
4487 {
4488 	int i;
4489 
4490 	for (i = 0; i < nitems(fw_info); i++) {
4491 		if (fw_info[i].chip == chip)
4492 			return (&fw_info[i]);
4493 	}
4494 	return (NULL);
4495 }
4496 
4497 /*
4498  * Is the given firmware API compatible with the one the driver was compiled
4499  * with?
4500  */
4501 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4502 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4503 {
4504 
4505 	/* short circuit if it's the exact same firmware version */
4506 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4507 		return (1);
4508 
4509 	/*
4510 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4511 	 * features that are supported in the driver.
4512 	 */
4513 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4514 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4515 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4516 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4517 		return (1);
4518 #undef SAME_INTF
4519 
4520 	return (0);
4521 }
4522 
4523 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4524 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4525     const struct firmware **fw)
4526 {
4527 	struct fw_info *fw_info;
4528 
4529 	*dcfg = NULL;
4530 	if (fw != NULL)
4531 		*fw = NULL;
4532 
4533 	fw_info = find_fw_info(chip_id(sc));
4534 	if (fw_info == NULL) {
4535 		device_printf(sc->dev,
4536 		    "unable to look up firmware information for chip %d.\n",
4537 		    chip_id(sc));
4538 		return (EINVAL);
4539 	}
4540 
4541 	*dcfg = firmware_get(fw_info->kld_name);
4542 	if (*dcfg != NULL) {
4543 		if (fw != NULL)
4544 			*fw = firmware_get(fw_info->fw_mod_name);
4545 		return (0);
4546 	}
4547 
4548 	return (ENOENT);
4549 }
4550 
4551 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4552 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4553     const struct firmware *fw)
4554 {
4555 
4556 	if (fw != NULL)
4557 		firmware_put(fw, FIRMWARE_UNLOAD);
4558 	if (dcfg != NULL)
4559 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4560 }
4561 
4562 /*
4563  * Return values:
4564  * 0 means no firmware install attempted.
4565  * ERESTART means a firmware install was attempted and was successful.
4566  * +ve errno means a firmware install was attempted but failed.
4567  */
4568 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4569 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4570     const struct fw_h *drv_fw, const char *reason, int *already)
4571 {
4572 	const struct firmware *cfg, *fw;
4573 	const uint32_t c = be32toh(card_fw->fw_ver);
4574 	uint32_t d, k;
4575 	int rc, fw_install;
4576 	struct fw_h bundled_fw;
4577 	bool load_attempted;
4578 
4579 	cfg = fw = NULL;
4580 	load_attempted = false;
4581 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4582 
4583 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4584 	if (t4_fw_install < 0) {
4585 		rc = load_fw_module(sc, &cfg, &fw);
4586 		if (rc != 0 || fw == NULL) {
4587 			device_printf(sc->dev,
4588 			    "failed to load firmware module: %d. cfg %p, fw %p;"
4589 			    " will use compiled-in firmware version for"
4590 			    "hw.cxgbe.fw_install checks.\n",
4591 			    rc, cfg, fw);
4592 		} else {
4593 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
4594 		}
4595 		load_attempted = true;
4596 	}
4597 	d = be32toh(bundled_fw.fw_ver);
4598 
4599 	if (reason != NULL)
4600 		goto install;
4601 
4602 	if ((sc->flags & FW_OK) == 0) {
4603 
4604 		if (c == 0xffffffff) {
4605 			reason = "missing";
4606 			goto install;
4607 		}
4608 
4609 		rc = 0;
4610 		goto done;
4611 	}
4612 
4613 	if (!fw_compatible(card_fw, &bundled_fw)) {
4614 		reason = "incompatible or unusable";
4615 		goto install;
4616 	}
4617 
4618 	if (d > c) {
4619 		reason = "older than the version bundled with this driver";
4620 		goto install;
4621 	}
4622 
4623 	if (fw_install == 2 && d != c) {
4624 		reason = "different than the version bundled with this driver";
4625 		goto install;
4626 	}
4627 
4628 	/* No reason to do anything to the firmware already on the card. */
4629 	rc = 0;
4630 	goto done;
4631 
4632 install:
4633 	rc = 0;
4634 	if ((*already)++)
4635 		goto done;
4636 
4637 	if (fw_install == 0) {
4638 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4639 		    "but the driver is prohibited from installing a firmware "
4640 		    "on the card.\n",
4641 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4642 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4643 
4644 		goto done;
4645 	}
4646 
4647 	/*
4648 	 * We'll attempt to install a firmware.  Load the module first (if it
4649 	 * hasn't been loaded already).
4650 	 */
4651 	if (!load_attempted) {
4652 		rc = load_fw_module(sc, &cfg, &fw);
4653 		if (rc != 0 || fw == NULL) {
4654 			device_printf(sc->dev,
4655 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
4656 			    rc, cfg, fw);
4657 			/* carry on */
4658 		}
4659 	}
4660 	if (fw == NULL) {
4661 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4662 		    "but the driver cannot take corrective action because it "
4663 		    "is unable to load the firmware module.\n",
4664 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4665 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4666 		rc = sc->flags & FW_OK ? 0 : ENOENT;
4667 		goto done;
4668 	}
4669 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
4670 	if (k != d) {
4671 		MPASS(t4_fw_install > 0);
4672 		device_printf(sc->dev,
4673 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
4674 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
4675 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
4676 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
4677 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4678 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4679 		rc = sc->flags & FW_OK ? 0 : EINVAL;
4680 		goto done;
4681 	}
4682 
4683 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4684 	    "installing firmware %u.%u.%u.%u on card.\n",
4685 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4686 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
4687 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4688 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4689 
4690 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
4691 	if (rc != 0) {
4692 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
4693 	} else {
4694 		/* Installed successfully, update the cached header too. */
4695 		rc = ERESTART;
4696 		memcpy(card_fw, fw->data, sizeof(*card_fw));
4697 	}
4698 done:
4699 	unload_fw_module(sc, cfg, fw);
4700 
4701 	return (rc);
4702 }
4703 
4704 /*
4705  * Establish contact with the firmware and attempt to become the master driver.
4706  *
4707  * A firmware will be installed to the card if needed (if the driver is allowed
4708  * to do so).
4709  */
4710 static int
contact_firmware(struct adapter * sc)4711 contact_firmware(struct adapter *sc)
4712 {
4713 	int rc, already = 0;
4714 	enum dev_state state;
4715 	struct fw_info *fw_info;
4716 	struct fw_hdr *card_fw;		/* fw on the card */
4717 	const struct fw_h *drv_fw;
4718 
4719 	fw_info = find_fw_info(chip_id(sc));
4720 	if (fw_info == NULL) {
4721 		device_printf(sc->dev,
4722 		    "unable to look up firmware information for chip %d.\n",
4723 		    chip_id(sc));
4724 		return (EINVAL);
4725 	}
4726 	drv_fw = &fw_info->fw_h;
4727 
4728 	/* Read the header of the firmware on the card */
4729 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
4730 restart:
4731 	rc = -t4_get_fw_hdr(sc, card_fw);
4732 	if (rc != 0) {
4733 		device_printf(sc->dev,
4734 		    "unable to read firmware header from card's flash: %d\n",
4735 		    rc);
4736 		goto done;
4737 	}
4738 
4739 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
4740 	    &already);
4741 	if (rc == ERESTART)
4742 		goto restart;
4743 	if (rc != 0)
4744 		goto done;
4745 
4746 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
4747 	if (rc < 0 || state == DEV_STATE_ERR) {
4748 		rc = -rc;
4749 		device_printf(sc->dev,
4750 		    "failed to connect to the firmware: %d, %d.  "
4751 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4752 #if 0
4753 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4754 		    "not responding properly to HELLO", &already) == ERESTART)
4755 			goto restart;
4756 #endif
4757 		goto done;
4758 	}
4759 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
4760 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
4761 
4762 	if (rc == sc->pf) {
4763 		sc->flags |= MASTER_PF;
4764 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4765 		    NULL, &already);
4766 		if (rc == ERESTART)
4767 			rc = 0;
4768 		else if (rc != 0)
4769 			goto done;
4770 	} else if (state == DEV_STATE_UNINIT) {
4771 		/*
4772 		 * We didn't get to be the master so we definitely won't be
4773 		 * configuring the chip.  It's a bug if someone else hasn't
4774 		 * configured it already.
4775 		 */
4776 		device_printf(sc->dev, "couldn't be master(%d), "
4777 		    "device not already initialized either(%d).  "
4778 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4779 		rc = EPROTO;
4780 		goto done;
4781 	} else {
4782 		/*
4783 		 * Some other PF is the master and has configured the chip.
4784 		 * This is allowed but untested.
4785 		 */
4786 		device_printf(sc->dev, "PF%d is master, device state %d.  "
4787 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4788 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
4789 		sc->cfcsum = 0;
4790 		rc = 0;
4791 	}
4792 done:
4793 	if (rc != 0 && sc->flags & FW_OK) {
4794 		t4_fw_bye(sc, sc->mbox);
4795 		sc->flags &= ~FW_OK;
4796 	}
4797 	free(card_fw, M_CXGBE);
4798 	return (rc);
4799 }
4800 
4801 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff)4802 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
4803     uint32_t mtype, uint32_t moff)
4804 {
4805 	struct fw_info *fw_info;
4806 	const struct firmware *dcfg, *rcfg = NULL;
4807 	const uint32_t *cfdata;
4808 	uint32_t cflen, addr;
4809 	int rc;
4810 
4811 	load_fw_module(sc, &dcfg, NULL);
4812 
4813 	/* Card specific interpretation of "default". */
4814 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4815 		if (pci_get_device(sc->dev) == 0x440a)
4816 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
4817 		if (is_fpga(sc))
4818 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
4819 	}
4820 
4821 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4822 		if (dcfg == NULL) {
4823 			device_printf(sc->dev,
4824 			    "KLD with default config is not available.\n");
4825 			rc = ENOENT;
4826 			goto done;
4827 		}
4828 		cfdata = dcfg->data;
4829 		cflen = dcfg->datasize & ~3;
4830 	} else {
4831 		char s[32];
4832 
4833 		fw_info = find_fw_info(chip_id(sc));
4834 		if (fw_info == NULL) {
4835 			device_printf(sc->dev,
4836 			    "unable to look up firmware information for chip %d.\n",
4837 			    chip_id(sc));
4838 			rc = EINVAL;
4839 			goto done;
4840 		}
4841 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
4842 
4843 		rcfg = firmware_get(s);
4844 		if (rcfg == NULL) {
4845 			device_printf(sc->dev,
4846 			    "unable to load module \"%s\" for configuration "
4847 			    "profile \"%s\".\n", s, cfg_file);
4848 			rc = ENOENT;
4849 			goto done;
4850 		}
4851 		cfdata = rcfg->data;
4852 		cflen = rcfg->datasize & ~3;
4853 	}
4854 
4855 	if (cflen > FLASH_CFG_MAX_SIZE) {
4856 		device_printf(sc->dev,
4857 		    "config file too long (%d, max allowed is %d).\n",
4858 		    cflen, FLASH_CFG_MAX_SIZE);
4859 		rc = EINVAL;
4860 		goto done;
4861 	}
4862 
4863 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
4864 	if (rc != 0) {
4865 		device_printf(sc->dev,
4866 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
4867 		    __func__, mtype, moff, cflen, rc);
4868 		rc = EINVAL;
4869 		goto done;
4870 	}
4871 	write_via_memwin(sc, 2, addr, cfdata, cflen);
4872 done:
4873 	if (rcfg != NULL)
4874 		firmware_put(rcfg, FIRMWARE_UNLOAD);
4875 	unload_fw_module(sc, dcfg, NULL);
4876 	return (rc);
4877 }
4878 
4879 struct caps_allowed {
4880 	uint16_t nbmcaps;
4881 	uint16_t linkcaps;
4882 	uint16_t switchcaps;
4883 	uint16_t niccaps;
4884 	uint16_t toecaps;
4885 	uint16_t rdmacaps;
4886 	uint16_t cryptocaps;
4887 	uint16_t iscsicaps;
4888 	uint16_t fcoecaps;
4889 };
4890 
4891 #define FW_PARAM_DEV(param) \
4892 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
4893 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
4894 #define FW_PARAM_PFVF(param) \
4895 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
4896 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
4897 
4898 /*
4899  * Provide a configuration profile to the firmware and have it initialize the
4900  * chip accordingly.  This may involve uploading a configuration file to the
4901  * card.
4902  */
4903 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)4904 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
4905     const struct caps_allowed *caps_allowed)
4906 {
4907 	int rc;
4908 	struct fw_caps_config_cmd caps;
4909 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
4910 
4911 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
4912 	if (rc != 0) {
4913 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
4914 		return (rc);
4915 	}
4916 
4917 	bzero(&caps, sizeof(caps));
4918 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
4919 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
4920 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
4921 		mtype = 0;
4922 		moff = 0;
4923 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
4924 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
4925 		mtype = FW_MEMTYPE_FLASH;
4926 		moff = t4_flash_cfg_addr(sc);
4927 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
4928 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
4929 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
4930 		    FW_LEN16(caps));
4931 	} else {
4932 		/*
4933 		 * Ask the firmware where it wants us to upload the config file.
4934 		 */
4935 		param = FW_PARAM_DEV(CF);
4936 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
4937 		if (rc != 0) {
4938 			/* No support for config file?  Shouldn't happen. */
4939 			device_printf(sc->dev,
4940 			    "failed to query config file location: %d.\n", rc);
4941 			goto done;
4942 		}
4943 		mtype = G_FW_PARAMS_PARAM_Y(val);
4944 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
4945 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
4946 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
4947 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
4948 		    FW_LEN16(caps));
4949 
4950 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
4951 		if (rc != 0) {
4952 			device_printf(sc->dev,
4953 			    "failed to upload config file to card: %d.\n", rc);
4954 			goto done;
4955 		}
4956 	}
4957 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
4958 	if (rc != 0) {
4959 		device_printf(sc->dev, "failed to pre-process config file: %d "
4960 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
4961 		goto done;
4962 	}
4963 
4964 	finicsum = be32toh(caps.finicsum);
4965 	cfcsum = be32toh(caps.cfcsum);	/* actual */
4966 	if (finicsum != cfcsum) {
4967 		device_printf(sc->dev,
4968 		    "WARNING: config file checksum mismatch: %08x %08x\n",
4969 		    finicsum, cfcsum);
4970 	}
4971 	sc->cfcsum = cfcsum;
4972 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
4973 
4974 	/*
4975 	 * Let the firmware know what features will (not) be used so it can tune
4976 	 * things accordingly.
4977 	 */
4978 #define LIMIT_CAPS(x) do { \
4979 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
4980 } while (0)
4981 	LIMIT_CAPS(nbm);
4982 	LIMIT_CAPS(link);
4983 	LIMIT_CAPS(switch);
4984 	LIMIT_CAPS(nic);
4985 	LIMIT_CAPS(toe);
4986 	LIMIT_CAPS(rdma);
4987 	LIMIT_CAPS(crypto);
4988 	LIMIT_CAPS(iscsi);
4989 	LIMIT_CAPS(fcoe);
4990 #undef LIMIT_CAPS
4991 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
4992 		/*
4993 		 * TOE and hashfilters are mutually exclusive.  It is a config
4994 		 * file or firmware bug if both are reported as available.  Try
4995 		 * to cope with the situation in non-debug builds by disabling
4996 		 * TOE.
4997 		 */
4998 		MPASS(caps.toecaps == 0);
4999 
5000 		caps.toecaps = 0;
5001 		caps.rdmacaps = 0;
5002 		caps.iscsicaps = 0;
5003 	}
5004 
5005 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5006 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5007 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5008 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5009 	if (rc != 0) {
5010 		device_printf(sc->dev,
5011 		    "failed to process config file: %d.\n", rc);
5012 		goto done;
5013 	}
5014 
5015 	t4_tweak_chip_settings(sc);
5016 	set_params__pre_init(sc);
5017 
5018 	/* get basic stuff going */
5019 	rc = -t4_fw_initialize(sc, sc->mbox);
5020 	if (rc != 0) {
5021 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5022 		goto done;
5023 	}
5024 done:
5025 	return (rc);
5026 }
5027 
5028 /*
5029  * Partition chip resources for use between various PFs, VFs, etc.
5030  */
5031 static int
partition_resources(struct adapter * sc)5032 partition_resources(struct adapter *sc)
5033 {
5034 	char cfg_file[sizeof(t4_cfg_file)];
5035 	struct caps_allowed caps_allowed;
5036 	int rc;
5037 	bool fallback;
5038 
5039 	/* Only the master driver gets to configure the chip resources. */
5040 	MPASS(sc->flags & MASTER_PF);
5041 
5042 #define COPY_CAPS(x) do { \
5043 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5044 } while (0)
5045 	bzero(&caps_allowed, sizeof(caps_allowed));
5046 	COPY_CAPS(nbm);
5047 	COPY_CAPS(link);
5048 	COPY_CAPS(switch);
5049 	COPY_CAPS(nic);
5050 	COPY_CAPS(toe);
5051 	COPY_CAPS(rdma);
5052 	COPY_CAPS(crypto);
5053 	COPY_CAPS(iscsi);
5054 	COPY_CAPS(fcoe);
5055 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5056 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5057 retry:
5058 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5059 	if (rc != 0 && fallback) {
5060 		device_printf(sc->dev,
5061 		    "failed (%d) to configure card with \"%s\" profile, "
5062 		    "will fall back to a basic configuration and retry.\n",
5063 		    rc, cfg_file);
5064 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5065 		bzero(&caps_allowed, sizeof(caps_allowed));
5066 		COPY_CAPS(switch);
5067 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5068 		fallback = false;
5069 		goto retry;
5070 	}
5071 #undef COPY_CAPS
5072 	return (rc);
5073 }
5074 
5075 /*
5076  * Retrieve parameters that are needed (or nice to have) very early.
5077  */
5078 static int
get_params__pre_init(struct adapter * sc)5079 get_params__pre_init(struct adapter *sc)
5080 {
5081 	int rc;
5082 	uint32_t param[2], val[2];
5083 
5084 	t4_get_version_info(sc);
5085 
5086 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5087 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5088 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5089 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5090 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5091 
5092 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5093 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5094 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5095 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5096 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5097 
5098 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5099 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5100 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5101 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5102 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5103 
5104 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5105 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5106 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5107 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5108 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5109 
5110 	param[0] = FW_PARAM_DEV(PORTVEC);
5111 	param[1] = FW_PARAM_DEV(CCLK);
5112 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5113 	if (rc != 0) {
5114 		device_printf(sc->dev,
5115 		    "failed to query parameters (pre_init): %d.\n", rc);
5116 		return (rc);
5117 	}
5118 
5119 	sc->params.portvec = val[0];
5120 	sc->params.nports = bitcount32(val[0]);
5121 	sc->params.vpd.cclk = val[1];
5122 
5123 	/* Read device log parameters. */
5124 	rc = -t4_init_devlog_params(sc, 1);
5125 	if (rc == 0)
5126 		fixup_devlog_params(sc);
5127 	else {
5128 		device_printf(sc->dev,
5129 		    "failed to get devlog parameters: %d.\n", rc);
5130 		rc = 0;	/* devlog isn't critical for device operation */
5131 	}
5132 
5133 	return (rc);
5134 }
5135 
5136 /*
5137  * Any params that need to be set before FW_INITIALIZE.
5138  */
5139 static int
set_params__pre_init(struct adapter * sc)5140 set_params__pre_init(struct adapter *sc)
5141 {
5142 	int rc = 0;
5143 	uint32_t param, val;
5144 
5145 	if (chip_id(sc) >= CHELSIO_T6) {
5146 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5147 		val = 1;
5148 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5149 		/* firmwares < 1.20.1.0 do not have this param. */
5150 		if (rc == FW_EINVAL &&
5151 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5152 			rc = 0;
5153 		}
5154 		if (rc != 0) {
5155 			device_printf(sc->dev,
5156 			    "failed to enable high priority filters :%d.\n",
5157 			    rc);
5158 		}
5159 	}
5160 
5161 	/* Enable opaque VIIDs with firmwares that support it. */
5162 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5163 	val = 1;
5164 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5165 	if (rc == 0 && val == 1)
5166 		sc->params.viid_smt_extn_support = true;
5167 	else
5168 		sc->params.viid_smt_extn_support = false;
5169 
5170 	return (rc);
5171 }
5172 
5173 /*
5174  * Retrieve various parameters that are of interest to the driver.  The device
5175  * has been initialized by the firmware at this point.
5176  */
5177 static int
get_params__post_init(struct adapter * sc)5178 get_params__post_init(struct adapter *sc)
5179 {
5180 	int rc;
5181 	uint32_t param[7], val[7];
5182 	struct fw_caps_config_cmd caps;
5183 
5184 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5185 	param[1] = FW_PARAM_PFVF(EQ_START);
5186 	param[2] = FW_PARAM_PFVF(FILTER_START);
5187 	param[3] = FW_PARAM_PFVF(FILTER_END);
5188 	param[4] = FW_PARAM_PFVF(L2T_START);
5189 	param[5] = FW_PARAM_PFVF(L2T_END);
5190 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5191 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5192 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5193 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5194 	if (rc != 0) {
5195 		device_printf(sc->dev,
5196 		    "failed to query parameters (post_init): %d.\n", rc);
5197 		return (rc);
5198 	}
5199 
5200 	sc->sge.iq_start = val[0];
5201 	sc->sge.eq_start = val[1];
5202 	if ((int)val[3] > (int)val[2]) {
5203 		sc->tids.ftid_base = val[2];
5204 		sc->tids.ftid_end = val[3];
5205 		sc->tids.nftids = val[3] - val[2] + 1;
5206 	}
5207 	sc->vres.l2t.start = val[4];
5208 	sc->vres.l2t.size = val[5] - val[4] + 1;
5209 	KASSERT(sc->vres.l2t.size <= L2T_SIZE,
5210 	    ("%s: L2 table size (%u) larger than expected (%u)",
5211 	    __func__, sc->vres.l2t.size, L2T_SIZE));
5212 	sc->params.core_vdd = val[6];
5213 
5214 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5215 	param[1] = FW_PARAM_PFVF(EQ_END);
5216 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5217 	if (rc != 0) {
5218 		device_printf(sc->dev,
5219 		    "failed to query parameters (post_init2): %d.\n", rc);
5220 		return (rc);
5221 	}
5222 	MPASS((int)val[0] >= sc->sge.iq_start);
5223 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5224 	MPASS((int)val[1] >= sc->sge.eq_start);
5225 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5226 
5227 	if (chip_id(sc) >= CHELSIO_T6) {
5228 
5229 		sc->tids.tid_base = t4_read_reg(sc,
5230 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5231 
5232 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5233 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5234 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5235 		if (rc != 0) {
5236 			device_printf(sc->dev,
5237 			   "failed to query hpfilter parameters: %d.\n", rc);
5238 			return (rc);
5239 		}
5240 		if ((int)val[1] > (int)val[0]) {
5241 			sc->tids.hpftid_base = val[0];
5242 			sc->tids.hpftid_end = val[1];
5243 			sc->tids.nhpftids = val[1] - val[0] + 1;
5244 
5245 			/*
5246 			 * These should go off if the layout changes and the
5247 			 * driver needs to catch up.
5248 			 */
5249 			MPASS(sc->tids.hpftid_base == 0);
5250 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5251 		}
5252 
5253 		param[0] = FW_PARAM_PFVF(RAWF_START);
5254 		param[1] = FW_PARAM_PFVF(RAWF_END);
5255 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5256 		if (rc != 0) {
5257 			device_printf(sc->dev,
5258 			   "failed to query rawf parameters: %d.\n", rc);
5259 			return (rc);
5260 		}
5261 		if ((int)val[1] > (int)val[0]) {
5262 			sc->rawf_base = val[0];
5263 			sc->nrawf = val[1] - val[0] + 1;
5264 		}
5265 	}
5266 
5267 	/*
5268 	 * MPSBGMAP is queried separately because only recent firmwares support
5269 	 * it as a parameter and we don't want the compound query above to fail
5270 	 * on older firmwares.
5271 	 */
5272 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5273 	val[0] = 0;
5274 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5275 	if (rc == 0)
5276 		sc->params.mps_bg_map = val[0];
5277 	else
5278 		sc->params.mps_bg_map = 0;
5279 
5280 	/*
5281 	 * Determine whether the firmware supports the filter2 work request.
5282 	 * This is queried separately for the same reason as MPSBGMAP above.
5283 	 */
5284 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5285 	val[0] = 0;
5286 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5287 	if (rc == 0)
5288 		sc->params.filter2_wr_support = val[0] != 0;
5289 	else
5290 		sc->params.filter2_wr_support = 0;
5291 
5292 	/*
5293 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5294 	 * This is queried separately for the same reason as other params above.
5295 	 */
5296 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5297 	val[0] = 0;
5298 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5299 	if (rc == 0)
5300 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5301 	else
5302 		sc->params.ulptx_memwrite_dsgl = false;
5303 
5304 	/* FW_RI_FR_NSMR_TPTE_WR support */
5305 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5306 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5307 	if (rc == 0)
5308 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5309 	else
5310 		sc->params.fr_nsmr_tpte_wr_support = false;
5311 
5312 	/* Support for 512 SGL entries per FR MR. */
5313 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5314 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5315 	if (rc == 0)
5316 		sc->params.dev_512sgl_mr = val[0] != 0;
5317 	else
5318 		sc->params.dev_512sgl_mr = false;
5319 
5320 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5321 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5322 	if (rc == 0)
5323 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5324 	else
5325 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5326 
5327 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5328 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5329 	if (rc == 0) {
5330 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5331 		sc->params.nsched_cls = val[0];
5332 	} else
5333 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5334 
5335 	/* get capabilites */
5336 	bzero(&caps, sizeof(caps));
5337 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5338 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5339 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5340 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5341 	if (rc != 0) {
5342 		device_printf(sc->dev,
5343 		    "failed to get card capabilities: %d.\n", rc);
5344 		return (rc);
5345 	}
5346 
5347 #define READ_CAPS(x) do { \
5348 	sc->x = htobe16(caps.x); \
5349 } while (0)
5350 	READ_CAPS(nbmcaps);
5351 	READ_CAPS(linkcaps);
5352 	READ_CAPS(switchcaps);
5353 	READ_CAPS(niccaps);
5354 	READ_CAPS(toecaps);
5355 	READ_CAPS(rdmacaps);
5356 	READ_CAPS(cryptocaps);
5357 	READ_CAPS(iscsicaps);
5358 	READ_CAPS(fcoecaps);
5359 
5360 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5361 		MPASS(chip_id(sc) > CHELSIO_T4);
5362 		MPASS(sc->toecaps == 0);
5363 		sc->toecaps = 0;
5364 
5365 		param[0] = FW_PARAM_DEV(NTID);
5366 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5367 		if (rc != 0) {
5368 			device_printf(sc->dev,
5369 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5370 			return (rc);
5371 		}
5372 		sc->tids.ntids = val[0];
5373 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5374 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5375 			sc->tids.ntids -= sc->tids.nhpftids;
5376 		}
5377 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5378 		sc->params.hash_filter = 1;
5379 	}
5380 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5381 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5382 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5383 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5384 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5385 		if (rc != 0) {
5386 			device_printf(sc->dev,
5387 			    "failed to query NIC parameters: %d.\n", rc);
5388 			return (rc);
5389 		}
5390 		if ((int)val[1] > (int)val[0]) {
5391 			sc->tids.etid_base = val[0];
5392 			sc->tids.etid_end = val[1];
5393 			sc->tids.netids = val[1] - val[0] + 1;
5394 			sc->params.eo_wr_cred = val[2];
5395 			sc->params.ethoffload = 1;
5396 		}
5397 	}
5398 	if (sc->toecaps) {
5399 		/* query offload-related parameters */
5400 		param[0] = FW_PARAM_DEV(NTID);
5401 		param[1] = FW_PARAM_PFVF(SERVER_START);
5402 		param[2] = FW_PARAM_PFVF(SERVER_END);
5403 		param[3] = FW_PARAM_PFVF(TDDP_START);
5404 		param[4] = FW_PARAM_PFVF(TDDP_END);
5405 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5406 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5407 		if (rc != 0) {
5408 			device_printf(sc->dev,
5409 			    "failed to query TOE parameters: %d.\n", rc);
5410 			return (rc);
5411 		}
5412 		sc->tids.ntids = val[0];
5413 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5414 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5415 			sc->tids.ntids -= sc->tids.nhpftids;
5416 		}
5417 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5418 		if ((int)val[2] > (int)val[1]) {
5419 			sc->tids.stid_base = val[1];
5420 			sc->tids.nstids = val[2] - val[1] + 1;
5421 		}
5422 		sc->vres.ddp.start = val[3];
5423 		sc->vres.ddp.size = val[4] - val[3] + 1;
5424 		sc->params.ofldq_wr_cred = val[5];
5425 		sc->params.offload = 1;
5426 	} else {
5427 		/*
5428 		 * The firmware attempts memfree TOE configuration for -SO cards
5429 		 * and will report toecaps=0 if it runs out of resources (this
5430 		 * depends on the config file).  It may not report 0 for other
5431 		 * capabilities dependent on the TOE in this case.  Set them to
5432 		 * 0 here so that the driver doesn't bother tracking resources
5433 		 * that will never be used.
5434 		 */
5435 		sc->iscsicaps = 0;
5436 		sc->rdmacaps = 0;
5437 	}
5438 	if (sc->rdmacaps) {
5439 		param[0] = FW_PARAM_PFVF(STAG_START);
5440 		param[1] = FW_PARAM_PFVF(STAG_END);
5441 		param[2] = FW_PARAM_PFVF(RQ_START);
5442 		param[3] = FW_PARAM_PFVF(RQ_END);
5443 		param[4] = FW_PARAM_PFVF(PBL_START);
5444 		param[5] = FW_PARAM_PFVF(PBL_END);
5445 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5446 		if (rc != 0) {
5447 			device_printf(sc->dev,
5448 			    "failed to query RDMA parameters(1): %d.\n", rc);
5449 			return (rc);
5450 		}
5451 		sc->vres.stag.start = val[0];
5452 		sc->vres.stag.size = val[1] - val[0] + 1;
5453 		sc->vres.rq.start = val[2];
5454 		sc->vres.rq.size = val[3] - val[2] + 1;
5455 		sc->vres.pbl.start = val[4];
5456 		sc->vres.pbl.size = val[5] - val[4] + 1;
5457 
5458 		param[0] = FW_PARAM_PFVF(SQRQ_START);
5459 		param[1] = FW_PARAM_PFVF(SQRQ_END);
5460 		param[2] = FW_PARAM_PFVF(CQ_START);
5461 		param[3] = FW_PARAM_PFVF(CQ_END);
5462 		param[4] = FW_PARAM_PFVF(OCQ_START);
5463 		param[5] = FW_PARAM_PFVF(OCQ_END);
5464 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5465 		if (rc != 0) {
5466 			device_printf(sc->dev,
5467 			    "failed to query RDMA parameters(2): %d.\n", rc);
5468 			return (rc);
5469 		}
5470 		sc->vres.qp.start = val[0];
5471 		sc->vres.qp.size = val[1] - val[0] + 1;
5472 		sc->vres.cq.start = val[2];
5473 		sc->vres.cq.size = val[3] - val[2] + 1;
5474 		sc->vres.ocq.start = val[4];
5475 		sc->vres.ocq.size = val[5] - val[4] + 1;
5476 
5477 		param[0] = FW_PARAM_PFVF(SRQ_START);
5478 		param[1] = FW_PARAM_PFVF(SRQ_END);
5479 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
5480 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5481 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5482 		if (rc != 0) {
5483 			device_printf(sc->dev,
5484 			    "failed to query RDMA parameters(3): %d.\n", rc);
5485 			return (rc);
5486 		}
5487 		sc->vres.srq.start = val[0];
5488 		sc->vres.srq.size = val[1] - val[0] + 1;
5489 		sc->params.max_ordird_qp = val[2];
5490 		sc->params.max_ird_adapter = val[3];
5491 	}
5492 	if (sc->iscsicaps) {
5493 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5494 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5495 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5496 		if (rc != 0) {
5497 			device_printf(sc->dev,
5498 			    "failed to query iSCSI parameters: %d.\n", rc);
5499 			return (rc);
5500 		}
5501 		sc->vres.iscsi.start = val[0];
5502 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5503 	}
5504 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5505 		param[0] = FW_PARAM_PFVF(TLS_START);
5506 		param[1] = FW_PARAM_PFVF(TLS_END);
5507 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5508 		if (rc != 0) {
5509 			device_printf(sc->dev,
5510 			    "failed to query TLS parameters: %d.\n", rc);
5511 			return (rc);
5512 		}
5513 		sc->vres.key.start = val[0];
5514 		sc->vres.key.size = val[1] - val[0] + 1;
5515 	}
5516 
5517 	/*
5518 	 * We've got the params we wanted to query directly from the firmware.
5519 	 * Grab some others via other means.
5520 	 */
5521 	t4_init_sge_params(sc);
5522 	t4_init_tp_params(sc);
5523 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5524 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5525 
5526 	rc = t4_verify_chip_settings(sc);
5527 	if (rc != 0)
5528 		return (rc);
5529 	t4_init_rx_buf_info(sc);
5530 
5531 	return (rc);
5532 }
5533 
5534 #ifdef KERN_TLS
5535 static void
ktls_tick(void * arg)5536 ktls_tick(void *arg)
5537 {
5538 	struct adapter *sc;
5539 	uint32_t tstamp;
5540 
5541 	sc = arg;
5542 	tstamp = tcp_ts_getticks();
5543 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
5544 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
5545 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
5546 }
5547 
5548 static int
t4_config_kern_tls(struct adapter * sc,bool enable)5549 t4_config_kern_tls(struct adapter *sc, bool enable)
5550 {
5551 	int rc;
5552 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5553 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
5554 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
5555 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
5556 
5557 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
5558 	if (rc != 0) {
5559 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
5560 		    enable ?  "enable" : "disable", rc);
5561 		return (rc);
5562 	}
5563 
5564 	if (enable) {
5565 		sc->flags |= KERN_TLS_ON;
5566 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
5567 		    C_HARDCLOCK);
5568 	} else {
5569 		sc->flags &= ~KERN_TLS_ON;
5570 		callout_stop(&sc->ktls_tick);
5571 	}
5572 
5573 	return (rc);
5574 }
5575 #endif
5576 
5577 static int
set_params__post_init(struct adapter * sc)5578 set_params__post_init(struct adapter *sc)
5579 {
5580 	uint32_t mask, param, val;
5581 #ifdef TCP_OFFLOAD
5582 	int i, v, shift;
5583 #endif
5584 
5585 	/* ask for encapsulated CPLs */
5586 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5587 	val = 1;
5588 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5589 
5590 	/* Enable 32b port caps if the firmware supports it. */
5591 	param = FW_PARAM_PFVF(PORT_CAPS32);
5592 	val = 1;
5593 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
5594 		sc->params.port_caps32 = 1;
5595 
5596 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
5597 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
5598 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
5599 	    V_MASKFILTER(val - 1));
5600 
5601 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
5602 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
5603 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5604 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
5605 	val = 0;
5606 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
5607 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
5608 		    F_ATTACKFILTERENABLE);
5609 		val |= F_DROPERRORATTACK;
5610 	}
5611 	if (t4_drop_ip_fragments != 0) {
5612 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
5613 		    F_FRAGMENTDROP);
5614 		val |= F_DROPERRORFRAG;
5615 	}
5616 	if (t4_drop_pkts_with_l2_errors != 0)
5617 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
5618 	if (t4_drop_pkts_with_l3_errors != 0) {
5619 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
5620 		    F_DROPERRORCSUMIP;
5621 	}
5622 	if (t4_drop_pkts_with_l4_errors != 0) {
5623 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5624 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
5625 	}
5626 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
5627 
5628 #ifdef TCP_OFFLOAD
5629 	/*
5630 	 * Override the TOE timers with user provided tunables.  This is not the
5631 	 * recommended way to change the timers (the firmware config file is) so
5632 	 * these tunables are not documented.
5633 	 *
5634 	 * All the timer tunables are in microseconds.
5635 	 */
5636 	if (t4_toe_keepalive_idle != 0) {
5637 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
5638 		v &= M_KEEPALIVEIDLE;
5639 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
5640 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
5641 	}
5642 	if (t4_toe_keepalive_interval != 0) {
5643 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
5644 		v &= M_KEEPALIVEINTVL;
5645 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
5646 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
5647 	}
5648 	if (t4_toe_keepalive_count != 0) {
5649 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
5650 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5651 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
5652 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
5653 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
5654 	}
5655 	if (t4_toe_rexmt_min != 0) {
5656 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
5657 		v &= M_RXTMIN;
5658 		t4_set_reg_field(sc, A_TP_RXT_MIN,
5659 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
5660 	}
5661 	if (t4_toe_rexmt_max != 0) {
5662 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
5663 		v &= M_RXTMAX;
5664 		t4_set_reg_field(sc, A_TP_RXT_MAX,
5665 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
5666 	}
5667 	if (t4_toe_rexmt_count != 0) {
5668 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
5669 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5670 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
5671 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
5672 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
5673 	}
5674 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
5675 		if (t4_toe_rexmt_backoff[i] != -1) {
5676 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
5677 			shift = (i & 3) << 3;
5678 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
5679 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
5680 		}
5681 	}
5682 #endif
5683 
5684 #ifdef KERN_TLS
5685 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS &&
5686 	    sc->toecaps & FW_CAPS_CONFIG_TOE) {
5687 		/*
5688 		 * Limit TOE connections to 2 reassembly "islands".  This is
5689 		 * required for TOE TLS connections to downgrade to plain TOE
5690 		 * connections if an unsupported TLS version or ciphersuite is
5691 		 * used.
5692 		 */
5693 		t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG,
5694 		    V_PASSMODE(M_PASSMODE), V_PASSMODE(2));
5695 		if (is_ktls(sc)) {
5696 			sc->tlst.inline_keys = t4_tls_inline_keys;
5697 			sc->tlst.combo_wrs = t4_tls_combo_wrs;
5698 			if (t4_kern_tls != 0)
5699 				t4_config_kern_tls(sc, true);
5700 		}
5701 	}
5702 #endif
5703 	return (0);
5704 }
5705 
5706 #undef FW_PARAM_PFVF
5707 #undef FW_PARAM_DEV
5708 
5709 static void
t4_set_desc(struct adapter * sc)5710 t4_set_desc(struct adapter *sc)
5711 {
5712 	char buf[128];
5713 	struct adapter_params *p = &sc->params;
5714 
5715 	snprintf(buf, sizeof(buf), "Chelsio %s", p->vpd.id);
5716 
5717 	device_set_desc_copy(sc->dev, buf);
5718 }
5719 
5720 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)5721 ifmedia_add4(struct ifmedia *ifm, int m)
5722 {
5723 
5724 	ifmedia_add(ifm, m, 0, NULL);
5725 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
5726 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
5727 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
5728 }
5729 
5730 /*
5731  * This is the selected media, which is not quite the same as the active media.
5732  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
5733  * and active are not the same, and "media: Ethernet selected" otherwise.
5734  */
5735 static void
set_current_media(struct port_info * pi)5736 set_current_media(struct port_info *pi)
5737 {
5738 	struct link_config *lc;
5739 	struct ifmedia *ifm;
5740 	int mword;
5741 	u_int speed;
5742 
5743 	PORT_LOCK_ASSERT_OWNED(pi);
5744 
5745 	/* Leave current media alone if it's already set to IFM_NONE. */
5746 	ifm = &pi->media;
5747 	if (ifm->ifm_cur != NULL &&
5748 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
5749 		return;
5750 
5751 	lc = &pi->link_cfg;
5752 	if (lc->requested_aneg != AUTONEG_DISABLE &&
5753 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
5754 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
5755 		return;
5756 	}
5757 	mword = IFM_ETHER | IFM_FDX;
5758 	if (lc->requested_fc & PAUSE_TX)
5759 		mword |= IFM_ETH_TXPAUSE;
5760 	if (lc->requested_fc & PAUSE_RX)
5761 		mword |= IFM_ETH_RXPAUSE;
5762 	if (lc->requested_speed == 0)
5763 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
5764 	else
5765 		speed = lc->requested_speed;
5766 	mword |= port_mword(pi, speed_to_fwcap(speed));
5767 	ifmedia_set(ifm, mword);
5768 }
5769 
5770 /*
5771  * Returns true if the ifmedia list for the port cannot change.
5772  */
5773 static bool
fixed_ifmedia(struct port_info * pi)5774 fixed_ifmedia(struct port_info *pi)
5775 {
5776 
5777 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
5778 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
5779 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
5780 	    pi->port_type == FW_PORT_TYPE_KX4 ||
5781 	    pi->port_type == FW_PORT_TYPE_KX ||
5782 	    pi->port_type == FW_PORT_TYPE_KR ||
5783 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
5784 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
5785 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
5786 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
5787 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
5788 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
5789 }
5790 
5791 static void
build_medialist(struct port_info * pi)5792 build_medialist(struct port_info *pi)
5793 {
5794 	uint32_t ss, speed;
5795 	int unknown, mword, bit;
5796 	struct link_config *lc;
5797 	struct ifmedia *ifm;
5798 
5799 	PORT_LOCK_ASSERT_OWNED(pi);
5800 
5801 	if (pi->flags & FIXED_IFMEDIA)
5802 		return;
5803 
5804 	/*
5805 	 * Rebuild the ifmedia list.
5806 	 */
5807 	ifm = &pi->media;
5808 	ifmedia_removeall(ifm);
5809 	lc = &pi->link_cfg;
5810 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
5811 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
5812 		MPASS(ss != 0);
5813 no_media:
5814 		MPASS(LIST_EMPTY(&ifm->ifm_list));
5815 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
5816 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
5817 		return;
5818 	}
5819 
5820 	unknown = 0;
5821 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
5822 		speed = 1 << bit;
5823 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
5824 		if (ss & speed) {
5825 			mword = port_mword(pi, speed);
5826 			if (mword == IFM_NONE) {
5827 				goto no_media;
5828 			} else if (mword == IFM_UNKNOWN)
5829 				unknown++;
5830 			else
5831 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
5832 		}
5833 	}
5834 	if (unknown > 0) /* Add one unknown for all unknown media types. */
5835 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
5836 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
5837 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
5838 
5839 	set_current_media(pi);
5840 }
5841 
5842 /*
5843  * Initialize the requested fields in the link config based on driver tunables.
5844  */
5845 static void
init_link_config(struct port_info * pi)5846 init_link_config(struct port_info *pi)
5847 {
5848 	struct link_config *lc = &pi->link_cfg;
5849 
5850 	PORT_LOCK_ASSERT_OWNED(pi);
5851 	MPASS(lc->pcaps != 0);
5852 
5853 	lc->requested_caps = 0;
5854 	lc->requested_speed = 0;
5855 
5856 	if (t4_autoneg == 0)
5857 		lc->requested_aneg = AUTONEG_DISABLE;
5858 	else if (t4_autoneg == 1)
5859 		lc->requested_aneg = AUTONEG_ENABLE;
5860 	else
5861 		lc->requested_aneg = AUTONEG_AUTO;
5862 
5863 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
5864 	    PAUSE_AUTONEG);
5865 
5866 	if (t4_fec & FEC_AUTO)
5867 		lc->requested_fec = FEC_AUTO;
5868 	else if (t4_fec == 0)
5869 		lc->requested_fec = FEC_NONE;
5870 	else {
5871 		/* -1 is handled by the FEC_AUTO block above and not here. */
5872 		lc->requested_fec = t4_fec &
5873 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
5874 		if (lc->requested_fec == 0)
5875 			lc->requested_fec = FEC_AUTO;
5876 	}
5877 	lc->force_fec = 0;
5878 	if (lc->pcaps & FW_PORT_CAP32_FORCE_FEC) {
5879 		if (t4_force_fec < 0)
5880 			lc->force_fec = -1;
5881 		else if (t4_force_fec > 0)
5882 			lc->force_fec = 1;
5883 	}
5884 }
5885 
5886 /*
5887  * Makes sure that all requested settings comply with what's supported by the
5888  * port.  Returns the number of settings that were invalid and had to be fixed.
5889  */
5890 static int
fixup_link_config(struct port_info * pi)5891 fixup_link_config(struct port_info *pi)
5892 {
5893 	int n = 0;
5894 	struct link_config *lc = &pi->link_cfg;
5895 	uint32_t fwspeed;
5896 
5897 	PORT_LOCK_ASSERT_OWNED(pi);
5898 
5899 	/* Speed (when not autonegotiating) */
5900 	if (lc->requested_speed != 0) {
5901 		fwspeed = speed_to_fwcap(lc->requested_speed);
5902 		if ((fwspeed & lc->pcaps) == 0) {
5903 			n++;
5904 			lc->requested_speed = 0;
5905 		}
5906 	}
5907 
5908 	/* Link autonegotiation */
5909 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
5910 	    lc->requested_aneg == AUTONEG_DISABLE ||
5911 	    lc->requested_aneg == AUTONEG_AUTO);
5912 	if (lc->requested_aneg == AUTONEG_ENABLE &&
5913 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
5914 		n++;
5915 		lc->requested_aneg = AUTONEG_AUTO;
5916 	}
5917 
5918 	/* Flow control */
5919 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
5920 	if (lc->requested_fc & PAUSE_TX &&
5921 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
5922 		n++;
5923 		lc->requested_fc &= ~PAUSE_TX;
5924 	}
5925 	if (lc->requested_fc & PAUSE_RX &&
5926 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
5927 		n++;
5928 		lc->requested_fc &= ~PAUSE_RX;
5929 	}
5930 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
5931 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
5932 		n++;
5933 		lc->requested_fc |= PAUSE_AUTONEG;
5934 	}
5935 
5936 	/* FEC */
5937 	if ((lc->requested_fec & FEC_RS &&
5938 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
5939 	    (lc->requested_fec & FEC_BASER_RS &&
5940 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
5941 		n++;
5942 		lc->requested_fec = FEC_AUTO;
5943 	}
5944 
5945 	return (n);
5946 }
5947 
5948 /*
5949  * Apply the requested L1 settings, which are expected to be valid, to the
5950  * hardware.
5951  */
5952 static int
apply_link_config(struct port_info * pi)5953 apply_link_config(struct port_info *pi)
5954 {
5955 	struct adapter *sc = pi->adapter;
5956 	struct link_config *lc = &pi->link_cfg;
5957 	int rc;
5958 
5959 #ifdef INVARIANTS
5960 	ASSERT_SYNCHRONIZED_OP(sc);
5961 	PORT_LOCK_ASSERT_OWNED(pi);
5962 
5963 	if (lc->requested_aneg == AUTONEG_ENABLE)
5964 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
5965 	if (!(lc->requested_fc & PAUSE_AUTONEG))
5966 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
5967 	if (lc->requested_fc & PAUSE_TX)
5968 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
5969 	if (lc->requested_fc & PAUSE_RX)
5970 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
5971 	if (lc->requested_fec & FEC_RS)
5972 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
5973 	if (lc->requested_fec & FEC_BASER_RS)
5974 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
5975 #endif
5976 	rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
5977 	if (rc != 0) {
5978 		/* Don't complain if the VF driver gets back an EPERM. */
5979 		if (!(sc->flags & IS_VF) || rc != FW_EPERM)
5980 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
5981 	} else {
5982 		/*
5983 		 * An L1_CFG will almost always result in a link-change event if
5984 		 * the link is up, and the driver will refresh the actual
5985 		 * fec/fc/etc. when the notification is processed.  If the link
5986 		 * is down then the actual settings are meaningless.
5987 		 *
5988 		 * This takes care of the case where a change in the L1 settings
5989 		 * may not result in a notification.
5990 		 */
5991 		if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
5992 			lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
5993 	}
5994 	return (rc);
5995 }
5996 
5997 #define FW_MAC_EXACT_CHUNK	7
5998 struct mcaddr_ctx {
5999 	struct ifnet *ifp;
6000 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6001 	uint64_t hash;
6002 	int i;
6003 	int del;
6004 	int rc;
6005 };
6006 
6007 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6008 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6009 {
6010 	struct mcaddr_ctx *ctx = arg;
6011 	struct vi_info *vi = ctx->ifp->if_softc;
6012 	struct port_info *pi = vi->pi;
6013 	struct adapter *sc = pi->adapter;
6014 
6015 	if (ctx->rc < 0)
6016 		return (0);
6017 
6018 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6019 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6020 	ctx->i++;
6021 
6022 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6023 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6024 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6025 		if (ctx->rc < 0) {
6026 			int j;
6027 
6028 			for (j = 0; j < ctx->i; j++) {
6029 				if_printf(ctx->ifp,
6030 				    "failed to add mc address"
6031 				    " %02x:%02x:%02x:"
6032 				    "%02x:%02x:%02x rc=%d\n",
6033 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6034 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6035 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6036 				    -ctx->rc);
6037 			}
6038 			return (0);
6039 		}
6040 		ctx->del = 0;
6041 		ctx->i = 0;
6042 	}
6043 
6044 	return (1);
6045 }
6046 
6047 /*
6048  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6049  * indicates which parameters should be programmed (the rest are left alone).
6050  */
6051 int
update_mac_settings(struct ifnet * ifp,int flags)6052 update_mac_settings(struct ifnet *ifp, int flags)
6053 {
6054 	int rc = 0;
6055 	struct vi_info *vi = ifp->if_softc;
6056 	struct port_info *pi = vi->pi;
6057 	struct adapter *sc = pi->adapter;
6058 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6059 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6060 
6061 	ASSERT_SYNCHRONIZED_OP(sc);
6062 	KASSERT(flags, ("%s: not told what to update.", __func__));
6063 
6064 	if (flags & XGMAC_MTU)
6065 		mtu = ifp->if_mtu;
6066 
6067 	if (flags & XGMAC_PROMISC)
6068 		promisc = ifp->if_flags & IFF_PROMISC ? 1 : 0;
6069 
6070 	if (flags & XGMAC_ALLMULTI)
6071 		allmulti = ifp->if_flags & IFF_ALLMULTI ? 1 : 0;
6072 
6073 	if (flags & XGMAC_VLANEX)
6074 		vlanex = ifp->if_capenable & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6075 
6076 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6077 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6078 		    allmulti, 1, vlanex, false);
6079 		if (rc) {
6080 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6081 			    rc);
6082 			return (rc);
6083 		}
6084 	}
6085 
6086 	if (flags & XGMAC_UCADDR) {
6087 		uint8_t ucaddr[ETHER_ADDR_LEN];
6088 
6089 		bcopy(IF_LLADDR(ifp), ucaddr, sizeof(ucaddr));
6090 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6091 		    ucaddr, true, &vi->smt_idx);
6092 		if (rc < 0) {
6093 			rc = -rc;
6094 			if_printf(ifp, "change_mac failed: %d\n", rc);
6095 			return (rc);
6096 		} else {
6097 			vi->xact_addr_filt = rc;
6098 			rc = 0;
6099 		}
6100 	}
6101 
6102 	if (flags & XGMAC_MCADDRS) {
6103 		struct epoch_tracker et;
6104 		struct mcaddr_ctx ctx;
6105 		int j;
6106 
6107 		ctx.ifp = ifp;
6108 		ctx.hash = 0;
6109 		ctx.i = 0;
6110 		ctx.del = 1;
6111 		ctx.rc = 0;
6112 		/*
6113 		 * Unlike other drivers, we accumulate list of pointers into
6114 		 * interface address lists and we need to keep it safe even
6115 		 * after if_foreach_llmaddr() returns, thus we must enter the
6116 		 * network epoch.
6117 		 */
6118 		NET_EPOCH_ENTER(et);
6119 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6120 		if (ctx.rc < 0) {
6121 			NET_EPOCH_EXIT(et);
6122 			rc = -ctx.rc;
6123 			return (rc);
6124 		}
6125 		if (ctx.i > 0) {
6126 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6127 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6128 			NET_EPOCH_EXIT(et);
6129 			if (rc < 0) {
6130 				rc = -rc;
6131 				for (j = 0; j < ctx.i; j++) {
6132 					if_printf(ifp,
6133 					    "failed to add mcast address"
6134 					    " %02x:%02x:%02x:"
6135 					    "%02x:%02x:%02x rc=%d\n",
6136 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6137 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6138 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6139 					    rc);
6140 				}
6141 				return (rc);
6142 			}
6143 			ctx.del = 0;
6144 		} else
6145 			NET_EPOCH_EXIT(et);
6146 
6147 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6148 		if (rc != 0)
6149 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6150 			    rc);
6151 		if (ctx.del == 0) {
6152 			/* We clobbered the VXLAN entry if there was one. */
6153 			pi->vxlan_tcam_entry = false;
6154 		}
6155 	}
6156 
6157 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6158 	    pi->vxlan_tcam_entry == false) {
6159 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6160 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6161 		    true);
6162 		if (rc < 0) {
6163 			rc = -rc;
6164 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6165 			    rc);
6166 		} else {
6167 			MPASS(rc == sc->rawf_base + pi->port_id);
6168 			rc = 0;
6169 			pi->vxlan_tcam_entry = true;
6170 		}
6171 	}
6172 
6173 	return (rc);
6174 }
6175 
6176 /*
6177  * {begin|end}_synchronized_op must be called from the same thread.
6178  */
6179 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6180 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6181     char *wmesg)
6182 {
6183 	int rc, pri;
6184 
6185 #ifdef WITNESS
6186 	/* the caller thinks it's ok to sleep, but is it really? */
6187 	if (flags & SLEEP_OK)
6188 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
6189 		    "begin_synchronized_op");
6190 #endif
6191 
6192 	if (INTR_OK)
6193 		pri = PCATCH;
6194 	else
6195 		pri = 0;
6196 
6197 	ADAPTER_LOCK(sc);
6198 	for (;;) {
6199 
6200 		if (vi && IS_DOOMED(vi)) {
6201 			rc = ENXIO;
6202 			goto done;
6203 		}
6204 
6205 		if (!IS_BUSY(sc)) {
6206 			rc = 0;
6207 			break;
6208 		}
6209 
6210 		if (!(flags & SLEEP_OK)) {
6211 			rc = EBUSY;
6212 			goto done;
6213 		}
6214 
6215 		if (mtx_sleep(&sc->flags, &sc->sc_lock, pri, wmesg, 0)) {
6216 			rc = EINTR;
6217 			goto done;
6218 		}
6219 	}
6220 
6221 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6222 	SET_BUSY(sc);
6223 #ifdef INVARIANTS
6224 	sc->last_op = wmesg;
6225 	sc->last_op_thr = curthread;
6226 	sc->last_op_flags = flags;
6227 #endif
6228 
6229 done:
6230 	if (!(flags & HOLD_LOCK) || rc)
6231 		ADAPTER_UNLOCK(sc);
6232 
6233 	return (rc);
6234 }
6235 
6236 /*
6237  * Tell if_ioctl and if_init that the VI is going away.  This is
6238  * special variant of begin_synchronized_op and must be paired with a
6239  * call to end_synchronized_op.
6240  */
6241 void
doom_vi(struct adapter * sc,struct vi_info * vi)6242 doom_vi(struct adapter *sc, struct vi_info *vi)
6243 {
6244 
6245 	ADAPTER_LOCK(sc);
6246 	SET_DOOMED(vi);
6247 	wakeup(&sc->flags);
6248 	while (IS_BUSY(sc))
6249 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6250 	SET_BUSY(sc);
6251 #ifdef INVARIANTS
6252 	sc->last_op = "t4detach";
6253 	sc->last_op_thr = curthread;
6254 	sc->last_op_flags = 0;
6255 #endif
6256 	ADAPTER_UNLOCK(sc);
6257 }
6258 
6259 /*
6260  * {begin|end}_synchronized_op must be called from the same thread.
6261  */
6262 void
end_synchronized_op(struct adapter * sc,int flags)6263 end_synchronized_op(struct adapter *sc, int flags)
6264 {
6265 
6266 	if (flags & LOCK_HELD)
6267 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6268 	else
6269 		ADAPTER_LOCK(sc);
6270 
6271 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6272 	CLR_BUSY(sc);
6273 	wakeup(&sc->flags);
6274 	ADAPTER_UNLOCK(sc);
6275 }
6276 
6277 static int
cxgbe_init_synchronized(struct vi_info * vi)6278 cxgbe_init_synchronized(struct vi_info *vi)
6279 {
6280 	struct port_info *pi = vi->pi;
6281 	struct adapter *sc = pi->adapter;
6282 	struct ifnet *ifp = vi->ifp;
6283 	int rc = 0, i;
6284 	struct sge_txq *txq;
6285 
6286 	ASSERT_SYNCHRONIZED_OP(sc);
6287 
6288 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6289 		return (0);	/* already running */
6290 
6291 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6292 		return (rc);	/* error message displayed already */
6293 
6294 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6295 		return (rc); /* error message displayed already */
6296 
6297 	rc = update_mac_settings(ifp, XGMAC_ALL);
6298 	if (rc)
6299 		goto done;	/* error message displayed already */
6300 
6301 	PORT_LOCK(pi);
6302 	if (pi->up_vis == 0) {
6303 		t4_update_port_info(pi);
6304 		fixup_link_config(pi);
6305 		build_medialist(pi);
6306 		apply_link_config(pi);
6307 	}
6308 
6309 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6310 	if (rc != 0) {
6311 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6312 		PORT_UNLOCK(pi);
6313 		goto done;
6314 	}
6315 
6316 	/*
6317 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6318 	 * if this changes.
6319 	 */
6320 
6321 	for_each_txq(vi, i, txq) {
6322 		TXQ_LOCK(txq);
6323 		txq->eq.flags |= EQ_ENABLED;
6324 		TXQ_UNLOCK(txq);
6325 	}
6326 
6327 	/*
6328 	 * The first iq of the first port to come up is used for tracing.
6329 	 */
6330 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6331 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6332 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
6333 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
6334 		    V_QUEUENUMBER(sc->traceq));
6335 		pi->flags |= HAS_TRACEQ;
6336 	}
6337 
6338 	/* all ok */
6339 	pi->up_vis++;
6340 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
6341 	if (pi->link_cfg.link_ok)
6342 		t4_os_link_changed(pi);
6343 	PORT_UNLOCK(pi);
6344 
6345 	mtx_lock(&vi->tick_mtx);
6346 	if (ifp->if_get_counter == vi_get_counter)
6347 		callout_reset(&vi->tick, hz, vi_tick, vi);
6348 	else
6349 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6350 	mtx_unlock(&vi->tick_mtx);
6351 done:
6352 	if (rc != 0)
6353 		cxgbe_uninit_synchronized(vi);
6354 
6355 	return (rc);
6356 }
6357 
6358 /*
6359  * Idempotent.
6360  */
6361 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6362 cxgbe_uninit_synchronized(struct vi_info *vi)
6363 {
6364 	struct port_info *pi = vi->pi;
6365 	struct adapter *sc = pi->adapter;
6366 	struct ifnet *ifp = vi->ifp;
6367 	int rc, i;
6368 	struct sge_txq *txq;
6369 
6370 	ASSERT_SYNCHRONIZED_OP(sc);
6371 
6372 	if (!(vi->flags & VI_INIT_DONE)) {
6373 		if (__predict_false(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
6374 			KASSERT(0, ("uninited VI is running"));
6375 			if_printf(ifp, "uninited VI with running ifnet.  "
6376 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6377 			    "if_drv_flags 0x%08x\n", vi->flags, ifp->if_flags,
6378 			    ifp->if_drv_flags);
6379 		}
6380 		return (0);
6381 	}
6382 
6383 	/*
6384 	 * Disable the VI so that all its data in either direction is discarded
6385 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6386 	 * tick) intact as the TP can deliver negative advice or data that it's
6387 	 * holding in its RAM (for an offloaded connection) even after the VI is
6388 	 * disabled.
6389 	 */
6390 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6391 	if (rc) {
6392 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6393 		return (rc);
6394 	}
6395 
6396 	for_each_txq(vi, i, txq) {
6397 		TXQ_LOCK(txq);
6398 		txq->eq.flags &= ~EQ_ENABLED;
6399 		TXQ_UNLOCK(txq);
6400 	}
6401 
6402 	mtx_lock(&vi->tick_mtx);
6403 	callout_stop(&vi->tick);
6404 	mtx_unlock(&vi->tick_mtx);
6405 
6406 	PORT_LOCK(pi);
6407 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
6408 		PORT_UNLOCK(pi);
6409 		return (0);
6410 	}
6411 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
6412 	pi->up_vis--;
6413 	if (pi->up_vis > 0) {
6414 		PORT_UNLOCK(pi);
6415 		return (0);
6416 	}
6417 
6418 	pi->link_cfg.link_ok = false;
6419 	pi->link_cfg.speed = 0;
6420 	pi->link_cfg.link_down_rc = 255;
6421 	t4_os_link_changed(pi);
6422 	PORT_UNLOCK(pi);
6423 
6424 	return (0);
6425 }
6426 
6427 /*
6428  * It is ok for this function to fail midway and return right away.  t4_detach
6429  * will walk the entire sc->irq list and clean up whatever is valid.
6430  */
6431 int
t4_setup_intr_handlers(struct adapter * sc)6432 t4_setup_intr_handlers(struct adapter *sc)
6433 {
6434 	int rc, rid, p, q, v;
6435 	char s[8];
6436 	struct irq *irq;
6437 	struct port_info *pi;
6438 	struct vi_info *vi;
6439 	struct sge *sge = &sc->sge;
6440 	struct sge_rxq *rxq;
6441 #ifdef TCP_OFFLOAD
6442 	struct sge_ofld_rxq *ofld_rxq;
6443 #endif
6444 #ifdef DEV_NETMAP
6445 	struct sge_nm_rxq *nm_rxq;
6446 #endif
6447 #ifdef RSS
6448 	int nbuckets = rss_getnumbuckets();
6449 #endif
6450 
6451 	/*
6452 	 * Setup interrupts.
6453 	 */
6454 	irq = &sc->irq[0];
6455 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6456 	if (forwarding_intr_to_fwq(sc))
6457 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6458 
6459 	/* Multiple interrupts. */
6460 	if (sc->flags & IS_VF)
6461 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6462 		    ("%s: too few intr.", __func__));
6463 	else
6464 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6465 		    ("%s: too few intr.", __func__));
6466 
6467 	/* The first one is always error intr on PFs */
6468 	if (!(sc->flags & IS_VF)) {
6469 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6470 		if (rc != 0)
6471 			return (rc);
6472 		irq++;
6473 		rid++;
6474 	}
6475 
6476 	/* The second one is always the firmware event queue (first on VFs) */
6477 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6478 	if (rc != 0)
6479 		return (rc);
6480 	irq++;
6481 	rid++;
6482 
6483 	for_each_port(sc, p) {
6484 		pi = sc->port[p];
6485 		for_each_vi(pi, v, vi) {
6486 			vi->first_intr = rid - 1;
6487 
6488 			if (vi->nnmrxq > 0) {
6489 				int n = max(vi->nrxq, vi->nnmrxq);
6490 
6491 				rxq = &sge->rxq[vi->first_rxq];
6492 #ifdef DEV_NETMAP
6493 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6494 #endif
6495 				for (q = 0; q < n; q++) {
6496 					snprintf(s, sizeof(s), "%x%c%x", p,
6497 					    'a' + v, q);
6498 					if (q < vi->nrxq)
6499 						irq->rxq = rxq++;
6500 #ifdef DEV_NETMAP
6501 					if (q < vi->nnmrxq)
6502 						irq->nm_rxq = nm_rxq++;
6503 
6504 					if (irq->nm_rxq != NULL &&
6505 					    irq->rxq == NULL) {
6506 						/* Netmap rx only */
6507 						rc = t4_alloc_irq(sc, irq, rid,
6508 						    t4_nm_intr, irq->nm_rxq, s);
6509 					}
6510 					if (irq->nm_rxq != NULL &&
6511 					    irq->rxq != NULL) {
6512 						/* NIC and Netmap rx */
6513 						rc = t4_alloc_irq(sc, irq, rid,
6514 						    t4_vi_intr, irq, s);
6515 					}
6516 #endif
6517 					if (irq->rxq != NULL &&
6518 					    irq->nm_rxq == NULL) {
6519 						/* NIC rx only */
6520 						rc = t4_alloc_irq(sc, irq, rid,
6521 						    t4_intr, irq->rxq, s);
6522 					}
6523 					if (rc != 0)
6524 						return (rc);
6525 #ifdef RSS
6526 					if (q < vi->nrxq) {
6527 						bus_bind_intr(sc->dev, irq->res,
6528 						    rss_getcpu(q % nbuckets));
6529 					}
6530 #endif
6531 					irq++;
6532 					rid++;
6533 					vi->nintr++;
6534 				}
6535 			} else {
6536 				for_each_rxq(vi, q, rxq) {
6537 					snprintf(s, sizeof(s), "%x%c%x", p,
6538 					    'a' + v, q);
6539 					rc = t4_alloc_irq(sc, irq, rid,
6540 					    t4_intr, rxq, s);
6541 					if (rc != 0)
6542 						return (rc);
6543 #ifdef RSS
6544 					bus_bind_intr(sc->dev, irq->res,
6545 					    rss_getcpu(q % nbuckets));
6546 #endif
6547 					irq++;
6548 					rid++;
6549 					vi->nintr++;
6550 				}
6551 			}
6552 #ifdef TCP_OFFLOAD
6553 			for_each_ofld_rxq(vi, q, ofld_rxq) {
6554 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
6555 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
6556 				    ofld_rxq, s);
6557 				if (rc != 0)
6558 					return (rc);
6559 				irq++;
6560 				rid++;
6561 				vi->nintr++;
6562 			}
6563 #endif
6564 		}
6565 	}
6566 	MPASS(irq == &sc->irq[sc->intr_count]);
6567 
6568 	return (0);
6569 }
6570 
6571 static void
write_global_rss_key(struct adapter * sc)6572 write_global_rss_key(struct adapter *sc)
6573 {
6574 #ifdef RSS
6575 	int i;
6576 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6577 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6578 
6579 	CTASSERT(RSS_KEYSIZE == 40);
6580 
6581 	rss_getkey((void *)&raw_rss_key[0]);
6582 	for (i = 0; i < nitems(rss_key); i++) {
6583 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
6584 	}
6585 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
6586 #endif
6587 }
6588 
6589 /*
6590  * Idempotent.
6591  */
6592 static int
adapter_full_init(struct adapter * sc)6593 adapter_full_init(struct adapter *sc)
6594 {
6595 	int rc, i;
6596 
6597 	ASSERT_SYNCHRONIZED_OP(sc);
6598 
6599 	/*
6600 	 * queues that belong to the adapter (not any particular port).
6601 	 */
6602 	rc = t4_setup_adapter_queues(sc);
6603 	if (rc != 0)
6604 		return (rc);
6605 
6606 	for (i = 0; i < nitems(sc->tq); i++) {
6607 		if (sc->tq[i] != NULL)
6608 			continue;
6609 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
6610 		    taskqueue_thread_enqueue, &sc->tq[i]);
6611 		if (sc->tq[i] == NULL) {
6612 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
6613 			return (ENOMEM);
6614 		}
6615 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
6616 		    device_get_nameunit(sc->dev), i);
6617 	}
6618 
6619 	if (!(sc->flags & IS_VF)) {
6620 		write_global_rss_key(sc);
6621 		t4_intr_enable(sc);
6622 	}
6623 	return (0);
6624 }
6625 
6626 int
adapter_init(struct adapter * sc)6627 adapter_init(struct adapter *sc)
6628 {
6629 	int rc;
6630 
6631 	ASSERT_SYNCHRONIZED_OP(sc);
6632 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
6633 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
6634 	    ("%s: FULL_INIT_DONE already", __func__));
6635 
6636 	rc = adapter_full_init(sc);
6637 	if (rc != 0)
6638 		adapter_full_uninit(sc);
6639 	else
6640 		sc->flags |= FULL_INIT_DONE;
6641 
6642 	return (rc);
6643 }
6644 
6645 /*
6646  * Idempotent.
6647  */
6648 static void
adapter_full_uninit(struct adapter * sc)6649 adapter_full_uninit(struct adapter *sc)
6650 {
6651 	int i;
6652 
6653 	t4_teardown_adapter_queues(sc);
6654 
6655 	for (i = 0; i < nitems(sc->tq) && sc->tq[i]; i++) {
6656 		taskqueue_free(sc->tq[i]);
6657 		sc->tq[i] = NULL;
6658 	}
6659 
6660 	sc->flags &= ~FULL_INIT_DONE;
6661 }
6662 
6663 #ifdef RSS
6664 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
6665     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
6666     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
6667     RSS_HASHTYPE_RSS_UDP_IPV6)
6668 
6669 /* Translates kernel hash types to hardware. */
6670 static int
hashconfig_to_hashen(int hashconfig)6671 hashconfig_to_hashen(int hashconfig)
6672 {
6673 	int hashen = 0;
6674 
6675 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
6676 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
6677 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
6678 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
6679 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
6680 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6681 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6682 	}
6683 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
6684 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6685 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6686 	}
6687 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
6688 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6689 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
6690 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6691 
6692 	return (hashen);
6693 }
6694 
6695 /* Translates hardware hash types to kernel. */
6696 static int
hashen_to_hashconfig(int hashen)6697 hashen_to_hashconfig(int hashen)
6698 {
6699 	int hashconfig = 0;
6700 
6701 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
6702 		/*
6703 		 * If UDP hashing was enabled it must have been enabled for
6704 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
6705 		 * enabling any 4-tuple hash is nonsense configuration.
6706 		 */
6707 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6708 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
6709 
6710 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6711 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
6712 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6713 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
6714 	}
6715 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6716 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
6717 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6718 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
6719 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
6720 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
6721 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
6722 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
6723 
6724 	return (hashconfig);
6725 }
6726 #endif
6727 
6728 /*
6729  * Idempotent.
6730  */
6731 static int
vi_full_init(struct vi_info * vi)6732 vi_full_init(struct vi_info *vi)
6733 {
6734 	struct adapter *sc = vi->adapter;
6735 	struct sge_rxq *rxq;
6736 	int rc, i, j;
6737 #ifdef RSS
6738 	int nbuckets = rss_getnumbuckets();
6739 	int hashconfig = rss_gethashconfig();
6740 	int extra;
6741 #endif
6742 
6743 	ASSERT_SYNCHRONIZED_OP(sc);
6744 
6745 	/*
6746 	 * Allocate tx/rx/fl queues for this VI.
6747 	 */
6748 	rc = t4_setup_vi_queues(vi);
6749 	if (rc != 0)
6750 		return (rc);
6751 
6752 	/*
6753 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
6754 	 */
6755 	if (vi->nrxq > vi->rss_size) {
6756 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
6757 		    "some queues will never receive traffic.\n", vi->nrxq,
6758 		    vi->rss_size);
6759 	} else if (vi->rss_size % vi->nrxq) {
6760 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
6761 		    "expect uneven traffic distribution.\n", vi->nrxq,
6762 		    vi->rss_size);
6763 	}
6764 #ifdef RSS
6765 	if (vi->nrxq != nbuckets) {
6766 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
6767 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
6768 	}
6769 #endif
6770 	if (vi->rss == NULL)
6771 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
6772 		    M_ZERO | M_WAITOK);
6773 	for (i = 0; i < vi->rss_size;) {
6774 #ifdef RSS
6775 		j = rss_get_indirection_to_bucket(i);
6776 		j %= vi->nrxq;
6777 		rxq = &sc->sge.rxq[vi->first_rxq + j];
6778 		vi->rss[i++] = rxq->iq.abs_id;
6779 #else
6780 		for_each_rxq(vi, j, rxq) {
6781 			vi->rss[i++] = rxq->iq.abs_id;
6782 			if (i == vi->rss_size)
6783 				break;
6784 		}
6785 #endif
6786 	}
6787 
6788 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
6789 	    vi->rss, vi->rss_size);
6790 	if (rc != 0) {
6791 		CH_ERR(vi, "rss_config failed: %d\n", rc);
6792 		return (rc);
6793 	}
6794 
6795 #ifdef RSS
6796 	vi->hashen = hashconfig_to_hashen(hashconfig);
6797 
6798 	/*
6799 	 * We may have had to enable some hashes even though the global config
6800 	 * wants them disabled.  This is a potential problem that must be
6801 	 * reported to the user.
6802 	 */
6803 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
6804 
6805 	/*
6806 	 * If we consider only the supported hash types, then the enabled hashes
6807 	 * are a superset of the requested hashes.  In other words, there cannot
6808 	 * be any supported hash that was requested but not enabled, but there
6809 	 * can be hashes that were not requested but had to be enabled.
6810 	 */
6811 	extra &= SUPPORTED_RSS_HASHTYPES;
6812 	MPASS((extra & hashconfig) == 0);
6813 
6814 	if (extra) {
6815 		CH_ALERT(vi,
6816 		    "global RSS config (0x%x) cannot be accommodated.\n",
6817 		    hashconfig);
6818 	}
6819 	if (extra & RSS_HASHTYPE_RSS_IPV4)
6820 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
6821 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
6822 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
6823 	if (extra & RSS_HASHTYPE_RSS_IPV6)
6824 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
6825 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
6826 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
6827 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
6828 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
6829 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
6830 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
6831 #else
6832 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
6833 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
6834 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6835 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
6836 #endif
6837 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
6838 	    0, 0);
6839 	if (rc != 0) {
6840 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
6841 		return (rc);
6842 	}
6843 
6844 	return (0);
6845 }
6846 
6847 int
vi_init(struct vi_info * vi)6848 vi_init(struct vi_info *vi)
6849 {
6850 	int rc;
6851 
6852 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
6853 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
6854 	    ("%s: VI_INIT_DONE already", __func__));
6855 
6856 	rc = vi_full_init(vi);
6857 	if (rc != 0)
6858 		vi_full_uninit(vi);
6859 	else
6860 		vi->flags |= VI_INIT_DONE;
6861 
6862 	return (rc);
6863 }
6864 
6865 /*
6866  * Idempotent.
6867  */
6868 static void
vi_full_uninit(struct vi_info * vi)6869 vi_full_uninit(struct vi_info *vi)
6870 {
6871 
6872 	if (vi->flags & VI_INIT_DONE) {
6873 		quiesce_vi(vi);
6874 		free(vi->rss, M_CXGBE);
6875 		free(vi->nm_rss, M_CXGBE);
6876 	}
6877 
6878 	t4_teardown_vi_queues(vi);
6879 	vi->flags &= ~VI_INIT_DONE;
6880 }
6881 
6882 static void
quiesce_txq(struct sge_txq * txq)6883 quiesce_txq(struct sge_txq *txq)
6884 {
6885 	struct sge_eq *eq = &txq->eq;
6886 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
6887 
6888 	MPASS(eq->flags & EQ_SW_ALLOCATED);
6889 	MPASS(!(eq->flags & EQ_ENABLED));
6890 
6891 	/* Wait for the mp_ring to empty. */
6892 	while (!mp_ring_is_idle(txq->r)) {
6893 		mp_ring_check_drainage(txq->r, 4096);
6894 		pause("rquiesce", 1);
6895 	}
6896 	MPASS(txq->txp.npkt == 0);
6897 
6898 	if (eq->flags & EQ_HW_ALLOCATED) {
6899 		/*
6900 		 * Hardware is alive and working normally.  Wait for it to
6901 		 * finish and then wait for the driver to catch up and reclaim
6902 		 * all descriptors.
6903 		 */
6904 		while (spg->cidx != htobe16(eq->pidx))
6905 			pause("equiesce", 1);
6906 		while (eq->cidx != eq->pidx)
6907 			pause("dquiesce", 1);
6908 	} else {
6909 		/*
6910 		 * Hardware is unavailable.  Discard all pending tx and reclaim
6911 		 * descriptors directly.
6912 		 */
6913 		TXQ_LOCK(txq);
6914 		while (eq->cidx != eq->pidx) {
6915 			struct mbuf *m, *nextpkt;
6916 			struct tx_sdesc *txsd;
6917 
6918 			txsd = &txq->sdesc[eq->cidx];
6919 			for (m = txsd->m; m != NULL; m = nextpkt) {
6920 				nextpkt = m->m_nextpkt;
6921 				m->m_nextpkt = NULL;
6922 				m_freem(m);
6923 			}
6924 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
6925 		}
6926 		spg->pidx = spg->cidx = htobe16(eq->cidx);
6927 		TXQ_UNLOCK(txq);
6928 	}
6929 }
6930 
6931 static void
quiesce_wrq(struct sge_wrq * wrq)6932 quiesce_wrq(struct sge_wrq *wrq)
6933 {
6934 
6935 	/* XXXTX */
6936 }
6937 
6938 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)6939 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
6940 {
6941 	/* Synchronize with the interrupt handler */
6942 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
6943 		pause("iqfree", 1);
6944 
6945 	if (fl != NULL) {
6946 		MPASS(iq->flags & IQ_HAS_FL);
6947 
6948 		mtx_lock(&sc->sfl_lock);
6949 		FL_LOCK(fl);
6950 		fl->flags |= FL_DOOMED;
6951 		FL_UNLOCK(fl);
6952 		callout_stop(&sc->sfl_callout);
6953 		mtx_unlock(&sc->sfl_lock);
6954 
6955 		KASSERT((fl->flags & FL_STARVING) == 0,
6956 		    ("%s: still starving", __func__));
6957 
6958 		/* Release all buffers if hardware is no longer available. */
6959 		if (!(iq->flags & IQ_HW_ALLOCATED))
6960 			free_fl_buffers(sc, fl);
6961 	}
6962 }
6963 
6964 /*
6965  * Wait for all activity on all the queues of the VI to complete.  It is assumed
6966  * that no new work is being enqueued by the hardware or the driver.  That part
6967  * should be arranged before calling this function.
6968  */
6969 static void
quiesce_vi(struct vi_info * vi)6970 quiesce_vi(struct vi_info *vi)
6971 {
6972 	int i;
6973 	struct adapter *sc = vi->adapter;
6974 	struct sge_rxq *rxq;
6975 	struct sge_txq *txq;
6976 #ifdef TCP_OFFLOAD
6977 	struct sge_ofld_rxq *ofld_rxq;
6978 #endif
6979 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
6980 	struct sge_ofld_txq *ofld_txq;
6981 #endif
6982 
6983 	if (!(vi->flags & VI_INIT_DONE))
6984 		return;
6985 
6986 	for_each_txq(vi, i, txq) {
6987 		quiesce_txq(txq);
6988 	}
6989 
6990 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
6991 	for_each_ofld_txq(vi, i, ofld_txq) {
6992 		quiesce_wrq(&ofld_txq->wrq);
6993 	}
6994 #endif
6995 
6996 	for_each_rxq(vi, i, rxq) {
6997 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
6998 	}
6999 
7000 #ifdef TCP_OFFLOAD
7001 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7002 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7003 	}
7004 #endif
7005 }
7006 
7007 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7008 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7009     driver_intr_t *handler, void *arg, char *name)
7010 {
7011 	int rc;
7012 
7013 	irq->rid = rid;
7014 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7015 	    RF_SHAREABLE | RF_ACTIVE);
7016 	if (irq->res == NULL) {
7017 		device_printf(sc->dev,
7018 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7019 		return (ENOMEM);
7020 	}
7021 
7022 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7023 	    NULL, handler, arg, &irq->tag);
7024 	if (rc != 0) {
7025 		device_printf(sc->dev,
7026 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7027 		    rid, name, rc);
7028 	} else if (name)
7029 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7030 
7031 	return (rc);
7032 }
7033 
7034 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7035 t4_free_irq(struct adapter *sc, struct irq *irq)
7036 {
7037 	if (irq->tag)
7038 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7039 	if (irq->res)
7040 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7041 
7042 	bzero(irq, sizeof(*irq));
7043 
7044 	return (0);
7045 }
7046 
7047 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7048 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7049 {
7050 
7051 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7052 	t4_get_regs(sc, buf, regs->len);
7053 }
7054 
7055 #define	A_PL_INDIR_CMD	0x1f8
7056 
7057 #define	S_PL_AUTOINC	31
7058 #define	M_PL_AUTOINC	0x1U
7059 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7060 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7061 
7062 #define	S_PL_VFID	20
7063 #define	M_PL_VFID	0xffU
7064 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7065 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7066 
7067 #define	S_PL_ADDR	0
7068 #define	M_PL_ADDR	0xfffffU
7069 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7070 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7071 
7072 #define	A_PL_INDIR_DATA	0x1fc
7073 
7074 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7075 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7076 {
7077 	u32 stats[2];
7078 
7079 	if (sc->flags & IS_VF) {
7080 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7081 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7082 	} else {
7083 		mtx_assert(&sc->reg_lock, MA_OWNED);
7084 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7085 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7086 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7087 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7088 	}
7089 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7090 }
7091 
7092 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7093 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7094 {
7095 
7096 #define GET_STAT(name) \
7097 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7098 
7099 	if (!(sc->flags & IS_VF))
7100 		mtx_lock(&sc->reg_lock);
7101 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7102 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7103 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7104 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7105 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7106 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7107 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7108 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7109 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7110 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7111 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7112 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7113 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7114 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7115 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7116 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7117 	if (!(sc->flags & IS_VF))
7118 		mtx_unlock(&sc->reg_lock);
7119 
7120 #undef GET_STAT
7121 }
7122 
7123 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7124 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7125 {
7126 	int reg;
7127 
7128 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7129 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7130 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7131 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7132 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7133 }
7134 
7135 static void
vi_refresh_stats(struct vi_info * vi)7136 vi_refresh_stats(struct vi_info *vi)
7137 {
7138 	struct timeval tv;
7139 	const struct timeval interval = {0, 250000};	/* 250ms */
7140 
7141 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7142 
7143 	if (vi->flags & VI_SKIP_STATS)
7144 		return;
7145 
7146 	getmicrotime(&tv);
7147 	timevalsub(&tv, &interval);
7148 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7149 		return;
7150 
7151 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7152 	getmicrotime(&vi->last_refreshed);
7153 }
7154 
7155 static void
cxgbe_refresh_stats(struct vi_info * vi)7156 cxgbe_refresh_stats(struct vi_info *vi)
7157 {
7158 	u_int i, v, tnl_cong_drops, chan_map;
7159 	struct timeval tv;
7160 	const struct timeval interval = {0, 250000};	/* 250ms */
7161 	struct port_info *pi;
7162 	struct adapter *sc;
7163 
7164 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7165 
7166 	if (vi->flags & VI_SKIP_STATS)
7167 		return;
7168 
7169 	getmicrotime(&tv);
7170 	timevalsub(&tv, &interval);
7171 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7172 		return;
7173 
7174 	pi = vi->pi;
7175 	sc = vi->adapter;
7176 	tnl_cong_drops = 0;
7177 	t4_get_port_stats(sc, pi->port_id, &pi->stats);
7178 	chan_map = pi->rx_e_chan_map;
7179 	while (chan_map) {
7180 		i = ffs(chan_map) - 1;
7181 		mtx_lock(&sc->reg_lock);
7182 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7183 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7184 		mtx_unlock(&sc->reg_lock);
7185 		tnl_cong_drops += v;
7186 		chan_map &= ~(1 << i);
7187 	}
7188 	pi->tnl_cong_drops = tnl_cong_drops;
7189 	getmicrotime(&vi->last_refreshed);
7190 }
7191 
7192 static void
cxgbe_tick(void * arg)7193 cxgbe_tick(void *arg)
7194 {
7195 	struct vi_info *vi = arg;
7196 
7197 	MPASS(IS_MAIN_VI(vi));
7198 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7199 
7200 	cxgbe_refresh_stats(vi);
7201 	callout_schedule(&vi->tick, hz);
7202 }
7203 
7204 static void
vi_tick(void * arg)7205 vi_tick(void *arg)
7206 {
7207 	struct vi_info *vi = arg;
7208 
7209 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7210 
7211 	vi_refresh_stats(vi);
7212 	callout_schedule(&vi->tick, hz);
7213 }
7214 
7215 /*
7216  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7217  */
7218 static char *caps_decoder[] = {
7219 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7220 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7221 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7222 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7223 	    "\006HASHFILTER\007ETHOFLD",
7224 	"\20\001TOE",					/* 4: TOE */
7225 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
7226 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7227 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7228 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7229 	    "\007T10DIF"
7230 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7231 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7232 	    "\004TLS_HW",
7233 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7234 		    "\004PO_INITIATOR\005PO_TARGET",
7235 };
7236 
7237 void
t4_sysctls(struct adapter * sc)7238 t4_sysctls(struct adapter *sc)
7239 {
7240 	struct sysctl_ctx_list *ctx = &sc->ctx;
7241 	struct sysctl_oid *oid;
7242 	struct sysctl_oid_list *children, *c0;
7243 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7244 
7245 	/*
7246 	 * dev.t4nex.X.
7247 	 */
7248 	oid = device_get_sysctl_tree(sc->dev);
7249 	c0 = children = SYSCTL_CHILDREN(oid);
7250 
7251 	sc->sc_do_rxcopy = 1;
7252 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7253 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7254 
7255 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7256 	    sc->params.nports, "# of ports");
7257 
7258 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7259 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7260 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7261 	    "available doorbells");
7262 
7263 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7264 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7265 
7266 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7267 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7268 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7269 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7270 
7271 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7272 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7273 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7274 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7275 
7276 	t4_sge_sysctls(sc, ctx, children);
7277 
7278 	sc->lro_timeout = 100;
7279 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7280 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7281 
7282 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7283 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7284 
7285 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7286 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7287 
7288 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7289 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7290 
7291 	if (sc->flags & IS_VF)
7292 		return;
7293 
7294 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7295 	    NULL, chip_rev(sc), "chip hardware revision");
7296 
7297 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7298 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7299 
7300 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7301 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7302 
7303 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7304 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7305 
7306 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7307 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7308 
7309 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7310 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7311 
7312 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7313 	    sc->er_version, 0, "expansion ROM version");
7314 
7315 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7316 	    sc->bs_version, 0, "bootstrap firmware version");
7317 
7318 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7319 	    NULL, sc->params.scfg_vers, "serial config version");
7320 
7321 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7322 	    NULL, sc->params.vpd_vers, "VPD version");
7323 
7324 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7325 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7326 
7327 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7328 	    sc->cfcsum, "config file checksum");
7329 
7330 #define SYSCTL_CAP(name, n, text) \
7331 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7332 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7333 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7334 	    "available " text " capabilities")
7335 
7336 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7337 	SYSCTL_CAP(linkcaps, 1, "link");
7338 	SYSCTL_CAP(switchcaps, 2, "switch");
7339 	SYSCTL_CAP(niccaps, 3, "NIC");
7340 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7341 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7342 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7343 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7344 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7345 #undef SYSCTL_CAP
7346 
7347 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7348 	    NULL, sc->tids.nftids, "number of filters");
7349 
7350 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7351 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7352 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7353 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7354 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7355 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7356 
7357 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg",
7358 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7359 	    sysctl_loadavg, "A",
7360 	    "microprocessor load averages (debug firmwares only)");
7361 
7362 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7363 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7364 	    "I", "core Vdd (in mV)");
7365 
7366 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7367 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7368 	    sysctl_cpus, "A", "local CPUs");
7369 
7370 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7371 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7372 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7373 
7374 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7375 	    &sc->swintr, 0, "software triggered interrupts");
7376 
7377 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7378 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7379 	    "1 = reset adapter, 0 = zero reset counter");
7380 
7381 	/*
7382 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7383 	 */
7384 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7385 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
7386 	    "logs and miscellaneous information");
7387 	children = SYSCTL_CHILDREN(oid);
7388 
7389 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
7390 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7391 	    sysctl_cctrl, "A", "congestion control");
7392 
7393 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
7394 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7395 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
7396 
7397 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
7398 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7399 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
7400 
7401 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
7402 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7403 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
7404 
7405 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
7406 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 3,
7407 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
7408 
7409 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
7410 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 4,
7411 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
7412 
7413 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
7414 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 5,
7415 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
7416 
7417 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
7418 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7419 	    sysctl_cim_la, "A", "CIM logic analyzer");
7420 
7421 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
7422 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7423 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7424 
7425 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
7426 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7427 	    0 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
7428 
7429 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
7430 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7431 	    1 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
7432 
7433 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
7434 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7435 	    2 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
7436 
7437 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
7438 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7439 	    3 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
7440 
7441 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
7442 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7443 	    4 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
7444 
7445 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
7446 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7447 	    5 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
7448 
7449 	if (chip_id(sc) > CHELSIO_T4) {
7450 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
7451 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7452 		    6 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7453 		    "CIM OBQ 6 (SGE0-RX)");
7454 
7455 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
7456 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7457 		    7 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7458 		    "CIM OBQ 7 (SGE1-RX)");
7459 	}
7460 
7461 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
7462 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7463 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7464 
7465 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
7466 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7467 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
7468 
7469 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
7470 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7471 	    sysctl_cpl_stats, "A", "CPL statistics");
7472 
7473 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
7474 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7475 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
7476 
7477 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
7478 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7479 	    sysctl_tid_stats, "A", "tid stats");
7480 
7481 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
7482 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7483 	    sysctl_devlog, "A", "firmware's device log");
7484 
7485 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
7486 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7487 	    sysctl_fcoe_stats, "A", "FCoE statistics");
7488 
7489 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
7490 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7491 	    sysctl_hw_sched, "A", "hardware scheduler ");
7492 
7493 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
7494 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7495 	    sysctl_l2t, "A", "hardware L2 table");
7496 
7497 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
7498 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7499 	    sysctl_smt, "A", "hardware source MAC table");
7500 
7501 #ifdef INET6
7502 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
7503 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7504 	    sysctl_clip, "A", "active CLIP table entries");
7505 #endif
7506 
7507 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
7508 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7509 	    sysctl_lb_stats, "A", "loopback statistics");
7510 
7511 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
7512 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7513 	    sysctl_meminfo, "A", "memory regions");
7514 
7515 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
7516 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7517 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
7518 	    "A", "MPS TCAM entries");
7519 
7520 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
7521 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7522 	    sysctl_path_mtus, "A", "path MTUs");
7523 
7524 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
7525 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7526 	    sysctl_pm_stats, "A", "PM statistics");
7527 
7528 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
7529 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7530 	    sysctl_rdma_stats, "A", "RDMA statistics");
7531 
7532 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
7533 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7534 	    sysctl_tcp_stats, "A", "TCP statistics");
7535 
7536 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
7537 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7538 	    sysctl_tids, "A", "TID information");
7539 
7540 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
7541 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7542 	    sysctl_tp_err_stats, "A", "TP error statistics");
7543 
7544 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
7545 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7546 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
7547 
7548 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
7549 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7550 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
7551 
7552 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
7553 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7554 	    sysctl_tp_la, "A", "TP logic analyzer");
7555 
7556 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
7557 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7558 	    sysctl_tx_rate, "A", "Tx rate");
7559 
7560 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
7561 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7562 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
7563 
7564 	if (chip_id(sc) >= CHELSIO_T5) {
7565 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
7566 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7567 		    sysctl_wcwr_stats, "A", "write combined work requests");
7568 	}
7569 
7570 #ifdef KERN_TLS
7571 	if (is_ktls(sc)) {
7572 		/*
7573 		 * dev.t4nex.0.tls.
7574 		 */
7575 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
7576 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
7577 		children = SYSCTL_CHILDREN(oid);
7578 
7579 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
7580 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
7581 		    "keys in work requests (1) or attempt to store TLS keys "
7582 		    "in card memory.");
7583 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
7584 		    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to combine "
7585 		    "TCB field updates with TLS record work requests.");
7586 	}
7587 #endif
7588 
7589 #ifdef TCP_OFFLOAD
7590 	if (is_offload(sc)) {
7591 		int i;
7592 		char s[4];
7593 
7594 		/*
7595 		 * dev.t4nex.X.toe.
7596 		 */
7597 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
7598 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
7599 		children = SYSCTL_CHILDREN(oid);
7600 
7601 		sc->tt.cong_algorithm = -1;
7602 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
7603 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
7604 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
7605 		    "3 = highspeed)");
7606 
7607 		sc->tt.sndbuf = -1;
7608 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
7609 		    &sc->tt.sndbuf, 0, "hardware send buffer");
7610 
7611 		sc->tt.ddp = 0;
7612 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
7613 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
7614 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
7615 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
7616 
7617 		sc->tt.rx_coalesce = -1;
7618 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
7619 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
7620 
7621 		sc->tt.tls = 0;
7622 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
7623 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
7624 		    "Inline TLS allowed");
7625 
7626 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls_rx_ports",
7627 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7628 		    sysctl_tls_rx_ports, "I",
7629 		    "TCP ports that use inline TLS+TOE RX");
7630 
7631 		sc->tt.tls_rx_timeout = t4_toe_tls_rx_timeout;
7632 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls_rx_timeout",
7633 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7634 		    sysctl_tls_rx_timeout, "I",
7635 		    "Timeout in seconds to downgrade TLS sockets to plain TOE");
7636 
7637 		sc->tt.tx_align = -1;
7638 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
7639 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
7640 
7641 		sc->tt.tx_zcopy = 0;
7642 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
7643 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
7644 		    "Enable zero-copy aio_write(2)");
7645 
7646 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
7647 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7648 		    "cop_managed_offloading", CTLFLAG_RW,
7649 		    &sc->tt.cop_managed_offloading, 0,
7650 		    "COP (Connection Offload Policy) controls all TOE offload");
7651 
7652 		sc->tt.autorcvbuf_inc = 16 * 1024;
7653 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
7654 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
7655 		    "autorcvbuf increment");
7656 
7657 		sc->tt.iso = 1;
7658 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
7659 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
7660 
7661 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
7662 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7663 		    sysctl_tp_tick, "A", "TP timer tick (us)");
7664 
7665 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
7666 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7667 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
7668 
7669 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
7670 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7671 		    sysctl_tp_tick, "A", "DACK tick (us)");
7672 
7673 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
7674 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7675 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
7676 
7677 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
7678 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7679 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
7680 		    "Minimum retransmit interval (us)");
7681 
7682 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
7683 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7684 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
7685 		    "Maximum retransmit interval (us)");
7686 
7687 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
7688 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7689 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
7690 		    "Persist timer min (us)");
7691 
7692 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
7693 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7694 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
7695 		    "Persist timer max (us)");
7696 
7697 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
7698 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7699 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
7700 		    "Keepalive idle timer (us)");
7701 
7702 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
7703 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7704 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
7705 		    "Keepalive interval timer (us)");
7706 
7707 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
7708 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7709 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
7710 
7711 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
7712 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7713 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
7714 		    "FINWAIT2 timer (us)");
7715 
7716 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
7717 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7718 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
7719 		    "Number of SYN retransmissions before abort");
7720 
7721 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
7722 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7723 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
7724 		    "Number of retransmissions before abort");
7725 
7726 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
7727 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7728 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
7729 		    "Number of keepalive probes before abort");
7730 
7731 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
7732 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7733 		    "TOE retransmit backoffs");
7734 		children = SYSCTL_CHILDREN(oid);
7735 		for (i = 0; i < 16; i++) {
7736 			snprintf(s, sizeof(s), "%u", i);
7737 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
7738 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7739 			    i, sysctl_tp_backoff, "IU",
7740 			    "TOE retransmit backoff");
7741 		}
7742 	}
7743 #endif
7744 }
7745 
7746 void
vi_sysctls(struct vi_info * vi)7747 vi_sysctls(struct vi_info *vi)
7748 {
7749 	struct sysctl_ctx_list *ctx = &vi->ctx;
7750 	struct sysctl_oid *oid;
7751 	struct sysctl_oid_list *children;
7752 
7753 	/*
7754 	 * dev.v?(cxgbe|cxl).X.
7755 	 */
7756 	oid = device_get_sysctl_tree(vi->dev);
7757 	children = SYSCTL_CHILDREN(oid);
7758 
7759 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
7760 	    vi->viid, "VI identifer");
7761 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
7762 	    &vi->nrxq, 0, "# of rx queues");
7763 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
7764 	    &vi->ntxq, 0, "# of tx queues");
7765 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
7766 	    &vi->first_rxq, 0, "index of first rx queue");
7767 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
7768 	    &vi->first_txq, 0, "index of first tx queue");
7769 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
7770 	    vi->rss_base, "start of RSS indirection table");
7771 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
7772 	    vi->rss_size, "size of RSS indirection table");
7773 
7774 	if (IS_MAIN_VI(vi)) {
7775 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
7776 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7777 		    sysctl_noflowq, "IU",
7778 		    "Reserve queue 0 for non-flowid packets");
7779 	}
7780 
7781 	if (vi->adapter->flags & IS_VF) {
7782 		MPASS(vi->flags & TX_USES_VM_WR);
7783 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
7784 		    NULL, 1, "use VM work requests for transmit");
7785 	} else {
7786 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
7787 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7788 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
7789 	}
7790 
7791 #ifdef TCP_OFFLOAD
7792 	if (vi->nofldrxq != 0) {
7793 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
7794 		    &vi->nofldrxq, 0,
7795 		    "# of rx queues for offloaded TCP connections");
7796 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
7797 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
7798 		    "index of first TOE rx queue");
7799 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
7800 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7801 		    sysctl_holdoff_tmr_idx_ofld, "I",
7802 		    "holdoff timer index for TOE queues");
7803 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
7804 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7805 		    sysctl_holdoff_pktc_idx_ofld, "I",
7806 		    "holdoff packet counter index for TOE queues");
7807 	}
7808 #endif
7809 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7810 	if (vi->nofldtxq != 0) {
7811 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
7812 		    &vi->nofldtxq, 0,
7813 		    "# of tx queues for TOE/ETHOFLD");
7814 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
7815 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
7816 		    "index of first TOE/ETHOFLD tx queue");
7817 	}
7818 #endif
7819 #ifdef DEV_NETMAP
7820 	if (vi->nnmrxq != 0) {
7821 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
7822 		    &vi->nnmrxq, 0, "# of netmap rx queues");
7823 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
7824 		    &vi->nnmtxq, 0, "# of netmap tx queues");
7825 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
7826 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
7827 		    "index of first netmap rx queue");
7828 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
7829 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
7830 		    "index of first netmap tx queue");
7831 	}
7832 #endif
7833 
7834 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
7835 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7836 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
7837 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
7838 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7839 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
7840 
7841 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
7842 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7843 	    sysctl_qsize_rxq, "I", "rx queue size");
7844 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
7845 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7846 	    sysctl_qsize_txq, "I", "tx queue size");
7847 }
7848 
7849 static void
cxgbe_sysctls(struct port_info * pi)7850 cxgbe_sysctls(struct port_info *pi)
7851 {
7852 	struct sysctl_ctx_list *ctx = &pi->ctx;
7853 	struct sysctl_oid *oid;
7854 	struct sysctl_oid_list *children, *children2;
7855 	struct adapter *sc = pi->adapter;
7856 	int i;
7857 	char name[16];
7858 	static char *tc_flags = {"\20\1USER"};
7859 
7860 	/*
7861 	 * dev.cxgbe.X.
7862 	 */
7863 	oid = device_get_sysctl_tree(pi->dev);
7864 	children = SYSCTL_CHILDREN(oid);
7865 
7866 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
7867 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
7868 	    sysctl_linkdnrc, "A", "reason why link is down");
7869 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
7870 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7871 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
7872 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
7873 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
7874 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
7875 		    sysctl_btphy, "I", "PHY firmware version");
7876 	}
7877 
7878 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
7879 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
7880 	    sysctl_pause_settings, "A",
7881 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
7882 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
7883 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
7884 	    "FEC in use on the link");
7885 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
7886 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
7887 	    sysctl_requested_fec, "A",
7888 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
7889 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
7890 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
7891 	    "FEC recommended by the cable/transceiver");
7892 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
7893 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
7894 	    sysctl_autoneg, "I",
7895 	    "autonegotiation (-1 = not supported)");
7896 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
7897 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
7898 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
7899 
7900 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
7901 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
7902 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
7903 	    &pi->link_cfg.pcaps, 0, "port capabilities");
7904 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
7905 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
7906 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
7907 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
7908 
7909 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
7910 	    port_top_speed(pi), "max speed (in Gbps)");
7911 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
7912 	    pi->mps_bg_map, "MPS buffer group map");
7913 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
7914 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
7915 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_c_chan", CTLFLAG_RD, NULL,
7916 	    pi->rx_c_chan, "TP rx c-channel");
7917 
7918 	if (sc->flags & IS_VF)
7919 		return;
7920 
7921 	/*
7922 	 * dev.(cxgbe|cxl).X.tc.
7923 	 */
7924 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
7925 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7926 	    "Tx scheduler traffic classes (cl_rl)");
7927 	children2 = SYSCTL_CHILDREN(oid);
7928 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
7929 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
7930 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
7931 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
7932 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
7933 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
7934 	for (i = 0; i < sc->params.nsched_cls; i++) {
7935 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
7936 
7937 		snprintf(name, sizeof(name), "%d", i);
7938 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
7939 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
7940 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
7941 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
7942 		    CTLFLAG_RD, &tc->state, 0, "current state");
7943 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
7944 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
7945 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
7946 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
7947 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
7948 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
7949 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7950 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
7951 		    "traffic class parameters");
7952 	}
7953 
7954 	/*
7955 	 * dev.cxgbe.X.stats.
7956 	 */
7957 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
7958 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
7959 	children = SYSCTL_CHILDREN(oid);
7960 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
7961 	    &pi->tx_parse_error, 0,
7962 	    "# of tx packets with invalid length or # of segments");
7963 
7964 #define T4_REGSTAT(name, stat, desc) \
7965     SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
7966         CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
7967 	(is_t4(sc) ? PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_##stat##_L) : \
7968 	T5_PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_##stat##_L)), \
7969         sysctl_handle_t4_reg64, "QU", desc)
7970 
7971 /* We get these from port_stats and they may be stale by up to 1s */
7972 #define T4_PORTSTAT(name, desc) \
7973 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
7974 	    &pi->stats.name, desc)
7975 
7976 	T4_REGSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
7977 	T4_REGSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
7978 	T4_REGSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
7979 	T4_REGSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
7980 	T4_REGSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
7981 	T4_REGSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
7982 	T4_REGSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
7983 	T4_REGSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
7984 	T4_REGSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
7985 	T4_REGSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
7986 	T4_REGSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
7987 	T4_REGSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
7988 	T4_REGSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
7989 	T4_REGSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
7990 	T4_REGSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
7991 	T4_REGSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
7992 	T4_REGSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
7993 	T4_REGSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
7994 	T4_REGSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
7995 	T4_REGSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
7996 	T4_REGSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
7997 	T4_REGSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
7998 	T4_REGSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
7999 
8000 	T4_REGSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8001 	T4_REGSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8002 	T4_REGSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8003 	T4_REGSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8004 	T4_REGSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8005 	T4_REGSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8006 	T4_REGSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8007 	if (is_t6(sc)) {
8008 		T4_PORTSTAT(rx_fcs_err,
8009 		    "# of frames received with bad FCS since last link up");
8010 	} else {
8011 		T4_REGSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8012 		    "# of frames received with bad FCS");
8013 	}
8014 	T4_REGSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8015 	T4_REGSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8016 	T4_REGSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8017 	T4_REGSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8018 	T4_REGSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8019 	T4_REGSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8020 	T4_REGSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8021 	T4_REGSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8022 	T4_REGSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8023 	T4_REGSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8024 	T4_REGSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8025 	T4_REGSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8026 	T4_REGSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8027 	T4_REGSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8028 	T4_REGSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8029 	T4_REGSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8030 	T4_REGSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8031 	T4_REGSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8032 	T4_REGSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8033 
8034 	T4_PORTSTAT(rx_ovflow0, "# drops due to buffer-group 0 overflows");
8035 	T4_PORTSTAT(rx_ovflow1, "# drops due to buffer-group 1 overflows");
8036 	T4_PORTSTAT(rx_ovflow2, "# drops due to buffer-group 2 overflows");
8037 	T4_PORTSTAT(rx_ovflow3, "# drops due to buffer-group 3 overflows");
8038 	T4_PORTSTAT(rx_trunc0, "# of buffer-group 0 truncated packets");
8039 	T4_PORTSTAT(rx_trunc1, "# of buffer-group 1 truncated packets");
8040 	T4_PORTSTAT(rx_trunc2, "# of buffer-group 2 truncated packets");
8041 	T4_PORTSTAT(rx_trunc3, "# of buffer-group 3 truncated packets");
8042 
8043 #undef T4_REGSTAT
8044 #undef T4_PORTSTAT
8045 }
8046 
8047 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8048 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8049 {
8050 	int rc, *i, space = 0;
8051 	struct sbuf sb;
8052 
8053 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8054 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8055 		if (space)
8056 			sbuf_printf(&sb, " ");
8057 		sbuf_printf(&sb, "%d", *i);
8058 		space = 1;
8059 	}
8060 	rc = sbuf_finish(&sb);
8061 	sbuf_delete(&sb);
8062 	return (rc);
8063 }
8064 
8065 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8066 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8067 {
8068 	int rc;
8069 	struct sbuf *sb;
8070 
8071 	rc = sysctl_wire_old_buffer(req, 0);
8072 	if (rc != 0)
8073 		return(rc);
8074 
8075 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8076 	if (sb == NULL)
8077 		return (ENOMEM);
8078 
8079 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8080 	rc = sbuf_finish(sb);
8081 	sbuf_delete(sb);
8082 
8083 	return (rc);
8084 }
8085 
8086 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8087 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8088 {
8089 	int rc;
8090 	struct sbuf *sb;
8091 
8092 	rc = sysctl_wire_old_buffer(req, 0);
8093 	if (rc != 0)
8094 		return(rc);
8095 
8096 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8097 	if (sb == NULL)
8098 		return (ENOMEM);
8099 
8100 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8101 	rc = sbuf_finish(sb);
8102 	sbuf_delete(sb);
8103 
8104 	return (rc);
8105 }
8106 
8107 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8108 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8109 {
8110 	struct port_info *pi = arg1;
8111 	int op = arg2;
8112 	struct adapter *sc = pi->adapter;
8113 	u_int v;
8114 	int rc;
8115 
8116 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8117 	if (rc)
8118 		return (rc);
8119 	if (hw_off_limits(sc))
8120 		rc = ENXIO;
8121 	else {
8122 		/* XXX: magic numbers */
8123 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8124 		    op ? 0x20 : 0xc820, &v);
8125 	}
8126 	end_synchronized_op(sc, 0);
8127 	if (rc)
8128 		return (rc);
8129 	if (op == 0)
8130 		v /= 256;
8131 
8132 	rc = sysctl_handle_int(oidp, &v, 0, req);
8133 	return (rc);
8134 }
8135 
8136 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8137 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8138 {
8139 	struct vi_info *vi = arg1;
8140 	int rc, val;
8141 
8142 	val = vi->rsrv_noflowq;
8143 	rc = sysctl_handle_int(oidp, &val, 0, req);
8144 	if (rc != 0 || req->newptr == NULL)
8145 		return (rc);
8146 
8147 	if ((val >= 1) && (vi->ntxq > 1))
8148 		vi->rsrv_noflowq = 1;
8149 	else
8150 		vi->rsrv_noflowq = 0;
8151 
8152 	return (rc);
8153 }
8154 
8155 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8156 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8157 {
8158 	struct vi_info *vi = arg1;
8159 	struct adapter *sc = vi->adapter;
8160 	int rc, val, i;
8161 
8162 	MPASS(!(sc->flags & IS_VF));
8163 
8164 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8165 	rc = sysctl_handle_int(oidp, &val, 0, req);
8166 	if (rc != 0 || req->newptr == NULL)
8167 		return (rc);
8168 
8169 	if (val != 0 && val != 1)
8170 		return (EINVAL);
8171 
8172 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8173 	    "t4txvm");
8174 	if (rc)
8175 		return (rc);
8176 	if (hw_off_limits(sc))
8177 		rc = ENXIO;
8178 	else if (vi->ifp->if_drv_flags & IFF_DRV_RUNNING) {
8179 		/*
8180 		 * We don't want parse_pkt to run with one setting (VF or PF)
8181 		 * and then eth_tx to see a different setting but still use
8182 		 * stale information calculated by parse_pkt.
8183 		 */
8184 		rc = EBUSY;
8185 	} else {
8186 		struct port_info *pi = vi->pi;
8187 		struct sge_txq *txq;
8188 		uint32_t ctrl0;
8189 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8190 
8191 		if (val) {
8192 			vi->flags |= TX_USES_VM_WR;
8193 			vi->ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_VM_TSO;
8194 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8195 			    V_TXPKT_INTF(pi->tx_chan));
8196 			if (!(sc->flags & IS_VF))
8197 				npkt--;
8198 		} else {
8199 			vi->flags &= ~TX_USES_VM_WR;
8200 			vi->ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_TSO;
8201 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8202 			    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
8203 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8204 		}
8205 		for_each_txq(vi, i, txq) {
8206 			txq->cpl_ctrl0 = ctrl0;
8207 			txq->txp.max_npkt = npkt;
8208 		}
8209 	}
8210 	end_synchronized_op(sc, LOCK_HELD);
8211 	return (rc);
8212 }
8213 
8214 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8215 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8216 {
8217 	struct vi_info *vi = arg1;
8218 	struct adapter *sc = vi->adapter;
8219 	int idx, rc, i;
8220 	struct sge_rxq *rxq;
8221 	uint8_t v;
8222 
8223 	idx = vi->tmr_idx;
8224 
8225 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8226 	if (rc != 0 || req->newptr == NULL)
8227 		return (rc);
8228 
8229 	if (idx < 0 || idx >= SGE_NTIMERS)
8230 		return (EINVAL);
8231 
8232 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8233 	    "t4tmr");
8234 	if (rc)
8235 		return (rc);
8236 
8237 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8238 	for_each_rxq(vi, i, rxq) {
8239 #ifdef atomic_store_rel_8
8240 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8241 #else
8242 		rxq->iq.intr_params = v;
8243 #endif
8244 	}
8245 	vi->tmr_idx = idx;
8246 
8247 	end_synchronized_op(sc, LOCK_HELD);
8248 	return (0);
8249 }
8250 
8251 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8252 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8253 {
8254 	struct vi_info *vi = arg1;
8255 	struct adapter *sc = vi->adapter;
8256 	int idx, rc;
8257 
8258 	idx = vi->pktc_idx;
8259 
8260 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8261 	if (rc != 0 || req->newptr == NULL)
8262 		return (rc);
8263 
8264 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8265 		return (EINVAL);
8266 
8267 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8268 	    "t4pktc");
8269 	if (rc)
8270 		return (rc);
8271 
8272 	if (vi->flags & VI_INIT_DONE)
8273 		rc = EBUSY; /* cannot be changed once the queues are created */
8274 	else
8275 		vi->pktc_idx = idx;
8276 
8277 	end_synchronized_op(sc, LOCK_HELD);
8278 	return (rc);
8279 }
8280 
8281 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8282 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8283 {
8284 	struct vi_info *vi = arg1;
8285 	struct adapter *sc = vi->adapter;
8286 	int qsize, rc;
8287 
8288 	qsize = vi->qsize_rxq;
8289 
8290 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8291 	if (rc != 0 || req->newptr == NULL)
8292 		return (rc);
8293 
8294 	if (qsize < 128 || (qsize & 7))
8295 		return (EINVAL);
8296 
8297 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8298 	    "t4rxqs");
8299 	if (rc)
8300 		return (rc);
8301 
8302 	if (vi->flags & VI_INIT_DONE)
8303 		rc = EBUSY; /* cannot be changed once the queues are created */
8304 	else
8305 		vi->qsize_rxq = qsize;
8306 
8307 	end_synchronized_op(sc, LOCK_HELD);
8308 	return (rc);
8309 }
8310 
8311 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8312 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8313 {
8314 	struct vi_info *vi = arg1;
8315 	struct adapter *sc = vi->adapter;
8316 	int qsize, rc;
8317 
8318 	qsize = vi->qsize_txq;
8319 
8320 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8321 	if (rc != 0 || req->newptr == NULL)
8322 		return (rc);
8323 
8324 	if (qsize < 128 || qsize > 65536)
8325 		return (EINVAL);
8326 
8327 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8328 	    "t4txqs");
8329 	if (rc)
8330 		return (rc);
8331 
8332 	if (vi->flags & VI_INIT_DONE)
8333 		rc = EBUSY; /* cannot be changed once the queues are created */
8334 	else
8335 		vi->qsize_txq = qsize;
8336 
8337 	end_synchronized_op(sc, LOCK_HELD);
8338 	return (rc);
8339 }
8340 
8341 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8342 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8343 {
8344 	struct port_info *pi = arg1;
8345 	struct adapter *sc = pi->adapter;
8346 	struct link_config *lc = &pi->link_cfg;
8347 	int rc;
8348 
8349 	if (req->newptr == NULL) {
8350 		struct sbuf *sb;
8351 		static char *bits = "\20\1RX\2TX\3AUTO";
8352 
8353 		rc = sysctl_wire_old_buffer(req, 0);
8354 		if (rc != 0)
8355 			return(rc);
8356 
8357 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8358 		if (sb == NULL)
8359 			return (ENOMEM);
8360 
8361 		if (lc->link_ok) {
8362 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8363 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8364 		} else {
8365 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8366 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8367 		}
8368 		rc = sbuf_finish(sb);
8369 		sbuf_delete(sb);
8370 	} else {
8371 		char s[2];
8372 		int n;
8373 
8374 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8375 		    PAUSE_AUTONEG));
8376 		s[1] = 0;
8377 
8378 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8379 		if (rc != 0)
8380 			return(rc);
8381 
8382 		if (s[1] != 0)
8383 			return (EINVAL);
8384 		if (s[0] < '0' || s[0] > '9')
8385 			return (EINVAL);	/* not a number */
8386 		n = s[0] - '0';
8387 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8388 			return (EINVAL);	/* some other bit is set too */
8389 
8390 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8391 		    "t4PAUSE");
8392 		if (rc)
8393 			return (rc);
8394 		if (!hw_off_limits(sc)) {
8395 			PORT_LOCK(pi);
8396 			lc->requested_fc = n;
8397 			fixup_link_config(pi);
8398 			if (pi->up_vis > 0)
8399 				rc = apply_link_config(pi);
8400 			set_current_media(pi);
8401 			PORT_UNLOCK(pi);
8402 		}
8403 		end_synchronized_op(sc, 0);
8404 	}
8405 
8406 	return (rc);
8407 }
8408 
8409 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8410 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8411 {
8412 	struct port_info *pi = arg1;
8413 	struct link_config *lc = &pi->link_cfg;
8414 	int rc;
8415 	struct sbuf *sb;
8416 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2";
8417 
8418 	rc = sysctl_wire_old_buffer(req, 0);
8419 	if (rc != 0)
8420 		return(rc);
8421 
8422 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8423 	if (sb == NULL)
8424 		return (ENOMEM);
8425 	if (lc->link_ok)
8426 		sbuf_printf(sb, "%b", lc->fec, bits);
8427 	else
8428 		sbuf_printf(sb, "no link");
8429 	rc = sbuf_finish(sb);
8430 	sbuf_delete(sb);
8431 
8432 	return (rc);
8433 }
8434 
8435 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)8436 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8437 {
8438 	struct port_info *pi = arg1;
8439 	struct adapter *sc = pi->adapter;
8440 	struct link_config *lc = &pi->link_cfg;
8441 	int rc;
8442 	int8_t old;
8443 
8444 	if (req->newptr == NULL) {
8445 		struct sbuf *sb;
8446 		static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2"
8447 		    "\5RSVD3\6auto\7module";
8448 
8449 		rc = sysctl_wire_old_buffer(req, 0);
8450 		if (rc != 0)
8451 			return(rc);
8452 
8453 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8454 		if (sb == NULL)
8455 			return (ENOMEM);
8456 
8457 		sbuf_printf(sb, "%b", lc->requested_fec, bits);
8458 		rc = sbuf_finish(sb);
8459 		sbuf_delete(sb);
8460 	} else {
8461 		char s[8];
8462 		int n;
8463 
8464 		snprintf(s, sizeof(s), "%d",
8465 		    lc->requested_fec == FEC_AUTO ? -1 :
8466 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
8467 
8468 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8469 		if (rc != 0)
8470 			return(rc);
8471 
8472 		n = strtol(&s[0], NULL, 0);
8473 		if (n < 0 || n & FEC_AUTO)
8474 			n = FEC_AUTO;
8475 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
8476 			return (EINVAL);/* some other bit is set too */
8477 
8478 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8479 		    "t4reqf");
8480 		if (rc)
8481 			return (rc);
8482 		PORT_LOCK(pi);
8483 		old = lc->requested_fec;
8484 		if (n == FEC_AUTO)
8485 			lc->requested_fec = FEC_AUTO;
8486 		else if (n == 0 || n == FEC_NONE)
8487 			lc->requested_fec = FEC_NONE;
8488 		else {
8489 			if ((lc->pcaps |
8490 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
8491 			    lc->pcaps) {
8492 				rc = ENOTSUP;
8493 				goto done;
8494 			}
8495 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
8496 			    FEC_MODULE);
8497 		}
8498 		if (!hw_off_limits(sc)) {
8499 			fixup_link_config(pi);
8500 			if (pi->up_vis > 0) {
8501 				rc = apply_link_config(pi);
8502 				if (rc != 0) {
8503 					lc->requested_fec = old;
8504 					if (rc == FW_EPROTO)
8505 						rc = ENOTSUP;
8506 				}
8507 			}
8508 		}
8509 done:
8510 		PORT_UNLOCK(pi);
8511 		end_synchronized_op(sc, 0);
8512 	}
8513 
8514 	return (rc);
8515 }
8516 
8517 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)8518 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
8519 {
8520 	struct port_info *pi = arg1;
8521 	struct adapter *sc = pi->adapter;
8522 	struct link_config *lc = &pi->link_cfg;
8523 	int rc;
8524 	int8_t fec;
8525 	struct sbuf *sb;
8526 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2\5RSVD3";
8527 
8528 	rc = sysctl_wire_old_buffer(req, 0);
8529 	if (rc != 0)
8530 		return (rc);
8531 
8532 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8533 	if (sb == NULL)
8534 		return (ENOMEM);
8535 
8536 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
8537 		rc = EBUSY;
8538 		goto done;
8539 	}
8540 	if (hw_off_limits(sc)) {
8541 		rc = ENXIO;
8542 		goto done;
8543 	}
8544 	PORT_LOCK(pi);
8545 	if (pi->up_vis == 0) {
8546 		/*
8547 		 * If all the interfaces are administratively down the firmware
8548 		 * does not report transceiver changes.  Refresh port info here.
8549 		 * This is the only reason we have a synchronized op in this
8550 		 * function.  Just PORT_LOCK would have been enough otherwise.
8551 		 */
8552 		t4_update_port_info(pi);
8553 	}
8554 
8555 	fec = lc->fec_hint;
8556 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
8557 	    !fec_supported(lc->pcaps)) {
8558 		sbuf_printf(sb, "n/a");
8559 	} else {
8560 		if (fec == 0)
8561 			fec = FEC_NONE;
8562 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, bits);
8563 	}
8564 	rc = sbuf_finish(sb);
8565 	PORT_UNLOCK(pi);
8566 done:
8567 	sbuf_delete(sb);
8568 	end_synchronized_op(sc, 0);
8569 
8570 	return (rc);
8571 }
8572 
8573 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)8574 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
8575 {
8576 	struct port_info *pi = arg1;
8577 	struct adapter *sc = pi->adapter;
8578 	struct link_config *lc = &pi->link_cfg;
8579 	int rc, val;
8580 
8581 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
8582 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
8583 	else
8584 		val = -1;
8585 	rc = sysctl_handle_int(oidp, &val, 0, req);
8586 	if (rc != 0 || req->newptr == NULL)
8587 		return (rc);
8588 	if (val == 0)
8589 		val = AUTONEG_DISABLE;
8590 	else if (val == 1)
8591 		val = AUTONEG_ENABLE;
8592 	else
8593 		val = AUTONEG_AUTO;
8594 
8595 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8596 	    "t4aneg");
8597 	if (rc)
8598 		return (rc);
8599 	PORT_LOCK(pi);
8600 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
8601 		rc = ENOTSUP;
8602 		goto done;
8603 	}
8604 	lc->requested_aneg = val;
8605 	if (!hw_off_limits(sc)) {
8606 		fixup_link_config(pi);
8607 		if (pi->up_vis > 0)
8608 			rc = apply_link_config(pi);
8609 		set_current_media(pi);
8610 	}
8611 done:
8612 	PORT_UNLOCK(pi);
8613 	end_synchronized_op(sc, 0);
8614 	return (rc);
8615 }
8616 
8617 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)8618 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
8619 {
8620 	struct port_info *pi = arg1;
8621 	struct adapter *sc = pi->adapter;
8622 	struct link_config *lc = &pi->link_cfg;
8623 	int rc, val;
8624 
8625 	val = lc->force_fec;
8626 	MPASS(val >= -1 && val <= 1);
8627 	rc = sysctl_handle_int(oidp, &val, 0, req);
8628 	if (rc != 0 || req->newptr == NULL)
8629 		return (rc);
8630 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
8631 		return (ENOTSUP);
8632 	if (val < -1 || val > 1)
8633 		return (EINVAL);
8634 
8635 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
8636 	if (rc)
8637 		return (rc);
8638 	PORT_LOCK(pi);
8639 	lc->force_fec = val;
8640 	if (!hw_off_limits(sc)) {
8641 		fixup_link_config(pi);
8642 		if (pi->up_vis > 0)
8643 			rc = apply_link_config(pi);
8644 	}
8645 	PORT_UNLOCK(pi);
8646 	end_synchronized_op(sc, 0);
8647 	return (rc);
8648 }
8649 
8650 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)8651 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
8652 {
8653 	struct adapter *sc = arg1;
8654 	int rc, reg = arg2;
8655 	uint64_t val;
8656 
8657 	mtx_lock(&sc->reg_lock);
8658 	if (hw_off_limits(sc))
8659 		rc = ENXIO;
8660 	else {
8661 		rc = 0;
8662 		val = t4_read_reg64(sc, reg);
8663 	}
8664 	mtx_unlock(&sc->reg_lock);
8665 	if (rc == 0)
8666 		rc = sysctl_handle_64(oidp, &val, 0, req);
8667 	return (rc);
8668 }
8669 
8670 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)8671 sysctl_temperature(SYSCTL_HANDLER_ARGS)
8672 {
8673 	struct adapter *sc = arg1;
8674 	int rc, t;
8675 	uint32_t param, val;
8676 
8677 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
8678 	if (rc)
8679 		return (rc);
8680 	if (hw_off_limits(sc))
8681 		rc = ENXIO;
8682 	else {
8683 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8684 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8685 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
8686 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8687 	}
8688 	end_synchronized_op(sc, 0);
8689 	if (rc)
8690 		return (rc);
8691 
8692 	/* unknown is returned as 0 but we display -1 in that case */
8693 	t = val == 0 ? -1 : val;
8694 
8695 	rc = sysctl_handle_int(oidp, &t, 0, req);
8696 	return (rc);
8697 }
8698 
8699 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)8700 sysctl_vdd(SYSCTL_HANDLER_ARGS)
8701 {
8702 	struct adapter *sc = arg1;
8703 	int rc;
8704 	uint32_t param, val;
8705 
8706 	if (sc->params.core_vdd == 0) {
8707 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8708 		    "t4vdd");
8709 		if (rc)
8710 			return (rc);
8711 		if (hw_off_limits(sc))
8712 			rc = ENXIO;
8713 		else {
8714 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8715 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8716 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
8717 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
8718 			    &param, &val);
8719 		}
8720 		end_synchronized_op(sc, 0);
8721 		if (rc)
8722 			return (rc);
8723 		sc->params.core_vdd = val;
8724 	}
8725 
8726 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
8727 }
8728 
8729 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)8730 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
8731 {
8732 	struct adapter *sc = arg1;
8733 	int rc, v;
8734 	uint32_t param, val;
8735 
8736 	v = sc->sensor_resets;
8737 	rc = sysctl_handle_int(oidp, &v, 0, req);
8738 	if (rc != 0 || req->newptr == NULL || v <= 0)
8739 		return (rc);
8740 
8741 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
8742 	    chip_id(sc) < CHELSIO_T5)
8743 		return (ENOTSUP);
8744 
8745 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
8746 	if (rc)
8747 		return (rc);
8748 	if (hw_off_limits(sc))
8749 		rc = ENXIO;
8750 	else {
8751 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8752 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8753 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
8754 		val = 1;
8755 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8756 	}
8757 	end_synchronized_op(sc, 0);
8758 	if (rc == 0)
8759 		sc->sensor_resets++;
8760 	return (rc);
8761 }
8762 
8763 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)8764 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
8765 {
8766 	struct adapter *sc = arg1;
8767 	struct sbuf *sb;
8768 	int rc;
8769 	uint32_t param, val;
8770 
8771 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
8772 	if (rc)
8773 		return (rc);
8774 	if (hw_off_limits(sc))
8775 		rc = ENXIO;
8776 	else {
8777 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8778 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
8779 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8780 	}
8781 	end_synchronized_op(sc, 0);
8782 	if (rc)
8783 		return (rc);
8784 
8785 	rc = sysctl_wire_old_buffer(req, 0);
8786 	if (rc != 0)
8787 		return (rc);
8788 
8789 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8790 	if (sb == NULL)
8791 		return (ENOMEM);
8792 
8793 	if (val == 0xffffffff) {
8794 		/* Only debug and custom firmwares report load averages. */
8795 		sbuf_printf(sb, "not available");
8796 	} else {
8797 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
8798 		    (val >> 16) & 0xff);
8799 	}
8800 	rc = sbuf_finish(sb);
8801 	sbuf_delete(sb);
8802 
8803 	return (rc);
8804 }
8805 
8806 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)8807 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
8808 {
8809 	struct adapter *sc = arg1;
8810 	struct sbuf *sb;
8811 	int rc, i;
8812 	uint16_t incr[NMTUS][NCCTRL_WIN];
8813 	static const char *dec_fac[] = {
8814 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
8815 		"0.9375"
8816 	};
8817 
8818 	rc = sysctl_wire_old_buffer(req, 0);
8819 	if (rc != 0)
8820 		return (rc);
8821 
8822 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8823 	if (sb == NULL)
8824 		return (ENOMEM);
8825 
8826 	mtx_lock(&sc->reg_lock);
8827 	if (hw_off_limits(sc))
8828 		rc = ENXIO;
8829 	else
8830 		t4_read_cong_tbl(sc, incr);
8831 	mtx_unlock(&sc->reg_lock);
8832 	if (rc)
8833 		goto done;
8834 
8835 	for (i = 0; i < NCCTRL_WIN; ++i) {
8836 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
8837 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
8838 		    incr[5][i], incr[6][i], incr[7][i]);
8839 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
8840 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
8841 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
8842 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
8843 	}
8844 
8845 	rc = sbuf_finish(sb);
8846 done:
8847 	sbuf_delete(sb);
8848 	return (rc);
8849 }
8850 
8851 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
8852 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
8853 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
8854 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
8855 };
8856 
8857 static int
sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)8858 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
8859 {
8860 	struct adapter *sc = arg1;
8861 	struct sbuf *sb;
8862 	int rc, i, n, qid = arg2;
8863 	uint32_t *buf, *p;
8864 	char *qtype;
8865 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
8866 
8867 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
8868 	    ("%s: bad qid %d\n", __func__, qid));
8869 
8870 	if (qid < CIM_NUM_IBQ) {
8871 		/* inbound queue */
8872 		qtype = "IBQ";
8873 		n = 4 * CIM_IBQ_SIZE;
8874 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
8875 		mtx_lock(&sc->reg_lock);
8876 		if (hw_off_limits(sc))
8877 			rc = -ENXIO;
8878 		else
8879 			rc = t4_read_cim_ibq(sc, qid, buf, n);
8880 		mtx_unlock(&sc->reg_lock);
8881 	} else {
8882 		/* outbound queue */
8883 		qtype = "OBQ";
8884 		qid -= CIM_NUM_IBQ;
8885 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
8886 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
8887 		mtx_lock(&sc->reg_lock);
8888 		if (hw_off_limits(sc))
8889 			rc = -ENXIO;
8890 		else
8891 			rc = t4_read_cim_obq(sc, qid, buf, n);
8892 		mtx_unlock(&sc->reg_lock);
8893 	}
8894 
8895 	if (rc < 0) {
8896 		rc = -rc;
8897 		goto done;
8898 	}
8899 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
8900 
8901 	rc = sysctl_wire_old_buffer(req, 0);
8902 	if (rc != 0)
8903 		goto done;
8904 
8905 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
8906 	if (sb == NULL) {
8907 		rc = ENOMEM;
8908 		goto done;
8909 	}
8910 
8911 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
8912 	for (i = 0, p = buf; i < n; i += 16, p += 4)
8913 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
8914 		    p[2], p[3]);
8915 
8916 	rc = sbuf_finish(sb);
8917 	sbuf_delete(sb);
8918 done:
8919 	free(buf, M_CXGBE);
8920 	return (rc);
8921 }
8922 
8923 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)8924 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
8925 {
8926 	uint32_t *p;
8927 
8928 	sbuf_printf(sb, "Status   Data      PC%s",
8929 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
8930 	    "     LS0Stat  LS0Addr             LS0Data");
8931 
8932 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
8933 		if (cfg & F_UPDBGLACAPTPCONLY) {
8934 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
8935 			    p[6], p[7]);
8936 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
8937 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
8938 			    p[4] & 0xff, p[5] >> 8);
8939 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
8940 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
8941 			    p[1] & 0xf, p[2] >> 4);
8942 		} else {
8943 			sbuf_printf(sb,
8944 			    "\n  %02x   %x%07x %x%07x %08x %08x "
8945 			    "%08x%08x%08x%08x",
8946 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
8947 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
8948 			    p[6], p[7]);
8949 		}
8950 	}
8951 }
8952 
8953 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)8954 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
8955 {
8956 	uint32_t *p;
8957 
8958 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
8959 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
8960 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
8961 
8962 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
8963 		if (cfg & F_UPDBGLACAPTPCONLY) {
8964 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
8965 			    p[3] & 0xff, p[2], p[1], p[0]);
8966 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
8967 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
8968 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
8969 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
8970 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
8971 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
8972 			    p[6] >> 16);
8973 		} else {
8974 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
8975 			    "%08x %08x %08x %08x %08x %08x",
8976 			    (p[9] >> 16) & 0xff,
8977 			    p[9] & 0xffff, p[8] >> 16,
8978 			    p[8] & 0xffff, p[7] >> 16,
8979 			    p[7] & 0xffff, p[6] >> 16,
8980 			    p[2], p[1], p[0], p[5], p[4], p[3]);
8981 		}
8982 	}
8983 }
8984 
8985 static int
sbuf_cim_la(struct adapter * sc,struct sbuf * sb,int flags)8986 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
8987 {
8988 	uint32_t cfg, *buf;
8989 	int rc;
8990 
8991 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
8992 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
8993 	    M_ZERO | flags);
8994 	if (buf == NULL)
8995 		return (ENOMEM);
8996 
8997 	mtx_lock(&sc->reg_lock);
8998 	if (hw_off_limits(sc))
8999 		rc = ENXIO;
9000 	else {
9001 		rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
9002 		if (rc == 0)
9003 			rc = -t4_cim_read_la(sc, buf, NULL);
9004 	}
9005 	mtx_unlock(&sc->reg_lock);
9006 	if (rc == 0) {
9007 		if (chip_id(sc) < CHELSIO_T6)
9008 			sbuf_cim_la4(sc, sb, buf, cfg);
9009 		else
9010 			sbuf_cim_la6(sc, sb, buf, cfg);
9011 	}
9012 	free(buf, M_CXGBE);
9013 	return (rc);
9014 }
9015 
9016 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9017 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9018 {
9019 	struct adapter *sc = arg1;
9020 	struct sbuf *sb;
9021 	int rc;
9022 
9023 	rc = sysctl_wire_old_buffer(req, 0);
9024 	if (rc != 0)
9025 		return (rc);
9026 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9027 	if (sb == NULL)
9028 		return (ENOMEM);
9029 
9030 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
9031 	if (rc == 0)
9032 		rc = sbuf_finish(sb);
9033 	sbuf_delete(sb);
9034 	return (rc);
9035 }
9036 
9037 static void
dump_cim_regs(struct adapter * sc)9038 dump_cim_regs(struct adapter *sc)
9039 {
9040 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9041 	    device_get_nameunit(sc->dev),
9042 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9043 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9044 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9045 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9046 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9047 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9048 	    device_get_nameunit(sc->dev),
9049 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9050 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9051 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9052 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9053 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9054 }
9055 
9056 static void
dump_cimla(struct adapter * sc)9057 dump_cimla(struct adapter *sc)
9058 {
9059 	struct sbuf sb;
9060 	int rc;
9061 
9062 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9063 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9064 		    device_get_nameunit(sc->dev));
9065 		return;
9066 	}
9067 	rc = sbuf_cim_la(sc, &sb, M_NOWAIT);
9068 	if (rc == 0) {
9069 		rc = sbuf_finish(&sb);
9070 		if (rc == 0) {
9071 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9072 		    		device_get_nameunit(sc->dev), sbuf_data(&sb));
9073 		}
9074 	}
9075 	sbuf_delete(&sb);
9076 }
9077 
9078 void
t4_os_cim_err(struct adapter * sc)9079 t4_os_cim_err(struct adapter *sc)
9080 {
9081 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9082 }
9083 
9084 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9085 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9086 {
9087 	struct adapter *sc = arg1;
9088 	u_int i;
9089 	struct sbuf *sb;
9090 	uint32_t *buf, *p;
9091 	int rc;
9092 
9093 	rc = sysctl_wire_old_buffer(req, 0);
9094 	if (rc != 0)
9095 		return (rc);
9096 
9097 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9098 	if (sb == NULL)
9099 		return (ENOMEM);
9100 
9101 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9102 	    M_ZERO | M_WAITOK);
9103 
9104 	mtx_lock(&sc->reg_lock);
9105 	if (hw_off_limits(sc))
9106 		rc = ENXIO;
9107 	else
9108 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9109 	mtx_unlock(&sc->reg_lock);
9110 	if (rc)
9111 		goto done;
9112 
9113 	p = buf;
9114 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9115 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9116 		    p[1], p[0]);
9117 	}
9118 
9119 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9120 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9121 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9122 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9123 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9124 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9125 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9126 		    p[0] & 1);
9127 	}
9128 	rc = sbuf_finish(sb);
9129 done:
9130 	sbuf_delete(sb);
9131 	free(buf, M_CXGBE);
9132 	return (rc);
9133 }
9134 
9135 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9136 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9137 {
9138 	struct adapter *sc = arg1;
9139 	u_int i;
9140 	struct sbuf *sb;
9141 	uint32_t *buf, *p;
9142 	int rc;
9143 
9144 	rc = sysctl_wire_old_buffer(req, 0);
9145 	if (rc != 0)
9146 		return (rc);
9147 
9148 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9149 	if (sb == NULL)
9150 		return (ENOMEM);
9151 
9152 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9153 	    M_ZERO | M_WAITOK);
9154 
9155 	mtx_lock(&sc->reg_lock);
9156 	if (hw_off_limits(sc))
9157 		rc = ENXIO;
9158 	else
9159 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9160 	mtx_unlock(&sc->reg_lock);
9161 	if (rc)
9162 		goto done;
9163 
9164 	p = buf;
9165 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9166 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9167 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9168 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9169 		    p[4], p[3], p[2], p[1], p[0]);
9170 	}
9171 
9172 	sbuf_printf(sb, "\n\nCntl ID               Data");
9173 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9174 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9175 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9176 	}
9177 
9178 	rc = sbuf_finish(sb);
9179 done:
9180 	sbuf_delete(sb);
9181 	free(buf, M_CXGBE);
9182 	return (rc);
9183 }
9184 
9185 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9186 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9187 {
9188 	struct adapter *sc = arg1;
9189 	struct sbuf *sb;
9190 	int rc, i;
9191 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9192 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9193 	uint16_t thres[CIM_NUM_IBQ];
9194 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9195 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9196 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9197 
9198 	cim_num_obq = sc->chip_params->cim_num_obq;
9199 	if (is_t4(sc)) {
9200 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9201 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9202 	} else {
9203 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9204 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9205 	}
9206 	nq = CIM_NUM_IBQ + cim_num_obq;
9207 
9208 	mtx_lock(&sc->reg_lock);
9209 	if (hw_off_limits(sc))
9210 		rc = ENXIO;
9211 	else {
9212 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9213 		if (rc == 0) {
9214 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9215 			    obq_wr);
9216 			if (rc == 0)
9217 				t4_read_cimq_cfg(sc, base, size, thres);
9218 		}
9219 	}
9220 	mtx_unlock(&sc->reg_lock);
9221 	if (rc)
9222 		return (rc);
9223 
9224 	rc = sysctl_wire_old_buffer(req, 0);
9225 	if (rc != 0)
9226 		return (rc);
9227 
9228 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9229 	if (sb == NULL)
9230 		return (ENOMEM);
9231 
9232 	sbuf_printf(sb,
9233 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9234 
9235 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9236 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9237 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9238 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9239 		    G_QUEREMFLITS(p[2]) * 16);
9240 	for ( ; i < nq; i++, p += 4, wr += 2)
9241 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9242 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9243 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9244 		    G_QUEREMFLITS(p[2]) * 16);
9245 
9246 	rc = sbuf_finish(sb);
9247 	sbuf_delete(sb);
9248 
9249 	return (rc);
9250 }
9251 
9252 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9253 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9254 {
9255 	struct adapter *sc = arg1;
9256 	struct sbuf *sb;
9257 	int rc;
9258 	struct tp_cpl_stats stats;
9259 
9260 	rc = sysctl_wire_old_buffer(req, 0);
9261 	if (rc != 0)
9262 		return (rc);
9263 
9264 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9265 	if (sb == NULL)
9266 		return (ENOMEM);
9267 
9268 	mtx_lock(&sc->reg_lock);
9269 	if (hw_off_limits(sc))
9270 		rc = ENXIO;
9271 	else
9272 		t4_tp_get_cpl_stats(sc, &stats, 0);
9273 	mtx_unlock(&sc->reg_lock);
9274 	if (rc)
9275 		goto done;
9276 
9277 	if (sc->chip_params->nchan > 2) {
9278 		sbuf_printf(sb, "                 channel 0  channel 1"
9279 		    "  channel 2  channel 3");
9280 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9281 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9282 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9283 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9284 	} else {
9285 		sbuf_printf(sb, "                 channel 0  channel 1");
9286 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9287 		    stats.req[0], stats.req[1]);
9288 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9289 		    stats.rsp[0], stats.rsp[1]);
9290 	}
9291 
9292 	rc = sbuf_finish(sb);
9293 done:
9294 	sbuf_delete(sb);
9295 	return (rc);
9296 }
9297 
9298 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9299 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9300 {
9301 	struct adapter *sc = arg1;
9302 	struct sbuf *sb;
9303 	int rc;
9304 	struct tp_usm_stats stats;
9305 
9306 	rc = sysctl_wire_old_buffer(req, 0);
9307 	if (rc != 0)
9308 		return(rc);
9309 
9310 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9311 	if (sb == NULL)
9312 		return (ENOMEM);
9313 
9314 	mtx_lock(&sc->reg_lock);
9315 	if (hw_off_limits(sc))
9316 		rc = ENXIO;
9317 	else
9318 		t4_get_usm_stats(sc, &stats, 1);
9319 	mtx_unlock(&sc->reg_lock);
9320 	if (rc == 0) {
9321 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
9322 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
9323 		sbuf_printf(sb, "Drops:  %u", stats.drops);
9324 		rc = sbuf_finish(sb);
9325 	}
9326 	sbuf_delete(sb);
9327 
9328 	return (rc);
9329 }
9330 
9331 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)9332 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
9333 {
9334 	struct adapter *sc = arg1;
9335 	struct sbuf *sb;
9336 	int rc;
9337 	struct tp_tid_stats stats;
9338 
9339 	rc = sysctl_wire_old_buffer(req, 0);
9340 	if (rc != 0)
9341 		return(rc);
9342 
9343 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9344 	if (sb == NULL)
9345 		return (ENOMEM);
9346 
9347 	mtx_lock(&sc->reg_lock);
9348 	if (hw_off_limits(sc))
9349 		rc = ENXIO;
9350 	else
9351 		t4_tp_get_tid_stats(sc, &stats, 1);
9352 	mtx_unlock(&sc->reg_lock);
9353 	if (rc == 0) {
9354 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
9355 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
9356 		sbuf_printf(sb, "Active:     %u\n", stats.act);
9357 		sbuf_printf(sb, "Passive:    %u", stats.pas);
9358 		rc = sbuf_finish(sb);
9359 	}
9360 	sbuf_delete(sb);
9361 
9362 	return (rc);
9363 }
9364 
9365 static const char * const devlog_level_strings[] = {
9366 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
9367 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
9368 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
9369 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
9370 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
9371 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
9372 };
9373 
9374 static const char * const devlog_facility_strings[] = {
9375 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
9376 	[FW_DEVLOG_FACILITY_CF]		= "CF",
9377 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
9378 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
9379 	[FW_DEVLOG_FACILITY_RES]	= "RES",
9380 	[FW_DEVLOG_FACILITY_HW]		= "HW",
9381 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
9382 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
9383 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
9384 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
9385 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
9386 	[FW_DEVLOG_FACILITY_VI]		= "VI",
9387 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
9388 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
9389 	[FW_DEVLOG_FACILITY_TM]		= "TM",
9390 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
9391 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
9392 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
9393 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
9394 	[FW_DEVLOG_FACILITY_RI]		= "RI",
9395 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
9396 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
9397 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
9398 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
9399 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
9400 };
9401 
9402 static int
sbuf_devlog(struct adapter * sc,struct sbuf * sb,int flags)9403 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
9404 {
9405 	int i, j, rc, nentries, first = 0;
9406 	struct devlog_params *dparams = &sc->params.devlog;
9407 	struct fw_devlog_e *buf, *e;
9408 	uint64_t ftstamp = UINT64_MAX;
9409 
9410 	if (dparams->addr == 0)
9411 		return (ENXIO);
9412 
9413 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9414 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
9415 	if (buf == NULL)
9416 		return (ENOMEM);
9417 
9418 	mtx_lock(&sc->reg_lock);
9419 	if (hw_off_limits(sc))
9420 		rc = ENXIO;
9421 	else
9422 		rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf,
9423 		    dparams->size);
9424 	mtx_unlock(&sc->reg_lock);
9425 	if (rc != 0)
9426 		goto done;
9427 
9428 	nentries = dparams->size / sizeof(struct fw_devlog_e);
9429 	for (i = 0; i < nentries; i++) {
9430 		e = &buf[i];
9431 
9432 		if (e->timestamp == 0)
9433 			break;	/* end */
9434 
9435 		e->timestamp = be64toh(e->timestamp);
9436 		e->seqno = be32toh(e->seqno);
9437 		for (j = 0; j < 8; j++)
9438 			e->params[j] = be32toh(e->params[j]);
9439 
9440 		if (e->timestamp < ftstamp) {
9441 			ftstamp = e->timestamp;
9442 			first = i;
9443 		}
9444 	}
9445 
9446 	if (buf[first].timestamp == 0)
9447 		goto done;	/* nothing in the log */
9448 
9449 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
9450 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
9451 
9452 	i = first;
9453 	do {
9454 		e = &buf[i];
9455 		if (e->timestamp == 0)
9456 			break;	/* end */
9457 
9458 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
9459 		    e->seqno, e->timestamp,
9460 		    (e->level < nitems(devlog_level_strings) ?
9461 			devlog_level_strings[e->level] : "UNKNOWN"),
9462 		    (e->facility < nitems(devlog_facility_strings) ?
9463 			devlog_facility_strings[e->facility] : "UNKNOWN"));
9464 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
9465 		    e->params[2], e->params[3], e->params[4],
9466 		    e->params[5], e->params[6], e->params[7]);
9467 
9468 		if (++i == nentries)
9469 			i = 0;
9470 	} while (i != first);
9471 done:
9472 	free(buf, M_CXGBE);
9473 	return (rc);
9474 }
9475 
9476 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)9477 sysctl_devlog(SYSCTL_HANDLER_ARGS)
9478 {
9479 	struct adapter *sc = arg1;
9480 	int rc;
9481 	struct sbuf *sb;
9482 
9483 	rc = sysctl_wire_old_buffer(req, 0);
9484 	if (rc != 0)
9485 		return (rc);
9486 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9487 	if (sb == NULL)
9488 		return (ENOMEM);
9489 
9490 	rc = sbuf_devlog(sc, sb, M_WAITOK);
9491 	if (rc == 0)
9492 		rc = sbuf_finish(sb);
9493 	sbuf_delete(sb);
9494 	return (rc);
9495 }
9496 
9497 static void
dump_devlog(struct adapter * sc)9498 dump_devlog(struct adapter *sc)
9499 {
9500 	int rc;
9501 	struct sbuf sb;
9502 
9503 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9504 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
9505 		    device_get_nameunit(sc->dev));
9506 		return;
9507 	}
9508 	rc = sbuf_devlog(sc, &sb, M_NOWAIT);
9509 	if (rc == 0) {
9510 		rc = sbuf_finish(&sb);
9511 		if (rc == 0) {
9512 			log(LOG_DEBUG, "%s: device log follows.\n%s",
9513 		    		device_get_nameunit(sc->dev), sbuf_data(&sb));
9514 		}
9515 	}
9516 	sbuf_delete(&sb);
9517 }
9518 
9519 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)9520 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
9521 {
9522 	struct adapter *sc = arg1;
9523 	struct sbuf *sb;
9524 	int rc;
9525 	struct tp_fcoe_stats stats[MAX_NCHAN];
9526 	int i, nchan = sc->chip_params->nchan;
9527 
9528 	rc = sysctl_wire_old_buffer(req, 0);
9529 	if (rc != 0)
9530 		return (rc);
9531 
9532 	mtx_lock(&sc->reg_lock);
9533 	if (hw_off_limits(sc))
9534 		rc = ENXIO;
9535 	else {
9536 		for (i = 0; i < nchan; i++)
9537 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
9538 	}
9539 	mtx_unlock(&sc->reg_lock);
9540 	if (rc != 0)
9541 		return (rc);
9542 
9543 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9544 	if (sb == NULL)
9545 		return (ENOMEM);
9546 
9547 	if (nchan > 2) {
9548 		sbuf_printf(sb, "                   channel 0        channel 1"
9549 		    "        channel 2        channel 3");
9550 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
9551 		    stats[0].octets_ddp, stats[1].octets_ddp,
9552 		    stats[2].octets_ddp, stats[3].octets_ddp);
9553 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
9554 		    stats[0].frames_ddp, stats[1].frames_ddp,
9555 		    stats[2].frames_ddp, stats[3].frames_ddp);
9556 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
9557 		    stats[0].frames_drop, stats[1].frames_drop,
9558 		    stats[2].frames_drop, stats[3].frames_drop);
9559 	} else {
9560 		sbuf_printf(sb, "                   channel 0        channel 1");
9561 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
9562 		    stats[0].octets_ddp, stats[1].octets_ddp);
9563 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
9564 		    stats[0].frames_ddp, stats[1].frames_ddp);
9565 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
9566 		    stats[0].frames_drop, stats[1].frames_drop);
9567 	}
9568 
9569 	rc = sbuf_finish(sb);
9570 	sbuf_delete(sb);
9571 
9572 	return (rc);
9573 }
9574 
9575 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)9576 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
9577 {
9578 	struct adapter *sc = arg1;
9579 	struct sbuf *sb;
9580 	int rc, i;
9581 	unsigned int map, kbps, ipg, mode;
9582 	unsigned int pace_tab[NTX_SCHED];
9583 
9584 	rc = sysctl_wire_old_buffer(req, 0);
9585 	if (rc != 0)
9586 		return (rc);
9587 
9588 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
9589 	if (sb == NULL)
9590 		return (ENOMEM);
9591 
9592 	mtx_lock(&sc->reg_lock);
9593 	if (hw_off_limits(sc)) {
9594 		rc = ENXIO;
9595 		goto done;
9596 	}
9597 
9598 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
9599 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
9600 	t4_read_pace_tbl(sc, pace_tab);
9601 
9602 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
9603 	    "Class IPG (0.1 ns)   Flow IPG (us)");
9604 
9605 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
9606 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
9607 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
9608 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
9609 		if (kbps)
9610 			sbuf_printf(sb, "%9u     ", kbps);
9611 		else
9612 			sbuf_printf(sb, " disabled     ");
9613 
9614 		if (ipg)
9615 			sbuf_printf(sb, "%13u        ", ipg);
9616 		else
9617 			sbuf_printf(sb, "     disabled        ");
9618 
9619 		if (pace_tab[i])
9620 			sbuf_printf(sb, "%10u", pace_tab[i]);
9621 		else
9622 			sbuf_printf(sb, "  disabled");
9623 	}
9624 	rc = sbuf_finish(sb);
9625 done:
9626 	mtx_unlock(&sc->reg_lock);
9627 	sbuf_delete(sb);
9628 	return (rc);
9629 }
9630 
9631 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)9632 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
9633 {
9634 	struct adapter *sc = arg1;
9635 	struct sbuf *sb;
9636 	int rc, i, j;
9637 	uint64_t *p0, *p1;
9638 	struct lb_port_stats s[2];
9639 	static const char *stat_name[] = {
9640 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
9641 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
9642 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
9643 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
9644 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
9645 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
9646 		"BG2FramesTrunc:", "BG3FramesTrunc:"
9647 	};
9648 
9649 	rc = sysctl_wire_old_buffer(req, 0);
9650 	if (rc != 0)
9651 		return (rc);
9652 
9653 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9654 	if (sb == NULL)
9655 		return (ENOMEM);
9656 
9657 	memset(s, 0, sizeof(s));
9658 
9659 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
9660 		mtx_lock(&sc->reg_lock);
9661 		if (hw_off_limits(sc))
9662 			rc = ENXIO;
9663 		else {
9664 			t4_get_lb_stats(sc, i, &s[0]);
9665 			t4_get_lb_stats(sc, i + 1, &s[1]);
9666 		}
9667 		mtx_unlock(&sc->reg_lock);
9668 		if (rc != 0)
9669 			break;
9670 
9671 		p0 = &s[0].octets;
9672 		p1 = &s[1].octets;
9673 		sbuf_printf(sb, "%s                       Loopback %u"
9674 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
9675 
9676 		for (j = 0; j < nitems(stat_name); j++)
9677 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
9678 				   *p0++, *p1++);
9679 	}
9680 
9681 	rc = sbuf_finish(sb);
9682 	sbuf_delete(sb);
9683 
9684 	return (rc);
9685 }
9686 
9687 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)9688 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
9689 {
9690 	int rc = 0;
9691 	struct port_info *pi = arg1;
9692 	struct link_config *lc = &pi->link_cfg;
9693 	struct sbuf *sb;
9694 
9695 	rc = sysctl_wire_old_buffer(req, 0);
9696 	if (rc != 0)
9697 		return(rc);
9698 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
9699 	if (sb == NULL)
9700 		return (ENOMEM);
9701 
9702 	if (lc->link_ok || lc->link_down_rc == 255)
9703 		sbuf_printf(sb, "n/a");
9704 	else
9705 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
9706 
9707 	rc = sbuf_finish(sb);
9708 	sbuf_delete(sb);
9709 
9710 	return (rc);
9711 }
9712 
9713 struct mem_desc {
9714 	unsigned int base;
9715 	unsigned int limit;
9716 	unsigned int idx;
9717 };
9718 
9719 static int
mem_desc_cmp(const void * a,const void * b)9720 mem_desc_cmp(const void *a, const void *b)
9721 {
9722 	return ((const struct mem_desc *)a)->base -
9723 	       ((const struct mem_desc *)b)->base;
9724 }
9725 
9726 static void
mem_region_show(struct sbuf * sb,const char * name,unsigned int from,unsigned int to)9727 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
9728     unsigned int to)
9729 {
9730 	unsigned int size;
9731 
9732 	if (from == to)
9733 		return;
9734 
9735 	size = to - from + 1;
9736 	if (size == 0)
9737 		return;
9738 
9739 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
9740 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
9741 }
9742 
9743 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)9744 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
9745 {
9746 	struct adapter *sc = arg1;
9747 	struct sbuf *sb;
9748 	int rc, i, n;
9749 	uint32_t lo, hi, used, alloc;
9750 	static const char *memory[] = {
9751 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
9752 	};
9753 	static const char *region[] = {
9754 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
9755 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
9756 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
9757 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
9758 		"RQUDP region:", "PBL region:", "TXPBL region:",
9759 		"DBVFIFO region:", "ULPRX state:", "ULPTX state:",
9760 		"On-chip queues:", "TLS keys:",
9761 	};
9762 	struct mem_desc avail[4];
9763 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
9764 	struct mem_desc *md = mem;
9765 
9766 	rc = sysctl_wire_old_buffer(req, 0);
9767 	if (rc != 0)
9768 		return (rc);
9769 
9770 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9771 	if (sb == NULL)
9772 		return (ENOMEM);
9773 
9774 	for (i = 0; i < nitems(mem); i++) {
9775 		mem[i].limit = 0;
9776 		mem[i].idx = i;
9777 	}
9778 
9779 	mtx_lock(&sc->reg_lock);
9780 	if (hw_off_limits(sc)) {
9781 		rc = ENXIO;
9782 		goto done;
9783 	}
9784 
9785 	/* Find and sort the populated memory ranges */
9786 	i = 0;
9787 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
9788 	if (lo & F_EDRAM0_ENABLE) {
9789 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
9790 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
9791 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
9792 		avail[i].idx = 0;
9793 		i++;
9794 	}
9795 	if (lo & F_EDRAM1_ENABLE) {
9796 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
9797 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
9798 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
9799 		avail[i].idx = 1;
9800 		i++;
9801 	}
9802 	if (lo & F_EXT_MEM_ENABLE) {
9803 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
9804 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
9805 		avail[i].limit = avail[i].base + (G_EXT_MEM_SIZE(hi) << 20);
9806 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
9807 		i++;
9808 	}
9809 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
9810 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9811 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9812 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9813 		avail[i].idx = 4;
9814 		i++;
9815 	}
9816 	if (is_t6(sc) && lo & F_HMA_MUX) {
9817 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9818 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9819 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9820 		avail[i].idx = 5;
9821 		i++;
9822 	}
9823 	MPASS(i <= nitems(avail));
9824 	if (!i)                                    /* no memory available */
9825 		goto done;
9826 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
9827 
9828 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
9829 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
9830 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
9831 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
9832 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
9833 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
9834 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
9835 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
9836 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
9837 
9838 	/* the next few have explicit upper bounds */
9839 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
9840 	md->limit = md->base - 1 +
9841 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
9842 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
9843 	md++;
9844 
9845 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
9846 	md->limit = md->base - 1 +
9847 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
9848 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
9849 	md++;
9850 
9851 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
9852 		if (chip_id(sc) <= CHELSIO_T5)
9853 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
9854 		else
9855 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
9856 		md->limit = 0;
9857 	} else {
9858 		md->base = 0;
9859 		md->idx = nitems(region);  /* hide it */
9860 	}
9861 	md++;
9862 
9863 #define ulp_region(reg) \
9864 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
9865 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
9866 
9867 	ulp_region(RX_ISCSI);
9868 	ulp_region(RX_TDDP);
9869 	ulp_region(TX_TPT);
9870 	ulp_region(RX_STAG);
9871 	ulp_region(RX_RQ);
9872 	ulp_region(RX_RQUDP);
9873 	ulp_region(RX_PBL);
9874 	ulp_region(TX_PBL);
9875 #undef ulp_region
9876 
9877 	md->base = 0;
9878 	if (is_t4(sc))
9879 		md->idx = nitems(region);
9880 	else {
9881 		uint32_t size = 0;
9882 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
9883 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
9884 
9885 		if (is_t5(sc)) {
9886 			if (sge_ctrl & F_VFIFO_ENABLE)
9887 				size = fifo_size << 2;
9888 		} else
9889 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
9890 
9891 		if (size) {
9892 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
9893 			md->limit = md->base + size - 1;
9894 		} else
9895 			md->idx = nitems(region);
9896 	}
9897 	md++;
9898 
9899 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
9900 	md->limit = 0;
9901 	md++;
9902 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
9903 	md->limit = 0;
9904 	md++;
9905 
9906 	md->base = sc->vres.ocq.start;
9907 	if (sc->vres.ocq.size)
9908 		md->limit = md->base + sc->vres.ocq.size - 1;
9909 	else
9910 		md->idx = nitems(region);  /* hide it */
9911 	md++;
9912 
9913 	md->base = sc->vres.key.start;
9914 	if (sc->vres.key.size)
9915 		md->limit = md->base + sc->vres.key.size - 1;
9916 	else
9917 		md->idx = nitems(region);  /* hide it */
9918 	md++;
9919 
9920 	/* add any address-space holes, there can be up to 3 */
9921 	for (n = 0; n < i - 1; n++)
9922 		if (avail[n].limit < avail[n + 1].base)
9923 			(md++)->base = avail[n].limit;
9924 	if (avail[n].limit)
9925 		(md++)->base = avail[n].limit;
9926 
9927 	n = md - mem;
9928 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
9929 
9930 	for (lo = 0; lo < i; lo++)
9931 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
9932 				avail[lo].limit - 1);
9933 
9934 	sbuf_printf(sb, "\n");
9935 	for (i = 0; i < n; i++) {
9936 		if (mem[i].idx >= nitems(region))
9937 			continue;                        /* skip holes */
9938 		if (!mem[i].limit)
9939 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
9940 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
9941 				mem[i].limit);
9942 	}
9943 
9944 	sbuf_printf(sb, "\n");
9945 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
9946 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
9947 	mem_region_show(sb, "uP RAM:", lo, hi);
9948 
9949 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
9950 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
9951 	mem_region_show(sb, "uP Extmem2:", lo, hi);
9952 
9953 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
9954 	sbuf_printf(sb, "\n%u Rx pages of size %uKiB for %u channels\n",
9955 		   G_PMRXMAXPAGE(lo),
9956 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
9957 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
9958 
9959 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
9960 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
9961 	sbuf_printf(sb, "%u Tx pages of size %u%ciB for %u channels\n",
9962 		   G_PMTXMAXPAGE(lo),
9963 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
9964 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
9965 	sbuf_printf(sb, "%u p-structs\n",
9966 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT));
9967 
9968 	for (i = 0; i < 4; i++) {
9969 		if (chip_id(sc) > CHELSIO_T5)
9970 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
9971 		else
9972 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
9973 		if (is_t5(sc)) {
9974 			used = G_T5_USED(lo);
9975 			alloc = G_T5_ALLOC(lo);
9976 		} else {
9977 			used = G_USED(lo);
9978 			alloc = G_ALLOC(lo);
9979 		}
9980 		/* For T6 these are MAC buffer groups */
9981 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
9982 		    i, used, alloc);
9983 	}
9984 	for (i = 0; i < sc->chip_params->nchan; i++) {
9985 		if (chip_id(sc) > CHELSIO_T5)
9986 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
9987 		else
9988 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
9989 		if (is_t5(sc)) {
9990 			used = G_T5_USED(lo);
9991 			alloc = G_T5_ALLOC(lo);
9992 		} else {
9993 			used = G_USED(lo);
9994 			alloc = G_ALLOC(lo);
9995 		}
9996 		/* For T6 these are MAC buffer groups */
9997 		sbuf_printf(sb,
9998 		    "\nLoopback %d using %u pages out of %u allocated",
9999 		    i, used, alloc);
10000 	}
10001 done:
10002 	mtx_unlock(&sc->reg_lock);
10003 	if (rc == 0)
10004 		rc = sbuf_finish(sb);
10005 	sbuf_delete(sb);
10006 	return (rc);
10007 }
10008 
10009 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10010 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10011 {
10012 	*mask = x | y;
10013 	y = htobe64(y);
10014 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10015 }
10016 
10017 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10018 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10019 {
10020 	struct adapter *sc = arg1;
10021 	struct sbuf *sb;
10022 	int rc, i;
10023 
10024 	MPASS(chip_id(sc) <= CHELSIO_T5);
10025 
10026 	rc = sysctl_wire_old_buffer(req, 0);
10027 	if (rc != 0)
10028 		return (rc);
10029 
10030 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10031 	if (sb == NULL)
10032 		return (ENOMEM);
10033 
10034 	sbuf_printf(sb,
10035 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10036 	    "  VF              Replication             P0 P1 P2 P3  ML");
10037 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10038 		uint64_t tcamx, tcamy, mask;
10039 		uint32_t cls_lo, cls_hi;
10040 		uint8_t addr[ETHER_ADDR_LEN];
10041 
10042 		mtx_lock(&sc->reg_lock);
10043 		if (hw_off_limits(sc))
10044 			rc = ENXIO;
10045 		else {
10046 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10047 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10048 		}
10049 		mtx_unlock(&sc->reg_lock);
10050 		if (rc != 0)
10051 			break;
10052 		if (tcamx & tcamy)
10053 			continue;
10054 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10055 		mtx_lock(&sc->reg_lock);
10056 		if (hw_off_limits(sc))
10057 			rc = ENXIO;
10058 		else {
10059 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10060 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10061 		}
10062 		mtx_unlock(&sc->reg_lock);
10063 		if (rc != 0)
10064 			break;
10065 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10066 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10067 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10068 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10069 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10070 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10071 
10072 		if (cls_lo & F_REPLICATE) {
10073 			struct fw_ldst_cmd ldst_cmd;
10074 
10075 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10076 			ldst_cmd.op_to_addrspace =
10077 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10078 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10079 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10080 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10081 			ldst_cmd.u.mps.rplc.fid_idx =
10082 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10083 				V_FW_LDST_CMD_IDX(i));
10084 
10085 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10086 			    "t4mps");
10087 			if (rc)
10088 				break;
10089 			if (hw_off_limits(sc))
10090 				rc = ENXIO;
10091 			else
10092 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10093 				    sizeof(ldst_cmd), &ldst_cmd);
10094 			end_synchronized_op(sc, 0);
10095 			if (rc != 0)
10096 				break;
10097 			else {
10098 				sbuf_printf(sb, " %08x %08x %08x %08x",
10099 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10100 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10101 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10102 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10103 			}
10104 		} else
10105 			sbuf_printf(sb, "%36s", "");
10106 
10107 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10108 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10109 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10110 	}
10111 
10112 	if (rc)
10113 		(void) sbuf_finish(sb);
10114 	else
10115 		rc = sbuf_finish(sb);
10116 	sbuf_delete(sb);
10117 
10118 	return (rc);
10119 }
10120 
10121 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10122 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10123 {
10124 	struct adapter *sc = arg1;
10125 	struct sbuf *sb;
10126 	int rc, i;
10127 
10128 	MPASS(chip_id(sc) > CHELSIO_T5);
10129 
10130 	rc = sysctl_wire_old_buffer(req, 0);
10131 	if (rc != 0)
10132 		return (rc);
10133 
10134 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10135 	if (sb == NULL)
10136 		return (ENOMEM);
10137 
10138 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10139 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10140 	    "                           Replication"
10141 	    "                                    P0 P1 P2 P3  ML\n");
10142 
10143 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10144 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10145 		uint16_t ivlan;
10146 		uint64_t tcamx, tcamy, val, mask;
10147 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10148 		uint8_t addr[ETHER_ADDR_LEN];
10149 
10150 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10151 		if (i < 256)
10152 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10153 		else
10154 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10155 		mtx_lock(&sc->reg_lock);
10156 		if (hw_off_limits(sc))
10157 			rc = ENXIO;
10158 		else {
10159 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10160 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10161 			tcamy = G_DMACH(val) << 32;
10162 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10163 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10164 		}
10165 		mtx_unlock(&sc->reg_lock);
10166 		if (rc != 0)
10167 			break;
10168 
10169 		lookup_type = G_DATALKPTYPE(data2);
10170 		port_num = G_DATAPORTNUM(data2);
10171 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10172 			/* Inner header VNI */
10173 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10174 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10175 			dip_hit = data2 & F_DATADIPHIT;
10176 			vlan_vld = 0;
10177 		} else {
10178 			vniy = 0;
10179 			dip_hit = 0;
10180 			vlan_vld = data2 & F_DATAVIDH2;
10181 			ivlan = G_VIDL(val);
10182 		}
10183 
10184 		ctl |= V_CTLXYBITSEL(1);
10185 		mtx_lock(&sc->reg_lock);
10186 		if (hw_off_limits(sc))
10187 			rc = ENXIO;
10188 		else {
10189 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10190 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10191 			tcamx = G_DMACH(val) << 32;
10192 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10193 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10194 		}
10195 		mtx_unlock(&sc->reg_lock);
10196 		if (rc != 0)
10197 			break;
10198 
10199 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10200 			/* Inner header VNI mask */
10201 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10202 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10203 		} else
10204 			vnix = 0;
10205 
10206 		if (tcamx & tcamy)
10207 			continue;
10208 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10209 
10210 		mtx_lock(&sc->reg_lock);
10211 		if (hw_off_limits(sc))
10212 			rc = ENXIO;
10213 		else {
10214 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10215 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10216 		}
10217 		mtx_unlock(&sc->reg_lock);
10218 		if (rc != 0)
10219 			break;
10220 
10221 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10222 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10223 			    "%012jx %06x %06x    -    -   %3c"
10224 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
10225 			    addr[1], addr[2], addr[3], addr[4], addr[5],
10226 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
10227 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10228 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10229 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10230 		} else {
10231 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10232 			    "%012jx    -       -   ", i, addr[0], addr[1],
10233 			    addr[2], addr[3], addr[4], addr[5],
10234 			    (uintmax_t)mask);
10235 
10236 			if (vlan_vld)
10237 				sbuf_printf(sb, "%4u   Y     ", ivlan);
10238 			else
10239 				sbuf_printf(sb, "  -    N     ");
10240 
10241 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
10242 			    lookup_type ? 'I' : 'O', port_num,
10243 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10244 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10245 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10246 		}
10247 
10248 
10249 		if (cls_lo & F_T6_REPLICATE) {
10250 			struct fw_ldst_cmd ldst_cmd;
10251 
10252 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10253 			ldst_cmd.op_to_addrspace =
10254 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10255 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10256 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10257 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10258 			ldst_cmd.u.mps.rplc.fid_idx =
10259 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10260 				V_FW_LDST_CMD_IDX(i));
10261 
10262 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10263 			    "t6mps");
10264 			if (rc)
10265 				break;
10266 			if (hw_off_limits(sc))
10267 				rc = ENXIO;
10268 			else
10269 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10270 				    sizeof(ldst_cmd), &ldst_cmd);
10271 			end_synchronized_op(sc, 0);
10272 			if (rc != 0)
10273 				break;
10274 			else {
10275 				sbuf_printf(sb, " %08x %08x %08x %08x"
10276 				    " %08x %08x %08x %08x",
10277 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
10278 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
10279 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
10280 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
10281 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10282 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10283 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10284 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10285 			}
10286 		} else
10287 			sbuf_printf(sb, "%72s", "");
10288 
10289 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
10290 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
10291 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
10292 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
10293 	}
10294 
10295 	if (rc)
10296 		(void) sbuf_finish(sb);
10297 	else
10298 		rc = sbuf_finish(sb);
10299 	sbuf_delete(sb);
10300 
10301 	return (rc);
10302 }
10303 
10304 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)10305 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
10306 {
10307 	struct adapter *sc = arg1;
10308 	struct sbuf *sb;
10309 	int rc;
10310 	uint16_t mtus[NMTUS];
10311 
10312 	rc = sysctl_wire_old_buffer(req, 0);
10313 	if (rc != 0)
10314 		return (rc);
10315 
10316 	mtx_lock(&sc->reg_lock);
10317 	if (hw_off_limits(sc))
10318 		rc = ENXIO;
10319 	else
10320 		t4_read_mtu_tbl(sc, mtus, NULL);
10321 	mtx_unlock(&sc->reg_lock);
10322 	if (rc != 0)
10323 		return (rc);
10324 
10325 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10326 	if (sb == NULL)
10327 		return (ENOMEM);
10328 
10329 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
10330 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
10331 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
10332 	    mtus[14], mtus[15]);
10333 
10334 	rc = sbuf_finish(sb);
10335 	sbuf_delete(sb);
10336 
10337 	return (rc);
10338 }
10339 
10340 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)10341 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
10342 {
10343 	struct adapter *sc = arg1;
10344 	struct sbuf *sb;
10345 	int rc, i;
10346 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
10347 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
10348 	static const char *tx_stats[MAX_PM_NSTATS] = {
10349 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
10350 		"Tx FIFO wait", NULL, "Tx latency"
10351 	};
10352 	static const char *rx_stats[MAX_PM_NSTATS] = {
10353 		"Read:", "Write bypass:", "Write mem:", "Flush:",
10354 		"Rx FIFO wait", NULL, "Rx latency"
10355 	};
10356 
10357 	rc = sysctl_wire_old_buffer(req, 0);
10358 	if (rc != 0)
10359 		return (rc);
10360 
10361 	mtx_lock(&sc->reg_lock);
10362 	if (hw_off_limits(sc))
10363 		rc = ENXIO;
10364 	else {
10365 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
10366 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
10367 	}
10368 	mtx_unlock(&sc->reg_lock);
10369 	if (rc != 0)
10370 		return (rc);
10371 
10372 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10373 	if (sb == NULL)
10374 		return (ENOMEM);
10375 
10376 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
10377 	for (i = 0; i < 4; i++) {
10378 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10379 		    tx_cyc[i]);
10380 	}
10381 
10382 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
10383 	for (i = 0; i < 4; i++) {
10384 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10385 		    rx_cyc[i]);
10386 	}
10387 
10388 	if (chip_id(sc) > CHELSIO_T5) {
10389 		sbuf_printf(sb,
10390 		    "\n              Total wait      Total occupancy");
10391 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10392 		    tx_cyc[i]);
10393 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10394 		    rx_cyc[i]);
10395 
10396 		i += 2;
10397 		MPASS(i < nitems(tx_stats));
10398 
10399 		sbuf_printf(sb,
10400 		    "\n                   Reads           Total wait");
10401 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10402 		    tx_cyc[i]);
10403 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10404 		    rx_cyc[i]);
10405 	}
10406 
10407 	rc = sbuf_finish(sb);
10408 	sbuf_delete(sb);
10409 
10410 	return (rc);
10411 }
10412 
10413 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)10414 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
10415 {
10416 	struct adapter *sc = arg1;
10417 	struct sbuf *sb;
10418 	int rc;
10419 	struct tp_rdma_stats stats;
10420 
10421 	rc = sysctl_wire_old_buffer(req, 0);
10422 	if (rc != 0)
10423 		return (rc);
10424 
10425 	mtx_lock(&sc->reg_lock);
10426 	if (hw_off_limits(sc))
10427 		rc = ENXIO;
10428 	else
10429 		t4_tp_get_rdma_stats(sc, &stats, 0);
10430 	mtx_unlock(&sc->reg_lock);
10431 	if (rc != 0)
10432 		return (rc);
10433 
10434 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10435 	if (sb == NULL)
10436 		return (ENOMEM);
10437 
10438 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
10439 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
10440 
10441 	rc = sbuf_finish(sb);
10442 	sbuf_delete(sb);
10443 
10444 	return (rc);
10445 }
10446 
10447 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)10448 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
10449 {
10450 	struct adapter *sc = arg1;
10451 	struct sbuf *sb;
10452 	int rc;
10453 	struct tp_tcp_stats v4, v6;
10454 
10455 	rc = sysctl_wire_old_buffer(req, 0);
10456 	if (rc != 0)
10457 		return (rc);
10458 
10459 	mtx_lock(&sc->reg_lock);
10460 	if (hw_off_limits(sc))
10461 		rc = ENXIO;
10462 	else
10463 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
10464 	mtx_unlock(&sc->reg_lock);
10465 	if (rc != 0)
10466 		return (rc);
10467 
10468 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10469 	if (sb == NULL)
10470 		return (ENOMEM);
10471 
10472 	sbuf_printf(sb,
10473 	    "                                IP                 IPv6\n");
10474 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
10475 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
10476 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
10477 	    v4.tcp_in_segs, v6.tcp_in_segs);
10478 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
10479 	    v4.tcp_out_segs, v6.tcp_out_segs);
10480 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
10481 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
10482 
10483 	rc = sbuf_finish(sb);
10484 	sbuf_delete(sb);
10485 
10486 	return (rc);
10487 }
10488 
10489 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)10490 sysctl_tids(SYSCTL_HANDLER_ARGS)
10491 {
10492 	struct adapter *sc = arg1;
10493 	struct sbuf *sb;
10494 	int rc;
10495 	uint32_t x, y;
10496 	struct tid_info *t = &sc->tids;
10497 
10498 	rc = sysctl_wire_old_buffer(req, 0);
10499 	if (rc != 0)
10500 		return (rc);
10501 
10502 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10503 	if (sb == NULL)
10504 		return (ENOMEM);
10505 
10506 	if (t->natids) {
10507 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
10508 		    t->atids_in_use);
10509 	}
10510 
10511 	if (t->nhpftids) {
10512 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
10513 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
10514 	}
10515 
10516 	if (t->ntids) {
10517 		bool hashen = false;
10518 
10519 		mtx_lock(&sc->reg_lock);
10520 		if (hw_off_limits(sc))
10521 			rc = ENXIO;
10522 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10523 			hashen = true;
10524 			if (chip_id(sc) <= CHELSIO_T5) {
10525 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
10526 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
10527 			} else {
10528 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
10529 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
10530 			}
10531 		}
10532 		mtx_unlock(&sc->reg_lock);
10533 		if (rc != 0)
10534 			goto done;
10535 
10536 		sbuf_printf(sb, "TID range: ");
10537 		if (hashen) {
10538 			if (x)
10539 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
10540 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
10541 		} else {
10542 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
10543 			    t->ntids - 1);
10544 		}
10545 		sbuf_printf(sb, ", in use: %u\n",
10546 		    atomic_load_acq_int(&t->tids_in_use));
10547 	}
10548 
10549 	if (t->nstids) {
10550 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
10551 		    t->stid_base + t->nstids - 1, t->stids_in_use);
10552 	}
10553 
10554 	if (t->nftids) {
10555 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
10556 		    t->ftid_end, t->ftids_in_use);
10557 	}
10558 
10559 	if (t->netids) {
10560 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
10561 		    t->etid_base + t->netids - 1, t->etids_in_use);
10562 	}
10563 
10564 	mtx_lock(&sc->reg_lock);
10565 	if (hw_off_limits(sc))
10566 		rc = ENXIO;
10567 	else {
10568 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
10569 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
10570 	}
10571 	mtx_unlock(&sc->reg_lock);
10572 	if (rc != 0)
10573 		goto done;
10574 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
10575 done:
10576 	if (rc == 0)
10577 		rc = sbuf_finish(sb);
10578 	else
10579 		(void)sbuf_finish(sb);
10580 	sbuf_delete(sb);
10581 
10582 	return (rc);
10583 }
10584 
10585 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)10586 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
10587 {
10588 	struct adapter *sc = arg1;
10589 	struct sbuf *sb;
10590 	int rc;
10591 	struct tp_err_stats stats;
10592 
10593 	rc = sysctl_wire_old_buffer(req, 0);
10594 	if (rc != 0)
10595 		return (rc);
10596 
10597 	mtx_lock(&sc->reg_lock);
10598 	if (hw_off_limits(sc))
10599 		rc = ENXIO;
10600 	else
10601 		t4_tp_get_err_stats(sc, &stats, 0);
10602 	mtx_unlock(&sc->reg_lock);
10603 	if (rc != 0)
10604 		return (rc);
10605 
10606 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10607 	if (sb == NULL)
10608 		return (ENOMEM);
10609 
10610 	if (sc->chip_params->nchan > 2) {
10611 		sbuf_printf(sb, "                 channel 0  channel 1"
10612 		    "  channel 2  channel 3\n");
10613 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
10614 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
10615 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
10616 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
10617 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
10618 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
10619 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
10620 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
10621 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
10622 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
10623 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
10624 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
10625 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
10626 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
10627 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
10628 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
10629 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
10630 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
10631 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
10632 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
10633 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
10634 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
10635 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
10636 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
10637 	} else {
10638 		sbuf_printf(sb, "                 channel 0  channel 1\n");
10639 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
10640 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
10641 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
10642 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
10643 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
10644 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
10645 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
10646 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
10647 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
10648 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
10649 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
10650 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
10651 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
10652 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
10653 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
10654 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
10655 	}
10656 
10657 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
10658 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
10659 
10660 	rc = sbuf_finish(sb);
10661 	sbuf_delete(sb);
10662 
10663 	return (rc);
10664 }
10665 
10666 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)10667 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
10668 {
10669 	struct adapter *sc = arg1;
10670 	struct sbuf *sb;
10671 	int rc;
10672 	struct tp_tnl_stats stats;
10673 
10674 	rc = sysctl_wire_old_buffer(req, 0);
10675 	if (rc != 0)
10676 		return(rc);
10677 
10678 	mtx_lock(&sc->reg_lock);
10679 	if (hw_off_limits(sc))
10680 		rc = ENXIO;
10681 	else
10682 		t4_tp_get_tnl_stats(sc, &stats, 1);
10683 	mtx_unlock(&sc->reg_lock);
10684 	if (rc != 0)
10685 		return (rc);
10686 
10687 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10688 	if (sb == NULL)
10689 		return (ENOMEM);
10690 
10691 	if (sc->chip_params->nchan > 2) {
10692 		sbuf_printf(sb, "           channel 0  channel 1"
10693 		    "  channel 2  channel 3\n");
10694 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
10695 		    stats.out_pkt[0], stats.out_pkt[1],
10696 		    stats.out_pkt[2], stats.out_pkt[3]);
10697 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
10698 		    stats.in_pkt[0], stats.in_pkt[1],
10699 		    stats.in_pkt[2], stats.in_pkt[3]);
10700 	} else {
10701 		sbuf_printf(sb, "           channel 0  channel 1\n");
10702 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
10703 		    stats.out_pkt[0], stats.out_pkt[1]);
10704 		sbuf_printf(sb, "InPkts:   %10u %10u",
10705 		    stats.in_pkt[0], stats.in_pkt[1]);
10706 	}
10707 
10708 	rc = sbuf_finish(sb);
10709 	sbuf_delete(sb);
10710 
10711 	return (rc);
10712 }
10713 
10714 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)10715 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
10716 {
10717 	struct adapter *sc = arg1;
10718 	struct tp_params *tpp = &sc->params.tp;
10719 	u_int mask;
10720 	int rc;
10721 
10722 	mask = tpp->la_mask >> 16;
10723 	rc = sysctl_handle_int(oidp, &mask, 0, req);
10724 	if (rc != 0 || req->newptr == NULL)
10725 		return (rc);
10726 	if (mask > 0xffff)
10727 		return (EINVAL);
10728 	mtx_lock(&sc->reg_lock);
10729 	if (hw_off_limits(sc))
10730 		rc = ENXIO;
10731 	else {
10732 		tpp->la_mask = mask << 16;
10733 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
10734 		    tpp->la_mask);
10735 	}
10736 	mtx_unlock(&sc->reg_lock);
10737 
10738 	return (rc);
10739 }
10740 
10741 struct field_desc {
10742 	const char *name;
10743 	u_int start;
10744 	u_int width;
10745 };
10746 
10747 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)10748 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
10749 {
10750 	char buf[32];
10751 	int line_size = 0;
10752 
10753 	while (f->name) {
10754 		uint64_t mask = (1ULL << f->width) - 1;
10755 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
10756 		    ((uintmax_t)v >> f->start) & mask);
10757 
10758 		if (line_size + len >= 79) {
10759 			line_size = 8;
10760 			sbuf_printf(sb, "\n        ");
10761 		}
10762 		sbuf_printf(sb, "%s ", buf);
10763 		line_size += len + 1;
10764 		f++;
10765 	}
10766 	sbuf_printf(sb, "\n");
10767 }
10768 
10769 static const struct field_desc tp_la0[] = {
10770 	{ "RcfOpCodeOut", 60, 4 },
10771 	{ "State", 56, 4 },
10772 	{ "WcfState", 52, 4 },
10773 	{ "RcfOpcSrcOut", 50, 2 },
10774 	{ "CRxError", 49, 1 },
10775 	{ "ERxError", 48, 1 },
10776 	{ "SanityFailed", 47, 1 },
10777 	{ "SpuriousMsg", 46, 1 },
10778 	{ "FlushInputMsg", 45, 1 },
10779 	{ "FlushInputCpl", 44, 1 },
10780 	{ "RssUpBit", 43, 1 },
10781 	{ "RssFilterHit", 42, 1 },
10782 	{ "Tid", 32, 10 },
10783 	{ "InitTcb", 31, 1 },
10784 	{ "LineNumber", 24, 7 },
10785 	{ "Emsg", 23, 1 },
10786 	{ "EdataOut", 22, 1 },
10787 	{ "Cmsg", 21, 1 },
10788 	{ "CdataOut", 20, 1 },
10789 	{ "EreadPdu", 19, 1 },
10790 	{ "CreadPdu", 18, 1 },
10791 	{ "TunnelPkt", 17, 1 },
10792 	{ "RcfPeerFin", 16, 1 },
10793 	{ "RcfReasonOut", 12, 4 },
10794 	{ "TxCchannel", 10, 2 },
10795 	{ "RcfTxChannel", 8, 2 },
10796 	{ "RxEchannel", 6, 2 },
10797 	{ "RcfRxChannel", 5, 1 },
10798 	{ "RcfDataOutSrdy", 4, 1 },
10799 	{ "RxDvld", 3, 1 },
10800 	{ "RxOoDvld", 2, 1 },
10801 	{ "RxCongestion", 1, 1 },
10802 	{ "TxCongestion", 0, 1 },
10803 	{ NULL }
10804 };
10805 
10806 static const struct field_desc tp_la1[] = {
10807 	{ "CplCmdIn", 56, 8 },
10808 	{ "CplCmdOut", 48, 8 },
10809 	{ "ESynOut", 47, 1 },
10810 	{ "EAckOut", 46, 1 },
10811 	{ "EFinOut", 45, 1 },
10812 	{ "ERstOut", 44, 1 },
10813 	{ "SynIn", 43, 1 },
10814 	{ "AckIn", 42, 1 },
10815 	{ "FinIn", 41, 1 },
10816 	{ "RstIn", 40, 1 },
10817 	{ "DataIn", 39, 1 },
10818 	{ "DataInVld", 38, 1 },
10819 	{ "PadIn", 37, 1 },
10820 	{ "RxBufEmpty", 36, 1 },
10821 	{ "RxDdp", 35, 1 },
10822 	{ "RxFbCongestion", 34, 1 },
10823 	{ "TxFbCongestion", 33, 1 },
10824 	{ "TxPktSumSrdy", 32, 1 },
10825 	{ "RcfUlpType", 28, 4 },
10826 	{ "Eread", 27, 1 },
10827 	{ "Ebypass", 26, 1 },
10828 	{ "Esave", 25, 1 },
10829 	{ "Static0", 24, 1 },
10830 	{ "Cread", 23, 1 },
10831 	{ "Cbypass", 22, 1 },
10832 	{ "Csave", 21, 1 },
10833 	{ "CPktOut", 20, 1 },
10834 	{ "RxPagePoolFull", 18, 2 },
10835 	{ "RxLpbkPkt", 17, 1 },
10836 	{ "TxLpbkPkt", 16, 1 },
10837 	{ "RxVfValid", 15, 1 },
10838 	{ "SynLearned", 14, 1 },
10839 	{ "SetDelEntry", 13, 1 },
10840 	{ "SetInvEntry", 12, 1 },
10841 	{ "CpcmdDvld", 11, 1 },
10842 	{ "CpcmdSave", 10, 1 },
10843 	{ "RxPstructsFull", 8, 2 },
10844 	{ "EpcmdDvld", 7, 1 },
10845 	{ "EpcmdFlush", 6, 1 },
10846 	{ "EpcmdTrimPrefix", 5, 1 },
10847 	{ "EpcmdTrimPostfix", 4, 1 },
10848 	{ "ERssIp4Pkt", 3, 1 },
10849 	{ "ERssIp6Pkt", 2, 1 },
10850 	{ "ERssTcpUdpPkt", 1, 1 },
10851 	{ "ERssFceFipPkt", 0, 1 },
10852 	{ NULL }
10853 };
10854 
10855 static const struct field_desc tp_la2[] = {
10856 	{ "CplCmdIn", 56, 8 },
10857 	{ "MpsVfVld", 55, 1 },
10858 	{ "MpsPf", 52, 3 },
10859 	{ "MpsVf", 44, 8 },
10860 	{ "SynIn", 43, 1 },
10861 	{ "AckIn", 42, 1 },
10862 	{ "FinIn", 41, 1 },
10863 	{ "RstIn", 40, 1 },
10864 	{ "DataIn", 39, 1 },
10865 	{ "DataInVld", 38, 1 },
10866 	{ "PadIn", 37, 1 },
10867 	{ "RxBufEmpty", 36, 1 },
10868 	{ "RxDdp", 35, 1 },
10869 	{ "RxFbCongestion", 34, 1 },
10870 	{ "TxFbCongestion", 33, 1 },
10871 	{ "TxPktSumSrdy", 32, 1 },
10872 	{ "RcfUlpType", 28, 4 },
10873 	{ "Eread", 27, 1 },
10874 	{ "Ebypass", 26, 1 },
10875 	{ "Esave", 25, 1 },
10876 	{ "Static0", 24, 1 },
10877 	{ "Cread", 23, 1 },
10878 	{ "Cbypass", 22, 1 },
10879 	{ "Csave", 21, 1 },
10880 	{ "CPktOut", 20, 1 },
10881 	{ "RxPagePoolFull", 18, 2 },
10882 	{ "RxLpbkPkt", 17, 1 },
10883 	{ "TxLpbkPkt", 16, 1 },
10884 	{ "RxVfValid", 15, 1 },
10885 	{ "SynLearned", 14, 1 },
10886 	{ "SetDelEntry", 13, 1 },
10887 	{ "SetInvEntry", 12, 1 },
10888 	{ "CpcmdDvld", 11, 1 },
10889 	{ "CpcmdSave", 10, 1 },
10890 	{ "RxPstructsFull", 8, 2 },
10891 	{ "EpcmdDvld", 7, 1 },
10892 	{ "EpcmdFlush", 6, 1 },
10893 	{ "EpcmdTrimPrefix", 5, 1 },
10894 	{ "EpcmdTrimPostfix", 4, 1 },
10895 	{ "ERssIp4Pkt", 3, 1 },
10896 	{ "ERssIp6Pkt", 2, 1 },
10897 	{ "ERssTcpUdpPkt", 1, 1 },
10898 	{ "ERssFceFipPkt", 0, 1 },
10899 	{ NULL }
10900 };
10901 
10902 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)10903 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
10904 {
10905 
10906 	field_desc_show(sb, *p, tp_la0);
10907 }
10908 
10909 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)10910 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
10911 {
10912 
10913 	if (idx)
10914 		sbuf_printf(sb, "\n");
10915 	field_desc_show(sb, p[0], tp_la0);
10916 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
10917 		field_desc_show(sb, p[1], tp_la0);
10918 }
10919 
10920 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)10921 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
10922 {
10923 
10924 	if (idx)
10925 		sbuf_printf(sb, "\n");
10926 	field_desc_show(sb, p[0], tp_la0);
10927 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
10928 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
10929 }
10930 
10931 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)10932 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
10933 {
10934 	struct adapter *sc = arg1;
10935 	struct sbuf *sb;
10936 	uint64_t *buf, *p;
10937 	int rc;
10938 	u_int i, inc;
10939 	void (*show_func)(struct sbuf *, uint64_t *, int);
10940 
10941 	rc = sysctl_wire_old_buffer(req, 0);
10942 	if (rc != 0)
10943 		return (rc);
10944 
10945 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10946 	if (sb == NULL)
10947 		return (ENOMEM);
10948 
10949 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
10950 
10951 	mtx_lock(&sc->reg_lock);
10952 	if (hw_off_limits(sc))
10953 		rc = ENXIO;
10954 	else {
10955 		t4_tp_read_la(sc, buf, NULL);
10956 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
10957 		case 2:
10958 			inc = 2;
10959 			show_func = tp_la_show2;
10960 			break;
10961 		case 3:
10962 			inc = 2;
10963 			show_func = tp_la_show3;
10964 			break;
10965 		default:
10966 			inc = 1;
10967 			show_func = tp_la_show;
10968 		}
10969 	}
10970 	mtx_unlock(&sc->reg_lock);
10971 	if (rc != 0)
10972 		goto done;
10973 
10974 	p = buf;
10975 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
10976 		(*show_func)(sb, p, i);
10977 	rc = sbuf_finish(sb);
10978 done:
10979 	sbuf_delete(sb);
10980 	free(buf, M_CXGBE);
10981 	return (rc);
10982 }
10983 
10984 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)10985 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
10986 {
10987 	struct adapter *sc = arg1;
10988 	struct sbuf *sb;
10989 	int rc;
10990 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
10991 
10992 	rc = sysctl_wire_old_buffer(req, 0);
10993 	if (rc != 0)
10994 		return (rc);
10995 
10996 	mtx_lock(&sc->reg_lock);
10997 	if (hw_off_limits(sc))
10998 		rc = ENXIO;
10999 	else
11000 		t4_get_chan_txrate(sc, nrate, orate);
11001 	mtx_unlock(&sc->reg_lock);
11002 	if (rc != 0)
11003 		return (rc);
11004 
11005 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11006 	if (sb == NULL)
11007 		return (ENOMEM);
11008 
11009 	if (sc->chip_params->nchan > 2) {
11010 		sbuf_printf(sb, "              channel 0   channel 1"
11011 		    "   channel 2   channel 3\n");
11012 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
11013 		    nrate[0], nrate[1], nrate[2], nrate[3]);
11014 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
11015 		    orate[0], orate[1], orate[2], orate[3]);
11016 	} else {
11017 		sbuf_printf(sb, "              channel 0   channel 1\n");
11018 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
11019 		    nrate[0], nrate[1]);
11020 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
11021 		    orate[0], orate[1]);
11022 	}
11023 
11024 	rc = sbuf_finish(sb);
11025 	sbuf_delete(sb);
11026 
11027 	return (rc);
11028 }
11029 
11030 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)11031 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
11032 {
11033 	struct adapter *sc = arg1;
11034 	struct sbuf *sb;
11035 	uint32_t *buf, *p;
11036 	int rc, i;
11037 
11038 	rc = sysctl_wire_old_buffer(req, 0);
11039 	if (rc != 0)
11040 		return (rc);
11041 
11042 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11043 	if (sb == NULL)
11044 		return (ENOMEM);
11045 
11046 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
11047 	    M_ZERO | M_WAITOK);
11048 
11049 	mtx_lock(&sc->reg_lock);
11050 	if (hw_off_limits(sc))
11051 		rc = ENXIO;
11052 	else
11053 		t4_ulprx_read_la(sc, buf);
11054 	mtx_unlock(&sc->reg_lock);
11055 	if (rc != 0)
11056 		goto done;
11057 
11058 	p = buf;
11059 	sbuf_printf(sb, "      Pcmd        Type   Message"
11060 	    "                Data");
11061 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
11062 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
11063 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
11064 	}
11065 	rc = sbuf_finish(sb);
11066 done:
11067 	sbuf_delete(sb);
11068 	free(buf, M_CXGBE);
11069 	return (rc);
11070 }
11071 
11072 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)11073 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
11074 {
11075 	struct adapter *sc = arg1;
11076 	struct sbuf *sb;
11077 	int rc;
11078 	uint32_t cfg, s1, s2;
11079 
11080 	MPASS(chip_id(sc) >= CHELSIO_T5);
11081 
11082 	rc = sysctl_wire_old_buffer(req, 0);
11083 	if (rc != 0)
11084 		return (rc);
11085 
11086 	mtx_lock(&sc->reg_lock);
11087 	if (hw_off_limits(sc))
11088 		rc = ENXIO;
11089 	else {
11090 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
11091 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
11092 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
11093 	}
11094 	mtx_unlock(&sc->reg_lock);
11095 	if (rc != 0)
11096 		return (rc);
11097 
11098 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11099 	if (sb == NULL)
11100 		return (ENOMEM);
11101 
11102 	if (G_STATSOURCE_T5(cfg) == 7) {
11103 		int mode;
11104 
11105 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
11106 		if (mode == 0)
11107 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
11108 		else if (mode == 1)
11109 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
11110 		else
11111 			sbuf_printf(sb, "unknown mode %d", mode);
11112 	}
11113 	rc = sbuf_finish(sb);
11114 	sbuf_delete(sb);
11115 
11116 	return (rc);
11117 }
11118 
11119 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)11120 sysctl_cpus(SYSCTL_HANDLER_ARGS)
11121 {
11122 	struct adapter *sc = arg1;
11123 	enum cpu_sets op = arg2;
11124 	cpuset_t cpuset;
11125 	struct sbuf *sb;
11126 	int i, rc;
11127 
11128 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
11129 
11130 	CPU_ZERO(&cpuset);
11131 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
11132 	if (rc != 0)
11133 		return (rc);
11134 
11135 	rc = sysctl_wire_old_buffer(req, 0);
11136 	if (rc != 0)
11137 		return (rc);
11138 
11139 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11140 	if (sb == NULL)
11141 		return (ENOMEM);
11142 
11143 	CPU_FOREACH(i)
11144 		sbuf_printf(sb, "%d ", i);
11145 	rc = sbuf_finish(sb);
11146 	sbuf_delete(sb);
11147 
11148 	return (rc);
11149 }
11150 
11151 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)11152 sysctl_reset(SYSCTL_HANDLER_ARGS)
11153 {
11154 	struct adapter *sc = arg1;
11155 	u_int val;
11156 	int rc;
11157 
11158 	val = atomic_load_int(&sc->num_resets);
11159 	rc = sysctl_handle_int(oidp, &val, 0, req);
11160 	if (rc != 0 || req->newptr == NULL)
11161 		return (rc);
11162 
11163 	if (val == 0) {
11164 		/* Zero out the counter that tracks reset. */
11165 		atomic_store_int(&sc->num_resets, 0);
11166 		return (0);
11167 	}
11168 
11169 	if (val != 1)
11170 		return (EINVAL);	/* 0 or 1 are the only legal values */
11171 
11172 	if (hw_off_limits(sc))		/* harmless race */
11173 		return (EALREADY);
11174 
11175 	taskqueue_enqueue(reset_tq, &sc->reset_task);
11176 	return (0);
11177 }
11178 
11179 #ifdef TCP_OFFLOAD
11180 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)11181 sysctl_tls(SYSCTL_HANDLER_ARGS)
11182 {
11183 	struct adapter *sc = arg1;
11184 	int i, j, v, rc;
11185 	struct vi_info *vi;
11186 
11187 	v = sc->tt.tls;
11188 	rc = sysctl_handle_int(oidp, &v, 0, req);
11189 	if (rc != 0 || req->newptr == NULL)
11190 		return (rc);
11191 
11192 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11193 		return (ENOTSUP);
11194 
11195 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
11196 	if (rc)
11197 		return (rc);
11198 	if (hw_off_limits(sc))
11199 		rc = ENXIO;
11200 	else {
11201 		sc->tt.tls = !!v;
11202 		for_each_port(sc, i) {
11203 			for_each_vi(sc->port[i], j, vi) {
11204 				if (vi->flags & VI_INIT_DONE)
11205 					t4_update_fl_bufsize(vi->ifp);
11206 			}
11207 		}
11208 	}
11209 	end_synchronized_op(sc, 0);
11210 
11211 	return (rc);
11212 
11213 }
11214 
11215 static int
sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS)11216 sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS)
11217 {
11218 	struct adapter *sc = arg1;
11219 	int *old_ports, *new_ports;
11220 	int i, new_count, rc;
11221 
11222 	if (req->newptr == NULL && req->oldptr == NULL)
11223 		return (SYSCTL_OUT(req, NULL, imax(sc->tt.num_tls_rx_ports, 1) *
11224 		    sizeof(sc->tt.tls_rx_ports[0])));
11225 
11226 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tlsrx");
11227 	if (rc)
11228 		return (rc);
11229 
11230 	if (hw_off_limits(sc)) {
11231 		rc = ENXIO;
11232 		goto done;
11233 	}
11234 
11235 	if (sc->tt.num_tls_rx_ports == 0) {
11236 		i = -1;
11237 		rc = SYSCTL_OUT(req, &i, sizeof(i));
11238 	} else
11239 		rc = SYSCTL_OUT(req, sc->tt.tls_rx_ports,
11240 		    sc->tt.num_tls_rx_ports * sizeof(sc->tt.tls_rx_ports[0]));
11241 	if (rc == 0 && req->newptr != NULL) {
11242 		new_count = req->newlen / sizeof(new_ports[0]);
11243 		new_ports = malloc(new_count * sizeof(new_ports[0]), M_CXGBE,
11244 		    M_WAITOK);
11245 		rc = SYSCTL_IN(req, new_ports, new_count *
11246 		    sizeof(new_ports[0]));
11247 		if (rc)
11248 			goto err;
11249 
11250 		/* Allow setting to a single '-1' to clear the list. */
11251 		if (new_count == 1 && new_ports[0] == -1) {
11252 			ADAPTER_LOCK(sc);
11253 			old_ports = sc->tt.tls_rx_ports;
11254 			sc->tt.tls_rx_ports = NULL;
11255 			sc->tt.num_tls_rx_ports = 0;
11256 			ADAPTER_UNLOCK(sc);
11257 			free(old_ports, M_CXGBE);
11258 		} else {
11259 			for (i = 0; i < new_count; i++) {
11260 				if (new_ports[i] < 1 ||
11261 				    new_ports[i] > IPPORT_MAX) {
11262 					rc = EINVAL;
11263 					goto err;
11264 				}
11265 			}
11266 
11267 			ADAPTER_LOCK(sc);
11268 			old_ports = sc->tt.tls_rx_ports;
11269 			sc->tt.tls_rx_ports = new_ports;
11270 			sc->tt.num_tls_rx_ports = new_count;
11271 			ADAPTER_UNLOCK(sc);
11272 			free(old_ports, M_CXGBE);
11273 			new_ports = NULL;
11274 		}
11275 	err:
11276 		free(new_ports, M_CXGBE);
11277 	}
11278 done:
11279 	end_synchronized_op(sc, 0);
11280 	return (rc);
11281 }
11282 
11283 static int
sysctl_tls_rx_timeout(SYSCTL_HANDLER_ARGS)11284 sysctl_tls_rx_timeout(SYSCTL_HANDLER_ARGS)
11285 {
11286 	struct adapter *sc = arg1;
11287 	int v, rc;
11288 
11289 	v = sc->tt.tls_rx_timeout;
11290 	rc = sysctl_handle_int(oidp, &v, 0, req);
11291 	if (rc != 0 || req->newptr == NULL)
11292 		return (rc);
11293 
11294 	if (v < 0)
11295 		return (EINVAL);
11296 
11297 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11298 		return (ENOTSUP);
11299 
11300 	sc->tt.tls_rx_timeout = v;
11301 
11302 	return (0);
11303 
11304 }
11305 
11306 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)11307 unit_conv(char *buf, size_t len, u_int val, u_int factor)
11308 {
11309 	u_int rem = val % factor;
11310 
11311 	if (rem == 0)
11312 		snprintf(buf, len, "%u", val / factor);
11313 	else {
11314 		while (rem % 10 == 0)
11315 			rem /= 10;
11316 		snprintf(buf, len, "%u.%u", val / factor, rem);
11317 	}
11318 }
11319 
11320 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)11321 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
11322 {
11323 	struct adapter *sc = arg1;
11324 	char buf[16];
11325 	u_int res, re;
11326 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11327 
11328 	mtx_lock(&sc->reg_lock);
11329 	if (hw_off_limits(sc))
11330 		res = (u_int)-1;
11331 	else
11332 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
11333 	mtx_unlock(&sc->reg_lock);
11334 	if (res == (u_int)-1)
11335 		return (ENXIO);
11336 
11337 	switch (arg2) {
11338 	case 0:
11339 		/* timer_tick */
11340 		re = G_TIMERRESOLUTION(res);
11341 		break;
11342 	case 1:
11343 		/* TCP timestamp tick */
11344 		re = G_TIMESTAMPRESOLUTION(res);
11345 		break;
11346 	case 2:
11347 		/* DACK tick */
11348 		re = G_DELAYEDACKRESOLUTION(res);
11349 		break;
11350 	default:
11351 		return (EDOOFUS);
11352 	}
11353 
11354 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
11355 
11356 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
11357 }
11358 
11359 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)11360 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
11361 {
11362 	struct adapter *sc = arg1;
11363 	int rc;
11364 	u_int dack_tmr, dack_re, v;
11365 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11366 
11367 	mtx_lock(&sc->reg_lock);
11368 	if (hw_off_limits(sc))
11369 		rc = ENXIO;
11370 	else {
11371 		rc = 0;
11372 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
11373 		    A_TP_TIMER_RESOLUTION));
11374 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
11375 	}
11376 	mtx_unlock(&sc->reg_lock);
11377 	if (rc != 0)
11378 		return (rc);
11379 
11380 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
11381 
11382 	return (sysctl_handle_int(oidp, &v, 0, req));
11383 }
11384 
11385 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)11386 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
11387 {
11388 	struct adapter *sc = arg1;
11389 	int rc, reg = arg2;
11390 	u_int tre;
11391 	u_long tp_tick_us, v;
11392 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11393 
11394 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
11395 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
11396 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
11397 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
11398 
11399 	mtx_lock(&sc->reg_lock);
11400 	if (hw_off_limits(sc))
11401 		rc = ENXIO;
11402 	else {
11403 		rc = 0;
11404 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
11405 		tp_tick_us = (cclk_ps << tre) / 1000000;
11406 		if (reg == A_TP_INIT_SRTT)
11407 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
11408 		else
11409 			v = tp_tick_us * t4_read_reg(sc, reg);
11410 	}
11411 	mtx_unlock(&sc->reg_lock);
11412 	if (rc != 0)
11413 		return (rc);
11414 	else
11415 		return (sysctl_handle_long(oidp, &v, 0, req));
11416 }
11417 
11418 /*
11419  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
11420  * passed to this function.
11421  */
11422 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)11423 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
11424 {
11425 	struct adapter *sc = arg1;
11426 	int rc, idx = arg2;
11427 	u_int v;
11428 
11429 	MPASS(idx >= 0 && idx <= 24);
11430 
11431 	mtx_lock(&sc->reg_lock);
11432 	if (hw_off_limits(sc))
11433 		rc = ENXIO;
11434 	else {
11435 		rc = 0;
11436 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
11437 	}
11438 	mtx_unlock(&sc->reg_lock);
11439 	if (rc != 0)
11440 		return (rc);
11441 	else
11442 		return (sysctl_handle_int(oidp, &v, 0, req));
11443 }
11444 
11445 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)11446 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
11447 {
11448 	struct adapter *sc = arg1;
11449 	int rc, idx = arg2;
11450 	u_int shift, v, r;
11451 
11452 	MPASS(idx >= 0 && idx < 16);
11453 
11454 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
11455 	shift = (idx & 3) << 3;
11456 	mtx_lock(&sc->reg_lock);
11457 	if (hw_off_limits(sc))
11458 		rc = ENXIO;
11459 	else {
11460 		rc = 0;
11461 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
11462 	}
11463 	mtx_unlock(&sc->reg_lock);
11464 	if (rc != 0)
11465 		return (rc);
11466 	else
11467 		return (sysctl_handle_int(oidp, &v, 0, req));
11468 }
11469 
11470 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)11471 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
11472 {
11473 	struct vi_info *vi = arg1;
11474 	struct adapter *sc = vi->adapter;
11475 	int idx, rc, i;
11476 	struct sge_ofld_rxq *ofld_rxq;
11477 	uint8_t v;
11478 
11479 	idx = vi->ofld_tmr_idx;
11480 
11481 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11482 	if (rc != 0 || req->newptr == NULL)
11483 		return (rc);
11484 
11485 	if (idx < 0 || idx >= SGE_NTIMERS)
11486 		return (EINVAL);
11487 
11488 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11489 	    "t4otmr");
11490 	if (rc)
11491 		return (rc);
11492 
11493 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
11494 	for_each_ofld_rxq(vi, i, ofld_rxq) {
11495 #ifdef atomic_store_rel_8
11496 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
11497 #else
11498 		ofld_rxq->iq.intr_params = v;
11499 #endif
11500 	}
11501 	vi->ofld_tmr_idx = idx;
11502 
11503 	end_synchronized_op(sc, LOCK_HELD);
11504 	return (0);
11505 }
11506 
11507 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)11508 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
11509 {
11510 	struct vi_info *vi = arg1;
11511 	struct adapter *sc = vi->adapter;
11512 	int idx, rc;
11513 
11514 	idx = vi->ofld_pktc_idx;
11515 
11516 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11517 	if (rc != 0 || req->newptr == NULL)
11518 		return (rc);
11519 
11520 	if (idx < -1 || idx >= SGE_NCOUNTERS)
11521 		return (EINVAL);
11522 
11523 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11524 	    "t4opktc");
11525 	if (rc)
11526 		return (rc);
11527 
11528 	if (vi->flags & VI_INIT_DONE)
11529 		rc = EBUSY; /* cannot be changed once the queues are created */
11530 	else
11531 		vi->ofld_pktc_idx = idx;
11532 
11533 	end_synchronized_op(sc, LOCK_HELD);
11534 	return (rc);
11535 }
11536 #endif
11537 
11538 static int
get_sge_context(struct adapter * sc,struct t4_sge_context * cntxt)11539 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
11540 {
11541 	int rc;
11542 
11543 	if (cntxt->cid > M_CTXTQID)
11544 		return (EINVAL);
11545 
11546 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
11547 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
11548 		return (EINVAL);
11549 
11550 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
11551 	if (rc)
11552 		return (rc);
11553 
11554 	if (hw_off_limits(sc)) {
11555 		rc = ENXIO;
11556 		goto done;
11557 	}
11558 
11559 	if (sc->flags & FW_OK) {
11560 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
11561 		    &cntxt->data[0]);
11562 		if (rc == 0)
11563 			goto done;
11564 	}
11565 
11566 	/*
11567 	 * Read via firmware failed or wasn't even attempted.  Read directly via
11568 	 * the backdoor.
11569 	 */
11570 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
11571 done:
11572 	end_synchronized_op(sc, 0);
11573 	return (rc);
11574 }
11575 
11576 static int
load_fw(struct adapter * sc,struct t4_data * fw)11577 load_fw(struct adapter *sc, struct t4_data *fw)
11578 {
11579 	int rc;
11580 	uint8_t *fw_data;
11581 
11582 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
11583 	if (rc)
11584 		return (rc);
11585 
11586 	if (hw_off_limits(sc)) {
11587 		rc = ENXIO;
11588 		goto done;
11589 	}
11590 
11591 	/*
11592 	 * The firmware, with the sole exception of the memory parity error
11593 	 * handler, runs from memory and not flash.  It is almost always safe to
11594 	 * install a new firmware on a running system.  Just set bit 1 in
11595 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
11596 	 */
11597 	if (sc->flags & FULL_INIT_DONE &&
11598 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
11599 		rc = EBUSY;
11600 		goto done;
11601 	}
11602 
11603 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
11604 
11605 	rc = copyin(fw->data, fw_data, fw->len);
11606 	if (rc == 0)
11607 		rc = -t4_load_fw(sc, fw_data, fw->len);
11608 
11609 	free(fw_data, M_CXGBE);
11610 done:
11611 	end_synchronized_op(sc, 0);
11612 	return (rc);
11613 }
11614 
11615 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)11616 load_cfg(struct adapter *sc, struct t4_data *cfg)
11617 {
11618 	int rc;
11619 	uint8_t *cfg_data = NULL;
11620 
11621 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11622 	if (rc)
11623 		return (rc);
11624 
11625 	if (hw_off_limits(sc)) {
11626 		rc = ENXIO;
11627 		goto done;
11628 	}
11629 
11630 	if (cfg->len == 0) {
11631 		/* clear */
11632 		rc = -t4_load_cfg(sc, NULL, 0);
11633 		goto done;
11634 	}
11635 
11636 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
11637 
11638 	rc = copyin(cfg->data, cfg_data, cfg->len);
11639 	if (rc == 0)
11640 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
11641 
11642 	free(cfg_data, M_CXGBE);
11643 done:
11644 	end_synchronized_op(sc, 0);
11645 	return (rc);
11646 }
11647 
11648 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)11649 load_boot(struct adapter *sc, struct t4_bootrom *br)
11650 {
11651 	int rc;
11652 	uint8_t *br_data = NULL;
11653 	u_int offset;
11654 
11655 	if (br->len > 1024 * 1024)
11656 		return (EFBIG);
11657 
11658 	if (br->pf_offset == 0) {
11659 		/* pfidx */
11660 		if (br->pfidx_addr > 7)
11661 			return (EINVAL);
11662 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
11663 		    A_PCIE_PF_EXPROM_OFST)));
11664 	} else if (br->pf_offset == 1) {
11665 		/* offset */
11666 		offset = G_OFFSET(br->pfidx_addr);
11667 	} else {
11668 		return (EINVAL);
11669 	}
11670 
11671 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
11672 	if (rc)
11673 		return (rc);
11674 
11675 	if (hw_off_limits(sc)) {
11676 		rc = ENXIO;
11677 		goto done;
11678 	}
11679 
11680 	if (br->len == 0) {
11681 		/* clear */
11682 		rc = -t4_load_boot(sc, NULL, offset, 0);
11683 		goto done;
11684 	}
11685 
11686 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
11687 
11688 	rc = copyin(br->data, br_data, br->len);
11689 	if (rc == 0)
11690 		rc = -t4_load_boot(sc, br_data, offset, br->len);
11691 
11692 	free(br_data, M_CXGBE);
11693 done:
11694 	end_synchronized_op(sc, 0);
11695 	return (rc);
11696 }
11697 
11698 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)11699 load_bootcfg(struct adapter *sc, struct t4_data *bc)
11700 {
11701 	int rc;
11702 	uint8_t *bc_data = NULL;
11703 
11704 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11705 	if (rc)
11706 		return (rc);
11707 
11708 	if (hw_off_limits(sc)) {
11709 		rc = ENXIO;
11710 		goto done;
11711 	}
11712 
11713 	if (bc->len == 0) {
11714 		/* clear */
11715 		rc = -t4_load_bootcfg(sc, NULL, 0);
11716 		goto done;
11717 	}
11718 
11719 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
11720 
11721 	rc = copyin(bc->data, bc_data, bc->len);
11722 	if (rc == 0)
11723 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
11724 
11725 	free(bc_data, M_CXGBE);
11726 done:
11727 	end_synchronized_op(sc, 0);
11728 	return (rc);
11729 }
11730 
11731 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)11732 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
11733 {
11734 	int rc;
11735 	struct cudbg_init *cudbg;
11736 	void *handle, *buf;
11737 
11738 	/* buf is large, don't block if no memory is available */
11739 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
11740 	if (buf == NULL)
11741 		return (ENOMEM);
11742 
11743 	handle = cudbg_alloc_handle();
11744 	if (handle == NULL) {
11745 		rc = ENOMEM;
11746 		goto done;
11747 	}
11748 
11749 	cudbg = cudbg_get_init(handle);
11750 	cudbg->adap = sc;
11751 	cudbg->print = (cudbg_print_cb)printf;
11752 
11753 #ifndef notyet
11754 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
11755 	    __func__, dump->wr_flash, dump->len, dump->data);
11756 #endif
11757 
11758 	if (dump->wr_flash)
11759 		cudbg->use_flash = 1;
11760 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
11761 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
11762 
11763 	rc = cudbg_collect(handle, buf, &dump->len);
11764 	if (rc != 0)
11765 		goto done;
11766 
11767 	rc = copyout(buf, dump->data, dump->len);
11768 done:
11769 	cudbg_free_handle(handle);
11770 	free(buf, M_CXGBE);
11771 	return (rc);
11772 }
11773 
11774 static void
free_offload_policy(struct t4_offload_policy * op)11775 free_offload_policy(struct t4_offload_policy *op)
11776 {
11777 	struct offload_rule *r;
11778 	int i;
11779 
11780 	if (op == NULL)
11781 		return;
11782 
11783 	r = &op->rule[0];
11784 	for (i = 0; i < op->nrules; i++, r++) {
11785 		free(r->bpf_prog.bf_insns, M_CXGBE);
11786 	}
11787 	free(op->rule, M_CXGBE);
11788 	free(op, M_CXGBE);
11789 }
11790 
11791 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)11792 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
11793 {
11794 	int i, rc, len;
11795 	struct t4_offload_policy *op, *old;
11796 	struct bpf_program *bf;
11797 	const struct offload_settings *s;
11798 	struct offload_rule *r;
11799 	void *u;
11800 
11801 	if (!is_offload(sc))
11802 		return (ENODEV);
11803 
11804 	if (uop->nrules == 0) {
11805 		/* Delete installed policies. */
11806 		op = NULL;
11807 		goto set_policy;
11808 	} else if (uop->nrules > 256) { /* arbitrary */
11809 		return (E2BIG);
11810 	}
11811 
11812 	/* Copy userspace offload policy to kernel */
11813 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
11814 	op->nrules = uop->nrules;
11815 	len = op->nrules * sizeof(struct offload_rule);
11816 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11817 	rc = copyin(uop->rule, op->rule, len);
11818 	if (rc) {
11819 		free(op->rule, M_CXGBE);
11820 		free(op, M_CXGBE);
11821 		return (rc);
11822 	}
11823 
11824 	r = &op->rule[0];
11825 	for (i = 0; i < op->nrules; i++, r++) {
11826 
11827 		/* Validate open_type */
11828 		if (r->open_type != OPEN_TYPE_LISTEN &&
11829 		    r->open_type != OPEN_TYPE_ACTIVE &&
11830 		    r->open_type != OPEN_TYPE_PASSIVE &&
11831 		    r->open_type != OPEN_TYPE_DONTCARE) {
11832 error:
11833 			/*
11834 			 * Rules 0 to i have malloc'd filters that need to be
11835 			 * freed.  Rules i+1 to nrules have userspace pointers
11836 			 * and should be left alone.
11837 			 */
11838 			op->nrules = i;
11839 			free_offload_policy(op);
11840 			return (rc);
11841 		}
11842 
11843 		/* Validate settings */
11844 		s = &r->settings;
11845 		if ((s->offload != 0 && s->offload != 1) ||
11846 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
11847 		    s->sched_class < -1 ||
11848 		    s->sched_class >= sc->params.nsched_cls) {
11849 			rc = EINVAL;
11850 			goto error;
11851 		}
11852 
11853 		bf = &r->bpf_prog;
11854 		u = bf->bf_insns;	/* userspace ptr */
11855 		bf->bf_insns = NULL;
11856 		if (bf->bf_len == 0) {
11857 			/* legal, matches everything */
11858 			continue;
11859 		}
11860 		len = bf->bf_len * sizeof(*bf->bf_insns);
11861 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11862 		rc = copyin(u, bf->bf_insns, len);
11863 		if (rc != 0)
11864 			goto error;
11865 
11866 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
11867 			rc = EINVAL;
11868 			goto error;
11869 		}
11870 	}
11871 set_policy:
11872 	rw_wlock(&sc->policy_lock);
11873 	old = sc->policy;
11874 	sc->policy = op;
11875 	rw_wunlock(&sc->policy_lock);
11876 	free_offload_policy(old);
11877 
11878 	return (0);
11879 }
11880 
11881 #define MAX_READ_BUF_SIZE (128 * 1024)
11882 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)11883 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
11884 {
11885 	uint32_t addr, remaining, n;
11886 	uint32_t *buf;
11887 	int rc;
11888 	uint8_t *dst;
11889 
11890 	mtx_lock(&sc->reg_lock);
11891 	if (hw_off_limits(sc))
11892 		rc = ENXIO;
11893 	else
11894 		rc = validate_mem_range(sc, mr->addr, mr->len);
11895 	mtx_unlock(&sc->reg_lock);
11896 	if (rc != 0)
11897 		return (rc);
11898 
11899 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
11900 	addr = mr->addr;
11901 	remaining = mr->len;
11902 	dst = (void *)mr->data;
11903 
11904 	while (remaining) {
11905 		n = min(remaining, MAX_READ_BUF_SIZE);
11906 		mtx_lock(&sc->reg_lock);
11907 		if (hw_off_limits(sc))
11908 			rc = ENXIO;
11909 		else
11910 			read_via_memwin(sc, 2, addr, buf, n);
11911 		mtx_unlock(&sc->reg_lock);
11912 		if (rc != 0)
11913 			break;
11914 
11915 		rc = copyout(buf, dst, n);
11916 		if (rc != 0)
11917 			break;
11918 
11919 		dst += n;
11920 		remaining -= n;
11921 		addr += n;
11922 	}
11923 
11924 	free(buf, M_CXGBE);
11925 	return (rc);
11926 }
11927 #undef MAX_READ_BUF_SIZE
11928 
11929 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)11930 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
11931 {
11932 	int rc;
11933 
11934 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
11935 		return (EINVAL);
11936 
11937 	if (i2cd->len > sizeof(i2cd->data))
11938 		return (EFBIG);
11939 
11940 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
11941 	if (rc)
11942 		return (rc);
11943 	if (hw_off_limits(sc))
11944 		rc = ENXIO;
11945 	else
11946 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
11947 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
11948 	end_synchronized_op(sc, 0);
11949 
11950 	return (rc);
11951 }
11952 
11953 static int
clear_stats(struct adapter * sc,u_int port_id)11954 clear_stats(struct adapter *sc, u_int port_id)
11955 {
11956 	int i, v, chan_map;
11957 	struct port_info *pi;
11958 	struct vi_info *vi;
11959 	struct sge_rxq *rxq;
11960 	struct sge_txq *txq;
11961 	struct sge_wrq *wrq;
11962 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
11963 	struct sge_ofld_txq *ofld_txq;
11964 #endif
11965 #ifdef TCP_OFFLOAD
11966 	struct sge_ofld_rxq *ofld_rxq;
11967 #endif
11968 
11969 	if (port_id >= sc->params.nports)
11970 		return (EINVAL);
11971 	pi = sc->port[port_id];
11972 	if (pi == NULL)
11973 		return (EIO);
11974 
11975 	mtx_lock(&sc->reg_lock);
11976 	if (!hw_off_limits(sc)) {
11977 		/* MAC stats */
11978 		t4_clr_port_stats(sc, pi->tx_chan);
11979 		if (is_t6(sc)) {
11980 			if (pi->fcs_reg != -1)
11981 				pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
11982 			else
11983 				pi->stats.rx_fcs_err = 0;
11984 		}
11985 		for_each_vi(pi, v, vi) {
11986 			if (vi->flags & VI_INIT_DONE)
11987 				t4_clr_vi_stats(sc, vi->vin);
11988 		}
11989 		chan_map = pi->rx_e_chan_map;
11990 		v = 0;	/* reuse */
11991 		while (chan_map) {
11992 			i = ffs(chan_map) - 1;
11993 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
11994 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
11995 			chan_map &= ~(1 << i);
11996 		}
11997 	}
11998 	mtx_unlock(&sc->reg_lock);
11999 	pi->tx_parse_error = 0;
12000 	pi->tnl_cong_drops = 0;
12001 
12002 	/*
12003 	 * Since this command accepts a port, clear stats for
12004 	 * all VIs on this port.
12005 	 */
12006 	for_each_vi(pi, v, vi) {
12007 		if (vi->flags & VI_INIT_DONE) {
12008 
12009 			for_each_rxq(vi, i, rxq) {
12010 #if defined(INET) || defined(INET6)
12011 				rxq->lro.lro_queued = 0;
12012 				rxq->lro.lro_flushed = 0;
12013 #endif
12014 				rxq->rxcsum = 0;
12015 				rxq->vlan_extraction = 0;
12016 				rxq->vxlan_rxcsum = 0;
12017 
12018 				rxq->fl.cl_allocated = 0;
12019 				rxq->fl.cl_recycled = 0;
12020 				rxq->fl.cl_fast_recycled = 0;
12021 			}
12022 
12023 			for_each_txq(vi, i, txq) {
12024 				txq->txcsum = 0;
12025 				txq->tso_wrs = 0;
12026 				txq->vlan_insertion = 0;
12027 				txq->imm_wrs = 0;
12028 				txq->sgl_wrs = 0;
12029 				txq->txpkt_wrs = 0;
12030 				txq->txpkts0_wrs = 0;
12031 				txq->txpkts1_wrs = 0;
12032 				txq->txpkts0_pkts = 0;
12033 				txq->txpkts1_pkts = 0;
12034 				txq->txpkts_flush = 0;
12035 				txq->raw_wrs = 0;
12036 				txq->vxlan_tso_wrs = 0;
12037 				txq->vxlan_txcsum = 0;
12038 				txq->kern_tls_records = 0;
12039 				txq->kern_tls_short = 0;
12040 				txq->kern_tls_partial = 0;
12041 				txq->kern_tls_full = 0;
12042 				txq->kern_tls_octets = 0;
12043 				txq->kern_tls_waste = 0;
12044 				txq->kern_tls_options = 0;
12045 				txq->kern_tls_header = 0;
12046 				txq->kern_tls_fin = 0;
12047 				txq->kern_tls_fin_short = 0;
12048 				txq->kern_tls_cbc = 0;
12049 				txq->kern_tls_gcm = 0;
12050 				mp_ring_reset_stats(txq->r);
12051 			}
12052 
12053 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12054 			for_each_ofld_txq(vi, i, ofld_txq) {
12055 				ofld_txq->wrq.tx_wrs_direct = 0;
12056 				ofld_txq->wrq.tx_wrs_copied = 0;
12057 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12058 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12059 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12060 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
12061 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
12062 			}
12063 #endif
12064 #ifdef TCP_OFFLOAD
12065 			for_each_ofld_rxq(vi, i, ofld_rxq) {
12066 				ofld_rxq->fl.cl_allocated = 0;
12067 				ofld_rxq->fl.cl_recycled = 0;
12068 				ofld_rxq->fl.cl_fast_recycled = 0;
12069 				counter_u64_zero(
12070 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
12071 				counter_u64_zero(
12072 				    ofld_rxq->rx_iscsi_ddp_setup_error);
12073 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
12074 				ofld_rxq->rx_iscsi_ddp_octets = 0;
12075 				ofld_rxq->rx_iscsi_fl_pdus = 0;
12076 				ofld_rxq->rx_iscsi_fl_octets = 0;
12077 				ofld_rxq->rx_toe_tls_records = 0;
12078 				ofld_rxq->rx_toe_tls_octets = 0;
12079 			}
12080 #endif
12081 
12082 			if (IS_MAIN_VI(vi)) {
12083 				wrq = &sc->sge.ctrlq[pi->port_id];
12084 				wrq->tx_wrs_direct = 0;
12085 				wrq->tx_wrs_copied = 0;
12086 			}
12087 		}
12088 	}
12089 
12090 	return (0);
12091 }
12092 
12093 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12094 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12095 {
12096 #ifdef INET6
12097 	struct in6_addr in6;
12098 
12099 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12100 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
12101 		return (0);
12102 	else
12103 		return (EIO);
12104 #else
12105 	return (ENOTSUP);
12106 #endif
12107 }
12108 
12109 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12110 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12111 {
12112 #ifdef INET6
12113 	struct in6_addr in6;
12114 
12115 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12116 	return (t4_release_clip_addr(sc, &in6));
12117 #else
12118 	return (ENOTSUP);
12119 #endif
12120 }
12121 
12122 int
t4_os_find_pci_capability(struct adapter * sc,int cap)12123 t4_os_find_pci_capability(struct adapter *sc, int cap)
12124 {
12125 	int i;
12126 
12127 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
12128 }
12129 
12130 int
t4_os_pci_save_state(struct adapter * sc)12131 t4_os_pci_save_state(struct adapter *sc)
12132 {
12133 	device_t dev;
12134 	struct pci_devinfo *dinfo;
12135 
12136 	dev = sc->dev;
12137 	dinfo = device_get_ivars(dev);
12138 
12139 	pci_cfg_save(dev, dinfo, 0);
12140 	return (0);
12141 }
12142 
12143 int
t4_os_pci_restore_state(struct adapter * sc)12144 t4_os_pci_restore_state(struct adapter *sc)
12145 {
12146 	device_t dev;
12147 	struct pci_devinfo *dinfo;
12148 
12149 	dev = sc->dev;
12150 	dinfo = device_get_ivars(dev);
12151 
12152 	pci_cfg_restore(dev, dinfo);
12153 	return (0);
12154 }
12155 
12156 void
t4_os_portmod_changed(struct port_info * pi)12157 t4_os_portmod_changed(struct port_info *pi)
12158 {
12159 	struct adapter *sc = pi->adapter;
12160 	struct vi_info *vi;
12161 	struct ifnet *ifp;
12162 	static const char *mod_str[] = {
12163 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
12164 	};
12165 
12166 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
12167 	    ("%s: port_type %u", __func__, pi->port_type));
12168 
12169 	vi = &pi->vi[0];
12170 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
12171 		PORT_LOCK(pi);
12172 		build_medialist(pi);
12173 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
12174 			fixup_link_config(pi);
12175 			apply_link_config(pi);
12176 		}
12177 		PORT_UNLOCK(pi);
12178 		end_synchronized_op(sc, LOCK_HELD);
12179 	}
12180 
12181 	ifp = vi->ifp;
12182 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
12183 		if_printf(ifp, "transceiver unplugged.\n");
12184 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
12185 		if_printf(ifp, "unknown transceiver inserted.\n");
12186 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
12187 		if_printf(ifp, "unsupported transceiver inserted.\n");
12188 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
12189 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
12190 		    port_top_speed(pi), mod_str[pi->mod_type]);
12191 	} else {
12192 		if_printf(ifp, "transceiver (type %d) inserted.\n",
12193 		    pi->mod_type);
12194 	}
12195 }
12196 
12197 void
t4_os_link_changed(struct port_info * pi)12198 t4_os_link_changed(struct port_info *pi)
12199 {
12200 	struct vi_info *vi;
12201 	struct ifnet *ifp;
12202 	struct link_config *lc = &pi->link_cfg;
12203 	struct adapter *sc = pi->adapter;
12204 	int v;
12205 
12206 	PORT_LOCK_ASSERT_OWNED(pi);
12207 
12208 	if (is_t6(sc)) {
12209 		if (lc->link_ok) {
12210 			if (lc->speed > 25000 ||
12211 			    (lc->speed == 25000 && lc->fec == FEC_RS)) {
12212 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12213 				    A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS);
12214 			} else {
12215 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12216 				    A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS);
12217 			}
12218 			pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12219 			pi->stats.rx_fcs_err = 0;
12220 		} else {
12221 			pi->fcs_reg = -1;
12222 		}
12223 	} else {
12224 		MPASS(pi->fcs_reg != -1);
12225 		MPASS(pi->fcs_base == 0);
12226 	}
12227 
12228 	for_each_vi(pi, v, vi) {
12229 		ifp = vi->ifp;
12230 		if (ifp == NULL)
12231 			continue;
12232 
12233 		if (lc->link_ok) {
12234 			ifp->if_baudrate = IF_Mbps(lc->speed);
12235 			if_link_state_change(ifp, LINK_STATE_UP);
12236 		} else {
12237 			if_link_state_change(ifp, LINK_STATE_DOWN);
12238 		}
12239 	}
12240 }
12241 
12242 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)12243 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
12244 {
12245 	struct adapter *sc;
12246 
12247 	sx_slock(&t4_list_lock);
12248 	SLIST_FOREACH(sc, &t4_list, link) {
12249 		/*
12250 		 * func should not make any assumptions about what state sc is
12251 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
12252 		 */
12253 		func(sc, arg);
12254 	}
12255 	sx_sunlock(&t4_list_lock);
12256 }
12257 
12258 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)12259 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
12260     struct thread *td)
12261 {
12262 	int rc;
12263 	struct adapter *sc = dev->si_drv1;
12264 
12265 	rc = priv_check(td, PRIV_DRIVER);
12266 	if (rc != 0)
12267 		return (rc);
12268 
12269 	switch (cmd) {
12270 	case CHELSIO_T4_GETREG: {
12271 		struct t4_reg *edata = (struct t4_reg *)data;
12272 
12273 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12274 			return (EFAULT);
12275 
12276 		mtx_lock(&sc->reg_lock);
12277 		if (hw_off_limits(sc))
12278 			rc = ENXIO;
12279 		else if (edata->size == 4)
12280 			edata->val = t4_read_reg(sc, edata->addr);
12281 		else if (edata->size == 8)
12282 			edata->val = t4_read_reg64(sc, edata->addr);
12283 		else
12284 			rc = EINVAL;
12285 		mtx_unlock(&sc->reg_lock);
12286 
12287 		break;
12288 	}
12289 	case CHELSIO_T4_SETREG: {
12290 		struct t4_reg *edata = (struct t4_reg *)data;
12291 
12292 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12293 			return (EFAULT);
12294 
12295 		mtx_lock(&sc->reg_lock);
12296 		if (hw_off_limits(sc))
12297 			rc = ENXIO;
12298 		else if (edata->size == 4) {
12299 			if (edata->val & 0xffffffff00000000)
12300 				rc = EINVAL;
12301 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
12302 		} else if (edata->size == 8)
12303 			t4_write_reg64(sc, edata->addr, edata->val);
12304 		else
12305 			rc = EINVAL;
12306 		mtx_unlock(&sc->reg_lock);
12307 
12308 		break;
12309 	}
12310 	case CHELSIO_T4_REGDUMP: {
12311 		struct t4_regdump *regs = (struct t4_regdump *)data;
12312 		int reglen = t4_get_regs_len(sc);
12313 		uint8_t *buf;
12314 
12315 		if (regs->len < reglen) {
12316 			regs->len = reglen; /* hint to the caller */
12317 			return (ENOBUFS);
12318 		}
12319 
12320 		regs->len = reglen;
12321 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
12322 		mtx_lock(&sc->reg_lock);
12323 		if (hw_off_limits(sc))
12324 			rc = ENXIO;
12325 		else
12326 			get_regs(sc, regs, buf);
12327 		mtx_unlock(&sc->reg_lock);
12328 		if (rc == 0)
12329 			rc = copyout(buf, regs->data, reglen);
12330 		free(buf, M_CXGBE);
12331 		break;
12332 	}
12333 	case CHELSIO_T4_GET_FILTER_MODE:
12334 		rc = get_filter_mode(sc, (uint32_t *)data);
12335 		break;
12336 	case CHELSIO_T4_SET_FILTER_MODE:
12337 		rc = set_filter_mode(sc, *(uint32_t *)data);
12338 		break;
12339 	case CHELSIO_T4_SET_FILTER_MASK:
12340 		rc = set_filter_mask(sc, *(uint32_t *)data);
12341 		break;
12342 	case CHELSIO_T4_GET_FILTER:
12343 		rc = get_filter(sc, (struct t4_filter *)data);
12344 		break;
12345 	case CHELSIO_T4_SET_FILTER:
12346 		rc = set_filter(sc, (struct t4_filter *)data);
12347 		break;
12348 	case CHELSIO_T4_DEL_FILTER:
12349 		rc = del_filter(sc, (struct t4_filter *)data);
12350 		break;
12351 	case CHELSIO_T4_GET_SGE_CONTEXT:
12352 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
12353 		break;
12354 	case CHELSIO_T4_LOAD_FW:
12355 		rc = load_fw(sc, (struct t4_data *)data);
12356 		break;
12357 	case CHELSIO_T4_GET_MEM:
12358 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
12359 		break;
12360 	case CHELSIO_T4_GET_I2C:
12361 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
12362 		break;
12363 	case CHELSIO_T4_CLEAR_STATS:
12364 		rc = clear_stats(sc, *(uint32_t *)data);
12365 		break;
12366 	case CHELSIO_T4_SCHED_CLASS:
12367 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
12368 		break;
12369 	case CHELSIO_T4_SCHED_QUEUE:
12370 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
12371 		break;
12372 	case CHELSIO_T4_GET_TRACER:
12373 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
12374 		break;
12375 	case CHELSIO_T4_SET_TRACER:
12376 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
12377 		break;
12378 	case CHELSIO_T4_LOAD_CFG:
12379 		rc = load_cfg(sc, (struct t4_data *)data);
12380 		break;
12381 	case CHELSIO_T4_LOAD_BOOT:
12382 		rc = load_boot(sc, (struct t4_bootrom *)data);
12383 		break;
12384 	case CHELSIO_T4_LOAD_BOOTCFG:
12385 		rc = load_bootcfg(sc, (struct t4_data *)data);
12386 		break;
12387 	case CHELSIO_T4_CUDBG_DUMP:
12388 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
12389 		break;
12390 	case CHELSIO_T4_SET_OFLD_POLICY:
12391 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
12392 		break;
12393 	case CHELSIO_T4_HOLD_CLIP_ADDR:
12394 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
12395 		break;
12396 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
12397 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
12398 		break;
12399 	default:
12400 		rc = ENOTTY;
12401 	}
12402 
12403 	return (rc);
12404 }
12405 
12406 #ifdef TCP_OFFLOAD
12407 static int
toe_capability(struct vi_info * vi,bool enable)12408 toe_capability(struct vi_info *vi, bool enable)
12409 {
12410 	int rc;
12411 	struct port_info *pi = vi->pi;
12412 	struct adapter *sc = pi->adapter;
12413 
12414 	ASSERT_SYNCHRONIZED_OP(sc);
12415 
12416 	if (!is_offload(sc))
12417 		return (ENODEV);
12418 	if (hw_off_limits(sc))
12419 		return (ENXIO);
12420 
12421 	if (enable) {
12422 #ifdef KERN_TLS
12423 		if (sc->flags & KERN_TLS_ON) {
12424 			int i, j, n;
12425 			struct port_info *p;
12426 			struct vi_info *v;
12427 
12428 			/*
12429 			 * Reconfigure hardware for TOE if TXTLS is not enabled
12430 			 * on any ifnet.
12431 			 */
12432 			n = 0;
12433 			for_each_port(sc, i) {
12434 				p = sc->port[i];
12435 				for_each_vi(p, j, v) {
12436 					if (v->ifp->if_capenable & IFCAP_TXTLS) {
12437 						CH_WARN(sc,
12438 						    "%s has NIC TLS enabled.\n",
12439 						    device_get_nameunit(v->dev));
12440 						n++;
12441 					}
12442 				}
12443 			}
12444 			if (n > 0) {
12445 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
12446 				    "associated with this adapter before "
12447 				    "trying to enable TOE.\n");
12448 				return (EAGAIN);
12449 			}
12450 			rc = t4_config_kern_tls(sc, false);
12451 			if (rc)
12452 				return (rc);
12453 		}
12454 #endif
12455 		if ((vi->ifp->if_capenable & IFCAP_TOE) != 0) {
12456 			/* TOE is already enabled. */
12457 			return (0);
12458 		}
12459 
12460 		/*
12461 		 * We need the port's queues around so that we're able to send
12462 		 * and receive CPLs to/from the TOE even if the ifnet for this
12463 		 * port has never been UP'd administratively.
12464 		 */
12465 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
12466 			return (rc);
12467 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
12468 		    ((rc = vi_init(&pi->vi[0])) != 0))
12469 			return (rc);
12470 
12471 		if (isset(&sc->offload_map, pi->port_id)) {
12472 			/* TOE is enabled on another VI of this port. */
12473 			pi->uld_vis++;
12474 			return (0);
12475 		}
12476 
12477 		if (!uld_active(sc, ULD_TOM)) {
12478 			rc = t4_activate_uld(sc, ULD_TOM);
12479 			if (rc == EAGAIN) {
12480 				log(LOG_WARNING,
12481 				    "You must kldload t4_tom.ko before trying "
12482 				    "to enable TOE on a cxgbe interface.\n");
12483 			}
12484 			if (rc != 0)
12485 				return (rc);
12486 			KASSERT(sc->tom_softc != NULL,
12487 			    ("%s: TOM activated but softc NULL", __func__));
12488 			KASSERT(uld_active(sc, ULD_TOM),
12489 			    ("%s: TOM activated but flag not set", __func__));
12490 		}
12491 
12492 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
12493 		if (!uld_active(sc, ULD_IWARP))
12494 			(void) t4_activate_uld(sc, ULD_IWARP);
12495 		if (!uld_active(sc, ULD_ISCSI))
12496 			(void) t4_activate_uld(sc, ULD_ISCSI);
12497 
12498 		pi->uld_vis++;
12499 		setbit(&sc->offload_map, pi->port_id);
12500 	} else {
12501 		pi->uld_vis--;
12502 
12503 		if (!isset(&sc->offload_map, pi->port_id) || pi->uld_vis > 0)
12504 			return (0);
12505 
12506 		KASSERT(uld_active(sc, ULD_TOM),
12507 		    ("%s: TOM never initialized?", __func__));
12508 		clrbit(&sc->offload_map, pi->port_id);
12509 	}
12510 
12511 	return (0);
12512 }
12513 
12514 /*
12515  * Add an upper layer driver to the global list.
12516  */
12517 int
t4_register_uld(struct uld_info * ui)12518 t4_register_uld(struct uld_info *ui)
12519 {
12520 	int rc = 0;
12521 	struct uld_info *u;
12522 
12523 	sx_xlock(&t4_uld_list_lock);
12524 	SLIST_FOREACH(u, &t4_uld_list, link) {
12525 	    if (u->uld_id == ui->uld_id) {
12526 		    rc = EEXIST;
12527 		    goto done;
12528 	    }
12529 	}
12530 
12531 	SLIST_INSERT_HEAD(&t4_uld_list, ui, link);
12532 	ui->refcount = 0;
12533 done:
12534 	sx_xunlock(&t4_uld_list_lock);
12535 	return (rc);
12536 }
12537 
12538 int
t4_unregister_uld(struct uld_info * ui)12539 t4_unregister_uld(struct uld_info *ui)
12540 {
12541 	int rc = EINVAL;
12542 	struct uld_info *u;
12543 
12544 	sx_xlock(&t4_uld_list_lock);
12545 
12546 	SLIST_FOREACH(u, &t4_uld_list, link) {
12547 	    if (u == ui) {
12548 		    if (ui->refcount > 0) {
12549 			    rc = EBUSY;
12550 			    goto done;
12551 		    }
12552 
12553 		    SLIST_REMOVE(&t4_uld_list, ui, uld_info, link);
12554 		    rc = 0;
12555 		    goto done;
12556 	    }
12557 	}
12558 done:
12559 	sx_xunlock(&t4_uld_list_lock);
12560 	return (rc);
12561 }
12562 
12563 int
t4_activate_uld(struct adapter * sc,int id)12564 t4_activate_uld(struct adapter *sc, int id)
12565 {
12566 	int rc;
12567 	struct uld_info *ui;
12568 
12569 	ASSERT_SYNCHRONIZED_OP(sc);
12570 
12571 	if (id < 0 || id > ULD_MAX)
12572 		return (EINVAL);
12573 	rc = EAGAIN;	/* kldoad the module with this ULD and try again. */
12574 
12575 	sx_slock(&t4_uld_list_lock);
12576 
12577 	SLIST_FOREACH(ui, &t4_uld_list, link) {
12578 		if (ui->uld_id == id) {
12579 			if (!(sc->flags & FULL_INIT_DONE)) {
12580 				rc = adapter_init(sc);
12581 				if (rc != 0)
12582 					break;
12583 			}
12584 
12585 			rc = ui->activate(sc);
12586 			if (rc == 0) {
12587 				setbit(&sc->active_ulds, id);
12588 				ui->refcount++;
12589 			}
12590 			break;
12591 		}
12592 	}
12593 
12594 	sx_sunlock(&t4_uld_list_lock);
12595 
12596 	return (rc);
12597 }
12598 
12599 int
t4_deactivate_uld(struct adapter * sc,int id)12600 t4_deactivate_uld(struct adapter *sc, int id)
12601 {
12602 	int rc;
12603 	struct uld_info *ui;
12604 
12605 	ASSERT_SYNCHRONIZED_OP(sc);
12606 
12607 	if (id < 0 || id > ULD_MAX)
12608 		return (EINVAL);
12609 	rc = ENXIO;
12610 
12611 	sx_slock(&t4_uld_list_lock);
12612 
12613 	SLIST_FOREACH(ui, &t4_uld_list, link) {
12614 		if (ui->uld_id == id) {
12615 			rc = ui->deactivate(sc);
12616 			if (rc == 0) {
12617 				clrbit(&sc->active_ulds, id);
12618 				ui->refcount--;
12619 			}
12620 			break;
12621 		}
12622 	}
12623 
12624 	sx_sunlock(&t4_uld_list_lock);
12625 
12626 	return (rc);
12627 }
12628 
12629 static void
t4_async_event(struct adapter * sc)12630 t4_async_event(struct adapter *sc)
12631 {
12632 	struct uld_info *ui;
12633 
12634 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4async") != 0)
12635 		return;
12636 	sx_slock(&t4_uld_list_lock);
12637 	SLIST_FOREACH(ui, &t4_uld_list, link) {
12638 		if (ui->uld_id == ULD_IWARP) {
12639 			ui->async_event(sc);
12640 			break;
12641 		}
12642 	}
12643 	sx_sunlock(&t4_uld_list_lock);
12644 	end_synchronized_op(sc, 0);
12645 }
12646 
12647 int
uld_active(struct adapter * sc,int uld_id)12648 uld_active(struct adapter *sc, int uld_id)
12649 {
12650 
12651 	MPASS(uld_id >= 0 && uld_id <= ULD_MAX);
12652 
12653 	return (isset(&sc->active_ulds, uld_id));
12654 }
12655 #endif
12656 
12657 #ifdef KERN_TLS
12658 static int
ktls_capability(struct adapter * sc,bool enable)12659 ktls_capability(struct adapter *sc, bool enable)
12660 {
12661 	ASSERT_SYNCHRONIZED_OP(sc);
12662 
12663 	if (!is_ktls(sc))
12664 		return (ENODEV);
12665 	if (hw_off_limits(sc))
12666 		return (ENXIO);
12667 
12668 	if (enable) {
12669 		if (sc->flags & KERN_TLS_ON)
12670 			return (0);	/* already on */
12671 		if (sc->offload_map != 0) {
12672 			CH_WARN(sc,
12673 			    "Disable TOE on all interfaces associated with "
12674 			    "this adapter before trying to enable NIC TLS.\n");
12675 			return (EAGAIN);
12676 		}
12677 		return (t4_config_kern_tls(sc, true));
12678 	} else {
12679 		/*
12680 		 * Nothing to do for disable.  If TOE is enabled sometime later
12681 		 * then toe_capability will reconfigure the hardware.
12682 		 */
12683 		return (0);
12684 	}
12685 }
12686 #endif
12687 
12688 /*
12689  * t  = ptr to tunable.
12690  * nc = number of CPUs.
12691  * c  = compiled in default for that tunable.
12692  */
12693 static void
calculate_nqueues(int * t,int nc,const int c)12694 calculate_nqueues(int *t, int nc, const int c)
12695 {
12696 	int nq;
12697 
12698 	if (*t > 0)
12699 		return;
12700 	nq = *t < 0 ? -*t : c;
12701 	*t = min(nc, nq);
12702 }
12703 
12704 /*
12705  * Come up with reasonable defaults for some of the tunables, provided they're
12706  * not set by the user (in which case we'll use the values as is).
12707  */
12708 static void
tweak_tunables(void)12709 tweak_tunables(void)
12710 {
12711 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
12712 
12713 	if (t4_ntxq < 1) {
12714 #ifdef RSS
12715 		t4_ntxq = rss_getnumbuckets();
12716 #else
12717 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
12718 #endif
12719 	}
12720 
12721 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
12722 
12723 	if (t4_nrxq < 1) {
12724 #ifdef RSS
12725 		t4_nrxq = rss_getnumbuckets();
12726 #else
12727 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
12728 #endif
12729 	}
12730 
12731 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
12732 
12733 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12734 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
12735 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
12736 #endif
12737 #ifdef TCP_OFFLOAD
12738 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
12739 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
12740 #endif
12741 
12742 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
12743 	if (t4_toecaps_allowed == -1)
12744 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
12745 #else
12746 	if (t4_toecaps_allowed == -1)
12747 		t4_toecaps_allowed = 0;
12748 #endif
12749 
12750 #ifdef TCP_OFFLOAD
12751 	if (t4_rdmacaps_allowed == -1) {
12752 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
12753 		    FW_CAPS_CONFIG_RDMA_RDMAC;
12754 	}
12755 
12756 	if (t4_iscsicaps_allowed == -1) {
12757 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
12758 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
12759 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
12760 	}
12761 
12762 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
12763 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
12764 
12765 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
12766 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
12767 
12768 	if (t4_toe_tls_rx_timeout < 0)
12769 		t4_toe_tls_rx_timeout = 0;
12770 #else
12771 	if (t4_rdmacaps_allowed == -1)
12772 		t4_rdmacaps_allowed = 0;
12773 
12774 	if (t4_iscsicaps_allowed == -1)
12775 		t4_iscsicaps_allowed = 0;
12776 #endif
12777 
12778 #ifdef DEV_NETMAP
12779 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
12780 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
12781 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
12782 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
12783 #endif
12784 
12785 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
12786 		t4_tmr_idx = TMR_IDX;
12787 
12788 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
12789 		t4_pktc_idx = PKTC_IDX;
12790 
12791 	if (t4_qsize_txq < 128)
12792 		t4_qsize_txq = 128;
12793 
12794 	if (t4_qsize_rxq < 128)
12795 		t4_qsize_rxq = 128;
12796 	while (t4_qsize_rxq & 7)
12797 		t4_qsize_rxq++;
12798 
12799 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
12800 
12801 	/*
12802 	 * Number of VIs to create per-port.  The first VI is the "main" regular
12803 	 * VI for the port.  The rest are additional virtual interfaces on the
12804 	 * same physical port.  Note that the main VI does not have native
12805 	 * netmap support but the extra VIs do.
12806 	 *
12807 	 * Limit the number of VIs per port to the number of available
12808 	 * MAC addresses per port.
12809 	 */
12810 	if (t4_num_vis < 1)
12811 		t4_num_vis = 1;
12812 	if (t4_num_vis > nitems(vi_mac_funcs)) {
12813 		t4_num_vis = nitems(vi_mac_funcs);
12814 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
12815 	}
12816 
12817 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
12818 		pcie_relaxed_ordering = 1;
12819 #if defined(__i386__) || defined(__amd64__)
12820 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
12821 			pcie_relaxed_ordering = 0;
12822 #endif
12823 	}
12824 }
12825 
12826 #ifdef DDB
12827 static void
t4_dump_tcb(struct adapter * sc,int tid)12828 t4_dump_tcb(struct adapter *sc, int tid)
12829 {
12830 	uint32_t base, i, j, off, pf, reg, save, tcb_addr, win_pos;
12831 
12832 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
12833 	save = t4_read_reg(sc, reg);
12834 	base = sc->memwin[2].mw_base;
12835 
12836 	/* Dump TCB for the tid */
12837 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
12838 	tcb_addr += tid * TCB_SIZE;
12839 
12840 	if (is_t4(sc)) {
12841 		pf = 0;
12842 		win_pos = tcb_addr & ~0xf;	/* start must be 16B aligned */
12843 	} else {
12844 		pf = V_PFNUM(sc->pf);
12845 		win_pos = tcb_addr & ~0x7f;	/* start must be 128B aligned */
12846 	}
12847 	t4_write_reg(sc, reg, win_pos | pf);
12848 	t4_read_reg(sc, reg);
12849 
12850 	off = tcb_addr - win_pos;
12851 	for (i = 0; i < 4; i++) {
12852 		uint32_t buf[8];
12853 		for (j = 0; j < 8; j++, off += 4)
12854 			buf[j] = htonl(t4_read_reg(sc, base + off));
12855 
12856 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
12857 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12858 		    buf[7]);
12859 	}
12860 
12861 	t4_write_reg(sc, reg, save);
12862 	t4_read_reg(sc, reg);
12863 }
12864 
12865 static void
t4_dump_devlog(struct adapter * sc)12866 t4_dump_devlog(struct adapter *sc)
12867 {
12868 	struct devlog_params *dparams = &sc->params.devlog;
12869 	struct fw_devlog_e e;
12870 	int i, first, j, m, nentries, rc;
12871 	uint64_t ftstamp = UINT64_MAX;
12872 
12873 	if (dparams->start == 0) {
12874 		db_printf("devlog params not valid\n");
12875 		return;
12876 	}
12877 
12878 	nentries = dparams->size / sizeof(struct fw_devlog_e);
12879 	m = fwmtype_to_hwmtype(dparams->memtype);
12880 
12881 	/* Find the first entry. */
12882 	first = -1;
12883 	for (i = 0; i < nentries && !db_pager_quit; i++) {
12884 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12885 		    sizeof(e), (void *)&e);
12886 		if (rc != 0)
12887 			break;
12888 
12889 		if (e.timestamp == 0)
12890 			break;
12891 
12892 		e.timestamp = be64toh(e.timestamp);
12893 		if (e.timestamp < ftstamp) {
12894 			ftstamp = e.timestamp;
12895 			first = i;
12896 		}
12897 	}
12898 
12899 	if (first == -1)
12900 		return;
12901 
12902 	i = first;
12903 	do {
12904 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12905 		    sizeof(e), (void *)&e);
12906 		if (rc != 0)
12907 			return;
12908 
12909 		if (e.timestamp == 0)
12910 			return;
12911 
12912 		e.timestamp = be64toh(e.timestamp);
12913 		e.seqno = be32toh(e.seqno);
12914 		for (j = 0; j < 8; j++)
12915 			e.params[j] = be32toh(e.params[j]);
12916 
12917 		db_printf("%10d  %15ju  %8s  %8s  ",
12918 		    e.seqno, e.timestamp,
12919 		    (e.level < nitems(devlog_level_strings) ?
12920 			devlog_level_strings[e.level] : "UNKNOWN"),
12921 		    (e.facility < nitems(devlog_facility_strings) ?
12922 			devlog_facility_strings[e.facility] : "UNKNOWN"));
12923 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
12924 		    e.params[3], e.params[4], e.params[5], e.params[6],
12925 		    e.params[7]);
12926 
12927 		if (++i == nentries)
12928 			i = 0;
12929 	} while (i != first && !db_pager_quit);
12930 }
12931 
12932 static struct command_table db_t4_table = LIST_HEAD_INITIALIZER(db_t4_table);
12933 _DB_SET(_show, t4, NULL, db_show_table, 0, &db_t4_table);
12934 
DB_FUNC(devlog,db_show_devlog,db_t4_table,CS_OWN,NULL)12935 DB_FUNC(devlog, db_show_devlog, db_t4_table, CS_OWN, NULL)
12936 {
12937 	device_t dev;
12938 	int t;
12939 	bool valid;
12940 
12941 	valid = false;
12942 	t = db_read_token();
12943 	if (t == tIDENT) {
12944 		dev = device_lookup_by_name(db_tok_string);
12945 		valid = true;
12946 	}
12947 	db_skip_to_eol();
12948 	if (!valid) {
12949 		db_printf("usage: show t4 devlog <nexus>\n");
12950 		return;
12951 	}
12952 
12953 	if (dev == NULL) {
12954 		db_printf("device not found\n");
12955 		return;
12956 	}
12957 
12958 	t4_dump_devlog(device_get_softc(dev));
12959 }
12960 
DB_FUNC(tcb,db_show_t4tcb,db_t4_table,CS_OWN,NULL)12961 DB_FUNC(tcb, db_show_t4tcb, db_t4_table, CS_OWN, NULL)
12962 {
12963 	device_t dev;
12964 	int radix, tid, t;
12965 	bool valid;
12966 
12967 	valid = false;
12968 	radix = db_radix;
12969 	db_radix = 10;
12970 	t = db_read_token();
12971 	if (t == tIDENT) {
12972 		dev = device_lookup_by_name(db_tok_string);
12973 		t = db_read_token();
12974 		if (t == tNUMBER) {
12975 			tid = db_tok_number;
12976 			valid = true;
12977 		}
12978 	}
12979 	db_radix = radix;
12980 	db_skip_to_eol();
12981 	if (!valid) {
12982 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
12983 		return;
12984 	}
12985 
12986 	if (dev == NULL) {
12987 		db_printf("device not found\n");
12988 		return;
12989 	}
12990 	if (tid < 0) {
12991 		db_printf("invalid tid\n");
12992 		return;
12993 	}
12994 
12995 	t4_dump_tcb(device_get_softc(dev), tid);
12996 }
12997 #endif
12998 
12999 static eventhandler_tag vxlan_start_evtag;
13000 static eventhandler_tag vxlan_stop_evtag;
13001 
13002 struct vxlan_evargs {
13003 	struct ifnet *ifp;
13004 	uint16_t port;
13005 };
13006 
13007 static void
enable_vxlan_rx(struct adapter * sc)13008 enable_vxlan_rx(struct adapter *sc)
13009 {
13010 	int i, rc;
13011 	struct port_info *pi;
13012 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
13013 
13014 	ASSERT_SYNCHRONIZED_OP(sc);
13015 
13016 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
13017 	    F_VXLAN_EN);
13018 	for_each_port(sc, i) {
13019 		pi = sc->port[i];
13020 		if (pi->vxlan_tcam_entry == true)
13021 			continue;
13022 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
13023 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
13024 		    true);
13025 		if (rc < 0) {
13026 			rc = -rc;
13027 			CH_ERR(&pi->vi[0],
13028 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
13029 		} else {
13030 			MPASS(rc == sc->rawf_base + pi->port_id);
13031 			pi->vxlan_tcam_entry = true;
13032 		}
13033 	}
13034 }
13035 
13036 static void
t4_vxlan_start(struct adapter * sc,void * arg)13037 t4_vxlan_start(struct adapter *sc, void *arg)
13038 {
13039 	struct vxlan_evargs *v = arg;
13040 
13041 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13042 		return;
13043 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
13044 		return;
13045 
13046 	if (sc->vxlan_refcount == 0) {
13047 		sc->vxlan_port = v->port;
13048 		sc->vxlan_refcount = 1;
13049 		if (!hw_off_limits(sc))
13050 			enable_vxlan_rx(sc);
13051 	} else if (sc->vxlan_port == v->port) {
13052 		sc->vxlan_refcount++;
13053 	} else {
13054 		CH_ERR(sc, "VXLAN already configured on port  %d; "
13055 		    "ignoring attempt to configure it on port %d\n",
13056 		    sc->vxlan_port, v->port);
13057 	}
13058 	end_synchronized_op(sc, 0);
13059 }
13060 
13061 static void
t4_vxlan_stop(struct adapter * sc,void * arg)13062 t4_vxlan_stop(struct adapter *sc, void *arg)
13063 {
13064 	struct vxlan_evargs *v = arg;
13065 
13066 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13067 		return;
13068 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
13069 		return;
13070 
13071 	/*
13072 	 * VXLANs may have been configured before the driver was loaded so we
13073 	 * may see more stops than starts.  This is not handled cleanly but at
13074 	 * least we keep the refcount sane.
13075 	 */
13076 	if (sc->vxlan_port != v->port)
13077 		goto done;
13078 	if (sc->vxlan_refcount == 0) {
13079 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
13080 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
13081 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
13082 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
13083 done:
13084 	end_synchronized_op(sc, 0);
13085 }
13086 
13087 static void
t4_vxlan_start_handler(void * arg __unused,struct ifnet * ifp,sa_family_t family,u_int port)13088 t4_vxlan_start_handler(void *arg __unused, struct ifnet *ifp,
13089     sa_family_t family, u_int port)
13090 {
13091 	struct vxlan_evargs v;
13092 
13093 	MPASS(family == AF_INET || family == AF_INET6);
13094 	v.ifp = ifp;
13095 	v.port = port;
13096 
13097 	t4_iterate(t4_vxlan_start, &v);
13098 }
13099 
13100 static void
t4_vxlan_stop_handler(void * arg __unused,struct ifnet * ifp,sa_family_t family,u_int port)13101 t4_vxlan_stop_handler(void *arg __unused, struct ifnet *ifp, sa_family_t family,
13102     u_int port)
13103 {
13104 	struct vxlan_evargs v;
13105 
13106 	MPASS(family == AF_INET || family == AF_INET6);
13107 	v.ifp = ifp;
13108 	v.port = port;
13109 
13110 	t4_iterate(t4_vxlan_stop, &v);
13111 }
13112 
13113 
13114 static struct sx mlu;	/* mod load unload */
13115 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
13116 
13117 static int
mod_event(module_t mod,int cmd,void * arg)13118 mod_event(module_t mod, int cmd, void *arg)
13119 {
13120 	int rc = 0;
13121 	static int loaded = 0;
13122 
13123 	switch (cmd) {
13124 	case MOD_LOAD:
13125 		sx_xlock(&mlu);
13126 		if (loaded++ == 0) {
13127 			t4_sge_modload();
13128 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13129 			    t4_filter_rpl, CPL_COOKIE_FILTER);
13130 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
13131 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
13132 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
13133 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
13134 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13135 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
13136 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
13137 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
13138 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
13139 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
13140 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
13141 			    do_smt_write_rpl);
13142 			sx_init(&t4_list_lock, "T4/T5 adapters");
13143 			SLIST_INIT(&t4_list);
13144 			callout_init(&fatal_callout, 1);
13145 #ifdef TCP_OFFLOAD
13146 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
13147 			SLIST_INIT(&t4_uld_list);
13148 #endif
13149 #ifdef INET6
13150 			t4_clip_modload();
13151 #endif
13152 #ifdef KERN_TLS
13153 			t6_ktls_modload();
13154 #endif
13155 			t4_tracer_modload();
13156 			tweak_tunables();
13157 			vxlan_start_evtag =
13158 			    EVENTHANDLER_REGISTER(vxlan_start,
13159 				t4_vxlan_start_handler, NULL,
13160 				EVENTHANDLER_PRI_ANY);
13161 			vxlan_stop_evtag =
13162 			    EVENTHANDLER_REGISTER(vxlan_stop,
13163 				t4_vxlan_stop_handler, NULL,
13164 				EVENTHANDLER_PRI_ANY);
13165 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
13166 			    taskqueue_thread_enqueue, &reset_tq);
13167 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
13168 			    "t4_rst_thr");
13169 		}
13170 		sx_xunlock(&mlu);
13171 		break;
13172 
13173 	case MOD_UNLOAD:
13174 		sx_xlock(&mlu);
13175 		if (--loaded == 0) {
13176 			int tries;
13177 
13178 			taskqueue_free(reset_tq);
13179 			sx_slock(&t4_list_lock);
13180 			if (!SLIST_EMPTY(&t4_list)) {
13181 				rc = EBUSY;
13182 				sx_sunlock(&t4_list_lock);
13183 				goto done_unload;
13184 			}
13185 #ifdef TCP_OFFLOAD
13186 			sx_slock(&t4_uld_list_lock);
13187 			if (!SLIST_EMPTY(&t4_uld_list)) {
13188 				rc = EBUSY;
13189 				sx_sunlock(&t4_uld_list_lock);
13190 				sx_sunlock(&t4_list_lock);
13191 				goto done_unload;
13192 			}
13193 #endif
13194 			tries = 0;
13195 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
13196 				uprintf("%ju clusters with custom free routine "
13197 				    "still is use.\n", t4_sge_extfree_refs());
13198 				pause("t4unload", 2 * hz);
13199 			}
13200 #ifdef TCP_OFFLOAD
13201 			sx_sunlock(&t4_uld_list_lock);
13202 #endif
13203 			sx_sunlock(&t4_list_lock);
13204 
13205 			if (t4_sge_extfree_refs() == 0) {
13206 				EVENTHANDLER_DEREGISTER(vxlan_start,
13207 				    vxlan_start_evtag);
13208 				EVENTHANDLER_DEREGISTER(vxlan_stop,
13209 				    vxlan_stop_evtag);
13210 				t4_tracer_modunload();
13211 #ifdef KERN_TLS
13212 				t6_ktls_modunload();
13213 #endif
13214 #ifdef INET6
13215 				t4_clip_modunload();
13216 #endif
13217 #ifdef TCP_OFFLOAD
13218 				sx_destroy(&t4_uld_list_lock);
13219 #endif
13220 				sx_destroy(&t4_list_lock);
13221 				t4_sge_modunload();
13222 				loaded = 0;
13223 			} else {
13224 				rc = EBUSY;
13225 				loaded++;	/* undo earlier decrement */
13226 			}
13227 		}
13228 done_unload:
13229 		sx_xunlock(&mlu);
13230 		break;
13231 	}
13232 
13233 	return (rc);
13234 }
13235 
13236 static devclass_t t4_devclass, t5_devclass, t6_devclass;
13237 static devclass_t cxgbe_devclass, cxl_devclass, cc_devclass;
13238 static devclass_t vcxgbe_devclass, vcxl_devclass, vcc_devclass;
13239 
13240 DRIVER_MODULE(t4nex, pci, t4_driver, t4_devclass, mod_event, 0);
13241 MODULE_VERSION(t4nex, 1);
13242 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
13243 #ifdef DEV_NETMAP
13244 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
13245 #endif /* DEV_NETMAP */
13246 
13247 DRIVER_MODULE(t5nex, pci, t5_driver, t5_devclass, mod_event, 0);
13248 MODULE_VERSION(t5nex, 1);
13249 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
13250 #ifdef DEV_NETMAP
13251 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
13252 #endif /* DEV_NETMAP */
13253 
13254 DRIVER_MODULE(t6nex, pci, t6_driver, t6_devclass, mod_event, 0);
13255 MODULE_VERSION(t6nex, 1);
13256 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
13257 #ifdef DEV_NETMAP
13258 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
13259 #endif /* DEV_NETMAP */
13260 
13261 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, cxgbe_devclass, 0, 0);
13262 MODULE_VERSION(cxgbe, 1);
13263 
13264 DRIVER_MODULE(cxl, t5nex, cxl_driver, cxl_devclass, 0, 0);
13265 MODULE_VERSION(cxl, 1);
13266 
13267 DRIVER_MODULE(cc, t6nex, cc_driver, cc_devclass, 0, 0);
13268 MODULE_VERSION(cc, 1);
13269 
13270 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, vcxgbe_devclass, 0, 0);
13271 MODULE_VERSION(vcxgbe, 1);
13272 
13273 DRIVER_MODULE(vcxl, cxl, vcxl_driver, vcxl_devclass, 0, 0);
13274 MODULE_VERSION(vcxl, 1);
13275 
13276 DRIVER_MODULE(vcc, cc, vcc_driver, vcc_devclass, 0, 0);
13277 MODULE_VERSION(vcc, 1);
13278