xref: /freebsd/sys/dev/cxgbe/t4_main.c (revision c22b297062e1440676973a8aa89cbad1571e22f9)
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 <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <sys/firmware.h>
51 #include <sys/sbuf.h>
52 #include <sys/smp.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <net/ethernet.h>
57 #include <net/if.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/if_vlan_var.h>
61 #ifdef RSS
62 #include <net/rss_config.h>
63 #endif
64 #include <netinet/in.h>
65 #include <netinet/ip.h>
66 #ifdef KERN_TLS
67 #include <netinet/tcp_seq.h>
68 #endif
69 #if defined(__i386__) || defined(__amd64__)
70 #include <machine/md_var.h>
71 #include <machine/cputypes.h>
72 #include <vm/vm.h>
73 #include <vm/pmap.h>
74 #endif
75 #ifdef DDB
76 #include <ddb/ddb.h>
77 #include <ddb/db_lex.h>
78 #endif
79 
80 #include "common/common.h"
81 #include "common/t4_msg.h"
82 #include "common/t4_regs.h"
83 #include "common/t4_regs_values.h"
84 #include "cudbg/cudbg.h"
85 #include "t4_clip.h"
86 #include "t4_ioctl.h"
87 #include "t4_l2t.h"
88 #include "t4_mp_ring.h"
89 #include "t4_if.h"
90 #include "t4_smt.h"
91 
92 /* T4 bus driver interface */
93 static int t4_probe(device_t);
94 static int t4_attach(device_t);
95 static int t4_detach(device_t);
96 static int t4_child_location(device_t, device_t, struct sbuf *);
97 static int t4_ready(device_t);
98 static int t4_read_port_device(device_t, int, device_t *);
99 static int t4_suspend(device_t);
100 static int t4_resume(device_t);
101 static int t4_reset_prepare(device_t, device_t);
102 static int t4_reset_post(device_t, device_t);
103 static device_method_t t4_methods[] = {
104 	DEVMETHOD(device_probe,		t4_probe),
105 	DEVMETHOD(device_attach,	t4_attach),
106 	DEVMETHOD(device_detach,	t4_detach),
107 	DEVMETHOD(device_suspend,	t4_suspend),
108 	DEVMETHOD(device_resume,	t4_resume),
109 
110 	DEVMETHOD(bus_child_location,	t4_child_location),
111 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
112 	DEVMETHOD(bus_reset_post,	t4_reset_post),
113 
114 	DEVMETHOD(t4_is_main_ready,	t4_ready),
115 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
116 
117 	DEVMETHOD_END
118 };
119 static driver_t t4_driver = {
120 	"t4nex",
121 	t4_methods,
122 	sizeof(struct adapter)
123 };
124 
125 
126 /* T4 port (cxgbe) interface */
127 static int cxgbe_probe(device_t);
128 static int cxgbe_attach(device_t);
129 static int cxgbe_detach(device_t);
130 device_method_t cxgbe_methods[] = {
131 	DEVMETHOD(device_probe,		cxgbe_probe),
132 	DEVMETHOD(device_attach,	cxgbe_attach),
133 	DEVMETHOD(device_detach,	cxgbe_detach),
134 	{ 0, 0 }
135 };
136 static driver_t cxgbe_driver = {
137 	"cxgbe",
138 	cxgbe_methods,
139 	sizeof(struct port_info)
140 };
141 
142 /* T4 VI (vcxgbe) interface */
143 static int vcxgbe_probe(device_t);
144 static int vcxgbe_attach(device_t);
145 static int vcxgbe_detach(device_t);
146 static device_method_t vcxgbe_methods[] = {
147 	DEVMETHOD(device_probe,		vcxgbe_probe),
148 	DEVMETHOD(device_attach,	vcxgbe_attach),
149 	DEVMETHOD(device_detach,	vcxgbe_detach),
150 	{ 0, 0 }
151 };
152 static driver_t vcxgbe_driver = {
153 	"vcxgbe",
154 	vcxgbe_methods,
155 	sizeof(struct vi_info)
156 };
157 
158 static d_ioctl_t t4_ioctl;
159 
160 static struct cdevsw t4_cdevsw = {
161        .d_version = D_VERSION,
162        .d_ioctl = t4_ioctl,
163        .d_name = "t4nex",
164 };
165 
166 /* T5 bus driver interface */
167 static int t5_probe(device_t);
168 static device_method_t t5_methods[] = {
169 	DEVMETHOD(device_probe,		t5_probe),
170 	DEVMETHOD(device_attach,	t4_attach),
171 	DEVMETHOD(device_detach,	t4_detach),
172 	DEVMETHOD(device_suspend,	t4_suspend),
173 	DEVMETHOD(device_resume,	t4_resume),
174 
175 	DEVMETHOD(bus_child_location,	t4_child_location),
176 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
177 	DEVMETHOD(bus_reset_post,	t4_reset_post),
178 
179 	DEVMETHOD(t4_is_main_ready,	t4_ready),
180 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
181 
182 	DEVMETHOD_END
183 };
184 static driver_t t5_driver = {
185 	"t5nex",
186 	t5_methods,
187 	sizeof(struct adapter)
188 };
189 
190 
191 /* T5 port (cxl) interface */
192 static driver_t cxl_driver = {
193 	"cxl",
194 	cxgbe_methods,
195 	sizeof(struct port_info)
196 };
197 
198 /* T5 VI (vcxl) interface */
199 static driver_t vcxl_driver = {
200 	"vcxl",
201 	vcxgbe_methods,
202 	sizeof(struct vi_info)
203 };
204 
205 /* T6 bus driver interface */
206 static int t6_probe(device_t);
207 static device_method_t t6_methods[] = {
208 	DEVMETHOD(device_probe,		t6_probe),
209 	DEVMETHOD(device_attach,	t4_attach),
210 	DEVMETHOD(device_detach,	t4_detach),
211 	DEVMETHOD(device_suspend,	t4_suspend),
212 	DEVMETHOD(device_resume,	t4_resume),
213 
214 	DEVMETHOD(bus_child_location,	t4_child_location),
215 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
216 	DEVMETHOD(bus_reset_post,	t4_reset_post),
217 
218 	DEVMETHOD(t4_is_main_ready,	t4_ready),
219 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
220 
221 	DEVMETHOD_END
222 };
223 static driver_t t6_driver = {
224 	"t6nex",
225 	t6_methods,
226 	sizeof(struct adapter)
227 };
228 
229 
230 /* T6 port (cc) interface */
231 static driver_t cc_driver = {
232 	"cc",
233 	cxgbe_methods,
234 	sizeof(struct port_info)
235 };
236 
237 /* T6 VI (vcc) interface */
238 static driver_t vcc_driver = {
239 	"vcc",
240 	vcxgbe_methods,
241 	sizeof(struct vi_info)
242 };
243 
244 /* ifnet interface */
245 static void cxgbe_init(void *);
246 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
247 static int cxgbe_transmit(if_t, struct mbuf *);
248 static void cxgbe_qflush(if_t);
249 #if defined(KERN_TLS) || defined(RATELIMIT)
250 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
251     struct m_snd_tag **);
252 #endif
253 
254 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
255 
256 /*
257  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
258  * then ADAPTER_LOCK, then t4_uld_list_lock.
259  */
260 static struct sx t4_list_lock;
261 SLIST_HEAD(, adapter) t4_list;
262 #ifdef TCP_OFFLOAD
263 static struct sx t4_uld_list_lock;
264 struct uld_info *t4_uld_list[ULD_MAX + 1];
265 #endif
266 
267 /*
268  * Tunables.  See tweak_tunables() too.
269  *
270  * Each tunable is set to a default value here if it's known at compile-time.
271  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
272  * provide a reasonable default (upto n) when the driver is loaded.
273  *
274  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
275  * T5 are under hw.cxl.
276  */
277 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
278     "cxgbe(4) parameters");
279 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
280     "cxgbe(4) T5+ parameters");
281 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
282     "cxgbe(4) TOE parameters");
283 
284 /*
285  * Number of queues for tx and rx, NIC and offload.
286  */
287 #define NTXQ 16
288 int t4_ntxq = -NTXQ;
289 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
290     "Number of TX queues per port");
291 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
292 
293 #define NRXQ 8
294 int t4_nrxq = -NRXQ;
295 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
296     "Number of RX queues per port");
297 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
298 
299 #define NTXQ_VI 1
300 static int t4_ntxq_vi = -NTXQ_VI;
301 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
302     "Number of TX queues per VI");
303 
304 #define NRXQ_VI 1
305 static int t4_nrxq_vi = -NRXQ_VI;
306 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
307     "Number of RX queues per VI");
308 
309 static int t4_rsrv_noflowq = 0;
310 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
311     0, "Reserve TX queue 0 of each VI for non-flowid packets");
312 
313 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
314 #define NOFLDTXQ 8
315 static int t4_nofldtxq = -NOFLDTXQ;
316 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
317     "Number of offload TX queues per port");
318 
319 #define NOFLDTXQ_VI 1
320 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
321 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
322     "Number of offload TX queues per VI");
323 #endif
324 
325 #if defined(TCP_OFFLOAD)
326 #define NOFLDRXQ 2
327 static int t4_nofldrxq = -NOFLDRXQ;
328 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
329     "Number of offload RX queues per port");
330 
331 #define 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 static 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 static 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_reset_method = 1;
637 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_method, CTLFLAG_RWTUN, &t4_reset_method,
638     0, "reset method: 0 = PL_RST, 1 = PCIe secondary bus reset, 2 = PCIe link bounce");
639 
640 static int t4_clock_gate_on_suspend = 0;
641 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
642     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
643 
644 static int t4_tx_vm_wr = 0;
645 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
646     "Use VM work requests to transmit packets.");
647 
648 /*
649  * Set to non-zero to enable the attack filter.  A packet that matches any of
650  * these conditions will get dropped on ingress:
651  * 1) IP && source address == destination address.
652  * 2) TCP/IP && source address is not a unicast address.
653  * 3) TCP/IP && destination address is not a unicast address.
654  * 4) IP && source address is loopback (127.x.y.z).
655  * 5) IP && destination address is loopback (127.x.y.z).
656  * 6) IPv6 && source address == destination address.
657  * 7) IPv6 && source address is not a unicast address.
658  * 8) IPv6 && source address is loopback (::1/128).
659  * 9) IPv6 && destination address is loopback (::1/128).
660  * 10) IPv6 && source address is unspecified (::/128).
661  * 11) IPv6 && destination address is unspecified (::/128).
662  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
663  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
664  */
665 static int t4_attack_filter = 0;
666 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
667     &t4_attack_filter, 0, "Drop suspicious traffic");
668 
669 static int t4_drop_ip_fragments = 0;
670 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
671     &t4_drop_ip_fragments, 0, "Drop IP fragments");
672 
673 static int t4_drop_pkts_with_l2_errors = 1;
674 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
675     &t4_drop_pkts_with_l2_errors, 0,
676     "Drop all frames with Layer 2 length or checksum errors");
677 
678 static int t4_drop_pkts_with_l3_errors = 0;
679 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
680     &t4_drop_pkts_with_l3_errors, 0,
681     "Drop all frames with IP version, length, or checksum errors");
682 
683 static int t4_drop_pkts_with_l4_errors = 0;
684 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
685     &t4_drop_pkts_with_l4_errors, 0,
686     "Drop all frames with Layer 4 length, checksum, or other errors");
687 
688 #ifdef TCP_OFFLOAD
689 /*
690  * TOE tunables.
691  */
692 static int t4_cop_managed_offloading = 0;
693 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
694     &t4_cop_managed_offloading, 0,
695     "COP (Connection Offload Policy) controls all TOE offload");
696 TUNABLE_INT("hw.cxgbe.cop_managed_offloading", &t4_cop_managed_offloading);
697 #endif
698 
699 #ifdef KERN_TLS
700 /*
701  * This enables KERN_TLS for all adapters if set.
702  */
703 static int t4_kern_tls = 0;
704 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
705     "Enable KERN_TLS mode for T6 adapters");
706 
707 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
708     "cxgbe(4) KERN_TLS parameters");
709 
710 static int t4_tls_inline_keys = 0;
711 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
712     &t4_tls_inline_keys, 0,
713     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
714     "in card memory.");
715 
716 static int t4_tls_combo_wrs = 0;
717 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
718     0, "Attempt to combine TCB field updates with TLS record work requests.");
719 #endif
720 
721 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
722 static int vi_mac_funcs[] = {
723 	FW_VI_FUNC_ETH,
724 	FW_VI_FUNC_OFLD,
725 	FW_VI_FUNC_IWARP,
726 	FW_VI_FUNC_OPENISCSI,
727 	FW_VI_FUNC_OPENFCOE,
728 	FW_VI_FUNC_FOISCSI,
729 	FW_VI_FUNC_FOFCOE,
730 };
731 
732 struct intrs_and_queues {
733 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
734 	uint16_t num_vis;	/* number of VIs for each port */
735 	uint16_t nirq;		/* Total # of vectors */
736 	uint16_t ntxq;		/* # of NIC txq's for each port */
737 	uint16_t nrxq;		/* # of NIC rxq's for each port */
738 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
739 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
740 	uint16_t nnmtxq;	/* # of netmap txq's */
741 	uint16_t nnmrxq;	/* # of netmap rxq's */
742 
743 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
744 	uint16_t ntxq_vi;	/* # of NIC txq's */
745 	uint16_t nrxq_vi;	/* # of NIC rxq's */
746 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
747 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
748 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
749 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
750 };
751 
752 static void setup_memwin(struct adapter *);
753 static void position_memwin(struct adapter *, int, uint32_t);
754 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
755 static int fwmtype_to_hwmtype(int);
756 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
757     uint32_t *);
758 static int fixup_devlog_params(struct adapter *);
759 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
760 static int contact_firmware(struct adapter *);
761 static int partition_resources(struct adapter *);
762 static int get_params__pre_init(struct adapter *);
763 static int set_params__pre_init(struct adapter *);
764 static int get_params__post_init(struct adapter *);
765 static int set_params__post_init(struct adapter *);
766 static void t4_set_desc(struct adapter *);
767 static bool fixed_ifmedia(struct port_info *);
768 static void build_medialist(struct port_info *);
769 static void init_link_config(struct port_info *);
770 static int fixup_link_config(struct port_info *);
771 static int apply_link_config(struct port_info *);
772 static int cxgbe_init_synchronized(struct vi_info *);
773 static int cxgbe_uninit_synchronized(struct vi_info *);
774 static int adapter_full_init(struct adapter *);
775 static void adapter_full_uninit(struct adapter *);
776 static int vi_full_init(struct vi_info *);
777 static void vi_full_uninit(struct vi_info *);
778 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
779 static void quiesce_txq(struct sge_txq *);
780 static void quiesce_wrq(struct sge_wrq *);
781 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
782 static void quiesce_vi(struct vi_info *);
783 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
784     driver_intr_t *, void *, char *);
785 static int t4_free_irq(struct adapter *, struct irq *);
786 static void t4_init_atid_table(struct adapter *);
787 static void t4_free_atid_table(struct adapter *);
788 static void stop_atid_allocator(struct adapter *);
789 static void restart_atid_allocator(struct adapter *);
790 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
791 static void vi_refresh_stats(struct vi_info *);
792 static void cxgbe_refresh_stats(struct vi_info *);
793 static void cxgbe_tick(void *);
794 static void vi_tick(void *);
795 static void cxgbe_sysctls(struct port_info *);
796 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
797 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
798 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
799 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
800 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
801 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
802 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
803 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
804 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
805 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
806 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
807 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
808 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
809 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
810 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
811 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
812 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
813 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
814 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
815 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
816 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
817 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
818 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
819 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
820 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
821 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
822 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
823 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
824 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
825 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
826 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
827 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
828 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
829 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
830 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
831 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
832 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
833 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
834 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
835 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
836 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
837 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
838 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
839 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
840 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
841 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
842 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
843 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
844 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
845 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
846 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
847 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
848 #ifdef TCP_OFFLOAD
849 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
850 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
851 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
852 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
853 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
854 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
855 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
856 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
857 #endif
858 static int get_sge_context(struct adapter *, struct t4_sge_context *);
859 static int load_fw(struct adapter *, struct t4_data *);
860 static int load_cfg(struct adapter *, struct t4_data *);
861 static int load_boot(struct adapter *, struct t4_bootrom *);
862 static int load_bootcfg(struct adapter *, struct t4_data *);
863 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
864 static void free_offload_policy(struct t4_offload_policy *);
865 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
866 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
867 static int read_i2c(struct adapter *, struct t4_i2c_data *);
868 static int clear_stats(struct adapter *, u_int);
869 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
870 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
871 static inline int stop_adapter(struct adapter *);
872 static inline void set_adapter_hwstatus(struct adapter *, const bool);
873 static int stop_lld(struct adapter *);
874 static inline int restart_adapter(struct adapter *);
875 static int restart_lld(struct adapter *);
876 #ifdef TCP_OFFLOAD
877 static int deactivate_all_uld(struct adapter *);
878 static void stop_all_uld(struct adapter *);
879 static void restart_all_uld(struct adapter *);
880 #endif
881 #ifdef KERN_TLS
882 static int ktls_capability(struct adapter *, bool);
883 #endif
884 static int mod_event(module_t, int, void *);
885 static int notify_siblings(device_t, int);
886 static uint64_t vi_get_counter(if_t, ift_counter);
887 static uint64_t cxgbe_get_counter(if_t, ift_counter);
888 static void enable_vxlan_rx(struct adapter *);
889 static void reset_adapter_task(void *, int);
890 static void fatal_error_task(void *, int);
891 static void dump_devlog(struct adapter *);
892 static void dump_cim_regs(struct adapter *);
893 static void dump_cimla(struct adapter *);
894 
895 struct {
896 	uint16_t device;
897 	char *desc;
898 } t4_pciids[] = {
899 	{0xa000, "Chelsio Terminator 4 FPGA"},
900 	{0x4400, "Chelsio T440-dbg"},
901 	{0x4401, "Chelsio T420-CR"},
902 	{0x4402, "Chelsio T422-CR"},
903 	{0x4403, "Chelsio T440-CR"},
904 	{0x4404, "Chelsio T420-BCH"},
905 	{0x4405, "Chelsio T440-BCH"},
906 	{0x4406, "Chelsio T440-CH"},
907 	{0x4407, "Chelsio T420-SO"},
908 	{0x4408, "Chelsio T420-CX"},
909 	{0x4409, "Chelsio T420-BT"},
910 	{0x440a, "Chelsio T404-BT"},
911 	{0x440e, "Chelsio T440-LP-CR"},
912 }, t5_pciids[] = {
913 	{0xb000, "Chelsio Terminator 5 FPGA"},
914 	{0x5400, "Chelsio T580-dbg"},
915 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
916 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
917 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
918 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
919 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
920 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
921 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
922 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
923 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
924 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
925 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
926 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
927 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
928 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
929 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
930 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
931 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
932 
933 	/* Custom */
934 	{0x5483, "Custom T540-CR"},
935 	{0x5484, "Custom T540-BT"},
936 }, t6_pciids[] = {
937 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
938 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
939 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
940 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
941 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
942 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
943 	{0x6405, "Chelsio T6225-SO-OCP3"},	/* 2 x 10/25G, nomem */
944 	{0x6406, "Chelsio T6225-OCP3"},		/* 2 x 10/25G */
945 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
946 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
947 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
948 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
949 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
950 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
951 	{0x6414, "Chelsio T62100-SO-OCP3"},	/* 2 x 40/50/100G, nomem */
952 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
953 
954 	/* Custom */
955 	{0x6480, "Custom T6225-CR"},
956 	{0x6481, "Custom T62100-CR"},
957 	{0x6482, "Custom T6225-CR"},
958 	{0x6483, "Custom T62100-CR"},
959 	{0x6484, "Custom T64100-CR"},
960 	{0x6485, "Custom T6240-SO"},
961 	{0x6486, "Custom T6225-SO-CR"},
962 	{0x6487, "Custom T6225-CR"},
963 };
964 
965 #ifdef TCP_OFFLOAD
966 /*
967  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
968  * be exactly the same for both rxq and ofld_rxq.
969  */
970 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
971 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
972 #endif
973 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
974 
975 static int
t4_probe(device_t dev)976 t4_probe(device_t dev)
977 {
978 	int i;
979 	uint16_t v = pci_get_vendor(dev);
980 	uint16_t d = pci_get_device(dev);
981 	uint8_t f = pci_get_function(dev);
982 
983 	if (v != PCI_VENDOR_ID_CHELSIO)
984 		return (ENXIO);
985 
986 	/* Attach only to PF0 of the FPGA */
987 	if (d == 0xa000 && f != 0)
988 		return (ENXIO);
989 
990 	for (i = 0; i < nitems(t4_pciids); i++) {
991 		if (d == t4_pciids[i].device) {
992 			device_set_desc(dev, t4_pciids[i].desc);
993 			return (BUS_PROBE_DEFAULT);
994 		}
995 	}
996 
997 	return (ENXIO);
998 }
999 
1000 static int
t5_probe(device_t dev)1001 t5_probe(device_t dev)
1002 {
1003 	int i;
1004 	uint16_t v = pci_get_vendor(dev);
1005 	uint16_t d = pci_get_device(dev);
1006 	uint8_t f = pci_get_function(dev);
1007 
1008 	if (v != PCI_VENDOR_ID_CHELSIO)
1009 		return (ENXIO);
1010 
1011 	/* Attach only to PF0 of the FPGA */
1012 	if (d == 0xb000 && f != 0)
1013 		return (ENXIO);
1014 
1015 	for (i = 0; i < nitems(t5_pciids); i++) {
1016 		if (d == t5_pciids[i].device) {
1017 			device_set_desc(dev, t5_pciids[i].desc);
1018 			return (BUS_PROBE_DEFAULT);
1019 		}
1020 	}
1021 
1022 	return (ENXIO);
1023 }
1024 
1025 static int
t6_probe(device_t dev)1026 t6_probe(device_t dev)
1027 {
1028 	int i;
1029 	uint16_t v = pci_get_vendor(dev);
1030 	uint16_t d = pci_get_device(dev);
1031 
1032 	if (v != PCI_VENDOR_ID_CHELSIO)
1033 		return (ENXIO);
1034 
1035 	for (i = 0; i < nitems(t6_pciids); i++) {
1036 		if (d == t6_pciids[i].device) {
1037 			device_set_desc(dev, t6_pciids[i].desc);
1038 			return (BUS_PROBE_DEFAULT);
1039 		}
1040 	}
1041 
1042 	return (ENXIO);
1043 }
1044 
1045 static void
t5_attribute_workaround(device_t dev)1046 t5_attribute_workaround(device_t dev)
1047 {
1048 	device_t root_port;
1049 	uint32_t v;
1050 
1051 	/*
1052 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1053 	 * Ordering attributes when replying to a TLP from a Root
1054 	 * Port.  As a workaround, find the parent Root Port and
1055 	 * disable No Snoop and Relaxed Ordering.  Note that this
1056 	 * affects all devices under this root port.
1057 	 */
1058 	root_port = pci_find_pcie_root_port(dev);
1059 	if (root_port == NULL) {
1060 		device_printf(dev, "Unable to find parent root port\n");
1061 		return;
1062 	}
1063 
1064 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1065 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1066 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1067 	    0)
1068 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1069 		    device_get_nameunit(root_port));
1070 }
1071 
1072 static const struct devnames devnames[] = {
1073 	{
1074 		.nexus_name = "t4nex",
1075 		.ifnet_name = "cxgbe",
1076 		.vi_ifnet_name = "vcxgbe",
1077 		.pf03_drv_name = "t4iov",
1078 		.vf_nexus_name = "t4vf",
1079 		.vf_ifnet_name = "cxgbev"
1080 	}, {
1081 		.nexus_name = "t5nex",
1082 		.ifnet_name = "cxl",
1083 		.vi_ifnet_name = "vcxl",
1084 		.pf03_drv_name = "t5iov",
1085 		.vf_nexus_name = "t5vf",
1086 		.vf_ifnet_name = "cxlv"
1087 	}, {
1088 		.nexus_name = "t6nex",
1089 		.ifnet_name = "cc",
1090 		.vi_ifnet_name = "vcc",
1091 		.pf03_drv_name = "t6iov",
1092 		.vf_nexus_name = "t6vf",
1093 		.vf_ifnet_name = "ccv"
1094 	}
1095 };
1096 
1097 void
t4_init_devnames(struct adapter * sc)1098 t4_init_devnames(struct adapter *sc)
1099 {
1100 	int id;
1101 
1102 	id = chip_id(sc);
1103 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
1104 		sc->names = &devnames[id - CHELSIO_T4];
1105 	else {
1106 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1107 		sc->names = NULL;
1108 	}
1109 }
1110 
1111 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1112 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1113 {
1114 	const char *parent, *name;
1115 	long value;
1116 	int line, unit;
1117 
1118 	line = 0;
1119 	parent = device_get_nameunit(sc->dev);
1120 	name = sc->names->ifnet_name;
1121 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1122 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1123 		    value == pi->port_id)
1124 			return (unit);
1125 	}
1126 	return (-1);
1127 }
1128 
1129 static void
t4_calibration(void * arg)1130 t4_calibration(void *arg)
1131 {
1132 	struct adapter *sc;
1133 	struct clock_sync *cur, *nex;
1134 	uint64_t hw;
1135 	sbintime_t sbt;
1136 	int next_up;
1137 
1138 	sc = (struct adapter *)arg;
1139 
1140 	KASSERT(hw_all_ok(sc), ("!hw_all_ok at t4_calibration"));
1141 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1142 	sbt = sbinuptime();
1143 
1144 	cur = &sc->cal_info[sc->cal_current];
1145 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1146 	nex = &sc->cal_info[next_up];
1147 	if (__predict_false(sc->cal_count == 0)) {
1148 		/* First time in, just get the values in */
1149 		cur->hw_cur = hw;
1150 		cur->sbt_cur = sbt;
1151 		sc->cal_count++;
1152 		goto done;
1153 	}
1154 
1155 	if (cur->hw_cur == hw) {
1156 		/* The clock is not advancing? */
1157 		sc->cal_count = 0;
1158 		atomic_store_rel_int(&cur->gen, 0);
1159 		goto done;
1160 	}
1161 
1162 	seqc_write_begin(&nex->gen);
1163 	nex->hw_prev = cur->hw_cur;
1164 	nex->sbt_prev = cur->sbt_cur;
1165 	nex->hw_cur = hw;
1166 	nex->sbt_cur = sbt;
1167 	seqc_write_end(&nex->gen);
1168 	sc->cal_current = next_up;
1169 done:
1170 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1171 	    sc, C_DIRECT_EXEC);
1172 }
1173 
1174 static void
t4_calibration_start(struct adapter * sc)1175 t4_calibration_start(struct adapter *sc)
1176 {
1177 	/*
1178 	 * Here if we have not done a calibration
1179 	 * then do so otherwise start the appropriate
1180 	 * timer.
1181 	 */
1182 	int i;
1183 
1184 	for (i = 0; i < CNT_CAL_INFO; i++) {
1185 		sc->cal_info[i].gen = 0;
1186 	}
1187 	sc->cal_current = 0;
1188 	sc->cal_count = 0;
1189 	sc->cal_gen = 0;
1190 	t4_calibration(sc);
1191 }
1192 
1193 static int
t4_attach(device_t dev)1194 t4_attach(device_t dev)
1195 {
1196 	struct adapter *sc;
1197 	int rc = 0, i, j, rqidx, tqidx, nports;
1198 	struct make_dev_args mda;
1199 	struct intrs_and_queues iaq;
1200 	struct sge *s;
1201 	uint32_t *buf;
1202 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1203 	int ofld_tqidx;
1204 #endif
1205 #ifdef TCP_OFFLOAD
1206 	int ofld_rqidx;
1207 #endif
1208 #ifdef DEV_NETMAP
1209 	int nm_rqidx, nm_tqidx;
1210 #endif
1211 	int num_vis;
1212 
1213 	sc = device_get_softc(dev);
1214 	sc->dev = dev;
1215 	sysctl_ctx_init(&sc->ctx);
1216 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1217 
1218 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1219 		t5_attribute_workaround(dev);
1220 	pci_enable_busmaster(dev);
1221 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1222 		uint32_t v;
1223 
1224 		pci_set_max_read_req(dev, 4096);
1225 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1226 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1227 		if (pcie_relaxed_ordering == 0 &&
1228 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1229 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1230 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1231 		} else if (pcie_relaxed_ordering == 1 &&
1232 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1233 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1234 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1235 		}
1236 	}
1237 
1238 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1239 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1240 	sc->traceq = -1;
1241 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1242 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1243 	    device_get_nameunit(dev));
1244 
1245 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1246 	    device_get_nameunit(dev));
1247 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1248 	t4_add_adapter(sc);
1249 
1250 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1251 	TAILQ_INIT(&sc->sfl);
1252 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1253 
1254 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1255 
1256 	sc->policy = NULL;
1257 	rw_init(&sc->policy_lock, "connection offload policy");
1258 
1259 	callout_init(&sc->ktls_tick, 1);
1260 
1261 	callout_init(&sc->cal_callout, 1);
1262 
1263 	refcount_init(&sc->vxlan_refcount, 0);
1264 
1265 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1266 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1267 
1268 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1269 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1270 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1271 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1272 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1273 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1274 
1275 	rc = t4_map_bars_0_and_4(sc);
1276 	if (rc != 0)
1277 		goto done; /* error message displayed already */
1278 
1279 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1280 
1281 	/* Prepare the adapter for operation. */
1282 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1283 	rc = -t4_prep_adapter(sc, buf);
1284 	free(buf, M_CXGBE);
1285 	if (rc != 0) {
1286 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1287 		goto done;
1288 	}
1289 
1290 	/*
1291 	 * This is the real PF# to which we're attaching.  Works from within PCI
1292 	 * passthrough environments too, where pci_get_function() could return a
1293 	 * different PF# depending on the passthrough configuration.  We need to
1294 	 * use the real PF# in all our communication with the firmware.
1295 	 */
1296 	j = t4_read_reg(sc, A_PL_WHOAMI);
1297 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1298 	sc->mbox = sc->pf;
1299 
1300 	t4_init_devnames(sc);
1301 	if (sc->names == NULL) {
1302 		rc = ENOTSUP;
1303 		goto done; /* error message displayed already */
1304 	}
1305 
1306 	/*
1307 	 * Do this really early, with the memory windows set up even before the
1308 	 * character device.  The userland tool's register i/o and mem read
1309 	 * will work even in "recovery mode".
1310 	 */
1311 	setup_memwin(sc);
1312 	if (t4_init_devlog_params(sc, 0) == 0)
1313 		fixup_devlog_params(sc);
1314 	make_dev_args_init(&mda);
1315 	mda.mda_devsw = &t4_cdevsw;
1316 	mda.mda_uid = UID_ROOT;
1317 	mda.mda_gid = GID_WHEEL;
1318 	mda.mda_mode = 0600;
1319 	mda.mda_si_drv1 = sc;
1320 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1321 	if (rc != 0)
1322 		device_printf(dev, "failed to create nexus char device: %d.\n",
1323 		    rc);
1324 
1325 	/* Go no further if recovery mode has been requested. */
1326 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1327 		device_printf(dev, "recovery mode.\n");
1328 		goto done;
1329 	}
1330 
1331 #if defined(__i386__)
1332 	if ((cpu_feature & CPUID_CX8) == 0) {
1333 		device_printf(dev, "64 bit atomics not available.\n");
1334 		rc = ENOTSUP;
1335 		goto done;
1336 	}
1337 #endif
1338 
1339 	/* Contact the firmware and try to become the master driver. */
1340 	rc = contact_firmware(sc);
1341 	if (rc != 0)
1342 		goto done; /* error message displayed already */
1343 	MPASS(sc->flags & FW_OK);
1344 
1345 	rc = get_params__pre_init(sc);
1346 	if (rc != 0)
1347 		goto done; /* error message displayed already */
1348 
1349 	if (sc->flags & MASTER_PF) {
1350 		rc = partition_resources(sc);
1351 		if (rc != 0)
1352 			goto done; /* error message displayed already */
1353 	}
1354 
1355 	rc = get_params__post_init(sc);
1356 	if (rc != 0)
1357 		goto done; /* error message displayed already */
1358 
1359 	rc = set_params__post_init(sc);
1360 	if (rc != 0)
1361 		goto done; /* error message displayed already */
1362 
1363 	rc = t4_map_bar_2(sc);
1364 	if (rc != 0)
1365 		goto done; /* error message displayed already */
1366 
1367 	rc = t4_adj_doorbells(sc);
1368 	if (rc != 0)
1369 		goto done; /* error message displayed already */
1370 
1371 	rc = t4_create_dma_tag(sc);
1372 	if (rc != 0)
1373 		goto done; /* error message displayed already */
1374 
1375 	/*
1376 	 * First pass over all the ports - allocate VIs and initialize some
1377 	 * basic parameters like mac address, port type, etc.
1378 	 */
1379 	for_each_port(sc, i) {
1380 		struct port_info *pi;
1381 
1382 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1383 		sc->port[i] = pi;
1384 
1385 		/* These must be set before t4_port_init */
1386 		pi->adapter = sc;
1387 		pi->port_id = i;
1388 		/*
1389 		 * XXX: vi[0] is special so we can't delay this allocation until
1390 		 * pi->nvi's final value is known.
1391 		 */
1392 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1393 		    M_ZERO | M_WAITOK);
1394 
1395 		/*
1396 		 * Allocate the "main" VI and initialize parameters
1397 		 * like mac addr.
1398 		 */
1399 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1400 		if (rc != 0) {
1401 			device_printf(dev, "unable to initialize port %d: %d\n",
1402 			    i, rc);
1403 			free(pi->vi, M_CXGBE);
1404 			free(pi, M_CXGBE);
1405 			sc->port[i] = NULL;
1406 			goto done;
1407 		}
1408 
1409 		if (is_bt(pi->port_type))
1410 			setbit(&sc->bt_map, pi->tx_chan);
1411 		else
1412 			MPASS(!isset(&sc->bt_map, pi->tx_chan));
1413 
1414 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1415 		    device_get_nameunit(dev), i);
1416 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1417 		sc->chan_map[pi->tx_chan] = i;
1418 
1419 		/*
1420 		 * The MPS counter for FCS errors doesn't work correctly on the
1421 		 * T6 so we use the MAC counter here.  Which MAC is in use
1422 		 * depends on the link settings which will be known when the
1423 		 * link comes up.
1424 		 */
1425 		if (is_t6(sc))
1426 			pi->fcs_reg = -1;
1427 		else {
1428 			pi->fcs_reg = t4_port_reg(sc, pi->tx_chan,
1429 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1430 		}
1431 		pi->fcs_base = 0;
1432 
1433 		/* All VIs on this port share this media. */
1434 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1435 		    cxgbe_media_status);
1436 
1437 		PORT_LOCK(pi);
1438 		init_link_config(pi);
1439 		fixup_link_config(pi);
1440 		build_medialist(pi);
1441 		if (fixed_ifmedia(pi))
1442 			pi->flags |= FIXED_IFMEDIA;
1443 		PORT_UNLOCK(pi);
1444 
1445 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1446 		    t4_ifnet_unit(sc, pi));
1447 		if (pi->dev == NULL) {
1448 			device_printf(dev,
1449 			    "failed to add device for port %d.\n", i);
1450 			rc = ENXIO;
1451 			goto done;
1452 		}
1453 		pi->vi[0].dev = pi->dev;
1454 		device_set_softc(pi->dev, pi);
1455 	}
1456 
1457 	/*
1458 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1459 	 */
1460 	nports = sc->params.nports;
1461 	rc = cfg_itype_and_nqueues(sc, &iaq);
1462 	if (rc != 0)
1463 		goto done; /* error message displayed already */
1464 
1465 	num_vis = iaq.num_vis;
1466 	sc->intr_type = iaq.intr_type;
1467 	sc->intr_count = iaq.nirq;
1468 
1469 	s = &sc->sge;
1470 	s->nrxq = nports * iaq.nrxq;
1471 	s->ntxq = nports * iaq.ntxq;
1472 	if (num_vis > 1) {
1473 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1474 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1475 	}
1476 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1477 	s->neq += nports;		/* ctrl queues: 1 per port */
1478 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1479 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1480 	if (is_offload(sc) || is_ethoffload(sc)) {
1481 		s->nofldtxq = nports * iaq.nofldtxq;
1482 		if (num_vis > 1)
1483 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1484 		s->neq += s->nofldtxq;
1485 
1486 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1487 		    M_CXGBE, M_ZERO | M_WAITOK);
1488 	}
1489 #endif
1490 #ifdef TCP_OFFLOAD
1491 	if (is_offload(sc)) {
1492 		s->nofldrxq = nports * iaq.nofldrxq;
1493 		if (num_vis > 1)
1494 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1495 		s->neq += s->nofldrxq;	/* free list */
1496 		s->niq += s->nofldrxq;
1497 
1498 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1499 		    M_CXGBE, M_ZERO | M_WAITOK);
1500 	}
1501 #endif
1502 #ifdef DEV_NETMAP
1503 	s->nnmrxq = 0;
1504 	s->nnmtxq = 0;
1505 	if (t4_native_netmap & NN_MAIN_VI) {
1506 		s->nnmrxq += nports * iaq.nnmrxq;
1507 		s->nnmtxq += nports * iaq.nnmtxq;
1508 	}
1509 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1510 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1511 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1512 	}
1513 	s->neq += s->nnmtxq + s->nnmrxq;
1514 	s->niq += s->nnmrxq;
1515 
1516 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1517 	    M_CXGBE, M_ZERO | M_WAITOK);
1518 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1519 	    M_CXGBE, M_ZERO | M_WAITOK);
1520 #endif
1521 	MPASS(s->niq <= s->iqmap_sz);
1522 	MPASS(s->neq <= s->eqmap_sz);
1523 
1524 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1525 	    M_ZERO | M_WAITOK);
1526 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1527 	    M_ZERO | M_WAITOK);
1528 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1529 	    M_ZERO | M_WAITOK);
1530 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1531 	    M_ZERO | M_WAITOK);
1532 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1533 	    M_ZERO | M_WAITOK);
1534 
1535 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1536 	    M_ZERO | M_WAITOK);
1537 
1538 	t4_init_l2t(sc, M_WAITOK);
1539 	t4_init_smt(sc, M_WAITOK);
1540 	t4_init_tx_sched(sc);
1541 	t4_init_atid_table(sc);
1542 #ifdef RATELIMIT
1543 	t4_init_etid_table(sc);
1544 #endif
1545 #ifdef INET6
1546 	t4_init_clip_table(sc);
1547 #endif
1548 	if (sc->vres.key.size != 0)
1549 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1550 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1551 
1552 	/*
1553 	 * Second pass over the ports.  This time we know the number of rx and
1554 	 * tx queues that each port should get.
1555 	 */
1556 	rqidx = tqidx = 0;
1557 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1558 	ofld_tqidx = 0;
1559 #endif
1560 #ifdef TCP_OFFLOAD
1561 	ofld_rqidx = 0;
1562 #endif
1563 #ifdef DEV_NETMAP
1564 	nm_rqidx = nm_tqidx = 0;
1565 #endif
1566 	for_each_port(sc, i) {
1567 		struct port_info *pi = sc->port[i];
1568 		struct vi_info *vi;
1569 
1570 		if (pi == NULL)
1571 			continue;
1572 
1573 		pi->nvi = num_vis;
1574 		for_each_vi(pi, j, vi) {
1575 			vi->pi = pi;
1576 			vi->adapter = sc;
1577 			vi->first_intr = -1;
1578 			vi->qsize_rxq = t4_qsize_rxq;
1579 			vi->qsize_txq = t4_qsize_txq;
1580 
1581 			vi->first_rxq = rqidx;
1582 			vi->first_txq = tqidx;
1583 			vi->tmr_idx = t4_tmr_idx;
1584 			vi->pktc_idx = t4_pktc_idx;
1585 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1586 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1587 
1588 			rqidx += vi->nrxq;
1589 			tqidx += vi->ntxq;
1590 
1591 			if (j == 0 && vi->ntxq > 1)
1592 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1593 			else
1594 				vi->rsrv_noflowq = 0;
1595 
1596 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1597 			vi->first_ofld_txq = ofld_tqidx;
1598 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1599 			ofld_tqidx += vi->nofldtxq;
1600 #endif
1601 #ifdef TCP_OFFLOAD
1602 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1603 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1604 			vi->first_ofld_rxq = ofld_rqidx;
1605 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1606 
1607 			ofld_rqidx += vi->nofldrxq;
1608 #endif
1609 #ifdef DEV_NETMAP
1610 			vi->first_nm_rxq = nm_rqidx;
1611 			vi->first_nm_txq = nm_tqidx;
1612 			if (j == 0) {
1613 				vi->nnmrxq = iaq.nnmrxq;
1614 				vi->nnmtxq = iaq.nnmtxq;
1615 			} else {
1616 				vi->nnmrxq = iaq.nnmrxq_vi;
1617 				vi->nnmtxq = iaq.nnmtxq_vi;
1618 			}
1619 			nm_rqidx += vi->nnmrxq;
1620 			nm_tqidx += vi->nnmtxq;
1621 #endif
1622 		}
1623 	}
1624 
1625 	rc = t4_setup_intr_handlers(sc);
1626 	if (rc != 0) {
1627 		device_printf(dev,
1628 		    "failed to setup interrupt handlers: %d\n", rc);
1629 		goto done;
1630 	}
1631 
1632 	bus_identify_children(dev);
1633 
1634 	/*
1635 	 * Ensure thread-safe mailbox access (in debug builds).
1636 	 *
1637 	 * So far this was the only thread accessing the mailbox but various
1638 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1639 	 * will access the mailbox from different threads.
1640 	 */
1641 	sc->flags |= CHK_MBOX_ACCESS;
1642 
1643 	bus_attach_children(dev);
1644 	t4_calibration_start(sc);
1645 
1646 	device_printf(dev,
1647 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1648 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1649 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1650 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1651 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1652 
1653 	t4_set_desc(sc);
1654 
1655 	notify_siblings(dev, 0);
1656 
1657 done:
1658 	if (rc != 0 && sc->cdev) {
1659 		/* cdev was created and so cxgbetool works; recover that way. */
1660 		device_printf(dev,
1661 		    "error during attach, adapter is now in recovery mode.\n");
1662 		rc = 0;
1663 	}
1664 
1665 	if (rc != 0)
1666 		t4_detach_common(dev);
1667 	else
1668 		t4_sysctls(sc);
1669 
1670 	return (rc);
1671 }
1672 
1673 static int
t4_child_location(device_t bus,device_t dev,struct sbuf * sb)1674 t4_child_location(device_t bus, device_t dev, struct sbuf *sb)
1675 {
1676 	struct adapter *sc;
1677 	struct port_info *pi;
1678 	int i;
1679 
1680 	sc = device_get_softc(bus);
1681 	for_each_port(sc, i) {
1682 		pi = sc->port[i];
1683 		if (pi != NULL && pi->dev == dev) {
1684 			sbuf_printf(sb, "port=%d", pi->port_id);
1685 			break;
1686 		}
1687 	}
1688 	return (0);
1689 }
1690 
1691 static int
t4_ready(device_t dev)1692 t4_ready(device_t dev)
1693 {
1694 	struct adapter *sc;
1695 
1696 	sc = device_get_softc(dev);
1697 	if (sc->flags & FW_OK)
1698 		return (0);
1699 	return (ENXIO);
1700 }
1701 
1702 static int
t4_read_port_device(device_t dev,int port,device_t * child)1703 t4_read_port_device(device_t dev, int port, device_t *child)
1704 {
1705 	struct adapter *sc;
1706 	struct port_info *pi;
1707 
1708 	sc = device_get_softc(dev);
1709 	if (port < 0 || port >= MAX_NPORTS)
1710 		return (EINVAL);
1711 	pi = sc->port[port];
1712 	if (pi == NULL || pi->dev == NULL)
1713 		return (ENXIO);
1714 	*child = pi->dev;
1715 	return (0);
1716 }
1717 
1718 static int
notify_siblings(device_t dev,int detaching)1719 notify_siblings(device_t dev, int detaching)
1720 {
1721 	device_t sibling;
1722 	int error, i;
1723 
1724 	error = 0;
1725 	for (i = 0; i < PCI_FUNCMAX; i++) {
1726 		if (i == pci_get_function(dev))
1727 			continue;
1728 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1729 		    pci_get_slot(dev), i);
1730 		if (sibling == NULL || !device_is_attached(sibling))
1731 			continue;
1732 		if (detaching)
1733 			error = T4_DETACH_CHILD(sibling);
1734 		else
1735 			(void)T4_ATTACH_CHILD(sibling);
1736 		if (error)
1737 			break;
1738 	}
1739 	return (error);
1740 }
1741 
1742 /*
1743  * Idempotent
1744  */
1745 static int
t4_detach(device_t dev)1746 t4_detach(device_t dev)
1747 {
1748 	int rc;
1749 
1750 	rc = notify_siblings(dev, 1);
1751 	if (rc) {
1752 		device_printf(dev,
1753 		    "failed to detach sibling devices: %d\n", rc);
1754 		return (rc);
1755 	}
1756 
1757 	return (t4_detach_common(dev));
1758 }
1759 
1760 int
t4_detach_common(device_t dev)1761 t4_detach_common(device_t dev)
1762 {
1763 	struct adapter *sc;
1764 	struct port_info *pi;
1765 	int i, rc;
1766 
1767 	sc = device_get_softc(dev);
1768 
1769 #ifdef TCP_OFFLOAD
1770 	rc = deactivate_all_uld(sc);
1771 	if (rc) {
1772 		device_printf(dev,
1773 		    "failed to detach upper layer drivers: %d\n", rc);
1774 		return (rc);
1775 	}
1776 #endif
1777 
1778 	if (sc->cdev) {
1779 		destroy_dev(sc->cdev);
1780 		sc->cdev = NULL;
1781 	}
1782 
1783 	sx_xlock(&t4_list_lock);
1784 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1785 	sx_xunlock(&t4_list_lock);
1786 
1787 	sc->flags &= ~CHK_MBOX_ACCESS;
1788 	if (sc->flags & FULL_INIT_DONE) {
1789 		if (!(sc->flags & IS_VF))
1790 			t4_intr_disable(sc);
1791 	}
1792 
1793 	if (device_is_attached(dev)) {
1794 		rc = bus_detach_children(dev);
1795 		if (rc) {
1796 			device_printf(dev,
1797 			    "failed to detach child devices: %d\n", rc);
1798 			return (rc);
1799 		}
1800 	}
1801 
1802 	for (i = 0; i < sc->intr_count; i++)
1803 		t4_free_irq(sc, &sc->irq[i]);
1804 
1805 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1806 		t4_free_tx_sched(sc);
1807 
1808 	for (i = 0; i < MAX_NPORTS; i++) {
1809 		pi = sc->port[i];
1810 		if (pi) {
1811 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1812 
1813 			mtx_destroy(&pi->pi_lock);
1814 			free(pi->vi, M_CXGBE);
1815 			free(pi, M_CXGBE);
1816 		}
1817 	}
1818 	callout_stop(&sc->cal_callout);
1819 	callout_drain(&sc->cal_callout);
1820 	device_delete_children(dev);
1821 	sysctl_ctx_free(&sc->ctx);
1822 	adapter_full_uninit(sc);
1823 
1824 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1825 		t4_fw_bye(sc, sc->mbox);
1826 
1827 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1828 		pci_release_msi(dev);
1829 
1830 	if (sc->regs_res)
1831 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1832 		    sc->regs_res);
1833 
1834 	if (sc->udbs_res)
1835 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1836 		    sc->udbs_res);
1837 
1838 	if (sc->msix_res)
1839 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1840 		    sc->msix_res);
1841 
1842 	if (sc->l2t)
1843 		t4_free_l2t(sc);
1844 	if (sc->smt)
1845 		t4_free_smt(sc->smt);
1846 	t4_free_atid_table(sc);
1847 #ifdef RATELIMIT
1848 	t4_free_etid_table(sc);
1849 #endif
1850 	if (sc->key_map)
1851 		vmem_destroy(sc->key_map);
1852 #ifdef INET6
1853 	t4_destroy_clip_table(sc);
1854 #endif
1855 
1856 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1857 	free(sc->sge.ofld_txq, M_CXGBE);
1858 #endif
1859 #ifdef TCP_OFFLOAD
1860 	free(sc->sge.ofld_rxq, M_CXGBE);
1861 #endif
1862 #ifdef DEV_NETMAP
1863 	free(sc->sge.nm_rxq, M_CXGBE);
1864 	free(sc->sge.nm_txq, M_CXGBE);
1865 #endif
1866 	free(sc->irq, M_CXGBE);
1867 	free(sc->sge.rxq, M_CXGBE);
1868 	free(sc->sge.txq, M_CXGBE);
1869 	free(sc->sge.ctrlq, M_CXGBE);
1870 	free(sc->sge.iqmap, M_CXGBE);
1871 	free(sc->sge.eqmap, M_CXGBE);
1872 	free(sc->tids.ftid_tab, M_CXGBE);
1873 	free(sc->tids.hpftid_tab, M_CXGBE);
1874 	free_hftid_hash(&sc->tids);
1875 	free(sc->tids.tid_tab, M_CXGBE);
1876 	t4_destroy_dma_tag(sc);
1877 
1878 	callout_drain(&sc->ktls_tick);
1879 	callout_drain(&sc->sfl_callout);
1880 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1881 		mtx_destroy(&sc->tids.ftid_lock);
1882 		cv_destroy(&sc->tids.ftid_cv);
1883 	}
1884 	if (mtx_initialized(&sc->tids.atid_lock))
1885 		mtx_destroy(&sc->tids.atid_lock);
1886 	if (mtx_initialized(&sc->ifp_lock))
1887 		mtx_destroy(&sc->ifp_lock);
1888 
1889 	if (rw_initialized(&sc->policy_lock)) {
1890 		rw_destroy(&sc->policy_lock);
1891 #ifdef TCP_OFFLOAD
1892 		if (sc->policy != NULL)
1893 			free_offload_policy(sc->policy);
1894 #endif
1895 	}
1896 
1897 	for (i = 0; i < NUM_MEMWIN; i++) {
1898 		struct memwin *mw = &sc->memwin[i];
1899 
1900 		if (rw_initialized(&mw->mw_lock))
1901 			rw_destroy(&mw->mw_lock);
1902 	}
1903 
1904 	mtx_destroy(&sc->sfl_lock);
1905 	mtx_destroy(&sc->reg_lock);
1906 	mtx_destroy(&sc->sc_lock);
1907 
1908 	bzero(sc, sizeof(*sc));
1909 
1910 	return (0);
1911 }
1912 
1913 static inline int
stop_adapter(struct adapter * sc)1914 stop_adapter(struct adapter *sc)
1915 {
1916 	struct port_info *pi;
1917 	int i;
1918 
1919 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1920 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1921 			 __func__, curthread, sc->flags, sc->error_flags);
1922 		return (EALREADY);
1923 	}
1924 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1925 		 sc->flags, sc->error_flags);
1926 	t4_shutdown_adapter(sc);
1927 	for_each_port(sc, i) {
1928 		pi = sc->port[i];
1929 		if (pi == NULL)
1930 			continue;
1931 		PORT_LOCK(pi);
1932 		if (pi->up_vis > 0 && pi->link_cfg.link_ok) {
1933 			/*
1934 			 * t4_shutdown_adapter has already shut down all the
1935 			 * PHYs but it also disables interrupts and DMA so there
1936 			 * won't be a link interrupt.  Update the state manually
1937 			 * if the link was up previously and inform the kernel.
1938 			 */
1939 			pi->link_cfg.link_ok = false;
1940 			t4_os_link_changed(pi);
1941 		}
1942 		PORT_UNLOCK(pi);
1943 	}
1944 
1945 	return (0);
1946 }
1947 
1948 static inline int
restart_adapter(struct adapter * sc)1949 restart_adapter(struct adapter *sc)
1950 {
1951 	uint32_t val;
1952 
1953 	if (!atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1954 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1955 			 __func__, curthread, sc->flags, sc->error_flags);
1956 		return (EALREADY);
1957 	}
1958 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1959 		 sc->flags, sc->error_flags);
1960 
1961 	MPASS(hw_off_limits(sc));
1962 	MPASS((sc->flags & FW_OK) == 0);
1963 	MPASS((sc->flags & MASTER_PF) == 0);
1964 	MPASS(sc->reset_thread == NULL);
1965 
1966 	/*
1967 	 * The adapter is supposed to be back on PCIE with its config space and
1968 	 * BARs restored to their state before reset.  Register access via
1969 	 * t4_read_reg BAR0 should just work.
1970 	 */
1971 	sc->reset_thread = curthread;
1972 	val = t4_read_reg(sc, A_PL_WHOAMI);
1973 	if (val == 0xffffffff || val == 0xeeeeeeee) {
1974 		CH_ERR(sc, "%s: device registers not readable.\n", __func__);
1975 		sc->reset_thread = NULL;
1976 		atomic_set_int(&sc->error_flags, ADAP_STOPPED);
1977 		return (ENXIO);
1978 	}
1979 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR);
1980 	atomic_add_int(&sc->incarnation, 1);
1981 	atomic_add_int(&sc->num_resets, 1);
1982 
1983 	return (0);
1984 }
1985 
1986 static inline void
set_adapter_hwstatus(struct adapter * sc,const bool usable)1987 set_adapter_hwstatus(struct adapter *sc, const bool usable)
1988 {
1989 	if (usable) {
1990 		/* Must be marked reusable by the designated thread. */
1991 		ASSERT_SYNCHRONIZED_OP(sc);
1992 		MPASS(sc->reset_thread == curthread);
1993 		mtx_lock(&sc->reg_lock);
1994 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
1995 		mtx_unlock(&sc->reg_lock);
1996 	} else {
1997 		/* Mark the adapter totally off limits. */
1998 		begin_synchronized_op(sc, NULL, SLEEP_OK, "t4hwsts");
1999 		mtx_lock(&sc->reg_lock);
2000 		atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
2001 		mtx_unlock(&sc->reg_lock);
2002 		sc->flags &= ~(FW_OK | MASTER_PF);
2003 		sc->reset_thread = NULL;
2004 		end_synchronized_op(sc, 0);
2005 	}
2006 }
2007 
2008 static int
stop_lld(struct adapter * sc)2009 stop_lld(struct adapter *sc)
2010 {
2011 	struct port_info *pi;
2012 	struct vi_info *vi;
2013 	if_t ifp;
2014 	struct sge_rxq *rxq;
2015 	struct sge_txq *txq;
2016 	struct sge_wrq *wrq;
2017 #ifdef TCP_OFFLOAD
2018 	struct sge_ofld_rxq *ofld_rxq;
2019 #endif
2020 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2021 	struct sge_ofld_txq *ofld_txq;
2022 #endif
2023 	int rc, i, j, k;
2024 
2025 	/*
2026 	 * XXX: Can there be a synch_op in progress that will hang because
2027 	 * hardware has been stopped?  We'll hang too and the solution will be
2028 	 * to use a version of begin_synch_op that wakes up existing synch_op
2029 	 * with errors.  Maybe stop_adapter should do this wakeup?
2030 	 *
2031 	 * I don't think any synch_op could get stranded waiting for DMA or
2032 	 * interrupt so I think we're okay here.  Remove this comment block
2033 	 * after testing.
2034 	 */
2035 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4slld");
2036 	if (rc != 0)
2037 		return (ENXIO);
2038 
2039 	/* Quiesce all activity. */
2040 	for_each_port(sc, i) {
2041 		pi = sc->port[i];
2042 		if (pi == NULL)
2043 			continue;
2044 		pi->vxlan_tcam_entry = false;
2045 		for_each_vi(pi, j, vi) {
2046 			vi->xact_addr_filt = -1;
2047 			mtx_lock(&vi->tick_mtx);
2048 			vi->flags |= VI_SKIP_STATS;
2049 			mtx_unlock(&vi->tick_mtx);
2050 			if (!(vi->flags & VI_INIT_DONE))
2051 				continue;
2052 
2053 			ifp = vi->ifp;
2054 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2055 				mtx_lock(&vi->tick_mtx);
2056 				callout_stop(&vi->tick);
2057 				mtx_unlock(&vi->tick_mtx);
2058 				callout_drain(&vi->tick);
2059 			}
2060 
2061 			/*
2062 			 * Note that the HW is not available.
2063 			 */
2064 			for_each_txq(vi, k, txq) {
2065 				TXQ_LOCK(txq);
2066 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2067 				TXQ_UNLOCK(txq);
2068 			}
2069 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2070 			for_each_ofld_txq(vi, k, ofld_txq) {
2071 				TXQ_LOCK(&ofld_txq->wrq);
2072 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2073 				TXQ_UNLOCK(&ofld_txq->wrq);
2074 			}
2075 #endif
2076 			for_each_rxq(vi, k, rxq) {
2077 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2078 			}
2079 #if defined(TCP_OFFLOAD)
2080 			for_each_ofld_rxq(vi, k, ofld_rxq) {
2081 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2082 			}
2083 #endif
2084 
2085 			quiesce_vi(vi);
2086 		}
2087 
2088 		if (sc->flags & FULL_INIT_DONE) {
2089 			/* Control queue */
2090 			wrq = &sc->sge.ctrlq[i];
2091 			TXQ_LOCK(wrq);
2092 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2093 			TXQ_UNLOCK(wrq);
2094 			quiesce_wrq(wrq);
2095 		}
2096 
2097 		if (pi->flags & HAS_TRACEQ) {
2098 			pi->flags &= ~HAS_TRACEQ;
2099 			sc->traceq = -1;
2100 			sc->tracer_valid = 0;
2101 			sc->tracer_enabled = 0;
2102 		}
2103 	}
2104 	if (sc->flags & FULL_INIT_DONE) {
2105 		/* Firmware event queue */
2106 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2107 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2108 	}
2109 
2110 	/* Stop calibration */
2111 	callout_stop(&sc->cal_callout);
2112 	callout_drain(&sc->cal_callout);
2113 
2114 	if (t4_clock_gate_on_suspend) {
2115 		t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2116 		    F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2117 		    F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2118 	}
2119 
2120 	end_synchronized_op(sc, 0);
2121 
2122 	stop_atid_allocator(sc);
2123 	t4_stop_l2t(sc);
2124 
2125 	return (rc);
2126 }
2127 
2128 int
suspend_adapter(struct adapter * sc)2129 suspend_adapter(struct adapter *sc)
2130 {
2131 	stop_adapter(sc);
2132 	stop_lld(sc);
2133 #ifdef TCP_OFFLOAD
2134 	stop_all_uld(sc);
2135 #endif
2136 	set_adapter_hwstatus(sc, false);
2137 
2138 	return (0);
2139 }
2140 
2141 static int
t4_suspend(device_t dev)2142 t4_suspend(device_t dev)
2143 {
2144 	struct adapter *sc = device_get_softc(dev);
2145 	int rc;
2146 
2147 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2148 	rc = suspend_adapter(sc);
2149 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2150 
2151 	return (rc);
2152 }
2153 
2154 struct adapter_pre_reset_state {
2155 	u_int flags;
2156 	uint16_t nbmcaps;
2157 	uint16_t linkcaps;
2158 	uint16_t switchcaps;
2159 	uint16_t niccaps;
2160 	uint16_t toecaps;
2161 	uint16_t rdmacaps;
2162 	uint16_t cryptocaps;
2163 	uint16_t iscsicaps;
2164 	uint16_t fcoecaps;
2165 
2166 	u_int cfcsum;
2167 	char cfg_file[32];
2168 
2169 	struct adapter_params params;
2170 	struct t4_virt_res vres;
2171 	struct tid_info tids;
2172 	struct sge sge;
2173 
2174 	int rawf_base;
2175 	int nrawf;
2176 
2177 };
2178 
2179 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2180 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2181 {
2182 
2183 	ASSERT_SYNCHRONIZED_OP(sc);
2184 
2185 	o->flags = sc->flags;
2186 
2187 	o->nbmcaps =  sc->nbmcaps;
2188 	o->linkcaps = sc->linkcaps;
2189 	o->switchcaps = sc->switchcaps;
2190 	o->niccaps = sc->niccaps;
2191 	o->toecaps = sc->toecaps;
2192 	o->rdmacaps = sc->rdmacaps;
2193 	o->cryptocaps = sc->cryptocaps;
2194 	o->iscsicaps = sc->iscsicaps;
2195 	o->fcoecaps = sc->fcoecaps;
2196 
2197 	o->cfcsum = sc->cfcsum;
2198 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2199 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2200 
2201 	o->params = sc->params;
2202 	o->vres = sc->vres;
2203 	o->tids = sc->tids;
2204 	o->sge = sc->sge;
2205 
2206 	o->rawf_base = sc->rawf_base;
2207 	o->nrawf = sc->nrawf;
2208 }
2209 
2210 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2211 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2212 {
2213 	int rc = 0;
2214 
2215 	ASSERT_SYNCHRONIZED_OP(sc);
2216 
2217 	/* Capabilities */
2218 #define COMPARE_CAPS(c) do { \
2219 	if (o->c##caps != sc->c##caps) { \
2220 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2221 		    sc->c##caps); \
2222 		rc = EINVAL; \
2223 	} \
2224 } while (0)
2225 	COMPARE_CAPS(nbm);
2226 	COMPARE_CAPS(link);
2227 	COMPARE_CAPS(switch);
2228 	COMPARE_CAPS(nic);
2229 	COMPARE_CAPS(toe);
2230 	COMPARE_CAPS(rdma);
2231 	COMPARE_CAPS(crypto);
2232 	COMPARE_CAPS(iscsi);
2233 	COMPARE_CAPS(fcoe);
2234 #undef COMPARE_CAPS
2235 
2236 	/* Firmware config file */
2237 	if (o->cfcsum != sc->cfcsum) {
2238 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2239 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2240 		rc = EINVAL;
2241 	}
2242 
2243 #define COMPARE_PARAM(p, name) do { \
2244 	if (o->p != sc->p) { \
2245 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2246 		rc = EINVAL; \
2247 	} \
2248 } while (0)
2249 	COMPARE_PARAM(sge.iq_start, iq_start);
2250 	COMPARE_PARAM(sge.eq_start, eq_start);
2251 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2252 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2253 	COMPARE_PARAM(tids.nftids, nftids);
2254 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2255 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2256 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2257 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2258 	COMPARE_PARAM(tids.tid_base, tid_base);
2259 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2260 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2261 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2262 	COMPARE_PARAM(rawf_base, rawf_base);
2263 	COMPARE_PARAM(nrawf, nrawf);
2264 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2265 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2266 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2267 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2268 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2269 	COMPARE_PARAM(tids.ntids, ntids);
2270 	COMPARE_PARAM(tids.etid_base, etid_base);
2271 	COMPARE_PARAM(tids.etid_end, etid_end);
2272 	COMPARE_PARAM(tids.netids, netids);
2273 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2274 	COMPARE_PARAM(params.ethoffload, ethoffload);
2275 	COMPARE_PARAM(tids.natids, natids);
2276 	COMPARE_PARAM(tids.stid_base, stid_base);
2277 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2278 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2279 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2280 	COMPARE_PARAM(vres.stag.start, stag_start);
2281 	COMPARE_PARAM(vres.stag.size, stag_size);
2282 	COMPARE_PARAM(vres.rq.start, rq_start);
2283 	COMPARE_PARAM(vres.rq.size, rq_size);
2284 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2285 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2286 	COMPARE_PARAM(vres.qp.start, qp_start);
2287 	COMPARE_PARAM(vres.qp.size, qp_size);
2288 	COMPARE_PARAM(vres.cq.start, cq_start);
2289 	COMPARE_PARAM(vres.cq.size, cq_size);
2290 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2291 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2292 	COMPARE_PARAM(vres.srq.start, srq_start);
2293 	COMPARE_PARAM(vres.srq.size, srq_size);
2294 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2295 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2296 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2297 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2298 	COMPARE_PARAM(vres.key.start, key_start);
2299 	COMPARE_PARAM(vres.key.size, key_size);
2300 #undef COMPARE_PARAM
2301 
2302 	return (rc);
2303 }
2304 
2305 static int
restart_lld(struct adapter * sc)2306 restart_lld(struct adapter *sc)
2307 {
2308 	struct adapter_pre_reset_state *old_state = NULL;
2309 	struct port_info *pi;
2310 	struct vi_info *vi;
2311 	if_t ifp;
2312 	struct sge_txq *txq;
2313 	int rc, i, j, k;
2314 
2315 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rlld");
2316 	if (rc != 0)
2317 		return (ENXIO);
2318 
2319 	/* Restore memory window. */
2320 	setup_memwin(sc);
2321 
2322 	/* Go no further if recovery mode has been requested. */
2323 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2324 		CH_ALERT(sc, "%s: recovery mode during restart.\n", __func__);
2325 		rc = 0;
2326 		set_adapter_hwstatus(sc, true);
2327 		goto done;
2328 	}
2329 
2330 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2331 	save_caps_and_params(sc, old_state);
2332 
2333 	/* Reestablish contact with firmware and become the primary PF. */
2334 	rc = contact_firmware(sc);
2335 	if (rc != 0)
2336 		goto done; /* error message displayed already */
2337 	MPASS(sc->flags & FW_OK);
2338 
2339 	if (sc->flags & MASTER_PF) {
2340 		rc = partition_resources(sc);
2341 		if (rc != 0)
2342 			goto done; /* error message displayed already */
2343 	}
2344 
2345 	rc = get_params__post_init(sc);
2346 	if (rc != 0)
2347 		goto done; /* error message displayed already */
2348 
2349 	rc = set_params__post_init(sc);
2350 	if (rc != 0)
2351 		goto done; /* error message displayed already */
2352 
2353 	rc = compare_caps_and_params(sc, old_state);
2354 	if (rc != 0)
2355 		goto done; /* error message displayed already */
2356 
2357 	for_each_port(sc, i) {
2358 		pi = sc->port[i];
2359 		MPASS(pi != NULL);
2360 		MPASS(pi->vi != NULL);
2361 		MPASS(pi->vi[0].dev == pi->dev);
2362 
2363 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2364 		if (rc != 0) {
2365 			CH_ERR(sc,
2366 			    "failed to re-initialize port %d: %d\n", i, rc);
2367 			goto done;
2368 		}
2369 		MPASS(sc->chan_map[pi->tx_chan] == i);
2370 
2371 		PORT_LOCK(pi);
2372 		fixup_link_config(pi);
2373 		build_medialist(pi);
2374 		PORT_UNLOCK(pi);
2375 		for_each_vi(pi, j, vi) {
2376 			if (IS_MAIN_VI(vi))
2377 				continue;
2378 			rc = alloc_extra_vi(sc, pi, vi);
2379 			if (rc != 0) {
2380 				CH_ERR(vi,
2381 				    "failed to re-allocate extra VI: %d\n", rc);
2382 				goto done;
2383 			}
2384 		}
2385 	}
2386 
2387 	/*
2388 	 * Interrupts and queues are about to be enabled and other threads will
2389 	 * want to access the hardware too.  It is safe to do so.  Note that
2390 	 * this thread is still in the middle of a synchronized_op.
2391 	 */
2392 	set_adapter_hwstatus(sc, true);
2393 
2394 	if (sc->flags & FULL_INIT_DONE) {
2395 		rc = adapter_full_init(sc);
2396 		if (rc != 0) {
2397 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2398 			goto done;
2399 		}
2400 
2401 		if (sc->vxlan_refcount > 0)
2402 			enable_vxlan_rx(sc);
2403 
2404 		for_each_port(sc, i) {
2405 			pi = sc->port[i];
2406 			for_each_vi(pi, j, vi) {
2407 				mtx_lock(&vi->tick_mtx);
2408 				vi->flags &= ~VI_SKIP_STATS;
2409 				mtx_unlock(&vi->tick_mtx);
2410 				if (!(vi->flags & VI_INIT_DONE))
2411 					continue;
2412 				rc = vi_full_init(vi);
2413 				if (rc != 0) {
2414 					CH_ERR(vi, "failed to re-initialize "
2415 					    "interface: %d\n", rc);
2416 					goto done;
2417 				}
2418 				if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
2419 					sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
2420 					t4_write_reg(sc, is_t4(sc) ?
2421 					    A_MPS_TRC_RSS_CONTROL :
2422 					    A_MPS_T5_TRC_RSS_CONTROL,
2423 					    V_RSSCONTROL(pi->tx_chan) |
2424 					    V_QUEUENUMBER(sc->traceq));
2425 					pi->flags |= HAS_TRACEQ;
2426 				}
2427 
2428 				ifp = vi->ifp;
2429 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2430 					continue;
2431 				/*
2432 				 * Note that we do not setup multicast addresses
2433 				 * in the first pass.  This ensures that the
2434 				 * unicast DMACs for all VIs on all ports get an
2435 				 * MPS TCAM entry.
2436 				 */
2437 				rc = update_mac_settings(ifp, XGMAC_ALL &
2438 				    ~XGMAC_MCADDRS);
2439 				if (rc != 0) {
2440 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2441 					goto done;
2442 				}
2443 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2444 				    true);
2445 				if (rc != 0) {
2446 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2447 					goto done;
2448 				}
2449 				for_each_txq(vi, k, txq) {
2450 					TXQ_LOCK(txq);
2451 					txq->eq.flags |= EQ_ENABLED;
2452 					TXQ_UNLOCK(txq);
2453 				}
2454 				mtx_lock(&vi->tick_mtx);
2455 				callout_schedule(&vi->tick, hz);
2456 				mtx_unlock(&vi->tick_mtx);
2457 			}
2458 			PORT_LOCK(pi);
2459 			if (pi->up_vis > 0) {
2460 				t4_update_port_info(pi);
2461 				fixup_link_config(pi);
2462 				build_medialist(pi);
2463 				apply_link_config(pi);
2464 				if (pi->link_cfg.link_ok)
2465 					t4_os_link_changed(pi);
2466 			}
2467 			PORT_UNLOCK(pi);
2468 		}
2469 
2470 		/* Now reprogram the L2 multicast addresses. */
2471 		for_each_port(sc, i) {
2472 			pi = sc->port[i];
2473 			for_each_vi(pi, j, vi) {
2474 				if (!(vi->flags & VI_INIT_DONE))
2475 					continue;
2476 				ifp = vi->ifp;
2477 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2478 					continue;
2479 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2480 				if (rc != 0) {
2481 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2482 					rc = 0;	/* carry on */
2483 				}
2484 			}
2485 		}
2486 	}
2487 
2488 	/* Reset all calibration */
2489 	t4_calibration_start(sc);
2490 done:
2491 	end_synchronized_op(sc, 0);
2492 	free(old_state, M_CXGBE);
2493 
2494 	restart_atid_allocator(sc);
2495 	t4_restart_l2t(sc);
2496 
2497 	return (rc);
2498 }
2499 
2500 int
resume_adapter(struct adapter * sc)2501 resume_adapter(struct adapter *sc)
2502 {
2503 	restart_adapter(sc);
2504 	restart_lld(sc);
2505 #ifdef TCP_OFFLOAD
2506 	restart_all_uld(sc);
2507 #endif
2508 	return (0);
2509 }
2510 
2511 static int
t4_resume(device_t dev)2512 t4_resume(device_t dev)
2513 {
2514 	struct adapter *sc = device_get_softc(dev);
2515 	int rc;
2516 
2517 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2518 	rc = resume_adapter(sc);
2519 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2520 
2521 	return (rc);
2522 }
2523 
2524 static int
t4_reset_prepare(device_t dev,device_t child)2525 t4_reset_prepare(device_t dev, device_t child)
2526 {
2527 	struct adapter *sc = device_get_softc(dev);
2528 
2529 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2530 	return (0);
2531 }
2532 
2533 static int
t4_reset_post(device_t dev,device_t child)2534 t4_reset_post(device_t dev, device_t child)
2535 {
2536 	struct adapter *sc = device_get_softc(dev);
2537 
2538 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2539 	return (0);
2540 }
2541 
2542 static int
reset_adapter_with_pl_rst(struct adapter * sc)2543 reset_adapter_with_pl_rst(struct adapter *sc)
2544 {
2545 	/* This is a t4_write_reg without the hw_off_limits check. */
2546 	MPASS(sc->error_flags & HW_OFF_LIMITS);
2547 	bus_space_write_4(sc->bt, sc->bh, A_PL_RST,
2548 			  F_PIORSTMODE | F_PIORST | F_AUTOPCIEPAUSE);
2549 	pause("pl_rst", 1 * hz);		/* Wait 1s for reset */
2550 	return (0);
2551 }
2552 
2553 static int
reset_adapter_with_pcie_sbr(struct adapter * sc)2554 reset_adapter_with_pcie_sbr(struct adapter *sc)
2555 {
2556 	device_t pdev = device_get_parent(sc->dev);
2557 	device_t gpdev = device_get_parent(pdev);
2558 	device_t *children;
2559 	int rc, i, lcap, lsta, nchildren;
2560 	uint32_t v;
2561 
2562 	rc = pci_find_cap(gpdev, PCIY_EXPRESS, &v);
2563 	if (rc != 0) {
2564 		CH_ERR(sc, "%s: pci_find_cap(%s, pcie) failed: %d\n", __func__,
2565 		    device_get_nameunit(gpdev), rc);
2566 		return (ENOTSUP);
2567 	}
2568 	lcap = v + PCIER_LINK_CAP;
2569 	lsta = v + PCIER_LINK_STA;
2570 
2571 	nchildren = 0;
2572 	device_get_children(pdev, &children, &nchildren);
2573 	for (i = 0; i < nchildren; i++)
2574 		pci_save_state(children[i]);
2575 	v = pci_read_config(gpdev, PCIR_BRIDGECTL_1, 2);
2576 	pci_write_config(gpdev, PCIR_BRIDGECTL_1, v | PCIB_BCR_SECBUS_RESET, 2);
2577 	pause("pcie_sbr1", hz / 10);	/* 100ms */
2578 	pci_write_config(gpdev, PCIR_BRIDGECTL_1, v, 2);
2579 	pause("pcie_sbr2", hz);		/* Wait 1s before restore_state. */
2580 	v = pci_read_config(gpdev, lsta, 2);
2581 	if (pci_read_config(gpdev, lcap, 2) & PCIEM_LINK_CAP_DL_ACTIVE)
2582 		rc = v & PCIEM_LINK_STA_DL_ACTIVE ? 0 : ETIMEDOUT;
2583 	else if (v & (PCIEM_LINK_STA_TRAINING_ERROR | PCIEM_LINK_STA_TRAINING))
2584 		rc = ETIMEDOUT;
2585 	else
2586 		rc = 0;
2587 	if (rc != 0)
2588 		CH_ERR(sc, "%s: PCIe link is down after reset, LINK_STA 0x%x\n",
2589 		    __func__, v);
2590 	else {
2591 		for (i = 0; i < nchildren; i++)
2592 			pci_restore_state(children[i]);
2593 	}
2594 	free(children, M_TEMP);
2595 
2596 	return (rc);
2597 }
2598 
2599 static int
reset_adapter_with_pcie_link_bounce(struct adapter * sc)2600 reset_adapter_with_pcie_link_bounce(struct adapter *sc)
2601 {
2602 	device_t pdev = device_get_parent(sc->dev);
2603 	device_t gpdev = device_get_parent(pdev);
2604 	device_t *children;
2605 	int rc, i, lcap, lctl, lsta, nchildren;
2606 	uint32_t v;
2607 
2608 	rc = pci_find_cap(gpdev, PCIY_EXPRESS, &v);
2609 	if (rc != 0) {
2610 		CH_ERR(sc, "%s: pci_find_cap(%s, pcie) failed: %d\n", __func__,
2611 		    device_get_nameunit(gpdev), rc);
2612 		return (ENOTSUP);
2613 	}
2614 	lcap = v + PCIER_LINK_CAP;
2615 	lctl = v + PCIER_LINK_CTL;
2616 	lsta = v + PCIER_LINK_STA;
2617 
2618 	nchildren = 0;
2619 	device_get_children(pdev, &children, &nchildren);
2620 	for (i = 0; i < nchildren; i++)
2621 		pci_save_state(children[i]);
2622 	v = pci_read_config(gpdev, lctl, 2);
2623 	pci_write_config(gpdev, lctl, v | PCIEM_LINK_CTL_LINK_DIS, 2);
2624 	pause("pcie_lnk1", 100 * hz / 1000);	/* 100ms */
2625 	pci_write_config(gpdev, lctl, v | PCIEM_LINK_CTL_RETRAIN_LINK, 2);
2626 	pause("pcie_lnk2", hz);		/* Wait 1s before restore_state. */
2627 	v = pci_read_config(gpdev, lsta, 2);
2628 	if (pci_read_config(gpdev, lcap, 2) & PCIEM_LINK_CAP_DL_ACTIVE)
2629 		rc = v & PCIEM_LINK_STA_DL_ACTIVE ? 0 : ETIMEDOUT;
2630 	else if (v & (PCIEM_LINK_STA_TRAINING_ERROR | PCIEM_LINK_STA_TRAINING))
2631 		rc = ETIMEDOUT;
2632 	else
2633 		rc = 0;
2634 	if (rc != 0)
2635 		CH_ERR(sc, "%s: PCIe link is down after reset, LINK_STA 0x%x\n",
2636 		    __func__, v);
2637 	else {
2638 		for (i = 0; i < nchildren; i++)
2639 			pci_restore_state(children[i]);
2640 	}
2641 	free(children, M_TEMP);
2642 
2643 	return (rc);
2644 }
2645 
2646 static inline int
reset_adapter(struct adapter * sc)2647 reset_adapter(struct adapter *sc)
2648 {
2649 	int rc;
2650 	const int reset_method = vm_guest == VM_GUEST_NO ? t4_reset_method : 0;
2651 
2652 	rc = suspend_adapter(sc);
2653 	if (rc != 0)
2654 		return (rc);
2655 
2656 	switch (reset_method) {
2657 	case 1:
2658 		rc = reset_adapter_with_pcie_sbr(sc);
2659 		break;
2660 	case 2:
2661 		rc = reset_adapter_with_pcie_link_bounce(sc);
2662 		break;
2663 	case 0:
2664 	default:
2665 		rc = reset_adapter_with_pl_rst(sc);
2666 		break;
2667 	}
2668 	if (rc == 0)
2669 		rc = resume_adapter(sc);
2670 	return (rc);
2671 }
2672 
2673 static void
reset_adapter_task(void * arg,int pending)2674 reset_adapter_task(void *arg, int pending)
2675 {
2676 	struct adapter *sc = arg;
2677 	const int flags = sc->flags;
2678 	const int eflags = sc->error_flags;
2679 	int rc;
2680 
2681 	if (pending > 1)
2682 		CH_ALERT(sc, "%s: pending %d\n", __func__, pending);
2683 	rc = reset_adapter(sc);
2684 	if (rc != 0) {
2685 		CH_ERR(sc, "adapter did not reset properly, rc = %d, "
2686 		       "flags 0x%08x -> 0x%08x, err_flags 0x%08x -> 0x%08x.\n",
2687 		       rc, flags, sc->flags, eflags, sc->error_flags);
2688 	}
2689 }
2690 
2691 static int
cxgbe_probe(device_t dev)2692 cxgbe_probe(device_t dev)
2693 {
2694 	struct port_info *pi = device_get_softc(dev);
2695 
2696 	device_set_descf(dev, "port %d", pi->port_id);
2697 
2698 	return (BUS_PROBE_DEFAULT);
2699 }
2700 
2701 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2702     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2703     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2704     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2705 #define T4_CAP_ENABLE (T4_CAP)
2706 
2707 static void
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2708 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2709 {
2710 	if_t ifp;
2711 	struct sbuf *sb;
2712 	struct sysctl_ctx_list *ctx = &vi->ctx;
2713 	struct sysctl_oid_list *children;
2714 	struct pfil_head_args pa;
2715 	struct adapter *sc = vi->adapter;
2716 
2717 	sysctl_ctx_init(ctx);
2718 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2719 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2720 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2721 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2722 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2723 #ifdef DEV_NETMAP
2724 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2725 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2726 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2727 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2728 #endif
2729 #ifdef TCP_OFFLOAD
2730 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2731 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2732 #endif
2733 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2734 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2735 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2736 #endif
2737 
2738 	vi->xact_addr_filt = -1;
2739 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2740 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2741 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2742 		vi->flags |= TX_USES_VM_WR;
2743 
2744 	/* Allocate an ifnet and set it up */
2745 	ifp = if_alloc_dev(IFT_ETHER, dev);
2746 	vi->ifp = ifp;
2747 	if_setsoftc(ifp, vi);
2748 
2749 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2750 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2751 
2752 	if_setinitfn(ifp, cxgbe_init);
2753 	if_setioctlfn(ifp, cxgbe_ioctl);
2754 	if_settransmitfn(ifp, cxgbe_transmit);
2755 	if_setqflushfn(ifp, cxgbe_qflush);
2756 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2757 		if_setgetcounterfn(ifp, vi_get_counter);
2758 	else
2759 		if_setgetcounterfn(ifp, cxgbe_get_counter);
2760 #if defined(KERN_TLS) || defined(RATELIMIT)
2761 	if_setsndtagallocfn(ifp, cxgbe_snd_tag_alloc);
2762 #endif
2763 #ifdef RATELIMIT
2764 	if_setratelimitqueryfn(ifp, cxgbe_ratelimit_query);
2765 #endif
2766 
2767 	if_setcapabilities(ifp, T4_CAP);
2768 	if_setcapenable(ifp, T4_CAP_ENABLE);
2769 	if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2770 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2771 	if (chip_id(sc) >= CHELSIO_T6) {
2772 		if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2773 		if_setcapenablebit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2774 		if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2775 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2776 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN, 0);
2777 	}
2778 
2779 #ifdef TCP_OFFLOAD
2780 	if (vi->nofldrxq != 0)
2781 		if_setcapabilitiesbit(ifp, IFCAP_TOE, 0);
2782 #endif
2783 #ifdef RATELIMIT
2784 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2785 		if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT, 0);
2786 		if_setcapenablebit(ifp, IFCAP_TXRTLMT, 0);
2787 	}
2788 #endif
2789 
2790 	if_sethwtsomax(ifp, IP_MAXPACKET);
2791 	if (vi->flags & TX_USES_VM_WR)
2792 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_VM_TSO);
2793 	else
2794 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_TSO);
2795 #ifdef RATELIMIT
2796 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2797 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_EO_TSO);
2798 #endif
2799 	if_sethwtsomaxsegsize(ifp, 65536);
2800 #ifdef KERN_TLS
2801 	if (is_ktls(sc)) {
2802 		if_setcapabilitiesbit(ifp, IFCAP_TXTLS, 0);
2803 		if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2804 			if_setcapenablebit(ifp, IFCAP_TXTLS, 0);
2805 	}
2806 #endif
2807 
2808 	ether_ifattach(ifp, vi->hw_addr);
2809 #ifdef DEV_NETMAP
2810 	if (vi->nnmrxq != 0)
2811 		cxgbe_nm_attach(vi);
2812 #endif
2813 	sb = sbuf_new_auto();
2814 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2815 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2816 	switch (if_getcapabilities(ifp) & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2817 	case IFCAP_TOE:
2818 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2819 		break;
2820 	case IFCAP_TOE | IFCAP_TXRTLMT:
2821 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2822 		break;
2823 	case IFCAP_TXRTLMT:
2824 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2825 		break;
2826 	}
2827 #endif
2828 #ifdef TCP_OFFLOAD
2829 	if (if_getcapabilities(ifp) & IFCAP_TOE)
2830 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2831 #endif
2832 #ifdef DEV_NETMAP
2833 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2834 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2835 		    vi->nnmtxq, vi->nnmrxq);
2836 #endif
2837 	sbuf_finish(sb);
2838 	device_printf(dev, "%s\n", sbuf_data(sb));
2839 	sbuf_delete(sb);
2840 
2841 	vi_sysctls(vi);
2842 
2843 	pa.pa_version = PFIL_VERSION;
2844 	pa.pa_flags = PFIL_IN;
2845 	pa.pa_type = PFIL_TYPE_ETHERNET;
2846 	pa.pa_headname = if_name(ifp);
2847 	vi->pfil = pfil_head_register(&pa);
2848 }
2849 
2850 static int
cxgbe_attach(device_t dev)2851 cxgbe_attach(device_t dev)
2852 {
2853 	struct port_info *pi = device_get_softc(dev);
2854 	struct adapter *sc = pi->adapter;
2855 	struct vi_info *vi;
2856 	int i;
2857 
2858 	sysctl_ctx_init(&pi->ctx);
2859 
2860 	cxgbe_vi_attach(dev, &pi->vi[0]);
2861 
2862 	for_each_vi(pi, i, vi) {
2863 		if (i == 0)
2864 			continue;
2865 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, DEVICE_UNIT_ANY);
2866 		if (vi->dev == NULL) {
2867 			device_printf(dev, "failed to add VI %d\n", i);
2868 			continue;
2869 		}
2870 		device_set_softc(vi->dev, vi);
2871 	}
2872 
2873 	cxgbe_sysctls(pi);
2874 
2875 	bus_attach_children(dev);
2876 
2877 	return (0);
2878 }
2879 
2880 static void
cxgbe_vi_detach(struct vi_info * vi)2881 cxgbe_vi_detach(struct vi_info *vi)
2882 {
2883 	if_t ifp = vi->ifp;
2884 
2885 	if (vi->pfil != NULL) {
2886 		pfil_head_unregister(vi->pfil);
2887 		vi->pfil = NULL;
2888 	}
2889 
2890 	ether_ifdetach(ifp);
2891 
2892 	/* Let detach proceed even if these fail. */
2893 #ifdef DEV_NETMAP
2894 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2895 		cxgbe_nm_detach(vi);
2896 #endif
2897 	cxgbe_uninit_synchronized(vi);
2898 	callout_drain(&vi->tick);
2899 	mtx_destroy(&vi->tick_mtx);
2900 	sysctl_ctx_free(&vi->ctx);
2901 	vi_full_uninit(vi);
2902 
2903 	if_free(vi->ifp);
2904 	vi->ifp = NULL;
2905 }
2906 
2907 static int
cxgbe_detach(device_t dev)2908 cxgbe_detach(device_t dev)
2909 {
2910 	struct port_info *pi = device_get_softc(dev);
2911 	struct adapter *sc = pi->adapter;
2912 	int rc;
2913 
2914 	/* Detach the extra VIs first. */
2915 	rc = bus_generic_detach(dev);
2916 	if (rc)
2917 		return (rc);
2918 
2919 	sysctl_ctx_free(&pi->ctx);
2920 	begin_vi_detach(sc, &pi->vi[0]);
2921 	if (pi->flags & HAS_TRACEQ) {
2922 		sc->traceq = -1;	/* cloner should not create ifnet */
2923 		t4_tracer_port_detach(sc);
2924 	}
2925 	cxgbe_vi_detach(&pi->vi[0]);
2926 	ifmedia_removeall(&pi->media);
2927 	end_vi_detach(sc, &pi->vi[0]);
2928 
2929 	return (0);
2930 }
2931 
2932 static void
cxgbe_init(void * arg)2933 cxgbe_init(void *arg)
2934 {
2935 	struct vi_info *vi = arg;
2936 	struct adapter *sc = vi->adapter;
2937 
2938 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
2939 		return;
2940 	cxgbe_init_synchronized(vi);
2941 	end_synchronized_op(sc, 0);
2942 }
2943 
2944 static int
cxgbe_ioctl(if_t ifp,unsigned long cmd,caddr_t data)2945 cxgbe_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
2946 {
2947 	int rc = 0, mtu, flags;
2948 	struct vi_info *vi = if_getsoftc(ifp);
2949 	struct port_info *pi = vi->pi;
2950 	struct adapter *sc = pi->adapter;
2951 	struct ifreq *ifr = (struct ifreq *)data;
2952 	uint32_t mask;
2953 
2954 	switch (cmd) {
2955 	case SIOCSIFMTU:
2956 		mtu = ifr->ifr_mtu;
2957 		if (mtu < ETHERMIN || mtu > MAX_MTU)
2958 			return (EINVAL);
2959 
2960 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
2961 		if (rc)
2962 			return (rc);
2963 		if_setmtu(ifp, mtu);
2964 		if (vi->flags & VI_INIT_DONE) {
2965 			t4_update_fl_bufsize(ifp);
2966 			if (hw_all_ok(sc) &&
2967 			    if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2968 				rc = update_mac_settings(ifp, XGMAC_MTU);
2969 		}
2970 		end_synchronized_op(sc, 0);
2971 		break;
2972 
2973 	case SIOCSIFFLAGS:
2974 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
2975 		if (rc)
2976 			return (rc);
2977 
2978 		if (!hw_all_ok(sc)) {
2979 			rc = ENXIO;
2980 			goto fail;
2981 		}
2982 
2983 		if (if_getflags(ifp) & IFF_UP) {
2984 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2985 				flags = vi->if_flags;
2986 				if ((if_getflags(ifp) ^ flags) &
2987 				    (IFF_PROMISC | IFF_ALLMULTI)) {
2988 					rc = update_mac_settings(ifp,
2989 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
2990 				}
2991 			} else {
2992 				rc = cxgbe_init_synchronized(vi);
2993 			}
2994 			vi->if_flags = if_getflags(ifp);
2995 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2996 			rc = cxgbe_uninit_synchronized(vi);
2997 		}
2998 		end_synchronized_op(sc, 0);
2999 		break;
3000 
3001 	case SIOCADDMULTI:
3002 	case SIOCDELMULTI:
3003 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
3004 		if (rc)
3005 			return (rc);
3006 		if (hw_all_ok(sc) && if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3007 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
3008 		end_synchronized_op(sc, 0);
3009 		break;
3010 
3011 	case SIOCSIFCAP:
3012 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
3013 		if (rc)
3014 			return (rc);
3015 
3016 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
3017 		if (mask & IFCAP_TXCSUM) {
3018 			if_togglecapenable(ifp, IFCAP_TXCSUM);
3019 			if_togglehwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP);
3020 
3021 			if (IFCAP_TSO4 & if_getcapenable(ifp) &&
3022 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3023 				mask &= ~IFCAP_TSO4;
3024 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
3025 				if_printf(ifp,
3026 				    "tso4 disabled due to -txcsum.\n");
3027 			}
3028 		}
3029 		if (mask & IFCAP_TXCSUM_IPV6) {
3030 			if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
3031 			if_togglehwassist(ifp, CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
3032 
3033 			if (IFCAP_TSO6 & if_getcapenable(ifp) &&
3034 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3035 				mask &= ~IFCAP_TSO6;
3036 				if_setcapenablebit(ifp, 0, IFCAP_TSO6);
3037 				if_printf(ifp,
3038 				    "tso6 disabled due to -txcsum6.\n");
3039 			}
3040 		}
3041 		if (mask & IFCAP_RXCSUM)
3042 			if_togglecapenable(ifp, IFCAP_RXCSUM);
3043 		if (mask & IFCAP_RXCSUM_IPV6)
3044 			if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
3045 
3046 		/*
3047 		 * Note that we leave CSUM_TSO alone (it is always set).  The
3048 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
3049 		 * sending a TSO request our way, so it's sufficient to toggle
3050 		 * IFCAP_TSOx only.
3051 		 */
3052 		if (mask & IFCAP_TSO4) {
3053 			if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
3054 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3055 				if_printf(ifp, "enable txcsum first.\n");
3056 				rc = EAGAIN;
3057 				goto fail;
3058 			}
3059 			if_togglecapenable(ifp, IFCAP_TSO4);
3060 		}
3061 		if (mask & IFCAP_TSO6) {
3062 			if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
3063 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3064 				if_printf(ifp, "enable txcsum6 first.\n");
3065 				rc = EAGAIN;
3066 				goto fail;
3067 			}
3068 			if_togglecapenable(ifp, IFCAP_TSO6);
3069 		}
3070 		if (mask & IFCAP_LRO) {
3071 #if defined(INET) || defined(INET6)
3072 			int i;
3073 			struct sge_rxq *rxq;
3074 
3075 			if_togglecapenable(ifp, IFCAP_LRO);
3076 			for_each_rxq(vi, i, rxq) {
3077 				if (if_getcapenable(ifp) & IFCAP_LRO)
3078 					rxq->iq.flags |= IQ_LRO_ENABLED;
3079 				else
3080 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
3081 			}
3082 #endif
3083 		}
3084 #ifdef TCP_OFFLOAD
3085 		if (mask & IFCAP_TOE) {
3086 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TOE;
3087 
3088 			rc = toe_capability(vi, enable);
3089 			if (rc != 0)
3090 				goto fail;
3091 
3092 			if_togglecapenable(ifp, mask);
3093 		}
3094 #endif
3095 		if (mask & IFCAP_VLAN_HWTAGGING) {
3096 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3097 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3098 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
3099 		}
3100 		if (mask & IFCAP_VLAN_MTU) {
3101 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
3102 
3103 			/* Need to find out how to disable auto-mtu-inflation */
3104 		}
3105 		if (mask & IFCAP_VLAN_HWTSO)
3106 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3107 		if (mask & IFCAP_VLAN_HWCSUM)
3108 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
3109 #ifdef RATELIMIT
3110 		if (mask & IFCAP_TXRTLMT)
3111 			if_togglecapenable(ifp, IFCAP_TXRTLMT);
3112 #endif
3113 		if (mask & IFCAP_HWRXTSTMP) {
3114 			int i;
3115 			struct sge_rxq *rxq;
3116 
3117 			if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3118 			for_each_rxq(vi, i, rxq) {
3119 				if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3120 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
3121 				else
3122 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
3123 			}
3124 		}
3125 		if (mask & IFCAP_MEXTPG)
3126 			if_togglecapenable(ifp, IFCAP_MEXTPG);
3127 
3128 #ifdef KERN_TLS
3129 		if (mask & IFCAP_TXTLS) {
3130 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TXTLS;
3131 
3132 			rc = ktls_capability(sc, enable);
3133 			if (rc != 0)
3134 				goto fail;
3135 
3136 			if_togglecapenable(ifp, mask & IFCAP_TXTLS);
3137 		}
3138 #endif
3139 		if (mask & IFCAP_VXLAN_HWCSUM) {
3140 			if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3141 			if_togglehwassist(ifp, CSUM_INNER_IP6_UDP |
3142 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
3143 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP);
3144 		}
3145 		if (mask & IFCAP_VXLAN_HWTSO) {
3146 			if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3147 			if_togglehwassist(ifp, CSUM_INNER_IP6_TSO |
3148 			    CSUM_INNER_IP_TSO);
3149 		}
3150 
3151 #ifdef VLAN_CAPABILITIES
3152 		VLAN_CAPABILITIES(ifp);
3153 #endif
3154 fail:
3155 		end_synchronized_op(sc, 0);
3156 		break;
3157 
3158 	case SIOCSIFMEDIA:
3159 	case SIOCGIFMEDIA:
3160 	case SIOCGIFXMEDIA:
3161 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3162 		break;
3163 
3164 	case SIOCGI2C: {
3165 		struct ifi2creq i2c;
3166 
3167 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3168 		if (rc != 0)
3169 			break;
3170 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3171 			rc = EPERM;
3172 			break;
3173 		}
3174 		if (i2c.len > sizeof(i2c.data)) {
3175 			rc = EINVAL;
3176 			break;
3177 		}
3178 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3179 		if (rc)
3180 			return (rc);
3181 		if (!hw_all_ok(sc))
3182 			rc = ENXIO;
3183 		else
3184 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3185 			    i2c.offset, i2c.len, &i2c.data[0]);
3186 		end_synchronized_op(sc, 0);
3187 		if (rc == 0)
3188 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3189 		break;
3190 	}
3191 
3192 	default:
3193 		rc = ether_ioctl(ifp, cmd, data);
3194 	}
3195 
3196 	return (rc);
3197 }
3198 
3199 static int
cxgbe_transmit(if_t ifp,struct mbuf * m)3200 cxgbe_transmit(if_t ifp, struct mbuf *m)
3201 {
3202 	struct vi_info *vi = if_getsoftc(ifp);
3203 	struct port_info *pi = vi->pi;
3204 	struct adapter *sc;
3205 	struct sge_txq *txq;
3206 	void *items[1];
3207 	int rc;
3208 
3209 	M_ASSERTPKTHDR(m);
3210 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
3211 #if defined(KERN_TLS) || defined(RATELIMIT)
3212 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3213 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3214 #endif
3215 
3216 	if (__predict_false(pi->link_cfg.link_ok == false)) {
3217 		m_freem(m);
3218 		return (ENETDOWN);
3219 	}
3220 
3221 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3222 	if (__predict_false(rc != 0)) {
3223 		if (__predict_true(rc == EINPROGRESS)) {
3224 			/* queued by parse_pkt */
3225 			MPASS(m != NULL);
3226 			return (0);
3227 		}
3228 
3229 		MPASS(m == NULL);			/* was freed already */
3230 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
3231 		return (rc);
3232 	}
3233 
3234 	/* Select a txq. */
3235 	sc = vi->adapter;
3236 	txq = &sc->sge.txq[vi->first_txq];
3237 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3238 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3239 		    vi->rsrv_noflowq);
3240 
3241 	items[0] = m;
3242 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3243 	if (__predict_false(rc != 0))
3244 		m_freem(m);
3245 
3246 	return (rc);
3247 }
3248 
3249 static void
cxgbe_qflush(if_t ifp)3250 cxgbe_qflush(if_t ifp)
3251 {
3252 	struct vi_info *vi = if_getsoftc(ifp);
3253 	struct sge_txq *txq;
3254 	int i;
3255 
3256 	/* queues do not exist if !VI_INIT_DONE. */
3257 	if (vi->flags & VI_INIT_DONE) {
3258 		for_each_txq(vi, i, txq) {
3259 			TXQ_LOCK(txq);
3260 			txq->eq.flags |= EQ_QFLUSH;
3261 			TXQ_UNLOCK(txq);
3262 			while (!mp_ring_is_idle(txq->r)) {
3263 				mp_ring_check_drainage(txq->r, 4096);
3264 				pause("qflush", 1);
3265 			}
3266 			TXQ_LOCK(txq);
3267 			txq->eq.flags &= ~EQ_QFLUSH;
3268 			TXQ_UNLOCK(txq);
3269 		}
3270 	}
3271 	if_qflush(ifp);
3272 }
3273 
3274 static uint64_t
vi_get_counter(if_t ifp,ift_counter c)3275 vi_get_counter(if_t ifp, ift_counter c)
3276 {
3277 	struct vi_info *vi = if_getsoftc(ifp);
3278 	struct fw_vi_stats_vf *s = &vi->stats;
3279 
3280 	mtx_lock(&vi->tick_mtx);
3281 	vi_refresh_stats(vi);
3282 	mtx_unlock(&vi->tick_mtx);
3283 
3284 	switch (c) {
3285 	case IFCOUNTER_IPACKETS:
3286 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3287 		    s->rx_ucast_frames);
3288 	case IFCOUNTER_IERRORS:
3289 		return (s->rx_err_frames);
3290 	case IFCOUNTER_OPACKETS:
3291 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3292 		    s->tx_ucast_frames + s->tx_offload_frames);
3293 	case IFCOUNTER_OERRORS:
3294 		return (s->tx_drop_frames);
3295 	case IFCOUNTER_IBYTES:
3296 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3297 		    s->rx_ucast_bytes);
3298 	case IFCOUNTER_OBYTES:
3299 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3300 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3301 	case IFCOUNTER_IMCASTS:
3302 		return (s->rx_mcast_frames);
3303 	case IFCOUNTER_OMCASTS:
3304 		return (s->tx_mcast_frames);
3305 	case IFCOUNTER_OQDROPS: {
3306 		uint64_t drops;
3307 
3308 		drops = 0;
3309 		if (vi->flags & VI_INIT_DONE) {
3310 			int i;
3311 			struct sge_txq *txq;
3312 
3313 			for_each_txq(vi, i, txq)
3314 				drops += counter_u64_fetch(txq->r->dropped);
3315 		}
3316 
3317 		return (drops);
3318 
3319 	}
3320 
3321 	default:
3322 		return (if_get_counter_default(ifp, c));
3323 	}
3324 }
3325 
3326 static uint64_t
cxgbe_get_counter(if_t ifp,ift_counter c)3327 cxgbe_get_counter(if_t ifp, ift_counter c)
3328 {
3329 	struct vi_info *vi = if_getsoftc(ifp);
3330 	struct port_info *pi = vi->pi;
3331 	struct port_stats *s = &pi->stats;
3332 
3333 	mtx_lock(&vi->tick_mtx);
3334 	cxgbe_refresh_stats(vi);
3335 	mtx_unlock(&vi->tick_mtx);
3336 
3337 	switch (c) {
3338 	case IFCOUNTER_IPACKETS:
3339 		return (s->rx_frames);
3340 
3341 	case IFCOUNTER_IERRORS:
3342 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3343 		    s->rx_fcs_err + s->rx_len_err);
3344 
3345 	case IFCOUNTER_OPACKETS:
3346 		return (s->tx_frames);
3347 
3348 	case IFCOUNTER_OERRORS:
3349 		return (s->tx_error_frames);
3350 
3351 	case IFCOUNTER_IBYTES:
3352 		return (s->rx_octets);
3353 
3354 	case IFCOUNTER_OBYTES:
3355 		return (s->tx_octets);
3356 
3357 	case IFCOUNTER_IMCASTS:
3358 		return (s->rx_mcast_frames);
3359 
3360 	case IFCOUNTER_OMCASTS:
3361 		return (s->tx_mcast_frames);
3362 
3363 	case IFCOUNTER_IQDROPS:
3364 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3365 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3366 		    s->rx_trunc3 + pi->tnl_cong_drops);
3367 
3368 	case IFCOUNTER_OQDROPS: {
3369 		uint64_t drops;
3370 
3371 		drops = s->tx_drop;
3372 		if (vi->flags & VI_INIT_DONE) {
3373 			int i;
3374 			struct sge_txq *txq;
3375 
3376 			for_each_txq(vi, i, txq)
3377 				drops += counter_u64_fetch(txq->r->dropped);
3378 		}
3379 
3380 		return (drops);
3381 
3382 	}
3383 
3384 	default:
3385 		return (if_get_counter_default(ifp, c));
3386 	}
3387 }
3388 
3389 #if defined(KERN_TLS) || defined(RATELIMIT)
3390 static int
cxgbe_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3391 cxgbe_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3392     struct m_snd_tag **pt)
3393 {
3394 	int error;
3395 
3396 	switch (params->hdr.type) {
3397 #ifdef RATELIMIT
3398 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3399 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3400 		break;
3401 #endif
3402 #ifdef KERN_TLS
3403 	case IF_SND_TAG_TYPE_TLS:
3404 	{
3405 		struct vi_info *vi = if_getsoftc(ifp);
3406 
3407 		if (is_t6(vi->pi->adapter))
3408 			error = t6_tls_tag_alloc(ifp, params, pt);
3409 		else
3410 			error = EOPNOTSUPP;
3411 		break;
3412 	}
3413 #endif
3414 	default:
3415 		error = EOPNOTSUPP;
3416 	}
3417 	return (error);
3418 }
3419 #endif
3420 
3421 /*
3422  * The kernel picks a media from the list we had provided but we still validate
3423  * the requeste.
3424  */
3425 int
cxgbe_media_change(if_t ifp)3426 cxgbe_media_change(if_t ifp)
3427 {
3428 	struct vi_info *vi = if_getsoftc(ifp);
3429 	struct port_info *pi = vi->pi;
3430 	struct ifmedia *ifm = &pi->media;
3431 	struct link_config *lc = &pi->link_cfg;
3432 	struct adapter *sc = pi->adapter;
3433 	int rc;
3434 
3435 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3436 	if (rc != 0)
3437 		return (rc);
3438 	PORT_LOCK(pi);
3439 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3440 		/* ifconfig .. media autoselect */
3441 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3442 			rc = ENOTSUP; /* AN not supported by transceiver */
3443 			goto done;
3444 		}
3445 		lc->requested_aneg = AUTONEG_ENABLE;
3446 		lc->requested_speed = 0;
3447 		lc->requested_fc |= PAUSE_AUTONEG;
3448 	} else {
3449 		lc->requested_aneg = AUTONEG_DISABLE;
3450 		lc->requested_speed =
3451 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3452 		lc->requested_fc = 0;
3453 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3454 			lc->requested_fc |= PAUSE_RX;
3455 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3456 			lc->requested_fc |= PAUSE_TX;
3457 	}
3458 	if (pi->up_vis > 0 && hw_all_ok(sc)) {
3459 		fixup_link_config(pi);
3460 		rc = apply_link_config(pi);
3461 	}
3462 done:
3463 	PORT_UNLOCK(pi);
3464 	end_synchronized_op(sc, 0);
3465 	return (rc);
3466 }
3467 
3468 /*
3469  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3470  * given speed.
3471  */
3472 static int
port_mword(struct port_info * pi,uint32_t speed)3473 port_mword(struct port_info *pi, uint32_t speed)
3474 {
3475 
3476 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3477 	MPASS(powerof2(speed));
3478 
3479 	switch(pi->port_type) {
3480 	case FW_PORT_TYPE_BT_SGMII:
3481 	case FW_PORT_TYPE_BT_XFI:
3482 	case FW_PORT_TYPE_BT_XAUI:
3483 		/* BaseT */
3484 		switch (speed) {
3485 		case FW_PORT_CAP32_SPEED_100M:
3486 			return (IFM_100_T);
3487 		case FW_PORT_CAP32_SPEED_1G:
3488 			return (IFM_1000_T);
3489 		case FW_PORT_CAP32_SPEED_10G:
3490 			return (IFM_10G_T);
3491 		}
3492 		break;
3493 	case FW_PORT_TYPE_KX4:
3494 		if (speed == FW_PORT_CAP32_SPEED_10G)
3495 			return (IFM_10G_KX4);
3496 		break;
3497 	case FW_PORT_TYPE_CX4:
3498 		if (speed == FW_PORT_CAP32_SPEED_10G)
3499 			return (IFM_10G_CX4);
3500 		break;
3501 	case FW_PORT_TYPE_KX:
3502 		if (speed == FW_PORT_CAP32_SPEED_1G)
3503 			return (IFM_1000_KX);
3504 		break;
3505 	case FW_PORT_TYPE_KR:
3506 	case FW_PORT_TYPE_BP_AP:
3507 	case FW_PORT_TYPE_BP4_AP:
3508 	case FW_PORT_TYPE_BP40_BA:
3509 	case FW_PORT_TYPE_KR4_100G:
3510 	case FW_PORT_TYPE_KR_SFP28:
3511 	case FW_PORT_TYPE_KR_XLAUI:
3512 		switch (speed) {
3513 		case FW_PORT_CAP32_SPEED_1G:
3514 			return (IFM_1000_KX);
3515 		case FW_PORT_CAP32_SPEED_10G:
3516 			return (IFM_10G_KR);
3517 		case FW_PORT_CAP32_SPEED_25G:
3518 			return (IFM_25G_KR);
3519 		case FW_PORT_CAP32_SPEED_40G:
3520 			return (IFM_40G_KR4);
3521 		case FW_PORT_CAP32_SPEED_50G:
3522 			return (IFM_50G_KR2);
3523 		case FW_PORT_CAP32_SPEED_100G:
3524 			return (IFM_100G_KR4);
3525 		}
3526 		break;
3527 	case FW_PORT_TYPE_FIBER_XFI:
3528 	case FW_PORT_TYPE_FIBER_XAUI:
3529 	case FW_PORT_TYPE_SFP:
3530 	case FW_PORT_TYPE_QSFP_10G:
3531 	case FW_PORT_TYPE_QSA:
3532 	case FW_PORT_TYPE_QSFP:
3533 	case FW_PORT_TYPE_CR4_QSFP:
3534 	case FW_PORT_TYPE_CR_QSFP:
3535 	case FW_PORT_TYPE_CR2_QSFP:
3536 	case FW_PORT_TYPE_SFP28:
3537 		/* Pluggable transceiver */
3538 		switch (pi->mod_type) {
3539 		case FW_PORT_MOD_TYPE_LR:
3540 		case FW_PORT_MOD_TYPE_LR_SIMPLEX:
3541 			switch (speed) {
3542 			case FW_PORT_CAP32_SPEED_1G:
3543 				return (IFM_1000_LX);
3544 			case FW_PORT_CAP32_SPEED_10G:
3545 				return (IFM_10G_LR);
3546 			case FW_PORT_CAP32_SPEED_25G:
3547 				return (IFM_25G_LR);
3548 			case FW_PORT_CAP32_SPEED_40G:
3549 				return (IFM_40G_LR4);
3550 			case FW_PORT_CAP32_SPEED_50G:
3551 				return (IFM_50G_LR2);
3552 			case FW_PORT_CAP32_SPEED_100G:
3553 				return (IFM_100G_LR4);
3554 			}
3555 			break;
3556 		case FW_PORT_MOD_TYPE_SR:
3557 			switch (speed) {
3558 			case FW_PORT_CAP32_SPEED_1G:
3559 				return (IFM_1000_SX);
3560 			case FW_PORT_CAP32_SPEED_10G:
3561 				return (IFM_10G_SR);
3562 			case FW_PORT_CAP32_SPEED_25G:
3563 				return (IFM_25G_SR);
3564 			case FW_PORT_CAP32_SPEED_40G:
3565 				return (IFM_40G_SR4);
3566 			case FW_PORT_CAP32_SPEED_50G:
3567 				return (IFM_50G_SR2);
3568 			case FW_PORT_CAP32_SPEED_100G:
3569 				return (IFM_100G_SR4);
3570 			}
3571 			break;
3572 		case FW_PORT_MOD_TYPE_ER:
3573 			if (speed == FW_PORT_CAP32_SPEED_10G)
3574 				return (IFM_10G_ER);
3575 			break;
3576 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3577 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3578 			switch (speed) {
3579 			case FW_PORT_CAP32_SPEED_1G:
3580 				return (IFM_1000_CX);
3581 			case FW_PORT_CAP32_SPEED_10G:
3582 				return (IFM_10G_TWINAX);
3583 			case FW_PORT_CAP32_SPEED_25G:
3584 				return (IFM_25G_CR);
3585 			case FW_PORT_CAP32_SPEED_40G:
3586 				return (IFM_40G_CR4);
3587 			case FW_PORT_CAP32_SPEED_50G:
3588 				return (IFM_50G_CR2);
3589 			case FW_PORT_CAP32_SPEED_100G:
3590 				return (IFM_100G_CR4);
3591 			}
3592 			break;
3593 		case FW_PORT_MOD_TYPE_LRM:
3594 			if (speed == FW_PORT_CAP32_SPEED_10G)
3595 				return (IFM_10G_LRM);
3596 			break;
3597 		case FW_PORT_MOD_TYPE_DR:
3598 			if (speed == FW_PORT_CAP32_SPEED_100G)
3599 				return (IFM_100G_DR);
3600 			break;
3601 		case FW_PORT_MOD_TYPE_NA:
3602 			MPASS(0);	/* Not pluggable? */
3603 			/* fall throough */
3604 		case FW_PORT_MOD_TYPE_ERROR:
3605 		case FW_PORT_MOD_TYPE_UNKNOWN:
3606 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3607 			break;
3608 		case FW_PORT_MOD_TYPE_NONE:
3609 			return (IFM_NONE);
3610 		}
3611 		break;
3612 	case FW_PORT_TYPE_NONE:
3613 		return (IFM_NONE);
3614 	}
3615 
3616 	return (IFM_UNKNOWN);
3617 }
3618 
3619 void
cxgbe_media_status(if_t ifp,struct ifmediareq * ifmr)3620 cxgbe_media_status(if_t ifp, struct ifmediareq *ifmr)
3621 {
3622 	struct vi_info *vi = if_getsoftc(ifp);
3623 	struct port_info *pi = vi->pi;
3624 	struct adapter *sc = pi->adapter;
3625 	struct link_config *lc = &pi->link_cfg;
3626 
3627 	if (begin_synchronized_op(sc, vi , SLEEP_OK | INTR_OK, "t4med") != 0)
3628 		return;
3629 	PORT_LOCK(pi);
3630 
3631 	if (pi->up_vis == 0 && hw_all_ok(sc)) {
3632 		/*
3633 		 * If all the interfaces are administratively down the firmware
3634 		 * does not report transceiver changes.  Refresh port info here
3635 		 * so that ifconfig displays accurate ifmedia at all times.
3636 		 * This is the only reason we have a synchronized op in this
3637 		 * function.  Just PORT_LOCK would have been enough otherwise.
3638 		 */
3639 		t4_update_port_info(pi);
3640 		build_medialist(pi);
3641 	}
3642 
3643 	/* ifm_status */
3644 	ifmr->ifm_status = IFM_AVALID;
3645 	if (lc->link_ok == false)
3646 		goto done;
3647 	ifmr->ifm_status |= IFM_ACTIVE;
3648 
3649 	/* ifm_active */
3650 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3651 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3652 	if (lc->fc & PAUSE_RX)
3653 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3654 	if (lc->fc & PAUSE_TX)
3655 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3656 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3657 done:
3658 	PORT_UNLOCK(pi);
3659 	end_synchronized_op(sc, 0);
3660 }
3661 
3662 static int
vcxgbe_probe(device_t dev)3663 vcxgbe_probe(device_t dev)
3664 {
3665 	struct vi_info *vi = device_get_softc(dev);
3666 
3667 	device_set_descf(dev, "port %d vi %td", vi->pi->port_id,
3668 	    vi - vi->pi->vi);
3669 
3670 	return (BUS_PROBE_DEFAULT);
3671 }
3672 
3673 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3674 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3675 {
3676 	int func, index, rc;
3677 	uint32_t param, val;
3678 
3679 	ASSERT_SYNCHRONIZED_OP(sc);
3680 
3681 	index = vi - pi->vi;
3682 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3683 	KASSERT(index < nitems(vi_mac_funcs),
3684 	    ("%s: VI %s doesn't have a MAC func", __func__,
3685 	    device_get_nameunit(vi->dev)));
3686 	func = vi_mac_funcs[index];
3687 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
3688 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3689 	if (rc < 0) {
3690 		CH_ERR(vi, "failed to allocate virtual interface %d"
3691 		    "for port %d: %d\n", index, pi->port_id, -rc);
3692 		return (-rc);
3693 	}
3694 	vi->viid = rc;
3695 
3696 	if (vi->rss_size == 1) {
3697 		/*
3698 		 * This VI didn't get a slice of the RSS table.  Reduce the
3699 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3700 		 * configuration file (nvi, rssnvi for this PF) if this is a
3701 		 * problem.
3702 		 */
3703 		device_printf(vi->dev, "RSS table not available.\n");
3704 		vi->rss_base = 0xffff;
3705 
3706 		return (0);
3707 	}
3708 
3709 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3710 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3711 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3712 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3713 	if (rc)
3714 		vi->rss_base = 0xffff;
3715 	else {
3716 		MPASS((val >> 16) == vi->rss_size);
3717 		vi->rss_base = val & 0xffff;
3718 	}
3719 
3720 	return (0);
3721 }
3722 
3723 static int
vcxgbe_attach(device_t dev)3724 vcxgbe_attach(device_t dev)
3725 {
3726 	struct vi_info *vi;
3727 	struct port_info *pi;
3728 	struct adapter *sc;
3729 	int rc;
3730 
3731 	vi = device_get_softc(dev);
3732 	pi = vi->pi;
3733 	sc = pi->adapter;
3734 
3735 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3736 	if (rc)
3737 		return (rc);
3738 	rc = alloc_extra_vi(sc, pi, vi);
3739 	end_synchronized_op(sc, 0);
3740 	if (rc)
3741 		return (rc);
3742 
3743 	cxgbe_vi_attach(dev, vi);
3744 
3745 	return (0);
3746 }
3747 
3748 static int
vcxgbe_detach(device_t dev)3749 vcxgbe_detach(device_t dev)
3750 {
3751 	struct vi_info *vi;
3752 	struct adapter *sc;
3753 
3754 	vi = device_get_softc(dev);
3755 	sc = vi->adapter;
3756 
3757 	begin_vi_detach(sc, vi);
3758 	cxgbe_vi_detach(vi);
3759 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3760 	end_vi_detach(sc, vi);
3761 
3762 	return (0);
3763 }
3764 
3765 static struct callout fatal_callout;
3766 static struct taskqueue *reset_tq;
3767 
3768 static void
delayed_panic(void * arg)3769 delayed_panic(void *arg)
3770 {
3771 	struct adapter *sc = arg;
3772 
3773 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3774 }
3775 
3776 static void
fatal_error_task(void * arg,int pending)3777 fatal_error_task(void *arg, int pending)
3778 {
3779 	struct adapter *sc = arg;
3780 	int rc;
3781 
3782 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3783 		dump_cim_regs(sc);
3784 		dump_cimla(sc);
3785 		dump_devlog(sc);
3786 	}
3787 
3788 	if (t4_reset_on_fatal_err) {
3789 		CH_ALERT(sc, "resetting adapter after fatal error.\n");
3790 		rc = reset_adapter(sc);
3791 		if (rc == 0 && t4_panic_on_fatal_err) {
3792 			CH_ALERT(sc, "reset was successful, "
3793 			    "system will NOT panic.\n");
3794 			return;
3795 		}
3796 	}
3797 
3798 	if (t4_panic_on_fatal_err) {
3799 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3800 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3801 	}
3802 }
3803 
3804 void
t4_fatal_err(struct adapter * sc,bool fw_error)3805 t4_fatal_err(struct adapter *sc, bool fw_error)
3806 {
3807 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3808 
3809 	stop_adapter(sc);
3810 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3811 		return;
3812 	if (fw_error) {
3813 		/*
3814 		 * We are here because of a firmware error/timeout and not
3815 		 * because of a hardware interrupt.  It is possible (although
3816 		 * not very likely) that an error interrupt was also raised but
3817 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3818 		 * main INT_CAUSE registers here to make sure we haven't missed
3819 		 * anything interesting.
3820 		 */
3821 		t4_slow_intr_handler(sc, verbose);
3822 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3823 	}
3824 	t4_report_fw_error(sc);
3825 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3826 	    device_get_nameunit(sc->dev), fw_error);
3827 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3828 }
3829 
3830 void
t4_add_adapter(struct adapter * sc)3831 t4_add_adapter(struct adapter *sc)
3832 {
3833 	sx_xlock(&t4_list_lock);
3834 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3835 	sx_xunlock(&t4_list_lock);
3836 }
3837 
3838 int
t4_map_bars_0_and_4(struct adapter * sc)3839 t4_map_bars_0_and_4(struct adapter *sc)
3840 {
3841 	sc->regs_rid = PCIR_BAR(0);
3842 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3843 	    &sc->regs_rid, RF_ACTIVE);
3844 	if (sc->regs_res == NULL) {
3845 		device_printf(sc->dev, "cannot map registers.\n");
3846 		return (ENXIO);
3847 	}
3848 	sc->bt = rman_get_bustag(sc->regs_res);
3849 	sc->bh = rman_get_bushandle(sc->regs_res);
3850 	sc->mmio_len = rman_get_size(sc->regs_res);
3851 	setbit(&sc->doorbells, DOORBELL_KDB);
3852 
3853 	sc->msix_rid = PCIR_BAR(4);
3854 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3855 	    &sc->msix_rid, RF_ACTIVE);
3856 	if (sc->msix_res == NULL) {
3857 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3858 		return (ENXIO);
3859 	}
3860 
3861 	return (0);
3862 }
3863 
3864 int
t4_map_bar_2(struct adapter * sc)3865 t4_map_bar_2(struct adapter *sc)
3866 {
3867 
3868 	/*
3869 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
3870 	 * to map it if RDMA is disabled.
3871 	 */
3872 	if (is_t4(sc) && sc->rdmacaps == 0)
3873 		return (0);
3874 
3875 	sc->udbs_rid = PCIR_BAR(2);
3876 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3877 	    &sc->udbs_rid, RF_ACTIVE);
3878 	if (sc->udbs_res == NULL) {
3879 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
3880 		return (ENXIO);
3881 	}
3882 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
3883 
3884 	if (chip_id(sc) >= CHELSIO_T5) {
3885 		setbit(&sc->doorbells, DOORBELL_UDB);
3886 #if defined(__i386__) || defined(__amd64__)
3887 		if (t5_write_combine) {
3888 			int rc, mode;
3889 
3890 			/*
3891 			 * Enable write combining on BAR2.  This is the
3892 			 * userspace doorbell BAR and is split into 128B
3893 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
3894 			 * with an egress queue.  The first 64B has the doorbell
3895 			 * and the second 64B can be used to submit a tx work
3896 			 * request with an implicit doorbell.
3897 			 */
3898 
3899 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
3900 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
3901 			if (rc == 0) {
3902 				clrbit(&sc->doorbells, DOORBELL_UDB);
3903 				setbit(&sc->doorbells, DOORBELL_WCWR);
3904 				setbit(&sc->doorbells, DOORBELL_UDBWC);
3905 			} else {
3906 				device_printf(sc->dev,
3907 				    "couldn't enable write combining: %d\n",
3908 				    rc);
3909 			}
3910 
3911 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
3912 			t4_write_reg(sc, A_SGE_STAT_CFG,
3913 			    V_STATSOURCE_T5(7) | mode);
3914 		}
3915 #endif
3916 	}
3917 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
3918 
3919 	return (0);
3920 }
3921 
3922 int
t4_adj_doorbells(struct adapter * sc)3923 t4_adj_doorbells(struct adapter *sc)
3924 {
3925 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
3926 		sc->doorbells &= t4_doorbells_allowed;
3927 		return (0);
3928 	}
3929 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
3930 	       sc->doorbells, t4_doorbells_allowed);
3931 	return (EINVAL);
3932 }
3933 
3934 struct memwin_init {
3935 	uint32_t base;
3936 	uint32_t aperture;
3937 };
3938 
3939 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
3940 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3941 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3942 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
3943 };
3944 
3945 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
3946 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3947 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3948 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
3949 };
3950 
3951 static void
setup_memwin(struct adapter * sc)3952 setup_memwin(struct adapter *sc)
3953 {
3954 	const struct memwin_init *mw_init;
3955 	struct memwin *mw;
3956 	int i;
3957 	uint32_t bar0;
3958 
3959 	if (is_t4(sc)) {
3960 		/*
3961 		 * Read low 32b of bar0 indirectly via the hardware backdoor
3962 		 * mechanism.  Works from within PCI passthrough environments
3963 		 * too, where rman_get_start() can return a different value.  We
3964 		 * need to program the T4 memory window decoders with the actual
3965 		 * addresses that will be coming across the PCIe link.
3966 		 */
3967 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
3968 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
3969 
3970 		mw_init = &t4_memwin[0];
3971 	} else {
3972 		/* T5+ use the relative offset inside the PCIe BAR */
3973 		bar0 = 0;
3974 
3975 		mw_init = &t5_memwin[0];
3976 	}
3977 
3978 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
3979 		if (!rw_initialized(&mw->mw_lock)) {
3980 			rw_init(&mw->mw_lock, "memory window access");
3981 			mw->mw_base = mw_init->base;
3982 			mw->mw_aperture = mw_init->aperture;
3983 			mw->mw_curpos = 0;
3984 		}
3985 		t4_write_reg(sc,
3986 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
3987 		    (mw->mw_base + bar0) | V_BIR(0) |
3988 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
3989 		rw_wlock(&mw->mw_lock);
3990 		position_memwin(sc, i, mw->mw_curpos);
3991 		rw_wunlock(&mw->mw_lock);
3992 	}
3993 
3994 	/* flush */
3995 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
3996 }
3997 
3998 /*
3999  * Positions the memory window at the given address in the card's address space.
4000  * There are some alignment requirements and the actual position may be at an
4001  * address prior to the requested address.  mw->mw_curpos always has the actual
4002  * position of the window.
4003  */
4004 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)4005 position_memwin(struct adapter *sc, int idx, uint32_t addr)
4006 {
4007 	struct memwin *mw;
4008 	uint32_t pf;
4009 	uint32_t reg;
4010 
4011 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
4012 	mw = &sc->memwin[idx];
4013 	rw_assert(&mw->mw_lock, RA_WLOCKED);
4014 
4015 	if (is_t4(sc)) {
4016 		pf = 0;
4017 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
4018 	} else {
4019 		pf = V_PFNUM(sc->pf);
4020 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
4021 	}
4022 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
4023 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
4024 	t4_read_reg(sc, reg);	/* flush */
4025 }
4026 
4027 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)4028 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
4029     int len, int rw)
4030 {
4031 	struct memwin *mw;
4032 	uint32_t mw_end, v;
4033 
4034 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
4035 
4036 	/* Memory can only be accessed in naturally aligned 4 byte units */
4037 	if (addr & 3 || len & 3 || len <= 0)
4038 		return (EINVAL);
4039 
4040 	mw = &sc->memwin[idx];
4041 	while (len > 0) {
4042 		rw_rlock(&mw->mw_lock);
4043 		mw_end = mw->mw_curpos + mw->mw_aperture;
4044 		if (addr >= mw_end || addr < mw->mw_curpos) {
4045 			/* Will need to reposition the window */
4046 			if (!rw_try_upgrade(&mw->mw_lock)) {
4047 				rw_runlock(&mw->mw_lock);
4048 				rw_wlock(&mw->mw_lock);
4049 			}
4050 			rw_assert(&mw->mw_lock, RA_WLOCKED);
4051 			position_memwin(sc, idx, addr);
4052 			rw_downgrade(&mw->mw_lock);
4053 			mw_end = mw->mw_curpos + mw->mw_aperture;
4054 		}
4055 		rw_assert(&mw->mw_lock, RA_RLOCKED);
4056 		while (addr < mw_end && len > 0) {
4057 			if (rw == 0) {
4058 				v = t4_read_reg(sc, mw->mw_base + addr -
4059 				    mw->mw_curpos);
4060 				*val++ = le32toh(v);
4061 			} else {
4062 				v = *val++;
4063 				t4_write_reg(sc, mw->mw_base + addr -
4064 				    mw->mw_curpos, htole32(v));
4065 			}
4066 			addr += 4;
4067 			len -= 4;
4068 		}
4069 		rw_runlock(&mw->mw_lock);
4070 	}
4071 
4072 	return (0);
4073 }
4074 
4075 CTASSERT(M_TID_COOKIE == M_COOKIE);
4076 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
4077 
4078 static void
t4_init_atid_table(struct adapter * sc)4079 t4_init_atid_table(struct adapter *sc)
4080 {
4081 	struct tid_info *t;
4082 	int i;
4083 
4084 	t = &sc->tids;
4085 	if (t->natids == 0)
4086 		return;
4087 
4088 	MPASS(t->atid_tab == NULL);
4089 
4090 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
4091 	    M_ZERO | M_WAITOK);
4092 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
4093 	t->afree = t->atid_tab;
4094 	t->atids_in_use = 0;
4095 	t->atid_alloc_stopped = false;
4096 	for (i = 1; i < t->natids; i++)
4097 		t->atid_tab[i - 1].next = &t->atid_tab[i];
4098 	t->atid_tab[t->natids - 1].next = NULL;
4099 }
4100 
4101 static void
t4_free_atid_table(struct adapter * sc)4102 t4_free_atid_table(struct adapter *sc)
4103 {
4104 	struct tid_info *t;
4105 
4106 	t = &sc->tids;
4107 
4108 	KASSERT(t->atids_in_use == 0,
4109 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4110 
4111 	if (mtx_initialized(&t->atid_lock))
4112 		mtx_destroy(&t->atid_lock);
4113 	free(t->atid_tab, M_CXGBE);
4114 	t->atid_tab = NULL;
4115 }
4116 
4117 static void
stop_atid_allocator(struct adapter * sc)4118 stop_atid_allocator(struct adapter *sc)
4119 {
4120 	struct tid_info *t = &sc->tids;
4121 
4122 	if (t->natids == 0)
4123 		return;
4124 	mtx_lock(&t->atid_lock);
4125 	t->atid_alloc_stopped = true;
4126 	mtx_unlock(&t->atid_lock);
4127 }
4128 
4129 static void
restart_atid_allocator(struct adapter * sc)4130 restart_atid_allocator(struct adapter *sc)
4131 {
4132 	struct tid_info *t = &sc->tids;
4133 
4134 	if (t->natids == 0)
4135 		return;
4136 	mtx_lock(&t->atid_lock);
4137 	KASSERT(t->atids_in_use == 0,
4138 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4139 	t->atid_alloc_stopped = false;
4140 	mtx_unlock(&t->atid_lock);
4141 }
4142 
4143 int
alloc_atid(struct adapter * sc,void * ctx)4144 alloc_atid(struct adapter *sc, void *ctx)
4145 {
4146 	struct tid_info *t = &sc->tids;
4147 	int atid = -1;
4148 
4149 	mtx_lock(&t->atid_lock);
4150 	if (t->afree && !t->atid_alloc_stopped) {
4151 		union aopen_entry *p = t->afree;
4152 
4153 		atid = p - t->atid_tab;
4154 		MPASS(atid <= M_TID_TID);
4155 		t->afree = p->next;
4156 		p->data = ctx;
4157 		t->atids_in_use++;
4158 	}
4159 	mtx_unlock(&t->atid_lock);
4160 	return (atid);
4161 }
4162 
4163 void *
lookup_atid(struct adapter * sc,int atid)4164 lookup_atid(struct adapter *sc, int atid)
4165 {
4166 	struct tid_info *t = &sc->tids;
4167 
4168 	return (t->atid_tab[atid].data);
4169 }
4170 
4171 void
free_atid(struct adapter * sc,int atid)4172 free_atid(struct adapter *sc, int atid)
4173 {
4174 	struct tid_info *t = &sc->tids;
4175 	union aopen_entry *p = &t->atid_tab[atid];
4176 
4177 	mtx_lock(&t->atid_lock);
4178 	p->next = t->afree;
4179 	t->afree = p;
4180 	t->atids_in_use--;
4181 	mtx_unlock(&t->atid_lock);
4182 }
4183 
4184 static void
queue_tid_release(struct adapter * sc,int tid)4185 queue_tid_release(struct adapter *sc, int tid)
4186 {
4187 
4188 	CXGBE_UNIMPLEMENTED("deferred tid release");
4189 }
4190 
4191 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4192 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4193 {
4194 	struct wrqe *wr;
4195 	struct cpl_tid_release *req;
4196 
4197 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4198 	if (wr == NULL) {
4199 		queue_tid_release(sc, tid);	/* defer */
4200 		return;
4201 	}
4202 	req = wrtod(wr);
4203 
4204 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4205 
4206 	t4_wrq_tx(sc, wr);
4207 }
4208 
4209 static int
t4_range_cmp(const void * a,const void * b)4210 t4_range_cmp(const void *a, const void *b)
4211 {
4212 	return ((const struct t4_range *)a)->start -
4213 	       ((const struct t4_range *)b)->start;
4214 }
4215 
4216 /*
4217  * Verify that the memory range specified by the addr/len pair is valid within
4218  * the card's address space.
4219  */
4220 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4221 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4222 {
4223 	struct t4_range mem_ranges[4], *r, *next;
4224 	uint32_t em, addr_len;
4225 	int i, n, remaining;
4226 
4227 	/* Memory can only be accessed in naturally aligned 4 byte units */
4228 	if (addr & 3 || len & 3 || len == 0)
4229 		return (EINVAL);
4230 
4231 	/* Enabled memories */
4232 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4233 
4234 	r = &mem_ranges[0];
4235 	n = 0;
4236 	bzero(r, sizeof(mem_ranges));
4237 	if (em & F_EDRAM0_ENABLE) {
4238 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4239 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4240 		if (r->size > 0) {
4241 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4242 			if (addr >= r->start &&
4243 			    addr + len <= r->start + r->size)
4244 				return (0);
4245 			r++;
4246 			n++;
4247 		}
4248 	}
4249 	if (em & F_EDRAM1_ENABLE) {
4250 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4251 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4252 		if (r->size > 0) {
4253 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4254 			if (addr >= r->start &&
4255 			    addr + len <= r->start + r->size)
4256 				return (0);
4257 			r++;
4258 			n++;
4259 		}
4260 	}
4261 	if (em & F_EXT_MEM_ENABLE) {
4262 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4263 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4264 		if (r->size > 0) {
4265 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4266 			if (addr >= r->start &&
4267 			    addr + len <= r->start + r->size)
4268 				return (0);
4269 			r++;
4270 			n++;
4271 		}
4272 	}
4273 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4274 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4275 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4276 		if (r->size > 0) {
4277 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4278 			if (addr >= r->start &&
4279 			    addr + len <= r->start + r->size)
4280 				return (0);
4281 			r++;
4282 			n++;
4283 		}
4284 	}
4285 	MPASS(n <= nitems(mem_ranges));
4286 
4287 	if (n > 1) {
4288 		/* Sort and merge the ranges. */
4289 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4290 
4291 		/* Start from index 0 and examine the next n - 1 entries. */
4292 		r = &mem_ranges[0];
4293 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4294 
4295 			MPASS(r->size > 0);	/* r is a valid entry. */
4296 			next = r + 1;
4297 			MPASS(next->size > 0);	/* and so is the next one. */
4298 
4299 			while (r->start + r->size >= next->start) {
4300 				/* Merge the next one into the current entry. */
4301 				r->size = max(r->start + r->size,
4302 				    next->start + next->size) - r->start;
4303 				n--;	/* One fewer entry in total. */
4304 				if (--remaining == 0)
4305 					goto done;	/* short circuit */
4306 				next++;
4307 			}
4308 			if (next != r + 1) {
4309 				/*
4310 				 * Some entries were merged into r and next
4311 				 * points to the first valid entry that couldn't
4312 				 * be merged.
4313 				 */
4314 				MPASS(next->size > 0);	/* must be valid */
4315 				memcpy(r + 1, next, remaining * sizeof(*r));
4316 #ifdef INVARIANTS
4317 				/*
4318 				 * This so that the foo->size assertion in the
4319 				 * next iteration of the loop do the right
4320 				 * thing for entries that were pulled up and are
4321 				 * no longer valid.
4322 				 */
4323 				MPASS(n < nitems(mem_ranges));
4324 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4325 				    sizeof(struct t4_range));
4326 #endif
4327 			}
4328 		}
4329 done:
4330 		/* Done merging the ranges. */
4331 		MPASS(n > 0);
4332 		r = &mem_ranges[0];
4333 		for (i = 0; i < n; i++, r++) {
4334 			if (addr >= r->start &&
4335 			    addr + len <= r->start + r->size)
4336 				return (0);
4337 		}
4338 	}
4339 
4340 	return (EFAULT);
4341 }
4342 
4343 static int
fwmtype_to_hwmtype(int mtype)4344 fwmtype_to_hwmtype(int mtype)
4345 {
4346 
4347 	switch (mtype) {
4348 	case FW_MEMTYPE_EDC0:
4349 		return (MEM_EDC0);
4350 	case FW_MEMTYPE_EDC1:
4351 		return (MEM_EDC1);
4352 	case FW_MEMTYPE_EXTMEM:
4353 		return (MEM_MC0);
4354 	case FW_MEMTYPE_EXTMEM1:
4355 		return (MEM_MC1);
4356 	default:
4357 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4358 	}
4359 }
4360 
4361 /*
4362  * Verify that the memory range specified by the memtype/offset/len pair is
4363  * valid and lies entirely within the memtype specified.  The global address of
4364  * the start of the range is returned in addr.
4365  */
4366 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4367 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4368     uint32_t *addr)
4369 {
4370 	uint32_t em, addr_len, maddr;
4371 
4372 	/* Memory can only be accessed in naturally aligned 4 byte units */
4373 	if (off & 3 || len & 3 || len == 0)
4374 		return (EINVAL);
4375 
4376 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4377 	switch (fwmtype_to_hwmtype(mtype)) {
4378 	case MEM_EDC0:
4379 		if (!(em & F_EDRAM0_ENABLE))
4380 			return (EINVAL);
4381 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4382 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4383 		break;
4384 	case MEM_EDC1:
4385 		if (!(em & F_EDRAM1_ENABLE))
4386 			return (EINVAL);
4387 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4388 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4389 		break;
4390 	case MEM_MC:
4391 		if (!(em & F_EXT_MEM_ENABLE))
4392 			return (EINVAL);
4393 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4394 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4395 		break;
4396 	case MEM_MC1:
4397 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4398 			return (EINVAL);
4399 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4400 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4401 		break;
4402 	default:
4403 		return (EINVAL);
4404 	}
4405 
4406 	*addr = maddr + off;	/* global address */
4407 	return (validate_mem_range(sc, *addr, len));
4408 }
4409 
4410 static int
fixup_devlog_params(struct adapter * sc)4411 fixup_devlog_params(struct adapter *sc)
4412 {
4413 	struct devlog_params *dparams = &sc->params.devlog;
4414 	int rc;
4415 
4416 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4417 	    dparams->size, &dparams->addr);
4418 
4419 	return (rc);
4420 }
4421 
4422 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4423 update_nirq(struct intrs_and_queues *iaq, int nports)
4424 {
4425 
4426 	iaq->nirq = T4_EXTRA_INTR;
4427 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4428 	iaq->nirq += nports * iaq->nofldrxq;
4429 	iaq->nirq += nports * (iaq->num_vis - 1) *
4430 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4431 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4432 }
4433 
4434 /*
4435  * Adjust requirements to fit the number of interrupts available.
4436  */
4437 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4438 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4439     int navail)
4440 {
4441 	int old_nirq;
4442 	const int nports = sc->params.nports;
4443 
4444 	MPASS(nports > 0);
4445 	MPASS(navail > 0);
4446 
4447 	bzero(iaq, sizeof(*iaq));
4448 	iaq->intr_type = itype;
4449 	iaq->num_vis = t4_num_vis;
4450 	iaq->ntxq = t4_ntxq;
4451 	iaq->ntxq_vi = t4_ntxq_vi;
4452 	iaq->nrxq = t4_nrxq;
4453 	iaq->nrxq_vi = t4_nrxq_vi;
4454 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4455 	if (is_offload(sc) || is_ethoffload(sc)) {
4456 		iaq->nofldtxq = t4_nofldtxq;
4457 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
4458 	}
4459 #endif
4460 #ifdef TCP_OFFLOAD
4461 	if (is_offload(sc)) {
4462 		iaq->nofldrxq = t4_nofldrxq;
4463 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4464 	}
4465 #endif
4466 #ifdef DEV_NETMAP
4467 	if (t4_native_netmap & NN_MAIN_VI) {
4468 		iaq->nnmtxq = t4_nnmtxq;
4469 		iaq->nnmrxq = t4_nnmrxq;
4470 	}
4471 	if (t4_native_netmap & NN_EXTRA_VI) {
4472 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4473 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4474 	}
4475 #endif
4476 
4477 	update_nirq(iaq, nports);
4478 	if (iaq->nirq <= navail &&
4479 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4480 		/*
4481 		 * This is the normal case -- there are enough interrupts for
4482 		 * everything.
4483 		 */
4484 		goto done;
4485 	}
4486 
4487 	/*
4488 	 * If extra VIs have been configured try reducing their count and see if
4489 	 * that works.
4490 	 */
4491 	while (iaq->num_vis > 1) {
4492 		iaq->num_vis--;
4493 		update_nirq(iaq, nports);
4494 		if (iaq->nirq <= navail &&
4495 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4496 			device_printf(sc->dev, "virtual interfaces per port "
4497 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4498 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4499 			    "itype %d, navail %u, nirq %d.\n",
4500 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4501 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4502 			    itype, navail, iaq->nirq);
4503 			goto done;
4504 		}
4505 	}
4506 
4507 	/*
4508 	 * Extra VIs will not be created.  Log a message if they were requested.
4509 	 */
4510 	MPASS(iaq->num_vis == 1);
4511 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4512 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4513 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4514 	if (iaq->num_vis != t4_num_vis) {
4515 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4516 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4517 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4518 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4519 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4520 	}
4521 
4522 	/*
4523 	 * Keep reducing the number of NIC rx queues to the next lower power of
4524 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4525 	 * if that works.
4526 	 */
4527 	do {
4528 		if (iaq->nrxq > 1) {
4529 			iaq->nrxq = rounddown_pow_of_two(iaq->nrxq - 1);
4530 			if (iaq->nnmrxq > iaq->nrxq)
4531 				iaq->nnmrxq = iaq->nrxq;
4532 		}
4533 		if (iaq->nofldrxq > 1)
4534 			iaq->nofldrxq >>= 1;
4535 
4536 		old_nirq = iaq->nirq;
4537 		update_nirq(iaq, nports);
4538 		if (iaq->nirq <= navail &&
4539 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4540 			device_printf(sc->dev, "running with reduced number of "
4541 			    "rx queues because of shortage of interrupts.  "
4542 			    "nrxq=%u, nofldrxq=%u.  "
4543 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4544 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4545 			goto done;
4546 		}
4547 	} while (old_nirq != iaq->nirq);
4548 
4549 	/* One interrupt for everything.  Ugh. */
4550 	device_printf(sc->dev, "running with minimal number of queues.  "
4551 	    "itype %d, navail %u.\n", itype, navail);
4552 	iaq->nirq = 1;
4553 	iaq->nrxq = 1;
4554 	iaq->ntxq = 1;
4555 	if (iaq->nofldrxq > 0) {
4556 		iaq->nofldrxq = 1;
4557 		iaq->nofldtxq = 1;
4558 	}
4559 	iaq->nnmtxq = 0;
4560 	iaq->nnmrxq = 0;
4561 done:
4562 	MPASS(iaq->num_vis > 0);
4563 	if (iaq->num_vis > 1) {
4564 		MPASS(iaq->nrxq_vi > 0);
4565 		MPASS(iaq->ntxq_vi > 0);
4566 	}
4567 	MPASS(iaq->nirq > 0);
4568 	MPASS(iaq->nrxq > 0);
4569 	MPASS(iaq->ntxq > 0);
4570 	if (itype == INTR_MSI) {
4571 		MPASS(powerof2(iaq->nirq));
4572 	}
4573 }
4574 
4575 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4576 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4577 {
4578 	int rc, itype, navail, nalloc;
4579 
4580 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4581 
4582 		if ((itype & t4_intr_types) == 0)
4583 			continue;	/* not allowed */
4584 
4585 		if (itype == INTR_MSIX)
4586 			navail = pci_msix_count(sc->dev);
4587 		else if (itype == INTR_MSI)
4588 			navail = pci_msi_count(sc->dev);
4589 		else
4590 			navail = 1;
4591 restart:
4592 		if (navail == 0)
4593 			continue;
4594 
4595 		calculate_iaq(sc, iaq, itype, navail);
4596 		nalloc = iaq->nirq;
4597 		rc = 0;
4598 		if (itype == INTR_MSIX)
4599 			rc = pci_alloc_msix(sc->dev, &nalloc);
4600 		else if (itype == INTR_MSI)
4601 			rc = pci_alloc_msi(sc->dev, &nalloc);
4602 
4603 		if (rc == 0 && nalloc > 0) {
4604 			if (nalloc == iaq->nirq)
4605 				return (0);
4606 
4607 			/*
4608 			 * Didn't get the number requested.  Use whatever number
4609 			 * the kernel is willing to allocate.
4610 			 */
4611 			device_printf(sc->dev, "fewer vectors than requested, "
4612 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4613 			    itype, iaq->nirq, nalloc);
4614 			pci_release_msi(sc->dev);
4615 			navail = nalloc;
4616 			goto restart;
4617 		}
4618 
4619 		device_printf(sc->dev,
4620 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4621 		    itype, rc, iaq->nirq, nalloc);
4622 	}
4623 
4624 	device_printf(sc->dev,
4625 	    "failed to find a usable interrupt type.  "
4626 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4627 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4628 
4629 	return (ENXIO);
4630 }
4631 
4632 #define FW_VERSION(chip) ( \
4633     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4634     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4635     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4636     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4637 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4638 
4639 /* Just enough of fw_hdr to cover all version info. */
4640 struct fw_h {
4641 	__u8	ver;
4642 	__u8	chip;
4643 	__be16	len512;
4644 	__be32	fw_ver;
4645 	__be32	tp_microcode_ver;
4646 	__u8	intfver_nic;
4647 	__u8	intfver_vnic;
4648 	__u8	intfver_ofld;
4649 	__u8	intfver_ri;
4650 	__u8	intfver_iscsipdu;
4651 	__u8	intfver_iscsi;
4652 	__u8	intfver_fcoepdu;
4653 	__u8	intfver_fcoe;
4654 };
4655 /* Spot check a couple of fields. */
4656 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4657 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4658 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4659 
4660 struct fw_info {
4661 	uint8_t chip;
4662 	char *kld_name;
4663 	char *fw_mod_name;
4664 	struct fw_h fw_h;
4665 } fw_info[] = {
4666 	{
4667 		.chip = CHELSIO_T4,
4668 		.kld_name = "t4fw_cfg",
4669 		.fw_mod_name = "t4fw",
4670 		.fw_h = {
4671 			.chip = FW_HDR_CHIP_T4,
4672 			.fw_ver = htobe32(FW_VERSION(T4)),
4673 			.intfver_nic = FW_INTFVER(T4, NIC),
4674 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4675 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4676 			.intfver_ri = FW_INTFVER(T4, RI),
4677 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4678 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4679 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4680 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4681 		},
4682 	}, {
4683 		.chip = CHELSIO_T5,
4684 		.kld_name = "t5fw_cfg",
4685 		.fw_mod_name = "t5fw",
4686 		.fw_h = {
4687 			.chip = FW_HDR_CHIP_T5,
4688 			.fw_ver = htobe32(FW_VERSION(T5)),
4689 			.intfver_nic = FW_INTFVER(T5, NIC),
4690 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4691 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4692 			.intfver_ri = FW_INTFVER(T5, RI),
4693 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4694 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4695 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4696 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4697 		},
4698 	}, {
4699 		.chip = CHELSIO_T6,
4700 		.kld_name = "t6fw_cfg",
4701 		.fw_mod_name = "t6fw",
4702 		.fw_h = {
4703 			.chip = FW_HDR_CHIP_T6,
4704 			.fw_ver = htobe32(FW_VERSION(T6)),
4705 			.intfver_nic = FW_INTFVER(T6, NIC),
4706 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4707 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4708 			.intfver_ri = FW_INTFVER(T6, RI),
4709 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4710 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4711 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4712 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4713 		},
4714 	}
4715 };
4716 
4717 static struct fw_info *
find_fw_info(int chip)4718 find_fw_info(int chip)
4719 {
4720 	int i;
4721 
4722 	for (i = 0; i < nitems(fw_info); i++) {
4723 		if (fw_info[i].chip == chip)
4724 			return (&fw_info[i]);
4725 	}
4726 	return (NULL);
4727 }
4728 
4729 /*
4730  * Is the given firmware API compatible with the one the driver was compiled
4731  * with?
4732  */
4733 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4734 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4735 {
4736 
4737 	/* short circuit if it's the exact same firmware version */
4738 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4739 		return (1);
4740 
4741 	/*
4742 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4743 	 * features that are supported in the driver.
4744 	 */
4745 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4746 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4747 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4748 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4749 		return (1);
4750 #undef SAME_INTF
4751 
4752 	return (0);
4753 }
4754 
4755 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4756 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4757     const struct firmware **fw)
4758 {
4759 	struct fw_info *fw_info;
4760 
4761 	*dcfg = NULL;
4762 	if (fw != NULL)
4763 		*fw = NULL;
4764 
4765 	fw_info = find_fw_info(chip_id(sc));
4766 	if (fw_info == NULL) {
4767 		device_printf(sc->dev,
4768 		    "unable to look up firmware information for chip %d.\n",
4769 		    chip_id(sc));
4770 		return (EINVAL);
4771 	}
4772 
4773 	*dcfg = firmware_get(fw_info->kld_name);
4774 	if (*dcfg != NULL) {
4775 		if (fw != NULL)
4776 			*fw = firmware_get(fw_info->fw_mod_name);
4777 		return (0);
4778 	}
4779 
4780 	return (ENOENT);
4781 }
4782 
4783 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4784 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4785     const struct firmware *fw)
4786 {
4787 
4788 	if (fw != NULL)
4789 		firmware_put(fw, FIRMWARE_UNLOAD);
4790 	if (dcfg != NULL)
4791 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4792 }
4793 
4794 /*
4795  * Return values:
4796  * 0 means no firmware install attempted.
4797  * ERESTART means a firmware install was attempted and was successful.
4798  * +ve errno means a firmware install was attempted but failed.
4799  */
4800 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4801 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4802     const struct fw_h *drv_fw, const char *reason, int *already)
4803 {
4804 	const struct firmware *cfg, *fw;
4805 	const uint32_t c = be32toh(card_fw->fw_ver);
4806 	uint32_t d, k;
4807 	int rc, fw_install;
4808 	struct fw_h bundled_fw;
4809 	bool load_attempted;
4810 
4811 	cfg = fw = NULL;
4812 	load_attempted = false;
4813 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4814 
4815 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4816 	if (t4_fw_install < 0) {
4817 		rc = load_fw_module(sc, &cfg, &fw);
4818 		if (rc != 0 || fw == NULL) {
4819 			device_printf(sc->dev,
4820 			    "failed to load firmware module: %d. cfg %p, fw %p;"
4821 			    " will use compiled-in firmware version for"
4822 			    "hw.cxgbe.fw_install checks.\n",
4823 			    rc, cfg, fw);
4824 		} else {
4825 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
4826 		}
4827 		load_attempted = true;
4828 	}
4829 	d = be32toh(bundled_fw.fw_ver);
4830 
4831 	if (reason != NULL)
4832 		goto install;
4833 
4834 	if ((sc->flags & FW_OK) == 0) {
4835 
4836 		if (c == 0xffffffff) {
4837 			reason = "missing";
4838 			goto install;
4839 		}
4840 
4841 		rc = 0;
4842 		goto done;
4843 	}
4844 
4845 	if (!fw_compatible(card_fw, &bundled_fw)) {
4846 		reason = "incompatible or unusable";
4847 		goto install;
4848 	}
4849 
4850 	if (d > c) {
4851 		reason = "older than the version bundled with this driver";
4852 		goto install;
4853 	}
4854 
4855 	if (fw_install == 2 && d != c) {
4856 		reason = "different than the version bundled with this driver";
4857 		goto install;
4858 	}
4859 
4860 	/* No reason to do anything to the firmware already on the card. */
4861 	rc = 0;
4862 	goto done;
4863 
4864 install:
4865 	rc = 0;
4866 	if ((*already)++)
4867 		goto done;
4868 
4869 	if (fw_install == 0) {
4870 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4871 		    "but the driver is prohibited from installing a firmware "
4872 		    "on the card.\n",
4873 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4874 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4875 
4876 		goto done;
4877 	}
4878 
4879 	/*
4880 	 * We'll attempt to install a firmware.  Load the module first (if it
4881 	 * hasn't been loaded already).
4882 	 */
4883 	if (!load_attempted) {
4884 		rc = load_fw_module(sc, &cfg, &fw);
4885 		if (rc != 0 || fw == NULL) {
4886 			device_printf(sc->dev,
4887 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
4888 			    rc, cfg, fw);
4889 			/* carry on */
4890 		}
4891 	}
4892 	if (fw == NULL) {
4893 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4894 		    "but the driver cannot take corrective action because it "
4895 		    "is unable to load the firmware module.\n",
4896 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4897 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4898 		rc = sc->flags & FW_OK ? 0 : ENOENT;
4899 		goto done;
4900 	}
4901 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
4902 	if (k != d) {
4903 		MPASS(t4_fw_install > 0);
4904 		device_printf(sc->dev,
4905 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
4906 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
4907 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
4908 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
4909 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4910 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4911 		rc = sc->flags & FW_OK ? 0 : EINVAL;
4912 		goto done;
4913 	}
4914 
4915 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4916 	    "installing firmware %u.%u.%u.%u on card.\n",
4917 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4918 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
4919 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4920 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4921 
4922 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
4923 	if (rc != 0) {
4924 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
4925 	} else {
4926 		/* Installed successfully, update the cached header too. */
4927 		rc = ERESTART;
4928 		memcpy(card_fw, fw->data, sizeof(*card_fw));
4929 	}
4930 done:
4931 	unload_fw_module(sc, cfg, fw);
4932 
4933 	return (rc);
4934 }
4935 
4936 /*
4937  * Establish contact with the firmware and attempt to become the master driver.
4938  *
4939  * A firmware will be installed to the card if needed (if the driver is allowed
4940  * to do so).
4941  */
4942 static int
contact_firmware(struct adapter * sc)4943 contact_firmware(struct adapter *sc)
4944 {
4945 	int rc, already = 0;
4946 	enum dev_state state;
4947 	struct fw_info *fw_info;
4948 	struct fw_hdr *card_fw;		/* fw on the card */
4949 	const struct fw_h *drv_fw;
4950 
4951 	fw_info = find_fw_info(chip_id(sc));
4952 	if (fw_info == NULL) {
4953 		device_printf(sc->dev,
4954 		    "unable to look up firmware information for chip %d.\n",
4955 		    chip_id(sc));
4956 		return (EINVAL);
4957 	}
4958 	drv_fw = &fw_info->fw_h;
4959 
4960 	/* Read the header of the firmware on the card */
4961 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
4962 restart:
4963 	rc = -t4_get_fw_hdr(sc, card_fw);
4964 	if (rc != 0) {
4965 		device_printf(sc->dev,
4966 		    "unable to read firmware header from card's flash: %d\n",
4967 		    rc);
4968 		goto done;
4969 	}
4970 
4971 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
4972 	    &already);
4973 	if (rc == ERESTART)
4974 		goto restart;
4975 	if (rc != 0)
4976 		goto done;
4977 
4978 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
4979 	if (rc < 0 || state == DEV_STATE_ERR) {
4980 		rc = -rc;
4981 		device_printf(sc->dev,
4982 		    "failed to connect to the firmware: %d, %d.  "
4983 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4984 #if 0
4985 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4986 		    "not responding properly to HELLO", &already) == ERESTART)
4987 			goto restart;
4988 #endif
4989 		goto done;
4990 	}
4991 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
4992 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
4993 
4994 	if (rc == sc->pf) {
4995 		sc->flags |= MASTER_PF;
4996 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4997 		    NULL, &already);
4998 		if (rc == ERESTART)
4999 			rc = 0;
5000 		else if (rc != 0)
5001 			goto done;
5002 	} else if (state == DEV_STATE_UNINIT) {
5003 		/*
5004 		 * We didn't get to be the master so we definitely won't be
5005 		 * configuring the chip.  It's a bug if someone else hasn't
5006 		 * configured it already.
5007 		 */
5008 		device_printf(sc->dev, "couldn't be master(%d), "
5009 		    "device not already initialized either(%d).  "
5010 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5011 		rc = EPROTO;
5012 		goto done;
5013 	} else {
5014 		/*
5015 		 * Some other PF is the master and has configured the chip.
5016 		 * This is allowed but untested.
5017 		 */
5018 		device_printf(sc->dev, "PF%d is master, device state %d.  "
5019 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5020 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
5021 		sc->cfcsum = 0;
5022 		rc = 0;
5023 	}
5024 done:
5025 	if (rc != 0 && sc->flags & FW_OK) {
5026 		t4_fw_bye(sc, sc->mbox);
5027 		sc->flags &= ~FW_OK;
5028 	}
5029 	free(card_fw, M_CXGBE);
5030 	return (rc);
5031 }
5032 
5033 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff)5034 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
5035     uint32_t mtype, uint32_t moff)
5036 {
5037 	struct fw_info *fw_info;
5038 	const struct firmware *dcfg, *rcfg = NULL;
5039 	const uint32_t *cfdata;
5040 	uint32_t cflen, addr;
5041 	int rc;
5042 
5043 	load_fw_module(sc, &dcfg, NULL);
5044 
5045 	/* Card specific interpretation of "default". */
5046 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5047 		if (pci_get_device(sc->dev) == 0x440a)
5048 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
5049 		if (is_fpga(sc))
5050 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
5051 	}
5052 
5053 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5054 		if (dcfg == NULL) {
5055 			device_printf(sc->dev,
5056 			    "KLD with default config is not available.\n");
5057 			rc = ENOENT;
5058 			goto done;
5059 		}
5060 		cfdata = dcfg->data;
5061 		cflen = dcfg->datasize & ~3;
5062 	} else {
5063 		char s[32];
5064 
5065 		fw_info = find_fw_info(chip_id(sc));
5066 		if (fw_info == NULL) {
5067 			device_printf(sc->dev,
5068 			    "unable to look up firmware information for chip %d.\n",
5069 			    chip_id(sc));
5070 			rc = EINVAL;
5071 			goto done;
5072 		}
5073 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
5074 
5075 		rcfg = firmware_get(s);
5076 		if (rcfg == NULL) {
5077 			device_printf(sc->dev,
5078 			    "unable to load module \"%s\" for configuration "
5079 			    "profile \"%s\".\n", s, cfg_file);
5080 			rc = ENOENT;
5081 			goto done;
5082 		}
5083 		cfdata = rcfg->data;
5084 		cflen = rcfg->datasize & ~3;
5085 	}
5086 
5087 	if (cflen > FLASH_CFG_MAX_SIZE) {
5088 		device_printf(sc->dev,
5089 		    "config file too long (%d, max allowed is %d).\n",
5090 		    cflen, FLASH_CFG_MAX_SIZE);
5091 		rc = EINVAL;
5092 		goto done;
5093 	}
5094 
5095 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
5096 	if (rc != 0) {
5097 		device_printf(sc->dev,
5098 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
5099 		    __func__, mtype, moff, cflen, rc);
5100 		rc = EINVAL;
5101 		goto done;
5102 	}
5103 	write_via_memwin(sc, 2, addr, cfdata, cflen);
5104 done:
5105 	if (rcfg != NULL)
5106 		firmware_put(rcfg, FIRMWARE_UNLOAD);
5107 	unload_fw_module(sc, dcfg, NULL);
5108 	return (rc);
5109 }
5110 
5111 struct caps_allowed {
5112 	uint16_t nbmcaps;
5113 	uint16_t linkcaps;
5114 	uint16_t switchcaps;
5115 	uint16_t niccaps;
5116 	uint16_t toecaps;
5117 	uint16_t rdmacaps;
5118 	uint16_t cryptocaps;
5119 	uint16_t iscsicaps;
5120 	uint16_t fcoecaps;
5121 };
5122 
5123 #define FW_PARAM_DEV(param) \
5124 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5125 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5126 #define FW_PARAM_PFVF(param) \
5127 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5128 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5129 
5130 /*
5131  * Provide a configuration profile to the firmware and have it initialize the
5132  * chip accordingly.  This may involve uploading a configuration file to the
5133  * card.
5134  */
5135 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5136 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5137     const struct caps_allowed *caps_allowed)
5138 {
5139 	int rc;
5140 	struct fw_caps_config_cmd caps;
5141 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5142 
5143 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5144 	if (rc != 0) {
5145 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5146 		return (rc);
5147 	}
5148 
5149 	bzero(&caps, sizeof(caps));
5150 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5151 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5152 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5153 		mtype = 0;
5154 		moff = 0;
5155 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5156 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5157 		mtype = FW_MEMTYPE_FLASH;
5158 		moff = t4_flash_cfg_addr(sc);
5159 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5160 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5161 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5162 		    FW_LEN16(caps));
5163 	} else {
5164 		/*
5165 		 * Ask the firmware where it wants us to upload the config file.
5166 		 */
5167 		param = FW_PARAM_DEV(CF);
5168 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5169 		if (rc != 0) {
5170 			/* No support for config file?  Shouldn't happen. */
5171 			device_printf(sc->dev,
5172 			    "failed to query config file location: %d.\n", rc);
5173 			goto done;
5174 		}
5175 		mtype = G_FW_PARAMS_PARAM_Y(val);
5176 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5177 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5178 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5179 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5180 		    FW_LEN16(caps));
5181 
5182 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
5183 		if (rc != 0) {
5184 			device_printf(sc->dev,
5185 			    "failed to upload config file to card: %d.\n", rc);
5186 			goto done;
5187 		}
5188 	}
5189 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5190 	if (rc != 0) {
5191 		device_printf(sc->dev, "failed to pre-process config file: %d "
5192 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5193 		goto done;
5194 	}
5195 
5196 	finicsum = be32toh(caps.finicsum);
5197 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5198 	if (finicsum != cfcsum) {
5199 		device_printf(sc->dev,
5200 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5201 		    finicsum, cfcsum);
5202 	}
5203 	sc->cfcsum = cfcsum;
5204 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5205 
5206 	/*
5207 	 * Let the firmware know what features will (not) be used so it can tune
5208 	 * things accordingly.
5209 	 */
5210 #define LIMIT_CAPS(x) do { \
5211 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5212 } while (0)
5213 	LIMIT_CAPS(nbm);
5214 	LIMIT_CAPS(link);
5215 	LIMIT_CAPS(switch);
5216 	LIMIT_CAPS(nic);
5217 	LIMIT_CAPS(toe);
5218 	LIMIT_CAPS(rdma);
5219 	LIMIT_CAPS(crypto);
5220 	LIMIT_CAPS(iscsi);
5221 	LIMIT_CAPS(fcoe);
5222 #undef LIMIT_CAPS
5223 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5224 		/*
5225 		 * TOE and hashfilters are mutually exclusive.  It is a config
5226 		 * file or firmware bug if both are reported as available.  Try
5227 		 * to cope with the situation in non-debug builds by disabling
5228 		 * TOE.
5229 		 */
5230 		MPASS(caps.toecaps == 0);
5231 
5232 		caps.toecaps = 0;
5233 		caps.rdmacaps = 0;
5234 		caps.iscsicaps = 0;
5235 	}
5236 
5237 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5238 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5239 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5240 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5241 	if (rc != 0) {
5242 		device_printf(sc->dev,
5243 		    "failed to process config file: %d.\n", rc);
5244 		goto done;
5245 	}
5246 
5247 	t4_tweak_chip_settings(sc);
5248 	set_params__pre_init(sc);
5249 
5250 	/* get basic stuff going */
5251 	rc = -t4_fw_initialize(sc, sc->mbox);
5252 	if (rc != 0) {
5253 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5254 		goto done;
5255 	}
5256 done:
5257 	return (rc);
5258 }
5259 
5260 /*
5261  * Partition chip resources for use between various PFs, VFs, etc.
5262  */
5263 static int
partition_resources(struct adapter * sc)5264 partition_resources(struct adapter *sc)
5265 {
5266 	char cfg_file[sizeof(t4_cfg_file)];
5267 	struct caps_allowed caps_allowed;
5268 	int rc;
5269 	bool fallback;
5270 
5271 	/* Only the master driver gets to configure the chip resources. */
5272 	MPASS(sc->flags & MASTER_PF);
5273 
5274 #define COPY_CAPS(x) do { \
5275 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5276 } while (0)
5277 	bzero(&caps_allowed, sizeof(caps_allowed));
5278 	COPY_CAPS(nbm);
5279 	COPY_CAPS(link);
5280 	COPY_CAPS(switch);
5281 	COPY_CAPS(nic);
5282 	COPY_CAPS(toe);
5283 	COPY_CAPS(rdma);
5284 	COPY_CAPS(crypto);
5285 	COPY_CAPS(iscsi);
5286 	COPY_CAPS(fcoe);
5287 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5288 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5289 retry:
5290 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5291 	if (rc != 0 && fallback) {
5292 		dump_devlog(sc);
5293 		device_printf(sc->dev,
5294 		    "failed (%d) to configure card with \"%s\" profile, "
5295 		    "will fall back to a basic configuration and retry.\n",
5296 		    rc, cfg_file);
5297 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5298 		bzero(&caps_allowed, sizeof(caps_allowed));
5299 		COPY_CAPS(switch);
5300 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5301 		fallback = false;
5302 		goto retry;
5303 	}
5304 #undef COPY_CAPS
5305 	return (rc);
5306 }
5307 
5308 /*
5309  * Retrieve parameters that are needed (or nice to have) very early.
5310  */
5311 static int
get_params__pre_init(struct adapter * sc)5312 get_params__pre_init(struct adapter *sc)
5313 {
5314 	int rc;
5315 	uint32_t param[2], val[2];
5316 
5317 	t4_get_version_info(sc);
5318 
5319 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5320 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5321 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5322 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5323 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5324 
5325 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5326 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5327 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5328 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5329 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5330 
5331 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5332 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5333 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5334 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5335 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5336 
5337 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5338 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5339 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5340 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5341 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5342 
5343 	param[0] = FW_PARAM_DEV(PORTVEC);
5344 	param[1] = FW_PARAM_DEV(CCLK);
5345 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5346 	if (rc != 0) {
5347 		device_printf(sc->dev,
5348 		    "failed to query parameters (pre_init): %d.\n", rc);
5349 		return (rc);
5350 	}
5351 
5352 	sc->params.portvec = val[0];
5353 	sc->params.nports = bitcount32(val[0]);
5354 	sc->params.vpd.cclk = val[1];
5355 
5356 	/* Read device log parameters. */
5357 	rc = -t4_init_devlog_params(sc, 1);
5358 	if (rc == 0)
5359 		fixup_devlog_params(sc);
5360 	else {
5361 		device_printf(sc->dev,
5362 		    "failed to get devlog parameters: %d.\n", rc);
5363 		rc = 0;	/* devlog isn't critical for device operation */
5364 	}
5365 
5366 	return (rc);
5367 }
5368 
5369 /*
5370  * Any params that need to be set before FW_INITIALIZE.
5371  */
5372 static int
set_params__pre_init(struct adapter * sc)5373 set_params__pre_init(struct adapter *sc)
5374 {
5375 	int rc = 0;
5376 	uint32_t param, val;
5377 
5378 	if (chip_id(sc) >= CHELSIO_T6) {
5379 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5380 		val = 1;
5381 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5382 		/* firmwares < 1.20.1.0 do not have this param. */
5383 		if (rc == FW_EINVAL &&
5384 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5385 			rc = 0;
5386 		}
5387 		if (rc != 0) {
5388 			device_printf(sc->dev,
5389 			    "failed to enable high priority filters :%d.\n",
5390 			    rc);
5391 		}
5392 
5393 		param = FW_PARAM_DEV(PPOD_EDRAM);
5394 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5395 		if (rc == 0 && val == 1) {
5396 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5397 			    &val);
5398 			if (rc != 0) {
5399 				device_printf(sc->dev,
5400 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5401 			}
5402 		}
5403 	}
5404 
5405 	/* Enable opaque VIIDs with firmwares that support it. */
5406 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5407 	val = 1;
5408 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5409 	if (rc == 0 && val == 1)
5410 		sc->params.viid_smt_extn_support = true;
5411 	else
5412 		sc->params.viid_smt_extn_support = false;
5413 
5414 	return (rc);
5415 }
5416 
5417 /*
5418  * Retrieve various parameters that are of interest to the driver.  The device
5419  * has been initialized by the firmware at this point.
5420  */
5421 static int
get_params__post_init(struct adapter * sc)5422 get_params__post_init(struct adapter *sc)
5423 {
5424 	int rc;
5425 	uint32_t param[7], val[7];
5426 	struct fw_caps_config_cmd caps;
5427 
5428 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5429 	param[1] = FW_PARAM_PFVF(EQ_START);
5430 	param[2] = FW_PARAM_PFVF(FILTER_START);
5431 	param[3] = FW_PARAM_PFVF(FILTER_END);
5432 	param[4] = FW_PARAM_PFVF(L2T_START);
5433 	param[5] = FW_PARAM_PFVF(L2T_END);
5434 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5435 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5436 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5437 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5438 	if (rc != 0) {
5439 		device_printf(sc->dev,
5440 		    "failed to query parameters (post_init): %d.\n", rc);
5441 		return (rc);
5442 	}
5443 
5444 	sc->sge.iq_start = val[0];
5445 	sc->sge.eq_start = val[1];
5446 	if ((int)val[3] > (int)val[2]) {
5447 		sc->tids.ftid_base = val[2];
5448 		sc->tids.ftid_end = val[3];
5449 		sc->tids.nftids = val[3] - val[2] + 1;
5450 	}
5451 	sc->vres.l2t.start = val[4];
5452 	sc->vres.l2t.size = val[5] - val[4] + 1;
5453 	/* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5454 	if (sc->vres.l2t.size > 0)
5455 		MPASS(fls(val[5]) <= S_SYNC_WR);
5456 	sc->params.core_vdd = val[6];
5457 
5458 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5459 	param[1] = FW_PARAM_PFVF(EQ_END);
5460 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5461 	if (rc != 0) {
5462 		device_printf(sc->dev,
5463 		    "failed to query parameters (post_init2): %d.\n", rc);
5464 		return (rc);
5465 	}
5466 	MPASS((int)val[0] >= sc->sge.iq_start);
5467 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5468 	MPASS((int)val[1] >= sc->sge.eq_start);
5469 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5470 
5471 	if (chip_id(sc) >= CHELSIO_T6) {
5472 
5473 		sc->tids.tid_base = t4_read_reg(sc,
5474 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5475 
5476 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5477 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5478 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5479 		if (rc != 0) {
5480 			device_printf(sc->dev,
5481 			   "failed to query hpfilter parameters: %d.\n", rc);
5482 			return (rc);
5483 		}
5484 		if ((int)val[1] > (int)val[0]) {
5485 			sc->tids.hpftid_base = val[0];
5486 			sc->tids.hpftid_end = val[1];
5487 			sc->tids.nhpftids = val[1] - val[0] + 1;
5488 
5489 			/*
5490 			 * These should go off if the layout changes and the
5491 			 * driver needs to catch up.
5492 			 */
5493 			MPASS(sc->tids.hpftid_base == 0);
5494 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5495 		}
5496 
5497 		param[0] = FW_PARAM_PFVF(RAWF_START);
5498 		param[1] = FW_PARAM_PFVF(RAWF_END);
5499 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5500 		if (rc != 0) {
5501 			device_printf(sc->dev,
5502 			   "failed to query rawf parameters: %d.\n", rc);
5503 			return (rc);
5504 		}
5505 		if ((int)val[1] > (int)val[0]) {
5506 			sc->rawf_base = val[0];
5507 			sc->nrawf = val[1] - val[0] + 1;
5508 		}
5509 	}
5510 
5511 	/*
5512 	 * The parameters that follow may not be available on all firmwares.  We
5513 	 * query them individually rather than in a compound query because old
5514 	 * firmwares fail the entire query if an unknown parameter is queried.
5515 	 */
5516 
5517 	/*
5518 	 * MPS buffer group configuration.
5519 	 */
5520 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5521 	val[0] = 0;
5522 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5523 	if (rc == 0)
5524 		sc->params.mps_bg_map = val[0];
5525 	else
5526 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5527 
5528 	param[0] = FW_PARAM_DEV(TPCHMAP);
5529 	val[0] = 0;
5530 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5531 	if (rc == 0)
5532 		sc->params.tp_ch_map = val[0];
5533 	else
5534 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5535 
5536 	/*
5537 	 * Determine whether the firmware supports the filter2 work request.
5538 	 */
5539 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5540 	val[0] = 0;
5541 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5542 	if (rc == 0)
5543 		sc->params.filter2_wr_support = val[0] != 0;
5544 	else
5545 		sc->params.filter2_wr_support = 0;
5546 
5547 	/*
5548 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5549 	 */
5550 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5551 	val[0] = 0;
5552 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5553 	if (rc == 0)
5554 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5555 	else
5556 		sc->params.ulptx_memwrite_dsgl = false;
5557 
5558 	/* FW_RI_FR_NSMR_TPTE_WR support */
5559 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5560 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5561 	if (rc == 0)
5562 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5563 	else
5564 		sc->params.fr_nsmr_tpte_wr_support = false;
5565 
5566 	/* Support for 512 SGL entries per FR MR. */
5567 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5568 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5569 	if (rc == 0)
5570 		sc->params.dev_512sgl_mr = val[0] != 0;
5571 	else
5572 		sc->params.dev_512sgl_mr = false;
5573 
5574 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5575 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5576 	if (rc == 0)
5577 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5578 	else
5579 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5580 
5581 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5582 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5583 	if (rc == 0) {
5584 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5585 		sc->params.nsched_cls = val[0];
5586 	} else
5587 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5588 
5589 	/* get capabilites */
5590 	bzero(&caps, sizeof(caps));
5591 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5592 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5593 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5594 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5595 	if (rc != 0) {
5596 		device_printf(sc->dev,
5597 		    "failed to get card capabilities: %d.\n", rc);
5598 		return (rc);
5599 	}
5600 
5601 #define READ_CAPS(x) do { \
5602 	sc->x = htobe16(caps.x); \
5603 } while (0)
5604 	READ_CAPS(nbmcaps);
5605 	READ_CAPS(linkcaps);
5606 	READ_CAPS(switchcaps);
5607 	READ_CAPS(niccaps);
5608 	READ_CAPS(toecaps);
5609 	READ_CAPS(rdmacaps);
5610 	READ_CAPS(cryptocaps);
5611 	READ_CAPS(iscsicaps);
5612 	READ_CAPS(fcoecaps);
5613 
5614 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5615 		MPASS(chip_id(sc) > CHELSIO_T4);
5616 		MPASS(sc->toecaps == 0);
5617 		sc->toecaps = 0;
5618 
5619 		param[0] = FW_PARAM_DEV(NTID);
5620 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5621 		if (rc != 0) {
5622 			device_printf(sc->dev,
5623 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5624 			return (rc);
5625 		}
5626 		sc->tids.ntids = val[0];
5627 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5628 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5629 			sc->tids.ntids -= sc->tids.nhpftids;
5630 		}
5631 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5632 		sc->params.hash_filter = 1;
5633 	}
5634 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5635 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5636 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5637 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5638 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5639 		if (rc != 0) {
5640 			device_printf(sc->dev,
5641 			    "failed to query NIC parameters: %d.\n", rc);
5642 			return (rc);
5643 		}
5644 		if ((int)val[1] > (int)val[0]) {
5645 			sc->tids.etid_base = val[0];
5646 			sc->tids.etid_end = val[1];
5647 			sc->tids.netids = val[1] - val[0] + 1;
5648 			sc->params.eo_wr_cred = val[2];
5649 			sc->params.ethoffload = 1;
5650 		}
5651 	}
5652 	if (sc->toecaps) {
5653 		/* query offload-related parameters */
5654 		param[0] = FW_PARAM_DEV(NTID);
5655 		param[1] = FW_PARAM_PFVF(SERVER_START);
5656 		param[2] = FW_PARAM_PFVF(SERVER_END);
5657 		param[3] = FW_PARAM_PFVF(TDDP_START);
5658 		param[4] = FW_PARAM_PFVF(TDDP_END);
5659 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5660 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5661 		if (rc != 0) {
5662 			device_printf(sc->dev,
5663 			    "failed to query TOE parameters: %d.\n", rc);
5664 			return (rc);
5665 		}
5666 		sc->tids.ntids = val[0];
5667 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5668 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5669 			sc->tids.ntids -= sc->tids.nhpftids;
5670 		}
5671 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5672 		if ((int)val[2] > (int)val[1]) {
5673 			sc->tids.stid_base = val[1];
5674 			sc->tids.nstids = val[2] - val[1] + 1;
5675 		}
5676 		sc->vres.ddp.start = val[3];
5677 		sc->vres.ddp.size = val[4] - val[3] + 1;
5678 		sc->params.ofldq_wr_cred = val[5];
5679 		sc->params.offload = 1;
5680 	} else {
5681 		/*
5682 		 * The firmware attempts memfree TOE configuration for -SO cards
5683 		 * and will report toecaps=0 if it runs out of resources (this
5684 		 * depends on the config file).  It may not report 0 for other
5685 		 * capabilities dependent on the TOE in this case.  Set them to
5686 		 * 0 here so that the driver doesn't bother tracking resources
5687 		 * that will never be used.
5688 		 */
5689 		sc->iscsicaps = 0;
5690 		sc->rdmacaps = 0;
5691 	}
5692 	if (sc->rdmacaps) {
5693 		param[0] = FW_PARAM_PFVF(STAG_START);
5694 		param[1] = FW_PARAM_PFVF(STAG_END);
5695 		param[2] = FW_PARAM_PFVF(RQ_START);
5696 		param[3] = FW_PARAM_PFVF(RQ_END);
5697 		param[4] = FW_PARAM_PFVF(PBL_START);
5698 		param[5] = FW_PARAM_PFVF(PBL_END);
5699 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5700 		if (rc != 0) {
5701 			device_printf(sc->dev,
5702 			    "failed to query RDMA parameters(1): %d.\n", rc);
5703 			return (rc);
5704 		}
5705 		sc->vres.stag.start = val[0];
5706 		sc->vres.stag.size = val[1] - val[0] + 1;
5707 		sc->vres.rq.start = val[2];
5708 		sc->vres.rq.size = val[3] - val[2] + 1;
5709 		sc->vres.pbl.start = val[4];
5710 		sc->vres.pbl.size = val[5] - val[4] + 1;
5711 
5712 		param[0] = FW_PARAM_PFVF(SQRQ_START);
5713 		param[1] = FW_PARAM_PFVF(SQRQ_END);
5714 		param[2] = FW_PARAM_PFVF(CQ_START);
5715 		param[3] = FW_PARAM_PFVF(CQ_END);
5716 		param[4] = FW_PARAM_PFVF(OCQ_START);
5717 		param[5] = FW_PARAM_PFVF(OCQ_END);
5718 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5719 		if (rc != 0) {
5720 			device_printf(sc->dev,
5721 			    "failed to query RDMA parameters(2): %d.\n", rc);
5722 			return (rc);
5723 		}
5724 		sc->vres.qp.start = val[0];
5725 		sc->vres.qp.size = val[1] - val[0] + 1;
5726 		sc->vres.cq.start = val[2];
5727 		sc->vres.cq.size = val[3] - val[2] + 1;
5728 		sc->vres.ocq.start = val[4];
5729 		sc->vres.ocq.size = val[5] - val[4] + 1;
5730 
5731 		param[0] = FW_PARAM_PFVF(SRQ_START);
5732 		param[1] = FW_PARAM_PFVF(SRQ_END);
5733 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
5734 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5735 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5736 		if (rc != 0) {
5737 			device_printf(sc->dev,
5738 			    "failed to query RDMA parameters(3): %d.\n", rc);
5739 			return (rc);
5740 		}
5741 		sc->vres.srq.start = val[0];
5742 		sc->vres.srq.size = val[1] - val[0] + 1;
5743 		sc->params.max_ordird_qp = val[2];
5744 		sc->params.max_ird_adapter = val[3];
5745 	}
5746 	if (sc->iscsicaps) {
5747 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5748 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5749 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5750 		if (rc != 0) {
5751 			device_printf(sc->dev,
5752 			    "failed to query iSCSI parameters: %d.\n", rc);
5753 			return (rc);
5754 		}
5755 		sc->vres.iscsi.start = val[0];
5756 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5757 	}
5758 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5759 		param[0] = FW_PARAM_PFVF(TLS_START);
5760 		param[1] = FW_PARAM_PFVF(TLS_END);
5761 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5762 		if (rc != 0) {
5763 			device_printf(sc->dev,
5764 			    "failed to query TLS parameters: %d.\n", rc);
5765 			return (rc);
5766 		}
5767 		sc->vres.key.start = val[0];
5768 		sc->vres.key.size = val[1] - val[0] + 1;
5769 	}
5770 
5771 	/*
5772 	 * We've got the params we wanted to query directly from the firmware.
5773 	 * Grab some others via other means.
5774 	 */
5775 	t4_init_sge_params(sc);
5776 	t4_init_tp_params(sc);
5777 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5778 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5779 
5780 	rc = t4_verify_chip_settings(sc);
5781 	if (rc != 0)
5782 		return (rc);
5783 	t4_init_rx_buf_info(sc);
5784 
5785 	return (rc);
5786 }
5787 
5788 #ifdef KERN_TLS
5789 static void
ktls_tick(void * arg)5790 ktls_tick(void *arg)
5791 {
5792 	struct adapter *sc;
5793 	uint32_t tstamp;
5794 
5795 	sc = arg;
5796 	tstamp = tcp_ts_getticks();
5797 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
5798 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
5799 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
5800 }
5801 
5802 static int
t6_config_kern_tls(struct adapter * sc,bool enable)5803 t6_config_kern_tls(struct adapter *sc, bool enable)
5804 {
5805 	int rc;
5806 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5807 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
5808 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
5809 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
5810 
5811 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
5812 	if (rc != 0) {
5813 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
5814 		    enable ?  "enable" : "disable", rc);
5815 		return (rc);
5816 	}
5817 
5818 	if (enable) {
5819 		sc->flags |= KERN_TLS_ON;
5820 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
5821 		    C_HARDCLOCK);
5822 	} else {
5823 		sc->flags &= ~KERN_TLS_ON;
5824 		callout_stop(&sc->ktls_tick);
5825 	}
5826 
5827 	return (rc);
5828 }
5829 #endif
5830 
5831 static int
set_params__post_init(struct adapter * sc)5832 set_params__post_init(struct adapter *sc)
5833 {
5834 	uint32_t mask, param, val;
5835 #ifdef TCP_OFFLOAD
5836 	int i, v, shift;
5837 #endif
5838 
5839 	/* ask for encapsulated CPLs */
5840 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5841 	val = 1;
5842 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5843 
5844 	/* Enable 32b port caps if the firmware supports it. */
5845 	param = FW_PARAM_PFVF(PORT_CAPS32);
5846 	val = 1;
5847 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
5848 		sc->params.port_caps32 = 1;
5849 
5850 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
5851 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
5852 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
5853 	    V_MASKFILTER(val - 1));
5854 
5855 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
5856 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
5857 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5858 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
5859 	val = 0;
5860 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
5861 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
5862 		    F_ATTACKFILTERENABLE);
5863 		val |= F_DROPERRORATTACK;
5864 	}
5865 	if (t4_drop_ip_fragments != 0) {
5866 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
5867 		    F_FRAGMENTDROP);
5868 		val |= F_DROPERRORFRAG;
5869 	}
5870 	if (t4_drop_pkts_with_l2_errors != 0)
5871 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
5872 	if (t4_drop_pkts_with_l3_errors != 0) {
5873 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
5874 		    F_DROPERRORCSUMIP;
5875 	}
5876 	if (t4_drop_pkts_with_l4_errors != 0) {
5877 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5878 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
5879 	}
5880 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
5881 
5882 #ifdef TCP_OFFLOAD
5883 	/*
5884 	 * Override the TOE timers with user provided tunables.  This is not the
5885 	 * recommended way to change the timers (the firmware config file is) so
5886 	 * these tunables are not documented.
5887 	 *
5888 	 * All the timer tunables are in microseconds.
5889 	 */
5890 	if (t4_toe_keepalive_idle != 0) {
5891 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
5892 		v &= M_KEEPALIVEIDLE;
5893 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
5894 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
5895 	}
5896 	if (t4_toe_keepalive_interval != 0) {
5897 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
5898 		v &= M_KEEPALIVEINTVL;
5899 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
5900 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
5901 	}
5902 	if (t4_toe_keepalive_count != 0) {
5903 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
5904 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5905 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
5906 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
5907 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
5908 	}
5909 	if (t4_toe_rexmt_min != 0) {
5910 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
5911 		v &= M_RXTMIN;
5912 		t4_set_reg_field(sc, A_TP_RXT_MIN,
5913 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
5914 	}
5915 	if (t4_toe_rexmt_max != 0) {
5916 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
5917 		v &= M_RXTMAX;
5918 		t4_set_reg_field(sc, A_TP_RXT_MAX,
5919 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
5920 	}
5921 	if (t4_toe_rexmt_count != 0) {
5922 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
5923 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5924 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
5925 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
5926 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
5927 	}
5928 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
5929 		if (t4_toe_rexmt_backoff[i] != -1) {
5930 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
5931 			shift = (i & 3) << 3;
5932 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
5933 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
5934 		}
5935 	}
5936 #endif
5937 
5938 	/*
5939 	 * Limit TOE connections to 2 reassembly "islands".  This is
5940 	 * required to permit migrating TOE connections to either
5941 	 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
5942 	 */
5943 	t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
5944 	    V_PASSMODE(2));
5945 
5946 #ifdef KERN_TLS
5947 	if (is_ktls(sc)) {
5948 		sc->tlst.inline_keys = t4_tls_inline_keys;
5949 		sc->tlst.combo_wrs = t4_tls_combo_wrs;
5950 		if (t4_kern_tls != 0 && is_t6(sc))
5951 			t6_config_kern_tls(sc, true);
5952 	}
5953 #endif
5954 	return (0);
5955 }
5956 
5957 #undef FW_PARAM_PFVF
5958 #undef FW_PARAM_DEV
5959 
5960 static void
t4_set_desc(struct adapter * sc)5961 t4_set_desc(struct adapter *sc)
5962 {
5963 	struct adapter_params *p = &sc->params;
5964 
5965 	device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
5966 }
5967 
5968 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)5969 ifmedia_add4(struct ifmedia *ifm, int m)
5970 {
5971 
5972 	ifmedia_add(ifm, m, 0, NULL);
5973 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
5974 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
5975 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
5976 }
5977 
5978 /*
5979  * This is the selected media, which is not quite the same as the active media.
5980  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
5981  * and active are not the same, and "media: Ethernet selected" otherwise.
5982  */
5983 static void
set_current_media(struct port_info * pi)5984 set_current_media(struct port_info *pi)
5985 {
5986 	struct link_config *lc;
5987 	struct ifmedia *ifm;
5988 	int mword;
5989 	u_int speed;
5990 
5991 	PORT_LOCK_ASSERT_OWNED(pi);
5992 
5993 	/* Leave current media alone if it's already set to IFM_NONE. */
5994 	ifm = &pi->media;
5995 	if (ifm->ifm_cur != NULL &&
5996 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
5997 		return;
5998 
5999 	lc = &pi->link_cfg;
6000 	if (lc->requested_aneg != AUTONEG_DISABLE &&
6001 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
6002 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
6003 		return;
6004 	}
6005 	mword = IFM_ETHER | IFM_FDX;
6006 	if (lc->requested_fc & PAUSE_TX)
6007 		mword |= IFM_ETH_TXPAUSE;
6008 	if (lc->requested_fc & PAUSE_RX)
6009 		mword |= IFM_ETH_RXPAUSE;
6010 	if (lc->requested_speed == 0)
6011 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
6012 	else
6013 		speed = lc->requested_speed;
6014 	mword |= port_mword(pi, speed_to_fwcap(speed));
6015 	ifmedia_set(ifm, mword);
6016 }
6017 
6018 /*
6019  * Returns true if the ifmedia list for the port cannot change.
6020  */
6021 static bool
fixed_ifmedia(struct port_info * pi)6022 fixed_ifmedia(struct port_info *pi)
6023 {
6024 
6025 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
6026 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
6027 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
6028 	    pi->port_type == FW_PORT_TYPE_KX4 ||
6029 	    pi->port_type == FW_PORT_TYPE_KX ||
6030 	    pi->port_type == FW_PORT_TYPE_KR ||
6031 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
6032 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
6033 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
6034 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
6035 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
6036 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
6037 }
6038 
6039 static void
build_medialist(struct port_info * pi)6040 build_medialist(struct port_info *pi)
6041 {
6042 	uint32_t ss, speed;
6043 	int unknown, mword, bit;
6044 	struct link_config *lc;
6045 	struct ifmedia *ifm;
6046 
6047 	PORT_LOCK_ASSERT_OWNED(pi);
6048 
6049 	if (pi->flags & FIXED_IFMEDIA)
6050 		return;
6051 
6052 	/*
6053 	 * Rebuild the ifmedia list.
6054 	 */
6055 	ifm = &pi->media;
6056 	ifmedia_removeall(ifm);
6057 	lc = &pi->link_cfg;
6058 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
6059 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
6060 		MPASS(ss != 0);
6061 no_media:
6062 		MPASS(LIST_EMPTY(&ifm->ifm_list));
6063 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
6064 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
6065 		return;
6066 	}
6067 
6068 	unknown = 0;
6069 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
6070 		speed = 1 << bit;
6071 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
6072 		if (ss & speed) {
6073 			mword = port_mword(pi, speed);
6074 			if (mword == IFM_NONE) {
6075 				goto no_media;
6076 			} else if (mword == IFM_UNKNOWN)
6077 				unknown++;
6078 			else
6079 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
6080 		}
6081 	}
6082 	if (unknown > 0) /* Add one unknown for all unknown media types. */
6083 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
6084 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
6085 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
6086 
6087 	set_current_media(pi);
6088 }
6089 
6090 /*
6091  * Initialize the requested fields in the link config based on driver tunables.
6092  */
6093 static void
init_link_config(struct port_info * pi)6094 init_link_config(struct port_info *pi)
6095 {
6096 	struct link_config *lc = &pi->link_cfg;
6097 
6098 	PORT_LOCK_ASSERT_OWNED(pi);
6099 
6100 	lc->requested_caps = 0;
6101 	lc->requested_speed = 0;
6102 
6103 	if (t4_autoneg == 0)
6104 		lc->requested_aneg = AUTONEG_DISABLE;
6105 	else if (t4_autoneg == 1)
6106 		lc->requested_aneg = AUTONEG_ENABLE;
6107 	else
6108 		lc->requested_aneg = AUTONEG_AUTO;
6109 
6110 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
6111 	    PAUSE_AUTONEG);
6112 
6113 	if (t4_fec & FEC_AUTO)
6114 		lc->requested_fec = FEC_AUTO;
6115 	else if (t4_fec == 0)
6116 		lc->requested_fec = FEC_NONE;
6117 	else {
6118 		/* -1 is handled by the FEC_AUTO block above and not here. */
6119 		lc->requested_fec = t4_fec &
6120 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6121 		if (lc->requested_fec == 0)
6122 			lc->requested_fec = FEC_AUTO;
6123 	}
6124 	if (t4_force_fec < 0)
6125 		lc->force_fec = -1;
6126 	else if (t4_force_fec > 0)
6127 		lc->force_fec = 1;
6128 	else
6129 		lc->force_fec = 0;
6130 }
6131 
6132 /*
6133  * Makes sure that all requested settings comply with what's supported by the
6134  * port.  Returns the number of settings that were invalid and had to be fixed.
6135  */
6136 static int
fixup_link_config(struct port_info * pi)6137 fixup_link_config(struct port_info *pi)
6138 {
6139 	int n = 0;
6140 	struct link_config *lc = &pi->link_cfg;
6141 	uint32_t fwspeed;
6142 
6143 	PORT_LOCK_ASSERT_OWNED(pi);
6144 
6145 	/* Speed (when not autonegotiating) */
6146 	if (lc->requested_speed != 0) {
6147 		fwspeed = speed_to_fwcap(lc->requested_speed);
6148 		if ((fwspeed & lc->pcaps) == 0) {
6149 			n++;
6150 			lc->requested_speed = 0;
6151 		}
6152 	}
6153 
6154 	/* Link autonegotiation */
6155 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6156 	    lc->requested_aneg == AUTONEG_DISABLE ||
6157 	    lc->requested_aneg == AUTONEG_AUTO);
6158 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6159 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6160 		n++;
6161 		lc->requested_aneg = AUTONEG_AUTO;
6162 	}
6163 
6164 	/* Flow control */
6165 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6166 	if (lc->requested_fc & PAUSE_TX &&
6167 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6168 		n++;
6169 		lc->requested_fc &= ~PAUSE_TX;
6170 	}
6171 	if (lc->requested_fc & PAUSE_RX &&
6172 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6173 		n++;
6174 		lc->requested_fc &= ~PAUSE_RX;
6175 	}
6176 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6177 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6178 		n++;
6179 		lc->requested_fc |= PAUSE_AUTONEG;
6180 	}
6181 
6182 	/* FEC */
6183 	if ((lc->requested_fec & FEC_RS &&
6184 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6185 	    (lc->requested_fec & FEC_BASER_RS &&
6186 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6187 		n++;
6188 		lc->requested_fec = FEC_AUTO;
6189 	}
6190 
6191 	return (n);
6192 }
6193 
6194 /*
6195  * Apply the requested L1 settings, which are expected to be valid, to the
6196  * hardware.
6197  */
6198 static int
apply_link_config(struct port_info * pi)6199 apply_link_config(struct port_info *pi)
6200 {
6201 	struct adapter *sc = pi->adapter;
6202 	struct link_config *lc = &pi->link_cfg;
6203 	int rc;
6204 
6205 #ifdef INVARIANTS
6206 	ASSERT_SYNCHRONIZED_OP(sc);
6207 	PORT_LOCK_ASSERT_OWNED(pi);
6208 
6209 	if (lc->requested_aneg == AUTONEG_ENABLE)
6210 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6211 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6212 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6213 	if (lc->requested_fc & PAUSE_TX)
6214 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6215 	if (lc->requested_fc & PAUSE_RX)
6216 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6217 	if (lc->requested_fec & FEC_RS)
6218 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6219 	if (lc->requested_fec & FEC_BASER_RS)
6220 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6221 #endif
6222 	if (!(sc->flags & IS_VF)) {
6223 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
6224 		if (rc != 0) {
6225 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6226 			return (rc);
6227 		}
6228 	}
6229 
6230 	/*
6231 	 * An L1_CFG will almost always result in a link-change event if the
6232 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6233 	 * the notification is processed.  If the link is down then the actual
6234 	 * settings are meaningless.
6235 	 *
6236 	 * This takes care of the case where a change in the L1 settings may not
6237 	 * result in a notification.
6238 	 */
6239 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6240 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6241 
6242 	return (0);
6243 }
6244 
6245 #define FW_MAC_EXACT_CHUNK	7
6246 struct mcaddr_ctx {
6247 	if_t ifp;
6248 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6249 	uint64_t hash;
6250 	int i;
6251 	int del;
6252 	int rc;
6253 };
6254 
6255 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6256 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6257 {
6258 	struct mcaddr_ctx *ctx = arg;
6259 	struct vi_info *vi = if_getsoftc(ctx->ifp);
6260 	struct port_info *pi = vi->pi;
6261 	struct adapter *sc = pi->adapter;
6262 
6263 	if (ctx->rc < 0)
6264 		return (0);
6265 
6266 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6267 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6268 	ctx->i++;
6269 
6270 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6271 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6272 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6273 		if (ctx->rc < 0) {
6274 			int j;
6275 
6276 			for (j = 0; j < ctx->i; j++) {
6277 				if_printf(ctx->ifp,
6278 				    "failed to add mc address"
6279 				    " %02x:%02x:%02x:"
6280 				    "%02x:%02x:%02x rc=%d\n",
6281 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6282 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6283 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6284 				    -ctx->rc);
6285 			}
6286 			return (0);
6287 		}
6288 		ctx->del = 0;
6289 		ctx->i = 0;
6290 	}
6291 
6292 	return (1);
6293 }
6294 
6295 /*
6296  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6297  * indicates which parameters should be programmed (the rest are left alone).
6298  */
6299 int
update_mac_settings(if_t ifp,int flags)6300 update_mac_settings(if_t ifp, int flags)
6301 {
6302 	int rc = 0;
6303 	struct vi_info *vi = if_getsoftc(ifp);
6304 	struct port_info *pi = vi->pi;
6305 	struct adapter *sc = pi->adapter;
6306 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6307 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6308 
6309 	ASSERT_SYNCHRONIZED_OP(sc);
6310 	KASSERT(flags, ("%s: not told what to update.", __func__));
6311 
6312 	if (flags & XGMAC_MTU)
6313 		mtu = if_getmtu(ifp);
6314 
6315 	if (flags & XGMAC_PROMISC)
6316 		promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6317 
6318 	if (flags & XGMAC_ALLMULTI)
6319 		allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6320 
6321 	if (flags & XGMAC_VLANEX)
6322 		vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6323 
6324 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6325 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6326 		    allmulti, 1, vlanex, false);
6327 		if (rc) {
6328 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6329 			    rc);
6330 			return (rc);
6331 		}
6332 	}
6333 
6334 	if (flags & XGMAC_UCADDR) {
6335 		uint8_t ucaddr[ETHER_ADDR_LEN];
6336 
6337 		bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6338 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6339 		    ucaddr, true, &vi->smt_idx);
6340 		if (rc < 0) {
6341 			rc = -rc;
6342 			if_printf(ifp, "change_mac failed: %d\n", rc);
6343 			return (rc);
6344 		} else {
6345 			vi->xact_addr_filt = rc;
6346 			rc = 0;
6347 		}
6348 	}
6349 
6350 	if (flags & XGMAC_MCADDRS) {
6351 		struct epoch_tracker et;
6352 		struct mcaddr_ctx ctx;
6353 		int j;
6354 
6355 		ctx.ifp = ifp;
6356 		ctx.hash = 0;
6357 		ctx.i = 0;
6358 		ctx.del = 1;
6359 		ctx.rc = 0;
6360 		/*
6361 		 * Unlike other drivers, we accumulate list of pointers into
6362 		 * interface address lists and we need to keep it safe even
6363 		 * after if_foreach_llmaddr() returns, thus we must enter the
6364 		 * network epoch.
6365 		 */
6366 		NET_EPOCH_ENTER(et);
6367 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6368 		if (ctx.rc < 0) {
6369 			NET_EPOCH_EXIT(et);
6370 			rc = -ctx.rc;
6371 			return (rc);
6372 		}
6373 		if (ctx.i > 0) {
6374 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6375 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6376 			NET_EPOCH_EXIT(et);
6377 			if (rc < 0) {
6378 				rc = -rc;
6379 				for (j = 0; j < ctx.i; j++) {
6380 					if_printf(ifp,
6381 					    "failed to add mcast address"
6382 					    " %02x:%02x:%02x:"
6383 					    "%02x:%02x:%02x rc=%d\n",
6384 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6385 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6386 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6387 					    rc);
6388 				}
6389 				return (rc);
6390 			}
6391 			ctx.del = 0;
6392 		} else
6393 			NET_EPOCH_EXIT(et);
6394 
6395 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6396 		if (rc != 0)
6397 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6398 			    rc);
6399 		if (ctx.del == 0) {
6400 			/* We clobbered the VXLAN entry if there was one. */
6401 			pi->vxlan_tcam_entry = false;
6402 		}
6403 	}
6404 
6405 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6406 	    pi->vxlan_tcam_entry == false) {
6407 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6408 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6409 		    true);
6410 		if (rc < 0) {
6411 			rc = -rc;
6412 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6413 			    rc);
6414 		} else {
6415 			MPASS(rc == sc->rawf_base + pi->port_id);
6416 			rc = 0;
6417 			pi->vxlan_tcam_entry = true;
6418 		}
6419 	}
6420 
6421 	return (rc);
6422 }
6423 
6424 /*
6425  * {begin|end}_synchronized_op must be called from the same thread.
6426  */
6427 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6428 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6429     char *wmesg)
6430 {
6431 	int rc;
6432 
6433 #ifdef WITNESS
6434 	/* the caller thinks it's ok to sleep, but is it really? */
6435 	if (flags & SLEEP_OK)
6436 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
6437 #endif
6438 	ADAPTER_LOCK(sc);
6439 	for (;;) {
6440 
6441 		if (vi && IS_DETACHING(vi)) {
6442 			rc = ENXIO;
6443 			goto done;
6444 		}
6445 
6446 		if (!IS_BUSY(sc)) {
6447 			rc = 0;
6448 			break;
6449 		}
6450 
6451 		if (!(flags & SLEEP_OK)) {
6452 			rc = EBUSY;
6453 			goto done;
6454 		}
6455 
6456 		if (mtx_sleep(&sc->flags, &sc->sc_lock,
6457 		    flags & INTR_OK ? PCATCH : 0, wmesg, 0)) {
6458 			rc = EINTR;
6459 			goto done;
6460 		}
6461 	}
6462 
6463 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6464 	SET_BUSY(sc);
6465 #ifdef INVARIANTS
6466 	sc->last_op = wmesg;
6467 	sc->last_op_thr = curthread;
6468 	sc->last_op_flags = flags;
6469 #endif
6470 
6471 done:
6472 	if (!(flags & HOLD_LOCK) || rc)
6473 		ADAPTER_UNLOCK(sc);
6474 
6475 	return (rc);
6476 }
6477 
6478 /*
6479  * Tell if_ioctl and if_init that the VI is going away.  This is
6480  * special variant of begin_synchronized_op and must be paired with a
6481  * call to end_vi_detach.
6482  */
6483 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6484 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6485 {
6486 	ADAPTER_LOCK(sc);
6487 	SET_DETACHING(vi);
6488 	wakeup(&sc->flags);
6489 	while (IS_BUSY(sc))
6490 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6491 	SET_BUSY(sc);
6492 #ifdef INVARIANTS
6493 	sc->last_op = "t4detach";
6494 	sc->last_op_thr = curthread;
6495 	sc->last_op_flags = 0;
6496 #endif
6497 	ADAPTER_UNLOCK(sc);
6498 }
6499 
6500 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6501 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6502 {
6503 	ADAPTER_LOCK(sc);
6504 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6505 	CLR_BUSY(sc);
6506 	CLR_DETACHING(vi);
6507 	wakeup(&sc->flags);
6508 	ADAPTER_UNLOCK(sc);
6509 }
6510 
6511 /*
6512  * {begin|end}_synchronized_op must be called from the same thread.
6513  */
6514 void
end_synchronized_op(struct adapter * sc,int flags)6515 end_synchronized_op(struct adapter *sc, int flags)
6516 {
6517 
6518 	if (flags & LOCK_HELD)
6519 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6520 	else
6521 		ADAPTER_LOCK(sc);
6522 
6523 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6524 	CLR_BUSY(sc);
6525 	wakeup(&sc->flags);
6526 	ADAPTER_UNLOCK(sc);
6527 }
6528 
6529 static int
cxgbe_init_synchronized(struct vi_info * vi)6530 cxgbe_init_synchronized(struct vi_info *vi)
6531 {
6532 	struct port_info *pi = vi->pi;
6533 	struct adapter *sc = pi->adapter;
6534 	if_t ifp = vi->ifp;
6535 	int rc = 0, i;
6536 	struct sge_txq *txq;
6537 
6538 	ASSERT_SYNCHRONIZED_OP(sc);
6539 
6540 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6541 		return (0);	/* already running */
6542 
6543 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6544 		return (rc);	/* error message displayed already */
6545 
6546 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6547 		return (rc); /* error message displayed already */
6548 
6549 	rc = update_mac_settings(ifp, XGMAC_ALL);
6550 	if (rc)
6551 		goto done;	/* error message displayed already */
6552 
6553 	PORT_LOCK(pi);
6554 	if (pi->up_vis == 0) {
6555 		t4_update_port_info(pi);
6556 		fixup_link_config(pi);
6557 		build_medialist(pi);
6558 		apply_link_config(pi);
6559 	}
6560 
6561 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6562 	if (rc != 0) {
6563 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6564 		PORT_UNLOCK(pi);
6565 		goto done;
6566 	}
6567 
6568 	/*
6569 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6570 	 * if this changes.
6571 	 */
6572 
6573 	for_each_txq(vi, i, txq) {
6574 		TXQ_LOCK(txq);
6575 		txq->eq.flags |= EQ_ENABLED;
6576 		TXQ_UNLOCK(txq);
6577 	}
6578 
6579 	/*
6580 	 * The first iq of the first port to come up is used for tracing.
6581 	 */
6582 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6583 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6584 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
6585 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
6586 		    V_QUEUENUMBER(sc->traceq));
6587 		pi->flags |= HAS_TRACEQ;
6588 	}
6589 
6590 	/* all ok */
6591 	pi->up_vis++;
6592 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6593 	if (pi->link_cfg.link_ok)
6594 		t4_os_link_changed(pi);
6595 	PORT_UNLOCK(pi);
6596 
6597 	mtx_lock(&vi->tick_mtx);
6598 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6599 		callout_reset(&vi->tick, hz, vi_tick, vi);
6600 	else
6601 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6602 	mtx_unlock(&vi->tick_mtx);
6603 done:
6604 	if (rc != 0)
6605 		cxgbe_uninit_synchronized(vi);
6606 
6607 	return (rc);
6608 }
6609 
6610 /*
6611  * Idempotent.
6612  */
6613 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6614 cxgbe_uninit_synchronized(struct vi_info *vi)
6615 {
6616 	struct port_info *pi = vi->pi;
6617 	struct adapter *sc = pi->adapter;
6618 	if_t ifp = vi->ifp;
6619 	int rc, i;
6620 	struct sge_txq *txq;
6621 
6622 	ASSERT_SYNCHRONIZED_OP(sc);
6623 
6624 	if (!(vi->flags & VI_INIT_DONE)) {
6625 		if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6626 			KASSERT(0, ("uninited VI is running"));
6627 			if_printf(ifp, "uninited VI with running ifnet.  "
6628 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6629 			    "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6630 			    if_getdrvflags(ifp));
6631 		}
6632 		return (0);
6633 	}
6634 
6635 	/*
6636 	 * Disable the VI so that all its data in either direction is discarded
6637 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6638 	 * tick) intact as the TP can deliver negative advice or data that it's
6639 	 * holding in its RAM (for an offloaded connection) even after the VI is
6640 	 * disabled.
6641 	 */
6642 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6643 	if (rc) {
6644 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6645 		return (rc);
6646 	}
6647 
6648 	for_each_txq(vi, i, txq) {
6649 		TXQ_LOCK(txq);
6650 		txq->eq.flags &= ~EQ_ENABLED;
6651 		TXQ_UNLOCK(txq);
6652 	}
6653 
6654 	mtx_lock(&vi->tick_mtx);
6655 	callout_stop(&vi->tick);
6656 	mtx_unlock(&vi->tick_mtx);
6657 
6658 	PORT_LOCK(pi);
6659 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6660 		PORT_UNLOCK(pi);
6661 		return (0);
6662 	}
6663 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6664 	pi->up_vis--;
6665 	if (pi->up_vis > 0) {
6666 		PORT_UNLOCK(pi);
6667 		return (0);
6668 	}
6669 
6670 	pi->link_cfg.link_ok = false;
6671 	pi->link_cfg.speed = 0;
6672 	pi->link_cfg.link_down_rc = 255;
6673 	t4_os_link_changed(pi);
6674 	PORT_UNLOCK(pi);
6675 
6676 	return (0);
6677 }
6678 
6679 /*
6680  * It is ok for this function to fail midway and return right away.  t4_detach
6681  * will walk the entire sc->irq list and clean up whatever is valid.
6682  */
6683 int
t4_setup_intr_handlers(struct adapter * sc)6684 t4_setup_intr_handlers(struct adapter *sc)
6685 {
6686 	int rc, rid, p, q, v;
6687 	char s[8];
6688 	struct irq *irq;
6689 	struct port_info *pi;
6690 	struct vi_info *vi;
6691 	struct sge *sge = &sc->sge;
6692 	struct sge_rxq *rxq;
6693 #ifdef TCP_OFFLOAD
6694 	struct sge_ofld_rxq *ofld_rxq;
6695 #endif
6696 #ifdef DEV_NETMAP
6697 	struct sge_nm_rxq *nm_rxq;
6698 #endif
6699 #ifdef RSS
6700 	int nbuckets = rss_getnumbuckets();
6701 #endif
6702 
6703 	/*
6704 	 * Setup interrupts.
6705 	 */
6706 	irq = &sc->irq[0];
6707 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6708 	if (forwarding_intr_to_fwq(sc))
6709 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6710 
6711 	/* Multiple interrupts. */
6712 	if (sc->flags & IS_VF)
6713 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6714 		    ("%s: too few intr.", __func__));
6715 	else
6716 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6717 		    ("%s: too few intr.", __func__));
6718 
6719 	/* The first one is always error intr on PFs */
6720 	if (!(sc->flags & IS_VF)) {
6721 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6722 		if (rc != 0)
6723 			return (rc);
6724 		irq++;
6725 		rid++;
6726 	}
6727 
6728 	/* The second one is always the firmware event queue (first on VFs) */
6729 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6730 	if (rc != 0)
6731 		return (rc);
6732 	irq++;
6733 	rid++;
6734 
6735 	for_each_port(sc, p) {
6736 		pi = sc->port[p];
6737 		for_each_vi(pi, v, vi) {
6738 			vi->first_intr = rid - 1;
6739 
6740 			if (vi->nnmrxq > 0) {
6741 				int n = max(vi->nrxq, vi->nnmrxq);
6742 
6743 				rxq = &sge->rxq[vi->first_rxq];
6744 #ifdef DEV_NETMAP
6745 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6746 #endif
6747 				for (q = 0; q < n; q++) {
6748 					snprintf(s, sizeof(s), "%x%c%x", p,
6749 					    'a' + v, q);
6750 					if (q < vi->nrxq)
6751 						irq->rxq = rxq++;
6752 #ifdef DEV_NETMAP
6753 					if (q < vi->nnmrxq)
6754 						irq->nm_rxq = nm_rxq++;
6755 
6756 					if (irq->nm_rxq != NULL &&
6757 					    irq->rxq == NULL) {
6758 						/* Netmap rx only */
6759 						rc = t4_alloc_irq(sc, irq, rid,
6760 						    t4_nm_intr, irq->nm_rxq, s);
6761 					}
6762 					if (irq->nm_rxq != NULL &&
6763 					    irq->rxq != NULL) {
6764 						/* NIC and Netmap rx */
6765 						rc = t4_alloc_irq(sc, irq, rid,
6766 						    t4_vi_intr, irq, s);
6767 					}
6768 #endif
6769 					if (irq->rxq != NULL &&
6770 					    irq->nm_rxq == NULL) {
6771 						/* NIC rx only */
6772 						rc = t4_alloc_irq(sc, irq, rid,
6773 						    t4_intr, irq->rxq, s);
6774 					}
6775 					if (rc != 0)
6776 						return (rc);
6777 #ifdef RSS
6778 					if (q < vi->nrxq) {
6779 						bus_bind_intr(sc->dev, irq->res,
6780 						    rss_getcpu(q % nbuckets));
6781 					}
6782 #endif
6783 					irq++;
6784 					rid++;
6785 					vi->nintr++;
6786 				}
6787 			} else {
6788 				for_each_rxq(vi, q, rxq) {
6789 					snprintf(s, sizeof(s), "%x%c%x", p,
6790 					    'a' + v, q);
6791 					rc = t4_alloc_irq(sc, irq, rid,
6792 					    t4_intr, rxq, s);
6793 					if (rc != 0)
6794 						return (rc);
6795 #ifdef RSS
6796 					bus_bind_intr(sc->dev, irq->res,
6797 					    rss_getcpu(q % nbuckets));
6798 #endif
6799 					irq++;
6800 					rid++;
6801 					vi->nintr++;
6802 				}
6803 			}
6804 #ifdef TCP_OFFLOAD
6805 			for_each_ofld_rxq(vi, q, ofld_rxq) {
6806 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
6807 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
6808 				    ofld_rxq, s);
6809 				if (rc != 0)
6810 					return (rc);
6811 				irq++;
6812 				rid++;
6813 				vi->nintr++;
6814 			}
6815 #endif
6816 		}
6817 	}
6818 	MPASS(irq == &sc->irq[sc->intr_count]);
6819 
6820 	return (0);
6821 }
6822 
6823 static void
write_global_rss_key(struct adapter * sc)6824 write_global_rss_key(struct adapter *sc)
6825 {
6826 #ifdef RSS
6827 	int i;
6828 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6829 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6830 
6831 	CTASSERT(RSS_KEYSIZE == 40);
6832 
6833 	rss_getkey((void *)&raw_rss_key[0]);
6834 	for (i = 0; i < nitems(rss_key); i++) {
6835 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
6836 	}
6837 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
6838 #endif
6839 }
6840 
6841 /*
6842  * Idempotent.
6843  */
6844 static int
adapter_full_init(struct adapter * sc)6845 adapter_full_init(struct adapter *sc)
6846 {
6847 	int rc, i;
6848 
6849 	ASSERT_SYNCHRONIZED_OP(sc);
6850 
6851 	/*
6852 	 * queues that belong to the adapter (not any particular port).
6853 	 */
6854 	rc = t4_setup_adapter_queues(sc);
6855 	if (rc != 0)
6856 		return (rc);
6857 
6858 	MPASS(sc->params.nports <= nitems(sc->tq));
6859 	for (i = 0; i < sc->params.nports; i++) {
6860 		if (sc->tq[i] != NULL)
6861 			continue;
6862 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
6863 		    taskqueue_thread_enqueue, &sc->tq[i]);
6864 		if (sc->tq[i] == NULL) {
6865 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
6866 			return (ENOMEM);
6867 		}
6868 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
6869 		    device_get_nameunit(sc->dev), i);
6870 	}
6871 
6872 	if (!(sc->flags & IS_VF)) {
6873 		write_global_rss_key(sc);
6874 		t4_intr_enable(sc);
6875 	}
6876 	return (0);
6877 }
6878 
6879 int
adapter_init(struct adapter * sc)6880 adapter_init(struct adapter *sc)
6881 {
6882 	int rc;
6883 
6884 	ASSERT_SYNCHRONIZED_OP(sc);
6885 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
6886 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
6887 	    ("%s: FULL_INIT_DONE already", __func__));
6888 
6889 	rc = adapter_full_init(sc);
6890 	if (rc != 0)
6891 		adapter_full_uninit(sc);
6892 	else
6893 		sc->flags |= FULL_INIT_DONE;
6894 
6895 	return (rc);
6896 }
6897 
6898 /*
6899  * Idempotent.
6900  */
6901 static void
adapter_full_uninit(struct adapter * sc)6902 adapter_full_uninit(struct adapter *sc)
6903 {
6904 	int i;
6905 
6906 	t4_teardown_adapter_queues(sc);
6907 
6908 	for (i = 0; i < nitems(sc->tq); i++) {
6909 		if (sc->tq[i] == NULL)
6910 			continue;
6911 		taskqueue_free(sc->tq[i]);
6912 		sc->tq[i] = NULL;
6913 	}
6914 
6915 	sc->flags &= ~FULL_INIT_DONE;
6916 }
6917 
6918 #ifdef RSS
6919 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
6920     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
6921     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
6922     RSS_HASHTYPE_RSS_UDP_IPV6)
6923 
6924 /* Translates kernel hash types to hardware. */
6925 static int
hashconfig_to_hashen(int hashconfig)6926 hashconfig_to_hashen(int hashconfig)
6927 {
6928 	int hashen = 0;
6929 
6930 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
6931 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
6932 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
6933 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
6934 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
6935 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6936 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6937 	}
6938 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
6939 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6940 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6941 	}
6942 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
6943 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6944 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
6945 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6946 
6947 	return (hashen);
6948 }
6949 
6950 /* Translates hardware hash types to kernel. */
6951 static int
hashen_to_hashconfig(int hashen)6952 hashen_to_hashconfig(int hashen)
6953 {
6954 	int hashconfig = 0;
6955 
6956 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
6957 		/*
6958 		 * If UDP hashing was enabled it must have been enabled for
6959 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
6960 		 * enabling any 4-tuple hash is nonsense configuration.
6961 		 */
6962 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6963 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
6964 
6965 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6966 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
6967 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6968 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
6969 	}
6970 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6971 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
6972 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6973 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
6974 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
6975 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
6976 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
6977 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
6978 
6979 	return (hashconfig);
6980 }
6981 #endif
6982 
6983 /*
6984  * Idempotent.
6985  */
6986 static int
vi_full_init(struct vi_info * vi)6987 vi_full_init(struct vi_info *vi)
6988 {
6989 	struct adapter *sc = vi->adapter;
6990 	struct sge_rxq *rxq;
6991 	int rc, i, j;
6992 #ifdef RSS
6993 	int nbuckets = rss_getnumbuckets();
6994 	int hashconfig = rss_gethashconfig();
6995 	int extra;
6996 #endif
6997 
6998 	ASSERT_SYNCHRONIZED_OP(sc);
6999 
7000 	/*
7001 	 * Allocate tx/rx/fl queues for this VI.
7002 	 */
7003 	rc = t4_setup_vi_queues(vi);
7004 	if (rc != 0)
7005 		return (rc);
7006 
7007 	/*
7008 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
7009 	 */
7010 	if (vi->nrxq > vi->rss_size) {
7011 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
7012 		    "some queues will never receive traffic.\n", vi->nrxq,
7013 		    vi->rss_size);
7014 	} else if (vi->rss_size % vi->nrxq) {
7015 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
7016 		    "expect uneven traffic distribution.\n", vi->nrxq,
7017 		    vi->rss_size);
7018 	}
7019 #ifdef RSS
7020 	if (vi->nrxq != nbuckets) {
7021 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
7022 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
7023 	}
7024 #endif
7025 	if (vi->rss == NULL)
7026 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
7027 		    M_ZERO | M_WAITOK);
7028 	for (i = 0; i < vi->rss_size;) {
7029 #ifdef RSS
7030 		j = rss_get_indirection_to_bucket(i);
7031 		j %= vi->nrxq;
7032 		rxq = &sc->sge.rxq[vi->first_rxq + j];
7033 		vi->rss[i++] = rxq->iq.abs_id;
7034 #else
7035 		for_each_rxq(vi, j, rxq) {
7036 			vi->rss[i++] = rxq->iq.abs_id;
7037 			if (i == vi->rss_size)
7038 				break;
7039 		}
7040 #endif
7041 	}
7042 
7043 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
7044 	    vi->rss, vi->rss_size);
7045 	if (rc != 0) {
7046 		CH_ERR(vi, "rss_config failed: %d\n", rc);
7047 		return (rc);
7048 	}
7049 
7050 #ifdef RSS
7051 	vi->hashen = hashconfig_to_hashen(hashconfig);
7052 
7053 	/*
7054 	 * We may have had to enable some hashes even though the global config
7055 	 * wants them disabled.  This is a potential problem that must be
7056 	 * reported to the user.
7057 	 */
7058 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
7059 
7060 	/*
7061 	 * If we consider only the supported hash types, then the enabled hashes
7062 	 * are a superset of the requested hashes.  In other words, there cannot
7063 	 * be any supported hash that was requested but not enabled, but there
7064 	 * can be hashes that were not requested but had to be enabled.
7065 	 */
7066 	extra &= SUPPORTED_RSS_HASHTYPES;
7067 	MPASS((extra & hashconfig) == 0);
7068 
7069 	if (extra) {
7070 		CH_ALERT(vi,
7071 		    "global RSS config (0x%x) cannot be accommodated.\n",
7072 		    hashconfig);
7073 	}
7074 	if (extra & RSS_HASHTYPE_RSS_IPV4)
7075 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
7076 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
7077 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
7078 	if (extra & RSS_HASHTYPE_RSS_IPV6)
7079 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
7080 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
7081 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
7082 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
7083 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
7084 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
7085 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
7086 #else
7087 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
7088 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
7089 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
7090 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
7091 #endif
7092 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
7093 	    0, 0);
7094 	if (rc != 0) {
7095 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
7096 		return (rc);
7097 	}
7098 
7099 	return (0);
7100 }
7101 
7102 int
vi_init(struct vi_info * vi)7103 vi_init(struct vi_info *vi)
7104 {
7105 	int rc;
7106 
7107 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
7108 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
7109 	    ("%s: VI_INIT_DONE already", __func__));
7110 
7111 	rc = vi_full_init(vi);
7112 	if (rc != 0)
7113 		vi_full_uninit(vi);
7114 	else
7115 		vi->flags |= VI_INIT_DONE;
7116 
7117 	return (rc);
7118 }
7119 
7120 /*
7121  * Idempotent.
7122  */
7123 static void
vi_full_uninit(struct vi_info * vi)7124 vi_full_uninit(struct vi_info *vi)
7125 {
7126 
7127 	if (vi->flags & VI_INIT_DONE) {
7128 		quiesce_vi(vi);
7129 		free(vi->rss, M_CXGBE);
7130 		free(vi->nm_rss, M_CXGBE);
7131 	}
7132 
7133 	t4_teardown_vi_queues(vi);
7134 	vi->flags &= ~VI_INIT_DONE;
7135 }
7136 
7137 static void
quiesce_txq(struct sge_txq * txq)7138 quiesce_txq(struct sge_txq *txq)
7139 {
7140 	struct sge_eq *eq = &txq->eq;
7141 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7142 
7143 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7144 	MPASS(!(eq->flags & EQ_ENABLED));
7145 
7146 	/* Wait for the mp_ring to empty. */
7147 	while (!mp_ring_is_idle(txq->r)) {
7148 		mp_ring_check_drainage(txq->r, 4096);
7149 		pause("rquiesce", 1);
7150 	}
7151 	MPASS(txq->txp.npkt == 0);
7152 
7153 	if (eq->flags & EQ_HW_ALLOCATED) {
7154 		/*
7155 		 * Hardware is alive and working normally.  Wait for it to
7156 		 * finish and then wait for the driver to catch up and reclaim
7157 		 * all descriptors.
7158 		 */
7159 		while (spg->cidx != htobe16(eq->pidx))
7160 			pause("equiesce", 1);
7161 		while (eq->cidx != eq->pidx)
7162 			pause("dquiesce", 1);
7163 	} else {
7164 		/*
7165 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7166 		 * descriptors directly.
7167 		 */
7168 		TXQ_LOCK(txq);
7169 		while (eq->cidx != eq->pidx) {
7170 			struct mbuf *m, *nextpkt;
7171 			struct tx_sdesc *txsd;
7172 
7173 			txsd = &txq->sdesc[eq->cidx];
7174 			for (m = txsd->m; m != NULL; m = nextpkt) {
7175 				nextpkt = m->m_nextpkt;
7176 				m->m_nextpkt = NULL;
7177 				m_freem(m);
7178 			}
7179 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7180 		}
7181 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7182 		TXQ_UNLOCK(txq);
7183 	}
7184 }
7185 
7186 static void
quiesce_wrq(struct sge_wrq * wrq)7187 quiesce_wrq(struct sge_wrq *wrq)
7188 {
7189 	struct wrqe *wr;
7190 
7191 	TXQ_LOCK(wrq);
7192 	while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7193 		STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7194 #ifdef INVARIANTS
7195 		wrq->nwr_pending--;
7196 		wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7197 #endif
7198 		free(wr, M_CXGBE);
7199 	}
7200 	MPASS(wrq->nwr_pending == 0);
7201 	MPASS(wrq->ndesc_needed == 0);
7202 	wrq->nwr_pending = 0;
7203 	wrq->ndesc_needed = 0;
7204 	TXQ_UNLOCK(wrq);
7205 }
7206 
7207 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7208 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7209 {
7210 	/* Synchronize with the interrupt handler */
7211 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7212 		pause("iqfree", 1);
7213 
7214 	if (fl != NULL) {
7215 		MPASS(iq->flags & IQ_HAS_FL);
7216 
7217 		mtx_lock(&sc->sfl_lock);
7218 		FL_LOCK(fl);
7219 		fl->flags |= FL_DOOMED;
7220 		FL_UNLOCK(fl);
7221 		callout_stop(&sc->sfl_callout);
7222 		mtx_unlock(&sc->sfl_lock);
7223 
7224 		KASSERT((fl->flags & FL_STARVING) == 0,
7225 		    ("%s: still starving", __func__));
7226 
7227 		/* Release all buffers if hardware is no longer available. */
7228 		if (!(iq->flags & IQ_HW_ALLOCATED))
7229 			free_fl_buffers(sc, fl);
7230 	}
7231 }
7232 
7233 /*
7234  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7235  * that no new work is being enqueued by the hardware or the driver.  That part
7236  * should be arranged before calling this function.
7237  */
7238 static void
quiesce_vi(struct vi_info * vi)7239 quiesce_vi(struct vi_info *vi)
7240 {
7241 	int i;
7242 	struct adapter *sc = vi->adapter;
7243 	struct sge_rxq *rxq;
7244 	struct sge_txq *txq;
7245 #ifdef TCP_OFFLOAD
7246 	struct sge_ofld_rxq *ofld_rxq;
7247 #endif
7248 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7249 	struct sge_ofld_txq *ofld_txq;
7250 #endif
7251 
7252 	if (!(vi->flags & VI_INIT_DONE))
7253 		return;
7254 
7255 	for_each_txq(vi, i, txq) {
7256 		quiesce_txq(txq);
7257 	}
7258 
7259 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7260 	for_each_ofld_txq(vi, i, ofld_txq) {
7261 		quiesce_wrq(&ofld_txq->wrq);
7262 	}
7263 #endif
7264 
7265 	for_each_rxq(vi, i, rxq) {
7266 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7267 	}
7268 
7269 #ifdef TCP_OFFLOAD
7270 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7271 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7272 	}
7273 #endif
7274 }
7275 
7276 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7277 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7278     driver_intr_t *handler, void *arg, char *name)
7279 {
7280 	int rc;
7281 
7282 	irq->rid = rid;
7283 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7284 	    RF_SHAREABLE | RF_ACTIVE);
7285 	if (irq->res == NULL) {
7286 		device_printf(sc->dev,
7287 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7288 		return (ENOMEM);
7289 	}
7290 
7291 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7292 	    NULL, handler, arg, &irq->tag);
7293 	if (rc != 0) {
7294 		device_printf(sc->dev,
7295 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7296 		    rid, name, rc);
7297 	} else if (name)
7298 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7299 
7300 	return (rc);
7301 }
7302 
7303 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7304 t4_free_irq(struct adapter *sc, struct irq *irq)
7305 {
7306 	if (irq->tag)
7307 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7308 	if (irq->res)
7309 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7310 
7311 	bzero(irq, sizeof(*irq));
7312 
7313 	return (0);
7314 }
7315 
7316 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7317 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7318 {
7319 
7320 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7321 	t4_get_regs(sc, buf, regs->len);
7322 }
7323 
7324 #define	A_PL_INDIR_CMD	0x1f8
7325 
7326 #define	S_PL_AUTOINC	31
7327 #define	M_PL_AUTOINC	0x1U
7328 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7329 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7330 
7331 #define	S_PL_VFID	20
7332 #define	M_PL_VFID	0xffU
7333 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7334 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7335 
7336 #define	S_PL_ADDR	0
7337 #define	M_PL_ADDR	0xfffffU
7338 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7339 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7340 
7341 #define	A_PL_INDIR_DATA	0x1fc
7342 
7343 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7344 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7345 {
7346 	u32 stats[2];
7347 
7348 	if (sc->flags & IS_VF) {
7349 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7350 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7351 	} else {
7352 		mtx_assert(&sc->reg_lock, MA_OWNED);
7353 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7354 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7355 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7356 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7357 	}
7358 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7359 }
7360 
7361 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7362 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7363 {
7364 
7365 #define GET_STAT(name) \
7366 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7367 
7368 	if (!(sc->flags & IS_VF))
7369 		mtx_lock(&sc->reg_lock);
7370 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7371 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7372 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7373 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7374 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7375 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7376 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7377 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7378 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7379 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7380 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7381 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7382 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7383 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7384 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7385 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7386 	if (!(sc->flags & IS_VF))
7387 		mtx_unlock(&sc->reg_lock);
7388 
7389 #undef GET_STAT
7390 }
7391 
7392 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7393 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7394 {
7395 	int reg;
7396 
7397 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7398 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7399 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7400 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7401 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7402 }
7403 
7404 static void
vi_refresh_stats(struct vi_info * vi)7405 vi_refresh_stats(struct vi_info *vi)
7406 {
7407 	struct timeval tv;
7408 	const struct timeval interval = {0, 250000};	/* 250ms */
7409 
7410 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7411 
7412 	if (vi->flags & VI_SKIP_STATS)
7413 		return;
7414 
7415 	getmicrotime(&tv);
7416 	timevalsub(&tv, &interval);
7417 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7418 		return;
7419 
7420 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7421 	getmicrotime(&vi->last_refreshed);
7422 }
7423 
7424 static void
cxgbe_refresh_stats(struct vi_info * vi)7425 cxgbe_refresh_stats(struct vi_info *vi)
7426 {
7427 	u_int i, v, tnl_cong_drops, chan_map;
7428 	struct timeval tv;
7429 	const struct timeval interval = {0, 250000};	/* 250ms */
7430 	struct port_info *pi;
7431 	struct adapter *sc;
7432 
7433 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7434 
7435 	if (vi->flags & VI_SKIP_STATS)
7436 		return;
7437 
7438 	getmicrotime(&tv);
7439 	timevalsub(&tv, &interval);
7440 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7441 		return;
7442 
7443 	pi = vi->pi;
7444 	sc = vi->adapter;
7445 	tnl_cong_drops = 0;
7446 	t4_get_port_stats(sc, pi->port_id, &pi->stats);
7447 	chan_map = pi->rx_e_chan_map;
7448 	while (chan_map) {
7449 		i = ffs(chan_map) - 1;
7450 		mtx_lock(&sc->reg_lock);
7451 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7452 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7453 		mtx_unlock(&sc->reg_lock);
7454 		tnl_cong_drops += v;
7455 		chan_map &= ~(1 << i);
7456 	}
7457 	pi->tnl_cong_drops = tnl_cong_drops;
7458 	getmicrotime(&vi->last_refreshed);
7459 }
7460 
7461 static void
cxgbe_tick(void * arg)7462 cxgbe_tick(void *arg)
7463 {
7464 	struct vi_info *vi = arg;
7465 
7466 	MPASS(IS_MAIN_VI(vi));
7467 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7468 
7469 	cxgbe_refresh_stats(vi);
7470 	callout_schedule(&vi->tick, hz);
7471 }
7472 
7473 static void
vi_tick(void * arg)7474 vi_tick(void *arg)
7475 {
7476 	struct vi_info *vi = arg;
7477 
7478 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7479 
7480 	vi_refresh_stats(vi);
7481 	callout_schedule(&vi->tick, hz);
7482 }
7483 
7484 /*
7485  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7486  */
7487 static char *caps_decoder[] = {
7488 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7489 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7490 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7491 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7492 	    "\006HASHFILTER\007ETHOFLD",
7493 	"\20\001TOE",					/* 4: TOE */
7494 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
7495 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7496 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7497 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7498 	    "\007T10DIF"
7499 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7500 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7501 	    "\004TLS_HW",
7502 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7503 		    "\004PO_INITIATOR\005PO_TARGET",
7504 };
7505 
7506 void
t4_sysctls(struct adapter * sc)7507 t4_sysctls(struct adapter *sc)
7508 {
7509 	struct sysctl_ctx_list *ctx = &sc->ctx;
7510 	struct sysctl_oid *oid;
7511 	struct sysctl_oid_list *children, *c0;
7512 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7513 
7514 	/*
7515 	 * dev.t4nex.X.
7516 	 */
7517 	oid = device_get_sysctl_tree(sc->dev);
7518 	c0 = children = SYSCTL_CHILDREN(oid);
7519 
7520 	sc->sc_do_rxcopy = 1;
7521 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7522 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7523 
7524 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7525 	    sc->params.nports, "# of ports");
7526 
7527 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7528 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7529 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7530 	    "available doorbells");
7531 
7532 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7533 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7534 
7535 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7536 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7537 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7538 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7539 
7540 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7541 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7542 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7543 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7544 
7545 	t4_sge_sysctls(sc, ctx, children);
7546 
7547 	sc->lro_timeout = 100;
7548 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7549 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7550 
7551 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7552 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7553 
7554 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7555 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7556 
7557 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7558 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7559 
7560 	if (sc->flags & IS_VF)
7561 		return;
7562 
7563 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7564 	    NULL, chip_rev(sc), "chip hardware revision");
7565 
7566 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7567 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7568 
7569 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7570 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7571 
7572 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7573 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7574 
7575 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7576 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7577 
7578 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7579 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7580 
7581 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7582 	    sc->er_version, 0, "expansion ROM version");
7583 
7584 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7585 	    sc->bs_version, 0, "bootstrap firmware version");
7586 
7587 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7588 	    NULL, sc->params.scfg_vers, "serial config version");
7589 
7590 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7591 	    NULL, sc->params.vpd_vers, "VPD version");
7592 
7593 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7594 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7595 
7596 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7597 	    sc->cfcsum, "config file checksum");
7598 
7599 #define SYSCTL_CAP(name, n, text) \
7600 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7601 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7602 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7603 	    "available " text " capabilities")
7604 
7605 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7606 	SYSCTL_CAP(linkcaps, 1, "link");
7607 	SYSCTL_CAP(switchcaps, 2, "switch");
7608 	SYSCTL_CAP(niccaps, 3, "NIC");
7609 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7610 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7611 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7612 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7613 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7614 #undef SYSCTL_CAP
7615 
7616 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7617 	    NULL, sc->tids.nftids, "number of filters");
7618 
7619 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7620 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7621 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7622 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7623 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7624 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7625 
7626 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg",
7627 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7628 	    sysctl_loadavg, "A",
7629 	    "microprocessor load averages (debug firmwares only)");
7630 
7631 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7632 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7633 	    "I", "core Vdd (in mV)");
7634 
7635 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7636 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7637 	    sysctl_cpus, "A", "local CPUs");
7638 
7639 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7640 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7641 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7642 
7643 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7644 	    &sc->swintr, 0, "software triggered interrupts");
7645 
7646 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7647 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7648 	    "1 = reset adapter, 0 = zero reset counter");
7649 
7650 	/*
7651 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7652 	 */
7653 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7654 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
7655 	    "logs and miscellaneous information");
7656 	children = SYSCTL_CHILDREN(oid);
7657 
7658 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
7659 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7660 	    sysctl_cctrl, "A", "congestion control");
7661 
7662 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
7663 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7664 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
7665 
7666 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
7667 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7668 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
7669 
7670 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
7671 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7672 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
7673 
7674 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
7675 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 3,
7676 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
7677 
7678 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
7679 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 4,
7680 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
7681 
7682 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
7683 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 5,
7684 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
7685 
7686 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
7687 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7688 	    sysctl_cim_la, "A", "CIM logic analyzer");
7689 
7690 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
7691 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7692 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7693 
7694 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
7695 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7696 	    0 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
7697 
7698 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
7699 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7700 	    1 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
7701 
7702 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
7703 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7704 	    2 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
7705 
7706 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
7707 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7708 	    3 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
7709 
7710 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
7711 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7712 	    4 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
7713 
7714 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
7715 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7716 	    5 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
7717 
7718 	if (chip_id(sc) > CHELSIO_T4) {
7719 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
7720 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7721 		    6 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7722 		    "CIM OBQ 6 (SGE0-RX)");
7723 
7724 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
7725 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7726 		    7 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7727 		    "CIM OBQ 7 (SGE1-RX)");
7728 	}
7729 
7730 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
7731 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7732 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7733 
7734 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
7735 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7736 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
7737 
7738 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
7739 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7740 	    sysctl_cpl_stats, "A", "CPL statistics");
7741 
7742 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
7743 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7744 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
7745 
7746 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
7747 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7748 	    sysctl_tid_stats, "A", "tid stats");
7749 
7750 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
7751 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7752 	    sysctl_devlog, "A", "firmware's device log");
7753 
7754 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
7755 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7756 	    sysctl_fcoe_stats, "A", "FCoE statistics");
7757 
7758 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
7759 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7760 	    sysctl_hw_sched, "A", "hardware scheduler ");
7761 
7762 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
7763 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7764 	    sysctl_l2t, "A", "hardware L2 table");
7765 
7766 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
7767 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7768 	    sysctl_smt, "A", "hardware source MAC table");
7769 
7770 #ifdef INET6
7771 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
7772 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7773 	    sysctl_clip, "A", "active CLIP table entries");
7774 #endif
7775 
7776 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
7777 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7778 	    sysctl_lb_stats, "A", "loopback statistics");
7779 
7780 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
7781 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7782 	    sysctl_meminfo, "A", "memory regions");
7783 
7784 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
7785 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7786 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
7787 	    "A", "MPS TCAM entries");
7788 
7789 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
7790 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7791 	    sysctl_path_mtus, "A", "path MTUs");
7792 
7793 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
7794 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7795 	    sysctl_pm_stats, "A", "PM statistics");
7796 
7797 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
7798 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7799 	    sysctl_rdma_stats, "A", "RDMA statistics");
7800 
7801 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
7802 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7803 	    sysctl_tcp_stats, "A", "TCP statistics");
7804 
7805 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
7806 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7807 	    sysctl_tids, "A", "TID information");
7808 
7809 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
7810 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7811 	    sysctl_tp_err_stats, "A", "TP error statistics");
7812 
7813 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
7814 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7815 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
7816 
7817 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
7818 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7819 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
7820 
7821 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
7822 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7823 	    sysctl_tp_la, "A", "TP logic analyzer");
7824 
7825 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
7826 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7827 	    sysctl_tx_rate, "A", "Tx rate");
7828 
7829 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
7830 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7831 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
7832 
7833 	if (chip_id(sc) >= CHELSIO_T5) {
7834 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
7835 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7836 		    sysctl_wcwr_stats, "A", "write combined work requests");
7837 	}
7838 
7839 #ifdef KERN_TLS
7840 	if (is_ktls(sc)) {
7841 		/*
7842 		 * dev.t4nex.0.tls.
7843 		 */
7844 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
7845 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
7846 		children = SYSCTL_CHILDREN(oid);
7847 
7848 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
7849 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
7850 		    "keys in work requests (1) or attempt to store TLS keys "
7851 		    "in card memory.");
7852 
7853 		if (is_t6(sc))
7854 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
7855 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
7856 			    "combine TCB field updates with TLS record work "
7857 			    "requests.");
7858 	}
7859 #endif
7860 
7861 #ifdef TCP_OFFLOAD
7862 	if (is_offload(sc)) {
7863 		int i;
7864 		char s[4];
7865 
7866 		/*
7867 		 * dev.t4nex.X.toe.
7868 		 */
7869 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
7870 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
7871 		children = SYSCTL_CHILDREN(oid);
7872 
7873 		sc->tt.cong_algorithm = -1;
7874 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
7875 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
7876 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
7877 		    "3 = highspeed)");
7878 
7879 		sc->tt.sndbuf = -1;
7880 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
7881 		    &sc->tt.sndbuf, 0, "hardware send buffer");
7882 
7883 		sc->tt.ddp = 0;
7884 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
7885 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
7886 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
7887 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
7888 
7889 		sc->tt.rx_coalesce = -1;
7890 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
7891 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
7892 
7893 		sc->tt.tls = 1;
7894 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
7895 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
7896 		    "Inline TLS allowed");
7897 
7898 		sc->tt.tx_align = -1;
7899 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
7900 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
7901 
7902 		sc->tt.tx_zcopy = 0;
7903 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
7904 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
7905 		    "Enable zero-copy aio_write(2)");
7906 
7907 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
7908 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7909 		    "cop_managed_offloading", CTLFLAG_RW,
7910 		    &sc->tt.cop_managed_offloading, 0,
7911 		    "COP (Connection Offload Policy) controls all TOE offload");
7912 
7913 		sc->tt.autorcvbuf_inc = 16 * 1024;
7914 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
7915 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
7916 		    "autorcvbuf increment");
7917 
7918 		sc->tt.update_hc_on_pmtu_change = 1;
7919 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7920 		    "update_hc_on_pmtu_change", CTLFLAG_RW,
7921 		    &sc->tt.update_hc_on_pmtu_change, 0,
7922 		    "Update hostcache entry if the PMTU changes");
7923 
7924 		sc->tt.iso = 1;
7925 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
7926 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
7927 
7928 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
7929 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7930 		    sysctl_tp_tick, "A", "TP timer tick (us)");
7931 
7932 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
7933 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7934 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
7935 
7936 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
7937 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7938 		    sysctl_tp_tick, "A", "DACK tick (us)");
7939 
7940 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
7941 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7942 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
7943 
7944 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
7945 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7946 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
7947 		    "Minimum retransmit interval (us)");
7948 
7949 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
7950 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7951 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
7952 		    "Maximum retransmit interval (us)");
7953 
7954 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
7955 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7956 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
7957 		    "Persist timer min (us)");
7958 
7959 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
7960 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7961 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
7962 		    "Persist timer max (us)");
7963 
7964 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
7965 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7966 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
7967 		    "Keepalive idle timer (us)");
7968 
7969 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
7970 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7971 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
7972 		    "Keepalive interval timer (us)");
7973 
7974 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
7975 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7976 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
7977 
7978 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
7979 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7980 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
7981 		    "FINWAIT2 timer (us)");
7982 
7983 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
7984 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7985 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
7986 		    "Number of SYN retransmissions before abort");
7987 
7988 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
7989 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7990 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
7991 		    "Number of retransmissions before abort");
7992 
7993 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
7994 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7995 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
7996 		    "Number of keepalive probes before abort");
7997 
7998 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
7999 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8000 		    "TOE retransmit backoffs");
8001 		children = SYSCTL_CHILDREN(oid);
8002 		for (i = 0; i < 16; i++) {
8003 			snprintf(s, sizeof(s), "%u", i);
8004 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
8005 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8006 			    i, sysctl_tp_backoff, "IU",
8007 			    "TOE retransmit backoff");
8008 		}
8009 	}
8010 #endif
8011 }
8012 
8013 void
vi_sysctls(struct vi_info * vi)8014 vi_sysctls(struct vi_info *vi)
8015 {
8016 	struct sysctl_ctx_list *ctx = &vi->ctx;
8017 	struct sysctl_oid *oid;
8018 	struct sysctl_oid_list *children;
8019 
8020 	/*
8021 	 * dev.v?(cxgbe|cxl).X.
8022 	 */
8023 	oid = device_get_sysctl_tree(vi->dev);
8024 	children = SYSCTL_CHILDREN(oid);
8025 
8026 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
8027 	    vi->viid, "VI identifer");
8028 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
8029 	    &vi->nrxq, 0, "# of rx queues");
8030 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
8031 	    &vi->ntxq, 0, "# of tx queues");
8032 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
8033 	    &vi->first_rxq, 0, "index of first rx queue");
8034 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
8035 	    &vi->first_txq, 0, "index of first tx queue");
8036 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
8037 	    vi->rss_base, "start of RSS indirection table");
8038 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
8039 	    vi->rss_size, "size of RSS indirection table");
8040 
8041 	if (IS_MAIN_VI(vi)) {
8042 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
8043 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8044 		    sysctl_noflowq, "IU",
8045 		    "Reserve queue 0 for non-flowid packets");
8046 	}
8047 
8048 	if (vi->adapter->flags & IS_VF) {
8049 		MPASS(vi->flags & TX_USES_VM_WR);
8050 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
8051 		    NULL, 1, "use VM work requests for transmit");
8052 	} else {
8053 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
8054 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8055 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
8056 	}
8057 
8058 #ifdef TCP_OFFLOAD
8059 	if (vi->nofldrxq != 0) {
8060 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
8061 		    &vi->nofldrxq, 0,
8062 		    "# of rx queues for offloaded TCP connections");
8063 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
8064 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
8065 		    "index of first TOE rx queue");
8066 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
8067 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8068 		    sysctl_holdoff_tmr_idx_ofld, "I",
8069 		    "holdoff timer index for TOE queues");
8070 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
8071 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8072 		    sysctl_holdoff_pktc_idx_ofld, "I",
8073 		    "holdoff packet counter index for TOE queues");
8074 	}
8075 #endif
8076 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
8077 	if (vi->nofldtxq != 0) {
8078 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
8079 		    &vi->nofldtxq, 0,
8080 		    "# of tx queues for TOE/ETHOFLD");
8081 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
8082 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
8083 		    "index of first TOE/ETHOFLD tx queue");
8084 	}
8085 #endif
8086 #ifdef DEV_NETMAP
8087 	if (vi->nnmrxq != 0) {
8088 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
8089 		    &vi->nnmrxq, 0, "# of netmap rx queues");
8090 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
8091 		    &vi->nnmtxq, 0, "# of netmap tx queues");
8092 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
8093 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
8094 		    "index of first netmap rx queue");
8095 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
8096 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
8097 		    "index of first netmap tx queue");
8098 	}
8099 #endif
8100 
8101 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
8102 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8103 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
8104 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
8105 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8106 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
8107 
8108 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
8109 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8110 	    sysctl_qsize_rxq, "I", "rx queue size");
8111 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
8112 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8113 	    sysctl_qsize_txq, "I", "tx queue size");
8114 }
8115 
8116 static void
cxgbe_sysctls(struct port_info * pi)8117 cxgbe_sysctls(struct port_info *pi)
8118 {
8119 	struct sysctl_ctx_list *ctx = &pi->ctx;
8120 	struct sysctl_oid *oid;
8121 	struct sysctl_oid_list *children, *children2;
8122 	struct adapter *sc = pi->adapter;
8123 	int i;
8124 	char name[16];
8125 	static char *tc_flags = {"\20\1USER"};
8126 
8127 	/*
8128 	 * dev.cxgbe.X.
8129 	 */
8130 	oid = device_get_sysctl_tree(pi->dev);
8131 	children = SYSCTL_CHILDREN(oid);
8132 
8133 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8134 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8135 	    sysctl_linkdnrc, "A", "reason why link is down");
8136 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8137 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8138 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8139 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8140 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8141 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8142 		    sysctl_btphy, "I", "PHY firmware version");
8143 	}
8144 
8145 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8146 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8147 	    sysctl_pause_settings, "A",
8148 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8149 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8150 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8151 	    "FEC in use on the link");
8152 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8153 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8154 	    sysctl_requested_fec, "A",
8155 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8156 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8157 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8158 	    "FEC recommended by the cable/transceiver");
8159 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8160 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8161 	    sysctl_autoneg, "I",
8162 	    "autonegotiation (-1 = not supported)");
8163 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8164 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8165 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8166 
8167 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8168 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8169 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8170 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8171 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8172 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8173 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8174 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8175 
8176 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8177 	    port_top_speed(pi), "max speed (in Gbps)");
8178 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8179 	    pi->mps_bg_map, "MPS buffer group map");
8180 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8181 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8182 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8183 	    pi->tx_chan, "TP tx c-channel");
8184 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8185 	    pi->rx_chan, "TP rx c-channel");
8186 
8187 	if (sc->flags & IS_VF)
8188 		return;
8189 
8190 	/*
8191 	 * dev.(cxgbe|cxl).X.tc.
8192 	 */
8193 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8194 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8195 	    "Tx scheduler traffic classes (cl_rl)");
8196 	children2 = SYSCTL_CHILDREN(oid);
8197 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8198 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8199 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8200 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8201 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8202 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8203 	for (i = 0; i < sc->params.nsched_cls; i++) {
8204 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8205 
8206 		snprintf(name, sizeof(name), "%d", i);
8207 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8208 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8209 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8210 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8211 		    CTLFLAG_RD, &tc->state, 0, "current state");
8212 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8213 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8214 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8215 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8216 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8217 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8218 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8219 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8220 		    "traffic class parameters");
8221 	}
8222 
8223 	/*
8224 	 * dev.cxgbe.X.stats.
8225 	 */
8226 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8227 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8228 	children = SYSCTL_CHILDREN(oid);
8229 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8230 	    &pi->tx_parse_error, 0,
8231 	    "# of tx packets with invalid length or # of segments");
8232 
8233 #define T4_REGSTAT(name, stat, desc) \
8234     SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8235 	CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8236 	t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8237         sysctl_handle_t4_reg64, "QU", desc)
8238 
8239 /* We get these from port_stats and they may be stale by up to 1s */
8240 #define T4_PORTSTAT(name, desc) \
8241 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
8242 	    &pi->stats.name, desc)
8243 
8244 	T4_REGSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8245 	T4_REGSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8246 	T4_REGSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8247 	T4_REGSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8248 	T4_REGSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8249 	T4_REGSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8250 	T4_REGSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8251 	T4_REGSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8252 	T4_REGSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8253 	T4_REGSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8254 	T4_REGSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8255 	T4_REGSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8256 	T4_REGSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8257 	T4_REGSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8258 	T4_REGSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8259 	T4_REGSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8260 	T4_REGSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8261 	T4_REGSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8262 	T4_REGSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8263 	T4_REGSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8264 	T4_REGSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8265 	T4_REGSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8266 	T4_REGSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8267 
8268 	T4_REGSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8269 	T4_REGSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8270 	T4_REGSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8271 	T4_REGSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8272 	T4_REGSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8273 	T4_REGSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8274 	T4_REGSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8275 	if (is_t6(sc)) {
8276 		T4_PORTSTAT(rx_fcs_err,
8277 		    "# of frames received with bad FCS since last link up");
8278 	} else {
8279 		T4_REGSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8280 		    "# of frames received with bad FCS");
8281 	}
8282 	T4_REGSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8283 	T4_REGSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8284 	T4_REGSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8285 	T4_REGSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8286 	T4_REGSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8287 	T4_REGSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8288 	T4_REGSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8289 	T4_REGSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8290 	T4_REGSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8291 	T4_REGSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8292 	T4_REGSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8293 	T4_REGSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8294 	T4_REGSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8295 	T4_REGSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8296 	T4_REGSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8297 	T4_REGSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8298 	T4_REGSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8299 	T4_REGSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8300 	T4_REGSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8301 
8302 	T4_PORTSTAT(rx_ovflow0, "# drops due to buffer-group 0 overflows");
8303 	T4_PORTSTAT(rx_ovflow1, "# drops due to buffer-group 1 overflows");
8304 	T4_PORTSTAT(rx_ovflow2, "# drops due to buffer-group 2 overflows");
8305 	T4_PORTSTAT(rx_ovflow3, "# drops due to buffer-group 3 overflows");
8306 	T4_PORTSTAT(rx_trunc0, "# of buffer-group 0 truncated packets");
8307 	T4_PORTSTAT(rx_trunc1, "# of buffer-group 1 truncated packets");
8308 	T4_PORTSTAT(rx_trunc2, "# of buffer-group 2 truncated packets");
8309 	T4_PORTSTAT(rx_trunc3, "# of buffer-group 3 truncated packets");
8310 
8311 #undef T4_REGSTAT
8312 #undef T4_PORTSTAT
8313 }
8314 
8315 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8316 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8317 {
8318 	int rc, *i, space = 0;
8319 	struct sbuf sb;
8320 
8321 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8322 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8323 		if (space)
8324 			sbuf_printf(&sb, " ");
8325 		sbuf_printf(&sb, "%d", *i);
8326 		space = 1;
8327 	}
8328 	rc = sbuf_finish(&sb);
8329 	sbuf_delete(&sb);
8330 	return (rc);
8331 }
8332 
8333 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8334 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8335 {
8336 	int rc;
8337 	struct sbuf *sb;
8338 
8339 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8340 	if (sb == NULL)
8341 		return (ENOMEM);
8342 
8343 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8344 	rc = sbuf_finish(sb);
8345 	sbuf_delete(sb);
8346 
8347 	return (rc);
8348 }
8349 
8350 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8351 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8352 {
8353 	int rc;
8354 	struct sbuf *sb;
8355 
8356 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8357 	if (sb == NULL)
8358 		return (ENOMEM);
8359 
8360 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8361 	rc = sbuf_finish(sb);
8362 	sbuf_delete(sb);
8363 
8364 	return (rc);
8365 }
8366 
8367 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8368 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8369 {
8370 	struct port_info *pi = arg1;
8371 	int op = arg2;
8372 	struct adapter *sc = pi->adapter;
8373 	u_int v;
8374 	int rc;
8375 
8376 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8377 	if (rc)
8378 		return (rc);
8379 	if (!hw_all_ok(sc))
8380 		rc = ENXIO;
8381 	else {
8382 		/* XXX: magic numbers */
8383 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8384 		    op ? 0x20 : 0xc820, &v);
8385 	}
8386 	end_synchronized_op(sc, 0);
8387 	if (rc)
8388 		return (rc);
8389 	if (op == 0)
8390 		v /= 256;
8391 
8392 	rc = sysctl_handle_int(oidp, &v, 0, req);
8393 	return (rc);
8394 }
8395 
8396 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8397 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8398 {
8399 	struct vi_info *vi = arg1;
8400 	int rc, val;
8401 
8402 	val = vi->rsrv_noflowq;
8403 	rc = sysctl_handle_int(oidp, &val, 0, req);
8404 	if (rc != 0 || req->newptr == NULL)
8405 		return (rc);
8406 
8407 	if ((val >= 1) && (vi->ntxq > 1))
8408 		vi->rsrv_noflowq = 1;
8409 	else
8410 		vi->rsrv_noflowq = 0;
8411 
8412 	return (rc);
8413 }
8414 
8415 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8416 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8417 {
8418 	struct vi_info *vi = arg1;
8419 	struct adapter *sc = vi->adapter;
8420 	int rc, val, i;
8421 
8422 	MPASS(!(sc->flags & IS_VF));
8423 
8424 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8425 	rc = sysctl_handle_int(oidp, &val, 0, req);
8426 	if (rc != 0 || req->newptr == NULL)
8427 		return (rc);
8428 
8429 	if (val != 0 && val != 1)
8430 		return (EINVAL);
8431 
8432 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8433 	    "t4txvm");
8434 	if (rc)
8435 		return (rc);
8436 	if (!hw_all_ok(sc))
8437 		rc = ENXIO;
8438 	else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8439 		/*
8440 		 * We don't want parse_pkt to run with one setting (VF or PF)
8441 		 * and then eth_tx to see a different setting but still use
8442 		 * stale information calculated by parse_pkt.
8443 		 */
8444 		rc = EBUSY;
8445 	} else {
8446 		struct port_info *pi = vi->pi;
8447 		struct sge_txq *txq;
8448 		uint32_t ctrl0;
8449 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8450 
8451 		if (val) {
8452 			vi->flags |= TX_USES_VM_WR;
8453 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8454 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8455 			    V_TXPKT_INTF(pi->tx_chan));
8456 			if (!(sc->flags & IS_VF))
8457 				npkt--;
8458 		} else {
8459 			vi->flags &= ~TX_USES_VM_WR;
8460 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8461 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8462 			    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
8463 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8464 		}
8465 		for_each_txq(vi, i, txq) {
8466 			txq->cpl_ctrl0 = ctrl0;
8467 			txq->txp.max_npkt = npkt;
8468 		}
8469 	}
8470 	end_synchronized_op(sc, LOCK_HELD);
8471 	return (rc);
8472 }
8473 
8474 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8475 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8476 {
8477 	struct vi_info *vi = arg1;
8478 	struct adapter *sc = vi->adapter;
8479 	int idx, rc, i;
8480 	struct sge_rxq *rxq;
8481 	uint8_t v;
8482 
8483 	idx = vi->tmr_idx;
8484 
8485 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8486 	if (rc != 0 || req->newptr == NULL)
8487 		return (rc);
8488 
8489 	if (idx < 0 || idx >= SGE_NTIMERS)
8490 		return (EINVAL);
8491 
8492 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8493 	    "t4tmr");
8494 	if (rc)
8495 		return (rc);
8496 
8497 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8498 	for_each_rxq(vi, i, rxq) {
8499 #ifdef atomic_store_rel_8
8500 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8501 #else
8502 		rxq->iq.intr_params = v;
8503 #endif
8504 	}
8505 	vi->tmr_idx = idx;
8506 
8507 	end_synchronized_op(sc, LOCK_HELD);
8508 	return (0);
8509 }
8510 
8511 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8512 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8513 {
8514 	struct vi_info *vi = arg1;
8515 	struct adapter *sc = vi->adapter;
8516 	int idx, rc;
8517 
8518 	idx = vi->pktc_idx;
8519 
8520 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8521 	if (rc != 0 || req->newptr == NULL)
8522 		return (rc);
8523 
8524 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8525 		return (EINVAL);
8526 
8527 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8528 	    "t4pktc");
8529 	if (rc)
8530 		return (rc);
8531 
8532 	if (vi->flags & VI_INIT_DONE)
8533 		rc = EBUSY; /* cannot be changed once the queues are created */
8534 	else
8535 		vi->pktc_idx = idx;
8536 
8537 	end_synchronized_op(sc, LOCK_HELD);
8538 	return (rc);
8539 }
8540 
8541 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8542 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8543 {
8544 	struct vi_info *vi = arg1;
8545 	struct adapter *sc = vi->adapter;
8546 	int qsize, rc;
8547 
8548 	qsize = vi->qsize_rxq;
8549 
8550 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8551 	if (rc != 0 || req->newptr == NULL)
8552 		return (rc);
8553 
8554 	if (qsize < 128 || (qsize & 7))
8555 		return (EINVAL);
8556 
8557 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8558 	    "t4rxqs");
8559 	if (rc)
8560 		return (rc);
8561 
8562 	if (vi->flags & VI_INIT_DONE)
8563 		rc = EBUSY; /* cannot be changed once the queues are created */
8564 	else
8565 		vi->qsize_rxq = qsize;
8566 
8567 	end_synchronized_op(sc, LOCK_HELD);
8568 	return (rc);
8569 }
8570 
8571 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8572 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8573 {
8574 	struct vi_info *vi = arg1;
8575 	struct adapter *sc = vi->adapter;
8576 	int qsize, rc;
8577 
8578 	qsize = vi->qsize_txq;
8579 
8580 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8581 	if (rc != 0 || req->newptr == NULL)
8582 		return (rc);
8583 
8584 	if (qsize < 128 || qsize > 65536)
8585 		return (EINVAL);
8586 
8587 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8588 	    "t4txqs");
8589 	if (rc)
8590 		return (rc);
8591 
8592 	if (vi->flags & VI_INIT_DONE)
8593 		rc = EBUSY; /* cannot be changed once the queues are created */
8594 	else
8595 		vi->qsize_txq = qsize;
8596 
8597 	end_synchronized_op(sc, LOCK_HELD);
8598 	return (rc);
8599 }
8600 
8601 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8602 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8603 {
8604 	struct port_info *pi = arg1;
8605 	struct adapter *sc = pi->adapter;
8606 	struct link_config *lc = &pi->link_cfg;
8607 	int rc;
8608 
8609 	if (req->newptr == NULL) {
8610 		struct sbuf *sb;
8611 		static char *bits = "\20\1RX\2TX\3AUTO";
8612 
8613 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8614 		if (sb == NULL)
8615 			return (ENOMEM);
8616 
8617 		if (lc->link_ok) {
8618 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8619 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8620 		} else {
8621 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8622 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8623 		}
8624 		rc = sbuf_finish(sb);
8625 		sbuf_delete(sb);
8626 	} else {
8627 		char s[2];
8628 		int n;
8629 
8630 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8631 		    PAUSE_AUTONEG));
8632 		s[1] = 0;
8633 
8634 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8635 		if (rc != 0)
8636 			return(rc);
8637 
8638 		if (s[1] != 0)
8639 			return (EINVAL);
8640 		if (s[0] < '0' || s[0] > '9')
8641 			return (EINVAL);	/* not a number */
8642 		n = s[0] - '0';
8643 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8644 			return (EINVAL);	/* some other bit is set too */
8645 
8646 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8647 		    "t4PAUSE");
8648 		if (rc)
8649 			return (rc);
8650 		if (hw_all_ok(sc)) {
8651 			PORT_LOCK(pi);
8652 			lc->requested_fc = n;
8653 			fixup_link_config(pi);
8654 			if (pi->up_vis > 0)
8655 				rc = apply_link_config(pi);
8656 			set_current_media(pi);
8657 			PORT_UNLOCK(pi);
8658 		}
8659 		end_synchronized_op(sc, 0);
8660 	}
8661 
8662 	return (rc);
8663 }
8664 
8665 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8666 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8667 {
8668 	struct port_info *pi = arg1;
8669 	struct link_config *lc = &pi->link_cfg;
8670 	int rc;
8671 	struct sbuf *sb;
8672 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2";
8673 
8674 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8675 	if (sb == NULL)
8676 		return (ENOMEM);
8677 	if (lc->link_ok)
8678 		sbuf_printf(sb, "%b", lc->fec, bits);
8679 	else
8680 		sbuf_printf(sb, "no link");
8681 	rc = sbuf_finish(sb);
8682 	sbuf_delete(sb);
8683 
8684 	return (rc);
8685 }
8686 
8687 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)8688 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8689 {
8690 	struct port_info *pi = arg1;
8691 	struct adapter *sc = pi->adapter;
8692 	struct link_config *lc = &pi->link_cfg;
8693 	int rc;
8694 	int8_t old;
8695 
8696 	if (req->newptr == NULL) {
8697 		struct sbuf *sb;
8698 		static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2"
8699 		    "\5RSVD3\6auto\7module";
8700 
8701 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8702 		if (sb == NULL)
8703 			return (ENOMEM);
8704 
8705 		sbuf_printf(sb, "%b", lc->requested_fec, bits);
8706 		rc = sbuf_finish(sb);
8707 		sbuf_delete(sb);
8708 	} else {
8709 		char s[8];
8710 		int n;
8711 
8712 		snprintf(s, sizeof(s), "%d",
8713 		    lc->requested_fec == FEC_AUTO ? -1 :
8714 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
8715 
8716 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8717 		if (rc != 0)
8718 			return(rc);
8719 
8720 		n = strtol(&s[0], NULL, 0);
8721 		if (n < 0 || n & FEC_AUTO)
8722 			n = FEC_AUTO;
8723 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
8724 			return (EINVAL);/* some other bit is set too */
8725 
8726 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8727 		    "t4reqf");
8728 		if (rc)
8729 			return (rc);
8730 		PORT_LOCK(pi);
8731 		old = lc->requested_fec;
8732 		if (n == FEC_AUTO)
8733 			lc->requested_fec = FEC_AUTO;
8734 		else if (n == 0 || n == FEC_NONE)
8735 			lc->requested_fec = FEC_NONE;
8736 		else {
8737 			if ((lc->pcaps |
8738 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
8739 			    lc->pcaps) {
8740 				rc = ENOTSUP;
8741 				goto done;
8742 			}
8743 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
8744 			    FEC_MODULE);
8745 		}
8746 		if (hw_all_ok(sc)) {
8747 			fixup_link_config(pi);
8748 			if (pi->up_vis > 0) {
8749 				rc = apply_link_config(pi);
8750 				if (rc != 0) {
8751 					lc->requested_fec = old;
8752 					if (rc == FW_EPROTO)
8753 						rc = ENOTSUP;
8754 				}
8755 			}
8756 		}
8757 done:
8758 		PORT_UNLOCK(pi);
8759 		end_synchronized_op(sc, 0);
8760 	}
8761 
8762 	return (rc);
8763 }
8764 
8765 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)8766 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
8767 {
8768 	struct port_info *pi = arg1;
8769 	struct adapter *sc = pi->adapter;
8770 	struct link_config *lc = &pi->link_cfg;
8771 	int rc;
8772 	int8_t fec;
8773 	struct sbuf *sb;
8774 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2\5RSVD3";
8775 
8776 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8777 	if (sb == NULL)
8778 		return (ENOMEM);
8779 
8780 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
8781 		rc = EBUSY;
8782 		goto done;
8783 	}
8784 	if (!hw_all_ok(sc)) {
8785 		rc = ENXIO;
8786 		goto done;
8787 	}
8788 	PORT_LOCK(pi);
8789 	if (pi->up_vis == 0) {
8790 		/*
8791 		 * If all the interfaces are administratively down the firmware
8792 		 * does not report transceiver changes.  Refresh port info here.
8793 		 * This is the only reason we have a synchronized op in this
8794 		 * function.  Just PORT_LOCK would have been enough otherwise.
8795 		 */
8796 		t4_update_port_info(pi);
8797 	}
8798 
8799 	fec = lc->fec_hint;
8800 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
8801 	    !fec_supported(lc->pcaps)) {
8802 		PORT_UNLOCK(pi);
8803 		sbuf_printf(sb, "n/a");
8804 	} else {
8805 		if (fec == 0)
8806 			fec = FEC_NONE;
8807 		PORT_UNLOCK(pi);
8808 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, bits);
8809 	}
8810 	rc = sbuf_finish(sb);
8811 done:
8812 	sbuf_delete(sb);
8813 	end_synchronized_op(sc, 0);
8814 
8815 	return (rc);
8816 }
8817 
8818 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)8819 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
8820 {
8821 	struct port_info *pi = arg1;
8822 	struct adapter *sc = pi->adapter;
8823 	struct link_config *lc = &pi->link_cfg;
8824 	int rc, val;
8825 
8826 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
8827 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
8828 	else
8829 		val = -1;
8830 	rc = sysctl_handle_int(oidp, &val, 0, req);
8831 	if (rc != 0 || req->newptr == NULL)
8832 		return (rc);
8833 	if (val == 0)
8834 		val = AUTONEG_DISABLE;
8835 	else if (val == 1)
8836 		val = AUTONEG_ENABLE;
8837 	else
8838 		val = AUTONEG_AUTO;
8839 
8840 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8841 	    "t4aneg");
8842 	if (rc)
8843 		return (rc);
8844 	PORT_LOCK(pi);
8845 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
8846 		rc = ENOTSUP;
8847 		goto done;
8848 	}
8849 	lc->requested_aneg = val;
8850 	if (hw_all_ok(sc)) {
8851 		fixup_link_config(pi);
8852 		if (pi->up_vis > 0)
8853 			rc = apply_link_config(pi);
8854 		set_current_media(pi);
8855 	}
8856 done:
8857 	PORT_UNLOCK(pi);
8858 	end_synchronized_op(sc, 0);
8859 	return (rc);
8860 }
8861 
8862 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)8863 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
8864 {
8865 	struct port_info *pi = arg1;
8866 	struct adapter *sc = pi->adapter;
8867 	struct link_config *lc = &pi->link_cfg;
8868 	int rc, val;
8869 
8870 	val = lc->force_fec;
8871 	MPASS(val >= -1 && val <= 1);
8872 	rc = sysctl_handle_int(oidp, &val, 0, req);
8873 	if (rc != 0 || req->newptr == NULL)
8874 		return (rc);
8875 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
8876 		return (ENOTSUP);
8877 	if (val < -1 || val > 1)
8878 		return (EINVAL);
8879 
8880 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
8881 	if (rc)
8882 		return (rc);
8883 	PORT_LOCK(pi);
8884 	lc->force_fec = val;
8885 	if (hw_all_ok(sc)) {
8886 		fixup_link_config(pi);
8887 		if (pi->up_vis > 0)
8888 			rc = apply_link_config(pi);
8889 	}
8890 	PORT_UNLOCK(pi);
8891 	end_synchronized_op(sc, 0);
8892 	return (rc);
8893 }
8894 
8895 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)8896 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
8897 {
8898 	struct adapter *sc = arg1;
8899 	int rc, reg = arg2;
8900 	uint64_t val;
8901 
8902 	mtx_lock(&sc->reg_lock);
8903 	if (hw_off_limits(sc))
8904 		rc = ENXIO;
8905 	else {
8906 		rc = 0;
8907 		val = t4_read_reg64(sc, reg);
8908 	}
8909 	mtx_unlock(&sc->reg_lock);
8910 	if (rc == 0)
8911 		rc = sysctl_handle_64(oidp, &val, 0, req);
8912 	return (rc);
8913 }
8914 
8915 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)8916 sysctl_temperature(SYSCTL_HANDLER_ARGS)
8917 {
8918 	struct adapter *sc = arg1;
8919 	int rc, t;
8920 	uint32_t param, val;
8921 
8922 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
8923 	if (rc)
8924 		return (rc);
8925 	if (!hw_all_ok(sc))
8926 		rc = ENXIO;
8927 	else {
8928 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8929 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8930 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
8931 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8932 	}
8933 	end_synchronized_op(sc, 0);
8934 	if (rc)
8935 		return (rc);
8936 
8937 	/* unknown is returned as 0 but we display -1 in that case */
8938 	t = val == 0 ? -1 : val;
8939 
8940 	rc = sysctl_handle_int(oidp, &t, 0, req);
8941 	return (rc);
8942 }
8943 
8944 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)8945 sysctl_vdd(SYSCTL_HANDLER_ARGS)
8946 {
8947 	struct adapter *sc = arg1;
8948 	int rc;
8949 	uint32_t param, val;
8950 
8951 	if (sc->params.core_vdd == 0) {
8952 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8953 		    "t4vdd");
8954 		if (rc)
8955 			return (rc);
8956 		if (!hw_all_ok(sc))
8957 			rc = ENXIO;
8958 		else {
8959 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8960 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8961 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
8962 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
8963 			    &param, &val);
8964 		}
8965 		end_synchronized_op(sc, 0);
8966 		if (rc)
8967 			return (rc);
8968 		sc->params.core_vdd = val;
8969 	}
8970 
8971 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
8972 }
8973 
8974 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)8975 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
8976 {
8977 	struct adapter *sc = arg1;
8978 	int rc, v;
8979 	uint32_t param, val;
8980 
8981 	v = sc->sensor_resets;
8982 	rc = sysctl_handle_int(oidp, &v, 0, req);
8983 	if (rc != 0 || req->newptr == NULL || v <= 0)
8984 		return (rc);
8985 
8986 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
8987 	    chip_id(sc) < CHELSIO_T5)
8988 		return (ENOTSUP);
8989 
8990 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
8991 	if (rc)
8992 		return (rc);
8993 	if (!hw_all_ok(sc))
8994 		rc = ENXIO;
8995 	else {
8996 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8997 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8998 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
8999 		val = 1;
9000 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9001 	}
9002 	end_synchronized_op(sc, 0);
9003 	if (rc == 0)
9004 		sc->sensor_resets++;
9005 	return (rc);
9006 }
9007 
9008 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)9009 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
9010 {
9011 	struct adapter *sc = arg1;
9012 	struct sbuf *sb;
9013 	int rc;
9014 	uint32_t param, val;
9015 
9016 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
9017 	if (rc)
9018 		return (rc);
9019 	if (hw_all_ok(sc))
9020 		rc = ENXIO;
9021 	else {
9022 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9023 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
9024 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9025 	}
9026 	end_synchronized_op(sc, 0);
9027 	if (rc)
9028 		return (rc);
9029 
9030 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9031 	if (sb == NULL)
9032 		return (ENOMEM);
9033 
9034 	if (val == 0xffffffff) {
9035 		/* Only debug and custom firmwares report load averages. */
9036 		sbuf_printf(sb, "not available");
9037 	} else {
9038 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
9039 		    (val >> 16) & 0xff);
9040 	}
9041 	rc = sbuf_finish(sb);
9042 	sbuf_delete(sb);
9043 
9044 	return (rc);
9045 }
9046 
9047 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)9048 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
9049 {
9050 	struct adapter *sc = arg1;
9051 	struct sbuf *sb;
9052 	int rc, i;
9053 	uint16_t incr[NMTUS][NCCTRL_WIN];
9054 	static const char *dec_fac[] = {
9055 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
9056 		"0.9375"
9057 	};
9058 
9059 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9060 	if (sb == NULL)
9061 		return (ENOMEM);
9062 
9063 	rc = 0;
9064 	mtx_lock(&sc->reg_lock);
9065 	if (hw_off_limits(sc))
9066 		rc = ENXIO;
9067 	else
9068 		t4_read_cong_tbl(sc, incr);
9069 	mtx_unlock(&sc->reg_lock);
9070 	if (rc)
9071 		goto done;
9072 
9073 	for (i = 0; i < NCCTRL_WIN; ++i) {
9074 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
9075 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
9076 		    incr[5][i], incr[6][i], incr[7][i]);
9077 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
9078 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
9079 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
9080 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
9081 	}
9082 
9083 	rc = sbuf_finish(sb);
9084 done:
9085 	sbuf_delete(sb);
9086 	return (rc);
9087 }
9088 
9089 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
9090 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
9091 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
9092 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
9093 };
9094 
9095 static int
sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)9096 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
9097 {
9098 	struct adapter *sc = arg1;
9099 	struct sbuf *sb;
9100 	int rc, i, n, qid = arg2;
9101 	uint32_t *buf, *p;
9102 	char *qtype;
9103 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
9104 
9105 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
9106 	    ("%s: bad qid %d\n", __func__, qid));
9107 
9108 	if (qid < CIM_NUM_IBQ) {
9109 		/* inbound queue */
9110 		qtype = "IBQ";
9111 		n = 4 * CIM_IBQ_SIZE;
9112 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9113 		mtx_lock(&sc->reg_lock);
9114 		if (hw_off_limits(sc))
9115 			rc = -ENXIO;
9116 		else
9117 			rc = t4_read_cim_ibq(sc, qid, buf, n);
9118 		mtx_unlock(&sc->reg_lock);
9119 	} else {
9120 		/* outbound queue */
9121 		qtype = "OBQ";
9122 		qid -= CIM_NUM_IBQ;
9123 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
9124 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9125 		mtx_lock(&sc->reg_lock);
9126 		if (hw_off_limits(sc))
9127 			rc = -ENXIO;
9128 		else
9129 			rc = t4_read_cim_obq(sc, qid, buf, n);
9130 		mtx_unlock(&sc->reg_lock);
9131 	}
9132 
9133 	if (rc < 0) {
9134 		rc = -rc;
9135 		goto done;
9136 	}
9137 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9138 
9139 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9140 	if (sb == NULL) {
9141 		rc = ENOMEM;
9142 		goto done;
9143 	}
9144 
9145 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
9146 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9147 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9148 		    p[2], p[3]);
9149 
9150 	rc = sbuf_finish(sb);
9151 	sbuf_delete(sb);
9152 done:
9153 	free(buf, M_CXGBE);
9154 	return (rc);
9155 }
9156 
9157 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9158 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9159 {
9160 	uint32_t *p;
9161 
9162 	sbuf_printf(sb, "Status   Data      PC%s",
9163 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9164 	    "     LS0Stat  LS0Addr             LS0Data");
9165 
9166 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9167 		if (cfg & F_UPDBGLACAPTPCONLY) {
9168 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9169 			    p[6], p[7]);
9170 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9171 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9172 			    p[4] & 0xff, p[5] >> 8);
9173 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9174 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9175 			    p[1] & 0xf, p[2] >> 4);
9176 		} else {
9177 			sbuf_printf(sb,
9178 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9179 			    "%08x%08x%08x%08x",
9180 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9181 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9182 			    p[6], p[7]);
9183 		}
9184 	}
9185 }
9186 
9187 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9188 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9189 {
9190 	uint32_t *p;
9191 
9192 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9193 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9194 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9195 
9196 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9197 		if (cfg & F_UPDBGLACAPTPCONLY) {
9198 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9199 			    p[3] & 0xff, p[2], p[1], p[0]);
9200 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9201 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9202 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9203 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9204 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9205 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9206 			    p[6] >> 16);
9207 		} else {
9208 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9209 			    "%08x %08x %08x %08x %08x %08x",
9210 			    (p[9] >> 16) & 0xff,
9211 			    p[9] & 0xffff, p[8] >> 16,
9212 			    p[8] & 0xffff, p[7] >> 16,
9213 			    p[7] & 0xffff, p[6] >> 16,
9214 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9215 		}
9216 	}
9217 }
9218 
9219 static int
sbuf_cim_la(struct adapter * sc,struct sbuf * sb,int flags)9220 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
9221 {
9222 	uint32_t cfg, *buf;
9223 	int rc;
9224 
9225 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9226 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9227 	    M_ZERO | flags);
9228 	if (buf == NULL)
9229 		return (ENOMEM);
9230 
9231 	mtx_lock(&sc->reg_lock);
9232 	if (hw_off_limits(sc))
9233 		rc = ENXIO;
9234 	else {
9235 		rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
9236 		if (rc == 0)
9237 			rc = -t4_cim_read_la(sc, buf, NULL);
9238 	}
9239 	mtx_unlock(&sc->reg_lock);
9240 	if (rc == 0) {
9241 		if (chip_id(sc) < CHELSIO_T6)
9242 			sbuf_cim_la4(sc, sb, buf, cfg);
9243 		else
9244 			sbuf_cim_la6(sc, sb, buf, cfg);
9245 	}
9246 	free(buf, M_CXGBE);
9247 	return (rc);
9248 }
9249 
9250 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9251 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9252 {
9253 	struct adapter *sc = arg1;
9254 	struct sbuf *sb;
9255 	int rc;
9256 
9257 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9258 	if (sb == NULL)
9259 		return (ENOMEM);
9260 
9261 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
9262 	if (rc == 0)
9263 		rc = sbuf_finish(sb);
9264 	sbuf_delete(sb);
9265 	return (rc);
9266 }
9267 
9268 static void
dump_cim_regs(struct adapter * sc)9269 dump_cim_regs(struct adapter *sc)
9270 {
9271 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9272 	    device_get_nameunit(sc->dev),
9273 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9274 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9275 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9276 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9277 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9278 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9279 	    device_get_nameunit(sc->dev),
9280 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9281 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9282 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9283 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9284 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9285 }
9286 
9287 static void
dump_cimla(struct adapter * sc)9288 dump_cimla(struct adapter *sc)
9289 {
9290 	struct sbuf sb;
9291 	int rc;
9292 
9293 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9294 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9295 		    device_get_nameunit(sc->dev));
9296 		return;
9297 	}
9298 	rc = sbuf_cim_la(sc, &sb, M_WAITOK);
9299 	if (rc == 0) {
9300 		rc = sbuf_finish(&sb);
9301 		if (rc == 0) {
9302 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9303 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9304 		}
9305 	}
9306 	sbuf_delete(&sb);
9307 }
9308 
9309 void
t4_os_cim_err(struct adapter * sc)9310 t4_os_cim_err(struct adapter *sc)
9311 {
9312 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9313 }
9314 
9315 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9316 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9317 {
9318 	struct adapter *sc = arg1;
9319 	u_int i;
9320 	struct sbuf *sb;
9321 	uint32_t *buf, *p;
9322 	int rc;
9323 
9324 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9325 	if (sb == NULL)
9326 		return (ENOMEM);
9327 
9328 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9329 	    M_ZERO | M_WAITOK);
9330 
9331 	rc = 0;
9332 	mtx_lock(&sc->reg_lock);
9333 	if (hw_off_limits(sc))
9334 		rc = ENXIO;
9335 	else
9336 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9337 	mtx_unlock(&sc->reg_lock);
9338 	if (rc)
9339 		goto done;
9340 
9341 	p = buf;
9342 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9343 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9344 		    p[1], p[0]);
9345 	}
9346 
9347 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9348 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9349 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9350 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9351 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9352 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9353 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9354 		    p[0] & 1);
9355 	}
9356 	rc = sbuf_finish(sb);
9357 done:
9358 	sbuf_delete(sb);
9359 	free(buf, M_CXGBE);
9360 	return (rc);
9361 }
9362 
9363 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9364 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9365 {
9366 	struct adapter *sc = arg1;
9367 	u_int i;
9368 	struct sbuf *sb;
9369 	uint32_t *buf, *p;
9370 	int rc;
9371 
9372 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9373 	if (sb == NULL)
9374 		return (ENOMEM);
9375 
9376 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9377 	    M_ZERO | M_WAITOK);
9378 
9379 	rc = 0;
9380 	mtx_lock(&sc->reg_lock);
9381 	if (hw_off_limits(sc))
9382 		rc = ENXIO;
9383 	else
9384 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9385 	mtx_unlock(&sc->reg_lock);
9386 	if (rc)
9387 		goto done;
9388 
9389 	p = buf;
9390 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9391 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9392 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9393 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9394 		    p[4], p[3], p[2], p[1], p[0]);
9395 	}
9396 
9397 	sbuf_printf(sb, "\n\nCntl ID               Data");
9398 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9399 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9400 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9401 	}
9402 
9403 	rc = sbuf_finish(sb);
9404 done:
9405 	sbuf_delete(sb);
9406 	free(buf, M_CXGBE);
9407 	return (rc);
9408 }
9409 
9410 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9411 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9412 {
9413 	struct adapter *sc = arg1;
9414 	struct sbuf *sb;
9415 	int rc, i;
9416 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9417 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9418 	uint16_t thres[CIM_NUM_IBQ];
9419 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9420 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9421 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9422 
9423 	cim_num_obq = sc->chip_params->cim_num_obq;
9424 	if (is_t4(sc)) {
9425 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9426 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9427 	} else {
9428 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9429 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9430 	}
9431 	nq = CIM_NUM_IBQ + cim_num_obq;
9432 
9433 	mtx_lock(&sc->reg_lock);
9434 	if (hw_off_limits(sc))
9435 		rc = ENXIO;
9436 	else {
9437 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9438 		if (rc == 0) {
9439 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9440 			    obq_wr);
9441 			if (rc == 0)
9442 				t4_read_cimq_cfg(sc, base, size, thres);
9443 		}
9444 	}
9445 	mtx_unlock(&sc->reg_lock);
9446 	if (rc)
9447 		return (rc);
9448 
9449 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9450 	if (sb == NULL)
9451 		return (ENOMEM);
9452 
9453 	sbuf_printf(sb,
9454 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9455 
9456 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9457 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9458 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9459 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9460 		    G_QUEREMFLITS(p[2]) * 16);
9461 	for ( ; i < nq; i++, p += 4, wr += 2)
9462 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9463 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9464 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9465 		    G_QUEREMFLITS(p[2]) * 16);
9466 
9467 	rc = sbuf_finish(sb);
9468 	sbuf_delete(sb);
9469 
9470 	return (rc);
9471 }
9472 
9473 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9474 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9475 {
9476 	struct adapter *sc = arg1;
9477 	struct sbuf *sb;
9478 	int rc;
9479 	struct tp_cpl_stats stats;
9480 
9481 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9482 	if (sb == NULL)
9483 		return (ENOMEM);
9484 
9485 	rc = 0;
9486 	mtx_lock(&sc->reg_lock);
9487 	if (hw_off_limits(sc))
9488 		rc = ENXIO;
9489 	else
9490 		t4_tp_get_cpl_stats(sc, &stats, 0);
9491 	mtx_unlock(&sc->reg_lock);
9492 	if (rc)
9493 		goto done;
9494 
9495 	if (sc->chip_params->nchan > 2) {
9496 		sbuf_printf(sb, "                 channel 0  channel 1"
9497 		    "  channel 2  channel 3");
9498 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9499 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9500 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9501 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9502 	} else {
9503 		sbuf_printf(sb, "                 channel 0  channel 1");
9504 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9505 		    stats.req[0], stats.req[1]);
9506 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9507 		    stats.rsp[0], stats.rsp[1]);
9508 	}
9509 
9510 	rc = sbuf_finish(sb);
9511 done:
9512 	sbuf_delete(sb);
9513 	return (rc);
9514 }
9515 
9516 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9517 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9518 {
9519 	struct adapter *sc = arg1;
9520 	struct sbuf *sb;
9521 	int rc;
9522 	struct tp_usm_stats stats;
9523 
9524 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9525 	if (sb == NULL)
9526 		return (ENOMEM);
9527 
9528 	rc = 0;
9529 	mtx_lock(&sc->reg_lock);
9530 	if (hw_off_limits(sc))
9531 		rc = ENXIO;
9532 	else
9533 		t4_get_usm_stats(sc, &stats, 1);
9534 	mtx_unlock(&sc->reg_lock);
9535 	if (rc == 0) {
9536 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
9537 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
9538 		sbuf_printf(sb, "Drops:  %u", stats.drops);
9539 		rc = sbuf_finish(sb);
9540 	}
9541 	sbuf_delete(sb);
9542 
9543 	return (rc);
9544 }
9545 
9546 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)9547 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
9548 {
9549 	struct adapter *sc = arg1;
9550 	struct sbuf *sb;
9551 	int rc;
9552 	struct tp_tid_stats stats;
9553 
9554 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9555 	if (sb == NULL)
9556 		return (ENOMEM);
9557 
9558 	rc = 0;
9559 	mtx_lock(&sc->reg_lock);
9560 	if (hw_off_limits(sc))
9561 		rc = ENXIO;
9562 	else
9563 		t4_tp_get_tid_stats(sc, &stats, 1);
9564 	mtx_unlock(&sc->reg_lock);
9565 	if (rc == 0) {
9566 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
9567 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
9568 		sbuf_printf(sb, "Active:     %u\n", stats.act);
9569 		sbuf_printf(sb, "Passive:    %u", stats.pas);
9570 		rc = sbuf_finish(sb);
9571 	}
9572 	sbuf_delete(sb);
9573 
9574 	return (rc);
9575 }
9576 
9577 static const char * const devlog_level_strings[] = {
9578 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
9579 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
9580 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
9581 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
9582 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
9583 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
9584 };
9585 
9586 static const char * const devlog_facility_strings[] = {
9587 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
9588 	[FW_DEVLOG_FACILITY_CF]		= "CF",
9589 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
9590 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
9591 	[FW_DEVLOG_FACILITY_RES]	= "RES",
9592 	[FW_DEVLOG_FACILITY_HW]		= "HW",
9593 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
9594 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
9595 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
9596 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
9597 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
9598 	[FW_DEVLOG_FACILITY_VI]		= "VI",
9599 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
9600 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
9601 	[FW_DEVLOG_FACILITY_TM]		= "TM",
9602 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
9603 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
9604 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
9605 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
9606 	[FW_DEVLOG_FACILITY_RI]		= "RI",
9607 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
9608 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
9609 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
9610 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
9611 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
9612 };
9613 
9614 static int
sbuf_devlog(struct adapter * sc,struct sbuf * sb,int flags)9615 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
9616 {
9617 	int i, j, rc, nentries, first = 0;
9618 	struct devlog_params *dparams = &sc->params.devlog;
9619 	struct fw_devlog_e *buf, *e;
9620 	uint64_t ftstamp = UINT64_MAX;
9621 
9622 	if (dparams->addr == 0)
9623 		return (ENXIO);
9624 
9625 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9626 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
9627 	if (buf == NULL)
9628 		return (ENOMEM);
9629 
9630 	mtx_lock(&sc->reg_lock);
9631 	if (hw_off_limits(sc))
9632 		rc = ENXIO;
9633 	else
9634 		rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf,
9635 		    dparams->size);
9636 	mtx_unlock(&sc->reg_lock);
9637 	if (rc != 0)
9638 		goto done;
9639 
9640 	nentries = dparams->size / sizeof(struct fw_devlog_e);
9641 	for (i = 0; i < nentries; i++) {
9642 		e = &buf[i];
9643 
9644 		if (e->timestamp == 0)
9645 			break;	/* end */
9646 
9647 		e->timestamp = be64toh(e->timestamp);
9648 		e->seqno = be32toh(e->seqno);
9649 		for (j = 0; j < 8; j++)
9650 			e->params[j] = be32toh(e->params[j]);
9651 
9652 		if (e->timestamp < ftstamp) {
9653 			ftstamp = e->timestamp;
9654 			first = i;
9655 		}
9656 	}
9657 
9658 	if (buf[first].timestamp == 0)
9659 		goto done;	/* nothing in the log */
9660 
9661 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
9662 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
9663 
9664 	i = first;
9665 	do {
9666 		e = &buf[i];
9667 		if (e->timestamp == 0)
9668 			break;	/* end */
9669 
9670 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
9671 		    e->seqno, e->timestamp,
9672 		    (e->level < nitems(devlog_level_strings) ?
9673 			devlog_level_strings[e->level] : "UNKNOWN"),
9674 		    (e->facility < nitems(devlog_facility_strings) ?
9675 			devlog_facility_strings[e->facility] : "UNKNOWN"));
9676 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
9677 		    e->params[2], e->params[3], e->params[4],
9678 		    e->params[5], e->params[6], e->params[7]);
9679 
9680 		if (++i == nentries)
9681 			i = 0;
9682 	} while (i != first);
9683 done:
9684 	free(buf, M_CXGBE);
9685 	return (rc);
9686 }
9687 
9688 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)9689 sysctl_devlog(SYSCTL_HANDLER_ARGS)
9690 {
9691 	struct adapter *sc = arg1;
9692 	int rc;
9693 	struct sbuf *sb;
9694 
9695 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9696 	if (sb == NULL)
9697 		return (ENOMEM);
9698 
9699 	rc = sbuf_devlog(sc, sb, M_WAITOK);
9700 	if (rc == 0)
9701 		rc = sbuf_finish(sb);
9702 	sbuf_delete(sb);
9703 	return (rc);
9704 }
9705 
9706 static void
dump_devlog(struct adapter * sc)9707 dump_devlog(struct adapter *sc)
9708 {
9709 	int rc;
9710 	struct sbuf sb;
9711 
9712 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9713 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
9714 		    device_get_nameunit(sc->dev));
9715 		return;
9716 	}
9717 	rc = sbuf_devlog(sc, &sb, M_WAITOK);
9718 	if (rc == 0) {
9719 		rc = sbuf_finish(&sb);
9720 		if (rc == 0) {
9721 			log(LOG_DEBUG, "%s: device log follows.\n%s",
9722 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9723 		}
9724 	}
9725 	sbuf_delete(&sb);
9726 }
9727 
9728 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)9729 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
9730 {
9731 	struct adapter *sc = arg1;
9732 	struct sbuf *sb;
9733 	int rc;
9734 	struct tp_fcoe_stats stats[MAX_NCHAN];
9735 	int i, nchan = sc->chip_params->nchan;
9736 
9737 	rc = 0;
9738 	mtx_lock(&sc->reg_lock);
9739 	if (hw_off_limits(sc))
9740 		rc = ENXIO;
9741 	else {
9742 		for (i = 0; i < nchan; i++)
9743 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
9744 	}
9745 	mtx_unlock(&sc->reg_lock);
9746 	if (rc != 0)
9747 		return (rc);
9748 
9749 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9750 	if (sb == NULL)
9751 		return (ENOMEM);
9752 
9753 	if (nchan > 2) {
9754 		sbuf_printf(sb, "                   channel 0        channel 1"
9755 		    "        channel 2        channel 3");
9756 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
9757 		    stats[0].octets_ddp, stats[1].octets_ddp,
9758 		    stats[2].octets_ddp, stats[3].octets_ddp);
9759 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
9760 		    stats[0].frames_ddp, stats[1].frames_ddp,
9761 		    stats[2].frames_ddp, stats[3].frames_ddp);
9762 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
9763 		    stats[0].frames_drop, stats[1].frames_drop,
9764 		    stats[2].frames_drop, stats[3].frames_drop);
9765 	} else {
9766 		sbuf_printf(sb, "                   channel 0        channel 1");
9767 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
9768 		    stats[0].octets_ddp, stats[1].octets_ddp);
9769 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
9770 		    stats[0].frames_ddp, stats[1].frames_ddp);
9771 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
9772 		    stats[0].frames_drop, stats[1].frames_drop);
9773 	}
9774 
9775 	rc = sbuf_finish(sb);
9776 	sbuf_delete(sb);
9777 
9778 	return (rc);
9779 }
9780 
9781 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)9782 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
9783 {
9784 	struct adapter *sc = arg1;
9785 	struct sbuf *sb;
9786 	int rc, i;
9787 	unsigned int map, kbps, ipg, mode;
9788 	unsigned int pace_tab[NTX_SCHED];
9789 
9790 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
9791 	if (sb == NULL)
9792 		return (ENOMEM);
9793 
9794 	mtx_lock(&sc->reg_lock);
9795 	if (hw_off_limits(sc)) {
9796 		mtx_unlock(&sc->reg_lock);
9797 		rc = ENXIO;
9798 		goto done;
9799 	}
9800 
9801 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
9802 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
9803 	t4_read_pace_tbl(sc, pace_tab);
9804 	mtx_unlock(&sc->reg_lock);
9805 
9806 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
9807 	    "Class IPG (0.1 ns)   Flow IPG (us)");
9808 
9809 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
9810 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
9811 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
9812 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
9813 		if (kbps)
9814 			sbuf_printf(sb, "%9u     ", kbps);
9815 		else
9816 			sbuf_printf(sb, " disabled     ");
9817 
9818 		if (ipg)
9819 			sbuf_printf(sb, "%13u        ", ipg);
9820 		else
9821 			sbuf_printf(sb, "     disabled        ");
9822 
9823 		if (pace_tab[i])
9824 			sbuf_printf(sb, "%10u", pace_tab[i]);
9825 		else
9826 			sbuf_printf(sb, "  disabled");
9827 	}
9828 	rc = sbuf_finish(sb);
9829 done:
9830 	sbuf_delete(sb);
9831 	return (rc);
9832 }
9833 
9834 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)9835 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
9836 {
9837 	struct adapter *sc = arg1;
9838 	struct sbuf *sb;
9839 	int rc, i, j;
9840 	uint64_t *p0, *p1;
9841 	struct lb_port_stats s[2];
9842 	static const char *stat_name[] = {
9843 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
9844 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
9845 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
9846 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
9847 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
9848 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
9849 		"BG2FramesTrunc:", "BG3FramesTrunc:"
9850 	};
9851 
9852 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9853 	if (sb == NULL)
9854 		return (ENOMEM);
9855 
9856 	memset(s, 0, sizeof(s));
9857 
9858 	rc = 0;
9859 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
9860 		mtx_lock(&sc->reg_lock);
9861 		if (hw_off_limits(sc))
9862 			rc = ENXIO;
9863 		else {
9864 			t4_get_lb_stats(sc, i, &s[0]);
9865 			t4_get_lb_stats(sc, i + 1, &s[1]);
9866 		}
9867 		mtx_unlock(&sc->reg_lock);
9868 		if (rc != 0)
9869 			break;
9870 
9871 		p0 = &s[0].octets;
9872 		p1 = &s[1].octets;
9873 		sbuf_printf(sb, "%s                       Loopback %u"
9874 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
9875 
9876 		for (j = 0; j < nitems(stat_name); j++)
9877 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
9878 				   *p0++, *p1++);
9879 	}
9880 
9881 	if (rc == 0)
9882 		rc = sbuf_finish(sb);
9883 	sbuf_delete(sb);
9884 
9885 	return (rc);
9886 }
9887 
9888 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)9889 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
9890 {
9891 	int rc = 0;
9892 	struct port_info *pi = arg1;
9893 	struct link_config *lc = &pi->link_cfg;
9894 	struct sbuf *sb;
9895 
9896 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
9897 	if (sb == NULL)
9898 		return (ENOMEM);
9899 
9900 	if (lc->link_ok || lc->link_down_rc == 255)
9901 		sbuf_printf(sb, "n/a");
9902 	else
9903 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
9904 
9905 	rc = sbuf_finish(sb);
9906 	sbuf_delete(sb);
9907 
9908 	return (rc);
9909 }
9910 
9911 struct mem_desc {
9912 	u_int base;
9913 	u_int limit;
9914 	u_int idx;
9915 };
9916 
9917 static int
mem_desc_cmp(const void * a,const void * b)9918 mem_desc_cmp(const void *a, const void *b)
9919 {
9920 	const u_int v1 = ((const struct mem_desc *)a)->base;
9921 	const u_int v2 = ((const struct mem_desc *)b)->base;
9922 
9923 	if (v1 < v2)
9924 		return (-1);
9925 	else if (v1 > v2)
9926 		return (1);
9927 
9928 	return (0);
9929 }
9930 
9931 static void
mem_region_show(struct sbuf * sb,const char * name,unsigned int from,unsigned int to)9932 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
9933     unsigned int to)
9934 {
9935 	unsigned int size;
9936 
9937 	if (from == to)
9938 		return;
9939 
9940 	size = to - from + 1;
9941 	if (size == 0)
9942 		return;
9943 
9944 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
9945 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
9946 }
9947 
9948 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)9949 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
9950 {
9951 	struct adapter *sc = arg1;
9952 	struct sbuf *sb;
9953 	int rc, i, n;
9954 	uint32_t lo, hi, used, free, alloc;
9955 	static const char *memory[] = {
9956 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
9957 	};
9958 	static const char *region[] = {
9959 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
9960 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
9961 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
9962 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
9963 		"RQUDP region:", "PBL region:", "TXPBL region:",
9964 		"TLSKey region:", "DBVFIFO region:", "ULPRX state:",
9965 		"ULPTX state:", "On-chip queues:",
9966 	};
9967 	struct mem_desc avail[4];
9968 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
9969 	struct mem_desc *md = mem;
9970 
9971 	rc = sysctl_wire_old_buffer(req, 0);
9972 	if (rc != 0)
9973 		return (rc);
9974 
9975 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9976 	if (sb == NULL)
9977 		return (ENOMEM);
9978 
9979 	for (i = 0; i < nitems(mem); i++) {
9980 		mem[i].limit = 0;
9981 		mem[i].idx = i;
9982 	}
9983 
9984 	mtx_lock(&sc->reg_lock);
9985 	if (hw_off_limits(sc)) {
9986 		rc = ENXIO;
9987 		goto done;
9988 	}
9989 
9990 	/* Find and sort the populated memory ranges */
9991 	i = 0;
9992 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
9993 	if (lo & F_EDRAM0_ENABLE) {
9994 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
9995 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
9996 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
9997 		avail[i].idx = 0;
9998 		i++;
9999 	}
10000 	if (lo & F_EDRAM1_ENABLE) {
10001 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
10002 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
10003 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
10004 		avail[i].idx = 1;
10005 		i++;
10006 	}
10007 	if (lo & F_EXT_MEM_ENABLE) {
10008 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
10009 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
10010 		avail[i].limit = avail[i].base + (G_EXT_MEM_SIZE(hi) << 20);
10011 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
10012 		i++;
10013 	}
10014 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
10015 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10016 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
10017 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
10018 		avail[i].idx = 4;
10019 		i++;
10020 	}
10021 	if (is_t6(sc) && lo & F_HMA_MUX) {
10022 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10023 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
10024 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
10025 		avail[i].idx = 5;
10026 		i++;
10027 	}
10028 	MPASS(i <= nitems(avail));
10029 	if (!i)                                    /* no memory available */
10030 		goto done;
10031 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
10032 
10033 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
10034 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
10035 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
10036 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
10037 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
10038 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
10039 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
10040 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
10041 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
10042 
10043 	/* the next few have explicit upper bounds */
10044 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
10045 	md->limit = md->base - 1 +
10046 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
10047 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
10048 	md++;
10049 
10050 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
10051 	md->limit = md->base - 1 +
10052 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
10053 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
10054 	md++;
10055 
10056 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10057 		if (chip_id(sc) <= CHELSIO_T5)
10058 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
10059 		else
10060 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
10061 		md->limit = 0;
10062 	} else {
10063 		md->base = 0;
10064 		md->idx = nitems(region);  /* hide it */
10065 	}
10066 	md++;
10067 
10068 #define ulp_region(reg) \
10069 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
10070 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
10071 
10072 	ulp_region(RX_ISCSI);
10073 	ulp_region(RX_TDDP);
10074 	ulp_region(TX_TPT);
10075 	ulp_region(RX_STAG);
10076 	ulp_region(RX_RQ);
10077 	ulp_region(RX_RQUDP);
10078 	ulp_region(RX_PBL);
10079 	ulp_region(TX_PBL);
10080 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
10081 		ulp_region(RX_TLS_KEY);
10082 	}
10083 #undef ulp_region
10084 
10085 	md->base = 0;
10086 	if (is_t4(sc))
10087 		md->idx = nitems(region);
10088 	else {
10089 		uint32_t size = 0;
10090 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
10091 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
10092 
10093 		if (is_t5(sc)) {
10094 			if (sge_ctrl & F_VFIFO_ENABLE)
10095 				size = fifo_size << 2;
10096 		} else
10097 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
10098 
10099 		if (size) {
10100 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
10101 			md->limit = md->base + size - 1;
10102 		} else
10103 			md->idx = nitems(region);
10104 	}
10105 	md++;
10106 
10107 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
10108 	md->limit = 0;
10109 	md++;
10110 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
10111 	md->limit = 0;
10112 	md++;
10113 
10114 	md->base = sc->vres.ocq.start;
10115 	if (sc->vres.ocq.size)
10116 		md->limit = md->base + sc->vres.ocq.size - 1;
10117 	else
10118 		md->idx = nitems(region);  /* hide it */
10119 	md++;
10120 
10121 	/* add any address-space holes, there can be up to 3 */
10122 	for (n = 0; n < i - 1; n++)
10123 		if (avail[n].limit < avail[n + 1].base)
10124 			(md++)->base = avail[n].limit;
10125 	if (avail[n].limit)
10126 		(md++)->base = avail[n].limit;
10127 
10128 	n = md - mem;
10129 	MPASS(n <= nitems(mem));
10130 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10131 
10132 	for (lo = 0; lo < i; lo++)
10133 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10134 				avail[lo].limit - 1);
10135 
10136 	sbuf_printf(sb, "\n");
10137 	for (i = 0; i < n; i++) {
10138 		if (mem[i].idx >= nitems(region))
10139 			continue;                        /* skip holes */
10140 		if (!mem[i].limit)
10141 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10142 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10143 				mem[i].limit);
10144 	}
10145 
10146 	sbuf_printf(sb, "\n");
10147 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10148 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10149 	mem_region_show(sb, "uP RAM:", lo, hi);
10150 
10151 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10152 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10153 	mem_region_show(sb, "uP Extmem2:", lo, hi);
10154 
10155 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10156 	for (i = 0, free = 0; i < 2; i++)
10157 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10158 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10159 		   G_PMRXMAXPAGE(lo), free,
10160 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
10161 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
10162 
10163 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10164 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10165 	for (i = 0, free = 0; i < 4; i++)
10166 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10167 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10168 		   G_PMTXMAXPAGE(lo), free,
10169 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10170 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
10171 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10172 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10173 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10174 
10175 	for (i = 0; i < 4; i++) {
10176 		if (chip_id(sc) > CHELSIO_T5)
10177 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10178 		else
10179 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10180 		if (is_t5(sc)) {
10181 			used = G_T5_USED(lo);
10182 			alloc = G_T5_ALLOC(lo);
10183 		} else {
10184 			used = G_USED(lo);
10185 			alloc = G_ALLOC(lo);
10186 		}
10187 		/* For T6 these are MAC buffer groups */
10188 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10189 		    i, used, alloc);
10190 	}
10191 	for (i = 0; i < sc->chip_params->nchan; i++) {
10192 		if (chip_id(sc) > CHELSIO_T5)
10193 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10194 		else
10195 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10196 		if (is_t5(sc)) {
10197 			used = G_T5_USED(lo);
10198 			alloc = G_T5_ALLOC(lo);
10199 		} else {
10200 			used = G_USED(lo);
10201 			alloc = G_ALLOC(lo);
10202 		}
10203 		/* For T6 these are MAC buffer groups */
10204 		sbuf_printf(sb,
10205 		    "\nLoopback %d using %u pages out of %u allocated",
10206 		    i, used, alloc);
10207 	}
10208 done:
10209 	mtx_unlock(&sc->reg_lock);
10210 	if (rc == 0)
10211 		rc = sbuf_finish(sb);
10212 	sbuf_delete(sb);
10213 	return (rc);
10214 }
10215 
10216 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10217 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10218 {
10219 	*mask = x | y;
10220 	y = htobe64(y);
10221 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10222 }
10223 
10224 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10225 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10226 {
10227 	struct adapter *sc = arg1;
10228 	struct sbuf *sb;
10229 	int rc, i;
10230 
10231 	MPASS(chip_id(sc) <= CHELSIO_T5);
10232 
10233 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10234 	if (sb == NULL)
10235 		return (ENOMEM);
10236 
10237 	sbuf_printf(sb,
10238 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10239 	    "  VF              Replication             P0 P1 P2 P3  ML");
10240 	rc = 0;
10241 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10242 		uint64_t tcamx, tcamy, mask;
10243 		uint32_t cls_lo, cls_hi;
10244 		uint8_t addr[ETHER_ADDR_LEN];
10245 
10246 		mtx_lock(&sc->reg_lock);
10247 		if (hw_off_limits(sc))
10248 			rc = ENXIO;
10249 		else {
10250 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10251 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10252 		}
10253 		mtx_unlock(&sc->reg_lock);
10254 		if (rc != 0)
10255 			break;
10256 		if (tcamx & tcamy)
10257 			continue;
10258 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10259 		mtx_lock(&sc->reg_lock);
10260 		if (hw_off_limits(sc))
10261 			rc = ENXIO;
10262 		else {
10263 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10264 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10265 		}
10266 		mtx_unlock(&sc->reg_lock);
10267 		if (rc != 0)
10268 			break;
10269 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10270 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10271 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10272 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10273 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10274 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10275 
10276 		if (cls_lo & F_REPLICATE) {
10277 			struct fw_ldst_cmd ldst_cmd;
10278 
10279 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10280 			ldst_cmd.op_to_addrspace =
10281 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10282 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10283 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10284 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10285 			ldst_cmd.u.mps.rplc.fid_idx =
10286 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10287 				V_FW_LDST_CMD_IDX(i));
10288 
10289 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10290 			    "t4mps");
10291 			if (rc)
10292 				break;
10293 			if (hw_off_limits(sc))
10294 				rc = ENXIO;
10295 			else
10296 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10297 				    sizeof(ldst_cmd), &ldst_cmd);
10298 			end_synchronized_op(sc, 0);
10299 			if (rc != 0)
10300 				break;
10301 			else {
10302 				sbuf_printf(sb, " %08x %08x %08x %08x",
10303 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10304 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10305 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10306 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10307 			}
10308 		} else
10309 			sbuf_printf(sb, "%36s", "");
10310 
10311 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10312 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10313 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10314 	}
10315 
10316 	if (rc)
10317 		(void) sbuf_finish(sb);
10318 	else
10319 		rc = sbuf_finish(sb);
10320 	sbuf_delete(sb);
10321 
10322 	return (rc);
10323 }
10324 
10325 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10326 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10327 {
10328 	struct adapter *sc = arg1;
10329 	struct sbuf *sb;
10330 	int rc, i;
10331 
10332 	MPASS(chip_id(sc) > CHELSIO_T5);
10333 
10334 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10335 	if (sb == NULL)
10336 		return (ENOMEM);
10337 
10338 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10339 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10340 	    "                           Replication"
10341 	    "                                    P0 P1 P2 P3  ML\n");
10342 
10343 	rc = 0;
10344 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10345 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10346 		uint16_t ivlan;
10347 		uint64_t tcamx, tcamy, val, mask;
10348 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10349 		uint8_t addr[ETHER_ADDR_LEN];
10350 
10351 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10352 		if (i < 256)
10353 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10354 		else
10355 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10356 		mtx_lock(&sc->reg_lock);
10357 		if (hw_off_limits(sc))
10358 			rc = ENXIO;
10359 		else {
10360 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10361 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10362 			tcamy = G_DMACH(val) << 32;
10363 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10364 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10365 		}
10366 		mtx_unlock(&sc->reg_lock);
10367 		if (rc != 0)
10368 			break;
10369 
10370 		lookup_type = G_DATALKPTYPE(data2);
10371 		port_num = G_DATAPORTNUM(data2);
10372 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10373 			/* Inner header VNI */
10374 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10375 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10376 			dip_hit = data2 & F_DATADIPHIT;
10377 			vlan_vld = 0;
10378 		} else {
10379 			vniy = 0;
10380 			dip_hit = 0;
10381 			vlan_vld = data2 & F_DATAVIDH2;
10382 			ivlan = G_VIDL(val);
10383 		}
10384 
10385 		ctl |= V_CTLXYBITSEL(1);
10386 		mtx_lock(&sc->reg_lock);
10387 		if (hw_off_limits(sc))
10388 			rc = ENXIO;
10389 		else {
10390 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10391 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10392 			tcamx = G_DMACH(val) << 32;
10393 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10394 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10395 		}
10396 		mtx_unlock(&sc->reg_lock);
10397 		if (rc != 0)
10398 			break;
10399 
10400 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10401 			/* Inner header VNI mask */
10402 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10403 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10404 		} else
10405 			vnix = 0;
10406 
10407 		if (tcamx & tcamy)
10408 			continue;
10409 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10410 
10411 		mtx_lock(&sc->reg_lock);
10412 		if (hw_off_limits(sc))
10413 			rc = ENXIO;
10414 		else {
10415 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10416 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10417 		}
10418 		mtx_unlock(&sc->reg_lock);
10419 		if (rc != 0)
10420 			break;
10421 
10422 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10423 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10424 			    "%012jx %06x %06x    -    -   %3c"
10425 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
10426 			    addr[1], addr[2], addr[3], addr[4], addr[5],
10427 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
10428 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10429 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10430 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10431 		} else {
10432 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10433 			    "%012jx    -       -   ", i, addr[0], addr[1],
10434 			    addr[2], addr[3], addr[4], addr[5],
10435 			    (uintmax_t)mask);
10436 
10437 			if (vlan_vld)
10438 				sbuf_printf(sb, "%4u   Y     ", ivlan);
10439 			else
10440 				sbuf_printf(sb, "  -    N     ");
10441 
10442 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
10443 			    lookup_type ? 'I' : 'O', port_num,
10444 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10445 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10446 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10447 		}
10448 
10449 
10450 		if (cls_lo & F_T6_REPLICATE) {
10451 			struct fw_ldst_cmd ldst_cmd;
10452 
10453 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10454 			ldst_cmd.op_to_addrspace =
10455 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10456 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10457 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10458 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10459 			ldst_cmd.u.mps.rplc.fid_idx =
10460 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10461 				V_FW_LDST_CMD_IDX(i));
10462 
10463 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10464 			    "t6mps");
10465 			if (rc)
10466 				break;
10467 			if (hw_off_limits(sc))
10468 				rc = ENXIO;
10469 			else
10470 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10471 				    sizeof(ldst_cmd), &ldst_cmd);
10472 			end_synchronized_op(sc, 0);
10473 			if (rc != 0)
10474 				break;
10475 			else {
10476 				sbuf_printf(sb, " %08x %08x %08x %08x"
10477 				    " %08x %08x %08x %08x",
10478 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
10479 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
10480 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
10481 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
10482 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10483 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10484 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10485 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10486 			}
10487 		} else
10488 			sbuf_printf(sb, "%72s", "");
10489 
10490 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
10491 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
10492 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
10493 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
10494 	}
10495 
10496 	if (rc)
10497 		(void) sbuf_finish(sb);
10498 	else
10499 		rc = sbuf_finish(sb);
10500 	sbuf_delete(sb);
10501 
10502 	return (rc);
10503 }
10504 
10505 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)10506 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
10507 {
10508 	struct adapter *sc = arg1;
10509 	struct sbuf *sb;
10510 	int rc;
10511 	uint16_t mtus[NMTUS];
10512 
10513 	rc = 0;
10514 	mtx_lock(&sc->reg_lock);
10515 	if (hw_off_limits(sc))
10516 		rc = ENXIO;
10517 	else
10518 		t4_read_mtu_tbl(sc, mtus, NULL);
10519 	mtx_unlock(&sc->reg_lock);
10520 	if (rc != 0)
10521 		return (rc);
10522 
10523 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10524 	if (sb == NULL)
10525 		return (ENOMEM);
10526 
10527 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
10528 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
10529 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
10530 	    mtus[14], mtus[15]);
10531 
10532 	rc = sbuf_finish(sb);
10533 	sbuf_delete(sb);
10534 
10535 	return (rc);
10536 }
10537 
10538 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)10539 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
10540 {
10541 	struct adapter *sc = arg1;
10542 	struct sbuf *sb;
10543 	int rc, i;
10544 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
10545 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
10546 	static const char *tx_stats[MAX_PM_NSTATS] = {
10547 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
10548 		"Tx FIFO wait", NULL, "Tx latency"
10549 	};
10550 	static const char *rx_stats[MAX_PM_NSTATS] = {
10551 		"Read:", "Write bypass:", "Write mem:", "Flush:",
10552 		"Rx FIFO wait", NULL, "Rx latency"
10553 	};
10554 
10555 	rc = 0;
10556 	mtx_lock(&sc->reg_lock);
10557 	if (hw_off_limits(sc))
10558 		rc = ENXIO;
10559 	else {
10560 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
10561 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
10562 	}
10563 	mtx_unlock(&sc->reg_lock);
10564 	if (rc != 0)
10565 		return (rc);
10566 
10567 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10568 	if (sb == NULL)
10569 		return (ENOMEM);
10570 
10571 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
10572 	for (i = 0; i < 4; i++) {
10573 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10574 		    tx_cyc[i]);
10575 	}
10576 
10577 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
10578 	for (i = 0; i < 4; i++) {
10579 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10580 		    rx_cyc[i]);
10581 	}
10582 
10583 	if (chip_id(sc) > CHELSIO_T5) {
10584 		sbuf_printf(sb,
10585 		    "\n              Total wait      Total occupancy");
10586 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10587 		    tx_cyc[i]);
10588 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10589 		    rx_cyc[i]);
10590 
10591 		i += 2;
10592 		MPASS(i < nitems(tx_stats));
10593 
10594 		sbuf_printf(sb,
10595 		    "\n                   Reads           Total wait");
10596 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10597 		    tx_cyc[i]);
10598 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10599 		    rx_cyc[i]);
10600 	}
10601 
10602 	rc = sbuf_finish(sb);
10603 	sbuf_delete(sb);
10604 
10605 	return (rc);
10606 }
10607 
10608 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)10609 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
10610 {
10611 	struct adapter *sc = arg1;
10612 	struct sbuf *sb;
10613 	int rc;
10614 	struct tp_rdma_stats stats;
10615 
10616 	rc = 0;
10617 	mtx_lock(&sc->reg_lock);
10618 	if (hw_off_limits(sc))
10619 		rc = ENXIO;
10620 	else
10621 		t4_tp_get_rdma_stats(sc, &stats, 0);
10622 	mtx_unlock(&sc->reg_lock);
10623 	if (rc != 0)
10624 		return (rc);
10625 
10626 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10627 	if (sb == NULL)
10628 		return (ENOMEM);
10629 
10630 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
10631 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
10632 
10633 	rc = sbuf_finish(sb);
10634 	sbuf_delete(sb);
10635 
10636 	return (rc);
10637 }
10638 
10639 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)10640 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
10641 {
10642 	struct adapter *sc = arg1;
10643 	struct sbuf *sb;
10644 	int rc;
10645 	struct tp_tcp_stats v4, v6;
10646 
10647 	rc = 0;
10648 	mtx_lock(&sc->reg_lock);
10649 	if (hw_off_limits(sc))
10650 		rc = ENXIO;
10651 	else
10652 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
10653 	mtx_unlock(&sc->reg_lock);
10654 	if (rc != 0)
10655 		return (rc);
10656 
10657 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10658 	if (sb == NULL)
10659 		return (ENOMEM);
10660 
10661 	sbuf_printf(sb,
10662 	    "                                IP                 IPv6\n");
10663 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
10664 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
10665 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
10666 	    v4.tcp_in_segs, v6.tcp_in_segs);
10667 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
10668 	    v4.tcp_out_segs, v6.tcp_out_segs);
10669 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
10670 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
10671 
10672 	rc = sbuf_finish(sb);
10673 	sbuf_delete(sb);
10674 
10675 	return (rc);
10676 }
10677 
10678 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)10679 sysctl_tids(SYSCTL_HANDLER_ARGS)
10680 {
10681 	struct adapter *sc = arg1;
10682 	struct sbuf *sb;
10683 	int rc;
10684 	uint32_t x, y;
10685 	struct tid_info *t = &sc->tids;
10686 
10687 	rc = 0;
10688 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10689 	if (sb == NULL)
10690 		return (ENOMEM);
10691 
10692 	if (t->natids) {
10693 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
10694 		    t->atids_in_use);
10695 	}
10696 
10697 	if (t->nhpftids) {
10698 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
10699 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
10700 	}
10701 
10702 	if (t->ntids) {
10703 		bool hashen = false;
10704 
10705 		mtx_lock(&sc->reg_lock);
10706 		if (hw_off_limits(sc))
10707 			rc = ENXIO;
10708 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10709 			hashen = true;
10710 			if (chip_id(sc) <= CHELSIO_T5) {
10711 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
10712 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
10713 			} else {
10714 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
10715 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
10716 			}
10717 		}
10718 		mtx_unlock(&sc->reg_lock);
10719 		if (rc != 0)
10720 			goto done;
10721 
10722 		sbuf_printf(sb, "TID range: ");
10723 		if (hashen) {
10724 			if (x)
10725 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
10726 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
10727 		} else {
10728 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
10729 			    t->ntids - 1);
10730 		}
10731 		sbuf_printf(sb, ", in use: %u\n",
10732 		    atomic_load_acq_int(&t->tids_in_use));
10733 	}
10734 
10735 	if (t->nstids) {
10736 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
10737 		    t->stid_base + t->nstids - 1, t->stids_in_use);
10738 	}
10739 
10740 	if (t->nftids) {
10741 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
10742 		    t->ftid_end, t->ftids_in_use);
10743 	}
10744 
10745 	if (t->netids) {
10746 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
10747 		    t->etid_base + t->netids - 1, t->etids_in_use);
10748 	}
10749 
10750 	mtx_lock(&sc->reg_lock);
10751 	if (hw_off_limits(sc))
10752 		rc = ENXIO;
10753 	else {
10754 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
10755 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
10756 	}
10757 	mtx_unlock(&sc->reg_lock);
10758 	if (rc != 0)
10759 		goto done;
10760 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
10761 done:
10762 	if (rc == 0)
10763 		rc = sbuf_finish(sb);
10764 	else
10765 		(void)sbuf_finish(sb);
10766 	sbuf_delete(sb);
10767 
10768 	return (rc);
10769 }
10770 
10771 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)10772 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
10773 {
10774 	struct adapter *sc = arg1;
10775 	struct sbuf *sb;
10776 	int rc;
10777 	struct tp_err_stats stats;
10778 
10779 	rc = 0;
10780 	mtx_lock(&sc->reg_lock);
10781 	if (hw_off_limits(sc))
10782 		rc = ENXIO;
10783 	else
10784 		t4_tp_get_err_stats(sc, &stats, 0);
10785 	mtx_unlock(&sc->reg_lock);
10786 	if (rc != 0)
10787 		return (rc);
10788 
10789 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10790 	if (sb == NULL)
10791 		return (ENOMEM);
10792 
10793 	if (sc->chip_params->nchan > 2) {
10794 		sbuf_printf(sb, "                 channel 0  channel 1"
10795 		    "  channel 2  channel 3\n");
10796 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
10797 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
10798 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
10799 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
10800 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
10801 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
10802 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
10803 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
10804 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
10805 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
10806 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
10807 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
10808 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
10809 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
10810 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
10811 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
10812 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
10813 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
10814 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
10815 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
10816 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
10817 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
10818 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
10819 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
10820 	} else {
10821 		sbuf_printf(sb, "                 channel 0  channel 1\n");
10822 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
10823 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
10824 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
10825 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
10826 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
10827 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
10828 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
10829 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
10830 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
10831 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
10832 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
10833 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
10834 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
10835 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
10836 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
10837 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
10838 	}
10839 
10840 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
10841 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
10842 
10843 	rc = sbuf_finish(sb);
10844 	sbuf_delete(sb);
10845 
10846 	return (rc);
10847 }
10848 
10849 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)10850 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
10851 {
10852 	struct adapter *sc = arg1;
10853 	struct sbuf *sb;
10854 	int rc;
10855 	struct tp_tnl_stats stats;
10856 
10857 	rc = 0;
10858 	mtx_lock(&sc->reg_lock);
10859 	if (hw_off_limits(sc))
10860 		rc = ENXIO;
10861 	else
10862 		t4_tp_get_tnl_stats(sc, &stats, 1);
10863 	mtx_unlock(&sc->reg_lock);
10864 	if (rc != 0)
10865 		return (rc);
10866 
10867 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10868 	if (sb == NULL)
10869 		return (ENOMEM);
10870 
10871 	if (sc->chip_params->nchan > 2) {
10872 		sbuf_printf(sb, "           channel 0  channel 1"
10873 		    "  channel 2  channel 3\n");
10874 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
10875 		    stats.out_pkt[0], stats.out_pkt[1],
10876 		    stats.out_pkt[2], stats.out_pkt[3]);
10877 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
10878 		    stats.in_pkt[0], stats.in_pkt[1],
10879 		    stats.in_pkt[2], stats.in_pkt[3]);
10880 	} else {
10881 		sbuf_printf(sb, "           channel 0  channel 1\n");
10882 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
10883 		    stats.out_pkt[0], stats.out_pkt[1]);
10884 		sbuf_printf(sb, "InPkts:   %10u %10u",
10885 		    stats.in_pkt[0], stats.in_pkt[1]);
10886 	}
10887 
10888 	rc = sbuf_finish(sb);
10889 	sbuf_delete(sb);
10890 
10891 	return (rc);
10892 }
10893 
10894 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)10895 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
10896 {
10897 	struct adapter *sc = arg1;
10898 	struct tp_params *tpp = &sc->params.tp;
10899 	u_int mask;
10900 	int rc;
10901 
10902 	mask = tpp->la_mask >> 16;
10903 	rc = sysctl_handle_int(oidp, &mask, 0, req);
10904 	if (rc != 0 || req->newptr == NULL)
10905 		return (rc);
10906 	if (mask > 0xffff)
10907 		return (EINVAL);
10908 	mtx_lock(&sc->reg_lock);
10909 	if (hw_off_limits(sc))
10910 		rc = ENXIO;
10911 	else {
10912 		tpp->la_mask = mask << 16;
10913 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
10914 		    tpp->la_mask);
10915 	}
10916 	mtx_unlock(&sc->reg_lock);
10917 
10918 	return (rc);
10919 }
10920 
10921 struct field_desc {
10922 	const char *name;
10923 	u_int start;
10924 	u_int width;
10925 };
10926 
10927 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)10928 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
10929 {
10930 	char buf[32];
10931 	int line_size = 0;
10932 
10933 	while (f->name) {
10934 		uint64_t mask = (1ULL << f->width) - 1;
10935 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
10936 		    ((uintmax_t)v >> f->start) & mask);
10937 
10938 		if (line_size + len >= 79) {
10939 			line_size = 8;
10940 			sbuf_printf(sb, "\n        ");
10941 		}
10942 		sbuf_printf(sb, "%s ", buf);
10943 		line_size += len + 1;
10944 		f++;
10945 	}
10946 	sbuf_printf(sb, "\n");
10947 }
10948 
10949 static const struct field_desc tp_la0[] = {
10950 	{ "RcfOpCodeOut", 60, 4 },
10951 	{ "State", 56, 4 },
10952 	{ "WcfState", 52, 4 },
10953 	{ "RcfOpcSrcOut", 50, 2 },
10954 	{ "CRxError", 49, 1 },
10955 	{ "ERxError", 48, 1 },
10956 	{ "SanityFailed", 47, 1 },
10957 	{ "SpuriousMsg", 46, 1 },
10958 	{ "FlushInputMsg", 45, 1 },
10959 	{ "FlushInputCpl", 44, 1 },
10960 	{ "RssUpBit", 43, 1 },
10961 	{ "RssFilterHit", 42, 1 },
10962 	{ "Tid", 32, 10 },
10963 	{ "InitTcb", 31, 1 },
10964 	{ "LineNumber", 24, 7 },
10965 	{ "Emsg", 23, 1 },
10966 	{ "EdataOut", 22, 1 },
10967 	{ "Cmsg", 21, 1 },
10968 	{ "CdataOut", 20, 1 },
10969 	{ "EreadPdu", 19, 1 },
10970 	{ "CreadPdu", 18, 1 },
10971 	{ "TunnelPkt", 17, 1 },
10972 	{ "RcfPeerFin", 16, 1 },
10973 	{ "RcfReasonOut", 12, 4 },
10974 	{ "TxCchannel", 10, 2 },
10975 	{ "RcfTxChannel", 8, 2 },
10976 	{ "RxEchannel", 6, 2 },
10977 	{ "RcfRxChannel", 5, 1 },
10978 	{ "RcfDataOutSrdy", 4, 1 },
10979 	{ "RxDvld", 3, 1 },
10980 	{ "RxOoDvld", 2, 1 },
10981 	{ "RxCongestion", 1, 1 },
10982 	{ "TxCongestion", 0, 1 },
10983 	{ NULL }
10984 };
10985 
10986 static const struct field_desc tp_la1[] = {
10987 	{ "CplCmdIn", 56, 8 },
10988 	{ "CplCmdOut", 48, 8 },
10989 	{ "ESynOut", 47, 1 },
10990 	{ "EAckOut", 46, 1 },
10991 	{ "EFinOut", 45, 1 },
10992 	{ "ERstOut", 44, 1 },
10993 	{ "SynIn", 43, 1 },
10994 	{ "AckIn", 42, 1 },
10995 	{ "FinIn", 41, 1 },
10996 	{ "RstIn", 40, 1 },
10997 	{ "DataIn", 39, 1 },
10998 	{ "DataInVld", 38, 1 },
10999 	{ "PadIn", 37, 1 },
11000 	{ "RxBufEmpty", 36, 1 },
11001 	{ "RxDdp", 35, 1 },
11002 	{ "RxFbCongestion", 34, 1 },
11003 	{ "TxFbCongestion", 33, 1 },
11004 	{ "TxPktSumSrdy", 32, 1 },
11005 	{ "RcfUlpType", 28, 4 },
11006 	{ "Eread", 27, 1 },
11007 	{ "Ebypass", 26, 1 },
11008 	{ "Esave", 25, 1 },
11009 	{ "Static0", 24, 1 },
11010 	{ "Cread", 23, 1 },
11011 	{ "Cbypass", 22, 1 },
11012 	{ "Csave", 21, 1 },
11013 	{ "CPktOut", 20, 1 },
11014 	{ "RxPagePoolFull", 18, 2 },
11015 	{ "RxLpbkPkt", 17, 1 },
11016 	{ "TxLpbkPkt", 16, 1 },
11017 	{ "RxVfValid", 15, 1 },
11018 	{ "SynLearned", 14, 1 },
11019 	{ "SetDelEntry", 13, 1 },
11020 	{ "SetInvEntry", 12, 1 },
11021 	{ "CpcmdDvld", 11, 1 },
11022 	{ "CpcmdSave", 10, 1 },
11023 	{ "RxPstructsFull", 8, 2 },
11024 	{ "EpcmdDvld", 7, 1 },
11025 	{ "EpcmdFlush", 6, 1 },
11026 	{ "EpcmdTrimPrefix", 5, 1 },
11027 	{ "EpcmdTrimPostfix", 4, 1 },
11028 	{ "ERssIp4Pkt", 3, 1 },
11029 	{ "ERssIp6Pkt", 2, 1 },
11030 	{ "ERssTcpUdpPkt", 1, 1 },
11031 	{ "ERssFceFipPkt", 0, 1 },
11032 	{ NULL }
11033 };
11034 
11035 static const struct field_desc tp_la2[] = {
11036 	{ "CplCmdIn", 56, 8 },
11037 	{ "MpsVfVld", 55, 1 },
11038 	{ "MpsPf", 52, 3 },
11039 	{ "MpsVf", 44, 8 },
11040 	{ "SynIn", 43, 1 },
11041 	{ "AckIn", 42, 1 },
11042 	{ "FinIn", 41, 1 },
11043 	{ "RstIn", 40, 1 },
11044 	{ "DataIn", 39, 1 },
11045 	{ "DataInVld", 38, 1 },
11046 	{ "PadIn", 37, 1 },
11047 	{ "RxBufEmpty", 36, 1 },
11048 	{ "RxDdp", 35, 1 },
11049 	{ "RxFbCongestion", 34, 1 },
11050 	{ "TxFbCongestion", 33, 1 },
11051 	{ "TxPktSumSrdy", 32, 1 },
11052 	{ "RcfUlpType", 28, 4 },
11053 	{ "Eread", 27, 1 },
11054 	{ "Ebypass", 26, 1 },
11055 	{ "Esave", 25, 1 },
11056 	{ "Static0", 24, 1 },
11057 	{ "Cread", 23, 1 },
11058 	{ "Cbypass", 22, 1 },
11059 	{ "Csave", 21, 1 },
11060 	{ "CPktOut", 20, 1 },
11061 	{ "RxPagePoolFull", 18, 2 },
11062 	{ "RxLpbkPkt", 17, 1 },
11063 	{ "TxLpbkPkt", 16, 1 },
11064 	{ "RxVfValid", 15, 1 },
11065 	{ "SynLearned", 14, 1 },
11066 	{ "SetDelEntry", 13, 1 },
11067 	{ "SetInvEntry", 12, 1 },
11068 	{ "CpcmdDvld", 11, 1 },
11069 	{ "CpcmdSave", 10, 1 },
11070 	{ "RxPstructsFull", 8, 2 },
11071 	{ "EpcmdDvld", 7, 1 },
11072 	{ "EpcmdFlush", 6, 1 },
11073 	{ "EpcmdTrimPrefix", 5, 1 },
11074 	{ "EpcmdTrimPostfix", 4, 1 },
11075 	{ "ERssIp4Pkt", 3, 1 },
11076 	{ "ERssIp6Pkt", 2, 1 },
11077 	{ "ERssTcpUdpPkt", 1, 1 },
11078 	{ "ERssFceFipPkt", 0, 1 },
11079 	{ NULL }
11080 };
11081 
11082 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)11083 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
11084 {
11085 
11086 	field_desc_show(sb, *p, tp_la0);
11087 }
11088 
11089 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)11090 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
11091 {
11092 
11093 	if (idx)
11094 		sbuf_printf(sb, "\n");
11095 	field_desc_show(sb, p[0], tp_la0);
11096 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11097 		field_desc_show(sb, p[1], tp_la0);
11098 }
11099 
11100 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)11101 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
11102 {
11103 
11104 	if (idx)
11105 		sbuf_printf(sb, "\n");
11106 	field_desc_show(sb, p[0], tp_la0);
11107 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11108 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
11109 }
11110 
11111 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)11112 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
11113 {
11114 	struct adapter *sc = arg1;
11115 	struct sbuf *sb;
11116 	uint64_t *buf, *p;
11117 	int rc;
11118 	u_int i, inc;
11119 	void (*show_func)(struct sbuf *, uint64_t *, int);
11120 
11121 	rc = 0;
11122 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11123 	if (sb == NULL)
11124 		return (ENOMEM);
11125 
11126 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11127 
11128 	mtx_lock(&sc->reg_lock);
11129 	if (hw_off_limits(sc))
11130 		rc = ENXIO;
11131 	else {
11132 		t4_tp_read_la(sc, buf, NULL);
11133 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11134 		case 2:
11135 			inc = 2;
11136 			show_func = tp_la_show2;
11137 			break;
11138 		case 3:
11139 			inc = 2;
11140 			show_func = tp_la_show3;
11141 			break;
11142 		default:
11143 			inc = 1;
11144 			show_func = tp_la_show;
11145 		}
11146 	}
11147 	mtx_unlock(&sc->reg_lock);
11148 	if (rc != 0)
11149 		goto done;
11150 
11151 	p = buf;
11152 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
11153 		(*show_func)(sb, p, i);
11154 	rc = sbuf_finish(sb);
11155 done:
11156 	sbuf_delete(sb);
11157 	free(buf, M_CXGBE);
11158 	return (rc);
11159 }
11160 
11161 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)11162 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
11163 {
11164 	struct adapter *sc = arg1;
11165 	struct sbuf *sb;
11166 	int rc;
11167 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
11168 
11169 	rc = 0;
11170 	mtx_lock(&sc->reg_lock);
11171 	if (hw_off_limits(sc))
11172 		rc = ENXIO;
11173 	else
11174 		t4_get_chan_txrate(sc, nrate, orate);
11175 	mtx_unlock(&sc->reg_lock);
11176 	if (rc != 0)
11177 		return (rc);
11178 
11179 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11180 	if (sb == NULL)
11181 		return (ENOMEM);
11182 
11183 	if (sc->chip_params->nchan > 2) {
11184 		sbuf_printf(sb, "              channel 0   channel 1"
11185 		    "   channel 2   channel 3\n");
11186 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
11187 		    nrate[0], nrate[1], nrate[2], nrate[3]);
11188 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
11189 		    orate[0], orate[1], orate[2], orate[3]);
11190 	} else {
11191 		sbuf_printf(sb, "              channel 0   channel 1\n");
11192 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
11193 		    nrate[0], nrate[1]);
11194 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
11195 		    orate[0], orate[1]);
11196 	}
11197 
11198 	rc = sbuf_finish(sb);
11199 	sbuf_delete(sb);
11200 
11201 	return (rc);
11202 }
11203 
11204 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)11205 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
11206 {
11207 	struct adapter *sc = arg1;
11208 	struct sbuf *sb;
11209 	uint32_t *buf, *p;
11210 	int rc, i;
11211 
11212 	rc = 0;
11213 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11214 	if (sb == NULL)
11215 		return (ENOMEM);
11216 
11217 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
11218 	    M_ZERO | M_WAITOK);
11219 
11220 	mtx_lock(&sc->reg_lock);
11221 	if (hw_off_limits(sc))
11222 		rc = ENXIO;
11223 	else
11224 		t4_ulprx_read_la(sc, buf);
11225 	mtx_unlock(&sc->reg_lock);
11226 	if (rc != 0)
11227 		goto done;
11228 
11229 	p = buf;
11230 	sbuf_printf(sb, "      Pcmd        Type   Message"
11231 	    "                Data");
11232 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
11233 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
11234 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
11235 	}
11236 	rc = sbuf_finish(sb);
11237 done:
11238 	sbuf_delete(sb);
11239 	free(buf, M_CXGBE);
11240 	return (rc);
11241 }
11242 
11243 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)11244 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
11245 {
11246 	struct adapter *sc = arg1;
11247 	struct sbuf *sb;
11248 	int rc;
11249 	uint32_t cfg, s1, s2;
11250 
11251 	MPASS(chip_id(sc) >= CHELSIO_T5);
11252 
11253 	rc = 0;
11254 	mtx_lock(&sc->reg_lock);
11255 	if (hw_off_limits(sc))
11256 		rc = ENXIO;
11257 	else {
11258 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
11259 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
11260 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
11261 	}
11262 	mtx_unlock(&sc->reg_lock);
11263 	if (rc != 0)
11264 		return (rc);
11265 
11266 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11267 	if (sb == NULL)
11268 		return (ENOMEM);
11269 
11270 	if (G_STATSOURCE_T5(cfg) == 7) {
11271 		int mode;
11272 
11273 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
11274 		if (mode == 0)
11275 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
11276 		else if (mode == 1)
11277 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
11278 		else
11279 			sbuf_printf(sb, "unknown mode %d", mode);
11280 	}
11281 	rc = sbuf_finish(sb);
11282 	sbuf_delete(sb);
11283 
11284 	return (rc);
11285 }
11286 
11287 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)11288 sysctl_cpus(SYSCTL_HANDLER_ARGS)
11289 {
11290 	struct adapter *sc = arg1;
11291 	enum cpu_sets op = arg2;
11292 	cpuset_t cpuset;
11293 	struct sbuf *sb;
11294 	int i, rc;
11295 
11296 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
11297 
11298 	CPU_ZERO(&cpuset);
11299 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
11300 	if (rc != 0)
11301 		return (rc);
11302 
11303 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11304 	if (sb == NULL)
11305 		return (ENOMEM);
11306 
11307 	CPU_FOREACH(i)
11308 		sbuf_printf(sb, "%d ", i);
11309 	rc = sbuf_finish(sb);
11310 	sbuf_delete(sb);
11311 
11312 	return (rc);
11313 }
11314 
11315 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)11316 sysctl_reset(SYSCTL_HANDLER_ARGS)
11317 {
11318 	struct adapter *sc = arg1;
11319 	u_int val;
11320 	int rc;
11321 
11322 	val = atomic_load_int(&sc->num_resets);
11323 	rc = sysctl_handle_int(oidp, &val, 0, req);
11324 	if (rc != 0 || req->newptr == NULL)
11325 		return (rc);
11326 
11327 	if (val == 0) {
11328 		/* Zero out the counter that tracks reset. */
11329 		atomic_store_int(&sc->num_resets, 0);
11330 		return (0);
11331 	}
11332 
11333 	if (val != 1)
11334 		return (EINVAL);	/* 0 or 1 are the only legal values */
11335 
11336 	if (hw_off_limits(sc))		/* harmless race */
11337 		return (EALREADY);
11338 
11339 	taskqueue_enqueue(reset_tq, &sc->reset_task);
11340 	return (0);
11341 }
11342 
11343 #ifdef TCP_OFFLOAD
11344 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)11345 sysctl_tls(SYSCTL_HANDLER_ARGS)
11346 {
11347 	struct adapter *sc = arg1;
11348 	int i, j, v, rc;
11349 	struct vi_info *vi;
11350 
11351 	v = sc->tt.tls;
11352 	rc = sysctl_handle_int(oidp, &v, 0, req);
11353 	if (rc != 0 || req->newptr == NULL)
11354 		return (rc);
11355 
11356 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11357 		return (ENOTSUP);
11358 
11359 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
11360 	if (rc)
11361 		return (rc);
11362 	if (hw_off_limits(sc))
11363 		rc = ENXIO;
11364 	else {
11365 		sc->tt.tls = !!v;
11366 		for_each_port(sc, i) {
11367 			for_each_vi(sc->port[i], j, vi) {
11368 				if (vi->flags & VI_INIT_DONE)
11369 					t4_update_fl_bufsize(vi->ifp);
11370 			}
11371 		}
11372 	}
11373 	end_synchronized_op(sc, 0);
11374 
11375 	return (rc);
11376 
11377 }
11378 
11379 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)11380 unit_conv(char *buf, size_t len, u_int val, u_int factor)
11381 {
11382 	u_int rem = val % factor;
11383 
11384 	if (rem == 0)
11385 		snprintf(buf, len, "%u", val / factor);
11386 	else {
11387 		while (rem % 10 == 0)
11388 			rem /= 10;
11389 		snprintf(buf, len, "%u.%u", val / factor, rem);
11390 	}
11391 }
11392 
11393 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)11394 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
11395 {
11396 	struct adapter *sc = arg1;
11397 	char buf[16];
11398 	u_int res, re;
11399 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11400 
11401 	mtx_lock(&sc->reg_lock);
11402 	if (hw_off_limits(sc))
11403 		res = (u_int)-1;
11404 	else
11405 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
11406 	mtx_unlock(&sc->reg_lock);
11407 	if (res == (u_int)-1)
11408 		return (ENXIO);
11409 
11410 	switch (arg2) {
11411 	case 0:
11412 		/* timer_tick */
11413 		re = G_TIMERRESOLUTION(res);
11414 		break;
11415 	case 1:
11416 		/* TCP timestamp tick */
11417 		re = G_TIMESTAMPRESOLUTION(res);
11418 		break;
11419 	case 2:
11420 		/* DACK tick */
11421 		re = G_DELAYEDACKRESOLUTION(res);
11422 		break;
11423 	default:
11424 		return (EDOOFUS);
11425 	}
11426 
11427 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
11428 
11429 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
11430 }
11431 
11432 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)11433 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
11434 {
11435 	struct adapter *sc = arg1;
11436 	int rc;
11437 	u_int dack_tmr, dack_re, v;
11438 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11439 
11440 	mtx_lock(&sc->reg_lock);
11441 	if (hw_off_limits(sc))
11442 		rc = ENXIO;
11443 	else {
11444 		rc = 0;
11445 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
11446 		    A_TP_TIMER_RESOLUTION));
11447 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
11448 	}
11449 	mtx_unlock(&sc->reg_lock);
11450 	if (rc != 0)
11451 		return (rc);
11452 
11453 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
11454 
11455 	return (sysctl_handle_int(oidp, &v, 0, req));
11456 }
11457 
11458 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)11459 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
11460 {
11461 	struct adapter *sc = arg1;
11462 	int rc, reg = arg2;
11463 	u_int tre;
11464 	u_long tp_tick_us, v;
11465 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11466 
11467 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
11468 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
11469 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
11470 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
11471 
11472 	mtx_lock(&sc->reg_lock);
11473 	if (hw_off_limits(sc))
11474 		rc = ENXIO;
11475 	else {
11476 		rc = 0;
11477 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
11478 		tp_tick_us = (cclk_ps << tre) / 1000000;
11479 		if (reg == A_TP_INIT_SRTT)
11480 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
11481 		else
11482 			v = tp_tick_us * t4_read_reg(sc, reg);
11483 	}
11484 	mtx_unlock(&sc->reg_lock);
11485 	if (rc != 0)
11486 		return (rc);
11487 	else
11488 		return (sysctl_handle_long(oidp, &v, 0, req));
11489 }
11490 
11491 /*
11492  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
11493  * passed to this function.
11494  */
11495 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)11496 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
11497 {
11498 	struct adapter *sc = arg1;
11499 	int rc, idx = arg2;
11500 	u_int v;
11501 
11502 	MPASS(idx >= 0 && idx <= 24);
11503 
11504 	mtx_lock(&sc->reg_lock);
11505 	if (hw_off_limits(sc))
11506 		rc = ENXIO;
11507 	else {
11508 		rc = 0;
11509 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
11510 	}
11511 	mtx_unlock(&sc->reg_lock);
11512 	if (rc != 0)
11513 		return (rc);
11514 	else
11515 		return (sysctl_handle_int(oidp, &v, 0, req));
11516 }
11517 
11518 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)11519 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
11520 {
11521 	struct adapter *sc = arg1;
11522 	int rc, idx = arg2;
11523 	u_int shift, v, r;
11524 
11525 	MPASS(idx >= 0 && idx < 16);
11526 
11527 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
11528 	shift = (idx & 3) << 3;
11529 	mtx_lock(&sc->reg_lock);
11530 	if (hw_off_limits(sc))
11531 		rc = ENXIO;
11532 	else {
11533 		rc = 0;
11534 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
11535 	}
11536 	mtx_unlock(&sc->reg_lock);
11537 	if (rc != 0)
11538 		return (rc);
11539 	else
11540 		return (sysctl_handle_int(oidp, &v, 0, req));
11541 }
11542 
11543 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)11544 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
11545 {
11546 	struct vi_info *vi = arg1;
11547 	struct adapter *sc = vi->adapter;
11548 	int idx, rc, i;
11549 	struct sge_ofld_rxq *ofld_rxq;
11550 	uint8_t v;
11551 
11552 	idx = vi->ofld_tmr_idx;
11553 
11554 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11555 	if (rc != 0 || req->newptr == NULL)
11556 		return (rc);
11557 
11558 	if (idx < 0 || idx >= SGE_NTIMERS)
11559 		return (EINVAL);
11560 
11561 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11562 	    "t4otmr");
11563 	if (rc)
11564 		return (rc);
11565 
11566 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
11567 	for_each_ofld_rxq(vi, i, ofld_rxq) {
11568 #ifdef atomic_store_rel_8
11569 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
11570 #else
11571 		ofld_rxq->iq.intr_params = v;
11572 #endif
11573 	}
11574 	vi->ofld_tmr_idx = idx;
11575 
11576 	end_synchronized_op(sc, LOCK_HELD);
11577 	return (0);
11578 }
11579 
11580 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)11581 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
11582 {
11583 	struct vi_info *vi = arg1;
11584 	struct adapter *sc = vi->adapter;
11585 	int idx, rc;
11586 
11587 	idx = vi->ofld_pktc_idx;
11588 
11589 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11590 	if (rc != 0 || req->newptr == NULL)
11591 		return (rc);
11592 
11593 	if (idx < -1 || idx >= SGE_NCOUNTERS)
11594 		return (EINVAL);
11595 
11596 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11597 	    "t4opktc");
11598 	if (rc)
11599 		return (rc);
11600 
11601 	if (vi->flags & VI_INIT_DONE)
11602 		rc = EBUSY; /* cannot be changed once the queues are created */
11603 	else
11604 		vi->ofld_pktc_idx = idx;
11605 
11606 	end_synchronized_op(sc, LOCK_HELD);
11607 	return (rc);
11608 }
11609 #endif
11610 
11611 static int
get_sge_context(struct adapter * sc,struct t4_sge_context * cntxt)11612 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
11613 {
11614 	int rc;
11615 
11616 	if (cntxt->cid > M_CTXTQID)
11617 		return (EINVAL);
11618 
11619 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
11620 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
11621 		return (EINVAL);
11622 
11623 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
11624 	if (rc)
11625 		return (rc);
11626 
11627 	if (hw_off_limits(sc)) {
11628 		rc = ENXIO;
11629 		goto done;
11630 	}
11631 
11632 	if (sc->flags & FW_OK) {
11633 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
11634 		    &cntxt->data[0]);
11635 		if (rc == 0)
11636 			goto done;
11637 	}
11638 
11639 	/*
11640 	 * Read via firmware failed or wasn't even attempted.  Read directly via
11641 	 * the backdoor.
11642 	 */
11643 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
11644 done:
11645 	end_synchronized_op(sc, 0);
11646 	return (rc);
11647 }
11648 
11649 static int
load_fw(struct adapter * sc,struct t4_data * fw)11650 load_fw(struct adapter *sc, struct t4_data *fw)
11651 {
11652 	int rc;
11653 	uint8_t *fw_data;
11654 
11655 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
11656 	if (rc)
11657 		return (rc);
11658 
11659 	if (hw_off_limits(sc)) {
11660 		rc = ENXIO;
11661 		goto done;
11662 	}
11663 
11664 	/*
11665 	 * The firmware, with the sole exception of the memory parity error
11666 	 * handler, runs from memory and not flash.  It is almost always safe to
11667 	 * install a new firmware on a running system.  Just set bit 1 in
11668 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
11669 	 */
11670 	if (sc->flags & FULL_INIT_DONE &&
11671 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
11672 		rc = EBUSY;
11673 		goto done;
11674 	}
11675 
11676 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
11677 
11678 	rc = copyin(fw->data, fw_data, fw->len);
11679 	if (rc == 0)
11680 		rc = -t4_load_fw(sc, fw_data, fw->len);
11681 
11682 	free(fw_data, M_CXGBE);
11683 done:
11684 	end_synchronized_op(sc, 0);
11685 	return (rc);
11686 }
11687 
11688 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)11689 load_cfg(struct adapter *sc, struct t4_data *cfg)
11690 {
11691 	int rc;
11692 	uint8_t *cfg_data = NULL;
11693 
11694 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11695 	if (rc)
11696 		return (rc);
11697 
11698 	if (hw_off_limits(sc)) {
11699 		rc = ENXIO;
11700 		goto done;
11701 	}
11702 
11703 	if (cfg->len == 0) {
11704 		/* clear */
11705 		rc = -t4_load_cfg(sc, NULL, 0);
11706 		goto done;
11707 	}
11708 
11709 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
11710 
11711 	rc = copyin(cfg->data, cfg_data, cfg->len);
11712 	if (rc == 0)
11713 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
11714 
11715 	free(cfg_data, M_CXGBE);
11716 done:
11717 	end_synchronized_op(sc, 0);
11718 	return (rc);
11719 }
11720 
11721 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)11722 load_boot(struct adapter *sc, struct t4_bootrom *br)
11723 {
11724 	int rc;
11725 	uint8_t *br_data = NULL;
11726 	u_int offset;
11727 
11728 	if (br->len > 1024 * 1024)
11729 		return (EFBIG);
11730 
11731 	if (br->pf_offset == 0) {
11732 		/* pfidx */
11733 		if (br->pfidx_addr > 7)
11734 			return (EINVAL);
11735 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
11736 		    A_PCIE_PF_EXPROM_OFST)));
11737 	} else if (br->pf_offset == 1) {
11738 		/* offset */
11739 		offset = G_OFFSET(br->pfidx_addr);
11740 	} else {
11741 		return (EINVAL);
11742 	}
11743 
11744 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
11745 	if (rc)
11746 		return (rc);
11747 
11748 	if (hw_off_limits(sc)) {
11749 		rc = ENXIO;
11750 		goto done;
11751 	}
11752 
11753 	if (br->len == 0) {
11754 		/* clear */
11755 		rc = -t4_load_boot(sc, NULL, offset, 0);
11756 		goto done;
11757 	}
11758 
11759 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
11760 
11761 	rc = copyin(br->data, br_data, br->len);
11762 	if (rc == 0)
11763 		rc = -t4_load_boot(sc, br_data, offset, br->len);
11764 
11765 	free(br_data, M_CXGBE);
11766 done:
11767 	end_synchronized_op(sc, 0);
11768 	return (rc);
11769 }
11770 
11771 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)11772 load_bootcfg(struct adapter *sc, struct t4_data *bc)
11773 {
11774 	int rc;
11775 	uint8_t *bc_data = NULL;
11776 
11777 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11778 	if (rc)
11779 		return (rc);
11780 
11781 	if (hw_off_limits(sc)) {
11782 		rc = ENXIO;
11783 		goto done;
11784 	}
11785 
11786 	if (bc->len == 0) {
11787 		/* clear */
11788 		rc = -t4_load_bootcfg(sc, NULL, 0);
11789 		goto done;
11790 	}
11791 
11792 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
11793 
11794 	rc = copyin(bc->data, bc_data, bc->len);
11795 	if (rc == 0)
11796 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
11797 
11798 	free(bc_data, M_CXGBE);
11799 done:
11800 	end_synchronized_op(sc, 0);
11801 	return (rc);
11802 }
11803 
11804 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)11805 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
11806 {
11807 	int rc;
11808 	struct cudbg_init *cudbg;
11809 	void *handle, *buf;
11810 
11811 	/* buf is large, don't block if no memory is available */
11812 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
11813 	if (buf == NULL)
11814 		return (ENOMEM);
11815 
11816 	handle = cudbg_alloc_handle();
11817 	if (handle == NULL) {
11818 		rc = ENOMEM;
11819 		goto done;
11820 	}
11821 
11822 	cudbg = cudbg_get_init(handle);
11823 	cudbg->adap = sc;
11824 	cudbg->print = (cudbg_print_cb)printf;
11825 
11826 #ifndef notyet
11827 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
11828 	    __func__, dump->wr_flash, dump->len, dump->data);
11829 #endif
11830 
11831 	if (dump->wr_flash)
11832 		cudbg->use_flash = 1;
11833 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
11834 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
11835 
11836 	rc = cudbg_collect(handle, buf, &dump->len);
11837 	if (rc != 0)
11838 		goto done;
11839 
11840 	rc = copyout(buf, dump->data, dump->len);
11841 done:
11842 	cudbg_free_handle(handle);
11843 	free(buf, M_CXGBE);
11844 	return (rc);
11845 }
11846 
11847 static void
free_offload_policy(struct t4_offload_policy * op)11848 free_offload_policy(struct t4_offload_policy *op)
11849 {
11850 	struct offload_rule *r;
11851 	int i;
11852 
11853 	if (op == NULL)
11854 		return;
11855 
11856 	r = &op->rule[0];
11857 	for (i = 0; i < op->nrules; i++, r++) {
11858 		free(r->bpf_prog.bf_insns, M_CXGBE);
11859 	}
11860 	free(op->rule, M_CXGBE);
11861 	free(op, M_CXGBE);
11862 }
11863 
11864 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)11865 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
11866 {
11867 	int i, rc, len;
11868 	struct t4_offload_policy *op, *old;
11869 	struct bpf_program *bf;
11870 	const struct offload_settings *s;
11871 	struct offload_rule *r;
11872 	void *u;
11873 
11874 	if (!is_offload(sc))
11875 		return (ENODEV);
11876 
11877 	if (uop->nrules == 0) {
11878 		/* Delete installed policies. */
11879 		op = NULL;
11880 		goto set_policy;
11881 	} else if (uop->nrules > 256) { /* arbitrary */
11882 		return (E2BIG);
11883 	}
11884 
11885 	/* Copy userspace offload policy to kernel */
11886 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
11887 	op->nrules = uop->nrules;
11888 	len = op->nrules * sizeof(struct offload_rule);
11889 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11890 	rc = copyin(uop->rule, op->rule, len);
11891 	if (rc) {
11892 		free(op->rule, M_CXGBE);
11893 		free(op, M_CXGBE);
11894 		return (rc);
11895 	}
11896 
11897 	r = &op->rule[0];
11898 	for (i = 0; i < op->nrules; i++, r++) {
11899 
11900 		/* Validate open_type */
11901 		if (r->open_type != OPEN_TYPE_LISTEN &&
11902 		    r->open_type != OPEN_TYPE_ACTIVE &&
11903 		    r->open_type != OPEN_TYPE_PASSIVE &&
11904 		    r->open_type != OPEN_TYPE_DONTCARE) {
11905 error:
11906 			/*
11907 			 * Rules 0 to i have malloc'd filters that need to be
11908 			 * freed.  Rules i+1 to nrules have userspace pointers
11909 			 * and should be left alone.
11910 			 */
11911 			op->nrules = i;
11912 			free_offload_policy(op);
11913 			return (rc);
11914 		}
11915 
11916 		/* Validate settings */
11917 		s = &r->settings;
11918 		if ((s->offload != 0 && s->offload != 1) ||
11919 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
11920 		    s->sched_class < -1 ||
11921 		    s->sched_class >= sc->params.nsched_cls) {
11922 			rc = EINVAL;
11923 			goto error;
11924 		}
11925 
11926 		bf = &r->bpf_prog;
11927 		u = bf->bf_insns;	/* userspace ptr */
11928 		bf->bf_insns = NULL;
11929 		if (bf->bf_len == 0) {
11930 			/* legal, matches everything */
11931 			continue;
11932 		}
11933 		len = bf->bf_len * sizeof(*bf->bf_insns);
11934 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11935 		rc = copyin(u, bf->bf_insns, len);
11936 		if (rc != 0)
11937 			goto error;
11938 
11939 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
11940 			rc = EINVAL;
11941 			goto error;
11942 		}
11943 	}
11944 set_policy:
11945 	rw_wlock(&sc->policy_lock);
11946 	old = sc->policy;
11947 	sc->policy = op;
11948 	rw_wunlock(&sc->policy_lock);
11949 	free_offload_policy(old);
11950 
11951 	return (0);
11952 }
11953 
11954 #define MAX_READ_BUF_SIZE (128 * 1024)
11955 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)11956 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
11957 {
11958 	uint32_t addr, remaining, n;
11959 	uint32_t *buf;
11960 	int rc;
11961 	uint8_t *dst;
11962 
11963 	mtx_lock(&sc->reg_lock);
11964 	if (hw_off_limits(sc))
11965 		rc = ENXIO;
11966 	else
11967 		rc = validate_mem_range(sc, mr->addr, mr->len);
11968 	mtx_unlock(&sc->reg_lock);
11969 	if (rc != 0)
11970 		return (rc);
11971 
11972 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
11973 	addr = mr->addr;
11974 	remaining = mr->len;
11975 	dst = (void *)mr->data;
11976 
11977 	while (remaining) {
11978 		n = min(remaining, MAX_READ_BUF_SIZE);
11979 		mtx_lock(&sc->reg_lock);
11980 		if (hw_off_limits(sc))
11981 			rc = ENXIO;
11982 		else
11983 			read_via_memwin(sc, 2, addr, buf, n);
11984 		mtx_unlock(&sc->reg_lock);
11985 		if (rc != 0)
11986 			break;
11987 
11988 		rc = copyout(buf, dst, n);
11989 		if (rc != 0)
11990 			break;
11991 
11992 		dst += n;
11993 		remaining -= n;
11994 		addr += n;
11995 	}
11996 
11997 	free(buf, M_CXGBE);
11998 	return (rc);
11999 }
12000 #undef MAX_READ_BUF_SIZE
12001 
12002 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)12003 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
12004 {
12005 	int rc;
12006 
12007 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
12008 		return (EINVAL);
12009 
12010 	if (i2cd->len > sizeof(i2cd->data))
12011 		return (EFBIG);
12012 
12013 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
12014 	if (rc)
12015 		return (rc);
12016 	if (hw_off_limits(sc))
12017 		rc = ENXIO;
12018 	else
12019 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
12020 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
12021 	end_synchronized_op(sc, 0);
12022 
12023 	return (rc);
12024 }
12025 
12026 static int
clear_stats(struct adapter * sc,u_int port_id)12027 clear_stats(struct adapter *sc, u_int port_id)
12028 {
12029 	int i, v, chan_map;
12030 	struct port_info *pi;
12031 	struct vi_info *vi;
12032 	struct sge_rxq *rxq;
12033 	struct sge_txq *txq;
12034 	struct sge_wrq *wrq;
12035 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12036 	struct sge_ofld_txq *ofld_txq;
12037 #endif
12038 #ifdef TCP_OFFLOAD
12039 	struct sge_ofld_rxq *ofld_rxq;
12040 #endif
12041 
12042 	if (port_id >= sc->params.nports)
12043 		return (EINVAL);
12044 	pi = sc->port[port_id];
12045 	if (pi == NULL)
12046 		return (EIO);
12047 
12048 	mtx_lock(&sc->reg_lock);
12049 	if (!hw_off_limits(sc)) {
12050 		/* MAC stats */
12051 		t4_clr_port_stats(sc, pi->tx_chan);
12052 		if (is_t6(sc)) {
12053 			if (pi->fcs_reg != -1)
12054 				pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12055 			else
12056 				pi->stats.rx_fcs_err = 0;
12057 		}
12058 		for_each_vi(pi, v, vi) {
12059 			if (vi->flags & VI_INIT_DONE)
12060 				t4_clr_vi_stats(sc, vi->vin);
12061 		}
12062 		chan_map = pi->rx_e_chan_map;
12063 		v = 0;	/* reuse */
12064 		while (chan_map) {
12065 			i = ffs(chan_map) - 1;
12066 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
12067 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
12068 			chan_map &= ~(1 << i);
12069 		}
12070 	}
12071 	mtx_unlock(&sc->reg_lock);
12072 	pi->tx_parse_error = 0;
12073 	pi->tnl_cong_drops = 0;
12074 
12075 	/*
12076 	 * Since this command accepts a port, clear stats for
12077 	 * all VIs on this port.
12078 	 */
12079 	for_each_vi(pi, v, vi) {
12080 		if (vi->flags & VI_INIT_DONE) {
12081 
12082 			for_each_rxq(vi, i, rxq) {
12083 #if defined(INET) || defined(INET6)
12084 				rxq->lro.lro_queued = 0;
12085 				rxq->lro.lro_flushed = 0;
12086 #endif
12087 				rxq->rxcsum = 0;
12088 				rxq->vlan_extraction = 0;
12089 				rxq->vxlan_rxcsum = 0;
12090 
12091 				rxq->fl.cl_allocated = 0;
12092 				rxq->fl.cl_recycled = 0;
12093 				rxq->fl.cl_fast_recycled = 0;
12094 			}
12095 
12096 			for_each_txq(vi, i, txq) {
12097 				txq->txcsum = 0;
12098 				txq->tso_wrs = 0;
12099 				txq->vlan_insertion = 0;
12100 				txq->imm_wrs = 0;
12101 				txq->sgl_wrs = 0;
12102 				txq->txpkt_wrs = 0;
12103 				txq->txpkts0_wrs = 0;
12104 				txq->txpkts1_wrs = 0;
12105 				txq->txpkts0_pkts = 0;
12106 				txq->txpkts1_pkts = 0;
12107 				txq->txpkts_flush = 0;
12108 				txq->raw_wrs = 0;
12109 				txq->vxlan_tso_wrs = 0;
12110 				txq->vxlan_txcsum = 0;
12111 				txq->kern_tls_records = 0;
12112 				txq->kern_tls_short = 0;
12113 				txq->kern_tls_partial = 0;
12114 				txq->kern_tls_full = 0;
12115 				txq->kern_tls_octets = 0;
12116 				txq->kern_tls_waste = 0;
12117 				txq->kern_tls_options = 0;
12118 				txq->kern_tls_header = 0;
12119 				txq->kern_tls_fin = 0;
12120 				txq->kern_tls_fin_short = 0;
12121 				txq->kern_tls_cbc = 0;
12122 				txq->kern_tls_gcm = 0;
12123 				mp_ring_reset_stats(txq->r);
12124 			}
12125 
12126 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12127 			for_each_ofld_txq(vi, i, ofld_txq) {
12128 				ofld_txq->wrq.tx_wrs_direct = 0;
12129 				ofld_txq->wrq.tx_wrs_copied = 0;
12130 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12131 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12132 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12133 				counter_u64_zero(ofld_txq->tx_aio_jobs);
12134 				counter_u64_zero(ofld_txq->tx_aio_octets);
12135 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
12136 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
12137 			}
12138 #endif
12139 #ifdef TCP_OFFLOAD
12140 			for_each_ofld_rxq(vi, i, ofld_rxq) {
12141 				ofld_rxq->fl.cl_allocated = 0;
12142 				ofld_rxq->fl.cl_recycled = 0;
12143 				ofld_rxq->fl.cl_fast_recycled = 0;
12144 				counter_u64_zero(
12145 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
12146 				counter_u64_zero(
12147 				    ofld_rxq->rx_iscsi_ddp_setup_error);
12148 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
12149 				ofld_rxq->rx_iscsi_ddp_octets = 0;
12150 				ofld_rxq->rx_iscsi_fl_pdus = 0;
12151 				ofld_rxq->rx_iscsi_fl_octets = 0;
12152 				ofld_rxq->rx_aio_ddp_jobs = 0;
12153 				ofld_rxq->rx_aio_ddp_octets = 0;
12154 				ofld_rxq->rx_toe_tls_records = 0;
12155 				ofld_rxq->rx_toe_tls_octets = 0;
12156 				ofld_rxq->rx_toe_ddp_octets = 0;
12157 				counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
12158 				counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
12159 				counter_u64_zero(ofld_rxq->ddp_buffer_free);
12160 			}
12161 #endif
12162 
12163 			if (IS_MAIN_VI(vi)) {
12164 				wrq = &sc->sge.ctrlq[pi->port_id];
12165 				wrq->tx_wrs_direct = 0;
12166 				wrq->tx_wrs_copied = 0;
12167 			}
12168 		}
12169 	}
12170 
12171 	return (0);
12172 }
12173 
12174 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12175 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12176 {
12177 #ifdef INET6
12178 	struct in6_addr in6;
12179 
12180 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12181 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
12182 		return (0);
12183 	else
12184 		return (EIO);
12185 #else
12186 	return (ENOTSUP);
12187 #endif
12188 }
12189 
12190 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12191 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12192 {
12193 #ifdef INET6
12194 	struct in6_addr in6;
12195 
12196 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12197 	return (t4_release_clip_addr(sc, &in6));
12198 #else
12199 	return (ENOTSUP);
12200 #endif
12201 }
12202 
12203 int
t4_os_find_pci_capability(struct adapter * sc,int cap)12204 t4_os_find_pci_capability(struct adapter *sc, int cap)
12205 {
12206 	int i;
12207 
12208 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
12209 }
12210 
12211 void
t4_os_portmod_changed(struct port_info * pi)12212 t4_os_portmod_changed(struct port_info *pi)
12213 {
12214 	struct adapter *sc = pi->adapter;
12215 	struct vi_info *vi;
12216 	if_t ifp;
12217 	static const char *mod_str[] = {
12218 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM",
12219 		"LR_SIMPLEX", "DR"
12220 	};
12221 
12222 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
12223 	    ("%s: port_type %u", __func__, pi->port_type));
12224 
12225 	vi = &pi->vi[0];
12226 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
12227 		PORT_LOCK(pi);
12228 		build_medialist(pi);
12229 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
12230 			fixup_link_config(pi);
12231 			apply_link_config(pi);
12232 		}
12233 		PORT_UNLOCK(pi);
12234 		end_synchronized_op(sc, LOCK_HELD);
12235 	}
12236 
12237 	ifp = vi->ifp;
12238 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
12239 		if_printf(ifp, "transceiver unplugged.\n");
12240 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
12241 		if_printf(ifp, "unknown transceiver inserted.\n");
12242 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
12243 		if_printf(ifp, "unsupported transceiver inserted.\n");
12244 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
12245 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
12246 		    port_top_speed(pi), mod_str[pi->mod_type]);
12247 	} else {
12248 		if_printf(ifp, "transceiver (type %d) inserted.\n",
12249 		    pi->mod_type);
12250 	}
12251 }
12252 
12253 void
t4_os_link_changed(struct port_info * pi)12254 t4_os_link_changed(struct port_info *pi)
12255 {
12256 	struct vi_info *vi;
12257 	if_t ifp;
12258 	struct link_config *lc = &pi->link_cfg;
12259 	struct adapter *sc = pi->adapter;
12260 	int v;
12261 
12262 	PORT_LOCK_ASSERT_OWNED(pi);
12263 
12264 	if (is_t6(sc)) {
12265 		if (lc->link_ok) {
12266 			if (lc->speed > 25000 ||
12267 			    (lc->speed == 25000 && lc->fec == FEC_RS)) {
12268 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12269 				    A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS);
12270 			} else {
12271 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12272 				    A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS);
12273 			}
12274 			pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12275 			pi->stats.rx_fcs_err = 0;
12276 		} else {
12277 			pi->fcs_reg = -1;
12278 		}
12279 	} else {
12280 		MPASS(pi->fcs_reg != -1);
12281 		MPASS(pi->fcs_base == 0);
12282 	}
12283 
12284 	for_each_vi(pi, v, vi) {
12285 		ifp = vi->ifp;
12286 		if (ifp == NULL || IS_DETACHING(vi))
12287 			continue;
12288 
12289 		if (lc->link_ok) {
12290 			if_setbaudrate(ifp, IF_Mbps(lc->speed));
12291 			if_link_state_change(ifp, LINK_STATE_UP);
12292 		} else {
12293 			if_link_state_change(ifp, LINK_STATE_DOWN);
12294 		}
12295 	}
12296 }
12297 
12298 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)12299 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
12300 {
12301 	struct adapter *sc;
12302 
12303 	sx_slock(&t4_list_lock);
12304 	SLIST_FOREACH(sc, &t4_list, link) {
12305 		/*
12306 		 * func should not make any assumptions about what state sc is
12307 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
12308 		 */
12309 		func(sc, arg);
12310 	}
12311 	sx_sunlock(&t4_list_lock);
12312 }
12313 
12314 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)12315 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
12316     struct thread *td)
12317 {
12318 	int rc;
12319 	struct adapter *sc = dev->si_drv1;
12320 
12321 	rc = priv_check(td, PRIV_DRIVER);
12322 	if (rc != 0)
12323 		return (rc);
12324 
12325 	switch (cmd) {
12326 	case CHELSIO_T4_GETREG: {
12327 		struct t4_reg *edata = (struct t4_reg *)data;
12328 
12329 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12330 			return (EFAULT);
12331 
12332 		mtx_lock(&sc->reg_lock);
12333 		if (hw_off_limits(sc))
12334 			rc = ENXIO;
12335 		else if (edata->size == 4)
12336 			edata->val = t4_read_reg(sc, edata->addr);
12337 		else if (edata->size == 8)
12338 			edata->val = t4_read_reg64(sc, edata->addr);
12339 		else
12340 			rc = EINVAL;
12341 		mtx_unlock(&sc->reg_lock);
12342 
12343 		break;
12344 	}
12345 	case CHELSIO_T4_SETREG: {
12346 		struct t4_reg *edata = (struct t4_reg *)data;
12347 
12348 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12349 			return (EFAULT);
12350 
12351 		mtx_lock(&sc->reg_lock);
12352 		if (hw_off_limits(sc))
12353 			rc = ENXIO;
12354 		else if (edata->size == 4) {
12355 			if (edata->val & 0xffffffff00000000)
12356 				rc = EINVAL;
12357 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
12358 		} else if (edata->size == 8)
12359 			t4_write_reg64(sc, edata->addr, edata->val);
12360 		else
12361 			rc = EINVAL;
12362 		mtx_unlock(&sc->reg_lock);
12363 
12364 		break;
12365 	}
12366 	case CHELSIO_T4_REGDUMP: {
12367 		struct t4_regdump *regs = (struct t4_regdump *)data;
12368 		int reglen = t4_get_regs_len(sc);
12369 		uint8_t *buf;
12370 
12371 		if (regs->len < reglen) {
12372 			regs->len = reglen; /* hint to the caller */
12373 			return (ENOBUFS);
12374 		}
12375 
12376 		regs->len = reglen;
12377 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
12378 		mtx_lock(&sc->reg_lock);
12379 		if (hw_off_limits(sc))
12380 			rc = ENXIO;
12381 		else
12382 			get_regs(sc, regs, buf);
12383 		mtx_unlock(&sc->reg_lock);
12384 		if (rc == 0)
12385 			rc = copyout(buf, regs->data, reglen);
12386 		free(buf, M_CXGBE);
12387 		break;
12388 	}
12389 	case CHELSIO_T4_GET_FILTER_MODE:
12390 		rc = get_filter_mode(sc, (uint32_t *)data);
12391 		break;
12392 	case CHELSIO_T4_SET_FILTER_MODE:
12393 		rc = set_filter_mode(sc, *(uint32_t *)data);
12394 		break;
12395 	case CHELSIO_T4_SET_FILTER_MASK:
12396 		rc = set_filter_mask(sc, *(uint32_t *)data);
12397 		break;
12398 	case CHELSIO_T4_GET_FILTER:
12399 		rc = get_filter(sc, (struct t4_filter *)data);
12400 		break;
12401 	case CHELSIO_T4_SET_FILTER:
12402 		rc = set_filter(sc, (struct t4_filter *)data);
12403 		break;
12404 	case CHELSIO_T4_DEL_FILTER:
12405 		rc = del_filter(sc, (struct t4_filter *)data);
12406 		break;
12407 	case CHELSIO_T4_GET_SGE_CONTEXT:
12408 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
12409 		break;
12410 	case CHELSIO_T4_LOAD_FW:
12411 		rc = load_fw(sc, (struct t4_data *)data);
12412 		break;
12413 	case CHELSIO_T4_GET_MEM:
12414 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
12415 		break;
12416 	case CHELSIO_T4_GET_I2C:
12417 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
12418 		break;
12419 	case CHELSIO_T4_CLEAR_STATS:
12420 		rc = clear_stats(sc, *(uint32_t *)data);
12421 		break;
12422 	case CHELSIO_T4_SCHED_CLASS:
12423 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
12424 		break;
12425 	case CHELSIO_T4_SCHED_QUEUE:
12426 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
12427 		break;
12428 	case CHELSIO_T4_GET_TRACER:
12429 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
12430 		break;
12431 	case CHELSIO_T4_SET_TRACER:
12432 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
12433 		break;
12434 	case CHELSIO_T4_LOAD_CFG:
12435 		rc = load_cfg(sc, (struct t4_data *)data);
12436 		break;
12437 	case CHELSIO_T4_LOAD_BOOT:
12438 		rc = load_boot(sc, (struct t4_bootrom *)data);
12439 		break;
12440 	case CHELSIO_T4_LOAD_BOOTCFG:
12441 		rc = load_bootcfg(sc, (struct t4_data *)data);
12442 		break;
12443 	case CHELSIO_T4_CUDBG_DUMP:
12444 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
12445 		break;
12446 	case CHELSIO_T4_SET_OFLD_POLICY:
12447 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
12448 		break;
12449 	case CHELSIO_T4_HOLD_CLIP_ADDR:
12450 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
12451 		break;
12452 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
12453 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
12454 		break;
12455 	default:
12456 		rc = ENOTTY;
12457 	}
12458 
12459 	return (rc);
12460 }
12461 
12462 #ifdef TCP_OFFLOAD
12463 int
toe_capability(struct vi_info * vi,bool enable)12464 toe_capability(struct vi_info *vi, bool enable)
12465 {
12466 	int rc;
12467 	struct port_info *pi = vi->pi;
12468 	struct adapter *sc = pi->adapter;
12469 
12470 	ASSERT_SYNCHRONIZED_OP(sc);
12471 
12472 	if (!is_offload(sc))
12473 		return (ENODEV);
12474 	if (!hw_all_ok(sc))
12475 		return (ENXIO);
12476 
12477 	if (enable) {
12478 #ifdef KERN_TLS
12479 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
12480 			int i, j, n;
12481 			struct port_info *p;
12482 			struct vi_info *v;
12483 
12484 			/*
12485 			 * Reconfigure hardware for TOE if TXTLS is not enabled
12486 			 * on any ifnet.
12487 			 */
12488 			n = 0;
12489 			for_each_port(sc, i) {
12490 				p = sc->port[i];
12491 				for_each_vi(p, j, v) {
12492 					if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
12493 						CH_WARN(sc,
12494 						    "%s has NIC TLS enabled.\n",
12495 						    device_get_nameunit(v->dev));
12496 						n++;
12497 					}
12498 				}
12499 			}
12500 			if (n > 0) {
12501 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
12502 				    "associated with this adapter before "
12503 				    "trying to enable TOE.\n");
12504 				return (EAGAIN);
12505 			}
12506 			rc = t6_config_kern_tls(sc, false);
12507 			if (rc)
12508 				return (rc);
12509 		}
12510 #endif
12511 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
12512 			/* TOE is already enabled. */
12513 			return (0);
12514 		}
12515 
12516 		/*
12517 		 * We need the port's queues around so that we're able to send
12518 		 * and receive CPLs to/from the TOE even if the ifnet for this
12519 		 * port has never been UP'd administratively.
12520 		 */
12521 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
12522 			return (rc);
12523 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
12524 		    ((rc = vi_init(&pi->vi[0])) != 0))
12525 			return (rc);
12526 
12527 		if (isset(&sc->offload_map, pi->port_id)) {
12528 			/* TOE is enabled on another VI of this port. */
12529 			MPASS(pi->uld_vis > 0);
12530 			pi->uld_vis++;
12531 			return (0);
12532 		}
12533 
12534 		if (!uld_active(sc, ULD_TOM)) {
12535 			rc = t4_activate_uld(sc, ULD_TOM);
12536 			if (rc == EAGAIN) {
12537 				log(LOG_WARNING,
12538 				    "You must kldload t4_tom.ko before trying "
12539 				    "to enable TOE on a cxgbe interface.\n");
12540 			}
12541 			if (rc != 0)
12542 				return (rc);
12543 			KASSERT(sc->tom_softc != NULL,
12544 			    ("%s: TOM activated but softc NULL", __func__));
12545 			KASSERT(uld_active(sc, ULD_TOM),
12546 			    ("%s: TOM activated but flag not set", __func__));
12547 		}
12548 
12549 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
12550 		if (!uld_active(sc, ULD_IWARP))
12551 			(void) t4_activate_uld(sc, ULD_IWARP);
12552 		if (!uld_active(sc, ULD_ISCSI))
12553 			(void) t4_activate_uld(sc, ULD_ISCSI);
12554 
12555 		if (pi->uld_vis++ == 0)
12556 			setbit(&sc->offload_map, pi->port_id);
12557 	} else {
12558 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) == 0) {
12559 			/* TOE is already disabled. */
12560 			return (0);
12561 		}
12562 		MPASS(isset(&sc->offload_map, pi->port_id));
12563 		MPASS(pi->uld_vis > 0);
12564 		if (--pi->uld_vis == 0)
12565 			clrbit(&sc->offload_map, pi->port_id);
12566 	}
12567 
12568 	return (0);
12569 }
12570 
12571 /*
12572  * Add an upper layer driver to the global list.
12573  */
12574 int
t4_register_uld(struct uld_info * ui,int id)12575 t4_register_uld(struct uld_info *ui, int id)
12576 {
12577 	int rc;
12578 
12579 	if (id < 0 || id > ULD_MAX)
12580 		return (EINVAL);
12581 	sx_xlock(&t4_uld_list_lock);
12582 	if (t4_uld_list[id] != NULL)
12583 		rc = EEXIST;
12584 	else {
12585 		t4_uld_list[id] = ui;
12586 		rc = 0;
12587 	}
12588 	sx_xunlock(&t4_uld_list_lock);
12589 	return (rc);
12590 }
12591 
12592 int
t4_unregister_uld(struct uld_info * ui,int id)12593 t4_unregister_uld(struct uld_info *ui, int id)
12594 {
12595 
12596 	if (id < 0 || id > ULD_MAX)
12597 		return (EINVAL);
12598 	sx_xlock(&t4_uld_list_lock);
12599 	MPASS(t4_uld_list[id] == ui);
12600 	t4_uld_list[id] = NULL;
12601 	sx_xunlock(&t4_uld_list_lock);
12602 	return (0);
12603 }
12604 
12605 int
t4_activate_uld(struct adapter * sc,int id)12606 t4_activate_uld(struct adapter *sc, int id)
12607 {
12608 	int rc;
12609 
12610 	ASSERT_SYNCHRONIZED_OP(sc);
12611 
12612 	if (id < 0 || id > ULD_MAX)
12613 		return (EINVAL);
12614 
12615 	/* Adapter needs to be initialized before any ULD can be activated. */
12616 	if (!(sc->flags & FULL_INIT_DONE)) {
12617 		rc = adapter_init(sc);
12618 		if (rc != 0)
12619 			return (rc);
12620 	}
12621 
12622 	sx_slock(&t4_uld_list_lock);
12623 	if (t4_uld_list[id] == NULL)
12624 		rc = EAGAIN;	/* load the KLD with this ULD and try again. */
12625 	else {
12626 		rc = t4_uld_list[id]->uld_activate(sc);
12627 		if (rc == 0)
12628 			setbit(&sc->active_ulds, id);
12629 	}
12630 	sx_sunlock(&t4_uld_list_lock);
12631 
12632 	return (rc);
12633 }
12634 
12635 int
t4_deactivate_uld(struct adapter * sc,int id)12636 t4_deactivate_uld(struct adapter *sc, int id)
12637 {
12638 	int rc;
12639 
12640 	ASSERT_SYNCHRONIZED_OP(sc);
12641 
12642 	if (id < 0 || id > ULD_MAX)
12643 		return (EINVAL);
12644 
12645 	sx_slock(&t4_uld_list_lock);
12646 	if (t4_uld_list[id] == NULL)
12647 		rc = ENXIO;
12648 	else {
12649 		rc = t4_uld_list[id]->uld_deactivate(sc);
12650 		if (rc == 0)
12651 			clrbit(&sc->active_ulds, id);
12652 	}
12653 	sx_sunlock(&t4_uld_list_lock);
12654 
12655 	return (rc);
12656 }
12657 
12658 static int
deactivate_all_uld(struct adapter * sc)12659 deactivate_all_uld(struct adapter *sc)
12660 {
12661 	int i, rc;
12662 
12663 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
12664 	if (rc != 0)
12665 		return (ENXIO);
12666 	sx_slock(&t4_uld_list_lock);
12667 	for (i = 0; i <= ULD_MAX; i++) {
12668 		if (t4_uld_list[i] == NULL || !uld_active(sc, i))
12669 			continue;
12670 		rc = t4_uld_list[i]->uld_deactivate(sc);
12671 		if (rc != 0)
12672 			break;
12673 		clrbit(&sc->active_ulds, i);
12674 	}
12675 	sx_sunlock(&t4_uld_list_lock);
12676 	end_synchronized_op(sc, 0);
12677 
12678 	return (rc);
12679 }
12680 
12681 static void
stop_all_uld(struct adapter * sc)12682 stop_all_uld(struct adapter *sc)
12683 {
12684 	int i;
12685 
12686 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
12687 		return;
12688 	sx_slock(&t4_uld_list_lock);
12689 	for (i = 0; i <= ULD_MAX; i++) {
12690 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12691 		    t4_uld_list[i]->uld_stop == NULL)
12692 			continue;
12693 		(void) t4_uld_list[i]->uld_stop(sc);
12694 	}
12695 	sx_sunlock(&t4_uld_list_lock);
12696 	end_synchronized_op(sc, 0);
12697 }
12698 
12699 static void
restart_all_uld(struct adapter * sc)12700 restart_all_uld(struct adapter *sc)
12701 {
12702 	int i;
12703 
12704 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
12705 		return;
12706 	sx_slock(&t4_uld_list_lock);
12707 	for (i = 0; i <= ULD_MAX; i++) {
12708 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12709 		    t4_uld_list[i]->uld_restart == NULL)
12710 			continue;
12711 		(void) t4_uld_list[i]->uld_restart(sc);
12712 	}
12713 	sx_sunlock(&t4_uld_list_lock);
12714 	end_synchronized_op(sc, 0);
12715 }
12716 
12717 int
uld_active(struct adapter * sc,int id)12718 uld_active(struct adapter *sc, int id)
12719 {
12720 
12721 	MPASS(id >= 0 && id <= ULD_MAX);
12722 
12723 	return (isset(&sc->active_ulds, id));
12724 }
12725 #endif
12726 
12727 #ifdef KERN_TLS
12728 static int
ktls_capability(struct adapter * sc,bool enable)12729 ktls_capability(struct adapter *sc, bool enable)
12730 {
12731 	ASSERT_SYNCHRONIZED_OP(sc);
12732 
12733 	if (!is_ktls(sc))
12734 		return (ENODEV);
12735 	if (!is_t6(sc))
12736 		return (0);
12737 	if (!hw_all_ok(sc))
12738 		return (ENXIO);
12739 
12740 	if (enable) {
12741 		if (sc->flags & KERN_TLS_ON)
12742 			return (0);	/* already on */
12743 		if (sc->offload_map != 0) {
12744 			CH_WARN(sc,
12745 			    "Disable TOE on all interfaces associated with "
12746 			    "this adapter before trying to enable NIC TLS.\n");
12747 			return (EAGAIN);
12748 		}
12749 		return (t6_config_kern_tls(sc, true));
12750 	} else {
12751 		/*
12752 		 * Nothing to do for disable.  If TOE is enabled sometime later
12753 		 * then toe_capability will reconfigure the hardware.
12754 		 */
12755 		return (0);
12756 	}
12757 }
12758 #endif
12759 
12760 /*
12761  * t  = ptr to tunable.
12762  * nc = number of CPUs.
12763  * c  = compiled in default for that tunable.
12764  */
12765 static void
calculate_nqueues(int * t,int nc,const int c)12766 calculate_nqueues(int *t, int nc, const int c)
12767 {
12768 	int nq;
12769 
12770 	if (*t > 0)
12771 		return;
12772 	nq = *t < 0 ? -*t : c;
12773 	*t = min(nc, nq);
12774 }
12775 
12776 /*
12777  * Come up with reasonable defaults for some of the tunables, provided they're
12778  * not set by the user (in which case we'll use the values as is).
12779  */
12780 static void
tweak_tunables(void)12781 tweak_tunables(void)
12782 {
12783 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
12784 
12785 	if (t4_ntxq < 1) {
12786 #ifdef RSS
12787 		t4_ntxq = rss_getnumbuckets();
12788 #else
12789 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
12790 #endif
12791 	}
12792 
12793 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
12794 
12795 	if (t4_nrxq < 1) {
12796 #ifdef RSS
12797 		t4_nrxq = rss_getnumbuckets();
12798 #else
12799 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
12800 #endif
12801 	}
12802 
12803 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
12804 
12805 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12806 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
12807 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
12808 #endif
12809 #ifdef TCP_OFFLOAD
12810 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
12811 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
12812 #endif
12813 
12814 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
12815 	if (t4_toecaps_allowed == -1)
12816 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
12817 #else
12818 	if (t4_toecaps_allowed == -1)
12819 		t4_toecaps_allowed = 0;
12820 #endif
12821 
12822 #ifdef TCP_OFFLOAD
12823 	if (t4_rdmacaps_allowed == -1) {
12824 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
12825 		    FW_CAPS_CONFIG_RDMA_RDMAC;
12826 	}
12827 
12828 	if (t4_iscsicaps_allowed == -1) {
12829 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
12830 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
12831 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
12832 	}
12833 
12834 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
12835 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
12836 
12837 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
12838 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
12839 #else
12840 	if (t4_rdmacaps_allowed == -1)
12841 		t4_rdmacaps_allowed = 0;
12842 
12843 	if (t4_iscsicaps_allowed == -1)
12844 		t4_iscsicaps_allowed = 0;
12845 #endif
12846 
12847 #ifdef DEV_NETMAP
12848 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
12849 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
12850 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
12851 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
12852 #endif
12853 
12854 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
12855 		t4_tmr_idx = TMR_IDX;
12856 
12857 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
12858 		t4_pktc_idx = PKTC_IDX;
12859 
12860 	if (t4_qsize_txq < 128)
12861 		t4_qsize_txq = 128;
12862 
12863 	if (t4_qsize_rxq < 128)
12864 		t4_qsize_rxq = 128;
12865 	while (t4_qsize_rxq & 7)
12866 		t4_qsize_rxq++;
12867 
12868 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
12869 
12870 	/*
12871 	 * Number of VIs to create per-port.  The first VI is the "main" regular
12872 	 * VI for the port.  The rest are additional virtual interfaces on the
12873 	 * same physical port.  Note that the main VI does not have native
12874 	 * netmap support but the extra VIs do.
12875 	 *
12876 	 * Limit the number of VIs per port to the number of available
12877 	 * MAC addresses per port.
12878 	 */
12879 	if (t4_num_vis < 1)
12880 		t4_num_vis = 1;
12881 	if (t4_num_vis > nitems(vi_mac_funcs)) {
12882 		t4_num_vis = nitems(vi_mac_funcs);
12883 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
12884 	}
12885 
12886 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
12887 		pcie_relaxed_ordering = 1;
12888 #if defined(__i386__) || defined(__amd64__)
12889 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
12890 			pcie_relaxed_ordering = 0;
12891 #endif
12892 	}
12893 }
12894 
12895 #ifdef DDB
12896 static void
t4_dump_mem(struct adapter * sc,u_int addr,u_int len)12897 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
12898 {
12899 	uint32_t base, j, off, pf, reg, save, win_pos;
12900 
12901 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
12902 	save = t4_read_reg(sc, reg);
12903 	base = sc->memwin[2].mw_base;
12904 
12905 	if (is_t4(sc)) {
12906 		pf = 0;
12907 		win_pos = addr & ~0xf;	/* start must be 16B aligned */
12908 	} else {
12909 		pf = V_PFNUM(sc->pf);
12910 		win_pos = addr & ~0x7f;	/* start must be 128B aligned */
12911 	}
12912 	off = addr - win_pos;
12913 	t4_write_reg(sc, reg, win_pos | pf);
12914 	t4_read_reg(sc, reg);
12915 
12916 	while (len > 0 && !db_pager_quit) {
12917 		uint32_t buf[8];
12918 		for (j = 0; j < 8; j++, off += 4)
12919 			buf[j] = htonl(t4_read_reg(sc, base + off));
12920 
12921 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
12922 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12923 		    buf[7]);
12924 		if (len <= sizeof(buf))
12925 			len = 0;
12926 		else
12927 			len -= sizeof(buf);
12928 	}
12929 
12930 	t4_write_reg(sc, reg, save);
12931 	t4_read_reg(sc, reg);
12932 }
12933 
12934 static void
t4_dump_tcb(struct adapter * sc,int tid)12935 t4_dump_tcb(struct adapter *sc, int tid)
12936 {
12937 	uint32_t tcb_addr;
12938 
12939 	/* Dump TCB for the tid */
12940 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
12941 	tcb_addr += tid * TCB_SIZE;
12942 	t4_dump_mem(sc, tcb_addr, TCB_SIZE);
12943 }
12944 
12945 static void
t4_dump_devlog(struct adapter * sc)12946 t4_dump_devlog(struct adapter *sc)
12947 {
12948 	struct devlog_params *dparams = &sc->params.devlog;
12949 	struct fw_devlog_e e;
12950 	int i, first, j, m, nentries, rc;
12951 	uint64_t ftstamp = UINT64_MAX;
12952 
12953 	if (dparams->start == 0) {
12954 		db_printf("devlog params not valid\n");
12955 		return;
12956 	}
12957 
12958 	nentries = dparams->size / sizeof(struct fw_devlog_e);
12959 	m = fwmtype_to_hwmtype(dparams->memtype);
12960 
12961 	/* Find the first entry. */
12962 	first = -1;
12963 	for (i = 0; i < nentries && !db_pager_quit; i++) {
12964 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12965 		    sizeof(e), (void *)&e);
12966 		if (rc != 0)
12967 			break;
12968 
12969 		if (e.timestamp == 0)
12970 			break;
12971 
12972 		e.timestamp = be64toh(e.timestamp);
12973 		if (e.timestamp < ftstamp) {
12974 			ftstamp = e.timestamp;
12975 			first = i;
12976 		}
12977 	}
12978 
12979 	if (first == -1)
12980 		return;
12981 
12982 	i = first;
12983 	do {
12984 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12985 		    sizeof(e), (void *)&e);
12986 		if (rc != 0)
12987 			return;
12988 
12989 		if (e.timestamp == 0)
12990 			return;
12991 
12992 		e.timestamp = be64toh(e.timestamp);
12993 		e.seqno = be32toh(e.seqno);
12994 		for (j = 0; j < 8; j++)
12995 			e.params[j] = be32toh(e.params[j]);
12996 
12997 		db_printf("%10d  %15ju  %8s  %8s  ",
12998 		    e.seqno, e.timestamp,
12999 		    (e.level < nitems(devlog_level_strings) ?
13000 			devlog_level_strings[e.level] : "UNKNOWN"),
13001 		    (e.facility < nitems(devlog_facility_strings) ?
13002 			devlog_facility_strings[e.facility] : "UNKNOWN"));
13003 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
13004 		    e.params[3], e.params[4], e.params[5], e.params[6],
13005 		    e.params[7]);
13006 
13007 		if (++i == nentries)
13008 			i = 0;
13009 	} while (i != first && !db_pager_quit);
13010 }
13011 
13012 static DB_DEFINE_TABLE(show, t4, show_t4);
13013 
DB_TABLE_COMMAND_FLAGS(show_t4,devlog,db_show_devlog,CS_OWN)13014 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
13015 {
13016 	device_t dev;
13017 	int t;
13018 	bool valid;
13019 
13020 	valid = false;
13021 	t = db_read_token();
13022 	if (t == tIDENT) {
13023 		dev = device_lookup_by_name(db_tok_string);
13024 		valid = true;
13025 	}
13026 	db_skip_to_eol();
13027 	if (!valid) {
13028 		db_printf("usage: show t4 devlog <nexus>\n");
13029 		return;
13030 	}
13031 
13032 	if (dev == NULL) {
13033 		db_printf("device not found\n");
13034 		return;
13035 	}
13036 
13037 	t4_dump_devlog(device_get_softc(dev));
13038 }
13039 
DB_TABLE_COMMAND_FLAGS(show_t4,tcb,db_show_t4tcb,CS_OWN)13040 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
13041 {
13042 	device_t dev;
13043 	int radix, tid, t;
13044 	bool valid;
13045 
13046 	valid = false;
13047 	radix = db_radix;
13048 	db_radix = 10;
13049 	t = db_read_token();
13050 	if (t == tIDENT) {
13051 		dev = device_lookup_by_name(db_tok_string);
13052 		t = db_read_token();
13053 		if (t == tNUMBER) {
13054 			tid = db_tok_number;
13055 			valid = true;
13056 		}
13057 	}
13058 	db_radix = radix;
13059 	db_skip_to_eol();
13060 	if (!valid) {
13061 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
13062 		return;
13063 	}
13064 
13065 	if (dev == NULL) {
13066 		db_printf("device not found\n");
13067 		return;
13068 	}
13069 	if (tid < 0) {
13070 		db_printf("invalid tid\n");
13071 		return;
13072 	}
13073 
13074 	t4_dump_tcb(device_get_softc(dev), tid);
13075 }
13076 
DB_TABLE_COMMAND_FLAGS(show_t4,memdump,db_show_memdump,CS_OWN)13077 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
13078 {
13079 	device_t dev;
13080 	int radix, t;
13081 	bool valid;
13082 
13083 	valid = false;
13084 	radix = db_radix;
13085 	db_radix = 10;
13086 	t = db_read_token();
13087 	if (t == tIDENT) {
13088 		dev = device_lookup_by_name(db_tok_string);
13089 		t = db_read_token();
13090 		if (t == tNUMBER) {
13091 			addr = db_tok_number;
13092 			t = db_read_token();
13093 			if (t == tNUMBER) {
13094 				count = db_tok_number;
13095 				valid = true;
13096 			}
13097 		}
13098 	}
13099 	db_radix = radix;
13100 	db_skip_to_eol();
13101 	if (!valid) {
13102 		db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
13103 		return;
13104 	}
13105 
13106 	if (dev == NULL) {
13107 		db_printf("device not found\n");
13108 		return;
13109 	}
13110 	if (addr < 0) {
13111 		db_printf("invalid address\n");
13112 		return;
13113 	}
13114 	if (count <= 0) {
13115 		db_printf("invalid length\n");
13116 		return;
13117 	}
13118 
13119 	t4_dump_mem(device_get_softc(dev), addr, count);
13120 }
13121 #endif
13122 
13123 static eventhandler_tag vxlan_start_evtag;
13124 static eventhandler_tag vxlan_stop_evtag;
13125 
13126 struct vxlan_evargs {
13127 	if_t ifp;
13128 	uint16_t port;
13129 };
13130 
13131 static void
enable_vxlan_rx(struct adapter * sc)13132 enable_vxlan_rx(struct adapter *sc)
13133 {
13134 	int i, rc;
13135 	struct port_info *pi;
13136 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
13137 
13138 	ASSERT_SYNCHRONIZED_OP(sc);
13139 
13140 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
13141 	    F_VXLAN_EN);
13142 	for_each_port(sc, i) {
13143 		pi = sc->port[i];
13144 		if (pi->vxlan_tcam_entry == true)
13145 			continue;
13146 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
13147 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
13148 		    true);
13149 		if (rc < 0) {
13150 			rc = -rc;
13151 			CH_ERR(&pi->vi[0],
13152 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
13153 		} else {
13154 			MPASS(rc == sc->rawf_base + pi->port_id);
13155 			pi->vxlan_tcam_entry = true;
13156 		}
13157 	}
13158 }
13159 
13160 static void
t4_vxlan_start(struct adapter * sc,void * arg)13161 t4_vxlan_start(struct adapter *sc, void *arg)
13162 {
13163 	struct vxlan_evargs *v = arg;
13164 
13165 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13166 		return;
13167 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
13168 		return;
13169 
13170 	if (sc->vxlan_refcount == 0) {
13171 		sc->vxlan_port = v->port;
13172 		sc->vxlan_refcount = 1;
13173 		if (!hw_off_limits(sc))
13174 			enable_vxlan_rx(sc);
13175 	} else if (sc->vxlan_port == v->port) {
13176 		sc->vxlan_refcount++;
13177 	} else {
13178 		CH_ERR(sc, "VXLAN already configured on port  %d; "
13179 		    "ignoring attempt to configure it on port %d\n",
13180 		    sc->vxlan_port, v->port);
13181 	}
13182 	end_synchronized_op(sc, 0);
13183 }
13184 
13185 static void
t4_vxlan_stop(struct adapter * sc,void * arg)13186 t4_vxlan_stop(struct adapter *sc, void *arg)
13187 {
13188 	struct vxlan_evargs *v = arg;
13189 
13190 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13191 		return;
13192 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
13193 		return;
13194 
13195 	/*
13196 	 * VXLANs may have been configured before the driver was loaded so we
13197 	 * may see more stops than starts.  This is not handled cleanly but at
13198 	 * least we keep the refcount sane.
13199 	 */
13200 	if (sc->vxlan_port != v->port)
13201 		goto done;
13202 	if (sc->vxlan_refcount == 0) {
13203 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
13204 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
13205 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
13206 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
13207 done:
13208 	end_synchronized_op(sc, 0);
13209 }
13210 
13211 static void
t4_vxlan_start_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13212 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
13213     sa_family_t family, u_int port)
13214 {
13215 	struct vxlan_evargs v;
13216 
13217 	MPASS(family == AF_INET || family == AF_INET6);
13218 	v.ifp = ifp;
13219 	v.port = port;
13220 
13221 	t4_iterate(t4_vxlan_start, &v);
13222 }
13223 
13224 static void
t4_vxlan_stop_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13225 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
13226     u_int port)
13227 {
13228 	struct vxlan_evargs v;
13229 
13230 	MPASS(family == AF_INET || family == AF_INET6);
13231 	v.ifp = ifp;
13232 	v.port = port;
13233 
13234 	t4_iterate(t4_vxlan_stop, &v);
13235 }
13236 
13237 
13238 static struct sx mlu;	/* mod load unload */
13239 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
13240 
13241 static int
mod_event(module_t mod,int cmd,void * arg)13242 mod_event(module_t mod, int cmd, void *arg)
13243 {
13244 	int rc = 0;
13245 	static int loaded = 0;
13246 
13247 	switch (cmd) {
13248 	case MOD_LOAD:
13249 		sx_xlock(&mlu);
13250 		if (loaded++ == 0) {
13251 			t4_sge_modload();
13252 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13253 			    t4_filter_rpl, CPL_COOKIE_FILTER);
13254 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
13255 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
13256 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
13257 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
13258 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13259 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
13260 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
13261 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
13262 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
13263 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
13264 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
13265 			    do_smt_write_rpl);
13266 			sx_init(&t4_list_lock, "T4/T5 adapters");
13267 			SLIST_INIT(&t4_list);
13268 			callout_init(&fatal_callout, 1);
13269 #ifdef TCP_OFFLOAD
13270 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
13271 #endif
13272 #ifdef INET6
13273 			t4_clip_modload();
13274 #endif
13275 #ifdef KERN_TLS
13276 			t6_ktls_modload();
13277 #endif
13278 			t4_tracer_modload();
13279 			tweak_tunables();
13280 			vxlan_start_evtag =
13281 			    EVENTHANDLER_REGISTER(vxlan_start,
13282 				t4_vxlan_start_handler, NULL,
13283 				EVENTHANDLER_PRI_ANY);
13284 			vxlan_stop_evtag =
13285 			    EVENTHANDLER_REGISTER(vxlan_stop,
13286 				t4_vxlan_stop_handler, NULL,
13287 				EVENTHANDLER_PRI_ANY);
13288 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
13289 			    taskqueue_thread_enqueue, &reset_tq);
13290 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
13291 			    "t4_rst_thr");
13292 		}
13293 		sx_xunlock(&mlu);
13294 		break;
13295 
13296 	case MOD_UNLOAD:
13297 		sx_xlock(&mlu);
13298 		if (--loaded == 0) {
13299 #ifdef TCP_OFFLOAD
13300 			int i;
13301 #endif
13302 			int tries;
13303 
13304 			taskqueue_free(reset_tq);
13305 
13306 			tries = 0;
13307 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
13308 				uprintf("%ju clusters with custom free routine "
13309 				    "still is use.\n", t4_sge_extfree_refs());
13310 				pause("t4unload", 2 * hz);
13311 			}
13312 
13313 			sx_slock(&t4_list_lock);
13314 			if (!SLIST_EMPTY(&t4_list)) {
13315 				rc = EBUSY;
13316 				sx_sunlock(&t4_list_lock);
13317 				goto done_unload;
13318 			}
13319 #ifdef TCP_OFFLOAD
13320 			sx_slock(&t4_uld_list_lock);
13321 			for (i = 0; i <= ULD_MAX; i++) {
13322 				if (t4_uld_list[i] != NULL) {
13323 					rc = EBUSY;
13324 					sx_sunlock(&t4_uld_list_lock);
13325 					sx_sunlock(&t4_list_lock);
13326 					goto done_unload;
13327 				}
13328 			}
13329 			sx_sunlock(&t4_uld_list_lock);
13330 #endif
13331 			sx_sunlock(&t4_list_lock);
13332 
13333 			if (t4_sge_extfree_refs() == 0) {
13334 				EVENTHANDLER_DEREGISTER(vxlan_start,
13335 				    vxlan_start_evtag);
13336 				EVENTHANDLER_DEREGISTER(vxlan_stop,
13337 				    vxlan_stop_evtag);
13338 				t4_tracer_modunload();
13339 #ifdef KERN_TLS
13340 				t6_ktls_modunload();
13341 #endif
13342 #ifdef INET6
13343 				t4_clip_modunload();
13344 #endif
13345 #ifdef TCP_OFFLOAD
13346 				sx_destroy(&t4_uld_list_lock);
13347 #endif
13348 				sx_destroy(&t4_list_lock);
13349 				t4_sge_modunload();
13350 				loaded = 0;
13351 			} else {
13352 				rc = EBUSY;
13353 				loaded++;	/* undo earlier decrement */
13354 			}
13355 		}
13356 done_unload:
13357 		sx_xunlock(&mlu);
13358 		break;
13359 	}
13360 
13361 	return (rc);
13362 }
13363 
13364 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
13365 MODULE_VERSION(t4nex, 1);
13366 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
13367 #ifdef DEV_NETMAP
13368 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
13369 #endif /* DEV_NETMAP */
13370 
13371 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
13372 MODULE_VERSION(t5nex, 1);
13373 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
13374 #ifdef DEV_NETMAP
13375 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
13376 #endif /* DEV_NETMAP */
13377 
13378 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
13379 MODULE_VERSION(t6nex, 1);
13380 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
13381 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
13382 #ifdef DEV_NETMAP
13383 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
13384 #endif /* DEV_NETMAP */
13385 
13386 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
13387 MODULE_VERSION(cxgbe, 1);
13388 
13389 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
13390 MODULE_VERSION(cxl, 1);
13391 
13392 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
13393 MODULE_VERSION(cc, 1);
13394 
13395 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
13396 MODULE_VERSION(vcxgbe, 1);
13397 
13398 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
13399 MODULE_VERSION(vcxl, 1);
13400 
13401 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
13402 MODULE_VERSION(vcc, 1);
13403