xref: /freebsd/sys/dev/cxgbe/t4_main.c (revision 90652188232458891a2922fb5a542971aa58055d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_ddb.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_kern_tls.h"
35 #include "opt_ratelimit.h"
36 #include "opt_rss.h"
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/priv.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/eventhandler.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/taskqueue.h>
48 #include <sys/pciio.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pci_private.h>
52 #include <sys/firmware.h>
53 #include <sys/sbuf.h>
54 #include <sys/smp.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <net/if_dl.h>
62 #include <net/if_vlan_var.h>
63 #ifdef RSS
64 #include <net/rss_config.h>
65 #endif
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 #ifdef KERN_TLS
69 #include <netinet/tcp_seq.h>
70 #endif
71 #if defined(__i386__) || defined(__amd64__)
72 #include <machine/md_var.h>
73 #include <machine/cputypes.h>
74 #include <vm/vm.h>
75 #include <vm/pmap.h>
76 #endif
77 #ifdef DDB
78 #include <ddb/ddb.h>
79 #include <ddb/db_lex.h>
80 #endif
81 
82 #include "common/common.h"
83 #include "common/t4_msg.h"
84 #include "common/t4_regs.h"
85 #include "common/t4_regs_values.h"
86 #include "cudbg/cudbg.h"
87 #include "t4_clip.h"
88 #include "t4_ioctl.h"
89 #include "t4_l2t.h"
90 #include "t4_mp_ring.h"
91 #include "t4_if.h"
92 #include "t4_smt.h"
93 
94 /* T4 bus driver interface */
95 static int t4_probe(device_t);
96 static int t4_attach(device_t);
97 static int t4_detach(device_t);
98 static int t4_child_location(device_t, device_t, struct sbuf *);
99 static int t4_ready(device_t);
100 static int t4_read_port_device(device_t, int, device_t *);
101 static int t4_suspend(device_t);
102 static int t4_resume(device_t);
103 static int t4_reset_prepare(device_t, device_t);
104 static int t4_reset_post(device_t, device_t);
105 static device_method_t t4_methods[] = {
106 	DEVMETHOD(device_probe,		t4_probe),
107 	DEVMETHOD(device_attach,	t4_attach),
108 	DEVMETHOD(device_detach,	t4_detach),
109 	DEVMETHOD(device_suspend,	t4_suspend),
110 	DEVMETHOD(device_resume,	t4_resume),
111 
112 	DEVMETHOD(bus_child_location,	t4_child_location),
113 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
114 	DEVMETHOD(bus_reset_post,	t4_reset_post),
115 
116 	DEVMETHOD(t4_is_main_ready,	t4_ready),
117 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
118 
119 	DEVMETHOD_END
120 };
121 static driver_t t4_driver = {
122 	"t4nex",
123 	t4_methods,
124 	sizeof(struct adapter)
125 };
126 
127 
128 /* T4 port (cxgbe) interface */
129 static int cxgbe_probe(device_t);
130 static int cxgbe_attach(device_t);
131 static int cxgbe_detach(device_t);
132 device_method_t cxgbe_methods[] = {
133 	DEVMETHOD(device_probe,		cxgbe_probe),
134 	DEVMETHOD(device_attach,	cxgbe_attach),
135 	DEVMETHOD(device_detach,	cxgbe_detach),
136 	{ 0, 0 }
137 };
138 static driver_t cxgbe_driver = {
139 	"cxgbe",
140 	cxgbe_methods,
141 	sizeof(struct port_info)
142 };
143 
144 /* T4 VI (vcxgbe) interface */
145 static int vcxgbe_probe(device_t);
146 static int vcxgbe_attach(device_t);
147 static int vcxgbe_detach(device_t);
148 static device_method_t vcxgbe_methods[] = {
149 	DEVMETHOD(device_probe,		vcxgbe_probe),
150 	DEVMETHOD(device_attach,	vcxgbe_attach),
151 	DEVMETHOD(device_detach,	vcxgbe_detach),
152 	{ 0, 0 }
153 };
154 static driver_t vcxgbe_driver = {
155 	"vcxgbe",
156 	vcxgbe_methods,
157 	sizeof(struct vi_info)
158 };
159 
160 static d_ioctl_t t4_ioctl;
161 
162 static struct cdevsw t4_cdevsw = {
163        .d_version = D_VERSION,
164        .d_ioctl = t4_ioctl,
165        .d_name = "t4nex",
166 };
167 
168 /* T5 bus driver interface */
169 static int t5_probe(device_t);
170 static device_method_t t5_methods[] = {
171 	DEVMETHOD(device_probe,		t5_probe),
172 	DEVMETHOD(device_attach,	t4_attach),
173 	DEVMETHOD(device_detach,	t4_detach),
174 	DEVMETHOD(device_suspend,	t4_suspend),
175 	DEVMETHOD(device_resume,	t4_resume),
176 
177 	DEVMETHOD(bus_child_location,	t4_child_location),
178 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
179 	DEVMETHOD(bus_reset_post,	t4_reset_post),
180 
181 	DEVMETHOD(t4_is_main_ready,	t4_ready),
182 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
183 
184 	DEVMETHOD_END
185 };
186 static driver_t t5_driver = {
187 	"t5nex",
188 	t5_methods,
189 	sizeof(struct adapter)
190 };
191 
192 
193 /* T5 port (cxl) interface */
194 static driver_t cxl_driver = {
195 	"cxl",
196 	cxgbe_methods,
197 	sizeof(struct port_info)
198 };
199 
200 /* T5 VI (vcxl) interface */
201 static driver_t vcxl_driver = {
202 	"vcxl",
203 	vcxgbe_methods,
204 	sizeof(struct vi_info)
205 };
206 
207 /* T6 bus driver interface */
208 static int t6_probe(device_t);
209 static device_method_t t6_methods[] = {
210 	DEVMETHOD(device_probe,		t6_probe),
211 	DEVMETHOD(device_attach,	t4_attach),
212 	DEVMETHOD(device_detach,	t4_detach),
213 	DEVMETHOD(device_suspend,	t4_suspend),
214 	DEVMETHOD(device_resume,	t4_resume),
215 
216 	DEVMETHOD(bus_child_location,	t4_child_location),
217 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
218 	DEVMETHOD(bus_reset_post,	t4_reset_post),
219 
220 	DEVMETHOD(t4_is_main_ready,	t4_ready),
221 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
222 
223 	DEVMETHOD_END
224 };
225 static driver_t t6_driver = {
226 	"t6nex",
227 	t6_methods,
228 	sizeof(struct adapter)
229 };
230 
231 
232 /* T6 port (cc) interface */
233 static driver_t cc_driver = {
234 	"cc",
235 	cxgbe_methods,
236 	sizeof(struct port_info)
237 };
238 
239 /* T6 VI (vcc) interface */
240 static driver_t vcc_driver = {
241 	"vcc",
242 	vcxgbe_methods,
243 	sizeof(struct vi_info)
244 };
245 
246 /* ifnet interface */
247 static void cxgbe_init(void *);
248 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
249 static int cxgbe_transmit(if_t, struct mbuf *);
250 static void cxgbe_qflush(if_t);
251 #if defined(KERN_TLS) || defined(RATELIMIT)
252 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
253     struct m_snd_tag **);
254 #endif
255 
256 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
257 
258 /*
259  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
260  * then ADAPTER_LOCK, then t4_uld_list_lock.
261  */
262 static struct sx t4_list_lock;
263 SLIST_HEAD(, adapter) t4_list;
264 #ifdef TCP_OFFLOAD
265 static struct sx t4_uld_list_lock;
266 struct uld_info *t4_uld_list[ULD_MAX + 1];
267 #endif
268 
269 /*
270  * Tunables.  See tweak_tunables() too.
271  *
272  * Each tunable is set to a default value here if it's known at compile-time.
273  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
274  * provide a reasonable default (upto n) when the driver is loaded.
275  *
276  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
277  * T5 are under hw.cxl.
278  */
279 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
280     "cxgbe(4) parameters");
281 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
282     "cxgbe(4) T5+ parameters");
283 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
284     "cxgbe(4) TOE parameters");
285 
286 /*
287  * Number of queues for tx and rx, NIC and offload.
288  */
289 #define NTXQ 16
290 int t4_ntxq = -NTXQ;
291 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
292     "Number of TX queues per port");
293 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
294 
295 #define NRXQ 8
296 int t4_nrxq = -NRXQ;
297 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
298     "Number of RX queues per port");
299 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
300 
301 #define NTXQ_VI 1
302 static int t4_ntxq_vi = -NTXQ_VI;
303 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
304     "Number of TX queues per VI");
305 
306 #define NRXQ_VI 1
307 static int t4_nrxq_vi = -NRXQ_VI;
308 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
309     "Number of RX queues per VI");
310 
311 static int t4_rsrv_noflowq = 0;
312 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
313     0, "Reserve TX queue 0 of each VI for non-flowid packets");
314 
315 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
316 #define NOFLDTXQ 8
317 static int t4_nofldtxq = -NOFLDTXQ;
318 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
319     "Number of offload TX queues per port");
320 
321 #define NOFLDTXQ_VI 1
322 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
323 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
324     "Number of offload TX queues per VI");
325 #endif
326 
327 #if defined(TCP_OFFLOAD)
328 #define NOFLDRXQ 2
329 static int t4_nofldrxq = -NOFLDRXQ;
330 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
331     "Number of offload RX queues per port");
332 
333 #define NOFLDRXQ_VI 1
334 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
335 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
336     "Number of offload RX queues per VI");
337 
338 #define TMR_IDX_OFLD 1
339 static int t4_tmr_idx_ofld = TMR_IDX_OFLD;
340 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
341     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
342 
343 #define PKTC_IDX_OFLD (-1)
344 static int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
345 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
346     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
347 
348 /* 0 means chip/fw default, non-zero number is value in microseconds */
349 static u_long t4_toe_keepalive_idle = 0;
350 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
351     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
352 
353 /* 0 means chip/fw default, non-zero number is value in microseconds */
354 static u_long t4_toe_keepalive_interval = 0;
355 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
356     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
357 
358 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
359 static int t4_toe_keepalive_count = 0;
360 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
361     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
362 
363 /* 0 means chip/fw default, non-zero number is value in microseconds */
364 static u_long t4_toe_rexmt_min = 0;
365 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
366     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
367 
368 /* 0 means chip/fw default, non-zero number is value in microseconds */
369 static u_long t4_toe_rexmt_max = 0;
370 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
371     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
372 
373 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
374 static int t4_toe_rexmt_count = 0;
375 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
376     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
377 
378 /* -1 means chip/fw default, other values are raw backoff values to use */
379 static int t4_toe_rexmt_backoff[16] = {
380 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
381 };
382 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
383     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
384     "cxgbe(4) TOE retransmit backoff values");
385 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
386     &t4_toe_rexmt_backoff[0], 0, "");
387 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
388     &t4_toe_rexmt_backoff[1], 0, "");
389 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
390     &t4_toe_rexmt_backoff[2], 0, "");
391 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
392     &t4_toe_rexmt_backoff[3], 0, "");
393 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
394     &t4_toe_rexmt_backoff[4], 0, "");
395 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
396     &t4_toe_rexmt_backoff[5], 0, "");
397 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
398     &t4_toe_rexmt_backoff[6], 0, "");
399 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
400     &t4_toe_rexmt_backoff[7], 0, "");
401 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
402     &t4_toe_rexmt_backoff[8], 0, "");
403 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
404     &t4_toe_rexmt_backoff[9], 0, "");
405 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
406     &t4_toe_rexmt_backoff[10], 0, "");
407 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
408     &t4_toe_rexmt_backoff[11], 0, "");
409 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
410     &t4_toe_rexmt_backoff[12], 0, "");
411 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
412     &t4_toe_rexmt_backoff[13], 0, "");
413 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
414     &t4_toe_rexmt_backoff[14], 0, "");
415 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
416     &t4_toe_rexmt_backoff[15], 0, "");
417 
418 int t4_ddp_rcvbuf_len = 256 * 1024;
419 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_len, CTLFLAG_RWTUN,
420     &t4_ddp_rcvbuf_len, 0, "length of each DDP RX buffer");
421 
422 unsigned int t4_ddp_rcvbuf_cache = 4;
423 SYSCTL_UINT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_cache, CTLFLAG_RWTUN,
424     &t4_ddp_rcvbuf_cache, 0,
425     "maximum number of free DDP RX buffers to cache per connection");
426 #endif
427 
428 #ifdef DEV_NETMAP
429 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
430 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
431 static int t4_native_netmap = NN_EXTRA_VI;
432 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
433     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
434 
435 #define NNMTXQ 8
436 static int t4_nnmtxq = -NNMTXQ;
437 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
438     "Number of netmap TX queues");
439 
440 #define NNMRXQ 8
441 static int t4_nnmrxq = -NNMRXQ;
442 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
443     "Number of netmap RX queues");
444 
445 #define NNMTXQ_VI 2
446 static int t4_nnmtxq_vi = -NNMTXQ_VI;
447 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
448     "Number of netmap TX queues per VI");
449 
450 #define NNMRXQ_VI 2
451 static int t4_nnmrxq_vi = -NNMRXQ_VI;
452 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
453     "Number of netmap RX queues per VI");
454 #endif
455 
456 /*
457  * Holdoff parameters for ports.
458  */
459 #define TMR_IDX 1
460 int t4_tmr_idx = TMR_IDX;
461 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
462     0, "Holdoff timer index");
463 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
464 
465 #define PKTC_IDX (-1)
466 int t4_pktc_idx = PKTC_IDX;
467 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
468     0, "Holdoff packet counter index");
469 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
470 
471 /*
472  * Size (# of entries) of each tx and rx queue.
473  */
474 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
475 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
476     "Number of descriptors in each TX queue");
477 
478 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
479 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
480     "Number of descriptors in each RX queue");
481 
482 /*
483  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
484  */
485 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
486 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
487     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
488 
489 /*
490  * Configuration file.  All the _CF names here are special.
491  */
492 #define DEFAULT_CF	"default"
493 #define BUILTIN_CF	"built-in"
494 #define FLASH_CF	"flash"
495 #define UWIRE_CF	"uwire"
496 #define FPGA_CF		"fpga"
497 static char t4_cfg_file[32] = DEFAULT_CF;
498 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
499     sizeof(t4_cfg_file), "Firmware configuration file");
500 
501 /*
502  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
503  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
504  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
505  *            mark or when signalled to do so, 0 to never emit PAUSE.
506  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
507  *                 negotiated settings will override rx_pause/tx_pause.
508  *                 Otherwise rx_pause/tx_pause are applied forcibly.
509  */
510 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
511 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
512     &t4_pause_settings, 0,
513     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
514 
515 /*
516  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
517  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
518  *  0 to disable FEC.
519  */
520 static int t4_fec = -1;
521 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
522     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
523 
524 /*
525  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
526  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
527  * driver runs as if this is set to 0.
528  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
529  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
530  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
531  *    the firmware anyway (may result in l1cfg errors with old firmwares).
532  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
533  *    means set all FEC bits that are valid for the speed.
534  */
535 static int t4_force_fec = -1;
536 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
537     "Controls the use of FORCE_FEC bit in L1 configuration.");
538 
539 /*
540  * Link autonegotiation.
541  * -1 to run with the firmware default.
542  *  0 to disable.
543  *  1 to enable.
544  */
545 static int t4_autoneg = -1;
546 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
547     "Link autonegotiation");
548 
549 /*
550  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
551  * encouraged respectively).  '-n' is the same as 'n' except the firmware
552  * version used in the checks is read from the firmware bundled with the driver.
553  */
554 static int t4_fw_install = 1;
555 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
556     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
557 
558 /*
559  * ASIC features that will be used.  Disable the ones you don't want so that the
560  * chip resources aren't wasted on features that will not be used.
561  */
562 static int t4_nbmcaps_allowed = 0;
563 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
564     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
565 
566 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
567 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
568     &t4_linkcaps_allowed, 0, "Default link capabilities");
569 
570 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
571     FW_CAPS_CONFIG_SWITCH_EGRESS;
572 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
573     &t4_switchcaps_allowed, 0, "Default switch capabilities");
574 
575 #ifdef RATELIMIT
576 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
577 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
578 #else
579 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
580 	FW_CAPS_CONFIG_NIC_HASHFILTER;
581 #endif
582 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
583     &t4_niccaps_allowed, 0, "Default NIC capabilities");
584 
585 static int t4_toecaps_allowed = -1;
586 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
587     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
588 
589 static int t4_rdmacaps_allowed = -1;
590 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
591     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
592 
593 static int t4_cryptocaps_allowed = -1;
594 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
595     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
596 
597 static int t4_iscsicaps_allowed = -1;
598 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
599     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
600 
601 static int t4_fcoecaps_allowed = 0;
602 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
603     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
604 
605 static int t5_write_combine = 0;
606 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
607     0, "Use WC instead of UC for BAR2");
608 
609 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
610 static int t4_doorbells_allowed = 0xf;
611 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
612 	   &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
613 
614 static int t4_num_vis = 1;
615 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
616     "Number of VIs per port");
617 
618 /*
619  * PCIe Relaxed Ordering.
620  * -1: driver should figure out a good value.
621  * 0: disable RO.
622  * 1: enable RO.
623  * 2: leave RO alone.
624  */
625 static int pcie_relaxed_ordering = -1;
626 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
627     &pcie_relaxed_ordering, 0,
628     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
629 
630 static int t4_panic_on_fatal_err = 0;
631 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
632     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
633 
634 static int t4_reset_on_fatal_err = 0;
635 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
636     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
637 
638 static int t4_clock_gate_on_suspend = 0;
639 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
640     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
641 
642 static int t4_tx_vm_wr = 0;
643 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
644     "Use VM work requests to transmit packets.");
645 
646 /*
647  * Set to non-zero to enable the attack filter.  A packet that matches any of
648  * these conditions will get dropped on ingress:
649  * 1) IP && source address == destination address.
650  * 2) TCP/IP && source address is not a unicast address.
651  * 3) TCP/IP && destination address is not a unicast address.
652  * 4) IP && source address is loopback (127.x.y.z).
653  * 5) IP && destination address is loopback (127.x.y.z).
654  * 6) IPv6 && source address == destination address.
655  * 7) IPv6 && source address is not a unicast address.
656  * 8) IPv6 && source address is loopback (::1/128).
657  * 9) IPv6 && destination address is loopback (::1/128).
658  * 10) IPv6 && source address is unspecified (::/128).
659  * 11) IPv6 && destination address is unspecified (::/128).
660  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
661  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
662  */
663 static int t4_attack_filter = 0;
664 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
665     &t4_attack_filter, 0, "Drop suspicious traffic");
666 
667 static int t4_drop_ip_fragments = 0;
668 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
669     &t4_drop_ip_fragments, 0, "Drop IP fragments");
670 
671 static int t4_drop_pkts_with_l2_errors = 1;
672 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
673     &t4_drop_pkts_with_l2_errors, 0,
674     "Drop all frames with Layer 2 length or checksum errors");
675 
676 static int t4_drop_pkts_with_l3_errors = 0;
677 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
678     &t4_drop_pkts_with_l3_errors, 0,
679     "Drop all frames with IP version, length, or checksum errors");
680 
681 static int t4_drop_pkts_with_l4_errors = 0;
682 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
683     &t4_drop_pkts_with_l4_errors, 0,
684     "Drop all frames with Layer 4 length, checksum, or other errors");
685 
686 #ifdef TCP_OFFLOAD
687 /*
688  * TOE tunables.
689  */
690 static int t4_cop_managed_offloading = 0;
691 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
692     &t4_cop_managed_offloading, 0,
693     "COP (Connection Offload Policy) controls all TOE offload");
694 TUNABLE_INT("hw.cxgbe.cop_managed_offloading", &t4_cop_managed_offloading);
695 #endif
696 
697 #ifdef KERN_TLS
698 /*
699  * This enables KERN_TLS for all adapters if set.
700  */
701 static int t4_kern_tls = 0;
702 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
703     "Enable KERN_TLS mode for T6 adapters");
704 
705 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
706     "cxgbe(4) KERN_TLS parameters");
707 
708 static int t4_tls_inline_keys = 0;
709 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
710     &t4_tls_inline_keys, 0,
711     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
712     "in card memory.");
713 
714 static int t4_tls_combo_wrs = 0;
715 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
716     0, "Attempt to combine TCB field updates with TLS record work requests.");
717 #endif
718 
719 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
720 static int vi_mac_funcs[] = {
721 	FW_VI_FUNC_ETH,
722 	FW_VI_FUNC_OFLD,
723 	FW_VI_FUNC_IWARP,
724 	FW_VI_FUNC_OPENISCSI,
725 	FW_VI_FUNC_OPENFCOE,
726 	FW_VI_FUNC_FOISCSI,
727 	FW_VI_FUNC_FOFCOE,
728 };
729 
730 struct intrs_and_queues {
731 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
732 	uint16_t num_vis;	/* number of VIs for each port */
733 	uint16_t nirq;		/* Total # of vectors */
734 	uint16_t ntxq;		/* # of NIC txq's for each port */
735 	uint16_t nrxq;		/* # of NIC rxq's for each port */
736 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
737 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
738 	uint16_t nnmtxq;	/* # of netmap txq's */
739 	uint16_t nnmrxq;	/* # of netmap rxq's */
740 
741 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
742 	uint16_t ntxq_vi;	/* # of NIC txq's */
743 	uint16_t nrxq_vi;	/* # of NIC rxq's */
744 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
745 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
746 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
747 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
748 };
749 
750 static void setup_memwin(struct adapter *);
751 static void position_memwin(struct adapter *, int, uint32_t);
752 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
753 static int fwmtype_to_hwmtype(int);
754 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
755     uint32_t *);
756 static int fixup_devlog_params(struct adapter *);
757 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
758 static int contact_firmware(struct adapter *);
759 static int partition_resources(struct adapter *);
760 static int get_params__pre_init(struct adapter *);
761 static int set_params__pre_init(struct adapter *);
762 static int get_params__post_init(struct adapter *);
763 static int set_params__post_init(struct adapter *);
764 static void t4_set_desc(struct adapter *);
765 static bool fixed_ifmedia(struct port_info *);
766 static void build_medialist(struct port_info *);
767 static void init_link_config(struct port_info *);
768 static int fixup_link_config(struct port_info *);
769 static int apply_link_config(struct port_info *);
770 static int cxgbe_init_synchronized(struct vi_info *);
771 static int cxgbe_uninit_synchronized(struct vi_info *);
772 static int adapter_full_init(struct adapter *);
773 static void adapter_full_uninit(struct adapter *);
774 static int vi_full_init(struct vi_info *);
775 static void vi_full_uninit(struct vi_info *);
776 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
777 static void quiesce_txq(struct sge_txq *);
778 static void quiesce_wrq(struct sge_wrq *);
779 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
780 static void quiesce_vi(struct vi_info *);
781 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
782     driver_intr_t *, void *, char *);
783 static int t4_free_irq(struct adapter *, struct irq *);
784 static void t4_init_atid_table(struct adapter *);
785 static void t4_free_atid_table(struct adapter *);
786 static void stop_atid_allocator(struct adapter *);
787 static void restart_atid_allocator(struct adapter *);
788 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
789 static void vi_refresh_stats(struct vi_info *);
790 static void cxgbe_refresh_stats(struct vi_info *);
791 static void cxgbe_tick(void *);
792 static void vi_tick(void *);
793 static void cxgbe_sysctls(struct port_info *);
794 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
795 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
796 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
797 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
798 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
799 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
800 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
801 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
802 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
803 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
804 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
805 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
806 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
807 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
808 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
809 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
810 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
811 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
812 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
813 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
814 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
815 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
816 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
817 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
818 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
819 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
820 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
821 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
822 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
823 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
824 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
825 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
826 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
827 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
828 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
829 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
830 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
831 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
832 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
833 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
834 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
835 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
836 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
837 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
838 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
839 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
840 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
841 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
842 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
843 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
844 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
845 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
846 #ifdef TCP_OFFLOAD
847 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
848 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
849 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
850 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
851 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
852 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
853 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
854 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
855 #endif
856 static int get_sge_context(struct adapter *, struct t4_sge_context *);
857 static int load_fw(struct adapter *, struct t4_data *);
858 static int load_cfg(struct adapter *, struct t4_data *);
859 static int load_boot(struct adapter *, struct t4_bootrom *);
860 static int load_bootcfg(struct adapter *, struct t4_data *);
861 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
862 static void free_offload_policy(struct t4_offload_policy *);
863 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
864 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
865 static int read_i2c(struct adapter *, struct t4_i2c_data *);
866 static int clear_stats(struct adapter *, u_int);
867 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
868 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
869 static inline int stop_adapter(struct adapter *);
870 static inline void set_adapter_hwstatus(struct adapter *, const bool);
871 static int stop_lld(struct adapter *);
872 static inline int restart_adapter(struct adapter *);
873 static int restart_lld(struct adapter *);
874 #ifdef TCP_OFFLOAD
875 static int deactivate_all_uld(struct adapter *);
876 static void stop_all_uld(struct adapter *);
877 static void restart_all_uld(struct adapter *);
878 #endif
879 #ifdef KERN_TLS
880 static int ktls_capability(struct adapter *, bool);
881 #endif
882 static int mod_event(module_t, int, void *);
883 static int notify_siblings(device_t, int);
884 static uint64_t vi_get_counter(if_t, ift_counter);
885 static uint64_t cxgbe_get_counter(if_t, ift_counter);
886 static void enable_vxlan_rx(struct adapter *);
887 static void reset_adapter_task(void *, int);
888 static void fatal_error_task(void *, int);
889 static void dump_devlog(struct adapter *);
890 static void dump_cim_regs(struct adapter *);
891 static void dump_cimla(struct adapter *);
892 
893 struct {
894 	uint16_t device;
895 	char *desc;
896 } t4_pciids[] = {
897 	{0xa000, "Chelsio Terminator 4 FPGA"},
898 	{0x4400, "Chelsio T440-dbg"},
899 	{0x4401, "Chelsio T420-CR"},
900 	{0x4402, "Chelsio T422-CR"},
901 	{0x4403, "Chelsio T440-CR"},
902 	{0x4404, "Chelsio T420-BCH"},
903 	{0x4405, "Chelsio T440-BCH"},
904 	{0x4406, "Chelsio T440-CH"},
905 	{0x4407, "Chelsio T420-SO"},
906 	{0x4408, "Chelsio T420-CX"},
907 	{0x4409, "Chelsio T420-BT"},
908 	{0x440a, "Chelsio T404-BT"},
909 	{0x440e, "Chelsio T440-LP-CR"},
910 }, t5_pciids[] = {
911 	{0xb000, "Chelsio Terminator 5 FPGA"},
912 	{0x5400, "Chelsio T580-dbg"},
913 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
914 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
915 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
916 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
917 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
918 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
919 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
920 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
921 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
922 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
923 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
924 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
925 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
926 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
927 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
928 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
929 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
930 
931 	/* Custom */
932 	{0x5483, "Custom T540-CR"},
933 	{0x5484, "Custom T540-BT"},
934 }, t6_pciids[] = {
935 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
936 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
937 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
938 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
939 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
940 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
941 	{0x6405, "Chelsio T6225-SO-OCP3"},	/* 2 x 10/25G, nomem */
942 	{0x6406, "Chelsio T6225-OCP3"},		/* 2 x 10/25G */
943 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
944 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
945 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
946 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
947 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
948 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
949 	{0x6414, "Chelsio T62100-SO-OCP3"},	/* 2 x 40/50/100G, nomem */
950 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
951 
952 	/* Custom */
953 	{0x6480, "Custom T6225-CR"},
954 	{0x6481, "Custom T62100-CR"},
955 	{0x6482, "Custom T6225-CR"},
956 	{0x6483, "Custom T62100-CR"},
957 	{0x6484, "Custom T64100-CR"},
958 	{0x6485, "Custom T6240-SO"},
959 	{0x6486, "Custom T6225-SO-CR"},
960 	{0x6487, "Custom T6225-CR"},
961 };
962 
963 #ifdef TCP_OFFLOAD
964 /*
965  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
966  * be exactly the same for both rxq and ofld_rxq.
967  */
968 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
969 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
970 #endif
971 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
972 
973 static int
t4_probe(device_t dev)974 t4_probe(device_t dev)
975 {
976 	int i;
977 	uint16_t v = pci_get_vendor(dev);
978 	uint16_t d = pci_get_device(dev);
979 	uint8_t f = pci_get_function(dev);
980 
981 	if (v != PCI_VENDOR_ID_CHELSIO)
982 		return (ENXIO);
983 
984 	/* Attach only to PF0 of the FPGA */
985 	if (d == 0xa000 && f != 0)
986 		return (ENXIO);
987 
988 	for (i = 0; i < nitems(t4_pciids); i++) {
989 		if (d == t4_pciids[i].device) {
990 			device_set_desc(dev, t4_pciids[i].desc);
991 			return (BUS_PROBE_DEFAULT);
992 		}
993 	}
994 
995 	return (ENXIO);
996 }
997 
998 static int
t5_probe(device_t dev)999 t5_probe(device_t dev)
1000 {
1001 	int i;
1002 	uint16_t v = pci_get_vendor(dev);
1003 	uint16_t d = pci_get_device(dev);
1004 	uint8_t f = pci_get_function(dev);
1005 
1006 	if (v != PCI_VENDOR_ID_CHELSIO)
1007 		return (ENXIO);
1008 
1009 	/* Attach only to PF0 of the FPGA */
1010 	if (d == 0xb000 && f != 0)
1011 		return (ENXIO);
1012 
1013 	for (i = 0; i < nitems(t5_pciids); i++) {
1014 		if (d == t5_pciids[i].device) {
1015 			device_set_desc(dev, t5_pciids[i].desc);
1016 			return (BUS_PROBE_DEFAULT);
1017 		}
1018 	}
1019 
1020 	return (ENXIO);
1021 }
1022 
1023 static int
t6_probe(device_t dev)1024 t6_probe(device_t dev)
1025 {
1026 	int i;
1027 	uint16_t v = pci_get_vendor(dev);
1028 	uint16_t d = pci_get_device(dev);
1029 
1030 	if (v != PCI_VENDOR_ID_CHELSIO)
1031 		return (ENXIO);
1032 
1033 	for (i = 0; i < nitems(t6_pciids); i++) {
1034 		if (d == t6_pciids[i].device) {
1035 			device_set_desc(dev, t6_pciids[i].desc);
1036 			return (BUS_PROBE_DEFAULT);
1037 		}
1038 	}
1039 
1040 	return (ENXIO);
1041 }
1042 
1043 static void
t5_attribute_workaround(device_t dev)1044 t5_attribute_workaround(device_t dev)
1045 {
1046 	device_t root_port;
1047 	uint32_t v;
1048 
1049 	/*
1050 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1051 	 * Ordering attributes when replying to a TLP from a Root
1052 	 * Port.  As a workaround, find the parent Root Port and
1053 	 * disable No Snoop and Relaxed Ordering.  Note that this
1054 	 * affects all devices under this root port.
1055 	 */
1056 	root_port = pci_find_pcie_root_port(dev);
1057 	if (root_port == NULL) {
1058 		device_printf(dev, "Unable to find parent root port\n");
1059 		return;
1060 	}
1061 
1062 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1063 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1064 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1065 	    0)
1066 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1067 		    device_get_nameunit(root_port));
1068 }
1069 
1070 static const struct devnames devnames[] = {
1071 	{
1072 		.nexus_name = "t4nex",
1073 		.ifnet_name = "cxgbe",
1074 		.vi_ifnet_name = "vcxgbe",
1075 		.pf03_drv_name = "t4iov",
1076 		.vf_nexus_name = "t4vf",
1077 		.vf_ifnet_name = "cxgbev"
1078 	}, {
1079 		.nexus_name = "t5nex",
1080 		.ifnet_name = "cxl",
1081 		.vi_ifnet_name = "vcxl",
1082 		.pf03_drv_name = "t5iov",
1083 		.vf_nexus_name = "t5vf",
1084 		.vf_ifnet_name = "cxlv"
1085 	}, {
1086 		.nexus_name = "t6nex",
1087 		.ifnet_name = "cc",
1088 		.vi_ifnet_name = "vcc",
1089 		.pf03_drv_name = "t6iov",
1090 		.vf_nexus_name = "t6vf",
1091 		.vf_ifnet_name = "ccv"
1092 	}
1093 };
1094 
1095 void
t4_init_devnames(struct adapter * sc)1096 t4_init_devnames(struct adapter *sc)
1097 {
1098 	int id;
1099 
1100 	id = chip_id(sc);
1101 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
1102 		sc->names = &devnames[id - CHELSIO_T4];
1103 	else {
1104 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1105 		sc->names = NULL;
1106 	}
1107 }
1108 
1109 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1110 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1111 {
1112 	const char *parent, *name;
1113 	long value;
1114 	int line, unit;
1115 
1116 	line = 0;
1117 	parent = device_get_nameunit(sc->dev);
1118 	name = sc->names->ifnet_name;
1119 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1120 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1121 		    value == pi->port_id)
1122 			return (unit);
1123 	}
1124 	return (-1);
1125 }
1126 
1127 static void
t4_calibration(void * arg)1128 t4_calibration(void *arg)
1129 {
1130 	struct adapter *sc;
1131 	struct clock_sync *cur, *nex;
1132 	uint64_t hw;
1133 	sbintime_t sbt;
1134 	int next_up;
1135 
1136 	sc = (struct adapter *)arg;
1137 
1138 	KASSERT((hw_off_limits(sc) == 0), ("hw_off_limits at t4_calibration"));
1139 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1140 	sbt = sbinuptime();
1141 
1142 	cur = &sc->cal_info[sc->cal_current];
1143 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1144 	nex = &sc->cal_info[next_up];
1145 	if (__predict_false(sc->cal_count == 0)) {
1146 		/* First time in, just get the values in */
1147 		cur->hw_cur = hw;
1148 		cur->sbt_cur = sbt;
1149 		sc->cal_count++;
1150 		goto done;
1151 	}
1152 
1153 	if (cur->hw_cur == hw) {
1154 		/* The clock is not advancing? */
1155 		sc->cal_count = 0;
1156 		atomic_store_rel_int(&cur->gen, 0);
1157 		goto done;
1158 	}
1159 
1160 	seqc_write_begin(&nex->gen);
1161 	nex->hw_prev = cur->hw_cur;
1162 	nex->sbt_prev = cur->sbt_cur;
1163 	nex->hw_cur = hw;
1164 	nex->sbt_cur = sbt;
1165 	seqc_write_end(&nex->gen);
1166 	sc->cal_current = next_up;
1167 done:
1168 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1169 	    sc, C_DIRECT_EXEC);
1170 }
1171 
1172 static void
t4_calibration_start(struct adapter * sc)1173 t4_calibration_start(struct adapter *sc)
1174 {
1175 	/*
1176 	 * Here if we have not done a calibration
1177 	 * then do so otherwise start the appropriate
1178 	 * timer.
1179 	 */
1180 	int i;
1181 
1182 	for (i = 0; i < CNT_CAL_INFO; i++) {
1183 		sc->cal_info[i].gen = 0;
1184 	}
1185 	sc->cal_current = 0;
1186 	sc->cal_count = 0;
1187 	sc->cal_gen = 0;
1188 	t4_calibration(sc);
1189 }
1190 
1191 static int
t4_attach(device_t dev)1192 t4_attach(device_t dev)
1193 {
1194 	struct adapter *sc;
1195 	int rc = 0, i, j, rqidx, tqidx, nports;
1196 	struct make_dev_args mda;
1197 	struct intrs_and_queues iaq;
1198 	struct sge *s;
1199 	uint32_t *buf;
1200 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1201 	int ofld_tqidx;
1202 #endif
1203 #ifdef TCP_OFFLOAD
1204 	int ofld_rqidx;
1205 #endif
1206 #ifdef DEV_NETMAP
1207 	int nm_rqidx, nm_tqidx;
1208 #endif
1209 	int num_vis;
1210 
1211 	sc = device_get_softc(dev);
1212 	sc->dev = dev;
1213 	sysctl_ctx_init(&sc->ctx);
1214 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1215 
1216 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1217 		t5_attribute_workaround(dev);
1218 	pci_enable_busmaster(dev);
1219 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1220 		uint32_t v;
1221 
1222 		pci_set_max_read_req(dev, 4096);
1223 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1224 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1225 		if (pcie_relaxed_ordering == 0 &&
1226 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1227 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1228 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1229 		} else if (pcie_relaxed_ordering == 1 &&
1230 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1231 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1232 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1233 		}
1234 	}
1235 
1236 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1237 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1238 	sc->traceq = -1;
1239 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1240 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1241 	    device_get_nameunit(dev));
1242 
1243 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1244 	    device_get_nameunit(dev));
1245 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1246 	t4_add_adapter(sc);
1247 
1248 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1249 	TAILQ_INIT(&sc->sfl);
1250 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1251 
1252 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1253 
1254 	sc->policy = NULL;
1255 	rw_init(&sc->policy_lock, "connection offload policy");
1256 
1257 	callout_init(&sc->ktls_tick, 1);
1258 
1259 	callout_init(&sc->cal_callout, 1);
1260 
1261 	refcount_init(&sc->vxlan_refcount, 0);
1262 
1263 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1264 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1265 
1266 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1267 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1268 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1269 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1270 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1271 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1272 
1273 	rc = t4_map_bars_0_and_4(sc);
1274 	if (rc != 0)
1275 		goto done; /* error message displayed already */
1276 
1277 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1278 
1279 	/* Prepare the adapter for operation. */
1280 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1281 	rc = -t4_prep_adapter(sc, buf);
1282 	free(buf, M_CXGBE);
1283 	if (rc != 0) {
1284 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1285 		goto done;
1286 	}
1287 
1288 	/*
1289 	 * This is the real PF# to which we're attaching.  Works from within PCI
1290 	 * passthrough environments too, where pci_get_function() could return a
1291 	 * different PF# depending on the passthrough configuration.  We need to
1292 	 * use the real PF# in all our communication with the firmware.
1293 	 */
1294 	j = t4_read_reg(sc, A_PL_WHOAMI);
1295 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1296 	sc->mbox = sc->pf;
1297 
1298 	t4_init_devnames(sc);
1299 	if (sc->names == NULL) {
1300 		rc = ENOTSUP;
1301 		goto done; /* error message displayed already */
1302 	}
1303 
1304 	/*
1305 	 * Do this really early, with the memory windows set up even before the
1306 	 * character device.  The userland tool's register i/o and mem read
1307 	 * will work even in "recovery mode".
1308 	 */
1309 	setup_memwin(sc);
1310 	if (t4_init_devlog_params(sc, 0) == 0)
1311 		fixup_devlog_params(sc);
1312 	make_dev_args_init(&mda);
1313 	mda.mda_devsw = &t4_cdevsw;
1314 	mda.mda_uid = UID_ROOT;
1315 	mda.mda_gid = GID_WHEEL;
1316 	mda.mda_mode = 0600;
1317 	mda.mda_si_drv1 = sc;
1318 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1319 	if (rc != 0)
1320 		device_printf(dev, "failed to create nexus char device: %d.\n",
1321 		    rc);
1322 
1323 	/* Go no further if recovery mode has been requested. */
1324 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1325 		device_printf(dev, "recovery mode.\n");
1326 		goto done;
1327 	}
1328 
1329 #if defined(__i386__)
1330 	if ((cpu_feature & CPUID_CX8) == 0) {
1331 		device_printf(dev, "64 bit atomics not available.\n");
1332 		rc = ENOTSUP;
1333 		goto done;
1334 	}
1335 #endif
1336 
1337 	/* Contact the firmware and try to become the master driver. */
1338 	rc = contact_firmware(sc);
1339 	if (rc != 0)
1340 		goto done; /* error message displayed already */
1341 	MPASS(sc->flags & FW_OK);
1342 
1343 	rc = get_params__pre_init(sc);
1344 	if (rc != 0)
1345 		goto done; /* error message displayed already */
1346 
1347 	if (sc->flags & MASTER_PF) {
1348 		rc = partition_resources(sc);
1349 		if (rc != 0)
1350 			goto done; /* error message displayed already */
1351 	}
1352 
1353 	rc = get_params__post_init(sc);
1354 	if (rc != 0)
1355 		goto done; /* error message displayed already */
1356 
1357 	rc = set_params__post_init(sc);
1358 	if (rc != 0)
1359 		goto done; /* error message displayed already */
1360 
1361 	rc = t4_map_bar_2(sc);
1362 	if (rc != 0)
1363 		goto done; /* error message displayed already */
1364 
1365 	rc = t4_adj_doorbells(sc);
1366 	if (rc != 0)
1367 		goto done; /* error message displayed already */
1368 
1369 	rc = t4_create_dma_tag(sc);
1370 	if (rc != 0)
1371 		goto done; /* error message displayed already */
1372 
1373 	/*
1374 	 * First pass over all the ports - allocate VIs and initialize some
1375 	 * basic parameters like mac address, port type, etc.
1376 	 */
1377 	for_each_port(sc, i) {
1378 		struct port_info *pi;
1379 
1380 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1381 		sc->port[i] = pi;
1382 
1383 		/* These must be set before t4_port_init */
1384 		pi->adapter = sc;
1385 		pi->port_id = i;
1386 		/*
1387 		 * XXX: vi[0] is special so we can't delay this allocation until
1388 		 * pi->nvi's final value is known.
1389 		 */
1390 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1391 		    M_ZERO | M_WAITOK);
1392 
1393 		/*
1394 		 * Allocate the "main" VI and initialize parameters
1395 		 * like mac addr.
1396 		 */
1397 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1398 		if (rc != 0) {
1399 			device_printf(dev, "unable to initialize port %d: %d\n",
1400 			    i, rc);
1401 			free(pi->vi, M_CXGBE);
1402 			free(pi, M_CXGBE);
1403 			sc->port[i] = NULL;
1404 			goto done;
1405 		}
1406 
1407 		if (is_bt(pi->port_type))
1408 			setbit(&sc->bt_map, pi->tx_chan);
1409 		else
1410 			MPASS(!isset(&sc->bt_map, pi->tx_chan));
1411 
1412 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1413 		    device_get_nameunit(dev), i);
1414 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1415 		sc->chan_map[pi->tx_chan] = i;
1416 
1417 		/*
1418 		 * The MPS counter for FCS errors doesn't work correctly on the
1419 		 * T6 so we use the MAC counter here.  Which MAC is in use
1420 		 * depends on the link settings which will be known when the
1421 		 * link comes up.
1422 		 */
1423 		if (is_t6(sc))
1424 			pi->fcs_reg = -1;
1425 		else {
1426 			pi->fcs_reg = t4_port_reg(sc, pi->tx_chan,
1427 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1428 		}
1429 		pi->fcs_base = 0;
1430 
1431 		/* All VIs on this port share this media. */
1432 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1433 		    cxgbe_media_status);
1434 
1435 		PORT_LOCK(pi);
1436 		init_link_config(pi);
1437 		fixup_link_config(pi);
1438 		build_medialist(pi);
1439 		if (fixed_ifmedia(pi))
1440 			pi->flags |= FIXED_IFMEDIA;
1441 		PORT_UNLOCK(pi);
1442 
1443 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1444 		    t4_ifnet_unit(sc, pi));
1445 		if (pi->dev == NULL) {
1446 			device_printf(dev,
1447 			    "failed to add device for port %d.\n", i);
1448 			rc = ENXIO;
1449 			goto done;
1450 		}
1451 		pi->vi[0].dev = pi->dev;
1452 		device_set_softc(pi->dev, pi);
1453 	}
1454 
1455 	/*
1456 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1457 	 */
1458 	nports = sc->params.nports;
1459 	rc = cfg_itype_and_nqueues(sc, &iaq);
1460 	if (rc != 0)
1461 		goto done; /* error message displayed already */
1462 
1463 	num_vis = iaq.num_vis;
1464 	sc->intr_type = iaq.intr_type;
1465 	sc->intr_count = iaq.nirq;
1466 
1467 	s = &sc->sge;
1468 	s->nrxq = nports * iaq.nrxq;
1469 	s->ntxq = nports * iaq.ntxq;
1470 	if (num_vis > 1) {
1471 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1472 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1473 	}
1474 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1475 	s->neq += nports;		/* ctrl queues: 1 per port */
1476 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1477 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1478 	if (is_offload(sc) || is_ethoffload(sc)) {
1479 		s->nofldtxq = nports * iaq.nofldtxq;
1480 		if (num_vis > 1)
1481 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1482 		s->neq += s->nofldtxq;
1483 
1484 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1485 		    M_CXGBE, M_ZERO | M_WAITOK);
1486 	}
1487 #endif
1488 #ifdef TCP_OFFLOAD
1489 	if (is_offload(sc)) {
1490 		s->nofldrxq = nports * iaq.nofldrxq;
1491 		if (num_vis > 1)
1492 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1493 		s->neq += s->nofldrxq;	/* free list */
1494 		s->niq += s->nofldrxq;
1495 
1496 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1497 		    M_CXGBE, M_ZERO | M_WAITOK);
1498 	}
1499 #endif
1500 #ifdef DEV_NETMAP
1501 	s->nnmrxq = 0;
1502 	s->nnmtxq = 0;
1503 	if (t4_native_netmap & NN_MAIN_VI) {
1504 		s->nnmrxq += nports * iaq.nnmrxq;
1505 		s->nnmtxq += nports * iaq.nnmtxq;
1506 	}
1507 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1508 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1509 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1510 	}
1511 	s->neq += s->nnmtxq + s->nnmrxq;
1512 	s->niq += s->nnmrxq;
1513 
1514 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1515 	    M_CXGBE, M_ZERO | M_WAITOK);
1516 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1517 	    M_CXGBE, M_ZERO | M_WAITOK);
1518 #endif
1519 	MPASS(s->niq <= s->iqmap_sz);
1520 	MPASS(s->neq <= s->eqmap_sz);
1521 
1522 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1523 	    M_ZERO | M_WAITOK);
1524 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1525 	    M_ZERO | M_WAITOK);
1526 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1527 	    M_ZERO | M_WAITOK);
1528 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1529 	    M_ZERO | M_WAITOK);
1530 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1531 	    M_ZERO | M_WAITOK);
1532 
1533 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1534 	    M_ZERO | M_WAITOK);
1535 
1536 	t4_init_l2t(sc, M_WAITOK);
1537 	t4_init_smt(sc, M_WAITOK);
1538 	t4_init_tx_sched(sc);
1539 	t4_init_atid_table(sc);
1540 #ifdef RATELIMIT
1541 	t4_init_etid_table(sc);
1542 #endif
1543 #ifdef INET6
1544 	t4_init_clip_table(sc);
1545 #endif
1546 	if (sc->vres.key.size != 0)
1547 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1548 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1549 
1550 	/*
1551 	 * Second pass over the ports.  This time we know the number of rx and
1552 	 * tx queues that each port should get.
1553 	 */
1554 	rqidx = tqidx = 0;
1555 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1556 	ofld_tqidx = 0;
1557 #endif
1558 #ifdef TCP_OFFLOAD
1559 	ofld_rqidx = 0;
1560 #endif
1561 #ifdef DEV_NETMAP
1562 	nm_rqidx = nm_tqidx = 0;
1563 #endif
1564 	for_each_port(sc, i) {
1565 		struct port_info *pi = sc->port[i];
1566 		struct vi_info *vi;
1567 
1568 		if (pi == NULL)
1569 			continue;
1570 
1571 		pi->nvi = num_vis;
1572 		for_each_vi(pi, j, vi) {
1573 			vi->pi = pi;
1574 			vi->adapter = sc;
1575 			vi->first_intr = -1;
1576 			vi->qsize_rxq = t4_qsize_rxq;
1577 			vi->qsize_txq = t4_qsize_txq;
1578 
1579 			vi->first_rxq = rqidx;
1580 			vi->first_txq = tqidx;
1581 			vi->tmr_idx = t4_tmr_idx;
1582 			vi->pktc_idx = t4_pktc_idx;
1583 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1584 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1585 
1586 			rqidx += vi->nrxq;
1587 			tqidx += vi->ntxq;
1588 
1589 			if (j == 0 && vi->ntxq > 1)
1590 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1591 			else
1592 				vi->rsrv_noflowq = 0;
1593 
1594 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1595 			vi->first_ofld_txq = ofld_tqidx;
1596 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1597 			ofld_tqidx += vi->nofldtxq;
1598 #endif
1599 #ifdef TCP_OFFLOAD
1600 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1601 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1602 			vi->first_ofld_rxq = ofld_rqidx;
1603 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1604 
1605 			ofld_rqidx += vi->nofldrxq;
1606 #endif
1607 #ifdef DEV_NETMAP
1608 			vi->first_nm_rxq = nm_rqidx;
1609 			vi->first_nm_txq = nm_tqidx;
1610 			if (j == 0) {
1611 				vi->nnmrxq = iaq.nnmrxq;
1612 				vi->nnmtxq = iaq.nnmtxq;
1613 			} else {
1614 				vi->nnmrxq = iaq.nnmrxq_vi;
1615 				vi->nnmtxq = iaq.nnmtxq_vi;
1616 			}
1617 			nm_rqidx += vi->nnmrxq;
1618 			nm_tqidx += vi->nnmtxq;
1619 #endif
1620 		}
1621 	}
1622 
1623 	rc = t4_setup_intr_handlers(sc);
1624 	if (rc != 0) {
1625 		device_printf(dev,
1626 		    "failed to setup interrupt handlers: %d\n", rc);
1627 		goto done;
1628 	}
1629 
1630 	bus_identify_children(dev);
1631 
1632 	/*
1633 	 * Ensure thread-safe mailbox access (in debug builds).
1634 	 *
1635 	 * So far this was the only thread accessing the mailbox but various
1636 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1637 	 * will access the mailbox from different threads.
1638 	 */
1639 	sc->flags |= CHK_MBOX_ACCESS;
1640 
1641 	bus_attach_children(dev);
1642 	t4_calibration_start(sc);
1643 
1644 	device_printf(dev,
1645 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1646 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1647 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1648 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1649 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1650 
1651 	t4_set_desc(sc);
1652 
1653 	notify_siblings(dev, 0);
1654 
1655 done:
1656 	if (rc != 0 && sc->cdev) {
1657 		/* cdev was created and so cxgbetool works; recover that way. */
1658 		device_printf(dev,
1659 		    "error during attach, adapter is now in recovery mode.\n");
1660 		rc = 0;
1661 	}
1662 
1663 	if (rc != 0)
1664 		t4_detach_common(dev);
1665 	else
1666 		t4_sysctls(sc);
1667 
1668 	return (rc);
1669 }
1670 
1671 static int
t4_child_location(device_t bus,device_t dev,struct sbuf * sb)1672 t4_child_location(device_t bus, device_t dev, struct sbuf *sb)
1673 {
1674 	struct adapter *sc;
1675 	struct port_info *pi;
1676 	int i;
1677 
1678 	sc = device_get_softc(bus);
1679 	for_each_port(sc, i) {
1680 		pi = sc->port[i];
1681 		if (pi != NULL && pi->dev == dev) {
1682 			sbuf_printf(sb, "port=%d", pi->port_id);
1683 			break;
1684 		}
1685 	}
1686 	return (0);
1687 }
1688 
1689 static int
t4_ready(device_t dev)1690 t4_ready(device_t dev)
1691 {
1692 	struct adapter *sc;
1693 
1694 	sc = device_get_softc(dev);
1695 	if (sc->flags & FW_OK)
1696 		return (0);
1697 	return (ENXIO);
1698 }
1699 
1700 static int
t4_read_port_device(device_t dev,int port,device_t * child)1701 t4_read_port_device(device_t dev, int port, device_t *child)
1702 {
1703 	struct adapter *sc;
1704 	struct port_info *pi;
1705 
1706 	sc = device_get_softc(dev);
1707 	if (port < 0 || port >= MAX_NPORTS)
1708 		return (EINVAL);
1709 	pi = sc->port[port];
1710 	if (pi == NULL || pi->dev == NULL)
1711 		return (ENXIO);
1712 	*child = pi->dev;
1713 	return (0);
1714 }
1715 
1716 static int
notify_siblings(device_t dev,int detaching)1717 notify_siblings(device_t dev, int detaching)
1718 {
1719 	device_t sibling;
1720 	int error, i;
1721 
1722 	error = 0;
1723 	for (i = 0; i < PCI_FUNCMAX; i++) {
1724 		if (i == pci_get_function(dev))
1725 			continue;
1726 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1727 		    pci_get_slot(dev), i);
1728 		if (sibling == NULL || !device_is_attached(sibling))
1729 			continue;
1730 		if (detaching)
1731 			error = T4_DETACH_CHILD(sibling);
1732 		else
1733 			(void)T4_ATTACH_CHILD(sibling);
1734 		if (error)
1735 			break;
1736 	}
1737 	return (error);
1738 }
1739 
1740 /*
1741  * Idempotent
1742  */
1743 static int
t4_detach(device_t dev)1744 t4_detach(device_t dev)
1745 {
1746 	int rc;
1747 
1748 	rc = notify_siblings(dev, 1);
1749 	if (rc) {
1750 		device_printf(dev,
1751 		    "failed to detach sibling devices: %d\n", rc);
1752 		return (rc);
1753 	}
1754 
1755 	return (t4_detach_common(dev));
1756 }
1757 
1758 int
t4_detach_common(device_t dev)1759 t4_detach_common(device_t dev)
1760 {
1761 	struct adapter *sc;
1762 	struct port_info *pi;
1763 	int i, rc;
1764 
1765 	sc = device_get_softc(dev);
1766 
1767 #ifdef TCP_OFFLOAD
1768 	rc = deactivate_all_uld(sc);
1769 	if (rc) {
1770 		device_printf(dev,
1771 		    "failed to detach upper layer drivers: %d\n", rc);
1772 		return (rc);
1773 	}
1774 #endif
1775 
1776 	if (sc->cdev) {
1777 		destroy_dev(sc->cdev);
1778 		sc->cdev = NULL;
1779 	}
1780 
1781 	sx_xlock(&t4_list_lock);
1782 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1783 	sx_xunlock(&t4_list_lock);
1784 
1785 	sc->flags &= ~CHK_MBOX_ACCESS;
1786 	if (sc->flags & FULL_INIT_DONE) {
1787 		if (!(sc->flags & IS_VF))
1788 			t4_intr_disable(sc);
1789 	}
1790 
1791 	if (device_is_attached(dev)) {
1792 		rc = bus_generic_detach(dev);
1793 		if (rc) {
1794 			device_printf(dev,
1795 			    "failed to detach child devices: %d\n", rc);
1796 			return (rc);
1797 		}
1798 	}
1799 
1800 	for (i = 0; i < sc->intr_count; i++)
1801 		t4_free_irq(sc, &sc->irq[i]);
1802 
1803 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1804 		t4_free_tx_sched(sc);
1805 
1806 	for (i = 0; i < MAX_NPORTS; i++) {
1807 		pi = sc->port[i];
1808 		if (pi) {
1809 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1810 			if (pi->dev)
1811 				device_delete_child(dev, pi->dev);
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 		PORT_LOCK(pi);
1930 		if (pi->up_vis > 0 && pi->link_cfg.link_ok) {
1931 			/*
1932 			 * t4_shutdown_adapter has already shut down all the
1933 			 * PHYs but it also disables interrupts and DMA so there
1934 			 * won't be a link interrupt.  Update the state manually
1935 			 * if the link was up previously and inform the kernel.
1936 			 */
1937 			pi->link_cfg.link_ok = false;
1938 			t4_os_link_changed(pi);
1939 		}
1940 		PORT_UNLOCK(pi);
1941 	}
1942 
1943 	return (0);
1944 }
1945 
1946 static inline int
restart_adapter(struct adapter * sc)1947 restart_adapter(struct adapter *sc)
1948 {
1949 	uint32_t val;
1950 
1951 	if (!atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1952 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1953 			 __func__, curthread, sc->flags, sc->error_flags);
1954 		return (EALREADY);
1955 	}
1956 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1957 		 sc->flags, sc->error_flags);
1958 
1959 	MPASS(hw_off_limits(sc));
1960 	MPASS((sc->flags & FW_OK) == 0);
1961 	MPASS((sc->flags & MASTER_PF) == 0);
1962 	MPASS(sc->reset_thread == NULL);
1963 
1964 	/*
1965 	 * The adapter is supposed to be back on PCIE with its config space and
1966 	 * BARs restored to their state before reset.  Register access via
1967 	 * t4_read_reg BAR0 should just work.
1968 	 */
1969 	sc->reset_thread = curthread;
1970 	val = t4_read_reg(sc, A_PL_WHOAMI);
1971 	if (val == 0xffffffff || val == 0xeeeeeeee) {
1972 		CH_ERR(sc, "%s: device registers not readable.\n", __func__);
1973 		sc->reset_thread = NULL;
1974 		atomic_set_int(&sc->error_flags, ADAP_STOPPED);
1975 		return (ENXIO);
1976 	}
1977 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR);
1978 	atomic_add_int(&sc->incarnation, 1);
1979 	atomic_add_int(&sc->num_resets, 1);
1980 
1981 	return (0);
1982 }
1983 
1984 static inline void
set_adapter_hwstatus(struct adapter * sc,const bool usable)1985 set_adapter_hwstatus(struct adapter *sc, const bool usable)
1986 {
1987 	if (usable) {
1988 		/* Must be marked reusable by the designated thread. */
1989 		ASSERT_SYNCHRONIZED_OP(sc);
1990 		MPASS(sc->reset_thread == curthread);
1991 		mtx_lock(&sc->reg_lock);
1992 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
1993 		mtx_unlock(&sc->reg_lock);
1994 	} else {
1995 		/* Mark the adapter totally off limits. */
1996 		begin_synchronized_op(sc, NULL, SLEEP_OK, "t4hwsts");
1997 		mtx_lock(&sc->reg_lock);
1998 		atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
1999 		mtx_unlock(&sc->reg_lock);
2000 		sc->flags &= ~(FW_OK | MASTER_PF);
2001 		sc->reset_thread = NULL;
2002 		end_synchronized_op(sc, 0);
2003 	}
2004 }
2005 
2006 static int
stop_lld(struct adapter * sc)2007 stop_lld(struct adapter *sc)
2008 {
2009 	struct port_info *pi;
2010 	struct vi_info *vi;
2011 	if_t ifp;
2012 	struct sge_rxq *rxq;
2013 	struct sge_txq *txq;
2014 	struct sge_wrq *wrq;
2015 #ifdef TCP_OFFLOAD
2016 	struct sge_ofld_rxq *ofld_rxq;
2017 #endif
2018 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2019 	struct sge_ofld_txq *ofld_txq;
2020 #endif
2021 	int rc, i, j, k;
2022 
2023 	/*
2024 	 * XXX: Can there be a synch_op in progress that will hang because
2025 	 * hardware has been stopped?  We'll hang too and the solution will be
2026 	 * to use a version of begin_synch_op that wakes up existing synch_op
2027 	 * with errors.  Maybe stop_adapter should do this wakeup?
2028 	 *
2029 	 * I don't think any synch_op could get stranded waiting for DMA or
2030 	 * interrupt so I think we're okay here.  Remove this comment block
2031 	 * after testing.
2032 	 */
2033 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4slld");
2034 	if (rc != 0)
2035 		return (ENXIO);
2036 
2037 	/* Quiesce all activity. */
2038 	for_each_port(sc, i) {
2039 		pi = sc->port[i];
2040 		pi->vxlan_tcam_entry = false;
2041 		for_each_vi(pi, j, vi) {
2042 			vi->xact_addr_filt = -1;
2043 			mtx_lock(&vi->tick_mtx);
2044 			vi->flags |= VI_SKIP_STATS;
2045 			mtx_unlock(&vi->tick_mtx);
2046 			if (!(vi->flags & VI_INIT_DONE))
2047 				continue;
2048 
2049 			ifp = vi->ifp;
2050 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2051 				mtx_lock(&vi->tick_mtx);
2052 				callout_stop(&vi->tick);
2053 				mtx_unlock(&vi->tick_mtx);
2054 				callout_drain(&vi->tick);
2055 			}
2056 
2057 			/*
2058 			 * Note that the HW is not available.
2059 			 */
2060 			for_each_txq(vi, k, txq) {
2061 				TXQ_LOCK(txq);
2062 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2063 				TXQ_UNLOCK(txq);
2064 			}
2065 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2066 			for_each_ofld_txq(vi, k, ofld_txq) {
2067 				TXQ_LOCK(&ofld_txq->wrq);
2068 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2069 				TXQ_UNLOCK(&ofld_txq->wrq);
2070 			}
2071 #endif
2072 			for_each_rxq(vi, k, rxq) {
2073 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2074 			}
2075 #if defined(TCP_OFFLOAD)
2076 			for_each_ofld_rxq(vi, k, ofld_rxq) {
2077 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2078 			}
2079 #endif
2080 
2081 			quiesce_vi(vi);
2082 		}
2083 
2084 		if (sc->flags & FULL_INIT_DONE) {
2085 			/* Control queue */
2086 			wrq = &sc->sge.ctrlq[i];
2087 			TXQ_LOCK(wrq);
2088 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2089 			TXQ_UNLOCK(wrq);
2090 			quiesce_wrq(wrq);
2091 		}
2092 
2093 		if (pi->flags & HAS_TRACEQ) {
2094 			pi->flags &= ~HAS_TRACEQ;
2095 			sc->traceq = -1;
2096 			sc->tracer_valid = 0;
2097 			sc->tracer_enabled = 0;
2098 		}
2099 	}
2100 	if (sc->flags & FULL_INIT_DONE) {
2101 		/* Firmware event queue */
2102 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2103 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2104 	}
2105 
2106 	/* Stop calibration */
2107 	callout_stop(&sc->cal_callout);
2108 	callout_drain(&sc->cal_callout);
2109 
2110 	if (t4_clock_gate_on_suspend) {
2111 		t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2112 		    F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2113 		    F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2114 	}
2115 
2116 	end_synchronized_op(sc, 0);
2117 
2118 	stop_atid_allocator(sc);
2119 	t4_stop_l2t(sc);
2120 
2121 	return (rc);
2122 }
2123 
2124 int
suspend_adapter(struct adapter * sc)2125 suspend_adapter(struct adapter *sc)
2126 {
2127 	stop_adapter(sc);
2128 	stop_lld(sc);
2129 #ifdef TCP_OFFLOAD
2130 	stop_all_uld(sc);
2131 #endif
2132 	set_adapter_hwstatus(sc, false);
2133 
2134 	return (0);
2135 }
2136 
2137 static int
t4_suspend(device_t dev)2138 t4_suspend(device_t dev)
2139 {
2140 	struct adapter *sc = device_get_softc(dev);
2141 	int rc;
2142 
2143 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2144 	rc = suspend_adapter(sc);
2145 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2146 
2147 	return (rc);
2148 }
2149 
2150 struct adapter_pre_reset_state {
2151 	u_int flags;
2152 	uint16_t nbmcaps;
2153 	uint16_t linkcaps;
2154 	uint16_t switchcaps;
2155 	uint16_t niccaps;
2156 	uint16_t toecaps;
2157 	uint16_t rdmacaps;
2158 	uint16_t cryptocaps;
2159 	uint16_t iscsicaps;
2160 	uint16_t fcoecaps;
2161 
2162 	u_int cfcsum;
2163 	char cfg_file[32];
2164 
2165 	struct adapter_params params;
2166 	struct t4_virt_res vres;
2167 	struct tid_info tids;
2168 	struct sge sge;
2169 
2170 	int rawf_base;
2171 	int nrawf;
2172 
2173 };
2174 
2175 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2176 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2177 {
2178 
2179 	ASSERT_SYNCHRONIZED_OP(sc);
2180 
2181 	o->flags = sc->flags;
2182 
2183 	o->nbmcaps =  sc->nbmcaps;
2184 	o->linkcaps = sc->linkcaps;
2185 	o->switchcaps = sc->switchcaps;
2186 	o->niccaps = sc->niccaps;
2187 	o->toecaps = sc->toecaps;
2188 	o->rdmacaps = sc->rdmacaps;
2189 	o->cryptocaps = sc->cryptocaps;
2190 	o->iscsicaps = sc->iscsicaps;
2191 	o->fcoecaps = sc->fcoecaps;
2192 
2193 	o->cfcsum = sc->cfcsum;
2194 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2195 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2196 
2197 	o->params = sc->params;
2198 	o->vres = sc->vres;
2199 	o->tids = sc->tids;
2200 	o->sge = sc->sge;
2201 
2202 	o->rawf_base = sc->rawf_base;
2203 	o->nrawf = sc->nrawf;
2204 }
2205 
2206 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2207 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2208 {
2209 	int rc = 0;
2210 
2211 	ASSERT_SYNCHRONIZED_OP(sc);
2212 
2213 	/* Capabilities */
2214 #define COMPARE_CAPS(c) do { \
2215 	if (o->c##caps != sc->c##caps) { \
2216 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2217 		    sc->c##caps); \
2218 		rc = EINVAL; \
2219 	} \
2220 } while (0)
2221 	COMPARE_CAPS(nbm);
2222 	COMPARE_CAPS(link);
2223 	COMPARE_CAPS(switch);
2224 	COMPARE_CAPS(nic);
2225 	COMPARE_CAPS(toe);
2226 	COMPARE_CAPS(rdma);
2227 	COMPARE_CAPS(crypto);
2228 	COMPARE_CAPS(iscsi);
2229 	COMPARE_CAPS(fcoe);
2230 #undef COMPARE_CAPS
2231 
2232 	/* Firmware config file */
2233 	if (o->cfcsum != sc->cfcsum) {
2234 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2235 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2236 		rc = EINVAL;
2237 	}
2238 
2239 #define COMPARE_PARAM(p, name) do { \
2240 	if (o->p != sc->p) { \
2241 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2242 		rc = EINVAL; \
2243 	} \
2244 } while (0)
2245 	COMPARE_PARAM(sge.iq_start, iq_start);
2246 	COMPARE_PARAM(sge.eq_start, eq_start);
2247 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2248 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2249 	COMPARE_PARAM(tids.nftids, nftids);
2250 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2251 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2252 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2253 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2254 	COMPARE_PARAM(tids.tid_base, tid_base);
2255 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2256 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2257 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2258 	COMPARE_PARAM(rawf_base, rawf_base);
2259 	COMPARE_PARAM(nrawf, nrawf);
2260 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2261 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2262 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2263 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2264 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2265 	COMPARE_PARAM(tids.ntids, ntids);
2266 	COMPARE_PARAM(tids.etid_base, etid_base);
2267 	COMPARE_PARAM(tids.etid_end, etid_end);
2268 	COMPARE_PARAM(tids.netids, netids);
2269 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2270 	COMPARE_PARAM(params.ethoffload, ethoffload);
2271 	COMPARE_PARAM(tids.natids, natids);
2272 	COMPARE_PARAM(tids.stid_base, stid_base);
2273 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2274 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2275 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2276 	COMPARE_PARAM(vres.stag.start, stag_start);
2277 	COMPARE_PARAM(vres.stag.size, stag_size);
2278 	COMPARE_PARAM(vres.rq.start, rq_start);
2279 	COMPARE_PARAM(vres.rq.size, rq_size);
2280 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2281 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2282 	COMPARE_PARAM(vres.qp.start, qp_start);
2283 	COMPARE_PARAM(vres.qp.size, qp_size);
2284 	COMPARE_PARAM(vres.cq.start, cq_start);
2285 	COMPARE_PARAM(vres.cq.size, cq_size);
2286 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2287 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2288 	COMPARE_PARAM(vres.srq.start, srq_start);
2289 	COMPARE_PARAM(vres.srq.size, srq_size);
2290 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2291 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2292 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2293 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2294 	COMPARE_PARAM(vres.key.start, key_start);
2295 	COMPARE_PARAM(vres.key.size, key_size);
2296 #undef COMPARE_PARAM
2297 
2298 	return (rc);
2299 }
2300 
2301 static int
restart_lld(struct adapter * sc)2302 restart_lld(struct adapter *sc)
2303 {
2304 	struct adapter_pre_reset_state *old_state = NULL;
2305 	struct port_info *pi;
2306 	struct vi_info *vi;
2307 	if_t ifp;
2308 	struct sge_txq *txq;
2309 	int rc, i, j, k;
2310 
2311 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rlld");
2312 	if (rc != 0)
2313 		return (ENXIO);
2314 
2315 	/* Restore memory window. */
2316 	setup_memwin(sc);
2317 
2318 	/* Go no further if recovery mode has been requested. */
2319 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2320 		CH_ALERT(sc, "%s: recovery mode during restart.\n", __func__);
2321 		rc = 0;
2322 		set_adapter_hwstatus(sc, true);
2323 		goto done;
2324 	}
2325 
2326 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2327 	save_caps_and_params(sc, old_state);
2328 
2329 	/* Reestablish contact with firmware and become the primary PF. */
2330 	rc = contact_firmware(sc);
2331 	if (rc != 0)
2332 		goto done; /* error message displayed already */
2333 	MPASS(sc->flags & FW_OK);
2334 
2335 	if (sc->flags & MASTER_PF) {
2336 		rc = partition_resources(sc);
2337 		if (rc != 0)
2338 			goto done; /* error message displayed already */
2339 	}
2340 
2341 	rc = get_params__post_init(sc);
2342 	if (rc != 0)
2343 		goto done; /* error message displayed already */
2344 
2345 	rc = set_params__post_init(sc);
2346 	if (rc != 0)
2347 		goto done; /* error message displayed already */
2348 
2349 	rc = compare_caps_and_params(sc, old_state);
2350 	if (rc != 0)
2351 		goto done; /* error message displayed already */
2352 
2353 	for_each_port(sc, i) {
2354 		pi = sc->port[i];
2355 		MPASS(pi != NULL);
2356 		MPASS(pi->vi != NULL);
2357 		MPASS(pi->vi[0].dev == pi->dev);
2358 
2359 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2360 		if (rc != 0) {
2361 			CH_ERR(sc,
2362 			    "failed to re-initialize port %d: %d\n", i, rc);
2363 			goto done;
2364 		}
2365 		MPASS(sc->chan_map[pi->tx_chan] == i);
2366 
2367 		PORT_LOCK(pi);
2368 		fixup_link_config(pi);
2369 		build_medialist(pi);
2370 		PORT_UNLOCK(pi);
2371 		for_each_vi(pi, j, vi) {
2372 			if (IS_MAIN_VI(vi))
2373 				continue;
2374 			rc = alloc_extra_vi(sc, pi, vi);
2375 			if (rc != 0) {
2376 				CH_ERR(vi,
2377 				    "failed to re-allocate extra VI: %d\n", rc);
2378 				goto done;
2379 			}
2380 		}
2381 	}
2382 
2383 	/*
2384 	 * Interrupts and queues are about to be enabled and other threads will
2385 	 * want to access the hardware too.  It is safe to do so.  Note that
2386 	 * this thread is still in the middle of a synchronized_op.
2387 	 */
2388 	set_adapter_hwstatus(sc, true);
2389 
2390 	if (sc->flags & FULL_INIT_DONE) {
2391 		rc = adapter_full_init(sc);
2392 		if (rc != 0) {
2393 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2394 			goto done;
2395 		}
2396 
2397 		if (sc->vxlan_refcount > 0)
2398 			enable_vxlan_rx(sc);
2399 
2400 		for_each_port(sc, i) {
2401 			pi = sc->port[i];
2402 			for_each_vi(pi, j, vi) {
2403 				mtx_lock(&vi->tick_mtx);
2404 				vi->flags &= ~VI_SKIP_STATS;
2405 				mtx_unlock(&vi->tick_mtx);
2406 				if (!(vi->flags & VI_INIT_DONE))
2407 					continue;
2408 				rc = vi_full_init(vi);
2409 				if (rc != 0) {
2410 					CH_ERR(vi, "failed to re-initialize "
2411 					    "interface: %d\n", rc);
2412 					goto done;
2413 				}
2414 				if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
2415 					sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
2416 					t4_write_reg(sc, is_t4(sc) ?
2417 					    A_MPS_TRC_RSS_CONTROL :
2418 					    A_MPS_T5_TRC_RSS_CONTROL,
2419 					    V_RSSCONTROL(pi->tx_chan) |
2420 					    V_QUEUENUMBER(sc->traceq));
2421 					pi->flags |= HAS_TRACEQ;
2422 				}
2423 
2424 				ifp = vi->ifp;
2425 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2426 					continue;
2427 				/*
2428 				 * Note that we do not setup multicast addresses
2429 				 * in the first pass.  This ensures that the
2430 				 * unicast DMACs for all VIs on all ports get an
2431 				 * MPS TCAM entry.
2432 				 */
2433 				rc = update_mac_settings(ifp, XGMAC_ALL &
2434 				    ~XGMAC_MCADDRS);
2435 				if (rc != 0) {
2436 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2437 					goto done;
2438 				}
2439 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2440 				    true);
2441 				if (rc != 0) {
2442 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2443 					goto done;
2444 				}
2445 				for_each_txq(vi, k, txq) {
2446 					TXQ_LOCK(txq);
2447 					txq->eq.flags |= EQ_ENABLED;
2448 					TXQ_UNLOCK(txq);
2449 				}
2450 				mtx_lock(&vi->tick_mtx);
2451 				callout_schedule(&vi->tick, hz);
2452 				mtx_unlock(&vi->tick_mtx);
2453 			}
2454 			PORT_LOCK(pi);
2455 			if (pi->up_vis > 0) {
2456 				t4_update_port_info(pi);
2457 				fixup_link_config(pi);
2458 				build_medialist(pi);
2459 				apply_link_config(pi);
2460 				if (pi->link_cfg.link_ok)
2461 					t4_os_link_changed(pi);
2462 			}
2463 			PORT_UNLOCK(pi);
2464 		}
2465 
2466 		/* Now reprogram the L2 multicast addresses. */
2467 		for_each_port(sc, i) {
2468 			pi = sc->port[i];
2469 			for_each_vi(pi, j, vi) {
2470 				if (!(vi->flags & VI_INIT_DONE))
2471 					continue;
2472 				ifp = vi->ifp;
2473 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2474 					continue;
2475 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2476 				if (rc != 0) {
2477 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2478 					rc = 0;	/* carry on */
2479 				}
2480 			}
2481 		}
2482 	}
2483 
2484 	/* Reset all calibration */
2485 	t4_calibration_start(sc);
2486 done:
2487 	end_synchronized_op(sc, 0);
2488 	free(old_state, M_CXGBE);
2489 
2490 	restart_atid_allocator(sc);
2491 	t4_restart_l2t(sc);
2492 
2493 	return (rc);
2494 }
2495 
2496 int
resume_adapter(struct adapter * sc)2497 resume_adapter(struct adapter *sc)
2498 {
2499 	restart_adapter(sc);
2500 	restart_lld(sc);
2501 #ifdef TCP_OFFLOAD
2502 	restart_all_uld(sc);
2503 #endif
2504 	return (0);
2505 }
2506 
2507 static int
t4_resume(device_t dev)2508 t4_resume(device_t dev)
2509 {
2510 	struct adapter *sc = device_get_softc(dev);
2511 	int rc;
2512 
2513 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2514 	rc = resume_adapter(sc);
2515 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2516 
2517 	return (rc);
2518 }
2519 
2520 static int
t4_reset_prepare(device_t dev,device_t child)2521 t4_reset_prepare(device_t dev, device_t child)
2522 {
2523 	struct adapter *sc = device_get_softc(dev);
2524 
2525 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2526 	return (0);
2527 }
2528 
2529 static int
t4_reset_post(device_t dev,device_t child)2530 t4_reset_post(device_t dev, device_t child)
2531 {
2532 	struct adapter *sc = device_get_softc(dev);
2533 
2534 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2535 	return (0);
2536 }
2537 
2538 static int
reset_adapter_with_pci_bus_reset(struct adapter * sc)2539 reset_adapter_with_pci_bus_reset(struct adapter *sc)
2540 {
2541 	int rc;
2542 
2543 	mtx_lock(&Giant);
2544 	rc = BUS_RESET_CHILD(device_get_parent(sc->dev), sc->dev, 0);
2545 	mtx_unlock(&Giant);
2546 	return (rc);
2547 }
2548 
2549 static int
reset_adapter_with_pl_rst(struct adapter * sc)2550 reset_adapter_with_pl_rst(struct adapter *sc)
2551 {
2552 	suspend_adapter(sc);
2553 
2554 	/* This is a t4_write_reg without the hw_off_limits check. */
2555 	MPASS(sc->error_flags & HW_OFF_LIMITS);
2556 	bus_space_write_4(sc->bt, sc->bh, A_PL_RST,
2557 			  F_PIORSTMODE | F_PIORST | F_AUTOPCIEPAUSE);
2558 	pause("pl_rst", 1 * hz);		/* Wait 1s for reset */
2559 
2560 	resume_adapter(sc);
2561 
2562 	return (0);
2563 }
2564 
2565 static inline int
reset_adapter(struct adapter * sc)2566 reset_adapter(struct adapter *sc)
2567 {
2568 	if (vm_guest == 0)
2569 		return (reset_adapter_with_pci_bus_reset(sc));
2570 	else
2571 		return (reset_adapter_with_pl_rst(sc));
2572 }
2573 
2574 static void
reset_adapter_task(void * arg,int pending)2575 reset_adapter_task(void *arg, int pending)
2576 {
2577 	struct adapter *sc = arg;
2578 	const int flags = sc->flags;
2579 	const int eflags = sc->error_flags;
2580 	int rc;
2581 
2582 	if (pending > 1)
2583 		CH_ALERT(sc, "%s: pending %d\n", __func__, pending);
2584 	rc = reset_adapter(sc);
2585 	if (rc != 0) {
2586 		CH_ERR(sc, "adapter did not reset properly, rc = %d, "
2587 		       "flags 0x%08x -> 0x%08x, err_flags 0x%08x -> 0x%08x.\n",
2588 		       rc, flags, sc->flags, eflags, sc->error_flags);
2589 	}
2590 }
2591 
2592 static int
cxgbe_probe(device_t dev)2593 cxgbe_probe(device_t dev)
2594 {
2595 	struct port_info *pi = device_get_softc(dev);
2596 
2597 	device_set_descf(dev, "port %d", pi->port_id);
2598 
2599 	return (BUS_PROBE_DEFAULT);
2600 }
2601 
2602 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2603     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2604     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2605     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2606 #define T4_CAP_ENABLE (T4_CAP)
2607 
2608 static void
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2609 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2610 {
2611 	if_t ifp;
2612 	struct sbuf *sb;
2613 	struct sysctl_ctx_list *ctx = &vi->ctx;
2614 	struct sysctl_oid_list *children;
2615 	struct pfil_head_args pa;
2616 	struct adapter *sc = vi->adapter;
2617 
2618 	sysctl_ctx_init(ctx);
2619 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2620 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2621 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2622 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2623 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2624 #ifdef DEV_NETMAP
2625 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2626 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2627 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2628 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2629 #endif
2630 #ifdef TCP_OFFLOAD
2631 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2632 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2633 #endif
2634 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2635 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2636 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2637 #endif
2638 
2639 	vi->xact_addr_filt = -1;
2640 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2641 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2642 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2643 		vi->flags |= TX_USES_VM_WR;
2644 
2645 	/* Allocate an ifnet and set it up */
2646 	ifp = if_alloc_dev(IFT_ETHER, dev);
2647 	vi->ifp = ifp;
2648 	if_setsoftc(ifp, vi);
2649 
2650 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2651 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2652 
2653 	if_setinitfn(ifp, cxgbe_init);
2654 	if_setioctlfn(ifp, cxgbe_ioctl);
2655 	if_settransmitfn(ifp, cxgbe_transmit);
2656 	if_setqflushfn(ifp, cxgbe_qflush);
2657 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2658 		if_setgetcounterfn(ifp, vi_get_counter);
2659 	else
2660 		if_setgetcounterfn(ifp, cxgbe_get_counter);
2661 #if defined(KERN_TLS) || defined(RATELIMIT)
2662 	if_setsndtagallocfn(ifp, cxgbe_snd_tag_alloc);
2663 #endif
2664 #ifdef RATELIMIT
2665 	if_setratelimitqueryfn(ifp, cxgbe_ratelimit_query);
2666 #endif
2667 
2668 	if_setcapabilities(ifp, T4_CAP);
2669 	if_setcapenable(ifp, T4_CAP_ENABLE);
2670 	if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2671 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2672 	if (chip_id(sc) >= CHELSIO_T6) {
2673 		if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2674 		if_setcapenablebit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2675 		if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2676 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2677 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN, 0);
2678 	}
2679 
2680 #ifdef TCP_OFFLOAD
2681 	if (vi->nofldrxq != 0)
2682 		if_setcapabilitiesbit(ifp, IFCAP_TOE, 0);
2683 #endif
2684 #ifdef RATELIMIT
2685 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2686 		if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT, 0);
2687 		if_setcapenablebit(ifp, IFCAP_TXRTLMT, 0);
2688 	}
2689 #endif
2690 
2691 	if_sethwtsomax(ifp, IP_MAXPACKET);
2692 	if (vi->flags & TX_USES_VM_WR)
2693 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_VM_TSO);
2694 	else
2695 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_TSO);
2696 #ifdef RATELIMIT
2697 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2698 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_EO_TSO);
2699 #endif
2700 	if_sethwtsomaxsegsize(ifp, 65536);
2701 #ifdef KERN_TLS
2702 	if (is_ktls(sc)) {
2703 		if_setcapabilitiesbit(ifp, IFCAP_TXTLS, 0);
2704 		if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2705 			if_setcapenablebit(ifp, IFCAP_TXTLS, 0);
2706 	}
2707 #endif
2708 
2709 	ether_ifattach(ifp, vi->hw_addr);
2710 #ifdef DEV_NETMAP
2711 	if (vi->nnmrxq != 0)
2712 		cxgbe_nm_attach(vi);
2713 #endif
2714 	sb = sbuf_new_auto();
2715 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2716 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2717 	switch (if_getcapabilities(ifp) & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2718 	case IFCAP_TOE:
2719 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2720 		break;
2721 	case IFCAP_TOE | IFCAP_TXRTLMT:
2722 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2723 		break;
2724 	case IFCAP_TXRTLMT:
2725 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2726 		break;
2727 	}
2728 #endif
2729 #ifdef TCP_OFFLOAD
2730 	if (if_getcapabilities(ifp) & IFCAP_TOE)
2731 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2732 #endif
2733 #ifdef DEV_NETMAP
2734 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2735 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2736 		    vi->nnmtxq, vi->nnmrxq);
2737 #endif
2738 	sbuf_finish(sb);
2739 	device_printf(dev, "%s\n", sbuf_data(sb));
2740 	sbuf_delete(sb);
2741 
2742 	vi_sysctls(vi);
2743 
2744 	pa.pa_version = PFIL_VERSION;
2745 	pa.pa_flags = PFIL_IN;
2746 	pa.pa_type = PFIL_TYPE_ETHERNET;
2747 	pa.pa_headname = if_name(ifp);
2748 	vi->pfil = pfil_head_register(&pa);
2749 }
2750 
2751 static int
cxgbe_attach(device_t dev)2752 cxgbe_attach(device_t dev)
2753 {
2754 	struct port_info *pi = device_get_softc(dev);
2755 	struct adapter *sc = pi->adapter;
2756 	struct vi_info *vi;
2757 	int i;
2758 
2759 	sysctl_ctx_init(&pi->ctx);
2760 
2761 	cxgbe_vi_attach(dev, &pi->vi[0]);
2762 
2763 	for_each_vi(pi, i, vi) {
2764 		if (i == 0)
2765 			continue;
2766 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, DEVICE_UNIT_ANY);
2767 		if (vi->dev == NULL) {
2768 			device_printf(dev, "failed to add VI %d\n", i);
2769 			continue;
2770 		}
2771 		device_set_softc(vi->dev, vi);
2772 	}
2773 
2774 	cxgbe_sysctls(pi);
2775 
2776 	bus_attach_children(dev);
2777 
2778 	return (0);
2779 }
2780 
2781 static void
cxgbe_vi_detach(struct vi_info * vi)2782 cxgbe_vi_detach(struct vi_info *vi)
2783 {
2784 	if_t ifp = vi->ifp;
2785 
2786 	if (vi->pfil != NULL) {
2787 		pfil_head_unregister(vi->pfil);
2788 		vi->pfil = NULL;
2789 	}
2790 
2791 	ether_ifdetach(ifp);
2792 
2793 	/* Let detach proceed even if these fail. */
2794 #ifdef DEV_NETMAP
2795 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2796 		cxgbe_nm_detach(vi);
2797 #endif
2798 	cxgbe_uninit_synchronized(vi);
2799 	callout_drain(&vi->tick);
2800 	mtx_destroy(&vi->tick_mtx);
2801 	sysctl_ctx_free(&vi->ctx);
2802 	vi_full_uninit(vi);
2803 
2804 	if_free(vi->ifp);
2805 	vi->ifp = NULL;
2806 }
2807 
2808 static int
cxgbe_detach(device_t dev)2809 cxgbe_detach(device_t dev)
2810 {
2811 	struct port_info *pi = device_get_softc(dev);
2812 	struct adapter *sc = pi->adapter;
2813 	int rc;
2814 
2815 	/* Detach the extra VIs first. */
2816 	rc = bus_generic_detach(dev);
2817 	if (rc)
2818 		return (rc);
2819 	device_delete_children(dev);
2820 
2821 	sysctl_ctx_free(&pi->ctx);
2822 	begin_vi_detach(sc, &pi->vi[0]);
2823 	if (pi->flags & HAS_TRACEQ) {
2824 		sc->traceq = -1;	/* cloner should not create ifnet */
2825 		t4_tracer_port_detach(sc);
2826 	}
2827 	cxgbe_vi_detach(&pi->vi[0]);
2828 	ifmedia_removeall(&pi->media);
2829 	end_vi_detach(sc, &pi->vi[0]);
2830 
2831 	return (0);
2832 }
2833 
2834 static void
cxgbe_init(void * arg)2835 cxgbe_init(void *arg)
2836 {
2837 	struct vi_info *vi = arg;
2838 	struct adapter *sc = vi->adapter;
2839 
2840 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
2841 		return;
2842 	cxgbe_init_synchronized(vi);
2843 	end_synchronized_op(sc, 0);
2844 }
2845 
2846 static int
cxgbe_ioctl(if_t ifp,unsigned long cmd,caddr_t data)2847 cxgbe_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
2848 {
2849 	int rc = 0, mtu, flags;
2850 	struct vi_info *vi = if_getsoftc(ifp);
2851 	struct port_info *pi = vi->pi;
2852 	struct adapter *sc = pi->adapter;
2853 	struct ifreq *ifr = (struct ifreq *)data;
2854 	uint32_t mask;
2855 
2856 	switch (cmd) {
2857 	case SIOCSIFMTU:
2858 		mtu = ifr->ifr_mtu;
2859 		if (mtu < ETHERMIN || mtu > MAX_MTU)
2860 			return (EINVAL);
2861 
2862 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
2863 		if (rc)
2864 			return (rc);
2865 		if_setmtu(ifp, mtu);
2866 		if (vi->flags & VI_INIT_DONE) {
2867 			t4_update_fl_bufsize(ifp);
2868 			if (!hw_off_limits(sc) &&
2869 			    if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2870 				rc = update_mac_settings(ifp, XGMAC_MTU);
2871 		}
2872 		end_synchronized_op(sc, 0);
2873 		break;
2874 
2875 	case SIOCSIFFLAGS:
2876 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
2877 		if (rc)
2878 			return (rc);
2879 
2880 		if (hw_off_limits(sc)) {
2881 			rc = ENXIO;
2882 			goto fail;
2883 		}
2884 
2885 		if (if_getflags(ifp) & IFF_UP) {
2886 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2887 				flags = vi->if_flags;
2888 				if ((if_getflags(ifp) ^ flags) &
2889 				    (IFF_PROMISC | IFF_ALLMULTI)) {
2890 					rc = update_mac_settings(ifp,
2891 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
2892 				}
2893 			} else {
2894 				rc = cxgbe_init_synchronized(vi);
2895 			}
2896 			vi->if_flags = if_getflags(ifp);
2897 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2898 			rc = cxgbe_uninit_synchronized(vi);
2899 		}
2900 		end_synchronized_op(sc, 0);
2901 		break;
2902 
2903 	case SIOCADDMULTI:
2904 	case SIOCDELMULTI:
2905 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
2906 		if (rc)
2907 			return (rc);
2908 		if (!hw_off_limits(sc) && if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2909 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2910 		end_synchronized_op(sc, 0);
2911 		break;
2912 
2913 	case SIOCSIFCAP:
2914 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
2915 		if (rc)
2916 			return (rc);
2917 
2918 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
2919 		if (mask & IFCAP_TXCSUM) {
2920 			if_togglecapenable(ifp, IFCAP_TXCSUM);
2921 			if_togglehwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP);
2922 
2923 			if (IFCAP_TSO4 & if_getcapenable(ifp) &&
2924 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
2925 				mask &= ~IFCAP_TSO4;
2926 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
2927 				if_printf(ifp,
2928 				    "tso4 disabled due to -txcsum.\n");
2929 			}
2930 		}
2931 		if (mask & IFCAP_TXCSUM_IPV6) {
2932 			if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
2933 			if_togglehwassist(ifp, CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2934 
2935 			if (IFCAP_TSO6 & if_getcapenable(ifp) &&
2936 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
2937 				mask &= ~IFCAP_TSO6;
2938 				if_setcapenablebit(ifp, 0, IFCAP_TSO6);
2939 				if_printf(ifp,
2940 				    "tso6 disabled due to -txcsum6.\n");
2941 			}
2942 		}
2943 		if (mask & IFCAP_RXCSUM)
2944 			if_togglecapenable(ifp, IFCAP_RXCSUM);
2945 		if (mask & IFCAP_RXCSUM_IPV6)
2946 			if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
2947 
2948 		/*
2949 		 * Note that we leave CSUM_TSO alone (it is always set).  The
2950 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
2951 		 * sending a TSO request our way, so it's sufficient to toggle
2952 		 * IFCAP_TSOx only.
2953 		 */
2954 		if (mask & IFCAP_TSO4) {
2955 			if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
2956 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
2957 				if_printf(ifp, "enable txcsum first.\n");
2958 				rc = EAGAIN;
2959 				goto fail;
2960 			}
2961 			if_togglecapenable(ifp, IFCAP_TSO4);
2962 		}
2963 		if (mask & IFCAP_TSO6) {
2964 			if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
2965 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
2966 				if_printf(ifp, "enable txcsum6 first.\n");
2967 				rc = EAGAIN;
2968 				goto fail;
2969 			}
2970 			if_togglecapenable(ifp, IFCAP_TSO6);
2971 		}
2972 		if (mask & IFCAP_LRO) {
2973 #if defined(INET) || defined(INET6)
2974 			int i;
2975 			struct sge_rxq *rxq;
2976 
2977 			if_togglecapenable(ifp, IFCAP_LRO);
2978 			for_each_rxq(vi, i, rxq) {
2979 				if (if_getcapenable(ifp) & IFCAP_LRO)
2980 					rxq->iq.flags |= IQ_LRO_ENABLED;
2981 				else
2982 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
2983 			}
2984 #endif
2985 		}
2986 #ifdef TCP_OFFLOAD
2987 		if (mask & IFCAP_TOE) {
2988 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TOE;
2989 
2990 			rc = toe_capability(vi, enable);
2991 			if (rc != 0)
2992 				goto fail;
2993 
2994 			if_togglecapenable(ifp, mask);
2995 		}
2996 #endif
2997 		if (mask & IFCAP_VLAN_HWTAGGING) {
2998 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
2999 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3000 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
3001 		}
3002 		if (mask & IFCAP_VLAN_MTU) {
3003 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
3004 
3005 			/* Need to find out how to disable auto-mtu-inflation */
3006 		}
3007 		if (mask & IFCAP_VLAN_HWTSO)
3008 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3009 		if (mask & IFCAP_VLAN_HWCSUM)
3010 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
3011 #ifdef RATELIMIT
3012 		if (mask & IFCAP_TXRTLMT)
3013 			if_togglecapenable(ifp, IFCAP_TXRTLMT);
3014 #endif
3015 		if (mask & IFCAP_HWRXTSTMP) {
3016 			int i;
3017 			struct sge_rxq *rxq;
3018 
3019 			if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3020 			for_each_rxq(vi, i, rxq) {
3021 				if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3022 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
3023 				else
3024 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
3025 			}
3026 		}
3027 		if (mask & IFCAP_MEXTPG)
3028 			if_togglecapenable(ifp, IFCAP_MEXTPG);
3029 
3030 #ifdef KERN_TLS
3031 		if (mask & IFCAP_TXTLS) {
3032 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TXTLS;
3033 
3034 			rc = ktls_capability(sc, enable);
3035 			if (rc != 0)
3036 				goto fail;
3037 
3038 			if_togglecapenable(ifp, mask & IFCAP_TXTLS);
3039 		}
3040 #endif
3041 		if (mask & IFCAP_VXLAN_HWCSUM) {
3042 			if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3043 			if_togglehwassist(ifp, CSUM_INNER_IP6_UDP |
3044 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
3045 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP);
3046 		}
3047 		if (mask & IFCAP_VXLAN_HWTSO) {
3048 			if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3049 			if_togglehwassist(ifp, CSUM_INNER_IP6_TSO |
3050 			    CSUM_INNER_IP_TSO);
3051 		}
3052 
3053 #ifdef VLAN_CAPABILITIES
3054 		VLAN_CAPABILITIES(ifp);
3055 #endif
3056 fail:
3057 		end_synchronized_op(sc, 0);
3058 		break;
3059 
3060 	case SIOCSIFMEDIA:
3061 	case SIOCGIFMEDIA:
3062 	case SIOCGIFXMEDIA:
3063 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3064 		break;
3065 
3066 	case SIOCGI2C: {
3067 		struct ifi2creq i2c;
3068 
3069 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3070 		if (rc != 0)
3071 			break;
3072 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3073 			rc = EPERM;
3074 			break;
3075 		}
3076 		if (i2c.len > sizeof(i2c.data)) {
3077 			rc = EINVAL;
3078 			break;
3079 		}
3080 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3081 		if (rc)
3082 			return (rc);
3083 		if (hw_off_limits(sc))
3084 			rc = ENXIO;
3085 		else
3086 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3087 			    i2c.offset, i2c.len, &i2c.data[0]);
3088 		end_synchronized_op(sc, 0);
3089 		if (rc == 0)
3090 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3091 		break;
3092 	}
3093 
3094 	default:
3095 		rc = ether_ioctl(ifp, cmd, data);
3096 	}
3097 
3098 	return (rc);
3099 }
3100 
3101 static int
cxgbe_transmit(if_t ifp,struct mbuf * m)3102 cxgbe_transmit(if_t ifp, struct mbuf *m)
3103 {
3104 	struct vi_info *vi = if_getsoftc(ifp);
3105 	struct port_info *pi = vi->pi;
3106 	struct adapter *sc;
3107 	struct sge_txq *txq;
3108 	void *items[1];
3109 	int rc;
3110 
3111 	M_ASSERTPKTHDR(m);
3112 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
3113 #if defined(KERN_TLS) || defined(RATELIMIT)
3114 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3115 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3116 #endif
3117 
3118 	if (__predict_false(pi->link_cfg.link_ok == false)) {
3119 		m_freem(m);
3120 		return (ENETDOWN);
3121 	}
3122 
3123 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3124 	if (__predict_false(rc != 0)) {
3125 		if (__predict_true(rc == EINPROGRESS)) {
3126 			/* queued by parse_pkt */
3127 			MPASS(m != NULL);
3128 			return (0);
3129 		}
3130 
3131 		MPASS(m == NULL);			/* was freed already */
3132 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
3133 		return (rc);
3134 	}
3135 
3136 	/* Select a txq. */
3137 	sc = vi->adapter;
3138 	txq = &sc->sge.txq[vi->first_txq];
3139 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3140 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3141 		    vi->rsrv_noflowq);
3142 
3143 	items[0] = m;
3144 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3145 	if (__predict_false(rc != 0))
3146 		m_freem(m);
3147 
3148 	return (rc);
3149 }
3150 
3151 static void
cxgbe_qflush(if_t ifp)3152 cxgbe_qflush(if_t ifp)
3153 {
3154 	struct vi_info *vi = if_getsoftc(ifp);
3155 	struct sge_txq *txq;
3156 	int i;
3157 
3158 	/* queues do not exist if !VI_INIT_DONE. */
3159 	if (vi->flags & VI_INIT_DONE) {
3160 		for_each_txq(vi, i, txq) {
3161 			TXQ_LOCK(txq);
3162 			txq->eq.flags |= EQ_QFLUSH;
3163 			TXQ_UNLOCK(txq);
3164 			while (!mp_ring_is_idle(txq->r)) {
3165 				mp_ring_check_drainage(txq->r, 4096);
3166 				pause("qflush", 1);
3167 			}
3168 			TXQ_LOCK(txq);
3169 			txq->eq.flags &= ~EQ_QFLUSH;
3170 			TXQ_UNLOCK(txq);
3171 		}
3172 	}
3173 	if_qflush(ifp);
3174 }
3175 
3176 static uint64_t
vi_get_counter(if_t ifp,ift_counter c)3177 vi_get_counter(if_t ifp, ift_counter c)
3178 {
3179 	struct vi_info *vi = if_getsoftc(ifp);
3180 	struct fw_vi_stats_vf *s = &vi->stats;
3181 
3182 	mtx_lock(&vi->tick_mtx);
3183 	vi_refresh_stats(vi);
3184 	mtx_unlock(&vi->tick_mtx);
3185 
3186 	switch (c) {
3187 	case IFCOUNTER_IPACKETS:
3188 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3189 		    s->rx_ucast_frames);
3190 	case IFCOUNTER_IERRORS:
3191 		return (s->rx_err_frames);
3192 	case IFCOUNTER_OPACKETS:
3193 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3194 		    s->tx_ucast_frames + s->tx_offload_frames);
3195 	case IFCOUNTER_OERRORS:
3196 		return (s->tx_drop_frames);
3197 	case IFCOUNTER_IBYTES:
3198 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3199 		    s->rx_ucast_bytes);
3200 	case IFCOUNTER_OBYTES:
3201 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3202 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3203 	case IFCOUNTER_IMCASTS:
3204 		return (s->rx_mcast_frames);
3205 	case IFCOUNTER_OMCASTS:
3206 		return (s->tx_mcast_frames);
3207 	case IFCOUNTER_OQDROPS: {
3208 		uint64_t drops;
3209 
3210 		drops = 0;
3211 		if (vi->flags & VI_INIT_DONE) {
3212 			int i;
3213 			struct sge_txq *txq;
3214 
3215 			for_each_txq(vi, i, txq)
3216 				drops += counter_u64_fetch(txq->r->dropped);
3217 		}
3218 
3219 		return (drops);
3220 
3221 	}
3222 
3223 	default:
3224 		return (if_get_counter_default(ifp, c));
3225 	}
3226 }
3227 
3228 static uint64_t
cxgbe_get_counter(if_t ifp,ift_counter c)3229 cxgbe_get_counter(if_t ifp, ift_counter c)
3230 {
3231 	struct vi_info *vi = if_getsoftc(ifp);
3232 	struct port_info *pi = vi->pi;
3233 	struct port_stats *s = &pi->stats;
3234 
3235 	mtx_lock(&vi->tick_mtx);
3236 	cxgbe_refresh_stats(vi);
3237 	mtx_unlock(&vi->tick_mtx);
3238 
3239 	switch (c) {
3240 	case IFCOUNTER_IPACKETS:
3241 		return (s->rx_frames);
3242 
3243 	case IFCOUNTER_IERRORS:
3244 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3245 		    s->rx_fcs_err + s->rx_len_err);
3246 
3247 	case IFCOUNTER_OPACKETS:
3248 		return (s->tx_frames);
3249 
3250 	case IFCOUNTER_OERRORS:
3251 		return (s->tx_error_frames);
3252 
3253 	case IFCOUNTER_IBYTES:
3254 		return (s->rx_octets);
3255 
3256 	case IFCOUNTER_OBYTES:
3257 		return (s->tx_octets);
3258 
3259 	case IFCOUNTER_IMCASTS:
3260 		return (s->rx_mcast_frames);
3261 
3262 	case IFCOUNTER_OMCASTS:
3263 		return (s->tx_mcast_frames);
3264 
3265 	case IFCOUNTER_IQDROPS:
3266 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3267 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3268 		    s->rx_trunc3 + pi->tnl_cong_drops);
3269 
3270 	case IFCOUNTER_OQDROPS: {
3271 		uint64_t drops;
3272 
3273 		drops = s->tx_drop;
3274 		if (vi->flags & VI_INIT_DONE) {
3275 			int i;
3276 			struct sge_txq *txq;
3277 
3278 			for_each_txq(vi, i, txq)
3279 				drops += counter_u64_fetch(txq->r->dropped);
3280 		}
3281 
3282 		return (drops);
3283 
3284 	}
3285 
3286 	default:
3287 		return (if_get_counter_default(ifp, c));
3288 	}
3289 }
3290 
3291 #if defined(KERN_TLS) || defined(RATELIMIT)
3292 static int
cxgbe_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3293 cxgbe_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3294     struct m_snd_tag **pt)
3295 {
3296 	int error;
3297 
3298 	switch (params->hdr.type) {
3299 #ifdef RATELIMIT
3300 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3301 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3302 		break;
3303 #endif
3304 #ifdef KERN_TLS
3305 	case IF_SND_TAG_TYPE_TLS:
3306 	{
3307 		struct vi_info *vi = if_getsoftc(ifp);
3308 
3309 		if (is_t6(vi->pi->adapter))
3310 			error = t6_tls_tag_alloc(ifp, params, pt);
3311 		else
3312 			error = EOPNOTSUPP;
3313 		break;
3314 	}
3315 #endif
3316 	default:
3317 		error = EOPNOTSUPP;
3318 	}
3319 	return (error);
3320 }
3321 #endif
3322 
3323 /*
3324  * The kernel picks a media from the list we had provided but we still validate
3325  * the requeste.
3326  */
3327 int
cxgbe_media_change(if_t ifp)3328 cxgbe_media_change(if_t ifp)
3329 {
3330 	struct vi_info *vi = if_getsoftc(ifp);
3331 	struct port_info *pi = vi->pi;
3332 	struct ifmedia *ifm = &pi->media;
3333 	struct link_config *lc = &pi->link_cfg;
3334 	struct adapter *sc = pi->adapter;
3335 	int rc;
3336 
3337 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3338 	if (rc != 0)
3339 		return (rc);
3340 	PORT_LOCK(pi);
3341 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3342 		/* ifconfig .. media autoselect */
3343 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3344 			rc = ENOTSUP; /* AN not supported by transceiver */
3345 			goto done;
3346 		}
3347 		lc->requested_aneg = AUTONEG_ENABLE;
3348 		lc->requested_speed = 0;
3349 		lc->requested_fc |= PAUSE_AUTONEG;
3350 	} else {
3351 		lc->requested_aneg = AUTONEG_DISABLE;
3352 		lc->requested_speed =
3353 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3354 		lc->requested_fc = 0;
3355 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3356 			lc->requested_fc |= PAUSE_RX;
3357 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3358 			lc->requested_fc |= PAUSE_TX;
3359 	}
3360 	if (pi->up_vis > 0 && !hw_off_limits(sc)) {
3361 		fixup_link_config(pi);
3362 		rc = apply_link_config(pi);
3363 	}
3364 done:
3365 	PORT_UNLOCK(pi);
3366 	end_synchronized_op(sc, 0);
3367 	return (rc);
3368 }
3369 
3370 /*
3371  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3372  * given speed.
3373  */
3374 static int
port_mword(struct port_info * pi,uint32_t speed)3375 port_mword(struct port_info *pi, uint32_t speed)
3376 {
3377 
3378 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3379 	MPASS(powerof2(speed));
3380 
3381 	switch(pi->port_type) {
3382 	case FW_PORT_TYPE_BT_SGMII:
3383 	case FW_PORT_TYPE_BT_XFI:
3384 	case FW_PORT_TYPE_BT_XAUI:
3385 		/* BaseT */
3386 		switch (speed) {
3387 		case FW_PORT_CAP32_SPEED_100M:
3388 			return (IFM_100_T);
3389 		case FW_PORT_CAP32_SPEED_1G:
3390 			return (IFM_1000_T);
3391 		case FW_PORT_CAP32_SPEED_10G:
3392 			return (IFM_10G_T);
3393 		}
3394 		break;
3395 	case FW_PORT_TYPE_KX4:
3396 		if (speed == FW_PORT_CAP32_SPEED_10G)
3397 			return (IFM_10G_KX4);
3398 		break;
3399 	case FW_PORT_TYPE_CX4:
3400 		if (speed == FW_PORT_CAP32_SPEED_10G)
3401 			return (IFM_10G_CX4);
3402 		break;
3403 	case FW_PORT_TYPE_KX:
3404 		if (speed == FW_PORT_CAP32_SPEED_1G)
3405 			return (IFM_1000_KX);
3406 		break;
3407 	case FW_PORT_TYPE_KR:
3408 	case FW_PORT_TYPE_BP_AP:
3409 	case FW_PORT_TYPE_BP4_AP:
3410 	case FW_PORT_TYPE_BP40_BA:
3411 	case FW_PORT_TYPE_KR4_100G:
3412 	case FW_PORT_TYPE_KR_SFP28:
3413 	case FW_PORT_TYPE_KR_XLAUI:
3414 		switch (speed) {
3415 		case FW_PORT_CAP32_SPEED_1G:
3416 			return (IFM_1000_KX);
3417 		case FW_PORT_CAP32_SPEED_10G:
3418 			return (IFM_10G_KR);
3419 		case FW_PORT_CAP32_SPEED_25G:
3420 			return (IFM_25G_KR);
3421 		case FW_PORT_CAP32_SPEED_40G:
3422 			return (IFM_40G_KR4);
3423 		case FW_PORT_CAP32_SPEED_50G:
3424 			return (IFM_50G_KR2);
3425 		case FW_PORT_CAP32_SPEED_100G:
3426 			return (IFM_100G_KR4);
3427 		}
3428 		break;
3429 	case FW_PORT_TYPE_FIBER_XFI:
3430 	case FW_PORT_TYPE_FIBER_XAUI:
3431 	case FW_PORT_TYPE_SFP:
3432 	case FW_PORT_TYPE_QSFP_10G:
3433 	case FW_PORT_TYPE_QSA:
3434 	case FW_PORT_TYPE_QSFP:
3435 	case FW_PORT_TYPE_CR4_QSFP:
3436 	case FW_PORT_TYPE_CR_QSFP:
3437 	case FW_PORT_TYPE_CR2_QSFP:
3438 	case FW_PORT_TYPE_SFP28:
3439 		/* Pluggable transceiver */
3440 		switch (pi->mod_type) {
3441 		case FW_PORT_MOD_TYPE_LR:
3442 			switch (speed) {
3443 			case FW_PORT_CAP32_SPEED_1G:
3444 				return (IFM_1000_LX);
3445 			case FW_PORT_CAP32_SPEED_10G:
3446 				return (IFM_10G_LR);
3447 			case FW_PORT_CAP32_SPEED_25G:
3448 				return (IFM_25G_LR);
3449 			case FW_PORT_CAP32_SPEED_40G:
3450 				return (IFM_40G_LR4);
3451 			case FW_PORT_CAP32_SPEED_50G:
3452 				return (IFM_50G_LR2);
3453 			case FW_PORT_CAP32_SPEED_100G:
3454 				return (IFM_100G_LR4);
3455 			}
3456 			break;
3457 		case FW_PORT_MOD_TYPE_SR:
3458 			switch (speed) {
3459 			case FW_PORT_CAP32_SPEED_1G:
3460 				return (IFM_1000_SX);
3461 			case FW_PORT_CAP32_SPEED_10G:
3462 				return (IFM_10G_SR);
3463 			case FW_PORT_CAP32_SPEED_25G:
3464 				return (IFM_25G_SR);
3465 			case FW_PORT_CAP32_SPEED_40G:
3466 				return (IFM_40G_SR4);
3467 			case FW_PORT_CAP32_SPEED_50G:
3468 				return (IFM_50G_SR2);
3469 			case FW_PORT_CAP32_SPEED_100G:
3470 				return (IFM_100G_SR4);
3471 			}
3472 			break;
3473 		case FW_PORT_MOD_TYPE_ER:
3474 			if (speed == FW_PORT_CAP32_SPEED_10G)
3475 				return (IFM_10G_ER);
3476 			break;
3477 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3478 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3479 			switch (speed) {
3480 			case FW_PORT_CAP32_SPEED_1G:
3481 				return (IFM_1000_CX);
3482 			case FW_PORT_CAP32_SPEED_10G:
3483 				return (IFM_10G_TWINAX);
3484 			case FW_PORT_CAP32_SPEED_25G:
3485 				return (IFM_25G_CR);
3486 			case FW_PORT_CAP32_SPEED_40G:
3487 				return (IFM_40G_CR4);
3488 			case FW_PORT_CAP32_SPEED_50G:
3489 				return (IFM_50G_CR2);
3490 			case FW_PORT_CAP32_SPEED_100G:
3491 				return (IFM_100G_CR4);
3492 			}
3493 			break;
3494 		case FW_PORT_MOD_TYPE_LRM:
3495 			if (speed == FW_PORT_CAP32_SPEED_10G)
3496 				return (IFM_10G_LRM);
3497 			break;
3498 		case FW_PORT_MOD_TYPE_NA:
3499 			MPASS(0);	/* Not pluggable? */
3500 			/* fall throough */
3501 		case FW_PORT_MOD_TYPE_ERROR:
3502 		case FW_PORT_MOD_TYPE_UNKNOWN:
3503 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3504 			break;
3505 		case FW_PORT_MOD_TYPE_NONE:
3506 			return (IFM_NONE);
3507 		}
3508 		break;
3509 	case FW_PORT_TYPE_NONE:
3510 		return (IFM_NONE);
3511 	}
3512 
3513 	return (IFM_UNKNOWN);
3514 }
3515 
3516 void
cxgbe_media_status(if_t ifp,struct ifmediareq * ifmr)3517 cxgbe_media_status(if_t ifp, struct ifmediareq *ifmr)
3518 {
3519 	struct vi_info *vi = if_getsoftc(ifp);
3520 	struct port_info *pi = vi->pi;
3521 	struct adapter *sc = pi->adapter;
3522 	struct link_config *lc = &pi->link_cfg;
3523 
3524 	if (begin_synchronized_op(sc, vi , SLEEP_OK | INTR_OK, "t4med") != 0)
3525 		return;
3526 	PORT_LOCK(pi);
3527 
3528 	if (pi->up_vis == 0 && !hw_off_limits(sc)) {
3529 		/*
3530 		 * If all the interfaces are administratively down the firmware
3531 		 * does not report transceiver changes.  Refresh port info here
3532 		 * so that ifconfig displays accurate ifmedia at all times.
3533 		 * This is the only reason we have a synchronized op in this
3534 		 * function.  Just PORT_LOCK would have been enough otherwise.
3535 		 */
3536 		t4_update_port_info(pi);
3537 		build_medialist(pi);
3538 	}
3539 
3540 	/* ifm_status */
3541 	ifmr->ifm_status = IFM_AVALID;
3542 	if (lc->link_ok == false)
3543 		goto done;
3544 	ifmr->ifm_status |= IFM_ACTIVE;
3545 
3546 	/* ifm_active */
3547 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3548 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3549 	if (lc->fc & PAUSE_RX)
3550 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3551 	if (lc->fc & PAUSE_TX)
3552 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3553 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3554 done:
3555 	PORT_UNLOCK(pi);
3556 	end_synchronized_op(sc, 0);
3557 }
3558 
3559 static int
vcxgbe_probe(device_t dev)3560 vcxgbe_probe(device_t dev)
3561 {
3562 	struct vi_info *vi = device_get_softc(dev);
3563 
3564 	device_set_descf(dev, "port %d vi %td", vi->pi->port_id,
3565 	    vi - vi->pi->vi);
3566 
3567 	return (BUS_PROBE_DEFAULT);
3568 }
3569 
3570 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3571 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3572 {
3573 	int func, index, rc;
3574 	uint32_t param, val;
3575 
3576 	ASSERT_SYNCHRONIZED_OP(sc);
3577 
3578 	index = vi - pi->vi;
3579 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3580 	KASSERT(index < nitems(vi_mac_funcs),
3581 	    ("%s: VI %s doesn't have a MAC func", __func__,
3582 	    device_get_nameunit(vi->dev)));
3583 	func = vi_mac_funcs[index];
3584 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
3585 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3586 	if (rc < 0) {
3587 		CH_ERR(vi, "failed to allocate virtual interface %d"
3588 		    "for port %d: %d\n", index, pi->port_id, -rc);
3589 		return (-rc);
3590 	}
3591 	vi->viid = rc;
3592 
3593 	if (vi->rss_size == 1) {
3594 		/*
3595 		 * This VI didn't get a slice of the RSS table.  Reduce the
3596 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3597 		 * configuration file (nvi, rssnvi for this PF) if this is a
3598 		 * problem.
3599 		 */
3600 		device_printf(vi->dev, "RSS table not available.\n");
3601 		vi->rss_base = 0xffff;
3602 
3603 		return (0);
3604 	}
3605 
3606 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3607 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3608 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3609 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3610 	if (rc)
3611 		vi->rss_base = 0xffff;
3612 	else {
3613 		MPASS((val >> 16) == vi->rss_size);
3614 		vi->rss_base = val & 0xffff;
3615 	}
3616 
3617 	return (0);
3618 }
3619 
3620 static int
vcxgbe_attach(device_t dev)3621 vcxgbe_attach(device_t dev)
3622 {
3623 	struct vi_info *vi;
3624 	struct port_info *pi;
3625 	struct adapter *sc;
3626 	int rc;
3627 
3628 	vi = device_get_softc(dev);
3629 	pi = vi->pi;
3630 	sc = pi->adapter;
3631 
3632 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3633 	if (rc)
3634 		return (rc);
3635 	rc = alloc_extra_vi(sc, pi, vi);
3636 	end_synchronized_op(sc, 0);
3637 	if (rc)
3638 		return (rc);
3639 
3640 	cxgbe_vi_attach(dev, vi);
3641 
3642 	return (0);
3643 }
3644 
3645 static int
vcxgbe_detach(device_t dev)3646 vcxgbe_detach(device_t dev)
3647 {
3648 	struct vi_info *vi;
3649 	struct adapter *sc;
3650 
3651 	vi = device_get_softc(dev);
3652 	sc = vi->adapter;
3653 
3654 	begin_vi_detach(sc, vi);
3655 	cxgbe_vi_detach(vi);
3656 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3657 	end_vi_detach(sc, vi);
3658 
3659 	return (0);
3660 }
3661 
3662 static struct callout fatal_callout;
3663 static struct taskqueue *reset_tq;
3664 
3665 static void
delayed_panic(void * arg)3666 delayed_panic(void *arg)
3667 {
3668 	struct adapter *sc = arg;
3669 
3670 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3671 }
3672 
3673 static void
fatal_error_task(void * arg,int pending)3674 fatal_error_task(void *arg, int pending)
3675 {
3676 	struct adapter *sc = arg;
3677 	int rc;
3678 
3679 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3680 		dump_cim_regs(sc);
3681 		dump_cimla(sc);
3682 		dump_devlog(sc);
3683 	}
3684 
3685 	if (t4_reset_on_fatal_err) {
3686 		CH_ALERT(sc, "resetting adapter after fatal error.\n");
3687 		rc = reset_adapter(sc);
3688 		if (rc == 0 && t4_panic_on_fatal_err) {
3689 			CH_ALERT(sc, "reset was successful, "
3690 			    "system will NOT panic.\n");
3691 			return;
3692 		}
3693 	}
3694 
3695 	if (t4_panic_on_fatal_err) {
3696 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3697 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3698 	}
3699 }
3700 
3701 void
t4_fatal_err(struct adapter * sc,bool fw_error)3702 t4_fatal_err(struct adapter *sc, bool fw_error)
3703 {
3704 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3705 
3706 	stop_adapter(sc);
3707 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3708 		return;
3709 	if (fw_error) {
3710 		/*
3711 		 * We are here because of a firmware error/timeout and not
3712 		 * because of a hardware interrupt.  It is possible (although
3713 		 * not very likely) that an error interrupt was also raised but
3714 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3715 		 * main INT_CAUSE registers here to make sure we haven't missed
3716 		 * anything interesting.
3717 		 */
3718 		t4_slow_intr_handler(sc, verbose);
3719 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3720 	}
3721 	t4_report_fw_error(sc);
3722 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3723 	    device_get_nameunit(sc->dev), fw_error);
3724 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3725 }
3726 
3727 void
t4_add_adapter(struct adapter * sc)3728 t4_add_adapter(struct adapter *sc)
3729 {
3730 	sx_xlock(&t4_list_lock);
3731 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3732 	sx_xunlock(&t4_list_lock);
3733 }
3734 
3735 int
t4_map_bars_0_and_4(struct adapter * sc)3736 t4_map_bars_0_and_4(struct adapter *sc)
3737 {
3738 	sc->regs_rid = PCIR_BAR(0);
3739 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3740 	    &sc->regs_rid, RF_ACTIVE);
3741 	if (sc->regs_res == NULL) {
3742 		device_printf(sc->dev, "cannot map registers.\n");
3743 		return (ENXIO);
3744 	}
3745 	sc->bt = rman_get_bustag(sc->regs_res);
3746 	sc->bh = rman_get_bushandle(sc->regs_res);
3747 	sc->mmio_len = rman_get_size(sc->regs_res);
3748 	setbit(&sc->doorbells, DOORBELL_KDB);
3749 
3750 	sc->msix_rid = PCIR_BAR(4);
3751 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3752 	    &sc->msix_rid, RF_ACTIVE);
3753 	if (sc->msix_res == NULL) {
3754 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3755 		return (ENXIO);
3756 	}
3757 
3758 	return (0);
3759 }
3760 
3761 int
t4_map_bar_2(struct adapter * sc)3762 t4_map_bar_2(struct adapter *sc)
3763 {
3764 
3765 	/*
3766 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
3767 	 * to map it if RDMA is disabled.
3768 	 */
3769 	if (is_t4(sc) && sc->rdmacaps == 0)
3770 		return (0);
3771 
3772 	sc->udbs_rid = PCIR_BAR(2);
3773 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3774 	    &sc->udbs_rid, RF_ACTIVE);
3775 	if (sc->udbs_res == NULL) {
3776 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
3777 		return (ENXIO);
3778 	}
3779 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
3780 
3781 	if (chip_id(sc) >= CHELSIO_T5) {
3782 		setbit(&sc->doorbells, DOORBELL_UDB);
3783 #if defined(__i386__) || defined(__amd64__)
3784 		if (t5_write_combine) {
3785 			int rc, mode;
3786 
3787 			/*
3788 			 * Enable write combining on BAR2.  This is the
3789 			 * userspace doorbell BAR and is split into 128B
3790 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
3791 			 * with an egress queue.  The first 64B has the doorbell
3792 			 * and the second 64B can be used to submit a tx work
3793 			 * request with an implicit doorbell.
3794 			 */
3795 
3796 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
3797 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
3798 			if (rc == 0) {
3799 				clrbit(&sc->doorbells, DOORBELL_UDB);
3800 				setbit(&sc->doorbells, DOORBELL_WCWR);
3801 				setbit(&sc->doorbells, DOORBELL_UDBWC);
3802 			} else {
3803 				device_printf(sc->dev,
3804 				    "couldn't enable write combining: %d\n",
3805 				    rc);
3806 			}
3807 
3808 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
3809 			t4_write_reg(sc, A_SGE_STAT_CFG,
3810 			    V_STATSOURCE_T5(7) | mode);
3811 		}
3812 #endif
3813 	}
3814 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
3815 
3816 	return (0);
3817 }
3818 
3819 int
t4_adj_doorbells(struct adapter * sc)3820 t4_adj_doorbells(struct adapter *sc)
3821 {
3822 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
3823 		sc->doorbells &= t4_doorbells_allowed;
3824 		return (0);
3825 	}
3826 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
3827 	       sc->doorbells, t4_doorbells_allowed);
3828 	return (EINVAL);
3829 }
3830 
3831 struct memwin_init {
3832 	uint32_t base;
3833 	uint32_t aperture;
3834 };
3835 
3836 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
3837 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3838 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3839 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
3840 };
3841 
3842 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
3843 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3844 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3845 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
3846 };
3847 
3848 static void
setup_memwin(struct adapter * sc)3849 setup_memwin(struct adapter *sc)
3850 {
3851 	const struct memwin_init *mw_init;
3852 	struct memwin *mw;
3853 	int i;
3854 	uint32_t bar0;
3855 
3856 	if (is_t4(sc)) {
3857 		/*
3858 		 * Read low 32b of bar0 indirectly via the hardware backdoor
3859 		 * mechanism.  Works from within PCI passthrough environments
3860 		 * too, where rman_get_start() can return a different value.  We
3861 		 * need to program the T4 memory window decoders with the actual
3862 		 * addresses that will be coming across the PCIe link.
3863 		 */
3864 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
3865 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
3866 
3867 		mw_init = &t4_memwin[0];
3868 	} else {
3869 		/* T5+ use the relative offset inside the PCIe BAR */
3870 		bar0 = 0;
3871 
3872 		mw_init = &t5_memwin[0];
3873 	}
3874 
3875 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
3876 		if (!rw_initialized(&mw->mw_lock)) {
3877 			rw_init(&mw->mw_lock, "memory window access");
3878 			mw->mw_base = mw_init->base;
3879 			mw->mw_aperture = mw_init->aperture;
3880 			mw->mw_curpos = 0;
3881 		}
3882 		t4_write_reg(sc,
3883 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
3884 		    (mw->mw_base + bar0) | V_BIR(0) |
3885 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
3886 		rw_wlock(&mw->mw_lock);
3887 		position_memwin(sc, i, mw->mw_curpos);
3888 		rw_wunlock(&mw->mw_lock);
3889 	}
3890 
3891 	/* flush */
3892 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
3893 }
3894 
3895 /*
3896  * Positions the memory window at the given address in the card's address space.
3897  * There are some alignment requirements and the actual position may be at an
3898  * address prior to the requested address.  mw->mw_curpos always has the actual
3899  * position of the window.
3900  */
3901 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)3902 position_memwin(struct adapter *sc, int idx, uint32_t addr)
3903 {
3904 	struct memwin *mw;
3905 	uint32_t pf;
3906 	uint32_t reg;
3907 
3908 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3909 	mw = &sc->memwin[idx];
3910 	rw_assert(&mw->mw_lock, RA_WLOCKED);
3911 
3912 	if (is_t4(sc)) {
3913 		pf = 0;
3914 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
3915 	} else {
3916 		pf = V_PFNUM(sc->pf);
3917 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
3918 	}
3919 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
3920 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
3921 	t4_read_reg(sc, reg);	/* flush */
3922 }
3923 
3924 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)3925 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
3926     int len, int rw)
3927 {
3928 	struct memwin *mw;
3929 	uint32_t mw_end, v;
3930 
3931 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3932 
3933 	/* Memory can only be accessed in naturally aligned 4 byte units */
3934 	if (addr & 3 || len & 3 || len <= 0)
3935 		return (EINVAL);
3936 
3937 	mw = &sc->memwin[idx];
3938 	while (len > 0) {
3939 		rw_rlock(&mw->mw_lock);
3940 		mw_end = mw->mw_curpos + mw->mw_aperture;
3941 		if (addr >= mw_end || addr < mw->mw_curpos) {
3942 			/* Will need to reposition the window */
3943 			if (!rw_try_upgrade(&mw->mw_lock)) {
3944 				rw_runlock(&mw->mw_lock);
3945 				rw_wlock(&mw->mw_lock);
3946 			}
3947 			rw_assert(&mw->mw_lock, RA_WLOCKED);
3948 			position_memwin(sc, idx, addr);
3949 			rw_downgrade(&mw->mw_lock);
3950 			mw_end = mw->mw_curpos + mw->mw_aperture;
3951 		}
3952 		rw_assert(&mw->mw_lock, RA_RLOCKED);
3953 		while (addr < mw_end && len > 0) {
3954 			if (rw == 0) {
3955 				v = t4_read_reg(sc, mw->mw_base + addr -
3956 				    mw->mw_curpos);
3957 				*val++ = le32toh(v);
3958 			} else {
3959 				v = *val++;
3960 				t4_write_reg(sc, mw->mw_base + addr -
3961 				    mw->mw_curpos, htole32(v));
3962 			}
3963 			addr += 4;
3964 			len -= 4;
3965 		}
3966 		rw_runlock(&mw->mw_lock);
3967 	}
3968 
3969 	return (0);
3970 }
3971 
3972 CTASSERT(M_TID_COOKIE == M_COOKIE);
3973 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
3974 
3975 static void
t4_init_atid_table(struct adapter * sc)3976 t4_init_atid_table(struct adapter *sc)
3977 {
3978 	struct tid_info *t;
3979 	int i;
3980 
3981 	t = &sc->tids;
3982 	if (t->natids == 0)
3983 		return;
3984 
3985 	MPASS(t->atid_tab == NULL);
3986 
3987 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
3988 	    M_ZERO | M_WAITOK);
3989 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
3990 	t->afree = t->atid_tab;
3991 	t->atids_in_use = 0;
3992 	t->atid_alloc_stopped = false;
3993 	for (i = 1; i < t->natids; i++)
3994 		t->atid_tab[i - 1].next = &t->atid_tab[i];
3995 	t->atid_tab[t->natids - 1].next = NULL;
3996 }
3997 
3998 static void
t4_free_atid_table(struct adapter * sc)3999 t4_free_atid_table(struct adapter *sc)
4000 {
4001 	struct tid_info *t;
4002 
4003 	t = &sc->tids;
4004 
4005 	KASSERT(t->atids_in_use == 0,
4006 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4007 
4008 	if (mtx_initialized(&t->atid_lock))
4009 		mtx_destroy(&t->atid_lock);
4010 	free(t->atid_tab, M_CXGBE);
4011 	t->atid_tab = NULL;
4012 }
4013 
4014 static void
stop_atid_allocator(struct adapter * sc)4015 stop_atid_allocator(struct adapter *sc)
4016 {
4017 	struct tid_info *t = &sc->tids;
4018 
4019 	mtx_lock(&t->atid_lock);
4020 	t->atid_alloc_stopped = true;
4021 	mtx_unlock(&t->atid_lock);
4022 }
4023 
4024 static void
restart_atid_allocator(struct adapter * sc)4025 restart_atid_allocator(struct adapter *sc)
4026 {
4027 	struct tid_info *t = &sc->tids;
4028 
4029 	mtx_lock(&t->atid_lock);
4030 	KASSERT(t->atids_in_use == 0,
4031 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4032 	t->atid_alloc_stopped = false;
4033 	mtx_unlock(&t->atid_lock);
4034 }
4035 
4036 int
alloc_atid(struct adapter * sc,void * ctx)4037 alloc_atid(struct adapter *sc, void *ctx)
4038 {
4039 	struct tid_info *t = &sc->tids;
4040 	int atid = -1;
4041 
4042 	mtx_lock(&t->atid_lock);
4043 	if (t->afree && !t->atid_alloc_stopped) {
4044 		union aopen_entry *p = t->afree;
4045 
4046 		atid = p - t->atid_tab;
4047 		MPASS(atid <= M_TID_TID);
4048 		t->afree = p->next;
4049 		p->data = ctx;
4050 		t->atids_in_use++;
4051 	}
4052 	mtx_unlock(&t->atid_lock);
4053 	return (atid);
4054 }
4055 
4056 void *
lookup_atid(struct adapter * sc,int atid)4057 lookup_atid(struct adapter *sc, int atid)
4058 {
4059 	struct tid_info *t = &sc->tids;
4060 
4061 	return (t->atid_tab[atid].data);
4062 }
4063 
4064 void
free_atid(struct adapter * sc,int atid)4065 free_atid(struct adapter *sc, int atid)
4066 {
4067 	struct tid_info *t = &sc->tids;
4068 	union aopen_entry *p = &t->atid_tab[atid];
4069 
4070 	mtx_lock(&t->atid_lock);
4071 	p->next = t->afree;
4072 	t->afree = p;
4073 	t->atids_in_use--;
4074 	mtx_unlock(&t->atid_lock);
4075 }
4076 
4077 static void
queue_tid_release(struct adapter * sc,int tid)4078 queue_tid_release(struct adapter *sc, int tid)
4079 {
4080 
4081 	CXGBE_UNIMPLEMENTED("deferred tid release");
4082 }
4083 
4084 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4085 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4086 {
4087 	struct wrqe *wr;
4088 	struct cpl_tid_release *req;
4089 
4090 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4091 	if (wr == NULL) {
4092 		queue_tid_release(sc, tid);	/* defer */
4093 		return;
4094 	}
4095 	req = wrtod(wr);
4096 
4097 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4098 
4099 	t4_wrq_tx(sc, wr);
4100 }
4101 
4102 static int
t4_range_cmp(const void * a,const void * b)4103 t4_range_cmp(const void *a, const void *b)
4104 {
4105 	return ((const struct t4_range *)a)->start -
4106 	       ((const struct t4_range *)b)->start;
4107 }
4108 
4109 /*
4110  * Verify that the memory range specified by the addr/len pair is valid within
4111  * the card's address space.
4112  */
4113 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4114 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4115 {
4116 	struct t4_range mem_ranges[4], *r, *next;
4117 	uint32_t em, addr_len;
4118 	int i, n, remaining;
4119 
4120 	/* Memory can only be accessed in naturally aligned 4 byte units */
4121 	if (addr & 3 || len & 3 || len == 0)
4122 		return (EINVAL);
4123 
4124 	/* Enabled memories */
4125 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4126 
4127 	r = &mem_ranges[0];
4128 	n = 0;
4129 	bzero(r, sizeof(mem_ranges));
4130 	if (em & F_EDRAM0_ENABLE) {
4131 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4132 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4133 		if (r->size > 0) {
4134 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4135 			if (addr >= r->start &&
4136 			    addr + len <= r->start + r->size)
4137 				return (0);
4138 			r++;
4139 			n++;
4140 		}
4141 	}
4142 	if (em & F_EDRAM1_ENABLE) {
4143 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4144 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4145 		if (r->size > 0) {
4146 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4147 			if (addr >= r->start &&
4148 			    addr + len <= r->start + r->size)
4149 				return (0);
4150 			r++;
4151 			n++;
4152 		}
4153 	}
4154 	if (em & F_EXT_MEM_ENABLE) {
4155 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4156 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4157 		if (r->size > 0) {
4158 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4159 			if (addr >= r->start &&
4160 			    addr + len <= r->start + r->size)
4161 				return (0);
4162 			r++;
4163 			n++;
4164 		}
4165 	}
4166 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4167 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4168 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4169 		if (r->size > 0) {
4170 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4171 			if (addr >= r->start &&
4172 			    addr + len <= r->start + r->size)
4173 				return (0);
4174 			r++;
4175 			n++;
4176 		}
4177 	}
4178 	MPASS(n <= nitems(mem_ranges));
4179 
4180 	if (n > 1) {
4181 		/* Sort and merge the ranges. */
4182 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4183 
4184 		/* Start from index 0 and examine the next n - 1 entries. */
4185 		r = &mem_ranges[0];
4186 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4187 
4188 			MPASS(r->size > 0);	/* r is a valid entry. */
4189 			next = r + 1;
4190 			MPASS(next->size > 0);	/* and so is the next one. */
4191 
4192 			while (r->start + r->size >= next->start) {
4193 				/* Merge the next one into the current entry. */
4194 				r->size = max(r->start + r->size,
4195 				    next->start + next->size) - r->start;
4196 				n--;	/* One fewer entry in total. */
4197 				if (--remaining == 0)
4198 					goto done;	/* short circuit */
4199 				next++;
4200 			}
4201 			if (next != r + 1) {
4202 				/*
4203 				 * Some entries were merged into r and next
4204 				 * points to the first valid entry that couldn't
4205 				 * be merged.
4206 				 */
4207 				MPASS(next->size > 0);	/* must be valid */
4208 				memcpy(r + 1, next, remaining * sizeof(*r));
4209 #ifdef INVARIANTS
4210 				/*
4211 				 * This so that the foo->size assertion in the
4212 				 * next iteration of the loop do the right
4213 				 * thing for entries that were pulled up and are
4214 				 * no longer valid.
4215 				 */
4216 				MPASS(n < nitems(mem_ranges));
4217 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4218 				    sizeof(struct t4_range));
4219 #endif
4220 			}
4221 		}
4222 done:
4223 		/* Done merging the ranges. */
4224 		MPASS(n > 0);
4225 		r = &mem_ranges[0];
4226 		for (i = 0; i < n; i++, r++) {
4227 			if (addr >= r->start &&
4228 			    addr + len <= r->start + r->size)
4229 				return (0);
4230 		}
4231 	}
4232 
4233 	return (EFAULT);
4234 }
4235 
4236 static int
fwmtype_to_hwmtype(int mtype)4237 fwmtype_to_hwmtype(int mtype)
4238 {
4239 
4240 	switch (mtype) {
4241 	case FW_MEMTYPE_EDC0:
4242 		return (MEM_EDC0);
4243 	case FW_MEMTYPE_EDC1:
4244 		return (MEM_EDC1);
4245 	case FW_MEMTYPE_EXTMEM:
4246 		return (MEM_MC0);
4247 	case FW_MEMTYPE_EXTMEM1:
4248 		return (MEM_MC1);
4249 	default:
4250 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4251 	}
4252 }
4253 
4254 /*
4255  * Verify that the memory range specified by the memtype/offset/len pair is
4256  * valid and lies entirely within the memtype specified.  The global address of
4257  * the start of the range is returned in addr.
4258  */
4259 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4260 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4261     uint32_t *addr)
4262 {
4263 	uint32_t em, addr_len, maddr;
4264 
4265 	/* Memory can only be accessed in naturally aligned 4 byte units */
4266 	if (off & 3 || len & 3 || len == 0)
4267 		return (EINVAL);
4268 
4269 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4270 	switch (fwmtype_to_hwmtype(mtype)) {
4271 	case MEM_EDC0:
4272 		if (!(em & F_EDRAM0_ENABLE))
4273 			return (EINVAL);
4274 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4275 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4276 		break;
4277 	case MEM_EDC1:
4278 		if (!(em & F_EDRAM1_ENABLE))
4279 			return (EINVAL);
4280 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4281 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4282 		break;
4283 	case MEM_MC:
4284 		if (!(em & F_EXT_MEM_ENABLE))
4285 			return (EINVAL);
4286 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4287 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4288 		break;
4289 	case MEM_MC1:
4290 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4291 			return (EINVAL);
4292 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4293 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4294 		break;
4295 	default:
4296 		return (EINVAL);
4297 	}
4298 
4299 	*addr = maddr + off;	/* global address */
4300 	return (validate_mem_range(sc, *addr, len));
4301 }
4302 
4303 static int
fixup_devlog_params(struct adapter * sc)4304 fixup_devlog_params(struct adapter *sc)
4305 {
4306 	struct devlog_params *dparams = &sc->params.devlog;
4307 	int rc;
4308 
4309 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4310 	    dparams->size, &dparams->addr);
4311 
4312 	return (rc);
4313 }
4314 
4315 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4316 update_nirq(struct intrs_and_queues *iaq, int nports)
4317 {
4318 
4319 	iaq->nirq = T4_EXTRA_INTR;
4320 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4321 	iaq->nirq += nports * iaq->nofldrxq;
4322 	iaq->nirq += nports * (iaq->num_vis - 1) *
4323 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4324 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4325 }
4326 
4327 /*
4328  * Adjust requirements to fit the number of interrupts available.
4329  */
4330 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4331 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4332     int navail)
4333 {
4334 	int old_nirq;
4335 	const int nports = sc->params.nports;
4336 
4337 	MPASS(nports > 0);
4338 	MPASS(navail > 0);
4339 
4340 	bzero(iaq, sizeof(*iaq));
4341 	iaq->intr_type = itype;
4342 	iaq->num_vis = t4_num_vis;
4343 	iaq->ntxq = t4_ntxq;
4344 	iaq->ntxq_vi = t4_ntxq_vi;
4345 	iaq->nrxq = t4_nrxq;
4346 	iaq->nrxq_vi = t4_nrxq_vi;
4347 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4348 	if (is_offload(sc) || is_ethoffload(sc)) {
4349 		iaq->nofldtxq = t4_nofldtxq;
4350 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
4351 	}
4352 #endif
4353 #ifdef TCP_OFFLOAD
4354 	if (is_offload(sc)) {
4355 		iaq->nofldrxq = t4_nofldrxq;
4356 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4357 	}
4358 #endif
4359 #ifdef DEV_NETMAP
4360 	if (t4_native_netmap & NN_MAIN_VI) {
4361 		iaq->nnmtxq = t4_nnmtxq;
4362 		iaq->nnmrxq = t4_nnmrxq;
4363 	}
4364 	if (t4_native_netmap & NN_EXTRA_VI) {
4365 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4366 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4367 	}
4368 #endif
4369 
4370 	update_nirq(iaq, nports);
4371 	if (iaq->nirq <= navail &&
4372 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4373 		/*
4374 		 * This is the normal case -- there are enough interrupts for
4375 		 * everything.
4376 		 */
4377 		goto done;
4378 	}
4379 
4380 	/*
4381 	 * If extra VIs have been configured try reducing their count and see if
4382 	 * that works.
4383 	 */
4384 	while (iaq->num_vis > 1) {
4385 		iaq->num_vis--;
4386 		update_nirq(iaq, nports);
4387 		if (iaq->nirq <= navail &&
4388 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4389 			device_printf(sc->dev, "virtual interfaces per port "
4390 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4391 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4392 			    "itype %d, navail %u, nirq %d.\n",
4393 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4394 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4395 			    itype, navail, iaq->nirq);
4396 			goto done;
4397 		}
4398 	}
4399 
4400 	/*
4401 	 * Extra VIs will not be created.  Log a message if they were requested.
4402 	 */
4403 	MPASS(iaq->num_vis == 1);
4404 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4405 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4406 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4407 	if (iaq->num_vis != t4_num_vis) {
4408 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4409 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4410 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4411 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4412 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4413 	}
4414 
4415 	/*
4416 	 * Keep reducing the number of NIC rx queues to the next lower power of
4417 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4418 	 * if that works.
4419 	 */
4420 	do {
4421 		if (iaq->nrxq > 1) {
4422 			iaq->nrxq = rounddown_pow_of_two(iaq->nrxq - 1);
4423 			if (iaq->nnmrxq > iaq->nrxq)
4424 				iaq->nnmrxq = iaq->nrxq;
4425 		}
4426 		if (iaq->nofldrxq > 1)
4427 			iaq->nofldrxq >>= 1;
4428 
4429 		old_nirq = iaq->nirq;
4430 		update_nirq(iaq, nports);
4431 		if (iaq->nirq <= navail &&
4432 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4433 			device_printf(sc->dev, "running with reduced number of "
4434 			    "rx queues because of shortage of interrupts.  "
4435 			    "nrxq=%u, nofldrxq=%u.  "
4436 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4437 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4438 			goto done;
4439 		}
4440 	} while (old_nirq != iaq->nirq);
4441 
4442 	/* One interrupt for everything.  Ugh. */
4443 	device_printf(sc->dev, "running with minimal number of queues.  "
4444 	    "itype %d, navail %u.\n", itype, navail);
4445 	iaq->nirq = 1;
4446 	iaq->nrxq = 1;
4447 	iaq->ntxq = 1;
4448 	if (iaq->nofldrxq > 0) {
4449 		iaq->nofldrxq = 1;
4450 		iaq->nofldtxq = 1;
4451 	}
4452 	iaq->nnmtxq = 0;
4453 	iaq->nnmrxq = 0;
4454 done:
4455 	MPASS(iaq->num_vis > 0);
4456 	if (iaq->num_vis > 1) {
4457 		MPASS(iaq->nrxq_vi > 0);
4458 		MPASS(iaq->ntxq_vi > 0);
4459 	}
4460 	MPASS(iaq->nirq > 0);
4461 	MPASS(iaq->nrxq > 0);
4462 	MPASS(iaq->ntxq > 0);
4463 	if (itype == INTR_MSI) {
4464 		MPASS(powerof2(iaq->nirq));
4465 	}
4466 }
4467 
4468 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4469 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4470 {
4471 	int rc, itype, navail, nalloc;
4472 
4473 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4474 
4475 		if ((itype & t4_intr_types) == 0)
4476 			continue;	/* not allowed */
4477 
4478 		if (itype == INTR_MSIX)
4479 			navail = pci_msix_count(sc->dev);
4480 		else if (itype == INTR_MSI)
4481 			navail = pci_msi_count(sc->dev);
4482 		else
4483 			navail = 1;
4484 restart:
4485 		if (navail == 0)
4486 			continue;
4487 
4488 		calculate_iaq(sc, iaq, itype, navail);
4489 		nalloc = iaq->nirq;
4490 		rc = 0;
4491 		if (itype == INTR_MSIX)
4492 			rc = pci_alloc_msix(sc->dev, &nalloc);
4493 		else if (itype == INTR_MSI)
4494 			rc = pci_alloc_msi(sc->dev, &nalloc);
4495 
4496 		if (rc == 0 && nalloc > 0) {
4497 			if (nalloc == iaq->nirq)
4498 				return (0);
4499 
4500 			/*
4501 			 * Didn't get the number requested.  Use whatever number
4502 			 * the kernel is willing to allocate.
4503 			 */
4504 			device_printf(sc->dev, "fewer vectors than requested, "
4505 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4506 			    itype, iaq->nirq, nalloc);
4507 			pci_release_msi(sc->dev);
4508 			navail = nalloc;
4509 			goto restart;
4510 		}
4511 
4512 		device_printf(sc->dev,
4513 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4514 		    itype, rc, iaq->nirq, nalloc);
4515 	}
4516 
4517 	device_printf(sc->dev,
4518 	    "failed to find a usable interrupt type.  "
4519 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4520 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4521 
4522 	return (ENXIO);
4523 }
4524 
4525 #define FW_VERSION(chip) ( \
4526     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4527     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4528     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4529     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4530 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4531 
4532 /* Just enough of fw_hdr to cover all version info. */
4533 struct fw_h {
4534 	__u8	ver;
4535 	__u8	chip;
4536 	__be16	len512;
4537 	__be32	fw_ver;
4538 	__be32	tp_microcode_ver;
4539 	__u8	intfver_nic;
4540 	__u8	intfver_vnic;
4541 	__u8	intfver_ofld;
4542 	__u8	intfver_ri;
4543 	__u8	intfver_iscsipdu;
4544 	__u8	intfver_iscsi;
4545 	__u8	intfver_fcoepdu;
4546 	__u8	intfver_fcoe;
4547 };
4548 /* Spot check a couple of fields. */
4549 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4550 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4551 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4552 
4553 struct fw_info {
4554 	uint8_t chip;
4555 	char *kld_name;
4556 	char *fw_mod_name;
4557 	struct fw_h fw_h;
4558 } fw_info[] = {
4559 	{
4560 		.chip = CHELSIO_T4,
4561 		.kld_name = "t4fw_cfg",
4562 		.fw_mod_name = "t4fw",
4563 		.fw_h = {
4564 			.chip = FW_HDR_CHIP_T4,
4565 			.fw_ver = htobe32(FW_VERSION(T4)),
4566 			.intfver_nic = FW_INTFVER(T4, NIC),
4567 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4568 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4569 			.intfver_ri = FW_INTFVER(T4, RI),
4570 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4571 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4572 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4573 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4574 		},
4575 	}, {
4576 		.chip = CHELSIO_T5,
4577 		.kld_name = "t5fw_cfg",
4578 		.fw_mod_name = "t5fw",
4579 		.fw_h = {
4580 			.chip = FW_HDR_CHIP_T5,
4581 			.fw_ver = htobe32(FW_VERSION(T5)),
4582 			.intfver_nic = FW_INTFVER(T5, NIC),
4583 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4584 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4585 			.intfver_ri = FW_INTFVER(T5, RI),
4586 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4587 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4588 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4589 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4590 		},
4591 	}, {
4592 		.chip = CHELSIO_T6,
4593 		.kld_name = "t6fw_cfg",
4594 		.fw_mod_name = "t6fw",
4595 		.fw_h = {
4596 			.chip = FW_HDR_CHIP_T6,
4597 			.fw_ver = htobe32(FW_VERSION(T6)),
4598 			.intfver_nic = FW_INTFVER(T6, NIC),
4599 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4600 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4601 			.intfver_ri = FW_INTFVER(T6, RI),
4602 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4603 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4604 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4605 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4606 		},
4607 	}
4608 };
4609 
4610 static struct fw_info *
find_fw_info(int chip)4611 find_fw_info(int chip)
4612 {
4613 	int i;
4614 
4615 	for (i = 0; i < nitems(fw_info); i++) {
4616 		if (fw_info[i].chip == chip)
4617 			return (&fw_info[i]);
4618 	}
4619 	return (NULL);
4620 }
4621 
4622 /*
4623  * Is the given firmware API compatible with the one the driver was compiled
4624  * with?
4625  */
4626 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4627 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4628 {
4629 
4630 	/* short circuit if it's the exact same firmware version */
4631 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4632 		return (1);
4633 
4634 	/*
4635 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4636 	 * features that are supported in the driver.
4637 	 */
4638 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4639 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4640 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4641 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4642 		return (1);
4643 #undef SAME_INTF
4644 
4645 	return (0);
4646 }
4647 
4648 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4649 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4650     const struct firmware **fw)
4651 {
4652 	struct fw_info *fw_info;
4653 
4654 	*dcfg = NULL;
4655 	if (fw != NULL)
4656 		*fw = NULL;
4657 
4658 	fw_info = find_fw_info(chip_id(sc));
4659 	if (fw_info == NULL) {
4660 		device_printf(sc->dev,
4661 		    "unable to look up firmware information for chip %d.\n",
4662 		    chip_id(sc));
4663 		return (EINVAL);
4664 	}
4665 
4666 	*dcfg = firmware_get(fw_info->kld_name);
4667 	if (*dcfg != NULL) {
4668 		if (fw != NULL)
4669 			*fw = firmware_get(fw_info->fw_mod_name);
4670 		return (0);
4671 	}
4672 
4673 	return (ENOENT);
4674 }
4675 
4676 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4677 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4678     const struct firmware *fw)
4679 {
4680 
4681 	if (fw != NULL)
4682 		firmware_put(fw, FIRMWARE_UNLOAD);
4683 	if (dcfg != NULL)
4684 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4685 }
4686 
4687 /*
4688  * Return values:
4689  * 0 means no firmware install attempted.
4690  * ERESTART means a firmware install was attempted and was successful.
4691  * +ve errno means a firmware install was attempted but failed.
4692  */
4693 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4694 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4695     const struct fw_h *drv_fw, const char *reason, int *already)
4696 {
4697 	const struct firmware *cfg, *fw;
4698 	const uint32_t c = be32toh(card_fw->fw_ver);
4699 	uint32_t d, k;
4700 	int rc, fw_install;
4701 	struct fw_h bundled_fw;
4702 	bool load_attempted;
4703 
4704 	cfg = fw = NULL;
4705 	load_attempted = false;
4706 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4707 
4708 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4709 	if (t4_fw_install < 0) {
4710 		rc = load_fw_module(sc, &cfg, &fw);
4711 		if (rc != 0 || fw == NULL) {
4712 			device_printf(sc->dev,
4713 			    "failed to load firmware module: %d. cfg %p, fw %p;"
4714 			    " will use compiled-in firmware version for"
4715 			    "hw.cxgbe.fw_install checks.\n",
4716 			    rc, cfg, fw);
4717 		} else {
4718 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
4719 		}
4720 		load_attempted = true;
4721 	}
4722 	d = be32toh(bundled_fw.fw_ver);
4723 
4724 	if (reason != NULL)
4725 		goto install;
4726 
4727 	if ((sc->flags & FW_OK) == 0) {
4728 
4729 		if (c == 0xffffffff) {
4730 			reason = "missing";
4731 			goto install;
4732 		}
4733 
4734 		rc = 0;
4735 		goto done;
4736 	}
4737 
4738 	if (!fw_compatible(card_fw, &bundled_fw)) {
4739 		reason = "incompatible or unusable";
4740 		goto install;
4741 	}
4742 
4743 	if (d > c) {
4744 		reason = "older than the version bundled with this driver";
4745 		goto install;
4746 	}
4747 
4748 	if (fw_install == 2 && d != c) {
4749 		reason = "different than the version bundled with this driver";
4750 		goto install;
4751 	}
4752 
4753 	/* No reason to do anything to the firmware already on the card. */
4754 	rc = 0;
4755 	goto done;
4756 
4757 install:
4758 	rc = 0;
4759 	if ((*already)++)
4760 		goto done;
4761 
4762 	if (fw_install == 0) {
4763 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4764 		    "but the driver is prohibited from installing a firmware "
4765 		    "on the card.\n",
4766 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4767 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4768 
4769 		goto done;
4770 	}
4771 
4772 	/*
4773 	 * We'll attempt to install a firmware.  Load the module first (if it
4774 	 * hasn't been loaded already).
4775 	 */
4776 	if (!load_attempted) {
4777 		rc = load_fw_module(sc, &cfg, &fw);
4778 		if (rc != 0 || fw == NULL) {
4779 			device_printf(sc->dev,
4780 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
4781 			    rc, cfg, fw);
4782 			/* carry on */
4783 		}
4784 	}
4785 	if (fw == NULL) {
4786 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4787 		    "but the driver cannot take corrective action because it "
4788 		    "is unable to load the firmware module.\n",
4789 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4790 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4791 		rc = sc->flags & FW_OK ? 0 : ENOENT;
4792 		goto done;
4793 	}
4794 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
4795 	if (k != d) {
4796 		MPASS(t4_fw_install > 0);
4797 		device_printf(sc->dev,
4798 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
4799 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
4800 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
4801 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
4802 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4803 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4804 		rc = sc->flags & FW_OK ? 0 : EINVAL;
4805 		goto done;
4806 	}
4807 
4808 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4809 	    "installing firmware %u.%u.%u.%u on card.\n",
4810 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4811 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
4812 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4813 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4814 
4815 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
4816 	if (rc != 0) {
4817 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
4818 	} else {
4819 		/* Installed successfully, update the cached header too. */
4820 		rc = ERESTART;
4821 		memcpy(card_fw, fw->data, sizeof(*card_fw));
4822 	}
4823 done:
4824 	unload_fw_module(sc, cfg, fw);
4825 
4826 	return (rc);
4827 }
4828 
4829 /*
4830  * Establish contact with the firmware and attempt to become the master driver.
4831  *
4832  * A firmware will be installed to the card if needed (if the driver is allowed
4833  * to do so).
4834  */
4835 static int
contact_firmware(struct adapter * sc)4836 contact_firmware(struct adapter *sc)
4837 {
4838 	int rc, already = 0;
4839 	enum dev_state state;
4840 	struct fw_info *fw_info;
4841 	struct fw_hdr *card_fw;		/* fw on the card */
4842 	const struct fw_h *drv_fw;
4843 
4844 	fw_info = find_fw_info(chip_id(sc));
4845 	if (fw_info == NULL) {
4846 		device_printf(sc->dev,
4847 		    "unable to look up firmware information for chip %d.\n",
4848 		    chip_id(sc));
4849 		return (EINVAL);
4850 	}
4851 	drv_fw = &fw_info->fw_h;
4852 
4853 	/* Read the header of the firmware on the card */
4854 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
4855 restart:
4856 	rc = -t4_get_fw_hdr(sc, card_fw);
4857 	if (rc != 0) {
4858 		device_printf(sc->dev,
4859 		    "unable to read firmware header from card's flash: %d\n",
4860 		    rc);
4861 		goto done;
4862 	}
4863 
4864 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
4865 	    &already);
4866 	if (rc == ERESTART)
4867 		goto restart;
4868 	if (rc != 0)
4869 		goto done;
4870 
4871 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
4872 	if (rc < 0 || state == DEV_STATE_ERR) {
4873 		rc = -rc;
4874 		device_printf(sc->dev,
4875 		    "failed to connect to the firmware: %d, %d.  "
4876 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4877 #if 0
4878 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4879 		    "not responding properly to HELLO", &already) == ERESTART)
4880 			goto restart;
4881 #endif
4882 		goto done;
4883 	}
4884 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
4885 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
4886 
4887 	if (rc == sc->pf) {
4888 		sc->flags |= MASTER_PF;
4889 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4890 		    NULL, &already);
4891 		if (rc == ERESTART)
4892 			rc = 0;
4893 		else if (rc != 0)
4894 			goto done;
4895 	} else if (state == DEV_STATE_UNINIT) {
4896 		/*
4897 		 * We didn't get to be the master so we definitely won't be
4898 		 * configuring the chip.  It's a bug if someone else hasn't
4899 		 * configured it already.
4900 		 */
4901 		device_printf(sc->dev, "couldn't be master(%d), "
4902 		    "device not already initialized either(%d).  "
4903 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4904 		rc = EPROTO;
4905 		goto done;
4906 	} else {
4907 		/*
4908 		 * Some other PF is the master and has configured the chip.
4909 		 * This is allowed but untested.
4910 		 */
4911 		device_printf(sc->dev, "PF%d is master, device state %d.  "
4912 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4913 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
4914 		sc->cfcsum = 0;
4915 		rc = 0;
4916 	}
4917 done:
4918 	if (rc != 0 && sc->flags & FW_OK) {
4919 		t4_fw_bye(sc, sc->mbox);
4920 		sc->flags &= ~FW_OK;
4921 	}
4922 	free(card_fw, M_CXGBE);
4923 	return (rc);
4924 }
4925 
4926 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff)4927 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
4928     uint32_t mtype, uint32_t moff)
4929 {
4930 	struct fw_info *fw_info;
4931 	const struct firmware *dcfg, *rcfg = NULL;
4932 	const uint32_t *cfdata;
4933 	uint32_t cflen, addr;
4934 	int rc;
4935 
4936 	load_fw_module(sc, &dcfg, NULL);
4937 
4938 	/* Card specific interpretation of "default". */
4939 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4940 		if (pci_get_device(sc->dev) == 0x440a)
4941 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
4942 		if (is_fpga(sc))
4943 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
4944 	}
4945 
4946 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4947 		if (dcfg == NULL) {
4948 			device_printf(sc->dev,
4949 			    "KLD with default config is not available.\n");
4950 			rc = ENOENT;
4951 			goto done;
4952 		}
4953 		cfdata = dcfg->data;
4954 		cflen = dcfg->datasize & ~3;
4955 	} else {
4956 		char s[32];
4957 
4958 		fw_info = find_fw_info(chip_id(sc));
4959 		if (fw_info == NULL) {
4960 			device_printf(sc->dev,
4961 			    "unable to look up firmware information for chip %d.\n",
4962 			    chip_id(sc));
4963 			rc = EINVAL;
4964 			goto done;
4965 		}
4966 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
4967 
4968 		rcfg = firmware_get(s);
4969 		if (rcfg == NULL) {
4970 			device_printf(sc->dev,
4971 			    "unable to load module \"%s\" for configuration "
4972 			    "profile \"%s\".\n", s, cfg_file);
4973 			rc = ENOENT;
4974 			goto done;
4975 		}
4976 		cfdata = rcfg->data;
4977 		cflen = rcfg->datasize & ~3;
4978 	}
4979 
4980 	if (cflen > FLASH_CFG_MAX_SIZE) {
4981 		device_printf(sc->dev,
4982 		    "config file too long (%d, max allowed is %d).\n",
4983 		    cflen, FLASH_CFG_MAX_SIZE);
4984 		rc = EINVAL;
4985 		goto done;
4986 	}
4987 
4988 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
4989 	if (rc != 0) {
4990 		device_printf(sc->dev,
4991 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
4992 		    __func__, mtype, moff, cflen, rc);
4993 		rc = EINVAL;
4994 		goto done;
4995 	}
4996 	write_via_memwin(sc, 2, addr, cfdata, cflen);
4997 done:
4998 	if (rcfg != NULL)
4999 		firmware_put(rcfg, FIRMWARE_UNLOAD);
5000 	unload_fw_module(sc, dcfg, NULL);
5001 	return (rc);
5002 }
5003 
5004 struct caps_allowed {
5005 	uint16_t nbmcaps;
5006 	uint16_t linkcaps;
5007 	uint16_t switchcaps;
5008 	uint16_t niccaps;
5009 	uint16_t toecaps;
5010 	uint16_t rdmacaps;
5011 	uint16_t cryptocaps;
5012 	uint16_t iscsicaps;
5013 	uint16_t fcoecaps;
5014 };
5015 
5016 #define FW_PARAM_DEV(param) \
5017 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5018 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5019 #define FW_PARAM_PFVF(param) \
5020 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5021 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5022 
5023 /*
5024  * Provide a configuration profile to the firmware and have it initialize the
5025  * chip accordingly.  This may involve uploading a configuration file to the
5026  * card.
5027  */
5028 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5029 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5030     const struct caps_allowed *caps_allowed)
5031 {
5032 	int rc;
5033 	struct fw_caps_config_cmd caps;
5034 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5035 
5036 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5037 	if (rc != 0) {
5038 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5039 		return (rc);
5040 	}
5041 
5042 	bzero(&caps, sizeof(caps));
5043 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5044 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5045 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5046 		mtype = 0;
5047 		moff = 0;
5048 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5049 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5050 		mtype = FW_MEMTYPE_FLASH;
5051 		moff = t4_flash_cfg_addr(sc);
5052 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5053 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5054 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5055 		    FW_LEN16(caps));
5056 	} else {
5057 		/*
5058 		 * Ask the firmware where it wants us to upload the config file.
5059 		 */
5060 		param = FW_PARAM_DEV(CF);
5061 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5062 		if (rc != 0) {
5063 			/* No support for config file?  Shouldn't happen. */
5064 			device_printf(sc->dev,
5065 			    "failed to query config file location: %d.\n", rc);
5066 			goto done;
5067 		}
5068 		mtype = G_FW_PARAMS_PARAM_Y(val);
5069 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5070 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5071 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5072 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5073 		    FW_LEN16(caps));
5074 
5075 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
5076 		if (rc != 0) {
5077 			device_printf(sc->dev,
5078 			    "failed to upload config file to card: %d.\n", rc);
5079 			goto done;
5080 		}
5081 	}
5082 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5083 	if (rc != 0) {
5084 		device_printf(sc->dev, "failed to pre-process config file: %d "
5085 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5086 		goto done;
5087 	}
5088 
5089 	finicsum = be32toh(caps.finicsum);
5090 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5091 	if (finicsum != cfcsum) {
5092 		device_printf(sc->dev,
5093 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5094 		    finicsum, cfcsum);
5095 	}
5096 	sc->cfcsum = cfcsum;
5097 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5098 
5099 	/*
5100 	 * Let the firmware know what features will (not) be used so it can tune
5101 	 * things accordingly.
5102 	 */
5103 #define LIMIT_CAPS(x) do { \
5104 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5105 } while (0)
5106 	LIMIT_CAPS(nbm);
5107 	LIMIT_CAPS(link);
5108 	LIMIT_CAPS(switch);
5109 	LIMIT_CAPS(nic);
5110 	LIMIT_CAPS(toe);
5111 	LIMIT_CAPS(rdma);
5112 	LIMIT_CAPS(crypto);
5113 	LIMIT_CAPS(iscsi);
5114 	LIMIT_CAPS(fcoe);
5115 #undef LIMIT_CAPS
5116 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5117 		/*
5118 		 * TOE and hashfilters are mutually exclusive.  It is a config
5119 		 * file or firmware bug if both are reported as available.  Try
5120 		 * to cope with the situation in non-debug builds by disabling
5121 		 * TOE.
5122 		 */
5123 		MPASS(caps.toecaps == 0);
5124 
5125 		caps.toecaps = 0;
5126 		caps.rdmacaps = 0;
5127 		caps.iscsicaps = 0;
5128 	}
5129 
5130 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5131 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5132 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5133 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5134 	if (rc != 0) {
5135 		device_printf(sc->dev,
5136 		    "failed to process config file: %d.\n", rc);
5137 		goto done;
5138 	}
5139 
5140 	t4_tweak_chip_settings(sc);
5141 	set_params__pre_init(sc);
5142 
5143 	/* get basic stuff going */
5144 	rc = -t4_fw_initialize(sc, sc->mbox);
5145 	if (rc != 0) {
5146 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5147 		goto done;
5148 	}
5149 done:
5150 	return (rc);
5151 }
5152 
5153 /*
5154  * Partition chip resources for use between various PFs, VFs, etc.
5155  */
5156 static int
partition_resources(struct adapter * sc)5157 partition_resources(struct adapter *sc)
5158 {
5159 	char cfg_file[sizeof(t4_cfg_file)];
5160 	struct caps_allowed caps_allowed;
5161 	int rc;
5162 	bool fallback;
5163 
5164 	/* Only the master driver gets to configure the chip resources. */
5165 	MPASS(sc->flags & MASTER_PF);
5166 
5167 #define COPY_CAPS(x) do { \
5168 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5169 } while (0)
5170 	bzero(&caps_allowed, sizeof(caps_allowed));
5171 	COPY_CAPS(nbm);
5172 	COPY_CAPS(link);
5173 	COPY_CAPS(switch);
5174 	COPY_CAPS(nic);
5175 	COPY_CAPS(toe);
5176 	COPY_CAPS(rdma);
5177 	COPY_CAPS(crypto);
5178 	COPY_CAPS(iscsi);
5179 	COPY_CAPS(fcoe);
5180 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5181 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5182 retry:
5183 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5184 	if (rc != 0 && fallback) {
5185 		dump_devlog(sc);
5186 		device_printf(sc->dev,
5187 		    "failed (%d) to configure card with \"%s\" profile, "
5188 		    "will fall back to a basic configuration and retry.\n",
5189 		    rc, cfg_file);
5190 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5191 		bzero(&caps_allowed, sizeof(caps_allowed));
5192 		COPY_CAPS(switch);
5193 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5194 		fallback = false;
5195 		goto retry;
5196 	}
5197 #undef COPY_CAPS
5198 	return (rc);
5199 }
5200 
5201 /*
5202  * Retrieve parameters that are needed (or nice to have) very early.
5203  */
5204 static int
get_params__pre_init(struct adapter * sc)5205 get_params__pre_init(struct adapter *sc)
5206 {
5207 	int rc;
5208 	uint32_t param[2], val[2];
5209 
5210 	t4_get_version_info(sc);
5211 
5212 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5213 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5214 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5215 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5216 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5217 
5218 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5219 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5220 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5221 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5222 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5223 
5224 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5225 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5226 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5227 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5228 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5229 
5230 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5231 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5232 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5233 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5234 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5235 
5236 	param[0] = FW_PARAM_DEV(PORTVEC);
5237 	param[1] = FW_PARAM_DEV(CCLK);
5238 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5239 	if (rc != 0) {
5240 		device_printf(sc->dev,
5241 		    "failed to query parameters (pre_init): %d.\n", rc);
5242 		return (rc);
5243 	}
5244 
5245 	sc->params.portvec = val[0];
5246 	sc->params.nports = bitcount32(val[0]);
5247 	sc->params.vpd.cclk = val[1];
5248 
5249 	/* Read device log parameters. */
5250 	rc = -t4_init_devlog_params(sc, 1);
5251 	if (rc == 0)
5252 		fixup_devlog_params(sc);
5253 	else {
5254 		device_printf(sc->dev,
5255 		    "failed to get devlog parameters: %d.\n", rc);
5256 		rc = 0;	/* devlog isn't critical for device operation */
5257 	}
5258 
5259 	return (rc);
5260 }
5261 
5262 /*
5263  * Any params that need to be set before FW_INITIALIZE.
5264  */
5265 static int
set_params__pre_init(struct adapter * sc)5266 set_params__pre_init(struct adapter *sc)
5267 {
5268 	int rc = 0;
5269 	uint32_t param, val;
5270 
5271 	if (chip_id(sc) >= CHELSIO_T6) {
5272 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5273 		val = 1;
5274 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5275 		/* firmwares < 1.20.1.0 do not have this param. */
5276 		if (rc == FW_EINVAL &&
5277 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5278 			rc = 0;
5279 		}
5280 		if (rc != 0) {
5281 			device_printf(sc->dev,
5282 			    "failed to enable high priority filters :%d.\n",
5283 			    rc);
5284 		}
5285 
5286 		param = FW_PARAM_DEV(PPOD_EDRAM);
5287 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5288 		if (rc == 0 && val == 1) {
5289 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5290 			    &val);
5291 			if (rc != 0) {
5292 				device_printf(sc->dev,
5293 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5294 			}
5295 		}
5296 	}
5297 
5298 	/* Enable opaque VIIDs with firmwares that support it. */
5299 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5300 	val = 1;
5301 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5302 	if (rc == 0 && val == 1)
5303 		sc->params.viid_smt_extn_support = true;
5304 	else
5305 		sc->params.viid_smt_extn_support = false;
5306 
5307 	return (rc);
5308 }
5309 
5310 /*
5311  * Retrieve various parameters that are of interest to the driver.  The device
5312  * has been initialized by the firmware at this point.
5313  */
5314 static int
get_params__post_init(struct adapter * sc)5315 get_params__post_init(struct adapter *sc)
5316 {
5317 	int rc;
5318 	uint32_t param[7], val[7];
5319 	struct fw_caps_config_cmd caps;
5320 
5321 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5322 	param[1] = FW_PARAM_PFVF(EQ_START);
5323 	param[2] = FW_PARAM_PFVF(FILTER_START);
5324 	param[3] = FW_PARAM_PFVF(FILTER_END);
5325 	param[4] = FW_PARAM_PFVF(L2T_START);
5326 	param[5] = FW_PARAM_PFVF(L2T_END);
5327 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5328 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5329 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5330 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5331 	if (rc != 0) {
5332 		device_printf(sc->dev,
5333 		    "failed to query parameters (post_init): %d.\n", rc);
5334 		return (rc);
5335 	}
5336 
5337 	sc->sge.iq_start = val[0];
5338 	sc->sge.eq_start = val[1];
5339 	if ((int)val[3] > (int)val[2]) {
5340 		sc->tids.ftid_base = val[2];
5341 		sc->tids.ftid_end = val[3];
5342 		sc->tids.nftids = val[3] - val[2] + 1;
5343 	}
5344 	sc->vres.l2t.start = val[4];
5345 	sc->vres.l2t.size = val[5] - val[4] + 1;
5346 	/* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5347 	if (sc->vres.l2t.size > 0)
5348 		MPASS(fls(val[5]) <= S_SYNC_WR);
5349 	sc->params.core_vdd = val[6];
5350 
5351 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5352 	param[1] = FW_PARAM_PFVF(EQ_END);
5353 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5354 	if (rc != 0) {
5355 		device_printf(sc->dev,
5356 		    "failed to query parameters (post_init2): %d.\n", rc);
5357 		return (rc);
5358 	}
5359 	MPASS((int)val[0] >= sc->sge.iq_start);
5360 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5361 	MPASS((int)val[1] >= sc->sge.eq_start);
5362 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5363 
5364 	if (chip_id(sc) >= CHELSIO_T6) {
5365 
5366 		sc->tids.tid_base = t4_read_reg(sc,
5367 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5368 
5369 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5370 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5371 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5372 		if (rc != 0) {
5373 			device_printf(sc->dev,
5374 			   "failed to query hpfilter parameters: %d.\n", rc);
5375 			return (rc);
5376 		}
5377 		if ((int)val[1] > (int)val[0]) {
5378 			sc->tids.hpftid_base = val[0];
5379 			sc->tids.hpftid_end = val[1];
5380 			sc->tids.nhpftids = val[1] - val[0] + 1;
5381 
5382 			/*
5383 			 * These should go off if the layout changes and the
5384 			 * driver needs to catch up.
5385 			 */
5386 			MPASS(sc->tids.hpftid_base == 0);
5387 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5388 		}
5389 
5390 		param[0] = FW_PARAM_PFVF(RAWF_START);
5391 		param[1] = FW_PARAM_PFVF(RAWF_END);
5392 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5393 		if (rc != 0) {
5394 			device_printf(sc->dev,
5395 			   "failed to query rawf parameters: %d.\n", rc);
5396 			return (rc);
5397 		}
5398 		if ((int)val[1] > (int)val[0]) {
5399 			sc->rawf_base = val[0];
5400 			sc->nrawf = val[1] - val[0] + 1;
5401 		}
5402 	}
5403 
5404 	/*
5405 	 * The parameters that follow may not be available on all firmwares.  We
5406 	 * query them individually rather than in a compound query because old
5407 	 * firmwares fail the entire query if an unknown parameter is queried.
5408 	 */
5409 
5410 	/*
5411 	 * MPS buffer group configuration.
5412 	 */
5413 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5414 	val[0] = 0;
5415 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5416 	if (rc == 0)
5417 		sc->params.mps_bg_map = val[0];
5418 	else
5419 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5420 
5421 	param[0] = FW_PARAM_DEV(TPCHMAP);
5422 	val[0] = 0;
5423 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5424 	if (rc == 0)
5425 		sc->params.tp_ch_map = val[0];
5426 	else
5427 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5428 
5429 	/*
5430 	 * Determine whether the firmware supports the filter2 work request.
5431 	 */
5432 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5433 	val[0] = 0;
5434 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5435 	if (rc == 0)
5436 		sc->params.filter2_wr_support = val[0] != 0;
5437 	else
5438 		sc->params.filter2_wr_support = 0;
5439 
5440 	/*
5441 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5442 	 */
5443 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5444 	val[0] = 0;
5445 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5446 	if (rc == 0)
5447 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5448 	else
5449 		sc->params.ulptx_memwrite_dsgl = false;
5450 
5451 	/* FW_RI_FR_NSMR_TPTE_WR support */
5452 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5453 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5454 	if (rc == 0)
5455 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5456 	else
5457 		sc->params.fr_nsmr_tpte_wr_support = false;
5458 
5459 	/* Support for 512 SGL entries per FR MR. */
5460 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5461 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5462 	if (rc == 0)
5463 		sc->params.dev_512sgl_mr = val[0] != 0;
5464 	else
5465 		sc->params.dev_512sgl_mr = false;
5466 
5467 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5468 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5469 	if (rc == 0)
5470 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5471 	else
5472 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5473 
5474 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5475 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5476 	if (rc == 0) {
5477 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5478 		sc->params.nsched_cls = val[0];
5479 	} else
5480 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5481 
5482 	/* get capabilites */
5483 	bzero(&caps, sizeof(caps));
5484 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5485 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5486 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5487 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5488 	if (rc != 0) {
5489 		device_printf(sc->dev,
5490 		    "failed to get card capabilities: %d.\n", rc);
5491 		return (rc);
5492 	}
5493 
5494 #define READ_CAPS(x) do { \
5495 	sc->x = htobe16(caps.x); \
5496 } while (0)
5497 	READ_CAPS(nbmcaps);
5498 	READ_CAPS(linkcaps);
5499 	READ_CAPS(switchcaps);
5500 	READ_CAPS(niccaps);
5501 	READ_CAPS(toecaps);
5502 	READ_CAPS(rdmacaps);
5503 	READ_CAPS(cryptocaps);
5504 	READ_CAPS(iscsicaps);
5505 	READ_CAPS(fcoecaps);
5506 
5507 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5508 		MPASS(chip_id(sc) > CHELSIO_T4);
5509 		MPASS(sc->toecaps == 0);
5510 		sc->toecaps = 0;
5511 
5512 		param[0] = FW_PARAM_DEV(NTID);
5513 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5514 		if (rc != 0) {
5515 			device_printf(sc->dev,
5516 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5517 			return (rc);
5518 		}
5519 		sc->tids.ntids = val[0];
5520 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5521 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5522 			sc->tids.ntids -= sc->tids.nhpftids;
5523 		}
5524 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5525 		sc->params.hash_filter = 1;
5526 	}
5527 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5528 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5529 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5530 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5531 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5532 		if (rc != 0) {
5533 			device_printf(sc->dev,
5534 			    "failed to query NIC parameters: %d.\n", rc);
5535 			return (rc);
5536 		}
5537 		if ((int)val[1] > (int)val[0]) {
5538 			sc->tids.etid_base = val[0];
5539 			sc->tids.etid_end = val[1];
5540 			sc->tids.netids = val[1] - val[0] + 1;
5541 			sc->params.eo_wr_cred = val[2];
5542 			sc->params.ethoffload = 1;
5543 		}
5544 	}
5545 	if (sc->toecaps) {
5546 		/* query offload-related parameters */
5547 		param[0] = FW_PARAM_DEV(NTID);
5548 		param[1] = FW_PARAM_PFVF(SERVER_START);
5549 		param[2] = FW_PARAM_PFVF(SERVER_END);
5550 		param[3] = FW_PARAM_PFVF(TDDP_START);
5551 		param[4] = FW_PARAM_PFVF(TDDP_END);
5552 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5553 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5554 		if (rc != 0) {
5555 			device_printf(sc->dev,
5556 			    "failed to query TOE parameters: %d.\n", rc);
5557 			return (rc);
5558 		}
5559 		sc->tids.ntids = val[0];
5560 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5561 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5562 			sc->tids.ntids -= sc->tids.nhpftids;
5563 		}
5564 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5565 		if ((int)val[2] > (int)val[1]) {
5566 			sc->tids.stid_base = val[1];
5567 			sc->tids.nstids = val[2] - val[1] + 1;
5568 		}
5569 		sc->vres.ddp.start = val[3];
5570 		sc->vres.ddp.size = val[4] - val[3] + 1;
5571 		sc->params.ofldq_wr_cred = val[5];
5572 		sc->params.offload = 1;
5573 	} else {
5574 		/*
5575 		 * The firmware attempts memfree TOE configuration for -SO cards
5576 		 * and will report toecaps=0 if it runs out of resources (this
5577 		 * depends on the config file).  It may not report 0 for other
5578 		 * capabilities dependent on the TOE in this case.  Set them to
5579 		 * 0 here so that the driver doesn't bother tracking resources
5580 		 * that will never be used.
5581 		 */
5582 		sc->iscsicaps = 0;
5583 		sc->rdmacaps = 0;
5584 	}
5585 	if (sc->rdmacaps) {
5586 		param[0] = FW_PARAM_PFVF(STAG_START);
5587 		param[1] = FW_PARAM_PFVF(STAG_END);
5588 		param[2] = FW_PARAM_PFVF(RQ_START);
5589 		param[3] = FW_PARAM_PFVF(RQ_END);
5590 		param[4] = FW_PARAM_PFVF(PBL_START);
5591 		param[5] = FW_PARAM_PFVF(PBL_END);
5592 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5593 		if (rc != 0) {
5594 			device_printf(sc->dev,
5595 			    "failed to query RDMA parameters(1): %d.\n", rc);
5596 			return (rc);
5597 		}
5598 		sc->vres.stag.start = val[0];
5599 		sc->vres.stag.size = val[1] - val[0] + 1;
5600 		sc->vres.rq.start = val[2];
5601 		sc->vres.rq.size = val[3] - val[2] + 1;
5602 		sc->vres.pbl.start = val[4];
5603 		sc->vres.pbl.size = val[5] - val[4] + 1;
5604 
5605 		param[0] = FW_PARAM_PFVF(SQRQ_START);
5606 		param[1] = FW_PARAM_PFVF(SQRQ_END);
5607 		param[2] = FW_PARAM_PFVF(CQ_START);
5608 		param[3] = FW_PARAM_PFVF(CQ_END);
5609 		param[4] = FW_PARAM_PFVF(OCQ_START);
5610 		param[5] = FW_PARAM_PFVF(OCQ_END);
5611 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5612 		if (rc != 0) {
5613 			device_printf(sc->dev,
5614 			    "failed to query RDMA parameters(2): %d.\n", rc);
5615 			return (rc);
5616 		}
5617 		sc->vres.qp.start = val[0];
5618 		sc->vres.qp.size = val[1] - val[0] + 1;
5619 		sc->vres.cq.start = val[2];
5620 		sc->vres.cq.size = val[3] - val[2] + 1;
5621 		sc->vres.ocq.start = val[4];
5622 		sc->vres.ocq.size = val[5] - val[4] + 1;
5623 
5624 		param[0] = FW_PARAM_PFVF(SRQ_START);
5625 		param[1] = FW_PARAM_PFVF(SRQ_END);
5626 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
5627 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5628 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5629 		if (rc != 0) {
5630 			device_printf(sc->dev,
5631 			    "failed to query RDMA parameters(3): %d.\n", rc);
5632 			return (rc);
5633 		}
5634 		sc->vres.srq.start = val[0];
5635 		sc->vres.srq.size = val[1] - val[0] + 1;
5636 		sc->params.max_ordird_qp = val[2];
5637 		sc->params.max_ird_adapter = val[3];
5638 	}
5639 	if (sc->iscsicaps) {
5640 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5641 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5642 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5643 		if (rc != 0) {
5644 			device_printf(sc->dev,
5645 			    "failed to query iSCSI parameters: %d.\n", rc);
5646 			return (rc);
5647 		}
5648 		sc->vres.iscsi.start = val[0];
5649 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5650 	}
5651 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5652 		param[0] = FW_PARAM_PFVF(TLS_START);
5653 		param[1] = FW_PARAM_PFVF(TLS_END);
5654 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5655 		if (rc != 0) {
5656 			device_printf(sc->dev,
5657 			    "failed to query TLS parameters: %d.\n", rc);
5658 			return (rc);
5659 		}
5660 		sc->vres.key.start = val[0];
5661 		sc->vres.key.size = val[1] - val[0] + 1;
5662 	}
5663 
5664 	/*
5665 	 * We've got the params we wanted to query directly from the firmware.
5666 	 * Grab some others via other means.
5667 	 */
5668 	t4_init_sge_params(sc);
5669 	t4_init_tp_params(sc);
5670 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5671 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5672 
5673 	rc = t4_verify_chip_settings(sc);
5674 	if (rc != 0)
5675 		return (rc);
5676 	t4_init_rx_buf_info(sc);
5677 
5678 	return (rc);
5679 }
5680 
5681 #ifdef KERN_TLS
5682 static void
ktls_tick(void * arg)5683 ktls_tick(void *arg)
5684 {
5685 	struct adapter *sc;
5686 	uint32_t tstamp;
5687 
5688 	sc = arg;
5689 	tstamp = tcp_ts_getticks();
5690 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
5691 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
5692 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
5693 }
5694 
5695 static int
t6_config_kern_tls(struct adapter * sc,bool enable)5696 t6_config_kern_tls(struct adapter *sc, bool enable)
5697 {
5698 	int rc;
5699 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5700 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
5701 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
5702 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
5703 
5704 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
5705 	if (rc != 0) {
5706 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
5707 		    enable ?  "enable" : "disable", rc);
5708 		return (rc);
5709 	}
5710 
5711 	if (enable) {
5712 		sc->flags |= KERN_TLS_ON;
5713 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
5714 		    C_HARDCLOCK);
5715 	} else {
5716 		sc->flags &= ~KERN_TLS_ON;
5717 		callout_stop(&sc->ktls_tick);
5718 	}
5719 
5720 	return (rc);
5721 }
5722 #endif
5723 
5724 static int
set_params__post_init(struct adapter * sc)5725 set_params__post_init(struct adapter *sc)
5726 {
5727 	uint32_t mask, param, val;
5728 #ifdef TCP_OFFLOAD
5729 	int i, v, shift;
5730 #endif
5731 
5732 	/* ask for encapsulated CPLs */
5733 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5734 	val = 1;
5735 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5736 
5737 	/* Enable 32b port caps if the firmware supports it. */
5738 	param = FW_PARAM_PFVF(PORT_CAPS32);
5739 	val = 1;
5740 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
5741 		sc->params.port_caps32 = 1;
5742 
5743 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
5744 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
5745 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
5746 	    V_MASKFILTER(val - 1));
5747 
5748 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
5749 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
5750 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5751 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
5752 	val = 0;
5753 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
5754 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
5755 		    F_ATTACKFILTERENABLE);
5756 		val |= F_DROPERRORATTACK;
5757 	}
5758 	if (t4_drop_ip_fragments != 0) {
5759 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
5760 		    F_FRAGMENTDROP);
5761 		val |= F_DROPERRORFRAG;
5762 	}
5763 	if (t4_drop_pkts_with_l2_errors != 0)
5764 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
5765 	if (t4_drop_pkts_with_l3_errors != 0) {
5766 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
5767 		    F_DROPERRORCSUMIP;
5768 	}
5769 	if (t4_drop_pkts_with_l4_errors != 0) {
5770 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5771 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
5772 	}
5773 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
5774 
5775 #ifdef TCP_OFFLOAD
5776 	/*
5777 	 * Override the TOE timers with user provided tunables.  This is not the
5778 	 * recommended way to change the timers (the firmware config file is) so
5779 	 * these tunables are not documented.
5780 	 *
5781 	 * All the timer tunables are in microseconds.
5782 	 */
5783 	if (t4_toe_keepalive_idle != 0) {
5784 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
5785 		v &= M_KEEPALIVEIDLE;
5786 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
5787 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
5788 	}
5789 	if (t4_toe_keepalive_interval != 0) {
5790 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
5791 		v &= M_KEEPALIVEINTVL;
5792 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
5793 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
5794 	}
5795 	if (t4_toe_keepalive_count != 0) {
5796 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
5797 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5798 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
5799 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
5800 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
5801 	}
5802 	if (t4_toe_rexmt_min != 0) {
5803 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
5804 		v &= M_RXTMIN;
5805 		t4_set_reg_field(sc, A_TP_RXT_MIN,
5806 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
5807 	}
5808 	if (t4_toe_rexmt_max != 0) {
5809 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
5810 		v &= M_RXTMAX;
5811 		t4_set_reg_field(sc, A_TP_RXT_MAX,
5812 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
5813 	}
5814 	if (t4_toe_rexmt_count != 0) {
5815 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
5816 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5817 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
5818 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
5819 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
5820 	}
5821 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
5822 		if (t4_toe_rexmt_backoff[i] != -1) {
5823 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
5824 			shift = (i & 3) << 3;
5825 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
5826 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
5827 		}
5828 	}
5829 #endif
5830 
5831 	/*
5832 	 * Limit TOE connections to 2 reassembly "islands".  This is
5833 	 * required to permit migrating TOE connections to either
5834 	 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
5835 	 */
5836 	t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
5837 	    V_PASSMODE(2));
5838 
5839 #ifdef KERN_TLS
5840 	if (is_ktls(sc)) {
5841 		sc->tlst.inline_keys = t4_tls_inline_keys;
5842 		sc->tlst.combo_wrs = t4_tls_combo_wrs;
5843 		if (t4_kern_tls != 0 && is_t6(sc))
5844 			t6_config_kern_tls(sc, true);
5845 	}
5846 #endif
5847 	return (0);
5848 }
5849 
5850 #undef FW_PARAM_PFVF
5851 #undef FW_PARAM_DEV
5852 
5853 static void
t4_set_desc(struct adapter * sc)5854 t4_set_desc(struct adapter *sc)
5855 {
5856 	struct adapter_params *p = &sc->params;
5857 
5858 	device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
5859 }
5860 
5861 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)5862 ifmedia_add4(struct ifmedia *ifm, int m)
5863 {
5864 
5865 	ifmedia_add(ifm, m, 0, NULL);
5866 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
5867 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
5868 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
5869 }
5870 
5871 /*
5872  * This is the selected media, which is not quite the same as the active media.
5873  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
5874  * and active are not the same, and "media: Ethernet selected" otherwise.
5875  */
5876 static void
set_current_media(struct port_info * pi)5877 set_current_media(struct port_info *pi)
5878 {
5879 	struct link_config *lc;
5880 	struct ifmedia *ifm;
5881 	int mword;
5882 	u_int speed;
5883 
5884 	PORT_LOCK_ASSERT_OWNED(pi);
5885 
5886 	/* Leave current media alone if it's already set to IFM_NONE. */
5887 	ifm = &pi->media;
5888 	if (ifm->ifm_cur != NULL &&
5889 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
5890 		return;
5891 
5892 	lc = &pi->link_cfg;
5893 	if (lc->requested_aneg != AUTONEG_DISABLE &&
5894 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
5895 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
5896 		return;
5897 	}
5898 	mword = IFM_ETHER | IFM_FDX;
5899 	if (lc->requested_fc & PAUSE_TX)
5900 		mword |= IFM_ETH_TXPAUSE;
5901 	if (lc->requested_fc & PAUSE_RX)
5902 		mword |= IFM_ETH_RXPAUSE;
5903 	if (lc->requested_speed == 0)
5904 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
5905 	else
5906 		speed = lc->requested_speed;
5907 	mword |= port_mword(pi, speed_to_fwcap(speed));
5908 	ifmedia_set(ifm, mword);
5909 }
5910 
5911 /*
5912  * Returns true if the ifmedia list for the port cannot change.
5913  */
5914 static bool
fixed_ifmedia(struct port_info * pi)5915 fixed_ifmedia(struct port_info *pi)
5916 {
5917 
5918 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
5919 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
5920 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
5921 	    pi->port_type == FW_PORT_TYPE_KX4 ||
5922 	    pi->port_type == FW_PORT_TYPE_KX ||
5923 	    pi->port_type == FW_PORT_TYPE_KR ||
5924 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
5925 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
5926 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
5927 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
5928 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
5929 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
5930 }
5931 
5932 static void
build_medialist(struct port_info * pi)5933 build_medialist(struct port_info *pi)
5934 {
5935 	uint32_t ss, speed;
5936 	int unknown, mword, bit;
5937 	struct link_config *lc;
5938 	struct ifmedia *ifm;
5939 
5940 	PORT_LOCK_ASSERT_OWNED(pi);
5941 
5942 	if (pi->flags & FIXED_IFMEDIA)
5943 		return;
5944 
5945 	/*
5946 	 * Rebuild the ifmedia list.
5947 	 */
5948 	ifm = &pi->media;
5949 	ifmedia_removeall(ifm);
5950 	lc = &pi->link_cfg;
5951 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
5952 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
5953 		MPASS(ss != 0);
5954 no_media:
5955 		MPASS(LIST_EMPTY(&ifm->ifm_list));
5956 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
5957 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
5958 		return;
5959 	}
5960 
5961 	unknown = 0;
5962 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
5963 		speed = 1 << bit;
5964 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
5965 		if (ss & speed) {
5966 			mword = port_mword(pi, speed);
5967 			if (mword == IFM_NONE) {
5968 				goto no_media;
5969 			} else if (mword == IFM_UNKNOWN)
5970 				unknown++;
5971 			else
5972 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
5973 		}
5974 	}
5975 	if (unknown > 0) /* Add one unknown for all unknown media types. */
5976 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
5977 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
5978 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
5979 
5980 	set_current_media(pi);
5981 }
5982 
5983 /*
5984  * Initialize the requested fields in the link config based on driver tunables.
5985  */
5986 static void
init_link_config(struct port_info * pi)5987 init_link_config(struct port_info *pi)
5988 {
5989 	struct link_config *lc = &pi->link_cfg;
5990 
5991 	PORT_LOCK_ASSERT_OWNED(pi);
5992 
5993 	lc->requested_caps = 0;
5994 	lc->requested_speed = 0;
5995 
5996 	if (t4_autoneg == 0)
5997 		lc->requested_aneg = AUTONEG_DISABLE;
5998 	else if (t4_autoneg == 1)
5999 		lc->requested_aneg = AUTONEG_ENABLE;
6000 	else
6001 		lc->requested_aneg = AUTONEG_AUTO;
6002 
6003 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
6004 	    PAUSE_AUTONEG);
6005 
6006 	if (t4_fec & FEC_AUTO)
6007 		lc->requested_fec = FEC_AUTO;
6008 	else if (t4_fec == 0)
6009 		lc->requested_fec = FEC_NONE;
6010 	else {
6011 		/* -1 is handled by the FEC_AUTO block above and not here. */
6012 		lc->requested_fec = t4_fec &
6013 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6014 		if (lc->requested_fec == 0)
6015 			lc->requested_fec = FEC_AUTO;
6016 	}
6017 	if (t4_force_fec < 0)
6018 		lc->force_fec = -1;
6019 	else if (t4_force_fec > 0)
6020 		lc->force_fec = 1;
6021 	else
6022 		lc->force_fec = 0;
6023 }
6024 
6025 /*
6026  * Makes sure that all requested settings comply with what's supported by the
6027  * port.  Returns the number of settings that were invalid and had to be fixed.
6028  */
6029 static int
fixup_link_config(struct port_info * pi)6030 fixup_link_config(struct port_info *pi)
6031 {
6032 	int n = 0;
6033 	struct link_config *lc = &pi->link_cfg;
6034 	uint32_t fwspeed;
6035 
6036 	PORT_LOCK_ASSERT_OWNED(pi);
6037 
6038 	/* Speed (when not autonegotiating) */
6039 	if (lc->requested_speed != 0) {
6040 		fwspeed = speed_to_fwcap(lc->requested_speed);
6041 		if ((fwspeed & lc->pcaps) == 0) {
6042 			n++;
6043 			lc->requested_speed = 0;
6044 		}
6045 	}
6046 
6047 	/* Link autonegotiation */
6048 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6049 	    lc->requested_aneg == AUTONEG_DISABLE ||
6050 	    lc->requested_aneg == AUTONEG_AUTO);
6051 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6052 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6053 		n++;
6054 		lc->requested_aneg = AUTONEG_AUTO;
6055 	}
6056 
6057 	/* Flow control */
6058 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6059 	if (lc->requested_fc & PAUSE_TX &&
6060 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6061 		n++;
6062 		lc->requested_fc &= ~PAUSE_TX;
6063 	}
6064 	if (lc->requested_fc & PAUSE_RX &&
6065 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6066 		n++;
6067 		lc->requested_fc &= ~PAUSE_RX;
6068 	}
6069 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6070 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6071 		n++;
6072 		lc->requested_fc |= PAUSE_AUTONEG;
6073 	}
6074 
6075 	/* FEC */
6076 	if ((lc->requested_fec & FEC_RS &&
6077 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6078 	    (lc->requested_fec & FEC_BASER_RS &&
6079 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6080 		n++;
6081 		lc->requested_fec = FEC_AUTO;
6082 	}
6083 
6084 	return (n);
6085 }
6086 
6087 /*
6088  * Apply the requested L1 settings, which are expected to be valid, to the
6089  * hardware.
6090  */
6091 static int
apply_link_config(struct port_info * pi)6092 apply_link_config(struct port_info *pi)
6093 {
6094 	struct adapter *sc = pi->adapter;
6095 	struct link_config *lc = &pi->link_cfg;
6096 	int rc;
6097 
6098 #ifdef INVARIANTS
6099 	ASSERT_SYNCHRONIZED_OP(sc);
6100 	PORT_LOCK_ASSERT_OWNED(pi);
6101 
6102 	if (lc->requested_aneg == AUTONEG_ENABLE)
6103 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6104 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6105 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6106 	if (lc->requested_fc & PAUSE_TX)
6107 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6108 	if (lc->requested_fc & PAUSE_RX)
6109 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6110 	if (lc->requested_fec & FEC_RS)
6111 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6112 	if (lc->requested_fec & FEC_BASER_RS)
6113 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6114 #endif
6115 	if (!(sc->flags & IS_VF)) {
6116 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
6117 		if (rc != 0) {
6118 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6119 			return (rc);
6120 		}
6121 	}
6122 
6123 	/*
6124 	 * An L1_CFG will almost always result in a link-change event if the
6125 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6126 	 * the notification is processed.  If the link is down then the actual
6127 	 * settings are meaningless.
6128 	 *
6129 	 * This takes care of the case where a change in the L1 settings may not
6130 	 * result in a notification.
6131 	 */
6132 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6133 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6134 
6135 	return (0);
6136 }
6137 
6138 #define FW_MAC_EXACT_CHUNK	7
6139 struct mcaddr_ctx {
6140 	if_t ifp;
6141 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6142 	uint64_t hash;
6143 	int i;
6144 	int del;
6145 	int rc;
6146 };
6147 
6148 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6149 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6150 {
6151 	struct mcaddr_ctx *ctx = arg;
6152 	struct vi_info *vi = if_getsoftc(ctx->ifp);
6153 	struct port_info *pi = vi->pi;
6154 	struct adapter *sc = pi->adapter;
6155 
6156 	if (ctx->rc < 0)
6157 		return (0);
6158 
6159 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6160 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6161 	ctx->i++;
6162 
6163 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6164 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6165 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6166 		if (ctx->rc < 0) {
6167 			int j;
6168 
6169 			for (j = 0; j < ctx->i; j++) {
6170 				if_printf(ctx->ifp,
6171 				    "failed to add mc address"
6172 				    " %02x:%02x:%02x:"
6173 				    "%02x:%02x:%02x rc=%d\n",
6174 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6175 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6176 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6177 				    -ctx->rc);
6178 			}
6179 			return (0);
6180 		}
6181 		ctx->del = 0;
6182 		ctx->i = 0;
6183 	}
6184 
6185 	return (1);
6186 }
6187 
6188 /*
6189  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6190  * indicates which parameters should be programmed (the rest are left alone).
6191  */
6192 int
update_mac_settings(if_t ifp,int flags)6193 update_mac_settings(if_t ifp, int flags)
6194 {
6195 	int rc = 0;
6196 	struct vi_info *vi = if_getsoftc(ifp);
6197 	struct port_info *pi = vi->pi;
6198 	struct adapter *sc = pi->adapter;
6199 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6200 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6201 
6202 	ASSERT_SYNCHRONIZED_OP(sc);
6203 	KASSERT(flags, ("%s: not told what to update.", __func__));
6204 
6205 	if (flags & XGMAC_MTU)
6206 		mtu = if_getmtu(ifp);
6207 
6208 	if (flags & XGMAC_PROMISC)
6209 		promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6210 
6211 	if (flags & XGMAC_ALLMULTI)
6212 		allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6213 
6214 	if (flags & XGMAC_VLANEX)
6215 		vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6216 
6217 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6218 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6219 		    allmulti, 1, vlanex, false);
6220 		if (rc) {
6221 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6222 			    rc);
6223 			return (rc);
6224 		}
6225 	}
6226 
6227 	if (flags & XGMAC_UCADDR) {
6228 		uint8_t ucaddr[ETHER_ADDR_LEN];
6229 
6230 		bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6231 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6232 		    ucaddr, true, &vi->smt_idx);
6233 		if (rc < 0) {
6234 			rc = -rc;
6235 			if_printf(ifp, "change_mac failed: %d\n", rc);
6236 			return (rc);
6237 		} else {
6238 			vi->xact_addr_filt = rc;
6239 			rc = 0;
6240 		}
6241 	}
6242 
6243 	if (flags & XGMAC_MCADDRS) {
6244 		struct epoch_tracker et;
6245 		struct mcaddr_ctx ctx;
6246 		int j;
6247 
6248 		ctx.ifp = ifp;
6249 		ctx.hash = 0;
6250 		ctx.i = 0;
6251 		ctx.del = 1;
6252 		ctx.rc = 0;
6253 		/*
6254 		 * Unlike other drivers, we accumulate list of pointers into
6255 		 * interface address lists and we need to keep it safe even
6256 		 * after if_foreach_llmaddr() returns, thus we must enter the
6257 		 * network epoch.
6258 		 */
6259 		NET_EPOCH_ENTER(et);
6260 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6261 		if (ctx.rc < 0) {
6262 			NET_EPOCH_EXIT(et);
6263 			rc = -ctx.rc;
6264 			return (rc);
6265 		}
6266 		if (ctx.i > 0) {
6267 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6268 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6269 			NET_EPOCH_EXIT(et);
6270 			if (rc < 0) {
6271 				rc = -rc;
6272 				for (j = 0; j < ctx.i; j++) {
6273 					if_printf(ifp,
6274 					    "failed to add mcast address"
6275 					    " %02x:%02x:%02x:"
6276 					    "%02x:%02x:%02x rc=%d\n",
6277 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6278 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6279 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6280 					    rc);
6281 				}
6282 				return (rc);
6283 			}
6284 			ctx.del = 0;
6285 		} else
6286 			NET_EPOCH_EXIT(et);
6287 
6288 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6289 		if (rc != 0)
6290 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6291 			    rc);
6292 		if (ctx.del == 0) {
6293 			/* We clobbered the VXLAN entry if there was one. */
6294 			pi->vxlan_tcam_entry = false;
6295 		}
6296 	}
6297 
6298 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6299 	    pi->vxlan_tcam_entry == false) {
6300 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6301 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6302 		    true);
6303 		if (rc < 0) {
6304 			rc = -rc;
6305 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6306 			    rc);
6307 		} else {
6308 			MPASS(rc == sc->rawf_base + pi->port_id);
6309 			rc = 0;
6310 			pi->vxlan_tcam_entry = true;
6311 		}
6312 	}
6313 
6314 	return (rc);
6315 }
6316 
6317 /*
6318  * {begin|end}_synchronized_op must be called from the same thread.
6319  */
6320 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6321 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6322     char *wmesg)
6323 {
6324 	int rc, pri;
6325 
6326 #ifdef WITNESS
6327 	/* the caller thinks it's ok to sleep, but is it really? */
6328 	if (flags & SLEEP_OK)
6329 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
6330 		    "begin_synchronized_op");
6331 #endif
6332 
6333 	if (INTR_OK)
6334 		pri = PCATCH;
6335 	else
6336 		pri = 0;
6337 
6338 	ADAPTER_LOCK(sc);
6339 	for (;;) {
6340 
6341 		if (vi && IS_DETACHING(vi)) {
6342 			rc = ENXIO;
6343 			goto done;
6344 		}
6345 
6346 		if (!IS_BUSY(sc)) {
6347 			rc = 0;
6348 			break;
6349 		}
6350 
6351 		if (!(flags & SLEEP_OK)) {
6352 			rc = EBUSY;
6353 			goto done;
6354 		}
6355 
6356 		if (mtx_sleep(&sc->flags, &sc->sc_lock, pri, wmesg, 0)) {
6357 			rc = EINTR;
6358 			goto done;
6359 		}
6360 	}
6361 
6362 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6363 	SET_BUSY(sc);
6364 #ifdef INVARIANTS
6365 	sc->last_op = wmesg;
6366 	sc->last_op_thr = curthread;
6367 	sc->last_op_flags = flags;
6368 #endif
6369 
6370 done:
6371 	if (!(flags & HOLD_LOCK) || rc)
6372 		ADAPTER_UNLOCK(sc);
6373 
6374 	return (rc);
6375 }
6376 
6377 /*
6378  * Tell if_ioctl and if_init that the VI is going away.  This is
6379  * special variant of begin_synchronized_op and must be paired with a
6380  * call to end_vi_detach.
6381  */
6382 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6383 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6384 {
6385 	ADAPTER_LOCK(sc);
6386 	SET_DETACHING(vi);
6387 	wakeup(&sc->flags);
6388 	while (IS_BUSY(sc))
6389 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6390 	SET_BUSY(sc);
6391 #ifdef INVARIANTS
6392 	sc->last_op = "t4detach";
6393 	sc->last_op_thr = curthread;
6394 	sc->last_op_flags = 0;
6395 #endif
6396 	ADAPTER_UNLOCK(sc);
6397 }
6398 
6399 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6400 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6401 {
6402 	ADAPTER_LOCK(sc);
6403 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6404 	CLR_BUSY(sc);
6405 	CLR_DETACHING(vi);
6406 	wakeup(&sc->flags);
6407 	ADAPTER_UNLOCK(sc);
6408 }
6409 
6410 /*
6411  * {begin|end}_synchronized_op must be called from the same thread.
6412  */
6413 void
end_synchronized_op(struct adapter * sc,int flags)6414 end_synchronized_op(struct adapter *sc, int flags)
6415 {
6416 
6417 	if (flags & LOCK_HELD)
6418 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6419 	else
6420 		ADAPTER_LOCK(sc);
6421 
6422 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6423 	CLR_BUSY(sc);
6424 	wakeup(&sc->flags);
6425 	ADAPTER_UNLOCK(sc);
6426 }
6427 
6428 static int
cxgbe_init_synchronized(struct vi_info * vi)6429 cxgbe_init_synchronized(struct vi_info *vi)
6430 {
6431 	struct port_info *pi = vi->pi;
6432 	struct adapter *sc = pi->adapter;
6433 	if_t ifp = vi->ifp;
6434 	int rc = 0, i;
6435 	struct sge_txq *txq;
6436 
6437 	ASSERT_SYNCHRONIZED_OP(sc);
6438 
6439 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6440 		return (0);	/* already running */
6441 
6442 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6443 		return (rc);	/* error message displayed already */
6444 
6445 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6446 		return (rc); /* error message displayed already */
6447 
6448 	rc = update_mac_settings(ifp, XGMAC_ALL);
6449 	if (rc)
6450 		goto done;	/* error message displayed already */
6451 
6452 	PORT_LOCK(pi);
6453 	if (pi->up_vis == 0) {
6454 		t4_update_port_info(pi);
6455 		fixup_link_config(pi);
6456 		build_medialist(pi);
6457 		apply_link_config(pi);
6458 	}
6459 
6460 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6461 	if (rc != 0) {
6462 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6463 		PORT_UNLOCK(pi);
6464 		goto done;
6465 	}
6466 
6467 	/*
6468 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6469 	 * if this changes.
6470 	 */
6471 
6472 	for_each_txq(vi, i, txq) {
6473 		TXQ_LOCK(txq);
6474 		txq->eq.flags |= EQ_ENABLED;
6475 		TXQ_UNLOCK(txq);
6476 	}
6477 
6478 	/*
6479 	 * The first iq of the first port to come up is used for tracing.
6480 	 */
6481 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6482 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6483 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
6484 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
6485 		    V_QUEUENUMBER(sc->traceq));
6486 		pi->flags |= HAS_TRACEQ;
6487 	}
6488 
6489 	/* all ok */
6490 	pi->up_vis++;
6491 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6492 	if (pi->link_cfg.link_ok)
6493 		t4_os_link_changed(pi);
6494 	PORT_UNLOCK(pi);
6495 
6496 	mtx_lock(&vi->tick_mtx);
6497 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6498 		callout_reset(&vi->tick, hz, vi_tick, vi);
6499 	else
6500 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6501 	mtx_unlock(&vi->tick_mtx);
6502 done:
6503 	if (rc != 0)
6504 		cxgbe_uninit_synchronized(vi);
6505 
6506 	return (rc);
6507 }
6508 
6509 /*
6510  * Idempotent.
6511  */
6512 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6513 cxgbe_uninit_synchronized(struct vi_info *vi)
6514 {
6515 	struct port_info *pi = vi->pi;
6516 	struct adapter *sc = pi->adapter;
6517 	if_t ifp = vi->ifp;
6518 	int rc, i;
6519 	struct sge_txq *txq;
6520 
6521 	ASSERT_SYNCHRONIZED_OP(sc);
6522 
6523 	if (!(vi->flags & VI_INIT_DONE)) {
6524 		if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6525 			KASSERT(0, ("uninited VI is running"));
6526 			if_printf(ifp, "uninited VI with running ifnet.  "
6527 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6528 			    "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6529 			    if_getdrvflags(ifp));
6530 		}
6531 		return (0);
6532 	}
6533 
6534 	/*
6535 	 * Disable the VI so that all its data in either direction is discarded
6536 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6537 	 * tick) intact as the TP can deliver negative advice or data that it's
6538 	 * holding in its RAM (for an offloaded connection) even after the VI is
6539 	 * disabled.
6540 	 */
6541 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6542 	if (rc) {
6543 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6544 		return (rc);
6545 	}
6546 
6547 	for_each_txq(vi, i, txq) {
6548 		TXQ_LOCK(txq);
6549 		txq->eq.flags &= ~EQ_ENABLED;
6550 		TXQ_UNLOCK(txq);
6551 	}
6552 
6553 	mtx_lock(&vi->tick_mtx);
6554 	callout_stop(&vi->tick);
6555 	mtx_unlock(&vi->tick_mtx);
6556 
6557 	PORT_LOCK(pi);
6558 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6559 		PORT_UNLOCK(pi);
6560 		return (0);
6561 	}
6562 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6563 	pi->up_vis--;
6564 	if (pi->up_vis > 0) {
6565 		PORT_UNLOCK(pi);
6566 		return (0);
6567 	}
6568 
6569 	pi->link_cfg.link_ok = false;
6570 	pi->link_cfg.speed = 0;
6571 	pi->link_cfg.link_down_rc = 255;
6572 	t4_os_link_changed(pi);
6573 	PORT_UNLOCK(pi);
6574 
6575 	return (0);
6576 }
6577 
6578 /*
6579  * It is ok for this function to fail midway and return right away.  t4_detach
6580  * will walk the entire sc->irq list and clean up whatever is valid.
6581  */
6582 int
t4_setup_intr_handlers(struct adapter * sc)6583 t4_setup_intr_handlers(struct adapter *sc)
6584 {
6585 	int rc, rid, p, q, v;
6586 	char s[8];
6587 	struct irq *irq;
6588 	struct port_info *pi;
6589 	struct vi_info *vi;
6590 	struct sge *sge = &sc->sge;
6591 	struct sge_rxq *rxq;
6592 #ifdef TCP_OFFLOAD
6593 	struct sge_ofld_rxq *ofld_rxq;
6594 #endif
6595 #ifdef DEV_NETMAP
6596 	struct sge_nm_rxq *nm_rxq;
6597 #endif
6598 #ifdef RSS
6599 	int nbuckets = rss_getnumbuckets();
6600 #endif
6601 
6602 	/*
6603 	 * Setup interrupts.
6604 	 */
6605 	irq = &sc->irq[0];
6606 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6607 	if (forwarding_intr_to_fwq(sc))
6608 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6609 
6610 	/* Multiple interrupts. */
6611 	if (sc->flags & IS_VF)
6612 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6613 		    ("%s: too few intr.", __func__));
6614 	else
6615 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6616 		    ("%s: too few intr.", __func__));
6617 
6618 	/* The first one is always error intr on PFs */
6619 	if (!(sc->flags & IS_VF)) {
6620 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6621 		if (rc != 0)
6622 			return (rc);
6623 		irq++;
6624 		rid++;
6625 	}
6626 
6627 	/* The second one is always the firmware event queue (first on VFs) */
6628 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6629 	if (rc != 0)
6630 		return (rc);
6631 	irq++;
6632 	rid++;
6633 
6634 	for_each_port(sc, p) {
6635 		pi = sc->port[p];
6636 		for_each_vi(pi, v, vi) {
6637 			vi->first_intr = rid - 1;
6638 
6639 			if (vi->nnmrxq > 0) {
6640 				int n = max(vi->nrxq, vi->nnmrxq);
6641 
6642 				rxq = &sge->rxq[vi->first_rxq];
6643 #ifdef DEV_NETMAP
6644 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6645 #endif
6646 				for (q = 0; q < n; q++) {
6647 					snprintf(s, sizeof(s), "%x%c%x", p,
6648 					    'a' + v, q);
6649 					if (q < vi->nrxq)
6650 						irq->rxq = rxq++;
6651 #ifdef DEV_NETMAP
6652 					if (q < vi->nnmrxq)
6653 						irq->nm_rxq = nm_rxq++;
6654 
6655 					if (irq->nm_rxq != NULL &&
6656 					    irq->rxq == NULL) {
6657 						/* Netmap rx only */
6658 						rc = t4_alloc_irq(sc, irq, rid,
6659 						    t4_nm_intr, irq->nm_rxq, s);
6660 					}
6661 					if (irq->nm_rxq != NULL &&
6662 					    irq->rxq != NULL) {
6663 						/* NIC and Netmap rx */
6664 						rc = t4_alloc_irq(sc, irq, rid,
6665 						    t4_vi_intr, irq, s);
6666 					}
6667 #endif
6668 					if (irq->rxq != NULL &&
6669 					    irq->nm_rxq == NULL) {
6670 						/* NIC rx only */
6671 						rc = t4_alloc_irq(sc, irq, rid,
6672 						    t4_intr, irq->rxq, s);
6673 					}
6674 					if (rc != 0)
6675 						return (rc);
6676 #ifdef RSS
6677 					if (q < vi->nrxq) {
6678 						bus_bind_intr(sc->dev, irq->res,
6679 						    rss_getcpu(q % nbuckets));
6680 					}
6681 #endif
6682 					irq++;
6683 					rid++;
6684 					vi->nintr++;
6685 				}
6686 			} else {
6687 				for_each_rxq(vi, q, rxq) {
6688 					snprintf(s, sizeof(s), "%x%c%x", p,
6689 					    'a' + v, q);
6690 					rc = t4_alloc_irq(sc, irq, rid,
6691 					    t4_intr, rxq, s);
6692 					if (rc != 0)
6693 						return (rc);
6694 #ifdef RSS
6695 					bus_bind_intr(sc->dev, irq->res,
6696 					    rss_getcpu(q % nbuckets));
6697 #endif
6698 					irq++;
6699 					rid++;
6700 					vi->nintr++;
6701 				}
6702 			}
6703 #ifdef TCP_OFFLOAD
6704 			for_each_ofld_rxq(vi, q, ofld_rxq) {
6705 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
6706 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
6707 				    ofld_rxq, s);
6708 				if (rc != 0)
6709 					return (rc);
6710 				irq++;
6711 				rid++;
6712 				vi->nintr++;
6713 			}
6714 #endif
6715 		}
6716 	}
6717 	MPASS(irq == &sc->irq[sc->intr_count]);
6718 
6719 	return (0);
6720 }
6721 
6722 static void
write_global_rss_key(struct adapter * sc)6723 write_global_rss_key(struct adapter *sc)
6724 {
6725 #ifdef RSS
6726 	int i;
6727 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6728 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6729 
6730 	CTASSERT(RSS_KEYSIZE == 40);
6731 
6732 	rss_getkey((void *)&raw_rss_key[0]);
6733 	for (i = 0; i < nitems(rss_key); i++) {
6734 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
6735 	}
6736 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
6737 #endif
6738 }
6739 
6740 /*
6741  * Idempotent.
6742  */
6743 static int
adapter_full_init(struct adapter * sc)6744 adapter_full_init(struct adapter *sc)
6745 {
6746 	int rc, i;
6747 
6748 	ASSERT_SYNCHRONIZED_OP(sc);
6749 
6750 	/*
6751 	 * queues that belong to the adapter (not any particular port).
6752 	 */
6753 	rc = t4_setup_adapter_queues(sc);
6754 	if (rc != 0)
6755 		return (rc);
6756 
6757 	MPASS(sc->params.nports <= nitems(sc->tq));
6758 	for (i = 0; i < sc->params.nports; i++) {
6759 		if (sc->tq[i] != NULL)
6760 			continue;
6761 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
6762 		    taskqueue_thread_enqueue, &sc->tq[i]);
6763 		if (sc->tq[i] == NULL) {
6764 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
6765 			return (ENOMEM);
6766 		}
6767 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
6768 		    device_get_nameunit(sc->dev), i);
6769 	}
6770 
6771 	if (!(sc->flags & IS_VF)) {
6772 		write_global_rss_key(sc);
6773 		t4_intr_enable(sc);
6774 	}
6775 	return (0);
6776 }
6777 
6778 int
adapter_init(struct adapter * sc)6779 adapter_init(struct adapter *sc)
6780 {
6781 	int rc;
6782 
6783 	ASSERT_SYNCHRONIZED_OP(sc);
6784 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
6785 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
6786 	    ("%s: FULL_INIT_DONE already", __func__));
6787 
6788 	rc = adapter_full_init(sc);
6789 	if (rc != 0)
6790 		adapter_full_uninit(sc);
6791 	else
6792 		sc->flags |= FULL_INIT_DONE;
6793 
6794 	return (rc);
6795 }
6796 
6797 /*
6798  * Idempotent.
6799  */
6800 static void
adapter_full_uninit(struct adapter * sc)6801 adapter_full_uninit(struct adapter *sc)
6802 {
6803 	int i;
6804 
6805 	t4_teardown_adapter_queues(sc);
6806 
6807 	for (i = 0; i < nitems(sc->tq); i++) {
6808 		if (sc->tq[i] == NULL)
6809 			continue;
6810 		taskqueue_free(sc->tq[i]);
6811 		sc->tq[i] = NULL;
6812 	}
6813 
6814 	sc->flags &= ~FULL_INIT_DONE;
6815 }
6816 
6817 #ifdef RSS
6818 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
6819     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
6820     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
6821     RSS_HASHTYPE_RSS_UDP_IPV6)
6822 
6823 /* Translates kernel hash types to hardware. */
6824 static int
hashconfig_to_hashen(int hashconfig)6825 hashconfig_to_hashen(int hashconfig)
6826 {
6827 	int hashen = 0;
6828 
6829 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
6830 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
6831 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
6832 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
6833 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
6834 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6835 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6836 	}
6837 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
6838 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6839 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6840 	}
6841 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
6842 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6843 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
6844 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6845 
6846 	return (hashen);
6847 }
6848 
6849 /* Translates hardware hash types to kernel. */
6850 static int
hashen_to_hashconfig(int hashen)6851 hashen_to_hashconfig(int hashen)
6852 {
6853 	int hashconfig = 0;
6854 
6855 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
6856 		/*
6857 		 * If UDP hashing was enabled it must have been enabled for
6858 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
6859 		 * enabling any 4-tuple hash is nonsense configuration.
6860 		 */
6861 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6862 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
6863 
6864 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6865 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
6866 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6867 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
6868 	}
6869 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6870 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
6871 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6872 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
6873 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
6874 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
6875 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
6876 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
6877 
6878 	return (hashconfig);
6879 }
6880 #endif
6881 
6882 /*
6883  * Idempotent.
6884  */
6885 static int
vi_full_init(struct vi_info * vi)6886 vi_full_init(struct vi_info *vi)
6887 {
6888 	struct adapter *sc = vi->adapter;
6889 	struct sge_rxq *rxq;
6890 	int rc, i, j;
6891 #ifdef RSS
6892 	int nbuckets = rss_getnumbuckets();
6893 	int hashconfig = rss_gethashconfig();
6894 	int extra;
6895 #endif
6896 
6897 	ASSERT_SYNCHRONIZED_OP(sc);
6898 
6899 	/*
6900 	 * Allocate tx/rx/fl queues for this VI.
6901 	 */
6902 	rc = t4_setup_vi_queues(vi);
6903 	if (rc != 0)
6904 		return (rc);
6905 
6906 	/*
6907 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
6908 	 */
6909 	if (vi->nrxq > vi->rss_size) {
6910 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
6911 		    "some queues will never receive traffic.\n", vi->nrxq,
6912 		    vi->rss_size);
6913 	} else if (vi->rss_size % vi->nrxq) {
6914 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
6915 		    "expect uneven traffic distribution.\n", vi->nrxq,
6916 		    vi->rss_size);
6917 	}
6918 #ifdef RSS
6919 	if (vi->nrxq != nbuckets) {
6920 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
6921 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
6922 	}
6923 #endif
6924 	if (vi->rss == NULL)
6925 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
6926 		    M_ZERO | M_WAITOK);
6927 	for (i = 0; i < vi->rss_size;) {
6928 #ifdef RSS
6929 		j = rss_get_indirection_to_bucket(i);
6930 		j %= vi->nrxq;
6931 		rxq = &sc->sge.rxq[vi->first_rxq + j];
6932 		vi->rss[i++] = rxq->iq.abs_id;
6933 #else
6934 		for_each_rxq(vi, j, rxq) {
6935 			vi->rss[i++] = rxq->iq.abs_id;
6936 			if (i == vi->rss_size)
6937 				break;
6938 		}
6939 #endif
6940 	}
6941 
6942 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
6943 	    vi->rss, vi->rss_size);
6944 	if (rc != 0) {
6945 		CH_ERR(vi, "rss_config failed: %d\n", rc);
6946 		return (rc);
6947 	}
6948 
6949 #ifdef RSS
6950 	vi->hashen = hashconfig_to_hashen(hashconfig);
6951 
6952 	/*
6953 	 * We may have had to enable some hashes even though the global config
6954 	 * wants them disabled.  This is a potential problem that must be
6955 	 * reported to the user.
6956 	 */
6957 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
6958 
6959 	/*
6960 	 * If we consider only the supported hash types, then the enabled hashes
6961 	 * are a superset of the requested hashes.  In other words, there cannot
6962 	 * be any supported hash that was requested but not enabled, but there
6963 	 * can be hashes that were not requested but had to be enabled.
6964 	 */
6965 	extra &= SUPPORTED_RSS_HASHTYPES;
6966 	MPASS((extra & hashconfig) == 0);
6967 
6968 	if (extra) {
6969 		CH_ALERT(vi,
6970 		    "global RSS config (0x%x) cannot be accommodated.\n",
6971 		    hashconfig);
6972 	}
6973 	if (extra & RSS_HASHTYPE_RSS_IPV4)
6974 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
6975 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
6976 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
6977 	if (extra & RSS_HASHTYPE_RSS_IPV6)
6978 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
6979 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
6980 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
6981 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
6982 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
6983 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
6984 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
6985 #else
6986 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
6987 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
6988 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6989 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
6990 #endif
6991 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
6992 	    0, 0);
6993 	if (rc != 0) {
6994 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
6995 		return (rc);
6996 	}
6997 
6998 	return (0);
6999 }
7000 
7001 int
vi_init(struct vi_info * vi)7002 vi_init(struct vi_info *vi)
7003 {
7004 	int rc;
7005 
7006 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
7007 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
7008 	    ("%s: VI_INIT_DONE already", __func__));
7009 
7010 	rc = vi_full_init(vi);
7011 	if (rc != 0)
7012 		vi_full_uninit(vi);
7013 	else
7014 		vi->flags |= VI_INIT_DONE;
7015 
7016 	return (rc);
7017 }
7018 
7019 /*
7020  * Idempotent.
7021  */
7022 static void
vi_full_uninit(struct vi_info * vi)7023 vi_full_uninit(struct vi_info *vi)
7024 {
7025 
7026 	if (vi->flags & VI_INIT_DONE) {
7027 		quiesce_vi(vi);
7028 		free(vi->rss, M_CXGBE);
7029 		free(vi->nm_rss, M_CXGBE);
7030 	}
7031 
7032 	t4_teardown_vi_queues(vi);
7033 	vi->flags &= ~VI_INIT_DONE;
7034 }
7035 
7036 static void
quiesce_txq(struct sge_txq * txq)7037 quiesce_txq(struct sge_txq *txq)
7038 {
7039 	struct sge_eq *eq = &txq->eq;
7040 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7041 
7042 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7043 	MPASS(!(eq->flags & EQ_ENABLED));
7044 
7045 	/* Wait for the mp_ring to empty. */
7046 	while (!mp_ring_is_idle(txq->r)) {
7047 		mp_ring_check_drainage(txq->r, 4096);
7048 		pause("rquiesce", 1);
7049 	}
7050 	MPASS(txq->txp.npkt == 0);
7051 
7052 	if (eq->flags & EQ_HW_ALLOCATED) {
7053 		/*
7054 		 * Hardware is alive and working normally.  Wait for it to
7055 		 * finish and then wait for the driver to catch up and reclaim
7056 		 * all descriptors.
7057 		 */
7058 		while (spg->cidx != htobe16(eq->pidx))
7059 			pause("equiesce", 1);
7060 		while (eq->cidx != eq->pidx)
7061 			pause("dquiesce", 1);
7062 	} else {
7063 		/*
7064 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7065 		 * descriptors directly.
7066 		 */
7067 		TXQ_LOCK(txq);
7068 		while (eq->cidx != eq->pidx) {
7069 			struct mbuf *m, *nextpkt;
7070 			struct tx_sdesc *txsd;
7071 
7072 			txsd = &txq->sdesc[eq->cidx];
7073 			for (m = txsd->m; m != NULL; m = nextpkt) {
7074 				nextpkt = m->m_nextpkt;
7075 				m->m_nextpkt = NULL;
7076 				m_freem(m);
7077 			}
7078 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7079 		}
7080 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7081 		TXQ_UNLOCK(txq);
7082 	}
7083 }
7084 
7085 static void
quiesce_wrq(struct sge_wrq * wrq)7086 quiesce_wrq(struct sge_wrq *wrq)
7087 {
7088 	struct wrqe *wr;
7089 
7090 	TXQ_LOCK(wrq);
7091 	while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7092 		STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7093 #ifdef INVARIANTS
7094 		wrq->nwr_pending--;
7095 		wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7096 #endif
7097 		free(wr, M_CXGBE);
7098 	}
7099 	MPASS(wrq->nwr_pending == 0);
7100 	MPASS(wrq->ndesc_needed == 0);
7101 	wrq->nwr_pending = 0;
7102 	wrq->ndesc_needed = 0;
7103 	TXQ_UNLOCK(wrq);
7104 }
7105 
7106 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7107 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7108 {
7109 	/* Synchronize with the interrupt handler */
7110 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7111 		pause("iqfree", 1);
7112 
7113 	if (fl != NULL) {
7114 		MPASS(iq->flags & IQ_HAS_FL);
7115 
7116 		mtx_lock(&sc->sfl_lock);
7117 		FL_LOCK(fl);
7118 		fl->flags |= FL_DOOMED;
7119 		FL_UNLOCK(fl);
7120 		callout_stop(&sc->sfl_callout);
7121 		mtx_unlock(&sc->sfl_lock);
7122 
7123 		KASSERT((fl->flags & FL_STARVING) == 0,
7124 		    ("%s: still starving", __func__));
7125 
7126 		/* Release all buffers if hardware is no longer available. */
7127 		if (!(iq->flags & IQ_HW_ALLOCATED))
7128 			free_fl_buffers(sc, fl);
7129 	}
7130 }
7131 
7132 /*
7133  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7134  * that no new work is being enqueued by the hardware or the driver.  That part
7135  * should be arranged before calling this function.
7136  */
7137 static void
quiesce_vi(struct vi_info * vi)7138 quiesce_vi(struct vi_info *vi)
7139 {
7140 	int i;
7141 	struct adapter *sc = vi->adapter;
7142 	struct sge_rxq *rxq;
7143 	struct sge_txq *txq;
7144 #ifdef TCP_OFFLOAD
7145 	struct sge_ofld_rxq *ofld_rxq;
7146 #endif
7147 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7148 	struct sge_ofld_txq *ofld_txq;
7149 #endif
7150 
7151 	if (!(vi->flags & VI_INIT_DONE))
7152 		return;
7153 
7154 	for_each_txq(vi, i, txq) {
7155 		quiesce_txq(txq);
7156 	}
7157 
7158 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7159 	for_each_ofld_txq(vi, i, ofld_txq) {
7160 		quiesce_wrq(&ofld_txq->wrq);
7161 	}
7162 #endif
7163 
7164 	for_each_rxq(vi, i, rxq) {
7165 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7166 	}
7167 
7168 #ifdef TCP_OFFLOAD
7169 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7170 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7171 	}
7172 #endif
7173 }
7174 
7175 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7176 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7177     driver_intr_t *handler, void *arg, char *name)
7178 {
7179 	int rc;
7180 
7181 	irq->rid = rid;
7182 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7183 	    RF_SHAREABLE | RF_ACTIVE);
7184 	if (irq->res == NULL) {
7185 		device_printf(sc->dev,
7186 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7187 		return (ENOMEM);
7188 	}
7189 
7190 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7191 	    NULL, handler, arg, &irq->tag);
7192 	if (rc != 0) {
7193 		device_printf(sc->dev,
7194 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7195 		    rid, name, rc);
7196 	} else if (name)
7197 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7198 
7199 	return (rc);
7200 }
7201 
7202 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7203 t4_free_irq(struct adapter *sc, struct irq *irq)
7204 {
7205 	if (irq->tag)
7206 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7207 	if (irq->res)
7208 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7209 
7210 	bzero(irq, sizeof(*irq));
7211 
7212 	return (0);
7213 }
7214 
7215 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7216 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7217 {
7218 
7219 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7220 	t4_get_regs(sc, buf, regs->len);
7221 }
7222 
7223 #define	A_PL_INDIR_CMD	0x1f8
7224 
7225 #define	S_PL_AUTOINC	31
7226 #define	M_PL_AUTOINC	0x1U
7227 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7228 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7229 
7230 #define	S_PL_VFID	20
7231 #define	M_PL_VFID	0xffU
7232 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7233 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7234 
7235 #define	S_PL_ADDR	0
7236 #define	M_PL_ADDR	0xfffffU
7237 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7238 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7239 
7240 #define	A_PL_INDIR_DATA	0x1fc
7241 
7242 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7243 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7244 {
7245 	u32 stats[2];
7246 
7247 	if (sc->flags & IS_VF) {
7248 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7249 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7250 	} else {
7251 		mtx_assert(&sc->reg_lock, MA_OWNED);
7252 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7253 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7254 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7255 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7256 	}
7257 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7258 }
7259 
7260 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7261 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7262 {
7263 
7264 #define GET_STAT(name) \
7265 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7266 
7267 	if (!(sc->flags & IS_VF))
7268 		mtx_lock(&sc->reg_lock);
7269 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7270 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7271 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7272 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7273 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7274 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7275 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7276 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7277 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7278 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7279 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7280 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7281 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7282 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7283 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7284 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7285 	if (!(sc->flags & IS_VF))
7286 		mtx_unlock(&sc->reg_lock);
7287 
7288 #undef GET_STAT
7289 }
7290 
7291 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7292 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7293 {
7294 	int reg;
7295 
7296 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7297 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7298 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7299 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7300 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7301 }
7302 
7303 static void
vi_refresh_stats(struct vi_info * vi)7304 vi_refresh_stats(struct vi_info *vi)
7305 {
7306 	struct timeval tv;
7307 	const struct timeval interval = {0, 250000};	/* 250ms */
7308 
7309 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7310 
7311 	if (vi->flags & VI_SKIP_STATS)
7312 		return;
7313 
7314 	getmicrotime(&tv);
7315 	timevalsub(&tv, &interval);
7316 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7317 		return;
7318 
7319 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7320 	getmicrotime(&vi->last_refreshed);
7321 }
7322 
7323 static void
cxgbe_refresh_stats(struct vi_info * vi)7324 cxgbe_refresh_stats(struct vi_info *vi)
7325 {
7326 	u_int i, v, tnl_cong_drops, chan_map;
7327 	struct timeval tv;
7328 	const struct timeval interval = {0, 250000};	/* 250ms */
7329 	struct port_info *pi;
7330 	struct adapter *sc;
7331 
7332 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7333 
7334 	if (vi->flags & VI_SKIP_STATS)
7335 		return;
7336 
7337 	getmicrotime(&tv);
7338 	timevalsub(&tv, &interval);
7339 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7340 		return;
7341 
7342 	pi = vi->pi;
7343 	sc = vi->adapter;
7344 	tnl_cong_drops = 0;
7345 	t4_get_port_stats(sc, pi->port_id, &pi->stats);
7346 	chan_map = pi->rx_e_chan_map;
7347 	while (chan_map) {
7348 		i = ffs(chan_map) - 1;
7349 		mtx_lock(&sc->reg_lock);
7350 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7351 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7352 		mtx_unlock(&sc->reg_lock);
7353 		tnl_cong_drops += v;
7354 		chan_map &= ~(1 << i);
7355 	}
7356 	pi->tnl_cong_drops = tnl_cong_drops;
7357 	getmicrotime(&vi->last_refreshed);
7358 }
7359 
7360 static void
cxgbe_tick(void * arg)7361 cxgbe_tick(void *arg)
7362 {
7363 	struct vi_info *vi = arg;
7364 
7365 	MPASS(IS_MAIN_VI(vi));
7366 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7367 
7368 	cxgbe_refresh_stats(vi);
7369 	callout_schedule(&vi->tick, hz);
7370 }
7371 
7372 static void
vi_tick(void * arg)7373 vi_tick(void *arg)
7374 {
7375 	struct vi_info *vi = arg;
7376 
7377 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7378 
7379 	vi_refresh_stats(vi);
7380 	callout_schedule(&vi->tick, hz);
7381 }
7382 
7383 /*
7384  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7385  */
7386 static char *caps_decoder[] = {
7387 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7388 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7389 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7390 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7391 	    "\006HASHFILTER\007ETHOFLD",
7392 	"\20\001TOE",					/* 4: TOE */
7393 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
7394 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7395 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7396 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7397 	    "\007T10DIF"
7398 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7399 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7400 	    "\004TLS_HW",
7401 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7402 		    "\004PO_INITIATOR\005PO_TARGET",
7403 };
7404 
7405 void
t4_sysctls(struct adapter * sc)7406 t4_sysctls(struct adapter *sc)
7407 {
7408 	struct sysctl_ctx_list *ctx = &sc->ctx;
7409 	struct sysctl_oid *oid;
7410 	struct sysctl_oid_list *children, *c0;
7411 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7412 
7413 	/*
7414 	 * dev.t4nex.X.
7415 	 */
7416 	oid = device_get_sysctl_tree(sc->dev);
7417 	c0 = children = SYSCTL_CHILDREN(oid);
7418 
7419 	sc->sc_do_rxcopy = 1;
7420 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7421 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7422 
7423 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7424 	    sc->params.nports, "# of ports");
7425 
7426 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7427 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7428 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7429 	    "available doorbells");
7430 
7431 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7432 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7433 
7434 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7435 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7436 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7437 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7438 
7439 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7440 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7441 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7442 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7443 
7444 	t4_sge_sysctls(sc, ctx, children);
7445 
7446 	sc->lro_timeout = 100;
7447 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7448 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7449 
7450 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7451 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7452 
7453 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7454 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7455 
7456 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7457 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7458 
7459 	if (sc->flags & IS_VF)
7460 		return;
7461 
7462 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7463 	    NULL, chip_rev(sc), "chip hardware revision");
7464 
7465 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7466 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7467 
7468 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7469 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7470 
7471 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7472 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7473 
7474 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7475 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7476 
7477 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7478 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7479 
7480 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7481 	    sc->er_version, 0, "expansion ROM version");
7482 
7483 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7484 	    sc->bs_version, 0, "bootstrap firmware version");
7485 
7486 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7487 	    NULL, sc->params.scfg_vers, "serial config version");
7488 
7489 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7490 	    NULL, sc->params.vpd_vers, "VPD version");
7491 
7492 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7493 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7494 
7495 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7496 	    sc->cfcsum, "config file checksum");
7497 
7498 #define SYSCTL_CAP(name, n, text) \
7499 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7500 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7501 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7502 	    "available " text " capabilities")
7503 
7504 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7505 	SYSCTL_CAP(linkcaps, 1, "link");
7506 	SYSCTL_CAP(switchcaps, 2, "switch");
7507 	SYSCTL_CAP(niccaps, 3, "NIC");
7508 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7509 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7510 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7511 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7512 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7513 #undef SYSCTL_CAP
7514 
7515 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7516 	    NULL, sc->tids.nftids, "number of filters");
7517 
7518 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7519 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7520 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7521 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7522 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7523 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7524 
7525 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg",
7526 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7527 	    sysctl_loadavg, "A",
7528 	    "microprocessor load averages (debug firmwares only)");
7529 
7530 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7531 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7532 	    "I", "core Vdd (in mV)");
7533 
7534 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7535 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7536 	    sysctl_cpus, "A", "local CPUs");
7537 
7538 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7539 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7540 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7541 
7542 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7543 	    &sc->swintr, 0, "software triggered interrupts");
7544 
7545 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7546 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7547 	    "1 = reset adapter, 0 = zero reset counter");
7548 
7549 	/*
7550 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7551 	 */
7552 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7553 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
7554 	    "logs and miscellaneous information");
7555 	children = SYSCTL_CHILDREN(oid);
7556 
7557 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
7558 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7559 	    sysctl_cctrl, "A", "congestion control");
7560 
7561 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
7562 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7563 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
7564 
7565 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
7566 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7567 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
7568 
7569 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
7570 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7571 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
7572 
7573 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
7574 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 3,
7575 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
7576 
7577 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
7578 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 4,
7579 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
7580 
7581 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
7582 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 5,
7583 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
7584 
7585 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
7586 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7587 	    sysctl_cim_la, "A", "CIM logic analyzer");
7588 
7589 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
7590 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7591 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7592 
7593 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
7594 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7595 	    0 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
7596 
7597 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
7598 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7599 	    1 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
7600 
7601 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
7602 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7603 	    2 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
7604 
7605 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
7606 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7607 	    3 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
7608 
7609 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
7610 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7611 	    4 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
7612 
7613 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
7614 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7615 	    5 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
7616 
7617 	if (chip_id(sc) > CHELSIO_T4) {
7618 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
7619 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7620 		    6 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7621 		    "CIM OBQ 6 (SGE0-RX)");
7622 
7623 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
7624 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7625 		    7 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7626 		    "CIM OBQ 7 (SGE1-RX)");
7627 	}
7628 
7629 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
7630 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7631 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7632 
7633 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
7634 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7635 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
7636 
7637 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
7638 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7639 	    sysctl_cpl_stats, "A", "CPL statistics");
7640 
7641 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
7642 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7643 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
7644 
7645 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
7646 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7647 	    sysctl_tid_stats, "A", "tid stats");
7648 
7649 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
7650 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7651 	    sysctl_devlog, "A", "firmware's device log");
7652 
7653 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
7654 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7655 	    sysctl_fcoe_stats, "A", "FCoE statistics");
7656 
7657 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
7658 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7659 	    sysctl_hw_sched, "A", "hardware scheduler ");
7660 
7661 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
7662 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7663 	    sysctl_l2t, "A", "hardware L2 table");
7664 
7665 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
7666 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7667 	    sysctl_smt, "A", "hardware source MAC table");
7668 
7669 #ifdef INET6
7670 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
7671 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7672 	    sysctl_clip, "A", "active CLIP table entries");
7673 #endif
7674 
7675 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
7676 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7677 	    sysctl_lb_stats, "A", "loopback statistics");
7678 
7679 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
7680 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7681 	    sysctl_meminfo, "A", "memory regions");
7682 
7683 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
7684 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7685 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
7686 	    "A", "MPS TCAM entries");
7687 
7688 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
7689 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7690 	    sysctl_path_mtus, "A", "path MTUs");
7691 
7692 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
7693 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7694 	    sysctl_pm_stats, "A", "PM statistics");
7695 
7696 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
7697 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7698 	    sysctl_rdma_stats, "A", "RDMA statistics");
7699 
7700 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
7701 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7702 	    sysctl_tcp_stats, "A", "TCP statistics");
7703 
7704 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
7705 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7706 	    sysctl_tids, "A", "TID information");
7707 
7708 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
7709 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7710 	    sysctl_tp_err_stats, "A", "TP error statistics");
7711 
7712 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
7713 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7714 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
7715 
7716 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
7717 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7718 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
7719 
7720 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
7721 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7722 	    sysctl_tp_la, "A", "TP logic analyzer");
7723 
7724 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
7725 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7726 	    sysctl_tx_rate, "A", "Tx rate");
7727 
7728 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
7729 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7730 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
7731 
7732 	if (chip_id(sc) >= CHELSIO_T5) {
7733 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
7734 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7735 		    sysctl_wcwr_stats, "A", "write combined work requests");
7736 	}
7737 
7738 #ifdef KERN_TLS
7739 	if (is_ktls(sc)) {
7740 		/*
7741 		 * dev.t4nex.0.tls.
7742 		 */
7743 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
7744 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
7745 		children = SYSCTL_CHILDREN(oid);
7746 
7747 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
7748 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
7749 		    "keys in work requests (1) or attempt to store TLS keys "
7750 		    "in card memory.");
7751 
7752 		if (is_t6(sc))
7753 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
7754 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
7755 			    "combine TCB field updates with TLS record work "
7756 			    "requests.");
7757 	}
7758 #endif
7759 
7760 #ifdef TCP_OFFLOAD
7761 	if (is_offload(sc)) {
7762 		int i;
7763 		char s[4];
7764 
7765 		/*
7766 		 * dev.t4nex.X.toe.
7767 		 */
7768 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
7769 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
7770 		children = SYSCTL_CHILDREN(oid);
7771 
7772 		sc->tt.cong_algorithm = -1;
7773 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
7774 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
7775 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
7776 		    "3 = highspeed)");
7777 
7778 		sc->tt.sndbuf = -1;
7779 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
7780 		    &sc->tt.sndbuf, 0, "hardware send buffer");
7781 
7782 		sc->tt.ddp = 0;
7783 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
7784 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
7785 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
7786 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
7787 
7788 		sc->tt.rx_coalesce = -1;
7789 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
7790 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
7791 
7792 		sc->tt.tls = 1;
7793 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
7794 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
7795 		    "Inline TLS allowed");
7796 
7797 		sc->tt.tx_align = -1;
7798 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
7799 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
7800 
7801 		sc->tt.tx_zcopy = 0;
7802 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
7803 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
7804 		    "Enable zero-copy aio_write(2)");
7805 
7806 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
7807 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7808 		    "cop_managed_offloading", CTLFLAG_RW,
7809 		    &sc->tt.cop_managed_offloading, 0,
7810 		    "COP (Connection Offload Policy) controls all TOE offload");
7811 
7812 		sc->tt.autorcvbuf_inc = 16 * 1024;
7813 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
7814 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
7815 		    "autorcvbuf increment");
7816 
7817 		sc->tt.update_hc_on_pmtu_change = 1;
7818 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7819 		    "update_hc_on_pmtu_change", CTLFLAG_RW,
7820 		    &sc->tt.update_hc_on_pmtu_change, 0,
7821 		    "Update hostcache entry if the PMTU changes");
7822 
7823 		sc->tt.iso = 1;
7824 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
7825 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
7826 
7827 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
7828 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7829 		    sysctl_tp_tick, "A", "TP timer tick (us)");
7830 
7831 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
7832 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7833 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
7834 
7835 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
7836 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7837 		    sysctl_tp_tick, "A", "DACK tick (us)");
7838 
7839 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
7840 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7841 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
7842 
7843 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
7844 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7845 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
7846 		    "Minimum retransmit interval (us)");
7847 
7848 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
7849 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7850 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
7851 		    "Maximum retransmit interval (us)");
7852 
7853 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
7854 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7855 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
7856 		    "Persist timer min (us)");
7857 
7858 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
7859 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7860 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
7861 		    "Persist timer max (us)");
7862 
7863 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
7864 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7865 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
7866 		    "Keepalive idle timer (us)");
7867 
7868 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
7869 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7870 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
7871 		    "Keepalive interval timer (us)");
7872 
7873 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
7874 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7875 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
7876 
7877 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
7878 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7879 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
7880 		    "FINWAIT2 timer (us)");
7881 
7882 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
7883 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7884 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
7885 		    "Number of SYN retransmissions before abort");
7886 
7887 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
7888 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7889 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
7890 		    "Number of retransmissions before abort");
7891 
7892 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
7893 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7894 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
7895 		    "Number of keepalive probes before abort");
7896 
7897 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
7898 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7899 		    "TOE retransmit backoffs");
7900 		children = SYSCTL_CHILDREN(oid);
7901 		for (i = 0; i < 16; i++) {
7902 			snprintf(s, sizeof(s), "%u", i);
7903 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
7904 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7905 			    i, sysctl_tp_backoff, "IU",
7906 			    "TOE retransmit backoff");
7907 		}
7908 	}
7909 #endif
7910 }
7911 
7912 void
vi_sysctls(struct vi_info * vi)7913 vi_sysctls(struct vi_info *vi)
7914 {
7915 	struct sysctl_ctx_list *ctx = &vi->ctx;
7916 	struct sysctl_oid *oid;
7917 	struct sysctl_oid_list *children;
7918 
7919 	/*
7920 	 * dev.v?(cxgbe|cxl).X.
7921 	 */
7922 	oid = device_get_sysctl_tree(vi->dev);
7923 	children = SYSCTL_CHILDREN(oid);
7924 
7925 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
7926 	    vi->viid, "VI identifer");
7927 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
7928 	    &vi->nrxq, 0, "# of rx queues");
7929 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
7930 	    &vi->ntxq, 0, "# of tx queues");
7931 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
7932 	    &vi->first_rxq, 0, "index of first rx queue");
7933 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
7934 	    &vi->first_txq, 0, "index of first tx queue");
7935 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
7936 	    vi->rss_base, "start of RSS indirection table");
7937 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
7938 	    vi->rss_size, "size of RSS indirection table");
7939 
7940 	if (IS_MAIN_VI(vi)) {
7941 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
7942 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7943 		    sysctl_noflowq, "IU",
7944 		    "Reserve queue 0 for non-flowid packets");
7945 	}
7946 
7947 	if (vi->adapter->flags & IS_VF) {
7948 		MPASS(vi->flags & TX_USES_VM_WR);
7949 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
7950 		    NULL, 1, "use VM work requests for transmit");
7951 	} else {
7952 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
7953 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7954 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
7955 	}
7956 
7957 #ifdef TCP_OFFLOAD
7958 	if (vi->nofldrxq != 0) {
7959 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
7960 		    &vi->nofldrxq, 0,
7961 		    "# of rx queues for offloaded TCP connections");
7962 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
7963 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
7964 		    "index of first TOE rx queue");
7965 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
7966 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7967 		    sysctl_holdoff_tmr_idx_ofld, "I",
7968 		    "holdoff timer index for TOE queues");
7969 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
7970 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7971 		    sysctl_holdoff_pktc_idx_ofld, "I",
7972 		    "holdoff packet counter index for TOE queues");
7973 	}
7974 #endif
7975 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7976 	if (vi->nofldtxq != 0) {
7977 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
7978 		    &vi->nofldtxq, 0,
7979 		    "# of tx queues for TOE/ETHOFLD");
7980 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
7981 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
7982 		    "index of first TOE/ETHOFLD tx queue");
7983 	}
7984 #endif
7985 #ifdef DEV_NETMAP
7986 	if (vi->nnmrxq != 0) {
7987 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
7988 		    &vi->nnmrxq, 0, "# of netmap rx queues");
7989 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
7990 		    &vi->nnmtxq, 0, "# of netmap tx queues");
7991 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
7992 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
7993 		    "index of first netmap rx queue");
7994 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
7995 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
7996 		    "index of first netmap tx queue");
7997 	}
7998 #endif
7999 
8000 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
8001 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8002 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
8003 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
8004 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8005 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
8006 
8007 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
8008 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8009 	    sysctl_qsize_rxq, "I", "rx queue size");
8010 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
8011 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8012 	    sysctl_qsize_txq, "I", "tx queue size");
8013 }
8014 
8015 static void
cxgbe_sysctls(struct port_info * pi)8016 cxgbe_sysctls(struct port_info *pi)
8017 {
8018 	struct sysctl_ctx_list *ctx = &pi->ctx;
8019 	struct sysctl_oid *oid;
8020 	struct sysctl_oid_list *children, *children2;
8021 	struct adapter *sc = pi->adapter;
8022 	int i;
8023 	char name[16];
8024 	static char *tc_flags = {"\20\1USER"};
8025 
8026 	/*
8027 	 * dev.cxgbe.X.
8028 	 */
8029 	oid = device_get_sysctl_tree(pi->dev);
8030 	children = SYSCTL_CHILDREN(oid);
8031 
8032 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8033 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8034 	    sysctl_linkdnrc, "A", "reason why link is down");
8035 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8036 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8037 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8038 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8039 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8040 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8041 		    sysctl_btphy, "I", "PHY firmware version");
8042 	}
8043 
8044 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8045 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8046 	    sysctl_pause_settings, "A",
8047 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8048 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8049 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8050 	    "FEC in use on the link");
8051 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8052 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8053 	    sysctl_requested_fec, "A",
8054 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8055 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8056 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8057 	    "FEC recommended by the cable/transceiver");
8058 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8059 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8060 	    sysctl_autoneg, "I",
8061 	    "autonegotiation (-1 = not supported)");
8062 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8063 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8064 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8065 
8066 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8067 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8068 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8069 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8070 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8071 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8072 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8073 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8074 
8075 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8076 	    port_top_speed(pi), "max speed (in Gbps)");
8077 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8078 	    pi->mps_bg_map, "MPS buffer group map");
8079 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8080 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8081 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8082 	    pi->tx_chan, "TP tx c-channel");
8083 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8084 	    pi->rx_chan, "TP rx c-channel");
8085 
8086 	if (sc->flags & IS_VF)
8087 		return;
8088 
8089 	/*
8090 	 * dev.(cxgbe|cxl).X.tc.
8091 	 */
8092 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8093 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8094 	    "Tx scheduler traffic classes (cl_rl)");
8095 	children2 = SYSCTL_CHILDREN(oid);
8096 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8097 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8098 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8099 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8100 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8101 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8102 	for (i = 0; i < sc->params.nsched_cls; i++) {
8103 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8104 
8105 		snprintf(name, sizeof(name), "%d", i);
8106 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8107 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8108 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8109 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8110 		    CTLFLAG_RD, &tc->state, 0, "current state");
8111 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8112 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8113 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8114 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8115 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8116 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8117 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8118 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8119 		    "traffic class parameters");
8120 	}
8121 
8122 	/*
8123 	 * dev.cxgbe.X.stats.
8124 	 */
8125 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8126 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8127 	children = SYSCTL_CHILDREN(oid);
8128 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8129 	    &pi->tx_parse_error, 0,
8130 	    "# of tx packets with invalid length or # of segments");
8131 
8132 #define T4_REGSTAT(name, stat, desc) \
8133     SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8134 	CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8135 	t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8136         sysctl_handle_t4_reg64, "QU", desc)
8137 
8138 /* We get these from port_stats and they may be stale by up to 1s */
8139 #define T4_PORTSTAT(name, desc) \
8140 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
8141 	    &pi->stats.name, desc)
8142 
8143 	T4_REGSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8144 	T4_REGSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8145 	T4_REGSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8146 	T4_REGSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8147 	T4_REGSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8148 	T4_REGSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8149 	T4_REGSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8150 	T4_REGSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8151 	T4_REGSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8152 	T4_REGSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8153 	T4_REGSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8154 	T4_REGSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8155 	T4_REGSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8156 	T4_REGSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8157 	T4_REGSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8158 	T4_REGSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8159 	T4_REGSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8160 	T4_REGSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8161 	T4_REGSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8162 	T4_REGSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8163 	T4_REGSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8164 	T4_REGSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8165 	T4_REGSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8166 
8167 	T4_REGSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8168 	T4_REGSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8169 	T4_REGSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8170 	T4_REGSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8171 	T4_REGSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8172 	T4_REGSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8173 	T4_REGSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8174 	if (is_t6(sc)) {
8175 		T4_PORTSTAT(rx_fcs_err,
8176 		    "# of frames received with bad FCS since last link up");
8177 	} else {
8178 		T4_REGSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8179 		    "# of frames received with bad FCS");
8180 	}
8181 	T4_REGSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8182 	T4_REGSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8183 	T4_REGSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8184 	T4_REGSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8185 	T4_REGSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8186 	T4_REGSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8187 	T4_REGSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8188 	T4_REGSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8189 	T4_REGSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8190 	T4_REGSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8191 	T4_REGSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8192 	T4_REGSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8193 	T4_REGSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8194 	T4_REGSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8195 	T4_REGSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8196 	T4_REGSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8197 	T4_REGSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8198 	T4_REGSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8199 	T4_REGSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8200 
8201 	T4_PORTSTAT(rx_ovflow0, "# drops due to buffer-group 0 overflows");
8202 	T4_PORTSTAT(rx_ovflow1, "# drops due to buffer-group 1 overflows");
8203 	T4_PORTSTAT(rx_ovflow2, "# drops due to buffer-group 2 overflows");
8204 	T4_PORTSTAT(rx_ovflow3, "# drops due to buffer-group 3 overflows");
8205 	T4_PORTSTAT(rx_trunc0, "# of buffer-group 0 truncated packets");
8206 	T4_PORTSTAT(rx_trunc1, "# of buffer-group 1 truncated packets");
8207 	T4_PORTSTAT(rx_trunc2, "# of buffer-group 2 truncated packets");
8208 	T4_PORTSTAT(rx_trunc3, "# of buffer-group 3 truncated packets");
8209 
8210 #undef T4_REGSTAT
8211 #undef T4_PORTSTAT
8212 }
8213 
8214 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8215 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8216 {
8217 	int rc, *i, space = 0;
8218 	struct sbuf sb;
8219 
8220 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8221 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8222 		if (space)
8223 			sbuf_printf(&sb, " ");
8224 		sbuf_printf(&sb, "%d", *i);
8225 		space = 1;
8226 	}
8227 	rc = sbuf_finish(&sb);
8228 	sbuf_delete(&sb);
8229 	return (rc);
8230 }
8231 
8232 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8233 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8234 {
8235 	int rc;
8236 	struct sbuf *sb;
8237 
8238 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8239 	if (sb == NULL)
8240 		return (ENOMEM);
8241 
8242 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8243 	rc = sbuf_finish(sb);
8244 	sbuf_delete(sb);
8245 
8246 	return (rc);
8247 }
8248 
8249 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8250 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8251 {
8252 	int rc;
8253 	struct sbuf *sb;
8254 
8255 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8256 	if (sb == NULL)
8257 		return (ENOMEM);
8258 
8259 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8260 	rc = sbuf_finish(sb);
8261 	sbuf_delete(sb);
8262 
8263 	return (rc);
8264 }
8265 
8266 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8267 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8268 {
8269 	struct port_info *pi = arg1;
8270 	int op = arg2;
8271 	struct adapter *sc = pi->adapter;
8272 	u_int v;
8273 	int rc;
8274 
8275 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8276 	if (rc)
8277 		return (rc);
8278 	if (hw_off_limits(sc))
8279 		rc = ENXIO;
8280 	else {
8281 		/* XXX: magic numbers */
8282 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8283 		    op ? 0x20 : 0xc820, &v);
8284 	}
8285 	end_synchronized_op(sc, 0);
8286 	if (rc)
8287 		return (rc);
8288 	if (op == 0)
8289 		v /= 256;
8290 
8291 	rc = sysctl_handle_int(oidp, &v, 0, req);
8292 	return (rc);
8293 }
8294 
8295 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8296 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8297 {
8298 	struct vi_info *vi = arg1;
8299 	int rc, val;
8300 
8301 	val = vi->rsrv_noflowq;
8302 	rc = sysctl_handle_int(oidp, &val, 0, req);
8303 	if (rc != 0 || req->newptr == NULL)
8304 		return (rc);
8305 
8306 	if ((val >= 1) && (vi->ntxq > 1))
8307 		vi->rsrv_noflowq = 1;
8308 	else
8309 		vi->rsrv_noflowq = 0;
8310 
8311 	return (rc);
8312 }
8313 
8314 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8315 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8316 {
8317 	struct vi_info *vi = arg1;
8318 	struct adapter *sc = vi->adapter;
8319 	int rc, val, i;
8320 
8321 	MPASS(!(sc->flags & IS_VF));
8322 
8323 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8324 	rc = sysctl_handle_int(oidp, &val, 0, req);
8325 	if (rc != 0 || req->newptr == NULL)
8326 		return (rc);
8327 
8328 	if (val != 0 && val != 1)
8329 		return (EINVAL);
8330 
8331 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8332 	    "t4txvm");
8333 	if (rc)
8334 		return (rc);
8335 	if (hw_off_limits(sc))
8336 		rc = ENXIO;
8337 	else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8338 		/*
8339 		 * We don't want parse_pkt to run with one setting (VF or PF)
8340 		 * and then eth_tx to see a different setting but still use
8341 		 * stale information calculated by parse_pkt.
8342 		 */
8343 		rc = EBUSY;
8344 	} else {
8345 		struct port_info *pi = vi->pi;
8346 		struct sge_txq *txq;
8347 		uint32_t ctrl0;
8348 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8349 
8350 		if (val) {
8351 			vi->flags |= TX_USES_VM_WR;
8352 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8353 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8354 			    V_TXPKT_INTF(pi->tx_chan));
8355 			if (!(sc->flags & IS_VF))
8356 				npkt--;
8357 		} else {
8358 			vi->flags &= ~TX_USES_VM_WR;
8359 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8360 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8361 			    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
8362 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8363 		}
8364 		for_each_txq(vi, i, txq) {
8365 			txq->cpl_ctrl0 = ctrl0;
8366 			txq->txp.max_npkt = npkt;
8367 		}
8368 	}
8369 	end_synchronized_op(sc, LOCK_HELD);
8370 	return (rc);
8371 }
8372 
8373 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8374 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8375 {
8376 	struct vi_info *vi = arg1;
8377 	struct adapter *sc = vi->adapter;
8378 	int idx, rc, i;
8379 	struct sge_rxq *rxq;
8380 	uint8_t v;
8381 
8382 	idx = vi->tmr_idx;
8383 
8384 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8385 	if (rc != 0 || req->newptr == NULL)
8386 		return (rc);
8387 
8388 	if (idx < 0 || idx >= SGE_NTIMERS)
8389 		return (EINVAL);
8390 
8391 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8392 	    "t4tmr");
8393 	if (rc)
8394 		return (rc);
8395 
8396 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8397 	for_each_rxq(vi, i, rxq) {
8398 #ifdef atomic_store_rel_8
8399 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8400 #else
8401 		rxq->iq.intr_params = v;
8402 #endif
8403 	}
8404 	vi->tmr_idx = idx;
8405 
8406 	end_synchronized_op(sc, LOCK_HELD);
8407 	return (0);
8408 }
8409 
8410 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8411 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8412 {
8413 	struct vi_info *vi = arg1;
8414 	struct adapter *sc = vi->adapter;
8415 	int idx, rc;
8416 
8417 	idx = vi->pktc_idx;
8418 
8419 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8420 	if (rc != 0 || req->newptr == NULL)
8421 		return (rc);
8422 
8423 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8424 		return (EINVAL);
8425 
8426 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8427 	    "t4pktc");
8428 	if (rc)
8429 		return (rc);
8430 
8431 	if (vi->flags & VI_INIT_DONE)
8432 		rc = EBUSY; /* cannot be changed once the queues are created */
8433 	else
8434 		vi->pktc_idx = idx;
8435 
8436 	end_synchronized_op(sc, LOCK_HELD);
8437 	return (rc);
8438 }
8439 
8440 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8441 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8442 {
8443 	struct vi_info *vi = arg1;
8444 	struct adapter *sc = vi->adapter;
8445 	int qsize, rc;
8446 
8447 	qsize = vi->qsize_rxq;
8448 
8449 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8450 	if (rc != 0 || req->newptr == NULL)
8451 		return (rc);
8452 
8453 	if (qsize < 128 || (qsize & 7))
8454 		return (EINVAL);
8455 
8456 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8457 	    "t4rxqs");
8458 	if (rc)
8459 		return (rc);
8460 
8461 	if (vi->flags & VI_INIT_DONE)
8462 		rc = EBUSY; /* cannot be changed once the queues are created */
8463 	else
8464 		vi->qsize_rxq = qsize;
8465 
8466 	end_synchronized_op(sc, LOCK_HELD);
8467 	return (rc);
8468 }
8469 
8470 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8471 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8472 {
8473 	struct vi_info *vi = arg1;
8474 	struct adapter *sc = vi->adapter;
8475 	int qsize, rc;
8476 
8477 	qsize = vi->qsize_txq;
8478 
8479 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8480 	if (rc != 0 || req->newptr == NULL)
8481 		return (rc);
8482 
8483 	if (qsize < 128 || qsize > 65536)
8484 		return (EINVAL);
8485 
8486 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8487 	    "t4txqs");
8488 	if (rc)
8489 		return (rc);
8490 
8491 	if (vi->flags & VI_INIT_DONE)
8492 		rc = EBUSY; /* cannot be changed once the queues are created */
8493 	else
8494 		vi->qsize_txq = qsize;
8495 
8496 	end_synchronized_op(sc, LOCK_HELD);
8497 	return (rc);
8498 }
8499 
8500 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8501 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8502 {
8503 	struct port_info *pi = arg1;
8504 	struct adapter *sc = pi->adapter;
8505 	struct link_config *lc = &pi->link_cfg;
8506 	int rc;
8507 
8508 	if (req->newptr == NULL) {
8509 		struct sbuf *sb;
8510 		static char *bits = "\20\1RX\2TX\3AUTO";
8511 
8512 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8513 		if (sb == NULL)
8514 			return (ENOMEM);
8515 
8516 		if (lc->link_ok) {
8517 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8518 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8519 		} else {
8520 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8521 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8522 		}
8523 		rc = sbuf_finish(sb);
8524 		sbuf_delete(sb);
8525 	} else {
8526 		char s[2];
8527 		int n;
8528 
8529 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8530 		    PAUSE_AUTONEG));
8531 		s[1] = 0;
8532 
8533 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8534 		if (rc != 0)
8535 			return(rc);
8536 
8537 		if (s[1] != 0)
8538 			return (EINVAL);
8539 		if (s[0] < '0' || s[0] > '9')
8540 			return (EINVAL);	/* not a number */
8541 		n = s[0] - '0';
8542 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8543 			return (EINVAL);	/* some other bit is set too */
8544 
8545 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8546 		    "t4PAUSE");
8547 		if (rc)
8548 			return (rc);
8549 		if (!hw_off_limits(sc)) {
8550 			PORT_LOCK(pi);
8551 			lc->requested_fc = n;
8552 			fixup_link_config(pi);
8553 			if (pi->up_vis > 0)
8554 				rc = apply_link_config(pi);
8555 			set_current_media(pi);
8556 			PORT_UNLOCK(pi);
8557 		}
8558 		end_synchronized_op(sc, 0);
8559 	}
8560 
8561 	return (rc);
8562 }
8563 
8564 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8565 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8566 {
8567 	struct port_info *pi = arg1;
8568 	struct link_config *lc = &pi->link_cfg;
8569 	int rc;
8570 	struct sbuf *sb;
8571 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2";
8572 
8573 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8574 	if (sb == NULL)
8575 		return (ENOMEM);
8576 	if (lc->link_ok)
8577 		sbuf_printf(sb, "%b", lc->fec, bits);
8578 	else
8579 		sbuf_printf(sb, "no link");
8580 	rc = sbuf_finish(sb);
8581 	sbuf_delete(sb);
8582 
8583 	return (rc);
8584 }
8585 
8586 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)8587 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8588 {
8589 	struct port_info *pi = arg1;
8590 	struct adapter *sc = pi->adapter;
8591 	struct link_config *lc = &pi->link_cfg;
8592 	int rc;
8593 	int8_t old;
8594 
8595 	if (req->newptr == NULL) {
8596 		struct sbuf *sb;
8597 		static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2"
8598 		    "\5RSVD3\6auto\7module";
8599 
8600 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8601 		if (sb == NULL)
8602 			return (ENOMEM);
8603 
8604 		sbuf_printf(sb, "%b", lc->requested_fec, bits);
8605 		rc = sbuf_finish(sb);
8606 		sbuf_delete(sb);
8607 	} else {
8608 		char s[8];
8609 		int n;
8610 
8611 		snprintf(s, sizeof(s), "%d",
8612 		    lc->requested_fec == FEC_AUTO ? -1 :
8613 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
8614 
8615 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8616 		if (rc != 0)
8617 			return(rc);
8618 
8619 		n = strtol(&s[0], NULL, 0);
8620 		if (n < 0 || n & FEC_AUTO)
8621 			n = FEC_AUTO;
8622 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
8623 			return (EINVAL);/* some other bit is set too */
8624 
8625 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8626 		    "t4reqf");
8627 		if (rc)
8628 			return (rc);
8629 		PORT_LOCK(pi);
8630 		old = lc->requested_fec;
8631 		if (n == FEC_AUTO)
8632 			lc->requested_fec = FEC_AUTO;
8633 		else if (n == 0 || n == FEC_NONE)
8634 			lc->requested_fec = FEC_NONE;
8635 		else {
8636 			if ((lc->pcaps |
8637 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
8638 			    lc->pcaps) {
8639 				rc = ENOTSUP;
8640 				goto done;
8641 			}
8642 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
8643 			    FEC_MODULE);
8644 		}
8645 		if (!hw_off_limits(sc)) {
8646 			fixup_link_config(pi);
8647 			if (pi->up_vis > 0) {
8648 				rc = apply_link_config(pi);
8649 				if (rc != 0) {
8650 					lc->requested_fec = old;
8651 					if (rc == FW_EPROTO)
8652 						rc = ENOTSUP;
8653 				}
8654 			}
8655 		}
8656 done:
8657 		PORT_UNLOCK(pi);
8658 		end_synchronized_op(sc, 0);
8659 	}
8660 
8661 	return (rc);
8662 }
8663 
8664 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)8665 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
8666 {
8667 	struct port_info *pi = arg1;
8668 	struct adapter *sc = pi->adapter;
8669 	struct link_config *lc = &pi->link_cfg;
8670 	int rc;
8671 	int8_t fec;
8672 	struct sbuf *sb;
8673 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2\5RSVD3";
8674 
8675 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8676 	if (sb == NULL)
8677 		return (ENOMEM);
8678 
8679 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
8680 		rc = EBUSY;
8681 		goto done;
8682 	}
8683 	if (hw_off_limits(sc)) {
8684 		rc = ENXIO;
8685 		goto done;
8686 	}
8687 	PORT_LOCK(pi);
8688 	if (pi->up_vis == 0) {
8689 		/*
8690 		 * If all the interfaces are administratively down the firmware
8691 		 * does not report transceiver changes.  Refresh port info here.
8692 		 * This is the only reason we have a synchronized op in this
8693 		 * function.  Just PORT_LOCK would have been enough otherwise.
8694 		 */
8695 		t4_update_port_info(pi);
8696 	}
8697 
8698 	fec = lc->fec_hint;
8699 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
8700 	    !fec_supported(lc->pcaps)) {
8701 		PORT_UNLOCK(pi);
8702 		sbuf_printf(sb, "n/a");
8703 	} else {
8704 		if (fec == 0)
8705 			fec = FEC_NONE;
8706 		PORT_UNLOCK(pi);
8707 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, bits);
8708 	}
8709 	rc = sbuf_finish(sb);
8710 done:
8711 	sbuf_delete(sb);
8712 	end_synchronized_op(sc, 0);
8713 
8714 	return (rc);
8715 }
8716 
8717 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)8718 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
8719 {
8720 	struct port_info *pi = arg1;
8721 	struct adapter *sc = pi->adapter;
8722 	struct link_config *lc = &pi->link_cfg;
8723 	int rc, val;
8724 
8725 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
8726 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
8727 	else
8728 		val = -1;
8729 	rc = sysctl_handle_int(oidp, &val, 0, req);
8730 	if (rc != 0 || req->newptr == NULL)
8731 		return (rc);
8732 	if (val == 0)
8733 		val = AUTONEG_DISABLE;
8734 	else if (val == 1)
8735 		val = AUTONEG_ENABLE;
8736 	else
8737 		val = AUTONEG_AUTO;
8738 
8739 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8740 	    "t4aneg");
8741 	if (rc)
8742 		return (rc);
8743 	PORT_LOCK(pi);
8744 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
8745 		rc = ENOTSUP;
8746 		goto done;
8747 	}
8748 	lc->requested_aneg = val;
8749 	if (!hw_off_limits(sc)) {
8750 		fixup_link_config(pi);
8751 		if (pi->up_vis > 0)
8752 			rc = apply_link_config(pi);
8753 		set_current_media(pi);
8754 	}
8755 done:
8756 	PORT_UNLOCK(pi);
8757 	end_synchronized_op(sc, 0);
8758 	return (rc);
8759 }
8760 
8761 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)8762 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
8763 {
8764 	struct port_info *pi = arg1;
8765 	struct adapter *sc = pi->adapter;
8766 	struct link_config *lc = &pi->link_cfg;
8767 	int rc, val;
8768 
8769 	val = lc->force_fec;
8770 	MPASS(val >= -1 && val <= 1);
8771 	rc = sysctl_handle_int(oidp, &val, 0, req);
8772 	if (rc != 0 || req->newptr == NULL)
8773 		return (rc);
8774 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
8775 		return (ENOTSUP);
8776 	if (val < -1 || val > 1)
8777 		return (EINVAL);
8778 
8779 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
8780 	if (rc)
8781 		return (rc);
8782 	PORT_LOCK(pi);
8783 	lc->force_fec = val;
8784 	if (!hw_off_limits(sc)) {
8785 		fixup_link_config(pi);
8786 		if (pi->up_vis > 0)
8787 			rc = apply_link_config(pi);
8788 	}
8789 	PORT_UNLOCK(pi);
8790 	end_synchronized_op(sc, 0);
8791 	return (rc);
8792 }
8793 
8794 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)8795 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
8796 {
8797 	struct adapter *sc = arg1;
8798 	int rc, reg = arg2;
8799 	uint64_t val;
8800 
8801 	mtx_lock(&sc->reg_lock);
8802 	if (hw_off_limits(sc))
8803 		rc = ENXIO;
8804 	else {
8805 		rc = 0;
8806 		val = t4_read_reg64(sc, reg);
8807 	}
8808 	mtx_unlock(&sc->reg_lock);
8809 	if (rc == 0)
8810 		rc = sysctl_handle_64(oidp, &val, 0, req);
8811 	return (rc);
8812 }
8813 
8814 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)8815 sysctl_temperature(SYSCTL_HANDLER_ARGS)
8816 {
8817 	struct adapter *sc = arg1;
8818 	int rc, t;
8819 	uint32_t param, val;
8820 
8821 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
8822 	if (rc)
8823 		return (rc);
8824 	if (hw_off_limits(sc))
8825 		rc = ENXIO;
8826 	else {
8827 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8828 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8829 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
8830 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8831 	}
8832 	end_synchronized_op(sc, 0);
8833 	if (rc)
8834 		return (rc);
8835 
8836 	/* unknown is returned as 0 but we display -1 in that case */
8837 	t = val == 0 ? -1 : val;
8838 
8839 	rc = sysctl_handle_int(oidp, &t, 0, req);
8840 	return (rc);
8841 }
8842 
8843 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)8844 sysctl_vdd(SYSCTL_HANDLER_ARGS)
8845 {
8846 	struct adapter *sc = arg1;
8847 	int rc;
8848 	uint32_t param, val;
8849 
8850 	if (sc->params.core_vdd == 0) {
8851 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8852 		    "t4vdd");
8853 		if (rc)
8854 			return (rc);
8855 		if (hw_off_limits(sc))
8856 			rc = ENXIO;
8857 		else {
8858 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8859 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8860 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
8861 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
8862 			    &param, &val);
8863 		}
8864 		end_synchronized_op(sc, 0);
8865 		if (rc)
8866 			return (rc);
8867 		sc->params.core_vdd = val;
8868 	}
8869 
8870 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
8871 }
8872 
8873 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)8874 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
8875 {
8876 	struct adapter *sc = arg1;
8877 	int rc, v;
8878 	uint32_t param, val;
8879 
8880 	v = sc->sensor_resets;
8881 	rc = sysctl_handle_int(oidp, &v, 0, req);
8882 	if (rc != 0 || req->newptr == NULL || v <= 0)
8883 		return (rc);
8884 
8885 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
8886 	    chip_id(sc) < CHELSIO_T5)
8887 		return (ENOTSUP);
8888 
8889 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
8890 	if (rc)
8891 		return (rc);
8892 	if (hw_off_limits(sc))
8893 		rc = ENXIO;
8894 	else {
8895 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8896 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8897 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
8898 		val = 1;
8899 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8900 	}
8901 	end_synchronized_op(sc, 0);
8902 	if (rc == 0)
8903 		sc->sensor_resets++;
8904 	return (rc);
8905 }
8906 
8907 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)8908 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
8909 {
8910 	struct adapter *sc = arg1;
8911 	struct sbuf *sb;
8912 	int rc;
8913 	uint32_t param, val;
8914 
8915 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
8916 	if (rc)
8917 		return (rc);
8918 	if (hw_off_limits(sc))
8919 		rc = ENXIO;
8920 	else {
8921 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8922 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
8923 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8924 	}
8925 	end_synchronized_op(sc, 0);
8926 	if (rc)
8927 		return (rc);
8928 
8929 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8930 	if (sb == NULL)
8931 		return (ENOMEM);
8932 
8933 	if (val == 0xffffffff) {
8934 		/* Only debug and custom firmwares report load averages. */
8935 		sbuf_printf(sb, "not available");
8936 	} else {
8937 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
8938 		    (val >> 16) & 0xff);
8939 	}
8940 	rc = sbuf_finish(sb);
8941 	sbuf_delete(sb);
8942 
8943 	return (rc);
8944 }
8945 
8946 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)8947 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
8948 {
8949 	struct adapter *sc = arg1;
8950 	struct sbuf *sb;
8951 	int rc, i;
8952 	uint16_t incr[NMTUS][NCCTRL_WIN];
8953 	static const char *dec_fac[] = {
8954 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
8955 		"0.9375"
8956 	};
8957 
8958 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8959 	if (sb == NULL)
8960 		return (ENOMEM);
8961 
8962 	rc = 0;
8963 	mtx_lock(&sc->reg_lock);
8964 	if (hw_off_limits(sc))
8965 		rc = ENXIO;
8966 	else
8967 		t4_read_cong_tbl(sc, incr);
8968 	mtx_unlock(&sc->reg_lock);
8969 	if (rc)
8970 		goto done;
8971 
8972 	for (i = 0; i < NCCTRL_WIN; ++i) {
8973 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
8974 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
8975 		    incr[5][i], incr[6][i], incr[7][i]);
8976 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
8977 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
8978 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
8979 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
8980 	}
8981 
8982 	rc = sbuf_finish(sb);
8983 done:
8984 	sbuf_delete(sb);
8985 	return (rc);
8986 }
8987 
8988 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
8989 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
8990 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
8991 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
8992 };
8993 
8994 static int
sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)8995 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
8996 {
8997 	struct adapter *sc = arg1;
8998 	struct sbuf *sb;
8999 	int rc, i, n, qid = arg2;
9000 	uint32_t *buf, *p;
9001 	char *qtype;
9002 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
9003 
9004 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
9005 	    ("%s: bad qid %d\n", __func__, qid));
9006 
9007 	if (qid < CIM_NUM_IBQ) {
9008 		/* inbound queue */
9009 		qtype = "IBQ";
9010 		n = 4 * CIM_IBQ_SIZE;
9011 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9012 		mtx_lock(&sc->reg_lock);
9013 		if (hw_off_limits(sc))
9014 			rc = -ENXIO;
9015 		else
9016 			rc = t4_read_cim_ibq(sc, qid, buf, n);
9017 		mtx_unlock(&sc->reg_lock);
9018 	} else {
9019 		/* outbound queue */
9020 		qtype = "OBQ";
9021 		qid -= CIM_NUM_IBQ;
9022 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
9023 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9024 		mtx_lock(&sc->reg_lock);
9025 		if (hw_off_limits(sc))
9026 			rc = -ENXIO;
9027 		else
9028 			rc = t4_read_cim_obq(sc, qid, buf, n);
9029 		mtx_unlock(&sc->reg_lock);
9030 	}
9031 
9032 	if (rc < 0) {
9033 		rc = -rc;
9034 		goto done;
9035 	}
9036 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9037 
9038 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9039 	if (sb == NULL) {
9040 		rc = ENOMEM;
9041 		goto done;
9042 	}
9043 
9044 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
9045 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9046 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9047 		    p[2], p[3]);
9048 
9049 	rc = sbuf_finish(sb);
9050 	sbuf_delete(sb);
9051 done:
9052 	free(buf, M_CXGBE);
9053 	return (rc);
9054 }
9055 
9056 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9057 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9058 {
9059 	uint32_t *p;
9060 
9061 	sbuf_printf(sb, "Status   Data      PC%s",
9062 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9063 	    "     LS0Stat  LS0Addr             LS0Data");
9064 
9065 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9066 		if (cfg & F_UPDBGLACAPTPCONLY) {
9067 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9068 			    p[6], p[7]);
9069 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9070 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9071 			    p[4] & 0xff, p[5] >> 8);
9072 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9073 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9074 			    p[1] & 0xf, p[2] >> 4);
9075 		} else {
9076 			sbuf_printf(sb,
9077 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9078 			    "%08x%08x%08x%08x",
9079 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9080 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9081 			    p[6], p[7]);
9082 		}
9083 	}
9084 }
9085 
9086 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9087 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9088 {
9089 	uint32_t *p;
9090 
9091 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9092 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9093 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9094 
9095 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9096 		if (cfg & F_UPDBGLACAPTPCONLY) {
9097 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9098 			    p[3] & 0xff, p[2], p[1], p[0]);
9099 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9100 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9101 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9102 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9103 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9104 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9105 			    p[6] >> 16);
9106 		} else {
9107 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9108 			    "%08x %08x %08x %08x %08x %08x",
9109 			    (p[9] >> 16) & 0xff,
9110 			    p[9] & 0xffff, p[8] >> 16,
9111 			    p[8] & 0xffff, p[7] >> 16,
9112 			    p[7] & 0xffff, p[6] >> 16,
9113 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9114 		}
9115 	}
9116 }
9117 
9118 static int
sbuf_cim_la(struct adapter * sc,struct sbuf * sb,int flags)9119 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
9120 {
9121 	uint32_t cfg, *buf;
9122 	int rc;
9123 
9124 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9125 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9126 	    M_ZERO | flags);
9127 	if (buf == NULL)
9128 		return (ENOMEM);
9129 
9130 	mtx_lock(&sc->reg_lock);
9131 	if (hw_off_limits(sc))
9132 		rc = ENXIO;
9133 	else {
9134 		rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
9135 		if (rc == 0)
9136 			rc = -t4_cim_read_la(sc, buf, NULL);
9137 	}
9138 	mtx_unlock(&sc->reg_lock);
9139 	if (rc == 0) {
9140 		if (chip_id(sc) < CHELSIO_T6)
9141 			sbuf_cim_la4(sc, sb, buf, cfg);
9142 		else
9143 			sbuf_cim_la6(sc, sb, buf, cfg);
9144 	}
9145 	free(buf, M_CXGBE);
9146 	return (rc);
9147 }
9148 
9149 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9150 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9151 {
9152 	struct adapter *sc = arg1;
9153 	struct sbuf *sb;
9154 	int rc;
9155 
9156 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9157 	if (sb == NULL)
9158 		return (ENOMEM);
9159 
9160 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
9161 	if (rc == 0)
9162 		rc = sbuf_finish(sb);
9163 	sbuf_delete(sb);
9164 	return (rc);
9165 }
9166 
9167 static void
dump_cim_regs(struct adapter * sc)9168 dump_cim_regs(struct adapter *sc)
9169 {
9170 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9171 	    device_get_nameunit(sc->dev),
9172 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9173 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9174 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9175 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9176 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9177 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9178 	    device_get_nameunit(sc->dev),
9179 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9180 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9181 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9182 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9183 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9184 }
9185 
9186 static void
dump_cimla(struct adapter * sc)9187 dump_cimla(struct adapter *sc)
9188 {
9189 	struct sbuf sb;
9190 	int rc;
9191 
9192 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9193 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9194 		    device_get_nameunit(sc->dev));
9195 		return;
9196 	}
9197 	rc = sbuf_cim_la(sc, &sb, M_WAITOK);
9198 	if (rc == 0) {
9199 		rc = sbuf_finish(&sb);
9200 		if (rc == 0) {
9201 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9202 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9203 		}
9204 	}
9205 	sbuf_delete(&sb);
9206 }
9207 
9208 void
t4_os_cim_err(struct adapter * sc)9209 t4_os_cim_err(struct adapter *sc)
9210 {
9211 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9212 }
9213 
9214 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9215 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9216 {
9217 	struct adapter *sc = arg1;
9218 	u_int i;
9219 	struct sbuf *sb;
9220 	uint32_t *buf, *p;
9221 	int rc;
9222 
9223 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9224 	if (sb == NULL)
9225 		return (ENOMEM);
9226 
9227 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9228 	    M_ZERO | M_WAITOK);
9229 
9230 	rc = 0;
9231 	mtx_lock(&sc->reg_lock);
9232 	if (hw_off_limits(sc))
9233 		rc = ENXIO;
9234 	else
9235 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9236 	mtx_unlock(&sc->reg_lock);
9237 	if (rc)
9238 		goto done;
9239 
9240 	p = buf;
9241 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9242 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9243 		    p[1], p[0]);
9244 	}
9245 
9246 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9247 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9248 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9249 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9250 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9251 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9252 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9253 		    p[0] & 1);
9254 	}
9255 	rc = sbuf_finish(sb);
9256 done:
9257 	sbuf_delete(sb);
9258 	free(buf, M_CXGBE);
9259 	return (rc);
9260 }
9261 
9262 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9263 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9264 {
9265 	struct adapter *sc = arg1;
9266 	u_int i;
9267 	struct sbuf *sb;
9268 	uint32_t *buf, *p;
9269 	int rc;
9270 
9271 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9272 	if (sb == NULL)
9273 		return (ENOMEM);
9274 
9275 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9276 	    M_ZERO | M_WAITOK);
9277 
9278 	rc = 0;
9279 	mtx_lock(&sc->reg_lock);
9280 	if (hw_off_limits(sc))
9281 		rc = ENXIO;
9282 	else
9283 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9284 	mtx_unlock(&sc->reg_lock);
9285 	if (rc)
9286 		goto done;
9287 
9288 	p = buf;
9289 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9290 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9291 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9292 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9293 		    p[4], p[3], p[2], p[1], p[0]);
9294 	}
9295 
9296 	sbuf_printf(sb, "\n\nCntl ID               Data");
9297 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9298 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9299 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9300 	}
9301 
9302 	rc = sbuf_finish(sb);
9303 done:
9304 	sbuf_delete(sb);
9305 	free(buf, M_CXGBE);
9306 	return (rc);
9307 }
9308 
9309 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9310 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9311 {
9312 	struct adapter *sc = arg1;
9313 	struct sbuf *sb;
9314 	int rc, i;
9315 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9316 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9317 	uint16_t thres[CIM_NUM_IBQ];
9318 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9319 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9320 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9321 
9322 	cim_num_obq = sc->chip_params->cim_num_obq;
9323 	if (is_t4(sc)) {
9324 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9325 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9326 	} else {
9327 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9328 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9329 	}
9330 	nq = CIM_NUM_IBQ + cim_num_obq;
9331 
9332 	mtx_lock(&sc->reg_lock);
9333 	if (hw_off_limits(sc))
9334 		rc = ENXIO;
9335 	else {
9336 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9337 		if (rc == 0) {
9338 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9339 			    obq_wr);
9340 			if (rc == 0)
9341 				t4_read_cimq_cfg(sc, base, size, thres);
9342 		}
9343 	}
9344 	mtx_unlock(&sc->reg_lock);
9345 	if (rc)
9346 		return (rc);
9347 
9348 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9349 	if (sb == NULL)
9350 		return (ENOMEM);
9351 
9352 	sbuf_printf(sb,
9353 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9354 
9355 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9356 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9357 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9358 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9359 		    G_QUEREMFLITS(p[2]) * 16);
9360 	for ( ; i < nq; i++, p += 4, wr += 2)
9361 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9362 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9363 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9364 		    G_QUEREMFLITS(p[2]) * 16);
9365 
9366 	rc = sbuf_finish(sb);
9367 	sbuf_delete(sb);
9368 
9369 	return (rc);
9370 }
9371 
9372 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9373 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9374 {
9375 	struct adapter *sc = arg1;
9376 	struct sbuf *sb;
9377 	int rc;
9378 	struct tp_cpl_stats stats;
9379 
9380 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9381 	if (sb == NULL)
9382 		return (ENOMEM);
9383 
9384 	rc = 0;
9385 	mtx_lock(&sc->reg_lock);
9386 	if (hw_off_limits(sc))
9387 		rc = ENXIO;
9388 	else
9389 		t4_tp_get_cpl_stats(sc, &stats, 0);
9390 	mtx_unlock(&sc->reg_lock);
9391 	if (rc)
9392 		goto done;
9393 
9394 	if (sc->chip_params->nchan > 2) {
9395 		sbuf_printf(sb, "                 channel 0  channel 1"
9396 		    "  channel 2  channel 3");
9397 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9398 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9399 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9400 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9401 	} else {
9402 		sbuf_printf(sb, "                 channel 0  channel 1");
9403 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9404 		    stats.req[0], stats.req[1]);
9405 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9406 		    stats.rsp[0], stats.rsp[1]);
9407 	}
9408 
9409 	rc = sbuf_finish(sb);
9410 done:
9411 	sbuf_delete(sb);
9412 	return (rc);
9413 }
9414 
9415 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9416 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9417 {
9418 	struct adapter *sc = arg1;
9419 	struct sbuf *sb;
9420 	int rc;
9421 	struct tp_usm_stats stats;
9422 
9423 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9424 	if (sb == NULL)
9425 		return (ENOMEM);
9426 
9427 	rc = 0;
9428 	mtx_lock(&sc->reg_lock);
9429 	if (hw_off_limits(sc))
9430 		rc = ENXIO;
9431 	else
9432 		t4_get_usm_stats(sc, &stats, 1);
9433 	mtx_unlock(&sc->reg_lock);
9434 	if (rc == 0) {
9435 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
9436 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
9437 		sbuf_printf(sb, "Drops:  %u", stats.drops);
9438 		rc = sbuf_finish(sb);
9439 	}
9440 	sbuf_delete(sb);
9441 
9442 	return (rc);
9443 }
9444 
9445 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)9446 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
9447 {
9448 	struct adapter *sc = arg1;
9449 	struct sbuf *sb;
9450 	int rc;
9451 	struct tp_tid_stats stats;
9452 
9453 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9454 	if (sb == NULL)
9455 		return (ENOMEM);
9456 
9457 	rc = 0;
9458 	mtx_lock(&sc->reg_lock);
9459 	if (hw_off_limits(sc))
9460 		rc = ENXIO;
9461 	else
9462 		t4_tp_get_tid_stats(sc, &stats, 1);
9463 	mtx_unlock(&sc->reg_lock);
9464 	if (rc == 0) {
9465 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
9466 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
9467 		sbuf_printf(sb, "Active:     %u\n", stats.act);
9468 		sbuf_printf(sb, "Passive:    %u", stats.pas);
9469 		rc = sbuf_finish(sb);
9470 	}
9471 	sbuf_delete(sb);
9472 
9473 	return (rc);
9474 }
9475 
9476 static const char * const devlog_level_strings[] = {
9477 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
9478 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
9479 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
9480 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
9481 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
9482 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
9483 };
9484 
9485 static const char * const devlog_facility_strings[] = {
9486 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
9487 	[FW_DEVLOG_FACILITY_CF]		= "CF",
9488 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
9489 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
9490 	[FW_DEVLOG_FACILITY_RES]	= "RES",
9491 	[FW_DEVLOG_FACILITY_HW]		= "HW",
9492 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
9493 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
9494 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
9495 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
9496 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
9497 	[FW_DEVLOG_FACILITY_VI]		= "VI",
9498 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
9499 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
9500 	[FW_DEVLOG_FACILITY_TM]		= "TM",
9501 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
9502 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
9503 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
9504 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
9505 	[FW_DEVLOG_FACILITY_RI]		= "RI",
9506 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
9507 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
9508 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
9509 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
9510 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
9511 };
9512 
9513 static int
sbuf_devlog(struct adapter * sc,struct sbuf * sb,int flags)9514 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
9515 {
9516 	int i, j, rc, nentries, first = 0;
9517 	struct devlog_params *dparams = &sc->params.devlog;
9518 	struct fw_devlog_e *buf, *e;
9519 	uint64_t ftstamp = UINT64_MAX;
9520 
9521 	if (dparams->addr == 0)
9522 		return (ENXIO);
9523 
9524 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9525 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
9526 	if (buf == NULL)
9527 		return (ENOMEM);
9528 
9529 	mtx_lock(&sc->reg_lock);
9530 	if (hw_off_limits(sc))
9531 		rc = ENXIO;
9532 	else
9533 		rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf,
9534 		    dparams->size);
9535 	mtx_unlock(&sc->reg_lock);
9536 	if (rc != 0)
9537 		goto done;
9538 
9539 	nentries = dparams->size / sizeof(struct fw_devlog_e);
9540 	for (i = 0; i < nentries; i++) {
9541 		e = &buf[i];
9542 
9543 		if (e->timestamp == 0)
9544 			break;	/* end */
9545 
9546 		e->timestamp = be64toh(e->timestamp);
9547 		e->seqno = be32toh(e->seqno);
9548 		for (j = 0; j < 8; j++)
9549 			e->params[j] = be32toh(e->params[j]);
9550 
9551 		if (e->timestamp < ftstamp) {
9552 			ftstamp = e->timestamp;
9553 			first = i;
9554 		}
9555 	}
9556 
9557 	if (buf[first].timestamp == 0)
9558 		goto done;	/* nothing in the log */
9559 
9560 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
9561 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
9562 
9563 	i = first;
9564 	do {
9565 		e = &buf[i];
9566 		if (e->timestamp == 0)
9567 			break;	/* end */
9568 
9569 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
9570 		    e->seqno, e->timestamp,
9571 		    (e->level < nitems(devlog_level_strings) ?
9572 			devlog_level_strings[e->level] : "UNKNOWN"),
9573 		    (e->facility < nitems(devlog_facility_strings) ?
9574 			devlog_facility_strings[e->facility] : "UNKNOWN"));
9575 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
9576 		    e->params[2], e->params[3], e->params[4],
9577 		    e->params[5], e->params[6], e->params[7]);
9578 
9579 		if (++i == nentries)
9580 			i = 0;
9581 	} while (i != first);
9582 done:
9583 	free(buf, M_CXGBE);
9584 	return (rc);
9585 }
9586 
9587 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)9588 sysctl_devlog(SYSCTL_HANDLER_ARGS)
9589 {
9590 	struct adapter *sc = arg1;
9591 	int rc;
9592 	struct sbuf *sb;
9593 
9594 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9595 	if (sb == NULL)
9596 		return (ENOMEM);
9597 
9598 	rc = sbuf_devlog(sc, sb, M_WAITOK);
9599 	if (rc == 0)
9600 		rc = sbuf_finish(sb);
9601 	sbuf_delete(sb);
9602 	return (rc);
9603 }
9604 
9605 static void
dump_devlog(struct adapter * sc)9606 dump_devlog(struct adapter *sc)
9607 {
9608 	int rc;
9609 	struct sbuf sb;
9610 
9611 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9612 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
9613 		    device_get_nameunit(sc->dev));
9614 		return;
9615 	}
9616 	rc = sbuf_devlog(sc, &sb, M_WAITOK);
9617 	if (rc == 0) {
9618 		rc = sbuf_finish(&sb);
9619 		if (rc == 0) {
9620 			log(LOG_DEBUG, "%s: device log follows.\n%s",
9621 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9622 		}
9623 	}
9624 	sbuf_delete(&sb);
9625 }
9626 
9627 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)9628 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
9629 {
9630 	struct adapter *sc = arg1;
9631 	struct sbuf *sb;
9632 	int rc;
9633 	struct tp_fcoe_stats stats[MAX_NCHAN];
9634 	int i, nchan = sc->chip_params->nchan;
9635 
9636 	rc = 0;
9637 	mtx_lock(&sc->reg_lock);
9638 	if (hw_off_limits(sc))
9639 		rc = ENXIO;
9640 	else {
9641 		for (i = 0; i < nchan; i++)
9642 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
9643 	}
9644 	mtx_unlock(&sc->reg_lock);
9645 	if (rc != 0)
9646 		return (rc);
9647 
9648 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9649 	if (sb == NULL)
9650 		return (ENOMEM);
9651 
9652 	if (nchan > 2) {
9653 		sbuf_printf(sb, "                   channel 0        channel 1"
9654 		    "        channel 2        channel 3");
9655 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
9656 		    stats[0].octets_ddp, stats[1].octets_ddp,
9657 		    stats[2].octets_ddp, stats[3].octets_ddp);
9658 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
9659 		    stats[0].frames_ddp, stats[1].frames_ddp,
9660 		    stats[2].frames_ddp, stats[3].frames_ddp);
9661 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
9662 		    stats[0].frames_drop, stats[1].frames_drop,
9663 		    stats[2].frames_drop, stats[3].frames_drop);
9664 	} else {
9665 		sbuf_printf(sb, "                   channel 0        channel 1");
9666 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
9667 		    stats[0].octets_ddp, stats[1].octets_ddp);
9668 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
9669 		    stats[0].frames_ddp, stats[1].frames_ddp);
9670 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
9671 		    stats[0].frames_drop, stats[1].frames_drop);
9672 	}
9673 
9674 	rc = sbuf_finish(sb);
9675 	sbuf_delete(sb);
9676 
9677 	return (rc);
9678 }
9679 
9680 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)9681 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
9682 {
9683 	struct adapter *sc = arg1;
9684 	struct sbuf *sb;
9685 	int rc, i;
9686 	unsigned int map, kbps, ipg, mode;
9687 	unsigned int pace_tab[NTX_SCHED];
9688 
9689 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
9690 	if (sb == NULL)
9691 		return (ENOMEM);
9692 
9693 	mtx_lock(&sc->reg_lock);
9694 	if (hw_off_limits(sc)) {
9695 		mtx_unlock(&sc->reg_lock);
9696 		rc = ENXIO;
9697 		goto done;
9698 	}
9699 
9700 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
9701 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
9702 	t4_read_pace_tbl(sc, pace_tab);
9703 	mtx_unlock(&sc->reg_lock);
9704 
9705 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
9706 	    "Class IPG (0.1 ns)   Flow IPG (us)");
9707 
9708 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
9709 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
9710 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
9711 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
9712 		if (kbps)
9713 			sbuf_printf(sb, "%9u     ", kbps);
9714 		else
9715 			sbuf_printf(sb, " disabled     ");
9716 
9717 		if (ipg)
9718 			sbuf_printf(sb, "%13u        ", ipg);
9719 		else
9720 			sbuf_printf(sb, "     disabled        ");
9721 
9722 		if (pace_tab[i])
9723 			sbuf_printf(sb, "%10u", pace_tab[i]);
9724 		else
9725 			sbuf_printf(sb, "  disabled");
9726 	}
9727 	rc = sbuf_finish(sb);
9728 done:
9729 	sbuf_delete(sb);
9730 	return (rc);
9731 }
9732 
9733 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)9734 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
9735 {
9736 	struct adapter *sc = arg1;
9737 	struct sbuf *sb;
9738 	int rc, i, j;
9739 	uint64_t *p0, *p1;
9740 	struct lb_port_stats s[2];
9741 	static const char *stat_name[] = {
9742 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
9743 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
9744 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
9745 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
9746 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
9747 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
9748 		"BG2FramesTrunc:", "BG3FramesTrunc:"
9749 	};
9750 
9751 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9752 	if (sb == NULL)
9753 		return (ENOMEM);
9754 
9755 	memset(s, 0, sizeof(s));
9756 
9757 	rc = 0;
9758 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
9759 		mtx_lock(&sc->reg_lock);
9760 		if (hw_off_limits(sc))
9761 			rc = ENXIO;
9762 		else {
9763 			t4_get_lb_stats(sc, i, &s[0]);
9764 			t4_get_lb_stats(sc, i + 1, &s[1]);
9765 		}
9766 		mtx_unlock(&sc->reg_lock);
9767 		if (rc != 0)
9768 			break;
9769 
9770 		p0 = &s[0].octets;
9771 		p1 = &s[1].octets;
9772 		sbuf_printf(sb, "%s                       Loopback %u"
9773 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
9774 
9775 		for (j = 0; j < nitems(stat_name); j++)
9776 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
9777 				   *p0++, *p1++);
9778 	}
9779 
9780 	if (rc == 0)
9781 		rc = sbuf_finish(sb);
9782 	sbuf_delete(sb);
9783 
9784 	return (rc);
9785 }
9786 
9787 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)9788 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
9789 {
9790 	int rc = 0;
9791 	struct port_info *pi = arg1;
9792 	struct link_config *lc = &pi->link_cfg;
9793 	struct sbuf *sb;
9794 
9795 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
9796 	if (sb == NULL)
9797 		return (ENOMEM);
9798 
9799 	if (lc->link_ok || lc->link_down_rc == 255)
9800 		sbuf_printf(sb, "n/a");
9801 	else
9802 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
9803 
9804 	rc = sbuf_finish(sb);
9805 	sbuf_delete(sb);
9806 
9807 	return (rc);
9808 }
9809 
9810 struct mem_desc {
9811 	u_int base;
9812 	u_int limit;
9813 	u_int idx;
9814 };
9815 
9816 static int
mem_desc_cmp(const void * a,const void * b)9817 mem_desc_cmp(const void *a, const void *b)
9818 {
9819 	const u_int v1 = ((const struct mem_desc *)a)->base;
9820 	const u_int v2 = ((const struct mem_desc *)b)->base;
9821 
9822 	if (v1 < v2)
9823 		return (-1);
9824 	else if (v1 > v2)
9825 		return (1);
9826 
9827 	return (0);
9828 }
9829 
9830 static void
mem_region_show(struct sbuf * sb,const char * name,unsigned int from,unsigned int to)9831 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
9832     unsigned int to)
9833 {
9834 	unsigned int size;
9835 
9836 	if (from == to)
9837 		return;
9838 
9839 	size = to - from + 1;
9840 	if (size == 0)
9841 		return;
9842 
9843 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
9844 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
9845 }
9846 
9847 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)9848 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
9849 {
9850 	struct adapter *sc = arg1;
9851 	struct sbuf *sb;
9852 	int rc, i, n;
9853 	uint32_t lo, hi, used, free, alloc;
9854 	static const char *memory[] = {
9855 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
9856 	};
9857 	static const char *region[] = {
9858 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
9859 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
9860 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
9861 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
9862 		"RQUDP region:", "PBL region:", "TXPBL region:",
9863 		"TLSKey region:", "DBVFIFO region:", "ULPRX state:",
9864 		"ULPTX state:", "On-chip queues:",
9865 	};
9866 	struct mem_desc avail[4];
9867 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
9868 	struct mem_desc *md = mem;
9869 
9870 	rc = sysctl_wire_old_buffer(req, 0);
9871 	if (rc != 0)
9872 		return (rc);
9873 
9874 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9875 	if (sb == NULL)
9876 		return (ENOMEM);
9877 
9878 	for (i = 0; i < nitems(mem); i++) {
9879 		mem[i].limit = 0;
9880 		mem[i].idx = i;
9881 	}
9882 
9883 	mtx_lock(&sc->reg_lock);
9884 	if (hw_off_limits(sc)) {
9885 		rc = ENXIO;
9886 		goto done;
9887 	}
9888 
9889 	/* Find and sort the populated memory ranges */
9890 	i = 0;
9891 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
9892 	if (lo & F_EDRAM0_ENABLE) {
9893 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
9894 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
9895 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
9896 		avail[i].idx = 0;
9897 		i++;
9898 	}
9899 	if (lo & F_EDRAM1_ENABLE) {
9900 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
9901 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
9902 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
9903 		avail[i].idx = 1;
9904 		i++;
9905 	}
9906 	if (lo & F_EXT_MEM_ENABLE) {
9907 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
9908 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
9909 		avail[i].limit = avail[i].base + (G_EXT_MEM_SIZE(hi) << 20);
9910 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
9911 		i++;
9912 	}
9913 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
9914 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9915 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9916 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9917 		avail[i].idx = 4;
9918 		i++;
9919 	}
9920 	if (is_t6(sc) && lo & F_HMA_MUX) {
9921 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9922 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9923 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9924 		avail[i].idx = 5;
9925 		i++;
9926 	}
9927 	MPASS(i <= nitems(avail));
9928 	if (!i)                                    /* no memory available */
9929 		goto done;
9930 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
9931 
9932 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
9933 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
9934 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
9935 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
9936 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
9937 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
9938 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
9939 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
9940 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
9941 
9942 	/* the next few have explicit upper bounds */
9943 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
9944 	md->limit = md->base - 1 +
9945 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
9946 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
9947 	md++;
9948 
9949 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
9950 	md->limit = md->base - 1 +
9951 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
9952 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
9953 	md++;
9954 
9955 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
9956 		if (chip_id(sc) <= CHELSIO_T5)
9957 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
9958 		else
9959 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
9960 		md->limit = 0;
9961 	} else {
9962 		md->base = 0;
9963 		md->idx = nitems(region);  /* hide it */
9964 	}
9965 	md++;
9966 
9967 #define ulp_region(reg) \
9968 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
9969 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
9970 
9971 	ulp_region(RX_ISCSI);
9972 	ulp_region(RX_TDDP);
9973 	ulp_region(TX_TPT);
9974 	ulp_region(RX_STAG);
9975 	ulp_region(RX_RQ);
9976 	ulp_region(RX_RQUDP);
9977 	ulp_region(RX_PBL);
9978 	ulp_region(TX_PBL);
9979 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
9980 		ulp_region(RX_TLS_KEY);
9981 	}
9982 #undef ulp_region
9983 
9984 	md->base = 0;
9985 	if (is_t4(sc))
9986 		md->idx = nitems(region);
9987 	else {
9988 		uint32_t size = 0;
9989 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
9990 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
9991 
9992 		if (is_t5(sc)) {
9993 			if (sge_ctrl & F_VFIFO_ENABLE)
9994 				size = fifo_size << 2;
9995 		} else
9996 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
9997 
9998 		if (size) {
9999 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
10000 			md->limit = md->base + size - 1;
10001 		} else
10002 			md->idx = nitems(region);
10003 	}
10004 	md++;
10005 
10006 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
10007 	md->limit = 0;
10008 	md++;
10009 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
10010 	md->limit = 0;
10011 	md++;
10012 
10013 	md->base = sc->vres.ocq.start;
10014 	if (sc->vres.ocq.size)
10015 		md->limit = md->base + sc->vres.ocq.size - 1;
10016 	else
10017 		md->idx = nitems(region);  /* hide it */
10018 	md++;
10019 
10020 	/* add any address-space holes, there can be up to 3 */
10021 	for (n = 0; n < i - 1; n++)
10022 		if (avail[n].limit < avail[n + 1].base)
10023 			(md++)->base = avail[n].limit;
10024 	if (avail[n].limit)
10025 		(md++)->base = avail[n].limit;
10026 
10027 	n = md - mem;
10028 	MPASS(n <= nitems(mem));
10029 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10030 
10031 	for (lo = 0; lo < i; lo++)
10032 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10033 				avail[lo].limit - 1);
10034 
10035 	sbuf_printf(sb, "\n");
10036 	for (i = 0; i < n; i++) {
10037 		if (mem[i].idx >= nitems(region))
10038 			continue;                        /* skip holes */
10039 		if (!mem[i].limit)
10040 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10041 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10042 				mem[i].limit);
10043 	}
10044 
10045 	sbuf_printf(sb, "\n");
10046 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10047 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10048 	mem_region_show(sb, "uP RAM:", lo, hi);
10049 
10050 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10051 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10052 	mem_region_show(sb, "uP Extmem2:", lo, hi);
10053 
10054 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10055 	for (i = 0, free = 0; i < 2; i++)
10056 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10057 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10058 		   G_PMRXMAXPAGE(lo), free,
10059 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
10060 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
10061 
10062 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10063 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10064 	for (i = 0, free = 0; i < 4; i++)
10065 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10066 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10067 		   G_PMTXMAXPAGE(lo), free,
10068 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10069 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
10070 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10071 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10072 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10073 
10074 	for (i = 0; i < 4; i++) {
10075 		if (chip_id(sc) > CHELSIO_T5)
10076 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10077 		else
10078 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10079 		if (is_t5(sc)) {
10080 			used = G_T5_USED(lo);
10081 			alloc = G_T5_ALLOC(lo);
10082 		} else {
10083 			used = G_USED(lo);
10084 			alloc = G_ALLOC(lo);
10085 		}
10086 		/* For T6 these are MAC buffer groups */
10087 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10088 		    i, used, alloc);
10089 	}
10090 	for (i = 0; i < sc->chip_params->nchan; i++) {
10091 		if (chip_id(sc) > CHELSIO_T5)
10092 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10093 		else
10094 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10095 		if (is_t5(sc)) {
10096 			used = G_T5_USED(lo);
10097 			alloc = G_T5_ALLOC(lo);
10098 		} else {
10099 			used = G_USED(lo);
10100 			alloc = G_ALLOC(lo);
10101 		}
10102 		/* For T6 these are MAC buffer groups */
10103 		sbuf_printf(sb,
10104 		    "\nLoopback %d using %u pages out of %u allocated",
10105 		    i, used, alloc);
10106 	}
10107 done:
10108 	mtx_unlock(&sc->reg_lock);
10109 	if (rc == 0)
10110 		rc = sbuf_finish(sb);
10111 	sbuf_delete(sb);
10112 	return (rc);
10113 }
10114 
10115 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10116 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10117 {
10118 	*mask = x | y;
10119 	y = htobe64(y);
10120 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10121 }
10122 
10123 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10124 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10125 {
10126 	struct adapter *sc = arg1;
10127 	struct sbuf *sb;
10128 	int rc, i;
10129 
10130 	MPASS(chip_id(sc) <= CHELSIO_T5);
10131 
10132 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10133 	if (sb == NULL)
10134 		return (ENOMEM);
10135 
10136 	sbuf_printf(sb,
10137 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10138 	    "  VF              Replication             P0 P1 P2 P3  ML");
10139 	rc = 0;
10140 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10141 		uint64_t tcamx, tcamy, mask;
10142 		uint32_t cls_lo, cls_hi;
10143 		uint8_t addr[ETHER_ADDR_LEN];
10144 
10145 		mtx_lock(&sc->reg_lock);
10146 		if (hw_off_limits(sc))
10147 			rc = ENXIO;
10148 		else {
10149 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10150 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10151 		}
10152 		mtx_unlock(&sc->reg_lock);
10153 		if (rc != 0)
10154 			break;
10155 		if (tcamx & tcamy)
10156 			continue;
10157 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10158 		mtx_lock(&sc->reg_lock);
10159 		if (hw_off_limits(sc))
10160 			rc = ENXIO;
10161 		else {
10162 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10163 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10164 		}
10165 		mtx_unlock(&sc->reg_lock);
10166 		if (rc != 0)
10167 			break;
10168 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10169 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10170 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10171 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10172 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10173 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10174 
10175 		if (cls_lo & F_REPLICATE) {
10176 			struct fw_ldst_cmd ldst_cmd;
10177 
10178 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10179 			ldst_cmd.op_to_addrspace =
10180 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10181 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10182 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10183 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10184 			ldst_cmd.u.mps.rplc.fid_idx =
10185 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10186 				V_FW_LDST_CMD_IDX(i));
10187 
10188 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10189 			    "t4mps");
10190 			if (rc)
10191 				break;
10192 			if (hw_off_limits(sc))
10193 				rc = ENXIO;
10194 			else
10195 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10196 				    sizeof(ldst_cmd), &ldst_cmd);
10197 			end_synchronized_op(sc, 0);
10198 			if (rc != 0)
10199 				break;
10200 			else {
10201 				sbuf_printf(sb, " %08x %08x %08x %08x",
10202 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10203 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10204 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10205 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10206 			}
10207 		} else
10208 			sbuf_printf(sb, "%36s", "");
10209 
10210 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10211 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10212 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10213 	}
10214 
10215 	if (rc)
10216 		(void) sbuf_finish(sb);
10217 	else
10218 		rc = sbuf_finish(sb);
10219 	sbuf_delete(sb);
10220 
10221 	return (rc);
10222 }
10223 
10224 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10225 sysctl_mps_tcam_t6(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, "Idx  Ethernet address     Mask       VNI   Mask"
10238 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10239 	    "                           Replication"
10240 	    "                                    P0 P1 P2 P3  ML\n");
10241 
10242 	rc = 0;
10243 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10244 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10245 		uint16_t ivlan;
10246 		uint64_t tcamx, tcamy, val, mask;
10247 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10248 		uint8_t addr[ETHER_ADDR_LEN];
10249 
10250 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10251 		if (i < 256)
10252 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10253 		else
10254 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10255 		mtx_lock(&sc->reg_lock);
10256 		if (hw_off_limits(sc))
10257 			rc = ENXIO;
10258 		else {
10259 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10260 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10261 			tcamy = G_DMACH(val) << 32;
10262 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10263 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10264 		}
10265 		mtx_unlock(&sc->reg_lock);
10266 		if (rc != 0)
10267 			break;
10268 
10269 		lookup_type = G_DATALKPTYPE(data2);
10270 		port_num = G_DATAPORTNUM(data2);
10271 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10272 			/* Inner header VNI */
10273 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10274 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10275 			dip_hit = data2 & F_DATADIPHIT;
10276 			vlan_vld = 0;
10277 		} else {
10278 			vniy = 0;
10279 			dip_hit = 0;
10280 			vlan_vld = data2 & F_DATAVIDH2;
10281 			ivlan = G_VIDL(val);
10282 		}
10283 
10284 		ctl |= V_CTLXYBITSEL(1);
10285 		mtx_lock(&sc->reg_lock);
10286 		if (hw_off_limits(sc))
10287 			rc = ENXIO;
10288 		else {
10289 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10290 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10291 			tcamx = G_DMACH(val) << 32;
10292 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10293 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10294 		}
10295 		mtx_unlock(&sc->reg_lock);
10296 		if (rc != 0)
10297 			break;
10298 
10299 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10300 			/* Inner header VNI mask */
10301 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10302 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10303 		} else
10304 			vnix = 0;
10305 
10306 		if (tcamx & tcamy)
10307 			continue;
10308 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10309 
10310 		mtx_lock(&sc->reg_lock);
10311 		if (hw_off_limits(sc))
10312 			rc = ENXIO;
10313 		else {
10314 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10315 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10316 		}
10317 		mtx_unlock(&sc->reg_lock);
10318 		if (rc != 0)
10319 			break;
10320 
10321 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10322 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10323 			    "%012jx %06x %06x    -    -   %3c"
10324 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
10325 			    addr[1], addr[2], addr[3], addr[4], addr[5],
10326 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
10327 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10328 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10329 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10330 		} else {
10331 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10332 			    "%012jx    -       -   ", i, addr[0], addr[1],
10333 			    addr[2], addr[3], addr[4], addr[5],
10334 			    (uintmax_t)mask);
10335 
10336 			if (vlan_vld)
10337 				sbuf_printf(sb, "%4u   Y     ", ivlan);
10338 			else
10339 				sbuf_printf(sb, "  -    N     ");
10340 
10341 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
10342 			    lookup_type ? 'I' : 'O', port_num,
10343 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10344 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10345 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10346 		}
10347 
10348 
10349 		if (cls_lo & F_T6_REPLICATE) {
10350 			struct fw_ldst_cmd ldst_cmd;
10351 
10352 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10353 			ldst_cmd.op_to_addrspace =
10354 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10355 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10356 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10357 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10358 			ldst_cmd.u.mps.rplc.fid_idx =
10359 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10360 				V_FW_LDST_CMD_IDX(i));
10361 
10362 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10363 			    "t6mps");
10364 			if (rc)
10365 				break;
10366 			if (hw_off_limits(sc))
10367 				rc = ENXIO;
10368 			else
10369 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10370 				    sizeof(ldst_cmd), &ldst_cmd);
10371 			end_synchronized_op(sc, 0);
10372 			if (rc != 0)
10373 				break;
10374 			else {
10375 				sbuf_printf(sb, " %08x %08x %08x %08x"
10376 				    " %08x %08x %08x %08x",
10377 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
10378 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
10379 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
10380 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
10381 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10382 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10383 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10384 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10385 			}
10386 		} else
10387 			sbuf_printf(sb, "%72s", "");
10388 
10389 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
10390 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
10391 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
10392 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
10393 	}
10394 
10395 	if (rc)
10396 		(void) sbuf_finish(sb);
10397 	else
10398 		rc = sbuf_finish(sb);
10399 	sbuf_delete(sb);
10400 
10401 	return (rc);
10402 }
10403 
10404 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)10405 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
10406 {
10407 	struct adapter *sc = arg1;
10408 	struct sbuf *sb;
10409 	int rc;
10410 	uint16_t mtus[NMTUS];
10411 
10412 	rc = 0;
10413 	mtx_lock(&sc->reg_lock);
10414 	if (hw_off_limits(sc))
10415 		rc = ENXIO;
10416 	else
10417 		t4_read_mtu_tbl(sc, mtus, NULL);
10418 	mtx_unlock(&sc->reg_lock);
10419 	if (rc != 0)
10420 		return (rc);
10421 
10422 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10423 	if (sb == NULL)
10424 		return (ENOMEM);
10425 
10426 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
10427 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
10428 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
10429 	    mtus[14], mtus[15]);
10430 
10431 	rc = sbuf_finish(sb);
10432 	sbuf_delete(sb);
10433 
10434 	return (rc);
10435 }
10436 
10437 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)10438 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
10439 {
10440 	struct adapter *sc = arg1;
10441 	struct sbuf *sb;
10442 	int rc, i;
10443 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
10444 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
10445 	static const char *tx_stats[MAX_PM_NSTATS] = {
10446 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
10447 		"Tx FIFO wait", NULL, "Tx latency"
10448 	};
10449 	static const char *rx_stats[MAX_PM_NSTATS] = {
10450 		"Read:", "Write bypass:", "Write mem:", "Flush:",
10451 		"Rx FIFO wait", NULL, "Rx latency"
10452 	};
10453 
10454 	rc = 0;
10455 	mtx_lock(&sc->reg_lock);
10456 	if (hw_off_limits(sc))
10457 		rc = ENXIO;
10458 	else {
10459 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
10460 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
10461 	}
10462 	mtx_unlock(&sc->reg_lock);
10463 	if (rc != 0)
10464 		return (rc);
10465 
10466 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10467 	if (sb == NULL)
10468 		return (ENOMEM);
10469 
10470 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
10471 	for (i = 0; i < 4; i++) {
10472 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10473 		    tx_cyc[i]);
10474 	}
10475 
10476 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
10477 	for (i = 0; i < 4; i++) {
10478 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10479 		    rx_cyc[i]);
10480 	}
10481 
10482 	if (chip_id(sc) > CHELSIO_T5) {
10483 		sbuf_printf(sb,
10484 		    "\n              Total wait      Total occupancy");
10485 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10486 		    tx_cyc[i]);
10487 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10488 		    rx_cyc[i]);
10489 
10490 		i += 2;
10491 		MPASS(i < nitems(tx_stats));
10492 
10493 		sbuf_printf(sb,
10494 		    "\n                   Reads           Total wait");
10495 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10496 		    tx_cyc[i]);
10497 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10498 		    rx_cyc[i]);
10499 	}
10500 
10501 	rc = sbuf_finish(sb);
10502 	sbuf_delete(sb);
10503 
10504 	return (rc);
10505 }
10506 
10507 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)10508 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
10509 {
10510 	struct adapter *sc = arg1;
10511 	struct sbuf *sb;
10512 	int rc;
10513 	struct tp_rdma_stats stats;
10514 
10515 	rc = 0;
10516 	mtx_lock(&sc->reg_lock);
10517 	if (hw_off_limits(sc))
10518 		rc = ENXIO;
10519 	else
10520 		t4_tp_get_rdma_stats(sc, &stats, 0);
10521 	mtx_unlock(&sc->reg_lock);
10522 	if (rc != 0)
10523 		return (rc);
10524 
10525 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10526 	if (sb == NULL)
10527 		return (ENOMEM);
10528 
10529 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
10530 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
10531 
10532 	rc = sbuf_finish(sb);
10533 	sbuf_delete(sb);
10534 
10535 	return (rc);
10536 }
10537 
10538 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)10539 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
10540 {
10541 	struct adapter *sc = arg1;
10542 	struct sbuf *sb;
10543 	int rc;
10544 	struct tp_tcp_stats v4, v6;
10545 
10546 	rc = 0;
10547 	mtx_lock(&sc->reg_lock);
10548 	if (hw_off_limits(sc))
10549 		rc = ENXIO;
10550 	else
10551 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
10552 	mtx_unlock(&sc->reg_lock);
10553 	if (rc != 0)
10554 		return (rc);
10555 
10556 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10557 	if (sb == NULL)
10558 		return (ENOMEM);
10559 
10560 	sbuf_printf(sb,
10561 	    "                                IP                 IPv6\n");
10562 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
10563 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
10564 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
10565 	    v4.tcp_in_segs, v6.tcp_in_segs);
10566 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
10567 	    v4.tcp_out_segs, v6.tcp_out_segs);
10568 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
10569 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
10570 
10571 	rc = sbuf_finish(sb);
10572 	sbuf_delete(sb);
10573 
10574 	return (rc);
10575 }
10576 
10577 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)10578 sysctl_tids(SYSCTL_HANDLER_ARGS)
10579 {
10580 	struct adapter *sc = arg1;
10581 	struct sbuf *sb;
10582 	int rc;
10583 	uint32_t x, y;
10584 	struct tid_info *t = &sc->tids;
10585 
10586 	rc = 0;
10587 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10588 	if (sb == NULL)
10589 		return (ENOMEM);
10590 
10591 	if (t->natids) {
10592 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
10593 		    t->atids_in_use);
10594 	}
10595 
10596 	if (t->nhpftids) {
10597 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
10598 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
10599 	}
10600 
10601 	if (t->ntids) {
10602 		bool hashen = false;
10603 
10604 		mtx_lock(&sc->reg_lock);
10605 		if (hw_off_limits(sc))
10606 			rc = ENXIO;
10607 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10608 			hashen = true;
10609 			if (chip_id(sc) <= CHELSIO_T5) {
10610 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
10611 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
10612 			} else {
10613 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
10614 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
10615 			}
10616 		}
10617 		mtx_unlock(&sc->reg_lock);
10618 		if (rc != 0)
10619 			goto done;
10620 
10621 		sbuf_printf(sb, "TID range: ");
10622 		if (hashen) {
10623 			if (x)
10624 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
10625 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
10626 		} else {
10627 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
10628 			    t->ntids - 1);
10629 		}
10630 		sbuf_printf(sb, ", in use: %u\n",
10631 		    atomic_load_acq_int(&t->tids_in_use));
10632 	}
10633 
10634 	if (t->nstids) {
10635 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
10636 		    t->stid_base + t->nstids - 1, t->stids_in_use);
10637 	}
10638 
10639 	if (t->nftids) {
10640 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
10641 		    t->ftid_end, t->ftids_in_use);
10642 	}
10643 
10644 	if (t->netids) {
10645 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
10646 		    t->etid_base + t->netids - 1, t->etids_in_use);
10647 	}
10648 
10649 	mtx_lock(&sc->reg_lock);
10650 	if (hw_off_limits(sc))
10651 		rc = ENXIO;
10652 	else {
10653 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
10654 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
10655 	}
10656 	mtx_unlock(&sc->reg_lock);
10657 	if (rc != 0)
10658 		goto done;
10659 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
10660 done:
10661 	if (rc == 0)
10662 		rc = sbuf_finish(sb);
10663 	else
10664 		(void)sbuf_finish(sb);
10665 	sbuf_delete(sb);
10666 
10667 	return (rc);
10668 }
10669 
10670 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)10671 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
10672 {
10673 	struct adapter *sc = arg1;
10674 	struct sbuf *sb;
10675 	int rc;
10676 	struct tp_err_stats stats;
10677 
10678 	rc = 0;
10679 	mtx_lock(&sc->reg_lock);
10680 	if (hw_off_limits(sc))
10681 		rc = ENXIO;
10682 	else
10683 		t4_tp_get_err_stats(sc, &stats, 0);
10684 	mtx_unlock(&sc->reg_lock);
10685 	if (rc != 0)
10686 		return (rc);
10687 
10688 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10689 	if (sb == NULL)
10690 		return (ENOMEM);
10691 
10692 	if (sc->chip_params->nchan > 2) {
10693 		sbuf_printf(sb, "                 channel 0  channel 1"
10694 		    "  channel 2  channel 3\n");
10695 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
10696 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
10697 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
10698 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
10699 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
10700 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
10701 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
10702 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
10703 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
10704 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
10705 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
10706 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
10707 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
10708 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
10709 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
10710 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
10711 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
10712 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
10713 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
10714 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
10715 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
10716 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
10717 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
10718 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
10719 	} else {
10720 		sbuf_printf(sb, "                 channel 0  channel 1\n");
10721 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
10722 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
10723 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
10724 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
10725 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
10726 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
10727 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
10728 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
10729 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
10730 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
10731 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
10732 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
10733 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
10734 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
10735 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
10736 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
10737 	}
10738 
10739 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
10740 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
10741 
10742 	rc = sbuf_finish(sb);
10743 	sbuf_delete(sb);
10744 
10745 	return (rc);
10746 }
10747 
10748 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)10749 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
10750 {
10751 	struct adapter *sc = arg1;
10752 	struct sbuf *sb;
10753 	int rc;
10754 	struct tp_tnl_stats stats;
10755 
10756 	rc = 0;
10757 	mtx_lock(&sc->reg_lock);
10758 	if (hw_off_limits(sc))
10759 		rc = ENXIO;
10760 	else
10761 		t4_tp_get_tnl_stats(sc, &stats, 1);
10762 	mtx_unlock(&sc->reg_lock);
10763 	if (rc != 0)
10764 		return (rc);
10765 
10766 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10767 	if (sb == NULL)
10768 		return (ENOMEM);
10769 
10770 	if (sc->chip_params->nchan > 2) {
10771 		sbuf_printf(sb, "           channel 0  channel 1"
10772 		    "  channel 2  channel 3\n");
10773 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
10774 		    stats.out_pkt[0], stats.out_pkt[1],
10775 		    stats.out_pkt[2], stats.out_pkt[3]);
10776 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
10777 		    stats.in_pkt[0], stats.in_pkt[1],
10778 		    stats.in_pkt[2], stats.in_pkt[3]);
10779 	} else {
10780 		sbuf_printf(sb, "           channel 0  channel 1\n");
10781 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
10782 		    stats.out_pkt[0], stats.out_pkt[1]);
10783 		sbuf_printf(sb, "InPkts:   %10u %10u",
10784 		    stats.in_pkt[0], stats.in_pkt[1]);
10785 	}
10786 
10787 	rc = sbuf_finish(sb);
10788 	sbuf_delete(sb);
10789 
10790 	return (rc);
10791 }
10792 
10793 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)10794 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
10795 {
10796 	struct adapter *sc = arg1;
10797 	struct tp_params *tpp = &sc->params.tp;
10798 	u_int mask;
10799 	int rc;
10800 
10801 	mask = tpp->la_mask >> 16;
10802 	rc = sysctl_handle_int(oidp, &mask, 0, req);
10803 	if (rc != 0 || req->newptr == NULL)
10804 		return (rc);
10805 	if (mask > 0xffff)
10806 		return (EINVAL);
10807 	mtx_lock(&sc->reg_lock);
10808 	if (hw_off_limits(sc))
10809 		rc = ENXIO;
10810 	else {
10811 		tpp->la_mask = mask << 16;
10812 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
10813 		    tpp->la_mask);
10814 	}
10815 	mtx_unlock(&sc->reg_lock);
10816 
10817 	return (rc);
10818 }
10819 
10820 struct field_desc {
10821 	const char *name;
10822 	u_int start;
10823 	u_int width;
10824 };
10825 
10826 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)10827 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
10828 {
10829 	char buf[32];
10830 	int line_size = 0;
10831 
10832 	while (f->name) {
10833 		uint64_t mask = (1ULL << f->width) - 1;
10834 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
10835 		    ((uintmax_t)v >> f->start) & mask);
10836 
10837 		if (line_size + len >= 79) {
10838 			line_size = 8;
10839 			sbuf_printf(sb, "\n        ");
10840 		}
10841 		sbuf_printf(sb, "%s ", buf);
10842 		line_size += len + 1;
10843 		f++;
10844 	}
10845 	sbuf_printf(sb, "\n");
10846 }
10847 
10848 static const struct field_desc tp_la0[] = {
10849 	{ "RcfOpCodeOut", 60, 4 },
10850 	{ "State", 56, 4 },
10851 	{ "WcfState", 52, 4 },
10852 	{ "RcfOpcSrcOut", 50, 2 },
10853 	{ "CRxError", 49, 1 },
10854 	{ "ERxError", 48, 1 },
10855 	{ "SanityFailed", 47, 1 },
10856 	{ "SpuriousMsg", 46, 1 },
10857 	{ "FlushInputMsg", 45, 1 },
10858 	{ "FlushInputCpl", 44, 1 },
10859 	{ "RssUpBit", 43, 1 },
10860 	{ "RssFilterHit", 42, 1 },
10861 	{ "Tid", 32, 10 },
10862 	{ "InitTcb", 31, 1 },
10863 	{ "LineNumber", 24, 7 },
10864 	{ "Emsg", 23, 1 },
10865 	{ "EdataOut", 22, 1 },
10866 	{ "Cmsg", 21, 1 },
10867 	{ "CdataOut", 20, 1 },
10868 	{ "EreadPdu", 19, 1 },
10869 	{ "CreadPdu", 18, 1 },
10870 	{ "TunnelPkt", 17, 1 },
10871 	{ "RcfPeerFin", 16, 1 },
10872 	{ "RcfReasonOut", 12, 4 },
10873 	{ "TxCchannel", 10, 2 },
10874 	{ "RcfTxChannel", 8, 2 },
10875 	{ "RxEchannel", 6, 2 },
10876 	{ "RcfRxChannel", 5, 1 },
10877 	{ "RcfDataOutSrdy", 4, 1 },
10878 	{ "RxDvld", 3, 1 },
10879 	{ "RxOoDvld", 2, 1 },
10880 	{ "RxCongestion", 1, 1 },
10881 	{ "TxCongestion", 0, 1 },
10882 	{ NULL }
10883 };
10884 
10885 static const struct field_desc tp_la1[] = {
10886 	{ "CplCmdIn", 56, 8 },
10887 	{ "CplCmdOut", 48, 8 },
10888 	{ "ESynOut", 47, 1 },
10889 	{ "EAckOut", 46, 1 },
10890 	{ "EFinOut", 45, 1 },
10891 	{ "ERstOut", 44, 1 },
10892 	{ "SynIn", 43, 1 },
10893 	{ "AckIn", 42, 1 },
10894 	{ "FinIn", 41, 1 },
10895 	{ "RstIn", 40, 1 },
10896 	{ "DataIn", 39, 1 },
10897 	{ "DataInVld", 38, 1 },
10898 	{ "PadIn", 37, 1 },
10899 	{ "RxBufEmpty", 36, 1 },
10900 	{ "RxDdp", 35, 1 },
10901 	{ "RxFbCongestion", 34, 1 },
10902 	{ "TxFbCongestion", 33, 1 },
10903 	{ "TxPktSumSrdy", 32, 1 },
10904 	{ "RcfUlpType", 28, 4 },
10905 	{ "Eread", 27, 1 },
10906 	{ "Ebypass", 26, 1 },
10907 	{ "Esave", 25, 1 },
10908 	{ "Static0", 24, 1 },
10909 	{ "Cread", 23, 1 },
10910 	{ "Cbypass", 22, 1 },
10911 	{ "Csave", 21, 1 },
10912 	{ "CPktOut", 20, 1 },
10913 	{ "RxPagePoolFull", 18, 2 },
10914 	{ "RxLpbkPkt", 17, 1 },
10915 	{ "TxLpbkPkt", 16, 1 },
10916 	{ "RxVfValid", 15, 1 },
10917 	{ "SynLearned", 14, 1 },
10918 	{ "SetDelEntry", 13, 1 },
10919 	{ "SetInvEntry", 12, 1 },
10920 	{ "CpcmdDvld", 11, 1 },
10921 	{ "CpcmdSave", 10, 1 },
10922 	{ "RxPstructsFull", 8, 2 },
10923 	{ "EpcmdDvld", 7, 1 },
10924 	{ "EpcmdFlush", 6, 1 },
10925 	{ "EpcmdTrimPrefix", 5, 1 },
10926 	{ "EpcmdTrimPostfix", 4, 1 },
10927 	{ "ERssIp4Pkt", 3, 1 },
10928 	{ "ERssIp6Pkt", 2, 1 },
10929 	{ "ERssTcpUdpPkt", 1, 1 },
10930 	{ "ERssFceFipPkt", 0, 1 },
10931 	{ NULL }
10932 };
10933 
10934 static const struct field_desc tp_la2[] = {
10935 	{ "CplCmdIn", 56, 8 },
10936 	{ "MpsVfVld", 55, 1 },
10937 	{ "MpsPf", 52, 3 },
10938 	{ "MpsVf", 44, 8 },
10939 	{ "SynIn", 43, 1 },
10940 	{ "AckIn", 42, 1 },
10941 	{ "FinIn", 41, 1 },
10942 	{ "RstIn", 40, 1 },
10943 	{ "DataIn", 39, 1 },
10944 	{ "DataInVld", 38, 1 },
10945 	{ "PadIn", 37, 1 },
10946 	{ "RxBufEmpty", 36, 1 },
10947 	{ "RxDdp", 35, 1 },
10948 	{ "RxFbCongestion", 34, 1 },
10949 	{ "TxFbCongestion", 33, 1 },
10950 	{ "TxPktSumSrdy", 32, 1 },
10951 	{ "RcfUlpType", 28, 4 },
10952 	{ "Eread", 27, 1 },
10953 	{ "Ebypass", 26, 1 },
10954 	{ "Esave", 25, 1 },
10955 	{ "Static0", 24, 1 },
10956 	{ "Cread", 23, 1 },
10957 	{ "Cbypass", 22, 1 },
10958 	{ "Csave", 21, 1 },
10959 	{ "CPktOut", 20, 1 },
10960 	{ "RxPagePoolFull", 18, 2 },
10961 	{ "RxLpbkPkt", 17, 1 },
10962 	{ "TxLpbkPkt", 16, 1 },
10963 	{ "RxVfValid", 15, 1 },
10964 	{ "SynLearned", 14, 1 },
10965 	{ "SetDelEntry", 13, 1 },
10966 	{ "SetInvEntry", 12, 1 },
10967 	{ "CpcmdDvld", 11, 1 },
10968 	{ "CpcmdSave", 10, 1 },
10969 	{ "RxPstructsFull", 8, 2 },
10970 	{ "EpcmdDvld", 7, 1 },
10971 	{ "EpcmdFlush", 6, 1 },
10972 	{ "EpcmdTrimPrefix", 5, 1 },
10973 	{ "EpcmdTrimPostfix", 4, 1 },
10974 	{ "ERssIp4Pkt", 3, 1 },
10975 	{ "ERssIp6Pkt", 2, 1 },
10976 	{ "ERssTcpUdpPkt", 1, 1 },
10977 	{ "ERssFceFipPkt", 0, 1 },
10978 	{ NULL }
10979 };
10980 
10981 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)10982 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
10983 {
10984 
10985 	field_desc_show(sb, *p, tp_la0);
10986 }
10987 
10988 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)10989 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
10990 {
10991 
10992 	if (idx)
10993 		sbuf_printf(sb, "\n");
10994 	field_desc_show(sb, p[0], tp_la0);
10995 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
10996 		field_desc_show(sb, p[1], tp_la0);
10997 }
10998 
10999 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)11000 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
11001 {
11002 
11003 	if (idx)
11004 		sbuf_printf(sb, "\n");
11005 	field_desc_show(sb, p[0], tp_la0);
11006 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11007 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
11008 }
11009 
11010 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)11011 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
11012 {
11013 	struct adapter *sc = arg1;
11014 	struct sbuf *sb;
11015 	uint64_t *buf, *p;
11016 	int rc;
11017 	u_int i, inc;
11018 	void (*show_func)(struct sbuf *, uint64_t *, int);
11019 
11020 	rc = 0;
11021 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11022 	if (sb == NULL)
11023 		return (ENOMEM);
11024 
11025 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11026 
11027 	mtx_lock(&sc->reg_lock);
11028 	if (hw_off_limits(sc))
11029 		rc = ENXIO;
11030 	else {
11031 		t4_tp_read_la(sc, buf, NULL);
11032 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11033 		case 2:
11034 			inc = 2;
11035 			show_func = tp_la_show2;
11036 			break;
11037 		case 3:
11038 			inc = 2;
11039 			show_func = tp_la_show3;
11040 			break;
11041 		default:
11042 			inc = 1;
11043 			show_func = tp_la_show;
11044 		}
11045 	}
11046 	mtx_unlock(&sc->reg_lock);
11047 	if (rc != 0)
11048 		goto done;
11049 
11050 	p = buf;
11051 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
11052 		(*show_func)(sb, p, i);
11053 	rc = sbuf_finish(sb);
11054 done:
11055 	sbuf_delete(sb);
11056 	free(buf, M_CXGBE);
11057 	return (rc);
11058 }
11059 
11060 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)11061 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
11062 {
11063 	struct adapter *sc = arg1;
11064 	struct sbuf *sb;
11065 	int rc;
11066 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
11067 
11068 	rc = 0;
11069 	mtx_lock(&sc->reg_lock);
11070 	if (hw_off_limits(sc))
11071 		rc = ENXIO;
11072 	else
11073 		t4_get_chan_txrate(sc, nrate, orate);
11074 	mtx_unlock(&sc->reg_lock);
11075 	if (rc != 0)
11076 		return (rc);
11077 
11078 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11079 	if (sb == NULL)
11080 		return (ENOMEM);
11081 
11082 	if (sc->chip_params->nchan > 2) {
11083 		sbuf_printf(sb, "              channel 0   channel 1"
11084 		    "   channel 2   channel 3\n");
11085 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
11086 		    nrate[0], nrate[1], nrate[2], nrate[3]);
11087 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
11088 		    orate[0], orate[1], orate[2], orate[3]);
11089 	} else {
11090 		sbuf_printf(sb, "              channel 0   channel 1\n");
11091 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
11092 		    nrate[0], nrate[1]);
11093 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
11094 		    orate[0], orate[1]);
11095 	}
11096 
11097 	rc = sbuf_finish(sb);
11098 	sbuf_delete(sb);
11099 
11100 	return (rc);
11101 }
11102 
11103 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)11104 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
11105 {
11106 	struct adapter *sc = arg1;
11107 	struct sbuf *sb;
11108 	uint32_t *buf, *p;
11109 	int rc, i;
11110 
11111 	rc = 0;
11112 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11113 	if (sb == NULL)
11114 		return (ENOMEM);
11115 
11116 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
11117 	    M_ZERO | M_WAITOK);
11118 
11119 	mtx_lock(&sc->reg_lock);
11120 	if (hw_off_limits(sc))
11121 		rc = ENXIO;
11122 	else
11123 		t4_ulprx_read_la(sc, buf);
11124 	mtx_unlock(&sc->reg_lock);
11125 	if (rc != 0)
11126 		goto done;
11127 
11128 	p = buf;
11129 	sbuf_printf(sb, "      Pcmd        Type   Message"
11130 	    "                Data");
11131 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
11132 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
11133 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
11134 	}
11135 	rc = sbuf_finish(sb);
11136 done:
11137 	sbuf_delete(sb);
11138 	free(buf, M_CXGBE);
11139 	return (rc);
11140 }
11141 
11142 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)11143 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
11144 {
11145 	struct adapter *sc = arg1;
11146 	struct sbuf *sb;
11147 	int rc;
11148 	uint32_t cfg, s1, s2;
11149 
11150 	MPASS(chip_id(sc) >= CHELSIO_T5);
11151 
11152 	rc = 0;
11153 	mtx_lock(&sc->reg_lock);
11154 	if (hw_off_limits(sc))
11155 		rc = ENXIO;
11156 	else {
11157 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
11158 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
11159 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
11160 	}
11161 	mtx_unlock(&sc->reg_lock);
11162 	if (rc != 0)
11163 		return (rc);
11164 
11165 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11166 	if (sb == NULL)
11167 		return (ENOMEM);
11168 
11169 	if (G_STATSOURCE_T5(cfg) == 7) {
11170 		int mode;
11171 
11172 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
11173 		if (mode == 0)
11174 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
11175 		else if (mode == 1)
11176 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
11177 		else
11178 			sbuf_printf(sb, "unknown mode %d", mode);
11179 	}
11180 	rc = sbuf_finish(sb);
11181 	sbuf_delete(sb);
11182 
11183 	return (rc);
11184 }
11185 
11186 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)11187 sysctl_cpus(SYSCTL_HANDLER_ARGS)
11188 {
11189 	struct adapter *sc = arg1;
11190 	enum cpu_sets op = arg2;
11191 	cpuset_t cpuset;
11192 	struct sbuf *sb;
11193 	int i, rc;
11194 
11195 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
11196 
11197 	CPU_ZERO(&cpuset);
11198 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
11199 	if (rc != 0)
11200 		return (rc);
11201 
11202 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11203 	if (sb == NULL)
11204 		return (ENOMEM);
11205 
11206 	CPU_FOREACH(i)
11207 		sbuf_printf(sb, "%d ", i);
11208 	rc = sbuf_finish(sb);
11209 	sbuf_delete(sb);
11210 
11211 	return (rc);
11212 }
11213 
11214 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)11215 sysctl_reset(SYSCTL_HANDLER_ARGS)
11216 {
11217 	struct adapter *sc = arg1;
11218 	u_int val;
11219 	int rc;
11220 
11221 	val = atomic_load_int(&sc->num_resets);
11222 	rc = sysctl_handle_int(oidp, &val, 0, req);
11223 	if (rc != 0 || req->newptr == NULL)
11224 		return (rc);
11225 
11226 	if (val == 0) {
11227 		/* Zero out the counter that tracks reset. */
11228 		atomic_store_int(&sc->num_resets, 0);
11229 		return (0);
11230 	}
11231 
11232 	if (val != 1)
11233 		return (EINVAL);	/* 0 or 1 are the only legal values */
11234 
11235 	if (hw_off_limits(sc))		/* harmless race */
11236 		return (EALREADY);
11237 
11238 	taskqueue_enqueue(reset_tq, &sc->reset_task);
11239 	return (0);
11240 }
11241 
11242 #ifdef TCP_OFFLOAD
11243 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)11244 sysctl_tls(SYSCTL_HANDLER_ARGS)
11245 {
11246 	struct adapter *sc = arg1;
11247 	int i, j, v, rc;
11248 	struct vi_info *vi;
11249 
11250 	v = sc->tt.tls;
11251 	rc = sysctl_handle_int(oidp, &v, 0, req);
11252 	if (rc != 0 || req->newptr == NULL)
11253 		return (rc);
11254 
11255 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11256 		return (ENOTSUP);
11257 
11258 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
11259 	if (rc)
11260 		return (rc);
11261 	if (hw_off_limits(sc))
11262 		rc = ENXIO;
11263 	else {
11264 		sc->tt.tls = !!v;
11265 		for_each_port(sc, i) {
11266 			for_each_vi(sc->port[i], j, vi) {
11267 				if (vi->flags & VI_INIT_DONE)
11268 					t4_update_fl_bufsize(vi->ifp);
11269 			}
11270 		}
11271 	}
11272 	end_synchronized_op(sc, 0);
11273 
11274 	return (rc);
11275 
11276 }
11277 
11278 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)11279 unit_conv(char *buf, size_t len, u_int val, u_int factor)
11280 {
11281 	u_int rem = val % factor;
11282 
11283 	if (rem == 0)
11284 		snprintf(buf, len, "%u", val / factor);
11285 	else {
11286 		while (rem % 10 == 0)
11287 			rem /= 10;
11288 		snprintf(buf, len, "%u.%u", val / factor, rem);
11289 	}
11290 }
11291 
11292 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)11293 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
11294 {
11295 	struct adapter *sc = arg1;
11296 	char buf[16];
11297 	u_int res, re;
11298 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11299 
11300 	mtx_lock(&sc->reg_lock);
11301 	if (hw_off_limits(sc))
11302 		res = (u_int)-1;
11303 	else
11304 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
11305 	mtx_unlock(&sc->reg_lock);
11306 	if (res == (u_int)-1)
11307 		return (ENXIO);
11308 
11309 	switch (arg2) {
11310 	case 0:
11311 		/* timer_tick */
11312 		re = G_TIMERRESOLUTION(res);
11313 		break;
11314 	case 1:
11315 		/* TCP timestamp tick */
11316 		re = G_TIMESTAMPRESOLUTION(res);
11317 		break;
11318 	case 2:
11319 		/* DACK tick */
11320 		re = G_DELAYEDACKRESOLUTION(res);
11321 		break;
11322 	default:
11323 		return (EDOOFUS);
11324 	}
11325 
11326 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
11327 
11328 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
11329 }
11330 
11331 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)11332 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
11333 {
11334 	struct adapter *sc = arg1;
11335 	int rc;
11336 	u_int dack_tmr, dack_re, v;
11337 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11338 
11339 	mtx_lock(&sc->reg_lock);
11340 	if (hw_off_limits(sc))
11341 		rc = ENXIO;
11342 	else {
11343 		rc = 0;
11344 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
11345 		    A_TP_TIMER_RESOLUTION));
11346 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
11347 	}
11348 	mtx_unlock(&sc->reg_lock);
11349 	if (rc != 0)
11350 		return (rc);
11351 
11352 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
11353 
11354 	return (sysctl_handle_int(oidp, &v, 0, req));
11355 }
11356 
11357 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)11358 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
11359 {
11360 	struct adapter *sc = arg1;
11361 	int rc, reg = arg2;
11362 	u_int tre;
11363 	u_long tp_tick_us, v;
11364 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11365 
11366 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
11367 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
11368 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
11369 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
11370 
11371 	mtx_lock(&sc->reg_lock);
11372 	if (hw_off_limits(sc))
11373 		rc = ENXIO;
11374 	else {
11375 		rc = 0;
11376 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
11377 		tp_tick_us = (cclk_ps << tre) / 1000000;
11378 		if (reg == A_TP_INIT_SRTT)
11379 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
11380 		else
11381 			v = tp_tick_us * t4_read_reg(sc, reg);
11382 	}
11383 	mtx_unlock(&sc->reg_lock);
11384 	if (rc != 0)
11385 		return (rc);
11386 	else
11387 		return (sysctl_handle_long(oidp, &v, 0, req));
11388 }
11389 
11390 /*
11391  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
11392  * passed to this function.
11393  */
11394 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)11395 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
11396 {
11397 	struct adapter *sc = arg1;
11398 	int rc, idx = arg2;
11399 	u_int v;
11400 
11401 	MPASS(idx >= 0 && idx <= 24);
11402 
11403 	mtx_lock(&sc->reg_lock);
11404 	if (hw_off_limits(sc))
11405 		rc = ENXIO;
11406 	else {
11407 		rc = 0;
11408 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
11409 	}
11410 	mtx_unlock(&sc->reg_lock);
11411 	if (rc != 0)
11412 		return (rc);
11413 	else
11414 		return (sysctl_handle_int(oidp, &v, 0, req));
11415 }
11416 
11417 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)11418 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
11419 {
11420 	struct adapter *sc = arg1;
11421 	int rc, idx = arg2;
11422 	u_int shift, v, r;
11423 
11424 	MPASS(idx >= 0 && idx < 16);
11425 
11426 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
11427 	shift = (idx & 3) << 3;
11428 	mtx_lock(&sc->reg_lock);
11429 	if (hw_off_limits(sc))
11430 		rc = ENXIO;
11431 	else {
11432 		rc = 0;
11433 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
11434 	}
11435 	mtx_unlock(&sc->reg_lock);
11436 	if (rc != 0)
11437 		return (rc);
11438 	else
11439 		return (sysctl_handle_int(oidp, &v, 0, req));
11440 }
11441 
11442 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)11443 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
11444 {
11445 	struct vi_info *vi = arg1;
11446 	struct adapter *sc = vi->adapter;
11447 	int idx, rc, i;
11448 	struct sge_ofld_rxq *ofld_rxq;
11449 	uint8_t v;
11450 
11451 	idx = vi->ofld_tmr_idx;
11452 
11453 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11454 	if (rc != 0 || req->newptr == NULL)
11455 		return (rc);
11456 
11457 	if (idx < 0 || idx >= SGE_NTIMERS)
11458 		return (EINVAL);
11459 
11460 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11461 	    "t4otmr");
11462 	if (rc)
11463 		return (rc);
11464 
11465 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
11466 	for_each_ofld_rxq(vi, i, ofld_rxq) {
11467 #ifdef atomic_store_rel_8
11468 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
11469 #else
11470 		ofld_rxq->iq.intr_params = v;
11471 #endif
11472 	}
11473 	vi->ofld_tmr_idx = idx;
11474 
11475 	end_synchronized_op(sc, LOCK_HELD);
11476 	return (0);
11477 }
11478 
11479 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)11480 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
11481 {
11482 	struct vi_info *vi = arg1;
11483 	struct adapter *sc = vi->adapter;
11484 	int idx, rc;
11485 
11486 	idx = vi->ofld_pktc_idx;
11487 
11488 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11489 	if (rc != 0 || req->newptr == NULL)
11490 		return (rc);
11491 
11492 	if (idx < -1 || idx >= SGE_NCOUNTERS)
11493 		return (EINVAL);
11494 
11495 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11496 	    "t4opktc");
11497 	if (rc)
11498 		return (rc);
11499 
11500 	if (vi->flags & VI_INIT_DONE)
11501 		rc = EBUSY; /* cannot be changed once the queues are created */
11502 	else
11503 		vi->ofld_pktc_idx = idx;
11504 
11505 	end_synchronized_op(sc, LOCK_HELD);
11506 	return (rc);
11507 }
11508 #endif
11509 
11510 static int
get_sge_context(struct adapter * sc,struct t4_sge_context * cntxt)11511 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
11512 {
11513 	int rc;
11514 
11515 	if (cntxt->cid > M_CTXTQID)
11516 		return (EINVAL);
11517 
11518 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
11519 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
11520 		return (EINVAL);
11521 
11522 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
11523 	if (rc)
11524 		return (rc);
11525 
11526 	if (hw_off_limits(sc)) {
11527 		rc = ENXIO;
11528 		goto done;
11529 	}
11530 
11531 	if (sc->flags & FW_OK) {
11532 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
11533 		    &cntxt->data[0]);
11534 		if (rc == 0)
11535 			goto done;
11536 	}
11537 
11538 	/*
11539 	 * Read via firmware failed or wasn't even attempted.  Read directly via
11540 	 * the backdoor.
11541 	 */
11542 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
11543 done:
11544 	end_synchronized_op(sc, 0);
11545 	return (rc);
11546 }
11547 
11548 static int
load_fw(struct adapter * sc,struct t4_data * fw)11549 load_fw(struct adapter *sc, struct t4_data *fw)
11550 {
11551 	int rc;
11552 	uint8_t *fw_data;
11553 
11554 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
11555 	if (rc)
11556 		return (rc);
11557 
11558 	if (hw_off_limits(sc)) {
11559 		rc = ENXIO;
11560 		goto done;
11561 	}
11562 
11563 	/*
11564 	 * The firmware, with the sole exception of the memory parity error
11565 	 * handler, runs from memory and not flash.  It is almost always safe to
11566 	 * install a new firmware on a running system.  Just set bit 1 in
11567 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
11568 	 */
11569 	if (sc->flags & FULL_INIT_DONE &&
11570 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
11571 		rc = EBUSY;
11572 		goto done;
11573 	}
11574 
11575 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
11576 
11577 	rc = copyin(fw->data, fw_data, fw->len);
11578 	if (rc == 0)
11579 		rc = -t4_load_fw(sc, fw_data, fw->len);
11580 
11581 	free(fw_data, M_CXGBE);
11582 done:
11583 	end_synchronized_op(sc, 0);
11584 	return (rc);
11585 }
11586 
11587 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)11588 load_cfg(struct adapter *sc, struct t4_data *cfg)
11589 {
11590 	int rc;
11591 	uint8_t *cfg_data = NULL;
11592 
11593 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11594 	if (rc)
11595 		return (rc);
11596 
11597 	if (hw_off_limits(sc)) {
11598 		rc = ENXIO;
11599 		goto done;
11600 	}
11601 
11602 	if (cfg->len == 0) {
11603 		/* clear */
11604 		rc = -t4_load_cfg(sc, NULL, 0);
11605 		goto done;
11606 	}
11607 
11608 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
11609 
11610 	rc = copyin(cfg->data, cfg_data, cfg->len);
11611 	if (rc == 0)
11612 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
11613 
11614 	free(cfg_data, M_CXGBE);
11615 done:
11616 	end_synchronized_op(sc, 0);
11617 	return (rc);
11618 }
11619 
11620 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)11621 load_boot(struct adapter *sc, struct t4_bootrom *br)
11622 {
11623 	int rc;
11624 	uint8_t *br_data = NULL;
11625 	u_int offset;
11626 
11627 	if (br->len > 1024 * 1024)
11628 		return (EFBIG);
11629 
11630 	if (br->pf_offset == 0) {
11631 		/* pfidx */
11632 		if (br->pfidx_addr > 7)
11633 			return (EINVAL);
11634 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
11635 		    A_PCIE_PF_EXPROM_OFST)));
11636 	} else if (br->pf_offset == 1) {
11637 		/* offset */
11638 		offset = G_OFFSET(br->pfidx_addr);
11639 	} else {
11640 		return (EINVAL);
11641 	}
11642 
11643 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
11644 	if (rc)
11645 		return (rc);
11646 
11647 	if (hw_off_limits(sc)) {
11648 		rc = ENXIO;
11649 		goto done;
11650 	}
11651 
11652 	if (br->len == 0) {
11653 		/* clear */
11654 		rc = -t4_load_boot(sc, NULL, offset, 0);
11655 		goto done;
11656 	}
11657 
11658 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
11659 
11660 	rc = copyin(br->data, br_data, br->len);
11661 	if (rc == 0)
11662 		rc = -t4_load_boot(sc, br_data, offset, br->len);
11663 
11664 	free(br_data, M_CXGBE);
11665 done:
11666 	end_synchronized_op(sc, 0);
11667 	return (rc);
11668 }
11669 
11670 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)11671 load_bootcfg(struct adapter *sc, struct t4_data *bc)
11672 {
11673 	int rc;
11674 	uint8_t *bc_data = NULL;
11675 
11676 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11677 	if (rc)
11678 		return (rc);
11679 
11680 	if (hw_off_limits(sc)) {
11681 		rc = ENXIO;
11682 		goto done;
11683 	}
11684 
11685 	if (bc->len == 0) {
11686 		/* clear */
11687 		rc = -t4_load_bootcfg(sc, NULL, 0);
11688 		goto done;
11689 	}
11690 
11691 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
11692 
11693 	rc = copyin(bc->data, bc_data, bc->len);
11694 	if (rc == 0)
11695 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
11696 
11697 	free(bc_data, M_CXGBE);
11698 done:
11699 	end_synchronized_op(sc, 0);
11700 	return (rc);
11701 }
11702 
11703 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)11704 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
11705 {
11706 	int rc;
11707 	struct cudbg_init *cudbg;
11708 	void *handle, *buf;
11709 
11710 	/* buf is large, don't block if no memory is available */
11711 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
11712 	if (buf == NULL)
11713 		return (ENOMEM);
11714 
11715 	handle = cudbg_alloc_handle();
11716 	if (handle == NULL) {
11717 		rc = ENOMEM;
11718 		goto done;
11719 	}
11720 
11721 	cudbg = cudbg_get_init(handle);
11722 	cudbg->adap = sc;
11723 	cudbg->print = (cudbg_print_cb)printf;
11724 
11725 #ifndef notyet
11726 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
11727 	    __func__, dump->wr_flash, dump->len, dump->data);
11728 #endif
11729 
11730 	if (dump->wr_flash)
11731 		cudbg->use_flash = 1;
11732 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
11733 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
11734 
11735 	rc = cudbg_collect(handle, buf, &dump->len);
11736 	if (rc != 0)
11737 		goto done;
11738 
11739 	rc = copyout(buf, dump->data, dump->len);
11740 done:
11741 	cudbg_free_handle(handle);
11742 	free(buf, M_CXGBE);
11743 	return (rc);
11744 }
11745 
11746 static void
free_offload_policy(struct t4_offload_policy * op)11747 free_offload_policy(struct t4_offload_policy *op)
11748 {
11749 	struct offload_rule *r;
11750 	int i;
11751 
11752 	if (op == NULL)
11753 		return;
11754 
11755 	r = &op->rule[0];
11756 	for (i = 0; i < op->nrules; i++, r++) {
11757 		free(r->bpf_prog.bf_insns, M_CXGBE);
11758 	}
11759 	free(op->rule, M_CXGBE);
11760 	free(op, M_CXGBE);
11761 }
11762 
11763 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)11764 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
11765 {
11766 	int i, rc, len;
11767 	struct t4_offload_policy *op, *old;
11768 	struct bpf_program *bf;
11769 	const struct offload_settings *s;
11770 	struct offload_rule *r;
11771 	void *u;
11772 
11773 	if (!is_offload(sc))
11774 		return (ENODEV);
11775 
11776 	if (uop->nrules == 0) {
11777 		/* Delete installed policies. */
11778 		op = NULL;
11779 		goto set_policy;
11780 	} else if (uop->nrules > 256) { /* arbitrary */
11781 		return (E2BIG);
11782 	}
11783 
11784 	/* Copy userspace offload policy to kernel */
11785 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
11786 	op->nrules = uop->nrules;
11787 	len = op->nrules * sizeof(struct offload_rule);
11788 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11789 	rc = copyin(uop->rule, op->rule, len);
11790 	if (rc) {
11791 		free(op->rule, M_CXGBE);
11792 		free(op, M_CXGBE);
11793 		return (rc);
11794 	}
11795 
11796 	r = &op->rule[0];
11797 	for (i = 0; i < op->nrules; i++, r++) {
11798 
11799 		/* Validate open_type */
11800 		if (r->open_type != OPEN_TYPE_LISTEN &&
11801 		    r->open_type != OPEN_TYPE_ACTIVE &&
11802 		    r->open_type != OPEN_TYPE_PASSIVE &&
11803 		    r->open_type != OPEN_TYPE_DONTCARE) {
11804 error:
11805 			/*
11806 			 * Rules 0 to i have malloc'd filters that need to be
11807 			 * freed.  Rules i+1 to nrules have userspace pointers
11808 			 * and should be left alone.
11809 			 */
11810 			op->nrules = i;
11811 			free_offload_policy(op);
11812 			return (rc);
11813 		}
11814 
11815 		/* Validate settings */
11816 		s = &r->settings;
11817 		if ((s->offload != 0 && s->offload != 1) ||
11818 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
11819 		    s->sched_class < -1 ||
11820 		    s->sched_class >= sc->params.nsched_cls) {
11821 			rc = EINVAL;
11822 			goto error;
11823 		}
11824 
11825 		bf = &r->bpf_prog;
11826 		u = bf->bf_insns;	/* userspace ptr */
11827 		bf->bf_insns = NULL;
11828 		if (bf->bf_len == 0) {
11829 			/* legal, matches everything */
11830 			continue;
11831 		}
11832 		len = bf->bf_len * sizeof(*bf->bf_insns);
11833 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11834 		rc = copyin(u, bf->bf_insns, len);
11835 		if (rc != 0)
11836 			goto error;
11837 
11838 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
11839 			rc = EINVAL;
11840 			goto error;
11841 		}
11842 	}
11843 set_policy:
11844 	rw_wlock(&sc->policy_lock);
11845 	old = sc->policy;
11846 	sc->policy = op;
11847 	rw_wunlock(&sc->policy_lock);
11848 	free_offload_policy(old);
11849 
11850 	return (0);
11851 }
11852 
11853 #define MAX_READ_BUF_SIZE (128 * 1024)
11854 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)11855 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
11856 {
11857 	uint32_t addr, remaining, n;
11858 	uint32_t *buf;
11859 	int rc;
11860 	uint8_t *dst;
11861 
11862 	mtx_lock(&sc->reg_lock);
11863 	if (hw_off_limits(sc))
11864 		rc = ENXIO;
11865 	else
11866 		rc = validate_mem_range(sc, mr->addr, mr->len);
11867 	mtx_unlock(&sc->reg_lock);
11868 	if (rc != 0)
11869 		return (rc);
11870 
11871 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
11872 	addr = mr->addr;
11873 	remaining = mr->len;
11874 	dst = (void *)mr->data;
11875 
11876 	while (remaining) {
11877 		n = min(remaining, MAX_READ_BUF_SIZE);
11878 		mtx_lock(&sc->reg_lock);
11879 		if (hw_off_limits(sc))
11880 			rc = ENXIO;
11881 		else
11882 			read_via_memwin(sc, 2, addr, buf, n);
11883 		mtx_unlock(&sc->reg_lock);
11884 		if (rc != 0)
11885 			break;
11886 
11887 		rc = copyout(buf, dst, n);
11888 		if (rc != 0)
11889 			break;
11890 
11891 		dst += n;
11892 		remaining -= n;
11893 		addr += n;
11894 	}
11895 
11896 	free(buf, M_CXGBE);
11897 	return (rc);
11898 }
11899 #undef MAX_READ_BUF_SIZE
11900 
11901 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)11902 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
11903 {
11904 	int rc;
11905 
11906 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
11907 		return (EINVAL);
11908 
11909 	if (i2cd->len > sizeof(i2cd->data))
11910 		return (EFBIG);
11911 
11912 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
11913 	if (rc)
11914 		return (rc);
11915 	if (hw_off_limits(sc))
11916 		rc = ENXIO;
11917 	else
11918 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
11919 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
11920 	end_synchronized_op(sc, 0);
11921 
11922 	return (rc);
11923 }
11924 
11925 static int
clear_stats(struct adapter * sc,u_int port_id)11926 clear_stats(struct adapter *sc, u_int port_id)
11927 {
11928 	int i, v, chan_map;
11929 	struct port_info *pi;
11930 	struct vi_info *vi;
11931 	struct sge_rxq *rxq;
11932 	struct sge_txq *txq;
11933 	struct sge_wrq *wrq;
11934 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
11935 	struct sge_ofld_txq *ofld_txq;
11936 #endif
11937 #ifdef TCP_OFFLOAD
11938 	struct sge_ofld_rxq *ofld_rxq;
11939 #endif
11940 
11941 	if (port_id >= sc->params.nports)
11942 		return (EINVAL);
11943 	pi = sc->port[port_id];
11944 	if (pi == NULL)
11945 		return (EIO);
11946 
11947 	mtx_lock(&sc->reg_lock);
11948 	if (!hw_off_limits(sc)) {
11949 		/* MAC stats */
11950 		t4_clr_port_stats(sc, pi->tx_chan);
11951 		if (is_t6(sc)) {
11952 			if (pi->fcs_reg != -1)
11953 				pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
11954 			else
11955 				pi->stats.rx_fcs_err = 0;
11956 		}
11957 		for_each_vi(pi, v, vi) {
11958 			if (vi->flags & VI_INIT_DONE)
11959 				t4_clr_vi_stats(sc, vi->vin);
11960 		}
11961 		chan_map = pi->rx_e_chan_map;
11962 		v = 0;	/* reuse */
11963 		while (chan_map) {
11964 			i = ffs(chan_map) - 1;
11965 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
11966 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
11967 			chan_map &= ~(1 << i);
11968 		}
11969 	}
11970 	mtx_unlock(&sc->reg_lock);
11971 	pi->tx_parse_error = 0;
11972 	pi->tnl_cong_drops = 0;
11973 
11974 	/*
11975 	 * Since this command accepts a port, clear stats for
11976 	 * all VIs on this port.
11977 	 */
11978 	for_each_vi(pi, v, vi) {
11979 		if (vi->flags & VI_INIT_DONE) {
11980 
11981 			for_each_rxq(vi, i, rxq) {
11982 #if defined(INET) || defined(INET6)
11983 				rxq->lro.lro_queued = 0;
11984 				rxq->lro.lro_flushed = 0;
11985 #endif
11986 				rxq->rxcsum = 0;
11987 				rxq->vlan_extraction = 0;
11988 				rxq->vxlan_rxcsum = 0;
11989 
11990 				rxq->fl.cl_allocated = 0;
11991 				rxq->fl.cl_recycled = 0;
11992 				rxq->fl.cl_fast_recycled = 0;
11993 			}
11994 
11995 			for_each_txq(vi, i, txq) {
11996 				txq->txcsum = 0;
11997 				txq->tso_wrs = 0;
11998 				txq->vlan_insertion = 0;
11999 				txq->imm_wrs = 0;
12000 				txq->sgl_wrs = 0;
12001 				txq->txpkt_wrs = 0;
12002 				txq->txpkts0_wrs = 0;
12003 				txq->txpkts1_wrs = 0;
12004 				txq->txpkts0_pkts = 0;
12005 				txq->txpkts1_pkts = 0;
12006 				txq->txpkts_flush = 0;
12007 				txq->raw_wrs = 0;
12008 				txq->vxlan_tso_wrs = 0;
12009 				txq->vxlan_txcsum = 0;
12010 				txq->kern_tls_records = 0;
12011 				txq->kern_tls_short = 0;
12012 				txq->kern_tls_partial = 0;
12013 				txq->kern_tls_full = 0;
12014 				txq->kern_tls_octets = 0;
12015 				txq->kern_tls_waste = 0;
12016 				txq->kern_tls_options = 0;
12017 				txq->kern_tls_header = 0;
12018 				txq->kern_tls_fin = 0;
12019 				txq->kern_tls_fin_short = 0;
12020 				txq->kern_tls_cbc = 0;
12021 				txq->kern_tls_gcm = 0;
12022 				mp_ring_reset_stats(txq->r);
12023 			}
12024 
12025 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12026 			for_each_ofld_txq(vi, i, ofld_txq) {
12027 				ofld_txq->wrq.tx_wrs_direct = 0;
12028 				ofld_txq->wrq.tx_wrs_copied = 0;
12029 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12030 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12031 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12032 				counter_u64_zero(ofld_txq->tx_aio_jobs);
12033 				counter_u64_zero(ofld_txq->tx_aio_octets);
12034 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
12035 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
12036 			}
12037 #endif
12038 #ifdef TCP_OFFLOAD
12039 			for_each_ofld_rxq(vi, i, ofld_rxq) {
12040 				ofld_rxq->fl.cl_allocated = 0;
12041 				ofld_rxq->fl.cl_recycled = 0;
12042 				ofld_rxq->fl.cl_fast_recycled = 0;
12043 				counter_u64_zero(
12044 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
12045 				counter_u64_zero(
12046 				    ofld_rxq->rx_iscsi_ddp_setup_error);
12047 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
12048 				ofld_rxq->rx_iscsi_ddp_octets = 0;
12049 				ofld_rxq->rx_iscsi_fl_pdus = 0;
12050 				ofld_rxq->rx_iscsi_fl_octets = 0;
12051 				ofld_rxq->rx_aio_ddp_jobs = 0;
12052 				ofld_rxq->rx_aio_ddp_octets = 0;
12053 				ofld_rxq->rx_toe_tls_records = 0;
12054 				ofld_rxq->rx_toe_tls_octets = 0;
12055 				ofld_rxq->rx_toe_ddp_octets = 0;
12056 				counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
12057 				counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
12058 				counter_u64_zero(ofld_rxq->ddp_buffer_free);
12059 			}
12060 #endif
12061 
12062 			if (IS_MAIN_VI(vi)) {
12063 				wrq = &sc->sge.ctrlq[pi->port_id];
12064 				wrq->tx_wrs_direct = 0;
12065 				wrq->tx_wrs_copied = 0;
12066 			}
12067 		}
12068 	}
12069 
12070 	return (0);
12071 }
12072 
12073 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12074 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12075 {
12076 #ifdef INET6
12077 	struct in6_addr in6;
12078 
12079 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12080 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
12081 		return (0);
12082 	else
12083 		return (EIO);
12084 #else
12085 	return (ENOTSUP);
12086 #endif
12087 }
12088 
12089 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12090 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12091 {
12092 #ifdef INET6
12093 	struct in6_addr in6;
12094 
12095 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12096 	return (t4_release_clip_addr(sc, &in6));
12097 #else
12098 	return (ENOTSUP);
12099 #endif
12100 }
12101 
12102 int
t4_os_find_pci_capability(struct adapter * sc,int cap)12103 t4_os_find_pci_capability(struct adapter *sc, int cap)
12104 {
12105 	int i;
12106 
12107 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
12108 }
12109 
12110 int
t4_os_pci_save_state(struct adapter * sc)12111 t4_os_pci_save_state(struct adapter *sc)
12112 {
12113 	device_t dev;
12114 	struct pci_devinfo *dinfo;
12115 
12116 	dev = sc->dev;
12117 	dinfo = device_get_ivars(dev);
12118 
12119 	pci_cfg_save(dev, dinfo, 0);
12120 	return (0);
12121 }
12122 
12123 int
t4_os_pci_restore_state(struct adapter * sc)12124 t4_os_pci_restore_state(struct adapter *sc)
12125 {
12126 	device_t dev;
12127 	struct pci_devinfo *dinfo;
12128 
12129 	dev = sc->dev;
12130 	dinfo = device_get_ivars(dev);
12131 
12132 	pci_cfg_restore(dev, dinfo);
12133 	return (0);
12134 }
12135 
12136 void
t4_os_portmod_changed(struct port_info * pi)12137 t4_os_portmod_changed(struct port_info *pi)
12138 {
12139 	struct adapter *sc = pi->adapter;
12140 	struct vi_info *vi;
12141 	if_t ifp;
12142 	static const char *mod_str[] = {
12143 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
12144 	};
12145 
12146 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
12147 	    ("%s: port_type %u", __func__, pi->port_type));
12148 
12149 	vi = &pi->vi[0];
12150 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
12151 		PORT_LOCK(pi);
12152 		build_medialist(pi);
12153 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
12154 			fixup_link_config(pi);
12155 			apply_link_config(pi);
12156 		}
12157 		PORT_UNLOCK(pi);
12158 		end_synchronized_op(sc, LOCK_HELD);
12159 	}
12160 
12161 	ifp = vi->ifp;
12162 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
12163 		if_printf(ifp, "transceiver unplugged.\n");
12164 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
12165 		if_printf(ifp, "unknown transceiver inserted.\n");
12166 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
12167 		if_printf(ifp, "unsupported transceiver inserted.\n");
12168 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
12169 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
12170 		    port_top_speed(pi), mod_str[pi->mod_type]);
12171 	} else {
12172 		if_printf(ifp, "transceiver (type %d) inserted.\n",
12173 		    pi->mod_type);
12174 	}
12175 }
12176 
12177 void
t4_os_link_changed(struct port_info * pi)12178 t4_os_link_changed(struct port_info *pi)
12179 {
12180 	struct vi_info *vi;
12181 	if_t ifp;
12182 	struct link_config *lc = &pi->link_cfg;
12183 	struct adapter *sc = pi->adapter;
12184 	int v;
12185 
12186 	PORT_LOCK_ASSERT_OWNED(pi);
12187 
12188 	if (is_t6(sc)) {
12189 		if (lc->link_ok) {
12190 			if (lc->speed > 25000 ||
12191 			    (lc->speed == 25000 && lc->fec == FEC_RS)) {
12192 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12193 				    A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS);
12194 			} else {
12195 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12196 				    A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS);
12197 			}
12198 			pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12199 			pi->stats.rx_fcs_err = 0;
12200 		} else {
12201 			pi->fcs_reg = -1;
12202 		}
12203 	} else {
12204 		MPASS(pi->fcs_reg != -1);
12205 		MPASS(pi->fcs_base == 0);
12206 	}
12207 
12208 	for_each_vi(pi, v, vi) {
12209 		ifp = vi->ifp;
12210 		if (ifp == NULL || IS_DETACHING(vi))
12211 			continue;
12212 
12213 		if (lc->link_ok) {
12214 			if_setbaudrate(ifp, IF_Mbps(lc->speed));
12215 			if_link_state_change(ifp, LINK_STATE_UP);
12216 		} else {
12217 			if_link_state_change(ifp, LINK_STATE_DOWN);
12218 		}
12219 	}
12220 }
12221 
12222 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)12223 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
12224 {
12225 	struct adapter *sc;
12226 
12227 	sx_slock(&t4_list_lock);
12228 	SLIST_FOREACH(sc, &t4_list, link) {
12229 		/*
12230 		 * func should not make any assumptions about what state sc is
12231 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
12232 		 */
12233 		func(sc, arg);
12234 	}
12235 	sx_sunlock(&t4_list_lock);
12236 }
12237 
12238 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)12239 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
12240     struct thread *td)
12241 {
12242 	int rc;
12243 	struct adapter *sc = dev->si_drv1;
12244 
12245 	rc = priv_check(td, PRIV_DRIVER);
12246 	if (rc != 0)
12247 		return (rc);
12248 
12249 	switch (cmd) {
12250 	case CHELSIO_T4_GETREG: {
12251 		struct t4_reg *edata = (struct t4_reg *)data;
12252 
12253 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12254 			return (EFAULT);
12255 
12256 		mtx_lock(&sc->reg_lock);
12257 		if (hw_off_limits(sc))
12258 			rc = ENXIO;
12259 		else if (edata->size == 4)
12260 			edata->val = t4_read_reg(sc, edata->addr);
12261 		else if (edata->size == 8)
12262 			edata->val = t4_read_reg64(sc, edata->addr);
12263 		else
12264 			rc = EINVAL;
12265 		mtx_unlock(&sc->reg_lock);
12266 
12267 		break;
12268 	}
12269 	case CHELSIO_T4_SETREG: {
12270 		struct t4_reg *edata = (struct t4_reg *)data;
12271 
12272 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12273 			return (EFAULT);
12274 
12275 		mtx_lock(&sc->reg_lock);
12276 		if (hw_off_limits(sc))
12277 			rc = ENXIO;
12278 		else if (edata->size == 4) {
12279 			if (edata->val & 0xffffffff00000000)
12280 				rc = EINVAL;
12281 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
12282 		} else if (edata->size == 8)
12283 			t4_write_reg64(sc, edata->addr, edata->val);
12284 		else
12285 			rc = EINVAL;
12286 		mtx_unlock(&sc->reg_lock);
12287 
12288 		break;
12289 	}
12290 	case CHELSIO_T4_REGDUMP: {
12291 		struct t4_regdump *regs = (struct t4_regdump *)data;
12292 		int reglen = t4_get_regs_len(sc);
12293 		uint8_t *buf;
12294 
12295 		if (regs->len < reglen) {
12296 			regs->len = reglen; /* hint to the caller */
12297 			return (ENOBUFS);
12298 		}
12299 
12300 		regs->len = reglen;
12301 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
12302 		mtx_lock(&sc->reg_lock);
12303 		if (hw_off_limits(sc))
12304 			rc = ENXIO;
12305 		else
12306 			get_regs(sc, regs, buf);
12307 		mtx_unlock(&sc->reg_lock);
12308 		if (rc == 0)
12309 			rc = copyout(buf, regs->data, reglen);
12310 		free(buf, M_CXGBE);
12311 		break;
12312 	}
12313 	case CHELSIO_T4_GET_FILTER_MODE:
12314 		rc = get_filter_mode(sc, (uint32_t *)data);
12315 		break;
12316 	case CHELSIO_T4_SET_FILTER_MODE:
12317 		rc = set_filter_mode(sc, *(uint32_t *)data);
12318 		break;
12319 	case CHELSIO_T4_SET_FILTER_MASK:
12320 		rc = set_filter_mask(sc, *(uint32_t *)data);
12321 		break;
12322 	case CHELSIO_T4_GET_FILTER:
12323 		rc = get_filter(sc, (struct t4_filter *)data);
12324 		break;
12325 	case CHELSIO_T4_SET_FILTER:
12326 		rc = set_filter(sc, (struct t4_filter *)data);
12327 		break;
12328 	case CHELSIO_T4_DEL_FILTER:
12329 		rc = del_filter(sc, (struct t4_filter *)data);
12330 		break;
12331 	case CHELSIO_T4_GET_SGE_CONTEXT:
12332 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
12333 		break;
12334 	case CHELSIO_T4_LOAD_FW:
12335 		rc = load_fw(sc, (struct t4_data *)data);
12336 		break;
12337 	case CHELSIO_T4_GET_MEM:
12338 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
12339 		break;
12340 	case CHELSIO_T4_GET_I2C:
12341 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
12342 		break;
12343 	case CHELSIO_T4_CLEAR_STATS:
12344 		rc = clear_stats(sc, *(uint32_t *)data);
12345 		break;
12346 	case CHELSIO_T4_SCHED_CLASS:
12347 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
12348 		break;
12349 	case CHELSIO_T4_SCHED_QUEUE:
12350 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
12351 		break;
12352 	case CHELSIO_T4_GET_TRACER:
12353 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
12354 		break;
12355 	case CHELSIO_T4_SET_TRACER:
12356 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
12357 		break;
12358 	case CHELSIO_T4_LOAD_CFG:
12359 		rc = load_cfg(sc, (struct t4_data *)data);
12360 		break;
12361 	case CHELSIO_T4_LOAD_BOOT:
12362 		rc = load_boot(sc, (struct t4_bootrom *)data);
12363 		break;
12364 	case CHELSIO_T4_LOAD_BOOTCFG:
12365 		rc = load_bootcfg(sc, (struct t4_data *)data);
12366 		break;
12367 	case CHELSIO_T4_CUDBG_DUMP:
12368 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
12369 		break;
12370 	case CHELSIO_T4_SET_OFLD_POLICY:
12371 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
12372 		break;
12373 	case CHELSIO_T4_HOLD_CLIP_ADDR:
12374 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
12375 		break;
12376 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
12377 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
12378 		break;
12379 	default:
12380 		rc = ENOTTY;
12381 	}
12382 
12383 	return (rc);
12384 }
12385 
12386 #ifdef TCP_OFFLOAD
12387 int
toe_capability(struct vi_info * vi,bool enable)12388 toe_capability(struct vi_info *vi, bool enable)
12389 {
12390 	int rc;
12391 	struct port_info *pi = vi->pi;
12392 	struct adapter *sc = pi->adapter;
12393 
12394 	ASSERT_SYNCHRONIZED_OP(sc);
12395 
12396 	if (!is_offload(sc))
12397 		return (ENODEV);
12398 	if (hw_off_limits(sc))
12399 		return (ENXIO);
12400 
12401 	if (enable) {
12402 #ifdef KERN_TLS
12403 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
12404 			int i, j, n;
12405 			struct port_info *p;
12406 			struct vi_info *v;
12407 
12408 			/*
12409 			 * Reconfigure hardware for TOE if TXTLS is not enabled
12410 			 * on any ifnet.
12411 			 */
12412 			n = 0;
12413 			for_each_port(sc, i) {
12414 				p = sc->port[i];
12415 				for_each_vi(p, j, v) {
12416 					if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
12417 						CH_WARN(sc,
12418 						    "%s has NIC TLS enabled.\n",
12419 						    device_get_nameunit(v->dev));
12420 						n++;
12421 					}
12422 				}
12423 			}
12424 			if (n > 0) {
12425 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
12426 				    "associated with this adapter before "
12427 				    "trying to enable TOE.\n");
12428 				return (EAGAIN);
12429 			}
12430 			rc = t6_config_kern_tls(sc, false);
12431 			if (rc)
12432 				return (rc);
12433 		}
12434 #endif
12435 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
12436 			/* TOE is already enabled. */
12437 			return (0);
12438 		}
12439 
12440 		/*
12441 		 * We need the port's queues around so that we're able to send
12442 		 * and receive CPLs to/from the TOE even if the ifnet for this
12443 		 * port has never been UP'd administratively.
12444 		 */
12445 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
12446 			return (rc);
12447 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
12448 		    ((rc = vi_init(&pi->vi[0])) != 0))
12449 			return (rc);
12450 
12451 		if (isset(&sc->offload_map, pi->port_id)) {
12452 			/* TOE is enabled on another VI of this port. */
12453 			MPASS(pi->uld_vis > 0);
12454 			pi->uld_vis++;
12455 			return (0);
12456 		}
12457 
12458 		if (!uld_active(sc, ULD_TOM)) {
12459 			rc = t4_activate_uld(sc, ULD_TOM);
12460 			if (rc == EAGAIN) {
12461 				log(LOG_WARNING,
12462 				    "You must kldload t4_tom.ko before trying "
12463 				    "to enable TOE on a cxgbe interface.\n");
12464 			}
12465 			if (rc != 0)
12466 				return (rc);
12467 			KASSERT(sc->tom_softc != NULL,
12468 			    ("%s: TOM activated but softc NULL", __func__));
12469 			KASSERT(uld_active(sc, ULD_TOM),
12470 			    ("%s: TOM activated but flag not set", __func__));
12471 		}
12472 
12473 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
12474 		if (!uld_active(sc, ULD_IWARP))
12475 			(void) t4_activate_uld(sc, ULD_IWARP);
12476 		if (!uld_active(sc, ULD_ISCSI))
12477 			(void) t4_activate_uld(sc, ULD_ISCSI);
12478 
12479 		if (pi->uld_vis++ == 0)
12480 			setbit(&sc->offload_map, pi->port_id);
12481 	} else {
12482 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) == 0) {
12483 			/* TOE is already disabled. */
12484 			return (0);
12485 		}
12486 		MPASS(isset(&sc->offload_map, pi->port_id));
12487 		MPASS(pi->uld_vis > 0);
12488 		if (--pi->uld_vis == 0)
12489 			clrbit(&sc->offload_map, pi->port_id);
12490 	}
12491 
12492 	return (0);
12493 }
12494 
12495 /*
12496  * Add an upper layer driver to the global list.
12497  */
12498 int
t4_register_uld(struct uld_info * ui,int id)12499 t4_register_uld(struct uld_info *ui, int id)
12500 {
12501 	int rc;
12502 
12503 	if (id < 0 || id > ULD_MAX)
12504 		return (EINVAL);
12505 	sx_xlock(&t4_uld_list_lock);
12506 	if (t4_uld_list[id] != NULL)
12507 		rc = EEXIST;
12508 	else {
12509 		t4_uld_list[id] = ui;
12510 		rc = 0;
12511 	}
12512 	sx_xunlock(&t4_uld_list_lock);
12513 	return (rc);
12514 }
12515 
12516 int
t4_unregister_uld(struct uld_info * ui,int id)12517 t4_unregister_uld(struct uld_info *ui, int id)
12518 {
12519 
12520 	if (id < 0 || id > ULD_MAX)
12521 		return (EINVAL);
12522 	sx_xlock(&t4_uld_list_lock);
12523 	MPASS(t4_uld_list[id] == ui);
12524 	t4_uld_list[id] = NULL;
12525 	sx_xunlock(&t4_uld_list_lock);
12526 	return (0);
12527 }
12528 
12529 int
t4_activate_uld(struct adapter * sc,int id)12530 t4_activate_uld(struct adapter *sc, int id)
12531 {
12532 	int rc;
12533 
12534 	ASSERT_SYNCHRONIZED_OP(sc);
12535 
12536 	if (id < 0 || id > ULD_MAX)
12537 		return (EINVAL);
12538 
12539 	/* Adapter needs to be initialized before any ULD can be activated. */
12540 	if (!(sc->flags & FULL_INIT_DONE)) {
12541 		rc = adapter_init(sc);
12542 		if (rc != 0)
12543 			return (rc);
12544 	}
12545 
12546 	sx_slock(&t4_uld_list_lock);
12547 	if (t4_uld_list[id] == NULL)
12548 		rc = EAGAIN;	/* load the KLD with this ULD and try again. */
12549 	else {
12550 		rc = t4_uld_list[id]->uld_activate(sc);
12551 		if (rc == 0)
12552 			setbit(&sc->active_ulds, id);
12553 	}
12554 	sx_sunlock(&t4_uld_list_lock);
12555 
12556 	return (rc);
12557 }
12558 
12559 int
t4_deactivate_uld(struct adapter * sc,int id)12560 t4_deactivate_uld(struct adapter *sc, int id)
12561 {
12562 	int rc;
12563 
12564 	ASSERT_SYNCHRONIZED_OP(sc);
12565 
12566 	if (id < 0 || id > ULD_MAX)
12567 		return (EINVAL);
12568 
12569 	sx_slock(&t4_uld_list_lock);
12570 	if (t4_uld_list[id] == NULL)
12571 		rc = ENXIO;
12572 	else {
12573 		rc = t4_uld_list[id]->uld_deactivate(sc);
12574 		if (rc == 0)
12575 			clrbit(&sc->active_ulds, id);
12576 	}
12577 	sx_sunlock(&t4_uld_list_lock);
12578 
12579 	return (rc);
12580 }
12581 
12582 static int
deactivate_all_uld(struct adapter * sc)12583 deactivate_all_uld(struct adapter *sc)
12584 {
12585 	int i, rc;
12586 
12587 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
12588 	if (rc != 0)
12589 		return (ENXIO);
12590 	sx_slock(&t4_uld_list_lock);
12591 	for (i = 0; i <= ULD_MAX; i++) {
12592 		if (t4_uld_list[i] == NULL || !uld_active(sc, i))
12593 			continue;
12594 		rc = t4_uld_list[i]->uld_deactivate(sc);
12595 		if (rc != 0)
12596 			break;
12597 		clrbit(&sc->active_ulds, i);
12598 	}
12599 	sx_sunlock(&t4_uld_list_lock);
12600 	end_synchronized_op(sc, 0);
12601 
12602 	return (rc);
12603 }
12604 
12605 static void
stop_all_uld(struct adapter * sc)12606 stop_all_uld(struct adapter *sc)
12607 {
12608 	int i;
12609 
12610 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
12611 		return;
12612 	sx_slock(&t4_uld_list_lock);
12613 	for (i = 0; i <= ULD_MAX; i++) {
12614 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12615 		    t4_uld_list[i]->uld_stop == NULL)
12616 			continue;
12617 		(void) t4_uld_list[i]->uld_stop(sc);
12618 	}
12619 	sx_sunlock(&t4_uld_list_lock);
12620 	end_synchronized_op(sc, 0);
12621 }
12622 
12623 static void
restart_all_uld(struct adapter * sc)12624 restart_all_uld(struct adapter *sc)
12625 {
12626 	int i;
12627 
12628 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
12629 		return;
12630 	sx_slock(&t4_uld_list_lock);
12631 	for (i = 0; i <= ULD_MAX; i++) {
12632 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12633 		    t4_uld_list[i]->uld_restart == NULL)
12634 			continue;
12635 		(void) t4_uld_list[i]->uld_restart(sc);
12636 	}
12637 	sx_sunlock(&t4_uld_list_lock);
12638 	end_synchronized_op(sc, 0);
12639 }
12640 
12641 int
uld_active(struct adapter * sc,int id)12642 uld_active(struct adapter *sc, int id)
12643 {
12644 
12645 	MPASS(id >= 0 && id <= ULD_MAX);
12646 
12647 	return (isset(&sc->active_ulds, id));
12648 }
12649 #endif
12650 
12651 #ifdef KERN_TLS
12652 static int
ktls_capability(struct adapter * sc,bool enable)12653 ktls_capability(struct adapter *sc, bool enable)
12654 {
12655 	ASSERT_SYNCHRONIZED_OP(sc);
12656 
12657 	if (!is_ktls(sc))
12658 		return (ENODEV);
12659 	if (!is_t6(sc))
12660 		return (0);
12661 	if (hw_off_limits(sc))
12662 		return (ENXIO);
12663 
12664 	if (enable) {
12665 		if (sc->flags & KERN_TLS_ON)
12666 			return (0);	/* already on */
12667 		if (sc->offload_map != 0) {
12668 			CH_WARN(sc,
12669 			    "Disable TOE on all interfaces associated with "
12670 			    "this adapter before trying to enable NIC TLS.\n");
12671 			return (EAGAIN);
12672 		}
12673 		return (t6_config_kern_tls(sc, true));
12674 	} else {
12675 		/*
12676 		 * Nothing to do for disable.  If TOE is enabled sometime later
12677 		 * then toe_capability will reconfigure the hardware.
12678 		 */
12679 		return (0);
12680 	}
12681 }
12682 #endif
12683 
12684 /*
12685  * t  = ptr to tunable.
12686  * nc = number of CPUs.
12687  * c  = compiled in default for that tunable.
12688  */
12689 static void
calculate_nqueues(int * t,int nc,const int c)12690 calculate_nqueues(int *t, int nc, const int c)
12691 {
12692 	int nq;
12693 
12694 	if (*t > 0)
12695 		return;
12696 	nq = *t < 0 ? -*t : c;
12697 	*t = min(nc, nq);
12698 }
12699 
12700 /*
12701  * Come up with reasonable defaults for some of the tunables, provided they're
12702  * not set by the user (in which case we'll use the values as is).
12703  */
12704 static void
tweak_tunables(void)12705 tweak_tunables(void)
12706 {
12707 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
12708 
12709 	if (t4_ntxq < 1) {
12710 #ifdef RSS
12711 		t4_ntxq = rss_getnumbuckets();
12712 #else
12713 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
12714 #endif
12715 	}
12716 
12717 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
12718 
12719 	if (t4_nrxq < 1) {
12720 #ifdef RSS
12721 		t4_nrxq = rss_getnumbuckets();
12722 #else
12723 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
12724 #endif
12725 	}
12726 
12727 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
12728 
12729 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12730 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
12731 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
12732 #endif
12733 #ifdef TCP_OFFLOAD
12734 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
12735 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
12736 #endif
12737 
12738 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
12739 	if (t4_toecaps_allowed == -1)
12740 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
12741 #else
12742 	if (t4_toecaps_allowed == -1)
12743 		t4_toecaps_allowed = 0;
12744 #endif
12745 
12746 #ifdef TCP_OFFLOAD
12747 	if (t4_rdmacaps_allowed == -1) {
12748 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
12749 		    FW_CAPS_CONFIG_RDMA_RDMAC;
12750 	}
12751 
12752 	if (t4_iscsicaps_allowed == -1) {
12753 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
12754 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
12755 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
12756 	}
12757 
12758 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
12759 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
12760 
12761 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
12762 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
12763 #else
12764 	if (t4_rdmacaps_allowed == -1)
12765 		t4_rdmacaps_allowed = 0;
12766 
12767 	if (t4_iscsicaps_allowed == -1)
12768 		t4_iscsicaps_allowed = 0;
12769 #endif
12770 
12771 #ifdef DEV_NETMAP
12772 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
12773 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
12774 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
12775 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
12776 #endif
12777 
12778 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
12779 		t4_tmr_idx = TMR_IDX;
12780 
12781 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
12782 		t4_pktc_idx = PKTC_IDX;
12783 
12784 	if (t4_qsize_txq < 128)
12785 		t4_qsize_txq = 128;
12786 
12787 	if (t4_qsize_rxq < 128)
12788 		t4_qsize_rxq = 128;
12789 	while (t4_qsize_rxq & 7)
12790 		t4_qsize_rxq++;
12791 
12792 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
12793 
12794 	/*
12795 	 * Number of VIs to create per-port.  The first VI is the "main" regular
12796 	 * VI for the port.  The rest are additional virtual interfaces on the
12797 	 * same physical port.  Note that the main VI does not have native
12798 	 * netmap support but the extra VIs do.
12799 	 *
12800 	 * Limit the number of VIs per port to the number of available
12801 	 * MAC addresses per port.
12802 	 */
12803 	if (t4_num_vis < 1)
12804 		t4_num_vis = 1;
12805 	if (t4_num_vis > nitems(vi_mac_funcs)) {
12806 		t4_num_vis = nitems(vi_mac_funcs);
12807 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
12808 	}
12809 
12810 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
12811 		pcie_relaxed_ordering = 1;
12812 #if defined(__i386__) || defined(__amd64__)
12813 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
12814 			pcie_relaxed_ordering = 0;
12815 #endif
12816 	}
12817 }
12818 
12819 #ifdef DDB
12820 static void
t4_dump_mem(struct adapter * sc,u_int addr,u_int len)12821 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
12822 {
12823 	uint32_t base, j, off, pf, reg, save, win_pos;
12824 
12825 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
12826 	save = t4_read_reg(sc, reg);
12827 	base = sc->memwin[2].mw_base;
12828 
12829 	if (is_t4(sc)) {
12830 		pf = 0;
12831 		win_pos = addr & ~0xf;	/* start must be 16B aligned */
12832 	} else {
12833 		pf = V_PFNUM(sc->pf);
12834 		win_pos = addr & ~0x7f;	/* start must be 128B aligned */
12835 	}
12836 	off = addr - win_pos;
12837 	t4_write_reg(sc, reg, win_pos | pf);
12838 	t4_read_reg(sc, reg);
12839 
12840 	while (len > 0 && !db_pager_quit) {
12841 		uint32_t buf[8];
12842 		for (j = 0; j < 8; j++, off += 4)
12843 			buf[j] = htonl(t4_read_reg(sc, base + off));
12844 
12845 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
12846 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12847 		    buf[7]);
12848 		if (len <= sizeof(buf))
12849 			len = 0;
12850 		else
12851 			len -= sizeof(buf);
12852 	}
12853 
12854 	t4_write_reg(sc, reg, save);
12855 	t4_read_reg(sc, reg);
12856 }
12857 
12858 static void
t4_dump_tcb(struct adapter * sc,int tid)12859 t4_dump_tcb(struct adapter *sc, int tid)
12860 {
12861 	uint32_t tcb_addr;
12862 
12863 	/* Dump TCB for the tid */
12864 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
12865 	tcb_addr += tid * TCB_SIZE;
12866 	t4_dump_mem(sc, tcb_addr, TCB_SIZE);
12867 }
12868 
12869 static void
t4_dump_devlog(struct adapter * sc)12870 t4_dump_devlog(struct adapter *sc)
12871 {
12872 	struct devlog_params *dparams = &sc->params.devlog;
12873 	struct fw_devlog_e e;
12874 	int i, first, j, m, nentries, rc;
12875 	uint64_t ftstamp = UINT64_MAX;
12876 
12877 	if (dparams->start == 0) {
12878 		db_printf("devlog params not valid\n");
12879 		return;
12880 	}
12881 
12882 	nentries = dparams->size / sizeof(struct fw_devlog_e);
12883 	m = fwmtype_to_hwmtype(dparams->memtype);
12884 
12885 	/* Find the first entry. */
12886 	first = -1;
12887 	for (i = 0; i < nentries && !db_pager_quit; i++) {
12888 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12889 		    sizeof(e), (void *)&e);
12890 		if (rc != 0)
12891 			break;
12892 
12893 		if (e.timestamp == 0)
12894 			break;
12895 
12896 		e.timestamp = be64toh(e.timestamp);
12897 		if (e.timestamp < ftstamp) {
12898 			ftstamp = e.timestamp;
12899 			first = i;
12900 		}
12901 	}
12902 
12903 	if (first == -1)
12904 		return;
12905 
12906 	i = first;
12907 	do {
12908 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12909 		    sizeof(e), (void *)&e);
12910 		if (rc != 0)
12911 			return;
12912 
12913 		if (e.timestamp == 0)
12914 			return;
12915 
12916 		e.timestamp = be64toh(e.timestamp);
12917 		e.seqno = be32toh(e.seqno);
12918 		for (j = 0; j < 8; j++)
12919 			e.params[j] = be32toh(e.params[j]);
12920 
12921 		db_printf("%10d  %15ju  %8s  %8s  ",
12922 		    e.seqno, e.timestamp,
12923 		    (e.level < nitems(devlog_level_strings) ?
12924 			devlog_level_strings[e.level] : "UNKNOWN"),
12925 		    (e.facility < nitems(devlog_facility_strings) ?
12926 			devlog_facility_strings[e.facility] : "UNKNOWN"));
12927 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
12928 		    e.params[3], e.params[4], e.params[5], e.params[6],
12929 		    e.params[7]);
12930 
12931 		if (++i == nentries)
12932 			i = 0;
12933 	} while (i != first && !db_pager_quit);
12934 }
12935 
12936 static DB_DEFINE_TABLE(show, t4, show_t4);
12937 
DB_TABLE_COMMAND_FLAGS(show_t4,devlog,db_show_devlog,CS_OWN)12938 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
12939 {
12940 	device_t dev;
12941 	int t;
12942 	bool valid;
12943 
12944 	valid = false;
12945 	t = db_read_token();
12946 	if (t == tIDENT) {
12947 		dev = device_lookup_by_name(db_tok_string);
12948 		valid = true;
12949 	}
12950 	db_skip_to_eol();
12951 	if (!valid) {
12952 		db_printf("usage: show t4 devlog <nexus>\n");
12953 		return;
12954 	}
12955 
12956 	if (dev == NULL) {
12957 		db_printf("device not found\n");
12958 		return;
12959 	}
12960 
12961 	t4_dump_devlog(device_get_softc(dev));
12962 }
12963 
DB_TABLE_COMMAND_FLAGS(show_t4,tcb,db_show_t4tcb,CS_OWN)12964 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
12965 {
12966 	device_t dev;
12967 	int radix, tid, t;
12968 	bool valid;
12969 
12970 	valid = false;
12971 	radix = db_radix;
12972 	db_radix = 10;
12973 	t = db_read_token();
12974 	if (t == tIDENT) {
12975 		dev = device_lookup_by_name(db_tok_string);
12976 		t = db_read_token();
12977 		if (t == tNUMBER) {
12978 			tid = db_tok_number;
12979 			valid = true;
12980 		}
12981 	}
12982 	db_radix = radix;
12983 	db_skip_to_eol();
12984 	if (!valid) {
12985 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
12986 		return;
12987 	}
12988 
12989 	if (dev == NULL) {
12990 		db_printf("device not found\n");
12991 		return;
12992 	}
12993 	if (tid < 0) {
12994 		db_printf("invalid tid\n");
12995 		return;
12996 	}
12997 
12998 	t4_dump_tcb(device_get_softc(dev), tid);
12999 }
13000 
DB_TABLE_COMMAND_FLAGS(show_t4,memdump,db_show_memdump,CS_OWN)13001 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
13002 {
13003 	device_t dev;
13004 	int radix, t;
13005 	bool valid;
13006 
13007 	valid = false;
13008 	radix = db_radix;
13009 	db_radix = 10;
13010 	t = db_read_token();
13011 	if (t == tIDENT) {
13012 		dev = device_lookup_by_name(db_tok_string);
13013 		t = db_read_token();
13014 		if (t == tNUMBER) {
13015 			addr = db_tok_number;
13016 			t = db_read_token();
13017 			if (t == tNUMBER) {
13018 				count = db_tok_number;
13019 				valid = true;
13020 			}
13021 		}
13022 	}
13023 	db_radix = radix;
13024 	db_skip_to_eol();
13025 	if (!valid) {
13026 		db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
13027 		return;
13028 	}
13029 
13030 	if (dev == NULL) {
13031 		db_printf("device not found\n");
13032 		return;
13033 	}
13034 	if (addr < 0) {
13035 		db_printf("invalid address\n");
13036 		return;
13037 	}
13038 	if (count <= 0) {
13039 		db_printf("invalid length\n");
13040 		return;
13041 	}
13042 
13043 	t4_dump_mem(device_get_softc(dev), addr, count);
13044 }
13045 #endif
13046 
13047 static eventhandler_tag vxlan_start_evtag;
13048 static eventhandler_tag vxlan_stop_evtag;
13049 
13050 struct vxlan_evargs {
13051 	if_t ifp;
13052 	uint16_t port;
13053 };
13054 
13055 static void
enable_vxlan_rx(struct adapter * sc)13056 enable_vxlan_rx(struct adapter *sc)
13057 {
13058 	int i, rc;
13059 	struct port_info *pi;
13060 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
13061 
13062 	ASSERT_SYNCHRONIZED_OP(sc);
13063 
13064 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
13065 	    F_VXLAN_EN);
13066 	for_each_port(sc, i) {
13067 		pi = sc->port[i];
13068 		if (pi->vxlan_tcam_entry == true)
13069 			continue;
13070 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
13071 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
13072 		    true);
13073 		if (rc < 0) {
13074 			rc = -rc;
13075 			CH_ERR(&pi->vi[0],
13076 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
13077 		} else {
13078 			MPASS(rc == sc->rawf_base + pi->port_id);
13079 			pi->vxlan_tcam_entry = true;
13080 		}
13081 	}
13082 }
13083 
13084 static void
t4_vxlan_start(struct adapter * sc,void * arg)13085 t4_vxlan_start(struct adapter *sc, void *arg)
13086 {
13087 	struct vxlan_evargs *v = arg;
13088 
13089 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13090 		return;
13091 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
13092 		return;
13093 
13094 	if (sc->vxlan_refcount == 0) {
13095 		sc->vxlan_port = v->port;
13096 		sc->vxlan_refcount = 1;
13097 		if (!hw_off_limits(sc))
13098 			enable_vxlan_rx(sc);
13099 	} else if (sc->vxlan_port == v->port) {
13100 		sc->vxlan_refcount++;
13101 	} else {
13102 		CH_ERR(sc, "VXLAN already configured on port  %d; "
13103 		    "ignoring attempt to configure it on port %d\n",
13104 		    sc->vxlan_port, v->port);
13105 	}
13106 	end_synchronized_op(sc, 0);
13107 }
13108 
13109 static void
t4_vxlan_stop(struct adapter * sc,void * arg)13110 t4_vxlan_stop(struct adapter *sc, void *arg)
13111 {
13112 	struct vxlan_evargs *v = arg;
13113 
13114 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13115 		return;
13116 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
13117 		return;
13118 
13119 	/*
13120 	 * VXLANs may have been configured before the driver was loaded so we
13121 	 * may see more stops than starts.  This is not handled cleanly but at
13122 	 * least we keep the refcount sane.
13123 	 */
13124 	if (sc->vxlan_port != v->port)
13125 		goto done;
13126 	if (sc->vxlan_refcount == 0) {
13127 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
13128 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
13129 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
13130 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
13131 done:
13132 	end_synchronized_op(sc, 0);
13133 }
13134 
13135 static void
t4_vxlan_start_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13136 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
13137     sa_family_t family, u_int port)
13138 {
13139 	struct vxlan_evargs v;
13140 
13141 	MPASS(family == AF_INET || family == AF_INET6);
13142 	v.ifp = ifp;
13143 	v.port = port;
13144 
13145 	t4_iterate(t4_vxlan_start, &v);
13146 }
13147 
13148 static void
t4_vxlan_stop_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13149 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
13150     u_int port)
13151 {
13152 	struct vxlan_evargs v;
13153 
13154 	MPASS(family == AF_INET || family == AF_INET6);
13155 	v.ifp = ifp;
13156 	v.port = port;
13157 
13158 	t4_iterate(t4_vxlan_stop, &v);
13159 }
13160 
13161 
13162 static struct sx mlu;	/* mod load unload */
13163 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
13164 
13165 static int
mod_event(module_t mod,int cmd,void * arg)13166 mod_event(module_t mod, int cmd, void *arg)
13167 {
13168 	int rc = 0;
13169 	static int loaded = 0;
13170 
13171 	switch (cmd) {
13172 	case MOD_LOAD:
13173 		sx_xlock(&mlu);
13174 		if (loaded++ == 0) {
13175 			t4_sge_modload();
13176 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13177 			    t4_filter_rpl, CPL_COOKIE_FILTER);
13178 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
13179 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
13180 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
13181 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
13182 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13183 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
13184 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
13185 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
13186 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
13187 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
13188 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
13189 			    do_smt_write_rpl);
13190 			sx_init(&t4_list_lock, "T4/T5 adapters");
13191 			SLIST_INIT(&t4_list);
13192 			callout_init(&fatal_callout, 1);
13193 #ifdef TCP_OFFLOAD
13194 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
13195 #endif
13196 #ifdef INET6
13197 			t4_clip_modload();
13198 #endif
13199 #ifdef KERN_TLS
13200 			t6_ktls_modload();
13201 #endif
13202 			t4_tracer_modload();
13203 			tweak_tunables();
13204 			vxlan_start_evtag =
13205 			    EVENTHANDLER_REGISTER(vxlan_start,
13206 				t4_vxlan_start_handler, NULL,
13207 				EVENTHANDLER_PRI_ANY);
13208 			vxlan_stop_evtag =
13209 			    EVENTHANDLER_REGISTER(vxlan_stop,
13210 				t4_vxlan_stop_handler, NULL,
13211 				EVENTHANDLER_PRI_ANY);
13212 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
13213 			    taskqueue_thread_enqueue, &reset_tq);
13214 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
13215 			    "t4_rst_thr");
13216 		}
13217 		sx_xunlock(&mlu);
13218 		break;
13219 
13220 	case MOD_UNLOAD:
13221 		sx_xlock(&mlu);
13222 		if (--loaded == 0) {
13223 #ifdef TCP_OFFLOAD
13224 			int i;
13225 #endif
13226 			int tries;
13227 
13228 			taskqueue_free(reset_tq);
13229 
13230 			tries = 0;
13231 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
13232 				uprintf("%ju clusters with custom free routine "
13233 				    "still is use.\n", t4_sge_extfree_refs());
13234 				pause("t4unload", 2 * hz);
13235 			}
13236 
13237 			sx_slock(&t4_list_lock);
13238 			if (!SLIST_EMPTY(&t4_list)) {
13239 				rc = EBUSY;
13240 				sx_sunlock(&t4_list_lock);
13241 				goto done_unload;
13242 			}
13243 #ifdef TCP_OFFLOAD
13244 			sx_slock(&t4_uld_list_lock);
13245 			for (i = 0; i <= ULD_MAX; i++) {
13246 				if (t4_uld_list[i] != NULL) {
13247 					rc = EBUSY;
13248 					sx_sunlock(&t4_uld_list_lock);
13249 					sx_sunlock(&t4_list_lock);
13250 					goto done_unload;
13251 				}
13252 			}
13253 			sx_sunlock(&t4_uld_list_lock);
13254 #endif
13255 			sx_sunlock(&t4_list_lock);
13256 
13257 			if (t4_sge_extfree_refs() == 0) {
13258 				EVENTHANDLER_DEREGISTER(vxlan_start,
13259 				    vxlan_start_evtag);
13260 				EVENTHANDLER_DEREGISTER(vxlan_stop,
13261 				    vxlan_stop_evtag);
13262 				t4_tracer_modunload();
13263 #ifdef KERN_TLS
13264 				t6_ktls_modunload();
13265 #endif
13266 #ifdef INET6
13267 				t4_clip_modunload();
13268 #endif
13269 #ifdef TCP_OFFLOAD
13270 				sx_destroy(&t4_uld_list_lock);
13271 #endif
13272 				sx_destroy(&t4_list_lock);
13273 				t4_sge_modunload();
13274 				loaded = 0;
13275 			} else {
13276 				rc = EBUSY;
13277 				loaded++;	/* undo earlier decrement */
13278 			}
13279 		}
13280 done_unload:
13281 		sx_xunlock(&mlu);
13282 		break;
13283 	}
13284 
13285 	return (rc);
13286 }
13287 
13288 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
13289 MODULE_VERSION(t4nex, 1);
13290 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
13291 #ifdef DEV_NETMAP
13292 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
13293 #endif /* DEV_NETMAP */
13294 
13295 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
13296 MODULE_VERSION(t5nex, 1);
13297 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
13298 #ifdef DEV_NETMAP
13299 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
13300 #endif /* DEV_NETMAP */
13301 
13302 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
13303 MODULE_VERSION(t6nex, 1);
13304 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
13305 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
13306 #ifdef DEV_NETMAP
13307 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
13308 #endif /* DEV_NETMAP */
13309 
13310 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
13311 MODULE_VERSION(cxgbe, 1);
13312 
13313 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
13314 MODULE_VERSION(cxl, 1);
13315 
13316 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
13317 MODULE_VERSION(cc, 1);
13318 
13319 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
13320 MODULE_VERSION(vcxgbe, 1);
13321 
13322 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
13323 MODULE_VERSION(vcxl, 1);
13324 
13325 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
13326 MODULE_VERSION(vcc, 1);
13327