xref: /freebsd-14.2/sys/dev/cxgbe/t4_main.c (revision e42a182b)
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 <[email protected]>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_ddb.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_kern_tls.h"
35 #include "opt_ratelimit.h"
36 #include "opt_rss.h"
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/priv.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/eventhandler.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/taskqueue.h>
48 #include <sys/pciio.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pci_private.h>
52 #include <sys/firmware.h>
53 #include <sys/sbuf.h>
54 #include <sys/smp.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <net/if_dl.h>
62 #include <net/if_vlan_var.h>
63 #ifdef RSS
64 #include <net/rss_config.h>
65 #endif
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 #ifdef KERN_TLS
69 #include <netinet/tcp_seq.h>
70 #endif
71 #if defined(__i386__) || defined(__amd64__)
72 #include <machine/md_var.h>
73 #include <machine/cputypes.h>
74 #include <vm/vm.h>
75 #include <vm/pmap.h>
76 #endif
77 #ifdef DDB
78 #include <ddb/ddb.h>
79 #include <ddb/db_lex.h>
80 #endif
81 
82 #include "common/common.h"
83 #include "common/t4_msg.h"
84 #include "common/t4_regs.h"
85 #include "common/t4_regs_values.h"
86 #include "cudbg/cudbg.h"
87 #include "t4_clip.h"
88 #include "t4_ioctl.h"
89 #include "t4_l2t.h"
90 #include "t4_mp_ring.h"
91 #include "t4_if.h"
92 #include "t4_smt.h"
93 
94 /* T4 bus driver interface */
95 static int t4_probe(device_t);
96 static int t4_attach(device_t);
97 static int t4_detach(device_t);
98 static int t4_child_location(device_t, device_t, struct sbuf *);
99 static int t4_ready(device_t);
100 static int t4_read_port_device(device_t, int, device_t *);
101 static int t4_suspend(device_t);
102 static int t4_resume(device_t);
103 static int t4_reset_prepare(device_t, device_t);
104 static int t4_reset_post(device_t, device_t);
105 static device_method_t t4_methods[] = {
106 	DEVMETHOD(device_probe,		t4_probe),
107 	DEVMETHOD(device_attach,	t4_attach),
108 	DEVMETHOD(device_detach,	t4_detach),
109 	DEVMETHOD(device_suspend,	t4_suspend),
110 	DEVMETHOD(device_resume,	t4_resume),
111 
112 	DEVMETHOD(bus_child_location,	t4_child_location),
113 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
114 	DEVMETHOD(bus_reset_post,	t4_reset_post),
115 
116 	DEVMETHOD(t4_is_main_ready,	t4_ready),
117 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
118 
119 	DEVMETHOD_END
120 };
121 static driver_t t4_driver = {
122 	"t4nex",
123 	t4_methods,
124 	sizeof(struct adapter)
125 };
126 
127 
128 /* T4 port (cxgbe) interface */
129 static int cxgbe_probe(device_t);
130 static int cxgbe_attach(device_t);
131 static int cxgbe_detach(device_t);
132 device_method_t cxgbe_methods[] = {
133 	DEVMETHOD(device_probe,		cxgbe_probe),
134 	DEVMETHOD(device_attach,	cxgbe_attach),
135 	DEVMETHOD(device_detach,	cxgbe_detach),
136 	{ 0, 0 }
137 };
138 static driver_t cxgbe_driver = {
139 	"cxgbe",
140 	cxgbe_methods,
141 	sizeof(struct port_info)
142 };
143 
144 /* T4 VI (vcxgbe) interface */
145 static int vcxgbe_probe(device_t);
146 static int vcxgbe_attach(device_t);
147 static int vcxgbe_detach(device_t);
148 static device_method_t vcxgbe_methods[] = {
149 	DEVMETHOD(device_probe,		vcxgbe_probe),
150 	DEVMETHOD(device_attach,	vcxgbe_attach),
151 	DEVMETHOD(device_detach,	vcxgbe_detach),
152 	{ 0, 0 }
153 };
154 static driver_t vcxgbe_driver = {
155 	"vcxgbe",
156 	vcxgbe_methods,
157 	sizeof(struct vi_info)
158 };
159 
160 static d_ioctl_t t4_ioctl;
161 
162 static struct cdevsw t4_cdevsw = {
163        .d_version = D_VERSION,
164        .d_ioctl = t4_ioctl,
165        .d_name = "t4nex",
166 };
167 
168 /* T5 bus driver interface */
169 static int t5_probe(device_t);
170 static device_method_t t5_methods[] = {
171 	DEVMETHOD(device_probe,		t5_probe),
172 	DEVMETHOD(device_attach,	t4_attach),
173 	DEVMETHOD(device_detach,	t4_detach),
174 	DEVMETHOD(device_suspend,	t4_suspend),
175 	DEVMETHOD(device_resume,	t4_resume),
176 
177 	DEVMETHOD(bus_child_location,	t4_child_location),
178 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
179 	DEVMETHOD(bus_reset_post,	t4_reset_post),
180 
181 	DEVMETHOD(t4_is_main_ready,	t4_ready),
182 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
183 
184 	DEVMETHOD_END
185 };
186 static driver_t t5_driver = {
187 	"t5nex",
188 	t5_methods,
189 	sizeof(struct adapter)
190 };
191 
192 
193 /* T5 port (cxl) interface */
194 static driver_t cxl_driver = {
195 	"cxl",
196 	cxgbe_methods,
197 	sizeof(struct port_info)
198 };
199 
200 /* T5 VI (vcxl) interface */
201 static driver_t vcxl_driver = {
202 	"vcxl",
203 	vcxgbe_methods,
204 	sizeof(struct vi_info)
205 };
206 
207 /* T6 bus driver interface */
208 static int t6_probe(device_t);
209 static device_method_t t6_methods[] = {
210 	DEVMETHOD(device_probe,		t6_probe),
211 	DEVMETHOD(device_attach,	t4_attach),
212 	DEVMETHOD(device_detach,	t4_detach),
213 	DEVMETHOD(device_suspend,	t4_suspend),
214 	DEVMETHOD(device_resume,	t4_resume),
215 
216 	DEVMETHOD(bus_child_location,	t4_child_location),
217 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
218 	DEVMETHOD(bus_reset_post,	t4_reset_post),
219 
220 	DEVMETHOD(t4_is_main_ready,	t4_ready),
221 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
222 
223 	DEVMETHOD_END
224 };
225 static driver_t t6_driver = {
226 	"t6nex",
227 	t6_methods,
228 	sizeof(struct adapter)
229 };
230 
231 
232 /* T6 port (cc) interface */
233 static driver_t cc_driver = {
234 	"cc",
235 	cxgbe_methods,
236 	sizeof(struct port_info)
237 };
238 
239 /* T6 VI (vcc) interface */
240 static driver_t vcc_driver = {
241 	"vcc",
242 	vcxgbe_methods,
243 	sizeof(struct vi_info)
244 };
245 
246 /* ifnet interface */
247 static void cxgbe_init(void *);
248 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
249 static int cxgbe_transmit(if_t, struct mbuf *);
250 static void cxgbe_qflush(if_t);
251 #if defined(KERN_TLS) || defined(RATELIMIT)
252 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
253     struct m_snd_tag **);
254 #endif
255 
256 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
257 
258 /*
259  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
260  * then ADAPTER_LOCK, then t4_uld_list_lock.
261  */
262 static struct sx t4_list_lock;
263 SLIST_HEAD(, adapter) t4_list;
264 #ifdef TCP_OFFLOAD
265 static struct sx t4_uld_list_lock;
266 struct uld_info *t4_uld_list[ULD_MAX + 1];
267 #endif
268 
269 /*
270  * Tunables.  See tweak_tunables() too.
271  *
272  * Each tunable is set to a default value here if it's known at compile-time.
273  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
274  * provide a reasonable default (upto n) when the driver is loaded.
275  *
276  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
277  * T5 are under hw.cxl.
278  */
279 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
280     "cxgbe(4) parameters");
281 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
282     "cxgbe(4) T5+ parameters");
283 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
284     "cxgbe(4) TOE parameters");
285 
286 /*
287  * Number of queues for tx and rx, NIC and offload.
288  */
289 #define NTXQ 16
290 int t4_ntxq = -NTXQ;
291 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
292     "Number of TX queues per port");
293 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
294 
295 #define NRXQ 8
296 int t4_nrxq = -NRXQ;
297 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
298     "Number of RX queues per port");
299 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
300 
301 #define NTXQ_VI 1
302 static int t4_ntxq_vi = -NTXQ_VI;
303 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
304     "Number of TX queues per VI");
305 
306 #define NRXQ_VI 1
307 static int t4_nrxq_vi = -NRXQ_VI;
308 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
309     "Number of RX queues per VI");
310 
311 static int t4_rsrv_noflowq = 0;
312 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
313     0, "Reserve TX queue 0 of each VI for non-flowid packets");
314 
315 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
316 #define NOFLDTXQ 8
317 static int t4_nofldtxq = -NOFLDTXQ;
318 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
319     "Number of offload TX queues per port");
320 
321 #define NOFLDRXQ 2
322 static int t4_nofldrxq = -NOFLDRXQ;
323 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
324     "Number of offload RX queues per port");
325 
326 #define NOFLDTXQ_VI 1
327 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
328 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
329     "Number of offload TX queues per VI");
330 
331 #define NOFLDRXQ_VI 1
332 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
333 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
334     "Number of offload RX queues per VI");
335 
336 #define TMR_IDX_OFLD 1
337 int t4_tmr_idx_ofld = TMR_IDX_OFLD;
338 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
339     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
340 
341 #define PKTC_IDX_OFLD (-1)
342 int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
343 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
344     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
345 
346 /* 0 means chip/fw default, non-zero number is value in microseconds */
347 static u_long t4_toe_keepalive_idle = 0;
348 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
349     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
350 
351 /* 0 means chip/fw default, non-zero number is value in microseconds */
352 static u_long t4_toe_keepalive_interval = 0;
353 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
354     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
355 
356 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
357 static int t4_toe_keepalive_count = 0;
358 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
359     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
360 
361 /* 0 means chip/fw default, non-zero number is value in microseconds */
362 static u_long t4_toe_rexmt_min = 0;
363 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
364     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
365 
366 /* 0 means chip/fw default, non-zero number is value in microseconds */
367 static u_long t4_toe_rexmt_max = 0;
368 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
369     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
370 
371 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
372 static int t4_toe_rexmt_count = 0;
373 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
374     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
375 
376 /* -1 means chip/fw default, other values are raw backoff values to use */
377 static int t4_toe_rexmt_backoff[16] = {
378 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
379 };
380 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
381     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
382     "cxgbe(4) TOE retransmit backoff values");
383 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
384     &t4_toe_rexmt_backoff[0], 0, "");
385 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
386     &t4_toe_rexmt_backoff[1], 0, "");
387 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
388     &t4_toe_rexmt_backoff[2], 0, "");
389 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
390     &t4_toe_rexmt_backoff[3], 0, "");
391 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
392     &t4_toe_rexmt_backoff[4], 0, "");
393 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
394     &t4_toe_rexmt_backoff[5], 0, "");
395 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
396     &t4_toe_rexmt_backoff[6], 0, "");
397 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
398     &t4_toe_rexmt_backoff[7], 0, "");
399 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
400     &t4_toe_rexmt_backoff[8], 0, "");
401 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
402     &t4_toe_rexmt_backoff[9], 0, "");
403 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
404     &t4_toe_rexmt_backoff[10], 0, "");
405 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
406     &t4_toe_rexmt_backoff[11], 0, "");
407 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
408     &t4_toe_rexmt_backoff[12], 0, "");
409 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
410     &t4_toe_rexmt_backoff[13], 0, "");
411 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
412     &t4_toe_rexmt_backoff[14], 0, "");
413 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
414     &t4_toe_rexmt_backoff[15], 0, "");
415 
416 int t4_ddp_rcvbuf_len = 256 * 1024;
417 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_len, CTLFLAG_RWTUN,
418     &t4_ddp_rcvbuf_len, 0, "length of each DDP RX buffer");
419 
420 unsigned int t4_ddp_rcvbuf_cache = 4;
421 SYSCTL_UINT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_cache, CTLFLAG_RWTUN,
422     &t4_ddp_rcvbuf_cache, 0,
423     "maximum number of free DDP RX buffers to cache per connection");
424 #endif
425 
426 #ifdef DEV_NETMAP
427 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
428 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
429 static int t4_native_netmap = NN_EXTRA_VI;
430 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
431     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
432 
433 #define NNMTXQ 8
434 static int t4_nnmtxq = -NNMTXQ;
435 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
436     "Number of netmap TX queues");
437 
438 #define NNMRXQ 8
439 static int t4_nnmrxq = -NNMRXQ;
440 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
441     "Number of netmap RX queues");
442 
443 #define NNMTXQ_VI 2
444 static int t4_nnmtxq_vi = -NNMTXQ_VI;
445 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
446     "Number of netmap TX queues per VI");
447 
448 #define NNMRXQ_VI 2
449 static int t4_nnmrxq_vi = -NNMRXQ_VI;
450 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
451     "Number of netmap RX queues per VI");
452 #endif
453 
454 /*
455  * Holdoff parameters for ports.
456  */
457 #define TMR_IDX 1
458 int t4_tmr_idx = TMR_IDX;
459 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
460     0, "Holdoff timer index");
461 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
462 
463 #define PKTC_IDX (-1)
464 int t4_pktc_idx = PKTC_IDX;
465 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
466     0, "Holdoff packet counter index");
467 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
468 
469 /*
470  * Size (# of entries) of each tx and rx queue.
471  */
472 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
473 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
474     "Number of descriptors in each TX queue");
475 
476 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
477 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
478     "Number of descriptors in each RX queue");
479 
480 /*
481  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
482  */
483 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
484 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
485     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
486 
487 /*
488  * Configuration file.  All the _CF names here are special.
489  */
490 #define DEFAULT_CF	"default"
491 #define BUILTIN_CF	"built-in"
492 #define FLASH_CF	"flash"
493 #define UWIRE_CF	"uwire"
494 #define FPGA_CF		"fpga"
495 static char t4_cfg_file[32] = DEFAULT_CF;
496 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
497     sizeof(t4_cfg_file), "Firmware configuration file");
498 
499 /*
500  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
501  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
502  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
503  *            mark or when signalled to do so, 0 to never emit PAUSE.
504  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
505  *                 negotiated settings will override rx_pause/tx_pause.
506  *                 Otherwise rx_pause/tx_pause are applied forcibly.
507  */
508 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
509 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
510     &t4_pause_settings, 0,
511     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
512 
513 /*
514  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
515  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
516  *  0 to disable FEC.
517  */
518 static int t4_fec = -1;
519 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
520     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
521 
522 /*
523  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
524  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
525  * driver runs as if this is set to 0.
526  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
527  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
528  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
529  *    the firmware anyway (may result in l1cfg errors with old firmwares).
530  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
531  *    means set all FEC bits that are valid for the speed.
532  */
533 static int t4_force_fec = -1;
534 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
535     "Controls the use of FORCE_FEC bit in L1 configuration.");
536 
537 /*
538  * Link autonegotiation.
539  * -1 to run with the firmware default.
540  *  0 to disable.
541  *  1 to enable.
542  */
543 static int t4_autoneg = -1;
544 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
545     "Link autonegotiation");
546 
547 /*
548  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
549  * encouraged respectively).  '-n' is the same as 'n' except the firmware
550  * version used in the checks is read from the firmware bundled with the driver.
551  */
552 static int t4_fw_install = 1;
553 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
554     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
555 
556 /*
557  * ASIC features that will be used.  Disable the ones you don't want so that the
558  * chip resources aren't wasted on features that will not be used.
559  */
560 static int t4_nbmcaps_allowed = 0;
561 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
562     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
563 
564 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
565 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
566     &t4_linkcaps_allowed, 0, "Default link capabilities");
567 
568 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
569     FW_CAPS_CONFIG_SWITCH_EGRESS;
570 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
571     &t4_switchcaps_allowed, 0, "Default switch capabilities");
572 
573 #ifdef RATELIMIT
574 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
575 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
576 #else
577 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
578 	FW_CAPS_CONFIG_NIC_HASHFILTER;
579 #endif
580 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
581     &t4_niccaps_allowed, 0, "Default NIC capabilities");
582 
583 static int t4_toecaps_allowed = -1;
584 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
585     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
586 
587 static int t4_rdmacaps_allowed = -1;
588 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
589     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
590 
591 static int t4_cryptocaps_allowed = -1;
592 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
593     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
594 
595 static int t4_iscsicaps_allowed = -1;
596 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
597     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
598 
599 static int t4_fcoecaps_allowed = 0;
600 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
601     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
602 
603 static int t5_write_combine = 0;
604 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
605     0, "Use WC instead of UC for BAR2");
606 
607 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
608 static int t4_doorbells_allowed = 0xf;
609 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
610 	   &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
611 
612 static int t4_num_vis = 1;
613 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
614     "Number of VIs per port");
615 
616 /*
617  * PCIe Relaxed Ordering.
618  * -1: driver should figure out a good value.
619  * 0: disable RO.
620  * 1: enable RO.
621  * 2: leave RO alone.
622  */
623 static int pcie_relaxed_ordering = -1;
624 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
625     &pcie_relaxed_ordering, 0,
626     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
627 
628 static int t4_panic_on_fatal_err = 0;
629 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
630     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
631 
632 static int t4_reset_on_fatal_err = 0;
633 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
634     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
635 
636 static int t4_clock_gate_on_suspend = 0;
637 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
638     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
639 
640 static int t4_tx_vm_wr = 0;
641 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
642     "Use VM work requests to transmit packets.");
643 
644 /*
645  * Set to non-zero to enable the attack filter.  A packet that matches any of
646  * these conditions will get dropped on ingress:
647  * 1) IP && source address == destination address.
648  * 2) TCP/IP && source address is not a unicast address.
649  * 3) TCP/IP && destination address is not a unicast address.
650  * 4) IP && source address is loopback (127.x.y.z).
651  * 5) IP && destination address is loopback (127.x.y.z).
652  * 6) IPv6 && source address == destination address.
653  * 7) IPv6 && source address is not a unicast address.
654  * 8) IPv6 && source address is loopback (::1/128).
655  * 9) IPv6 && destination address is loopback (::1/128).
656  * 10) IPv6 && source address is unspecified (::/128).
657  * 11) IPv6 && destination address is unspecified (::/128).
658  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
659  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
660  */
661 static int t4_attack_filter = 0;
662 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
663     &t4_attack_filter, 0, "Drop suspicious traffic");
664 
665 static int t4_drop_ip_fragments = 0;
666 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
667     &t4_drop_ip_fragments, 0, "Drop IP fragments");
668 
669 static int t4_drop_pkts_with_l2_errors = 1;
670 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
671     &t4_drop_pkts_with_l2_errors, 0,
672     "Drop all frames with Layer 2 length or checksum errors");
673 
674 static int t4_drop_pkts_with_l3_errors = 0;
675 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
676     &t4_drop_pkts_with_l3_errors, 0,
677     "Drop all frames with IP version, length, or checksum errors");
678 
679 static int t4_drop_pkts_with_l4_errors = 0;
680 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
681     &t4_drop_pkts_with_l4_errors, 0,
682     "Drop all frames with Layer 4 length, checksum, or other errors");
683 
684 #ifdef TCP_OFFLOAD
685 /*
686  * TOE tunables.
687  */
688 static int t4_cop_managed_offloading = 0;
689 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
690     &t4_cop_managed_offloading, 0,
691     "COP (Connection Offload Policy) controls all TOE offload");
692 #endif
693 
694 #ifdef KERN_TLS
695 /*
696  * This enables KERN_TLS for all adapters if set.
697  */
698 static int t4_kern_tls = 0;
699 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
700     "Enable KERN_TLS mode for T6 adapters");
701 
702 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
703     "cxgbe(4) KERN_TLS parameters");
704 
705 static int t4_tls_inline_keys = 0;
706 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
707     &t4_tls_inline_keys, 0,
708     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
709     "in card memory.");
710 
711 static int t4_tls_combo_wrs = 0;
712 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
713     0, "Attempt to combine TCB field updates with TLS record work requests.");
714 #endif
715 
716 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
717 static int vi_mac_funcs[] = {
718 	FW_VI_FUNC_ETH,
719 	FW_VI_FUNC_OFLD,
720 	FW_VI_FUNC_IWARP,
721 	FW_VI_FUNC_OPENISCSI,
722 	FW_VI_FUNC_OPENFCOE,
723 	FW_VI_FUNC_FOISCSI,
724 	FW_VI_FUNC_FOFCOE,
725 };
726 
727 struct intrs_and_queues {
728 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
729 	uint16_t num_vis;	/* number of VIs for each port */
730 	uint16_t nirq;		/* Total # of vectors */
731 	uint16_t ntxq;		/* # of NIC txq's for each port */
732 	uint16_t nrxq;		/* # of NIC rxq's for each port */
733 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
734 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
735 	uint16_t nnmtxq;	/* # of netmap txq's */
736 	uint16_t nnmrxq;	/* # of netmap rxq's */
737 
738 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
739 	uint16_t ntxq_vi;	/* # of NIC txq's */
740 	uint16_t nrxq_vi;	/* # of NIC rxq's */
741 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
742 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
743 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
744 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
745 };
746 
747 static void setup_memwin(struct adapter *);
748 static void position_memwin(struct adapter *, int, uint32_t);
749 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
750 static int fwmtype_to_hwmtype(int);
751 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
752     uint32_t *);
753 static int fixup_devlog_params(struct adapter *);
754 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
755 static int contact_firmware(struct adapter *);
756 static int partition_resources(struct adapter *);
757 static int get_params__pre_init(struct adapter *);
758 static int set_params__pre_init(struct adapter *);
759 static int get_params__post_init(struct adapter *);
760 static int set_params__post_init(struct adapter *);
761 static void t4_set_desc(struct adapter *);
762 static bool fixed_ifmedia(struct port_info *);
763 static void build_medialist(struct port_info *);
764 static void init_link_config(struct port_info *);
765 static int fixup_link_config(struct port_info *);
766 static int apply_link_config(struct port_info *);
767 static int cxgbe_init_synchronized(struct vi_info *);
768 static int cxgbe_uninit_synchronized(struct vi_info *);
769 static int adapter_full_init(struct adapter *);
770 static void adapter_full_uninit(struct adapter *);
771 static int vi_full_init(struct vi_info *);
772 static void vi_full_uninit(struct vi_info *);
773 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
774 static void quiesce_txq(struct sge_txq *);
775 static void quiesce_wrq(struct sge_wrq *);
776 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
777 static void quiesce_vi(struct vi_info *);
778 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
779     driver_intr_t *, void *, char *);
780 static int t4_free_irq(struct adapter *, struct irq *);
781 static void t4_init_atid_table(struct adapter *);
782 static void t4_free_atid_table(struct adapter *);
783 static void stop_atid_allocator(struct adapter *);
784 static void restart_atid_allocator(struct adapter *);
785 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
786 static void vi_refresh_stats(struct vi_info *);
787 static void cxgbe_refresh_stats(struct vi_info *);
788 static void cxgbe_tick(void *);
789 static void vi_tick(void *);
790 static void cxgbe_sysctls(struct port_info *);
791 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
792 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
793 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
794 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
795 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
796 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
797 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
798 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
799 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
800 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
801 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
802 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
803 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
804 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
805 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
806 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
807 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
808 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
809 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
810 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
811 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
812 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
813 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
814 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
815 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
816 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
817 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
818 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
819 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
820 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
821 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
822 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
823 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
824 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
825 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
826 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
827 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
828 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
829 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
830 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
831 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
832 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
833 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
834 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
835 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
836 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
837 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
838 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
839 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
840 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
841 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
842 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
843 #ifdef TCP_OFFLOAD
844 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
845 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
846 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
847 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
848 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
849 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
850 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
851 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
852 #endif
853 static int get_sge_context(struct adapter *, struct t4_sge_context *);
854 static int load_fw(struct adapter *, struct t4_data *);
855 static int load_cfg(struct adapter *, struct t4_data *);
856 static int load_boot(struct adapter *, struct t4_bootrom *);
857 static int load_bootcfg(struct adapter *, struct t4_data *);
858 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
859 static void free_offload_policy(struct t4_offload_policy *);
860 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
861 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
862 static int read_i2c(struct adapter *, struct t4_i2c_data *);
863 static int clear_stats(struct adapter *, u_int);
864 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
865 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
866 static inline int stop_adapter(struct adapter *);
867 static inline void set_adapter_hwstatus(struct adapter *, const bool);
868 static int stop_lld(struct adapter *);
869 static inline int restart_adapter(struct adapter *);
870 static int restart_lld(struct adapter *);
871 #ifdef TCP_OFFLOAD
872 static int deactivate_all_uld(struct adapter *);
873 static void stop_all_uld(struct adapter *);
874 static void restart_all_uld(struct adapter *);
875 #endif
876 #ifdef KERN_TLS
877 static int ktls_capability(struct adapter *, bool);
878 #endif
879 static int mod_event(module_t, int, void *);
880 static int notify_siblings(device_t, int);
881 static uint64_t vi_get_counter(if_t, ift_counter);
882 static uint64_t cxgbe_get_counter(if_t, ift_counter);
883 static void enable_vxlan_rx(struct adapter *);
884 static void reset_adapter_task(void *, int);
885 static void fatal_error_task(void *, int);
886 static void dump_devlog(struct adapter *);
887 static void dump_cim_regs(struct adapter *);
888 static void dump_cimla(struct adapter *);
889 
890 struct {
891 	uint16_t device;
892 	char *desc;
893 } t4_pciids[] = {
894 	{0xa000, "Chelsio Terminator 4 FPGA"},
895 	{0x4400, "Chelsio T440-dbg"},
896 	{0x4401, "Chelsio T420-CR"},
897 	{0x4402, "Chelsio T422-CR"},
898 	{0x4403, "Chelsio T440-CR"},
899 	{0x4404, "Chelsio T420-BCH"},
900 	{0x4405, "Chelsio T440-BCH"},
901 	{0x4406, "Chelsio T440-CH"},
902 	{0x4407, "Chelsio T420-SO"},
903 	{0x4408, "Chelsio T420-CX"},
904 	{0x4409, "Chelsio T420-BT"},
905 	{0x440a, "Chelsio T404-BT"},
906 	{0x440e, "Chelsio T440-LP-CR"},
907 }, t5_pciids[] = {
908 	{0xb000, "Chelsio Terminator 5 FPGA"},
909 	{0x5400, "Chelsio T580-dbg"},
910 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
911 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
912 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
913 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
914 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
915 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
916 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
917 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
918 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
919 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
920 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
921 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
922 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
923 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
924 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
925 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
926 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
927 
928 	/* Custom */
929 	{0x5483, "Custom T540-CR"},
930 	{0x5484, "Custom T540-BT"},
931 }, t6_pciids[] = {
932 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
933 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
934 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
935 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
936 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
937 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
938 	{0x6405, "Chelsio T6225-OCP-SO"},	/* 2 x 10/25G, nomem */
939 	{0x6406, "Chelsio T62100-OCP-SO"},	/* 2 x 40/50/100G, nomem */
940 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
941 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
942 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
943 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
944 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
945 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
946 	{0x6414, "Chelsio T61100-OCP-SO"},	/* 1 x 40/50/100G, nomem */
947 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
948 
949 	/* Custom */
950 	{0x6480, "Custom T6225-CR"},
951 	{0x6481, "Custom T62100-CR"},
952 	{0x6482, "Custom T6225-CR"},
953 	{0x6483, "Custom T62100-CR"},
954 	{0x6484, "Custom T64100-CR"},
955 	{0x6485, "Custom T6240-SO"},
956 	{0x6486, "Custom T6225-SO-CR"},
957 	{0x6487, "Custom T6225-CR"},
958 };
959 
960 #ifdef TCP_OFFLOAD
961 /*
962  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
963  * be exactly the same for both rxq and ofld_rxq.
964  */
965 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
966 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
967 #endif
968 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
969 
970 static int
t4_probe(device_t dev)971 t4_probe(device_t dev)
972 {
973 	int i;
974 	uint16_t v = pci_get_vendor(dev);
975 	uint16_t d = pci_get_device(dev);
976 	uint8_t f = pci_get_function(dev);
977 
978 	if (v != PCI_VENDOR_ID_CHELSIO)
979 		return (ENXIO);
980 
981 	/* Attach only to PF0 of the FPGA */
982 	if (d == 0xa000 && f != 0)
983 		return (ENXIO);
984 
985 	for (i = 0; i < nitems(t4_pciids); i++) {
986 		if (d == t4_pciids[i].device) {
987 			device_set_desc(dev, t4_pciids[i].desc);
988 			return (BUS_PROBE_DEFAULT);
989 		}
990 	}
991 
992 	return (ENXIO);
993 }
994 
995 static int
t5_probe(device_t dev)996 t5_probe(device_t dev)
997 {
998 	int i;
999 	uint16_t v = pci_get_vendor(dev);
1000 	uint16_t d = pci_get_device(dev);
1001 	uint8_t f = pci_get_function(dev);
1002 
1003 	if (v != PCI_VENDOR_ID_CHELSIO)
1004 		return (ENXIO);
1005 
1006 	/* Attach only to PF0 of the FPGA */
1007 	if (d == 0xb000 && f != 0)
1008 		return (ENXIO);
1009 
1010 	for (i = 0; i < nitems(t5_pciids); i++) {
1011 		if (d == t5_pciids[i].device) {
1012 			device_set_desc(dev, t5_pciids[i].desc);
1013 			return (BUS_PROBE_DEFAULT);
1014 		}
1015 	}
1016 
1017 	return (ENXIO);
1018 }
1019 
1020 static int
t6_probe(device_t dev)1021 t6_probe(device_t dev)
1022 {
1023 	int i;
1024 	uint16_t v = pci_get_vendor(dev);
1025 	uint16_t d = pci_get_device(dev);
1026 
1027 	if (v != PCI_VENDOR_ID_CHELSIO)
1028 		return (ENXIO);
1029 
1030 	for (i = 0; i < nitems(t6_pciids); i++) {
1031 		if (d == t6_pciids[i].device) {
1032 			device_set_desc(dev, t6_pciids[i].desc);
1033 			return (BUS_PROBE_DEFAULT);
1034 		}
1035 	}
1036 
1037 	return (ENXIO);
1038 }
1039 
1040 static void
t5_attribute_workaround(device_t dev)1041 t5_attribute_workaround(device_t dev)
1042 {
1043 	device_t root_port;
1044 	uint32_t v;
1045 
1046 	/*
1047 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1048 	 * Ordering attributes when replying to a TLP from a Root
1049 	 * Port.  As a workaround, find the parent Root Port and
1050 	 * disable No Snoop and Relaxed Ordering.  Note that this
1051 	 * affects all devices under this root port.
1052 	 */
1053 	root_port = pci_find_pcie_root_port(dev);
1054 	if (root_port == NULL) {
1055 		device_printf(dev, "Unable to find parent root port\n");
1056 		return;
1057 	}
1058 
1059 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1060 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1061 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1062 	    0)
1063 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1064 		    device_get_nameunit(root_port));
1065 }
1066 
1067 static const struct devnames devnames[] = {
1068 	{
1069 		.nexus_name = "t4nex",
1070 		.ifnet_name = "cxgbe",
1071 		.vi_ifnet_name = "vcxgbe",
1072 		.pf03_drv_name = "t4iov",
1073 		.vf_nexus_name = "t4vf",
1074 		.vf_ifnet_name = "cxgbev"
1075 	}, {
1076 		.nexus_name = "t5nex",
1077 		.ifnet_name = "cxl",
1078 		.vi_ifnet_name = "vcxl",
1079 		.pf03_drv_name = "t5iov",
1080 		.vf_nexus_name = "t5vf",
1081 		.vf_ifnet_name = "cxlv"
1082 	}, {
1083 		.nexus_name = "t6nex",
1084 		.ifnet_name = "cc",
1085 		.vi_ifnet_name = "vcc",
1086 		.pf03_drv_name = "t6iov",
1087 		.vf_nexus_name = "t6vf",
1088 		.vf_ifnet_name = "ccv"
1089 	}
1090 };
1091 
1092 void
t4_init_devnames(struct adapter * sc)1093 t4_init_devnames(struct adapter *sc)
1094 {
1095 	int id;
1096 
1097 	id = chip_id(sc);
1098 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
1099 		sc->names = &devnames[id - CHELSIO_T4];
1100 	else {
1101 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1102 		sc->names = NULL;
1103 	}
1104 }
1105 
1106 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1107 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1108 {
1109 	const char *parent, *name;
1110 	long value;
1111 	int line, unit;
1112 
1113 	line = 0;
1114 	parent = device_get_nameunit(sc->dev);
1115 	name = sc->names->ifnet_name;
1116 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1117 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1118 		    value == pi->port_id)
1119 			return (unit);
1120 	}
1121 	return (-1);
1122 }
1123 
1124 static void
t4_calibration(void * arg)1125 t4_calibration(void *arg)
1126 {
1127 	struct adapter *sc;
1128 	struct clock_sync *cur, *nex;
1129 	uint64_t hw;
1130 	sbintime_t sbt;
1131 	int next_up;
1132 
1133 	sc = (struct adapter *)arg;
1134 
1135 	KASSERT((hw_off_limits(sc) == 0), ("hw_off_limits at t4_calibration"));
1136 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1137 	sbt = sbinuptime();
1138 
1139 	cur = &sc->cal_info[sc->cal_current];
1140 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1141 	nex = &sc->cal_info[next_up];
1142 	if (__predict_false(sc->cal_count == 0)) {
1143 		/* First time in, just get the values in */
1144 		cur->hw_cur = hw;
1145 		cur->sbt_cur = sbt;
1146 		sc->cal_count++;
1147 		goto done;
1148 	}
1149 
1150 	if (cur->hw_cur == hw) {
1151 		/* The clock is not advancing? */
1152 		sc->cal_count = 0;
1153 		atomic_store_rel_int(&cur->gen, 0);
1154 		goto done;
1155 	}
1156 
1157 	seqc_write_begin(&nex->gen);
1158 	nex->hw_prev = cur->hw_cur;
1159 	nex->sbt_prev = cur->sbt_cur;
1160 	nex->hw_cur = hw;
1161 	nex->sbt_cur = sbt;
1162 	seqc_write_end(&nex->gen);
1163 	sc->cal_current = next_up;
1164 done:
1165 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1166 	    sc, C_DIRECT_EXEC);
1167 }
1168 
1169 static void
t4_calibration_start(struct adapter * sc)1170 t4_calibration_start(struct adapter *sc)
1171 {
1172 	/*
1173 	 * Here if we have not done a calibration
1174 	 * then do so otherwise start the appropriate
1175 	 * timer.
1176 	 */
1177 	int i;
1178 
1179 	for (i = 0; i < CNT_CAL_INFO; i++) {
1180 		sc->cal_info[i].gen = 0;
1181 	}
1182 	sc->cal_current = 0;
1183 	sc->cal_count = 0;
1184 	sc->cal_gen = 0;
1185 	t4_calibration(sc);
1186 }
1187 
1188 static int
t4_attach(device_t dev)1189 t4_attach(device_t dev)
1190 {
1191 	struct adapter *sc;
1192 	int rc = 0, i, j, rqidx, tqidx, nports;
1193 	struct make_dev_args mda;
1194 	struct intrs_and_queues iaq;
1195 	struct sge *s;
1196 	uint32_t *buf;
1197 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1198 	int ofld_tqidx;
1199 #endif
1200 #ifdef TCP_OFFLOAD
1201 	int ofld_rqidx;
1202 #endif
1203 #ifdef DEV_NETMAP
1204 	int nm_rqidx, nm_tqidx;
1205 #endif
1206 	int num_vis;
1207 
1208 	sc = device_get_softc(dev);
1209 	sc->dev = dev;
1210 	sysctl_ctx_init(&sc->ctx);
1211 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1212 
1213 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1214 		t5_attribute_workaround(dev);
1215 	pci_enable_busmaster(dev);
1216 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1217 		uint32_t v;
1218 
1219 		pci_set_max_read_req(dev, 4096);
1220 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1221 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1222 		if (pcie_relaxed_ordering == 0 &&
1223 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1224 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1225 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1226 		} else if (pcie_relaxed_ordering == 1 &&
1227 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1228 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1229 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1230 		}
1231 	}
1232 
1233 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1234 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1235 	sc->traceq = -1;
1236 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1237 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1238 	    device_get_nameunit(dev));
1239 
1240 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1241 	    device_get_nameunit(dev));
1242 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1243 	t4_add_adapter(sc);
1244 
1245 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1246 	TAILQ_INIT(&sc->sfl);
1247 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1248 
1249 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1250 
1251 	sc->policy = NULL;
1252 	rw_init(&sc->policy_lock, "connection offload policy");
1253 
1254 	callout_init(&sc->ktls_tick, 1);
1255 
1256 	callout_init(&sc->cal_callout, 1);
1257 
1258 	refcount_init(&sc->vxlan_refcount, 0);
1259 
1260 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1261 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1262 
1263 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1264 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1265 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1266 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1267 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1268 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1269 
1270 	rc = t4_map_bars_0_and_4(sc);
1271 	if (rc != 0)
1272 		goto done; /* error message displayed already */
1273 
1274 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1275 
1276 	/* Prepare the adapter for operation. */
1277 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1278 	rc = -t4_prep_adapter(sc, buf);
1279 	free(buf, M_CXGBE);
1280 	if (rc != 0) {
1281 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1282 		goto done;
1283 	}
1284 
1285 	/*
1286 	 * This is the real PF# to which we're attaching.  Works from within PCI
1287 	 * passthrough environments too, where pci_get_function() could return a
1288 	 * different PF# depending on the passthrough configuration.  We need to
1289 	 * use the real PF# in all our communication with the firmware.
1290 	 */
1291 	j = t4_read_reg(sc, A_PL_WHOAMI);
1292 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1293 	sc->mbox = sc->pf;
1294 
1295 	t4_init_devnames(sc);
1296 	if (sc->names == NULL) {
1297 		rc = ENOTSUP;
1298 		goto done; /* error message displayed already */
1299 	}
1300 
1301 	/*
1302 	 * Do this really early, with the memory windows set up even before the
1303 	 * character device.  The userland tool's register i/o and mem read
1304 	 * will work even in "recovery mode".
1305 	 */
1306 	setup_memwin(sc);
1307 	if (t4_init_devlog_params(sc, 0) == 0)
1308 		fixup_devlog_params(sc);
1309 	make_dev_args_init(&mda);
1310 	mda.mda_devsw = &t4_cdevsw;
1311 	mda.mda_uid = UID_ROOT;
1312 	mda.mda_gid = GID_WHEEL;
1313 	mda.mda_mode = 0600;
1314 	mda.mda_si_drv1 = sc;
1315 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1316 	if (rc != 0)
1317 		device_printf(dev, "failed to create nexus char device: %d.\n",
1318 		    rc);
1319 
1320 	/* Go no further if recovery mode has been requested. */
1321 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1322 		device_printf(dev, "recovery mode.\n");
1323 		goto done;
1324 	}
1325 
1326 #if defined(__i386__)
1327 	if ((cpu_feature & CPUID_CX8) == 0) {
1328 		device_printf(dev, "64 bit atomics not available.\n");
1329 		rc = ENOTSUP;
1330 		goto done;
1331 	}
1332 #endif
1333 
1334 	/* Contact the firmware and try to become the master driver. */
1335 	rc = contact_firmware(sc);
1336 	if (rc != 0)
1337 		goto done; /* error message displayed already */
1338 	MPASS(sc->flags & FW_OK);
1339 
1340 	rc = get_params__pre_init(sc);
1341 	if (rc != 0)
1342 		goto done; /* error message displayed already */
1343 
1344 	if (sc->flags & MASTER_PF) {
1345 		rc = partition_resources(sc);
1346 		if (rc != 0)
1347 			goto done; /* error message displayed already */
1348 	}
1349 
1350 	rc = get_params__post_init(sc);
1351 	if (rc != 0)
1352 		goto done; /* error message displayed already */
1353 
1354 	rc = set_params__post_init(sc);
1355 	if (rc != 0)
1356 		goto done; /* error message displayed already */
1357 
1358 	rc = t4_map_bar_2(sc);
1359 	if (rc != 0)
1360 		goto done; /* error message displayed already */
1361 
1362 	rc = t4_adj_doorbells(sc);
1363 	if (rc != 0)
1364 		goto done; /* error message displayed already */
1365 
1366 	rc = t4_create_dma_tag(sc);
1367 	if (rc != 0)
1368 		goto done; /* error message displayed already */
1369 
1370 	/*
1371 	 * First pass over all the ports - allocate VIs and initialize some
1372 	 * basic parameters like mac address, port type, etc.
1373 	 */
1374 	for_each_port(sc, i) {
1375 		struct port_info *pi;
1376 
1377 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1378 		sc->port[i] = pi;
1379 
1380 		/* These must be set before t4_port_init */
1381 		pi->adapter = sc;
1382 		pi->port_id = i;
1383 		/*
1384 		 * XXX: vi[0] is special so we can't delay this allocation until
1385 		 * pi->nvi's final value is known.
1386 		 */
1387 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1388 		    M_ZERO | M_WAITOK);
1389 
1390 		/*
1391 		 * Allocate the "main" VI and initialize parameters
1392 		 * like mac addr.
1393 		 */
1394 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1395 		if (rc != 0) {
1396 			device_printf(dev, "unable to initialize port %d: %d\n",
1397 			    i, rc);
1398 			free(pi->vi, M_CXGBE);
1399 			free(pi, M_CXGBE);
1400 			sc->port[i] = NULL;
1401 			goto done;
1402 		}
1403 
1404 		if (is_bt(pi->port_type))
1405 			setbit(&sc->bt_map, pi->tx_chan);
1406 		else
1407 			MPASS(!isset(&sc->bt_map, pi->tx_chan));
1408 
1409 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1410 		    device_get_nameunit(dev), i);
1411 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1412 		sc->chan_map[pi->tx_chan] = i;
1413 
1414 		/*
1415 		 * The MPS counter for FCS errors doesn't work correctly on the
1416 		 * T6 so we use the MAC counter here.  Which MAC is in use
1417 		 * depends on the link settings which will be known when the
1418 		 * link comes up.
1419 		 */
1420 		if (is_t6(sc))
1421 			pi->fcs_reg = -1;
1422 		else {
1423 			pi->fcs_reg = t4_port_reg(sc, pi->tx_chan,
1424 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1425 		}
1426 		pi->fcs_base = 0;
1427 
1428 		/* All VIs on this port share this media. */
1429 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1430 		    cxgbe_media_status);
1431 
1432 		PORT_LOCK(pi);
1433 		init_link_config(pi);
1434 		fixup_link_config(pi);
1435 		build_medialist(pi);
1436 		if (fixed_ifmedia(pi))
1437 			pi->flags |= FIXED_IFMEDIA;
1438 		PORT_UNLOCK(pi);
1439 
1440 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1441 		    t4_ifnet_unit(sc, pi));
1442 		if (pi->dev == NULL) {
1443 			device_printf(dev,
1444 			    "failed to add device for port %d.\n", i);
1445 			rc = ENXIO;
1446 			goto done;
1447 		}
1448 		pi->vi[0].dev = pi->dev;
1449 		device_set_softc(pi->dev, pi);
1450 	}
1451 
1452 	/*
1453 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1454 	 */
1455 	nports = sc->params.nports;
1456 	rc = cfg_itype_and_nqueues(sc, &iaq);
1457 	if (rc != 0)
1458 		goto done; /* error message displayed already */
1459 
1460 	num_vis = iaq.num_vis;
1461 	sc->intr_type = iaq.intr_type;
1462 	sc->intr_count = iaq.nirq;
1463 
1464 	s = &sc->sge;
1465 	s->nrxq = nports * iaq.nrxq;
1466 	s->ntxq = nports * iaq.ntxq;
1467 	if (num_vis > 1) {
1468 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1469 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1470 	}
1471 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1472 	s->neq += nports;		/* ctrl queues: 1 per port */
1473 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1474 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1475 	if (is_offload(sc) || is_ethoffload(sc)) {
1476 		s->nofldtxq = nports * iaq.nofldtxq;
1477 		if (num_vis > 1)
1478 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1479 		s->neq += s->nofldtxq;
1480 
1481 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1482 		    M_CXGBE, M_ZERO | M_WAITOK);
1483 	}
1484 #endif
1485 #ifdef TCP_OFFLOAD
1486 	if (is_offload(sc)) {
1487 		s->nofldrxq = nports * iaq.nofldrxq;
1488 		if (num_vis > 1)
1489 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1490 		s->neq += s->nofldrxq;	/* free list */
1491 		s->niq += s->nofldrxq;
1492 
1493 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1494 		    M_CXGBE, M_ZERO | M_WAITOK);
1495 	}
1496 #endif
1497 #ifdef DEV_NETMAP
1498 	s->nnmrxq = 0;
1499 	s->nnmtxq = 0;
1500 	if (t4_native_netmap & NN_MAIN_VI) {
1501 		s->nnmrxq += nports * iaq.nnmrxq;
1502 		s->nnmtxq += nports * iaq.nnmtxq;
1503 	}
1504 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1505 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1506 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1507 	}
1508 	s->neq += s->nnmtxq + s->nnmrxq;
1509 	s->niq += s->nnmrxq;
1510 
1511 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1512 	    M_CXGBE, M_ZERO | M_WAITOK);
1513 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1514 	    M_CXGBE, M_ZERO | M_WAITOK);
1515 #endif
1516 	MPASS(s->niq <= s->iqmap_sz);
1517 	MPASS(s->neq <= s->eqmap_sz);
1518 
1519 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1520 	    M_ZERO | M_WAITOK);
1521 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1522 	    M_ZERO | M_WAITOK);
1523 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1524 	    M_ZERO | M_WAITOK);
1525 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1526 	    M_ZERO | M_WAITOK);
1527 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1528 	    M_ZERO | M_WAITOK);
1529 
1530 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1531 	    M_ZERO | M_WAITOK);
1532 
1533 	t4_init_l2t(sc, M_WAITOK);
1534 	t4_init_smt(sc, M_WAITOK);
1535 	t4_init_tx_sched(sc);
1536 	t4_init_atid_table(sc);
1537 #ifdef RATELIMIT
1538 	t4_init_etid_table(sc);
1539 #endif
1540 #ifdef INET6
1541 	t4_init_clip_table(sc);
1542 #endif
1543 	if (sc->vres.key.size != 0)
1544 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1545 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1546 
1547 	/*
1548 	 * Second pass over the ports.  This time we know the number of rx and
1549 	 * tx queues that each port should get.
1550 	 */
1551 	rqidx = tqidx = 0;
1552 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1553 	ofld_tqidx = 0;
1554 #endif
1555 #ifdef TCP_OFFLOAD
1556 	ofld_rqidx = 0;
1557 #endif
1558 #ifdef DEV_NETMAP
1559 	nm_rqidx = nm_tqidx = 0;
1560 #endif
1561 	for_each_port(sc, i) {
1562 		struct port_info *pi = sc->port[i];
1563 		struct vi_info *vi;
1564 
1565 		if (pi == NULL)
1566 			continue;
1567 
1568 		pi->nvi = num_vis;
1569 		for_each_vi(pi, j, vi) {
1570 			vi->pi = pi;
1571 			vi->adapter = sc;
1572 			vi->first_intr = -1;
1573 			vi->qsize_rxq = t4_qsize_rxq;
1574 			vi->qsize_txq = t4_qsize_txq;
1575 
1576 			vi->first_rxq = rqidx;
1577 			vi->first_txq = tqidx;
1578 			vi->tmr_idx = t4_tmr_idx;
1579 			vi->pktc_idx = t4_pktc_idx;
1580 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1581 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1582 
1583 			rqidx += vi->nrxq;
1584 			tqidx += vi->ntxq;
1585 
1586 			if (j == 0 && vi->ntxq > 1)
1587 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1588 			else
1589 				vi->rsrv_noflowq = 0;
1590 
1591 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1592 			vi->first_ofld_txq = ofld_tqidx;
1593 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1594 			ofld_tqidx += vi->nofldtxq;
1595 #endif
1596 #ifdef TCP_OFFLOAD
1597 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1598 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1599 			vi->first_ofld_rxq = ofld_rqidx;
1600 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1601 
1602 			ofld_rqidx += vi->nofldrxq;
1603 #endif
1604 #ifdef DEV_NETMAP
1605 			vi->first_nm_rxq = nm_rqidx;
1606 			vi->first_nm_txq = nm_tqidx;
1607 			if (j == 0) {
1608 				vi->nnmrxq = iaq.nnmrxq;
1609 				vi->nnmtxq = iaq.nnmtxq;
1610 			} else {
1611 				vi->nnmrxq = iaq.nnmrxq_vi;
1612 				vi->nnmtxq = iaq.nnmtxq_vi;
1613 			}
1614 			nm_rqidx += vi->nnmrxq;
1615 			nm_tqidx += vi->nnmtxq;
1616 #endif
1617 		}
1618 	}
1619 
1620 	rc = t4_setup_intr_handlers(sc);
1621 	if (rc != 0) {
1622 		device_printf(dev,
1623 		    "failed to setup interrupt handlers: %d\n", rc);
1624 		goto done;
1625 	}
1626 
1627 	rc = bus_generic_probe(dev);
1628 	if (rc != 0) {
1629 		device_printf(dev, "failed to probe child drivers: %d\n", rc);
1630 		goto done;
1631 	}
1632 
1633 	/*
1634 	 * Ensure thread-safe mailbox access (in debug builds).
1635 	 *
1636 	 * So far this was the only thread accessing the mailbox but various
1637 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1638 	 * will access the mailbox from different threads.
1639 	 */
1640 	sc->flags |= CHK_MBOX_ACCESS;
1641 
1642 	rc = bus_generic_attach(dev);
1643 	if (rc != 0) {
1644 		device_printf(dev,
1645 		    "failed to attach all child ports: %d\n", rc);
1646 		goto done;
1647 	}
1648 	t4_calibration_start(sc);
1649 
1650 	device_printf(dev,
1651 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1652 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1653 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1654 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1655 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1656 
1657 	t4_set_desc(sc);
1658 
1659 	notify_siblings(dev, 0);
1660 
1661 done:
1662 	if (rc != 0 && sc->cdev) {
1663 		/* cdev was created and so cxgbetool works; recover that way. */
1664 		device_printf(dev,
1665 		    "error during attach, adapter is now in recovery mode.\n");
1666 		rc = 0;
1667 	}
1668 
1669 	if (rc != 0)
1670 		t4_detach_common(dev);
1671 	else
1672 		t4_sysctls(sc);
1673 
1674 	return (rc);
1675 }
1676 
1677 static int
t4_child_location(device_t bus,device_t dev,struct sbuf * sb)1678 t4_child_location(device_t bus, device_t dev, struct sbuf *sb)
1679 {
1680 	struct adapter *sc;
1681 	struct port_info *pi;
1682 	int i;
1683 
1684 	sc = device_get_softc(bus);
1685 	for_each_port(sc, i) {
1686 		pi = sc->port[i];
1687 		if (pi != NULL && pi->dev == dev) {
1688 			sbuf_printf(sb, "port=%d", pi->port_id);
1689 			break;
1690 		}
1691 	}
1692 	return (0);
1693 }
1694 
1695 static int
t4_ready(device_t dev)1696 t4_ready(device_t dev)
1697 {
1698 	struct adapter *sc;
1699 
1700 	sc = device_get_softc(dev);
1701 	if (sc->flags & FW_OK)
1702 		return (0);
1703 	return (ENXIO);
1704 }
1705 
1706 static int
t4_read_port_device(device_t dev,int port,device_t * child)1707 t4_read_port_device(device_t dev, int port, device_t *child)
1708 {
1709 	struct adapter *sc;
1710 	struct port_info *pi;
1711 
1712 	sc = device_get_softc(dev);
1713 	if (port < 0 || port >= MAX_NPORTS)
1714 		return (EINVAL);
1715 	pi = sc->port[port];
1716 	if (pi == NULL || pi->dev == NULL)
1717 		return (ENXIO);
1718 	*child = pi->dev;
1719 	return (0);
1720 }
1721 
1722 static int
notify_siblings(device_t dev,int detaching)1723 notify_siblings(device_t dev, int detaching)
1724 {
1725 	device_t sibling;
1726 	int error, i;
1727 
1728 	error = 0;
1729 	for (i = 0; i < PCI_FUNCMAX; i++) {
1730 		if (i == pci_get_function(dev))
1731 			continue;
1732 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1733 		    pci_get_slot(dev), i);
1734 		if (sibling == NULL || !device_is_attached(sibling))
1735 			continue;
1736 		if (detaching)
1737 			error = T4_DETACH_CHILD(sibling);
1738 		else
1739 			(void)T4_ATTACH_CHILD(sibling);
1740 		if (error)
1741 			break;
1742 	}
1743 	return (error);
1744 }
1745 
1746 /*
1747  * Idempotent
1748  */
1749 static int
t4_detach(device_t dev)1750 t4_detach(device_t dev)
1751 {
1752 	int rc;
1753 
1754 	rc = notify_siblings(dev, 1);
1755 	if (rc) {
1756 		device_printf(dev,
1757 		    "failed to detach sibling devices: %d\n", rc);
1758 		return (rc);
1759 	}
1760 
1761 	return (t4_detach_common(dev));
1762 }
1763 
1764 int
t4_detach_common(device_t dev)1765 t4_detach_common(device_t dev)
1766 {
1767 	struct adapter *sc;
1768 	struct port_info *pi;
1769 	int i, rc;
1770 
1771 	sc = device_get_softc(dev);
1772 
1773 #ifdef TCP_OFFLOAD
1774 	rc = deactivate_all_uld(sc);
1775 	if (rc) {
1776 		device_printf(dev,
1777 		    "failed to detach upper layer drivers: %d\n", rc);
1778 		return (rc);
1779 	}
1780 #endif
1781 
1782 	if (sc->cdev) {
1783 		destroy_dev(sc->cdev);
1784 		sc->cdev = NULL;
1785 	}
1786 
1787 	sx_xlock(&t4_list_lock);
1788 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1789 	sx_xunlock(&t4_list_lock);
1790 
1791 	sc->flags &= ~CHK_MBOX_ACCESS;
1792 	if (sc->flags & FULL_INIT_DONE) {
1793 		if (!(sc->flags & IS_VF))
1794 			t4_intr_disable(sc);
1795 	}
1796 
1797 	if (device_is_attached(dev)) {
1798 		rc = bus_generic_detach(dev);
1799 		if (rc) {
1800 			device_printf(dev,
1801 			    "failed to detach child devices: %d\n", rc);
1802 			return (rc);
1803 		}
1804 	}
1805 
1806 	for (i = 0; i < sc->intr_count; i++)
1807 		t4_free_irq(sc, &sc->irq[i]);
1808 
1809 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1810 		t4_free_tx_sched(sc);
1811 
1812 	for (i = 0; i < MAX_NPORTS; i++) {
1813 		pi = sc->port[i];
1814 		if (pi) {
1815 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1816 			if (pi->dev)
1817 				device_delete_child(dev, pi->dev);
1818 
1819 			mtx_destroy(&pi->pi_lock);
1820 			free(pi->vi, M_CXGBE);
1821 			free(pi, M_CXGBE);
1822 		}
1823 	}
1824 	callout_stop(&sc->cal_callout);
1825 	callout_drain(&sc->cal_callout);
1826 	device_delete_children(dev);
1827 	sysctl_ctx_free(&sc->ctx);
1828 	adapter_full_uninit(sc);
1829 
1830 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1831 		t4_fw_bye(sc, sc->mbox);
1832 
1833 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1834 		pci_release_msi(dev);
1835 
1836 	if (sc->regs_res)
1837 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1838 		    sc->regs_res);
1839 
1840 	if (sc->udbs_res)
1841 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1842 		    sc->udbs_res);
1843 
1844 	if (sc->msix_res)
1845 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1846 		    sc->msix_res);
1847 
1848 	if (sc->l2t)
1849 		t4_free_l2t(sc);
1850 	if (sc->smt)
1851 		t4_free_smt(sc->smt);
1852 	t4_free_atid_table(sc);
1853 #ifdef RATELIMIT
1854 	t4_free_etid_table(sc);
1855 #endif
1856 	if (sc->key_map)
1857 		vmem_destroy(sc->key_map);
1858 #ifdef INET6
1859 	t4_destroy_clip_table(sc);
1860 #endif
1861 
1862 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1863 	free(sc->sge.ofld_txq, M_CXGBE);
1864 #endif
1865 #ifdef TCP_OFFLOAD
1866 	free(sc->sge.ofld_rxq, M_CXGBE);
1867 #endif
1868 #ifdef DEV_NETMAP
1869 	free(sc->sge.nm_rxq, M_CXGBE);
1870 	free(sc->sge.nm_txq, M_CXGBE);
1871 #endif
1872 	free(sc->irq, M_CXGBE);
1873 	free(sc->sge.rxq, M_CXGBE);
1874 	free(sc->sge.txq, M_CXGBE);
1875 	free(sc->sge.ctrlq, M_CXGBE);
1876 	free(sc->sge.iqmap, M_CXGBE);
1877 	free(sc->sge.eqmap, M_CXGBE);
1878 	free(sc->tids.ftid_tab, M_CXGBE);
1879 	free(sc->tids.hpftid_tab, M_CXGBE);
1880 	free_hftid_hash(&sc->tids);
1881 	free(sc->tids.tid_tab, M_CXGBE);
1882 	t4_destroy_dma_tag(sc);
1883 
1884 	callout_drain(&sc->ktls_tick);
1885 	callout_drain(&sc->sfl_callout);
1886 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1887 		mtx_destroy(&sc->tids.ftid_lock);
1888 		cv_destroy(&sc->tids.ftid_cv);
1889 	}
1890 	if (mtx_initialized(&sc->tids.atid_lock))
1891 		mtx_destroy(&sc->tids.atid_lock);
1892 	if (mtx_initialized(&sc->ifp_lock))
1893 		mtx_destroy(&sc->ifp_lock);
1894 
1895 	if (rw_initialized(&sc->policy_lock)) {
1896 		rw_destroy(&sc->policy_lock);
1897 #ifdef TCP_OFFLOAD
1898 		if (sc->policy != NULL)
1899 			free_offload_policy(sc->policy);
1900 #endif
1901 	}
1902 
1903 	for (i = 0; i < NUM_MEMWIN; i++) {
1904 		struct memwin *mw = &sc->memwin[i];
1905 
1906 		if (rw_initialized(&mw->mw_lock))
1907 			rw_destroy(&mw->mw_lock);
1908 	}
1909 
1910 	mtx_destroy(&sc->sfl_lock);
1911 	mtx_destroy(&sc->reg_lock);
1912 	mtx_destroy(&sc->sc_lock);
1913 
1914 	bzero(sc, sizeof(*sc));
1915 
1916 	return (0);
1917 }
1918 
1919 static inline int
stop_adapter(struct adapter * sc)1920 stop_adapter(struct adapter *sc)
1921 {
1922 	struct port_info *pi;
1923 	int i;
1924 
1925 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1926 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1927 			 __func__, curthread, sc->flags, sc->error_flags);
1928 		return (EALREADY);
1929 	}
1930 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1931 		 sc->flags, sc->error_flags);
1932 	t4_shutdown_adapter(sc);
1933 	for_each_port(sc, i) {
1934 		pi = sc->port[i];
1935 		PORT_LOCK(pi);
1936 		if (pi->up_vis > 0 && pi->link_cfg.link_ok) {
1937 			/*
1938 			 * t4_shutdown_adapter has already shut down all the
1939 			 * PHYs but it also disables interrupts and DMA so there
1940 			 * won't be a link interrupt.  Update the state manually
1941 			 * if the link was up previously and inform the kernel.
1942 			 */
1943 			pi->link_cfg.link_ok = false;
1944 			t4_os_link_changed(pi);
1945 		}
1946 		PORT_UNLOCK(pi);
1947 	}
1948 
1949 	return (0);
1950 }
1951 
1952 static inline int
restart_adapter(struct adapter * sc)1953 restart_adapter(struct adapter *sc)
1954 {
1955 	uint32_t val;
1956 
1957 	if (!atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1958 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1959 			 __func__, curthread, sc->flags, sc->error_flags);
1960 		return (EALREADY);
1961 	}
1962 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1963 		 sc->flags, sc->error_flags);
1964 
1965 	MPASS(hw_off_limits(sc));
1966 	MPASS((sc->flags & FW_OK) == 0);
1967 	MPASS((sc->flags & MASTER_PF) == 0);
1968 	MPASS(sc->reset_thread == NULL);
1969 
1970 	/*
1971 	 * The adapter is supposed to be back on PCIE with its config space and
1972 	 * BARs restored to their state before reset.  Register access via
1973 	 * t4_read_reg BAR0 should just work.
1974 	 */
1975 	sc->reset_thread = curthread;
1976 	val = t4_read_reg(sc, A_PL_WHOAMI);
1977 	if (val == 0xffffffff || val == 0xeeeeeeee) {
1978 		CH_ERR(sc, "%s: device registers not readable.\n", __func__);
1979 		sc->reset_thread = NULL;
1980 		atomic_set_int(&sc->error_flags, ADAP_STOPPED);
1981 		return (ENXIO);
1982 	}
1983 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR);
1984 	atomic_add_int(&sc->incarnation, 1);
1985 	atomic_add_int(&sc->num_resets, 1);
1986 
1987 	return (0);
1988 }
1989 
1990 static inline void
set_adapter_hwstatus(struct adapter * sc,const bool usable)1991 set_adapter_hwstatus(struct adapter *sc, const bool usable)
1992 {
1993 	if (usable) {
1994 		/* Must be marked reusable by the designated thread. */
1995 		ASSERT_SYNCHRONIZED_OP(sc);
1996 		MPASS(sc->reset_thread == curthread);
1997 		mtx_lock(&sc->reg_lock);
1998 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
1999 		mtx_unlock(&sc->reg_lock);
2000 	} else {
2001 		/* Mark the adapter totally off limits. */
2002 		begin_synchronized_op(sc, NULL, SLEEP_OK, "t4hwsts");
2003 		mtx_lock(&sc->reg_lock);
2004 		atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
2005 		mtx_unlock(&sc->reg_lock);
2006 		sc->flags &= ~(FW_OK | MASTER_PF);
2007 		sc->reset_thread = NULL;
2008 		end_synchronized_op(sc, 0);
2009 	}
2010 }
2011 
2012 static int
stop_lld(struct adapter * sc)2013 stop_lld(struct adapter *sc)
2014 {
2015 	struct port_info *pi;
2016 	struct vi_info *vi;
2017 	if_t ifp;
2018 	struct sge_rxq *rxq;
2019 	struct sge_txq *txq;
2020 	struct sge_wrq *wrq;
2021 #ifdef TCP_OFFLOAD
2022 	struct sge_ofld_rxq *ofld_rxq;
2023 #endif
2024 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2025 	struct sge_ofld_txq *ofld_txq;
2026 #endif
2027 	int rc, i, j, k;
2028 
2029 	/*
2030 	 * XXX: Can there be a synch_op in progress that will hang because
2031 	 * hardware has been stopped?  We'll hang too and the solution will be
2032 	 * to use a version of begin_synch_op that wakes up existing synch_op
2033 	 * with errors.  Maybe stop_adapter should do this wakeup?
2034 	 *
2035 	 * I don't think any synch_op could get stranded waiting for DMA or
2036 	 * interrupt so I think we're okay here.  Remove this comment block
2037 	 * after testing.
2038 	 */
2039 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4slld");
2040 	if (rc != 0)
2041 		return (ENXIO);
2042 
2043 	/* Quiesce all activity. */
2044 	for_each_port(sc, i) {
2045 		pi = sc->port[i];
2046 		pi->vxlan_tcam_entry = false;
2047 		for_each_vi(pi, j, vi) {
2048 			vi->xact_addr_filt = -1;
2049 			mtx_lock(&vi->tick_mtx);
2050 			vi->flags |= VI_SKIP_STATS;
2051 			mtx_unlock(&vi->tick_mtx);
2052 			if (!(vi->flags & VI_INIT_DONE))
2053 				continue;
2054 
2055 			ifp = vi->ifp;
2056 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2057 				mtx_lock(&vi->tick_mtx);
2058 				callout_stop(&vi->tick);
2059 				mtx_unlock(&vi->tick_mtx);
2060 				callout_drain(&vi->tick);
2061 			}
2062 
2063 			/*
2064 			 * Note that the HW is not available.
2065 			 */
2066 			for_each_txq(vi, k, txq) {
2067 				TXQ_LOCK(txq);
2068 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2069 				TXQ_UNLOCK(txq);
2070 			}
2071 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2072 			for_each_ofld_txq(vi, k, ofld_txq) {
2073 				TXQ_LOCK(&ofld_txq->wrq);
2074 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2075 				TXQ_UNLOCK(&ofld_txq->wrq);
2076 			}
2077 #endif
2078 			for_each_rxq(vi, k, rxq) {
2079 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2080 			}
2081 #if defined(TCP_OFFLOAD)
2082 			for_each_ofld_rxq(vi, k, ofld_rxq) {
2083 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2084 			}
2085 #endif
2086 
2087 			quiesce_vi(vi);
2088 		}
2089 
2090 		if (sc->flags & FULL_INIT_DONE) {
2091 			/* Control queue */
2092 			wrq = &sc->sge.ctrlq[i];
2093 			TXQ_LOCK(wrq);
2094 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2095 			TXQ_UNLOCK(wrq);
2096 			quiesce_wrq(wrq);
2097 		}
2098 
2099 		if (pi->flags & HAS_TRACEQ) {
2100 			pi->flags &= ~HAS_TRACEQ;
2101 			sc->traceq = -1;
2102 			sc->tracer_valid = 0;
2103 			sc->tracer_enabled = 0;
2104 		}
2105 	}
2106 	if (sc->flags & FULL_INIT_DONE) {
2107 		/* Firmware event queue */
2108 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2109 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2110 	}
2111 
2112 	/* Stop calibration */
2113 	callout_stop(&sc->cal_callout);
2114 	callout_drain(&sc->cal_callout);
2115 
2116 	if (t4_clock_gate_on_suspend) {
2117 		t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2118 		    F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2119 		    F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2120 	}
2121 
2122 	end_synchronized_op(sc, 0);
2123 
2124 	stop_atid_allocator(sc);
2125 	t4_stop_l2t(sc);
2126 
2127 	return (rc);
2128 }
2129 
2130 int
suspend_adapter(struct adapter * sc)2131 suspend_adapter(struct adapter *sc)
2132 {
2133 	stop_adapter(sc);
2134 	stop_lld(sc);
2135 #ifdef TCP_OFFLOAD
2136 	stop_all_uld(sc);
2137 #endif
2138 	set_adapter_hwstatus(sc, false);
2139 
2140 	return (0);
2141 }
2142 
2143 static int
t4_suspend(device_t dev)2144 t4_suspend(device_t dev)
2145 {
2146 	struct adapter *sc = device_get_softc(dev);
2147 	int rc;
2148 
2149 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2150 	rc = suspend_adapter(sc);
2151 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2152 
2153 	return (rc);
2154 }
2155 
2156 struct adapter_pre_reset_state {
2157 	u_int flags;
2158 	uint16_t nbmcaps;
2159 	uint16_t linkcaps;
2160 	uint16_t switchcaps;
2161 	uint16_t niccaps;
2162 	uint16_t toecaps;
2163 	uint16_t rdmacaps;
2164 	uint16_t cryptocaps;
2165 	uint16_t iscsicaps;
2166 	uint16_t fcoecaps;
2167 
2168 	u_int cfcsum;
2169 	char cfg_file[32];
2170 
2171 	struct adapter_params params;
2172 	struct t4_virt_res vres;
2173 	struct tid_info tids;
2174 	struct sge sge;
2175 
2176 	int rawf_base;
2177 	int nrawf;
2178 
2179 };
2180 
2181 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2182 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2183 {
2184 
2185 	ASSERT_SYNCHRONIZED_OP(sc);
2186 
2187 	o->flags = sc->flags;
2188 
2189 	o->nbmcaps =  sc->nbmcaps;
2190 	o->linkcaps = sc->linkcaps;
2191 	o->switchcaps = sc->switchcaps;
2192 	o->niccaps = sc->niccaps;
2193 	o->toecaps = sc->toecaps;
2194 	o->rdmacaps = sc->rdmacaps;
2195 	o->cryptocaps = sc->cryptocaps;
2196 	o->iscsicaps = sc->iscsicaps;
2197 	o->fcoecaps = sc->fcoecaps;
2198 
2199 	o->cfcsum = sc->cfcsum;
2200 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2201 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2202 
2203 	o->params = sc->params;
2204 	o->vres = sc->vres;
2205 	o->tids = sc->tids;
2206 	o->sge = sc->sge;
2207 
2208 	o->rawf_base = sc->rawf_base;
2209 	o->nrawf = sc->nrawf;
2210 }
2211 
2212 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2213 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2214 {
2215 	int rc = 0;
2216 
2217 	ASSERT_SYNCHRONIZED_OP(sc);
2218 
2219 	/* Capabilities */
2220 #define COMPARE_CAPS(c) do { \
2221 	if (o->c##caps != sc->c##caps) { \
2222 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2223 		    sc->c##caps); \
2224 		rc = EINVAL; \
2225 	} \
2226 } while (0)
2227 	COMPARE_CAPS(nbm);
2228 	COMPARE_CAPS(link);
2229 	COMPARE_CAPS(switch);
2230 	COMPARE_CAPS(nic);
2231 	COMPARE_CAPS(toe);
2232 	COMPARE_CAPS(rdma);
2233 	COMPARE_CAPS(crypto);
2234 	COMPARE_CAPS(iscsi);
2235 	COMPARE_CAPS(fcoe);
2236 #undef COMPARE_CAPS
2237 
2238 	/* Firmware config file */
2239 	if (o->cfcsum != sc->cfcsum) {
2240 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2241 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2242 		rc = EINVAL;
2243 	}
2244 
2245 #define COMPARE_PARAM(p, name) do { \
2246 	if (o->p != sc->p) { \
2247 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2248 		rc = EINVAL; \
2249 	} \
2250 } while (0)
2251 	COMPARE_PARAM(sge.iq_start, iq_start);
2252 	COMPARE_PARAM(sge.eq_start, eq_start);
2253 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2254 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2255 	COMPARE_PARAM(tids.nftids, nftids);
2256 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2257 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2258 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2259 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2260 	COMPARE_PARAM(tids.tid_base, tid_base);
2261 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2262 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2263 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2264 	COMPARE_PARAM(rawf_base, rawf_base);
2265 	COMPARE_PARAM(nrawf, nrawf);
2266 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2267 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2268 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2269 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2270 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2271 	COMPARE_PARAM(tids.ntids, ntids);
2272 	COMPARE_PARAM(tids.etid_base, etid_base);
2273 	COMPARE_PARAM(tids.etid_end, etid_end);
2274 	COMPARE_PARAM(tids.netids, netids);
2275 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2276 	COMPARE_PARAM(params.ethoffload, ethoffload);
2277 	COMPARE_PARAM(tids.natids, natids);
2278 	COMPARE_PARAM(tids.stid_base, stid_base);
2279 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2280 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2281 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2282 	COMPARE_PARAM(vres.stag.start, stag_start);
2283 	COMPARE_PARAM(vres.stag.size, stag_size);
2284 	COMPARE_PARAM(vres.rq.start, rq_start);
2285 	COMPARE_PARAM(vres.rq.size, rq_size);
2286 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2287 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2288 	COMPARE_PARAM(vres.qp.start, qp_start);
2289 	COMPARE_PARAM(vres.qp.size, qp_size);
2290 	COMPARE_PARAM(vres.cq.start, cq_start);
2291 	COMPARE_PARAM(vres.cq.size, cq_size);
2292 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2293 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2294 	COMPARE_PARAM(vres.srq.start, srq_start);
2295 	COMPARE_PARAM(vres.srq.size, srq_size);
2296 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2297 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2298 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2299 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2300 	COMPARE_PARAM(vres.key.start, key_start);
2301 	COMPARE_PARAM(vres.key.size, key_size);
2302 #undef COMPARE_PARAM
2303 
2304 	return (rc);
2305 }
2306 
2307 static int
restart_lld(struct adapter * sc)2308 restart_lld(struct adapter *sc)
2309 {
2310 	struct adapter_pre_reset_state *old_state = NULL;
2311 	struct port_info *pi;
2312 	struct vi_info *vi;
2313 	if_t ifp;
2314 	struct sge_txq *txq;
2315 	int rc, i, j, k;
2316 
2317 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rlld");
2318 	if (rc != 0)
2319 		return (ENXIO);
2320 
2321 	/* Restore memory window. */
2322 	setup_memwin(sc);
2323 
2324 	/* Go no further if recovery mode has been requested. */
2325 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2326 		CH_ALERT(sc, "%s: recovery mode during restart.\n", __func__);
2327 		rc = 0;
2328 		set_adapter_hwstatus(sc, true);
2329 		goto done;
2330 	}
2331 
2332 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2333 	save_caps_and_params(sc, old_state);
2334 
2335 	/* Reestablish contact with firmware and become the primary PF. */
2336 	rc = contact_firmware(sc);
2337 	if (rc != 0)
2338 		goto done; /* error message displayed already */
2339 	MPASS(sc->flags & FW_OK);
2340 
2341 	if (sc->flags & MASTER_PF) {
2342 		rc = partition_resources(sc);
2343 		if (rc != 0)
2344 			goto done; /* error message displayed already */
2345 	}
2346 
2347 	rc = get_params__post_init(sc);
2348 	if (rc != 0)
2349 		goto done; /* error message displayed already */
2350 
2351 	rc = set_params__post_init(sc);
2352 	if (rc != 0)
2353 		goto done; /* error message displayed already */
2354 
2355 	rc = compare_caps_and_params(sc, old_state);
2356 	if (rc != 0)
2357 		goto done; /* error message displayed already */
2358 
2359 	for_each_port(sc, i) {
2360 		pi = sc->port[i];
2361 		MPASS(pi != NULL);
2362 		MPASS(pi->vi != NULL);
2363 		MPASS(pi->vi[0].dev == pi->dev);
2364 
2365 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2366 		if (rc != 0) {
2367 			CH_ERR(sc,
2368 			    "failed to re-initialize port %d: %d\n", i, rc);
2369 			goto done;
2370 		}
2371 		MPASS(sc->chan_map[pi->tx_chan] == i);
2372 
2373 		PORT_LOCK(pi);
2374 		fixup_link_config(pi);
2375 		build_medialist(pi);
2376 		PORT_UNLOCK(pi);
2377 		for_each_vi(pi, j, vi) {
2378 			if (IS_MAIN_VI(vi))
2379 				continue;
2380 			rc = alloc_extra_vi(sc, pi, vi);
2381 			if (rc != 0) {
2382 				CH_ERR(vi,
2383 				    "failed to re-allocate extra VI: %d\n", rc);
2384 				goto done;
2385 			}
2386 		}
2387 	}
2388 
2389 	/*
2390 	 * Interrupts and queues are about to be enabled and other threads will
2391 	 * want to access the hardware too.  It is safe to do so.  Note that
2392 	 * this thread is still in the middle of a synchronized_op.
2393 	 */
2394 	set_adapter_hwstatus(sc, true);
2395 
2396 	if (sc->flags & FULL_INIT_DONE) {
2397 		rc = adapter_full_init(sc);
2398 		if (rc != 0) {
2399 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2400 			goto done;
2401 		}
2402 
2403 		if (sc->vxlan_refcount > 0)
2404 			enable_vxlan_rx(sc);
2405 
2406 		for_each_port(sc, i) {
2407 			pi = sc->port[i];
2408 			for_each_vi(pi, j, vi) {
2409 				mtx_lock(&vi->tick_mtx);
2410 				vi->flags &= ~VI_SKIP_STATS;
2411 				mtx_unlock(&vi->tick_mtx);
2412 				if (!(vi->flags & VI_INIT_DONE))
2413 					continue;
2414 				rc = vi_full_init(vi);
2415 				if (rc != 0) {
2416 					CH_ERR(vi, "failed to re-initialize "
2417 					    "interface: %d\n", rc);
2418 					goto done;
2419 				}
2420 				if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
2421 					sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
2422 					t4_write_reg(sc, is_t4(sc) ?
2423 					    A_MPS_TRC_RSS_CONTROL :
2424 					    A_MPS_T5_TRC_RSS_CONTROL,
2425 					    V_RSSCONTROL(pi->tx_chan) |
2426 					    V_QUEUENUMBER(sc->traceq));
2427 					pi->flags |= HAS_TRACEQ;
2428 				}
2429 
2430 				ifp = vi->ifp;
2431 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2432 					continue;
2433 				/*
2434 				 * Note that we do not setup multicast addresses
2435 				 * in the first pass.  This ensures that the
2436 				 * unicast DMACs for all VIs on all ports get an
2437 				 * MPS TCAM entry.
2438 				 */
2439 				rc = update_mac_settings(ifp, XGMAC_ALL &
2440 				    ~XGMAC_MCADDRS);
2441 				if (rc != 0) {
2442 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2443 					goto done;
2444 				}
2445 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2446 				    true);
2447 				if (rc != 0) {
2448 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2449 					goto done;
2450 				}
2451 				for_each_txq(vi, k, txq) {
2452 					TXQ_LOCK(txq);
2453 					txq->eq.flags |= EQ_ENABLED;
2454 					TXQ_UNLOCK(txq);
2455 				}
2456 				mtx_lock(&vi->tick_mtx);
2457 				callout_schedule(&vi->tick, hz);
2458 				mtx_unlock(&vi->tick_mtx);
2459 			}
2460 			PORT_LOCK(pi);
2461 			if (pi->up_vis > 0) {
2462 				t4_update_port_info(pi);
2463 				fixup_link_config(pi);
2464 				build_medialist(pi);
2465 				apply_link_config(pi);
2466 				if (pi->link_cfg.link_ok)
2467 					t4_os_link_changed(pi);
2468 			}
2469 			PORT_UNLOCK(pi);
2470 		}
2471 
2472 		/* Now reprogram the L2 multicast addresses. */
2473 		for_each_port(sc, i) {
2474 			pi = sc->port[i];
2475 			for_each_vi(pi, j, vi) {
2476 				if (!(vi->flags & VI_INIT_DONE))
2477 					continue;
2478 				ifp = vi->ifp;
2479 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2480 					continue;
2481 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2482 				if (rc != 0) {
2483 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2484 					rc = 0;	/* carry on */
2485 				}
2486 			}
2487 		}
2488 	}
2489 
2490 	/* Reset all calibration */
2491 	t4_calibration_start(sc);
2492 done:
2493 	end_synchronized_op(sc, 0);
2494 	free(old_state, M_CXGBE);
2495 
2496 	restart_atid_allocator(sc);
2497 	t4_restart_l2t(sc);
2498 
2499 	return (rc);
2500 }
2501 
2502 int
resume_adapter(struct adapter * sc)2503 resume_adapter(struct adapter *sc)
2504 {
2505 	restart_adapter(sc);
2506 	restart_lld(sc);
2507 #ifdef TCP_OFFLOAD
2508 	restart_all_uld(sc);
2509 #endif
2510 	return (0);
2511 }
2512 
2513 static int
t4_resume(device_t dev)2514 t4_resume(device_t dev)
2515 {
2516 	struct adapter *sc = device_get_softc(dev);
2517 	int rc;
2518 
2519 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2520 	rc = resume_adapter(sc);
2521 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2522 
2523 	return (rc);
2524 }
2525 
2526 static int
t4_reset_prepare(device_t dev,device_t child)2527 t4_reset_prepare(device_t dev, device_t child)
2528 {
2529 	struct adapter *sc = device_get_softc(dev);
2530 
2531 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2532 	return (0);
2533 }
2534 
2535 static int
t4_reset_post(device_t dev,device_t child)2536 t4_reset_post(device_t dev, device_t child)
2537 {
2538 	struct adapter *sc = device_get_softc(dev);
2539 
2540 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2541 	return (0);
2542 }
2543 
2544 static int
reset_adapter_with_pci_bus_reset(struct adapter * sc)2545 reset_adapter_with_pci_bus_reset(struct adapter *sc)
2546 {
2547 	int rc;
2548 
2549 	mtx_lock(&Giant);
2550 	rc = BUS_RESET_CHILD(device_get_parent(sc->dev), sc->dev, 0);
2551 	mtx_unlock(&Giant);
2552 	return (rc);
2553 }
2554 
2555 static int
reset_adapter_with_pl_rst(struct adapter * sc)2556 reset_adapter_with_pl_rst(struct adapter *sc)
2557 {
2558 	suspend_adapter(sc);
2559 
2560 	/* This is a t4_write_reg without the hw_off_limits check. */
2561 	MPASS(sc->error_flags & HW_OFF_LIMITS);
2562 	bus_space_write_4(sc->bt, sc->bh, A_PL_RST,
2563 			  F_PIORSTMODE | F_PIORST | F_AUTOPCIEPAUSE);
2564 	pause("pl_rst", 1 * hz);		/* Wait 1s for reset */
2565 
2566 	resume_adapter(sc);
2567 
2568 	return (0);
2569 }
2570 
2571 static inline int
reset_adapter(struct adapter * sc)2572 reset_adapter(struct adapter *sc)
2573 {
2574 	if (vm_guest == 0)
2575 		return (reset_adapter_with_pci_bus_reset(sc));
2576 	else
2577 		return (reset_adapter_with_pl_rst(sc));
2578 }
2579 
2580 static void
reset_adapter_task(void * arg,int pending)2581 reset_adapter_task(void *arg, int pending)
2582 {
2583 	struct adapter *sc = arg;
2584 	const int flags = sc->flags;
2585 	const int eflags = sc->error_flags;
2586 	int rc;
2587 
2588 	if (pending > 1)
2589 		CH_ALERT(sc, "%s: pending %d\n", __func__, pending);
2590 	rc = reset_adapter(sc);
2591 	if (rc != 0) {
2592 		CH_ERR(sc, "adapter did not reset properly, rc = %d, "
2593 		       "flags 0x%08x -> 0x%08x, err_flags 0x%08x -> 0x%08x.\n",
2594 		       rc, flags, sc->flags, eflags, sc->error_flags);
2595 	}
2596 }
2597 
2598 static int
cxgbe_probe(device_t dev)2599 cxgbe_probe(device_t dev)
2600 {
2601 	struct port_info *pi = device_get_softc(dev);
2602 
2603 	device_set_descf(dev, "port %d", pi->port_id);
2604 
2605 	return (BUS_PROBE_DEFAULT);
2606 }
2607 
2608 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2609     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2610     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2611     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2612 #define T4_CAP_ENABLE (T4_CAP)
2613 
2614 static void
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2615 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2616 {
2617 	if_t ifp;
2618 	struct sbuf *sb;
2619 	struct sysctl_ctx_list *ctx = &vi->ctx;
2620 	struct sysctl_oid_list *children;
2621 	struct pfil_head_args pa;
2622 	struct adapter *sc = vi->adapter;
2623 
2624 	sysctl_ctx_init(ctx);
2625 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2626 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2627 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2628 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2629 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2630 #ifdef DEV_NETMAP
2631 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2632 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2633 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2634 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2635 #endif
2636 #ifdef TCP_OFFLOAD
2637 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2638 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2639 #endif
2640 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2641 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2642 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2643 #endif
2644 
2645 	vi->xact_addr_filt = -1;
2646 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2647 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2648 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2649 		vi->flags |= TX_USES_VM_WR;
2650 
2651 	/* Allocate an ifnet and set it up */
2652 	ifp = if_alloc_dev(IFT_ETHER, dev);
2653 	vi->ifp = ifp;
2654 	if_setsoftc(ifp, vi);
2655 
2656 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2657 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2658 
2659 	if_setinitfn(ifp, cxgbe_init);
2660 	if_setioctlfn(ifp, cxgbe_ioctl);
2661 	if_settransmitfn(ifp, cxgbe_transmit);
2662 	if_setqflushfn(ifp, cxgbe_qflush);
2663 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2664 		if_setgetcounterfn(ifp, vi_get_counter);
2665 	else
2666 		if_setgetcounterfn(ifp, cxgbe_get_counter);
2667 #if defined(KERN_TLS) || defined(RATELIMIT)
2668 	if_setsndtagallocfn(ifp, cxgbe_snd_tag_alloc);
2669 #endif
2670 #ifdef RATELIMIT
2671 	if_setratelimitqueryfn(ifp, cxgbe_ratelimit_query);
2672 #endif
2673 
2674 	if_setcapabilities(ifp, T4_CAP);
2675 	if_setcapenable(ifp, T4_CAP_ENABLE);
2676 	if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2677 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2678 	if (chip_id(sc) >= CHELSIO_T6) {
2679 		if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2680 		if_setcapenablebit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2681 		if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2682 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2683 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN, 0);
2684 	}
2685 
2686 #ifdef TCP_OFFLOAD
2687 	if (vi->nofldrxq != 0)
2688 		if_setcapabilitiesbit(ifp, IFCAP_TOE, 0);
2689 #endif
2690 #ifdef RATELIMIT
2691 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2692 		if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT, 0);
2693 		if_setcapenablebit(ifp, IFCAP_TXRTLMT, 0);
2694 	}
2695 #endif
2696 
2697 	if_sethwtsomax(ifp, IP_MAXPACKET);
2698 	if (vi->flags & TX_USES_VM_WR)
2699 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_VM_TSO);
2700 	else
2701 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_TSO);
2702 #ifdef RATELIMIT
2703 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2704 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_EO_TSO);
2705 #endif
2706 	if_sethwtsomaxsegsize(ifp, 65536);
2707 #ifdef KERN_TLS
2708 	if (is_ktls(sc)) {
2709 		if_setcapabilitiesbit(ifp, IFCAP_TXTLS, 0);
2710 		if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2711 			if_setcapenablebit(ifp, IFCAP_TXTLS, 0);
2712 	}
2713 #endif
2714 
2715 	ether_ifattach(ifp, vi->hw_addr);
2716 #ifdef DEV_NETMAP
2717 	if (vi->nnmrxq != 0)
2718 		cxgbe_nm_attach(vi);
2719 #endif
2720 	sb = sbuf_new_auto();
2721 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2722 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2723 	switch (if_getcapabilities(ifp) & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2724 	case IFCAP_TOE:
2725 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2726 		break;
2727 	case IFCAP_TOE | IFCAP_TXRTLMT:
2728 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2729 		break;
2730 	case IFCAP_TXRTLMT:
2731 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2732 		break;
2733 	}
2734 #endif
2735 #ifdef TCP_OFFLOAD
2736 	if (if_getcapabilities(ifp) & IFCAP_TOE)
2737 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2738 #endif
2739 #ifdef DEV_NETMAP
2740 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2741 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2742 		    vi->nnmtxq, vi->nnmrxq);
2743 #endif
2744 	sbuf_finish(sb);
2745 	device_printf(dev, "%s\n", sbuf_data(sb));
2746 	sbuf_delete(sb);
2747 
2748 	vi_sysctls(vi);
2749 
2750 	pa.pa_version = PFIL_VERSION;
2751 	pa.pa_flags = PFIL_IN;
2752 	pa.pa_type = PFIL_TYPE_ETHERNET;
2753 	pa.pa_headname = if_name(ifp);
2754 	vi->pfil = pfil_head_register(&pa);
2755 }
2756 
2757 static int
cxgbe_attach(device_t dev)2758 cxgbe_attach(device_t dev)
2759 {
2760 	struct port_info *pi = device_get_softc(dev);
2761 	struct adapter *sc = pi->adapter;
2762 	struct vi_info *vi;
2763 	int i;
2764 
2765 	sysctl_ctx_init(&pi->ctx);
2766 
2767 	cxgbe_vi_attach(dev, &pi->vi[0]);
2768 
2769 	for_each_vi(pi, i, vi) {
2770 		if (i == 0)
2771 			continue;
2772 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, -1);
2773 		if (vi->dev == NULL) {
2774 			device_printf(dev, "failed to add VI %d\n", i);
2775 			continue;
2776 		}
2777 		device_set_softc(vi->dev, vi);
2778 	}
2779 
2780 	cxgbe_sysctls(pi);
2781 
2782 	bus_generic_attach(dev);
2783 
2784 	return (0);
2785 }
2786 
2787 static void
cxgbe_vi_detach(struct vi_info * vi)2788 cxgbe_vi_detach(struct vi_info *vi)
2789 {
2790 	if_t ifp = vi->ifp;
2791 
2792 	if (vi->pfil != NULL) {
2793 		pfil_head_unregister(vi->pfil);
2794 		vi->pfil = NULL;
2795 	}
2796 
2797 	ether_ifdetach(ifp);
2798 
2799 	/* Let detach proceed even if these fail. */
2800 #ifdef DEV_NETMAP
2801 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2802 		cxgbe_nm_detach(vi);
2803 #endif
2804 	cxgbe_uninit_synchronized(vi);
2805 	callout_drain(&vi->tick);
2806 	mtx_destroy(&vi->tick_mtx);
2807 	sysctl_ctx_free(&vi->ctx);
2808 	vi_full_uninit(vi);
2809 
2810 	if_free(vi->ifp);
2811 	vi->ifp = NULL;
2812 }
2813 
2814 static int
cxgbe_detach(device_t dev)2815 cxgbe_detach(device_t dev)
2816 {
2817 	struct port_info *pi = device_get_softc(dev);
2818 	struct adapter *sc = pi->adapter;
2819 	int rc;
2820 
2821 	/* Detach the extra VIs first. */
2822 	rc = bus_generic_detach(dev);
2823 	if (rc)
2824 		return (rc);
2825 	device_delete_children(dev);
2826 
2827 	sysctl_ctx_free(&pi->ctx);
2828 	begin_vi_detach(sc, &pi->vi[0]);
2829 	if (pi->flags & HAS_TRACEQ) {
2830 		sc->traceq = -1;	/* cloner should not create ifnet */
2831 		t4_tracer_port_detach(sc);
2832 	}
2833 	cxgbe_vi_detach(&pi->vi[0]);
2834 	ifmedia_removeall(&pi->media);
2835 	end_vi_detach(sc, &pi->vi[0]);
2836 
2837 	return (0);
2838 }
2839 
2840 static void
cxgbe_init(void * arg)2841 cxgbe_init(void *arg)
2842 {
2843 	struct vi_info *vi = arg;
2844 	struct adapter *sc = vi->adapter;
2845 
2846 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
2847 		return;
2848 	cxgbe_init_synchronized(vi);
2849 	end_synchronized_op(sc, 0);
2850 }
2851 
2852 static int
cxgbe_ioctl(if_t ifp,unsigned long cmd,caddr_t data)2853 cxgbe_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
2854 {
2855 	int rc = 0, mtu, flags;
2856 	struct vi_info *vi = if_getsoftc(ifp);
2857 	struct port_info *pi = vi->pi;
2858 	struct adapter *sc = pi->adapter;
2859 	struct ifreq *ifr = (struct ifreq *)data;
2860 	uint32_t mask;
2861 
2862 	switch (cmd) {
2863 	case SIOCSIFMTU:
2864 		mtu = ifr->ifr_mtu;
2865 		if (mtu < ETHERMIN || mtu > MAX_MTU)
2866 			return (EINVAL);
2867 
2868 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
2869 		if (rc)
2870 			return (rc);
2871 		if_setmtu(ifp, mtu);
2872 		if (vi->flags & VI_INIT_DONE) {
2873 			t4_update_fl_bufsize(ifp);
2874 			if (!hw_off_limits(sc) &&
2875 			    if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2876 				rc = update_mac_settings(ifp, XGMAC_MTU);
2877 		}
2878 		end_synchronized_op(sc, 0);
2879 		break;
2880 
2881 	case SIOCSIFFLAGS:
2882 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
2883 		if (rc)
2884 			return (rc);
2885 
2886 		if (hw_off_limits(sc)) {
2887 			rc = ENXIO;
2888 			goto fail;
2889 		}
2890 
2891 		if (if_getflags(ifp) & IFF_UP) {
2892 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2893 				flags = vi->if_flags;
2894 				if ((if_getflags(ifp) ^ flags) &
2895 				    (IFF_PROMISC | IFF_ALLMULTI)) {
2896 					rc = update_mac_settings(ifp,
2897 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
2898 				}
2899 			} else {
2900 				rc = cxgbe_init_synchronized(vi);
2901 			}
2902 			vi->if_flags = if_getflags(ifp);
2903 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2904 			rc = cxgbe_uninit_synchronized(vi);
2905 		}
2906 		end_synchronized_op(sc, 0);
2907 		break;
2908 
2909 	case SIOCADDMULTI:
2910 	case SIOCDELMULTI:
2911 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
2912 		if (rc)
2913 			return (rc);
2914 		if (!hw_off_limits(sc) && if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2915 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2916 		end_synchronized_op(sc, 0);
2917 		break;
2918 
2919 	case SIOCSIFCAP:
2920 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
2921 		if (rc)
2922 			return (rc);
2923 
2924 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
2925 		if (mask & IFCAP_TXCSUM) {
2926 			if_togglecapenable(ifp, IFCAP_TXCSUM);
2927 			if_togglehwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP);
2928 
2929 			if (IFCAP_TSO4 & if_getcapenable(ifp) &&
2930 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
2931 				mask &= ~IFCAP_TSO4;
2932 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
2933 				if_printf(ifp,
2934 				    "tso4 disabled due to -txcsum.\n");
2935 			}
2936 		}
2937 		if (mask & IFCAP_TXCSUM_IPV6) {
2938 			if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
2939 			if_togglehwassist(ifp, CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2940 
2941 			if (IFCAP_TSO6 & if_getcapenable(ifp) &&
2942 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
2943 				mask &= ~IFCAP_TSO6;
2944 				if_setcapenablebit(ifp, 0, IFCAP_TSO6);
2945 				if_printf(ifp,
2946 				    "tso6 disabled due to -txcsum6.\n");
2947 			}
2948 		}
2949 		if (mask & IFCAP_RXCSUM)
2950 			if_togglecapenable(ifp, IFCAP_RXCSUM);
2951 		if (mask & IFCAP_RXCSUM_IPV6)
2952 			if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
2953 
2954 		/*
2955 		 * Note that we leave CSUM_TSO alone (it is always set).  The
2956 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
2957 		 * sending a TSO request our way, so it's sufficient to toggle
2958 		 * IFCAP_TSOx only.
2959 		 */
2960 		if (mask & IFCAP_TSO4) {
2961 			if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
2962 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
2963 				if_printf(ifp, "enable txcsum first.\n");
2964 				rc = EAGAIN;
2965 				goto fail;
2966 			}
2967 			if_togglecapenable(ifp, IFCAP_TSO4);
2968 		}
2969 		if (mask & IFCAP_TSO6) {
2970 			if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
2971 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
2972 				if_printf(ifp, "enable txcsum6 first.\n");
2973 				rc = EAGAIN;
2974 				goto fail;
2975 			}
2976 			if_togglecapenable(ifp, IFCAP_TSO6);
2977 		}
2978 		if (mask & IFCAP_LRO) {
2979 #if defined(INET) || defined(INET6)
2980 			int i;
2981 			struct sge_rxq *rxq;
2982 
2983 			if_togglecapenable(ifp, IFCAP_LRO);
2984 			for_each_rxq(vi, i, rxq) {
2985 				if (if_getcapenable(ifp) & IFCAP_LRO)
2986 					rxq->iq.flags |= IQ_LRO_ENABLED;
2987 				else
2988 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
2989 			}
2990 #endif
2991 		}
2992 #ifdef TCP_OFFLOAD
2993 		if (mask & IFCAP_TOE) {
2994 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TOE;
2995 
2996 			rc = toe_capability(vi, enable);
2997 			if (rc != 0)
2998 				goto fail;
2999 
3000 			if_togglecapenable(ifp, mask);
3001 		}
3002 #endif
3003 		if (mask & IFCAP_VLAN_HWTAGGING) {
3004 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3005 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3006 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
3007 		}
3008 		if (mask & IFCAP_VLAN_MTU) {
3009 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
3010 
3011 			/* Need to find out how to disable auto-mtu-inflation */
3012 		}
3013 		if (mask & IFCAP_VLAN_HWTSO)
3014 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3015 		if (mask & IFCAP_VLAN_HWCSUM)
3016 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
3017 #ifdef RATELIMIT
3018 		if (mask & IFCAP_TXRTLMT)
3019 			if_togglecapenable(ifp, IFCAP_TXRTLMT);
3020 #endif
3021 		if (mask & IFCAP_HWRXTSTMP) {
3022 			int i;
3023 			struct sge_rxq *rxq;
3024 
3025 			if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3026 			for_each_rxq(vi, i, rxq) {
3027 				if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3028 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
3029 				else
3030 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
3031 			}
3032 		}
3033 		if (mask & IFCAP_MEXTPG)
3034 			if_togglecapenable(ifp, IFCAP_MEXTPG);
3035 
3036 #ifdef KERN_TLS
3037 		if (mask & IFCAP_TXTLS) {
3038 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TXTLS;
3039 
3040 			rc = ktls_capability(sc, enable);
3041 			if (rc != 0)
3042 				goto fail;
3043 
3044 			if_togglecapenable(ifp, mask & IFCAP_TXTLS);
3045 		}
3046 #endif
3047 		if (mask & IFCAP_VXLAN_HWCSUM) {
3048 			if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3049 			if_togglehwassist(ifp, CSUM_INNER_IP6_UDP |
3050 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
3051 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP);
3052 		}
3053 		if (mask & IFCAP_VXLAN_HWTSO) {
3054 			if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3055 			if_togglehwassist(ifp, CSUM_INNER_IP6_TSO |
3056 			    CSUM_INNER_IP_TSO);
3057 		}
3058 
3059 #ifdef VLAN_CAPABILITIES
3060 		VLAN_CAPABILITIES(ifp);
3061 #endif
3062 fail:
3063 		end_synchronized_op(sc, 0);
3064 		break;
3065 
3066 	case SIOCSIFMEDIA:
3067 	case SIOCGIFMEDIA:
3068 	case SIOCGIFXMEDIA:
3069 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3070 		break;
3071 
3072 	case SIOCGI2C: {
3073 		struct ifi2creq i2c;
3074 
3075 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3076 		if (rc != 0)
3077 			break;
3078 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3079 			rc = EPERM;
3080 			break;
3081 		}
3082 		if (i2c.len > sizeof(i2c.data)) {
3083 			rc = EINVAL;
3084 			break;
3085 		}
3086 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3087 		if (rc)
3088 			return (rc);
3089 		if (hw_off_limits(sc))
3090 			rc = ENXIO;
3091 		else
3092 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3093 			    i2c.offset, i2c.len, &i2c.data[0]);
3094 		end_synchronized_op(sc, 0);
3095 		if (rc == 0)
3096 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3097 		break;
3098 	}
3099 
3100 	default:
3101 		rc = ether_ioctl(ifp, cmd, data);
3102 	}
3103 
3104 	return (rc);
3105 }
3106 
3107 static int
cxgbe_transmit(if_t ifp,struct mbuf * m)3108 cxgbe_transmit(if_t ifp, struct mbuf *m)
3109 {
3110 	struct vi_info *vi = if_getsoftc(ifp);
3111 	struct port_info *pi = vi->pi;
3112 	struct adapter *sc;
3113 	struct sge_txq *txq;
3114 	void *items[1];
3115 	int rc;
3116 
3117 	M_ASSERTPKTHDR(m);
3118 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
3119 #if defined(KERN_TLS) || defined(RATELIMIT)
3120 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3121 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3122 #endif
3123 
3124 	if (__predict_false(pi->link_cfg.link_ok == false)) {
3125 		m_freem(m);
3126 		return (ENETDOWN);
3127 	}
3128 
3129 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3130 	if (__predict_false(rc != 0)) {
3131 		if (__predict_true(rc == EINPROGRESS)) {
3132 			/* queued by parse_pkt */
3133 			MPASS(m != NULL);
3134 			return (0);
3135 		}
3136 
3137 		MPASS(m == NULL);			/* was freed already */
3138 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
3139 		return (rc);
3140 	}
3141 
3142 	/* Select a txq. */
3143 	sc = vi->adapter;
3144 	txq = &sc->sge.txq[vi->first_txq];
3145 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3146 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3147 		    vi->rsrv_noflowq);
3148 
3149 	items[0] = m;
3150 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3151 	if (__predict_false(rc != 0))
3152 		m_freem(m);
3153 
3154 	return (rc);
3155 }
3156 
3157 static void
cxgbe_qflush(if_t ifp)3158 cxgbe_qflush(if_t ifp)
3159 {
3160 	struct vi_info *vi = if_getsoftc(ifp);
3161 	struct sge_txq *txq;
3162 	int i;
3163 
3164 	/* queues do not exist if !VI_INIT_DONE. */
3165 	if (vi->flags & VI_INIT_DONE) {
3166 		for_each_txq(vi, i, txq) {
3167 			TXQ_LOCK(txq);
3168 			txq->eq.flags |= EQ_QFLUSH;
3169 			TXQ_UNLOCK(txq);
3170 			while (!mp_ring_is_idle(txq->r)) {
3171 				mp_ring_check_drainage(txq->r, 4096);
3172 				pause("qflush", 1);
3173 			}
3174 			TXQ_LOCK(txq);
3175 			txq->eq.flags &= ~EQ_QFLUSH;
3176 			TXQ_UNLOCK(txq);
3177 		}
3178 	}
3179 	if_qflush(ifp);
3180 }
3181 
3182 static uint64_t
vi_get_counter(if_t ifp,ift_counter c)3183 vi_get_counter(if_t ifp, ift_counter c)
3184 {
3185 	struct vi_info *vi = if_getsoftc(ifp);
3186 	struct fw_vi_stats_vf *s = &vi->stats;
3187 
3188 	mtx_lock(&vi->tick_mtx);
3189 	vi_refresh_stats(vi);
3190 	mtx_unlock(&vi->tick_mtx);
3191 
3192 	switch (c) {
3193 	case IFCOUNTER_IPACKETS:
3194 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3195 		    s->rx_ucast_frames);
3196 	case IFCOUNTER_IERRORS:
3197 		return (s->rx_err_frames);
3198 	case IFCOUNTER_OPACKETS:
3199 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3200 		    s->tx_ucast_frames + s->tx_offload_frames);
3201 	case IFCOUNTER_OERRORS:
3202 		return (s->tx_drop_frames);
3203 	case IFCOUNTER_IBYTES:
3204 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3205 		    s->rx_ucast_bytes);
3206 	case IFCOUNTER_OBYTES:
3207 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3208 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3209 	case IFCOUNTER_IMCASTS:
3210 		return (s->rx_mcast_frames);
3211 	case IFCOUNTER_OMCASTS:
3212 		return (s->tx_mcast_frames);
3213 	case IFCOUNTER_OQDROPS: {
3214 		uint64_t drops;
3215 
3216 		drops = 0;
3217 		if (vi->flags & VI_INIT_DONE) {
3218 			int i;
3219 			struct sge_txq *txq;
3220 
3221 			for_each_txq(vi, i, txq)
3222 				drops += counter_u64_fetch(txq->r->dropped);
3223 		}
3224 
3225 		return (drops);
3226 
3227 	}
3228 
3229 	default:
3230 		return (if_get_counter_default(ifp, c));
3231 	}
3232 }
3233 
3234 static uint64_t
cxgbe_get_counter(if_t ifp,ift_counter c)3235 cxgbe_get_counter(if_t ifp, ift_counter c)
3236 {
3237 	struct vi_info *vi = if_getsoftc(ifp);
3238 	struct port_info *pi = vi->pi;
3239 	struct port_stats *s = &pi->stats;
3240 
3241 	mtx_lock(&vi->tick_mtx);
3242 	cxgbe_refresh_stats(vi);
3243 	mtx_unlock(&vi->tick_mtx);
3244 
3245 	switch (c) {
3246 	case IFCOUNTER_IPACKETS:
3247 		return (s->rx_frames);
3248 
3249 	case IFCOUNTER_IERRORS:
3250 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3251 		    s->rx_fcs_err + s->rx_len_err);
3252 
3253 	case IFCOUNTER_OPACKETS:
3254 		return (s->tx_frames);
3255 
3256 	case IFCOUNTER_OERRORS:
3257 		return (s->tx_error_frames);
3258 
3259 	case IFCOUNTER_IBYTES:
3260 		return (s->rx_octets);
3261 
3262 	case IFCOUNTER_OBYTES:
3263 		return (s->tx_octets);
3264 
3265 	case IFCOUNTER_IMCASTS:
3266 		return (s->rx_mcast_frames);
3267 
3268 	case IFCOUNTER_OMCASTS:
3269 		return (s->tx_mcast_frames);
3270 
3271 	case IFCOUNTER_IQDROPS:
3272 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3273 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3274 		    s->rx_trunc3 + pi->tnl_cong_drops);
3275 
3276 	case IFCOUNTER_OQDROPS: {
3277 		uint64_t drops;
3278 
3279 		drops = s->tx_drop;
3280 		if (vi->flags & VI_INIT_DONE) {
3281 			int i;
3282 			struct sge_txq *txq;
3283 
3284 			for_each_txq(vi, i, txq)
3285 				drops += counter_u64_fetch(txq->r->dropped);
3286 		}
3287 
3288 		return (drops);
3289 
3290 	}
3291 
3292 	default:
3293 		return (if_get_counter_default(ifp, c));
3294 	}
3295 }
3296 
3297 #if defined(KERN_TLS) || defined(RATELIMIT)
3298 static int
cxgbe_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3299 cxgbe_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3300     struct m_snd_tag **pt)
3301 {
3302 	int error;
3303 
3304 	switch (params->hdr.type) {
3305 #ifdef RATELIMIT
3306 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3307 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3308 		break;
3309 #endif
3310 #ifdef KERN_TLS
3311 	case IF_SND_TAG_TYPE_TLS:
3312 	{
3313 		struct vi_info *vi = if_getsoftc(ifp);
3314 
3315 		if (is_t6(vi->pi->adapter))
3316 			error = t6_tls_tag_alloc(ifp, params, pt);
3317 		else
3318 			error = EOPNOTSUPP;
3319 		break;
3320 	}
3321 #endif
3322 	default:
3323 		error = EOPNOTSUPP;
3324 	}
3325 	return (error);
3326 }
3327 #endif
3328 
3329 /*
3330  * The kernel picks a media from the list we had provided but we still validate
3331  * the requeste.
3332  */
3333 int
cxgbe_media_change(if_t ifp)3334 cxgbe_media_change(if_t ifp)
3335 {
3336 	struct vi_info *vi = if_getsoftc(ifp);
3337 	struct port_info *pi = vi->pi;
3338 	struct ifmedia *ifm = &pi->media;
3339 	struct link_config *lc = &pi->link_cfg;
3340 	struct adapter *sc = pi->adapter;
3341 	int rc;
3342 
3343 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3344 	if (rc != 0)
3345 		return (rc);
3346 	PORT_LOCK(pi);
3347 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3348 		/* ifconfig .. media autoselect */
3349 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3350 			rc = ENOTSUP; /* AN not supported by transceiver */
3351 			goto done;
3352 		}
3353 		lc->requested_aneg = AUTONEG_ENABLE;
3354 		lc->requested_speed = 0;
3355 		lc->requested_fc |= PAUSE_AUTONEG;
3356 	} else {
3357 		lc->requested_aneg = AUTONEG_DISABLE;
3358 		lc->requested_speed =
3359 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3360 		lc->requested_fc = 0;
3361 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3362 			lc->requested_fc |= PAUSE_RX;
3363 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3364 			lc->requested_fc |= PAUSE_TX;
3365 	}
3366 	if (pi->up_vis > 0 && !hw_off_limits(sc)) {
3367 		fixup_link_config(pi);
3368 		rc = apply_link_config(pi);
3369 	}
3370 done:
3371 	PORT_UNLOCK(pi);
3372 	end_synchronized_op(sc, 0);
3373 	return (rc);
3374 }
3375 
3376 /*
3377  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3378  * given speed.
3379  */
3380 static int
port_mword(struct port_info * pi,uint32_t speed)3381 port_mword(struct port_info *pi, uint32_t speed)
3382 {
3383 
3384 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3385 	MPASS(powerof2(speed));
3386 
3387 	switch(pi->port_type) {
3388 	case FW_PORT_TYPE_BT_SGMII:
3389 	case FW_PORT_TYPE_BT_XFI:
3390 	case FW_PORT_TYPE_BT_XAUI:
3391 		/* BaseT */
3392 		switch (speed) {
3393 		case FW_PORT_CAP32_SPEED_100M:
3394 			return (IFM_100_T);
3395 		case FW_PORT_CAP32_SPEED_1G:
3396 			return (IFM_1000_T);
3397 		case FW_PORT_CAP32_SPEED_10G:
3398 			return (IFM_10G_T);
3399 		}
3400 		break;
3401 	case FW_PORT_TYPE_KX4:
3402 		if (speed == FW_PORT_CAP32_SPEED_10G)
3403 			return (IFM_10G_KX4);
3404 		break;
3405 	case FW_PORT_TYPE_CX4:
3406 		if (speed == FW_PORT_CAP32_SPEED_10G)
3407 			return (IFM_10G_CX4);
3408 		break;
3409 	case FW_PORT_TYPE_KX:
3410 		if (speed == FW_PORT_CAP32_SPEED_1G)
3411 			return (IFM_1000_KX);
3412 		break;
3413 	case FW_PORT_TYPE_KR:
3414 	case FW_PORT_TYPE_BP_AP:
3415 	case FW_PORT_TYPE_BP4_AP:
3416 	case FW_PORT_TYPE_BP40_BA:
3417 	case FW_PORT_TYPE_KR4_100G:
3418 	case FW_PORT_TYPE_KR_SFP28:
3419 	case FW_PORT_TYPE_KR_XLAUI:
3420 		switch (speed) {
3421 		case FW_PORT_CAP32_SPEED_1G:
3422 			return (IFM_1000_KX);
3423 		case FW_PORT_CAP32_SPEED_10G:
3424 			return (IFM_10G_KR);
3425 		case FW_PORT_CAP32_SPEED_25G:
3426 			return (IFM_25G_KR);
3427 		case FW_PORT_CAP32_SPEED_40G:
3428 			return (IFM_40G_KR4);
3429 		case FW_PORT_CAP32_SPEED_50G:
3430 			return (IFM_50G_KR2);
3431 		case FW_PORT_CAP32_SPEED_100G:
3432 			return (IFM_100G_KR4);
3433 		}
3434 		break;
3435 	case FW_PORT_TYPE_FIBER_XFI:
3436 	case FW_PORT_TYPE_FIBER_XAUI:
3437 	case FW_PORT_TYPE_SFP:
3438 	case FW_PORT_TYPE_QSFP_10G:
3439 	case FW_PORT_TYPE_QSA:
3440 	case FW_PORT_TYPE_QSFP:
3441 	case FW_PORT_TYPE_CR4_QSFP:
3442 	case FW_PORT_TYPE_CR_QSFP:
3443 	case FW_PORT_TYPE_CR2_QSFP:
3444 	case FW_PORT_TYPE_SFP28:
3445 		/* Pluggable transceiver */
3446 		switch (pi->mod_type) {
3447 		case FW_PORT_MOD_TYPE_LR:
3448 			switch (speed) {
3449 			case FW_PORT_CAP32_SPEED_1G:
3450 				return (IFM_1000_LX);
3451 			case FW_PORT_CAP32_SPEED_10G:
3452 				return (IFM_10G_LR);
3453 			case FW_PORT_CAP32_SPEED_25G:
3454 				return (IFM_25G_LR);
3455 			case FW_PORT_CAP32_SPEED_40G:
3456 				return (IFM_40G_LR4);
3457 			case FW_PORT_CAP32_SPEED_50G:
3458 				return (IFM_50G_LR2);
3459 			case FW_PORT_CAP32_SPEED_100G:
3460 				return (IFM_100G_LR4);
3461 			}
3462 			break;
3463 		case FW_PORT_MOD_TYPE_SR:
3464 			switch (speed) {
3465 			case FW_PORT_CAP32_SPEED_1G:
3466 				return (IFM_1000_SX);
3467 			case FW_PORT_CAP32_SPEED_10G:
3468 				return (IFM_10G_SR);
3469 			case FW_PORT_CAP32_SPEED_25G:
3470 				return (IFM_25G_SR);
3471 			case FW_PORT_CAP32_SPEED_40G:
3472 				return (IFM_40G_SR4);
3473 			case FW_PORT_CAP32_SPEED_50G:
3474 				return (IFM_50G_SR2);
3475 			case FW_PORT_CAP32_SPEED_100G:
3476 				return (IFM_100G_SR4);
3477 			}
3478 			break;
3479 		case FW_PORT_MOD_TYPE_ER:
3480 			if (speed == FW_PORT_CAP32_SPEED_10G)
3481 				return (IFM_10G_ER);
3482 			break;
3483 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3484 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3485 			switch (speed) {
3486 			case FW_PORT_CAP32_SPEED_1G:
3487 				return (IFM_1000_CX);
3488 			case FW_PORT_CAP32_SPEED_10G:
3489 				return (IFM_10G_TWINAX);
3490 			case FW_PORT_CAP32_SPEED_25G:
3491 				return (IFM_25G_CR);
3492 			case FW_PORT_CAP32_SPEED_40G:
3493 				return (IFM_40G_CR4);
3494 			case FW_PORT_CAP32_SPEED_50G:
3495 				return (IFM_50G_CR2);
3496 			case FW_PORT_CAP32_SPEED_100G:
3497 				return (IFM_100G_CR4);
3498 			}
3499 			break;
3500 		case FW_PORT_MOD_TYPE_LRM:
3501 			if (speed == FW_PORT_CAP32_SPEED_10G)
3502 				return (IFM_10G_LRM);
3503 			break;
3504 		case FW_PORT_MOD_TYPE_NA:
3505 			MPASS(0);	/* Not pluggable? */
3506 			/* fall throough */
3507 		case FW_PORT_MOD_TYPE_ERROR:
3508 		case FW_PORT_MOD_TYPE_UNKNOWN:
3509 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3510 			break;
3511 		case FW_PORT_MOD_TYPE_NONE:
3512 			return (IFM_NONE);
3513 		}
3514 		break;
3515 	case FW_PORT_TYPE_NONE:
3516 		return (IFM_NONE);
3517 	}
3518 
3519 	return (IFM_UNKNOWN);
3520 }
3521 
3522 void
cxgbe_media_status(if_t ifp,struct ifmediareq * ifmr)3523 cxgbe_media_status(if_t ifp, struct ifmediareq *ifmr)
3524 {
3525 	struct vi_info *vi = if_getsoftc(ifp);
3526 	struct port_info *pi = vi->pi;
3527 	struct adapter *sc = pi->adapter;
3528 	struct link_config *lc = &pi->link_cfg;
3529 
3530 	if (begin_synchronized_op(sc, vi , SLEEP_OK | INTR_OK, "t4med") != 0)
3531 		return;
3532 	PORT_LOCK(pi);
3533 
3534 	if (pi->up_vis == 0 && !hw_off_limits(sc)) {
3535 		/*
3536 		 * If all the interfaces are administratively down the firmware
3537 		 * does not report transceiver changes.  Refresh port info here
3538 		 * so that ifconfig displays accurate ifmedia at all times.
3539 		 * This is the only reason we have a synchronized op in this
3540 		 * function.  Just PORT_LOCK would have been enough otherwise.
3541 		 */
3542 		t4_update_port_info(pi);
3543 		build_medialist(pi);
3544 	}
3545 
3546 	/* ifm_status */
3547 	ifmr->ifm_status = IFM_AVALID;
3548 	if (lc->link_ok == false)
3549 		goto done;
3550 	ifmr->ifm_status |= IFM_ACTIVE;
3551 
3552 	/* ifm_active */
3553 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3554 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3555 	if (lc->fc & PAUSE_RX)
3556 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3557 	if (lc->fc & PAUSE_TX)
3558 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3559 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3560 done:
3561 	PORT_UNLOCK(pi);
3562 	end_synchronized_op(sc, 0);
3563 }
3564 
3565 static int
vcxgbe_probe(device_t dev)3566 vcxgbe_probe(device_t dev)
3567 {
3568 	struct vi_info *vi = device_get_softc(dev);
3569 
3570 	device_set_descf(dev, "port %d vi %td", vi->pi->port_id,
3571 	    vi - vi->pi->vi);
3572 
3573 	return (BUS_PROBE_DEFAULT);
3574 }
3575 
3576 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3577 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3578 {
3579 	int func, index, rc;
3580 	uint32_t param, val;
3581 
3582 	ASSERT_SYNCHRONIZED_OP(sc);
3583 
3584 	index = vi - pi->vi;
3585 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3586 	KASSERT(index < nitems(vi_mac_funcs),
3587 	    ("%s: VI %s doesn't have a MAC func", __func__,
3588 	    device_get_nameunit(vi->dev)));
3589 	func = vi_mac_funcs[index];
3590 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
3591 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3592 	if (rc < 0) {
3593 		CH_ERR(vi, "failed to allocate virtual interface %d"
3594 		    "for port %d: %d\n", index, pi->port_id, -rc);
3595 		return (-rc);
3596 	}
3597 	vi->viid = rc;
3598 
3599 	if (vi->rss_size == 1) {
3600 		/*
3601 		 * This VI didn't get a slice of the RSS table.  Reduce the
3602 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3603 		 * configuration file (nvi, rssnvi for this PF) if this is a
3604 		 * problem.
3605 		 */
3606 		device_printf(vi->dev, "RSS table not available.\n");
3607 		vi->rss_base = 0xffff;
3608 
3609 		return (0);
3610 	}
3611 
3612 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3613 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3614 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3615 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3616 	if (rc)
3617 		vi->rss_base = 0xffff;
3618 	else {
3619 		MPASS((val >> 16) == vi->rss_size);
3620 		vi->rss_base = val & 0xffff;
3621 	}
3622 
3623 	return (0);
3624 }
3625 
3626 static int
vcxgbe_attach(device_t dev)3627 vcxgbe_attach(device_t dev)
3628 {
3629 	struct vi_info *vi;
3630 	struct port_info *pi;
3631 	struct adapter *sc;
3632 	int rc;
3633 
3634 	vi = device_get_softc(dev);
3635 	pi = vi->pi;
3636 	sc = pi->adapter;
3637 
3638 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3639 	if (rc)
3640 		return (rc);
3641 	rc = alloc_extra_vi(sc, pi, vi);
3642 	end_synchronized_op(sc, 0);
3643 	if (rc)
3644 		return (rc);
3645 
3646 	cxgbe_vi_attach(dev, vi);
3647 
3648 	return (0);
3649 }
3650 
3651 static int
vcxgbe_detach(device_t dev)3652 vcxgbe_detach(device_t dev)
3653 {
3654 	struct vi_info *vi;
3655 	struct adapter *sc;
3656 
3657 	vi = device_get_softc(dev);
3658 	sc = vi->adapter;
3659 
3660 	begin_vi_detach(sc, vi);
3661 	cxgbe_vi_detach(vi);
3662 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3663 	end_vi_detach(sc, vi);
3664 
3665 	return (0);
3666 }
3667 
3668 static struct callout fatal_callout;
3669 static struct taskqueue *reset_tq;
3670 
3671 static void
delayed_panic(void * arg)3672 delayed_panic(void *arg)
3673 {
3674 	struct adapter *sc = arg;
3675 
3676 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3677 }
3678 
3679 static void
fatal_error_task(void * arg,int pending)3680 fatal_error_task(void *arg, int pending)
3681 {
3682 	struct adapter *sc = arg;
3683 	int rc;
3684 
3685 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3686 		dump_cim_regs(sc);
3687 		dump_cimla(sc);
3688 		dump_devlog(sc);
3689 	}
3690 
3691 	if (t4_reset_on_fatal_err) {
3692 		CH_ALERT(sc, "resetting adapter after fatal error.\n");
3693 		rc = reset_adapter(sc);
3694 		if (rc == 0 && t4_panic_on_fatal_err) {
3695 			CH_ALERT(sc, "reset was successful, "
3696 			    "system will NOT panic.\n");
3697 			return;
3698 		}
3699 	}
3700 
3701 	if (t4_panic_on_fatal_err) {
3702 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3703 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3704 	}
3705 }
3706 
3707 void
t4_fatal_err(struct adapter * sc,bool fw_error)3708 t4_fatal_err(struct adapter *sc, bool fw_error)
3709 {
3710 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3711 
3712 	stop_adapter(sc);
3713 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3714 		return;
3715 	if (fw_error) {
3716 		/*
3717 		 * We are here because of a firmware error/timeout and not
3718 		 * because of a hardware interrupt.  It is possible (although
3719 		 * not very likely) that an error interrupt was also raised but
3720 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3721 		 * main INT_CAUSE registers here to make sure we haven't missed
3722 		 * anything interesting.
3723 		 */
3724 		t4_slow_intr_handler(sc, verbose);
3725 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3726 	}
3727 	t4_report_fw_error(sc);
3728 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3729 	    device_get_nameunit(sc->dev), fw_error);
3730 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3731 }
3732 
3733 void
t4_add_adapter(struct adapter * sc)3734 t4_add_adapter(struct adapter *sc)
3735 {
3736 	sx_xlock(&t4_list_lock);
3737 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3738 	sx_xunlock(&t4_list_lock);
3739 }
3740 
3741 int
t4_map_bars_0_and_4(struct adapter * sc)3742 t4_map_bars_0_and_4(struct adapter *sc)
3743 {
3744 	sc->regs_rid = PCIR_BAR(0);
3745 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3746 	    &sc->regs_rid, RF_ACTIVE);
3747 	if (sc->regs_res == NULL) {
3748 		device_printf(sc->dev, "cannot map registers.\n");
3749 		return (ENXIO);
3750 	}
3751 	sc->bt = rman_get_bustag(sc->regs_res);
3752 	sc->bh = rman_get_bushandle(sc->regs_res);
3753 	sc->mmio_len = rman_get_size(sc->regs_res);
3754 	setbit(&sc->doorbells, DOORBELL_KDB);
3755 
3756 	sc->msix_rid = PCIR_BAR(4);
3757 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3758 	    &sc->msix_rid, RF_ACTIVE);
3759 	if (sc->msix_res == NULL) {
3760 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3761 		return (ENXIO);
3762 	}
3763 
3764 	return (0);
3765 }
3766 
3767 int
t4_map_bar_2(struct adapter * sc)3768 t4_map_bar_2(struct adapter *sc)
3769 {
3770 
3771 	/*
3772 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
3773 	 * to map it if RDMA is disabled.
3774 	 */
3775 	if (is_t4(sc) && sc->rdmacaps == 0)
3776 		return (0);
3777 
3778 	sc->udbs_rid = PCIR_BAR(2);
3779 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3780 	    &sc->udbs_rid, RF_ACTIVE);
3781 	if (sc->udbs_res == NULL) {
3782 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
3783 		return (ENXIO);
3784 	}
3785 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
3786 
3787 	if (chip_id(sc) >= CHELSIO_T5) {
3788 		setbit(&sc->doorbells, DOORBELL_UDB);
3789 #if defined(__i386__) || defined(__amd64__)
3790 		if (t5_write_combine) {
3791 			int rc, mode;
3792 
3793 			/*
3794 			 * Enable write combining on BAR2.  This is the
3795 			 * userspace doorbell BAR and is split into 128B
3796 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
3797 			 * with an egress queue.  The first 64B has the doorbell
3798 			 * and the second 64B can be used to submit a tx work
3799 			 * request with an implicit doorbell.
3800 			 */
3801 
3802 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
3803 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
3804 			if (rc == 0) {
3805 				clrbit(&sc->doorbells, DOORBELL_UDB);
3806 				setbit(&sc->doorbells, DOORBELL_WCWR);
3807 				setbit(&sc->doorbells, DOORBELL_UDBWC);
3808 			} else {
3809 				device_printf(sc->dev,
3810 				    "couldn't enable write combining: %d\n",
3811 				    rc);
3812 			}
3813 
3814 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
3815 			t4_write_reg(sc, A_SGE_STAT_CFG,
3816 			    V_STATSOURCE_T5(7) | mode);
3817 		}
3818 #endif
3819 	}
3820 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
3821 
3822 	return (0);
3823 }
3824 
3825 int
t4_adj_doorbells(struct adapter * sc)3826 t4_adj_doorbells(struct adapter *sc)
3827 {
3828 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
3829 		sc->doorbells &= t4_doorbells_allowed;
3830 		return (0);
3831 	}
3832 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
3833 	       sc->doorbells, t4_doorbells_allowed);
3834 	return (EINVAL);
3835 }
3836 
3837 struct memwin_init {
3838 	uint32_t base;
3839 	uint32_t aperture;
3840 };
3841 
3842 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
3843 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3844 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3845 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
3846 };
3847 
3848 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
3849 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3850 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3851 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
3852 };
3853 
3854 static void
setup_memwin(struct adapter * sc)3855 setup_memwin(struct adapter *sc)
3856 {
3857 	const struct memwin_init *mw_init;
3858 	struct memwin *mw;
3859 	int i;
3860 	uint32_t bar0;
3861 
3862 	if (is_t4(sc)) {
3863 		/*
3864 		 * Read low 32b of bar0 indirectly via the hardware backdoor
3865 		 * mechanism.  Works from within PCI passthrough environments
3866 		 * too, where rman_get_start() can return a different value.  We
3867 		 * need to program the T4 memory window decoders with the actual
3868 		 * addresses that will be coming across the PCIe link.
3869 		 */
3870 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
3871 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
3872 
3873 		mw_init = &t4_memwin[0];
3874 	} else {
3875 		/* T5+ use the relative offset inside the PCIe BAR */
3876 		bar0 = 0;
3877 
3878 		mw_init = &t5_memwin[0];
3879 	}
3880 
3881 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
3882 		if (!rw_initialized(&mw->mw_lock)) {
3883 			rw_init(&mw->mw_lock, "memory window access");
3884 			mw->mw_base = mw_init->base;
3885 			mw->mw_aperture = mw_init->aperture;
3886 			mw->mw_curpos = 0;
3887 		}
3888 		t4_write_reg(sc,
3889 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
3890 		    (mw->mw_base + bar0) | V_BIR(0) |
3891 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
3892 		rw_wlock(&mw->mw_lock);
3893 		position_memwin(sc, i, mw->mw_curpos);
3894 		rw_wunlock(&mw->mw_lock);
3895 	}
3896 
3897 	/* flush */
3898 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
3899 }
3900 
3901 /*
3902  * Positions the memory window at the given address in the card's address space.
3903  * There are some alignment requirements and the actual position may be at an
3904  * address prior to the requested address.  mw->mw_curpos always has the actual
3905  * position of the window.
3906  */
3907 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)3908 position_memwin(struct adapter *sc, int idx, uint32_t addr)
3909 {
3910 	struct memwin *mw;
3911 	uint32_t pf;
3912 	uint32_t reg;
3913 
3914 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3915 	mw = &sc->memwin[idx];
3916 	rw_assert(&mw->mw_lock, RA_WLOCKED);
3917 
3918 	if (is_t4(sc)) {
3919 		pf = 0;
3920 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
3921 	} else {
3922 		pf = V_PFNUM(sc->pf);
3923 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
3924 	}
3925 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
3926 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
3927 	t4_read_reg(sc, reg);	/* flush */
3928 }
3929 
3930 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)3931 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
3932     int len, int rw)
3933 {
3934 	struct memwin *mw;
3935 	uint32_t mw_end, v;
3936 
3937 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3938 
3939 	/* Memory can only be accessed in naturally aligned 4 byte units */
3940 	if (addr & 3 || len & 3 || len <= 0)
3941 		return (EINVAL);
3942 
3943 	mw = &sc->memwin[idx];
3944 	while (len > 0) {
3945 		rw_rlock(&mw->mw_lock);
3946 		mw_end = mw->mw_curpos + mw->mw_aperture;
3947 		if (addr >= mw_end || addr < mw->mw_curpos) {
3948 			/* Will need to reposition the window */
3949 			if (!rw_try_upgrade(&mw->mw_lock)) {
3950 				rw_runlock(&mw->mw_lock);
3951 				rw_wlock(&mw->mw_lock);
3952 			}
3953 			rw_assert(&mw->mw_lock, RA_WLOCKED);
3954 			position_memwin(sc, idx, addr);
3955 			rw_downgrade(&mw->mw_lock);
3956 			mw_end = mw->mw_curpos + mw->mw_aperture;
3957 		}
3958 		rw_assert(&mw->mw_lock, RA_RLOCKED);
3959 		while (addr < mw_end && len > 0) {
3960 			if (rw == 0) {
3961 				v = t4_read_reg(sc, mw->mw_base + addr -
3962 				    mw->mw_curpos);
3963 				*val++ = le32toh(v);
3964 			} else {
3965 				v = *val++;
3966 				t4_write_reg(sc, mw->mw_base + addr -
3967 				    mw->mw_curpos, htole32(v));
3968 			}
3969 			addr += 4;
3970 			len -= 4;
3971 		}
3972 		rw_runlock(&mw->mw_lock);
3973 	}
3974 
3975 	return (0);
3976 }
3977 
3978 CTASSERT(M_TID_COOKIE == M_COOKIE);
3979 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
3980 
3981 static void
t4_init_atid_table(struct adapter * sc)3982 t4_init_atid_table(struct adapter *sc)
3983 {
3984 	struct tid_info *t;
3985 	int i;
3986 
3987 	t = &sc->tids;
3988 	if (t->natids == 0)
3989 		return;
3990 
3991 	MPASS(t->atid_tab == NULL);
3992 
3993 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
3994 	    M_ZERO | M_WAITOK);
3995 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
3996 	t->afree = t->atid_tab;
3997 	t->atids_in_use = 0;
3998 	t->atid_alloc_stopped = false;
3999 	for (i = 1; i < t->natids; i++)
4000 		t->atid_tab[i - 1].next = &t->atid_tab[i];
4001 	t->atid_tab[t->natids - 1].next = NULL;
4002 }
4003 
4004 static void
t4_free_atid_table(struct adapter * sc)4005 t4_free_atid_table(struct adapter *sc)
4006 {
4007 	struct tid_info *t;
4008 
4009 	t = &sc->tids;
4010 
4011 	KASSERT(t->atids_in_use == 0,
4012 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4013 
4014 	if (mtx_initialized(&t->atid_lock))
4015 		mtx_destroy(&t->atid_lock);
4016 	free(t->atid_tab, M_CXGBE);
4017 	t->atid_tab = NULL;
4018 }
4019 
4020 static void
stop_atid_allocator(struct adapter * sc)4021 stop_atid_allocator(struct adapter *sc)
4022 {
4023 	struct tid_info *t = &sc->tids;
4024 
4025 	mtx_lock(&t->atid_lock);
4026 	t->atid_alloc_stopped = true;
4027 	mtx_unlock(&t->atid_lock);
4028 }
4029 
4030 static void
restart_atid_allocator(struct adapter * sc)4031 restart_atid_allocator(struct adapter *sc)
4032 {
4033 	struct tid_info *t = &sc->tids;
4034 
4035 	mtx_lock(&t->atid_lock);
4036 	KASSERT(t->atids_in_use == 0,
4037 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4038 	t->atid_alloc_stopped = false;
4039 	mtx_unlock(&t->atid_lock);
4040 }
4041 
4042 int
alloc_atid(struct adapter * sc,void * ctx)4043 alloc_atid(struct adapter *sc, void *ctx)
4044 {
4045 	struct tid_info *t = &sc->tids;
4046 	int atid = -1;
4047 
4048 	mtx_lock(&t->atid_lock);
4049 	if (t->afree && !t->atid_alloc_stopped) {
4050 		union aopen_entry *p = t->afree;
4051 
4052 		atid = p - t->atid_tab;
4053 		MPASS(atid <= M_TID_TID);
4054 		t->afree = p->next;
4055 		p->data = ctx;
4056 		t->atids_in_use++;
4057 	}
4058 	mtx_unlock(&t->atid_lock);
4059 	return (atid);
4060 }
4061 
4062 void *
lookup_atid(struct adapter * sc,int atid)4063 lookup_atid(struct adapter *sc, int atid)
4064 {
4065 	struct tid_info *t = &sc->tids;
4066 
4067 	return (t->atid_tab[atid].data);
4068 }
4069 
4070 void
free_atid(struct adapter * sc,int atid)4071 free_atid(struct adapter *sc, int atid)
4072 {
4073 	struct tid_info *t = &sc->tids;
4074 	union aopen_entry *p = &t->atid_tab[atid];
4075 
4076 	mtx_lock(&t->atid_lock);
4077 	p->next = t->afree;
4078 	t->afree = p;
4079 	t->atids_in_use--;
4080 	mtx_unlock(&t->atid_lock);
4081 }
4082 
4083 static void
queue_tid_release(struct adapter * sc,int tid)4084 queue_tid_release(struct adapter *sc, int tid)
4085 {
4086 
4087 	CXGBE_UNIMPLEMENTED("deferred tid release");
4088 }
4089 
4090 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4091 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4092 {
4093 	struct wrqe *wr;
4094 	struct cpl_tid_release *req;
4095 
4096 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4097 	if (wr == NULL) {
4098 		queue_tid_release(sc, tid);	/* defer */
4099 		return;
4100 	}
4101 	req = wrtod(wr);
4102 
4103 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4104 
4105 	t4_wrq_tx(sc, wr);
4106 }
4107 
4108 static int
t4_range_cmp(const void * a,const void * b)4109 t4_range_cmp(const void *a, const void *b)
4110 {
4111 	return ((const struct t4_range *)a)->start -
4112 	       ((const struct t4_range *)b)->start;
4113 }
4114 
4115 /*
4116  * Verify that the memory range specified by the addr/len pair is valid within
4117  * the card's address space.
4118  */
4119 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4120 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4121 {
4122 	struct t4_range mem_ranges[4], *r, *next;
4123 	uint32_t em, addr_len;
4124 	int i, n, remaining;
4125 
4126 	/* Memory can only be accessed in naturally aligned 4 byte units */
4127 	if (addr & 3 || len & 3 || len == 0)
4128 		return (EINVAL);
4129 
4130 	/* Enabled memories */
4131 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4132 
4133 	r = &mem_ranges[0];
4134 	n = 0;
4135 	bzero(r, sizeof(mem_ranges));
4136 	if (em & F_EDRAM0_ENABLE) {
4137 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4138 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4139 		if (r->size > 0) {
4140 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4141 			if (addr >= r->start &&
4142 			    addr + len <= r->start + r->size)
4143 				return (0);
4144 			r++;
4145 			n++;
4146 		}
4147 	}
4148 	if (em & F_EDRAM1_ENABLE) {
4149 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4150 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4151 		if (r->size > 0) {
4152 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4153 			if (addr >= r->start &&
4154 			    addr + len <= r->start + r->size)
4155 				return (0);
4156 			r++;
4157 			n++;
4158 		}
4159 	}
4160 	if (em & F_EXT_MEM_ENABLE) {
4161 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4162 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4163 		if (r->size > 0) {
4164 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4165 			if (addr >= r->start &&
4166 			    addr + len <= r->start + r->size)
4167 				return (0);
4168 			r++;
4169 			n++;
4170 		}
4171 	}
4172 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4173 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4174 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4175 		if (r->size > 0) {
4176 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4177 			if (addr >= r->start &&
4178 			    addr + len <= r->start + r->size)
4179 				return (0);
4180 			r++;
4181 			n++;
4182 		}
4183 	}
4184 	MPASS(n <= nitems(mem_ranges));
4185 
4186 	if (n > 1) {
4187 		/* Sort and merge the ranges. */
4188 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4189 
4190 		/* Start from index 0 and examine the next n - 1 entries. */
4191 		r = &mem_ranges[0];
4192 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4193 
4194 			MPASS(r->size > 0);	/* r is a valid entry. */
4195 			next = r + 1;
4196 			MPASS(next->size > 0);	/* and so is the next one. */
4197 
4198 			while (r->start + r->size >= next->start) {
4199 				/* Merge the next one into the current entry. */
4200 				r->size = max(r->start + r->size,
4201 				    next->start + next->size) - r->start;
4202 				n--;	/* One fewer entry in total. */
4203 				if (--remaining == 0)
4204 					goto done;	/* short circuit */
4205 				next++;
4206 			}
4207 			if (next != r + 1) {
4208 				/*
4209 				 * Some entries were merged into r and next
4210 				 * points to the first valid entry that couldn't
4211 				 * be merged.
4212 				 */
4213 				MPASS(next->size > 0);	/* must be valid */
4214 				memcpy(r + 1, next, remaining * sizeof(*r));
4215 #ifdef INVARIANTS
4216 				/*
4217 				 * This so that the foo->size assertion in the
4218 				 * next iteration of the loop do the right
4219 				 * thing for entries that were pulled up and are
4220 				 * no longer valid.
4221 				 */
4222 				MPASS(n < nitems(mem_ranges));
4223 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4224 				    sizeof(struct t4_range));
4225 #endif
4226 			}
4227 		}
4228 done:
4229 		/* Done merging the ranges. */
4230 		MPASS(n > 0);
4231 		r = &mem_ranges[0];
4232 		for (i = 0; i < n; i++, r++) {
4233 			if (addr >= r->start &&
4234 			    addr + len <= r->start + r->size)
4235 				return (0);
4236 		}
4237 	}
4238 
4239 	return (EFAULT);
4240 }
4241 
4242 static int
fwmtype_to_hwmtype(int mtype)4243 fwmtype_to_hwmtype(int mtype)
4244 {
4245 
4246 	switch (mtype) {
4247 	case FW_MEMTYPE_EDC0:
4248 		return (MEM_EDC0);
4249 	case FW_MEMTYPE_EDC1:
4250 		return (MEM_EDC1);
4251 	case FW_MEMTYPE_EXTMEM:
4252 		return (MEM_MC0);
4253 	case FW_MEMTYPE_EXTMEM1:
4254 		return (MEM_MC1);
4255 	default:
4256 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4257 	}
4258 }
4259 
4260 /*
4261  * Verify that the memory range specified by the memtype/offset/len pair is
4262  * valid and lies entirely within the memtype specified.  The global address of
4263  * the start of the range is returned in addr.
4264  */
4265 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4266 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4267     uint32_t *addr)
4268 {
4269 	uint32_t em, addr_len, maddr;
4270 
4271 	/* Memory can only be accessed in naturally aligned 4 byte units */
4272 	if (off & 3 || len & 3 || len == 0)
4273 		return (EINVAL);
4274 
4275 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4276 	switch (fwmtype_to_hwmtype(mtype)) {
4277 	case MEM_EDC0:
4278 		if (!(em & F_EDRAM0_ENABLE))
4279 			return (EINVAL);
4280 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4281 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4282 		break;
4283 	case MEM_EDC1:
4284 		if (!(em & F_EDRAM1_ENABLE))
4285 			return (EINVAL);
4286 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4287 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4288 		break;
4289 	case MEM_MC:
4290 		if (!(em & F_EXT_MEM_ENABLE))
4291 			return (EINVAL);
4292 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4293 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4294 		break;
4295 	case MEM_MC1:
4296 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4297 			return (EINVAL);
4298 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4299 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4300 		break;
4301 	default:
4302 		return (EINVAL);
4303 	}
4304 
4305 	*addr = maddr + off;	/* global address */
4306 	return (validate_mem_range(sc, *addr, len));
4307 }
4308 
4309 static int
fixup_devlog_params(struct adapter * sc)4310 fixup_devlog_params(struct adapter *sc)
4311 {
4312 	struct devlog_params *dparams = &sc->params.devlog;
4313 	int rc;
4314 
4315 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4316 	    dparams->size, &dparams->addr);
4317 
4318 	return (rc);
4319 }
4320 
4321 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4322 update_nirq(struct intrs_and_queues *iaq, int nports)
4323 {
4324 
4325 	iaq->nirq = T4_EXTRA_INTR;
4326 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4327 	iaq->nirq += nports * iaq->nofldrxq;
4328 	iaq->nirq += nports * (iaq->num_vis - 1) *
4329 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4330 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4331 }
4332 
4333 /*
4334  * Adjust requirements to fit the number of interrupts available.
4335  */
4336 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4337 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4338     int navail)
4339 {
4340 	int old_nirq;
4341 	const int nports = sc->params.nports;
4342 
4343 	MPASS(nports > 0);
4344 	MPASS(navail > 0);
4345 
4346 	bzero(iaq, sizeof(*iaq));
4347 	iaq->intr_type = itype;
4348 	iaq->num_vis = t4_num_vis;
4349 	iaq->ntxq = t4_ntxq;
4350 	iaq->ntxq_vi = t4_ntxq_vi;
4351 	iaq->nrxq = t4_nrxq;
4352 	iaq->nrxq_vi = t4_nrxq_vi;
4353 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4354 	if (is_offload(sc) || is_ethoffload(sc)) {
4355 		iaq->nofldtxq = t4_nofldtxq;
4356 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
4357 	}
4358 #endif
4359 #ifdef TCP_OFFLOAD
4360 	if (is_offload(sc)) {
4361 		iaq->nofldrxq = t4_nofldrxq;
4362 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4363 	}
4364 #endif
4365 #ifdef DEV_NETMAP
4366 	if (t4_native_netmap & NN_MAIN_VI) {
4367 		iaq->nnmtxq = t4_nnmtxq;
4368 		iaq->nnmrxq = t4_nnmrxq;
4369 	}
4370 	if (t4_native_netmap & NN_EXTRA_VI) {
4371 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4372 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4373 	}
4374 #endif
4375 
4376 	update_nirq(iaq, nports);
4377 	if (iaq->nirq <= navail &&
4378 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4379 		/*
4380 		 * This is the normal case -- there are enough interrupts for
4381 		 * everything.
4382 		 */
4383 		goto done;
4384 	}
4385 
4386 	/*
4387 	 * If extra VIs have been configured try reducing their count and see if
4388 	 * that works.
4389 	 */
4390 	while (iaq->num_vis > 1) {
4391 		iaq->num_vis--;
4392 		update_nirq(iaq, nports);
4393 		if (iaq->nirq <= navail &&
4394 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4395 			device_printf(sc->dev, "virtual interfaces per port "
4396 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4397 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4398 			    "itype %d, navail %u, nirq %d.\n",
4399 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4400 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4401 			    itype, navail, iaq->nirq);
4402 			goto done;
4403 		}
4404 	}
4405 
4406 	/*
4407 	 * Extra VIs will not be created.  Log a message if they were requested.
4408 	 */
4409 	MPASS(iaq->num_vis == 1);
4410 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4411 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4412 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4413 	if (iaq->num_vis != t4_num_vis) {
4414 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4415 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4416 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4417 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4418 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4419 	}
4420 
4421 	/*
4422 	 * Keep reducing the number of NIC rx queues to the next lower power of
4423 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4424 	 * if that works.
4425 	 */
4426 	do {
4427 		if (iaq->nrxq > 1) {
4428 			do {
4429 				iaq->nrxq--;
4430 			} while (!powerof2(iaq->nrxq));
4431 			if (iaq->nnmrxq > iaq->nrxq)
4432 				iaq->nnmrxq = iaq->nrxq;
4433 		}
4434 		if (iaq->nofldrxq > 1)
4435 			iaq->nofldrxq >>= 1;
4436 
4437 		old_nirq = iaq->nirq;
4438 		update_nirq(iaq, nports);
4439 		if (iaq->nirq <= navail &&
4440 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4441 			device_printf(sc->dev, "running with reduced number of "
4442 			    "rx queues because of shortage of interrupts.  "
4443 			    "nrxq=%u, nofldrxq=%u.  "
4444 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4445 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4446 			goto done;
4447 		}
4448 	} while (old_nirq != iaq->nirq);
4449 
4450 	/* One interrupt for everything.  Ugh. */
4451 	device_printf(sc->dev, "running with minimal number of queues.  "
4452 	    "itype %d, navail %u.\n", itype, navail);
4453 	iaq->nirq = 1;
4454 	iaq->nrxq = 1;
4455 	iaq->ntxq = 1;
4456 	if (iaq->nofldrxq > 0) {
4457 		iaq->nofldrxq = 1;
4458 		iaq->nofldtxq = 1;
4459 	}
4460 	iaq->nnmtxq = 0;
4461 	iaq->nnmrxq = 0;
4462 done:
4463 	MPASS(iaq->num_vis > 0);
4464 	if (iaq->num_vis > 1) {
4465 		MPASS(iaq->nrxq_vi > 0);
4466 		MPASS(iaq->ntxq_vi > 0);
4467 	}
4468 	MPASS(iaq->nirq > 0);
4469 	MPASS(iaq->nrxq > 0);
4470 	MPASS(iaq->ntxq > 0);
4471 	if (itype == INTR_MSI) {
4472 		MPASS(powerof2(iaq->nirq));
4473 	}
4474 }
4475 
4476 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4477 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4478 {
4479 	int rc, itype, navail, nalloc;
4480 
4481 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4482 
4483 		if ((itype & t4_intr_types) == 0)
4484 			continue;	/* not allowed */
4485 
4486 		if (itype == INTR_MSIX)
4487 			navail = pci_msix_count(sc->dev);
4488 		else if (itype == INTR_MSI)
4489 			navail = pci_msi_count(sc->dev);
4490 		else
4491 			navail = 1;
4492 restart:
4493 		if (navail == 0)
4494 			continue;
4495 
4496 		calculate_iaq(sc, iaq, itype, navail);
4497 		nalloc = iaq->nirq;
4498 		rc = 0;
4499 		if (itype == INTR_MSIX)
4500 			rc = pci_alloc_msix(sc->dev, &nalloc);
4501 		else if (itype == INTR_MSI)
4502 			rc = pci_alloc_msi(sc->dev, &nalloc);
4503 
4504 		if (rc == 0 && nalloc > 0) {
4505 			if (nalloc == iaq->nirq)
4506 				return (0);
4507 
4508 			/*
4509 			 * Didn't get the number requested.  Use whatever number
4510 			 * the kernel is willing to allocate.
4511 			 */
4512 			device_printf(sc->dev, "fewer vectors than requested, "
4513 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4514 			    itype, iaq->nirq, nalloc);
4515 			pci_release_msi(sc->dev);
4516 			navail = nalloc;
4517 			goto restart;
4518 		}
4519 
4520 		device_printf(sc->dev,
4521 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4522 		    itype, rc, iaq->nirq, nalloc);
4523 	}
4524 
4525 	device_printf(sc->dev,
4526 	    "failed to find a usable interrupt type.  "
4527 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4528 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4529 
4530 	return (ENXIO);
4531 }
4532 
4533 #define FW_VERSION(chip) ( \
4534     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4535     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4536     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4537     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4538 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4539 
4540 /* Just enough of fw_hdr to cover all version info. */
4541 struct fw_h {
4542 	__u8	ver;
4543 	__u8	chip;
4544 	__be16	len512;
4545 	__be32	fw_ver;
4546 	__be32	tp_microcode_ver;
4547 	__u8	intfver_nic;
4548 	__u8	intfver_vnic;
4549 	__u8	intfver_ofld;
4550 	__u8	intfver_ri;
4551 	__u8	intfver_iscsipdu;
4552 	__u8	intfver_iscsi;
4553 	__u8	intfver_fcoepdu;
4554 	__u8	intfver_fcoe;
4555 };
4556 /* Spot check a couple of fields. */
4557 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4558 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4559 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4560 
4561 struct fw_info {
4562 	uint8_t chip;
4563 	char *kld_name;
4564 	char *fw_mod_name;
4565 	struct fw_h fw_h;
4566 } fw_info[] = {
4567 	{
4568 		.chip = CHELSIO_T4,
4569 		.kld_name = "t4fw_cfg",
4570 		.fw_mod_name = "t4fw",
4571 		.fw_h = {
4572 			.chip = FW_HDR_CHIP_T4,
4573 			.fw_ver = htobe32(FW_VERSION(T4)),
4574 			.intfver_nic = FW_INTFVER(T4, NIC),
4575 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4576 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4577 			.intfver_ri = FW_INTFVER(T4, RI),
4578 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4579 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4580 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4581 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4582 		},
4583 	}, {
4584 		.chip = CHELSIO_T5,
4585 		.kld_name = "t5fw_cfg",
4586 		.fw_mod_name = "t5fw",
4587 		.fw_h = {
4588 			.chip = FW_HDR_CHIP_T5,
4589 			.fw_ver = htobe32(FW_VERSION(T5)),
4590 			.intfver_nic = FW_INTFVER(T5, NIC),
4591 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4592 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4593 			.intfver_ri = FW_INTFVER(T5, RI),
4594 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4595 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4596 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4597 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4598 		},
4599 	}, {
4600 		.chip = CHELSIO_T6,
4601 		.kld_name = "t6fw_cfg",
4602 		.fw_mod_name = "t6fw",
4603 		.fw_h = {
4604 			.chip = FW_HDR_CHIP_T6,
4605 			.fw_ver = htobe32(FW_VERSION(T6)),
4606 			.intfver_nic = FW_INTFVER(T6, NIC),
4607 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4608 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4609 			.intfver_ri = FW_INTFVER(T6, RI),
4610 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4611 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4612 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4613 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4614 		},
4615 	}
4616 };
4617 
4618 static struct fw_info *
find_fw_info(int chip)4619 find_fw_info(int chip)
4620 {
4621 	int i;
4622 
4623 	for (i = 0; i < nitems(fw_info); i++) {
4624 		if (fw_info[i].chip == chip)
4625 			return (&fw_info[i]);
4626 	}
4627 	return (NULL);
4628 }
4629 
4630 /*
4631  * Is the given firmware API compatible with the one the driver was compiled
4632  * with?
4633  */
4634 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4635 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4636 {
4637 
4638 	/* short circuit if it's the exact same firmware version */
4639 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4640 		return (1);
4641 
4642 	/*
4643 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4644 	 * features that are supported in the driver.
4645 	 */
4646 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4647 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4648 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4649 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4650 		return (1);
4651 #undef SAME_INTF
4652 
4653 	return (0);
4654 }
4655 
4656 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4657 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4658     const struct firmware **fw)
4659 {
4660 	struct fw_info *fw_info;
4661 
4662 	*dcfg = NULL;
4663 	if (fw != NULL)
4664 		*fw = NULL;
4665 
4666 	fw_info = find_fw_info(chip_id(sc));
4667 	if (fw_info == NULL) {
4668 		device_printf(sc->dev,
4669 		    "unable to look up firmware information for chip %d.\n",
4670 		    chip_id(sc));
4671 		return (EINVAL);
4672 	}
4673 
4674 	*dcfg = firmware_get(fw_info->kld_name);
4675 	if (*dcfg != NULL) {
4676 		if (fw != NULL)
4677 			*fw = firmware_get(fw_info->fw_mod_name);
4678 		return (0);
4679 	}
4680 
4681 	return (ENOENT);
4682 }
4683 
4684 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4685 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4686     const struct firmware *fw)
4687 {
4688 
4689 	if (fw != NULL)
4690 		firmware_put(fw, FIRMWARE_UNLOAD);
4691 	if (dcfg != NULL)
4692 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4693 }
4694 
4695 /*
4696  * Return values:
4697  * 0 means no firmware install attempted.
4698  * ERESTART means a firmware install was attempted and was successful.
4699  * +ve errno means a firmware install was attempted but failed.
4700  */
4701 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4702 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4703     const struct fw_h *drv_fw, const char *reason, int *already)
4704 {
4705 	const struct firmware *cfg, *fw;
4706 	const uint32_t c = be32toh(card_fw->fw_ver);
4707 	uint32_t d, k;
4708 	int rc, fw_install;
4709 	struct fw_h bundled_fw;
4710 	bool load_attempted;
4711 
4712 	cfg = fw = NULL;
4713 	load_attempted = false;
4714 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4715 
4716 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4717 	if (t4_fw_install < 0) {
4718 		rc = load_fw_module(sc, &cfg, &fw);
4719 		if (rc != 0 || fw == NULL) {
4720 			device_printf(sc->dev,
4721 			    "failed to load firmware module: %d. cfg %p, fw %p;"
4722 			    " will use compiled-in firmware version for"
4723 			    "hw.cxgbe.fw_install checks.\n",
4724 			    rc, cfg, fw);
4725 		} else {
4726 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
4727 		}
4728 		load_attempted = true;
4729 	}
4730 	d = be32toh(bundled_fw.fw_ver);
4731 
4732 	if (reason != NULL)
4733 		goto install;
4734 
4735 	if ((sc->flags & FW_OK) == 0) {
4736 
4737 		if (c == 0xffffffff) {
4738 			reason = "missing";
4739 			goto install;
4740 		}
4741 
4742 		rc = 0;
4743 		goto done;
4744 	}
4745 
4746 	if (!fw_compatible(card_fw, &bundled_fw)) {
4747 		reason = "incompatible or unusable";
4748 		goto install;
4749 	}
4750 
4751 	if (d > c) {
4752 		reason = "older than the version bundled with this driver";
4753 		goto install;
4754 	}
4755 
4756 	if (fw_install == 2 && d != c) {
4757 		reason = "different than the version bundled with this driver";
4758 		goto install;
4759 	}
4760 
4761 	/* No reason to do anything to the firmware already on the card. */
4762 	rc = 0;
4763 	goto done;
4764 
4765 install:
4766 	rc = 0;
4767 	if ((*already)++)
4768 		goto done;
4769 
4770 	if (fw_install == 0) {
4771 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4772 		    "but the driver is prohibited from installing a firmware "
4773 		    "on the card.\n",
4774 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4775 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4776 
4777 		goto done;
4778 	}
4779 
4780 	/*
4781 	 * We'll attempt to install a firmware.  Load the module first (if it
4782 	 * hasn't been loaded already).
4783 	 */
4784 	if (!load_attempted) {
4785 		rc = load_fw_module(sc, &cfg, &fw);
4786 		if (rc != 0 || fw == NULL) {
4787 			device_printf(sc->dev,
4788 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
4789 			    rc, cfg, fw);
4790 			/* carry on */
4791 		}
4792 	}
4793 	if (fw == NULL) {
4794 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4795 		    "but the driver cannot take corrective action because it "
4796 		    "is unable to load the firmware module.\n",
4797 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4798 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4799 		rc = sc->flags & FW_OK ? 0 : ENOENT;
4800 		goto done;
4801 	}
4802 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
4803 	if (k != d) {
4804 		MPASS(t4_fw_install > 0);
4805 		device_printf(sc->dev,
4806 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
4807 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
4808 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
4809 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
4810 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4811 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4812 		rc = sc->flags & FW_OK ? 0 : EINVAL;
4813 		goto done;
4814 	}
4815 
4816 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4817 	    "installing firmware %u.%u.%u.%u on card.\n",
4818 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4819 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
4820 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4821 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4822 
4823 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
4824 	if (rc != 0) {
4825 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
4826 	} else {
4827 		/* Installed successfully, update the cached header too. */
4828 		rc = ERESTART;
4829 		memcpy(card_fw, fw->data, sizeof(*card_fw));
4830 	}
4831 done:
4832 	unload_fw_module(sc, cfg, fw);
4833 
4834 	return (rc);
4835 }
4836 
4837 /*
4838  * Establish contact with the firmware and attempt to become the master driver.
4839  *
4840  * A firmware will be installed to the card if needed (if the driver is allowed
4841  * to do so).
4842  */
4843 static int
contact_firmware(struct adapter * sc)4844 contact_firmware(struct adapter *sc)
4845 {
4846 	int rc, already = 0;
4847 	enum dev_state state;
4848 	struct fw_info *fw_info;
4849 	struct fw_hdr *card_fw;		/* fw on the card */
4850 	const struct fw_h *drv_fw;
4851 
4852 	fw_info = find_fw_info(chip_id(sc));
4853 	if (fw_info == NULL) {
4854 		device_printf(sc->dev,
4855 		    "unable to look up firmware information for chip %d.\n",
4856 		    chip_id(sc));
4857 		return (EINVAL);
4858 	}
4859 	drv_fw = &fw_info->fw_h;
4860 
4861 	/* Read the header of the firmware on the card */
4862 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
4863 restart:
4864 	rc = -t4_get_fw_hdr(sc, card_fw);
4865 	if (rc != 0) {
4866 		device_printf(sc->dev,
4867 		    "unable to read firmware header from card's flash: %d\n",
4868 		    rc);
4869 		goto done;
4870 	}
4871 
4872 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
4873 	    &already);
4874 	if (rc == ERESTART)
4875 		goto restart;
4876 	if (rc != 0)
4877 		goto done;
4878 
4879 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
4880 	if (rc < 0 || state == DEV_STATE_ERR) {
4881 		rc = -rc;
4882 		device_printf(sc->dev,
4883 		    "failed to connect to the firmware: %d, %d.  "
4884 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4885 #if 0
4886 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4887 		    "not responding properly to HELLO", &already) == ERESTART)
4888 			goto restart;
4889 #endif
4890 		goto done;
4891 	}
4892 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
4893 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
4894 
4895 	if (rc == sc->pf) {
4896 		sc->flags |= MASTER_PF;
4897 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4898 		    NULL, &already);
4899 		if (rc == ERESTART)
4900 			rc = 0;
4901 		else if (rc != 0)
4902 			goto done;
4903 	} else if (state == DEV_STATE_UNINIT) {
4904 		/*
4905 		 * We didn't get to be the master so we definitely won't be
4906 		 * configuring the chip.  It's a bug if someone else hasn't
4907 		 * configured it already.
4908 		 */
4909 		device_printf(sc->dev, "couldn't be master(%d), "
4910 		    "device not already initialized either(%d).  "
4911 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4912 		rc = EPROTO;
4913 		goto done;
4914 	} else {
4915 		/*
4916 		 * Some other PF is the master and has configured the chip.
4917 		 * This is allowed but untested.
4918 		 */
4919 		device_printf(sc->dev, "PF%d is master, device state %d.  "
4920 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4921 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
4922 		sc->cfcsum = 0;
4923 		rc = 0;
4924 	}
4925 done:
4926 	if (rc != 0 && sc->flags & FW_OK) {
4927 		t4_fw_bye(sc, sc->mbox);
4928 		sc->flags &= ~FW_OK;
4929 	}
4930 	free(card_fw, M_CXGBE);
4931 	return (rc);
4932 }
4933 
4934 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff)4935 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
4936     uint32_t mtype, uint32_t moff)
4937 {
4938 	struct fw_info *fw_info;
4939 	const struct firmware *dcfg, *rcfg = NULL;
4940 	const uint32_t *cfdata;
4941 	uint32_t cflen, addr;
4942 	int rc;
4943 
4944 	load_fw_module(sc, &dcfg, NULL);
4945 
4946 	/* Card specific interpretation of "default". */
4947 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4948 		if (pci_get_device(sc->dev) == 0x440a)
4949 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
4950 		if (is_fpga(sc))
4951 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
4952 	}
4953 
4954 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4955 		if (dcfg == NULL) {
4956 			device_printf(sc->dev,
4957 			    "KLD with default config is not available.\n");
4958 			rc = ENOENT;
4959 			goto done;
4960 		}
4961 		cfdata = dcfg->data;
4962 		cflen = dcfg->datasize & ~3;
4963 	} else {
4964 		char s[32];
4965 
4966 		fw_info = find_fw_info(chip_id(sc));
4967 		if (fw_info == NULL) {
4968 			device_printf(sc->dev,
4969 			    "unable to look up firmware information for chip %d.\n",
4970 			    chip_id(sc));
4971 			rc = EINVAL;
4972 			goto done;
4973 		}
4974 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
4975 
4976 		rcfg = firmware_get(s);
4977 		if (rcfg == NULL) {
4978 			device_printf(sc->dev,
4979 			    "unable to load module \"%s\" for configuration "
4980 			    "profile \"%s\".\n", s, cfg_file);
4981 			rc = ENOENT;
4982 			goto done;
4983 		}
4984 		cfdata = rcfg->data;
4985 		cflen = rcfg->datasize & ~3;
4986 	}
4987 
4988 	if (cflen > FLASH_CFG_MAX_SIZE) {
4989 		device_printf(sc->dev,
4990 		    "config file too long (%d, max allowed is %d).\n",
4991 		    cflen, FLASH_CFG_MAX_SIZE);
4992 		rc = EINVAL;
4993 		goto done;
4994 	}
4995 
4996 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
4997 	if (rc != 0) {
4998 		device_printf(sc->dev,
4999 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
5000 		    __func__, mtype, moff, cflen, rc);
5001 		rc = EINVAL;
5002 		goto done;
5003 	}
5004 	write_via_memwin(sc, 2, addr, cfdata, cflen);
5005 done:
5006 	if (rcfg != NULL)
5007 		firmware_put(rcfg, FIRMWARE_UNLOAD);
5008 	unload_fw_module(sc, dcfg, NULL);
5009 	return (rc);
5010 }
5011 
5012 struct caps_allowed {
5013 	uint16_t nbmcaps;
5014 	uint16_t linkcaps;
5015 	uint16_t switchcaps;
5016 	uint16_t niccaps;
5017 	uint16_t toecaps;
5018 	uint16_t rdmacaps;
5019 	uint16_t cryptocaps;
5020 	uint16_t iscsicaps;
5021 	uint16_t fcoecaps;
5022 };
5023 
5024 #define FW_PARAM_DEV(param) \
5025 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5026 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5027 #define FW_PARAM_PFVF(param) \
5028 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5029 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5030 
5031 /*
5032  * Provide a configuration profile to the firmware and have it initialize the
5033  * chip accordingly.  This may involve uploading a configuration file to the
5034  * card.
5035  */
5036 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5037 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5038     const struct caps_allowed *caps_allowed)
5039 {
5040 	int rc;
5041 	struct fw_caps_config_cmd caps;
5042 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5043 
5044 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5045 	if (rc != 0) {
5046 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5047 		return (rc);
5048 	}
5049 
5050 	bzero(&caps, sizeof(caps));
5051 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5052 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5053 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5054 		mtype = 0;
5055 		moff = 0;
5056 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5057 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5058 		mtype = FW_MEMTYPE_FLASH;
5059 		moff = t4_flash_cfg_addr(sc);
5060 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5061 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5062 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5063 		    FW_LEN16(caps));
5064 	} else {
5065 		/*
5066 		 * Ask the firmware where it wants us to upload the config file.
5067 		 */
5068 		param = FW_PARAM_DEV(CF);
5069 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5070 		if (rc != 0) {
5071 			/* No support for config file?  Shouldn't happen. */
5072 			device_printf(sc->dev,
5073 			    "failed to query config file location: %d.\n", rc);
5074 			goto done;
5075 		}
5076 		mtype = G_FW_PARAMS_PARAM_Y(val);
5077 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5078 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5079 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5080 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5081 		    FW_LEN16(caps));
5082 
5083 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
5084 		if (rc != 0) {
5085 			device_printf(sc->dev,
5086 			    "failed to upload config file to card: %d.\n", rc);
5087 			goto done;
5088 		}
5089 	}
5090 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5091 	if (rc != 0) {
5092 		device_printf(sc->dev, "failed to pre-process config file: %d "
5093 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5094 		goto done;
5095 	}
5096 
5097 	finicsum = be32toh(caps.finicsum);
5098 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5099 	if (finicsum != cfcsum) {
5100 		device_printf(sc->dev,
5101 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5102 		    finicsum, cfcsum);
5103 	}
5104 	sc->cfcsum = cfcsum;
5105 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5106 
5107 	/*
5108 	 * Let the firmware know what features will (not) be used so it can tune
5109 	 * things accordingly.
5110 	 */
5111 #define LIMIT_CAPS(x) do { \
5112 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5113 } while (0)
5114 	LIMIT_CAPS(nbm);
5115 	LIMIT_CAPS(link);
5116 	LIMIT_CAPS(switch);
5117 	LIMIT_CAPS(nic);
5118 	LIMIT_CAPS(toe);
5119 	LIMIT_CAPS(rdma);
5120 	LIMIT_CAPS(crypto);
5121 	LIMIT_CAPS(iscsi);
5122 	LIMIT_CAPS(fcoe);
5123 #undef LIMIT_CAPS
5124 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5125 		/*
5126 		 * TOE and hashfilters are mutually exclusive.  It is a config
5127 		 * file or firmware bug if both are reported as available.  Try
5128 		 * to cope with the situation in non-debug builds by disabling
5129 		 * TOE.
5130 		 */
5131 		MPASS(caps.toecaps == 0);
5132 
5133 		caps.toecaps = 0;
5134 		caps.rdmacaps = 0;
5135 		caps.iscsicaps = 0;
5136 	}
5137 
5138 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5139 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5140 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5141 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5142 	if (rc != 0) {
5143 		device_printf(sc->dev,
5144 		    "failed to process config file: %d.\n", rc);
5145 		goto done;
5146 	}
5147 
5148 	t4_tweak_chip_settings(sc);
5149 	set_params__pre_init(sc);
5150 
5151 	/* get basic stuff going */
5152 	rc = -t4_fw_initialize(sc, sc->mbox);
5153 	if (rc != 0) {
5154 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5155 		goto done;
5156 	}
5157 done:
5158 	return (rc);
5159 }
5160 
5161 /*
5162  * Partition chip resources for use between various PFs, VFs, etc.
5163  */
5164 static int
partition_resources(struct adapter * sc)5165 partition_resources(struct adapter *sc)
5166 {
5167 	char cfg_file[sizeof(t4_cfg_file)];
5168 	struct caps_allowed caps_allowed;
5169 	int rc;
5170 	bool fallback;
5171 
5172 	/* Only the master driver gets to configure the chip resources. */
5173 	MPASS(sc->flags & MASTER_PF);
5174 
5175 #define COPY_CAPS(x) do { \
5176 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5177 } while (0)
5178 	bzero(&caps_allowed, sizeof(caps_allowed));
5179 	COPY_CAPS(nbm);
5180 	COPY_CAPS(link);
5181 	COPY_CAPS(switch);
5182 	COPY_CAPS(nic);
5183 	COPY_CAPS(toe);
5184 	COPY_CAPS(rdma);
5185 	COPY_CAPS(crypto);
5186 	COPY_CAPS(iscsi);
5187 	COPY_CAPS(fcoe);
5188 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5189 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5190 retry:
5191 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5192 	if (rc != 0 && fallback) {
5193 		dump_devlog(sc);
5194 		device_printf(sc->dev,
5195 		    "failed (%d) to configure card with \"%s\" profile, "
5196 		    "will fall back to a basic configuration and retry.\n",
5197 		    rc, cfg_file);
5198 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5199 		bzero(&caps_allowed, sizeof(caps_allowed));
5200 		COPY_CAPS(switch);
5201 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5202 		fallback = false;
5203 		goto retry;
5204 	}
5205 #undef COPY_CAPS
5206 	return (rc);
5207 }
5208 
5209 /*
5210  * Retrieve parameters that are needed (or nice to have) very early.
5211  */
5212 static int
get_params__pre_init(struct adapter * sc)5213 get_params__pre_init(struct adapter *sc)
5214 {
5215 	int rc;
5216 	uint32_t param[2], val[2];
5217 
5218 	t4_get_version_info(sc);
5219 
5220 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5221 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5222 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5223 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5224 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5225 
5226 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5227 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5228 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5229 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5230 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5231 
5232 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5233 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5234 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5235 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5236 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5237 
5238 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5239 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5240 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5241 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5242 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5243 
5244 	param[0] = FW_PARAM_DEV(PORTVEC);
5245 	param[1] = FW_PARAM_DEV(CCLK);
5246 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5247 	if (rc != 0) {
5248 		device_printf(sc->dev,
5249 		    "failed to query parameters (pre_init): %d.\n", rc);
5250 		return (rc);
5251 	}
5252 
5253 	sc->params.portvec = val[0];
5254 	sc->params.nports = bitcount32(val[0]);
5255 	sc->params.vpd.cclk = val[1];
5256 
5257 	/* Read device log parameters. */
5258 	rc = -t4_init_devlog_params(sc, 1);
5259 	if (rc == 0)
5260 		fixup_devlog_params(sc);
5261 	else {
5262 		device_printf(sc->dev,
5263 		    "failed to get devlog parameters: %d.\n", rc);
5264 		rc = 0;	/* devlog isn't critical for device operation */
5265 	}
5266 
5267 	return (rc);
5268 }
5269 
5270 /*
5271  * Any params that need to be set before FW_INITIALIZE.
5272  */
5273 static int
set_params__pre_init(struct adapter * sc)5274 set_params__pre_init(struct adapter *sc)
5275 {
5276 	int rc = 0;
5277 	uint32_t param, val;
5278 
5279 	if (chip_id(sc) >= CHELSIO_T6) {
5280 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5281 		val = 1;
5282 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5283 		/* firmwares < 1.20.1.0 do not have this param. */
5284 		if (rc == FW_EINVAL &&
5285 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5286 			rc = 0;
5287 		}
5288 		if (rc != 0) {
5289 			device_printf(sc->dev,
5290 			    "failed to enable high priority filters :%d.\n",
5291 			    rc);
5292 		}
5293 
5294 		param = FW_PARAM_DEV(PPOD_EDRAM);
5295 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5296 		if (rc == 0 && val == 1) {
5297 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5298 			    &val);
5299 			if (rc != 0) {
5300 				device_printf(sc->dev,
5301 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5302 			}
5303 		}
5304 	}
5305 
5306 	/* Enable opaque VIIDs with firmwares that support it. */
5307 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5308 	val = 1;
5309 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5310 	if (rc == 0 && val == 1)
5311 		sc->params.viid_smt_extn_support = true;
5312 	else
5313 		sc->params.viid_smt_extn_support = false;
5314 
5315 	return (rc);
5316 }
5317 
5318 /*
5319  * Retrieve various parameters that are of interest to the driver.  The device
5320  * has been initialized by the firmware at this point.
5321  */
5322 static int
get_params__post_init(struct adapter * sc)5323 get_params__post_init(struct adapter *sc)
5324 {
5325 	int rc;
5326 	uint32_t param[7], val[7];
5327 	struct fw_caps_config_cmd caps;
5328 
5329 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5330 	param[1] = FW_PARAM_PFVF(EQ_START);
5331 	param[2] = FW_PARAM_PFVF(FILTER_START);
5332 	param[3] = FW_PARAM_PFVF(FILTER_END);
5333 	param[4] = FW_PARAM_PFVF(L2T_START);
5334 	param[5] = FW_PARAM_PFVF(L2T_END);
5335 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5336 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5337 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5338 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5339 	if (rc != 0) {
5340 		device_printf(sc->dev,
5341 		    "failed to query parameters (post_init): %d.\n", rc);
5342 		return (rc);
5343 	}
5344 
5345 	sc->sge.iq_start = val[0];
5346 	sc->sge.eq_start = val[1];
5347 	if ((int)val[3] > (int)val[2]) {
5348 		sc->tids.ftid_base = val[2];
5349 		sc->tids.ftid_end = val[3];
5350 		sc->tids.nftids = val[3] - val[2] + 1;
5351 	}
5352 	sc->vres.l2t.start = val[4];
5353 	sc->vres.l2t.size = val[5] - val[4] + 1;
5354 	/* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5355 	if (sc->vres.l2t.size > 0)
5356 		MPASS(fls(val[5]) <= S_SYNC_WR);
5357 	sc->params.core_vdd = val[6];
5358 
5359 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5360 	param[1] = FW_PARAM_PFVF(EQ_END);
5361 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5362 	if (rc != 0) {
5363 		device_printf(sc->dev,
5364 		    "failed to query parameters (post_init2): %d.\n", rc);
5365 		return (rc);
5366 	}
5367 	MPASS((int)val[0] >= sc->sge.iq_start);
5368 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5369 	MPASS((int)val[1] >= sc->sge.eq_start);
5370 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5371 
5372 	if (chip_id(sc) >= CHELSIO_T6) {
5373 
5374 		sc->tids.tid_base = t4_read_reg(sc,
5375 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5376 
5377 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5378 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5379 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5380 		if (rc != 0) {
5381 			device_printf(sc->dev,
5382 			   "failed to query hpfilter parameters: %d.\n", rc);
5383 			return (rc);
5384 		}
5385 		if ((int)val[1] > (int)val[0]) {
5386 			sc->tids.hpftid_base = val[0];
5387 			sc->tids.hpftid_end = val[1];
5388 			sc->tids.nhpftids = val[1] - val[0] + 1;
5389 
5390 			/*
5391 			 * These should go off if the layout changes and the
5392 			 * driver needs to catch up.
5393 			 */
5394 			MPASS(sc->tids.hpftid_base == 0);
5395 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5396 		}
5397 
5398 		param[0] = FW_PARAM_PFVF(RAWF_START);
5399 		param[1] = FW_PARAM_PFVF(RAWF_END);
5400 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5401 		if (rc != 0) {
5402 			device_printf(sc->dev,
5403 			   "failed to query rawf parameters: %d.\n", rc);
5404 			return (rc);
5405 		}
5406 		if ((int)val[1] > (int)val[0]) {
5407 			sc->rawf_base = val[0];
5408 			sc->nrawf = val[1] - val[0] + 1;
5409 		}
5410 	}
5411 
5412 	/*
5413 	 * The parameters that follow may not be available on all firmwares.  We
5414 	 * query them individually rather than in a compound query because old
5415 	 * firmwares fail the entire query if an unknown parameter is queried.
5416 	 */
5417 
5418 	/*
5419 	 * MPS buffer group configuration.
5420 	 */
5421 	param[0] = FW_PARAM_DEV(MPSBGMAP);
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.mps_bg_map = val[0];
5426 	else
5427 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5428 
5429 	param[0] = FW_PARAM_DEV(TPCHMAP);
5430 	val[0] = 0;
5431 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5432 	if (rc == 0)
5433 		sc->params.tp_ch_map = val[0];
5434 	else
5435 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5436 
5437 	/*
5438 	 * Determine whether the firmware supports the filter2 work request.
5439 	 */
5440 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5441 	val[0] = 0;
5442 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5443 	if (rc == 0)
5444 		sc->params.filter2_wr_support = val[0] != 0;
5445 	else
5446 		sc->params.filter2_wr_support = 0;
5447 
5448 	/*
5449 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5450 	 */
5451 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5452 	val[0] = 0;
5453 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5454 	if (rc == 0)
5455 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5456 	else
5457 		sc->params.ulptx_memwrite_dsgl = false;
5458 
5459 	/* FW_RI_FR_NSMR_TPTE_WR support */
5460 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5461 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5462 	if (rc == 0)
5463 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5464 	else
5465 		sc->params.fr_nsmr_tpte_wr_support = false;
5466 
5467 	/* Support for 512 SGL entries per FR MR. */
5468 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5469 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5470 	if (rc == 0)
5471 		sc->params.dev_512sgl_mr = val[0] != 0;
5472 	else
5473 		sc->params.dev_512sgl_mr = false;
5474 
5475 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5476 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5477 	if (rc == 0)
5478 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5479 	else
5480 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5481 
5482 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5483 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5484 	if (rc == 0) {
5485 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5486 		sc->params.nsched_cls = val[0];
5487 	} else
5488 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5489 
5490 	/* get capabilites */
5491 	bzero(&caps, sizeof(caps));
5492 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5493 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5494 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5495 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5496 	if (rc != 0) {
5497 		device_printf(sc->dev,
5498 		    "failed to get card capabilities: %d.\n", rc);
5499 		return (rc);
5500 	}
5501 
5502 #define READ_CAPS(x) do { \
5503 	sc->x = htobe16(caps.x); \
5504 } while (0)
5505 	READ_CAPS(nbmcaps);
5506 	READ_CAPS(linkcaps);
5507 	READ_CAPS(switchcaps);
5508 	READ_CAPS(niccaps);
5509 	READ_CAPS(toecaps);
5510 	READ_CAPS(rdmacaps);
5511 	READ_CAPS(cryptocaps);
5512 	READ_CAPS(iscsicaps);
5513 	READ_CAPS(fcoecaps);
5514 
5515 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5516 		MPASS(chip_id(sc) > CHELSIO_T4);
5517 		MPASS(sc->toecaps == 0);
5518 		sc->toecaps = 0;
5519 
5520 		param[0] = FW_PARAM_DEV(NTID);
5521 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5522 		if (rc != 0) {
5523 			device_printf(sc->dev,
5524 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5525 			return (rc);
5526 		}
5527 		sc->tids.ntids = val[0];
5528 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5529 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5530 			sc->tids.ntids -= sc->tids.nhpftids;
5531 		}
5532 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5533 		sc->params.hash_filter = 1;
5534 	}
5535 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5536 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5537 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5538 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5539 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5540 		if (rc != 0) {
5541 			device_printf(sc->dev,
5542 			    "failed to query NIC parameters: %d.\n", rc);
5543 			return (rc);
5544 		}
5545 		if ((int)val[1] > (int)val[0]) {
5546 			sc->tids.etid_base = val[0];
5547 			sc->tids.etid_end = val[1];
5548 			sc->tids.netids = val[1] - val[0] + 1;
5549 			sc->params.eo_wr_cred = val[2];
5550 			sc->params.ethoffload = 1;
5551 		}
5552 	}
5553 	if (sc->toecaps) {
5554 		/* query offload-related parameters */
5555 		param[0] = FW_PARAM_DEV(NTID);
5556 		param[1] = FW_PARAM_PFVF(SERVER_START);
5557 		param[2] = FW_PARAM_PFVF(SERVER_END);
5558 		param[3] = FW_PARAM_PFVF(TDDP_START);
5559 		param[4] = FW_PARAM_PFVF(TDDP_END);
5560 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5561 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5562 		if (rc != 0) {
5563 			device_printf(sc->dev,
5564 			    "failed to query TOE parameters: %d.\n", rc);
5565 			return (rc);
5566 		}
5567 		sc->tids.ntids = val[0];
5568 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5569 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5570 			sc->tids.ntids -= sc->tids.nhpftids;
5571 		}
5572 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5573 		if ((int)val[2] > (int)val[1]) {
5574 			sc->tids.stid_base = val[1];
5575 			sc->tids.nstids = val[2] - val[1] + 1;
5576 		}
5577 		sc->vres.ddp.start = val[3];
5578 		sc->vres.ddp.size = val[4] - val[3] + 1;
5579 		sc->params.ofldq_wr_cred = val[5];
5580 		sc->params.offload = 1;
5581 	} else {
5582 		/*
5583 		 * The firmware attempts memfree TOE configuration for -SO cards
5584 		 * and will report toecaps=0 if it runs out of resources (this
5585 		 * depends on the config file).  It may not report 0 for other
5586 		 * capabilities dependent on the TOE in this case.  Set them to
5587 		 * 0 here so that the driver doesn't bother tracking resources
5588 		 * that will never be used.
5589 		 */
5590 		sc->iscsicaps = 0;
5591 		sc->rdmacaps = 0;
5592 	}
5593 	if (sc->rdmacaps) {
5594 		param[0] = FW_PARAM_PFVF(STAG_START);
5595 		param[1] = FW_PARAM_PFVF(STAG_END);
5596 		param[2] = FW_PARAM_PFVF(RQ_START);
5597 		param[3] = FW_PARAM_PFVF(RQ_END);
5598 		param[4] = FW_PARAM_PFVF(PBL_START);
5599 		param[5] = FW_PARAM_PFVF(PBL_END);
5600 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5601 		if (rc != 0) {
5602 			device_printf(sc->dev,
5603 			    "failed to query RDMA parameters(1): %d.\n", rc);
5604 			return (rc);
5605 		}
5606 		sc->vres.stag.start = val[0];
5607 		sc->vres.stag.size = val[1] - val[0] + 1;
5608 		sc->vres.rq.start = val[2];
5609 		sc->vres.rq.size = val[3] - val[2] + 1;
5610 		sc->vres.pbl.start = val[4];
5611 		sc->vres.pbl.size = val[5] - val[4] + 1;
5612 
5613 		param[0] = FW_PARAM_PFVF(SQRQ_START);
5614 		param[1] = FW_PARAM_PFVF(SQRQ_END);
5615 		param[2] = FW_PARAM_PFVF(CQ_START);
5616 		param[3] = FW_PARAM_PFVF(CQ_END);
5617 		param[4] = FW_PARAM_PFVF(OCQ_START);
5618 		param[5] = FW_PARAM_PFVF(OCQ_END);
5619 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5620 		if (rc != 0) {
5621 			device_printf(sc->dev,
5622 			    "failed to query RDMA parameters(2): %d.\n", rc);
5623 			return (rc);
5624 		}
5625 		sc->vres.qp.start = val[0];
5626 		sc->vres.qp.size = val[1] - val[0] + 1;
5627 		sc->vres.cq.start = val[2];
5628 		sc->vres.cq.size = val[3] - val[2] + 1;
5629 		sc->vres.ocq.start = val[4];
5630 		sc->vres.ocq.size = val[5] - val[4] + 1;
5631 
5632 		param[0] = FW_PARAM_PFVF(SRQ_START);
5633 		param[1] = FW_PARAM_PFVF(SRQ_END);
5634 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
5635 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5636 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5637 		if (rc != 0) {
5638 			device_printf(sc->dev,
5639 			    "failed to query RDMA parameters(3): %d.\n", rc);
5640 			return (rc);
5641 		}
5642 		sc->vres.srq.start = val[0];
5643 		sc->vres.srq.size = val[1] - val[0] + 1;
5644 		sc->params.max_ordird_qp = val[2];
5645 		sc->params.max_ird_adapter = val[3];
5646 	}
5647 	if (sc->iscsicaps) {
5648 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5649 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5650 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5651 		if (rc != 0) {
5652 			device_printf(sc->dev,
5653 			    "failed to query iSCSI parameters: %d.\n", rc);
5654 			return (rc);
5655 		}
5656 		sc->vres.iscsi.start = val[0];
5657 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5658 	}
5659 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5660 		param[0] = FW_PARAM_PFVF(TLS_START);
5661 		param[1] = FW_PARAM_PFVF(TLS_END);
5662 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5663 		if (rc != 0) {
5664 			device_printf(sc->dev,
5665 			    "failed to query TLS parameters: %d.\n", rc);
5666 			return (rc);
5667 		}
5668 		sc->vres.key.start = val[0];
5669 		sc->vres.key.size = val[1] - val[0] + 1;
5670 	}
5671 
5672 	/*
5673 	 * We've got the params we wanted to query directly from the firmware.
5674 	 * Grab some others via other means.
5675 	 */
5676 	t4_init_sge_params(sc);
5677 	t4_init_tp_params(sc);
5678 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5679 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5680 
5681 	rc = t4_verify_chip_settings(sc);
5682 	if (rc != 0)
5683 		return (rc);
5684 	t4_init_rx_buf_info(sc);
5685 
5686 	return (rc);
5687 }
5688 
5689 #ifdef KERN_TLS
5690 static void
ktls_tick(void * arg)5691 ktls_tick(void *arg)
5692 {
5693 	struct adapter *sc;
5694 	uint32_t tstamp;
5695 
5696 	sc = arg;
5697 	tstamp = tcp_ts_getticks();
5698 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
5699 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
5700 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
5701 }
5702 
5703 static int
t6_config_kern_tls(struct adapter * sc,bool enable)5704 t6_config_kern_tls(struct adapter *sc, bool enable)
5705 {
5706 	int rc;
5707 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5708 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
5709 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
5710 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
5711 
5712 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
5713 	if (rc != 0) {
5714 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
5715 		    enable ?  "enable" : "disable", rc);
5716 		return (rc);
5717 	}
5718 
5719 	if (enable) {
5720 		sc->flags |= KERN_TLS_ON;
5721 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
5722 		    C_HARDCLOCK);
5723 	} else {
5724 		sc->flags &= ~KERN_TLS_ON;
5725 		callout_stop(&sc->ktls_tick);
5726 	}
5727 
5728 	return (rc);
5729 }
5730 #endif
5731 
5732 static int
set_params__post_init(struct adapter * sc)5733 set_params__post_init(struct adapter *sc)
5734 {
5735 	uint32_t mask, param, val;
5736 #ifdef TCP_OFFLOAD
5737 	int i, v, shift;
5738 #endif
5739 
5740 	/* ask for encapsulated CPLs */
5741 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5742 	val = 1;
5743 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5744 
5745 	/* Enable 32b port caps if the firmware supports it. */
5746 	param = FW_PARAM_PFVF(PORT_CAPS32);
5747 	val = 1;
5748 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
5749 		sc->params.port_caps32 = 1;
5750 
5751 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
5752 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
5753 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
5754 	    V_MASKFILTER(val - 1));
5755 
5756 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
5757 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
5758 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5759 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
5760 	val = 0;
5761 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
5762 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
5763 		    F_ATTACKFILTERENABLE);
5764 		val |= F_DROPERRORATTACK;
5765 	}
5766 	if (t4_drop_ip_fragments != 0) {
5767 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
5768 		    F_FRAGMENTDROP);
5769 		val |= F_DROPERRORFRAG;
5770 	}
5771 	if (t4_drop_pkts_with_l2_errors != 0)
5772 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
5773 	if (t4_drop_pkts_with_l3_errors != 0) {
5774 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
5775 		    F_DROPERRORCSUMIP;
5776 	}
5777 	if (t4_drop_pkts_with_l4_errors != 0) {
5778 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5779 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
5780 	}
5781 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
5782 
5783 #ifdef TCP_OFFLOAD
5784 	/*
5785 	 * Override the TOE timers with user provided tunables.  This is not the
5786 	 * recommended way to change the timers (the firmware config file is) so
5787 	 * these tunables are not documented.
5788 	 *
5789 	 * All the timer tunables are in microseconds.
5790 	 */
5791 	if (t4_toe_keepalive_idle != 0) {
5792 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
5793 		v &= M_KEEPALIVEIDLE;
5794 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
5795 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
5796 	}
5797 	if (t4_toe_keepalive_interval != 0) {
5798 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
5799 		v &= M_KEEPALIVEINTVL;
5800 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
5801 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
5802 	}
5803 	if (t4_toe_keepalive_count != 0) {
5804 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
5805 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5806 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
5807 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
5808 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
5809 	}
5810 	if (t4_toe_rexmt_min != 0) {
5811 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
5812 		v &= M_RXTMIN;
5813 		t4_set_reg_field(sc, A_TP_RXT_MIN,
5814 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
5815 	}
5816 	if (t4_toe_rexmt_max != 0) {
5817 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
5818 		v &= M_RXTMAX;
5819 		t4_set_reg_field(sc, A_TP_RXT_MAX,
5820 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
5821 	}
5822 	if (t4_toe_rexmt_count != 0) {
5823 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
5824 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5825 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
5826 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
5827 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
5828 	}
5829 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
5830 		if (t4_toe_rexmt_backoff[i] != -1) {
5831 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
5832 			shift = (i & 3) << 3;
5833 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
5834 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
5835 		}
5836 	}
5837 #endif
5838 
5839 	/*
5840 	 * Limit TOE connections to 2 reassembly "islands".  This is
5841 	 * required to permit migrating TOE connections to either
5842 	 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
5843 	 */
5844 	t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
5845 	    V_PASSMODE(2));
5846 
5847 #ifdef KERN_TLS
5848 	if (is_ktls(sc)) {
5849 		sc->tlst.inline_keys = t4_tls_inline_keys;
5850 		sc->tlst.combo_wrs = t4_tls_combo_wrs;
5851 		if (t4_kern_tls != 0 && is_t6(sc))
5852 			t6_config_kern_tls(sc, true);
5853 	}
5854 #endif
5855 	return (0);
5856 }
5857 
5858 #undef FW_PARAM_PFVF
5859 #undef FW_PARAM_DEV
5860 
5861 static void
t4_set_desc(struct adapter * sc)5862 t4_set_desc(struct adapter *sc)
5863 {
5864 	struct adapter_params *p = &sc->params;
5865 
5866 	device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
5867 }
5868 
5869 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)5870 ifmedia_add4(struct ifmedia *ifm, int m)
5871 {
5872 
5873 	ifmedia_add(ifm, m, 0, NULL);
5874 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
5875 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
5876 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
5877 }
5878 
5879 /*
5880  * This is the selected media, which is not quite the same as the active media.
5881  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
5882  * and active are not the same, and "media: Ethernet selected" otherwise.
5883  */
5884 static void
set_current_media(struct port_info * pi)5885 set_current_media(struct port_info *pi)
5886 {
5887 	struct link_config *lc;
5888 	struct ifmedia *ifm;
5889 	int mword;
5890 	u_int speed;
5891 
5892 	PORT_LOCK_ASSERT_OWNED(pi);
5893 
5894 	/* Leave current media alone if it's already set to IFM_NONE. */
5895 	ifm = &pi->media;
5896 	if (ifm->ifm_cur != NULL &&
5897 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
5898 		return;
5899 
5900 	lc = &pi->link_cfg;
5901 	if (lc->requested_aneg != AUTONEG_DISABLE &&
5902 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
5903 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
5904 		return;
5905 	}
5906 	mword = IFM_ETHER | IFM_FDX;
5907 	if (lc->requested_fc & PAUSE_TX)
5908 		mword |= IFM_ETH_TXPAUSE;
5909 	if (lc->requested_fc & PAUSE_RX)
5910 		mword |= IFM_ETH_RXPAUSE;
5911 	if (lc->requested_speed == 0)
5912 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
5913 	else
5914 		speed = lc->requested_speed;
5915 	mword |= port_mword(pi, speed_to_fwcap(speed));
5916 	ifmedia_set(ifm, mword);
5917 }
5918 
5919 /*
5920  * Returns true if the ifmedia list for the port cannot change.
5921  */
5922 static bool
fixed_ifmedia(struct port_info * pi)5923 fixed_ifmedia(struct port_info *pi)
5924 {
5925 
5926 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
5927 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
5928 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
5929 	    pi->port_type == FW_PORT_TYPE_KX4 ||
5930 	    pi->port_type == FW_PORT_TYPE_KX ||
5931 	    pi->port_type == FW_PORT_TYPE_KR ||
5932 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
5933 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
5934 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
5935 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
5936 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
5937 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
5938 }
5939 
5940 static void
build_medialist(struct port_info * pi)5941 build_medialist(struct port_info *pi)
5942 {
5943 	uint32_t ss, speed;
5944 	int unknown, mword, bit;
5945 	struct link_config *lc;
5946 	struct ifmedia *ifm;
5947 
5948 	PORT_LOCK_ASSERT_OWNED(pi);
5949 
5950 	if (pi->flags & FIXED_IFMEDIA)
5951 		return;
5952 
5953 	/*
5954 	 * Rebuild the ifmedia list.
5955 	 */
5956 	ifm = &pi->media;
5957 	ifmedia_removeall(ifm);
5958 	lc = &pi->link_cfg;
5959 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
5960 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
5961 		MPASS(ss != 0);
5962 no_media:
5963 		MPASS(LIST_EMPTY(&ifm->ifm_list));
5964 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
5965 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
5966 		return;
5967 	}
5968 
5969 	unknown = 0;
5970 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
5971 		speed = 1 << bit;
5972 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
5973 		if (ss & speed) {
5974 			mword = port_mword(pi, speed);
5975 			if (mword == IFM_NONE) {
5976 				goto no_media;
5977 			} else if (mword == IFM_UNKNOWN)
5978 				unknown++;
5979 			else
5980 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
5981 		}
5982 	}
5983 	if (unknown > 0) /* Add one unknown for all unknown media types. */
5984 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
5985 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
5986 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
5987 
5988 	set_current_media(pi);
5989 }
5990 
5991 /*
5992  * Initialize the requested fields in the link config based on driver tunables.
5993  */
5994 static void
init_link_config(struct port_info * pi)5995 init_link_config(struct port_info *pi)
5996 {
5997 	struct link_config *lc = &pi->link_cfg;
5998 
5999 	PORT_LOCK_ASSERT_OWNED(pi);
6000 
6001 	lc->requested_caps = 0;
6002 	lc->requested_speed = 0;
6003 
6004 	if (t4_autoneg == 0)
6005 		lc->requested_aneg = AUTONEG_DISABLE;
6006 	else if (t4_autoneg == 1)
6007 		lc->requested_aneg = AUTONEG_ENABLE;
6008 	else
6009 		lc->requested_aneg = AUTONEG_AUTO;
6010 
6011 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
6012 	    PAUSE_AUTONEG);
6013 
6014 	if (t4_fec & FEC_AUTO)
6015 		lc->requested_fec = FEC_AUTO;
6016 	else if (t4_fec == 0)
6017 		lc->requested_fec = FEC_NONE;
6018 	else {
6019 		/* -1 is handled by the FEC_AUTO block above and not here. */
6020 		lc->requested_fec = t4_fec &
6021 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6022 		if (lc->requested_fec == 0)
6023 			lc->requested_fec = FEC_AUTO;
6024 	}
6025 	if (t4_force_fec < 0)
6026 		lc->force_fec = -1;
6027 	else if (t4_force_fec > 0)
6028 		lc->force_fec = 1;
6029 	else
6030 		lc->force_fec = 0;
6031 }
6032 
6033 /*
6034  * Makes sure that all requested settings comply with what's supported by the
6035  * port.  Returns the number of settings that were invalid and had to be fixed.
6036  */
6037 static int
fixup_link_config(struct port_info * pi)6038 fixup_link_config(struct port_info *pi)
6039 {
6040 	int n = 0;
6041 	struct link_config *lc = &pi->link_cfg;
6042 	uint32_t fwspeed;
6043 
6044 	PORT_LOCK_ASSERT_OWNED(pi);
6045 
6046 	/* Speed (when not autonegotiating) */
6047 	if (lc->requested_speed != 0) {
6048 		fwspeed = speed_to_fwcap(lc->requested_speed);
6049 		if ((fwspeed & lc->pcaps) == 0) {
6050 			n++;
6051 			lc->requested_speed = 0;
6052 		}
6053 	}
6054 
6055 	/* Link autonegotiation */
6056 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6057 	    lc->requested_aneg == AUTONEG_DISABLE ||
6058 	    lc->requested_aneg == AUTONEG_AUTO);
6059 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6060 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6061 		n++;
6062 		lc->requested_aneg = AUTONEG_AUTO;
6063 	}
6064 
6065 	/* Flow control */
6066 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6067 	if (lc->requested_fc & PAUSE_TX &&
6068 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6069 		n++;
6070 		lc->requested_fc &= ~PAUSE_TX;
6071 	}
6072 	if (lc->requested_fc & PAUSE_RX &&
6073 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6074 		n++;
6075 		lc->requested_fc &= ~PAUSE_RX;
6076 	}
6077 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6078 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6079 		n++;
6080 		lc->requested_fc |= PAUSE_AUTONEG;
6081 	}
6082 
6083 	/* FEC */
6084 	if ((lc->requested_fec & FEC_RS &&
6085 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6086 	    (lc->requested_fec & FEC_BASER_RS &&
6087 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6088 		n++;
6089 		lc->requested_fec = FEC_AUTO;
6090 	}
6091 
6092 	return (n);
6093 }
6094 
6095 /*
6096  * Apply the requested L1 settings, which are expected to be valid, to the
6097  * hardware.
6098  */
6099 static int
apply_link_config(struct port_info * pi)6100 apply_link_config(struct port_info *pi)
6101 {
6102 	struct adapter *sc = pi->adapter;
6103 	struct link_config *lc = &pi->link_cfg;
6104 	int rc;
6105 
6106 #ifdef INVARIANTS
6107 	ASSERT_SYNCHRONIZED_OP(sc);
6108 	PORT_LOCK_ASSERT_OWNED(pi);
6109 
6110 	if (lc->requested_aneg == AUTONEG_ENABLE)
6111 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6112 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6113 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6114 	if (lc->requested_fc & PAUSE_TX)
6115 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6116 	if (lc->requested_fc & PAUSE_RX)
6117 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6118 	if (lc->requested_fec & FEC_RS)
6119 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6120 	if (lc->requested_fec & FEC_BASER_RS)
6121 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6122 #endif
6123 	if (!(sc->flags & IS_VF)) {
6124 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
6125 		if (rc != 0) {
6126 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6127 			return (rc);
6128 		}
6129 	}
6130 
6131 	/*
6132 	 * An L1_CFG will almost always result in a link-change event if the
6133 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6134 	 * the notification is processed.  If the link is down then the actual
6135 	 * settings are meaningless.
6136 	 *
6137 	 * This takes care of the case where a change in the L1 settings may not
6138 	 * result in a notification.
6139 	 */
6140 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6141 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6142 
6143 	return (0);
6144 }
6145 
6146 #define FW_MAC_EXACT_CHUNK	7
6147 struct mcaddr_ctx {
6148 	if_t ifp;
6149 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6150 	uint64_t hash;
6151 	int i;
6152 	int del;
6153 	int rc;
6154 };
6155 
6156 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6157 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6158 {
6159 	struct mcaddr_ctx *ctx = arg;
6160 	struct vi_info *vi = if_getsoftc(ctx->ifp);
6161 	struct port_info *pi = vi->pi;
6162 	struct adapter *sc = pi->adapter;
6163 
6164 	if (ctx->rc < 0)
6165 		return (0);
6166 
6167 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6168 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6169 	ctx->i++;
6170 
6171 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6172 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6173 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6174 		if (ctx->rc < 0) {
6175 			int j;
6176 
6177 			for (j = 0; j < ctx->i; j++) {
6178 				if_printf(ctx->ifp,
6179 				    "failed to add mc address"
6180 				    " %02x:%02x:%02x:"
6181 				    "%02x:%02x:%02x rc=%d\n",
6182 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6183 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6184 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6185 				    -ctx->rc);
6186 			}
6187 			return (0);
6188 		}
6189 		ctx->del = 0;
6190 		ctx->i = 0;
6191 	}
6192 
6193 	return (1);
6194 }
6195 
6196 /*
6197  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6198  * indicates which parameters should be programmed (the rest are left alone).
6199  */
6200 int
update_mac_settings(if_t ifp,int flags)6201 update_mac_settings(if_t ifp, int flags)
6202 {
6203 	int rc = 0;
6204 	struct vi_info *vi = if_getsoftc(ifp);
6205 	struct port_info *pi = vi->pi;
6206 	struct adapter *sc = pi->adapter;
6207 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6208 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6209 
6210 	ASSERT_SYNCHRONIZED_OP(sc);
6211 	KASSERT(flags, ("%s: not told what to update.", __func__));
6212 
6213 	if (flags & XGMAC_MTU)
6214 		mtu = if_getmtu(ifp);
6215 
6216 	if (flags & XGMAC_PROMISC)
6217 		promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6218 
6219 	if (flags & XGMAC_ALLMULTI)
6220 		allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6221 
6222 	if (flags & XGMAC_VLANEX)
6223 		vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6224 
6225 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6226 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6227 		    allmulti, 1, vlanex, false);
6228 		if (rc) {
6229 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6230 			    rc);
6231 			return (rc);
6232 		}
6233 	}
6234 
6235 	if (flags & XGMAC_UCADDR) {
6236 		uint8_t ucaddr[ETHER_ADDR_LEN];
6237 
6238 		bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6239 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6240 		    ucaddr, true, &vi->smt_idx);
6241 		if (rc < 0) {
6242 			rc = -rc;
6243 			if_printf(ifp, "change_mac failed: %d\n", rc);
6244 			return (rc);
6245 		} else {
6246 			vi->xact_addr_filt = rc;
6247 			rc = 0;
6248 		}
6249 	}
6250 
6251 	if (flags & XGMAC_MCADDRS) {
6252 		struct epoch_tracker et;
6253 		struct mcaddr_ctx ctx;
6254 		int j;
6255 
6256 		ctx.ifp = ifp;
6257 		ctx.hash = 0;
6258 		ctx.i = 0;
6259 		ctx.del = 1;
6260 		ctx.rc = 0;
6261 		/*
6262 		 * Unlike other drivers, we accumulate list of pointers into
6263 		 * interface address lists and we need to keep it safe even
6264 		 * after if_foreach_llmaddr() returns, thus we must enter the
6265 		 * network epoch.
6266 		 */
6267 		NET_EPOCH_ENTER(et);
6268 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6269 		if (ctx.rc < 0) {
6270 			NET_EPOCH_EXIT(et);
6271 			rc = -ctx.rc;
6272 			return (rc);
6273 		}
6274 		if (ctx.i > 0) {
6275 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6276 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6277 			NET_EPOCH_EXIT(et);
6278 			if (rc < 0) {
6279 				rc = -rc;
6280 				for (j = 0; j < ctx.i; j++) {
6281 					if_printf(ifp,
6282 					    "failed to add mcast address"
6283 					    " %02x:%02x:%02x:"
6284 					    "%02x:%02x:%02x rc=%d\n",
6285 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6286 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6287 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6288 					    rc);
6289 				}
6290 				return (rc);
6291 			}
6292 			ctx.del = 0;
6293 		} else
6294 			NET_EPOCH_EXIT(et);
6295 
6296 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6297 		if (rc != 0)
6298 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6299 			    rc);
6300 		if (ctx.del == 0) {
6301 			/* We clobbered the VXLAN entry if there was one. */
6302 			pi->vxlan_tcam_entry = false;
6303 		}
6304 	}
6305 
6306 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6307 	    pi->vxlan_tcam_entry == false) {
6308 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6309 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6310 		    true);
6311 		if (rc < 0) {
6312 			rc = -rc;
6313 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6314 			    rc);
6315 		} else {
6316 			MPASS(rc == sc->rawf_base + pi->port_id);
6317 			rc = 0;
6318 			pi->vxlan_tcam_entry = true;
6319 		}
6320 	}
6321 
6322 	return (rc);
6323 }
6324 
6325 /*
6326  * {begin|end}_synchronized_op must be called from the same thread.
6327  */
6328 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6329 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6330     char *wmesg)
6331 {
6332 	int rc, pri;
6333 
6334 #ifdef WITNESS
6335 	/* the caller thinks it's ok to sleep, but is it really? */
6336 	if (flags & SLEEP_OK)
6337 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
6338 		    "begin_synchronized_op");
6339 #endif
6340 
6341 	if (INTR_OK)
6342 		pri = PCATCH;
6343 	else
6344 		pri = 0;
6345 
6346 	ADAPTER_LOCK(sc);
6347 	for (;;) {
6348 
6349 		if (vi && IS_DETACHING(vi)) {
6350 			rc = ENXIO;
6351 			goto done;
6352 		}
6353 
6354 		if (!IS_BUSY(sc)) {
6355 			rc = 0;
6356 			break;
6357 		}
6358 
6359 		if (!(flags & SLEEP_OK)) {
6360 			rc = EBUSY;
6361 			goto done;
6362 		}
6363 
6364 		if (mtx_sleep(&sc->flags, &sc->sc_lock, pri, wmesg, 0)) {
6365 			rc = EINTR;
6366 			goto done;
6367 		}
6368 	}
6369 
6370 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6371 	SET_BUSY(sc);
6372 #ifdef INVARIANTS
6373 	sc->last_op = wmesg;
6374 	sc->last_op_thr = curthread;
6375 	sc->last_op_flags = flags;
6376 #endif
6377 
6378 done:
6379 	if (!(flags & HOLD_LOCK) || rc)
6380 		ADAPTER_UNLOCK(sc);
6381 
6382 	return (rc);
6383 }
6384 
6385 /*
6386  * Tell if_ioctl and if_init that the VI is going away.  This is
6387  * special variant of begin_synchronized_op and must be paired with a
6388  * call to end_vi_detach.
6389  */
6390 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6391 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6392 {
6393 	ADAPTER_LOCK(sc);
6394 	SET_DETACHING(vi);
6395 	wakeup(&sc->flags);
6396 	while (IS_BUSY(sc))
6397 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6398 	SET_BUSY(sc);
6399 #ifdef INVARIANTS
6400 	sc->last_op = "t4detach";
6401 	sc->last_op_thr = curthread;
6402 	sc->last_op_flags = 0;
6403 #endif
6404 	ADAPTER_UNLOCK(sc);
6405 }
6406 
6407 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6408 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6409 {
6410 	ADAPTER_LOCK(sc);
6411 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6412 	CLR_BUSY(sc);
6413 	CLR_DETACHING(vi);
6414 	wakeup(&sc->flags);
6415 	ADAPTER_UNLOCK(sc);
6416 }
6417 
6418 /*
6419  * {begin|end}_synchronized_op must be called from the same thread.
6420  */
6421 void
end_synchronized_op(struct adapter * sc,int flags)6422 end_synchronized_op(struct adapter *sc, int flags)
6423 {
6424 
6425 	if (flags & LOCK_HELD)
6426 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6427 	else
6428 		ADAPTER_LOCK(sc);
6429 
6430 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6431 	CLR_BUSY(sc);
6432 	wakeup(&sc->flags);
6433 	ADAPTER_UNLOCK(sc);
6434 }
6435 
6436 static int
cxgbe_init_synchronized(struct vi_info * vi)6437 cxgbe_init_synchronized(struct vi_info *vi)
6438 {
6439 	struct port_info *pi = vi->pi;
6440 	struct adapter *sc = pi->adapter;
6441 	if_t ifp = vi->ifp;
6442 	int rc = 0, i;
6443 	struct sge_txq *txq;
6444 
6445 	ASSERT_SYNCHRONIZED_OP(sc);
6446 
6447 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6448 		return (0);	/* already running */
6449 
6450 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6451 		return (rc);	/* error message displayed already */
6452 
6453 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6454 		return (rc); /* error message displayed already */
6455 
6456 	rc = update_mac_settings(ifp, XGMAC_ALL);
6457 	if (rc)
6458 		goto done;	/* error message displayed already */
6459 
6460 	PORT_LOCK(pi);
6461 	if (pi->up_vis == 0) {
6462 		t4_update_port_info(pi);
6463 		fixup_link_config(pi);
6464 		build_medialist(pi);
6465 		apply_link_config(pi);
6466 	}
6467 
6468 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6469 	if (rc != 0) {
6470 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6471 		PORT_UNLOCK(pi);
6472 		goto done;
6473 	}
6474 
6475 	/*
6476 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6477 	 * if this changes.
6478 	 */
6479 
6480 	for_each_txq(vi, i, txq) {
6481 		TXQ_LOCK(txq);
6482 		txq->eq.flags |= EQ_ENABLED;
6483 		TXQ_UNLOCK(txq);
6484 	}
6485 
6486 	/*
6487 	 * The first iq of the first port to come up is used for tracing.
6488 	 */
6489 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6490 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6491 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
6492 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
6493 		    V_QUEUENUMBER(sc->traceq));
6494 		pi->flags |= HAS_TRACEQ;
6495 	}
6496 
6497 	/* all ok */
6498 	pi->up_vis++;
6499 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6500 	if (pi->link_cfg.link_ok)
6501 		t4_os_link_changed(pi);
6502 	PORT_UNLOCK(pi);
6503 
6504 	mtx_lock(&vi->tick_mtx);
6505 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6506 		callout_reset(&vi->tick, hz, vi_tick, vi);
6507 	else
6508 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6509 	mtx_unlock(&vi->tick_mtx);
6510 done:
6511 	if (rc != 0)
6512 		cxgbe_uninit_synchronized(vi);
6513 
6514 	return (rc);
6515 }
6516 
6517 /*
6518  * Idempotent.
6519  */
6520 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6521 cxgbe_uninit_synchronized(struct vi_info *vi)
6522 {
6523 	struct port_info *pi = vi->pi;
6524 	struct adapter *sc = pi->adapter;
6525 	if_t ifp = vi->ifp;
6526 	int rc, i;
6527 	struct sge_txq *txq;
6528 
6529 	ASSERT_SYNCHRONIZED_OP(sc);
6530 
6531 	if (!(vi->flags & VI_INIT_DONE)) {
6532 		if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6533 			KASSERT(0, ("uninited VI is running"));
6534 			if_printf(ifp, "uninited VI with running ifnet.  "
6535 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6536 			    "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6537 			    if_getdrvflags(ifp));
6538 		}
6539 		return (0);
6540 	}
6541 
6542 	/*
6543 	 * Disable the VI so that all its data in either direction is discarded
6544 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6545 	 * tick) intact as the TP can deliver negative advice or data that it's
6546 	 * holding in its RAM (for an offloaded connection) even after the VI is
6547 	 * disabled.
6548 	 */
6549 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6550 	if (rc) {
6551 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6552 		return (rc);
6553 	}
6554 
6555 	for_each_txq(vi, i, txq) {
6556 		TXQ_LOCK(txq);
6557 		txq->eq.flags &= ~EQ_ENABLED;
6558 		TXQ_UNLOCK(txq);
6559 	}
6560 
6561 	mtx_lock(&vi->tick_mtx);
6562 	callout_stop(&vi->tick);
6563 	mtx_unlock(&vi->tick_mtx);
6564 
6565 	PORT_LOCK(pi);
6566 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6567 		PORT_UNLOCK(pi);
6568 		return (0);
6569 	}
6570 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6571 	pi->up_vis--;
6572 	if (pi->up_vis > 0) {
6573 		PORT_UNLOCK(pi);
6574 		return (0);
6575 	}
6576 
6577 	pi->link_cfg.link_ok = false;
6578 	pi->link_cfg.speed = 0;
6579 	pi->link_cfg.link_down_rc = 255;
6580 	t4_os_link_changed(pi);
6581 	PORT_UNLOCK(pi);
6582 
6583 	return (0);
6584 }
6585 
6586 /*
6587  * It is ok for this function to fail midway and return right away.  t4_detach
6588  * will walk the entire sc->irq list and clean up whatever is valid.
6589  */
6590 int
t4_setup_intr_handlers(struct adapter * sc)6591 t4_setup_intr_handlers(struct adapter *sc)
6592 {
6593 	int rc, rid, p, q, v;
6594 	char s[8];
6595 	struct irq *irq;
6596 	struct port_info *pi;
6597 	struct vi_info *vi;
6598 	struct sge *sge = &sc->sge;
6599 	struct sge_rxq *rxq;
6600 #ifdef TCP_OFFLOAD
6601 	struct sge_ofld_rxq *ofld_rxq;
6602 #endif
6603 #ifdef DEV_NETMAP
6604 	struct sge_nm_rxq *nm_rxq;
6605 #endif
6606 #ifdef RSS
6607 	int nbuckets = rss_getnumbuckets();
6608 #endif
6609 
6610 	/*
6611 	 * Setup interrupts.
6612 	 */
6613 	irq = &sc->irq[0];
6614 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6615 	if (forwarding_intr_to_fwq(sc))
6616 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6617 
6618 	/* Multiple interrupts. */
6619 	if (sc->flags & IS_VF)
6620 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6621 		    ("%s: too few intr.", __func__));
6622 	else
6623 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6624 		    ("%s: too few intr.", __func__));
6625 
6626 	/* The first one is always error intr on PFs */
6627 	if (!(sc->flags & IS_VF)) {
6628 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6629 		if (rc != 0)
6630 			return (rc);
6631 		irq++;
6632 		rid++;
6633 	}
6634 
6635 	/* The second one is always the firmware event queue (first on VFs) */
6636 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6637 	if (rc != 0)
6638 		return (rc);
6639 	irq++;
6640 	rid++;
6641 
6642 	for_each_port(sc, p) {
6643 		pi = sc->port[p];
6644 		for_each_vi(pi, v, vi) {
6645 			vi->first_intr = rid - 1;
6646 
6647 			if (vi->nnmrxq > 0) {
6648 				int n = max(vi->nrxq, vi->nnmrxq);
6649 
6650 				rxq = &sge->rxq[vi->first_rxq];
6651 #ifdef DEV_NETMAP
6652 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6653 #endif
6654 				for (q = 0; q < n; q++) {
6655 					snprintf(s, sizeof(s), "%x%c%x", p,
6656 					    'a' + v, q);
6657 					if (q < vi->nrxq)
6658 						irq->rxq = rxq++;
6659 #ifdef DEV_NETMAP
6660 					if (q < vi->nnmrxq)
6661 						irq->nm_rxq = nm_rxq++;
6662 
6663 					if (irq->nm_rxq != NULL &&
6664 					    irq->rxq == NULL) {
6665 						/* Netmap rx only */
6666 						rc = t4_alloc_irq(sc, irq, rid,
6667 						    t4_nm_intr, irq->nm_rxq, s);
6668 					}
6669 					if (irq->nm_rxq != NULL &&
6670 					    irq->rxq != NULL) {
6671 						/* NIC and Netmap rx */
6672 						rc = t4_alloc_irq(sc, irq, rid,
6673 						    t4_vi_intr, irq, s);
6674 					}
6675 #endif
6676 					if (irq->rxq != NULL &&
6677 					    irq->nm_rxq == NULL) {
6678 						/* NIC rx only */
6679 						rc = t4_alloc_irq(sc, irq, rid,
6680 						    t4_intr, irq->rxq, s);
6681 					}
6682 					if (rc != 0)
6683 						return (rc);
6684 #ifdef RSS
6685 					if (q < vi->nrxq) {
6686 						bus_bind_intr(sc->dev, irq->res,
6687 						    rss_getcpu(q % nbuckets));
6688 					}
6689 #endif
6690 					irq++;
6691 					rid++;
6692 					vi->nintr++;
6693 				}
6694 			} else {
6695 				for_each_rxq(vi, q, rxq) {
6696 					snprintf(s, sizeof(s), "%x%c%x", p,
6697 					    'a' + v, q);
6698 					rc = t4_alloc_irq(sc, irq, rid,
6699 					    t4_intr, rxq, s);
6700 					if (rc != 0)
6701 						return (rc);
6702 #ifdef RSS
6703 					bus_bind_intr(sc->dev, irq->res,
6704 					    rss_getcpu(q % nbuckets));
6705 #endif
6706 					irq++;
6707 					rid++;
6708 					vi->nintr++;
6709 				}
6710 			}
6711 #ifdef TCP_OFFLOAD
6712 			for_each_ofld_rxq(vi, q, ofld_rxq) {
6713 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
6714 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
6715 				    ofld_rxq, s);
6716 				if (rc != 0)
6717 					return (rc);
6718 				irq++;
6719 				rid++;
6720 				vi->nintr++;
6721 			}
6722 #endif
6723 		}
6724 	}
6725 	MPASS(irq == &sc->irq[sc->intr_count]);
6726 
6727 	return (0);
6728 }
6729 
6730 static void
write_global_rss_key(struct adapter * sc)6731 write_global_rss_key(struct adapter *sc)
6732 {
6733 #ifdef RSS
6734 	int i;
6735 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6736 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6737 
6738 	CTASSERT(RSS_KEYSIZE == 40);
6739 
6740 	rss_getkey((void *)&raw_rss_key[0]);
6741 	for (i = 0; i < nitems(rss_key); i++) {
6742 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
6743 	}
6744 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
6745 #endif
6746 }
6747 
6748 /*
6749  * Idempotent.
6750  */
6751 static int
adapter_full_init(struct adapter * sc)6752 adapter_full_init(struct adapter *sc)
6753 {
6754 	int rc, i;
6755 
6756 	ASSERT_SYNCHRONIZED_OP(sc);
6757 
6758 	/*
6759 	 * queues that belong to the adapter (not any particular port).
6760 	 */
6761 	rc = t4_setup_adapter_queues(sc);
6762 	if (rc != 0)
6763 		return (rc);
6764 
6765 	MPASS(sc->params.nports <= nitems(sc->tq));
6766 	for (i = 0; i < sc->params.nports; i++) {
6767 		if (sc->tq[i] != NULL)
6768 			continue;
6769 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
6770 		    taskqueue_thread_enqueue, &sc->tq[i]);
6771 		if (sc->tq[i] == NULL) {
6772 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
6773 			return (ENOMEM);
6774 		}
6775 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
6776 		    device_get_nameunit(sc->dev), i);
6777 	}
6778 
6779 	if (!(sc->flags & IS_VF)) {
6780 		write_global_rss_key(sc);
6781 		t4_intr_enable(sc);
6782 	}
6783 	return (0);
6784 }
6785 
6786 int
adapter_init(struct adapter * sc)6787 adapter_init(struct adapter *sc)
6788 {
6789 	int rc;
6790 
6791 	ASSERT_SYNCHRONIZED_OP(sc);
6792 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
6793 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
6794 	    ("%s: FULL_INIT_DONE already", __func__));
6795 
6796 	rc = adapter_full_init(sc);
6797 	if (rc != 0)
6798 		adapter_full_uninit(sc);
6799 	else
6800 		sc->flags |= FULL_INIT_DONE;
6801 
6802 	return (rc);
6803 }
6804 
6805 /*
6806  * Idempotent.
6807  */
6808 static void
adapter_full_uninit(struct adapter * sc)6809 adapter_full_uninit(struct adapter *sc)
6810 {
6811 	int i;
6812 
6813 	t4_teardown_adapter_queues(sc);
6814 
6815 	for (i = 0; i < nitems(sc->tq); i++) {
6816 		if (sc->tq[i] == NULL)
6817 			continue;
6818 		taskqueue_free(sc->tq[i]);
6819 		sc->tq[i] = NULL;
6820 	}
6821 
6822 	sc->flags &= ~FULL_INIT_DONE;
6823 }
6824 
6825 #ifdef RSS
6826 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
6827     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
6828     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
6829     RSS_HASHTYPE_RSS_UDP_IPV6)
6830 
6831 /* Translates kernel hash types to hardware. */
6832 static int
hashconfig_to_hashen(int hashconfig)6833 hashconfig_to_hashen(int hashconfig)
6834 {
6835 	int hashen = 0;
6836 
6837 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
6838 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
6839 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
6840 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
6841 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
6842 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6843 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6844 	}
6845 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
6846 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6847 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6848 	}
6849 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
6850 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6851 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
6852 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6853 
6854 	return (hashen);
6855 }
6856 
6857 /* Translates hardware hash types to kernel. */
6858 static int
hashen_to_hashconfig(int hashen)6859 hashen_to_hashconfig(int hashen)
6860 {
6861 	int hashconfig = 0;
6862 
6863 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
6864 		/*
6865 		 * If UDP hashing was enabled it must have been enabled for
6866 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
6867 		 * enabling any 4-tuple hash is nonsense configuration.
6868 		 */
6869 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6870 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
6871 
6872 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6873 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
6874 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6875 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
6876 	}
6877 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6878 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
6879 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6880 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
6881 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
6882 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
6883 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
6884 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
6885 
6886 	return (hashconfig);
6887 }
6888 #endif
6889 
6890 /*
6891  * Idempotent.
6892  */
6893 static int
vi_full_init(struct vi_info * vi)6894 vi_full_init(struct vi_info *vi)
6895 {
6896 	struct adapter *sc = vi->adapter;
6897 	struct sge_rxq *rxq;
6898 	int rc, i, j;
6899 #ifdef RSS
6900 	int nbuckets = rss_getnumbuckets();
6901 	int hashconfig = rss_gethashconfig();
6902 	int extra;
6903 #endif
6904 
6905 	ASSERT_SYNCHRONIZED_OP(sc);
6906 
6907 	/*
6908 	 * Allocate tx/rx/fl queues for this VI.
6909 	 */
6910 	rc = t4_setup_vi_queues(vi);
6911 	if (rc != 0)
6912 		return (rc);
6913 
6914 	/*
6915 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
6916 	 */
6917 	if (vi->nrxq > vi->rss_size) {
6918 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
6919 		    "some queues will never receive traffic.\n", vi->nrxq,
6920 		    vi->rss_size);
6921 	} else if (vi->rss_size % vi->nrxq) {
6922 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
6923 		    "expect uneven traffic distribution.\n", vi->nrxq,
6924 		    vi->rss_size);
6925 	}
6926 #ifdef RSS
6927 	if (vi->nrxq != nbuckets) {
6928 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
6929 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
6930 	}
6931 #endif
6932 	if (vi->rss == NULL)
6933 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
6934 		    M_ZERO | M_WAITOK);
6935 	for (i = 0; i < vi->rss_size;) {
6936 #ifdef RSS
6937 		j = rss_get_indirection_to_bucket(i);
6938 		j %= vi->nrxq;
6939 		rxq = &sc->sge.rxq[vi->first_rxq + j];
6940 		vi->rss[i++] = rxq->iq.abs_id;
6941 #else
6942 		for_each_rxq(vi, j, rxq) {
6943 			vi->rss[i++] = rxq->iq.abs_id;
6944 			if (i == vi->rss_size)
6945 				break;
6946 		}
6947 #endif
6948 	}
6949 
6950 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
6951 	    vi->rss, vi->rss_size);
6952 	if (rc != 0) {
6953 		CH_ERR(vi, "rss_config failed: %d\n", rc);
6954 		return (rc);
6955 	}
6956 
6957 #ifdef RSS
6958 	vi->hashen = hashconfig_to_hashen(hashconfig);
6959 
6960 	/*
6961 	 * We may have had to enable some hashes even though the global config
6962 	 * wants them disabled.  This is a potential problem that must be
6963 	 * reported to the user.
6964 	 */
6965 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
6966 
6967 	/*
6968 	 * If we consider only the supported hash types, then the enabled hashes
6969 	 * are a superset of the requested hashes.  In other words, there cannot
6970 	 * be any supported hash that was requested but not enabled, but there
6971 	 * can be hashes that were not requested but had to be enabled.
6972 	 */
6973 	extra &= SUPPORTED_RSS_HASHTYPES;
6974 	MPASS((extra & hashconfig) == 0);
6975 
6976 	if (extra) {
6977 		CH_ALERT(vi,
6978 		    "global RSS config (0x%x) cannot be accommodated.\n",
6979 		    hashconfig);
6980 	}
6981 	if (extra & RSS_HASHTYPE_RSS_IPV4)
6982 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
6983 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
6984 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
6985 	if (extra & RSS_HASHTYPE_RSS_IPV6)
6986 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
6987 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
6988 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
6989 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
6990 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
6991 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
6992 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
6993 #else
6994 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
6995 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
6996 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6997 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
6998 #endif
6999 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
7000 	    0, 0);
7001 	if (rc != 0) {
7002 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
7003 		return (rc);
7004 	}
7005 
7006 	return (0);
7007 }
7008 
7009 int
vi_init(struct vi_info * vi)7010 vi_init(struct vi_info *vi)
7011 {
7012 	int rc;
7013 
7014 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
7015 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
7016 	    ("%s: VI_INIT_DONE already", __func__));
7017 
7018 	rc = vi_full_init(vi);
7019 	if (rc != 0)
7020 		vi_full_uninit(vi);
7021 	else
7022 		vi->flags |= VI_INIT_DONE;
7023 
7024 	return (rc);
7025 }
7026 
7027 /*
7028  * Idempotent.
7029  */
7030 static void
vi_full_uninit(struct vi_info * vi)7031 vi_full_uninit(struct vi_info *vi)
7032 {
7033 
7034 	if (vi->flags & VI_INIT_DONE) {
7035 		quiesce_vi(vi);
7036 		free(vi->rss, M_CXGBE);
7037 		free(vi->nm_rss, M_CXGBE);
7038 	}
7039 
7040 	t4_teardown_vi_queues(vi);
7041 	vi->flags &= ~VI_INIT_DONE;
7042 }
7043 
7044 static void
quiesce_txq(struct sge_txq * txq)7045 quiesce_txq(struct sge_txq *txq)
7046 {
7047 	struct sge_eq *eq = &txq->eq;
7048 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7049 
7050 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7051 	MPASS(!(eq->flags & EQ_ENABLED));
7052 
7053 	/* Wait for the mp_ring to empty. */
7054 	while (!mp_ring_is_idle(txq->r)) {
7055 		mp_ring_check_drainage(txq->r, 4096);
7056 		pause("rquiesce", 1);
7057 	}
7058 	MPASS(txq->txp.npkt == 0);
7059 
7060 	if (eq->flags & EQ_HW_ALLOCATED) {
7061 		/*
7062 		 * Hardware is alive and working normally.  Wait for it to
7063 		 * finish and then wait for the driver to catch up and reclaim
7064 		 * all descriptors.
7065 		 */
7066 		while (spg->cidx != htobe16(eq->pidx))
7067 			pause("equiesce", 1);
7068 		while (eq->cidx != eq->pidx)
7069 			pause("dquiesce", 1);
7070 	} else {
7071 		/*
7072 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7073 		 * descriptors directly.
7074 		 */
7075 		TXQ_LOCK(txq);
7076 		while (eq->cidx != eq->pidx) {
7077 			struct mbuf *m, *nextpkt;
7078 			struct tx_sdesc *txsd;
7079 
7080 			txsd = &txq->sdesc[eq->cidx];
7081 			for (m = txsd->m; m != NULL; m = nextpkt) {
7082 				nextpkt = m->m_nextpkt;
7083 				m->m_nextpkt = NULL;
7084 				m_freem(m);
7085 			}
7086 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7087 		}
7088 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7089 		TXQ_UNLOCK(txq);
7090 	}
7091 }
7092 
7093 static void
quiesce_wrq(struct sge_wrq * wrq)7094 quiesce_wrq(struct sge_wrq *wrq)
7095 {
7096 	struct wrqe *wr;
7097 
7098 	TXQ_LOCK(wrq);
7099 	while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7100 		STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7101 #ifdef INVARIANTS
7102 		wrq->nwr_pending--;
7103 		wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7104 #endif
7105 		free(wr, M_CXGBE);
7106 	}
7107 	MPASS(wrq->nwr_pending == 0);
7108 	MPASS(wrq->ndesc_needed == 0);
7109 	wrq->nwr_pending = 0;
7110 	wrq->ndesc_needed = 0;
7111 	TXQ_UNLOCK(wrq);
7112 }
7113 
7114 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7115 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7116 {
7117 	/* Synchronize with the interrupt handler */
7118 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7119 		pause("iqfree", 1);
7120 
7121 	if (fl != NULL) {
7122 		MPASS(iq->flags & IQ_HAS_FL);
7123 
7124 		mtx_lock(&sc->sfl_lock);
7125 		FL_LOCK(fl);
7126 		fl->flags |= FL_DOOMED;
7127 		FL_UNLOCK(fl);
7128 		callout_stop(&sc->sfl_callout);
7129 		mtx_unlock(&sc->sfl_lock);
7130 
7131 		KASSERT((fl->flags & FL_STARVING) == 0,
7132 		    ("%s: still starving", __func__));
7133 
7134 		/* Release all buffers if hardware is no longer available. */
7135 		if (!(iq->flags & IQ_HW_ALLOCATED))
7136 			free_fl_buffers(sc, fl);
7137 	}
7138 }
7139 
7140 /*
7141  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7142  * that no new work is being enqueued by the hardware or the driver.  That part
7143  * should be arranged before calling this function.
7144  */
7145 static void
quiesce_vi(struct vi_info * vi)7146 quiesce_vi(struct vi_info *vi)
7147 {
7148 	int i;
7149 	struct adapter *sc = vi->adapter;
7150 	struct sge_rxq *rxq;
7151 	struct sge_txq *txq;
7152 #ifdef TCP_OFFLOAD
7153 	struct sge_ofld_rxq *ofld_rxq;
7154 #endif
7155 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7156 	struct sge_ofld_txq *ofld_txq;
7157 #endif
7158 
7159 	if (!(vi->flags & VI_INIT_DONE))
7160 		return;
7161 
7162 	for_each_txq(vi, i, txq) {
7163 		quiesce_txq(txq);
7164 	}
7165 
7166 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7167 	for_each_ofld_txq(vi, i, ofld_txq) {
7168 		quiesce_wrq(&ofld_txq->wrq);
7169 	}
7170 #endif
7171 
7172 	for_each_rxq(vi, i, rxq) {
7173 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7174 	}
7175 
7176 #ifdef TCP_OFFLOAD
7177 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7178 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7179 	}
7180 #endif
7181 }
7182 
7183 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7184 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7185     driver_intr_t *handler, void *arg, char *name)
7186 {
7187 	int rc;
7188 
7189 	irq->rid = rid;
7190 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7191 	    RF_SHAREABLE | RF_ACTIVE);
7192 	if (irq->res == NULL) {
7193 		device_printf(sc->dev,
7194 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7195 		return (ENOMEM);
7196 	}
7197 
7198 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7199 	    NULL, handler, arg, &irq->tag);
7200 	if (rc != 0) {
7201 		device_printf(sc->dev,
7202 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7203 		    rid, name, rc);
7204 	} else if (name)
7205 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7206 
7207 	return (rc);
7208 }
7209 
7210 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7211 t4_free_irq(struct adapter *sc, struct irq *irq)
7212 {
7213 	if (irq->tag)
7214 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7215 	if (irq->res)
7216 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7217 
7218 	bzero(irq, sizeof(*irq));
7219 
7220 	return (0);
7221 }
7222 
7223 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7224 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7225 {
7226 
7227 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7228 	t4_get_regs(sc, buf, regs->len);
7229 }
7230 
7231 #define	A_PL_INDIR_CMD	0x1f8
7232 
7233 #define	S_PL_AUTOINC	31
7234 #define	M_PL_AUTOINC	0x1U
7235 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7236 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7237 
7238 #define	S_PL_VFID	20
7239 #define	M_PL_VFID	0xffU
7240 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7241 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7242 
7243 #define	S_PL_ADDR	0
7244 #define	M_PL_ADDR	0xfffffU
7245 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7246 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7247 
7248 #define	A_PL_INDIR_DATA	0x1fc
7249 
7250 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7251 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7252 {
7253 	u32 stats[2];
7254 
7255 	if (sc->flags & IS_VF) {
7256 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7257 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7258 	} else {
7259 		mtx_assert(&sc->reg_lock, MA_OWNED);
7260 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7261 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7262 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7263 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7264 	}
7265 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7266 }
7267 
7268 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7269 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7270 {
7271 
7272 #define GET_STAT(name) \
7273 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7274 
7275 	if (!(sc->flags & IS_VF))
7276 		mtx_lock(&sc->reg_lock);
7277 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7278 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7279 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7280 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7281 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7282 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7283 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7284 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7285 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7286 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7287 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7288 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7289 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7290 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7291 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7292 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7293 	if (!(sc->flags & IS_VF))
7294 		mtx_unlock(&sc->reg_lock);
7295 
7296 #undef GET_STAT
7297 }
7298 
7299 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7300 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7301 {
7302 	int reg;
7303 
7304 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7305 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7306 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7307 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7308 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7309 }
7310 
7311 static void
vi_refresh_stats(struct vi_info * vi)7312 vi_refresh_stats(struct vi_info *vi)
7313 {
7314 	struct timeval tv;
7315 	const struct timeval interval = {0, 250000};	/* 250ms */
7316 
7317 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7318 
7319 	if (vi->flags & VI_SKIP_STATS)
7320 		return;
7321 
7322 	getmicrotime(&tv);
7323 	timevalsub(&tv, &interval);
7324 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7325 		return;
7326 
7327 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7328 	getmicrotime(&vi->last_refreshed);
7329 }
7330 
7331 static void
cxgbe_refresh_stats(struct vi_info * vi)7332 cxgbe_refresh_stats(struct vi_info *vi)
7333 {
7334 	u_int i, v, tnl_cong_drops, chan_map;
7335 	struct timeval tv;
7336 	const struct timeval interval = {0, 250000};	/* 250ms */
7337 	struct port_info *pi;
7338 	struct adapter *sc;
7339 
7340 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7341 
7342 	if (vi->flags & VI_SKIP_STATS)
7343 		return;
7344 
7345 	getmicrotime(&tv);
7346 	timevalsub(&tv, &interval);
7347 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7348 		return;
7349 
7350 	pi = vi->pi;
7351 	sc = vi->adapter;
7352 	tnl_cong_drops = 0;
7353 	t4_get_port_stats(sc, pi->port_id, &pi->stats);
7354 	chan_map = pi->rx_e_chan_map;
7355 	while (chan_map) {
7356 		i = ffs(chan_map) - 1;
7357 		mtx_lock(&sc->reg_lock);
7358 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7359 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7360 		mtx_unlock(&sc->reg_lock);
7361 		tnl_cong_drops += v;
7362 		chan_map &= ~(1 << i);
7363 	}
7364 	pi->tnl_cong_drops = tnl_cong_drops;
7365 	getmicrotime(&vi->last_refreshed);
7366 }
7367 
7368 static void
cxgbe_tick(void * arg)7369 cxgbe_tick(void *arg)
7370 {
7371 	struct vi_info *vi = arg;
7372 
7373 	MPASS(IS_MAIN_VI(vi));
7374 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7375 
7376 	cxgbe_refresh_stats(vi);
7377 	callout_schedule(&vi->tick, hz);
7378 }
7379 
7380 static void
vi_tick(void * arg)7381 vi_tick(void *arg)
7382 {
7383 	struct vi_info *vi = arg;
7384 
7385 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7386 
7387 	vi_refresh_stats(vi);
7388 	callout_schedule(&vi->tick, hz);
7389 }
7390 
7391 /*
7392  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7393  */
7394 static char *caps_decoder[] = {
7395 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7396 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7397 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7398 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7399 	    "\006HASHFILTER\007ETHOFLD",
7400 	"\20\001TOE",					/* 4: TOE */
7401 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
7402 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7403 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7404 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7405 	    "\007T10DIF"
7406 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7407 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7408 	    "\004TLS_HW",
7409 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7410 		    "\004PO_INITIATOR\005PO_TARGET",
7411 };
7412 
7413 void
t4_sysctls(struct adapter * sc)7414 t4_sysctls(struct adapter *sc)
7415 {
7416 	struct sysctl_ctx_list *ctx = &sc->ctx;
7417 	struct sysctl_oid *oid;
7418 	struct sysctl_oid_list *children, *c0;
7419 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7420 
7421 	/*
7422 	 * dev.t4nex.X.
7423 	 */
7424 	oid = device_get_sysctl_tree(sc->dev);
7425 	c0 = children = SYSCTL_CHILDREN(oid);
7426 
7427 	sc->sc_do_rxcopy = 1;
7428 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7429 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7430 
7431 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7432 	    sc->params.nports, "# of ports");
7433 
7434 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7435 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7436 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7437 	    "available doorbells");
7438 
7439 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7440 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7441 
7442 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7443 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7444 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7445 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7446 
7447 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7448 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7449 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7450 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7451 
7452 	t4_sge_sysctls(sc, ctx, children);
7453 
7454 	sc->lro_timeout = 100;
7455 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7456 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7457 
7458 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7459 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7460 
7461 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7462 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7463 
7464 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7465 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7466 
7467 	if (sc->flags & IS_VF)
7468 		return;
7469 
7470 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7471 	    NULL, chip_rev(sc), "chip hardware revision");
7472 
7473 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7474 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7475 
7476 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7477 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7478 
7479 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7480 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7481 
7482 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7483 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7484 
7485 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7486 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7487 
7488 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7489 	    sc->er_version, 0, "expansion ROM version");
7490 
7491 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7492 	    sc->bs_version, 0, "bootstrap firmware version");
7493 
7494 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7495 	    NULL, sc->params.scfg_vers, "serial config version");
7496 
7497 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7498 	    NULL, sc->params.vpd_vers, "VPD version");
7499 
7500 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7501 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7502 
7503 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7504 	    sc->cfcsum, "config file checksum");
7505 
7506 #define SYSCTL_CAP(name, n, text) \
7507 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7508 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7509 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7510 	    "available " text " capabilities")
7511 
7512 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7513 	SYSCTL_CAP(linkcaps, 1, "link");
7514 	SYSCTL_CAP(switchcaps, 2, "switch");
7515 	SYSCTL_CAP(niccaps, 3, "NIC");
7516 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7517 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7518 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7519 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7520 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7521 #undef SYSCTL_CAP
7522 
7523 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7524 	    NULL, sc->tids.nftids, "number of filters");
7525 
7526 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7527 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7528 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7529 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7530 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7531 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7532 
7533 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg",
7534 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7535 	    sysctl_loadavg, "A",
7536 	    "microprocessor load averages (debug firmwares only)");
7537 
7538 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7539 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7540 	    "I", "core Vdd (in mV)");
7541 
7542 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7543 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7544 	    sysctl_cpus, "A", "local CPUs");
7545 
7546 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7547 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7548 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7549 
7550 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7551 	    &sc->swintr, 0, "software triggered interrupts");
7552 
7553 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7554 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7555 	    "1 = reset adapter, 0 = zero reset counter");
7556 
7557 	/*
7558 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7559 	 */
7560 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7561 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
7562 	    "logs and miscellaneous information");
7563 	children = SYSCTL_CHILDREN(oid);
7564 
7565 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
7566 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7567 	    sysctl_cctrl, "A", "congestion control");
7568 
7569 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
7570 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7571 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
7572 
7573 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
7574 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7575 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
7576 
7577 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
7578 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7579 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
7580 
7581 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
7582 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 3,
7583 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
7584 
7585 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
7586 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 4,
7587 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
7588 
7589 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
7590 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 5,
7591 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
7592 
7593 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
7594 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7595 	    sysctl_cim_la, "A", "CIM logic analyzer");
7596 
7597 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
7598 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7599 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7600 
7601 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
7602 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7603 	    0 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
7604 
7605 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
7606 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7607 	    1 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
7608 
7609 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
7610 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7611 	    2 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
7612 
7613 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
7614 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7615 	    3 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
7616 
7617 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
7618 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7619 	    4 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
7620 
7621 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
7622 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7623 	    5 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
7624 
7625 	if (chip_id(sc) > CHELSIO_T4) {
7626 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
7627 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7628 		    6 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7629 		    "CIM OBQ 6 (SGE0-RX)");
7630 
7631 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
7632 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7633 		    7 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7634 		    "CIM OBQ 7 (SGE1-RX)");
7635 	}
7636 
7637 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
7638 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7639 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7640 
7641 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
7642 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7643 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
7644 
7645 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
7646 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7647 	    sysctl_cpl_stats, "A", "CPL statistics");
7648 
7649 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
7650 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7651 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
7652 
7653 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
7654 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7655 	    sysctl_tid_stats, "A", "tid stats");
7656 
7657 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
7658 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7659 	    sysctl_devlog, "A", "firmware's device log");
7660 
7661 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
7662 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7663 	    sysctl_fcoe_stats, "A", "FCoE statistics");
7664 
7665 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
7666 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7667 	    sysctl_hw_sched, "A", "hardware scheduler ");
7668 
7669 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
7670 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7671 	    sysctl_l2t, "A", "hardware L2 table");
7672 
7673 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
7674 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7675 	    sysctl_smt, "A", "hardware source MAC table");
7676 
7677 #ifdef INET6
7678 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
7679 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7680 	    sysctl_clip, "A", "active CLIP table entries");
7681 #endif
7682 
7683 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
7684 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7685 	    sysctl_lb_stats, "A", "loopback statistics");
7686 
7687 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
7688 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7689 	    sysctl_meminfo, "A", "memory regions");
7690 
7691 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
7692 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7693 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
7694 	    "A", "MPS TCAM entries");
7695 
7696 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
7697 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7698 	    sysctl_path_mtus, "A", "path MTUs");
7699 
7700 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
7701 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7702 	    sysctl_pm_stats, "A", "PM statistics");
7703 
7704 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
7705 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7706 	    sysctl_rdma_stats, "A", "RDMA statistics");
7707 
7708 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
7709 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7710 	    sysctl_tcp_stats, "A", "TCP statistics");
7711 
7712 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
7713 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7714 	    sysctl_tids, "A", "TID information");
7715 
7716 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
7717 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7718 	    sysctl_tp_err_stats, "A", "TP error statistics");
7719 
7720 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
7721 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7722 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
7723 
7724 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
7725 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7726 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
7727 
7728 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
7729 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7730 	    sysctl_tp_la, "A", "TP logic analyzer");
7731 
7732 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
7733 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7734 	    sysctl_tx_rate, "A", "Tx rate");
7735 
7736 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
7737 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7738 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
7739 
7740 	if (chip_id(sc) >= CHELSIO_T5) {
7741 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
7742 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7743 		    sysctl_wcwr_stats, "A", "write combined work requests");
7744 	}
7745 
7746 #ifdef KERN_TLS
7747 	if (is_ktls(sc)) {
7748 		/*
7749 		 * dev.t4nex.0.tls.
7750 		 */
7751 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
7752 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
7753 		children = SYSCTL_CHILDREN(oid);
7754 
7755 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
7756 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
7757 		    "keys in work requests (1) or attempt to store TLS keys "
7758 		    "in card memory.");
7759 
7760 		if (is_t6(sc))
7761 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
7762 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
7763 			    "combine TCB field updates with TLS record work "
7764 			    "requests.");
7765 	}
7766 #endif
7767 
7768 #ifdef TCP_OFFLOAD
7769 	if (is_offload(sc)) {
7770 		int i;
7771 		char s[4];
7772 
7773 		/*
7774 		 * dev.t4nex.X.toe.
7775 		 */
7776 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
7777 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
7778 		children = SYSCTL_CHILDREN(oid);
7779 
7780 		sc->tt.cong_algorithm = -1;
7781 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
7782 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
7783 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
7784 		    "3 = highspeed)");
7785 
7786 		sc->tt.sndbuf = -1;
7787 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
7788 		    &sc->tt.sndbuf, 0, "hardware send buffer");
7789 
7790 		sc->tt.ddp = 0;
7791 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
7792 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
7793 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
7794 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
7795 
7796 		sc->tt.rx_coalesce = -1;
7797 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
7798 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
7799 
7800 		sc->tt.tls = 0;
7801 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
7802 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
7803 		    "Inline TLS allowed");
7804 
7805 		sc->tt.tx_align = -1;
7806 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
7807 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
7808 
7809 		sc->tt.tx_zcopy = 0;
7810 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
7811 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
7812 		    "Enable zero-copy aio_write(2)");
7813 
7814 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
7815 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7816 		    "cop_managed_offloading", CTLFLAG_RW,
7817 		    &sc->tt.cop_managed_offloading, 0,
7818 		    "COP (Connection Offload Policy) controls all TOE offload");
7819 
7820 		sc->tt.autorcvbuf_inc = 16 * 1024;
7821 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
7822 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
7823 		    "autorcvbuf increment");
7824 
7825 		sc->tt.update_hc_on_pmtu_change = 1;
7826 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7827 		    "update_hc_on_pmtu_change", CTLFLAG_RW,
7828 		    &sc->tt.update_hc_on_pmtu_change, 0,
7829 		    "Update hostcache entry if the PMTU changes");
7830 
7831 		sc->tt.iso = 1;
7832 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
7833 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
7834 
7835 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
7836 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7837 		    sysctl_tp_tick, "A", "TP timer tick (us)");
7838 
7839 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
7840 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7841 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
7842 
7843 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
7844 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7845 		    sysctl_tp_tick, "A", "DACK tick (us)");
7846 
7847 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
7848 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7849 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
7850 
7851 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
7852 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7853 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
7854 		    "Minimum retransmit interval (us)");
7855 
7856 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
7857 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7858 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
7859 		    "Maximum retransmit interval (us)");
7860 
7861 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
7862 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7863 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
7864 		    "Persist timer min (us)");
7865 
7866 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
7867 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7868 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
7869 		    "Persist timer max (us)");
7870 
7871 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
7872 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7873 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
7874 		    "Keepalive idle timer (us)");
7875 
7876 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
7877 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7878 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
7879 		    "Keepalive interval timer (us)");
7880 
7881 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
7882 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7883 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
7884 
7885 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
7886 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7887 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
7888 		    "FINWAIT2 timer (us)");
7889 
7890 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
7891 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7892 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
7893 		    "Number of SYN retransmissions before abort");
7894 
7895 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
7896 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7897 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
7898 		    "Number of retransmissions before abort");
7899 
7900 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
7901 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7902 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
7903 		    "Number of keepalive probes before abort");
7904 
7905 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
7906 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7907 		    "TOE retransmit backoffs");
7908 		children = SYSCTL_CHILDREN(oid);
7909 		for (i = 0; i < 16; i++) {
7910 			snprintf(s, sizeof(s), "%u", i);
7911 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
7912 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7913 			    i, sysctl_tp_backoff, "IU",
7914 			    "TOE retransmit backoff");
7915 		}
7916 	}
7917 #endif
7918 }
7919 
7920 void
vi_sysctls(struct vi_info * vi)7921 vi_sysctls(struct vi_info *vi)
7922 {
7923 	struct sysctl_ctx_list *ctx = &vi->ctx;
7924 	struct sysctl_oid *oid;
7925 	struct sysctl_oid_list *children;
7926 
7927 	/*
7928 	 * dev.v?(cxgbe|cxl).X.
7929 	 */
7930 	oid = device_get_sysctl_tree(vi->dev);
7931 	children = SYSCTL_CHILDREN(oid);
7932 
7933 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
7934 	    vi->viid, "VI identifer");
7935 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
7936 	    &vi->nrxq, 0, "# of rx queues");
7937 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
7938 	    &vi->ntxq, 0, "# of tx queues");
7939 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
7940 	    &vi->first_rxq, 0, "index of first rx queue");
7941 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
7942 	    &vi->first_txq, 0, "index of first tx queue");
7943 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
7944 	    vi->rss_base, "start of RSS indirection table");
7945 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
7946 	    vi->rss_size, "size of RSS indirection table");
7947 
7948 	if (IS_MAIN_VI(vi)) {
7949 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
7950 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7951 		    sysctl_noflowq, "IU",
7952 		    "Reserve queue 0 for non-flowid packets");
7953 	}
7954 
7955 	if (vi->adapter->flags & IS_VF) {
7956 		MPASS(vi->flags & TX_USES_VM_WR);
7957 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
7958 		    NULL, 1, "use VM work requests for transmit");
7959 	} else {
7960 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
7961 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7962 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
7963 	}
7964 
7965 #ifdef TCP_OFFLOAD
7966 	if (vi->nofldrxq != 0) {
7967 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
7968 		    &vi->nofldrxq, 0,
7969 		    "# of rx queues for offloaded TCP connections");
7970 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
7971 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
7972 		    "index of first TOE rx queue");
7973 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
7974 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7975 		    sysctl_holdoff_tmr_idx_ofld, "I",
7976 		    "holdoff timer index for TOE queues");
7977 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
7978 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7979 		    sysctl_holdoff_pktc_idx_ofld, "I",
7980 		    "holdoff packet counter index for TOE queues");
7981 	}
7982 #endif
7983 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7984 	if (vi->nofldtxq != 0) {
7985 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
7986 		    &vi->nofldtxq, 0,
7987 		    "# of tx queues for TOE/ETHOFLD");
7988 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
7989 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
7990 		    "index of first TOE/ETHOFLD tx queue");
7991 	}
7992 #endif
7993 #ifdef DEV_NETMAP
7994 	if (vi->nnmrxq != 0) {
7995 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
7996 		    &vi->nnmrxq, 0, "# of netmap rx queues");
7997 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
7998 		    &vi->nnmtxq, 0, "# of netmap tx queues");
7999 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
8000 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
8001 		    "index of first netmap rx queue");
8002 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
8003 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
8004 		    "index of first netmap tx queue");
8005 	}
8006 #endif
8007 
8008 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
8009 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8010 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
8011 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
8012 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8013 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
8014 
8015 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
8016 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8017 	    sysctl_qsize_rxq, "I", "rx queue size");
8018 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
8019 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8020 	    sysctl_qsize_txq, "I", "tx queue size");
8021 }
8022 
8023 static void
cxgbe_sysctls(struct port_info * pi)8024 cxgbe_sysctls(struct port_info *pi)
8025 {
8026 	struct sysctl_ctx_list *ctx = &pi->ctx;
8027 	struct sysctl_oid *oid;
8028 	struct sysctl_oid_list *children, *children2;
8029 	struct adapter *sc = pi->adapter;
8030 	int i;
8031 	char name[16];
8032 	static char *tc_flags = {"\20\1USER"};
8033 
8034 	/*
8035 	 * dev.cxgbe.X.
8036 	 */
8037 	oid = device_get_sysctl_tree(pi->dev);
8038 	children = SYSCTL_CHILDREN(oid);
8039 
8040 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8041 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8042 	    sysctl_linkdnrc, "A", "reason why link is down");
8043 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8044 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8045 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8046 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8047 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8048 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8049 		    sysctl_btphy, "I", "PHY firmware version");
8050 	}
8051 
8052 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8053 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8054 	    sysctl_pause_settings, "A",
8055 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8056 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8057 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8058 	    "FEC in use on the link");
8059 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8060 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8061 	    sysctl_requested_fec, "A",
8062 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8063 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8064 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8065 	    "FEC recommended by the cable/transceiver");
8066 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8067 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8068 	    sysctl_autoneg, "I",
8069 	    "autonegotiation (-1 = not supported)");
8070 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8071 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8072 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8073 
8074 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8075 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8076 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8077 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8078 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8079 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8080 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8081 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8082 
8083 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8084 	    port_top_speed(pi), "max speed (in Gbps)");
8085 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8086 	    pi->mps_bg_map, "MPS buffer group map");
8087 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8088 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8089 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8090 	    pi->tx_chan, "TP tx c-channel");
8091 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8092 	    pi->rx_chan, "TP rx c-channel");
8093 
8094 	if (sc->flags & IS_VF)
8095 		return;
8096 
8097 	/*
8098 	 * dev.(cxgbe|cxl).X.tc.
8099 	 */
8100 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8101 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8102 	    "Tx scheduler traffic classes (cl_rl)");
8103 	children2 = SYSCTL_CHILDREN(oid);
8104 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8105 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8106 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8107 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8108 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8109 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8110 	for (i = 0; i < sc->params.nsched_cls; i++) {
8111 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8112 
8113 		snprintf(name, sizeof(name), "%d", i);
8114 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8115 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8116 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8117 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8118 		    CTLFLAG_RD, &tc->state, 0, "current state");
8119 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8120 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8121 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8122 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8123 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8124 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8125 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8126 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8127 		    "traffic class parameters");
8128 	}
8129 
8130 	/*
8131 	 * dev.cxgbe.X.stats.
8132 	 */
8133 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8134 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8135 	children = SYSCTL_CHILDREN(oid);
8136 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8137 	    &pi->tx_parse_error, 0,
8138 	    "# of tx packets with invalid length or # of segments");
8139 
8140 #define T4_REGSTAT(name, stat, desc) \
8141     SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8142 	CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8143 	t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8144         sysctl_handle_t4_reg64, "QU", desc)
8145 
8146 /* We get these from port_stats and they may be stale by up to 1s */
8147 #define T4_PORTSTAT(name, desc) \
8148 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
8149 	    &pi->stats.name, desc)
8150 
8151 	T4_REGSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8152 	T4_REGSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8153 	T4_REGSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8154 	T4_REGSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8155 	T4_REGSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8156 	T4_REGSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8157 	T4_REGSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8158 	T4_REGSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8159 	T4_REGSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8160 	T4_REGSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8161 	T4_REGSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8162 	T4_REGSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8163 	T4_REGSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8164 	T4_REGSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8165 	T4_REGSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8166 	T4_REGSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8167 	T4_REGSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8168 	T4_REGSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8169 	T4_REGSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8170 	T4_REGSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8171 	T4_REGSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8172 	T4_REGSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8173 	T4_REGSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8174 
8175 	T4_REGSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8176 	T4_REGSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8177 	T4_REGSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8178 	T4_REGSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8179 	T4_REGSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8180 	T4_REGSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8181 	T4_REGSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8182 	if (is_t6(sc)) {
8183 		T4_PORTSTAT(rx_fcs_err,
8184 		    "# of frames received with bad FCS since last link up");
8185 	} else {
8186 		T4_REGSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8187 		    "# of frames received with bad FCS");
8188 	}
8189 	T4_REGSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8190 	T4_REGSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8191 	T4_REGSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8192 	T4_REGSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8193 	T4_REGSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8194 	T4_REGSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8195 	T4_REGSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8196 	T4_REGSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8197 	T4_REGSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8198 	T4_REGSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8199 	T4_REGSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8200 	T4_REGSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8201 	T4_REGSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8202 	T4_REGSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8203 	T4_REGSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8204 	T4_REGSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8205 	T4_REGSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8206 	T4_REGSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8207 	T4_REGSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8208 
8209 	T4_PORTSTAT(rx_ovflow0, "# drops due to buffer-group 0 overflows");
8210 	T4_PORTSTAT(rx_ovflow1, "# drops due to buffer-group 1 overflows");
8211 	T4_PORTSTAT(rx_ovflow2, "# drops due to buffer-group 2 overflows");
8212 	T4_PORTSTAT(rx_ovflow3, "# drops due to buffer-group 3 overflows");
8213 	T4_PORTSTAT(rx_trunc0, "# of buffer-group 0 truncated packets");
8214 	T4_PORTSTAT(rx_trunc1, "# of buffer-group 1 truncated packets");
8215 	T4_PORTSTAT(rx_trunc2, "# of buffer-group 2 truncated packets");
8216 	T4_PORTSTAT(rx_trunc3, "# of buffer-group 3 truncated packets");
8217 
8218 #undef T4_REGSTAT
8219 #undef T4_PORTSTAT
8220 }
8221 
8222 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8223 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8224 {
8225 	int rc, *i, space = 0;
8226 	struct sbuf sb;
8227 
8228 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8229 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8230 		if (space)
8231 			sbuf_printf(&sb, " ");
8232 		sbuf_printf(&sb, "%d", *i);
8233 		space = 1;
8234 	}
8235 	rc = sbuf_finish(&sb);
8236 	sbuf_delete(&sb);
8237 	return (rc);
8238 }
8239 
8240 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8241 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8242 {
8243 	int rc;
8244 	struct sbuf *sb;
8245 
8246 	rc = sysctl_wire_old_buffer(req, 0);
8247 	if (rc != 0)
8248 		return(rc);
8249 
8250 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8251 	if (sb == NULL)
8252 		return (ENOMEM);
8253 
8254 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8255 	rc = sbuf_finish(sb);
8256 	sbuf_delete(sb);
8257 
8258 	return (rc);
8259 }
8260 
8261 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8262 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8263 {
8264 	int rc;
8265 	struct sbuf *sb;
8266 
8267 	rc = sysctl_wire_old_buffer(req, 0);
8268 	if (rc != 0)
8269 		return(rc);
8270 
8271 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8272 	if (sb == NULL)
8273 		return (ENOMEM);
8274 
8275 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8276 	rc = sbuf_finish(sb);
8277 	sbuf_delete(sb);
8278 
8279 	return (rc);
8280 }
8281 
8282 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8283 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8284 {
8285 	struct port_info *pi = arg1;
8286 	int op = arg2;
8287 	struct adapter *sc = pi->adapter;
8288 	u_int v;
8289 	int rc;
8290 
8291 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8292 	if (rc)
8293 		return (rc);
8294 	if (hw_off_limits(sc))
8295 		rc = ENXIO;
8296 	else {
8297 		/* XXX: magic numbers */
8298 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8299 		    op ? 0x20 : 0xc820, &v);
8300 	}
8301 	end_synchronized_op(sc, 0);
8302 	if (rc)
8303 		return (rc);
8304 	if (op == 0)
8305 		v /= 256;
8306 
8307 	rc = sysctl_handle_int(oidp, &v, 0, req);
8308 	return (rc);
8309 }
8310 
8311 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8312 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8313 {
8314 	struct vi_info *vi = arg1;
8315 	int rc, val;
8316 
8317 	val = vi->rsrv_noflowq;
8318 	rc = sysctl_handle_int(oidp, &val, 0, req);
8319 	if (rc != 0 || req->newptr == NULL)
8320 		return (rc);
8321 
8322 	if ((val >= 1) && (vi->ntxq > 1))
8323 		vi->rsrv_noflowq = 1;
8324 	else
8325 		vi->rsrv_noflowq = 0;
8326 
8327 	return (rc);
8328 }
8329 
8330 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8331 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8332 {
8333 	struct vi_info *vi = arg1;
8334 	struct adapter *sc = vi->adapter;
8335 	int rc, val, i;
8336 
8337 	MPASS(!(sc->flags & IS_VF));
8338 
8339 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8340 	rc = sysctl_handle_int(oidp, &val, 0, req);
8341 	if (rc != 0 || req->newptr == NULL)
8342 		return (rc);
8343 
8344 	if (val != 0 && val != 1)
8345 		return (EINVAL);
8346 
8347 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8348 	    "t4txvm");
8349 	if (rc)
8350 		return (rc);
8351 	if (hw_off_limits(sc))
8352 		rc = ENXIO;
8353 	else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8354 		/*
8355 		 * We don't want parse_pkt to run with one setting (VF or PF)
8356 		 * and then eth_tx to see a different setting but still use
8357 		 * stale information calculated by parse_pkt.
8358 		 */
8359 		rc = EBUSY;
8360 	} else {
8361 		struct port_info *pi = vi->pi;
8362 		struct sge_txq *txq;
8363 		uint32_t ctrl0;
8364 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8365 
8366 		if (val) {
8367 			vi->flags |= TX_USES_VM_WR;
8368 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8369 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8370 			    V_TXPKT_INTF(pi->tx_chan));
8371 			if (!(sc->flags & IS_VF))
8372 				npkt--;
8373 		} else {
8374 			vi->flags &= ~TX_USES_VM_WR;
8375 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8376 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8377 			    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
8378 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8379 		}
8380 		for_each_txq(vi, i, txq) {
8381 			txq->cpl_ctrl0 = ctrl0;
8382 			txq->txp.max_npkt = npkt;
8383 		}
8384 	}
8385 	end_synchronized_op(sc, LOCK_HELD);
8386 	return (rc);
8387 }
8388 
8389 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8390 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8391 {
8392 	struct vi_info *vi = arg1;
8393 	struct adapter *sc = vi->adapter;
8394 	int idx, rc, i;
8395 	struct sge_rxq *rxq;
8396 	uint8_t v;
8397 
8398 	idx = vi->tmr_idx;
8399 
8400 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8401 	if (rc != 0 || req->newptr == NULL)
8402 		return (rc);
8403 
8404 	if (idx < 0 || idx >= SGE_NTIMERS)
8405 		return (EINVAL);
8406 
8407 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8408 	    "t4tmr");
8409 	if (rc)
8410 		return (rc);
8411 
8412 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8413 	for_each_rxq(vi, i, rxq) {
8414 #ifdef atomic_store_rel_8
8415 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8416 #else
8417 		rxq->iq.intr_params = v;
8418 #endif
8419 	}
8420 	vi->tmr_idx = idx;
8421 
8422 	end_synchronized_op(sc, LOCK_HELD);
8423 	return (0);
8424 }
8425 
8426 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8427 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8428 {
8429 	struct vi_info *vi = arg1;
8430 	struct adapter *sc = vi->adapter;
8431 	int idx, rc;
8432 
8433 	idx = vi->pktc_idx;
8434 
8435 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8436 	if (rc != 0 || req->newptr == NULL)
8437 		return (rc);
8438 
8439 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8440 		return (EINVAL);
8441 
8442 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8443 	    "t4pktc");
8444 	if (rc)
8445 		return (rc);
8446 
8447 	if (vi->flags & VI_INIT_DONE)
8448 		rc = EBUSY; /* cannot be changed once the queues are created */
8449 	else
8450 		vi->pktc_idx = idx;
8451 
8452 	end_synchronized_op(sc, LOCK_HELD);
8453 	return (rc);
8454 }
8455 
8456 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8457 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8458 {
8459 	struct vi_info *vi = arg1;
8460 	struct adapter *sc = vi->adapter;
8461 	int qsize, rc;
8462 
8463 	qsize = vi->qsize_rxq;
8464 
8465 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8466 	if (rc != 0 || req->newptr == NULL)
8467 		return (rc);
8468 
8469 	if (qsize < 128 || (qsize & 7))
8470 		return (EINVAL);
8471 
8472 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8473 	    "t4rxqs");
8474 	if (rc)
8475 		return (rc);
8476 
8477 	if (vi->flags & VI_INIT_DONE)
8478 		rc = EBUSY; /* cannot be changed once the queues are created */
8479 	else
8480 		vi->qsize_rxq = qsize;
8481 
8482 	end_synchronized_op(sc, LOCK_HELD);
8483 	return (rc);
8484 }
8485 
8486 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8487 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8488 {
8489 	struct vi_info *vi = arg1;
8490 	struct adapter *sc = vi->adapter;
8491 	int qsize, rc;
8492 
8493 	qsize = vi->qsize_txq;
8494 
8495 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8496 	if (rc != 0 || req->newptr == NULL)
8497 		return (rc);
8498 
8499 	if (qsize < 128 || qsize > 65536)
8500 		return (EINVAL);
8501 
8502 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8503 	    "t4txqs");
8504 	if (rc)
8505 		return (rc);
8506 
8507 	if (vi->flags & VI_INIT_DONE)
8508 		rc = EBUSY; /* cannot be changed once the queues are created */
8509 	else
8510 		vi->qsize_txq = qsize;
8511 
8512 	end_synchronized_op(sc, LOCK_HELD);
8513 	return (rc);
8514 }
8515 
8516 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8517 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8518 {
8519 	struct port_info *pi = arg1;
8520 	struct adapter *sc = pi->adapter;
8521 	struct link_config *lc = &pi->link_cfg;
8522 	int rc;
8523 
8524 	if (req->newptr == NULL) {
8525 		struct sbuf *sb;
8526 		static char *bits = "\20\1RX\2TX\3AUTO";
8527 
8528 		rc = sysctl_wire_old_buffer(req, 0);
8529 		if (rc != 0)
8530 			return(rc);
8531 
8532 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8533 		if (sb == NULL)
8534 			return (ENOMEM);
8535 
8536 		if (lc->link_ok) {
8537 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8538 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8539 		} else {
8540 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8541 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8542 		}
8543 		rc = sbuf_finish(sb);
8544 		sbuf_delete(sb);
8545 	} else {
8546 		char s[2];
8547 		int n;
8548 
8549 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8550 		    PAUSE_AUTONEG));
8551 		s[1] = 0;
8552 
8553 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8554 		if (rc != 0)
8555 			return(rc);
8556 
8557 		if (s[1] != 0)
8558 			return (EINVAL);
8559 		if (s[0] < '0' || s[0] > '9')
8560 			return (EINVAL);	/* not a number */
8561 		n = s[0] - '0';
8562 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8563 			return (EINVAL);	/* some other bit is set too */
8564 
8565 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8566 		    "t4PAUSE");
8567 		if (rc)
8568 			return (rc);
8569 		if (!hw_off_limits(sc)) {
8570 			PORT_LOCK(pi);
8571 			lc->requested_fc = n;
8572 			fixup_link_config(pi);
8573 			if (pi->up_vis > 0)
8574 				rc = apply_link_config(pi);
8575 			set_current_media(pi);
8576 			PORT_UNLOCK(pi);
8577 		}
8578 		end_synchronized_op(sc, 0);
8579 	}
8580 
8581 	return (rc);
8582 }
8583 
8584 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8585 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8586 {
8587 	struct port_info *pi = arg1;
8588 	struct link_config *lc = &pi->link_cfg;
8589 	int rc;
8590 	struct sbuf *sb;
8591 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2";
8592 
8593 	rc = sysctl_wire_old_buffer(req, 0);
8594 	if (rc != 0)
8595 		return(rc);
8596 
8597 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8598 	if (sb == NULL)
8599 		return (ENOMEM);
8600 	if (lc->link_ok)
8601 		sbuf_printf(sb, "%b", lc->fec, bits);
8602 	else
8603 		sbuf_printf(sb, "no link");
8604 	rc = sbuf_finish(sb);
8605 	sbuf_delete(sb);
8606 
8607 	return (rc);
8608 }
8609 
8610 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)8611 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8612 {
8613 	struct port_info *pi = arg1;
8614 	struct adapter *sc = pi->adapter;
8615 	struct link_config *lc = &pi->link_cfg;
8616 	int rc;
8617 	int8_t old;
8618 
8619 	if (req->newptr == NULL) {
8620 		struct sbuf *sb;
8621 		static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2"
8622 		    "\5RSVD3\6auto\7module";
8623 
8624 		rc = sysctl_wire_old_buffer(req, 0);
8625 		if (rc != 0)
8626 			return(rc);
8627 
8628 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8629 		if (sb == NULL)
8630 			return (ENOMEM);
8631 
8632 		sbuf_printf(sb, "%b", lc->requested_fec, bits);
8633 		rc = sbuf_finish(sb);
8634 		sbuf_delete(sb);
8635 	} else {
8636 		char s[8];
8637 		int n;
8638 
8639 		snprintf(s, sizeof(s), "%d",
8640 		    lc->requested_fec == FEC_AUTO ? -1 :
8641 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
8642 
8643 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8644 		if (rc != 0)
8645 			return(rc);
8646 
8647 		n = strtol(&s[0], NULL, 0);
8648 		if (n < 0 || n & FEC_AUTO)
8649 			n = FEC_AUTO;
8650 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
8651 			return (EINVAL);/* some other bit is set too */
8652 
8653 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8654 		    "t4reqf");
8655 		if (rc)
8656 			return (rc);
8657 		PORT_LOCK(pi);
8658 		old = lc->requested_fec;
8659 		if (n == FEC_AUTO)
8660 			lc->requested_fec = FEC_AUTO;
8661 		else if (n == 0 || n == FEC_NONE)
8662 			lc->requested_fec = FEC_NONE;
8663 		else {
8664 			if ((lc->pcaps |
8665 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
8666 			    lc->pcaps) {
8667 				rc = ENOTSUP;
8668 				goto done;
8669 			}
8670 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
8671 			    FEC_MODULE);
8672 		}
8673 		if (!hw_off_limits(sc)) {
8674 			fixup_link_config(pi);
8675 			if (pi->up_vis > 0) {
8676 				rc = apply_link_config(pi);
8677 				if (rc != 0) {
8678 					lc->requested_fec = old;
8679 					if (rc == FW_EPROTO)
8680 						rc = ENOTSUP;
8681 				}
8682 			}
8683 		}
8684 done:
8685 		PORT_UNLOCK(pi);
8686 		end_synchronized_op(sc, 0);
8687 	}
8688 
8689 	return (rc);
8690 }
8691 
8692 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)8693 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
8694 {
8695 	struct port_info *pi = arg1;
8696 	struct adapter *sc = pi->adapter;
8697 	struct link_config *lc = &pi->link_cfg;
8698 	int rc;
8699 	int8_t fec;
8700 	struct sbuf *sb;
8701 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2\5RSVD3";
8702 
8703 	rc = sysctl_wire_old_buffer(req, 0);
8704 	if (rc != 0)
8705 		return (rc);
8706 
8707 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8708 	if (sb == NULL)
8709 		return (ENOMEM);
8710 
8711 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
8712 		rc = EBUSY;
8713 		goto done;
8714 	}
8715 	if (hw_off_limits(sc)) {
8716 		rc = ENXIO;
8717 		goto done;
8718 	}
8719 	PORT_LOCK(pi);
8720 	if (pi->up_vis == 0) {
8721 		/*
8722 		 * If all the interfaces are administratively down the firmware
8723 		 * does not report transceiver changes.  Refresh port info here.
8724 		 * This is the only reason we have a synchronized op in this
8725 		 * function.  Just PORT_LOCK would have been enough otherwise.
8726 		 */
8727 		t4_update_port_info(pi);
8728 	}
8729 
8730 	fec = lc->fec_hint;
8731 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
8732 	    !fec_supported(lc->pcaps)) {
8733 		sbuf_printf(sb, "n/a");
8734 	} else {
8735 		if (fec == 0)
8736 			fec = FEC_NONE;
8737 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, bits);
8738 	}
8739 	rc = sbuf_finish(sb);
8740 	PORT_UNLOCK(pi);
8741 done:
8742 	sbuf_delete(sb);
8743 	end_synchronized_op(sc, 0);
8744 
8745 	return (rc);
8746 }
8747 
8748 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)8749 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
8750 {
8751 	struct port_info *pi = arg1;
8752 	struct adapter *sc = pi->adapter;
8753 	struct link_config *lc = &pi->link_cfg;
8754 	int rc, val;
8755 
8756 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
8757 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
8758 	else
8759 		val = -1;
8760 	rc = sysctl_handle_int(oidp, &val, 0, req);
8761 	if (rc != 0 || req->newptr == NULL)
8762 		return (rc);
8763 	if (val == 0)
8764 		val = AUTONEG_DISABLE;
8765 	else if (val == 1)
8766 		val = AUTONEG_ENABLE;
8767 	else
8768 		val = AUTONEG_AUTO;
8769 
8770 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8771 	    "t4aneg");
8772 	if (rc)
8773 		return (rc);
8774 	PORT_LOCK(pi);
8775 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
8776 		rc = ENOTSUP;
8777 		goto done;
8778 	}
8779 	lc->requested_aneg = val;
8780 	if (!hw_off_limits(sc)) {
8781 		fixup_link_config(pi);
8782 		if (pi->up_vis > 0)
8783 			rc = apply_link_config(pi);
8784 		set_current_media(pi);
8785 	}
8786 done:
8787 	PORT_UNLOCK(pi);
8788 	end_synchronized_op(sc, 0);
8789 	return (rc);
8790 }
8791 
8792 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)8793 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
8794 {
8795 	struct port_info *pi = arg1;
8796 	struct adapter *sc = pi->adapter;
8797 	struct link_config *lc = &pi->link_cfg;
8798 	int rc, val;
8799 
8800 	val = lc->force_fec;
8801 	MPASS(val >= -1 && val <= 1);
8802 	rc = sysctl_handle_int(oidp, &val, 0, req);
8803 	if (rc != 0 || req->newptr == NULL)
8804 		return (rc);
8805 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
8806 		return (ENOTSUP);
8807 	if (val < -1 || val > 1)
8808 		return (EINVAL);
8809 
8810 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
8811 	if (rc)
8812 		return (rc);
8813 	PORT_LOCK(pi);
8814 	lc->force_fec = val;
8815 	if (!hw_off_limits(sc)) {
8816 		fixup_link_config(pi);
8817 		if (pi->up_vis > 0)
8818 			rc = apply_link_config(pi);
8819 	}
8820 	PORT_UNLOCK(pi);
8821 	end_synchronized_op(sc, 0);
8822 	return (rc);
8823 }
8824 
8825 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)8826 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
8827 {
8828 	struct adapter *sc = arg1;
8829 	int rc, reg = arg2;
8830 	uint64_t val;
8831 
8832 	mtx_lock(&sc->reg_lock);
8833 	if (hw_off_limits(sc))
8834 		rc = ENXIO;
8835 	else {
8836 		rc = 0;
8837 		val = t4_read_reg64(sc, reg);
8838 	}
8839 	mtx_unlock(&sc->reg_lock);
8840 	if (rc == 0)
8841 		rc = sysctl_handle_64(oidp, &val, 0, req);
8842 	return (rc);
8843 }
8844 
8845 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)8846 sysctl_temperature(SYSCTL_HANDLER_ARGS)
8847 {
8848 	struct adapter *sc = arg1;
8849 	int rc, t;
8850 	uint32_t param, val;
8851 
8852 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
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_TMP);
8861 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8862 	}
8863 	end_synchronized_op(sc, 0);
8864 	if (rc)
8865 		return (rc);
8866 
8867 	/* unknown is returned as 0 but we display -1 in that case */
8868 	t = val == 0 ? -1 : val;
8869 
8870 	rc = sysctl_handle_int(oidp, &t, 0, req);
8871 	return (rc);
8872 }
8873 
8874 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)8875 sysctl_vdd(SYSCTL_HANDLER_ARGS)
8876 {
8877 	struct adapter *sc = arg1;
8878 	int rc;
8879 	uint32_t param, val;
8880 
8881 	if (sc->params.core_vdd == 0) {
8882 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8883 		    "t4vdd");
8884 		if (rc)
8885 			return (rc);
8886 		if (hw_off_limits(sc))
8887 			rc = ENXIO;
8888 		else {
8889 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8890 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8891 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
8892 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
8893 			    &param, &val);
8894 		}
8895 		end_synchronized_op(sc, 0);
8896 		if (rc)
8897 			return (rc);
8898 		sc->params.core_vdd = val;
8899 	}
8900 
8901 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
8902 }
8903 
8904 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)8905 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
8906 {
8907 	struct adapter *sc = arg1;
8908 	int rc, v;
8909 	uint32_t param, val;
8910 
8911 	v = sc->sensor_resets;
8912 	rc = sysctl_handle_int(oidp, &v, 0, req);
8913 	if (rc != 0 || req->newptr == NULL || v <= 0)
8914 		return (rc);
8915 
8916 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
8917 	    chip_id(sc) < CHELSIO_T5)
8918 		return (ENOTSUP);
8919 
8920 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
8921 	if (rc)
8922 		return (rc);
8923 	if (hw_off_limits(sc))
8924 		rc = ENXIO;
8925 	else {
8926 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8927 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8928 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
8929 		val = 1;
8930 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8931 	}
8932 	end_synchronized_op(sc, 0);
8933 	if (rc == 0)
8934 		sc->sensor_resets++;
8935 	return (rc);
8936 }
8937 
8938 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)8939 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
8940 {
8941 	struct adapter *sc = arg1;
8942 	struct sbuf *sb;
8943 	int rc;
8944 	uint32_t param, val;
8945 
8946 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
8947 	if (rc)
8948 		return (rc);
8949 	if (hw_off_limits(sc))
8950 		rc = ENXIO;
8951 	else {
8952 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8953 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
8954 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8955 	}
8956 	end_synchronized_op(sc, 0);
8957 	if (rc)
8958 		return (rc);
8959 
8960 	rc = sysctl_wire_old_buffer(req, 0);
8961 	if (rc != 0)
8962 		return (rc);
8963 
8964 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8965 	if (sb == NULL)
8966 		return (ENOMEM);
8967 
8968 	if (val == 0xffffffff) {
8969 		/* Only debug and custom firmwares report load averages. */
8970 		sbuf_printf(sb, "not available");
8971 	} else {
8972 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
8973 		    (val >> 16) & 0xff);
8974 	}
8975 	rc = sbuf_finish(sb);
8976 	sbuf_delete(sb);
8977 
8978 	return (rc);
8979 }
8980 
8981 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)8982 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
8983 {
8984 	struct adapter *sc = arg1;
8985 	struct sbuf *sb;
8986 	int rc, i;
8987 	uint16_t incr[NMTUS][NCCTRL_WIN];
8988 	static const char *dec_fac[] = {
8989 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
8990 		"0.9375"
8991 	};
8992 
8993 	rc = sysctl_wire_old_buffer(req, 0);
8994 	if (rc != 0)
8995 		return (rc);
8996 
8997 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8998 	if (sb == NULL)
8999 		return (ENOMEM);
9000 
9001 	mtx_lock(&sc->reg_lock);
9002 	if (hw_off_limits(sc))
9003 		rc = ENXIO;
9004 	else
9005 		t4_read_cong_tbl(sc, incr);
9006 	mtx_unlock(&sc->reg_lock);
9007 	if (rc)
9008 		goto done;
9009 
9010 	for (i = 0; i < NCCTRL_WIN; ++i) {
9011 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
9012 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
9013 		    incr[5][i], incr[6][i], incr[7][i]);
9014 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
9015 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
9016 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
9017 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
9018 	}
9019 
9020 	rc = sbuf_finish(sb);
9021 done:
9022 	sbuf_delete(sb);
9023 	return (rc);
9024 }
9025 
9026 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
9027 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
9028 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
9029 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
9030 };
9031 
9032 static int
sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)9033 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
9034 {
9035 	struct adapter *sc = arg1;
9036 	struct sbuf *sb;
9037 	int rc, i, n, qid = arg2;
9038 	uint32_t *buf, *p;
9039 	char *qtype;
9040 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
9041 
9042 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
9043 	    ("%s: bad qid %d\n", __func__, qid));
9044 
9045 	if (qid < CIM_NUM_IBQ) {
9046 		/* inbound queue */
9047 		qtype = "IBQ";
9048 		n = 4 * CIM_IBQ_SIZE;
9049 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9050 		mtx_lock(&sc->reg_lock);
9051 		if (hw_off_limits(sc))
9052 			rc = -ENXIO;
9053 		else
9054 			rc = t4_read_cim_ibq(sc, qid, buf, n);
9055 		mtx_unlock(&sc->reg_lock);
9056 	} else {
9057 		/* outbound queue */
9058 		qtype = "OBQ";
9059 		qid -= CIM_NUM_IBQ;
9060 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
9061 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9062 		mtx_lock(&sc->reg_lock);
9063 		if (hw_off_limits(sc))
9064 			rc = -ENXIO;
9065 		else
9066 			rc = t4_read_cim_obq(sc, qid, buf, n);
9067 		mtx_unlock(&sc->reg_lock);
9068 	}
9069 
9070 	if (rc < 0) {
9071 		rc = -rc;
9072 		goto done;
9073 	}
9074 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9075 
9076 	rc = sysctl_wire_old_buffer(req, 0);
9077 	if (rc != 0)
9078 		goto done;
9079 
9080 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9081 	if (sb == NULL) {
9082 		rc = ENOMEM;
9083 		goto done;
9084 	}
9085 
9086 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
9087 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9088 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9089 		    p[2], p[3]);
9090 
9091 	rc = sbuf_finish(sb);
9092 	sbuf_delete(sb);
9093 done:
9094 	free(buf, M_CXGBE);
9095 	return (rc);
9096 }
9097 
9098 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9099 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9100 {
9101 	uint32_t *p;
9102 
9103 	sbuf_printf(sb, "Status   Data      PC%s",
9104 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9105 	    "     LS0Stat  LS0Addr             LS0Data");
9106 
9107 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9108 		if (cfg & F_UPDBGLACAPTPCONLY) {
9109 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9110 			    p[6], p[7]);
9111 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9112 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9113 			    p[4] & 0xff, p[5] >> 8);
9114 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9115 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9116 			    p[1] & 0xf, p[2] >> 4);
9117 		} else {
9118 			sbuf_printf(sb,
9119 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9120 			    "%08x%08x%08x%08x",
9121 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9122 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9123 			    p[6], p[7]);
9124 		}
9125 	}
9126 }
9127 
9128 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9129 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9130 {
9131 	uint32_t *p;
9132 
9133 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9134 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9135 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9136 
9137 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9138 		if (cfg & F_UPDBGLACAPTPCONLY) {
9139 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9140 			    p[3] & 0xff, p[2], p[1], p[0]);
9141 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9142 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9143 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9144 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9145 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9146 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9147 			    p[6] >> 16);
9148 		} else {
9149 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9150 			    "%08x %08x %08x %08x %08x %08x",
9151 			    (p[9] >> 16) & 0xff,
9152 			    p[9] & 0xffff, p[8] >> 16,
9153 			    p[8] & 0xffff, p[7] >> 16,
9154 			    p[7] & 0xffff, p[6] >> 16,
9155 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9156 		}
9157 	}
9158 }
9159 
9160 static int
sbuf_cim_la(struct adapter * sc,struct sbuf * sb,int flags)9161 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
9162 {
9163 	uint32_t cfg, *buf;
9164 	int rc;
9165 
9166 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9167 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9168 	    M_ZERO | flags);
9169 	if (buf == NULL)
9170 		return (ENOMEM);
9171 
9172 	mtx_lock(&sc->reg_lock);
9173 	if (hw_off_limits(sc))
9174 		rc = ENXIO;
9175 	else {
9176 		rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
9177 		if (rc == 0)
9178 			rc = -t4_cim_read_la(sc, buf, NULL);
9179 	}
9180 	mtx_unlock(&sc->reg_lock);
9181 	if (rc == 0) {
9182 		if (chip_id(sc) < CHELSIO_T6)
9183 			sbuf_cim_la4(sc, sb, buf, cfg);
9184 		else
9185 			sbuf_cim_la6(sc, sb, buf, cfg);
9186 	}
9187 	free(buf, M_CXGBE);
9188 	return (rc);
9189 }
9190 
9191 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9192 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9193 {
9194 	struct adapter *sc = arg1;
9195 	struct sbuf *sb;
9196 	int rc;
9197 
9198 	rc = sysctl_wire_old_buffer(req, 0);
9199 	if (rc != 0)
9200 		return (rc);
9201 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9202 	if (sb == NULL)
9203 		return (ENOMEM);
9204 
9205 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
9206 	if (rc == 0)
9207 		rc = sbuf_finish(sb);
9208 	sbuf_delete(sb);
9209 	return (rc);
9210 }
9211 
9212 static void
dump_cim_regs(struct adapter * sc)9213 dump_cim_regs(struct adapter *sc)
9214 {
9215 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9216 	    device_get_nameunit(sc->dev),
9217 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9218 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9219 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9220 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9221 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9222 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9223 	    device_get_nameunit(sc->dev),
9224 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9225 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9226 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9227 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9228 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9229 }
9230 
9231 static void
dump_cimla(struct adapter * sc)9232 dump_cimla(struct adapter *sc)
9233 {
9234 	struct sbuf sb;
9235 	int rc;
9236 
9237 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9238 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9239 		    device_get_nameunit(sc->dev));
9240 		return;
9241 	}
9242 	rc = sbuf_cim_la(sc, &sb, M_WAITOK);
9243 	if (rc == 0) {
9244 		rc = sbuf_finish(&sb);
9245 		if (rc == 0) {
9246 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9247 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9248 		}
9249 	}
9250 	sbuf_delete(&sb);
9251 }
9252 
9253 void
t4_os_cim_err(struct adapter * sc)9254 t4_os_cim_err(struct adapter *sc)
9255 {
9256 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9257 }
9258 
9259 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9260 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9261 {
9262 	struct adapter *sc = arg1;
9263 	u_int i;
9264 	struct sbuf *sb;
9265 	uint32_t *buf, *p;
9266 	int rc;
9267 
9268 	rc = sysctl_wire_old_buffer(req, 0);
9269 	if (rc != 0)
9270 		return (rc);
9271 
9272 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9273 	if (sb == NULL)
9274 		return (ENOMEM);
9275 
9276 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9277 	    M_ZERO | M_WAITOK);
9278 
9279 	mtx_lock(&sc->reg_lock);
9280 	if (hw_off_limits(sc))
9281 		rc = ENXIO;
9282 	else
9283 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9284 	mtx_unlock(&sc->reg_lock);
9285 	if (rc)
9286 		goto done;
9287 
9288 	p = buf;
9289 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9290 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9291 		    p[1], p[0]);
9292 	}
9293 
9294 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9295 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9296 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9297 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9298 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9299 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9300 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9301 		    p[0] & 1);
9302 	}
9303 	rc = sbuf_finish(sb);
9304 done:
9305 	sbuf_delete(sb);
9306 	free(buf, M_CXGBE);
9307 	return (rc);
9308 }
9309 
9310 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9311 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9312 {
9313 	struct adapter *sc = arg1;
9314 	u_int i;
9315 	struct sbuf *sb;
9316 	uint32_t *buf, *p;
9317 	int rc;
9318 
9319 	rc = sysctl_wire_old_buffer(req, 0);
9320 	if (rc != 0)
9321 		return (rc);
9322 
9323 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9324 	if (sb == NULL)
9325 		return (ENOMEM);
9326 
9327 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9328 	    M_ZERO | M_WAITOK);
9329 
9330 	mtx_lock(&sc->reg_lock);
9331 	if (hw_off_limits(sc))
9332 		rc = ENXIO;
9333 	else
9334 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9335 	mtx_unlock(&sc->reg_lock);
9336 	if (rc)
9337 		goto done;
9338 
9339 	p = buf;
9340 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9341 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9342 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9343 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9344 		    p[4], p[3], p[2], p[1], p[0]);
9345 	}
9346 
9347 	sbuf_printf(sb, "\n\nCntl ID               Data");
9348 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9349 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9350 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9351 	}
9352 
9353 	rc = sbuf_finish(sb);
9354 done:
9355 	sbuf_delete(sb);
9356 	free(buf, M_CXGBE);
9357 	return (rc);
9358 }
9359 
9360 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9361 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9362 {
9363 	struct adapter *sc = arg1;
9364 	struct sbuf *sb;
9365 	int rc, i;
9366 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9367 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9368 	uint16_t thres[CIM_NUM_IBQ];
9369 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9370 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9371 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9372 
9373 	cim_num_obq = sc->chip_params->cim_num_obq;
9374 	if (is_t4(sc)) {
9375 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9376 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9377 	} else {
9378 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9379 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9380 	}
9381 	nq = CIM_NUM_IBQ + cim_num_obq;
9382 
9383 	mtx_lock(&sc->reg_lock);
9384 	if (hw_off_limits(sc))
9385 		rc = ENXIO;
9386 	else {
9387 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9388 		if (rc == 0) {
9389 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9390 			    obq_wr);
9391 			if (rc == 0)
9392 				t4_read_cimq_cfg(sc, base, size, thres);
9393 		}
9394 	}
9395 	mtx_unlock(&sc->reg_lock);
9396 	if (rc)
9397 		return (rc);
9398 
9399 	rc = sysctl_wire_old_buffer(req, 0);
9400 	if (rc != 0)
9401 		return (rc);
9402 
9403 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9404 	if (sb == NULL)
9405 		return (ENOMEM);
9406 
9407 	sbuf_printf(sb,
9408 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9409 
9410 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9411 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9412 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9413 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9414 		    G_QUEREMFLITS(p[2]) * 16);
9415 	for ( ; i < nq; i++, p += 4, wr += 2)
9416 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9417 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9418 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9419 		    G_QUEREMFLITS(p[2]) * 16);
9420 
9421 	rc = sbuf_finish(sb);
9422 	sbuf_delete(sb);
9423 
9424 	return (rc);
9425 }
9426 
9427 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9428 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9429 {
9430 	struct adapter *sc = arg1;
9431 	struct sbuf *sb;
9432 	int rc;
9433 	struct tp_cpl_stats stats;
9434 
9435 	rc = sysctl_wire_old_buffer(req, 0);
9436 	if (rc != 0)
9437 		return (rc);
9438 
9439 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9440 	if (sb == NULL)
9441 		return (ENOMEM);
9442 
9443 	mtx_lock(&sc->reg_lock);
9444 	if (hw_off_limits(sc))
9445 		rc = ENXIO;
9446 	else
9447 		t4_tp_get_cpl_stats(sc, &stats, 0);
9448 	mtx_unlock(&sc->reg_lock);
9449 	if (rc)
9450 		goto done;
9451 
9452 	if (sc->chip_params->nchan > 2) {
9453 		sbuf_printf(sb, "                 channel 0  channel 1"
9454 		    "  channel 2  channel 3");
9455 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9456 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9457 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9458 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9459 	} else {
9460 		sbuf_printf(sb, "                 channel 0  channel 1");
9461 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9462 		    stats.req[0], stats.req[1]);
9463 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9464 		    stats.rsp[0], stats.rsp[1]);
9465 	}
9466 
9467 	rc = sbuf_finish(sb);
9468 done:
9469 	sbuf_delete(sb);
9470 	return (rc);
9471 }
9472 
9473 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9474 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9475 {
9476 	struct adapter *sc = arg1;
9477 	struct sbuf *sb;
9478 	int rc;
9479 	struct tp_usm_stats stats;
9480 
9481 	rc = sysctl_wire_old_buffer(req, 0);
9482 	if (rc != 0)
9483 		return(rc);
9484 
9485 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9486 	if (sb == NULL)
9487 		return (ENOMEM);
9488 
9489 	mtx_lock(&sc->reg_lock);
9490 	if (hw_off_limits(sc))
9491 		rc = ENXIO;
9492 	else
9493 		t4_get_usm_stats(sc, &stats, 1);
9494 	mtx_unlock(&sc->reg_lock);
9495 	if (rc == 0) {
9496 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
9497 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
9498 		sbuf_printf(sb, "Drops:  %u", stats.drops);
9499 		rc = sbuf_finish(sb);
9500 	}
9501 	sbuf_delete(sb);
9502 
9503 	return (rc);
9504 }
9505 
9506 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)9507 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
9508 {
9509 	struct adapter *sc = arg1;
9510 	struct sbuf *sb;
9511 	int rc;
9512 	struct tp_tid_stats stats;
9513 
9514 	rc = sysctl_wire_old_buffer(req, 0);
9515 	if (rc != 0)
9516 		return(rc);
9517 
9518 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9519 	if (sb == NULL)
9520 		return (ENOMEM);
9521 
9522 	mtx_lock(&sc->reg_lock);
9523 	if (hw_off_limits(sc))
9524 		rc = ENXIO;
9525 	else
9526 		t4_tp_get_tid_stats(sc, &stats, 1);
9527 	mtx_unlock(&sc->reg_lock);
9528 	if (rc == 0) {
9529 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
9530 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
9531 		sbuf_printf(sb, "Active:     %u\n", stats.act);
9532 		sbuf_printf(sb, "Passive:    %u", stats.pas);
9533 		rc = sbuf_finish(sb);
9534 	}
9535 	sbuf_delete(sb);
9536 
9537 	return (rc);
9538 }
9539 
9540 static const char * const devlog_level_strings[] = {
9541 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
9542 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
9543 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
9544 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
9545 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
9546 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
9547 };
9548 
9549 static const char * const devlog_facility_strings[] = {
9550 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
9551 	[FW_DEVLOG_FACILITY_CF]		= "CF",
9552 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
9553 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
9554 	[FW_DEVLOG_FACILITY_RES]	= "RES",
9555 	[FW_DEVLOG_FACILITY_HW]		= "HW",
9556 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
9557 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
9558 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
9559 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
9560 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
9561 	[FW_DEVLOG_FACILITY_VI]		= "VI",
9562 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
9563 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
9564 	[FW_DEVLOG_FACILITY_TM]		= "TM",
9565 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
9566 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
9567 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
9568 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
9569 	[FW_DEVLOG_FACILITY_RI]		= "RI",
9570 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
9571 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
9572 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
9573 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
9574 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
9575 };
9576 
9577 static int
sbuf_devlog(struct adapter * sc,struct sbuf * sb,int flags)9578 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
9579 {
9580 	int i, j, rc, nentries, first = 0;
9581 	struct devlog_params *dparams = &sc->params.devlog;
9582 	struct fw_devlog_e *buf, *e;
9583 	uint64_t ftstamp = UINT64_MAX;
9584 
9585 	if (dparams->addr == 0)
9586 		return (ENXIO);
9587 
9588 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9589 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
9590 	if (buf == NULL)
9591 		return (ENOMEM);
9592 
9593 	mtx_lock(&sc->reg_lock);
9594 	if (hw_off_limits(sc))
9595 		rc = ENXIO;
9596 	else
9597 		rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf,
9598 		    dparams->size);
9599 	mtx_unlock(&sc->reg_lock);
9600 	if (rc != 0)
9601 		goto done;
9602 
9603 	nentries = dparams->size / sizeof(struct fw_devlog_e);
9604 	for (i = 0; i < nentries; i++) {
9605 		e = &buf[i];
9606 
9607 		if (e->timestamp == 0)
9608 			break;	/* end */
9609 
9610 		e->timestamp = be64toh(e->timestamp);
9611 		e->seqno = be32toh(e->seqno);
9612 		for (j = 0; j < 8; j++)
9613 			e->params[j] = be32toh(e->params[j]);
9614 
9615 		if (e->timestamp < ftstamp) {
9616 			ftstamp = e->timestamp;
9617 			first = i;
9618 		}
9619 	}
9620 
9621 	if (buf[first].timestamp == 0)
9622 		goto done;	/* nothing in the log */
9623 
9624 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
9625 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
9626 
9627 	i = first;
9628 	do {
9629 		e = &buf[i];
9630 		if (e->timestamp == 0)
9631 			break;	/* end */
9632 
9633 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
9634 		    e->seqno, e->timestamp,
9635 		    (e->level < nitems(devlog_level_strings) ?
9636 			devlog_level_strings[e->level] : "UNKNOWN"),
9637 		    (e->facility < nitems(devlog_facility_strings) ?
9638 			devlog_facility_strings[e->facility] : "UNKNOWN"));
9639 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
9640 		    e->params[2], e->params[3], e->params[4],
9641 		    e->params[5], e->params[6], e->params[7]);
9642 
9643 		if (++i == nentries)
9644 			i = 0;
9645 	} while (i != first);
9646 done:
9647 	free(buf, M_CXGBE);
9648 	return (rc);
9649 }
9650 
9651 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)9652 sysctl_devlog(SYSCTL_HANDLER_ARGS)
9653 {
9654 	struct adapter *sc = arg1;
9655 	int rc;
9656 	struct sbuf *sb;
9657 
9658 	rc = sysctl_wire_old_buffer(req, 0);
9659 	if (rc != 0)
9660 		return (rc);
9661 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9662 	if (sb == NULL)
9663 		return (ENOMEM);
9664 
9665 	rc = sbuf_devlog(sc, sb, M_WAITOK);
9666 	if (rc == 0)
9667 		rc = sbuf_finish(sb);
9668 	sbuf_delete(sb);
9669 	return (rc);
9670 }
9671 
9672 static void
dump_devlog(struct adapter * sc)9673 dump_devlog(struct adapter *sc)
9674 {
9675 	int rc;
9676 	struct sbuf sb;
9677 
9678 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9679 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
9680 		    device_get_nameunit(sc->dev));
9681 		return;
9682 	}
9683 	rc = sbuf_devlog(sc, &sb, M_WAITOK);
9684 	if (rc == 0) {
9685 		rc = sbuf_finish(&sb);
9686 		if (rc == 0) {
9687 			log(LOG_DEBUG, "%s: device log follows.\n%s",
9688 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9689 		}
9690 	}
9691 	sbuf_delete(&sb);
9692 }
9693 
9694 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)9695 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
9696 {
9697 	struct adapter *sc = arg1;
9698 	struct sbuf *sb;
9699 	int rc;
9700 	struct tp_fcoe_stats stats[MAX_NCHAN];
9701 	int i, nchan = sc->chip_params->nchan;
9702 
9703 	rc = sysctl_wire_old_buffer(req, 0);
9704 	if (rc != 0)
9705 		return (rc);
9706 
9707 	mtx_lock(&sc->reg_lock);
9708 	if (hw_off_limits(sc))
9709 		rc = ENXIO;
9710 	else {
9711 		for (i = 0; i < nchan; i++)
9712 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
9713 	}
9714 	mtx_unlock(&sc->reg_lock);
9715 	if (rc != 0)
9716 		return (rc);
9717 
9718 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9719 	if (sb == NULL)
9720 		return (ENOMEM);
9721 
9722 	if (nchan > 2) {
9723 		sbuf_printf(sb, "                   channel 0        channel 1"
9724 		    "        channel 2        channel 3");
9725 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
9726 		    stats[0].octets_ddp, stats[1].octets_ddp,
9727 		    stats[2].octets_ddp, stats[3].octets_ddp);
9728 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
9729 		    stats[0].frames_ddp, stats[1].frames_ddp,
9730 		    stats[2].frames_ddp, stats[3].frames_ddp);
9731 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
9732 		    stats[0].frames_drop, stats[1].frames_drop,
9733 		    stats[2].frames_drop, stats[3].frames_drop);
9734 	} else {
9735 		sbuf_printf(sb, "                   channel 0        channel 1");
9736 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
9737 		    stats[0].octets_ddp, stats[1].octets_ddp);
9738 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
9739 		    stats[0].frames_ddp, stats[1].frames_ddp);
9740 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
9741 		    stats[0].frames_drop, stats[1].frames_drop);
9742 	}
9743 
9744 	rc = sbuf_finish(sb);
9745 	sbuf_delete(sb);
9746 
9747 	return (rc);
9748 }
9749 
9750 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)9751 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
9752 {
9753 	struct adapter *sc = arg1;
9754 	struct sbuf *sb;
9755 	int rc, i;
9756 	unsigned int map, kbps, ipg, mode;
9757 	unsigned int pace_tab[NTX_SCHED];
9758 
9759 	rc = sysctl_wire_old_buffer(req, 0);
9760 	if (rc != 0)
9761 		return (rc);
9762 
9763 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
9764 	if (sb == NULL)
9765 		return (ENOMEM);
9766 
9767 	mtx_lock(&sc->reg_lock);
9768 	if (hw_off_limits(sc)) {
9769 		rc = ENXIO;
9770 		goto done;
9771 	}
9772 
9773 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
9774 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
9775 	t4_read_pace_tbl(sc, pace_tab);
9776 
9777 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
9778 	    "Class IPG (0.1 ns)   Flow IPG (us)");
9779 
9780 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
9781 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
9782 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
9783 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
9784 		if (kbps)
9785 			sbuf_printf(sb, "%9u     ", kbps);
9786 		else
9787 			sbuf_printf(sb, " disabled     ");
9788 
9789 		if (ipg)
9790 			sbuf_printf(sb, "%13u        ", ipg);
9791 		else
9792 			sbuf_printf(sb, "     disabled        ");
9793 
9794 		if (pace_tab[i])
9795 			sbuf_printf(sb, "%10u", pace_tab[i]);
9796 		else
9797 			sbuf_printf(sb, "  disabled");
9798 	}
9799 	rc = sbuf_finish(sb);
9800 done:
9801 	mtx_unlock(&sc->reg_lock);
9802 	sbuf_delete(sb);
9803 	return (rc);
9804 }
9805 
9806 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)9807 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
9808 {
9809 	struct adapter *sc = arg1;
9810 	struct sbuf *sb;
9811 	int rc, i, j;
9812 	uint64_t *p0, *p1;
9813 	struct lb_port_stats s[2];
9814 	static const char *stat_name[] = {
9815 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
9816 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
9817 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
9818 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
9819 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
9820 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
9821 		"BG2FramesTrunc:", "BG3FramesTrunc:"
9822 	};
9823 
9824 	rc = sysctl_wire_old_buffer(req, 0);
9825 	if (rc != 0)
9826 		return (rc);
9827 
9828 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9829 	if (sb == NULL)
9830 		return (ENOMEM);
9831 
9832 	memset(s, 0, sizeof(s));
9833 
9834 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
9835 		mtx_lock(&sc->reg_lock);
9836 		if (hw_off_limits(sc))
9837 			rc = ENXIO;
9838 		else {
9839 			t4_get_lb_stats(sc, i, &s[0]);
9840 			t4_get_lb_stats(sc, i + 1, &s[1]);
9841 		}
9842 		mtx_unlock(&sc->reg_lock);
9843 		if (rc != 0)
9844 			break;
9845 
9846 		p0 = &s[0].octets;
9847 		p1 = &s[1].octets;
9848 		sbuf_printf(sb, "%s                       Loopback %u"
9849 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
9850 
9851 		for (j = 0; j < nitems(stat_name); j++)
9852 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
9853 				   *p0++, *p1++);
9854 	}
9855 
9856 	rc = sbuf_finish(sb);
9857 	sbuf_delete(sb);
9858 
9859 	return (rc);
9860 }
9861 
9862 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)9863 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
9864 {
9865 	int rc = 0;
9866 	struct port_info *pi = arg1;
9867 	struct link_config *lc = &pi->link_cfg;
9868 	struct sbuf *sb;
9869 
9870 	rc = sysctl_wire_old_buffer(req, 0);
9871 	if (rc != 0)
9872 		return(rc);
9873 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
9874 	if (sb == NULL)
9875 		return (ENOMEM);
9876 
9877 	if (lc->link_ok || lc->link_down_rc == 255)
9878 		sbuf_printf(sb, "n/a");
9879 	else
9880 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
9881 
9882 	rc = sbuf_finish(sb);
9883 	sbuf_delete(sb);
9884 
9885 	return (rc);
9886 }
9887 
9888 struct mem_desc {
9889 	u_int base;
9890 	u_int limit;
9891 	u_int idx;
9892 };
9893 
9894 static int
mem_desc_cmp(const void * a,const void * b)9895 mem_desc_cmp(const void *a, const void *b)
9896 {
9897 	const u_int v1 = ((const struct mem_desc *)a)->base;
9898 	const u_int v2 = ((const struct mem_desc *)b)->base;
9899 
9900 	if (v1 < v2)
9901 		return (-1);
9902 	else if (v1 > v2)
9903 		return (1);
9904 
9905 	return (0);
9906 }
9907 
9908 static void
mem_region_show(struct sbuf * sb,const char * name,unsigned int from,unsigned int to)9909 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
9910     unsigned int to)
9911 {
9912 	unsigned int size;
9913 
9914 	if (from == to)
9915 		return;
9916 
9917 	size = to - from + 1;
9918 	if (size == 0)
9919 		return;
9920 
9921 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
9922 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
9923 }
9924 
9925 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)9926 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
9927 {
9928 	struct adapter *sc = arg1;
9929 	struct sbuf *sb;
9930 	int rc, i, n;
9931 	uint32_t lo, hi, used, free, alloc;
9932 	static const char *memory[] = {
9933 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
9934 	};
9935 	static const char *region[] = {
9936 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
9937 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
9938 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
9939 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
9940 		"RQUDP region:", "PBL region:", "TXPBL region:",
9941 		"TLSKey region:", "DBVFIFO region:", "ULPRX state:",
9942 		"ULPTX state:", "On-chip queues:",
9943 	};
9944 	struct mem_desc avail[4];
9945 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
9946 	struct mem_desc *md = mem;
9947 
9948 	rc = sysctl_wire_old_buffer(req, 0);
9949 	if (rc != 0)
9950 		return (rc);
9951 
9952 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9953 	if (sb == NULL)
9954 		return (ENOMEM);
9955 
9956 	for (i = 0; i < nitems(mem); i++) {
9957 		mem[i].limit = 0;
9958 		mem[i].idx = i;
9959 	}
9960 
9961 	mtx_lock(&sc->reg_lock);
9962 	if (hw_off_limits(sc)) {
9963 		rc = ENXIO;
9964 		goto done;
9965 	}
9966 
9967 	/* Find and sort the populated memory ranges */
9968 	i = 0;
9969 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
9970 	if (lo & F_EDRAM0_ENABLE) {
9971 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
9972 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
9973 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
9974 		avail[i].idx = 0;
9975 		i++;
9976 	}
9977 	if (lo & F_EDRAM1_ENABLE) {
9978 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
9979 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
9980 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
9981 		avail[i].idx = 1;
9982 		i++;
9983 	}
9984 	if (lo & F_EXT_MEM_ENABLE) {
9985 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
9986 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
9987 		avail[i].limit = avail[i].base + (G_EXT_MEM_SIZE(hi) << 20);
9988 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
9989 		i++;
9990 	}
9991 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
9992 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9993 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9994 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9995 		avail[i].idx = 4;
9996 		i++;
9997 	}
9998 	if (is_t6(sc) && lo & F_HMA_MUX) {
9999 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10000 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
10001 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
10002 		avail[i].idx = 5;
10003 		i++;
10004 	}
10005 	MPASS(i <= nitems(avail));
10006 	if (!i)                                    /* no memory available */
10007 		goto done;
10008 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
10009 
10010 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
10011 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
10012 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
10013 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
10014 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
10015 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
10016 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
10017 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
10018 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
10019 
10020 	/* the next few have explicit upper bounds */
10021 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
10022 	md->limit = md->base - 1 +
10023 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
10024 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
10025 	md++;
10026 
10027 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
10028 	md->limit = md->base - 1 +
10029 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
10030 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
10031 	md++;
10032 
10033 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10034 		if (chip_id(sc) <= CHELSIO_T5)
10035 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
10036 		else
10037 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
10038 		md->limit = 0;
10039 	} else {
10040 		md->base = 0;
10041 		md->idx = nitems(region);  /* hide it */
10042 	}
10043 	md++;
10044 
10045 #define ulp_region(reg) \
10046 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
10047 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
10048 
10049 	ulp_region(RX_ISCSI);
10050 	ulp_region(RX_TDDP);
10051 	ulp_region(TX_TPT);
10052 	ulp_region(RX_STAG);
10053 	ulp_region(RX_RQ);
10054 	ulp_region(RX_RQUDP);
10055 	ulp_region(RX_PBL);
10056 	ulp_region(TX_PBL);
10057 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
10058 		ulp_region(RX_TLS_KEY);
10059 	}
10060 #undef ulp_region
10061 
10062 	md->base = 0;
10063 	if (is_t4(sc))
10064 		md->idx = nitems(region);
10065 	else {
10066 		uint32_t size = 0;
10067 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
10068 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
10069 
10070 		if (is_t5(sc)) {
10071 			if (sge_ctrl & F_VFIFO_ENABLE)
10072 				size = fifo_size << 2;
10073 		} else
10074 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
10075 
10076 		if (size) {
10077 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
10078 			md->limit = md->base + size - 1;
10079 		} else
10080 			md->idx = nitems(region);
10081 	}
10082 	md++;
10083 
10084 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
10085 	md->limit = 0;
10086 	md++;
10087 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
10088 	md->limit = 0;
10089 	md++;
10090 
10091 	md->base = sc->vres.ocq.start;
10092 	if (sc->vres.ocq.size)
10093 		md->limit = md->base + sc->vres.ocq.size - 1;
10094 	else
10095 		md->idx = nitems(region);  /* hide it */
10096 	md++;
10097 
10098 	/* add any address-space holes, there can be up to 3 */
10099 	for (n = 0; n < i - 1; n++)
10100 		if (avail[n].limit < avail[n + 1].base)
10101 			(md++)->base = avail[n].limit;
10102 	if (avail[n].limit)
10103 		(md++)->base = avail[n].limit;
10104 
10105 	n = md - mem;
10106 	MPASS(n <= nitems(mem));
10107 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10108 
10109 	for (lo = 0; lo < i; lo++)
10110 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10111 				avail[lo].limit - 1);
10112 
10113 	sbuf_printf(sb, "\n");
10114 	for (i = 0; i < n; i++) {
10115 		if (mem[i].idx >= nitems(region))
10116 			continue;                        /* skip holes */
10117 		if (!mem[i].limit)
10118 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10119 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10120 				mem[i].limit);
10121 	}
10122 
10123 	sbuf_printf(sb, "\n");
10124 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10125 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10126 	mem_region_show(sb, "uP RAM:", lo, hi);
10127 
10128 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10129 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10130 	mem_region_show(sb, "uP Extmem2:", lo, hi);
10131 
10132 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10133 	for (i = 0, free = 0; i < 2; i++)
10134 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10135 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10136 		   G_PMRXMAXPAGE(lo), free,
10137 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
10138 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
10139 
10140 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10141 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10142 	for (i = 0, free = 0; i < 4; i++)
10143 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10144 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10145 		   G_PMTXMAXPAGE(lo), free,
10146 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10147 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
10148 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10149 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10150 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10151 
10152 	for (i = 0; i < 4; i++) {
10153 		if (chip_id(sc) > CHELSIO_T5)
10154 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10155 		else
10156 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10157 		if (is_t5(sc)) {
10158 			used = G_T5_USED(lo);
10159 			alloc = G_T5_ALLOC(lo);
10160 		} else {
10161 			used = G_USED(lo);
10162 			alloc = G_ALLOC(lo);
10163 		}
10164 		/* For T6 these are MAC buffer groups */
10165 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10166 		    i, used, alloc);
10167 	}
10168 	for (i = 0; i < sc->chip_params->nchan; i++) {
10169 		if (chip_id(sc) > CHELSIO_T5)
10170 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10171 		else
10172 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10173 		if (is_t5(sc)) {
10174 			used = G_T5_USED(lo);
10175 			alloc = G_T5_ALLOC(lo);
10176 		} else {
10177 			used = G_USED(lo);
10178 			alloc = G_ALLOC(lo);
10179 		}
10180 		/* For T6 these are MAC buffer groups */
10181 		sbuf_printf(sb,
10182 		    "\nLoopback %d using %u pages out of %u allocated",
10183 		    i, used, alloc);
10184 	}
10185 done:
10186 	mtx_unlock(&sc->reg_lock);
10187 	if (rc == 0)
10188 		rc = sbuf_finish(sb);
10189 	sbuf_delete(sb);
10190 	return (rc);
10191 }
10192 
10193 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10194 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10195 {
10196 	*mask = x | y;
10197 	y = htobe64(y);
10198 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10199 }
10200 
10201 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10202 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10203 {
10204 	struct adapter *sc = arg1;
10205 	struct sbuf *sb;
10206 	int rc, i;
10207 
10208 	MPASS(chip_id(sc) <= CHELSIO_T5);
10209 
10210 	rc = sysctl_wire_old_buffer(req, 0);
10211 	if (rc != 0)
10212 		return (rc);
10213 
10214 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10215 	if (sb == NULL)
10216 		return (ENOMEM);
10217 
10218 	sbuf_printf(sb,
10219 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10220 	    "  VF              Replication             P0 P1 P2 P3  ML");
10221 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10222 		uint64_t tcamx, tcamy, mask;
10223 		uint32_t cls_lo, cls_hi;
10224 		uint8_t addr[ETHER_ADDR_LEN];
10225 
10226 		mtx_lock(&sc->reg_lock);
10227 		if (hw_off_limits(sc))
10228 			rc = ENXIO;
10229 		else {
10230 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10231 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10232 		}
10233 		mtx_unlock(&sc->reg_lock);
10234 		if (rc != 0)
10235 			break;
10236 		if (tcamx & tcamy)
10237 			continue;
10238 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10239 		mtx_lock(&sc->reg_lock);
10240 		if (hw_off_limits(sc))
10241 			rc = ENXIO;
10242 		else {
10243 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10244 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10245 		}
10246 		mtx_unlock(&sc->reg_lock);
10247 		if (rc != 0)
10248 			break;
10249 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10250 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10251 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10252 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10253 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10254 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10255 
10256 		if (cls_lo & F_REPLICATE) {
10257 			struct fw_ldst_cmd ldst_cmd;
10258 
10259 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10260 			ldst_cmd.op_to_addrspace =
10261 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10262 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10263 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10264 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10265 			ldst_cmd.u.mps.rplc.fid_idx =
10266 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10267 				V_FW_LDST_CMD_IDX(i));
10268 
10269 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10270 			    "t4mps");
10271 			if (rc)
10272 				break;
10273 			if (hw_off_limits(sc))
10274 				rc = ENXIO;
10275 			else
10276 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10277 				    sizeof(ldst_cmd), &ldst_cmd);
10278 			end_synchronized_op(sc, 0);
10279 			if (rc != 0)
10280 				break;
10281 			else {
10282 				sbuf_printf(sb, " %08x %08x %08x %08x",
10283 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10284 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10285 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10286 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10287 			}
10288 		} else
10289 			sbuf_printf(sb, "%36s", "");
10290 
10291 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10292 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10293 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10294 	}
10295 
10296 	if (rc)
10297 		(void) sbuf_finish(sb);
10298 	else
10299 		rc = sbuf_finish(sb);
10300 	sbuf_delete(sb);
10301 
10302 	return (rc);
10303 }
10304 
10305 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10306 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10307 {
10308 	struct adapter *sc = arg1;
10309 	struct sbuf *sb;
10310 	int rc, i;
10311 
10312 	MPASS(chip_id(sc) > CHELSIO_T5);
10313 
10314 	rc = sysctl_wire_old_buffer(req, 0);
10315 	if (rc != 0)
10316 		return (rc);
10317 
10318 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10319 	if (sb == NULL)
10320 		return (ENOMEM);
10321 
10322 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10323 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10324 	    "                           Replication"
10325 	    "                                    P0 P1 P2 P3  ML\n");
10326 
10327 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10328 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10329 		uint16_t ivlan;
10330 		uint64_t tcamx, tcamy, val, mask;
10331 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10332 		uint8_t addr[ETHER_ADDR_LEN];
10333 
10334 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10335 		if (i < 256)
10336 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10337 		else
10338 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10339 		mtx_lock(&sc->reg_lock);
10340 		if (hw_off_limits(sc))
10341 			rc = ENXIO;
10342 		else {
10343 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10344 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10345 			tcamy = G_DMACH(val) << 32;
10346 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10347 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10348 		}
10349 		mtx_unlock(&sc->reg_lock);
10350 		if (rc != 0)
10351 			break;
10352 
10353 		lookup_type = G_DATALKPTYPE(data2);
10354 		port_num = G_DATAPORTNUM(data2);
10355 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10356 			/* Inner header VNI */
10357 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10358 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10359 			dip_hit = data2 & F_DATADIPHIT;
10360 			vlan_vld = 0;
10361 		} else {
10362 			vniy = 0;
10363 			dip_hit = 0;
10364 			vlan_vld = data2 & F_DATAVIDH2;
10365 			ivlan = G_VIDL(val);
10366 		}
10367 
10368 		ctl |= V_CTLXYBITSEL(1);
10369 		mtx_lock(&sc->reg_lock);
10370 		if (hw_off_limits(sc))
10371 			rc = ENXIO;
10372 		else {
10373 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10374 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10375 			tcamx = G_DMACH(val) << 32;
10376 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10377 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10378 		}
10379 		mtx_unlock(&sc->reg_lock);
10380 		if (rc != 0)
10381 			break;
10382 
10383 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10384 			/* Inner header VNI mask */
10385 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10386 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10387 		} else
10388 			vnix = 0;
10389 
10390 		if (tcamx & tcamy)
10391 			continue;
10392 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10393 
10394 		mtx_lock(&sc->reg_lock);
10395 		if (hw_off_limits(sc))
10396 			rc = ENXIO;
10397 		else {
10398 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10399 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10400 		}
10401 		mtx_unlock(&sc->reg_lock);
10402 		if (rc != 0)
10403 			break;
10404 
10405 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10406 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10407 			    "%012jx %06x %06x    -    -   %3c"
10408 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
10409 			    addr[1], addr[2], addr[3], addr[4], addr[5],
10410 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
10411 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10412 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10413 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10414 		} else {
10415 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10416 			    "%012jx    -       -   ", i, addr[0], addr[1],
10417 			    addr[2], addr[3], addr[4], addr[5],
10418 			    (uintmax_t)mask);
10419 
10420 			if (vlan_vld)
10421 				sbuf_printf(sb, "%4u   Y     ", ivlan);
10422 			else
10423 				sbuf_printf(sb, "  -    N     ");
10424 
10425 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
10426 			    lookup_type ? 'I' : 'O', port_num,
10427 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10428 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10429 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10430 		}
10431 
10432 
10433 		if (cls_lo & F_T6_REPLICATE) {
10434 			struct fw_ldst_cmd ldst_cmd;
10435 
10436 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10437 			ldst_cmd.op_to_addrspace =
10438 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10439 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10440 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10441 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10442 			ldst_cmd.u.mps.rplc.fid_idx =
10443 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10444 				V_FW_LDST_CMD_IDX(i));
10445 
10446 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10447 			    "t6mps");
10448 			if (rc)
10449 				break;
10450 			if (hw_off_limits(sc))
10451 				rc = ENXIO;
10452 			else
10453 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10454 				    sizeof(ldst_cmd), &ldst_cmd);
10455 			end_synchronized_op(sc, 0);
10456 			if (rc != 0)
10457 				break;
10458 			else {
10459 				sbuf_printf(sb, " %08x %08x %08x %08x"
10460 				    " %08x %08x %08x %08x",
10461 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
10462 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
10463 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
10464 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
10465 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10466 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10467 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10468 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10469 			}
10470 		} else
10471 			sbuf_printf(sb, "%72s", "");
10472 
10473 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
10474 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
10475 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
10476 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
10477 	}
10478 
10479 	if (rc)
10480 		(void) sbuf_finish(sb);
10481 	else
10482 		rc = sbuf_finish(sb);
10483 	sbuf_delete(sb);
10484 
10485 	return (rc);
10486 }
10487 
10488 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)10489 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
10490 {
10491 	struct adapter *sc = arg1;
10492 	struct sbuf *sb;
10493 	int rc;
10494 	uint16_t mtus[NMTUS];
10495 
10496 	rc = sysctl_wire_old_buffer(req, 0);
10497 	if (rc != 0)
10498 		return (rc);
10499 
10500 	mtx_lock(&sc->reg_lock);
10501 	if (hw_off_limits(sc))
10502 		rc = ENXIO;
10503 	else
10504 		t4_read_mtu_tbl(sc, mtus, NULL);
10505 	mtx_unlock(&sc->reg_lock);
10506 	if (rc != 0)
10507 		return (rc);
10508 
10509 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10510 	if (sb == NULL)
10511 		return (ENOMEM);
10512 
10513 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
10514 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
10515 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
10516 	    mtus[14], mtus[15]);
10517 
10518 	rc = sbuf_finish(sb);
10519 	sbuf_delete(sb);
10520 
10521 	return (rc);
10522 }
10523 
10524 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)10525 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
10526 {
10527 	struct adapter *sc = arg1;
10528 	struct sbuf *sb;
10529 	int rc, i;
10530 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
10531 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
10532 	static const char *tx_stats[MAX_PM_NSTATS] = {
10533 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
10534 		"Tx FIFO wait", NULL, "Tx latency"
10535 	};
10536 	static const char *rx_stats[MAX_PM_NSTATS] = {
10537 		"Read:", "Write bypass:", "Write mem:", "Flush:",
10538 		"Rx FIFO wait", NULL, "Rx latency"
10539 	};
10540 
10541 	rc = sysctl_wire_old_buffer(req, 0);
10542 	if (rc != 0)
10543 		return (rc);
10544 
10545 	mtx_lock(&sc->reg_lock);
10546 	if (hw_off_limits(sc))
10547 		rc = ENXIO;
10548 	else {
10549 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
10550 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
10551 	}
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, "                Tx pcmds             Tx bytes");
10561 	for (i = 0; i < 4; i++) {
10562 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10563 		    tx_cyc[i]);
10564 	}
10565 
10566 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
10567 	for (i = 0; i < 4; i++) {
10568 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10569 		    rx_cyc[i]);
10570 	}
10571 
10572 	if (chip_id(sc) > CHELSIO_T5) {
10573 		sbuf_printf(sb,
10574 		    "\n              Total wait      Total occupancy");
10575 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10576 		    tx_cyc[i]);
10577 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10578 		    rx_cyc[i]);
10579 
10580 		i += 2;
10581 		MPASS(i < nitems(tx_stats));
10582 
10583 		sbuf_printf(sb,
10584 		    "\n                   Reads           Total wait");
10585 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10586 		    tx_cyc[i]);
10587 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10588 		    rx_cyc[i]);
10589 	}
10590 
10591 	rc = sbuf_finish(sb);
10592 	sbuf_delete(sb);
10593 
10594 	return (rc);
10595 }
10596 
10597 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)10598 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
10599 {
10600 	struct adapter *sc = arg1;
10601 	struct sbuf *sb;
10602 	int rc;
10603 	struct tp_rdma_stats stats;
10604 
10605 	rc = sysctl_wire_old_buffer(req, 0);
10606 	if (rc != 0)
10607 		return (rc);
10608 
10609 	mtx_lock(&sc->reg_lock);
10610 	if (hw_off_limits(sc))
10611 		rc = ENXIO;
10612 	else
10613 		t4_tp_get_rdma_stats(sc, &stats, 0);
10614 	mtx_unlock(&sc->reg_lock);
10615 	if (rc != 0)
10616 		return (rc);
10617 
10618 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10619 	if (sb == NULL)
10620 		return (ENOMEM);
10621 
10622 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
10623 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
10624 
10625 	rc = sbuf_finish(sb);
10626 	sbuf_delete(sb);
10627 
10628 	return (rc);
10629 }
10630 
10631 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)10632 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
10633 {
10634 	struct adapter *sc = arg1;
10635 	struct sbuf *sb;
10636 	int rc;
10637 	struct tp_tcp_stats v4, v6;
10638 
10639 	rc = sysctl_wire_old_buffer(req, 0);
10640 	if (rc != 0)
10641 		return (rc);
10642 
10643 	mtx_lock(&sc->reg_lock);
10644 	if (hw_off_limits(sc))
10645 		rc = ENXIO;
10646 	else
10647 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
10648 	mtx_unlock(&sc->reg_lock);
10649 	if (rc != 0)
10650 		return (rc);
10651 
10652 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10653 	if (sb == NULL)
10654 		return (ENOMEM);
10655 
10656 	sbuf_printf(sb,
10657 	    "                                IP                 IPv6\n");
10658 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
10659 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
10660 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
10661 	    v4.tcp_in_segs, v6.tcp_in_segs);
10662 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
10663 	    v4.tcp_out_segs, v6.tcp_out_segs);
10664 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
10665 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
10666 
10667 	rc = sbuf_finish(sb);
10668 	sbuf_delete(sb);
10669 
10670 	return (rc);
10671 }
10672 
10673 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)10674 sysctl_tids(SYSCTL_HANDLER_ARGS)
10675 {
10676 	struct adapter *sc = arg1;
10677 	struct sbuf *sb;
10678 	int rc;
10679 	uint32_t x, y;
10680 	struct tid_info *t = &sc->tids;
10681 
10682 	rc = sysctl_wire_old_buffer(req, 0);
10683 	if (rc != 0)
10684 		return (rc);
10685 
10686 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10687 	if (sb == NULL)
10688 		return (ENOMEM);
10689 
10690 	if (t->natids) {
10691 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
10692 		    t->atids_in_use);
10693 	}
10694 
10695 	if (t->nhpftids) {
10696 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
10697 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
10698 	}
10699 
10700 	if (t->ntids) {
10701 		bool hashen = false;
10702 
10703 		mtx_lock(&sc->reg_lock);
10704 		if (hw_off_limits(sc))
10705 			rc = ENXIO;
10706 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10707 			hashen = true;
10708 			if (chip_id(sc) <= CHELSIO_T5) {
10709 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
10710 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
10711 			} else {
10712 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
10713 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
10714 			}
10715 		}
10716 		mtx_unlock(&sc->reg_lock);
10717 		if (rc != 0)
10718 			goto done;
10719 
10720 		sbuf_printf(sb, "TID range: ");
10721 		if (hashen) {
10722 			if (x)
10723 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
10724 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
10725 		} else {
10726 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
10727 			    t->ntids - 1);
10728 		}
10729 		sbuf_printf(sb, ", in use: %u\n",
10730 		    atomic_load_acq_int(&t->tids_in_use));
10731 	}
10732 
10733 	if (t->nstids) {
10734 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
10735 		    t->stid_base + t->nstids - 1, t->stids_in_use);
10736 	}
10737 
10738 	if (t->nftids) {
10739 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
10740 		    t->ftid_end, t->ftids_in_use);
10741 	}
10742 
10743 	if (t->netids) {
10744 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
10745 		    t->etid_base + t->netids - 1, t->etids_in_use);
10746 	}
10747 
10748 	mtx_lock(&sc->reg_lock);
10749 	if (hw_off_limits(sc))
10750 		rc = ENXIO;
10751 	else {
10752 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
10753 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
10754 	}
10755 	mtx_unlock(&sc->reg_lock);
10756 	if (rc != 0)
10757 		goto done;
10758 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
10759 done:
10760 	if (rc == 0)
10761 		rc = sbuf_finish(sb);
10762 	else
10763 		(void)sbuf_finish(sb);
10764 	sbuf_delete(sb);
10765 
10766 	return (rc);
10767 }
10768 
10769 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)10770 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
10771 {
10772 	struct adapter *sc = arg1;
10773 	struct sbuf *sb;
10774 	int rc;
10775 	struct tp_err_stats stats;
10776 
10777 	rc = sysctl_wire_old_buffer(req, 0);
10778 	if (rc != 0)
10779 		return (rc);
10780 
10781 	mtx_lock(&sc->reg_lock);
10782 	if (hw_off_limits(sc))
10783 		rc = ENXIO;
10784 	else
10785 		t4_tp_get_err_stats(sc, &stats, 0);
10786 	mtx_unlock(&sc->reg_lock);
10787 	if (rc != 0)
10788 		return (rc);
10789 
10790 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10791 	if (sb == NULL)
10792 		return (ENOMEM);
10793 
10794 	if (sc->chip_params->nchan > 2) {
10795 		sbuf_printf(sb, "                 channel 0  channel 1"
10796 		    "  channel 2  channel 3\n");
10797 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
10798 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
10799 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
10800 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
10801 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
10802 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
10803 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
10804 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
10805 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
10806 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
10807 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
10808 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
10809 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
10810 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
10811 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
10812 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
10813 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
10814 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
10815 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
10816 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
10817 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
10818 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
10819 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
10820 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
10821 	} else {
10822 		sbuf_printf(sb, "                 channel 0  channel 1\n");
10823 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
10824 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
10825 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
10826 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
10827 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
10828 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
10829 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
10830 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
10831 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
10832 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
10833 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
10834 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
10835 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
10836 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
10837 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
10838 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
10839 	}
10840 
10841 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
10842 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
10843 
10844 	rc = sbuf_finish(sb);
10845 	sbuf_delete(sb);
10846 
10847 	return (rc);
10848 }
10849 
10850 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)10851 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
10852 {
10853 	struct adapter *sc = arg1;
10854 	struct sbuf *sb;
10855 	int rc;
10856 	struct tp_tnl_stats stats;
10857 
10858 	rc = sysctl_wire_old_buffer(req, 0);
10859 	if (rc != 0)
10860 		return(rc);
10861 
10862 	mtx_lock(&sc->reg_lock);
10863 	if (hw_off_limits(sc))
10864 		rc = ENXIO;
10865 	else
10866 		t4_tp_get_tnl_stats(sc, &stats, 1);
10867 	mtx_unlock(&sc->reg_lock);
10868 	if (rc != 0)
10869 		return (rc);
10870 
10871 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10872 	if (sb == NULL)
10873 		return (ENOMEM);
10874 
10875 	if (sc->chip_params->nchan > 2) {
10876 		sbuf_printf(sb, "           channel 0  channel 1"
10877 		    "  channel 2  channel 3\n");
10878 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
10879 		    stats.out_pkt[0], stats.out_pkt[1],
10880 		    stats.out_pkt[2], stats.out_pkt[3]);
10881 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
10882 		    stats.in_pkt[0], stats.in_pkt[1],
10883 		    stats.in_pkt[2], stats.in_pkt[3]);
10884 	} else {
10885 		sbuf_printf(sb, "           channel 0  channel 1\n");
10886 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
10887 		    stats.out_pkt[0], stats.out_pkt[1]);
10888 		sbuf_printf(sb, "InPkts:   %10u %10u",
10889 		    stats.in_pkt[0], stats.in_pkt[1]);
10890 	}
10891 
10892 	rc = sbuf_finish(sb);
10893 	sbuf_delete(sb);
10894 
10895 	return (rc);
10896 }
10897 
10898 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)10899 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
10900 {
10901 	struct adapter *sc = arg1;
10902 	struct tp_params *tpp = &sc->params.tp;
10903 	u_int mask;
10904 	int rc;
10905 
10906 	mask = tpp->la_mask >> 16;
10907 	rc = sysctl_handle_int(oidp, &mask, 0, req);
10908 	if (rc != 0 || req->newptr == NULL)
10909 		return (rc);
10910 	if (mask > 0xffff)
10911 		return (EINVAL);
10912 	mtx_lock(&sc->reg_lock);
10913 	if (hw_off_limits(sc))
10914 		rc = ENXIO;
10915 	else {
10916 		tpp->la_mask = mask << 16;
10917 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
10918 		    tpp->la_mask);
10919 	}
10920 	mtx_unlock(&sc->reg_lock);
10921 
10922 	return (rc);
10923 }
10924 
10925 struct field_desc {
10926 	const char *name;
10927 	u_int start;
10928 	u_int width;
10929 };
10930 
10931 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)10932 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
10933 {
10934 	char buf[32];
10935 	int line_size = 0;
10936 
10937 	while (f->name) {
10938 		uint64_t mask = (1ULL << f->width) - 1;
10939 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
10940 		    ((uintmax_t)v >> f->start) & mask);
10941 
10942 		if (line_size + len >= 79) {
10943 			line_size = 8;
10944 			sbuf_printf(sb, "\n        ");
10945 		}
10946 		sbuf_printf(sb, "%s ", buf);
10947 		line_size += len + 1;
10948 		f++;
10949 	}
10950 	sbuf_printf(sb, "\n");
10951 }
10952 
10953 static const struct field_desc tp_la0[] = {
10954 	{ "RcfOpCodeOut", 60, 4 },
10955 	{ "State", 56, 4 },
10956 	{ "WcfState", 52, 4 },
10957 	{ "RcfOpcSrcOut", 50, 2 },
10958 	{ "CRxError", 49, 1 },
10959 	{ "ERxError", 48, 1 },
10960 	{ "SanityFailed", 47, 1 },
10961 	{ "SpuriousMsg", 46, 1 },
10962 	{ "FlushInputMsg", 45, 1 },
10963 	{ "FlushInputCpl", 44, 1 },
10964 	{ "RssUpBit", 43, 1 },
10965 	{ "RssFilterHit", 42, 1 },
10966 	{ "Tid", 32, 10 },
10967 	{ "InitTcb", 31, 1 },
10968 	{ "LineNumber", 24, 7 },
10969 	{ "Emsg", 23, 1 },
10970 	{ "EdataOut", 22, 1 },
10971 	{ "Cmsg", 21, 1 },
10972 	{ "CdataOut", 20, 1 },
10973 	{ "EreadPdu", 19, 1 },
10974 	{ "CreadPdu", 18, 1 },
10975 	{ "TunnelPkt", 17, 1 },
10976 	{ "RcfPeerFin", 16, 1 },
10977 	{ "RcfReasonOut", 12, 4 },
10978 	{ "TxCchannel", 10, 2 },
10979 	{ "RcfTxChannel", 8, 2 },
10980 	{ "RxEchannel", 6, 2 },
10981 	{ "RcfRxChannel", 5, 1 },
10982 	{ "RcfDataOutSrdy", 4, 1 },
10983 	{ "RxDvld", 3, 1 },
10984 	{ "RxOoDvld", 2, 1 },
10985 	{ "RxCongestion", 1, 1 },
10986 	{ "TxCongestion", 0, 1 },
10987 	{ NULL }
10988 };
10989 
10990 static const struct field_desc tp_la1[] = {
10991 	{ "CplCmdIn", 56, 8 },
10992 	{ "CplCmdOut", 48, 8 },
10993 	{ "ESynOut", 47, 1 },
10994 	{ "EAckOut", 46, 1 },
10995 	{ "EFinOut", 45, 1 },
10996 	{ "ERstOut", 44, 1 },
10997 	{ "SynIn", 43, 1 },
10998 	{ "AckIn", 42, 1 },
10999 	{ "FinIn", 41, 1 },
11000 	{ "RstIn", 40, 1 },
11001 	{ "DataIn", 39, 1 },
11002 	{ "DataInVld", 38, 1 },
11003 	{ "PadIn", 37, 1 },
11004 	{ "RxBufEmpty", 36, 1 },
11005 	{ "RxDdp", 35, 1 },
11006 	{ "RxFbCongestion", 34, 1 },
11007 	{ "TxFbCongestion", 33, 1 },
11008 	{ "TxPktSumSrdy", 32, 1 },
11009 	{ "RcfUlpType", 28, 4 },
11010 	{ "Eread", 27, 1 },
11011 	{ "Ebypass", 26, 1 },
11012 	{ "Esave", 25, 1 },
11013 	{ "Static0", 24, 1 },
11014 	{ "Cread", 23, 1 },
11015 	{ "Cbypass", 22, 1 },
11016 	{ "Csave", 21, 1 },
11017 	{ "CPktOut", 20, 1 },
11018 	{ "RxPagePoolFull", 18, 2 },
11019 	{ "RxLpbkPkt", 17, 1 },
11020 	{ "TxLpbkPkt", 16, 1 },
11021 	{ "RxVfValid", 15, 1 },
11022 	{ "SynLearned", 14, 1 },
11023 	{ "SetDelEntry", 13, 1 },
11024 	{ "SetInvEntry", 12, 1 },
11025 	{ "CpcmdDvld", 11, 1 },
11026 	{ "CpcmdSave", 10, 1 },
11027 	{ "RxPstructsFull", 8, 2 },
11028 	{ "EpcmdDvld", 7, 1 },
11029 	{ "EpcmdFlush", 6, 1 },
11030 	{ "EpcmdTrimPrefix", 5, 1 },
11031 	{ "EpcmdTrimPostfix", 4, 1 },
11032 	{ "ERssIp4Pkt", 3, 1 },
11033 	{ "ERssIp6Pkt", 2, 1 },
11034 	{ "ERssTcpUdpPkt", 1, 1 },
11035 	{ "ERssFceFipPkt", 0, 1 },
11036 	{ NULL }
11037 };
11038 
11039 static const struct field_desc tp_la2[] = {
11040 	{ "CplCmdIn", 56, 8 },
11041 	{ "MpsVfVld", 55, 1 },
11042 	{ "MpsPf", 52, 3 },
11043 	{ "MpsVf", 44, 8 },
11044 	{ "SynIn", 43, 1 },
11045 	{ "AckIn", 42, 1 },
11046 	{ "FinIn", 41, 1 },
11047 	{ "RstIn", 40, 1 },
11048 	{ "DataIn", 39, 1 },
11049 	{ "DataInVld", 38, 1 },
11050 	{ "PadIn", 37, 1 },
11051 	{ "RxBufEmpty", 36, 1 },
11052 	{ "RxDdp", 35, 1 },
11053 	{ "RxFbCongestion", 34, 1 },
11054 	{ "TxFbCongestion", 33, 1 },
11055 	{ "TxPktSumSrdy", 32, 1 },
11056 	{ "RcfUlpType", 28, 4 },
11057 	{ "Eread", 27, 1 },
11058 	{ "Ebypass", 26, 1 },
11059 	{ "Esave", 25, 1 },
11060 	{ "Static0", 24, 1 },
11061 	{ "Cread", 23, 1 },
11062 	{ "Cbypass", 22, 1 },
11063 	{ "Csave", 21, 1 },
11064 	{ "CPktOut", 20, 1 },
11065 	{ "RxPagePoolFull", 18, 2 },
11066 	{ "RxLpbkPkt", 17, 1 },
11067 	{ "TxLpbkPkt", 16, 1 },
11068 	{ "RxVfValid", 15, 1 },
11069 	{ "SynLearned", 14, 1 },
11070 	{ "SetDelEntry", 13, 1 },
11071 	{ "SetInvEntry", 12, 1 },
11072 	{ "CpcmdDvld", 11, 1 },
11073 	{ "CpcmdSave", 10, 1 },
11074 	{ "RxPstructsFull", 8, 2 },
11075 	{ "EpcmdDvld", 7, 1 },
11076 	{ "EpcmdFlush", 6, 1 },
11077 	{ "EpcmdTrimPrefix", 5, 1 },
11078 	{ "EpcmdTrimPostfix", 4, 1 },
11079 	{ "ERssIp4Pkt", 3, 1 },
11080 	{ "ERssIp6Pkt", 2, 1 },
11081 	{ "ERssTcpUdpPkt", 1, 1 },
11082 	{ "ERssFceFipPkt", 0, 1 },
11083 	{ NULL }
11084 };
11085 
11086 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)11087 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
11088 {
11089 
11090 	field_desc_show(sb, *p, tp_la0);
11091 }
11092 
11093 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)11094 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
11095 {
11096 
11097 	if (idx)
11098 		sbuf_printf(sb, "\n");
11099 	field_desc_show(sb, p[0], tp_la0);
11100 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11101 		field_desc_show(sb, p[1], tp_la0);
11102 }
11103 
11104 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)11105 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
11106 {
11107 
11108 	if (idx)
11109 		sbuf_printf(sb, "\n");
11110 	field_desc_show(sb, p[0], tp_la0);
11111 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11112 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
11113 }
11114 
11115 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)11116 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
11117 {
11118 	struct adapter *sc = arg1;
11119 	struct sbuf *sb;
11120 	uint64_t *buf, *p;
11121 	int rc;
11122 	u_int i, inc;
11123 	void (*show_func)(struct sbuf *, uint64_t *, int);
11124 
11125 	rc = sysctl_wire_old_buffer(req, 0);
11126 	if (rc != 0)
11127 		return (rc);
11128 
11129 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11130 	if (sb == NULL)
11131 		return (ENOMEM);
11132 
11133 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11134 
11135 	mtx_lock(&sc->reg_lock);
11136 	if (hw_off_limits(sc))
11137 		rc = ENXIO;
11138 	else {
11139 		t4_tp_read_la(sc, buf, NULL);
11140 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11141 		case 2:
11142 			inc = 2;
11143 			show_func = tp_la_show2;
11144 			break;
11145 		case 3:
11146 			inc = 2;
11147 			show_func = tp_la_show3;
11148 			break;
11149 		default:
11150 			inc = 1;
11151 			show_func = tp_la_show;
11152 		}
11153 	}
11154 	mtx_unlock(&sc->reg_lock);
11155 	if (rc != 0)
11156 		goto done;
11157 
11158 	p = buf;
11159 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
11160 		(*show_func)(sb, p, i);
11161 	rc = sbuf_finish(sb);
11162 done:
11163 	sbuf_delete(sb);
11164 	free(buf, M_CXGBE);
11165 	return (rc);
11166 }
11167 
11168 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)11169 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
11170 {
11171 	struct adapter *sc = arg1;
11172 	struct sbuf *sb;
11173 	int rc;
11174 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
11175 
11176 	rc = sysctl_wire_old_buffer(req, 0);
11177 	if (rc != 0)
11178 		return (rc);
11179 
11180 	mtx_lock(&sc->reg_lock);
11181 	if (hw_off_limits(sc))
11182 		rc = ENXIO;
11183 	else
11184 		t4_get_chan_txrate(sc, nrate, orate);
11185 	mtx_unlock(&sc->reg_lock);
11186 	if (rc != 0)
11187 		return (rc);
11188 
11189 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11190 	if (sb == NULL)
11191 		return (ENOMEM);
11192 
11193 	if (sc->chip_params->nchan > 2) {
11194 		sbuf_printf(sb, "              channel 0   channel 1"
11195 		    "   channel 2   channel 3\n");
11196 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
11197 		    nrate[0], nrate[1], nrate[2], nrate[3]);
11198 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
11199 		    orate[0], orate[1], orate[2], orate[3]);
11200 	} else {
11201 		sbuf_printf(sb, "              channel 0   channel 1\n");
11202 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
11203 		    nrate[0], nrate[1]);
11204 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
11205 		    orate[0], orate[1]);
11206 	}
11207 
11208 	rc = sbuf_finish(sb);
11209 	sbuf_delete(sb);
11210 
11211 	return (rc);
11212 }
11213 
11214 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)11215 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
11216 {
11217 	struct adapter *sc = arg1;
11218 	struct sbuf *sb;
11219 	uint32_t *buf, *p;
11220 	int rc, i;
11221 
11222 	rc = sysctl_wire_old_buffer(req, 0);
11223 	if (rc != 0)
11224 		return (rc);
11225 
11226 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11227 	if (sb == NULL)
11228 		return (ENOMEM);
11229 
11230 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
11231 	    M_ZERO | M_WAITOK);
11232 
11233 	mtx_lock(&sc->reg_lock);
11234 	if (hw_off_limits(sc))
11235 		rc = ENXIO;
11236 	else
11237 		t4_ulprx_read_la(sc, buf);
11238 	mtx_unlock(&sc->reg_lock);
11239 	if (rc != 0)
11240 		goto done;
11241 
11242 	p = buf;
11243 	sbuf_printf(sb, "      Pcmd        Type   Message"
11244 	    "                Data");
11245 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
11246 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
11247 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
11248 	}
11249 	rc = sbuf_finish(sb);
11250 done:
11251 	sbuf_delete(sb);
11252 	free(buf, M_CXGBE);
11253 	return (rc);
11254 }
11255 
11256 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)11257 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
11258 {
11259 	struct adapter *sc = arg1;
11260 	struct sbuf *sb;
11261 	int rc;
11262 	uint32_t cfg, s1, s2;
11263 
11264 	MPASS(chip_id(sc) >= CHELSIO_T5);
11265 
11266 	rc = sysctl_wire_old_buffer(req, 0);
11267 	if (rc != 0)
11268 		return (rc);
11269 
11270 	mtx_lock(&sc->reg_lock);
11271 	if (hw_off_limits(sc))
11272 		rc = ENXIO;
11273 	else {
11274 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
11275 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
11276 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
11277 	}
11278 	mtx_unlock(&sc->reg_lock);
11279 	if (rc != 0)
11280 		return (rc);
11281 
11282 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11283 	if (sb == NULL)
11284 		return (ENOMEM);
11285 
11286 	if (G_STATSOURCE_T5(cfg) == 7) {
11287 		int mode;
11288 
11289 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
11290 		if (mode == 0)
11291 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
11292 		else if (mode == 1)
11293 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
11294 		else
11295 			sbuf_printf(sb, "unknown mode %d", mode);
11296 	}
11297 	rc = sbuf_finish(sb);
11298 	sbuf_delete(sb);
11299 
11300 	return (rc);
11301 }
11302 
11303 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)11304 sysctl_cpus(SYSCTL_HANDLER_ARGS)
11305 {
11306 	struct adapter *sc = arg1;
11307 	enum cpu_sets op = arg2;
11308 	cpuset_t cpuset;
11309 	struct sbuf *sb;
11310 	int i, rc;
11311 
11312 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
11313 
11314 	CPU_ZERO(&cpuset);
11315 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
11316 	if (rc != 0)
11317 		return (rc);
11318 
11319 	rc = sysctl_wire_old_buffer(req, 0);
11320 	if (rc != 0)
11321 		return (rc);
11322 
11323 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11324 	if (sb == NULL)
11325 		return (ENOMEM);
11326 
11327 	CPU_FOREACH(i)
11328 		sbuf_printf(sb, "%d ", i);
11329 	rc = sbuf_finish(sb);
11330 	sbuf_delete(sb);
11331 
11332 	return (rc);
11333 }
11334 
11335 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)11336 sysctl_reset(SYSCTL_HANDLER_ARGS)
11337 {
11338 	struct adapter *sc = arg1;
11339 	u_int val;
11340 	int rc;
11341 
11342 	val = atomic_load_int(&sc->num_resets);
11343 	rc = sysctl_handle_int(oidp, &val, 0, req);
11344 	if (rc != 0 || req->newptr == NULL)
11345 		return (rc);
11346 
11347 	if (val == 0) {
11348 		/* Zero out the counter that tracks reset. */
11349 		atomic_store_int(&sc->num_resets, 0);
11350 		return (0);
11351 	}
11352 
11353 	if (val != 1)
11354 		return (EINVAL);	/* 0 or 1 are the only legal values */
11355 
11356 	if (hw_off_limits(sc))		/* harmless race */
11357 		return (EALREADY);
11358 
11359 	taskqueue_enqueue(reset_tq, &sc->reset_task);
11360 	return (0);
11361 }
11362 
11363 #ifdef TCP_OFFLOAD
11364 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)11365 sysctl_tls(SYSCTL_HANDLER_ARGS)
11366 {
11367 	struct adapter *sc = arg1;
11368 	int i, j, v, rc;
11369 	struct vi_info *vi;
11370 
11371 	v = sc->tt.tls;
11372 	rc = sysctl_handle_int(oidp, &v, 0, req);
11373 	if (rc != 0 || req->newptr == NULL)
11374 		return (rc);
11375 
11376 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11377 		return (ENOTSUP);
11378 
11379 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
11380 	if (rc)
11381 		return (rc);
11382 	if (hw_off_limits(sc))
11383 		rc = ENXIO;
11384 	else {
11385 		sc->tt.tls = !!v;
11386 		for_each_port(sc, i) {
11387 			for_each_vi(sc->port[i], j, vi) {
11388 				if (vi->flags & VI_INIT_DONE)
11389 					t4_update_fl_bufsize(vi->ifp);
11390 			}
11391 		}
11392 	}
11393 	end_synchronized_op(sc, 0);
11394 
11395 	return (rc);
11396 
11397 }
11398 
11399 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)11400 unit_conv(char *buf, size_t len, u_int val, u_int factor)
11401 {
11402 	u_int rem = val % factor;
11403 
11404 	if (rem == 0)
11405 		snprintf(buf, len, "%u", val / factor);
11406 	else {
11407 		while (rem % 10 == 0)
11408 			rem /= 10;
11409 		snprintf(buf, len, "%u.%u", val / factor, rem);
11410 	}
11411 }
11412 
11413 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)11414 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
11415 {
11416 	struct adapter *sc = arg1;
11417 	char buf[16];
11418 	u_int res, re;
11419 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11420 
11421 	mtx_lock(&sc->reg_lock);
11422 	if (hw_off_limits(sc))
11423 		res = (u_int)-1;
11424 	else
11425 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
11426 	mtx_unlock(&sc->reg_lock);
11427 	if (res == (u_int)-1)
11428 		return (ENXIO);
11429 
11430 	switch (arg2) {
11431 	case 0:
11432 		/* timer_tick */
11433 		re = G_TIMERRESOLUTION(res);
11434 		break;
11435 	case 1:
11436 		/* TCP timestamp tick */
11437 		re = G_TIMESTAMPRESOLUTION(res);
11438 		break;
11439 	case 2:
11440 		/* DACK tick */
11441 		re = G_DELAYEDACKRESOLUTION(res);
11442 		break;
11443 	default:
11444 		return (EDOOFUS);
11445 	}
11446 
11447 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
11448 
11449 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
11450 }
11451 
11452 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)11453 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
11454 {
11455 	struct adapter *sc = arg1;
11456 	int rc;
11457 	u_int dack_tmr, dack_re, v;
11458 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11459 
11460 	mtx_lock(&sc->reg_lock);
11461 	if (hw_off_limits(sc))
11462 		rc = ENXIO;
11463 	else {
11464 		rc = 0;
11465 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
11466 		    A_TP_TIMER_RESOLUTION));
11467 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
11468 	}
11469 	mtx_unlock(&sc->reg_lock);
11470 	if (rc != 0)
11471 		return (rc);
11472 
11473 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
11474 
11475 	return (sysctl_handle_int(oidp, &v, 0, req));
11476 }
11477 
11478 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)11479 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
11480 {
11481 	struct adapter *sc = arg1;
11482 	int rc, reg = arg2;
11483 	u_int tre;
11484 	u_long tp_tick_us, v;
11485 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11486 
11487 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
11488 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
11489 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
11490 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
11491 
11492 	mtx_lock(&sc->reg_lock);
11493 	if (hw_off_limits(sc))
11494 		rc = ENXIO;
11495 	else {
11496 		rc = 0;
11497 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
11498 		tp_tick_us = (cclk_ps << tre) / 1000000;
11499 		if (reg == A_TP_INIT_SRTT)
11500 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
11501 		else
11502 			v = tp_tick_us * t4_read_reg(sc, reg);
11503 	}
11504 	mtx_unlock(&sc->reg_lock);
11505 	if (rc != 0)
11506 		return (rc);
11507 	else
11508 		return (sysctl_handle_long(oidp, &v, 0, req));
11509 }
11510 
11511 /*
11512  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
11513  * passed to this function.
11514  */
11515 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)11516 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
11517 {
11518 	struct adapter *sc = arg1;
11519 	int rc, idx = arg2;
11520 	u_int v;
11521 
11522 	MPASS(idx >= 0 && idx <= 24);
11523 
11524 	mtx_lock(&sc->reg_lock);
11525 	if (hw_off_limits(sc))
11526 		rc = ENXIO;
11527 	else {
11528 		rc = 0;
11529 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
11530 	}
11531 	mtx_unlock(&sc->reg_lock);
11532 	if (rc != 0)
11533 		return (rc);
11534 	else
11535 		return (sysctl_handle_int(oidp, &v, 0, req));
11536 }
11537 
11538 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)11539 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
11540 {
11541 	struct adapter *sc = arg1;
11542 	int rc, idx = arg2;
11543 	u_int shift, v, r;
11544 
11545 	MPASS(idx >= 0 && idx < 16);
11546 
11547 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
11548 	shift = (idx & 3) << 3;
11549 	mtx_lock(&sc->reg_lock);
11550 	if (hw_off_limits(sc))
11551 		rc = ENXIO;
11552 	else {
11553 		rc = 0;
11554 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
11555 	}
11556 	mtx_unlock(&sc->reg_lock);
11557 	if (rc != 0)
11558 		return (rc);
11559 	else
11560 		return (sysctl_handle_int(oidp, &v, 0, req));
11561 }
11562 
11563 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)11564 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
11565 {
11566 	struct vi_info *vi = arg1;
11567 	struct adapter *sc = vi->adapter;
11568 	int idx, rc, i;
11569 	struct sge_ofld_rxq *ofld_rxq;
11570 	uint8_t v;
11571 
11572 	idx = vi->ofld_tmr_idx;
11573 
11574 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11575 	if (rc != 0 || req->newptr == NULL)
11576 		return (rc);
11577 
11578 	if (idx < 0 || idx >= SGE_NTIMERS)
11579 		return (EINVAL);
11580 
11581 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11582 	    "t4otmr");
11583 	if (rc)
11584 		return (rc);
11585 
11586 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
11587 	for_each_ofld_rxq(vi, i, ofld_rxq) {
11588 #ifdef atomic_store_rel_8
11589 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
11590 #else
11591 		ofld_rxq->iq.intr_params = v;
11592 #endif
11593 	}
11594 	vi->ofld_tmr_idx = idx;
11595 
11596 	end_synchronized_op(sc, LOCK_HELD);
11597 	return (0);
11598 }
11599 
11600 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)11601 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
11602 {
11603 	struct vi_info *vi = arg1;
11604 	struct adapter *sc = vi->adapter;
11605 	int idx, rc;
11606 
11607 	idx = vi->ofld_pktc_idx;
11608 
11609 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11610 	if (rc != 0 || req->newptr == NULL)
11611 		return (rc);
11612 
11613 	if (idx < -1 || idx >= SGE_NCOUNTERS)
11614 		return (EINVAL);
11615 
11616 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11617 	    "t4opktc");
11618 	if (rc)
11619 		return (rc);
11620 
11621 	if (vi->flags & VI_INIT_DONE)
11622 		rc = EBUSY; /* cannot be changed once the queues are created */
11623 	else
11624 		vi->ofld_pktc_idx = idx;
11625 
11626 	end_synchronized_op(sc, LOCK_HELD);
11627 	return (rc);
11628 }
11629 #endif
11630 
11631 static int
get_sge_context(struct adapter * sc,struct t4_sge_context * cntxt)11632 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
11633 {
11634 	int rc;
11635 
11636 	if (cntxt->cid > M_CTXTQID)
11637 		return (EINVAL);
11638 
11639 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
11640 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
11641 		return (EINVAL);
11642 
11643 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
11644 	if (rc)
11645 		return (rc);
11646 
11647 	if (hw_off_limits(sc)) {
11648 		rc = ENXIO;
11649 		goto done;
11650 	}
11651 
11652 	if (sc->flags & FW_OK) {
11653 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
11654 		    &cntxt->data[0]);
11655 		if (rc == 0)
11656 			goto done;
11657 	}
11658 
11659 	/*
11660 	 * Read via firmware failed or wasn't even attempted.  Read directly via
11661 	 * the backdoor.
11662 	 */
11663 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
11664 done:
11665 	end_synchronized_op(sc, 0);
11666 	return (rc);
11667 }
11668 
11669 static int
load_fw(struct adapter * sc,struct t4_data * fw)11670 load_fw(struct adapter *sc, struct t4_data *fw)
11671 {
11672 	int rc;
11673 	uint8_t *fw_data;
11674 
11675 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
11676 	if (rc)
11677 		return (rc);
11678 
11679 	if (hw_off_limits(sc)) {
11680 		rc = ENXIO;
11681 		goto done;
11682 	}
11683 
11684 	/*
11685 	 * The firmware, with the sole exception of the memory parity error
11686 	 * handler, runs from memory and not flash.  It is almost always safe to
11687 	 * install a new firmware on a running system.  Just set bit 1 in
11688 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
11689 	 */
11690 	if (sc->flags & FULL_INIT_DONE &&
11691 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
11692 		rc = EBUSY;
11693 		goto done;
11694 	}
11695 
11696 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
11697 
11698 	rc = copyin(fw->data, fw_data, fw->len);
11699 	if (rc == 0)
11700 		rc = -t4_load_fw(sc, fw_data, fw->len);
11701 
11702 	free(fw_data, M_CXGBE);
11703 done:
11704 	end_synchronized_op(sc, 0);
11705 	return (rc);
11706 }
11707 
11708 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)11709 load_cfg(struct adapter *sc, struct t4_data *cfg)
11710 {
11711 	int rc;
11712 	uint8_t *cfg_data = NULL;
11713 
11714 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11715 	if (rc)
11716 		return (rc);
11717 
11718 	if (hw_off_limits(sc)) {
11719 		rc = ENXIO;
11720 		goto done;
11721 	}
11722 
11723 	if (cfg->len == 0) {
11724 		/* clear */
11725 		rc = -t4_load_cfg(sc, NULL, 0);
11726 		goto done;
11727 	}
11728 
11729 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
11730 
11731 	rc = copyin(cfg->data, cfg_data, cfg->len);
11732 	if (rc == 0)
11733 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
11734 
11735 	free(cfg_data, M_CXGBE);
11736 done:
11737 	end_synchronized_op(sc, 0);
11738 	return (rc);
11739 }
11740 
11741 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)11742 load_boot(struct adapter *sc, struct t4_bootrom *br)
11743 {
11744 	int rc;
11745 	uint8_t *br_data = NULL;
11746 	u_int offset;
11747 
11748 	if (br->len > 1024 * 1024)
11749 		return (EFBIG);
11750 
11751 	if (br->pf_offset == 0) {
11752 		/* pfidx */
11753 		if (br->pfidx_addr > 7)
11754 			return (EINVAL);
11755 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
11756 		    A_PCIE_PF_EXPROM_OFST)));
11757 	} else if (br->pf_offset == 1) {
11758 		/* offset */
11759 		offset = G_OFFSET(br->pfidx_addr);
11760 	} else {
11761 		return (EINVAL);
11762 	}
11763 
11764 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
11765 	if (rc)
11766 		return (rc);
11767 
11768 	if (hw_off_limits(sc)) {
11769 		rc = ENXIO;
11770 		goto done;
11771 	}
11772 
11773 	if (br->len == 0) {
11774 		/* clear */
11775 		rc = -t4_load_boot(sc, NULL, offset, 0);
11776 		goto done;
11777 	}
11778 
11779 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
11780 
11781 	rc = copyin(br->data, br_data, br->len);
11782 	if (rc == 0)
11783 		rc = -t4_load_boot(sc, br_data, offset, br->len);
11784 
11785 	free(br_data, M_CXGBE);
11786 done:
11787 	end_synchronized_op(sc, 0);
11788 	return (rc);
11789 }
11790 
11791 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)11792 load_bootcfg(struct adapter *sc, struct t4_data *bc)
11793 {
11794 	int rc;
11795 	uint8_t *bc_data = NULL;
11796 
11797 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11798 	if (rc)
11799 		return (rc);
11800 
11801 	if (hw_off_limits(sc)) {
11802 		rc = ENXIO;
11803 		goto done;
11804 	}
11805 
11806 	if (bc->len == 0) {
11807 		/* clear */
11808 		rc = -t4_load_bootcfg(sc, NULL, 0);
11809 		goto done;
11810 	}
11811 
11812 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
11813 
11814 	rc = copyin(bc->data, bc_data, bc->len);
11815 	if (rc == 0)
11816 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
11817 
11818 	free(bc_data, M_CXGBE);
11819 done:
11820 	end_synchronized_op(sc, 0);
11821 	return (rc);
11822 }
11823 
11824 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)11825 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
11826 {
11827 	int rc;
11828 	struct cudbg_init *cudbg;
11829 	void *handle, *buf;
11830 
11831 	/* buf is large, don't block if no memory is available */
11832 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
11833 	if (buf == NULL)
11834 		return (ENOMEM);
11835 
11836 	handle = cudbg_alloc_handle();
11837 	if (handle == NULL) {
11838 		rc = ENOMEM;
11839 		goto done;
11840 	}
11841 
11842 	cudbg = cudbg_get_init(handle);
11843 	cudbg->adap = sc;
11844 	cudbg->print = (cudbg_print_cb)printf;
11845 
11846 #ifndef notyet
11847 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
11848 	    __func__, dump->wr_flash, dump->len, dump->data);
11849 #endif
11850 
11851 	if (dump->wr_flash)
11852 		cudbg->use_flash = 1;
11853 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
11854 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
11855 
11856 	rc = cudbg_collect(handle, buf, &dump->len);
11857 	if (rc != 0)
11858 		goto done;
11859 
11860 	rc = copyout(buf, dump->data, dump->len);
11861 done:
11862 	cudbg_free_handle(handle);
11863 	free(buf, M_CXGBE);
11864 	return (rc);
11865 }
11866 
11867 static void
free_offload_policy(struct t4_offload_policy * op)11868 free_offload_policy(struct t4_offload_policy *op)
11869 {
11870 	struct offload_rule *r;
11871 	int i;
11872 
11873 	if (op == NULL)
11874 		return;
11875 
11876 	r = &op->rule[0];
11877 	for (i = 0; i < op->nrules; i++, r++) {
11878 		free(r->bpf_prog.bf_insns, M_CXGBE);
11879 	}
11880 	free(op->rule, M_CXGBE);
11881 	free(op, M_CXGBE);
11882 }
11883 
11884 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)11885 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
11886 {
11887 	int i, rc, len;
11888 	struct t4_offload_policy *op, *old;
11889 	struct bpf_program *bf;
11890 	const struct offload_settings *s;
11891 	struct offload_rule *r;
11892 	void *u;
11893 
11894 	if (!is_offload(sc))
11895 		return (ENODEV);
11896 
11897 	if (uop->nrules == 0) {
11898 		/* Delete installed policies. */
11899 		op = NULL;
11900 		goto set_policy;
11901 	} else if (uop->nrules > 256) { /* arbitrary */
11902 		return (E2BIG);
11903 	}
11904 
11905 	/* Copy userspace offload policy to kernel */
11906 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
11907 	op->nrules = uop->nrules;
11908 	len = op->nrules * sizeof(struct offload_rule);
11909 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11910 	rc = copyin(uop->rule, op->rule, len);
11911 	if (rc) {
11912 		free(op->rule, M_CXGBE);
11913 		free(op, M_CXGBE);
11914 		return (rc);
11915 	}
11916 
11917 	r = &op->rule[0];
11918 	for (i = 0; i < op->nrules; i++, r++) {
11919 
11920 		/* Validate open_type */
11921 		if (r->open_type != OPEN_TYPE_LISTEN &&
11922 		    r->open_type != OPEN_TYPE_ACTIVE &&
11923 		    r->open_type != OPEN_TYPE_PASSIVE &&
11924 		    r->open_type != OPEN_TYPE_DONTCARE) {
11925 error:
11926 			/*
11927 			 * Rules 0 to i have malloc'd filters that need to be
11928 			 * freed.  Rules i+1 to nrules have userspace pointers
11929 			 * and should be left alone.
11930 			 */
11931 			op->nrules = i;
11932 			free_offload_policy(op);
11933 			return (rc);
11934 		}
11935 
11936 		/* Validate settings */
11937 		s = &r->settings;
11938 		if ((s->offload != 0 && s->offload != 1) ||
11939 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
11940 		    s->sched_class < -1 ||
11941 		    s->sched_class >= sc->params.nsched_cls) {
11942 			rc = EINVAL;
11943 			goto error;
11944 		}
11945 
11946 		bf = &r->bpf_prog;
11947 		u = bf->bf_insns;	/* userspace ptr */
11948 		bf->bf_insns = NULL;
11949 		if (bf->bf_len == 0) {
11950 			/* legal, matches everything */
11951 			continue;
11952 		}
11953 		len = bf->bf_len * sizeof(*bf->bf_insns);
11954 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11955 		rc = copyin(u, bf->bf_insns, len);
11956 		if (rc != 0)
11957 			goto error;
11958 
11959 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
11960 			rc = EINVAL;
11961 			goto error;
11962 		}
11963 	}
11964 set_policy:
11965 	rw_wlock(&sc->policy_lock);
11966 	old = sc->policy;
11967 	sc->policy = op;
11968 	rw_wunlock(&sc->policy_lock);
11969 	free_offload_policy(old);
11970 
11971 	return (0);
11972 }
11973 
11974 #define MAX_READ_BUF_SIZE (128 * 1024)
11975 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)11976 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
11977 {
11978 	uint32_t addr, remaining, n;
11979 	uint32_t *buf;
11980 	int rc;
11981 	uint8_t *dst;
11982 
11983 	mtx_lock(&sc->reg_lock);
11984 	if (hw_off_limits(sc))
11985 		rc = ENXIO;
11986 	else
11987 		rc = validate_mem_range(sc, mr->addr, mr->len);
11988 	mtx_unlock(&sc->reg_lock);
11989 	if (rc != 0)
11990 		return (rc);
11991 
11992 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
11993 	addr = mr->addr;
11994 	remaining = mr->len;
11995 	dst = (void *)mr->data;
11996 
11997 	while (remaining) {
11998 		n = min(remaining, MAX_READ_BUF_SIZE);
11999 		mtx_lock(&sc->reg_lock);
12000 		if (hw_off_limits(sc))
12001 			rc = ENXIO;
12002 		else
12003 			read_via_memwin(sc, 2, addr, buf, n);
12004 		mtx_unlock(&sc->reg_lock);
12005 		if (rc != 0)
12006 			break;
12007 
12008 		rc = copyout(buf, dst, n);
12009 		if (rc != 0)
12010 			break;
12011 
12012 		dst += n;
12013 		remaining -= n;
12014 		addr += n;
12015 	}
12016 
12017 	free(buf, M_CXGBE);
12018 	return (rc);
12019 }
12020 #undef MAX_READ_BUF_SIZE
12021 
12022 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)12023 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
12024 {
12025 	int rc;
12026 
12027 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
12028 		return (EINVAL);
12029 
12030 	if (i2cd->len > sizeof(i2cd->data))
12031 		return (EFBIG);
12032 
12033 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
12034 	if (rc)
12035 		return (rc);
12036 	if (hw_off_limits(sc))
12037 		rc = ENXIO;
12038 	else
12039 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
12040 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
12041 	end_synchronized_op(sc, 0);
12042 
12043 	return (rc);
12044 }
12045 
12046 static int
clear_stats(struct adapter * sc,u_int port_id)12047 clear_stats(struct adapter *sc, u_int port_id)
12048 {
12049 	int i, v, chan_map;
12050 	struct port_info *pi;
12051 	struct vi_info *vi;
12052 	struct sge_rxq *rxq;
12053 	struct sge_txq *txq;
12054 	struct sge_wrq *wrq;
12055 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12056 	struct sge_ofld_txq *ofld_txq;
12057 #endif
12058 #ifdef TCP_OFFLOAD
12059 	struct sge_ofld_rxq *ofld_rxq;
12060 #endif
12061 
12062 	if (port_id >= sc->params.nports)
12063 		return (EINVAL);
12064 	pi = sc->port[port_id];
12065 	if (pi == NULL)
12066 		return (EIO);
12067 
12068 	mtx_lock(&sc->reg_lock);
12069 	if (!hw_off_limits(sc)) {
12070 		/* MAC stats */
12071 		t4_clr_port_stats(sc, pi->tx_chan);
12072 		if (is_t6(sc)) {
12073 			if (pi->fcs_reg != -1)
12074 				pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12075 			else
12076 				pi->stats.rx_fcs_err = 0;
12077 		}
12078 		for_each_vi(pi, v, vi) {
12079 			if (vi->flags & VI_INIT_DONE)
12080 				t4_clr_vi_stats(sc, vi->vin);
12081 		}
12082 		chan_map = pi->rx_e_chan_map;
12083 		v = 0;	/* reuse */
12084 		while (chan_map) {
12085 			i = ffs(chan_map) - 1;
12086 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
12087 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
12088 			chan_map &= ~(1 << i);
12089 		}
12090 	}
12091 	mtx_unlock(&sc->reg_lock);
12092 	pi->tx_parse_error = 0;
12093 	pi->tnl_cong_drops = 0;
12094 
12095 	/*
12096 	 * Since this command accepts a port, clear stats for
12097 	 * all VIs on this port.
12098 	 */
12099 	for_each_vi(pi, v, vi) {
12100 		if (vi->flags & VI_INIT_DONE) {
12101 
12102 			for_each_rxq(vi, i, rxq) {
12103 #if defined(INET) || defined(INET6)
12104 				rxq->lro.lro_queued = 0;
12105 				rxq->lro.lro_flushed = 0;
12106 #endif
12107 				rxq->rxcsum = 0;
12108 				rxq->vlan_extraction = 0;
12109 				rxq->vxlan_rxcsum = 0;
12110 
12111 				rxq->fl.cl_allocated = 0;
12112 				rxq->fl.cl_recycled = 0;
12113 				rxq->fl.cl_fast_recycled = 0;
12114 			}
12115 
12116 			for_each_txq(vi, i, txq) {
12117 				txq->txcsum = 0;
12118 				txq->tso_wrs = 0;
12119 				txq->vlan_insertion = 0;
12120 				txq->imm_wrs = 0;
12121 				txq->sgl_wrs = 0;
12122 				txq->txpkt_wrs = 0;
12123 				txq->txpkts0_wrs = 0;
12124 				txq->txpkts1_wrs = 0;
12125 				txq->txpkts0_pkts = 0;
12126 				txq->txpkts1_pkts = 0;
12127 				txq->txpkts_flush = 0;
12128 				txq->raw_wrs = 0;
12129 				txq->vxlan_tso_wrs = 0;
12130 				txq->vxlan_txcsum = 0;
12131 				txq->kern_tls_records = 0;
12132 				txq->kern_tls_short = 0;
12133 				txq->kern_tls_partial = 0;
12134 				txq->kern_tls_full = 0;
12135 				txq->kern_tls_octets = 0;
12136 				txq->kern_tls_waste = 0;
12137 				txq->kern_tls_options = 0;
12138 				txq->kern_tls_header = 0;
12139 				txq->kern_tls_fin = 0;
12140 				txq->kern_tls_fin_short = 0;
12141 				txq->kern_tls_cbc = 0;
12142 				txq->kern_tls_gcm = 0;
12143 				mp_ring_reset_stats(txq->r);
12144 			}
12145 
12146 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12147 			for_each_ofld_txq(vi, i, ofld_txq) {
12148 				ofld_txq->wrq.tx_wrs_direct = 0;
12149 				ofld_txq->wrq.tx_wrs_copied = 0;
12150 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12151 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12152 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12153 				counter_u64_zero(ofld_txq->tx_aio_jobs);
12154 				counter_u64_zero(ofld_txq->tx_aio_octets);
12155 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
12156 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
12157 			}
12158 #endif
12159 #ifdef TCP_OFFLOAD
12160 			for_each_ofld_rxq(vi, i, ofld_rxq) {
12161 				ofld_rxq->fl.cl_allocated = 0;
12162 				ofld_rxq->fl.cl_recycled = 0;
12163 				ofld_rxq->fl.cl_fast_recycled = 0;
12164 				counter_u64_zero(
12165 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
12166 				counter_u64_zero(
12167 				    ofld_rxq->rx_iscsi_ddp_setup_error);
12168 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
12169 				ofld_rxq->rx_iscsi_ddp_octets = 0;
12170 				ofld_rxq->rx_iscsi_fl_pdus = 0;
12171 				ofld_rxq->rx_iscsi_fl_octets = 0;
12172 				ofld_rxq->rx_aio_ddp_jobs = 0;
12173 				ofld_rxq->rx_aio_ddp_octets = 0;
12174 				ofld_rxq->rx_toe_tls_records = 0;
12175 				ofld_rxq->rx_toe_tls_octets = 0;
12176 				ofld_rxq->rx_toe_ddp_octets = 0;
12177 				counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
12178 				counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
12179 				counter_u64_zero(ofld_rxq->ddp_buffer_free);
12180 			}
12181 #endif
12182 
12183 			if (IS_MAIN_VI(vi)) {
12184 				wrq = &sc->sge.ctrlq[pi->port_id];
12185 				wrq->tx_wrs_direct = 0;
12186 				wrq->tx_wrs_copied = 0;
12187 			}
12188 		}
12189 	}
12190 
12191 	return (0);
12192 }
12193 
12194 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12195 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12196 {
12197 #ifdef INET6
12198 	struct in6_addr in6;
12199 
12200 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12201 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
12202 		return (0);
12203 	else
12204 		return (EIO);
12205 #else
12206 	return (ENOTSUP);
12207 #endif
12208 }
12209 
12210 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12211 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12212 {
12213 #ifdef INET6
12214 	struct in6_addr in6;
12215 
12216 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12217 	return (t4_release_clip_addr(sc, &in6));
12218 #else
12219 	return (ENOTSUP);
12220 #endif
12221 }
12222 
12223 int
t4_os_find_pci_capability(struct adapter * sc,int cap)12224 t4_os_find_pci_capability(struct adapter *sc, int cap)
12225 {
12226 	int i;
12227 
12228 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
12229 }
12230 
12231 int
t4_os_pci_save_state(struct adapter * sc)12232 t4_os_pci_save_state(struct adapter *sc)
12233 {
12234 	device_t dev;
12235 	struct pci_devinfo *dinfo;
12236 
12237 	dev = sc->dev;
12238 	dinfo = device_get_ivars(dev);
12239 
12240 	pci_cfg_save(dev, dinfo, 0);
12241 	return (0);
12242 }
12243 
12244 int
t4_os_pci_restore_state(struct adapter * sc)12245 t4_os_pci_restore_state(struct adapter *sc)
12246 {
12247 	device_t dev;
12248 	struct pci_devinfo *dinfo;
12249 
12250 	dev = sc->dev;
12251 	dinfo = device_get_ivars(dev);
12252 
12253 	pci_cfg_restore(dev, dinfo);
12254 	return (0);
12255 }
12256 
12257 void
t4_os_portmod_changed(struct port_info * pi)12258 t4_os_portmod_changed(struct port_info *pi)
12259 {
12260 	struct adapter *sc = pi->adapter;
12261 	struct vi_info *vi;
12262 	if_t ifp;
12263 	static const char *mod_str[] = {
12264 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
12265 	};
12266 
12267 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
12268 	    ("%s: port_type %u", __func__, pi->port_type));
12269 
12270 	vi = &pi->vi[0];
12271 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
12272 		PORT_LOCK(pi);
12273 		build_medialist(pi);
12274 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
12275 			fixup_link_config(pi);
12276 			apply_link_config(pi);
12277 		}
12278 		PORT_UNLOCK(pi);
12279 		end_synchronized_op(sc, LOCK_HELD);
12280 	}
12281 
12282 	ifp = vi->ifp;
12283 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
12284 		if_printf(ifp, "transceiver unplugged.\n");
12285 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
12286 		if_printf(ifp, "unknown transceiver inserted.\n");
12287 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
12288 		if_printf(ifp, "unsupported transceiver inserted.\n");
12289 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
12290 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
12291 		    port_top_speed(pi), mod_str[pi->mod_type]);
12292 	} else {
12293 		if_printf(ifp, "transceiver (type %d) inserted.\n",
12294 		    pi->mod_type);
12295 	}
12296 }
12297 
12298 void
t4_os_link_changed(struct port_info * pi)12299 t4_os_link_changed(struct port_info *pi)
12300 {
12301 	struct vi_info *vi;
12302 	if_t ifp;
12303 	struct link_config *lc = &pi->link_cfg;
12304 	struct adapter *sc = pi->adapter;
12305 	int v;
12306 
12307 	PORT_LOCK_ASSERT_OWNED(pi);
12308 
12309 	if (is_t6(sc)) {
12310 		if (lc->link_ok) {
12311 			if (lc->speed > 25000 ||
12312 			    (lc->speed == 25000 && lc->fec == FEC_RS)) {
12313 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12314 				    A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS);
12315 			} else {
12316 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12317 				    A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS);
12318 			}
12319 			pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12320 			pi->stats.rx_fcs_err = 0;
12321 		} else {
12322 			pi->fcs_reg = -1;
12323 		}
12324 	} else {
12325 		MPASS(pi->fcs_reg != -1);
12326 		MPASS(pi->fcs_base == 0);
12327 	}
12328 
12329 	for_each_vi(pi, v, vi) {
12330 		ifp = vi->ifp;
12331 		if (ifp == NULL || IS_DETACHING(vi))
12332 			continue;
12333 
12334 		if (lc->link_ok) {
12335 			if_setbaudrate(ifp, IF_Mbps(lc->speed));
12336 			if_link_state_change(ifp, LINK_STATE_UP);
12337 		} else {
12338 			if_link_state_change(ifp, LINK_STATE_DOWN);
12339 		}
12340 	}
12341 }
12342 
12343 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)12344 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
12345 {
12346 	struct adapter *sc;
12347 
12348 	sx_slock(&t4_list_lock);
12349 	SLIST_FOREACH(sc, &t4_list, link) {
12350 		/*
12351 		 * func should not make any assumptions about what state sc is
12352 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
12353 		 */
12354 		func(sc, arg);
12355 	}
12356 	sx_sunlock(&t4_list_lock);
12357 }
12358 
12359 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)12360 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
12361     struct thread *td)
12362 {
12363 	int rc;
12364 	struct adapter *sc = dev->si_drv1;
12365 
12366 	rc = priv_check(td, PRIV_DRIVER);
12367 	if (rc != 0)
12368 		return (rc);
12369 
12370 	switch (cmd) {
12371 	case CHELSIO_T4_GETREG: {
12372 		struct t4_reg *edata = (struct t4_reg *)data;
12373 
12374 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12375 			return (EFAULT);
12376 
12377 		mtx_lock(&sc->reg_lock);
12378 		if (hw_off_limits(sc))
12379 			rc = ENXIO;
12380 		else if (edata->size == 4)
12381 			edata->val = t4_read_reg(sc, edata->addr);
12382 		else if (edata->size == 8)
12383 			edata->val = t4_read_reg64(sc, edata->addr);
12384 		else
12385 			rc = EINVAL;
12386 		mtx_unlock(&sc->reg_lock);
12387 
12388 		break;
12389 	}
12390 	case CHELSIO_T4_SETREG: {
12391 		struct t4_reg *edata = (struct t4_reg *)data;
12392 
12393 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12394 			return (EFAULT);
12395 
12396 		mtx_lock(&sc->reg_lock);
12397 		if (hw_off_limits(sc))
12398 			rc = ENXIO;
12399 		else if (edata->size == 4) {
12400 			if (edata->val & 0xffffffff00000000)
12401 				rc = EINVAL;
12402 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
12403 		} else if (edata->size == 8)
12404 			t4_write_reg64(sc, edata->addr, edata->val);
12405 		else
12406 			rc = EINVAL;
12407 		mtx_unlock(&sc->reg_lock);
12408 
12409 		break;
12410 	}
12411 	case CHELSIO_T4_REGDUMP: {
12412 		struct t4_regdump *regs = (struct t4_regdump *)data;
12413 		int reglen = t4_get_regs_len(sc);
12414 		uint8_t *buf;
12415 
12416 		if (regs->len < reglen) {
12417 			regs->len = reglen; /* hint to the caller */
12418 			return (ENOBUFS);
12419 		}
12420 
12421 		regs->len = reglen;
12422 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
12423 		mtx_lock(&sc->reg_lock);
12424 		if (hw_off_limits(sc))
12425 			rc = ENXIO;
12426 		else
12427 			get_regs(sc, regs, buf);
12428 		mtx_unlock(&sc->reg_lock);
12429 		if (rc == 0)
12430 			rc = copyout(buf, regs->data, reglen);
12431 		free(buf, M_CXGBE);
12432 		break;
12433 	}
12434 	case CHELSIO_T4_GET_FILTER_MODE:
12435 		rc = get_filter_mode(sc, (uint32_t *)data);
12436 		break;
12437 	case CHELSIO_T4_SET_FILTER_MODE:
12438 		rc = set_filter_mode(sc, *(uint32_t *)data);
12439 		break;
12440 	case CHELSIO_T4_SET_FILTER_MASK:
12441 		rc = set_filter_mask(sc, *(uint32_t *)data);
12442 		break;
12443 	case CHELSIO_T4_GET_FILTER:
12444 		rc = get_filter(sc, (struct t4_filter *)data);
12445 		break;
12446 	case CHELSIO_T4_SET_FILTER:
12447 		rc = set_filter(sc, (struct t4_filter *)data);
12448 		break;
12449 	case CHELSIO_T4_DEL_FILTER:
12450 		rc = del_filter(sc, (struct t4_filter *)data);
12451 		break;
12452 	case CHELSIO_T4_GET_SGE_CONTEXT:
12453 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
12454 		break;
12455 	case CHELSIO_T4_LOAD_FW:
12456 		rc = load_fw(sc, (struct t4_data *)data);
12457 		break;
12458 	case CHELSIO_T4_GET_MEM:
12459 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
12460 		break;
12461 	case CHELSIO_T4_GET_I2C:
12462 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
12463 		break;
12464 	case CHELSIO_T4_CLEAR_STATS:
12465 		rc = clear_stats(sc, *(uint32_t *)data);
12466 		break;
12467 	case CHELSIO_T4_SCHED_CLASS:
12468 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
12469 		break;
12470 	case CHELSIO_T4_SCHED_QUEUE:
12471 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
12472 		break;
12473 	case CHELSIO_T4_GET_TRACER:
12474 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
12475 		break;
12476 	case CHELSIO_T4_SET_TRACER:
12477 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
12478 		break;
12479 	case CHELSIO_T4_LOAD_CFG:
12480 		rc = load_cfg(sc, (struct t4_data *)data);
12481 		break;
12482 	case CHELSIO_T4_LOAD_BOOT:
12483 		rc = load_boot(sc, (struct t4_bootrom *)data);
12484 		break;
12485 	case CHELSIO_T4_LOAD_BOOTCFG:
12486 		rc = load_bootcfg(sc, (struct t4_data *)data);
12487 		break;
12488 	case CHELSIO_T4_CUDBG_DUMP:
12489 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
12490 		break;
12491 	case CHELSIO_T4_SET_OFLD_POLICY:
12492 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
12493 		break;
12494 	case CHELSIO_T4_HOLD_CLIP_ADDR:
12495 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
12496 		break;
12497 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
12498 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
12499 		break;
12500 	default:
12501 		rc = ENOTTY;
12502 	}
12503 
12504 	return (rc);
12505 }
12506 
12507 #ifdef TCP_OFFLOAD
12508 int
toe_capability(struct vi_info * vi,bool enable)12509 toe_capability(struct vi_info *vi, bool enable)
12510 {
12511 	int rc;
12512 	struct port_info *pi = vi->pi;
12513 	struct adapter *sc = pi->adapter;
12514 
12515 	ASSERT_SYNCHRONIZED_OP(sc);
12516 
12517 	if (!is_offload(sc))
12518 		return (ENODEV);
12519 	if (hw_off_limits(sc))
12520 		return (ENXIO);
12521 
12522 	if (enable) {
12523 #ifdef KERN_TLS
12524 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
12525 			int i, j, n;
12526 			struct port_info *p;
12527 			struct vi_info *v;
12528 
12529 			/*
12530 			 * Reconfigure hardware for TOE if TXTLS is not enabled
12531 			 * on any ifnet.
12532 			 */
12533 			n = 0;
12534 			for_each_port(sc, i) {
12535 				p = sc->port[i];
12536 				for_each_vi(p, j, v) {
12537 					if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
12538 						CH_WARN(sc,
12539 						    "%s has NIC TLS enabled.\n",
12540 						    device_get_nameunit(v->dev));
12541 						n++;
12542 					}
12543 				}
12544 			}
12545 			if (n > 0) {
12546 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
12547 				    "associated with this adapter before "
12548 				    "trying to enable TOE.\n");
12549 				return (EAGAIN);
12550 			}
12551 			rc = t6_config_kern_tls(sc, false);
12552 			if (rc)
12553 				return (rc);
12554 		}
12555 #endif
12556 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
12557 			/* TOE is already enabled. */
12558 			return (0);
12559 		}
12560 
12561 		/*
12562 		 * We need the port's queues around so that we're able to send
12563 		 * and receive CPLs to/from the TOE even if the ifnet for this
12564 		 * port has never been UP'd administratively.
12565 		 */
12566 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
12567 			return (rc);
12568 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
12569 		    ((rc = vi_init(&pi->vi[0])) != 0))
12570 			return (rc);
12571 
12572 		if (isset(&sc->offload_map, pi->port_id)) {
12573 			/* TOE is enabled on another VI of this port. */
12574 			MPASS(pi->uld_vis > 0);
12575 			pi->uld_vis++;
12576 			return (0);
12577 		}
12578 
12579 		if (!uld_active(sc, ULD_TOM)) {
12580 			rc = t4_activate_uld(sc, ULD_TOM);
12581 			if (rc == EAGAIN) {
12582 				log(LOG_WARNING,
12583 				    "You must kldload t4_tom.ko before trying "
12584 				    "to enable TOE on a cxgbe interface.\n");
12585 			}
12586 			if (rc != 0)
12587 				return (rc);
12588 			KASSERT(sc->tom_softc != NULL,
12589 			    ("%s: TOM activated but softc NULL", __func__));
12590 			KASSERT(uld_active(sc, ULD_TOM),
12591 			    ("%s: TOM activated but flag not set", __func__));
12592 		}
12593 
12594 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
12595 		if (!uld_active(sc, ULD_IWARP))
12596 			(void) t4_activate_uld(sc, ULD_IWARP);
12597 		if (!uld_active(sc, ULD_ISCSI))
12598 			(void) t4_activate_uld(sc, ULD_ISCSI);
12599 
12600 		if (pi->uld_vis++ == 0)
12601 			setbit(&sc->offload_map, pi->port_id);
12602 	} else {
12603 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) == 0) {
12604 			/* TOE is already disabled. */
12605 			return (0);
12606 		}
12607 		MPASS(isset(&sc->offload_map, pi->port_id));
12608 		MPASS(pi->uld_vis > 0);
12609 		if (--pi->uld_vis == 0)
12610 			clrbit(&sc->offload_map, pi->port_id);
12611 	}
12612 
12613 	return (0);
12614 }
12615 
12616 /*
12617  * Add an upper layer driver to the global list.
12618  */
12619 int
t4_register_uld(struct uld_info * ui,int id)12620 t4_register_uld(struct uld_info *ui, int id)
12621 {
12622 	int rc;
12623 
12624 	if (id < 0 || id > ULD_MAX)
12625 		return (EINVAL);
12626 	sx_xlock(&t4_uld_list_lock);
12627 	if (t4_uld_list[id] != NULL)
12628 		rc = EEXIST;
12629 	else {
12630 		t4_uld_list[id] = ui;
12631 		rc = 0;
12632 	}
12633 	sx_xunlock(&t4_uld_list_lock);
12634 	return (rc);
12635 }
12636 
12637 int
t4_unregister_uld(struct uld_info * ui,int id)12638 t4_unregister_uld(struct uld_info *ui, int id)
12639 {
12640 
12641 	if (id < 0 || id > ULD_MAX)
12642 		return (EINVAL);
12643 	sx_xlock(&t4_uld_list_lock);
12644 	MPASS(t4_uld_list[id] == ui);
12645 	t4_uld_list[id] = NULL;
12646 	sx_xunlock(&t4_uld_list_lock);
12647 	return (0);
12648 }
12649 
12650 int
t4_activate_uld(struct adapter * sc,int id)12651 t4_activate_uld(struct adapter *sc, int id)
12652 {
12653 	int rc;
12654 
12655 	ASSERT_SYNCHRONIZED_OP(sc);
12656 
12657 	if (id < 0 || id > ULD_MAX)
12658 		return (EINVAL);
12659 
12660 	/* Adapter needs to be initialized before any ULD can be activated. */
12661 	if (!(sc->flags & FULL_INIT_DONE)) {
12662 		rc = adapter_init(sc);
12663 		if (rc != 0)
12664 			return (rc);
12665 	}
12666 
12667 	sx_slock(&t4_uld_list_lock);
12668 	if (t4_uld_list[id] == NULL)
12669 		rc = EAGAIN;	/* load the KLD with this ULD and try again. */
12670 	else {
12671 		rc = t4_uld_list[id]->uld_activate(sc);
12672 		if (rc == 0)
12673 			setbit(&sc->active_ulds, id);
12674 	}
12675 	sx_sunlock(&t4_uld_list_lock);
12676 
12677 	return (rc);
12678 }
12679 
12680 int
t4_deactivate_uld(struct adapter * sc,int id)12681 t4_deactivate_uld(struct adapter *sc, int id)
12682 {
12683 	int rc;
12684 
12685 	ASSERT_SYNCHRONIZED_OP(sc);
12686 
12687 	if (id < 0 || id > ULD_MAX)
12688 		return (EINVAL);
12689 
12690 	sx_slock(&t4_uld_list_lock);
12691 	if (t4_uld_list[id] == NULL)
12692 		rc = ENXIO;
12693 	else {
12694 		rc = t4_uld_list[id]->uld_deactivate(sc);
12695 		if (rc == 0)
12696 			clrbit(&sc->active_ulds, id);
12697 	}
12698 	sx_sunlock(&t4_uld_list_lock);
12699 
12700 	return (rc);
12701 }
12702 
12703 static int
deactivate_all_uld(struct adapter * sc)12704 deactivate_all_uld(struct adapter *sc)
12705 {
12706 	int i, rc;
12707 
12708 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
12709 	if (rc != 0)
12710 		return (ENXIO);
12711 	sx_slock(&t4_uld_list_lock);
12712 	for (i = 0; i <= ULD_MAX; i++) {
12713 		if (t4_uld_list[i] == NULL || !uld_active(sc, i))
12714 			continue;
12715 		rc = t4_uld_list[i]->uld_deactivate(sc);
12716 		if (rc != 0)
12717 			break;
12718 		clrbit(&sc->active_ulds, i);
12719 	}
12720 	sx_sunlock(&t4_uld_list_lock);
12721 	end_synchronized_op(sc, 0);
12722 
12723 	return (rc);
12724 }
12725 
12726 static void
stop_all_uld(struct adapter * sc)12727 stop_all_uld(struct adapter *sc)
12728 {
12729 	int i;
12730 
12731 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
12732 		return;
12733 	sx_slock(&t4_uld_list_lock);
12734 	for (i = 0; i <= ULD_MAX; i++) {
12735 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12736 		    t4_uld_list[i]->uld_stop == NULL)
12737 			continue;
12738 		(void) t4_uld_list[i]->uld_stop(sc);
12739 	}
12740 	sx_sunlock(&t4_uld_list_lock);
12741 	end_synchronized_op(sc, 0);
12742 }
12743 
12744 static void
restart_all_uld(struct adapter * sc)12745 restart_all_uld(struct adapter *sc)
12746 {
12747 	int i;
12748 
12749 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
12750 		return;
12751 	sx_slock(&t4_uld_list_lock);
12752 	for (i = 0; i <= ULD_MAX; i++) {
12753 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12754 		    t4_uld_list[i]->uld_restart == NULL)
12755 			continue;
12756 		(void) t4_uld_list[i]->uld_restart(sc);
12757 	}
12758 	sx_sunlock(&t4_uld_list_lock);
12759 	end_synchronized_op(sc, 0);
12760 }
12761 
12762 int
uld_active(struct adapter * sc,int id)12763 uld_active(struct adapter *sc, int id)
12764 {
12765 
12766 	MPASS(id >= 0 && id <= ULD_MAX);
12767 
12768 	return (isset(&sc->active_ulds, id));
12769 }
12770 #endif
12771 
12772 #ifdef KERN_TLS
12773 static int
ktls_capability(struct adapter * sc,bool enable)12774 ktls_capability(struct adapter *sc, bool enable)
12775 {
12776 	ASSERT_SYNCHRONIZED_OP(sc);
12777 
12778 	if (!is_ktls(sc))
12779 		return (ENODEV);
12780 	if (!is_t6(sc))
12781 		return (0);
12782 	if (hw_off_limits(sc))
12783 		return (ENXIO);
12784 
12785 	if (enable) {
12786 		if (sc->flags & KERN_TLS_ON)
12787 			return (0);	/* already on */
12788 		if (sc->offload_map != 0) {
12789 			CH_WARN(sc,
12790 			    "Disable TOE on all interfaces associated with "
12791 			    "this adapter before trying to enable NIC TLS.\n");
12792 			return (EAGAIN);
12793 		}
12794 		return (t6_config_kern_tls(sc, true));
12795 	} else {
12796 		/*
12797 		 * Nothing to do for disable.  If TOE is enabled sometime later
12798 		 * then toe_capability will reconfigure the hardware.
12799 		 */
12800 		return (0);
12801 	}
12802 }
12803 #endif
12804 
12805 /*
12806  * t  = ptr to tunable.
12807  * nc = number of CPUs.
12808  * c  = compiled in default for that tunable.
12809  */
12810 static void
calculate_nqueues(int * t,int nc,const int c)12811 calculate_nqueues(int *t, int nc, const int c)
12812 {
12813 	int nq;
12814 
12815 	if (*t > 0)
12816 		return;
12817 	nq = *t < 0 ? -*t : c;
12818 	*t = min(nc, nq);
12819 }
12820 
12821 /*
12822  * Come up with reasonable defaults for some of the tunables, provided they're
12823  * not set by the user (in which case we'll use the values as is).
12824  */
12825 static void
tweak_tunables(void)12826 tweak_tunables(void)
12827 {
12828 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
12829 
12830 	if (t4_ntxq < 1) {
12831 #ifdef RSS
12832 		t4_ntxq = rss_getnumbuckets();
12833 #else
12834 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
12835 #endif
12836 	}
12837 
12838 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
12839 
12840 	if (t4_nrxq < 1) {
12841 #ifdef RSS
12842 		t4_nrxq = rss_getnumbuckets();
12843 #else
12844 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
12845 #endif
12846 	}
12847 
12848 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
12849 
12850 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12851 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
12852 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
12853 #endif
12854 #ifdef TCP_OFFLOAD
12855 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
12856 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
12857 #endif
12858 
12859 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
12860 	if (t4_toecaps_allowed == -1)
12861 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
12862 #else
12863 	if (t4_toecaps_allowed == -1)
12864 		t4_toecaps_allowed = 0;
12865 #endif
12866 
12867 #ifdef TCP_OFFLOAD
12868 	if (t4_rdmacaps_allowed == -1) {
12869 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
12870 		    FW_CAPS_CONFIG_RDMA_RDMAC;
12871 	}
12872 
12873 	if (t4_iscsicaps_allowed == -1) {
12874 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
12875 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
12876 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
12877 	}
12878 
12879 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
12880 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
12881 
12882 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
12883 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
12884 #else
12885 	if (t4_rdmacaps_allowed == -1)
12886 		t4_rdmacaps_allowed = 0;
12887 
12888 	if (t4_iscsicaps_allowed == -1)
12889 		t4_iscsicaps_allowed = 0;
12890 #endif
12891 
12892 #ifdef DEV_NETMAP
12893 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
12894 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
12895 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
12896 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
12897 #endif
12898 
12899 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
12900 		t4_tmr_idx = TMR_IDX;
12901 
12902 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
12903 		t4_pktc_idx = PKTC_IDX;
12904 
12905 	if (t4_qsize_txq < 128)
12906 		t4_qsize_txq = 128;
12907 
12908 	if (t4_qsize_rxq < 128)
12909 		t4_qsize_rxq = 128;
12910 	while (t4_qsize_rxq & 7)
12911 		t4_qsize_rxq++;
12912 
12913 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
12914 
12915 	/*
12916 	 * Number of VIs to create per-port.  The first VI is the "main" regular
12917 	 * VI for the port.  The rest are additional virtual interfaces on the
12918 	 * same physical port.  Note that the main VI does not have native
12919 	 * netmap support but the extra VIs do.
12920 	 *
12921 	 * Limit the number of VIs per port to the number of available
12922 	 * MAC addresses per port.
12923 	 */
12924 	if (t4_num_vis < 1)
12925 		t4_num_vis = 1;
12926 	if (t4_num_vis > nitems(vi_mac_funcs)) {
12927 		t4_num_vis = nitems(vi_mac_funcs);
12928 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
12929 	}
12930 
12931 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
12932 		pcie_relaxed_ordering = 1;
12933 #if defined(__i386__) || defined(__amd64__)
12934 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
12935 			pcie_relaxed_ordering = 0;
12936 #endif
12937 	}
12938 }
12939 
12940 #ifdef DDB
12941 static void
t4_dump_mem(struct adapter * sc,u_int addr,u_int len)12942 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
12943 {
12944 	uint32_t base, j, off, pf, reg, save, win_pos;
12945 
12946 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
12947 	save = t4_read_reg(sc, reg);
12948 	base = sc->memwin[2].mw_base;
12949 
12950 	if (is_t4(sc)) {
12951 		pf = 0;
12952 		win_pos = addr & ~0xf;	/* start must be 16B aligned */
12953 	} else {
12954 		pf = V_PFNUM(sc->pf);
12955 		win_pos = addr & ~0x7f;	/* start must be 128B aligned */
12956 	}
12957 	off = addr - win_pos;
12958 	t4_write_reg(sc, reg, win_pos | pf);
12959 	t4_read_reg(sc, reg);
12960 
12961 	while (len > 0 && !db_pager_quit) {
12962 		uint32_t buf[8];
12963 		for (j = 0; j < 8; j++, off += 4)
12964 			buf[j] = htonl(t4_read_reg(sc, base + off));
12965 
12966 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
12967 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12968 		    buf[7]);
12969 		if (len <= sizeof(buf))
12970 			len = 0;
12971 		else
12972 			len -= sizeof(buf);
12973 	}
12974 
12975 	t4_write_reg(sc, reg, save);
12976 	t4_read_reg(sc, reg);
12977 }
12978 
12979 static void
t4_dump_tcb(struct adapter * sc,int tid)12980 t4_dump_tcb(struct adapter *sc, int tid)
12981 {
12982 	uint32_t tcb_addr;
12983 
12984 	/* Dump TCB for the tid */
12985 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
12986 	tcb_addr += tid * TCB_SIZE;
12987 	t4_dump_mem(sc, tcb_addr, TCB_SIZE);
12988 }
12989 
12990 static void
t4_dump_devlog(struct adapter * sc)12991 t4_dump_devlog(struct adapter *sc)
12992 {
12993 	struct devlog_params *dparams = &sc->params.devlog;
12994 	struct fw_devlog_e e;
12995 	int i, first, j, m, nentries, rc;
12996 	uint64_t ftstamp = UINT64_MAX;
12997 
12998 	if (dparams->start == 0) {
12999 		db_printf("devlog params not valid\n");
13000 		return;
13001 	}
13002 
13003 	nentries = dparams->size / sizeof(struct fw_devlog_e);
13004 	m = fwmtype_to_hwmtype(dparams->memtype);
13005 
13006 	/* Find the first entry. */
13007 	first = -1;
13008 	for (i = 0; i < nentries && !db_pager_quit; i++) {
13009 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
13010 		    sizeof(e), (void *)&e);
13011 		if (rc != 0)
13012 			break;
13013 
13014 		if (e.timestamp == 0)
13015 			break;
13016 
13017 		e.timestamp = be64toh(e.timestamp);
13018 		if (e.timestamp < ftstamp) {
13019 			ftstamp = e.timestamp;
13020 			first = i;
13021 		}
13022 	}
13023 
13024 	if (first == -1)
13025 		return;
13026 
13027 	i = first;
13028 	do {
13029 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
13030 		    sizeof(e), (void *)&e);
13031 		if (rc != 0)
13032 			return;
13033 
13034 		if (e.timestamp == 0)
13035 			return;
13036 
13037 		e.timestamp = be64toh(e.timestamp);
13038 		e.seqno = be32toh(e.seqno);
13039 		for (j = 0; j < 8; j++)
13040 			e.params[j] = be32toh(e.params[j]);
13041 
13042 		db_printf("%10d  %15ju  %8s  %8s  ",
13043 		    e.seqno, e.timestamp,
13044 		    (e.level < nitems(devlog_level_strings) ?
13045 			devlog_level_strings[e.level] : "UNKNOWN"),
13046 		    (e.facility < nitems(devlog_facility_strings) ?
13047 			devlog_facility_strings[e.facility] : "UNKNOWN"));
13048 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
13049 		    e.params[3], e.params[4], e.params[5], e.params[6],
13050 		    e.params[7]);
13051 
13052 		if (++i == nentries)
13053 			i = 0;
13054 	} while (i != first && !db_pager_quit);
13055 }
13056 
13057 static DB_DEFINE_TABLE(show, t4, show_t4);
13058 
DB_TABLE_COMMAND_FLAGS(show_t4,devlog,db_show_devlog,CS_OWN)13059 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
13060 {
13061 	device_t dev;
13062 	int t;
13063 	bool valid;
13064 
13065 	valid = false;
13066 	t = db_read_token();
13067 	if (t == tIDENT) {
13068 		dev = device_lookup_by_name(db_tok_string);
13069 		valid = true;
13070 	}
13071 	db_skip_to_eol();
13072 	if (!valid) {
13073 		db_printf("usage: show t4 devlog <nexus>\n");
13074 		return;
13075 	}
13076 
13077 	if (dev == NULL) {
13078 		db_printf("device not found\n");
13079 		return;
13080 	}
13081 
13082 	t4_dump_devlog(device_get_softc(dev));
13083 }
13084 
DB_TABLE_COMMAND_FLAGS(show_t4,tcb,db_show_t4tcb,CS_OWN)13085 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
13086 {
13087 	device_t dev;
13088 	int radix, tid, t;
13089 	bool valid;
13090 
13091 	valid = false;
13092 	radix = db_radix;
13093 	db_radix = 10;
13094 	t = db_read_token();
13095 	if (t == tIDENT) {
13096 		dev = device_lookup_by_name(db_tok_string);
13097 		t = db_read_token();
13098 		if (t == tNUMBER) {
13099 			tid = db_tok_number;
13100 			valid = true;
13101 		}
13102 	}
13103 	db_radix = radix;
13104 	db_skip_to_eol();
13105 	if (!valid) {
13106 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
13107 		return;
13108 	}
13109 
13110 	if (dev == NULL) {
13111 		db_printf("device not found\n");
13112 		return;
13113 	}
13114 	if (tid < 0) {
13115 		db_printf("invalid tid\n");
13116 		return;
13117 	}
13118 
13119 	t4_dump_tcb(device_get_softc(dev), tid);
13120 }
13121 
DB_TABLE_COMMAND_FLAGS(show_t4,memdump,db_show_memdump,CS_OWN)13122 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
13123 {
13124 	device_t dev;
13125 	int radix, t;
13126 	bool valid;
13127 
13128 	valid = false;
13129 	radix = db_radix;
13130 	db_radix = 10;
13131 	t = db_read_token();
13132 	if (t == tIDENT) {
13133 		dev = device_lookup_by_name(db_tok_string);
13134 		t = db_read_token();
13135 		if (t == tNUMBER) {
13136 			addr = db_tok_number;
13137 			t = db_read_token();
13138 			if (t == tNUMBER) {
13139 				count = db_tok_number;
13140 				valid = true;
13141 			}
13142 		}
13143 	}
13144 	db_radix = radix;
13145 	db_skip_to_eol();
13146 	if (!valid) {
13147 		db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
13148 		return;
13149 	}
13150 
13151 	if (dev == NULL) {
13152 		db_printf("device not found\n");
13153 		return;
13154 	}
13155 	if (addr < 0) {
13156 		db_printf("invalid address\n");
13157 		return;
13158 	}
13159 	if (count <= 0) {
13160 		db_printf("invalid length\n");
13161 		return;
13162 	}
13163 
13164 	t4_dump_mem(device_get_softc(dev), addr, count);
13165 }
13166 #endif
13167 
13168 static eventhandler_tag vxlan_start_evtag;
13169 static eventhandler_tag vxlan_stop_evtag;
13170 
13171 struct vxlan_evargs {
13172 	if_t ifp;
13173 	uint16_t port;
13174 };
13175 
13176 static void
enable_vxlan_rx(struct adapter * sc)13177 enable_vxlan_rx(struct adapter *sc)
13178 {
13179 	int i, rc;
13180 	struct port_info *pi;
13181 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
13182 
13183 	ASSERT_SYNCHRONIZED_OP(sc);
13184 
13185 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
13186 	    F_VXLAN_EN);
13187 	for_each_port(sc, i) {
13188 		pi = sc->port[i];
13189 		if (pi->vxlan_tcam_entry == true)
13190 			continue;
13191 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
13192 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
13193 		    true);
13194 		if (rc < 0) {
13195 			rc = -rc;
13196 			CH_ERR(&pi->vi[0],
13197 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
13198 		} else {
13199 			MPASS(rc == sc->rawf_base + pi->port_id);
13200 			pi->vxlan_tcam_entry = true;
13201 		}
13202 	}
13203 }
13204 
13205 static void
t4_vxlan_start(struct adapter * sc,void * arg)13206 t4_vxlan_start(struct adapter *sc, void *arg)
13207 {
13208 	struct vxlan_evargs *v = arg;
13209 
13210 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13211 		return;
13212 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
13213 		return;
13214 
13215 	if (sc->vxlan_refcount == 0) {
13216 		sc->vxlan_port = v->port;
13217 		sc->vxlan_refcount = 1;
13218 		if (!hw_off_limits(sc))
13219 			enable_vxlan_rx(sc);
13220 	} else if (sc->vxlan_port == v->port) {
13221 		sc->vxlan_refcount++;
13222 	} else {
13223 		CH_ERR(sc, "VXLAN already configured on port  %d; "
13224 		    "ignoring attempt to configure it on port %d\n",
13225 		    sc->vxlan_port, v->port);
13226 	}
13227 	end_synchronized_op(sc, 0);
13228 }
13229 
13230 static void
t4_vxlan_stop(struct adapter * sc,void * arg)13231 t4_vxlan_stop(struct adapter *sc, void *arg)
13232 {
13233 	struct vxlan_evargs *v = arg;
13234 
13235 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13236 		return;
13237 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
13238 		return;
13239 
13240 	/*
13241 	 * VXLANs may have been configured before the driver was loaded so we
13242 	 * may see more stops than starts.  This is not handled cleanly but at
13243 	 * least we keep the refcount sane.
13244 	 */
13245 	if (sc->vxlan_port != v->port)
13246 		goto done;
13247 	if (sc->vxlan_refcount == 0) {
13248 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
13249 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
13250 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
13251 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
13252 done:
13253 	end_synchronized_op(sc, 0);
13254 }
13255 
13256 static void
t4_vxlan_start_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13257 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
13258     sa_family_t family, u_int port)
13259 {
13260 	struct vxlan_evargs v;
13261 
13262 	MPASS(family == AF_INET || family == AF_INET6);
13263 	v.ifp = ifp;
13264 	v.port = port;
13265 
13266 	t4_iterate(t4_vxlan_start, &v);
13267 }
13268 
13269 static void
t4_vxlan_stop_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13270 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
13271     u_int port)
13272 {
13273 	struct vxlan_evargs v;
13274 
13275 	MPASS(family == AF_INET || family == AF_INET6);
13276 	v.ifp = ifp;
13277 	v.port = port;
13278 
13279 	t4_iterate(t4_vxlan_stop, &v);
13280 }
13281 
13282 
13283 static struct sx mlu;	/* mod load unload */
13284 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
13285 
13286 static int
mod_event(module_t mod,int cmd,void * arg)13287 mod_event(module_t mod, int cmd, void *arg)
13288 {
13289 	int rc = 0;
13290 	static int loaded = 0;
13291 
13292 	switch (cmd) {
13293 	case MOD_LOAD:
13294 		sx_xlock(&mlu);
13295 		if (loaded++ == 0) {
13296 			t4_sge_modload();
13297 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13298 			    t4_filter_rpl, CPL_COOKIE_FILTER);
13299 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
13300 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
13301 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
13302 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
13303 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13304 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
13305 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
13306 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
13307 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
13308 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
13309 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
13310 			    do_smt_write_rpl);
13311 			sx_init(&t4_list_lock, "T4/T5 adapters");
13312 			SLIST_INIT(&t4_list);
13313 			callout_init(&fatal_callout, 1);
13314 #ifdef TCP_OFFLOAD
13315 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
13316 #endif
13317 #ifdef INET6
13318 			t4_clip_modload();
13319 #endif
13320 #ifdef KERN_TLS
13321 			t6_ktls_modload();
13322 #endif
13323 			t4_tracer_modload();
13324 			tweak_tunables();
13325 			vxlan_start_evtag =
13326 			    EVENTHANDLER_REGISTER(vxlan_start,
13327 				t4_vxlan_start_handler, NULL,
13328 				EVENTHANDLER_PRI_ANY);
13329 			vxlan_stop_evtag =
13330 			    EVENTHANDLER_REGISTER(vxlan_stop,
13331 				t4_vxlan_stop_handler, NULL,
13332 				EVENTHANDLER_PRI_ANY);
13333 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
13334 			    taskqueue_thread_enqueue, &reset_tq);
13335 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
13336 			    "t4_rst_thr");
13337 		}
13338 		sx_xunlock(&mlu);
13339 		break;
13340 
13341 	case MOD_UNLOAD:
13342 		sx_xlock(&mlu);
13343 		if (--loaded == 0) {
13344 #ifdef TCP_OFFLOAD
13345 			int i;
13346 #endif
13347 			int tries;
13348 
13349 			taskqueue_free(reset_tq);
13350 
13351 			tries = 0;
13352 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
13353 				uprintf("%ju clusters with custom free routine "
13354 				    "still is use.\n", t4_sge_extfree_refs());
13355 				pause("t4unload", 2 * hz);
13356 			}
13357 
13358 			sx_slock(&t4_list_lock);
13359 			if (!SLIST_EMPTY(&t4_list)) {
13360 				rc = EBUSY;
13361 				sx_sunlock(&t4_list_lock);
13362 				goto done_unload;
13363 			}
13364 #ifdef TCP_OFFLOAD
13365 			sx_slock(&t4_uld_list_lock);
13366 			for (i = 0; i <= ULD_MAX; i++) {
13367 				if (t4_uld_list[i] != NULL) {
13368 					rc = EBUSY;
13369 					sx_sunlock(&t4_uld_list_lock);
13370 					sx_sunlock(&t4_list_lock);
13371 					goto done_unload;
13372 				}
13373 			}
13374 			sx_sunlock(&t4_uld_list_lock);
13375 #endif
13376 			sx_sunlock(&t4_list_lock);
13377 
13378 			if (t4_sge_extfree_refs() == 0) {
13379 				EVENTHANDLER_DEREGISTER(vxlan_start,
13380 				    vxlan_start_evtag);
13381 				EVENTHANDLER_DEREGISTER(vxlan_stop,
13382 				    vxlan_stop_evtag);
13383 				t4_tracer_modunload();
13384 #ifdef KERN_TLS
13385 				t6_ktls_modunload();
13386 #endif
13387 #ifdef INET6
13388 				t4_clip_modunload();
13389 #endif
13390 #ifdef TCP_OFFLOAD
13391 				sx_destroy(&t4_uld_list_lock);
13392 #endif
13393 				sx_destroy(&t4_list_lock);
13394 				t4_sge_modunload();
13395 				loaded = 0;
13396 			} else {
13397 				rc = EBUSY;
13398 				loaded++;	/* undo earlier decrement */
13399 			}
13400 		}
13401 done_unload:
13402 		sx_xunlock(&mlu);
13403 		break;
13404 	}
13405 
13406 	return (rc);
13407 }
13408 
13409 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
13410 MODULE_VERSION(t4nex, 1);
13411 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
13412 #ifdef DEV_NETMAP
13413 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
13414 #endif /* DEV_NETMAP */
13415 
13416 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
13417 MODULE_VERSION(t5nex, 1);
13418 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
13419 #ifdef DEV_NETMAP
13420 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
13421 #endif /* DEV_NETMAP */
13422 
13423 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
13424 MODULE_VERSION(t6nex, 1);
13425 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
13426 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
13427 #ifdef DEV_NETMAP
13428 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
13429 #endif /* DEV_NETMAP */
13430 
13431 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
13432 MODULE_VERSION(cxgbe, 1);
13433 
13434 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
13435 MODULE_VERSION(cxl, 1);
13436 
13437 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
13438 MODULE_VERSION(cc, 1);
13439 
13440 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
13441 MODULE_VERSION(vcxgbe, 1);
13442 
13443 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
13444 MODULE_VERSION(vcxl, 1);
13445 
13446 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
13447 MODULE_VERSION(vcc, 1);
13448