xref: /freebsd-14-stable/sys/dev/cxgbe/t4_main.c (revision 47cdd7a9ebc6f8ced53c57cd6d3c322c453caad8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_ddb.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_kern_tls.h"
35 #include "opt_ratelimit.h"
36 #include "opt_rss.h"
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/priv.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/eventhandler.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/taskqueue.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <sys/firmware.h>
51 #include <sys/sbuf.h>
52 #include <sys/smp.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <net/ethernet.h>
57 #include <net/if.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/if_vlan_var.h>
61 #ifdef RSS
62 #include <net/rss_config.h>
63 #endif
64 #include <netinet/in.h>
65 #include <netinet/ip.h>
66 #ifdef KERN_TLS
67 #include <netinet/tcp_seq.h>
68 #endif
69 #if defined(__i386__) || defined(__amd64__)
70 #include <machine/md_var.h>
71 #include <machine/cputypes.h>
72 #include <vm/vm.h>
73 #include <vm/pmap.h>
74 #endif
75 #ifdef DDB
76 #include <ddb/ddb.h>
77 #include <ddb/db_lex.h>
78 #endif
79 
80 #include "common/common.h"
81 #include "common/t4_msg.h"
82 #include "common/t4_regs.h"
83 #include "common/t4_regs_values.h"
84 #include "cudbg/cudbg.h"
85 #include "t4_clip.h"
86 #include "t4_ioctl.h"
87 #include "t4_l2t.h"
88 #include "t4_mp_ring.h"
89 #include "t4_if.h"
90 #include "t4_smt.h"
91 
92 /* T4 bus driver interface */
93 static int t4_probe(device_t);
94 static int t4_attach(device_t);
95 static int t4_detach(device_t);
96 static int t4_child_location(device_t, device_t, struct sbuf *);
97 static int t4_ready(device_t);
98 static int t4_read_port_device(device_t, int, device_t *);
99 static int t4_suspend(device_t);
100 static int t4_resume(device_t);
101 static int t4_reset_prepare(device_t, device_t);
102 static int t4_reset_post(device_t, device_t);
103 static device_method_t t4_methods[] = {
104 	DEVMETHOD(device_probe,		t4_probe),
105 	DEVMETHOD(device_attach,	t4_attach),
106 	DEVMETHOD(device_detach,	t4_detach),
107 	DEVMETHOD(device_suspend,	t4_suspend),
108 	DEVMETHOD(device_resume,	t4_resume),
109 
110 	DEVMETHOD(bus_child_location,	t4_child_location),
111 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
112 	DEVMETHOD(bus_reset_post,	t4_reset_post),
113 
114 	DEVMETHOD(t4_is_main_ready,	t4_ready),
115 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
116 
117 	DEVMETHOD_END
118 };
119 static driver_t t4_driver = {
120 	"t4nex",
121 	t4_methods,
122 	sizeof(struct adapter)
123 };
124 
125 
126 /* T4 port (cxgbe) interface */
127 static int cxgbe_probe(device_t);
128 static int cxgbe_attach(device_t);
129 static int cxgbe_detach(device_t);
130 device_method_t cxgbe_methods[] = {
131 	DEVMETHOD(device_probe,		cxgbe_probe),
132 	DEVMETHOD(device_attach,	cxgbe_attach),
133 	DEVMETHOD(device_detach,	cxgbe_detach),
134 	{ 0, 0 }
135 };
136 static driver_t cxgbe_driver = {
137 	"cxgbe",
138 	cxgbe_methods,
139 	sizeof(struct port_info)
140 };
141 
142 /* T4 VI (vcxgbe) interface */
143 static int vcxgbe_probe(device_t);
144 static int vcxgbe_attach(device_t);
145 static int vcxgbe_detach(device_t);
146 static device_method_t vcxgbe_methods[] = {
147 	DEVMETHOD(device_probe,		vcxgbe_probe),
148 	DEVMETHOD(device_attach,	vcxgbe_attach),
149 	DEVMETHOD(device_detach,	vcxgbe_detach),
150 	{ 0, 0 }
151 };
152 static driver_t vcxgbe_driver = {
153 	"vcxgbe",
154 	vcxgbe_methods,
155 	sizeof(struct vi_info)
156 };
157 
158 static d_ioctl_t t4_ioctl;
159 
160 static struct cdevsw t4_cdevsw = {
161        .d_version = D_VERSION,
162        .d_ioctl = t4_ioctl,
163        .d_name = "t4nex",
164 };
165 
166 /* T5 bus driver interface */
167 static int t5_probe(device_t);
168 static device_method_t t5_methods[] = {
169 	DEVMETHOD(device_probe,		t5_probe),
170 	DEVMETHOD(device_attach,	t4_attach),
171 	DEVMETHOD(device_detach,	t4_detach),
172 	DEVMETHOD(device_suspend,	t4_suspend),
173 	DEVMETHOD(device_resume,	t4_resume),
174 
175 	DEVMETHOD(bus_child_location,	t4_child_location),
176 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
177 	DEVMETHOD(bus_reset_post,	t4_reset_post),
178 
179 	DEVMETHOD(t4_is_main_ready,	t4_ready),
180 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
181 
182 	DEVMETHOD_END
183 };
184 static driver_t t5_driver = {
185 	"t5nex",
186 	t5_methods,
187 	sizeof(struct adapter)
188 };
189 
190 
191 /* T5 port (cxl) interface */
192 static driver_t cxl_driver = {
193 	"cxl",
194 	cxgbe_methods,
195 	sizeof(struct port_info)
196 };
197 
198 /* T5 VI (vcxl) interface */
199 static driver_t vcxl_driver = {
200 	"vcxl",
201 	vcxgbe_methods,
202 	sizeof(struct vi_info)
203 };
204 
205 /* T6 bus driver interface */
206 static int t6_probe(device_t);
207 static device_method_t t6_methods[] = {
208 	DEVMETHOD(device_probe,		t6_probe),
209 	DEVMETHOD(device_attach,	t4_attach),
210 	DEVMETHOD(device_detach,	t4_detach),
211 	DEVMETHOD(device_suspend,	t4_suspend),
212 	DEVMETHOD(device_resume,	t4_resume),
213 
214 	DEVMETHOD(bus_child_location,	t4_child_location),
215 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
216 	DEVMETHOD(bus_reset_post,	t4_reset_post),
217 
218 	DEVMETHOD(t4_is_main_ready,	t4_ready),
219 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
220 
221 	DEVMETHOD_END
222 };
223 static driver_t t6_driver = {
224 	"t6nex",
225 	t6_methods,
226 	sizeof(struct adapter)
227 };
228 
229 
230 /* T6 port (cc) interface */
231 static driver_t cc_driver = {
232 	"cc",
233 	cxgbe_methods,
234 	sizeof(struct port_info)
235 };
236 
237 /* T6 VI (vcc) interface */
238 static driver_t vcc_driver = {
239 	"vcc",
240 	vcxgbe_methods,
241 	sizeof(struct vi_info)
242 };
243 
244 /* ifnet interface */
245 static void cxgbe_init(void *);
246 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
247 static int cxgbe_transmit(if_t, struct mbuf *);
248 static void cxgbe_qflush(if_t);
249 #if defined(KERN_TLS) || defined(RATELIMIT)
250 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
251     struct m_snd_tag **);
252 #endif
253 
254 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
255 
256 /*
257  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
258  * then ADAPTER_LOCK, then t4_uld_list_lock.
259  */
260 static struct sx t4_list_lock;
261 SLIST_HEAD(, adapter) t4_list;
262 #ifdef TCP_OFFLOAD
263 static struct sx t4_uld_list_lock;
264 struct uld_info *t4_uld_list[ULD_MAX + 1];
265 #endif
266 
267 /*
268  * Tunables.  See tweak_tunables() too.
269  *
270  * Each tunable is set to a default value here if it's known at compile-time.
271  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
272  * provide a reasonable default (upto n) when the driver is loaded.
273  *
274  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
275  * T5 are under hw.cxl.
276  */
277 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
278     "cxgbe(4) parameters");
279 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
280     "cxgbe(4) T5+ parameters");
281 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
282     "cxgbe(4) TOE parameters");
283 
284 /*
285  * Number of queues for tx and rx, NIC and offload.
286  */
287 #define NTXQ 16
288 int t4_ntxq = -NTXQ;
289 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
290     "Number of TX queues per port");
291 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
292 
293 #define NRXQ 8
294 int t4_nrxq = -NRXQ;
295 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
296     "Number of RX queues per port");
297 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
298 
299 #define NTXQ_VI 1
300 static int t4_ntxq_vi = -NTXQ_VI;
301 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
302     "Number of TX queues per VI");
303 
304 #define NRXQ_VI 1
305 static int t4_nrxq_vi = -NRXQ_VI;
306 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
307     "Number of RX queues per VI");
308 
309 static int t4_rsrv_noflowq = 0;
310 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
311     0, "Reserve TX queue 0 of each VI for non-flowid packets");
312 
313 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
314 #define NOFLDTXQ 8
315 static int t4_nofldtxq = -NOFLDTXQ;
316 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
317     "Number of offload TX queues per port");
318 
319 #define NOFLDTXQ_VI 1
320 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
321 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
322     "Number of offload TX queues per VI");
323 #endif
324 
325 #if defined(TCP_OFFLOAD)
326 #define NOFLDRXQ 2
327 static int t4_nofldrxq = -NOFLDRXQ;
328 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
329     "Number of offload RX queues per port");
330 
331 #define NOFLDRXQ_VI 1
332 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
333 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
334     "Number of offload RX queues per VI");
335 
336 #define TMR_IDX_OFLD 1
337 static int t4_tmr_idx_ofld = TMR_IDX_OFLD;
338 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
339     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
340 
341 #define PKTC_IDX_OFLD (-1)
342 static int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
343 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
344     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
345 
346 /* 0 means chip/fw default, non-zero number is value in microseconds */
347 static u_long t4_toe_keepalive_idle = 0;
348 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
349     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
350 
351 /* 0 means chip/fw default, non-zero number is value in microseconds */
352 static u_long t4_toe_keepalive_interval = 0;
353 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
354     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
355 
356 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
357 static int t4_toe_keepalive_count = 0;
358 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
359     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
360 
361 /* 0 means chip/fw default, non-zero number is value in microseconds */
362 static u_long t4_toe_rexmt_min = 0;
363 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
364     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
365 
366 /* 0 means chip/fw default, non-zero number is value in microseconds */
367 static u_long t4_toe_rexmt_max = 0;
368 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
369     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
370 
371 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
372 static int t4_toe_rexmt_count = 0;
373 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
374     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
375 
376 /* -1 means chip/fw default, other values are raw backoff values to use */
377 static int t4_toe_rexmt_backoff[16] = {
378 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
379 };
380 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
381     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
382     "cxgbe(4) TOE retransmit backoff values");
383 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
384     &t4_toe_rexmt_backoff[0], 0, "");
385 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
386     &t4_toe_rexmt_backoff[1], 0, "");
387 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
388     &t4_toe_rexmt_backoff[2], 0, "");
389 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
390     &t4_toe_rexmt_backoff[3], 0, "");
391 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
392     &t4_toe_rexmt_backoff[4], 0, "");
393 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
394     &t4_toe_rexmt_backoff[5], 0, "");
395 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
396     &t4_toe_rexmt_backoff[6], 0, "");
397 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
398     &t4_toe_rexmt_backoff[7], 0, "");
399 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
400     &t4_toe_rexmt_backoff[8], 0, "");
401 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
402     &t4_toe_rexmt_backoff[9], 0, "");
403 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
404     &t4_toe_rexmt_backoff[10], 0, "");
405 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
406     &t4_toe_rexmt_backoff[11], 0, "");
407 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
408     &t4_toe_rexmt_backoff[12], 0, "");
409 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
410     &t4_toe_rexmt_backoff[13], 0, "");
411 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
412     &t4_toe_rexmt_backoff[14], 0, "");
413 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
414     &t4_toe_rexmt_backoff[15], 0, "");
415 
416 int t4_ddp_rcvbuf_len = 256 * 1024;
417 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_len, CTLFLAG_RWTUN,
418     &t4_ddp_rcvbuf_len, 0, "length of each DDP RX buffer");
419 
420 unsigned int t4_ddp_rcvbuf_cache = 4;
421 SYSCTL_UINT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_cache, CTLFLAG_RWTUN,
422     &t4_ddp_rcvbuf_cache, 0,
423     "maximum number of free DDP RX buffers to cache per connection");
424 #endif
425 
426 #ifdef DEV_NETMAP
427 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
428 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
429 static int t4_native_netmap = NN_EXTRA_VI;
430 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
431     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
432 
433 #define NNMTXQ 8
434 static int t4_nnmtxq = -NNMTXQ;
435 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
436     "Number of netmap TX queues");
437 
438 #define NNMRXQ 8
439 static int t4_nnmrxq = -NNMRXQ;
440 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
441     "Number of netmap RX queues");
442 
443 #define NNMTXQ_VI 2
444 static int t4_nnmtxq_vi = -NNMTXQ_VI;
445 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
446     "Number of netmap TX queues per VI");
447 
448 #define NNMRXQ_VI 2
449 static int t4_nnmrxq_vi = -NNMRXQ_VI;
450 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
451     "Number of netmap RX queues per VI");
452 #endif
453 
454 /*
455  * Holdoff parameters for ports.
456  */
457 #define TMR_IDX 1
458 int t4_tmr_idx = TMR_IDX;
459 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
460     0, "Holdoff timer index");
461 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
462 
463 #define PKTC_IDX (-1)
464 int t4_pktc_idx = PKTC_IDX;
465 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
466     0, "Holdoff packet counter index");
467 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
468 
469 /*
470  * Size (# of entries) of each tx and rx queue.
471  */
472 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
473 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
474     "Number of descriptors in each TX queue");
475 
476 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
477 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
478     "Number of descriptors in each RX queue");
479 
480 /*
481  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
482  */
483 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
484 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
485     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
486 
487 /*
488  * Configuration file.  All the _CF names here are special.
489  */
490 #define DEFAULT_CF	"default"
491 #define BUILTIN_CF	"built-in"
492 #define FLASH_CF	"flash"
493 #define UWIRE_CF	"uwire"
494 #define FPGA_CF		"fpga"
495 static char t4_cfg_file[32] = DEFAULT_CF;
496 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
497     sizeof(t4_cfg_file), "Firmware configuration file");
498 
499 /*
500  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
501  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
502  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
503  *            mark or when signalled to do so, 0 to never emit PAUSE.
504  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
505  *                 negotiated settings will override rx_pause/tx_pause.
506  *                 Otherwise rx_pause/tx_pause are applied forcibly.
507  */
508 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
509 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
510     &t4_pause_settings, 0,
511     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
512 
513 /*
514  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
515  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
516  *  0 to disable FEC.
517  */
518 static int t4_fec = -1;
519 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
520     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
521 
522 /*
523  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
524  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
525  * driver runs as if this is set to 0.
526  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
527  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
528  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
529  *    the firmware anyway (may result in l1cfg errors with old firmwares).
530  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
531  *    means set all FEC bits that are valid for the speed.
532  */
533 static int t4_force_fec = -1;
534 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
535     "Controls the use of FORCE_FEC bit in L1 configuration.");
536 
537 /*
538  * Link autonegotiation.
539  * -1 to run with the firmware default.
540  *  0 to disable.
541  *  1 to enable.
542  */
543 static int t4_autoneg = -1;
544 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
545     "Link autonegotiation");
546 
547 /*
548  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
549  * encouraged respectively).  '-n' is the same as 'n' except the firmware
550  * version used in the checks is read from the firmware bundled with the driver.
551  */
552 static int t4_fw_install = 1;
553 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
554     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
555 
556 /*
557  * ASIC features that will be used.  Disable the ones you don't want so that the
558  * chip resources aren't wasted on features that will not be used.
559  */
560 static int t4_nbmcaps_allowed = 0;
561 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
562     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
563 
564 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
565 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
566     &t4_linkcaps_allowed, 0, "Default link capabilities");
567 
568 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
569     FW_CAPS_CONFIG_SWITCH_EGRESS;
570 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
571     &t4_switchcaps_allowed, 0, "Default switch capabilities");
572 
573 #ifdef RATELIMIT
574 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
575 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
576 #else
577 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
578 	FW_CAPS_CONFIG_NIC_HASHFILTER;
579 #endif
580 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
581     &t4_niccaps_allowed, 0, "Default NIC capabilities");
582 
583 static int t4_toecaps_allowed = -1;
584 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
585     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
586 
587 static int t4_rdmacaps_allowed = -1;
588 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
589     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
590 
591 static int t4_cryptocaps_allowed = -1;
592 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
593     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
594 
595 static int t4_iscsicaps_allowed = -1;
596 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
597     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
598 
599 static int t4_fcoecaps_allowed = 0;
600 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
601     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
602 
603 static int t5_write_combine = 0;
604 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
605     0, "Use WC instead of UC for BAR2");
606 
607 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
608 static int t4_doorbells_allowed = 0xf;
609 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
610 	   &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
611 
612 static int t4_num_vis = 1;
613 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
614     "Number of VIs per port");
615 
616 /*
617  * PCIe Relaxed Ordering.
618  * -1: driver should figure out a good value.
619  * 0: disable RO.
620  * 1: enable RO.
621  * 2: leave RO alone.
622  */
623 static int pcie_relaxed_ordering = -1;
624 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
625     &pcie_relaxed_ordering, 0,
626     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
627 
628 static int t4_panic_on_fatal_err = 0;
629 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
630     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
631 
632 static int t4_reset_on_fatal_err = 0;
633 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
634     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
635 
636 static int t4_reset_method = 1;
637 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_method, CTLFLAG_RWTUN, &t4_reset_method,
638     0, "reset method: 0 = PL_RST, 1 = PCIe secondary bus reset, 2 = PCIe link bounce");
639 
640 static int t4_clock_gate_on_suspend = 0;
641 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
642     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
643 
644 static int t4_tx_vm_wr = 0;
645 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
646     "Use VM work requests to transmit packets.");
647 
648 /*
649  * Set to non-zero to enable the attack filter.  A packet that matches any of
650  * these conditions will get dropped on ingress:
651  * 1) IP && source address == destination address.
652  * 2) TCP/IP && source address is not a unicast address.
653  * 3) TCP/IP && destination address is not a unicast address.
654  * 4) IP && source address is loopback (127.x.y.z).
655  * 5) IP && destination address is loopback (127.x.y.z).
656  * 6) IPv6 && source address == destination address.
657  * 7) IPv6 && source address is not a unicast address.
658  * 8) IPv6 && source address is loopback (::1/128).
659  * 9) IPv6 && destination address is loopback (::1/128).
660  * 10) IPv6 && source address is unspecified (::/128).
661  * 11) IPv6 && destination address is unspecified (::/128).
662  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
663  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
664  */
665 static int t4_attack_filter = 0;
666 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
667     &t4_attack_filter, 0, "Drop suspicious traffic");
668 
669 static int t4_drop_ip_fragments = 0;
670 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
671     &t4_drop_ip_fragments, 0, "Drop IP fragments");
672 
673 static int t4_drop_pkts_with_l2_errors = 1;
674 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
675     &t4_drop_pkts_with_l2_errors, 0,
676     "Drop all frames with Layer 2 length or checksum errors");
677 
678 static int t4_drop_pkts_with_l3_errors = 0;
679 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
680     &t4_drop_pkts_with_l3_errors, 0,
681     "Drop all frames with IP version, length, or checksum errors");
682 
683 static int t4_drop_pkts_with_l4_errors = 0;
684 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
685     &t4_drop_pkts_with_l4_errors, 0,
686     "Drop all frames with Layer 4 length, checksum, or other errors");
687 
688 #ifdef TCP_OFFLOAD
689 /*
690  * TOE tunables.
691  */
692 static int t4_cop_managed_offloading = 0;
693 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
694     &t4_cop_managed_offloading, 0,
695     "COP (Connection Offload Policy) controls all TOE offload");
696 TUNABLE_INT("hw.cxgbe.cop_managed_offloading", &t4_cop_managed_offloading);
697 #endif
698 
699 #ifdef KERN_TLS
700 /*
701  * This enables KERN_TLS for all adapters if set.
702  */
703 static int t4_kern_tls = 0;
704 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
705     "Enable KERN_TLS mode for T6 adapters");
706 
707 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
708     "cxgbe(4) KERN_TLS parameters");
709 
710 static int t4_tls_inline_keys = 0;
711 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
712     &t4_tls_inline_keys, 0,
713     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
714     "in card memory.");
715 
716 static int t4_tls_combo_wrs = 0;
717 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
718     0, "Attempt to combine TCB field updates with TLS record work requests.");
719 #endif
720 
721 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
722 static int vi_mac_funcs[] = {
723 	FW_VI_FUNC_ETH,
724 	FW_VI_FUNC_OFLD,
725 	FW_VI_FUNC_IWARP,
726 	FW_VI_FUNC_OPENISCSI,
727 	FW_VI_FUNC_OPENFCOE,
728 	FW_VI_FUNC_FOISCSI,
729 	FW_VI_FUNC_FOFCOE,
730 };
731 
732 struct intrs_and_queues {
733 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
734 	uint16_t num_vis;	/* number of VIs for each port */
735 	uint16_t nirq;		/* Total # of vectors */
736 	uint16_t ntxq;		/* # of NIC txq's for each port */
737 	uint16_t nrxq;		/* # of NIC rxq's for each port */
738 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
739 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
740 	uint16_t nnmtxq;	/* # of netmap txq's */
741 	uint16_t nnmrxq;	/* # of netmap rxq's */
742 
743 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
744 	uint16_t ntxq_vi;	/* # of NIC txq's */
745 	uint16_t nrxq_vi;	/* # of NIC rxq's */
746 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
747 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
748 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
749 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
750 };
751 
752 static void setup_memwin(struct adapter *);
753 static void position_memwin(struct adapter *, int, uint32_t);
754 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
755 static int fwmtype_to_hwmtype(int);
756 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
757     uint32_t *);
758 static int fixup_devlog_params(struct adapter *);
759 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
760 static int contact_firmware(struct adapter *);
761 static int partition_resources(struct adapter *);
762 static int get_params__pre_init(struct adapter *);
763 static int set_params__pre_init(struct adapter *);
764 static int get_params__post_init(struct adapter *);
765 static int set_params__post_init(struct adapter *);
766 static void t4_set_desc(struct adapter *);
767 static bool fixed_ifmedia(struct port_info *);
768 static void build_medialist(struct port_info *);
769 static void init_link_config(struct port_info *);
770 static int fixup_link_config(struct port_info *);
771 static int apply_link_config(struct port_info *);
772 static int cxgbe_init_synchronized(struct vi_info *);
773 static int cxgbe_uninit_synchronized(struct vi_info *);
774 static int adapter_full_init(struct adapter *);
775 static void adapter_full_uninit(struct adapter *);
776 static int vi_full_init(struct vi_info *);
777 static void vi_full_uninit(struct vi_info *);
778 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
779 static void quiesce_txq(struct sge_txq *);
780 static void quiesce_wrq(struct sge_wrq *);
781 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
782 static void quiesce_vi(struct vi_info *);
783 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
784     driver_intr_t *, void *, char *);
785 static int t4_free_irq(struct adapter *, struct irq *);
786 static void t4_init_atid_table(struct adapter *);
787 static void t4_free_atid_table(struct adapter *);
788 static void stop_atid_allocator(struct adapter *);
789 static void restart_atid_allocator(struct adapter *);
790 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
791 static void vi_refresh_stats(struct vi_info *);
792 static void cxgbe_refresh_stats(struct vi_info *);
793 static void cxgbe_tick(void *);
794 static void vi_tick(void *);
795 static void cxgbe_sysctls(struct port_info *);
796 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
797 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
798 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
799 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
800 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
801 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
802 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
803 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
804 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
805 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
806 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
807 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
808 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
809 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
810 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
811 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
812 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
813 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
814 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
815 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
816 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
817 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
818 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
819 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
820 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
821 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
822 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
823 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
824 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
825 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
826 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
827 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
828 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
829 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
830 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
831 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
832 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
833 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
834 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
835 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
836 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
837 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
838 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
839 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
840 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
841 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
842 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
843 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
844 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
845 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
846 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
847 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
848 #ifdef TCP_OFFLOAD
849 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
850 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
851 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
852 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
853 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
854 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
855 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
856 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
857 #endif
858 static int get_sge_context(struct adapter *, struct t4_sge_context *);
859 static int load_fw(struct adapter *, struct t4_data *);
860 static int load_cfg(struct adapter *, struct t4_data *);
861 static int load_boot(struct adapter *, struct t4_bootrom *);
862 static int load_bootcfg(struct adapter *, struct t4_data *);
863 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
864 static void free_offload_policy(struct t4_offload_policy *);
865 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
866 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
867 static int read_i2c(struct adapter *, struct t4_i2c_data *);
868 static int clear_stats(struct adapter *, u_int);
869 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
870 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
871 static inline int stop_adapter(struct adapter *);
872 static inline void set_adapter_hwstatus(struct adapter *, const bool);
873 static int stop_lld(struct adapter *);
874 static inline int restart_adapter(struct adapter *);
875 static int restart_lld(struct adapter *);
876 #ifdef TCP_OFFLOAD
877 static int deactivate_all_uld(struct adapter *);
878 static void stop_all_uld(struct adapter *);
879 static void restart_all_uld(struct adapter *);
880 #endif
881 #ifdef KERN_TLS
882 static int ktls_capability(struct adapter *, bool);
883 #endif
884 static int mod_event(module_t, int, void *);
885 static int notify_siblings(device_t, int);
886 static uint64_t vi_get_counter(if_t, ift_counter);
887 static uint64_t cxgbe_get_counter(if_t, ift_counter);
888 static void enable_vxlan_rx(struct adapter *);
889 static void reset_adapter_task(void *, int);
890 static void fatal_error_task(void *, int);
891 static void dump_devlog(struct adapter *);
892 static void dump_cim_regs(struct adapter *);
893 static void dump_cimla(struct adapter *);
894 
895 struct {
896 	uint16_t device;
897 	char *desc;
898 } t4_pciids[] = {
899 	{0xa000, "Chelsio Terminator 4 FPGA"},
900 	{0x4400, "Chelsio T440-dbg"},
901 	{0x4401, "Chelsio T420-CR"},
902 	{0x4402, "Chelsio T422-CR"},
903 	{0x4403, "Chelsio T440-CR"},
904 	{0x4404, "Chelsio T420-BCH"},
905 	{0x4405, "Chelsio T440-BCH"},
906 	{0x4406, "Chelsio T440-CH"},
907 	{0x4407, "Chelsio T420-SO"},
908 	{0x4408, "Chelsio T420-CX"},
909 	{0x4409, "Chelsio T420-BT"},
910 	{0x440a, "Chelsio T404-BT"},
911 	{0x440e, "Chelsio T440-LP-CR"},
912 }, t5_pciids[] = {
913 	{0xb000, "Chelsio Terminator 5 FPGA"},
914 	{0x5400, "Chelsio T580-dbg"},
915 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
916 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
917 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
918 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
919 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
920 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
921 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
922 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
923 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
924 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
925 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
926 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
927 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
928 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
929 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
930 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
931 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
932 
933 	/* Custom */
934 	{0x5483, "Custom T540-CR"},
935 	{0x5484, "Custom T540-BT"},
936 }, t6_pciids[] = {
937 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
938 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
939 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
940 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
941 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
942 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
943 	{0x6405, "Chelsio T6225-SO-OCP3"},	/* 2 x 10/25G, nomem */
944 	{0x6406, "Chelsio T6225-OCP3"},		/* 2 x 10/25G */
945 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
946 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
947 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
948 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
949 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
950 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
951 	{0x6414, "Chelsio T62100-SO-OCP3"},	/* 2 x 40/50/100G, nomem */
952 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
953 
954 	/* Custom */
955 	{0x6480, "Custom T6225-CR"},
956 	{0x6481, "Custom T62100-CR"},
957 	{0x6482, "Custom T6225-CR"},
958 	{0x6483, "Custom T62100-CR"},
959 	{0x6484, "Custom T64100-CR"},
960 	{0x6485, "Custom T6240-SO"},
961 	{0x6486, "Custom T6225-SO-CR"},
962 	{0x6487, "Custom T6225-CR"},
963 };
964 
965 #ifdef TCP_OFFLOAD
966 /*
967  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
968  * be exactly the same for both rxq and ofld_rxq.
969  */
970 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
971 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
972 #endif
973 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
974 
975 static int
t4_probe(device_t dev)976 t4_probe(device_t dev)
977 {
978 	int i;
979 	uint16_t v = pci_get_vendor(dev);
980 	uint16_t d = pci_get_device(dev);
981 	uint8_t f = pci_get_function(dev);
982 
983 	if (v != PCI_VENDOR_ID_CHELSIO)
984 		return (ENXIO);
985 
986 	/* Attach only to PF0 of the FPGA */
987 	if (d == 0xa000 && f != 0)
988 		return (ENXIO);
989 
990 	for (i = 0; i < nitems(t4_pciids); i++) {
991 		if (d == t4_pciids[i].device) {
992 			device_set_desc(dev, t4_pciids[i].desc);
993 			return (BUS_PROBE_DEFAULT);
994 		}
995 	}
996 
997 	return (ENXIO);
998 }
999 
1000 static int
t5_probe(device_t dev)1001 t5_probe(device_t dev)
1002 {
1003 	int i;
1004 	uint16_t v = pci_get_vendor(dev);
1005 	uint16_t d = pci_get_device(dev);
1006 	uint8_t f = pci_get_function(dev);
1007 
1008 	if (v != PCI_VENDOR_ID_CHELSIO)
1009 		return (ENXIO);
1010 
1011 	/* Attach only to PF0 of the FPGA */
1012 	if (d == 0xb000 && f != 0)
1013 		return (ENXIO);
1014 
1015 	for (i = 0; i < nitems(t5_pciids); i++) {
1016 		if (d == t5_pciids[i].device) {
1017 			device_set_desc(dev, t5_pciids[i].desc);
1018 			return (BUS_PROBE_DEFAULT);
1019 		}
1020 	}
1021 
1022 	return (ENXIO);
1023 }
1024 
1025 static int
t6_probe(device_t dev)1026 t6_probe(device_t dev)
1027 {
1028 	int i;
1029 	uint16_t v = pci_get_vendor(dev);
1030 	uint16_t d = pci_get_device(dev);
1031 
1032 	if (v != PCI_VENDOR_ID_CHELSIO)
1033 		return (ENXIO);
1034 
1035 	for (i = 0; i < nitems(t6_pciids); i++) {
1036 		if (d == t6_pciids[i].device) {
1037 			device_set_desc(dev, t6_pciids[i].desc);
1038 			return (BUS_PROBE_DEFAULT);
1039 		}
1040 	}
1041 
1042 	return (ENXIO);
1043 }
1044 
1045 static void
t5_attribute_workaround(device_t dev)1046 t5_attribute_workaround(device_t dev)
1047 {
1048 	device_t root_port;
1049 	uint32_t v;
1050 
1051 	/*
1052 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1053 	 * Ordering attributes when replying to a TLP from a Root
1054 	 * Port.  As a workaround, find the parent Root Port and
1055 	 * disable No Snoop and Relaxed Ordering.  Note that this
1056 	 * affects all devices under this root port.
1057 	 */
1058 	root_port = pci_find_pcie_root_port(dev);
1059 	if (root_port == NULL) {
1060 		device_printf(dev, "Unable to find parent root port\n");
1061 		return;
1062 	}
1063 
1064 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1065 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1066 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1067 	    0)
1068 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1069 		    device_get_nameunit(root_port));
1070 }
1071 
1072 static const struct devnames devnames[] = {
1073 	{
1074 		.nexus_name = "t4nex",
1075 		.ifnet_name = "cxgbe",
1076 		.vi_ifnet_name = "vcxgbe",
1077 		.pf03_drv_name = "t4iov",
1078 		.vf_nexus_name = "t4vf",
1079 		.vf_ifnet_name = "cxgbev"
1080 	}, {
1081 		.nexus_name = "t5nex",
1082 		.ifnet_name = "cxl",
1083 		.vi_ifnet_name = "vcxl",
1084 		.pf03_drv_name = "t5iov",
1085 		.vf_nexus_name = "t5vf",
1086 		.vf_ifnet_name = "cxlv"
1087 	}, {
1088 		.nexus_name = "t6nex",
1089 		.ifnet_name = "cc",
1090 		.vi_ifnet_name = "vcc",
1091 		.pf03_drv_name = "t6iov",
1092 		.vf_nexus_name = "t6vf",
1093 		.vf_ifnet_name = "ccv"
1094 	}
1095 };
1096 
1097 void
t4_init_devnames(struct adapter * sc)1098 t4_init_devnames(struct adapter *sc)
1099 {
1100 	int id;
1101 
1102 	id = chip_id(sc);
1103 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
1104 		sc->names = &devnames[id - CHELSIO_T4];
1105 	else {
1106 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1107 		sc->names = NULL;
1108 	}
1109 }
1110 
1111 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1112 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1113 {
1114 	const char *parent, *name;
1115 	long value;
1116 	int line, unit;
1117 
1118 	line = 0;
1119 	parent = device_get_nameunit(sc->dev);
1120 	name = sc->names->ifnet_name;
1121 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1122 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1123 		    value == pi->port_id)
1124 			return (unit);
1125 	}
1126 	return (-1);
1127 }
1128 
1129 static void
t4_calibration(void * arg)1130 t4_calibration(void *arg)
1131 {
1132 	struct adapter *sc;
1133 	struct clock_sync *cur, *nex;
1134 	uint64_t hw;
1135 	sbintime_t sbt;
1136 	int next_up;
1137 
1138 	sc = (struct adapter *)arg;
1139 
1140 	KASSERT(hw_all_ok(sc), ("!hw_all_ok at t4_calibration"));
1141 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1142 	sbt = sbinuptime();
1143 
1144 	cur = &sc->cal_info[sc->cal_current];
1145 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1146 	nex = &sc->cal_info[next_up];
1147 	if (__predict_false(sc->cal_count == 0)) {
1148 		/* First time in, just get the values in */
1149 		cur->hw_cur = hw;
1150 		cur->sbt_cur = sbt;
1151 		sc->cal_count++;
1152 		goto done;
1153 	}
1154 
1155 	if (cur->hw_cur == hw) {
1156 		/* The clock is not advancing? */
1157 		sc->cal_count = 0;
1158 		atomic_store_rel_int(&cur->gen, 0);
1159 		goto done;
1160 	}
1161 
1162 	seqc_write_begin(&nex->gen);
1163 	nex->hw_prev = cur->hw_cur;
1164 	nex->sbt_prev = cur->sbt_cur;
1165 	nex->hw_cur = hw;
1166 	nex->sbt_cur = sbt;
1167 	seqc_write_end(&nex->gen);
1168 	sc->cal_current = next_up;
1169 done:
1170 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1171 	    sc, C_DIRECT_EXEC);
1172 }
1173 
1174 static void
t4_calibration_start(struct adapter * sc)1175 t4_calibration_start(struct adapter *sc)
1176 {
1177 	/*
1178 	 * Here if we have not done a calibration
1179 	 * then do so otherwise start the appropriate
1180 	 * timer.
1181 	 */
1182 	int i;
1183 
1184 	for (i = 0; i < CNT_CAL_INFO; i++) {
1185 		sc->cal_info[i].gen = 0;
1186 	}
1187 	sc->cal_current = 0;
1188 	sc->cal_count = 0;
1189 	sc->cal_gen = 0;
1190 	t4_calibration(sc);
1191 }
1192 
1193 static int
t4_attach(device_t dev)1194 t4_attach(device_t dev)
1195 {
1196 	struct adapter *sc;
1197 	int rc = 0, i, j, rqidx, tqidx, nports;
1198 	struct make_dev_args mda;
1199 	struct intrs_and_queues iaq;
1200 	struct sge *s;
1201 	uint32_t *buf;
1202 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1203 	int ofld_tqidx;
1204 #endif
1205 #ifdef TCP_OFFLOAD
1206 	int ofld_rqidx;
1207 #endif
1208 #ifdef DEV_NETMAP
1209 	int nm_rqidx, nm_tqidx;
1210 #endif
1211 	int num_vis;
1212 
1213 	sc = device_get_softc(dev);
1214 	sc->dev = dev;
1215 	sysctl_ctx_init(&sc->ctx);
1216 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1217 
1218 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1219 		t5_attribute_workaround(dev);
1220 	pci_enable_busmaster(dev);
1221 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1222 		uint32_t v;
1223 
1224 		pci_set_max_read_req(dev, 4096);
1225 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1226 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1227 		if (pcie_relaxed_ordering == 0 &&
1228 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1229 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1230 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1231 		} else if (pcie_relaxed_ordering == 1 &&
1232 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1233 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1234 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1235 		}
1236 	}
1237 
1238 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1239 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1240 	sc->traceq = -1;
1241 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1242 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1243 	    device_get_nameunit(dev));
1244 
1245 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1246 	    device_get_nameunit(dev));
1247 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1248 	t4_add_adapter(sc);
1249 
1250 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1251 	TAILQ_INIT(&sc->sfl);
1252 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1253 
1254 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1255 
1256 	sc->policy = NULL;
1257 	rw_init(&sc->policy_lock, "connection offload policy");
1258 
1259 	callout_init(&sc->ktls_tick, 1);
1260 
1261 	callout_init(&sc->cal_callout, 1);
1262 
1263 	refcount_init(&sc->vxlan_refcount, 0);
1264 
1265 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1266 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1267 
1268 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1269 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1270 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1271 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1272 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1273 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1274 
1275 	rc = t4_map_bars_0_and_4(sc);
1276 	if (rc != 0)
1277 		goto done; /* error message displayed already */
1278 
1279 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1280 
1281 	/* Prepare the adapter for operation. */
1282 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1283 	rc = -t4_prep_adapter(sc, buf);
1284 	free(buf, M_CXGBE);
1285 	if (rc != 0) {
1286 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1287 		goto done;
1288 	}
1289 
1290 	/*
1291 	 * This is the real PF# to which we're attaching.  Works from within PCI
1292 	 * passthrough environments too, where pci_get_function() could return a
1293 	 * different PF# depending on the passthrough configuration.  We need to
1294 	 * use the real PF# in all our communication with the firmware.
1295 	 */
1296 	j = t4_read_reg(sc, A_PL_WHOAMI);
1297 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1298 	sc->mbox = sc->pf;
1299 
1300 	t4_init_devnames(sc);
1301 	if (sc->names == NULL) {
1302 		rc = ENOTSUP;
1303 		goto done; /* error message displayed already */
1304 	}
1305 
1306 	/*
1307 	 * Do this really early, with the memory windows set up even before the
1308 	 * character device.  The userland tool's register i/o and mem read
1309 	 * will work even in "recovery mode".
1310 	 */
1311 	setup_memwin(sc);
1312 	if (t4_init_devlog_params(sc, 0) == 0)
1313 		fixup_devlog_params(sc);
1314 	make_dev_args_init(&mda);
1315 	mda.mda_devsw = &t4_cdevsw;
1316 	mda.mda_uid = UID_ROOT;
1317 	mda.mda_gid = GID_WHEEL;
1318 	mda.mda_mode = 0600;
1319 	mda.mda_si_drv1 = sc;
1320 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1321 	if (rc != 0)
1322 		device_printf(dev, "failed to create nexus char device: %d.\n",
1323 		    rc);
1324 
1325 	/* Go no further if recovery mode has been requested. */
1326 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1327 		device_printf(dev, "recovery mode.\n");
1328 		goto done;
1329 	}
1330 
1331 #if defined(__i386__)
1332 	if ((cpu_feature & CPUID_CX8) == 0) {
1333 		device_printf(dev, "64 bit atomics not available.\n");
1334 		rc = ENOTSUP;
1335 		goto done;
1336 	}
1337 #endif
1338 
1339 	/* Contact the firmware and try to become the master driver. */
1340 	rc = contact_firmware(sc);
1341 	if (rc != 0)
1342 		goto done; /* error message displayed already */
1343 	MPASS(sc->flags & FW_OK);
1344 
1345 	rc = get_params__pre_init(sc);
1346 	if (rc != 0)
1347 		goto done; /* error message displayed already */
1348 
1349 	if (sc->flags & MASTER_PF) {
1350 		rc = partition_resources(sc);
1351 		if (rc != 0)
1352 			goto done; /* error message displayed already */
1353 	}
1354 
1355 	rc = get_params__post_init(sc);
1356 	if (rc != 0)
1357 		goto done; /* error message displayed already */
1358 
1359 	rc = set_params__post_init(sc);
1360 	if (rc != 0)
1361 		goto done; /* error message displayed already */
1362 
1363 	rc = t4_map_bar_2(sc);
1364 	if (rc != 0)
1365 		goto done; /* error message displayed already */
1366 
1367 	rc = t4_adj_doorbells(sc);
1368 	if (rc != 0)
1369 		goto done; /* error message displayed already */
1370 
1371 	rc = t4_create_dma_tag(sc);
1372 	if (rc != 0)
1373 		goto done; /* error message displayed already */
1374 
1375 	/*
1376 	 * First pass over all the ports - allocate VIs and initialize some
1377 	 * basic parameters like mac address, port type, etc.
1378 	 */
1379 	for_each_port(sc, i) {
1380 		struct port_info *pi;
1381 
1382 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1383 		sc->port[i] = pi;
1384 
1385 		/* These must be set before t4_port_init */
1386 		pi->adapter = sc;
1387 		pi->port_id = i;
1388 		/*
1389 		 * XXX: vi[0] is special so we can't delay this allocation until
1390 		 * pi->nvi's final value is known.
1391 		 */
1392 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1393 		    M_ZERO | M_WAITOK);
1394 
1395 		/*
1396 		 * Allocate the "main" VI and initialize parameters
1397 		 * like mac addr.
1398 		 */
1399 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1400 		if (rc != 0) {
1401 			device_printf(dev, "unable to initialize port %d: %d\n",
1402 			    i, rc);
1403 			free(pi->vi, M_CXGBE);
1404 			free(pi, M_CXGBE);
1405 			sc->port[i] = NULL;
1406 			goto done;
1407 		}
1408 
1409 		if (is_bt(pi->port_type))
1410 			setbit(&sc->bt_map, pi->tx_chan);
1411 		else
1412 			MPASS(!isset(&sc->bt_map, pi->tx_chan));
1413 
1414 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1415 		    device_get_nameunit(dev), i);
1416 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1417 		sc->chan_map[pi->tx_chan] = i;
1418 
1419 		/*
1420 		 * The MPS counter for FCS errors doesn't work correctly on the
1421 		 * T6 so we use the MAC counter here.  Which MAC is in use
1422 		 * depends on the link settings which will be known when the
1423 		 * link comes up.
1424 		 */
1425 		if (is_t6(sc))
1426 			pi->fcs_reg = -1;
1427 		else {
1428 			pi->fcs_reg = t4_port_reg(sc, pi->tx_chan,
1429 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1430 		}
1431 		pi->fcs_base = 0;
1432 
1433 		/* All VIs on this port share this media. */
1434 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1435 		    cxgbe_media_status);
1436 
1437 		PORT_LOCK(pi);
1438 		init_link_config(pi);
1439 		fixup_link_config(pi);
1440 		build_medialist(pi);
1441 		if (fixed_ifmedia(pi))
1442 			pi->flags |= FIXED_IFMEDIA;
1443 		PORT_UNLOCK(pi);
1444 
1445 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1446 		    t4_ifnet_unit(sc, pi));
1447 		if (pi->dev == NULL) {
1448 			device_printf(dev,
1449 			    "failed to add device for port %d.\n", i);
1450 			rc = ENXIO;
1451 			goto done;
1452 		}
1453 		pi->vi[0].dev = pi->dev;
1454 		device_set_softc(pi->dev, pi);
1455 	}
1456 
1457 	/*
1458 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1459 	 */
1460 	nports = sc->params.nports;
1461 	rc = cfg_itype_and_nqueues(sc, &iaq);
1462 	if (rc != 0)
1463 		goto done; /* error message displayed already */
1464 
1465 	num_vis = iaq.num_vis;
1466 	sc->intr_type = iaq.intr_type;
1467 	sc->intr_count = iaq.nirq;
1468 
1469 	s = &sc->sge;
1470 	s->nrxq = nports * iaq.nrxq;
1471 	s->ntxq = nports * iaq.ntxq;
1472 	if (num_vis > 1) {
1473 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1474 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1475 	}
1476 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1477 	s->neq += nports;		/* ctrl queues: 1 per port */
1478 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1479 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1480 	if (is_offload(sc) || is_ethoffload(sc)) {
1481 		s->nofldtxq = nports * iaq.nofldtxq;
1482 		if (num_vis > 1)
1483 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1484 		s->neq += s->nofldtxq;
1485 
1486 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1487 		    M_CXGBE, M_ZERO | M_WAITOK);
1488 	}
1489 #endif
1490 #ifdef TCP_OFFLOAD
1491 	if (is_offload(sc)) {
1492 		s->nofldrxq = nports * iaq.nofldrxq;
1493 		if (num_vis > 1)
1494 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1495 		s->neq += s->nofldrxq;	/* free list */
1496 		s->niq += s->nofldrxq;
1497 
1498 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1499 		    M_CXGBE, M_ZERO | M_WAITOK);
1500 	}
1501 #endif
1502 #ifdef DEV_NETMAP
1503 	s->nnmrxq = 0;
1504 	s->nnmtxq = 0;
1505 	if (t4_native_netmap & NN_MAIN_VI) {
1506 		s->nnmrxq += nports * iaq.nnmrxq;
1507 		s->nnmtxq += nports * iaq.nnmtxq;
1508 	}
1509 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1510 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1511 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1512 	}
1513 	s->neq += s->nnmtxq + s->nnmrxq;
1514 	s->niq += s->nnmrxq;
1515 
1516 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1517 	    M_CXGBE, M_ZERO | M_WAITOK);
1518 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1519 	    M_CXGBE, M_ZERO | M_WAITOK);
1520 #endif
1521 	MPASS(s->niq <= s->iqmap_sz);
1522 	MPASS(s->neq <= s->eqmap_sz);
1523 
1524 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1525 	    M_ZERO | M_WAITOK);
1526 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1527 	    M_ZERO | M_WAITOK);
1528 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1529 	    M_ZERO | M_WAITOK);
1530 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1531 	    M_ZERO | M_WAITOK);
1532 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1533 	    M_ZERO | M_WAITOK);
1534 
1535 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1536 	    M_ZERO | M_WAITOK);
1537 
1538 	t4_init_l2t(sc, M_WAITOK);
1539 	t4_init_smt(sc, M_WAITOK);
1540 	t4_init_tx_sched(sc);
1541 	t4_init_atid_table(sc);
1542 #ifdef RATELIMIT
1543 	t4_init_etid_table(sc);
1544 #endif
1545 #ifdef INET6
1546 	t4_init_clip_table(sc);
1547 #endif
1548 	if (sc->vres.key.size != 0)
1549 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1550 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1551 
1552 	/*
1553 	 * Second pass over the ports.  This time we know the number of rx and
1554 	 * tx queues that each port should get.
1555 	 */
1556 	rqidx = tqidx = 0;
1557 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1558 	ofld_tqidx = 0;
1559 #endif
1560 #ifdef TCP_OFFLOAD
1561 	ofld_rqidx = 0;
1562 #endif
1563 #ifdef DEV_NETMAP
1564 	nm_rqidx = nm_tqidx = 0;
1565 #endif
1566 	for_each_port(sc, i) {
1567 		struct port_info *pi = sc->port[i];
1568 		struct vi_info *vi;
1569 
1570 		if (pi == NULL)
1571 			continue;
1572 
1573 		pi->nvi = num_vis;
1574 		for_each_vi(pi, j, vi) {
1575 			vi->pi = pi;
1576 			vi->adapter = sc;
1577 			vi->first_intr = -1;
1578 			vi->qsize_rxq = t4_qsize_rxq;
1579 			vi->qsize_txq = t4_qsize_txq;
1580 
1581 			vi->first_rxq = rqidx;
1582 			vi->first_txq = tqidx;
1583 			vi->tmr_idx = t4_tmr_idx;
1584 			vi->pktc_idx = t4_pktc_idx;
1585 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1586 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1587 
1588 			rqidx += vi->nrxq;
1589 			tqidx += vi->ntxq;
1590 
1591 			if (j == 0 && vi->ntxq > 1)
1592 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1593 			else
1594 				vi->rsrv_noflowq = 0;
1595 
1596 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1597 			vi->first_ofld_txq = ofld_tqidx;
1598 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1599 			ofld_tqidx += vi->nofldtxq;
1600 #endif
1601 #ifdef TCP_OFFLOAD
1602 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1603 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1604 			vi->first_ofld_rxq = ofld_rqidx;
1605 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1606 
1607 			ofld_rqidx += vi->nofldrxq;
1608 #endif
1609 #ifdef DEV_NETMAP
1610 			vi->first_nm_rxq = nm_rqidx;
1611 			vi->first_nm_txq = nm_tqidx;
1612 			if (j == 0) {
1613 				vi->nnmrxq = iaq.nnmrxq;
1614 				vi->nnmtxq = iaq.nnmtxq;
1615 			} else {
1616 				vi->nnmrxq = iaq.nnmrxq_vi;
1617 				vi->nnmtxq = iaq.nnmtxq_vi;
1618 			}
1619 			nm_rqidx += vi->nnmrxq;
1620 			nm_tqidx += vi->nnmtxq;
1621 #endif
1622 		}
1623 	}
1624 
1625 	rc = t4_setup_intr_handlers(sc);
1626 	if (rc != 0) {
1627 		device_printf(dev,
1628 		    "failed to setup interrupt handlers: %d\n", rc);
1629 		goto done;
1630 	}
1631 
1632 	rc = bus_generic_probe(dev);
1633 	if (rc != 0) {
1634 		device_printf(dev, "failed to probe child drivers: %d\n", rc);
1635 		goto done;
1636 	}
1637 
1638 	/*
1639 	 * Ensure thread-safe mailbox access (in debug builds).
1640 	 *
1641 	 * So far this was the only thread accessing the mailbox but various
1642 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1643 	 * will access the mailbox from different threads.
1644 	 */
1645 	sc->flags |= CHK_MBOX_ACCESS;
1646 
1647 	rc = bus_generic_attach(dev);
1648 	if (rc != 0) {
1649 		device_printf(dev,
1650 		    "failed to attach all child ports: %d\n", rc);
1651 		goto done;
1652 	}
1653 	t4_calibration_start(sc);
1654 
1655 	device_printf(dev,
1656 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1657 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1658 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1659 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1660 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1661 
1662 	t4_set_desc(sc);
1663 
1664 	notify_siblings(dev, 0);
1665 
1666 done:
1667 	if (rc != 0 && sc->cdev) {
1668 		/* cdev was created and so cxgbetool works; recover that way. */
1669 		device_printf(dev,
1670 		    "error during attach, adapter is now in recovery mode.\n");
1671 		rc = 0;
1672 	}
1673 
1674 	if (rc != 0)
1675 		t4_detach_common(dev);
1676 	else
1677 		t4_sysctls(sc);
1678 
1679 	return (rc);
1680 }
1681 
1682 static int
t4_child_location(device_t bus,device_t dev,struct sbuf * sb)1683 t4_child_location(device_t bus, device_t dev, struct sbuf *sb)
1684 {
1685 	struct adapter *sc;
1686 	struct port_info *pi;
1687 	int i;
1688 
1689 	sc = device_get_softc(bus);
1690 	for_each_port(sc, i) {
1691 		pi = sc->port[i];
1692 		if (pi != NULL && pi->dev == dev) {
1693 			sbuf_printf(sb, "port=%d", pi->port_id);
1694 			break;
1695 		}
1696 	}
1697 	return (0);
1698 }
1699 
1700 static int
t4_ready(device_t dev)1701 t4_ready(device_t dev)
1702 {
1703 	struct adapter *sc;
1704 
1705 	sc = device_get_softc(dev);
1706 	if (sc->flags & FW_OK)
1707 		return (0);
1708 	return (ENXIO);
1709 }
1710 
1711 static int
t4_read_port_device(device_t dev,int port,device_t * child)1712 t4_read_port_device(device_t dev, int port, device_t *child)
1713 {
1714 	struct adapter *sc;
1715 	struct port_info *pi;
1716 
1717 	sc = device_get_softc(dev);
1718 	if (port < 0 || port >= MAX_NPORTS)
1719 		return (EINVAL);
1720 	pi = sc->port[port];
1721 	if (pi == NULL || pi->dev == NULL)
1722 		return (ENXIO);
1723 	*child = pi->dev;
1724 	return (0);
1725 }
1726 
1727 static int
notify_siblings(device_t dev,int detaching)1728 notify_siblings(device_t dev, int detaching)
1729 {
1730 	device_t sibling;
1731 	int error, i;
1732 
1733 	error = 0;
1734 	for (i = 0; i < PCI_FUNCMAX; i++) {
1735 		if (i == pci_get_function(dev))
1736 			continue;
1737 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1738 		    pci_get_slot(dev), i);
1739 		if (sibling == NULL || !device_is_attached(sibling))
1740 			continue;
1741 		if (detaching)
1742 			error = T4_DETACH_CHILD(sibling);
1743 		else
1744 			(void)T4_ATTACH_CHILD(sibling);
1745 		if (error)
1746 			break;
1747 	}
1748 	return (error);
1749 }
1750 
1751 /*
1752  * Idempotent
1753  */
1754 static int
t4_detach(device_t dev)1755 t4_detach(device_t dev)
1756 {
1757 	int rc;
1758 
1759 	rc = notify_siblings(dev, 1);
1760 	if (rc) {
1761 		device_printf(dev,
1762 		    "failed to detach sibling devices: %d\n", rc);
1763 		return (rc);
1764 	}
1765 
1766 	return (t4_detach_common(dev));
1767 }
1768 
1769 int
t4_detach_common(device_t dev)1770 t4_detach_common(device_t dev)
1771 {
1772 	struct adapter *sc;
1773 	struct port_info *pi;
1774 	int i, rc;
1775 
1776 	sc = device_get_softc(dev);
1777 
1778 #ifdef TCP_OFFLOAD
1779 	rc = deactivate_all_uld(sc);
1780 	if (rc) {
1781 		device_printf(dev,
1782 		    "failed to detach upper layer drivers: %d\n", rc);
1783 		return (rc);
1784 	}
1785 #endif
1786 
1787 	if (sc->cdev) {
1788 		destroy_dev(sc->cdev);
1789 		sc->cdev = NULL;
1790 	}
1791 
1792 	sx_xlock(&t4_list_lock);
1793 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1794 	sx_xunlock(&t4_list_lock);
1795 
1796 	sc->flags &= ~CHK_MBOX_ACCESS;
1797 	if (sc->flags & FULL_INIT_DONE) {
1798 		if (!(sc->flags & IS_VF))
1799 			t4_intr_disable(sc);
1800 	}
1801 
1802 	if (device_is_attached(dev)) {
1803 		rc = bus_generic_detach(dev);
1804 		if (rc) {
1805 			device_printf(dev,
1806 			    "failed to detach child devices: %d\n", rc);
1807 			return (rc);
1808 		}
1809 	}
1810 
1811 	for (i = 0; i < sc->intr_count; i++)
1812 		t4_free_irq(sc, &sc->irq[i]);
1813 
1814 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1815 		t4_free_tx_sched(sc);
1816 
1817 	for (i = 0; i < MAX_NPORTS; i++) {
1818 		pi = sc->port[i];
1819 		if (pi) {
1820 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1821 			if (pi->dev)
1822 				device_delete_child(dev, pi->dev);
1823 
1824 			mtx_destroy(&pi->pi_lock);
1825 			free(pi->vi, M_CXGBE);
1826 			free(pi, M_CXGBE);
1827 		}
1828 	}
1829 	callout_stop(&sc->cal_callout);
1830 	callout_drain(&sc->cal_callout);
1831 	device_delete_children(dev);
1832 	sysctl_ctx_free(&sc->ctx);
1833 	adapter_full_uninit(sc);
1834 
1835 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1836 		t4_fw_bye(sc, sc->mbox);
1837 
1838 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1839 		pci_release_msi(dev);
1840 
1841 	if (sc->regs_res)
1842 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1843 		    sc->regs_res);
1844 
1845 	if (sc->udbs_res)
1846 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1847 		    sc->udbs_res);
1848 
1849 	if (sc->msix_res)
1850 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1851 		    sc->msix_res);
1852 
1853 	if (sc->l2t)
1854 		t4_free_l2t(sc);
1855 	if (sc->smt)
1856 		t4_free_smt(sc->smt);
1857 	t4_free_atid_table(sc);
1858 #ifdef RATELIMIT
1859 	t4_free_etid_table(sc);
1860 #endif
1861 	if (sc->key_map)
1862 		vmem_destroy(sc->key_map);
1863 #ifdef INET6
1864 	t4_destroy_clip_table(sc);
1865 #endif
1866 
1867 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1868 	free(sc->sge.ofld_txq, M_CXGBE);
1869 #endif
1870 #ifdef TCP_OFFLOAD
1871 	free(sc->sge.ofld_rxq, M_CXGBE);
1872 #endif
1873 #ifdef DEV_NETMAP
1874 	free(sc->sge.nm_rxq, M_CXGBE);
1875 	free(sc->sge.nm_txq, M_CXGBE);
1876 #endif
1877 	free(sc->irq, M_CXGBE);
1878 	free(sc->sge.rxq, M_CXGBE);
1879 	free(sc->sge.txq, M_CXGBE);
1880 	free(sc->sge.ctrlq, M_CXGBE);
1881 	free(sc->sge.iqmap, M_CXGBE);
1882 	free(sc->sge.eqmap, M_CXGBE);
1883 	free(sc->tids.ftid_tab, M_CXGBE);
1884 	free(sc->tids.hpftid_tab, M_CXGBE);
1885 	free_hftid_hash(&sc->tids);
1886 	free(sc->tids.tid_tab, M_CXGBE);
1887 	t4_destroy_dma_tag(sc);
1888 
1889 	callout_drain(&sc->ktls_tick);
1890 	callout_drain(&sc->sfl_callout);
1891 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1892 		mtx_destroy(&sc->tids.ftid_lock);
1893 		cv_destroy(&sc->tids.ftid_cv);
1894 	}
1895 	if (mtx_initialized(&sc->tids.atid_lock))
1896 		mtx_destroy(&sc->tids.atid_lock);
1897 	if (mtx_initialized(&sc->ifp_lock))
1898 		mtx_destroy(&sc->ifp_lock);
1899 
1900 	if (rw_initialized(&sc->policy_lock)) {
1901 		rw_destroy(&sc->policy_lock);
1902 #ifdef TCP_OFFLOAD
1903 		if (sc->policy != NULL)
1904 			free_offload_policy(sc->policy);
1905 #endif
1906 	}
1907 
1908 	for (i = 0; i < NUM_MEMWIN; i++) {
1909 		struct memwin *mw = &sc->memwin[i];
1910 
1911 		if (rw_initialized(&mw->mw_lock))
1912 			rw_destroy(&mw->mw_lock);
1913 	}
1914 
1915 	mtx_destroy(&sc->sfl_lock);
1916 	mtx_destroy(&sc->reg_lock);
1917 	mtx_destroy(&sc->sc_lock);
1918 
1919 	bzero(sc, sizeof(*sc));
1920 
1921 	return (0);
1922 }
1923 
1924 static inline int
stop_adapter(struct adapter * sc)1925 stop_adapter(struct adapter *sc)
1926 {
1927 	struct port_info *pi;
1928 	int i;
1929 
1930 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1931 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1932 			 __func__, curthread, sc->flags, sc->error_flags);
1933 		return (EALREADY);
1934 	}
1935 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1936 		 sc->flags, sc->error_flags);
1937 	t4_shutdown_adapter(sc);
1938 	for_each_port(sc, i) {
1939 		pi = sc->port[i];
1940 		if (pi == NULL)
1941 			continue;
1942 		PORT_LOCK(pi);
1943 		if (pi->up_vis > 0 && pi->link_cfg.link_ok) {
1944 			/*
1945 			 * t4_shutdown_adapter has already shut down all the
1946 			 * PHYs but it also disables interrupts and DMA so there
1947 			 * won't be a link interrupt.  Update the state manually
1948 			 * if the link was up previously and inform the kernel.
1949 			 */
1950 			pi->link_cfg.link_ok = false;
1951 			t4_os_link_changed(pi);
1952 		}
1953 		PORT_UNLOCK(pi);
1954 	}
1955 
1956 	return (0);
1957 }
1958 
1959 static inline int
restart_adapter(struct adapter * sc)1960 restart_adapter(struct adapter *sc)
1961 {
1962 	uint32_t val;
1963 
1964 	if (!atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1965 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1966 			 __func__, curthread, sc->flags, sc->error_flags);
1967 		return (EALREADY);
1968 	}
1969 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1970 		 sc->flags, sc->error_flags);
1971 
1972 	MPASS(hw_off_limits(sc));
1973 	MPASS((sc->flags & FW_OK) == 0);
1974 	MPASS((sc->flags & MASTER_PF) == 0);
1975 	MPASS(sc->reset_thread == NULL);
1976 
1977 	/*
1978 	 * The adapter is supposed to be back on PCIE with its config space and
1979 	 * BARs restored to their state before reset.  Register access via
1980 	 * t4_read_reg BAR0 should just work.
1981 	 */
1982 	sc->reset_thread = curthread;
1983 	val = t4_read_reg(sc, A_PL_WHOAMI);
1984 	if (val == 0xffffffff || val == 0xeeeeeeee) {
1985 		CH_ERR(sc, "%s: device registers not readable.\n", __func__);
1986 		sc->reset_thread = NULL;
1987 		atomic_set_int(&sc->error_flags, ADAP_STOPPED);
1988 		return (ENXIO);
1989 	}
1990 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR);
1991 	atomic_add_int(&sc->incarnation, 1);
1992 	atomic_add_int(&sc->num_resets, 1);
1993 
1994 	return (0);
1995 }
1996 
1997 static inline void
set_adapter_hwstatus(struct adapter * sc,const bool usable)1998 set_adapter_hwstatus(struct adapter *sc, const bool usable)
1999 {
2000 	if (usable) {
2001 		/* Must be marked reusable by the designated thread. */
2002 		ASSERT_SYNCHRONIZED_OP(sc);
2003 		MPASS(sc->reset_thread == curthread);
2004 		mtx_lock(&sc->reg_lock);
2005 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
2006 		mtx_unlock(&sc->reg_lock);
2007 	} else {
2008 		/* Mark the adapter totally off limits. */
2009 		begin_synchronized_op(sc, NULL, SLEEP_OK, "t4hwsts");
2010 		mtx_lock(&sc->reg_lock);
2011 		atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
2012 		mtx_unlock(&sc->reg_lock);
2013 		sc->flags &= ~(FW_OK | MASTER_PF);
2014 		sc->reset_thread = NULL;
2015 		end_synchronized_op(sc, 0);
2016 	}
2017 }
2018 
2019 static int
stop_lld(struct adapter * sc)2020 stop_lld(struct adapter *sc)
2021 {
2022 	struct port_info *pi;
2023 	struct vi_info *vi;
2024 	if_t ifp;
2025 	struct sge_rxq *rxq;
2026 	struct sge_txq *txq;
2027 	struct sge_wrq *wrq;
2028 #ifdef TCP_OFFLOAD
2029 	struct sge_ofld_rxq *ofld_rxq;
2030 #endif
2031 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2032 	struct sge_ofld_txq *ofld_txq;
2033 #endif
2034 	int rc, i, j, k;
2035 
2036 	/*
2037 	 * XXX: Can there be a synch_op in progress that will hang because
2038 	 * hardware has been stopped?  We'll hang too and the solution will be
2039 	 * to use a version of begin_synch_op that wakes up existing synch_op
2040 	 * with errors.  Maybe stop_adapter should do this wakeup?
2041 	 *
2042 	 * I don't think any synch_op could get stranded waiting for DMA or
2043 	 * interrupt so I think we're okay here.  Remove this comment block
2044 	 * after testing.
2045 	 */
2046 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4slld");
2047 	if (rc != 0)
2048 		return (ENXIO);
2049 
2050 	/* Quiesce all activity. */
2051 	for_each_port(sc, i) {
2052 		pi = sc->port[i];
2053 		if (pi == NULL)
2054 			continue;
2055 		pi->vxlan_tcam_entry = false;
2056 		for_each_vi(pi, j, vi) {
2057 			vi->xact_addr_filt = -1;
2058 			mtx_lock(&vi->tick_mtx);
2059 			vi->flags |= VI_SKIP_STATS;
2060 			mtx_unlock(&vi->tick_mtx);
2061 			if (!(vi->flags & VI_INIT_DONE))
2062 				continue;
2063 
2064 			ifp = vi->ifp;
2065 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2066 				mtx_lock(&vi->tick_mtx);
2067 				callout_stop(&vi->tick);
2068 				mtx_unlock(&vi->tick_mtx);
2069 				callout_drain(&vi->tick);
2070 			}
2071 
2072 			/*
2073 			 * Note that the HW is not available.
2074 			 */
2075 			for_each_txq(vi, k, txq) {
2076 				TXQ_LOCK(txq);
2077 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2078 				TXQ_UNLOCK(txq);
2079 			}
2080 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2081 			for_each_ofld_txq(vi, k, ofld_txq) {
2082 				TXQ_LOCK(&ofld_txq->wrq);
2083 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2084 				TXQ_UNLOCK(&ofld_txq->wrq);
2085 			}
2086 #endif
2087 			for_each_rxq(vi, k, rxq) {
2088 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2089 			}
2090 #if defined(TCP_OFFLOAD)
2091 			for_each_ofld_rxq(vi, k, ofld_rxq) {
2092 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2093 			}
2094 #endif
2095 
2096 			quiesce_vi(vi);
2097 		}
2098 
2099 		if (sc->flags & FULL_INIT_DONE) {
2100 			/* Control queue */
2101 			wrq = &sc->sge.ctrlq[i];
2102 			TXQ_LOCK(wrq);
2103 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2104 			TXQ_UNLOCK(wrq);
2105 			quiesce_wrq(wrq);
2106 		}
2107 
2108 		if (pi->flags & HAS_TRACEQ) {
2109 			pi->flags &= ~HAS_TRACEQ;
2110 			sc->traceq = -1;
2111 			sc->tracer_valid = 0;
2112 			sc->tracer_enabled = 0;
2113 		}
2114 	}
2115 	if (sc->flags & FULL_INIT_DONE) {
2116 		/* Firmware event queue */
2117 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2118 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2119 	}
2120 
2121 	/* Stop calibration */
2122 	callout_stop(&sc->cal_callout);
2123 	callout_drain(&sc->cal_callout);
2124 
2125 	if (t4_clock_gate_on_suspend) {
2126 		t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2127 		    F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2128 		    F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2129 	}
2130 
2131 	end_synchronized_op(sc, 0);
2132 
2133 	stop_atid_allocator(sc);
2134 	t4_stop_l2t(sc);
2135 
2136 	return (rc);
2137 }
2138 
2139 int
suspend_adapter(struct adapter * sc)2140 suspend_adapter(struct adapter *sc)
2141 {
2142 	stop_adapter(sc);
2143 	stop_lld(sc);
2144 #ifdef TCP_OFFLOAD
2145 	stop_all_uld(sc);
2146 #endif
2147 	set_adapter_hwstatus(sc, false);
2148 
2149 	return (0);
2150 }
2151 
2152 static int
t4_suspend(device_t dev)2153 t4_suspend(device_t dev)
2154 {
2155 	struct adapter *sc = device_get_softc(dev);
2156 	int rc;
2157 
2158 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2159 	rc = suspend_adapter(sc);
2160 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2161 
2162 	return (rc);
2163 }
2164 
2165 struct adapter_pre_reset_state {
2166 	u_int flags;
2167 	uint16_t nbmcaps;
2168 	uint16_t linkcaps;
2169 	uint16_t switchcaps;
2170 	uint16_t niccaps;
2171 	uint16_t toecaps;
2172 	uint16_t rdmacaps;
2173 	uint16_t cryptocaps;
2174 	uint16_t iscsicaps;
2175 	uint16_t fcoecaps;
2176 
2177 	u_int cfcsum;
2178 	char cfg_file[32];
2179 
2180 	struct adapter_params params;
2181 	struct t4_virt_res vres;
2182 	struct tid_info tids;
2183 	struct sge sge;
2184 
2185 	int rawf_base;
2186 	int nrawf;
2187 
2188 };
2189 
2190 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2191 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2192 {
2193 
2194 	ASSERT_SYNCHRONIZED_OP(sc);
2195 
2196 	o->flags = sc->flags;
2197 
2198 	o->nbmcaps =  sc->nbmcaps;
2199 	o->linkcaps = sc->linkcaps;
2200 	o->switchcaps = sc->switchcaps;
2201 	o->niccaps = sc->niccaps;
2202 	o->toecaps = sc->toecaps;
2203 	o->rdmacaps = sc->rdmacaps;
2204 	o->cryptocaps = sc->cryptocaps;
2205 	o->iscsicaps = sc->iscsicaps;
2206 	o->fcoecaps = sc->fcoecaps;
2207 
2208 	o->cfcsum = sc->cfcsum;
2209 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2210 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2211 
2212 	o->params = sc->params;
2213 	o->vres = sc->vres;
2214 	o->tids = sc->tids;
2215 	o->sge = sc->sge;
2216 
2217 	o->rawf_base = sc->rawf_base;
2218 	o->nrawf = sc->nrawf;
2219 }
2220 
2221 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2222 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2223 {
2224 	int rc = 0;
2225 
2226 	ASSERT_SYNCHRONIZED_OP(sc);
2227 
2228 	/* Capabilities */
2229 #define COMPARE_CAPS(c) do { \
2230 	if (o->c##caps != sc->c##caps) { \
2231 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2232 		    sc->c##caps); \
2233 		rc = EINVAL; \
2234 	} \
2235 } while (0)
2236 	COMPARE_CAPS(nbm);
2237 	COMPARE_CAPS(link);
2238 	COMPARE_CAPS(switch);
2239 	COMPARE_CAPS(nic);
2240 	COMPARE_CAPS(toe);
2241 	COMPARE_CAPS(rdma);
2242 	COMPARE_CAPS(crypto);
2243 	COMPARE_CAPS(iscsi);
2244 	COMPARE_CAPS(fcoe);
2245 #undef COMPARE_CAPS
2246 
2247 	/* Firmware config file */
2248 	if (o->cfcsum != sc->cfcsum) {
2249 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2250 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2251 		rc = EINVAL;
2252 	}
2253 
2254 #define COMPARE_PARAM(p, name) do { \
2255 	if (o->p != sc->p) { \
2256 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2257 		rc = EINVAL; \
2258 	} \
2259 } while (0)
2260 	COMPARE_PARAM(sge.iq_start, iq_start);
2261 	COMPARE_PARAM(sge.eq_start, eq_start);
2262 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2263 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2264 	COMPARE_PARAM(tids.nftids, nftids);
2265 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2266 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2267 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2268 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2269 	COMPARE_PARAM(tids.tid_base, tid_base);
2270 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2271 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2272 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2273 	COMPARE_PARAM(rawf_base, rawf_base);
2274 	COMPARE_PARAM(nrawf, nrawf);
2275 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2276 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2277 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2278 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2279 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2280 	COMPARE_PARAM(tids.ntids, ntids);
2281 	COMPARE_PARAM(tids.etid_base, etid_base);
2282 	COMPARE_PARAM(tids.etid_end, etid_end);
2283 	COMPARE_PARAM(tids.netids, netids);
2284 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2285 	COMPARE_PARAM(params.ethoffload, ethoffload);
2286 	COMPARE_PARAM(tids.natids, natids);
2287 	COMPARE_PARAM(tids.stid_base, stid_base);
2288 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2289 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2290 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2291 	COMPARE_PARAM(vres.stag.start, stag_start);
2292 	COMPARE_PARAM(vres.stag.size, stag_size);
2293 	COMPARE_PARAM(vres.rq.start, rq_start);
2294 	COMPARE_PARAM(vres.rq.size, rq_size);
2295 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2296 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2297 	COMPARE_PARAM(vres.qp.start, qp_start);
2298 	COMPARE_PARAM(vres.qp.size, qp_size);
2299 	COMPARE_PARAM(vres.cq.start, cq_start);
2300 	COMPARE_PARAM(vres.cq.size, cq_size);
2301 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2302 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2303 	COMPARE_PARAM(vres.srq.start, srq_start);
2304 	COMPARE_PARAM(vres.srq.size, srq_size);
2305 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2306 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2307 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2308 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2309 	COMPARE_PARAM(vres.key.start, key_start);
2310 	COMPARE_PARAM(vres.key.size, key_size);
2311 #undef COMPARE_PARAM
2312 
2313 	return (rc);
2314 }
2315 
2316 static int
restart_lld(struct adapter * sc)2317 restart_lld(struct adapter *sc)
2318 {
2319 	struct adapter_pre_reset_state *old_state = NULL;
2320 	struct port_info *pi;
2321 	struct vi_info *vi;
2322 	if_t ifp;
2323 	struct sge_txq *txq;
2324 	int rc, i, j, k;
2325 
2326 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rlld");
2327 	if (rc != 0)
2328 		return (ENXIO);
2329 
2330 	/* Restore memory window. */
2331 	setup_memwin(sc);
2332 
2333 	/* Go no further if recovery mode has been requested. */
2334 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2335 		CH_ALERT(sc, "%s: recovery mode during restart.\n", __func__);
2336 		rc = 0;
2337 		set_adapter_hwstatus(sc, true);
2338 		goto done;
2339 	}
2340 
2341 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2342 	save_caps_and_params(sc, old_state);
2343 
2344 	/* Reestablish contact with firmware and become the primary PF. */
2345 	rc = contact_firmware(sc);
2346 	if (rc != 0)
2347 		goto done; /* error message displayed already */
2348 	MPASS(sc->flags & FW_OK);
2349 
2350 	if (sc->flags & MASTER_PF) {
2351 		rc = partition_resources(sc);
2352 		if (rc != 0)
2353 			goto done; /* error message displayed already */
2354 	}
2355 
2356 	rc = get_params__post_init(sc);
2357 	if (rc != 0)
2358 		goto done; /* error message displayed already */
2359 
2360 	rc = set_params__post_init(sc);
2361 	if (rc != 0)
2362 		goto done; /* error message displayed already */
2363 
2364 	rc = compare_caps_and_params(sc, old_state);
2365 	if (rc != 0)
2366 		goto done; /* error message displayed already */
2367 
2368 	for_each_port(sc, i) {
2369 		pi = sc->port[i];
2370 		MPASS(pi != NULL);
2371 		MPASS(pi->vi != NULL);
2372 		MPASS(pi->vi[0].dev == pi->dev);
2373 
2374 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2375 		if (rc != 0) {
2376 			CH_ERR(sc,
2377 			    "failed to re-initialize port %d: %d\n", i, rc);
2378 			goto done;
2379 		}
2380 		MPASS(sc->chan_map[pi->tx_chan] == i);
2381 
2382 		PORT_LOCK(pi);
2383 		fixup_link_config(pi);
2384 		build_medialist(pi);
2385 		PORT_UNLOCK(pi);
2386 		for_each_vi(pi, j, vi) {
2387 			if (IS_MAIN_VI(vi))
2388 				continue;
2389 			rc = alloc_extra_vi(sc, pi, vi);
2390 			if (rc != 0) {
2391 				CH_ERR(vi,
2392 				    "failed to re-allocate extra VI: %d\n", rc);
2393 				goto done;
2394 			}
2395 		}
2396 	}
2397 
2398 	/*
2399 	 * Interrupts and queues are about to be enabled and other threads will
2400 	 * want to access the hardware too.  It is safe to do so.  Note that
2401 	 * this thread is still in the middle of a synchronized_op.
2402 	 */
2403 	set_adapter_hwstatus(sc, true);
2404 
2405 	if (sc->flags & FULL_INIT_DONE) {
2406 		rc = adapter_full_init(sc);
2407 		if (rc != 0) {
2408 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2409 			goto done;
2410 		}
2411 
2412 		if (sc->vxlan_refcount > 0)
2413 			enable_vxlan_rx(sc);
2414 
2415 		for_each_port(sc, i) {
2416 			pi = sc->port[i];
2417 			for_each_vi(pi, j, vi) {
2418 				mtx_lock(&vi->tick_mtx);
2419 				vi->flags &= ~VI_SKIP_STATS;
2420 				mtx_unlock(&vi->tick_mtx);
2421 				if (!(vi->flags & VI_INIT_DONE))
2422 					continue;
2423 				rc = vi_full_init(vi);
2424 				if (rc != 0) {
2425 					CH_ERR(vi, "failed to re-initialize "
2426 					    "interface: %d\n", rc);
2427 					goto done;
2428 				}
2429 				if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
2430 					sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
2431 					t4_write_reg(sc, is_t4(sc) ?
2432 					    A_MPS_TRC_RSS_CONTROL :
2433 					    A_MPS_T5_TRC_RSS_CONTROL,
2434 					    V_RSSCONTROL(pi->tx_chan) |
2435 					    V_QUEUENUMBER(sc->traceq));
2436 					pi->flags |= HAS_TRACEQ;
2437 				}
2438 
2439 				ifp = vi->ifp;
2440 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2441 					continue;
2442 				/*
2443 				 * Note that we do not setup multicast addresses
2444 				 * in the first pass.  This ensures that the
2445 				 * unicast DMACs for all VIs on all ports get an
2446 				 * MPS TCAM entry.
2447 				 */
2448 				rc = update_mac_settings(ifp, XGMAC_ALL &
2449 				    ~XGMAC_MCADDRS);
2450 				if (rc != 0) {
2451 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2452 					goto done;
2453 				}
2454 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2455 				    true);
2456 				if (rc != 0) {
2457 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2458 					goto done;
2459 				}
2460 				for_each_txq(vi, k, txq) {
2461 					TXQ_LOCK(txq);
2462 					txq->eq.flags |= EQ_ENABLED;
2463 					TXQ_UNLOCK(txq);
2464 				}
2465 				mtx_lock(&vi->tick_mtx);
2466 				callout_schedule(&vi->tick, hz);
2467 				mtx_unlock(&vi->tick_mtx);
2468 			}
2469 			PORT_LOCK(pi);
2470 			if (pi->up_vis > 0) {
2471 				t4_update_port_info(pi);
2472 				fixup_link_config(pi);
2473 				build_medialist(pi);
2474 				apply_link_config(pi);
2475 				if (pi->link_cfg.link_ok)
2476 					t4_os_link_changed(pi);
2477 			}
2478 			PORT_UNLOCK(pi);
2479 		}
2480 
2481 		/* Now reprogram the L2 multicast addresses. */
2482 		for_each_port(sc, i) {
2483 			pi = sc->port[i];
2484 			for_each_vi(pi, j, vi) {
2485 				if (!(vi->flags & VI_INIT_DONE))
2486 					continue;
2487 				ifp = vi->ifp;
2488 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2489 					continue;
2490 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2491 				if (rc != 0) {
2492 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2493 					rc = 0;	/* carry on */
2494 				}
2495 			}
2496 		}
2497 	}
2498 
2499 	/* Reset all calibration */
2500 	t4_calibration_start(sc);
2501 done:
2502 	end_synchronized_op(sc, 0);
2503 	free(old_state, M_CXGBE);
2504 
2505 	restart_atid_allocator(sc);
2506 	t4_restart_l2t(sc);
2507 
2508 	return (rc);
2509 }
2510 
2511 int
resume_adapter(struct adapter * sc)2512 resume_adapter(struct adapter *sc)
2513 {
2514 	restart_adapter(sc);
2515 	restart_lld(sc);
2516 #ifdef TCP_OFFLOAD
2517 	restart_all_uld(sc);
2518 #endif
2519 	return (0);
2520 }
2521 
2522 static int
t4_resume(device_t dev)2523 t4_resume(device_t dev)
2524 {
2525 	struct adapter *sc = device_get_softc(dev);
2526 	int rc;
2527 
2528 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2529 	rc = resume_adapter(sc);
2530 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2531 
2532 	return (rc);
2533 }
2534 
2535 static int
t4_reset_prepare(device_t dev,device_t child)2536 t4_reset_prepare(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
t4_reset_post(device_t dev,device_t child)2545 t4_reset_post(device_t dev, device_t child)
2546 {
2547 	struct adapter *sc = device_get_softc(dev);
2548 
2549 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2550 	return (0);
2551 }
2552 
2553 static int
reset_adapter_with_pl_rst(struct adapter * sc)2554 reset_adapter_with_pl_rst(struct adapter *sc)
2555 {
2556 	/* This is a t4_write_reg without the hw_off_limits check. */
2557 	MPASS(sc->error_flags & HW_OFF_LIMITS);
2558 	bus_space_write_4(sc->bt, sc->bh, A_PL_RST,
2559 			  F_PIORSTMODE | F_PIORST | F_AUTOPCIEPAUSE);
2560 	pause("pl_rst", 1 * hz);		/* Wait 1s for reset */
2561 	return (0);
2562 }
2563 
2564 static int
reset_adapter_with_pcie_sbr(struct adapter * sc)2565 reset_adapter_with_pcie_sbr(struct adapter *sc)
2566 {
2567 	device_t pdev = device_get_parent(sc->dev);
2568 	device_t gpdev = device_get_parent(pdev);
2569 	device_t *children;
2570 	int rc, i, lcap, lsta, nchildren;
2571 	uint32_t v;
2572 
2573 	rc = pci_find_cap(gpdev, PCIY_EXPRESS, &v);
2574 	if (rc != 0) {
2575 		CH_ERR(sc, "%s: pci_find_cap(%s, pcie) failed: %d\n", __func__,
2576 		    device_get_nameunit(gpdev), rc);
2577 		return (ENOTSUP);
2578 	}
2579 	lcap = v + PCIER_LINK_CAP;
2580 	lsta = v + PCIER_LINK_STA;
2581 
2582 	nchildren = 0;
2583 	device_get_children(pdev, &children, &nchildren);
2584 	for (i = 0; i < nchildren; i++)
2585 		pci_save_state(children[i]);
2586 	v = pci_read_config(gpdev, PCIR_BRIDGECTL_1, 2);
2587 	pci_write_config(gpdev, PCIR_BRIDGECTL_1, v | PCIB_BCR_SECBUS_RESET, 2);
2588 	pause("pcie_sbr1", hz / 10);	/* 100ms */
2589 	pci_write_config(gpdev, PCIR_BRIDGECTL_1, v, 2);
2590 	pause("pcie_sbr2", hz);		/* Wait 1s before restore_state. */
2591 	v = pci_read_config(gpdev, lsta, 2);
2592 	if (pci_read_config(gpdev, lcap, 2) & PCIEM_LINK_CAP_DL_ACTIVE)
2593 		rc = v & PCIEM_LINK_STA_DL_ACTIVE ? 0 : ETIMEDOUT;
2594 	else if (v & (PCIEM_LINK_STA_TRAINING_ERROR | PCIEM_LINK_STA_TRAINING))
2595 		rc = ETIMEDOUT;
2596 	else
2597 		rc = 0;
2598 	if (rc != 0)
2599 		CH_ERR(sc, "%s: PCIe link is down after reset, LINK_STA 0x%x\n",
2600 		    __func__, v);
2601 	else {
2602 		for (i = 0; i < nchildren; i++)
2603 			pci_restore_state(children[i]);
2604 	}
2605 	free(children, M_TEMP);
2606 
2607 	return (rc);
2608 }
2609 
2610 static int
reset_adapter_with_pcie_link_bounce(struct adapter * sc)2611 reset_adapter_with_pcie_link_bounce(struct adapter *sc)
2612 {
2613 	device_t pdev = device_get_parent(sc->dev);
2614 	device_t gpdev = device_get_parent(pdev);
2615 	device_t *children;
2616 	int rc, i, lcap, lctl, lsta, nchildren;
2617 	uint32_t v;
2618 
2619 	rc = pci_find_cap(gpdev, PCIY_EXPRESS, &v);
2620 	if (rc != 0) {
2621 		CH_ERR(sc, "%s: pci_find_cap(%s, pcie) failed: %d\n", __func__,
2622 		    device_get_nameunit(gpdev), rc);
2623 		return (ENOTSUP);
2624 	}
2625 	lcap = v + PCIER_LINK_CAP;
2626 	lctl = v + PCIER_LINK_CTL;
2627 	lsta = v + PCIER_LINK_STA;
2628 
2629 	nchildren = 0;
2630 	device_get_children(pdev, &children, &nchildren);
2631 	for (i = 0; i < nchildren; i++)
2632 		pci_save_state(children[i]);
2633 	v = pci_read_config(gpdev, lctl, 2);
2634 	pci_write_config(gpdev, lctl, v | PCIEM_LINK_CTL_LINK_DIS, 2);
2635 	pause("pcie_lnk1", 100 * hz / 1000);	/* 100ms */
2636 	pci_write_config(gpdev, lctl, v | PCIEM_LINK_CTL_RETRAIN_LINK, 2);
2637 	pause("pcie_lnk2", hz);		/* Wait 1s before restore_state. */
2638 	v = pci_read_config(gpdev, lsta, 2);
2639 	if (pci_read_config(gpdev, lcap, 2) & PCIEM_LINK_CAP_DL_ACTIVE)
2640 		rc = v & PCIEM_LINK_STA_DL_ACTIVE ? 0 : ETIMEDOUT;
2641 	else if (v & (PCIEM_LINK_STA_TRAINING_ERROR | PCIEM_LINK_STA_TRAINING))
2642 		rc = ETIMEDOUT;
2643 	else
2644 		rc = 0;
2645 	if (rc != 0)
2646 		CH_ERR(sc, "%s: PCIe link is down after reset, LINK_STA 0x%x\n",
2647 		    __func__, v);
2648 	else {
2649 		for (i = 0; i < nchildren; i++)
2650 			pci_restore_state(children[i]);
2651 	}
2652 	free(children, M_TEMP);
2653 
2654 	return (rc);
2655 }
2656 
2657 static inline int
reset_adapter(struct adapter * sc)2658 reset_adapter(struct adapter *sc)
2659 {
2660 	int rc;
2661 	const int reset_method = vm_guest == VM_GUEST_NO ? t4_reset_method : 0;
2662 
2663 	rc = suspend_adapter(sc);
2664 	if (rc != 0)
2665 		return (rc);
2666 
2667 	switch (reset_method) {
2668 	case 1:
2669 		rc = reset_adapter_with_pcie_sbr(sc);
2670 		break;
2671 	case 2:
2672 		rc = reset_adapter_with_pcie_link_bounce(sc);
2673 		break;
2674 	case 0:
2675 	default:
2676 		rc = reset_adapter_with_pl_rst(sc);
2677 		break;
2678 	}
2679 	if (rc == 0)
2680 		rc = resume_adapter(sc);
2681 	return (rc);
2682 }
2683 
2684 static void
reset_adapter_task(void * arg,int pending)2685 reset_adapter_task(void *arg, int pending)
2686 {
2687 	struct adapter *sc = arg;
2688 	const int flags = sc->flags;
2689 	const int eflags = sc->error_flags;
2690 	int rc;
2691 
2692 	if (pending > 1)
2693 		CH_ALERT(sc, "%s: pending %d\n", __func__, pending);
2694 	rc = reset_adapter(sc);
2695 	if (rc != 0) {
2696 		CH_ERR(sc, "adapter did not reset properly, rc = %d, "
2697 		       "flags 0x%08x -> 0x%08x, err_flags 0x%08x -> 0x%08x.\n",
2698 		       rc, flags, sc->flags, eflags, sc->error_flags);
2699 	}
2700 }
2701 
2702 static int
cxgbe_probe(device_t dev)2703 cxgbe_probe(device_t dev)
2704 {
2705 	struct port_info *pi = device_get_softc(dev);
2706 
2707 	device_set_descf(dev, "port %d", pi->port_id);
2708 
2709 	return (BUS_PROBE_DEFAULT);
2710 }
2711 
2712 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2713     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2714     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2715     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2716 #define T4_CAP_ENABLE (T4_CAP)
2717 
2718 static void
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2719 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2720 {
2721 	if_t ifp;
2722 	struct sbuf *sb;
2723 	struct sysctl_ctx_list *ctx = &vi->ctx;
2724 	struct sysctl_oid_list *children;
2725 	struct pfil_head_args pa;
2726 	struct adapter *sc = vi->adapter;
2727 
2728 	sysctl_ctx_init(ctx);
2729 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2730 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2731 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2732 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2733 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2734 #ifdef DEV_NETMAP
2735 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2736 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2737 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2738 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2739 #endif
2740 #ifdef TCP_OFFLOAD
2741 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2742 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2743 #endif
2744 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2745 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2746 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2747 #endif
2748 
2749 	vi->xact_addr_filt = -1;
2750 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2751 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2752 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2753 		vi->flags |= TX_USES_VM_WR;
2754 
2755 	/* Allocate an ifnet and set it up */
2756 	ifp = if_alloc_dev(IFT_ETHER, dev);
2757 	vi->ifp = ifp;
2758 	if_setsoftc(ifp, vi);
2759 
2760 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2761 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2762 
2763 	if_setinitfn(ifp, cxgbe_init);
2764 	if_setioctlfn(ifp, cxgbe_ioctl);
2765 	if_settransmitfn(ifp, cxgbe_transmit);
2766 	if_setqflushfn(ifp, cxgbe_qflush);
2767 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2768 		if_setgetcounterfn(ifp, vi_get_counter);
2769 	else
2770 		if_setgetcounterfn(ifp, cxgbe_get_counter);
2771 #if defined(KERN_TLS) || defined(RATELIMIT)
2772 	if_setsndtagallocfn(ifp, cxgbe_snd_tag_alloc);
2773 #endif
2774 #ifdef RATELIMIT
2775 	if_setratelimitqueryfn(ifp, cxgbe_ratelimit_query);
2776 #endif
2777 
2778 	if_setcapabilities(ifp, T4_CAP);
2779 	if_setcapenable(ifp, T4_CAP_ENABLE);
2780 	if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2781 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2782 	if (chip_id(sc) >= CHELSIO_T6) {
2783 		if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2784 		if_setcapenablebit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2785 		if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2786 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2787 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN, 0);
2788 	}
2789 
2790 #ifdef TCP_OFFLOAD
2791 	if (vi->nofldrxq != 0)
2792 		if_setcapabilitiesbit(ifp, IFCAP_TOE, 0);
2793 #endif
2794 #ifdef RATELIMIT
2795 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2796 		if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT, 0);
2797 		if_setcapenablebit(ifp, IFCAP_TXRTLMT, 0);
2798 	}
2799 #endif
2800 
2801 	if_sethwtsomax(ifp, IP_MAXPACKET);
2802 	if (vi->flags & TX_USES_VM_WR)
2803 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_VM_TSO);
2804 	else
2805 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_TSO);
2806 #ifdef RATELIMIT
2807 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2808 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_EO_TSO);
2809 #endif
2810 	if_sethwtsomaxsegsize(ifp, 65536);
2811 #ifdef KERN_TLS
2812 	if (is_ktls(sc)) {
2813 		if_setcapabilitiesbit(ifp, IFCAP_TXTLS, 0);
2814 		if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2815 			if_setcapenablebit(ifp, IFCAP_TXTLS, 0);
2816 	}
2817 #endif
2818 
2819 	ether_ifattach(ifp, vi->hw_addr);
2820 #ifdef DEV_NETMAP
2821 	if (vi->nnmrxq != 0)
2822 		cxgbe_nm_attach(vi);
2823 #endif
2824 	sb = sbuf_new_auto();
2825 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2826 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2827 	switch (if_getcapabilities(ifp) & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2828 	case IFCAP_TOE:
2829 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2830 		break;
2831 	case IFCAP_TOE | IFCAP_TXRTLMT:
2832 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2833 		break;
2834 	case IFCAP_TXRTLMT:
2835 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2836 		break;
2837 	}
2838 #endif
2839 #ifdef TCP_OFFLOAD
2840 	if (if_getcapabilities(ifp) & IFCAP_TOE)
2841 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2842 #endif
2843 #ifdef DEV_NETMAP
2844 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2845 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2846 		    vi->nnmtxq, vi->nnmrxq);
2847 #endif
2848 	sbuf_finish(sb);
2849 	device_printf(dev, "%s\n", sbuf_data(sb));
2850 	sbuf_delete(sb);
2851 
2852 	vi_sysctls(vi);
2853 
2854 	pa.pa_version = PFIL_VERSION;
2855 	pa.pa_flags = PFIL_IN;
2856 	pa.pa_type = PFIL_TYPE_ETHERNET;
2857 	pa.pa_headname = if_name(ifp);
2858 	vi->pfil = pfil_head_register(&pa);
2859 }
2860 
2861 static int
cxgbe_attach(device_t dev)2862 cxgbe_attach(device_t dev)
2863 {
2864 	struct port_info *pi = device_get_softc(dev);
2865 	struct adapter *sc = pi->adapter;
2866 	struct vi_info *vi;
2867 	int i;
2868 
2869 	sysctl_ctx_init(&pi->ctx);
2870 
2871 	cxgbe_vi_attach(dev, &pi->vi[0]);
2872 
2873 	for_each_vi(pi, i, vi) {
2874 		if (i == 0)
2875 			continue;
2876 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, -1);
2877 		if (vi->dev == NULL) {
2878 			device_printf(dev, "failed to add VI %d\n", i);
2879 			continue;
2880 		}
2881 		device_set_softc(vi->dev, vi);
2882 	}
2883 
2884 	cxgbe_sysctls(pi);
2885 
2886 	bus_generic_attach(dev);
2887 
2888 	return (0);
2889 }
2890 
2891 static void
cxgbe_vi_detach(struct vi_info * vi)2892 cxgbe_vi_detach(struct vi_info *vi)
2893 {
2894 	if_t ifp = vi->ifp;
2895 
2896 	if (vi->pfil != NULL) {
2897 		pfil_head_unregister(vi->pfil);
2898 		vi->pfil = NULL;
2899 	}
2900 
2901 	ether_ifdetach(ifp);
2902 
2903 	/* Let detach proceed even if these fail. */
2904 #ifdef DEV_NETMAP
2905 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2906 		cxgbe_nm_detach(vi);
2907 #endif
2908 	cxgbe_uninit_synchronized(vi);
2909 	callout_drain(&vi->tick);
2910 	mtx_destroy(&vi->tick_mtx);
2911 	sysctl_ctx_free(&vi->ctx);
2912 	vi_full_uninit(vi);
2913 
2914 	if_free(vi->ifp);
2915 	vi->ifp = NULL;
2916 }
2917 
2918 static int
cxgbe_detach(device_t dev)2919 cxgbe_detach(device_t dev)
2920 {
2921 	struct port_info *pi = device_get_softc(dev);
2922 	struct adapter *sc = pi->adapter;
2923 	int rc;
2924 
2925 	/* Detach the extra VIs first. */
2926 	rc = bus_generic_detach(dev);
2927 	if (rc)
2928 		return (rc);
2929 	device_delete_children(dev);
2930 
2931 	sysctl_ctx_free(&pi->ctx);
2932 	begin_vi_detach(sc, &pi->vi[0]);
2933 	if (pi->flags & HAS_TRACEQ) {
2934 		sc->traceq = -1;	/* cloner should not create ifnet */
2935 		t4_tracer_port_detach(sc);
2936 	}
2937 	cxgbe_vi_detach(&pi->vi[0]);
2938 	ifmedia_removeall(&pi->media);
2939 	end_vi_detach(sc, &pi->vi[0]);
2940 
2941 	return (0);
2942 }
2943 
2944 static void
cxgbe_init(void * arg)2945 cxgbe_init(void *arg)
2946 {
2947 	struct vi_info *vi = arg;
2948 	struct adapter *sc = vi->adapter;
2949 
2950 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
2951 		return;
2952 	cxgbe_init_synchronized(vi);
2953 	end_synchronized_op(sc, 0);
2954 }
2955 
2956 static int
cxgbe_ioctl(if_t ifp,unsigned long cmd,caddr_t data)2957 cxgbe_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
2958 {
2959 	int rc = 0, mtu, flags;
2960 	struct vi_info *vi = if_getsoftc(ifp);
2961 	struct port_info *pi = vi->pi;
2962 	struct adapter *sc = pi->adapter;
2963 	struct ifreq *ifr = (struct ifreq *)data;
2964 	uint32_t mask;
2965 
2966 	switch (cmd) {
2967 	case SIOCSIFMTU:
2968 		mtu = ifr->ifr_mtu;
2969 		if (mtu < ETHERMIN || mtu > MAX_MTU)
2970 			return (EINVAL);
2971 
2972 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
2973 		if (rc)
2974 			return (rc);
2975 		if_setmtu(ifp, mtu);
2976 		if (vi->flags & VI_INIT_DONE) {
2977 			t4_update_fl_bufsize(ifp);
2978 			if (hw_all_ok(sc) &&
2979 			    if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2980 				rc = update_mac_settings(ifp, XGMAC_MTU);
2981 		}
2982 		end_synchronized_op(sc, 0);
2983 		break;
2984 
2985 	case SIOCSIFFLAGS:
2986 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
2987 		if (rc)
2988 			return (rc);
2989 
2990 		if (!hw_all_ok(sc)) {
2991 			rc = ENXIO;
2992 			goto fail;
2993 		}
2994 
2995 		if (if_getflags(ifp) & IFF_UP) {
2996 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2997 				flags = vi->if_flags;
2998 				if ((if_getflags(ifp) ^ flags) &
2999 				    (IFF_PROMISC | IFF_ALLMULTI)) {
3000 					rc = update_mac_settings(ifp,
3001 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
3002 				}
3003 			} else {
3004 				rc = cxgbe_init_synchronized(vi);
3005 			}
3006 			vi->if_flags = if_getflags(ifp);
3007 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3008 			rc = cxgbe_uninit_synchronized(vi);
3009 		}
3010 		end_synchronized_op(sc, 0);
3011 		break;
3012 
3013 	case SIOCADDMULTI:
3014 	case SIOCDELMULTI:
3015 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
3016 		if (rc)
3017 			return (rc);
3018 		if (hw_all_ok(sc) && if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3019 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
3020 		end_synchronized_op(sc, 0);
3021 		break;
3022 
3023 	case SIOCSIFCAP:
3024 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
3025 		if (rc)
3026 			return (rc);
3027 
3028 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
3029 		if (mask & IFCAP_TXCSUM) {
3030 			if_togglecapenable(ifp, IFCAP_TXCSUM);
3031 			if_togglehwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP);
3032 
3033 			if (IFCAP_TSO4 & if_getcapenable(ifp) &&
3034 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3035 				mask &= ~IFCAP_TSO4;
3036 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
3037 				if_printf(ifp,
3038 				    "tso4 disabled due to -txcsum.\n");
3039 			}
3040 		}
3041 		if (mask & IFCAP_TXCSUM_IPV6) {
3042 			if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
3043 			if_togglehwassist(ifp, CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
3044 
3045 			if (IFCAP_TSO6 & if_getcapenable(ifp) &&
3046 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3047 				mask &= ~IFCAP_TSO6;
3048 				if_setcapenablebit(ifp, 0, IFCAP_TSO6);
3049 				if_printf(ifp,
3050 				    "tso6 disabled due to -txcsum6.\n");
3051 			}
3052 		}
3053 		if (mask & IFCAP_RXCSUM)
3054 			if_togglecapenable(ifp, IFCAP_RXCSUM);
3055 		if (mask & IFCAP_RXCSUM_IPV6)
3056 			if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
3057 
3058 		/*
3059 		 * Note that we leave CSUM_TSO alone (it is always set).  The
3060 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
3061 		 * sending a TSO request our way, so it's sufficient to toggle
3062 		 * IFCAP_TSOx only.
3063 		 */
3064 		if (mask & IFCAP_TSO4) {
3065 			if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
3066 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3067 				if_printf(ifp, "enable txcsum first.\n");
3068 				rc = EAGAIN;
3069 				goto fail;
3070 			}
3071 			if_togglecapenable(ifp, IFCAP_TSO4);
3072 		}
3073 		if (mask & IFCAP_TSO6) {
3074 			if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
3075 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3076 				if_printf(ifp, "enable txcsum6 first.\n");
3077 				rc = EAGAIN;
3078 				goto fail;
3079 			}
3080 			if_togglecapenable(ifp, IFCAP_TSO6);
3081 		}
3082 		if (mask & IFCAP_LRO) {
3083 #if defined(INET) || defined(INET6)
3084 			int i;
3085 			struct sge_rxq *rxq;
3086 
3087 			if_togglecapenable(ifp, IFCAP_LRO);
3088 			for_each_rxq(vi, i, rxq) {
3089 				if (if_getcapenable(ifp) & IFCAP_LRO)
3090 					rxq->iq.flags |= IQ_LRO_ENABLED;
3091 				else
3092 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
3093 			}
3094 #endif
3095 		}
3096 #ifdef TCP_OFFLOAD
3097 		if (mask & IFCAP_TOE) {
3098 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TOE;
3099 
3100 			rc = toe_capability(vi, enable);
3101 			if (rc != 0)
3102 				goto fail;
3103 
3104 			if_togglecapenable(ifp, mask);
3105 		}
3106 #endif
3107 		if (mask & IFCAP_VLAN_HWTAGGING) {
3108 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3109 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3110 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
3111 		}
3112 		if (mask & IFCAP_VLAN_MTU) {
3113 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
3114 
3115 			/* Need to find out how to disable auto-mtu-inflation */
3116 		}
3117 		if (mask & IFCAP_VLAN_HWTSO)
3118 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3119 		if (mask & IFCAP_VLAN_HWCSUM)
3120 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
3121 #ifdef RATELIMIT
3122 		if (mask & IFCAP_TXRTLMT)
3123 			if_togglecapenable(ifp, IFCAP_TXRTLMT);
3124 #endif
3125 		if (mask & IFCAP_HWRXTSTMP) {
3126 			int i;
3127 			struct sge_rxq *rxq;
3128 
3129 			if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3130 			for_each_rxq(vi, i, rxq) {
3131 				if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3132 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
3133 				else
3134 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
3135 			}
3136 		}
3137 		if (mask & IFCAP_MEXTPG)
3138 			if_togglecapenable(ifp, IFCAP_MEXTPG);
3139 
3140 #ifdef KERN_TLS
3141 		if (mask & IFCAP_TXTLS) {
3142 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TXTLS;
3143 
3144 			rc = ktls_capability(sc, enable);
3145 			if (rc != 0)
3146 				goto fail;
3147 
3148 			if_togglecapenable(ifp, mask & IFCAP_TXTLS);
3149 		}
3150 #endif
3151 		if (mask & IFCAP_VXLAN_HWCSUM) {
3152 			if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3153 			if_togglehwassist(ifp, CSUM_INNER_IP6_UDP |
3154 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
3155 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP);
3156 		}
3157 		if (mask & IFCAP_VXLAN_HWTSO) {
3158 			if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3159 			if_togglehwassist(ifp, CSUM_INNER_IP6_TSO |
3160 			    CSUM_INNER_IP_TSO);
3161 		}
3162 
3163 #ifdef VLAN_CAPABILITIES
3164 		VLAN_CAPABILITIES(ifp);
3165 #endif
3166 fail:
3167 		end_synchronized_op(sc, 0);
3168 		break;
3169 
3170 	case SIOCSIFMEDIA:
3171 	case SIOCGIFMEDIA:
3172 	case SIOCGIFXMEDIA:
3173 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3174 		break;
3175 
3176 	case SIOCGI2C: {
3177 		struct ifi2creq i2c;
3178 
3179 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3180 		if (rc != 0)
3181 			break;
3182 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3183 			rc = EPERM;
3184 			break;
3185 		}
3186 		if (i2c.len > sizeof(i2c.data)) {
3187 			rc = EINVAL;
3188 			break;
3189 		}
3190 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3191 		if (rc)
3192 			return (rc);
3193 		if (!hw_all_ok(sc))
3194 			rc = ENXIO;
3195 		else
3196 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3197 			    i2c.offset, i2c.len, &i2c.data[0]);
3198 		end_synchronized_op(sc, 0);
3199 		if (rc == 0)
3200 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3201 		break;
3202 	}
3203 
3204 	default:
3205 		rc = ether_ioctl(ifp, cmd, data);
3206 	}
3207 
3208 	return (rc);
3209 }
3210 
3211 static int
cxgbe_transmit(if_t ifp,struct mbuf * m)3212 cxgbe_transmit(if_t ifp, struct mbuf *m)
3213 {
3214 	struct vi_info *vi = if_getsoftc(ifp);
3215 	struct port_info *pi = vi->pi;
3216 	struct adapter *sc;
3217 	struct sge_txq *txq;
3218 	void *items[1];
3219 	int rc;
3220 
3221 	M_ASSERTPKTHDR(m);
3222 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
3223 #if defined(KERN_TLS) || defined(RATELIMIT)
3224 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3225 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3226 #endif
3227 
3228 	if (__predict_false(pi->link_cfg.link_ok == false)) {
3229 		m_freem(m);
3230 		return (ENETDOWN);
3231 	}
3232 
3233 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3234 	if (__predict_false(rc != 0)) {
3235 		if (__predict_true(rc == EINPROGRESS)) {
3236 			/* queued by parse_pkt */
3237 			MPASS(m != NULL);
3238 			return (0);
3239 		}
3240 
3241 		MPASS(m == NULL);			/* was freed already */
3242 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
3243 		return (rc);
3244 	}
3245 
3246 	/* Select a txq. */
3247 	sc = vi->adapter;
3248 	txq = &sc->sge.txq[vi->first_txq];
3249 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3250 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3251 		    vi->rsrv_noflowq);
3252 
3253 	items[0] = m;
3254 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3255 	if (__predict_false(rc != 0))
3256 		m_freem(m);
3257 
3258 	return (rc);
3259 }
3260 
3261 static void
cxgbe_qflush(if_t ifp)3262 cxgbe_qflush(if_t ifp)
3263 {
3264 	struct vi_info *vi = if_getsoftc(ifp);
3265 	struct sge_txq *txq;
3266 	int i;
3267 
3268 	/* queues do not exist if !VI_INIT_DONE. */
3269 	if (vi->flags & VI_INIT_DONE) {
3270 		for_each_txq(vi, i, txq) {
3271 			TXQ_LOCK(txq);
3272 			txq->eq.flags |= EQ_QFLUSH;
3273 			TXQ_UNLOCK(txq);
3274 			while (!mp_ring_is_idle(txq->r)) {
3275 				mp_ring_check_drainage(txq->r, 4096);
3276 				pause("qflush", 1);
3277 			}
3278 			TXQ_LOCK(txq);
3279 			txq->eq.flags &= ~EQ_QFLUSH;
3280 			TXQ_UNLOCK(txq);
3281 		}
3282 	}
3283 	if_qflush(ifp);
3284 }
3285 
3286 static uint64_t
vi_get_counter(if_t ifp,ift_counter c)3287 vi_get_counter(if_t ifp, ift_counter c)
3288 {
3289 	struct vi_info *vi = if_getsoftc(ifp);
3290 	struct fw_vi_stats_vf *s = &vi->stats;
3291 
3292 	mtx_lock(&vi->tick_mtx);
3293 	vi_refresh_stats(vi);
3294 	mtx_unlock(&vi->tick_mtx);
3295 
3296 	switch (c) {
3297 	case IFCOUNTER_IPACKETS:
3298 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3299 		    s->rx_ucast_frames);
3300 	case IFCOUNTER_IERRORS:
3301 		return (s->rx_err_frames);
3302 	case IFCOUNTER_OPACKETS:
3303 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3304 		    s->tx_ucast_frames + s->tx_offload_frames);
3305 	case IFCOUNTER_OERRORS:
3306 		return (s->tx_drop_frames);
3307 	case IFCOUNTER_IBYTES:
3308 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3309 		    s->rx_ucast_bytes);
3310 	case IFCOUNTER_OBYTES:
3311 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3312 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3313 	case IFCOUNTER_IMCASTS:
3314 		return (s->rx_mcast_frames);
3315 	case IFCOUNTER_OMCASTS:
3316 		return (s->tx_mcast_frames);
3317 	case IFCOUNTER_OQDROPS: {
3318 		uint64_t drops;
3319 
3320 		drops = 0;
3321 		if (vi->flags & VI_INIT_DONE) {
3322 			int i;
3323 			struct sge_txq *txq;
3324 
3325 			for_each_txq(vi, i, txq)
3326 				drops += counter_u64_fetch(txq->r->dropped);
3327 		}
3328 
3329 		return (drops);
3330 
3331 	}
3332 
3333 	default:
3334 		return (if_get_counter_default(ifp, c));
3335 	}
3336 }
3337 
3338 static uint64_t
cxgbe_get_counter(if_t ifp,ift_counter c)3339 cxgbe_get_counter(if_t ifp, ift_counter c)
3340 {
3341 	struct vi_info *vi = if_getsoftc(ifp);
3342 	struct port_info *pi = vi->pi;
3343 	struct port_stats *s = &pi->stats;
3344 
3345 	mtx_lock(&vi->tick_mtx);
3346 	cxgbe_refresh_stats(vi);
3347 	mtx_unlock(&vi->tick_mtx);
3348 
3349 	switch (c) {
3350 	case IFCOUNTER_IPACKETS:
3351 		return (s->rx_frames);
3352 
3353 	case IFCOUNTER_IERRORS:
3354 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3355 		    s->rx_fcs_err + s->rx_len_err);
3356 
3357 	case IFCOUNTER_OPACKETS:
3358 		return (s->tx_frames);
3359 
3360 	case IFCOUNTER_OERRORS:
3361 		return (s->tx_error_frames);
3362 
3363 	case IFCOUNTER_IBYTES:
3364 		return (s->rx_octets);
3365 
3366 	case IFCOUNTER_OBYTES:
3367 		return (s->tx_octets);
3368 
3369 	case IFCOUNTER_IMCASTS:
3370 		return (s->rx_mcast_frames);
3371 
3372 	case IFCOUNTER_OMCASTS:
3373 		return (s->tx_mcast_frames);
3374 
3375 	case IFCOUNTER_IQDROPS:
3376 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3377 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3378 		    s->rx_trunc3 + pi->tnl_cong_drops);
3379 
3380 	case IFCOUNTER_OQDROPS: {
3381 		uint64_t drops;
3382 
3383 		drops = s->tx_drop;
3384 		if (vi->flags & VI_INIT_DONE) {
3385 			int i;
3386 			struct sge_txq *txq;
3387 
3388 			for_each_txq(vi, i, txq)
3389 				drops += counter_u64_fetch(txq->r->dropped);
3390 		}
3391 
3392 		return (drops);
3393 
3394 	}
3395 
3396 	default:
3397 		return (if_get_counter_default(ifp, c));
3398 	}
3399 }
3400 
3401 #if defined(KERN_TLS) || defined(RATELIMIT)
3402 static int
cxgbe_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3403 cxgbe_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3404     struct m_snd_tag **pt)
3405 {
3406 	int error;
3407 
3408 	switch (params->hdr.type) {
3409 #ifdef RATELIMIT
3410 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3411 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3412 		break;
3413 #endif
3414 #ifdef KERN_TLS
3415 	case IF_SND_TAG_TYPE_TLS:
3416 	{
3417 		struct vi_info *vi = if_getsoftc(ifp);
3418 
3419 		if (is_t6(vi->pi->adapter))
3420 			error = t6_tls_tag_alloc(ifp, params, pt);
3421 		else
3422 			error = EOPNOTSUPP;
3423 		break;
3424 	}
3425 #endif
3426 	default:
3427 		error = EOPNOTSUPP;
3428 	}
3429 	return (error);
3430 }
3431 #endif
3432 
3433 /*
3434  * The kernel picks a media from the list we had provided but we still validate
3435  * the requeste.
3436  */
3437 int
cxgbe_media_change(if_t ifp)3438 cxgbe_media_change(if_t ifp)
3439 {
3440 	struct vi_info *vi = if_getsoftc(ifp);
3441 	struct port_info *pi = vi->pi;
3442 	struct ifmedia *ifm = &pi->media;
3443 	struct link_config *lc = &pi->link_cfg;
3444 	struct adapter *sc = pi->adapter;
3445 	int rc;
3446 
3447 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3448 	if (rc != 0)
3449 		return (rc);
3450 	PORT_LOCK(pi);
3451 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3452 		/* ifconfig .. media autoselect */
3453 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3454 			rc = ENOTSUP; /* AN not supported by transceiver */
3455 			goto done;
3456 		}
3457 		lc->requested_aneg = AUTONEG_ENABLE;
3458 		lc->requested_speed = 0;
3459 		lc->requested_fc |= PAUSE_AUTONEG;
3460 	} else {
3461 		lc->requested_aneg = AUTONEG_DISABLE;
3462 		lc->requested_speed =
3463 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3464 		lc->requested_fc = 0;
3465 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3466 			lc->requested_fc |= PAUSE_RX;
3467 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3468 			lc->requested_fc |= PAUSE_TX;
3469 	}
3470 	if (pi->up_vis > 0 && hw_all_ok(sc)) {
3471 		fixup_link_config(pi);
3472 		rc = apply_link_config(pi);
3473 	}
3474 done:
3475 	PORT_UNLOCK(pi);
3476 	end_synchronized_op(sc, 0);
3477 	return (rc);
3478 }
3479 
3480 /*
3481  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3482  * given speed.
3483  */
3484 static int
port_mword(struct port_info * pi,uint32_t speed)3485 port_mword(struct port_info *pi, uint32_t speed)
3486 {
3487 
3488 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3489 	MPASS(powerof2(speed));
3490 
3491 	switch(pi->port_type) {
3492 	case FW_PORT_TYPE_BT_SGMII:
3493 	case FW_PORT_TYPE_BT_XFI:
3494 	case FW_PORT_TYPE_BT_XAUI:
3495 		/* BaseT */
3496 		switch (speed) {
3497 		case FW_PORT_CAP32_SPEED_100M:
3498 			return (IFM_100_T);
3499 		case FW_PORT_CAP32_SPEED_1G:
3500 			return (IFM_1000_T);
3501 		case FW_PORT_CAP32_SPEED_10G:
3502 			return (IFM_10G_T);
3503 		}
3504 		break;
3505 	case FW_PORT_TYPE_KX4:
3506 		if (speed == FW_PORT_CAP32_SPEED_10G)
3507 			return (IFM_10G_KX4);
3508 		break;
3509 	case FW_PORT_TYPE_CX4:
3510 		if (speed == FW_PORT_CAP32_SPEED_10G)
3511 			return (IFM_10G_CX4);
3512 		break;
3513 	case FW_PORT_TYPE_KX:
3514 		if (speed == FW_PORT_CAP32_SPEED_1G)
3515 			return (IFM_1000_KX);
3516 		break;
3517 	case FW_PORT_TYPE_KR:
3518 	case FW_PORT_TYPE_BP_AP:
3519 	case FW_PORT_TYPE_BP4_AP:
3520 	case FW_PORT_TYPE_BP40_BA:
3521 	case FW_PORT_TYPE_KR4_100G:
3522 	case FW_PORT_TYPE_KR_SFP28:
3523 	case FW_PORT_TYPE_KR_XLAUI:
3524 		switch (speed) {
3525 		case FW_PORT_CAP32_SPEED_1G:
3526 			return (IFM_1000_KX);
3527 		case FW_PORT_CAP32_SPEED_10G:
3528 			return (IFM_10G_KR);
3529 		case FW_PORT_CAP32_SPEED_25G:
3530 			return (IFM_25G_KR);
3531 		case FW_PORT_CAP32_SPEED_40G:
3532 			return (IFM_40G_KR4);
3533 		case FW_PORT_CAP32_SPEED_50G:
3534 			return (IFM_50G_KR2);
3535 		case FW_PORT_CAP32_SPEED_100G:
3536 			return (IFM_100G_KR4);
3537 		}
3538 		break;
3539 	case FW_PORT_TYPE_FIBER_XFI:
3540 	case FW_PORT_TYPE_FIBER_XAUI:
3541 	case FW_PORT_TYPE_SFP:
3542 	case FW_PORT_TYPE_QSFP_10G:
3543 	case FW_PORT_TYPE_QSA:
3544 	case FW_PORT_TYPE_QSFP:
3545 	case FW_PORT_TYPE_CR4_QSFP:
3546 	case FW_PORT_TYPE_CR_QSFP:
3547 	case FW_PORT_TYPE_CR2_QSFP:
3548 	case FW_PORT_TYPE_SFP28:
3549 		/* Pluggable transceiver */
3550 		switch (pi->mod_type) {
3551 		case FW_PORT_MOD_TYPE_LR:
3552 		case FW_PORT_MOD_TYPE_LR_SIMPLEX:
3553 			switch (speed) {
3554 			case FW_PORT_CAP32_SPEED_1G:
3555 				return (IFM_1000_LX);
3556 			case FW_PORT_CAP32_SPEED_10G:
3557 				return (IFM_10G_LR);
3558 			case FW_PORT_CAP32_SPEED_25G:
3559 				return (IFM_25G_LR);
3560 			case FW_PORT_CAP32_SPEED_40G:
3561 				return (IFM_40G_LR4);
3562 			case FW_PORT_CAP32_SPEED_50G:
3563 				return (IFM_50G_LR2);
3564 			case FW_PORT_CAP32_SPEED_100G:
3565 				return (IFM_100G_LR4);
3566 			}
3567 			break;
3568 		case FW_PORT_MOD_TYPE_SR:
3569 			switch (speed) {
3570 			case FW_PORT_CAP32_SPEED_1G:
3571 				return (IFM_1000_SX);
3572 			case FW_PORT_CAP32_SPEED_10G:
3573 				return (IFM_10G_SR);
3574 			case FW_PORT_CAP32_SPEED_25G:
3575 				return (IFM_25G_SR);
3576 			case FW_PORT_CAP32_SPEED_40G:
3577 				return (IFM_40G_SR4);
3578 			case FW_PORT_CAP32_SPEED_50G:
3579 				return (IFM_50G_SR2);
3580 			case FW_PORT_CAP32_SPEED_100G:
3581 				return (IFM_100G_SR4);
3582 			}
3583 			break;
3584 		case FW_PORT_MOD_TYPE_ER:
3585 			if (speed == FW_PORT_CAP32_SPEED_10G)
3586 				return (IFM_10G_ER);
3587 			break;
3588 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3589 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3590 			switch (speed) {
3591 			case FW_PORT_CAP32_SPEED_1G:
3592 				return (IFM_1000_CX);
3593 			case FW_PORT_CAP32_SPEED_10G:
3594 				return (IFM_10G_TWINAX);
3595 			case FW_PORT_CAP32_SPEED_25G:
3596 				return (IFM_25G_CR);
3597 			case FW_PORT_CAP32_SPEED_40G:
3598 				return (IFM_40G_CR4);
3599 			case FW_PORT_CAP32_SPEED_50G:
3600 				return (IFM_50G_CR2);
3601 			case FW_PORT_CAP32_SPEED_100G:
3602 				return (IFM_100G_CR4);
3603 			}
3604 			break;
3605 		case FW_PORT_MOD_TYPE_LRM:
3606 			if (speed == FW_PORT_CAP32_SPEED_10G)
3607 				return (IFM_10G_LRM);
3608 			break;
3609 		case FW_PORT_MOD_TYPE_DR:
3610 			if (speed == FW_PORT_CAP32_SPEED_100G)
3611 				return (IFM_100G_DR);
3612 			break;
3613 		case FW_PORT_MOD_TYPE_NA:
3614 			MPASS(0);	/* Not pluggable? */
3615 			/* fall throough */
3616 		case FW_PORT_MOD_TYPE_ERROR:
3617 		case FW_PORT_MOD_TYPE_UNKNOWN:
3618 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3619 			break;
3620 		case FW_PORT_MOD_TYPE_NONE:
3621 			return (IFM_NONE);
3622 		}
3623 		break;
3624 	case FW_PORT_TYPE_NONE:
3625 		return (IFM_NONE);
3626 	}
3627 
3628 	return (IFM_UNKNOWN);
3629 }
3630 
3631 void
cxgbe_media_status(if_t ifp,struct ifmediareq * ifmr)3632 cxgbe_media_status(if_t ifp, struct ifmediareq *ifmr)
3633 {
3634 	struct vi_info *vi = if_getsoftc(ifp);
3635 	struct port_info *pi = vi->pi;
3636 	struct adapter *sc = pi->adapter;
3637 	struct link_config *lc = &pi->link_cfg;
3638 
3639 	if (begin_synchronized_op(sc, vi , SLEEP_OK | INTR_OK, "t4med") != 0)
3640 		return;
3641 	PORT_LOCK(pi);
3642 
3643 	if (pi->up_vis == 0 && hw_all_ok(sc)) {
3644 		/*
3645 		 * If all the interfaces are administratively down the firmware
3646 		 * does not report transceiver changes.  Refresh port info here
3647 		 * so that ifconfig displays accurate ifmedia at all times.
3648 		 * This is the only reason we have a synchronized op in this
3649 		 * function.  Just PORT_LOCK would have been enough otherwise.
3650 		 */
3651 		t4_update_port_info(pi);
3652 		build_medialist(pi);
3653 	}
3654 
3655 	/* ifm_status */
3656 	ifmr->ifm_status = IFM_AVALID;
3657 	if (lc->link_ok == false)
3658 		goto done;
3659 	ifmr->ifm_status |= IFM_ACTIVE;
3660 
3661 	/* ifm_active */
3662 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3663 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3664 	if (lc->fc & PAUSE_RX)
3665 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3666 	if (lc->fc & PAUSE_TX)
3667 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3668 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3669 done:
3670 	PORT_UNLOCK(pi);
3671 	end_synchronized_op(sc, 0);
3672 }
3673 
3674 static int
vcxgbe_probe(device_t dev)3675 vcxgbe_probe(device_t dev)
3676 {
3677 	struct vi_info *vi = device_get_softc(dev);
3678 
3679 	device_set_descf(dev, "port %d vi %td", vi->pi->port_id,
3680 	    vi - vi->pi->vi);
3681 
3682 	return (BUS_PROBE_DEFAULT);
3683 }
3684 
3685 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3686 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3687 {
3688 	int func, index, rc;
3689 	uint32_t param, val;
3690 
3691 	ASSERT_SYNCHRONIZED_OP(sc);
3692 
3693 	index = vi - pi->vi;
3694 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3695 	KASSERT(index < nitems(vi_mac_funcs),
3696 	    ("%s: VI %s doesn't have a MAC func", __func__,
3697 	    device_get_nameunit(vi->dev)));
3698 	func = vi_mac_funcs[index];
3699 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
3700 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3701 	if (rc < 0) {
3702 		CH_ERR(vi, "failed to allocate virtual interface %d"
3703 		    "for port %d: %d\n", index, pi->port_id, -rc);
3704 		return (-rc);
3705 	}
3706 	vi->viid = rc;
3707 
3708 	if (vi->rss_size == 1) {
3709 		/*
3710 		 * This VI didn't get a slice of the RSS table.  Reduce the
3711 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3712 		 * configuration file (nvi, rssnvi for this PF) if this is a
3713 		 * problem.
3714 		 */
3715 		device_printf(vi->dev, "RSS table not available.\n");
3716 		vi->rss_base = 0xffff;
3717 
3718 		return (0);
3719 	}
3720 
3721 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3722 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3723 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3724 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3725 	if (rc)
3726 		vi->rss_base = 0xffff;
3727 	else {
3728 		MPASS((val >> 16) == vi->rss_size);
3729 		vi->rss_base = val & 0xffff;
3730 	}
3731 
3732 	return (0);
3733 }
3734 
3735 static int
vcxgbe_attach(device_t dev)3736 vcxgbe_attach(device_t dev)
3737 {
3738 	struct vi_info *vi;
3739 	struct port_info *pi;
3740 	struct adapter *sc;
3741 	int rc;
3742 
3743 	vi = device_get_softc(dev);
3744 	pi = vi->pi;
3745 	sc = pi->adapter;
3746 
3747 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3748 	if (rc)
3749 		return (rc);
3750 	rc = alloc_extra_vi(sc, pi, vi);
3751 	end_synchronized_op(sc, 0);
3752 	if (rc)
3753 		return (rc);
3754 
3755 	cxgbe_vi_attach(dev, vi);
3756 
3757 	return (0);
3758 }
3759 
3760 static int
vcxgbe_detach(device_t dev)3761 vcxgbe_detach(device_t dev)
3762 {
3763 	struct vi_info *vi;
3764 	struct adapter *sc;
3765 
3766 	vi = device_get_softc(dev);
3767 	sc = vi->adapter;
3768 
3769 	begin_vi_detach(sc, vi);
3770 	cxgbe_vi_detach(vi);
3771 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3772 	end_vi_detach(sc, vi);
3773 
3774 	return (0);
3775 }
3776 
3777 static struct callout fatal_callout;
3778 static struct taskqueue *reset_tq;
3779 
3780 static void
delayed_panic(void * arg)3781 delayed_panic(void *arg)
3782 {
3783 	struct adapter *sc = arg;
3784 
3785 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3786 }
3787 
3788 static void
fatal_error_task(void * arg,int pending)3789 fatal_error_task(void *arg, int pending)
3790 {
3791 	struct adapter *sc = arg;
3792 	int rc;
3793 
3794 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3795 		dump_cim_regs(sc);
3796 		dump_cimla(sc);
3797 		dump_devlog(sc);
3798 	}
3799 
3800 	if (t4_reset_on_fatal_err) {
3801 		CH_ALERT(sc, "resetting adapter after fatal error.\n");
3802 		rc = reset_adapter(sc);
3803 		if (rc == 0 && t4_panic_on_fatal_err) {
3804 			CH_ALERT(sc, "reset was successful, "
3805 			    "system will NOT panic.\n");
3806 			return;
3807 		}
3808 	}
3809 
3810 	if (t4_panic_on_fatal_err) {
3811 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3812 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3813 	}
3814 }
3815 
3816 void
t4_fatal_err(struct adapter * sc,bool fw_error)3817 t4_fatal_err(struct adapter *sc, bool fw_error)
3818 {
3819 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3820 
3821 	stop_adapter(sc);
3822 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3823 		return;
3824 	if (fw_error) {
3825 		/*
3826 		 * We are here because of a firmware error/timeout and not
3827 		 * because of a hardware interrupt.  It is possible (although
3828 		 * not very likely) that an error interrupt was also raised but
3829 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3830 		 * main INT_CAUSE registers here to make sure we haven't missed
3831 		 * anything interesting.
3832 		 */
3833 		t4_slow_intr_handler(sc, verbose);
3834 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3835 	}
3836 	t4_report_fw_error(sc);
3837 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3838 	    device_get_nameunit(sc->dev), fw_error);
3839 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3840 }
3841 
3842 void
t4_add_adapter(struct adapter * sc)3843 t4_add_adapter(struct adapter *sc)
3844 {
3845 	sx_xlock(&t4_list_lock);
3846 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3847 	sx_xunlock(&t4_list_lock);
3848 }
3849 
3850 int
t4_map_bars_0_and_4(struct adapter * sc)3851 t4_map_bars_0_and_4(struct adapter *sc)
3852 {
3853 	sc->regs_rid = PCIR_BAR(0);
3854 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3855 	    &sc->regs_rid, RF_ACTIVE);
3856 	if (sc->regs_res == NULL) {
3857 		device_printf(sc->dev, "cannot map registers.\n");
3858 		return (ENXIO);
3859 	}
3860 	sc->bt = rman_get_bustag(sc->regs_res);
3861 	sc->bh = rman_get_bushandle(sc->regs_res);
3862 	sc->mmio_len = rman_get_size(sc->regs_res);
3863 	setbit(&sc->doorbells, DOORBELL_KDB);
3864 
3865 	sc->msix_rid = PCIR_BAR(4);
3866 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3867 	    &sc->msix_rid, RF_ACTIVE);
3868 	if (sc->msix_res == NULL) {
3869 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3870 		return (ENXIO);
3871 	}
3872 
3873 	return (0);
3874 }
3875 
3876 int
t4_map_bar_2(struct adapter * sc)3877 t4_map_bar_2(struct adapter *sc)
3878 {
3879 
3880 	/*
3881 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
3882 	 * to map it if RDMA is disabled.
3883 	 */
3884 	if (is_t4(sc) && sc->rdmacaps == 0)
3885 		return (0);
3886 
3887 	sc->udbs_rid = PCIR_BAR(2);
3888 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3889 	    &sc->udbs_rid, RF_ACTIVE);
3890 	if (sc->udbs_res == NULL) {
3891 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
3892 		return (ENXIO);
3893 	}
3894 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
3895 
3896 	if (chip_id(sc) >= CHELSIO_T5) {
3897 		setbit(&sc->doorbells, DOORBELL_UDB);
3898 #if defined(__i386__) || defined(__amd64__)
3899 		if (t5_write_combine) {
3900 			int rc, mode;
3901 
3902 			/*
3903 			 * Enable write combining on BAR2.  This is the
3904 			 * userspace doorbell BAR and is split into 128B
3905 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
3906 			 * with an egress queue.  The first 64B has the doorbell
3907 			 * and the second 64B can be used to submit a tx work
3908 			 * request with an implicit doorbell.
3909 			 */
3910 
3911 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
3912 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
3913 			if (rc == 0) {
3914 				clrbit(&sc->doorbells, DOORBELL_UDB);
3915 				setbit(&sc->doorbells, DOORBELL_WCWR);
3916 				setbit(&sc->doorbells, DOORBELL_UDBWC);
3917 			} else {
3918 				device_printf(sc->dev,
3919 				    "couldn't enable write combining: %d\n",
3920 				    rc);
3921 			}
3922 
3923 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
3924 			t4_write_reg(sc, A_SGE_STAT_CFG,
3925 			    V_STATSOURCE_T5(7) | mode);
3926 		}
3927 #endif
3928 	}
3929 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
3930 
3931 	return (0);
3932 }
3933 
3934 int
t4_adj_doorbells(struct adapter * sc)3935 t4_adj_doorbells(struct adapter *sc)
3936 {
3937 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
3938 		sc->doorbells &= t4_doorbells_allowed;
3939 		return (0);
3940 	}
3941 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
3942 	       sc->doorbells, t4_doorbells_allowed);
3943 	return (EINVAL);
3944 }
3945 
3946 struct memwin_init {
3947 	uint32_t base;
3948 	uint32_t aperture;
3949 };
3950 
3951 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
3952 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3953 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3954 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
3955 };
3956 
3957 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
3958 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3959 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3960 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
3961 };
3962 
3963 static void
setup_memwin(struct adapter * sc)3964 setup_memwin(struct adapter *sc)
3965 {
3966 	const struct memwin_init *mw_init;
3967 	struct memwin *mw;
3968 	int i;
3969 	uint32_t bar0;
3970 
3971 	if (is_t4(sc)) {
3972 		/*
3973 		 * Read low 32b of bar0 indirectly via the hardware backdoor
3974 		 * mechanism.  Works from within PCI passthrough environments
3975 		 * too, where rman_get_start() can return a different value.  We
3976 		 * need to program the T4 memory window decoders with the actual
3977 		 * addresses that will be coming across the PCIe link.
3978 		 */
3979 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
3980 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
3981 
3982 		mw_init = &t4_memwin[0];
3983 	} else {
3984 		/* T5+ use the relative offset inside the PCIe BAR */
3985 		bar0 = 0;
3986 
3987 		mw_init = &t5_memwin[0];
3988 	}
3989 
3990 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
3991 		if (!rw_initialized(&mw->mw_lock)) {
3992 			rw_init(&mw->mw_lock, "memory window access");
3993 			mw->mw_base = mw_init->base;
3994 			mw->mw_aperture = mw_init->aperture;
3995 			mw->mw_curpos = 0;
3996 		}
3997 		t4_write_reg(sc,
3998 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
3999 		    (mw->mw_base + bar0) | V_BIR(0) |
4000 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
4001 		rw_wlock(&mw->mw_lock);
4002 		position_memwin(sc, i, mw->mw_curpos);
4003 		rw_wunlock(&mw->mw_lock);
4004 	}
4005 
4006 	/* flush */
4007 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
4008 }
4009 
4010 /*
4011  * Positions the memory window at the given address in the card's address space.
4012  * There are some alignment requirements and the actual position may be at an
4013  * address prior to the requested address.  mw->mw_curpos always has the actual
4014  * position of the window.
4015  */
4016 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)4017 position_memwin(struct adapter *sc, int idx, uint32_t addr)
4018 {
4019 	struct memwin *mw;
4020 	uint32_t pf;
4021 	uint32_t reg;
4022 
4023 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
4024 	mw = &sc->memwin[idx];
4025 	rw_assert(&mw->mw_lock, RA_WLOCKED);
4026 
4027 	if (is_t4(sc)) {
4028 		pf = 0;
4029 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
4030 	} else {
4031 		pf = V_PFNUM(sc->pf);
4032 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
4033 	}
4034 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
4035 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
4036 	t4_read_reg(sc, reg);	/* flush */
4037 }
4038 
4039 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)4040 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
4041     int len, int rw)
4042 {
4043 	struct memwin *mw;
4044 	uint32_t mw_end, v;
4045 
4046 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
4047 
4048 	/* Memory can only be accessed in naturally aligned 4 byte units */
4049 	if (addr & 3 || len & 3 || len <= 0)
4050 		return (EINVAL);
4051 
4052 	mw = &sc->memwin[idx];
4053 	while (len > 0) {
4054 		rw_rlock(&mw->mw_lock);
4055 		mw_end = mw->mw_curpos + mw->mw_aperture;
4056 		if (addr >= mw_end || addr < mw->mw_curpos) {
4057 			/* Will need to reposition the window */
4058 			if (!rw_try_upgrade(&mw->mw_lock)) {
4059 				rw_runlock(&mw->mw_lock);
4060 				rw_wlock(&mw->mw_lock);
4061 			}
4062 			rw_assert(&mw->mw_lock, RA_WLOCKED);
4063 			position_memwin(sc, idx, addr);
4064 			rw_downgrade(&mw->mw_lock);
4065 			mw_end = mw->mw_curpos + mw->mw_aperture;
4066 		}
4067 		rw_assert(&mw->mw_lock, RA_RLOCKED);
4068 		while (addr < mw_end && len > 0) {
4069 			if (rw == 0) {
4070 				v = t4_read_reg(sc, mw->mw_base + addr -
4071 				    mw->mw_curpos);
4072 				*val++ = le32toh(v);
4073 			} else {
4074 				v = *val++;
4075 				t4_write_reg(sc, mw->mw_base + addr -
4076 				    mw->mw_curpos, htole32(v));
4077 			}
4078 			addr += 4;
4079 			len -= 4;
4080 		}
4081 		rw_runlock(&mw->mw_lock);
4082 	}
4083 
4084 	return (0);
4085 }
4086 
4087 CTASSERT(M_TID_COOKIE == M_COOKIE);
4088 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
4089 
4090 static void
t4_init_atid_table(struct adapter * sc)4091 t4_init_atid_table(struct adapter *sc)
4092 {
4093 	struct tid_info *t;
4094 	int i;
4095 
4096 	t = &sc->tids;
4097 	if (t->natids == 0)
4098 		return;
4099 
4100 	MPASS(t->atid_tab == NULL);
4101 
4102 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
4103 	    M_ZERO | M_WAITOK);
4104 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
4105 	t->afree = t->atid_tab;
4106 	t->atids_in_use = 0;
4107 	t->atid_alloc_stopped = false;
4108 	for (i = 1; i < t->natids; i++)
4109 		t->atid_tab[i - 1].next = &t->atid_tab[i];
4110 	t->atid_tab[t->natids - 1].next = NULL;
4111 }
4112 
4113 static void
t4_free_atid_table(struct adapter * sc)4114 t4_free_atid_table(struct adapter *sc)
4115 {
4116 	struct tid_info *t;
4117 
4118 	t = &sc->tids;
4119 
4120 	KASSERT(t->atids_in_use == 0,
4121 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4122 
4123 	if (mtx_initialized(&t->atid_lock))
4124 		mtx_destroy(&t->atid_lock);
4125 	free(t->atid_tab, M_CXGBE);
4126 	t->atid_tab = NULL;
4127 }
4128 
4129 static void
stop_atid_allocator(struct adapter * sc)4130 stop_atid_allocator(struct adapter *sc)
4131 {
4132 	struct tid_info *t = &sc->tids;
4133 
4134 	if (t->natids == 0)
4135 		return;
4136 	mtx_lock(&t->atid_lock);
4137 	t->atid_alloc_stopped = true;
4138 	mtx_unlock(&t->atid_lock);
4139 }
4140 
4141 static void
restart_atid_allocator(struct adapter * sc)4142 restart_atid_allocator(struct adapter *sc)
4143 {
4144 	struct tid_info *t = &sc->tids;
4145 
4146 	if (t->natids == 0)
4147 		return;
4148 	mtx_lock(&t->atid_lock);
4149 	KASSERT(t->atids_in_use == 0,
4150 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4151 	t->atid_alloc_stopped = false;
4152 	mtx_unlock(&t->atid_lock);
4153 }
4154 
4155 int
alloc_atid(struct adapter * sc,void * ctx)4156 alloc_atid(struct adapter *sc, void *ctx)
4157 {
4158 	struct tid_info *t = &sc->tids;
4159 	int atid = -1;
4160 
4161 	mtx_lock(&t->atid_lock);
4162 	if (t->afree && !t->atid_alloc_stopped) {
4163 		union aopen_entry *p = t->afree;
4164 
4165 		atid = p - t->atid_tab;
4166 		MPASS(atid <= M_TID_TID);
4167 		t->afree = p->next;
4168 		p->data = ctx;
4169 		t->atids_in_use++;
4170 	}
4171 	mtx_unlock(&t->atid_lock);
4172 	return (atid);
4173 }
4174 
4175 void *
lookup_atid(struct adapter * sc,int atid)4176 lookup_atid(struct adapter *sc, int atid)
4177 {
4178 	struct tid_info *t = &sc->tids;
4179 
4180 	return (t->atid_tab[atid].data);
4181 }
4182 
4183 void
free_atid(struct adapter * sc,int atid)4184 free_atid(struct adapter *sc, int atid)
4185 {
4186 	struct tid_info *t = &sc->tids;
4187 	union aopen_entry *p = &t->atid_tab[atid];
4188 
4189 	mtx_lock(&t->atid_lock);
4190 	p->next = t->afree;
4191 	t->afree = p;
4192 	t->atids_in_use--;
4193 	mtx_unlock(&t->atid_lock);
4194 }
4195 
4196 static void
queue_tid_release(struct adapter * sc,int tid)4197 queue_tid_release(struct adapter *sc, int tid)
4198 {
4199 
4200 	CXGBE_UNIMPLEMENTED("deferred tid release");
4201 }
4202 
4203 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4204 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4205 {
4206 	struct wrqe *wr;
4207 	struct cpl_tid_release *req;
4208 
4209 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4210 	if (wr == NULL) {
4211 		queue_tid_release(sc, tid);	/* defer */
4212 		return;
4213 	}
4214 	req = wrtod(wr);
4215 
4216 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4217 
4218 	t4_wrq_tx(sc, wr);
4219 }
4220 
4221 static int
t4_range_cmp(const void * a,const void * b)4222 t4_range_cmp(const void *a, const void *b)
4223 {
4224 	return ((const struct t4_range *)a)->start -
4225 	       ((const struct t4_range *)b)->start;
4226 }
4227 
4228 /*
4229  * Verify that the memory range specified by the addr/len pair is valid within
4230  * the card's address space.
4231  */
4232 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4233 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4234 {
4235 	struct t4_range mem_ranges[4], *r, *next;
4236 	uint32_t em, addr_len;
4237 	int i, n, remaining;
4238 
4239 	/* Memory can only be accessed in naturally aligned 4 byte units */
4240 	if (addr & 3 || len & 3 || len == 0)
4241 		return (EINVAL);
4242 
4243 	/* Enabled memories */
4244 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4245 
4246 	r = &mem_ranges[0];
4247 	n = 0;
4248 	bzero(r, sizeof(mem_ranges));
4249 	if (em & F_EDRAM0_ENABLE) {
4250 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4251 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4252 		if (r->size > 0) {
4253 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4254 			if (addr >= r->start &&
4255 			    addr + len <= r->start + r->size)
4256 				return (0);
4257 			r++;
4258 			n++;
4259 		}
4260 	}
4261 	if (em & F_EDRAM1_ENABLE) {
4262 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4263 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4264 		if (r->size > 0) {
4265 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4266 			if (addr >= r->start &&
4267 			    addr + len <= r->start + r->size)
4268 				return (0);
4269 			r++;
4270 			n++;
4271 		}
4272 	}
4273 	if (em & F_EXT_MEM_ENABLE) {
4274 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4275 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4276 		if (r->size > 0) {
4277 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4278 			if (addr >= r->start &&
4279 			    addr + len <= r->start + r->size)
4280 				return (0);
4281 			r++;
4282 			n++;
4283 		}
4284 	}
4285 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4286 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4287 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4288 		if (r->size > 0) {
4289 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4290 			if (addr >= r->start &&
4291 			    addr + len <= r->start + r->size)
4292 				return (0);
4293 			r++;
4294 			n++;
4295 		}
4296 	}
4297 	MPASS(n <= nitems(mem_ranges));
4298 
4299 	if (n > 1) {
4300 		/* Sort and merge the ranges. */
4301 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4302 
4303 		/* Start from index 0 and examine the next n - 1 entries. */
4304 		r = &mem_ranges[0];
4305 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4306 
4307 			MPASS(r->size > 0);	/* r is a valid entry. */
4308 			next = r + 1;
4309 			MPASS(next->size > 0);	/* and so is the next one. */
4310 
4311 			while (r->start + r->size >= next->start) {
4312 				/* Merge the next one into the current entry. */
4313 				r->size = max(r->start + r->size,
4314 				    next->start + next->size) - r->start;
4315 				n--;	/* One fewer entry in total. */
4316 				if (--remaining == 0)
4317 					goto done;	/* short circuit */
4318 				next++;
4319 			}
4320 			if (next != r + 1) {
4321 				/*
4322 				 * Some entries were merged into r and next
4323 				 * points to the first valid entry that couldn't
4324 				 * be merged.
4325 				 */
4326 				MPASS(next->size > 0);	/* must be valid */
4327 				memcpy(r + 1, next, remaining * sizeof(*r));
4328 #ifdef INVARIANTS
4329 				/*
4330 				 * This so that the foo->size assertion in the
4331 				 * next iteration of the loop do the right
4332 				 * thing for entries that were pulled up and are
4333 				 * no longer valid.
4334 				 */
4335 				MPASS(n < nitems(mem_ranges));
4336 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4337 				    sizeof(struct t4_range));
4338 #endif
4339 			}
4340 		}
4341 done:
4342 		/* Done merging the ranges. */
4343 		MPASS(n > 0);
4344 		r = &mem_ranges[0];
4345 		for (i = 0; i < n; i++, r++) {
4346 			if (addr >= r->start &&
4347 			    addr + len <= r->start + r->size)
4348 				return (0);
4349 		}
4350 	}
4351 
4352 	return (EFAULT);
4353 }
4354 
4355 static int
fwmtype_to_hwmtype(int mtype)4356 fwmtype_to_hwmtype(int mtype)
4357 {
4358 
4359 	switch (mtype) {
4360 	case FW_MEMTYPE_EDC0:
4361 		return (MEM_EDC0);
4362 	case FW_MEMTYPE_EDC1:
4363 		return (MEM_EDC1);
4364 	case FW_MEMTYPE_EXTMEM:
4365 		return (MEM_MC0);
4366 	case FW_MEMTYPE_EXTMEM1:
4367 		return (MEM_MC1);
4368 	default:
4369 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4370 	}
4371 }
4372 
4373 /*
4374  * Verify that the memory range specified by the memtype/offset/len pair is
4375  * valid and lies entirely within the memtype specified.  The global address of
4376  * the start of the range is returned in addr.
4377  */
4378 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4379 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4380     uint32_t *addr)
4381 {
4382 	uint32_t em, addr_len, maddr;
4383 
4384 	/* Memory can only be accessed in naturally aligned 4 byte units */
4385 	if (off & 3 || len & 3 || len == 0)
4386 		return (EINVAL);
4387 
4388 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4389 	switch (fwmtype_to_hwmtype(mtype)) {
4390 	case MEM_EDC0:
4391 		if (!(em & F_EDRAM0_ENABLE))
4392 			return (EINVAL);
4393 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4394 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4395 		break;
4396 	case MEM_EDC1:
4397 		if (!(em & F_EDRAM1_ENABLE))
4398 			return (EINVAL);
4399 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4400 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4401 		break;
4402 	case MEM_MC:
4403 		if (!(em & F_EXT_MEM_ENABLE))
4404 			return (EINVAL);
4405 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4406 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4407 		break;
4408 	case MEM_MC1:
4409 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4410 			return (EINVAL);
4411 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4412 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4413 		break;
4414 	default:
4415 		return (EINVAL);
4416 	}
4417 
4418 	*addr = maddr + off;	/* global address */
4419 	return (validate_mem_range(sc, *addr, len));
4420 }
4421 
4422 static int
fixup_devlog_params(struct adapter * sc)4423 fixup_devlog_params(struct adapter *sc)
4424 {
4425 	struct devlog_params *dparams = &sc->params.devlog;
4426 	int rc;
4427 
4428 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4429 	    dparams->size, &dparams->addr);
4430 
4431 	return (rc);
4432 }
4433 
4434 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4435 update_nirq(struct intrs_and_queues *iaq, int nports)
4436 {
4437 
4438 	iaq->nirq = T4_EXTRA_INTR;
4439 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4440 	iaq->nirq += nports * iaq->nofldrxq;
4441 	iaq->nirq += nports * (iaq->num_vis - 1) *
4442 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4443 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4444 }
4445 
4446 /*
4447  * Adjust requirements to fit the number of interrupts available.
4448  */
4449 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4450 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4451     int navail)
4452 {
4453 	int old_nirq;
4454 	const int nports = sc->params.nports;
4455 
4456 	MPASS(nports > 0);
4457 	MPASS(navail > 0);
4458 
4459 	bzero(iaq, sizeof(*iaq));
4460 	iaq->intr_type = itype;
4461 	iaq->num_vis = t4_num_vis;
4462 	iaq->ntxq = t4_ntxq;
4463 	iaq->ntxq_vi = t4_ntxq_vi;
4464 	iaq->nrxq = t4_nrxq;
4465 	iaq->nrxq_vi = t4_nrxq_vi;
4466 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4467 	if (is_offload(sc) || is_ethoffload(sc)) {
4468 		iaq->nofldtxq = t4_nofldtxq;
4469 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
4470 	}
4471 #endif
4472 #ifdef TCP_OFFLOAD
4473 	if (is_offload(sc)) {
4474 		iaq->nofldrxq = t4_nofldrxq;
4475 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4476 	}
4477 #endif
4478 #ifdef DEV_NETMAP
4479 	if (t4_native_netmap & NN_MAIN_VI) {
4480 		iaq->nnmtxq = t4_nnmtxq;
4481 		iaq->nnmrxq = t4_nnmrxq;
4482 	}
4483 	if (t4_native_netmap & NN_EXTRA_VI) {
4484 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4485 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4486 	}
4487 #endif
4488 
4489 	update_nirq(iaq, nports);
4490 	if (iaq->nirq <= navail &&
4491 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4492 		/*
4493 		 * This is the normal case -- there are enough interrupts for
4494 		 * everything.
4495 		 */
4496 		goto done;
4497 	}
4498 
4499 	/*
4500 	 * If extra VIs have been configured try reducing their count and see if
4501 	 * that works.
4502 	 */
4503 	while (iaq->num_vis > 1) {
4504 		iaq->num_vis--;
4505 		update_nirq(iaq, nports);
4506 		if (iaq->nirq <= navail &&
4507 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4508 			device_printf(sc->dev, "virtual interfaces per port "
4509 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4510 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4511 			    "itype %d, navail %u, nirq %d.\n",
4512 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4513 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4514 			    itype, navail, iaq->nirq);
4515 			goto done;
4516 		}
4517 	}
4518 
4519 	/*
4520 	 * Extra VIs will not be created.  Log a message if they were requested.
4521 	 */
4522 	MPASS(iaq->num_vis == 1);
4523 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4524 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4525 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4526 	if (iaq->num_vis != t4_num_vis) {
4527 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4528 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4529 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4530 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4531 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4532 	}
4533 
4534 	/*
4535 	 * Keep reducing the number of NIC rx queues to the next lower power of
4536 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4537 	 * if that works.
4538 	 */
4539 	do {
4540 		if (iaq->nrxq > 1) {
4541 			do {
4542 				iaq->nrxq--;
4543 			} while (!powerof2(iaq->nrxq));
4544 			if (iaq->nnmrxq > iaq->nrxq)
4545 				iaq->nnmrxq = iaq->nrxq;
4546 		}
4547 		if (iaq->nofldrxq > 1)
4548 			iaq->nofldrxq >>= 1;
4549 
4550 		old_nirq = iaq->nirq;
4551 		update_nirq(iaq, nports);
4552 		if (iaq->nirq <= navail &&
4553 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4554 			device_printf(sc->dev, "running with reduced number of "
4555 			    "rx queues because of shortage of interrupts.  "
4556 			    "nrxq=%u, nofldrxq=%u.  "
4557 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4558 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4559 			goto done;
4560 		}
4561 	} while (old_nirq != iaq->nirq);
4562 
4563 	/* One interrupt for everything.  Ugh. */
4564 	device_printf(sc->dev, "running with minimal number of queues.  "
4565 	    "itype %d, navail %u.\n", itype, navail);
4566 	iaq->nirq = 1;
4567 	iaq->nrxq = 1;
4568 	iaq->ntxq = 1;
4569 	if (iaq->nofldrxq > 0) {
4570 		iaq->nofldrxq = 1;
4571 		iaq->nofldtxq = 1;
4572 	}
4573 	iaq->nnmtxq = 0;
4574 	iaq->nnmrxq = 0;
4575 done:
4576 	MPASS(iaq->num_vis > 0);
4577 	if (iaq->num_vis > 1) {
4578 		MPASS(iaq->nrxq_vi > 0);
4579 		MPASS(iaq->ntxq_vi > 0);
4580 	}
4581 	MPASS(iaq->nirq > 0);
4582 	MPASS(iaq->nrxq > 0);
4583 	MPASS(iaq->ntxq > 0);
4584 	if (itype == INTR_MSI) {
4585 		MPASS(powerof2(iaq->nirq));
4586 	}
4587 }
4588 
4589 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4590 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4591 {
4592 	int rc, itype, navail, nalloc;
4593 
4594 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4595 
4596 		if ((itype & t4_intr_types) == 0)
4597 			continue;	/* not allowed */
4598 
4599 		if (itype == INTR_MSIX)
4600 			navail = pci_msix_count(sc->dev);
4601 		else if (itype == INTR_MSI)
4602 			navail = pci_msi_count(sc->dev);
4603 		else
4604 			navail = 1;
4605 restart:
4606 		if (navail == 0)
4607 			continue;
4608 
4609 		calculate_iaq(sc, iaq, itype, navail);
4610 		nalloc = iaq->nirq;
4611 		rc = 0;
4612 		if (itype == INTR_MSIX)
4613 			rc = pci_alloc_msix(sc->dev, &nalloc);
4614 		else if (itype == INTR_MSI)
4615 			rc = pci_alloc_msi(sc->dev, &nalloc);
4616 
4617 		if (rc == 0 && nalloc > 0) {
4618 			if (nalloc == iaq->nirq)
4619 				return (0);
4620 
4621 			/*
4622 			 * Didn't get the number requested.  Use whatever number
4623 			 * the kernel is willing to allocate.
4624 			 */
4625 			device_printf(sc->dev, "fewer vectors than requested, "
4626 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4627 			    itype, iaq->nirq, nalloc);
4628 			pci_release_msi(sc->dev);
4629 			navail = nalloc;
4630 			goto restart;
4631 		}
4632 
4633 		device_printf(sc->dev,
4634 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4635 		    itype, rc, iaq->nirq, nalloc);
4636 	}
4637 
4638 	device_printf(sc->dev,
4639 	    "failed to find a usable interrupt type.  "
4640 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4641 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4642 
4643 	return (ENXIO);
4644 }
4645 
4646 #define FW_VERSION(chip) ( \
4647     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4648     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4649     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4650     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4651 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4652 
4653 /* Just enough of fw_hdr to cover all version info. */
4654 struct fw_h {
4655 	__u8	ver;
4656 	__u8	chip;
4657 	__be16	len512;
4658 	__be32	fw_ver;
4659 	__be32	tp_microcode_ver;
4660 	__u8	intfver_nic;
4661 	__u8	intfver_vnic;
4662 	__u8	intfver_ofld;
4663 	__u8	intfver_ri;
4664 	__u8	intfver_iscsipdu;
4665 	__u8	intfver_iscsi;
4666 	__u8	intfver_fcoepdu;
4667 	__u8	intfver_fcoe;
4668 };
4669 /* Spot check a couple of fields. */
4670 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4671 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4672 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4673 
4674 struct fw_info {
4675 	uint8_t chip;
4676 	char *kld_name;
4677 	char *fw_mod_name;
4678 	struct fw_h fw_h;
4679 } fw_info[] = {
4680 	{
4681 		.chip = CHELSIO_T4,
4682 		.kld_name = "t4fw_cfg",
4683 		.fw_mod_name = "t4fw",
4684 		.fw_h = {
4685 			.chip = FW_HDR_CHIP_T4,
4686 			.fw_ver = htobe32(FW_VERSION(T4)),
4687 			.intfver_nic = FW_INTFVER(T4, NIC),
4688 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4689 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4690 			.intfver_ri = FW_INTFVER(T4, RI),
4691 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4692 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4693 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4694 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4695 		},
4696 	}, {
4697 		.chip = CHELSIO_T5,
4698 		.kld_name = "t5fw_cfg",
4699 		.fw_mod_name = "t5fw",
4700 		.fw_h = {
4701 			.chip = FW_HDR_CHIP_T5,
4702 			.fw_ver = htobe32(FW_VERSION(T5)),
4703 			.intfver_nic = FW_INTFVER(T5, NIC),
4704 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4705 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4706 			.intfver_ri = FW_INTFVER(T5, RI),
4707 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4708 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4709 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4710 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4711 		},
4712 	}, {
4713 		.chip = CHELSIO_T6,
4714 		.kld_name = "t6fw_cfg",
4715 		.fw_mod_name = "t6fw",
4716 		.fw_h = {
4717 			.chip = FW_HDR_CHIP_T6,
4718 			.fw_ver = htobe32(FW_VERSION(T6)),
4719 			.intfver_nic = FW_INTFVER(T6, NIC),
4720 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4721 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4722 			.intfver_ri = FW_INTFVER(T6, RI),
4723 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4724 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4725 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4726 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4727 		},
4728 	}
4729 };
4730 
4731 static struct fw_info *
find_fw_info(int chip)4732 find_fw_info(int chip)
4733 {
4734 	int i;
4735 
4736 	for (i = 0; i < nitems(fw_info); i++) {
4737 		if (fw_info[i].chip == chip)
4738 			return (&fw_info[i]);
4739 	}
4740 	return (NULL);
4741 }
4742 
4743 /*
4744  * Is the given firmware API compatible with the one the driver was compiled
4745  * with?
4746  */
4747 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4748 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4749 {
4750 
4751 	/* short circuit if it's the exact same firmware version */
4752 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4753 		return (1);
4754 
4755 	/*
4756 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4757 	 * features that are supported in the driver.
4758 	 */
4759 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4760 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4761 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4762 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4763 		return (1);
4764 #undef SAME_INTF
4765 
4766 	return (0);
4767 }
4768 
4769 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4770 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4771     const struct firmware **fw)
4772 {
4773 	struct fw_info *fw_info;
4774 
4775 	*dcfg = NULL;
4776 	if (fw != NULL)
4777 		*fw = NULL;
4778 
4779 	fw_info = find_fw_info(chip_id(sc));
4780 	if (fw_info == NULL) {
4781 		device_printf(sc->dev,
4782 		    "unable to look up firmware information for chip %d.\n",
4783 		    chip_id(sc));
4784 		return (EINVAL);
4785 	}
4786 
4787 	*dcfg = firmware_get(fw_info->kld_name);
4788 	if (*dcfg != NULL) {
4789 		if (fw != NULL)
4790 			*fw = firmware_get(fw_info->fw_mod_name);
4791 		return (0);
4792 	}
4793 
4794 	return (ENOENT);
4795 }
4796 
4797 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4798 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4799     const struct firmware *fw)
4800 {
4801 
4802 	if (fw != NULL)
4803 		firmware_put(fw, FIRMWARE_UNLOAD);
4804 	if (dcfg != NULL)
4805 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4806 }
4807 
4808 /*
4809  * Return values:
4810  * 0 means no firmware install attempted.
4811  * ERESTART means a firmware install was attempted and was successful.
4812  * +ve errno means a firmware install was attempted but failed.
4813  */
4814 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4815 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4816     const struct fw_h *drv_fw, const char *reason, int *already)
4817 {
4818 	const struct firmware *cfg, *fw;
4819 	const uint32_t c = be32toh(card_fw->fw_ver);
4820 	uint32_t d, k;
4821 	int rc, fw_install;
4822 	struct fw_h bundled_fw;
4823 	bool load_attempted;
4824 
4825 	cfg = fw = NULL;
4826 	load_attempted = false;
4827 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4828 
4829 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4830 	if (t4_fw_install < 0) {
4831 		rc = load_fw_module(sc, &cfg, &fw);
4832 		if (rc != 0 || fw == NULL) {
4833 			device_printf(sc->dev,
4834 			    "failed to load firmware module: %d. cfg %p, fw %p;"
4835 			    " will use compiled-in firmware version for"
4836 			    "hw.cxgbe.fw_install checks.\n",
4837 			    rc, cfg, fw);
4838 		} else {
4839 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
4840 		}
4841 		load_attempted = true;
4842 	}
4843 	d = be32toh(bundled_fw.fw_ver);
4844 
4845 	if (reason != NULL)
4846 		goto install;
4847 
4848 	if ((sc->flags & FW_OK) == 0) {
4849 
4850 		if (c == 0xffffffff) {
4851 			reason = "missing";
4852 			goto install;
4853 		}
4854 
4855 		rc = 0;
4856 		goto done;
4857 	}
4858 
4859 	if (!fw_compatible(card_fw, &bundled_fw)) {
4860 		reason = "incompatible or unusable";
4861 		goto install;
4862 	}
4863 
4864 	if (d > c) {
4865 		reason = "older than the version bundled with this driver";
4866 		goto install;
4867 	}
4868 
4869 	if (fw_install == 2 && d != c) {
4870 		reason = "different than the version bundled with this driver";
4871 		goto install;
4872 	}
4873 
4874 	/* No reason to do anything to the firmware already on the card. */
4875 	rc = 0;
4876 	goto done;
4877 
4878 install:
4879 	rc = 0;
4880 	if ((*already)++)
4881 		goto done;
4882 
4883 	if (fw_install == 0) {
4884 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4885 		    "but the driver is prohibited from installing a firmware "
4886 		    "on the card.\n",
4887 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4888 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4889 
4890 		goto done;
4891 	}
4892 
4893 	/*
4894 	 * We'll attempt to install a firmware.  Load the module first (if it
4895 	 * hasn't been loaded already).
4896 	 */
4897 	if (!load_attempted) {
4898 		rc = load_fw_module(sc, &cfg, &fw);
4899 		if (rc != 0 || fw == NULL) {
4900 			device_printf(sc->dev,
4901 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
4902 			    rc, cfg, fw);
4903 			/* carry on */
4904 		}
4905 	}
4906 	if (fw == NULL) {
4907 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4908 		    "but the driver cannot take corrective action because it "
4909 		    "is unable to load the firmware module.\n",
4910 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4911 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4912 		rc = sc->flags & FW_OK ? 0 : ENOENT;
4913 		goto done;
4914 	}
4915 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
4916 	if (k != d) {
4917 		MPASS(t4_fw_install > 0);
4918 		device_printf(sc->dev,
4919 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
4920 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
4921 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
4922 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
4923 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4924 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4925 		rc = sc->flags & FW_OK ? 0 : EINVAL;
4926 		goto done;
4927 	}
4928 
4929 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4930 	    "installing firmware %u.%u.%u.%u on card.\n",
4931 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4932 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
4933 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4934 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4935 
4936 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
4937 	if (rc != 0) {
4938 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
4939 	} else {
4940 		/* Installed successfully, update the cached header too. */
4941 		rc = ERESTART;
4942 		memcpy(card_fw, fw->data, sizeof(*card_fw));
4943 	}
4944 done:
4945 	unload_fw_module(sc, cfg, fw);
4946 
4947 	return (rc);
4948 }
4949 
4950 /*
4951  * Establish contact with the firmware and attempt to become the master driver.
4952  *
4953  * A firmware will be installed to the card if needed (if the driver is allowed
4954  * to do so).
4955  */
4956 static int
contact_firmware(struct adapter * sc)4957 contact_firmware(struct adapter *sc)
4958 {
4959 	int rc, already = 0;
4960 	enum dev_state state;
4961 	struct fw_info *fw_info;
4962 	struct fw_hdr *card_fw;		/* fw on the card */
4963 	const struct fw_h *drv_fw;
4964 
4965 	fw_info = find_fw_info(chip_id(sc));
4966 	if (fw_info == NULL) {
4967 		device_printf(sc->dev,
4968 		    "unable to look up firmware information for chip %d.\n",
4969 		    chip_id(sc));
4970 		return (EINVAL);
4971 	}
4972 	drv_fw = &fw_info->fw_h;
4973 
4974 	/* Read the header of the firmware on the card */
4975 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
4976 restart:
4977 	rc = -t4_get_fw_hdr(sc, card_fw);
4978 	if (rc != 0) {
4979 		device_printf(sc->dev,
4980 		    "unable to read firmware header from card's flash: %d\n",
4981 		    rc);
4982 		goto done;
4983 	}
4984 
4985 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
4986 	    &already);
4987 	if (rc == ERESTART)
4988 		goto restart;
4989 	if (rc != 0)
4990 		goto done;
4991 
4992 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
4993 	if (rc < 0 || state == DEV_STATE_ERR) {
4994 		rc = -rc;
4995 		device_printf(sc->dev,
4996 		    "failed to connect to the firmware: %d, %d.  "
4997 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4998 #if 0
4999 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
5000 		    "not responding properly to HELLO", &already) == ERESTART)
5001 			goto restart;
5002 #endif
5003 		goto done;
5004 	}
5005 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
5006 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
5007 
5008 	if (rc == sc->pf) {
5009 		sc->flags |= MASTER_PF;
5010 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
5011 		    NULL, &already);
5012 		if (rc == ERESTART)
5013 			rc = 0;
5014 		else if (rc != 0)
5015 			goto done;
5016 	} else if (state == DEV_STATE_UNINIT) {
5017 		/*
5018 		 * We didn't get to be the master so we definitely won't be
5019 		 * configuring the chip.  It's a bug if someone else hasn't
5020 		 * configured it already.
5021 		 */
5022 		device_printf(sc->dev, "couldn't be master(%d), "
5023 		    "device not already initialized either(%d).  "
5024 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5025 		rc = EPROTO;
5026 		goto done;
5027 	} else {
5028 		/*
5029 		 * Some other PF is the master and has configured the chip.
5030 		 * This is allowed but untested.
5031 		 */
5032 		device_printf(sc->dev, "PF%d is master, device state %d.  "
5033 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5034 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
5035 		sc->cfcsum = 0;
5036 		rc = 0;
5037 	}
5038 done:
5039 	if (rc != 0 && sc->flags & FW_OK) {
5040 		t4_fw_bye(sc, sc->mbox);
5041 		sc->flags &= ~FW_OK;
5042 	}
5043 	free(card_fw, M_CXGBE);
5044 	return (rc);
5045 }
5046 
5047 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff)5048 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
5049     uint32_t mtype, uint32_t moff)
5050 {
5051 	struct fw_info *fw_info;
5052 	const struct firmware *dcfg, *rcfg = NULL;
5053 	const uint32_t *cfdata;
5054 	uint32_t cflen, addr;
5055 	int rc;
5056 
5057 	load_fw_module(sc, &dcfg, NULL);
5058 
5059 	/* Card specific interpretation of "default". */
5060 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5061 		if (pci_get_device(sc->dev) == 0x440a)
5062 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
5063 		if (is_fpga(sc))
5064 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
5065 	}
5066 
5067 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5068 		if (dcfg == NULL) {
5069 			device_printf(sc->dev,
5070 			    "KLD with default config is not available.\n");
5071 			rc = ENOENT;
5072 			goto done;
5073 		}
5074 		cfdata = dcfg->data;
5075 		cflen = dcfg->datasize & ~3;
5076 	} else {
5077 		char s[32];
5078 
5079 		fw_info = find_fw_info(chip_id(sc));
5080 		if (fw_info == NULL) {
5081 			device_printf(sc->dev,
5082 			    "unable to look up firmware information for chip %d.\n",
5083 			    chip_id(sc));
5084 			rc = EINVAL;
5085 			goto done;
5086 		}
5087 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
5088 
5089 		rcfg = firmware_get(s);
5090 		if (rcfg == NULL) {
5091 			device_printf(sc->dev,
5092 			    "unable to load module \"%s\" for configuration "
5093 			    "profile \"%s\".\n", s, cfg_file);
5094 			rc = ENOENT;
5095 			goto done;
5096 		}
5097 		cfdata = rcfg->data;
5098 		cflen = rcfg->datasize & ~3;
5099 	}
5100 
5101 	if (cflen > FLASH_CFG_MAX_SIZE) {
5102 		device_printf(sc->dev,
5103 		    "config file too long (%d, max allowed is %d).\n",
5104 		    cflen, FLASH_CFG_MAX_SIZE);
5105 		rc = EINVAL;
5106 		goto done;
5107 	}
5108 
5109 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
5110 	if (rc != 0) {
5111 		device_printf(sc->dev,
5112 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
5113 		    __func__, mtype, moff, cflen, rc);
5114 		rc = EINVAL;
5115 		goto done;
5116 	}
5117 	write_via_memwin(sc, 2, addr, cfdata, cflen);
5118 done:
5119 	if (rcfg != NULL)
5120 		firmware_put(rcfg, FIRMWARE_UNLOAD);
5121 	unload_fw_module(sc, dcfg, NULL);
5122 	return (rc);
5123 }
5124 
5125 struct caps_allowed {
5126 	uint16_t nbmcaps;
5127 	uint16_t linkcaps;
5128 	uint16_t switchcaps;
5129 	uint16_t niccaps;
5130 	uint16_t toecaps;
5131 	uint16_t rdmacaps;
5132 	uint16_t cryptocaps;
5133 	uint16_t iscsicaps;
5134 	uint16_t fcoecaps;
5135 };
5136 
5137 #define FW_PARAM_DEV(param) \
5138 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5139 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5140 #define FW_PARAM_PFVF(param) \
5141 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5142 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5143 
5144 /*
5145  * Provide a configuration profile to the firmware and have it initialize the
5146  * chip accordingly.  This may involve uploading a configuration file to the
5147  * card.
5148  */
5149 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5150 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5151     const struct caps_allowed *caps_allowed)
5152 {
5153 	int rc;
5154 	struct fw_caps_config_cmd caps;
5155 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5156 
5157 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5158 	if (rc != 0) {
5159 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5160 		return (rc);
5161 	}
5162 
5163 	bzero(&caps, sizeof(caps));
5164 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5165 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5166 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5167 		mtype = 0;
5168 		moff = 0;
5169 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5170 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5171 		mtype = FW_MEMTYPE_FLASH;
5172 		moff = t4_flash_cfg_addr(sc);
5173 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5174 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5175 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5176 		    FW_LEN16(caps));
5177 	} else {
5178 		/*
5179 		 * Ask the firmware where it wants us to upload the config file.
5180 		 */
5181 		param = FW_PARAM_DEV(CF);
5182 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5183 		if (rc != 0) {
5184 			/* No support for config file?  Shouldn't happen. */
5185 			device_printf(sc->dev,
5186 			    "failed to query config file location: %d.\n", rc);
5187 			goto done;
5188 		}
5189 		mtype = G_FW_PARAMS_PARAM_Y(val);
5190 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5191 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5192 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5193 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5194 		    FW_LEN16(caps));
5195 
5196 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
5197 		if (rc != 0) {
5198 			device_printf(sc->dev,
5199 			    "failed to upload config file to card: %d.\n", rc);
5200 			goto done;
5201 		}
5202 	}
5203 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5204 	if (rc != 0) {
5205 		device_printf(sc->dev, "failed to pre-process config file: %d "
5206 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5207 		goto done;
5208 	}
5209 
5210 	finicsum = be32toh(caps.finicsum);
5211 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5212 	if (finicsum != cfcsum) {
5213 		device_printf(sc->dev,
5214 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5215 		    finicsum, cfcsum);
5216 	}
5217 	sc->cfcsum = cfcsum;
5218 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5219 
5220 	/*
5221 	 * Let the firmware know what features will (not) be used so it can tune
5222 	 * things accordingly.
5223 	 */
5224 #define LIMIT_CAPS(x) do { \
5225 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5226 } while (0)
5227 	LIMIT_CAPS(nbm);
5228 	LIMIT_CAPS(link);
5229 	LIMIT_CAPS(switch);
5230 	LIMIT_CAPS(nic);
5231 	LIMIT_CAPS(toe);
5232 	LIMIT_CAPS(rdma);
5233 	LIMIT_CAPS(crypto);
5234 	LIMIT_CAPS(iscsi);
5235 	LIMIT_CAPS(fcoe);
5236 #undef LIMIT_CAPS
5237 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5238 		/*
5239 		 * TOE and hashfilters are mutually exclusive.  It is a config
5240 		 * file or firmware bug if both are reported as available.  Try
5241 		 * to cope with the situation in non-debug builds by disabling
5242 		 * TOE.
5243 		 */
5244 		MPASS(caps.toecaps == 0);
5245 
5246 		caps.toecaps = 0;
5247 		caps.rdmacaps = 0;
5248 		caps.iscsicaps = 0;
5249 	}
5250 
5251 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5252 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5253 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5254 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5255 	if (rc != 0) {
5256 		device_printf(sc->dev,
5257 		    "failed to process config file: %d.\n", rc);
5258 		goto done;
5259 	}
5260 
5261 	t4_tweak_chip_settings(sc);
5262 	set_params__pre_init(sc);
5263 
5264 	/* get basic stuff going */
5265 	rc = -t4_fw_initialize(sc, sc->mbox);
5266 	if (rc != 0) {
5267 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5268 		goto done;
5269 	}
5270 done:
5271 	return (rc);
5272 }
5273 
5274 /*
5275  * Partition chip resources for use between various PFs, VFs, etc.
5276  */
5277 static int
partition_resources(struct adapter * sc)5278 partition_resources(struct adapter *sc)
5279 {
5280 	char cfg_file[sizeof(t4_cfg_file)];
5281 	struct caps_allowed caps_allowed;
5282 	int rc;
5283 	bool fallback;
5284 
5285 	/* Only the master driver gets to configure the chip resources. */
5286 	MPASS(sc->flags & MASTER_PF);
5287 
5288 #define COPY_CAPS(x) do { \
5289 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5290 } while (0)
5291 	bzero(&caps_allowed, sizeof(caps_allowed));
5292 	COPY_CAPS(nbm);
5293 	COPY_CAPS(link);
5294 	COPY_CAPS(switch);
5295 	COPY_CAPS(nic);
5296 	COPY_CAPS(toe);
5297 	COPY_CAPS(rdma);
5298 	COPY_CAPS(crypto);
5299 	COPY_CAPS(iscsi);
5300 	COPY_CAPS(fcoe);
5301 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5302 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5303 retry:
5304 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5305 	if (rc != 0 && fallback) {
5306 		dump_devlog(sc);
5307 		device_printf(sc->dev,
5308 		    "failed (%d) to configure card with \"%s\" profile, "
5309 		    "will fall back to a basic configuration and retry.\n",
5310 		    rc, cfg_file);
5311 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5312 		bzero(&caps_allowed, sizeof(caps_allowed));
5313 		COPY_CAPS(switch);
5314 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5315 		fallback = false;
5316 		goto retry;
5317 	}
5318 #undef COPY_CAPS
5319 	return (rc);
5320 }
5321 
5322 /*
5323  * Retrieve parameters that are needed (or nice to have) very early.
5324  */
5325 static int
get_params__pre_init(struct adapter * sc)5326 get_params__pre_init(struct adapter *sc)
5327 {
5328 	int rc;
5329 	uint32_t param[2], val[2];
5330 
5331 	t4_get_version_info(sc);
5332 
5333 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5334 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5335 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5336 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5337 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5338 
5339 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5340 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5341 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5342 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5343 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5344 
5345 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5346 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5347 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5348 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5349 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5350 
5351 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5352 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5353 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5354 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5355 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5356 
5357 	param[0] = FW_PARAM_DEV(PORTVEC);
5358 	param[1] = FW_PARAM_DEV(CCLK);
5359 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5360 	if (rc != 0) {
5361 		device_printf(sc->dev,
5362 		    "failed to query parameters (pre_init): %d.\n", rc);
5363 		return (rc);
5364 	}
5365 
5366 	sc->params.portvec = val[0];
5367 	sc->params.nports = bitcount32(val[0]);
5368 	sc->params.vpd.cclk = val[1];
5369 
5370 	/* Read device log parameters. */
5371 	rc = -t4_init_devlog_params(sc, 1);
5372 	if (rc == 0)
5373 		fixup_devlog_params(sc);
5374 	else {
5375 		device_printf(sc->dev,
5376 		    "failed to get devlog parameters: %d.\n", rc);
5377 		rc = 0;	/* devlog isn't critical for device operation */
5378 	}
5379 
5380 	return (rc);
5381 }
5382 
5383 /*
5384  * Any params that need to be set before FW_INITIALIZE.
5385  */
5386 static int
set_params__pre_init(struct adapter * sc)5387 set_params__pre_init(struct adapter *sc)
5388 {
5389 	int rc = 0;
5390 	uint32_t param, val;
5391 
5392 	if (chip_id(sc) >= CHELSIO_T6) {
5393 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5394 		val = 1;
5395 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5396 		/* firmwares < 1.20.1.0 do not have this param. */
5397 		if (rc == FW_EINVAL &&
5398 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5399 			rc = 0;
5400 		}
5401 		if (rc != 0) {
5402 			device_printf(sc->dev,
5403 			    "failed to enable high priority filters :%d.\n",
5404 			    rc);
5405 		}
5406 
5407 		param = FW_PARAM_DEV(PPOD_EDRAM);
5408 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5409 		if (rc == 0 && val == 1) {
5410 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5411 			    &val);
5412 			if (rc != 0) {
5413 				device_printf(sc->dev,
5414 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5415 			}
5416 		}
5417 	}
5418 
5419 	/* Enable opaque VIIDs with firmwares that support it. */
5420 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5421 	val = 1;
5422 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5423 	if (rc == 0 && val == 1)
5424 		sc->params.viid_smt_extn_support = true;
5425 	else
5426 		sc->params.viid_smt_extn_support = false;
5427 
5428 	return (rc);
5429 }
5430 
5431 /*
5432  * Retrieve various parameters that are of interest to the driver.  The device
5433  * has been initialized by the firmware at this point.
5434  */
5435 static int
get_params__post_init(struct adapter * sc)5436 get_params__post_init(struct adapter *sc)
5437 {
5438 	int rc;
5439 	uint32_t param[7], val[7];
5440 	struct fw_caps_config_cmd caps;
5441 
5442 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5443 	param[1] = FW_PARAM_PFVF(EQ_START);
5444 	param[2] = FW_PARAM_PFVF(FILTER_START);
5445 	param[3] = FW_PARAM_PFVF(FILTER_END);
5446 	param[4] = FW_PARAM_PFVF(L2T_START);
5447 	param[5] = FW_PARAM_PFVF(L2T_END);
5448 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5449 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5450 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5451 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5452 	if (rc != 0) {
5453 		device_printf(sc->dev,
5454 		    "failed to query parameters (post_init): %d.\n", rc);
5455 		return (rc);
5456 	}
5457 
5458 	sc->sge.iq_start = val[0];
5459 	sc->sge.eq_start = val[1];
5460 	if ((int)val[3] > (int)val[2]) {
5461 		sc->tids.ftid_base = val[2];
5462 		sc->tids.ftid_end = val[3];
5463 		sc->tids.nftids = val[3] - val[2] + 1;
5464 	}
5465 	sc->vres.l2t.start = val[4];
5466 	sc->vres.l2t.size = val[5] - val[4] + 1;
5467 	/* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5468 	if (sc->vres.l2t.size > 0)
5469 		MPASS(fls(val[5]) <= S_SYNC_WR);
5470 	sc->params.core_vdd = val[6];
5471 
5472 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5473 	param[1] = FW_PARAM_PFVF(EQ_END);
5474 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5475 	if (rc != 0) {
5476 		device_printf(sc->dev,
5477 		    "failed to query parameters (post_init2): %d.\n", rc);
5478 		return (rc);
5479 	}
5480 	MPASS((int)val[0] >= sc->sge.iq_start);
5481 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5482 	MPASS((int)val[1] >= sc->sge.eq_start);
5483 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5484 
5485 	if (chip_id(sc) >= CHELSIO_T6) {
5486 
5487 		sc->tids.tid_base = t4_read_reg(sc,
5488 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5489 
5490 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5491 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5492 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5493 		if (rc != 0) {
5494 			device_printf(sc->dev,
5495 			   "failed to query hpfilter parameters: %d.\n", rc);
5496 			return (rc);
5497 		}
5498 		if ((int)val[1] > (int)val[0]) {
5499 			sc->tids.hpftid_base = val[0];
5500 			sc->tids.hpftid_end = val[1];
5501 			sc->tids.nhpftids = val[1] - val[0] + 1;
5502 
5503 			/*
5504 			 * These should go off if the layout changes and the
5505 			 * driver needs to catch up.
5506 			 */
5507 			MPASS(sc->tids.hpftid_base == 0);
5508 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5509 		}
5510 
5511 		param[0] = FW_PARAM_PFVF(RAWF_START);
5512 		param[1] = FW_PARAM_PFVF(RAWF_END);
5513 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5514 		if (rc != 0) {
5515 			device_printf(sc->dev,
5516 			   "failed to query rawf parameters: %d.\n", rc);
5517 			return (rc);
5518 		}
5519 		if ((int)val[1] > (int)val[0]) {
5520 			sc->rawf_base = val[0];
5521 			sc->nrawf = val[1] - val[0] + 1;
5522 		}
5523 	}
5524 
5525 	/*
5526 	 * The parameters that follow may not be available on all firmwares.  We
5527 	 * query them individually rather than in a compound query because old
5528 	 * firmwares fail the entire query if an unknown parameter is queried.
5529 	 */
5530 
5531 	/*
5532 	 * MPS buffer group configuration.
5533 	 */
5534 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5535 	val[0] = 0;
5536 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5537 	if (rc == 0)
5538 		sc->params.mps_bg_map = val[0];
5539 	else
5540 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5541 
5542 	param[0] = FW_PARAM_DEV(TPCHMAP);
5543 	val[0] = 0;
5544 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5545 	if (rc == 0)
5546 		sc->params.tp_ch_map = val[0];
5547 	else
5548 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5549 
5550 	/*
5551 	 * Determine whether the firmware supports the filter2 work request.
5552 	 */
5553 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5554 	val[0] = 0;
5555 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5556 	if (rc == 0)
5557 		sc->params.filter2_wr_support = val[0] != 0;
5558 	else
5559 		sc->params.filter2_wr_support = 0;
5560 
5561 	/*
5562 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5563 	 */
5564 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5565 	val[0] = 0;
5566 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5567 	if (rc == 0)
5568 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5569 	else
5570 		sc->params.ulptx_memwrite_dsgl = false;
5571 
5572 	/* FW_RI_FR_NSMR_TPTE_WR support */
5573 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5574 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5575 	if (rc == 0)
5576 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5577 	else
5578 		sc->params.fr_nsmr_tpte_wr_support = false;
5579 
5580 	/* Support for 512 SGL entries per FR MR. */
5581 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5582 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5583 	if (rc == 0)
5584 		sc->params.dev_512sgl_mr = val[0] != 0;
5585 	else
5586 		sc->params.dev_512sgl_mr = false;
5587 
5588 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5589 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5590 	if (rc == 0)
5591 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5592 	else
5593 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5594 
5595 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5596 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5597 	if (rc == 0) {
5598 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5599 		sc->params.nsched_cls = val[0];
5600 	} else
5601 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5602 
5603 	/* get capabilites */
5604 	bzero(&caps, sizeof(caps));
5605 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5606 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5607 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5608 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5609 	if (rc != 0) {
5610 		device_printf(sc->dev,
5611 		    "failed to get card capabilities: %d.\n", rc);
5612 		return (rc);
5613 	}
5614 
5615 #define READ_CAPS(x) do { \
5616 	sc->x = htobe16(caps.x); \
5617 } while (0)
5618 	READ_CAPS(nbmcaps);
5619 	READ_CAPS(linkcaps);
5620 	READ_CAPS(switchcaps);
5621 	READ_CAPS(niccaps);
5622 	READ_CAPS(toecaps);
5623 	READ_CAPS(rdmacaps);
5624 	READ_CAPS(cryptocaps);
5625 	READ_CAPS(iscsicaps);
5626 	READ_CAPS(fcoecaps);
5627 
5628 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5629 		MPASS(chip_id(sc) > CHELSIO_T4);
5630 		MPASS(sc->toecaps == 0);
5631 		sc->toecaps = 0;
5632 
5633 		param[0] = FW_PARAM_DEV(NTID);
5634 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5635 		if (rc != 0) {
5636 			device_printf(sc->dev,
5637 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5638 			return (rc);
5639 		}
5640 		sc->tids.ntids = val[0];
5641 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5642 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5643 			sc->tids.ntids -= sc->tids.nhpftids;
5644 		}
5645 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5646 		sc->params.hash_filter = 1;
5647 	}
5648 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5649 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5650 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5651 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5652 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5653 		if (rc != 0) {
5654 			device_printf(sc->dev,
5655 			    "failed to query NIC parameters: %d.\n", rc);
5656 			return (rc);
5657 		}
5658 		if ((int)val[1] > (int)val[0]) {
5659 			sc->tids.etid_base = val[0];
5660 			sc->tids.etid_end = val[1];
5661 			sc->tids.netids = val[1] - val[0] + 1;
5662 			sc->params.eo_wr_cred = val[2];
5663 			sc->params.ethoffload = 1;
5664 		}
5665 	}
5666 	if (sc->toecaps) {
5667 		/* query offload-related parameters */
5668 		param[0] = FW_PARAM_DEV(NTID);
5669 		param[1] = FW_PARAM_PFVF(SERVER_START);
5670 		param[2] = FW_PARAM_PFVF(SERVER_END);
5671 		param[3] = FW_PARAM_PFVF(TDDP_START);
5672 		param[4] = FW_PARAM_PFVF(TDDP_END);
5673 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5674 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5675 		if (rc != 0) {
5676 			device_printf(sc->dev,
5677 			    "failed to query TOE parameters: %d.\n", rc);
5678 			return (rc);
5679 		}
5680 		sc->tids.ntids = val[0];
5681 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5682 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5683 			sc->tids.ntids -= sc->tids.nhpftids;
5684 		}
5685 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5686 		if ((int)val[2] > (int)val[1]) {
5687 			sc->tids.stid_base = val[1];
5688 			sc->tids.nstids = val[2] - val[1] + 1;
5689 		}
5690 		sc->vres.ddp.start = val[3];
5691 		sc->vres.ddp.size = val[4] - val[3] + 1;
5692 		sc->params.ofldq_wr_cred = val[5];
5693 		sc->params.offload = 1;
5694 	} else {
5695 		/*
5696 		 * The firmware attempts memfree TOE configuration for -SO cards
5697 		 * and will report toecaps=0 if it runs out of resources (this
5698 		 * depends on the config file).  It may not report 0 for other
5699 		 * capabilities dependent on the TOE in this case.  Set them to
5700 		 * 0 here so that the driver doesn't bother tracking resources
5701 		 * that will never be used.
5702 		 */
5703 		sc->iscsicaps = 0;
5704 		sc->rdmacaps = 0;
5705 	}
5706 	if (sc->rdmacaps) {
5707 		param[0] = FW_PARAM_PFVF(STAG_START);
5708 		param[1] = FW_PARAM_PFVF(STAG_END);
5709 		param[2] = FW_PARAM_PFVF(RQ_START);
5710 		param[3] = FW_PARAM_PFVF(RQ_END);
5711 		param[4] = FW_PARAM_PFVF(PBL_START);
5712 		param[5] = FW_PARAM_PFVF(PBL_END);
5713 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5714 		if (rc != 0) {
5715 			device_printf(sc->dev,
5716 			    "failed to query RDMA parameters(1): %d.\n", rc);
5717 			return (rc);
5718 		}
5719 		sc->vres.stag.start = val[0];
5720 		sc->vres.stag.size = val[1] - val[0] + 1;
5721 		sc->vres.rq.start = val[2];
5722 		sc->vres.rq.size = val[3] - val[2] + 1;
5723 		sc->vres.pbl.start = val[4];
5724 		sc->vres.pbl.size = val[5] - val[4] + 1;
5725 
5726 		param[0] = FW_PARAM_PFVF(SQRQ_START);
5727 		param[1] = FW_PARAM_PFVF(SQRQ_END);
5728 		param[2] = FW_PARAM_PFVF(CQ_START);
5729 		param[3] = FW_PARAM_PFVF(CQ_END);
5730 		param[4] = FW_PARAM_PFVF(OCQ_START);
5731 		param[5] = FW_PARAM_PFVF(OCQ_END);
5732 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5733 		if (rc != 0) {
5734 			device_printf(sc->dev,
5735 			    "failed to query RDMA parameters(2): %d.\n", rc);
5736 			return (rc);
5737 		}
5738 		sc->vres.qp.start = val[0];
5739 		sc->vres.qp.size = val[1] - val[0] + 1;
5740 		sc->vres.cq.start = val[2];
5741 		sc->vres.cq.size = val[3] - val[2] + 1;
5742 		sc->vres.ocq.start = val[4];
5743 		sc->vres.ocq.size = val[5] - val[4] + 1;
5744 
5745 		param[0] = FW_PARAM_PFVF(SRQ_START);
5746 		param[1] = FW_PARAM_PFVF(SRQ_END);
5747 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
5748 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5749 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5750 		if (rc != 0) {
5751 			device_printf(sc->dev,
5752 			    "failed to query RDMA parameters(3): %d.\n", rc);
5753 			return (rc);
5754 		}
5755 		sc->vres.srq.start = val[0];
5756 		sc->vres.srq.size = val[1] - val[0] + 1;
5757 		sc->params.max_ordird_qp = val[2];
5758 		sc->params.max_ird_adapter = val[3];
5759 	}
5760 	if (sc->iscsicaps) {
5761 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5762 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5763 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5764 		if (rc != 0) {
5765 			device_printf(sc->dev,
5766 			    "failed to query iSCSI parameters: %d.\n", rc);
5767 			return (rc);
5768 		}
5769 		sc->vres.iscsi.start = val[0];
5770 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5771 	}
5772 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5773 		param[0] = FW_PARAM_PFVF(TLS_START);
5774 		param[1] = FW_PARAM_PFVF(TLS_END);
5775 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5776 		if (rc != 0) {
5777 			device_printf(sc->dev,
5778 			    "failed to query TLS parameters: %d.\n", rc);
5779 			return (rc);
5780 		}
5781 		sc->vres.key.start = val[0];
5782 		sc->vres.key.size = val[1] - val[0] + 1;
5783 	}
5784 
5785 	/*
5786 	 * We've got the params we wanted to query directly from the firmware.
5787 	 * Grab some others via other means.
5788 	 */
5789 	t4_init_sge_params(sc);
5790 	t4_init_tp_params(sc);
5791 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5792 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5793 
5794 	rc = t4_verify_chip_settings(sc);
5795 	if (rc != 0)
5796 		return (rc);
5797 	t4_init_rx_buf_info(sc);
5798 
5799 	return (rc);
5800 }
5801 
5802 #ifdef KERN_TLS
5803 static void
ktls_tick(void * arg)5804 ktls_tick(void *arg)
5805 {
5806 	struct adapter *sc;
5807 	uint32_t tstamp;
5808 
5809 	sc = arg;
5810 	tstamp = tcp_ts_getticks();
5811 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
5812 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
5813 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
5814 }
5815 
5816 static int
t6_config_kern_tls(struct adapter * sc,bool enable)5817 t6_config_kern_tls(struct adapter *sc, bool enable)
5818 {
5819 	int rc;
5820 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5821 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
5822 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
5823 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
5824 
5825 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
5826 	if (rc != 0) {
5827 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
5828 		    enable ?  "enable" : "disable", rc);
5829 		return (rc);
5830 	}
5831 
5832 	if (enable) {
5833 		sc->flags |= KERN_TLS_ON;
5834 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
5835 		    C_HARDCLOCK);
5836 	} else {
5837 		sc->flags &= ~KERN_TLS_ON;
5838 		callout_stop(&sc->ktls_tick);
5839 	}
5840 
5841 	return (rc);
5842 }
5843 #endif
5844 
5845 static int
set_params__post_init(struct adapter * sc)5846 set_params__post_init(struct adapter *sc)
5847 {
5848 	uint32_t mask, param, val;
5849 #ifdef TCP_OFFLOAD
5850 	int i, v, shift;
5851 #endif
5852 
5853 	/* ask for encapsulated CPLs */
5854 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5855 	val = 1;
5856 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5857 
5858 	/* Enable 32b port caps if the firmware supports it. */
5859 	param = FW_PARAM_PFVF(PORT_CAPS32);
5860 	val = 1;
5861 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
5862 		sc->params.port_caps32 = 1;
5863 
5864 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
5865 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
5866 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
5867 	    V_MASKFILTER(val - 1));
5868 
5869 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
5870 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
5871 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5872 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
5873 	val = 0;
5874 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
5875 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
5876 		    F_ATTACKFILTERENABLE);
5877 		val |= F_DROPERRORATTACK;
5878 	}
5879 	if (t4_drop_ip_fragments != 0) {
5880 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
5881 		    F_FRAGMENTDROP);
5882 		val |= F_DROPERRORFRAG;
5883 	}
5884 	if (t4_drop_pkts_with_l2_errors != 0)
5885 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
5886 	if (t4_drop_pkts_with_l3_errors != 0) {
5887 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
5888 		    F_DROPERRORCSUMIP;
5889 	}
5890 	if (t4_drop_pkts_with_l4_errors != 0) {
5891 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5892 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
5893 	}
5894 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
5895 
5896 #ifdef TCP_OFFLOAD
5897 	/*
5898 	 * Override the TOE timers with user provided tunables.  This is not the
5899 	 * recommended way to change the timers (the firmware config file is) so
5900 	 * these tunables are not documented.
5901 	 *
5902 	 * All the timer tunables are in microseconds.
5903 	 */
5904 	if (t4_toe_keepalive_idle != 0) {
5905 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
5906 		v &= M_KEEPALIVEIDLE;
5907 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
5908 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
5909 	}
5910 	if (t4_toe_keepalive_interval != 0) {
5911 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
5912 		v &= M_KEEPALIVEINTVL;
5913 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
5914 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
5915 	}
5916 	if (t4_toe_keepalive_count != 0) {
5917 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
5918 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5919 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
5920 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
5921 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
5922 	}
5923 	if (t4_toe_rexmt_min != 0) {
5924 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
5925 		v &= M_RXTMIN;
5926 		t4_set_reg_field(sc, A_TP_RXT_MIN,
5927 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
5928 	}
5929 	if (t4_toe_rexmt_max != 0) {
5930 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
5931 		v &= M_RXTMAX;
5932 		t4_set_reg_field(sc, A_TP_RXT_MAX,
5933 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
5934 	}
5935 	if (t4_toe_rexmt_count != 0) {
5936 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
5937 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5938 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
5939 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
5940 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
5941 	}
5942 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
5943 		if (t4_toe_rexmt_backoff[i] != -1) {
5944 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
5945 			shift = (i & 3) << 3;
5946 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
5947 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
5948 		}
5949 	}
5950 #endif
5951 
5952 	/*
5953 	 * Limit TOE connections to 2 reassembly "islands".  This is
5954 	 * required to permit migrating TOE connections to either
5955 	 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
5956 	 */
5957 	t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
5958 	    V_PASSMODE(2));
5959 
5960 #ifdef KERN_TLS
5961 	if (is_ktls(sc)) {
5962 		sc->tlst.inline_keys = t4_tls_inline_keys;
5963 		sc->tlst.combo_wrs = t4_tls_combo_wrs;
5964 		if (t4_kern_tls != 0 && is_t6(sc))
5965 			t6_config_kern_tls(sc, true);
5966 	}
5967 #endif
5968 	return (0);
5969 }
5970 
5971 #undef FW_PARAM_PFVF
5972 #undef FW_PARAM_DEV
5973 
5974 static void
t4_set_desc(struct adapter * sc)5975 t4_set_desc(struct adapter *sc)
5976 {
5977 	struct adapter_params *p = &sc->params;
5978 
5979 	device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
5980 }
5981 
5982 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)5983 ifmedia_add4(struct ifmedia *ifm, int m)
5984 {
5985 
5986 	ifmedia_add(ifm, m, 0, NULL);
5987 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
5988 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
5989 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
5990 }
5991 
5992 /*
5993  * This is the selected media, which is not quite the same as the active media.
5994  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
5995  * and active are not the same, and "media: Ethernet selected" otherwise.
5996  */
5997 static void
set_current_media(struct port_info * pi)5998 set_current_media(struct port_info *pi)
5999 {
6000 	struct link_config *lc;
6001 	struct ifmedia *ifm;
6002 	int mword;
6003 	u_int speed;
6004 
6005 	PORT_LOCK_ASSERT_OWNED(pi);
6006 
6007 	/* Leave current media alone if it's already set to IFM_NONE. */
6008 	ifm = &pi->media;
6009 	if (ifm->ifm_cur != NULL &&
6010 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
6011 		return;
6012 
6013 	lc = &pi->link_cfg;
6014 	if (lc->requested_aneg != AUTONEG_DISABLE &&
6015 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
6016 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
6017 		return;
6018 	}
6019 	mword = IFM_ETHER | IFM_FDX;
6020 	if (lc->requested_fc & PAUSE_TX)
6021 		mword |= IFM_ETH_TXPAUSE;
6022 	if (lc->requested_fc & PAUSE_RX)
6023 		mword |= IFM_ETH_RXPAUSE;
6024 	if (lc->requested_speed == 0)
6025 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
6026 	else
6027 		speed = lc->requested_speed;
6028 	mword |= port_mword(pi, speed_to_fwcap(speed));
6029 	ifmedia_set(ifm, mword);
6030 }
6031 
6032 /*
6033  * Returns true if the ifmedia list for the port cannot change.
6034  */
6035 static bool
fixed_ifmedia(struct port_info * pi)6036 fixed_ifmedia(struct port_info *pi)
6037 {
6038 
6039 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
6040 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
6041 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
6042 	    pi->port_type == FW_PORT_TYPE_KX4 ||
6043 	    pi->port_type == FW_PORT_TYPE_KX ||
6044 	    pi->port_type == FW_PORT_TYPE_KR ||
6045 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
6046 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
6047 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
6048 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
6049 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
6050 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
6051 }
6052 
6053 static void
build_medialist(struct port_info * pi)6054 build_medialist(struct port_info *pi)
6055 {
6056 	uint32_t ss, speed;
6057 	int unknown, mword, bit;
6058 	struct link_config *lc;
6059 	struct ifmedia *ifm;
6060 
6061 	PORT_LOCK_ASSERT_OWNED(pi);
6062 
6063 	if (pi->flags & FIXED_IFMEDIA)
6064 		return;
6065 
6066 	/*
6067 	 * Rebuild the ifmedia list.
6068 	 */
6069 	ifm = &pi->media;
6070 	ifmedia_removeall(ifm);
6071 	lc = &pi->link_cfg;
6072 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
6073 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
6074 		MPASS(ss != 0);
6075 no_media:
6076 		MPASS(LIST_EMPTY(&ifm->ifm_list));
6077 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
6078 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
6079 		return;
6080 	}
6081 
6082 	unknown = 0;
6083 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
6084 		speed = 1 << bit;
6085 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
6086 		if (ss & speed) {
6087 			mword = port_mword(pi, speed);
6088 			if (mword == IFM_NONE) {
6089 				goto no_media;
6090 			} else if (mword == IFM_UNKNOWN)
6091 				unknown++;
6092 			else
6093 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
6094 		}
6095 	}
6096 	if (unknown > 0) /* Add one unknown for all unknown media types. */
6097 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
6098 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
6099 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
6100 
6101 	set_current_media(pi);
6102 }
6103 
6104 /*
6105  * Initialize the requested fields in the link config based on driver tunables.
6106  */
6107 static void
init_link_config(struct port_info * pi)6108 init_link_config(struct port_info *pi)
6109 {
6110 	struct link_config *lc = &pi->link_cfg;
6111 
6112 	PORT_LOCK_ASSERT_OWNED(pi);
6113 
6114 	lc->requested_caps = 0;
6115 	lc->requested_speed = 0;
6116 
6117 	if (t4_autoneg == 0)
6118 		lc->requested_aneg = AUTONEG_DISABLE;
6119 	else if (t4_autoneg == 1)
6120 		lc->requested_aneg = AUTONEG_ENABLE;
6121 	else
6122 		lc->requested_aneg = AUTONEG_AUTO;
6123 
6124 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
6125 	    PAUSE_AUTONEG);
6126 
6127 	if (t4_fec & FEC_AUTO)
6128 		lc->requested_fec = FEC_AUTO;
6129 	else if (t4_fec == 0)
6130 		lc->requested_fec = FEC_NONE;
6131 	else {
6132 		/* -1 is handled by the FEC_AUTO block above and not here. */
6133 		lc->requested_fec = t4_fec &
6134 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6135 		if (lc->requested_fec == 0)
6136 			lc->requested_fec = FEC_AUTO;
6137 	}
6138 	if (t4_force_fec < 0)
6139 		lc->force_fec = -1;
6140 	else if (t4_force_fec > 0)
6141 		lc->force_fec = 1;
6142 	else
6143 		lc->force_fec = 0;
6144 }
6145 
6146 /*
6147  * Makes sure that all requested settings comply with what's supported by the
6148  * port.  Returns the number of settings that were invalid and had to be fixed.
6149  */
6150 static int
fixup_link_config(struct port_info * pi)6151 fixup_link_config(struct port_info *pi)
6152 {
6153 	int n = 0;
6154 	struct link_config *lc = &pi->link_cfg;
6155 	uint32_t fwspeed;
6156 
6157 	PORT_LOCK_ASSERT_OWNED(pi);
6158 
6159 	/* Speed (when not autonegotiating) */
6160 	if (lc->requested_speed != 0) {
6161 		fwspeed = speed_to_fwcap(lc->requested_speed);
6162 		if ((fwspeed & lc->pcaps) == 0) {
6163 			n++;
6164 			lc->requested_speed = 0;
6165 		}
6166 	}
6167 
6168 	/* Link autonegotiation */
6169 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6170 	    lc->requested_aneg == AUTONEG_DISABLE ||
6171 	    lc->requested_aneg == AUTONEG_AUTO);
6172 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6173 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6174 		n++;
6175 		lc->requested_aneg = AUTONEG_AUTO;
6176 	}
6177 
6178 	/* Flow control */
6179 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6180 	if (lc->requested_fc & PAUSE_TX &&
6181 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6182 		n++;
6183 		lc->requested_fc &= ~PAUSE_TX;
6184 	}
6185 	if (lc->requested_fc & PAUSE_RX &&
6186 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6187 		n++;
6188 		lc->requested_fc &= ~PAUSE_RX;
6189 	}
6190 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6191 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6192 		n++;
6193 		lc->requested_fc |= PAUSE_AUTONEG;
6194 	}
6195 
6196 	/* FEC */
6197 	if ((lc->requested_fec & FEC_RS &&
6198 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6199 	    (lc->requested_fec & FEC_BASER_RS &&
6200 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6201 		n++;
6202 		lc->requested_fec = FEC_AUTO;
6203 	}
6204 
6205 	return (n);
6206 }
6207 
6208 /*
6209  * Apply the requested L1 settings, which are expected to be valid, to the
6210  * hardware.
6211  */
6212 static int
apply_link_config(struct port_info * pi)6213 apply_link_config(struct port_info *pi)
6214 {
6215 	struct adapter *sc = pi->adapter;
6216 	struct link_config *lc = &pi->link_cfg;
6217 	int rc;
6218 
6219 #ifdef INVARIANTS
6220 	ASSERT_SYNCHRONIZED_OP(sc);
6221 	PORT_LOCK_ASSERT_OWNED(pi);
6222 
6223 	if (lc->requested_aneg == AUTONEG_ENABLE)
6224 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6225 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6226 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6227 	if (lc->requested_fc & PAUSE_TX)
6228 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6229 	if (lc->requested_fc & PAUSE_RX)
6230 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6231 	if (lc->requested_fec & FEC_RS)
6232 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6233 	if (lc->requested_fec & FEC_BASER_RS)
6234 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6235 #endif
6236 	if (!(sc->flags & IS_VF)) {
6237 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
6238 		if (rc != 0) {
6239 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6240 			return (rc);
6241 		}
6242 	}
6243 
6244 	/*
6245 	 * An L1_CFG will almost always result in a link-change event if the
6246 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6247 	 * the notification is processed.  If the link is down then the actual
6248 	 * settings are meaningless.
6249 	 *
6250 	 * This takes care of the case where a change in the L1 settings may not
6251 	 * result in a notification.
6252 	 */
6253 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6254 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6255 
6256 	return (0);
6257 }
6258 
6259 #define FW_MAC_EXACT_CHUNK	7
6260 struct mcaddr_ctx {
6261 	if_t ifp;
6262 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6263 	uint64_t hash;
6264 	int i;
6265 	int del;
6266 	int rc;
6267 };
6268 
6269 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6270 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6271 {
6272 	struct mcaddr_ctx *ctx = arg;
6273 	struct vi_info *vi = if_getsoftc(ctx->ifp);
6274 	struct port_info *pi = vi->pi;
6275 	struct adapter *sc = pi->adapter;
6276 
6277 	if (ctx->rc < 0)
6278 		return (0);
6279 
6280 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6281 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6282 	ctx->i++;
6283 
6284 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6285 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6286 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6287 		if (ctx->rc < 0) {
6288 			int j;
6289 
6290 			for (j = 0; j < ctx->i; j++) {
6291 				if_printf(ctx->ifp,
6292 				    "failed to add mc address"
6293 				    " %02x:%02x:%02x:"
6294 				    "%02x:%02x:%02x rc=%d\n",
6295 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6296 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6297 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6298 				    -ctx->rc);
6299 			}
6300 			return (0);
6301 		}
6302 		ctx->del = 0;
6303 		ctx->i = 0;
6304 	}
6305 
6306 	return (1);
6307 }
6308 
6309 /*
6310  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6311  * indicates which parameters should be programmed (the rest are left alone).
6312  */
6313 int
update_mac_settings(if_t ifp,int flags)6314 update_mac_settings(if_t ifp, int flags)
6315 {
6316 	int rc = 0;
6317 	struct vi_info *vi = if_getsoftc(ifp);
6318 	struct port_info *pi = vi->pi;
6319 	struct adapter *sc = pi->adapter;
6320 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6321 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6322 
6323 	ASSERT_SYNCHRONIZED_OP(sc);
6324 	KASSERT(flags, ("%s: not told what to update.", __func__));
6325 
6326 	if (flags & XGMAC_MTU)
6327 		mtu = if_getmtu(ifp);
6328 
6329 	if (flags & XGMAC_PROMISC)
6330 		promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6331 
6332 	if (flags & XGMAC_ALLMULTI)
6333 		allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6334 
6335 	if (flags & XGMAC_VLANEX)
6336 		vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6337 
6338 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6339 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6340 		    allmulti, 1, vlanex, false);
6341 		if (rc) {
6342 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6343 			    rc);
6344 			return (rc);
6345 		}
6346 	}
6347 
6348 	if (flags & XGMAC_UCADDR) {
6349 		uint8_t ucaddr[ETHER_ADDR_LEN];
6350 
6351 		bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6352 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6353 		    ucaddr, true, &vi->smt_idx);
6354 		if (rc < 0) {
6355 			rc = -rc;
6356 			if_printf(ifp, "change_mac failed: %d\n", rc);
6357 			return (rc);
6358 		} else {
6359 			vi->xact_addr_filt = rc;
6360 			rc = 0;
6361 		}
6362 	}
6363 
6364 	if (flags & XGMAC_MCADDRS) {
6365 		struct epoch_tracker et;
6366 		struct mcaddr_ctx ctx;
6367 		int j;
6368 
6369 		ctx.ifp = ifp;
6370 		ctx.hash = 0;
6371 		ctx.i = 0;
6372 		ctx.del = 1;
6373 		ctx.rc = 0;
6374 		/*
6375 		 * Unlike other drivers, we accumulate list of pointers into
6376 		 * interface address lists and we need to keep it safe even
6377 		 * after if_foreach_llmaddr() returns, thus we must enter the
6378 		 * network epoch.
6379 		 */
6380 		NET_EPOCH_ENTER(et);
6381 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6382 		if (ctx.rc < 0) {
6383 			NET_EPOCH_EXIT(et);
6384 			rc = -ctx.rc;
6385 			return (rc);
6386 		}
6387 		if (ctx.i > 0) {
6388 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6389 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6390 			NET_EPOCH_EXIT(et);
6391 			if (rc < 0) {
6392 				rc = -rc;
6393 				for (j = 0; j < ctx.i; j++) {
6394 					if_printf(ifp,
6395 					    "failed to add mcast address"
6396 					    " %02x:%02x:%02x:"
6397 					    "%02x:%02x:%02x rc=%d\n",
6398 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6399 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6400 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6401 					    rc);
6402 				}
6403 				return (rc);
6404 			}
6405 			ctx.del = 0;
6406 		} else
6407 			NET_EPOCH_EXIT(et);
6408 
6409 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6410 		if (rc != 0)
6411 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6412 			    rc);
6413 		if (ctx.del == 0) {
6414 			/* We clobbered the VXLAN entry if there was one. */
6415 			pi->vxlan_tcam_entry = false;
6416 		}
6417 	}
6418 
6419 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6420 	    pi->vxlan_tcam_entry == false) {
6421 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6422 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6423 		    true);
6424 		if (rc < 0) {
6425 			rc = -rc;
6426 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6427 			    rc);
6428 		} else {
6429 			MPASS(rc == sc->rawf_base + pi->port_id);
6430 			rc = 0;
6431 			pi->vxlan_tcam_entry = true;
6432 		}
6433 	}
6434 
6435 	return (rc);
6436 }
6437 
6438 /*
6439  * {begin|end}_synchronized_op must be called from the same thread.
6440  */
6441 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6442 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6443     char *wmesg)
6444 {
6445 	int rc;
6446 
6447 #ifdef WITNESS
6448 	/* the caller thinks it's ok to sleep, but is it really? */
6449 	if (flags & SLEEP_OK)
6450 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
6451 #endif
6452 	ADAPTER_LOCK(sc);
6453 	for (;;) {
6454 
6455 		if (vi && IS_DETACHING(vi)) {
6456 			rc = ENXIO;
6457 			goto done;
6458 		}
6459 
6460 		if (!IS_BUSY(sc)) {
6461 			rc = 0;
6462 			break;
6463 		}
6464 
6465 		if (!(flags & SLEEP_OK)) {
6466 			rc = EBUSY;
6467 			goto done;
6468 		}
6469 
6470 		if (mtx_sleep(&sc->flags, &sc->sc_lock,
6471 		    flags & INTR_OK ? PCATCH : 0, wmesg, 0)) {
6472 			rc = EINTR;
6473 			goto done;
6474 		}
6475 	}
6476 
6477 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6478 	SET_BUSY(sc);
6479 #ifdef INVARIANTS
6480 	sc->last_op = wmesg;
6481 	sc->last_op_thr = curthread;
6482 	sc->last_op_flags = flags;
6483 #endif
6484 
6485 done:
6486 	if (!(flags & HOLD_LOCK) || rc)
6487 		ADAPTER_UNLOCK(sc);
6488 
6489 	return (rc);
6490 }
6491 
6492 /*
6493  * Tell if_ioctl and if_init that the VI is going away.  This is
6494  * special variant of begin_synchronized_op and must be paired with a
6495  * call to end_vi_detach.
6496  */
6497 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6498 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6499 {
6500 	ADAPTER_LOCK(sc);
6501 	SET_DETACHING(vi);
6502 	wakeup(&sc->flags);
6503 	while (IS_BUSY(sc))
6504 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6505 	SET_BUSY(sc);
6506 #ifdef INVARIANTS
6507 	sc->last_op = "t4detach";
6508 	sc->last_op_thr = curthread;
6509 	sc->last_op_flags = 0;
6510 #endif
6511 	ADAPTER_UNLOCK(sc);
6512 }
6513 
6514 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6515 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6516 {
6517 	ADAPTER_LOCK(sc);
6518 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6519 	CLR_BUSY(sc);
6520 	CLR_DETACHING(vi);
6521 	wakeup(&sc->flags);
6522 	ADAPTER_UNLOCK(sc);
6523 }
6524 
6525 /*
6526  * {begin|end}_synchronized_op must be called from the same thread.
6527  */
6528 void
end_synchronized_op(struct adapter * sc,int flags)6529 end_synchronized_op(struct adapter *sc, int flags)
6530 {
6531 
6532 	if (flags & LOCK_HELD)
6533 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6534 	else
6535 		ADAPTER_LOCK(sc);
6536 
6537 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6538 	CLR_BUSY(sc);
6539 	wakeup(&sc->flags);
6540 	ADAPTER_UNLOCK(sc);
6541 }
6542 
6543 static int
cxgbe_init_synchronized(struct vi_info * vi)6544 cxgbe_init_synchronized(struct vi_info *vi)
6545 {
6546 	struct port_info *pi = vi->pi;
6547 	struct adapter *sc = pi->adapter;
6548 	if_t ifp = vi->ifp;
6549 	int rc = 0, i;
6550 	struct sge_txq *txq;
6551 
6552 	ASSERT_SYNCHRONIZED_OP(sc);
6553 
6554 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6555 		return (0);	/* already running */
6556 
6557 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6558 		return (rc);	/* error message displayed already */
6559 
6560 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6561 		return (rc); /* error message displayed already */
6562 
6563 	rc = update_mac_settings(ifp, XGMAC_ALL);
6564 	if (rc)
6565 		goto done;	/* error message displayed already */
6566 
6567 	PORT_LOCK(pi);
6568 	if (pi->up_vis == 0) {
6569 		t4_update_port_info(pi);
6570 		fixup_link_config(pi);
6571 		build_medialist(pi);
6572 		apply_link_config(pi);
6573 	}
6574 
6575 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6576 	if (rc != 0) {
6577 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6578 		PORT_UNLOCK(pi);
6579 		goto done;
6580 	}
6581 
6582 	/*
6583 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6584 	 * if this changes.
6585 	 */
6586 
6587 	for_each_txq(vi, i, txq) {
6588 		TXQ_LOCK(txq);
6589 		txq->eq.flags |= EQ_ENABLED;
6590 		TXQ_UNLOCK(txq);
6591 	}
6592 
6593 	/*
6594 	 * The first iq of the first port to come up is used for tracing.
6595 	 */
6596 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6597 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6598 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
6599 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
6600 		    V_QUEUENUMBER(sc->traceq));
6601 		pi->flags |= HAS_TRACEQ;
6602 	}
6603 
6604 	/* all ok */
6605 	pi->up_vis++;
6606 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6607 	if (pi->link_cfg.link_ok)
6608 		t4_os_link_changed(pi);
6609 	PORT_UNLOCK(pi);
6610 
6611 	mtx_lock(&vi->tick_mtx);
6612 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6613 		callout_reset(&vi->tick, hz, vi_tick, vi);
6614 	else
6615 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6616 	mtx_unlock(&vi->tick_mtx);
6617 done:
6618 	if (rc != 0)
6619 		cxgbe_uninit_synchronized(vi);
6620 
6621 	return (rc);
6622 }
6623 
6624 /*
6625  * Idempotent.
6626  */
6627 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6628 cxgbe_uninit_synchronized(struct vi_info *vi)
6629 {
6630 	struct port_info *pi = vi->pi;
6631 	struct adapter *sc = pi->adapter;
6632 	if_t ifp = vi->ifp;
6633 	int rc, i;
6634 	struct sge_txq *txq;
6635 
6636 	ASSERT_SYNCHRONIZED_OP(sc);
6637 
6638 	if (!(vi->flags & VI_INIT_DONE)) {
6639 		if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6640 			KASSERT(0, ("uninited VI is running"));
6641 			if_printf(ifp, "uninited VI with running ifnet.  "
6642 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6643 			    "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6644 			    if_getdrvflags(ifp));
6645 		}
6646 		return (0);
6647 	}
6648 
6649 	/*
6650 	 * Disable the VI so that all its data in either direction is discarded
6651 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6652 	 * tick) intact as the TP can deliver negative advice or data that it's
6653 	 * holding in its RAM (for an offloaded connection) even after the VI is
6654 	 * disabled.
6655 	 */
6656 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6657 	if (rc) {
6658 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6659 		return (rc);
6660 	}
6661 
6662 	for_each_txq(vi, i, txq) {
6663 		TXQ_LOCK(txq);
6664 		txq->eq.flags &= ~EQ_ENABLED;
6665 		TXQ_UNLOCK(txq);
6666 	}
6667 
6668 	mtx_lock(&vi->tick_mtx);
6669 	callout_stop(&vi->tick);
6670 	mtx_unlock(&vi->tick_mtx);
6671 
6672 	PORT_LOCK(pi);
6673 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6674 		PORT_UNLOCK(pi);
6675 		return (0);
6676 	}
6677 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6678 	pi->up_vis--;
6679 	if (pi->up_vis > 0) {
6680 		PORT_UNLOCK(pi);
6681 		return (0);
6682 	}
6683 
6684 	pi->link_cfg.link_ok = false;
6685 	pi->link_cfg.speed = 0;
6686 	pi->link_cfg.link_down_rc = 255;
6687 	t4_os_link_changed(pi);
6688 	PORT_UNLOCK(pi);
6689 
6690 	return (0);
6691 }
6692 
6693 /*
6694  * It is ok for this function to fail midway and return right away.  t4_detach
6695  * will walk the entire sc->irq list and clean up whatever is valid.
6696  */
6697 int
t4_setup_intr_handlers(struct adapter * sc)6698 t4_setup_intr_handlers(struct adapter *sc)
6699 {
6700 	int rc, rid, p, q, v;
6701 	char s[8];
6702 	struct irq *irq;
6703 	struct port_info *pi;
6704 	struct vi_info *vi;
6705 	struct sge *sge = &sc->sge;
6706 	struct sge_rxq *rxq;
6707 #ifdef TCP_OFFLOAD
6708 	struct sge_ofld_rxq *ofld_rxq;
6709 #endif
6710 #ifdef DEV_NETMAP
6711 	struct sge_nm_rxq *nm_rxq;
6712 #endif
6713 #ifdef RSS
6714 	int nbuckets = rss_getnumbuckets();
6715 #endif
6716 
6717 	/*
6718 	 * Setup interrupts.
6719 	 */
6720 	irq = &sc->irq[0];
6721 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6722 	if (forwarding_intr_to_fwq(sc))
6723 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6724 
6725 	/* Multiple interrupts. */
6726 	if (sc->flags & IS_VF)
6727 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6728 		    ("%s: too few intr.", __func__));
6729 	else
6730 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6731 		    ("%s: too few intr.", __func__));
6732 
6733 	/* The first one is always error intr on PFs */
6734 	if (!(sc->flags & IS_VF)) {
6735 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6736 		if (rc != 0)
6737 			return (rc);
6738 		irq++;
6739 		rid++;
6740 	}
6741 
6742 	/* The second one is always the firmware event queue (first on VFs) */
6743 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6744 	if (rc != 0)
6745 		return (rc);
6746 	irq++;
6747 	rid++;
6748 
6749 	for_each_port(sc, p) {
6750 		pi = sc->port[p];
6751 		for_each_vi(pi, v, vi) {
6752 			vi->first_intr = rid - 1;
6753 
6754 			if (vi->nnmrxq > 0) {
6755 				int n = max(vi->nrxq, vi->nnmrxq);
6756 
6757 				rxq = &sge->rxq[vi->first_rxq];
6758 #ifdef DEV_NETMAP
6759 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6760 #endif
6761 				for (q = 0; q < n; q++) {
6762 					snprintf(s, sizeof(s), "%x%c%x", p,
6763 					    'a' + v, q);
6764 					if (q < vi->nrxq)
6765 						irq->rxq = rxq++;
6766 #ifdef DEV_NETMAP
6767 					if (q < vi->nnmrxq)
6768 						irq->nm_rxq = nm_rxq++;
6769 
6770 					if (irq->nm_rxq != NULL &&
6771 					    irq->rxq == NULL) {
6772 						/* Netmap rx only */
6773 						rc = t4_alloc_irq(sc, irq, rid,
6774 						    t4_nm_intr, irq->nm_rxq, s);
6775 					}
6776 					if (irq->nm_rxq != NULL &&
6777 					    irq->rxq != NULL) {
6778 						/* NIC and Netmap rx */
6779 						rc = t4_alloc_irq(sc, irq, rid,
6780 						    t4_vi_intr, irq, s);
6781 					}
6782 #endif
6783 					if (irq->rxq != NULL &&
6784 					    irq->nm_rxq == NULL) {
6785 						/* NIC rx only */
6786 						rc = t4_alloc_irq(sc, irq, rid,
6787 						    t4_intr, irq->rxq, s);
6788 					}
6789 					if (rc != 0)
6790 						return (rc);
6791 #ifdef RSS
6792 					if (q < vi->nrxq) {
6793 						bus_bind_intr(sc->dev, irq->res,
6794 						    rss_getcpu(q % nbuckets));
6795 					}
6796 #endif
6797 					irq++;
6798 					rid++;
6799 					vi->nintr++;
6800 				}
6801 			} else {
6802 				for_each_rxq(vi, q, rxq) {
6803 					snprintf(s, sizeof(s), "%x%c%x", p,
6804 					    'a' + v, q);
6805 					rc = t4_alloc_irq(sc, irq, rid,
6806 					    t4_intr, rxq, s);
6807 					if (rc != 0)
6808 						return (rc);
6809 #ifdef RSS
6810 					bus_bind_intr(sc->dev, irq->res,
6811 					    rss_getcpu(q % nbuckets));
6812 #endif
6813 					irq++;
6814 					rid++;
6815 					vi->nintr++;
6816 				}
6817 			}
6818 #ifdef TCP_OFFLOAD
6819 			for_each_ofld_rxq(vi, q, ofld_rxq) {
6820 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
6821 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
6822 				    ofld_rxq, s);
6823 				if (rc != 0)
6824 					return (rc);
6825 				irq++;
6826 				rid++;
6827 				vi->nintr++;
6828 			}
6829 #endif
6830 		}
6831 	}
6832 	MPASS(irq == &sc->irq[sc->intr_count]);
6833 
6834 	return (0);
6835 }
6836 
6837 static void
write_global_rss_key(struct adapter * sc)6838 write_global_rss_key(struct adapter *sc)
6839 {
6840 #ifdef RSS
6841 	int i;
6842 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6843 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6844 
6845 	CTASSERT(RSS_KEYSIZE == 40);
6846 
6847 	rss_getkey((void *)&raw_rss_key[0]);
6848 	for (i = 0; i < nitems(rss_key); i++) {
6849 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
6850 	}
6851 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
6852 #endif
6853 }
6854 
6855 /*
6856  * Idempotent.
6857  */
6858 static int
adapter_full_init(struct adapter * sc)6859 adapter_full_init(struct adapter *sc)
6860 {
6861 	int rc, i;
6862 
6863 	ASSERT_SYNCHRONIZED_OP(sc);
6864 
6865 	/*
6866 	 * queues that belong to the adapter (not any particular port).
6867 	 */
6868 	rc = t4_setup_adapter_queues(sc);
6869 	if (rc != 0)
6870 		return (rc);
6871 
6872 	MPASS(sc->params.nports <= nitems(sc->tq));
6873 	for (i = 0; i < sc->params.nports; i++) {
6874 		if (sc->tq[i] != NULL)
6875 			continue;
6876 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
6877 		    taskqueue_thread_enqueue, &sc->tq[i]);
6878 		if (sc->tq[i] == NULL) {
6879 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
6880 			return (ENOMEM);
6881 		}
6882 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
6883 		    device_get_nameunit(sc->dev), i);
6884 	}
6885 
6886 	if (!(sc->flags & IS_VF)) {
6887 		write_global_rss_key(sc);
6888 		t4_intr_enable(sc);
6889 	}
6890 	return (0);
6891 }
6892 
6893 int
adapter_init(struct adapter * sc)6894 adapter_init(struct adapter *sc)
6895 {
6896 	int rc;
6897 
6898 	ASSERT_SYNCHRONIZED_OP(sc);
6899 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
6900 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
6901 	    ("%s: FULL_INIT_DONE already", __func__));
6902 
6903 	rc = adapter_full_init(sc);
6904 	if (rc != 0)
6905 		adapter_full_uninit(sc);
6906 	else
6907 		sc->flags |= FULL_INIT_DONE;
6908 
6909 	return (rc);
6910 }
6911 
6912 /*
6913  * Idempotent.
6914  */
6915 static void
adapter_full_uninit(struct adapter * sc)6916 adapter_full_uninit(struct adapter *sc)
6917 {
6918 	int i;
6919 
6920 	t4_teardown_adapter_queues(sc);
6921 
6922 	for (i = 0; i < nitems(sc->tq); i++) {
6923 		if (sc->tq[i] == NULL)
6924 			continue;
6925 		taskqueue_free(sc->tq[i]);
6926 		sc->tq[i] = NULL;
6927 	}
6928 
6929 	sc->flags &= ~FULL_INIT_DONE;
6930 }
6931 
6932 #ifdef RSS
6933 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
6934     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
6935     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
6936     RSS_HASHTYPE_RSS_UDP_IPV6)
6937 
6938 /* Translates kernel hash types to hardware. */
6939 static int
hashconfig_to_hashen(int hashconfig)6940 hashconfig_to_hashen(int hashconfig)
6941 {
6942 	int hashen = 0;
6943 
6944 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
6945 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
6946 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
6947 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
6948 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
6949 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6950 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6951 	}
6952 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
6953 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6954 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6955 	}
6956 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
6957 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6958 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
6959 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6960 
6961 	return (hashen);
6962 }
6963 
6964 /* Translates hardware hash types to kernel. */
6965 static int
hashen_to_hashconfig(int hashen)6966 hashen_to_hashconfig(int hashen)
6967 {
6968 	int hashconfig = 0;
6969 
6970 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
6971 		/*
6972 		 * If UDP hashing was enabled it must have been enabled for
6973 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
6974 		 * enabling any 4-tuple hash is nonsense configuration.
6975 		 */
6976 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6977 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
6978 
6979 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6980 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
6981 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6982 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
6983 	}
6984 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6985 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
6986 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6987 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
6988 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
6989 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
6990 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
6991 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
6992 
6993 	return (hashconfig);
6994 }
6995 #endif
6996 
6997 /*
6998  * Idempotent.
6999  */
7000 static int
vi_full_init(struct vi_info * vi)7001 vi_full_init(struct vi_info *vi)
7002 {
7003 	struct adapter *sc = vi->adapter;
7004 	struct sge_rxq *rxq;
7005 	int rc, i, j;
7006 #ifdef RSS
7007 	int nbuckets = rss_getnumbuckets();
7008 	int hashconfig = rss_gethashconfig();
7009 	int extra;
7010 #endif
7011 
7012 	ASSERT_SYNCHRONIZED_OP(sc);
7013 
7014 	/*
7015 	 * Allocate tx/rx/fl queues for this VI.
7016 	 */
7017 	rc = t4_setup_vi_queues(vi);
7018 	if (rc != 0)
7019 		return (rc);
7020 
7021 	/*
7022 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
7023 	 */
7024 	if (vi->nrxq > vi->rss_size) {
7025 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
7026 		    "some queues will never receive traffic.\n", vi->nrxq,
7027 		    vi->rss_size);
7028 	} else if (vi->rss_size % vi->nrxq) {
7029 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
7030 		    "expect uneven traffic distribution.\n", vi->nrxq,
7031 		    vi->rss_size);
7032 	}
7033 #ifdef RSS
7034 	if (vi->nrxq != nbuckets) {
7035 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
7036 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
7037 	}
7038 #endif
7039 	if (vi->rss == NULL)
7040 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
7041 		    M_ZERO | M_WAITOK);
7042 	for (i = 0; i < vi->rss_size;) {
7043 #ifdef RSS
7044 		j = rss_get_indirection_to_bucket(i);
7045 		j %= vi->nrxq;
7046 		rxq = &sc->sge.rxq[vi->first_rxq + j];
7047 		vi->rss[i++] = rxq->iq.abs_id;
7048 #else
7049 		for_each_rxq(vi, j, rxq) {
7050 			vi->rss[i++] = rxq->iq.abs_id;
7051 			if (i == vi->rss_size)
7052 				break;
7053 		}
7054 #endif
7055 	}
7056 
7057 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
7058 	    vi->rss, vi->rss_size);
7059 	if (rc != 0) {
7060 		CH_ERR(vi, "rss_config failed: %d\n", rc);
7061 		return (rc);
7062 	}
7063 
7064 #ifdef RSS
7065 	vi->hashen = hashconfig_to_hashen(hashconfig);
7066 
7067 	/*
7068 	 * We may have had to enable some hashes even though the global config
7069 	 * wants them disabled.  This is a potential problem that must be
7070 	 * reported to the user.
7071 	 */
7072 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
7073 
7074 	/*
7075 	 * If we consider only the supported hash types, then the enabled hashes
7076 	 * are a superset of the requested hashes.  In other words, there cannot
7077 	 * be any supported hash that was requested but not enabled, but there
7078 	 * can be hashes that were not requested but had to be enabled.
7079 	 */
7080 	extra &= SUPPORTED_RSS_HASHTYPES;
7081 	MPASS((extra & hashconfig) == 0);
7082 
7083 	if (extra) {
7084 		CH_ALERT(vi,
7085 		    "global RSS config (0x%x) cannot be accommodated.\n",
7086 		    hashconfig);
7087 	}
7088 	if (extra & RSS_HASHTYPE_RSS_IPV4)
7089 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
7090 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
7091 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
7092 	if (extra & RSS_HASHTYPE_RSS_IPV6)
7093 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
7094 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
7095 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
7096 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
7097 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
7098 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
7099 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
7100 #else
7101 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
7102 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
7103 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
7104 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
7105 #endif
7106 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
7107 	    0, 0);
7108 	if (rc != 0) {
7109 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
7110 		return (rc);
7111 	}
7112 
7113 	return (0);
7114 }
7115 
7116 int
vi_init(struct vi_info * vi)7117 vi_init(struct vi_info *vi)
7118 {
7119 	int rc;
7120 
7121 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
7122 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
7123 	    ("%s: VI_INIT_DONE already", __func__));
7124 
7125 	rc = vi_full_init(vi);
7126 	if (rc != 0)
7127 		vi_full_uninit(vi);
7128 	else
7129 		vi->flags |= VI_INIT_DONE;
7130 
7131 	return (rc);
7132 }
7133 
7134 /*
7135  * Idempotent.
7136  */
7137 static void
vi_full_uninit(struct vi_info * vi)7138 vi_full_uninit(struct vi_info *vi)
7139 {
7140 
7141 	if (vi->flags & VI_INIT_DONE) {
7142 		quiesce_vi(vi);
7143 		free(vi->rss, M_CXGBE);
7144 		free(vi->nm_rss, M_CXGBE);
7145 	}
7146 
7147 	t4_teardown_vi_queues(vi);
7148 	vi->flags &= ~VI_INIT_DONE;
7149 }
7150 
7151 static void
quiesce_txq(struct sge_txq * txq)7152 quiesce_txq(struct sge_txq *txq)
7153 {
7154 	struct sge_eq *eq = &txq->eq;
7155 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7156 
7157 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7158 	MPASS(!(eq->flags & EQ_ENABLED));
7159 
7160 	/* Wait for the mp_ring to empty. */
7161 	while (!mp_ring_is_idle(txq->r)) {
7162 		mp_ring_check_drainage(txq->r, 4096);
7163 		pause("rquiesce", 1);
7164 	}
7165 	MPASS(txq->txp.npkt == 0);
7166 
7167 	if (eq->flags & EQ_HW_ALLOCATED) {
7168 		/*
7169 		 * Hardware is alive and working normally.  Wait for it to
7170 		 * finish and then wait for the driver to catch up and reclaim
7171 		 * all descriptors.
7172 		 */
7173 		while (spg->cidx != htobe16(eq->pidx))
7174 			pause("equiesce", 1);
7175 		while (eq->cidx != eq->pidx)
7176 			pause("dquiesce", 1);
7177 	} else {
7178 		/*
7179 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7180 		 * descriptors directly.
7181 		 */
7182 		TXQ_LOCK(txq);
7183 		while (eq->cidx != eq->pidx) {
7184 			struct mbuf *m, *nextpkt;
7185 			struct tx_sdesc *txsd;
7186 
7187 			txsd = &txq->sdesc[eq->cidx];
7188 			for (m = txsd->m; m != NULL; m = nextpkt) {
7189 				nextpkt = m->m_nextpkt;
7190 				m->m_nextpkt = NULL;
7191 				m_freem(m);
7192 			}
7193 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7194 		}
7195 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7196 		TXQ_UNLOCK(txq);
7197 	}
7198 }
7199 
7200 static void
quiesce_wrq(struct sge_wrq * wrq)7201 quiesce_wrq(struct sge_wrq *wrq)
7202 {
7203 	struct wrqe *wr;
7204 
7205 	TXQ_LOCK(wrq);
7206 	while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7207 		STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7208 #ifdef INVARIANTS
7209 		wrq->nwr_pending--;
7210 		wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7211 #endif
7212 		free(wr, M_CXGBE);
7213 	}
7214 	MPASS(wrq->nwr_pending == 0);
7215 	MPASS(wrq->ndesc_needed == 0);
7216 	wrq->nwr_pending = 0;
7217 	wrq->ndesc_needed = 0;
7218 	TXQ_UNLOCK(wrq);
7219 }
7220 
7221 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7222 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7223 {
7224 	/* Synchronize with the interrupt handler */
7225 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7226 		pause("iqfree", 1);
7227 
7228 	if (fl != NULL) {
7229 		MPASS(iq->flags & IQ_HAS_FL);
7230 
7231 		mtx_lock(&sc->sfl_lock);
7232 		FL_LOCK(fl);
7233 		fl->flags |= FL_DOOMED;
7234 		FL_UNLOCK(fl);
7235 		callout_stop(&sc->sfl_callout);
7236 		mtx_unlock(&sc->sfl_lock);
7237 
7238 		KASSERT((fl->flags & FL_STARVING) == 0,
7239 		    ("%s: still starving", __func__));
7240 
7241 		/* Release all buffers if hardware is no longer available. */
7242 		if (!(iq->flags & IQ_HW_ALLOCATED))
7243 			free_fl_buffers(sc, fl);
7244 	}
7245 }
7246 
7247 /*
7248  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7249  * that no new work is being enqueued by the hardware or the driver.  That part
7250  * should be arranged before calling this function.
7251  */
7252 static void
quiesce_vi(struct vi_info * vi)7253 quiesce_vi(struct vi_info *vi)
7254 {
7255 	int i;
7256 	struct adapter *sc = vi->adapter;
7257 	struct sge_rxq *rxq;
7258 	struct sge_txq *txq;
7259 #ifdef TCP_OFFLOAD
7260 	struct sge_ofld_rxq *ofld_rxq;
7261 #endif
7262 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7263 	struct sge_ofld_txq *ofld_txq;
7264 #endif
7265 
7266 	if (!(vi->flags & VI_INIT_DONE))
7267 		return;
7268 
7269 	for_each_txq(vi, i, txq) {
7270 		quiesce_txq(txq);
7271 	}
7272 
7273 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7274 	for_each_ofld_txq(vi, i, ofld_txq) {
7275 		quiesce_wrq(&ofld_txq->wrq);
7276 	}
7277 #endif
7278 
7279 	for_each_rxq(vi, i, rxq) {
7280 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7281 	}
7282 
7283 #ifdef TCP_OFFLOAD
7284 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7285 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7286 	}
7287 #endif
7288 }
7289 
7290 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7291 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7292     driver_intr_t *handler, void *arg, char *name)
7293 {
7294 	int rc;
7295 
7296 	irq->rid = rid;
7297 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7298 	    RF_SHAREABLE | RF_ACTIVE);
7299 	if (irq->res == NULL) {
7300 		device_printf(sc->dev,
7301 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7302 		return (ENOMEM);
7303 	}
7304 
7305 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7306 	    NULL, handler, arg, &irq->tag);
7307 	if (rc != 0) {
7308 		device_printf(sc->dev,
7309 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7310 		    rid, name, rc);
7311 	} else if (name)
7312 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7313 
7314 	return (rc);
7315 }
7316 
7317 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7318 t4_free_irq(struct adapter *sc, struct irq *irq)
7319 {
7320 	if (irq->tag)
7321 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7322 	if (irq->res)
7323 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7324 
7325 	bzero(irq, sizeof(*irq));
7326 
7327 	return (0);
7328 }
7329 
7330 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7331 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7332 {
7333 
7334 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7335 	t4_get_regs(sc, buf, regs->len);
7336 }
7337 
7338 #define	A_PL_INDIR_CMD	0x1f8
7339 
7340 #define	S_PL_AUTOINC	31
7341 #define	M_PL_AUTOINC	0x1U
7342 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7343 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7344 
7345 #define	S_PL_VFID	20
7346 #define	M_PL_VFID	0xffU
7347 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7348 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7349 
7350 #define	S_PL_ADDR	0
7351 #define	M_PL_ADDR	0xfffffU
7352 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7353 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7354 
7355 #define	A_PL_INDIR_DATA	0x1fc
7356 
7357 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7358 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7359 {
7360 	u32 stats[2];
7361 
7362 	if (sc->flags & IS_VF) {
7363 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7364 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7365 	} else {
7366 		mtx_assert(&sc->reg_lock, MA_OWNED);
7367 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7368 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7369 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7370 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7371 	}
7372 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7373 }
7374 
7375 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7376 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7377 {
7378 
7379 #define GET_STAT(name) \
7380 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7381 
7382 	if (!(sc->flags & IS_VF))
7383 		mtx_lock(&sc->reg_lock);
7384 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7385 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7386 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7387 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7388 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7389 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7390 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7391 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7392 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7393 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7394 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7395 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7396 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7397 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7398 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7399 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7400 	if (!(sc->flags & IS_VF))
7401 		mtx_unlock(&sc->reg_lock);
7402 
7403 #undef GET_STAT
7404 }
7405 
7406 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7407 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7408 {
7409 	int reg;
7410 
7411 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7412 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7413 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7414 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7415 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7416 }
7417 
7418 static void
vi_refresh_stats(struct vi_info * vi)7419 vi_refresh_stats(struct vi_info *vi)
7420 {
7421 	struct timeval tv;
7422 	const struct timeval interval = {0, 250000};	/* 250ms */
7423 
7424 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7425 
7426 	if (vi->flags & VI_SKIP_STATS)
7427 		return;
7428 
7429 	getmicrotime(&tv);
7430 	timevalsub(&tv, &interval);
7431 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7432 		return;
7433 
7434 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7435 	getmicrotime(&vi->last_refreshed);
7436 }
7437 
7438 static void
cxgbe_refresh_stats(struct vi_info * vi)7439 cxgbe_refresh_stats(struct vi_info *vi)
7440 {
7441 	u_int i, v, tnl_cong_drops, chan_map;
7442 	struct timeval tv;
7443 	const struct timeval interval = {0, 250000};	/* 250ms */
7444 	struct port_info *pi;
7445 	struct adapter *sc;
7446 
7447 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7448 
7449 	if (vi->flags & VI_SKIP_STATS)
7450 		return;
7451 
7452 	getmicrotime(&tv);
7453 	timevalsub(&tv, &interval);
7454 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7455 		return;
7456 
7457 	pi = vi->pi;
7458 	sc = vi->adapter;
7459 	tnl_cong_drops = 0;
7460 	t4_get_port_stats(sc, pi->port_id, &pi->stats);
7461 	chan_map = pi->rx_e_chan_map;
7462 	while (chan_map) {
7463 		i = ffs(chan_map) - 1;
7464 		mtx_lock(&sc->reg_lock);
7465 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7466 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7467 		mtx_unlock(&sc->reg_lock);
7468 		tnl_cong_drops += v;
7469 		chan_map &= ~(1 << i);
7470 	}
7471 	pi->tnl_cong_drops = tnl_cong_drops;
7472 	getmicrotime(&vi->last_refreshed);
7473 }
7474 
7475 static void
cxgbe_tick(void * arg)7476 cxgbe_tick(void *arg)
7477 {
7478 	struct vi_info *vi = arg;
7479 
7480 	MPASS(IS_MAIN_VI(vi));
7481 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7482 
7483 	cxgbe_refresh_stats(vi);
7484 	callout_schedule(&vi->tick, hz);
7485 }
7486 
7487 static void
vi_tick(void * arg)7488 vi_tick(void *arg)
7489 {
7490 	struct vi_info *vi = arg;
7491 
7492 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7493 
7494 	vi_refresh_stats(vi);
7495 	callout_schedule(&vi->tick, hz);
7496 }
7497 
7498 /*
7499  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7500  */
7501 static char *caps_decoder[] = {
7502 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7503 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7504 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7505 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7506 	    "\006HASHFILTER\007ETHOFLD",
7507 	"\20\001TOE",					/* 4: TOE */
7508 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
7509 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7510 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7511 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7512 	    "\007T10DIF"
7513 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7514 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7515 	    "\004TLS_HW",
7516 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7517 		    "\004PO_INITIATOR\005PO_TARGET",
7518 };
7519 
7520 void
t4_sysctls(struct adapter * sc)7521 t4_sysctls(struct adapter *sc)
7522 {
7523 	struct sysctl_ctx_list *ctx = &sc->ctx;
7524 	struct sysctl_oid *oid;
7525 	struct sysctl_oid_list *children, *c0;
7526 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7527 
7528 	/*
7529 	 * dev.t4nex.X.
7530 	 */
7531 	oid = device_get_sysctl_tree(sc->dev);
7532 	c0 = children = SYSCTL_CHILDREN(oid);
7533 
7534 	sc->sc_do_rxcopy = 1;
7535 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7536 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7537 
7538 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7539 	    sc->params.nports, "# of ports");
7540 
7541 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7542 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7543 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7544 	    "available doorbells");
7545 
7546 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7547 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7548 
7549 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7550 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7551 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7552 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7553 
7554 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7555 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7556 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7557 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7558 
7559 	t4_sge_sysctls(sc, ctx, children);
7560 
7561 	sc->lro_timeout = 100;
7562 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7563 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7564 
7565 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7566 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7567 
7568 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7569 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7570 
7571 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7572 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7573 
7574 	if (sc->flags & IS_VF)
7575 		return;
7576 
7577 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7578 	    NULL, chip_rev(sc), "chip hardware revision");
7579 
7580 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7581 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7582 
7583 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7584 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7585 
7586 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7587 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7588 
7589 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7590 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7591 
7592 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7593 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7594 
7595 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7596 	    sc->er_version, 0, "expansion ROM version");
7597 
7598 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7599 	    sc->bs_version, 0, "bootstrap firmware version");
7600 
7601 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7602 	    NULL, sc->params.scfg_vers, "serial config version");
7603 
7604 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7605 	    NULL, sc->params.vpd_vers, "VPD version");
7606 
7607 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7608 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7609 
7610 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7611 	    sc->cfcsum, "config file checksum");
7612 
7613 #define SYSCTL_CAP(name, n, text) \
7614 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7615 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7616 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7617 	    "available " text " capabilities")
7618 
7619 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7620 	SYSCTL_CAP(linkcaps, 1, "link");
7621 	SYSCTL_CAP(switchcaps, 2, "switch");
7622 	SYSCTL_CAP(niccaps, 3, "NIC");
7623 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7624 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7625 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7626 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7627 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7628 #undef SYSCTL_CAP
7629 
7630 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7631 	    NULL, sc->tids.nftids, "number of filters");
7632 
7633 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7634 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7635 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7636 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7637 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7638 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7639 
7640 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg",
7641 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7642 	    sysctl_loadavg, "A",
7643 	    "microprocessor load averages (debug firmwares only)");
7644 
7645 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7646 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7647 	    "I", "core Vdd (in mV)");
7648 
7649 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7650 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7651 	    sysctl_cpus, "A", "local CPUs");
7652 
7653 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7654 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7655 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7656 
7657 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7658 	    &sc->swintr, 0, "software triggered interrupts");
7659 
7660 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7661 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7662 	    "1 = reset adapter, 0 = zero reset counter");
7663 
7664 	/*
7665 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7666 	 */
7667 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7668 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
7669 	    "logs and miscellaneous information");
7670 	children = SYSCTL_CHILDREN(oid);
7671 
7672 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
7673 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7674 	    sysctl_cctrl, "A", "congestion control");
7675 
7676 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
7677 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7678 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
7679 
7680 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
7681 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7682 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
7683 
7684 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
7685 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7686 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
7687 
7688 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
7689 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 3,
7690 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
7691 
7692 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
7693 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 4,
7694 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
7695 
7696 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
7697 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 5,
7698 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
7699 
7700 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
7701 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7702 	    sysctl_cim_la, "A", "CIM logic analyzer");
7703 
7704 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
7705 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7706 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7707 
7708 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
7709 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7710 	    0 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
7711 
7712 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
7713 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7714 	    1 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
7715 
7716 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
7717 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7718 	    2 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
7719 
7720 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
7721 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7722 	    3 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
7723 
7724 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
7725 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7726 	    4 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
7727 
7728 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
7729 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7730 	    5 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
7731 
7732 	if (chip_id(sc) > CHELSIO_T4) {
7733 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
7734 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7735 		    6 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7736 		    "CIM OBQ 6 (SGE0-RX)");
7737 
7738 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
7739 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7740 		    7 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7741 		    "CIM OBQ 7 (SGE1-RX)");
7742 	}
7743 
7744 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
7745 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7746 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7747 
7748 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
7749 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7750 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
7751 
7752 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
7753 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7754 	    sysctl_cpl_stats, "A", "CPL statistics");
7755 
7756 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
7757 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7758 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
7759 
7760 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
7761 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7762 	    sysctl_tid_stats, "A", "tid stats");
7763 
7764 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
7765 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7766 	    sysctl_devlog, "A", "firmware's device log");
7767 
7768 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
7769 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7770 	    sysctl_fcoe_stats, "A", "FCoE statistics");
7771 
7772 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
7773 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7774 	    sysctl_hw_sched, "A", "hardware scheduler ");
7775 
7776 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
7777 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7778 	    sysctl_l2t, "A", "hardware L2 table");
7779 
7780 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
7781 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7782 	    sysctl_smt, "A", "hardware source MAC table");
7783 
7784 #ifdef INET6
7785 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
7786 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7787 	    sysctl_clip, "A", "active CLIP table entries");
7788 #endif
7789 
7790 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
7791 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7792 	    sysctl_lb_stats, "A", "loopback statistics");
7793 
7794 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
7795 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7796 	    sysctl_meminfo, "A", "memory regions");
7797 
7798 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
7799 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7800 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
7801 	    "A", "MPS TCAM entries");
7802 
7803 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
7804 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7805 	    sysctl_path_mtus, "A", "path MTUs");
7806 
7807 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
7808 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7809 	    sysctl_pm_stats, "A", "PM statistics");
7810 
7811 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
7812 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7813 	    sysctl_rdma_stats, "A", "RDMA statistics");
7814 
7815 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
7816 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7817 	    sysctl_tcp_stats, "A", "TCP statistics");
7818 
7819 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
7820 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7821 	    sysctl_tids, "A", "TID information");
7822 
7823 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
7824 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7825 	    sysctl_tp_err_stats, "A", "TP error statistics");
7826 
7827 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
7828 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7829 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
7830 
7831 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
7832 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7833 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
7834 
7835 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
7836 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7837 	    sysctl_tp_la, "A", "TP logic analyzer");
7838 
7839 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
7840 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7841 	    sysctl_tx_rate, "A", "Tx rate");
7842 
7843 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
7844 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7845 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
7846 
7847 	if (chip_id(sc) >= CHELSIO_T5) {
7848 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
7849 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7850 		    sysctl_wcwr_stats, "A", "write combined work requests");
7851 	}
7852 
7853 #ifdef KERN_TLS
7854 	if (is_ktls(sc)) {
7855 		/*
7856 		 * dev.t4nex.0.tls.
7857 		 */
7858 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
7859 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
7860 		children = SYSCTL_CHILDREN(oid);
7861 
7862 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
7863 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
7864 		    "keys in work requests (1) or attempt to store TLS keys "
7865 		    "in card memory.");
7866 
7867 		if (is_t6(sc))
7868 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
7869 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
7870 			    "combine TCB field updates with TLS record work "
7871 			    "requests.");
7872 	}
7873 #endif
7874 
7875 #ifdef TCP_OFFLOAD
7876 	if (is_offload(sc)) {
7877 		int i;
7878 		char s[4];
7879 
7880 		/*
7881 		 * dev.t4nex.X.toe.
7882 		 */
7883 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
7884 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
7885 		children = SYSCTL_CHILDREN(oid);
7886 
7887 		sc->tt.cong_algorithm = -1;
7888 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
7889 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
7890 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
7891 		    "3 = highspeed)");
7892 
7893 		sc->tt.sndbuf = -1;
7894 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
7895 		    &sc->tt.sndbuf, 0, "hardware send buffer");
7896 
7897 		sc->tt.ddp = 0;
7898 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
7899 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
7900 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
7901 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
7902 
7903 		sc->tt.rx_coalesce = -1;
7904 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
7905 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
7906 
7907 		sc->tt.tls = 1;
7908 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
7909 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
7910 		    "Inline TLS allowed");
7911 
7912 		sc->tt.tx_align = -1;
7913 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
7914 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
7915 
7916 		sc->tt.tx_zcopy = 0;
7917 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
7918 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
7919 		    "Enable zero-copy aio_write(2)");
7920 
7921 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
7922 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7923 		    "cop_managed_offloading", CTLFLAG_RW,
7924 		    &sc->tt.cop_managed_offloading, 0,
7925 		    "COP (Connection Offload Policy) controls all TOE offload");
7926 
7927 		sc->tt.autorcvbuf_inc = 16 * 1024;
7928 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
7929 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
7930 		    "autorcvbuf increment");
7931 
7932 		sc->tt.update_hc_on_pmtu_change = 1;
7933 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7934 		    "update_hc_on_pmtu_change", CTLFLAG_RW,
7935 		    &sc->tt.update_hc_on_pmtu_change, 0,
7936 		    "Update hostcache entry if the PMTU changes");
7937 
7938 		sc->tt.iso = 1;
7939 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
7940 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
7941 
7942 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
7943 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7944 		    sysctl_tp_tick, "A", "TP timer tick (us)");
7945 
7946 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
7947 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7948 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
7949 
7950 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
7951 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7952 		    sysctl_tp_tick, "A", "DACK tick (us)");
7953 
7954 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
7955 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7956 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
7957 
7958 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
7959 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7960 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
7961 		    "Minimum retransmit interval (us)");
7962 
7963 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
7964 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7965 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
7966 		    "Maximum retransmit interval (us)");
7967 
7968 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
7969 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7970 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
7971 		    "Persist timer min (us)");
7972 
7973 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
7974 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7975 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
7976 		    "Persist timer max (us)");
7977 
7978 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
7979 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7980 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
7981 		    "Keepalive idle timer (us)");
7982 
7983 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
7984 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7985 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
7986 		    "Keepalive interval timer (us)");
7987 
7988 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
7989 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7990 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
7991 
7992 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
7993 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7994 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
7995 		    "FINWAIT2 timer (us)");
7996 
7997 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
7998 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7999 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
8000 		    "Number of SYN retransmissions before abort");
8001 
8002 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
8003 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8004 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
8005 		    "Number of retransmissions before abort");
8006 
8007 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
8008 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8009 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
8010 		    "Number of keepalive probes before abort");
8011 
8012 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
8013 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8014 		    "TOE retransmit backoffs");
8015 		children = SYSCTL_CHILDREN(oid);
8016 		for (i = 0; i < 16; i++) {
8017 			snprintf(s, sizeof(s), "%u", i);
8018 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
8019 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8020 			    i, sysctl_tp_backoff, "IU",
8021 			    "TOE retransmit backoff");
8022 		}
8023 	}
8024 #endif
8025 }
8026 
8027 void
vi_sysctls(struct vi_info * vi)8028 vi_sysctls(struct vi_info *vi)
8029 {
8030 	struct sysctl_ctx_list *ctx = &vi->ctx;
8031 	struct sysctl_oid *oid;
8032 	struct sysctl_oid_list *children;
8033 
8034 	/*
8035 	 * dev.v?(cxgbe|cxl).X.
8036 	 */
8037 	oid = device_get_sysctl_tree(vi->dev);
8038 	children = SYSCTL_CHILDREN(oid);
8039 
8040 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
8041 	    vi->viid, "VI identifer");
8042 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
8043 	    &vi->nrxq, 0, "# of rx queues");
8044 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
8045 	    &vi->ntxq, 0, "# of tx queues");
8046 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
8047 	    &vi->first_rxq, 0, "index of first rx queue");
8048 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
8049 	    &vi->first_txq, 0, "index of first tx queue");
8050 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
8051 	    vi->rss_base, "start of RSS indirection table");
8052 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
8053 	    vi->rss_size, "size of RSS indirection table");
8054 
8055 	if (IS_MAIN_VI(vi)) {
8056 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
8057 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8058 		    sysctl_noflowq, "IU",
8059 		    "Reserve queue 0 for non-flowid packets");
8060 	}
8061 
8062 	if (vi->adapter->flags & IS_VF) {
8063 		MPASS(vi->flags & TX_USES_VM_WR);
8064 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
8065 		    NULL, 1, "use VM work requests for transmit");
8066 	} else {
8067 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
8068 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8069 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
8070 	}
8071 
8072 #ifdef TCP_OFFLOAD
8073 	if (vi->nofldrxq != 0) {
8074 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
8075 		    &vi->nofldrxq, 0,
8076 		    "# of rx queues for offloaded TCP connections");
8077 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
8078 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
8079 		    "index of first TOE rx queue");
8080 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
8081 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8082 		    sysctl_holdoff_tmr_idx_ofld, "I",
8083 		    "holdoff timer index for TOE queues");
8084 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
8085 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8086 		    sysctl_holdoff_pktc_idx_ofld, "I",
8087 		    "holdoff packet counter index for TOE queues");
8088 	}
8089 #endif
8090 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
8091 	if (vi->nofldtxq != 0) {
8092 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
8093 		    &vi->nofldtxq, 0,
8094 		    "# of tx queues for TOE/ETHOFLD");
8095 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
8096 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
8097 		    "index of first TOE/ETHOFLD tx queue");
8098 	}
8099 #endif
8100 #ifdef DEV_NETMAP
8101 	if (vi->nnmrxq != 0) {
8102 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
8103 		    &vi->nnmrxq, 0, "# of netmap rx queues");
8104 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
8105 		    &vi->nnmtxq, 0, "# of netmap tx queues");
8106 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
8107 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
8108 		    "index of first netmap rx queue");
8109 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
8110 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
8111 		    "index of first netmap tx queue");
8112 	}
8113 #endif
8114 
8115 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
8116 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8117 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
8118 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
8119 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8120 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
8121 
8122 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
8123 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8124 	    sysctl_qsize_rxq, "I", "rx queue size");
8125 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
8126 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8127 	    sysctl_qsize_txq, "I", "tx queue size");
8128 }
8129 
8130 static void
cxgbe_sysctls(struct port_info * pi)8131 cxgbe_sysctls(struct port_info *pi)
8132 {
8133 	struct sysctl_ctx_list *ctx = &pi->ctx;
8134 	struct sysctl_oid *oid;
8135 	struct sysctl_oid_list *children, *children2;
8136 	struct adapter *sc = pi->adapter;
8137 	int i;
8138 	char name[16];
8139 	static char *tc_flags = {"\20\1USER"};
8140 
8141 	/*
8142 	 * dev.cxgbe.X.
8143 	 */
8144 	oid = device_get_sysctl_tree(pi->dev);
8145 	children = SYSCTL_CHILDREN(oid);
8146 
8147 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8148 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8149 	    sysctl_linkdnrc, "A", "reason why link is down");
8150 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8151 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8152 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8153 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8154 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8155 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8156 		    sysctl_btphy, "I", "PHY firmware version");
8157 	}
8158 
8159 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8160 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8161 	    sysctl_pause_settings, "A",
8162 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8163 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8164 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8165 	    "FEC in use on the link");
8166 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8167 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8168 	    sysctl_requested_fec, "A",
8169 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8170 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8171 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8172 	    "FEC recommended by the cable/transceiver");
8173 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8174 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8175 	    sysctl_autoneg, "I",
8176 	    "autonegotiation (-1 = not supported)");
8177 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8178 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8179 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8180 
8181 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8182 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8183 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8184 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8185 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8186 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8187 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8188 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8189 
8190 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8191 	    port_top_speed(pi), "max speed (in Gbps)");
8192 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8193 	    pi->mps_bg_map, "MPS buffer group map");
8194 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8195 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8196 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8197 	    pi->tx_chan, "TP tx c-channel");
8198 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8199 	    pi->rx_chan, "TP rx c-channel");
8200 
8201 	if (sc->flags & IS_VF)
8202 		return;
8203 
8204 	/*
8205 	 * dev.(cxgbe|cxl).X.tc.
8206 	 */
8207 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8208 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8209 	    "Tx scheduler traffic classes (cl_rl)");
8210 	children2 = SYSCTL_CHILDREN(oid);
8211 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8212 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8213 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8214 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8215 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8216 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8217 	for (i = 0; i < sc->params.nsched_cls; i++) {
8218 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8219 
8220 		snprintf(name, sizeof(name), "%d", i);
8221 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8222 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8223 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8224 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8225 		    CTLFLAG_RD, &tc->state, 0, "current state");
8226 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8227 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8228 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8229 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8230 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8231 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8232 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8233 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8234 		    "traffic class parameters");
8235 	}
8236 
8237 	/*
8238 	 * dev.cxgbe.X.stats.
8239 	 */
8240 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8241 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8242 	children = SYSCTL_CHILDREN(oid);
8243 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8244 	    &pi->tx_parse_error, 0,
8245 	    "# of tx packets with invalid length or # of segments");
8246 
8247 #define T4_REGSTAT(name, stat, desc) \
8248     SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8249 	CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8250 	t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8251         sysctl_handle_t4_reg64, "QU", desc)
8252 
8253 /* We get these from port_stats and they may be stale by up to 1s */
8254 #define T4_PORTSTAT(name, desc) \
8255 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
8256 	    &pi->stats.name, desc)
8257 
8258 	T4_REGSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8259 	T4_REGSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8260 	T4_REGSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8261 	T4_REGSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8262 	T4_REGSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8263 	T4_REGSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8264 	T4_REGSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8265 	T4_REGSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8266 	T4_REGSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8267 	T4_REGSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8268 	T4_REGSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8269 	T4_REGSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8270 	T4_REGSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8271 	T4_REGSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8272 	T4_REGSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8273 	T4_REGSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8274 	T4_REGSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8275 	T4_REGSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8276 	T4_REGSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8277 	T4_REGSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8278 	T4_REGSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8279 	T4_REGSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8280 	T4_REGSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8281 
8282 	T4_REGSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8283 	T4_REGSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8284 	T4_REGSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8285 	T4_REGSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8286 	T4_REGSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8287 	T4_REGSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8288 	T4_REGSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8289 	if (is_t6(sc)) {
8290 		T4_PORTSTAT(rx_fcs_err,
8291 		    "# of frames received with bad FCS since last link up");
8292 	} else {
8293 		T4_REGSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8294 		    "# of frames received with bad FCS");
8295 	}
8296 	T4_REGSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8297 	T4_REGSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8298 	T4_REGSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8299 	T4_REGSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8300 	T4_REGSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8301 	T4_REGSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8302 	T4_REGSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8303 	T4_REGSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8304 	T4_REGSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8305 	T4_REGSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8306 	T4_REGSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8307 	T4_REGSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8308 	T4_REGSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8309 	T4_REGSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8310 	T4_REGSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8311 	T4_REGSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8312 	T4_REGSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8313 	T4_REGSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8314 	T4_REGSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8315 
8316 	T4_PORTSTAT(rx_ovflow0, "# drops due to buffer-group 0 overflows");
8317 	T4_PORTSTAT(rx_ovflow1, "# drops due to buffer-group 1 overflows");
8318 	T4_PORTSTAT(rx_ovflow2, "# drops due to buffer-group 2 overflows");
8319 	T4_PORTSTAT(rx_ovflow3, "# drops due to buffer-group 3 overflows");
8320 	T4_PORTSTAT(rx_trunc0, "# of buffer-group 0 truncated packets");
8321 	T4_PORTSTAT(rx_trunc1, "# of buffer-group 1 truncated packets");
8322 	T4_PORTSTAT(rx_trunc2, "# of buffer-group 2 truncated packets");
8323 	T4_PORTSTAT(rx_trunc3, "# of buffer-group 3 truncated packets");
8324 
8325 #undef T4_REGSTAT
8326 #undef T4_PORTSTAT
8327 }
8328 
8329 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8330 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8331 {
8332 	int rc, *i, space = 0;
8333 	struct sbuf sb;
8334 
8335 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8336 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8337 		if (space)
8338 			sbuf_printf(&sb, " ");
8339 		sbuf_printf(&sb, "%d", *i);
8340 		space = 1;
8341 	}
8342 	rc = sbuf_finish(&sb);
8343 	sbuf_delete(&sb);
8344 	return (rc);
8345 }
8346 
8347 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8348 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8349 {
8350 	int rc;
8351 	struct sbuf *sb;
8352 
8353 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8354 	if (sb == NULL)
8355 		return (ENOMEM);
8356 
8357 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8358 	rc = sbuf_finish(sb);
8359 	sbuf_delete(sb);
8360 
8361 	return (rc);
8362 }
8363 
8364 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8365 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8366 {
8367 	int rc;
8368 	struct sbuf *sb;
8369 
8370 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8371 	if (sb == NULL)
8372 		return (ENOMEM);
8373 
8374 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8375 	rc = sbuf_finish(sb);
8376 	sbuf_delete(sb);
8377 
8378 	return (rc);
8379 }
8380 
8381 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8382 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8383 {
8384 	struct port_info *pi = arg1;
8385 	int op = arg2;
8386 	struct adapter *sc = pi->adapter;
8387 	u_int v;
8388 	int rc;
8389 
8390 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8391 	if (rc)
8392 		return (rc);
8393 	if (!hw_all_ok(sc))
8394 		rc = ENXIO;
8395 	else {
8396 		/* XXX: magic numbers */
8397 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8398 		    op ? 0x20 : 0xc820, &v);
8399 	}
8400 	end_synchronized_op(sc, 0);
8401 	if (rc)
8402 		return (rc);
8403 	if (op == 0)
8404 		v /= 256;
8405 
8406 	rc = sysctl_handle_int(oidp, &v, 0, req);
8407 	return (rc);
8408 }
8409 
8410 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8411 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8412 {
8413 	struct vi_info *vi = arg1;
8414 	int rc, val;
8415 
8416 	val = vi->rsrv_noflowq;
8417 	rc = sysctl_handle_int(oidp, &val, 0, req);
8418 	if (rc != 0 || req->newptr == NULL)
8419 		return (rc);
8420 
8421 	if ((val >= 1) && (vi->ntxq > 1))
8422 		vi->rsrv_noflowq = 1;
8423 	else
8424 		vi->rsrv_noflowq = 0;
8425 
8426 	return (rc);
8427 }
8428 
8429 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8430 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8431 {
8432 	struct vi_info *vi = arg1;
8433 	struct adapter *sc = vi->adapter;
8434 	int rc, val, i;
8435 
8436 	MPASS(!(sc->flags & IS_VF));
8437 
8438 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8439 	rc = sysctl_handle_int(oidp, &val, 0, req);
8440 	if (rc != 0 || req->newptr == NULL)
8441 		return (rc);
8442 
8443 	if (val != 0 && val != 1)
8444 		return (EINVAL);
8445 
8446 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8447 	    "t4txvm");
8448 	if (rc)
8449 		return (rc);
8450 	if (!hw_all_ok(sc))
8451 		rc = ENXIO;
8452 	else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8453 		/*
8454 		 * We don't want parse_pkt to run with one setting (VF or PF)
8455 		 * and then eth_tx to see a different setting but still use
8456 		 * stale information calculated by parse_pkt.
8457 		 */
8458 		rc = EBUSY;
8459 	} else {
8460 		struct port_info *pi = vi->pi;
8461 		struct sge_txq *txq;
8462 		uint32_t ctrl0;
8463 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8464 
8465 		if (val) {
8466 			vi->flags |= TX_USES_VM_WR;
8467 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8468 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8469 			    V_TXPKT_INTF(pi->tx_chan));
8470 			if (!(sc->flags & IS_VF))
8471 				npkt--;
8472 		} else {
8473 			vi->flags &= ~TX_USES_VM_WR;
8474 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8475 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8476 			    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
8477 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8478 		}
8479 		for_each_txq(vi, i, txq) {
8480 			txq->cpl_ctrl0 = ctrl0;
8481 			txq->txp.max_npkt = npkt;
8482 		}
8483 	}
8484 	end_synchronized_op(sc, LOCK_HELD);
8485 	return (rc);
8486 }
8487 
8488 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8489 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8490 {
8491 	struct vi_info *vi = arg1;
8492 	struct adapter *sc = vi->adapter;
8493 	int idx, rc, i;
8494 	struct sge_rxq *rxq;
8495 	uint8_t v;
8496 
8497 	idx = vi->tmr_idx;
8498 
8499 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8500 	if (rc != 0 || req->newptr == NULL)
8501 		return (rc);
8502 
8503 	if (idx < 0 || idx >= SGE_NTIMERS)
8504 		return (EINVAL);
8505 
8506 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8507 	    "t4tmr");
8508 	if (rc)
8509 		return (rc);
8510 
8511 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8512 	for_each_rxq(vi, i, rxq) {
8513 #ifdef atomic_store_rel_8
8514 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8515 #else
8516 		rxq->iq.intr_params = v;
8517 #endif
8518 	}
8519 	vi->tmr_idx = idx;
8520 
8521 	end_synchronized_op(sc, LOCK_HELD);
8522 	return (0);
8523 }
8524 
8525 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8526 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8527 {
8528 	struct vi_info *vi = arg1;
8529 	struct adapter *sc = vi->adapter;
8530 	int idx, rc;
8531 
8532 	idx = vi->pktc_idx;
8533 
8534 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8535 	if (rc != 0 || req->newptr == NULL)
8536 		return (rc);
8537 
8538 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8539 		return (EINVAL);
8540 
8541 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8542 	    "t4pktc");
8543 	if (rc)
8544 		return (rc);
8545 
8546 	if (vi->flags & VI_INIT_DONE)
8547 		rc = EBUSY; /* cannot be changed once the queues are created */
8548 	else
8549 		vi->pktc_idx = idx;
8550 
8551 	end_synchronized_op(sc, LOCK_HELD);
8552 	return (rc);
8553 }
8554 
8555 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8556 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8557 {
8558 	struct vi_info *vi = arg1;
8559 	struct adapter *sc = vi->adapter;
8560 	int qsize, rc;
8561 
8562 	qsize = vi->qsize_rxq;
8563 
8564 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8565 	if (rc != 0 || req->newptr == NULL)
8566 		return (rc);
8567 
8568 	if (qsize < 128 || (qsize & 7))
8569 		return (EINVAL);
8570 
8571 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8572 	    "t4rxqs");
8573 	if (rc)
8574 		return (rc);
8575 
8576 	if (vi->flags & VI_INIT_DONE)
8577 		rc = EBUSY; /* cannot be changed once the queues are created */
8578 	else
8579 		vi->qsize_rxq = qsize;
8580 
8581 	end_synchronized_op(sc, LOCK_HELD);
8582 	return (rc);
8583 }
8584 
8585 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8586 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8587 {
8588 	struct vi_info *vi = arg1;
8589 	struct adapter *sc = vi->adapter;
8590 	int qsize, rc;
8591 
8592 	qsize = vi->qsize_txq;
8593 
8594 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8595 	if (rc != 0 || req->newptr == NULL)
8596 		return (rc);
8597 
8598 	if (qsize < 128 || qsize > 65536)
8599 		return (EINVAL);
8600 
8601 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8602 	    "t4txqs");
8603 	if (rc)
8604 		return (rc);
8605 
8606 	if (vi->flags & VI_INIT_DONE)
8607 		rc = EBUSY; /* cannot be changed once the queues are created */
8608 	else
8609 		vi->qsize_txq = qsize;
8610 
8611 	end_synchronized_op(sc, LOCK_HELD);
8612 	return (rc);
8613 }
8614 
8615 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8616 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8617 {
8618 	struct port_info *pi = arg1;
8619 	struct adapter *sc = pi->adapter;
8620 	struct link_config *lc = &pi->link_cfg;
8621 	int rc;
8622 
8623 	if (req->newptr == NULL) {
8624 		struct sbuf *sb;
8625 		static char *bits = "\20\1RX\2TX\3AUTO";
8626 
8627 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8628 		if (sb == NULL)
8629 			return (ENOMEM);
8630 
8631 		if (lc->link_ok) {
8632 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8633 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8634 		} else {
8635 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8636 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8637 		}
8638 		rc = sbuf_finish(sb);
8639 		sbuf_delete(sb);
8640 	} else {
8641 		char s[2];
8642 		int n;
8643 
8644 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8645 		    PAUSE_AUTONEG));
8646 		s[1] = 0;
8647 
8648 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8649 		if (rc != 0)
8650 			return(rc);
8651 
8652 		if (s[1] != 0)
8653 			return (EINVAL);
8654 		if (s[0] < '0' || s[0] > '9')
8655 			return (EINVAL);	/* not a number */
8656 		n = s[0] - '0';
8657 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8658 			return (EINVAL);	/* some other bit is set too */
8659 
8660 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8661 		    "t4PAUSE");
8662 		if (rc)
8663 			return (rc);
8664 		if (hw_all_ok(sc)) {
8665 			PORT_LOCK(pi);
8666 			lc->requested_fc = n;
8667 			fixup_link_config(pi);
8668 			if (pi->up_vis > 0)
8669 				rc = apply_link_config(pi);
8670 			set_current_media(pi);
8671 			PORT_UNLOCK(pi);
8672 		}
8673 		end_synchronized_op(sc, 0);
8674 	}
8675 
8676 	return (rc);
8677 }
8678 
8679 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8680 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8681 {
8682 	struct port_info *pi = arg1;
8683 	struct link_config *lc = &pi->link_cfg;
8684 	int rc;
8685 	struct sbuf *sb;
8686 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2";
8687 
8688 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8689 	if (sb == NULL)
8690 		return (ENOMEM);
8691 	if (lc->link_ok)
8692 		sbuf_printf(sb, "%b", lc->fec, bits);
8693 	else
8694 		sbuf_printf(sb, "no link");
8695 	rc = sbuf_finish(sb);
8696 	sbuf_delete(sb);
8697 
8698 	return (rc);
8699 }
8700 
8701 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)8702 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8703 {
8704 	struct port_info *pi = arg1;
8705 	struct adapter *sc = pi->adapter;
8706 	struct link_config *lc = &pi->link_cfg;
8707 	int rc;
8708 	int8_t old;
8709 
8710 	if (req->newptr == NULL) {
8711 		struct sbuf *sb;
8712 		static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2"
8713 		    "\5RSVD3\6auto\7module";
8714 
8715 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8716 		if (sb == NULL)
8717 			return (ENOMEM);
8718 
8719 		sbuf_printf(sb, "%b", lc->requested_fec, bits);
8720 		rc = sbuf_finish(sb);
8721 		sbuf_delete(sb);
8722 	} else {
8723 		char s[8];
8724 		int n;
8725 
8726 		snprintf(s, sizeof(s), "%d",
8727 		    lc->requested_fec == FEC_AUTO ? -1 :
8728 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
8729 
8730 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8731 		if (rc != 0)
8732 			return(rc);
8733 
8734 		n = strtol(&s[0], NULL, 0);
8735 		if (n < 0 || n & FEC_AUTO)
8736 			n = FEC_AUTO;
8737 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
8738 			return (EINVAL);/* some other bit is set too */
8739 
8740 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8741 		    "t4reqf");
8742 		if (rc)
8743 			return (rc);
8744 		PORT_LOCK(pi);
8745 		old = lc->requested_fec;
8746 		if (n == FEC_AUTO)
8747 			lc->requested_fec = FEC_AUTO;
8748 		else if (n == 0 || n == FEC_NONE)
8749 			lc->requested_fec = FEC_NONE;
8750 		else {
8751 			if ((lc->pcaps |
8752 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
8753 			    lc->pcaps) {
8754 				rc = ENOTSUP;
8755 				goto done;
8756 			}
8757 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
8758 			    FEC_MODULE);
8759 		}
8760 		if (hw_all_ok(sc)) {
8761 			fixup_link_config(pi);
8762 			if (pi->up_vis > 0) {
8763 				rc = apply_link_config(pi);
8764 				if (rc != 0) {
8765 					lc->requested_fec = old;
8766 					if (rc == FW_EPROTO)
8767 						rc = ENOTSUP;
8768 				}
8769 			}
8770 		}
8771 done:
8772 		PORT_UNLOCK(pi);
8773 		end_synchronized_op(sc, 0);
8774 	}
8775 
8776 	return (rc);
8777 }
8778 
8779 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)8780 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
8781 {
8782 	struct port_info *pi = arg1;
8783 	struct adapter *sc = pi->adapter;
8784 	struct link_config *lc = &pi->link_cfg;
8785 	int rc;
8786 	int8_t fec;
8787 	struct sbuf *sb;
8788 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2\5RSVD3";
8789 
8790 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8791 	if (sb == NULL)
8792 		return (ENOMEM);
8793 
8794 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
8795 		rc = EBUSY;
8796 		goto done;
8797 	}
8798 	if (!hw_all_ok(sc)) {
8799 		rc = ENXIO;
8800 		goto done;
8801 	}
8802 	PORT_LOCK(pi);
8803 	if (pi->up_vis == 0) {
8804 		/*
8805 		 * If all the interfaces are administratively down the firmware
8806 		 * does not report transceiver changes.  Refresh port info here.
8807 		 * This is the only reason we have a synchronized op in this
8808 		 * function.  Just PORT_LOCK would have been enough otherwise.
8809 		 */
8810 		t4_update_port_info(pi);
8811 	}
8812 
8813 	fec = lc->fec_hint;
8814 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
8815 	    !fec_supported(lc->pcaps)) {
8816 		PORT_UNLOCK(pi);
8817 		sbuf_printf(sb, "n/a");
8818 	} else {
8819 		if (fec == 0)
8820 			fec = FEC_NONE;
8821 		PORT_UNLOCK(pi);
8822 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, bits);
8823 	}
8824 	rc = sbuf_finish(sb);
8825 done:
8826 	sbuf_delete(sb);
8827 	end_synchronized_op(sc, 0);
8828 
8829 	return (rc);
8830 }
8831 
8832 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)8833 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
8834 {
8835 	struct port_info *pi = arg1;
8836 	struct adapter *sc = pi->adapter;
8837 	struct link_config *lc = &pi->link_cfg;
8838 	int rc, val;
8839 
8840 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
8841 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
8842 	else
8843 		val = -1;
8844 	rc = sysctl_handle_int(oidp, &val, 0, req);
8845 	if (rc != 0 || req->newptr == NULL)
8846 		return (rc);
8847 	if (val == 0)
8848 		val = AUTONEG_DISABLE;
8849 	else if (val == 1)
8850 		val = AUTONEG_ENABLE;
8851 	else
8852 		val = AUTONEG_AUTO;
8853 
8854 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8855 	    "t4aneg");
8856 	if (rc)
8857 		return (rc);
8858 	PORT_LOCK(pi);
8859 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
8860 		rc = ENOTSUP;
8861 		goto done;
8862 	}
8863 	lc->requested_aneg = val;
8864 	if (hw_all_ok(sc)) {
8865 		fixup_link_config(pi);
8866 		if (pi->up_vis > 0)
8867 			rc = apply_link_config(pi);
8868 		set_current_media(pi);
8869 	}
8870 done:
8871 	PORT_UNLOCK(pi);
8872 	end_synchronized_op(sc, 0);
8873 	return (rc);
8874 }
8875 
8876 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)8877 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
8878 {
8879 	struct port_info *pi = arg1;
8880 	struct adapter *sc = pi->adapter;
8881 	struct link_config *lc = &pi->link_cfg;
8882 	int rc, val;
8883 
8884 	val = lc->force_fec;
8885 	MPASS(val >= -1 && val <= 1);
8886 	rc = sysctl_handle_int(oidp, &val, 0, req);
8887 	if (rc != 0 || req->newptr == NULL)
8888 		return (rc);
8889 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
8890 		return (ENOTSUP);
8891 	if (val < -1 || val > 1)
8892 		return (EINVAL);
8893 
8894 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
8895 	if (rc)
8896 		return (rc);
8897 	PORT_LOCK(pi);
8898 	lc->force_fec = val;
8899 	if (hw_all_ok(sc)) {
8900 		fixup_link_config(pi);
8901 		if (pi->up_vis > 0)
8902 			rc = apply_link_config(pi);
8903 	}
8904 	PORT_UNLOCK(pi);
8905 	end_synchronized_op(sc, 0);
8906 	return (rc);
8907 }
8908 
8909 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)8910 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
8911 {
8912 	struct adapter *sc = arg1;
8913 	int rc, reg = arg2;
8914 	uint64_t val;
8915 
8916 	mtx_lock(&sc->reg_lock);
8917 	if (hw_off_limits(sc))
8918 		rc = ENXIO;
8919 	else {
8920 		rc = 0;
8921 		val = t4_read_reg64(sc, reg);
8922 	}
8923 	mtx_unlock(&sc->reg_lock);
8924 	if (rc == 0)
8925 		rc = sysctl_handle_64(oidp, &val, 0, req);
8926 	return (rc);
8927 }
8928 
8929 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)8930 sysctl_temperature(SYSCTL_HANDLER_ARGS)
8931 {
8932 	struct adapter *sc = arg1;
8933 	int rc, t;
8934 	uint32_t param, val;
8935 
8936 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
8937 	if (rc)
8938 		return (rc);
8939 	if (!hw_all_ok(sc))
8940 		rc = ENXIO;
8941 	else {
8942 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8943 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8944 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
8945 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8946 	}
8947 	end_synchronized_op(sc, 0);
8948 	if (rc)
8949 		return (rc);
8950 
8951 	/* unknown is returned as 0 but we display -1 in that case */
8952 	t = val == 0 ? -1 : val;
8953 
8954 	rc = sysctl_handle_int(oidp, &t, 0, req);
8955 	return (rc);
8956 }
8957 
8958 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)8959 sysctl_vdd(SYSCTL_HANDLER_ARGS)
8960 {
8961 	struct adapter *sc = arg1;
8962 	int rc;
8963 	uint32_t param, val;
8964 
8965 	if (sc->params.core_vdd == 0) {
8966 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8967 		    "t4vdd");
8968 		if (rc)
8969 			return (rc);
8970 		if (!hw_all_ok(sc))
8971 			rc = ENXIO;
8972 		else {
8973 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8974 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8975 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
8976 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
8977 			    &param, &val);
8978 		}
8979 		end_synchronized_op(sc, 0);
8980 		if (rc)
8981 			return (rc);
8982 		sc->params.core_vdd = val;
8983 	}
8984 
8985 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
8986 }
8987 
8988 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)8989 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
8990 {
8991 	struct adapter *sc = arg1;
8992 	int rc, v;
8993 	uint32_t param, val;
8994 
8995 	v = sc->sensor_resets;
8996 	rc = sysctl_handle_int(oidp, &v, 0, req);
8997 	if (rc != 0 || req->newptr == NULL || v <= 0)
8998 		return (rc);
8999 
9000 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
9001 	    chip_id(sc) < CHELSIO_T5)
9002 		return (ENOTSUP);
9003 
9004 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
9005 	if (rc)
9006 		return (rc);
9007 	if (!hw_all_ok(sc))
9008 		rc = ENXIO;
9009 	else {
9010 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9011 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9012 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
9013 		val = 1;
9014 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9015 	}
9016 	end_synchronized_op(sc, 0);
9017 	if (rc == 0)
9018 		sc->sensor_resets++;
9019 	return (rc);
9020 }
9021 
9022 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)9023 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
9024 {
9025 	struct adapter *sc = arg1;
9026 	struct sbuf *sb;
9027 	int rc;
9028 	uint32_t param, val;
9029 
9030 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
9031 	if (rc)
9032 		return (rc);
9033 	if (hw_all_ok(sc))
9034 		rc = ENXIO;
9035 	else {
9036 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9037 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
9038 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9039 	}
9040 	end_synchronized_op(sc, 0);
9041 	if (rc)
9042 		return (rc);
9043 
9044 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9045 	if (sb == NULL)
9046 		return (ENOMEM);
9047 
9048 	if (val == 0xffffffff) {
9049 		/* Only debug and custom firmwares report load averages. */
9050 		sbuf_printf(sb, "not available");
9051 	} else {
9052 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
9053 		    (val >> 16) & 0xff);
9054 	}
9055 	rc = sbuf_finish(sb);
9056 	sbuf_delete(sb);
9057 
9058 	return (rc);
9059 }
9060 
9061 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)9062 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
9063 {
9064 	struct adapter *sc = arg1;
9065 	struct sbuf *sb;
9066 	int rc, i;
9067 	uint16_t incr[NMTUS][NCCTRL_WIN];
9068 	static const char *dec_fac[] = {
9069 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
9070 		"0.9375"
9071 	};
9072 
9073 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9074 	if (sb == NULL)
9075 		return (ENOMEM);
9076 
9077 	rc = 0;
9078 	mtx_lock(&sc->reg_lock);
9079 	if (hw_off_limits(sc))
9080 		rc = ENXIO;
9081 	else
9082 		t4_read_cong_tbl(sc, incr);
9083 	mtx_unlock(&sc->reg_lock);
9084 	if (rc)
9085 		goto done;
9086 
9087 	for (i = 0; i < NCCTRL_WIN; ++i) {
9088 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
9089 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
9090 		    incr[5][i], incr[6][i], incr[7][i]);
9091 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
9092 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
9093 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
9094 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
9095 	}
9096 
9097 	rc = sbuf_finish(sb);
9098 done:
9099 	sbuf_delete(sb);
9100 	return (rc);
9101 }
9102 
9103 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
9104 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
9105 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
9106 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
9107 };
9108 
9109 static int
sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)9110 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
9111 {
9112 	struct adapter *sc = arg1;
9113 	struct sbuf *sb;
9114 	int rc, i, n, qid = arg2;
9115 	uint32_t *buf, *p;
9116 	char *qtype;
9117 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
9118 
9119 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
9120 	    ("%s: bad qid %d\n", __func__, qid));
9121 
9122 	if (qid < CIM_NUM_IBQ) {
9123 		/* inbound queue */
9124 		qtype = "IBQ";
9125 		n = 4 * CIM_IBQ_SIZE;
9126 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9127 		mtx_lock(&sc->reg_lock);
9128 		if (hw_off_limits(sc))
9129 			rc = -ENXIO;
9130 		else
9131 			rc = t4_read_cim_ibq(sc, qid, buf, n);
9132 		mtx_unlock(&sc->reg_lock);
9133 	} else {
9134 		/* outbound queue */
9135 		qtype = "OBQ";
9136 		qid -= CIM_NUM_IBQ;
9137 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
9138 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9139 		mtx_lock(&sc->reg_lock);
9140 		if (hw_off_limits(sc))
9141 			rc = -ENXIO;
9142 		else
9143 			rc = t4_read_cim_obq(sc, qid, buf, n);
9144 		mtx_unlock(&sc->reg_lock);
9145 	}
9146 
9147 	if (rc < 0) {
9148 		rc = -rc;
9149 		goto done;
9150 	}
9151 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9152 
9153 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9154 	if (sb == NULL) {
9155 		rc = ENOMEM;
9156 		goto done;
9157 	}
9158 
9159 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
9160 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9161 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9162 		    p[2], p[3]);
9163 
9164 	rc = sbuf_finish(sb);
9165 	sbuf_delete(sb);
9166 done:
9167 	free(buf, M_CXGBE);
9168 	return (rc);
9169 }
9170 
9171 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9172 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9173 {
9174 	uint32_t *p;
9175 
9176 	sbuf_printf(sb, "Status   Data      PC%s",
9177 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9178 	    "     LS0Stat  LS0Addr             LS0Data");
9179 
9180 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9181 		if (cfg & F_UPDBGLACAPTPCONLY) {
9182 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9183 			    p[6], p[7]);
9184 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9185 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9186 			    p[4] & 0xff, p[5] >> 8);
9187 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9188 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9189 			    p[1] & 0xf, p[2] >> 4);
9190 		} else {
9191 			sbuf_printf(sb,
9192 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9193 			    "%08x%08x%08x%08x",
9194 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9195 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9196 			    p[6], p[7]);
9197 		}
9198 	}
9199 }
9200 
9201 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9202 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9203 {
9204 	uint32_t *p;
9205 
9206 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9207 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9208 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9209 
9210 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9211 		if (cfg & F_UPDBGLACAPTPCONLY) {
9212 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9213 			    p[3] & 0xff, p[2], p[1], p[0]);
9214 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9215 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9216 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9217 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9218 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9219 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9220 			    p[6] >> 16);
9221 		} else {
9222 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9223 			    "%08x %08x %08x %08x %08x %08x",
9224 			    (p[9] >> 16) & 0xff,
9225 			    p[9] & 0xffff, p[8] >> 16,
9226 			    p[8] & 0xffff, p[7] >> 16,
9227 			    p[7] & 0xffff, p[6] >> 16,
9228 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9229 		}
9230 	}
9231 }
9232 
9233 static int
sbuf_cim_la(struct adapter * sc,struct sbuf * sb,int flags)9234 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
9235 {
9236 	uint32_t cfg, *buf;
9237 	int rc;
9238 
9239 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9240 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9241 	    M_ZERO | flags);
9242 	if (buf == NULL)
9243 		return (ENOMEM);
9244 
9245 	mtx_lock(&sc->reg_lock);
9246 	if (hw_off_limits(sc))
9247 		rc = ENXIO;
9248 	else {
9249 		rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
9250 		if (rc == 0)
9251 			rc = -t4_cim_read_la(sc, buf, NULL);
9252 	}
9253 	mtx_unlock(&sc->reg_lock);
9254 	if (rc == 0) {
9255 		if (chip_id(sc) < CHELSIO_T6)
9256 			sbuf_cim_la4(sc, sb, buf, cfg);
9257 		else
9258 			sbuf_cim_la6(sc, sb, buf, cfg);
9259 	}
9260 	free(buf, M_CXGBE);
9261 	return (rc);
9262 }
9263 
9264 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9265 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9266 {
9267 	struct adapter *sc = arg1;
9268 	struct sbuf *sb;
9269 	int rc;
9270 
9271 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9272 	if (sb == NULL)
9273 		return (ENOMEM);
9274 
9275 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
9276 	if (rc == 0)
9277 		rc = sbuf_finish(sb);
9278 	sbuf_delete(sb);
9279 	return (rc);
9280 }
9281 
9282 static void
dump_cim_regs(struct adapter * sc)9283 dump_cim_regs(struct adapter *sc)
9284 {
9285 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9286 	    device_get_nameunit(sc->dev),
9287 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9288 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9289 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9290 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9291 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9292 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9293 	    device_get_nameunit(sc->dev),
9294 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9295 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9296 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9297 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9298 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9299 }
9300 
9301 static void
dump_cimla(struct adapter * sc)9302 dump_cimla(struct adapter *sc)
9303 {
9304 	struct sbuf sb;
9305 	int rc;
9306 
9307 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9308 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9309 		    device_get_nameunit(sc->dev));
9310 		return;
9311 	}
9312 	rc = sbuf_cim_la(sc, &sb, M_WAITOK);
9313 	if (rc == 0) {
9314 		rc = sbuf_finish(&sb);
9315 		if (rc == 0) {
9316 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9317 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9318 		}
9319 	}
9320 	sbuf_delete(&sb);
9321 }
9322 
9323 void
t4_os_cim_err(struct adapter * sc)9324 t4_os_cim_err(struct adapter *sc)
9325 {
9326 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9327 }
9328 
9329 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9330 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9331 {
9332 	struct adapter *sc = arg1;
9333 	u_int i;
9334 	struct sbuf *sb;
9335 	uint32_t *buf, *p;
9336 	int rc;
9337 
9338 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9339 	if (sb == NULL)
9340 		return (ENOMEM);
9341 
9342 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9343 	    M_ZERO | M_WAITOK);
9344 
9345 	rc = 0;
9346 	mtx_lock(&sc->reg_lock);
9347 	if (hw_off_limits(sc))
9348 		rc = ENXIO;
9349 	else
9350 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9351 	mtx_unlock(&sc->reg_lock);
9352 	if (rc)
9353 		goto done;
9354 
9355 	p = buf;
9356 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9357 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9358 		    p[1], p[0]);
9359 	}
9360 
9361 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9362 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9363 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9364 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9365 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9366 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9367 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9368 		    p[0] & 1);
9369 	}
9370 	rc = sbuf_finish(sb);
9371 done:
9372 	sbuf_delete(sb);
9373 	free(buf, M_CXGBE);
9374 	return (rc);
9375 }
9376 
9377 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9378 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9379 {
9380 	struct adapter *sc = arg1;
9381 	u_int i;
9382 	struct sbuf *sb;
9383 	uint32_t *buf, *p;
9384 	int rc;
9385 
9386 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9387 	if (sb == NULL)
9388 		return (ENOMEM);
9389 
9390 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9391 	    M_ZERO | M_WAITOK);
9392 
9393 	rc = 0;
9394 	mtx_lock(&sc->reg_lock);
9395 	if (hw_off_limits(sc))
9396 		rc = ENXIO;
9397 	else
9398 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9399 	mtx_unlock(&sc->reg_lock);
9400 	if (rc)
9401 		goto done;
9402 
9403 	p = buf;
9404 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9405 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9406 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9407 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9408 		    p[4], p[3], p[2], p[1], p[0]);
9409 	}
9410 
9411 	sbuf_printf(sb, "\n\nCntl ID               Data");
9412 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9413 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9414 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9415 	}
9416 
9417 	rc = sbuf_finish(sb);
9418 done:
9419 	sbuf_delete(sb);
9420 	free(buf, M_CXGBE);
9421 	return (rc);
9422 }
9423 
9424 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9425 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9426 {
9427 	struct adapter *sc = arg1;
9428 	struct sbuf *sb;
9429 	int rc, i;
9430 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9431 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9432 	uint16_t thres[CIM_NUM_IBQ];
9433 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9434 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9435 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9436 
9437 	cim_num_obq = sc->chip_params->cim_num_obq;
9438 	if (is_t4(sc)) {
9439 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9440 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9441 	} else {
9442 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9443 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9444 	}
9445 	nq = CIM_NUM_IBQ + cim_num_obq;
9446 
9447 	mtx_lock(&sc->reg_lock);
9448 	if (hw_off_limits(sc))
9449 		rc = ENXIO;
9450 	else {
9451 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9452 		if (rc == 0) {
9453 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9454 			    obq_wr);
9455 			if (rc == 0)
9456 				t4_read_cimq_cfg(sc, base, size, thres);
9457 		}
9458 	}
9459 	mtx_unlock(&sc->reg_lock);
9460 	if (rc)
9461 		return (rc);
9462 
9463 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9464 	if (sb == NULL)
9465 		return (ENOMEM);
9466 
9467 	sbuf_printf(sb,
9468 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9469 
9470 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9471 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9472 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9473 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9474 		    G_QUEREMFLITS(p[2]) * 16);
9475 	for ( ; i < nq; i++, p += 4, wr += 2)
9476 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9477 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9478 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9479 		    G_QUEREMFLITS(p[2]) * 16);
9480 
9481 	rc = sbuf_finish(sb);
9482 	sbuf_delete(sb);
9483 
9484 	return (rc);
9485 }
9486 
9487 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9488 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9489 {
9490 	struct adapter *sc = arg1;
9491 	struct sbuf *sb;
9492 	int rc;
9493 	struct tp_cpl_stats stats;
9494 
9495 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9496 	if (sb == NULL)
9497 		return (ENOMEM);
9498 
9499 	rc = 0;
9500 	mtx_lock(&sc->reg_lock);
9501 	if (hw_off_limits(sc))
9502 		rc = ENXIO;
9503 	else
9504 		t4_tp_get_cpl_stats(sc, &stats, 0);
9505 	mtx_unlock(&sc->reg_lock);
9506 	if (rc)
9507 		goto done;
9508 
9509 	if (sc->chip_params->nchan > 2) {
9510 		sbuf_printf(sb, "                 channel 0  channel 1"
9511 		    "  channel 2  channel 3");
9512 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9513 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9514 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9515 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9516 	} else {
9517 		sbuf_printf(sb, "                 channel 0  channel 1");
9518 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9519 		    stats.req[0], stats.req[1]);
9520 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9521 		    stats.rsp[0], stats.rsp[1]);
9522 	}
9523 
9524 	rc = sbuf_finish(sb);
9525 done:
9526 	sbuf_delete(sb);
9527 	return (rc);
9528 }
9529 
9530 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9531 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9532 {
9533 	struct adapter *sc = arg1;
9534 	struct sbuf *sb;
9535 	int rc;
9536 	struct tp_usm_stats stats;
9537 
9538 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9539 	if (sb == NULL)
9540 		return (ENOMEM);
9541 
9542 	rc = 0;
9543 	mtx_lock(&sc->reg_lock);
9544 	if (hw_off_limits(sc))
9545 		rc = ENXIO;
9546 	else
9547 		t4_get_usm_stats(sc, &stats, 1);
9548 	mtx_unlock(&sc->reg_lock);
9549 	if (rc == 0) {
9550 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
9551 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
9552 		sbuf_printf(sb, "Drops:  %u", stats.drops);
9553 		rc = sbuf_finish(sb);
9554 	}
9555 	sbuf_delete(sb);
9556 
9557 	return (rc);
9558 }
9559 
9560 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)9561 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
9562 {
9563 	struct adapter *sc = arg1;
9564 	struct sbuf *sb;
9565 	int rc;
9566 	struct tp_tid_stats stats;
9567 
9568 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9569 	if (sb == NULL)
9570 		return (ENOMEM);
9571 
9572 	rc = 0;
9573 	mtx_lock(&sc->reg_lock);
9574 	if (hw_off_limits(sc))
9575 		rc = ENXIO;
9576 	else
9577 		t4_tp_get_tid_stats(sc, &stats, 1);
9578 	mtx_unlock(&sc->reg_lock);
9579 	if (rc == 0) {
9580 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
9581 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
9582 		sbuf_printf(sb, "Active:     %u\n", stats.act);
9583 		sbuf_printf(sb, "Passive:    %u", stats.pas);
9584 		rc = sbuf_finish(sb);
9585 	}
9586 	sbuf_delete(sb);
9587 
9588 	return (rc);
9589 }
9590 
9591 static const char * const devlog_level_strings[] = {
9592 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
9593 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
9594 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
9595 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
9596 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
9597 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
9598 };
9599 
9600 static const char * const devlog_facility_strings[] = {
9601 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
9602 	[FW_DEVLOG_FACILITY_CF]		= "CF",
9603 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
9604 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
9605 	[FW_DEVLOG_FACILITY_RES]	= "RES",
9606 	[FW_DEVLOG_FACILITY_HW]		= "HW",
9607 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
9608 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
9609 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
9610 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
9611 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
9612 	[FW_DEVLOG_FACILITY_VI]		= "VI",
9613 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
9614 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
9615 	[FW_DEVLOG_FACILITY_TM]		= "TM",
9616 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
9617 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
9618 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
9619 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
9620 	[FW_DEVLOG_FACILITY_RI]		= "RI",
9621 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
9622 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
9623 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
9624 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
9625 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
9626 };
9627 
9628 static int
sbuf_devlog(struct adapter * sc,struct sbuf * sb,int flags)9629 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
9630 {
9631 	int i, j, rc, nentries, first = 0;
9632 	struct devlog_params *dparams = &sc->params.devlog;
9633 	struct fw_devlog_e *buf, *e;
9634 	uint64_t ftstamp = UINT64_MAX;
9635 
9636 	if (dparams->addr == 0)
9637 		return (ENXIO);
9638 
9639 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9640 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
9641 	if (buf == NULL)
9642 		return (ENOMEM);
9643 
9644 	mtx_lock(&sc->reg_lock);
9645 	if (hw_off_limits(sc))
9646 		rc = ENXIO;
9647 	else
9648 		rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf,
9649 		    dparams->size);
9650 	mtx_unlock(&sc->reg_lock);
9651 	if (rc != 0)
9652 		goto done;
9653 
9654 	nentries = dparams->size / sizeof(struct fw_devlog_e);
9655 	for (i = 0; i < nentries; i++) {
9656 		e = &buf[i];
9657 
9658 		if (e->timestamp == 0)
9659 			break;	/* end */
9660 
9661 		e->timestamp = be64toh(e->timestamp);
9662 		e->seqno = be32toh(e->seqno);
9663 		for (j = 0; j < 8; j++)
9664 			e->params[j] = be32toh(e->params[j]);
9665 
9666 		if (e->timestamp < ftstamp) {
9667 			ftstamp = e->timestamp;
9668 			first = i;
9669 		}
9670 	}
9671 
9672 	if (buf[first].timestamp == 0)
9673 		goto done;	/* nothing in the log */
9674 
9675 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
9676 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
9677 
9678 	i = first;
9679 	do {
9680 		e = &buf[i];
9681 		if (e->timestamp == 0)
9682 			break;	/* end */
9683 
9684 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
9685 		    e->seqno, e->timestamp,
9686 		    (e->level < nitems(devlog_level_strings) ?
9687 			devlog_level_strings[e->level] : "UNKNOWN"),
9688 		    (e->facility < nitems(devlog_facility_strings) ?
9689 			devlog_facility_strings[e->facility] : "UNKNOWN"));
9690 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
9691 		    e->params[2], e->params[3], e->params[4],
9692 		    e->params[5], e->params[6], e->params[7]);
9693 
9694 		if (++i == nentries)
9695 			i = 0;
9696 	} while (i != first);
9697 done:
9698 	free(buf, M_CXGBE);
9699 	return (rc);
9700 }
9701 
9702 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)9703 sysctl_devlog(SYSCTL_HANDLER_ARGS)
9704 {
9705 	struct adapter *sc = arg1;
9706 	int rc;
9707 	struct sbuf *sb;
9708 
9709 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9710 	if (sb == NULL)
9711 		return (ENOMEM);
9712 
9713 	rc = sbuf_devlog(sc, sb, M_WAITOK);
9714 	if (rc == 0)
9715 		rc = sbuf_finish(sb);
9716 	sbuf_delete(sb);
9717 	return (rc);
9718 }
9719 
9720 static void
dump_devlog(struct adapter * sc)9721 dump_devlog(struct adapter *sc)
9722 {
9723 	int rc;
9724 	struct sbuf sb;
9725 
9726 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9727 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
9728 		    device_get_nameunit(sc->dev));
9729 		return;
9730 	}
9731 	rc = sbuf_devlog(sc, &sb, M_WAITOK);
9732 	if (rc == 0) {
9733 		rc = sbuf_finish(&sb);
9734 		if (rc == 0) {
9735 			log(LOG_DEBUG, "%s: device log follows.\n%s",
9736 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9737 		}
9738 	}
9739 	sbuf_delete(&sb);
9740 }
9741 
9742 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)9743 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
9744 {
9745 	struct adapter *sc = arg1;
9746 	struct sbuf *sb;
9747 	int rc;
9748 	struct tp_fcoe_stats stats[MAX_NCHAN];
9749 	int i, nchan = sc->chip_params->nchan;
9750 
9751 	rc = 0;
9752 	mtx_lock(&sc->reg_lock);
9753 	if (hw_off_limits(sc))
9754 		rc = ENXIO;
9755 	else {
9756 		for (i = 0; i < nchan; i++)
9757 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
9758 	}
9759 	mtx_unlock(&sc->reg_lock);
9760 	if (rc != 0)
9761 		return (rc);
9762 
9763 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9764 	if (sb == NULL)
9765 		return (ENOMEM);
9766 
9767 	if (nchan > 2) {
9768 		sbuf_printf(sb, "                   channel 0        channel 1"
9769 		    "        channel 2        channel 3");
9770 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
9771 		    stats[0].octets_ddp, stats[1].octets_ddp,
9772 		    stats[2].octets_ddp, stats[3].octets_ddp);
9773 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
9774 		    stats[0].frames_ddp, stats[1].frames_ddp,
9775 		    stats[2].frames_ddp, stats[3].frames_ddp);
9776 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
9777 		    stats[0].frames_drop, stats[1].frames_drop,
9778 		    stats[2].frames_drop, stats[3].frames_drop);
9779 	} else {
9780 		sbuf_printf(sb, "                   channel 0        channel 1");
9781 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
9782 		    stats[0].octets_ddp, stats[1].octets_ddp);
9783 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
9784 		    stats[0].frames_ddp, stats[1].frames_ddp);
9785 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
9786 		    stats[0].frames_drop, stats[1].frames_drop);
9787 	}
9788 
9789 	rc = sbuf_finish(sb);
9790 	sbuf_delete(sb);
9791 
9792 	return (rc);
9793 }
9794 
9795 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)9796 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
9797 {
9798 	struct adapter *sc = arg1;
9799 	struct sbuf *sb;
9800 	int rc, i;
9801 	unsigned int map, kbps, ipg, mode;
9802 	unsigned int pace_tab[NTX_SCHED];
9803 
9804 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
9805 	if (sb == NULL)
9806 		return (ENOMEM);
9807 
9808 	mtx_lock(&sc->reg_lock);
9809 	if (hw_off_limits(sc)) {
9810 		mtx_unlock(&sc->reg_lock);
9811 		rc = ENXIO;
9812 		goto done;
9813 	}
9814 
9815 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
9816 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
9817 	t4_read_pace_tbl(sc, pace_tab);
9818 	mtx_unlock(&sc->reg_lock);
9819 
9820 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
9821 	    "Class IPG (0.1 ns)   Flow IPG (us)");
9822 
9823 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
9824 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
9825 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
9826 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
9827 		if (kbps)
9828 			sbuf_printf(sb, "%9u     ", kbps);
9829 		else
9830 			sbuf_printf(sb, " disabled     ");
9831 
9832 		if (ipg)
9833 			sbuf_printf(sb, "%13u        ", ipg);
9834 		else
9835 			sbuf_printf(sb, "     disabled        ");
9836 
9837 		if (pace_tab[i])
9838 			sbuf_printf(sb, "%10u", pace_tab[i]);
9839 		else
9840 			sbuf_printf(sb, "  disabled");
9841 	}
9842 	rc = sbuf_finish(sb);
9843 done:
9844 	sbuf_delete(sb);
9845 	return (rc);
9846 }
9847 
9848 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)9849 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
9850 {
9851 	struct adapter *sc = arg1;
9852 	struct sbuf *sb;
9853 	int rc, i, j;
9854 	uint64_t *p0, *p1;
9855 	struct lb_port_stats s[2];
9856 	static const char *stat_name[] = {
9857 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
9858 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
9859 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
9860 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
9861 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
9862 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
9863 		"BG2FramesTrunc:", "BG3FramesTrunc:"
9864 	};
9865 
9866 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9867 	if (sb == NULL)
9868 		return (ENOMEM);
9869 
9870 	memset(s, 0, sizeof(s));
9871 
9872 	rc = 0;
9873 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
9874 		mtx_lock(&sc->reg_lock);
9875 		if (hw_off_limits(sc))
9876 			rc = ENXIO;
9877 		else {
9878 			t4_get_lb_stats(sc, i, &s[0]);
9879 			t4_get_lb_stats(sc, i + 1, &s[1]);
9880 		}
9881 		mtx_unlock(&sc->reg_lock);
9882 		if (rc != 0)
9883 			break;
9884 
9885 		p0 = &s[0].octets;
9886 		p1 = &s[1].octets;
9887 		sbuf_printf(sb, "%s                       Loopback %u"
9888 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
9889 
9890 		for (j = 0; j < nitems(stat_name); j++)
9891 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
9892 				   *p0++, *p1++);
9893 	}
9894 
9895 	if (rc == 0)
9896 		rc = sbuf_finish(sb);
9897 	sbuf_delete(sb);
9898 
9899 	return (rc);
9900 }
9901 
9902 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)9903 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
9904 {
9905 	int rc = 0;
9906 	struct port_info *pi = arg1;
9907 	struct link_config *lc = &pi->link_cfg;
9908 	struct sbuf *sb;
9909 
9910 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
9911 	if (sb == NULL)
9912 		return (ENOMEM);
9913 
9914 	if (lc->link_ok || lc->link_down_rc == 255)
9915 		sbuf_printf(sb, "n/a");
9916 	else
9917 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
9918 
9919 	rc = sbuf_finish(sb);
9920 	sbuf_delete(sb);
9921 
9922 	return (rc);
9923 }
9924 
9925 struct mem_desc {
9926 	u_int base;
9927 	u_int limit;
9928 	u_int idx;
9929 };
9930 
9931 static int
mem_desc_cmp(const void * a,const void * b)9932 mem_desc_cmp(const void *a, const void *b)
9933 {
9934 	const u_int v1 = ((const struct mem_desc *)a)->base;
9935 	const u_int v2 = ((const struct mem_desc *)b)->base;
9936 
9937 	if (v1 < v2)
9938 		return (-1);
9939 	else if (v1 > v2)
9940 		return (1);
9941 
9942 	return (0);
9943 }
9944 
9945 static void
mem_region_show(struct sbuf * sb,const char * name,unsigned int from,unsigned int to)9946 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
9947     unsigned int to)
9948 {
9949 	unsigned int size;
9950 
9951 	if (from == to)
9952 		return;
9953 
9954 	size = to - from + 1;
9955 	if (size == 0)
9956 		return;
9957 
9958 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
9959 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
9960 }
9961 
9962 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)9963 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
9964 {
9965 	struct adapter *sc = arg1;
9966 	struct sbuf *sb;
9967 	int rc, i, n;
9968 	uint32_t lo, hi, used, free, alloc;
9969 	static const char *memory[] = {
9970 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
9971 	};
9972 	static const char *region[] = {
9973 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
9974 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
9975 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
9976 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
9977 		"RQUDP region:", "PBL region:", "TXPBL region:",
9978 		"TLSKey region:", "DBVFIFO region:", "ULPRX state:",
9979 		"ULPTX state:", "On-chip queues:",
9980 	};
9981 	struct mem_desc avail[4];
9982 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
9983 	struct mem_desc *md = mem;
9984 
9985 	rc = sysctl_wire_old_buffer(req, 0);
9986 	if (rc != 0)
9987 		return (rc);
9988 
9989 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9990 	if (sb == NULL)
9991 		return (ENOMEM);
9992 
9993 	for (i = 0; i < nitems(mem); i++) {
9994 		mem[i].limit = 0;
9995 		mem[i].idx = i;
9996 	}
9997 
9998 	mtx_lock(&sc->reg_lock);
9999 	if (hw_off_limits(sc)) {
10000 		rc = ENXIO;
10001 		goto done;
10002 	}
10003 
10004 	/* Find and sort the populated memory ranges */
10005 	i = 0;
10006 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
10007 	if (lo & F_EDRAM0_ENABLE) {
10008 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
10009 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
10010 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
10011 		avail[i].idx = 0;
10012 		i++;
10013 	}
10014 	if (lo & F_EDRAM1_ENABLE) {
10015 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
10016 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
10017 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
10018 		avail[i].idx = 1;
10019 		i++;
10020 	}
10021 	if (lo & F_EXT_MEM_ENABLE) {
10022 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
10023 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
10024 		avail[i].limit = avail[i].base + (G_EXT_MEM_SIZE(hi) << 20);
10025 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
10026 		i++;
10027 	}
10028 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
10029 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10030 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
10031 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
10032 		avail[i].idx = 4;
10033 		i++;
10034 	}
10035 	if (is_t6(sc) && lo & F_HMA_MUX) {
10036 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10037 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
10038 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
10039 		avail[i].idx = 5;
10040 		i++;
10041 	}
10042 	MPASS(i <= nitems(avail));
10043 	if (!i)                                    /* no memory available */
10044 		goto done;
10045 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
10046 
10047 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
10048 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
10049 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
10050 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
10051 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
10052 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
10053 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
10054 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
10055 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
10056 
10057 	/* the next few have explicit upper bounds */
10058 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
10059 	md->limit = md->base - 1 +
10060 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
10061 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
10062 	md++;
10063 
10064 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
10065 	md->limit = md->base - 1 +
10066 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
10067 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
10068 	md++;
10069 
10070 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10071 		if (chip_id(sc) <= CHELSIO_T5)
10072 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
10073 		else
10074 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
10075 		md->limit = 0;
10076 	} else {
10077 		md->base = 0;
10078 		md->idx = nitems(region);  /* hide it */
10079 	}
10080 	md++;
10081 
10082 #define ulp_region(reg) \
10083 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
10084 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
10085 
10086 	ulp_region(RX_ISCSI);
10087 	ulp_region(RX_TDDP);
10088 	ulp_region(TX_TPT);
10089 	ulp_region(RX_STAG);
10090 	ulp_region(RX_RQ);
10091 	ulp_region(RX_RQUDP);
10092 	ulp_region(RX_PBL);
10093 	ulp_region(TX_PBL);
10094 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
10095 		ulp_region(RX_TLS_KEY);
10096 	}
10097 #undef ulp_region
10098 
10099 	md->base = 0;
10100 	if (is_t4(sc))
10101 		md->idx = nitems(region);
10102 	else {
10103 		uint32_t size = 0;
10104 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
10105 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
10106 
10107 		if (is_t5(sc)) {
10108 			if (sge_ctrl & F_VFIFO_ENABLE)
10109 				size = fifo_size << 2;
10110 		} else
10111 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
10112 
10113 		if (size) {
10114 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
10115 			md->limit = md->base + size - 1;
10116 		} else
10117 			md->idx = nitems(region);
10118 	}
10119 	md++;
10120 
10121 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
10122 	md->limit = 0;
10123 	md++;
10124 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
10125 	md->limit = 0;
10126 	md++;
10127 
10128 	md->base = sc->vres.ocq.start;
10129 	if (sc->vres.ocq.size)
10130 		md->limit = md->base + sc->vres.ocq.size - 1;
10131 	else
10132 		md->idx = nitems(region);  /* hide it */
10133 	md++;
10134 
10135 	/* add any address-space holes, there can be up to 3 */
10136 	for (n = 0; n < i - 1; n++)
10137 		if (avail[n].limit < avail[n + 1].base)
10138 			(md++)->base = avail[n].limit;
10139 	if (avail[n].limit)
10140 		(md++)->base = avail[n].limit;
10141 
10142 	n = md - mem;
10143 	MPASS(n <= nitems(mem));
10144 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10145 
10146 	for (lo = 0; lo < i; lo++)
10147 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10148 				avail[lo].limit - 1);
10149 
10150 	sbuf_printf(sb, "\n");
10151 	for (i = 0; i < n; i++) {
10152 		if (mem[i].idx >= nitems(region))
10153 			continue;                        /* skip holes */
10154 		if (!mem[i].limit)
10155 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10156 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10157 				mem[i].limit);
10158 	}
10159 
10160 	sbuf_printf(sb, "\n");
10161 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10162 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10163 	mem_region_show(sb, "uP RAM:", lo, hi);
10164 
10165 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10166 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10167 	mem_region_show(sb, "uP Extmem2:", lo, hi);
10168 
10169 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10170 	for (i = 0, free = 0; i < 2; i++)
10171 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10172 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10173 		   G_PMRXMAXPAGE(lo), free,
10174 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
10175 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
10176 
10177 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10178 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10179 	for (i = 0, free = 0; i < 4; i++)
10180 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10181 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10182 		   G_PMTXMAXPAGE(lo), free,
10183 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10184 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
10185 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10186 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10187 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10188 
10189 	for (i = 0; i < 4; i++) {
10190 		if (chip_id(sc) > CHELSIO_T5)
10191 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10192 		else
10193 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10194 		if (is_t5(sc)) {
10195 			used = G_T5_USED(lo);
10196 			alloc = G_T5_ALLOC(lo);
10197 		} else {
10198 			used = G_USED(lo);
10199 			alloc = G_ALLOC(lo);
10200 		}
10201 		/* For T6 these are MAC buffer groups */
10202 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10203 		    i, used, alloc);
10204 	}
10205 	for (i = 0; i < sc->chip_params->nchan; i++) {
10206 		if (chip_id(sc) > CHELSIO_T5)
10207 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10208 		else
10209 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10210 		if (is_t5(sc)) {
10211 			used = G_T5_USED(lo);
10212 			alloc = G_T5_ALLOC(lo);
10213 		} else {
10214 			used = G_USED(lo);
10215 			alloc = G_ALLOC(lo);
10216 		}
10217 		/* For T6 these are MAC buffer groups */
10218 		sbuf_printf(sb,
10219 		    "\nLoopback %d using %u pages out of %u allocated",
10220 		    i, used, alloc);
10221 	}
10222 done:
10223 	mtx_unlock(&sc->reg_lock);
10224 	if (rc == 0)
10225 		rc = sbuf_finish(sb);
10226 	sbuf_delete(sb);
10227 	return (rc);
10228 }
10229 
10230 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10231 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10232 {
10233 	*mask = x | y;
10234 	y = htobe64(y);
10235 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10236 }
10237 
10238 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10239 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10240 {
10241 	struct adapter *sc = arg1;
10242 	struct sbuf *sb;
10243 	int rc, i;
10244 
10245 	MPASS(chip_id(sc) <= CHELSIO_T5);
10246 
10247 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10248 	if (sb == NULL)
10249 		return (ENOMEM);
10250 
10251 	sbuf_printf(sb,
10252 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10253 	    "  VF              Replication             P0 P1 P2 P3  ML");
10254 	rc = 0;
10255 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10256 		uint64_t tcamx, tcamy, mask;
10257 		uint32_t cls_lo, cls_hi;
10258 		uint8_t addr[ETHER_ADDR_LEN];
10259 
10260 		mtx_lock(&sc->reg_lock);
10261 		if (hw_off_limits(sc))
10262 			rc = ENXIO;
10263 		else {
10264 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10265 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10266 		}
10267 		mtx_unlock(&sc->reg_lock);
10268 		if (rc != 0)
10269 			break;
10270 		if (tcamx & tcamy)
10271 			continue;
10272 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10273 		mtx_lock(&sc->reg_lock);
10274 		if (hw_off_limits(sc))
10275 			rc = ENXIO;
10276 		else {
10277 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10278 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10279 		}
10280 		mtx_unlock(&sc->reg_lock);
10281 		if (rc != 0)
10282 			break;
10283 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10284 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10285 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10286 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10287 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10288 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10289 
10290 		if (cls_lo & F_REPLICATE) {
10291 			struct fw_ldst_cmd ldst_cmd;
10292 
10293 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10294 			ldst_cmd.op_to_addrspace =
10295 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10296 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10297 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10298 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10299 			ldst_cmd.u.mps.rplc.fid_idx =
10300 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10301 				V_FW_LDST_CMD_IDX(i));
10302 
10303 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10304 			    "t4mps");
10305 			if (rc)
10306 				break;
10307 			if (hw_off_limits(sc))
10308 				rc = ENXIO;
10309 			else
10310 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10311 				    sizeof(ldst_cmd), &ldst_cmd);
10312 			end_synchronized_op(sc, 0);
10313 			if (rc != 0)
10314 				break;
10315 			else {
10316 				sbuf_printf(sb, " %08x %08x %08x %08x",
10317 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10318 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10319 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10320 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10321 			}
10322 		} else
10323 			sbuf_printf(sb, "%36s", "");
10324 
10325 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10326 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10327 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10328 	}
10329 
10330 	if (rc)
10331 		(void) sbuf_finish(sb);
10332 	else
10333 		rc = sbuf_finish(sb);
10334 	sbuf_delete(sb);
10335 
10336 	return (rc);
10337 }
10338 
10339 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10340 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10341 {
10342 	struct adapter *sc = arg1;
10343 	struct sbuf *sb;
10344 	int rc, i;
10345 
10346 	MPASS(chip_id(sc) > CHELSIO_T5);
10347 
10348 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10349 	if (sb == NULL)
10350 		return (ENOMEM);
10351 
10352 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10353 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10354 	    "                           Replication"
10355 	    "                                    P0 P1 P2 P3  ML\n");
10356 
10357 	rc = 0;
10358 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10359 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10360 		uint16_t ivlan;
10361 		uint64_t tcamx, tcamy, val, mask;
10362 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10363 		uint8_t addr[ETHER_ADDR_LEN];
10364 
10365 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10366 		if (i < 256)
10367 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10368 		else
10369 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10370 		mtx_lock(&sc->reg_lock);
10371 		if (hw_off_limits(sc))
10372 			rc = ENXIO;
10373 		else {
10374 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10375 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10376 			tcamy = G_DMACH(val) << 32;
10377 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10378 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10379 		}
10380 		mtx_unlock(&sc->reg_lock);
10381 		if (rc != 0)
10382 			break;
10383 
10384 		lookup_type = G_DATALKPTYPE(data2);
10385 		port_num = G_DATAPORTNUM(data2);
10386 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10387 			/* Inner header VNI */
10388 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10389 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10390 			dip_hit = data2 & F_DATADIPHIT;
10391 			vlan_vld = 0;
10392 		} else {
10393 			vniy = 0;
10394 			dip_hit = 0;
10395 			vlan_vld = data2 & F_DATAVIDH2;
10396 			ivlan = G_VIDL(val);
10397 		}
10398 
10399 		ctl |= V_CTLXYBITSEL(1);
10400 		mtx_lock(&sc->reg_lock);
10401 		if (hw_off_limits(sc))
10402 			rc = ENXIO;
10403 		else {
10404 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10405 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10406 			tcamx = G_DMACH(val) << 32;
10407 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10408 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10409 		}
10410 		mtx_unlock(&sc->reg_lock);
10411 		if (rc != 0)
10412 			break;
10413 
10414 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10415 			/* Inner header VNI mask */
10416 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10417 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10418 		} else
10419 			vnix = 0;
10420 
10421 		if (tcamx & tcamy)
10422 			continue;
10423 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10424 
10425 		mtx_lock(&sc->reg_lock);
10426 		if (hw_off_limits(sc))
10427 			rc = ENXIO;
10428 		else {
10429 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10430 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10431 		}
10432 		mtx_unlock(&sc->reg_lock);
10433 		if (rc != 0)
10434 			break;
10435 
10436 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10437 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10438 			    "%012jx %06x %06x    -    -   %3c"
10439 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
10440 			    addr[1], addr[2], addr[3], addr[4], addr[5],
10441 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
10442 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10443 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10444 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10445 		} else {
10446 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10447 			    "%012jx    -       -   ", i, addr[0], addr[1],
10448 			    addr[2], addr[3], addr[4], addr[5],
10449 			    (uintmax_t)mask);
10450 
10451 			if (vlan_vld)
10452 				sbuf_printf(sb, "%4u   Y     ", ivlan);
10453 			else
10454 				sbuf_printf(sb, "  -    N     ");
10455 
10456 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
10457 			    lookup_type ? 'I' : 'O', port_num,
10458 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10459 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10460 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10461 		}
10462 
10463 
10464 		if (cls_lo & F_T6_REPLICATE) {
10465 			struct fw_ldst_cmd ldst_cmd;
10466 
10467 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10468 			ldst_cmd.op_to_addrspace =
10469 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10470 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10471 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10472 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10473 			ldst_cmd.u.mps.rplc.fid_idx =
10474 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10475 				V_FW_LDST_CMD_IDX(i));
10476 
10477 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10478 			    "t6mps");
10479 			if (rc)
10480 				break;
10481 			if (hw_off_limits(sc))
10482 				rc = ENXIO;
10483 			else
10484 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10485 				    sizeof(ldst_cmd), &ldst_cmd);
10486 			end_synchronized_op(sc, 0);
10487 			if (rc != 0)
10488 				break;
10489 			else {
10490 				sbuf_printf(sb, " %08x %08x %08x %08x"
10491 				    " %08x %08x %08x %08x",
10492 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
10493 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
10494 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
10495 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
10496 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10497 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10498 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10499 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10500 			}
10501 		} else
10502 			sbuf_printf(sb, "%72s", "");
10503 
10504 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
10505 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
10506 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
10507 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
10508 	}
10509 
10510 	if (rc)
10511 		(void) sbuf_finish(sb);
10512 	else
10513 		rc = sbuf_finish(sb);
10514 	sbuf_delete(sb);
10515 
10516 	return (rc);
10517 }
10518 
10519 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)10520 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
10521 {
10522 	struct adapter *sc = arg1;
10523 	struct sbuf *sb;
10524 	int rc;
10525 	uint16_t mtus[NMTUS];
10526 
10527 	rc = 0;
10528 	mtx_lock(&sc->reg_lock);
10529 	if (hw_off_limits(sc))
10530 		rc = ENXIO;
10531 	else
10532 		t4_read_mtu_tbl(sc, mtus, NULL);
10533 	mtx_unlock(&sc->reg_lock);
10534 	if (rc != 0)
10535 		return (rc);
10536 
10537 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10538 	if (sb == NULL)
10539 		return (ENOMEM);
10540 
10541 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
10542 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
10543 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
10544 	    mtus[14], mtus[15]);
10545 
10546 	rc = sbuf_finish(sb);
10547 	sbuf_delete(sb);
10548 
10549 	return (rc);
10550 }
10551 
10552 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)10553 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
10554 {
10555 	struct adapter *sc = arg1;
10556 	struct sbuf *sb;
10557 	int rc, i;
10558 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
10559 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
10560 	static const char *tx_stats[MAX_PM_NSTATS] = {
10561 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
10562 		"Tx FIFO wait", NULL, "Tx latency"
10563 	};
10564 	static const char *rx_stats[MAX_PM_NSTATS] = {
10565 		"Read:", "Write bypass:", "Write mem:", "Flush:",
10566 		"Rx FIFO wait", NULL, "Rx latency"
10567 	};
10568 
10569 	rc = 0;
10570 	mtx_lock(&sc->reg_lock);
10571 	if (hw_off_limits(sc))
10572 		rc = ENXIO;
10573 	else {
10574 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
10575 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
10576 	}
10577 	mtx_unlock(&sc->reg_lock);
10578 	if (rc != 0)
10579 		return (rc);
10580 
10581 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10582 	if (sb == NULL)
10583 		return (ENOMEM);
10584 
10585 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
10586 	for (i = 0; i < 4; i++) {
10587 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10588 		    tx_cyc[i]);
10589 	}
10590 
10591 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
10592 	for (i = 0; i < 4; i++) {
10593 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10594 		    rx_cyc[i]);
10595 	}
10596 
10597 	if (chip_id(sc) > CHELSIO_T5) {
10598 		sbuf_printf(sb,
10599 		    "\n              Total wait      Total occupancy");
10600 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10601 		    tx_cyc[i]);
10602 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10603 		    rx_cyc[i]);
10604 
10605 		i += 2;
10606 		MPASS(i < nitems(tx_stats));
10607 
10608 		sbuf_printf(sb,
10609 		    "\n                   Reads           Total wait");
10610 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10611 		    tx_cyc[i]);
10612 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10613 		    rx_cyc[i]);
10614 	}
10615 
10616 	rc = sbuf_finish(sb);
10617 	sbuf_delete(sb);
10618 
10619 	return (rc);
10620 }
10621 
10622 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)10623 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
10624 {
10625 	struct adapter *sc = arg1;
10626 	struct sbuf *sb;
10627 	int rc;
10628 	struct tp_rdma_stats stats;
10629 
10630 	rc = 0;
10631 	mtx_lock(&sc->reg_lock);
10632 	if (hw_off_limits(sc))
10633 		rc = ENXIO;
10634 	else
10635 		t4_tp_get_rdma_stats(sc, &stats, 0);
10636 	mtx_unlock(&sc->reg_lock);
10637 	if (rc != 0)
10638 		return (rc);
10639 
10640 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10641 	if (sb == NULL)
10642 		return (ENOMEM);
10643 
10644 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
10645 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
10646 
10647 	rc = sbuf_finish(sb);
10648 	sbuf_delete(sb);
10649 
10650 	return (rc);
10651 }
10652 
10653 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)10654 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
10655 {
10656 	struct adapter *sc = arg1;
10657 	struct sbuf *sb;
10658 	int rc;
10659 	struct tp_tcp_stats v4, v6;
10660 
10661 	rc = 0;
10662 	mtx_lock(&sc->reg_lock);
10663 	if (hw_off_limits(sc))
10664 		rc = ENXIO;
10665 	else
10666 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
10667 	mtx_unlock(&sc->reg_lock);
10668 	if (rc != 0)
10669 		return (rc);
10670 
10671 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10672 	if (sb == NULL)
10673 		return (ENOMEM);
10674 
10675 	sbuf_printf(sb,
10676 	    "                                IP                 IPv6\n");
10677 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
10678 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
10679 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
10680 	    v4.tcp_in_segs, v6.tcp_in_segs);
10681 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
10682 	    v4.tcp_out_segs, v6.tcp_out_segs);
10683 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
10684 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
10685 
10686 	rc = sbuf_finish(sb);
10687 	sbuf_delete(sb);
10688 
10689 	return (rc);
10690 }
10691 
10692 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)10693 sysctl_tids(SYSCTL_HANDLER_ARGS)
10694 {
10695 	struct adapter *sc = arg1;
10696 	struct sbuf *sb;
10697 	int rc;
10698 	uint32_t x, y;
10699 	struct tid_info *t = &sc->tids;
10700 
10701 	rc = 0;
10702 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10703 	if (sb == NULL)
10704 		return (ENOMEM);
10705 
10706 	if (t->natids) {
10707 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
10708 		    t->atids_in_use);
10709 	}
10710 
10711 	if (t->nhpftids) {
10712 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
10713 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
10714 	}
10715 
10716 	if (t->ntids) {
10717 		bool hashen = false;
10718 
10719 		mtx_lock(&sc->reg_lock);
10720 		if (hw_off_limits(sc))
10721 			rc = ENXIO;
10722 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10723 			hashen = true;
10724 			if (chip_id(sc) <= CHELSIO_T5) {
10725 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
10726 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
10727 			} else {
10728 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
10729 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
10730 			}
10731 		}
10732 		mtx_unlock(&sc->reg_lock);
10733 		if (rc != 0)
10734 			goto done;
10735 
10736 		sbuf_printf(sb, "TID range: ");
10737 		if (hashen) {
10738 			if (x)
10739 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
10740 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
10741 		} else {
10742 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
10743 			    t->ntids - 1);
10744 		}
10745 		sbuf_printf(sb, ", in use: %u\n",
10746 		    atomic_load_acq_int(&t->tids_in_use));
10747 	}
10748 
10749 	if (t->nstids) {
10750 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
10751 		    t->stid_base + t->nstids - 1, t->stids_in_use);
10752 	}
10753 
10754 	if (t->nftids) {
10755 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
10756 		    t->ftid_end, t->ftids_in_use);
10757 	}
10758 
10759 	if (t->netids) {
10760 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
10761 		    t->etid_base + t->netids - 1, t->etids_in_use);
10762 	}
10763 
10764 	mtx_lock(&sc->reg_lock);
10765 	if (hw_off_limits(sc))
10766 		rc = ENXIO;
10767 	else {
10768 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
10769 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
10770 	}
10771 	mtx_unlock(&sc->reg_lock);
10772 	if (rc != 0)
10773 		goto done;
10774 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
10775 done:
10776 	if (rc == 0)
10777 		rc = sbuf_finish(sb);
10778 	else
10779 		(void)sbuf_finish(sb);
10780 	sbuf_delete(sb);
10781 
10782 	return (rc);
10783 }
10784 
10785 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)10786 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
10787 {
10788 	struct adapter *sc = arg1;
10789 	struct sbuf *sb;
10790 	int rc;
10791 	struct tp_err_stats stats;
10792 
10793 	rc = 0;
10794 	mtx_lock(&sc->reg_lock);
10795 	if (hw_off_limits(sc))
10796 		rc = ENXIO;
10797 	else
10798 		t4_tp_get_err_stats(sc, &stats, 0);
10799 	mtx_unlock(&sc->reg_lock);
10800 	if (rc != 0)
10801 		return (rc);
10802 
10803 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10804 	if (sb == NULL)
10805 		return (ENOMEM);
10806 
10807 	if (sc->chip_params->nchan > 2) {
10808 		sbuf_printf(sb, "                 channel 0  channel 1"
10809 		    "  channel 2  channel 3\n");
10810 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
10811 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
10812 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
10813 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
10814 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
10815 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
10816 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
10817 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
10818 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
10819 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
10820 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
10821 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
10822 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
10823 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
10824 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
10825 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
10826 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
10827 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
10828 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
10829 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
10830 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
10831 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
10832 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
10833 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
10834 	} else {
10835 		sbuf_printf(sb, "                 channel 0  channel 1\n");
10836 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
10837 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
10838 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
10839 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
10840 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
10841 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
10842 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
10843 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
10844 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
10845 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
10846 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
10847 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
10848 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
10849 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
10850 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
10851 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
10852 	}
10853 
10854 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
10855 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
10856 
10857 	rc = sbuf_finish(sb);
10858 	sbuf_delete(sb);
10859 
10860 	return (rc);
10861 }
10862 
10863 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)10864 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
10865 {
10866 	struct adapter *sc = arg1;
10867 	struct sbuf *sb;
10868 	int rc;
10869 	struct tp_tnl_stats stats;
10870 
10871 	rc = 0;
10872 	mtx_lock(&sc->reg_lock);
10873 	if (hw_off_limits(sc))
10874 		rc = ENXIO;
10875 	else
10876 		t4_tp_get_tnl_stats(sc, &stats, 1);
10877 	mtx_unlock(&sc->reg_lock);
10878 	if (rc != 0)
10879 		return (rc);
10880 
10881 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10882 	if (sb == NULL)
10883 		return (ENOMEM);
10884 
10885 	if (sc->chip_params->nchan > 2) {
10886 		sbuf_printf(sb, "           channel 0  channel 1"
10887 		    "  channel 2  channel 3\n");
10888 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
10889 		    stats.out_pkt[0], stats.out_pkt[1],
10890 		    stats.out_pkt[2], stats.out_pkt[3]);
10891 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
10892 		    stats.in_pkt[0], stats.in_pkt[1],
10893 		    stats.in_pkt[2], stats.in_pkt[3]);
10894 	} else {
10895 		sbuf_printf(sb, "           channel 0  channel 1\n");
10896 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
10897 		    stats.out_pkt[0], stats.out_pkt[1]);
10898 		sbuf_printf(sb, "InPkts:   %10u %10u",
10899 		    stats.in_pkt[0], stats.in_pkt[1]);
10900 	}
10901 
10902 	rc = sbuf_finish(sb);
10903 	sbuf_delete(sb);
10904 
10905 	return (rc);
10906 }
10907 
10908 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)10909 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
10910 {
10911 	struct adapter *sc = arg1;
10912 	struct tp_params *tpp = &sc->params.tp;
10913 	u_int mask;
10914 	int rc;
10915 
10916 	mask = tpp->la_mask >> 16;
10917 	rc = sysctl_handle_int(oidp, &mask, 0, req);
10918 	if (rc != 0 || req->newptr == NULL)
10919 		return (rc);
10920 	if (mask > 0xffff)
10921 		return (EINVAL);
10922 	mtx_lock(&sc->reg_lock);
10923 	if (hw_off_limits(sc))
10924 		rc = ENXIO;
10925 	else {
10926 		tpp->la_mask = mask << 16;
10927 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
10928 		    tpp->la_mask);
10929 	}
10930 	mtx_unlock(&sc->reg_lock);
10931 
10932 	return (rc);
10933 }
10934 
10935 struct field_desc {
10936 	const char *name;
10937 	u_int start;
10938 	u_int width;
10939 };
10940 
10941 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)10942 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
10943 {
10944 	char buf[32];
10945 	int line_size = 0;
10946 
10947 	while (f->name) {
10948 		uint64_t mask = (1ULL << f->width) - 1;
10949 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
10950 		    ((uintmax_t)v >> f->start) & mask);
10951 
10952 		if (line_size + len >= 79) {
10953 			line_size = 8;
10954 			sbuf_printf(sb, "\n        ");
10955 		}
10956 		sbuf_printf(sb, "%s ", buf);
10957 		line_size += len + 1;
10958 		f++;
10959 	}
10960 	sbuf_printf(sb, "\n");
10961 }
10962 
10963 static const struct field_desc tp_la0[] = {
10964 	{ "RcfOpCodeOut", 60, 4 },
10965 	{ "State", 56, 4 },
10966 	{ "WcfState", 52, 4 },
10967 	{ "RcfOpcSrcOut", 50, 2 },
10968 	{ "CRxError", 49, 1 },
10969 	{ "ERxError", 48, 1 },
10970 	{ "SanityFailed", 47, 1 },
10971 	{ "SpuriousMsg", 46, 1 },
10972 	{ "FlushInputMsg", 45, 1 },
10973 	{ "FlushInputCpl", 44, 1 },
10974 	{ "RssUpBit", 43, 1 },
10975 	{ "RssFilterHit", 42, 1 },
10976 	{ "Tid", 32, 10 },
10977 	{ "InitTcb", 31, 1 },
10978 	{ "LineNumber", 24, 7 },
10979 	{ "Emsg", 23, 1 },
10980 	{ "EdataOut", 22, 1 },
10981 	{ "Cmsg", 21, 1 },
10982 	{ "CdataOut", 20, 1 },
10983 	{ "EreadPdu", 19, 1 },
10984 	{ "CreadPdu", 18, 1 },
10985 	{ "TunnelPkt", 17, 1 },
10986 	{ "RcfPeerFin", 16, 1 },
10987 	{ "RcfReasonOut", 12, 4 },
10988 	{ "TxCchannel", 10, 2 },
10989 	{ "RcfTxChannel", 8, 2 },
10990 	{ "RxEchannel", 6, 2 },
10991 	{ "RcfRxChannel", 5, 1 },
10992 	{ "RcfDataOutSrdy", 4, 1 },
10993 	{ "RxDvld", 3, 1 },
10994 	{ "RxOoDvld", 2, 1 },
10995 	{ "RxCongestion", 1, 1 },
10996 	{ "TxCongestion", 0, 1 },
10997 	{ NULL }
10998 };
10999 
11000 static const struct field_desc tp_la1[] = {
11001 	{ "CplCmdIn", 56, 8 },
11002 	{ "CplCmdOut", 48, 8 },
11003 	{ "ESynOut", 47, 1 },
11004 	{ "EAckOut", 46, 1 },
11005 	{ "EFinOut", 45, 1 },
11006 	{ "ERstOut", 44, 1 },
11007 	{ "SynIn", 43, 1 },
11008 	{ "AckIn", 42, 1 },
11009 	{ "FinIn", 41, 1 },
11010 	{ "RstIn", 40, 1 },
11011 	{ "DataIn", 39, 1 },
11012 	{ "DataInVld", 38, 1 },
11013 	{ "PadIn", 37, 1 },
11014 	{ "RxBufEmpty", 36, 1 },
11015 	{ "RxDdp", 35, 1 },
11016 	{ "RxFbCongestion", 34, 1 },
11017 	{ "TxFbCongestion", 33, 1 },
11018 	{ "TxPktSumSrdy", 32, 1 },
11019 	{ "RcfUlpType", 28, 4 },
11020 	{ "Eread", 27, 1 },
11021 	{ "Ebypass", 26, 1 },
11022 	{ "Esave", 25, 1 },
11023 	{ "Static0", 24, 1 },
11024 	{ "Cread", 23, 1 },
11025 	{ "Cbypass", 22, 1 },
11026 	{ "Csave", 21, 1 },
11027 	{ "CPktOut", 20, 1 },
11028 	{ "RxPagePoolFull", 18, 2 },
11029 	{ "RxLpbkPkt", 17, 1 },
11030 	{ "TxLpbkPkt", 16, 1 },
11031 	{ "RxVfValid", 15, 1 },
11032 	{ "SynLearned", 14, 1 },
11033 	{ "SetDelEntry", 13, 1 },
11034 	{ "SetInvEntry", 12, 1 },
11035 	{ "CpcmdDvld", 11, 1 },
11036 	{ "CpcmdSave", 10, 1 },
11037 	{ "RxPstructsFull", 8, 2 },
11038 	{ "EpcmdDvld", 7, 1 },
11039 	{ "EpcmdFlush", 6, 1 },
11040 	{ "EpcmdTrimPrefix", 5, 1 },
11041 	{ "EpcmdTrimPostfix", 4, 1 },
11042 	{ "ERssIp4Pkt", 3, 1 },
11043 	{ "ERssIp6Pkt", 2, 1 },
11044 	{ "ERssTcpUdpPkt", 1, 1 },
11045 	{ "ERssFceFipPkt", 0, 1 },
11046 	{ NULL }
11047 };
11048 
11049 static const struct field_desc tp_la2[] = {
11050 	{ "CplCmdIn", 56, 8 },
11051 	{ "MpsVfVld", 55, 1 },
11052 	{ "MpsPf", 52, 3 },
11053 	{ "MpsVf", 44, 8 },
11054 	{ "SynIn", 43, 1 },
11055 	{ "AckIn", 42, 1 },
11056 	{ "FinIn", 41, 1 },
11057 	{ "RstIn", 40, 1 },
11058 	{ "DataIn", 39, 1 },
11059 	{ "DataInVld", 38, 1 },
11060 	{ "PadIn", 37, 1 },
11061 	{ "RxBufEmpty", 36, 1 },
11062 	{ "RxDdp", 35, 1 },
11063 	{ "RxFbCongestion", 34, 1 },
11064 	{ "TxFbCongestion", 33, 1 },
11065 	{ "TxPktSumSrdy", 32, 1 },
11066 	{ "RcfUlpType", 28, 4 },
11067 	{ "Eread", 27, 1 },
11068 	{ "Ebypass", 26, 1 },
11069 	{ "Esave", 25, 1 },
11070 	{ "Static0", 24, 1 },
11071 	{ "Cread", 23, 1 },
11072 	{ "Cbypass", 22, 1 },
11073 	{ "Csave", 21, 1 },
11074 	{ "CPktOut", 20, 1 },
11075 	{ "RxPagePoolFull", 18, 2 },
11076 	{ "RxLpbkPkt", 17, 1 },
11077 	{ "TxLpbkPkt", 16, 1 },
11078 	{ "RxVfValid", 15, 1 },
11079 	{ "SynLearned", 14, 1 },
11080 	{ "SetDelEntry", 13, 1 },
11081 	{ "SetInvEntry", 12, 1 },
11082 	{ "CpcmdDvld", 11, 1 },
11083 	{ "CpcmdSave", 10, 1 },
11084 	{ "RxPstructsFull", 8, 2 },
11085 	{ "EpcmdDvld", 7, 1 },
11086 	{ "EpcmdFlush", 6, 1 },
11087 	{ "EpcmdTrimPrefix", 5, 1 },
11088 	{ "EpcmdTrimPostfix", 4, 1 },
11089 	{ "ERssIp4Pkt", 3, 1 },
11090 	{ "ERssIp6Pkt", 2, 1 },
11091 	{ "ERssTcpUdpPkt", 1, 1 },
11092 	{ "ERssFceFipPkt", 0, 1 },
11093 	{ NULL }
11094 };
11095 
11096 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)11097 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
11098 {
11099 
11100 	field_desc_show(sb, *p, tp_la0);
11101 }
11102 
11103 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)11104 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
11105 {
11106 
11107 	if (idx)
11108 		sbuf_printf(sb, "\n");
11109 	field_desc_show(sb, p[0], tp_la0);
11110 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11111 		field_desc_show(sb, p[1], tp_la0);
11112 }
11113 
11114 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)11115 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
11116 {
11117 
11118 	if (idx)
11119 		sbuf_printf(sb, "\n");
11120 	field_desc_show(sb, p[0], tp_la0);
11121 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11122 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
11123 }
11124 
11125 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)11126 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
11127 {
11128 	struct adapter *sc = arg1;
11129 	struct sbuf *sb;
11130 	uint64_t *buf, *p;
11131 	int rc;
11132 	u_int i, inc;
11133 	void (*show_func)(struct sbuf *, uint64_t *, int);
11134 
11135 	rc = 0;
11136 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11137 	if (sb == NULL)
11138 		return (ENOMEM);
11139 
11140 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11141 
11142 	mtx_lock(&sc->reg_lock);
11143 	if (hw_off_limits(sc))
11144 		rc = ENXIO;
11145 	else {
11146 		t4_tp_read_la(sc, buf, NULL);
11147 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11148 		case 2:
11149 			inc = 2;
11150 			show_func = tp_la_show2;
11151 			break;
11152 		case 3:
11153 			inc = 2;
11154 			show_func = tp_la_show3;
11155 			break;
11156 		default:
11157 			inc = 1;
11158 			show_func = tp_la_show;
11159 		}
11160 	}
11161 	mtx_unlock(&sc->reg_lock);
11162 	if (rc != 0)
11163 		goto done;
11164 
11165 	p = buf;
11166 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
11167 		(*show_func)(sb, p, i);
11168 	rc = sbuf_finish(sb);
11169 done:
11170 	sbuf_delete(sb);
11171 	free(buf, M_CXGBE);
11172 	return (rc);
11173 }
11174 
11175 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)11176 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
11177 {
11178 	struct adapter *sc = arg1;
11179 	struct sbuf *sb;
11180 	int rc;
11181 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
11182 
11183 	rc = 0;
11184 	mtx_lock(&sc->reg_lock);
11185 	if (hw_off_limits(sc))
11186 		rc = ENXIO;
11187 	else
11188 		t4_get_chan_txrate(sc, nrate, orate);
11189 	mtx_unlock(&sc->reg_lock);
11190 	if (rc != 0)
11191 		return (rc);
11192 
11193 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11194 	if (sb == NULL)
11195 		return (ENOMEM);
11196 
11197 	if (sc->chip_params->nchan > 2) {
11198 		sbuf_printf(sb, "              channel 0   channel 1"
11199 		    "   channel 2   channel 3\n");
11200 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
11201 		    nrate[0], nrate[1], nrate[2], nrate[3]);
11202 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
11203 		    orate[0], orate[1], orate[2], orate[3]);
11204 	} else {
11205 		sbuf_printf(sb, "              channel 0   channel 1\n");
11206 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
11207 		    nrate[0], nrate[1]);
11208 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
11209 		    orate[0], orate[1]);
11210 	}
11211 
11212 	rc = sbuf_finish(sb);
11213 	sbuf_delete(sb);
11214 
11215 	return (rc);
11216 }
11217 
11218 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)11219 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
11220 {
11221 	struct adapter *sc = arg1;
11222 	struct sbuf *sb;
11223 	uint32_t *buf, *p;
11224 	int rc, i;
11225 
11226 	rc = 0;
11227 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11228 	if (sb == NULL)
11229 		return (ENOMEM);
11230 
11231 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
11232 	    M_ZERO | M_WAITOK);
11233 
11234 	mtx_lock(&sc->reg_lock);
11235 	if (hw_off_limits(sc))
11236 		rc = ENXIO;
11237 	else
11238 		t4_ulprx_read_la(sc, buf);
11239 	mtx_unlock(&sc->reg_lock);
11240 	if (rc != 0)
11241 		goto done;
11242 
11243 	p = buf;
11244 	sbuf_printf(sb, "      Pcmd        Type   Message"
11245 	    "                Data");
11246 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
11247 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
11248 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
11249 	}
11250 	rc = sbuf_finish(sb);
11251 done:
11252 	sbuf_delete(sb);
11253 	free(buf, M_CXGBE);
11254 	return (rc);
11255 }
11256 
11257 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)11258 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
11259 {
11260 	struct adapter *sc = arg1;
11261 	struct sbuf *sb;
11262 	int rc;
11263 	uint32_t cfg, s1, s2;
11264 
11265 	MPASS(chip_id(sc) >= CHELSIO_T5);
11266 
11267 	rc = 0;
11268 	mtx_lock(&sc->reg_lock);
11269 	if (hw_off_limits(sc))
11270 		rc = ENXIO;
11271 	else {
11272 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
11273 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
11274 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
11275 	}
11276 	mtx_unlock(&sc->reg_lock);
11277 	if (rc != 0)
11278 		return (rc);
11279 
11280 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11281 	if (sb == NULL)
11282 		return (ENOMEM);
11283 
11284 	if (G_STATSOURCE_T5(cfg) == 7) {
11285 		int mode;
11286 
11287 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
11288 		if (mode == 0)
11289 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
11290 		else if (mode == 1)
11291 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
11292 		else
11293 			sbuf_printf(sb, "unknown mode %d", mode);
11294 	}
11295 	rc = sbuf_finish(sb);
11296 	sbuf_delete(sb);
11297 
11298 	return (rc);
11299 }
11300 
11301 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)11302 sysctl_cpus(SYSCTL_HANDLER_ARGS)
11303 {
11304 	struct adapter *sc = arg1;
11305 	enum cpu_sets op = arg2;
11306 	cpuset_t cpuset;
11307 	struct sbuf *sb;
11308 	int i, rc;
11309 
11310 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
11311 
11312 	CPU_ZERO(&cpuset);
11313 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
11314 	if (rc != 0)
11315 		return (rc);
11316 
11317 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11318 	if (sb == NULL)
11319 		return (ENOMEM);
11320 
11321 	CPU_FOREACH(i)
11322 		sbuf_printf(sb, "%d ", i);
11323 	rc = sbuf_finish(sb);
11324 	sbuf_delete(sb);
11325 
11326 	return (rc);
11327 }
11328 
11329 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)11330 sysctl_reset(SYSCTL_HANDLER_ARGS)
11331 {
11332 	struct adapter *sc = arg1;
11333 	u_int val;
11334 	int rc;
11335 
11336 	val = atomic_load_int(&sc->num_resets);
11337 	rc = sysctl_handle_int(oidp, &val, 0, req);
11338 	if (rc != 0 || req->newptr == NULL)
11339 		return (rc);
11340 
11341 	if (val == 0) {
11342 		/* Zero out the counter that tracks reset. */
11343 		atomic_store_int(&sc->num_resets, 0);
11344 		return (0);
11345 	}
11346 
11347 	if (val != 1)
11348 		return (EINVAL);	/* 0 or 1 are the only legal values */
11349 
11350 	if (hw_off_limits(sc))		/* harmless race */
11351 		return (EALREADY);
11352 
11353 	taskqueue_enqueue(reset_tq, &sc->reset_task);
11354 	return (0);
11355 }
11356 
11357 #ifdef TCP_OFFLOAD
11358 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)11359 sysctl_tls(SYSCTL_HANDLER_ARGS)
11360 {
11361 	struct adapter *sc = arg1;
11362 	int i, j, v, rc;
11363 	struct vi_info *vi;
11364 
11365 	v = sc->tt.tls;
11366 	rc = sysctl_handle_int(oidp, &v, 0, req);
11367 	if (rc != 0 || req->newptr == NULL)
11368 		return (rc);
11369 
11370 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11371 		return (ENOTSUP);
11372 
11373 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
11374 	if (rc)
11375 		return (rc);
11376 	if (hw_off_limits(sc))
11377 		rc = ENXIO;
11378 	else {
11379 		sc->tt.tls = !!v;
11380 		for_each_port(sc, i) {
11381 			for_each_vi(sc->port[i], j, vi) {
11382 				if (vi->flags & VI_INIT_DONE)
11383 					t4_update_fl_bufsize(vi->ifp);
11384 			}
11385 		}
11386 	}
11387 	end_synchronized_op(sc, 0);
11388 
11389 	return (rc);
11390 
11391 }
11392 
11393 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)11394 unit_conv(char *buf, size_t len, u_int val, u_int factor)
11395 {
11396 	u_int rem = val % factor;
11397 
11398 	if (rem == 0)
11399 		snprintf(buf, len, "%u", val / factor);
11400 	else {
11401 		while (rem % 10 == 0)
11402 			rem /= 10;
11403 		snprintf(buf, len, "%u.%u", val / factor, rem);
11404 	}
11405 }
11406 
11407 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)11408 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
11409 {
11410 	struct adapter *sc = arg1;
11411 	char buf[16];
11412 	u_int res, re;
11413 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11414 
11415 	mtx_lock(&sc->reg_lock);
11416 	if (hw_off_limits(sc))
11417 		res = (u_int)-1;
11418 	else
11419 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
11420 	mtx_unlock(&sc->reg_lock);
11421 	if (res == (u_int)-1)
11422 		return (ENXIO);
11423 
11424 	switch (arg2) {
11425 	case 0:
11426 		/* timer_tick */
11427 		re = G_TIMERRESOLUTION(res);
11428 		break;
11429 	case 1:
11430 		/* TCP timestamp tick */
11431 		re = G_TIMESTAMPRESOLUTION(res);
11432 		break;
11433 	case 2:
11434 		/* DACK tick */
11435 		re = G_DELAYEDACKRESOLUTION(res);
11436 		break;
11437 	default:
11438 		return (EDOOFUS);
11439 	}
11440 
11441 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
11442 
11443 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
11444 }
11445 
11446 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)11447 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
11448 {
11449 	struct adapter *sc = arg1;
11450 	int rc;
11451 	u_int dack_tmr, dack_re, v;
11452 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11453 
11454 	mtx_lock(&sc->reg_lock);
11455 	if (hw_off_limits(sc))
11456 		rc = ENXIO;
11457 	else {
11458 		rc = 0;
11459 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
11460 		    A_TP_TIMER_RESOLUTION));
11461 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
11462 	}
11463 	mtx_unlock(&sc->reg_lock);
11464 	if (rc != 0)
11465 		return (rc);
11466 
11467 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
11468 
11469 	return (sysctl_handle_int(oidp, &v, 0, req));
11470 }
11471 
11472 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)11473 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
11474 {
11475 	struct adapter *sc = arg1;
11476 	int rc, reg = arg2;
11477 	u_int tre;
11478 	u_long tp_tick_us, v;
11479 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11480 
11481 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
11482 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
11483 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
11484 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
11485 
11486 	mtx_lock(&sc->reg_lock);
11487 	if (hw_off_limits(sc))
11488 		rc = ENXIO;
11489 	else {
11490 		rc = 0;
11491 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
11492 		tp_tick_us = (cclk_ps << tre) / 1000000;
11493 		if (reg == A_TP_INIT_SRTT)
11494 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
11495 		else
11496 			v = tp_tick_us * t4_read_reg(sc, reg);
11497 	}
11498 	mtx_unlock(&sc->reg_lock);
11499 	if (rc != 0)
11500 		return (rc);
11501 	else
11502 		return (sysctl_handle_long(oidp, &v, 0, req));
11503 }
11504 
11505 /*
11506  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
11507  * passed to this function.
11508  */
11509 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)11510 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
11511 {
11512 	struct adapter *sc = arg1;
11513 	int rc, idx = arg2;
11514 	u_int v;
11515 
11516 	MPASS(idx >= 0 && idx <= 24);
11517 
11518 	mtx_lock(&sc->reg_lock);
11519 	if (hw_off_limits(sc))
11520 		rc = ENXIO;
11521 	else {
11522 		rc = 0;
11523 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
11524 	}
11525 	mtx_unlock(&sc->reg_lock);
11526 	if (rc != 0)
11527 		return (rc);
11528 	else
11529 		return (sysctl_handle_int(oidp, &v, 0, req));
11530 }
11531 
11532 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)11533 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
11534 {
11535 	struct adapter *sc = arg1;
11536 	int rc, idx = arg2;
11537 	u_int shift, v, r;
11538 
11539 	MPASS(idx >= 0 && idx < 16);
11540 
11541 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
11542 	shift = (idx & 3) << 3;
11543 	mtx_lock(&sc->reg_lock);
11544 	if (hw_off_limits(sc))
11545 		rc = ENXIO;
11546 	else {
11547 		rc = 0;
11548 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
11549 	}
11550 	mtx_unlock(&sc->reg_lock);
11551 	if (rc != 0)
11552 		return (rc);
11553 	else
11554 		return (sysctl_handle_int(oidp, &v, 0, req));
11555 }
11556 
11557 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)11558 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
11559 {
11560 	struct vi_info *vi = arg1;
11561 	struct adapter *sc = vi->adapter;
11562 	int idx, rc, i;
11563 	struct sge_ofld_rxq *ofld_rxq;
11564 	uint8_t v;
11565 
11566 	idx = vi->ofld_tmr_idx;
11567 
11568 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11569 	if (rc != 0 || req->newptr == NULL)
11570 		return (rc);
11571 
11572 	if (idx < 0 || idx >= SGE_NTIMERS)
11573 		return (EINVAL);
11574 
11575 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11576 	    "t4otmr");
11577 	if (rc)
11578 		return (rc);
11579 
11580 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
11581 	for_each_ofld_rxq(vi, i, ofld_rxq) {
11582 #ifdef atomic_store_rel_8
11583 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
11584 #else
11585 		ofld_rxq->iq.intr_params = v;
11586 #endif
11587 	}
11588 	vi->ofld_tmr_idx = idx;
11589 
11590 	end_synchronized_op(sc, LOCK_HELD);
11591 	return (0);
11592 }
11593 
11594 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)11595 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
11596 {
11597 	struct vi_info *vi = arg1;
11598 	struct adapter *sc = vi->adapter;
11599 	int idx, rc;
11600 
11601 	idx = vi->ofld_pktc_idx;
11602 
11603 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11604 	if (rc != 0 || req->newptr == NULL)
11605 		return (rc);
11606 
11607 	if (idx < -1 || idx >= SGE_NCOUNTERS)
11608 		return (EINVAL);
11609 
11610 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11611 	    "t4opktc");
11612 	if (rc)
11613 		return (rc);
11614 
11615 	if (vi->flags & VI_INIT_DONE)
11616 		rc = EBUSY; /* cannot be changed once the queues are created */
11617 	else
11618 		vi->ofld_pktc_idx = idx;
11619 
11620 	end_synchronized_op(sc, LOCK_HELD);
11621 	return (rc);
11622 }
11623 #endif
11624 
11625 static int
get_sge_context(struct adapter * sc,struct t4_sge_context * cntxt)11626 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
11627 {
11628 	int rc;
11629 
11630 	if (cntxt->cid > M_CTXTQID)
11631 		return (EINVAL);
11632 
11633 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
11634 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
11635 		return (EINVAL);
11636 
11637 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
11638 	if (rc)
11639 		return (rc);
11640 
11641 	if (hw_off_limits(sc)) {
11642 		rc = ENXIO;
11643 		goto done;
11644 	}
11645 
11646 	if (sc->flags & FW_OK) {
11647 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
11648 		    &cntxt->data[0]);
11649 		if (rc == 0)
11650 			goto done;
11651 	}
11652 
11653 	/*
11654 	 * Read via firmware failed or wasn't even attempted.  Read directly via
11655 	 * the backdoor.
11656 	 */
11657 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
11658 done:
11659 	end_synchronized_op(sc, 0);
11660 	return (rc);
11661 }
11662 
11663 static int
load_fw(struct adapter * sc,struct t4_data * fw)11664 load_fw(struct adapter *sc, struct t4_data *fw)
11665 {
11666 	int rc;
11667 	uint8_t *fw_data;
11668 
11669 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
11670 	if (rc)
11671 		return (rc);
11672 
11673 	if (hw_off_limits(sc)) {
11674 		rc = ENXIO;
11675 		goto done;
11676 	}
11677 
11678 	/*
11679 	 * The firmware, with the sole exception of the memory parity error
11680 	 * handler, runs from memory and not flash.  It is almost always safe to
11681 	 * install a new firmware on a running system.  Just set bit 1 in
11682 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
11683 	 */
11684 	if (sc->flags & FULL_INIT_DONE &&
11685 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
11686 		rc = EBUSY;
11687 		goto done;
11688 	}
11689 
11690 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
11691 
11692 	rc = copyin(fw->data, fw_data, fw->len);
11693 	if (rc == 0)
11694 		rc = -t4_load_fw(sc, fw_data, fw->len);
11695 
11696 	free(fw_data, M_CXGBE);
11697 done:
11698 	end_synchronized_op(sc, 0);
11699 	return (rc);
11700 }
11701 
11702 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)11703 load_cfg(struct adapter *sc, struct t4_data *cfg)
11704 {
11705 	int rc;
11706 	uint8_t *cfg_data = NULL;
11707 
11708 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11709 	if (rc)
11710 		return (rc);
11711 
11712 	if (hw_off_limits(sc)) {
11713 		rc = ENXIO;
11714 		goto done;
11715 	}
11716 
11717 	if (cfg->len == 0) {
11718 		/* clear */
11719 		rc = -t4_load_cfg(sc, NULL, 0);
11720 		goto done;
11721 	}
11722 
11723 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
11724 
11725 	rc = copyin(cfg->data, cfg_data, cfg->len);
11726 	if (rc == 0)
11727 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
11728 
11729 	free(cfg_data, M_CXGBE);
11730 done:
11731 	end_synchronized_op(sc, 0);
11732 	return (rc);
11733 }
11734 
11735 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)11736 load_boot(struct adapter *sc, struct t4_bootrom *br)
11737 {
11738 	int rc;
11739 	uint8_t *br_data = NULL;
11740 	u_int offset;
11741 
11742 	if (br->len > 1024 * 1024)
11743 		return (EFBIG);
11744 
11745 	if (br->pf_offset == 0) {
11746 		/* pfidx */
11747 		if (br->pfidx_addr > 7)
11748 			return (EINVAL);
11749 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
11750 		    A_PCIE_PF_EXPROM_OFST)));
11751 	} else if (br->pf_offset == 1) {
11752 		/* offset */
11753 		offset = G_OFFSET(br->pfidx_addr);
11754 	} else {
11755 		return (EINVAL);
11756 	}
11757 
11758 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
11759 	if (rc)
11760 		return (rc);
11761 
11762 	if (hw_off_limits(sc)) {
11763 		rc = ENXIO;
11764 		goto done;
11765 	}
11766 
11767 	if (br->len == 0) {
11768 		/* clear */
11769 		rc = -t4_load_boot(sc, NULL, offset, 0);
11770 		goto done;
11771 	}
11772 
11773 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
11774 
11775 	rc = copyin(br->data, br_data, br->len);
11776 	if (rc == 0)
11777 		rc = -t4_load_boot(sc, br_data, offset, br->len);
11778 
11779 	free(br_data, M_CXGBE);
11780 done:
11781 	end_synchronized_op(sc, 0);
11782 	return (rc);
11783 }
11784 
11785 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)11786 load_bootcfg(struct adapter *sc, struct t4_data *bc)
11787 {
11788 	int rc;
11789 	uint8_t *bc_data = NULL;
11790 
11791 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11792 	if (rc)
11793 		return (rc);
11794 
11795 	if (hw_off_limits(sc)) {
11796 		rc = ENXIO;
11797 		goto done;
11798 	}
11799 
11800 	if (bc->len == 0) {
11801 		/* clear */
11802 		rc = -t4_load_bootcfg(sc, NULL, 0);
11803 		goto done;
11804 	}
11805 
11806 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
11807 
11808 	rc = copyin(bc->data, bc_data, bc->len);
11809 	if (rc == 0)
11810 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
11811 
11812 	free(bc_data, M_CXGBE);
11813 done:
11814 	end_synchronized_op(sc, 0);
11815 	return (rc);
11816 }
11817 
11818 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)11819 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
11820 {
11821 	int rc;
11822 	struct cudbg_init *cudbg;
11823 	void *handle, *buf;
11824 
11825 	/* buf is large, don't block if no memory is available */
11826 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
11827 	if (buf == NULL)
11828 		return (ENOMEM);
11829 
11830 	handle = cudbg_alloc_handle();
11831 	if (handle == NULL) {
11832 		rc = ENOMEM;
11833 		goto done;
11834 	}
11835 
11836 	cudbg = cudbg_get_init(handle);
11837 	cudbg->adap = sc;
11838 	cudbg->print = (cudbg_print_cb)printf;
11839 
11840 #ifndef notyet
11841 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
11842 	    __func__, dump->wr_flash, dump->len, dump->data);
11843 #endif
11844 
11845 	if (dump->wr_flash)
11846 		cudbg->use_flash = 1;
11847 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
11848 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
11849 
11850 	rc = cudbg_collect(handle, buf, &dump->len);
11851 	if (rc != 0)
11852 		goto done;
11853 
11854 	rc = copyout(buf, dump->data, dump->len);
11855 done:
11856 	cudbg_free_handle(handle);
11857 	free(buf, M_CXGBE);
11858 	return (rc);
11859 }
11860 
11861 static void
free_offload_policy(struct t4_offload_policy * op)11862 free_offload_policy(struct t4_offload_policy *op)
11863 {
11864 	struct offload_rule *r;
11865 	int i;
11866 
11867 	if (op == NULL)
11868 		return;
11869 
11870 	r = &op->rule[0];
11871 	for (i = 0; i < op->nrules; i++, r++) {
11872 		free(r->bpf_prog.bf_insns, M_CXGBE);
11873 	}
11874 	free(op->rule, M_CXGBE);
11875 	free(op, M_CXGBE);
11876 }
11877 
11878 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)11879 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
11880 {
11881 	int i, rc, len;
11882 	struct t4_offload_policy *op, *old;
11883 	struct bpf_program *bf;
11884 	const struct offload_settings *s;
11885 	struct offload_rule *r;
11886 	void *u;
11887 
11888 	if (!is_offload(sc))
11889 		return (ENODEV);
11890 
11891 	if (uop->nrules == 0) {
11892 		/* Delete installed policies. */
11893 		op = NULL;
11894 		goto set_policy;
11895 	} else if (uop->nrules > 256) { /* arbitrary */
11896 		return (E2BIG);
11897 	}
11898 
11899 	/* Copy userspace offload policy to kernel */
11900 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
11901 	op->nrules = uop->nrules;
11902 	len = op->nrules * sizeof(struct offload_rule);
11903 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11904 	rc = copyin(uop->rule, op->rule, len);
11905 	if (rc) {
11906 		free(op->rule, M_CXGBE);
11907 		free(op, M_CXGBE);
11908 		return (rc);
11909 	}
11910 
11911 	r = &op->rule[0];
11912 	for (i = 0; i < op->nrules; i++, r++) {
11913 
11914 		/* Validate open_type */
11915 		if (r->open_type != OPEN_TYPE_LISTEN &&
11916 		    r->open_type != OPEN_TYPE_ACTIVE &&
11917 		    r->open_type != OPEN_TYPE_PASSIVE &&
11918 		    r->open_type != OPEN_TYPE_DONTCARE) {
11919 error:
11920 			/*
11921 			 * Rules 0 to i have malloc'd filters that need to be
11922 			 * freed.  Rules i+1 to nrules have userspace pointers
11923 			 * and should be left alone.
11924 			 */
11925 			op->nrules = i;
11926 			free_offload_policy(op);
11927 			return (rc);
11928 		}
11929 
11930 		/* Validate settings */
11931 		s = &r->settings;
11932 		if ((s->offload != 0 && s->offload != 1) ||
11933 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
11934 		    s->sched_class < -1 ||
11935 		    s->sched_class >= sc->params.nsched_cls) {
11936 			rc = EINVAL;
11937 			goto error;
11938 		}
11939 
11940 		bf = &r->bpf_prog;
11941 		u = bf->bf_insns;	/* userspace ptr */
11942 		bf->bf_insns = NULL;
11943 		if (bf->bf_len == 0) {
11944 			/* legal, matches everything */
11945 			continue;
11946 		}
11947 		len = bf->bf_len * sizeof(*bf->bf_insns);
11948 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11949 		rc = copyin(u, bf->bf_insns, len);
11950 		if (rc != 0)
11951 			goto error;
11952 
11953 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
11954 			rc = EINVAL;
11955 			goto error;
11956 		}
11957 	}
11958 set_policy:
11959 	rw_wlock(&sc->policy_lock);
11960 	old = sc->policy;
11961 	sc->policy = op;
11962 	rw_wunlock(&sc->policy_lock);
11963 	free_offload_policy(old);
11964 
11965 	return (0);
11966 }
11967 
11968 #define MAX_READ_BUF_SIZE (128 * 1024)
11969 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)11970 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
11971 {
11972 	uint32_t addr, remaining, n;
11973 	uint32_t *buf;
11974 	int rc;
11975 	uint8_t *dst;
11976 
11977 	mtx_lock(&sc->reg_lock);
11978 	if (hw_off_limits(sc))
11979 		rc = ENXIO;
11980 	else
11981 		rc = validate_mem_range(sc, mr->addr, mr->len);
11982 	mtx_unlock(&sc->reg_lock);
11983 	if (rc != 0)
11984 		return (rc);
11985 
11986 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
11987 	addr = mr->addr;
11988 	remaining = mr->len;
11989 	dst = (void *)mr->data;
11990 
11991 	while (remaining) {
11992 		n = min(remaining, MAX_READ_BUF_SIZE);
11993 		mtx_lock(&sc->reg_lock);
11994 		if (hw_off_limits(sc))
11995 			rc = ENXIO;
11996 		else
11997 			read_via_memwin(sc, 2, addr, buf, n);
11998 		mtx_unlock(&sc->reg_lock);
11999 		if (rc != 0)
12000 			break;
12001 
12002 		rc = copyout(buf, dst, n);
12003 		if (rc != 0)
12004 			break;
12005 
12006 		dst += n;
12007 		remaining -= n;
12008 		addr += n;
12009 	}
12010 
12011 	free(buf, M_CXGBE);
12012 	return (rc);
12013 }
12014 #undef MAX_READ_BUF_SIZE
12015 
12016 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)12017 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
12018 {
12019 	int rc;
12020 
12021 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
12022 		return (EINVAL);
12023 
12024 	if (i2cd->len > sizeof(i2cd->data))
12025 		return (EFBIG);
12026 
12027 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
12028 	if (rc)
12029 		return (rc);
12030 	if (hw_off_limits(sc))
12031 		rc = ENXIO;
12032 	else
12033 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
12034 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
12035 	end_synchronized_op(sc, 0);
12036 
12037 	return (rc);
12038 }
12039 
12040 static int
clear_stats(struct adapter * sc,u_int port_id)12041 clear_stats(struct adapter *sc, u_int port_id)
12042 {
12043 	int i, v, chan_map;
12044 	struct port_info *pi;
12045 	struct vi_info *vi;
12046 	struct sge_rxq *rxq;
12047 	struct sge_txq *txq;
12048 	struct sge_wrq *wrq;
12049 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12050 	struct sge_ofld_txq *ofld_txq;
12051 #endif
12052 #ifdef TCP_OFFLOAD
12053 	struct sge_ofld_rxq *ofld_rxq;
12054 #endif
12055 
12056 	if (port_id >= sc->params.nports)
12057 		return (EINVAL);
12058 	pi = sc->port[port_id];
12059 	if (pi == NULL)
12060 		return (EIO);
12061 
12062 	mtx_lock(&sc->reg_lock);
12063 	if (!hw_off_limits(sc)) {
12064 		/* MAC stats */
12065 		t4_clr_port_stats(sc, pi->tx_chan);
12066 		if (is_t6(sc)) {
12067 			if (pi->fcs_reg != -1)
12068 				pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12069 			else
12070 				pi->stats.rx_fcs_err = 0;
12071 		}
12072 		for_each_vi(pi, v, vi) {
12073 			if (vi->flags & VI_INIT_DONE)
12074 				t4_clr_vi_stats(sc, vi->vin);
12075 		}
12076 		chan_map = pi->rx_e_chan_map;
12077 		v = 0;	/* reuse */
12078 		while (chan_map) {
12079 			i = ffs(chan_map) - 1;
12080 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
12081 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
12082 			chan_map &= ~(1 << i);
12083 		}
12084 	}
12085 	mtx_unlock(&sc->reg_lock);
12086 	pi->tx_parse_error = 0;
12087 	pi->tnl_cong_drops = 0;
12088 
12089 	/*
12090 	 * Since this command accepts a port, clear stats for
12091 	 * all VIs on this port.
12092 	 */
12093 	for_each_vi(pi, v, vi) {
12094 		if (vi->flags & VI_INIT_DONE) {
12095 
12096 			for_each_rxq(vi, i, rxq) {
12097 #if defined(INET) || defined(INET6)
12098 				rxq->lro.lro_queued = 0;
12099 				rxq->lro.lro_flushed = 0;
12100 #endif
12101 				rxq->rxcsum = 0;
12102 				rxq->vlan_extraction = 0;
12103 				rxq->vxlan_rxcsum = 0;
12104 
12105 				rxq->fl.cl_allocated = 0;
12106 				rxq->fl.cl_recycled = 0;
12107 				rxq->fl.cl_fast_recycled = 0;
12108 			}
12109 
12110 			for_each_txq(vi, i, txq) {
12111 				txq->txcsum = 0;
12112 				txq->tso_wrs = 0;
12113 				txq->vlan_insertion = 0;
12114 				txq->imm_wrs = 0;
12115 				txq->sgl_wrs = 0;
12116 				txq->txpkt_wrs = 0;
12117 				txq->txpkts0_wrs = 0;
12118 				txq->txpkts1_wrs = 0;
12119 				txq->txpkts0_pkts = 0;
12120 				txq->txpkts1_pkts = 0;
12121 				txq->txpkts_flush = 0;
12122 				txq->raw_wrs = 0;
12123 				txq->vxlan_tso_wrs = 0;
12124 				txq->vxlan_txcsum = 0;
12125 				txq->kern_tls_records = 0;
12126 				txq->kern_tls_short = 0;
12127 				txq->kern_tls_partial = 0;
12128 				txq->kern_tls_full = 0;
12129 				txq->kern_tls_octets = 0;
12130 				txq->kern_tls_waste = 0;
12131 				txq->kern_tls_options = 0;
12132 				txq->kern_tls_header = 0;
12133 				txq->kern_tls_fin = 0;
12134 				txq->kern_tls_fin_short = 0;
12135 				txq->kern_tls_cbc = 0;
12136 				txq->kern_tls_gcm = 0;
12137 				mp_ring_reset_stats(txq->r);
12138 			}
12139 
12140 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12141 			for_each_ofld_txq(vi, i, ofld_txq) {
12142 				ofld_txq->wrq.tx_wrs_direct = 0;
12143 				ofld_txq->wrq.tx_wrs_copied = 0;
12144 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12145 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12146 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12147 				counter_u64_zero(ofld_txq->tx_aio_jobs);
12148 				counter_u64_zero(ofld_txq->tx_aio_octets);
12149 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
12150 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
12151 			}
12152 #endif
12153 #ifdef TCP_OFFLOAD
12154 			for_each_ofld_rxq(vi, i, ofld_rxq) {
12155 				ofld_rxq->fl.cl_allocated = 0;
12156 				ofld_rxq->fl.cl_recycled = 0;
12157 				ofld_rxq->fl.cl_fast_recycled = 0;
12158 				counter_u64_zero(
12159 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
12160 				counter_u64_zero(
12161 				    ofld_rxq->rx_iscsi_ddp_setup_error);
12162 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
12163 				ofld_rxq->rx_iscsi_ddp_octets = 0;
12164 				ofld_rxq->rx_iscsi_fl_pdus = 0;
12165 				ofld_rxq->rx_iscsi_fl_octets = 0;
12166 				ofld_rxq->rx_aio_ddp_jobs = 0;
12167 				ofld_rxq->rx_aio_ddp_octets = 0;
12168 				ofld_rxq->rx_toe_tls_records = 0;
12169 				ofld_rxq->rx_toe_tls_octets = 0;
12170 				ofld_rxq->rx_toe_ddp_octets = 0;
12171 				counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
12172 				counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
12173 				counter_u64_zero(ofld_rxq->ddp_buffer_free);
12174 			}
12175 #endif
12176 
12177 			if (IS_MAIN_VI(vi)) {
12178 				wrq = &sc->sge.ctrlq[pi->port_id];
12179 				wrq->tx_wrs_direct = 0;
12180 				wrq->tx_wrs_copied = 0;
12181 			}
12182 		}
12183 	}
12184 
12185 	return (0);
12186 }
12187 
12188 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12189 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12190 {
12191 #ifdef INET6
12192 	struct in6_addr in6;
12193 
12194 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12195 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
12196 		return (0);
12197 	else
12198 		return (EIO);
12199 #else
12200 	return (ENOTSUP);
12201 #endif
12202 }
12203 
12204 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)12205 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12206 {
12207 #ifdef INET6
12208 	struct in6_addr in6;
12209 
12210 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12211 	return (t4_release_clip_addr(sc, &in6));
12212 #else
12213 	return (ENOTSUP);
12214 #endif
12215 }
12216 
12217 int
t4_os_find_pci_capability(struct adapter * sc,int cap)12218 t4_os_find_pci_capability(struct adapter *sc, int cap)
12219 {
12220 	int i;
12221 
12222 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
12223 }
12224 
12225 void
t4_os_portmod_changed(struct port_info * pi)12226 t4_os_portmod_changed(struct port_info *pi)
12227 {
12228 	struct adapter *sc = pi->adapter;
12229 	struct vi_info *vi;
12230 	if_t ifp;
12231 	static const char *mod_str[] = {
12232 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM",
12233 		"LR_SIMPLEX", "DR"
12234 	};
12235 
12236 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
12237 	    ("%s: port_type %u", __func__, pi->port_type));
12238 
12239 	vi = &pi->vi[0];
12240 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
12241 		PORT_LOCK(pi);
12242 		build_medialist(pi);
12243 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
12244 			fixup_link_config(pi);
12245 			apply_link_config(pi);
12246 		}
12247 		PORT_UNLOCK(pi);
12248 		end_synchronized_op(sc, LOCK_HELD);
12249 	}
12250 
12251 	ifp = vi->ifp;
12252 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
12253 		if_printf(ifp, "transceiver unplugged.\n");
12254 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
12255 		if_printf(ifp, "unknown transceiver inserted.\n");
12256 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
12257 		if_printf(ifp, "unsupported transceiver inserted.\n");
12258 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
12259 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
12260 		    port_top_speed(pi), mod_str[pi->mod_type]);
12261 	} else {
12262 		if_printf(ifp, "transceiver (type %d) inserted.\n",
12263 		    pi->mod_type);
12264 	}
12265 }
12266 
12267 void
t4_os_link_changed(struct port_info * pi)12268 t4_os_link_changed(struct port_info *pi)
12269 {
12270 	struct vi_info *vi;
12271 	if_t ifp;
12272 	struct link_config *lc = &pi->link_cfg;
12273 	struct adapter *sc = pi->adapter;
12274 	int v;
12275 
12276 	PORT_LOCK_ASSERT_OWNED(pi);
12277 
12278 	if (is_t6(sc)) {
12279 		if (lc->link_ok) {
12280 			if (lc->speed > 25000 ||
12281 			    (lc->speed == 25000 && lc->fec == FEC_RS)) {
12282 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12283 				    A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS);
12284 			} else {
12285 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12286 				    A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS);
12287 			}
12288 			pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12289 			pi->stats.rx_fcs_err = 0;
12290 		} else {
12291 			pi->fcs_reg = -1;
12292 		}
12293 	} else {
12294 		MPASS(pi->fcs_reg != -1);
12295 		MPASS(pi->fcs_base == 0);
12296 	}
12297 
12298 	for_each_vi(pi, v, vi) {
12299 		ifp = vi->ifp;
12300 		if (ifp == NULL || IS_DETACHING(vi))
12301 			continue;
12302 
12303 		if (lc->link_ok) {
12304 			if_setbaudrate(ifp, IF_Mbps(lc->speed));
12305 			if_link_state_change(ifp, LINK_STATE_UP);
12306 		} else {
12307 			if_link_state_change(ifp, LINK_STATE_DOWN);
12308 		}
12309 	}
12310 }
12311 
12312 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)12313 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
12314 {
12315 	struct adapter *sc;
12316 
12317 	sx_slock(&t4_list_lock);
12318 	SLIST_FOREACH(sc, &t4_list, link) {
12319 		/*
12320 		 * func should not make any assumptions about what state sc is
12321 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
12322 		 */
12323 		func(sc, arg);
12324 	}
12325 	sx_sunlock(&t4_list_lock);
12326 }
12327 
12328 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)12329 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
12330     struct thread *td)
12331 {
12332 	int rc;
12333 	struct adapter *sc = dev->si_drv1;
12334 
12335 	rc = priv_check(td, PRIV_DRIVER);
12336 	if (rc != 0)
12337 		return (rc);
12338 
12339 	switch (cmd) {
12340 	case CHELSIO_T4_GETREG: {
12341 		struct t4_reg *edata = (struct t4_reg *)data;
12342 
12343 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12344 			return (EFAULT);
12345 
12346 		mtx_lock(&sc->reg_lock);
12347 		if (hw_off_limits(sc))
12348 			rc = ENXIO;
12349 		else if (edata->size == 4)
12350 			edata->val = t4_read_reg(sc, edata->addr);
12351 		else if (edata->size == 8)
12352 			edata->val = t4_read_reg64(sc, edata->addr);
12353 		else
12354 			rc = EINVAL;
12355 		mtx_unlock(&sc->reg_lock);
12356 
12357 		break;
12358 	}
12359 	case CHELSIO_T4_SETREG: {
12360 		struct t4_reg *edata = (struct t4_reg *)data;
12361 
12362 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12363 			return (EFAULT);
12364 
12365 		mtx_lock(&sc->reg_lock);
12366 		if (hw_off_limits(sc))
12367 			rc = ENXIO;
12368 		else if (edata->size == 4) {
12369 			if (edata->val & 0xffffffff00000000)
12370 				rc = EINVAL;
12371 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
12372 		} else if (edata->size == 8)
12373 			t4_write_reg64(sc, edata->addr, edata->val);
12374 		else
12375 			rc = EINVAL;
12376 		mtx_unlock(&sc->reg_lock);
12377 
12378 		break;
12379 	}
12380 	case CHELSIO_T4_REGDUMP: {
12381 		struct t4_regdump *regs = (struct t4_regdump *)data;
12382 		int reglen = t4_get_regs_len(sc);
12383 		uint8_t *buf;
12384 
12385 		if (regs->len < reglen) {
12386 			regs->len = reglen; /* hint to the caller */
12387 			return (ENOBUFS);
12388 		}
12389 
12390 		regs->len = reglen;
12391 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
12392 		mtx_lock(&sc->reg_lock);
12393 		if (hw_off_limits(sc))
12394 			rc = ENXIO;
12395 		else
12396 			get_regs(sc, regs, buf);
12397 		mtx_unlock(&sc->reg_lock);
12398 		if (rc == 0)
12399 			rc = copyout(buf, regs->data, reglen);
12400 		free(buf, M_CXGBE);
12401 		break;
12402 	}
12403 	case CHELSIO_T4_GET_FILTER_MODE:
12404 		rc = get_filter_mode(sc, (uint32_t *)data);
12405 		break;
12406 	case CHELSIO_T4_SET_FILTER_MODE:
12407 		rc = set_filter_mode(sc, *(uint32_t *)data);
12408 		break;
12409 	case CHELSIO_T4_SET_FILTER_MASK:
12410 		rc = set_filter_mask(sc, *(uint32_t *)data);
12411 		break;
12412 	case CHELSIO_T4_GET_FILTER:
12413 		rc = get_filter(sc, (struct t4_filter *)data);
12414 		break;
12415 	case CHELSIO_T4_SET_FILTER:
12416 		rc = set_filter(sc, (struct t4_filter *)data);
12417 		break;
12418 	case CHELSIO_T4_DEL_FILTER:
12419 		rc = del_filter(sc, (struct t4_filter *)data);
12420 		break;
12421 	case CHELSIO_T4_GET_SGE_CONTEXT:
12422 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
12423 		break;
12424 	case CHELSIO_T4_LOAD_FW:
12425 		rc = load_fw(sc, (struct t4_data *)data);
12426 		break;
12427 	case CHELSIO_T4_GET_MEM:
12428 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
12429 		break;
12430 	case CHELSIO_T4_GET_I2C:
12431 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
12432 		break;
12433 	case CHELSIO_T4_CLEAR_STATS:
12434 		rc = clear_stats(sc, *(uint32_t *)data);
12435 		break;
12436 	case CHELSIO_T4_SCHED_CLASS:
12437 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
12438 		break;
12439 	case CHELSIO_T4_SCHED_QUEUE:
12440 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
12441 		break;
12442 	case CHELSIO_T4_GET_TRACER:
12443 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
12444 		break;
12445 	case CHELSIO_T4_SET_TRACER:
12446 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
12447 		break;
12448 	case CHELSIO_T4_LOAD_CFG:
12449 		rc = load_cfg(sc, (struct t4_data *)data);
12450 		break;
12451 	case CHELSIO_T4_LOAD_BOOT:
12452 		rc = load_boot(sc, (struct t4_bootrom *)data);
12453 		break;
12454 	case CHELSIO_T4_LOAD_BOOTCFG:
12455 		rc = load_bootcfg(sc, (struct t4_data *)data);
12456 		break;
12457 	case CHELSIO_T4_CUDBG_DUMP:
12458 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
12459 		break;
12460 	case CHELSIO_T4_SET_OFLD_POLICY:
12461 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
12462 		break;
12463 	case CHELSIO_T4_HOLD_CLIP_ADDR:
12464 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
12465 		break;
12466 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
12467 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
12468 		break;
12469 	default:
12470 		rc = ENOTTY;
12471 	}
12472 
12473 	return (rc);
12474 }
12475 
12476 #ifdef TCP_OFFLOAD
12477 int
toe_capability(struct vi_info * vi,bool enable)12478 toe_capability(struct vi_info *vi, bool enable)
12479 {
12480 	int rc;
12481 	struct port_info *pi = vi->pi;
12482 	struct adapter *sc = pi->adapter;
12483 
12484 	ASSERT_SYNCHRONIZED_OP(sc);
12485 
12486 	if (!is_offload(sc))
12487 		return (ENODEV);
12488 	if (!hw_all_ok(sc))
12489 		return (ENXIO);
12490 
12491 	if (enable) {
12492 #ifdef KERN_TLS
12493 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
12494 			int i, j, n;
12495 			struct port_info *p;
12496 			struct vi_info *v;
12497 
12498 			/*
12499 			 * Reconfigure hardware for TOE if TXTLS is not enabled
12500 			 * on any ifnet.
12501 			 */
12502 			n = 0;
12503 			for_each_port(sc, i) {
12504 				p = sc->port[i];
12505 				for_each_vi(p, j, v) {
12506 					if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
12507 						CH_WARN(sc,
12508 						    "%s has NIC TLS enabled.\n",
12509 						    device_get_nameunit(v->dev));
12510 						n++;
12511 					}
12512 				}
12513 			}
12514 			if (n > 0) {
12515 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
12516 				    "associated with this adapter before "
12517 				    "trying to enable TOE.\n");
12518 				return (EAGAIN);
12519 			}
12520 			rc = t6_config_kern_tls(sc, false);
12521 			if (rc)
12522 				return (rc);
12523 		}
12524 #endif
12525 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
12526 			/* TOE is already enabled. */
12527 			return (0);
12528 		}
12529 
12530 		/*
12531 		 * We need the port's queues around so that we're able to send
12532 		 * and receive CPLs to/from the TOE even if the ifnet for this
12533 		 * port has never been UP'd administratively.
12534 		 */
12535 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
12536 			return (rc);
12537 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
12538 		    ((rc = vi_init(&pi->vi[0])) != 0))
12539 			return (rc);
12540 
12541 		if (isset(&sc->offload_map, pi->port_id)) {
12542 			/* TOE is enabled on another VI of this port. */
12543 			MPASS(pi->uld_vis > 0);
12544 			pi->uld_vis++;
12545 			return (0);
12546 		}
12547 
12548 		if (!uld_active(sc, ULD_TOM)) {
12549 			rc = t4_activate_uld(sc, ULD_TOM);
12550 			if (rc == EAGAIN) {
12551 				log(LOG_WARNING,
12552 				    "You must kldload t4_tom.ko before trying "
12553 				    "to enable TOE on a cxgbe interface.\n");
12554 			}
12555 			if (rc != 0)
12556 				return (rc);
12557 			KASSERT(sc->tom_softc != NULL,
12558 			    ("%s: TOM activated but softc NULL", __func__));
12559 			KASSERT(uld_active(sc, ULD_TOM),
12560 			    ("%s: TOM activated but flag not set", __func__));
12561 		}
12562 
12563 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
12564 		if (!uld_active(sc, ULD_IWARP))
12565 			(void) t4_activate_uld(sc, ULD_IWARP);
12566 		if (!uld_active(sc, ULD_ISCSI))
12567 			(void) t4_activate_uld(sc, ULD_ISCSI);
12568 
12569 		if (pi->uld_vis++ == 0)
12570 			setbit(&sc->offload_map, pi->port_id);
12571 	} else {
12572 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) == 0) {
12573 			/* TOE is already disabled. */
12574 			return (0);
12575 		}
12576 		MPASS(isset(&sc->offload_map, pi->port_id));
12577 		MPASS(pi->uld_vis > 0);
12578 		if (--pi->uld_vis == 0)
12579 			clrbit(&sc->offload_map, pi->port_id);
12580 	}
12581 
12582 	return (0);
12583 }
12584 
12585 /*
12586  * Add an upper layer driver to the global list.
12587  */
12588 int
t4_register_uld(struct uld_info * ui,int id)12589 t4_register_uld(struct uld_info *ui, int id)
12590 {
12591 	int rc;
12592 
12593 	if (id < 0 || id > ULD_MAX)
12594 		return (EINVAL);
12595 	sx_xlock(&t4_uld_list_lock);
12596 	if (t4_uld_list[id] != NULL)
12597 		rc = EEXIST;
12598 	else {
12599 		t4_uld_list[id] = ui;
12600 		rc = 0;
12601 	}
12602 	sx_xunlock(&t4_uld_list_lock);
12603 	return (rc);
12604 }
12605 
12606 int
t4_unregister_uld(struct uld_info * ui,int id)12607 t4_unregister_uld(struct uld_info *ui, int id)
12608 {
12609 
12610 	if (id < 0 || id > ULD_MAX)
12611 		return (EINVAL);
12612 	sx_xlock(&t4_uld_list_lock);
12613 	MPASS(t4_uld_list[id] == ui);
12614 	t4_uld_list[id] = NULL;
12615 	sx_xunlock(&t4_uld_list_lock);
12616 	return (0);
12617 }
12618 
12619 int
t4_activate_uld(struct adapter * sc,int id)12620 t4_activate_uld(struct adapter *sc, int id)
12621 {
12622 	int rc;
12623 
12624 	ASSERT_SYNCHRONIZED_OP(sc);
12625 
12626 	if (id < 0 || id > ULD_MAX)
12627 		return (EINVAL);
12628 
12629 	/* Adapter needs to be initialized before any ULD can be activated. */
12630 	if (!(sc->flags & FULL_INIT_DONE)) {
12631 		rc = adapter_init(sc);
12632 		if (rc != 0)
12633 			return (rc);
12634 	}
12635 
12636 	sx_slock(&t4_uld_list_lock);
12637 	if (t4_uld_list[id] == NULL)
12638 		rc = EAGAIN;	/* load the KLD with this ULD and try again. */
12639 	else {
12640 		rc = t4_uld_list[id]->uld_activate(sc);
12641 		if (rc == 0)
12642 			setbit(&sc->active_ulds, id);
12643 	}
12644 	sx_sunlock(&t4_uld_list_lock);
12645 
12646 	return (rc);
12647 }
12648 
12649 int
t4_deactivate_uld(struct adapter * sc,int id)12650 t4_deactivate_uld(struct adapter *sc, int id)
12651 {
12652 	int rc;
12653 
12654 	ASSERT_SYNCHRONIZED_OP(sc);
12655 
12656 	if (id < 0 || id > ULD_MAX)
12657 		return (EINVAL);
12658 
12659 	sx_slock(&t4_uld_list_lock);
12660 	if (t4_uld_list[id] == NULL)
12661 		rc = ENXIO;
12662 	else {
12663 		rc = t4_uld_list[id]->uld_deactivate(sc);
12664 		if (rc == 0)
12665 			clrbit(&sc->active_ulds, id);
12666 	}
12667 	sx_sunlock(&t4_uld_list_lock);
12668 
12669 	return (rc);
12670 }
12671 
12672 static int
deactivate_all_uld(struct adapter * sc)12673 deactivate_all_uld(struct adapter *sc)
12674 {
12675 	int i, rc;
12676 
12677 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
12678 	if (rc != 0)
12679 		return (ENXIO);
12680 	sx_slock(&t4_uld_list_lock);
12681 	for (i = 0; i <= ULD_MAX; i++) {
12682 		if (t4_uld_list[i] == NULL || !uld_active(sc, i))
12683 			continue;
12684 		rc = t4_uld_list[i]->uld_deactivate(sc);
12685 		if (rc != 0)
12686 			break;
12687 		clrbit(&sc->active_ulds, i);
12688 	}
12689 	sx_sunlock(&t4_uld_list_lock);
12690 	end_synchronized_op(sc, 0);
12691 
12692 	return (rc);
12693 }
12694 
12695 static void
stop_all_uld(struct adapter * sc)12696 stop_all_uld(struct adapter *sc)
12697 {
12698 	int i;
12699 
12700 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
12701 		return;
12702 	sx_slock(&t4_uld_list_lock);
12703 	for (i = 0; i <= ULD_MAX; i++) {
12704 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12705 		    t4_uld_list[i]->uld_stop == NULL)
12706 			continue;
12707 		(void) t4_uld_list[i]->uld_stop(sc);
12708 	}
12709 	sx_sunlock(&t4_uld_list_lock);
12710 	end_synchronized_op(sc, 0);
12711 }
12712 
12713 static void
restart_all_uld(struct adapter * sc)12714 restart_all_uld(struct adapter *sc)
12715 {
12716 	int i;
12717 
12718 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
12719 		return;
12720 	sx_slock(&t4_uld_list_lock);
12721 	for (i = 0; i <= ULD_MAX; i++) {
12722 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12723 		    t4_uld_list[i]->uld_restart == NULL)
12724 			continue;
12725 		(void) t4_uld_list[i]->uld_restart(sc);
12726 	}
12727 	sx_sunlock(&t4_uld_list_lock);
12728 	end_synchronized_op(sc, 0);
12729 }
12730 
12731 int
uld_active(struct adapter * sc,int id)12732 uld_active(struct adapter *sc, int id)
12733 {
12734 
12735 	MPASS(id >= 0 && id <= ULD_MAX);
12736 
12737 	return (isset(&sc->active_ulds, id));
12738 }
12739 #endif
12740 
12741 #ifdef KERN_TLS
12742 static int
ktls_capability(struct adapter * sc,bool enable)12743 ktls_capability(struct adapter *sc, bool enable)
12744 {
12745 	ASSERT_SYNCHRONIZED_OP(sc);
12746 
12747 	if (!is_ktls(sc))
12748 		return (ENODEV);
12749 	if (!is_t6(sc))
12750 		return (0);
12751 	if (!hw_all_ok(sc))
12752 		return (ENXIO);
12753 
12754 	if (enable) {
12755 		if (sc->flags & KERN_TLS_ON)
12756 			return (0);	/* already on */
12757 		if (sc->offload_map != 0) {
12758 			CH_WARN(sc,
12759 			    "Disable TOE on all interfaces associated with "
12760 			    "this adapter before trying to enable NIC TLS.\n");
12761 			return (EAGAIN);
12762 		}
12763 		return (t6_config_kern_tls(sc, true));
12764 	} else {
12765 		/*
12766 		 * Nothing to do for disable.  If TOE is enabled sometime later
12767 		 * then toe_capability will reconfigure the hardware.
12768 		 */
12769 		return (0);
12770 	}
12771 }
12772 #endif
12773 
12774 /*
12775  * t  = ptr to tunable.
12776  * nc = number of CPUs.
12777  * c  = compiled in default for that tunable.
12778  */
12779 static void
calculate_nqueues(int * t,int nc,const int c)12780 calculate_nqueues(int *t, int nc, const int c)
12781 {
12782 	int nq;
12783 
12784 	if (*t > 0)
12785 		return;
12786 	nq = *t < 0 ? -*t : c;
12787 	*t = min(nc, nq);
12788 }
12789 
12790 /*
12791  * Come up with reasonable defaults for some of the tunables, provided they're
12792  * not set by the user (in which case we'll use the values as is).
12793  */
12794 static void
tweak_tunables(void)12795 tweak_tunables(void)
12796 {
12797 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
12798 
12799 	if (t4_ntxq < 1) {
12800 #ifdef RSS
12801 		t4_ntxq = rss_getnumbuckets();
12802 #else
12803 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
12804 #endif
12805 	}
12806 
12807 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
12808 
12809 	if (t4_nrxq < 1) {
12810 #ifdef RSS
12811 		t4_nrxq = rss_getnumbuckets();
12812 #else
12813 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
12814 #endif
12815 	}
12816 
12817 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
12818 
12819 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12820 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
12821 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
12822 #endif
12823 #ifdef TCP_OFFLOAD
12824 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
12825 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
12826 #endif
12827 
12828 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
12829 	if (t4_toecaps_allowed == -1)
12830 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
12831 #else
12832 	if (t4_toecaps_allowed == -1)
12833 		t4_toecaps_allowed = 0;
12834 #endif
12835 
12836 #ifdef TCP_OFFLOAD
12837 	if (t4_rdmacaps_allowed == -1) {
12838 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
12839 		    FW_CAPS_CONFIG_RDMA_RDMAC;
12840 	}
12841 
12842 	if (t4_iscsicaps_allowed == -1) {
12843 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
12844 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
12845 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
12846 	}
12847 
12848 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
12849 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
12850 
12851 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
12852 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
12853 #else
12854 	if (t4_rdmacaps_allowed == -1)
12855 		t4_rdmacaps_allowed = 0;
12856 
12857 	if (t4_iscsicaps_allowed == -1)
12858 		t4_iscsicaps_allowed = 0;
12859 #endif
12860 
12861 #ifdef DEV_NETMAP
12862 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
12863 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
12864 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
12865 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
12866 #endif
12867 
12868 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
12869 		t4_tmr_idx = TMR_IDX;
12870 
12871 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
12872 		t4_pktc_idx = PKTC_IDX;
12873 
12874 	if (t4_qsize_txq < 128)
12875 		t4_qsize_txq = 128;
12876 
12877 	if (t4_qsize_rxq < 128)
12878 		t4_qsize_rxq = 128;
12879 	while (t4_qsize_rxq & 7)
12880 		t4_qsize_rxq++;
12881 
12882 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
12883 
12884 	/*
12885 	 * Number of VIs to create per-port.  The first VI is the "main" regular
12886 	 * VI for the port.  The rest are additional virtual interfaces on the
12887 	 * same physical port.  Note that the main VI does not have native
12888 	 * netmap support but the extra VIs do.
12889 	 *
12890 	 * Limit the number of VIs per port to the number of available
12891 	 * MAC addresses per port.
12892 	 */
12893 	if (t4_num_vis < 1)
12894 		t4_num_vis = 1;
12895 	if (t4_num_vis > nitems(vi_mac_funcs)) {
12896 		t4_num_vis = nitems(vi_mac_funcs);
12897 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
12898 	}
12899 
12900 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
12901 		pcie_relaxed_ordering = 1;
12902 #if defined(__i386__) || defined(__amd64__)
12903 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
12904 			pcie_relaxed_ordering = 0;
12905 #endif
12906 	}
12907 }
12908 
12909 #ifdef DDB
12910 static void
t4_dump_mem(struct adapter * sc,u_int addr,u_int len)12911 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
12912 {
12913 	uint32_t base, j, off, pf, reg, save, win_pos;
12914 
12915 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
12916 	save = t4_read_reg(sc, reg);
12917 	base = sc->memwin[2].mw_base;
12918 
12919 	if (is_t4(sc)) {
12920 		pf = 0;
12921 		win_pos = addr & ~0xf;	/* start must be 16B aligned */
12922 	} else {
12923 		pf = V_PFNUM(sc->pf);
12924 		win_pos = addr & ~0x7f;	/* start must be 128B aligned */
12925 	}
12926 	off = addr - win_pos;
12927 	t4_write_reg(sc, reg, win_pos | pf);
12928 	t4_read_reg(sc, reg);
12929 
12930 	while (len > 0 && !db_pager_quit) {
12931 		uint32_t buf[8];
12932 		for (j = 0; j < 8; j++, off += 4)
12933 			buf[j] = htonl(t4_read_reg(sc, base + off));
12934 
12935 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
12936 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12937 		    buf[7]);
12938 		if (len <= sizeof(buf))
12939 			len = 0;
12940 		else
12941 			len -= sizeof(buf);
12942 	}
12943 
12944 	t4_write_reg(sc, reg, save);
12945 	t4_read_reg(sc, reg);
12946 }
12947 
12948 static void
t4_dump_tcb(struct adapter * sc,int tid)12949 t4_dump_tcb(struct adapter *sc, int tid)
12950 {
12951 	uint32_t tcb_addr;
12952 
12953 	/* Dump TCB for the tid */
12954 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
12955 	tcb_addr += tid * TCB_SIZE;
12956 	t4_dump_mem(sc, tcb_addr, TCB_SIZE);
12957 }
12958 
12959 static void
t4_dump_devlog(struct adapter * sc)12960 t4_dump_devlog(struct adapter *sc)
12961 {
12962 	struct devlog_params *dparams = &sc->params.devlog;
12963 	struct fw_devlog_e e;
12964 	int i, first, j, m, nentries, rc;
12965 	uint64_t ftstamp = UINT64_MAX;
12966 
12967 	if (dparams->start == 0) {
12968 		db_printf("devlog params not valid\n");
12969 		return;
12970 	}
12971 
12972 	nentries = dparams->size / sizeof(struct fw_devlog_e);
12973 	m = fwmtype_to_hwmtype(dparams->memtype);
12974 
12975 	/* Find the first entry. */
12976 	first = -1;
12977 	for (i = 0; i < nentries && !db_pager_quit; i++) {
12978 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12979 		    sizeof(e), (void *)&e);
12980 		if (rc != 0)
12981 			break;
12982 
12983 		if (e.timestamp == 0)
12984 			break;
12985 
12986 		e.timestamp = be64toh(e.timestamp);
12987 		if (e.timestamp < ftstamp) {
12988 			ftstamp = e.timestamp;
12989 			first = i;
12990 		}
12991 	}
12992 
12993 	if (first == -1)
12994 		return;
12995 
12996 	i = first;
12997 	do {
12998 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12999 		    sizeof(e), (void *)&e);
13000 		if (rc != 0)
13001 			return;
13002 
13003 		if (e.timestamp == 0)
13004 			return;
13005 
13006 		e.timestamp = be64toh(e.timestamp);
13007 		e.seqno = be32toh(e.seqno);
13008 		for (j = 0; j < 8; j++)
13009 			e.params[j] = be32toh(e.params[j]);
13010 
13011 		db_printf("%10d  %15ju  %8s  %8s  ",
13012 		    e.seqno, e.timestamp,
13013 		    (e.level < nitems(devlog_level_strings) ?
13014 			devlog_level_strings[e.level] : "UNKNOWN"),
13015 		    (e.facility < nitems(devlog_facility_strings) ?
13016 			devlog_facility_strings[e.facility] : "UNKNOWN"));
13017 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
13018 		    e.params[3], e.params[4], e.params[5], e.params[6],
13019 		    e.params[7]);
13020 
13021 		if (++i == nentries)
13022 			i = 0;
13023 	} while (i != first && !db_pager_quit);
13024 }
13025 
13026 static DB_DEFINE_TABLE(show, t4, show_t4);
13027 
DB_TABLE_COMMAND_FLAGS(show_t4,devlog,db_show_devlog,CS_OWN)13028 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
13029 {
13030 	device_t dev;
13031 	int t;
13032 	bool valid;
13033 
13034 	valid = false;
13035 	t = db_read_token();
13036 	if (t == tIDENT) {
13037 		dev = device_lookup_by_name(db_tok_string);
13038 		valid = true;
13039 	}
13040 	db_skip_to_eol();
13041 	if (!valid) {
13042 		db_printf("usage: show t4 devlog <nexus>\n");
13043 		return;
13044 	}
13045 
13046 	if (dev == NULL) {
13047 		db_printf("device not found\n");
13048 		return;
13049 	}
13050 
13051 	t4_dump_devlog(device_get_softc(dev));
13052 }
13053 
DB_TABLE_COMMAND_FLAGS(show_t4,tcb,db_show_t4tcb,CS_OWN)13054 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
13055 {
13056 	device_t dev;
13057 	int radix, tid, t;
13058 	bool valid;
13059 
13060 	valid = false;
13061 	radix = db_radix;
13062 	db_radix = 10;
13063 	t = db_read_token();
13064 	if (t == tIDENT) {
13065 		dev = device_lookup_by_name(db_tok_string);
13066 		t = db_read_token();
13067 		if (t == tNUMBER) {
13068 			tid = db_tok_number;
13069 			valid = true;
13070 		}
13071 	}
13072 	db_radix = radix;
13073 	db_skip_to_eol();
13074 	if (!valid) {
13075 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
13076 		return;
13077 	}
13078 
13079 	if (dev == NULL) {
13080 		db_printf("device not found\n");
13081 		return;
13082 	}
13083 	if (tid < 0) {
13084 		db_printf("invalid tid\n");
13085 		return;
13086 	}
13087 
13088 	t4_dump_tcb(device_get_softc(dev), tid);
13089 }
13090 
DB_TABLE_COMMAND_FLAGS(show_t4,memdump,db_show_memdump,CS_OWN)13091 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
13092 {
13093 	device_t dev;
13094 	int radix, t;
13095 	bool valid;
13096 
13097 	valid = false;
13098 	radix = db_radix;
13099 	db_radix = 10;
13100 	t = db_read_token();
13101 	if (t == tIDENT) {
13102 		dev = device_lookup_by_name(db_tok_string);
13103 		t = db_read_token();
13104 		if (t == tNUMBER) {
13105 			addr = db_tok_number;
13106 			t = db_read_token();
13107 			if (t == tNUMBER) {
13108 				count = db_tok_number;
13109 				valid = true;
13110 			}
13111 		}
13112 	}
13113 	db_radix = radix;
13114 	db_skip_to_eol();
13115 	if (!valid) {
13116 		db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
13117 		return;
13118 	}
13119 
13120 	if (dev == NULL) {
13121 		db_printf("device not found\n");
13122 		return;
13123 	}
13124 	if (addr < 0) {
13125 		db_printf("invalid address\n");
13126 		return;
13127 	}
13128 	if (count <= 0) {
13129 		db_printf("invalid length\n");
13130 		return;
13131 	}
13132 
13133 	t4_dump_mem(device_get_softc(dev), addr, count);
13134 }
13135 #endif
13136 
13137 static eventhandler_tag vxlan_start_evtag;
13138 static eventhandler_tag vxlan_stop_evtag;
13139 
13140 struct vxlan_evargs {
13141 	if_t ifp;
13142 	uint16_t port;
13143 };
13144 
13145 static void
enable_vxlan_rx(struct adapter * sc)13146 enable_vxlan_rx(struct adapter *sc)
13147 {
13148 	int i, rc;
13149 	struct port_info *pi;
13150 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
13151 
13152 	ASSERT_SYNCHRONIZED_OP(sc);
13153 
13154 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
13155 	    F_VXLAN_EN);
13156 	for_each_port(sc, i) {
13157 		pi = sc->port[i];
13158 		if (pi->vxlan_tcam_entry == true)
13159 			continue;
13160 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
13161 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
13162 		    true);
13163 		if (rc < 0) {
13164 			rc = -rc;
13165 			CH_ERR(&pi->vi[0],
13166 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
13167 		} else {
13168 			MPASS(rc == sc->rawf_base + pi->port_id);
13169 			pi->vxlan_tcam_entry = true;
13170 		}
13171 	}
13172 }
13173 
13174 static void
t4_vxlan_start(struct adapter * sc,void * arg)13175 t4_vxlan_start(struct adapter *sc, void *arg)
13176 {
13177 	struct vxlan_evargs *v = arg;
13178 
13179 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13180 		return;
13181 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
13182 		return;
13183 
13184 	if (sc->vxlan_refcount == 0) {
13185 		sc->vxlan_port = v->port;
13186 		sc->vxlan_refcount = 1;
13187 		if (!hw_off_limits(sc))
13188 			enable_vxlan_rx(sc);
13189 	} else if (sc->vxlan_port == v->port) {
13190 		sc->vxlan_refcount++;
13191 	} else {
13192 		CH_ERR(sc, "VXLAN already configured on port  %d; "
13193 		    "ignoring attempt to configure it on port %d\n",
13194 		    sc->vxlan_port, v->port);
13195 	}
13196 	end_synchronized_op(sc, 0);
13197 }
13198 
13199 static void
t4_vxlan_stop(struct adapter * sc,void * arg)13200 t4_vxlan_stop(struct adapter *sc, void *arg)
13201 {
13202 	struct vxlan_evargs *v = arg;
13203 
13204 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13205 		return;
13206 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
13207 		return;
13208 
13209 	/*
13210 	 * VXLANs may have been configured before the driver was loaded so we
13211 	 * may see more stops than starts.  This is not handled cleanly but at
13212 	 * least we keep the refcount sane.
13213 	 */
13214 	if (sc->vxlan_port != v->port)
13215 		goto done;
13216 	if (sc->vxlan_refcount == 0) {
13217 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
13218 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
13219 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
13220 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
13221 done:
13222 	end_synchronized_op(sc, 0);
13223 }
13224 
13225 static void
t4_vxlan_start_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13226 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
13227     sa_family_t family, u_int port)
13228 {
13229 	struct vxlan_evargs v;
13230 
13231 	MPASS(family == AF_INET || family == AF_INET6);
13232 	v.ifp = ifp;
13233 	v.port = port;
13234 
13235 	t4_iterate(t4_vxlan_start, &v);
13236 }
13237 
13238 static void
t4_vxlan_stop_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)13239 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
13240     u_int port)
13241 {
13242 	struct vxlan_evargs v;
13243 
13244 	MPASS(family == AF_INET || family == AF_INET6);
13245 	v.ifp = ifp;
13246 	v.port = port;
13247 
13248 	t4_iterate(t4_vxlan_stop, &v);
13249 }
13250 
13251 
13252 static struct sx mlu;	/* mod load unload */
13253 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
13254 
13255 static int
mod_event(module_t mod,int cmd,void * arg)13256 mod_event(module_t mod, int cmd, void *arg)
13257 {
13258 	int rc = 0;
13259 	static int loaded = 0;
13260 
13261 	switch (cmd) {
13262 	case MOD_LOAD:
13263 		sx_xlock(&mlu);
13264 		if (loaded++ == 0) {
13265 			t4_sge_modload();
13266 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13267 			    t4_filter_rpl, CPL_COOKIE_FILTER);
13268 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
13269 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
13270 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
13271 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
13272 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13273 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
13274 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
13275 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
13276 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
13277 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
13278 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
13279 			    do_smt_write_rpl);
13280 			sx_init(&t4_list_lock, "T4/T5 adapters");
13281 			SLIST_INIT(&t4_list);
13282 			callout_init(&fatal_callout, 1);
13283 #ifdef TCP_OFFLOAD
13284 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
13285 #endif
13286 #ifdef INET6
13287 			t4_clip_modload();
13288 #endif
13289 #ifdef KERN_TLS
13290 			t6_ktls_modload();
13291 #endif
13292 			t4_tracer_modload();
13293 			tweak_tunables();
13294 			vxlan_start_evtag =
13295 			    EVENTHANDLER_REGISTER(vxlan_start,
13296 				t4_vxlan_start_handler, NULL,
13297 				EVENTHANDLER_PRI_ANY);
13298 			vxlan_stop_evtag =
13299 			    EVENTHANDLER_REGISTER(vxlan_stop,
13300 				t4_vxlan_stop_handler, NULL,
13301 				EVENTHANDLER_PRI_ANY);
13302 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
13303 			    taskqueue_thread_enqueue, &reset_tq);
13304 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
13305 			    "t4_rst_thr");
13306 		}
13307 		sx_xunlock(&mlu);
13308 		break;
13309 
13310 	case MOD_UNLOAD:
13311 		sx_xlock(&mlu);
13312 		if (--loaded == 0) {
13313 #ifdef TCP_OFFLOAD
13314 			int i;
13315 #endif
13316 			int tries;
13317 
13318 			taskqueue_free(reset_tq);
13319 
13320 			tries = 0;
13321 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
13322 				uprintf("%ju clusters with custom free routine "
13323 				    "still is use.\n", t4_sge_extfree_refs());
13324 				pause("t4unload", 2 * hz);
13325 			}
13326 
13327 			sx_slock(&t4_list_lock);
13328 			if (!SLIST_EMPTY(&t4_list)) {
13329 				rc = EBUSY;
13330 				sx_sunlock(&t4_list_lock);
13331 				goto done_unload;
13332 			}
13333 #ifdef TCP_OFFLOAD
13334 			sx_slock(&t4_uld_list_lock);
13335 			for (i = 0; i <= ULD_MAX; i++) {
13336 				if (t4_uld_list[i] != NULL) {
13337 					rc = EBUSY;
13338 					sx_sunlock(&t4_uld_list_lock);
13339 					sx_sunlock(&t4_list_lock);
13340 					goto done_unload;
13341 				}
13342 			}
13343 			sx_sunlock(&t4_uld_list_lock);
13344 #endif
13345 			sx_sunlock(&t4_list_lock);
13346 
13347 			if (t4_sge_extfree_refs() == 0) {
13348 				EVENTHANDLER_DEREGISTER(vxlan_start,
13349 				    vxlan_start_evtag);
13350 				EVENTHANDLER_DEREGISTER(vxlan_stop,
13351 				    vxlan_stop_evtag);
13352 				t4_tracer_modunload();
13353 #ifdef KERN_TLS
13354 				t6_ktls_modunload();
13355 #endif
13356 #ifdef INET6
13357 				t4_clip_modunload();
13358 #endif
13359 #ifdef TCP_OFFLOAD
13360 				sx_destroy(&t4_uld_list_lock);
13361 #endif
13362 				sx_destroy(&t4_list_lock);
13363 				t4_sge_modunload();
13364 				loaded = 0;
13365 			} else {
13366 				rc = EBUSY;
13367 				loaded++;	/* undo earlier decrement */
13368 			}
13369 		}
13370 done_unload:
13371 		sx_xunlock(&mlu);
13372 		break;
13373 	}
13374 
13375 	return (rc);
13376 }
13377 
13378 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
13379 MODULE_VERSION(t4nex, 1);
13380 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
13381 #ifdef DEV_NETMAP
13382 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
13383 #endif /* DEV_NETMAP */
13384 
13385 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
13386 MODULE_VERSION(t5nex, 1);
13387 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
13388 #ifdef DEV_NETMAP
13389 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
13390 #endif /* DEV_NETMAP */
13391 
13392 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
13393 MODULE_VERSION(t6nex, 1);
13394 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
13395 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
13396 #ifdef DEV_NETMAP
13397 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
13398 #endif /* DEV_NETMAP */
13399 
13400 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
13401 MODULE_VERSION(cxgbe, 1);
13402 
13403 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
13404 MODULE_VERSION(cxl, 1);
13405 
13406 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
13407 MODULE_VERSION(cc, 1);
13408 
13409 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
13410 MODULE_VERSION(vcxgbe, 1);
13411 
13412 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
13413 MODULE_VERSION(vcxl, 1);
13414 
13415 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
13416 MODULE_VERSION(vcc, 1);
13417