xref: /freebsd/sys/dev/cxgbe/t4_main.c (revision 52e5a66eac22d24e29f204c5b65232378e71f4cf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_ddb.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_kern_tls.h"
35 #include "opt_ratelimit.h"
36 #include "opt_rss.h"
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/priv.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/eventhandler.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/taskqueue.h>
48 #include <sys/pciio.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pci_private.h>
52 #include <sys/firmware.h>
53 #include <sys/sbuf.h>
54 #include <sys/smp.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <net/if_dl.h>
62 #include <net/if_vlan_var.h>
63 #ifdef RSS
64 #include <net/rss_config.h>
65 #endif
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 #ifdef KERN_TLS
69 #include <netinet/tcp_seq.h>
70 #endif
71 #if defined(__i386__) || defined(__amd64__)
72 #include <machine/md_var.h>
73 #include <machine/cputypes.h>
74 #include <vm/vm.h>
75 #include <vm/pmap.h>
76 #endif
77 #ifdef DDB
78 #include <ddb/ddb.h>
79 #include <ddb/db_lex.h>
80 #endif
81 
82 #include "common/common.h"
83 #include "common/t4_msg.h"
84 #include "common/t4_regs.h"
85 #include "common/t4_regs_values.h"
86 #include "cudbg/cudbg.h"
87 #include "t4_clip.h"
88 #include "t4_ioctl.h"
89 #include "t4_l2t.h"
90 #include "t4_mp_ring.h"
91 #include "t4_if.h"
92 #include "t4_smt.h"
93 
94 /* T4 bus driver interface */
95 static int t4_probe(device_t);
96 static int t4_attach(device_t);
97 static int t4_detach(device_t);
98 static int t4_child_location(device_t, device_t, struct sbuf *);
99 static int t4_ready(device_t);
100 static int t4_read_port_device(device_t, int, device_t *);
101 static int t4_suspend(device_t);
102 static int t4_resume(device_t);
103 static int t4_reset_prepare(device_t, device_t);
104 static int t4_reset_post(device_t, device_t);
105 static device_method_t t4_methods[] = {
106 	DEVMETHOD(device_probe,		t4_probe),
107 	DEVMETHOD(device_attach,	t4_attach),
108 	DEVMETHOD(device_detach,	t4_detach),
109 	DEVMETHOD(device_suspend,	t4_suspend),
110 	DEVMETHOD(device_resume,	t4_resume),
111 
112 	DEVMETHOD(bus_child_location,	t4_child_location),
113 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
114 	DEVMETHOD(bus_reset_post,	t4_reset_post),
115 
116 	DEVMETHOD(t4_is_main_ready,	t4_ready),
117 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
118 
119 	DEVMETHOD_END
120 };
121 static driver_t t4_driver = {
122 	"t4nex",
123 	t4_methods,
124 	sizeof(struct adapter)
125 };
126 
127 
128 /* T4 port (cxgbe) interface */
129 static int cxgbe_probe(device_t);
130 static int cxgbe_attach(device_t);
131 static int cxgbe_detach(device_t);
132 device_method_t cxgbe_methods[] = {
133 	DEVMETHOD(device_probe,		cxgbe_probe),
134 	DEVMETHOD(device_attach,	cxgbe_attach),
135 	DEVMETHOD(device_detach,	cxgbe_detach),
136 	{ 0, 0 }
137 };
138 static driver_t cxgbe_driver = {
139 	"cxgbe",
140 	cxgbe_methods,
141 	sizeof(struct port_info)
142 };
143 
144 /* T4 VI (vcxgbe) interface */
145 static int vcxgbe_probe(device_t);
146 static int vcxgbe_attach(device_t);
147 static int vcxgbe_detach(device_t);
148 static device_method_t vcxgbe_methods[] = {
149 	DEVMETHOD(device_probe,		vcxgbe_probe),
150 	DEVMETHOD(device_attach,	vcxgbe_attach),
151 	DEVMETHOD(device_detach,	vcxgbe_detach),
152 	{ 0, 0 }
153 };
154 static driver_t vcxgbe_driver = {
155 	"vcxgbe",
156 	vcxgbe_methods,
157 	sizeof(struct vi_info)
158 };
159 
160 static d_ioctl_t t4_ioctl;
161 
162 static struct cdevsw t4_cdevsw = {
163        .d_version = D_VERSION,
164        .d_ioctl = t4_ioctl,
165        .d_name = "t4nex",
166 };
167 
168 /* T5 bus driver interface */
169 static int t5_probe(device_t);
170 static device_method_t t5_methods[] = {
171 	DEVMETHOD(device_probe,		t5_probe),
172 	DEVMETHOD(device_attach,	t4_attach),
173 	DEVMETHOD(device_detach,	t4_detach),
174 	DEVMETHOD(device_suspend,	t4_suspend),
175 	DEVMETHOD(device_resume,	t4_resume),
176 
177 	DEVMETHOD(bus_child_location,	t4_child_location),
178 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
179 	DEVMETHOD(bus_reset_post,	t4_reset_post),
180 
181 	DEVMETHOD(t4_is_main_ready,	t4_ready),
182 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
183 
184 	DEVMETHOD_END
185 };
186 static driver_t t5_driver = {
187 	"t5nex",
188 	t5_methods,
189 	sizeof(struct adapter)
190 };
191 
192 
193 /* T5 port (cxl) interface */
194 static driver_t cxl_driver = {
195 	"cxl",
196 	cxgbe_methods,
197 	sizeof(struct port_info)
198 };
199 
200 /* T5 VI (vcxl) interface */
201 static driver_t vcxl_driver = {
202 	"vcxl",
203 	vcxgbe_methods,
204 	sizeof(struct vi_info)
205 };
206 
207 /* T6 bus driver interface */
208 static int t6_probe(device_t);
209 static device_method_t t6_methods[] = {
210 	DEVMETHOD(device_probe,		t6_probe),
211 	DEVMETHOD(device_attach,	t4_attach),
212 	DEVMETHOD(device_detach,	t4_detach),
213 	DEVMETHOD(device_suspend,	t4_suspend),
214 	DEVMETHOD(device_resume,	t4_resume),
215 
216 	DEVMETHOD(bus_child_location,	t4_child_location),
217 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
218 	DEVMETHOD(bus_reset_post,	t4_reset_post),
219 
220 	DEVMETHOD(t4_is_main_ready,	t4_ready),
221 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
222 
223 	DEVMETHOD_END
224 };
225 static driver_t t6_driver = {
226 	"t6nex",
227 	t6_methods,
228 	sizeof(struct adapter)
229 };
230 
231 
232 /* T6 port (cc) interface */
233 static driver_t cc_driver = {
234 	"cc",
235 	cxgbe_methods,
236 	sizeof(struct port_info)
237 };
238 
239 /* T6 VI (vcc) interface */
240 static driver_t vcc_driver = {
241 	"vcc",
242 	vcxgbe_methods,
243 	sizeof(struct vi_info)
244 };
245 
246 /* ifnet interface */
247 static void cxgbe_init(void *);
248 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
249 static int cxgbe_transmit(if_t, struct mbuf *);
250 static void cxgbe_qflush(if_t);
251 #if defined(KERN_TLS) || defined(RATELIMIT)
252 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
253     struct m_snd_tag **);
254 #endif
255 
256 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
257 
258 /*
259  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
260  * then ADAPTER_LOCK, then t4_uld_list_lock.
261  */
262 static struct sx t4_list_lock;
263 SLIST_HEAD(, adapter) t4_list;
264 #ifdef TCP_OFFLOAD
265 static struct sx t4_uld_list_lock;
266 struct uld_info *t4_uld_list[ULD_MAX + 1];
267 #endif
268 
269 /*
270  * Tunables.  See tweak_tunables() too.
271  *
272  * Each tunable is set to a default value here if it's known at compile-time.
273  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
274  * provide a reasonable default (upto n) when the driver is loaded.
275  *
276  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
277  * T5 are under hw.cxl.
278  */
279 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
280     "cxgbe(4) parameters");
281 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
282     "cxgbe(4) T5+ parameters");
283 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
284     "cxgbe(4) TOE parameters");
285 
286 /*
287  * Number of queues for tx and rx, NIC and offload.
288  */
289 #define NTXQ 16
290 int t4_ntxq = -NTXQ;
291 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
292     "Number of TX queues per port");
293 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
294 
295 #define NRXQ 8
296 int t4_nrxq = -NRXQ;
297 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
298     "Number of RX queues per port");
299 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
300 
301 #define NTXQ_VI 1
302 static int t4_ntxq_vi = -NTXQ_VI;
303 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
304     "Number of TX queues per VI");
305 
306 #define NRXQ_VI 1
307 static int t4_nrxq_vi = -NRXQ_VI;
308 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
309     "Number of RX queues per VI");
310 
311 static int t4_rsrv_noflowq = 0;
312 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
313     0, "Reserve TX queue 0 of each VI for non-flowid packets");
314 
315 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
316 #define NOFLDTXQ 8
317 static int t4_nofldtxq = -NOFLDTXQ;
318 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
319     "Number of offload TX queues per port");
320 
321 #define NOFLDRXQ 2
322 static int t4_nofldrxq = -NOFLDRXQ;
323 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
324     "Number of offload RX queues per port");
325 
326 #define NOFLDTXQ_VI 1
327 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
328 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
329     "Number of offload TX queues per VI");
330 
331 #define NOFLDRXQ_VI 1
332 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
333 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
334     "Number of offload RX queues per VI");
335 
336 #define TMR_IDX_OFLD 1
337 int t4_tmr_idx_ofld = TMR_IDX_OFLD;
338 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
339     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
340 
341 #define PKTC_IDX_OFLD (-1)
342 int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
343 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
344     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
345 
346 /* 0 means chip/fw default, non-zero number is value in microseconds */
347 static u_long t4_toe_keepalive_idle = 0;
348 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
349     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
350 
351 /* 0 means chip/fw default, non-zero number is value in microseconds */
352 static u_long t4_toe_keepalive_interval = 0;
353 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
354     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
355 
356 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
357 static int t4_toe_keepalive_count = 0;
358 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
359     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
360 
361 /* 0 means chip/fw default, non-zero number is value in microseconds */
362 static u_long t4_toe_rexmt_min = 0;
363 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
364     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
365 
366 /* 0 means chip/fw default, non-zero number is value in microseconds */
367 static u_long t4_toe_rexmt_max = 0;
368 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
369     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
370 
371 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
372 static int t4_toe_rexmt_count = 0;
373 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
374     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
375 
376 /* -1 means chip/fw default, other values are raw backoff values to use */
377 static int t4_toe_rexmt_backoff[16] = {
378 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
379 };
380 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
381     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
382     "cxgbe(4) TOE retransmit backoff values");
383 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
384     &t4_toe_rexmt_backoff[0], 0, "");
385 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
386     &t4_toe_rexmt_backoff[1], 0, "");
387 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
388     &t4_toe_rexmt_backoff[2], 0, "");
389 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
390     &t4_toe_rexmt_backoff[3], 0, "");
391 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
392     &t4_toe_rexmt_backoff[4], 0, "");
393 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
394     &t4_toe_rexmt_backoff[5], 0, "");
395 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
396     &t4_toe_rexmt_backoff[6], 0, "");
397 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
398     &t4_toe_rexmt_backoff[7], 0, "");
399 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
400     &t4_toe_rexmt_backoff[8], 0, "");
401 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
402     &t4_toe_rexmt_backoff[9], 0, "");
403 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
404     &t4_toe_rexmt_backoff[10], 0, "");
405 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
406     &t4_toe_rexmt_backoff[11], 0, "");
407 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
408     &t4_toe_rexmt_backoff[12], 0, "");
409 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
410     &t4_toe_rexmt_backoff[13], 0, "");
411 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
412     &t4_toe_rexmt_backoff[14], 0, "");
413 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
414     &t4_toe_rexmt_backoff[15], 0, "");
415 
416 int t4_ddp_rcvbuf_len = 256 * 1024;
417 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_len, CTLFLAG_RWTUN,
418     &t4_ddp_rcvbuf_len, 0, "length of each DDP RX buffer");
419 
420 unsigned int t4_ddp_rcvbuf_cache = 4;
421 SYSCTL_UINT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_cache, CTLFLAG_RWTUN,
422     &t4_ddp_rcvbuf_cache, 0,
423     "maximum number of free DDP RX buffers to cache per connection");
424 #endif
425 
426 #ifdef DEV_NETMAP
427 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
428 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
429 static int t4_native_netmap = NN_EXTRA_VI;
430 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
431     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
432 
433 #define NNMTXQ 8
434 static int t4_nnmtxq = -NNMTXQ;
435 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
436     "Number of netmap TX queues");
437 
438 #define NNMRXQ 8
439 static int t4_nnmrxq = -NNMRXQ;
440 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
441     "Number of netmap RX queues");
442 
443 #define NNMTXQ_VI 2
444 static int t4_nnmtxq_vi = -NNMTXQ_VI;
445 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
446     "Number of netmap TX queues per VI");
447 
448 #define NNMRXQ_VI 2
449 static int t4_nnmrxq_vi = -NNMRXQ_VI;
450 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
451     "Number of netmap RX queues per VI");
452 #endif
453 
454 /*
455  * Holdoff parameters for ports.
456  */
457 #define TMR_IDX 1
458 int t4_tmr_idx = TMR_IDX;
459 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
460     0, "Holdoff timer index");
461 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
462 
463 #define PKTC_IDX (-1)
464 int t4_pktc_idx = PKTC_IDX;
465 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
466     0, "Holdoff packet counter index");
467 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
468 
469 /*
470  * Size (# of entries) of each tx and rx queue.
471  */
472 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
473 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
474     "Number of descriptors in each TX queue");
475 
476 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
477 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
478     "Number of descriptors in each RX queue");
479 
480 /*
481  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
482  */
483 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
484 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
485     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
486 
487 /*
488  * Configuration file.  All the _CF names here are special.
489  */
490 #define DEFAULT_CF	"default"
491 #define BUILTIN_CF	"built-in"
492 #define FLASH_CF	"flash"
493 #define UWIRE_CF	"uwire"
494 #define FPGA_CF		"fpga"
495 static char t4_cfg_file[32] = DEFAULT_CF;
496 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
497     sizeof(t4_cfg_file), "Firmware configuration file");
498 
499 /*
500  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
501  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
502  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
503  *            mark or when signalled to do so, 0 to never emit PAUSE.
504  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
505  *                 negotiated settings will override rx_pause/tx_pause.
506  *                 Otherwise rx_pause/tx_pause are applied forcibly.
507  */
508 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
509 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
510     &t4_pause_settings, 0,
511     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
512 
513 /*
514  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
515  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
516  *  0 to disable FEC.
517  */
518 static int t4_fec = -1;
519 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
520     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
521 
522 /*
523  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
524  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
525  * driver runs as if this is set to 0.
526  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
527  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
528  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
529  *    the firmware anyway (may result in l1cfg errors with old firmwares).
530  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
531  *    means set all FEC bits that are valid for the speed.
532  */
533 static int t4_force_fec = -1;
534 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
535     "Controls the use of FORCE_FEC bit in L1 configuration.");
536 
537 /*
538  * Link autonegotiation.
539  * -1 to run with the firmware default.
540  *  0 to disable.
541  *  1 to enable.
542  */
543 static int t4_autoneg = -1;
544 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
545     "Link autonegotiation");
546 
547 /*
548  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
549  * encouraged respectively).  '-n' is the same as 'n' except the firmware
550  * version used in the checks is read from the firmware bundled with the driver.
551  */
552 static int t4_fw_install = 1;
553 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
554     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
555 
556 /*
557  * ASIC features that will be used.  Disable the ones you don't want so that the
558  * chip resources aren't wasted on features that will not be used.
559  */
560 static int t4_nbmcaps_allowed = 0;
561 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
562     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
563 
564 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
565 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
566     &t4_linkcaps_allowed, 0, "Default link capabilities");
567 
568 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
569     FW_CAPS_CONFIG_SWITCH_EGRESS;
570 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
571     &t4_switchcaps_allowed, 0, "Default switch capabilities");
572 
573 #ifdef RATELIMIT
574 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
575 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
576 #else
577 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
578 	FW_CAPS_CONFIG_NIC_HASHFILTER;
579 #endif
580 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
581     &t4_niccaps_allowed, 0, "Default NIC capabilities");
582 
583 static int t4_toecaps_allowed = -1;
584 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
585     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
586 
587 static int t4_rdmacaps_allowed = -1;
588 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
589     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
590 
591 static int t4_cryptocaps_allowed = -1;
592 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
593     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
594 
595 static int t4_iscsicaps_allowed = -1;
596 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
597     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
598 
599 static int t4_fcoecaps_allowed = 0;
600 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
601     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
602 
603 static int t5_write_combine = 0;
604 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
605     0, "Use WC instead of UC for BAR2");
606 
607 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
608 static int t4_doorbells_allowed = 0xf;
609 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
610 	   &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
611 
612 static int t4_num_vis = 1;
613 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
614     "Number of VIs per port");
615 
616 /*
617  * PCIe Relaxed Ordering.
618  * -1: driver should figure out a good value.
619  * 0: disable RO.
620  * 1: enable RO.
621  * 2: leave RO alone.
622  */
623 static int pcie_relaxed_ordering = -1;
624 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
625     &pcie_relaxed_ordering, 0,
626     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
627 
628 static int t4_panic_on_fatal_err = 0;
629 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
630     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
631 
632 static int t4_reset_on_fatal_err = 0;
633 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
634     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
635 
636 static int t4_clock_gate_on_suspend = 0;
637 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
638     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
639 
640 static int t4_tx_vm_wr = 0;
641 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
642     "Use VM work requests to transmit packets.");
643 
644 /*
645  * Set to non-zero to enable the attack filter.  A packet that matches any of
646  * these conditions will get dropped on ingress:
647  * 1) IP && source address == destination address.
648  * 2) TCP/IP && source address is not a unicast address.
649  * 3) TCP/IP && destination address is not a unicast address.
650  * 4) IP && source address is loopback (127.x.y.z).
651  * 5) IP && destination address is loopback (127.x.y.z).
652  * 6) IPv6 && source address == destination address.
653  * 7) IPv6 && source address is not a unicast address.
654  * 8) IPv6 && source address is loopback (::1/128).
655  * 9) IPv6 && destination address is loopback (::1/128).
656  * 10) IPv6 && source address is unspecified (::/128).
657  * 11) IPv6 && destination address is unspecified (::/128).
658  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
659  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
660  */
661 static int t4_attack_filter = 0;
662 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
663     &t4_attack_filter, 0, "Drop suspicious traffic");
664 
665 static int t4_drop_ip_fragments = 0;
666 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
667     &t4_drop_ip_fragments, 0, "Drop IP fragments");
668 
669 static int t4_drop_pkts_with_l2_errors = 1;
670 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
671     &t4_drop_pkts_with_l2_errors, 0,
672     "Drop all frames with Layer 2 length or checksum errors");
673 
674 static int t4_drop_pkts_with_l3_errors = 0;
675 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
676     &t4_drop_pkts_with_l3_errors, 0,
677     "Drop all frames with IP version, length, or checksum errors");
678 
679 static int t4_drop_pkts_with_l4_errors = 0;
680 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
681     &t4_drop_pkts_with_l4_errors, 0,
682     "Drop all frames with Layer 4 length, checksum, or other errors");
683 
684 #ifdef TCP_OFFLOAD
685 /*
686  * TOE tunables.
687  */
688 static int t4_cop_managed_offloading = 0;
689 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
690     &t4_cop_managed_offloading, 0,
691     "COP (Connection Offload Policy) controls all TOE offload");
692 #endif
693 
694 #ifdef KERN_TLS
695 /*
696  * This enables KERN_TLS for all adapters if set.
697  */
698 static int t4_kern_tls = 0;
699 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
700     "Enable KERN_TLS mode for T6 adapters");
701 
702 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
703     "cxgbe(4) KERN_TLS parameters");
704 
705 static int t4_tls_inline_keys = 0;
706 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
707     &t4_tls_inline_keys, 0,
708     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
709     "in card memory.");
710 
711 static int t4_tls_combo_wrs = 0;
712 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
713     0, "Attempt to combine TCB field updates with TLS record work requests.");
714 #endif
715 
716 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
717 static int vi_mac_funcs[] = {
718 	FW_VI_FUNC_ETH,
719 	FW_VI_FUNC_OFLD,
720 	FW_VI_FUNC_IWARP,
721 	FW_VI_FUNC_OPENISCSI,
722 	FW_VI_FUNC_OPENFCOE,
723 	FW_VI_FUNC_FOISCSI,
724 	FW_VI_FUNC_FOFCOE,
725 };
726 
727 struct intrs_and_queues {
728 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
729 	uint16_t num_vis;	/* number of VIs for each port */
730 	uint16_t nirq;		/* Total # of vectors */
731 	uint16_t ntxq;		/* # of NIC txq's for each port */
732 	uint16_t nrxq;		/* # of NIC rxq's for each port */
733 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
734 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
735 	uint16_t nnmtxq;	/* # of netmap txq's */
736 	uint16_t nnmrxq;	/* # of netmap rxq's */
737 
738 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
739 	uint16_t ntxq_vi;	/* # of NIC txq's */
740 	uint16_t nrxq_vi;	/* # of NIC rxq's */
741 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
742 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
743 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
744 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
745 };
746 
747 static void setup_memwin(struct adapter *);
748 static void position_memwin(struct adapter *, int, uint32_t);
749 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
750 static int fwmtype_to_hwmtype(int);
751 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
752     uint32_t *);
753 static int fixup_devlog_params(struct adapter *);
754 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
755 static int contact_firmware(struct adapter *);
756 static int partition_resources(struct adapter *);
757 static int get_params__pre_init(struct adapter *);
758 static int set_params__pre_init(struct adapter *);
759 static int get_params__post_init(struct adapter *);
760 static int set_params__post_init(struct adapter *);
761 static void t4_set_desc(struct adapter *);
762 static bool fixed_ifmedia(struct port_info *);
763 static void build_medialist(struct port_info *);
764 static void init_link_config(struct port_info *);
765 static int fixup_link_config(struct port_info *);
766 static int apply_link_config(struct port_info *);
767 static int cxgbe_init_synchronized(struct vi_info *);
768 static int cxgbe_uninit_synchronized(struct vi_info *);
769 static int adapter_full_init(struct adapter *);
770 static void adapter_full_uninit(struct adapter *);
771 static int vi_full_init(struct vi_info *);
772 static void vi_full_uninit(struct vi_info *);
773 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
774 static void quiesce_txq(struct sge_txq *);
775 static void quiesce_wrq(struct sge_wrq *);
776 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
777 static void quiesce_vi(struct vi_info *);
778 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
779     driver_intr_t *, void *, char *);
780 static int t4_free_irq(struct adapter *, struct irq *);
781 static void t4_init_atid_table(struct adapter *);
782 static void t4_free_atid_table(struct adapter *);
783 static void stop_atid_allocator(struct adapter *);
784 static void restart_atid_allocator(struct adapter *);
785 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
786 static void vi_refresh_stats(struct vi_info *);
787 static void cxgbe_refresh_stats(struct vi_info *);
788 static void cxgbe_tick(void *);
789 static void vi_tick(void *);
790 static void cxgbe_sysctls(struct port_info *);
791 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
792 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
793 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
794 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
795 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
796 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
797 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
798 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
799 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
800 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
801 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
802 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
803 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
804 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
805 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
806 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
807 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
808 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
809 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
810 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
811 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
812 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
813 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
814 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
815 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
816 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
817 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
818 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
819 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
820 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
821 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
822 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
823 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
824 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
825 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
826 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
827 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
828 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
829 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
830 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
831 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
832 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
833 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
834 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
835 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
836 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
837 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
838 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
839 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
840 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
841 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
842 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
843 #ifdef TCP_OFFLOAD
844 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
845 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
846 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
847 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
848 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
849 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
850 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
851 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
852 #endif
853 static int get_sge_context(struct adapter *, struct t4_sge_context *);
854 static int load_fw(struct adapter *, struct t4_data *);
855 static int load_cfg(struct adapter *, struct t4_data *);
856 static int load_boot(struct adapter *, struct t4_bootrom *);
857 static int load_bootcfg(struct adapter *, struct t4_data *);
858 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
859 static void free_offload_policy(struct t4_offload_policy *);
860 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
861 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
862 static int read_i2c(struct adapter *, struct t4_i2c_data *);
863 static int clear_stats(struct adapter *, u_int);
864 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
865 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
866 static inline int stop_adapter(struct adapter *);
867 static inline void set_adapter_hwstatus(struct adapter *, const bool);
868 static int stop_lld(struct adapter *);
869 static inline int restart_adapter(struct adapter *);
870 static int restart_lld(struct adapter *);
871 #ifdef TCP_OFFLOAD
872 static int deactivate_all_uld(struct adapter *);
873 static void stop_all_uld(struct adapter *);
874 static void restart_all_uld(struct adapter *);
875 #endif
876 #ifdef KERN_TLS
877 static int ktls_capability(struct adapter *, bool);
878 #endif
879 static int mod_event(module_t, int, void *);
880 static int notify_siblings(device_t, int);
881 static uint64_t vi_get_counter(if_t, ift_counter);
882 static uint64_t cxgbe_get_counter(if_t, ift_counter);
883 static void enable_vxlan_rx(struct adapter *);
884 static void reset_adapter_task(void *, int);
885 static void fatal_error_task(void *, int);
886 static void dump_devlog(struct adapter *);
887 static void dump_cim_regs(struct adapter *);
888 static void dump_cimla(struct adapter *);
889 
890 struct {
891 	uint16_t device;
892 	char *desc;
893 } t4_pciids[] = {
894 	{0xa000, "Chelsio Terminator 4 FPGA"},
895 	{0x4400, "Chelsio T440-dbg"},
896 	{0x4401, "Chelsio T420-CR"},
897 	{0x4402, "Chelsio T422-CR"},
898 	{0x4403, "Chelsio T440-CR"},
899 	{0x4404, "Chelsio T420-BCH"},
900 	{0x4405, "Chelsio T440-BCH"},
901 	{0x4406, "Chelsio T440-CH"},
902 	{0x4407, "Chelsio T420-SO"},
903 	{0x4408, "Chelsio T420-CX"},
904 	{0x4409, "Chelsio T420-BT"},
905 	{0x440a, "Chelsio T404-BT"},
906 	{0x440e, "Chelsio T440-LP-CR"},
907 }, t5_pciids[] = {
908 	{0xb000, "Chelsio Terminator 5 FPGA"},
909 	{0x5400, "Chelsio T580-dbg"},
910 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
911 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
912 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
913 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
914 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
915 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
916 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
917 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
918 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
919 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
920 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
921 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
922 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
923 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
924 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
925 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
926 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
927 
928 	/* Custom */
929 	{0x5483, "Custom T540-CR"},
930 	{0x5484, "Custom T540-BT"},
931 }, t6_pciids[] = {
932 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
933 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
934 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
935 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
936 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
937 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
938 	{0x6405, "Chelsio T6225-OCP-SO"},	/* 2 x 10/25G, nomem */
939 	{0x6406, "Chelsio T62100-OCP-SO"},	/* 2 x 40/50/100G, nomem */
940 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
941 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
942 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
943 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
944 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
945 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
946 	{0x6414, "Chelsio T61100-OCP-SO"},	/* 1 x 40/50/100G, nomem */
947 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
948 
949 	/* Custom */
950 	{0x6480, "Custom T6225-CR"},
951 	{0x6481, "Custom T62100-CR"},
952 	{0x6482, "Custom T6225-CR"},
953 	{0x6483, "Custom T62100-CR"},
954 	{0x6484, "Custom T64100-CR"},
955 	{0x6485, "Custom T6240-SO"},
956 	{0x6486, "Custom T6225-SO-CR"},
957 	{0x6487, "Custom T6225-CR"},
958 };
959 
960 #ifdef TCP_OFFLOAD
961 /*
962  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
963  * be exactly the same for both rxq and ofld_rxq.
964  */
965 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
966 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
967 #endif
968 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
969 
970 static int
t4_probe(device_t dev)971 t4_probe(device_t dev)
972 {
973 	int i;
974 	uint16_t v = pci_get_vendor(dev);
975 	uint16_t d = pci_get_device(dev);
976 	uint8_t f = pci_get_function(dev);
977 
978 	if (v != PCI_VENDOR_ID_CHELSIO)
979 		return (ENXIO);
980 
981 	/* Attach only to PF0 of the FPGA */
982 	if (d == 0xa000 && f != 0)
983 		return (ENXIO);
984 
985 	for (i = 0; i < nitems(t4_pciids); i++) {
986 		if (d == t4_pciids[i].device) {
987 			device_set_desc(dev, t4_pciids[i].desc);
988 			return (BUS_PROBE_DEFAULT);
989 		}
990 	}
991 
992 	return (ENXIO);
993 }
994 
995 static int
t5_probe(device_t dev)996 t5_probe(device_t dev)
997 {
998 	int i;
999 	uint16_t v = pci_get_vendor(dev);
1000 	uint16_t d = pci_get_device(dev);
1001 	uint8_t f = pci_get_function(dev);
1002 
1003 	if (v != PCI_VENDOR_ID_CHELSIO)
1004 		return (ENXIO);
1005 
1006 	/* Attach only to PF0 of the FPGA */
1007 	if (d == 0xb000 && f != 0)
1008 		return (ENXIO);
1009 
1010 	for (i = 0; i < nitems(t5_pciids); i++) {
1011 		if (d == t5_pciids[i].device) {
1012 			device_set_desc(dev, t5_pciids[i].desc);
1013 			return (BUS_PROBE_DEFAULT);
1014 		}
1015 	}
1016 
1017 	return (ENXIO);
1018 }
1019 
1020 static int
t6_probe(device_t dev)1021 t6_probe(device_t dev)
1022 {
1023 	int i;
1024 	uint16_t v = pci_get_vendor(dev);
1025 	uint16_t d = pci_get_device(dev);
1026 
1027 	if (v != PCI_VENDOR_ID_CHELSIO)
1028 		return (ENXIO);
1029 
1030 	for (i = 0; i < nitems(t6_pciids); i++) {
1031 		if (d == t6_pciids[i].device) {
1032 			device_set_desc(dev, t6_pciids[i].desc);
1033 			return (BUS_PROBE_DEFAULT);
1034 		}
1035 	}
1036 
1037 	return (ENXIO);
1038 }
1039 
1040 static void
t5_attribute_workaround(device_t dev)1041 t5_attribute_workaround(device_t dev)
1042 {
1043 	device_t root_port;
1044 	uint32_t v;
1045 
1046 	/*
1047 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1048 	 * Ordering attributes when replying to a TLP from a Root
1049 	 * Port.  As a workaround, find the parent Root Port and
1050 	 * disable No Snoop and Relaxed Ordering.  Note that this
1051 	 * affects all devices under this root port.
1052 	 */
1053 	root_port = pci_find_pcie_root_port(dev);
1054 	if (root_port == NULL) {
1055 		device_printf(dev, "Unable to find parent root port\n");
1056 		return;
1057 	}
1058 
1059 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1060 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1061 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1062 	    0)
1063 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1064 		    device_get_nameunit(root_port));
1065 }
1066 
1067 static const struct devnames devnames[] = {
1068 	{
1069 		.nexus_name = "t4nex",
1070 		.ifnet_name = "cxgbe",
1071 		.vi_ifnet_name = "vcxgbe",
1072 		.pf03_drv_name = "t4iov",
1073 		.vf_nexus_name = "t4vf",
1074 		.vf_ifnet_name = "cxgbev"
1075 	}, {
1076 		.nexus_name = "t5nex",
1077 		.ifnet_name = "cxl",
1078 		.vi_ifnet_name = "vcxl",
1079 		.pf03_drv_name = "t5iov",
1080 		.vf_nexus_name = "t5vf",
1081 		.vf_ifnet_name = "cxlv"
1082 	}, {
1083 		.nexus_name = "t6nex",
1084 		.ifnet_name = "cc",
1085 		.vi_ifnet_name = "vcc",
1086 		.pf03_drv_name = "t6iov",
1087 		.vf_nexus_name = "t6vf",
1088 		.vf_ifnet_name = "ccv"
1089 	}
1090 };
1091 
1092 void
t4_init_devnames(struct adapter * sc)1093 t4_init_devnames(struct adapter *sc)
1094 {
1095 	int id;
1096 
1097 	id = chip_id(sc);
1098 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
1099 		sc->names = &devnames[id - CHELSIO_T4];
1100 	else {
1101 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1102 		sc->names = NULL;
1103 	}
1104 }
1105 
1106 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1107 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1108 {
1109 	const char *parent, *name;
1110 	long value;
1111 	int line, unit;
1112 
1113 	line = 0;
1114 	parent = device_get_nameunit(sc->dev);
1115 	name = sc->names->ifnet_name;
1116 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1117 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1118 		    value == pi->port_id)
1119 			return (unit);
1120 	}
1121 	return (-1);
1122 }
1123 
1124 static void
t4_calibration(void * arg)1125 t4_calibration(void *arg)
1126 {
1127 	struct adapter *sc;
1128 	struct clock_sync *cur, *nex;
1129 	uint64_t hw;
1130 	sbintime_t sbt;
1131 	int next_up;
1132 
1133 	sc = (struct adapter *)arg;
1134 
1135 	KASSERT((hw_off_limits(sc) == 0), ("hw_off_limits at t4_calibration"));
1136 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1137 	sbt = sbinuptime();
1138 
1139 	cur = &sc->cal_info[sc->cal_current];
1140 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1141 	nex = &sc->cal_info[next_up];
1142 	if (__predict_false(sc->cal_count == 0)) {
1143 		/* First time in, just get the values in */
1144 		cur->hw_cur = hw;
1145 		cur->sbt_cur = sbt;
1146 		sc->cal_count++;
1147 		goto done;
1148 	}
1149 
1150 	if (cur->hw_cur == hw) {
1151 		/* The clock is not advancing? */
1152 		sc->cal_count = 0;
1153 		atomic_store_rel_int(&cur->gen, 0);
1154 		goto done;
1155 	}
1156 
1157 	seqc_write_begin(&nex->gen);
1158 	nex->hw_prev = cur->hw_cur;
1159 	nex->sbt_prev = cur->sbt_cur;
1160 	nex->hw_cur = hw;
1161 	nex->sbt_cur = sbt;
1162 	seqc_write_end(&nex->gen);
1163 	sc->cal_current = next_up;
1164 done:
1165 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1166 	    sc, C_DIRECT_EXEC);
1167 }
1168 
1169 static void
t4_calibration_start(struct adapter * sc)1170 t4_calibration_start(struct adapter *sc)
1171 {
1172 	/*
1173 	 * Here if we have not done a calibration
1174 	 * then do so otherwise start the appropriate
1175 	 * timer.
1176 	 */
1177 	int i;
1178 
1179 	for (i = 0; i < CNT_CAL_INFO; i++) {
1180 		sc->cal_info[i].gen = 0;
1181 	}
1182 	sc->cal_current = 0;
1183 	sc->cal_count = 0;
1184 	sc->cal_gen = 0;
1185 	t4_calibration(sc);
1186 }
1187 
1188 static int
t4_attach(device_t dev)1189 t4_attach(device_t dev)
1190 {
1191 	struct adapter *sc;
1192 	int rc = 0, i, j, rqidx, tqidx, nports;
1193 	struct make_dev_args mda;
1194 	struct intrs_and_queues iaq;
1195 	struct sge *s;
1196 	uint32_t *buf;
1197 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1198 	int ofld_tqidx;
1199 #endif
1200 #ifdef TCP_OFFLOAD
1201 	int ofld_rqidx;
1202 #endif
1203 #ifdef DEV_NETMAP
1204 	int nm_rqidx, nm_tqidx;
1205 #endif
1206 	int num_vis;
1207 
1208 	sc = device_get_softc(dev);
1209 	sc->dev = dev;
1210 	sysctl_ctx_init(&sc->ctx);
1211 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1212 
1213 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1214 		t5_attribute_workaround(dev);
1215 	pci_enable_busmaster(dev);
1216 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1217 		uint32_t v;
1218 
1219 		pci_set_max_read_req(dev, 4096);
1220 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1221 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1222 		if (pcie_relaxed_ordering == 0 &&
1223 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1224 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1225 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1226 		} else if (pcie_relaxed_ordering == 1 &&
1227 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1228 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1229 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1230 		}
1231 	}
1232 
1233 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1234 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1235 	sc->traceq = -1;
1236 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1237 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1238 	    device_get_nameunit(dev));
1239 
1240 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1241 	    device_get_nameunit(dev));
1242 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1243 	t4_add_adapter(sc);
1244 
1245 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1246 	TAILQ_INIT(&sc->sfl);
1247 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1248 
1249 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1250 
1251 	sc->policy = NULL;
1252 	rw_init(&sc->policy_lock, "connection offload policy");
1253 
1254 	callout_init(&sc->ktls_tick, 1);
1255 
1256 	callout_init(&sc->cal_callout, 1);
1257 
1258 	refcount_init(&sc->vxlan_refcount, 0);
1259 
1260 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1261 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1262 
1263 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1264 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1265 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1266 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1267 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1268 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1269 
1270 	rc = t4_map_bars_0_and_4(sc);
1271 	if (rc != 0)
1272 		goto done; /* error message displayed already */
1273 
1274 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1275 
1276 	/* Prepare the adapter for operation. */
1277 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1278 	rc = -t4_prep_adapter(sc, buf);
1279 	free(buf, M_CXGBE);
1280 	if (rc != 0) {
1281 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1282 		goto done;
1283 	}
1284 
1285 	/*
1286 	 * This is the real PF# to which we're attaching.  Works from within PCI
1287 	 * passthrough environments too, where pci_get_function() could return a
1288 	 * different PF# depending on the passthrough configuration.  We need to
1289 	 * use the real PF# in all our communication with the firmware.
1290 	 */
1291 	j = t4_read_reg(sc, A_PL_WHOAMI);
1292 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1293 	sc->mbox = sc->pf;
1294 
1295 	t4_init_devnames(sc);
1296 	if (sc->names == NULL) {
1297 		rc = ENOTSUP;
1298 		goto done; /* error message displayed already */
1299 	}
1300 
1301 	/*
1302 	 * Do this really early, with the memory windows set up even before the
1303 	 * character device.  The userland tool's register i/o and mem read
1304 	 * will work even in "recovery mode".
1305 	 */
1306 	setup_memwin(sc);
1307 	if (t4_init_devlog_params(sc, 0) == 0)
1308 		fixup_devlog_params(sc);
1309 	make_dev_args_init(&mda);
1310 	mda.mda_devsw = &t4_cdevsw;
1311 	mda.mda_uid = UID_ROOT;
1312 	mda.mda_gid = GID_WHEEL;
1313 	mda.mda_mode = 0600;
1314 	mda.mda_si_drv1 = sc;
1315 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1316 	if (rc != 0)
1317 		device_printf(dev, "failed to create nexus char device: %d.\n",
1318 		    rc);
1319 
1320 	/* Go no further if recovery mode has been requested. */
1321 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1322 		device_printf(dev, "recovery mode.\n");
1323 		goto done;
1324 	}
1325 
1326 #if defined(__i386__)
1327 	if ((cpu_feature & CPUID_CX8) == 0) {
1328 		device_printf(dev, "64 bit atomics not available.\n");
1329 		rc = ENOTSUP;
1330 		goto done;
1331 	}
1332 #endif
1333 
1334 	/* Contact the firmware and try to become the master driver. */
1335 	rc = contact_firmware(sc);
1336 	if (rc != 0)
1337 		goto done; /* error message displayed already */
1338 	MPASS(sc->flags & FW_OK);
1339 
1340 	rc = get_params__pre_init(sc);
1341 	if (rc != 0)
1342 		goto done; /* error message displayed already */
1343 
1344 	if (sc->flags & MASTER_PF) {
1345 		rc = partition_resources(sc);
1346 		if (rc != 0)
1347 			goto done; /* error message displayed already */
1348 	}
1349 
1350 	rc = get_params__post_init(sc);
1351 	if (rc != 0)
1352 		goto done; /* error message displayed already */
1353 
1354 	rc = set_params__post_init(sc);
1355 	if (rc != 0)
1356 		goto done; /* error message displayed already */
1357 
1358 	rc = t4_map_bar_2(sc);
1359 	if (rc != 0)
1360 		goto done; /* error message displayed already */
1361 
1362 	rc = t4_adj_doorbells(sc);
1363 	if (rc != 0)
1364 		goto done; /* error message displayed already */
1365 
1366 	rc = t4_create_dma_tag(sc);
1367 	if (rc != 0)
1368 		goto done; /* error message displayed already */
1369 
1370 	/*
1371 	 * First pass over all the ports - allocate VIs and initialize some
1372 	 * basic parameters like mac address, port type, etc.
1373 	 */
1374 	for_each_port(sc, i) {
1375 		struct port_info *pi;
1376 
1377 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1378 		sc->port[i] = pi;
1379 
1380 		/* These must be set before t4_port_init */
1381 		pi->adapter = sc;
1382 		pi->port_id = i;
1383 		/*
1384 		 * XXX: vi[0] is special so we can't delay this allocation until
1385 		 * pi->nvi's final value is known.
1386 		 */
1387 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1388 		    M_ZERO | M_WAITOK);
1389 
1390 		/*
1391 		 * Allocate the "main" VI and initialize parameters
1392 		 * like mac addr.
1393 		 */
1394 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1395 		if (rc != 0) {
1396 			device_printf(dev, "unable to initialize port %d: %d\n",
1397 			    i, rc);
1398 			free(pi->vi, M_CXGBE);
1399 			free(pi, M_CXGBE);
1400 			sc->port[i] = NULL;
1401 			goto done;
1402 		}
1403 
1404 		if (is_bt(pi->port_type))
1405 			setbit(&sc->bt_map, pi->tx_chan);
1406 		else
1407 			MPASS(!isset(&sc->bt_map, pi->tx_chan));
1408 
1409 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1410 		    device_get_nameunit(dev), i);
1411 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1412 		sc->chan_map[pi->tx_chan] = i;
1413 
1414 		/*
1415 		 * The MPS counter for FCS errors doesn't work correctly on the
1416 		 * T6 so we use the MAC counter here.  Which MAC is in use
1417 		 * depends on the link settings which will be known when the
1418 		 * link comes up.
1419 		 */
1420 		if (is_t6(sc))
1421 			pi->fcs_reg = -1;
1422 		else {
1423 			pi->fcs_reg = t4_port_reg(sc, pi->tx_chan,
1424 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1425 		}
1426 		pi->fcs_base = 0;
1427 
1428 		/* All VIs on this port share this media. */
1429 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1430 		    cxgbe_media_status);
1431 
1432 		PORT_LOCK(pi);
1433 		init_link_config(pi);
1434 		fixup_link_config(pi);
1435 		build_medialist(pi);
1436 		if (fixed_ifmedia(pi))
1437 			pi->flags |= FIXED_IFMEDIA;
1438 		PORT_UNLOCK(pi);
1439 
1440 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1441 		    t4_ifnet_unit(sc, pi));
1442 		if (pi->dev == NULL) {
1443 			device_printf(dev,
1444 			    "failed to add device for port %d.\n", i);
1445 			rc = ENXIO;
1446 			goto done;
1447 		}
1448 		pi->vi[0].dev = pi->dev;
1449 		device_set_softc(pi->dev, pi);
1450 	}
1451 
1452 	/*
1453 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1454 	 */
1455 	nports = sc->params.nports;
1456 	rc = cfg_itype_and_nqueues(sc, &iaq);
1457 	if (rc != 0)
1458 		goto done; /* error message displayed already */
1459 
1460 	num_vis = iaq.num_vis;
1461 	sc->intr_type = iaq.intr_type;
1462 	sc->intr_count = iaq.nirq;
1463 
1464 	s = &sc->sge;
1465 	s->nrxq = nports * iaq.nrxq;
1466 	s->ntxq = nports * iaq.ntxq;
1467 	if (num_vis > 1) {
1468 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1469 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1470 	}
1471 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1472 	s->neq += nports;		/* ctrl queues: 1 per port */
1473 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1474 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1475 	if (is_offload(sc) || is_ethoffload(sc)) {
1476 		s->nofldtxq = nports * iaq.nofldtxq;
1477 		if (num_vis > 1)
1478 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1479 		s->neq += s->nofldtxq;
1480 
1481 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1482 		    M_CXGBE, M_ZERO | M_WAITOK);
1483 	}
1484 #endif
1485 #ifdef TCP_OFFLOAD
1486 	if (is_offload(sc)) {
1487 		s->nofldrxq = nports * iaq.nofldrxq;
1488 		if (num_vis > 1)
1489 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1490 		s->neq += s->nofldrxq;	/* free list */
1491 		s->niq += s->nofldrxq;
1492 
1493 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1494 		    M_CXGBE, M_ZERO | M_WAITOK);
1495 	}
1496 #endif
1497 #ifdef DEV_NETMAP
1498 	s->nnmrxq = 0;
1499 	s->nnmtxq = 0;
1500 	if (t4_native_netmap & NN_MAIN_VI) {
1501 		s->nnmrxq += nports * iaq.nnmrxq;
1502 		s->nnmtxq += nports * iaq.nnmtxq;
1503 	}
1504 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1505 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1506 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1507 	}
1508 	s->neq += s->nnmtxq + s->nnmrxq;
1509 	s->niq += s->nnmrxq;
1510 
1511 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1512 	    M_CXGBE, M_ZERO | M_WAITOK);
1513 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1514 	    M_CXGBE, M_ZERO | M_WAITOK);
1515 #endif
1516 	MPASS(s->niq <= s->iqmap_sz);
1517 	MPASS(s->neq <= s->eqmap_sz);
1518 
1519 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1520 	    M_ZERO | M_WAITOK);
1521 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1522 	    M_ZERO | M_WAITOK);
1523 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1524 	    M_ZERO | M_WAITOK);
1525 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1526 	    M_ZERO | M_WAITOK);
1527 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1528 	    M_ZERO | M_WAITOK);
1529 
1530 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1531 	    M_ZERO | M_WAITOK);
1532 
1533 	t4_init_l2t(sc, M_WAITOK);
1534 	t4_init_smt(sc, M_WAITOK);
1535 	t4_init_tx_sched(sc);
1536 	t4_init_atid_table(sc);
1537 #ifdef RATELIMIT
1538 	t4_init_etid_table(sc);
1539 #endif
1540 #ifdef INET6
1541 	t4_init_clip_table(sc);
1542 #endif
1543 	if (sc->vres.key.size != 0)
1544 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1545 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1546 
1547 	/*
1548 	 * Second pass over the ports.  This time we know the number of rx and
1549 	 * tx queues that each port should get.
1550 	 */
1551 	rqidx = tqidx = 0;
1552 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1553 	ofld_tqidx = 0;
1554 #endif
1555 #ifdef TCP_OFFLOAD
1556 	ofld_rqidx = 0;
1557 #endif
1558 #ifdef DEV_NETMAP
1559 	nm_rqidx = nm_tqidx = 0;
1560 #endif
1561 	for_each_port(sc, i) {
1562 		struct port_info *pi = sc->port[i];
1563 		struct vi_info *vi;
1564 
1565 		if (pi == NULL)
1566 			continue;
1567 
1568 		pi->nvi = num_vis;
1569 		for_each_vi(pi, j, vi) {
1570 			vi->pi = pi;
1571 			vi->adapter = sc;
1572 			vi->first_intr = -1;
1573 			vi->qsize_rxq = t4_qsize_rxq;
1574 			vi->qsize_txq = t4_qsize_txq;
1575 
1576 			vi->first_rxq = rqidx;
1577 			vi->first_txq = tqidx;
1578 			vi->tmr_idx = t4_tmr_idx;
1579 			vi->pktc_idx = t4_pktc_idx;
1580 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1581 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1582 
1583 			rqidx += vi->nrxq;
1584 			tqidx += vi->ntxq;
1585 
1586 			if (j == 0 && vi->ntxq > 1)
1587 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1588 			else
1589 				vi->rsrv_noflowq = 0;
1590 
1591 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1592 			vi->first_ofld_txq = ofld_tqidx;
1593 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1594 			ofld_tqidx += vi->nofldtxq;
1595 #endif
1596 #ifdef TCP_OFFLOAD
1597 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1598 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1599 			vi->first_ofld_rxq = ofld_rqidx;
1600 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1601 
1602 			ofld_rqidx += vi->nofldrxq;
1603 #endif
1604 #ifdef DEV_NETMAP
1605 			vi->first_nm_rxq = nm_rqidx;
1606 			vi->first_nm_txq = nm_tqidx;
1607 			if (j == 0) {
1608 				vi->nnmrxq = iaq.nnmrxq;
1609 				vi->nnmtxq = iaq.nnmtxq;
1610 			} else {
1611 				vi->nnmrxq = iaq.nnmrxq_vi;
1612 				vi->nnmtxq = iaq.nnmtxq_vi;
1613 			}
1614 			nm_rqidx += vi->nnmrxq;
1615 			nm_tqidx += vi->nnmtxq;
1616 #endif
1617 		}
1618 	}
1619 
1620 	rc = t4_setup_intr_handlers(sc);
1621 	if (rc != 0) {
1622 		device_printf(dev,
1623 		    "failed to setup interrupt handlers: %d\n", rc);
1624 		goto done;
1625 	}
1626 
1627 	rc = bus_generic_probe(dev);
1628 	if (rc != 0) {
1629 		device_printf(dev, "failed to probe child drivers: %d\n", rc);
1630 		goto done;
1631 	}
1632 
1633 	/*
1634 	 * Ensure thread-safe mailbox access (in debug builds).
1635 	 *
1636 	 * So far this was the only thread accessing the mailbox but various
1637 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1638 	 * will access the mailbox from different threads.
1639 	 */
1640 	sc->flags |= CHK_MBOX_ACCESS;
1641 
1642 	rc = bus_generic_attach(dev);
1643 	if (rc != 0) {
1644 		device_printf(dev,
1645 		    "failed to attach all child ports: %d\n", rc);
1646 		goto done;
1647 	}
1648 	t4_calibration_start(sc);
1649 
1650 	device_printf(dev,
1651 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1652 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1653 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1654 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1655 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1656 
1657 	t4_set_desc(sc);
1658 
1659 	notify_siblings(dev, 0);
1660 
1661 done:
1662 	if (rc != 0 && sc->cdev) {
1663 		/* cdev was created and so cxgbetool works; recover that way. */
1664 		device_printf(dev,
1665 		    "error during attach, adapter is now in recovery mode.\n");
1666 		rc = 0;
1667 	}
1668 
1669 	if (rc != 0)
1670 		t4_detach_common(dev);
1671 	else
1672 		t4_sysctls(sc);
1673 
1674 	return (rc);
1675 }
1676 
1677 static int
t4_child_location(device_t bus,device_t dev,struct sbuf * sb)1678 t4_child_location(device_t bus, device_t dev, struct sbuf *sb)
1679 {
1680 	struct adapter *sc;
1681 	struct port_info *pi;
1682 	int i;
1683 
1684 	sc = device_get_softc(bus);
1685 	for_each_port(sc, i) {
1686 		pi = sc->port[i];
1687 		if (pi != NULL && pi->dev == dev) {
1688 			sbuf_printf(sb, "port=%d", pi->port_id);
1689 			break;
1690 		}
1691 	}
1692 	return (0);
1693 }
1694 
1695 static int
t4_ready(device_t dev)1696 t4_ready(device_t dev)
1697 {
1698 	struct adapter *sc;
1699 
1700 	sc = device_get_softc(dev);
1701 	if (sc->flags & FW_OK)
1702 		return (0);
1703 	return (ENXIO);
1704 }
1705 
1706 static int
t4_read_port_device(device_t dev,int port,device_t * child)1707 t4_read_port_device(device_t dev, int port, device_t *child)
1708 {
1709 	struct adapter *sc;
1710 	struct port_info *pi;
1711 
1712 	sc = device_get_softc(dev);
1713 	if (port < 0 || port >= MAX_NPORTS)
1714 		return (EINVAL);
1715 	pi = sc->port[port];
1716 	if (pi == NULL || pi->dev == NULL)
1717 		return (ENXIO);
1718 	*child = pi->dev;
1719 	return (0);
1720 }
1721 
1722 static int
notify_siblings(device_t dev,int detaching)1723 notify_siblings(device_t dev, int detaching)
1724 {
1725 	device_t sibling;
1726 	int error, i;
1727 
1728 	error = 0;
1729 	for (i = 0; i < PCI_FUNCMAX; i++) {
1730 		if (i == pci_get_function(dev))
1731 			continue;
1732 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1733 		    pci_get_slot(dev), i);
1734 		if (sibling == NULL || !device_is_attached(sibling))
1735 			continue;
1736 		if (detaching)
1737 			error = T4_DETACH_CHILD(sibling);
1738 		else
1739 			(void)T4_ATTACH_CHILD(sibling);
1740 		if (error)
1741 			break;
1742 	}
1743 	return (error);
1744 }
1745 
1746 /*
1747  * Idempotent
1748  */
1749 static int
t4_detach(device_t dev)1750 t4_detach(device_t dev)
1751 {
1752 	int rc;
1753 
1754 	rc = notify_siblings(dev, 1);
1755 	if (rc) {
1756 		device_printf(dev,
1757 		    "failed to detach sibling devices: %d\n", rc);
1758 		return (rc);
1759 	}
1760 
1761 	return (t4_detach_common(dev));
1762 }
1763 
1764 int
t4_detach_common(device_t dev)1765 t4_detach_common(device_t dev)
1766 {
1767 	struct adapter *sc;
1768 	struct port_info *pi;
1769 	int i, rc;
1770 
1771 	sc = device_get_softc(dev);
1772 
1773 #ifdef TCP_OFFLOAD
1774 	rc = deactivate_all_uld(sc);
1775 	if (rc) {
1776 		device_printf(dev,
1777 		    "failed to detach upper layer drivers: %d\n", rc);
1778 		return (rc);
1779 	}
1780 #endif
1781 
1782 	if (sc->cdev) {
1783 		destroy_dev(sc->cdev);
1784 		sc->cdev = NULL;
1785 	}
1786 
1787 	sx_xlock(&t4_list_lock);
1788 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1789 	sx_xunlock(&t4_list_lock);
1790 
1791 	sc->flags &= ~CHK_MBOX_ACCESS;
1792 	if (sc->flags & FULL_INIT_DONE) {
1793 		if (!(sc->flags & IS_VF))
1794 			t4_intr_disable(sc);
1795 	}
1796 
1797 	if (device_is_attached(dev)) {
1798 		rc = bus_generic_detach(dev);
1799 		if (rc) {
1800 			device_printf(dev,
1801 			    "failed to detach child devices: %d\n", rc);
1802 			return (rc);
1803 		}
1804 	}
1805 
1806 	for (i = 0; i < sc->intr_count; i++)
1807 		t4_free_irq(sc, &sc->irq[i]);
1808 
1809 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1810 		t4_free_tx_sched(sc);
1811 
1812 	for (i = 0; i < MAX_NPORTS; i++) {
1813 		pi = sc->port[i];
1814 		if (pi) {
1815 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1816 			if (pi->dev)
1817 				device_delete_child(dev, pi->dev);
1818 
1819 			mtx_destroy(&pi->pi_lock);
1820 			free(pi->vi, M_CXGBE);
1821 			free(pi, M_CXGBE);
1822 		}
1823 	}
1824 	callout_stop(&sc->cal_callout);
1825 	callout_drain(&sc->cal_callout);
1826 	device_delete_children(dev);
1827 	sysctl_ctx_free(&sc->ctx);
1828 	adapter_full_uninit(sc);
1829 
1830 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1831 		t4_fw_bye(sc, sc->mbox);
1832 
1833 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1834 		pci_release_msi(dev);
1835 
1836 	if (sc->regs_res)
1837 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1838 		    sc->regs_res);
1839 
1840 	if (sc->udbs_res)
1841 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1842 		    sc->udbs_res);
1843 
1844 	if (sc->msix_res)
1845 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1846 		    sc->msix_res);
1847 
1848 	if (sc->l2t)
1849 		t4_free_l2t(sc);
1850 	if (sc->smt)
1851 		t4_free_smt(sc->smt);
1852 	t4_free_atid_table(sc);
1853 #ifdef RATELIMIT
1854 	t4_free_etid_table(sc);
1855 #endif
1856 	if (sc->key_map)
1857 		vmem_destroy(sc->key_map);
1858 #ifdef INET6
1859 	t4_destroy_clip_table(sc);
1860 #endif
1861 
1862 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1863 	free(sc->sge.ofld_txq, M_CXGBE);
1864 #endif
1865 #ifdef TCP_OFFLOAD
1866 	free(sc->sge.ofld_rxq, M_CXGBE);
1867 #endif
1868 #ifdef DEV_NETMAP
1869 	free(sc->sge.nm_rxq, M_CXGBE);
1870 	free(sc->sge.nm_txq, M_CXGBE);
1871 #endif
1872 	free(sc->irq, M_CXGBE);
1873 	free(sc->sge.rxq, M_CXGBE);
1874 	free(sc->sge.txq, M_CXGBE);
1875 	free(sc->sge.ctrlq, M_CXGBE);
1876 	free(sc->sge.iqmap, M_CXGBE);
1877 	free(sc->sge.eqmap, M_CXGBE);
1878 	free(sc->tids.ftid_tab, M_CXGBE);
1879 	free(sc->tids.hpftid_tab, M_CXGBE);
1880 	free_hftid_hash(&sc->tids);
1881 	free(sc->tids.tid_tab, M_CXGBE);
1882 	t4_destroy_dma_tag(sc);
1883 
1884 	callout_drain(&sc->ktls_tick);
1885 	callout_drain(&sc->sfl_callout);
1886 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1887 		mtx_destroy(&sc->tids.ftid_lock);
1888 		cv_destroy(&sc->tids.ftid_cv);
1889 	}
1890 	if (mtx_initialized(&sc->tids.atid_lock))
1891 		mtx_destroy(&sc->tids.atid_lock);
1892 	if (mtx_initialized(&sc->ifp_lock))
1893 		mtx_destroy(&sc->ifp_lock);
1894 
1895 	if (rw_initialized(&sc->policy_lock)) {
1896 		rw_destroy(&sc->policy_lock);
1897 #ifdef TCP_OFFLOAD
1898 		if (sc->policy != NULL)
1899 			free_offload_policy(sc->policy);
1900 #endif
1901 	}
1902 
1903 	for (i = 0; i < NUM_MEMWIN; i++) {
1904 		struct memwin *mw = &sc->memwin[i];
1905 
1906 		if (rw_initialized(&mw->mw_lock))
1907 			rw_destroy(&mw->mw_lock);
1908 	}
1909 
1910 	mtx_destroy(&sc->sfl_lock);
1911 	mtx_destroy(&sc->reg_lock);
1912 	mtx_destroy(&sc->sc_lock);
1913 
1914 	bzero(sc, sizeof(*sc));
1915 
1916 	return (0);
1917 }
1918 
1919 static inline int
stop_adapter(struct adapter * sc)1920 stop_adapter(struct adapter *sc)
1921 {
1922 	struct port_info *pi;
1923 	int i;
1924 
1925 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1926 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1927 			 __func__, curthread, sc->flags, sc->error_flags);
1928 		return (EALREADY);
1929 	}
1930 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1931 		 sc->flags, sc->error_flags);
1932 	t4_shutdown_adapter(sc);
1933 	for_each_port(sc, i) {
1934 		pi = sc->port[i];
1935 		PORT_LOCK(pi);
1936 		if (pi->up_vis > 0 && pi->link_cfg.link_ok) {
1937 			/*
1938 			 * t4_shutdown_adapter has already shut down all the
1939 			 * PHYs but it also disables interrupts and DMA so there
1940 			 * won't be a link interrupt.  Update the state manually
1941 			 * if the link was up previously and inform the kernel.
1942 			 */
1943 			pi->link_cfg.link_ok = false;
1944 			t4_os_link_changed(pi);
1945 		}
1946 		PORT_UNLOCK(pi);
1947 	}
1948 
1949 	return (0);
1950 }
1951 
1952 static inline int
restart_adapter(struct adapter * sc)1953 restart_adapter(struct adapter *sc)
1954 {
1955 	uint32_t val;
1956 
1957 	if (!atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1958 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1959 			 __func__, curthread, sc->flags, sc->error_flags);
1960 		return (EALREADY);
1961 	}
1962 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1963 		 sc->flags, sc->error_flags);
1964 
1965 	MPASS(hw_off_limits(sc));
1966 	MPASS((sc->flags & FW_OK) == 0);
1967 	MPASS((sc->flags & MASTER_PF) == 0);
1968 	MPASS(sc->reset_thread == NULL);
1969 
1970 	/*
1971 	 * The adapter is supposed to be back on PCIE with its config space and
1972 	 * BARs restored to their state before reset.  Register access via
1973 	 * t4_read_reg BAR0 should just work.
1974 	 */
1975 	sc->reset_thread = curthread;
1976 	val = t4_read_reg(sc, A_PL_WHOAMI);
1977 	if (val == 0xffffffff || val == 0xeeeeeeee) {
1978 		CH_ERR(sc, "%s: device registers not readable.\n", __func__);
1979 		sc->reset_thread = NULL;
1980 		atomic_set_int(&sc->error_flags, ADAP_STOPPED);
1981 		return (ENXIO);
1982 	}
1983 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR);
1984 	atomic_add_int(&sc->incarnation, 1);
1985 	atomic_add_int(&sc->num_resets, 1);
1986 
1987 	return (0);
1988 }
1989 
1990 static inline void
set_adapter_hwstatus(struct adapter * sc,const bool usable)1991 set_adapter_hwstatus(struct adapter *sc, const bool usable)
1992 {
1993 	if (usable) {
1994 		/* Must be marked reusable by the designated thread. */
1995 		ASSERT_SYNCHRONIZED_OP(sc);
1996 		MPASS(sc->reset_thread == curthread);
1997 		mtx_lock(&sc->reg_lock);
1998 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
1999 		mtx_unlock(&sc->reg_lock);
2000 	} else {
2001 		/* Mark the adapter totally off limits. */
2002 		begin_synchronized_op(sc, NULL, SLEEP_OK, "t4hwsts");
2003 		mtx_lock(&sc->reg_lock);
2004 		atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
2005 		mtx_unlock(&sc->reg_lock);
2006 		sc->flags &= ~(FW_OK | MASTER_PF);
2007 		sc->reset_thread = NULL;
2008 		end_synchronized_op(sc, 0);
2009 	}
2010 }
2011 
2012 static int
stop_lld(struct adapter * sc)2013 stop_lld(struct adapter *sc)
2014 {
2015 	struct port_info *pi;
2016 	struct vi_info *vi;
2017 	if_t ifp;
2018 	struct sge_rxq *rxq;
2019 	struct sge_txq *txq;
2020 	struct sge_wrq *wrq;
2021 #ifdef TCP_OFFLOAD
2022 	struct sge_ofld_rxq *ofld_rxq;
2023 #endif
2024 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2025 	struct sge_ofld_txq *ofld_txq;
2026 #endif
2027 	int rc, i, j, k;
2028 
2029 	/*
2030 	 * XXX: Can there be a synch_op in progress that will hang because
2031 	 * hardware has been stopped?  We'll hang too and the solution will be
2032 	 * to use a version of begin_synch_op that wakes up existing synch_op
2033 	 * with errors.  Maybe stop_adapter should do this wakeup?
2034 	 *
2035 	 * I don't think any synch_op could get stranded waiting for DMA or
2036 	 * interrupt so I think we're okay here.  Remove this comment block
2037 	 * after testing.
2038 	 */
2039 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4slld");
2040 	if (rc != 0)
2041 		return (ENXIO);
2042 
2043 	/* Quiesce all activity. */
2044 	for_each_port(sc, i) {
2045 		pi = sc->port[i];
2046 		pi->vxlan_tcam_entry = false;
2047 		for_each_vi(pi, j, vi) {
2048 			vi->xact_addr_filt = -1;
2049 			mtx_lock(&vi->tick_mtx);
2050 			vi->flags |= VI_SKIP_STATS;
2051 			mtx_unlock(&vi->tick_mtx);
2052 			if (!(vi->flags & VI_INIT_DONE))
2053 				continue;
2054 
2055 			ifp = vi->ifp;
2056 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2057 				mtx_lock(&vi->tick_mtx);
2058 				callout_stop(&vi->tick);
2059 				mtx_unlock(&vi->tick_mtx);
2060 				callout_drain(&vi->tick);
2061 			}
2062 
2063 			/*
2064 			 * Note that the HW is not available.
2065 			 */
2066 			for_each_txq(vi, k, txq) {
2067 				TXQ_LOCK(txq);
2068 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2069 				TXQ_UNLOCK(txq);
2070 			}
2071 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2072 			for_each_ofld_txq(vi, k, ofld_txq) {
2073 				TXQ_LOCK(&ofld_txq->wrq);
2074 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2075 				TXQ_UNLOCK(&ofld_txq->wrq);
2076 			}
2077 #endif
2078 			for_each_rxq(vi, k, rxq) {
2079 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2080 			}
2081 #if defined(TCP_OFFLOAD)
2082 			for_each_ofld_rxq(vi, k, ofld_rxq) {
2083 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2084 			}
2085 #endif
2086 
2087 			quiesce_vi(vi);
2088 		}
2089 
2090 		if (sc->flags & FULL_INIT_DONE) {
2091 			/* Control queue */
2092 			wrq = &sc->sge.ctrlq[i];
2093 			TXQ_LOCK(wrq);
2094 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2095 			TXQ_UNLOCK(wrq);
2096 			quiesce_wrq(wrq);
2097 		}
2098 
2099 		if (pi->flags & HAS_TRACEQ) {
2100 			pi->flags &= ~HAS_TRACEQ;
2101 			sc->traceq = -1;
2102 			sc->tracer_valid = 0;
2103 			sc->tracer_enabled = 0;
2104 		}
2105 	}
2106 	if (sc->flags & FULL_INIT_DONE) {
2107 		/* Firmware event queue */
2108 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2109 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2110 	}
2111 
2112 	/* Stop calibration */
2113 	callout_stop(&sc->cal_callout);
2114 	callout_drain(&sc->cal_callout);
2115 
2116 	if (t4_clock_gate_on_suspend) {
2117 		t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2118 		    F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2119 		    F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2120 	}
2121 
2122 	end_synchronized_op(sc, 0);
2123 
2124 	stop_atid_allocator(sc);
2125 	t4_stop_l2t(sc);
2126 
2127 	return (rc);
2128 }
2129 
2130 int
suspend_adapter(struct adapter * sc)2131 suspend_adapter(struct adapter *sc)
2132 {
2133 	stop_adapter(sc);
2134 	stop_lld(sc);
2135 #ifdef TCP_OFFLOAD
2136 	stop_all_uld(sc);
2137 #endif
2138 	set_adapter_hwstatus(sc, false);
2139 
2140 	return (0);
2141 }
2142 
2143 static int
t4_suspend(device_t dev)2144 t4_suspend(device_t dev)
2145 {
2146 	struct adapter *sc = device_get_softc(dev);
2147 	int rc;
2148 
2149 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2150 	rc = suspend_adapter(sc);
2151 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2152 
2153 	return (rc);
2154 }
2155 
2156 struct adapter_pre_reset_state {
2157 	u_int flags;
2158 	uint16_t nbmcaps;
2159 	uint16_t linkcaps;
2160 	uint16_t switchcaps;
2161 	uint16_t niccaps;
2162 	uint16_t toecaps;
2163 	uint16_t rdmacaps;
2164 	uint16_t cryptocaps;
2165 	uint16_t iscsicaps;
2166 	uint16_t fcoecaps;
2167 
2168 	u_int cfcsum;
2169 	char cfg_file[32];
2170 
2171 	struct adapter_params params;
2172 	struct t4_virt_res vres;
2173 	struct tid_info tids;
2174 	struct sge sge;
2175 
2176 	int rawf_base;
2177 	int nrawf;
2178 
2179 };
2180 
2181 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2182 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2183 {
2184 
2185 	ASSERT_SYNCHRONIZED_OP(sc);
2186 
2187 	o->flags = sc->flags;
2188 
2189 	o->nbmcaps =  sc->nbmcaps;
2190 	o->linkcaps = sc->linkcaps;
2191 	o->switchcaps = sc->switchcaps;
2192 	o->niccaps = sc->niccaps;
2193 	o->toecaps = sc->toecaps;
2194 	o->rdmacaps = sc->rdmacaps;
2195 	o->cryptocaps = sc->cryptocaps;
2196 	o->iscsicaps = sc->iscsicaps;
2197 	o->fcoecaps = sc->fcoecaps;
2198 
2199 	o->cfcsum = sc->cfcsum;
2200 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2201 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2202 
2203 	o->params = sc->params;
2204 	o->vres = sc->vres;
2205 	o->tids = sc->tids;
2206 	o->sge = sc->sge;
2207 
2208 	o->rawf_base = sc->rawf_base;
2209 	o->nrawf = sc->nrawf;
2210 }
2211 
2212 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2213 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2214 {
2215 	int rc = 0;
2216 
2217 	ASSERT_SYNCHRONIZED_OP(sc);
2218 
2219 	/* Capabilities */
2220 #define COMPARE_CAPS(c) do { \
2221 	if (o->c##caps != sc->c##caps) { \
2222 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2223 		    sc->c##caps); \
2224 		rc = EINVAL; \
2225 	} \
2226 } while (0)
2227 	COMPARE_CAPS(nbm);
2228 	COMPARE_CAPS(link);
2229 	COMPARE_CAPS(switch);
2230 	COMPARE_CAPS(nic);
2231 	COMPARE_CAPS(toe);
2232 	COMPARE_CAPS(rdma);
2233 	COMPARE_CAPS(crypto);
2234 	COMPARE_CAPS(iscsi);
2235 	COMPARE_CAPS(fcoe);
2236 #undef COMPARE_CAPS
2237 
2238 	/* Firmware config file */
2239 	if (o->cfcsum != sc->cfcsum) {
2240 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2241 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2242 		rc = EINVAL;
2243 	}
2244 
2245 #define COMPARE_PARAM(p, name) do { \
2246 	if (o->p != sc->p) { \
2247 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2248 		rc = EINVAL; \
2249 	} \
2250 } while (0)
2251 	COMPARE_PARAM(sge.iq_start, iq_start);
2252 	COMPARE_PARAM(sge.eq_start, eq_start);
2253 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2254 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2255 	COMPARE_PARAM(tids.nftids, nftids);
2256 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2257 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2258 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2259 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2260 	COMPARE_PARAM(tids.tid_base, tid_base);
2261 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2262 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2263 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2264 	COMPARE_PARAM(rawf_base, rawf_base);
2265 	COMPARE_PARAM(nrawf, nrawf);
2266 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2267 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2268 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2269 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2270 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2271 	COMPARE_PARAM(tids.ntids, ntids);
2272 	COMPARE_PARAM(tids.etid_base, etid_base);
2273 	COMPARE_PARAM(tids.etid_end, etid_end);
2274 	COMPARE_PARAM(tids.netids, netids);
2275 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2276 	COMPARE_PARAM(params.ethoffload, ethoffload);
2277 	COMPARE_PARAM(tids.natids, natids);
2278 	COMPARE_PARAM(tids.stid_base, stid_base);
2279 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2280 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2281 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2282 	COMPARE_PARAM(vres.stag.start, stag_start);
2283 	COMPARE_PARAM(vres.stag.size, stag_size);
2284 	COMPARE_PARAM(vres.rq.start, rq_start);
2285 	COMPARE_PARAM(vres.rq.size, rq_size);
2286 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2287 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2288 	COMPARE_PARAM(vres.qp.start, qp_start);
2289 	COMPARE_PARAM(vres.qp.size, qp_size);
2290 	COMPARE_PARAM(vres.cq.start, cq_start);
2291 	COMPARE_PARAM(vres.cq.size, cq_size);
2292 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2293 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2294 	COMPARE_PARAM(vres.srq.start, srq_start);
2295 	COMPARE_PARAM(vres.srq.size, srq_size);
2296 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2297 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2298 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2299 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2300 	COMPARE_PARAM(vres.key.start, key_start);
2301 	COMPARE_PARAM(vres.key.size, key_size);
2302 #undef COMPARE_PARAM
2303 
2304 	return (rc);
2305 }
2306 
2307 static int
restart_lld(struct adapter * sc)2308 restart_lld(struct adapter *sc)
2309 {
2310 	struct adapter_pre_reset_state *old_state = NULL;
2311 	struct port_info *pi;
2312 	struct vi_info *vi;
2313 	if_t ifp;
2314 	struct sge_txq *txq;
2315 	int rc, i, j, k;
2316 
2317 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rlld");
2318 	if (rc != 0)
2319 		return (ENXIO);
2320 
2321 	/* Restore memory window. */
2322 	setup_memwin(sc);
2323 
2324 	/* Go no further if recovery mode has been requested. */
2325 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2326 		CH_ALERT(sc, "%s: recovery mode during restart.\n", __func__);
2327 		rc = 0;
2328 		set_adapter_hwstatus(sc, true);
2329 		goto done;
2330 	}
2331 
2332 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2333 	save_caps_and_params(sc, old_state);
2334 
2335 	/* Reestablish contact with firmware and become the primary PF. */
2336 	rc = contact_firmware(sc);
2337 	if (rc != 0)
2338 		goto done; /* error message displayed already */
2339 	MPASS(sc->flags & FW_OK);
2340 
2341 	if (sc->flags & MASTER_PF) {
2342 		rc = partition_resources(sc);
2343 		if (rc != 0)
2344 			goto done; /* error message displayed already */
2345 	}
2346 
2347 	rc = get_params__post_init(sc);
2348 	if (rc != 0)
2349 		goto done; /* error message displayed already */
2350 
2351 	rc = set_params__post_init(sc);
2352 	if (rc != 0)
2353 		goto done; /* error message displayed already */
2354 
2355 	rc = compare_caps_and_params(sc, old_state);
2356 	if (rc != 0)
2357 		goto done; /* error message displayed already */
2358 
2359 	for_each_port(sc, i) {
2360 		pi = sc->port[i];
2361 		MPASS(pi != NULL);
2362 		MPASS(pi->vi != NULL);
2363 		MPASS(pi->vi[0].dev == pi->dev);
2364 
2365 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2366 		if (rc != 0) {
2367 			CH_ERR(sc,
2368 			    "failed to re-initialize port %d: %d\n", i, rc);
2369 			goto done;
2370 		}
2371 		MPASS(sc->chan_map[pi->tx_chan] == i);
2372 
2373 		PORT_LOCK(pi);
2374 		fixup_link_config(pi);
2375 		build_medialist(pi);
2376 		PORT_UNLOCK(pi);
2377 		for_each_vi(pi, j, vi) {
2378 			if (IS_MAIN_VI(vi))
2379 				continue;
2380 			rc = alloc_extra_vi(sc, pi, vi);
2381 			if (rc != 0) {
2382 				CH_ERR(vi,
2383 				    "failed to re-allocate extra VI: %d\n", rc);
2384 				goto done;
2385 			}
2386 		}
2387 	}
2388 
2389 	/*
2390 	 * Interrupts and queues are about to be enabled and other threads will
2391 	 * want to access the hardware too.  It is safe to do so.  Note that
2392 	 * this thread is still in the middle of a synchronized_op.
2393 	 */
2394 	set_adapter_hwstatus(sc, true);
2395 
2396 	if (sc->flags & FULL_INIT_DONE) {
2397 		rc = adapter_full_init(sc);
2398 		if (rc != 0) {
2399 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2400 			goto done;
2401 		}
2402 
2403 		if (sc->vxlan_refcount > 0)
2404 			enable_vxlan_rx(sc);
2405 
2406 		for_each_port(sc, i) {
2407 			pi = sc->port[i];
2408 			for_each_vi(pi, j, vi) {
2409 				mtx_lock(&vi->tick_mtx);
2410 				vi->flags &= ~VI_SKIP_STATS;
2411 				mtx_unlock(&vi->tick_mtx);
2412 				if (!(vi->flags & VI_INIT_DONE))
2413 					continue;
2414 				rc = vi_full_init(vi);
2415 				if (rc != 0) {
2416 					CH_ERR(vi, "failed to re-initialize "
2417 					    "interface: %d\n", rc);
2418 					goto done;
2419 				}
2420 				if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
2421 					sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
2422 					t4_write_reg(sc, is_t4(sc) ?
2423 					    A_MPS_TRC_RSS_CONTROL :
2424 					    A_MPS_T5_TRC_RSS_CONTROL,
2425 					    V_RSSCONTROL(pi->tx_chan) |
2426 					    V_QUEUENUMBER(sc->traceq));
2427 					pi->flags |= HAS_TRACEQ;
2428 				}
2429 
2430 				ifp = vi->ifp;
2431 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2432 					continue;
2433 				/*
2434 				 * Note that we do not setup multicast addresses
2435 				 * in the first pass.  This ensures that the
2436 				 * unicast DMACs for all VIs on all ports get an
2437 				 * MPS TCAM entry.
2438 				 */
2439 				rc = update_mac_settings(ifp, XGMAC_ALL &
2440 				    ~XGMAC_MCADDRS);
2441 				if (rc != 0) {
2442 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2443 					goto done;
2444 				}
2445 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2446 				    true);
2447 				if (rc != 0) {
2448 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2449 					goto done;
2450 				}
2451 				for_each_txq(vi, k, txq) {
2452 					TXQ_LOCK(txq);
2453 					txq->eq.flags |= EQ_ENABLED;
2454 					TXQ_UNLOCK(txq);
2455 				}
2456 				mtx_lock(&vi->tick_mtx);
2457 				callout_schedule(&vi->tick, hz);
2458 				mtx_unlock(&vi->tick_mtx);
2459 			}
2460 			PORT_LOCK(pi);
2461 			if (pi->up_vis > 0) {
2462 				t4_update_port_info(pi);
2463 				fixup_link_config(pi);
2464 				build_medialist(pi);
2465 				apply_link_config(pi);
2466 				if (pi->link_cfg.link_ok)
2467 					t4_os_link_changed(pi);
2468 			}
2469 			PORT_UNLOCK(pi);
2470 		}
2471 
2472 		/* Now reprogram the L2 multicast addresses. */
2473 		for_each_port(sc, i) {
2474 			pi = sc->port[i];
2475 			for_each_vi(pi, j, vi) {
2476 				if (!(vi->flags & VI_INIT_DONE))
2477 					continue;
2478 				ifp = vi->ifp;
2479 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2480 					continue;
2481 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2482 				if (rc != 0) {
2483 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2484 					rc = 0;	/* carry on */
2485 				}
2486 			}
2487 		}
2488 	}
2489 
2490 	/* Reset all calibration */
2491 	t4_calibration_start(sc);
2492 done:
2493 	end_synchronized_op(sc, 0);
2494 	free(old_state, M_CXGBE);
2495 
2496 	restart_atid_allocator(sc);
2497 	t4_restart_l2t(sc);
2498 
2499 	return (rc);
2500 }
2501 
2502 int
resume_adapter(struct adapter * sc)2503 resume_adapter(struct adapter *sc)
2504 {
2505 	restart_adapter(sc);
2506 	restart_lld(sc);
2507 #ifdef TCP_OFFLOAD
2508 	restart_all_uld(sc);
2509 #endif
2510 	return (0);
2511 }
2512 
2513 static int
t4_resume(device_t dev)2514 t4_resume(device_t dev)
2515 {
2516 	struct adapter *sc = device_get_softc(dev);
2517 	int rc;
2518 
2519 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2520 	rc = resume_adapter(sc);
2521 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2522 
2523 	return (rc);
2524 }
2525 
2526 static int
t4_reset_prepare(device_t dev,device_t child)2527 t4_reset_prepare(device_t dev, device_t child)
2528 {
2529 	struct adapter *sc = device_get_softc(dev);
2530 
2531 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2532 	return (0);
2533 }
2534 
2535 static int
t4_reset_post(device_t dev,device_t child)2536 t4_reset_post(device_t dev, device_t child)
2537 {
2538 	struct adapter *sc = device_get_softc(dev);
2539 
2540 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2541 	return (0);
2542 }
2543 
2544 static int
reset_adapter_with_pci_bus_reset(struct adapter * sc)2545 reset_adapter_with_pci_bus_reset(struct adapter *sc)
2546 {
2547 	int rc;
2548 
2549 	mtx_lock(&Giant);
2550 	rc = BUS_RESET_CHILD(device_get_parent(sc->dev), sc->dev, 0);
2551 	mtx_unlock(&Giant);
2552 	return (rc);
2553 }
2554 
2555 static int
reset_adapter_with_pl_rst(struct adapter * sc)2556 reset_adapter_with_pl_rst(struct adapter *sc)
2557 {
2558 	suspend_adapter(sc);
2559 
2560 	/* This is a t4_write_reg without the hw_off_limits check. */
2561 	MPASS(sc->error_flags & HW_OFF_LIMITS);
2562 	bus_space_write_4(sc->bt, sc->bh, A_PL_RST,
2563 			  F_PIORSTMODE | F_PIORST | F_AUTOPCIEPAUSE);
2564 	pause("pl_rst", 1 * hz);		/* Wait 1s for reset */
2565 
2566 	resume_adapter(sc);
2567 
2568 	return (0);
2569 }
2570 
2571 static inline int
reset_adapter(struct adapter * sc)2572 reset_adapter(struct adapter *sc)
2573 {
2574 	if (vm_guest == 0)
2575 		return (reset_adapter_with_pci_bus_reset(sc));
2576 	else
2577 		return (reset_adapter_with_pl_rst(sc));
2578 }
2579 
2580 static void
reset_adapter_task(void * arg,int pending)2581 reset_adapter_task(void *arg, int pending)
2582 {
2583 	struct adapter *sc = arg;
2584 	const int flags = sc->flags;
2585 	const int eflags = sc->error_flags;
2586 	int rc;
2587 
2588 	if (pending > 1)
2589 		CH_ALERT(sc, "%s: pending %d\n", __func__, pending);
2590 	rc = reset_adapter(sc);
2591 	if (rc != 0) {
2592 		CH_ERR(sc, "adapter did not reset properly, rc = %d, "
2593 		       "flags 0x%08x -> 0x%08x, err_flags 0x%08x -> 0x%08x.\n",
2594 		       rc, flags, sc->flags, eflags, sc->error_flags);
2595 	}
2596 }
2597 
2598 static int
cxgbe_probe(device_t dev)2599 cxgbe_probe(device_t dev)
2600 {
2601 	struct port_info *pi = device_get_softc(dev);
2602 
2603 	device_set_descf(dev, "port %d", pi->port_id);
2604 
2605 	return (BUS_PROBE_DEFAULT);
2606 }
2607 
2608 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2609     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2610     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2611     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2612 #define T4_CAP_ENABLE (T4_CAP)
2613 
2614 static void
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2615 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2616 {
2617 	if_t ifp;
2618 	struct sbuf *sb;
2619 	struct sysctl_ctx_list *ctx = &vi->ctx;
2620 	struct sysctl_oid_list *children;
2621 	struct pfil_head_args pa;
2622 	struct adapter *sc = vi->adapter;
2623 
2624 	sysctl_ctx_init(ctx);
2625 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2626 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2627 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2628 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2629 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2630 #ifdef DEV_NETMAP
2631 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2632 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2633 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2634 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2635 #endif
2636 #ifdef TCP_OFFLOAD
2637 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2638 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2639 #endif
2640 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2641 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2642 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2643 #endif
2644 
2645 	vi->xact_addr_filt = -1;
2646 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2647 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2648 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2649 		vi->flags |= TX_USES_VM_WR;
2650 
2651 	/* Allocate an ifnet and set it up */
2652 	ifp = if_alloc_dev(IFT_ETHER, dev);
2653 	vi->ifp = ifp;
2654 	if_setsoftc(ifp, vi);
2655 
2656 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2657 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2658 
2659 	if_setinitfn(ifp, cxgbe_init);
2660 	if_setioctlfn(ifp, cxgbe_ioctl);
2661 	if_settransmitfn(ifp, cxgbe_transmit);
2662 	if_setqflushfn(ifp, cxgbe_qflush);
2663 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2664 		if_setgetcounterfn(ifp, vi_get_counter);
2665 	else
2666 		if_setgetcounterfn(ifp, cxgbe_get_counter);
2667 #if defined(KERN_TLS) || defined(RATELIMIT)
2668 	if_setsndtagallocfn(ifp, cxgbe_snd_tag_alloc);
2669 #endif
2670 #ifdef RATELIMIT
2671 	if_setratelimitqueryfn(ifp, cxgbe_ratelimit_query);
2672 #endif
2673 
2674 	if_setcapabilities(ifp, T4_CAP);
2675 	if_setcapenable(ifp, T4_CAP_ENABLE);
2676 	if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2677 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2678 	if (chip_id(sc) >= CHELSIO_T6) {
2679 		if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2680 		if_setcapenablebit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2681 		if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2682 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2683 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN, 0);
2684 	}
2685 
2686 #ifdef TCP_OFFLOAD
2687 	if (vi->nofldrxq != 0)
2688 		if_setcapabilitiesbit(ifp, IFCAP_TOE, 0);
2689 #endif
2690 #ifdef RATELIMIT
2691 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2692 		if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT, 0);
2693 		if_setcapenablebit(ifp, IFCAP_TXRTLMT, 0);
2694 	}
2695 #endif
2696 
2697 	if_sethwtsomax(ifp, IP_MAXPACKET);
2698 	if (vi->flags & TX_USES_VM_WR)
2699 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_VM_TSO);
2700 	else
2701 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_TSO);
2702 #ifdef RATELIMIT
2703 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2704 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_EO_TSO);
2705 #endif
2706 	if_sethwtsomaxsegsize(ifp, 65536);
2707 #ifdef KERN_TLS
2708 	if (is_ktls(sc)) {
2709 		if_setcapabilitiesbit(ifp, IFCAP_TXTLS, 0);
2710 		if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2711 			if_setcapenablebit(ifp, IFCAP_TXTLS, 0);
2712 	}
2713 #endif
2714 
2715 	ether_ifattach(ifp, vi->hw_addr);
2716 #ifdef DEV_NETMAP
2717 	if (vi->nnmrxq != 0)
2718 		cxgbe_nm_attach(vi);
2719 #endif
2720 	sb = sbuf_new_auto();
2721 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2722 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2723 	switch (if_getcapabilities(ifp) & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2724 	case IFCAP_TOE:
2725 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2726 		break;
2727 	case IFCAP_TOE | IFCAP_TXRTLMT:
2728 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2729 		break;
2730 	case IFCAP_TXRTLMT:
2731 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2732 		break;
2733 	}
2734 #endif
2735 #ifdef TCP_OFFLOAD
2736 	if (if_getcapabilities(ifp) & IFCAP_TOE)
2737 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2738 #endif
2739 #ifdef DEV_NETMAP
2740 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2741 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2742 		    vi->nnmtxq, vi->nnmrxq);
2743 #endif
2744 	sbuf_finish(sb);
2745 	device_printf(dev, "%s\n", sbuf_data(sb));
2746 	sbuf_delete(sb);
2747 
2748 	vi_sysctls(vi);
2749 
2750 	pa.pa_version = PFIL_VERSION;
2751 	pa.pa_flags = PFIL_IN;
2752 	pa.pa_type = PFIL_TYPE_ETHERNET;
2753 	pa.pa_headname = if_name(ifp);
2754 	vi->pfil = pfil_head_register(&pa);
2755 }
2756 
2757 static int
cxgbe_attach(device_t dev)2758 cxgbe_attach(device_t dev)
2759 {
2760 	struct port_info *pi = device_get_softc(dev);
2761 	struct adapter *sc = pi->adapter;
2762 	struct vi_info *vi;
2763 	int i;
2764 
2765 	sysctl_ctx_init(&pi->ctx);
2766 
2767 	cxgbe_vi_attach(dev, &pi->vi[0]);
2768 
2769 	for_each_vi(pi, i, vi) {
2770 		if (i == 0)
2771 			continue;
2772 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, DEVICE_UNIT_ANY);
2773 		if (vi->dev == NULL) {
2774 			device_printf(dev, "failed to add VI %d\n", i);
2775 			continue;
2776 		}
2777 		device_set_softc(vi->dev, vi);
2778 	}
2779 
2780 	cxgbe_sysctls(pi);
2781 
2782 	bus_generic_attach(dev);
2783 
2784 	return (0);
2785 }
2786 
2787 static void
cxgbe_vi_detach(struct vi_info * vi)2788 cxgbe_vi_detach(struct vi_info *vi)
2789 {
2790 	if_t ifp = vi->ifp;
2791 
2792 	if (vi->pfil != NULL) {
2793 		pfil_head_unregister(vi->pfil);
2794 		vi->pfil = NULL;
2795 	}
2796 
2797 	ether_ifdetach(ifp);
2798 
2799 	/* Let detach proceed even if these fail. */
2800 #ifdef DEV_NETMAP
2801 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2802 		cxgbe_nm_detach(vi);
2803 #endif
2804 	cxgbe_uninit_synchronized(vi);
2805 	callout_drain(&vi->tick);
2806 	mtx_destroy(&vi->tick_mtx);
2807 	sysctl_ctx_free(&vi->ctx);
2808 	vi_full_uninit(vi);
2809 
2810 	if_free(vi->ifp);
2811 	vi->ifp = NULL;
2812 }
2813 
2814 static int
cxgbe_detach(device_t dev)2815 cxgbe_detach(device_t dev)
2816 {
2817 	struct port_info *pi = device_get_softc(dev);
2818 	struct adapter *sc = pi->adapter;
2819 	int rc;
2820 
2821 	/* Detach the extra VIs first. */
2822 	rc = bus_generic_detach(dev);
2823 	if (rc)
2824 		return (rc);
2825 	device_delete_children(dev);
2826 
2827 	sysctl_ctx_free(&pi->ctx);
2828 	begin_vi_detach(sc, &pi->vi[0]);
2829 	if (pi->flags & HAS_TRACEQ) {
2830 		sc->traceq = -1;	/* cloner should not create ifnet */
2831 		t4_tracer_port_detach(sc);
2832 	}
2833 	cxgbe_vi_detach(&pi->vi[0]);
2834 	ifmedia_removeall(&pi->media);
2835 	end_vi_detach(sc, &pi->vi[0]);
2836 
2837 	return (0);
2838 }
2839 
2840 static void
cxgbe_init(void * arg)2841 cxgbe_init(void *arg)
2842 {
2843 	struct vi_info *vi = arg;
2844 	struct adapter *sc = vi->adapter;
2845 
2846 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
2847 		return;
2848 	cxgbe_init_synchronized(vi);
2849 	end_synchronized_op(sc, 0);
2850 }
2851 
2852 static int
cxgbe_ioctl(if_t ifp,unsigned long cmd,caddr_t data)2853 cxgbe_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
2854 {
2855 	int rc = 0, mtu, flags;
2856 	struct vi_info *vi = if_getsoftc(ifp);
2857 	struct port_info *pi = vi->pi;
2858 	struct adapter *sc = pi->adapter;
2859 	struct ifreq *ifr = (struct ifreq *)data;
2860 	uint32_t mask;
2861 
2862 	switch (cmd) {
2863 	case SIOCSIFMTU:
2864 		mtu = ifr->ifr_mtu;
2865 		if (mtu < ETHERMIN || mtu > MAX_MTU)
2866 			return (EINVAL);
2867 
2868 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
2869 		if (rc)
2870 			return (rc);
2871 		if_setmtu(ifp, mtu);
2872 		if (vi->flags & VI_INIT_DONE) {
2873 			t4_update_fl_bufsize(ifp);
2874 			if (!hw_off_limits(sc) &&
2875 			    if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2876 				rc = update_mac_settings(ifp, XGMAC_MTU);
2877 		}
2878 		end_synchronized_op(sc, 0);
2879 		break;
2880 
2881 	case SIOCSIFFLAGS:
2882 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
2883 		if (rc)
2884 			return (rc);
2885 
2886 		if (hw_off_limits(sc)) {
2887 			rc = ENXIO;
2888 			goto fail;
2889 		}
2890 
2891 		if (if_getflags(ifp) & IFF_UP) {
2892 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2893 				flags = vi->if_flags;
2894 				if ((if_getflags(ifp) ^ flags) &
2895 				    (IFF_PROMISC | IFF_ALLMULTI)) {
2896 					rc = update_mac_settings(ifp,
2897 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
2898 				}
2899 			} else {
2900 				rc = cxgbe_init_synchronized(vi);
2901 			}
2902 			vi->if_flags = if_getflags(ifp);
2903 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2904 			rc = cxgbe_uninit_synchronized(vi);
2905 		}
2906 		end_synchronized_op(sc, 0);
2907 		break;
2908 
2909 	case SIOCADDMULTI:
2910 	case SIOCDELMULTI:
2911 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
2912 		if (rc)
2913 			return (rc);
2914 		if (!hw_off_limits(sc) && if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2915 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2916 		end_synchronized_op(sc, 0);
2917 		break;
2918 
2919 	case SIOCSIFCAP:
2920 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
2921 		if (rc)
2922 			return (rc);
2923 
2924 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
2925 		if (mask & IFCAP_TXCSUM) {
2926 			if_togglecapenable(ifp, IFCAP_TXCSUM);
2927 			if_togglehwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP);
2928 
2929 			if (IFCAP_TSO4 & if_getcapenable(ifp) &&
2930 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
2931 				mask &= ~IFCAP_TSO4;
2932 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
2933 				if_printf(ifp,
2934 				    "tso4 disabled due to -txcsum.\n");
2935 			}
2936 		}
2937 		if (mask & IFCAP_TXCSUM_IPV6) {
2938 			if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
2939 			if_togglehwassist(ifp, CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2940 
2941 			if (IFCAP_TSO6 & if_getcapenable(ifp) &&
2942 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
2943 				mask &= ~IFCAP_TSO6;
2944 				if_setcapenablebit(ifp, 0, IFCAP_TSO6);
2945 				if_printf(ifp,
2946 				    "tso6 disabled due to -txcsum6.\n");
2947 			}
2948 		}
2949 		if (mask & IFCAP_RXCSUM)
2950 			if_togglecapenable(ifp, IFCAP_RXCSUM);
2951 		if (mask & IFCAP_RXCSUM_IPV6)
2952 			if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
2953 
2954 		/*
2955 		 * Note that we leave CSUM_TSO alone (it is always set).  The
2956 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
2957 		 * sending a TSO request our way, so it's sufficient to toggle
2958 		 * IFCAP_TSOx only.
2959 		 */
2960 		if (mask & IFCAP_TSO4) {
2961 			if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
2962 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
2963 				if_printf(ifp, "enable txcsum first.\n");
2964 				rc = EAGAIN;
2965 				goto fail;
2966 			}
2967 			if_togglecapenable(ifp, IFCAP_TSO4);
2968 		}
2969 		if (mask & IFCAP_TSO6) {
2970 			if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
2971 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
2972 				if_printf(ifp, "enable txcsum6 first.\n");
2973 				rc = EAGAIN;
2974 				goto fail;
2975 			}
2976 			if_togglecapenable(ifp, IFCAP_TSO6);
2977 		}
2978 		if (mask & IFCAP_LRO) {
2979 #if defined(INET) || defined(INET6)
2980 			int i;
2981 			struct sge_rxq *rxq;
2982 
2983 			if_togglecapenable(ifp, IFCAP_LRO);
2984 			for_each_rxq(vi, i, rxq) {
2985 				if (if_getcapenable(ifp) & IFCAP_LRO)
2986 					rxq->iq.flags |= IQ_LRO_ENABLED;
2987 				else
2988 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
2989 			}
2990 #endif
2991 		}
2992 #ifdef TCP_OFFLOAD
2993 		if (mask & IFCAP_TOE) {
2994 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TOE;
2995 
2996 			rc = toe_capability(vi, enable);
2997 			if (rc != 0)
2998 				goto fail;
2999 
3000 			if_togglecapenable(ifp, mask);
3001 		}
3002 #endif
3003 		if (mask & IFCAP_VLAN_HWTAGGING) {
3004 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3005 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3006 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
3007 		}
3008 		if (mask & IFCAP_VLAN_MTU) {
3009 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
3010 
3011 			/* Need to find out how to disable auto-mtu-inflation */
3012 		}
3013 		if (mask & IFCAP_VLAN_HWTSO)
3014 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3015 		if (mask & IFCAP_VLAN_HWCSUM)
3016 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
3017 #ifdef RATELIMIT
3018 		if (mask & IFCAP_TXRTLMT)
3019 			if_togglecapenable(ifp, IFCAP_TXRTLMT);
3020 #endif
3021 		if (mask & IFCAP_HWRXTSTMP) {
3022 			int i;
3023 			struct sge_rxq *rxq;
3024 
3025 			if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3026 			for_each_rxq(vi, i, rxq) {
3027 				if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3028 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
3029 				else
3030 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
3031 			}
3032 		}
3033 		if (mask & IFCAP_MEXTPG)
3034 			if_togglecapenable(ifp, IFCAP_MEXTPG);
3035 
3036 #ifdef KERN_TLS
3037 		if (mask & IFCAP_TXTLS) {
3038 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TXTLS;
3039 
3040 			rc = ktls_capability(sc, enable);
3041 			if (rc != 0)
3042 				goto fail;
3043 
3044 			if_togglecapenable(ifp, mask & IFCAP_TXTLS);
3045 		}
3046 #endif
3047 		if (mask & IFCAP_VXLAN_HWCSUM) {
3048 			if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3049 			if_togglehwassist(ifp, CSUM_INNER_IP6_UDP |
3050 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
3051 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP);
3052 		}
3053 		if (mask & IFCAP_VXLAN_HWTSO) {
3054 			if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3055 			if_togglehwassist(ifp, CSUM_INNER_IP6_TSO |
3056 			    CSUM_INNER_IP_TSO);
3057 		}
3058 
3059 #ifdef VLAN_CAPABILITIES
3060 		VLAN_CAPABILITIES(ifp);
3061 #endif
3062 fail:
3063 		end_synchronized_op(sc, 0);
3064 		break;
3065 
3066 	case SIOCSIFMEDIA:
3067 	case SIOCGIFMEDIA:
3068 	case SIOCGIFXMEDIA:
3069 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3070 		break;
3071 
3072 	case SIOCGI2C: {
3073 		struct ifi2creq i2c;
3074 
3075 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3076 		if (rc != 0)
3077 			break;
3078 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3079 			rc = EPERM;
3080 			break;
3081 		}
3082 		if (i2c.len > sizeof(i2c.data)) {
3083 			rc = EINVAL;
3084 			break;
3085 		}
3086 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3087 		if (rc)
3088 			return (rc);
3089 		if (hw_off_limits(sc))
3090 			rc = ENXIO;
3091 		else
3092 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3093 			    i2c.offset, i2c.len, &i2c.data[0]);
3094 		end_synchronized_op(sc, 0);
3095 		if (rc == 0)
3096 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3097 		break;
3098 	}
3099 
3100 	default:
3101 		rc = ether_ioctl(ifp, cmd, data);
3102 	}
3103 
3104 	return (rc);
3105 }
3106 
3107 static int
cxgbe_transmit(if_t ifp,struct mbuf * m)3108 cxgbe_transmit(if_t ifp, struct mbuf *m)
3109 {
3110 	struct vi_info *vi = if_getsoftc(ifp);
3111 	struct port_info *pi = vi->pi;
3112 	struct adapter *sc;
3113 	struct sge_txq *txq;
3114 	void *items[1];
3115 	int rc;
3116 
3117 	M_ASSERTPKTHDR(m);
3118 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
3119 #if defined(KERN_TLS) || defined(RATELIMIT)
3120 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3121 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3122 #endif
3123 
3124 	if (__predict_false(pi->link_cfg.link_ok == false)) {
3125 		m_freem(m);
3126 		return (ENETDOWN);
3127 	}
3128 
3129 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3130 	if (__predict_false(rc != 0)) {
3131 		if (__predict_true(rc == EINPROGRESS)) {
3132 			/* queued by parse_pkt */
3133 			MPASS(m != NULL);
3134 			return (0);
3135 		}
3136 
3137 		MPASS(m == NULL);			/* was freed already */
3138 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
3139 		return (rc);
3140 	}
3141 
3142 	/* Select a txq. */
3143 	sc = vi->adapter;
3144 	txq = &sc->sge.txq[vi->first_txq];
3145 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3146 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3147 		    vi->rsrv_noflowq);
3148 
3149 	items[0] = m;
3150 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3151 	if (__predict_false(rc != 0))
3152 		m_freem(m);
3153 
3154 	return (rc);
3155 }
3156 
3157 static void
cxgbe_qflush(if_t ifp)3158 cxgbe_qflush(if_t ifp)
3159 {
3160 	struct vi_info *vi = if_getsoftc(ifp);
3161 	struct sge_txq *txq;
3162 	int i;
3163 
3164 	/* queues do not exist if !VI_INIT_DONE. */
3165 	if (vi->flags & VI_INIT_DONE) {
3166 		for_each_txq(vi, i, txq) {
3167 			TXQ_LOCK(txq);
3168 			txq->eq.flags |= EQ_QFLUSH;
3169 			TXQ_UNLOCK(txq);
3170 			while (!mp_ring_is_idle(txq->r)) {
3171 				mp_ring_check_drainage(txq->r, 4096);
3172 				pause("qflush", 1);
3173 			}
3174 			TXQ_LOCK(txq);
3175 			txq->eq.flags &= ~EQ_QFLUSH;
3176 			TXQ_UNLOCK(txq);
3177 		}
3178 	}
3179 	if_qflush(ifp);
3180 }
3181 
3182 static uint64_t
vi_get_counter(if_t ifp,ift_counter c)3183 vi_get_counter(if_t ifp, ift_counter c)
3184 {
3185 	struct vi_info *vi = if_getsoftc(ifp);
3186 	struct fw_vi_stats_vf *s = &vi->stats;
3187 
3188 	mtx_lock(&vi->tick_mtx);
3189 	vi_refresh_stats(vi);
3190 	mtx_unlock(&vi->tick_mtx);
3191 
3192 	switch (c) {
3193 	case IFCOUNTER_IPACKETS:
3194 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3195 		    s->rx_ucast_frames);
3196 	case IFCOUNTER_IERRORS:
3197 		return (s->rx_err_frames);
3198 	case IFCOUNTER_OPACKETS:
3199 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3200 		    s->tx_ucast_frames + s->tx_offload_frames);
3201 	case IFCOUNTER_OERRORS:
3202 		return (s->tx_drop_frames);
3203 	case IFCOUNTER_IBYTES:
3204 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3205 		    s->rx_ucast_bytes);
3206 	case IFCOUNTER_OBYTES:
3207 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3208 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3209 	case IFCOUNTER_IMCASTS:
3210 		return (s->rx_mcast_frames);
3211 	case IFCOUNTER_OMCASTS:
3212 		return (s->tx_mcast_frames);
3213 	case IFCOUNTER_OQDROPS: {
3214 		uint64_t drops;
3215 
3216 		drops = 0;
3217 		if (vi->flags & VI_INIT_DONE) {
3218 			int i;
3219 			struct sge_txq *txq;
3220 
3221 			for_each_txq(vi, i, txq)
3222 				drops += counter_u64_fetch(txq->r->dropped);
3223 		}
3224 
3225 		return (drops);
3226 
3227 	}
3228 
3229 	default:
3230 		return (if_get_counter_default(ifp, c));
3231 	}
3232 }
3233 
3234 static uint64_t
cxgbe_get_counter(if_t ifp,ift_counter c)3235 cxgbe_get_counter(if_t ifp, ift_counter c)
3236 {
3237 	struct vi_info *vi = if_getsoftc(ifp);
3238 	struct port_info *pi = vi->pi;
3239 	struct port_stats *s = &pi->stats;
3240 
3241 	mtx_lock(&vi->tick_mtx);
3242 	cxgbe_refresh_stats(vi);
3243 	mtx_unlock(&vi->tick_mtx);
3244 
3245 	switch (c) {
3246 	case IFCOUNTER_IPACKETS:
3247 		return (s->rx_frames);
3248 
3249 	case IFCOUNTER_IERRORS:
3250 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3251 		    s->rx_fcs_err + s->rx_len_err);
3252 
3253 	case IFCOUNTER_OPACKETS:
3254 		return (s->tx_frames);
3255 
3256 	case IFCOUNTER_OERRORS:
3257 		return (s->tx_error_frames);
3258 
3259 	case IFCOUNTER_IBYTES:
3260 		return (s->rx_octets);
3261 
3262 	case IFCOUNTER_OBYTES:
3263 		return (s->tx_octets);
3264 
3265 	case IFCOUNTER_IMCASTS:
3266 		return (s->rx_mcast_frames);
3267 
3268 	case IFCOUNTER_OMCASTS:
3269 		return (s->tx_mcast_frames);
3270 
3271 	case IFCOUNTER_IQDROPS:
3272 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3273 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3274 		    s->rx_trunc3 + pi->tnl_cong_drops);
3275 
3276 	case IFCOUNTER_OQDROPS: {
3277 		uint64_t drops;
3278 
3279 		drops = s->tx_drop;
3280 		if (vi->flags & VI_INIT_DONE) {
3281 			int i;
3282 			struct sge_txq *txq;
3283 
3284 			for_each_txq(vi, i, txq)
3285 				drops += counter_u64_fetch(txq->r->dropped);
3286 		}
3287 
3288 		return (drops);
3289 
3290 	}
3291 
3292 	default:
3293 		return (if_get_counter_default(ifp, c));
3294 	}
3295 }
3296 
3297 #if defined(KERN_TLS) || defined(RATELIMIT)
3298 static int
cxgbe_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3299 cxgbe_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3300     struct m_snd_tag **pt)
3301 {
3302 	int error;
3303 
3304 	switch (params->hdr.type) {
3305 #ifdef RATELIMIT
3306 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3307 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3308 		break;
3309 #endif
3310 #ifdef KERN_TLS
3311 	case IF_SND_TAG_TYPE_TLS:
3312 	{
3313 		struct vi_info *vi = if_getsoftc(ifp);
3314 
3315 		if (is_t6(vi->pi->adapter))
3316 			error = t6_tls_tag_alloc(ifp, params, pt);
3317 		else
3318 			error = EOPNOTSUPP;
3319 		break;
3320 	}
3321 #endif
3322 	default:
3323 		error = EOPNOTSUPP;
3324 	}
3325 	return (error);
3326 }
3327 #endif
3328 
3329 /*
3330  * The kernel picks a media from the list we had provided but we still validate
3331  * the requeste.
3332  */
3333 int
cxgbe_media_change(if_t ifp)3334 cxgbe_media_change(if_t ifp)
3335 {
3336 	struct vi_info *vi = if_getsoftc(ifp);
3337 	struct port_info *pi = vi->pi;
3338 	struct ifmedia *ifm = &pi->media;
3339 	struct link_config *lc = &pi->link_cfg;
3340 	struct adapter *sc = pi->adapter;
3341 	int rc;
3342 
3343 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3344 	if (rc != 0)
3345 		return (rc);
3346 	PORT_LOCK(pi);
3347 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3348 		/* ifconfig .. media autoselect */
3349 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3350 			rc = ENOTSUP; /* AN not supported by transceiver */
3351 			goto done;
3352 		}
3353 		lc->requested_aneg = AUTONEG_ENABLE;
3354 		lc->requested_speed = 0;
3355 		lc->requested_fc |= PAUSE_AUTONEG;
3356 	} else {
3357 		lc->requested_aneg = AUTONEG_DISABLE;
3358 		lc->requested_speed =
3359 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3360 		lc->requested_fc = 0;
3361 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3362 			lc->requested_fc |= PAUSE_RX;
3363 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3364 			lc->requested_fc |= PAUSE_TX;
3365 	}
3366 	if (pi->up_vis > 0 && !hw_off_limits(sc)) {
3367 		fixup_link_config(pi);
3368 		rc = apply_link_config(pi);
3369 	}
3370 done:
3371 	PORT_UNLOCK(pi);
3372 	end_synchronized_op(sc, 0);
3373 	return (rc);
3374 }
3375 
3376 /*
3377  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3378  * given speed.
3379  */
3380 static int
port_mword(struct port_info * pi,uint32_t speed)3381 port_mword(struct port_info *pi, uint32_t speed)
3382 {
3383 
3384 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3385 	MPASS(powerof2(speed));
3386 
3387 	switch(pi->port_type) {
3388 	case FW_PORT_TYPE_BT_SGMII:
3389 	case FW_PORT_TYPE_BT_XFI:
3390 	case FW_PORT_TYPE_BT_XAUI:
3391 		/* BaseT */
3392 		switch (speed) {
3393 		case FW_PORT_CAP32_SPEED_100M:
3394 			return (IFM_100_T);
3395 		case FW_PORT_CAP32_SPEED_1G:
3396 			return (IFM_1000_T);
3397 		case FW_PORT_CAP32_SPEED_10G:
3398 			return (IFM_10G_T);
3399 		}
3400 		break;
3401 	case FW_PORT_TYPE_KX4:
3402 		if (speed == FW_PORT_CAP32_SPEED_10G)
3403 			return (IFM_10G_KX4);
3404 		break;
3405 	case FW_PORT_TYPE_CX4:
3406 		if (speed == FW_PORT_CAP32_SPEED_10G)
3407 			return (IFM_10G_CX4);
3408 		break;
3409 	case FW_PORT_TYPE_KX:
3410 		if (speed == FW_PORT_CAP32_SPEED_1G)
3411 			return (IFM_1000_KX);
3412 		break;
3413 	case FW_PORT_TYPE_KR:
3414 	case FW_PORT_TYPE_BP_AP:
3415 	case FW_PORT_TYPE_BP4_AP:
3416 	case FW_PORT_TYPE_BP40_BA:
3417 	case FW_PORT_TYPE_KR4_100G:
3418 	case FW_PORT_TYPE_KR_SFP28:
3419 	case FW_PORT_TYPE_KR_XLAUI:
3420 		switch (speed) {
3421 		case FW_PORT_CAP32_SPEED_1G:
3422 			return (IFM_1000_KX);
3423 		case FW_PORT_CAP32_SPEED_10G:
3424 			return (IFM_10G_KR);
3425 		case FW_PORT_CAP32_SPEED_25G:
3426 			return (IFM_25G_KR);
3427 		case FW_PORT_CAP32_SPEED_40G:
3428 			return (IFM_40G_KR4);
3429 		case FW_PORT_CAP32_SPEED_50G:
3430 			return (IFM_50G_KR2);
3431 		case FW_PORT_CAP32_SPEED_100G:
3432 			return (IFM_100G_KR4);
3433 		}
3434 		break;
3435 	case FW_PORT_TYPE_FIBER_XFI:
3436 	case FW_PORT_TYPE_FIBER_XAUI:
3437 	case FW_PORT_TYPE_SFP:
3438 	case FW_PORT_TYPE_QSFP_10G:
3439 	case FW_PORT_TYPE_QSA:
3440 	case FW_PORT_TYPE_QSFP:
3441 	case FW_PORT_TYPE_CR4_QSFP:
3442 	case FW_PORT_TYPE_CR_QSFP:
3443 	case FW_PORT_TYPE_CR2_QSFP:
3444 	case FW_PORT_TYPE_SFP28:
3445 		/* Pluggable transceiver */
3446 		switch (pi->mod_type) {
3447 		case FW_PORT_MOD_TYPE_LR:
3448 			switch (speed) {
3449 			case FW_PORT_CAP32_SPEED_1G:
3450 				return (IFM_1000_LX);
3451 			case FW_PORT_CAP32_SPEED_10G:
3452 				return (IFM_10G_LR);
3453 			case FW_PORT_CAP32_SPEED_25G:
3454 				return (IFM_25G_LR);
3455 			case FW_PORT_CAP32_SPEED_40G:
3456 				return (IFM_40G_LR4);
3457 			case FW_PORT_CAP32_SPEED_50G:
3458 				return (IFM_50G_LR2);
3459 			case FW_PORT_CAP32_SPEED_100G:
3460 				return (IFM_100G_LR4);
3461 			}
3462 			break;
3463 		case FW_PORT_MOD_TYPE_SR:
3464 			switch (speed) {
3465 			case FW_PORT_CAP32_SPEED_1G:
3466 				return (IFM_1000_SX);
3467 			case FW_PORT_CAP32_SPEED_10G:
3468 				return (IFM_10G_SR);
3469 			case FW_PORT_CAP32_SPEED_25G:
3470 				return (IFM_25G_SR);
3471 			case FW_PORT_CAP32_SPEED_40G:
3472 				return (IFM_40G_SR4);
3473 			case FW_PORT_CAP32_SPEED_50G:
3474 				return (IFM_50G_SR2);
3475 			case FW_PORT_CAP32_SPEED_100G:
3476 				return (IFM_100G_SR4);
3477 			}
3478 			break;
3479 		case FW_PORT_MOD_TYPE_ER:
3480 			if (speed == FW_PORT_CAP32_SPEED_10G)
3481 				return (IFM_10G_ER);
3482 			break;
3483 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3484 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3485 			switch (speed) {
3486 			case FW_PORT_CAP32_SPEED_1G:
3487 				return (IFM_1000_CX);
3488 			case FW_PORT_CAP32_SPEED_10G:
3489 				return (IFM_10G_TWINAX);
3490 			case FW_PORT_CAP32_SPEED_25G:
3491 				return (IFM_25G_CR);
3492 			case FW_PORT_CAP32_SPEED_40G:
3493 				return (IFM_40G_CR4);
3494 			case FW_PORT_CAP32_SPEED_50G:
3495 				return (IFM_50G_CR2);
3496 			case FW_PORT_CAP32_SPEED_100G:
3497 				return (IFM_100G_CR4);
3498 			}
3499 			break;
3500 		case FW_PORT_MOD_TYPE_LRM:
3501 			if (speed == FW_PORT_CAP32_SPEED_10G)
3502 				return (IFM_10G_LRM);
3503 			break;
3504 		case FW_PORT_MOD_TYPE_NA:
3505 			MPASS(0);	/* Not pluggable? */
3506 			/* fall throough */
3507 		case FW_PORT_MOD_TYPE_ERROR:
3508 		case FW_PORT_MOD_TYPE_UNKNOWN:
3509 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3510 			break;
3511 		case FW_PORT_MOD_TYPE_NONE:
3512 			return (IFM_NONE);
3513 		}
3514 		break;
3515 	case FW_PORT_TYPE_NONE:
3516 		return (IFM_NONE);
3517 	}
3518 
3519 	return (IFM_UNKNOWN);
3520 }
3521 
3522 void
cxgbe_media_status(if_t ifp,struct ifmediareq * ifmr)3523 cxgbe_media_status(if_t ifp, struct ifmediareq *ifmr)
3524 {
3525 	struct vi_info *vi = if_getsoftc(ifp);
3526 	struct port_info *pi = vi->pi;
3527 	struct adapter *sc = pi->adapter;
3528 	struct link_config *lc = &pi->link_cfg;
3529 
3530 	if (begin_synchronized_op(sc, vi , SLEEP_OK | INTR_OK, "t4med") != 0)
3531 		return;
3532 	PORT_LOCK(pi);
3533 
3534 	if (pi->up_vis == 0 && !hw_off_limits(sc)) {
3535 		/*
3536 		 * If all the interfaces are administratively down the firmware
3537 		 * does not report transceiver changes.  Refresh port info here
3538 		 * so that ifconfig displays accurate ifmedia at all times.
3539 		 * This is the only reason we have a synchronized op in this
3540 		 * function.  Just PORT_LOCK would have been enough otherwise.
3541 		 */
3542 		t4_update_port_info(pi);
3543 		build_medialist(pi);
3544 	}
3545 
3546 	/* ifm_status */
3547 	ifmr->ifm_status = IFM_AVALID;
3548 	if (lc->link_ok == false)
3549 		goto done;
3550 	ifmr->ifm_status |= IFM_ACTIVE;
3551 
3552 	/* ifm_active */
3553 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3554 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3555 	if (lc->fc & PAUSE_RX)
3556 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3557 	if (lc->fc & PAUSE_TX)
3558 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3559 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3560 done:
3561 	PORT_UNLOCK(pi);
3562 	end_synchronized_op(sc, 0);
3563 }
3564 
3565 static int
vcxgbe_probe(device_t dev)3566 vcxgbe_probe(device_t dev)
3567 {
3568 	struct vi_info *vi = device_get_softc(dev);
3569 
3570 	device_set_descf(dev, "port %d vi %td", vi->pi->port_id,
3571 	    vi - vi->pi->vi);
3572 
3573 	return (BUS_PROBE_DEFAULT);
3574 }
3575 
3576 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3577 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3578 {
3579 	int func, index, rc;
3580 	uint32_t param, val;
3581 
3582 	ASSERT_SYNCHRONIZED_OP(sc);
3583 
3584 	index = vi - pi->vi;
3585 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3586 	KASSERT(index < nitems(vi_mac_funcs),
3587 	    ("%s: VI %s doesn't have a MAC func", __func__,
3588 	    device_get_nameunit(vi->dev)));
3589 	func = vi_mac_funcs[index];
3590 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
3591 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3592 	if (rc < 0) {
3593 		CH_ERR(vi, "failed to allocate virtual interface %d"
3594 		    "for port %d: %d\n", index, pi->port_id, -rc);
3595 		return (-rc);
3596 	}
3597 	vi->viid = rc;
3598 
3599 	if (vi->rss_size == 1) {
3600 		/*
3601 		 * This VI didn't get a slice of the RSS table.  Reduce the
3602 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3603 		 * configuration file (nvi, rssnvi for this PF) if this is a
3604 		 * problem.
3605 		 */
3606 		device_printf(vi->dev, "RSS table not available.\n");
3607 		vi->rss_base = 0xffff;
3608 
3609 		return (0);
3610 	}
3611 
3612 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3613 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3614 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3615 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3616 	if (rc)
3617 		vi->rss_base = 0xffff;
3618 	else {
3619 		MPASS((val >> 16) == vi->rss_size);
3620 		vi->rss_base = val & 0xffff;
3621 	}
3622 
3623 	return (0);
3624 }
3625 
3626 static int
vcxgbe_attach(device_t dev)3627 vcxgbe_attach(device_t dev)
3628 {
3629 	struct vi_info *vi;
3630 	struct port_info *pi;
3631 	struct adapter *sc;
3632 	int rc;
3633 
3634 	vi = device_get_softc(dev);
3635 	pi = vi->pi;
3636 	sc = pi->adapter;
3637 
3638 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3639 	if (rc)
3640 		return (rc);
3641 	rc = alloc_extra_vi(sc, pi, vi);
3642 	end_synchronized_op(sc, 0);
3643 	if (rc)
3644 		return (rc);
3645 
3646 	cxgbe_vi_attach(dev, vi);
3647 
3648 	return (0);
3649 }
3650 
3651 static int
vcxgbe_detach(device_t dev)3652 vcxgbe_detach(device_t dev)
3653 {
3654 	struct vi_info *vi;
3655 	struct adapter *sc;
3656 
3657 	vi = device_get_softc(dev);
3658 	sc = vi->adapter;
3659 
3660 	begin_vi_detach(sc, vi);
3661 	cxgbe_vi_detach(vi);
3662 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3663 	end_vi_detach(sc, vi);
3664 
3665 	return (0);
3666 }
3667 
3668 static struct callout fatal_callout;
3669 static struct taskqueue *reset_tq;
3670 
3671 static void
delayed_panic(void * arg)3672 delayed_panic(void *arg)
3673 {
3674 	struct adapter *sc = arg;
3675 
3676 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3677 }
3678 
3679 static void
fatal_error_task(void * arg,int pending)3680 fatal_error_task(void *arg, int pending)
3681 {
3682 	struct adapter *sc = arg;
3683 	int rc;
3684 
3685 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3686 		dump_cim_regs(sc);
3687 		dump_cimla(sc);
3688 		dump_devlog(sc);
3689 	}
3690 
3691 	if (t4_reset_on_fatal_err) {
3692 		CH_ALERT(sc, "resetting adapter after fatal error.\n");
3693 		rc = reset_adapter(sc);
3694 		if (rc == 0 && t4_panic_on_fatal_err) {
3695 			CH_ALERT(sc, "reset was successful, "
3696 			    "system will NOT panic.\n");
3697 			return;
3698 		}
3699 	}
3700 
3701 	if (t4_panic_on_fatal_err) {
3702 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3703 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3704 	}
3705 }
3706 
3707 void
t4_fatal_err(struct adapter * sc,bool fw_error)3708 t4_fatal_err(struct adapter *sc, bool fw_error)
3709 {
3710 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3711 
3712 	stop_adapter(sc);
3713 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3714 		return;
3715 	if (fw_error) {
3716 		/*
3717 		 * We are here because of a firmware error/timeout and not
3718 		 * because of a hardware interrupt.  It is possible (although
3719 		 * not very likely) that an error interrupt was also raised but
3720 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3721 		 * main INT_CAUSE registers here to make sure we haven't missed
3722 		 * anything interesting.
3723 		 */
3724 		t4_slow_intr_handler(sc, verbose);
3725 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3726 	}
3727 	t4_report_fw_error(sc);
3728 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3729 	    device_get_nameunit(sc->dev), fw_error);
3730 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3731 }
3732 
3733 void
t4_add_adapter(struct adapter * sc)3734 t4_add_adapter(struct adapter *sc)
3735 {
3736 	sx_xlock(&t4_list_lock);
3737 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3738 	sx_xunlock(&t4_list_lock);
3739 }
3740 
3741 int
t4_map_bars_0_and_4(struct adapter * sc)3742 t4_map_bars_0_and_4(struct adapter *sc)
3743 {
3744 	sc->regs_rid = PCIR_BAR(0);
3745 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3746 	    &sc->regs_rid, RF_ACTIVE);
3747 	if (sc->regs_res == NULL) {
3748 		device_printf(sc->dev, "cannot map registers.\n");
3749 		return (ENXIO);
3750 	}
3751 	sc->bt = rman_get_bustag(sc->regs_res);
3752 	sc->bh = rman_get_bushandle(sc->regs_res);
3753 	sc->mmio_len = rman_get_size(sc->regs_res);
3754 	setbit(&sc->doorbells, DOORBELL_KDB);
3755 
3756 	sc->msix_rid = PCIR_BAR(4);
3757 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3758 	    &sc->msix_rid, RF_ACTIVE);
3759 	if (sc->msix_res == NULL) {
3760 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3761 		return (ENXIO);
3762 	}
3763 
3764 	return (0);
3765 }
3766 
3767 int
t4_map_bar_2(struct adapter * sc)3768 t4_map_bar_2(struct adapter *sc)
3769 {
3770 
3771 	/*
3772 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
3773 	 * to map it if RDMA is disabled.
3774 	 */
3775 	if (is_t4(sc) && sc->rdmacaps == 0)
3776 		return (0);
3777 
3778 	sc->udbs_rid = PCIR_BAR(2);
3779 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3780 	    &sc->udbs_rid, RF_ACTIVE);
3781 	if (sc->udbs_res == NULL) {
3782 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
3783 		return (ENXIO);
3784 	}
3785 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
3786 
3787 	if (chip_id(sc) >= CHELSIO_T5) {
3788 		setbit(&sc->doorbells, DOORBELL_UDB);
3789 #if defined(__i386__) || defined(__amd64__)
3790 		if (t5_write_combine) {
3791 			int rc, mode;
3792 
3793 			/*
3794 			 * Enable write combining on BAR2.  This is the
3795 			 * userspace doorbell BAR and is split into 128B
3796 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
3797 			 * with an egress queue.  The first 64B has the doorbell
3798 			 * and the second 64B can be used to submit a tx work
3799 			 * request with an implicit doorbell.
3800 			 */
3801 
3802 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
3803 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
3804 			if (rc == 0) {
3805 				clrbit(&sc->doorbells, DOORBELL_UDB);
3806 				setbit(&sc->doorbells, DOORBELL_WCWR);
3807 				setbit(&sc->doorbells, DOORBELL_UDBWC);
3808 			} else {
3809 				device_printf(sc->dev,
3810 				    "couldn't enable write combining: %d\n",
3811 				    rc);
3812 			}
3813 
3814 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
3815 			t4_write_reg(sc, A_SGE_STAT_CFG,
3816 			    V_STATSOURCE_T5(7) | mode);
3817 		}
3818 #endif
3819 	}
3820 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
3821 
3822 	return (0);
3823 }
3824 
3825 int
t4_adj_doorbells(struct adapter * sc)3826 t4_adj_doorbells(struct adapter *sc)
3827 {
3828 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
3829 		sc->doorbells &= t4_doorbells_allowed;
3830 		return (0);
3831 	}
3832 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
3833 	       sc->doorbells, t4_doorbells_allowed);
3834 	return (EINVAL);
3835 }
3836 
3837 struct memwin_init {
3838 	uint32_t base;
3839 	uint32_t aperture;
3840 };
3841 
3842 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
3843 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3844 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3845 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
3846 };
3847 
3848 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
3849 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3850 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3851 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
3852 };
3853 
3854 static void
setup_memwin(struct adapter * sc)3855 setup_memwin(struct adapter *sc)
3856 {
3857 	const struct memwin_init *mw_init;
3858 	struct memwin *mw;
3859 	int i;
3860 	uint32_t bar0;
3861 
3862 	if (is_t4(sc)) {
3863 		/*
3864 		 * Read low 32b of bar0 indirectly via the hardware backdoor
3865 		 * mechanism.  Works from within PCI passthrough environments
3866 		 * too, where rman_get_start() can return a different value.  We
3867 		 * need to program the T4 memory window decoders with the actual
3868 		 * addresses that will be coming across the PCIe link.
3869 		 */
3870 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
3871 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
3872 
3873 		mw_init = &t4_memwin[0];
3874 	} else {
3875 		/* T5+ use the relative offset inside the PCIe BAR */
3876 		bar0 = 0;
3877 
3878 		mw_init = &t5_memwin[0];
3879 	}
3880 
3881 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
3882 		if (!rw_initialized(&mw->mw_lock)) {
3883 			rw_init(&mw->mw_lock, "memory window access");
3884 			mw->mw_base = mw_init->base;
3885 			mw->mw_aperture = mw_init->aperture;
3886 			mw->mw_curpos = 0;
3887 		}
3888 		t4_write_reg(sc,
3889 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
3890 		    (mw->mw_base + bar0) | V_BIR(0) |
3891 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
3892 		rw_wlock(&mw->mw_lock);
3893 		position_memwin(sc, i, mw->mw_curpos);
3894 		rw_wunlock(&mw->mw_lock);
3895 	}
3896 
3897 	/* flush */
3898 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
3899 }
3900 
3901 /*
3902  * Positions the memory window at the given address in the card's address space.
3903  * There are some alignment requirements and the actual position may be at an
3904  * address prior to the requested address.  mw->mw_curpos always has the actual
3905  * position of the window.
3906  */
3907 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)3908 position_memwin(struct adapter *sc, int idx, uint32_t addr)
3909 {
3910 	struct memwin *mw;
3911 	uint32_t pf;
3912 	uint32_t reg;
3913 
3914 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3915 	mw = &sc->memwin[idx];
3916 	rw_assert(&mw->mw_lock, RA_WLOCKED);
3917 
3918 	if (is_t4(sc)) {
3919 		pf = 0;
3920 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
3921 	} else {
3922 		pf = V_PFNUM(sc->pf);
3923 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
3924 	}
3925 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
3926 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
3927 	t4_read_reg(sc, reg);	/* flush */
3928 }
3929 
3930 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)3931 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
3932     int len, int rw)
3933 {
3934 	struct memwin *mw;
3935 	uint32_t mw_end, v;
3936 
3937 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3938 
3939 	/* Memory can only be accessed in naturally aligned 4 byte units */
3940 	if (addr & 3 || len & 3 || len <= 0)
3941 		return (EINVAL);
3942 
3943 	mw = &sc->memwin[idx];
3944 	while (len > 0) {
3945 		rw_rlock(&mw->mw_lock);
3946 		mw_end = mw->mw_curpos + mw->mw_aperture;
3947 		if (addr >= mw_end || addr < mw->mw_curpos) {
3948 			/* Will need to reposition the window */
3949 			if (!rw_try_upgrade(&mw->mw_lock)) {
3950 				rw_runlock(&mw->mw_lock);
3951 				rw_wlock(&mw->mw_lock);
3952 			}
3953 			rw_assert(&mw->mw_lock, RA_WLOCKED);
3954 			position_memwin(sc, idx, addr);
3955 			rw_downgrade(&mw->mw_lock);
3956 			mw_end = mw->mw_curpos + mw->mw_aperture;
3957 		}
3958 		rw_assert(&mw->mw_lock, RA_RLOCKED);
3959 		while (addr < mw_end && len > 0) {
3960 			if (rw == 0) {
3961 				v = t4_read_reg(sc, mw->mw_base + addr -
3962 				    mw->mw_curpos);
3963 				*val++ = le32toh(v);
3964 			} else {
3965 				v = *val++;
3966 				t4_write_reg(sc, mw->mw_base + addr -
3967 				    mw->mw_curpos, htole32(v));
3968 			}
3969 			addr += 4;
3970 			len -= 4;
3971 		}
3972 		rw_runlock(&mw->mw_lock);
3973 	}
3974 
3975 	return (0);
3976 }
3977 
3978 CTASSERT(M_TID_COOKIE == M_COOKIE);
3979 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
3980 
3981 static void
t4_init_atid_table(struct adapter * sc)3982 t4_init_atid_table(struct adapter *sc)
3983 {
3984 	struct tid_info *t;
3985 	int i;
3986 
3987 	t = &sc->tids;
3988 	if (t->natids == 0)
3989 		return;
3990 
3991 	MPASS(t->atid_tab == NULL);
3992 
3993 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
3994 	    M_ZERO | M_WAITOK);
3995 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
3996 	t->afree = t->atid_tab;
3997 	t->atids_in_use = 0;
3998 	t->atid_alloc_stopped = false;
3999 	for (i = 1; i < t->natids; i++)
4000 		t->atid_tab[i - 1].next = &t->atid_tab[i];
4001 	t->atid_tab[t->natids - 1].next = NULL;
4002 }
4003 
4004 static void
t4_free_atid_table(struct adapter * sc)4005 t4_free_atid_table(struct adapter *sc)
4006 {
4007 	struct tid_info *t;
4008 
4009 	t = &sc->tids;
4010 
4011 	KASSERT(t->atids_in_use == 0,
4012 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4013 
4014 	if (mtx_initialized(&t->atid_lock))
4015 		mtx_destroy(&t->atid_lock);
4016 	free(t->atid_tab, M_CXGBE);
4017 	t->atid_tab = NULL;
4018 }
4019 
4020 static void
stop_atid_allocator(struct adapter * sc)4021 stop_atid_allocator(struct adapter *sc)
4022 {
4023 	struct tid_info *t = &sc->tids;
4024 
4025 	mtx_lock(&t->atid_lock);
4026 	t->atid_alloc_stopped = true;
4027 	mtx_unlock(&t->atid_lock);
4028 }
4029 
4030 static void
restart_atid_allocator(struct adapter * sc)4031 restart_atid_allocator(struct adapter *sc)
4032 {
4033 	struct tid_info *t = &sc->tids;
4034 
4035 	mtx_lock(&t->atid_lock);
4036 	KASSERT(t->atids_in_use == 0,
4037 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4038 	t->atid_alloc_stopped = false;
4039 	mtx_unlock(&t->atid_lock);
4040 }
4041 
4042 int
alloc_atid(struct adapter * sc,void * ctx)4043 alloc_atid(struct adapter *sc, void *ctx)
4044 {
4045 	struct tid_info *t = &sc->tids;
4046 	int atid = -1;
4047 
4048 	mtx_lock(&t->atid_lock);
4049 	if (t->afree && !t->atid_alloc_stopped) {
4050 		union aopen_entry *p = t->afree;
4051 
4052 		atid = p - t->atid_tab;
4053 		MPASS(atid <= M_TID_TID);
4054 		t->afree = p->next;
4055 		p->data = ctx;
4056 		t->atids_in_use++;
4057 	}
4058 	mtx_unlock(&t->atid_lock);
4059 	return (atid);
4060 }
4061 
4062 void *
lookup_atid(struct adapter * sc,int atid)4063 lookup_atid(struct adapter *sc, int atid)
4064 {
4065 	struct tid_info *t = &sc->tids;
4066 
4067 	return (t->atid_tab[atid].data);
4068 }
4069 
4070 void
free_atid(struct adapter * sc,int atid)4071 free_atid(struct adapter *sc, int atid)
4072 {
4073 	struct tid_info *t = &sc->tids;
4074 	union aopen_entry *p = &t->atid_tab[atid];
4075 
4076 	mtx_lock(&t->atid_lock);
4077 	p->next = t->afree;
4078 	t->afree = p;
4079 	t->atids_in_use--;
4080 	mtx_unlock(&t->atid_lock);
4081 }
4082 
4083 static void
queue_tid_release(struct adapter * sc,int tid)4084 queue_tid_release(struct adapter *sc, int tid)
4085 {
4086 
4087 	CXGBE_UNIMPLEMENTED("deferred tid release");
4088 }
4089 
4090 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4091 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4092 {
4093 	struct wrqe *wr;
4094 	struct cpl_tid_release *req;
4095 
4096 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4097 	if (wr == NULL) {
4098 		queue_tid_release(sc, tid);	/* defer */
4099 		return;
4100 	}
4101 	req = wrtod(wr);
4102 
4103 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4104 
4105 	t4_wrq_tx(sc, wr);
4106 }
4107 
4108 static int
t4_range_cmp(const void * a,const void * b)4109 t4_range_cmp(const void *a, const void *b)
4110 {
4111 	return ((const struct t4_range *)a)->start -
4112 	       ((const struct t4_range *)b)->start;
4113 }
4114 
4115 /*
4116  * Verify that the memory range specified by the addr/len pair is valid within
4117  * the card's address space.
4118  */
4119 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4120 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4121 {
4122 	struct t4_range mem_ranges[4], *r, *next;
4123 	uint32_t em, addr_len;
4124 	int i, n, remaining;
4125 
4126 	/* Memory can only be accessed in naturally aligned 4 byte units */
4127 	if (addr & 3 || len & 3 || len == 0)
4128 		return (EINVAL);
4129 
4130 	/* Enabled memories */
4131 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4132 
4133 	r = &mem_ranges[0];
4134 	n = 0;
4135 	bzero(r, sizeof(mem_ranges));
4136 	if (em & F_EDRAM0_ENABLE) {
4137 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4138 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4139 		if (r->size > 0) {
4140 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4141 			if (addr >= r->start &&
4142 			    addr + len <= r->start + r->size)
4143 				return (0);
4144 			r++;
4145 			n++;
4146 		}
4147 	}
4148 	if (em & F_EDRAM1_ENABLE) {
4149 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4150 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4151 		if (r->size > 0) {
4152 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4153 			if (addr >= r->start &&
4154 			    addr + len <= r->start + r->size)
4155 				return (0);
4156 			r++;
4157 			n++;
4158 		}
4159 	}
4160 	if (em & F_EXT_MEM_ENABLE) {
4161 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4162 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4163 		if (r->size > 0) {
4164 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4165 			if (addr >= r->start &&
4166 			    addr + len <= r->start + r->size)
4167 				return (0);
4168 			r++;
4169 			n++;
4170 		}
4171 	}
4172 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4173 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4174 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4175 		if (r->size > 0) {
4176 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4177 			if (addr >= r->start &&
4178 			    addr + len <= r->start + r->size)
4179 				return (0);
4180 			r++;
4181 			n++;
4182 		}
4183 	}
4184 	MPASS(n <= nitems(mem_ranges));
4185 
4186 	if (n > 1) {
4187 		/* Sort and merge the ranges. */
4188 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4189 
4190 		/* Start from index 0 and examine the next n - 1 entries. */
4191 		r = &mem_ranges[0];
4192 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4193 
4194 			MPASS(r->size > 0);	/* r is a valid entry. */
4195 			next = r + 1;
4196 			MPASS(next->size > 0);	/* and so is the next one. */
4197 
4198 			while (r->start + r->size >= next->start) {
4199 				/* Merge the next one into the current entry. */
4200 				r->size = max(r->start + r->size,
4201 				    next->start + next->size) - r->start;
4202 				n--;	/* One fewer entry in total. */
4203 				if (--remaining == 0)
4204 					goto done;	/* short circuit */
4205 				next++;
4206 			}
4207 			if (next != r + 1) {
4208 				/*
4209 				 * Some entries were merged into r and next
4210 				 * points to the first valid entry that couldn't
4211 				 * be merged.
4212 				 */
4213 				MPASS(next->size > 0);	/* must be valid */
4214 				memcpy(r + 1, next, remaining * sizeof(*r));
4215 #ifdef INVARIANTS
4216 				/*
4217 				 * This so that the foo->size assertion in the
4218 				 * next iteration of the loop do the right
4219 				 * thing for entries that were pulled up and are
4220 				 * no longer valid.
4221 				 */
4222 				MPASS(n < nitems(mem_ranges));
4223 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4224 				    sizeof(struct t4_range));
4225 #endif
4226 			}
4227 		}
4228 done:
4229 		/* Done merging the ranges. */
4230 		MPASS(n > 0);
4231 		r = &mem_ranges[0];
4232 		for (i = 0; i < n; i++, r++) {
4233 			if (addr >= r->start &&
4234 			    addr + len <= r->start + r->size)
4235 				return (0);
4236 		}
4237 	}
4238 
4239 	return (EFAULT);
4240 }
4241 
4242 static int
fwmtype_to_hwmtype(int mtype)4243 fwmtype_to_hwmtype(int mtype)
4244 {
4245 
4246 	switch (mtype) {
4247 	case FW_MEMTYPE_EDC0:
4248 		return (MEM_EDC0);
4249 	case FW_MEMTYPE_EDC1:
4250 		return (MEM_EDC1);
4251 	case FW_MEMTYPE_EXTMEM:
4252 		return (MEM_MC0);
4253 	case FW_MEMTYPE_EXTMEM1:
4254 		return (MEM_MC1);
4255 	default:
4256 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4257 	}
4258 }
4259 
4260 /*
4261  * Verify that the memory range specified by the memtype/offset/len pair is
4262  * valid and lies entirely within the memtype specified.  The global address of
4263  * the start of the range is returned in addr.
4264  */
4265 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4266 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4267     uint32_t *addr)
4268 {
4269 	uint32_t em, addr_len, maddr;
4270 
4271 	/* Memory can only be accessed in naturally aligned 4 byte units */
4272 	if (off & 3 || len & 3 || len == 0)
4273 		return (EINVAL);
4274 
4275 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4276 	switch (fwmtype_to_hwmtype(mtype)) {
4277 	case MEM_EDC0:
4278 		if (!(em & F_EDRAM0_ENABLE))
4279 			return (EINVAL);
4280 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4281 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4282 		break;
4283 	case MEM_EDC1:
4284 		if (!(em & F_EDRAM1_ENABLE))
4285 			return (EINVAL);
4286 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4287 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4288 		break;
4289 	case MEM_MC:
4290 		if (!(em & F_EXT_MEM_ENABLE))
4291 			return (EINVAL);
4292 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4293 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4294 		break;
4295 	case MEM_MC1:
4296 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4297 			return (EINVAL);
4298 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4299 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4300 		break;
4301 	default:
4302 		return (EINVAL);
4303 	}
4304 
4305 	*addr = maddr + off;	/* global address */
4306 	return (validate_mem_range(sc, *addr, len));
4307 }
4308 
4309 static int
fixup_devlog_params(struct adapter * sc)4310 fixup_devlog_params(struct adapter *sc)
4311 {
4312 	struct devlog_params *dparams = &sc->params.devlog;
4313 	int rc;
4314 
4315 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4316 	    dparams->size, &dparams->addr);
4317 
4318 	return (rc);
4319 }
4320 
4321 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4322 update_nirq(struct intrs_and_queues *iaq, int nports)
4323 {
4324 
4325 	iaq->nirq = T4_EXTRA_INTR;
4326 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4327 	iaq->nirq += nports * iaq->nofldrxq;
4328 	iaq->nirq += nports * (iaq->num_vis - 1) *
4329 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4330 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4331 }
4332 
4333 /*
4334  * Adjust requirements to fit the number of interrupts available.
4335  */
4336 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4337 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4338     int navail)
4339 {
4340 	int old_nirq;
4341 	const int nports = sc->params.nports;
4342 
4343 	MPASS(nports > 0);
4344 	MPASS(navail > 0);
4345 
4346 	bzero(iaq, sizeof(*iaq));
4347 	iaq->intr_type = itype;
4348 	iaq->num_vis = t4_num_vis;
4349 	iaq->ntxq = t4_ntxq;
4350 	iaq->ntxq_vi = t4_ntxq_vi;
4351 	iaq->nrxq = t4_nrxq;
4352 	iaq->nrxq_vi = t4_nrxq_vi;
4353 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4354 	if (is_offload(sc) || is_ethoffload(sc)) {
4355 		iaq->nofldtxq = t4_nofldtxq;
4356 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
4357 	}
4358 #endif
4359 #ifdef TCP_OFFLOAD
4360 	if (is_offload(sc)) {
4361 		iaq->nofldrxq = t4_nofldrxq;
4362 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4363 	}
4364 #endif
4365 #ifdef DEV_NETMAP
4366 	if (t4_native_netmap & NN_MAIN_VI) {
4367 		iaq->nnmtxq = t4_nnmtxq;
4368 		iaq->nnmrxq = t4_nnmrxq;
4369 	}
4370 	if (t4_native_netmap & NN_EXTRA_VI) {
4371 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4372 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4373 	}
4374 #endif
4375 
4376 	update_nirq(iaq, nports);
4377 	if (iaq->nirq <= navail &&
4378 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4379 		/*
4380 		 * This is the normal case -- there are enough interrupts for
4381 		 * everything.
4382 		 */
4383 		goto done;
4384 	}
4385 
4386 	/*
4387 	 * If extra VIs have been configured try reducing their count and see if
4388 	 * that works.
4389 	 */
4390 	while (iaq->num_vis > 1) {
4391 		iaq->num_vis--;
4392 		update_nirq(iaq, nports);
4393 		if (iaq->nirq <= navail &&
4394 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4395 			device_printf(sc->dev, "virtual interfaces per port "
4396 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4397 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4398 			    "itype %d, navail %u, nirq %d.\n",
4399 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4400 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4401 			    itype, navail, iaq->nirq);
4402 			goto done;
4403 		}
4404 	}
4405 
4406 	/*
4407 	 * Extra VIs will not be created.  Log a message if they were requested.
4408 	 */
4409 	MPASS(iaq->num_vis == 1);
4410 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4411 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4412 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4413 	if (iaq->num_vis != t4_num_vis) {
4414 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4415 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4416 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4417 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4418 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4419 	}
4420 
4421 	/*
4422 	 * Keep reducing the number of NIC rx queues to the next lower power of
4423 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4424 	 * if that works.
4425 	 */
4426 	do {
4427 		if (iaq->nrxq > 1) {
4428 			iaq->nrxq = rounddown_pow_of_two(iaq->nrxq - 1);
4429 			if (iaq->nnmrxq > iaq->nrxq)
4430 				iaq->nnmrxq = iaq->nrxq;
4431 		}
4432 		if (iaq->nofldrxq > 1)
4433 			iaq->nofldrxq >>= 1;
4434 
4435 		old_nirq = iaq->nirq;
4436 		update_nirq(iaq, nports);
4437 		if (iaq->nirq <= navail &&
4438 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4439 			device_printf(sc->dev, "running with reduced number of "
4440 			    "rx queues because of shortage of interrupts.  "
4441 			    "nrxq=%u, nofldrxq=%u.  "
4442 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4443 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4444 			goto done;
4445 		}
4446 	} while (old_nirq != iaq->nirq);
4447 
4448 	/* One interrupt for everything.  Ugh. */
4449 	device_printf(sc->dev, "running with minimal number of queues.  "
4450 	    "itype %d, navail %u.\n", itype, navail);
4451 	iaq->nirq = 1;
4452 	iaq->nrxq = 1;
4453 	iaq->ntxq = 1;
4454 	if (iaq->nofldrxq > 0) {
4455 		iaq->nofldrxq = 1;
4456 		iaq->nofldtxq = 1;
4457 	}
4458 	iaq->nnmtxq = 0;
4459 	iaq->nnmrxq = 0;
4460 done:
4461 	MPASS(iaq->num_vis > 0);
4462 	if (iaq->num_vis > 1) {
4463 		MPASS(iaq->nrxq_vi > 0);
4464 		MPASS(iaq->ntxq_vi > 0);
4465 	}
4466 	MPASS(iaq->nirq > 0);
4467 	MPASS(iaq->nrxq > 0);
4468 	MPASS(iaq->ntxq > 0);
4469 	if (itype == INTR_MSI) {
4470 		MPASS(powerof2(iaq->nirq));
4471 	}
4472 }
4473 
4474 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4475 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4476 {
4477 	int rc, itype, navail, nalloc;
4478 
4479 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4480 
4481 		if ((itype & t4_intr_types) == 0)
4482 			continue;	/* not allowed */
4483 
4484 		if (itype == INTR_MSIX)
4485 			navail = pci_msix_count(sc->dev);
4486 		else if (itype == INTR_MSI)
4487 			navail = pci_msi_count(sc->dev);
4488 		else
4489 			navail = 1;
4490 restart:
4491 		if (navail == 0)
4492 			continue;
4493 
4494 		calculate_iaq(sc, iaq, itype, navail);
4495 		nalloc = iaq->nirq;
4496 		rc = 0;
4497 		if (itype == INTR_MSIX)
4498 			rc = pci_alloc_msix(sc->dev, &nalloc);
4499 		else if (itype == INTR_MSI)
4500 			rc = pci_alloc_msi(sc->dev, &nalloc);
4501 
4502 		if (rc == 0 && nalloc > 0) {
4503 			if (nalloc == iaq->nirq)
4504 				return (0);
4505 
4506 			/*
4507 			 * Didn't get the number requested.  Use whatever number
4508 			 * the kernel is willing to allocate.
4509 			 */
4510 			device_printf(sc->dev, "fewer vectors than requested, "
4511 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4512 			    itype, iaq->nirq, nalloc);
4513 			pci_release_msi(sc->dev);
4514 			navail = nalloc;
4515 			goto restart;
4516 		}
4517 
4518 		device_printf(sc->dev,
4519 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4520 		    itype, rc, iaq->nirq, nalloc);
4521 	}
4522 
4523 	device_printf(sc->dev,
4524 	    "failed to find a usable interrupt type.  "
4525 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4526 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4527 
4528 	return (ENXIO);
4529 }
4530 
4531 #define FW_VERSION(chip) ( \
4532     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4533     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4534     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4535     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4536 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4537 
4538 /* Just enough of fw_hdr to cover all version info. */
4539 struct fw_h {
4540 	__u8	ver;
4541 	__u8	chip;
4542 	__be16	len512;
4543 	__be32	fw_ver;
4544 	__be32	tp_microcode_ver;
4545 	__u8	intfver_nic;
4546 	__u8	intfver_vnic;
4547 	__u8	intfver_ofld;
4548 	__u8	intfver_ri;
4549 	__u8	intfver_iscsipdu;
4550 	__u8	intfver_iscsi;
4551 	__u8	intfver_fcoepdu;
4552 	__u8	intfver_fcoe;
4553 };
4554 /* Spot check a couple of fields. */
4555 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4556 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4557 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4558 
4559 struct fw_info {
4560 	uint8_t chip;
4561 	char *kld_name;
4562 	char *fw_mod_name;
4563 	struct fw_h fw_h;
4564 } fw_info[] = {
4565 	{
4566 		.chip = CHELSIO_T4,
4567 		.kld_name = "t4fw_cfg",
4568 		.fw_mod_name = "t4fw",
4569 		.fw_h = {
4570 			.chip = FW_HDR_CHIP_T4,
4571 			.fw_ver = htobe32(FW_VERSION(T4)),
4572 			.intfver_nic = FW_INTFVER(T4, NIC),
4573 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4574 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4575 			.intfver_ri = FW_INTFVER(T4, RI),
4576 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4577 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4578 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4579 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4580 		},
4581 	}, {
4582 		.chip = CHELSIO_T5,
4583 		.kld_name = "t5fw_cfg",
4584 		.fw_mod_name = "t5fw",
4585 		.fw_h = {
4586 			.chip = FW_HDR_CHIP_T5,
4587 			.fw_ver = htobe32(FW_VERSION(T5)),
4588 			.intfver_nic = FW_INTFVER(T5, NIC),
4589 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4590 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4591 			.intfver_ri = FW_INTFVER(T5, RI),
4592 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4593 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4594 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4595 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4596 		},
4597 	}, {
4598 		.chip = CHELSIO_T6,
4599 		.kld_name = "t6fw_cfg",
4600 		.fw_mod_name = "t6fw",
4601 		.fw_h = {
4602 			.chip = FW_HDR_CHIP_T6,
4603 			.fw_ver = htobe32(FW_VERSION(T6)),
4604 			.intfver_nic = FW_INTFVER(T6, NIC),
4605 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4606 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4607 			.intfver_ri = FW_INTFVER(T6, RI),
4608 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4609 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4610 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4611 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4612 		},
4613 	}
4614 };
4615 
4616 static struct fw_info *
find_fw_info(int chip)4617 find_fw_info(int chip)
4618 {
4619 	int i;
4620 
4621 	for (i = 0; i < nitems(fw_info); i++) {
4622 		if (fw_info[i].chip == chip)
4623 			return (&fw_info[i]);
4624 	}
4625 	return (NULL);
4626 }
4627 
4628 /*
4629  * Is the given firmware API compatible with the one the driver was compiled
4630  * with?
4631  */
4632 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4633 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4634 {
4635 
4636 	/* short circuit if it's the exact same firmware version */
4637 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4638 		return (1);
4639 
4640 	/*
4641 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4642 	 * features that are supported in the driver.
4643 	 */
4644 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4645 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4646 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4647 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4648 		return (1);
4649 #undef SAME_INTF
4650 
4651 	return (0);
4652 }
4653 
4654 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4655 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4656     const struct firmware **fw)
4657 {
4658 	struct fw_info *fw_info;
4659 
4660 	*dcfg = NULL;
4661 	if (fw != NULL)
4662 		*fw = NULL;
4663 
4664 	fw_info = find_fw_info(chip_id(sc));
4665 	if (fw_info == NULL) {
4666 		device_printf(sc->dev,
4667 		    "unable to look up firmware information for chip %d.\n",
4668 		    chip_id(sc));
4669 		return (EINVAL);
4670 	}
4671 
4672 	*dcfg = firmware_get(fw_info->kld_name);
4673 	if (*dcfg != NULL) {
4674 		if (fw != NULL)
4675 			*fw = firmware_get(fw_info->fw_mod_name);
4676 		return (0);
4677 	}
4678 
4679 	return (ENOENT);
4680 }
4681 
4682 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4683 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4684     const struct firmware *fw)
4685 {
4686 
4687 	if (fw != NULL)
4688 		firmware_put(fw, FIRMWARE_UNLOAD);
4689 	if (dcfg != NULL)
4690 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4691 }
4692 
4693 /*
4694  * Return values:
4695  * 0 means no firmware install attempted.
4696  * ERESTART means a firmware install was attempted and was successful.
4697  * +ve errno means a firmware install was attempted but failed.
4698  */
4699 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4700 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4701     const struct fw_h *drv_fw, const char *reason, int *already)
4702 {
4703 	const struct firmware *cfg, *fw;
4704 	const uint32_t c = be32toh(card_fw->fw_ver);
4705 	uint32_t d, k;
4706 	int rc, fw_install;
4707 	struct fw_h bundled_fw;
4708 	bool load_attempted;
4709 
4710 	cfg = fw = NULL;
4711 	load_attempted = false;
4712 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4713 
4714 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4715 	if (t4_fw_install < 0) {
4716 		rc = load_fw_module(sc, &cfg, &fw);
4717 		if (rc != 0 || fw == NULL) {
4718 			device_printf(sc->dev,
4719 			    "failed to load firmware module: %d. cfg %p, fw %p;"
4720 			    " will use compiled-in firmware version for"
4721 			    "hw.cxgbe.fw_install checks.\n",
4722 			    rc, cfg, fw);
4723 		} else {
4724 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
4725 		}
4726 		load_attempted = true;
4727 	}
4728 	d = be32toh(bundled_fw.fw_ver);
4729 
4730 	if (reason != NULL)
4731 		goto install;
4732 
4733 	if ((sc->flags & FW_OK) == 0) {
4734 
4735 		if (c == 0xffffffff) {
4736 			reason = "missing";
4737 			goto install;
4738 		}
4739 
4740 		rc = 0;
4741 		goto done;
4742 	}
4743 
4744 	if (!fw_compatible(card_fw, &bundled_fw)) {
4745 		reason = "incompatible or unusable";
4746 		goto install;
4747 	}
4748 
4749 	if (d > c) {
4750 		reason = "older than the version bundled with this driver";
4751 		goto install;
4752 	}
4753 
4754 	if (fw_install == 2 && d != c) {
4755 		reason = "different than the version bundled with this driver";
4756 		goto install;
4757 	}
4758 
4759 	/* No reason to do anything to the firmware already on the card. */
4760 	rc = 0;
4761 	goto done;
4762 
4763 install:
4764 	rc = 0;
4765 	if ((*already)++)
4766 		goto done;
4767 
4768 	if (fw_install == 0) {
4769 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4770 		    "but the driver is prohibited from installing a firmware "
4771 		    "on the card.\n",
4772 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4773 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4774 
4775 		goto done;
4776 	}
4777 
4778 	/*
4779 	 * We'll attempt to install a firmware.  Load the module first (if it
4780 	 * hasn't been loaded already).
4781 	 */
4782 	if (!load_attempted) {
4783 		rc = load_fw_module(sc, &cfg, &fw);
4784 		if (rc != 0 || fw == NULL) {
4785 			device_printf(sc->dev,
4786 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
4787 			    rc, cfg, fw);
4788 			/* carry on */
4789 		}
4790 	}
4791 	if (fw == NULL) {
4792 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4793 		    "but the driver cannot take corrective action because it "
4794 		    "is unable to load the firmware module.\n",
4795 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4796 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4797 		rc = sc->flags & FW_OK ? 0 : ENOENT;
4798 		goto done;
4799 	}
4800 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
4801 	if (k != d) {
4802 		MPASS(t4_fw_install > 0);
4803 		device_printf(sc->dev,
4804 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
4805 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
4806 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
4807 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
4808 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4809 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4810 		rc = sc->flags & FW_OK ? 0 : EINVAL;
4811 		goto done;
4812 	}
4813 
4814 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4815 	    "installing firmware %u.%u.%u.%u on card.\n",
4816 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4817 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
4818 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4819 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4820 
4821 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
4822 	if (rc != 0) {
4823 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
4824 	} else {
4825 		/* Installed successfully, update the cached header too. */
4826 		rc = ERESTART;
4827 		memcpy(card_fw, fw->data, sizeof(*card_fw));
4828 	}
4829 done:
4830 	unload_fw_module(sc, cfg, fw);
4831 
4832 	return (rc);
4833 }
4834 
4835 /*
4836  * Establish contact with the firmware and attempt to become the master driver.
4837  *
4838  * A firmware will be installed to the card if needed (if the driver is allowed
4839  * to do so).
4840  */
4841 static int
contact_firmware(struct adapter * sc)4842 contact_firmware(struct adapter *sc)
4843 {
4844 	int rc, already = 0;
4845 	enum dev_state state;
4846 	struct fw_info *fw_info;
4847 	struct fw_hdr *card_fw;		/* fw on the card */
4848 	const struct fw_h *drv_fw;
4849 
4850 	fw_info = find_fw_info(chip_id(sc));
4851 	if (fw_info == NULL) {
4852 		device_printf(sc->dev,
4853 		    "unable to look up firmware information for chip %d.\n",
4854 		    chip_id(sc));
4855 		return (EINVAL);
4856 	}
4857 	drv_fw = &fw_info->fw_h;
4858 
4859 	/* Read the header of the firmware on the card */
4860 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
4861 restart:
4862 	rc = -t4_get_fw_hdr(sc, card_fw);
4863 	if (rc != 0) {
4864 		device_printf(sc->dev,
4865 		    "unable to read firmware header from card's flash: %d\n",
4866 		    rc);
4867 		goto done;
4868 	}
4869 
4870 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
4871 	    &already);
4872 	if (rc == ERESTART)
4873 		goto restart;
4874 	if (rc != 0)
4875 		goto done;
4876 
4877 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
4878 	if (rc < 0 || state == DEV_STATE_ERR) {
4879 		rc = -rc;
4880 		device_printf(sc->dev,
4881 		    "failed to connect to the firmware: %d, %d.  "
4882 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4883 #if 0
4884 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4885 		    "not responding properly to HELLO", &already) == ERESTART)
4886 			goto restart;
4887 #endif
4888 		goto done;
4889 	}
4890 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
4891 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
4892 
4893 	if (rc == sc->pf) {
4894 		sc->flags |= MASTER_PF;
4895 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4896 		    NULL, &already);
4897 		if (rc == ERESTART)
4898 			rc = 0;
4899 		else if (rc != 0)
4900 			goto done;
4901 	} else if (state == DEV_STATE_UNINIT) {
4902 		/*
4903 		 * We didn't get to be the master so we definitely won't be
4904 		 * configuring the chip.  It's a bug if someone else hasn't
4905 		 * configured it already.
4906 		 */
4907 		device_printf(sc->dev, "couldn't be master(%d), "
4908 		    "device not already initialized either(%d).  "
4909 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4910 		rc = EPROTO;
4911 		goto done;
4912 	} else {
4913 		/*
4914 		 * Some other PF is the master and has configured the chip.
4915 		 * This is allowed but untested.
4916 		 */
4917 		device_printf(sc->dev, "PF%d is master, device state %d.  "
4918 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4919 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
4920 		sc->cfcsum = 0;
4921 		rc = 0;
4922 	}
4923 done:
4924 	if (rc != 0 && sc->flags & FW_OK) {
4925 		t4_fw_bye(sc, sc->mbox);
4926 		sc->flags &= ~FW_OK;
4927 	}
4928 	free(card_fw, M_CXGBE);
4929 	return (rc);
4930 }
4931 
4932 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff)4933 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
4934     uint32_t mtype, uint32_t moff)
4935 {
4936 	struct fw_info *fw_info;
4937 	const struct firmware *dcfg, *rcfg = NULL;
4938 	const uint32_t *cfdata;
4939 	uint32_t cflen, addr;
4940 	int rc;
4941 
4942 	load_fw_module(sc, &dcfg, NULL);
4943 
4944 	/* Card specific interpretation of "default". */
4945 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4946 		if (pci_get_device(sc->dev) == 0x440a)
4947 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
4948 		if (is_fpga(sc))
4949 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
4950 	}
4951 
4952 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4953 		if (dcfg == NULL) {
4954 			device_printf(sc->dev,
4955 			    "KLD with default config is not available.\n");
4956 			rc = ENOENT;
4957 			goto done;
4958 		}
4959 		cfdata = dcfg->data;
4960 		cflen = dcfg->datasize & ~3;
4961 	} else {
4962 		char s[32];
4963 
4964 		fw_info = find_fw_info(chip_id(sc));
4965 		if (fw_info == NULL) {
4966 			device_printf(sc->dev,
4967 			    "unable to look up firmware information for chip %d.\n",
4968 			    chip_id(sc));
4969 			rc = EINVAL;
4970 			goto done;
4971 		}
4972 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
4973 
4974 		rcfg = firmware_get(s);
4975 		if (rcfg == NULL) {
4976 			device_printf(sc->dev,
4977 			    "unable to load module \"%s\" for configuration "
4978 			    "profile \"%s\".\n", s, cfg_file);
4979 			rc = ENOENT;
4980 			goto done;
4981 		}
4982 		cfdata = rcfg->data;
4983 		cflen = rcfg->datasize & ~3;
4984 	}
4985 
4986 	if (cflen > FLASH_CFG_MAX_SIZE) {
4987 		device_printf(sc->dev,
4988 		    "config file too long (%d, max allowed is %d).\n",
4989 		    cflen, FLASH_CFG_MAX_SIZE);
4990 		rc = EINVAL;
4991 		goto done;
4992 	}
4993 
4994 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
4995 	if (rc != 0) {
4996 		device_printf(sc->dev,
4997 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
4998 		    __func__, mtype, moff, cflen, rc);
4999 		rc = EINVAL;
5000 		goto done;
5001 	}
5002 	write_via_memwin(sc, 2, addr, cfdata, cflen);
5003 done:
5004 	if (rcfg != NULL)
5005 		firmware_put(rcfg, FIRMWARE_UNLOAD);
5006 	unload_fw_module(sc, dcfg, NULL);
5007 	return (rc);
5008 }
5009 
5010 struct caps_allowed {
5011 	uint16_t nbmcaps;
5012 	uint16_t linkcaps;
5013 	uint16_t switchcaps;
5014 	uint16_t niccaps;
5015 	uint16_t toecaps;
5016 	uint16_t rdmacaps;
5017 	uint16_t cryptocaps;
5018 	uint16_t iscsicaps;
5019 	uint16_t fcoecaps;
5020 };
5021 
5022 #define FW_PARAM_DEV(param) \
5023 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5024 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5025 #define FW_PARAM_PFVF(param) \
5026 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5027 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5028 
5029 /*
5030  * Provide a configuration profile to the firmware and have it initialize the
5031  * chip accordingly.  This may involve uploading a configuration file to the
5032  * card.
5033  */
5034 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5035 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5036     const struct caps_allowed *caps_allowed)
5037 {
5038 	int rc;
5039 	struct fw_caps_config_cmd caps;
5040 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5041 
5042 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5043 	if (rc != 0) {
5044 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5045 		return (rc);
5046 	}
5047 
5048 	bzero(&caps, sizeof(caps));
5049 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5050 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5051 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5052 		mtype = 0;
5053 		moff = 0;
5054 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5055 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5056 		mtype = FW_MEMTYPE_FLASH;
5057 		moff = t4_flash_cfg_addr(sc);
5058 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5059 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5060 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5061 		    FW_LEN16(caps));
5062 	} else {
5063 		/*
5064 		 * Ask the firmware where it wants us to upload the config file.
5065 		 */
5066 		param = FW_PARAM_DEV(CF);
5067 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5068 		if (rc != 0) {
5069 			/* No support for config file?  Shouldn't happen. */
5070 			device_printf(sc->dev,
5071 			    "failed to query config file location: %d.\n", rc);
5072 			goto done;
5073 		}
5074 		mtype = G_FW_PARAMS_PARAM_Y(val);
5075 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5076 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5077 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5078 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5079 		    FW_LEN16(caps));
5080 
5081 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
5082 		if (rc != 0) {
5083 			device_printf(sc->dev,
5084 			    "failed to upload config file to card: %d.\n", rc);
5085 			goto done;
5086 		}
5087 	}
5088 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5089 	if (rc != 0) {
5090 		device_printf(sc->dev, "failed to pre-process config file: %d "
5091 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5092 		goto done;
5093 	}
5094 
5095 	finicsum = be32toh(caps.finicsum);
5096 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5097 	if (finicsum != cfcsum) {
5098 		device_printf(sc->dev,
5099 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5100 		    finicsum, cfcsum);
5101 	}
5102 	sc->cfcsum = cfcsum;
5103 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5104 
5105 	/*
5106 	 * Let the firmware know what features will (not) be used so it can tune
5107 	 * things accordingly.
5108 	 */
5109 #define LIMIT_CAPS(x) do { \
5110 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5111 } while (0)
5112 	LIMIT_CAPS(nbm);
5113 	LIMIT_CAPS(link);
5114 	LIMIT_CAPS(switch);
5115 	LIMIT_CAPS(nic);
5116 	LIMIT_CAPS(toe);
5117 	LIMIT_CAPS(rdma);
5118 	LIMIT_CAPS(crypto);
5119 	LIMIT_CAPS(iscsi);
5120 	LIMIT_CAPS(fcoe);
5121 #undef LIMIT_CAPS
5122 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5123 		/*
5124 		 * TOE and hashfilters are mutually exclusive.  It is a config
5125 		 * file or firmware bug if both are reported as available.  Try
5126 		 * to cope with the situation in non-debug builds by disabling
5127 		 * TOE.
5128 		 */
5129 		MPASS(caps.toecaps == 0);
5130 
5131 		caps.toecaps = 0;
5132 		caps.rdmacaps = 0;
5133 		caps.iscsicaps = 0;
5134 	}
5135 
5136 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5137 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5138 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5139 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5140 	if (rc != 0) {
5141 		device_printf(sc->dev,
5142 		    "failed to process config file: %d.\n", rc);
5143 		goto done;
5144 	}
5145 
5146 	t4_tweak_chip_settings(sc);
5147 	set_params__pre_init(sc);
5148 
5149 	/* get basic stuff going */
5150 	rc = -t4_fw_initialize(sc, sc->mbox);
5151 	if (rc != 0) {
5152 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5153 		goto done;
5154 	}
5155 done:
5156 	return (rc);
5157 }
5158 
5159 /*
5160  * Partition chip resources for use between various PFs, VFs, etc.
5161  */
5162 static int
partition_resources(struct adapter * sc)5163 partition_resources(struct adapter *sc)
5164 {
5165 	char cfg_file[sizeof(t4_cfg_file)];
5166 	struct caps_allowed caps_allowed;
5167 	int rc;
5168 	bool fallback;
5169 
5170 	/* Only the master driver gets to configure the chip resources. */
5171 	MPASS(sc->flags & MASTER_PF);
5172 
5173 #define COPY_CAPS(x) do { \
5174 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5175 } while (0)
5176 	bzero(&caps_allowed, sizeof(caps_allowed));
5177 	COPY_CAPS(nbm);
5178 	COPY_CAPS(link);
5179 	COPY_CAPS(switch);
5180 	COPY_CAPS(nic);
5181 	COPY_CAPS(toe);
5182 	COPY_CAPS(rdma);
5183 	COPY_CAPS(crypto);
5184 	COPY_CAPS(iscsi);
5185 	COPY_CAPS(fcoe);
5186 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5187 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5188 retry:
5189 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5190 	if (rc != 0 && fallback) {
5191 		dump_devlog(sc);
5192 		device_printf(sc->dev,
5193 		    "failed (%d) to configure card with \"%s\" profile, "
5194 		    "will fall back to a basic configuration and retry.\n",
5195 		    rc, cfg_file);
5196 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5197 		bzero(&caps_allowed, sizeof(caps_allowed));
5198 		COPY_CAPS(switch);
5199 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5200 		fallback = false;
5201 		goto retry;
5202 	}
5203 #undef COPY_CAPS
5204 	return (rc);
5205 }
5206 
5207 /*
5208  * Retrieve parameters that are needed (or nice to have) very early.
5209  */
5210 static int
get_params__pre_init(struct adapter * sc)5211 get_params__pre_init(struct adapter *sc)
5212 {
5213 	int rc;
5214 	uint32_t param[2], val[2];
5215 
5216 	t4_get_version_info(sc);
5217 
5218 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5219 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5220 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5221 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5222 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5223 
5224 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5225 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5226 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5227 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5228 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5229 
5230 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5231 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5232 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5233 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5234 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5235 
5236 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5237 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5238 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5239 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5240 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5241 
5242 	param[0] = FW_PARAM_DEV(PORTVEC);
5243 	param[1] = FW_PARAM_DEV(CCLK);
5244 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5245 	if (rc != 0) {
5246 		device_printf(sc->dev,
5247 		    "failed to query parameters (pre_init): %d.\n", rc);
5248 		return (rc);
5249 	}
5250 
5251 	sc->params.portvec = val[0];
5252 	sc->params.nports = bitcount32(val[0]);
5253 	sc->params.vpd.cclk = val[1];
5254 
5255 	/* Read device log parameters. */
5256 	rc = -t4_init_devlog_params(sc, 1);
5257 	if (rc == 0)
5258 		fixup_devlog_params(sc);
5259 	else {
5260 		device_printf(sc->dev,
5261 		    "failed to get devlog parameters: %d.\n", rc);
5262 		rc = 0;	/* devlog isn't critical for device operation */
5263 	}
5264 
5265 	return (rc);
5266 }
5267 
5268 /*
5269  * Any params that need to be set before FW_INITIALIZE.
5270  */
5271 static int
set_params__pre_init(struct adapter * sc)5272 set_params__pre_init(struct adapter *sc)
5273 {
5274 	int rc = 0;
5275 	uint32_t param, val;
5276 
5277 	if (chip_id(sc) >= CHELSIO_T6) {
5278 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5279 		val = 1;
5280 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5281 		/* firmwares < 1.20.1.0 do not have this param. */
5282 		if (rc == FW_EINVAL &&
5283 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5284 			rc = 0;
5285 		}
5286 		if (rc != 0) {
5287 			device_printf(sc->dev,
5288 			    "failed to enable high priority filters :%d.\n",
5289 			    rc);
5290 		}
5291 
5292 		param = FW_PARAM_DEV(PPOD_EDRAM);
5293 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5294 		if (rc == 0 && val == 1) {
5295 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5296 			    &val);
5297 			if (rc != 0) {
5298 				device_printf(sc->dev,
5299 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5300 			}
5301 		}
5302 	}
5303 
5304 	/* Enable opaque VIIDs with firmwares that support it. */
5305 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5306 	val = 1;
5307 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5308 	if (rc == 0 && val == 1)
5309 		sc->params.viid_smt_extn_support = true;
5310 	else
5311 		sc->params.viid_smt_extn_support = false;
5312 
5313 	return (rc);
5314 }
5315 
5316 /*
5317  * Retrieve various parameters that are of interest to the driver.  The device
5318  * has been initialized by the firmware at this point.
5319  */
5320 static int
get_params__post_init(struct adapter * sc)5321 get_params__post_init(struct adapter *sc)
5322 {
5323 	int rc;
5324 	uint32_t param[7], val[7];
5325 	struct fw_caps_config_cmd caps;
5326 
5327 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5328 	param[1] = FW_PARAM_PFVF(EQ_START);
5329 	param[2] = FW_PARAM_PFVF(FILTER_START);
5330 	param[3] = FW_PARAM_PFVF(FILTER_END);
5331 	param[4] = FW_PARAM_PFVF(L2T_START);
5332 	param[5] = FW_PARAM_PFVF(L2T_END);
5333 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5334 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5335 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5336 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5337 	if (rc != 0) {
5338 		device_printf(sc->dev,
5339 		    "failed to query parameters (post_init): %d.\n", rc);
5340 		return (rc);
5341 	}
5342 
5343 	sc->sge.iq_start = val[0];
5344 	sc->sge.eq_start = val[1];
5345 	if ((int)val[3] > (int)val[2]) {
5346 		sc->tids.ftid_base = val[2];
5347 		sc->tids.ftid_end = val[3];
5348 		sc->tids.nftids = val[3] - val[2] + 1;
5349 	}
5350 	sc->vres.l2t.start = val[4];
5351 	sc->vres.l2t.size = val[5] - val[4] + 1;
5352 	/* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5353 	if (sc->vres.l2t.size > 0)
5354 		MPASS(fls(val[5]) <= S_SYNC_WR);
5355 	sc->params.core_vdd = val[6];
5356 
5357 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5358 	param[1] = FW_PARAM_PFVF(EQ_END);
5359 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5360 	if (rc != 0) {
5361 		device_printf(sc->dev,
5362 		    "failed to query parameters (post_init2): %d.\n", rc);
5363 		return (rc);
5364 	}
5365 	MPASS((int)val[0] >= sc->sge.iq_start);
5366 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5367 	MPASS((int)val[1] >= sc->sge.eq_start);
5368 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5369 
5370 	if (chip_id(sc) >= CHELSIO_T6) {
5371 
5372 		sc->tids.tid_base = t4_read_reg(sc,
5373 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5374 
5375 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5376 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5377 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5378 		if (rc != 0) {
5379 			device_printf(sc->dev,
5380 			   "failed to query hpfilter parameters: %d.\n", rc);
5381 			return (rc);
5382 		}
5383 		if ((int)val[1] > (int)val[0]) {
5384 			sc->tids.hpftid_base = val[0];
5385 			sc->tids.hpftid_end = val[1];
5386 			sc->tids.nhpftids = val[1] - val[0] + 1;
5387 
5388 			/*
5389 			 * These should go off if the layout changes and the
5390 			 * driver needs to catch up.
5391 			 */
5392 			MPASS(sc->tids.hpftid_base == 0);
5393 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5394 		}
5395 
5396 		param[0] = FW_PARAM_PFVF(RAWF_START);
5397 		param[1] = FW_PARAM_PFVF(RAWF_END);
5398 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5399 		if (rc != 0) {
5400 			device_printf(sc->dev,
5401 			   "failed to query rawf parameters: %d.\n", rc);
5402 			return (rc);
5403 		}
5404 		if ((int)val[1] > (int)val[0]) {
5405 			sc->rawf_base = val[0];
5406 			sc->nrawf = val[1] - val[0] + 1;
5407 		}
5408 	}
5409 
5410 	/*
5411 	 * The parameters that follow may not be available on all firmwares.  We
5412 	 * query them individually rather than in a compound query because old
5413 	 * firmwares fail the entire query if an unknown parameter is queried.
5414 	 */
5415 
5416 	/*
5417 	 * MPS buffer group configuration.
5418 	 */
5419 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5420 	val[0] = 0;
5421 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5422 	if (rc == 0)
5423 		sc->params.mps_bg_map = val[0];
5424 	else
5425 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5426 
5427 	param[0] = FW_PARAM_DEV(TPCHMAP);
5428 	val[0] = 0;
5429 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5430 	if (rc == 0)
5431 		sc->params.tp_ch_map = val[0];
5432 	else
5433 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5434 
5435 	/*
5436 	 * Determine whether the firmware supports the filter2 work request.
5437 	 */
5438 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5439 	val[0] = 0;
5440 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5441 	if (rc == 0)
5442 		sc->params.filter2_wr_support = val[0] != 0;
5443 	else
5444 		sc->params.filter2_wr_support = 0;
5445 
5446 	/*
5447 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5448 	 */
5449 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5450 	val[0] = 0;
5451 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5452 	if (rc == 0)
5453 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5454 	else
5455 		sc->params.ulptx_memwrite_dsgl = false;
5456 
5457 	/* FW_RI_FR_NSMR_TPTE_WR support */
5458 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5459 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5460 	if (rc == 0)
5461 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5462 	else
5463 		sc->params.fr_nsmr_tpte_wr_support = false;
5464 
5465 	/* Support for 512 SGL entries per FR MR. */
5466 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5467 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5468 	if (rc == 0)
5469 		sc->params.dev_512sgl_mr = val[0] != 0;
5470 	else
5471 		sc->params.dev_512sgl_mr = false;
5472 
5473 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5474 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5475 	if (rc == 0)
5476 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5477 	else
5478 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5479 
5480 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5481 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5482 	if (rc == 0) {
5483 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5484 		sc->params.nsched_cls = val[0];
5485 	} else
5486 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5487 
5488 	/* get capabilites */
5489 	bzero(&caps, sizeof(caps));
5490 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5491 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5492 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5493 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5494 	if (rc != 0) {
5495 		device_printf(sc->dev,
5496 		    "failed to get card capabilities: %d.\n", rc);
5497 		return (rc);
5498 	}
5499 
5500 #define READ_CAPS(x) do { \
5501 	sc->x = htobe16(caps.x); \
5502 } while (0)
5503 	READ_CAPS(nbmcaps);
5504 	READ_CAPS(linkcaps);
5505 	READ_CAPS(switchcaps);
5506 	READ_CAPS(niccaps);
5507 	READ_CAPS(toecaps);
5508 	READ_CAPS(rdmacaps);
5509 	READ_CAPS(cryptocaps);
5510 	READ_CAPS(iscsicaps);
5511 	READ_CAPS(fcoecaps);
5512 
5513 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5514 		MPASS(chip_id(sc) > CHELSIO_T4);
5515 		MPASS(sc->toecaps == 0);
5516 		sc->toecaps = 0;
5517 
5518 		param[0] = FW_PARAM_DEV(NTID);
5519 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5520 		if (rc != 0) {
5521 			device_printf(sc->dev,
5522 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5523 			return (rc);
5524 		}
5525 		sc->tids.ntids = val[0];
5526 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5527 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5528 			sc->tids.ntids -= sc->tids.nhpftids;
5529 		}
5530 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5531 		sc->params.hash_filter = 1;
5532 	}
5533 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5534 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5535 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5536 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5537 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5538 		if (rc != 0) {
5539 			device_printf(sc->dev,
5540 			    "failed to query NIC parameters: %d.\n", rc);
5541 			return (rc);
5542 		}
5543 		if ((int)val[1] > (int)val[0]) {
5544 			sc->tids.etid_base = val[0];
5545 			sc->tids.etid_end = val[1];
5546 			sc->tids.netids = val[1] - val[0] + 1;
5547 			sc->params.eo_wr_cred = val[2];
5548 			sc->params.ethoffload = 1;
5549 		}
5550 	}
5551 	if (sc->toecaps) {
5552 		/* query offload-related parameters */
5553 		param[0] = FW_PARAM_DEV(NTID);
5554 		param[1] = FW_PARAM_PFVF(SERVER_START);
5555 		param[2] = FW_PARAM_PFVF(SERVER_END);
5556 		param[3] = FW_PARAM_PFVF(TDDP_START);
5557 		param[4] = FW_PARAM_PFVF(TDDP_END);
5558 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5559 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5560 		if (rc != 0) {
5561 			device_printf(sc->dev,
5562 			    "failed to query TOE parameters: %d.\n", rc);
5563 			return (rc);
5564 		}
5565 		sc->tids.ntids = val[0];
5566 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5567 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5568 			sc->tids.ntids -= sc->tids.nhpftids;
5569 		}
5570 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5571 		if ((int)val[2] > (int)val[1]) {
5572 			sc->tids.stid_base = val[1];
5573 			sc->tids.nstids = val[2] - val[1] + 1;
5574 		}
5575 		sc->vres.ddp.start = val[3];
5576 		sc->vres.ddp.size = val[4] - val[3] + 1;
5577 		sc->params.ofldq_wr_cred = val[5];
5578 		sc->params.offload = 1;
5579 	} else {
5580 		/*
5581 		 * The firmware attempts memfree TOE configuration for -SO cards
5582 		 * and will report toecaps=0 if it runs out of resources (this
5583 		 * depends on the config file).  It may not report 0 for other
5584 		 * capabilities dependent on the TOE in this case.  Set them to
5585 		 * 0 here so that the driver doesn't bother tracking resources
5586 		 * that will never be used.
5587 		 */
5588 		sc->iscsicaps = 0;
5589 		sc->rdmacaps = 0;
5590 	}
5591 	if (sc->rdmacaps) {
5592 		param[0] = FW_PARAM_PFVF(STAG_START);
5593 		param[1] = FW_PARAM_PFVF(STAG_END);
5594 		param[2] = FW_PARAM_PFVF(RQ_START);
5595 		param[3] = FW_PARAM_PFVF(RQ_END);
5596 		param[4] = FW_PARAM_PFVF(PBL_START);
5597 		param[5] = FW_PARAM_PFVF(PBL_END);
5598 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5599 		if (rc != 0) {
5600 			device_printf(sc->dev,
5601 			    "failed to query RDMA parameters(1): %d.\n", rc);
5602 			return (rc);
5603 		}
5604 		sc->vres.stag.start = val[0];
5605 		sc->vres.stag.size = val[1] - val[0] + 1;
5606 		sc->vres.rq.start = val[2];
5607 		sc->vres.rq.size = val[3] - val[2] + 1;
5608 		sc->vres.pbl.start = val[4];
5609 		sc->vres.pbl.size = val[5] - val[4] + 1;
5610 
5611 		param[0] = FW_PARAM_PFVF(SQRQ_START);
5612 		param[1] = FW_PARAM_PFVF(SQRQ_END);
5613 		param[2] = FW_PARAM_PFVF(CQ_START);
5614 		param[3] = FW_PARAM_PFVF(CQ_END);
5615 		param[4] = FW_PARAM_PFVF(OCQ_START);
5616 		param[5] = FW_PARAM_PFVF(OCQ_END);
5617 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5618 		if (rc != 0) {
5619 			device_printf(sc->dev,
5620 			    "failed to query RDMA parameters(2): %d.\n", rc);
5621 			return (rc);
5622 		}
5623 		sc->vres.qp.start = val[0];
5624 		sc->vres.qp.size = val[1] - val[0] + 1;
5625 		sc->vres.cq.start = val[2];
5626 		sc->vres.cq.size = val[3] - val[2] + 1;
5627 		sc->vres.ocq.start = val[4];
5628 		sc->vres.ocq.size = val[5] - val[4] + 1;
5629 
5630 		param[0] = FW_PARAM_PFVF(SRQ_START);
5631 		param[1] = FW_PARAM_PFVF(SRQ_END);
5632 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
5633 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5634 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5635 		if (rc != 0) {
5636 			device_printf(sc->dev,
5637 			    "failed to query RDMA parameters(3): %d.\n", rc);
5638 			return (rc);
5639 		}
5640 		sc->vres.srq.start = val[0];
5641 		sc->vres.srq.size = val[1] - val[0] + 1;
5642 		sc->params.max_ordird_qp = val[2];
5643 		sc->params.max_ird_adapter = val[3];
5644 	}
5645 	if (sc->iscsicaps) {
5646 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5647 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5648 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5649 		if (rc != 0) {
5650 			device_printf(sc->dev,
5651 			    "failed to query iSCSI parameters: %d.\n", rc);
5652 			return (rc);
5653 		}
5654 		sc->vres.iscsi.start = val[0];
5655 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5656 	}
5657 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5658 		param[0] = FW_PARAM_PFVF(TLS_START);
5659 		param[1] = FW_PARAM_PFVF(TLS_END);
5660 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5661 		if (rc != 0) {
5662 			device_printf(sc->dev,
5663 			    "failed to query TLS parameters: %d.\n", rc);
5664 			return (rc);
5665 		}
5666 		sc->vres.key.start = val[0];
5667 		sc->vres.key.size = val[1] - val[0] + 1;
5668 	}
5669 
5670 	/*
5671 	 * We've got the params we wanted to query directly from the firmware.
5672 	 * Grab some others via other means.
5673 	 */
5674 	t4_init_sge_params(sc);
5675 	t4_init_tp_params(sc);
5676 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5677 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5678 
5679 	rc = t4_verify_chip_settings(sc);
5680 	if (rc != 0)
5681 		return (rc);
5682 	t4_init_rx_buf_info(sc);
5683 
5684 	return (rc);
5685 }
5686 
5687 #ifdef KERN_TLS
5688 static void
ktls_tick(void * arg)5689 ktls_tick(void *arg)
5690 {
5691 	struct adapter *sc;
5692 	uint32_t tstamp;
5693 
5694 	sc = arg;
5695 	tstamp = tcp_ts_getticks();
5696 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
5697 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
5698 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
5699 }
5700 
5701 static int
t6_config_kern_tls(struct adapter * sc,bool enable)5702 t6_config_kern_tls(struct adapter *sc, bool enable)
5703 {
5704 	int rc;
5705 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5706 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
5707 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
5708 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
5709 
5710 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
5711 	if (rc != 0) {
5712 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
5713 		    enable ?  "enable" : "disable", rc);
5714 		return (rc);
5715 	}
5716 
5717 	if (enable) {
5718 		sc->flags |= KERN_TLS_ON;
5719 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
5720 		    C_HARDCLOCK);
5721 	} else {
5722 		sc->flags &= ~KERN_TLS_ON;
5723 		callout_stop(&sc->ktls_tick);
5724 	}
5725 
5726 	return (rc);
5727 }
5728 #endif
5729 
5730 static int
set_params__post_init(struct adapter * sc)5731 set_params__post_init(struct adapter *sc)
5732 {
5733 	uint32_t mask, param, val;
5734 #ifdef TCP_OFFLOAD
5735 	int i, v, shift;
5736 #endif
5737 
5738 	/* ask for encapsulated CPLs */
5739 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5740 	val = 1;
5741 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5742 
5743 	/* Enable 32b port caps if the firmware supports it. */
5744 	param = FW_PARAM_PFVF(PORT_CAPS32);
5745 	val = 1;
5746 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
5747 		sc->params.port_caps32 = 1;
5748 
5749 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
5750 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
5751 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
5752 	    V_MASKFILTER(val - 1));
5753 
5754 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
5755 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
5756 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5757 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
5758 	val = 0;
5759 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
5760 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
5761 		    F_ATTACKFILTERENABLE);
5762 		val |= F_DROPERRORATTACK;
5763 	}
5764 	if (t4_drop_ip_fragments != 0) {
5765 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
5766 		    F_FRAGMENTDROP);
5767 		val |= F_DROPERRORFRAG;
5768 	}
5769 	if (t4_drop_pkts_with_l2_errors != 0)
5770 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
5771 	if (t4_drop_pkts_with_l3_errors != 0) {
5772 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
5773 		    F_DROPERRORCSUMIP;
5774 	}
5775 	if (t4_drop_pkts_with_l4_errors != 0) {
5776 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5777 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
5778 	}
5779 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
5780 
5781 #ifdef TCP_OFFLOAD
5782 	/*
5783 	 * Override the TOE timers with user provided tunables.  This is not the
5784 	 * recommended way to change the timers (the firmware config file is) so
5785 	 * these tunables are not documented.
5786 	 *
5787 	 * All the timer tunables are in microseconds.
5788 	 */
5789 	if (t4_toe_keepalive_idle != 0) {
5790 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
5791 		v &= M_KEEPALIVEIDLE;
5792 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
5793 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
5794 	}
5795 	if (t4_toe_keepalive_interval != 0) {
5796 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
5797 		v &= M_KEEPALIVEINTVL;
5798 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
5799 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
5800 	}
5801 	if (t4_toe_keepalive_count != 0) {
5802 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
5803 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5804 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
5805 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
5806 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
5807 	}
5808 	if (t4_toe_rexmt_min != 0) {
5809 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
5810 		v &= M_RXTMIN;
5811 		t4_set_reg_field(sc, A_TP_RXT_MIN,
5812 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
5813 	}
5814 	if (t4_toe_rexmt_max != 0) {
5815 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
5816 		v &= M_RXTMAX;
5817 		t4_set_reg_field(sc, A_TP_RXT_MAX,
5818 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
5819 	}
5820 	if (t4_toe_rexmt_count != 0) {
5821 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
5822 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5823 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
5824 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
5825 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
5826 	}
5827 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
5828 		if (t4_toe_rexmt_backoff[i] != -1) {
5829 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
5830 			shift = (i & 3) << 3;
5831 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
5832 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
5833 		}
5834 	}
5835 #endif
5836 
5837 	/*
5838 	 * Limit TOE connections to 2 reassembly "islands".  This is
5839 	 * required to permit migrating TOE connections to either
5840 	 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
5841 	 */
5842 	t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
5843 	    V_PASSMODE(2));
5844 
5845 #ifdef KERN_TLS
5846 	if (is_ktls(sc)) {
5847 		sc->tlst.inline_keys = t4_tls_inline_keys;
5848 		sc->tlst.combo_wrs = t4_tls_combo_wrs;
5849 		if (t4_kern_tls != 0 && is_t6(sc))
5850 			t6_config_kern_tls(sc, true);
5851 	}
5852 #endif
5853 	return (0);
5854 }
5855 
5856 #undef FW_PARAM_PFVF
5857 #undef FW_PARAM_DEV
5858 
5859 static void
t4_set_desc(struct adapter * sc)5860 t4_set_desc(struct adapter *sc)
5861 {
5862 	struct adapter_params *p = &sc->params;
5863 
5864 	device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
5865 }
5866 
5867 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)5868 ifmedia_add4(struct ifmedia *ifm, int m)
5869 {
5870 
5871 	ifmedia_add(ifm, m, 0, NULL);
5872 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
5873 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
5874 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
5875 }
5876 
5877 /*
5878  * This is the selected media, which is not quite the same as the active media.
5879  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
5880  * and active are not the same, and "media: Ethernet selected" otherwise.
5881  */
5882 static void
set_current_media(struct port_info * pi)5883 set_current_media(struct port_info *pi)
5884 {
5885 	struct link_config *lc;
5886 	struct ifmedia *ifm;
5887 	int mword;
5888 	u_int speed;
5889 
5890 	PORT_LOCK_ASSERT_OWNED(pi);
5891 
5892 	/* Leave current media alone if it's already set to IFM_NONE. */
5893 	ifm = &pi->media;
5894 	if (ifm->ifm_cur != NULL &&
5895 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
5896 		return;
5897 
5898 	lc = &pi->link_cfg;
5899 	if (lc->requested_aneg != AUTONEG_DISABLE &&
5900 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
5901 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
5902 		return;
5903 	}
5904 	mword = IFM_ETHER | IFM_FDX;
5905 	if (lc->requested_fc & PAUSE_TX)
5906 		mword |= IFM_ETH_TXPAUSE;
5907 	if (lc->requested_fc & PAUSE_RX)
5908 		mword |= IFM_ETH_RXPAUSE;
5909 	if (lc->requested_speed == 0)
5910 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
5911 	else
5912 		speed = lc->requested_speed;
5913 	mword |= port_mword(pi, speed_to_fwcap(speed));
5914 	ifmedia_set(ifm, mword);
5915 }
5916 
5917 /*
5918  * Returns true if the ifmedia list for the port cannot change.
5919  */
5920 static bool
fixed_ifmedia(struct port_info * pi)5921 fixed_ifmedia(struct port_info *pi)
5922 {
5923 
5924 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
5925 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
5926 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
5927 	    pi->port_type == FW_PORT_TYPE_KX4 ||
5928 	    pi->port_type == FW_PORT_TYPE_KX ||
5929 	    pi->port_type == FW_PORT_TYPE_KR ||
5930 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
5931 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
5932 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
5933 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
5934 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
5935 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
5936 }
5937 
5938 static void
build_medialist(struct port_info * pi)5939 build_medialist(struct port_info *pi)
5940 {
5941 	uint32_t ss, speed;
5942 	int unknown, mword, bit;
5943 	struct link_config *lc;
5944 	struct ifmedia *ifm;
5945 
5946 	PORT_LOCK_ASSERT_OWNED(pi);
5947 
5948 	if (pi->flags & FIXED_IFMEDIA)
5949 		return;
5950 
5951 	/*
5952 	 * Rebuild the ifmedia list.
5953 	 */
5954 	ifm = &pi->media;
5955 	ifmedia_removeall(ifm);
5956 	lc = &pi->link_cfg;
5957 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
5958 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
5959 		MPASS(ss != 0);
5960 no_media:
5961 		MPASS(LIST_EMPTY(&ifm->ifm_list));
5962 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
5963 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
5964 		return;
5965 	}
5966 
5967 	unknown = 0;
5968 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
5969 		speed = 1 << bit;
5970 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
5971 		if (ss & speed) {
5972 			mword = port_mword(pi, speed);
5973 			if (mword == IFM_NONE) {
5974 				goto no_media;
5975 			} else if (mword == IFM_UNKNOWN)
5976 				unknown++;
5977 			else
5978 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
5979 		}
5980 	}
5981 	if (unknown > 0) /* Add one unknown for all unknown media types. */
5982 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
5983 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
5984 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
5985 
5986 	set_current_media(pi);
5987 }
5988 
5989 /*
5990  * Initialize the requested fields in the link config based on driver tunables.
5991  */
5992 static void
init_link_config(struct port_info * pi)5993 init_link_config(struct port_info *pi)
5994 {
5995 	struct link_config *lc = &pi->link_cfg;
5996 
5997 	PORT_LOCK_ASSERT_OWNED(pi);
5998 
5999 	lc->requested_caps = 0;
6000 	lc->requested_speed = 0;
6001 
6002 	if (t4_autoneg == 0)
6003 		lc->requested_aneg = AUTONEG_DISABLE;
6004 	else if (t4_autoneg == 1)
6005 		lc->requested_aneg = AUTONEG_ENABLE;
6006 	else
6007 		lc->requested_aneg = AUTONEG_AUTO;
6008 
6009 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
6010 	    PAUSE_AUTONEG);
6011 
6012 	if (t4_fec & FEC_AUTO)
6013 		lc->requested_fec = FEC_AUTO;
6014 	else if (t4_fec == 0)
6015 		lc->requested_fec = FEC_NONE;
6016 	else {
6017 		/* -1 is handled by the FEC_AUTO block above and not here. */
6018 		lc->requested_fec = t4_fec &
6019 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6020 		if (lc->requested_fec == 0)
6021 			lc->requested_fec = FEC_AUTO;
6022 	}
6023 	if (t4_force_fec < 0)
6024 		lc->force_fec = -1;
6025 	else if (t4_force_fec > 0)
6026 		lc->force_fec = 1;
6027 	else
6028 		lc->force_fec = 0;
6029 }
6030 
6031 /*
6032  * Makes sure that all requested settings comply with what's supported by the
6033  * port.  Returns the number of settings that were invalid and had to be fixed.
6034  */
6035 static int
fixup_link_config(struct port_info * pi)6036 fixup_link_config(struct port_info *pi)
6037 {
6038 	int n = 0;
6039 	struct link_config *lc = &pi->link_cfg;
6040 	uint32_t fwspeed;
6041 
6042 	PORT_LOCK_ASSERT_OWNED(pi);
6043 
6044 	/* Speed (when not autonegotiating) */
6045 	if (lc->requested_speed != 0) {
6046 		fwspeed = speed_to_fwcap(lc->requested_speed);
6047 		if ((fwspeed & lc->pcaps) == 0) {
6048 			n++;
6049 			lc->requested_speed = 0;
6050 		}
6051 	}
6052 
6053 	/* Link autonegotiation */
6054 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6055 	    lc->requested_aneg == AUTONEG_DISABLE ||
6056 	    lc->requested_aneg == AUTONEG_AUTO);
6057 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6058 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6059 		n++;
6060 		lc->requested_aneg = AUTONEG_AUTO;
6061 	}
6062 
6063 	/* Flow control */
6064 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6065 	if (lc->requested_fc & PAUSE_TX &&
6066 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6067 		n++;
6068 		lc->requested_fc &= ~PAUSE_TX;
6069 	}
6070 	if (lc->requested_fc & PAUSE_RX &&
6071 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6072 		n++;
6073 		lc->requested_fc &= ~PAUSE_RX;
6074 	}
6075 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6076 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6077 		n++;
6078 		lc->requested_fc |= PAUSE_AUTONEG;
6079 	}
6080 
6081 	/* FEC */
6082 	if ((lc->requested_fec & FEC_RS &&
6083 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6084 	    (lc->requested_fec & FEC_BASER_RS &&
6085 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6086 		n++;
6087 		lc->requested_fec = FEC_AUTO;
6088 	}
6089 
6090 	return (n);
6091 }
6092 
6093 /*
6094  * Apply the requested L1 settings, which are expected to be valid, to the
6095  * hardware.
6096  */
6097 static int
apply_link_config(struct port_info * pi)6098 apply_link_config(struct port_info *pi)
6099 {
6100 	struct adapter *sc = pi->adapter;
6101 	struct link_config *lc = &pi->link_cfg;
6102 	int rc;
6103 
6104 #ifdef INVARIANTS
6105 	ASSERT_SYNCHRONIZED_OP(sc);
6106 	PORT_LOCK_ASSERT_OWNED(pi);
6107 
6108 	if (lc->requested_aneg == AUTONEG_ENABLE)
6109 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6110 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6111 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6112 	if (lc->requested_fc & PAUSE_TX)
6113 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6114 	if (lc->requested_fc & PAUSE_RX)
6115 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6116 	if (lc->requested_fec & FEC_RS)
6117 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6118 	if (lc->requested_fec & FEC_BASER_RS)
6119 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6120 #endif
6121 	if (!(sc->flags & IS_VF)) {
6122 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
6123 		if (rc != 0) {
6124 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6125 			return (rc);
6126 		}
6127 	}
6128 
6129 	/*
6130 	 * An L1_CFG will almost always result in a link-change event if the
6131 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6132 	 * the notification is processed.  If the link is down then the actual
6133 	 * settings are meaningless.
6134 	 *
6135 	 * This takes care of the case where a change in the L1 settings may not
6136 	 * result in a notification.
6137 	 */
6138 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6139 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6140 
6141 	return (0);
6142 }
6143 
6144 #define FW_MAC_EXACT_CHUNK	7
6145 struct mcaddr_ctx {
6146 	if_t ifp;
6147 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6148 	uint64_t hash;
6149 	int i;
6150 	int del;
6151 	int rc;
6152 };
6153 
6154 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6155 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6156 {
6157 	struct mcaddr_ctx *ctx = arg;
6158 	struct vi_info *vi = if_getsoftc(ctx->ifp);
6159 	struct port_info *pi = vi->pi;
6160 	struct adapter *sc = pi->adapter;
6161 
6162 	if (ctx->rc < 0)
6163 		return (0);
6164 
6165 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6166 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6167 	ctx->i++;
6168 
6169 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6170 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6171 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6172 		if (ctx->rc < 0) {
6173 			int j;
6174 
6175 			for (j = 0; j < ctx->i; j++) {
6176 				if_printf(ctx->ifp,
6177 				    "failed to add mc address"
6178 				    " %02x:%02x:%02x:"
6179 				    "%02x:%02x:%02x rc=%d\n",
6180 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6181 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6182 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6183 				    -ctx->rc);
6184 			}
6185 			return (0);
6186 		}
6187 		ctx->del = 0;
6188 		ctx->i = 0;
6189 	}
6190 
6191 	return (1);
6192 }
6193 
6194 /*
6195  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6196  * indicates which parameters should be programmed (the rest are left alone).
6197  */
6198 int
update_mac_settings(if_t ifp,int flags)6199 update_mac_settings(if_t ifp, int flags)
6200 {
6201 	int rc = 0;
6202 	struct vi_info *vi = if_getsoftc(ifp);
6203 	struct port_info *pi = vi->pi;
6204 	struct adapter *sc = pi->adapter;
6205 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6206 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6207 
6208 	ASSERT_SYNCHRONIZED_OP(sc);
6209 	KASSERT(flags, ("%s: not told what to update.", __func__));
6210 
6211 	if (flags & XGMAC_MTU)
6212 		mtu = if_getmtu(ifp);
6213 
6214 	if (flags & XGMAC_PROMISC)
6215 		promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6216 
6217 	if (flags & XGMAC_ALLMULTI)
6218 		allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6219 
6220 	if (flags & XGMAC_VLANEX)
6221 		vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6222 
6223 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6224 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6225 		    allmulti, 1, vlanex, false);
6226 		if (rc) {
6227 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6228 			    rc);
6229 			return (rc);
6230 		}
6231 	}
6232 
6233 	if (flags & XGMAC_UCADDR) {
6234 		uint8_t ucaddr[ETHER_ADDR_LEN];
6235 
6236 		bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6237 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6238 		    ucaddr, true, &vi->smt_idx);
6239 		if (rc < 0) {
6240 			rc = -rc;
6241 			if_printf(ifp, "change_mac failed: %d\n", rc);
6242 			return (rc);
6243 		} else {
6244 			vi->xact_addr_filt = rc;
6245 			rc = 0;
6246 		}
6247 	}
6248 
6249 	if (flags & XGMAC_MCADDRS) {
6250 		struct epoch_tracker et;
6251 		struct mcaddr_ctx ctx;
6252 		int j;
6253 
6254 		ctx.ifp = ifp;
6255 		ctx.hash = 0;
6256 		ctx.i = 0;
6257 		ctx.del = 1;
6258 		ctx.rc = 0;
6259 		/*
6260 		 * Unlike other drivers, we accumulate list of pointers into
6261 		 * interface address lists and we need to keep it safe even
6262 		 * after if_foreach_llmaddr() returns, thus we must enter the
6263 		 * network epoch.
6264 		 */
6265 		NET_EPOCH_ENTER(et);
6266 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6267 		if (ctx.rc < 0) {
6268 			NET_EPOCH_EXIT(et);
6269 			rc = -ctx.rc;
6270 			return (rc);
6271 		}
6272 		if (ctx.i > 0) {
6273 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6274 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6275 			NET_EPOCH_EXIT(et);
6276 			if (rc < 0) {
6277 				rc = -rc;
6278 				for (j = 0; j < ctx.i; j++) {
6279 					if_printf(ifp,
6280 					    "failed to add mcast address"
6281 					    " %02x:%02x:%02x:"
6282 					    "%02x:%02x:%02x rc=%d\n",
6283 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6284 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6285 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6286 					    rc);
6287 				}
6288 				return (rc);
6289 			}
6290 			ctx.del = 0;
6291 		} else
6292 			NET_EPOCH_EXIT(et);
6293 
6294 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6295 		if (rc != 0)
6296 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6297 			    rc);
6298 		if (ctx.del == 0) {
6299 			/* We clobbered the VXLAN entry if there was one. */
6300 			pi->vxlan_tcam_entry = false;
6301 		}
6302 	}
6303 
6304 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6305 	    pi->vxlan_tcam_entry == false) {
6306 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6307 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6308 		    true);
6309 		if (rc < 0) {
6310 			rc = -rc;
6311 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6312 			    rc);
6313 		} else {
6314 			MPASS(rc == sc->rawf_base + pi->port_id);
6315 			rc = 0;
6316 			pi->vxlan_tcam_entry = true;
6317 		}
6318 	}
6319 
6320 	return (rc);
6321 }
6322 
6323 /*
6324  * {begin|end}_synchronized_op must be called from the same thread.
6325  */
6326 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6327 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6328     char *wmesg)
6329 {
6330 	int rc, pri;
6331 
6332 #ifdef WITNESS
6333 	/* the caller thinks it's ok to sleep, but is it really? */
6334 	if (flags & SLEEP_OK)
6335 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
6336 		    "begin_synchronized_op");
6337 #endif
6338 
6339 	if (INTR_OK)
6340 		pri = PCATCH;
6341 	else
6342 		pri = 0;
6343 
6344 	ADAPTER_LOCK(sc);
6345 	for (;;) {
6346 
6347 		if (vi && IS_DETACHING(vi)) {
6348 			rc = ENXIO;
6349 			goto done;
6350 		}
6351 
6352 		if (!IS_BUSY(sc)) {
6353 			rc = 0;
6354 			break;
6355 		}
6356 
6357 		if (!(flags & SLEEP_OK)) {
6358 			rc = EBUSY;
6359 			goto done;
6360 		}
6361 
6362 		if (mtx_sleep(&sc->flags, &sc->sc_lock, pri, wmesg, 0)) {
6363 			rc = EINTR;
6364 			goto done;
6365 		}
6366 	}
6367 
6368 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6369 	SET_BUSY(sc);
6370 #ifdef INVARIANTS
6371 	sc->last_op = wmesg;
6372 	sc->last_op_thr = curthread;
6373 	sc->last_op_flags = flags;
6374 #endif
6375 
6376 done:
6377 	if (!(flags & HOLD_LOCK) || rc)
6378 		ADAPTER_UNLOCK(sc);
6379 
6380 	return (rc);
6381 }
6382 
6383 /*
6384  * Tell if_ioctl and if_init that the VI is going away.  This is
6385  * special variant of begin_synchronized_op and must be paired with a
6386  * call to end_vi_detach.
6387  */
6388 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6389 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6390 {
6391 	ADAPTER_LOCK(sc);
6392 	SET_DETACHING(vi);
6393 	wakeup(&sc->flags);
6394 	while (IS_BUSY(sc))
6395 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6396 	SET_BUSY(sc);
6397 #ifdef INVARIANTS
6398 	sc->last_op = "t4detach";
6399 	sc->last_op_thr = curthread;
6400 	sc->last_op_flags = 0;
6401 #endif
6402 	ADAPTER_UNLOCK(sc);
6403 }
6404 
6405 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6406 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6407 {
6408 	ADAPTER_LOCK(sc);
6409 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6410 	CLR_BUSY(sc);
6411 	CLR_DETACHING(vi);
6412 	wakeup(&sc->flags);
6413 	ADAPTER_UNLOCK(sc);
6414 }
6415 
6416 /*
6417  * {begin|end}_synchronized_op must be called from the same thread.
6418  */
6419 void
end_synchronized_op(struct adapter * sc,int flags)6420 end_synchronized_op(struct adapter *sc, int flags)
6421 {
6422 
6423 	if (flags & LOCK_HELD)
6424 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6425 	else
6426 		ADAPTER_LOCK(sc);
6427 
6428 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6429 	CLR_BUSY(sc);
6430 	wakeup(&sc->flags);
6431 	ADAPTER_UNLOCK(sc);
6432 }
6433 
6434 static int
cxgbe_init_synchronized(struct vi_info * vi)6435 cxgbe_init_synchronized(struct vi_info *vi)
6436 {
6437 	struct port_info *pi = vi->pi;
6438 	struct adapter *sc = pi->adapter;
6439 	if_t ifp = vi->ifp;
6440 	int rc = 0, i;
6441 	struct sge_txq *txq;
6442 
6443 	ASSERT_SYNCHRONIZED_OP(sc);
6444 
6445 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6446 		return (0);	/* already running */
6447 
6448 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6449 		return (rc);	/* error message displayed already */
6450 
6451 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6452 		return (rc); /* error message displayed already */
6453 
6454 	rc = update_mac_settings(ifp, XGMAC_ALL);
6455 	if (rc)
6456 		goto done;	/* error message displayed already */
6457 
6458 	PORT_LOCK(pi);
6459 	if (pi->up_vis == 0) {
6460 		t4_update_port_info(pi);
6461 		fixup_link_config(pi);
6462 		build_medialist(pi);
6463 		apply_link_config(pi);
6464 	}
6465 
6466 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6467 	if (rc != 0) {
6468 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6469 		PORT_UNLOCK(pi);
6470 		goto done;
6471 	}
6472 
6473 	/*
6474 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6475 	 * if this changes.
6476 	 */
6477 
6478 	for_each_txq(vi, i, txq) {
6479 		TXQ_LOCK(txq);
6480 		txq->eq.flags |= EQ_ENABLED;
6481 		TXQ_UNLOCK(txq);
6482 	}
6483 
6484 	/*
6485 	 * The first iq of the first port to come up is used for tracing.
6486 	 */
6487 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6488 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6489 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
6490 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
6491 		    V_QUEUENUMBER(sc->traceq));
6492 		pi->flags |= HAS_TRACEQ;
6493 	}
6494 
6495 	/* all ok */
6496 	pi->up_vis++;
6497 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6498 	if (pi->link_cfg.link_ok)
6499 		t4_os_link_changed(pi);
6500 	PORT_UNLOCK(pi);
6501 
6502 	mtx_lock(&vi->tick_mtx);
6503 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6504 		callout_reset(&vi->tick, hz, vi_tick, vi);
6505 	else
6506 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6507 	mtx_unlock(&vi->tick_mtx);
6508 done:
6509 	if (rc != 0)
6510 		cxgbe_uninit_synchronized(vi);
6511 
6512 	return (rc);
6513 }
6514 
6515 /*
6516  * Idempotent.
6517  */
6518 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6519 cxgbe_uninit_synchronized(struct vi_info *vi)
6520 {
6521 	struct port_info *pi = vi->pi;
6522 	struct adapter *sc = pi->adapter;
6523 	if_t ifp = vi->ifp;
6524 	int rc, i;
6525 	struct sge_txq *txq;
6526 
6527 	ASSERT_SYNCHRONIZED_OP(sc);
6528 
6529 	if (!(vi->flags & VI_INIT_DONE)) {
6530 		if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6531 			KASSERT(0, ("uninited VI is running"));
6532 			if_printf(ifp, "uninited VI with running ifnet.  "
6533 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6534 			    "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6535 			    if_getdrvflags(ifp));
6536 		}
6537 		return (0);
6538 	}
6539 
6540 	/*
6541 	 * Disable the VI so that all its data in either direction is discarded
6542 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6543 	 * tick) intact as the TP can deliver negative advice or data that it's
6544 	 * holding in its RAM (for an offloaded connection) even after the VI is
6545 	 * disabled.
6546 	 */
6547 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6548 	if (rc) {
6549 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6550 		return (rc);
6551 	}
6552 
6553 	for_each_txq(vi, i, txq) {
6554 		TXQ_LOCK(txq);
6555 		txq->eq.flags &= ~EQ_ENABLED;
6556 		TXQ_UNLOCK(txq);
6557 	}
6558 
6559 	mtx_lock(&vi->tick_mtx);
6560 	callout_stop(&vi->tick);
6561 	mtx_unlock(&vi->tick_mtx);
6562 
6563 	PORT_LOCK(pi);
6564 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6565 		PORT_UNLOCK(pi);
6566 		return (0);
6567 	}
6568 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6569 	pi->up_vis--;
6570 	if (pi->up_vis > 0) {
6571 		PORT_UNLOCK(pi);
6572 		return (0);
6573 	}
6574 
6575 	pi->link_cfg.link_ok = false;
6576 	pi->link_cfg.speed = 0;
6577 	pi->link_cfg.link_down_rc = 255;
6578 	t4_os_link_changed(pi);
6579 	PORT_UNLOCK(pi);
6580 
6581 	return (0);
6582 }
6583 
6584 /*
6585  * It is ok for this function to fail midway and return right away.  t4_detach
6586  * will walk the entire sc->irq list and clean up whatever is valid.
6587  */
6588 int
t4_setup_intr_handlers(struct adapter * sc)6589 t4_setup_intr_handlers(struct adapter *sc)
6590 {
6591 	int rc, rid, p, q, v;
6592 	char s[8];
6593 	struct irq *irq;
6594 	struct port_info *pi;
6595 	struct vi_info *vi;
6596 	struct sge *sge = &sc->sge;
6597 	struct sge_rxq *rxq;
6598 #ifdef TCP_OFFLOAD
6599 	struct sge_ofld_rxq *ofld_rxq;
6600 #endif
6601 #ifdef DEV_NETMAP
6602 	struct sge_nm_rxq *nm_rxq;
6603 #endif
6604 #ifdef RSS
6605 	int nbuckets = rss_getnumbuckets();
6606 #endif
6607 
6608 	/*
6609 	 * Setup interrupts.
6610 	 */
6611 	irq = &sc->irq[0];
6612 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6613 	if (forwarding_intr_to_fwq(sc))
6614 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6615 
6616 	/* Multiple interrupts. */
6617 	if (sc->flags & IS_VF)
6618 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6619 		    ("%s: too few intr.", __func__));
6620 	else
6621 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6622 		    ("%s: too few intr.", __func__));
6623 
6624 	/* The first one is always error intr on PFs */
6625 	if (!(sc->flags & IS_VF)) {
6626 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6627 		if (rc != 0)
6628 			return (rc);
6629 		irq++;
6630 		rid++;
6631 	}
6632 
6633 	/* The second one is always the firmware event queue (first on VFs) */
6634 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6635 	if (rc != 0)
6636 		return (rc);
6637 	irq++;
6638 	rid++;
6639 
6640 	for_each_port(sc, p) {
6641 		pi = sc->port[p];
6642 		for_each_vi(pi, v, vi) {
6643 			vi->first_intr = rid - 1;
6644 
6645 			if (vi->nnmrxq > 0) {
6646 				int n = max(vi->nrxq, vi->nnmrxq);
6647 
6648 				rxq = &sge->rxq[vi->first_rxq];
6649 #ifdef DEV_NETMAP
6650 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6651 #endif
6652 				for (q = 0; q < n; q++) {
6653 					snprintf(s, sizeof(s), "%x%c%x", p,
6654 					    'a' + v, q);
6655 					if (q < vi->nrxq)
6656 						irq->rxq = rxq++;
6657 #ifdef DEV_NETMAP
6658 					if (q < vi->nnmrxq)
6659 						irq->nm_rxq = nm_rxq++;
6660 
6661 					if (irq->nm_rxq != NULL &&
6662 					    irq->rxq == NULL) {
6663 						/* Netmap rx only */
6664 						rc = t4_alloc_irq(sc, irq, rid,
6665 						    t4_nm_intr, irq->nm_rxq, s);
6666 					}
6667 					if (irq->nm_rxq != NULL &&
6668 					    irq->rxq != NULL) {
6669 						/* NIC and Netmap rx */
6670 						rc = t4_alloc_irq(sc, irq, rid,
6671 						    t4_vi_intr, irq, s);
6672 					}
6673 #endif
6674 					if (irq->rxq != NULL &&
6675 					    irq->nm_rxq == NULL) {
6676 						/* NIC rx only */
6677 						rc = t4_alloc_irq(sc, irq, rid,
6678 						    t4_intr, irq->rxq, s);
6679 					}
6680 					if (rc != 0)
6681 						return (rc);
6682 #ifdef RSS
6683 					if (q < vi->nrxq) {
6684 						bus_bind_intr(sc->dev, irq->res,
6685 						    rss_getcpu(q % nbuckets));
6686 					}
6687 #endif
6688 					irq++;
6689 					rid++;
6690 					vi->nintr++;
6691 				}
6692 			} else {
6693 				for_each_rxq(vi, q, rxq) {
6694 					snprintf(s, sizeof(s), "%x%c%x", p,
6695 					    'a' + v, q);
6696 					rc = t4_alloc_irq(sc, irq, rid,
6697 					    t4_intr, rxq, s);
6698 					if (rc != 0)
6699 						return (rc);
6700 #ifdef RSS
6701 					bus_bind_intr(sc->dev, irq->res,
6702 					    rss_getcpu(q % nbuckets));
6703 #endif
6704 					irq++;
6705 					rid++;
6706 					vi->nintr++;
6707 				}
6708 			}
6709 #ifdef TCP_OFFLOAD
6710 			for_each_ofld_rxq(vi, q, ofld_rxq) {
6711 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
6712 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
6713 				    ofld_rxq, s);
6714 				if (rc != 0)
6715 					return (rc);
6716 				irq++;
6717 				rid++;
6718 				vi->nintr++;
6719 			}
6720 #endif
6721 		}
6722 	}
6723 	MPASS(irq == &sc->irq[sc->intr_count]);
6724 
6725 	return (0);
6726 }
6727 
6728 static void
write_global_rss_key(struct adapter * sc)6729 write_global_rss_key(struct adapter *sc)
6730 {
6731 #ifdef RSS
6732 	int i;
6733 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6734 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6735 
6736 	CTASSERT(RSS_KEYSIZE == 40);
6737 
6738 	rss_getkey((void *)&raw_rss_key[0]);
6739 	for (i = 0; i < nitems(rss_key); i++) {
6740 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
6741 	}
6742 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
6743 #endif
6744 }
6745 
6746 /*
6747  * Idempotent.
6748  */
6749 static int
adapter_full_init(struct adapter * sc)6750 adapter_full_init(struct adapter *sc)
6751 {
6752 	int rc, i;
6753 
6754 	ASSERT_SYNCHRONIZED_OP(sc);
6755 
6756 	/*
6757 	 * queues that belong to the adapter (not any particular port).
6758 	 */
6759 	rc = t4_setup_adapter_queues(sc);
6760 	if (rc != 0)
6761 		return (rc);
6762 
6763 	MPASS(sc->params.nports <= nitems(sc->tq));
6764 	for (i = 0; i < sc->params.nports; i++) {
6765 		if (sc->tq[i] != NULL)
6766 			continue;
6767 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
6768 		    taskqueue_thread_enqueue, &sc->tq[i]);
6769 		if (sc->tq[i] == NULL) {
6770 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
6771 			return (ENOMEM);
6772 		}
6773 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
6774 		    device_get_nameunit(sc->dev), i);
6775 	}
6776 
6777 	if (!(sc->flags & IS_VF)) {
6778 		write_global_rss_key(sc);
6779 		t4_intr_enable(sc);
6780 	}
6781 	return (0);
6782 }
6783 
6784 int
adapter_init(struct adapter * sc)6785 adapter_init(struct adapter *sc)
6786 {
6787 	int rc;
6788 
6789 	ASSERT_SYNCHRONIZED_OP(sc);
6790 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
6791 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
6792 	    ("%s: FULL_INIT_DONE already", __func__));
6793 
6794 	rc = adapter_full_init(sc);
6795 	if (rc != 0)
6796 		adapter_full_uninit(sc);
6797 	else
6798 		sc->flags |= FULL_INIT_DONE;
6799 
6800 	return (rc);
6801 }
6802 
6803 /*
6804  * Idempotent.
6805  */
6806 static void
adapter_full_uninit(struct adapter * sc)6807 adapter_full_uninit(struct adapter *sc)
6808 {
6809 	int i;
6810 
6811 	t4_teardown_adapter_queues(sc);
6812 
6813 	for (i = 0; i < nitems(sc->tq); i++) {
6814 		if (sc->tq[i] == NULL)
6815 			continue;
6816 		taskqueue_free(sc->tq[i]);
6817 		sc->tq[i] = NULL;
6818 	}
6819 
6820 	sc->flags &= ~FULL_INIT_DONE;
6821 }
6822 
6823 #ifdef RSS
6824 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
6825     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
6826     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
6827     RSS_HASHTYPE_RSS_UDP_IPV6)
6828 
6829 /* Translates kernel hash types to hardware. */
6830 static int
hashconfig_to_hashen(int hashconfig)6831 hashconfig_to_hashen(int hashconfig)
6832 {
6833 	int hashen = 0;
6834 
6835 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
6836 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
6837 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
6838 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
6839 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
6840 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6841 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6842 	}
6843 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
6844 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6845 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6846 	}
6847 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
6848 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6849 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
6850 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6851 
6852 	return (hashen);
6853 }
6854 
6855 /* Translates hardware hash types to kernel. */
6856 static int
hashen_to_hashconfig(int hashen)6857 hashen_to_hashconfig(int hashen)
6858 {
6859 	int hashconfig = 0;
6860 
6861 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
6862 		/*
6863 		 * If UDP hashing was enabled it must have been enabled for
6864 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
6865 		 * enabling any 4-tuple hash is nonsense configuration.
6866 		 */
6867 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6868 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
6869 
6870 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6871 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
6872 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6873 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
6874 	}
6875 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6876 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
6877 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6878 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
6879 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
6880 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
6881 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
6882 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
6883 
6884 	return (hashconfig);
6885 }
6886 #endif
6887 
6888 /*
6889  * Idempotent.
6890  */
6891 static int
vi_full_init(struct vi_info * vi)6892 vi_full_init(struct vi_info *vi)
6893 {
6894 	struct adapter *sc = vi->adapter;
6895 	struct sge_rxq *rxq;
6896 	int rc, i, j;
6897 #ifdef RSS
6898 	int nbuckets = rss_getnumbuckets();
6899 	int hashconfig = rss_gethashconfig();
6900 	int extra;
6901 #endif
6902 
6903 	ASSERT_SYNCHRONIZED_OP(sc);
6904 
6905 	/*
6906 	 * Allocate tx/rx/fl queues for this VI.
6907 	 */
6908 	rc = t4_setup_vi_queues(vi);
6909 	if (rc != 0)
6910 		return (rc);
6911 
6912 	/*
6913 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
6914 	 */
6915 	if (vi->nrxq > vi->rss_size) {
6916 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
6917 		    "some queues will never receive traffic.\n", vi->nrxq,
6918 		    vi->rss_size);
6919 	} else if (vi->rss_size % vi->nrxq) {
6920 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
6921 		    "expect uneven traffic distribution.\n", vi->nrxq,
6922 		    vi->rss_size);
6923 	}
6924 #ifdef RSS
6925 	if (vi->nrxq != nbuckets) {
6926 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
6927 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
6928 	}
6929 #endif
6930 	if (vi->rss == NULL)
6931 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
6932 		    M_ZERO | M_WAITOK);
6933 	for (i = 0; i < vi->rss_size;) {
6934 #ifdef RSS
6935 		j = rss_get_indirection_to_bucket(i);
6936 		j %= vi->nrxq;
6937 		rxq = &sc->sge.rxq[vi->first_rxq + j];
6938 		vi->rss[i++] = rxq->iq.abs_id;
6939 #else
6940 		for_each_rxq(vi, j, rxq) {
6941 			vi->rss[i++] = rxq->iq.abs_id;
6942 			if (i == vi->rss_size)
6943 				break;
6944 		}
6945 #endif
6946 	}
6947 
6948 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
6949 	    vi->rss, vi->rss_size);
6950 	if (rc != 0) {
6951 		CH_ERR(vi, "rss_config failed: %d\n", rc);
6952 		return (rc);
6953 	}
6954 
6955 #ifdef RSS
6956 	vi->hashen = hashconfig_to_hashen(hashconfig);
6957 
6958 	/*
6959 	 * We may have had to enable some hashes even though the global config
6960 	 * wants them disabled.  This is a potential problem that must be
6961 	 * reported to the user.
6962 	 */
6963 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
6964 
6965 	/*
6966 	 * If we consider only the supported hash types, then the enabled hashes
6967 	 * are a superset of the requested hashes.  In other words, there cannot
6968 	 * be any supported hash that was requested but not enabled, but there
6969 	 * can be hashes that were not requested but had to be enabled.
6970 	 */
6971 	extra &= SUPPORTED_RSS_HASHTYPES;
6972 	MPASS((extra & hashconfig) == 0);
6973 
6974 	if (extra) {
6975 		CH_ALERT(vi,
6976 		    "global RSS config (0x%x) cannot be accommodated.\n",
6977 		    hashconfig);
6978 	}
6979 	if (extra & RSS_HASHTYPE_RSS_IPV4)
6980 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
6981 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
6982 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
6983 	if (extra & RSS_HASHTYPE_RSS_IPV6)
6984 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
6985 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
6986 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
6987 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
6988 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
6989 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
6990 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
6991 #else
6992 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
6993 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
6994 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6995 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
6996 #endif
6997 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
6998 	    0, 0);
6999 	if (rc != 0) {
7000 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
7001 		return (rc);
7002 	}
7003 
7004 	return (0);
7005 }
7006 
7007 int
vi_init(struct vi_info * vi)7008 vi_init(struct vi_info *vi)
7009 {
7010 	int rc;
7011 
7012 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
7013 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
7014 	    ("%s: VI_INIT_DONE already", __func__));
7015 
7016 	rc = vi_full_init(vi);
7017 	if (rc != 0)
7018 		vi_full_uninit(vi);
7019 	else
7020 		vi->flags |= VI_INIT_DONE;
7021 
7022 	return (rc);
7023 }
7024 
7025 /*
7026  * Idempotent.
7027  */
7028 static void
vi_full_uninit(struct vi_info * vi)7029 vi_full_uninit(struct vi_info *vi)
7030 {
7031 
7032 	if (vi->flags & VI_INIT_DONE) {
7033 		quiesce_vi(vi);
7034 		free(vi->rss, M_CXGBE);
7035 		free(vi->nm_rss, M_CXGBE);
7036 	}
7037 
7038 	t4_teardown_vi_queues(vi);
7039 	vi->flags &= ~VI_INIT_DONE;
7040 }
7041 
7042 static void
quiesce_txq(struct sge_txq * txq)7043 quiesce_txq(struct sge_txq *txq)
7044 {
7045 	struct sge_eq *eq = &txq->eq;
7046 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7047 
7048 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7049 	MPASS(!(eq->flags & EQ_ENABLED));
7050 
7051 	/* Wait for the mp_ring to empty. */
7052 	while (!mp_ring_is_idle(txq->r)) {
7053 		mp_ring_check_drainage(txq->r, 4096);
7054 		pause("rquiesce", 1);
7055 	}
7056 	MPASS(txq->txp.npkt == 0);
7057 
7058 	if (eq->flags & EQ_HW_ALLOCATED) {
7059 		/*
7060 		 * Hardware is alive and working normally.  Wait for it to
7061 		 * finish and then wait for the driver to catch up and reclaim
7062 		 * all descriptors.
7063 		 */
7064 		while (spg->cidx != htobe16(eq->pidx))
7065 			pause("equiesce", 1);
7066 		while (eq->cidx != eq->pidx)
7067 			pause("dquiesce", 1);
7068 	} else {
7069 		/*
7070 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7071 		 * descriptors directly.
7072 		 */
7073 		TXQ_LOCK(txq);
7074 		while (eq->cidx != eq->pidx) {
7075 			struct mbuf *m, *nextpkt;
7076 			struct tx_sdesc *txsd;
7077 
7078 			txsd = &txq->sdesc[eq->cidx];
7079 			for (m = txsd->m; m != NULL; m = nextpkt) {
7080 				nextpkt = m->m_nextpkt;
7081 				m->m_nextpkt = NULL;
7082 				m_freem(m);
7083 			}
7084 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7085 		}
7086 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7087 		TXQ_UNLOCK(txq);
7088 	}
7089 }
7090 
7091 static void
quiesce_wrq(struct sge_wrq * wrq)7092 quiesce_wrq(struct sge_wrq *wrq)
7093 {
7094 	struct wrqe *wr;
7095 
7096 	TXQ_LOCK(wrq);
7097 	while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7098 		STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7099 #ifdef INVARIANTS
7100 		wrq->nwr_pending--;
7101 		wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7102 #endif
7103 		free(wr, M_CXGBE);
7104 	}
7105 	MPASS(wrq->nwr_pending == 0);
7106 	MPASS(wrq->ndesc_needed == 0);
7107 	wrq->nwr_pending = 0;
7108 	wrq->ndesc_needed = 0;
7109 	TXQ_UNLOCK(wrq);
7110 }
7111 
7112 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7113 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7114 {
7115 	/* Synchronize with the interrupt handler */
7116 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7117 		pause("iqfree", 1);
7118 
7119 	if (fl != NULL) {
7120 		MPASS(iq->flags & IQ_HAS_FL);
7121 
7122 		mtx_lock(&sc->sfl_lock);
7123 		FL_LOCK(fl);
7124 		fl->flags |= FL_DOOMED;
7125 		FL_UNLOCK(fl);
7126 		callout_stop(&sc->sfl_callout);
7127 		mtx_unlock(&sc->sfl_lock);
7128 
7129 		KASSERT((fl->flags & FL_STARVING) == 0,
7130 		    ("%s: still starving", __func__));
7131 
7132 		/* Release all buffers if hardware is no longer available. */
7133 		if (!(iq->flags & IQ_HW_ALLOCATED))
7134 			free_fl_buffers(sc, fl);
7135 	}
7136 }
7137 
7138 /*
7139  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7140  * that no new work is being enqueued by the hardware or the driver.  That part
7141  * should be arranged before calling this function.
7142  */
7143 static void
quiesce_vi(struct vi_info * vi)7144 quiesce_vi(struct vi_info *vi)
7145 {
7146 	int i;
7147 	struct adapter *sc = vi->adapter;
7148 	struct sge_rxq *rxq;
7149 	struct sge_txq *txq;
7150 #ifdef TCP_OFFLOAD
7151 	struct sge_ofld_rxq *ofld_rxq;
7152 #endif
7153 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7154 	struct sge_ofld_txq *ofld_txq;
7155 #endif
7156 
7157 	if (!(vi->flags & VI_INIT_DONE))
7158 		return;
7159 
7160 	for_each_txq(vi, i, txq) {
7161 		quiesce_txq(txq);
7162 	}
7163 
7164 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7165 	for_each_ofld_txq(vi, i, ofld_txq) {
7166 		quiesce_wrq(&ofld_txq->wrq);
7167 	}
7168 #endif
7169 
7170 	for_each_rxq(vi, i, rxq) {
7171 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7172 	}
7173 
7174 #ifdef TCP_OFFLOAD
7175 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7176 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7177 	}
7178 #endif
7179 }
7180 
7181 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7182 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7183     driver_intr_t *handler, void *arg, char *name)
7184 {
7185 	int rc;
7186 
7187 	irq->rid = rid;
7188 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7189 	    RF_SHAREABLE | RF_ACTIVE);
7190 	if (irq->res == NULL) {
7191 		device_printf(sc->dev,
7192 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7193 		return (ENOMEM);
7194 	}
7195 
7196 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7197 	    NULL, handler, arg, &irq->tag);
7198 	if (rc != 0) {
7199 		device_printf(sc->dev,
7200 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7201 		    rid, name, rc);
7202 	} else if (name)
7203 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7204 
7205 	return (rc);
7206 }
7207 
7208 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7209 t4_free_irq(struct adapter *sc, struct irq *irq)
7210 {
7211 	if (irq->tag)
7212 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7213 	if (irq->res)
7214 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7215 
7216 	bzero(irq, sizeof(*irq));
7217 
7218 	return (0);
7219 }
7220 
7221 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7222 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7223 {
7224 
7225 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7226 	t4_get_regs(sc, buf, regs->len);
7227 }
7228 
7229 #define	A_PL_INDIR_CMD	0x1f8
7230 
7231 #define	S_PL_AUTOINC	31
7232 #define	M_PL_AUTOINC	0x1U
7233 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7234 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7235 
7236 #define	S_PL_VFID	20
7237 #define	M_PL_VFID	0xffU
7238 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7239 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7240 
7241 #define	S_PL_ADDR	0
7242 #define	M_PL_ADDR	0xfffffU
7243 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7244 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7245 
7246 #define	A_PL_INDIR_DATA	0x1fc
7247 
7248 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7249 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7250 {
7251 	u32 stats[2];
7252 
7253 	if (sc->flags & IS_VF) {
7254 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7255 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7256 	} else {
7257 		mtx_assert(&sc->reg_lock, MA_OWNED);
7258 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7259 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7260 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7261 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7262 	}
7263 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7264 }
7265 
7266 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7267 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7268 {
7269 
7270 #define GET_STAT(name) \
7271 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7272 
7273 	if (!(sc->flags & IS_VF))
7274 		mtx_lock(&sc->reg_lock);
7275 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7276 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7277 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7278 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7279 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7280 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7281 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7282 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7283 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7284 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7285 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7286 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7287 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7288 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7289 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7290 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7291 	if (!(sc->flags & IS_VF))
7292 		mtx_unlock(&sc->reg_lock);
7293 
7294 #undef GET_STAT
7295 }
7296 
7297 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7298 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7299 {
7300 	int reg;
7301 
7302 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7303 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7304 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7305 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7306 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7307 }
7308 
7309 static void
vi_refresh_stats(struct vi_info * vi)7310 vi_refresh_stats(struct vi_info *vi)
7311 {
7312 	struct timeval tv;
7313 	const struct timeval interval = {0, 250000};	/* 250ms */
7314 
7315 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7316 
7317 	if (vi->flags & VI_SKIP_STATS)
7318 		return;
7319 
7320 	getmicrotime(&tv);
7321 	timevalsub(&tv, &interval);
7322 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7323 		return;
7324 
7325 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7326 	getmicrotime(&vi->last_refreshed);
7327 }
7328 
7329 static void
cxgbe_refresh_stats(struct vi_info * vi)7330 cxgbe_refresh_stats(struct vi_info *vi)
7331 {
7332 	u_int i, v, tnl_cong_drops, chan_map;
7333 	struct timeval tv;
7334 	const struct timeval interval = {0, 250000};	/* 250ms */
7335 	struct port_info *pi;
7336 	struct adapter *sc;
7337 
7338 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7339 
7340 	if (vi->flags & VI_SKIP_STATS)
7341 		return;
7342 
7343 	getmicrotime(&tv);
7344 	timevalsub(&tv, &interval);
7345 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7346 		return;
7347 
7348 	pi = vi->pi;
7349 	sc = vi->adapter;
7350 	tnl_cong_drops = 0;
7351 	t4_get_port_stats(sc, pi->port_id, &pi->stats);
7352 	chan_map = pi->rx_e_chan_map;
7353 	while (chan_map) {
7354 		i = ffs(chan_map) - 1;
7355 		mtx_lock(&sc->reg_lock);
7356 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7357 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7358 		mtx_unlock(&sc->reg_lock);
7359 		tnl_cong_drops += v;
7360 		chan_map &= ~(1 << i);
7361 	}
7362 	pi->tnl_cong_drops = tnl_cong_drops;
7363 	getmicrotime(&vi->last_refreshed);
7364 }
7365 
7366 static void
cxgbe_tick(void * arg)7367 cxgbe_tick(void *arg)
7368 {
7369 	struct vi_info *vi = arg;
7370 
7371 	MPASS(IS_MAIN_VI(vi));
7372 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7373 
7374 	cxgbe_refresh_stats(vi);
7375 	callout_schedule(&vi->tick, hz);
7376 }
7377 
7378 static void
vi_tick(void * arg)7379 vi_tick(void *arg)
7380 {
7381 	struct vi_info *vi = arg;
7382 
7383 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7384 
7385 	vi_refresh_stats(vi);
7386 	callout_schedule(&vi->tick, hz);
7387 }
7388 
7389 /*
7390  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7391  */
7392 static char *caps_decoder[] = {
7393 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7394 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7395 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7396 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7397 	    "\006HASHFILTER\007ETHOFLD",
7398 	"\20\001TOE",					/* 4: TOE */
7399 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
7400 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7401 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7402 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7403 	    "\007T10DIF"
7404 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7405 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7406 	    "\004TLS_HW",
7407 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7408 		    "\004PO_INITIATOR\005PO_TARGET",
7409 };
7410 
7411 void
t4_sysctls(struct adapter * sc)7412 t4_sysctls(struct adapter *sc)
7413 {
7414 	struct sysctl_ctx_list *ctx = &sc->ctx;
7415 	struct sysctl_oid *oid;
7416 	struct sysctl_oid_list *children, *c0;
7417 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7418 
7419 	/*
7420 	 * dev.t4nex.X.
7421 	 */
7422 	oid = device_get_sysctl_tree(sc->dev);
7423 	c0 = children = SYSCTL_CHILDREN(oid);
7424 
7425 	sc->sc_do_rxcopy = 1;
7426 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7427 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7428 
7429 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7430 	    sc->params.nports, "# of ports");
7431 
7432 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7433 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7434 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7435 	    "available doorbells");
7436 
7437 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7438 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7439 
7440 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7441 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7442 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7443 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7444 
7445 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7446 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7447 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7448 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7449 
7450 	t4_sge_sysctls(sc, ctx, children);
7451 
7452 	sc->lro_timeout = 100;
7453 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7454 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7455 
7456 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7457 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7458 
7459 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7460 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7461 
7462 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7463 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7464 
7465 	if (sc->flags & IS_VF)
7466 		return;
7467 
7468 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7469 	    NULL, chip_rev(sc), "chip hardware revision");
7470 
7471 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7472 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7473 
7474 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7475 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7476 
7477 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7478 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7479 
7480 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7481 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7482 
7483 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7484 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7485 
7486 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7487 	    sc->er_version, 0, "expansion ROM version");
7488 
7489 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7490 	    sc->bs_version, 0, "bootstrap firmware version");
7491 
7492 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7493 	    NULL, sc->params.scfg_vers, "serial config version");
7494 
7495 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7496 	    NULL, sc->params.vpd_vers, "VPD version");
7497 
7498 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7499 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7500 
7501 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7502 	    sc->cfcsum, "config file checksum");
7503 
7504 #define SYSCTL_CAP(name, n, text) \
7505 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7506 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7507 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7508 	    "available " text " capabilities")
7509 
7510 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7511 	SYSCTL_CAP(linkcaps, 1, "link");
7512 	SYSCTL_CAP(switchcaps, 2, "switch");
7513 	SYSCTL_CAP(niccaps, 3, "NIC");
7514 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7515 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7516 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7517 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7518 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7519 #undef SYSCTL_CAP
7520 
7521 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7522 	    NULL, sc->tids.nftids, "number of filters");
7523 
7524 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7525 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7526 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7527 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7528 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7529 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7530 
7531 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg",
7532 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7533 	    sysctl_loadavg, "A",
7534 	    "microprocessor load averages (debug firmwares only)");
7535 
7536 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7537 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7538 	    "I", "core Vdd (in mV)");
7539 
7540 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7541 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7542 	    sysctl_cpus, "A", "local CPUs");
7543 
7544 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7545 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7546 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7547 
7548 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7549 	    &sc->swintr, 0, "software triggered interrupts");
7550 
7551 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7552 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7553 	    "1 = reset adapter, 0 = zero reset counter");
7554 
7555 	/*
7556 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7557 	 */
7558 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7559 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
7560 	    "logs and miscellaneous information");
7561 	children = SYSCTL_CHILDREN(oid);
7562 
7563 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
7564 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7565 	    sysctl_cctrl, "A", "congestion control");
7566 
7567 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
7568 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7569 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
7570 
7571 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
7572 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7573 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
7574 
7575 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
7576 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7577 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
7578 
7579 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
7580 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 3,
7581 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
7582 
7583 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
7584 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 4,
7585 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
7586 
7587 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
7588 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 5,
7589 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
7590 
7591 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
7592 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7593 	    sysctl_cim_la, "A", "CIM logic analyzer");
7594 
7595 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
7596 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7597 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7598 
7599 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
7600 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7601 	    0 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
7602 
7603 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
7604 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7605 	    1 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
7606 
7607 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
7608 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7609 	    2 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
7610 
7611 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
7612 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7613 	    3 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
7614 
7615 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
7616 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7617 	    4 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
7618 
7619 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
7620 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7621 	    5 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
7622 
7623 	if (chip_id(sc) > CHELSIO_T4) {
7624 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
7625 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7626 		    6 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7627 		    "CIM OBQ 6 (SGE0-RX)");
7628 
7629 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
7630 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7631 		    7 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7632 		    "CIM OBQ 7 (SGE1-RX)");
7633 	}
7634 
7635 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
7636 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7637 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7638 
7639 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
7640 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7641 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
7642 
7643 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
7644 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7645 	    sysctl_cpl_stats, "A", "CPL statistics");
7646 
7647 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
7648 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7649 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
7650 
7651 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
7652 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7653 	    sysctl_tid_stats, "A", "tid stats");
7654 
7655 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
7656 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7657 	    sysctl_devlog, "A", "firmware's device log");
7658 
7659 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
7660 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7661 	    sysctl_fcoe_stats, "A", "FCoE statistics");
7662 
7663 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
7664 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7665 	    sysctl_hw_sched, "A", "hardware scheduler ");
7666 
7667 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
7668 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7669 	    sysctl_l2t, "A", "hardware L2 table");
7670 
7671 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
7672 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7673 	    sysctl_smt, "A", "hardware source MAC table");
7674 
7675 #ifdef INET6
7676 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
7677 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7678 	    sysctl_clip, "A", "active CLIP table entries");
7679 #endif
7680 
7681 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
7682 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7683 	    sysctl_lb_stats, "A", "loopback statistics");
7684 
7685 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
7686 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7687 	    sysctl_meminfo, "A", "memory regions");
7688 
7689 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
7690 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7691 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
7692 	    "A", "MPS TCAM entries");
7693 
7694 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
7695 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7696 	    sysctl_path_mtus, "A", "path MTUs");
7697 
7698 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
7699 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7700 	    sysctl_pm_stats, "A", "PM statistics");
7701 
7702 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
7703 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7704 	    sysctl_rdma_stats, "A", "RDMA statistics");
7705 
7706 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
7707 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7708 	    sysctl_tcp_stats, "A", "TCP statistics");
7709 
7710 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
7711 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7712 	    sysctl_tids, "A", "TID information");
7713 
7714 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
7715 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7716 	    sysctl_tp_err_stats, "A", "TP error statistics");
7717 
7718 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
7719 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7720 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
7721 
7722 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
7723 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7724 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
7725 
7726 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
7727 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7728 	    sysctl_tp_la, "A", "TP logic analyzer");
7729 
7730 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
7731 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7732 	    sysctl_tx_rate, "A", "Tx rate");
7733 
7734 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
7735 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7736 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
7737 
7738 	if (chip_id(sc) >= CHELSIO_T5) {
7739 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
7740 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7741 		    sysctl_wcwr_stats, "A", "write combined work requests");
7742 	}
7743 
7744 #ifdef KERN_TLS
7745 	if (is_ktls(sc)) {
7746 		/*
7747 		 * dev.t4nex.0.tls.
7748 		 */
7749 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
7750 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
7751 		children = SYSCTL_CHILDREN(oid);
7752 
7753 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
7754 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
7755 		    "keys in work requests (1) or attempt to store TLS keys "
7756 		    "in card memory.");
7757 
7758 		if (is_t6(sc))
7759 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
7760 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
7761 			    "combine TCB field updates with TLS record work "
7762 			    "requests.");
7763 	}
7764 #endif
7765 
7766 #ifdef TCP_OFFLOAD
7767 	if (is_offload(sc)) {
7768 		int i;
7769 		char s[4];
7770 
7771 		/*
7772 		 * dev.t4nex.X.toe.
7773 		 */
7774 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
7775 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
7776 		children = SYSCTL_CHILDREN(oid);
7777 
7778 		sc->tt.cong_algorithm = -1;
7779 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
7780 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
7781 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
7782 		    "3 = highspeed)");
7783 
7784 		sc->tt.sndbuf = -1;
7785 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
7786 		    &sc->tt.sndbuf, 0, "hardware send buffer");
7787 
7788 		sc->tt.ddp = 0;
7789 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
7790 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
7791 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
7792 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
7793 
7794 		sc->tt.rx_coalesce = -1;
7795 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
7796 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
7797 
7798 		sc->tt.tls = 0;
7799 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
7800 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
7801 		    "Inline TLS allowed");
7802 
7803 		sc->tt.tx_align = -1;
7804 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
7805 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
7806 
7807 		sc->tt.tx_zcopy = 0;
7808 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
7809 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
7810 		    "Enable zero-copy aio_write(2)");
7811 
7812 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
7813 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7814 		    "cop_managed_offloading", CTLFLAG_RW,
7815 		    &sc->tt.cop_managed_offloading, 0,
7816 		    "COP (Connection Offload Policy) controls all TOE offload");
7817 
7818 		sc->tt.autorcvbuf_inc = 16 * 1024;
7819 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
7820 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
7821 		    "autorcvbuf increment");
7822 
7823 		sc->tt.update_hc_on_pmtu_change = 1;
7824 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7825 		    "update_hc_on_pmtu_change", CTLFLAG_RW,
7826 		    &sc->tt.update_hc_on_pmtu_change, 0,
7827 		    "Update hostcache entry if the PMTU changes");
7828 
7829 		sc->tt.iso = 1;
7830 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
7831 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
7832 
7833 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
7834 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7835 		    sysctl_tp_tick, "A", "TP timer tick (us)");
7836 
7837 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
7838 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7839 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
7840 
7841 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
7842 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7843 		    sysctl_tp_tick, "A", "DACK tick (us)");
7844 
7845 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
7846 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7847 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
7848 
7849 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
7850 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7851 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
7852 		    "Minimum retransmit interval (us)");
7853 
7854 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
7855 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7856 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
7857 		    "Maximum retransmit interval (us)");
7858 
7859 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
7860 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7861 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
7862 		    "Persist timer min (us)");
7863 
7864 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
7865 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7866 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
7867 		    "Persist timer max (us)");
7868 
7869 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
7870 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7871 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
7872 		    "Keepalive idle timer (us)");
7873 
7874 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
7875 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7876 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
7877 		    "Keepalive interval timer (us)");
7878 
7879 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
7880 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7881 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
7882 
7883 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
7884 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7885 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
7886 		    "FINWAIT2 timer (us)");
7887 
7888 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
7889 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7890 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
7891 		    "Number of SYN retransmissions before abort");
7892 
7893 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
7894 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7895 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
7896 		    "Number of retransmissions before abort");
7897 
7898 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
7899 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7900 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
7901 		    "Number of keepalive probes before abort");
7902 
7903 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
7904 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7905 		    "TOE retransmit backoffs");
7906 		children = SYSCTL_CHILDREN(oid);
7907 		for (i = 0; i < 16; i++) {
7908 			snprintf(s, sizeof(s), "%u", i);
7909 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
7910 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7911 			    i, sysctl_tp_backoff, "IU",
7912 			    "TOE retransmit backoff");
7913 		}
7914 	}
7915 #endif
7916 }
7917 
7918 void
vi_sysctls(struct vi_info * vi)7919 vi_sysctls(struct vi_info *vi)
7920 {
7921 	struct sysctl_ctx_list *ctx = &vi->ctx;
7922 	struct sysctl_oid *oid;
7923 	struct sysctl_oid_list *children;
7924 
7925 	/*
7926 	 * dev.v?(cxgbe|cxl).X.
7927 	 */
7928 	oid = device_get_sysctl_tree(vi->dev);
7929 	children = SYSCTL_CHILDREN(oid);
7930 
7931 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
7932 	    vi->viid, "VI identifer");
7933 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
7934 	    &vi->nrxq, 0, "# of rx queues");
7935 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
7936 	    &vi->ntxq, 0, "# of tx queues");
7937 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
7938 	    &vi->first_rxq, 0, "index of first rx queue");
7939 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
7940 	    &vi->first_txq, 0, "index of first tx queue");
7941 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
7942 	    vi->rss_base, "start of RSS indirection table");
7943 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
7944 	    vi->rss_size, "size of RSS indirection table");
7945 
7946 	if (IS_MAIN_VI(vi)) {
7947 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
7948 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7949 		    sysctl_noflowq, "IU",
7950 		    "Reserve queue 0 for non-flowid packets");
7951 	}
7952 
7953 	if (vi->adapter->flags & IS_VF) {
7954 		MPASS(vi->flags & TX_USES_VM_WR);
7955 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
7956 		    NULL, 1, "use VM work requests for transmit");
7957 	} else {
7958 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
7959 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7960 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
7961 	}
7962 
7963 #ifdef TCP_OFFLOAD
7964 	if (vi->nofldrxq != 0) {
7965 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
7966 		    &vi->nofldrxq, 0,
7967 		    "# of rx queues for offloaded TCP connections");
7968 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
7969 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
7970 		    "index of first TOE rx queue");
7971 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
7972 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7973 		    sysctl_holdoff_tmr_idx_ofld, "I",
7974 		    "holdoff timer index for TOE queues");
7975 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
7976 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7977 		    sysctl_holdoff_pktc_idx_ofld, "I",
7978 		    "holdoff packet counter index for TOE queues");
7979 	}
7980 #endif
7981 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7982 	if (vi->nofldtxq != 0) {
7983 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
7984 		    &vi->nofldtxq, 0,
7985 		    "# of tx queues for TOE/ETHOFLD");
7986 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
7987 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
7988 		    "index of first TOE/ETHOFLD tx queue");
7989 	}
7990 #endif
7991 #ifdef DEV_NETMAP
7992 	if (vi->nnmrxq != 0) {
7993 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
7994 		    &vi->nnmrxq, 0, "# of netmap rx queues");
7995 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
7996 		    &vi->nnmtxq, 0, "# of netmap tx queues");
7997 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
7998 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
7999 		    "index of first netmap rx queue");
8000 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
8001 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
8002 		    "index of first netmap tx queue");
8003 	}
8004 #endif
8005 
8006 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
8007 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8008 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
8009 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
8010 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8011 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
8012 
8013 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
8014 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8015 	    sysctl_qsize_rxq, "I", "rx queue size");
8016 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
8017 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8018 	    sysctl_qsize_txq, "I", "tx queue size");
8019 }
8020 
8021 static void
cxgbe_sysctls(struct port_info * pi)8022 cxgbe_sysctls(struct port_info *pi)
8023 {
8024 	struct sysctl_ctx_list *ctx = &pi->ctx;
8025 	struct sysctl_oid *oid;
8026 	struct sysctl_oid_list *children, *children2;
8027 	struct adapter *sc = pi->adapter;
8028 	int i;
8029 	char name[16];
8030 	static char *tc_flags = {"\20\1USER"};
8031 
8032 	/*
8033 	 * dev.cxgbe.X.
8034 	 */
8035 	oid = device_get_sysctl_tree(pi->dev);
8036 	children = SYSCTL_CHILDREN(oid);
8037 
8038 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8039 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8040 	    sysctl_linkdnrc, "A", "reason why link is down");
8041 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8042 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8043 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8044 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8045 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8046 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8047 		    sysctl_btphy, "I", "PHY firmware version");
8048 	}
8049 
8050 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8051 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8052 	    sysctl_pause_settings, "A",
8053 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8054 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8055 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8056 	    "FEC in use on the link");
8057 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8058 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8059 	    sysctl_requested_fec, "A",
8060 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8061 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8062 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8063 	    "FEC recommended by the cable/transceiver");
8064 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8065 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8066 	    sysctl_autoneg, "I",
8067 	    "autonegotiation (-1 = not supported)");
8068 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8069 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8070 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8071 
8072 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8073 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8074 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8075 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8076 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8077 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8078 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8079 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8080 
8081 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8082 	    port_top_speed(pi), "max speed (in Gbps)");
8083 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8084 	    pi->mps_bg_map, "MPS buffer group map");
8085 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8086 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8087 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8088 	    pi->tx_chan, "TP tx c-channel");
8089 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8090 	    pi->rx_chan, "TP rx c-channel");
8091 
8092 	if (sc->flags & IS_VF)
8093 		return;
8094 
8095 	/*
8096 	 * dev.(cxgbe|cxl).X.tc.
8097 	 */
8098 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8099 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8100 	    "Tx scheduler traffic classes (cl_rl)");
8101 	children2 = SYSCTL_CHILDREN(oid);
8102 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8103 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8104 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8105 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8106 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8107 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8108 	for (i = 0; i < sc->params.nsched_cls; i++) {
8109 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8110 
8111 		snprintf(name, sizeof(name), "%d", i);
8112 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8113 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8114 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8115 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8116 		    CTLFLAG_RD, &tc->state, 0, "current state");
8117 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8118 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8119 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8120 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8121 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8122 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8123 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8124 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8125 		    "traffic class parameters");
8126 	}
8127 
8128 	/*
8129 	 * dev.cxgbe.X.stats.
8130 	 */
8131 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8132 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8133 	children = SYSCTL_CHILDREN(oid);
8134 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8135 	    &pi->tx_parse_error, 0,
8136 	    "# of tx packets with invalid length or # of segments");
8137 
8138 #define T4_REGSTAT(name, stat, desc) \
8139     SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8140 	CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8141 	t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8142         sysctl_handle_t4_reg64, "QU", desc)
8143 
8144 /* We get these from port_stats and they may be stale by up to 1s */
8145 #define T4_PORTSTAT(name, desc) \
8146 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
8147 	    &pi->stats.name, desc)
8148 
8149 	T4_REGSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8150 	T4_REGSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8151 	T4_REGSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8152 	T4_REGSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8153 	T4_REGSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8154 	T4_REGSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8155 	T4_REGSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8156 	T4_REGSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8157 	T4_REGSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8158 	T4_REGSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8159 	T4_REGSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8160 	T4_REGSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8161 	T4_REGSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8162 	T4_REGSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8163 	T4_REGSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8164 	T4_REGSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8165 	T4_REGSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8166 	T4_REGSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8167 	T4_REGSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8168 	T4_REGSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8169 	T4_REGSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8170 	T4_REGSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8171 	T4_REGSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8172 
8173 	T4_REGSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8174 	T4_REGSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8175 	T4_REGSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8176 	T4_REGSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8177 	T4_REGSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8178 	T4_REGSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8179 	T4_REGSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8180 	if (is_t6(sc)) {
8181 		T4_PORTSTAT(rx_fcs_err,
8182 		    "# of frames received with bad FCS since last link up");
8183 	} else {
8184 		T4_REGSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8185 		    "# of frames received with bad FCS");
8186 	}
8187 	T4_REGSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8188 	T4_REGSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8189 	T4_REGSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8190 	T4_REGSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8191 	T4_REGSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8192 	T4_REGSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8193 	T4_REGSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8194 	T4_REGSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8195 	T4_REGSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8196 	T4_REGSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8197 	T4_REGSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8198 	T4_REGSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8199 	T4_REGSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8200 	T4_REGSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8201 	T4_REGSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8202 	T4_REGSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8203 	T4_REGSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8204 	T4_REGSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8205 	T4_REGSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8206 
8207 	T4_PORTSTAT(rx_ovflow0, "# drops due to buffer-group 0 overflows");
8208 	T4_PORTSTAT(rx_ovflow1, "# drops due to buffer-group 1 overflows");
8209 	T4_PORTSTAT(rx_ovflow2, "# drops due to buffer-group 2 overflows");
8210 	T4_PORTSTAT(rx_ovflow3, "# drops due to buffer-group 3 overflows");
8211 	T4_PORTSTAT(rx_trunc0, "# of buffer-group 0 truncated packets");
8212 	T4_PORTSTAT(rx_trunc1, "# of buffer-group 1 truncated packets");
8213 	T4_PORTSTAT(rx_trunc2, "# of buffer-group 2 truncated packets");
8214 	T4_PORTSTAT(rx_trunc3, "# of buffer-group 3 truncated packets");
8215 
8216 #undef T4_REGSTAT
8217 #undef T4_PORTSTAT
8218 }
8219 
8220 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8221 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8222 {
8223 	int rc, *i, space = 0;
8224 	struct sbuf sb;
8225 
8226 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8227 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8228 		if (space)
8229 			sbuf_printf(&sb, " ");
8230 		sbuf_printf(&sb, "%d", *i);
8231 		space = 1;
8232 	}
8233 	rc = sbuf_finish(&sb);
8234 	sbuf_delete(&sb);
8235 	return (rc);
8236 }
8237 
8238 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8239 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8240 {
8241 	int rc;
8242 	struct sbuf *sb;
8243 
8244 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8245 	if (sb == NULL)
8246 		return (ENOMEM);
8247 
8248 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8249 	rc = sbuf_finish(sb);
8250 	sbuf_delete(sb);
8251 
8252 	return (rc);
8253 }
8254 
8255 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8256 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8257 {
8258 	int rc;
8259 	struct sbuf *sb;
8260 
8261 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8262 	if (sb == NULL)
8263 		return (ENOMEM);
8264 
8265 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8266 	rc = sbuf_finish(sb);
8267 	sbuf_delete(sb);
8268 
8269 	return (rc);
8270 }
8271 
8272 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8273 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8274 {
8275 	struct port_info *pi = arg1;
8276 	int op = arg2;
8277 	struct adapter *sc = pi->adapter;
8278 	u_int v;
8279 	int rc;
8280 
8281 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8282 	if (rc)
8283 		return (rc);
8284 	if (hw_off_limits(sc))
8285 		rc = ENXIO;
8286 	else {
8287 		/* XXX: magic numbers */
8288 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8289 		    op ? 0x20 : 0xc820, &v);
8290 	}
8291 	end_synchronized_op(sc, 0);
8292 	if (rc)
8293 		return (rc);
8294 	if (op == 0)
8295 		v /= 256;
8296 
8297 	rc = sysctl_handle_int(oidp, &v, 0, req);
8298 	return (rc);
8299 }
8300 
8301 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8302 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8303 {
8304 	struct vi_info *vi = arg1;
8305 	int rc, val;
8306 
8307 	val = vi->rsrv_noflowq;
8308 	rc = sysctl_handle_int(oidp, &val, 0, req);
8309 	if (rc != 0 || req->newptr == NULL)
8310 		return (rc);
8311 
8312 	if ((val >= 1) && (vi->ntxq > 1))
8313 		vi->rsrv_noflowq = 1;
8314 	else
8315 		vi->rsrv_noflowq = 0;
8316 
8317 	return (rc);
8318 }
8319 
8320 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8321 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8322 {
8323 	struct vi_info *vi = arg1;
8324 	struct adapter *sc = vi->adapter;
8325 	int rc, val, i;
8326 
8327 	MPASS(!(sc->flags & IS_VF));
8328 
8329 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8330 	rc = sysctl_handle_int(oidp, &val, 0, req);
8331 	if (rc != 0 || req->newptr == NULL)
8332 		return (rc);
8333 
8334 	if (val != 0 && val != 1)
8335 		return (EINVAL);
8336 
8337 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8338 	    "t4txvm");
8339 	if (rc)
8340 		return (rc);
8341 	if (hw_off_limits(sc))
8342 		rc = ENXIO;
8343 	else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8344 		/*
8345 		 * We don't want parse_pkt to run with one setting (VF or PF)
8346 		 * and then eth_tx to see a different setting but still use
8347 		 * stale information calculated by parse_pkt.
8348 		 */
8349 		rc = EBUSY;
8350 	} else {
8351 		struct port_info *pi = vi->pi;
8352 		struct sge_txq *txq;
8353 		uint32_t ctrl0;
8354 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8355 
8356 		if (val) {
8357 			vi->flags |= TX_USES_VM_WR;
8358 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8359 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8360 			    V_TXPKT_INTF(pi->tx_chan));
8361 			if (!(sc->flags & IS_VF))
8362 				npkt--;
8363 		} else {
8364 			vi->flags &= ~TX_USES_VM_WR;
8365 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8366 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8367 			    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
8368 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8369 		}
8370 		for_each_txq(vi, i, txq) {
8371 			txq->cpl_ctrl0 = ctrl0;
8372 			txq->txp.max_npkt = npkt;
8373 		}
8374 	}
8375 	end_synchronized_op(sc, LOCK_HELD);
8376 	return (rc);
8377 }
8378 
8379 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8380 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8381 {
8382 	struct vi_info *vi = arg1;
8383 	struct adapter *sc = vi->adapter;
8384 	int idx, rc, i;
8385 	struct sge_rxq *rxq;
8386 	uint8_t v;
8387 
8388 	idx = vi->tmr_idx;
8389 
8390 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8391 	if (rc != 0 || req->newptr == NULL)
8392 		return (rc);
8393 
8394 	if (idx < 0 || idx >= SGE_NTIMERS)
8395 		return (EINVAL);
8396 
8397 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8398 	    "t4tmr");
8399 	if (rc)
8400 		return (rc);
8401 
8402 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8403 	for_each_rxq(vi, i, rxq) {
8404 #ifdef atomic_store_rel_8
8405 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8406 #else
8407 		rxq->iq.intr_params = v;
8408 #endif
8409 	}
8410 	vi->tmr_idx = idx;
8411 
8412 	end_synchronized_op(sc, LOCK_HELD);
8413 	return (0);
8414 }
8415 
8416 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8417 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8418 {
8419 	struct vi_info *vi = arg1;
8420 	struct adapter *sc = vi->adapter;
8421 	int idx, rc;
8422 
8423 	idx = vi->pktc_idx;
8424 
8425 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8426 	if (rc != 0 || req->newptr == NULL)
8427 		return (rc);
8428 
8429 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8430 		return (EINVAL);
8431 
8432 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8433 	    "t4pktc");
8434 	if (rc)
8435 		return (rc);
8436 
8437 	if (vi->flags & VI_INIT_DONE)
8438 		rc = EBUSY; /* cannot be changed once the queues are created */
8439 	else
8440 		vi->pktc_idx = idx;
8441 
8442 	end_synchronized_op(sc, LOCK_HELD);
8443 	return (rc);
8444 }
8445 
8446 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8447 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8448 {
8449 	struct vi_info *vi = arg1;
8450 	struct adapter *sc = vi->adapter;
8451 	int qsize, rc;
8452 
8453 	qsize = vi->qsize_rxq;
8454 
8455 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8456 	if (rc != 0 || req->newptr == NULL)
8457 		return (rc);
8458 
8459 	if (qsize < 128 || (qsize & 7))
8460 		return (EINVAL);
8461 
8462 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8463 	    "t4rxqs");
8464 	if (rc)
8465 		return (rc);
8466 
8467 	if (vi->flags & VI_INIT_DONE)
8468 		rc = EBUSY; /* cannot be changed once the queues are created */
8469 	else
8470 		vi->qsize_rxq = qsize;
8471 
8472 	end_synchronized_op(sc, LOCK_HELD);
8473 	return (rc);
8474 }
8475 
8476 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8477 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8478 {
8479 	struct vi_info *vi = arg1;
8480 	struct adapter *sc = vi->adapter;
8481 	int qsize, rc;
8482 
8483 	qsize = vi->qsize_txq;
8484 
8485 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8486 	if (rc != 0 || req->newptr == NULL)
8487 		return (rc);
8488 
8489 	if (qsize < 128 || qsize > 65536)
8490 		return (EINVAL);
8491 
8492 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8493 	    "t4txqs");
8494 	if (rc)
8495 		return (rc);
8496 
8497 	if (vi->flags & VI_INIT_DONE)
8498 		rc = EBUSY; /* cannot be changed once the queues are created */
8499 	else
8500 		vi->qsize_txq = qsize;
8501 
8502 	end_synchronized_op(sc, LOCK_HELD);
8503 	return (rc);
8504 }
8505 
8506 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8507 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8508 {
8509 	struct port_info *pi = arg1;
8510 	struct adapter *sc = pi->adapter;
8511 	struct link_config *lc = &pi->link_cfg;
8512 	int rc;
8513 
8514 	if (req->newptr == NULL) {
8515 		struct sbuf *sb;
8516 		static char *bits = "\20\1RX\2TX\3AUTO";
8517 
8518 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8519 		if (sb == NULL)
8520 			return (ENOMEM);
8521 
8522 		if (lc->link_ok) {
8523 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8524 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8525 		} else {
8526 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8527 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8528 		}
8529 		rc = sbuf_finish(sb);
8530 		sbuf_delete(sb);
8531 	} else {
8532 		char s[2];
8533 		int n;
8534 
8535 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8536 		    PAUSE_AUTONEG));
8537 		s[1] = 0;
8538 
8539 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8540 		if (rc != 0)
8541 			return(rc);
8542 
8543 		if (s[1] != 0)
8544 			return (EINVAL);
8545 		if (s[0] < '0' || s[0] > '9')
8546 			return (EINVAL);	/* not a number */
8547 		n = s[0] - '0';
8548 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8549 			return (EINVAL);	/* some other bit is set too */
8550 
8551 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8552 		    "t4PAUSE");
8553 		if (rc)
8554 			return (rc);
8555 		if (!hw_off_limits(sc)) {
8556 			PORT_LOCK(pi);
8557 			lc->requested_fc = n;
8558 			fixup_link_config(pi);
8559 			if (pi->up_vis > 0)
8560 				rc = apply_link_config(pi);
8561 			set_current_media(pi);
8562 			PORT_UNLOCK(pi);
8563 		}
8564 		end_synchronized_op(sc, 0);
8565 	}
8566 
8567 	return (rc);
8568 }
8569 
8570 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8571 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8572 {
8573 	struct port_info *pi = arg1;
8574 	struct link_config *lc = &pi->link_cfg;
8575 	int rc;
8576 	struct sbuf *sb;
8577 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2";
8578 
8579 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8580 	if (sb == NULL)
8581 		return (ENOMEM);
8582 	if (lc->link_ok)
8583 		sbuf_printf(sb, "%b", lc->fec, bits);
8584 	else
8585 		sbuf_printf(sb, "no link");
8586 	rc = sbuf_finish(sb);
8587 	sbuf_delete(sb);
8588 
8589 	return (rc);
8590 }
8591 
8592 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)8593 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8594 {
8595 	struct port_info *pi = arg1;
8596 	struct adapter *sc = pi->adapter;
8597 	struct link_config *lc = &pi->link_cfg;
8598 	int rc;
8599 	int8_t old;
8600 
8601 	if (req->newptr == NULL) {
8602 		struct sbuf *sb;
8603 		static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2"
8604 		    "\5RSVD3\6auto\7module";
8605 
8606 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8607 		if (sb == NULL)
8608 			return (ENOMEM);
8609 
8610 		sbuf_printf(sb, "%b", lc->requested_fec, bits);
8611 		rc = sbuf_finish(sb);
8612 		sbuf_delete(sb);
8613 	} else {
8614 		char s[8];
8615 		int n;
8616 
8617 		snprintf(s, sizeof(s), "%d",
8618 		    lc->requested_fec == FEC_AUTO ? -1 :
8619 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
8620 
8621 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8622 		if (rc != 0)
8623 			return(rc);
8624 
8625 		n = strtol(&s[0], NULL, 0);
8626 		if (n < 0 || n & FEC_AUTO)
8627 			n = FEC_AUTO;
8628 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
8629 			return (EINVAL);/* some other bit is set too */
8630 
8631 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8632 		    "t4reqf");
8633 		if (rc)
8634 			return (rc);
8635 		PORT_LOCK(pi);
8636 		old = lc->requested_fec;
8637 		if (n == FEC_AUTO)
8638 			lc->requested_fec = FEC_AUTO;
8639 		else if (n == 0 || n == FEC_NONE)
8640 			lc->requested_fec = FEC_NONE;
8641 		else {
8642 			if ((lc->pcaps |
8643 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
8644 			    lc->pcaps) {
8645 				rc = ENOTSUP;
8646 				goto done;
8647 			}
8648 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
8649 			    FEC_MODULE);
8650 		}
8651 		if (!hw_off_limits(sc)) {
8652 			fixup_link_config(pi);
8653 			if (pi->up_vis > 0) {
8654 				rc = apply_link_config(pi);
8655 				if (rc != 0) {
8656 					lc->requested_fec = old;
8657 					if (rc == FW_EPROTO)
8658 						rc = ENOTSUP;
8659 				}
8660 			}
8661 		}
8662 done:
8663 		PORT_UNLOCK(pi);
8664 		end_synchronized_op(sc, 0);
8665 	}
8666 
8667 	return (rc);
8668 }
8669 
8670 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)8671 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
8672 {
8673 	struct port_info *pi = arg1;
8674 	struct adapter *sc = pi->adapter;
8675 	struct link_config *lc = &pi->link_cfg;
8676 	int rc;
8677 	int8_t fec;
8678 	struct sbuf *sb;
8679 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2\5RSVD3";
8680 
8681 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8682 	if (sb == NULL)
8683 		return (ENOMEM);
8684 
8685 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
8686 		rc = EBUSY;
8687 		goto done;
8688 	}
8689 	if (hw_off_limits(sc)) {
8690 		rc = ENXIO;
8691 		goto done;
8692 	}
8693 	PORT_LOCK(pi);
8694 	if (pi->up_vis == 0) {
8695 		/*
8696 		 * If all the interfaces are administratively down the firmware
8697 		 * does not report transceiver changes.  Refresh port info here.
8698 		 * This is the only reason we have a synchronized op in this
8699 		 * function.  Just PORT_LOCK would have been enough otherwise.
8700 		 */
8701 		t4_update_port_info(pi);
8702 	}
8703 
8704 	fec = lc->fec_hint;
8705 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
8706 	    !fec_supported(lc->pcaps)) {
8707 		PORT_UNLOCK(pi);
8708 		sbuf_printf(sb, "n/a");
8709 	} else {
8710 		if (fec == 0)
8711 			fec = FEC_NONE;
8712 		PORT_UNLOCK(pi);
8713 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, bits);
8714 	}
8715 	rc = sbuf_finish(sb);
8716 done:
8717 	sbuf_delete(sb);
8718 	end_synchronized_op(sc, 0);
8719 
8720 	return (rc);
8721 }
8722 
8723 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)8724 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
8725 {
8726 	struct port_info *pi = arg1;
8727 	struct adapter *sc = pi->adapter;
8728 	struct link_config *lc = &pi->link_cfg;
8729 	int rc, val;
8730 
8731 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
8732 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
8733 	else
8734 		val = -1;
8735 	rc = sysctl_handle_int(oidp, &val, 0, req);
8736 	if (rc != 0 || req->newptr == NULL)
8737 		return (rc);
8738 	if (val == 0)
8739 		val = AUTONEG_DISABLE;
8740 	else if (val == 1)
8741 		val = AUTONEG_ENABLE;
8742 	else
8743 		val = AUTONEG_AUTO;
8744 
8745 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8746 	    "t4aneg");
8747 	if (rc)
8748 		return (rc);
8749 	PORT_LOCK(pi);
8750 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
8751 		rc = ENOTSUP;
8752 		goto done;
8753 	}
8754 	lc->requested_aneg = val;
8755 	if (!hw_off_limits(sc)) {
8756 		fixup_link_config(pi);
8757 		if (pi->up_vis > 0)
8758 			rc = apply_link_config(pi);
8759 		set_current_media(pi);
8760 	}
8761 done:
8762 	PORT_UNLOCK(pi);
8763 	end_synchronized_op(sc, 0);
8764 	return (rc);
8765 }
8766 
8767 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)8768 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
8769 {
8770 	struct port_info *pi = arg1;
8771 	struct adapter *sc = pi->adapter;
8772 	struct link_config *lc = &pi->link_cfg;
8773 	int rc, val;
8774 
8775 	val = lc->force_fec;
8776 	MPASS(val >= -1 && val <= 1);
8777 	rc = sysctl_handle_int(oidp, &val, 0, req);
8778 	if (rc != 0 || req->newptr == NULL)
8779 		return (rc);
8780 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
8781 		return (ENOTSUP);
8782 	if (val < -1 || val > 1)
8783 		return (EINVAL);
8784 
8785 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
8786 	if (rc)
8787 		return (rc);
8788 	PORT_LOCK(pi);
8789 	lc->force_fec = val;
8790 	if (!hw_off_limits(sc)) {
8791 		fixup_link_config(pi);
8792 		if (pi->up_vis > 0)
8793 			rc = apply_link_config(pi);
8794 	}
8795 	PORT_UNLOCK(pi);
8796 	end_synchronized_op(sc, 0);
8797 	return (rc);
8798 }
8799 
8800 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)8801 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
8802 {
8803 	struct adapter *sc = arg1;
8804 	int rc, reg = arg2;
8805 	uint64_t val;
8806 
8807 	mtx_lock(&sc->reg_lock);
8808 	if (hw_off_limits(sc))
8809 		rc = ENXIO;
8810 	else {
8811 		rc = 0;
8812 		val = t4_read_reg64(sc, reg);
8813 	}
8814 	mtx_unlock(&sc->reg_lock);
8815 	if (rc == 0)
8816 		rc = sysctl_handle_64(oidp, &val, 0, req);
8817 	return (rc);
8818 }
8819 
8820 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)8821 sysctl_temperature(SYSCTL_HANDLER_ARGS)
8822 {
8823 	struct adapter *sc = arg1;
8824 	int rc, t;
8825 	uint32_t param, val;
8826 
8827 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
8828 	if (rc)
8829 		return (rc);
8830 	if (hw_off_limits(sc))
8831 		rc = ENXIO;
8832 	else {
8833 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8834 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8835 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
8836 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8837 	}
8838 	end_synchronized_op(sc, 0);
8839 	if (rc)
8840 		return (rc);
8841 
8842 	/* unknown is returned as 0 but we display -1 in that case */
8843 	t = val == 0 ? -1 : val;
8844 
8845 	rc = sysctl_handle_int(oidp, &t, 0, req);
8846 	return (rc);
8847 }
8848 
8849 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)8850 sysctl_vdd(SYSCTL_HANDLER_ARGS)
8851 {
8852 	struct adapter *sc = arg1;
8853 	int rc;
8854 	uint32_t param, val;
8855 
8856 	if (sc->params.core_vdd == 0) {
8857 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8858 		    "t4vdd");
8859 		if (rc)
8860 			return (rc);
8861 		if (hw_off_limits(sc))
8862 			rc = ENXIO;
8863 		else {
8864 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8865 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8866 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
8867 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
8868 			    &param, &val);
8869 		}
8870 		end_synchronized_op(sc, 0);
8871 		if (rc)
8872 			return (rc);
8873 		sc->params.core_vdd = val;
8874 	}
8875 
8876 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
8877 }
8878 
8879 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)8880 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
8881 {
8882 	struct adapter *sc = arg1;
8883 	int rc, v;
8884 	uint32_t param, val;
8885 
8886 	v = sc->sensor_resets;
8887 	rc = sysctl_handle_int(oidp, &v, 0, req);
8888 	if (rc != 0 || req->newptr == NULL || v <= 0)
8889 		return (rc);
8890 
8891 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
8892 	    chip_id(sc) < CHELSIO_T5)
8893 		return (ENOTSUP);
8894 
8895 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
8896 	if (rc)
8897 		return (rc);
8898 	if (hw_off_limits(sc))
8899 		rc = ENXIO;
8900 	else {
8901 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8902 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8903 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
8904 		val = 1;
8905 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8906 	}
8907 	end_synchronized_op(sc, 0);
8908 	if (rc == 0)
8909 		sc->sensor_resets++;
8910 	return (rc);
8911 }
8912 
8913 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)8914 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
8915 {
8916 	struct adapter *sc = arg1;
8917 	struct sbuf *sb;
8918 	int rc;
8919 	uint32_t param, val;
8920 
8921 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
8922 	if (rc)
8923 		return (rc);
8924 	if (hw_off_limits(sc))
8925 		rc = ENXIO;
8926 	else {
8927 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8928 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
8929 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8930 	}
8931 	end_synchronized_op(sc, 0);
8932 	if (rc)
8933 		return (rc);
8934 
8935 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8936 	if (sb == NULL)
8937 		return (ENOMEM);
8938 
8939 	if (val == 0xffffffff) {
8940 		/* Only debug and custom firmwares report load averages. */
8941 		sbuf_printf(sb, "not available");
8942 	} else {
8943 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
8944 		    (val >> 16) & 0xff);
8945 	}
8946 	rc = sbuf_finish(sb);
8947 	sbuf_delete(sb);
8948 
8949 	return (rc);
8950 }
8951 
8952 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)8953 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
8954 {
8955 	struct adapter *sc = arg1;
8956 	struct sbuf *sb;
8957 	int rc, i;
8958 	uint16_t incr[NMTUS][NCCTRL_WIN];
8959 	static const char *dec_fac[] = {
8960 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
8961 		"0.9375"
8962 	};
8963 
8964 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8965 	if (sb == NULL)
8966 		return (ENOMEM);
8967 
8968 	rc = 0;
8969 	mtx_lock(&sc->reg_lock);
8970 	if (hw_off_limits(sc))
8971 		rc = ENXIO;
8972 	else
8973 		t4_read_cong_tbl(sc, incr);
8974 	mtx_unlock(&sc->reg_lock);
8975 	if (rc)
8976 		goto done;
8977 
8978 	for (i = 0; i < NCCTRL_WIN; ++i) {
8979 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
8980 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
8981 		    incr[5][i], incr[6][i], incr[7][i]);
8982 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
8983 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
8984 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
8985 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
8986 	}
8987 
8988 	rc = sbuf_finish(sb);
8989 done:
8990 	sbuf_delete(sb);
8991 	return (rc);
8992 }
8993 
8994 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
8995 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
8996 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
8997 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
8998 };
8999 
9000 static int
sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)9001 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
9002 {
9003 	struct adapter *sc = arg1;
9004 	struct sbuf *sb;
9005 	int rc, i, n, qid = arg2;
9006 	uint32_t *buf, *p;
9007 	char *qtype;
9008 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
9009 
9010 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
9011 	    ("%s: bad qid %d\n", __func__, qid));
9012 
9013 	if (qid < CIM_NUM_IBQ) {
9014 		/* inbound queue */
9015 		qtype = "IBQ";
9016 		n = 4 * CIM_IBQ_SIZE;
9017 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9018 		mtx_lock(&sc->reg_lock);
9019 		if (hw_off_limits(sc))
9020 			rc = -ENXIO;
9021 		else
9022 			rc = t4_read_cim_ibq(sc, qid, buf, n);
9023 		mtx_unlock(&sc->reg_lock);
9024 	} else {
9025 		/* outbound queue */
9026 		qtype = "OBQ";
9027 		qid -= CIM_NUM_IBQ;
9028 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
9029 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9030 		mtx_lock(&sc->reg_lock);
9031 		if (hw_off_limits(sc))
9032 			rc = -ENXIO;
9033 		else
9034 			rc = t4_read_cim_obq(sc, qid, buf, n);
9035 		mtx_unlock(&sc->reg_lock);
9036 	}
9037 
9038 	if (rc < 0) {
9039 		rc = -rc;
9040 		goto done;
9041 	}
9042 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9043 
9044 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9045 	if (sb == NULL) {
9046 		rc = ENOMEM;
9047 		goto done;
9048 	}
9049 
9050 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
9051 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9052 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9053 		    p[2], p[3]);
9054 
9055 	rc = sbuf_finish(sb);
9056 	sbuf_delete(sb);
9057 done:
9058 	free(buf, M_CXGBE);
9059 	return (rc);
9060 }
9061 
9062 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9063 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9064 {
9065 	uint32_t *p;
9066 
9067 	sbuf_printf(sb, "Status   Data      PC%s",
9068 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9069 	    "     LS0Stat  LS0Addr             LS0Data");
9070 
9071 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9072 		if (cfg & F_UPDBGLACAPTPCONLY) {
9073 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9074 			    p[6], p[7]);
9075 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9076 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9077 			    p[4] & 0xff, p[5] >> 8);
9078 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9079 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9080 			    p[1] & 0xf, p[2] >> 4);
9081 		} else {
9082 			sbuf_printf(sb,
9083 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9084 			    "%08x%08x%08x%08x",
9085 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9086 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9087 			    p[6], p[7]);
9088 		}
9089 	}
9090 }
9091 
9092 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9093 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9094 {
9095 	uint32_t *p;
9096 
9097 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9098 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9099 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9100 
9101 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9102 		if (cfg & F_UPDBGLACAPTPCONLY) {
9103 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9104 			    p[3] & 0xff, p[2], p[1], p[0]);
9105 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9106 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9107 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9108 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9109 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9110 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9111 			    p[6] >> 16);
9112 		} else {
9113 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9114 			    "%08x %08x %08x %08x %08x %08x",
9115 			    (p[9] >> 16) & 0xff,
9116 			    p[9] & 0xffff, p[8] >> 16,
9117 			    p[8] & 0xffff, p[7] >> 16,
9118 			    p[7] & 0xffff, p[6] >> 16,
9119 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9120 		}
9121 	}
9122 }
9123 
9124 static int
sbuf_cim_la(struct adapter * sc,struct sbuf * sb,int flags)9125 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
9126 {
9127 	uint32_t cfg, *buf;
9128 	int rc;
9129 
9130 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9131 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9132 	    M_ZERO | flags);
9133 	if (buf == NULL)
9134 		return (ENOMEM);
9135 
9136 	mtx_lock(&sc->reg_lock);
9137 	if (hw_off_limits(sc))
9138 		rc = ENXIO;
9139 	else {
9140 		rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
9141 		if (rc == 0)
9142 			rc = -t4_cim_read_la(sc, buf, NULL);
9143 	}
9144 	mtx_unlock(&sc->reg_lock);
9145 	if (rc == 0) {
9146 		if (chip_id(sc) < CHELSIO_T6)
9147 			sbuf_cim_la4(sc, sb, buf, cfg);
9148 		else
9149 			sbuf_cim_la6(sc, sb, buf, cfg);
9150 	}
9151 	free(buf, M_CXGBE);
9152 	return (rc);
9153 }
9154 
9155 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9156 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9157 {
9158 	struct adapter *sc = arg1;
9159 	struct sbuf *sb;
9160 	int rc;
9161 
9162 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9163 	if (sb == NULL)
9164 		return (ENOMEM);
9165 
9166 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
9167 	if (rc == 0)
9168 		rc = sbuf_finish(sb);
9169 	sbuf_delete(sb);
9170 	return (rc);
9171 }
9172 
9173 static void
dump_cim_regs(struct adapter * sc)9174 dump_cim_regs(struct adapter *sc)
9175 {
9176 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9177 	    device_get_nameunit(sc->dev),
9178 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9179 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9180 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9181 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9182 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9183 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9184 	    device_get_nameunit(sc->dev),
9185 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9186 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9187 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9188 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9189 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9190 }
9191 
9192 static void
dump_cimla(struct adapter * sc)9193 dump_cimla(struct adapter *sc)
9194 {
9195 	struct sbuf sb;
9196 	int rc;
9197 
9198 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9199 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9200 		    device_get_nameunit(sc->dev));
9201 		return;
9202 	}
9203 	rc = sbuf_cim_la(sc, &sb, M_WAITOK);
9204 	if (rc == 0) {
9205 		rc = sbuf_finish(&sb);
9206 		if (rc == 0) {
9207 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9208 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9209 		}
9210 	}
9211 	sbuf_delete(&sb);
9212 }
9213 
9214 void
t4_os_cim_err(struct adapter * sc)9215 t4_os_cim_err(struct adapter *sc)
9216 {
9217 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9218 }
9219 
9220 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9221 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9222 {
9223 	struct adapter *sc = arg1;
9224 	u_int i;
9225 	struct sbuf *sb;
9226 	uint32_t *buf, *p;
9227 	int rc;
9228 
9229 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9230 	if (sb == NULL)
9231 		return (ENOMEM);
9232 
9233 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9234 	    M_ZERO | M_WAITOK);
9235 
9236 	rc = 0;
9237 	mtx_lock(&sc->reg_lock);
9238 	if (hw_off_limits(sc))
9239 		rc = ENXIO;
9240 	else
9241 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9242 	mtx_unlock(&sc->reg_lock);
9243 	if (rc)
9244 		goto done;
9245 
9246 	p = buf;
9247 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9248 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9249 		    p[1], p[0]);
9250 	}
9251 
9252 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9253 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9254 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9255 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9256 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9257 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9258 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9259 		    p[0] & 1);
9260 	}
9261 	rc = sbuf_finish(sb);
9262 done:
9263 	sbuf_delete(sb);
9264 	free(buf, M_CXGBE);
9265 	return (rc);
9266 }
9267 
9268 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9269 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9270 {
9271 	struct adapter *sc = arg1;
9272 	u_int i;
9273 	struct sbuf *sb;
9274 	uint32_t *buf, *p;
9275 	int rc;
9276 
9277 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9278 	if (sb == NULL)
9279 		return (ENOMEM);
9280 
9281 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9282 	    M_ZERO | M_WAITOK);
9283 
9284 	rc = 0;
9285 	mtx_lock(&sc->reg_lock);
9286 	if (hw_off_limits(sc))
9287 		rc = ENXIO;
9288 	else
9289 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9290 	mtx_unlock(&sc->reg_lock);
9291 	if (rc)
9292 		goto done;
9293 
9294 	p = buf;
9295 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9296 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9297 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9298 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9299 		    p[4], p[3], p[2], p[1], p[0]);
9300 	}
9301 
9302 	sbuf_printf(sb, "\n\nCntl ID               Data");
9303 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9304 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9305 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9306 	}
9307 
9308 	rc = sbuf_finish(sb);
9309 done:
9310 	sbuf_delete(sb);
9311 	free(buf, M_CXGBE);
9312 	return (rc);
9313 }
9314 
9315 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9316 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9317 {
9318 	struct adapter *sc = arg1;
9319 	struct sbuf *sb;
9320 	int rc, i;
9321 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9322 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9323 	uint16_t thres[CIM_NUM_IBQ];
9324 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9325 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9326 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9327 
9328 	cim_num_obq = sc->chip_params->cim_num_obq;
9329 	if (is_t4(sc)) {
9330 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9331 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9332 	} else {
9333 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9334 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9335 	}
9336 	nq = CIM_NUM_IBQ + cim_num_obq;
9337 
9338 	mtx_lock(&sc->reg_lock);
9339 	if (hw_off_limits(sc))
9340 		rc = ENXIO;
9341 	else {
9342 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9343 		if (rc == 0) {
9344 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9345 			    obq_wr);
9346 			if (rc == 0)
9347 				t4_read_cimq_cfg(sc, base, size, thres);
9348 		}
9349 	}
9350 	mtx_unlock(&sc->reg_lock);
9351 	if (rc)
9352 		return (rc);
9353 
9354 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9355 	if (sb == NULL)
9356 		return (ENOMEM);
9357 
9358 	sbuf_printf(sb,
9359 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9360 
9361 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9362 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9363 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9364 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9365 		    G_QUEREMFLITS(p[2]) * 16);
9366 	for ( ; i < nq; i++, p += 4, wr += 2)
9367 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9368 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9369 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9370 		    G_QUEREMFLITS(p[2]) * 16);
9371 
9372 	rc = sbuf_finish(sb);
9373 	sbuf_delete(sb);
9374 
9375 	return (rc);
9376 }
9377 
9378 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9379 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9380 {
9381 	struct adapter *sc = arg1;
9382 	struct sbuf *sb;
9383 	int rc;
9384 	struct tp_cpl_stats stats;
9385 
9386 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9387 	if (sb == NULL)
9388 		return (ENOMEM);
9389 
9390 	rc = 0;
9391 	mtx_lock(&sc->reg_lock);
9392 	if (hw_off_limits(sc))
9393 		rc = ENXIO;
9394 	else
9395 		t4_tp_get_cpl_stats(sc, &stats, 0);
9396 	mtx_unlock(&sc->reg_lock);
9397 	if (rc)
9398 		goto done;
9399 
9400 	if (sc->chip_params->nchan > 2) {
9401 		sbuf_printf(sb, "                 channel 0  channel 1"
9402 		    "  channel 2  channel 3");
9403 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9404 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9405 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9406 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9407 	} else {
9408 		sbuf_printf(sb, "                 channel 0  channel 1");
9409 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9410 		    stats.req[0], stats.req[1]);
9411 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9412 		    stats.rsp[0], stats.rsp[1]);
9413 	}
9414 
9415 	rc = sbuf_finish(sb);
9416 done:
9417 	sbuf_delete(sb);
9418 	return (rc);
9419 }
9420 
9421 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9422 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9423 {
9424 	struct adapter *sc = arg1;
9425 	struct sbuf *sb;
9426 	int rc;
9427 	struct tp_usm_stats stats;
9428 
9429 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9430 	if (sb == NULL)
9431 		return (ENOMEM);
9432 
9433 	rc = 0;
9434 	mtx_lock(&sc->reg_lock);
9435 	if (hw_off_limits(sc))
9436 		rc = ENXIO;
9437 	else
9438 		t4_get_usm_stats(sc, &stats, 1);
9439 	mtx_unlock(&sc->reg_lock);
9440 	if (rc == 0) {
9441 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
9442 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
9443 		sbuf_printf(sb, "Drops:  %u", stats.drops);
9444 		rc = sbuf_finish(sb);
9445 	}
9446 	sbuf_delete(sb);
9447 
9448 	return (rc);
9449 }
9450 
9451 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)9452 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
9453 {
9454 	struct adapter *sc = arg1;
9455 	struct sbuf *sb;
9456 	int rc;
9457 	struct tp_tid_stats stats;
9458 
9459 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9460 	if (sb == NULL)
9461 		return (ENOMEM);
9462 
9463 	rc = 0;
9464 	mtx_lock(&sc->reg_lock);
9465 	if (hw_off_limits(sc))
9466 		rc = ENXIO;
9467 	else
9468 		t4_tp_get_tid_stats(sc, &stats, 1);
9469 	mtx_unlock(&sc->reg_lock);
9470 	if (rc == 0) {
9471 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
9472 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
9473 		sbuf_printf(sb, "Active:     %u\n", stats.act);
9474 		sbuf_printf(sb, "Passive:    %u", stats.pas);
9475 		rc = sbuf_finish(sb);
9476 	}
9477 	sbuf_delete(sb);
9478 
9479 	return (rc);
9480 }
9481 
9482 static const char * const devlog_level_strings[] = {
9483 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
9484 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
9485 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
9486 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
9487 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
9488 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
9489 };
9490 
9491 static const char * const devlog_facility_strings[] = {
9492 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
9493 	[FW_DEVLOG_FACILITY_CF]		= "CF",
9494 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
9495 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
9496 	[FW_DEVLOG_FACILITY_RES]	= "RES",
9497 	[FW_DEVLOG_FACILITY_HW]		= "HW",
9498 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
9499 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
9500 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
9501 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
9502 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
9503 	[FW_DEVLOG_FACILITY_VI]		= "VI",
9504 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
9505 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
9506 	[FW_DEVLOG_FACILITY_TM]		= "TM",
9507 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
9508 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
9509 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
9510 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
9511 	[FW_DEVLOG_FACILITY_RI]		= "RI",
9512 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
9513 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
9514 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
9515 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
9516 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
9517 };
9518 
9519 static int
sbuf_devlog(struct adapter * sc,struct sbuf * sb,int flags)9520 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
9521 {
9522 	int i, j, rc, nentries, first = 0;
9523 	struct devlog_params *dparams = &sc->params.devlog;
9524 	struct fw_devlog_e *buf, *e;
9525 	uint64_t ftstamp = UINT64_MAX;
9526 
9527 	if (dparams->addr == 0)
9528 		return (ENXIO);
9529 
9530 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9531 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
9532 	if (buf == NULL)
9533 		return (ENOMEM);
9534 
9535 	mtx_lock(&sc->reg_lock);
9536 	if (hw_off_limits(sc))
9537 		rc = ENXIO;
9538 	else
9539 		rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf,
9540 		    dparams->size);
9541 	mtx_unlock(&sc->reg_lock);
9542 	if (rc != 0)
9543 		goto done;
9544 
9545 	nentries = dparams->size / sizeof(struct fw_devlog_e);
9546 	for (i = 0; i < nentries; i++) {
9547 		e = &buf[i];
9548 
9549 		if (e->timestamp == 0)
9550 			break;	/* end */
9551 
9552 		e->timestamp = be64toh(e->timestamp);
9553 		e->seqno = be32toh(e->seqno);
9554 		for (j = 0; j < 8; j++)
9555 			e->params[j] = be32toh(e->params[j]);
9556 
9557 		if (e->timestamp < ftstamp) {
9558 			ftstamp = e->timestamp;
9559 			first = i;
9560 		}
9561 	}
9562 
9563 	if (buf[first].timestamp == 0)
9564 		goto done;	/* nothing in the log */
9565 
9566 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
9567 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
9568 
9569 	i = first;
9570 	do {
9571 		e = &buf[i];
9572 		if (e->timestamp == 0)
9573 			break;	/* end */
9574 
9575 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
9576 		    e->seqno, e->timestamp,
9577 		    (e->level < nitems(devlog_level_strings) ?
9578 			devlog_level_strings[e->level] : "UNKNOWN"),
9579 		    (e->facility < nitems(devlog_facility_strings) ?
9580 			devlog_facility_strings[e->facility] : "UNKNOWN"));
9581 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
9582 		    e->params[2], e->params[3], e->params[4],
9583 		    e->params[5], e->params[6], e->params[7]);
9584 
9585 		if (++i == nentries)
9586 			i = 0;
9587 	} while (i != first);
9588 done:
9589 	free(buf, M_CXGBE);
9590 	return (rc);
9591 }
9592 
9593 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)9594 sysctl_devlog(SYSCTL_HANDLER_ARGS)
9595 {
9596 	struct adapter *sc = arg1;
9597 	int rc;
9598 	struct sbuf *sb;
9599 
9600 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9601 	if (sb == NULL)
9602 		return (ENOMEM);
9603 
9604 	rc = sbuf_devlog(sc, sb, M_WAITOK);
9605 	if (rc == 0)
9606 		rc = sbuf_finish(sb);
9607 	sbuf_delete(sb);
9608 	return (rc);
9609 }
9610 
9611 static void
dump_devlog(struct adapter * sc)9612 dump_devlog(struct adapter *sc)
9613 {
9614 	int rc;
9615 	struct sbuf sb;
9616 
9617 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9618 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
9619 		    device_get_nameunit(sc->dev));
9620 		return;
9621 	}
9622 	rc = sbuf_devlog(sc, &sb, M_WAITOK);
9623 	if (rc == 0) {
9624 		rc = sbuf_finish(&sb);
9625 		if (rc == 0) {
9626 			log(LOG_DEBUG, "%s: device log follows.\n%s",
9627 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9628 		}
9629 	}
9630 	sbuf_delete(&sb);
9631 }
9632 
9633 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)9634 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
9635 {
9636 	struct adapter *sc = arg1;
9637 	struct sbuf *sb;
9638 	int rc;
9639 	struct tp_fcoe_stats stats[MAX_NCHAN];
9640 	int i, nchan = sc->chip_params->nchan;
9641 
9642 	rc = 0;
9643 	mtx_lock(&sc->reg_lock);
9644 	if (hw_off_limits(sc))
9645 		rc = ENXIO;
9646 	else {
9647 		for (i = 0; i < nchan; i++)
9648 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
9649 	}
9650 	mtx_unlock(&sc->reg_lock);
9651 	if (rc != 0)
9652 		return (rc);
9653 
9654 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9655 	if (sb == NULL)
9656 		return (ENOMEM);
9657 
9658 	if (nchan > 2) {
9659 		sbuf_printf(sb, "                   channel 0        channel 1"
9660 		    "        channel 2        channel 3");
9661 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
9662 		    stats[0].octets_ddp, stats[1].octets_ddp,
9663 		    stats[2].octets_ddp, stats[3].octets_ddp);
9664 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
9665 		    stats[0].frames_ddp, stats[1].frames_ddp,
9666 		    stats[2].frames_ddp, stats[3].frames_ddp);
9667 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
9668 		    stats[0].frames_drop, stats[1].frames_drop,
9669 		    stats[2].frames_drop, stats[3].frames_drop);
9670 	} else {
9671 		sbuf_printf(sb, "                   channel 0        channel 1");
9672 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
9673 		    stats[0].octets_ddp, stats[1].octets_ddp);
9674 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
9675 		    stats[0].frames_ddp, stats[1].frames_ddp);
9676 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
9677 		    stats[0].frames_drop, stats[1].frames_drop);
9678 	}
9679 
9680 	rc = sbuf_finish(sb);
9681 	sbuf_delete(sb);
9682 
9683 	return (rc);
9684 }
9685 
9686 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)9687 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
9688 {
9689 	struct adapter *sc = arg1;
9690 	struct sbuf *sb;
9691 	int rc, i;
9692 	unsigned int map, kbps, ipg, mode;
9693 	unsigned int pace_tab[NTX_SCHED];
9694 
9695 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
9696 	if (sb == NULL)
9697 		return (ENOMEM);
9698 
9699 	mtx_lock(&sc->reg_lock);
9700 	if (hw_off_limits(sc)) {
9701 		mtx_unlock(&sc->reg_lock);
9702 		rc = ENXIO;
9703 		goto done;
9704 	}
9705 
9706 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
9707 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
9708 	t4_read_pace_tbl(sc, pace_tab);
9709 	mtx_unlock(&sc->reg_lock);
9710 
9711 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
9712 	    "Class IPG (0.1 ns)   Flow IPG (us)");
9713 
9714 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
9715 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
9716 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
9717 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
9718 		if (kbps)
9719 			sbuf_printf(sb, "%9u     ", kbps);
9720 		else
9721 			sbuf_printf(sb, " disabled     ");
9722 
9723 		if (ipg)
9724 			sbuf_printf(sb, "%13u        ", ipg);
9725 		else
9726 			sbuf_printf(sb, "     disabled        ");
9727 
9728 		if (pace_tab[i])
9729 			sbuf_printf(sb, "%10u", pace_tab[i]);
9730 		else
9731 			sbuf_printf(sb, "  disabled");
9732 	}
9733 	rc = sbuf_finish(sb);
9734 done:
9735 	sbuf_delete(sb);
9736 	return (rc);
9737 }
9738 
9739 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)9740 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
9741 {
9742 	struct adapter *sc = arg1;
9743 	struct sbuf *sb;
9744 	int rc, i, j;
9745 	uint64_t *p0, *p1;
9746 	struct lb_port_stats s[2];
9747 	static const char *stat_name[] = {
9748 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
9749 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
9750 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
9751 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
9752 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
9753 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
9754 		"BG2FramesTrunc:", "BG3FramesTrunc:"
9755 	};
9756 
9757 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9758 	if (sb == NULL)
9759 		return (ENOMEM);
9760 
9761 	memset(s, 0, sizeof(s));
9762 
9763 	rc = 0;
9764 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
9765 		mtx_lock(&sc->reg_lock);
9766 		if (hw_off_limits(sc))
9767 			rc = ENXIO;
9768 		else {
9769 			t4_get_lb_stats(sc, i, &s[0]);
9770 			t4_get_lb_stats(sc, i + 1, &s[1]);
9771 		}
9772 		mtx_unlock(&sc->reg_lock);
9773 		if (rc != 0)
9774 			break;
9775 
9776 		p0 = &s[0].octets;
9777 		p1 = &s[1].octets;
9778 		sbuf_printf(sb, "%s                       Loopback %u"
9779 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
9780 
9781 		for (j = 0; j < nitems(stat_name); j++)
9782 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
9783 				   *p0++, *p1++);
9784 	}
9785 
9786 	if (rc == 0)
9787 		rc = sbuf_finish(sb);
9788 	sbuf_delete(sb);
9789 
9790 	return (rc);
9791 }
9792 
9793 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)9794 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
9795 {
9796 	int rc = 0;
9797 	struct port_info *pi = arg1;
9798 	struct link_config *lc = &pi->link_cfg;
9799 	struct sbuf *sb;
9800 
9801 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
9802 	if (sb == NULL)
9803 		return (ENOMEM);
9804 
9805 	if (lc->link_ok || lc->link_down_rc == 255)
9806 		sbuf_printf(sb, "n/a");
9807 	else
9808 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
9809 
9810 	rc = sbuf_finish(sb);
9811 	sbuf_delete(sb);
9812 
9813 	return (rc);
9814 }
9815 
9816 struct mem_desc {
9817 	u_int base;
9818 	u_int limit;
9819 	u_int idx;
9820 };
9821 
9822 static int
mem_desc_cmp(const void * a,const void * b)9823 mem_desc_cmp(const void *a, const void *b)
9824 {
9825 	const u_int v1 = ((const struct mem_desc *)a)->base;
9826 	const u_int v2 = ((const struct mem_desc *)b)->base;
9827 
9828 	if (v1 < v2)
9829 		return (-1);
9830 	else if (v1 > v2)
9831 		return (1);
9832 
9833 	return (0);
9834 }
9835 
9836 static void
mem_region_show(struct sbuf * sb,const char * name,unsigned int from,unsigned int to)9837 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
9838     unsigned int to)
9839 {
9840 	unsigned int size;
9841 
9842 	if (from == to)
9843 		return;
9844 
9845 	size = to - from + 1;
9846 	if (size == 0)
9847 		return;
9848 
9849 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
9850 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
9851 }
9852 
9853 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)9854 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
9855 {
9856 	struct adapter *sc = arg1;
9857 	struct sbuf *sb;
9858 	int rc, i, n;
9859 	uint32_t lo, hi, used, free, alloc;
9860 	static const char *memory[] = {
9861 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
9862 	};
9863 	static const char *region[] = {
9864 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
9865 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
9866 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
9867 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
9868 		"RQUDP region:", "PBL region:", "TXPBL region:",
9869 		"TLSKey region:", "DBVFIFO region:", "ULPRX state:",
9870 		"ULPTX state:", "On-chip queues:",
9871 	};
9872 	struct mem_desc avail[4];
9873 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
9874 	struct mem_desc *md = mem;
9875 
9876 	rc = sysctl_wire_old_buffer(req, 0);
9877 	if (rc != 0)
9878 		return (rc);
9879 
9880 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9881 	if (sb == NULL)
9882 		return (ENOMEM);
9883 
9884 	for (i = 0; i < nitems(mem); i++) {
9885 		mem[i].limit = 0;
9886 		mem[i].idx = i;
9887 	}
9888 
9889 	mtx_lock(&sc->reg_lock);
9890 	if (hw_off_limits(sc)) {
9891 		rc = ENXIO;
9892 		goto done;
9893 	}
9894 
9895 	/* Find and sort the populated memory ranges */
9896 	i = 0;
9897 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
9898 	if (lo & F_EDRAM0_ENABLE) {
9899 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
9900 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
9901 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
9902 		avail[i].idx = 0;
9903 		i++;
9904 	}
9905 	if (lo & F_EDRAM1_ENABLE) {
9906 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
9907 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
9908 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
9909 		avail[i].idx = 1;
9910 		i++;
9911 	}
9912 	if (lo & F_EXT_MEM_ENABLE) {
9913 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
9914 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
9915 		avail[i].limit = avail[i].base + (G_EXT_MEM_SIZE(hi) << 20);
9916 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
9917 		i++;
9918 	}
9919 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
9920 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9921 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9922 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9923 		avail[i].idx = 4;
9924 		i++;
9925 	}
9926 	if (is_t6(sc) && lo & F_HMA_MUX) {
9927 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9928 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9929 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9930 		avail[i].idx = 5;
9931 		i++;
9932 	}
9933 	MPASS(i <= nitems(avail));
9934 	if (!i)                                    /* no memory available */
9935 		goto done;
9936 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
9937 
9938 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
9939 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
9940 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
9941 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
9942 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
9943 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
9944 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
9945 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
9946 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
9947 
9948 	/* the next few have explicit upper bounds */
9949 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
9950 	md->limit = md->base - 1 +
9951 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
9952 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
9953 	md++;
9954 
9955 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
9956 	md->limit = md->base - 1 +
9957 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
9958 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
9959 	md++;
9960 
9961 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
9962 		if (chip_id(sc) <= CHELSIO_T5)
9963 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
9964 		else
9965 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
9966 		md->limit = 0;
9967 	} else {
9968 		md->base = 0;
9969 		md->idx = nitems(region);  /* hide it */
9970 	}
9971 	md++;
9972 
9973 #define ulp_region(reg) \
9974 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
9975 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
9976 
9977 	ulp_region(RX_ISCSI);
9978 	ulp_region(RX_TDDP);
9979 	ulp_region(TX_TPT);
9980 	ulp_region(RX_STAG);
9981 	ulp_region(RX_RQ);
9982 	ulp_region(RX_RQUDP);
9983 	ulp_region(RX_PBL);
9984 	ulp_region(TX_PBL);
9985 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
9986 		ulp_region(RX_TLS_KEY);
9987 	}
9988 #undef ulp_region
9989 
9990 	md->base = 0;
9991 	if (is_t4(sc))
9992 		md->idx = nitems(region);
9993 	else {
9994 		uint32_t size = 0;
9995 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
9996 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
9997 
9998 		if (is_t5(sc)) {
9999 			if (sge_ctrl & F_VFIFO_ENABLE)
10000 				size = fifo_size << 2;
10001 		} else
10002 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
10003 
10004 		if (size) {
10005 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
10006 			md->limit = md->base + size - 1;
10007 		} else
10008 			md->idx = nitems(region);
10009 	}
10010 	md++;
10011 
10012 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
10013 	md->limit = 0;
10014 	md++;
10015 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
10016 	md->limit = 0;
10017 	md++;
10018 
10019 	md->base = sc->vres.ocq.start;
10020 	if (sc->vres.ocq.size)
10021 		md->limit = md->base + sc->vres.ocq.size - 1;
10022 	else
10023 		md->idx = nitems(region);  /* hide it */
10024 	md++;
10025 
10026 	/* add any address-space holes, there can be up to 3 */
10027 	for (n = 0; n < i - 1; n++)
10028 		if (avail[n].limit < avail[n + 1].base)
10029 			(md++)->base = avail[n].limit;
10030 	if (avail[n].limit)
10031 		(md++)->base = avail[n].limit;
10032 
10033 	n = md - mem;
10034 	MPASS(n <= nitems(mem));
10035 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10036 
10037 	for (lo = 0; lo < i; lo++)
10038 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10039 				avail[lo].limit - 1);
10040 
10041 	sbuf_printf(sb, "\n");
10042 	for (i = 0; i < n; i++) {
10043 		if (mem[i].idx >= nitems(region))
10044 			continue;                        /* skip holes */
10045 		if (!mem[i].limit)
10046 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10047 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10048 				mem[i].limit);
10049 	}
10050 
10051 	sbuf_printf(sb, "\n");
10052 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10053 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10054 	mem_region_show(sb, "uP RAM:", lo, hi);
10055 
10056 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10057 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10058 	mem_region_show(sb, "uP Extmem2:", lo, hi);
10059 
10060 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10061 	for (i = 0, free = 0; i < 2; i++)
10062 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10063 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10064 		   G_PMRXMAXPAGE(lo), free,
10065 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
10066 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
10067 
10068 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10069 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10070 	for (i = 0, free = 0; i < 4; i++)
10071 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10072 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10073 		   G_PMTXMAXPAGE(lo), free,
10074 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10075 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
10076 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10077 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10078 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10079 
10080 	for (i = 0; i < 4; i++) {
10081 		if (chip_id(sc) > CHELSIO_T5)
10082 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10083 		else
10084 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10085 		if (is_t5(sc)) {
10086 			used = G_T5_USED(lo);
10087 			alloc = G_T5_ALLOC(lo);
10088 		} else {
10089 			used = G_USED(lo);
10090 			alloc = G_ALLOC(lo);
10091 		}
10092 		/* For T6 these are MAC buffer groups */
10093 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10094 		    i, used, alloc);
10095 	}
10096 	for (i = 0; i < sc->chip_params->nchan; i++) {
10097 		if (chip_id(sc) > CHELSIO_T5)
10098 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10099 		else
10100 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10101 		if (is_t5(sc)) {
10102 			used = G_T5_USED(lo);
10103 			alloc = G_T5_ALLOC(lo);
10104 		} else {
10105 			used = G_USED(lo);
10106 			alloc = G_ALLOC(lo);
10107 		}
10108 		/* For T6 these are MAC buffer groups */
10109 		sbuf_printf(sb,
10110 		    "\nLoopback %d using %u pages out of %u allocated",
10111 		    i, used, alloc);
10112 	}
10113 done:
10114 	mtx_unlock(&sc->reg_lock);
10115 	if (rc == 0)
10116 		rc = sbuf_finish(sb);
10117 	sbuf_delete(sb);
10118 	return (rc);
10119 }
10120 
10121 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10122 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10123 {
10124 	*mask = x | y;
10125 	y = htobe64(y);
10126 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10127 }
10128 
10129 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10130 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10131 {
10132 	struct adapter *sc = arg1;
10133 	struct sbuf *sb;
10134 	int rc, i;
10135 
10136 	MPASS(chip_id(sc) <= CHELSIO_T5);
10137 
10138 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10139 	if (sb == NULL)
10140 		return (ENOMEM);
10141 
10142 	sbuf_printf(sb,
10143 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10144 	    "  VF              Replication             P0 P1 P2 P3  ML");
10145 	rc = 0;
10146 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10147 		uint64_t tcamx, tcamy, mask;
10148 		uint32_t cls_lo, cls_hi;
10149 		uint8_t addr[ETHER_ADDR_LEN];
10150 
10151 		mtx_lock(&sc->reg_lock);
10152 		if (hw_off_limits(sc))
10153 			rc = ENXIO;
10154 		else {
10155 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10156 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10157 		}
10158 		mtx_unlock(&sc->reg_lock);
10159 		if (rc != 0)
10160 			break;
10161 		if (tcamx & tcamy)
10162 			continue;
10163 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10164 		mtx_lock(&sc->reg_lock);
10165 		if (hw_off_limits(sc))
10166 			rc = ENXIO;
10167 		else {
10168 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10169 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10170 		}
10171 		mtx_unlock(&sc->reg_lock);
10172 		if (rc != 0)
10173 			break;
10174 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10175 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10176 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10177 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10178 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10179 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10180 
10181 		if (cls_lo & F_REPLICATE) {
10182 			struct fw_ldst_cmd ldst_cmd;
10183 
10184 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10185 			ldst_cmd.op_to_addrspace =
10186 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10187 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10188 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10189 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10190 			ldst_cmd.u.mps.rplc.fid_idx =
10191 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10192 				V_FW_LDST_CMD_IDX(i));
10193 
10194 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10195 			    "t4mps");
10196 			if (rc)
10197 				break;
10198 			if (hw_off_limits(sc))
10199 				rc = ENXIO;
10200 			else
10201 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10202 				    sizeof(ldst_cmd), &ldst_cmd);
10203 			end_synchronized_op(sc, 0);
10204 			if (rc != 0)
10205 				break;
10206 			else {
10207 				sbuf_printf(sb, " %08x %08x %08x %08x",
10208 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10209 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10210 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10211 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10212 			}
10213 		} else
10214 			sbuf_printf(sb, "%36s", "");
10215 
10216 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10217 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10218 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10219 	}
10220 
10221 	if (rc)
10222 		(void) sbuf_finish(sb);
10223 	else
10224 		rc = sbuf_finish(sb);
10225 	sbuf_delete(sb);
10226 
10227 	return (rc);
10228 }
10229 
10230 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10231 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10232 {
10233 	struct adapter *sc = arg1;
10234 	struct sbuf *sb;
10235 	int rc, i;
10236 
10237 	MPASS(chip_id(sc) > CHELSIO_T5);
10238 
10239 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10240 	if (sb == NULL)
10241 		return (ENOMEM);
10242 
10243 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10244 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10245 	    "                           Replication"
10246 	    "                                    P0 P1 P2 P3  ML\n");
10247 
10248 	rc = 0;
10249 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10250 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10251 		uint16_t ivlan;
10252 		uint64_t tcamx, tcamy, val, mask;
10253 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10254 		uint8_t addr[ETHER_ADDR_LEN];
10255 
10256 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10257 		if (i < 256)
10258 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10259 		else
10260 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10261 		mtx_lock(&sc->reg_lock);
10262 		if (hw_off_limits(sc))
10263 			rc = ENXIO;
10264 		else {
10265 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10266 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10267 			tcamy = G_DMACH(val) << 32;
10268 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10269 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10270 		}
10271 		mtx_unlock(&sc->reg_lock);
10272 		if (rc != 0)
10273 			break;
10274 
10275 		lookup_type = G_DATALKPTYPE(data2);
10276 		port_num = G_DATAPORTNUM(data2);
10277 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10278 			/* Inner header VNI */
10279 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10280 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10281 			dip_hit = data2 & F_DATADIPHIT;
10282 			vlan_vld = 0;
10283 		} else {
10284 			vniy = 0;
10285 			dip_hit = 0;
10286 			vlan_vld = data2 & F_DATAVIDH2;
10287 			ivlan = G_VIDL(val);
10288 		}
10289 
10290 		ctl |= V_CTLXYBITSEL(1);
10291 		mtx_lock(&sc->reg_lock);
10292 		if (hw_off_limits(sc))
10293 			rc = ENXIO;
10294 		else {
10295 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10296 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10297 			tcamx = G_DMACH(val) << 32;
10298 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10299 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10300 		}
10301 		mtx_unlock(&sc->reg_lock);
10302 		if (rc != 0)
10303 			break;
10304 
10305 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10306 			/* Inner header VNI mask */
10307 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10308 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10309 		} else
10310 			vnix = 0;
10311 
10312 		if (tcamx & tcamy)
10313 			continue;
10314 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10315 
10316 		mtx_lock(&sc->reg_lock);
10317 		if (hw_off_limits(sc))
10318 			rc = ENXIO;
10319 		else {
10320 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10321 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10322 		}
10323 		mtx_unlock(&sc->reg_lock);
10324 		if (rc != 0)
10325 			break;
10326 
10327 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10328 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10329 			    "%012jx %06x %06x    -    -   %3c"
10330 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
10331 			    addr[1], addr[2], addr[3], addr[4], addr[5],
10332 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
10333 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10334 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10335 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10336 		} else {
10337 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10338 			    "%012jx    -       -   ", i, addr[0], addr[1],
10339 			    addr[2], addr[3], addr[4], addr[5],
10340 			    (uintmax_t)mask);
10341 
10342 			if (vlan_vld)
10343 				sbuf_printf(sb, "%4u   Y     ", ivlan);
10344 			else
10345 				sbuf_printf(sb, "  -    N     ");
10346 
10347 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
10348 			    lookup_type ? 'I' : 'O', port_num,
10349 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10350 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10351 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10352 		}
10353 
10354 
10355 		if (cls_lo & F_T6_REPLICATE) {
10356 			struct fw_ldst_cmd ldst_cmd;
10357 
10358 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10359 			ldst_cmd.op_to_addrspace =
10360 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10361 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10362 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10363 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10364 			ldst_cmd.u.mps.rplc.fid_idx =
10365 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10366 				V_FW_LDST_CMD_IDX(i));
10367 
10368 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10369 			    "t6mps");
10370 			if (rc)
10371 				break;
10372 			if (hw_off_limits(sc))
10373 				rc = ENXIO;
10374 			else
10375 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10376 				    sizeof(ldst_cmd), &ldst_cmd);
10377 			end_synchronized_op(sc, 0);
10378 			if (rc != 0)
10379 				break;
10380 			else {
10381 				sbuf_printf(sb, " %08x %08x %08x %08x"
10382 				    " %08x %08x %08x %08x",
10383 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
10384 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
10385 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
10386 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
10387 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10388 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10389 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10390 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10391 			}
10392 		} else
10393 			sbuf_printf(sb, "%72s", "");
10394 
10395 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
10396 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
10397 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
10398 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
10399 	}
10400 
10401 	if (rc)
10402 		(void) sbuf_finish(sb);
10403 	else
10404 		rc = sbuf_finish(sb);
10405 	sbuf_delete(sb);
10406 
10407 	return (rc);
10408 }
10409 
10410 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)10411 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
10412 {
10413 	struct adapter *sc = arg1;
10414 	struct sbuf *sb;
10415 	int rc;
10416 	uint16_t mtus[NMTUS];
10417 
10418 	rc = 0;
10419 	mtx_lock(&sc->reg_lock);
10420 	if (hw_off_limits(sc))
10421 		rc = ENXIO;
10422 	else
10423 		t4_read_mtu_tbl(sc, mtus, NULL);
10424 	mtx_unlock(&sc->reg_lock);
10425 	if (rc != 0)
10426 		return (rc);
10427 
10428 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10429 	if (sb == NULL)
10430 		return (ENOMEM);
10431 
10432 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
10433 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
10434 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
10435 	    mtus[14], mtus[15]);
10436 
10437 	rc = sbuf_finish(sb);
10438 	sbuf_delete(sb);
10439 
10440 	return (rc);
10441 }
10442 
10443 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)10444 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
10445 {
10446 	struct adapter *sc = arg1;
10447 	struct sbuf *sb;
10448 	int rc, i;
10449 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
10450 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
10451 	static const char *tx_stats[MAX_PM_NSTATS] = {
10452 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
10453 		"Tx FIFO wait", NULL, "Tx latency"
10454 	};
10455 	static const char *rx_stats[MAX_PM_NSTATS] = {
10456 		"Read:", "Write bypass:", "Write mem:", "Flush:",
10457 		"Rx FIFO wait", NULL, "Rx latency"
10458 	};
10459 
10460 	rc = 0;
10461 	mtx_lock(&sc->reg_lock);
10462 	if (hw_off_limits(sc))
10463 		rc = ENXIO;
10464 	else {
10465 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
10466 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
10467 	}
10468 	mtx_unlock(&sc->reg_lock);
10469 	if (rc != 0)
10470 		return (rc);
10471 
10472 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10473 	if (sb == NULL)
10474 		return (ENOMEM);
10475 
10476 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
10477 	for (i = 0; i < 4; i++) {
10478 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10479 		    tx_cyc[i]);
10480 	}
10481 
10482 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
10483 	for (i = 0; i < 4; i++) {
10484 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10485 		    rx_cyc[i]);
10486 	}
10487 
10488 	if (chip_id(sc) > CHELSIO_T5) {
10489 		sbuf_printf(sb,
10490 		    "\n              Total wait      Total occupancy");
10491 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10492 		    tx_cyc[i]);
10493 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10494 		    rx_cyc[i]);
10495 
10496 		i += 2;
10497 		MPASS(i < nitems(tx_stats));
10498 
10499 		sbuf_printf(sb,
10500 		    "\n                   Reads           Total wait");
10501 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10502 		    tx_cyc[i]);
10503 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10504 		    rx_cyc[i]);
10505 	}
10506 
10507 	rc = sbuf_finish(sb);
10508 	sbuf_delete(sb);
10509 
10510 	return (rc);
10511 }
10512 
10513 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)10514 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
10515 {
10516 	struct adapter *sc = arg1;
10517 	struct sbuf *sb;
10518 	int rc;
10519 	struct tp_rdma_stats stats;
10520 
10521 	rc = 0;
10522 	mtx_lock(&sc->reg_lock);
10523 	if (hw_off_limits(sc))
10524 		rc = ENXIO;
10525 	else
10526 		t4_tp_get_rdma_stats(sc, &stats, 0);
10527 	mtx_unlock(&sc->reg_lock);
10528 	if (rc != 0)
10529 		return (rc);
10530 
10531 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10532 	if (sb == NULL)
10533 		return (ENOMEM);
10534 
10535 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
10536 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
10537 
10538 	rc = sbuf_finish(sb);
10539 	sbuf_delete(sb);
10540 
10541 	return (rc);
10542 }
10543 
10544 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)10545 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
10546 {
10547 	struct adapter *sc = arg1;
10548 	struct sbuf *sb;
10549 	int rc;
10550 	struct tp_tcp_stats v4, v6;
10551 
10552 	rc = 0;
10553 	mtx_lock(&sc->reg_lock);
10554 	if (hw_off_limits(sc))
10555 		rc = ENXIO;
10556 	else
10557 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
10558 	mtx_unlock(&sc->reg_lock);
10559 	if (rc != 0)
10560 		return (rc);
10561 
10562 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10563 	if (sb == NULL)
10564 		return (ENOMEM);
10565 
10566 	sbuf_printf(sb,
10567 	    "                                IP                 IPv6\n");
10568 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
10569 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
10570 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
10571 	    v4.tcp_in_segs, v6.tcp_in_segs);
10572 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
10573 	    v4.tcp_out_segs, v6.tcp_out_segs);
10574 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
10575 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
10576 
10577 	rc = sbuf_finish(sb);
10578 	sbuf_delete(sb);
10579 
10580 	return (rc);
10581 }
10582 
10583 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)10584 sysctl_tids(SYSCTL_HANDLER_ARGS)
10585 {
10586 	struct adapter *sc = arg1;
10587 	struct sbuf *sb;
10588 	int rc;
10589 	uint32_t x, y;
10590 	struct tid_info *t = &sc->tids;
10591 
10592 	rc = 0;
10593 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10594 	if (sb == NULL)
10595 		return (ENOMEM);
10596 
10597 	if (t->natids) {
10598 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
10599 		    t->atids_in_use);
10600 	}
10601 
10602 	if (t->nhpftids) {
10603 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
10604 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
10605 	}
10606 
10607 	if (t->ntids) {
10608 		bool hashen = false;
10609 
10610 		mtx_lock(&sc->reg_lock);
10611 		if (hw_off_limits(sc))
10612 			rc = ENXIO;
10613 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10614 			hashen = true;
10615 			if (chip_id(sc) <= CHELSIO_T5) {
10616 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
10617 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
10618 			} else {
10619 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
10620 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
10621 			}
10622 		}
10623 		mtx_unlock(&sc->reg_lock);
10624 		if (rc != 0)
10625 			goto done;
10626 
10627 		sbuf_printf(sb, "TID range: ");
10628 		if (hashen) {
10629 			if (x)
10630 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
10631 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
10632 		} else {
10633 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
10634 			    t->ntids - 1);
10635 		}
10636 		sbuf_printf(sb, ", in use: %u\n",
10637 		    atomic_load_acq_int(&t->tids_in_use));
10638 	}
10639 
10640 	if (t->nstids) {
10641 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
10642 		    t->stid_base + t->nstids - 1, t->stids_in_use);
10643 	}
10644 
10645 	if (t->nftids) {
10646 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
10647 		    t->ftid_end, t->ftids_in_use);
10648 	}
10649 
10650 	if (t->netids) {
10651 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
10652 		    t->etid_base + t->netids - 1, t->etids_in_use);
10653 	}
10654 
10655 	mtx_lock(&sc->reg_lock);
10656 	if (hw_off_limits(sc))
10657 		rc = ENXIO;
10658 	else {
10659 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
10660 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
10661 	}
10662 	mtx_unlock(&sc->reg_lock);
10663 	if (rc != 0)
10664 		goto done;
10665 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
10666 done:
10667 	if (rc == 0)
10668 		rc = sbuf_finish(sb);
10669 	else
10670 		(void)sbuf_finish(sb);
10671 	sbuf_delete(sb);
10672 
10673 	return (rc);
10674 }
10675 
10676 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)10677 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
10678 {
10679 	struct adapter *sc = arg1;
10680 	struct sbuf *sb;
10681 	int rc;
10682 	struct tp_err_stats stats;
10683 
10684 	rc = 0;
10685 	mtx_lock(&sc->reg_lock);
10686 	if (hw_off_limits(sc))
10687 		rc = ENXIO;
10688 	else
10689 		t4_tp_get_err_stats(sc, &stats, 0);
10690 	mtx_unlock(&sc->reg_lock);
10691 	if (rc != 0)
10692 		return (rc);
10693 
10694 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10695 	if (sb == NULL)
10696 		return (ENOMEM);
10697 
10698 	if (sc->chip_params->nchan > 2) {
10699 		sbuf_printf(sb, "                 channel 0  channel 1"
10700 		    "  channel 2  channel 3\n");
10701 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
10702 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
10703 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
10704 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
10705 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
10706 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
10707 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
10708 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
10709 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
10710 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
10711 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
10712 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
10713 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
10714 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
10715 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
10716 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
10717 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
10718 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
10719 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
10720 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
10721 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
10722 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
10723 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
10724 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
10725 	} else {
10726 		sbuf_printf(sb, "                 channel 0  channel 1\n");
10727 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
10728 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
10729 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
10730 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
10731 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
10732 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
10733 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
10734 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
10735 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
10736 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
10737 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
10738 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
10739 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
10740 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
10741 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
10742 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
10743 	}
10744 
10745 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
10746 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
10747 
10748 	rc = sbuf_finish(sb);
10749 	sbuf_delete(sb);
10750 
10751 	return (rc);
10752 }
10753 
10754 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)10755 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
10756 {
10757 	struct adapter *sc = arg1;
10758 	struct sbuf *sb;
10759 	int rc;
10760 	struct tp_tnl_stats stats;
10761 
10762 	rc = 0;
10763 	mtx_lock(&sc->reg_lock);
10764 	if (hw_off_limits(sc))
10765 		rc = ENXIO;
10766 	else
10767 		t4_tp_get_tnl_stats(sc, &stats, 1);
10768 	mtx_unlock(&sc->reg_lock);
10769 	if (rc != 0)
10770 		return (rc);
10771 
10772 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10773 	if (sb == NULL)
10774 		return (ENOMEM);
10775 
10776 	if (sc->chip_params->nchan > 2) {
10777 		sbuf_printf(sb, "           channel 0  channel 1"
10778 		    "  channel 2  channel 3\n");
10779 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
10780 		    stats.out_pkt[0], stats.out_pkt[1],
10781 		    stats.out_pkt[2], stats.out_pkt[3]);
10782 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
10783 		    stats.in_pkt[0], stats.in_pkt[1],
10784 		    stats.in_pkt[2], stats.in_pkt[3]);
10785 	} else {
10786 		sbuf_printf(sb, "           channel 0  channel 1\n");
10787 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
10788 		    stats.out_pkt[0], stats.out_pkt[1]);
10789 		sbuf_printf(sb, "InPkts:   %10u %10u",
10790 		    stats.in_pkt[0], stats.in_pkt[1]);
10791 	}
10792 
10793 	rc = sbuf_finish(sb);
10794 	sbuf_delete(sb);
10795 
10796 	return (rc);
10797 }
10798 
10799 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)10800 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
10801 {
10802 	struct adapter *sc = arg1;
10803 	struct tp_params *tpp = &sc->params.tp;
10804 	u_int mask;
10805 	int rc;
10806 
10807 	mask = tpp->la_mask >> 16;
10808 	rc = sysctl_handle_int(oidp, &mask, 0, req);
10809 	if (rc != 0 || req->newptr == NULL)
10810 		return (rc);
10811 	if (mask > 0xffff)
10812 		return (EINVAL);
10813 	mtx_lock(&sc->reg_lock);
10814 	if (hw_off_limits(sc))
10815 		rc = ENXIO;
10816 	else {
10817 		tpp->la_mask = mask << 16;
10818 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
10819 		    tpp->la_mask);
10820 	}
10821 	mtx_unlock(&sc->reg_lock);
10822 
10823 	return (rc);
10824 }
10825 
10826 struct field_desc {
10827 	const char *name;
10828 	u_int start;
10829 	u_int width;
10830 };
10831 
10832 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)10833 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
10834 {
10835 	char buf[32];
10836 	int line_size = 0;
10837 
10838 	while (f->name) {
10839 		uint64_t mask = (1ULL << f->width) - 1;
10840 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
10841 		    ((uintmax_t)v >> f->start) & mask);
10842 
10843 		if (line_size + len >= 79) {
10844 			line_size = 8;
10845 			sbuf_printf(sb, "\n        ");
10846 		}
10847 		sbuf_printf(sb, "%s ", buf);
10848 		line_size += len + 1;
10849 		f++;
10850 	}
10851 	sbuf_printf(sb, "\n");
10852 }
10853 
10854 static const struct field_desc tp_la0[] = {
10855 	{ "RcfOpCodeOut", 60, 4 },
10856 	{ "State", 56, 4 },
10857 	{ "WcfState", 52, 4 },
10858 	{ "RcfOpcSrcOut", 50, 2 },
10859 	{ "CRxError", 49, 1 },
10860 	{ "ERxError", 48, 1 },
10861 	{ "SanityFailed", 47, 1 },
10862 	{ "SpuriousMsg", 46, 1 },
10863 	{ "FlushInputMsg", 45, 1 },
10864 	{ "FlushInputCpl", 44, 1 },
10865 	{ "RssUpBit", 43, 1 },
10866 	{ "RssFilterHit", 42, 1 },
10867 	{ "Tid", 32, 10 },
10868 	{ "InitTcb", 31, 1 },
10869 	{ "LineNumber", 24, 7 },
10870 	{ "Emsg", 23, 1 },
10871 	{ "EdataOut", 22, 1 },
10872 	{ "Cmsg", 21, 1 },
10873 	{ "CdataOut", 20, 1 },
10874 	{ "EreadPdu", 19, 1 },
10875 	{ "CreadPdu", 18, 1 },
10876 	{ "TunnelPkt", 17, 1 },
10877 	{ "RcfPeerFin", 16, 1 },
10878 	{ "RcfReasonOut", 12, 4 },
10879 	{ "TxCchannel", 10, 2 },
10880 	{ "RcfTxChannel", 8, 2 },
10881 	{ "RxEchannel", 6, 2 },
10882 	{ "RcfRxChannel", 5, 1 },
10883 	{ "RcfDataOutSrdy", 4, 1 },
10884 	{ "RxDvld", 3, 1 },
10885 	{ "RxOoDvld", 2, 1 },
10886 	{ "RxCongestion", 1, 1 },
10887 	{ "TxCongestion", 0, 1 },
10888 	{ NULL }
10889 };
10890 
10891 static const struct field_desc tp_la1[] = {
10892 	{ "CplCmdIn", 56, 8 },
10893 	{ "CplCmdOut", 48, 8 },
10894 	{ "ESynOut", 47, 1 },
10895 	{ "EAckOut", 46, 1 },
10896 	{ "EFinOut", 45, 1 },
10897 	{ "ERstOut", 44, 1 },
10898 	{ "SynIn", 43, 1 },
10899 	{ "AckIn", 42, 1 },
10900 	{ "FinIn", 41, 1 },
10901 	{ "RstIn", 40, 1 },
10902 	{ "DataIn", 39, 1 },
10903 	{ "DataInVld", 38, 1 },
10904 	{ "PadIn", 37, 1 },
10905 	{ "RxBufEmpty", 36, 1 },
10906 	{ "RxDdp", 35, 1 },
10907 	{ "RxFbCongestion", 34, 1 },
10908 	{ "TxFbCongestion", 33, 1 },
10909 	{ "TxPktSumSrdy", 32, 1 },
10910 	{ "RcfUlpType", 28, 4 },
10911 	{ "Eread", 27, 1 },
10912 	{ "Ebypass", 26, 1 },
10913 	{ "Esave", 25, 1 },
10914 	{ "Static0", 24, 1 },
10915 	{ "Cread", 23, 1 },
10916 	{ "Cbypass", 22, 1 },
10917 	{ "Csave", 21, 1 },
10918 	{ "CPktOut", 20, 1 },
10919 	{ "RxPagePoolFull", 18, 2 },
10920 	{ "RxLpbkPkt", 17, 1 },
10921 	{ "TxLpbkPkt", 16, 1 },
10922 	{ "RxVfValid", 15, 1 },
10923 	{ "SynLearned", 14, 1 },
10924 	{ "SetDelEntry", 13, 1 },
10925 	{ "SetInvEntry", 12, 1 },
10926 	{ "CpcmdDvld", 11, 1 },
10927 	{ "CpcmdSave", 10, 1 },
10928 	{ "RxPstructsFull", 8, 2 },
10929 	{ "EpcmdDvld", 7, 1 },
10930 	{ "EpcmdFlush", 6, 1 },
10931 	{ "EpcmdTrimPrefix", 5, 1 },
10932 	{ "EpcmdTrimPostfix", 4, 1 },
10933 	{ "ERssIp4Pkt", 3, 1 },
10934 	{ "ERssIp6Pkt", 2, 1 },
10935 	{ "ERssTcpUdpPkt", 1, 1 },
10936 	{ "ERssFceFipPkt", 0, 1 },
10937 	{ NULL }
10938 };
10939 
10940 static const struct field_desc tp_la2[] = {
10941 	{ "CplCmdIn", 56, 8 },
10942 	{ "MpsVfVld", 55, 1 },
10943 	{ "MpsPf", 52, 3 },
10944 	{ "MpsVf", 44, 8 },
10945 	{ "SynIn", 43, 1 },
10946 	{ "AckIn", 42, 1 },
10947 	{ "FinIn", 41, 1 },
10948 	{ "RstIn", 40, 1 },
10949 	{ "DataIn", 39, 1 },
10950 	{ "DataInVld", 38, 1 },
10951 	{ "PadIn", 37, 1 },
10952 	{ "RxBufEmpty", 36, 1 },
10953 	{ "RxDdp", 35, 1 },
10954 	{ "RxFbCongestion", 34, 1 },
10955 	{ "TxFbCongestion", 33, 1 },
10956 	{ "TxPktSumSrdy", 32, 1 },
10957 	{ "RcfUlpType", 28, 4 },
10958 	{ "Eread", 27, 1 },
10959 	{ "Ebypass", 26, 1 },
10960 	{ "Esave", 25, 1 },
10961 	{ "Static0", 24, 1 },
10962 	{ "Cread", 23, 1 },
10963 	{ "Cbypass", 22, 1 },
10964 	{ "Csave", 21, 1 },
10965 	{ "CPktOut", 20, 1 },
10966 	{ "RxPagePoolFull", 18, 2 },
10967 	{ "RxLpbkPkt", 17, 1 },
10968 	{ "TxLpbkPkt", 16, 1 },
10969 	{ "RxVfValid", 15, 1 },
10970 	{ "SynLearned", 14, 1 },
10971 	{ "SetDelEntry", 13, 1 },
10972 	{ "SetInvEntry", 12, 1 },
10973 	{ "CpcmdDvld", 11, 1 },
10974 	{ "CpcmdSave", 10, 1 },
10975 	{ "RxPstructsFull", 8, 2 },
10976 	{ "EpcmdDvld", 7, 1 },
10977 	{ "EpcmdFlush", 6, 1 },
10978 	{ "EpcmdTrimPrefix", 5, 1 },
10979 	{ "EpcmdTrimPostfix", 4, 1 },
10980 	{ "ERssIp4Pkt", 3, 1 },
10981 	{ "ERssIp6Pkt", 2, 1 },
10982 	{ "ERssTcpUdpPkt", 1, 1 },
10983 	{ "ERssFceFipPkt", 0, 1 },
10984 	{ NULL }
10985 };
10986 
10987 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)10988 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
10989 {
10990 
10991 	field_desc_show(sb, *p, tp_la0);
10992 }
10993 
10994 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)10995 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
10996 {
10997 
10998 	if (idx)
10999 		sbuf_printf(sb, "\n");
11000 	field_desc_show(sb, p[0], tp_la0);
11001 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11002 		field_desc_show(sb, p[1], tp_la0);
11003 }
11004 
11005 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)11006 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
11007 {
11008 
11009 	if (idx)
11010 		sbuf_printf(sb, "\n");
11011 	field_desc_show(sb, p[0], tp_la0);
11012 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11013 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
11014 }
11015 
11016 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)11017 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
11018 {
11019 	struct adapter *sc = arg1;
11020 	struct sbuf *sb;
11021 	uint64_t *buf, *p;
11022 	int rc;
11023 	u_int i, inc;
11024 	void (*show_func)(struct sbuf *, uint64_t *, int);
11025 
11026 	rc = 0;
11027 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11028 	if (sb == NULL)
11029 		return (ENOMEM);
11030 
11031 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11032 
11033 	mtx_lock(&sc->reg_lock);
11034 	if (hw_off_limits(sc))
11035 		rc = ENXIO;
11036 	else {
11037 		t4_tp_read_la(sc, buf, NULL);
11038 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11039 		case 2:
11040 			inc = 2;
11041 			show_func = tp_la_show2;
11042 			break;
11043 		case 3:
11044 			inc = 2;
11045 			show_func = tp_la_show3;
11046 			break;
11047 		default:
11048 			inc = 1;
11049 			show_func = tp_la_show;
11050 		}
11051 	}
11052 	mtx_unlock(&sc->reg_lock);
11053 	if (rc != 0)
11054 		goto done;
11055 
11056 	p = buf;
11057 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
11058 		(*show_func)(sb, p, i);
11059 	rc = sbuf_finish(sb);
11060 done:
11061 	sbuf_delete(sb);
11062 	free(buf, M_CXGBE);
11063 	return (rc);
11064 }
11065 
11066 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)11067 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
11068 {
11069 	struct adapter *sc = arg1;
11070 	struct sbuf *sb;
11071 	int rc;
11072 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
11073 
11074 	rc = 0;
11075 	mtx_lock(&sc->reg_lock);
11076 	if (hw_off_limits(sc))
11077 		rc = ENXIO;
11078 	else
11079 		t4_get_chan_txrate(sc, nrate, orate);
11080 	mtx_unlock(&sc->reg_lock);
11081 	if (rc != 0)
11082 		return (rc);
11083 
11084 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11085 	if (sb == NULL)
11086 		return (ENOMEM);
11087 
11088 	if (sc->chip_params->nchan > 2) {
11089 		sbuf_printf(sb, "              channel 0   channel 1"
11090 		    "   channel 2   channel 3\n");
11091 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
11092 		    nrate[0], nrate[1], nrate[2], nrate[3]);
11093 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
11094 		    orate[0], orate[1], orate[2], orate[3]);
11095 	} else {
11096 		sbuf_printf(sb, "              channel 0   channel 1\n");
11097 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
11098 		    nrate[0], nrate[1]);
11099 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
11100 		    orate[0], orate[1]);
11101 	}
11102 
11103 	rc = sbuf_finish(sb);
11104 	sbuf_delete(sb);
11105 
11106 	return (rc);
11107 }
11108 
11109 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)11110 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
11111 {
11112 	struct adapter *sc = arg1;
11113 	struct sbuf *sb;
11114 	uint32_t *buf, *p;
11115 	int rc, i;
11116 
11117 	rc = 0;
11118 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11119 	if (sb == NULL)
11120 		return (ENOMEM);
11121 
11122 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
11123 	    M_ZERO | M_WAITOK);
11124 
11125 	mtx_lock(&sc->reg_lock);
11126 	if (hw_off_limits(sc))
11127 		rc = ENXIO;
11128 	else
11129 		t4_ulprx_read_la(sc, buf);
11130 	mtx_unlock(&sc->reg_lock);
11131 	if (rc != 0)
11132 		goto done;
11133 
11134 	p = buf;
11135 	sbuf_printf(sb, "      Pcmd        Type   Message"
11136 	    "                Data");
11137 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
11138 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
11139 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
11140 	}
11141 	rc = sbuf_finish(sb);
11142 done:
11143 	sbuf_delete(sb);
11144 	free(buf, M_CXGBE);
11145 	return (rc);
11146 }
11147 
11148 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)11149 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
11150 {
11151 	struct adapter *sc = arg1;
11152 	struct sbuf *sb;
11153 	int rc;
11154 	uint32_t cfg, s1, s2;
11155 
11156 	MPASS(chip_id(sc) >= CHELSIO_T5);
11157 
11158 	rc = 0;
11159 	mtx_lock(&sc->reg_lock);
11160 	if (hw_off_limits(sc))
11161 		rc = ENXIO;
11162 	else {
11163 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
11164 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
11165 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
11166 	}
11167 	mtx_unlock(&sc->reg_lock);
11168 	if (rc != 0)
11169 		return (rc);
11170 
11171 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11172 	if (sb == NULL)
11173 		return (ENOMEM);
11174 
11175 	if (G_STATSOURCE_T5(cfg) == 7) {
11176 		int mode;
11177 
11178 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
11179 		if (mode == 0)
11180 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
11181 		else if (mode == 1)
11182 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
11183 		else
11184 			sbuf_printf(sb, "unknown mode %d", mode);
11185 	}
11186 	rc = sbuf_finish(sb);
11187 	sbuf_delete(sb);
11188 
11189 	return (rc);
11190 }
11191 
11192 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)11193 sysctl_cpus(SYSCTL_HANDLER_ARGS)
11194 {
11195 	struct adapter *sc = arg1;
11196 	enum cpu_sets op = arg2;
11197 	cpuset_t cpuset;
11198 	struct sbuf *sb;
11199 	int i, rc;
11200 
11201 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
11202 
11203 	CPU_ZERO(&cpuset);
11204 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
11205 	if (rc != 0)
11206 		return (rc);
11207 
11208 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11209 	if (sb == NULL)
11210 		return (ENOMEM);
11211 
11212 	CPU_FOREACH(i)
11213 		sbuf_printf(sb, "%d ", i);
11214 	rc = sbuf_finish(sb);
11215 	sbuf_delete(sb);
11216 
11217 	return (rc);
11218 }
11219 
11220 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)11221 sysctl_reset(SYSCTL_HANDLER_ARGS)
11222 {
11223 	struct adapter *sc = arg1;
11224 	u_int val;
11225 	int rc;
11226 
11227 	val = atomic_load_int(&sc->num_resets);
11228 	rc = sysctl_handle_int(oidp, &val, 0, req);
11229 	if (rc != 0 || req->newptr == NULL)
11230 		return (rc);
11231 
11232 	if (val == 0) {
11233 		/* Zero out the counter that tracks reset. */
11234 		atomic_store_int(&sc->num_resets, 0);
11235 		return (0);
11236 	}
11237 
11238 	if (val != 1)
11239 		return (EINVAL);	/* 0 or 1 are the only legal values */
11240 
11241 	if (hw_off_limits(sc))		/* harmless race */
11242 		return (EALREADY);
11243 
11244 	taskqueue_enqueue(reset_tq, &sc->reset_task);
11245 	return (0);
11246 }
11247 
11248 #ifdef TCP_OFFLOAD
11249 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)11250 sysctl_tls(SYSCTL_HANDLER_ARGS)
11251 {
11252 	struct adapter *sc = arg1;
11253 	int i, j, v, rc;
11254 	struct vi_info *vi;
11255 
11256 	v = sc->tt.tls;
11257 	rc = sysctl_handle_int(oidp, &v, 0, req);
11258 	if (rc != 0 || req->newptr == NULL)
11259 		return (rc);
11260 
11261 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11262 		return (ENOTSUP);
11263 
11264 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
11265 	if (rc)
11266 		return (rc);
11267 	if (hw_off_limits(sc))
11268 		rc = ENXIO;
11269 	else {
11270 		sc->tt.tls = !!v;
11271 		for_each_port(sc, i) {
11272 			for_each_vi(sc->port[i], j, vi) {
11273 				if (vi->flags & VI_INIT_DONE)
11274 					t4_update_fl_bufsize(vi->ifp);
11275 			}
11276 		}
11277 	}
11278 	end_synchronized_op(sc, 0);
11279 
11280 	return (rc);
11281 
11282 }
11283 
11284 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)11285 unit_conv(char *buf, size_t len, u_int val, u_int factor)
11286 {
11287 	u_int rem = val % factor;
11288 
11289 	if (rem == 0)
11290 		snprintf(buf, len, "%u", val / factor);
11291 	else {
11292 		while (rem % 10 == 0)
11293 			rem /= 10;
11294 		snprintf(buf, len, "%u.%u", val / factor, rem);
11295 	}
11296 }
11297 
11298 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)11299 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
11300 {
11301 	struct adapter *sc = arg1;
11302 	char buf[16];
11303 	u_int res, re;
11304 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11305 
11306 	mtx_lock(&sc->reg_lock);
11307 	if (hw_off_limits(sc))
11308 		res = (u_int)-1;
11309 	else
11310 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
11311 	mtx_unlock(&sc->reg_lock);
11312 	if (res == (u_int)-1)
11313 		return (ENXIO);
11314 
11315 	switch (arg2) {
11316 	case 0:
11317 		/* timer_tick */
11318 		re = G_TIMERRESOLUTION(res);
11319 		break;
11320 	case 1:
11321 		/* TCP timestamp tick */
11322 		re = G_TIMESTAMPRESOLUTION(res);
11323 		break;
11324 	case 2:
11325 		/* DACK tick */
11326 		re = G_DELAYEDACKRESOLUTION(res);
11327 		break;
11328 	default:
11329 		return (EDOOFUS);
11330 	}
11331 
11332 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
11333 
11334 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
11335 }
11336 
11337 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)11338 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
11339 {
11340 	struct adapter *sc = arg1;
11341 	int rc;
11342 	u_int dack_tmr, dack_re, v;
11343 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11344 
11345 	mtx_lock(&sc->reg_lock);
11346 	if (hw_off_limits(sc))
11347 		rc = ENXIO;
11348 	else {
11349 		rc = 0;
11350 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
11351 		    A_TP_TIMER_RESOLUTION));
11352 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
11353 	}
11354 	mtx_unlock(&sc->reg_lock);
11355 	if (rc != 0)
11356 		return (rc);
11357 
11358 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
11359 
11360 	return (sysctl_handle_int(oidp, &v, 0, req));
11361 }
11362 
11363 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)11364 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
11365 {
11366 	struct adapter *sc = arg1;
11367 	int rc, reg = arg2;
11368 	u_int tre;
11369 	u_long tp_tick_us, v;
11370 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11371 
11372 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
11373 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
11374 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
11375 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
11376 
11377 	mtx_lock(&sc->reg_lock);
11378 	if (hw_off_limits(sc))
11379 		rc = ENXIO;
11380 	else {
11381 		rc = 0;
11382 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
11383 		tp_tick_us = (cclk_ps << tre) / 1000000;
11384 		if (reg == A_TP_INIT_SRTT)
11385 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
11386 		else
11387 			v = tp_tick_us * t4_read_reg(sc, reg);
11388 	}
11389 	mtx_unlock(&sc->reg_lock);
11390 	if (rc != 0)
11391 		return (rc);
11392 	else
11393 		return (sysctl_handle_long(oidp, &v, 0, req));
11394 }
11395 
11396 /*
11397  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
11398  * passed to this function.
11399  */
11400 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)11401 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
11402 {
11403 	struct adapter *sc = arg1;
11404 	int rc, idx = arg2;
11405 	u_int v;
11406 
11407 	MPASS(idx >= 0 && idx <= 24);
11408 
11409 	mtx_lock(&sc->reg_lock);
11410 	if (hw_off_limits(sc))
11411 		rc = ENXIO;
11412 	else {
11413 		rc = 0;
11414 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
11415 	}
11416 	mtx_unlock(&sc->reg_lock);
11417 	if (rc != 0)
11418 		return (rc);
11419 	else
11420 		return (sysctl_handle_int(oidp, &v, 0, req));
11421 }
11422 
11423 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)11424 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
11425 {
11426 	struct adapter *sc = arg1;
11427 	int rc, idx = arg2;
11428 	u_int shift, v, r;
11429 
11430 	MPASS(idx >= 0 && idx < 16);
11431 
11432 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
11433 	shift = (idx & 3) << 3;
11434 	mtx_lock(&sc->reg_lock);
11435 	if (hw_off_limits(sc))
11436 		rc = ENXIO;
11437 	else {
11438 		rc = 0;
11439 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
11440 	}
11441 	mtx_unlock(&sc->reg_lock);
11442 	if (rc != 0)
11443 		return (rc);
11444 	else
11445 		return (sysctl_handle_int(oidp, &v, 0, req));
11446 }
11447 
11448 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)11449 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
11450 {
11451 	struct vi_info *vi = arg1;
11452 	struct adapter *sc = vi->adapter;
11453 	int idx, rc, i;
11454 	struct sge_ofld_rxq *ofld_rxq;
11455 	uint8_t v;
11456 
11457 	idx = vi->ofld_tmr_idx;
11458 
11459 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11460 	if (rc != 0 || req->newptr == NULL)
11461 		return (rc);
11462 
11463 	if (idx < 0 || idx >= SGE_NTIMERS)
11464 		return (EINVAL);
11465 
11466 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11467 	    "t4otmr");
11468 	if (rc)
11469 		return (rc);
11470 
11471 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
11472 	for_each_ofld_rxq(vi, i, ofld_rxq) {
11473 #ifdef atomic_store_rel_8
11474 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
11475 #else
11476 		ofld_rxq->iq.intr_params = v;
11477 #endif
11478 	}
11479 	vi->ofld_tmr_idx = idx;
11480 
11481 	end_synchronized_op(sc, LOCK_HELD);
11482 	return (0);
11483 }
11484 
11485 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)11486 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
11487 {
11488 	struct vi_info *vi = arg1;
11489 	struct adapter *sc = vi->adapter;
11490 	int idx, rc;
11491 
11492 	idx = vi->ofld_pktc_idx;
11493 
11494 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11495 	if (rc != 0 || req->newptr == NULL)
11496 		return (rc);
11497 
11498 	if (idx < -1 || idx >= SGE_NCOUNTERS)
11499 		return (EINVAL);
11500 
11501 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11502 	    "t4opktc");
11503 	if (rc)
11504 		return (rc);
11505 
11506 	if (vi->flags & VI_INIT_DONE)
11507 		rc = EBUSY; /* cannot be changed once the queues are created */
11508 	else
11509 		vi->ofld_pktc_idx = idx;
11510 
11511 	end_synchronized_op(sc, LOCK_HELD);
11512 	return (rc);
11513 }
11514 #endif
11515 
11516 static int
get_sge_context(struct adapter * sc,struct t4_sge_context * cntxt)11517 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
11518 {
11519 	int rc;
11520 
11521 	if (cntxt->cid > M_CTXTQID)
11522 		return (EINVAL);
11523 
11524 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
11525 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
11526 		return (EINVAL);
11527 
11528 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
11529 	if (rc)
11530 		return (rc);
11531 
11532 	if (hw_off_limits(sc)) {
11533 		rc = ENXIO;
11534 		goto done;
11535 	}
11536 
11537 	if (sc->flags & FW_OK) {
11538 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
11539 		    &cntxt->data[0]);
11540 		if (rc == 0)
11541 			goto done;
11542 	}
11543 
11544 	/*
11545 	 * Read via firmware failed or wasn't even attempted.  Read directly via
11546 	 * the backdoor.
11547 	 */
11548 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
11549 done:
11550 	end_synchronized_op(sc, 0);
11551 	return (rc);
11552 }
11553 
11554 static int
load_fw(struct adapter * sc,struct t4_data * fw)11555 load_fw(struct adapter *sc, struct t4_data *fw)
11556 {
11557 	int rc;
11558 	uint8_t *fw_data;
11559 
11560 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
11561 	if (rc)
11562 		return (rc);
11563 
11564 	if (hw_off_limits(sc)) {
11565 		rc = ENXIO;
11566 		goto done;
11567 	}
11568 
11569 	/*
11570 	 * The firmware, with the sole exception of the memory parity error
11571 	 * handler, runs from memory and not flash.  It is almost always safe to
11572 	 * install a new firmware on a running system.  Just set bit 1 in
11573 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
11574 	 */
11575 	if (sc->flags & FULL_INIT_DONE &&
11576 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
11577 		rc = EBUSY;
11578 		goto done;
11579 	}
11580 
11581 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
11582 
11583 	rc = copyin(fw->data, fw_data, fw->len);
11584 	if (rc == 0)
11585 		rc = -t4_load_fw(sc, fw_data, fw->len);
11586 
11587 	free(fw_data, M_CXGBE);
11588 done:
11589 	end_synchronized_op(sc, 0);
11590 	return (rc);
11591 }
11592 
11593 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)11594 load_cfg(struct adapter *sc, struct t4_data *cfg)
11595 {
11596 	int rc;
11597 	uint8_t *cfg_data = NULL;
11598 
11599 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11600 	if (rc)
11601 		return (rc);
11602 
11603 	if (hw_off_limits(sc)) {
11604 		rc = ENXIO;
11605 		goto done;
11606 	}
11607 
11608 	if (cfg->len == 0) {
11609 		/* clear */
11610 		rc = -t4_load_cfg(sc, NULL, 0);
11611 		goto done;
11612 	}
11613 
11614 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
11615 
11616 	rc = copyin(cfg->data, cfg_data, cfg->len);
11617 	if (rc == 0)
11618 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
11619 
11620 	free(cfg_data, M_CXGBE);
11621 done:
11622 	end_synchronized_op(sc, 0);
11623 	return (rc);
11624 }
11625 
11626 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)11627 load_boot(struct adapter *sc, struct t4_bootrom *br)
11628 {
11629 	int rc;
11630 	uint8_t *br_data = NULL;
11631 	u_int offset;
11632 
11633 	if (br->len > 1024 * 1024)
11634 		return (EFBIG);
11635 
11636 	if (br->pf_offset == 0) {
11637 		/* pfidx */
11638 		if (br->pfidx_addr > 7)
11639 			return (EINVAL);
11640 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
11641 		    A_PCIE_PF_EXPROM_OFST)));
11642 	} else if (br->pf_offset == 1) {
11643 		/* offset */
11644 		offset = G_OFFSET(br->pfidx_addr);
11645 	} else {
11646 		return (EINVAL);
11647 	}
11648 
11649 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
11650 	if (rc)
11651 		return (rc);
11652 
11653 	if (hw_off_limits(sc)) {
11654 		rc = ENXIO;
11655 		goto done;
11656 	}
11657 
11658 	if (br->len == 0) {
11659 		/* clear */
11660 		rc = -t4_load_boot(sc, NULL, offset, 0);
11661 		goto done;
11662 	}
11663 
11664 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
11665 
11666 	rc = copyin(br->data, br_data, br->len);
11667 	if (rc == 0)
11668 		rc = -t4_load_boot(sc, br_data, offset, br->len);
11669 
11670 	free(br_data, M_CXGBE);
11671 done:
11672 	end_synchronized_op(sc, 0);
11673 	return (rc);
11674 }
11675 
11676 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)11677 load_bootcfg(struct adapter *sc, struct t4_data *bc)
11678 {
11679 	int rc;
11680 	uint8_t *bc_data = NULL;
11681 
11682 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11683 	if (rc)
11684 		return (rc);
11685 
11686 	if (hw_off_limits(sc)) {
11687 		rc = ENXIO;
11688 		goto done;
11689 	}
11690 
11691 	if (bc->len == 0) {
11692 		/* clear */
11693 		rc = -t4_load_bootcfg(sc, NULL, 0);
11694 		goto done;
11695 	}
11696 
11697 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
11698 
11699 	rc = copyin(bc->data, bc_data, bc->len);
11700 	if (rc == 0)
11701 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
11702 
11703 	free(bc_data, M_CXGBE);
11704 done:
11705 	end_synchronized_op(sc, 0);
11706 	return (rc);
11707 }
11708 
11709 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)11710 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
11711 {
11712 	int rc;
11713 	struct cudbg_init *cudbg;
11714 	void *handle, *buf;
11715 
11716 	/* buf is large, don't block if no memory is available */
11717 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
11718 	if (buf == NULL)
11719 		return (ENOMEM);
11720 
11721 	handle = cudbg_alloc_handle();
11722 	if (handle == NULL) {
11723 		rc = ENOMEM;
11724 		goto done;
11725 	}
11726 
11727 	cudbg = cudbg_get_init(handle);
11728 	cudbg->adap = sc;
11729 	cudbg->print = (cudbg_print_cb)printf;
11730 
11731 #ifndef notyet
11732 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
11733 	    __func__, dump->wr_flash, dump->len, dump->data);
11734 #endif
11735 
11736 	if (dump->wr_flash)
11737 		cudbg->use_flash = 1;
11738 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
11739 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
11740 
11741 	rc = cudbg_collect(handle, buf, &dump->len);
11742 	if (rc != 0)
11743 		goto done;
11744 
11745 	rc = copyout(buf, dump->data, dump->len);
11746 done:
11747 	cudbg_free_handle(handle);
11748 	free(buf, M_CXGBE);
11749 	return (rc);
11750 }
11751 
11752 static void
free_offload_policy(struct t4_offload_policy * op)11753 free_offload_policy(struct t4_offload_policy *op)
11754 {
11755 	struct offload_rule *r;
11756 	int i;
11757 
11758 	if (op == NULL)
11759 		return;
11760 
11761 	r = &op->rule[0];
11762 	for (i = 0; i < op->nrules; i++, r++) {
11763 		free(r->bpf_prog.bf_insns, M_CXGBE);
11764 	}
11765 	free(op->rule, M_CXGBE);
11766 	free(op, M_CXGBE);
11767 }
11768 
11769 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)11770 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
11771 {
11772 	int i, rc, len;
11773 	struct t4_offload_policy *op, *old;
11774 	struct bpf_program *bf;
11775 	const struct offload_settings *s;
11776 	struct offload_rule *r;
11777 	void *u;
11778 
11779 	if (!is_offload(sc))
11780 		return (ENODEV);
11781 
11782 	if (uop->nrules == 0) {
11783 		/* Delete installed policies. */
11784 		op = NULL;
11785 		goto set_policy;
11786 	} else if (uop->nrules > 256) { /* arbitrary */
11787 		return (E2BIG);
11788 	}
11789 
11790 	/* Copy userspace offload policy to kernel */
11791 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
11792 	op->nrules = uop->nrules;
11793 	len = op->nrules * sizeof(struct offload_rule);
11794 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11795 	rc = copyin(uop->rule, op->rule, len);
11796 	if (rc) {
11797 		free(op->rule, M_CXGBE);
11798 		free(op, M_CXGBE);
11799 		return (rc);
11800 	}
11801 
11802 	r = &op->rule[0];
11803 	for (i = 0; i < op->nrules; i++, r++) {
11804 
11805 		/* Validate open_type */
11806 		if (r->open_type != OPEN_TYPE_LISTEN &&
11807 		    r->open_type != OPEN_TYPE_ACTIVE &&
11808 		    r->open_type != OPEN_TYPE_PASSIVE &&
11809 		    r->open_type != OPEN_TYPE_DONTCARE) {
11810 error:
11811 			/*
11812 			 * Rules 0 to i have malloc'd filters that need to be
11813 			 * freed.  Rules i+1 to nrules have userspace pointers
11814 			 * and should be left alone.
11815 			 */
11816 			op->nrules = i;
11817 			free_offload_policy(op);
11818 			return (rc);
11819 		}
11820 
11821 		/* Validate settings */
11822 		s = &r->settings;
11823 		if ((s->offload != 0 && s->offload != 1) ||
11824 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
11825 		    s->sched_class < -1 ||
11826 		    s->sched_class >= sc->params.nsched_cls) {
11827 			rc = EINVAL;
11828 			goto error;
11829 		}
11830 
11831 		bf = &r->bpf_prog;
11832 		u = bf->bf_insns;	/* userspace ptr */
11833 		bf->bf_insns = NULL;
11834 		if (bf->bf_len == 0) {
11835 			/* legal, matches everything */
11836 			continue;
11837 		}
11838 		len = bf->bf_len * sizeof(*bf->bf_insns);
11839 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11840 		rc = copyin(u, bf->bf_insns, len);
11841 		if (rc != 0)
11842 			goto error;
11843 
11844 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
11845 			rc = EINVAL;
11846 			goto error;
11847 		}
11848 	}
11849 set_policy:
11850 	rw_wlock(&sc->policy_lock);
11851 	old = sc->policy;
11852 	sc->policy = op;
11853 	rw_wunlock(&sc->policy_lock);
11854 	free_offload_policy(old);
11855 
11856 	return (0);
11857 }
11858 
11859 #define MAX_READ_BUF_SIZE (128 * 1024)
11860 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)11861 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
11862 {
11863 	uint32_t addr, remaining, n;
11864 	uint32_t *buf;
11865 	int rc;
11866 	uint8_t *dst;
11867 
11868 	mtx_lock(&sc->reg_lock);
11869 	if (hw_off_limits(sc))
11870 		rc = ENXIO;
11871 	else
11872 		rc = validate_mem_range(sc, mr->addr, mr->len);
11873 	mtx_unlock(&sc->reg_lock);
11874 	if (rc != 0)
11875 		return (rc);
11876 
11877 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
11878 	addr = mr->addr;
11879 	remaining = mr->len;
11880 	dst = (void *)mr->data;
11881 
11882 	while (remaining) {
11883 		n = min(remaining, MAX_READ_BUF_SIZE);
11884 		mtx_lock(&sc->reg_lock);
11885 		if (hw_off_limits(sc))
11886 			rc = ENXIO;
11887 		else
11888 			read_via_memwin(sc, 2, addr, buf, n);
11889 		mtx_unlock(&sc->reg_lock);
11890 		if (rc != 0)
11891 			break;
11892 
11893 		rc = copyout(buf, dst, n);
11894 		if (rc != 0)
11895 			break;
11896 
11897 		dst += n;
11898 		remaining -= n;
11899 		addr += n;
11900 	}
11901 
11902 	free(buf, M_CXGBE);
11903 	return (rc);
11904 }
11905 #undef MAX_READ_BUF_SIZE
11906 
11907 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)11908 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
11909 {
11910 	int rc;
11911 
11912 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
11913 		return (EINVAL);
11914 
11915 	if (i2cd->len > sizeof(i2cd->data))
11916 		return (EFBIG);
11917 
11918 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
11919 	if (rc)
11920 		return (rc);
11921 	if (hw_off_limits(sc))
11922 		rc = ENXIO;
11923 	else
11924 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
11925 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
11926 	end_synchronized_op(sc, 0);
11927 
11928 	return (rc);
11929 }
11930 
11931 static int
clear_stats(struct adapter * sc,u_int port_id)11932 clear_stats(struct adapter *sc, u_int port_id)
11933 {
11934 	int i, v, chan_map;
11935 	struct port_info *pi;
11936 	struct vi_info *vi;
11937 	struct sge_rxq *rxq;
11938 	struct sge_txq *txq;
11939 	struct sge_wrq *wrq;
11940 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
11941 	struct sge_ofld_txq *ofld_txq;
11942 #endif
11943 #ifdef TCP_OFFLOAD
11944 	struct sge_ofld_rxq *ofld_rxq;
11945 #endif
11946 
11947 	if (port_id >= sc->params.nports)
11948 		return (EINVAL);
11949 	pi = sc->port[port_id];
11950 	if (pi == NULL)
11951 		return (EIO);
11952 
11953 	mtx_lock(&sc->reg_lock);
11954 	if (!hw_off_limits(sc)) {
11955 		/* MAC stats */
11956 		t4_clr_port_stats(sc, pi->tx_chan);
11957 		if (is_t6(sc)) {
11958 			if (pi->fcs_reg != -1)
11959 				pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
11960 			else
11961 				pi->stats.rx_fcs_err = 0;
11962 		}
11963 		for_each_vi(pi, v, vi) {
11964 			if (vi->flags & VI_INIT_DONE)
11965 				t4_clr_vi_stats(sc, vi->vin);
11966 		}
11967 		chan_map = pi->rx_e_chan_map;
11968 		v = 0;	/* reuse */
11969 		while (chan_map) {
11970 			i = ffs(chan_map) - 1;
11971 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
11972 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
11973 			chan_map &= ~(1 << i);
11974 		}
11975 	}
11976 	mtx_unlock(&sc->reg_lock);
11977 	pi->tx_parse_error = 0;
11978 	pi->tnl_cong_drops = 0;
11979 
11980 	/*
11981 	 * Since this command accepts a port, clear stats for
11982 	 * all VIs on this port.
11983 	 */
11984 	for_each_vi(pi, v, vi) {
11985 		if (vi->flags & VI_INIT_DONE) {
11986 
11987 			for_each_rxq(vi, i, rxq) {
11988 #if defined(INET) || defined(INET6)
11989 				rxq->lro.lro_queued = 0;
11990 				rxq->lro.lro_flushed = 0;
11991 #endif
11992 				rxq->rxcsum = 0;
11993 				rxq->vlan_extraction = 0;
11994 				rxq->vxlan_rxcsum = 0;
11995 
11996 				rxq->fl.cl_allocated = 0;
11997 				rxq->fl.cl_recycled = 0;
11998 				rxq->fl.cl_fast_recycled = 0;
11999 			}
12000 
12001 			for_each_txq(vi, i, txq) {
12002 				txq->txcsum = 0;
12003 				txq->tso_wrs = 0;
12004 				txq->vlan_insertion = 0;
12005 				txq->imm_wrs = 0;
12006 				txq->sgl_wrs = 0;
12007 				txq->txpkt_wrs = 0;
12008 				txq->txpkts0_wrs = 0;
12009 				txq->txpkts1_wrs = 0;
12010 				txq->txpkts0_pkts = 0;
12011 				txq->txpkts1_pkts = 0;
12012 				txq->txpkts_flush = 0;
12013 				txq->raw_wrs = 0;
12014 				txq->vxlan_tso_wrs = 0;
12015 				txq->vxlan_txcsum = 0;
12016 				txq->kern_tls_records = 0;
12017 				txq->kern_tls_short = 0;
12018 				txq->kern_tls_partial = 0;
12019 				txq->kern_tls_full = 0;
12020 				txq->kern_tls_octets = 0;
12021 				txq->kern_tls_waste = 0;
12022 				txq->kern_tls_options = 0;
12023 				txq->kern_tls_header = 0;
12024 				txq->kern_tls_fin = 0;
12025 				txq->kern_tls_fin_short = 0;
12026 				txq->kern_tls_cbc = 0;
12027 				txq->kern_tls_gcm = 0;
12028 				mp_ring_reset_stats(txq->r);
12029 			}
12030 
12031 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12032 			for_each_ofld_txq(vi, i, ofld_txq) {
12033 				ofld_txq->wrq.tx_wrs_direct = 0;
12034 				ofld_txq->wrq.tx_wrs_copied = 0;
12035 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12036 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12037 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12038 				counter_u64_zero(ofld_txq->tx_aio_jobs);
12039 				counter_u64_zero(ofld_txq->tx_aio_octets);
12040 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
12041 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
12042 			}
12043 #endif
12044 #ifdef TCP_OFFLOAD
12045 			for_each_ofld_rxq(vi, i, ofld_rxq) {
12046 				ofld_rxq->fl.cl_allocated = 0;
12047 				ofld_rxq->fl.cl_recycled = 0;
12048 				ofld_rxq->fl.cl_fast_recycled = 0;
12049 				counter_u64_zero(
12050 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
12051 				counter_u64_zero(
12052 				    ofld_rxq->rx_iscsi_ddp_setup_error);
12053 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
12054 				ofld_rxq->rx_iscsi_ddp_octets = 0;
12055 				ofld_rxq->rx_iscsi_fl_pdus = 0;
12056 				ofld_rxq->rx_iscsi_fl_octets = 0;
12057 				ofld_rxq->rx_aio_ddp_jobs = 0;
12058 				ofld_rxq->rx_aio_ddp_octets = 0;
12059 				ofld_rxq->rx_toe_tls_records = 0;
12060 				ofld_rxq->rx_toe_tls_octets = 0;
12061 				ofld_rxq->rx_toe_ddp_octets = 0;
12062 				counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
12063 				counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
12064 				counter_u64_zero(ofld_rxq->ddp_buffer_free);
12065 			}
12066 #endif
12067 
12068 			if (IS_MAIN_VI(vi)) {
12069 				wrq = &sc->sge.ctrlq[pi->port_id];
12070 				wrq->tx_wrs_direct = 0;
12071 				wrq->tx_wrs_copied = 0;
12072 			}
12073 		}
12074 	}
12075 
12076 	return (0);
12077 }
12078 
12079 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12080 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12081 {
12082 #ifdef INET6
12083 	struct in6_addr in6;
12084 
12085 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12086 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
12087 		return (0);
12088 	else
12089 		return (EIO);
12090 #else
12091 	return (ENOTSUP);
12092 #endif
12093 }
12094 
12095 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12096 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12097 {
12098 #ifdef INET6
12099 	struct in6_addr in6;
12100 
12101 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12102 	return (t4_release_clip_addr(sc, &in6));
12103 #else
12104 	return (ENOTSUP);
12105 #endif
12106 }
12107 
12108 int
t4_os_find_pci_capability(struct adapter * sc,int cap)12109 t4_os_find_pci_capability(struct adapter *sc, int cap)
12110 {
12111 	int i;
12112 
12113 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
12114 }
12115 
12116 int
t4_os_pci_save_state(struct adapter * sc)12117 t4_os_pci_save_state(struct adapter *sc)
12118 {
12119 	device_t dev;
12120 	struct pci_devinfo *dinfo;
12121 
12122 	dev = sc->dev;
12123 	dinfo = device_get_ivars(dev);
12124 
12125 	pci_cfg_save(dev, dinfo, 0);
12126 	return (0);
12127 }
12128 
12129 int
t4_os_pci_restore_state(struct adapter * sc)12130 t4_os_pci_restore_state(struct adapter *sc)
12131 {
12132 	device_t dev;
12133 	struct pci_devinfo *dinfo;
12134 
12135 	dev = sc->dev;
12136 	dinfo = device_get_ivars(dev);
12137 
12138 	pci_cfg_restore(dev, dinfo);
12139 	return (0);
12140 }
12141 
12142 void
t4_os_portmod_changed(struct port_info * pi)12143 t4_os_portmod_changed(struct port_info *pi)
12144 {
12145 	struct adapter *sc = pi->adapter;
12146 	struct vi_info *vi;
12147 	if_t ifp;
12148 	static const char *mod_str[] = {
12149 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
12150 	};
12151 
12152 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
12153 	    ("%s: port_type %u", __func__, pi->port_type));
12154 
12155 	vi = &pi->vi[0];
12156 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
12157 		PORT_LOCK(pi);
12158 		build_medialist(pi);
12159 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
12160 			fixup_link_config(pi);
12161 			apply_link_config(pi);
12162 		}
12163 		PORT_UNLOCK(pi);
12164 		end_synchronized_op(sc, LOCK_HELD);
12165 	}
12166 
12167 	ifp = vi->ifp;
12168 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
12169 		if_printf(ifp, "transceiver unplugged.\n");
12170 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
12171 		if_printf(ifp, "unknown transceiver inserted.\n");
12172 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
12173 		if_printf(ifp, "unsupported transceiver inserted.\n");
12174 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
12175 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
12176 		    port_top_speed(pi), mod_str[pi->mod_type]);
12177 	} else {
12178 		if_printf(ifp, "transceiver (type %d) inserted.\n",
12179 		    pi->mod_type);
12180 	}
12181 }
12182 
12183 void
t4_os_link_changed(struct port_info * pi)12184 t4_os_link_changed(struct port_info *pi)
12185 {
12186 	struct vi_info *vi;
12187 	if_t ifp;
12188 	struct link_config *lc = &pi->link_cfg;
12189 	struct adapter *sc = pi->adapter;
12190 	int v;
12191 
12192 	PORT_LOCK_ASSERT_OWNED(pi);
12193 
12194 	if (is_t6(sc)) {
12195 		if (lc->link_ok) {
12196 			if (lc->speed > 25000 ||
12197 			    (lc->speed == 25000 && lc->fec == FEC_RS)) {
12198 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12199 				    A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS);
12200 			} else {
12201 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12202 				    A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS);
12203 			}
12204 			pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12205 			pi->stats.rx_fcs_err = 0;
12206 		} else {
12207 			pi->fcs_reg = -1;
12208 		}
12209 	} else {
12210 		MPASS(pi->fcs_reg != -1);
12211 		MPASS(pi->fcs_base == 0);
12212 	}
12213 
12214 	for_each_vi(pi, v, vi) {
12215 		ifp = vi->ifp;
12216 		if (ifp == NULL || IS_DETACHING(vi))
12217 			continue;
12218 
12219 		if (lc->link_ok) {
12220 			if_setbaudrate(ifp, IF_Mbps(lc->speed));
12221 			if_link_state_change(ifp, LINK_STATE_UP);
12222 		} else {
12223 			if_link_state_change(ifp, LINK_STATE_DOWN);
12224 		}
12225 	}
12226 }
12227 
12228 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)12229 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
12230 {
12231 	struct adapter *sc;
12232 
12233 	sx_slock(&t4_list_lock);
12234 	SLIST_FOREACH(sc, &t4_list, link) {
12235 		/*
12236 		 * func should not make any assumptions about what state sc is
12237 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
12238 		 */
12239 		func(sc, arg);
12240 	}
12241 	sx_sunlock(&t4_list_lock);
12242 }
12243 
12244 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)12245 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
12246     struct thread *td)
12247 {
12248 	int rc;
12249 	struct adapter *sc = dev->si_drv1;
12250 
12251 	rc = priv_check(td, PRIV_DRIVER);
12252 	if (rc != 0)
12253 		return (rc);
12254 
12255 	switch (cmd) {
12256 	case CHELSIO_T4_GETREG: {
12257 		struct t4_reg *edata = (struct t4_reg *)data;
12258 
12259 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12260 			return (EFAULT);
12261 
12262 		mtx_lock(&sc->reg_lock);
12263 		if (hw_off_limits(sc))
12264 			rc = ENXIO;
12265 		else if (edata->size == 4)
12266 			edata->val = t4_read_reg(sc, edata->addr);
12267 		else if (edata->size == 8)
12268 			edata->val = t4_read_reg64(sc, edata->addr);
12269 		else
12270 			rc = EINVAL;
12271 		mtx_unlock(&sc->reg_lock);
12272 
12273 		break;
12274 	}
12275 	case CHELSIO_T4_SETREG: {
12276 		struct t4_reg *edata = (struct t4_reg *)data;
12277 
12278 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12279 			return (EFAULT);
12280 
12281 		mtx_lock(&sc->reg_lock);
12282 		if (hw_off_limits(sc))
12283 			rc = ENXIO;
12284 		else if (edata->size == 4) {
12285 			if (edata->val & 0xffffffff00000000)
12286 				rc = EINVAL;
12287 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
12288 		} else if (edata->size == 8)
12289 			t4_write_reg64(sc, edata->addr, edata->val);
12290 		else
12291 			rc = EINVAL;
12292 		mtx_unlock(&sc->reg_lock);
12293 
12294 		break;
12295 	}
12296 	case CHELSIO_T4_REGDUMP: {
12297 		struct t4_regdump *regs = (struct t4_regdump *)data;
12298 		int reglen = t4_get_regs_len(sc);
12299 		uint8_t *buf;
12300 
12301 		if (regs->len < reglen) {
12302 			regs->len = reglen; /* hint to the caller */
12303 			return (ENOBUFS);
12304 		}
12305 
12306 		regs->len = reglen;
12307 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
12308 		mtx_lock(&sc->reg_lock);
12309 		if (hw_off_limits(sc))
12310 			rc = ENXIO;
12311 		else
12312 			get_regs(sc, regs, buf);
12313 		mtx_unlock(&sc->reg_lock);
12314 		if (rc == 0)
12315 			rc = copyout(buf, regs->data, reglen);
12316 		free(buf, M_CXGBE);
12317 		break;
12318 	}
12319 	case CHELSIO_T4_GET_FILTER_MODE:
12320 		rc = get_filter_mode(sc, (uint32_t *)data);
12321 		break;
12322 	case CHELSIO_T4_SET_FILTER_MODE:
12323 		rc = set_filter_mode(sc, *(uint32_t *)data);
12324 		break;
12325 	case CHELSIO_T4_SET_FILTER_MASK:
12326 		rc = set_filter_mask(sc, *(uint32_t *)data);
12327 		break;
12328 	case CHELSIO_T4_GET_FILTER:
12329 		rc = get_filter(sc, (struct t4_filter *)data);
12330 		break;
12331 	case CHELSIO_T4_SET_FILTER:
12332 		rc = set_filter(sc, (struct t4_filter *)data);
12333 		break;
12334 	case CHELSIO_T4_DEL_FILTER:
12335 		rc = del_filter(sc, (struct t4_filter *)data);
12336 		break;
12337 	case CHELSIO_T4_GET_SGE_CONTEXT:
12338 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
12339 		break;
12340 	case CHELSIO_T4_LOAD_FW:
12341 		rc = load_fw(sc, (struct t4_data *)data);
12342 		break;
12343 	case CHELSIO_T4_GET_MEM:
12344 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
12345 		break;
12346 	case CHELSIO_T4_GET_I2C:
12347 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
12348 		break;
12349 	case CHELSIO_T4_CLEAR_STATS:
12350 		rc = clear_stats(sc, *(uint32_t *)data);
12351 		break;
12352 	case CHELSIO_T4_SCHED_CLASS:
12353 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
12354 		break;
12355 	case CHELSIO_T4_SCHED_QUEUE:
12356 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
12357 		break;
12358 	case CHELSIO_T4_GET_TRACER:
12359 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
12360 		break;
12361 	case CHELSIO_T4_SET_TRACER:
12362 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
12363 		break;
12364 	case CHELSIO_T4_LOAD_CFG:
12365 		rc = load_cfg(sc, (struct t4_data *)data);
12366 		break;
12367 	case CHELSIO_T4_LOAD_BOOT:
12368 		rc = load_boot(sc, (struct t4_bootrom *)data);
12369 		break;
12370 	case CHELSIO_T4_LOAD_BOOTCFG:
12371 		rc = load_bootcfg(sc, (struct t4_data *)data);
12372 		break;
12373 	case CHELSIO_T4_CUDBG_DUMP:
12374 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
12375 		break;
12376 	case CHELSIO_T4_SET_OFLD_POLICY:
12377 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
12378 		break;
12379 	case CHELSIO_T4_HOLD_CLIP_ADDR:
12380 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
12381 		break;
12382 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
12383 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
12384 		break;
12385 	default:
12386 		rc = ENOTTY;
12387 	}
12388 
12389 	return (rc);
12390 }
12391 
12392 #ifdef TCP_OFFLOAD
12393 int
toe_capability(struct vi_info * vi,bool enable)12394 toe_capability(struct vi_info *vi, bool enable)
12395 {
12396 	int rc;
12397 	struct port_info *pi = vi->pi;
12398 	struct adapter *sc = pi->adapter;
12399 
12400 	ASSERT_SYNCHRONIZED_OP(sc);
12401 
12402 	if (!is_offload(sc))
12403 		return (ENODEV);
12404 	if (hw_off_limits(sc))
12405 		return (ENXIO);
12406 
12407 	if (enable) {
12408 #ifdef KERN_TLS
12409 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
12410 			int i, j, n;
12411 			struct port_info *p;
12412 			struct vi_info *v;
12413 
12414 			/*
12415 			 * Reconfigure hardware for TOE if TXTLS is not enabled
12416 			 * on any ifnet.
12417 			 */
12418 			n = 0;
12419 			for_each_port(sc, i) {
12420 				p = sc->port[i];
12421 				for_each_vi(p, j, v) {
12422 					if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
12423 						CH_WARN(sc,
12424 						    "%s has NIC TLS enabled.\n",
12425 						    device_get_nameunit(v->dev));
12426 						n++;
12427 					}
12428 				}
12429 			}
12430 			if (n > 0) {
12431 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
12432 				    "associated with this adapter before "
12433 				    "trying to enable TOE.\n");
12434 				return (EAGAIN);
12435 			}
12436 			rc = t6_config_kern_tls(sc, false);
12437 			if (rc)
12438 				return (rc);
12439 		}
12440 #endif
12441 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
12442 			/* TOE is already enabled. */
12443 			return (0);
12444 		}
12445 
12446 		/*
12447 		 * We need the port's queues around so that we're able to send
12448 		 * and receive CPLs to/from the TOE even if the ifnet for this
12449 		 * port has never been UP'd administratively.
12450 		 */
12451 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
12452 			return (rc);
12453 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
12454 		    ((rc = vi_init(&pi->vi[0])) != 0))
12455 			return (rc);
12456 
12457 		if (isset(&sc->offload_map, pi->port_id)) {
12458 			/* TOE is enabled on another VI of this port. */
12459 			MPASS(pi->uld_vis > 0);
12460 			pi->uld_vis++;
12461 			return (0);
12462 		}
12463 
12464 		if (!uld_active(sc, ULD_TOM)) {
12465 			rc = t4_activate_uld(sc, ULD_TOM);
12466 			if (rc == EAGAIN) {
12467 				log(LOG_WARNING,
12468 				    "You must kldload t4_tom.ko before trying "
12469 				    "to enable TOE on a cxgbe interface.\n");
12470 			}
12471 			if (rc != 0)
12472 				return (rc);
12473 			KASSERT(sc->tom_softc != NULL,
12474 			    ("%s: TOM activated but softc NULL", __func__));
12475 			KASSERT(uld_active(sc, ULD_TOM),
12476 			    ("%s: TOM activated but flag not set", __func__));
12477 		}
12478 
12479 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
12480 		if (!uld_active(sc, ULD_IWARP))
12481 			(void) t4_activate_uld(sc, ULD_IWARP);
12482 		if (!uld_active(sc, ULD_ISCSI))
12483 			(void) t4_activate_uld(sc, ULD_ISCSI);
12484 
12485 		if (pi->uld_vis++ == 0)
12486 			setbit(&sc->offload_map, pi->port_id);
12487 	} else {
12488 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) == 0) {
12489 			/* TOE is already disabled. */
12490 			return (0);
12491 		}
12492 		MPASS(isset(&sc->offload_map, pi->port_id));
12493 		MPASS(pi->uld_vis > 0);
12494 		if (--pi->uld_vis == 0)
12495 			clrbit(&sc->offload_map, pi->port_id);
12496 	}
12497 
12498 	return (0);
12499 }
12500 
12501 /*
12502  * Add an upper layer driver to the global list.
12503  */
12504 int
t4_register_uld(struct uld_info * ui,int id)12505 t4_register_uld(struct uld_info *ui, int id)
12506 {
12507 	int rc;
12508 
12509 	if (id < 0 || id > ULD_MAX)
12510 		return (EINVAL);
12511 	sx_xlock(&t4_uld_list_lock);
12512 	if (t4_uld_list[id] != NULL)
12513 		rc = EEXIST;
12514 	else {
12515 		t4_uld_list[id] = ui;
12516 		rc = 0;
12517 	}
12518 	sx_xunlock(&t4_uld_list_lock);
12519 	return (rc);
12520 }
12521 
12522 int
t4_unregister_uld(struct uld_info * ui,int id)12523 t4_unregister_uld(struct uld_info *ui, int id)
12524 {
12525 
12526 	if (id < 0 || id > ULD_MAX)
12527 		return (EINVAL);
12528 	sx_xlock(&t4_uld_list_lock);
12529 	MPASS(t4_uld_list[id] == ui);
12530 	t4_uld_list[id] = NULL;
12531 	sx_xunlock(&t4_uld_list_lock);
12532 	return (0);
12533 }
12534 
12535 int
t4_activate_uld(struct adapter * sc,int id)12536 t4_activate_uld(struct adapter *sc, int id)
12537 {
12538 	int rc;
12539 
12540 	ASSERT_SYNCHRONIZED_OP(sc);
12541 
12542 	if (id < 0 || id > ULD_MAX)
12543 		return (EINVAL);
12544 
12545 	/* Adapter needs to be initialized before any ULD can be activated. */
12546 	if (!(sc->flags & FULL_INIT_DONE)) {
12547 		rc = adapter_init(sc);
12548 		if (rc != 0)
12549 			return (rc);
12550 	}
12551 
12552 	sx_slock(&t4_uld_list_lock);
12553 	if (t4_uld_list[id] == NULL)
12554 		rc = EAGAIN;	/* load the KLD with this ULD and try again. */
12555 	else {
12556 		rc = t4_uld_list[id]->uld_activate(sc);
12557 		if (rc == 0)
12558 			setbit(&sc->active_ulds, id);
12559 	}
12560 	sx_sunlock(&t4_uld_list_lock);
12561 
12562 	return (rc);
12563 }
12564 
12565 int
t4_deactivate_uld(struct adapter * sc,int id)12566 t4_deactivate_uld(struct adapter *sc, int id)
12567 {
12568 	int rc;
12569 
12570 	ASSERT_SYNCHRONIZED_OP(sc);
12571 
12572 	if (id < 0 || id > ULD_MAX)
12573 		return (EINVAL);
12574 
12575 	sx_slock(&t4_uld_list_lock);
12576 	if (t4_uld_list[id] == NULL)
12577 		rc = ENXIO;
12578 	else {
12579 		rc = t4_uld_list[id]->uld_deactivate(sc);
12580 		if (rc == 0)
12581 			clrbit(&sc->active_ulds, id);
12582 	}
12583 	sx_sunlock(&t4_uld_list_lock);
12584 
12585 	return (rc);
12586 }
12587 
12588 static int
deactivate_all_uld(struct adapter * sc)12589 deactivate_all_uld(struct adapter *sc)
12590 {
12591 	int i, rc;
12592 
12593 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
12594 	if (rc != 0)
12595 		return (ENXIO);
12596 	sx_slock(&t4_uld_list_lock);
12597 	for (i = 0; i <= ULD_MAX; i++) {
12598 		if (t4_uld_list[i] == NULL || !uld_active(sc, i))
12599 			continue;
12600 		rc = t4_uld_list[i]->uld_deactivate(sc);
12601 		if (rc != 0)
12602 			break;
12603 		clrbit(&sc->active_ulds, i);
12604 	}
12605 	sx_sunlock(&t4_uld_list_lock);
12606 	end_synchronized_op(sc, 0);
12607 
12608 	return (rc);
12609 }
12610 
12611 static void
stop_all_uld(struct adapter * sc)12612 stop_all_uld(struct adapter *sc)
12613 {
12614 	int i;
12615 
12616 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
12617 		return;
12618 	sx_slock(&t4_uld_list_lock);
12619 	for (i = 0; i <= ULD_MAX; i++) {
12620 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12621 		    t4_uld_list[i]->uld_stop == NULL)
12622 			continue;
12623 		(void) t4_uld_list[i]->uld_stop(sc);
12624 	}
12625 	sx_sunlock(&t4_uld_list_lock);
12626 	end_synchronized_op(sc, 0);
12627 }
12628 
12629 static void
restart_all_uld(struct adapter * sc)12630 restart_all_uld(struct adapter *sc)
12631 {
12632 	int i;
12633 
12634 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
12635 		return;
12636 	sx_slock(&t4_uld_list_lock);
12637 	for (i = 0; i <= ULD_MAX; i++) {
12638 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12639 		    t4_uld_list[i]->uld_restart == NULL)
12640 			continue;
12641 		(void) t4_uld_list[i]->uld_restart(sc);
12642 	}
12643 	sx_sunlock(&t4_uld_list_lock);
12644 	end_synchronized_op(sc, 0);
12645 }
12646 
12647 int
uld_active(struct adapter * sc,int id)12648 uld_active(struct adapter *sc, int id)
12649 {
12650 
12651 	MPASS(id >= 0 && id <= ULD_MAX);
12652 
12653 	return (isset(&sc->active_ulds, 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 (!is_t6(sc))
12666 		return (0);
12667 	if (hw_off_limits(sc))
12668 		return (ENXIO);
12669 
12670 	if (enable) {
12671 		if (sc->flags & KERN_TLS_ON)
12672 			return (0);	/* already on */
12673 		if (sc->offload_map != 0) {
12674 			CH_WARN(sc,
12675 			    "Disable TOE on all interfaces associated with "
12676 			    "this adapter before trying to enable NIC TLS.\n");
12677 			return (EAGAIN);
12678 		}
12679 		return (t6_config_kern_tls(sc, true));
12680 	} else {
12681 		/*
12682 		 * Nothing to do for disable.  If TOE is enabled sometime later
12683 		 * then toe_capability will reconfigure the hardware.
12684 		 */
12685 		return (0);
12686 	}
12687 }
12688 #endif
12689 
12690 /*
12691  * t  = ptr to tunable.
12692  * nc = number of CPUs.
12693  * c  = compiled in default for that tunable.
12694  */
12695 static void
calculate_nqueues(int * t,int nc,const int c)12696 calculate_nqueues(int *t, int nc, const int c)
12697 {
12698 	int nq;
12699 
12700 	if (*t > 0)
12701 		return;
12702 	nq = *t < 0 ? -*t : c;
12703 	*t = min(nc, nq);
12704 }
12705 
12706 /*
12707  * Come up with reasonable defaults for some of the tunables, provided they're
12708  * not set by the user (in which case we'll use the values as is).
12709  */
12710 static void
tweak_tunables(void)12711 tweak_tunables(void)
12712 {
12713 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
12714 
12715 	if (t4_ntxq < 1) {
12716 #ifdef RSS
12717 		t4_ntxq = rss_getnumbuckets();
12718 #else
12719 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
12720 #endif
12721 	}
12722 
12723 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
12724 
12725 	if (t4_nrxq < 1) {
12726 #ifdef RSS
12727 		t4_nrxq = rss_getnumbuckets();
12728 #else
12729 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
12730 #endif
12731 	}
12732 
12733 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
12734 
12735 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12736 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
12737 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
12738 #endif
12739 #ifdef TCP_OFFLOAD
12740 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
12741 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
12742 #endif
12743 
12744 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
12745 	if (t4_toecaps_allowed == -1)
12746 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
12747 #else
12748 	if (t4_toecaps_allowed == -1)
12749 		t4_toecaps_allowed = 0;
12750 #endif
12751 
12752 #ifdef TCP_OFFLOAD
12753 	if (t4_rdmacaps_allowed == -1) {
12754 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
12755 		    FW_CAPS_CONFIG_RDMA_RDMAC;
12756 	}
12757 
12758 	if (t4_iscsicaps_allowed == -1) {
12759 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
12760 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
12761 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
12762 	}
12763 
12764 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
12765 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
12766 
12767 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
12768 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
12769 #else
12770 	if (t4_rdmacaps_allowed == -1)
12771 		t4_rdmacaps_allowed = 0;
12772 
12773 	if (t4_iscsicaps_allowed == -1)
12774 		t4_iscsicaps_allowed = 0;
12775 #endif
12776 
12777 #ifdef DEV_NETMAP
12778 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
12779 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
12780 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
12781 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
12782 #endif
12783 
12784 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
12785 		t4_tmr_idx = TMR_IDX;
12786 
12787 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
12788 		t4_pktc_idx = PKTC_IDX;
12789 
12790 	if (t4_qsize_txq < 128)
12791 		t4_qsize_txq = 128;
12792 
12793 	if (t4_qsize_rxq < 128)
12794 		t4_qsize_rxq = 128;
12795 	while (t4_qsize_rxq & 7)
12796 		t4_qsize_rxq++;
12797 
12798 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
12799 
12800 	/*
12801 	 * Number of VIs to create per-port.  The first VI is the "main" regular
12802 	 * VI for the port.  The rest are additional virtual interfaces on the
12803 	 * same physical port.  Note that the main VI does not have native
12804 	 * netmap support but the extra VIs do.
12805 	 *
12806 	 * Limit the number of VIs per port to the number of available
12807 	 * MAC addresses per port.
12808 	 */
12809 	if (t4_num_vis < 1)
12810 		t4_num_vis = 1;
12811 	if (t4_num_vis > nitems(vi_mac_funcs)) {
12812 		t4_num_vis = nitems(vi_mac_funcs);
12813 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
12814 	}
12815 
12816 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
12817 		pcie_relaxed_ordering = 1;
12818 #if defined(__i386__) || defined(__amd64__)
12819 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
12820 			pcie_relaxed_ordering = 0;
12821 #endif
12822 	}
12823 }
12824 
12825 #ifdef DDB
12826 static void
t4_dump_mem(struct adapter * sc,u_int addr,u_int len)12827 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
12828 {
12829 	uint32_t base, j, off, pf, reg, save, win_pos;
12830 
12831 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
12832 	save = t4_read_reg(sc, reg);
12833 	base = sc->memwin[2].mw_base;
12834 
12835 	if (is_t4(sc)) {
12836 		pf = 0;
12837 		win_pos = addr & ~0xf;	/* start must be 16B aligned */
12838 	} else {
12839 		pf = V_PFNUM(sc->pf);
12840 		win_pos = addr & ~0x7f;	/* start must be 128B aligned */
12841 	}
12842 	off = addr - win_pos;
12843 	t4_write_reg(sc, reg, win_pos | pf);
12844 	t4_read_reg(sc, reg);
12845 
12846 	while (len > 0 && !db_pager_quit) {
12847 		uint32_t buf[8];
12848 		for (j = 0; j < 8; j++, off += 4)
12849 			buf[j] = htonl(t4_read_reg(sc, base + off));
12850 
12851 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
12852 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12853 		    buf[7]);
12854 		if (len <= sizeof(buf))
12855 			len = 0;
12856 		else
12857 			len -= sizeof(buf);
12858 	}
12859 
12860 	t4_write_reg(sc, reg, save);
12861 	t4_read_reg(sc, reg);
12862 }
12863 
12864 static void
t4_dump_tcb(struct adapter * sc,int tid)12865 t4_dump_tcb(struct adapter *sc, int tid)
12866 {
12867 	uint32_t tcb_addr;
12868 
12869 	/* Dump TCB for the tid */
12870 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
12871 	tcb_addr += tid * TCB_SIZE;
12872 	t4_dump_mem(sc, tcb_addr, TCB_SIZE);
12873 }
12874 
12875 static void
t4_dump_devlog(struct adapter * sc)12876 t4_dump_devlog(struct adapter *sc)
12877 {
12878 	struct devlog_params *dparams = &sc->params.devlog;
12879 	struct fw_devlog_e e;
12880 	int i, first, j, m, nentries, rc;
12881 	uint64_t ftstamp = UINT64_MAX;
12882 
12883 	if (dparams->start == 0) {
12884 		db_printf("devlog params not valid\n");
12885 		return;
12886 	}
12887 
12888 	nentries = dparams->size / sizeof(struct fw_devlog_e);
12889 	m = fwmtype_to_hwmtype(dparams->memtype);
12890 
12891 	/* Find the first entry. */
12892 	first = -1;
12893 	for (i = 0; i < nentries && !db_pager_quit; i++) {
12894 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12895 		    sizeof(e), (void *)&e);
12896 		if (rc != 0)
12897 			break;
12898 
12899 		if (e.timestamp == 0)
12900 			break;
12901 
12902 		e.timestamp = be64toh(e.timestamp);
12903 		if (e.timestamp < ftstamp) {
12904 			ftstamp = e.timestamp;
12905 			first = i;
12906 		}
12907 	}
12908 
12909 	if (first == -1)
12910 		return;
12911 
12912 	i = first;
12913 	do {
12914 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12915 		    sizeof(e), (void *)&e);
12916 		if (rc != 0)
12917 			return;
12918 
12919 		if (e.timestamp == 0)
12920 			return;
12921 
12922 		e.timestamp = be64toh(e.timestamp);
12923 		e.seqno = be32toh(e.seqno);
12924 		for (j = 0; j < 8; j++)
12925 			e.params[j] = be32toh(e.params[j]);
12926 
12927 		db_printf("%10d  %15ju  %8s  %8s  ",
12928 		    e.seqno, e.timestamp,
12929 		    (e.level < nitems(devlog_level_strings) ?
12930 			devlog_level_strings[e.level] : "UNKNOWN"),
12931 		    (e.facility < nitems(devlog_facility_strings) ?
12932 			devlog_facility_strings[e.facility] : "UNKNOWN"));
12933 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
12934 		    e.params[3], e.params[4], e.params[5], e.params[6],
12935 		    e.params[7]);
12936 
12937 		if (++i == nentries)
12938 			i = 0;
12939 	} while (i != first && !db_pager_quit);
12940 }
12941 
12942 static DB_DEFINE_TABLE(show, t4, show_t4);
12943 
DB_TABLE_COMMAND_FLAGS(show_t4,devlog,db_show_devlog,CS_OWN)12944 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
12945 {
12946 	device_t dev;
12947 	int t;
12948 	bool valid;
12949 
12950 	valid = false;
12951 	t = db_read_token();
12952 	if (t == tIDENT) {
12953 		dev = device_lookup_by_name(db_tok_string);
12954 		valid = true;
12955 	}
12956 	db_skip_to_eol();
12957 	if (!valid) {
12958 		db_printf("usage: show t4 devlog <nexus>\n");
12959 		return;
12960 	}
12961 
12962 	if (dev == NULL) {
12963 		db_printf("device not found\n");
12964 		return;
12965 	}
12966 
12967 	t4_dump_devlog(device_get_softc(dev));
12968 }
12969 
DB_TABLE_COMMAND_FLAGS(show_t4,tcb,db_show_t4tcb,CS_OWN)12970 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
12971 {
12972 	device_t dev;
12973 	int radix, tid, t;
12974 	bool valid;
12975 
12976 	valid = false;
12977 	radix = db_radix;
12978 	db_radix = 10;
12979 	t = db_read_token();
12980 	if (t == tIDENT) {
12981 		dev = device_lookup_by_name(db_tok_string);
12982 		t = db_read_token();
12983 		if (t == tNUMBER) {
12984 			tid = db_tok_number;
12985 			valid = true;
12986 		}
12987 	}
12988 	db_radix = radix;
12989 	db_skip_to_eol();
12990 	if (!valid) {
12991 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
12992 		return;
12993 	}
12994 
12995 	if (dev == NULL) {
12996 		db_printf("device not found\n");
12997 		return;
12998 	}
12999 	if (tid < 0) {
13000 		db_printf("invalid tid\n");
13001 		return;
13002 	}
13003 
13004 	t4_dump_tcb(device_get_softc(dev), tid);
13005 }
13006 
DB_TABLE_COMMAND_FLAGS(show_t4,memdump,db_show_memdump,CS_OWN)13007 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
13008 {
13009 	device_t dev;
13010 	int radix, t;
13011 	bool valid;
13012 
13013 	valid = false;
13014 	radix = db_radix;
13015 	db_radix = 10;
13016 	t = db_read_token();
13017 	if (t == tIDENT) {
13018 		dev = device_lookup_by_name(db_tok_string);
13019 		t = db_read_token();
13020 		if (t == tNUMBER) {
13021 			addr = db_tok_number;
13022 			t = db_read_token();
13023 			if (t == tNUMBER) {
13024 				count = db_tok_number;
13025 				valid = true;
13026 			}
13027 		}
13028 	}
13029 	db_radix = radix;
13030 	db_skip_to_eol();
13031 	if (!valid) {
13032 		db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
13033 		return;
13034 	}
13035 
13036 	if (dev == NULL) {
13037 		db_printf("device not found\n");
13038 		return;
13039 	}
13040 	if (addr < 0) {
13041 		db_printf("invalid address\n");
13042 		return;
13043 	}
13044 	if (count <= 0) {
13045 		db_printf("invalid length\n");
13046 		return;
13047 	}
13048 
13049 	t4_dump_mem(device_get_softc(dev), addr, count);
13050 }
13051 #endif
13052 
13053 static eventhandler_tag vxlan_start_evtag;
13054 static eventhandler_tag vxlan_stop_evtag;
13055 
13056 struct vxlan_evargs {
13057 	if_t ifp;
13058 	uint16_t port;
13059 };
13060 
13061 static void
enable_vxlan_rx(struct adapter * sc)13062 enable_vxlan_rx(struct adapter *sc)
13063 {
13064 	int i, rc;
13065 	struct port_info *pi;
13066 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
13067 
13068 	ASSERT_SYNCHRONIZED_OP(sc);
13069 
13070 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
13071 	    F_VXLAN_EN);
13072 	for_each_port(sc, i) {
13073 		pi = sc->port[i];
13074 		if (pi->vxlan_tcam_entry == true)
13075 			continue;
13076 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
13077 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
13078 		    true);
13079 		if (rc < 0) {
13080 			rc = -rc;
13081 			CH_ERR(&pi->vi[0],
13082 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
13083 		} else {
13084 			MPASS(rc == sc->rawf_base + pi->port_id);
13085 			pi->vxlan_tcam_entry = true;
13086 		}
13087 	}
13088 }
13089 
13090 static void
t4_vxlan_start(struct adapter * sc,void * arg)13091 t4_vxlan_start(struct adapter *sc, void *arg)
13092 {
13093 	struct vxlan_evargs *v = arg;
13094 
13095 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13096 		return;
13097 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
13098 		return;
13099 
13100 	if (sc->vxlan_refcount == 0) {
13101 		sc->vxlan_port = v->port;
13102 		sc->vxlan_refcount = 1;
13103 		if (!hw_off_limits(sc))
13104 			enable_vxlan_rx(sc);
13105 	} else if (sc->vxlan_port == v->port) {
13106 		sc->vxlan_refcount++;
13107 	} else {
13108 		CH_ERR(sc, "VXLAN already configured on port  %d; "
13109 		    "ignoring attempt to configure it on port %d\n",
13110 		    sc->vxlan_port, v->port);
13111 	}
13112 	end_synchronized_op(sc, 0);
13113 }
13114 
13115 static void
t4_vxlan_stop(struct adapter * sc,void * arg)13116 t4_vxlan_stop(struct adapter *sc, void *arg)
13117 {
13118 	struct vxlan_evargs *v = arg;
13119 
13120 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13121 		return;
13122 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
13123 		return;
13124 
13125 	/*
13126 	 * VXLANs may have been configured before the driver was loaded so we
13127 	 * may see more stops than starts.  This is not handled cleanly but at
13128 	 * least we keep the refcount sane.
13129 	 */
13130 	if (sc->vxlan_port != v->port)
13131 		goto done;
13132 	if (sc->vxlan_refcount == 0) {
13133 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
13134 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
13135 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
13136 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
13137 done:
13138 	end_synchronized_op(sc, 0);
13139 }
13140 
13141 static void
t4_vxlan_start_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13142 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
13143     sa_family_t family, u_int port)
13144 {
13145 	struct vxlan_evargs v;
13146 
13147 	MPASS(family == AF_INET || family == AF_INET6);
13148 	v.ifp = ifp;
13149 	v.port = port;
13150 
13151 	t4_iterate(t4_vxlan_start, &v);
13152 }
13153 
13154 static void
t4_vxlan_stop_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13155 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
13156     u_int port)
13157 {
13158 	struct vxlan_evargs v;
13159 
13160 	MPASS(family == AF_INET || family == AF_INET6);
13161 	v.ifp = ifp;
13162 	v.port = port;
13163 
13164 	t4_iterate(t4_vxlan_stop, &v);
13165 }
13166 
13167 
13168 static struct sx mlu;	/* mod load unload */
13169 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
13170 
13171 static int
mod_event(module_t mod,int cmd,void * arg)13172 mod_event(module_t mod, int cmd, void *arg)
13173 {
13174 	int rc = 0;
13175 	static int loaded = 0;
13176 
13177 	switch (cmd) {
13178 	case MOD_LOAD:
13179 		sx_xlock(&mlu);
13180 		if (loaded++ == 0) {
13181 			t4_sge_modload();
13182 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13183 			    t4_filter_rpl, CPL_COOKIE_FILTER);
13184 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
13185 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
13186 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
13187 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
13188 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13189 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
13190 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
13191 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
13192 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
13193 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
13194 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
13195 			    do_smt_write_rpl);
13196 			sx_init(&t4_list_lock, "T4/T5 adapters");
13197 			SLIST_INIT(&t4_list);
13198 			callout_init(&fatal_callout, 1);
13199 #ifdef TCP_OFFLOAD
13200 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
13201 #endif
13202 #ifdef INET6
13203 			t4_clip_modload();
13204 #endif
13205 #ifdef KERN_TLS
13206 			t6_ktls_modload();
13207 #endif
13208 			t4_tracer_modload();
13209 			tweak_tunables();
13210 			vxlan_start_evtag =
13211 			    EVENTHANDLER_REGISTER(vxlan_start,
13212 				t4_vxlan_start_handler, NULL,
13213 				EVENTHANDLER_PRI_ANY);
13214 			vxlan_stop_evtag =
13215 			    EVENTHANDLER_REGISTER(vxlan_stop,
13216 				t4_vxlan_stop_handler, NULL,
13217 				EVENTHANDLER_PRI_ANY);
13218 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
13219 			    taskqueue_thread_enqueue, &reset_tq);
13220 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
13221 			    "t4_rst_thr");
13222 		}
13223 		sx_xunlock(&mlu);
13224 		break;
13225 
13226 	case MOD_UNLOAD:
13227 		sx_xlock(&mlu);
13228 		if (--loaded == 0) {
13229 #ifdef TCP_OFFLOAD
13230 			int i;
13231 #endif
13232 			int tries;
13233 
13234 			taskqueue_free(reset_tq);
13235 
13236 			tries = 0;
13237 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
13238 				uprintf("%ju clusters with custom free routine "
13239 				    "still is use.\n", t4_sge_extfree_refs());
13240 				pause("t4unload", 2 * hz);
13241 			}
13242 
13243 			sx_slock(&t4_list_lock);
13244 			if (!SLIST_EMPTY(&t4_list)) {
13245 				rc = EBUSY;
13246 				sx_sunlock(&t4_list_lock);
13247 				goto done_unload;
13248 			}
13249 #ifdef TCP_OFFLOAD
13250 			sx_slock(&t4_uld_list_lock);
13251 			for (i = 0; i <= ULD_MAX; i++) {
13252 				if (t4_uld_list[i] != NULL) {
13253 					rc = EBUSY;
13254 					sx_sunlock(&t4_uld_list_lock);
13255 					sx_sunlock(&t4_list_lock);
13256 					goto done_unload;
13257 				}
13258 			}
13259 			sx_sunlock(&t4_uld_list_lock);
13260 #endif
13261 			sx_sunlock(&t4_list_lock);
13262 
13263 			if (t4_sge_extfree_refs() == 0) {
13264 				EVENTHANDLER_DEREGISTER(vxlan_start,
13265 				    vxlan_start_evtag);
13266 				EVENTHANDLER_DEREGISTER(vxlan_stop,
13267 				    vxlan_stop_evtag);
13268 				t4_tracer_modunload();
13269 #ifdef KERN_TLS
13270 				t6_ktls_modunload();
13271 #endif
13272 #ifdef INET6
13273 				t4_clip_modunload();
13274 #endif
13275 #ifdef TCP_OFFLOAD
13276 				sx_destroy(&t4_uld_list_lock);
13277 #endif
13278 				sx_destroy(&t4_list_lock);
13279 				t4_sge_modunload();
13280 				loaded = 0;
13281 			} else {
13282 				rc = EBUSY;
13283 				loaded++;	/* undo earlier decrement */
13284 			}
13285 		}
13286 done_unload:
13287 		sx_xunlock(&mlu);
13288 		break;
13289 	}
13290 
13291 	return (rc);
13292 }
13293 
13294 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
13295 MODULE_VERSION(t4nex, 1);
13296 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
13297 #ifdef DEV_NETMAP
13298 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
13299 #endif /* DEV_NETMAP */
13300 
13301 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
13302 MODULE_VERSION(t5nex, 1);
13303 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
13304 #ifdef DEV_NETMAP
13305 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
13306 #endif /* DEV_NETMAP */
13307 
13308 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
13309 MODULE_VERSION(t6nex, 1);
13310 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
13311 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
13312 #ifdef DEV_NETMAP
13313 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
13314 #endif /* DEV_NETMAP */
13315 
13316 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
13317 MODULE_VERSION(cxgbe, 1);
13318 
13319 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
13320 MODULE_VERSION(cxl, 1);
13321 
13322 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
13323 MODULE_VERSION(cc, 1);
13324 
13325 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
13326 MODULE_VERSION(vcxgbe, 1);
13327 
13328 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
13329 MODULE_VERSION(vcxl, 1);
13330 
13331 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
13332 MODULE_VERSION(vcc, 1);
13333