1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011, 2025 Chelsio Communications.
5 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include "opt_ddb.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_kern_tls.h"
34 #include "opt_ratelimit.h"
35 #include "opt_rss.h"
36
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/priv.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/eventhandler.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
45 #include <sys/queue.h>
46 #include <sys/taskqueue.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <sys/firmware.h>
50 #include <sys/sbuf.h>
51 #include <sys/smp.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/if_vlan_var.h>
60 #include <net/rss_config.h>
61 #include <netinet/in.h>
62 #include <netinet/ip.h>
63 #ifdef KERN_TLS
64 #include <netinet/tcp_seq.h>
65 #endif
66 #if defined(__i386__) || defined(__amd64__)
67 #include <machine/md_var.h>
68 #include <machine/cputypes.h>
69 #include <vm/vm.h>
70 #include <vm/pmap.h>
71 #endif
72 #ifdef DDB
73 #include <ddb/ddb.h>
74 #include <ddb/db_lex.h>
75 #endif
76
77 #include "common/common.h"
78 #include "common/t4_msg.h"
79 #include "common/t4_regs.h"
80 #include "common/t4_regs_values.h"
81 #include "cudbg/cudbg.h"
82 #include "t4_clip.h"
83 #include "t4_ioctl.h"
84 #include "t4_l2t.h"
85 #include "t4_mp_ring.h"
86 #include "t4_if.h"
87 #include "t4_smt.h"
88
89 /* T4 bus driver interface */
90 static int t4_probe(device_t);
91 static int t4_attach(device_t);
92 static int t4_detach(device_t);
93 static int t4_child_location(device_t, device_t, struct sbuf *);
94 static int t4_ready(device_t);
95 static int t4_read_port_device(device_t, int, device_t *);
96 static int t4_suspend(device_t);
97 static int t4_resume(device_t);
98 static int t4_reset_prepare(device_t, device_t);
99 static int t4_reset_post(device_t, device_t);
100 static device_method_t t4_methods[] = {
101 DEVMETHOD(device_probe, t4_probe),
102 DEVMETHOD(device_attach, t4_attach),
103 DEVMETHOD(device_detach, t4_detach),
104 DEVMETHOD(device_suspend, t4_suspend),
105 DEVMETHOD(device_resume, t4_resume),
106
107 DEVMETHOD(bus_child_location, t4_child_location),
108 DEVMETHOD(bus_reset_prepare, t4_reset_prepare),
109 DEVMETHOD(bus_reset_post, t4_reset_post),
110
111 DEVMETHOD(t4_is_main_ready, t4_ready),
112 DEVMETHOD(t4_read_port_device, t4_read_port_device),
113
114 DEVMETHOD_END
115 };
116 static driver_t t4_driver = {
117 "t4nex",
118 t4_methods,
119 sizeof(struct adapter)
120 };
121
122
123 /* T4 port (cxgbe) interface */
124 static int cxgbe_probe(device_t);
125 static int cxgbe_attach(device_t);
126 static int cxgbe_detach(device_t);
127 device_method_t cxgbe_methods[] = {
128 DEVMETHOD(device_probe, cxgbe_probe),
129 DEVMETHOD(device_attach, cxgbe_attach),
130 DEVMETHOD(device_detach, cxgbe_detach),
131 { 0, 0 }
132 };
133 static driver_t cxgbe_driver = {
134 "cxgbe",
135 cxgbe_methods,
136 sizeof(struct port_info)
137 };
138
139 /* T4 VI (vcxgbe) interface */
140 static int vcxgbe_probe(device_t);
141 static int vcxgbe_attach(device_t);
142 static int vcxgbe_detach(device_t);
143 static device_method_t vcxgbe_methods[] = {
144 DEVMETHOD(device_probe, vcxgbe_probe),
145 DEVMETHOD(device_attach, vcxgbe_attach),
146 DEVMETHOD(device_detach, vcxgbe_detach),
147 { 0, 0 }
148 };
149 static driver_t vcxgbe_driver = {
150 "vcxgbe",
151 vcxgbe_methods,
152 sizeof(struct vi_info)
153 };
154
155 static d_ioctl_t t4_ioctl;
156
157 static struct cdevsw t4_cdevsw = {
158 .d_version = D_VERSION,
159 .d_ioctl = t4_ioctl,
160 .d_name = "t4nex",
161 };
162
163 /* T5 bus driver interface */
164 static int t5_probe(device_t);
165 static device_method_t t5_methods[] = {
166 DEVMETHOD(device_probe, t5_probe),
167 DEVMETHOD(device_attach, t4_attach),
168 DEVMETHOD(device_detach, t4_detach),
169 DEVMETHOD(device_suspend, t4_suspend),
170 DEVMETHOD(device_resume, t4_resume),
171
172 DEVMETHOD(bus_child_location, t4_child_location),
173 DEVMETHOD(bus_reset_prepare, t4_reset_prepare),
174 DEVMETHOD(bus_reset_post, t4_reset_post),
175
176 DEVMETHOD(t4_is_main_ready, t4_ready),
177 DEVMETHOD(t4_read_port_device, t4_read_port_device),
178
179 DEVMETHOD_END
180 };
181 static driver_t t5_driver = {
182 "t5nex",
183 t5_methods,
184 sizeof(struct adapter)
185 };
186
187
188 /* T5 port (cxl) interface */
189 static driver_t cxl_driver = {
190 "cxl",
191 cxgbe_methods,
192 sizeof(struct port_info)
193 };
194
195 /* T5 VI (vcxl) interface */
196 static driver_t vcxl_driver = {
197 "vcxl",
198 vcxgbe_methods,
199 sizeof(struct vi_info)
200 };
201
202 /* T6 bus driver interface */
203 static int t6_probe(device_t);
204 static device_method_t t6_methods[] = {
205 DEVMETHOD(device_probe, t6_probe),
206 DEVMETHOD(device_attach, t4_attach),
207 DEVMETHOD(device_detach, t4_detach),
208 DEVMETHOD(device_suspend, t4_suspend),
209 DEVMETHOD(device_resume, t4_resume),
210
211 DEVMETHOD(bus_child_location, t4_child_location),
212 DEVMETHOD(bus_reset_prepare, t4_reset_prepare),
213 DEVMETHOD(bus_reset_post, t4_reset_post),
214
215 DEVMETHOD(t4_is_main_ready, t4_ready),
216 DEVMETHOD(t4_read_port_device, t4_read_port_device),
217
218 DEVMETHOD_END
219 };
220 static driver_t t6_driver = {
221 "t6nex",
222 t6_methods,
223 sizeof(struct adapter)
224 };
225
226
227 /* T6 port (cc) interface */
228 static driver_t cc_driver = {
229 "cc",
230 cxgbe_methods,
231 sizeof(struct port_info)
232 };
233
234 /* T6 VI (vcc) interface */
235 static driver_t vcc_driver = {
236 "vcc",
237 vcxgbe_methods,
238 sizeof(struct vi_info)
239 };
240
241 /* T7+ bus driver interface */
242 static int ch_probe(device_t);
243 static device_method_t ch_methods[] = {
244 DEVMETHOD(device_probe, ch_probe),
245 DEVMETHOD(device_attach, t4_attach),
246 DEVMETHOD(device_detach, t4_detach),
247 DEVMETHOD(device_suspend, t4_suspend),
248 DEVMETHOD(device_resume, t4_resume),
249
250 DEVMETHOD(bus_child_location, t4_child_location),
251 DEVMETHOD(bus_reset_prepare, t4_reset_prepare),
252 DEVMETHOD(bus_reset_post, t4_reset_post),
253
254 DEVMETHOD(t4_is_main_ready, t4_ready),
255 DEVMETHOD(t4_read_port_device, t4_read_port_device),
256
257 DEVMETHOD_END
258 };
259 static driver_t ch_driver = {
260 "chnex",
261 ch_methods,
262 sizeof(struct adapter)
263 };
264
265
266 /* T7+ port (che) interface */
267 static driver_t che_driver = {
268 "che",
269 cxgbe_methods,
270 sizeof(struct port_info)
271 };
272
273 /* T7+ VI (vche) interface */
274 static driver_t vche_driver = {
275 "vche",
276 vcxgbe_methods,
277 sizeof(struct vi_info)
278 };
279
280 /* ifnet interface */
281 static void cxgbe_init(void *);
282 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
283 static int cxgbe_transmit(if_t, struct mbuf *);
284 static void cxgbe_qflush(if_t);
285 #if defined(KERN_TLS) || defined(RATELIMIT)
286 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
287 struct m_snd_tag **);
288 #endif
289
290 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
291
292 /*
293 * Correct lock order when you need to acquire multiple locks is t4_list_lock,
294 * then ADAPTER_LOCK, then t4_uld_list_lock.
295 */
296 static struct sx t4_list_lock;
297 SLIST_HEAD(, adapter) t4_list;
298 #ifdef TCP_OFFLOAD
299 static struct sx t4_uld_list_lock;
300 struct uld_info *t4_uld_list[ULD_MAX + 1];
301 #endif
302
303 /*
304 * Tunables. See tweak_tunables() too.
305 *
306 * Each tunable is set to a default value here if it's known at compile-time.
307 * Otherwise it is set to -n as an indication to tweak_tunables() that it should
308 * provide a reasonable default (upto n) when the driver is loaded.
309 *
310 * Tunables applicable to both T4 and T5 are under hw.cxgbe. Those specific to
311 * T5 are under hw.cxl.
312 */
313 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
314 "cxgbe(4) parameters");
315 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
316 "cxgbe(4) T5+ parameters");
317 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
318 "cxgbe(4) TOE parameters");
319
320 /*
321 * Number of queues for tx and rx, NIC and offload.
322 */
323 #define NTXQ 16
324 int t4_ntxq = -NTXQ;
325 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
326 "Number of TX queues per port");
327 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq); /* Old name, undocumented */
328
329 #define NRXQ 8
330 int t4_nrxq = -NRXQ;
331 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
332 "Number of RX queues per port");
333 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq); /* Old name, undocumented */
334
335 #define NTXQ_VI 1
336 static int t4_ntxq_vi = -NTXQ_VI;
337 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
338 "Number of TX queues per VI");
339
340 #define NRXQ_VI 1
341 static int t4_nrxq_vi = -NRXQ_VI;
342 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
343 "Number of RX queues per VI");
344
345 static int t4_rsrv_noflowq = 0;
346 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
347 0, "Reserve TX queue 0 of each VI for non-flowid packets");
348
349 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
350 #define NOFLDTXQ 8
351 static int t4_nofldtxq = -NOFLDTXQ;
352 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
353 "Number of offload TX queues per port");
354
355 #define NOFLDTXQ_VI 1
356 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
357 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
358 "Number of offload TX queues per VI");
359 #endif
360
361 #if defined(TCP_OFFLOAD)
362 #define NOFLDRXQ 2
363 static int t4_nofldrxq = -NOFLDRXQ;
364 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
365 "Number of offload RX queues per port");
366
367 #define NOFLDRXQ_VI 1
368 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
369 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
370 "Number of offload RX queues per VI");
371
372 #define TMR_IDX_OFLD 1
373 static int t4_tmr_idx_ofld = TMR_IDX_OFLD;
374 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
375 &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
376
377 #define PKTC_IDX_OFLD (-1)
378 static int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
379 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
380 &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
381
382 /* 0 means chip/fw default, non-zero number is value in microseconds */
383 static u_long t4_toe_keepalive_idle = 0;
384 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
385 &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
386
387 /* 0 means chip/fw default, non-zero number is value in microseconds */
388 static u_long t4_toe_keepalive_interval = 0;
389 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
390 &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
391
392 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
393 static int t4_toe_keepalive_count = 0;
394 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
395 &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
396
397 /* 0 means chip/fw default, non-zero number is value in microseconds */
398 static u_long t4_toe_rexmt_min = 0;
399 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
400 &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
401
402 /* 0 means chip/fw default, non-zero number is value in microseconds */
403 static u_long t4_toe_rexmt_max = 0;
404 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
405 &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
406
407 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
408 static int t4_toe_rexmt_count = 0;
409 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
410 &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
411
412 /* -1 means chip/fw default, other values are raw backoff values to use */
413 static int t4_toe_rexmt_backoff[16] = {
414 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
415 };
416 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
417 CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
418 "cxgbe(4) TOE retransmit backoff values");
419 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
420 &t4_toe_rexmt_backoff[0], 0, "");
421 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
422 &t4_toe_rexmt_backoff[1], 0, "");
423 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
424 &t4_toe_rexmt_backoff[2], 0, "");
425 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
426 &t4_toe_rexmt_backoff[3], 0, "");
427 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
428 &t4_toe_rexmt_backoff[4], 0, "");
429 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
430 &t4_toe_rexmt_backoff[5], 0, "");
431 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
432 &t4_toe_rexmt_backoff[6], 0, "");
433 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
434 &t4_toe_rexmt_backoff[7], 0, "");
435 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
436 &t4_toe_rexmt_backoff[8], 0, "");
437 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
438 &t4_toe_rexmt_backoff[9], 0, "");
439 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
440 &t4_toe_rexmt_backoff[10], 0, "");
441 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
442 &t4_toe_rexmt_backoff[11], 0, "");
443 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
444 &t4_toe_rexmt_backoff[12], 0, "");
445 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
446 &t4_toe_rexmt_backoff[13], 0, "");
447 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
448 &t4_toe_rexmt_backoff[14], 0, "");
449 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
450 &t4_toe_rexmt_backoff[15], 0, "");
451
452 int t4_ddp_rcvbuf_len = 256 * 1024;
453 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_len, CTLFLAG_RWTUN,
454 &t4_ddp_rcvbuf_len, 0, "length of each DDP RX buffer");
455
456 unsigned int t4_ddp_rcvbuf_cache = 4;
457 SYSCTL_UINT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_cache, CTLFLAG_RWTUN,
458 &t4_ddp_rcvbuf_cache, 0,
459 "maximum number of free DDP RX buffers to cache per connection");
460 #endif
461
462 #ifdef DEV_NETMAP
463 #define NN_MAIN_VI (1 << 0) /* Native netmap on the main VI */
464 #define NN_EXTRA_VI (1 << 1) /* Native netmap on the extra VI(s) */
465 static int t4_native_netmap = NN_EXTRA_VI;
466 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
467 0, "Native netmap support. bit 0 = main VI, bit 1 = extra VIs");
468
469 #define NNMTXQ 8
470 static int t4_nnmtxq = -NNMTXQ;
471 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
472 "Number of netmap TX queues");
473
474 #define NNMRXQ 8
475 static int t4_nnmrxq = -NNMRXQ;
476 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
477 "Number of netmap RX queues");
478
479 #define NNMTXQ_VI 2
480 static int t4_nnmtxq_vi = -NNMTXQ_VI;
481 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
482 "Number of netmap TX queues per VI");
483
484 #define NNMRXQ_VI 2
485 static int t4_nnmrxq_vi = -NNMRXQ_VI;
486 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
487 "Number of netmap RX queues per VI");
488 #endif
489
490 /*
491 * Holdoff parameters for ports.
492 */
493 #define TMR_IDX 1
494 int t4_tmr_idx = TMR_IDX;
495 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
496 0, "Holdoff timer index");
497 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx); /* Old name */
498
499 #define PKTC_IDX (-1)
500 int t4_pktc_idx = PKTC_IDX;
501 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
502 0, "Holdoff packet counter index");
503 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx); /* Old name */
504
505 /*
506 * Size (# of entries) of each tx and rx queue.
507 */
508 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
509 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
510 "Number of descriptors in each TX queue");
511
512 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
513 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
514 "Number of descriptors in each RX queue");
515
516 /*
517 * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
518 */
519 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
520 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
521 0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
522
523 /*
524 * Configuration file. All the _CF names here are special.
525 */
526 #define DEFAULT_CF "default"
527 #define BUILTIN_CF "built-in"
528 #define FLASH_CF "flash"
529 #define UWIRE_CF "uwire"
530 #define FPGA_CF "fpga"
531 static char t4_cfg_file[32] = DEFAULT_CF;
532 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
533 sizeof(t4_cfg_file), "Firmware configuration file");
534
535 /*
536 * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
537 * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
538 * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
539 * mark or when signalled to do so, 0 to never emit PAUSE.
540 * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
541 * negotiated settings will override rx_pause/tx_pause.
542 * Otherwise rx_pause/tx_pause are applied forcibly.
543 */
544 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
545 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
546 &t4_pause_settings, 0,
547 "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
548
549 /*
550 * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
551 * -1 to run with the firmware default. Same as FEC_AUTO (bit 5)
552 * 0 to disable FEC.
553 */
554 static int t4_fec = -1;
555 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
556 "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
557
558 static const char *
559 t4_fec_bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2\6auto\7module";
560
561 /*
562 * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
563 * issues to the firmware. If the firmware doesn't support FORCE_FEC then the
564 * driver runs as if this is set to 0.
565 * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
566 * 0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
567 * transceiver. Multiple FEC bits may not be okay but will be passed on to
568 * the firmware anyway (may result in l1cfg errors with old firmwares).
569 * 1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
570 * means set all FEC bits that are valid for the speed.
571 */
572 static int t4_force_fec = -1;
573 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
574 "Controls the use of FORCE_FEC bit in L1 configuration.");
575
576 /*
577 * Link autonegotiation.
578 * -1 to run with the firmware default.
579 * 0 to disable.
580 * 1 to enable.
581 */
582 static int t4_autoneg = -1;
583 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
584 "Link autonegotiation");
585
586 /*
587 * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
588 * encouraged respectively). '-n' is the same as 'n' except the firmware
589 * version used in the checks is read from the firmware bundled with the driver.
590 */
591 static int t4_fw_install = 1;
592 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
593 "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
594
595 /*
596 * ASIC features that will be used. Disable the ones you don't want so that the
597 * chip resources aren't wasted on features that will not be used.
598 */
599 static int t4_nbmcaps_allowed = 0;
600 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
601 &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
602
603 static int t4_linkcaps_allowed = 0; /* No DCBX, PPP, etc. by default */
604 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
605 &t4_linkcaps_allowed, 0, "Default link capabilities");
606
607 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
608 FW_CAPS_CONFIG_SWITCH_EGRESS;
609 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
610 &t4_switchcaps_allowed, 0, "Default switch capabilities");
611
612 static int t4_nvmecaps_allowed = -1;
613 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nvmecaps_allowed, CTLFLAG_RDTUN,
614 &t4_nvmecaps_allowed, 0, "Default NVMe capabilities");
615
616 #ifdef RATELIMIT
617 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
618 FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
619 #else
620 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
621 FW_CAPS_CONFIG_NIC_HASHFILTER;
622 #endif
623 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
624 &t4_niccaps_allowed, 0, "Default NIC capabilities");
625
626 static int t4_toecaps_allowed = -1;
627 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
628 &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
629
630 static int t4_rdmacaps_allowed = -1;
631 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
632 &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
633
634 static int t4_cryptocaps_allowed = -1;
635 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
636 &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
637
638 static int t4_iscsicaps_allowed = -1;
639 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
640 &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
641
642 static int t4_fcoecaps_allowed = 0;
643 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
644 &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
645
646 static int t5_write_combine = 0;
647 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
648 0, "Use WC instead of UC for BAR2");
649
650 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
651 static int t4_doorbells_allowed = 0xf;
652 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
653 &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
654
655 static int t4_num_vis = 1;
656 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
657 "Number of VIs per port");
658
659 /*
660 * PCIe Relaxed Ordering.
661 * -1: driver should figure out a good value.
662 * 0: disable RO.
663 * 1: enable RO.
664 * 2: leave RO alone.
665 */
666 static int pcie_relaxed_ordering = -1;
667 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
668 &pcie_relaxed_ordering, 0,
669 "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
670
671 static int t4_panic_on_fatal_err = 0;
672 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
673 &t4_panic_on_fatal_err, 0, "panic on fatal errors");
674
675 static int t4_reset_on_fatal_err = 0;
676 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
677 &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
678
679 static int t4_reset_method = 1;
680 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_method, CTLFLAG_RWTUN, &t4_reset_method,
681 0, "reset method: 0 = PL_RST, 1 = PCIe secondary bus reset, 2 = PCIe link bounce");
682
683 static int t4_clock_gate_on_suspend = 0;
684 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
685 &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
686
687 static int t4_tx_vm_wr = 0;
688 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
689 "Use VM work requests to transmit packets.");
690
691 /*
692 * Set to non-zero to enable the attack filter. A packet that matches any of
693 * these conditions will get dropped on ingress:
694 * 1) IP && source address == destination address.
695 * 2) TCP/IP && source address is not a unicast address.
696 * 3) TCP/IP && destination address is not a unicast address.
697 * 4) IP && source address is loopback (127.x.y.z).
698 * 5) IP && destination address is loopback (127.x.y.z).
699 * 6) IPv6 && source address == destination address.
700 * 7) IPv6 && source address is not a unicast address.
701 * 8) IPv6 && source address is loopback (::1/128).
702 * 9) IPv6 && destination address is loopback (::1/128).
703 * 10) IPv6 && source address is unspecified (::/128).
704 * 11) IPv6 && destination address is unspecified (::/128).
705 * 12) TCP/IPv6 && source address is multicast (ff00::/8).
706 * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
707 */
708 static int t4_attack_filter = 0;
709 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
710 &t4_attack_filter, 0, "Drop suspicious traffic");
711
712 static int t4_drop_ip_fragments = 0;
713 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
714 &t4_drop_ip_fragments, 0, "Drop IP fragments");
715
716 static int t4_drop_pkts_with_l2_errors = 1;
717 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
718 &t4_drop_pkts_with_l2_errors, 0,
719 "Drop all frames with Layer 2 length or checksum errors");
720
721 static int t4_drop_pkts_with_l3_errors = 0;
722 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
723 &t4_drop_pkts_with_l3_errors, 0,
724 "Drop all frames with IP version, length, or checksum errors");
725
726 static int t4_drop_pkts_with_l4_errors = 0;
727 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
728 &t4_drop_pkts_with_l4_errors, 0,
729 "Drop all frames with Layer 4 length, checksum, or other errors");
730
731 #ifdef TCP_OFFLOAD
732 /*
733 * TOE tunables.
734 */
735 static int t4_cop_managed_offloading = 0;
736 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
737 &t4_cop_managed_offloading, 0,
738 "COP (Connection Offload Policy) controls all TOE offload");
739 TUNABLE_INT("hw.cxgbe.cop_managed_offloading", &t4_cop_managed_offloading);
740 #endif
741
742 #ifdef KERN_TLS
743 /*
744 * This enables KERN_TLS for all adapters if set.
745 */
746 static int t4_kern_tls = 0;
747 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
748 "Enable KERN_TLS mode for T6 adapters");
749
750 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
751 "cxgbe(4) KERN_TLS parameters");
752
753 static int t4_tls_inline_keys = 0;
754 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
755 &t4_tls_inline_keys, 0,
756 "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
757 "in card memory.");
758
759 static int t4_tls_combo_wrs = 0;
760 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
761 0, "Attempt to combine TCB field updates with TLS record work requests.");
762
763 static int t4_tls_short_records = 1;
764 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, short_records, CTLFLAG_RDTUN,
765 &t4_tls_short_records, 0, "Use cipher-only mode for short records.");
766
767 static int t4_tls_partial_ghash = 1;
768 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, partial_ghash, CTLFLAG_RDTUN,
769 &t4_tls_partial_ghash, 0, "Use partial GHASH for AES-GCM records.");
770 #endif
771
772 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
773 static int vi_mac_funcs[] = {
774 FW_VI_FUNC_ETH,
775 FW_VI_FUNC_OFLD,
776 FW_VI_FUNC_IWARP,
777 FW_VI_FUNC_OPENISCSI,
778 FW_VI_FUNC_OPENFCOE,
779 FW_VI_FUNC_FOISCSI,
780 FW_VI_FUNC_FOFCOE,
781 };
782
783 struct intrs_and_queues {
784 uint16_t intr_type; /* INTx, MSI, or MSI-X */
785 uint16_t num_vis; /* number of VIs for each port */
786 uint16_t nirq; /* Total # of vectors */
787 uint16_t ntxq; /* # of NIC txq's for each port */
788 uint16_t nrxq; /* # of NIC rxq's for each port */
789 uint16_t nofldtxq; /* # of TOE/ETHOFLD txq's for each port */
790 uint16_t nofldrxq; /* # of TOE rxq's for each port */
791 uint16_t nnmtxq; /* # of netmap txq's */
792 uint16_t nnmrxq; /* # of netmap rxq's */
793
794 /* The vcxgbe/vcxl interfaces use these and not the ones above. */
795 uint16_t ntxq_vi; /* # of NIC txq's */
796 uint16_t nrxq_vi; /* # of NIC rxq's */
797 uint16_t nofldtxq_vi; /* # of TOE txq's */
798 uint16_t nofldrxq_vi; /* # of TOE rxq's */
799 uint16_t nnmtxq_vi; /* # of netmap txq's */
800 uint16_t nnmrxq_vi; /* # of netmap rxq's */
801 };
802
803 static void setup_memwin(struct adapter *);
804 static void position_memwin(struct adapter *, int, uint32_t);
805 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
806 static int fwmtype_to_hwmtype(int);
807 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
808 uint32_t *);
809 static int fixup_devlog_params(struct adapter *);
810 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
811 static int contact_firmware(struct adapter *);
812 static int partition_resources(struct adapter *);
813 static int get_params__pre_init(struct adapter *);
814 static int set_params__pre_init(struct adapter *);
815 static int get_params__post_init(struct adapter *);
816 static int set_params__post_init(struct adapter *);
817 static void t4_set_desc(struct adapter *);
818 static bool fixed_ifmedia(struct port_info *);
819 static void build_medialist(struct port_info *);
820 static void init_link_config(struct port_info *);
821 static int fixup_link_config(struct port_info *);
822 static int apply_link_config(struct port_info *);
823 static int cxgbe_init_synchronized(struct vi_info *);
824 static int cxgbe_uninit_synchronized(struct vi_info *);
825 static int adapter_full_init(struct adapter *);
826 static void adapter_full_uninit(struct adapter *);
827 static int vi_full_init(struct vi_info *);
828 static void vi_full_uninit(struct vi_info *);
829 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
830 static void quiesce_txq(struct sge_txq *);
831 static void quiesce_wrq(struct sge_wrq *);
832 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
833 static void quiesce_vi(struct vi_info *);
834 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
835 driver_intr_t *, void *, char *);
836 static int t4_free_irq(struct adapter *, struct irq *);
837 static void t4_init_atid_table(struct adapter *);
838 static void t4_free_atid_table(struct adapter *);
839 static void stop_atid_allocator(struct adapter *);
840 static void restart_atid_allocator(struct adapter *);
841 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
842 static void vi_refresh_stats(struct vi_info *);
843 static void cxgbe_refresh_stats(struct vi_info *);
844 static void cxgbe_tick(void *);
845 static void vi_tick(void *);
846 static void cxgbe_sysctls(struct port_info *);
847 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
848 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
849 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
850 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
851 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
852 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
853 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
854 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
855 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
856 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
857 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
858 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
859 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
860 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
861 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
862 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
863 static int sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS);
864 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
865 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
866 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
867 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
868 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
869 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
870 static int sysctl_cim_ibq(SYSCTL_HANDLER_ARGS);
871 static int sysctl_cim_obq(SYSCTL_HANDLER_ARGS);
872 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
873 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
874 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
875 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
876 static int sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS);
877 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
878 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
879 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
880 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
881 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
882 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
883 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
884 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
885 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
886 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
887 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
888 static int sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS);
889 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
890 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
891 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
892 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
893 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
894 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
895 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
896 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
897 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
898 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
899 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
900 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
901 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
902 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
903 #ifdef TCP_OFFLOAD
904 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
905 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
906 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
907 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
908 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
909 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
910 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
911 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
912 #endif
913 static int get_sge_context(struct adapter *, int, uint32_t, int, uint32_t *);
914 static int load_fw(struct adapter *, struct t4_data *);
915 static int load_cfg(struct adapter *, struct t4_data *);
916 static int load_boot(struct adapter *, struct t4_bootrom *);
917 static int load_bootcfg(struct adapter *, struct t4_data *);
918 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
919 static void free_offload_policy(struct t4_offload_policy *);
920 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
921 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
922 static int read_i2c(struct adapter *, struct t4_i2c_data *);
923 static int clear_stats(struct adapter *, u_int);
924 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
925 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
926 static inline int stop_adapter(struct adapter *);
927 static inline void set_adapter_hwstatus(struct adapter *, const bool);
928 static int stop_lld(struct adapter *);
929 static inline int restart_adapter(struct adapter *);
930 static int restart_lld(struct adapter *);
931 #ifdef TCP_OFFLOAD
932 static int deactivate_all_uld(struct adapter *);
933 static void stop_all_uld(struct adapter *);
934 static void restart_all_uld(struct adapter *);
935 #endif
936 #ifdef KERN_TLS
937 static int ktls_capability(struct adapter *, bool);
938 #endif
939 static int mod_event(module_t, int, void *);
940 static int notify_siblings(device_t, int);
941 static uint64_t vi_get_counter(if_t, ift_counter);
942 static uint64_t cxgbe_get_counter(if_t, ift_counter);
943 static void enable_vxlan_rx(struct adapter *);
944 static void reset_adapter_task(void *, int);
945 static void fatal_error_task(void *, int);
946 static void dump_devlog(struct adapter *);
947 static void dump_cim_regs(struct adapter *);
948 static void dump_cimla(struct adapter *);
949
950 struct {
951 uint16_t device;
952 char *desc;
953 } t4_pciids[] = {
954 {0xa000, "Chelsio Terminator 4 FPGA"},
955 {0x4400, "Chelsio T440-dbg"},
956 {0x4401, "Chelsio T420-CR"},
957 {0x4402, "Chelsio T422-CR"},
958 {0x4403, "Chelsio T440-CR"},
959 {0x4404, "Chelsio T420-BCH"},
960 {0x4405, "Chelsio T440-BCH"},
961 {0x4406, "Chelsio T440-CH"},
962 {0x4407, "Chelsio T420-SO"},
963 {0x4408, "Chelsio T420-CX"},
964 {0x4409, "Chelsio T420-BT"},
965 {0x440a, "Chelsio T404-BT"},
966 {0x440e, "Chelsio T440-LP-CR"},
967 }, t5_pciids[] = {
968 {0xb000, "Chelsio Terminator 5 FPGA"},
969 {0x5400, "Chelsio T580-dbg"},
970 {0x5401, "Chelsio T520-CR"}, /* 2 x 10G */
971 {0x5402, "Chelsio T522-CR"}, /* 2 x 10G, 2 X 1G */
972 {0x5403, "Chelsio T540-CR"}, /* 4 x 10G */
973 {0x5407, "Chelsio T520-SO"}, /* 2 x 10G, nomem */
974 {0x5409, "Chelsio T520-BT"}, /* 2 x 10GBaseT */
975 {0x540a, "Chelsio T504-BT"}, /* 4 x 1G */
976 {0x540d, "Chelsio T580-CR"}, /* 2 x 40G */
977 {0x540e, "Chelsio T540-LP-CR"}, /* 4 x 10G */
978 {0x5410, "Chelsio T580-LP-CR"}, /* 2 x 40G */
979 {0x5411, "Chelsio T520-LL-CR"}, /* 2 x 10G */
980 {0x5412, "Chelsio T560-CR"}, /* 1 x 40G, 2 x 10G */
981 {0x5414, "Chelsio T580-LP-SO-CR"}, /* 2 x 40G, nomem */
982 {0x5415, "Chelsio T502-BT"}, /* 2 x 1G */
983 {0x5418, "Chelsio T540-BT"}, /* 4 x 10GBaseT */
984 {0x5419, "Chelsio T540-LP-BT"}, /* 4 x 10GBaseT */
985 {0x541a, "Chelsio T540-SO-BT"}, /* 4 x 10GBaseT, nomem */
986 {0x541b, "Chelsio T540-SO-CR"}, /* 4 x 10G, nomem */
987
988 /* Custom */
989 {0x5483, "Custom T540-CR"},
990 {0x5484, "Custom T540-BT"},
991 }, t6_pciids[] = {
992 {0xc006, "Chelsio Terminator 6 FPGA"}, /* T6 PE10K6 FPGA (PF0) */
993 {0x6400, "Chelsio T6-DBG-25"}, /* 2 x 10/25G, debug */
994 {0x6401, "Chelsio T6225-CR"}, /* 2 x 10/25G */
995 {0x6402, "Chelsio T6225-SO-CR"}, /* 2 x 10/25G, nomem */
996 {0x6403, "Chelsio T6425-CR"}, /* 4 x 10/25G */
997 {0x6404, "Chelsio T6425-SO-CR"}, /* 4 x 10/25G, nomem */
998 {0x6405, "Chelsio T6225-SO-OCP3"}, /* 2 x 10/25G, nomem */
999 {0x6406, "Chelsio T6225-OCP3"}, /* 2 x 10/25G */
1000 {0x6407, "Chelsio T62100-LP-CR"}, /* 2 x 40/50/100G */
1001 {0x6408, "Chelsio T62100-SO-CR"}, /* 2 x 40/50/100G, nomem */
1002 {0x6409, "Chelsio T6210-BT"}, /* 2 x 10GBASE-T */
1003 {0x640d, "Chelsio T62100-CR"}, /* 2 x 40/50/100G */
1004 {0x6410, "Chelsio T6-DBG-100"}, /* 2 x 40/50/100G, debug */
1005 {0x6411, "Chelsio T6225-LL-CR"}, /* 2 x 10/25G */
1006 {0x6414, "Chelsio T62100-SO-OCP3"}, /* 2 x 40/50/100G, nomem */
1007 {0x6415, "Chelsio T6201-BT"}, /* 2 x 1000BASE-T */
1008
1009 /* Custom */
1010 {0x6480, "Custom T6225-CR"},
1011 {0x6481, "Custom T62100-CR"},
1012 {0x6482, "Custom T6225-CR"},
1013 {0x6483, "Custom T62100-CR"},
1014 {0x6484, "Custom T64100-CR"},
1015 {0x6485, "Custom T6240-SO"},
1016 {0x6486, "Custom T6225-SO-CR"},
1017 {0x6487, "Custom T6225-CR"},
1018 }, t7_pciids[] = {
1019 {0xd000, "Chelsio Terminator 7 FPGA"}, /* T7 PE12K FPGA */
1020 {0x7400, "Chelsio T72200-DBG"}, /* 2 x 200G, debug */
1021 {0x7401, "Chelsio T7250"}, /* 2 x 10/25/50G, 1 mem */
1022 {0x7402, "Chelsio S7250"}, /* 2 x 10/25/50G, nomem */
1023 {0x7403, "Chelsio T7450"}, /* 4 x 10/25/50G, 1 mem */
1024 {0x7404, "Chelsio S7450"}, /* 4 x 10/25/50G, nomem */
1025 {0x7405, "Chelsio T72200"}, /* 2 x 40/100/200G, 1 mem */
1026 {0x7406, "Chelsio S72200"}, /* 2 x 40/100/200G, nomem */
1027 {0x7407, "Chelsio T72200-FH"}, /* 2 x 40/100/200G, 2 mem */
1028 {0x7408, "Chelsio S71400"}, /* 1 x 400G, nomem */
1029 {0x7409, "Chelsio S7210-BT"}, /* 2 x 10GBASE-T, nomem */
1030 {0x740a, "Chelsio T7450-RC"}, /* 4 x 10/25/50G, 1 mem, RC */
1031 {0x740b, "Chelsio T72200-RC"}, /* 2 x 40/100/200G, 1 mem, RC */
1032 {0x740c, "Chelsio T72200-FH-RC"}, /* 2 x 40/100/200G, 2 mem, RC */
1033 {0x740d, "Chelsio S72200-OCP3"}, /* 2 x 40/100/200G OCP3 */
1034 {0x740e, "Chelsio S7450-OCP3"}, /* 4 x 1/20/25/50G OCP3 */
1035 {0x740f, "Chelsio S7410-BT-OCP3"}, /* 4 x 10GBASE-T OCP3 */
1036 {0x7410, "Chelsio S7210-BT-A"}, /* 2 x 10GBASE-T */
1037 {0x7411, "Chelsio T7_MAYRA_7"}, /* Motherboard */
1038
1039 /* Custom */
1040 {0x7480, "Custom T7"},
1041 };
1042
1043 #ifdef TCP_OFFLOAD
1044 /*
1045 * service_iq_fl() has an iq and needs the fl. Offset of fl from the iq should
1046 * be exactly the same for both rxq and ofld_rxq.
1047 */
1048 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
1049 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
1050 #endif
1051 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
1052
1053 static int
t4_probe(device_t dev)1054 t4_probe(device_t dev)
1055 {
1056 int i;
1057 uint16_t v = pci_get_vendor(dev);
1058 uint16_t d = pci_get_device(dev);
1059 uint8_t f = pci_get_function(dev);
1060
1061 if (v != PCI_VENDOR_ID_CHELSIO)
1062 return (ENXIO);
1063
1064 /* Attach only to PF0 of the FPGA */
1065 if (d == 0xa000 && f != 0)
1066 return (ENXIO);
1067
1068 for (i = 0; i < nitems(t4_pciids); i++) {
1069 if (d == t4_pciids[i].device) {
1070 device_set_desc(dev, t4_pciids[i].desc);
1071 return (BUS_PROBE_DEFAULT);
1072 }
1073 }
1074
1075 return (ENXIO);
1076 }
1077
1078 static int
t5_probe(device_t dev)1079 t5_probe(device_t dev)
1080 {
1081 int i;
1082 uint16_t v = pci_get_vendor(dev);
1083 uint16_t d = pci_get_device(dev);
1084 uint8_t f = pci_get_function(dev);
1085
1086 if (v != PCI_VENDOR_ID_CHELSIO)
1087 return (ENXIO);
1088
1089 /* Attach only to PF0 of the FPGA */
1090 if (d == 0xb000 && f != 0)
1091 return (ENXIO);
1092
1093 for (i = 0; i < nitems(t5_pciids); i++) {
1094 if (d == t5_pciids[i].device) {
1095 device_set_desc(dev, t5_pciids[i].desc);
1096 return (BUS_PROBE_DEFAULT);
1097 }
1098 }
1099
1100 return (ENXIO);
1101 }
1102
1103 static int
t6_probe(device_t dev)1104 t6_probe(device_t dev)
1105 {
1106 int i;
1107 uint16_t v = pci_get_vendor(dev);
1108 uint16_t d = pci_get_device(dev);
1109
1110 if (v != PCI_VENDOR_ID_CHELSIO)
1111 return (ENXIO);
1112
1113 for (i = 0; i < nitems(t6_pciids); i++) {
1114 if (d == t6_pciids[i].device) {
1115 device_set_desc(dev, t6_pciids[i].desc);
1116 return (BUS_PROBE_DEFAULT);
1117 }
1118 }
1119
1120 return (ENXIO);
1121 }
1122
1123 static int
ch_probe(device_t dev)1124 ch_probe(device_t dev)
1125 {
1126 int i;
1127 uint16_t v = pci_get_vendor(dev);
1128 uint16_t d = pci_get_device(dev);
1129 uint8_t f = pci_get_function(dev);
1130
1131 if (v != PCI_VENDOR_ID_CHELSIO)
1132 return (ENXIO);
1133
1134 /* Attach only to PF0 of the FPGA */
1135 if (d == 0xd000 && f != 0)
1136 return (ENXIO);
1137
1138 for (i = 0; i < nitems(t7_pciids); i++) {
1139 if (d == t7_pciids[i].device) {
1140 device_set_desc(dev, t7_pciids[i].desc);
1141 return (BUS_PROBE_DEFAULT);
1142 }
1143 }
1144
1145 return (ENXIO);
1146 }
1147
1148 static void
t5_attribute_workaround(device_t dev)1149 t5_attribute_workaround(device_t dev)
1150 {
1151 device_t root_port;
1152 uint32_t v;
1153
1154 /*
1155 * The T5 chips do not properly echo the No Snoop and Relaxed
1156 * Ordering attributes when replying to a TLP from a Root
1157 * Port. As a workaround, find the parent Root Port and
1158 * disable No Snoop and Relaxed Ordering. Note that this
1159 * affects all devices under this root port.
1160 */
1161 root_port = pci_find_pcie_root_port(dev);
1162 if (root_port == NULL) {
1163 device_printf(dev, "Unable to find parent root port\n");
1164 return;
1165 }
1166
1167 v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1168 PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1169 if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1170 0)
1171 device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1172 device_get_nameunit(root_port));
1173 }
1174
1175 static const struct devnames devnames[] = {
1176 {
1177 .nexus_name = "t4nex",
1178 .ifnet_name = "cxgbe",
1179 .vi_ifnet_name = "vcxgbe",
1180 .pf03_drv_name = "t4iov",
1181 .vf_nexus_name = "t4vf",
1182 .vf_ifnet_name = "cxgbev"
1183 }, {
1184 .nexus_name = "t5nex",
1185 .ifnet_name = "cxl",
1186 .vi_ifnet_name = "vcxl",
1187 .pf03_drv_name = "t5iov",
1188 .vf_nexus_name = "t5vf",
1189 .vf_ifnet_name = "cxlv"
1190 }, {
1191 .nexus_name = "t6nex",
1192 .ifnet_name = "cc",
1193 .vi_ifnet_name = "vcc",
1194 .pf03_drv_name = "t6iov",
1195 .vf_nexus_name = "t6vf",
1196 .vf_ifnet_name = "ccv"
1197 }, {
1198 .nexus_name = "chnex",
1199 .ifnet_name = "che",
1200 .vi_ifnet_name = "vche",
1201 .pf03_drv_name = "chiov",
1202 .vf_nexus_name = "chvf",
1203 .vf_ifnet_name = "chev"
1204 }
1205 };
1206
1207 void
t4_init_devnames(struct adapter * sc)1208 t4_init_devnames(struct adapter *sc)
1209 {
1210 int id;
1211
1212 id = chip_id(sc);
1213 if (id < CHELSIO_T4) {
1214 device_printf(sc->dev, "chip id %d is not supported.\n", id);
1215 sc->names = NULL;
1216 } else if (id - CHELSIO_T4 < nitems(devnames))
1217 sc->names = &devnames[id - CHELSIO_T4];
1218 else
1219 sc->names = &devnames[nitems(devnames) - 1];
1220 }
1221
1222 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1223 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1224 {
1225 const char *parent, *name;
1226 long value;
1227 int line, unit;
1228
1229 line = 0;
1230 parent = device_get_nameunit(sc->dev);
1231 name = sc->names->ifnet_name;
1232 while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1233 if (resource_long_value(name, unit, "port", &value) == 0 &&
1234 value == pi->port_id)
1235 return (unit);
1236 }
1237 return (-1);
1238 }
1239
1240 static void
t4_calibration(void * arg)1241 t4_calibration(void *arg)
1242 {
1243 struct adapter *sc;
1244 struct clock_sync *cur, *nex;
1245 uint64_t hw;
1246 sbintime_t sbt;
1247 int next_up;
1248
1249 sc = (struct adapter *)arg;
1250
1251 KASSERT(hw_all_ok(sc), ("!hw_all_ok at t4_calibration"));
1252 hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1253 sbt = sbinuptime();
1254
1255 cur = &sc->cal_info[sc->cal_current];
1256 next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1257 nex = &sc->cal_info[next_up];
1258 if (__predict_false(sc->cal_count == 0)) {
1259 /* First time in, just get the values in */
1260 cur->hw_cur = hw;
1261 cur->sbt_cur = sbt;
1262 sc->cal_count++;
1263 goto done;
1264 }
1265
1266 if (cur->hw_cur == hw) {
1267 /* The clock is not advancing? */
1268 sc->cal_count = 0;
1269 atomic_store_rel_int(&cur->gen, 0);
1270 goto done;
1271 }
1272
1273 seqc_write_begin(&nex->gen);
1274 nex->hw_prev = cur->hw_cur;
1275 nex->sbt_prev = cur->sbt_cur;
1276 nex->hw_cur = hw;
1277 nex->sbt_cur = sbt;
1278 seqc_write_end(&nex->gen);
1279 sc->cal_current = next_up;
1280 done:
1281 callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1282 sc, C_DIRECT_EXEC);
1283 }
1284
1285 static void
t4_calibration_start(struct adapter * sc)1286 t4_calibration_start(struct adapter *sc)
1287 {
1288 /*
1289 * Here if we have not done a calibration
1290 * then do so otherwise start the appropriate
1291 * timer.
1292 */
1293 int i;
1294
1295 for (i = 0; i < CNT_CAL_INFO; i++) {
1296 sc->cal_info[i].gen = 0;
1297 }
1298 sc->cal_current = 0;
1299 sc->cal_count = 0;
1300 sc->cal_gen = 0;
1301 t4_calibration(sc);
1302 }
1303
1304 static int
t4_attach(device_t dev)1305 t4_attach(device_t dev)
1306 {
1307 struct adapter *sc;
1308 int rc = 0, i, j, rqidx, tqidx, nports;
1309 struct make_dev_args mda;
1310 struct intrs_and_queues iaq;
1311 struct sge *s;
1312 uint32_t *buf;
1313 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1314 int ofld_tqidx;
1315 #endif
1316 #ifdef TCP_OFFLOAD
1317 int ofld_rqidx;
1318 #endif
1319 #ifdef DEV_NETMAP
1320 int nm_rqidx, nm_tqidx;
1321 #endif
1322 int num_vis;
1323
1324 sc = device_get_softc(dev);
1325 sc->dev = dev;
1326 sysctl_ctx_init(&sc->ctx);
1327 TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1328 if (TUNABLE_INT_FETCH("hw.cxgbe.iflags", &sc->intr_flags) == 0)
1329 sc->intr_flags = IHF_INTR_CLEAR_ON_INIT | IHF_CLR_ALL_UNIGNORED;
1330
1331 if ((pci_get_device(dev) & 0xff00) == 0x5400)
1332 t5_attribute_workaround(dev);
1333 pci_enable_busmaster(dev);
1334 if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1335 uint32_t v;
1336
1337 pci_set_max_read_req(dev, 4096);
1338 v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1339 sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1340 if (pcie_relaxed_ordering == 0 &&
1341 (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1342 v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1343 pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1344 } else if (pcie_relaxed_ordering == 1 &&
1345 (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1346 v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1347 pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1348 }
1349 }
1350
1351 sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1352 sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1353 sc->traceq = -1;
1354 mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1355 snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1356 device_get_nameunit(dev));
1357
1358 snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1359 device_get_nameunit(dev));
1360 mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1361 t4_add_adapter(sc);
1362
1363 mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1364 TAILQ_INIT(&sc->sfl);
1365 callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1366
1367 mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1368
1369 sc->policy = NULL;
1370 rw_init(&sc->policy_lock, "connection offload policy");
1371
1372 callout_init(&sc->ktls_tick, 1);
1373
1374 callout_init(&sc->cal_callout, 1);
1375
1376 refcount_init(&sc->vxlan_refcount, 0);
1377
1378 TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1379 TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1380
1381 sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1382 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1383 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1384 sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1385 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1386 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1387
1388 rc = t4_map_bars_0_and_4(sc);
1389 if (rc != 0)
1390 goto done; /* error message displayed already */
1391
1392 memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1393 memset(sc->port_map, 0xff, sizeof(sc->port_map));
1394
1395 /* Prepare the adapter for operation. */
1396 buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1397 rc = -t4_prep_adapter(sc, buf);
1398 free(buf, M_CXGBE);
1399 if (rc != 0) {
1400 device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1401 goto done;
1402 }
1403
1404 /*
1405 * This is the real PF# to which we're attaching. Works from within PCI
1406 * passthrough environments too, where pci_get_function() could return a
1407 * different PF# depending on the passthrough configuration. We need to
1408 * use the real PF# in all our communication with the firmware.
1409 */
1410 j = t4_read_reg(sc, A_PL_WHOAMI);
1411 sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1412 sc->mbox = sc->pf;
1413
1414 t4_init_devnames(sc);
1415 if (sc->names == NULL) {
1416 rc = ENOTSUP;
1417 goto done; /* error message displayed already */
1418 }
1419
1420 /*
1421 * Do this really early, with the memory windows set up even before the
1422 * character device. The userland tool's register i/o and mem read
1423 * will work even in "recovery mode".
1424 */
1425 setup_memwin(sc);
1426 if (t4_init_devlog_ncores_params(sc, 0) == 0)
1427 fixup_devlog_params(sc);
1428 make_dev_args_init(&mda);
1429 mda.mda_devsw = &t4_cdevsw;
1430 mda.mda_uid = UID_ROOT;
1431 mda.mda_gid = GID_WHEEL;
1432 mda.mda_mode = 0600;
1433 mda.mda_si_drv1 = sc;
1434 rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1435 if (rc != 0)
1436 device_printf(dev, "failed to create nexus char device: %d.\n",
1437 rc);
1438
1439 /* Go no further if recovery mode has been requested. */
1440 if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1441 device_printf(dev, "recovery mode.\n");
1442 goto done;
1443 }
1444
1445 #if defined(__i386__)
1446 if ((cpu_feature & CPUID_CX8) == 0) {
1447 device_printf(dev, "64 bit atomics not available.\n");
1448 rc = ENOTSUP;
1449 goto done;
1450 }
1451 #endif
1452
1453 /* Contact the firmware and try to become the master driver. */
1454 rc = contact_firmware(sc);
1455 if (rc != 0)
1456 goto done; /* error message displayed already */
1457 MPASS(sc->flags & FW_OK);
1458
1459 rc = get_params__pre_init(sc);
1460 if (rc != 0)
1461 goto done; /* error message displayed already */
1462
1463 if (sc->flags & MASTER_PF) {
1464 rc = partition_resources(sc);
1465 if (rc != 0)
1466 goto done; /* error message displayed already */
1467 }
1468
1469 rc = get_params__post_init(sc);
1470 if (rc != 0)
1471 goto done; /* error message displayed already */
1472
1473 rc = set_params__post_init(sc);
1474 if (rc != 0)
1475 goto done; /* error message displayed already */
1476
1477 rc = t4_map_bar_2(sc);
1478 if (rc != 0)
1479 goto done; /* error message displayed already */
1480
1481 rc = t4_adj_doorbells(sc);
1482 if (rc != 0)
1483 goto done; /* error message displayed already */
1484
1485 rc = t4_create_dma_tag(sc);
1486 if (rc != 0)
1487 goto done; /* error message displayed already */
1488
1489 /*
1490 * First pass over all the ports - allocate VIs and initialize some
1491 * basic parameters like mac address, port type, etc.
1492 */
1493 for_each_port(sc, i) {
1494 struct port_info *pi;
1495
1496 pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1497 sc->port[i] = pi;
1498
1499 /* These must be set before t4_port_init */
1500 pi->adapter = sc;
1501 pi->port_id = i;
1502 /*
1503 * XXX: vi[0] is special so we can't delay this allocation until
1504 * pi->nvi's final value is known.
1505 */
1506 pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1507 M_ZERO | M_WAITOK);
1508
1509 /*
1510 * Allocate the "main" VI and initialize parameters
1511 * like mac addr.
1512 */
1513 rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1514 if (rc != 0) {
1515 device_printf(dev, "unable to initialize port %d: %d\n",
1516 i, rc);
1517 free(pi->vi, M_CXGBE);
1518 free(pi, M_CXGBE);
1519 sc->port[i] = NULL;
1520 goto done;
1521 }
1522
1523 if (is_bt(pi->port_type))
1524 setbit(&sc->bt_map, pi->hw_port);
1525 else
1526 MPASS(!isset(&sc->bt_map, pi->hw_port));
1527
1528 snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1529 device_get_nameunit(dev), i);
1530 mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1531 for (j = 0; j < sc->params.tp.lb_nchan; j++)
1532 sc->chan_map[pi->tx_chan + j] = i;
1533 sc->port_map[pi->hw_port] = i;
1534
1535 /*
1536 * The MPS counter for FCS errors doesn't work correctly on the
1537 * T6 so we use the MAC counter here. Which MAC is in use
1538 * depends on the link settings which will be known when the
1539 * link comes up.
1540 */
1541 if (is_t6(sc))
1542 pi->fcs_reg = -1;
1543 else
1544 pi->fcs_reg = A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L;
1545 pi->fcs_base = 0;
1546
1547 /* All VIs on this port share this media. */
1548 ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1549 cxgbe_media_status);
1550
1551 PORT_LOCK(pi);
1552 init_link_config(pi);
1553 fixup_link_config(pi);
1554 build_medialist(pi);
1555 if (fixed_ifmedia(pi))
1556 pi->flags |= FIXED_IFMEDIA;
1557 PORT_UNLOCK(pi);
1558
1559 pi->dev = device_add_child(dev, sc->names->ifnet_name,
1560 t4_ifnet_unit(sc, pi));
1561 if (pi->dev == NULL) {
1562 device_printf(dev,
1563 "failed to add device for port %d.\n", i);
1564 rc = ENXIO;
1565 goto done;
1566 }
1567 pi->vi[0].dev = pi->dev;
1568 device_set_softc(pi->dev, pi);
1569 }
1570
1571 /*
1572 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1573 */
1574 nports = sc->params.nports;
1575 rc = cfg_itype_and_nqueues(sc, &iaq);
1576 if (rc != 0)
1577 goto done; /* error message displayed already */
1578
1579 num_vis = iaq.num_vis;
1580 sc->intr_type = iaq.intr_type;
1581 sc->intr_count = iaq.nirq;
1582
1583 s = &sc->sge;
1584 s->nctrlq = max(sc->params.nports, sc->params.ncores);
1585 s->nrxq = nports * iaq.nrxq;
1586 s->ntxq = nports * iaq.ntxq;
1587 if (num_vis > 1) {
1588 s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1589 s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1590 }
1591 s->neq = s->ntxq + s->nrxq; /* the free list in an rxq is an eq */
1592 s->neq += nports; /* ctrl queues: 1 per port */
1593 s->niq = s->nrxq + 1; /* 1 extra for firmware event queue */
1594 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1595 if (is_offload(sc) || is_ethoffload(sc)) {
1596 s->nofldtxq = nports * iaq.nofldtxq;
1597 if (num_vis > 1)
1598 s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1599 s->neq += s->nofldtxq;
1600
1601 s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1602 M_CXGBE, M_ZERO | M_WAITOK);
1603 }
1604 #endif
1605 #ifdef TCP_OFFLOAD
1606 if (is_offload(sc)) {
1607 s->nofldrxq = nports * iaq.nofldrxq;
1608 if (num_vis > 1)
1609 s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1610 s->neq += s->nofldrxq; /* free list */
1611 s->niq += s->nofldrxq;
1612
1613 s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1614 M_CXGBE, M_ZERO | M_WAITOK);
1615 }
1616 #endif
1617 #ifdef DEV_NETMAP
1618 s->nnmrxq = 0;
1619 s->nnmtxq = 0;
1620 if (t4_native_netmap & NN_MAIN_VI) {
1621 s->nnmrxq += nports * iaq.nnmrxq;
1622 s->nnmtxq += nports * iaq.nnmtxq;
1623 }
1624 if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1625 s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1626 s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1627 }
1628 s->neq += s->nnmtxq + s->nnmrxq;
1629 s->niq += s->nnmrxq;
1630
1631 s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1632 M_CXGBE, M_ZERO | M_WAITOK);
1633 s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1634 M_CXGBE, M_ZERO | M_WAITOK);
1635 #endif
1636 MPASS(s->niq <= s->iqmap_sz);
1637 MPASS(s->neq <= s->eqmap_sz);
1638
1639 s->ctrlq = malloc(s->nctrlq * sizeof(struct sge_wrq), M_CXGBE,
1640 M_ZERO | M_WAITOK);
1641 s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1642 M_ZERO | M_WAITOK);
1643 s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1644 M_ZERO | M_WAITOK);
1645 s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1646 M_ZERO | M_WAITOK);
1647 s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1648 M_ZERO | M_WAITOK);
1649
1650 sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1651 M_ZERO | M_WAITOK);
1652
1653 t4_init_l2t(sc, M_WAITOK);
1654 t4_init_smt(sc, M_WAITOK);
1655 t4_init_tx_sched(sc);
1656 t4_init_atid_table(sc);
1657 #ifdef RATELIMIT
1658 t4_init_etid_table(sc);
1659 #endif
1660 #ifdef INET6
1661 t4_init_clip_table(sc);
1662 #endif
1663 if (sc->vres.key.size != 0)
1664 sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1665 sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1666 t4_init_tpt(sc);
1667
1668 /*
1669 * Second pass over the ports. This time we know the number of rx and
1670 * tx queues that each port should get.
1671 */
1672 rqidx = tqidx = 0;
1673 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1674 ofld_tqidx = 0;
1675 #endif
1676 #ifdef TCP_OFFLOAD
1677 ofld_rqidx = 0;
1678 #endif
1679 #ifdef DEV_NETMAP
1680 nm_rqidx = nm_tqidx = 0;
1681 #endif
1682 for_each_port(sc, i) {
1683 struct port_info *pi = sc->port[i];
1684 struct vi_info *vi;
1685
1686 if (pi == NULL)
1687 continue;
1688
1689 pi->nvi = num_vis;
1690 for_each_vi(pi, j, vi) {
1691 vi->pi = pi;
1692 vi->adapter = sc;
1693 vi->first_intr = -1;
1694 vi->qsize_rxq = t4_qsize_rxq;
1695 vi->qsize_txq = t4_qsize_txq;
1696
1697 vi->first_rxq = rqidx;
1698 vi->first_txq = tqidx;
1699 vi->tmr_idx = t4_tmr_idx;
1700 vi->pktc_idx = t4_pktc_idx;
1701 vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1702 vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1703
1704 rqidx += vi->nrxq;
1705 tqidx += vi->ntxq;
1706
1707 if (j == 0 && vi->ntxq > 1)
1708 vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1709 else
1710 vi->rsrv_noflowq = 0;
1711
1712 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1713 vi->first_ofld_txq = ofld_tqidx;
1714 vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1715 ofld_tqidx += vi->nofldtxq;
1716 #endif
1717 #ifdef TCP_OFFLOAD
1718 vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1719 vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1720 vi->first_ofld_rxq = ofld_rqidx;
1721 vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1722
1723 ofld_rqidx += vi->nofldrxq;
1724 #endif
1725 #ifdef DEV_NETMAP
1726 vi->first_nm_rxq = nm_rqidx;
1727 vi->first_nm_txq = nm_tqidx;
1728 if (j == 0) {
1729 vi->nnmrxq = iaq.nnmrxq;
1730 vi->nnmtxq = iaq.nnmtxq;
1731 } else {
1732 vi->nnmrxq = iaq.nnmrxq_vi;
1733 vi->nnmtxq = iaq.nnmtxq_vi;
1734 }
1735 nm_rqidx += vi->nnmrxq;
1736 nm_tqidx += vi->nnmtxq;
1737 #endif
1738 }
1739 }
1740
1741 rc = t4_setup_intr_handlers(sc);
1742 if (rc != 0) {
1743 device_printf(dev,
1744 "failed to setup interrupt handlers: %d\n", rc);
1745 goto done;
1746 }
1747
1748 bus_identify_children(dev);
1749
1750 /*
1751 * Ensure thread-safe mailbox access (in debug builds).
1752 *
1753 * So far this was the only thread accessing the mailbox but various
1754 * ifnets and sysctls are about to be created and their handlers/ioctls
1755 * will access the mailbox from different threads.
1756 */
1757 sc->flags |= CHK_MBOX_ACCESS;
1758
1759 bus_attach_children(dev);
1760 t4_calibration_start(sc);
1761
1762 device_printf(dev,
1763 "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1764 sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1765 sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1766 (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1767 sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1768
1769 t4_set_desc(sc);
1770
1771 notify_siblings(dev, 0);
1772
1773 done:
1774 if (rc != 0 && sc->cdev) {
1775 /* cdev was created and so cxgbetool works; recover that way. */
1776 device_printf(dev,
1777 "error during attach, adapter is now in recovery mode.\n");
1778 rc = 0;
1779 }
1780
1781 if (rc != 0)
1782 t4_detach_common(dev);
1783 else
1784 t4_sysctls(sc);
1785
1786 return (rc);
1787 }
1788
1789 static int
t4_child_location(device_t bus,device_t dev,struct sbuf * sb)1790 t4_child_location(device_t bus, device_t dev, struct sbuf *sb)
1791 {
1792 struct adapter *sc;
1793 struct port_info *pi;
1794 int i;
1795
1796 sc = device_get_softc(bus);
1797 for_each_port(sc, i) {
1798 pi = sc->port[i];
1799 if (pi != NULL && pi->dev == dev) {
1800 sbuf_printf(sb, "port=%d", pi->port_id);
1801 break;
1802 }
1803 }
1804 return (0);
1805 }
1806
1807 static int
t4_ready(device_t dev)1808 t4_ready(device_t dev)
1809 {
1810 struct adapter *sc;
1811
1812 sc = device_get_softc(dev);
1813 if (sc->flags & FW_OK)
1814 return (0);
1815 return (ENXIO);
1816 }
1817
1818 static int
t4_read_port_device(device_t dev,int port,device_t * child)1819 t4_read_port_device(device_t dev, int port, device_t *child)
1820 {
1821 struct adapter *sc;
1822 struct port_info *pi;
1823
1824 sc = device_get_softc(dev);
1825 if (port < 0 || port >= MAX_NPORTS)
1826 return (EINVAL);
1827 pi = sc->port[port];
1828 if (pi == NULL || pi->dev == NULL)
1829 return (ENXIO);
1830 *child = pi->dev;
1831 return (0);
1832 }
1833
1834 static int
notify_siblings(device_t dev,int detaching)1835 notify_siblings(device_t dev, int detaching)
1836 {
1837 device_t sibling;
1838 int error, i;
1839
1840 error = 0;
1841 for (i = 0; i < PCI_FUNCMAX; i++) {
1842 if (i == pci_get_function(dev))
1843 continue;
1844 sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1845 pci_get_slot(dev), i);
1846 if (sibling == NULL || !device_is_attached(sibling))
1847 continue;
1848 if (detaching)
1849 error = T4_DETACH_CHILD(sibling);
1850 else
1851 (void)T4_ATTACH_CHILD(sibling);
1852 if (error)
1853 break;
1854 }
1855 return (error);
1856 }
1857
1858 /*
1859 * Idempotent
1860 */
1861 static int
t4_detach(device_t dev)1862 t4_detach(device_t dev)
1863 {
1864 int rc;
1865
1866 rc = notify_siblings(dev, 1);
1867 if (rc) {
1868 device_printf(dev,
1869 "failed to detach sibling devices: %d\n", rc);
1870 return (rc);
1871 }
1872
1873 return (t4_detach_common(dev));
1874 }
1875
1876 int
t4_detach_common(device_t dev)1877 t4_detach_common(device_t dev)
1878 {
1879 struct adapter *sc;
1880 struct port_info *pi;
1881 int i, rc;
1882
1883 sc = device_get_softc(dev);
1884
1885 #ifdef TCP_OFFLOAD
1886 rc = deactivate_all_uld(sc);
1887 if (rc) {
1888 device_printf(dev,
1889 "failed to detach upper layer drivers: %d\n", rc);
1890 return (rc);
1891 }
1892 #endif
1893
1894 if (sc->cdev) {
1895 destroy_dev(sc->cdev);
1896 sc->cdev = NULL;
1897 }
1898
1899 sx_xlock(&t4_list_lock);
1900 SLIST_REMOVE(&t4_list, sc, adapter, link);
1901 sx_xunlock(&t4_list_lock);
1902
1903 sc->flags &= ~CHK_MBOX_ACCESS;
1904 if (sc->flags & FULL_INIT_DONE) {
1905 if (!(sc->flags & IS_VF))
1906 t4_intr_disable(sc);
1907 }
1908
1909 if (device_is_attached(dev)) {
1910 rc = bus_detach_children(dev);
1911 if (rc) {
1912 device_printf(dev,
1913 "failed to detach child devices: %d\n", rc);
1914 return (rc);
1915 }
1916 }
1917
1918 for (i = 0; i < sc->intr_count; i++)
1919 t4_free_irq(sc, &sc->irq[i]);
1920
1921 if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1922 t4_free_tx_sched(sc);
1923
1924 for (i = 0; i < MAX_NPORTS; i++) {
1925 pi = sc->port[i];
1926 if (pi) {
1927 t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1928
1929 mtx_destroy(&pi->pi_lock);
1930 free(pi->vi, M_CXGBE);
1931 free(pi, M_CXGBE);
1932 }
1933 }
1934 callout_stop(&sc->cal_callout);
1935 callout_drain(&sc->cal_callout);
1936 device_delete_children(dev);
1937 sysctl_ctx_free(&sc->ctx);
1938 adapter_full_uninit(sc);
1939
1940 if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1941 t4_fw_bye(sc, sc->mbox);
1942
1943 if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1944 pci_release_msi(dev);
1945
1946 if (sc->regs_res)
1947 bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1948 sc->regs_res);
1949
1950 if (sc->udbs_res)
1951 bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1952 sc->udbs_res);
1953
1954 if (sc->msix_res)
1955 bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1956 sc->msix_res);
1957
1958 if (sc->l2t)
1959 t4_free_l2t(sc);
1960 if (sc->smt)
1961 t4_free_smt(sc->smt);
1962 t4_free_atid_table(sc);
1963 #ifdef RATELIMIT
1964 t4_free_etid_table(sc);
1965 #endif
1966 if (sc->key_map)
1967 vmem_destroy(sc->key_map);
1968 t4_free_tpt(sc);
1969 #ifdef INET6
1970 t4_destroy_clip_table(sc);
1971 #endif
1972
1973 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1974 free(sc->sge.ofld_txq, M_CXGBE);
1975 #endif
1976 #ifdef TCP_OFFLOAD
1977 free(sc->sge.ofld_rxq, M_CXGBE);
1978 #endif
1979 #ifdef DEV_NETMAP
1980 free(sc->sge.nm_rxq, M_CXGBE);
1981 free(sc->sge.nm_txq, M_CXGBE);
1982 #endif
1983 free(sc->irq, M_CXGBE);
1984 free(sc->sge.rxq, M_CXGBE);
1985 free(sc->sge.txq, M_CXGBE);
1986 free(sc->sge.ctrlq, M_CXGBE);
1987 free(sc->sge.iqmap, M_CXGBE);
1988 free(sc->sge.eqmap, M_CXGBE);
1989 free(sc->tids.ftid_tab, M_CXGBE);
1990 free(sc->tids.hpftid_tab, M_CXGBE);
1991 free_hftid_hash(&sc->tids);
1992 free(sc->tids.tid_tab, M_CXGBE);
1993 t4_destroy_dma_tag(sc);
1994
1995 callout_drain(&sc->ktls_tick);
1996 callout_drain(&sc->sfl_callout);
1997 if (mtx_initialized(&sc->tids.ftid_lock)) {
1998 mtx_destroy(&sc->tids.ftid_lock);
1999 cv_destroy(&sc->tids.ftid_cv);
2000 }
2001 if (mtx_initialized(&sc->tids.atid_lock))
2002 mtx_destroy(&sc->tids.atid_lock);
2003 if (mtx_initialized(&sc->ifp_lock))
2004 mtx_destroy(&sc->ifp_lock);
2005
2006 if (rw_initialized(&sc->policy_lock)) {
2007 rw_destroy(&sc->policy_lock);
2008 #ifdef TCP_OFFLOAD
2009 if (sc->policy != NULL)
2010 free_offload_policy(sc->policy);
2011 #endif
2012 }
2013
2014 for (i = 0; i < NUM_MEMWIN; i++) {
2015 struct memwin *mw = &sc->memwin[i];
2016
2017 if (rw_initialized(&mw->mw_lock))
2018 rw_destroy(&mw->mw_lock);
2019 }
2020
2021 mtx_destroy(&sc->sfl_lock);
2022 mtx_destroy(&sc->reg_lock);
2023 mtx_destroy(&sc->sc_lock);
2024
2025 bzero(sc, sizeof(*sc));
2026
2027 return (0);
2028 }
2029
2030 static inline int
stop_adapter(struct adapter * sc)2031 stop_adapter(struct adapter *sc)
2032 {
2033 struct port_info *pi;
2034 int i;
2035
2036 if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
2037 CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
2038 __func__, curthread, sc->flags, sc->error_flags);
2039 return (EALREADY);
2040 }
2041 CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
2042 sc->flags, sc->error_flags);
2043 t4_shutdown_adapter(sc);
2044 for_each_port(sc, i) {
2045 pi = sc->port[i];
2046 if (pi == NULL)
2047 continue;
2048 PORT_LOCK(pi);
2049 if (pi->up_vis > 0 && pi->link_cfg.link_ok) {
2050 /*
2051 * t4_shutdown_adapter has already shut down all the
2052 * PHYs but it also disables interrupts and DMA so there
2053 * won't be a link interrupt. Update the state manually
2054 * if the link was up previously and inform the kernel.
2055 */
2056 pi->link_cfg.link_ok = false;
2057 t4_os_link_changed(pi);
2058 }
2059 PORT_UNLOCK(pi);
2060 }
2061
2062 return (0);
2063 }
2064
2065 static inline int
restart_adapter(struct adapter * sc)2066 restart_adapter(struct adapter *sc)
2067 {
2068 uint32_t val;
2069
2070 if (!atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
2071 CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
2072 __func__, curthread, sc->flags, sc->error_flags);
2073 return (EALREADY);
2074 }
2075 CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
2076 sc->flags, sc->error_flags);
2077
2078 MPASS(hw_off_limits(sc));
2079 MPASS((sc->flags & FW_OK) == 0);
2080 MPASS((sc->flags & MASTER_PF) == 0);
2081 MPASS(sc->reset_thread == NULL);
2082
2083 /*
2084 * The adapter is supposed to be back on PCIE with its config space and
2085 * BARs restored to their state before reset. Register access via
2086 * t4_read_reg BAR0 should just work.
2087 */
2088 sc->reset_thread = curthread;
2089 val = t4_read_reg(sc, A_PL_WHOAMI);
2090 if (val == 0xffffffff || val == 0xeeeeeeee) {
2091 CH_ERR(sc, "%s: device registers not readable.\n", __func__);
2092 sc->reset_thread = NULL;
2093 atomic_set_int(&sc->error_flags, ADAP_STOPPED);
2094 return (ENXIO);
2095 }
2096 atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR);
2097 atomic_add_int(&sc->incarnation, 1);
2098 atomic_add_int(&sc->num_resets, 1);
2099
2100 return (0);
2101 }
2102
2103 static inline void
set_adapter_hwstatus(struct adapter * sc,const bool usable)2104 set_adapter_hwstatus(struct adapter *sc, const bool usable)
2105 {
2106 if (usable) {
2107 /* Must be marked reusable by the designated thread. */
2108 ASSERT_SYNCHRONIZED_OP(sc);
2109 MPASS(sc->reset_thread == curthread);
2110 mtx_lock(&sc->reg_lock);
2111 atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
2112 mtx_unlock(&sc->reg_lock);
2113 } else {
2114 /* Mark the adapter totally off limits. */
2115 begin_synchronized_op(sc, NULL, SLEEP_OK, "t4hwsts");
2116 mtx_lock(&sc->reg_lock);
2117 atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
2118 mtx_unlock(&sc->reg_lock);
2119 sc->flags &= ~(FW_OK | MASTER_PF);
2120 sc->reset_thread = NULL;
2121 end_synchronized_op(sc, 0);
2122 }
2123 }
2124
2125 static int
stop_lld(struct adapter * sc)2126 stop_lld(struct adapter *sc)
2127 {
2128 struct port_info *pi;
2129 struct vi_info *vi;
2130 if_t ifp;
2131 struct sge_rxq *rxq;
2132 struct sge_txq *txq;
2133 struct sge_wrq *wrq;
2134 #ifdef TCP_OFFLOAD
2135 struct sge_ofld_rxq *ofld_rxq;
2136 #endif
2137 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2138 struct sge_ofld_txq *ofld_txq;
2139 #endif
2140 int rc, i, j, k;
2141
2142 /*
2143 * XXX: Can there be a synch_op in progress that will hang because
2144 * hardware has been stopped? We'll hang too and the solution will be
2145 * to use a version of begin_synch_op that wakes up existing synch_op
2146 * with errors. Maybe stop_adapter should do this wakeup?
2147 *
2148 * I don't think any synch_op could get stranded waiting for DMA or
2149 * interrupt so I think we're okay here. Remove this comment block
2150 * after testing.
2151 */
2152 rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4slld");
2153 if (rc != 0)
2154 return (ENXIO);
2155
2156 /* Quiesce all activity. */
2157 for_each_port(sc, i) {
2158 pi = sc->port[i];
2159 if (pi == NULL)
2160 continue;
2161 pi->vxlan_tcam_entry = false;
2162 for_each_vi(pi, j, vi) {
2163 vi->xact_addr_filt = -1;
2164 mtx_lock(&vi->tick_mtx);
2165 vi->flags |= VI_SKIP_STATS;
2166 mtx_unlock(&vi->tick_mtx);
2167 if (!(vi->flags & VI_INIT_DONE))
2168 continue;
2169
2170 ifp = vi->ifp;
2171 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2172 mtx_lock(&vi->tick_mtx);
2173 callout_stop(&vi->tick);
2174 mtx_unlock(&vi->tick_mtx);
2175 callout_drain(&vi->tick);
2176 }
2177
2178 /*
2179 * Note that the HW is not available.
2180 */
2181 for_each_txq(vi, k, txq) {
2182 TXQ_LOCK(txq);
2183 txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2184 TXQ_UNLOCK(txq);
2185 }
2186 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2187 for_each_ofld_txq(vi, k, ofld_txq) {
2188 TXQ_LOCK(&ofld_txq->wrq);
2189 ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2190 TXQ_UNLOCK(&ofld_txq->wrq);
2191 }
2192 #endif
2193 for_each_rxq(vi, k, rxq) {
2194 rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2195 }
2196 #if defined(TCP_OFFLOAD)
2197 for_each_ofld_rxq(vi, k, ofld_rxq) {
2198 ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2199 }
2200 #endif
2201
2202 quiesce_vi(vi);
2203 }
2204
2205 if (sc->flags & FULL_INIT_DONE) {
2206 /* Control queue */
2207 wrq = &sc->sge.ctrlq[i];
2208 TXQ_LOCK(wrq);
2209 wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2210 TXQ_UNLOCK(wrq);
2211 quiesce_wrq(wrq);
2212 }
2213
2214 if (pi->flags & HAS_TRACEQ) {
2215 pi->flags &= ~HAS_TRACEQ;
2216 sc->traceq = -1;
2217 sc->tracer_valid = 0;
2218 sc->tracer_enabled = 0;
2219 }
2220 }
2221 if (sc->flags & FULL_INIT_DONE) {
2222 /* Firmware event queue */
2223 sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2224 quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2225 }
2226
2227 /* Stop calibration */
2228 callout_stop(&sc->cal_callout);
2229 callout_drain(&sc->cal_callout);
2230
2231 if (t4_clock_gate_on_suspend) {
2232 t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2233 F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2234 F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2235 }
2236
2237 end_synchronized_op(sc, 0);
2238
2239 stop_atid_allocator(sc);
2240 t4_stop_l2t(sc);
2241
2242 return (rc);
2243 }
2244
2245 int
suspend_adapter(struct adapter * sc)2246 suspend_adapter(struct adapter *sc)
2247 {
2248 stop_adapter(sc);
2249 stop_lld(sc);
2250 #ifdef TCP_OFFLOAD
2251 stop_all_uld(sc);
2252 #endif
2253 set_adapter_hwstatus(sc, false);
2254
2255 return (0);
2256 }
2257
2258 static int
t4_suspend(device_t dev)2259 t4_suspend(device_t dev)
2260 {
2261 struct adapter *sc = device_get_softc(dev);
2262 int rc;
2263
2264 CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2265 rc = suspend_adapter(sc);
2266 CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2267
2268 return (rc);
2269 }
2270
2271 struct adapter_pre_reset_state {
2272 u_int flags;
2273 uint16_t nbmcaps;
2274 uint16_t linkcaps;
2275 uint16_t switchcaps;
2276 uint16_t nvmecaps;
2277 uint16_t niccaps;
2278 uint16_t toecaps;
2279 uint16_t rdmacaps;
2280 uint16_t cryptocaps;
2281 uint16_t iscsicaps;
2282 uint16_t fcoecaps;
2283
2284 u_int cfcsum;
2285 char cfg_file[32];
2286
2287 struct adapter_params params;
2288 struct t4_virt_res vres;
2289 struct tid_info tids;
2290 struct sge sge;
2291
2292 int rawf_base;
2293 int nrawf;
2294
2295 };
2296
2297 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2298 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2299 {
2300
2301 ASSERT_SYNCHRONIZED_OP(sc);
2302
2303 o->flags = sc->flags;
2304
2305 o->nbmcaps = sc->nbmcaps;
2306 o->linkcaps = sc->linkcaps;
2307 o->switchcaps = sc->switchcaps;
2308 o->nvmecaps = sc->nvmecaps;
2309 o->niccaps = sc->niccaps;
2310 o->toecaps = sc->toecaps;
2311 o->rdmacaps = sc->rdmacaps;
2312 o->cryptocaps = sc->cryptocaps;
2313 o->iscsicaps = sc->iscsicaps;
2314 o->fcoecaps = sc->fcoecaps;
2315
2316 o->cfcsum = sc->cfcsum;
2317 MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2318 memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2319
2320 o->params = sc->params;
2321 o->vres = sc->vres;
2322 o->tids = sc->tids;
2323 o->sge = sc->sge;
2324
2325 o->rawf_base = sc->rawf_base;
2326 o->nrawf = sc->nrawf;
2327 }
2328
2329 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2330 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2331 {
2332 int rc = 0;
2333
2334 ASSERT_SYNCHRONIZED_OP(sc);
2335
2336 /* Capabilities */
2337 #define COMPARE_CAPS(c) do { \
2338 if (o->c##caps != sc->c##caps) { \
2339 CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2340 sc->c##caps); \
2341 rc = EINVAL; \
2342 } \
2343 } while (0)
2344 COMPARE_CAPS(nbm);
2345 COMPARE_CAPS(link);
2346 COMPARE_CAPS(switch);
2347 COMPARE_CAPS(nvme);
2348 COMPARE_CAPS(nic);
2349 COMPARE_CAPS(toe);
2350 COMPARE_CAPS(rdma);
2351 COMPARE_CAPS(crypto);
2352 COMPARE_CAPS(iscsi);
2353 COMPARE_CAPS(fcoe);
2354 #undef COMPARE_CAPS
2355
2356 /* Firmware config file */
2357 if (o->cfcsum != sc->cfcsum) {
2358 CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2359 o->cfcsum, sc->cfg_file, sc->cfcsum);
2360 rc = EINVAL;
2361 }
2362
2363 #define COMPARE_PARAM(p, name) do { \
2364 if (o->p != sc->p) { \
2365 CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2366 rc = EINVAL; \
2367 } \
2368 } while (0)
2369 COMPARE_PARAM(sge.iq_start, iq_start);
2370 COMPARE_PARAM(sge.eq_start, eq_start);
2371 COMPARE_PARAM(tids.ftid_base, ftid_base);
2372 COMPARE_PARAM(tids.ftid_end, ftid_end);
2373 COMPARE_PARAM(tids.nftids, nftids);
2374 COMPARE_PARAM(vres.l2t.start, l2t_start);
2375 COMPARE_PARAM(vres.l2t.size, l2t_size);
2376 COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2377 COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2378 COMPARE_PARAM(tids.tid_base, tid_base);
2379 COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2380 COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2381 COMPARE_PARAM(tids.nhpftids, nhpftids);
2382 COMPARE_PARAM(rawf_base, rawf_base);
2383 COMPARE_PARAM(nrawf, nrawf);
2384 COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2385 COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2386 COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2387 COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2388 COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2389 COMPARE_PARAM(tids.ntids, ntids);
2390 COMPARE_PARAM(tids.etid_base, etid_base);
2391 COMPARE_PARAM(tids.etid_end, etid_end);
2392 COMPARE_PARAM(tids.netids, netids);
2393 COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2394 COMPARE_PARAM(params.ethoffload, ethoffload);
2395 COMPARE_PARAM(tids.natids, natids);
2396 COMPARE_PARAM(tids.stid_base, stid_base);
2397 COMPARE_PARAM(vres.ddp.start, ddp_start);
2398 COMPARE_PARAM(vres.ddp.size, ddp_size);
2399 COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2400 COMPARE_PARAM(vres.stag.start, stag_start);
2401 COMPARE_PARAM(vres.stag.size, stag_size);
2402 COMPARE_PARAM(vres.rq.start, rq_start);
2403 COMPARE_PARAM(vres.rq.size, rq_size);
2404 COMPARE_PARAM(vres.pbl.start, pbl_start);
2405 COMPARE_PARAM(vres.pbl.size, pbl_size);
2406 COMPARE_PARAM(vres.qp.start, qp_start);
2407 COMPARE_PARAM(vres.qp.size, qp_size);
2408 COMPARE_PARAM(vres.cq.start, cq_start);
2409 COMPARE_PARAM(vres.cq.size, cq_size);
2410 COMPARE_PARAM(vres.ocq.start, ocq_start);
2411 COMPARE_PARAM(vres.ocq.size, ocq_size);
2412 COMPARE_PARAM(vres.srq.start, srq_start);
2413 COMPARE_PARAM(vres.srq.size, srq_size);
2414 COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2415 COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2416 COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2417 COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2418 COMPARE_PARAM(vres.key.start, key_start);
2419 COMPARE_PARAM(vres.key.size, key_size);
2420 #undef COMPARE_PARAM
2421
2422 return (rc);
2423 }
2424
2425 static int
restart_lld(struct adapter * sc)2426 restart_lld(struct adapter *sc)
2427 {
2428 struct adapter_pre_reset_state *old_state = NULL;
2429 struct port_info *pi;
2430 struct vi_info *vi;
2431 if_t ifp;
2432 struct sge_txq *txq;
2433 int rc, i, j, k;
2434
2435 rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rlld");
2436 if (rc != 0)
2437 return (ENXIO);
2438
2439 /* Restore memory window. */
2440 setup_memwin(sc);
2441
2442 /* Go no further if recovery mode has been requested. */
2443 if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2444 CH_ALERT(sc, "%s: recovery mode during restart.\n", __func__);
2445 rc = 0;
2446 set_adapter_hwstatus(sc, true);
2447 goto done;
2448 }
2449
2450 old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2451 save_caps_and_params(sc, old_state);
2452
2453 /* Reestablish contact with firmware and become the primary PF. */
2454 rc = contact_firmware(sc);
2455 if (rc != 0)
2456 goto done; /* error message displayed already */
2457 MPASS(sc->flags & FW_OK);
2458
2459 if (sc->flags & MASTER_PF) {
2460 rc = partition_resources(sc);
2461 if (rc != 0)
2462 goto done; /* error message displayed already */
2463 }
2464
2465 rc = get_params__post_init(sc);
2466 if (rc != 0)
2467 goto done; /* error message displayed already */
2468
2469 rc = set_params__post_init(sc);
2470 if (rc != 0)
2471 goto done; /* error message displayed already */
2472
2473 rc = compare_caps_and_params(sc, old_state);
2474 if (rc != 0)
2475 goto done; /* error message displayed already */
2476
2477 for_each_port(sc, i) {
2478 pi = sc->port[i];
2479 MPASS(pi != NULL);
2480 MPASS(pi->vi != NULL);
2481 MPASS(pi->vi[0].dev == pi->dev);
2482
2483 rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2484 if (rc != 0) {
2485 CH_ERR(sc,
2486 "failed to re-initialize port %d: %d\n", i, rc);
2487 goto done;
2488 }
2489 MPASS(sc->chan_map[pi->tx_chan] == i);
2490
2491 PORT_LOCK(pi);
2492 fixup_link_config(pi);
2493 build_medialist(pi);
2494 PORT_UNLOCK(pi);
2495 for_each_vi(pi, j, vi) {
2496 if (IS_MAIN_VI(vi))
2497 continue;
2498 rc = alloc_extra_vi(sc, pi, vi);
2499 if (rc != 0) {
2500 CH_ERR(vi,
2501 "failed to re-allocate extra VI: %d\n", rc);
2502 goto done;
2503 }
2504 }
2505 }
2506
2507 /*
2508 * Interrupts and queues are about to be enabled and other threads will
2509 * want to access the hardware too. It is safe to do so. Note that
2510 * this thread is still in the middle of a synchronized_op.
2511 */
2512 set_adapter_hwstatus(sc, true);
2513
2514 if (sc->flags & FULL_INIT_DONE) {
2515 rc = adapter_full_init(sc);
2516 if (rc != 0) {
2517 CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2518 goto done;
2519 }
2520
2521 if (sc->vxlan_refcount > 0)
2522 enable_vxlan_rx(sc);
2523
2524 for_each_port(sc, i) {
2525 pi = sc->port[i];
2526 for_each_vi(pi, j, vi) {
2527 mtx_lock(&vi->tick_mtx);
2528 vi->flags &= ~VI_SKIP_STATS;
2529 mtx_unlock(&vi->tick_mtx);
2530 if (!(vi->flags & VI_INIT_DONE))
2531 continue;
2532 rc = vi_full_init(vi);
2533 if (rc != 0) {
2534 CH_ERR(vi, "failed to re-initialize "
2535 "interface: %d\n", rc);
2536 goto done;
2537 }
2538 if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
2539 sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
2540 t4_set_trace_rss_control(sc, pi->tx_chan, sc->traceq);
2541 pi->flags |= HAS_TRACEQ;
2542 }
2543
2544 ifp = vi->ifp;
2545 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2546 continue;
2547 /*
2548 * Note that we do not setup multicast addresses
2549 * in the first pass. This ensures that the
2550 * unicast DMACs for all VIs on all ports get an
2551 * MPS TCAM entry.
2552 */
2553 rc = update_mac_settings(ifp, XGMAC_ALL &
2554 ~XGMAC_MCADDRS);
2555 if (rc != 0) {
2556 CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2557 goto done;
2558 }
2559 rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2560 true);
2561 if (rc != 0) {
2562 CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2563 goto done;
2564 }
2565 for_each_txq(vi, k, txq) {
2566 TXQ_LOCK(txq);
2567 txq->eq.flags |= EQ_ENABLED;
2568 TXQ_UNLOCK(txq);
2569 }
2570 mtx_lock(&vi->tick_mtx);
2571 callout_schedule(&vi->tick, hz);
2572 mtx_unlock(&vi->tick_mtx);
2573 }
2574 PORT_LOCK(pi);
2575 if (pi->up_vis > 0) {
2576 t4_update_port_info(pi);
2577 fixup_link_config(pi);
2578 build_medialist(pi);
2579 apply_link_config(pi);
2580 if (pi->link_cfg.link_ok)
2581 t4_os_link_changed(pi);
2582 }
2583 PORT_UNLOCK(pi);
2584 }
2585
2586 /* Now reprogram the L2 multicast addresses. */
2587 for_each_port(sc, i) {
2588 pi = sc->port[i];
2589 for_each_vi(pi, j, vi) {
2590 if (!(vi->flags & VI_INIT_DONE))
2591 continue;
2592 ifp = vi->ifp;
2593 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2594 continue;
2595 rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2596 if (rc != 0) {
2597 CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2598 rc = 0; /* carry on */
2599 }
2600 }
2601 }
2602 }
2603
2604 /* Reset all calibration */
2605 t4_calibration_start(sc);
2606 done:
2607 end_synchronized_op(sc, 0);
2608 free(old_state, M_CXGBE);
2609
2610 restart_atid_allocator(sc);
2611 t4_restart_l2t(sc);
2612
2613 return (rc);
2614 }
2615
2616 int
resume_adapter(struct adapter * sc)2617 resume_adapter(struct adapter *sc)
2618 {
2619 restart_adapter(sc);
2620 restart_lld(sc);
2621 #ifdef TCP_OFFLOAD
2622 restart_all_uld(sc);
2623 #endif
2624 return (0);
2625 }
2626
2627 static int
t4_resume(device_t dev)2628 t4_resume(device_t dev)
2629 {
2630 struct adapter *sc = device_get_softc(dev);
2631 int rc;
2632
2633 CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2634 rc = resume_adapter(sc);
2635 CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2636
2637 return (rc);
2638 }
2639
2640 static int
t4_reset_prepare(device_t dev,device_t child)2641 t4_reset_prepare(device_t dev, device_t child)
2642 {
2643 struct adapter *sc = device_get_softc(dev);
2644
2645 CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2646 return (0);
2647 }
2648
2649 static int
t4_reset_post(device_t dev,device_t child)2650 t4_reset_post(device_t dev, device_t child)
2651 {
2652 struct adapter *sc = device_get_softc(dev);
2653
2654 CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2655 return (0);
2656 }
2657
2658 static int
reset_adapter_with_pl_rst(struct adapter * sc)2659 reset_adapter_with_pl_rst(struct adapter *sc)
2660 {
2661 /* This is a t4_write_reg without the hw_off_limits check. */
2662 MPASS(sc->error_flags & HW_OFF_LIMITS);
2663 bus_write_4(sc->regs_res, A_PL_RST,
2664 F_PIORSTMODE | F_PIORST | F_AUTOPCIEPAUSE);
2665 pause("pl_rst", 1 * hz); /* Wait 1s for reset */
2666 return (0);
2667 }
2668
2669 static int
reset_adapter_with_pcie_sbr(struct adapter * sc)2670 reset_adapter_with_pcie_sbr(struct adapter *sc)
2671 {
2672 device_t pdev = device_get_parent(sc->dev);
2673 device_t gpdev = device_get_parent(pdev);
2674 device_t *children;
2675 int rc, i, lcap, lsta, nchildren;
2676 uint32_t v;
2677
2678 rc = pci_find_cap(gpdev, PCIY_EXPRESS, &v);
2679 if (rc != 0) {
2680 CH_ERR(sc, "%s: pci_find_cap(%s, pcie) failed: %d\n", __func__,
2681 device_get_nameunit(gpdev), rc);
2682 return (ENOTSUP);
2683 }
2684 lcap = v + PCIER_LINK_CAP;
2685 lsta = v + PCIER_LINK_STA;
2686
2687 nchildren = 0;
2688 device_get_children(pdev, &children, &nchildren);
2689 for (i = 0; i < nchildren; i++)
2690 pci_save_state(children[i]);
2691 v = pci_read_config(gpdev, PCIR_BRIDGECTL_1, 2);
2692 pci_write_config(gpdev, PCIR_BRIDGECTL_1, v | PCIB_BCR_SECBUS_RESET, 2);
2693 pause("pcie_sbr1", hz / 10); /* 100ms */
2694 pci_write_config(gpdev, PCIR_BRIDGECTL_1, v, 2);
2695 pause("pcie_sbr2", hz); /* Wait 1s before restore_state. */
2696 v = pci_read_config(gpdev, lsta, 2);
2697 if (pci_read_config(gpdev, lcap, 2) & PCIEM_LINK_CAP_DL_ACTIVE)
2698 rc = v & PCIEM_LINK_STA_DL_ACTIVE ? 0 : ETIMEDOUT;
2699 else if (v & (PCIEM_LINK_STA_TRAINING_ERROR | PCIEM_LINK_STA_TRAINING))
2700 rc = ETIMEDOUT;
2701 else
2702 rc = 0;
2703 if (rc != 0)
2704 CH_ERR(sc, "%s: PCIe link is down after reset, LINK_STA 0x%x\n",
2705 __func__, v);
2706 else {
2707 for (i = 0; i < nchildren; i++)
2708 pci_restore_state(children[i]);
2709 }
2710 free(children, M_TEMP);
2711
2712 return (rc);
2713 }
2714
2715 static int
reset_adapter_with_pcie_link_bounce(struct adapter * sc)2716 reset_adapter_with_pcie_link_bounce(struct adapter *sc)
2717 {
2718 device_t pdev = device_get_parent(sc->dev);
2719 device_t gpdev = device_get_parent(pdev);
2720 device_t *children;
2721 int rc, i, lcap, lctl, lsta, nchildren;
2722 uint32_t v;
2723
2724 rc = pci_find_cap(gpdev, PCIY_EXPRESS, &v);
2725 if (rc != 0) {
2726 CH_ERR(sc, "%s: pci_find_cap(%s, pcie) failed: %d\n", __func__,
2727 device_get_nameunit(gpdev), rc);
2728 return (ENOTSUP);
2729 }
2730 lcap = v + PCIER_LINK_CAP;
2731 lctl = v + PCIER_LINK_CTL;
2732 lsta = v + PCIER_LINK_STA;
2733
2734 nchildren = 0;
2735 device_get_children(pdev, &children, &nchildren);
2736 for (i = 0; i < nchildren; i++)
2737 pci_save_state(children[i]);
2738 v = pci_read_config(gpdev, lctl, 2);
2739 pci_write_config(gpdev, lctl, v | PCIEM_LINK_CTL_LINK_DIS, 2);
2740 pause("pcie_lnk1", 100 * hz / 1000); /* 100ms */
2741 pci_write_config(gpdev, lctl, v | PCIEM_LINK_CTL_RETRAIN_LINK, 2);
2742 pause("pcie_lnk2", hz); /* Wait 1s before restore_state. */
2743 v = pci_read_config(gpdev, lsta, 2);
2744 if (pci_read_config(gpdev, lcap, 2) & PCIEM_LINK_CAP_DL_ACTIVE)
2745 rc = v & PCIEM_LINK_STA_DL_ACTIVE ? 0 : ETIMEDOUT;
2746 else if (v & (PCIEM_LINK_STA_TRAINING_ERROR | PCIEM_LINK_STA_TRAINING))
2747 rc = ETIMEDOUT;
2748 else
2749 rc = 0;
2750 if (rc != 0)
2751 CH_ERR(sc, "%s: PCIe link is down after reset, LINK_STA 0x%x\n",
2752 __func__, v);
2753 else {
2754 for (i = 0; i < nchildren; i++)
2755 pci_restore_state(children[i]);
2756 }
2757 free(children, M_TEMP);
2758
2759 return (rc);
2760 }
2761
2762 static inline int
reset_adapter(struct adapter * sc)2763 reset_adapter(struct adapter *sc)
2764 {
2765 int rc;
2766 const int reset_method = vm_guest == VM_GUEST_NO ? t4_reset_method : 0;
2767
2768 rc = suspend_adapter(sc);
2769 if (rc != 0)
2770 return (rc);
2771
2772 switch (reset_method) {
2773 case 1:
2774 rc = reset_adapter_with_pcie_sbr(sc);
2775 break;
2776 case 2:
2777 rc = reset_adapter_with_pcie_link_bounce(sc);
2778 break;
2779 case 0:
2780 default:
2781 rc = reset_adapter_with_pl_rst(sc);
2782 break;
2783 }
2784 if (rc == 0)
2785 rc = resume_adapter(sc);
2786 return (rc);
2787 }
2788
2789 static void
reset_adapter_task(void * arg,int pending)2790 reset_adapter_task(void *arg, int pending)
2791 {
2792 struct adapter *sc = arg;
2793 const int flags = sc->flags;
2794 const int eflags = sc->error_flags;
2795 int rc;
2796
2797 if (pending > 1)
2798 CH_ALERT(sc, "%s: pending %d\n", __func__, pending);
2799 rc = reset_adapter(sc);
2800 if (rc != 0) {
2801 CH_ERR(sc, "adapter did not reset properly, rc = %d, "
2802 "flags 0x%08x -> 0x%08x, err_flags 0x%08x -> 0x%08x.\n",
2803 rc, flags, sc->flags, eflags, sc->error_flags);
2804 }
2805 }
2806
2807 static int
cxgbe_probe(device_t dev)2808 cxgbe_probe(device_t dev)
2809 {
2810 struct port_info *pi = device_get_softc(dev);
2811
2812 device_set_descf(dev, "port %d", pi->port_id);
2813
2814 return (BUS_PROBE_DEFAULT);
2815 }
2816
2817 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2818 IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2819 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2820 IFCAP_HWRXTSTMP | IFCAP_MEXTPG | IFCAP_NV)
2821 #define T4_CAP_ENABLE (T4_CAP)
2822
2823 static void
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2824 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2825 {
2826 if_t ifp;
2827 struct sbuf *sb;
2828 struct sysctl_ctx_list *ctx = &vi->ctx;
2829 struct sysctl_oid_list *children;
2830 struct pfil_head_args pa;
2831 struct adapter *sc = vi->adapter;
2832
2833 sysctl_ctx_init(ctx);
2834 children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2835 vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2836 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2837 vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2838 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2839 #ifdef DEV_NETMAP
2840 vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2841 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2842 vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2843 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2844 #endif
2845 #ifdef TCP_OFFLOAD
2846 vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2847 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2848 #endif
2849 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2850 vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2851 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2852 #endif
2853
2854 vi->xact_addr_filt = -1;
2855 mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2856 callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2857 if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2858 vi->flags |= TX_USES_VM_WR;
2859
2860 /* Allocate an ifnet and set it up */
2861 ifp = if_alloc_dev(IFT_ETHER, dev);
2862 vi->ifp = ifp;
2863 if_setsoftc(ifp, vi);
2864
2865 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2866 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2867
2868 if_setinitfn(ifp, cxgbe_init);
2869 if_setioctlfn(ifp, cxgbe_ioctl);
2870 if_settransmitfn(ifp, cxgbe_transmit);
2871 if_setqflushfn(ifp, cxgbe_qflush);
2872 if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2873 if_setgetcounterfn(ifp, vi_get_counter);
2874 else
2875 if_setgetcounterfn(ifp, cxgbe_get_counter);
2876 #if defined(KERN_TLS) || defined(RATELIMIT)
2877 if_setsndtagallocfn(ifp, cxgbe_snd_tag_alloc);
2878 #endif
2879 #ifdef RATELIMIT
2880 if_setratelimitqueryfn(ifp, cxgbe_ratelimit_query);
2881 #endif
2882
2883 if_setcapabilities(ifp, T4_CAP);
2884 if_setcapenable(ifp, T4_CAP_ENABLE);
2885 if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2886 CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2887 if (chip_id(sc) >= CHELSIO_T6) {
2888 if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2889 if_setcapenablebit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2890 if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2891 CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2892 CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN, 0);
2893 }
2894
2895 #ifdef TCP_OFFLOAD
2896 if (vi->nofldrxq != 0)
2897 if_setcapabilitiesbit(ifp, IFCAP_TOE, 0);
2898 #endif
2899 #ifdef RATELIMIT
2900 if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2901 if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT, 0);
2902 if_setcapenablebit(ifp, IFCAP_TXRTLMT, 0);
2903 }
2904 #endif
2905
2906 if_sethwtsomax(ifp, IP_MAXPACKET);
2907 if (vi->flags & TX_USES_VM_WR)
2908 if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_VM_TSO);
2909 else
2910 if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_TSO);
2911 #ifdef RATELIMIT
2912 if (is_ethoffload(sc) && vi->nofldtxq != 0)
2913 if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_EO_TSO);
2914 #endif
2915 if_sethwtsomaxsegsize(ifp, 65536);
2916 #ifdef KERN_TLS
2917 if (is_ktls(sc)) {
2918 if_setcapabilitiesbit(ifp, IFCAP_TXTLS, 0);
2919 if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2920 if_setcapenablebit(ifp, IFCAP_TXTLS, 0);
2921 }
2922 #endif
2923
2924 ether_ifattach(ifp, vi->hw_addr);
2925 #ifdef DEV_NETMAP
2926 if (vi->nnmrxq != 0)
2927 cxgbe_nm_attach(vi);
2928 #endif
2929 sb = sbuf_new_auto();
2930 sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2931 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2932 switch (if_getcapabilities(ifp) & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2933 case IFCAP_TOE:
2934 sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2935 break;
2936 case IFCAP_TOE | IFCAP_TXRTLMT:
2937 sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2938 break;
2939 case IFCAP_TXRTLMT:
2940 sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2941 break;
2942 }
2943 #endif
2944 #ifdef TCP_OFFLOAD
2945 if (if_getcapabilities(ifp) & IFCAP_TOE)
2946 sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2947 #endif
2948 #ifdef DEV_NETMAP
2949 if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2950 sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2951 vi->nnmtxq, vi->nnmrxq);
2952 #endif
2953 sbuf_finish(sb);
2954 device_printf(dev, "%s\n", sbuf_data(sb));
2955 sbuf_delete(sb);
2956
2957 vi_sysctls(vi);
2958
2959 pa.pa_version = PFIL_VERSION;
2960 pa.pa_flags = PFIL_IN;
2961 pa.pa_type = PFIL_TYPE_ETHERNET;
2962 pa.pa_headname = if_name(ifp);
2963 vi->pfil = pfil_head_register(&pa);
2964 }
2965
2966 static int
cxgbe_attach(device_t dev)2967 cxgbe_attach(device_t dev)
2968 {
2969 struct port_info *pi = device_get_softc(dev);
2970 struct adapter *sc = pi->adapter;
2971 struct vi_info *vi;
2972 int i;
2973
2974 sysctl_ctx_init(&pi->ctx);
2975
2976 cxgbe_vi_attach(dev, &pi->vi[0]);
2977
2978 for_each_vi(pi, i, vi) {
2979 if (i == 0)
2980 continue;
2981 vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, DEVICE_UNIT_ANY);
2982 if (vi->dev == NULL) {
2983 device_printf(dev, "failed to add VI %d\n", i);
2984 continue;
2985 }
2986 device_set_softc(vi->dev, vi);
2987 }
2988
2989 cxgbe_sysctls(pi);
2990
2991 bus_attach_children(dev);
2992
2993 return (0);
2994 }
2995
2996 static void
cxgbe_vi_detach(struct vi_info * vi)2997 cxgbe_vi_detach(struct vi_info *vi)
2998 {
2999 if_t ifp = vi->ifp;
3000
3001 if (vi->pfil != NULL) {
3002 pfil_head_unregister(vi->pfil);
3003 vi->pfil = NULL;
3004 }
3005
3006 ether_ifdetach(ifp);
3007
3008 /* Let detach proceed even if these fail. */
3009 #ifdef DEV_NETMAP
3010 if (if_getcapabilities(ifp) & IFCAP_NETMAP)
3011 cxgbe_nm_detach(vi);
3012 #endif
3013 cxgbe_uninit_synchronized(vi);
3014 callout_drain(&vi->tick);
3015 mtx_destroy(&vi->tick_mtx);
3016 sysctl_ctx_free(&vi->ctx);
3017 vi_full_uninit(vi);
3018
3019 if_free(vi->ifp);
3020 vi->ifp = NULL;
3021 }
3022
3023 static int
cxgbe_detach(device_t dev)3024 cxgbe_detach(device_t dev)
3025 {
3026 struct port_info *pi = device_get_softc(dev);
3027 struct adapter *sc = pi->adapter;
3028 int rc;
3029
3030 /* Detach the extra VIs first. */
3031 rc = bus_generic_detach(dev);
3032 if (rc)
3033 return (rc);
3034
3035 sysctl_ctx_free(&pi->ctx);
3036 begin_vi_detach(sc, &pi->vi[0]);
3037 if (pi->flags & HAS_TRACEQ) {
3038 sc->traceq = -1; /* cloner should not create ifnet */
3039 t4_tracer_port_detach(sc);
3040 }
3041 cxgbe_vi_detach(&pi->vi[0]);
3042 ifmedia_removeall(&pi->media);
3043 end_vi_detach(sc, &pi->vi[0]);
3044
3045 return (0);
3046 }
3047
3048 static void
cxgbe_init(void * arg)3049 cxgbe_init(void *arg)
3050 {
3051 struct vi_info *vi = arg;
3052 struct adapter *sc = vi->adapter;
3053
3054 if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
3055 return;
3056 cxgbe_init_synchronized(vi);
3057 end_synchronized_op(sc, 0);
3058 }
3059
3060 static int
cxgbe_ioctl(if_t ifp,unsigned long cmd,caddr_t data)3061 cxgbe_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
3062 {
3063 int rc = 0, mtu, flags;
3064 struct vi_info *vi = if_getsoftc(ifp);
3065 struct port_info *pi = vi->pi;
3066 struct adapter *sc = pi->adapter;
3067 struct ifreq *ifr = (struct ifreq *)data;
3068 uint32_t mask, mask2;
3069
3070 switch (cmd) {
3071 case SIOCSIFMTU:
3072 mtu = ifr->ifr_mtu;
3073 if (mtu < ETHERMIN || mtu > MAX_MTU)
3074 return (EINVAL);
3075
3076 rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
3077 if (rc)
3078 return (rc);
3079 if_setmtu(ifp, mtu);
3080 if (vi->flags & VI_INIT_DONE) {
3081 t4_update_fl_bufsize(ifp);
3082 if (hw_all_ok(sc) &&
3083 if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3084 rc = update_mac_settings(ifp, XGMAC_MTU);
3085 }
3086 end_synchronized_op(sc, 0);
3087 break;
3088
3089 case SIOCSIFFLAGS:
3090 rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
3091 if (rc)
3092 return (rc);
3093
3094 if (!hw_all_ok(sc)) {
3095 rc = ENXIO;
3096 goto fail;
3097 }
3098
3099 if (if_getflags(ifp) & IFF_UP) {
3100 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3101 flags = vi->if_flags;
3102 if ((if_getflags(ifp) ^ flags) &
3103 (IFF_PROMISC | IFF_ALLMULTI)) {
3104 rc = update_mac_settings(ifp,
3105 XGMAC_PROMISC | XGMAC_ALLMULTI);
3106 }
3107 } else {
3108 rc = cxgbe_init_synchronized(vi);
3109 }
3110 vi->if_flags = if_getflags(ifp);
3111 } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3112 rc = cxgbe_uninit_synchronized(vi);
3113 }
3114 end_synchronized_op(sc, 0);
3115 break;
3116
3117 case SIOCADDMULTI:
3118 case SIOCDELMULTI:
3119 rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
3120 if (rc)
3121 return (rc);
3122 if (hw_all_ok(sc) && if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3123 rc = update_mac_settings(ifp, XGMAC_MCADDRS);
3124 end_synchronized_op(sc, 0);
3125 break;
3126
3127 case SIOCGIFCAPNV:
3128 break;
3129 case SIOCSIFCAPNV:
3130 case SIOCSIFCAP:
3131 rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
3132 if (rc)
3133 return (rc);
3134
3135 if (cmd == SIOCSIFCAPNV) {
3136 const struct siocsifcapnv_driver_data *ifr_nv =
3137 (struct siocsifcapnv_driver_data *)data;
3138
3139 mask = ifr_nv->reqcap ^ if_getcapenable(ifp);
3140 mask2 = ifr_nv->reqcap2 ^ if_getcapenable2(ifp);
3141 } else {
3142 mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
3143 mask2 = 0;
3144 }
3145 if (mask & IFCAP_TXCSUM) {
3146 if_togglecapenable(ifp, IFCAP_TXCSUM);
3147 if_togglehwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP);
3148
3149 if (IFCAP_TSO4 & if_getcapenable(ifp) &&
3150 !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3151 mask &= ~IFCAP_TSO4;
3152 if_setcapenablebit(ifp, 0, IFCAP_TSO4);
3153 if_printf(ifp,
3154 "tso4 disabled due to -txcsum.\n");
3155 }
3156 }
3157 if (mask & IFCAP_TXCSUM_IPV6) {
3158 if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
3159 if_togglehwassist(ifp, CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
3160
3161 if (IFCAP_TSO6 & if_getcapenable(ifp) &&
3162 !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3163 mask &= ~IFCAP_TSO6;
3164 if_setcapenablebit(ifp, 0, IFCAP_TSO6);
3165 if_printf(ifp,
3166 "tso6 disabled due to -txcsum6.\n");
3167 }
3168 }
3169 if (mask & IFCAP_RXCSUM)
3170 if_togglecapenable(ifp, IFCAP_RXCSUM);
3171 if (mask & IFCAP_RXCSUM_IPV6)
3172 if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
3173
3174 /*
3175 * Note that we leave CSUM_TSO alone (it is always set). The
3176 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
3177 * sending a TSO request our way, so it's sufficient to toggle
3178 * IFCAP_TSOx only.
3179 */
3180 if (mask & IFCAP_TSO4) {
3181 if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
3182 !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3183 if_printf(ifp, "enable txcsum first.\n");
3184 rc = EAGAIN;
3185 goto fail;
3186 }
3187 if_togglecapenable(ifp, IFCAP_TSO4);
3188 }
3189 if (mask & IFCAP_TSO6) {
3190 if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
3191 !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3192 if_printf(ifp, "enable txcsum6 first.\n");
3193 rc = EAGAIN;
3194 goto fail;
3195 }
3196 if_togglecapenable(ifp, IFCAP_TSO6);
3197 }
3198 if (mask & IFCAP_LRO) {
3199 #if defined(INET) || defined(INET6)
3200 int i;
3201 struct sge_rxq *rxq;
3202
3203 if_togglecapenable(ifp, IFCAP_LRO);
3204 for_each_rxq(vi, i, rxq) {
3205 if (if_getcapenable(ifp) & IFCAP_LRO)
3206 rxq->iq.flags |= IQ_LRO_ENABLED;
3207 else
3208 rxq->iq.flags &= ~IQ_LRO_ENABLED;
3209 }
3210 #endif
3211 }
3212 #ifdef TCP_OFFLOAD
3213 if (mask & IFCAP_TOE) {
3214 int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TOE;
3215
3216 rc = toe_capability(vi, enable);
3217 if (rc != 0)
3218 goto fail;
3219
3220 if_togglecapenable(ifp, mask);
3221 }
3222 #endif
3223 if (mask & IFCAP_VLAN_HWTAGGING) {
3224 if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3225 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3226 rc = update_mac_settings(ifp, XGMAC_VLANEX);
3227 }
3228 if (mask & IFCAP_VLAN_MTU) {
3229 if_togglecapenable(ifp, IFCAP_VLAN_MTU);
3230
3231 /* Need to find out how to disable auto-mtu-inflation */
3232 }
3233 if (mask & IFCAP_VLAN_HWTSO)
3234 if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3235 if (mask & IFCAP_VLAN_HWCSUM)
3236 if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
3237 #ifdef RATELIMIT
3238 if (mask & IFCAP_TXRTLMT)
3239 if_togglecapenable(ifp, IFCAP_TXRTLMT);
3240 #endif
3241 if (mask & IFCAP_HWRXTSTMP) {
3242 int i;
3243 struct sge_rxq *rxq;
3244
3245 if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3246 for_each_rxq(vi, i, rxq) {
3247 if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3248 rxq->iq.flags |= IQ_RX_TIMESTAMP;
3249 else
3250 rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
3251 }
3252 }
3253 if (mask & IFCAP_MEXTPG)
3254 if_togglecapenable(ifp, IFCAP_MEXTPG);
3255
3256 #ifdef KERN_TLS
3257 if (mask & IFCAP_TXTLS) {
3258 int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TXTLS;
3259
3260 rc = ktls_capability(sc, enable);
3261 if (rc != 0)
3262 goto fail;
3263
3264 if_togglecapenable(ifp, mask & IFCAP_TXTLS);
3265 }
3266 #endif
3267 if (mask & IFCAP_VXLAN_HWCSUM) {
3268 if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3269 if_togglehwassist(ifp, CSUM_INNER_IP6_UDP |
3270 CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
3271 CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP);
3272 }
3273 if (mask & IFCAP_VXLAN_HWTSO) {
3274 if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3275 if_togglehwassist(ifp, CSUM_INNER_IP6_TSO |
3276 CSUM_INNER_IP_TSO);
3277 }
3278
3279 MPASS(mask2 == 0);
3280 (void)mask2;
3281
3282 #ifdef VLAN_CAPABILITIES
3283 VLAN_CAPABILITIES(ifp);
3284 #endif
3285 fail:
3286 end_synchronized_op(sc, 0);
3287 break;
3288
3289 case SIOCSIFMEDIA:
3290 case SIOCGIFMEDIA:
3291 case SIOCGIFXMEDIA:
3292 rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3293 break;
3294
3295 case SIOCGI2C: {
3296 struct ifi2creq i2c;
3297
3298 rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3299 if (rc != 0)
3300 break;
3301 if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3302 rc = EPERM;
3303 break;
3304 }
3305 if (i2c.len > sizeof(i2c.data)) {
3306 rc = EINVAL;
3307 break;
3308 }
3309 rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3310 if (rc)
3311 return (rc);
3312 if (!hw_all_ok(sc))
3313 rc = ENXIO;
3314 else
3315 rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3316 i2c.offset, i2c.len, &i2c.data[0]);
3317 end_synchronized_op(sc, 0);
3318 if (rc == 0)
3319 rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3320 break;
3321 }
3322
3323 default:
3324 rc = ether_ioctl(ifp, cmd, data);
3325 }
3326
3327 return (rc);
3328 }
3329
3330 static int
cxgbe_transmit(if_t ifp,struct mbuf * m)3331 cxgbe_transmit(if_t ifp, struct mbuf *m)
3332 {
3333 struct vi_info *vi = if_getsoftc(ifp);
3334 struct port_info *pi = vi->pi;
3335 struct adapter *sc;
3336 struct sge_txq *txq;
3337 void *items[1];
3338 int rc;
3339
3340 M_ASSERTPKTHDR(m);
3341 MPASS(m->m_nextpkt == NULL); /* not quite ready for this yet */
3342 #if defined(KERN_TLS) || defined(RATELIMIT)
3343 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3344 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3345 #endif
3346
3347 if (__predict_false(pi->link_cfg.link_ok == false)) {
3348 m_freem(m);
3349 return (ENETDOWN);
3350 }
3351
3352 rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3353 if (__predict_false(rc != 0)) {
3354 if (__predict_true(rc == EINPROGRESS)) {
3355 /* queued by parse_pkt */
3356 MPASS(m != NULL);
3357 return (0);
3358 }
3359
3360 MPASS(m == NULL); /* was freed already */
3361 atomic_add_int(&pi->tx_parse_error, 1); /* rare, atomic is ok */
3362 return (rc);
3363 }
3364
3365 /* Select a txq. */
3366 sc = vi->adapter;
3367 txq = &sc->sge.txq[vi->first_txq];
3368 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3369 txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3370 vi->rsrv_noflowq);
3371
3372 items[0] = m;
3373 rc = mp_ring_enqueue(txq->r, items, 1, 256);
3374 if (__predict_false(rc != 0))
3375 m_freem(m);
3376
3377 return (rc);
3378 }
3379
3380 static void
cxgbe_qflush(if_t ifp)3381 cxgbe_qflush(if_t ifp)
3382 {
3383 struct vi_info *vi = if_getsoftc(ifp);
3384 struct sge_txq *txq;
3385 int i;
3386
3387 /* queues do not exist if !VI_INIT_DONE. */
3388 if (vi->flags & VI_INIT_DONE) {
3389 for_each_txq(vi, i, txq) {
3390 TXQ_LOCK(txq);
3391 txq->eq.flags |= EQ_QFLUSH;
3392 TXQ_UNLOCK(txq);
3393 while (!mp_ring_is_idle(txq->r)) {
3394 mp_ring_check_drainage(txq->r, 4096);
3395 pause("qflush", 1);
3396 }
3397 TXQ_LOCK(txq);
3398 txq->eq.flags &= ~EQ_QFLUSH;
3399 TXQ_UNLOCK(txq);
3400 }
3401 }
3402 if_qflush(ifp);
3403 }
3404
3405 static uint64_t
vi_get_counter(if_t ifp,ift_counter c)3406 vi_get_counter(if_t ifp, ift_counter c)
3407 {
3408 struct vi_info *vi = if_getsoftc(ifp);
3409 struct fw_vi_stats_vf *s = &vi->stats;
3410
3411 mtx_lock(&vi->tick_mtx);
3412 vi_refresh_stats(vi);
3413 mtx_unlock(&vi->tick_mtx);
3414
3415 switch (c) {
3416 case IFCOUNTER_IPACKETS:
3417 return (s->rx_bcast_frames + s->rx_mcast_frames +
3418 s->rx_ucast_frames);
3419 case IFCOUNTER_IERRORS:
3420 return (s->rx_err_frames);
3421 case IFCOUNTER_OPACKETS:
3422 return (s->tx_bcast_frames + s->tx_mcast_frames +
3423 s->tx_ucast_frames + s->tx_offload_frames);
3424 case IFCOUNTER_OERRORS:
3425 return (s->tx_drop_frames);
3426 case IFCOUNTER_IBYTES:
3427 return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3428 s->rx_ucast_bytes);
3429 case IFCOUNTER_OBYTES:
3430 return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3431 s->tx_ucast_bytes + s->tx_offload_bytes);
3432 case IFCOUNTER_IMCASTS:
3433 return (s->rx_mcast_frames);
3434 case IFCOUNTER_OMCASTS:
3435 return (s->tx_mcast_frames);
3436 case IFCOUNTER_OQDROPS: {
3437 uint64_t drops;
3438
3439 drops = 0;
3440 if (vi->flags & VI_INIT_DONE) {
3441 int i;
3442 struct sge_txq *txq;
3443
3444 for_each_txq(vi, i, txq)
3445 drops += counter_u64_fetch(txq->r->dropped);
3446 }
3447
3448 return (drops);
3449
3450 }
3451
3452 default:
3453 return (if_get_counter_default(ifp, c));
3454 }
3455 }
3456
3457 static uint64_t
cxgbe_get_counter(if_t ifp,ift_counter c)3458 cxgbe_get_counter(if_t ifp, ift_counter c)
3459 {
3460 struct vi_info *vi = if_getsoftc(ifp);
3461 struct port_info *pi = vi->pi;
3462 struct port_stats *s = &pi->stats;
3463
3464 mtx_lock(&vi->tick_mtx);
3465 cxgbe_refresh_stats(vi);
3466 mtx_unlock(&vi->tick_mtx);
3467
3468 switch (c) {
3469 case IFCOUNTER_IPACKETS:
3470 return (s->rx_frames);
3471
3472 case IFCOUNTER_IERRORS:
3473 return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3474 s->rx_fcs_err + s->rx_len_err);
3475
3476 case IFCOUNTER_OPACKETS:
3477 return (s->tx_frames);
3478
3479 case IFCOUNTER_OERRORS:
3480 return (s->tx_error_frames);
3481
3482 case IFCOUNTER_IBYTES:
3483 return (s->rx_octets);
3484
3485 case IFCOUNTER_OBYTES:
3486 return (s->tx_octets);
3487
3488 case IFCOUNTER_IMCASTS:
3489 return (s->rx_mcast_frames);
3490
3491 case IFCOUNTER_OMCASTS:
3492 return (s->tx_mcast_frames);
3493
3494 case IFCOUNTER_IQDROPS:
3495 return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3496 s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3497 s->rx_trunc3 + pi->tnl_cong_drops);
3498
3499 case IFCOUNTER_OQDROPS: {
3500 uint64_t drops;
3501
3502 drops = s->tx_drop;
3503 if (vi->flags & VI_INIT_DONE) {
3504 int i;
3505 struct sge_txq *txq;
3506
3507 for_each_txq(vi, i, txq)
3508 drops += counter_u64_fetch(txq->r->dropped);
3509 }
3510
3511 return (drops);
3512
3513 }
3514
3515 default:
3516 return (if_get_counter_default(ifp, c));
3517 }
3518 }
3519
3520 #if defined(KERN_TLS) || defined(RATELIMIT)
3521 static int
cxgbe_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3522 cxgbe_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3523 struct m_snd_tag **pt)
3524 {
3525 int error;
3526
3527 switch (params->hdr.type) {
3528 #ifdef RATELIMIT
3529 case IF_SND_TAG_TYPE_RATE_LIMIT:
3530 error = cxgbe_rate_tag_alloc(ifp, params, pt);
3531 break;
3532 #endif
3533 #ifdef KERN_TLS
3534 case IF_SND_TAG_TYPE_TLS:
3535 {
3536 struct vi_info *vi = if_getsoftc(ifp);
3537
3538 if (is_t6(vi->pi->adapter))
3539 error = t6_tls_tag_alloc(ifp, params, pt);
3540 else
3541 error = t7_tls_tag_alloc(ifp, params, pt);
3542 break;
3543 }
3544 #endif
3545 default:
3546 error = EOPNOTSUPP;
3547 }
3548 return (error);
3549 }
3550 #endif
3551
3552 /*
3553 * The kernel picks a media from the list we had provided but we still validate
3554 * the requeste.
3555 */
3556 int
cxgbe_media_change(if_t ifp)3557 cxgbe_media_change(if_t ifp)
3558 {
3559 struct vi_info *vi = if_getsoftc(ifp);
3560 struct port_info *pi = vi->pi;
3561 struct ifmedia *ifm = &pi->media;
3562 struct link_config *lc = &pi->link_cfg;
3563 struct adapter *sc = pi->adapter;
3564 int rc;
3565
3566 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3567 if (rc != 0)
3568 return (rc);
3569 PORT_LOCK(pi);
3570 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3571 /* ifconfig .. media autoselect */
3572 if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3573 rc = ENOTSUP; /* AN not supported by transceiver */
3574 goto done;
3575 }
3576 lc->requested_aneg = AUTONEG_ENABLE;
3577 lc->requested_speed = 0;
3578 lc->requested_fc |= PAUSE_AUTONEG;
3579 } else {
3580 lc->requested_aneg = AUTONEG_DISABLE;
3581 lc->requested_speed =
3582 ifmedia_baudrate(ifm->ifm_media) / 1000000;
3583 lc->requested_fc = 0;
3584 if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3585 lc->requested_fc |= PAUSE_RX;
3586 if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3587 lc->requested_fc |= PAUSE_TX;
3588 }
3589 if (pi->up_vis > 0 && hw_all_ok(sc)) {
3590 fixup_link_config(pi);
3591 rc = apply_link_config(pi);
3592 }
3593 done:
3594 PORT_UNLOCK(pi);
3595 end_synchronized_op(sc, 0);
3596 return (rc);
3597 }
3598
3599 /*
3600 * Base media word (without ETHER, pause, link active, etc.) for the port at the
3601 * given speed.
3602 */
3603 static int
port_mword(struct port_info * pi,uint32_t speed)3604 port_mword(struct port_info *pi, uint32_t speed)
3605 {
3606
3607 MPASS(speed & M_FW_PORT_CAP32_SPEED);
3608 MPASS(powerof2(speed));
3609
3610 switch(pi->port_type) {
3611 case FW_PORT_TYPE_BT_SGMII:
3612 case FW_PORT_TYPE_BT_XFI:
3613 case FW_PORT_TYPE_BT_XAUI:
3614 /* BaseT */
3615 switch (speed) {
3616 case FW_PORT_CAP32_SPEED_100M:
3617 return (IFM_100_T);
3618 case FW_PORT_CAP32_SPEED_1G:
3619 return (IFM_1000_T);
3620 case FW_PORT_CAP32_SPEED_10G:
3621 return (IFM_10G_T);
3622 }
3623 break;
3624 case FW_PORT_TYPE_KX4:
3625 if (speed == FW_PORT_CAP32_SPEED_10G)
3626 return (IFM_10G_KX4);
3627 break;
3628 case FW_PORT_TYPE_CX4:
3629 if (speed == FW_PORT_CAP32_SPEED_10G)
3630 return (IFM_10G_CX4);
3631 break;
3632 case FW_PORT_TYPE_KX:
3633 if (speed == FW_PORT_CAP32_SPEED_1G)
3634 return (IFM_1000_KX);
3635 break;
3636 case FW_PORT_TYPE_KR:
3637 case FW_PORT_TYPE_BP_AP:
3638 case FW_PORT_TYPE_BP4_AP:
3639 case FW_PORT_TYPE_BP40_BA:
3640 case FW_PORT_TYPE_KR4_100G:
3641 case FW_PORT_TYPE_KR_SFP28:
3642 case FW_PORT_TYPE_KR_XLAUI:
3643 switch (speed) {
3644 case FW_PORT_CAP32_SPEED_1G:
3645 return (IFM_1000_KX);
3646 case FW_PORT_CAP32_SPEED_10G:
3647 return (IFM_10G_KR);
3648 case FW_PORT_CAP32_SPEED_25G:
3649 return (IFM_25G_KR);
3650 case FW_PORT_CAP32_SPEED_40G:
3651 return (IFM_40G_KR4);
3652 case FW_PORT_CAP32_SPEED_50G:
3653 return (IFM_50G_KR2);
3654 case FW_PORT_CAP32_SPEED_100G:
3655 return (IFM_100G_KR4);
3656 }
3657 break;
3658 case FW_PORT_TYPE_FIBER_XFI:
3659 case FW_PORT_TYPE_FIBER_XAUI:
3660 case FW_PORT_TYPE_SFP:
3661 case FW_PORT_TYPE_QSFP_10G:
3662 case FW_PORT_TYPE_QSA:
3663 case FW_PORT_TYPE_QSFP:
3664 case FW_PORT_TYPE_CR4_QSFP:
3665 case FW_PORT_TYPE_CR_QSFP:
3666 case FW_PORT_TYPE_CR2_QSFP:
3667 case FW_PORT_TYPE_SFP28:
3668 case FW_PORT_TYPE_SFP56:
3669 case FW_PORT_TYPE_QSFP56:
3670 case FW_PORT_TYPE_QSFPDD:
3671 /* Pluggable transceiver */
3672 switch (pi->mod_type) {
3673 case FW_PORT_MOD_TYPE_LR:
3674 case FW_PORT_MOD_TYPE_LR_SIMPLEX:
3675 switch (speed) {
3676 case FW_PORT_CAP32_SPEED_1G:
3677 return (IFM_1000_LX);
3678 case FW_PORT_CAP32_SPEED_10G:
3679 return (IFM_10G_LR);
3680 case FW_PORT_CAP32_SPEED_25G:
3681 return (IFM_25G_LR);
3682 case FW_PORT_CAP32_SPEED_40G:
3683 return (IFM_40G_LR4);
3684 case FW_PORT_CAP32_SPEED_50G:
3685 return (IFM_50G_LR2);
3686 case FW_PORT_CAP32_SPEED_100G:
3687 return (IFM_100G_LR4);
3688 case FW_PORT_CAP32_SPEED_200G:
3689 return (IFM_200G_LR4);
3690 case FW_PORT_CAP32_SPEED_400G:
3691 return (IFM_400G_LR8);
3692 }
3693 break;
3694 case FW_PORT_MOD_TYPE_SR:
3695 switch (speed) {
3696 case FW_PORT_CAP32_SPEED_1G:
3697 return (IFM_1000_SX);
3698 case FW_PORT_CAP32_SPEED_10G:
3699 return (IFM_10G_SR);
3700 case FW_PORT_CAP32_SPEED_25G:
3701 return (IFM_25G_SR);
3702 case FW_PORT_CAP32_SPEED_40G:
3703 return (IFM_40G_SR4);
3704 case FW_PORT_CAP32_SPEED_50G:
3705 return (IFM_50G_SR2);
3706 case FW_PORT_CAP32_SPEED_100G:
3707 return (IFM_100G_SR4);
3708 case FW_PORT_CAP32_SPEED_200G:
3709 return (IFM_200G_SR4);
3710 case FW_PORT_CAP32_SPEED_400G:
3711 return (IFM_400G_SR8);
3712 }
3713 break;
3714 case FW_PORT_MOD_TYPE_ER:
3715 if (speed == FW_PORT_CAP32_SPEED_10G)
3716 return (IFM_10G_ER);
3717 break;
3718 case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3719 case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3720 switch (speed) {
3721 case FW_PORT_CAP32_SPEED_1G:
3722 return (IFM_1000_CX);
3723 case FW_PORT_CAP32_SPEED_10G:
3724 return (IFM_10G_TWINAX);
3725 case FW_PORT_CAP32_SPEED_25G:
3726 return (IFM_25G_CR);
3727 case FW_PORT_CAP32_SPEED_40G:
3728 return (IFM_40G_CR4);
3729 case FW_PORT_CAP32_SPEED_50G:
3730 return (IFM_50G_CR2);
3731 case FW_PORT_CAP32_SPEED_100G:
3732 return (IFM_100G_CR4);
3733 case FW_PORT_CAP32_SPEED_200G:
3734 return (IFM_200G_CR4_PAM4);
3735 case FW_PORT_CAP32_SPEED_400G:
3736 return (IFM_400G_CR8);
3737 }
3738 break;
3739 case FW_PORT_MOD_TYPE_LRM:
3740 if (speed == FW_PORT_CAP32_SPEED_10G)
3741 return (IFM_10G_LRM);
3742 break;
3743 case FW_PORT_MOD_TYPE_DR:
3744 if (speed == FW_PORT_CAP32_SPEED_100G)
3745 return (IFM_100G_DR);
3746 if (speed == FW_PORT_CAP32_SPEED_200G)
3747 return (IFM_200G_DR4);
3748 if (speed == FW_PORT_CAP32_SPEED_400G)
3749 return (IFM_400G_DR4);
3750 break;
3751 case FW_PORT_MOD_TYPE_NA:
3752 MPASS(0); /* Not pluggable? */
3753 /* fall through */
3754 case FW_PORT_MOD_TYPE_ERROR:
3755 case FW_PORT_MOD_TYPE_UNKNOWN:
3756 case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3757 break;
3758 case FW_PORT_MOD_TYPE_NONE:
3759 return (IFM_NONE);
3760 }
3761 break;
3762 case M_FW_PORT_CMD_PTYPE: /* FW_PORT_TYPE_NONE for old firmware */
3763 if (chip_id(pi->adapter) >= CHELSIO_T7)
3764 return (IFM_UNKNOWN);
3765 /* fall through */
3766 case FW_PORT_TYPE_NONE:
3767 return (IFM_NONE);
3768 }
3769
3770 return (IFM_UNKNOWN);
3771 }
3772
3773 void
cxgbe_media_status(if_t ifp,struct ifmediareq * ifmr)3774 cxgbe_media_status(if_t ifp, struct ifmediareq *ifmr)
3775 {
3776 struct vi_info *vi = if_getsoftc(ifp);
3777 struct port_info *pi = vi->pi;
3778 struct adapter *sc = pi->adapter;
3779 struct link_config *lc = &pi->link_cfg;
3780
3781 if (begin_synchronized_op(sc, vi , SLEEP_OK | INTR_OK, "t4med") != 0)
3782 return;
3783 PORT_LOCK(pi);
3784
3785 if (pi->up_vis == 0 && hw_all_ok(sc)) {
3786 /*
3787 * If all the interfaces are administratively down the firmware
3788 * does not report transceiver changes. Refresh port info here
3789 * so that ifconfig displays accurate ifmedia at all times.
3790 * This is the only reason we have a synchronized op in this
3791 * function. Just PORT_LOCK would have been enough otherwise.
3792 */
3793 t4_update_port_info(pi);
3794 build_medialist(pi);
3795 }
3796
3797 /* ifm_status */
3798 ifmr->ifm_status = IFM_AVALID;
3799 if (lc->link_ok == false)
3800 goto done;
3801 ifmr->ifm_status |= IFM_ACTIVE;
3802
3803 /* ifm_active */
3804 ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3805 ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3806 if (lc->fc & PAUSE_RX)
3807 ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3808 if (lc->fc & PAUSE_TX)
3809 ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3810 ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3811 done:
3812 PORT_UNLOCK(pi);
3813 end_synchronized_op(sc, 0);
3814 }
3815
3816 static int
vcxgbe_probe(device_t dev)3817 vcxgbe_probe(device_t dev)
3818 {
3819 struct vi_info *vi = device_get_softc(dev);
3820
3821 device_set_descf(dev, "port %d vi %td", vi->pi->port_id,
3822 vi - vi->pi->vi);
3823
3824 return (BUS_PROBE_DEFAULT);
3825 }
3826
3827 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3828 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3829 {
3830 int func, index, rc;
3831 uint32_t param, val;
3832
3833 ASSERT_SYNCHRONIZED_OP(sc);
3834
3835 index = vi - pi->vi;
3836 MPASS(index > 0); /* This function deals with _extra_ VIs only */
3837 KASSERT(index < nitems(vi_mac_funcs),
3838 ("%s: VI %s doesn't have a MAC func", __func__,
3839 device_get_nameunit(vi->dev)));
3840 func = vi_mac_funcs[index];
3841 rc = t4_alloc_vi_func(sc, sc->mbox, pi->hw_port, sc->pf, 0, 1,
3842 vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3843 if (rc < 0) {
3844 CH_ERR(vi, "failed to allocate virtual interface %d"
3845 "for port %d: %d\n", index, pi->port_id, -rc);
3846 return (-rc);
3847 }
3848 vi->viid = rc;
3849
3850 if (vi->rss_size == 1) {
3851 /*
3852 * This VI didn't get a slice of the RSS table. Reduce the
3853 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3854 * configuration file (nvi, rssnvi for this PF) if this is a
3855 * problem.
3856 */
3857 device_printf(vi->dev, "RSS table not available.\n");
3858 vi->rss_base = 0xffff;
3859
3860 return (0);
3861 }
3862
3863 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3864 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3865 V_FW_PARAMS_PARAM_YZ(vi->viid);
3866 rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
3867 if (rc)
3868 vi->rss_base = 0xffff;
3869 else {
3870 MPASS((val >> 16) == vi->rss_size);
3871 vi->rss_base = val & 0xffff;
3872 }
3873
3874 return (0);
3875 }
3876
3877 static int
vcxgbe_attach(device_t dev)3878 vcxgbe_attach(device_t dev)
3879 {
3880 struct vi_info *vi;
3881 struct port_info *pi;
3882 struct adapter *sc;
3883 int rc;
3884
3885 vi = device_get_softc(dev);
3886 pi = vi->pi;
3887 sc = pi->adapter;
3888
3889 rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3890 if (rc)
3891 return (rc);
3892 rc = alloc_extra_vi(sc, pi, vi);
3893 end_synchronized_op(sc, 0);
3894 if (rc)
3895 return (rc);
3896
3897 cxgbe_vi_attach(dev, vi);
3898
3899 return (0);
3900 }
3901
3902 static int
vcxgbe_detach(device_t dev)3903 vcxgbe_detach(device_t dev)
3904 {
3905 struct vi_info *vi;
3906 struct adapter *sc;
3907
3908 vi = device_get_softc(dev);
3909 sc = vi->adapter;
3910
3911 begin_vi_detach(sc, vi);
3912 cxgbe_vi_detach(vi);
3913 t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3914 end_vi_detach(sc, vi);
3915
3916 return (0);
3917 }
3918
3919 static struct callout fatal_callout;
3920 static struct taskqueue *reset_tq;
3921
3922 static void
delayed_panic(void * arg)3923 delayed_panic(void *arg)
3924 {
3925 struct adapter *sc = arg;
3926
3927 panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3928 }
3929
3930 static void
fatal_error_task(void * arg,int pending)3931 fatal_error_task(void *arg, int pending)
3932 {
3933 struct adapter *sc = arg;
3934 int rc;
3935
3936 if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3937 dump_cim_regs(sc);
3938 dump_cimla(sc);
3939 dump_devlog(sc);
3940 }
3941
3942 if (t4_reset_on_fatal_err) {
3943 CH_ALERT(sc, "resetting adapter after fatal error.\n");
3944 rc = reset_adapter(sc);
3945 if (rc == 0 && t4_panic_on_fatal_err) {
3946 CH_ALERT(sc, "reset was successful, "
3947 "system will NOT panic.\n");
3948 return;
3949 }
3950 }
3951
3952 if (t4_panic_on_fatal_err) {
3953 CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3954 callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3955 }
3956 }
3957
3958 void
t4_fatal_err(struct adapter * sc,bool fw_error)3959 t4_fatal_err(struct adapter *sc, bool fw_error)
3960 {
3961 stop_adapter(sc);
3962 if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3963 return;
3964 if (fw_error) {
3965 /*
3966 * We are here because of a firmware error/timeout and not
3967 * because of a hardware interrupt. It is possible (although
3968 * not very likely) that an error interrupt was also raised but
3969 * this thread ran first and inhibited t4_intr_err. We walk the
3970 * main INT_CAUSE registers here to make sure we haven't missed
3971 * anything interesting.
3972 */
3973 t4_slow_intr_handler(sc, sc->intr_flags);
3974 atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3975 }
3976 t4_report_fw_error(sc);
3977 log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3978 device_get_nameunit(sc->dev), fw_error);
3979 taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3980 }
3981
3982 void
t4_add_adapter(struct adapter * sc)3983 t4_add_adapter(struct adapter *sc)
3984 {
3985 sx_xlock(&t4_list_lock);
3986 SLIST_INSERT_HEAD(&t4_list, sc, link);
3987 sx_xunlock(&t4_list_lock);
3988 }
3989
3990 int
t4_map_bars_0_and_4(struct adapter * sc)3991 t4_map_bars_0_and_4(struct adapter *sc)
3992 {
3993 sc->regs_rid = PCIR_BAR(0);
3994 sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3995 &sc->regs_rid, RF_ACTIVE);
3996 if (sc->regs_res == NULL) {
3997 device_printf(sc->dev, "cannot map registers.\n");
3998 return (ENXIO);
3999 }
4000 sc->mmio_len = rman_get_size(sc->regs_res);
4001 setbit(&sc->doorbells, DOORBELL_KDB);
4002
4003 sc->msix_rid = PCIR_BAR(4);
4004 sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
4005 &sc->msix_rid, RF_ACTIVE);
4006 if (sc->msix_res == NULL) {
4007 device_printf(sc->dev, "cannot map MSI-X BAR.\n");
4008 return (ENXIO);
4009 }
4010
4011 return (0);
4012 }
4013
4014 int
t4_map_bar_2(struct adapter * sc)4015 t4_map_bar_2(struct adapter *sc)
4016 {
4017
4018 /*
4019 * T4: only iWARP driver uses the userspace doorbells. There is no need
4020 * to map it if RDMA is disabled.
4021 */
4022 if (is_t4(sc) && sc->rdmacaps == 0)
4023 return (0);
4024
4025 sc->udbs_rid = PCIR_BAR(2);
4026 sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
4027 &sc->udbs_rid, RF_ACTIVE);
4028 if (sc->udbs_res == NULL) {
4029 device_printf(sc->dev, "cannot map doorbell BAR.\n");
4030 return (ENXIO);
4031 }
4032 sc->udbs_base = rman_get_virtual(sc->udbs_res);
4033
4034 if (chip_id(sc) >= CHELSIO_T5) {
4035 setbit(&sc->doorbells, DOORBELL_UDB);
4036 #if defined(__i386__) || defined(__amd64__)
4037 if (t5_write_combine) {
4038 int rc, mode;
4039
4040 /*
4041 * Enable write combining on BAR2. This is the
4042 * userspace doorbell BAR and is split into 128B
4043 * (UDBS_SEG_SIZE) doorbell regions, each associated
4044 * with an egress queue. The first 64B has the doorbell
4045 * and the second 64B can be used to submit a tx work
4046 * request with an implicit doorbell.
4047 */
4048
4049 rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
4050 rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
4051 if (rc == 0) {
4052 clrbit(&sc->doorbells, DOORBELL_UDB);
4053 setbit(&sc->doorbells, DOORBELL_WCWR);
4054 setbit(&sc->doorbells, DOORBELL_UDBWC);
4055 } else {
4056 device_printf(sc->dev,
4057 "couldn't enable write combining: %d\n",
4058 rc);
4059 }
4060
4061 mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
4062 t4_write_reg(sc, A_SGE_STAT_CFG,
4063 V_STATSOURCE_T5(7) | mode);
4064 }
4065 #endif
4066 }
4067 sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
4068
4069 return (0);
4070 }
4071
4072 int
t4_adj_doorbells(struct adapter * sc)4073 t4_adj_doorbells(struct adapter *sc)
4074 {
4075 if ((sc->doorbells & t4_doorbells_allowed) != 0) {
4076 sc->doorbells &= t4_doorbells_allowed;
4077 return (0);
4078 }
4079 CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
4080 sc->doorbells, t4_doorbells_allowed);
4081 return (EINVAL);
4082 }
4083
4084 struct memwin_init {
4085 uint32_t base;
4086 uint32_t aperture;
4087 };
4088
4089 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
4090 { MEMWIN0_BASE, MEMWIN0_APERTURE },
4091 { MEMWIN1_BASE, MEMWIN1_APERTURE },
4092 { MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
4093 };
4094
4095 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
4096 { MEMWIN0_BASE, MEMWIN0_APERTURE },
4097 { MEMWIN1_BASE, MEMWIN1_APERTURE },
4098 { MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
4099 };
4100
4101 static void
setup_memwin(struct adapter * sc)4102 setup_memwin(struct adapter *sc)
4103 {
4104 const struct memwin_init *mw_init;
4105 struct memwin *mw;
4106 int i;
4107 uint32_t bar0, reg;
4108
4109 if (is_t4(sc)) {
4110 /*
4111 * Read low 32b of bar0 indirectly via the hardware backdoor
4112 * mechanism. Works from within PCI passthrough environments
4113 * too, where rman_get_start() can return a different value. We
4114 * need to program the T4 memory window decoders with the actual
4115 * addresses that will be coming across the PCIe link.
4116 */
4117 bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
4118 bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
4119
4120 mw_init = &t4_memwin[0];
4121 } else {
4122 /* T5+ use the relative offset inside the PCIe BAR */
4123 bar0 = 0;
4124
4125 mw_init = &t5_memwin[0];
4126 }
4127
4128 for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
4129 if (!rw_initialized(&mw->mw_lock)) {
4130 rw_init(&mw->mw_lock, "memory window access");
4131 mw->mw_base = mw_init->base;
4132 mw->mw_aperture = mw_init->aperture;
4133 mw->mw_curpos = 0;
4134 }
4135 reg = chip_id(sc) > CHELSIO_T6 ?
4136 PCIE_MEM_ACCESS_T7_REG(A_T7_PCIE_MEM_ACCESS_BASE_WIN, i) :
4137 PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i);
4138 t4_write_reg(sc, reg, (mw->mw_base + bar0) | V_BIR(0) |
4139 V_WINDOW(ilog2(mw->mw_aperture) - 10));
4140 rw_wlock(&mw->mw_lock);
4141 position_memwin(sc, i, mw->mw_curpos);
4142 rw_wunlock(&mw->mw_lock);
4143 }
4144
4145 /* flush */
4146 t4_read_reg(sc, reg);
4147 }
4148
4149 /*
4150 * Positions the memory window at the given address in the card's address space.
4151 * There are some alignment requirements and the actual position may be at an
4152 * address prior to the requested address. mw->mw_curpos always has the actual
4153 * position of the window.
4154 */
4155 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)4156 position_memwin(struct adapter *sc, int idx, uint32_t addr)
4157 {
4158 struct memwin *mw;
4159 uint32_t pf, reg, val;
4160
4161 MPASS(idx >= 0 && idx < NUM_MEMWIN);
4162 mw = &sc->memwin[idx];
4163 rw_assert(&mw->mw_lock, RA_WLOCKED);
4164
4165 if (is_t4(sc)) {
4166 pf = 0;
4167 mw->mw_curpos = addr & ~0xf; /* start must be 16B aligned */
4168 } else {
4169 pf = V_PFNUM(sc->pf);
4170 mw->mw_curpos = addr & ~0x7f; /* start must be 128B aligned */
4171 }
4172 if (chip_id(sc) > CHELSIO_T6) {
4173 reg = PCIE_MEM_ACCESS_T7_REG(A_PCIE_MEM_ACCESS_OFFSET0, idx);
4174 val = (mw->mw_curpos >> X_T7_MEMOFST_SHIFT) | pf;
4175 } else {
4176 reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
4177 val = mw->mw_curpos | pf;
4178 }
4179 t4_write_reg(sc, reg, val);
4180 t4_read_reg(sc, reg); /* flush */
4181 }
4182
4183 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)4184 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
4185 int len, int rw)
4186 {
4187 struct memwin *mw;
4188 uint32_t mw_end, v;
4189
4190 MPASS(idx >= 0 && idx < NUM_MEMWIN);
4191
4192 /* Memory can only be accessed in naturally aligned 4 byte units */
4193 if (addr & 3 || len & 3 || len <= 0)
4194 return (EINVAL);
4195
4196 mw = &sc->memwin[idx];
4197 while (len > 0) {
4198 rw_rlock(&mw->mw_lock);
4199 mw_end = mw->mw_curpos + mw->mw_aperture;
4200 if (addr >= mw_end || addr < mw->mw_curpos) {
4201 /* Will need to reposition the window */
4202 if (!rw_try_upgrade(&mw->mw_lock)) {
4203 rw_runlock(&mw->mw_lock);
4204 rw_wlock(&mw->mw_lock);
4205 }
4206 rw_assert(&mw->mw_lock, RA_WLOCKED);
4207 position_memwin(sc, idx, addr);
4208 rw_downgrade(&mw->mw_lock);
4209 mw_end = mw->mw_curpos + mw->mw_aperture;
4210 }
4211 rw_assert(&mw->mw_lock, RA_RLOCKED);
4212 while (addr < mw_end && len > 0) {
4213 if (rw == 0) {
4214 v = t4_read_reg(sc, mw->mw_base + addr -
4215 mw->mw_curpos);
4216 *val++ = le32toh(v);
4217 } else {
4218 v = *val++;
4219 t4_write_reg(sc, mw->mw_base + addr -
4220 mw->mw_curpos, htole32(v));
4221 }
4222 addr += 4;
4223 len -= 4;
4224 }
4225 rw_runlock(&mw->mw_lock);
4226 }
4227
4228 return (0);
4229 }
4230
4231 CTASSERT(M_TID_COOKIE == M_COOKIE);
4232 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
4233
4234 static void
t4_init_atid_table(struct adapter * sc)4235 t4_init_atid_table(struct adapter *sc)
4236 {
4237 struct tid_info *t;
4238 int i;
4239
4240 t = &sc->tids;
4241 if (t->natids == 0)
4242 return;
4243
4244 MPASS(t->atid_tab == NULL);
4245
4246 t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
4247 M_ZERO | M_WAITOK);
4248 mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
4249 t->afree = t->atid_tab;
4250 t->atids_in_use = 0;
4251 t->atid_alloc_stopped = false;
4252 for (i = 1; i < t->natids; i++)
4253 t->atid_tab[i - 1].next = &t->atid_tab[i];
4254 t->atid_tab[t->natids - 1].next = NULL;
4255 }
4256
4257 static void
t4_free_atid_table(struct adapter * sc)4258 t4_free_atid_table(struct adapter *sc)
4259 {
4260 struct tid_info *t;
4261
4262 t = &sc->tids;
4263
4264 KASSERT(t->atids_in_use == 0,
4265 ("%s: %d atids still in use.", __func__, t->atids_in_use));
4266
4267 if (mtx_initialized(&t->atid_lock))
4268 mtx_destroy(&t->atid_lock);
4269 free(t->atid_tab, M_CXGBE);
4270 t->atid_tab = NULL;
4271 }
4272
4273 static void
stop_atid_allocator(struct adapter * sc)4274 stop_atid_allocator(struct adapter *sc)
4275 {
4276 struct tid_info *t = &sc->tids;
4277
4278 if (t->natids == 0)
4279 return;
4280 mtx_lock(&t->atid_lock);
4281 t->atid_alloc_stopped = true;
4282 mtx_unlock(&t->atid_lock);
4283 }
4284
4285 static void
restart_atid_allocator(struct adapter * sc)4286 restart_atid_allocator(struct adapter *sc)
4287 {
4288 struct tid_info *t = &sc->tids;
4289
4290 if (t->natids == 0)
4291 return;
4292 mtx_lock(&t->atid_lock);
4293 KASSERT(t->atids_in_use == 0,
4294 ("%s: %d atids still in use.", __func__, t->atids_in_use));
4295 t->atid_alloc_stopped = false;
4296 mtx_unlock(&t->atid_lock);
4297 }
4298
4299 int
alloc_atid(struct adapter * sc,void * ctx)4300 alloc_atid(struct adapter *sc, void *ctx)
4301 {
4302 struct tid_info *t = &sc->tids;
4303 int atid = -1;
4304
4305 mtx_lock(&t->atid_lock);
4306 if (t->afree && !t->atid_alloc_stopped) {
4307 union aopen_entry *p = t->afree;
4308
4309 atid = p - t->atid_tab;
4310 MPASS(atid <= M_TID_TID);
4311 t->afree = p->next;
4312 p->data = ctx;
4313 t->atids_in_use++;
4314 }
4315 mtx_unlock(&t->atid_lock);
4316 return (atid);
4317 }
4318
4319 void *
lookup_atid(struct adapter * sc,int atid)4320 lookup_atid(struct adapter *sc, int atid)
4321 {
4322 struct tid_info *t = &sc->tids;
4323
4324 return (t->atid_tab[atid].data);
4325 }
4326
4327 void
free_atid(struct adapter * sc,int atid)4328 free_atid(struct adapter *sc, int atid)
4329 {
4330 struct tid_info *t = &sc->tids;
4331 union aopen_entry *p = &t->atid_tab[atid];
4332
4333 mtx_lock(&t->atid_lock);
4334 p->next = t->afree;
4335 t->afree = p;
4336 t->atids_in_use--;
4337 mtx_unlock(&t->atid_lock);
4338 }
4339
4340 static void
queue_tid_release(struct adapter * sc,int tid)4341 queue_tid_release(struct adapter *sc, int tid)
4342 {
4343
4344 CXGBE_UNIMPLEMENTED("deferred tid release");
4345 }
4346
4347 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4348 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4349 {
4350 struct wrqe *wr;
4351 struct cpl_tid_release *req;
4352
4353 wr = alloc_wrqe(sizeof(*req), ctrlq);
4354 if (wr == NULL) {
4355 queue_tid_release(sc, tid); /* defer */
4356 return;
4357 }
4358 req = wrtod(wr);
4359
4360 INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4361
4362 t4_wrq_tx(sc, wr);
4363 }
4364
4365 static int
t4_range_cmp(const void * a,const void * b)4366 t4_range_cmp(const void *a, const void *b)
4367 {
4368 return ((const struct t4_range *)a)->start -
4369 ((const struct t4_range *)b)->start;
4370 }
4371
4372 /*
4373 * Verify that the memory range specified by the addr/len pair is valid within
4374 * the card's address space.
4375 */
4376 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4377 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4378 {
4379 struct t4_range mem_ranges[4], *r, *next;
4380 uint32_t em, addr_len;
4381 int i, n, remaining;
4382
4383 /* Memory can only be accessed in naturally aligned 4 byte units */
4384 if (addr & 3 || len & 3 || len == 0)
4385 return (EINVAL);
4386
4387 /* Enabled memories */
4388 em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4389
4390 r = &mem_ranges[0];
4391 n = 0;
4392 bzero(r, sizeof(mem_ranges));
4393 if (em & F_EDRAM0_ENABLE) {
4394 addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4395 r->size = G_EDRAM0_SIZE(addr_len) << 20;
4396 if (r->size > 0) {
4397 r->start = G_EDRAM0_BASE(addr_len) << 20;
4398 if (addr >= r->start &&
4399 addr + len <= r->start + r->size)
4400 return (0);
4401 r++;
4402 n++;
4403 }
4404 }
4405 if (em & F_EDRAM1_ENABLE) {
4406 addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4407 r->size = G_EDRAM1_SIZE(addr_len) << 20;
4408 if (r->size > 0) {
4409 r->start = G_EDRAM1_BASE(addr_len) << 20;
4410 if (addr >= r->start &&
4411 addr + len <= r->start + r->size)
4412 return (0);
4413 r++;
4414 n++;
4415 }
4416 }
4417 if (em & F_EXT_MEM_ENABLE) {
4418 addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4419 r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4420 if (r->size > 0) {
4421 r->start = G_EXT_MEM_BASE(addr_len) << 20;
4422 if (addr >= r->start &&
4423 addr + len <= r->start + r->size)
4424 return (0);
4425 r++;
4426 n++;
4427 }
4428 }
4429 if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4430 addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4431 r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4432 if (r->size > 0) {
4433 r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4434 if (addr >= r->start &&
4435 addr + len <= r->start + r->size)
4436 return (0);
4437 r++;
4438 n++;
4439 }
4440 }
4441 MPASS(n <= nitems(mem_ranges));
4442
4443 if (n > 1) {
4444 /* Sort and merge the ranges. */
4445 qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4446
4447 /* Start from index 0 and examine the next n - 1 entries. */
4448 r = &mem_ranges[0];
4449 for (remaining = n - 1; remaining > 0; remaining--, r++) {
4450
4451 MPASS(r->size > 0); /* r is a valid entry. */
4452 next = r + 1;
4453 MPASS(next->size > 0); /* and so is the next one. */
4454
4455 while (r->start + r->size >= next->start) {
4456 /* Merge the next one into the current entry. */
4457 r->size = max(r->start + r->size,
4458 next->start + next->size) - r->start;
4459 n--; /* One fewer entry in total. */
4460 if (--remaining == 0)
4461 goto done; /* short circuit */
4462 next++;
4463 }
4464 if (next != r + 1) {
4465 /*
4466 * Some entries were merged into r and next
4467 * points to the first valid entry that couldn't
4468 * be merged.
4469 */
4470 MPASS(next->size > 0); /* must be valid */
4471 memcpy(r + 1, next, remaining * sizeof(*r));
4472 #ifdef INVARIANTS
4473 /*
4474 * This so that the foo->size assertion in the
4475 * next iteration of the loop do the right
4476 * thing for entries that were pulled up and are
4477 * no longer valid.
4478 */
4479 MPASS(n < nitems(mem_ranges));
4480 bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4481 sizeof(struct t4_range));
4482 #endif
4483 }
4484 }
4485 done:
4486 /* Done merging the ranges. */
4487 MPASS(n > 0);
4488 r = &mem_ranges[0];
4489 for (i = 0; i < n; i++, r++) {
4490 if (addr >= r->start &&
4491 addr + len <= r->start + r->size)
4492 return (0);
4493 }
4494 }
4495
4496 return (EFAULT);
4497 }
4498
4499 static int
fwmtype_to_hwmtype(int mtype)4500 fwmtype_to_hwmtype(int mtype)
4501 {
4502
4503 switch (mtype) {
4504 case FW_MEMTYPE_EDC0:
4505 return (MEM_EDC0);
4506 case FW_MEMTYPE_EDC1:
4507 return (MEM_EDC1);
4508 case FW_MEMTYPE_EXTMEM:
4509 return (MEM_MC0);
4510 case FW_MEMTYPE_EXTMEM1:
4511 return (MEM_MC1);
4512 default:
4513 panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4514 }
4515 }
4516
4517 /*
4518 * Verify that the memory range specified by the memtype/offset/len pair is
4519 * valid and lies entirely within the memtype specified. The global address of
4520 * the start of the range is returned in addr.
4521 */
4522 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4523 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4524 uint32_t *addr)
4525 {
4526 uint32_t em, addr_len, maddr;
4527
4528 /* Memory can only be accessed in naturally aligned 4 byte units */
4529 if (off & 3 || len & 3 || len == 0)
4530 return (EINVAL);
4531
4532 em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4533 switch (fwmtype_to_hwmtype(mtype)) {
4534 case MEM_EDC0:
4535 if (!(em & F_EDRAM0_ENABLE))
4536 return (EINVAL);
4537 addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4538 maddr = G_EDRAM0_BASE(addr_len) << 20;
4539 break;
4540 case MEM_EDC1:
4541 if (!(em & F_EDRAM1_ENABLE))
4542 return (EINVAL);
4543 addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4544 maddr = G_EDRAM1_BASE(addr_len) << 20;
4545 break;
4546 case MEM_MC:
4547 if (!(em & F_EXT_MEM_ENABLE))
4548 return (EINVAL);
4549 addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4550 maddr = G_EXT_MEM_BASE(addr_len) << 20;
4551 break;
4552 case MEM_MC1:
4553 if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4554 return (EINVAL);
4555 addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4556 maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4557 break;
4558 default:
4559 return (EINVAL);
4560 }
4561
4562 *addr = maddr + off; /* global address */
4563 return (validate_mem_range(sc, *addr, len));
4564 }
4565
4566 static int
fixup_devlog_params(struct adapter * sc)4567 fixup_devlog_params(struct adapter *sc)
4568 {
4569 struct devlog_params *dparams = &sc->params.devlog;
4570 int rc;
4571
4572 rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4573 dparams->size, &dparams->addr);
4574
4575 return (rc);
4576 }
4577
4578 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4579 update_nirq(struct intrs_and_queues *iaq, int nports)
4580 {
4581
4582 iaq->nirq = T4_EXTRA_INTR;
4583 iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4584 iaq->nirq += nports * iaq->nofldrxq;
4585 iaq->nirq += nports * (iaq->num_vis - 1) *
4586 max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4587 iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4588 }
4589
4590 /*
4591 * Adjust requirements to fit the number of interrupts available.
4592 */
4593 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4594 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4595 int navail)
4596 {
4597 int old_nirq;
4598 const int nports = sc->params.nports;
4599
4600 MPASS(nports > 0);
4601 MPASS(navail > 0);
4602
4603 bzero(iaq, sizeof(*iaq));
4604 iaq->intr_type = itype;
4605 iaq->num_vis = t4_num_vis;
4606 iaq->ntxq = t4_ntxq;
4607 iaq->ntxq_vi = t4_ntxq_vi;
4608 iaq->nrxq = t4_nrxq;
4609 iaq->nrxq_vi = t4_nrxq_vi;
4610 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4611 if (is_offload(sc) || is_ethoffload(sc)) {
4612 if (sc->params.tid_qid_sel_mask == 0) {
4613 iaq->nofldtxq = t4_nofldtxq;
4614 iaq->nofldtxq_vi = t4_nofldtxq_vi;
4615 } else {
4616 iaq->nofldtxq = roundup(t4_nofldtxq, sc->params.ncores);
4617 iaq->nofldtxq_vi = roundup(t4_nofldtxq_vi,
4618 sc->params.ncores);
4619 if (iaq->nofldtxq != t4_nofldtxq)
4620 device_printf(sc->dev,
4621 "nofldtxq updated (%d -> %d) for correct"
4622 " operation with %d firmware cores.\n",
4623 t4_nofldtxq, iaq->nofldtxq,
4624 sc->params.ncores);
4625 if (iaq->num_vis > 1 &&
4626 iaq->nofldtxq_vi != t4_nofldtxq_vi)
4627 device_printf(sc->dev,
4628 "nofldtxq_vi updated (%d -> %d) for correct"
4629 " operation with %d firmware cores.\n",
4630 t4_nofldtxq_vi, iaq->nofldtxq_vi,
4631 sc->params.ncores);
4632 }
4633 }
4634 #endif
4635 #ifdef TCP_OFFLOAD
4636 if (is_offload(sc)) {
4637 iaq->nofldrxq = t4_nofldrxq;
4638 iaq->nofldrxq_vi = t4_nofldrxq_vi;
4639 }
4640 #endif
4641 #ifdef DEV_NETMAP
4642 if (t4_native_netmap & NN_MAIN_VI) {
4643 iaq->nnmtxq = t4_nnmtxq;
4644 iaq->nnmrxq = t4_nnmrxq;
4645 }
4646 if (t4_native_netmap & NN_EXTRA_VI) {
4647 iaq->nnmtxq_vi = t4_nnmtxq_vi;
4648 iaq->nnmrxq_vi = t4_nnmrxq_vi;
4649 }
4650 #endif
4651
4652 update_nirq(iaq, nports);
4653 if (iaq->nirq <= navail &&
4654 (itype != INTR_MSI || powerof2(iaq->nirq))) {
4655 /*
4656 * This is the normal case -- there are enough interrupts for
4657 * everything.
4658 */
4659 goto done;
4660 }
4661
4662 /*
4663 * If extra VIs have been configured try reducing their count and see if
4664 * that works.
4665 */
4666 while (iaq->num_vis > 1) {
4667 iaq->num_vis--;
4668 update_nirq(iaq, nports);
4669 if (iaq->nirq <= navail &&
4670 (itype != INTR_MSI || powerof2(iaq->nirq))) {
4671 device_printf(sc->dev, "virtual interfaces per port "
4672 "reduced to %d from %d. nrxq=%u, nofldrxq=%u, "
4673 "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u. "
4674 "itype %d, navail %u, nirq %d.\n",
4675 iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4676 iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4677 itype, navail, iaq->nirq);
4678 goto done;
4679 }
4680 }
4681
4682 /*
4683 * Extra VIs will not be created. Log a message if they were requested.
4684 */
4685 MPASS(iaq->num_vis == 1);
4686 iaq->ntxq_vi = iaq->nrxq_vi = 0;
4687 iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4688 iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4689 if (iaq->num_vis != t4_num_vis) {
4690 device_printf(sc->dev, "extra virtual interfaces disabled. "
4691 "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4692 "nnmrxq_vi=%u. itype %d, navail %u, nirq %d.\n",
4693 iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4694 iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4695 }
4696
4697 /*
4698 * Keep reducing the number of NIC rx queues to the next lower power of
4699 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4700 * if that works.
4701 */
4702 do {
4703 if (iaq->nrxq > 1) {
4704 iaq->nrxq = rounddown_pow_of_two(iaq->nrxq - 1);
4705 if (iaq->nnmrxq > iaq->nrxq)
4706 iaq->nnmrxq = iaq->nrxq;
4707 }
4708 if (iaq->nofldrxq > 1)
4709 iaq->nofldrxq >>= 1;
4710
4711 old_nirq = iaq->nirq;
4712 update_nirq(iaq, nports);
4713 if (iaq->nirq <= navail &&
4714 (itype != INTR_MSI || powerof2(iaq->nirq))) {
4715 device_printf(sc->dev, "running with reduced number of "
4716 "rx queues because of shortage of interrupts. "
4717 "nrxq=%u, nofldrxq=%u. "
4718 "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4719 iaq->nofldrxq, itype, navail, iaq->nirq);
4720 goto done;
4721 }
4722 } while (old_nirq != iaq->nirq);
4723
4724 /* One interrupt for everything. Ugh. */
4725 device_printf(sc->dev, "running with minimal number of queues. "
4726 "itype %d, navail %u.\n", itype, navail);
4727 iaq->nirq = 1;
4728 iaq->nrxq = 1;
4729 iaq->ntxq = 1;
4730 if (iaq->nofldrxq > 0) {
4731 iaq->nofldrxq = 1;
4732 iaq->nofldtxq = 1;
4733 if (sc->params.tid_qid_sel_mask == 0)
4734 iaq->nofldtxq = 1;
4735 else
4736 iaq->nofldtxq = sc->params.ncores;
4737 }
4738 iaq->nnmtxq = 0;
4739 iaq->nnmrxq = 0;
4740 done:
4741 MPASS(iaq->num_vis > 0);
4742 if (iaq->num_vis > 1) {
4743 MPASS(iaq->nrxq_vi > 0);
4744 MPASS(iaq->ntxq_vi > 0);
4745 }
4746 MPASS(iaq->nirq > 0);
4747 MPASS(iaq->nrxq > 0);
4748 MPASS(iaq->ntxq > 0);
4749 if (itype == INTR_MSI)
4750 MPASS(powerof2(iaq->nirq));
4751 if (sc->params.tid_qid_sel_mask != 0)
4752 MPASS(iaq->nofldtxq % sc->params.ncores == 0);
4753 }
4754
4755 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4756 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4757 {
4758 int rc, itype, navail, nalloc;
4759
4760 for (itype = INTR_MSIX; itype; itype >>= 1) {
4761
4762 if ((itype & t4_intr_types) == 0)
4763 continue; /* not allowed */
4764
4765 if (itype == INTR_MSIX)
4766 navail = pci_msix_count(sc->dev);
4767 else if (itype == INTR_MSI)
4768 navail = pci_msi_count(sc->dev);
4769 else
4770 navail = 1;
4771 restart:
4772 if (navail == 0)
4773 continue;
4774
4775 calculate_iaq(sc, iaq, itype, navail);
4776 nalloc = iaq->nirq;
4777 rc = 0;
4778 if (itype == INTR_MSIX)
4779 rc = pci_alloc_msix(sc->dev, &nalloc);
4780 else if (itype == INTR_MSI)
4781 rc = pci_alloc_msi(sc->dev, &nalloc);
4782
4783 if (rc == 0 && nalloc > 0) {
4784 if (nalloc == iaq->nirq)
4785 return (0);
4786
4787 /*
4788 * Didn't get the number requested. Use whatever number
4789 * the kernel is willing to allocate.
4790 */
4791 device_printf(sc->dev, "fewer vectors than requested, "
4792 "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4793 itype, iaq->nirq, nalloc);
4794 pci_release_msi(sc->dev);
4795 navail = nalloc;
4796 goto restart;
4797 }
4798
4799 device_printf(sc->dev,
4800 "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4801 itype, rc, iaq->nirq, nalloc);
4802 }
4803
4804 device_printf(sc->dev,
4805 "failed to find a usable interrupt type. "
4806 "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4807 pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4808
4809 return (ENXIO);
4810 }
4811
4812 #define FW_VERSION(chip) ( \
4813 V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4814 V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4815 V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4816 V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4817 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4818
4819 /* Just enough of fw_hdr to cover all version info. */
4820 struct fw_h {
4821 __u8 ver;
4822 __u8 chip;
4823 __be16 len512;
4824 __be32 fw_ver;
4825 __be32 tp_microcode_ver;
4826 __u8 intfver_nic;
4827 __u8 intfver_vnic;
4828 __u8 intfver_ofld;
4829 __u8 intfver_ri;
4830 __u8 intfver_iscsipdu;
4831 __u8 intfver_iscsi;
4832 __u8 intfver_fcoepdu;
4833 __u8 intfver_fcoe;
4834 };
4835 /* Spot check a couple of fields. */
4836 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4837 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4838 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4839
4840 struct fw_info {
4841 uint8_t chip;
4842 char *kld_name;
4843 char *fw_mod_name;
4844 struct fw_h fw_h;
4845 } fw_info[] = {
4846 {
4847 .chip = CHELSIO_T4,
4848 .kld_name = "t4fw_cfg",
4849 .fw_mod_name = "t4fw",
4850 .fw_h = {
4851 .chip = FW_HDR_CHIP_T4,
4852 .fw_ver = htobe32(FW_VERSION(T4)),
4853 .intfver_nic = FW_INTFVER(T4, NIC),
4854 .intfver_vnic = FW_INTFVER(T4, VNIC),
4855 .intfver_ofld = FW_INTFVER(T4, OFLD),
4856 .intfver_ri = FW_INTFVER(T4, RI),
4857 .intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4858 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
4859 .intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4860 .intfver_fcoe = FW_INTFVER(T4, FCOE),
4861 },
4862 }, {
4863 .chip = CHELSIO_T5,
4864 .kld_name = "t5fw_cfg",
4865 .fw_mod_name = "t5fw",
4866 .fw_h = {
4867 .chip = FW_HDR_CHIP_T5,
4868 .fw_ver = htobe32(FW_VERSION(T5)),
4869 .intfver_nic = FW_INTFVER(T5, NIC),
4870 .intfver_vnic = FW_INTFVER(T5, VNIC),
4871 .intfver_ofld = FW_INTFVER(T5, OFLD),
4872 .intfver_ri = FW_INTFVER(T5, RI),
4873 .intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4874 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
4875 .intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4876 .intfver_fcoe = FW_INTFVER(T5, FCOE),
4877 },
4878 }, {
4879 .chip = CHELSIO_T6,
4880 .kld_name = "t6fw_cfg",
4881 .fw_mod_name = "t6fw",
4882 .fw_h = {
4883 .chip = FW_HDR_CHIP_T6,
4884 .fw_ver = htobe32(FW_VERSION(T6)),
4885 .intfver_nic = FW_INTFVER(T6, NIC),
4886 .intfver_vnic = FW_INTFVER(T6, VNIC),
4887 .intfver_ofld = FW_INTFVER(T6, OFLD),
4888 .intfver_ri = FW_INTFVER(T6, RI),
4889 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4890 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
4891 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4892 .intfver_fcoe = FW_INTFVER(T6, FCOE),
4893 },
4894 }, {
4895 .chip = CHELSIO_T7,
4896 .kld_name = "t7fw_cfg",
4897 .fw_mod_name = "t7fw",
4898 .fw_h = {
4899 .chip = FW_HDR_CHIP_T7,
4900 .fw_ver = htobe32(FW_VERSION(T7)),
4901 .intfver_nic = FW_INTFVER(T7, NIC),
4902 .intfver_vnic = FW_INTFVER(T7, VNIC),
4903 .intfver_ofld = FW_INTFVER(T7, OFLD),
4904 .intfver_ri = FW_INTFVER(T7, RI),
4905 .intfver_iscsipdu = FW_INTFVER(T7, ISCSIPDU),
4906 .intfver_iscsi = FW_INTFVER(T7, ISCSI),
4907 .intfver_fcoepdu = FW_INTFVER(T7, FCOEPDU),
4908 .intfver_fcoe = FW_INTFVER(T7, FCOE),
4909 },
4910 }
4911 };
4912
4913 static struct fw_info *
find_fw_info(int chip)4914 find_fw_info(int chip)
4915 {
4916 int i;
4917
4918 for (i = 0; i < nitems(fw_info); i++) {
4919 if (fw_info[i].chip == chip)
4920 return (&fw_info[i]);
4921 }
4922 return (NULL);
4923 }
4924
4925 /*
4926 * Is the given firmware API compatible with the one the driver was compiled
4927 * with?
4928 */
4929 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4930 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4931 {
4932
4933 /* short circuit if it's the exact same firmware version */
4934 if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4935 return (1);
4936
4937 /*
4938 * XXX: Is this too conservative? Perhaps I should limit this to the
4939 * features that are supported in the driver.
4940 */
4941 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4942 if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4943 SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4944 SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4945 return (1);
4946 #undef SAME_INTF
4947
4948 return (0);
4949 }
4950
4951 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4952 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4953 const struct firmware **fw)
4954 {
4955 struct fw_info *fw_info;
4956
4957 *dcfg = NULL;
4958 if (fw != NULL)
4959 *fw = NULL;
4960
4961 fw_info = find_fw_info(chip_id(sc));
4962 if (fw_info == NULL) {
4963 device_printf(sc->dev,
4964 "unable to look up firmware information for chip %d.\n",
4965 chip_id(sc));
4966 return (EINVAL);
4967 }
4968
4969 *dcfg = firmware_get(fw_info->kld_name);
4970 if (*dcfg != NULL) {
4971 if (fw != NULL)
4972 *fw = firmware_get(fw_info->fw_mod_name);
4973 return (0);
4974 }
4975
4976 return (ENOENT);
4977 }
4978
4979 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4980 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4981 const struct firmware *fw)
4982 {
4983
4984 if (fw != NULL)
4985 firmware_put(fw, FIRMWARE_UNLOAD);
4986 if (dcfg != NULL)
4987 firmware_put(dcfg, FIRMWARE_UNLOAD);
4988 }
4989
4990 /*
4991 * Return values:
4992 * 0 means no firmware install attempted.
4993 * ERESTART means a firmware install was attempted and was successful.
4994 * +ve errno means a firmware install was attempted but failed.
4995 */
4996 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4997 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4998 const struct fw_h *drv_fw, const char *reason, int *already)
4999 {
5000 const struct firmware *cfg, *fw;
5001 const uint32_t c = be32toh(card_fw->fw_ver);
5002 uint32_t d, k;
5003 int rc, fw_install;
5004 struct fw_h bundled_fw;
5005 bool load_attempted;
5006
5007 cfg = fw = NULL;
5008 load_attempted = false;
5009 fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
5010
5011 memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
5012 if (t4_fw_install < 0) {
5013 rc = load_fw_module(sc, &cfg, &fw);
5014 if (rc != 0 || fw == NULL) {
5015 device_printf(sc->dev,
5016 "failed to load firmware module: %d. cfg %p, fw %p;"
5017 " will use compiled-in firmware version for"
5018 "hw.cxgbe.fw_install checks.\n",
5019 rc, cfg, fw);
5020 } else {
5021 memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
5022 }
5023 load_attempted = true;
5024 }
5025 d = be32toh(bundled_fw.fw_ver);
5026
5027 if (reason != NULL)
5028 goto install;
5029
5030 if ((sc->flags & FW_OK) == 0) {
5031
5032 if (c == 0xffffffff) {
5033 reason = "missing";
5034 goto install;
5035 }
5036
5037 rc = 0;
5038 goto done;
5039 }
5040
5041 if (!fw_compatible(card_fw, &bundled_fw)) {
5042 reason = "incompatible or unusable";
5043 goto install;
5044 }
5045
5046 if (d > c) {
5047 reason = "older than the version bundled with this driver";
5048 goto install;
5049 }
5050
5051 if (fw_install == 2 && d != c) {
5052 reason = "different than the version bundled with this driver";
5053 goto install;
5054 }
5055
5056 /* No reason to do anything to the firmware already on the card. */
5057 rc = 0;
5058 goto done;
5059
5060 install:
5061 rc = 0;
5062 if ((*already)++)
5063 goto done;
5064
5065 if (fw_install == 0) {
5066 device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5067 "but the driver is prohibited from installing a firmware "
5068 "on the card.\n",
5069 G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5070 G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
5071
5072 goto done;
5073 }
5074
5075 /*
5076 * We'll attempt to install a firmware. Load the module first (if it
5077 * hasn't been loaded already).
5078 */
5079 if (!load_attempted) {
5080 rc = load_fw_module(sc, &cfg, &fw);
5081 if (rc != 0 || fw == NULL) {
5082 device_printf(sc->dev,
5083 "failed to load firmware module: %d. cfg %p, fw %p\n",
5084 rc, cfg, fw);
5085 /* carry on */
5086 }
5087 }
5088 if (fw == NULL) {
5089 device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5090 "but the driver cannot take corrective action because it "
5091 "is unable to load the firmware module.\n",
5092 G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5093 G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
5094 rc = sc->flags & FW_OK ? 0 : ENOENT;
5095 goto done;
5096 }
5097 k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
5098 if (k != d) {
5099 MPASS(t4_fw_install > 0);
5100 device_printf(sc->dev,
5101 "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
5102 "expecting (%u.%u.%u.%u) and will not be used.\n",
5103 G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
5104 G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
5105 G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
5106 G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
5107 rc = sc->flags & FW_OK ? 0 : EINVAL;
5108 goto done;
5109 }
5110
5111 device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5112 "installing firmware %u.%u.%u.%u on card.\n",
5113 G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5114 G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
5115 G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
5116 G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
5117
5118 rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
5119 if (rc != 0) {
5120 device_printf(sc->dev, "failed to install firmware: %d\n", rc);
5121 } else {
5122 /* Installed successfully, update the cached header too. */
5123 rc = ERESTART;
5124 memcpy(card_fw, fw->data, sizeof(*card_fw));
5125 }
5126 done:
5127 unload_fw_module(sc, cfg, fw);
5128
5129 return (rc);
5130 }
5131
5132 /*
5133 * Establish contact with the firmware and attempt to become the master driver.
5134 *
5135 * A firmware will be installed to the card if needed (if the driver is allowed
5136 * to do so).
5137 */
5138 static int
contact_firmware(struct adapter * sc)5139 contact_firmware(struct adapter *sc)
5140 {
5141 int rc, already = 0;
5142 enum dev_state state;
5143 struct fw_info *fw_info;
5144 struct fw_hdr *card_fw; /* fw on the card */
5145 const struct fw_h *drv_fw;
5146
5147 fw_info = find_fw_info(chip_id(sc));
5148 if (fw_info == NULL) {
5149 device_printf(sc->dev,
5150 "unable to look up firmware information for chip %d.\n",
5151 chip_id(sc));
5152 return (EINVAL);
5153 }
5154 drv_fw = &fw_info->fw_h;
5155
5156 /* Read the header of the firmware on the card */
5157 card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
5158 restart:
5159 rc = -t4_get_fw_hdr(sc, card_fw);
5160 if (rc != 0) {
5161 device_printf(sc->dev,
5162 "unable to read firmware header from card's flash: %d\n",
5163 rc);
5164 goto done;
5165 }
5166
5167 rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
5168 &already);
5169 if (rc == ERESTART)
5170 goto restart;
5171 if (rc != 0)
5172 goto done;
5173
5174 rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
5175 if (rc < 0 || state == DEV_STATE_ERR) {
5176 rc = -rc;
5177 device_printf(sc->dev,
5178 "failed to connect to the firmware: %d, %d. "
5179 "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5180 #if 0
5181 if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
5182 "not responding properly to HELLO", &already) == ERESTART)
5183 goto restart;
5184 #endif
5185 goto done;
5186 }
5187 MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
5188 sc->flags |= FW_OK; /* The firmware responded to the FW_HELLO. */
5189
5190 if (rc == sc->pf) {
5191 sc->flags |= MASTER_PF;
5192 rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
5193 NULL, &already);
5194 if (rc == ERESTART)
5195 rc = 0;
5196 else if (rc != 0)
5197 goto done;
5198 } else if (state == DEV_STATE_UNINIT) {
5199 /*
5200 * We didn't get to be the master so we definitely won't be
5201 * configuring the chip. It's a bug if someone else hasn't
5202 * configured it already.
5203 */
5204 device_printf(sc->dev, "couldn't be master(%d), "
5205 "device not already initialized either(%d). "
5206 "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5207 rc = EPROTO;
5208 goto done;
5209 } else {
5210 /*
5211 * Some other PF is the master and has configured the chip.
5212 * This is allowed but untested.
5213 */
5214 device_printf(sc->dev, "PF%d is master, device state %d. "
5215 "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5216 snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
5217 sc->cfcsum = 0;
5218 rc = 0;
5219 }
5220 done:
5221 if (rc != 0 && sc->flags & FW_OK) {
5222 t4_fw_bye(sc, sc->mbox);
5223 sc->flags &= ~FW_OK;
5224 }
5225 free(card_fw, M_CXGBE);
5226 return (rc);
5227 }
5228
5229 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff,u_int maxlen)5230 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
5231 uint32_t mtype, uint32_t moff, u_int maxlen)
5232 {
5233 struct fw_info *fw_info;
5234 const struct firmware *dcfg, *rcfg = NULL;
5235 const uint32_t *cfdata;
5236 uint32_t cflen, addr;
5237 int rc;
5238
5239 load_fw_module(sc, &dcfg, NULL);
5240
5241 /* Card specific interpretation of "default". */
5242 if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5243 if (pci_get_device(sc->dev) == 0x440a)
5244 snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
5245 if (is_fpga(sc))
5246 snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
5247 }
5248
5249 if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5250 if (dcfg == NULL) {
5251 device_printf(sc->dev,
5252 "KLD with default config is not available.\n");
5253 rc = ENOENT;
5254 goto done;
5255 }
5256 cfdata = dcfg->data;
5257 cflen = dcfg->datasize & ~3;
5258 } else {
5259 char s[32];
5260
5261 fw_info = find_fw_info(chip_id(sc));
5262 if (fw_info == NULL) {
5263 device_printf(sc->dev,
5264 "unable to look up firmware information for chip %d.\n",
5265 chip_id(sc));
5266 rc = EINVAL;
5267 goto done;
5268 }
5269 snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
5270
5271 rcfg = firmware_get(s);
5272 if (rcfg == NULL) {
5273 device_printf(sc->dev,
5274 "unable to load module \"%s\" for configuration "
5275 "profile \"%s\".\n", s, cfg_file);
5276 rc = ENOENT;
5277 goto done;
5278 }
5279 cfdata = rcfg->data;
5280 cflen = rcfg->datasize & ~3;
5281 }
5282
5283 if (cflen > maxlen) {
5284 device_printf(sc->dev,
5285 "config file too long (%d, max allowed is %d).\n",
5286 cflen, maxlen);
5287 rc = EINVAL;
5288 goto done;
5289 }
5290
5291 rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
5292 if (rc != 0) {
5293 device_printf(sc->dev,
5294 "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
5295 __func__, mtype, moff, cflen, rc);
5296 rc = EINVAL;
5297 goto done;
5298 }
5299 write_via_memwin(sc, 2, addr, cfdata, cflen);
5300 done:
5301 if (rcfg != NULL)
5302 firmware_put(rcfg, FIRMWARE_UNLOAD);
5303 unload_fw_module(sc, dcfg, NULL);
5304 return (rc);
5305 }
5306
5307 struct caps_allowed {
5308 uint16_t nbmcaps;
5309 uint16_t linkcaps;
5310 uint16_t switchcaps;
5311 uint16_t nvmecaps;
5312 uint16_t niccaps;
5313 uint16_t toecaps;
5314 uint16_t rdmacaps;
5315 uint16_t cryptocaps;
5316 uint16_t iscsicaps;
5317 uint16_t fcoecaps;
5318 };
5319
5320 #define FW_PARAM_DEV(param) \
5321 (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5322 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5323 #define FW_PARAM_PFVF(param) \
5324 (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5325 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5326
5327 /*
5328 * Provide a configuration profile to the firmware and have it initialize the
5329 * chip accordingly. This may involve uploading a configuration file to the
5330 * card.
5331 */
5332 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5333 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5334 const struct caps_allowed *caps_allowed)
5335 {
5336 int rc;
5337 struct fw_caps_config_cmd caps;
5338 uint32_t mtype, moff, finicsum, cfcsum, param, val;
5339 unsigned int maxlen = 0;
5340 const int cfg_addr = t4_flash_cfg_addr(sc, &maxlen);
5341
5342 rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5343 if (rc != 0) {
5344 device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5345 return (rc);
5346 }
5347
5348 bzero(&caps, sizeof(caps));
5349 caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5350 F_FW_CMD_REQUEST | F_FW_CMD_READ);
5351 if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5352 mtype = 0;
5353 moff = 0;
5354 caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5355 } else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5356 mtype = FW_MEMTYPE_FLASH;
5357 moff = cfg_addr;
5358 caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5359 V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5360 V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5361 FW_LEN16(caps));
5362 } else {
5363 /*
5364 * Ask the firmware where it wants us to upload the config file.
5365 */
5366 param = FW_PARAM_DEV(CF);
5367 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
5368 if (rc != 0) {
5369 /* No support for config file? Shouldn't happen. */
5370 device_printf(sc->dev,
5371 "failed to query config file location: %d.\n", rc);
5372 goto done;
5373 }
5374 mtype = G_FW_PARAMS_PARAM_Y(val);
5375 moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5376 caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5377 V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5378 V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5379 FW_LEN16(caps));
5380
5381 rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff, maxlen);
5382 if (rc != 0) {
5383 device_printf(sc->dev,
5384 "failed to upload config file to card: %d.\n", rc);
5385 goto done;
5386 }
5387 }
5388 rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5389 if (rc != 0) {
5390 device_printf(sc->dev, "failed to pre-process config file: %d "
5391 "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5392 goto done;
5393 }
5394
5395 finicsum = be32toh(caps.finicsum);
5396 cfcsum = be32toh(caps.cfcsum); /* actual */
5397 if (finicsum != cfcsum) {
5398 device_printf(sc->dev,
5399 "WARNING: config file checksum mismatch: %08x %08x\n",
5400 finicsum, cfcsum);
5401 }
5402 sc->cfcsum = cfcsum;
5403 snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5404
5405 /*
5406 * Let the firmware know what features will (not) be used so it can tune
5407 * things accordingly.
5408 */
5409 #define LIMIT_CAPS(x) do { \
5410 caps.x##caps &= htobe16(caps_allowed->x##caps); \
5411 } while (0)
5412 LIMIT_CAPS(nbm);
5413 LIMIT_CAPS(link);
5414 LIMIT_CAPS(switch);
5415 LIMIT_CAPS(nvme);
5416 LIMIT_CAPS(nic);
5417 LIMIT_CAPS(toe);
5418 LIMIT_CAPS(rdma);
5419 LIMIT_CAPS(crypto);
5420 LIMIT_CAPS(iscsi);
5421 LIMIT_CAPS(fcoe);
5422 #undef LIMIT_CAPS
5423 if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5424 /*
5425 * TOE and hashfilters are mutually exclusive. It is a config
5426 * file or firmware bug if both are reported as available. Try
5427 * to cope with the situation in non-debug builds by disabling
5428 * TOE.
5429 */
5430 MPASS(caps.toecaps == 0);
5431
5432 caps.toecaps = 0;
5433 caps.rdmacaps = 0;
5434 caps.iscsicaps = 0;
5435 caps.nvmecaps = 0;
5436 }
5437
5438 caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5439 F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5440 caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5441 rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5442 if (rc != 0) {
5443 device_printf(sc->dev,
5444 "failed to process config file: %d.\n", rc);
5445 goto done;
5446 }
5447
5448 t4_tweak_chip_settings(sc);
5449 set_params__pre_init(sc);
5450
5451 /* get basic stuff going */
5452 rc = -t4_fw_initialize(sc, sc->mbox);
5453 if (rc != 0) {
5454 device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5455 goto done;
5456 }
5457 done:
5458 return (rc);
5459 }
5460
5461 /*
5462 * Partition chip resources for use between various PFs, VFs, etc.
5463 */
5464 static int
partition_resources(struct adapter * sc)5465 partition_resources(struct adapter *sc)
5466 {
5467 char cfg_file[sizeof(t4_cfg_file)];
5468 struct caps_allowed caps_allowed;
5469 int rc;
5470 bool fallback;
5471
5472 /* Only the master driver gets to configure the chip resources. */
5473 MPASS(sc->flags & MASTER_PF);
5474
5475 #define COPY_CAPS(x) do { \
5476 caps_allowed.x##caps = t4_##x##caps_allowed; \
5477 } while (0)
5478 bzero(&caps_allowed, sizeof(caps_allowed));
5479 COPY_CAPS(nbm);
5480 COPY_CAPS(link);
5481 COPY_CAPS(switch);
5482 COPY_CAPS(nvme);
5483 COPY_CAPS(nic);
5484 COPY_CAPS(toe);
5485 COPY_CAPS(rdma);
5486 COPY_CAPS(crypto);
5487 COPY_CAPS(iscsi);
5488 COPY_CAPS(fcoe);
5489 fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5490 snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5491 retry:
5492 rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5493 if (rc != 0 && fallback) {
5494 dump_devlog(sc);
5495 device_printf(sc->dev,
5496 "failed (%d) to configure card with \"%s\" profile, "
5497 "will fall back to a basic configuration and retry.\n",
5498 rc, cfg_file);
5499 snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5500 bzero(&caps_allowed, sizeof(caps_allowed));
5501 COPY_CAPS(switch);
5502 caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5503 fallback = false;
5504 goto retry;
5505 }
5506 #undef COPY_CAPS
5507 return (rc);
5508 }
5509
5510 /*
5511 * Retrieve parameters that are needed (or nice to have) very early.
5512 */
5513 static int
get_params__pre_init(struct adapter * sc)5514 get_params__pre_init(struct adapter *sc)
5515 {
5516 int rc;
5517 uint32_t param[2], val[2];
5518
5519 t4_get_version_info(sc);
5520
5521 snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5522 G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5523 G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5524 G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5525 G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5526
5527 snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5528 G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5529 G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5530 G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5531 G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5532
5533 snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5534 G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5535 G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5536 G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5537 G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5538
5539 snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5540 G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5541 G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5542 G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5543 G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5544
5545 param[0] = FW_PARAM_DEV(PORTVEC);
5546 param[1] = FW_PARAM_DEV(CCLK);
5547 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5548 if (rc != 0) {
5549 device_printf(sc->dev,
5550 "failed to query parameters (pre_init): %d.\n", rc);
5551 return (rc);
5552 }
5553
5554 sc->params.portvec = val[0];
5555 sc->params.nports = bitcount32(val[0]);
5556 sc->params.vpd.cclk = val[1];
5557
5558 /* Read device log parameters. */
5559 rc = -t4_init_devlog_ncores_params(sc, 1);
5560 if (rc == 0)
5561 fixup_devlog_params(sc);
5562 else {
5563 device_printf(sc->dev,
5564 "failed to get devlog parameters: %d.\n", rc);
5565 rc = 0; /* devlog isn't critical for device operation */
5566 }
5567
5568 return (rc);
5569 }
5570
5571 /*
5572 * Any params that need to be set before FW_INITIALIZE.
5573 */
5574 static int
set_params__pre_init(struct adapter * sc)5575 set_params__pre_init(struct adapter *sc)
5576 {
5577 int rc = 0;
5578 uint32_t param, val;
5579
5580 if (chip_id(sc) >= CHELSIO_T6) {
5581 param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5582 val = 1;
5583 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
5584 /* firmwares < 1.20.1.0 do not have this param. */
5585 if (rc == FW_EINVAL &&
5586 sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5587 rc = 0;
5588 }
5589 if (rc != 0) {
5590 device_printf(sc->dev,
5591 "failed to enable high priority filters :%d.\n",
5592 rc);
5593 }
5594
5595 param = FW_PARAM_DEV(PPOD_EDRAM);
5596 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
5597 if (rc == 0 && val == 1) {
5598 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m,
5599 &val);
5600 if (rc != 0) {
5601 device_printf(sc->dev,
5602 "failed to set PPOD_EDRAM: %d.\n", rc);
5603 }
5604 }
5605 }
5606
5607 /* Enable opaque VIIDs with firmwares that support it. */
5608 param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5609 val = 1;
5610 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
5611 if (rc == 0 && val == 1)
5612 sc->params.viid_smt_extn_support = true;
5613 else
5614 sc->params.viid_smt_extn_support = false;
5615
5616 return (rc);
5617 }
5618
5619 /*
5620 * Retrieve various parameters that are of interest to the driver. The device
5621 * has been initialized by the firmware at this point.
5622 */
5623 static int
get_params__post_init(struct adapter * sc)5624 get_params__post_init(struct adapter *sc)
5625 {
5626 int rc;
5627 uint32_t param[7], val[7];
5628 struct fw_caps_config_cmd caps;
5629
5630 param[0] = FW_PARAM_PFVF(IQFLINT_START);
5631 param[1] = FW_PARAM_PFVF(EQ_START);
5632 param[2] = FW_PARAM_PFVF(FILTER_START);
5633 param[3] = FW_PARAM_PFVF(FILTER_END);
5634 param[4] = FW_PARAM_PFVF(L2T_START);
5635 param[5] = FW_PARAM_PFVF(L2T_END);
5636 param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5637 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5638 V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5639 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5640 if (rc != 0) {
5641 device_printf(sc->dev,
5642 "failed to query parameters (post_init): %d.\n", rc);
5643 return (rc);
5644 }
5645
5646 sc->sge.iq_start = val[0];
5647 sc->sge.eq_start = val[1];
5648 if ((int)val[3] > (int)val[2]) {
5649 sc->tids.ftid_base = val[2];
5650 sc->tids.ftid_end = val[3];
5651 sc->tids.nftids = val[3] - val[2] + 1;
5652 }
5653 sc->vres.l2t.start = val[4];
5654 sc->vres.l2t.size = val[5] - val[4] + 1;
5655 /* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5656 if (sc->vres.l2t.size > 0)
5657 MPASS(fls(val[5]) <= S_SYNC_WR);
5658 sc->params.core_vdd = val[6];
5659
5660 param[0] = FW_PARAM_PFVF(IQFLINT_END);
5661 param[1] = FW_PARAM_PFVF(EQ_END);
5662 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5663 if (rc != 0) {
5664 device_printf(sc->dev,
5665 "failed to query parameters (post_init2): %d.\n", rc);
5666 return (rc);
5667 }
5668 MPASS((int)val[0] >= sc->sge.iq_start);
5669 sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5670 MPASS((int)val[1] >= sc->sge.eq_start);
5671 sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5672
5673 if (chip_id(sc) >= CHELSIO_T6) {
5674
5675 sc->tids.tid_base = t4_read_reg(sc,
5676 A_LE_DB_ACTIVE_TABLE_START_INDEX);
5677
5678 param[0] = FW_PARAM_PFVF(HPFILTER_START);
5679 param[1] = FW_PARAM_PFVF(HPFILTER_END);
5680 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5681 if (rc != 0) {
5682 device_printf(sc->dev,
5683 "failed to query hpfilter parameters: %d.\n", rc);
5684 return (rc);
5685 }
5686 if ((int)val[1] > (int)val[0]) {
5687 sc->tids.hpftid_base = val[0];
5688 sc->tids.hpftid_end = val[1];
5689 sc->tids.nhpftids = val[1] - val[0] + 1;
5690
5691 /*
5692 * These should go off if the layout changes and the
5693 * driver needs to catch up.
5694 */
5695 MPASS(sc->tids.hpftid_base == 0);
5696 MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5697 }
5698
5699 param[0] = FW_PARAM_PFVF(RAWF_START);
5700 param[1] = FW_PARAM_PFVF(RAWF_END);
5701 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5702 if (rc != 0) {
5703 device_printf(sc->dev,
5704 "failed to query rawf parameters: %d.\n", rc);
5705 return (rc);
5706 }
5707 if ((int)val[1] > (int)val[0]) {
5708 sc->rawf_base = val[0];
5709 sc->nrawf = val[1] - val[0] + 1;
5710 }
5711 }
5712
5713 if (sc->params.ncores > 1) {
5714 MPASS(chip_id(sc) >= CHELSIO_T7);
5715
5716 param[0] = FW_PARAM_DEV(TID_QID_SEL_MASK);
5717 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5718 sc->params.tid_qid_sel_mask = rc == 0 ? val[0] : 0;
5719 }
5720
5721 /*
5722 * The parameters that follow may not be available on all firmwares. We
5723 * query them individually rather than in a compound query because old
5724 * firmwares fail the entire query if an unknown parameter is queried.
5725 */
5726
5727 /*
5728 * MPS buffer group configuration.
5729 */
5730 param[0] = FW_PARAM_DEV(MPSBGMAP);
5731 val[0] = 0;
5732 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5733 if (rc == 0)
5734 sc->params.mps_bg_map = val[0];
5735 else
5736 sc->params.mps_bg_map = UINT32_MAX; /* Not a legal value. */
5737
5738 param[0] = FW_PARAM_DEV(TPCHMAP);
5739 val[0] = 0;
5740 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5741 if (rc == 0)
5742 sc->params.tp_ch_map = val[0];
5743 else
5744 sc->params.tp_ch_map = UINT32_MAX; /* Not a legal value. */
5745
5746 param[0] = FW_PARAM_DEV(TX_TPCHMAP);
5747 val[0] = 0;
5748 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5749 if (rc == 0)
5750 sc->params.tx_tp_ch_map = val[0];
5751 else
5752 sc->params.tx_tp_ch_map = UINT32_MAX; /* Not a legal value. */
5753
5754 /*
5755 * Determine whether the firmware supports the filter2 work request.
5756 */
5757 param[0] = FW_PARAM_DEV(FILTER2_WR);
5758 val[0] = 0;
5759 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5760 if (rc == 0)
5761 sc->params.filter2_wr_support = val[0] != 0;
5762 else
5763 sc->params.filter2_wr_support = 0;
5764
5765 /*
5766 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5767 */
5768 param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5769 val[0] = 0;
5770 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5771 if (rc == 0)
5772 sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5773 else
5774 sc->params.ulptx_memwrite_dsgl = false;
5775
5776 /* FW_RI_FR_NSMR_TPTE_WR support */
5777 param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5778 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5779 if (rc == 0)
5780 sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5781 else
5782 sc->params.fr_nsmr_tpte_wr_support = false;
5783
5784 /* Support for 512 SGL entries per FR MR. */
5785 param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5786 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5787 if (rc == 0)
5788 sc->params.dev_512sgl_mr = val[0] != 0;
5789 else
5790 sc->params.dev_512sgl_mr = false;
5791
5792 param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5793 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5794 if (rc == 0)
5795 sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5796 else
5797 sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5798
5799 param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5800 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5801 if (rc == 0) {
5802 MPASS(val[0] > 0 && val[0] < 256); /* nsched_cls is 8b */
5803 sc->params.nsched_cls = val[0];
5804 } else
5805 sc->params.nsched_cls = sc->chip_params->nsched_cls;
5806
5807 /* get capabilites */
5808 bzero(&caps, sizeof(caps));
5809 caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5810 F_FW_CMD_REQUEST | F_FW_CMD_READ);
5811 caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5812 rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5813 if (rc != 0) {
5814 device_printf(sc->dev,
5815 "failed to get card capabilities: %d.\n", rc);
5816 return (rc);
5817 }
5818
5819 #define READ_CAPS(x) do { \
5820 sc->x = htobe16(caps.x); \
5821 } while (0)
5822 READ_CAPS(nbmcaps);
5823 READ_CAPS(linkcaps);
5824 READ_CAPS(switchcaps);
5825 READ_CAPS(nvmecaps);
5826 READ_CAPS(niccaps);
5827 READ_CAPS(toecaps);
5828 READ_CAPS(rdmacaps);
5829 READ_CAPS(cryptocaps);
5830 READ_CAPS(iscsicaps);
5831 READ_CAPS(fcoecaps);
5832
5833 if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5834 MPASS(chip_id(sc) > CHELSIO_T4);
5835 MPASS(sc->toecaps == 0);
5836 sc->toecaps = 0;
5837
5838 param[0] = FW_PARAM_DEV(NTID);
5839 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5840 if (rc != 0) {
5841 device_printf(sc->dev,
5842 "failed to query HASHFILTER parameters: %d.\n", rc);
5843 return (rc);
5844 }
5845 sc->tids.ntids = val[0];
5846 if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5847 MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5848 sc->tids.ntids -= sc->tids.nhpftids;
5849 }
5850 sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5851 sc->params.hash_filter = 1;
5852 }
5853 if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5854 param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5855 param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5856 param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5857 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5858 if (rc != 0) {
5859 device_printf(sc->dev,
5860 "failed to query NIC parameters: %d.\n", rc);
5861 return (rc);
5862 }
5863 if ((int)val[1] > (int)val[0]) {
5864 sc->tids.etid_base = val[0];
5865 sc->tids.etid_end = val[1];
5866 sc->tids.netids = val[1] - val[0] + 1;
5867 sc->params.eo_wr_cred = val[2];
5868 sc->params.ethoffload = 1;
5869 }
5870 }
5871 if (sc->toecaps) {
5872 /* query offload-related parameters */
5873 param[0] = FW_PARAM_DEV(NTID);
5874 param[1] = FW_PARAM_PFVF(SERVER_START);
5875 param[2] = FW_PARAM_PFVF(SERVER_END);
5876 param[3] = FW_PARAM_PFVF(TDDP_START);
5877 param[4] = FW_PARAM_PFVF(TDDP_END);
5878 param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5879 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5880 if (rc != 0) {
5881 device_printf(sc->dev,
5882 "failed to query TOE parameters: %d.\n", rc);
5883 return (rc);
5884 }
5885 sc->tids.ntids = val[0];
5886 if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5887 MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5888 sc->tids.ntids -= sc->tids.nhpftids;
5889 }
5890 sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5891 if ((int)val[2] > (int)val[1]) {
5892 sc->tids.stid_base = val[1];
5893 sc->tids.nstids = val[2] - val[1] + 1;
5894 }
5895 sc->vres.ddp.start = val[3];
5896 sc->vres.ddp.size = val[4] - val[3] + 1;
5897 sc->params.ofldq_wr_cred = val[5];
5898 sc->params.offload = 1;
5899 } else {
5900 /*
5901 * The firmware attempts memfree TOE configuration for -SO cards
5902 * and will report toecaps=0 if it runs out of resources (this
5903 * depends on the config file). It may not report 0 for other
5904 * capabilities dependent on the TOE in this case. Set them to
5905 * 0 here so that the driver doesn't bother tracking resources
5906 * that will never be used.
5907 */
5908 sc->iscsicaps = 0;
5909 sc->nvmecaps = 0;
5910 sc->rdmacaps = 0;
5911 }
5912 if (sc->nvmecaps || sc->rdmacaps) {
5913 param[0] = FW_PARAM_PFVF(STAG_START);
5914 param[1] = FW_PARAM_PFVF(STAG_END);
5915 param[2] = FW_PARAM_PFVF(PBL_START);
5916 param[3] = FW_PARAM_PFVF(PBL_END);
5917 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5918 if (rc != 0) {
5919 device_printf(sc->dev,
5920 "failed to query NVMe/RDMA parameters: %d.\n", rc);
5921 return (rc);
5922 }
5923 sc->vres.stag.start = val[0];
5924 sc->vres.stag.size = val[1] - val[0] + 1;
5925 sc->vres.pbl.start = val[2];
5926 sc->vres.pbl.size = val[3] - val[2] + 1;
5927 }
5928 if (sc->rdmacaps) {
5929 param[0] = FW_PARAM_PFVF(RQ_START);
5930 param[1] = FW_PARAM_PFVF(RQ_END);
5931 param[2] = FW_PARAM_PFVF(SQRQ_START);
5932 param[3] = FW_PARAM_PFVF(SQRQ_END);
5933 param[4] = FW_PARAM_PFVF(CQ_START);
5934 param[5] = FW_PARAM_PFVF(CQ_END);
5935 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5936 if (rc != 0) {
5937 device_printf(sc->dev,
5938 "failed to query RDMA parameters(1): %d.\n", rc);
5939 return (rc);
5940 }
5941 sc->vres.rq.start = val[0];
5942 sc->vres.rq.size = val[1] - val[0] + 1;
5943 sc->vres.qp.start = val[2];
5944 sc->vres.qp.size = val[3] - val[2] + 1;
5945 sc->vres.cq.start = val[4];
5946 sc->vres.cq.size = val[5] - val[4] + 1;
5947
5948 param[0] = FW_PARAM_PFVF(OCQ_START);
5949 param[1] = FW_PARAM_PFVF(OCQ_END);
5950 param[2] = FW_PARAM_PFVF(SRQ_START);
5951 param[3] = FW_PARAM_PFVF(SRQ_END);
5952 param[4] = FW_PARAM_DEV(MAXORDIRD_QP);
5953 param[5] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5954 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5955 if (rc != 0) {
5956 device_printf(sc->dev,
5957 "failed to query RDMA parameters(2): %d.\n", rc);
5958 return (rc);
5959 }
5960 sc->vres.ocq.start = val[0];
5961 sc->vres.ocq.size = val[1] - val[0] + 1;
5962 sc->vres.srq.start = val[2];
5963 sc->vres.srq.size = val[3] - val[2] + 1;
5964 sc->params.max_ordird_qp = val[4];
5965 sc->params.max_ird_adapter = val[5];
5966 }
5967 if (sc->iscsicaps) {
5968 param[0] = FW_PARAM_PFVF(ISCSI_START);
5969 param[1] = FW_PARAM_PFVF(ISCSI_END);
5970 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5971 if (rc != 0) {
5972 device_printf(sc->dev,
5973 "failed to query iSCSI parameters: %d.\n", rc);
5974 return (rc);
5975 }
5976 sc->vres.iscsi.start = val[0];
5977 sc->vres.iscsi.size = val[1] - val[0] + 1;
5978 }
5979 if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5980 param[0] = FW_PARAM_PFVF(TLS_START);
5981 param[1] = FW_PARAM_PFVF(TLS_END);
5982 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5983 if (rc != 0) {
5984 device_printf(sc->dev,
5985 "failed to query TLS parameters: %d.\n", rc);
5986 return (rc);
5987 }
5988 sc->vres.key.start = val[0];
5989 sc->vres.key.size = val[1] - val[0] + 1;
5990 }
5991
5992 /*
5993 * We've got the params we wanted to query directly from the firmware.
5994 * Grab some others via other means.
5995 */
5996 t4_init_sge_params(sc);
5997 t4_init_tp_params(sc);
5998 t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5999 t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
6000
6001 rc = t4_verify_chip_settings(sc);
6002 if (rc != 0)
6003 return (rc);
6004 t4_init_rx_buf_info(sc);
6005
6006 return (rc);
6007 }
6008
6009 #ifdef KERN_TLS
6010 static void
ktls_tick(void * arg)6011 ktls_tick(void *arg)
6012 {
6013 struct adapter *sc;
6014 uint32_t tstamp;
6015
6016 sc = arg;
6017 tstamp = tcp_ts_getticks();
6018 t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
6019 t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
6020 callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
6021 }
6022
6023 static int
t6_config_kern_tls(struct adapter * sc,bool enable)6024 t6_config_kern_tls(struct adapter *sc, bool enable)
6025 {
6026 int rc;
6027 uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
6028 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
6029 V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
6030 V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
6031
6032 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, ¶m);
6033 if (rc != 0) {
6034 CH_ERR(sc, "failed to %s NIC TLS: %d\n",
6035 enable ? "enable" : "disable", rc);
6036 return (rc);
6037 }
6038
6039 if (enable) {
6040 sc->flags |= KERN_TLS_ON;
6041 callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
6042 C_HARDCLOCK);
6043 } else {
6044 sc->flags &= ~KERN_TLS_ON;
6045 callout_stop(&sc->ktls_tick);
6046 }
6047
6048 return (rc);
6049 }
6050 #endif
6051
6052 static int
set_params__post_init(struct adapter * sc)6053 set_params__post_init(struct adapter *sc)
6054 {
6055 uint32_t mask, param, val;
6056 #ifdef TCP_OFFLOAD
6057 int i, v, shift;
6058 #endif
6059
6060 /* ask for encapsulated CPLs */
6061 param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
6062 val = 1;
6063 (void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
6064
6065 /* Enable 32b port caps if the firmware supports it. */
6066 param = FW_PARAM_PFVF(PORT_CAPS32);
6067 val = 1;
6068 if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val) == 0)
6069 sc->params.port_caps32 = 1;
6070
6071 /* Let filter + maskhash steer to a part of the VI's RSS region. */
6072 val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
6073 t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
6074 V_MASKFILTER(val - 1));
6075
6076 mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
6077 F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
6078 F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
6079 F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
6080 val = 0;
6081 if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
6082 t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
6083 F_ATTACKFILTERENABLE);
6084 val |= F_DROPERRORATTACK;
6085 }
6086 if (t4_drop_ip_fragments != 0) {
6087 t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
6088 F_FRAGMENTDROP);
6089 val |= F_DROPERRORFRAG;
6090 }
6091 if (t4_drop_pkts_with_l2_errors != 0)
6092 val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
6093 if (t4_drop_pkts_with_l3_errors != 0) {
6094 val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
6095 F_DROPERRORCSUMIP;
6096 }
6097 if (t4_drop_pkts_with_l4_errors != 0) {
6098 val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
6099 F_DROPERRORTCPOPT | F_DROPERRORCSUM;
6100 }
6101 t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
6102
6103 #ifdef TCP_OFFLOAD
6104 /*
6105 * Override the TOE timers with user provided tunables. This is not the
6106 * recommended way to change the timers (the firmware config file is) so
6107 * these tunables are not documented.
6108 *
6109 * All the timer tunables are in microseconds.
6110 */
6111 if (t4_toe_keepalive_idle != 0) {
6112 v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
6113 v &= M_KEEPALIVEIDLE;
6114 t4_set_reg_field(sc, A_TP_KEEP_IDLE,
6115 V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
6116 }
6117 if (t4_toe_keepalive_interval != 0) {
6118 v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
6119 v &= M_KEEPALIVEINTVL;
6120 t4_set_reg_field(sc, A_TP_KEEP_INTVL,
6121 V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
6122 }
6123 if (t4_toe_keepalive_count != 0) {
6124 v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
6125 t4_set_reg_field(sc, A_TP_SHIFT_CNT,
6126 V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
6127 V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
6128 V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
6129 }
6130 if (t4_toe_rexmt_min != 0) {
6131 v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
6132 v &= M_RXTMIN;
6133 t4_set_reg_field(sc, A_TP_RXT_MIN,
6134 V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
6135 }
6136 if (t4_toe_rexmt_max != 0) {
6137 v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
6138 v &= M_RXTMAX;
6139 t4_set_reg_field(sc, A_TP_RXT_MAX,
6140 V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
6141 }
6142 if (t4_toe_rexmt_count != 0) {
6143 v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
6144 t4_set_reg_field(sc, A_TP_SHIFT_CNT,
6145 V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
6146 V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
6147 V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
6148 }
6149 for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
6150 if (t4_toe_rexmt_backoff[i] != -1) {
6151 v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
6152 shift = (i & 3) << 3;
6153 t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
6154 M_TIMERBACKOFFINDEX0 << shift, v << shift);
6155 }
6156 }
6157 #endif
6158
6159 /*
6160 * Limit TOE connections to 2 reassembly "islands". This is
6161 * required to permit migrating TOE connections to either
6162 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
6163 */
6164 t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
6165 V_PASSMODE(2));
6166
6167 #ifdef KERN_TLS
6168 if (is_ktls(sc)) {
6169 sc->tlst.inline_keys = t4_tls_inline_keys;
6170 if (t4_kern_tls != 0 && is_t6(sc)) {
6171 sc->tlst.combo_wrs = t4_tls_combo_wrs;
6172 t6_config_kern_tls(sc, true);
6173 } else {
6174 sc->tlst.short_records = t4_tls_short_records;
6175 sc->tlst.partial_ghash = t4_tls_partial_ghash;
6176 }
6177 }
6178 #endif
6179 return (0);
6180 }
6181
6182 #undef FW_PARAM_PFVF
6183 #undef FW_PARAM_DEV
6184
6185 static void
t4_set_desc(struct adapter * sc)6186 t4_set_desc(struct adapter *sc)
6187 {
6188 struct adapter_params *p = &sc->params;
6189
6190 device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
6191 }
6192
6193 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)6194 ifmedia_add4(struct ifmedia *ifm, int m)
6195 {
6196
6197 ifmedia_add(ifm, m, 0, NULL);
6198 ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
6199 ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
6200 ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
6201 }
6202
6203 /*
6204 * This is the selected media, which is not quite the same as the active media.
6205 * The media line in ifconfig is "media: Ethernet selected (active)" if selected
6206 * and active are not the same, and "media: Ethernet selected" otherwise.
6207 */
6208 static void
set_current_media(struct port_info * pi)6209 set_current_media(struct port_info *pi)
6210 {
6211 struct link_config *lc;
6212 struct ifmedia *ifm;
6213 int mword;
6214 u_int speed;
6215
6216 PORT_LOCK_ASSERT_OWNED(pi);
6217
6218 /* Leave current media alone if it's already set to IFM_NONE. */
6219 ifm = &pi->media;
6220 if (ifm->ifm_cur != NULL &&
6221 IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
6222 return;
6223
6224 lc = &pi->link_cfg;
6225 if (lc->requested_aneg != AUTONEG_DISABLE &&
6226 lc->pcaps & FW_PORT_CAP32_ANEG) {
6227 ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
6228 return;
6229 }
6230 mword = IFM_ETHER | IFM_FDX;
6231 if (lc->requested_fc & PAUSE_TX)
6232 mword |= IFM_ETH_TXPAUSE;
6233 if (lc->requested_fc & PAUSE_RX)
6234 mword |= IFM_ETH_RXPAUSE;
6235 if (lc->requested_speed == 0)
6236 speed = port_top_speed(pi) * 1000; /* Gbps -> Mbps */
6237 else
6238 speed = lc->requested_speed;
6239 mword |= port_mword(pi, speed_to_fwcap(speed));
6240 ifmedia_set(ifm, mword);
6241 }
6242
6243 /*
6244 * Returns true if the ifmedia list for the port cannot change.
6245 */
6246 static bool
fixed_ifmedia(struct port_info * pi)6247 fixed_ifmedia(struct port_info *pi)
6248 {
6249
6250 return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
6251 pi->port_type == FW_PORT_TYPE_BT_XFI ||
6252 pi->port_type == FW_PORT_TYPE_BT_XAUI ||
6253 pi->port_type == FW_PORT_TYPE_KX4 ||
6254 pi->port_type == FW_PORT_TYPE_KX ||
6255 pi->port_type == FW_PORT_TYPE_KR ||
6256 pi->port_type == FW_PORT_TYPE_BP_AP ||
6257 pi->port_type == FW_PORT_TYPE_BP4_AP ||
6258 pi->port_type == FW_PORT_TYPE_BP40_BA ||
6259 pi->port_type == FW_PORT_TYPE_KR4_100G ||
6260 pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
6261 pi->port_type == FW_PORT_TYPE_KR_XLAUI);
6262 }
6263
6264 static void
build_medialist(struct port_info * pi)6265 build_medialist(struct port_info *pi)
6266 {
6267 uint32_t ss, speed;
6268 int unknown, mword, bit;
6269 struct link_config *lc;
6270 struct ifmedia *ifm;
6271
6272 PORT_LOCK_ASSERT_OWNED(pi);
6273
6274 if (pi->flags & FIXED_IFMEDIA)
6275 return;
6276
6277 /*
6278 * Rebuild the ifmedia list.
6279 */
6280 ifm = &pi->media;
6281 ifmedia_removeall(ifm);
6282 lc = &pi->link_cfg;
6283 ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
6284 if (__predict_false(ss == 0)) { /* not supposed to happen. */
6285 MPASS(ss != 0);
6286 no_media:
6287 MPASS(LIST_EMPTY(&ifm->ifm_list));
6288 ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
6289 ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
6290 return;
6291 }
6292
6293 unknown = 0;
6294 for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
6295 speed = 1 << bit;
6296 MPASS(speed & M_FW_PORT_CAP32_SPEED);
6297 if (ss & speed) {
6298 mword = port_mword(pi, speed);
6299 if (mword == IFM_NONE) {
6300 goto no_media;
6301 } else if (mword == IFM_UNKNOWN)
6302 unknown++;
6303 else
6304 ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
6305 }
6306 }
6307 if (unknown > 0) /* Add one unknown for all unknown media types. */
6308 ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
6309 if (lc->pcaps & FW_PORT_CAP32_ANEG)
6310 ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
6311
6312 set_current_media(pi);
6313 }
6314
6315 /*
6316 * Initialize the requested fields in the link config based on driver tunables.
6317 */
6318 static void
init_link_config(struct port_info * pi)6319 init_link_config(struct port_info *pi)
6320 {
6321 struct link_config *lc = &pi->link_cfg;
6322
6323 PORT_LOCK_ASSERT_OWNED(pi);
6324
6325 lc->requested_caps = 0;
6326 lc->requested_speed = 0;
6327
6328 if (t4_autoneg == 0)
6329 lc->requested_aneg = AUTONEG_DISABLE;
6330 else if (t4_autoneg == 1)
6331 lc->requested_aneg = AUTONEG_ENABLE;
6332 else
6333 lc->requested_aneg = AUTONEG_AUTO;
6334
6335 lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
6336 PAUSE_AUTONEG);
6337
6338 if (t4_fec & FEC_AUTO)
6339 lc->requested_fec = FEC_AUTO;
6340 else if (t4_fec == 0)
6341 lc->requested_fec = FEC_NONE;
6342 else {
6343 /* -1 is handled by the FEC_AUTO block above and not here. */
6344 lc->requested_fec = t4_fec &
6345 (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6346 if (lc->requested_fec == 0)
6347 lc->requested_fec = FEC_AUTO;
6348 }
6349 if (t4_force_fec < 0)
6350 lc->force_fec = -1;
6351 else if (t4_force_fec > 0)
6352 lc->force_fec = 1;
6353 else
6354 lc->force_fec = 0;
6355 }
6356
6357 /*
6358 * Makes sure that all requested settings comply with what's supported by the
6359 * port. Returns the number of settings that were invalid and had to be fixed.
6360 */
6361 static int
fixup_link_config(struct port_info * pi)6362 fixup_link_config(struct port_info *pi)
6363 {
6364 int n = 0;
6365 struct link_config *lc = &pi->link_cfg;
6366 uint32_t fwspeed;
6367
6368 PORT_LOCK_ASSERT_OWNED(pi);
6369
6370 /* Speed (when not autonegotiating) */
6371 if (lc->requested_speed != 0) {
6372 fwspeed = speed_to_fwcap(lc->requested_speed);
6373 if ((fwspeed & lc->pcaps) == 0) {
6374 n++;
6375 lc->requested_speed = 0;
6376 }
6377 }
6378
6379 /* Link autonegotiation */
6380 MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6381 lc->requested_aneg == AUTONEG_DISABLE ||
6382 lc->requested_aneg == AUTONEG_AUTO);
6383 if (lc->requested_aneg == AUTONEG_ENABLE &&
6384 !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6385 n++;
6386 lc->requested_aneg = AUTONEG_AUTO;
6387 }
6388
6389 /* Flow control */
6390 MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6391 if (lc->requested_fc & PAUSE_TX &&
6392 !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6393 n++;
6394 lc->requested_fc &= ~PAUSE_TX;
6395 }
6396 if (lc->requested_fc & PAUSE_RX &&
6397 !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6398 n++;
6399 lc->requested_fc &= ~PAUSE_RX;
6400 }
6401 if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6402 !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6403 n++;
6404 lc->requested_fc |= PAUSE_AUTONEG;
6405 }
6406
6407 /* FEC */
6408 if ((lc->requested_fec & FEC_RS &&
6409 !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6410 (lc->requested_fec & FEC_BASER_RS &&
6411 !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6412 n++;
6413 lc->requested_fec = FEC_AUTO;
6414 }
6415
6416 return (n);
6417 }
6418
6419 /*
6420 * Apply the requested L1 settings, which are expected to be valid, to the
6421 * hardware.
6422 */
6423 static int
apply_link_config(struct port_info * pi)6424 apply_link_config(struct port_info *pi)
6425 {
6426 struct adapter *sc = pi->adapter;
6427 struct link_config *lc = &pi->link_cfg;
6428 int rc;
6429
6430 #ifdef INVARIANTS
6431 ASSERT_SYNCHRONIZED_OP(sc);
6432 PORT_LOCK_ASSERT_OWNED(pi);
6433
6434 if (lc->requested_aneg == AUTONEG_ENABLE)
6435 MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6436 if (!(lc->requested_fc & PAUSE_AUTONEG))
6437 MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6438 if (lc->requested_fc & PAUSE_TX)
6439 MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6440 if (lc->requested_fc & PAUSE_RX)
6441 MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6442 if (lc->requested_fec & FEC_RS)
6443 MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6444 if (lc->requested_fec & FEC_BASER_RS)
6445 MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6446 #endif
6447 if (!(sc->flags & IS_VF)) {
6448 rc = -t4_link_l1cfg(sc, sc->mbox, pi->hw_port, lc);
6449 if (rc != 0) {
6450 device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6451 return (rc);
6452 }
6453 }
6454
6455 /*
6456 * An L1_CFG will almost always result in a link-change event if the
6457 * link is up, and the driver will refresh the actual fec/fc/etc. when
6458 * the notification is processed. If the link is down then the actual
6459 * settings are meaningless.
6460 *
6461 * This takes care of the case where a change in the L1 settings may not
6462 * result in a notification.
6463 */
6464 if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6465 lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6466
6467 return (0);
6468 }
6469
6470 #define FW_MAC_EXACT_CHUNK 7
6471 struct mcaddr_ctx {
6472 if_t ifp;
6473 const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6474 uint64_t hash;
6475 int i;
6476 int del;
6477 int rc;
6478 };
6479
6480 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6481 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6482 {
6483 struct mcaddr_ctx *ctx = arg;
6484 struct vi_info *vi = if_getsoftc(ctx->ifp);
6485 struct port_info *pi = vi->pi;
6486 struct adapter *sc = pi->adapter;
6487
6488 if (ctx->rc < 0)
6489 return (0);
6490
6491 ctx->mcaddr[ctx->i] = LLADDR(sdl);
6492 MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6493 ctx->i++;
6494
6495 if (ctx->i == FW_MAC_EXACT_CHUNK) {
6496 ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6497 ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6498 if (ctx->rc < 0) {
6499 int j;
6500
6501 for (j = 0; j < ctx->i; j++) {
6502 if_printf(ctx->ifp,
6503 "failed to add mc address"
6504 " %02x:%02x:%02x:"
6505 "%02x:%02x:%02x rc=%d\n",
6506 ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6507 ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6508 ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6509 -ctx->rc);
6510 }
6511 return (0);
6512 }
6513 ctx->del = 0;
6514 ctx->i = 0;
6515 }
6516
6517 return (1);
6518 }
6519
6520 /*
6521 * Program the port's XGMAC based on parameters in ifnet. The caller also
6522 * indicates which parameters should be programmed (the rest are left alone).
6523 */
6524 int
update_mac_settings(if_t ifp,int flags)6525 update_mac_settings(if_t ifp, int flags)
6526 {
6527 int rc = 0;
6528 struct vi_info *vi = if_getsoftc(ifp);
6529 struct port_info *pi = vi->pi;
6530 struct adapter *sc = pi->adapter;
6531 int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6532 uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6533
6534 ASSERT_SYNCHRONIZED_OP(sc);
6535 KASSERT(flags, ("%s: not told what to update.", __func__));
6536
6537 if (flags & XGMAC_MTU)
6538 mtu = if_getmtu(ifp);
6539
6540 if (flags & XGMAC_PROMISC)
6541 promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6542
6543 if (flags & XGMAC_ALLMULTI)
6544 allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6545
6546 if (flags & XGMAC_VLANEX)
6547 vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6548
6549 if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6550 rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6551 allmulti, 1, vlanex, false);
6552 if (rc) {
6553 if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6554 rc);
6555 return (rc);
6556 }
6557 }
6558
6559 if (flags & XGMAC_UCADDR) {
6560 uint8_t ucaddr[ETHER_ADDR_LEN];
6561
6562 bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6563 rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6564 ucaddr, true, &vi->smt_idx);
6565 if (rc < 0) {
6566 rc = -rc;
6567 if_printf(ifp, "change_mac failed: %d\n", rc);
6568 return (rc);
6569 } else {
6570 vi->xact_addr_filt = rc;
6571 rc = 0;
6572 }
6573 }
6574
6575 if (flags & XGMAC_MCADDRS) {
6576 struct epoch_tracker et;
6577 struct mcaddr_ctx ctx;
6578 int j;
6579
6580 ctx.ifp = ifp;
6581 ctx.hash = 0;
6582 ctx.i = 0;
6583 ctx.del = 1;
6584 ctx.rc = 0;
6585 /*
6586 * Unlike other drivers, we accumulate list of pointers into
6587 * interface address lists and we need to keep it safe even
6588 * after if_foreach_llmaddr() returns, thus we must enter the
6589 * network epoch.
6590 */
6591 NET_EPOCH_ENTER(et);
6592 if_foreach_llmaddr(ifp, add_maddr, &ctx);
6593 if (ctx.rc < 0) {
6594 NET_EPOCH_EXIT(et);
6595 rc = -ctx.rc;
6596 return (rc);
6597 }
6598 if (ctx.i > 0) {
6599 rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6600 ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6601 NET_EPOCH_EXIT(et);
6602 if (rc < 0) {
6603 rc = -rc;
6604 for (j = 0; j < ctx.i; j++) {
6605 if_printf(ifp,
6606 "failed to add mcast address"
6607 " %02x:%02x:%02x:"
6608 "%02x:%02x:%02x rc=%d\n",
6609 ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6610 ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6611 ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6612 rc);
6613 }
6614 return (rc);
6615 }
6616 ctx.del = 0;
6617 } else
6618 NET_EPOCH_EXIT(et);
6619
6620 rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6621 if (rc != 0)
6622 if_printf(ifp, "failed to set mcast address hash: %d\n",
6623 rc);
6624 if (ctx.del == 0) {
6625 /* We clobbered the VXLAN entry if there was one. */
6626 pi->vxlan_tcam_entry = false;
6627 }
6628 }
6629
6630 if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6631 pi->vxlan_tcam_entry == false) {
6632 rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6633 match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6634 true);
6635 if (rc < 0) {
6636 rc = -rc;
6637 if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6638 rc);
6639 } else {
6640 MPASS(rc == sc->rawf_base + pi->port_id);
6641 rc = 0;
6642 pi->vxlan_tcam_entry = true;
6643 }
6644 }
6645
6646 return (rc);
6647 }
6648
6649 /*
6650 * {begin|end}_synchronized_op must be called from the same thread.
6651 */
6652 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6653 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6654 char *wmesg)
6655 {
6656 int rc;
6657
6658 #ifdef WITNESS
6659 /* the caller thinks it's ok to sleep, but is it really? */
6660 if (flags & SLEEP_OK)
6661 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
6662 #endif
6663 ADAPTER_LOCK(sc);
6664 for (;;) {
6665
6666 if (vi && IS_DETACHING(vi)) {
6667 rc = ENXIO;
6668 goto done;
6669 }
6670
6671 if (!IS_BUSY(sc)) {
6672 rc = 0;
6673 break;
6674 }
6675
6676 if (!(flags & SLEEP_OK)) {
6677 rc = EBUSY;
6678 goto done;
6679 }
6680
6681 if (mtx_sleep(&sc->flags, &sc->sc_lock,
6682 flags & INTR_OK ? PCATCH : 0, wmesg, 0)) {
6683 rc = EINTR;
6684 goto done;
6685 }
6686 }
6687
6688 KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6689 SET_BUSY(sc);
6690 #ifdef INVARIANTS
6691 sc->last_op = wmesg;
6692 sc->last_op_thr = curthread;
6693 sc->last_op_flags = flags;
6694 #endif
6695
6696 done:
6697 if (!(flags & HOLD_LOCK) || rc)
6698 ADAPTER_UNLOCK(sc);
6699
6700 return (rc);
6701 }
6702
6703 /*
6704 * Tell if_ioctl and if_init that the VI is going away. This is
6705 * special variant of begin_synchronized_op and must be paired with a
6706 * call to end_vi_detach.
6707 */
6708 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6709 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6710 {
6711 ADAPTER_LOCK(sc);
6712 SET_DETACHING(vi);
6713 wakeup(&sc->flags);
6714 while (IS_BUSY(sc))
6715 mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6716 SET_BUSY(sc);
6717 #ifdef INVARIANTS
6718 sc->last_op = "t4detach";
6719 sc->last_op_thr = curthread;
6720 sc->last_op_flags = 0;
6721 #endif
6722 ADAPTER_UNLOCK(sc);
6723 }
6724
6725 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6726 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6727 {
6728 ADAPTER_LOCK(sc);
6729 KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6730 CLR_BUSY(sc);
6731 CLR_DETACHING(vi);
6732 wakeup(&sc->flags);
6733 ADAPTER_UNLOCK(sc);
6734 }
6735
6736 /*
6737 * {begin|end}_synchronized_op must be called from the same thread.
6738 */
6739 void
end_synchronized_op(struct adapter * sc,int flags)6740 end_synchronized_op(struct adapter *sc, int flags)
6741 {
6742
6743 if (flags & LOCK_HELD)
6744 ADAPTER_LOCK_ASSERT_OWNED(sc);
6745 else
6746 ADAPTER_LOCK(sc);
6747
6748 KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6749 CLR_BUSY(sc);
6750 wakeup(&sc->flags);
6751 ADAPTER_UNLOCK(sc);
6752 }
6753
6754 static int
cxgbe_init_synchronized(struct vi_info * vi)6755 cxgbe_init_synchronized(struct vi_info *vi)
6756 {
6757 struct port_info *pi = vi->pi;
6758 struct adapter *sc = pi->adapter;
6759 if_t ifp = vi->ifp;
6760 int rc = 0, i;
6761 struct sge_txq *txq;
6762
6763 ASSERT_SYNCHRONIZED_OP(sc);
6764
6765 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6766 return (0); /* already running */
6767
6768 if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6769 return (rc); /* error message displayed already */
6770
6771 if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6772 return (rc); /* error message displayed already */
6773
6774 rc = update_mac_settings(ifp, XGMAC_ALL);
6775 if (rc)
6776 goto done; /* error message displayed already */
6777
6778 PORT_LOCK(pi);
6779 if (pi->up_vis == 0) {
6780 t4_update_port_info(pi);
6781 fixup_link_config(pi);
6782 build_medialist(pi);
6783 apply_link_config(pi);
6784 }
6785
6786 rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6787 if (rc != 0) {
6788 if_printf(ifp, "enable_vi failed: %d\n", rc);
6789 PORT_UNLOCK(pi);
6790 goto done;
6791 }
6792
6793 /*
6794 * Can't fail from this point onwards. Review cxgbe_uninit_synchronized
6795 * if this changes.
6796 */
6797
6798 for_each_txq(vi, i, txq) {
6799 TXQ_LOCK(txq);
6800 txq->eq.flags |= EQ_ENABLED;
6801 TXQ_UNLOCK(txq);
6802 }
6803
6804 /*
6805 * The first iq of the first port to come up is used for tracing.
6806 */
6807 if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6808 sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6809 t4_set_trace_rss_control(sc, pi->tx_chan, sc->traceq);
6810 pi->flags |= HAS_TRACEQ;
6811 }
6812
6813 /* all ok */
6814 pi->up_vis++;
6815 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6816 if (pi->link_cfg.link_ok)
6817 t4_os_link_changed(pi);
6818 PORT_UNLOCK(pi);
6819
6820 mtx_lock(&vi->tick_mtx);
6821 if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6822 callout_reset(&vi->tick, hz, vi_tick, vi);
6823 else
6824 callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6825 mtx_unlock(&vi->tick_mtx);
6826 done:
6827 if (rc != 0)
6828 cxgbe_uninit_synchronized(vi);
6829
6830 return (rc);
6831 }
6832
6833 /*
6834 * Idempotent.
6835 */
6836 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6837 cxgbe_uninit_synchronized(struct vi_info *vi)
6838 {
6839 struct port_info *pi = vi->pi;
6840 struct adapter *sc = pi->adapter;
6841 if_t ifp = vi->ifp;
6842 int rc, i;
6843 struct sge_txq *txq;
6844
6845 ASSERT_SYNCHRONIZED_OP(sc);
6846
6847 if (!(vi->flags & VI_INIT_DONE)) {
6848 if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6849 KASSERT(0, ("uninited VI is running"));
6850 if_printf(ifp, "uninited VI with running ifnet. "
6851 "vi->flags 0x%016lx, if_flags 0x%08x, "
6852 "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6853 if_getdrvflags(ifp));
6854 }
6855 return (0);
6856 }
6857
6858 /*
6859 * Disable the VI so that all its data in either direction is discarded
6860 * by the MPS. Leave everything else (the queues, interrupts, and 1Hz
6861 * tick) intact as the TP can deliver negative advice or data that it's
6862 * holding in its RAM (for an offloaded connection) even after the VI is
6863 * disabled.
6864 */
6865 rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6866 if (rc) {
6867 if_printf(ifp, "disable_vi failed: %d\n", rc);
6868 return (rc);
6869 }
6870
6871 for_each_txq(vi, i, txq) {
6872 TXQ_LOCK(txq);
6873 txq->eq.flags &= ~EQ_ENABLED;
6874 TXQ_UNLOCK(txq);
6875 }
6876
6877 mtx_lock(&vi->tick_mtx);
6878 callout_stop(&vi->tick);
6879 mtx_unlock(&vi->tick_mtx);
6880
6881 PORT_LOCK(pi);
6882 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6883 PORT_UNLOCK(pi);
6884 return (0);
6885 }
6886 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6887 pi->up_vis--;
6888 if (pi->up_vis > 0) {
6889 PORT_UNLOCK(pi);
6890 return (0);
6891 }
6892
6893 pi->link_cfg.link_ok = false;
6894 pi->link_cfg.speed = 0;
6895 pi->link_cfg.link_down_rc = 255;
6896 t4_os_link_changed(pi);
6897 PORT_UNLOCK(pi);
6898
6899 return (0);
6900 }
6901
6902 /*
6903 * It is ok for this function to fail midway and return right away. t4_detach
6904 * will walk the entire sc->irq list and clean up whatever is valid.
6905 */
6906 int
t4_setup_intr_handlers(struct adapter * sc)6907 t4_setup_intr_handlers(struct adapter *sc)
6908 {
6909 int rc, rid, p, q, v;
6910 char s[8];
6911 struct irq *irq;
6912 struct port_info *pi;
6913 struct vi_info *vi;
6914 struct sge *sge = &sc->sge;
6915 struct sge_rxq *rxq;
6916 #ifdef TCP_OFFLOAD
6917 struct sge_ofld_rxq *ofld_rxq;
6918 #endif
6919 #ifdef DEV_NETMAP
6920 struct sge_nm_rxq *nm_rxq;
6921 #endif
6922 #ifdef RSS
6923 int nbuckets = rss_getnumbuckets();
6924 #endif
6925
6926 /*
6927 * Setup interrupts.
6928 */
6929 irq = &sc->irq[0];
6930 rid = sc->intr_type == INTR_INTX ? 0 : 1;
6931 if (forwarding_intr_to_fwq(sc))
6932 return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6933
6934 /* Multiple interrupts. */
6935 if (sc->flags & IS_VF)
6936 KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6937 ("%s: too few intr.", __func__));
6938 else
6939 KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6940 ("%s: too few intr.", __func__));
6941
6942 /* The first one is always error intr on PFs */
6943 if (!(sc->flags & IS_VF)) {
6944 rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6945 if (rc != 0)
6946 return (rc);
6947 irq++;
6948 rid++;
6949 }
6950
6951 /* The second one is always the firmware event queue (first on VFs) */
6952 rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6953 if (rc != 0)
6954 return (rc);
6955 irq++;
6956 rid++;
6957
6958 for_each_port(sc, p) {
6959 pi = sc->port[p];
6960 for_each_vi(pi, v, vi) {
6961 vi->first_intr = rid - 1;
6962
6963 if (vi->nnmrxq > 0) {
6964 int n = max(vi->nrxq, vi->nnmrxq);
6965
6966 rxq = &sge->rxq[vi->first_rxq];
6967 #ifdef DEV_NETMAP
6968 nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6969 #endif
6970 for (q = 0; q < n; q++) {
6971 snprintf(s, sizeof(s), "%x%c%x", p,
6972 'a' + v, q);
6973 if (q < vi->nrxq)
6974 irq->rxq = rxq++;
6975 #ifdef DEV_NETMAP
6976 if (q < vi->nnmrxq)
6977 irq->nm_rxq = nm_rxq++;
6978
6979 if (irq->nm_rxq != NULL &&
6980 irq->rxq == NULL) {
6981 /* Netmap rx only */
6982 rc = t4_alloc_irq(sc, irq, rid,
6983 t4_nm_intr, irq->nm_rxq, s);
6984 }
6985 if (irq->nm_rxq != NULL &&
6986 irq->rxq != NULL) {
6987 /* NIC and Netmap rx */
6988 rc = t4_alloc_irq(sc, irq, rid,
6989 t4_vi_intr, irq, s);
6990 }
6991 #endif
6992 if (irq->rxq != NULL &&
6993 irq->nm_rxq == NULL) {
6994 /* NIC rx only */
6995 rc = t4_alloc_irq(sc, irq, rid,
6996 t4_intr, irq->rxq, s);
6997 }
6998 if (rc != 0)
6999 return (rc);
7000 #ifdef RSS
7001 if (q < vi->nrxq) {
7002 bus_bind_intr(sc->dev, irq->res,
7003 rss_getcpu(q % nbuckets));
7004 }
7005 #endif
7006 irq++;
7007 rid++;
7008 vi->nintr++;
7009 }
7010 } else {
7011 for_each_rxq(vi, q, rxq) {
7012 snprintf(s, sizeof(s), "%x%c%x", p,
7013 'a' + v, q);
7014 rc = t4_alloc_irq(sc, irq, rid,
7015 t4_intr, rxq, s);
7016 if (rc != 0)
7017 return (rc);
7018 #ifdef RSS
7019 bus_bind_intr(sc->dev, irq->res,
7020 rss_getcpu(q % nbuckets));
7021 #endif
7022 irq++;
7023 rid++;
7024 vi->nintr++;
7025 }
7026 }
7027 #ifdef TCP_OFFLOAD
7028 for_each_ofld_rxq(vi, q, ofld_rxq) {
7029 snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
7030 rc = t4_alloc_irq(sc, irq, rid, t4_intr,
7031 ofld_rxq, s);
7032 if (rc != 0)
7033 return (rc);
7034 irq++;
7035 rid++;
7036 vi->nintr++;
7037 }
7038 #endif
7039 }
7040 }
7041 MPASS(irq == &sc->irq[sc->intr_count]);
7042
7043 return (0);
7044 }
7045
7046 static void
write_global_rss_key(struct adapter * sc)7047 write_global_rss_key(struct adapter *sc)
7048 {
7049 int i;
7050 uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
7051 uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
7052
7053 CTASSERT(RSS_KEYSIZE == 40);
7054
7055 rss_getkey((void *)&raw_rss_key[0]);
7056 for (i = 0; i < nitems(rss_key); i++) {
7057 rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
7058 }
7059 t4_write_rss_key(sc, &rss_key[0], -1, 1);
7060 }
7061
7062 /*
7063 * Idempotent.
7064 */
7065 static int
adapter_full_init(struct adapter * sc)7066 adapter_full_init(struct adapter *sc)
7067 {
7068 int rc, i;
7069
7070 ASSERT_SYNCHRONIZED_OP(sc);
7071
7072 /*
7073 * queues that belong to the adapter (not any particular port).
7074 */
7075 rc = t4_setup_adapter_queues(sc);
7076 if (rc != 0)
7077 return (rc);
7078
7079 MPASS(sc->params.nports <= nitems(sc->tq));
7080 for (i = 0; i < sc->params.nports; i++) {
7081 if (sc->tq[i] != NULL)
7082 continue;
7083 sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
7084 taskqueue_thread_enqueue, &sc->tq[i]);
7085 if (sc->tq[i] == NULL) {
7086 CH_ERR(sc, "failed to allocate task queue %d\n", i);
7087 return (ENOMEM);
7088 }
7089 taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
7090 device_get_nameunit(sc->dev), i);
7091 }
7092
7093 if (!(sc->flags & IS_VF)) {
7094 write_global_rss_key(sc);
7095 t4_intr_enable(sc);
7096 }
7097 return (0);
7098 }
7099
7100 int
adapter_init(struct adapter * sc)7101 adapter_init(struct adapter *sc)
7102 {
7103 int rc;
7104
7105 ASSERT_SYNCHRONIZED_OP(sc);
7106 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
7107 KASSERT((sc->flags & FULL_INIT_DONE) == 0,
7108 ("%s: FULL_INIT_DONE already", __func__));
7109
7110 rc = adapter_full_init(sc);
7111 if (rc != 0)
7112 adapter_full_uninit(sc);
7113 else
7114 sc->flags |= FULL_INIT_DONE;
7115
7116 return (rc);
7117 }
7118
7119 /*
7120 * Idempotent.
7121 */
7122 static void
adapter_full_uninit(struct adapter * sc)7123 adapter_full_uninit(struct adapter *sc)
7124 {
7125 int i;
7126
7127 t4_teardown_adapter_queues(sc);
7128
7129 for (i = 0; i < nitems(sc->tq); i++) {
7130 if (sc->tq[i] == NULL)
7131 continue;
7132 taskqueue_free(sc->tq[i]);
7133 sc->tq[i] = NULL;
7134 }
7135
7136 sc->flags &= ~FULL_INIT_DONE;
7137 }
7138
7139 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
7140 RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
7141 RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
7142 RSS_HASHTYPE_RSS_UDP_IPV6)
7143
7144 /* Translates kernel hash types to hardware. */
7145 static int
hashconfig_to_hashen(int hashconfig)7146 hashconfig_to_hashen(int hashconfig)
7147 {
7148 int hashen = 0;
7149
7150 if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
7151 hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
7152 if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
7153 hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
7154 if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
7155 hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
7156 F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
7157 }
7158 if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
7159 hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
7160 F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
7161 }
7162 if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
7163 hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
7164 if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
7165 hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
7166
7167 return (hashen);
7168 }
7169
7170 /* Translates hardware hash types to kernel. */
7171 static int
hashen_to_hashconfig(int hashen)7172 hashen_to_hashconfig(int hashen)
7173 {
7174 int hashconfig = 0;
7175
7176 if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
7177 /*
7178 * If UDP hashing was enabled it must have been enabled for
7179 * either IPv4 or IPv6 (inclusive or). Enabling UDP without
7180 * enabling any 4-tuple hash is nonsense configuration.
7181 */
7182 MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
7183 F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
7184
7185 if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
7186 hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
7187 if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
7188 hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
7189 }
7190 if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
7191 hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
7192 if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
7193 hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
7194 if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
7195 hashconfig |= RSS_HASHTYPE_RSS_IPV4;
7196 if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
7197 hashconfig |= RSS_HASHTYPE_RSS_IPV6;
7198
7199 return (hashconfig);
7200 }
7201
7202 /*
7203 * Idempotent.
7204 */
7205 static int
vi_full_init(struct vi_info * vi)7206 vi_full_init(struct vi_info *vi)
7207 {
7208 struct adapter *sc = vi->adapter;
7209 struct sge_rxq *rxq;
7210 int rc, i, j, extra;
7211 int hashconfig = rss_gethashconfig();
7212 #ifdef RSS
7213 int nbuckets = rss_getnumbuckets();
7214 #endif
7215
7216 ASSERT_SYNCHRONIZED_OP(sc);
7217
7218 /*
7219 * Allocate tx/rx/fl queues for this VI.
7220 */
7221 rc = t4_setup_vi_queues(vi);
7222 if (rc != 0)
7223 return (rc);
7224
7225 /*
7226 * Setup RSS for this VI. Save a copy of the RSS table for later use.
7227 */
7228 if (vi->nrxq > vi->rss_size) {
7229 CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
7230 "some queues will never receive traffic.\n", vi->nrxq,
7231 vi->rss_size);
7232 } else if (vi->rss_size % vi->nrxq) {
7233 CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
7234 "expect uneven traffic distribution.\n", vi->nrxq,
7235 vi->rss_size);
7236 }
7237 #ifdef RSS
7238 if (vi->nrxq != nbuckets) {
7239 CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
7240 "performance will be impacted.\n", vi->nrxq, nbuckets);
7241 }
7242 #endif
7243 if (vi->rss == NULL)
7244 vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
7245 M_ZERO | M_WAITOK);
7246 for (i = 0; i < vi->rss_size;) {
7247 #ifdef RSS
7248 j = rss_get_indirection_to_bucket(i);
7249 j %= vi->nrxq;
7250 rxq = &sc->sge.rxq[vi->first_rxq + j];
7251 vi->rss[i++] = rxq->iq.abs_id;
7252 #else
7253 for_each_rxq(vi, j, rxq) {
7254 vi->rss[i++] = rxq->iq.abs_id;
7255 if (i == vi->rss_size)
7256 break;
7257 }
7258 #endif
7259 }
7260
7261 rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
7262 vi->rss, vi->rss_size);
7263 if (rc != 0) {
7264 CH_ERR(vi, "rss_config failed: %d\n", rc);
7265 return (rc);
7266 }
7267
7268 vi->hashen = hashconfig_to_hashen(hashconfig);
7269
7270 /*
7271 * We may have had to enable some hashes even though the global config
7272 * wants them disabled. This is a potential problem that must be
7273 * reported to the user.
7274 */
7275 extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
7276
7277 /*
7278 * If we consider only the supported hash types, then the enabled hashes
7279 * are a superset of the requested hashes. In other words, there cannot
7280 * be any supported hash that was requested but not enabled, but there
7281 * can be hashes that were not requested but had to be enabled.
7282 */
7283 extra &= SUPPORTED_RSS_HASHTYPES;
7284 MPASS((extra & hashconfig) == 0);
7285
7286 if (extra) {
7287 CH_ALERT(vi,
7288 "global RSS config (0x%x) cannot be accommodated.\n",
7289 hashconfig);
7290 }
7291 if (extra & RSS_HASHTYPE_RSS_IPV4)
7292 CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
7293 if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
7294 CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
7295 if (extra & RSS_HASHTYPE_RSS_IPV6)
7296 CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
7297 if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
7298 CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
7299 if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
7300 CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
7301 if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
7302 CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
7303
7304 rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
7305 0, 0);
7306 if (rc != 0) {
7307 CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
7308 return (rc);
7309 }
7310
7311 return (0);
7312 }
7313
7314 int
vi_init(struct vi_info * vi)7315 vi_init(struct vi_info *vi)
7316 {
7317 int rc;
7318
7319 ASSERT_SYNCHRONIZED_OP(vi->adapter);
7320 KASSERT((vi->flags & VI_INIT_DONE) == 0,
7321 ("%s: VI_INIT_DONE already", __func__));
7322
7323 rc = vi_full_init(vi);
7324 if (rc != 0)
7325 vi_full_uninit(vi);
7326 else
7327 vi->flags |= VI_INIT_DONE;
7328
7329 return (rc);
7330 }
7331
7332 /*
7333 * Idempotent.
7334 */
7335 static void
vi_full_uninit(struct vi_info * vi)7336 vi_full_uninit(struct vi_info *vi)
7337 {
7338
7339 if (vi->flags & VI_INIT_DONE) {
7340 quiesce_vi(vi);
7341 free(vi->rss, M_CXGBE);
7342 free(vi->nm_rss, M_CXGBE);
7343 }
7344
7345 t4_teardown_vi_queues(vi);
7346 vi->flags &= ~VI_INIT_DONE;
7347 }
7348
7349 static void
quiesce_txq(struct sge_txq * txq)7350 quiesce_txq(struct sge_txq *txq)
7351 {
7352 struct sge_eq *eq = &txq->eq;
7353 struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7354
7355 MPASS(eq->flags & EQ_SW_ALLOCATED);
7356 MPASS(!(eq->flags & EQ_ENABLED));
7357
7358 /* Wait for the mp_ring to empty. */
7359 while (!mp_ring_is_idle(txq->r)) {
7360 mp_ring_check_drainage(txq->r, 4096);
7361 pause("rquiesce", 1);
7362 }
7363 MPASS(txq->txp.npkt == 0);
7364
7365 if (eq->flags & EQ_HW_ALLOCATED) {
7366 /*
7367 * Hardware is alive and working normally. Wait for it to
7368 * finish and then wait for the driver to catch up and reclaim
7369 * all descriptors.
7370 */
7371 while (spg->cidx != htobe16(eq->pidx))
7372 pause("equiesce", 1);
7373 while (eq->cidx != eq->pidx)
7374 pause("dquiesce", 1);
7375 } else {
7376 /*
7377 * Hardware is unavailable. Discard all pending tx and reclaim
7378 * descriptors directly.
7379 */
7380 TXQ_LOCK(txq);
7381 while (eq->cidx != eq->pidx) {
7382 struct mbuf *m, *nextpkt;
7383 struct tx_sdesc *txsd;
7384
7385 txsd = &txq->sdesc[eq->cidx];
7386 for (m = txsd->m; m != NULL; m = nextpkt) {
7387 nextpkt = m->m_nextpkt;
7388 m->m_nextpkt = NULL;
7389 m_freem(m);
7390 }
7391 IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7392 }
7393 spg->pidx = spg->cidx = htobe16(eq->cidx);
7394 TXQ_UNLOCK(txq);
7395 }
7396 }
7397
7398 static void
quiesce_wrq(struct sge_wrq * wrq)7399 quiesce_wrq(struct sge_wrq *wrq)
7400 {
7401 struct wrqe *wr;
7402
7403 TXQ_LOCK(wrq);
7404 while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7405 STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7406 #ifdef INVARIANTS
7407 wrq->nwr_pending--;
7408 wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7409 #endif
7410 free(wr, M_CXGBE);
7411 }
7412 MPASS(wrq->nwr_pending == 0);
7413 MPASS(wrq->ndesc_needed == 0);
7414 wrq->nwr_pending = 0;
7415 wrq->ndesc_needed = 0;
7416 TXQ_UNLOCK(wrq);
7417 }
7418
7419 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7420 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7421 {
7422 /* Synchronize with the interrupt handler */
7423 while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7424 pause("iqfree", 1);
7425
7426 if (fl != NULL) {
7427 MPASS(iq->flags & IQ_HAS_FL);
7428
7429 mtx_lock(&sc->sfl_lock);
7430 FL_LOCK(fl);
7431 fl->flags |= FL_DOOMED;
7432 FL_UNLOCK(fl);
7433 callout_stop(&sc->sfl_callout);
7434 mtx_unlock(&sc->sfl_lock);
7435
7436 KASSERT((fl->flags & FL_STARVING) == 0,
7437 ("%s: still starving", __func__));
7438
7439 /* Release all buffers if hardware is no longer available. */
7440 if (!(iq->flags & IQ_HW_ALLOCATED))
7441 free_fl_buffers(sc, fl);
7442 }
7443 }
7444
7445 /*
7446 * Wait for all activity on all the queues of the VI to complete. It is assumed
7447 * that no new work is being enqueued by the hardware or the driver. That part
7448 * should be arranged before calling this function.
7449 */
7450 static void
quiesce_vi(struct vi_info * vi)7451 quiesce_vi(struct vi_info *vi)
7452 {
7453 int i;
7454 struct adapter *sc = vi->adapter;
7455 struct sge_rxq *rxq;
7456 struct sge_txq *txq;
7457 #ifdef TCP_OFFLOAD
7458 struct sge_ofld_rxq *ofld_rxq;
7459 #endif
7460 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7461 struct sge_ofld_txq *ofld_txq;
7462 #endif
7463
7464 if (!(vi->flags & VI_INIT_DONE))
7465 return;
7466
7467 for_each_txq(vi, i, txq) {
7468 quiesce_txq(txq);
7469 }
7470
7471 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7472 for_each_ofld_txq(vi, i, ofld_txq) {
7473 quiesce_wrq(&ofld_txq->wrq);
7474 }
7475 #endif
7476
7477 for_each_rxq(vi, i, rxq) {
7478 quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7479 }
7480
7481 #ifdef TCP_OFFLOAD
7482 for_each_ofld_rxq(vi, i, ofld_rxq) {
7483 quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7484 }
7485 #endif
7486 }
7487
7488 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7489 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7490 driver_intr_t *handler, void *arg, char *name)
7491 {
7492 int rc;
7493
7494 irq->rid = rid;
7495 irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7496 RF_SHAREABLE | RF_ACTIVE);
7497 if (irq->res == NULL) {
7498 device_printf(sc->dev,
7499 "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7500 return (ENOMEM);
7501 }
7502
7503 rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7504 NULL, handler, arg, &irq->tag);
7505 if (rc != 0) {
7506 device_printf(sc->dev,
7507 "failed to setup interrupt for rid %d, name %s: %d\n",
7508 rid, name, rc);
7509 } else if (name)
7510 bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7511
7512 return (rc);
7513 }
7514
7515 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7516 t4_free_irq(struct adapter *sc, struct irq *irq)
7517 {
7518 if (irq->tag)
7519 bus_teardown_intr(sc->dev, irq->res, irq->tag);
7520 if (irq->res)
7521 bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7522
7523 bzero(irq, sizeof(*irq));
7524
7525 return (0);
7526 }
7527
7528 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7529 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7530 {
7531
7532 regs->version = chip_id(sc) | chip_rev(sc) << 10;
7533 t4_get_regs(sc, buf, regs->len);
7534 }
7535
7536 #define A_PL_INDIR_CMD 0x1f8
7537
7538 #define S_PL_AUTOINC 31
7539 #define M_PL_AUTOINC 0x1U
7540 #define V_PL_AUTOINC(x) ((x) << S_PL_AUTOINC)
7541 #define G_PL_AUTOINC(x) (((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7542
7543 #define S_PL_VFID 20
7544 #define M_PL_VFID 0xffU
7545 #define V_PL_VFID(x) ((x) << S_PL_VFID)
7546 #define G_PL_VFID(x) (((x) >> S_PL_VFID) & M_PL_VFID)
7547
7548 #define S_PL_ADDR 0
7549 #define M_PL_ADDR 0xfffffU
7550 #define V_PL_ADDR(x) ((x) << S_PL_ADDR)
7551 #define G_PL_ADDR(x) (((x) >> S_PL_ADDR) & M_PL_ADDR)
7552
7553 #define A_PL_INDIR_DATA 0x1fc
7554
7555 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7556 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7557 {
7558 u32 stats[2];
7559
7560 if (sc->flags & IS_VF) {
7561 stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7562 stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7563 } else {
7564 mtx_assert(&sc->reg_lock, MA_OWNED);
7565 t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7566 V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7567 stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7568 stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7569 }
7570 return (((uint64_t)stats[1]) << 32 | stats[0]);
7571 }
7572
7573 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7574 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7575 {
7576
7577 #define GET_STAT(name) \
7578 read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7579
7580 if (!(sc->flags & IS_VF))
7581 mtx_lock(&sc->reg_lock);
7582 stats->tx_bcast_bytes = GET_STAT(TX_VF_BCAST_BYTES);
7583 stats->tx_bcast_frames = GET_STAT(TX_VF_BCAST_FRAMES);
7584 stats->tx_mcast_bytes = GET_STAT(TX_VF_MCAST_BYTES);
7585 stats->tx_mcast_frames = GET_STAT(TX_VF_MCAST_FRAMES);
7586 stats->tx_ucast_bytes = GET_STAT(TX_VF_UCAST_BYTES);
7587 stats->tx_ucast_frames = GET_STAT(TX_VF_UCAST_FRAMES);
7588 stats->tx_drop_frames = GET_STAT(TX_VF_DROP_FRAMES);
7589 stats->tx_offload_bytes = GET_STAT(TX_VF_OFFLOAD_BYTES);
7590 stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7591 stats->rx_bcast_bytes = GET_STAT(RX_VF_BCAST_BYTES);
7592 stats->rx_bcast_frames = GET_STAT(RX_VF_BCAST_FRAMES);
7593 stats->rx_mcast_bytes = GET_STAT(RX_VF_MCAST_BYTES);
7594 stats->rx_mcast_frames = GET_STAT(RX_VF_MCAST_FRAMES);
7595 stats->rx_ucast_bytes = GET_STAT(RX_VF_UCAST_BYTES);
7596 stats->rx_ucast_frames = GET_STAT(RX_VF_UCAST_FRAMES);
7597 stats->rx_err_frames = GET_STAT(RX_VF_ERR_FRAMES);
7598 if (!(sc->flags & IS_VF))
7599 mtx_unlock(&sc->reg_lock);
7600
7601 #undef GET_STAT
7602 }
7603
7604 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7605 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7606 {
7607 int reg;
7608
7609 t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7610 V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7611 for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7612 reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7613 t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7614 }
7615
7616 static void
vi_refresh_stats(struct vi_info * vi)7617 vi_refresh_stats(struct vi_info *vi)
7618 {
7619 struct timeval tv;
7620 const struct timeval interval = {0, 250000}; /* 250ms */
7621
7622 mtx_assert(&vi->tick_mtx, MA_OWNED);
7623
7624 if (vi->flags & VI_SKIP_STATS)
7625 return;
7626
7627 getmicrotime(&tv);
7628 timevalsub(&tv, &interval);
7629 if (timevalcmp(&tv, &vi->last_refreshed, <))
7630 return;
7631
7632 t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7633 getmicrotime(&vi->last_refreshed);
7634 }
7635
7636 static void
cxgbe_refresh_stats(struct vi_info * vi)7637 cxgbe_refresh_stats(struct vi_info *vi)
7638 {
7639 u_int i, v, tnl_cong_drops, chan_map;
7640 struct timeval tv;
7641 const struct timeval interval = {0, 250000}; /* 250ms */
7642 struct port_info *pi;
7643 struct adapter *sc;
7644
7645 mtx_assert(&vi->tick_mtx, MA_OWNED);
7646
7647 if (vi->flags & VI_SKIP_STATS)
7648 return;
7649
7650 getmicrotime(&tv);
7651 timevalsub(&tv, &interval);
7652 if (timevalcmp(&tv, &vi->last_refreshed, <))
7653 return;
7654
7655 pi = vi->pi;
7656 sc = vi->adapter;
7657 tnl_cong_drops = 0;
7658 t4_get_port_stats(sc, pi->hw_port, &pi->stats);
7659 chan_map = pi->rx_e_chan_map;
7660 while (chan_map) {
7661 i = ffs(chan_map) - 1;
7662 mtx_lock(&sc->reg_lock);
7663 t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7664 A_TP_MIB_TNL_CNG_DROP_0 + i);
7665 mtx_unlock(&sc->reg_lock);
7666 tnl_cong_drops += v;
7667 chan_map &= ~(1 << i);
7668 }
7669 pi->tnl_cong_drops = tnl_cong_drops;
7670 getmicrotime(&vi->last_refreshed);
7671 }
7672
7673 static void
cxgbe_tick(void * arg)7674 cxgbe_tick(void *arg)
7675 {
7676 struct vi_info *vi = arg;
7677
7678 MPASS(IS_MAIN_VI(vi));
7679 mtx_assert(&vi->tick_mtx, MA_OWNED);
7680
7681 cxgbe_refresh_stats(vi);
7682 callout_schedule(&vi->tick, hz);
7683 }
7684
7685 static void
vi_tick(void * arg)7686 vi_tick(void *arg)
7687 {
7688 struct vi_info *vi = arg;
7689
7690 mtx_assert(&vi->tick_mtx, MA_OWNED);
7691
7692 vi_refresh_stats(vi);
7693 callout_schedule(&vi->tick, hz);
7694 }
7695
7696 /* CIM inbound queues */
7697 static const char *t4_ibq[CIM_NUM_IBQ] = {
7698 "ibq_tp0", "ibq_tp1", "ibq_ulp", "ibq_sge0", "ibq_sge1", "ibq_ncsi"
7699 };
7700 static const char *t7_ibq[CIM_NUM_IBQ_T7] = {
7701 "ibq_tp0", "ibq_tp1", "ibq_tp2", "ibq_tp3", "ibq_ulp", "ibq_sge0",
7702 "ibq_sge1", "ibq_ncsi", NULL, "ibq_ipc1", "ibq_ipc2", "ibq_ipc3",
7703 "ibq_ipc4", "ibq_ipc5", "ibq_ipc6", "ibq_ipc7"
7704 };
7705 static const char *t7_ibq_sec[] = {
7706 "ibq_tp0", "ibq_tp1", "ibq_tp2", "ibq_tp3", "ibq_ulp", "ibq_sge0",
7707 NULL, NULL, NULL, "ibq_ipc0"
7708 };
7709
7710 /* CIM outbound queues */
7711 static const char *t4_obq[CIM_NUM_OBQ_T5] = {
7712 "obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", "obq_ncsi",
7713 "obq_sge_rx_q0", "obq_sge_rx_q1" /* These two are T5/T6 only */
7714 };
7715 static const char *t7_obq[CIM_NUM_OBQ_T7] = {
7716 "obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", "obq_ncsi",
7717 "obq_sge_rx_q0", NULL, NULL, "obq_ipc1", "obq_ipc2", "obq_ipc3",
7718 "obq_ipc4", "obq_ipc5", "obq_ipc6", "obq_ipc7"
7719 };
7720 static const char *t7_obq_sec[] = {
7721 "obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", NULL,
7722 "obq_sge_rx_q0", NULL, NULL, "obq_ipc0"
7723 };
7724
7725 static void
cim_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * c0)7726 cim_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
7727 struct sysctl_oid_list *c0)
7728 {
7729 struct sysctl_oid *oid;
7730 struct sysctl_oid_list *children1;
7731 int i, j, qcount;
7732 char s[16];
7733 const char **qname;
7734
7735 oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "cim",
7736 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "CIM block");
7737 c0 = SYSCTL_CHILDREN(oid);
7738
7739 SYSCTL_ADD_U8(ctx, c0, OID_AUTO, "ncores", CTLFLAG_RD, NULL,
7740 sc->params.ncores, "# of active CIM cores");
7741
7742 for (i = 0; i < sc->params.ncores; i++) {
7743 snprintf(s, sizeof(s), "%u", i);
7744 oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, s,
7745 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "CIM core");
7746 children1 = SYSCTL_CHILDREN(oid);
7747
7748 /*
7749 * CTLFLAG_SKIP because the misc.devlog sysctl already displays
7750 * the log for all cores. Use this sysctl to get the log for a
7751 * particular core only.
7752 */
7753 SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "devlog",
7754 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_SKIP,
7755 sc, i, sysctl_devlog, "A", "firmware's device log");
7756
7757 SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "loadavg",
7758 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7759 sysctl_loadavg, "A",
7760 "microprocessor load averages (select firmwares only)");
7761
7762 SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "qcfg",
7763 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7764 chip_id(sc) > CHELSIO_T6 ? sysctl_cim_qcfg_t7 : sysctl_cim_qcfg,
7765 "A", "Queue configuration");
7766
7767 SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "la",
7768 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7769 sysctl_cim_la, "A", "Logic analyzer");
7770
7771 SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "ma_la",
7772 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7773 sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7774
7775 SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "pif_la",
7776 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7777 sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7778
7779 /* IBQs */
7780 switch (chip_id(sc)) {
7781 case CHELSIO_T4:
7782 case CHELSIO_T5:
7783 case CHELSIO_T6:
7784 qname = &t4_ibq[0];
7785 qcount = nitems(t4_ibq);
7786 break;
7787 case CHELSIO_T7:
7788 default:
7789 if (i == 0) {
7790 qname = &t7_ibq[0];
7791 qcount = nitems(t7_ibq);
7792 } else {
7793 qname = &t7_ibq_sec[0];
7794 qcount = nitems(t7_ibq_sec);
7795 }
7796 break;
7797 }
7798 MPASS(qcount <= sc->chip_params->cim_num_ibq);
7799 for (j = 0; j < qcount; j++) {
7800 if (qname[j] == NULL)
7801 continue;
7802 SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, qname[j],
7803 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7804 (i << 16) | j, sysctl_cim_ibq, "A", NULL);
7805 }
7806
7807 /* OBQs */
7808 switch (chip_id(sc)) {
7809 case CHELSIO_T4:
7810 qname = t4_obq;
7811 qcount = CIM_NUM_OBQ;
7812 break;
7813 case CHELSIO_T5:
7814 case CHELSIO_T6:
7815 qname = t4_obq;
7816 qcount = nitems(t4_obq);
7817 break;
7818 case CHELSIO_T7:
7819 default:
7820 if (i == 0) {
7821 qname = t7_obq;
7822 qcount = nitems(t7_obq);
7823 } else {
7824 qname = t7_obq_sec;
7825 qcount = nitems(t7_obq_sec);
7826 }
7827 break;
7828 }
7829 MPASS(qcount <= sc->chip_params->cim_num_obq);
7830 for (j = 0; j < qcount; j++) {
7831 if (qname[j] == NULL)
7832 continue;
7833 SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, qname[j],
7834 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7835 (i << 16) | j, sysctl_cim_obq, "A", NULL);
7836 }
7837 }
7838 }
7839
7840 /*
7841 * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7842 */
7843 static char *caps_decoder[] = {
7844 "\20\001IPMI\002NCSI", /* 0: NBM */
7845 "\20\001PPP\002QFC\003DCBX", /* 1: link */
7846 "\20\001INGRESS\002EGRESS", /* 2: switch */
7847 "\20\001NIC\002VM\003IDS\004UM\005UM_ISGL" /* 3: NIC */
7848 "\006HASHFILTER\007ETHOFLD",
7849 "\20\001TOE\002SENDPATH", /* 4: TOE */
7850 "\20\001RDDP\002RDMAC\003ROCEv2", /* 5: RDMA */
7851 "\20\001INITIATOR_PDU\002TARGET_PDU" /* 6: iSCSI */
7852 "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7853 "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7854 "\007T10DIF"
7855 "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7856 "\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE" /* 7: Crypto */
7857 "\004TLS_HW,\005TOE_IPSEC",
7858 "\20\001INITIATOR\002TARGET\003CTRL_OFLD" /* 8: FCoE */
7859 "\004PO_INITIATOR\005PO_TARGET",
7860 "\20\001NVMe_TCP", /* 9: NVMe */
7861 };
7862
7863 void
t4_sysctls(struct adapter * sc)7864 t4_sysctls(struct adapter *sc)
7865 {
7866 struct sysctl_ctx_list *ctx = &sc->ctx;
7867 struct sysctl_oid *oid;
7868 struct sysctl_oid_list *children, *c0;
7869 static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7870
7871 /*
7872 * dev.t4nex.X.
7873 */
7874 oid = device_get_sysctl_tree(sc->dev);
7875 c0 = children = SYSCTL_CHILDREN(oid);
7876
7877 sc->sc_do_rxcopy = 1;
7878 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7879 &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7880
7881 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7882 sc->params.nports, "# of ports");
7883
7884 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7885 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7886 (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7887 "available doorbells");
7888
7889 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7890 sc->params.vpd.cclk, "core clock frequency (in KHz)");
7891
7892 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7893 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7894 sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7895 sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7896
7897 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7898 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7899 sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7900 sysctl_int_array, "A", "interrupt holdoff packet counter values");
7901
7902 t4_sge_sysctls(sc, ctx, children);
7903
7904 sc->lro_timeout = 100;
7905 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7906 &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7907
7908 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7909 &sc->debug_flags, 0, "flags to enable runtime debugging");
7910
7911 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iflags", CTLFLAG_RW,
7912 &sc->intr_flags, 0, "flags for the slow interrupt handler");
7913
7914 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7915 CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7916
7917 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7918 CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7919
7920 if (sc->flags & IS_VF)
7921 return;
7922
7923 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7924 NULL, chip_rev(sc), "chip hardware revision");
7925
7926 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7927 CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7928
7929 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7930 CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7931
7932 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7933 CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7934
7935 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7936 CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7937
7938 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7939 CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7940
7941 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7942 sc->er_version, 0, "expansion ROM version");
7943
7944 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7945 sc->bs_version, 0, "bootstrap firmware version");
7946
7947 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7948 NULL, sc->params.scfg_vers, "serial config version");
7949
7950 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7951 NULL, sc->params.vpd_vers, "VPD version");
7952
7953 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7954 CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7955
7956 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7957 sc->cfcsum, "config file checksum");
7958
7959 #define SYSCTL_CAP(name, n, text) \
7960 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7961 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7962 (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7963 "available " text " capabilities")
7964
7965 SYSCTL_CAP(nbmcaps, 0, "NBM");
7966 SYSCTL_CAP(linkcaps, 1, "link");
7967 SYSCTL_CAP(switchcaps, 2, "switch");
7968 SYSCTL_CAP(nvmecaps, 9, "NVMe");
7969 SYSCTL_CAP(niccaps, 3, "NIC");
7970 SYSCTL_CAP(toecaps, 4, "TCP offload");
7971 SYSCTL_CAP(rdmacaps, 5, "RDMA");
7972 SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7973 SYSCTL_CAP(cryptocaps, 7, "crypto");
7974 SYSCTL_CAP(fcoecaps, 8, "FCoE");
7975 #undef SYSCTL_CAP
7976
7977 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7978 NULL, sc->tids.nftids, "number of filters");
7979
7980 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7981 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7982 sysctl_temperature, "I", "chip temperature (in Celsius)");
7983 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7984 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7985 sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7986
7987 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7988 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7989 "I", "core Vdd (in mV)");
7990
7991 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7992 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7993 sysctl_cpus, "A", "local CPUs");
7994
7995 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7996 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7997 sysctl_cpus, "A", "preferred CPUs for interrupts");
7998
7999 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
8000 &sc->swintr, 0, "software triggered interrupts");
8001
8002 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
8003 CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
8004 "1 = reset adapter, 0 = zero reset counter");
8005
8006 /*
8007 * dev.t4nex.X.misc. Marked CTLFLAG_SKIP to avoid information overload.
8008 */
8009 oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
8010 CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
8011 "logs and miscellaneous information");
8012 children = SYSCTL_CHILDREN(oid);
8013
8014 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
8015 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8016 sysctl_cctrl, "A", "congestion control");
8017
8018 cim_sysctls(sc, ctx, children);
8019
8020 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
8021 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8022 sysctl_cpl_stats, "A", "CPL statistics");
8023
8024 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
8025 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8026 sysctl_ddp_stats, "A", "non-TCP DDP statistics");
8027
8028 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
8029 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8030 sysctl_tid_stats, "A", "tid stats");
8031
8032 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
8033 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, -1,
8034 sysctl_devlog, "A", "firmware's device log (all cores)");
8035
8036 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
8037 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8038 sysctl_fcoe_stats, "A", "FCoE statistics");
8039
8040 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
8041 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8042 sysctl_hw_sched, "A", "hardware scheduler ");
8043
8044 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
8045 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8046 sysctl_l2t, "A", "hardware L2 table");
8047
8048 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
8049 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8050 sysctl_smt, "A", "hardware source MAC table");
8051
8052 #ifdef INET6
8053 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
8054 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8055 sysctl_clip, "A", "active CLIP table entries");
8056 #endif
8057
8058 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
8059 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8060 sysctl_lb_stats, "A", "loopback statistics");
8061
8062 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
8063 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8064 sysctl_meminfo, "A", "memory regions");
8065
8066 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
8067 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8068 chip_id(sc) >= CHELSIO_T7 ? sysctl_mps_tcam_t7 :
8069 (chip_id(sc) >= CHELSIO_T6 ? sysctl_mps_tcam_t6 : sysctl_mps_tcam),
8070 "A", "MPS TCAM entries");
8071
8072 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
8073 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8074 sysctl_path_mtus, "A", "path MTUs");
8075
8076 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
8077 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8078 sysctl_pm_stats, "A", "PM statistics");
8079
8080 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
8081 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8082 sysctl_rdma_stats, "A", "RDMA statistics");
8083
8084 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
8085 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8086 sysctl_tcp_stats, "A", "TCP statistics");
8087
8088 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
8089 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8090 sysctl_tids, "A", "TID information");
8091
8092 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
8093 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8094 sysctl_tp_err_stats, "A", "TP error statistics");
8095
8096 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
8097 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8098 sysctl_tnl_stats, "A", "TP tunnel statistics");
8099
8100 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
8101 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
8102 sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
8103
8104 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
8105 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8106 sysctl_tp_la, "A", "TP logic analyzer");
8107
8108 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
8109 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8110 sysctl_tx_rate, "A", "Tx rate");
8111
8112 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
8113 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8114 sysctl_ulprx_la, "A", "ULPRX logic analyzer");
8115
8116 if (chip_id(sc) >= CHELSIO_T5) {
8117 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
8118 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8119 sysctl_wcwr_stats, "A", "write combined work requests");
8120 }
8121
8122 #ifdef KERN_TLS
8123 if (is_ktls(sc)) {
8124 /*
8125 * dev.t4nex.0.tls.
8126 */
8127 oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
8128 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
8129 children = SYSCTL_CHILDREN(oid);
8130
8131 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
8132 CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
8133 "keys in work requests (1) or attempt to store TLS keys "
8134 "in card memory.");
8135
8136 if (is_t6(sc))
8137 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
8138 CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
8139 "combine TCB field updates with TLS record work "
8140 "requests.");
8141 else {
8142 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "short_records",
8143 CTLFLAG_RW, &sc->tlst.short_records, 0,
8144 "Use cipher-only mode for short records.");
8145 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "partial_ghash",
8146 CTLFLAG_RW, &sc->tlst.partial_ghash, 0,
8147 "Use partial GHASH for AES-GCM records.");
8148 }
8149 }
8150 #endif
8151
8152 #ifdef TCP_OFFLOAD
8153 if (is_offload(sc)) {
8154 int i;
8155 char s[4];
8156
8157 /*
8158 * dev.t4nex.X.toe.
8159 */
8160 oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
8161 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
8162 children = SYSCTL_CHILDREN(oid);
8163
8164 sc->tt.cong_algorithm = -1;
8165 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
8166 CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
8167 "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
8168 "3 = highspeed)");
8169
8170 sc->tt.sndbuf = -1;
8171 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
8172 &sc->tt.sndbuf, 0, "hardware send buffer");
8173
8174 sc->tt.ddp = 0;
8175 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
8176 CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
8177 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
8178 &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
8179
8180 sc->tt.rx_coalesce = -1;
8181 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
8182 CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
8183
8184 sc->tt.tls = 1;
8185 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
8186 CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
8187 "Inline TLS allowed");
8188
8189 sc->tt.tx_align = -1;
8190 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
8191 CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
8192
8193 sc->tt.tx_zcopy = 0;
8194 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
8195 CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
8196 "Enable zero-copy aio_write(2)");
8197
8198 sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
8199 SYSCTL_ADD_INT(ctx, children, OID_AUTO,
8200 "cop_managed_offloading", CTLFLAG_RW,
8201 &sc->tt.cop_managed_offloading, 0,
8202 "COP (Connection Offload Policy) controls all TOE offload");
8203
8204 sc->tt.autorcvbuf_inc = 16 * 1024;
8205 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
8206 CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
8207 "autorcvbuf increment");
8208
8209 sc->tt.update_hc_on_pmtu_change = 1;
8210 SYSCTL_ADD_INT(ctx, children, OID_AUTO,
8211 "update_hc_on_pmtu_change", CTLFLAG_RW,
8212 &sc->tt.update_hc_on_pmtu_change, 0,
8213 "Update hostcache entry if the PMTU changes");
8214
8215 sc->tt.iso = 1;
8216 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
8217 &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
8218
8219 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
8220 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8221 sysctl_tp_tick, "A", "TP timer tick (us)");
8222
8223 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
8224 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
8225 sysctl_tp_tick, "A", "TCP timestamp tick (us)");
8226
8227 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
8228 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
8229 sysctl_tp_tick, "A", "DACK tick (us)");
8230
8231 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
8232 CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8233 sysctl_tp_dack_timer, "IU", "DACK timer (us)");
8234
8235 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
8236 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8237 A_TP_RXT_MIN, sysctl_tp_timer, "LU",
8238 "Minimum retransmit interval (us)");
8239
8240 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
8241 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8242 A_TP_RXT_MAX, sysctl_tp_timer, "LU",
8243 "Maximum retransmit interval (us)");
8244
8245 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
8246 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8247 A_TP_PERS_MIN, sysctl_tp_timer, "LU",
8248 "Persist timer min (us)");
8249
8250 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
8251 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8252 A_TP_PERS_MAX, sysctl_tp_timer, "LU",
8253 "Persist timer max (us)");
8254
8255 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
8256 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8257 A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
8258 "Keepalive idle timer (us)");
8259
8260 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
8261 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8262 A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
8263 "Keepalive interval timer (us)");
8264
8265 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
8266 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8267 A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
8268
8269 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
8270 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8271 A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
8272 "FINWAIT2 timer (us)");
8273
8274 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
8275 CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8276 S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
8277 "Number of SYN retransmissions before abort");
8278
8279 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
8280 CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8281 S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
8282 "Number of retransmissions before abort");
8283
8284 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
8285 CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8286 S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
8287 "Number of keepalive probes before abort");
8288
8289 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
8290 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8291 "TOE retransmit backoffs");
8292 children = SYSCTL_CHILDREN(oid);
8293 for (i = 0; i < 16; i++) {
8294 snprintf(s, sizeof(s), "%u", i);
8295 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
8296 CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8297 i, sysctl_tp_backoff, "IU",
8298 "TOE retransmit backoff");
8299 }
8300 }
8301 #endif
8302 }
8303
8304 void
vi_sysctls(struct vi_info * vi)8305 vi_sysctls(struct vi_info *vi)
8306 {
8307 struct sysctl_ctx_list *ctx = &vi->ctx;
8308 struct sysctl_oid *oid;
8309 struct sysctl_oid_list *children;
8310
8311 /*
8312 * dev.v?(cxgbe|cxl).X.
8313 */
8314 oid = device_get_sysctl_tree(vi->dev);
8315 children = SYSCTL_CHILDREN(oid);
8316
8317 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
8318 vi->viid, "VI identifer");
8319 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
8320 &vi->nrxq, 0, "# of rx queues");
8321 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
8322 &vi->ntxq, 0, "# of tx queues");
8323 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
8324 &vi->first_rxq, 0, "index of first rx queue");
8325 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
8326 &vi->first_txq, 0, "index of first tx queue");
8327 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
8328 vi->rss_base, "start of RSS indirection table");
8329 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
8330 vi->rss_size, "size of RSS indirection table");
8331
8332 if (IS_MAIN_VI(vi)) {
8333 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
8334 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8335 sysctl_noflowq, "IU",
8336 "Reserve queue 0 for non-flowid packets");
8337 }
8338
8339 if (vi->adapter->flags & IS_VF) {
8340 MPASS(vi->flags & TX_USES_VM_WR);
8341 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
8342 NULL, 1, "use VM work requests for transmit");
8343 } else {
8344 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
8345 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8346 sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
8347 }
8348
8349 #ifdef TCP_OFFLOAD
8350 if (vi->nofldrxq != 0) {
8351 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
8352 &vi->nofldrxq, 0,
8353 "# of rx queues for offloaded TCP connections");
8354 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
8355 CTLFLAG_RD, &vi->first_ofld_rxq, 0,
8356 "index of first TOE rx queue");
8357 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
8358 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8359 sysctl_holdoff_tmr_idx_ofld, "I",
8360 "holdoff timer index for TOE queues");
8361 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
8362 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8363 sysctl_holdoff_pktc_idx_ofld, "I",
8364 "holdoff packet counter index for TOE queues");
8365 }
8366 #endif
8367 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
8368 if (vi->nofldtxq != 0) {
8369 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
8370 &vi->nofldtxq, 0,
8371 "# of tx queues for TOE/ETHOFLD");
8372 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
8373 CTLFLAG_RD, &vi->first_ofld_txq, 0,
8374 "index of first TOE/ETHOFLD tx queue");
8375 }
8376 #endif
8377 #ifdef DEV_NETMAP
8378 if (vi->nnmrxq != 0) {
8379 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
8380 &vi->nnmrxq, 0, "# of netmap rx queues");
8381 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
8382 &vi->nnmtxq, 0, "# of netmap tx queues");
8383 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
8384 CTLFLAG_RD, &vi->first_nm_rxq, 0,
8385 "index of first netmap rx queue");
8386 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
8387 CTLFLAG_RD, &vi->first_nm_txq, 0,
8388 "index of first netmap tx queue");
8389 }
8390 #endif
8391
8392 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
8393 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8394 sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
8395 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
8396 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8397 sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
8398
8399 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
8400 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8401 sysctl_qsize_rxq, "I", "rx queue size");
8402 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
8403 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8404 sysctl_qsize_txq, "I", "tx queue size");
8405 }
8406
8407 static void
cxgbe_sysctls(struct port_info * pi)8408 cxgbe_sysctls(struct port_info *pi)
8409 {
8410 struct sysctl_ctx_list *ctx = &pi->ctx;
8411 struct sysctl_oid *oid;
8412 struct sysctl_oid_list *children, *children2;
8413 struct adapter *sc = pi->adapter;
8414 int i;
8415 char name[16];
8416 static char *tc_flags = {"\20\1USER"};
8417
8418 /*
8419 * dev.cxgbe.X.
8420 */
8421 oid = device_get_sysctl_tree(pi->dev);
8422 children = SYSCTL_CHILDREN(oid);
8423
8424 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8425 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8426 sysctl_linkdnrc, "A", "reason why link is down");
8427 if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8428 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8429 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8430 sysctl_btphy, "I", "PHY temperature (in Celsius)");
8431 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8432 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8433 sysctl_btphy, "I", "PHY firmware version");
8434 }
8435
8436 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8437 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8438 sysctl_pause_settings, "A",
8439 "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8440 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8441 CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8442 "FEC in use on the link");
8443 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8444 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8445 sysctl_requested_fec, "A",
8446 "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8447 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8448 CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8449 "FEC recommended by the cable/transceiver");
8450 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8451 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8452 sysctl_autoneg, "I",
8453 "autonegotiation (-1 = not supported)");
8454 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8455 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8456 sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8457
8458 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8459 &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8460 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8461 &pi->link_cfg.pcaps, 0, "port capabilities");
8462 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8463 &pi->link_cfg.acaps, 0, "advertised capabilities");
8464 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8465 &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8466
8467 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8468 port_top_speed(pi), "max speed (in Gbps)");
8469 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8470 pi->mps_bg_map, "MPS buffer group map");
8471 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8472 NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8473 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8474 pi->tx_chan, "TP tx c-channel");
8475 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8476 pi->rx_chan, "TP rx c-channel");
8477
8478 if (sc->flags & IS_VF)
8479 return;
8480
8481 /*
8482 * dev.(cxgbe|cxl).X.tc.
8483 */
8484 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8485 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8486 "Tx scheduler traffic classes (cl_rl)");
8487 children2 = SYSCTL_CHILDREN(oid);
8488 SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8489 CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8490 "pktsize for per-flow cl-rl (0 means up to the driver )");
8491 SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8492 CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8493 "burstsize for per-flow cl-rl (0 means up to the driver)");
8494 for (i = 0; i < sc->params.nsched_cls; i++) {
8495 struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8496
8497 snprintf(name, sizeof(name), "%d", i);
8498 children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8499 SYSCTL_CHILDREN(oid), OID_AUTO, name,
8500 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8501 SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8502 CTLFLAG_RD, &tc->state, 0, "current state");
8503 SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8504 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8505 (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8506 SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8507 CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8508 SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8509 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8510 (pi->port_id << 16) | i, sysctl_tc_params, "A",
8511 "traffic class parameters");
8512 }
8513
8514 /*
8515 * dev.cxgbe.X.stats.
8516 */
8517 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8518 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8519 children = SYSCTL_CHILDREN(oid);
8520 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8521 &pi->tx_parse_error, 0,
8522 "# of tx packets with invalid length or # of segments");
8523
8524 #define T4_LBSTAT(name, stat, desc) do { \
8525 if (sc->params.tp.lb_mode) { \
8526 SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8527 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, \
8528 A_MPS_PORT_STAT_##stat##_L, \
8529 sysctl_handle_t4_portstat64, "QU", desc); \
8530 } else { \
8531 SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8532 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8533 t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8534 sysctl_handle_t4_reg64, "QU", desc); \
8535 } \
8536 } while (0)
8537
8538 T4_LBSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8539 T4_LBSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8540 T4_LBSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8541 T4_LBSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8542 T4_LBSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8543 T4_LBSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8544 T4_LBSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8545 T4_LBSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8546 T4_LBSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8547 T4_LBSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8548 T4_LBSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8549 T4_LBSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8550 T4_LBSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8551 T4_LBSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8552 T4_LBSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8553 T4_LBSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8554 T4_LBSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8555 T4_LBSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8556 T4_LBSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8557 T4_LBSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8558 T4_LBSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8559 T4_LBSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8560 T4_LBSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8561
8562 T4_LBSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8563 T4_LBSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8564 T4_LBSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8565 T4_LBSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8566 T4_LBSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8567 T4_LBSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8568 T4_LBSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8569 if (is_t6(sc)) {
8570 /* Read from port_stats and may be stale by up to 1s */
8571 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "rx_fcs_err",
8572 CTLFLAG_RD, &pi->stats.rx_fcs_err,
8573 "# of frames received with bad FCS since last link up");
8574 } else {
8575 T4_LBSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8576 "# of frames received with bad FCS");
8577 }
8578 T4_LBSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8579 T4_LBSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8580 T4_LBSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8581 T4_LBSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8582 T4_LBSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8583 T4_LBSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8584 T4_LBSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8585 T4_LBSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8586 T4_LBSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8587 T4_LBSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8588 T4_LBSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8589 T4_LBSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8590 T4_LBSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8591 T4_LBSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8592 T4_LBSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8593 T4_LBSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8594 T4_LBSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8595 T4_LBSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8596 T4_LBSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8597 #undef T4_LBSTAT
8598
8599 #define T4_REGSTAT(name, stat, desc) do { \
8600 SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8601 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8602 A_MPS_STAT_##stat##_L, sysctl_handle_t4_reg64, "QU", desc); \
8603 } while (0)
8604
8605 if (pi->mps_bg_map & 1) {
8606 T4_REGSTAT(rx_ovflow0, RX_BG_0_MAC_DROP_FRAME,
8607 "# drops due to buffer-group 0 overflows");
8608 T4_REGSTAT(rx_trunc0, RX_BG_0_MAC_TRUNC_FRAME,
8609 "# of buffer-group 0 truncated packets");
8610 }
8611 if (pi->mps_bg_map & 2) {
8612 T4_REGSTAT(rx_ovflow1, RX_BG_1_MAC_DROP_FRAME,
8613 "# drops due to buffer-group 1 overflows");
8614 T4_REGSTAT(rx_trunc1, RX_BG_1_MAC_TRUNC_FRAME,
8615 "# of buffer-group 1 truncated packets");
8616 }
8617 if (pi->mps_bg_map & 4) {
8618 T4_REGSTAT(rx_ovflow2, RX_BG_2_MAC_DROP_FRAME,
8619 "# drops due to buffer-group 2 overflows");
8620 T4_REGSTAT(rx_trunc2, RX_BG_2_MAC_TRUNC_FRAME,
8621 "# of buffer-group 2 truncated packets");
8622 }
8623 if (pi->mps_bg_map & 8) {
8624 T4_REGSTAT(rx_ovflow3, RX_BG_3_MAC_DROP_FRAME,
8625 "# drops due to buffer-group 3 overflows");
8626 T4_REGSTAT(rx_trunc3, RX_BG_3_MAC_TRUNC_FRAME,
8627 "# of buffer-group 3 truncated packets");
8628 }
8629 #undef T4_REGSTAT
8630 }
8631
8632 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8633 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8634 {
8635 int rc, *i, space = 0;
8636 struct sbuf sb;
8637
8638 sbuf_new_for_sysctl(&sb, NULL, 64, req);
8639 for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8640 if (space)
8641 sbuf_printf(&sb, " ");
8642 sbuf_printf(&sb, "%d", *i);
8643 space = 1;
8644 }
8645 rc = sbuf_finish(&sb);
8646 sbuf_delete(&sb);
8647 return (rc);
8648 }
8649
8650 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8651 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8652 {
8653 int rc;
8654 struct sbuf *sb;
8655
8656 sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8657 if (sb == NULL)
8658 return (ENOMEM);
8659
8660 sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8661 rc = sbuf_finish(sb);
8662 sbuf_delete(sb);
8663
8664 return (rc);
8665 }
8666
8667 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8668 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8669 {
8670 int rc;
8671 struct sbuf *sb;
8672
8673 sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8674 if (sb == NULL)
8675 return (ENOMEM);
8676
8677 sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8678 rc = sbuf_finish(sb);
8679 sbuf_delete(sb);
8680
8681 return (rc);
8682 }
8683
8684 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8685 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8686 {
8687 struct port_info *pi = arg1;
8688 int op = arg2;
8689 struct adapter *sc = pi->adapter;
8690 u_int v;
8691 int rc;
8692
8693 rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8694 if (rc)
8695 return (rc);
8696 if (!hw_all_ok(sc))
8697 rc = ENXIO;
8698 else {
8699 /* XXX: magic numbers */
8700 rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8701 op ? 0x20 : 0xc820, &v);
8702 }
8703 end_synchronized_op(sc, 0);
8704 if (rc)
8705 return (rc);
8706 if (op == 0)
8707 v /= 256;
8708
8709 rc = sysctl_handle_int(oidp, &v, 0, req);
8710 return (rc);
8711 }
8712
8713 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8714 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8715 {
8716 struct vi_info *vi = arg1;
8717 int rc, val;
8718
8719 val = vi->rsrv_noflowq;
8720 rc = sysctl_handle_int(oidp, &val, 0, req);
8721 if (rc != 0 || req->newptr == NULL)
8722 return (rc);
8723
8724 if ((val >= 1) && (vi->ntxq > 1))
8725 vi->rsrv_noflowq = 1;
8726 else
8727 vi->rsrv_noflowq = 0;
8728
8729 return (rc);
8730 }
8731
8732 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8733 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8734 {
8735 struct vi_info *vi = arg1;
8736 struct adapter *sc = vi->adapter;
8737 int rc, val, i;
8738
8739 MPASS(!(sc->flags & IS_VF));
8740
8741 val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8742 rc = sysctl_handle_int(oidp, &val, 0, req);
8743 if (rc != 0 || req->newptr == NULL)
8744 return (rc);
8745
8746 if (val != 0 && val != 1)
8747 return (EINVAL);
8748
8749 rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8750 "t4txvm");
8751 if (rc)
8752 return (rc);
8753 if (!hw_all_ok(sc))
8754 rc = ENXIO;
8755 else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8756 /*
8757 * We don't want parse_pkt to run with one setting (VF or PF)
8758 * and then eth_tx to see a different setting but still use
8759 * stale information calculated by parse_pkt.
8760 */
8761 rc = EBUSY;
8762 } else {
8763 struct port_info *pi = vi->pi;
8764 struct sge_txq *txq;
8765 uint32_t ctrl0;
8766 uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8767
8768 if (val) {
8769 vi->flags |= TX_USES_VM_WR;
8770 if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8771 ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8772 V_TXPKT_INTF(pi->hw_port));
8773 if (!(sc->flags & IS_VF))
8774 npkt--;
8775 } else {
8776 vi->flags &= ~TX_USES_VM_WR;
8777 if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8778 ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8779 V_TXPKT_INTF(pi->hw_port) | V_TXPKT_PF(sc->pf) |
8780 V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8781 }
8782 for_each_txq(vi, i, txq) {
8783 txq->cpl_ctrl0 = ctrl0;
8784 txq->txp.max_npkt = npkt;
8785 }
8786 }
8787 end_synchronized_op(sc, LOCK_HELD);
8788 return (rc);
8789 }
8790
8791 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8792 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8793 {
8794 struct vi_info *vi = arg1;
8795 struct adapter *sc = vi->adapter;
8796 int idx, rc, i;
8797 struct sge_rxq *rxq;
8798 uint8_t v;
8799
8800 idx = vi->tmr_idx;
8801
8802 rc = sysctl_handle_int(oidp, &idx, 0, req);
8803 if (rc != 0 || req->newptr == NULL)
8804 return (rc);
8805
8806 if (idx < 0 || idx >= SGE_NTIMERS)
8807 return (EINVAL);
8808
8809 rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8810 "t4tmr");
8811 if (rc)
8812 return (rc);
8813
8814 v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8815 for_each_rxq(vi, i, rxq) {
8816 #ifdef atomic_store_rel_8
8817 atomic_store_rel_8(&rxq->iq.intr_params, v);
8818 #else
8819 rxq->iq.intr_params = v;
8820 #endif
8821 }
8822 vi->tmr_idx = idx;
8823
8824 end_synchronized_op(sc, LOCK_HELD);
8825 return (0);
8826 }
8827
8828 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8829 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8830 {
8831 struct vi_info *vi = arg1;
8832 struct adapter *sc = vi->adapter;
8833 int idx, rc;
8834
8835 idx = vi->pktc_idx;
8836
8837 rc = sysctl_handle_int(oidp, &idx, 0, req);
8838 if (rc != 0 || req->newptr == NULL)
8839 return (rc);
8840
8841 if (idx < -1 || idx >= SGE_NCOUNTERS)
8842 return (EINVAL);
8843
8844 rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8845 "t4pktc");
8846 if (rc)
8847 return (rc);
8848
8849 if (vi->flags & VI_INIT_DONE)
8850 rc = EBUSY; /* cannot be changed once the queues are created */
8851 else
8852 vi->pktc_idx = idx;
8853
8854 end_synchronized_op(sc, LOCK_HELD);
8855 return (rc);
8856 }
8857
8858 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8859 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8860 {
8861 struct vi_info *vi = arg1;
8862 struct adapter *sc = vi->adapter;
8863 int qsize, rc;
8864
8865 qsize = vi->qsize_rxq;
8866
8867 rc = sysctl_handle_int(oidp, &qsize, 0, req);
8868 if (rc != 0 || req->newptr == NULL)
8869 return (rc);
8870
8871 if (qsize < 128 || (qsize & 7))
8872 return (EINVAL);
8873
8874 rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8875 "t4rxqs");
8876 if (rc)
8877 return (rc);
8878
8879 if (vi->flags & VI_INIT_DONE)
8880 rc = EBUSY; /* cannot be changed once the queues are created */
8881 else
8882 vi->qsize_rxq = qsize;
8883
8884 end_synchronized_op(sc, LOCK_HELD);
8885 return (rc);
8886 }
8887
8888 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8889 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8890 {
8891 struct vi_info *vi = arg1;
8892 struct adapter *sc = vi->adapter;
8893 int qsize, rc;
8894
8895 qsize = vi->qsize_txq;
8896
8897 rc = sysctl_handle_int(oidp, &qsize, 0, req);
8898 if (rc != 0 || req->newptr == NULL)
8899 return (rc);
8900
8901 if (qsize < 128 || qsize > 65536)
8902 return (EINVAL);
8903
8904 rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8905 "t4txqs");
8906 if (rc)
8907 return (rc);
8908
8909 if (vi->flags & VI_INIT_DONE)
8910 rc = EBUSY; /* cannot be changed once the queues are created */
8911 else
8912 vi->qsize_txq = qsize;
8913
8914 end_synchronized_op(sc, LOCK_HELD);
8915 return (rc);
8916 }
8917
8918 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8919 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8920 {
8921 struct port_info *pi = arg1;
8922 struct adapter *sc = pi->adapter;
8923 struct link_config *lc = &pi->link_cfg;
8924 int rc;
8925
8926 if (req->newptr == NULL) {
8927 struct sbuf *sb;
8928 static char *bits = "\20\1RX\2TX\3AUTO";
8929
8930 sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8931 if (sb == NULL)
8932 return (ENOMEM);
8933
8934 if (lc->link_ok) {
8935 sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8936 (lc->requested_fc & PAUSE_AUTONEG), bits);
8937 } else {
8938 sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8939 PAUSE_RX | PAUSE_AUTONEG), bits);
8940 }
8941 rc = sbuf_finish(sb);
8942 sbuf_delete(sb);
8943 } else {
8944 char s[2];
8945 int n;
8946
8947 s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8948 PAUSE_AUTONEG));
8949 s[1] = 0;
8950
8951 rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8952 if (rc != 0)
8953 return(rc);
8954
8955 if (s[1] != 0)
8956 return (EINVAL);
8957 if (s[0] < '0' || s[0] > '9')
8958 return (EINVAL); /* not a number */
8959 n = s[0] - '0';
8960 if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8961 return (EINVAL); /* some other bit is set too */
8962
8963 rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8964 "t4PAUSE");
8965 if (rc)
8966 return (rc);
8967 if (hw_all_ok(sc)) {
8968 PORT_LOCK(pi);
8969 lc->requested_fc = n;
8970 fixup_link_config(pi);
8971 if (pi->up_vis > 0)
8972 rc = apply_link_config(pi);
8973 set_current_media(pi);
8974 PORT_UNLOCK(pi);
8975 }
8976 end_synchronized_op(sc, 0);
8977 }
8978
8979 return (rc);
8980 }
8981
8982 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8983 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8984 {
8985 struct port_info *pi = arg1;
8986 struct link_config *lc = &pi->link_cfg;
8987 int rc;
8988 struct sbuf *sb;
8989
8990 sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8991 if (sb == NULL)
8992 return (ENOMEM);
8993 if (lc->link_ok)
8994 sbuf_printf(sb, "%b", lc->fec, t4_fec_bits);
8995 else
8996 sbuf_printf(sb, "no link");
8997 rc = sbuf_finish(sb);
8998 sbuf_delete(sb);
8999
9000 return (rc);
9001 }
9002
9003 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)9004 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
9005 {
9006 struct port_info *pi = arg1;
9007 struct adapter *sc = pi->adapter;
9008 struct link_config *lc = &pi->link_cfg;
9009 int rc;
9010 int8_t old = lc->requested_fec;
9011
9012 if (req->newptr == NULL) {
9013 struct sbuf *sb;
9014
9015 sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
9016 if (sb == NULL)
9017 return (ENOMEM);
9018
9019 sbuf_printf(sb, "%b", old, t4_fec_bits);
9020 rc = sbuf_finish(sb);
9021 sbuf_delete(sb);
9022 } else {
9023 char s[8];
9024 int n;
9025
9026 snprintf(s, sizeof(s), "%d", old == FEC_AUTO ? -1 :
9027 old & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
9028
9029 rc = sysctl_handle_string(oidp, s, sizeof(s), req);
9030 if (rc != 0)
9031 return(rc);
9032
9033 n = strtol(&s[0], NULL, 0);
9034 if (n < 0 || n & FEC_AUTO)
9035 n = FEC_AUTO;
9036 else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
9037 return (EINVAL);/* some other bit is set too */
9038
9039 rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
9040 "t4reqf");
9041 if (rc)
9042 return (rc);
9043 PORT_LOCK(pi);
9044 if (lc->requested_fec != old) {
9045 rc = EBUSY;
9046 goto done;
9047 }
9048 if (n == FEC_AUTO)
9049 lc->requested_fec = FEC_AUTO;
9050 else if (n == 0 || n == FEC_NONE)
9051 lc->requested_fec = FEC_NONE;
9052 else {
9053 if ((lc->pcaps |
9054 V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
9055 lc->pcaps) {
9056 rc = ENOTSUP;
9057 goto done;
9058 }
9059 lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
9060 FEC_MODULE);
9061 }
9062 if (hw_all_ok(sc)) {
9063 fixup_link_config(pi);
9064 if (pi->up_vis > 0) {
9065 rc = apply_link_config(pi);
9066 if (rc != 0) {
9067 lc->requested_fec = old;
9068 if (rc == FW_EPROTO)
9069 rc = ENOTSUP;
9070 }
9071 }
9072 }
9073 done:
9074 PORT_UNLOCK(pi);
9075 end_synchronized_op(sc, 0);
9076 }
9077
9078 return (rc);
9079 }
9080
9081 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)9082 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
9083 {
9084 struct port_info *pi = arg1;
9085 struct adapter *sc = pi->adapter;
9086 struct link_config *lc = &pi->link_cfg;
9087 int rc;
9088 int8_t fec;
9089 struct sbuf *sb;
9090
9091 sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
9092 if (sb == NULL)
9093 return (ENOMEM);
9094
9095 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
9096 rc = EBUSY;
9097 goto done;
9098 }
9099 if (!hw_all_ok(sc)) {
9100 rc = ENXIO;
9101 goto done;
9102 }
9103 PORT_LOCK(pi);
9104 if (pi->up_vis == 0) {
9105 /*
9106 * If all the interfaces are administratively down the firmware
9107 * does not report transceiver changes. Refresh port info here.
9108 * This is the only reason we have a synchronized op in this
9109 * function. Just PORT_LOCK would have been enough otherwise.
9110 */
9111 t4_update_port_info(pi);
9112 }
9113
9114 fec = lc->fec_hint;
9115 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
9116 !fec_supported(lc->pcaps)) {
9117 PORT_UNLOCK(pi);
9118 sbuf_printf(sb, "n/a");
9119 } else {
9120 if (fec == 0)
9121 fec = FEC_NONE;
9122 PORT_UNLOCK(pi);
9123 sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, t4_fec_bits);
9124 }
9125 rc = sbuf_finish(sb);
9126 done:
9127 sbuf_delete(sb);
9128 end_synchronized_op(sc, 0);
9129
9130 return (rc);
9131 }
9132
9133 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)9134 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
9135 {
9136 struct port_info *pi = arg1;
9137 struct adapter *sc = pi->adapter;
9138 struct link_config *lc = &pi->link_cfg;
9139 int rc, val;
9140
9141 if (lc->pcaps & FW_PORT_CAP32_ANEG)
9142 val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
9143 else
9144 val = -1;
9145 rc = sysctl_handle_int(oidp, &val, 0, req);
9146 if (rc != 0 || req->newptr == NULL)
9147 return (rc);
9148 if (val == 0)
9149 val = AUTONEG_DISABLE;
9150 else if (val == 1)
9151 val = AUTONEG_ENABLE;
9152 else
9153 val = AUTONEG_AUTO;
9154
9155 rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
9156 "t4aneg");
9157 if (rc)
9158 return (rc);
9159 PORT_LOCK(pi);
9160 if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
9161 rc = ENOTSUP;
9162 goto done;
9163 }
9164 lc->requested_aneg = val;
9165 if (hw_all_ok(sc)) {
9166 fixup_link_config(pi);
9167 if (pi->up_vis > 0)
9168 rc = apply_link_config(pi);
9169 set_current_media(pi);
9170 }
9171 done:
9172 PORT_UNLOCK(pi);
9173 end_synchronized_op(sc, 0);
9174 return (rc);
9175 }
9176
9177 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)9178 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
9179 {
9180 struct port_info *pi = arg1;
9181 struct adapter *sc = pi->adapter;
9182 struct link_config *lc = &pi->link_cfg;
9183 int rc, val;
9184
9185 val = lc->force_fec;
9186 MPASS(val >= -1 && val <= 1);
9187 rc = sysctl_handle_int(oidp, &val, 0, req);
9188 if (rc != 0 || req->newptr == NULL)
9189 return (rc);
9190 if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
9191 return (ENOTSUP);
9192 if (val < -1 || val > 1)
9193 return (EINVAL);
9194
9195 rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
9196 if (rc)
9197 return (rc);
9198 PORT_LOCK(pi);
9199 lc->force_fec = val;
9200 if (hw_all_ok(sc)) {
9201 fixup_link_config(pi);
9202 if (pi->up_vis > 0)
9203 rc = apply_link_config(pi);
9204 }
9205 PORT_UNLOCK(pi);
9206 end_synchronized_op(sc, 0);
9207 return (rc);
9208 }
9209
9210 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)9211 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
9212 {
9213 struct adapter *sc = arg1;
9214 int rc, reg = arg2;
9215 uint64_t val;
9216
9217 mtx_lock(&sc->reg_lock);
9218 if (hw_off_limits(sc))
9219 rc = ENXIO;
9220 else {
9221 rc = 0;
9222 val = t4_read_reg64(sc, reg);
9223 }
9224 mtx_unlock(&sc->reg_lock);
9225 if (rc == 0)
9226 rc = sysctl_handle_64(oidp, &val, 0, req);
9227 return (rc);
9228 }
9229
9230 static int
sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS)9231 sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS)
9232 {
9233 struct port_info *pi = arg1;
9234 struct adapter *sc = pi->adapter;
9235 int rc, i, reg = arg2;
9236 uint64_t val;
9237
9238 mtx_lock(&sc->reg_lock);
9239 if (hw_off_limits(sc))
9240 rc = ENXIO;
9241 else {
9242 val = 0;
9243 for (i = 0; i < sc->params.tp.lb_nchan; i++) {
9244 val += t4_read_reg64(sc,
9245 t4_port_reg(sc, pi->tx_chan + i, reg));
9246 }
9247 rc = 0;
9248 }
9249 mtx_unlock(&sc->reg_lock);
9250 if (rc == 0)
9251 rc = sysctl_handle_64(oidp, &val, 0, req);
9252 return (rc);
9253 }
9254
9255 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)9256 sysctl_temperature(SYSCTL_HANDLER_ARGS)
9257 {
9258 struct adapter *sc = arg1;
9259 int rc, t;
9260 uint32_t param, val;
9261
9262 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
9263 if (rc)
9264 return (rc);
9265 if (!hw_all_ok(sc))
9266 rc = ENXIO;
9267 else {
9268 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9269 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9270 V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
9271 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
9272 }
9273 end_synchronized_op(sc, 0);
9274 if (rc)
9275 return (rc);
9276
9277 /* unknown is returned as 0 but we display -1 in that case */
9278 t = val == 0 ? -1 : val;
9279
9280 rc = sysctl_handle_int(oidp, &t, 0, req);
9281 return (rc);
9282 }
9283
9284 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)9285 sysctl_vdd(SYSCTL_HANDLER_ARGS)
9286 {
9287 struct adapter *sc = arg1;
9288 int rc;
9289 uint32_t param, val;
9290
9291 if (sc->params.core_vdd == 0) {
9292 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
9293 "t4vdd");
9294 if (rc)
9295 return (rc);
9296 if (!hw_all_ok(sc))
9297 rc = ENXIO;
9298 else {
9299 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9300 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9301 V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
9302 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
9303 ¶m, &val);
9304 }
9305 end_synchronized_op(sc, 0);
9306 if (rc)
9307 return (rc);
9308 sc->params.core_vdd = val;
9309 }
9310
9311 return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
9312 }
9313
9314 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)9315 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
9316 {
9317 struct adapter *sc = arg1;
9318 int rc, v;
9319 uint32_t param, val;
9320
9321 v = sc->sensor_resets;
9322 rc = sysctl_handle_int(oidp, &v, 0, req);
9323 if (rc != 0 || req->newptr == NULL || v <= 0)
9324 return (rc);
9325
9326 if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
9327 chip_id(sc) < CHELSIO_T5)
9328 return (ENOTSUP);
9329
9330 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
9331 if (rc)
9332 return (rc);
9333 if (!hw_all_ok(sc))
9334 rc = ENXIO;
9335 else {
9336 param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9337 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9338 V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
9339 val = 1;
9340 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
9341 }
9342 end_synchronized_op(sc, 0);
9343 if (rc == 0)
9344 sc->sensor_resets++;
9345 return (rc);
9346 }
9347
9348 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)9349 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
9350 {
9351 struct adapter *sc = arg1;
9352 struct sbuf *sb;
9353 int rc;
9354 uint32_t param, val;
9355 uint8_t coreid = (uint8_t)arg2;
9356
9357 KASSERT(coreid < sc->params.ncores,
9358 ("%s: bad coreid %u\n", __func__, coreid));
9359
9360 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
9361 if (rc)
9362 return (rc);
9363 if (!hw_all_ok(sc))
9364 rc = ENXIO;
9365 else {
9366 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9367 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD) |
9368 V_FW_PARAMS_PARAM_Y(coreid);
9369 rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
9370 }
9371 end_synchronized_op(sc, 0);
9372 if (rc)
9373 return (rc);
9374
9375 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9376 if (sb == NULL)
9377 return (ENOMEM);
9378
9379 if (val == 0xffffffff) {
9380 /* Only debug and custom firmwares report load averages. */
9381 sbuf_printf(sb, "not available");
9382 } else {
9383 sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
9384 (val >> 16) & 0xff);
9385 }
9386 rc = sbuf_finish(sb);
9387 sbuf_delete(sb);
9388
9389 return (rc);
9390 }
9391
9392 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)9393 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
9394 {
9395 struct adapter *sc = arg1;
9396 struct sbuf *sb;
9397 int rc, i;
9398 uint16_t incr[NMTUS][NCCTRL_WIN];
9399 static const char *dec_fac[] = {
9400 "0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
9401 "0.9375"
9402 };
9403
9404 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9405 if (sb == NULL)
9406 return (ENOMEM);
9407
9408 rc = 0;
9409 mtx_lock(&sc->reg_lock);
9410 if (hw_off_limits(sc))
9411 rc = ENXIO;
9412 else
9413 t4_read_cong_tbl(sc, incr);
9414 mtx_unlock(&sc->reg_lock);
9415 if (rc)
9416 goto done;
9417
9418 for (i = 0; i < NCCTRL_WIN; ++i) {
9419 sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
9420 incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
9421 incr[5][i], incr[6][i], incr[7][i]);
9422 sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
9423 incr[8][i], incr[9][i], incr[10][i], incr[11][i],
9424 incr[12][i], incr[13][i], incr[14][i], incr[15][i],
9425 sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
9426 }
9427
9428 rc = sbuf_finish(sb);
9429 done:
9430 sbuf_delete(sb);
9431 return (rc);
9432 }
9433
9434 static int
sysctl_cim_ibq(SYSCTL_HANDLER_ARGS)9435 sysctl_cim_ibq(SYSCTL_HANDLER_ARGS)
9436 {
9437 struct adapter *sc = arg1;
9438 struct sbuf *sb;
9439 int rc, i, n, qid, coreid;
9440 uint32_t *buf, *p;
9441
9442 qid = arg2 & 0xffff;
9443 coreid = arg2 >> 16;
9444
9445 KASSERT(qid >= 0 && qid < sc->chip_params->cim_num_ibq,
9446 ("%s: bad ibq qid %d\n", __func__, qid));
9447 KASSERT(coreid >= 0 && coreid < sc->params.ncores,
9448 ("%s: bad coreid %d\n", __func__, coreid));
9449
9450 n = 4 * CIM_IBQ_SIZE;
9451 buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9452 mtx_lock(&sc->reg_lock);
9453 if (hw_off_limits(sc))
9454 rc = -ENXIO;
9455 else
9456 rc = t4_read_cim_ibq_core(sc, coreid, qid, buf, n);
9457 mtx_unlock(&sc->reg_lock);
9458 if (rc < 0) {
9459 rc = -rc;
9460 goto done;
9461 }
9462 n = rc * sizeof(uint32_t); /* rc has # of words actually read */
9463
9464 sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9465 if (sb == NULL) {
9466 rc = ENOMEM;
9467 goto done;
9468 }
9469 for (i = 0, p = buf; i < n; i += 16, p += 4)
9470 sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9471 p[2], p[3]);
9472 rc = sbuf_finish(sb);
9473 sbuf_delete(sb);
9474 done:
9475 free(buf, M_CXGBE);
9476 return (rc);
9477 }
9478
9479 static int
sysctl_cim_obq(SYSCTL_HANDLER_ARGS)9480 sysctl_cim_obq(SYSCTL_HANDLER_ARGS)
9481 {
9482 struct adapter *sc = arg1;
9483 struct sbuf *sb;
9484 int rc, i, n, qid, coreid;
9485 uint32_t *buf, *p;
9486
9487 qid = arg2 & 0xffff;
9488 coreid = arg2 >> 16;
9489
9490 KASSERT(qid >= 0 && qid < sc->chip_params->cim_num_obq,
9491 ("%s: bad obq qid %d\n", __func__, qid));
9492 KASSERT(coreid >= 0 && coreid < sc->params.ncores,
9493 ("%s: bad coreid %d\n", __func__, coreid));
9494
9495 n = 6 * CIM_OBQ_SIZE * 4;
9496 buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9497 mtx_lock(&sc->reg_lock);
9498 if (hw_off_limits(sc))
9499 rc = -ENXIO;
9500 else
9501 rc = t4_read_cim_obq_core(sc, coreid, qid, buf, n);
9502 mtx_unlock(&sc->reg_lock);
9503 if (rc < 0) {
9504 rc = -rc;
9505 goto done;
9506 }
9507 n = rc * sizeof(uint32_t); /* rc has # of words actually read */
9508
9509 rc = sysctl_wire_old_buffer(req, 0);
9510 if (rc != 0)
9511 goto done;
9512
9513 sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9514 if (sb == NULL) {
9515 rc = ENOMEM;
9516 goto done;
9517 }
9518 for (i = 0, p = buf; i < n; i += 16, p += 4)
9519 sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9520 p[2], p[3]);
9521 rc = sbuf_finish(sb);
9522 sbuf_delete(sb);
9523 done:
9524 free(buf, M_CXGBE);
9525 return (rc);
9526 }
9527
9528 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9529 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9530 {
9531 uint32_t *p;
9532
9533 sbuf_printf(sb, "Status Data PC%s",
9534 cfg & F_UPDBGLACAPTPCONLY ? "" :
9535 " LS0Stat LS0Addr LS0Data");
9536
9537 for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9538 if (cfg & F_UPDBGLACAPTPCONLY) {
9539 sbuf_printf(sb, "\n %02x %08x %08x", p[5] & 0xff,
9540 p[6], p[7]);
9541 sbuf_printf(sb, "\n %02x %02x%06x %02x%06x",
9542 (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9543 p[4] & 0xff, p[5] >> 8);
9544 sbuf_printf(sb, "\n %02x %x%07x %x%07x",
9545 (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9546 p[1] & 0xf, p[2] >> 4);
9547 } else {
9548 sbuf_printf(sb,
9549 "\n %02x %x%07x %x%07x %08x %08x "
9550 "%08x%08x%08x%08x",
9551 (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9552 p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9553 p[6], p[7]);
9554 }
9555 }
9556 }
9557
9558 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9559 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9560 {
9561 uint32_t *p;
9562
9563 sbuf_printf(sb, "Status Inst Data PC%s",
9564 cfg & F_UPDBGLACAPTPCONLY ? "" :
9565 " LS0Stat LS0Addr LS0Data LS1Stat LS1Addr LS1Data");
9566
9567 for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9568 if (cfg & F_UPDBGLACAPTPCONLY) {
9569 sbuf_printf(sb, "\n %02x %08x %08x %08x",
9570 p[3] & 0xff, p[2], p[1], p[0]);
9571 sbuf_printf(sb, "\n %02x %02x%06x %02x%06x %02x%06x",
9572 (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9573 p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9574 sbuf_printf(sb, "\n %02x %04x%04x %04x%04x %04x%04x",
9575 (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9576 p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9577 p[6] >> 16);
9578 } else {
9579 sbuf_printf(sb, "\n %02x %04x%04x %04x%04x %04x%04x "
9580 "%08x %08x %08x %08x %08x %08x",
9581 (p[9] >> 16) & 0xff,
9582 p[9] & 0xffff, p[8] >> 16,
9583 p[8] & 0xffff, p[7] >> 16,
9584 p[7] & 0xffff, p[6] >> 16,
9585 p[2], p[1], p[0], p[5], p[4], p[3]);
9586 }
9587 }
9588 }
9589
9590 static int
sbuf_cim_la(struct adapter * sc,int coreid,struct sbuf * sb,int flags)9591 sbuf_cim_la(struct adapter *sc, int coreid, struct sbuf *sb, int flags)
9592 {
9593 uint32_t cfg, *buf;
9594 int rc;
9595
9596 MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9597 buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9598 M_ZERO | flags);
9599 if (buf == NULL)
9600 return (ENOMEM);
9601
9602 mtx_lock(&sc->reg_lock);
9603 if (hw_off_limits(sc))
9604 rc = ENXIO;
9605 else {
9606 rc = -t4_cim_read_core(sc, 1, coreid, A_UP_UP_DBG_LA_CFG, 1,
9607 &cfg);
9608 if (rc == 0)
9609 rc = -t4_cim_read_la_core(sc, coreid, buf, NULL);
9610 }
9611 mtx_unlock(&sc->reg_lock);
9612 if (rc == 0) {
9613 if (chip_id(sc) < CHELSIO_T6)
9614 sbuf_cim_la4(sc, sb, buf, cfg);
9615 else
9616 sbuf_cim_la6(sc, sb, buf, cfg);
9617 }
9618 free(buf, M_CXGBE);
9619 return (rc);
9620 }
9621
9622 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9623 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9624 {
9625 struct adapter *sc = arg1;
9626 int coreid = arg2;
9627 struct sbuf *sb;
9628 int rc;
9629
9630 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9631 if (sb == NULL)
9632 return (ENOMEM);
9633
9634 rc = sbuf_cim_la(sc, coreid, sb, M_WAITOK);
9635 if (rc == 0)
9636 rc = sbuf_finish(sb);
9637 sbuf_delete(sb);
9638 return (rc);
9639 }
9640
9641 static void
dump_cim_regs(struct adapter * sc)9642 dump_cim_regs(struct adapter *sc)
9643 {
9644 log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9645 device_get_nameunit(sc->dev),
9646 t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9647 t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9648 t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9649 t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9650 t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9651 log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9652 device_get_nameunit(sc->dev),
9653 t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9654 t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9655 t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9656 t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9657 t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9658 }
9659
9660 static void
dump_cimla(struct adapter * sc)9661 dump_cimla(struct adapter *sc)
9662 {
9663 struct sbuf sb;
9664 int rc;
9665
9666 if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9667 log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9668 device_get_nameunit(sc->dev));
9669 return;
9670 }
9671 rc = sbuf_cim_la(sc, 0, &sb, M_WAITOK);
9672 if (rc == 0) {
9673 rc = sbuf_finish(&sb);
9674 if (rc == 0) {
9675 log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9676 device_get_nameunit(sc->dev), sbuf_data(&sb));
9677 }
9678 }
9679 sbuf_delete(&sb);
9680 }
9681
9682 void
t4_os_cim_err(struct adapter * sc)9683 t4_os_cim_err(struct adapter *sc)
9684 {
9685 atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9686 }
9687
9688 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9689 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9690 {
9691 struct adapter *sc = arg1;
9692 u_int i;
9693 struct sbuf *sb;
9694 uint32_t *buf, *p;
9695 int rc;
9696
9697 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9698 if (sb == NULL)
9699 return (ENOMEM);
9700
9701 buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9702 M_ZERO | M_WAITOK);
9703
9704 rc = 0;
9705 mtx_lock(&sc->reg_lock);
9706 if (hw_off_limits(sc))
9707 rc = ENXIO;
9708 else
9709 t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9710 mtx_unlock(&sc->reg_lock);
9711 if (rc)
9712 goto done;
9713
9714 p = buf;
9715 for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9716 sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9717 p[1], p[0]);
9718 }
9719
9720 sbuf_printf(sb, "\n\nCnt ID Tag UE Data RDY VLD");
9721 for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9722 sbuf_printf(sb, "\n%3u %2u %x %u %08x%08x %u %u",
9723 (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9724 (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9725 (p[1] >> 2) | ((p[2] & 3) << 30),
9726 (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9727 p[0] & 1);
9728 }
9729 rc = sbuf_finish(sb);
9730 done:
9731 sbuf_delete(sb);
9732 free(buf, M_CXGBE);
9733 return (rc);
9734 }
9735
9736 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9737 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9738 {
9739 struct adapter *sc = arg1;
9740 u_int i;
9741 struct sbuf *sb;
9742 uint32_t *buf, *p;
9743 int rc;
9744
9745 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9746 if (sb == NULL)
9747 return (ENOMEM);
9748
9749 buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9750 M_ZERO | M_WAITOK);
9751
9752 rc = 0;
9753 mtx_lock(&sc->reg_lock);
9754 if (hw_off_limits(sc))
9755 rc = ENXIO;
9756 else
9757 t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9758 mtx_unlock(&sc->reg_lock);
9759 if (rc)
9760 goto done;
9761
9762 p = buf;
9763 sbuf_printf(sb, "Cntl ID DataBE Addr Data");
9764 for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9765 sbuf_printf(sb, "\n %02x %02x %04x %08x %08x%08x%08x%08x",
9766 (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9767 p[4], p[3], p[2], p[1], p[0]);
9768 }
9769
9770 sbuf_printf(sb, "\n\nCntl ID Data");
9771 for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9772 sbuf_printf(sb, "\n %02x %02x %08x%08x%08x%08x",
9773 (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9774 }
9775
9776 rc = sbuf_finish(sb);
9777 done:
9778 sbuf_delete(sb);
9779 free(buf, M_CXGBE);
9780 return (rc);
9781 }
9782
9783 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9784 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9785 {
9786 struct adapter *sc = arg1;
9787 struct sbuf *sb;
9788 int rc, i;
9789 uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9790 uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9791 uint16_t thres[CIM_NUM_IBQ];
9792 uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9793 uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9794 u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9795 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
9796 "TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI", /* ibq's */
9797 "ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI", /* obq's */
9798 "SGE0-RX", "SGE1-RX" /* additional obq's (T5 onwards) */
9799 };
9800
9801 MPASS(chip_id(sc) < CHELSIO_T7);
9802
9803 cim_num_obq = sc->chip_params->cim_num_obq;
9804 if (is_t4(sc)) {
9805 ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9806 obq_rdaddr = A_UP_OBQ_0_REALADDR;
9807 } else {
9808 ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9809 obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9810 }
9811 nq = CIM_NUM_IBQ + cim_num_obq;
9812
9813 mtx_lock(&sc->reg_lock);
9814 if (hw_off_limits(sc))
9815 rc = ENXIO;
9816 else {
9817 rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9818 if (rc == 0) {
9819 rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9820 obq_wr);
9821 if (rc == 0)
9822 t4_read_cimq_cfg(sc, base, size, thres);
9823 }
9824 }
9825 mtx_unlock(&sc->reg_lock);
9826 if (rc)
9827 return (rc);
9828
9829 sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9830 if (sb == NULL)
9831 return (ENOMEM);
9832
9833 sbuf_printf(sb,
9834 " Queue Base Size Thres RdPtr WrPtr SOP EOP Avail");
9835
9836 for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9837 sbuf_printf(sb, "\n%7s %5x %5u %5u %6x %4x %4u %4u %5u",
9838 qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9839 G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9840 G_QUEREMFLITS(p[2]) * 16);
9841 for ( ; i < nq; i++, p += 4, wr += 2)
9842 sbuf_printf(sb, "\n%7s %5x %5u %12x %4x %4u %4u %5u", qname[i],
9843 base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9844 wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9845 G_QUEREMFLITS(p[2]) * 16);
9846
9847 rc = sbuf_finish(sb);
9848 sbuf_delete(sb);
9849
9850 return (rc);
9851 }
9852
9853 static int
sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS)9854 sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS)
9855 {
9856 struct adapter *sc = arg1;
9857 u_int coreid = arg2;
9858 struct sbuf *sb;
9859 int rc, i;
9860 u_int addr;
9861 uint16_t base[CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7];
9862 uint16_t size[CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7];
9863 uint16_t thres[CIM_NUM_IBQ_T7];
9864 uint32_t obq_wr[2 * CIM_NUM_OBQ_T7], *wr = obq_wr;
9865 uint32_t stat[4 * (CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7)], *p = stat;
9866 static const char * const qname_ibq_t7[] = {
9867 "TP0", "TP1", "TP2", "TP3", "ULP", "SGE0", "SGE1", "NC-SI",
9868 "RSVD", "IPC1", "IPC2", "IPC3", "IPC4", "IPC5", "IPC6", "IPC7",
9869 };
9870 static const char * const qname_obq_t7[] = {
9871 "ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI", "SGE0-RX",
9872 "RSVD", "RSVD", "IPC1", "IPC2", "IPC3", "IPC4", "IPC5",
9873 "IPC6", "IPC7"
9874 };
9875 static const char * const qname_ibq_sec_t7[] = {
9876 "TP0", "TP1", "TP2", "TP3", "ULP", "SGE0", "RSVD", "RSVD",
9877 "RSVD", "IPC0", "RSVD", "RSVD", "RSVD", "RSVD", "RSVD", "RSVD",
9878 };
9879 static const char * const qname_obq_sec_t7[] = {
9880 "ULP0", "ULP1", "ULP2", "ULP3", "SGE", "RSVD", "SGE0-RX",
9881 "RSVD", "RSVD", "IPC0", "RSVD", "RSVD", "RSVD", "RSVD",
9882 "RSVD", "RSVD",
9883 };
9884
9885 MPASS(chip_id(sc) >= CHELSIO_T7);
9886
9887 mtx_lock(&sc->reg_lock);
9888 if (hw_off_limits(sc))
9889 rc = ENXIO;
9890 else {
9891 rc = -t4_cim_read_core(sc, 1, coreid,
9892 A_T7_UP_IBQ_0_SHADOW_RDADDR, 4 * CIM_NUM_IBQ_T7, stat);
9893 if (rc != 0)
9894 goto unlock;
9895
9896 rc = -t4_cim_read_core(sc, 1, coreid,
9897 A_T7_UP_OBQ_0_SHADOW_RDADDR, 4 * CIM_NUM_OBQ_T7,
9898 &stat[4 * CIM_NUM_IBQ_T7]);
9899 if (rc != 0)
9900 goto unlock;
9901
9902 addr = A_T7_UP_OBQ_0_SHADOW_REALADDR;
9903 for (i = 0; i < CIM_NUM_OBQ_T7 * 2; i++, addr += 8) {
9904 rc = -t4_cim_read_core(sc, 1, coreid, addr, 1,
9905 &obq_wr[i]);
9906 if (rc != 0)
9907 goto unlock;
9908 }
9909 t4_read_cimq_cfg_core(sc, coreid, base, size, thres);
9910 }
9911 unlock:
9912 mtx_unlock(&sc->reg_lock);
9913 if (rc)
9914 return (rc);
9915
9916 sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9917 if (sb == NULL)
9918 return (ENOMEM);
9919
9920 sbuf_printf(sb,
9921 " Queue Base Size Thres RdPtr WrPtr SOP EOP Avail");
9922
9923 for (i = 0; i < CIM_NUM_IBQ_T7; i++, p += 4) {
9924 if (!size[i])
9925 continue;
9926
9927 sbuf_printf(sb, "\n%7s %5x %5u %5u %6x %4x %4u %4u %5u",
9928 coreid == 0 ? qname_ibq_t7[i] : qname_ibq_sec_t7[i],
9929 base[i], size[i], thres[i], G_IBQRDADDR(p[0]) & 0xfff,
9930 G_IBQWRADDR(p[1]) & 0xfff, G_QUESOPCNT(p[3]),
9931 G_QUEEOPCNT(p[3]), G_T7_QUEREMFLITS(p[2]) * 16);
9932 }
9933
9934 for ( ; i < CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7; i++, p += 4, wr += 2) {
9935 if (!size[i])
9936 continue;
9937
9938 sbuf_printf(sb, "\n%7s %5x %5u %12x %4x %4u %4u %5u",
9939 coreid == 0 ? qname_obq_t7[i - CIM_NUM_IBQ_T7] :
9940 qname_obq_sec_t7[i - CIM_NUM_IBQ_T7],
9941 base[i], size[i], G_QUERDADDR(p[0]) & 0xfff,
9942 wr[0] << 1, G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9943 G_T7_QUEREMFLITS(p[2]) * 16);
9944 }
9945
9946 rc = sbuf_finish(sb);
9947 sbuf_delete(sb);
9948 return (rc);
9949 }
9950
9951 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9952 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9953 {
9954 struct adapter *sc = arg1;
9955 struct sbuf *sb;
9956 int rc;
9957 struct tp_cpl_stats stats;
9958
9959 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9960 if (sb == NULL)
9961 return (ENOMEM);
9962
9963 rc = 0;
9964 mtx_lock(&sc->reg_lock);
9965 if (hw_off_limits(sc))
9966 rc = ENXIO;
9967 else
9968 t4_tp_get_cpl_stats(sc, &stats, 0);
9969 mtx_unlock(&sc->reg_lock);
9970 if (rc)
9971 goto done;
9972
9973 if (sc->chip_params->nchan > 2) {
9974 sbuf_printf(sb, " channel 0 channel 1"
9975 " channel 2 channel 3");
9976 sbuf_printf(sb, "\nCPL requests: %10u %10u %10u %10u",
9977 stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9978 sbuf_printf(sb, "\nCPL responses: %10u %10u %10u %10u",
9979 stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9980 } else {
9981 sbuf_printf(sb, " channel 0 channel 1");
9982 sbuf_printf(sb, "\nCPL requests: %10u %10u",
9983 stats.req[0], stats.req[1]);
9984 sbuf_printf(sb, "\nCPL responses: %10u %10u",
9985 stats.rsp[0], stats.rsp[1]);
9986 }
9987
9988 rc = sbuf_finish(sb);
9989 done:
9990 sbuf_delete(sb);
9991 return (rc);
9992 }
9993
9994 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9995 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9996 {
9997 struct adapter *sc = arg1;
9998 struct sbuf *sb;
9999 int rc;
10000 struct tp_usm_stats stats;
10001
10002 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10003 if (sb == NULL)
10004 return (ENOMEM);
10005
10006 rc = 0;
10007 mtx_lock(&sc->reg_lock);
10008 if (hw_off_limits(sc))
10009 rc = ENXIO;
10010 else
10011 t4_get_usm_stats(sc, &stats, 1);
10012 mtx_unlock(&sc->reg_lock);
10013 if (rc == 0) {
10014 sbuf_printf(sb, "Frames: %u\n", stats.frames);
10015 sbuf_printf(sb, "Octets: %ju\n", stats.octets);
10016 sbuf_printf(sb, "Drops: %u", stats.drops);
10017 rc = sbuf_finish(sb);
10018 }
10019 sbuf_delete(sb);
10020
10021 return (rc);
10022 }
10023
10024 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)10025 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
10026 {
10027 struct adapter *sc = arg1;
10028 struct sbuf *sb;
10029 int rc;
10030 struct tp_tid_stats stats;
10031
10032 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10033 if (sb == NULL)
10034 return (ENOMEM);
10035
10036 rc = 0;
10037 mtx_lock(&sc->reg_lock);
10038 if (hw_off_limits(sc))
10039 rc = ENXIO;
10040 else
10041 t4_tp_get_tid_stats(sc, &stats, 1);
10042 mtx_unlock(&sc->reg_lock);
10043 if (rc == 0) {
10044 sbuf_printf(sb, "Delete: %u\n", stats.del);
10045 sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
10046 sbuf_printf(sb, "Active: %u\n", stats.act);
10047 sbuf_printf(sb, "Passive: %u", stats.pas);
10048 rc = sbuf_finish(sb);
10049 }
10050 sbuf_delete(sb);
10051
10052 return (rc);
10053 }
10054
10055 static const char * const devlog_level_strings[] = {
10056 [FW_DEVLOG_LEVEL_EMERG] = "EMERG",
10057 [FW_DEVLOG_LEVEL_CRIT] = "CRIT",
10058 [FW_DEVLOG_LEVEL_ERR] = "ERR",
10059 [FW_DEVLOG_LEVEL_NOTICE] = "NOTICE",
10060 [FW_DEVLOG_LEVEL_INFO] = "INFO",
10061 [FW_DEVLOG_LEVEL_DEBUG] = "DEBUG"
10062 };
10063
10064 static const char * const devlog_facility_strings[] = {
10065 [FW_DEVLOG_FACILITY_CORE] = "CORE",
10066 [FW_DEVLOG_FACILITY_CF] = "CF",
10067 [FW_DEVLOG_FACILITY_SCHED] = "SCHED",
10068 [FW_DEVLOG_FACILITY_TIMER] = "TIMER",
10069 [FW_DEVLOG_FACILITY_RES] = "RES",
10070 [FW_DEVLOG_FACILITY_HW] = "HW",
10071 [FW_DEVLOG_FACILITY_FLR] = "FLR",
10072 [FW_DEVLOG_FACILITY_DMAQ] = "DMAQ",
10073 [FW_DEVLOG_FACILITY_PHY] = "PHY",
10074 [FW_DEVLOG_FACILITY_MAC] = "MAC",
10075 [FW_DEVLOG_FACILITY_PORT] = "PORT",
10076 [FW_DEVLOG_FACILITY_VI] = "VI",
10077 [FW_DEVLOG_FACILITY_FILTER] = "FILTER",
10078 [FW_DEVLOG_FACILITY_ACL] = "ACL",
10079 [FW_DEVLOG_FACILITY_TM] = "TM",
10080 [FW_DEVLOG_FACILITY_QFC] = "QFC",
10081 [FW_DEVLOG_FACILITY_DCB] = "DCB",
10082 [FW_DEVLOG_FACILITY_ETH] = "ETH",
10083 [FW_DEVLOG_FACILITY_OFLD] = "OFLD",
10084 [FW_DEVLOG_FACILITY_RI] = "RI",
10085 [FW_DEVLOG_FACILITY_ISCSI] = "ISCSI",
10086 [FW_DEVLOG_FACILITY_FCOE] = "FCOE",
10087 [FW_DEVLOG_FACILITY_FOISCSI] = "FOISCSI",
10088 [FW_DEVLOG_FACILITY_FOFCOE] = "FOFCOE",
10089 [FW_DEVLOG_FACILITY_CHNET] = "CHNET",
10090 };
10091
10092 static int
sbuf_devlog(struct adapter * sc,int coreid,struct sbuf * sb,int flags)10093 sbuf_devlog(struct adapter *sc, int coreid, struct sbuf *sb, int flags)
10094 {
10095 int i, j, rc, nentries, first = 0;
10096 struct devlog_params *dparams = &sc->params.devlog;
10097 struct fw_devlog_e *buf, *e;
10098 uint32_t addr, size;
10099 uint64_t ftstamp = UINT64_MAX;
10100
10101 KASSERT(coreid >= 0 && coreid < sc->params.ncores,
10102 ("%s: bad coreid %d\n", __func__, coreid));
10103
10104 if (dparams->addr == 0)
10105 return (ENXIO);
10106
10107 size = dparams->size / sc->params.ncores;
10108 addr = dparams->addr + coreid * size;
10109
10110 MPASS(flags == M_WAITOK || flags == M_NOWAIT);
10111 buf = malloc(size, M_CXGBE, M_ZERO | flags);
10112 if (buf == NULL)
10113 return (ENOMEM);
10114
10115 mtx_lock(&sc->reg_lock);
10116 if (hw_off_limits(sc))
10117 rc = ENXIO;
10118 else
10119 rc = read_via_memwin(sc, 1, addr, (void *)buf, size);
10120 mtx_unlock(&sc->reg_lock);
10121 if (rc != 0)
10122 goto done;
10123
10124 nentries = size / sizeof(struct fw_devlog_e);
10125 for (i = 0; i < nentries; i++) {
10126 e = &buf[i];
10127
10128 if (e->timestamp == 0)
10129 break; /* end */
10130
10131 e->timestamp = be64toh(e->timestamp);
10132 e->seqno = be32toh(e->seqno);
10133 for (j = 0; j < 8; j++)
10134 e->params[j] = be32toh(e->params[j]);
10135
10136 if (e->timestamp < ftstamp) {
10137 ftstamp = e->timestamp;
10138 first = i;
10139 }
10140 }
10141
10142 if (buf[first].timestamp == 0)
10143 goto done; /* nothing in the log */
10144
10145 sbuf_printf(sb, "%10s %15s %8s %8s %s\n",
10146 "Seq#", "Tstamp", "Level", "Facility", "Message");
10147
10148 i = first;
10149 do {
10150 e = &buf[i];
10151 if (e->timestamp == 0)
10152 break; /* end */
10153
10154 sbuf_printf(sb, "%10d %15ju %8s %8s ",
10155 e->seqno, e->timestamp,
10156 (e->level < nitems(devlog_level_strings) ?
10157 devlog_level_strings[e->level] : "UNKNOWN"),
10158 (e->facility < nitems(devlog_facility_strings) ?
10159 devlog_facility_strings[e->facility] : "UNKNOWN"));
10160 sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
10161 e->params[2], e->params[3], e->params[4],
10162 e->params[5], e->params[6], e->params[7]);
10163
10164 if (++i == nentries)
10165 i = 0;
10166 } while (i != first);
10167 done:
10168 free(buf, M_CXGBE);
10169 return (rc);
10170 }
10171
10172 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)10173 sysctl_devlog(SYSCTL_HANDLER_ARGS)
10174 {
10175 struct adapter *sc = arg1;
10176 int rc, i, coreid = arg2;
10177 struct sbuf *sb;
10178
10179 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10180 if (sb == NULL)
10181 return (ENOMEM);
10182 if (coreid == -1) {
10183 /* -1 means all cores */
10184 for (i = rc = 0; i < sc->params.ncores && rc == 0; i++) {
10185 if (sc->params.ncores > 0)
10186 sbuf_printf(sb, "=== CIM core %u ===\n", i);
10187 rc = sbuf_devlog(sc, i, sb, M_WAITOK);
10188 }
10189 } else {
10190 KASSERT(coreid >= 0 && coreid < sc->params.ncores,
10191 ("%s: bad coreid %d\n", __func__, coreid));
10192 rc = sbuf_devlog(sc, coreid, sb, M_WAITOK);
10193 }
10194 if (rc == 0)
10195 rc = sbuf_finish(sb);
10196 sbuf_delete(sb);
10197 return (rc);
10198 }
10199
10200 static void
dump_devlog(struct adapter * sc)10201 dump_devlog(struct adapter *sc)
10202 {
10203 int rc, i;
10204 struct sbuf sb;
10205
10206 if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
10207 log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
10208 device_get_nameunit(sc->dev));
10209 return;
10210 }
10211 for (i = rc = 0; i < sc->params.ncores && rc == 0; i++) {
10212 if (sc->params.ncores > 0)
10213 sbuf_printf(&sb, "=== CIM core %u ===\n", i);
10214 rc = sbuf_devlog(sc, i, &sb, M_WAITOK);
10215 }
10216 if (rc == 0) {
10217 sbuf_finish(&sb);
10218 log(LOG_DEBUG, "%s: device log follows.\n%s",
10219 device_get_nameunit(sc->dev), sbuf_data(&sb));
10220 }
10221 sbuf_delete(&sb);
10222 }
10223
10224 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)10225 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
10226 {
10227 struct adapter *sc = arg1;
10228 struct sbuf *sb;
10229 int rc;
10230 struct tp_fcoe_stats stats[MAX_NCHAN];
10231 int i, nchan = sc->chip_params->nchan;
10232
10233 rc = 0;
10234 mtx_lock(&sc->reg_lock);
10235 if (hw_off_limits(sc))
10236 rc = ENXIO;
10237 else {
10238 for (i = 0; i < nchan; i++)
10239 t4_get_fcoe_stats(sc, i, &stats[i], 1);
10240 }
10241 mtx_unlock(&sc->reg_lock);
10242 if (rc != 0)
10243 return (rc);
10244
10245 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10246 if (sb == NULL)
10247 return (ENOMEM);
10248
10249 if (nchan > 2) {
10250 sbuf_printf(sb, " channel 0 channel 1"
10251 " channel 2 channel 3");
10252 sbuf_printf(sb, "\noctetsDDP: %16ju %16ju %16ju %16ju",
10253 stats[0].octets_ddp, stats[1].octets_ddp,
10254 stats[2].octets_ddp, stats[3].octets_ddp);
10255 sbuf_printf(sb, "\nframesDDP: %16u %16u %16u %16u",
10256 stats[0].frames_ddp, stats[1].frames_ddp,
10257 stats[2].frames_ddp, stats[3].frames_ddp);
10258 sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
10259 stats[0].frames_drop, stats[1].frames_drop,
10260 stats[2].frames_drop, stats[3].frames_drop);
10261 } else {
10262 sbuf_printf(sb, " channel 0 channel 1");
10263 sbuf_printf(sb, "\noctetsDDP: %16ju %16ju",
10264 stats[0].octets_ddp, stats[1].octets_ddp);
10265 sbuf_printf(sb, "\nframesDDP: %16u %16u",
10266 stats[0].frames_ddp, stats[1].frames_ddp);
10267 sbuf_printf(sb, "\nframesDrop: %16u %16u",
10268 stats[0].frames_drop, stats[1].frames_drop);
10269 }
10270
10271 rc = sbuf_finish(sb);
10272 sbuf_delete(sb);
10273
10274 return (rc);
10275 }
10276
10277 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)10278 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
10279 {
10280 struct adapter *sc = arg1;
10281 struct sbuf *sb;
10282 int rc, i;
10283 unsigned int map, kbps, ipg, mode;
10284 unsigned int pace_tab[NTX_SCHED];
10285
10286 sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
10287 if (sb == NULL)
10288 return (ENOMEM);
10289
10290 mtx_lock(&sc->reg_lock);
10291 if (hw_off_limits(sc)) {
10292 mtx_unlock(&sc->reg_lock);
10293 rc = ENXIO;
10294 goto done;
10295 }
10296
10297 map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
10298 mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
10299 t4_read_pace_tbl(sc, pace_tab);
10300 mtx_unlock(&sc->reg_lock);
10301
10302 sbuf_printf(sb, "Scheduler Mode Channel Rate (Kbps) "
10303 "Class IPG (0.1 ns) Flow IPG (us)");
10304
10305 for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
10306 t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
10307 sbuf_printf(sb, "\n %u %-5s %u ", i,
10308 (mode & (1 << i)) ? "flow" : "class", map & 3);
10309 if (kbps)
10310 sbuf_printf(sb, "%9u ", kbps);
10311 else
10312 sbuf_printf(sb, " disabled ");
10313
10314 if (ipg)
10315 sbuf_printf(sb, "%13u ", ipg);
10316 else
10317 sbuf_printf(sb, " disabled ");
10318
10319 if (pace_tab[i])
10320 sbuf_printf(sb, "%10u", pace_tab[i]);
10321 else
10322 sbuf_printf(sb, " disabled");
10323 }
10324 rc = sbuf_finish(sb);
10325 done:
10326 sbuf_delete(sb);
10327 return (rc);
10328 }
10329
10330 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)10331 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
10332 {
10333 struct adapter *sc = arg1;
10334 struct sbuf *sb;
10335 int rc, i, j;
10336 uint64_t *p0, *p1;
10337 struct lb_port_stats s[2];
10338 static const char *stat_name[] = {
10339 "OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
10340 "UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
10341 "Frames128To255:", "Frames256To511:", "Frames512To1023:",
10342 "Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
10343 "BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
10344 "BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
10345 "BG2FramesTrunc:", "BG3FramesTrunc:"
10346 };
10347
10348 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10349 if (sb == NULL)
10350 return (ENOMEM);
10351
10352 memset(s, 0, sizeof(s));
10353
10354 rc = 0;
10355 for (i = 0; i < sc->chip_params->nchan; i += 2) {
10356 mtx_lock(&sc->reg_lock);
10357 if (hw_off_limits(sc))
10358 rc = ENXIO;
10359 else {
10360 t4_get_lb_stats(sc, i, &s[0]);
10361 t4_get_lb_stats(sc, i + 1, &s[1]);
10362 }
10363 mtx_unlock(&sc->reg_lock);
10364 if (rc != 0)
10365 break;
10366
10367 p0 = &s[0].octets;
10368 p1 = &s[1].octets;
10369 sbuf_printf(sb, "%s Loopback %u"
10370 " Loopback %u", i == 0 ? "" : "\n", i, i + 1);
10371
10372 for (j = 0; j < nitems(stat_name); j++)
10373 sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
10374 *p0++, *p1++);
10375 }
10376
10377 if (rc == 0)
10378 rc = sbuf_finish(sb);
10379 sbuf_delete(sb);
10380
10381 return (rc);
10382 }
10383
10384 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)10385 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
10386 {
10387 int rc = 0;
10388 struct port_info *pi = arg1;
10389 struct link_config *lc = &pi->link_cfg;
10390 struct sbuf *sb;
10391
10392 sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
10393 if (sb == NULL)
10394 return (ENOMEM);
10395
10396 if (lc->link_ok || lc->link_down_rc == 255)
10397 sbuf_printf(sb, "n/a");
10398 else
10399 sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
10400
10401 rc = sbuf_finish(sb);
10402 sbuf_delete(sb);
10403
10404 return (rc);
10405 }
10406
10407 struct mem_desc {
10408 uint64_t base;
10409 uint64_t limit;
10410 u_int idx;
10411 };
10412
10413 static int
mem_desc_cmp(const void * a,const void * b)10414 mem_desc_cmp(const void *a, const void *b)
10415 {
10416 const uint64_t v1 = ((const struct mem_desc *)a)->base;
10417 const uint64_t v2 = ((const struct mem_desc *)b)->base;
10418
10419 if (v1 < v2)
10420 return (-1);
10421 else if (v1 > v2)
10422 return (1);
10423
10424 return (0);
10425 }
10426
10427 static void
mem_region_show(struct sbuf * sb,const char * name,uint64_t from,uint64_t to)10428 mem_region_show(struct sbuf *sb, const char *name, uint64_t from, uint64_t to)
10429 {
10430 uintmax_t size;
10431
10432 if (from == to)
10433 return;
10434
10435 size = to - from + 1;
10436 if (size == 0)
10437 return;
10438
10439 if (from > UINT32_MAX || to > UINT32_MAX)
10440 sbuf_printf(sb, "%-18s 0x%012jx-0x%012jx [%ju]\n", name,
10441 (uintmax_t)from, (uintmax_t)to, size);
10442 else
10443 sbuf_printf(sb, "%-18s 0x%08jx-0x%08jx [%ju]\n", name,
10444 (uintmax_t)from, (uintmax_t)to, size);
10445 }
10446
10447 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)10448 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
10449 {
10450 struct adapter *sc = arg1;
10451 struct sbuf *sb;
10452 int rc, i, n, nchan;
10453 uint32_t lo, hi, used, free, alloc;
10454 static const char *memory[] = {
10455 "EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
10456 };
10457 static const char *region[] = {
10458 "DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
10459 "Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
10460 "Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
10461 "TDDP region:", "TPT region:", "STAG region:", "RQ region:",
10462 "RQUDP region:", "PBL region:", "TXPBL region:",
10463 "TLSKey region:", "RRQ region:", "NVMe STAG region:",
10464 "NVMe RQ region:", "NVMe RXPBL region:", "NVMe TPT region:",
10465 "NVMe TXPBL region:", "DBVFIFO region:", "ULPRX state:",
10466 "ULPTX state:", "RoCE RRQ region:", "On-chip queues:",
10467 };
10468 struct mem_desc avail[4];
10469 struct mem_desc mem[nitems(region) + 3]; /* up to 3 holes */
10470 struct mem_desc *md;
10471
10472 rc = sysctl_wire_old_buffer(req, 0);
10473 if (rc != 0)
10474 return (rc);
10475
10476 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10477 if (sb == NULL)
10478 return (ENOMEM);
10479
10480 for (i = 0; i < nitems(mem); i++) {
10481 mem[i].limit = 0;
10482 mem[i].idx = i;
10483 }
10484
10485 mtx_lock(&sc->reg_lock);
10486 if (hw_off_limits(sc)) {
10487 rc = ENXIO;
10488 goto done;
10489 }
10490
10491 /* Find and sort the populated memory ranges */
10492 i = 0;
10493 lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
10494 if (lo & F_EDRAM0_ENABLE) {
10495 hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
10496 if (chip_id(sc) >= CHELSIO_T7) {
10497 avail[i].base = (uint64_t)G_T7_EDRAM0_BASE(hi) << 20;
10498 avail[i].limit = avail[i].base +
10499 (G_T7_EDRAM0_SIZE(hi) << 20);
10500 } else {
10501 avail[i].base = (uint64_t)G_EDRAM0_BASE(hi) << 20;
10502 avail[i].limit = avail[i].base +
10503 (G_EDRAM0_SIZE(hi) << 20);
10504 }
10505 avail[i].idx = 0;
10506 i++;
10507 }
10508 if (lo & F_EDRAM1_ENABLE) {
10509 hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
10510 if (chip_id(sc) >= CHELSIO_T7) {
10511 avail[i].base = (uint64_t)G_T7_EDRAM1_BASE(hi) << 20;
10512 avail[i].limit = avail[i].base +
10513 (G_T7_EDRAM1_SIZE(hi) << 20);
10514 } else {
10515 avail[i].base = (uint64_t)G_EDRAM1_BASE(hi) << 20;
10516 avail[i].limit = avail[i].base +
10517 (G_EDRAM1_SIZE(hi) << 20);
10518 }
10519 avail[i].idx = 1;
10520 i++;
10521 }
10522 if (lo & F_EXT_MEM_ENABLE) {
10523 switch (chip_id(sc)) {
10524 case CHELSIO_T4:
10525 case CHELSIO_T6:
10526 hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
10527 avail[i].base = (uint64_t)G_EXT_MEM_BASE(hi) << 20;
10528 avail[i].limit = avail[i].base +
10529 (G_EXT_MEM_SIZE(hi) << 20);
10530 avail[i].idx = 2;
10531 break;
10532 case CHELSIO_T5:
10533 hi = t4_read_reg(sc, A_MA_EXT_MEMORY0_BAR);
10534 avail[i].base = (uint64_t)G_EXT_MEM0_BASE(hi) << 20;
10535 avail[i].limit = avail[i].base +
10536 (G_EXT_MEM0_SIZE(hi) << 20);
10537 avail[i].idx = 3; /* Call it MC0 for T5 */
10538 break;
10539 default:
10540 hi = t4_read_reg(sc, A_MA_EXT_MEMORY0_BAR);
10541 avail[i].base = (uint64_t)G_T7_EXT_MEM0_BASE(hi) << 20;
10542 avail[i].limit = avail[i].base +
10543 (G_T7_EXT_MEM0_SIZE(hi) << 20);
10544 avail[i].idx = 3; /* Call it MC0 for T7+ */
10545 break;
10546 }
10547 i++;
10548 }
10549 if (lo & F_EXT_MEM1_ENABLE && !(lo & F_MC_SPLIT)) {
10550 /* Only T5 and T7+ have 2 MCs. */
10551 MPASS(is_t5(sc) || chip_id(sc) >= CHELSIO_T7);
10552
10553 hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10554 if (chip_id(sc) >= CHELSIO_T7) {
10555 avail[i].base = (uint64_t)G_T7_EXT_MEM1_BASE(hi) << 20;
10556 avail[i].limit = avail[i].base +
10557 (G_T7_EXT_MEM1_SIZE(hi) << 20);
10558 } else {
10559 avail[i].base = (uint64_t)G_EXT_MEM1_BASE(hi) << 20;
10560 avail[i].limit = avail[i].base +
10561 (G_EXT_MEM1_SIZE(hi) << 20);
10562 }
10563 avail[i].idx = 4;
10564 i++;
10565 }
10566 if (lo & F_HMA_MUX) {
10567 /* Only T6+ have HMA. */
10568 MPASS(chip_id(sc) >= CHELSIO_T6);
10569
10570 if (chip_id(sc) >= CHELSIO_T7) {
10571 hi = t4_read_reg(sc, A_MA_HOST_MEMORY_BAR);
10572 avail[i].base = (uint64_t)G_HMATARGETBASE(hi) << 20;
10573 avail[i].limit = avail[i].base +
10574 (G_T7_HMA_SIZE(hi) << 20);
10575 } else {
10576 hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10577 avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
10578 avail[i].limit = avail[i].base +
10579 (G_EXT_MEM1_SIZE(hi) << 20);
10580 }
10581 avail[i].idx = 5;
10582 i++;
10583 }
10584 MPASS(i <= nitems(avail));
10585 if (!i) /* no memory available */
10586 goto done;
10587 qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
10588
10589 md = &mem[0];
10590 (md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
10591 (md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
10592 (md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
10593 (md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
10594 (md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
10595 (md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
10596 (md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
10597 (md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
10598 (md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
10599
10600 /* the next few have explicit upper bounds */
10601 md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
10602 md->limit = md->base - 1 +
10603 t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
10604 G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
10605 md++;
10606
10607 md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
10608 md->limit = md->base - 1 +
10609 t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
10610 G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
10611 md++;
10612
10613 if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10614 if (chip_id(sc) <= CHELSIO_T5)
10615 md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
10616 else
10617 md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
10618 md->limit = 0;
10619 } else {
10620 md->base = 0;
10621 md->idx = nitems(region); /* hide it */
10622 }
10623 md++;
10624
10625 #define ulp_region(reg) do {\
10626 const u_int shift = chip_id(sc) >= CHELSIO_T7 ? 4 : 0; \
10627 md->base = (uint64_t)t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT) << shift; \
10628 md->limit = (uint64_t)t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT) << shift; \
10629 md->limit += (1 << shift) - 1; \
10630 md++; \
10631 } while (0)
10632
10633 #define hide_ulp_region() do { \
10634 md->base = 0; \
10635 md->idx = nitems(region); \
10636 md++; \
10637 } while (0)
10638
10639 ulp_region(RX_ISCSI);
10640 ulp_region(RX_TDDP);
10641 ulp_region(TX_TPT);
10642 ulp_region(RX_STAG);
10643 ulp_region(RX_RQ);
10644 if (chip_id(sc) < CHELSIO_T7)
10645 ulp_region(RX_RQUDP);
10646 else
10647 hide_ulp_region();
10648 ulp_region(RX_PBL);
10649 ulp_region(TX_PBL);
10650 if (chip_id(sc) >= CHELSIO_T6)
10651 ulp_region(RX_TLS_KEY);
10652 else
10653 hide_ulp_region();
10654 if (chip_id(sc) >= CHELSIO_T7) {
10655 ulp_region(RX_RRQ);
10656 ulp_region(RX_NVME_TCP_STAG);
10657 ulp_region(RX_NVME_TCP_RQ);
10658 ulp_region(RX_NVME_TCP_PBL);
10659 ulp_region(TX_NVME_TCP_TPT);
10660 ulp_region(TX_NVME_TCP_PBL);
10661 } else {
10662 hide_ulp_region();
10663 hide_ulp_region();
10664 hide_ulp_region();
10665 hide_ulp_region();
10666 hide_ulp_region();
10667 hide_ulp_region();
10668 }
10669 #undef ulp_region
10670 #undef hide_ulp_region
10671
10672 md->base = 0;
10673 if (is_t4(sc))
10674 md->idx = nitems(region);
10675 else {
10676 uint32_t size = 0;
10677 uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
10678 uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
10679
10680 if (is_t5(sc)) {
10681 if (sge_ctrl & F_VFIFO_ENABLE)
10682 size = fifo_size << 2;
10683 } else
10684 size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
10685
10686 if (size) {
10687 md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
10688 md->limit = md->base + size - 1;
10689 } else
10690 md->idx = nitems(region);
10691 }
10692 md++;
10693
10694 md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
10695 md->limit = 0;
10696 md++;
10697 md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
10698 md->limit = 0;
10699 md++;
10700
10701 if (chip_id(sc) >= CHELSIO_T7) {
10702 t4_tp_pio_read(sc, &lo, 1, A_TP_ROCE_RRQ_BASE, false);
10703 md->base = lo;
10704 } else {
10705 md->base = 0;
10706 md->idx = nitems(region);
10707 }
10708 md++;
10709
10710 md->base = sc->vres.ocq.start;
10711 if (sc->vres.ocq.size)
10712 md->limit = md->base + sc->vres.ocq.size - 1;
10713 else
10714 md->idx = nitems(region); /* hide it */
10715 md++;
10716
10717 /* add any address-space holes, there can be up to 3 */
10718 for (n = 0; n < i - 1; n++)
10719 if (avail[n].limit < avail[n + 1].base)
10720 (md++)->base = avail[n].limit;
10721 if (avail[n].limit)
10722 (md++)->base = avail[n].limit;
10723
10724 n = md - mem;
10725 MPASS(n <= nitems(mem));
10726 qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10727
10728 for (lo = 0; lo < i; lo++)
10729 mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10730 avail[lo].limit - 1);
10731
10732 sbuf_printf(sb, "\n");
10733 for (i = 0; i < n; i++) {
10734 if (mem[i].idx >= nitems(region))
10735 continue; /* skip holes */
10736 if (!mem[i].limit)
10737 mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10738 mem_region_show(sb, region[mem[i].idx], mem[i].base,
10739 mem[i].limit);
10740 }
10741
10742 lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10743 hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10744 if (hi != lo - 1) {
10745 sbuf_printf(sb, "\n");
10746 mem_region_show(sb, "uP RAM:", lo, hi);
10747 }
10748
10749 lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10750 hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10751 if (hi != lo - 1)
10752 mem_region_show(sb, "uP Extmem2:", lo, hi);
10753
10754 lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10755 if (chip_id(sc) >= CHELSIO_T7)
10756 nchan = 1 << G_T7_PMRXNUMCHN(lo);
10757 else
10758 nchan = lo & F_PMRXNUMCHN ? 2 : 1;
10759 for (i = 0, free = 0; i < nchan; i++)
10760 free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10761 sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10762 G_PMRXMAXPAGE(lo), free,
10763 t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10, nchan);
10764
10765 lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10766 hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10767 if (chip_id(sc) >= CHELSIO_T7)
10768 nchan = 1 << G_T7_PMTXNUMCHN(lo);
10769 else
10770 nchan = 1 << G_PMTXNUMCHN(lo);
10771 for (i = 0, free = 0; i < nchan; i++)
10772 free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10773 sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10774 G_PMTXMAXPAGE(lo), free,
10775 hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10776 hi >= (1 << 20) ? 'M' : 'K', nchan);
10777 sbuf_printf(sb, "%u p-structs (%u free)\n",
10778 t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10779 G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10780
10781 for (i = 0; i < 4; i++) {
10782 if (chip_id(sc) > CHELSIO_T5)
10783 lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10784 else
10785 lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10786 if (is_t5(sc)) {
10787 used = G_T5_USED(lo);
10788 alloc = G_T5_ALLOC(lo);
10789 } else {
10790 used = G_USED(lo);
10791 alloc = G_ALLOC(lo);
10792 }
10793 /* For T6+ these are MAC buffer groups */
10794 sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10795 i, used, alloc);
10796 }
10797 for (i = 0; i < sc->chip_params->nchan; i++) {
10798 if (chip_id(sc) > CHELSIO_T5)
10799 lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10800 else
10801 lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10802 if (is_t5(sc)) {
10803 used = G_T5_USED(lo);
10804 alloc = G_T5_ALLOC(lo);
10805 } else {
10806 used = G_USED(lo);
10807 alloc = G_ALLOC(lo);
10808 }
10809 /* For T6+ these are MAC buffer groups */
10810 sbuf_printf(sb,
10811 "\nLoopback %d using %u pages out of %u allocated",
10812 i, used, alloc);
10813 }
10814 done:
10815 mtx_unlock(&sc->reg_lock);
10816 if (rc == 0)
10817 rc = sbuf_finish(sb);
10818 sbuf_delete(sb);
10819 return (rc);
10820 }
10821
10822 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10823 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10824 {
10825 *mask = x | y;
10826 y = htobe64(y);
10827 memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10828 }
10829
10830 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10831 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10832 {
10833 struct adapter *sc = arg1;
10834 struct sbuf *sb;
10835 int rc, i;
10836
10837 MPASS(chip_id(sc) <= CHELSIO_T5);
10838
10839 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10840 if (sb == NULL)
10841 return (ENOMEM);
10842
10843 sbuf_printf(sb,
10844 "Idx Ethernet address Mask Vld Ports PF"
10845 " VF Replication P0 P1 P2 P3 ML");
10846 rc = 0;
10847 for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10848 uint64_t tcamx, tcamy, mask;
10849 uint32_t cls_lo, cls_hi;
10850 uint8_t addr[ETHER_ADDR_LEN];
10851
10852 mtx_lock(&sc->reg_lock);
10853 if (hw_off_limits(sc))
10854 rc = ENXIO;
10855 else {
10856 tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10857 tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10858 }
10859 mtx_unlock(&sc->reg_lock);
10860 if (rc != 0)
10861 break;
10862 if (tcamx & tcamy)
10863 continue;
10864 tcamxy2valmask(tcamx, tcamy, addr, &mask);
10865 mtx_lock(&sc->reg_lock);
10866 if (hw_off_limits(sc))
10867 rc = ENXIO;
10868 else {
10869 cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10870 cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10871 }
10872 mtx_unlock(&sc->reg_lock);
10873 if (rc != 0)
10874 break;
10875 sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10876 " %c %#x%4u%4d", i, addr[0], addr[1], addr[2],
10877 addr[3], addr[4], addr[5], (uintmax_t)mask,
10878 (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10879 G_PORTMAP(cls_hi), G_PF(cls_lo),
10880 (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10881
10882 if (cls_lo & F_REPLICATE) {
10883 struct fw_ldst_cmd ldst_cmd;
10884
10885 memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10886 ldst_cmd.op_to_addrspace =
10887 htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10888 F_FW_CMD_REQUEST | F_FW_CMD_READ |
10889 V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10890 ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10891 ldst_cmd.u.mps.rplc.fid_idx =
10892 htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10893 V_FW_LDST_CMD_IDX(i));
10894
10895 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10896 "t4mps");
10897 if (rc)
10898 break;
10899 if (hw_off_limits(sc))
10900 rc = ENXIO;
10901 else
10902 rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10903 sizeof(ldst_cmd), &ldst_cmd);
10904 end_synchronized_op(sc, 0);
10905 if (rc != 0)
10906 break;
10907 else {
10908 sbuf_printf(sb, " %08x %08x %08x %08x",
10909 be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10910 be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10911 be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10912 be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10913 }
10914 } else
10915 sbuf_printf(sb, "%36s", "");
10916
10917 sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10918 G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10919 G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10920 }
10921
10922 if (rc)
10923 (void) sbuf_finish(sb);
10924 else
10925 rc = sbuf_finish(sb);
10926 sbuf_delete(sb);
10927
10928 return (rc);
10929 }
10930
10931 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10932 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10933 {
10934 struct adapter *sc = arg1;
10935 struct sbuf *sb;
10936 int rc, i;
10937
10938 MPASS(chip_id(sc) == CHELSIO_T6);
10939
10940 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10941 if (sb == NULL)
10942 return (ENOMEM);
10943
10944 sbuf_printf(sb, "Idx Ethernet address Mask VNI Mask"
10945 " IVLAN Vld DIP_Hit Lookup Port Vld Ports PF VF"
10946 " Replication"
10947 " P0 P1 P2 P3 ML");
10948
10949 rc = 0;
10950 for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10951 uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10952 uint16_t ivlan;
10953 uint64_t tcamx, tcamy, val, mask;
10954 uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10955 uint8_t addr[ETHER_ADDR_LEN];
10956
10957 ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10958 if (i < 256)
10959 ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10960 else
10961 ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10962 mtx_lock(&sc->reg_lock);
10963 if (hw_off_limits(sc))
10964 rc = ENXIO;
10965 else {
10966 t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10967 val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10968 tcamy = G_DMACH(val) << 32;
10969 tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10970 data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10971 }
10972 mtx_unlock(&sc->reg_lock);
10973 if (rc != 0)
10974 break;
10975
10976 lookup_type = G_DATALKPTYPE(data2);
10977 port_num = G_DATAPORTNUM(data2);
10978 if (lookup_type && lookup_type != M_DATALKPTYPE) {
10979 /* Inner header VNI */
10980 vniy = ((data2 & F_DATAVIDH2) << 23) |
10981 (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10982 dip_hit = data2 & F_DATADIPHIT;
10983 vlan_vld = 0;
10984 } else {
10985 vniy = 0;
10986 dip_hit = 0;
10987 vlan_vld = data2 & F_DATAVIDH2;
10988 ivlan = G_VIDL(val);
10989 }
10990
10991 ctl |= V_CTLXYBITSEL(1);
10992 mtx_lock(&sc->reg_lock);
10993 if (hw_off_limits(sc))
10994 rc = ENXIO;
10995 else {
10996 t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10997 val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10998 tcamx = G_DMACH(val) << 32;
10999 tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
11000 data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
11001 }
11002 mtx_unlock(&sc->reg_lock);
11003 if (rc != 0)
11004 break;
11005
11006 if (lookup_type && lookup_type != M_DATALKPTYPE) {
11007 /* Inner header VNI mask */
11008 vnix = ((data2 & F_DATAVIDH2) << 23) |
11009 (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
11010 } else
11011 vnix = 0;
11012
11013 if (tcamx & tcamy)
11014 continue;
11015 tcamxy2valmask(tcamx, tcamy, addr, &mask);
11016
11017 mtx_lock(&sc->reg_lock);
11018 if (hw_off_limits(sc))
11019 rc = ENXIO;
11020 else {
11021 cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
11022 cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
11023 }
11024 mtx_unlock(&sc->reg_lock);
11025 if (rc != 0)
11026 break;
11027
11028 if (lookup_type && lookup_type != M_DATALKPTYPE) {
11029 sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11030 "%012jx %06x %06x - - %3c"
11031 " I %4x %3c %#x%4u%4d", i, addr[0],
11032 addr[1], addr[2], addr[3], addr[4], addr[5],
11033 (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
11034 port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11035 G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11036 cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11037 } else {
11038 sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11039 "%012jx - - ", i, addr[0], addr[1],
11040 addr[2], addr[3], addr[4], addr[5],
11041 (uintmax_t)mask);
11042
11043 if (vlan_vld)
11044 sbuf_printf(sb, "%4u Y ", ivlan);
11045 else
11046 sbuf_printf(sb, " - N ");
11047
11048 sbuf_printf(sb, "- %3c %4x %3c %#x%4u%4d",
11049 lookup_type ? 'I' : 'O', port_num,
11050 cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11051 G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11052 cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11053 }
11054
11055
11056 if (cls_lo & F_T6_REPLICATE) {
11057 struct fw_ldst_cmd ldst_cmd;
11058
11059 memset(&ldst_cmd, 0, sizeof(ldst_cmd));
11060 ldst_cmd.op_to_addrspace =
11061 htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
11062 F_FW_CMD_REQUEST | F_FW_CMD_READ |
11063 V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
11064 ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
11065 ldst_cmd.u.mps.rplc.fid_idx =
11066 htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
11067 V_FW_LDST_CMD_IDX(i));
11068
11069 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
11070 "t6mps");
11071 if (rc)
11072 break;
11073 if (hw_off_limits(sc))
11074 rc = ENXIO;
11075 else
11076 rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
11077 sizeof(ldst_cmd), &ldst_cmd);
11078 end_synchronized_op(sc, 0);
11079 if (rc != 0)
11080 break;
11081 else {
11082 sbuf_printf(sb, " %08x %08x %08x %08x"
11083 " %08x %08x %08x %08x",
11084 be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
11085 be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
11086 be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
11087 be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
11088 be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
11089 be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
11090 be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
11091 be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
11092 }
11093 } else
11094 sbuf_printf(sb, "%72s", "");
11095
11096 sbuf_printf(sb, "%4u%3u%3u%3u %#x",
11097 G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
11098 G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
11099 (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
11100 }
11101
11102 if (rc)
11103 (void) sbuf_finish(sb);
11104 else
11105 rc = sbuf_finish(sb);
11106 sbuf_delete(sb);
11107
11108 return (rc);
11109 }
11110
11111 static int
sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS)11112 sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS)
11113 {
11114 struct adapter *sc = arg1;
11115 struct sbuf *sb;
11116 int rc, i;
11117
11118 MPASS(chip_id(sc) >= CHELSIO_T7);
11119
11120 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11121 if (sb == NULL)
11122 return (ENOMEM);
11123
11124 sbuf_printf(sb, "Idx Ethernet address Mask VNI Mask"
11125 " IVLAN Vld DIP_Hit Lookup Port Vld Ports PF VF"
11126 " Replication"
11127 " P0 P1 P2 P3 ML");
11128
11129 rc = 0;
11130 for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
11131 uint8_t dip_hit, vlan_vld, lookup_type, port_num;
11132 uint16_t ivlan;
11133 uint64_t tcamx, tcamy, val, mask;
11134 uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
11135 uint8_t addr[ETHER_ADDR_LEN];
11136
11137 /* Read tcamy */
11138 ctl = (V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0));
11139 if (chip_rev(sc) == 0) {
11140 if (i < 256)
11141 ctl |= V_CTLTCAMINDEX(i) | V_T7_CTLTCAMSEL(0);
11142 else
11143 ctl |= V_CTLTCAMINDEX(i - 256) | V_T7_CTLTCAMSEL(1);
11144 } else {
11145 #if 0
11146 ctl = (V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0));
11147 #endif
11148 if (i < 512)
11149 ctl |= V_CTLTCAMINDEX(i) | V_T7_CTLTCAMSEL(0);
11150 else if (i < 1024)
11151 ctl |= V_CTLTCAMINDEX(i - 512) | V_T7_CTLTCAMSEL(1);
11152 else
11153 ctl |= V_CTLTCAMINDEX(i - 1024) | V_T7_CTLTCAMSEL(2);
11154 }
11155
11156 mtx_lock(&sc->reg_lock);
11157 if (hw_off_limits(sc))
11158 rc = ENXIO;
11159 else {
11160 t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
11161 val = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA1_REQ_ID1);
11162 tcamy = G_DMACH(val) << 32;
11163 tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA0_REQ_ID1);
11164 data2 = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA2_REQ_ID1);
11165 }
11166 mtx_unlock(&sc->reg_lock);
11167 if (rc != 0)
11168 break;
11169
11170 lookup_type = G_DATALKPTYPE(data2);
11171 port_num = G_DATAPORTNUM(data2);
11172 if (lookup_type && lookup_type != M_DATALKPTYPE) {
11173 /* Inner header VNI */
11174 vniy = (((data2 & F_DATAVIDH2) |
11175 G_DATAVIDH1(data2)) << 16) | G_VIDL(val);
11176 dip_hit = data2 & F_DATADIPHIT;
11177 vlan_vld = 0;
11178 } else {
11179 vniy = 0;
11180 dip_hit = 0;
11181 vlan_vld = data2 & F_DATAVIDH2;
11182 ivlan = G_VIDL(val);
11183 }
11184
11185 ctl |= V_CTLXYBITSEL(1);
11186 mtx_lock(&sc->reg_lock);
11187 if (hw_off_limits(sc))
11188 rc = ENXIO;
11189 else {
11190 t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
11191 val = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA1_REQ_ID1);
11192 tcamx = G_DMACH(val) << 32;
11193 tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA0_REQ_ID1);
11194 data2 = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA2_REQ_ID1);
11195 }
11196 mtx_unlock(&sc->reg_lock);
11197 if (rc != 0)
11198 break;
11199
11200 if (lookup_type && lookup_type != M_DATALKPTYPE) {
11201 /* Inner header VNI mask */
11202 vnix = (((data2 & F_DATAVIDH2) |
11203 G_DATAVIDH1(data2)) << 16) | G_VIDL(val);
11204 } else
11205 vnix = 0;
11206
11207 if (tcamx & tcamy)
11208 continue;
11209 tcamxy2valmask(tcamx, tcamy, addr, &mask);
11210
11211 mtx_lock(&sc->reg_lock);
11212 if (hw_off_limits(sc))
11213 rc = ENXIO;
11214 else {
11215 if (chip_rev(sc) == 0) {
11216 cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
11217 cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
11218 } else {
11219 t4_write_reg(sc, A_MPS_CLS_SRAM_H,
11220 V_SRAMWRN(0) | V_SRAMINDEX(i));
11221 cls_lo = t4_read_reg(sc, A_MPS_CLS_SRAM_L);
11222 cls_hi = t4_read_reg(sc, A_MPS_CLS_SRAM_H);
11223 }
11224 }
11225 mtx_unlock(&sc->reg_lock);
11226 if (rc != 0)
11227 break;
11228
11229 if (lookup_type && lookup_type != M_DATALKPTYPE) {
11230 sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11231 "%012jx %06x %06x - - %3c"
11232 " I %4x %3c %#x%4u%4d", i, addr[0],
11233 addr[1], addr[2], addr[3], addr[4], addr[5],
11234 (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
11235 port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11236 G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11237 cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11238 } else {
11239 sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11240 "%012jx - - ", i, addr[0], addr[1],
11241 addr[2], addr[3], addr[4], addr[5],
11242 (uintmax_t)mask);
11243
11244 if (vlan_vld)
11245 sbuf_printf(sb, "%4u Y ", ivlan);
11246 else
11247 sbuf_printf(sb, " - N ");
11248
11249 sbuf_printf(sb, "- %3c %4x %3c %#x%4u%4d",
11250 lookup_type ? 'I' : 'O', port_num,
11251 cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11252 G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11253 cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11254 }
11255
11256 if (cls_lo & F_T6_REPLICATE) {
11257 struct fw_ldst_cmd ldst_cmd;
11258
11259 memset(&ldst_cmd, 0, sizeof(ldst_cmd));
11260 ldst_cmd.op_to_addrspace =
11261 htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
11262 F_FW_CMD_REQUEST | F_FW_CMD_READ |
11263 V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
11264 ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
11265 ldst_cmd.u.mps.rplc.fid_idx =
11266 htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
11267 V_FW_LDST_CMD_IDX(i));
11268
11269 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
11270 "t6mps");
11271 if (rc)
11272 break;
11273 if (hw_off_limits(sc))
11274 rc = ENXIO;
11275 else
11276 rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
11277 sizeof(ldst_cmd), &ldst_cmd);
11278 end_synchronized_op(sc, 0);
11279 if (rc != 0)
11280 break;
11281 else {
11282 sbuf_printf(sb, " %08x %08x %08x %08x"
11283 " %08x %08x %08x %08x",
11284 be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
11285 be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
11286 be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
11287 be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
11288 be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
11289 be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
11290 be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
11291 be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
11292 }
11293 } else
11294 sbuf_printf(sb, "%72s", "");
11295
11296 sbuf_printf(sb, "%4u%3u%3u%3u %#x",
11297 G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
11298 G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
11299 (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
11300 }
11301
11302 if (rc)
11303 (void) sbuf_finish(sb);
11304 else
11305 rc = sbuf_finish(sb);
11306 sbuf_delete(sb);
11307
11308 return (rc);
11309 }
11310
11311 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)11312 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
11313 {
11314 struct adapter *sc = arg1;
11315 struct sbuf *sb;
11316 int rc;
11317 uint16_t mtus[NMTUS];
11318
11319 rc = 0;
11320 mtx_lock(&sc->reg_lock);
11321 if (hw_off_limits(sc))
11322 rc = ENXIO;
11323 else
11324 t4_read_mtu_tbl(sc, mtus, NULL);
11325 mtx_unlock(&sc->reg_lock);
11326 if (rc != 0)
11327 return (rc);
11328
11329 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11330 if (sb == NULL)
11331 return (ENOMEM);
11332
11333 sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
11334 mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
11335 mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
11336 mtus[14], mtus[15]);
11337
11338 rc = sbuf_finish(sb);
11339 sbuf_delete(sb);
11340
11341 return (rc);
11342 }
11343
11344 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)11345 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
11346 {
11347 struct adapter *sc = arg1;
11348 struct sbuf *sb;
11349 int rc, i;
11350 uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
11351 uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
11352 uint32_t stats[T7_PM_RX_CACHE_NSTATS];
11353 static const char *tx_stats[MAX_PM_NSTATS] = {
11354 "Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
11355 "Tx FIFO wait", NULL, "Tx latency"
11356 };
11357 static const char *rx_stats[MAX_PM_NSTATS] = {
11358 "Read:", "Write bypass:", "Write mem:", "Flush:",
11359 "Rx FIFO wait", NULL, "Rx latency"
11360 };
11361
11362 rc = 0;
11363 mtx_lock(&sc->reg_lock);
11364 if (hw_off_limits(sc))
11365 rc = ENXIO;
11366 else {
11367 t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
11368 t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
11369 if (chip_id(sc) >= CHELSIO_T7)
11370 t4_pmrx_cache_get_stats(sc, stats);
11371 }
11372 mtx_unlock(&sc->reg_lock);
11373 if (rc != 0)
11374 return (rc);
11375
11376 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11377 if (sb == NULL)
11378 return (ENOMEM);
11379
11380 sbuf_printf(sb, " Tx pcmds Tx bytes");
11381 for (i = 0; i < 4; i++) {
11382 sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11383 tx_cyc[i]);
11384 }
11385
11386 sbuf_printf(sb, "\n Rx pcmds Rx bytes");
11387 for (i = 0; i < 4; i++) {
11388 sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11389 rx_cyc[i]);
11390 }
11391
11392 if (chip_id(sc) > CHELSIO_T5) {
11393 sbuf_printf(sb,
11394 "\n Total wait Total occupancy");
11395 sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11396 tx_cyc[i]);
11397 sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11398 rx_cyc[i]);
11399
11400 i += 2;
11401 MPASS(i < nitems(tx_stats));
11402
11403 sbuf_printf(sb,
11404 "\n Reads Total wait");
11405 sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11406 tx_cyc[i]);
11407 sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11408 rx_cyc[i]);
11409 }
11410
11411 if (chip_id(sc) >= CHELSIO_T7) {
11412 i = 0;
11413 sbuf_printf(sb, "\n\nPM RX Cache Stats\n");
11414 sbuf_printf(sb, "%-40s %u\n", "ReqWrite", stats[i++]);
11415 sbuf_printf(sb, "%-40s %u\n", "ReqReadInv", stats[i++]);
11416 sbuf_printf(sb, "%-40s %u\n", "ReqReadNoInv", stats[i++]);
11417 sbuf_printf(sb, "%-40s %u\n", "Write Split Request",
11418 stats[i++]);
11419 sbuf_printf(sb, "%-40s %u\n",
11420 "Normal Read Split (Read Invalidate)", stats[i++]);
11421 sbuf_printf(sb, "%-40s %u\n",
11422 "Feedback Read Split (Read NoInvalidate)",
11423 stats[i++]);
11424 sbuf_printf(sb, "%-40s %u\n", "Write Hit", stats[i++]);
11425 sbuf_printf(sb, "%-40s %u\n", "Normal Read Hit",
11426 stats[i++]);
11427 sbuf_printf(sb, "%-40s %u\n", "Feedback Read Hit",
11428 stats[i++]);
11429 sbuf_printf(sb, "%-40s %u\n", "Normal Read Hit Full Avail",
11430 stats[i++]);
11431 sbuf_printf(sb, "%-40s %u\n", "Normal Read Hit Full UnAvail",
11432 stats[i++]);
11433 sbuf_printf(sb, "%-40s %u\n",
11434 "Normal Read Hit Partial Avail",
11435 stats[i++]);
11436 sbuf_printf(sb, "%-40s %u\n", "FB Read Hit Full Avail",
11437 stats[i++]);
11438 sbuf_printf(sb, "%-40s %u\n", "FB Read Hit Full UnAvail",
11439 stats[i++]);
11440 sbuf_printf(sb, "%-40s %u\n", "FB Read Hit Partial Avail",
11441 stats[i++]);
11442 sbuf_printf(sb, "%-40s %u\n", "Normal Read Full Free",
11443 stats[i++]);
11444 sbuf_printf(sb, "%-40s %u\n",
11445 "Normal Read Part-avail Mul-Regions",
11446 stats[i++]);
11447 sbuf_printf(sb, "%-40s %u\n",
11448 "FB Read Part-avail Mul-Regions",
11449 stats[i++]);
11450 sbuf_printf(sb, "%-40s %u\n", "Write Miss FL Used",
11451 stats[i++]);
11452 sbuf_printf(sb, "%-40s %u\n", "Write Miss LRU Used",
11453 stats[i++]);
11454 sbuf_printf(sb, "%-40s %u\n",
11455 "Write Miss LRU-Multiple Evict", stats[i++]);
11456 sbuf_printf(sb, "%-40s %u\n",
11457 "Write Hit Increasing Islands", stats[i++]);
11458 sbuf_printf(sb, "%-40s %u\n",
11459 "Normal Read Island Read split", stats[i++]);
11460 sbuf_printf(sb, "%-40s %u\n", "Write Overflow Eviction",
11461 stats[i++]);
11462 sbuf_printf(sb, "%-40s %u", "Read Overflow Eviction",
11463 stats[i++]);
11464 }
11465
11466 rc = sbuf_finish(sb);
11467 sbuf_delete(sb);
11468
11469 return (rc);
11470 }
11471
11472 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)11473 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
11474 {
11475 struct adapter *sc = arg1;
11476 struct sbuf *sb;
11477 int rc;
11478 struct tp_rdma_stats stats;
11479
11480 rc = 0;
11481 mtx_lock(&sc->reg_lock);
11482 if (hw_off_limits(sc))
11483 rc = ENXIO;
11484 else
11485 t4_tp_get_rdma_stats(sc, &stats, 0);
11486 mtx_unlock(&sc->reg_lock);
11487 if (rc != 0)
11488 return (rc);
11489
11490 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11491 if (sb == NULL)
11492 return (ENOMEM);
11493
11494 sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
11495 sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
11496
11497 rc = sbuf_finish(sb);
11498 sbuf_delete(sb);
11499
11500 return (rc);
11501 }
11502
11503 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)11504 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
11505 {
11506 struct adapter *sc = arg1;
11507 struct sbuf *sb;
11508 int rc;
11509 struct tp_tcp_stats v4, v6;
11510
11511 rc = 0;
11512 mtx_lock(&sc->reg_lock);
11513 if (hw_off_limits(sc))
11514 rc = ENXIO;
11515 else
11516 t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
11517 mtx_unlock(&sc->reg_lock);
11518 if (rc != 0)
11519 return (rc);
11520
11521 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11522 if (sb == NULL)
11523 return (ENOMEM);
11524
11525 sbuf_printf(sb,
11526 " IP IPv6\n");
11527 sbuf_printf(sb, "OutRsts: %20u %20u\n",
11528 v4.tcp_out_rsts, v6.tcp_out_rsts);
11529 sbuf_printf(sb, "InSegs: %20ju %20ju\n",
11530 v4.tcp_in_segs, v6.tcp_in_segs);
11531 sbuf_printf(sb, "OutSegs: %20ju %20ju\n",
11532 v4.tcp_out_segs, v6.tcp_out_segs);
11533 sbuf_printf(sb, "RetransSegs: %20ju %20ju",
11534 v4.tcp_retrans_segs, v6.tcp_retrans_segs);
11535
11536 rc = sbuf_finish(sb);
11537 sbuf_delete(sb);
11538
11539 return (rc);
11540 }
11541
11542 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)11543 sysctl_tids(SYSCTL_HANDLER_ARGS)
11544 {
11545 struct adapter *sc = arg1;
11546 struct sbuf *sb;
11547 int rc;
11548 uint32_t x, y;
11549 struct tid_info *t = &sc->tids;
11550
11551 rc = 0;
11552 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11553 if (sb == NULL)
11554 return (ENOMEM);
11555
11556 if (t->natids) {
11557 sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
11558 t->atids_in_use);
11559 }
11560
11561 if (t->nhpftids) {
11562 sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
11563 t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
11564 }
11565
11566 if (t->ntids) {
11567 bool hashen = false;
11568
11569 mtx_lock(&sc->reg_lock);
11570 if (hw_off_limits(sc))
11571 rc = ENXIO;
11572 else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
11573 hashen = true;
11574 if (chip_id(sc) <= CHELSIO_T5) {
11575 x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
11576 y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
11577 } else {
11578 x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
11579 y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
11580 }
11581 }
11582 mtx_unlock(&sc->reg_lock);
11583 if (rc != 0)
11584 goto done;
11585
11586 sbuf_printf(sb, "TID range: ");
11587 if (hashen) {
11588 if (x)
11589 sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
11590 sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
11591 } else {
11592 sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
11593 t->ntids - 1);
11594 }
11595 sbuf_printf(sb, ", in use: %u\n",
11596 atomic_load_acq_int(&t->tids_in_use));
11597 }
11598
11599 if (t->nstids) {
11600 sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
11601 t->stid_base + t->nstids - 1, t->stids_in_use);
11602 }
11603
11604 if (t->nftids) {
11605 sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
11606 t->ftid_end, t->ftids_in_use);
11607 }
11608
11609 if (t->netids) {
11610 sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
11611 t->etid_base + t->netids - 1, t->etids_in_use);
11612 }
11613
11614 mtx_lock(&sc->reg_lock);
11615 if (hw_off_limits(sc))
11616 rc = ENXIO;
11617 else {
11618 x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
11619 y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
11620 }
11621 mtx_unlock(&sc->reg_lock);
11622 if (rc != 0)
11623 goto done;
11624 sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
11625 done:
11626 if (rc == 0)
11627 rc = sbuf_finish(sb);
11628 else
11629 (void)sbuf_finish(sb);
11630 sbuf_delete(sb);
11631
11632 return (rc);
11633 }
11634
11635 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)11636 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
11637 {
11638 struct adapter *sc = arg1;
11639 struct sbuf *sb;
11640 int rc;
11641 struct tp_err_stats stats;
11642
11643 rc = 0;
11644 mtx_lock(&sc->reg_lock);
11645 if (hw_off_limits(sc))
11646 rc = ENXIO;
11647 else
11648 t4_tp_get_err_stats(sc, &stats, 0);
11649 mtx_unlock(&sc->reg_lock);
11650 if (rc != 0)
11651 return (rc);
11652
11653 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11654 if (sb == NULL)
11655 return (ENOMEM);
11656
11657 if (sc->chip_params->nchan > 2) {
11658 sbuf_printf(sb, " channel 0 channel 1"
11659 " channel 2 channel 3\n");
11660 sbuf_printf(sb, "macInErrs: %10u %10u %10u %10u\n",
11661 stats.mac_in_errs[0], stats.mac_in_errs[1],
11662 stats.mac_in_errs[2], stats.mac_in_errs[3]);
11663 sbuf_printf(sb, "hdrInErrs: %10u %10u %10u %10u\n",
11664 stats.hdr_in_errs[0], stats.hdr_in_errs[1],
11665 stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
11666 sbuf_printf(sb, "tcpInErrs: %10u %10u %10u %10u\n",
11667 stats.tcp_in_errs[0], stats.tcp_in_errs[1],
11668 stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
11669 sbuf_printf(sb, "tcp6InErrs: %10u %10u %10u %10u\n",
11670 stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
11671 stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
11672 sbuf_printf(sb, "tnlCongDrops: %10u %10u %10u %10u\n",
11673 stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
11674 stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
11675 sbuf_printf(sb, "tnlTxDrops: %10u %10u %10u %10u\n",
11676 stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
11677 stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
11678 sbuf_printf(sb, "ofldVlanDrops: %10u %10u %10u %10u\n",
11679 stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
11680 stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
11681 sbuf_printf(sb, "ofldChanDrops: %10u %10u %10u %10u\n\n",
11682 stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
11683 stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
11684 } else {
11685 sbuf_printf(sb, " channel 0 channel 1\n");
11686 sbuf_printf(sb, "macInErrs: %10u %10u\n",
11687 stats.mac_in_errs[0], stats.mac_in_errs[1]);
11688 sbuf_printf(sb, "hdrInErrs: %10u %10u\n",
11689 stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
11690 sbuf_printf(sb, "tcpInErrs: %10u %10u\n",
11691 stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
11692 sbuf_printf(sb, "tcp6InErrs: %10u %10u\n",
11693 stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
11694 sbuf_printf(sb, "tnlCongDrops: %10u %10u\n",
11695 stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
11696 sbuf_printf(sb, "tnlTxDrops: %10u %10u\n",
11697 stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
11698 sbuf_printf(sb, "ofldVlanDrops: %10u %10u\n",
11699 stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
11700 sbuf_printf(sb, "ofldChanDrops: %10u %10u\n\n",
11701 stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
11702 }
11703
11704 sbuf_printf(sb, "ofldNoNeigh: %u\nofldCongDefer: %u",
11705 stats.ofld_no_neigh, stats.ofld_cong_defer);
11706
11707 rc = sbuf_finish(sb);
11708 sbuf_delete(sb);
11709
11710 return (rc);
11711 }
11712
11713 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)11714 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
11715 {
11716 struct adapter *sc = arg1;
11717 struct sbuf *sb;
11718 int rc;
11719 struct tp_tnl_stats stats;
11720
11721 rc = 0;
11722 mtx_lock(&sc->reg_lock);
11723 if (hw_off_limits(sc))
11724 rc = ENXIO;
11725 else
11726 t4_tp_get_tnl_stats(sc, &stats, 1);
11727 mtx_unlock(&sc->reg_lock);
11728 if (rc != 0)
11729 return (rc);
11730
11731 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11732 if (sb == NULL)
11733 return (ENOMEM);
11734
11735 if (sc->chip_params->nchan > 2) {
11736 sbuf_printf(sb, " channel 0 channel 1"
11737 " channel 2 channel 3\n");
11738 sbuf_printf(sb, "OutPkts: %10u %10u %10u %10u\n",
11739 stats.out_pkt[0], stats.out_pkt[1],
11740 stats.out_pkt[2], stats.out_pkt[3]);
11741 sbuf_printf(sb, "InPkts: %10u %10u %10u %10u",
11742 stats.in_pkt[0], stats.in_pkt[1],
11743 stats.in_pkt[2], stats.in_pkt[3]);
11744 } else {
11745 sbuf_printf(sb, " channel 0 channel 1\n");
11746 sbuf_printf(sb, "OutPkts: %10u %10u\n",
11747 stats.out_pkt[0], stats.out_pkt[1]);
11748 sbuf_printf(sb, "InPkts: %10u %10u",
11749 stats.in_pkt[0], stats.in_pkt[1]);
11750 }
11751
11752 rc = sbuf_finish(sb);
11753 sbuf_delete(sb);
11754
11755 return (rc);
11756 }
11757
11758 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)11759 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
11760 {
11761 struct adapter *sc = arg1;
11762 struct tp_params *tpp = &sc->params.tp;
11763 u_int mask;
11764 int rc;
11765
11766 mask = tpp->la_mask >> 16;
11767 rc = sysctl_handle_int(oidp, &mask, 0, req);
11768 if (rc != 0 || req->newptr == NULL)
11769 return (rc);
11770 if (mask > 0xffff)
11771 return (EINVAL);
11772 mtx_lock(&sc->reg_lock);
11773 if (hw_off_limits(sc))
11774 rc = ENXIO;
11775 else {
11776 tpp->la_mask = mask << 16;
11777 t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
11778 tpp->la_mask);
11779 }
11780 mtx_unlock(&sc->reg_lock);
11781
11782 return (rc);
11783 }
11784
11785 struct field_desc {
11786 const char *name;
11787 u_int start;
11788 u_int width;
11789 };
11790
11791 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)11792 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
11793 {
11794 char buf[32];
11795 int line_size = 0;
11796
11797 while (f->name) {
11798 uint64_t mask = (1ULL << f->width) - 1;
11799 int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
11800 ((uintmax_t)v >> f->start) & mask);
11801
11802 if (line_size + len >= 79) {
11803 line_size = 8;
11804 sbuf_printf(sb, "\n ");
11805 }
11806 sbuf_printf(sb, "%s ", buf);
11807 line_size += len + 1;
11808 f++;
11809 }
11810 sbuf_printf(sb, "\n");
11811 }
11812
11813 static const struct field_desc tp_la0[] = {
11814 { "RcfOpCodeOut", 60, 4 },
11815 { "State", 56, 4 },
11816 { "WcfState", 52, 4 },
11817 { "RcfOpcSrcOut", 50, 2 },
11818 { "CRxError", 49, 1 },
11819 { "ERxError", 48, 1 },
11820 { "SanityFailed", 47, 1 },
11821 { "SpuriousMsg", 46, 1 },
11822 { "FlushInputMsg", 45, 1 },
11823 { "FlushInputCpl", 44, 1 },
11824 { "RssUpBit", 43, 1 },
11825 { "RssFilterHit", 42, 1 },
11826 { "Tid", 32, 10 },
11827 { "InitTcb", 31, 1 },
11828 { "LineNumber", 24, 7 },
11829 { "Emsg", 23, 1 },
11830 { "EdataOut", 22, 1 },
11831 { "Cmsg", 21, 1 },
11832 { "CdataOut", 20, 1 },
11833 { "EreadPdu", 19, 1 },
11834 { "CreadPdu", 18, 1 },
11835 { "TunnelPkt", 17, 1 },
11836 { "RcfPeerFin", 16, 1 },
11837 { "RcfReasonOut", 12, 4 },
11838 { "TxCchannel", 10, 2 },
11839 { "RcfTxChannel", 8, 2 },
11840 { "RxEchannel", 6, 2 },
11841 { "RcfRxChannel", 5, 1 },
11842 { "RcfDataOutSrdy", 4, 1 },
11843 { "RxDvld", 3, 1 },
11844 { "RxOoDvld", 2, 1 },
11845 { "RxCongestion", 1, 1 },
11846 { "TxCongestion", 0, 1 },
11847 { NULL }
11848 };
11849
11850 static const struct field_desc tp_la1[] = {
11851 { "CplCmdIn", 56, 8 },
11852 { "CplCmdOut", 48, 8 },
11853 { "ESynOut", 47, 1 },
11854 { "EAckOut", 46, 1 },
11855 { "EFinOut", 45, 1 },
11856 { "ERstOut", 44, 1 },
11857 { "SynIn", 43, 1 },
11858 { "AckIn", 42, 1 },
11859 { "FinIn", 41, 1 },
11860 { "RstIn", 40, 1 },
11861 { "DataIn", 39, 1 },
11862 { "DataInVld", 38, 1 },
11863 { "PadIn", 37, 1 },
11864 { "RxBufEmpty", 36, 1 },
11865 { "RxDdp", 35, 1 },
11866 { "RxFbCongestion", 34, 1 },
11867 { "TxFbCongestion", 33, 1 },
11868 { "TxPktSumSrdy", 32, 1 },
11869 { "RcfUlpType", 28, 4 },
11870 { "Eread", 27, 1 },
11871 { "Ebypass", 26, 1 },
11872 { "Esave", 25, 1 },
11873 { "Static0", 24, 1 },
11874 { "Cread", 23, 1 },
11875 { "Cbypass", 22, 1 },
11876 { "Csave", 21, 1 },
11877 { "CPktOut", 20, 1 },
11878 { "RxPagePoolFull", 18, 2 },
11879 { "RxLpbkPkt", 17, 1 },
11880 { "TxLpbkPkt", 16, 1 },
11881 { "RxVfValid", 15, 1 },
11882 { "SynLearned", 14, 1 },
11883 { "SetDelEntry", 13, 1 },
11884 { "SetInvEntry", 12, 1 },
11885 { "CpcmdDvld", 11, 1 },
11886 { "CpcmdSave", 10, 1 },
11887 { "RxPstructsFull", 8, 2 },
11888 { "EpcmdDvld", 7, 1 },
11889 { "EpcmdFlush", 6, 1 },
11890 { "EpcmdTrimPrefix", 5, 1 },
11891 { "EpcmdTrimPostfix", 4, 1 },
11892 { "ERssIp4Pkt", 3, 1 },
11893 { "ERssIp6Pkt", 2, 1 },
11894 { "ERssTcpUdpPkt", 1, 1 },
11895 { "ERssFceFipPkt", 0, 1 },
11896 { NULL }
11897 };
11898
11899 static const struct field_desc tp_la2[] = {
11900 { "CplCmdIn", 56, 8 },
11901 { "MpsVfVld", 55, 1 },
11902 { "MpsPf", 52, 3 },
11903 { "MpsVf", 44, 8 },
11904 { "SynIn", 43, 1 },
11905 { "AckIn", 42, 1 },
11906 { "FinIn", 41, 1 },
11907 { "RstIn", 40, 1 },
11908 { "DataIn", 39, 1 },
11909 { "DataInVld", 38, 1 },
11910 { "PadIn", 37, 1 },
11911 { "RxBufEmpty", 36, 1 },
11912 { "RxDdp", 35, 1 },
11913 { "RxFbCongestion", 34, 1 },
11914 { "TxFbCongestion", 33, 1 },
11915 { "TxPktSumSrdy", 32, 1 },
11916 { "RcfUlpType", 28, 4 },
11917 { "Eread", 27, 1 },
11918 { "Ebypass", 26, 1 },
11919 { "Esave", 25, 1 },
11920 { "Static0", 24, 1 },
11921 { "Cread", 23, 1 },
11922 { "Cbypass", 22, 1 },
11923 { "Csave", 21, 1 },
11924 { "CPktOut", 20, 1 },
11925 { "RxPagePoolFull", 18, 2 },
11926 { "RxLpbkPkt", 17, 1 },
11927 { "TxLpbkPkt", 16, 1 },
11928 { "RxVfValid", 15, 1 },
11929 { "SynLearned", 14, 1 },
11930 { "SetDelEntry", 13, 1 },
11931 { "SetInvEntry", 12, 1 },
11932 { "CpcmdDvld", 11, 1 },
11933 { "CpcmdSave", 10, 1 },
11934 { "RxPstructsFull", 8, 2 },
11935 { "EpcmdDvld", 7, 1 },
11936 { "EpcmdFlush", 6, 1 },
11937 { "EpcmdTrimPrefix", 5, 1 },
11938 { "EpcmdTrimPostfix", 4, 1 },
11939 { "ERssIp4Pkt", 3, 1 },
11940 { "ERssIp6Pkt", 2, 1 },
11941 { "ERssTcpUdpPkt", 1, 1 },
11942 { "ERssFceFipPkt", 0, 1 },
11943 { NULL }
11944 };
11945
11946 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)11947 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
11948 {
11949
11950 field_desc_show(sb, *p, tp_la0);
11951 }
11952
11953 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)11954 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
11955 {
11956
11957 if (idx)
11958 sbuf_printf(sb, "\n");
11959 field_desc_show(sb, p[0], tp_la0);
11960 if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11961 field_desc_show(sb, p[1], tp_la0);
11962 }
11963
11964 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)11965 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
11966 {
11967
11968 if (idx)
11969 sbuf_printf(sb, "\n");
11970 field_desc_show(sb, p[0], tp_la0);
11971 if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11972 field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
11973 }
11974
11975 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)11976 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
11977 {
11978 struct adapter *sc = arg1;
11979 struct sbuf *sb;
11980 uint64_t *buf, *p;
11981 int rc;
11982 u_int i, inc;
11983 void (*show_func)(struct sbuf *, uint64_t *, int);
11984
11985 rc = 0;
11986 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11987 if (sb == NULL)
11988 return (ENOMEM);
11989
11990 buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11991
11992 mtx_lock(&sc->reg_lock);
11993 if (hw_off_limits(sc))
11994 rc = ENXIO;
11995 else {
11996 t4_tp_read_la(sc, buf, NULL);
11997 switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11998 case 2:
11999 inc = 2;
12000 show_func = tp_la_show2;
12001 break;
12002 case 3:
12003 inc = 2;
12004 show_func = tp_la_show3;
12005 break;
12006 default:
12007 inc = 1;
12008 show_func = tp_la_show;
12009 }
12010 }
12011 mtx_unlock(&sc->reg_lock);
12012 if (rc != 0)
12013 goto done;
12014
12015 p = buf;
12016 for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
12017 (*show_func)(sb, p, i);
12018 rc = sbuf_finish(sb);
12019 done:
12020 sbuf_delete(sb);
12021 free(buf, M_CXGBE);
12022 return (rc);
12023 }
12024
12025 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)12026 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
12027 {
12028 struct adapter *sc = arg1;
12029 struct sbuf *sb;
12030 int rc;
12031 u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
12032
12033 rc = 0;
12034 mtx_lock(&sc->reg_lock);
12035 if (hw_off_limits(sc))
12036 rc = ENXIO;
12037 else
12038 t4_get_chan_txrate(sc, nrate, orate);
12039 mtx_unlock(&sc->reg_lock);
12040 if (rc != 0)
12041 return (rc);
12042
12043 sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
12044 if (sb == NULL)
12045 return (ENOMEM);
12046
12047 if (sc->chip_params->nchan > 2) {
12048 sbuf_printf(sb, " channel 0 channel 1"
12049 " channel 2 channel 3\n");
12050 sbuf_printf(sb, "NIC B/s: %10ju %10ju %10ju %10ju\n",
12051 nrate[0], nrate[1], nrate[2], nrate[3]);
12052 sbuf_printf(sb, "Offload B/s: %10ju %10ju %10ju %10ju",
12053 orate[0], orate[1], orate[2], orate[3]);
12054 } else {
12055 sbuf_printf(sb, " channel 0 channel 1\n");
12056 sbuf_printf(sb, "NIC B/s: %10ju %10ju\n",
12057 nrate[0], nrate[1]);
12058 sbuf_printf(sb, "Offload B/s: %10ju %10ju",
12059 orate[0], orate[1]);
12060 }
12061
12062 rc = sbuf_finish(sb);
12063 sbuf_delete(sb);
12064
12065 return (rc);
12066 }
12067
12068 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)12069 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
12070 {
12071 struct adapter *sc = arg1;
12072 struct sbuf *sb;
12073 uint32_t *buf, *p;
12074 int rc, i;
12075
12076 rc = 0;
12077 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12078 if (sb == NULL)
12079 return (ENOMEM);
12080
12081 buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
12082 M_ZERO | M_WAITOK);
12083
12084 mtx_lock(&sc->reg_lock);
12085 if (hw_off_limits(sc))
12086 rc = ENXIO;
12087 else
12088 t4_ulprx_read_la(sc, buf);
12089 mtx_unlock(&sc->reg_lock);
12090 if (rc != 0)
12091 goto done;
12092
12093 p = buf;
12094 sbuf_printf(sb, " Pcmd Type Message"
12095 " Data");
12096 for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
12097 sbuf_printf(sb, "\n%08x%08x %4x %08x %08x%08x%08x%08x",
12098 p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
12099 }
12100 rc = sbuf_finish(sb);
12101 done:
12102 sbuf_delete(sb);
12103 free(buf, M_CXGBE);
12104 return (rc);
12105 }
12106
12107 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)12108 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
12109 {
12110 struct adapter *sc = arg1;
12111 struct sbuf *sb;
12112 int rc;
12113 uint32_t cfg, s1, s2;
12114
12115 MPASS(chip_id(sc) >= CHELSIO_T5);
12116
12117 rc = 0;
12118 mtx_lock(&sc->reg_lock);
12119 if (hw_off_limits(sc))
12120 rc = ENXIO;
12121 else {
12122 cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
12123 s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
12124 s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
12125 }
12126 mtx_unlock(&sc->reg_lock);
12127 if (rc != 0)
12128 return (rc);
12129
12130 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12131 if (sb == NULL)
12132 return (ENOMEM);
12133
12134 if (G_STATSOURCE_T5(cfg) == 7) {
12135 int mode;
12136
12137 mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
12138 if (mode == 0)
12139 sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
12140 else if (mode == 1)
12141 sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
12142 else
12143 sbuf_printf(sb, "unknown mode %d", mode);
12144 }
12145 rc = sbuf_finish(sb);
12146 sbuf_delete(sb);
12147
12148 return (rc);
12149 }
12150
12151 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)12152 sysctl_cpus(SYSCTL_HANDLER_ARGS)
12153 {
12154 struct adapter *sc = arg1;
12155 enum cpu_sets op = arg2;
12156 cpuset_t cpuset;
12157 struct sbuf *sb;
12158 int i, rc;
12159
12160 MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
12161
12162 CPU_ZERO(&cpuset);
12163 rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
12164 if (rc != 0)
12165 return (rc);
12166
12167 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12168 if (sb == NULL)
12169 return (ENOMEM);
12170
12171 CPU_FOREACH(i)
12172 sbuf_printf(sb, "%d ", i);
12173 rc = sbuf_finish(sb);
12174 sbuf_delete(sb);
12175
12176 return (rc);
12177 }
12178
12179 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)12180 sysctl_reset(SYSCTL_HANDLER_ARGS)
12181 {
12182 struct adapter *sc = arg1;
12183 u_int val;
12184 int rc;
12185
12186 val = atomic_load_int(&sc->num_resets);
12187 rc = sysctl_handle_int(oidp, &val, 0, req);
12188 if (rc != 0 || req->newptr == NULL)
12189 return (rc);
12190
12191 if (val == 0) {
12192 /* Zero out the counter that tracks reset. */
12193 atomic_store_int(&sc->num_resets, 0);
12194 return (0);
12195 }
12196
12197 if (val != 1)
12198 return (EINVAL); /* 0 or 1 are the only legal values */
12199
12200 if (hw_off_limits(sc)) /* harmless race */
12201 return (EALREADY);
12202
12203 taskqueue_enqueue(reset_tq, &sc->reset_task);
12204 return (0);
12205 }
12206
12207 #ifdef TCP_OFFLOAD
12208 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)12209 sysctl_tls(SYSCTL_HANDLER_ARGS)
12210 {
12211 struct adapter *sc = arg1;
12212 int i, j, v, rc;
12213 struct vi_info *vi;
12214
12215 v = sc->tt.tls;
12216 rc = sysctl_handle_int(oidp, &v, 0, req);
12217 if (rc != 0 || req->newptr == NULL)
12218 return (rc);
12219
12220 if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
12221 return (ENOTSUP);
12222
12223 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
12224 if (rc)
12225 return (rc);
12226 if (hw_off_limits(sc))
12227 rc = ENXIO;
12228 else {
12229 sc->tt.tls = !!v;
12230 for_each_port(sc, i) {
12231 for_each_vi(sc->port[i], j, vi) {
12232 if (vi->flags & VI_INIT_DONE)
12233 t4_update_fl_bufsize(vi->ifp);
12234 }
12235 }
12236 }
12237 end_synchronized_op(sc, 0);
12238
12239 return (rc);
12240
12241 }
12242
12243 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)12244 unit_conv(char *buf, size_t len, u_int val, u_int factor)
12245 {
12246 u_int rem = val % factor;
12247
12248 if (rem == 0)
12249 snprintf(buf, len, "%u", val / factor);
12250 else {
12251 while (rem % 10 == 0)
12252 rem /= 10;
12253 snprintf(buf, len, "%u.%u", val / factor, rem);
12254 }
12255 }
12256
12257 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)12258 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
12259 {
12260 struct adapter *sc = arg1;
12261 char buf[16];
12262 u_int res, re;
12263 u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12264
12265 mtx_lock(&sc->reg_lock);
12266 if (hw_off_limits(sc))
12267 res = (u_int)-1;
12268 else
12269 res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
12270 mtx_unlock(&sc->reg_lock);
12271 if (res == (u_int)-1)
12272 return (ENXIO);
12273
12274 switch (arg2) {
12275 case 0:
12276 /* timer_tick */
12277 re = G_TIMERRESOLUTION(res);
12278 break;
12279 case 1:
12280 /* TCP timestamp tick */
12281 re = G_TIMESTAMPRESOLUTION(res);
12282 break;
12283 case 2:
12284 /* DACK tick */
12285 re = G_DELAYEDACKRESOLUTION(res);
12286 break;
12287 default:
12288 return (EDOOFUS);
12289 }
12290
12291 unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
12292
12293 return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
12294 }
12295
12296 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)12297 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
12298 {
12299 struct adapter *sc = arg1;
12300 int rc;
12301 u_int dack_tmr, dack_re, v;
12302 u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12303
12304 mtx_lock(&sc->reg_lock);
12305 if (hw_off_limits(sc))
12306 rc = ENXIO;
12307 else {
12308 rc = 0;
12309 dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
12310 A_TP_TIMER_RESOLUTION));
12311 dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
12312 }
12313 mtx_unlock(&sc->reg_lock);
12314 if (rc != 0)
12315 return (rc);
12316
12317 v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
12318
12319 return (sysctl_handle_int(oidp, &v, 0, req));
12320 }
12321
12322 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)12323 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
12324 {
12325 struct adapter *sc = arg1;
12326 int rc, reg = arg2;
12327 u_int tre;
12328 u_long tp_tick_us, v;
12329 u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12330
12331 MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
12332 reg == A_TP_PERS_MIN || reg == A_TP_PERS_MAX ||
12333 reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
12334 reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
12335
12336 mtx_lock(&sc->reg_lock);
12337 if (hw_off_limits(sc))
12338 rc = ENXIO;
12339 else {
12340 rc = 0;
12341 tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
12342 tp_tick_us = (cclk_ps << tre) / 1000000;
12343 if (reg == A_TP_INIT_SRTT)
12344 v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
12345 else
12346 v = tp_tick_us * t4_read_reg(sc, reg);
12347 }
12348 mtx_unlock(&sc->reg_lock);
12349 if (rc != 0)
12350 return (rc);
12351 else
12352 return (sysctl_handle_long(oidp, &v, 0, req));
12353 }
12354
12355 /*
12356 * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
12357 * passed to this function.
12358 */
12359 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)12360 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
12361 {
12362 struct adapter *sc = arg1;
12363 int rc, idx = arg2;
12364 u_int v;
12365
12366 MPASS(idx >= 0 && idx <= 24);
12367
12368 mtx_lock(&sc->reg_lock);
12369 if (hw_off_limits(sc))
12370 rc = ENXIO;
12371 else {
12372 rc = 0;
12373 v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
12374 }
12375 mtx_unlock(&sc->reg_lock);
12376 if (rc != 0)
12377 return (rc);
12378 else
12379 return (sysctl_handle_int(oidp, &v, 0, req));
12380 }
12381
12382 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)12383 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
12384 {
12385 struct adapter *sc = arg1;
12386 int rc, idx = arg2;
12387 u_int shift, v, r;
12388
12389 MPASS(idx >= 0 && idx < 16);
12390
12391 r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
12392 shift = (idx & 3) << 3;
12393 mtx_lock(&sc->reg_lock);
12394 if (hw_off_limits(sc))
12395 rc = ENXIO;
12396 else {
12397 rc = 0;
12398 v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
12399 }
12400 mtx_unlock(&sc->reg_lock);
12401 if (rc != 0)
12402 return (rc);
12403 else
12404 return (sysctl_handle_int(oidp, &v, 0, req));
12405 }
12406
12407 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)12408 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
12409 {
12410 struct vi_info *vi = arg1;
12411 struct adapter *sc = vi->adapter;
12412 int idx, rc, i;
12413 struct sge_ofld_rxq *ofld_rxq;
12414 uint8_t v;
12415
12416 idx = vi->ofld_tmr_idx;
12417
12418 rc = sysctl_handle_int(oidp, &idx, 0, req);
12419 if (rc != 0 || req->newptr == NULL)
12420 return (rc);
12421
12422 if (idx < 0 || idx >= SGE_NTIMERS)
12423 return (EINVAL);
12424
12425 rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
12426 "t4otmr");
12427 if (rc)
12428 return (rc);
12429
12430 v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
12431 for_each_ofld_rxq(vi, i, ofld_rxq) {
12432 #ifdef atomic_store_rel_8
12433 atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
12434 #else
12435 ofld_rxq->iq.intr_params = v;
12436 #endif
12437 }
12438 vi->ofld_tmr_idx = idx;
12439
12440 end_synchronized_op(sc, LOCK_HELD);
12441 return (0);
12442 }
12443
12444 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)12445 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
12446 {
12447 struct vi_info *vi = arg1;
12448 struct adapter *sc = vi->adapter;
12449 int idx, rc;
12450
12451 idx = vi->ofld_pktc_idx;
12452
12453 rc = sysctl_handle_int(oidp, &idx, 0, req);
12454 if (rc != 0 || req->newptr == NULL)
12455 return (rc);
12456
12457 if (idx < -1 || idx >= SGE_NCOUNTERS)
12458 return (EINVAL);
12459
12460 rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
12461 "t4opktc");
12462 if (rc)
12463 return (rc);
12464
12465 if (vi->flags & VI_INIT_DONE)
12466 rc = EBUSY; /* cannot be changed once the queues are created */
12467 else
12468 vi->ofld_pktc_idx = idx;
12469
12470 end_synchronized_op(sc, LOCK_HELD);
12471 return (rc);
12472 }
12473 #endif
12474
12475 static int
get_sge_context(struct adapter * sc,int mem_id,uint32_t cid,int len,uint32_t * data)12476 get_sge_context(struct adapter *sc, int mem_id, uint32_t cid, int len,
12477 uint32_t *data)
12478 {
12479 int rc;
12480
12481 if (len < sc->chip_params->sge_ctxt_size)
12482 return (ENOBUFS);
12483 if (cid > M_CTXTQID)
12484 return (EINVAL);
12485 if (mem_id != CTXT_EGRESS && mem_id != CTXT_INGRESS &&
12486 mem_id != CTXT_FLM && mem_id != CTXT_CNM)
12487 return (EINVAL);
12488
12489 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
12490 if (rc)
12491 return (rc);
12492
12493 if (hw_off_limits(sc)) {
12494 rc = ENXIO;
12495 goto done;
12496 }
12497
12498 if (sc->flags & FW_OK) {
12499 rc = -t4_sge_ctxt_rd(sc, sc->mbox, cid, mem_id, data);
12500 if (rc == 0)
12501 goto done;
12502 }
12503
12504 /*
12505 * Read via firmware failed or wasn't even attempted. Read directly via
12506 * the backdoor.
12507 */
12508 rc = -t4_sge_ctxt_rd_bd(sc, cid, mem_id, data);
12509 done:
12510 end_synchronized_op(sc, 0);
12511 return (rc);
12512 }
12513
12514 static int
load_fw(struct adapter * sc,struct t4_data * fw)12515 load_fw(struct adapter *sc, struct t4_data *fw)
12516 {
12517 int rc;
12518 uint8_t *fw_data;
12519
12520 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
12521 if (rc)
12522 return (rc);
12523
12524 if (hw_off_limits(sc)) {
12525 rc = ENXIO;
12526 goto done;
12527 }
12528
12529 /*
12530 * The firmware, with the sole exception of the memory parity error
12531 * handler, runs from memory and not flash. It is almost always safe to
12532 * install a new firmware on a running system. Just set bit 1 in
12533 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
12534 */
12535 if (sc->flags & FULL_INIT_DONE &&
12536 (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
12537 rc = EBUSY;
12538 goto done;
12539 }
12540
12541 fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
12542
12543 rc = copyin(fw->data, fw_data, fw->len);
12544 if (rc == 0)
12545 rc = -t4_load_fw(sc, fw_data, fw->len);
12546
12547 free(fw_data, M_CXGBE);
12548 done:
12549 end_synchronized_op(sc, 0);
12550 return (rc);
12551 }
12552
12553 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)12554 load_cfg(struct adapter *sc, struct t4_data *cfg)
12555 {
12556 int rc;
12557 uint8_t *cfg_data = NULL;
12558
12559 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
12560 if (rc)
12561 return (rc);
12562
12563 if (hw_off_limits(sc)) {
12564 rc = ENXIO;
12565 goto done;
12566 }
12567
12568 if (cfg->len == 0) {
12569 /* clear */
12570 rc = -t4_load_cfg(sc, NULL, 0);
12571 goto done;
12572 }
12573
12574 cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
12575
12576 rc = copyin(cfg->data, cfg_data, cfg->len);
12577 if (rc == 0)
12578 rc = -t4_load_cfg(sc, cfg_data, cfg->len);
12579
12580 free(cfg_data, M_CXGBE);
12581 done:
12582 end_synchronized_op(sc, 0);
12583 return (rc);
12584 }
12585
12586 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)12587 load_boot(struct adapter *sc, struct t4_bootrom *br)
12588 {
12589 int rc;
12590 uint8_t *br_data = NULL;
12591 u_int offset;
12592
12593 if (br->len > 1024 * 1024)
12594 return (EFBIG);
12595
12596 if (br->pf_offset == 0) {
12597 /* pfidx */
12598 if (br->pfidx_addr > 7)
12599 return (EINVAL);
12600 offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
12601 A_PCIE_PF_EXPROM_OFST)));
12602 } else if (br->pf_offset == 1) {
12603 /* offset */
12604 offset = G_OFFSET(br->pfidx_addr);
12605 } else {
12606 return (EINVAL);
12607 }
12608
12609 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
12610 if (rc)
12611 return (rc);
12612
12613 if (hw_off_limits(sc)) {
12614 rc = ENXIO;
12615 goto done;
12616 }
12617
12618 if (br->len == 0) {
12619 /* clear */
12620 rc = -t4_load_boot(sc, NULL, offset, 0);
12621 goto done;
12622 }
12623
12624 br_data = malloc(br->len, M_CXGBE, M_WAITOK);
12625
12626 rc = copyin(br->data, br_data, br->len);
12627 if (rc == 0)
12628 rc = -t4_load_boot(sc, br_data, offset, br->len);
12629
12630 free(br_data, M_CXGBE);
12631 done:
12632 end_synchronized_op(sc, 0);
12633 return (rc);
12634 }
12635
12636 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)12637 load_bootcfg(struct adapter *sc, struct t4_data *bc)
12638 {
12639 int rc;
12640 uint8_t *bc_data = NULL;
12641
12642 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
12643 if (rc)
12644 return (rc);
12645
12646 if (hw_off_limits(sc)) {
12647 rc = ENXIO;
12648 goto done;
12649 }
12650
12651 if (bc->len == 0) {
12652 /* clear */
12653 rc = -t4_load_bootcfg(sc, NULL, 0);
12654 goto done;
12655 }
12656
12657 bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
12658
12659 rc = copyin(bc->data, bc_data, bc->len);
12660 if (rc == 0)
12661 rc = -t4_load_bootcfg(sc, bc_data, bc->len);
12662
12663 free(bc_data, M_CXGBE);
12664 done:
12665 end_synchronized_op(sc, 0);
12666 return (rc);
12667 }
12668
12669 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)12670 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
12671 {
12672 int rc;
12673 struct cudbg_init *cudbg;
12674 void *handle, *buf;
12675
12676 /* buf is large, don't block if no memory is available */
12677 buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
12678 if (buf == NULL)
12679 return (ENOMEM);
12680
12681 handle = cudbg_alloc_handle();
12682 if (handle == NULL) {
12683 rc = ENOMEM;
12684 goto done;
12685 }
12686
12687 cudbg = cudbg_get_init(handle);
12688 cudbg->adap = sc;
12689 cudbg->print = (cudbg_print_cb)printf;
12690
12691 #ifndef notyet
12692 device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
12693 __func__, dump->wr_flash, dump->len, dump->data);
12694 #endif
12695
12696 if (dump->wr_flash)
12697 cudbg->use_flash = 1;
12698 MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
12699 memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
12700
12701 rc = cudbg_collect(handle, buf, &dump->len);
12702 if (rc != 0)
12703 goto done;
12704
12705 rc = copyout(buf, dump->data, dump->len);
12706 done:
12707 cudbg_free_handle(handle);
12708 free(buf, M_CXGBE);
12709 return (rc);
12710 }
12711
12712 static void
free_offload_policy(struct t4_offload_policy * op)12713 free_offload_policy(struct t4_offload_policy *op)
12714 {
12715 struct offload_rule *r;
12716 int i;
12717
12718 if (op == NULL)
12719 return;
12720
12721 r = &op->rule[0];
12722 for (i = 0; i < op->nrules; i++, r++) {
12723 free(r->bpf_prog.bf_insns, M_CXGBE);
12724 }
12725 free(op->rule, M_CXGBE);
12726 free(op, M_CXGBE);
12727 }
12728
12729 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)12730 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
12731 {
12732 int i, rc, len;
12733 struct t4_offload_policy *op, *old;
12734 struct bpf_program *bf;
12735 const struct offload_settings *s;
12736 struct offload_rule *r;
12737 void *u;
12738
12739 if (!is_offload(sc))
12740 return (ENODEV);
12741
12742 if (uop->nrules == 0) {
12743 /* Delete installed policies. */
12744 op = NULL;
12745 goto set_policy;
12746 } else if (uop->nrules > 256) { /* arbitrary */
12747 return (E2BIG);
12748 }
12749
12750 /* Copy userspace offload policy to kernel */
12751 op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
12752 op->nrules = uop->nrules;
12753 len = op->nrules * sizeof(struct offload_rule);
12754 op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
12755 rc = copyin(uop->rule, op->rule, len);
12756 if (rc) {
12757 free(op->rule, M_CXGBE);
12758 free(op, M_CXGBE);
12759 return (rc);
12760 }
12761
12762 r = &op->rule[0];
12763 for (i = 0; i < op->nrules; i++, r++) {
12764
12765 /* Validate open_type */
12766 if (r->open_type != OPEN_TYPE_LISTEN &&
12767 r->open_type != OPEN_TYPE_ACTIVE &&
12768 r->open_type != OPEN_TYPE_PASSIVE &&
12769 r->open_type != OPEN_TYPE_DONTCARE) {
12770 error:
12771 /*
12772 * Rules 0 to i have malloc'd filters that need to be
12773 * freed. Rules i+1 to nrules have userspace pointers
12774 * and should be left alone.
12775 */
12776 op->nrules = i;
12777 free_offload_policy(op);
12778 return (rc);
12779 }
12780
12781 /* Validate settings */
12782 s = &r->settings;
12783 if ((s->offload != 0 && s->offload != 1) ||
12784 s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
12785 s->sched_class < -1 ||
12786 s->sched_class >= sc->params.nsched_cls) {
12787 rc = EINVAL;
12788 goto error;
12789 }
12790
12791 bf = &r->bpf_prog;
12792 u = bf->bf_insns; /* userspace ptr */
12793 bf->bf_insns = NULL;
12794 if (bf->bf_len == 0) {
12795 /* legal, matches everything */
12796 continue;
12797 }
12798 len = bf->bf_len * sizeof(*bf->bf_insns);
12799 bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
12800 rc = copyin(u, bf->bf_insns, len);
12801 if (rc != 0)
12802 goto error;
12803
12804 if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
12805 rc = EINVAL;
12806 goto error;
12807 }
12808 }
12809 set_policy:
12810 rw_wlock(&sc->policy_lock);
12811 old = sc->policy;
12812 sc->policy = op;
12813 rw_wunlock(&sc->policy_lock);
12814 free_offload_policy(old);
12815
12816 return (0);
12817 }
12818
12819 #define MAX_READ_BUF_SIZE (128 * 1024)
12820 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)12821 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
12822 {
12823 uint32_t addr, remaining, n;
12824 uint32_t *buf;
12825 int rc;
12826 uint8_t *dst;
12827
12828 mtx_lock(&sc->reg_lock);
12829 if (hw_off_limits(sc))
12830 rc = ENXIO;
12831 else
12832 rc = validate_mem_range(sc, mr->addr, mr->len);
12833 mtx_unlock(&sc->reg_lock);
12834 if (rc != 0)
12835 return (rc);
12836
12837 buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
12838 addr = mr->addr;
12839 remaining = mr->len;
12840 dst = (void *)mr->data;
12841
12842 while (remaining) {
12843 n = min(remaining, MAX_READ_BUF_SIZE);
12844 mtx_lock(&sc->reg_lock);
12845 if (hw_off_limits(sc))
12846 rc = ENXIO;
12847 else
12848 read_via_memwin(sc, 2, addr, buf, n);
12849 mtx_unlock(&sc->reg_lock);
12850 if (rc != 0)
12851 break;
12852
12853 rc = copyout(buf, dst, n);
12854 if (rc != 0)
12855 break;
12856
12857 dst += n;
12858 remaining -= n;
12859 addr += n;
12860 }
12861
12862 free(buf, M_CXGBE);
12863 return (rc);
12864 }
12865 #undef MAX_READ_BUF_SIZE
12866
12867 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)12868 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
12869 {
12870 int rc;
12871
12872 if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
12873 return (EINVAL);
12874
12875 if (i2cd->len > sizeof(i2cd->data))
12876 return (EFBIG);
12877
12878 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
12879 if (rc)
12880 return (rc);
12881 if (hw_off_limits(sc))
12882 rc = ENXIO;
12883 else
12884 rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
12885 i2cd->offset, i2cd->len, &i2cd->data[0]);
12886 end_synchronized_op(sc, 0);
12887
12888 return (rc);
12889 }
12890
12891 static int
clear_stats(struct adapter * sc,u_int port_id)12892 clear_stats(struct adapter *sc, u_int port_id)
12893 {
12894 int i, v, chan_map;
12895 struct port_info *pi;
12896 struct vi_info *vi;
12897 struct sge_rxq *rxq;
12898 struct sge_txq *txq;
12899 struct sge_wrq *wrq;
12900 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12901 struct sge_ofld_txq *ofld_txq;
12902 #endif
12903 #ifdef TCP_OFFLOAD
12904 struct sge_ofld_rxq *ofld_rxq;
12905 #endif
12906
12907 if (port_id >= sc->params.nports)
12908 return (EINVAL);
12909 pi = sc->port[port_id];
12910 if (pi == NULL)
12911 return (EIO);
12912
12913 mtx_lock(&sc->reg_lock);
12914 if (!hw_off_limits(sc)) {
12915 /* MAC stats */
12916 t4_clr_port_stats(sc, pi->hw_port);
12917 if (is_t6(sc)) {
12918 if (pi->fcs_reg != -1)
12919 pi->fcs_base = t4_read_reg64(sc,
12920 t4_port_reg(sc, pi->tx_chan, pi->fcs_reg));
12921 else
12922 pi->stats.rx_fcs_err = 0;
12923 }
12924 for_each_vi(pi, v, vi) {
12925 if (vi->flags & VI_INIT_DONE)
12926 t4_clr_vi_stats(sc, vi->vin);
12927 }
12928 chan_map = pi->rx_e_chan_map;
12929 v = 0; /* reuse */
12930 while (chan_map) {
12931 i = ffs(chan_map) - 1;
12932 t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
12933 1, A_TP_MIB_TNL_CNG_DROP_0 + i);
12934 chan_map &= ~(1 << i);
12935 }
12936 }
12937 mtx_unlock(&sc->reg_lock);
12938 pi->tx_parse_error = 0;
12939 pi->tnl_cong_drops = 0;
12940
12941 /*
12942 * Since this command accepts a port, clear stats for
12943 * all VIs on this port.
12944 */
12945 for_each_vi(pi, v, vi) {
12946 if (vi->flags & VI_INIT_DONE) {
12947
12948 for_each_rxq(vi, i, rxq) {
12949 #if defined(INET) || defined(INET6)
12950 rxq->lro.lro_queued = 0;
12951 rxq->lro.lro_flushed = 0;
12952 #endif
12953 rxq->rxcsum = 0;
12954 rxq->vlan_extraction = 0;
12955 rxq->vxlan_rxcsum = 0;
12956
12957 rxq->fl.cl_allocated = 0;
12958 rxq->fl.cl_recycled = 0;
12959 rxq->fl.cl_fast_recycled = 0;
12960 }
12961
12962 for_each_txq(vi, i, txq) {
12963 txq->txcsum = 0;
12964 txq->tso_wrs = 0;
12965 txq->vlan_insertion = 0;
12966 txq->imm_wrs = 0;
12967 txq->sgl_wrs = 0;
12968 txq->txpkt_wrs = 0;
12969 txq->txpkts0_wrs = 0;
12970 txq->txpkts1_wrs = 0;
12971 txq->txpkts0_pkts = 0;
12972 txq->txpkts1_pkts = 0;
12973 txq->txpkts_flush = 0;
12974 txq->raw_wrs = 0;
12975 txq->vxlan_tso_wrs = 0;
12976 txq->vxlan_txcsum = 0;
12977 txq->kern_tls_records = 0;
12978 txq->kern_tls_short = 0;
12979 txq->kern_tls_partial = 0;
12980 txq->kern_tls_full = 0;
12981 txq->kern_tls_octets = 0;
12982 txq->kern_tls_waste = 0;
12983 txq->kern_tls_header = 0;
12984 txq->kern_tls_fin_short = 0;
12985 txq->kern_tls_cbc = 0;
12986 txq->kern_tls_gcm = 0;
12987 if (is_t6(sc)) {
12988 txq->kern_tls_options = 0;
12989 txq->kern_tls_fin = 0;
12990 } else {
12991 txq->kern_tls_ghash_received = 0;
12992 txq->kern_tls_ghash_requested = 0;
12993 txq->kern_tls_lso = 0;
12994 txq->kern_tls_partial_ghash = 0;
12995 txq->kern_tls_splitmode = 0;
12996 txq->kern_tls_trailer = 0;
12997 }
12998 mp_ring_reset_stats(txq->r);
12999 }
13000
13001 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
13002 for_each_ofld_txq(vi, i, ofld_txq) {
13003 ofld_txq->wrq.tx_wrs_direct = 0;
13004 ofld_txq->wrq.tx_wrs_copied = 0;
13005 counter_u64_zero(ofld_txq->tx_iscsi_pdus);
13006 counter_u64_zero(ofld_txq->tx_iscsi_octets);
13007 counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
13008 counter_u64_zero(ofld_txq->tx_nvme_pdus);
13009 counter_u64_zero(ofld_txq->tx_nvme_octets);
13010 counter_u64_zero(ofld_txq->tx_nvme_iso_wrs);
13011 counter_u64_zero(ofld_txq->tx_aio_jobs);
13012 counter_u64_zero(ofld_txq->tx_aio_octets);
13013 counter_u64_zero(ofld_txq->tx_toe_tls_records);
13014 counter_u64_zero(ofld_txq->tx_toe_tls_octets);
13015 }
13016 #endif
13017 #ifdef TCP_OFFLOAD
13018 for_each_ofld_rxq(vi, i, ofld_rxq) {
13019 ofld_rxq->fl.cl_allocated = 0;
13020 ofld_rxq->fl.cl_recycled = 0;
13021 ofld_rxq->fl.cl_fast_recycled = 0;
13022 counter_u64_zero(
13023 ofld_rxq->rx_iscsi_ddp_setup_ok);
13024 counter_u64_zero(
13025 ofld_rxq->rx_iscsi_ddp_setup_error);
13026 ofld_rxq->rx_iscsi_ddp_pdus = 0;
13027 ofld_rxq->rx_iscsi_ddp_octets = 0;
13028 ofld_rxq->rx_iscsi_fl_pdus = 0;
13029 ofld_rxq->rx_iscsi_fl_octets = 0;
13030 counter_u64_zero(
13031 ofld_rxq->rx_nvme_ddp_setup_ok);
13032 counter_u64_zero(
13033 ofld_rxq->rx_nvme_ddp_setup_no_stag);
13034 counter_u64_zero(
13035 ofld_rxq->rx_nvme_ddp_setup_error);
13036 counter_u64_zero(ofld_rxq->rx_nvme_ddp_pdus);
13037 counter_u64_zero(ofld_rxq->rx_nvme_ddp_octets);
13038 counter_u64_zero(ofld_rxq->rx_nvme_fl_pdus);
13039 counter_u64_zero(ofld_rxq->rx_nvme_fl_octets);
13040 counter_u64_zero(
13041 ofld_rxq->rx_nvme_invalid_headers);
13042 counter_u64_zero(
13043 ofld_rxq->rx_nvme_header_digest_errors);
13044 counter_u64_zero(
13045 ofld_rxq->rx_nvme_data_digest_errors);
13046 ofld_rxq->rx_aio_ddp_jobs = 0;
13047 ofld_rxq->rx_aio_ddp_octets = 0;
13048 ofld_rxq->rx_toe_tls_records = 0;
13049 ofld_rxq->rx_toe_tls_octets = 0;
13050 ofld_rxq->rx_toe_ddp_octets = 0;
13051 counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
13052 counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
13053 counter_u64_zero(ofld_rxq->ddp_buffer_free);
13054 }
13055 #endif
13056
13057 if (IS_MAIN_VI(vi)) {
13058 wrq = &sc->sge.ctrlq[pi->port_id];
13059 wrq->tx_wrs_direct = 0;
13060 wrq->tx_wrs_copied = 0;
13061 }
13062 }
13063 }
13064
13065 return (0);
13066 }
13067
13068 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)13069 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
13070 {
13071 #ifdef INET6
13072 struct in6_addr in6;
13073
13074 bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
13075 if (t4_get_clip_entry(sc, &in6, true) != NULL)
13076 return (0);
13077 else
13078 return (EIO);
13079 #else
13080 return (ENOTSUP);
13081 #endif
13082 }
13083
13084 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)13085 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
13086 {
13087 #ifdef INET6
13088 struct in6_addr in6;
13089
13090 bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
13091 return (t4_release_clip_addr(sc, &in6));
13092 #else
13093 return (ENOTSUP);
13094 #endif
13095 }
13096
13097 int
t4_os_find_pci_capability(struct adapter * sc,int cap)13098 t4_os_find_pci_capability(struct adapter *sc, int cap)
13099 {
13100 int i;
13101
13102 return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
13103 }
13104
13105 void
t4_os_portmod_changed(struct port_info * pi)13106 t4_os_portmod_changed(struct port_info *pi)
13107 {
13108 struct adapter *sc = pi->adapter;
13109 struct vi_info *vi;
13110 if_t ifp;
13111 static const char *mod_str[] = {
13112 NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM",
13113 "LR_SIMPLEX", "DR"
13114 };
13115
13116 KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
13117 ("%s: port_type %u", __func__, pi->port_type));
13118
13119 vi = &pi->vi[0];
13120 if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
13121 PORT_LOCK(pi);
13122 build_medialist(pi);
13123 if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
13124 fixup_link_config(pi);
13125 apply_link_config(pi);
13126 }
13127 PORT_UNLOCK(pi);
13128 end_synchronized_op(sc, LOCK_HELD);
13129 }
13130
13131 ifp = vi->ifp;
13132 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
13133 if_printf(ifp, "transceiver unplugged.\n");
13134 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
13135 if_printf(ifp, "unknown transceiver inserted.\n");
13136 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
13137 if_printf(ifp, "unsupported transceiver inserted.\n");
13138 else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
13139 if_printf(ifp, "%dGbps %s transceiver inserted.\n",
13140 port_top_speed(pi), mod_str[pi->mod_type]);
13141 } else {
13142 if_printf(ifp, "transceiver (type %d) inserted.\n",
13143 pi->mod_type);
13144 }
13145 }
13146
13147 void
t4_os_link_changed(struct port_info * pi)13148 t4_os_link_changed(struct port_info *pi)
13149 {
13150 struct vi_info *vi;
13151 if_t ifp;
13152 struct link_config *lc = &pi->link_cfg;
13153 struct adapter *sc = pi->adapter;
13154 int v;
13155
13156 PORT_LOCK_ASSERT_OWNED(pi);
13157
13158 if (is_t6(sc)) {
13159 if (lc->link_ok) {
13160 if (lc->speed > 25000 ||
13161 (lc->speed == 25000 && lc->fec == FEC_RS))
13162 pi->fcs_reg = A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS;
13163 else
13164 pi->fcs_reg = A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS;
13165 pi->fcs_base = t4_read_reg64(sc,
13166 t4_port_reg(sc, pi->tx_chan, pi->fcs_reg));
13167 pi->stats.rx_fcs_err = 0;
13168 } else {
13169 pi->fcs_reg = -1;
13170 }
13171 } else {
13172 MPASS(pi->fcs_reg != -1);
13173 MPASS(pi->fcs_base == 0);
13174 }
13175
13176 for_each_vi(pi, v, vi) {
13177 ifp = vi->ifp;
13178 if (ifp == NULL || IS_DETACHING(vi))
13179 continue;
13180
13181 if (lc->link_ok) {
13182 if_setbaudrate(ifp, IF_Mbps(lc->speed));
13183 if_link_state_change(ifp, LINK_STATE_UP);
13184 } else {
13185 if_link_state_change(ifp, LINK_STATE_DOWN);
13186 }
13187 }
13188 }
13189
13190 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)13191 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
13192 {
13193 struct adapter *sc;
13194
13195 sx_slock(&t4_list_lock);
13196 SLIST_FOREACH(sc, &t4_list, link) {
13197 /*
13198 * func should not make any assumptions about what state sc is
13199 * in - the only guarantee is that sc->sc_lock is a valid lock.
13200 */
13201 func(sc, arg);
13202 }
13203 sx_sunlock(&t4_list_lock);
13204 }
13205
13206 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)13207 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
13208 struct thread *td)
13209 {
13210 int rc;
13211 struct adapter *sc = dev->si_drv1;
13212
13213 rc = priv_check(td, PRIV_DRIVER);
13214 if (rc != 0)
13215 return (rc);
13216
13217 switch (cmd) {
13218 case CHELSIO_T4_GETREG: {
13219 struct t4_reg *edata = (struct t4_reg *)data;
13220
13221 if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
13222 return (EFAULT);
13223
13224 mtx_lock(&sc->reg_lock);
13225 if (hw_off_limits(sc))
13226 rc = ENXIO;
13227 else if (edata->size == 4)
13228 edata->val = t4_read_reg(sc, edata->addr);
13229 else if (edata->size == 8)
13230 edata->val = t4_read_reg64(sc, edata->addr);
13231 else
13232 rc = EINVAL;
13233 mtx_unlock(&sc->reg_lock);
13234
13235 break;
13236 }
13237 case CHELSIO_T4_SETREG: {
13238 struct t4_reg *edata = (struct t4_reg *)data;
13239
13240 if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
13241 return (EFAULT);
13242
13243 mtx_lock(&sc->reg_lock);
13244 if (hw_off_limits(sc))
13245 rc = ENXIO;
13246 else if (edata->size == 4) {
13247 if (edata->val & 0xffffffff00000000)
13248 rc = EINVAL;
13249 t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
13250 } else if (edata->size == 8)
13251 t4_write_reg64(sc, edata->addr, edata->val);
13252 else
13253 rc = EINVAL;
13254 mtx_unlock(&sc->reg_lock);
13255
13256 break;
13257 }
13258 case CHELSIO_T4_REGDUMP: {
13259 struct t4_regdump *regs = (struct t4_regdump *)data;
13260 int reglen = t4_get_regs_len(sc);
13261 uint8_t *buf;
13262
13263 if (regs->len < reglen) {
13264 regs->len = reglen; /* hint to the caller */
13265 return (ENOBUFS);
13266 }
13267
13268 regs->len = reglen;
13269 buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
13270 mtx_lock(&sc->reg_lock);
13271 if (hw_off_limits(sc))
13272 rc = ENXIO;
13273 else
13274 get_regs(sc, regs, buf);
13275 mtx_unlock(&sc->reg_lock);
13276 if (rc == 0)
13277 rc = copyout(buf, regs->data, reglen);
13278 free(buf, M_CXGBE);
13279 break;
13280 }
13281 case CHELSIO_T4_GET_FILTER_MODE:
13282 rc = get_filter_mode(sc, (uint32_t *)data);
13283 break;
13284 case CHELSIO_T4_SET_FILTER_MODE:
13285 rc = set_filter_mode(sc, *(uint32_t *)data);
13286 break;
13287 case CHELSIO_T4_SET_FILTER_MASK:
13288 rc = set_filter_mask(sc, *(uint32_t *)data);
13289 break;
13290 case CHELSIO_T4_GET_FILTER:
13291 rc = get_filter(sc, (struct t4_filter *)data);
13292 break;
13293 case CHELSIO_T4_SET_FILTER:
13294 rc = set_filter(sc, (struct t4_filter *)data);
13295 break;
13296 case CHELSIO_T4_DEL_FILTER:
13297 rc = del_filter(sc, (struct t4_filter *)data);
13298 break;
13299 case CHELSIO_T4_GET_SGE_CONTEXT: {
13300 struct t4_sge_context *ctxt = (struct t4_sge_context *)data;
13301
13302 rc = get_sge_context(sc, ctxt->mem_id, ctxt->cid,
13303 sizeof(ctxt->data), &ctxt->data[0]);
13304 break;
13305 }
13306 case CHELSIO_T4_LOAD_FW:
13307 rc = load_fw(sc, (struct t4_data *)data);
13308 break;
13309 case CHELSIO_T4_GET_MEM:
13310 rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
13311 break;
13312 case CHELSIO_T4_GET_I2C:
13313 rc = read_i2c(sc, (struct t4_i2c_data *)data);
13314 break;
13315 case CHELSIO_T4_CLEAR_STATS:
13316 rc = clear_stats(sc, *(uint32_t *)data);
13317 break;
13318 case CHELSIO_T4_SCHED_CLASS:
13319 rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
13320 break;
13321 case CHELSIO_T4_SCHED_QUEUE:
13322 rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
13323 break;
13324 case CHELSIO_T4_GET_TRACER:
13325 rc = t4_get_tracer(sc, (struct t4_tracer *)data);
13326 break;
13327 case CHELSIO_T4_SET_TRACER:
13328 rc = t4_set_tracer(sc, (struct t4_tracer *)data);
13329 break;
13330 case CHELSIO_T4_LOAD_CFG:
13331 rc = load_cfg(sc, (struct t4_data *)data);
13332 break;
13333 case CHELSIO_T4_LOAD_BOOT:
13334 rc = load_boot(sc, (struct t4_bootrom *)data);
13335 break;
13336 case CHELSIO_T4_LOAD_BOOTCFG:
13337 rc = load_bootcfg(sc, (struct t4_data *)data);
13338 break;
13339 case CHELSIO_T4_CUDBG_DUMP:
13340 rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
13341 break;
13342 case CHELSIO_T4_SET_OFLD_POLICY:
13343 rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
13344 break;
13345 case CHELSIO_T4_HOLD_CLIP_ADDR:
13346 rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
13347 break;
13348 case CHELSIO_T4_RELEASE_CLIP_ADDR:
13349 rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
13350 break;
13351 case CHELSIO_T4_GET_SGE_CTXT: {
13352 struct t4_sge_ctxt *ctxt = (struct t4_sge_ctxt *)data;
13353
13354 rc = get_sge_context(sc, ctxt->mem_id, ctxt->cid,
13355 sizeof(ctxt->data), &ctxt->data[0]);
13356 break;
13357 }
13358 default:
13359 rc = ENOTTY;
13360 }
13361
13362 return (rc);
13363 }
13364
13365 #ifdef TCP_OFFLOAD
13366 int
toe_capability(struct vi_info * vi,bool enable)13367 toe_capability(struct vi_info *vi, bool enable)
13368 {
13369 int rc;
13370 struct port_info *pi = vi->pi;
13371 struct adapter *sc = pi->adapter;
13372
13373 ASSERT_SYNCHRONIZED_OP(sc);
13374
13375 if (!is_offload(sc))
13376 return (ENODEV);
13377 if (!hw_all_ok(sc))
13378 return (ENXIO);
13379
13380 if (enable) {
13381 #ifdef KERN_TLS
13382 if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
13383 int i, j, n;
13384 struct port_info *p;
13385 struct vi_info *v;
13386
13387 /*
13388 * Reconfigure hardware for TOE if TXTLS is not enabled
13389 * on any ifnet.
13390 */
13391 n = 0;
13392 for_each_port(sc, i) {
13393 p = sc->port[i];
13394 for_each_vi(p, j, v) {
13395 if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
13396 CH_WARN(sc,
13397 "%s has NIC TLS enabled.\n",
13398 device_get_nameunit(v->dev));
13399 n++;
13400 }
13401 }
13402 }
13403 if (n > 0) {
13404 CH_WARN(sc, "Disable NIC TLS on all interfaces "
13405 "associated with this adapter before "
13406 "trying to enable TOE.\n");
13407 return (EAGAIN);
13408 }
13409 rc = t6_config_kern_tls(sc, false);
13410 if (rc)
13411 return (rc);
13412 }
13413 #endif
13414 if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
13415 /* TOE is already enabled. */
13416 return (0);
13417 }
13418
13419 /*
13420 * We need the port's queues around so that we're able to send
13421 * and receive CPLs to/from the TOE even if the ifnet for this
13422 * port has never been UP'd administratively.
13423 */
13424 if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
13425 return (rc);
13426 if (!(pi->vi[0].flags & VI_INIT_DONE) &&
13427 ((rc = vi_init(&pi->vi[0])) != 0))
13428 return (rc);
13429
13430 if (isset(&sc->offload_map, pi->port_id)) {
13431 /* TOE is enabled on another VI of this port. */
13432 MPASS(pi->uld_vis > 0);
13433 pi->uld_vis++;
13434 return (0);
13435 }
13436
13437 if (!uld_active(sc, ULD_TOM)) {
13438 rc = t4_activate_uld(sc, ULD_TOM);
13439 if (rc == EAGAIN) {
13440 log(LOG_WARNING,
13441 "You must kldload t4_tom.ko before trying "
13442 "to enable TOE on a cxgbe interface.\n");
13443 }
13444 if (rc != 0)
13445 return (rc);
13446 KASSERT(sc->tom_softc != NULL,
13447 ("%s: TOM activated but softc NULL", __func__));
13448 KASSERT(uld_active(sc, ULD_TOM),
13449 ("%s: TOM activated but flag not set", __func__));
13450 }
13451
13452 /*
13453 * Activate iWARP, iSCSI, and NVMe too, if the modules
13454 * are loaded.
13455 */
13456 if (!uld_active(sc, ULD_IWARP))
13457 (void) t4_activate_uld(sc, ULD_IWARP);
13458 if (!uld_active(sc, ULD_ISCSI))
13459 (void) t4_activate_uld(sc, ULD_ISCSI);
13460 if (!uld_active(sc, ULD_NVME))
13461 (void) t4_activate_uld(sc, ULD_NVME);
13462
13463 if (pi->uld_vis++ == 0)
13464 setbit(&sc->offload_map, pi->port_id);
13465 } else {
13466 if ((if_getcapenable(vi->ifp) & IFCAP_TOE) == 0) {
13467 /* TOE is already disabled. */
13468 return (0);
13469 }
13470 MPASS(isset(&sc->offload_map, pi->port_id));
13471 MPASS(pi->uld_vis > 0);
13472 if (--pi->uld_vis == 0)
13473 clrbit(&sc->offload_map, pi->port_id);
13474 }
13475
13476 return (0);
13477 }
13478
13479 /*
13480 * Add an upper layer driver to the global list.
13481 */
13482 int
t4_register_uld(struct uld_info * ui,int id)13483 t4_register_uld(struct uld_info *ui, int id)
13484 {
13485 int rc;
13486
13487 if (id < 0 || id > ULD_MAX)
13488 return (EINVAL);
13489 sx_xlock(&t4_uld_list_lock);
13490 if (t4_uld_list[id] != NULL)
13491 rc = EEXIST;
13492 else {
13493 t4_uld_list[id] = ui;
13494 rc = 0;
13495 }
13496 sx_xunlock(&t4_uld_list_lock);
13497 return (rc);
13498 }
13499
13500 int
t4_unregister_uld(struct uld_info * ui,int id)13501 t4_unregister_uld(struct uld_info *ui, int id)
13502 {
13503
13504 if (id < 0 || id > ULD_MAX)
13505 return (EINVAL);
13506 sx_xlock(&t4_uld_list_lock);
13507 MPASS(t4_uld_list[id] == ui);
13508 t4_uld_list[id] = NULL;
13509 sx_xunlock(&t4_uld_list_lock);
13510 return (0);
13511 }
13512
13513 int
t4_activate_uld(struct adapter * sc,int id)13514 t4_activate_uld(struct adapter *sc, int id)
13515 {
13516 int rc;
13517
13518 ASSERT_SYNCHRONIZED_OP(sc);
13519
13520 if (id < 0 || id > ULD_MAX)
13521 return (EINVAL);
13522
13523 /* Adapter needs to be initialized before any ULD can be activated. */
13524 if (!(sc->flags & FULL_INIT_DONE)) {
13525 rc = adapter_init(sc);
13526 if (rc != 0)
13527 return (rc);
13528 }
13529
13530 sx_slock(&t4_uld_list_lock);
13531 if (t4_uld_list[id] == NULL)
13532 rc = EAGAIN; /* load the KLD with this ULD and try again. */
13533 else {
13534 rc = t4_uld_list[id]->uld_activate(sc);
13535 if (rc == 0)
13536 setbit(&sc->active_ulds, id);
13537 }
13538 sx_sunlock(&t4_uld_list_lock);
13539
13540 return (rc);
13541 }
13542
13543 int
t4_deactivate_uld(struct adapter * sc,int id)13544 t4_deactivate_uld(struct adapter *sc, int id)
13545 {
13546 int rc;
13547
13548 ASSERT_SYNCHRONIZED_OP(sc);
13549
13550 if (id < 0 || id > ULD_MAX)
13551 return (EINVAL);
13552
13553 sx_slock(&t4_uld_list_lock);
13554 if (t4_uld_list[id] == NULL)
13555 rc = ENXIO;
13556 else {
13557 rc = t4_uld_list[id]->uld_deactivate(sc);
13558 if (rc == 0)
13559 clrbit(&sc->active_ulds, id);
13560 }
13561 sx_sunlock(&t4_uld_list_lock);
13562
13563 return (rc);
13564 }
13565
13566 static int
deactivate_all_uld(struct adapter * sc)13567 deactivate_all_uld(struct adapter *sc)
13568 {
13569 int i, rc;
13570
13571 rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
13572 if (rc != 0)
13573 return (ENXIO);
13574 sx_slock(&t4_uld_list_lock);
13575 for (i = 0; i <= ULD_MAX; i++) {
13576 if (t4_uld_list[i] == NULL || !uld_active(sc, i))
13577 continue;
13578 rc = t4_uld_list[i]->uld_deactivate(sc);
13579 if (rc != 0)
13580 break;
13581 clrbit(&sc->active_ulds, i);
13582 }
13583 sx_sunlock(&t4_uld_list_lock);
13584 end_synchronized_op(sc, 0);
13585
13586 return (rc);
13587 }
13588
13589 static void
stop_all_uld(struct adapter * sc)13590 stop_all_uld(struct adapter *sc)
13591 {
13592 int i;
13593
13594 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
13595 return;
13596 sx_slock(&t4_uld_list_lock);
13597 for (i = 0; i <= ULD_MAX; i++) {
13598 if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
13599 t4_uld_list[i]->uld_stop == NULL)
13600 continue;
13601 (void) t4_uld_list[i]->uld_stop(sc);
13602 }
13603 sx_sunlock(&t4_uld_list_lock);
13604 end_synchronized_op(sc, 0);
13605 }
13606
13607 static void
restart_all_uld(struct adapter * sc)13608 restart_all_uld(struct adapter *sc)
13609 {
13610 int i;
13611
13612 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
13613 return;
13614 sx_slock(&t4_uld_list_lock);
13615 for (i = 0; i <= ULD_MAX; i++) {
13616 if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
13617 t4_uld_list[i]->uld_restart == NULL)
13618 continue;
13619 (void) t4_uld_list[i]->uld_restart(sc);
13620 }
13621 sx_sunlock(&t4_uld_list_lock);
13622 end_synchronized_op(sc, 0);
13623 }
13624
13625 int
uld_active(struct adapter * sc,int id)13626 uld_active(struct adapter *sc, int id)
13627 {
13628
13629 MPASS(id >= 0 && id <= ULD_MAX);
13630
13631 return (isset(&sc->active_ulds, id));
13632 }
13633 #endif
13634
13635 #ifdef KERN_TLS
13636 static int
ktls_capability(struct adapter * sc,bool enable)13637 ktls_capability(struct adapter *sc, bool enable)
13638 {
13639 ASSERT_SYNCHRONIZED_OP(sc);
13640
13641 if (!is_ktls(sc))
13642 return (ENODEV);
13643 if (!is_t6(sc))
13644 return (0);
13645 if (!hw_all_ok(sc))
13646 return (ENXIO);
13647
13648 if (enable) {
13649 if (sc->flags & KERN_TLS_ON)
13650 return (0); /* already on */
13651 if (sc->offload_map != 0) {
13652 CH_WARN(sc,
13653 "Disable TOE on all interfaces associated with "
13654 "this adapter before trying to enable NIC TLS.\n");
13655 return (EAGAIN);
13656 }
13657 return (t6_config_kern_tls(sc, true));
13658 } else {
13659 /*
13660 * Nothing to do for disable. If TOE is enabled sometime later
13661 * then toe_capability will reconfigure the hardware.
13662 */
13663 return (0);
13664 }
13665 }
13666 #endif
13667
13668 /*
13669 * t = ptr to tunable.
13670 * nc = number of CPUs.
13671 * c = compiled in default for that tunable.
13672 */
13673 static void
calculate_nqueues(int * t,int nc,const int c)13674 calculate_nqueues(int *t, int nc, const int c)
13675 {
13676 int nq;
13677
13678 if (*t > 0)
13679 return;
13680 nq = *t < 0 ? -*t : c;
13681 *t = min(nc, nq);
13682 }
13683
13684 /*
13685 * Come up with reasonable defaults for some of the tunables, provided they're
13686 * not set by the user (in which case we'll use the values as is).
13687 */
13688 static void
tweak_tunables(void)13689 tweak_tunables(void)
13690 {
13691 int nc = mp_ncpus; /* our snapshot of the number of CPUs */
13692
13693 if (t4_ntxq < 1) {
13694 #ifdef RSS
13695 t4_ntxq = rss_getnumbuckets();
13696 #else
13697 calculate_nqueues(&t4_ntxq, nc, NTXQ);
13698 #endif
13699 }
13700
13701 calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
13702
13703 if (t4_nrxq < 1) {
13704 #ifdef RSS
13705 t4_nrxq = rss_getnumbuckets();
13706 #else
13707 calculate_nqueues(&t4_nrxq, nc, NRXQ);
13708 #endif
13709 }
13710
13711 calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
13712
13713 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
13714 calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
13715 calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
13716 #endif
13717 #ifdef TCP_OFFLOAD
13718 calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
13719 calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
13720 #endif
13721
13722 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
13723 if (t4_toecaps_allowed == -1)
13724 t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
13725 #else
13726 if (t4_toecaps_allowed == -1)
13727 t4_toecaps_allowed = 0;
13728 #endif
13729
13730 #ifdef TCP_OFFLOAD
13731 if (t4_rdmacaps_allowed == -1) {
13732 t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
13733 FW_CAPS_CONFIG_RDMA_RDMAC;
13734 }
13735
13736 if (t4_iscsicaps_allowed == -1) {
13737 t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
13738 FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
13739 FW_CAPS_CONFIG_ISCSI_T10DIF;
13740 }
13741
13742 if (t4_nvmecaps_allowed == -1)
13743 t4_nvmecaps_allowed = FW_CAPS_CONFIG_NVME_TCP;
13744
13745 if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
13746 t4_tmr_idx_ofld = TMR_IDX_OFLD;
13747
13748 if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
13749 t4_pktc_idx_ofld = PKTC_IDX_OFLD;
13750 #else
13751 if (t4_rdmacaps_allowed == -1)
13752 t4_rdmacaps_allowed = 0;
13753
13754 if (t4_iscsicaps_allowed == -1)
13755 t4_iscsicaps_allowed = 0;
13756
13757 if (t4_nvmecaps_allowed == -1)
13758 t4_nvmecaps_allowed = 0;
13759 #endif
13760
13761 #ifdef DEV_NETMAP
13762 calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
13763 calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
13764 calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
13765 calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
13766 #endif
13767
13768 if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
13769 t4_tmr_idx = TMR_IDX;
13770
13771 if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
13772 t4_pktc_idx = PKTC_IDX;
13773
13774 if (t4_qsize_txq < 128)
13775 t4_qsize_txq = 128;
13776
13777 if (t4_qsize_rxq < 128)
13778 t4_qsize_rxq = 128;
13779 while (t4_qsize_rxq & 7)
13780 t4_qsize_rxq++;
13781
13782 t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
13783
13784 /*
13785 * Number of VIs to create per-port. The first VI is the "main" regular
13786 * VI for the port. The rest are additional virtual interfaces on the
13787 * same physical port. Note that the main VI does not have native
13788 * netmap support but the extra VIs do.
13789 *
13790 * Limit the number of VIs per port to the number of available
13791 * MAC addresses per port.
13792 */
13793 if (t4_num_vis < 1)
13794 t4_num_vis = 1;
13795 if (t4_num_vis > nitems(vi_mac_funcs)) {
13796 t4_num_vis = nitems(vi_mac_funcs);
13797 printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
13798 }
13799
13800 if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
13801 pcie_relaxed_ordering = 1;
13802 #if defined(__i386__) || defined(__amd64__)
13803 if (cpu_vendor_id == CPU_VENDOR_INTEL)
13804 pcie_relaxed_ordering = 0;
13805 #endif
13806 }
13807 }
13808
13809 #ifdef DDB
13810 static void
t4_dump_mem(struct adapter * sc,u_int addr,u_int len)13811 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
13812 {
13813 uint32_t base, j, off, pf, reg, save, win_pos;
13814
13815 reg = chip_id(sc) > CHELSIO_T6 ?
13816 PCIE_MEM_ACCESS_T7_REG(A_PCIE_MEM_ACCESS_OFFSET0, 2) :
13817 PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
13818 save = t4_read_reg(sc, reg);
13819 base = sc->memwin[2].mw_base;
13820
13821 if (is_t4(sc)) {
13822 pf = 0;
13823 win_pos = addr & ~0xf; /* start must be 16B aligned */
13824 } else {
13825 pf = V_PFNUM(sc->pf);
13826 win_pos = addr & ~0x7f; /* start must be 128B aligned */
13827 }
13828 off = addr - win_pos;
13829 if (chip_id(sc) > CHELSIO_T6)
13830 win_pos >>= X_T7_MEMOFST_SHIFT;
13831 t4_write_reg(sc, reg, win_pos | pf);
13832 t4_read_reg(sc, reg);
13833
13834 while (len > 0 && !db_pager_quit) {
13835 uint32_t buf[8];
13836 for (j = 0; j < 8; j++, off += 4)
13837 buf[j] = htonl(t4_read_reg(sc, base + off));
13838
13839 db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
13840 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
13841 buf[7]);
13842 if (len <= sizeof(buf))
13843 len = 0;
13844 else
13845 len -= sizeof(buf);
13846 }
13847
13848 t4_write_reg(sc, reg, save);
13849 t4_read_reg(sc, reg);
13850 }
13851
13852 static void
t4_dump_tcb(struct adapter * sc,int tid)13853 t4_dump_tcb(struct adapter *sc, int tid)
13854 {
13855 uint32_t tcb_addr;
13856
13857 /* Dump TCB for the tid */
13858 tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
13859 tcb_addr += tid * TCB_SIZE;
13860 t4_dump_mem(sc, tcb_addr, TCB_SIZE);
13861 }
13862
13863 static void
t4_dump_devlog(struct adapter * sc)13864 t4_dump_devlog(struct adapter *sc)
13865 {
13866 struct devlog_params *dparams = &sc->params.devlog;
13867 struct fw_devlog_e e;
13868 int i, first, j, m, nentries, rc;
13869 uint64_t ftstamp = UINT64_MAX;
13870
13871 if (dparams->start == 0) {
13872 db_printf("devlog params not valid\n");
13873 return;
13874 }
13875
13876 nentries = dparams->size / sizeof(struct fw_devlog_e);
13877 m = fwmtype_to_hwmtype(dparams->memtype);
13878
13879 /* Find the first entry. */
13880 first = -1;
13881 for (i = 0; i < nentries && !db_pager_quit; i++) {
13882 rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
13883 sizeof(e), (void *)&e);
13884 if (rc != 0)
13885 break;
13886
13887 if (e.timestamp == 0)
13888 break;
13889
13890 e.timestamp = be64toh(e.timestamp);
13891 if (e.timestamp < ftstamp) {
13892 ftstamp = e.timestamp;
13893 first = i;
13894 }
13895 }
13896
13897 if (first == -1)
13898 return;
13899
13900 i = first;
13901 do {
13902 rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
13903 sizeof(e), (void *)&e);
13904 if (rc != 0)
13905 return;
13906
13907 if (e.timestamp == 0)
13908 return;
13909
13910 e.timestamp = be64toh(e.timestamp);
13911 e.seqno = be32toh(e.seqno);
13912 for (j = 0; j < 8; j++)
13913 e.params[j] = be32toh(e.params[j]);
13914
13915 db_printf("%10d %15ju %8s %8s ",
13916 e.seqno, e.timestamp,
13917 (e.level < nitems(devlog_level_strings) ?
13918 devlog_level_strings[e.level] : "UNKNOWN"),
13919 (e.facility < nitems(devlog_facility_strings) ?
13920 devlog_facility_strings[e.facility] : "UNKNOWN"));
13921 db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
13922 e.params[3], e.params[4], e.params[5], e.params[6],
13923 e.params[7]);
13924
13925 if (++i == nentries)
13926 i = 0;
13927 } while (i != first && !db_pager_quit);
13928 }
13929
13930 static DB_DEFINE_TABLE(show, t4, show_t4);
13931
DB_TABLE_COMMAND_FLAGS(show_t4,devlog,db_show_devlog,CS_OWN)13932 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
13933 {
13934 device_t dev;
13935 int t;
13936 bool valid;
13937
13938 valid = false;
13939 t = db_read_token();
13940 if (t == tIDENT) {
13941 dev = device_lookup_by_name(db_tok_string);
13942 valid = true;
13943 }
13944 db_skip_to_eol();
13945 if (!valid) {
13946 db_printf("usage: show t4 devlog <nexus>\n");
13947 return;
13948 }
13949
13950 if (dev == NULL) {
13951 db_printf("device not found\n");
13952 return;
13953 }
13954
13955 t4_dump_devlog(device_get_softc(dev));
13956 }
13957
DB_TABLE_COMMAND_FLAGS(show_t4,tcb,db_show_t4tcb,CS_OWN)13958 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
13959 {
13960 device_t dev;
13961 int radix, tid, t;
13962 bool valid;
13963
13964 valid = false;
13965 radix = db_radix;
13966 db_radix = 10;
13967 t = db_read_token();
13968 if (t == tIDENT) {
13969 dev = device_lookup_by_name(db_tok_string);
13970 t = db_read_token();
13971 if (t == tNUMBER) {
13972 tid = db_tok_number;
13973 valid = true;
13974 }
13975 }
13976 db_radix = radix;
13977 db_skip_to_eol();
13978 if (!valid) {
13979 db_printf("usage: show t4 tcb <nexus> <tid>\n");
13980 return;
13981 }
13982
13983 if (dev == NULL) {
13984 db_printf("device not found\n");
13985 return;
13986 }
13987 if (tid < 0) {
13988 db_printf("invalid tid\n");
13989 return;
13990 }
13991
13992 t4_dump_tcb(device_get_softc(dev), tid);
13993 }
13994
DB_TABLE_COMMAND_FLAGS(show_t4,memdump,db_show_memdump,CS_OWN)13995 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
13996 {
13997 device_t dev;
13998 int radix, t;
13999 bool valid;
14000
14001 valid = false;
14002 radix = db_radix;
14003 db_radix = 10;
14004 t = db_read_token();
14005 if (t == tIDENT) {
14006 dev = device_lookup_by_name(db_tok_string);
14007 t = db_read_token();
14008 if (t == tNUMBER) {
14009 addr = db_tok_number;
14010 t = db_read_token();
14011 if (t == tNUMBER) {
14012 count = db_tok_number;
14013 valid = true;
14014 }
14015 }
14016 }
14017 db_radix = radix;
14018 db_skip_to_eol();
14019 if (!valid) {
14020 db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
14021 return;
14022 }
14023
14024 if (dev == NULL) {
14025 db_printf("device not found\n");
14026 return;
14027 }
14028 if (addr < 0) {
14029 db_printf("invalid address\n");
14030 return;
14031 }
14032 if (count <= 0) {
14033 db_printf("invalid length\n");
14034 return;
14035 }
14036
14037 t4_dump_mem(device_get_softc(dev), addr, count);
14038 }
14039 #endif
14040
14041 static eventhandler_tag vxlan_start_evtag;
14042 static eventhandler_tag vxlan_stop_evtag;
14043
14044 struct vxlan_evargs {
14045 if_t ifp;
14046 uint16_t port;
14047 };
14048
14049 static void
enable_vxlan_rx(struct adapter * sc)14050 enable_vxlan_rx(struct adapter *sc)
14051 {
14052 int i, rc;
14053 struct port_info *pi;
14054 uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
14055
14056 ASSERT_SYNCHRONIZED_OP(sc);
14057
14058 t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
14059 F_VXLAN_EN);
14060 for_each_port(sc, i) {
14061 pi = sc->port[i];
14062 if (pi->vxlan_tcam_entry == true)
14063 continue;
14064 rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
14065 match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
14066 true);
14067 if (rc < 0) {
14068 rc = -rc;
14069 CH_ERR(&pi->vi[0],
14070 "failed to add VXLAN TCAM entry: %d.\n", rc);
14071 } else {
14072 MPASS(rc == sc->rawf_base + pi->port_id);
14073 pi->vxlan_tcam_entry = true;
14074 }
14075 }
14076 }
14077
14078 static void
t4_vxlan_start(struct adapter * sc,void * arg)14079 t4_vxlan_start(struct adapter *sc, void *arg)
14080 {
14081 struct vxlan_evargs *v = arg;
14082
14083 if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
14084 return;
14085 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
14086 return;
14087
14088 if (sc->vxlan_refcount == 0) {
14089 sc->vxlan_port = v->port;
14090 sc->vxlan_refcount = 1;
14091 if (!hw_off_limits(sc))
14092 enable_vxlan_rx(sc);
14093 } else if (sc->vxlan_port == v->port) {
14094 sc->vxlan_refcount++;
14095 } else {
14096 CH_ERR(sc, "VXLAN already configured on port %d; "
14097 "ignoring attempt to configure it on port %d\n",
14098 sc->vxlan_port, v->port);
14099 }
14100 end_synchronized_op(sc, 0);
14101 }
14102
14103 static void
t4_vxlan_stop(struct adapter * sc,void * arg)14104 t4_vxlan_stop(struct adapter *sc, void *arg)
14105 {
14106 struct vxlan_evargs *v = arg;
14107
14108 if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
14109 return;
14110 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
14111 return;
14112
14113 /*
14114 * VXLANs may have been configured before the driver was loaded so we
14115 * may see more stops than starts. This is not handled cleanly but at
14116 * least we keep the refcount sane.
14117 */
14118 if (sc->vxlan_port != v->port)
14119 goto done;
14120 if (sc->vxlan_refcount == 0) {
14121 CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
14122 "ignoring attempt to stop it again.\n", sc->vxlan_port);
14123 } else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
14124 t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
14125 done:
14126 end_synchronized_op(sc, 0);
14127 }
14128
14129 static void
t4_vxlan_start_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)14130 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
14131 sa_family_t family, u_int port)
14132 {
14133 struct vxlan_evargs v;
14134
14135 MPASS(family == AF_INET || family == AF_INET6);
14136 v.ifp = ifp;
14137 v.port = port;
14138
14139 t4_iterate(t4_vxlan_start, &v);
14140 }
14141
14142 static void
t4_vxlan_stop_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)14143 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
14144 u_int port)
14145 {
14146 struct vxlan_evargs v;
14147
14148 MPASS(family == AF_INET || family == AF_INET6);
14149 v.ifp = ifp;
14150 v.port = port;
14151
14152 t4_iterate(t4_vxlan_stop, &v);
14153 }
14154
14155
14156 static struct sx mlu; /* mod load unload */
14157 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
14158
14159 static int
mod_event(module_t mod,int cmd,void * arg)14160 mod_event(module_t mod, int cmd, void *arg)
14161 {
14162 int rc = 0;
14163 static int loaded = 0;
14164
14165 switch (cmd) {
14166 case MOD_LOAD:
14167 sx_xlock(&mlu);
14168 if (loaded++ == 0) {
14169 t4_sge_modload();
14170 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
14171 t4_filter_rpl, CPL_COOKIE_FILTER);
14172 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
14173 do_l2t_write_rpl, CPL_COOKIE_FILTER);
14174 t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
14175 t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
14176 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
14177 t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
14178 t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
14179 t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
14180 t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
14181 t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
14182 t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
14183 do_smt_write_rpl);
14184 sx_init(&t4_list_lock, "T4/T5 adapters");
14185 SLIST_INIT(&t4_list);
14186 callout_init(&fatal_callout, 1);
14187 #ifdef TCP_OFFLOAD
14188 sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
14189 #endif
14190 #ifdef INET6
14191 t4_clip_modload();
14192 #endif
14193 #ifdef KERN_TLS
14194 t6_ktls_modload();
14195 t7_ktls_modload();
14196 #endif
14197 t4_tracer_modload();
14198 tweak_tunables();
14199 vxlan_start_evtag =
14200 EVENTHANDLER_REGISTER(vxlan_start,
14201 t4_vxlan_start_handler, NULL,
14202 EVENTHANDLER_PRI_ANY);
14203 vxlan_stop_evtag =
14204 EVENTHANDLER_REGISTER(vxlan_stop,
14205 t4_vxlan_stop_handler, NULL,
14206 EVENTHANDLER_PRI_ANY);
14207 reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
14208 taskqueue_thread_enqueue, &reset_tq);
14209 taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
14210 "t4_rst_thr");
14211 }
14212 sx_xunlock(&mlu);
14213 break;
14214
14215 case MOD_UNLOAD:
14216 sx_xlock(&mlu);
14217 if (--loaded == 0) {
14218 #ifdef TCP_OFFLOAD
14219 int i;
14220 #endif
14221 int tries;
14222
14223 taskqueue_free(reset_tq);
14224
14225 tries = 0;
14226 while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
14227 uprintf("%ju clusters with custom free routine "
14228 "still is use.\n", t4_sge_extfree_refs());
14229 pause("t4unload", 2 * hz);
14230 }
14231
14232 sx_slock(&t4_list_lock);
14233 if (!SLIST_EMPTY(&t4_list)) {
14234 rc = EBUSY;
14235 sx_sunlock(&t4_list_lock);
14236 goto done_unload;
14237 }
14238 #ifdef TCP_OFFLOAD
14239 sx_slock(&t4_uld_list_lock);
14240 for (i = 0; i <= ULD_MAX; i++) {
14241 if (t4_uld_list[i] != NULL) {
14242 rc = EBUSY;
14243 sx_sunlock(&t4_uld_list_lock);
14244 sx_sunlock(&t4_list_lock);
14245 goto done_unload;
14246 }
14247 }
14248 sx_sunlock(&t4_uld_list_lock);
14249 #endif
14250 sx_sunlock(&t4_list_lock);
14251
14252 if (t4_sge_extfree_refs() == 0) {
14253 EVENTHANDLER_DEREGISTER(vxlan_start,
14254 vxlan_start_evtag);
14255 EVENTHANDLER_DEREGISTER(vxlan_stop,
14256 vxlan_stop_evtag);
14257 t4_tracer_modunload();
14258 #ifdef KERN_TLS
14259 t7_ktls_modunload();
14260 t6_ktls_modunload();
14261 #endif
14262 #ifdef INET6
14263 t4_clip_modunload();
14264 #endif
14265 #ifdef TCP_OFFLOAD
14266 sx_destroy(&t4_uld_list_lock);
14267 #endif
14268 sx_destroy(&t4_list_lock);
14269 t4_sge_modunload();
14270 loaded = 0;
14271 } else {
14272 rc = EBUSY;
14273 loaded++; /* undo earlier decrement */
14274 }
14275 }
14276 done_unload:
14277 sx_xunlock(&mlu);
14278 break;
14279 }
14280
14281 return (rc);
14282 }
14283
14284 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
14285 MODULE_VERSION(t4nex, 1);
14286 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
14287 #ifdef DEV_NETMAP
14288 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
14289 #endif /* DEV_NETMAP */
14290
14291 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
14292 MODULE_VERSION(t5nex, 1);
14293 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
14294 #ifdef DEV_NETMAP
14295 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
14296 #endif /* DEV_NETMAP */
14297
14298 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
14299 MODULE_VERSION(t6nex, 1);
14300 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
14301 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
14302 #ifdef DEV_NETMAP
14303 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
14304 #endif /* DEV_NETMAP */
14305
14306 DRIVER_MODULE(chnex, pci, ch_driver, mod_event, 0);
14307 MODULE_VERSION(chnex, 1);
14308 MODULE_DEPEND(chnex, crypto, 1, 1, 1);
14309 MODULE_DEPEND(chnex, firmware, 1, 1, 1);
14310 #ifdef DEV_NETMAP
14311 MODULE_DEPEND(chnex, netmap, 1, 1, 1);
14312 #endif /* DEV_NETMAP */
14313
14314 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
14315 MODULE_VERSION(cxgbe, 1);
14316
14317 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
14318 MODULE_VERSION(cxl, 1);
14319
14320 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
14321 MODULE_VERSION(cc, 1);
14322
14323 DRIVER_MODULE(che, chnex, che_driver, 0, 0);
14324 MODULE_VERSION(che, 1);
14325
14326 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
14327 MODULE_VERSION(vcxgbe, 1);
14328
14329 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
14330 MODULE_VERSION(vcxl, 1);
14331
14332 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
14333 MODULE_VERSION(vcc, 1);
14334
14335 DRIVER_MODULE(vche, che, vche_driver, 0, 0);
14336 MODULE_VERSION(vche, 1);
14337