xref: /freebsd-11-stable/sys/dev/cxgbe/t4_main.c (revision d074191a2d04bff9ead56a5c3f276f8c06f8ebe9)
1 /*-
2  * Copyright (c) 2011 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_ddb.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_rss.h"
35 
36 #include <sys/param.h>
37 #include <sys/conf.h>
38 #include <sys/priv.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/module.h>
42 #include <sys/malloc.h>
43 #include <sys/queue.h>
44 #include <sys/taskqueue.h>
45 #include <sys/pciio.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pci_private.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 #ifdef RSS
61 #include <net/rss_config.h>
62 #endif
63 #include <netinet/in.h>
64 #include <netinet/ip.h>
65 #if defined(__i386__) || defined(__amd64__)
66 #include <machine/md_var.h>
67 #include <machine/cputypes.h>
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 #endif
71 #include <crypto/rijndael/rijndael.h>
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_str(device_t, device_t, char *, size_t);
94 static int t4_ready(device_t);
95 static int t4_read_port_device(device_t, int, device_t *);
96 static device_method_t t4_methods[] = {
97 	DEVMETHOD(device_probe,		t4_probe),
98 	DEVMETHOD(device_attach,	t4_attach),
99 	DEVMETHOD(device_detach,	t4_detach),
100 
101 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
102 
103 	DEVMETHOD(t4_is_main_ready,	t4_ready),
104 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
105 
106 	DEVMETHOD_END
107 };
108 static driver_t t4_driver = {
109 	"t4nex",
110 	t4_methods,
111 	sizeof(struct adapter)
112 };
113 
114 
115 /* T4 port (cxgbe) interface */
116 static int cxgbe_probe(device_t);
117 static int cxgbe_attach(device_t);
118 static int cxgbe_detach(device_t);
119 device_method_t cxgbe_methods[] = {
120 	DEVMETHOD(device_probe,		cxgbe_probe),
121 	DEVMETHOD(device_attach,	cxgbe_attach),
122 	DEVMETHOD(device_detach,	cxgbe_detach),
123 	{ 0, 0 }
124 };
125 static driver_t cxgbe_driver = {
126 	"cxgbe",
127 	cxgbe_methods,
128 	sizeof(struct port_info)
129 };
130 
131 /* T4 VI (vcxgbe) interface */
132 static int vcxgbe_probe(device_t);
133 static int vcxgbe_attach(device_t);
134 static int vcxgbe_detach(device_t);
135 static device_method_t vcxgbe_methods[] = {
136 	DEVMETHOD(device_probe,		vcxgbe_probe),
137 	DEVMETHOD(device_attach,	vcxgbe_attach),
138 	DEVMETHOD(device_detach,	vcxgbe_detach),
139 	{ 0, 0 }
140 };
141 static driver_t vcxgbe_driver = {
142 	"vcxgbe",
143 	vcxgbe_methods,
144 	sizeof(struct vi_info)
145 };
146 
147 static d_ioctl_t t4_ioctl;
148 
149 static struct cdevsw t4_cdevsw = {
150        .d_version = D_VERSION,
151        .d_ioctl = t4_ioctl,
152        .d_name = "t4nex",
153 };
154 
155 /* T5 bus driver interface */
156 static int t5_probe(device_t);
157 static device_method_t t5_methods[] = {
158 	DEVMETHOD(device_probe,		t5_probe),
159 	DEVMETHOD(device_attach,	t4_attach),
160 	DEVMETHOD(device_detach,	t4_detach),
161 
162 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
163 
164 	DEVMETHOD(t4_is_main_ready,	t4_ready),
165 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
166 
167 	DEVMETHOD_END
168 };
169 static driver_t t5_driver = {
170 	"t5nex",
171 	t5_methods,
172 	sizeof(struct adapter)
173 };
174 
175 
176 /* T5 port (cxl) interface */
177 static driver_t cxl_driver = {
178 	"cxl",
179 	cxgbe_methods,
180 	sizeof(struct port_info)
181 };
182 
183 /* T5 VI (vcxl) interface */
184 static driver_t vcxl_driver = {
185 	"vcxl",
186 	vcxgbe_methods,
187 	sizeof(struct vi_info)
188 };
189 
190 /* T6 bus driver interface */
191 static int t6_probe(device_t);
192 static device_method_t t6_methods[] = {
193 	DEVMETHOD(device_probe,		t6_probe),
194 	DEVMETHOD(device_attach,	t4_attach),
195 	DEVMETHOD(device_detach,	t4_detach),
196 
197 	DEVMETHOD(bus_child_location_str, t4_child_location_str),
198 
199 	DEVMETHOD(t4_is_main_ready,	t4_ready),
200 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
201 
202 	DEVMETHOD_END
203 };
204 static driver_t t6_driver = {
205 	"t6nex",
206 	t6_methods,
207 	sizeof(struct adapter)
208 };
209 
210 
211 /* T6 port (cc) interface */
212 static driver_t cc_driver = {
213 	"cc",
214 	cxgbe_methods,
215 	sizeof(struct port_info)
216 };
217 
218 /* T6 VI (vcc) interface */
219 static driver_t vcc_driver = {
220 	"vcc",
221 	vcxgbe_methods,
222 	sizeof(struct vi_info)
223 };
224 
225 /* ifnet interface */
226 static void cxgbe_init(void *);
227 static int cxgbe_ioctl(struct ifnet *, unsigned long, caddr_t);
228 static int cxgbe_transmit(struct ifnet *, struct mbuf *);
229 static void cxgbe_qflush(struct ifnet *);
230 
231 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
232 
233 /*
234  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
235  * then ADAPTER_LOCK, then t4_uld_list_lock.
236  */
237 static struct sx t4_list_lock;
238 SLIST_HEAD(, adapter) t4_list;
239 #ifdef TCP_OFFLOAD
240 static struct sx t4_uld_list_lock;
241 SLIST_HEAD(, uld_info) t4_uld_list;
242 #endif
243 
244 /*
245  * Tunables.  See tweak_tunables() too.
246  *
247  * Each tunable is set to a default value here if it's known at compile-time.
248  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
249  * provide a reasonable default (upto n) when the driver is loaded.
250  *
251  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
252  * T5 are under hw.cxl.
253  */
254 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD, 0, "cxgbe(4) parameters");
255 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD, 0, "cxgbe(4) T5+ parameters");
256 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD, 0, "cxgbe(4) TOE parameters");
257 
258 /*
259  * Number of queues for tx and rx, NIC and offload.
260  */
261 #define NTXQ 16
262 int t4_ntxq = -NTXQ;
263 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
264     "Number of TX queues per port");
265 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
266 
267 #define NRXQ 8
268 int t4_nrxq = -NRXQ;
269 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
270     "Number of RX queues per port");
271 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
272 
273 #define NTXQ_VI 1
274 static int t4_ntxq_vi = -NTXQ_VI;
275 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
276     "Number of TX queues per VI");
277 
278 #define NRXQ_VI 1
279 static int t4_nrxq_vi = -NRXQ_VI;
280 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
281     "Number of RX queues per VI");
282 
283 static int t4_rsrv_noflowq = 0;
284 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
285     0, "Reserve TX queue 0 of each VI for non-flowid packets");
286 
287 #ifdef TCP_OFFLOAD
288 #define NOFLDTXQ 8
289 static int t4_nofldtxq = -NOFLDTXQ;
290 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
291     "Number of offload TX queues per port");
292 
293 #define NOFLDRXQ 2
294 static int t4_nofldrxq = -NOFLDRXQ;
295 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
296     "Number of offload RX queues per port");
297 
298 #define NOFLDTXQ_VI 1
299 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
300 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
301     "Number of offload TX queues per VI");
302 
303 #define NOFLDRXQ_VI 1
304 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
305 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
306     "Number of offload RX queues per VI");
307 
308 #define TMR_IDX_OFLD 1
309 int t4_tmr_idx_ofld = TMR_IDX_OFLD;
310 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
311     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
312 
313 #define PKTC_IDX_OFLD (-1)
314 int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
315 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
316     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
317 
318 /* 0 means chip/fw default, non-zero number is value in microseconds */
319 static u_long t4_toe_keepalive_idle = 0;
320 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
321     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
322 
323 /* 0 means chip/fw default, non-zero number is value in microseconds */
324 static u_long t4_toe_keepalive_interval = 0;
325 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
326     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
327 
328 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
329 static int t4_toe_keepalive_count = 0;
330 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
331     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
332 
333 /* 0 means chip/fw default, non-zero number is value in microseconds */
334 static u_long t4_toe_rexmt_min = 0;
335 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
336     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
337 
338 /* 0 means chip/fw default, non-zero number is value in microseconds */
339 static u_long t4_toe_rexmt_max = 0;
340 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
341     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
342 
343 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
344 static int t4_toe_rexmt_count = 0;
345 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
346     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
347 
348 /* -1 means chip/fw default, other values are raw backoff values to use */
349 static int t4_toe_rexmt_backoff[16] = {
350 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
351 };
352 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff, CTLFLAG_RD, 0,
353     "cxgbe(4) TOE retransmit backoff values");
354 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
355     &t4_toe_rexmt_backoff[0], 0, "");
356 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
357     &t4_toe_rexmt_backoff[1], 0, "");
358 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
359     &t4_toe_rexmt_backoff[2], 0, "");
360 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
361     &t4_toe_rexmt_backoff[3], 0, "");
362 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
363     &t4_toe_rexmt_backoff[4], 0, "");
364 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
365     &t4_toe_rexmt_backoff[5], 0, "");
366 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
367     &t4_toe_rexmt_backoff[6], 0, "");
368 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
369     &t4_toe_rexmt_backoff[7], 0, "");
370 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
371     &t4_toe_rexmt_backoff[8], 0, "");
372 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
373     &t4_toe_rexmt_backoff[9], 0, "");
374 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
375     &t4_toe_rexmt_backoff[10], 0, "");
376 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
377     &t4_toe_rexmt_backoff[11], 0, "");
378 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
379     &t4_toe_rexmt_backoff[12], 0, "");
380 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
381     &t4_toe_rexmt_backoff[13], 0, "");
382 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
383     &t4_toe_rexmt_backoff[14], 0, "");
384 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
385     &t4_toe_rexmt_backoff[15], 0, "");
386 #endif
387 
388 #ifdef DEV_NETMAP
389 #define NNMTXQ_VI 2
390 static int t4_nnmtxq_vi = -NNMTXQ_VI;
391 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
392     "Number of netmap TX queues per VI");
393 
394 #define NNMRXQ_VI 2
395 static int t4_nnmrxq_vi = -NNMRXQ_VI;
396 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
397     "Number of netmap RX queues per VI");
398 #endif
399 
400 /*
401  * Holdoff parameters for ports.
402  */
403 #define TMR_IDX 1
404 int t4_tmr_idx = TMR_IDX;
405 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
406     0, "Holdoff timer index");
407 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
408 
409 #define PKTC_IDX (-1)
410 int t4_pktc_idx = PKTC_IDX;
411 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
412     0, "Holdoff packet counter index");
413 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
414 
415 /*
416  * Size (# of entries) of each tx and rx queue.
417  */
418 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
419 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
420     "Number of descriptors in each TX queue");
421 
422 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
423 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
424     "Number of descriptors in each RX queue");
425 
426 /*
427  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
428  */
429 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
430 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
431     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
432 
433 /*
434  * Configuration file.  All the _CF names here are special.
435  */
436 #define DEFAULT_CF	"default"
437 #define BUILTIN_CF	"built-in"
438 #define FLASH_CF	"flash"
439 #define UWIRE_CF	"uwire"
440 #define FPGA_CF		"fpga"
441 static char t4_cfg_file[32] = DEFAULT_CF;
442 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
443     sizeof(t4_cfg_file), "Firmware configuration file");
444 
445 /*
446  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
447  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
448  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
449  *            mark or when signalled to do so, 0 to never emit PAUSE.
450  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
451  *                 negotiated settings will override rx_pause/tx_pause.
452  *                 Otherwise rx_pause/tx_pause are applied forcibly.
453  */
454 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
455 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
456     &t4_pause_settings, 0,
457     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
458 
459 /*
460  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
461  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
462  *  0 to disable FEC.
463  */
464 static int t4_fec = -1;
465 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
466     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
467 
468 /*
469  * Link autonegotiation.
470  * -1 to run with the firmware default.
471  *  0 to disable.
472  *  1 to enable.
473  */
474 static int t4_autoneg = -1;
475 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
476     "Link autonegotiation");
477 
478 /*
479  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
480  * encouraged respectively).  '-n' is the same as 'n' except the firmware
481  * version used in the checks is read from the firmware bundled with the driver.
482  */
483 static int t4_fw_install = 1;
484 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
485     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
486 
487 /*
488  * ASIC features that will be used.  Disable the ones you don't want so that the
489  * chip resources aren't wasted on features that will not be used.
490  */
491 static int t4_nbmcaps_allowed = 0;
492 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
493     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
494 
495 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
496 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
497     &t4_linkcaps_allowed, 0, "Default link capabilities");
498 
499 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
500     FW_CAPS_CONFIG_SWITCH_EGRESS;
501 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
502     &t4_switchcaps_allowed, 0, "Default switch capabilities");
503 
504 #ifdef RATELIMIT
505 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
506 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
507 #else
508 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
509 	FW_CAPS_CONFIG_NIC_HASHFILTER;
510 #endif
511 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
512     &t4_niccaps_allowed, 0, "Default NIC capabilities");
513 
514 static int t4_toecaps_allowed = -1;
515 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
516     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
517 
518 static int t4_rdmacaps_allowed = -1;
519 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
520     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
521 
522 static int t4_cryptocaps_allowed = -1;
523 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
524     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
525 
526 static int t4_iscsicaps_allowed = -1;
527 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
528     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
529 
530 static int t4_fcoecaps_allowed = 0;
531 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
532     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
533 
534 static int t5_write_combine = 0;
535 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
536     0, "Use WC instead of UC for BAR2");
537 
538 static int t4_num_vis = 1;
539 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
540     "Number of VIs per port");
541 
542 /*
543  * PCIe Relaxed Ordering.
544  * -1: driver should figure out a good value.
545  * 0: disable RO.
546  * 1: enable RO.
547  * 2: leave RO alone.
548  */
549 static int pcie_relaxed_ordering = -1;
550 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
551     &pcie_relaxed_ordering, 0,
552     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
553 
554 static int t4_panic_on_fatal_err = 0;
555 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RDTUN,
556     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
557 
558 #ifdef TCP_OFFLOAD
559 /*
560  * TOE tunables.
561  */
562 static int t4_cop_managed_offloading = 0;
563 TUNABLE_INT("hw.cxgbe.cop_managed_offloading", &t4_cop_managed_offloading);
564 #endif
565 
566 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
567 static int vi_mac_funcs[] = {
568 	FW_VI_FUNC_ETH,
569 	FW_VI_FUNC_OFLD,
570 	FW_VI_FUNC_IWARP,
571 	FW_VI_FUNC_OPENISCSI,
572 	FW_VI_FUNC_OPENFCOE,
573 	FW_VI_FUNC_FOISCSI,
574 	FW_VI_FUNC_FOFCOE,
575 };
576 
577 struct intrs_and_queues {
578 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
579 	uint16_t num_vis;	/* number of VIs for each port */
580 	uint16_t nirq;		/* Total # of vectors */
581 	uint16_t ntxq;		/* # of NIC txq's for each port */
582 	uint16_t nrxq;		/* # of NIC rxq's for each port */
583 	uint16_t nofldtxq;	/* # of TOE txq's for each port */
584 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
585 
586 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
587 	uint16_t ntxq_vi;	/* # of NIC txq's */
588 	uint16_t nrxq_vi;	/* # of NIC rxq's */
589 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
590 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
591 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
592 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
593 };
594 
595 static void setup_memwin(struct adapter *);
596 static void position_memwin(struct adapter *, int, uint32_t);
597 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
598 static int fwmtype_to_hwmtype(int);
599 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
600     uint32_t *);
601 static int fixup_devlog_params(struct adapter *);
602 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
603 static int contact_firmware(struct adapter *);
604 static int partition_resources(struct adapter *);
605 static int get_params__pre_init(struct adapter *);
606 static int set_params__pre_init(struct adapter *);
607 static int get_params__post_init(struct adapter *);
608 static int set_params__post_init(struct adapter *);
609 static void t4_set_desc(struct adapter *);
610 static bool fixed_ifmedia(struct port_info *);
611 static void build_medialist(struct port_info *);
612 static void init_link_config(struct port_info *);
613 static int fixup_link_config(struct port_info *);
614 static int apply_link_config(struct port_info *);
615 static int cxgbe_init_synchronized(struct vi_info *);
616 static int cxgbe_uninit_synchronized(struct vi_info *);
617 static void quiesce_txq(struct adapter *, struct sge_txq *);
618 static void quiesce_wrq(struct adapter *, struct sge_wrq *);
619 static void quiesce_iq(struct adapter *, struct sge_iq *);
620 static void quiesce_fl(struct adapter *, struct sge_fl *);
621 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
622     driver_intr_t *, void *, char *);
623 static int t4_free_irq(struct adapter *, struct irq *);
624 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
625 static void vi_refresh_stats(struct adapter *, struct vi_info *);
626 static void cxgbe_refresh_stats(struct adapter *, struct port_info *);
627 static void cxgbe_tick(void *);
628 static void cxgbe_sysctls(struct port_info *);
629 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
630 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
631 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
632 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
633 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
634 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
635 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
636 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
637 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
638 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
639 static int sysctl_fec(SYSCTL_HANDLER_ARGS);
640 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
641 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
642 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
643 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
644 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
645 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
646 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
647 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
648 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
649 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
650 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
651 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
652 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
653 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
654 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
655 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
656 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
657 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
658 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
659 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
660 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
661 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
662 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
663 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
664 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
665 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
666 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
667 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
668 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
669 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
670 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
671 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
672 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
673 #ifdef TCP_OFFLOAD
674 static int sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS);
675 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
676 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
677 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
678 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
679 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
680 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
681 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
682 #endif
683 static int get_sge_context(struct adapter *, struct t4_sge_context *);
684 static int load_fw(struct adapter *, struct t4_data *);
685 static int load_cfg(struct adapter *, struct t4_data *);
686 static int load_boot(struct adapter *, struct t4_bootrom *);
687 static int load_bootcfg(struct adapter *, struct t4_data *);
688 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
689 static void free_offload_policy(struct t4_offload_policy *);
690 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
691 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
692 static int read_i2c(struct adapter *, struct t4_i2c_data *);
693 #ifdef TCP_OFFLOAD
694 static int toe_capability(struct vi_info *, int);
695 #endif
696 static int mod_event(module_t, int, void *);
697 static int notify_siblings(device_t, int);
698 
699 struct {
700 	uint16_t device;
701 	char *desc;
702 } t4_pciids[] = {
703 	{0xa000, "Chelsio Terminator 4 FPGA"},
704 	{0x4400, "Chelsio T440-dbg"},
705 	{0x4401, "Chelsio T420-CR"},
706 	{0x4402, "Chelsio T422-CR"},
707 	{0x4403, "Chelsio T440-CR"},
708 	{0x4404, "Chelsio T420-BCH"},
709 	{0x4405, "Chelsio T440-BCH"},
710 	{0x4406, "Chelsio T440-CH"},
711 	{0x4407, "Chelsio T420-SO"},
712 	{0x4408, "Chelsio T420-CX"},
713 	{0x4409, "Chelsio T420-BT"},
714 	{0x440a, "Chelsio T404-BT"},
715 	{0x440e, "Chelsio T440-LP-CR"},
716 }, t5_pciids[] = {
717 	{0xb000, "Chelsio Terminator 5 FPGA"},
718 	{0x5400, "Chelsio T580-dbg"},
719 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
720 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
721 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
722 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
723 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
724 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
725 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
726 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
727 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
728 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
729 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
730 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
731 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
732 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
733 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
734 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
735 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
736 
737 	/* Custom */
738 	{0x5483, "Custom T540-CR"},
739 	{0x5484, "Custom T540-BT"},
740 }, t6_pciids[] = {
741 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
742 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
743 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
744 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
745 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
746 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
747 	{0x6405, "Chelsio T6225-OCP-SO"},	/* 2 x 10/25G, nomem */
748 	{0x6406, "Chelsio T62100-OCP-SO"},	/* 2 x 40/50/100G, nomem */
749 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
750 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
751 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
752 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
753 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
754 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
755 	{0x6414, "Chelsio T61100-OCP-SO"},	/* 1 x 40/50/100G, nomem */
756 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
757 
758 	/* Custom */
759 	{0x6480, "Custom T6225-CR"},
760 	{0x6481, "Custom T62100-CR"},
761 	{0x6482, "Custom T6225-CR"},
762 	{0x6483, "Custom T62100-CR"},
763 	{0x6484, "Custom T64100-CR"},
764 	{0x6485, "Custom T6240-SO"},
765 	{0x6486, "Custom T6225-SO-CR"},
766 	{0x6487, "Custom T6225-CR"},
767 };
768 
769 #ifdef TCP_OFFLOAD
770 /*
771  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
772  * be exactly the same for both rxq and ofld_rxq.
773  */
774 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
775 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
776 #endif
777 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
778 
779 static int
t4_probe(device_t dev)780 t4_probe(device_t dev)
781 {
782 	int i;
783 	uint16_t v = pci_get_vendor(dev);
784 	uint16_t d = pci_get_device(dev);
785 	uint8_t f = pci_get_function(dev);
786 
787 	if (v != PCI_VENDOR_ID_CHELSIO)
788 		return (ENXIO);
789 
790 	/* Attach only to PF0 of the FPGA */
791 	if (d == 0xa000 && f != 0)
792 		return (ENXIO);
793 
794 	for (i = 0; i < nitems(t4_pciids); i++) {
795 		if (d == t4_pciids[i].device) {
796 			device_set_desc(dev, t4_pciids[i].desc);
797 			return (BUS_PROBE_DEFAULT);
798 		}
799 	}
800 
801 	return (ENXIO);
802 }
803 
804 static int
t5_probe(device_t dev)805 t5_probe(device_t dev)
806 {
807 	int i;
808 	uint16_t v = pci_get_vendor(dev);
809 	uint16_t d = pci_get_device(dev);
810 	uint8_t f = pci_get_function(dev);
811 
812 	if (v != PCI_VENDOR_ID_CHELSIO)
813 		return (ENXIO);
814 
815 	/* Attach only to PF0 of the FPGA */
816 	if (d == 0xb000 && f != 0)
817 		return (ENXIO);
818 
819 	for (i = 0; i < nitems(t5_pciids); i++) {
820 		if (d == t5_pciids[i].device) {
821 			device_set_desc(dev, t5_pciids[i].desc);
822 			return (BUS_PROBE_DEFAULT);
823 		}
824 	}
825 
826 	return (ENXIO);
827 }
828 
829 static int
t6_probe(device_t dev)830 t6_probe(device_t dev)
831 {
832 	int i;
833 	uint16_t v = pci_get_vendor(dev);
834 	uint16_t d = pci_get_device(dev);
835 
836 	if (v != PCI_VENDOR_ID_CHELSIO)
837 		return (ENXIO);
838 
839 	for (i = 0; i < nitems(t6_pciids); i++) {
840 		if (d == t6_pciids[i].device) {
841 			device_set_desc(dev, t6_pciids[i].desc);
842 			return (BUS_PROBE_DEFAULT);
843 		}
844 	}
845 
846 	return (ENXIO);
847 }
848 
849 static void
t5_attribute_workaround(device_t dev)850 t5_attribute_workaround(device_t dev)
851 {
852 	device_t root_port;
853 	uint32_t v;
854 
855 	/*
856 	 * The T5 chips do not properly echo the No Snoop and Relaxed
857 	 * Ordering attributes when replying to a TLP from a Root
858 	 * Port.  As a workaround, find the parent Root Port and
859 	 * disable No Snoop and Relaxed Ordering.  Note that this
860 	 * affects all devices under this root port.
861 	 */
862 	root_port = pci_find_pcie_root_port(dev);
863 	if (root_port == NULL) {
864 		device_printf(dev, "Unable to find parent root port\n");
865 		return;
866 	}
867 
868 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
869 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
870 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
871 	    0)
872 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
873 		    device_get_nameunit(root_port));
874 }
875 
876 static const struct devnames devnames[] = {
877 	{
878 		.nexus_name = "t4nex",
879 		.ifnet_name = "cxgbe",
880 		.vi_ifnet_name = "vcxgbe",
881 		.pf03_drv_name = "t4iov",
882 		.vf_nexus_name = "t4vf",
883 		.vf_ifnet_name = "cxgbev"
884 	}, {
885 		.nexus_name = "t5nex",
886 		.ifnet_name = "cxl",
887 		.vi_ifnet_name = "vcxl",
888 		.pf03_drv_name = "t5iov",
889 		.vf_nexus_name = "t5vf",
890 		.vf_ifnet_name = "cxlv"
891 	}, {
892 		.nexus_name = "t6nex",
893 		.ifnet_name = "cc",
894 		.vi_ifnet_name = "vcc",
895 		.pf03_drv_name = "t6iov",
896 		.vf_nexus_name = "t6vf",
897 		.vf_ifnet_name = "ccv"
898 	}
899 };
900 
901 void
t4_init_devnames(struct adapter * sc)902 t4_init_devnames(struct adapter *sc)
903 {
904 	int id;
905 
906 	id = chip_id(sc);
907 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
908 		sc->names = &devnames[id - CHELSIO_T4];
909 	else {
910 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
911 		sc->names = NULL;
912 	}
913 }
914 
915 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)916 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
917 {
918 	const char *parent, *name;
919 	long value;
920 	int line, unit;
921 
922 	line = 0;
923 	parent = device_get_nameunit(sc->dev);
924 	name = sc->names->ifnet_name;
925 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
926 		if (resource_long_value(name, unit, "port", &value) == 0 &&
927 		    value == pi->port_id)
928 			return (unit);
929 	}
930 	return (-1);
931 }
932 
933 static int
t4_attach(device_t dev)934 t4_attach(device_t dev)
935 {
936 	struct adapter *sc;
937 	int rc = 0, i, j, rqidx, tqidx, nports;
938 	struct make_dev_args mda;
939 	struct intrs_and_queues iaq;
940 	struct sge *s;
941 	uint32_t *buf;
942 #ifdef TCP_OFFLOAD
943 	int ofld_rqidx, ofld_tqidx;
944 #endif
945 #ifdef DEV_NETMAP
946 	int nm_rqidx, nm_tqidx;
947 #endif
948 	int num_vis;
949 
950 	sc = device_get_softc(dev);
951 	sc->dev = dev;
952 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
953 
954 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
955 		t5_attribute_workaround(dev);
956 	pci_enable_busmaster(dev);
957 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
958 		uint32_t v;
959 
960 		pci_set_max_read_req(dev, 4096);
961 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
962 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
963 		if (pcie_relaxed_ordering == 0 &&
964 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
965 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
966 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
967 		} else if (pcie_relaxed_ordering == 1 &&
968 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
969 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
970 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
971 		}
972 	}
973 
974 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
975 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
976 	sc->traceq = -1;
977 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
978 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
979 	    device_get_nameunit(dev));
980 
981 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
982 	    device_get_nameunit(dev));
983 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
984 	t4_add_adapter(sc);
985 
986 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
987 	TAILQ_INIT(&sc->sfl);
988 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
989 
990 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
991 
992 	sc->policy = NULL;
993 	rw_init(&sc->policy_lock, "connection offload policy");
994 
995 	rc = t4_map_bars_0_and_4(sc);
996 	if (rc != 0)
997 		goto done; /* error message displayed already */
998 
999 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1000 
1001 	/* Prepare the adapter for operation. */
1002 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1003 	rc = -t4_prep_adapter(sc, buf);
1004 	free(buf, M_CXGBE);
1005 	if (rc != 0) {
1006 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1007 		goto done;
1008 	}
1009 
1010 	/*
1011 	 * This is the real PF# to which we're attaching.  Works from within PCI
1012 	 * passthrough environments too, where pci_get_function() could return a
1013 	 * different PF# depending on the passthrough configuration.  We need to
1014 	 * use the real PF# in all our communication with the firmware.
1015 	 */
1016 	j = t4_read_reg(sc, A_PL_WHOAMI);
1017 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1018 	sc->mbox = sc->pf;
1019 
1020 	t4_init_devnames(sc);
1021 	if (sc->names == NULL) {
1022 		rc = ENOTSUP;
1023 		goto done; /* error message displayed already */
1024 	}
1025 
1026 	/*
1027 	 * Do this really early, with the memory windows set up even before the
1028 	 * character device.  The userland tool's register i/o and mem read
1029 	 * will work even in "recovery mode".
1030 	 */
1031 	setup_memwin(sc);
1032 	if (t4_init_devlog_params(sc, 0) == 0)
1033 		fixup_devlog_params(sc);
1034 	make_dev_args_init(&mda);
1035 	mda.mda_devsw = &t4_cdevsw;
1036 	mda.mda_uid = UID_ROOT;
1037 	mda.mda_gid = GID_WHEEL;
1038 	mda.mda_mode = 0600;
1039 	mda.mda_si_drv1 = sc;
1040 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1041 	if (rc != 0)
1042 		device_printf(dev, "failed to create nexus char device: %d.\n",
1043 		    rc);
1044 
1045 	/* Go no further if recovery mode has been requested. */
1046 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1047 		device_printf(dev, "recovery mode.\n");
1048 		goto done;
1049 	}
1050 
1051 #if defined(__i386__)
1052 	if ((cpu_feature & CPUID_CX8) == 0) {
1053 		device_printf(dev, "64 bit atomics not available.\n");
1054 		rc = ENOTSUP;
1055 		goto done;
1056 	}
1057 #endif
1058 
1059 	/* Contact the firmware and try to become the master driver. */
1060 	rc = contact_firmware(sc);
1061 	if (rc != 0)
1062 		goto done; /* error message displayed already */
1063 	MPASS(sc->flags & FW_OK);
1064 
1065 	rc = get_params__pre_init(sc);
1066 	if (rc != 0)
1067 		goto done; /* error message displayed already */
1068 
1069 	if (sc->flags & MASTER_PF) {
1070 		rc = partition_resources(sc);
1071 		if (rc != 0)
1072 			goto done; /* error message displayed already */
1073 		t4_intr_clear(sc);
1074 	}
1075 
1076 	rc = get_params__post_init(sc);
1077 	if (rc != 0)
1078 		goto done; /* error message displayed already */
1079 
1080 	rc = set_params__post_init(sc);
1081 	if (rc != 0)
1082 		goto done; /* error message displayed already */
1083 
1084 	rc = t4_map_bar_2(sc);
1085 	if (rc != 0)
1086 		goto done; /* error message displayed already */
1087 
1088 	rc = t4_create_dma_tag(sc);
1089 	if (rc != 0)
1090 		goto done; /* error message displayed already */
1091 
1092 	/*
1093 	 * First pass over all the ports - allocate VIs and initialize some
1094 	 * basic parameters like mac address, port type, etc.
1095 	 */
1096 	for_each_port(sc, i) {
1097 		struct port_info *pi;
1098 
1099 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1100 		sc->port[i] = pi;
1101 
1102 		/* These must be set before t4_port_init */
1103 		pi->adapter = sc;
1104 		pi->port_id = i;
1105 		/*
1106 		 * XXX: vi[0] is special so we can't delay this allocation until
1107 		 * pi->nvi's final value is known.
1108 		 */
1109 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1110 		    M_ZERO | M_WAITOK);
1111 
1112 		/*
1113 		 * Allocate the "main" VI and initialize parameters
1114 		 * like mac addr.
1115 		 */
1116 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1117 		if (rc != 0) {
1118 			device_printf(dev, "unable to initialize port %d: %d\n",
1119 			    i, rc);
1120 			free(pi->vi, M_CXGBE);
1121 			free(pi, M_CXGBE);
1122 			sc->port[i] = NULL;
1123 			goto done;
1124 		}
1125 
1126 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1127 		    device_get_nameunit(dev), i);
1128 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1129 		sc->chan_map[pi->tx_chan] = i;
1130 
1131 		/* All VIs on this port share this media. */
1132 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1133 		    cxgbe_media_status);
1134 
1135 		PORT_LOCK(pi);
1136 		init_link_config(pi);
1137 		fixup_link_config(pi);
1138 		build_medialist(pi);
1139 		if (fixed_ifmedia(pi))
1140 			pi->flags |= FIXED_IFMEDIA;
1141 		PORT_UNLOCK(pi);
1142 
1143 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1144 		    t4_ifnet_unit(sc, pi));
1145 		if (pi->dev == NULL) {
1146 			device_printf(dev,
1147 			    "failed to add device for port %d.\n", i);
1148 			rc = ENXIO;
1149 			goto done;
1150 		}
1151 		pi->vi[0].dev = pi->dev;
1152 		device_set_softc(pi->dev, pi);
1153 	}
1154 
1155 	/*
1156 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1157 	 */
1158 	nports = sc->params.nports;
1159 	rc = cfg_itype_and_nqueues(sc, &iaq);
1160 	if (rc != 0)
1161 		goto done; /* error message displayed already */
1162 
1163 	num_vis = iaq.num_vis;
1164 	sc->intr_type = iaq.intr_type;
1165 	sc->intr_count = iaq.nirq;
1166 
1167 	s = &sc->sge;
1168 	s->nrxq = nports * iaq.nrxq;
1169 	s->ntxq = nports * iaq.ntxq;
1170 	if (num_vis > 1) {
1171 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1172 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1173 	}
1174 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1175 	s->neq += nports;		/* ctrl queues: 1 per port */
1176 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1177 #ifdef TCP_OFFLOAD
1178 	if (is_offload(sc)) {
1179 		s->nofldrxq = nports * iaq.nofldrxq;
1180 		s->nofldtxq = nports * iaq.nofldtxq;
1181 		if (num_vis > 1) {
1182 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1183 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1184 		}
1185 		s->neq += s->nofldtxq + s->nofldrxq;
1186 		s->niq += s->nofldrxq;
1187 
1188 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1189 		    M_CXGBE, M_ZERO | M_WAITOK);
1190 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_wrq),
1191 		    M_CXGBE, M_ZERO | M_WAITOK);
1192 	}
1193 #endif
1194 #ifdef DEV_NETMAP
1195 	if (num_vis > 1) {
1196 		s->nnmrxq = nports * (num_vis - 1) * iaq.nnmrxq_vi;
1197 		s->nnmtxq = nports * (num_vis - 1) * iaq.nnmtxq_vi;
1198 	}
1199 	s->neq += s->nnmtxq + s->nnmrxq;
1200 	s->niq += s->nnmrxq;
1201 
1202 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1203 	    M_CXGBE, M_ZERO | M_WAITOK);
1204 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1205 	    M_CXGBE, M_ZERO | M_WAITOK);
1206 #endif
1207 
1208 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1209 	    M_ZERO | M_WAITOK);
1210 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1211 	    M_ZERO | M_WAITOK);
1212 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1213 	    M_ZERO | M_WAITOK);
1214 	s->iqmap = malloc(s->niq * sizeof(struct sge_iq *), M_CXGBE,
1215 	    M_ZERO | M_WAITOK);
1216 	s->eqmap = malloc(s->neq * sizeof(struct sge_eq *), M_CXGBE,
1217 	    M_ZERO | M_WAITOK);
1218 
1219 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1220 	    M_ZERO | M_WAITOK);
1221 
1222 	t4_init_l2t(sc, M_WAITOK);
1223 	t4_init_smt(sc, M_WAITOK);
1224 	t4_init_tx_sched(sc);
1225 #ifdef INET6
1226 	t4_init_clip_table(sc);
1227 #endif
1228 	if (sc->vres.key.size != 0)
1229 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1230 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1231 
1232 	/*
1233 	 * Second pass over the ports.  This time we know the number of rx and
1234 	 * tx queues that each port should get.
1235 	 */
1236 	rqidx = tqidx = 0;
1237 #ifdef TCP_OFFLOAD
1238 	ofld_rqidx = ofld_tqidx = 0;
1239 #endif
1240 #ifdef DEV_NETMAP
1241 	nm_rqidx = nm_tqidx = 0;
1242 #endif
1243 	for_each_port(sc, i) {
1244 		struct port_info *pi = sc->port[i];
1245 		struct vi_info *vi;
1246 
1247 		if (pi == NULL)
1248 			continue;
1249 
1250 		pi->nvi = num_vis;
1251 		for_each_vi(pi, j, vi) {
1252 			vi->pi = pi;
1253 			vi->qsize_rxq = t4_qsize_rxq;
1254 			vi->qsize_txq = t4_qsize_txq;
1255 
1256 			vi->first_rxq = rqidx;
1257 			vi->first_txq = tqidx;
1258 			vi->tmr_idx = t4_tmr_idx;
1259 			vi->pktc_idx = t4_pktc_idx;
1260 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1261 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1262 
1263 			rqidx += vi->nrxq;
1264 			tqidx += vi->ntxq;
1265 
1266 			if (j == 0 && vi->ntxq > 1)
1267 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1268 			else
1269 				vi->rsrv_noflowq = 0;
1270 
1271 #ifdef TCP_OFFLOAD
1272 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1273 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1274 			vi->first_ofld_rxq = ofld_rqidx;
1275 			vi->first_ofld_txq = ofld_tqidx;
1276 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1277 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1278 
1279 			ofld_rqidx += vi->nofldrxq;
1280 			ofld_tqidx += vi->nofldtxq;
1281 #endif
1282 #ifdef DEV_NETMAP
1283 			if (j > 0) {
1284 				vi->first_nm_rxq = nm_rqidx;
1285 				vi->first_nm_txq = nm_tqidx;
1286 				vi->nnmrxq = iaq.nnmrxq_vi;
1287 				vi->nnmtxq = iaq.nnmtxq_vi;
1288 				nm_rqidx += vi->nnmrxq;
1289 				nm_tqidx += vi->nnmtxq;
1290 			}
1291 #endif
1292 		}
1293 	}
1294 
1295 	rc = t4_setup_intr_handlers(sc);
1296 	if (rc != 0) {
1297 		device_printf(dev,
1298 		    "failed to setup interrupt handlers: %d\n", rc);
1299 		goto done;
1300 	}
1301 
1302 	rc = bus_generic_probe(dev);
1303 	if (rc != 0) {
1304 		device_printf(dev, "failed to probe child drivers: %d\n", rc);
1305 		goto done;
1306 	}
1307 
1308 	/*
1309 	 * Ensure thread-safe mailbox access (in debug builds).
1310 	 *
1311 	 * So far this was the only thread accessing the mailbox but various
1312 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1313 	 * will access the mailbox from different threads.
1314 	 */
1315 	sc->flags |= CHK_MBOX_ACCESS;
1316 
1317 	rc = bus_generic_attach(dev);
1318 	if (rc != 0) {
1319 		device_printf(dev,
1320 		    "failed to attach all child ports: %d\n", rc);
1321 		goto done;
1322 	}
1323 
1324 	device_printf(dev,
1325 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1326 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1327 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1328 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1329 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1330 
1331 	t4_set_desc(sc);
1332 
1333 	notify_siblings(dev, 0);
1334 
1335 done:
1336 	if (rc != 0 && sc->cdev) {
1337 		/* cdev was created and so cxgbetool works; recover that way. */
1338 		device_printf(dev,
1339 		    "error during attach, adapter is now in recovery mode.\n");
1340 		rc = 0;
1341 	}
1342 
1343 	if (rc != 0)
1344 		t4_detach_common(dev);
1345 	else
1346 		t4_sysctls(sc);
1347 
1348 	return (rc);
1349 }
1350 
1351 static int
t4_child_location_str(device_t bus,device_t dev,char * buf,size_t buflen)1352 t4_child_location_str(device_t bus, device_t dev, char *buf, size_t buflen)
1353 {
1354 	struct adapter *sc;
1355 	struct port_info *pi;
1356 	int i;
1357 
1358 	sc = device_get_softc(bus);
1359 	buf[0] = '\0';
1360 	for_each_port(sc, i) {
1361 		pi = sc->port[i];
1362 		if (pi != NULL && pi->dev == dev) {
1363 			snprintf(buf, buflen, "port=%d", pi->port_id);
1364 			break;
1365 		}
1366 	}
1367 	return (0);
1368 }
1369 
1370 static int
t4_ready(device_t dev)1371 t4_ready(device_t dev)
1372 {
1373 	struct adapter *sc;
1374 
1375 	sc = device_get_softc(dev);
1376 	if (sc->flags & FW_OK)
1377 		return (0);
1378 	return (ENXIO);
1379 }
1380 
1381 static int
t4_read_port_device(device_t dev,int port,device_t * child)1382 t4_read_port_device(device_t dev, int port, device_t *child)
1383 {
1384 	struct adapter *sc;
1385 	struct port_info *pi;
1386 
1387 	sc = device_get_softc(dev);
1388 	if (port < 0 || port >= MAX_NPORTS)
1389 		return (EINVAL);
1390 	pi = sc->port[port];
1391 	if (pi == NULL || pi->dev == NULL)
1392 		return (ENXIO);
1393 	*child = pi->dev;
1394 	return (0);
1395 }
1396 
1397 static int
notify_siblings(device_t dev,int detaching)1398 notify_siblings(device_t dev, int detaching)
1399 {
1400 	device_t sibling;
1401 	int error, i;
1402 
1403 	error = 0;
1404 	for (i = 0; i < PCI_FUNCMAX; i++) {
1405 		if (i == pci_get_function(dev))
1406 			continue;
1407 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1408 		    pci_get_slot(dev), i);
1409 		if (sibling == NULL || !device_is_attached(sibling))
1410 			continue;
1411 		if (detaching)
1412 			error = T4_DETACH_CHILD(sibling);
1413 		else
1414 			(void)T4_ATTACH_CHILD(sibling);
1415 		if (error)
1416 			break;
1417 	}
1418 	return (error);
1419 }
1420 
1421 /*
1422  * Idempotent
1423  */
1424 static int
t4_detach(device_t dev)1425 t4_detach(device_t dev)
1426 {
1427 	struct adapter *sc;
1428 	int rc;
1429 
1430 	sc = device_get_softc(dev);
1431 
1432 	rc = notify_siblings(dev, 1);
1433 	if (rc) {
1434 		device_printf(dev,
1435 		    "failed to detach sibling devices: %d\n", rc);
1436 		return (rc);
1437 	}
1438 
1439 	return (t4_detach_common(dev));
1440 }
1441 
1442 int
t4_detach_common(device_t dev)1443 t4_detach_common(device_t dev)
1444 {
1445 	struct adapter *sc;
1446 	struct port_info *pi;
1447 	int i, rc;
1448 
1449 	sc = device_get_softc(dev);
1450 
1451 	if (sc->cdev) {
1452 		destroy_dev(sc->cdev);
1453 		sc->cdev = NULL;
1454 	}
1455 
1456 	sx_xlock(&t4_list_lock);
1457 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1458 	sx_xunlock(&t4_list_lock);
1459 
1460 	sc->flags &= ~CHK_MBOX_ACCESS;
1461 	if (sc->flags & FULL_INIT_DONE) {
1462 		if (!(sc->flags & IS_VF))
1463 			t4_intr_disable(sc);
1464 	}
1465 
1466 	if (device_is_attached(dev)) {
1467 		rc = bus_generic_detach(dev);
1468 		if (rc) {
1469 			device_printf(dev,
1470 			    "failed to detach child devices: %d\n", rc);
1471 			return (rc);
1472 		}
1473 	}
1474 
1475 	for (i = 0; i < sc->intr_count; i++)
1476 		t4_free_irq(sc, &sc->irq[i]);
1477 
1478 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1479 		t4_free_tx_sched(sc);
1480 
1481 	for (i = 0; i < MAX_NPORTS; i++) {
1482 		pi = sc->port[i];
1483 		if (pi) {
1484 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1485 			if (pi->dev)
1486 				device_delete_child(dev, pi->dev);
1487 
1488 			mtx_destroy(&pi->pi_lock);
1489 			free(pi->vi, M_CXGBE);
1490 			free(pi, M_CXGBE);
1491 		}
1492 	}
1493 
1494 	device_delete_children(dev);
1495 
1496 	if (sc->flags & FULL_INIT_DONE)
1497 		adapter_full_uninit(sc);
1498 
1499 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1500 		t4_fw_bye(sc, sc->mbox);
1501 
1502 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1503 		pci_release_msi(dev);
1504 
1505 	if (sc->regs_res)
1506 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1507 		    sc->regs_res);
1508 
1509 	if (sc->udbs_res)
1510 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1511 		    sc->udbs_res);
1512 
1513 	if (sc->msix_res)
1514 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1515 		    sc->msix_res);
1516 
1517 	if (sc->l2t)
1518 		t4_free_l2t(sc->l2t);
1519 	if (sc->key_map)
1520 		vmem_destroy(sc->key_map);
1521 	if (sc->smt)
1522 		t4_free_smt(sc->smt);
1523 #ifdef INET6
1524 	t4_destroy_clip_table(sc);
1525 #endif
1526 
1527 #ifdef TCP_OFFLOAD
1528 	free(sc->sge.ofld_rxq, M_CXGBE);
1529 	free(sc->sge.ofld_txq, M_CXGBE);
1530 #endif
1531 #ifdef DEV_NETMAP
1532 	free(sc->sge.nm_rxq, M_CXGBE);
1533 	free(sc->sge.nm_txq, M_CXGBE);
1534 #endif
1535 	free(sc->irq, M_CXGBE);
1536 	free(sc->sge.rxq, M_CXGBE);
1537 	free(sc->sge.txq, M_CXGBE);
1538 	free(sc->sge.ctrlq, M_CXGBE);
1539 	free(sc->sge.iqmap, M_CXGBE);
1540 	free(sc->sge.eqmap, M_CXGBE);
1541 	free(sc->tids.ftid_tab, M_CXGBE);
1542 	free(sc->tids.hpftid_tab, M_CXGBE);
1543 	free_hftid_hash(&sc->tids);
1544 	free(sc->tids.atid_tab, M_CXGBE);
1545 	free(sc->tids.tid_tab, M_CXGBE);
1546 	free(sc->tt.tls_rx_ports, M_CXGBE);
1547 	t4_destroy_dma_tag(sc);
1548 
1549 	callout_drain(&sc->sfl_callout);
1550 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1551 		mtx_destroy(&sc->tids.ftid_lock);
1552 		cv_destroy(&sc->tids.ftid_cv);
1553 	}
1554 	if (mtx_initialized(&sc->tids.atid_lock))
1555 		mtx_destroy(&sc->tids.atid_lock);
1556 	if (mtx_initialized(&sc->ifp_lock))
1557 		mtx_destroy(&sc->ifp_lock);
1558 
1559 	if (rw_initialized(&sc->policy_lock)) {
1560 		rw_destroy(&sc->policy_lock);
1561 #ifdef TCP_OFFLOAD
1562 		if (sc->policy != NULL)
1563 			free_offload_policy(sc->policy);
1564 #endif
1565 	}
1566 
1567 	for (i = 0; i < NUM_MEMWIN; i++) {
1568 		struct memwin *mw = &sc->memwin[i];
1569 
1570 		if (rw_initialized(&mw->mw_lock))
1571 			rw_destroy(&mw->mw_lock);
1572 	}
1573 
1574 	mtx_destroy(&sc->sfl_lock);
1575 	mtx_destroy(&sc->reg_lock);
1576 	mtx_destroy(&sc->sc_lock);
1577 
1578 	bzero(sc, sizeof(*sc));
1579 
1580 	return (0);
1581 }
1582 
1583 static int
cxgbe_probe(device_t dev)1584 cxgbe_probe(device_t dev)
1585 {
1586 	char buf[128];
1587 	struct port_info *pi = device_get_softc(dev);
1588 
1589 	snprintf(buf, sizeof(buf), "port %d", pi->port_id);
1590 	device_set_desc_copy(dev, buf);
1591 
1592 	return (BUS_PROBE_DEFAULT);
1593 }
1594 
1595 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
1596     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
1597     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS)
1598 #define T4_CAP_ENABLE (T4_CAP)
1599 
1600 static int
cxgbe_vi_attach(device_t dev,struct vi_info * vi)1601 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
1602 {
1603 	struct ifnet *ifp;
1604 	struct sbuf *sb;
1605 
1606 	vi->xact_addr_filt = -1;
1607 	callout_init(&vi->tick, 1);
1608 
1609 	/* Allocate an ifnet and set it up */
1610 	ifp = if_alloc(IFT_ETHER);
1611 	if (ifp == NULL) {
1612 		device_printf(dev, "Cannot allocate ifnet\n");
1613 		return (ENOMEM);
1614 	}
1615 	vi->ifp = ifp;
1616 	ifp->if_softc = vi;
1617 
1618 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1619 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1620 
1621 	ifp->if_init = cxgbe_init;
1622 	ifp->if_ioctl = cxgbe_ioctl;
1623 	ifp->if_transmit = cxgbe_transmit;
1624 	ifp->if_qflush = cxgbe_qflush;
1625 	ifp->if_get_counter = cxgbe_get_counter;
1626 
1627 	ifp->if_capabilities = T4_CAP;
1628 #ifdef TCP_OFFLOAD
1629 	if (vi->nofldrxq != 0)
1630 		ifp->if_capabilities |= IFCAP_TOE;
1631 #endif
1632 	ifp->if_capenable = T4_CAP_ENABLE;
1633 	ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
1634 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6;
1635 
1636 	ifp->if_hw_tsomax = IP_MAXPACKET;
1637 	ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_TSO;
1638 #ifdef RATELIMIT
1639 	if (is_ethoffload(vi->pi->adapter) && vi->nofldtxq != 0)
1640 		ifp->if_hw_tsomaxsegcount = TX_SGL_SEGS_EO_TSO;
1641 #endif
1642 	ifp->if_hw_tsomaxsegsize = 65536;
1643 
1644 	ether_ifattach(ifp, vi->hw_addr);
1645 #ifdef DEV_NETMAP
1646 	if (vi->nnmrxq != 0)
1647 		cxgbe_nm_attach(vi);
1648 #endif
1649 	sb = sbuf_new_auto();
1650 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
1651 #ifdef TCP_OFFLOAD
1652 	if (ifp->if_capabilities & IFCAP_TOE)
1653 		sbuf_printf(sb, "; %d txq, %d rxq (TOE)",
1654 		    vi->nofldtxq, vi->nofldrxq);
1655 #endif
1656 #ifdef DEV_NETMAP
1657 	if (ifp->if_capabilities & IFCAP_NETMAP)
1658 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
1659 		    vi->nnmtxq, vi->nnmrxq);
1660 #endif
1661 	sbuf_finish(sb);
1662 	device_printf(dev, "%s\n", sbuf_data(sb));
1663 	sbuf_delete(sb);
1664 
1665 	vi_sysctls(vi);
1666 
1667 	return (0);
1668 }
1669 
1670 static int
cxgbe_attach(device_t dev)1671 cxgbe_attach(device_t dev)
1672 {
1673 	struct port_info *pi = device_get_softc(dev);
1674 	struct adapter *sc = pi->adapter;
1675 	struct vi_info *vi;
1676 	int i, rc;
1677 
1678 	callout_init_mtx(&pi->tick, &pi->pi_lock, 0);
1679 
1680 	rc = cxgbe_vi_attach(dev, &pi->vi[0]);
1681 	if (rc)
1682 		return (rc);
1683 
1684 	for_each_vi(pi, i, vi) {
1685 		if (i == 0)
1686 			continue;
1687 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, -1);
1688 		if (vi->dev == NULL) {
1689 			device_printf(dev, "failed to add VI %d\n", i);
1690 			continue;
1691 		}
1692 		device_set_softc(vi->dev, vi);
1693 	}
1694 
1695 	cxgbe_sysctls(pi);
1696 
1697 	bus_generic_attach(dev);
1698 
1699 	return (0);
1700 }
1701 
1702 static void
cxgbe_vi_detach(struct vi_info * vi)1703 cxgbe_vi_detach(struct vi_info *vi)
1704 {
1705 	struct ifnet *ifp = vi->ifp;
1706 
1707 	ether_ifdetach(ifp);
1708 
1709 	/* Let detach proceed even if these fail. */
1710 #ifdef DEV_NETMAP
1711 	if (ifp->if_capabilities & IFCAP_NETMAP)
1712 		cxgbe_nm_detach(vi);
1713 #endif
1714 	cxgbe_uninit_synchronized(vi);
1715 	callout_drain(&vi->tick);
1716 	vi_full_uninit(vi);
1717 
1718 	if_free(vi->ifp);
1719 	vi->ifp = NULL;
1720 }
1721 
1722 static int
cxgbe_detach(device_t dev)1723 cxgbe_detach(device_t dev)
1724 {
1725 	struct port_info *pi = device_get_softc(dev);
1726 	struct adapter *sc = pi->adapter;
1727 	int rc;
1728 
1729 	/* Detach the extra VIs first. */
1730 	rc = bus_generic_detach(dev);
1731 	if (rc)
1732 		return (rc);
1733 	device_delete_children(dev);
1734 
1735 	doom_vi(sc, &pi->vi[0]);
1736 
1737 	if (pi->flags & HAS_TRACEQ) {
1738 		sc->traceq = -1;	/* cloner should not create ifnet */
1739 		t4_tracer_port_detach(sc);
1740 	}
1741 
1742 	cxgbe_vi_detach(&pi->vi[0]);
1743 	callout_drain(&pi->tick);
1744 	ifmedia_removeall(&pi->media);
1745 
1746 	end_synchronized_op(sc, 0);
1747 
1748 	return (0);
1749 }
1750 
1751 static void
cxgbe_init(void * arg)1752 cxgbe_init(void *arg)
1753 {
1754 	struct vi_info *vi = arg;
1755 	struct adapter *sc = vi->pi->adapter;
1756 
1757 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
1758 		return;
1759 	cxgbe_init_synchronized(vi);
1760 	end_synchronized_op(sc, 0);
1761 }
1762 
1763 static int
cxgbe_ioctl(struct ifnet * ifp,unsigned long cmd,caddr_t data)1764 cxgbe_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
1765 {
1766 	int rc = 0, mtu, can_sleep, if_flags, if_drv_flags, vi_if_flags;
1767 	struct vi_info *vi = ifp->if_softc;
1768 	struct port_info *pi = vi->pi;
1769 	struct adapter *sc = pi->adapter;
1770 	struct ifreq *ifr = (struct ifreq *)data;
1771 	uint32_t mask;
1772 
1773 	switch (cmd) {
1774 	case SIOCSIFMTU:
1775 		mtu = ifr->ifr_mtu;
1776 		if (mtu < ETHERMIN || mtu > MAX_MTU)
1777 			return (EINVAL);
1778 
1779 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
1780 		if (rc)
1781 			return (rc);
1782 		ifp->if_mtu = mtu;
1783 		if (vi->flags & VI_INIT_DONE) {
1784 			t4_update_fl_bufsize(ifp);
1785 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1786 				rc = update_mac_settings(ifp, XGMAC_MTU);
1787 		}
1788 		end_synchronized_op(sc, 0);
1789 		break;
1790 
1791 	case SIOCSIFFLAGS:
1792 		/*
1793 		 * Decide what to do, with the port lock held.
1794 		 */
1795 		PORT_LOCK(pi);
1796 		if_flags = ifp->if_flags;
1797 		if_drv_flags = ifp->if_drv_flags;
1798 		vi_if_flags = vi->if_flags;
1799 		if (if_flags & IFF_UP && if_drv_flags & IFF_DRV_RUNNING &&
1800 		    (vi_if_flags ^ if_flags) & (IFF_PROMISC | IFF_ALLMULTI)) {
1801 			can_sleep = 0;
1802 		} else {
1803 			can_sleep = 1;
1804 		}
1805 		PORT_UNLOCK(pi);
1806 
1807 		/*
1808 		 * ifp/vi flags may change here but we'll just do what our local
1809 		 * copy of the flags indicates and then update the driver owned
1810 		 * ifp/vi flags (in a synch-op and with the port lock held) to
1811 		 * reflect what we did.
1812 		 */
1813 
1814 		rc = begin_synchronized_op(sc, vi,
1815 		    can_sleep ? (SLEEP_OK | INTR_OK) : HOLD_LOCK, "t4flg");
1816 		if (rc) {
1817 			if_printf(ifp, "%ssleepable synch operation failed: %d."
1818 			    "  if_flags 0x%08x, if_drv_flags 0x%08x\n",
1819 			    can_sleep ? "" : "non-", rc, if_flags,
1820 			    if_drv_flags);
1821 			return (rc);
1822 		}
1823 
1824 		if (if_flags & IFF_UP) {
1825 			if (if_drv_flags & IFF_DRV_RUNNING) {
1826 				if ((if_flags ^ vi_if_flags) &
1827 				    (IFF_PROMISC | IFF_ALLMULTI)) {
1828 					MPASS(can_sleep == 0);
1829 					rc = update_mac_settings(ifp,
1830 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
1831 				}
1832 			} else {
1833 				MPASS(can_sleep == 1);
1834 				rc = cxgbe_init_synchronized(vi);
1835 			}
1836 		} else if (if_drv_flags & IFF_DRV_RUNNING) {
1837 			MPASS(can_sleep == 1);
1838 			rc = cxgbe_uninit_synchronized(vi);
1839 		}
1840 		PORT_LOCK(pi);
1841 		vi->if_flags = if_flags;
1842 		PORT_UNLOCK(pi);
1843 		end_synchronized_op(sc, can_sleep ? 0 : LOCK_HELD);
1844 		break;
1845 
1846 	case SIOCADDMULTI:
1847 	case SIOCDELMULTI: /* these two are called with a mutex held :-( */
1848 		rc = begin_synchronized_op(sc, vi, HOLD_LOCK, "t4multi");
1849 		if (rc)
1850 			return (rc);
1851 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1852 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
1853 		end_synchronized_op(sc, LOCK_HELD);
1854 		break;
1855 
1856 	case SIOCSIFCAP:
1857 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
1858 		if (rc)
1859 			return (rc);
1860 
1861 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1862 		if (mask & IFCAP_TXCSUM) {
1863 			ifp->if_capenable ^= IFCAP_TXCSUM;
1864 			ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
1865 
1866 			if (IFCAP_TSO4 & ifp->if_capenable &&
1867 			    !(IFCAP_TXCSUM & ifp->if_capenable)) {
1868 				mask &= ~IFCAP_TSO4;
1869 				ifp->if_capenable &= ~IFCAP_TSO4;
1870 				if_printf(ifp,
1871 				    "tso4 disabled due to -txcsum.\n");
1872 			}
1873 		}
1874 		if (mask & IFCAP_TXCSUM_IPV6) {
1875 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
1876 			ifp->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
1877 
1878 			if (IFCAP_TSO6 & ifp->if_capenable &&
1879 			    !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
1880 				mask &= ~IFCAP_TSO6;
1881 				ifp->if_capenable &= ~IFCAP_TSO6;
1882 				if_printf(ifp,
1883 				    "tso6 disabled due to -txcsum6.\n");
1884 			}
1885 		}
1886 		if (mask & IFCAP_RXCSUM)
1887 			ifp->if_capenable ^= IFCAP_RXCSUM;
1888 		if (mask & IFCAP_RXCSUM_IPV6)
1889 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
1890 
1891 		/*
1892 		 * Note that we leave CSUM_TSO alone (it is always set).  The
1893 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
1894 		 * sending a TSO request our way, so it's sufficient to toggle
1895 		 * IFCAP_TSOx only.
1896 		 */
1897 		if (mask & IFCAP_TSO4) {
1898 			if (!(IFCAP_TSO4 & ifp->if_capenable) &&
1899 			    !(IFCAP_TXCSUM & ifp->if_capenable)) {
1900 				if_printf(ifp, "enable txcsum first.\n");
1901 				rc = EAGAIN;
1902 				goto fail;
1903 			}
1904 			ifp->if_capenable ^= IFCAP_TSO4;
1905 		}
1906 		if (mask & IFCAP_TSO6) {
1907 			if (!(IFCAP_TSO6 & ifp->if_capenable) &&
1908 			    !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
1909 				if_printf(ifp, "enable txcsum6 first.\n");
1910 				rc = EAGAIN;
1911 				goto fail;
1912 			}
1913 			ifp->if_capenable ^= IFCAP_TSO6;
1914 		}
1915 		if (mask & IFCAP_LRO) {
1916 #if defined(INET) || defined(INET6)
1917 			int i;
1918 			struct sge_rxq *rxq;
1919 
1920 			ifp->if_capenable ^= IFCAP_LRO;
1921 			for_each_rxq(vi, i, rxq) {
1922 				if (ifp->if_capenable & IFCAP_LRO)
1923 					rxq->iq.flags |= IQ_LRO_ENABLED;
1924 				else
1925 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
1926 			}
1927 #endif
1928 		}
1929 #ifdef TCP_OFFLOAD
1930 		if (mask & IFCAP_TOE) {
1931 			int enable = (ifp->if_capenable ^ mask) & IFCAP_TOE;
1932 
1933 			rc = toe_capability(vi, enable);
1934 			if (rc != 0)
1935 				goto fail;
1936 
1937 			ifp->if_capenable ^= mask;
1938 		}
1939 #endif
1940 		if (mask & IFCAP_VLAN_HWTAGGING) {
1941 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1942 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1943 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
1944 		}
1945 		if (mask & IFCAP_VLAN_MTU) {
1946 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
1947 
1948 			/* Need to find out how to disable auto-mtu-inflation */
1949 		}
1950 		if (mask & IFCAP_VLAN_HWTSO)
1951 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1952 		if (mask & IFCAP_VLAN_HWCSUM)
1953 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
1954 
1955 #ifdef VLAN_CAPABILITIES
1956 		VLAN_CAPABILITIES(ifp);
1957 #endif
1958 fail:
1959 		end_synchronized_op(sc, 0);
1960 		break;
1961 
1962 	case SIOCSIFMEDIA:
1963 	case SIOCGIFMEDIA:
1964 	case SIOCGIFXMEDIA:
1965 		ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
1966 		break;
1967 
1968 	case SIOCGI2C: {
1969 		struct ifi2creq i2c;
1970 
1971 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
1972 		if (rc != 0)
1973 			break;
1974 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
1975 			rc = EPERM;
1976 			break;
1977 		}
1978 		if (i2c.len > sizeof(i2c.data)) {
1979 			rc = EINVAL;
1980 			break;
1981 		}
1982 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
1983 		if (rc)
1984 			return (rc);
1985 		rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
1986 		    i2c.offset, i2c.len, &i2c.data[0]);
1987 		end_synchronized_op(sc, 0);
1988 		if (rc == 0)
1989 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
1990 		break;
1991 	}
1992 
1993 	default:
1994 		rc = ether_ioctl(ifp, cmd, data);
1995 	}
1996 
1997 	return (rc);
1998 }
1999 
2000 static int
cxgbe_transmit(struct ifnet * ifp,struct mbuf * m)2001 cxgbe_transmit(struct ifnet *ifp, struct mbuf *m)
2002 {
2003 	struct vi_info *vi = ifp->if_softc;
2004 	struct port_info *pi = vi->pi;
2005 	struct adapter *sc = pi->adapter;
2006 	struct sge_txq *txq;
2007 	void *items[1];
2008 	int rc;
2009 
2010 	M_ASSERTPKTHDR(m);
2011 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
2012 
2013 	if (__predict_false(pi->link_cfg.link_ok == false)) {
2014 		m_freem(m);
2015 		return (ENETDOWN);
2016 	}
2017 
2018 	rc = parse_pkt(sc, &m);
2019 	if (__predict_false(rc != 0)) {
2020 		MPASS(m == NULL);			/* was freed already */
2021 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
2022 		return (rc);
2023 	}
2024 
2025 	/* Select a txq. */
2026 	txq = &sc->sge.txq[vi->first_txq];
2027 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2028 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
2029 		    vi->rsrv_noflowq);
2030 
2031 	items[0] = m;
2032 	rc = mp_ring_enqueue(txq->r, items, 1, 4096);
2033 	if (__predict_false(rc != 0))
2034 		m_freem(m);
2035 
2036 	return (rc);
2037 }
2038 
2039 static void
cxgbe_qflush(struct ifnet * ifp)2040 cxgbe_qflush(struct ifnet *ifp)
2041 {
2042 	struct vi_info *vi = ifp->if_softc;
2043 	struct sge_txq *txq;
2044 	int i;
2045 
2046 	/* queues do not exist if !VI_INIT_DONE. */
2047 	if (vi->flags & VI_INIT_DONE) {
2048 		for_each_txq(vi, i, txq) {
2049 			TXQ_LOCK(txq);
2050 			txq->eq.flags |= EQ_QFLUSH;
2051 			TXQ_UNLOCK(txq);
2052 			while (!mp_ring_is_idle(txq->r)) {
2053 				mp_ring_check_drainage(txq->r, 0);
2054 				pause("qflush", 1);
2055 			}
2056 			TXQ_LOCK(txq);
2057 			txq->eq.flags &= ~EQ_QFLUSH;
2058 			TXQ_UNLOCK(txq);
2059 		}
2060 	}
2061 	if_qflush(ifp);
2062 }
2063 
2064 static uint64_t
vi_get_counter(struct ifnet * ifp,ift_counter c)2065 vi_get_counter(struct ifnet *ifp, ift_counter c)
2066 {
2067 	struct vi_info *vi = ifp->if_softc;
2068 	struct fw_vi_stats_vf *s = &vi->stats;
2069 
2070 	vi_refresh_stats(vi->pi->adapter, vi);
2071 
2072 	switch (c) {
2073 	case IFCOUNTER_IPACKETS:
2074 		return (s->rx_bcast_frames + s->rx_mcast_frames +
2075 		    s->rx_ucast_frames);
2076 	case IFCOUNTER_IERRORS:
2077 		return (s->rx_err_frames);
2078 	case IFCOUNTER_OPACKETS:
2079 		return (s->tx_bcast_frames + s->tx_mcast_frames +
2080 		    s->tx_ucast_frames + s->tx_offload_frames);
2081 	case IFCOUNTER_OERRORS:
2082 		return (s->tx_drop_frames);
2083 	case IFCOUNTER_IBYTES:
2084 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
2085 		    s->rx_ucast_bytes);
2086 	case IFCOUNTER_OBYTES:
2087 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
2088 		    s->tx_ucast_bytes + s->tx_offload_bytes);
2089 	case IFCOUNTER_IMCASTS:
2090 		return (s->rx_mcast_frames);
2091 	case IFCOUNTER_OMCASTS:
2092 		return (s->tx_mcast_frames);
2093 	case IFCOUNTER_OQDROPS: {
2094 		uint64_t drops;
2095 
2096 		drops = 0;
2097 		if (vi->flags & VI_INIT_DONE) {
2098 			int i;
2099 			struct sge_txq *txq;
2100 
2101 			for_each_txq(vi, i, txq)
2102 				drops += counter_u64_fetch(txq->r->drops);
2103 		}
2104 
2105 		return (drops);
2106 
2107 	}
2108 
2109 	default:
2110 		return (if_get_counter_default(ifp, c));
2111 	}
2112 }
2113 
2114 uint64_t
cxgbe_get_counter(struct ifnet * ifp,ift_counter c)2115 cxgbe_get_counter(struct ifnet *ifp, ift_counter c)
2116 {
2117 	struct vi_info *vi = ifp->if_softc;
2118 	struct port_info *pi = vi->pi;
2119 	struct adapter *sc = pi->adapter;
2120 	struct port_stats *s = &pi->stats;
2121 
2122 	if (pi->nvi > 1 || sc->flags & IS_VF)
2123 		return (vi_get_counter(ifp, c));
2124 
2125 	cxgbe_refresh_stats(sc, pi);
2126 
2127 	switch (c) {
2128 	case IFCOUNTER_IPACKETS:
2129 		return (s->rx_frames);
2130 
2131 	case IFCOUNTER_IERRORS:
2132 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
2133 		    s->rx_fcs_err + s->rx_len_err);
2134 
2135 	case IFCOUNTER_OPACKETS:
2136 		return (s->tx_frames);
2137 
2138 	case IFCOUNTER_OERRORS:
2139 		return (s->tx_error_frames);
2140 
2141 	case IFCOUNTER_IBYTES:
2142 		return (s->rx_octets);
2143 
2144 	case IFCOUNTER_OBYTES:
2145 		return (s->tx_octets);
2146 
2147 	case IFCOUNTER_IMCASTS:
2148 		return (s->rx_mcast_frames);
2149 
2150 	case IFCOUNTER_OMCASTS:
2151 		return (s->tx_mcast_frames);
2152 
2153 	case IFCOUNTER_IQDROPS:
2154 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
2155 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
2156 		    s->rx_trunc3 + pi->tnl_cong_drops);
2157 
2158 	case IFCOUNTER_OQDROPS: {
2159 		uint64_t drops;
2160 
2161 		drops = s->tx_drop;
2162 		if (vi->flags & VI_INIT_DONE) {
2163 			int i;
2164 			struct sge_txq *txq;
2165 
2166 			for_each_txq(vi, i, txq)
2167 				drops += counter_u64_fetch(txq->r->drops);
2168 		}
2169 
2170 		return (drops);
2171 
2172 	}
2173 
2174 	default:
2175 		return (if_get_counter_default(ifp, c));
2176 	}
2177 }
2178 
2179 /*
2180  * The kernel picks a media from the list we had provided but we still validate
2181  * the requeste.
2182  */
2183 int
cxgbe_media_change(struct ifnet * ifp)2184 cxgbe_media_change(struct ifnet *ifp)
2185 {
2186 	struct vi_info *vi = ifp->if_softc;
2187 	struct port_info *pi = vi->pi;
2188 	struct ifmedia *ifm = &pi->media;
2189 	struct link_config *lc = &pi->link_cfg;
2190 	struct adapter *sc = pi->adapter;
2191 	int rc;
2192 
2193 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
2194 	if (rc != 0)
2195 		return (rc);
2196 	PORT_LOCK(pi);
2197 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
2198 		/* ifconfig .. media autoselect */
2199 		if (!(lc->supported & FW_PORT_CAP32_ANEG)) {
2200 			rc = ENOTSUP; /* AN not supported by transceiver */
2201 			goto done;
2202 		}
2203 		lc->requested_aneg = AUTONEG_ENABLE;
2204 		lc->requested_speed = 0;
2205 		lc->requested_fc |= PAUSE_AUTONEG;
2206 	} else {
2207 		lc->requested_aneg = AUTONEG_DISABLE;
2208 		lc->requested_speed =
2209 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
2210 		lc->requested_fc = 0;
2211 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
2212 			lc->requested_fc |= PAUSE_RX;
2213 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
2214 			lc->requested_fc |= PAUSE_TX;
2215 	}
2216 	if (pi->up_vis > 0) {
2217 		fixup_link_config(pi);
2218 		rc = apply_link_config(pi);
2219 	}
2220 done:
2221 	PORT_UNLOCK(pi);
2222 	end_synchronized_op(sc, 0);
2223 	return (rc);
2224 }
2225 
2226 /*
2227  * Base media word (without ETHER, pause, link active, etc.) for the port at the
2228  * given speed.
2229  */
2230 static int
port_mword(struct port_info * pi,uint32_t speed)2231 port_mword(struct port_info *pi, uint32_t speed)
2232 {
2233 
2234 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
2235 	MPASS(powerof2(speed));
2236 
2237 	switch(pi->port_type) {
2238 	case FW_PORT_TYPE_BT_SGMII:
2239 	case FW_PORT_TYPE_BT_XFI:
2240 	case FW_PORT_TYPE_BT_XAUI:
2241 		/* BaseT */
2242 		switch (speed) {
2243 		case FW_PORT_CAP32_SPEED_100M:
2244 			return (IFM_100_T);
2245 		case FW_PORT_CAP32_SPEED_1G:
2246 			return (IFM_1000_T);
2247 		case FW_PORT_CAP32_SPEED_10G:
2248 			return (IFM_10G_T);
2249 		}
2250 		break;
2251 	case FW_PORT_TYPE_KX4:
2252 		if (speed == FW_PORT_CAP32_SPEED_10G)
2253 			return (IFM_10G_KX4);
2254 		break;
2255 	case FW_PORT_TYPE_CX4:
2256 		if (speed == FW_PORT_CAP32_SPEED_10G)
2257 			return (IFM_10G_CX4);
2258 		break;
2259 	case FW_PORT_TYPE_KX:
2260 		if (speed == FW_PORT_CAP32_SPEED_1G)
2261 			return (IFM_1000_KX);
2262 		break;
2263 	case FW_PORT_TYPE_KR:
2264 	case FW_PORT_TYPE_BP_AP:
2265 	case FW_PORT_TYPE_BP4_AP:
2266 	case FW_PORT_TYPE_BP40_BA:
2267 	case FW_PORT_TYPE_KR4_100G:
2268 	case FW_PORT_TYPE_KR_SFP28:
2269 	case FW_PORT_TYPE_KR_XLAUI:
2270 		switch (speed) {
2271 		case FW_PORT_CAP32_SPEED_1G:
2272 			return (IFM_1000_KX);
2273 		case FW_PORT_CAP32_SPEED_10G:
2274 			return (IFM_10G_KR);
2275 		case FW_PORT_CAP32_SPEED_25G:
2276 			return (IFM_25G_KR);
2277 		case FW_PORT_CAP32_SPEED_40G:
2278 			return (IFM_40G_KR4);
2279 		case FW_PORT_CAP32_SPEED_50G:
2280 			return (IFM_50G_KR2);
2281 		case FW_PORT_CAP32_SPEED_100G:
2282 			return (IFM_100G_KR4);
2283 		}
2284 		break;
2285 	case FW_PORT_TYPE_FIBER_XFI:
2286 	case FW_PORT_TYPE_FIBER_XAUI:
2287 	case FW_PORT_TYPE_SFP:
2288 	case FW_PORT_TYPE_QSFP_10G:
2289 	case FW_PORT_TYPE_QSA:
2290 	case FW_PORT_TYPE_QSFP:
2291 	case FW_PORT_TYPE_CR4_QSFP:
2292 	case FW_PORT_TYPE_CR_QSFP:
2293 	case FW_PORT_TYPE_CR2_QSFP:
2294 	case FW_PORT_TYPE_SFP28:
2295 		/* Pluggable transceiver */
2296 		switch (pi->mod_type) {
2297 		case FW_PORT_MOD_TYPE_LR:
2298 			switch (speed) {
2299 			case FW_PORT_CAP32_SPEED_1G:
2300 				return (IFM_1000_LX);
2301 			case FW_PORT_CAP32_SPEED_10G:
2302 				return (IFM_10G_LR);
2303 			case FW_PORT_CAP32_SPEED_25G:
2304 				return (IFM_25G_LR);
2305 			case FW_PORT_CAP32_SPEED_40G:
2306 				return (IFM_40G_LR4);
2307 			case FW_PORT_CAP32_SPEED_50G:
2308 				return (IFM_50G_LR2);
2309 			case FW_PORT_CAP32_SPEED_100G:
2310 				return (IFM_100G_LR4);
2311 			}
2312 			break;
2313 		case FW_PORT_MOD_TYPE_SR:
2314 			switch (speed) {
2315 			case FW_PORT_CAP32_SPEED_1G:
2316 				return (IFM_1000_SX);
2317 			case FW_PORT_CAP32_SPEED_10G:
2318 				return (IFM_10G_SR);
2319 			case FW_PORT_CAP32_SPEED_25G:
2320 				return (IFM_25G_SR);
2321 			case FW_PORT_CAP32_SPEED_40G:
2322 				return (IFM_40G_SR4);
2323 			case FW_PORT_CAP32_SPEED_50G:
2324 				return (IFM_50G_SR2);
2325 			case FW_PORT_CAP32_SPEED_100G:
2326 				return (IFM_100G_SR4);
2327 			}
2328 			break;
2329 		case FW_PORT_MOD_TYPE_ER:
2330 			if (speed == FW_PORT_CAP32_SPEED_10G)
2331 				return (IFM_10G_ER);
2332 			break;
2333 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
2334 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
2335 			switch (speed) {
2336 			case FW_PORT_CAP32_SPEED_1G:
2337 				return (IFM_1000_CX);
2338 			case FW_PORT_CAP32_SPEED_10G:
2339 				return (IFM_10G_TWINAX);
2340 			case FW_PORT_CAP32_SPEED_25G:
2341 				return (IFM_25G_CR);
2342 			case FW_PORT_CAP32_SPEED_40G:
2343 				return (IFM_40G_CR4);
2344 			case FW_PORT_CAP32_SPEED_50G:
2345 				return (IFM_50G_CR2);
2346 			case FW_PORT_CAP32_SPEED_100G:
2347 				return (IFM_100G_CR4);
2348 			}
2349 			break;
2350 		case FW_PORT_MOD_TYPE_LRM:
2351 			if (speed == FW_PORT_CAP32_SPEED_10G)
2352 				return (IFM_10G_LRM);
2353 			break;
2354 		case FW_PORT_MOD_TYPE_NA:
2355 			MPASS(0);	/* Not pluggable? */
2356 			/* fall throough */
2357 		case FW_PORT_MOD_TYPE_ERROR:
2358 		case FW_PORT_MOD_TYPE_UNKNOWN:
2359 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
2360 			break;
2361 		case FW_PORT_MOD_TYPE_NONE:
2362 			return (IFM_NONE);
2363 		}
2364 		break;
2365 	case FW_PORT_TYPE_NONE:
2366 		return (IFM_NONE);
2367 	}
2368 
2369 	return (IFM_UNKNOWN);
2370 }
2371 
2372 void
cxgbe_media_status(struct ifnet * ifp,struct ifmediareq * ifmr)2373 cxgbe_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
2374 {
2375 	struct vi_info *vi = ifp->if_softc;
2376 	struct port_info *pi = vi->pi;
2377 	struct adapter *sc = pi->adapter;
2378 	struct link_config *lc = &pi->link_cfg;
2379 
2380 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4med") != 0)
2381 		return;
2382 	PORT_LOCK(pi);
2383 
2384 	if (pi->up_vis == 0) {
2385 		/*
2386 		 * If all the interfaces are administratively down the firmware
2387 		 * does not report transceiver changes.  Refresh port info here
2388 		 * so that ifconfig displays accurate ifmedia at all times.
2389 		 * This is the only reason we have a synchronized op in this
2390 		 * function.  Just PORT_LOCK would have been enough otherwise.
2391 		 */
2392 		t4_update_port_info(pi);
2393 		build_medialist(pi);
2394 	}
2395 
2396 	/* ifm_status */
2397 	ifmr->ifm_status = IFM_AVALID;
2398 	if (lc->link_ok == false)
2399 		goto done;
2400 	ifmr->ifm_status |= IFM_ACTIVE;
2401 
2402 	/* ifm_active */
2403 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
2404 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
2405 	if (lc->fc & PAUSE_RX)
2406 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
2407 	if (lc->fc & PAUSE_TX)
2408 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
2409 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
2410 done:
2411 	PORT_UNLOCK(pi);
2412 	end_synchronized_op(sc, 0);
2413 }
2414 
2415 static int
vcxgbe_probe(device_t dev)2416 vcxgbe_probe(device_t dev)
2417 {
2418 	char buf[128];
2419 	struct vi_info *vi = device_get_softc(dev);
2420 
2421 	snprintf(buf, sizeof(buf), "port %d vi %td", vi->pi->port_id,
2422 	    vi - vi->pi->vi);
2423 	device_set_desc_copy(dev, buf);
2424 
2425 	return (BUS_PROBE_DEFAULT);
2426 }
2427 
2428 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)2429 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
2430 {
2431 	int func, index, rc;
2432 	uint32_t param, val;
2433 
2434 	ASSERT_SYNCHRONIZED_OP(sc);
2435 
2436 	index = vi - pi->vi;
2437 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
2438 	KASSERT(index < nitems(vi_mac_funcs),
2439 	    ("%s: VI %s doesn't have a MAC func", __func__,
2440 	    device_get_nameunit(vi->dev)));
2441 	func = vi_mac_funcs[index];
2442 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
2443 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
2444 	if (rc < 0) {
2445 		device_printf(vi->dev, "failed to allocate virtual interface %d"
2446 		    "for port %d: %d\n", index, pi->port_id, -rc);
2447 		return (-rc);
2448 	}
2449 	vi->viid = rc;
2450 
2451 	if (vi->rss_size == 1) {
2452 		/*
2453 		 * This VI didn't get a slice of the RSS table.  Reduce the
2454 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
2455 		 * configuration file (nvi, rssnvi for this PF) if this is a
2456 		 * problem.
2457 		 */
2458 		device_printf(vi->dev, "RSS table not available.\n");
2459 		vi->rss_base = 0xffff;
2460 
2461 		return (0);
2462 	}
2463 
2464 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
2465 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
2466 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
2467 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
2468 	if (rc)
2469 		vi->rss_base = 0xffff;
2470 	else {
2471 		MPASS((val >> 16) == vi->rss_size);
2472 		vi->rss_base = val & 0xffff;
2473 	}
2474 
2475 	return (0);
2476 }
2477 
2478 static int
vcxgbe_attach(device_t dev)2479 vcxgbe_attach(device_t dev)
2480 {
2481 	struct vi_info *vi;
2482 	struct port_info *pi;
2483 	struct adapter *sc;
2484 	int rc;
2485 
2486 	vi = device_get_softc(dev);
2487 	pi = vi->pi;
2488 	sc = pi->adapter;
2489 
2490 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
2491 	if (rc)
2492 		return (rc);
2493 	rc = alloc_extra_vi(sc, pi, vi);
2494 	end_synchronized_op(sc, 0);
2495 	if (rc)
2496 		return (rc);
2497 
2498 	rc = cxgbe_vi_attach(dev, vi);
2499 	if (rc) {
2500 		t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
2501 		return (rc);
2502 	}
2503 	return (0);
2504 }
2505 
2506 static int
vcxgbe_detach(device_t dev)2507 vcxgbe_detach(device_t dev)
2508 {
2509 	struct vi_info *vi;
2510 	struct adapter *sc;
2511 
2512 	vi = device_get_softc(dev);
2513 	sc = vi->pi->adapter;
2514 
2515 	doom_vi(sc, vi);
2516 
2517 	cxgbe_vi_detach(vi);
2518 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
2519 
2520 	end_synchronized_op(sc, 0);
2521 
2522 	return (0);
2523 }
2524 
2525 static struct callout fatal_callout;
2526 
2527 static void
delayed_panic(void * arg)2528 delayed_panic(void *arg)
2529 {
2530 	struct adapter *sc = arg;
2531 
2532 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
2533 }
2534 
2535 void
t4_fatal_err(struct adapter * sc,bool fw_error)2536 t4_fatal_err(struct adapter *sc, bool fw_error)
2537 {
2538 
2539 	t4_shutdown_adapter(sc);
2540 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped.\n",
2541 	    device_get_nameunit(sc->dev));
2542 	if (fw_error) {
2543 		ASSERT_SYNCHRONIZED_OP(sc);
2544 		sc->flags |= ADAP_ERR;
2545 	} else {
2546 		ADAPTER_LOCK(sc);
2547 		sc->flags |= ADAP_ERR;
2548 		ADAPTER_UNLOCK(sc);
2549 	}
2550 
2551 	if (t4_panic_on_fatal_err) {
2552 		log(LOG_ALERT, "%s: panic on fatal error after 30s",
2553 		    device_get_nameunit(sc->dev));
2554 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
2555 	}
2556 }
2557 
2558 void
t4_add_adapter(struct adapter * sc)2559 t4_add_adapter(struct adapter *sc)
2560 {
2561 	sx_xlock(&t4_list_lock);
2562 	SLIST_INSERT_HEAD(&t4_list, sc, link);
2563 	sx_xunlock(&t4_list_lock);
2564 }
2565 
2566 int
t4_map_bars_0_and_4(struct adapter * sc)2567 t4_map_bars_0_and_4(struct adapter *sc)
2568 {
2569 	sc->regs_rid = PCIR_BAR(0);
2570 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
2571 	    &sc->regs_rid, RF_ACTIVE);
2572 	if (sc->regs_res == NULL) {
2573 		device_printf(sc->dev, "cannot map registers.\n");
2574 		return (ENXIO);
2575 	}
2576 	sc->bt = rman_get_bustag(sc->regs_res);
2577 	sc->bh = rman_get_bushandle(sc->regs_res);
2578 	sc->mmio_len = rman_get_size(sc->regs_res);
2579 	setbit(&sc->doorbells, DOORBELL_KDB);
2580 
2581 	sc->msix_rid = PCIR_BAR(4);
2582 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
2583 	    &sc->msix_rid, RF_ACTIVE);
2584 	if (sc->msix_res == NULL) {
2585 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
2586 		return (ENXIO);
2587 	}
2588 
2589 	return (0);
2590 }
2591 
2592 int
t4_map_bar_2(struct adapter * sc)2593 t4_map_bar_2(struct adapter *sc)
2594 {
2595 
2596 	/*
2597 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
2598 	 * to map it if RDMA is disabled.
2599 	 */
2600 	if (is_t4(sc) && sc->rdmacaps == 0)
2601 		return (0);
2602 
2603 	sc->udbs_rid = PCIR_BAR(2);
2604 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
2605 	    &sc->udbs_rid, RF_ACTIVE);
2606 	if (sc->udbs_res == NULL) {
2607 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
2608 		return (ENXIO);
2609 	}
2610 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
2611 
2612 	if (chip_id(sc) >= CHELSIO_T5) {
2613 		setbit(&sc->doorbells, DOORBELL_UDB);
2614 #if defined(__i386__) || defined(__amd64__)
2615 		if (t5_write_combine) {
2616 			int rc, mode;
2617 
2618 			/*
2619 			 * Enable write combining on BAR2.  This is the
2620 			 * userspace doorbell BAR and is split into 128B
2621 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
2622 			 * with an egress queue.  The first 64B has the doorbell
2623 			 * and the second 64B can be used to submit a tx work
2624 			 * request with an implicit doorbell.
2625 			 */
2626 
2627 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
2628 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
2629 			if (rc == 0) {
2630 				clrbit(&sc->doorbells, DOORBELL_UDB);
2631 				setbit(&sc->doorbells, DOORBELL_WCWR);
2632 				setbit(&sc->doorbells, DOORBELL_UDBWC);
2633 			} else {
2634 				device_printf(sc->dev,
2635 				    "couldn't enable write combining: %d\n",
2636 				    rc);
2637 			}
2638 
2639 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
2640 			t4_write_reg(sc, A_SGE_STAT_CFG,
2641 			    V_STATSOURCE_T5(7) | mode);
2642 		}
2643 #endif
2644 	}
2645 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
2646 
2647 	return (0);
2648 }
2649 
2650 struct memwin_init {
2651 	uint32_t base;
2652 	uint32_t aperture;
2653 };
2654 
2655 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
2656 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
2657 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
2658 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
2659 };
2660 
2661 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
2662 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
2663 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
2664 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
2665 };
2666 
2667 static void
setup_memwin(struct adapter * sc)2668 setup_memwin(struct adapter *sc)
2669 {
2670 	const struct memwin_init *mw_init;
2671 	struct memwin *mw;
2672 	int i;
2673 	uint32_t bar0;
2674 
2675 	if (is_t4(sc)) {
2676 		/*
2677 		 * Read low 32b of bar0 indirectly via the hardware backdoor
2678 		 * mechanism.  Works from within PCI passthrough environments
2679 		 * too, where rman_get_start() can return a different value.  We
2680 		 * need to program the T4 memory window decoders with the actual
2681 		 * addresses that will be coming across the PCIe link.
2682 		 */
2683 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
2684 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
2685 
2686 		mw_init = &t4_memwin[0];
2687 	} else {
2688 		/* T5+ use the relative offset inside the PCIe BAR */
2689 		bar0 = 0;
2690 
2691 		mw_init = &t5_memwin[0];
2692 	}
2693 
2694 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
2695 		rw_init(&mw->mw_lock, "memory window access");
2696 		mw->mw_base = mw_init->base;
2697 		mw->mw_aperture = mw_init->aperture;
2698 		mw->mw_curpos = 0;
2699 		t4_write_reg(sc,
2700 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
2701 		    (mw->mw_base + bar0) | V_BIR(0) |
2702 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
2703 		rw_wlock(&mw->mw_lock);
2704 		position_memwin(sc, i, 0);
2705 		rw_wunlock(&mw->mw_lock);
2706 	}
2707 
2708 	/* flush */
2709 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
2710 }
2711 
2712 /*
2713  * Positions the memory window at the given address in the card's address space.
2714  * There are some alignment requirements and the actual position may be at an
2715  * address prior to the requested address.  mw->mw_curpos always has the actual
2716  * position of the window.
2717  */
2718 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)2719 position_memwin(struct adapter *sc, int idx, uint32_t addr)
2720 {
2721 	struct memwin *mw;
2722 	uint32_t pf;
2723 	uint32_t reg;
2724 
2725 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
2726 	mw = &sc->memwin[idx];
2727 	rw_assert(&mw->mw_lock, RA_WLOCKED);
2728 
2729 	if (is_t4(sc)) {
2730 		pf = 0;
2731 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
2732 	} else {
2733 		pf = V_PFNUM(sc->pf);
2734 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
2735 	}
2736 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
2737 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
2738 	t4_read_reg(sc, reg);	/* flush */
2739 }
2740 
2741 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)2742 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
2743     int len, int rw)
2744 {
2745 	struct memwin *mw;
2746 	uint32_t mw_end, v;
2747 
2748 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
2749 
2750 	/* Memory can only be accessed in naturally aligned 4 byte units */
2751 	if (addr & 3 || len & 3 || len <= 0)
2752 		return (EINVAL);
2753 
2754 	mw = &sc->memwin[idx];
2755 	while (len > 0) {
2756 		rw_rlock(&mw->mw_lock);
2757 		mw_end = mw->mw_curpos + mw->mw_aperture;
2758 		if (addr >= mw_end || addr < mw->mw_curpos) {
2759 			/* Will need to reposition the window */
2760 			if (!rw_try_upgrade(&mw->mw_lock)) {
2761 				rw_runlock(&mw->mw_lock);
2762 				rw_wlock(&mw->mw_lock);
2763 			}
2764 			rw_assert(&mw->mw_lock, RA_WLOCKED);
2765 			position_memwin(sc, idx, addr);
2766 			rw_downgrade(&mw->mw_lock);
2767 			mw_end = mw->mw_curpos + mw->mw_aperture;
2768 		}
2769 		rw_assert(&mw->mw_lock, RA_RLOCKED);
2770 		while (addr < mw_end && len > 0) {
2771 			if (rw == 0) {
2772 				v = t4_read_reg(sc, mw->mw_base + addr -
2773 				    mw->mw_curpos);
2774 				*val++ = le32toh(v);
2775 			} else {
2776 				v = *val++;
2777 				t4_write_reg(sc, mw->mw_base + addr -
2778 				    mw->mw_curpos, htole32(v));
2779 			}
2780 			addr += 4;
2781 			len -= 4;
2782 		}
2783 		rw_runlock(&mw->mw_lock);
2784 	}
2785 
2786 	return (0);
2787 }
2788 
2789 int
alloc_atid_tab(struct tid_info * t,int flags)2790 alloc_atid_tab(struct tid_info *t, int flags)
2791 {
2792 	int i;
2793 
2794 	MPASS(t->natids > 0);
2795 	MPASS(t->atid_tab == NULL);
2796 
2797 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
2798 	    M_ZERO | flags);
2799 	if (t->atid_tab == NULL)
2800 		return (ENOMEM);
2801 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
2802 	t->afree = t->atid_tab;
2803 	t->atids_in_use = 0;
2804 	for (i = 1; i < t->natids; i++)
2805 		t->atid_tab[i - 1].next = &t->atid_tab[i];
2806 	t->atid_tab[t->natids - 1].next = NULL;
2807 
2808 	return (0);
2809 }
2810 
2811 void
free_atid_tab(struct tid_info * t)2812 free_atid_tab(struct tid_info *t)
2813 {
2814 
2815 	KASSERT(t->atids_in_use == 0,
2816 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
2817 
2818 	if (mtx_initialized(&t->atid_lock))
2819 		mtx_destroy(&t->atid_lock);
2820 	free(t->atid_tab, M_CXGBE);
2821 	t->atid_tab = NULL;
2822 }
2823 
2824 int
alloc_atid(struct adapter * sc,void * ctx)2825 alloc_atid(struct adapter *sc, void *ctx)
2826 {
2827 	struct tid_info *t = &sc->tids;
2828 	int atid = -1;
2829 
2830 	mtx_lock(&t->atid_lock);
2831 	if (t->afree) {
2832 		union aopen_entry *p = t->afree;
2833 
2834 		atid = p - t->atid_tab;
2835 		MPASS(atid <= M_TID_TID);
2836 		t->afree = p->next;
2837 		p->data = ctx;
2838 		t->atids_in_use++;
2839 	}
2840 	mtx_unlock(&t->atid_lock);
2841 	return (atid);
2842 }
2843 
2844 void *
lookup_atid(struct adapter * sc,int atid)2845 lookup_atid(struct adapter *sc, int atid)
2846 {
2847 	struct tid_info *t = &sc->tids;
2848 
2849 	return (t->atid_tab[atid].data);
2850 }
2851 
2852 void
free_atid(struct adapter * sc,int atid)2853 free_atid(struct adapter *sc, int atid)
2854 {
2855 	struct tid_info *t = &sc->tids;
2856 	union aopen_entry *p = &t->atid_tab[atid];
2857 
2858 	mtx_lock(&t->atid_lock);
2859 	p->next = t->afree;
2860 	t->afree = p;
2861 	t->atids_in_use--;
2862 	mtx_unlock(&t->atid_lock);
2863 }
2864 
2865 static void
queue_tid_release(struct adapter * sc,int tid)2866 queue_tid_release(struct adapter *sc, int tid)
2867 {
2868 
2869 	CXGBE_UNIMPLEMENTED("deferred tid release");
2870 }
2871 
2872 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)2873 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
2874 {
2875 	struct wrqe *wr;
2876 	struct cpl_tid_release *req;
2877 
2878 	wr = alloc_wrqe(sizeof(*req), ctrlq);
2879 	if (wr == NULL) {
2880 		queue_tid_release(sc, tid);	/* defer */
2881 		return;
2882 	}
2883 	req = wrtod(wr);
2884 
2885 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
2886 
2887 	t4_wrq_tx(sc, wr);
2888 }
2889 
2890 static int
t4_range_cmp(const void * a,const void * b)2891 t4_range_cmp(const void *a, const void *b)
2892 {
2893 	return ((const struct t4_range *)a)->start -
2894 	       ((const struct t4_range *)b)->start;
2895 }
2896 
2897 /*
2898  * Verify that the memory range specified by the addr/len pair is valid within
2899  * the card's address space.
2900  */
2901 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)2902 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
2903 {
2904 	struct t4_range mem_ranges[4], *r, *next;
2905 	uint32_t em, addr_len;
2906 	int i, n, remaining;
2907 
2908 	/* Memory can only be accessed in naturally aligned 4 byte units */
2909 	if (addr & 3 || len & 3 || len == 0)
2910 		return (EINVAL);
2911 
2912 	/* Enabled memories */
2913 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
2914 
2915 	r = &mem_ranges[0];
2916 	n = 0;
2917 	bzero(r, sizeof(mem_ranges));
2918 	if (em & F_EDRAM0_ENABLE) {
2919 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
2920 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
2921 		if (r->size > 0) {
2922 			r->start = G_EDRAM0_BASE(addr_len) << 20;
2923 			if (addr >= r->start &&
2924 			    addr + len <= r->start + r->size)
2925 				return (0);
2926 			r++;
2927 			n++;
2928 		}
2929 	}
2930 	if (em & F_EDRAM1_ENABLE) {
2931 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
2932 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
2933 		if (r->size > 0) {
2934 			r->start = G_EDRAM1_BASE(addr_len) << 20;
2935 			if (addr >= r->start &&
2936 			    addr + len <= r->start + r->size)
2937 				return (0);
2938 			r++;
2939 			n++;
2940 		}
2941 	}
2942 	if (em & F_EXT_MEM_ENABLE) {
2943 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
2944 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
2945 		if (r->size > 0) {
2946 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
2947 			if (addr >= r->start &&
2948 			    addr + len <= r->start + r->size)
2949 				return (0);
2950 			r++;
2951 			n++;
2952 		}
2953 	}
2954 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
2955 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
2956 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
2957 		if (r->size > 0) {
2958 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
2959 			if (addr >= r->start &&
2960 			    addr + len <= r->start + r->size)
2961 				return (0);
2962 			r++;
2963 			n++;
2964 		}
2965 	}
2966 	MPASS(n <= nitems(mem_ranges));
2967 
2968 	if (n > 1) {
2969 		/* Sort and merge the ranges. */
2970 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
2971 
2972 		/* Start from index 0 and examine the next n - 1 entries. */
2973 		r = &mem_ranges[0];
2974 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
2975 
2976 			MPASS(r->size > 0);	/* r is a valid entry. */
2977 			next = r + 1;
2978 			MPASS(next->size > 0);	/* and so is the next one. */
2979 
2980 			while (r->start + r->size >= next->start) {
2981 				/* Merge the next one into the current entry. */
2982 				r->size = max(r->start + r->size,
2983 				    next->start + next->size) - r->start;
2984 				n--;	/* One fewer entry in total. */
2985 				if (--remaining == 0)
2986 					goto done;	/* short circuit */
2987 				next++;
2988 			}
2989 			if (next != r + 1) {
2990 				/*
2991 				 * Some entries were merged into r and next
2992 				 * points to the first valid entry that couldn't
2993 				 * be merged.
2994 				 */
2995 				MPASS(next->size > 0);	/* must be valid */
2996 				memcpy(r + 1, next, remaining * sizeof(*r));
2997 #ifdef INVARIANTS
2998 				/*
2999 				 * This so that the foo->size assertion in the
3000 				 * next iteration of the loop do the right
3001 				 * thing for entries that were pulled up and are
3002 				 * no longer valid.
3003 				 */
3004 				MPASS(n < nitems(mem_ranges));
3005 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
3006 				    sizeof(struct t4_range));
3007 #endif
3008 			}
3009 		}
3010 done:
3011 		/* Done merging the ranges. */
3012 		MPASS(n > 0);
3013 		r = &mem_ranges[0];
3014 		for (i = 0; i < n; i++, r++) {
3015 			if (addr >= r->start &&
3016 			    addr + len <= r->start + r->size)
3017 				return (0);
3018 		}
3019 	}
3020 
3021 	return (EFAULT);
3022 }
3023 
3024 static int
fwmtype_to_hwmtype(int mtype)3025 fwmtype_to_hwmtype(int mtype)
3026 {
3027 
3028 	switch (mtype) {
3029 	case FW_MEMTYPE_EDC0:
3030 		return (MEM_EDC0);
3031 	case FW_MEMTYPE_EDC1:
3032 		return (MEM_EDC1);
3033 	case FW_MEMTYPE_EXTMEM:
3034 		return (MEM_MC0);
3035 	case FW_MEMTYPE_EXTMEM1:
3036 		return (MEM_MC1);
3037 	default:
3038 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
3039 	}
3040 }
3041 
3042 /*
3043  * Verify that the memory range specified by the memtype/offset/len pair is
3044  * valid and lies entirely within the memtype specified.  The global address of
3045  * the start of the range is returned in addr.
3046  */
3047 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)3048 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
3049     uint32_t *addr)
3050 {
3051 	uint32_t em, addr_len, maddr;
3052 
3053 	/* Memory can only be accessed in naturally aligned 4 byte units */
3054 	if (off & 3 || len & 3 || len == 0)
3055 		return (EINVAL);
3056 
3057 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
3058 	switch (fwmtype_to_hwmtype(mtype)) {
3059 	case MEM_EDC0:
3060 		if (!(em & F_EDRAM0_ENABLE))
3061 			return (EINVAL);
3062 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
3063 		maddr = G_EDRAM0_BASE(addr_len) << 20;
3064 		break;
3065 	case MEM_EDC1:
3066 		if (!(em & F_EDRAM1_ENABLE))
3067 			return (EINVAL);
3068 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
3069 		maddr = G_EDRAM1_BASE(addr_len) << 20;
3070 		break;
3071 	case MEM_MC:
3072 		if (!(em & F_EXT_MEM_ENABLE))
3073 			return (EINVAL);
3074 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
3075 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
3076 		break;
3077 	case MEM_MC1:
3078 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
3079 			return (EINVAL);
3080 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
3081 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
3082 		break;
3083 	default:
3084 		return (EINVAL);
3085 	}
3086 
3087 	*addr = maddr + off;	/* global address */
3088 	return (validate_mem_range(sc, *addr, len));
3089 }
3090 
3091 static int
fixup_devlog_params(struct adapter * sc)3092 fixup_devlog_params(struct adapter *sc)
3093 {
3094 	struct devlog_params *dparams = &sc->params.devlog;
3095 	int rc;
3096 
3097 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
3098 	    dparams->size, &dparams->addr);
3099 
3100 	return (rc);
3101 }
3102 
3103 static void
update_nirq(struct intrs_and_queues * iaq,int nports)3104 update_nirq(struct intrs_and_queues *iaq, int nports)
3105 {
3106 	int extra = T4_EXTRA_INTR;
3107 
3108 	iaq->nirq = extra;
3109 	iaq->nirq += nports * (iaq->nrxq + iaq->nofldrxq);
3110 	iaq->nirq += nports * (iaq->num_vis - 1) *
3111 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
3112 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
3113 }
3114 
3115 /*
3116  * Adjust requirements to fit the number of interrupts available.
3117  */
3118 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)3119 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
3120     int navail)
3121 {
3122 	int old_nirq;
3123 	const int nports = sc->params.nports;
3124 
3125 	MPASS(nports > 0);
3126 	MPASS(navail > 0);
3127 
3128 	bzero(iaq, sizeof(*iaq));
3129 	iaq->intr_type = itype;
3130 	iaq->num_vis = t4_num_vis;
3131 	iaq->ntxq = t4_ntxq;
3132 	iaq->ntxq_vi = t4_ntxq_vi;
3133 	iaq->nrxq = t4_nrxq;
3134 	iaq->nrxq_vi = t4_nrxq_vi;
3135 #ifdef TCP_OFFLOAD
3136 	if (is_offload(sc)) {
3137 		iaq->nofldtxq = t4_nofldtxq;
3138 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
3139 		iaq->nofldrxq = t4_nofldrxq;
3140 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
3141 	}
3142 #endif
3143 #ifdef DEV_NETMAP
3144 	iaq->nnmtxq_vi = t4_nnmtxq_vi;
3145 	iaq->nnmrxq_vi = t4_nnmrxq_vi;
3146 #endif
3147 
3148 	update_nirq(iaq, nports);
3149 	if (iaq->nirq <= navail &&
3150 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
3151 		/*
3152 		 * This is the normal case -- there are enough interrupts for
3153 		 * everything.
3154 		 */
3155 		goto done;
3156 	}
3157 
3158 	/*
3159 	 * If extra VIs have been configured try reducing their count and see if
3160 	 * that works.
3161 	 */
3162 	while (iaq->num_vis > 1) {
3163 		iaq->num_vis--;
3164 		update_nirq(iaq, nports);
3165 		if (iaq->nirq <= navail &&
3166 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
3167 			device_printf(sc->dev, "virtual interfaces per port "
3168 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
3169 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
3170 			    "itype %d, navail %u, nirq %d.\n",
3171 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
3172 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
3173 			    itype, navail, iaq->nirq);
3174 			goto done;
3175 		}
3176 	}
3177 
3178 	/*
3179 	 * Extra VIs will not be created.  Log a message if they were requested.
3180 	 */
3181 	MPASS(iaq->num_vis == 1);
3182 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
3183 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
3184 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
3185 	if (iaq->num_vis != t4_num_vis) {
3186 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
3187 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
3188 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
3189 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
3190 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
3191 	}
3192 
3193 	/*
3194 	 * Keep reducing the number of NIC rx queues to the next lower power of
3195 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
3196 	 * if that works.
3197 	 */
3198 	do {
3199 		if (iaq->nrxq > 1) {
3200 			do {
3201 				iaq->nrxq--;
3202 			} while (!powerof2(iaq->nrxq));
3203 		}
3204 		if (iaq->nofldrxq > 1)
3205 			iaq->nofldrxq >>= 1;
3206 
3207 		old_nirq = iaq->nirq;
3208 		update_nirq(iaq, nports);
3209 		if (iaq->nirq <= navail &&
3210 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
3211 			device_printf(sc->dev, "running with reduced number of "
3212 			    "rx queues because of shortage of interrupts.  "
3213 			    "nrxq=%u, nofldrxq=%u.  "
3214 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
3215 			    iaq->nofldrxq, itype, navail, iaq->nirq);
3216 			goto done;
3217 		}
3218 	} while (old_nirq != iaq->nirq);
3219 
3220 	/* One interrupt for everything.  Ugh. */
3221 	device_printf(sc->dev, "running with minimal number of queues.  "
3222 	    "itype %d, navail %u.\n", itype, navail);
3223 	iaq->nirq = 1;
3224 	MPASS(iaq->nrxq == 1);
3225 	iaq->ntxq = 1;
3226 	if (iaq->nofldrxq > 1)
3227 		iaq->nofldtxq = 1;
3228 done:
3229 	MPASS(iaq->num_vis > 0);
3230 	if (iaq->num_vis > 1) {
3231 		MPASS(iaq->nrxq_vi > 0);
3232 		MPASS(iaq->ntxq_vi > 0);
3233 	}
3234 	MPASS(iaq->nirq > 0);
3235 	MPASS(iaq->nrxq > 0);
3236 	MPASS(iaq->ntxq > 0);
3237 	if (itype == INTR_MSI) {
3238 		MPASS(powerof2(iaq->nirq));
3239 	}
3240 }
3241 
3242 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)3243 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
3244 {
3245 	int rc, itype, navail, nalloc;
3246 
3247 	for (itype = INTR_MSIX; itype; itype >>= 1) {
3248 
3249 		if ((itype & t4_intr_types) == 0)
3250 			continue;	/* not allowed */
3251 
3252 		if (itype == INTR_MSIX)
3253 			navail = pci_msix_count(sc->dev);
3254 		else if (itype == INTR_MSI)
3255 			navail = pci_msi_count(sc->dev);
3256 		else
3257 			navail = 1;
3258 restart:
3259 		if (navail == 0)
3260 			continue;
3261 
3262 		calculate_iaq(sc, iaq, itype, navail);
3263 		nalloc = iaq->nirq;
3264 		rc = 0;
3265 		if (itype == INTR_MSIX)
3266 			rc = pci_alloc_msix(sc->dev, &nalloc);
3267 		else if (itype == INTR_MSI)
3268 			rc = pci_alloc_msi(sc->dev, &nalloc);
3269 
3270 		if (rc == 0 && nalloc > 0) {
3271 			if (nalloc == iaq->nirq)
3272 				return (0);
3273 
3274 			/*
3275 			 * Didn't get the number requested.  Use whatever number
3276 			 * the kernel is willing to allocate.
3277 			 */
3278 			device_printf(sc->dev, "fewer vectors than requested, "
3279 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
3280 			    itype, iaq->nirq, nalloc);
3281 			pci_release_msi(sc->dev);
3282 			navail = nalloc;
3283 			goto restart;
3284 		}
3285 
3286 		device_printf(sc->dev,
3287 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
3288 		    itype, rc, iaq->nirq, nalloc);
3289 	}
3290 
3291 	device_printf(sc->dev,
3292 	    "failed to find a usable interrupt type.  "
3293 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
3294 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
3295 
3296 	return (ENXIO);
3297 }
3298 
3299 #define FW_VERSION(chip) ( \
3300     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
3301     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
3302     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
3303     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
3304 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
3305 
3306 /* Just enough of fw_hdr to cover all version info. */
3307 struct fw_h {
3308 	__u8	ver;
3309 	__u8	chip;
3310 	__be16	len512;
3311 	__be32	fw_ver;
3312 	__be32	tp_microcode_ver;
3313 	__u8	intfver_nic;
3314 	__u8	intfver_vnic;
3315 	__u8	intfver_ofld;
3316 	__u8	intfver_ri;
3317 	__u8	intfver_iscsipdu;
3318 	__u8	intfver_iscsi;
3319 	__u8	intfver_fcoepdu;
3320 	__u8	intfver_fcoe;
3321 };
3322 /* Spot check a couple of fields. */
3323 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
3324 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
3325 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
3326 
3327 struct fw_info {
3328 	uint8_t chip;
3329 	char *kld_name;
3330 	char *fw_mod_name;
3331 	struct fw_h fw_h;
3332 } fw_info[] = {
3333 	{
3334 		.chip = CHELSIO_T4,
3335 		.kld_name = "t4fw_cfg",
3336 		.fw_mod_name = "t4fw",
3337 		.fw_h = {
3338 			.chip = FW_HDR_CHIP_T4,
3339 			.fw_ver = htobe32(FW_VERSION(T4)),
3340 			.intfver_nic = FW_INTFVER(T4, NIC),
3341 			.intfver_vnic = FW_INTFVER(T4, VNIC),
3342 			.intfver_ofld = FW_INTFVER(T4, OFLD),
3343 			.intfver_ri = FW_INTFVER(T4, RI),
3344 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
3345 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
3346 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
3347 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
3348 		},
3349 	}, {
3350 		.chip = CHELSIO_T5,
3351 		.kld_name = "t5fw_cfg",
3352 		.fw_mod_name = "t5fw",
3353 		.fw_h = {
3354 			.chip = FW_HDR_CHIP_T5,
3355 			.fw_ver = htobe32(FW_VERSION(T5)),
3356 			.intfver_nic = FW_INTFVER(T5, NIC),
3357 			.intfver_vnic = FW_INTFVER(T5, VNIC),
3358 			.intfver_ofld = FW_INTFVER(T5, OFLD),
3359 			.intfver_ri = FW_INTFVER(T5, RI),
3360 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
3361 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
3362 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
3363 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
3364 		},
3365 	}, {
3366 		.chip = CHELSIO_T6,
3367 		.kld_name = "t6fw_cfg",
3368 		.fw_mod_name = "t6fw",
3369 		.fw_h = {
3370 			.chip = FW_HDR_CHIP_T6,
3371 			.fw_ver = htobe32(FW_VERSION(T6)),
3372 			.intfver_nic = FW_INTFVER(T6, NIC),
3373 			.intfver_vnic = FW_INTFVER(T6, VNIC),
3374 			.intfver_ofld = FW_INTFVER(T6, OFLD),
3375 			.intfver_ri = FW_INTFVER(T6, RI),
3376 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3377 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
3378 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3379 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
3380 		},
3381 	}
3382 };
3383 
3384 static struct fw_info *
find_fw_info(int chip)3385 find_fw_info(int chip)
3386 {
3387 	int i;
3388 
3389 	for (i = 0; i < nitems(fw_info); i++) {
3390 		if (fw_info[i].chip == chip)
3391 			return (&fw_info[i]);
3392 	}
3393 	return (NULL);
3394 }
3395 
3396 /*
3397  * Is the given firmware API compatible with the one the driver was compiled
3398  * with?
3399  */
3400 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)3401 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
3402 {
3403 
3404 	/* short circuit if it's the exact same firmware version */
3405 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
3406 		return (1);
3407 
3408 	/*
3409 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
3410 	 * features that are supported in the driver.
3411 	 */
3412 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
3413 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
3414 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
3415 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
3416 		return (1);
3417 #undef SAME_INTF
3418 
3419 	return (0);
3420 }
3421 
3422 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)3423 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
3424     const struct firmware **fw)
3425 {
3426 	struct fw_info *fw_info;
3427 
3428 	*dcfg = NULL;
3429 	if (fw != NULL)
3430 		*fw = NULL;
3431 
3432 	fw_info = find_fw_info(chip_id(sc));
3433 	if (fw_info == NULL) {
3434 		device_printf(sc->dev,
3435 		    "unable to look up firmware information for chip %d.\n",
3436 		    chip_id(sc));
3437 		return (EINVAL);
3438 	}
3439 
3440 	*dcfg = firmware_get(fw_info->kld_name);
3441 	if (*dcfg != NULL) {
3442 		if (fw != NULL)
3443 			*fw = firmware_get(fw_info->fw_mod_name);
3444 		return (0);
3445 	}
3446 
3447 	return (ENOENT);
3448 }
3449 
3450 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)3451 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
3452     const struct firmware *fw)
3453 {
3454 
3455 	if (fw != NULL)
3456 		firmware_put(fw, FIRMWARE_UNLOAD);
3457 	if (dcfg != NULL)
3458 		firmware_put(dcfg, FIRMWARE_UNLOAD);
3459 }
3460 
3461 /*
3462  * Return values:
3463  * 0 means no firmware install attempted.
3464  * ERESTART means a firmware install was attempted and was successful.
3465  * +ve errno means a firmware install was attempted but failed.
3466  */
3467 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)3468 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
3469     const struct fw_h *drv_fw, const char *reason, int *already)
3470 {
3471 	const struct firmware *cfg, *fw;
3472 	const uint32_t c = be32toh(card_fw->fw_ver);
3473 	uint32_t d, k;
3474 	int rc, fw_install;
3475 	struct fw_h bundled_fw;
3476 	bool load_attempted;
3477 
3478 	cfg = fw = NULL;
3479 	load_attempted = false;
3480 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
3481 
3482 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
3483 	if (t4_fw_install < 0) {
3484 		rc = load_fw_module(sc, &cfg, &fw);
3485 		if (rc != 0 || fw == NULL) {
3486 			device_printf(sc->dev,
3487 			    "failed to load firmware module: %d. cfg %p, fw %p;"
3488 			    " will use compiled-in firmware version for"
3489 			    "hw.cxgbe.fw_install checks.\n",
3490 			    rc, cfg, fw);
3491 		} else {
3492 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
3493 		}
3494 		load_attempted = true;
3495 	}
3496 	d = be32toh(bundled_fw.fw_ver);
3497 
3498 	if (reason != NULL)
3499 		goto install;
3500 
3501 	if ((sc->flags & FW_OK) == 0) {
3502 
3503 		if (c == 0xffffffff) {
3504 			reason = "missing";
3505 			goto install;
3506 		}
3507 
3508 		rc = 0;
3509 		goto done;
3510 	}
3511 
3512 	if (!fw_compatible(card_fw, &bundled_fw)) {
3513 		reason = "incompatible or unusable";
3514 		goto install;
3515 	}
3516 
3517 	if (d > c) {
3518 		reason = "older than the version bundled with this driver";
3519 		goto install;
3520 	}
3521 
3522 	if (fw_install == 2 && d != c) {
3523 		reason = "different than the version bundled with this driver";
3524 		goto install;
3525 	}
3526 
3527 	/* No reason to do anything to the firmware already on the card. */
3528 	rc = 0;
3529 	goto done;
3530 
3531 install:
3532 	rc = 0;
3533 	if ((*already)++)
3534 		goto done;
3535 
3536 	if (fw_install == 0) {
3537 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
3538 		    "but the driver is prohibited from installing a firmware "
3539 		    "on the card.\n",
3540 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
3541 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
3542 
3543 		goto done;
3544 	}
3545 
3546 	/*
3547 	 * We'll attempt to install a firmware.  Load the module first (if it
3548 	 * hasn't been loaded already).
3549 	 */
3550 	if (!load_attempted) {
3551 		rc = load_fw_module(sc, &cfg, &fw);
3552 		if (rc != 0 || fw == NULL) {
3553 			device_printf(sc->dev,
3554 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
3555 			    rc, cfg, fw);
3556 			/* carry on */
3557 		}
3558 	}
3559 	if (fw == NULL) {
3560 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
3561 		    "but the driver cannot take corrective action because it "
3562 		    "is unable to load the firmware module.\n",
3563 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
3564 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
3565 		rc = sc->flags & FW_OK ? 0 : ENOENT;
3566 		goto done;
3567 	}
3568 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
3569 	if (k != d) {
3570 		MPASS(t4_fw_install > 0);
3571 		device_printf(sc->dev,
3572 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
3573 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
3574 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
3575 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
3576 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
3577 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
3578 		rc = sc->flags & FW_OK ? 0 : EINVAL;
3579 		goto done;
3580 	}
3581 
3582 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
3583 	    "installing firmware %u.%u.%u.%u on card.\n",
3584 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
3585 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
3586 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
3587 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
3588 
3589 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
3590 	if (rc != 0) {
3591 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
3592 	} else {
3593 		/* Installed successfully, update the cached header too. */
3594 		rc = ERESTART;
3595 		memcpy(card_fw, fw->data, sizeof(*card_fw));
3596 	}
3597 done:
3598 	unload_fw_module(sc, cfg, fw);
3599 
3600 	return (rc);
3601 }
3602 
3603 /*
3604  * Establish contact with the firmware and attempt to become the master driver.
3605  *
3606  * A firmware will be installed to the card if needed (if the driver is allowed
3607  * to do so).
3608  */
3609 static int
contact_firmware(struct adapter * sc)3610 contact_firmware(struct adapter *sc)
3611 {
3612 	int rc, already = 0;
3613 	enum dev_state state;
3614 	struct fw_info *fw_info;
3615 	struct fw_hdr *card_fw;		/* fw on the card */
3616 	const struct fw_h *drv_fw;
3617 
3618 	fw_info = find_fw_info(chip_id(sc));
3619 	if (fw_info == NULL) {
3620 		device_printf(sc->dev,
3621 		    "unable to look up firmware information for chip %d.\n",
3622 		    chip_id(sc));
3623 		return (EINVAL);
3624 	}
3625 	drv_fw = &fw_info->fw_h;
3626 
3627 	/* Read the header of the firmware on the card */
3628 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
3629 restart:
3630 	rc = -t4_get_fw_hdr(sc, card_fw);
3631 	if (rc != 0) {
3632 		device_printf(sc->dev,
3633 		    "unable to read firmware header from card's flash: %d\n",
3634 		    rc);
3635 		goto done;
3636 	}
3637 
3638 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
3639 	    &already);
3640 	if (rc == ERESTART)
3641 		goto restart;
3642 	if (rc != 0)
3643 		goto done;
3644 
3645 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
3646 	if (rc < 0 || state == DEV_STATE_ERR) {
3647 		rc = -rc;
3648 		device_printf(sc->dev,
3649 		    "failed to connect to the firmware: %d, %d.  "
3650 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
3651 #if 0
3652 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
3653 		    "not responding properly to HELLO", &already) == ERESTART)
3654 			goto restart;
3655 #endif
3656 		goto done;
3657 	}
3658 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
3659 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
3660 
3661 	if (rc == sc->pf) {
3662 		sc->flags |= MASTER_PF;
3663 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
3664 		    NULL, &already);
3665 		if (rc == ERESTART)
3666 			rc = 0;
3667 		else if (rc != 0)
3668 			goto done;
3669 	} else if (state == DEV_STATE_UNINIT) {
3670 		/*
3671 		 * We didn't get to be the master so we definitely won't be
3672 		 * configuring the chip.  It's a bug if someone else hasn't
3673 		 * configured it already.
3674 		 */
3675 		device_printf(sc->dev, "couldn't be master(%d), "
3676 		    "device not already initialized either(%d).  "
3677 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
3678 		rc = EPROTO;
3679 		goto done;
3680 	} else {
3681 		/*
3682 		 * Some other PF is the master and has configured the chip.
3683 		 * This is allowed but untested.
3684 		 */
3685 		device_printf(sc->dev, "PF%d is master, device state %d.  "
3686 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
3687 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
3688 		sc->cfcsum = 0;
3689 		rc = 0;
3690 	}
3691 done:
3692 	if (rc != 0 && sc->flags & FW_OK) {
3693 		t4_fw_bye(sc, sc->mbox);
3694 		sc->flags &= ~FW_OK;
3695 	}
3696 	free(card_fw, M_CXGBE);
3697 	return (rc);
3698 }
3699 
3700 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff)3701 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
3702     uint32_t mtype, uint32_t moff)
3703 {
3704 	struct fw_info *fw_info;
3705 	const struct firmware *dcfg, *rcfg = NULL;
3706 	const uint32_t *cfdata;
3707 	uint32_t cflen, addr;
3708 	int rc;
3709 
3710 	load_fw_module(sc, &dcfg, NULL);
3711 
3712 	/* Card specific interpretation of "default". */
3713 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
3714 		if (pci_get_device(sc->dev) == 0x440a)
3715 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
3716 		if (is_fpga(sc))
3717 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
3718 	}
3719 
3720 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
3721 		if (dcfg == NULL) {
3722 			device_printf(sc->dev,
3723 			    "KLD with default config is not available.\n");
3724 			rc = ENOENT;
3725 			goto done;
3726 		}
3727 		cfdata = dcfg->data;
3728 		cflen = dcfg->datasize & ~3;
3729 	} else {
3730 		char s[32];
3731 
3732 		fw_info = find_fw_info(chip_id(sc));
3733 		if (fw_info == NULL) {
3734 			device_printf(sc->dev,
3735 			    "unable to look up firmware information for chip %d.\n",
3736 			    chip_id(sc));
3737 			rc = EINVAL;
3738 			goto done;
3739 		}
3740 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
3741 
3742 		rcfg = firmware_get(s);
3743 		if (rcfg == NULL) {
3744 			device_printf(sc->dev,
3745 			    "unable to load module \"%s\" for configuration "
3746 			    "profile \"%s\".\n", s, cfg_file);
3747 			rc = ENOENT;
3748 			goto done;
3749 		}
3750 		cfdata = rcfg->data;
3751 		cflen = rcfg->datasize & ~3;
3752 	}
3753 
3754 	if (cflen > FLASH_CFG_MAX_SIZE) {
3755 		device_printf(sc->dev,
3756 		    "config file too long (%d, max allowed is %d).\n",
3757 		    cflen, FLASH_CFG_MAX_SIZE);
3758 		rc = EINVAL;
3759 		goto done;
3760 	}
3761 
3762 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
3763 	if (rc != 0) {
3764 		device_printf(sc->dev,
3765 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
3766 		    __func__, mtype, moff, cflen, rc);
3767 		rc = EINVAL;
3768 		goto done;
3769 	}
3770 	write_via_memwin(sc, 2, addr, cfdata, cflen);
3771 done:
3772 	if (rcfg != NULL)
3773 		firmware_put(rcfg, FIRMWARE_UNLOAD);
3774 	unload_fw_module(sc, dcfg, NULL);
3775 	return (rc);
3776 }
3777 
3778 struct caps_allowed {
3779 	uint16_t nbmcaps;
3780 	uint16_t linkcaps;
3781 	uint16_t switchcaps;
3782 	uint16_t niccaps;
3783 	uint16_t toecaps;
3784 	uint16_t rdmacaps;
3785 	uint16_t cryptocaps;
3786 	uint16_t iscsicaps;
3787 	uint16_t fcoecaps;
3788 };
3789 
3790 #define FW_PARAM_DEV(param) \
3791 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
3792 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
3793 #define FW_PARAM_PFVF(param) \
3794 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
3795 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
3796 
3797 /*
3798  * Provide a configuration profile to the firmware and have it initialize the
3799  * chip accordingly.  This may involve uploading a configuration file to the
3800  * card.
3801  */
3802 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)3803 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
3804     const struct caps_allowed *caps_allowed)
3805 {
3806 	int rc;
3807 	struct fw_caps_config_cmd caps;
3808 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
3809 
3810 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
3811 	if (rc != 0) {
3812 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
3813 		return (rc);
3814 	}
3815 
3816 	bzero(&caps, sizeof(caps));
3817 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
3818 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
3819 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
3820 		mtype = 0;
3821 		moff = 0;
3822 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
3823 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
3824 		mtype = FW_MEMTYPE_FLASH;
3825 		moff = t4_flash_cfg_addr(sc);
3826 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
3827 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
3828 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
3829 		    FW_LEN16(caps));
3830 	} else {
3831 		/*
3832 		 * Ask the firmware where it wants us to upload the config file.
3833 		 */
3834 		param = FW_PARAM_DEV(CF);
3835 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3836 		if (rc != 0) {
3837 			/* No support for config file?  Shouldn't happen. */
3838 			device_printf(sc->dev,
3839 			    "failed to query config file location: %d.\n", rc);
3840 			goto done;
3841 		}
3842 		mtype = G_FW_PARAMS_PARAM_Y(val);
3843 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
3844 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
3845 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
3846 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
3847 		    FW_LEN16(caps));
3848 
3849 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
3850 		if (rc != 0) {
3851 			device_printf(sc->dev,
3852 			    "failed to upload config file to card: %d.\n", rc);
3853 			goto done;
3854 		}
3855 	}
3856 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
3857 	if (rc != 0) {
3858 		device_printf(sc->dev, "failed to pre-process config file: %d "
3859 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
3860 		goto done;
3861 	}
3862 
3863 	finicsum = be32toh(caps.finicsum);
3864 	cfcsum = be32toh(caps.cfcsum);	/* actual */
3865 	if (finicsum != cfcsum) {
3866 		device_printf(sc->dev,
3867 		    "WARNING: config file checksum mismatch: %08x %08x\n",
3868 		    finicsum, cfcsum);
3869 	}
3870 	sc->cfcsum = cfcsum;
3871 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
3872 
3873 	/*
3874 	 * Let the firmware know what features will (not) be used so it can tune
3875 	 * things accordingly.
3876 	 */
3877 #define LIMIT_CAPS(x) do { \
3878 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
3879 } while (0)
3880 	LIMIT_CAPS(nbm);
3881 	LIMIT_CAPS(link);
3882 	LIMIT_CAPS(switch);
3883 	LIMIT_CAPS(nic);
3884 	LIMIT_CAPS(toe);
3885 	LIMIT_CAPS(rdma);
3886 	LIMIT_CAPS(crypto);
3887 	LIMIT_CAPS(iscsi);
3888 	LIMIT_CAPS(fcoe);
3889 #undef LIMIT_CAPS
3890 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
3891 		/*
3892 		 * TOE and hashfilters are mutually exclusive.  It is a config
3893 		 * file or firmware bug if both are reported as available.  Try
3894 		 * to cope with the situation in non-debug builds by disabling
3895 		 * TOE.
3896 		 */
3897 		MPASS(caps.toecaps == 0);
3898 
3899 		caps.toecaps = 0;
3900 		caps.rdmacaps = 0;
3901 		caps.iscsicaps = 0;
3902 	}
3903 
3904 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
3905 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
3906 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
3907 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
3908 	if (rc != 0) {
3909 		device_printf(sc->dev,
3910 		    "failed to process config file: %d.\n", rc);
3911 		goto done;
3912 	}
3913 
3914 	t4_tweak_chip_settings(sc);
3915 	set_params__pre_init(sc);
3916 
3917 	/* get basic stuff going */
3918 	rc = -t4_fw_initialize(sc, sc->mbox);
3919 	if (rc != 0) {
3920 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
3921 		goto done;
3922 	}
3923 done:
3924 	return (rc);
3925 }
3926 
3927 /*
3928  * Partition chip resources for use between various PFs, VFs, etc.
3929  */
3930 static int
partition_resources(struct adapter * sc)3931 partition_resources(struct adapter *sc)
3932 {
3933 	char cfg_file[sizeof(t4_cfg_file)];
3934 	struct caps_allowed caps_allowed;
3935 	int rc;
3936 	bool fallback;
3937 
3938 	/* Only the master driver gets to configure the chip resources. */
3939 	MPASS(sc->flags & MASTER_PF);
3940 
3941 #define COPY_CAPS(x) do { \
3942 	caps_allowed.x##caps = t4_##x##caps_allowed; \
3943 } while (0)
3944 	bzero(&caps_allowed, sizeof(caps_allowed));
3945 	COPY_CAPS(nbm);
3946 	COPY_CAPS(link);
3947 	COPY_CAPS(switch);
3948 	COPY_CAPS(nic);
3949 	COPY_CAPS(toe);
3950 	COPY_CAPS(rdma);
3951 	COPY_CAPS(crypto);
3952 	COPY_CAPS(iscsi);
3953 	COPY_CAPS(fcoe);
3954 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
3955 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
3956 retry:
3957 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
3958 	if (rc != 0 && fallback) {
3959 		device_printf(sc->dev,
3960 		    "failed (%d) to configure card with \"%s\" profile, "
3961 		    "will fall back to a basic configuration and retry.\n",
3962 		    rc, cfg_file);
3963 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
3964 		bzero(&caps_allowed, sizeof(caps_allowed));
3965 		COPY_CAPS(switch);
3966 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
3967 		fallback = false;
3968 		goto retry;
3969 	}
3970 #undef COPY_CAPS
3971 	return (rc);
3972 }
3973 
3974 /*
3975  * Retrieve parameters that are needed (or nice to have) very early.
3976  */
3977 static int
get_params__pre_init(struct adapter * sc)3978 get_params__pre_init(struct adapter *sc)
3979 {
3980 	int rc;
3981 	uint32_t param[2], val[2];
3982 
3983 	t4_get_version_info(sc);
3984 
3985 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
3986 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
3987 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
3988 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
3989 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
3990 
3991 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
3992 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
3993 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
3994 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
3995 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
3996 
3997 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
3998 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
3999 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
4000 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
4001 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
4002 
4003 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
4004 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
4005 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
4006 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
4007 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
4008 
4009 	param[0] = FW_PARAM_DEV(PORTVEC);
4010 	param[1] = FW_PARAM_DEV(CCLK);
4011 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
4012 	if (rc != 0) {
4013 		device_printf(sc->dev,
4014 		    "failed to query parameters (pre_init): %d.\n", rc);
4015 		return (rc);
4016 	}
4017 
4018 	sc->params.portvec = val[0];
4019 	sc->params.nports = bitcount32(val[0]);
4020 	sc->params.vpd.cclk = val[1];
4021 
4022 	/* Read device log parameters. */
4023 	rc = -t4_init_devlog_params(sc, 1);
4024 	if (rc == 0)
4025 		fixup_devlog_params(sc);
4026 	else {
4027 		device_printf(sc->dev,
4028 		    "failed to get devlog parameters: %d.\n", rc);
4029 		rc = 0;	/* devlog isn't critical for device operation */
4030 	}
4031 
4032 	return (rc);
4033 }
4034 
4035 /*
4036  * Any params that need to be set before FW_INITIALIZE.
4037  */
4038 static int
set_params__pre_init(struct adapter * sc)4039 set_params__pre_init(struct adapter *sc)
4040 {
4041 	int rc = 0;
4042 	uint32_t param, val;
4043 
4044 	if (chip_id(sc) >= CHELSIO_T6) {
4045 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
4046 		val = 1;
4047 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
4048 		/* firmwares < 1.20.1.0 do not have this param. */
4049 		if (rc == FW_EINVAL && sc->params.fw_vers <
4050 		    (V_FW_HDR_FW_VER_MAJOR(1) | V_FW_HDR_FW_VER_MINOR(20) |
4051 		    V_FW_HDR_FW_VER_MICRO(1) | V_FW_HDR_FW_VER_BUILD(0))) {
4052 			rc = 0;
4053 		}
4054 		if (rc != 0) {
4055 			device_printf(sc->dev,
4056 			    "failed to enable high priority filters :%d.\n",
4057 			    rc);
4058 		}
4059 	}
4060 
4061 	/* Enable opaque VIIDs with firmwares that support it. */
4062 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
4063 	val = 1;
4064 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
4065 	if (rc == 0 && val == 1)
4066 		sc->params.viid_smt_extn_support = true;
4067 	else
4068 		sc->params.viid_smt_extn_support = false;
4069 
4070 	return (rc);
4071 }
4072 
4073 /*
4074  * Retrieve various parameters that are of interest to the driver.  The device
4075  * has been initialized by the firmware at this point.
4076  */
4077 static int
get_params__post_init(struct adapter * sc)4078 get_params__post_init(struct adapter *sc)
4079 {
4080 	int rc;
4081 	uint32_t param[7], val[7];
4082 	struct fw_caps_config_cmd caps;
4083 
4084 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
4085 	param[1] = FW_PARAM_PFVF(EQ_START);
4086 	param[2] = FW_PARAM_PFVF(FILTER_START);
4087 	param[3] = FW_PARAM_PFVF(FILTER_END);
4088 	param[4] = FW_PARAM_PFVF(L2T_START);
4089 	param[5] = FW_PARAM_PFVF(L2T_END);
4090 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
4091 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
4092 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
4093 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
4094 	if (rc != 0) {
4095 		device_printf(sc->dev,
4096 		    "failed to query parameters (post_init): %d.\n", rc);
4097 		return (rc);
4098 	}
4099 
4100 	sc->sge.iq_start = val[0];
4101 	sc->sge.eq_start = val[1];
4102 	if ((int)val[3] > (int)val[2]) {
4103 		sc->tids.ftid_base = val[2];
4104 		sc->tids.ftid_end = val[3];
4105 		sc->tids.nftids = val[3] - val[2] + 1;
4106 	}
4107 	sc->vres.l2t.start = val[4];
4108 	sc->vres.l2t.size = val[5] - val[4] + 1;
4109 	KASSERT(sc->vres.l2t.size <= L2T_SIZE,
4110 	    ("%s: L2 table size (%u) larger than expected (%u)",
4111 	    __func__, sc->vres.l2t.size, L2T_SIZE));
4112 	sc->params.core_vdd = val[6];
4113 
4114 	if (chip_id(sc) >= CHELSIO_T6) {
4115 
4116 		sc->tids.tid_base = t4_read_reg(sc,
4117 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
4118 
4119 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
4120 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
4121 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
4122 		if (rc != 0) {
4123 			device_printf(sc->dev,
4124 			   "failed to query hpfilter parameters: %d.\n", rc);
4125 			return (rc);
4126 		}
4127 		if ((int)val[1] > (int)val[0]) {
4128 			sc->tids.hpftid_base = val[0];
4129 			sc->tids.hpftid_end = val[1];
4130 			sc->tids.nhpftids = val[1] - val[0] + 1;
4131 
4132 			/*
4133 			 * These should go off if the layout changes and the
4134 			 * driver needs to catch up.
4135 			 */
4136 			MPASS(sc->tids.hpftid_base == 0);
4137 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
4138 		}
4139 	}
4140 
4141 	/*
4142 	 * MPSBGMAP is queried separately because only recent firmwares support
4143 	 * it as a parameter and we don't want the compound query above to fail
4144 	 * on older firmwares.
4145 	 */
4146 	param[0] = FW_PARAM_DEV(MPSBGMAP);
4147 	val[0] = 0;
4148 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
4149 	if (rc == 0)
4150 		sc->params.mps_bg_map = val[0];
4151 	else
4152 		sc->params.mps_bg_map = 0;
4153 
4154 	/*
4155 	 * Determine whether the firmware supports the filter2 work request.
4156 	 * This is queried separately for the same reason as MPSBGMAP above.
4157 	 */
4158 	param[0] = FW_PARAM_DEV(FILTER2_WR);
4159 	val[0] = 0;
4160 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
4161 	if (rc == 0)
4162 		sc->params.filter2_wr_support = val[0] != 0;
4163 	else
4164 		sc->params.filter2_wr_support = 0;
4165 
4166 	/*
4167 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
4168 	 * This is queried separately for the same reason as other params above.
4169 	 */
4170 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
4171 	val[0] = 0;
4172 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
4173 	if (rc == 0)
4174 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
4175 	else
4176 		sc->params.ulptx_memwrite_dsgl = false;
4177 
4178 	/* get capabilites */
4179 	bzero(&caps, sizeof(caps));
4180 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
4181 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
4182 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
4183 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
4184 	if (rc != 0) {
4185 		device_printf(sc->dev,
4186 		    "failed to get card capabilities: %d.\n", rc);
4187 		return (rc);
4188 	}
4189 
4190 #define READ_CAPS(x) do { \
4191 	sc->x = htobe16(caps.x); \
4192 } while (0)
4193 	READ_CAPS(nbmcaps);
4194 	READ_CAPS(linkcaps);
4195 	READ_CAPS(switchcaps);
4196 	READ_CAPS(niccaps);
4197 	READ_CAPS(toecaps);
4198 	READ_CAPS(rdmacaps);
4199 	READ_CAPS(cryptocaps);
4200 	READ_CAPS(iscsicaps);
4201 	READ_CAPS(fcoecaps);
4202 
4203 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
4204 		MPASS(chip_id(sc) > CHELSIO_T4);
4205 		MPASS(sc->toecaps == 0);
4206 		sc->toecaps = 0;
4207 
4208 		param[0] = FW_PARAM_DEV(NTID);
4209 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
4210 		if (rc != 0) {
4211 			device_printf(sc->dev,
4212 			    "failed to query HASHFILTER parameters: %d.\n", rc);
4213 			return (rc);
4214 		}
4215 		sc->tids.ntids = val[0];
4216 		if (sc->params.fw_vers <
4217 		    (V_FW_HDR_FW_VER_MAJOR(1) | V_FW_HDR_FW_VER_MINOR(20) |
4218 		    V_FW_HDR_FW_VER_MICRO(5) | V_FW_HDR_FW_VER_BUILD(0))) {
4219 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
4220 			sc->tids.ntids -= sc->tids.nhpftids;
4221 		}
4222 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
4223 		sc->params.hash_filter = 1;
4224 	}
4225 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
4226 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
4227 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
4228 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
4229 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
4230 		if (rc != 0) {
4231 			device_printf(sc->dev,
4232 			    "failed to query NIC parameters: %d.\n", rc);
4233 			return (rc);
4234 		}
4235 		if ((int)val[1] > (int)val[0]) {
4236 			sc->tids.etid_base = val[0];
4237 			sc->tids.etid_end = val[1];
4238 			sc->tids.netids = val[1] - val[0] + 1;
4239 			sc->params.eo_wr_cred = val[2];
4240 			sc->params.ethoffload = 1;
4241 		}
4242 	}
4243 	if (sc->toecaps) {
4244 		/* query offload-related parameters */
4245 		param[0] = FW_PARAM_DEV(NTID);
4246 		param[1] = FW_PARAM_PFVF(SERVER_START);
4247 		param[2] = FW_PARAM_PFVF(SERVER_END);
4248 		param[3] = FW_PARAM_PFVF(TDDP_START);
4249 		param[4] = FW_PARAM_PFVF(TDDP_END);
4250 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
4251 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
4252 		if (rc != 0) {
4253 			device_printf(sc->dev,
4254 			    "failed to query TOE parameters: %d.\n", rc);
4255 			return (rc);
4256 		}
4257 		sc->tids.ntids = val[0];
4258 		if (sc->params.fw_vers <
4259 		    (V_FW_HDR_FW_VER_MAJOR(1) | V_FW_HDR_FW_VER_MINOR(20) |
4260 		    V_FW_HDR_FW_VER_MICRO(5) | V_FW_HDR_FW_VER_BUILD(0))) {
4261 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
4262 			sc->tids.ntids -= sc->tids.nhpftids;
4263 		}
4264 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
4265 		if ((int)val[2] > (int)val[1]) {
4266 			sc->tids.stid_base = val[1];
4267 			sc->tids.nstids = val[2] - val[1] + 1;
4268 		}
4269 		sc->vres.ddp.start = val[3];
4270 		sc->vres.ddp.size = val[4] - val[3] + 1;
4271 		sc->params.ofldq_wr_cred = val[5];
4272 		sc->params.offload = 1;
4273 	} else {
4274 		/*
4275 		 * The firmware attempts memfree TOE configuration for -SO cards
4276 		 * and will report toecaps=0 if it runs out of resources (this
4277 		 * depends on the config file).  It may not report 0 for other
4278 		 * capabilities dependent on the TOE in this case.  Set them to
4279 		 * 0 here so that the driver doesn't bother tracking resources
4280 		 * that will never be used.
4281 		 */
4282 		sc->iscsicaps = 0;
4283 		sc->rdmacaps = 0;
4284 	}
4285 	if (sc->rdmacaps) {
4286 		param[0] = FW_PARAM_PFVF(STAG_START);
4287 		param[1] = FW_PARAM_PFVF(STAG_END);
4288 		param[2] = FW_PARAM_PFVF(RQ_START);
4289 		param[3] = FW_PARAM_PFVF(RQ_END);
4290 		param[4] = FW_PARAM_PFVF(PBL_START);
4291 		param[5] = FW_PARAM_PFVF(PBL_END);
4292 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
4293 		if (rc != 0) {
4294 			device_printf(sc->dev,
4295 			    "failed to query RDMA parameters(1): %d.\n", rc);
4296 			return (rc);
4297 		}
4298 		sc->vres.stag.start = val[0];
4299 		sc->vres.stag.size = val[1] - val[0] + 1;
4300 		sc->vres.rq.start = val[2];
4301 		sc->vres.rq.size = val[3] - val[2] + 1;
4302 		sc->vres.pbl.start = val[4];
4303 		sc->vres.pbl.size = val[5] - val[4] + 1;
4304 
4305 		param[0] = FW_PARAM_PFVF(SQRQ_START);
4306 		param[1] = FW_PARAM_PFVF(SQRQ_END);
4307 		param[2] = FW_PARAM_PFVF(CQ_START);
4308 		param[3] = FW_PARAM_PFVF(CQ_END);
4309 		param[4] = FW_PARAM_PFVF(OCQ_START);
4310 		param[5] = FW_PARAM_PFVF(OCQ_END);
4311 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
4312 		if (rc != 0) {
4313 			device_printf(sc->dev,
4314 			    "failed to query RDMA parameters(2): %d.\n", rc);
4315 			return (rc);
4316 		}
4317 		sc->vres.qp.start = val[0];
4318 		sc->vres.qp.size = val[1] - val[0] + 1;
4319 		sc->vres.cq.start = val[2];
4320 		sc->vres.cq.size = val[3] - val[2] + 1;
4321 		sc->vres.ocq.start = val[4];
4322 		sc->vres.ocq.size = val[5] - val[4] + 1;
4323 
4324 		param[0] = FW_PARAM_PFVF(SRQ_START);
4325 		param[1] = FW_PARAM_PFVF(SRQ_END);
4326 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
4327 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
4328 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
4329 		if (rc != 0) {
4330 			device_printf(sc->dev,
4331 			    "failed to query RDMA parameters(3): %d.\n", rc);
4332 			return (rc);
4333 		}
4334 		sc->vres.srq.start = val[0];
4335 		sc->vres.srq.size = val[1] - val[0] + 1;
4336 		sc->params.max_ordird_qp = val[2];
4337 		sc->params.max_ird_adapter = val[3];
4338 	}
4339 	if (sc->iscsicaps) {
4340 		param[0] = FW_PARAM_PFVF(ISCSI_START);
4341 		param[1] = FW_PARAM_PFVF(ISCSI_END);
4342 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
4343 		if (rc != 0) {
4344 			device_printf(sc->dev,
4345 			    "failed to query iSCSI parameters: %d.\n", rc);
4346 			return (rc);
4347 		}
4348 		sc->vres.iscsi.start = val[0];
4349 		sc->vres.iscsi.size = val[1] - val[0] + 1;
4350 	}
4351 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
4352 		param[0] = FW_PARAM_PFVF(TLS_START);
4353 		param[1] = FW_PARAM_PFVF(TLS_END);
4354 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
4355 		if (rc != 0) {
4356 			device_printf(sc->dev,
4357 			    "failed to query TLS parameters: %d.\n", rc);
4358 			return (rc);
4359 		}
4360 		sc->vres.key.start = val[0];
4361 		sc->vres.key.size = val[1] - val[0] + 1;
4362 	}
4363 
4364 	t4_init_sge_params(sc);
4365 
4366 	/*
4367 	 * We've got the params we wanted to query via the firmware.  Now grab
4368 	 * some others directly from the chip.
4369 	 */
4370 	rc = t4_read_chip_settings(sc);
4371 
4372 	return (rc);
4373 }
4374 
4375 static int
set_params__post_init(struct adapter * sc)4376 set_params__post_init(struct adapter *sc)
4377 {
4378 	uint32_t param, val;
4379 #ifdef TCP_OFFLOAD
4380 	int i, v, shift;
4381 #endif
4382 
4383 	/* ask for encapsulated CPLs */
4384 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
4385 	val = 1;
4386 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
4387 
4388 	/* Enable 32b port caps if the firmware supports it. */
4389 	param = FW_PARAM_PFVF(PORT_CAPS32);
4390 	val = 1;
4391 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
4392 		sc->params.port_caps32 = 1;
4393 
4394 #ifdef TCP_OFFLOAD
4395 	/*
4396 	 * Override the TOE timers with user provided tunables.  This is not the
4397 	 * recommended way to change the timers (the firmware config file is) so
4398 	 * these tunables are not documented.
4399 	 *
4400 	 * All the timer tunables are in microseconds.
4401 	 */
4402 	if (t4_toe_keepalive_idle != 0) {
4403 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
4404 		v &= M_KEEPALIVEIDLE;
4405 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
4406 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
4407 	}
4408 	if (t4_toe_keepalive_interval != 0) {
4409 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
4410 		v &= M_KEEPALIVEINTVL;
4411 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
4412 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
4413 	}
4414 	if (t4_toe_keepalive_count != 0) {
4415 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
4416 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
4417 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
4418 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
4419 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
4420 	}
4421 	if (t4_toe_rexmt_min != 0) {
4422 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
4423 		v &= M_RXTMIN;
4424 		t4_set_reg_field(sc, A_TP_RXT_MIN,
4425 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
4426 	}
4427 	if (t4_toe_rexmt_max != 0) {
4428 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
4429 		v &= M_RXTMAX;
4430 		t4_set_reg_field(sc, A_TP_RXT_MAX,
4431 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
4432 	}
4433 	if (t4_toe_rexmt_count != 0) {
4434 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
4435 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
4436 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
4437 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
4438 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
4439 	}
4440 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
4441 		if (t4_toe_rexmt_backoff[i] != -1) {
4442 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
4443 			shift = (i & 3) << 3;
4444 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
4445 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
4446 		}
4447 	}
4448 #endif
4449 	return (0);
4450 }
4451 
4452 #undef FW_PARAM_PFVF
4453 #undef FW_PARAM_DEV
4454 
4455 static void
t4_set_desc(struct adapter * sc)4456 t4_set_desc(struct adapter *sc)
4457 {
4458 	char buf[128];
4459 	struct adapter_params *p = &sc->params;
4460 
4461 	snprintf(buf, sizeof(buf), "Chelsio %s", p->vpd.id);
4462 
4463 	device_set_desc_copy(sc->dev, buf);
4464 }
4465 
4466 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)4467 ifmedia_add4(struct ifmedia *ifm, int m)
4468 {
4469 
4470 	ifmedia_add(ifm, m, 0, NULL);
4471 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
4472 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
4473 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
4474 }
4475 
4476 /*
4477  * This is the selected media, which is not quite the same as the active media.
4478  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
4479  * and active are not the same, and "media: Ethernet selected" otherwise.
4480  */
4481 static void
set_current_media(struct port_info * pi)4482 set_current_media(struct port_info *pi)
4483 {
4484 	struct link_config *lc;
4485 	struct ifmedia *ifm;
4486 	int mword;
4487 	u_int speed;
4488 
4489 	PORT_LOCK_ASSERT_OWNED(pi);
4490 
4491 	/* Leave current media alone if it's already set to IFM_NONE. */
4492 	ifm = &pi->media;
4493 	if (ifm->ifm_cur != NULL &&
4494 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
4495 		return;
4496 
4497 	lc = &pi->link_cfg;
4498 	if (lc->requested_aneg != AUTONEG_DISABLE &&
4499 	    lc->supported & FW_PORT_CAP32_ANEG) {
4500 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
4501 		return;
4502 	}
4503 	mword = IFM_ETHER | IFM_FDX;
4504 	if (lc->requested_fc & PAUSE_TX)
4505 		mword |= IFM_ETH_TXPAUSE;
4506 	if (lc->requested_fc & PAUSE_RX)
4507 		mword |= IFM_ETH_RXPAUSE;
4508 	if (lc->requested_speed == 0)
4509 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
4510 	else
4511 		speed = lc->requested_speed;
4512 	mword |= port_mword(pi, speed_to_fwcap(speed));
4513 	ifmedia_set(ifm, mword);
4514 }
4515 
4516 /*
4517  * Returns true if the ifmedia list for the port cannot change.
4518  */
4519 static bool
fixed_ifmedia(struct port_info * pi)4520 fixed_ifmedia(struct port_info *pi)
4521 {
4522 
4523 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
4524 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
4525 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
4526 	    pi->port_type == FW_PORT_TYPE_KX4 ||
4527 	    pi->port_type == FW_PORT_TYPE_KX ||
4528 	    pi->port_type == FW_PORT_TYPE_KR ||
4529 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
4530 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
4531 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
4532 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
4533 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
4534 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
4535 }
4536 
4537 static void
build_medialist(struct port_info * pi)4538 build_medialist(struct port_info *pi)
4539 {
4540 	uint32_t ss, speed;
4541 	int unknown, mword, bit;
4542 	struct link_config *lc;
4543 	struct ifmedia *ifm;
4544 
4545 	PORT_LOCK_ASSERT_OWNED(pi);
4546 
4547 	if (pi->flags & FIXED_IFMEDIA)
4548 		return;
4549 
4550 	/*
4551 	 * Rebuild the ifmedia list.
4552 	 */
4553 	ifm = &pi->media;
4554 	ifmedia_removeall(ifm);
4555 	lc = &pi->link_cfg;
4556 	ss = G_FW_PORT_CAP32_SPEED(lc->supported); /* Supported Speeds */
4557 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
4558 		MPASS(ss != 0);
4559 no_media:
4560 		MPASS(LIST_EMPTY(&ifm->ifm_list));
4561 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
4562 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
4563 		return;
4564 	}
4565 
4566 	unknown = 0;
4567 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
4568 		speed = 1 << bit;
4569 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
4570 		if (ss & speed) {
4571 			mword = port_mword(pi, speed);
4572 			if (mword == IFM_NONE) {
4573 				goto no_media;
4574 			} else if (mword == IFM_UNKNOWN)
4575 				unknown++;
4576 			else
4577 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
4578 		}
4579 	}
4580 	if (unknown > 0) /* Add one unknown for all unknown media types. */
4581 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
4582 	if (lc->supported & FW_PORT_CAP32_ANEG)
4583 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
4584 
4585 	set_current_media(pi);
4586 }
4587 
4588 /*
4589  * Initialize the requested fields in the link config based on driver tunables.
4590  */
4591 static void
init_link_config(struct port_info * pi)4592 init_link_config(struct port_info *pi)
4593 {
4594 	struct link_config *lc = &pi->link_cfg;
4595 
4596 	PORT_LOCK_ASSERT_OWNED(pi);
4597 
4598 	lc->requested_speed = 0;
4599 
4600 	if (t4_autoneg == 0)
4601 		lc->requested_aneg = AUTONEG_DISABLE;
4602 	else if (t4_autoneg == 1)
4603 		lc->requested_aneg = AUTONEG_ENABLE;
4604 	else
4605 		lc->requested_aneg = AUTONEG_AUTO;
4606 
4607 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
4608 	    PAUSE_AUTONEG);
4609 
4610 	if (t4_fec == -1 || t4_fec & FEC_AUTO)
4611 		lc->requested_fec = FEC_AUTO;
4612 	else {
4613 		lc->requested_fec = FEC_NONE;
4614 		if (t4_fec & FEC_RS)
4615 			lc->requested_fec |= FEC_RS;
4616 		if (t4_fec & FEC_BASER_RS)
4617 			lc->requested_fec |= FEC_BASER_RS;
4618 	}
4619 }
4620 
4621 /*
4622  * Makes sure that all requested settings comply with what's supported by the
4623  * port.  Returns the number of settings that were invalid and had to be fixed.
4624  */
4625 static int
fixup_link_config(struct port_info * pi)4626 fixup_link_config(struct port_info *pi)
4627 {
4628 	int n = 0;
4629 	struct link_config *lc = &pi->link_cfg;
4630 	uint32_t fwspeed;
4631 
4632 	PORT_LOCK_ASSERT_OWNED(pi);
4633 
4634 	/* Speed (when not autonegotiating) */
4635 	if (lc->requested_speed != 0) {
4636 		fwspeed = speed_to_fwcap(lc->requested_speed);
4637 		if ((fwspeed & lc->supported) == 0) {
4638 			n++;
4639 			lc->requested_speed = 0;
4640 		}
4641 	}
4642 
4643 	/* Link autonegotiation */
4644 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
4645 	    lc->requested_aneg == AUTONEG_DISABLE ||
4646 	    lc->requested_aneg == AUTONEG_AUTO);
4647 	if (lc->requested_aneg == AUTONEG_ENABLE &&
4648 	    !(lc->supported & FW_PORT_CAP32_ANEG)) {
4649 		n++;
4650 		lc->requested_aneg = AUTONEG_AUTO;
4651 	}
4652 
4653 	/* Flow control */
4654 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
4655 	if (lc->requested_fc & PAUSE_TX &&
4656 	    !(lc->supported & FW_PORT_CAP32_FC_TX)) {
4657 		n++;
4658 		lc->requested_fc &= ~PAUSE_TX;
4659 	}
4660 	if (lc->requested_fc & PAUSE_RX &&
4661 	    !(lc->supported & FW_PORT_CAP32_FC_RX)) {
4662 		n++;
4663 		lc->requested_fc &= ~PAUSE_RX;
4664 	}
4665 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
4666 	    !(lc->supported & FW_PORT_CAP32_FORCE_PAUSE)) {
4667 		n++;
4668 		lc->requested_fc |= PAUSE_AUTONEG;
4669 	}
4670 
4671 	/* FEC */
4672 	if ((lc->requested_fec & FEC_RS &&
4673 	    !(lc->supported & FW_PORT_CAP32_FEC_RS)) ||
4674 	    (lc->requested_fec & FEC_BASER_RS &&
4675 	    !(lc->supported & FW_PORT_CAP32_FEC_BASER_RS))) {
4676 		n++;
4677 		lc->requested_fec = FEC_AUTO;
4678 	}
4679 
4680 	return (n);
4681 }
4682 
4683 /*
4684  * Apply the requested L1 settings, which are expected to be valid, to the
4685  * hardware.
4686  */
4687 static int
apply_link_config(struct port_info * pi)4688 apply_link_config(struct port_info *pi)
4689 {
4690 	struct adapter *sc = pi->adapter;
4691 	struct link_config *lc = &pi->link_cfg;
4692 	int rc;
4693 
4694 #ifdef INVARIANTS
4695 	ASSERT_SYNCHRONIZED_OP(sc);
4696 	PORT_LOCK_ASSERT_OWNED(pi);
4697 
4698 	if (lc->requested_aneg == AUTONEG_ENABLE)
4699 		MPASS(lc->supported & FW_PORT_CAP32_ANEG);
4700 	if (!(lc->requested_fc & PAUSE_AUTONEG))
4701 		MPASS(lc->supported & FW_PORT_CAP32_FORCE_PAUSE);
4702 	if (lc->requested_fc & PAUSE_TX)
4703 		MPASS(lc->supported & FW_PORT_CAP32_FC_TX);
4704 	if (lc->requested_fc & PAUSE_RX)
4705 		MPASS(lc->supported & FW_PORT_CAP32_FC_RX);
4706 	if (lc->requested_fec & FEC_RS)
4707 		MPASS(lc->supported & FW_PORT_CAP32_FEC_RS);
4708 	if (lc->requested_fec & FEC_BASER_RS)
4709 		MPASS(lc->supported & FW_PORT_CAP32_FEC_BASER_RS);
4710 #endif
4711 	rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
4712 	if (rc != 0) {
4713 		/* Don't complain if the VF driver gets back an EPERM. */
4714 		if (!(sc->flags & IS_VF) || rc != FW_EPERM)
4715 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
4716 	} else {
4717 		/*
4718 		 * An L1_CFG will almost always result in a link-change event if
4719 		 * the link is up, and the driver will refresh the actual
4720 		 * fec/fc/etc. when the notification is processed.  If the link
4721 		 * is down then the actual settings are meaningless.
4722 		 *
4723 		 * This takes care of the case where a change in the L1 settings
4724 		 * may not result in a notification.
4725 		 */
4726 		if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
4727 			lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
4728 	}
4729 	return (rc);
4730 }
4731 
4732 #define FW_MAC_EXACT_CHUNK	7
4733 
4734 /*
4735  * Program the port's XGMAC based on parameters in ifnet.  The caller also
4736  * indicates which parameters should be programmed (the rest are left alone).
4737  */
4738 int
update_mac_settings(struct ifnet * ifp,int flags)4739 update_mac_settings(struct ifnet *ifp, int flags)
4740 {
4741 	int rc = 0;
4742 	struct vi_info *vi = ifp->if_softc;
4743 	struct port_info *pi = vi->pi;
4744 	struct adapter *sc = pi->adapter;
4745 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
4746 
4747 	ASSERT_SYNCHRONIZED_OP(sc);
4748 	KASSERT(flags, ("%s: not told what to update.", __func__));
4749 
4750 	if (flags & XGMAC_MTU)
4751 		mtu = ifp->if_mtu;
4752 
4753 	if (flags & XGMAC_PROMISC)
4754 		promisc = ifp->if_flags & IFF_PROMISC ? 1 : 0;
4755 
4756 	if (flags & XGMAC_ALLMULTI)
4757 		allmulti = ifp->if_flags & IFF_ALLMULTI ? 1 : 0;
4758 
4759 	if (flags & XGMAC_VLANEX)
4760 		vlanex = ifp->if_capenable & IFCAP_VLAN_HWTAGGING ? 1 : 0;
4761 
4762 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
4763 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
4764 		    allmulti, 1, vlanex, false);
4765 		if (rc) {
4766 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
4767 			    rc);
4768 			return (rc);
4769 		}
4770 	}
4771 
4772 	if (flags & XGMAC_UCADDR) {
4773 		uint8_t ucaddr[ETHER_ADDR_LEN];
4774 
4775 		bcopy(IF_LLADDR(ifp), ucaddr, sizeof(ucaddr));
4776 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
4777 		    ucaddr, true, &vi->smt_idx);
4778 		if (rc < 0) {
4779 			rc = -rc;
4780 			if_printf(ifp, "change_mac failed: %d\n", rc);
4781 			return (rc);
4782 		} else {
4783 			vi->xact_addr_filt = rc;
4784 			rc = 0;
4785 		}
4786 	}
4787 
4788 	if (flags & XGMAC_MCADDRS) {
4789 		const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
4790 		int del = 1;
4791 		uint64_t hash = 0;
4792 		struct ifmultiaddr *ifma;
4793 		int i = 0, j;
4794 
4795 		if_maddr_rlock(ifp);
4796 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4797 			if (ifma->ifma_addr->sa_family != AF_LINK)
4798 				continue;
4799 			mcaddr[i] =
4800 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
4801 			MPASS(ETHER_IS_MULTICAST(mcaddr[i]));
4802 			i++;
4803 
4804 			if (i == FW_MAC_EXACT_CHUNK) {
4805 				rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
4806 				    del, i, mcaddr, NULL, &hash, 0);
4807 				if (rc < 0) {
4808 					rc = -rc;
4809 					for (j = 0; j < i; j++) {
4810 						if_printf(ifp,
4811 						    "failed to add mc address"
4812 						    " %02x:%02x:%02x:"
4813 						    "%02x:%02x:%02x rc=%d\n",
4814 						    mcaddr[j][0], mcaddr[j][1],
4815 						    mcaddr[j][2], mcaddr[j][3],
4816 						    mcaddr[j][4], mcaddr[j][5],
4817 						    rc);
4818 					}
4819 					goto mcfail;
4820 				}
4821 				del = 0;
4822 				i = 0;
4823 			}
4824 		}
4825 		if (i > 0) {
4826 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, del, i,
4827 			    mcaddr, NULL, &hash, 0);
4828 			if (rc < 0) {
4829 				rc = -rc;
4830 				for (j = 0; j < i; j++) {
4831 					if_printf(ifp,
4832 					    "failed to add mc address"
4833 					    " %02x:%02x:%02x:"
4834 					    "%02x:%02x:%02x rc=%d\n",
4835 					    mcaddr[j][0], mcaddr[j][1],
4836 					    mcaddr[j][2], mcaddr[j][3],
4837 					    mcaddr[j][4], mcaddr[j][5],
4838 					    rc);
4839 				}
4840 				goto mcfail;
4841 			}
4842 		}
4843 
4844 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, hash, 0);
4845 		if (rc != 0)
4846 			if_printf(ifp, "failed to set mc address hash: %d", rc);
4847 mcfail:
4848 		if_maddr_runlock(ifp);
4849 	}
4850 
4851 	return (rc);
4852 }
4853 
4854 /*
4855  * {begin|end}_synchronized_op must be called from the same thread.
4856  */
4857 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)4858 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
4859     char *wmesg)
4860 {
4861 	int rc, pri;
4862 
4863 #ifdef WITNESS
4864 	/* the caller thinks it's ok to sleep, but is it really? */
4865 	if (flags & SLEEP_OK)
4866 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
4867 		    "begin_synchronized_op");
4868 #endif
4869 
4870 	if (INTR_OK)
4871 		pri = PCATCH;
4872 	else
4873 		pri = 0;
4874 
4875 	ADAPTER_LOCK(sc);
4876 	for (;;) {
4877 
4878 		if (vi && IS_DOOMED(vi)) {
4879 			rc = ENXIO;
4880 			goto done;
4881 		}
4882 
4883 		if (!IS_BUSY(sc)) {
4884 			rc = 0;
4885 			break;
4886 		}
4887 
4888 		if (!(flags & SLEEP_OK)) {
4889 			rc = EBUSY;
4890 			goto done;
4891 		}
4892 
4893 		if (mtx_sleep(&sc->flags, &sc->sc_lock, pri, wmesg, 0)) {
4894 			rc = EINTR;
4895 			goto done;
4896 		}
4897 	}
4898 
4899 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
4900 	SET_BUSY(sc);
4901 #ifdef INVARIANTS
4902 	sc->last_op = wmesg;
4903 	sc->last_op_thr = curthread;
4904 	sc->last_op_flags = flags;
4905 #endif
4906 
4907 done:
4908 	if (!(flags & HOLD_LOCK) || rc)
4909 		ADAPTER_UNLOCK(sc);
4910 
4911 	return (rc);
4912 }
4913 
4914 /*
4915  * Tell if_ioctl and if_init that the VI is going away.  This is
4916  * special variant of begin_synchronized_op and must be paired with a
4917  * call to end_synchronized_op.
4918  */
4919 void
doom_vi(struct adapter * sc,struct vi_info * vi)4920 doom_vi(struct adapter *sc, struct vi_info *vi)
4921 {
4922 
4923 	ADAPTER_LOCK(sc);
4924 	SET_DOOMED(vi);
4925 	wakeup(&sc->flags);
4926 	while (IS_BUSY(sc))
4927 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
4928 	SET_BUSY(sc);
4929 #ifdef INVARIANTS
4930 	sc->last_op = "t4detach";
4931 	sc->last_op_thr = curthread;
4932 	sc->last_op_flags = 0;
4933 #endif
4934 	ADAPTER_UNLOCK(sc);
4935 }
4936 
4937 /*
4938  * {begin|end}_synchronized_op must be called from the same thread.
4939  */
4940 void
end_synchronized_op(struct adapter * sc,int flags)4941 end_synchronized_op(struct adapter *sc, int flags)
4942 {
4943 
4944 	if (flags & LOCK_HELD)
4945 		ADAPTER_LOCK_ASSERT_OWNED(sc);
4946 	else
4947 		ADAPTER_LOCK(sc);
4948 
4949 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
4950 	CLR_BUSY(sc);
4951 	wakeup(&sc->flags);
4952 	ADAPTER_UNLOCK(sc);
4953 }
4954 
4955 static int
cxgbe_init_synchronized(struct vi_info * vi)4956 cxgbe_init_synchronized(struct vi_info *vi)
4957 {
4958 	struct port_info *pi = vi->pi;
4959 	struct adapter *sc = pi->adapter;
4960 	struct ifnet *ifp = vi->ifp;
4961 	int rc = 0, i;
4962 	struct sge_txq *txq;
4963 
4964 	ASSERT_SYNCHRONIZED_OP(sc);
4965 
4966 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4967 		return (0);	/* already running */
4968 
4969 	if (!(sc->flags & FULL_INIT_DONE) &&
4970 	    ((rc = adapter_full_init(sc)) != 0))
4971 		return (rc);	/* error message displayed already */
4972 
4973 	if (!(vi->flags & VI_INIT_DONE) &&
4974 	    ((rc = vi_full_init(vi)) != 0))
4975 		return (rc); /* error message displayed already */
4976 
4977 	rc = update_mac_settings(ifp, XGMAC_ALL);
4978 	if (rc)
4979 		goto done;	/* error message displayed already */
4980 
4981 	PORT_LOCK(pi);
4982 	if (pi->up_vis == 0) {
4983 		t4_update_port_info(pi);
4984 		fixup_link_config(pi);
4985 		build_medialist(pi);
4986 		apply_link_config(pi);
4987 	}
4988 
4989 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
4990 	if (rc != 0) {
4991 		if_printf(ifp, "enable_vi failed: %d\n", rc);
4992 		PORT_UNLOCK(pi);
4993 		goto done;
4994 	}
4995 
4996 	/*
4997 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
4998 	 * if this changes.
4999 	 */
5000 
5001 	for_each_txq(vi, i, txq) {
5002 		TXQ_LOCK(txq);
5003 		txq->eq.flags |= EQ_ENABLED;
5004 		TXQ_UNLOCK(txq);
5005 	}
5006 
5007 	/*
5008 	 * The first iq of the first port to come up is used for tracing.
5009 	 */
5010 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
5011 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
5012 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
5013 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
5014 		    V_QUEUENUMBER(sc->traceq));
5015 		pi->flags |= HAS_TRACEQ;
5016 	}
5017 
5018 	/* all ok */
5019 	pi->up_vis++;
5020 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
5021 
5022 	if (pi->nvi > 1 || sc->flags & IS_VF)
5023 		callout_reset(&vi->tick, hz, vi_tick, vi);
5024 	else
5025 		callout_reset(&pi->tick, hz, cxgbe_tick, pi);
5026 	if (pi->link_cfg.link_ok)
5027 		t4_os_link_changed(pi);
5028 	PORT_UNLOCK(pi);
5029 done:
5030 	if (rc != 0)
5031 		cxgbe_uninit_synchronized(vi);
5032 
5033 	return (rc);
5034 }
5035 
5036 /*
5037  * Idempotent.
5038  */
5039 static int
cxgbe_uninit_synchronized(struct vi_info * vi)5040 cxgbe_uninit_synchronized(struct vi_info *vi)
5041 {
5042 	struct port_info *pi = vi->pi;
5043 	struct adapter *sc = pi->adapter;
5044 	struct ifnet *ifp = vi->ifp;
5045 	int rc, i;
5046 	struct sge_txq *txq;
5047 
5048 	ASSERT_SYNCHRONIZED_OP(sc);
5049 
5050 	if (!(vi->flags & VI_INIT_DONE)) {
5051 		if (__predict_false(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
5052 			KASSERT(0, ("uninited VI is running"));
5053 			if_printf(ifp, "uninited VI with running ifnet.  "
5054 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
5055 			    "if_drv_flags 0x%08x\n", vi->flags, ifp->if_flags,
5056 			    ifp->if_drv_flags);
5057 		}
5058 		return (0);
5059 	}
5060 
5061 	/*
5062 	 * Disable the VI so that all its data in either direction is discarded
5063 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
5064 	 * tick) intact as the TP can deliver negative advice or data that it's
5065 	 * holding in its RAM (for an offloaded connection) even after the VI is
5066 	 * disabled.
5067 	 */
5068 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
5069 	if (rc) {
5070 		if_printf(ifp, "disable_vi failed: %d\n", rc);
5071 		return (rc);
5072 	}
5073 
5074 	for_each_txq(vi, i, txq) {
5075 		TXQ_LOCK(txq);
5076 		txq->eq.flags &= ~EQ_ENABLED;
5077 		TXQ_UNLOCK(txq);
5078 	}
5079 
5080 	PORT_LOCK(pi);
5081 	if (pi->nvi > 1 || sc->flags & IS_VF)
5082 		callout_stop(&vi->tick);
5083 	else
5084 		callout_stop(&pi->tick);
5085 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
5086 		PORT_UNLOCK(pi);
5087 		return (0);
5088 	}
5089 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5090 	pi->up_vis--;
5091 	if (pi->up_vis > 0) {
5092 		PORT_UNLOCK(pi);
5093 		return (0);
5094 	}
5095 
5096 	pi->link_cfg.link_ok = false;
5097 	pi->link_cfg.speed = 0;
5098 	pi->link_cfg.link_down_rc = 255;
5099 	t4_os_link_changed(pi);
5100 	PORT_UNLOCK(pi);
5101 
5102 	return (0);
5103 }
5104 
5105 /*
5106  * It is ok for this function to fail midway and return right away.  t4_detach
5107  * will walk the entire sc->irq list and clean up whatever is valid.
5108  */
5109 int
t4_setup_intr_handlers(struct adapter * sc)5110 t4_setup_intr_handlers(struct adapter *sc)
5111 {
5112 	int rc, rid, p, q, v;
5113 	char s[8];
5114 	struct irq *irq;
5115 	struct port_info *pi;
5116 	struct vi_info *vi;
5117 	struct sge *sge = &sc->sge;
5118 	struct sge_rxq *rxq;
5119 #ifdef TCP_OFFLOAD
5120 	struct sge_ofld_rxq *ofld_rxq;
5121 #endif
5122 #ifdef DEV_NETMAP
5123 	struct sge_nm_rxq *nm_rxq;
5124 #endif
5125 #ifdef RSS
5126 	int nbuckets = rss_getnumbuckets();
5127 #endif
5128 
5129 	/*
5130 	 * Setup interrupts.
5131 	 */
5132 	irq = &sc->irq[0];
5133 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
5134 	if (forwarding_intr_to_fwq(sc))
5135 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
5136 
5137 	/* Multiple interrupts. */
5138 	if (sc->flags & IS_VF)
5139 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
5140 		    ("%s: too few intr.", __func__));
5141 	else
5142 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
5143 		    ("%s: too few intr.", __func__));
5144 
5145 	/* The first one is always error intr on PFs */
5146 	if (!(sc->flags & IS_VF)) {
5147 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
5148 		if (rc != 0)
5149 			return (rc);
5150 		irq++;
5151 		rid++;
5152 	}
5153 
5154 	/* The second one is always the firmware event queue (first on VFs) */
5155 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
5156 	if (rc != 0)
5157 		return (rc);
5158 	irq++;
5159 	rid++;
5160 
5161 	for_each_port(sc, p) {
5162 		pi = sc->port[p];
5163 		for_each_vi(pi, v, vi) {
5164 			vi->first_intr = rid - 1;
5165 
5166 			if (vi->nnmrxq > 0) {
5167 				int n = max(vi->nrxq, vi->nnmrxq);
5168 
5169 				rxq = &sge->rxq[vi->first_rxq];
5170 #ifdef DEV_NETMAP
5171 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
5172 #endif
5173 				for (q = 0; q < n; q++) {
5174 					snprintf(s, sizeof(s), "%x%c%x", p,
5175 					    'a' + v, q);
5176 					if (q < vi->nrxq)
5177 						irq->rxq = rxq++;
5178 #ifdef DEV_NETMAP
5179 					if (q < vi->nnmrxq)
5180 						irq->nm_rxq = nm_rxq++;
5181 
5182 					if (irq->nm_rxq != NULL &&
5183 					    irq->rxq == NULL) {
5184 						/* Netmap rx only */
5185 						rc = t4_alloc_irq(sc, irq, rid,
5186 						    t4_nm_intr, irq->nm_rxq, s);
5187 					}
5188 					if (irq->nm_rxq != NULL &&
5189 					    irq->rxq != NULL) {
5190 						/* NIC and Netmap rx */
5191 						rc = t4_alloc_irq(sc, irq, rid,
5192 						    t4_vi_intr, irq, s);
5193 					}
5194 #endif
5195 					if (irq->rxq != NULL &&
5196 					    irq->nm_rxq == NULL) {
5197 						/* NIC rx only */
5198 						rc = t4_alloc_irq(sc, irq, rid,
5199 						    t4_intr, irq->rxq, s);
5200 					}
5201 					if (rc != 0)
5202 						return (rc);
5203 #ifdef RSS
5204 					if (q < vi->nrxq) {
5205 						bus_bind_intr(sc->dev, irq->res,
5206 						    rss_getcpu(q % nbuckets));
5207 					}
5208 #endif
5209 					irq++;
5210 					rid++;
5211 					vi->nintr++;
5212 				}
5213 			} else {
5214 				for_each_rxq(vi, q, rxq) {
5215 					snprintf(s, sizeof(s), "%x%c%x", p,
5216 					    'a' + v, q);
5217 					rc = t4_alloc_irq(sc, irq, rid,
5218 					    t4_intr, rxq, s);
5219 					if (rc != 0)
5220 						return (rc);
5221 #ifdef RSS
5222 					bus_bind_intr(sc->dev, irq->res,
5223 					    rss_getcpu(q % nbuckets));
5224 #endif
5225 					irq++;
5226 					rid++;
5227 					vi->nintr++;
5228 				}
5229 			}
5230 #ifdef TCP_OFFLOAD
5231 			for_each_ofld_rxq(vi, q, ofld_rxq) {
5232 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
5233 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
5234 				    ofld_rxq, s);
5235 				if (rc != 0)
5236 					return (rc);
5237 				irq++;
5238 				rid++;
5239 				vi->nintr++;
5240 			}
5241 #endif
5242 		}
5243 	}
5244 	MPASS(irq == &sc->irq[sc->intr_count]);
5245 
5246 	return (0);
5247 }
5248 
5249 int
adapter_full_init(struct adapter * sc)5250 adapter_full_init(struct adapter *sc)
5251 {
5252 	int rc, i;
5253 #ifdef RSS
5254 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
5255 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
5256 #endif
5257 
5258 	ASSERT_SYNCHRONIZED_OP(sc);
5259 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
5260 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
5261 	    ("%s: FULL_INIT_DONE already", __func__));
5262 
5263 	/*
5264 	 * queues that belong to the adapter (not any particular port).
5265 	 */
5266 	rc = t4_setup_adapter_queues(sc);
5267 	if (rc != 0)
5268 		goto done;
5269 
5270 	for (i = 0; i < nitems(sc->tq); i++) {
5271 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
5272 		    taskqueue_thread_enqueue, &sc->tq[i]);
5273 		if (sc->tq[i] == NULL) {
5274 			device_printf(sc->dev,
5275 			    "failed to allocate task queue %d\n", i);
5276 			rc = ENOMEM;
5277 			goto done;
5278 		}
5279 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
5280 		    device_get_nameunit(sc->dev), i);
5281 	}
5282 #ifdef RSS
5283 	MPASS(RSS_KEYSIZE == 40);
5284 	rss_getkey((void *)&raw_rss_key[0]);
5285 	for (i = 0; i < nitems(rss_key); i++) {
5286 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
5287 	}
5288 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
5289 #endif
5290 
5291 	if (!(sc->flags & IS_VF))
5292 		t4_intr_enable(sc);
5293 	sc->flags |= FULL_INIT_DONE;
5294 done:
5295 	if (rc != 0)
5296 		adapter_full_uninit(sc);
5297 
5298 	return (rc);
5299 }
5300 
5301 int
adapter_full_uninit(struct adapter * sc)5302 adapter_full_uninit(struct adapter *sc)
5303 {
5304 	int i;
5305 
5306 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
5307 
5308 	t4_teardown_adapter_queues(sc);
5309 
5310 	for (i = 0; i < nitems(sc->tq) && sc->tq[i]; i++) {
5311 		taskqueue_free(sc->tq[i]);
5312 		sc->tq[i] = NULL;
5313 	}
5314 
5315 	sc->flags &= ~FULL_INIT_DONE;
5316 
5317 	return (0);
5318 }
5319 
5320 #ifdef RSS
5321 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
5322     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
5323     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
5324     RSS_HASHTYPE_RSS_UDP_IPV6)
5325 
5326 /* Translates kernel hash types to hardware. */
5327 static int
hashconfig_to_hashen(int hashconfig)5328 hashconfig_to_hashen(int hashconfig)
5329 {
5330 	int hashen = 0;
5331 
5332 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
5333 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
5334 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
5335 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
5336 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
5337 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
5338 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
5339 	}
5340 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
5341 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
5342 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
5343 	}
5344 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
5345 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
5346 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
5347 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
5348 
5349 	return (hashen);
5350 }
5351 
5352 /* Translates hardware hash types to kernel. */
5353 static int
hashen_to_hashconfig(int hashen)5354 hashen_to_hashconfig(int hashen)
5355 {
5356 	int hashconfig = 0;
5357 
5358 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
5359 		/*
5360 		 * If UDP hashing was enabled it must have been enabled for
5361 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
5362 		 * enabling any 4-tuple hash is nonsense configuration.
5363 		 */
5364 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
5365 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
5366 
5367 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
5368 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
5369 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
5370 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
5371 	}
5372 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
5373 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
5374 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
5375 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
5376 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
5377 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
5378 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
5379 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
5380 
5381 	return (hashconfig);
5382 }
5383 #endif
5384 
5385 int
vi_full_init(struct vi_info * vi)5386 vi_full_init(struct vi_info *vi)
5387 {
5388 	struct adapter *sc = vi->pi->adapter;
5389 	struct ifnet *ifp = vi->ifp;
5390 	uint16_t *rss;
5391 	struct sge_rxq *rxq;
5392 	int rc, i, j, hashen;
5393 #ifdef RSS
5394 	int nbuckets = rss_getnumbuckets();
5395 	int hashconfig = rss_gethashconfig();
5396 	int extra;
5397 #endif
5398 
5399 	ASSERT_SYNCHRONIZED_OP(sc);
5400 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
5401 	    ("%s: VI_INIT_DONE already", __func__));
5402 
5403 	sysctl_ctx_init(&vi->ctx);
5404 	vi->flags |= VI_SYSCTL_CTX;
5405 
5406 	/*
5407 	 * Allocate tx/rx/fl queues for this VI.
5408 	 */
5409 	rc = t4_setup_vi_queues(vi);
5410 	if (rc != 0)
5411 		goto done;	/* error message displayed already */
5412 
5413 	/*
5414 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
5415 	 */
5416 	if (vi->nrxq > vi->rss_size) {
5417 		if_printf(ifp, "nrxq (%d) > hw RSS table size (%d); "
5418 		    "some queues will never receive traffic.\n", vi->nrxq,
5419 		    vi->rss_size);
5420 	} else if (vi->rss_size % vi->nrxq) {
5421 		if_printf(ifp, "nrxq (%d), hw RSS table size (%d); "
5422 		    "expect uneven traffic distribution.\n", vi->nrxq,
5423 		    vi->rss_size);
5424 	}
5425 #ifdef RSS
5426 	if (vi->nrxq != nbuckets) {
5427 		if_printf(ifp, "nrxq (%d) != kernel RSS buckets (%d);"
5428 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
5429 	}
5430 #endif
5431 	rss = malloc(vi->rss_size * sizeof (*rss), M_CXGBE, M_ZERO | M_WAITOK);
5432 	for (i = 0; i < vi->rss_size;) {
5433 #ifdef RSS
5434 		j = rss_get_indirection_to_bucket(i);
5435 		j %= vi->nrxq;
5436 		rxq = &sc->sge.rxq[vi->first_rxq + j];
5437 		rss[i++] = rxq->iq.abs_id;
5438 #else
5439 		for_each_rxq(vi, j, rxq) {
5440 			rss[i++] = rxq->iq.abs_id;
5441 			if (i == vi->rss_size)
5442 				break;
5443 		}
5444 #endif
5445 	}
5446 
5447 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size, rss,
5448 	    vi->rss_size);
5449 	if (rc != 0) {
5450 		free(rss, M_CXGBE);
5451 		if_printf(ifp, "rss_config failed: %d\n", rc);
5452 		goto done;
5453 	}
5454 
5455 #ifdef RSS
5456 	hashen = hashconfig_to_hashen(hashconfig);
5457 
5458 	/*
5459 	 * We may have had to enable some hashes even though the global config
5460 	 * wants them disabled.  This is a potential problem that must be
5461 	 * reported to the user.
5462 	 */
5463 	extra = hashen_to_hashconfig(hashen) ^ hashconfig;
5464 
5465 	/*
5466 	 * If we consider only the supported hash types, then the enabled hashes
5467 	 * are a superset of the requested hashes.  In other words, there cannot
5468 	 * be any supported hash that was requested but not enabled, but there
5469 	 * can be hashes that were not requested but had to be enabled.
5470 	 */
5471 	extra &= SUPPORTED_RSS_HASHTYPES;
5472 	MPASS((extra & hashconfig) == 0);
5473 
5474 	if (extra) {
5475 		if_printf(ifp,
5476 		    "global RSS config (0x%x) cannot be accommodated.\n",
5477 		    hashconfig);
5478 	}
5479 	if (extra & RSS_HASHTYPE_RSS_IPV4)
5480 		if_printf(ifp, "IPv4 2-tuple hashing forced on.\n");
5481 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
5482 		if_printf(ifp, "TCP/IPv4 4-tuple hashing forced on.\n");
5483 	if (extra & RSS_HASHTYPE_RSS_IPV6)
5484 		if_printf(ifp, "IPv6 2-tuple hashing forced on.\n");
5485 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
5486 		if_printf(ifp, "TCP/IPv6 4-tuple hashing forced on.\n");
5487 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
5488 		if_printf(ifp, "UDP/IPv4 4-tuple hashing forced on.\n");
5489 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
5490 		if_printf(ifp, "UDP/IPv6 4-tuple hashing forced on.\n");
5491 #else
5492 	hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
5493 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
5494 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
5495 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
5496 #endif
5497 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, hashen, rss[0], 0, 0);
5498 	if (rc != 0) {
5499 		free(rss, M_CXGBE);
5500 		if_printf(ifp, "rss hash/defaultq config failed: %d\n", rc);
5501 		goto done;
5502 	}
5503 
5504 	vi->rss = rss;
5505 	vi->flags |= VI_INIT_DONE;
5506 done:
5507 	if (rc != 0)
5508 		vi_full_uninit(vi);
5509 
5510 	return (rc);
5511 }
5512 
5513 /*
5514  * Idempotent.
5515  */
5516 int
vi_full_uninit(struct vi_info * vi)5517 vi_full_uninit(struct vi_info *vi)
5518 {
5519 	struct port_info *pi = vi->pi;
5520 	struct adapter *sc = pi->adapter;
5521 	int i;
5522 	struct sge_rxq *rxq;
5523 	struct sge_txq *txq;
5524 #ifdef TCP_OFFLOAD
5525 	struct sge_ofld_rxq *ofld_rxq;
5526 	struct sge_wrq *ofld_txq;
5527 #endif
5528 
5529 	if (vi->flags & VI_INIT_DONE) {
5530 
5531 		/* Need to quiesce queues.  */
5532 
5533 		/* XXX: Only for the first VI? */
5534 		if (IS_MAIN_VI(vi) && !(sc->flags & IS_VF))
5535 			quiesce_wrq(sc, &sc->sge.ctrlq[pi->port_id]);
5536 
5537 		for_each_txq(vi, i, txq) {
5538 			quiesce_txq(sc, txq);
5539 		}
5540 
5541 #ifdef TCP_OFFLOAD
5542 		for_each_ofld_txq(vi, i, ofld_txq) {
5543 			quiesce_wrq(sc, ofld_txq);
5544 		}
5545 #endif
5546 
5547 		for_each_rxq(vi, i, rxq) {
5548 			quiesce_iq(sc, &rxq->iq);
5549 			quiesce_fl(sc, &rxq->fl);
5550 		}
5551 
5552 #ifdef TCP_OFFLOAD
5553 		for_each_ofld_rxq(vi, i, ofld_rxq) {
5554 			quiesce_iq(sc, &ofld_rxq->iq);
5555 			quiesce_fl(sc, &ofld_rxq->fl);
5556 		}
5557 #endif
5558 		free(vi->rss, M_CXGBE);
5559 		free(vi->nm_rss, M_CXGBE);
5560 	}
5561 
5562 	t4_teardown_vi_queues(vi);
5563 	vi->flags &= ~VI_INIT_DONE;
5564 
5565 	return (0);
5566 }
5567 
5568 static void
quiesce_txq(struct adapter * sc,struct sge_txq * txq)5569 quiesce_txq(struct adapter *sc, struct sge_txq *txq)
5570 {
5571 	struct sge_eq *eq = &txq->eq;
5572 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
5573 
5574 	(void) sc;	/* unused */
5575 
5576 #ifdef INVARIANTS
5577 	TXQ_LOCK(txq);
5578 	MPASS((eq->flags & EQ_ENABLED) == 0);
5579 	TXQ_UNLOCK(txq);
5580 #endif
5581 
5582 	/* Wait for the mp_ring to empty. */
5583 	while (!mp_ring_is_idle(txq->r)) {
5584 		mp_ring_check_drainage(txq->r, 0);
5585 		pause("rquiesce", 1);
5586 	}
5587 
5588 	/* Then wait for the hardware to finish. */
5589 	while (spg->cidx != htobe16(eq->pidx))
5590 		pause("equiesce", 1);
5591 
5592 	/* Finally, wait for the driver to reclaim all descriptors. */
5593 	while (eq->cidx != eq->pidx)
5594 		pause("dquiesce", 1);
5595 }
5596 
5597 static void
quiesce_wrq(struct adapter * sc,struct sge_wrq * wrq)5598 quiesce_wrq(struct adapter *sc, struct sge_wrq *wrq)
5599 {
5600 
5601 	/* XXXTX */
5602 }
5603 
5604 static void
quiesce_iq(struct adapter * sc,struct sge_iq * iq)5605 quiesce_iq(struct adapter *sc, struct sge_iq *iq)
5606 {
5607 	(void) sc;	/* unused */
5608 
5609 	/* Synchronize with the interrupt handler */
5610 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
5611 		pause("iqfree", 1);
5612 }
5613 
5614 static void
quiesce_fl(struct adapter * sc,struct sge_fl * fl)5615 quiesce_fl(struct adapter *sc, struct sge_fl *fl)
5616 {
5617 	mtx_lock(&sc->sfl_lock);
5618 	FL_LOCK(fl);
5619 	fl->flags |= FL_DOOMED;
5620 	FL_UNLOCK(fl);
5621 	callout_stop(&sc->sfl_callout);
5622 	mtx_unlock(&sc->sfl_lock);
5623 
5624 	KASSERT((fl->flags & FL_STARVING) == 0,
5625 	    ("%s: still starving", __func__));
5626 }
5627 
5628 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)5629 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
5630     driver_intr_t *handler, void *arg, char *name)
5631 {
5632 	int rc;
5633 
5634 	irq->rid = rid;
5635 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
5636 	    RF_SHAREABLE | RF_ACTIVE);
5637 	if (irq->res == NULL) {
5638 		device_printf(sc->dev,
5639 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
5640 		return (ENOMEM);
5641 	}
5642 
5643 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
5644 	    NULL, handler, arg, &irq->tag);
5645 	if (rc != 0) {
5646 		device_printf(sc->dev,
5647 		    "failed to setup interrupt for rid %d, name %s: %d\n",
5648 		    rid, name, rc);
5649 	} else if (name)
5650 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
5651 
5652 	return (rc);
5653 }
5654 
5655 static int
t4_free_irq(struct adapter * sc,struct irq * irq)5656 t4_free_irq(struct adapter *sc, struct irq *irq)
5657 {
5658 	if (irq->tag)
5659 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
5660 	if (irq->res)
5661 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
5662 
5663 	bzero(irq, sizeof(*irq));
5664 
5665 	return (0);
5666 }
5667 
5668 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)5669 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
5670 {
5671 
5672 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
5673 	t4_get_regs(sc, buf, regs->len);
5674 }
5675 
5676 #define	A_PL_INDIR_CMD	0x1f8
5677 
5678 #define	S_PL_AUTOINC	31
5679 #define	M_PL_AUTOINC	0x1U
5680 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
5681 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
5682 
5683 #define	S_PL_VFID	20
5684 #define	M_PL_VFID	0xffU
5685 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
5686 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
5687 
5688 #define	S_PL_ADDR	0
5689 #define	M_PL_ADDR	0xfffffU
5690 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
5691 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
5692 
5693 #define	A_PL_INDIR_DATA	0x1fc
5694 
5695 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)5696 read_vf_stat(struct adapter *sc, u_int vin, int reg)
5697 {
5698 	u32 stats[2];
5699 
5700 	mtx_assert(&sc->reg_lock, MA_OWNED);
5701 	if (sc->flags & IS_VF) {
5702 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
5703 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
5704 	} else {
5705 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
5706 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
5707 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
5708 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
5709 	}
5710 	return (((uint64_t)stats[1]) << 32 | stats[0]);
5711 }
5712 
5713 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)5714 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
5715 {
5716 
5717 #define GET_STAT(name) \
5718 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
5719 
5720 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
5721 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
5722 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
5723 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
5724 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
5725 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
5726 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
5727 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
5728 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
5729 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
5730 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
5731 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
5732 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
5733 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
5734 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
5735 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
5736 
5737 #undef GET_STAT
5738 }
5739 
5740 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)5741 t4_clr_vi_stats(struct adapter *sc, u_int vin)
5742 {
5743 	int reg;
5744 
5745 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
5746 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
5747 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
5748 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
5749 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
5750 }
5751 
5752 static void
vi_refresh_stats(struct adapter * sc,struct vi_info * vi)5753 vi_refresh_stats(struct adapter *sc, struct vi_info *vi)
5754 {
5755 	struct timeval tv;
5756 	const struct timeval interval = {0, 250000};	/* 250ms */
5757 
5758 	if (!(vi->flags & VI_INIT_DONE))
5759 		return;
5760 
5761 	getmicrotime(&tv);
5762 	timevalsub(&tv, &interval);
5763 	if (timevalcmp(&tv, &vi->last_refreshed, <))
5764 		return;
5765 
5766 	mtx_lock(&sc->reg_lock);
5767 	t4_get_vi_stats(sc, vi->vin, &vi->stats);
5768 	getmicrotime(&vi->last_refreshed);
5769 	mtx_unlock(&sc->reg_lock);
5770 }
5771 
5772 static void
cxgbe_refresh_stats(struct adapter * sc,struct port_info * pi)5773 cxgbe_refresh_stats(struct adapter *sc, struct port_info *pi)
5774 {
5775 	u_int i, v, tnl_cong_drops, bg_map;
5776 	struct timeval tv;
5777 	const struct timeval interval = {0, 250000};	/* 250ms */
5778 
5779 	getmicrotime(&tv);
5780 	timevalsub(&tv, &interval);
5781 	if (timevalcmp(&tv, &pi->last_refreshed, <))
5782 		return;
5783 
5784 	tnl_cong_drops = 0;
5785 	t4_get_port_stats(sc, pi->tx_chan, &pi->stats);
5786 	bg_map = pi->mps_bg_map;
5787 	while (bg_map) {
5788 		i = ffs(bg_map) - 1;
5789 		mtx_lock(&sc->reg_lock);
5790 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
5791 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
5792 		mtx_unlock(&sc->reg_lock);
5793 		tnl_cong_drops += v;
5794 		bg_map &= ~(1 << i);
5795 	}
5796 	pi->tnl_cong_drops = tnl_cong_drops;
5797 	getmicrotime(&pi->last_refreshed);
5798 }
5799 
5800 static void
cxgbe_tick(void * arg)5801 cxgbe_tick(void *arg)
5802 {
5803 	struct port_info *pi = arg;
5804 	struct adapter *sc = pi->adapter;
5805 
5806 	PORT_LOCK_ASSERT_OWNED(pi);
5807 	cxgbe_refresh_stats(sc, pi);
5808 
5809 	callout_schedule(&pi->tick, hz);
5810 }
5811 
5812 void
vi_tick(void * arg)5813 vi_tick(void *arg)
5814 {
5815 	struct vi_info *vi = arg;
5816 	struct adapter *sc = vi->pi->adapter;
5817 
5818 	vi_refresh_stats(sc, vi);
5819 
5820 	callout_schedule(&vi->tick, hz);
5821 }
5822 
5823 /*
5824  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
5825  */
5826 static char *caps_decoder[] = {
5827 	"\20\001IPMI\002NCSI",				/* 0: NBM */
5828 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
5829 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
5830 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
5831 	    "\006HASHFILTER\007ETHOFLD",
5832 	"\20\001TOE",					/* 4: TOE */
5833 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
5834 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
5835 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
5836 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
5837 	    "\007T10DIF"
5838 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
5839 	"\20\001LOOKASIDE\002TLSKEYS",			/* 7: Crypto */
5840 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
5841 		    "\004PO_INITIATOR\005PO_TARGET",
5842 };
5843 
5844 void
t4_sysctls(struct adapter * sc)5845 t4_sysctls(struct adapter *sc)
5846 {
5847 	struct sysctl_ctx_list *ctx;
5848 	struct sysctl_oid *oid;
5849 	struct sysctl_oid_list *children, *c0;
5850 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
5851 
5852 	ctx = device_get_sysctl_ctx(sc->dev);
5853 
5854 	/*
5855 	 * dev.t4nex.X.
5856 	 */
5857 	oid = device_get_sysctl_tree(sc->dev);
5858 	c0 = children = SYSCTL_CHILDREN(oid);
5859 
5860 	sc->sc_do_rxcopy = 1;
5861 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
5862 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
5863 
5864 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
5865 	    sc->params.nports, "# of ports");
5866 
5867 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
5868 	    CTLTYPE_STRING | CTLFLAG_RD, doorbells, (uintptr_t)&sc->doorbells,
5869 	    sysctl_bitfield_8b, "A", "available doorbells");
5870 
5871 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
5872 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
5873 
5874 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
5875 	    CTLTYPE_STRING | CTLFLAG_RD, sc->params.sge.timer_val,
5876 	    sizeof(sc->params.sge.timer_val), sysctl_int_array, "A",
5877 	    "interrupt holdoff timer values (us)");
5878 
5879 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
5880 	    CTLTYPE_STRING | CTLFLAG_RD, sc->params.sge.counter_val,
5881 	    sizeof(sc->params.sge.counter_val), sysctl_int_array, "A",
5882 	    "interrupt holdoff packet counter values");
5883 
5884 	t4_sge_sysctls(sc, ctx, children);
5885 
5886 	sc->lro_timeout = 100;
5887 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
5888 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
5889 
5890 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
5891 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
5892 
5893 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
5894 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
5895 
5896 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
5897 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
5898 
5899 	if (sc->flags & IS_VF)
5900 		return;
5901 
5902 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
5903 	    NULL, chip_rev(sc), "chip hardware revision");
5904 
5905 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
5906 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
5907 
5908 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
5909 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
5910 
5911 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
5912 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
5913 
5914 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
5915 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
5916 
5917 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
5918 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
5919 
5920 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
5921 	    sc->er_version, 0, "expansion ROM version");
5922 
5923 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
5924 	    sc->bs_version, 0, "bootstrap firmware version");
5925 
5926 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
5927 	    NULL, sc->params.scfg_vers, "serial config version");
5928 
5929 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
5930 	    NULL, sc->params.vpd_vers, "VPD version");
5931 
5932 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
5933 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
5934 
5935 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
5936 	    sc->cfcsum, "config file checksum");
5937 
5938 #define SYSCTL_CAP(name, n, text) \
5939 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
5940 	    CTLTYPE_STRING | CTLFLAG_RD, caps_decoder[n], (uintptr_t)&sc->name, \
5941 	    sysctl_bitfield_16b, "A", "available " text " capabilities")
5942 
5943 	SYSCTL_CAP(nbmcaps, 0, "NBM");
5944 	SYSCTL_CAP(linkcaps, 1, "link");
5945 	SYSCTL_CAP(switchcaps, 2, "switch");
5946 	SYSCTL_CAP(niccaps, 3, "NIC");
5947 	SYSCTL_CAP(toecaps, 4, "TCP offload");
5948 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
5949 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
5950 	SYSCTL_CAP(cryptocaps, 7, "crypto");
5951 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
5952 #undef SYSCTL_CAP
5953 
5954 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
5955 	    NULL, sc->tids.nftids, "number of filters");
5956 
5957 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature", CTLTYPE_INT |
5958 	    CTLFLAG_RD, sc, 0, sysctl_temperature, "I",
5959 	    "chip temperature (in Celsius)");
5960 
5961 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg", CTLTYPE_STRING |
5962 	    CTLFLAG_RD, sc, 0, sysctl_loadavg, "A",
5963 	    "microprocessor load averages (debug firmwares only)");
5964 
5965 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd", CTLTYPE_INT |
5966 	    CTLFLAG_RD, sc, 0, sysctl_vdd, "I", "core Vdd (in mV)");
5967 
5968 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
5969 	    CTLTYPE_STRING | CTLFLAG_RD, sc, LOCAL_CPUS,
5970 	    sysctl_cpus, "A", "local CPUs");
5971 
5972 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
5973 	    CTLTYPE_STRING | CTLFLAG_RD, sc, INTR_CPUS,
5974 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
5975 
5976 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
5977 	    &sc->swintr, 0, "software triggered interrupts");
5978 
5979 	/*
5980 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
5981 	 */
5982 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
5983 	    CTLFLAG_RD | CTLFLAG_SKIP, NULL,
5984 	    "logs and miscellaneous information");
5985 	children = SYSCTL_CHILDREN(oid);
5986 
5987 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
5988 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
5989 	    sysctl_cctrl, "A", "congestion control");
5990 
5991 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
5992 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
5993 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
5994 
5995 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
5996 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 1,
5997 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
5998 
5999 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
6000 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 2,
6001 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
6002 
6003 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
6004 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 3,
6005 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
6006 
6007 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
6008 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 4,
6009 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
6010 
6011 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
6012 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 5,
6013 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
6014 
6015 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
6016 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0, sysctl_cim_la,
6017 	    "A", "CIM logic analyzer");
6018 
6019 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
6020 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6021 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
6022 
6023 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
6024 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0 + CIM_NUM_IBQ,
6025 	    sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
6026 
6027 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
6028 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 1 + CIM_NUM_IBQ,
6029 	    sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
6030 
6031 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
6032 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 2 + CIM_NUM_IBQ,
6033 	    sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
6034 
6035 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
6036 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 3 + CIM_NUM_IBQ,
6037 	    sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
6038 
6039 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
6040 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 4 + CIM_NUM_IBQ,
6041 	    sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
6042 
6043 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
6044 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 5 + CIM_NUM_IBQ,
6045 	    sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
6046 
6047 	if (chip_id(sc) > CHELSIO_T4) {
6048 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
6049 		    CTLTYPE_STRING | CTLFLAG_RD, sc, 6 + CIM_NUM_IBQ,
6050 		    sysctl_cim_ibq_obq, "A", "CIM OBQ 6 (SGE0-RX)");
6051 
6052 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
6053 		    CTLTYPE_STRING | CTLFLAG_RD, sc, 7 + CIM_NUM_IBQ,
6054 		    sysctl_cim_ibq_obq, "A", "CIM OBQ 7 (SGE1-RX)");
6055 	}
6056 
6057 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
6058 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6059 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
6060 
6061 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
6062 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6063 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
6064 
6065 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
6066 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6067 	    sysctl_cpl_stats, "A", "CPL statistics");
6068 
6069 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
6070 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6071 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
6072 
6073 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
6074 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6075 	    sysctl_devlog, "A", "firmware's device log");
6076 
6077 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
6078 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6079 	    sysctl_fcoe_stats, "A", "FCoE statistics");
6080 
6081 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
6082 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6083 	    sysctl_hw_sched, "A", "hardware scheduler ");
6084 
6085 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
6086 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6087 	    sysctl_l2t, "A", "hardware L2 table");
6088 
6089 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
6090 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6091 	    sysctl_smt, "A", "hardware source MAC table");
6092 
6093 #ifdef INET6
6094 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
6095 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6096 	    sysctl_clip, "A", "active CLIP table entries");
6097 #endif
6098 
6099 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
6100 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6101 	    sysctl_lb_stats, "A", "loopback statistics");
6102 
6103 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
6104 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6105 	    sysctl_meminfo, "A", "memory regions");
6106 
6107 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
6108 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6109 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
6110 	    "A", "MPS TCAM entries");
6111 
6112 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
6113 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6114 	    sysctl_path_mtus, "A", "path MTUs");
6115 
6116 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
6117 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6118 	    sysctl_pm_stats, "A", "PM statistics");
6119 
6120 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
6121 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6122 	    sysctl_rdma_stats, "A", "RDMA statistics");
6123 
6124 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
6125 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6126 	    sysctl_tcp_stats, "A", "TCP statistics");
6127 
6128 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
6129 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6130 	    sysctl_tids, "A", "TID information");
6131 
6132 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
6133 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6134 	    sysctl_tp_err_stats, "A", "TP error statistics");
6135 
6136 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
6137 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_tp_la_mask, "I",
6138 	    "TP logic analyzer event capture mask");
6139 
6140 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
6141 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6142 	    sysctl_tp_la, "A", "TP logic analyzer");
6143 
6144 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
6145 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6146 	    sysctl_tx_rate, "A", "Tx rate");
6147 
6148 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
6149 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6150 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
6151 
6152 	if (chip_id(sc) >= CHELSIO_T5) {
6153 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
6154 		    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
6155 		    sysctl_wcwr_stats, "A", "write combined work requests");
6156 	}
6157 
6158 #ifdef TCP_OFFLOAD
6159 	if (is_offload(sc)) {
6160 		int i;
6161 		char s[4];
6162 
6163 		/*
6164 		 * dev.t4nex.X.toe.
6165 		 */
6166 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe", CTLFLAG_RD,
6167 		    NULL, "TOE parameters");
6168 		children = SYSCTL_CHILDREN(oid);
6169 
6170 		sc->tt.cong_algorithm = -1;
6171 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
6172 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
6173 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
6174 		    "3 = highspeed)");
6175 
6176 		sc->tt.sndbuf = 256 * 1024;
6177 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
6178 		    &sc->tt.sndbuf, 0, "max hardware send buffer size");
6179 
6180 		sc->tt.ddp = 0;
6181 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp", CTLFLAG_RW,
6182 		    &sc->tt.ddp, 0, "DDP allowed");
6183 
6184 		sc->tt.rx_coalesce = 1;
6185 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
6186 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
6187 
6188 		sc->tt.tls = 0;
6189 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tls", CTLFLAG_RW,
6190 		    &sc->tt.tls, 0, "Inline TLS allowed");
6191 
6192 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls_rx_ports",
6193 		    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_tls_rx_ports,
6194 		    "I", "TCP ports that use inline TLS+TOE RX");
6195 
6196 		sc->tt.tx_align = 1;
6197 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
6198 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
6199 
6200 		sc->tt.tx_zcopy = 0;
6201 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
6202 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
6203 		    "Enable zero-copy aio_write(2)");
6204 
6205 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
6206 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
6207 		    "cop_managed_offloading", CTLFLAG_RW,
6208 		    &sc->tt.cop_managed_offloading, 0,
6209 		    "COP (Connection Offload Policy) controls all TOE offload");
6210 
6211 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
6212 		    CTLTYPE_STRING | CTLFLAG_RD, sc, 0, sysctl_tp_tick, "A",
6213 		    "TP timer tick (us)");
6214 
6215 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
6216 		    CTLTYPE_STRING | CTLFLAG_RD, sc, 1, sysctl_tp_tick, "A",
6217 		    "TCP timestamp tick (us)");
6218 
6219 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
6220 		    CTLTYPE_STRING | CTLFLAG_RD, sc, 2, sysctl_tp_tick, "A",
6221 		    "DACK tick (us)");
6222 
6223 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
6224 		    CTLTYPE_UINT | CTLFLAG_RD, sc, 0, sysctl_tp_dack_timer,
6225 		    "IU", "DACK timer (us)");
6226 
6227 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
6228 		    CTLTYPE_ULONG | CTLFLAG_RD, sc, A_TP_RXT_MIN,
6229 		    sysctl_tp_timer, "LU", "Minimum retransmit interval (us)");
6230 
6231 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
6232 		    CTLTYPE_ULONG | CTLFLAG_RD, sc, A_TP_RXT_MAX,
6233 		    sysctl_tp_timer, "LU", "Maximum retransmit interval (us)");
6234 
6235 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
6236 		    CTLTYPE_ULONG | CTLFLAG_RD, sc, A_TP_PERS_MIN,
6237 		    sysctl_tp_timer, "LU", "Persist timer min (us)");
6238 
6239 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
6240 		    CTLTYPE_ULONG | CTLFLAG_RD, sc, A_TP_PERS_MAX,
6241 		    sysctl_tp_timer, "LU", "Persist timer max (us)");
6242 
6243 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
6244 		    CTLTYPE_ULONG | CTLFLAG_RD, sc, A_TP_KEEP_IDLE,
6245 		    sysctl_tp_timer, "LU", "Keepalive idle timer (us)");
6246 
6247 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
6248 		    CTLTYPE_ULONG | CTLFLAG_RD, sc, A_TP_KEEP_INTVL,
6249 		    sysctl_tp_timer, "LU", "Keepalive interval timer (us)");
6250 
6251 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
6252 		    CTLTYPE_ULONG | CTLFLAG_RD, sc, A_TP_INIT_SRTT,
6253 		    sysctl_tp_timer, "LU", "Initial SRTT (us)");
6254 
6255 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
6256 		    CTLTYPE_ULONG | CTLFLAG_RD, sc, A_TP_FINWAIT2_TIMER,
6257 		    sysctl_tp_timer, "LU", "FINWAIT2 timer (us)");
6258 
6259 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
6260 		    CTLTYPE_UINT | CTLFLAG_RD, sc, S_SYNSHIFTMAX,
6261 		    sysctl_tp_shift_cnt, "IU",
6262 		    "Number of SYN retransmissions before abort");
6263 
6264 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
6265 		    CTLTYPE_UINT | CTLFLAG_RD, sc, S_RXTSHIFTMAXR2,
6266 		    sysctl_tp_shift_cnt, "IU",
6267 		    "Number of retransmissions before abort");
6268 
6269 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
6270 		    CTLTYPE_UINT | CTLFLAG_RD, sc, S_KEEPALIVEMAXR2,
6271 		    sysctl_tp_shift_cnt, "IU",
6272 		    "Number of keepalive probes before abort");
6273 
6274 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
6275 		    CTLFLAG_RD, NULL, "TOE retransmit backoffs");
6276 		children = SYSCTL_CHILDREN(oid);
6277 		for (i = 0; i < 16; i++) {
6278 			snprintf(s, sizeof(s), "%u", i);
6279 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
6280 			    CTLTYPE_UINT | CTLFLAG_RD, sc, i, sysctl_tp_backoff,
6281 			    "IU", "TOE retransmit backoff");
6282 		}
6283 	}
6284 #endif
6285 }
6286 
6287 void
vi_sysctls(struct vi_info * vi)6288 vi_sysctls(struct vi_info *vi)
6289 {
6290 	struct sysctl_ctx_list *ctx;
6291 	struct sysctl_oid *oid;
6292 	struct sysctl_oid_list *children;
6293 
6294 	ctx = device_get_sysctl_ctx(vi->dev);
6295 
6296 	/*
6297 	 * dev.v?(cxgbe|cxl).X.
6298 	 */
6299 	oid = device_get_sysctl_tree(vi->dev);
6300 	children = SYSCTL_CHILDREN(oid);
6301 
6302 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
6303 	    vi->viid, "VI identifer");
6304 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
6305 	    &vi->nrxq, 0, "# of rx queues");
6306 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
6307 	    &vi->ntxq, 0, "# of tx queues");
6308 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
6309 	    &vi->first_rxq, 0, "index of first rx queue");
6310 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
6311 	    &vi->first_txq, 0, "index of first tx queue");
6312 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
6313 	    vi->rss_base, "start of RSS indirection table");
6314 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
6315 	    vi->rss_size, "size of RSS indirection table");
6316 
6317 	if (IS_MAIN_VI(vi)) {
6318 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
6319 		    CTLTYPE_INT | CTLFLAG_RW, vi, 0, sysctl_noflowq, "IU",
6320 		    "Reserve queue 0 for non-flowid packets");
6321 	}
6322 
6323 #ifdef TCP_OFFLOAD
6324 	if (vi->nofldrxq != 0) {
6325 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
6326 		    &vi->nofldrxq, 0,
6327 		    "# of rx queues for offloaded TCP connections");
6328 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
6329 		    &vi->nofldtxq, 0,
6330 		    "# of tx queues for offloaded TCP connections");
6331 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
6332 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
6333 		    "index of first TOE rx queue");
6334 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
6335 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
6336 		    "index of first TOE tx queue");
6337 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
6338 		    CTLTYPE_INT | CTLFLAG_RW, vi, 0,
6339 		    sysctl_holdoff_tmr_idx_ofld, "I",
6340 		    "holdoff timer index for TOE queues");
6341 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
6342 		    CTLTYPE_INT | CTLFLAG_RW, vi, 0,
6343 		    sysctl_holdoff_pktc_idx_ofld, "I",
6344 		    "holdoff packet counter index for TOE queues");
6345 	}
6346 #endif
6347 #ifdef DEV_NETMAP
6348 	if (vi->nnmrxq != 0) {
6349 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
6350 		    &vi->nnmrxq, 0, "# of netmap rx queues");
6351 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
6352 		    &vi->nnmtxq, 0, "# of netmap tx queues");
6353 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
6354 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
6355 		    "index of first netmap rx queue");
6356 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
6357 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
6358 		    "index of first netmap tx queue");
6359 	}
6360 #endif
6361 
6362 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
6363 	    CTLTYPE_INT | CTLFLAG_RW, vi, 0, sysctl_holdoff_tmr_idx, "I",
6364 	    "holdoff timer index");
6365 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
6366 	    CTLTYPE_INT | CTLFLAG_RW, vi, 0, sysctl_holdoff_pktc_idx, "I",
6367 	    "holdoff packet counter index");
6368 
6369 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
6370 	    CTLTYPE_INT | CTLFLAG_RW, vi, 0, sysctl_qsize_rxq, "I",
6371 	    "rx queue size");
6372 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
6373 	    CTLTYPE_INT | CTLFLAG_RW, vi, 0, sysctl_qsize_txq, "I",
6374 	    "tx queue size");
6375 }
6376 
6377 static void
cxgbe_sysctls(struct port_info * pi)6378 cxgbe_sysctls(struct port_info *pi)
6379 {
6380 	struct sysctl_ctx_list *ctx;
6381 	struct sysctl_oid *oid;
6382 	struct sysctl_oid_list *children, *children2;
6383 	struct adapter *sc = pi->adapter;
6384 	int i;
6385 	char name[16];
6386 	static char *tc_flags = {"\20\1USER\2SYNC\3ASYNC\4ERR"};
6387 
6388 	ctx = device_get_sysctl_ctx(pi->dev);
6389 
6390 	/*
6391 	 * dev.cxgbe.X.
6392 	 */
6393 	oid = device_get_sysctl_tree(pi->dev);
6394 	children = SYSCTL_CHILDREN(oid);
6395 
6396 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc", CTLTYPE_STRING |
6397 	   CTLFLAG_RD, pi, 0, sysctl_linkdnrc, "A", "reason why link is down");
6398 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
6399 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
6400 		    CTLTYPE_INT | CTLFLAG_RD, pi, 0, sysctl_btphy, "I",
6401 		    "PHY temperature (in Celsius)");
6402 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
6403 		    CTLTYPE_INT | CTLFLAG_RD, pi, 1, sysctl_btphy, "I",
6404 		    "PHY firmware version");
6405 	}
6406 
6407 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
6408 	    CTLTYPE_STRING | CTLFLAG_RW, pi, 0, sysctl_pause_settings, "A",
6409     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
6410 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fec",
6411 	    CTLTYPE_STRING | CTLFLAG_RW, pi, 0, sysctl_fec, "A",
6412 	    "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
6413 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
6414 	    CTLTYPE_INT | CTLFLAG_RW, pi, 0, sysctl_autoneg, "I",
6415 	    "autonegotiation (-1 = not supported)");
6416 
6417 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
6418 	    port_top_speed(pi), "max speed (in Gbps)");
6419 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
6420 	    pi->mps_bg_map, "MPS buffer group map");
6421 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
6422 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
6423 
6424 	if (sc->flags & IS_VF)
6425 		return;
6426 
6427 	/*
6428 	 * dev.(cxgbe|cxl).X.tc.
6429 	 */
6430 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc", CTLFLAG_RD, NULL,
6431 	    "Tx scheduler traffic classes (cl_rl)");
6432 	children2 = SYSCTL_CHILDREN(oid);
6433 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
6434 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
6435 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
6436 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
6437 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
6438 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
6439 	for (i = 0; i < sc->chip_params->nsched_cls; i++) {
6440 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
6441 
6442 		snprintf(name, sizeof(name), "%d", i);
6443 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
6444 		    SYSCTL_CHILDREN(oid), OID_AUTO, name, CTLFLAG_RD, NULL,
6445 		    "traffic class"));
6446 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
6447 		    CTLTYPE_STRING | CTLFLAG_RD, tc_flags, (uintptr_t)&tc->flags,
6448 		    sysctl_bitfield_8b, "A", "flags");
6449 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
6450 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
6451 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
6452 		    CTLTYPE_STRING | CTLFLAG_RD, sc, (pi->port_id << 16) | i,
6453 		    sysctl_tc_params, "A", "traffic class parameters");
6454 	}
6455 
6456 	/*
6457 	 * dev.cxgbe.X.stats.
6458 	 */
6459 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats", CTLFLAG_RD,
6460 	    NULL, "port statistics");
6461 	children = SYSCTL_CHILDREN(oid);
6462 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
6463 	    &pi->tx_parse_error, 0,
6464 	    "# of tx packets with invalid length or # of segments");
6465 
6466 #define SYSCTL_ADD_T4_REG64(pi, name, desc, reg) \
6467 	SYSCTL_ADD_OID(ctx, children, OID_AUTO, name, \
6468 	    CTLTYPE_U64 | CTLFLAG_RD, sc, reg, \
6469 	    sysctl_handle_t4_reg64, "QU", desc)
6470 
6471 	SYSCTL_ADD_T4_REG64(pi, "tx_octets", "# of octets in good frames",
6472 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_BYTES_L));
6473 	SYSCTL_ADD_T4_REG64(pi, "tx_frames", "total # of good frames",
6474 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_FRAMES_L));
6475 	SYSCTL_ADD_T4_REG64(pi, "tx_bcast_frames", "# of broadcast frames",
6476 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_BCAST_L));
6477 	SYSCTL_ADD_T4_REG64(pi, "tx_mcast_frames", "# of multicast frames",
6478 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_MCAST_L));
6479 	SYSCTL_ADD_T4_REG64(pi, "tx_ucast_frames", "# of unicast frames",
6480 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_UCAST_L));
6481 	SYSCTL_ADD_T4_REG64(pi, "tx_error_frames", "# of error frames",
6482 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_ERROR_L));
6483 	SYSCTL_ADD_T4_REG64(pi, "tx_frames_64",
6484 	    "# of tx frames in this range",
6485 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_64B_L));
6486 	SYSCTL_ADD_T4_REG64(pi, "tx_frames_65_127",
6487 	    "# of tx frames in this range",
6488 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_65B_127B_L));
6489 	SYSCTL_ADD_T4_REG64(pi, "tx_frames_128_255",
6490 	    "# of tx frames in this range",
6491 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_128B_255B_L));
6492 	SYSCTL_ADD_T4_REG64(pi, "tx_frames_256_511",
6493 	    "# of tx frames in this range",
6494 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_256B_511B_L));
6495 	SYSCTL_ADD_T4_REG64(pi, "tx_frames_512_1023",
6496 	    "# of tx frames in this range",
6497 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_512B_1023B_L));
6498 	SYSCTL_ADD_T4_REG64(pi, "tx_frames_1024_1518",
6499 	    "# of tx frames in this range",
6500 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_1024B_1518B_L));
6501 	SYSCTL_ADD_T4_REG64(pi, "tx_frames_1519_max",
6502 	    "# of tx frames in this range",
6503 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_1519B_MAX_L));
6504 	SYSCTL_ADD_T4_REG64(pi, "tx_drop", "# of dropped tx frames",
6505 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_DROP_L));
6506 	SYSCTL_ADD_T4_REG64(pi, "tx_pause", "# of pause frames transmitted",
6507 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PAUSE_L));
6508 	SYSCTL_ADD_T4_REG64(pi, "tx_ppp0", "# of PPP prio 0 frames transmitted",
6509 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP0_L));
6510 	SYSCTL_ADD_T4_REG64(pi, "tx_ppp1", "# of PPP prio 1 frames transmitted",
6511 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP1_L));
6512 	SYSCTL_ADD_T4_REG64(pi, "tx_ppp2", "# of PPP prio 2 frames transmitted",
6513 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP2_L));
6514 	SYSCTL_ADD_T4_REG64(pi, "tx_ppp3", "# of PPP prio 3 frames transmitted",
6515 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP3_L));
6516 	SYSCTL_ADD_T4_REG64(pi, "tx_ppp4", "# of PPP prio 4 frames transmitted",
6517 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP4_L));
6518 	SYSCTL_ADD_T4_REG64(pi, "tx_ppp5", "# of PPP prio 5 frames transmitted",
6519 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP5_L));
6520 	SYSCTL_ADD_T4_REG64(pi, "tx_ppp6", "# of PPP prio 6 frames transmitted",
6521 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP6_L));
6522 	SYSCTL_ADD_T4_REG64(pi, "tx_ppp7", "# of PPP prio 7 frames transmitted",
6523 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_TX_PORT_PPP7_L));
6524 
6525 	SYSCTL_ADD_T4_REG64(pi, "rx_octets", "# of octets in good frames",
6526 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_BYTES_L));
6527 	SYSCTL_ADD_T4_REG64(pi, "rx_frames", "total # of good frames",
6528 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_FRAMES_L));
6529 	SYSCTL_ADD_T4_REG64(pi, "rx_bcast_frames", "# of broadcast frames",
6530 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_BCAST_L));
6531 	SYSCTL_ADD_T4_REG64(pi, "rx_mcast_frames", "# of multicast frames",
6532 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_MCAST_L));
6533 	SYSCTL_ADD_T4_REG64(pi, "rx_ucast_frames", "# of unicast frames",
6534 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_UCAST_L));
6535 	SYSCTL_ADD_T4_REG64(pi, "rx_too_long", "# of frames exceeding MTU",
6536 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_MTU_ERROR_L));
6537 	SYSCTL_ADD_T4_REG64(pi, "rx_jabber", "# of jabber frames",
6538 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_MTU_CRC_ERROR_L));
6539 	SYSCTL_ADD_T4_REG64(pi, "rx_fcs_err",
6540 	    "# of frames received with bad FCS",
6541 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L));
6542 	SYSCTL_ADD_T4_REG64(pi, "rx_len_err",
6543 	    "# of frames received with length error",
6544 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_LEN_ERROR_L));
6545 	SYSCTL_ADD_T4_REG64(pi, "rx_symbol_err", "symbol errors",
6546 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_SYM_ERROR_L));
6547 	SYSCTL_ADD_T4_REG64(pi, "rx_runt", "# of short frames received",
6548 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_LESS_64B_L));
6549 	SYSCTL_ADD_T4_REG64(pi, "rx_frames_64",
6550 	    "# of rx frames in this range",
6551 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_64B_L));
6552 	SYSCTL_ADD_T4_REG64(pi, "rx_frames_65_127",
6553 	    "# of rx frames in this range",
6554 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_65B_127B_L));
6555 	SYSCTL_ADD_T4_REG64(pi, "rx_frames_128_255",
6556 	    "# of rx frames in this range",
6557 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_128B_255B_L));
6558 	SYSCTL_ADD_T4_REG64(pi, "rx_frames_256_511",
6559 	    "# of rx frames in this range",
6560 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_256B_511B_L));
6561 	SYSCTL_ADD_T4_REG64(pi, "rx_frames_512_1023",
6562 	    "# of rx frames in this range",
6563 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_512B_1023B_L));
6564 	SYSCTL_ADD_T4_REG64(pi, "rx_frames_1024_1518",
6565 	    "# of rx frames in this range",
6566 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_1024B_1518B_L));
6567 	SYSCTL_ADD_T4_REG64(pi, "rx_frames_1519_max",
6568 	    "# of rx frames in this range",
6569 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_1519B_MAX_L));
6570 	SYSCTL_ADD_T4_REG64(pi, "rx_pause", "# of pause frames received",
6571 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PAUSE_L));
6572 	SYSCTL_ADD_T4_REG64(pi, "rx_ppp0", "# of PPP prio 0 frames received",
6573 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP0_L));
6574 	SYSCTL_ADD_T4_REG64(pi, "rx_ppp1", "# of PPP prio 1 frames received",
6575 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP1_L));
6576 	SYSCTL_ADD_T4_REG64(pi, "rx_ppp2", "# of PPP prio 2 frames received",
6577 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP2_L));
6578 	SYSCTL_ADD_T4_REG64(pi, "rx_ppp3", "# of PPP prio 3 frames received",
6579 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP3_L));
6580 	SYSCTL_ADD_T4_REG64(pi, "rx_ppp4", "# of PPP prio 4 frames received",
6581 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP4_L));
6582 	SYSCTL_ADD_T4_REG64(pi, "rx_ppp5", "# of PPP prio 5 frames received",
6583 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP5_L));
6584 	SYSCTL_ADD_T4_REG64(pi, "rx_ppp6", "# of PPP prio 6 frames received",
6585 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP6_L));
6586 	SYSCTL_ADD_T4_REG64(pi, "rx_ppp7", "# of PPP prio 7 frames received",
6587 	    PORT_REG(pi->tx_chan, A_MPS_PORT_STAT_RX_PORT_PPP7_L));
6588 
6589 #undef SYSCTL_ADD_T4_REG64
6590 
6591 #define SYSCTL_ADD_T4_PORTSTAT(name, desc) \
6592 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
6593 	    &pi->stats.name, desc)
6594 
6595 	/* We get these from port_stats and they may be stale by up to 1s */
6596 	SYSCTL_ADD_T4_PORTSTAT(rx_ovflow0,
6597 	    "# drops due to buffer-group 0 overflows");
6598 	SYSCTL_ADD_T4_PORTSTAT(rx_ovflow1,
6599 	    "# drops due to buffer-group 1 overflows");
6600 	SYSCTL_ADD_T4_PORTSTAT(rx_ovflow2,
6601 	    "# drops due to buffer-group 2 overflows");
6602 	SYSCTL_ADD_T4_PORTSTAT(rx_ovflow3,
6603 	    "# drops due to buffer-group 3 overflows");
6604 	SYSCTL_ADD_T4_PORTSTAT(rx_trunc0,
6605 	    "# of buffer-group 0 truncated packets");
6606 	SYSCTL_ADD_T4_PORTSTAT(rx_trunc1,
6607 	    "# of buffer-group 1 truncated packets");
6608 	SYSCTL_ADD_T4_PORTSTAT(rx_trunc2,
6609 	    "# of buffer-group 2 truncated packets");
6610 	SYSCTL_ADD_T4_PORTSTAT(rx_trunc3,
6611 	    "# of buffer-group 3 truncated packets");
6612 
6613 #undef SYSCTL_ADD_T4_PORTSTAT
6614 
6615 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "tx_tls_records",
6616 	    CTLFLAG_RD, &pi->tx_tls_records,
6617 	    "# of TLS records transmitted");
6618 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "tx_tls_octets",
6619 	    CTLFLAG_RD, &pi->tx_tls_octets,
6620 	    "# of payload octets in transmitted TLS records");
6621 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "rx_tls_records",
6622 	    CTLFLAG_RD, &pi->rx_tls_records,
6623 	    "# of TLS records received");
6624 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "rx_tls_octets",
6625 	    CTLFLAG_RD, &pi->rx_tls_octets,
6626 	    "# of payload octets in received TLS records");
6627 }
6628 
6629 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)6630 sysctl_int_array(SYSCTL_HANDLER_ARGS)
6631 {
6632 	int rc, *i, space = 0;
6633 	struct sbuf sb;
6634 
6635 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
6636 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
6637 		if (space)
6638 			sbuf_printf(&sb, " ");
6639 		sbuf_printf(&sb, "%d", *i);
6640 		space = 1;
6641 	}
6642 	rc = sbuf_finish(&sb);
6643 	sbuf_delete(&sb);
6644 	return (rc);
6645 }
6646 
6647 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)6648 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
6649 {
6650 	int rc;
6651 	struct sbuf *sb;
6652 
6653 	rc = sysctl_wire_old_buffer(req, 0);
6654 	if (rc != 0)
6655 		return(rc);
6656 
6657 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6658 	if (sb == NULL)
6659 		return (ENOMEM);
6660 
6661 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
6662 	rc = sbuf_finish(sb);
6663 	sbuf_delete(sb);
6664 
6665 	return (rc);
6666 }
6667 
6668 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)6669 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
6670 {
6671 	int rc;
6672 	struct sbuf *sb;
6673 
6674 	rc = sysctl_wire_old_buffer(req, 0);
6675 	if (rc != 0)
6676 		return(rc);
6677 
6678 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6679 	if (sb == NULL)
6680 		return (ENOMEM);
6681 
6682 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
6683 	rc = sbuf_finish(sb);
6684 	sbuf_delete(sb);
6685 
6686 	return (rc);
6687 }
6688 
6689 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)6690 sysctl_btphy(SYSCTL_HANDLER_ARGS)
6691 {
6692 	struct port_info *pi = arg1;
6693 	int op = arg2;
6694 	struct adapter *sc = pi->adapter;
6695 	u_int v;
6696 	int rc;
6697 
6698 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
6699 	if (rc)
6700 		return (rc);
6701 	/* XXX: magic numbers */
6702 	rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e, op ? 0x20 : 0xc820,
6703 	    &v);
6704 	end_synchronized_op(sc, 0);
6705 	if (rc)
6706 		return (rc);
6707 	if (op == 0)
6708 		v /= 256;
6709 
6710 	rc = sysctl_handle_int(oidp, &v, 0, req);
6711 	return (rc);
6712 }
6713 
6714 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)6715 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
6716 {
6717 	struct vi_info *vi = arg1;
6718 	int rc, val;
6719 
6720 	val = vi->rsrv_noflowq;
6721 	rc = sysctl_handle_int(oidp, &val, 0, req);
6722 	if (rc != 0 || req->newptr == NULL)
6723 		return (rc);
6724 
6725 	if ((val >= 1) && (vi->ntxq > 1))
6726 		vi->rsrv_noflowq = 1;
6727 	else
6728 		vi->rsrv_noflowq = 0;
6729 
6730 	return (rc);
6731 }
6732 
6733 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)6734 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
6735 {
6736 	struct vi_info *vi = arg1;
6737 	struct adapter *sc = vi->pi->adapter;
6738 	int idx, rc, i;
6739 	struct sge_rxq *rxq;
6740 	uint8_t v;
6741 
6742 	idx = vi->tmr_idx;
6743 
6744 	rc = sysctl_handle_int(oidp, &idx, 0, req);
6745 	if (rc != 0 || req->newptr == NULL)
6746 		return (rc);
6747 
6748 	if (idx < 0 || idx >= SGE_NTIMERS)
6749 		return (EINVAL);
6750 
6751 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
6752 	    "t4tmr");
6753 	if (rc)
6754 		return (rc);
6755 
6756 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
6757 	for_each_rxq(vi, i, rxq) {
6758 #ifdef atomic_store_rel_8
6759 		atomic_store_rel_8(&rxq->iq.intr_params, v);
6760 #else
6761 		rxq->iq.intr_params = v;
6762 #endif
6763 	}
6764 	vi->tmr_idx = idx;
6765 
6766 	end_synchronized_op(sc, LOCK_HELD);
6767 	return (0);
6768 }
6769 
6770 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)6771 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
6772 {
6773 	struct vi_info *vi = arg1;
6774 	struct adapter *sc = vi->pi->adapter;
6775 	int idx, rc;
6776 
6777 	idx = vi->pktc_idx;
6778 
6779 	rc = sysctl_handle_int(oidp, &idx, 0, req);
6780 	if (rc != 0 || req->newptr == NULL)
6781 		return (rc);
6782 
6783 	if (idx < -1 || idx >= SGE_NCOUNTERS)
6784 		return (EINVAL);
6785 
6786 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
6787 	    "t4pktc");
6788 	if (rc)
6789 		return (rc);
6790 
6791 	if (vi->flags & VI_INIT_DONE)
6792 		rc = EBUSY; /* cannot be changed once the queues are created */
6793 	else
6794 		vi->pktc_idx = idx;
6795 
6796 	end_synchronized_op(sc, LOCK_HELD);
6797 	return (rc);
6798 }
6799 
6800 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)6801 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
6802 {
6803 	struct vi_info *vi = arg1;
6804 	struct adapter *sc = vi->pi->adapter;
6805 	int qsize, rc;
6806 
6807 	qsize = vi->qsize_rxq;
6808 
6809 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
6810 	if (rc != 0 || req->newptr == NULL)
6811 		return (rc);
6812 
6813 	if (qsize < 128 || (qsize & 7))
6814 		return (EINVAL);
6815 
6816 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
6817 	    "t4rxqs");
6818 	if (rc)
6819 		return (rc);
6820 
6821 	if (vi->flags & VI_INIT_DONE)
6822 		rc = EBUSY; /* cannot be changed once the queues are created */
6823 	else
6824 		vi->qsize_rxq = qsize;
6825 
6826 	end_synchronized_op(sc, LOCK_HELD);
6827 	return (rc);
6828 }
6829 
6830 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)6831 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
6832 {
6833 	struct vi_info *vi = arg1;
6834 	struct adapter *sc = vi->pi->adapter;
6835 	int qsize, rc;
6836 
6837 	qsize = vi->qsize_txq;
6838 
6839 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
6840 	if (rc != 0 || req->newptr == NULL)
6841 		return (rc);
6842 
6843 	if (qsize < 128 || qsize > 65536)
6844 		return (EINVAL);
6845 
6846 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
6847 	    "t4txqs");
6848 	if (rc)
6849 		return (rc);
6850 
6851 	if (vi->flags & VI_INIT_DONE)
6852 		rc = EBUSY; /* cannot be changed once the queues are created */
6853 	else
6854 		vi->qsize_txq = qsize;
6855 
6856 	end_synchronized_op(sc, LOCK_HELD);
6857 	return (rc);
6858 }
6859 
6860 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)6861 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
6862 {
6863 	struct port_info *pi = arg1;
6864 	struct adapter *sc = pi->adapter;
6865 	struct link_config *lc = &pi->link_cfg;
6866 	int rc;
6867 
6868 	if (req->newptr == NULL) {
6869 		struct sbuf *sb;
6870 		static char *bits = "\20\1RX\2TX\3AUTO";
6871 
6872 		rc = sysctl_wire_old_buffer(req, 0);
6873 		if (rc != 0)
6874 			return(rc);
6875 
6876 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6877 		if (sb == NULL)
6878 			return (ENOMEM);
6879 
6880 		if (lc->link_ok) {
6881 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
6882 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
6883 		} else {
6884 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
6885 			    PAUSE_RX | PAUSE_AUTONEG), bits);
6886 		}
6887 		rc = sbuf_finish(sb);
6888 		sbuf_delete(sb);
6889 	} else {
6890 		char s[2];
6891 		int n;
6892 
6893 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
6894 		    PAUSE_AUTONEG));
6895 		s[1] = 0;
6896 
6897 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
6898 		if (rc != 0)
6899 			return(rc);
6900 
6901 		if (s[1] != 0)
6902 			return (EINVAL);
6903 		if (s[0] < '0' || s[0] > '9')
6904 			return (EINVAL);	/* not a number */
6905 		n = s[0] - '0';
6906 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
6907 			return (EINVAL);	/* some other bit is set too */
6908 
6909 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
6910 		    "t4PAUSE");
6911 		if (rc)
6912 			return (rc);
6913 		PORT_LOCK(pi);
6914 		lc->requested_fc = n;
6915 		fixup_link_config(pi);
6916 		if (pi->up_vis > 0)
6917 			rc = apply_link_config(pi);
6918 		set_current_media(pi);
6919 		PORT_UNLOCK(pi);
6920 		end_synchronized_op(sc, 0);
6921 	}
6922 
6923 	return (rc);
6924 }
6925 
6926 static int
sysctl_fec(SYSCTL_HANDLER_ARGS)6927 sysctl_fec(SYSCTL_HANDLER_ARGS)
6928 {
6929 	struct port_info *pi = arg1;
6930 	struct adapter *sc = pi->adapter;
6931 	struct link_config *lc = &pi->link_cfg;
6932 	int rc;
6933 	int8_t old;
6934 
6935 	if (req->newptr == NULL) {
6936 		struct sbuf *sb;
6937 		static char *bits = "\20\1RS\2BASE-R\3RSVD1\4RSVD2\5RSVD3\6AUTO";
6938 
6939 		rc = sysctl_wire_old_buffer(req, 0);
6940 		if (rc != 0)
6941 			return(rc);
6942 
6943 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
6944 		if (sb == NULL)
6945 			return (ENOMEM);
6946 
6947 		/*
6948 		 * Display the requested_fec when the link is down -- the actual
6949 		 * FEC makes sense only when the link is up.
6950 		 */
6951 		if (lc->link_ok) {
6952 			sbuf_printf(sb, "%b", (lc->fec & M_FW_PORT_CAP32_FEC) |
6953 			    (lc->requested_fec & FEC_AUTO), bits);
6954 		} else {
6955 			sbuf_printf(sb, "%b", lc->requested_fec, bits);
6956 		}
6957 		rc = sbuf_finish(sb);
6958 		sbuf_delete(sb);
6959 	} else {
6960 		char s[3];
6961 		int n;
6962 
6963 		snprintf(s, sizeof(s), "%d",
6964 		    lc->requested_fec == FEC_AUTO ? -1 :
6965 		    lc->requested_fec & M_FW_PORT_CAP32_FEC);
6966 
6967 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
6968 		if (rc != 0)
6969 			return(rc);
6970 
6971 		n = strtol(&s[0], NULL, 0);
6972 		if (n < 0 || n & FEC_AUTO)
6973 			n = FEC_AUTO;
6974 		else {
6975 			if (n & ~M_FW_PORT_CAP32_FEC)
6976 				return (EINVAL);/* some other bit is set too */
6977 			if (!powerof2(n))
6978 				return (EINVAL);/* one bit can be set at most */
6979 		}
6980 
6981 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
6982 		    "t4fec");
6983 		if (rc)
6984 			return (rc);
6985 		PORT_LOCK(pi);
6986 		old = lc->requested_fec;
6987 		if (n == FEC_AUTO)
6988 			lc->requested_fec = FEC_AUTO;
6989 		else if (n == 0)
6990 			lc->requested_fec = FEC_NONE;
6991 		else {
6992 			if ((lc->supported | V_FW_PORT_CAP32_FEC(n)) !=
6993 			    lc->supported) {
6994 				rc = ENOTSUP;
6995 				goto done;
6996 			}
6997 			lc->requested_fec = n;
6998 		}
6999 		fixup_link_config(pi);
7000 		if (pi->up_vis > 0) {
7001 			rc = apply_link_config(pi);
7002 			if (rc != 0) {
7003 				lc->requested_fec = old;
7004 				if (rc == FW_EPROTO)
7005 					rc = ENOTSUP;
7006 			}
7007 		}
7008 done:
7009 		PORT_UNLOCK(pi);
7010 		end_synchronized_op(sc, 0);
7011 	}
7012 
7013 	return (rc);
7014 }
7015 
7016 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)7017 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
7018 {
7019 	struct port_info *pi = arg1;
7020 	struct adapter *sc = pi->adapter;
7021 	struct link_config *lc = &pi->link_cfg;
7022 	int rc, val;
7023 
7024 	if (lc->supported & FW_PORT_CAP32_ANEG)
7025 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
7026 	else
7027 		val = -1;
7028 	rc = sysctl_handle_int(oidp, &val, 0, req);
7029 	if (rc != 0 || req->newptr == NULL)
7030 		return (rc);
7031 	if (val == 0)
7032 		val = AUTONEG_DISABLE;
7033 	else if (val == 1)
7034 		val = AUTONEG_ENABLE;
7035 	else
7036 		val = AUTONEG_AUTO;
7037 
7038 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
7039 	    "t4aneg");
7040 	if (rc)
7041 		return (rc);
7042 	PORT_LOCK(pi);
7043 	if (val == AUTONEG_ENABLE && !(lc->supported & FW_PORT_CAP32_ANEG)) {
7044 		rc = ENOTSUP;
7045 		goto done;
7046 	}
7047 	lc->requested_aneg = val;
7048 	fixup_link_config(pi);
7049 	if (pi->up_vis > 0)
7050 		rc = apply_link_config(pi);
7051 	set_current_media(pi);
7052 done:
7053 	PORT_UNLOCK(pi);
7054 	end_synchronized_op(sc, 0);
7055 	return (rc);
7056 }
7057 
7058 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)7059 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
7060 {
7061 	struct adapter *sc = arg1;
7062 	int reg = arg2;
7063 	uint64_t val;
7064 
7065 	val = t4_read_reg64(sc, reg);
7066 
7067 	return (sysctl_handle_64(oidp, &val, 0, req));
7068 }
7069 
7070 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)7071 sysctl_temperature(SYSCTL_HANDLER_ARGS)
7072 {
7073 	struct adapter *sc = arg1;
7074 	int rc, t;
7075 	uint32_t param, val;
7076 
7077 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
7078 	if (rc)
7079 		return (rc);
7080 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
7081 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
7082 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
7083 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
7084 	end_synchronized_op(sc, 0);
7085 	if (rc)
7086 		return (rc);
7087 
7088 	/* unknown is returned as 0 but we display -1 in that case */
7089 	t = val == 0 ? -1 : val;
7090 
7091 	rc = sysctl_handle_int(oidp, &t, 0, req);
7092 	return (rc);
7093 }
7094 
7095 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)7096 sysctl_vdd(SYSCTL_HANDLER_ARGS)
7097 {
7098 	struct adapter *sc = arg1;
7099 	int rc;
7100 	uint32_t param, val;
7101 
7102 	if (sc->params.core_vdd == 0) {
7103 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
7104 		    "t4vdd");
7105 		if (rc)
7106 			return (rc);
7107 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
7108 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
7109 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
7110 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
7111 		end_synchronized_op(sc, 0);
7112 		if (rc)
7113 			return (rc);
7114 		sc->params.core_vdd = val;
7115 	}
7116 
7117 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
7118 }
7119 
7120 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)7121 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
7122 {
7123 	struct adapter *sc = arg1;
7124 	struct sbuf *sb;
7125 	int rc;
7126 	uint32_t param, val;
7127 
7128 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
7129 	if (rc)
7130 		return (rc);
7131 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
7132 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
7133 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
7134 	end_synchronized_op(sc, 0);
7135 	if (rc)
7136 		return (rc);
7137 
7138 	rc = sysctl_wire_old_buffer(req, 0);
7139 	if (rc != 0)
7140 		return (rc);
7141 
7142 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
7143 	if (sb == NULL)
7144 		return (ENOMEM);
7145 
7146 	if (val == 0xffffffff) {
7147 		/* Only debug and custom firmwares report load averages. */
7148 		sbuf_printf(sb, "not available");
7149 	} else {
7150 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
7151 		    (val >> 16) & 0xff);
7152 	}
7153 	rc = sbuf_finish(sb);
7154 	sbuf_delete(sb);
7155 
7156 	return (rc);
7157 }
7158 
7159 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)7160 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
7161 {
7162 	struct adapter *sc = arg1;
7163 	struct sbuf *sb;
7164 	int rc, i;
7165 	uint16_t incr[NMTUS][NCCTRL_WIN];
7166 	static const char *dec_fac[] = {
7167 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
7168 		"0.9375"
7169 	};
7170 
7171 	rc = sysctl_wire_old_buffer(req, 0);
7172 	if (rc != 0)
7173 		return (rc);
7174 
7175 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
7176 	if (sb == NULL)
7177 		return (ENOMEM);
7178 
7179 	t4_read_cong_tbl(sc, incr);
7180 
7181 	for (i = 0; i < NCCTRL_WIN; ++i) {
7182 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
7183 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
7184 		    incr[5][i], incr[6][i], incr[7][i]);
7185 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
7186 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
7187 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
7188 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
7189 	}
7190 
7191 	rc = sbuf_finish(sb);
7192 	sbuf_delete(sb);
7193 
7194 	return (rc);
7195 }
7196 
7197 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
7198 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
7199 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
7200 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
7201 };
7202 
7203 static int
sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)7204 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
7205 {
7206 	struct adapter *sc = arg1;
7207 	struct sbuf *sb;
7208 	int rc, i, n, qid = arg2;
7209 	uint32_t *buf, *p;
7210 	char *qtype;
7211 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
7212 
7213 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
7214 	    ("%s: bad qid %d\n", __func__, qid));
7215 
7216 	if (qid < CIM_NUM_IBQ) {
7217 		/* inbound queue */
7218 		qtype = "IBQ";
7219 		n = 4 * CIM_IBQ_SIZE;
7220 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
7221 		rc = t4_read_cim_ibq(sc, qid, buf, n);
7222 	} else {
7223 		/* outbound queue */
7224 		qtype = "OBQ";
7225 		qid -= CIM_NUM_IBQ;
7226 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
7227 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
7228 		rc = t4_read_cim_obq(sc, qid, buf, n);
7229 	}
7230 
7231 	if (rc < 0) {
7232 		rc = -rc;
7233 		goto done;
7234 	}
7235 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
7236 
7237 	rc = sysctl_wire_old_buffer(req, 0);
7238 	if (rc != 0)
7239 		goto done;
7240 
7241 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
7242 	if (sb == NULL) {
7243 		rc = ENOMEM;
7244 		goto done;
7245 	}
7246 
7247 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
7248 	for (i = 0, p = buf; i < n; i += 16, p += 4)
7249 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
7250 		    p[2], p[3]);
7251 
7252 	rc = sbuf_finish(sb);
7253 	sbuf_delete(sb);
7254 done:
7255 	free(buf, M_CXGBE);
7256 	return (rc);
7257 }
7258 
7259 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)7260 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
7261 {
7262 	uint32_t *p;
7263 
7264 	sbuf_printf(sb, "Status   Data      PC%s",
7265 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
7266 	    "     LS0Stat  LS0Addr             LS0Data");
7267 
7268 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
7269 		if (cfg & F_UPDBGLACAPTPCONLY) {
7270 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
7271 			    p[6], p[7]);
7272 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
7273 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
7274 			    p[4] & 0xff, p[5] >> 8);
7275 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
7276 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
7277 			    p[1] & 0xf, p[2] >> 4);
7278 		} else {
7279 			sbuf_printf(sb,
7280 			    "\n  %02x   %x%07x %x%07x %08x %08x "
7281 			    "%08x%08x%08x%08x",
7282 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
7283 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
7284 			    p[6], p[7]);
7285 		}
7286 	}
7287 }
7288 
7289 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)7290 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
7291 {
7292 	uint32_t *p;
7293 
7294 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
7295 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
7296 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
7297 
7298 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
7299 		if (cfg & F_UPDBGLACAPTPCONLY) {
7300 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
7301 			    p[3] & 0xff, p[2], p[1], p[0]);
7302 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
7303 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
7304 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
7305 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
7306 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
7307 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
7308 			    p[6] >> 16);
7309 		} else {
7310 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
7311 			    "%08x %08x %08x %08x %08x %08x",
7312 			    (p[9] >> 16) & 0xff,
7313 			    p[9] & 0xffff, p[8] >> 16,
7314 			    p[8] & 0xffff, p[7] >> 16,
7315 			    p[7] & 0xffff, p[6] >> 16,
7316 			    p[2], p[1], p[0], p[5], p[4], p[3]);
7317 		}
7318 	}
7319 }
7320 
7321 static int
sbuf_cim_la(struct adapter * sc,struct sbuf * sb,int flags)7322 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
7323 {
7324 	uint32_t cfg, *buf;
7325 	int rc;
7326 
7327 	rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
7328 	if (rc != 0)
7329 		return (rc);
7330 
7331 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
7332 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
7333 	    M_ZERO | flags);
7334 	if (buf == NULL)
7335 		return (ENOMEM);
7336 
7337 	rc = -t4_cim_read_la(sc, buf, NULL);
7338 	if (rc != 0)
7339 		goto done;
7340 	if (chip_id(sc) < CHELSIO_T6)
7341 		sbuf_cim_la4(sc, sb, buf, cfg);
7342 	else
7343 		sbuf_cim_la6(sc, sb, buf, cfg);
7344 
7345 done:
7346 	free(buf, M_CXGBE);
7347 	return (rc);
7348 }
7349 
7350 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)7351 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
7352 {
7353 	struct adapter *sc = arg1;
7354 	struct sbuf *sb;
7355 	int rc;
7356 
7357 	rc = sysctl_wire_old_buffer(req, 0);
7358 	if (rc != 0)
7359 		return (rc);
7360 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
7361 	if (sb == NULL)
7362 		return (ENOMEM);
7363 
7364 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
7365 	if (rc == 0)
7366 		rc = sbuf_finish(sb);
7367 	sbuf_delete(sb);
7368 	return (rc);
7369 }
7370 
7371 bool
t4_os_dump_cimla(struct adapter * sc,int arg,bool verbose)7372 t4_os_dump_cimla(struct adapter *sc, int arg, bool verbose)
7373 {
7374 	struct sbuf sb;
7375 	int rc;
7376 
7377 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb)
7378 		return (false);
7379 	rc = sbuf_cim_la(sc, &sb, M_NOWAIT);
7380 	if (rc == 0) {
7381 		rc = sbuf_finish(&sb);
7382 		if (rc == 0) {
7383 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s",
7384 		    		device_get_nameunit(sc->dev), sbuf_data(&sb));
7385 		}
7386 	}
7387 	sbuf_delete(&sb);
7388 	return (false);
7389 }
7390 
7391 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)7392 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
7393 {
7394 	struct adapter *sc = arg1;
7395 	u_int i;
7396 	struct sbuf *sb;
7397 	uint32_t *buf, *p;
7398 	int rc;
7399 
7400 	rc = sysctl_wire_old_buffer(req, 0);
7401 	if (rc != 0)
7402 		return (rc);
7403 
7404 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
7405 	if (sb == NULL)
7406 		return (ENOMEM);
7407 
7408 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
7409 	    M_ZERO | M_WAITOK);
7410 
7411 	t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
7412 	p = buf;
7413 
7414 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
7415 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
7416 		    p[1], p[0]);
7417 	}
7418 
7419 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
7420 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
7421 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
7422 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
7423 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
7424 		    (p[1] >> 2) | ((p[2] & 3) << 30),
7425 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
7426 		    p[0] & 1);
7427 	}
7428 
7429 	rc = sbuf_finish(sb);
7430 	sbuf_delete(sb);
7431 	free(buf, M_CXGBE);
7432 	return (rc);
7433 }
7434 
7435 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)7436 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
7437 {
7438 	struct adapter *sc = arg1;
7439 	u_int i;
7440 	struct sbuf *sb;
7441 	uint32_t *buf, *p;
7442 	int rc;
7443 
7444 	rc = sysctl_wire_old_buffer(req, 0);
7445 	if (rc != 0)
7446 		return (rc);
7447 
7448 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
7449 	if (sb == NULL)
7450 		return (ENOMEM);
7451 
7452 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
7453 	    M_ZERO | M_WAITOK);
7454 
7455 	t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
7456 	p = buf;
7457 
7458 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
7459 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
7460 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
7461 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
7462 		    p[4], p[3], p[2], p[1], p[0]);
7463 	}
7464 
7465 	sbuf_printf(sb, "\n\nCntl ID               Data");
7466 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
7467 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
7468 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
7469 	}
7470 
7471 	rc = sbuf_finish(sb);
7472 	sbuf_delete(sb);
7473 	free(buf, M_CXGBE);
7474 	return (rc);
7475 }
7476 
7477 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)7478 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
7479 {
7480 	struct adapter *sc = arg1;
7481 	struct sbuf *sb;
7482 	int rc, i;
7483 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
7484 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
7485 	uint16_t thres[CIM_NUM_IBQ];
7486 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
7487 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
7488 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
7489 
7490 	cim_num_obq = sc->chip_params->cim_num_obq;
7491 	if (is_t4(sc)) {
7492 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
7493 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
7494 	} else {
7495 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
7496 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
7497 	}
7498 	nq = CIM_NUM_IBQ + cim_num_obq;
7499 
7500 	rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
7501 	if (rc == 0)
7502 		rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq, obq_wr);
7503 	if (rc != 0)
7504 		return (rc);
7505 
7506 	t4_read_cimq_cfg(sc, base, size, thres);
7507 
7508 	rc = sysctl_wire_old_buffer(req, 0);
7509 	if (rc != 0)
7510 		return (rc);
7511 
7512 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
7513 	if (sb == NULL)
7514 		return (ENOMEM);
7515 
7516 	sbuf_printf(sb,
7517 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
7518 
7519 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
7520 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
7521 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
7522 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
7523 		    G_QUEREMFLITS(p[2]) * 16);
7524 	for ( ; i < nq; i++, p += 4, wr += 2)
7525 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
7526 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
7527 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
7528 		    G_QUEREMFLITS(p[2]) * 16);
7529 
7530 	rc = sbuf_finish(sb);
7531 	sbuf_delete(sb);
7532 
7533 	return (rc);
7534 }
7535 
7536 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)7537 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
7538 {
7539 	struct adapter *sc = arg1;
7540 	struct sbuf *sb;
7541 	int rc;
7542 	struct tp_cpl_stats stats;
7543 
7544 	rc = sysctl_wire_old_buffer(req, 0);
7545 	if (rc != 0)
7546 		return (rc);
7547 
7548 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
7549 	if (sb == NULL)
7550 		return (ENOMEM);
7551 
7552 	mtx_lock(&sc->reg_lock);
7553 	t4_tp_get_cpl_stats(sc, &stats, 0);
7554 	mtx_unlock(&sc->reg_lock);
7555 
7556 	if (sc->chip_params->nchan > 2) {
7557 		sbuf_printf(sb, "                 channel 0  channel 1"
7558 		    "  channel 2  channel 3");
7559 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
7560 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
7561 		sbuf_printf(sb, "\nCPL responses:   %10u %10u %10u %10u",
7562 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
7563 	} else {
7564 		sbuf_printf(sb, "                 channel 0  channel 1");
7565 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
7566 		    stats.req[0], stats.req[1]);
7567 		sbuf_printf(sb, "\nCPL responses:   %10u %10u",
7568 		    stats.rsp[0], stats.rsp[1]);
7569 	}
7570 
7571 	rc = sbuf_finish(sb);
7572 	sbuf_delete(sb);
7573 
7574 	return (rc);
7575 }
7576 
7577 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)7578 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
7579 {
7580 	struct adapter *sc = arg1;
7581 	struct sbuf *sb;
7582 	int rc;
7583 	struct tp_usm_stats stats;
7584 
7585 	rc = sysctl_wire_old_buffer(req, 0);
7586 	if (rc != 0)
7587 		return(rc);
7588 
7589 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
7590 	if (sb == NULL)
7591 		return (ENOMEM);
7592 
7593 	t4_get_usm_stats(sc, &stats, 1);
7594 
7595 	sbuf_printf(sb, "Frames: %u\n", stats.frames);
7596 	sbuf_printf(sb, "Octets: %ju\n", stats.octets);
7597 	sbuf_printf(sb, "Drops:  %u", stats.drops);
7598 
7599 	rc = sbuf_finish(sb);
7600 	sbuf_delete(sb);
7601 
7602 	return (rc);
7603 }
7604 
7605 static const char * const devlog_level_strings[] = {
7606 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
7607 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
7608 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
7609 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
7610 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
7611 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
7612 };
7613 
7614 static const char * const devlog_facility_strings[] = {
7615 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
7616 	[FW_DEVLOG_FACILITY_CF]		= "CF",
7617 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
7618 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
7619 	[FW_DEVLOG_FACILITY_RES]	= "RES",
7620 	[FW_DEVLOG_FACILITY_HW]		= "HW",
7621 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
7622 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
7623 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
7624 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
7625 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
7626 	[FW_DEVLOG_FACILITY_VI]		= "VI",
7627 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
7628 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
7629 	[FW_DEVLOG_FACILITY_TM]		= "TM",
7630 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
7631 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
7632 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
7633 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
7634 	[FW_DEVLOG_FACILITY_RI]		= "RI",
7635 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
7636 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
7637 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
7638 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
7639 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
7640 };
7641 
7642 static int
sbuf_devlog(struct adapter * sc,struct sbuf * sb,int flags)7643 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
7644 {
7645 	int i, j, rc, nentries, first = 0;
7646 	struct devlog_params *dparams = &sc->params.devlog;
7647 	struct fw_devlog_e *buf, *e;
7648 	uint64_t ftstamp = UINT64_MAX;
7649 
7650 	if (dparams->addr == 0)
7651 		return (ENXIO);
7652 
7653 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
7654 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
7655 	if (buf == NULL)
7656 		return (ENOMEM);
7657 
7658 	rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf, dparams->size);
7659 	if (rc != 0)
7660 		goto done;
7661 
7662 	nentries = dparams->size / sizeof(struct fw_devlog_e);
7663 	for (i = 0; i < nentries; i++) {
7664 		e = &buf[i];
7665 
7666 		if (e->timestamp == 0)
7667 			break;	/* end */
7668 
7669 		e->timestamp = be64toh(e->timestamp);
7670 		e->seqno = be32toh(e->seqno);
7671 		for (j = 0; j < 8; j++)
7672 			e->params[j] = be32toh(e->params[j]);
7673 
7674 		if (e->timestamp < ftstamp) {
7675 			ftstamp = e->timestamp;
7676 			first = i;
7677 		}
7678 	}
7679 
7680 	if (buf[first].timestamp == 0)
7681 		goto done;	/* nothing in the log */
7682 
7683 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
7684 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
7685 
7686 	i = first;
7687 	do {
7688 		e = &buf[i];
7689 		if (e->timestamp == 0)
7690 			break;	/* end */
7691 
7692 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
7693 		    e->seqno, e->timestamp,
7694 		    (e->level < nitems(devlog_level_strings) ?
7695 			devlog_level_strings[e->level] : "UNKNOWN"),
7696 		    (e->facility < nitems(devlog_facility_strings) ?
7697 			devlog_facility_strings[e->facility] : "UNKNOWN"));
7698 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
7699 		    e->params[2], e->params[3], e->params[4],
7700 		    e->params[5], e->params[6], e->params[7]);
7701 
7702 		if (++i == nentries)
7703 			i = 0;
7704 	} while (i != first);
7705 done:
7706 	free(buf, M_CXGBE);
7707 	return (rc);
7708 }
7709 
7710 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)7711 sysctl_devlog(SYSCTL_HANDLER_ARGS)
7712 {
7713 	struct adapter *sc = arg1;
7714 	int rc;
7715 	struct sbuf *sb;
7716 
7717 	rc = sysctl_wire_old_buffer(req, 0);
7718 	if (rc != 0)
7719 		return (rc);
7720 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
7721 	if (sb == NULL)
7722 		return (ENOMEM);
7723 
7724 	rc = sbuf_devlog(sc, sb, M_WAITOK);
7725 	if (rc == 0)
7726 		rc = sbuf_finish(sb);
7727 	sbuf_delete(sb);
7728 	return (rc);
7729 }
7730 
7731 void
t4_os_dump_devlog(struct adapter * sc)7732 t4_os_dump_devlog(struct adapter *sc)
7733 {
7734 	int rc;
7735 	struct sbuf sb;
7736 
7737 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb)
7738 		return;
7739 	rc = sbuf_devlog(sc, &sb, M_NOWAIT);
7740 	if (rc == 0) {
7741 		rc = sbuf_finish(&sb);
7742 		if (rc == 0) {
7743 			log(LOG_DEBUG, "%s: device log follows.\n%s",
7744 		    		device_get_nameunit(sc->dev), sbuf_data(&sb));
7745 		}
7746 	}
7747 	sbuf_delete(&sb);
7748 }
7749 
7750 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)7751 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
7752 {
7753 	struct adapter *sc = arg1;
7754 	struct sbuf *sb;
7755 	int rc;
7756 	struct tp_fcoe_stats stats[MAX_NCHAN];
7757 	int i, nchan = sc->chip_params->nchan;
7758 
7759 	rc = sysctl_wire_old_buffer(req, 0);
7760 	if (rc != 0)
7761 		return (rc);
7762 
7763 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
7764 	if (sb == NULL)
7765 		return (ENOMEM);
7766 
7767 	for (i = 0; i < nchan; i++)
7768 		t4_get_fcoe_stats(sc, i, &stats[i], 1);
7769 
7770 	if (nchan > 2) {
7771 		sbuf_printf(sb, "                   channel 0        channel 1"
7772 		    "        channel 2        channel 3");
7773 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
7774 		    stats[0].octets_ddp, stats[1].octets_ddp,
7775 		    stats[2].octets_ddp, stats[3].octets_ddp);
7776 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
7777 		    stats[0].frames_ddp, stats[1].frames_ddp,
7778 		    stats[2].frames_ddp, stats[3].frames_ddp);
7779 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
7780 		    stats[0].frames_drop, stats[1].frames_drop,
7781 		    stats[2].frames_drop, stats[3].frames_drop);
7782 	} else {
7783 		sbuf_printf(sb, "                   channel 0        channel 1");
7784 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
7785 		    stats[0].octets_ddp, stats[1].octets_ddp);
7786 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
7787 		    stats[0].frames_ddp, stats[1].frames_ddp);
7788 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
7789 		    stats[0].frames_drop, stats[1].frames_drop);
7790 	}
7791 
7792 	rc = sbuf_finish(sb);
7793 	sbuf_delete(sb);
7794 
7795 	return (rc);
7796 }
7797 
7798 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)7799 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
7800 {
7801 	struct adapter *sc = arg1;
7802 	struct sbuf *sb;
7803 	int rc, i;
7804 	unsigned int map, kbps, ipg, mode;
7805 	unsigned int pace_tab[NTX_SCHED];
7806 
7807 	rc = sysctl_wire_old_buffer(req, 0);
7808 	if (rc != 0)
7809 		return (rc);
7810 
7811 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
7812 	if (sb == NULL)
7813 		return (ENOMEM);
7814 
7815 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
7816 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
7817 	t4_read_pace_tbl(sc, pace_tab);
7818 
7819 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
7820 	    "Class IPG (0.1 ns)   Flow IPG (us)");
7821 
7822 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
7823 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
7824 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
7825 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
7826 		if (kbps)
7827 			sbuf_printf(sb, "%9u     ", kbps);
7828 		else
7829 			sbuf_printf(sb, " disabled     ");
7830 
7831 		if (ipg)
7832 			sbuf_printf(sb, "%13u        ", ipg);
7833 		else
7834 			sbuf_printf(sb, "     disabled        ");
7835 
7836 		if (pace_tab[i])
7837 			sbuf_printf(sb, "%10u", pace_tab[i]);
7838 		else
7839 			sbuf_printf(sb, "  disabled");
7840 	}
7841 
7842 	rc = sbuf_finish(sb);
7843 	sbuf_delete(sb);
7844 
7845 	return (rc);
7846 }
7847 
7848 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)7849 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
7850 {
7851 	struct adapter *sc = arg1;
7852 	struct sbuf *sb;
7853 	int rc, i, j;
7854 	uint64_t *p0, *p1;
7855 	struct lb_port_stats s[2];
7856 	static const char *stat_name[] = {
7857 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
7858 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
7859 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
7860 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
7861 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
7862 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
7863 		"BG2FramesTrunc:", "BG3FramesTrunc:"
7864 	};
7865 
7866 	rc = sysctl_wire_old_buffer(req, 0);
7867 	if (rc != 0)
7868 		return (rc);
7869 
7870 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
7871 	if (sb == NULL)
7872 		return (ENOMEM);
7873 
7874 	memset(s, 0, sizeof(s));
7875 
7876 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
7877 		t4_get_lb_stats(sc, i, &s[0]);
7878 		t4_get_lb_stats(sc, i + 1, &s[1]);
7879 
7880 		p0 = &s[0].octets;
7881 		p1 = &s[1].octets;
7882 		sbuf_printf(sb, "%s                       Loopback %u"
7883 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
7884 
7885 		for (j = 0; j < nitems(stat_name); j++)
7886 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
7887 				   *p0++, *p1++);
7888 	}
7889 
7890 	rc = sbuf_finish(sb);
7891 	sbuf_delete(sb);
7892 
7893 	return (rc);
7894 }
7895 
7896 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)7897 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
7898 {
7899 	int rc = 0;
7900 	struct port_info *pi = arg1;
7901 	struct link_config *lc = &pi->link_cfg;
7902 	struct sbuf *sb;
7903 
7904 	rc = sysctl_wire_old_buffer(req, 0);
7905 	if (rc != 0)
7906 		return(rc);
7907 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
7908 	if (sb == NULL)
7909 		return (ENOMEM);
7910 
7911 	if (lc->link_ok || lc->link_down_rc == 255)
7912 		sbuf_printf(sb, "n/a");
7913 	else
7914 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
7915 
7916 	rc = sbuf_finish(sb);
7917 	sbuf_delete(sb);
7918 
7919 	return (rc);
7920 }
7921 
7922 struct mem_desc {
7923 	unsigned int base;
7924 	unsigned int limit;
7925 	unsigned int idx;
7926 };
7927 
7928 static int
mem_desc_cmp(const void * a,const void * b)7929 mem_desc_cmp(const void *a, const void *b)
7930 {
7931 	return ((const struct mem_desc *)a)->base -
7932 	       ((const struct mem_desc *)b)->base;
7933 }
7934 
7935 static void
mem_region_show(struct sbuf * sb,const char * name,unsigned int from,unsigned int to)7936 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
7937     unsigned int to)
7938 {
7939 	unsigned int size;
7940 
7941 	if (from == to)
7942 		return;
7943 
7944 	size = to - from + 1;
7945 	if (size == 0)
7946 		return;
7947 
7948 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
7949 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
7950 }
7951 
7952 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)7953 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
7954 {
7955 	struct adapter *sc = arg1;
7956 	struct sbuf *sb;
7957 	int rc, i, n;
7958 	uint32_t lo, hi, used, alloc;
7959 	static const char *memory[] = {"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:"};
7960 	static const char *region[] = {
7961 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
7962 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
7963 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
7964 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
7965 		"RQUDP region:", "PBL region:", "TXPBL region:",
7966 		"DBVFIFO region:", "ULPRX state:", "ULPTX state:",
7967 		"On-chip queues:", "TLS keys:",
7968 	};
7969 	struct mem_desc avail[4];
7970 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
7971 	struct mem_desc *md = mem;
7972 
7973 	rc = sysctl_wire_old_buffer(req, 0);
7974 	if (rc != 0)
7975 		return (rc);
7976 
7977 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
7978 	if (sb == NULL)
7979 		return (ENOMEM);
7980 
7981 	for (i = 0; i < nitems(mem); i++) {
7982 		mem[i].limit = 0;
7983 		mem[i].idx = i;
7984 	}
7985 
7986 	/* Find and sort the populated memory ranges */
7987 	i = 0;
7988 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
7989 	if (lo & F_EDRAM0_ENABLE) {
7990 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
7991 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
7992 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
7993 		avail[i].idx = 0;
7994 		i++;
7995 	}
7996 	if (lo & F_EDRAM1_ENABLE) {
7997 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
7998 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
7999 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
8000 		avail[i].idx = 1;
8001 		i++;
8002 	}
8003 	if (lo & F_EXT_MEM_ENABLE) {
8004 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
8005 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
8006 		avail[i].limit = avail[i].base +
8007 		    (G_EXT_MEM_SIZE(hi) << 20);
8008 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
8009 		i++;
8010 	}
8011 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
8012 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
8013 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
8014 		avail[i].limit = avail[i].base +
8015 		    (G_EXT_MEM1_SIZE(hi) << 20);
8016 		avail[i].idx = 4;
8017 		i++;
8018 	}
8019 	if (!i)                                    /* no memory available */
8020 		return 0;
8021 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
8022 
8023 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
8024 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
8025 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
8026 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
8027 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
8028 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
8029 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
8030 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
8031 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
8032 
8033 	/* the next few have explicit upper bounds */
8034 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
8035 	md->limit = md->base - 1 +
8036 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
8037 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
8038 	md++;
8039 
8040 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
8041 	md->limit = md->base - 1 +
8042 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
8043 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
8044 	md++;
8045 
8046 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
8047 		if (chip_id(sc) <= CHELSIO_T5)
8048 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
8049 		else
8050 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
8051 		md->limit = 0;
8052 	} else {
8053 		md->base = 0;
8054 		md->idx = nitems(region);  /* hide it */
8055 	}
8056 	md++;
8057 
8058 #define ulp_region(reg) \
8059 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
8060 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
8061 
8062 	ulp_region(RX_ISCSI);
8063 	ulp_region(RX_TDDP);
8064 	ulp_region(TX_TPT);
8065 	ulp_region(RX_STAG);
8066 	ulp_region(RX_RQ);
8067 	ulp_region(RX_RQUDP);
8068 	ulp_region(RX_PBL);
8069 	ulp_region(TX_PBL);
8070 #undef ulp_region
8071 
8072 	md->base = 0;
8073 	md->idx = nitems(region);
8074 	if (!is_t4(sc)) {
8075 		uint32_t size = 0;
8076 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
8077 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
8078 
8079 		if (is_t5(sc)) {
8080 			if (sge_ctrl & F_VFIFO_ENABLE)
8081 				size = G_DBVFIFO_SIZE(fifo_size);
8082 		} else
8083 			size = G_T6_DBVFIFO_SIZE(fifo_size);
8084 
8085 		if (size) {
8086 			md->base = G_BASEADDR(t4_read_reg(sc,
8087 			    A_SGE_DBVFIFO_BADDR));
8088 			md->limit = md->base + (size << 2) - 1;
8089 		}
8090 	}
8091 	md++;
8092 
8093 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
8094 	md->limit = 0;
8095 	md++;
8096 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
8097 	md->limit = 0;
8098 	md++;
8099 
8100 	md->base = sc->vres.ocq.start;
8101 	if (sc->vres.ocq.size)
8102 		md->limit = md->base + sc->vres.ocq.size - 1;
8103 	else
8104 		md->idx = nitems(region);  /* hide it */
8105 	md++;
8106 
8107 	md->base = sc->vres.key.start;
8108 	if (sc->vres.key.size)
8109 		md->limit = md->base + sc->vres.key.size - 1;
8110 	else
8111 		md->idx = nitems(region);  /* hide it */
8112 	md++;
8113 
8114 	/* add any address-space holes, there can be up to 3 */
8115 	for (n = 0; n < i - 1; n++)
8116 		if (avail[n].limit < avail[n + 1].base)
8117 			(md++)->base = avail[n].limit;
8118 	if (avail[n].limit)
8119 		(md++)->base = avail[n].limit;
8120 
8121 	n = md - mem;
8122 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
8123 
8124 	for (lo = 0; lo < i; lo++)
8125 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
8126 				avail[lo].limit - 1);
8127 
8128 	sbuf_printf(sb, "\n");
8129 	for (i = 0; i < n; i++) {
8130 		if (mem[i].idx >= nitems(region))
8131 			continue;                        /* skip holes */
8132 		if (!mem[i].limit)
8133 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
8134 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
8135 				mem[i].limit);
8136 	}
8137 
8138 	sbuf_printf(sb, "\n");
8139 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
8140 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
8141 	mem_region_show(sb, "uP RAM:", lo, hi);
8142 
8143 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
8144 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
8145 	mem_region_show(sb, "uP Extmem2:", lo, hi);
8146 
8147 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
8148 	sbuf_printf(sb, "\n%u Rx pages of size %uKiB for %u channels\n",
8149 		   G_PMRXMAXPAGE(lo),
8150 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
8151 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
8152 
8153 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
8154 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
8155 	sbuf_printf(sb, "%u Tx pages of size %u%ciB for %u channels\n",
8156 		   G_PMTXMAXPAGE(lo),
8157 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
8158 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
8159 	sbuf_printf(sb, "%u p-structs\n",
8160 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT));
8161 
8162 	for (i = 0; i < 4; i++) {
8163 		if (chip_id(sc) > CHELSIO_T5)
8164 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
8165 		else
8166 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
8167 		if (is_t5(sc)) {
8168 			used = G_T5_USED(lo);
8169 			alloc = G_T5_ALLOC(lo);
8170 		} else {
8171 			used = G_USED(lo);
8172 			alloc = G_ALLOC(lo);
8173 		}
8174 		/* For T6 these are MAC buffer groups */
8175 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
8176 		    i, used, alloc);
8177 	}
8178 	for (i = 0; i < sc->chip_params->nchan; i++) {
8179 		if (chip_id(sc) > CHELSIO_T5)
8180 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
8181 		else
8182 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
8183 		if (is_t5(sc)) {
8184 			used = G_T5_USED(lo);
8185 			alloc = G_T5_ALLOC(lo);
8186 		} else {
8187 			used = G_USED(lo);
8188 			alloc = G_ALLOC(lo);
8189 		}
8190 		/* For T6 these are MAC buffer groups */
8191 		sbuf_printf(sb,
8192 		    "\nLoopback %d using %u pages out of %u allocated",
8193 		    i, used, alloc);
8194 	}
8195 
8196 	rc = sbuf_finish(sb);
8197 	sbuf_delete(sb);
8198 
8199 	return (rc);
8200 }
8201 
8202 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)8203 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
8204 {
8205 	*mask = x | y;
8206 	y = htobe64(y);
8207 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
8208 }
8209 
8210 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)8211 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
8212 {
8213 	struct adapter *sc = arg1;
8214 	struct sbuf *sb;
8215 	int rc, i;
8216 
8217 	MPASS(chip_id(sc) <= CHELSIO_T5);
8218 
8219 	rc = sysctl_wire_old_buffer(req, 0);
8220 	if (rc != 0)
8221 		return (rc);
8222 
8223 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8224 	if (sb == NULL)
8225 		return (ENOMEM);
8226 
8227 	sbuf_printf(sb,
8228 	    "Idx  Ethernet address     Mask     Vld Ports PF"
8229 	    "  VF              Replication             P0 P1 P2 P3  ML");
8230 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
8231 		uint64_t tcamx, tcamy, mask;
8232 		uint32_t cls_lo, cls_hi;
8233 		uint8_t addr[ETHER_ADDR_LEN];
8234 
8235 		tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
8236 		tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
8237 		if (tcamx & tcamy)
8238 			continue;
8239 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
8240 		cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
8241 		cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
8242 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
8243 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
8244 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
8245 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
8246 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
8247 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
8248 
8249 		if (cls_lo & F_REPLICATE) {
8250 			struct fw_ldst_cmd ldst_cmd;
8251 
8252 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
8253 			ldst_cmd.op_to_addrspace =
8254 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
8255 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
8256 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
8257 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
8258 			ldst_cmd.u.mps.rplc.fid_idx =
8259 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
8260 				V_FW_LDST_CMD_IDX(i));
8261 
8262 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8263 			    "t4mps");
8264 			if (rc)
8265 				break;
8266 			rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
8267 			    sizeof(ldst_cmd), &ldst_cmd);
8268 			end_synchronized_op(sc, 0);
8269 
8270 			if (rc != 0) {
8271 				sbuf_printf(sb, "%36d", rc);
8272 				rc = 0;
8273 			} else {
8274 				sbuf_printf(sb, " %08x %08x %08x %08x",
8275 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
8276 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
8277 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
8278 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
8279 			}
8280 		} else
8281 			sbuf_printf(sb, "%36s", "");
8282 
8283 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
8284 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
8285 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
8286 	}
8287 
8288 	if (rc)
8289 		(void) sbuf_finish(sb);
8290 	else
8291 		rc = sbuf_finish(sb);
8292 	sbuf_delete(sb);
8293 
8294 	return (rc);
8295 }
8296 
8297 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)8298 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
8299 {
8300 	struct adapter *sc = arg1;
8301 	struct sbuf *sb;
8302 	int rc, i;
8303 
8304 	MPASS(chip_id(sc) > CHELSIO_T5);
8305 
8306 	rc = sysctl_wire_old_buffer(req, 0);
8307 	if (rc != 0)
8308 		return (rc);
8309 
8310 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8311 	if (sb == NULL)
8312 		return (ENOMEM);
8313 
8314 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
8315 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
8316 	    "                           Replication"
8317 	    "                                    P0 P1 P2 P3  ML\n");
8318 
8319 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
8320 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
8321 		uint16_t ivlan;
8322 		uint64_t tcamx, tcamy, val, mask;
8323 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
8324 		uint8_t addr[ETHER_ADDR_LEN];
8325 
8326 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
8327 		if (i < 256)
8328 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
8329 		else
8330 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
8331 		t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
8332 		val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
8333 		tcamy = G_DMACH(val) << 32;
8334 		tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
8335 		data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
8336 		lookup_type = G_DATALKPTYPE(data2);
8337 		port_num = G_DATAPORTNUM(data2);
8338 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
8339 			/* Inner header VNI */
8340 			vniy = ((data2 & F_DATAVIDH2) << 23) |
8341 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
8342 			dip_hit = data2 & F_DATADIPHIT;
8343 			vlan_vld = 0;
8344 		} else {
8345 			vniy = 0;
8346 			dip_hit = 0;
8347 			vlan_vld = data2 & F_DATAVIDH2;
8348 			ivlan = G_VIDL(val);
8349 		}
8350 
8351 		ctl |= V_CTLXYBITSEL(1);
8352 		t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
8353 		val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
8354 		tcamx = G_DMACH(val) << 32;
8355 		tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
8356 		data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
8357 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
8358 			/* Inner header VNI mask */
8359 			vnix = ((data2 & F_DATAVIDH2) << 23) |
8360 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
8361 		} else
8362 			vnix = 0;
8363 
8364 		if (tcamx & tcamy)
8365 			continue;
8366 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
8367 
8368 		cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
8369 		cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
8370 
8371 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
8372 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
8373 			    "%012jx %06x %06x    -    -   %3c"
8374 			    "      'I'  %4x   %3c   %#x%4u%4d", i, addr[0],
8375 			    addr[1], addr[2], addr[3], addr[4], addr[5],
8376 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
8377 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
8378 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
8379 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
8380 		} else {
8381 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
8382 			    "%012jx    -       -   ", i, addr[0], addr[1],
8383 			    addr[2], addr[3], addr[4], addr[5],
8384 			    (uintmax_t)mask);
8385 
8386 			if (vlan_vld)
8387 				sbuf_printf(sb, "%4u   Y     ", ivlan);
8388 			else
8389 				sbuf_printf(sb, "  -    N     ");
8390 
8391 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
8392 			    lookup_type ? 'I' : 'O', port_num,
8393 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
8394 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
8395 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
8396 		}
8397 
8398 
8399 		if (cls_lo & F_T6_REPLICATE) {
8400 			struct fw_ldst_cmd ldst_cmd;
8401 
8402 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
8403 			ldst_cmd.op_to_addrspace =
8404 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
8405 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
8406 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
8407 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
8408 			ldst_cmd.u.mps.rplc.fid_idx =
8409 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
8410 				V_FW_LDST_CMD_IDX(i));
8411 
8412 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8413 			    "t6mps");
8414 			if (rc)
8415 				break;
8416 			rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
8417 			    sizeof(ldst_cmd), &ldst_cmd);
8418 			end_synchronized_op(sc, 0);
8419 
8420 			if (rc != 0) {
8421 				sbuf_printf(sb, "%72d", rc);
8422 				rc = 0;
8423 			} else {
8424 				sbuf_printf(sb, " %08x %08x %08x %08x"
8425 				    " %08x %08x %08x %08x",
8426 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
8427 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
8428 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
8429 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
8430 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
8431 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
8432 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
8433 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
8434 			}
8435 		} else
8436 			sbuf_printf(sb, "%72s", "");
8437 
8438 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
8439 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
8440 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
8441 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
8442 	}
8443 
8444 	if (rc)
8445 		(void) sbuf_finish(sb);
8446 	else
8447 		rc = sbuf_finish(sb);
8448 	sbuf_delete(sb);
8449 
8450 	return (rc);
8451 }
8452 
8453 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)8454 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
8455 {
8456 	struct adapter *sc = arg1;
8457 	struct sbuf *sb;
8458 	int rc;
8459 	uint16_t mtus[NMTUS];
8460 
8461 	rc = sysctl_wire_old_buffer(req, 0);
8462 	if (rc != 0)
8463 		return (rc);
8464 
8465 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
8466 	if (sb == NULL)
8467 		return (ENOMEM);
8468 
8469 	t4_read_mtu_tbl(sc, mtus, NULL);
8470 
8471 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
8472 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
8473 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
8474 	    mtus[14], mtus[15]);
8475 
8476 	rc = sbuf_finish(sb);
8477 	sbuf_delete(sb);
8478 
8479 	return (rc);
8480 }
8481 
8482 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)8483 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
8484 {
8485 	struct adapter *sc = arg1;
8486 	struct sbuf *sb;
8487 	int rc, i;
8488 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
8489 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
8490 	static const char *tx_stats[MAX_PM_NSTATS] = {
8491 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
8492 		"Tx FIFO wait", NULL, "Tx latency"
8493 	};
8494 	static const char *rx_stats[MAX_PM_NSTATS] = {
8495 		"Read:", "Write bypass:", "Write mem:", "Flush:",
8496 		"Rx FIFO wait", NULL, "Rx latency"
8497 	};
8498 
8499 	rc = sysctl_wire_old_buffer(req, 0);
8500 	if (rc != 0)
8501 		return (rc);
8502 
8503 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
8504 	if (sb == NULL)
8505 		return (ENOMEM);
8506 
8507 	t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
8508 	t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
8509 
8510 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
8511 	for (i = 0; i < 4; i++) {
8512 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
8513 		    tx_cyc[i]);
8514 	}
8515 
8516 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
8517 	for (i = 0; i < 4; i++) {
8518 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
8519 		    rx_cyc[i]);
8520 	}
8521 
8522 	if (chip_id(sc) > CHELSIO_T5) {
8523 		sbuf_printf(sb,
8524 		    "\n              Total wait      Total occupancy");
8525 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
8526 		    tx_cyc[i]);
8527 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
8528 		    rx_cyc[i]);
8529 
8530 		i += 2;
8531 		MPASS(i < nitems(tx_stats));
8532 
8533 		sbuf_printf(sb,
8534 		    "\n                   Reads           Total wait");
8535 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
8536 		    tx_cyc[i]);
8537 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
8538 		    rx_cyc[i]);
8539 	}
8540 
8541 	rc = sbuf_finish(sb);
8542 	sbuf_delete(sb);
8543 
8544 	return (rc);
8545 }
8546 
8547 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)8548 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
8549 {
8550 	struct adapter *sc = arg1;
8551 	struct sbuf *sb;
8552 	int rc;
8553 	struct tp_rdma_stats stats;
8554 
8555 	rc = sysctl_wire_old_buffer(req, 0);
8556 	if (rc != 0)
8557 		return (rc);
8558 
8559 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
8560 	if (sb == NULL)
8561 		return (ENOMEM);
8562 
8563 	mtx_lock(&sc->reg_lock);
8564 	t4_tp_get_rdma_stats(sc, &stats, 0);
8565 	mtx_unlock(&sc->reg_lock);
8566 
8567 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
8568 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
8569 
8570 	rc = sbuf_finish(sb);
8571 	sbuf_delete(sb);
8572 
8573 	return (rc);
8574 }
8575 
8576 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)8577 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
8578 {
8579 	struct adapter *sc = arg1;
8580 	struct sbuf *sb;
8581 	int rc;
8582 	struct tp_tcp_stats v4, v6;
8583 
8584 	rc = sysctl_wire_old_buffer(req, 0);
8585 	if (rc != 0)
8586 		return (rc);
8587 
8588 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
8589 	if (sb == NULL)
8590 		return (ENOMEM);
8591 
8592 	mtx_lock(&sc->reg_lock);
8593 	t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
8594 	mtx_unlock(&sc->reg_lock);
8595 
8596 	sbuf_printf(sb,
8597 	    "                                IP                 IPv6\n");
8598 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
8599 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
8600 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
8601 	    v4.tcp_in_segs, v6.tcp_in_segs);
8602 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
8603 	    v4.tcp_out_segs, v6.tcp_out_segs);
8604 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
8605 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
8606 
8607 	rc = sbuf_finish(sb);
8608 	sbuf_delete(sb);
8609 
8610 	return (rc);
8611 }
8612 
8613 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)8614 sysctl_tids(SYSCTL_HANDLER_ARGS)
8615 {
8616 	struct adapter *sc = arg1;
8617 	struct sbuf *sb;
8618 	int rc;
8619 	struct tid_info *t = &sc->tids;
8620 
8621 	rc = sysctl_wire_old_buffer(req, 0);
8622 	if (rc != 0)
8623 		return (rc);
8624 
8625 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
8626 	if (sb == NULL)
8627 		return (ENOMEM);
8628 
8629 	if (t->natids) {
8630 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
8631 		    t->atids_in_use);
8632 	}
8633 
8634 	if (t->nhpftids) {
8635 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
8636 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
8637 	}
8638 
8639 	if (t->ntids) {
8640 		sbuf_printf(sb, "TID range: ");
8641 		if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
8642 			uint32_t b, hb;
8643 
8644 			if (chip_id(sc) <= CHELSIO_T5) {
8645 				b = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
8646 				hb = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
8647 			} else {
8648 				b = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
8649 				hb = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
8650 			}
8651 
8652 			if (b)
8653 				sbuf_printf(sb, "%u-%u, ", t->tid_base, b - 1);
8654 			sbuf_printf(sb, "%u-%u", hb, t->ntids - 1);
8655 		} else
8656 			sbuf_printf(sb, "%u-%u", t->tid_base, t->ntids - 1);
8657 		sbuf_printf(sb, ", in use: %u\n",
8658 		    atomic_load_acq_int(&t->tids_in_use));
8659 	}
8660 
8661 	if (t->nstids) {
8662 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
8663 		    t->stid_base + t->nstids - 1, t->stids_in_use);
8664 	}
8665 
8666 	if (t->nftids) {
8667 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
8668 		    t->ftid_end, t->ftids_in_use);
8669 	}
8670 
8671 	if (t->netids) {
8672 		sbuf_printf(sb, "ETID range: %u-%u\n", t->etid_base,
8673 		    t->etid_base + t->netids - 1);
8674 	}
8675 
8676 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users",
8677 	    t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4),
8678 	    t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6));
8679 
8680 	rc = sbuf_finish(sb);
8681 	sbuf_delete(sb);
8682 
8683 	return (rc);
8684 }
8685 
8686 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)8687 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
8688 {
8689 	struct adapter *sc = arg1;
8690 	struct sbuf *sb;
8691 	int rc;
8692 	struct tp_err_stats stats;
8693 
8694 	rc = sysctl_wire_old_buffer(req, 0);
8695 	if (rc != 0)
8696 		return (rc);
8697 
8698 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
8699 	if (sb == NULL)
8700 		return (ENOMEM);
8701 
8702 	mtx_lock(&sc->reg_lock);
8703 	t4_tp_get_err_stats(sc, &stats, 0);
8704 	mtx_unlock(&sc->reg_lock);
8705 
8706 	if (sc->chip_params->nchan > 2) {
8707 		sbuf_printf(sb, "                 channel 0  channel 1"
8708 		    "  channel 2  channel 3\n");
8709 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
8710 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
8711 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
8712 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
8713 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
8714 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
8715 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
8716 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
8717 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
8718 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
8719 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
8720 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
8721 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
8722 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
8723 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
8724 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
8725 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
8726 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
8727 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
8728 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
8729 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
8730 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
8731 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
8732 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
8733 	} else {
8734 		sbuf_printf(sb, "                 channel 0  channel 1\n");
8735 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
8736 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
8737 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
8738 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
8739 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
8740 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
8741 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
8742 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
8743 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
8744 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
8745 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
8746 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
8747 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
8748 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
8749 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
8750 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
8751 	}
8752 
8753 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
8754 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
8755 
8756 	rc = sbuf_finish(sb);
8757 	sbuf_delete(sb);
8758 
8759 	return (rc);
8760 }
8761 
8762 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)8763 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
8764 {
8765 	struct adapter *sc = arg1;
8766 	struct tp_params *tpp = &sc->params.tp;
8767 	u_int mask;
8768 	int rc;
8769 
8770 	mask = tpp->la_mask >> 16;
8771 	rc = sysctl_handle_int(oidp, &mask, 0, req);
8772 	if (rc != 0 || req->newptr == NULL)
8773 		return (rc);
8774 	if (mask > 0xffff)
8775 		return (EINVAL);
8776 	tpp->la_mask = mask << 16;
8777 	t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U, tpp->la_mask);
8778 
8779 	return (0);
8780 }
8781 
8782 struct field_desc {
8783 	const char *name;
8784 	u_int start;
8785 	u_int width;
8786 };
8787 
8788 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)8789 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
8790 {
8791 	char buf[32];
8792 	int line_size = 0;
8793 
8794 	while (f->name) {
8795 		uint64_t mask = (1ULL << f->width) - 1;
8796 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
8797 		    ((uintmax_t)v >> f->start) & mask);
8798 
8799 		if (line_size + len >= 79) {
8800 			line_size = 8;
8801 			sbuf_printf(sb, "\n        ");
8802 		}
8803 		sbuf_printf(sb, "%s ", buf);
8804 		line_size += len + 1;
8805 		f++;
8806 	}
8807 	sbuf_printf(sb, "\n");
8808 }
8809 
8810 static const struct field_desc tp_la0[] = {
8811 	{ "RcfOpCodeOut", 60, 4 },
8812 	{ "State", 56, 4 },
8813 	{ "WcfState", 52, 4 },
8814 	{ "RcfOpcSrcOut", 50, 2 },
8815 	{ "CRxError", 49, 1 },
8816 	{ "ERxError", 48, 1 },
8817 	{ "SanityFailed", 47, 1 },
8818 	{ "SpuriousMsg", 46, 1 },
8819 	{ "FlushInputMsg", 45, 1 },
8820 	{ "FlushInputCpl", 44, 1 },
8821 	{ "RssUpBit", 43, 1 },
8822 	{ "RssFilterHit", 42, 1 },
8823 	{ "Tid", 32, 10 },
8824 	{ "InitTcb", 31, 1 },
8825 	{ "LineNumber", 24, 7 },
8826 	{ "Emsg", 23, 1 },
8827 	{ "EdataOut", 22, 1 },
8828 	{ "Cmsg", 21, 1 },
8829 	{ "CdataOut", 20, 1 },
8830 	{ "EreadPdu", 19, 1 },
8831 	{ "CreadPdu", 18, 1 },
8832 	{ "TunnelPkt", 17, 1 },
8833 	{ "RcfPeerFin", 16, 1 },
8834 	{ "RcfReasonOut", 12, 4 },
8835 	{ "TxCchannel", 10, 2 },
8836 	{ "RcfTxChannel", 8, 2 },
8837 	{ "RxEchannel", 6, 2 },
8838 	{ "RcfRxChannel", 5, 1 },
8839 	{ "RcfDataOutSrdy", 4, 1 },
8840 	{ "RxDvld", 3, 1 },
8841 	{ "RxOoDvld", 2, 1 },
8842 	{ "RxCongestion", 1, 1 },
8843 	{ "TxCongestion", 0, 1 },
8844 	{ NULL }
8845 };
8846 
8847 static const struct field_desc tp_la1[] = {
8848 	{ "CplCmdIn", 56, 8 },
8849 	{ "CplCmdOut", 48, 8 },
8850 	{ "ESynOut", 47, 1 },
8851 	{ "EAckOut", 46, 1 },
8852 	{ "EFinOut", 45, 1 },
8853 	{ "ERstOut", 44, 1 },
8854 	{ "SynIn", 43, 1 },
8855 	{ "AckIn", 42, 1 },
8856 	{ "FinIn", 41, 1 },
8857 	{ "RstIn", 40, 1 },
8858 	{ "DataIn", 39, 1 },
8859 	{ "DataInVld", 38, 1 },
8860 	{ "PadIn", 37, 1 },
8861 	{ "RxBufEmpty", 36, 1 },
8862 	{ "RxDdp", 35, 1 },
8863 	{ "RxFbCongestion", 34, 1 },
8864 	{ "TxFbCongestion", 33, 1 },
8865 	{ "TxPktSumSrdy", 32, 1 },
8866 	{ "RcfUlpType", 28, 4 },
8867 	{ "Eread", 27, 1 },
8868 	{ "Ebypass", 26, 1 },
8869 	{ "Esave", 25, 1 },
8870 	{ "Static0", 24, 1 },
8871 	{ "Cread", 23, 1 },
8872 	{ "Cbypass", 22, 1 },
8873 	{ "Csave", 21, 1 },
8874 	{ "CPktOut", 20, 1 },
8875 	{ "RxPagePoolFull", 18, 2 },
8876 	{ "RxLpbkPkt", 17, 1 },
8877 	{ "TxLpbkPkt", 16, 1 },
8878 	{ "RxVfValid", 15, 1 },
8879 	{ "SynLearned", 14, 1 },
8880 	{ "SetDelEntry", 13, 1 },
8881 	{ "SetInvEntry", 12, 1 },
8882 	{ "CpcmdDvld", 11, 1 },
8883 	{ "CpcmdSave", 10, 1 },
8884 	{ "RxPstructsFull", 8, 2 },
8885 	{ "EpcmdDvld", 7, 1 },
8886 	{ "EpcmdFlush", 6, 1 },
8887 	{ "EpcmdTrimPrefix", 5, 1 },
8888 	{ "EpcmdTrimPostfix", 4, 1 },
8889 	{ "ERssIp4Pkt", 3, 1 },
8890 	{ "ERssIp6Pkt", 2, 1 },
8891 	{ "ERssTcpUdpPkt", 1, 1 },
8892 	{ "ERssFceFipPkt", 0, 1 },
8893 	{ NULL }
8894 };
8895 
8896 static const struct field_desc tp_la2[] = {
8897 	{ "CplCmdIn", 56, 8 },
8898 	{ "MpsVfVld", 55, 1 },
8899 	{ "MpsPf", 52, 3 },
8900 	{ "MpsVf", 44, 8 },
8901 	{ "SynIn", 43, 1 },
8902 	{ "AckIn", 42, 1 },
8903 	{ "FinIn", 41, 1 },
8904 	{ "RstIn", 40, 1 },
8905 	{ "DataIn", 39, 1 },
8906 	{ "DataInVld", 38, 1 },
8907 	{ "PadIn", 37, 1 },
8908 	{ "RxBufEmpty", 36, 1 },
8909 	{ "RxDdp", 35, 1 },
8910 	{ "RxFbCongestion", 34, 1 },
8911 	{ "TxFbCongestion", 33, 1 },
8912 	{ "TxPktSumSrdy", 32, 1 },
8913 	{ "RcfUlpType", 28, 4 },
8914 	{ "Eread", 27, 1 },
8915 	{ "Ebypass", 26, 1 },
8916 	{ "Esave", 25, 1 },
8917 	{ "Static0", 24, 1 },
8918 	{ "Cread", 23, 1 },
8919 	{ "Cbypass", 22, 1 },
8920 	{ "Csave", 21, 1 },
8921 	{ "CPktOut", 20, 1 },
8922 	{ "RxPagePoolFull", 18, 2 },
8923 	{ "RxLpbkPkt", 17, 1 },
8924 	{ "TxLpbkPkt", 16, 1 },
8925 	{ "RxVfValid", 15, 1 },
8926 	{ "SynLearned", 14, 1 },
8927 	{ "SetDelEntry", 13, 1 },
8928 	{ "SetInvEntry", 12, 1 },
8929 	{ "CpcmdDvld", 11, 1 },
8930 	{ "CpcmdSave", 10, 1 },
8931 	{ "RxPstructsFull", 8, 2 },
8932 	{ "EpcmdDvld", 7, 1 },
8933 	{ "EpcmdFlush", 6, 1 },
8934 	{ "EpcmdTrimPrefix", 5, 1 },
8935 	{ "EpcmdTrimPostfix", 4, 1 },
8936 	{ "ERssIp4Pkt", 3, 1 },
8937 	{ "ERssIp6Pkt", 2, 1 },
8938 	{ "ERssTcpUdpPkt", 1, 1 },
8939 	{ "ERssFceFipPkt", 0, 1 },
8940 	{ NULL }
8941 };
8942 
8943 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)8944 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
8945 {
8946 
8947 	field_desc_show(sb, *p, tp_la0);
8948 }
8949 
8950 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)8951 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
8952 {
8953 
8954 	if (idx)
8955 		sbuf_printf(sb, "\n");
8956 	field_desc_show(sb, p[0], tp_la0);
8957 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
8958 		field_desc_show(sb, p[1], tp_la0);
8959 }
8960 
8961 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)8962 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
8963 {
8964 
8965 	if (idx)
8966 		sbuf_printf(sb, "\n");
8967 	field_desc_show(sb, p[0], tp_la0);
8968 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
8969 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
8970 }
8971 
8972 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)8973 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
8974 {
8975 	struct adapter *sc = arg1;
8976 	struct sbuf *sb;
8977 	uint64_t *buf, *p;
8978 	int rc;
8979 	u_int i, inc;
8980 	void (*show_func)(struct sbuf *, uint64_t *, int);
8981 
8982 	rc = sysctl_wire_old_buffer(req, 0);
8983 	if (rc != 0)
8984 		return (rc);
8985 
8986 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8987 	if (sb == NULL)
8988 		return (ENOMEM);
8989 
8990 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
8991 
8992 	t4_tp_read_la(sc, buf, NULL);
8993 	p = buf;
8994 
8995 	switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
8996 	case 2:
8997 		inc = 2;
8998 		show_func = tp_la_show2;
8999 		break;
9000 	case 3:
9001 		inc = 2;
9002 		show_func = tp_la_show3;
9003 		break;
9004 	default:
9005 		inc = 1;
9006 		show_func = tp_la_show;
9007 	}
9008 
9009 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
9010 		(*show_func)(sb, p, i);
9011 
9012 	rc = sbuf_finish(sb);
9013 	sbuf_delete(sb);
9014 	free(buf, M_CXGBE);
9015 	return (rc);
9016 }
9017 
9018 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)9019 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
9020 {
9021 	struct adapter *sc = arg1;
9022 	struct sbuf *sb;
9023 	int rc;
9024 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
9025 
9026 	rc = sysctl_wire_old_buffer(req, 0);
9027 	if (rc != 0)
9028 		return (rc);
9029 
9030 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9031 	if (sb == NULL)
9032 		return (ENOMEM);
9033 
9034 	t4_get_chan_txrate(sc, nrate, orate);
9035 
9036 	if (sc->chip_params->nchan > 2) {
9037 		sbuf_printf(sb, "              channel 0   channel 1"
9038 		    "   channel 2   channel 3\n");
9039 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
9040 		    nrate[0], nrate[1], nrate[2], nrate[3]);
9041 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
9042 		    orate[0], orate[1], orate[2], orate[3]);
9043 	} else {
9044 		sbuf_printf(sb, "              channel 0   channel 1\n");
9045 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
9046 		    nrate[0], nrate[1]);
9047 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
9048 		    orate[0], orate[1]);
9049 	}
9050 
9051 	rc = sbuf_finish(sb);
9052 	sbuf_delete(sb);
9053 
9054 	return (rc);
9055 }
9056 
9057 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)9058 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
9059 {
9060 	struct adapter *sc = arg1;
9061 	struct sbuf *sb;
9062 	uint32_t *buf, *p;
9063 	int rc, i;
9064 
9065 	rc = sysctl_wire_old_buffer(req, 0);
9066 	if (rc != 0)
9067 		return (rc);
9068 
9069 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9070 	if (sb == NULL)
9071 		return (ENOMEM);
9072 
9073 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
9074 	    M_ZERO | M_WAITOK);
9075 
9076 	t4_ulprx_read_la(sc, buf);
9077 	p = buf;
9078 
9079 	sbuf_printf(sb, "      Pcmd        Type   Message"
9080 	    "                Data");
9081 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
9082 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
9083 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
9084 	}
9085 
9086 	rc = sbuf_finish(sb);
9087 	sbuf_delete(sb);
9088 	free(buf, M_CXGBE);
9089 	return (rc);
9090 }
9091 
9092 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)9093 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
9094 {
9095 	struct adapter *sc = arg1;
9096 	struct sbuf *sb;
9097 	int rc, v;
9098 
9099 	MPASS(chip_id(sc) >= CHELSIO_T5);
9100 
9101 	rc = sysctl_wire_old_buffer(req, 0);
9102 	if (rc != 0)
9103 		return (rc);
9104 
9105 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9106 	if (sb == NULL)
9107 		return (ENOMEM);
9108 
9109 	v = t4_read_reg(sc, A_SGE_STAT_CFG);
9110 	if (G_STATSOURCE_T5(v) == 7) {
9111 		int mode;
9112 
9113 		mode = is_t5(sc) ? G_STATMODE(v) : G_T6_STATMODE(v);
9114 		if (mode == 0) {
9115 			sbuf_printf(sb, "total %d, incomplete %d",
9116 			    t4_read_reg(sc, A_SGE_STAT_TOTAL),
9117 			    t4_read_reg(sc, A_SGE_STAT_MATCH));
9118 		} else if (mode == 1) {
9119 			sbuf_printf(sb, "total %d, data overflow %d",
9120 			    t4_read_reg(sc, A_SGE_STAT_TOTAL),
9121 			    t4_read_reg(sc, A_SGE_STAT_MATCH));
9122 		} else {
9123 			sbuf_printf(sb, "unknown mode %d", mode);
9124 		}
9125 	}
9126 	rc = sbuf_finish(sb);
9127 	sbuf_delete(sb);
9128 
9129 	return (rc);
9130 }
9131 
9132 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)9133 sysctl_cpus(SYSCTL_HANDLER_ARGS)
9134 {
9135 	struct adapter *sc = arg1;
9136 	enum cpu_sets op = arg2;
9137 	cpuset_t cpuset;
9138 	struct sbuf *sb;
9139 	int i, rc;
9140 
9141 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
9142 
9143 	CPU_ZERO(&cpuset);
9144 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
9145 	if (rc != 0)
9146 		return (rc);
9147 
9148 	rc = sysctl_wire_old_buffer(req, 0);
9149 	if (rc != 0)
9150 		return (rc);
9151 
9152 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9153 	if (sb == NULL)
9154 		return (ENOMEM);
9155 
9156 	CPU_FOREACH(i)
9157 		sbuf_printf(sb, "%d ", i);
9158 	rc = sbuf_finish(sb);
9159 	sbuf_delete(sb);
9160 
9161 	return (rc);
9162 
9163 }
9164 
9165 #ifdef TCP_OFFLOAD
9166 static int
sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS)9167 sysctl_tls_rx_ports(SYSCTL_HANDLER_ARGS)
9168 {
9169 	struct adapter *sc = arg1;
9170 	int *old_ports, *new_ports;
9171 	int i, new_count, rc;
9172 
9173 	if (req->newptr == NULL && req->oldptr == NULL)
9174 		return (SYSCTL_OUT(req, NULL, imax(sc->tt.num_tls_rx_ports, 1) *
9175 		    sizeof(sc->tt.tls_rx_ports[0])));
9176 
9177 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tlsrx");
9178 	if (rc)
9179 		return (rc);
9180 
9181 	if (sc->tt.num_tls_rx_ports == 0) {
9182 		i = -1;
9183 		rc = SYSCTL_OUT(req, &i, sizeof(i));
9184 	} else
9185 		rc = SYSCTL_OUT(req, sc->tt.tls_rx_ports,
9186 		    sc->tt.num_tls_rx_ports * sizeof(sc->tt.tls_rx_ports[0]));
9187 	if (rc == 0 && req->newptr != NULL) {
9188 		new_count = req->newlen / sizeof(new_ports[0]);
9189 		new_ports = malloc(new_count * sizeof(new_ports[0]), M_CXGBE,
9190 		    M_WAITOK);
9191 		rc = SYSCTL_IN(req, new_ports, new_count *
9192 		    sizeof(new_ports[0]));
9193 		if (rc)
9194 			goto err;
9195 
9196 		/* Allow setting to a single '-1' to clear the list. */
9197 		if (new_count == 1 && new_ports[0] == -1) {
9198 			ADAPTER_LOCK(sc);
9199 			old_ports = sc->tt.tls_rx_ports;
9200 			sc->tt.tls_rx_ports = NULL;
9201 			sc->tt.num_tls_rx_ports = 0;
9202 			ADAPTER_UNLOCK(sc);
9203 			free(old_ports, M_CXGBE);
9204 		} else {
9205 			for (i = 0; i < new_count; i++) {
9206 				if (new_ports[i] < 1 ||
9207 				    new_ports[i] > IPPORT_MAX) {
9208 					rc = EINVAL;
9209 					goto err;
9210 				}
9211 			}
9212 
9213 			ADAPTER_LOCK(sc);
9214 			old_ports = sc->tt.tls_rx_ports;
9215 			sc->tt.tls_rx_ports = new_ports;
9216 			sc->tt.num_tls_rx_ports = new_count;
9217 			ADAPTER_UNLOCK(sc);
9218 			free(old_ports, M_CXGBE);
9219 			new_ports = NULL;
9220 		}
9221 	err:
9222 		free(new_ports, M_CXGBE);
9223 	}
9224 	end_synchronized_op(sc, 0);
9225 	return (rc);
9226 }
9227 
9228 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)9229 unit_conv(char *buf, size_t len, u_int val, u_int factor)
9230 {
9231 	u_int rem = val % factor;
9232 
9233 	if (rem == 0)
9234 		snprintf(buf, len, "%u", val / factor);
9235 	else {
9236 		while (rem % 10 == 0)
9237 			rem /= 10;
9238 		snprintf(buf, len, "%u.%u", val / factor, rem);
9239 	}
9240 }
9241 
9242 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)9243 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
9244 {
9245 	struct adapter *sc = arg1;
9246 	char buf[16];
9247 	u_int res, re;
9248 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
9249 
9250 	res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
9251 	switch (arg2) {
9252 	case 0:
9253 		/* timer_tick */
9254 		re = G_TIMERRESOLUTION(res);
9255 		break;
9256 	case 1:
9257 		/* TCP timestamp tick */
9258 		re = G_TIMESTAMPRESOLUTION(res);
9259 		break;
9260 	case 2:
9261 		/* DACK tick */
9262 		re = G_DELAYEDACKRESOLUTION(res);
9263 		break;
9264 	default:
9265 		return (EDOOFUS);
9266 	}
9267 
9268 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
9269 
9270 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
9271 }
9272 
9273 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)9274 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
9275 {
9276 	struct adapter *sc = arg1;
9277 	u_int res, dack_re, v;
9278 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
9279 
9280 	res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
9281 	dack_re = G_DELAYEDACKRESOLUTION(res);
9282 	v = ((cclk_ps << dack_re) / 1000000) * t4_read_reg(sc, A_TP_DACK_TIMER);
9283 
9284 	return (sysctl_handle_int(oidp, &v, 0, req));
9285 }
9286 
9287 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)9288 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
9289 {
9290 	struct adapter *sc = arg1;
9291 	int reg = arg2;
9292 	u_int tre;
9293 	u_long tp_tick_us, v;
9294 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
9295 
9296 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
9297 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
9298 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
9299 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
9300 
9301 	tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
9302 	tp_tick_us = (cclk_ps << tre) / 1000000;
9303 
9304 	if (reg == A_TP_INIT_SRTT)
9305 		v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
9306 	else
9307 		v = tp_tick_us * t4_read_reg(sc, reg);
9308 
9309 	return (sysctl_handle_long(oidp, &v, 0, req));
9310 }
9311 
9312 /*
9313  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
9314  * passed to this function.
9315  */
9316 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)9317 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
9318 {
9319 	struct adapter *sc = arg1;
9320 	int idx = arg2;
9321 	u_int v;
9322 
9323 	MPASS(idx >= 0 && idx <= 24);
9324 
9325 	v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
9326 
9327 	return (sysctl_handle_int(oidp, &v, 0, req));
9328 }
9329 
9330 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)9331 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
9332 {
9333 	struct adapter *sc = arg1;
9334 	int idx = arg2;
9335 	u_int shift, v, r;
9336 
9337 	MPASS(idx >= 0 && idx < 16);
9338 
9339 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
9340 	shift = (idx & 3) << 3;
9341 	v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
9342 
9343 	return (sysctl_handle_int(oidp, &v, 0, req));
9344 }
9345 
9346 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)9347 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
9348 {
9349 	struct vi_info *vi = arg1;
9350 	struct adapter *sc = vi->pi->adapter;
9351 	int idx, rc, i;
9352 	struct sge_ofld_rxq *ofld_rxq;
9353 	uint8_t v;
9354 
9355 	idx = vi->ofld_tmr_idx;
9356 
9357 	rc = sysctl_handle_int(oidp, &idx, 0, req);
9358 	if (rc != 0 || req->newptr == NULL)
9359 		return (rc);
9360 
9361 	if (idx < 0 || idx >= SGE_NTIMERS)
9362 		return (EINVAL);
9363 
9364 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
9365 	    "t4otmr");
9366 	if (rc)
9367 		return (rc);
9368 
9369 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
9370 	for_each_ofld_rxq(vi, i, ofld_rxq) {
9371 #ifdef atomic_store_rel_8
9372 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
9373 #else
9374 		ofld_rxq->iq.intr_params = v;
9375 #endif
9376 	}
9377 	vi->ofld_tmr_idx = idx;
9378 
9379 	end_synchronized_op(sc, LOCK_HELD);
9380 	return (0);
9381 }
9382 
9383 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)9384 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
9385 {
9386 	struct vi_info *vi = arg1;
9387 	struct adapter *sc = vi->pi->adapter;
9388 	int idx, rc;
9389 
9390 	idx = vi->ofld_pktc_idx;
9391 
9392 	rc = sysctl_handle_int(oidp, &idx, 0, req);
9393 	if (rc != 0 || req->newptr == NULL)
9394 		return (rc);
9395 
9396 	if (idx < -1 || idx >= SGE_NCOUNTERS)
9397 		return (EINVAL);
9398 
9399 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
9400 	    "t4opktc");
9401 	if (rc)
9402 		return (rc);
9403 
9404 	if (vi->flags & VI_INIT_DONE)
9405 		rc = EBUSY; /* cannot be changed once the queues are created */
9406 	else
9407 		vi->ofld_pktc_idx = idx;
9408 
9409 	end_synchronized_op(sc, LOCK_HELD);
9410 	return (rc);
9411 }
9412 #endif
9413 
9414 static int
get_sge_context(struct adapter * sc,struct t4_sge_context * cntxt)9415 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
9416 {
9417 	int rc;
9418 
9419 	if (cntxt->cid > M_CTXTQID)
9420 		return (EINVAL);
9421 
9422 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
9423 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
9424 		return (EINVAL);
9425 
9426 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
9427 	if (rc)
9428 		return (rc);
9429 
9430 	if (sc->flags & FW_OK) {
9431 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
9432 		    &cntxt->data[0]);
9433 		if (rc == 0)
9434 			goto done;
9435 	}
9436 
9437 	/*
9438 	 * Read via firmware failed or wasn't even attempted.  Read directly via
9439 	 * the backdoor.
9440 	 */
9441 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
9442 done:
9443 	end_synchronized_op(sc, 0);
9444 	return (rc);
9445 }
9446 
9447 static int
load_fw(struct adapter * sc,struct t4_data * fw)9448 load_fw(struct adapter *sc, struct t4_data *fw)
9449 {
9450 	int rc;
9451 	uint8_t *fw_data;
9452 
9453 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
9454 	if (rc)
9455 		return (rc);
9456 
9457 	/*
9458 	 * The firmware, with the sole exception of the memory parity error
9459 	 * handler, runs from memory and not flash.  It is almost always safe to
9460 	 * install a new firmware on a running system.  Just set bit 1 in
9461 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
9462 	 */
9463 	if (sc->flags & FULL_INIT_DONE &&
9464 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
9465 		rc = EBUSY;
9466 		goto done;
9467 	}
9468 
9469 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
9470 	if (fw_data == NULL) {
9471 		rc = ENOMEM;
9472 		goto done;
9473 	}
9474 
9475 	rc = copyin(fw->data, fw_data, fw->len);
9476 	if (rc == 0)
9477 		rc = -t4_load_fw(sc, fw_data, fw->len);
9478 
9479 	free(fw_data, M_CXGBE);
9480 done:
9481 	end_synchronized_op(sc, 0);
9482 	return (rc);
9483 }
9484 
9485 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)9486 load_cfg(struct adapter *sc, struct t4_data *cfg)
9487 {
9488 	int rc;
9489 	uint8_t *cfg_data = NULL;
9490 
9491 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
9492 	if (rc)
9493 		return (rc);
9494 
9495 	if (cfg->len == 0) {
9496 		/* clear */
9497 		rc = -t4_load_cfg(sc, NULL, 0);
9498 		goto done;
9499 	}
9500 
9501 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
9502 	if (cfg_data == NULL) {
9503 		rc = ENOMEM;
9504 		goto done;
9505 	}
9506 
9507 	rc = copyin(cfg->data, cfg_data, cfg->len);
9508 	if (rc == 0)
9509 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
9510 
9511 	free(cfg_data, M_CXGBE);
9512 done:
9513 	end_synchronized_op(sc, 0);
9514 	return (rc);
9515 }
9516 
9517 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)9518 load_boot(struct adapter *sc, struct t4_bootrom *br)
9519 {
9520 	int rc;
9521 	uint8_t *br_data = NULL;
9522 	u_int offset;
9523 
9524 	if (br->len > 1024 * 1024)
9525 		return (EFBIG);
9526 
9527 	if (br->pf_offset == 0) {
9528 		/* pfidx */
9529 		if (br->pfidx_addr > 7)
9530 			return (EINVAL);
9531 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
9532 		    A_PCIE_PF_EXPROM_OFST)));
9533 	} else if (br->pf_offset == 1) {
9534 		/* offset */
9535 		offset = G_OFFSET(br->pfidx_addr);
9536 	} else {
9537 		return (EINVAL);
9538 	}
9539 
9540 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
9541 	if (rc)
9542 		return (rc);
9543 
9544 	if (br->len == 0) {
9545 		/* clear */
9546 		rc = -t4_load_boot(sc, NULL, offset, 0);
9547 		goto done;
9548 	}
9549 
9550 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
9551 	if (br_data == NULL) {
9552 		rc = ENOMEM;
9553 		goto done;
9554 	}
9555 
9556 	rc = copyin(br->data, br_data, br->len);
9557 	if (rc == 0)
9558 		rc = -t4_load_boot(sc, br_data, offset, br->len);
9559 
9560 	free(br_data, M_CXGBE);
9561 done:
9562 	end_synchronized_op(sc, 0);
9563 	return (rc);
9564 }
9565 
9566 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)9567 load_bootcfg(struct adapter *sc, struct t4_data *bc)
9568 {
9569 	int rc;
9570 	uint8_t *bc_data = NULL;
9571 
9572 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
9573 	if (rc)
9574 		return (rc);
9575 
9576 	if (bc->len == 0) {
9577 		/* clear */
9578 		rc = -t4_load_bootcfg(sc, NULL, 0);
9579 		goto done;
9580 	}
9581 
9582 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
9583 	if (bc_data == NULL) {
9584 		rc = ENOMEM;
9585 		goto done;
9586 	}
9587 
9588 	rc = copyin(bc->data, bc_data, bc->len);
9589 	if (rc == 0)
9590 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
9591 
9592 	free(bc_data, M_CXGBE);
9593 done:
9594 	end_synchronized_op(sc, 0);
9595 	return (rc);
9596 }
9597 
9598 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)9599 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
9600 {
9601 	int rc;
9602 	struct cudbg_init *cudbg;
9603 	void *handle, *buf;
9604 
9605 	/* buf is large, don't block if no memory is available */
9606 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
9607 	if (buf == NULL)
9608 		return (ENOMEM);
9609 
9610 	handle = cudbg_alloc_handle();
9611 	if (handle == NULL) {
9612 		rc = ENOMEM;
9613 		goto done;
9614 	}
9615 
9616 	cudbg = cudbg_get_init(handle);
9617 	cudbg->adap = sc;
9618 	cudbg->print = (cudbg_print_cb)printf;
9619 
9620 #ifndef notyet
9621 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
9622 	    __func__, dump->wr_flash, dump->len, dump->data);
9623 #endif
9624 
9625 	if (dump->wr_flash)
9626 		cudbg->use_flash = 1;
9627 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
9628 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
9629 
9630 	rc = cudbg_collect(handle, buf, &dump->len);
9631 	if (rc != 0)
9632 		goto done;
9633 
9634 	rc = copyout(buf, dump->data, dump->len);
9635 done:
9636 	cudbg_free_handle(handle);
9637 	free(buf, M_CXGBE);
9638 	return (rc);
9639 }
9640 
9641 static void
free_offload_policy(struct t4_offload_policy * op)9642 free_offload_policy(struct t4_offload_policy *op)
9643 {
9644 	struct offload_rule *r;
9645 	int i;
9646 
9647 	if (op == NULL)
9648 		return;
9649 
9650 	r = &op->rule[0];
9651 	for (i = 0; i < op->nrules; i++, r++) {
9652 		free(r->bpf_prog.bf_insns, M_CXGBE);
9653 	}
9654 	free(op->rule, M_CXGBE);
9655 	free(op, M_CXGBE);
9656 }
9657 
9658 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)9659 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
9660 {
9661 	int i, rc, len;
9662 	struct t4_offload_policy *op, *old;
9663 	struct bpf_program *bf;
9664 	const struct offload_settings *s;
9665 	struct offload_rule *r;
9666 	void *u;
9667 
9668 	if (!is_offload(sc))
9669 		return (ENODEV);
9670 
9671 	if (uop->nrules == 0) {
9672 		/* Delete installed policies. */
9673 		op = NULL;
9674 		goto set_policy;
9675 	} if (uop->nrules > 256) { /* arbitrary */
9676 		return (E2BIG);
9677 	}
9678 
9679 	/* Copy userspace offload policy to kernel */
9680 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
9681 	op->nrules = uop->nrules;
9682 	len = op->nrules * sizeof(struct offload_rule);
9683 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
9684 	rc = copyin(uop->rule, op->rule, len);
9685 	if (rc) {
9686 		free(op->rule, M_CXGBE);
9687 		free(op, M_CXGBE);
9688 		return (rc);
9689 	}
9690 
9691 	r = &op->rule[0];
9692 	for (i = 0; i < op->nrules; i++, r++) {
9693 
9694 		/* Validate open_type */
9695 		if (r->open_type != OPEN_TYPE_LISTEN &&
9696 		    r->open_type != OPEN_TYPE_ACTIVE &&
9697 		    r->open_type != OPEN_TYPE_PASSIVE &&
9698 		    r->open_type != OPEN_TYPE_DONTCARE) {
9699 error:
9700 			/*
9701 			 * Rules 0 to i have malloc'd filters that need to be
9702 			 * freed.  Rules i+1 to nrules have userspace pointers
9703 			 * and should be left alone.
9704 			 */
9705 			op->nrules = i;
9706 			free_offload_policy(op);
9707 			return (rc);
9708 		}
9709 
9710 		/* Validate settings */
9711 		s = &r->settings;
9712 		if ((s->offload != 0 && s->offload != 1) ||
9713 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
9714 		    s->sched_class < -1 ||
9715 		    s->sched_class >= sc->chip_params->nsched_cls) {
9716 			rc = EINVAL;
9717 			goto error;
9718 		}
9719 
9720 		bf = &r->bpf_prog;
9721 		u = bf->bf_insns;	/* userspace ptr */
9722 		bf->bf_insns = NULL;
9723 		if (bf->bf_len == 0) {
9724 			/* legal, matches everything */
9725 			continue;
9726 		}
9727 		len = bf->bf_len * sizeof(*bf->bf_insns);
9728 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
9729 		rc = copyin(u, bf->bf_insns, len);
9730 		if (rc != 0)
9731 			goto error;
9732 
9733 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
9734 			rc = EINVAL;
9735 			goto error;
9736 		}
9737 	}
9738 set_policy:
9739 	rw_wlock(&sc->policy_lock);
9740 	old = sc->policy;
9741 	sc->policy = op;
9742 	rw_wunlock(&sc->policy_lock);
9743 	free_offload_policy(old);
9744 
9745 	return (0);
9746 }
9747 
9748 #define MAX_READ_BUF_SIZE (128 * 1024)
9749 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)9750 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
9751 {
9752 	uint32_t addr, remaining, n;
9753 	uint32_t *buf;
9754 	int rc;
9755 	uint8_t *dst;
9756 
9757 	rc = validate_mem_range(sc, mr->addr, mr->len);
9758 	if (rc != 0)
9759 		return (rc);
9760 
9761 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
9762 	addr = mr->addr;
9763 	remaining = mr->len;
9764 	dst = (void *)mr->data;
9765 
9766 	while (remaining) {
9767 		n = min(remaining, MAX_READ_BUF_SIZE);
9768 		read_via_memwin(sc, 2, addr, buf, n);
9769 
9770 		rc = copyout(buf, dst, n);
9771 		if (rc != 0)
9772 			break;
9773 
9774 		dst += n;
9775 		remaining -= n;
9776 		addr += n;
9777 	}
9778 
9779 	free(buf, M_CXGBE);
9780 	return (rc);
9781 }
9782 #undef MAX_READ_BUF_SIZE
9783 
9784 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)9785 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
9786 {
9787 	int rc;
9788 
9789 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
9790 		return (EINVAL);
9791 
9792 	if (i2cd->len > sizeof(i2cd->data))
9793 		return (EFBIG);
9794 
9795 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
9796 	if (rc)
9797 		return (rc);
9798 	rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
9799 	    i2cd->offset, i2cd->len, &i2cd->data[0]);
9800 	end_synchronized_op(sc, 0);
9801 
9802 	return (rc);
9803 }
9804 
9805 int
t4_os_find_pci_capability(struct adapter * sc,int cap)9806 t4_os_find_pci_capability(struct adapter *sc, int cap)
9807 {
9808 	int i;
9809 
9810 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
9811 }
9812 
9813 int
t4_os_pci_save_state(struct adapter * sc)9814 t4_os_pci_save_state(struct adapter *sc)
9815 {
9816 	device_t dev;
9817 	struct pci_devinfo *dinfo;
9818 
9819 	dev = sc->dev;
9820 	dinfo = device_get_ivars(dev);
9821 
9822 	pci_cfg_save(dev, dinfo, 0);
9823 	return (0);
9824 }
9825 
9826 int
t4_os_pci_restore_state(struct adapter * sc)9827 t4_os_pci_restore_state(struct adapter *sc)
9828 {
9829 	device_t dev;
9830 	struct pci_devinfo *dinfo;
9831 
9832 	dev = sc->dev;
9833 	dinfo = device_get_ivars(dev);
9834 
9835 	pci_cfg_restore(dev, dinfo);
9836 	return (0);
9837 }
9838 
9839 void
t4_os_portmod_changed(struct port_info * pi)9840 t4_os_portmod_changed(struct port_info *pi)
9841 {
9842 	struct adapter *sc = pi->adapter;
9843 	struct vi_info *vi;
9844 	struct ifnet *ifp;
9845 	static const char *mod_str[] = {
9846 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
9847 	};
9848 
9849 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
9850 	    ("%s: port_type %u", __func__, pi->port_type));
9851 
9852 	vi = &pi->vi[0];
9853 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
9854 		PORT_LOCK(pi);
9855 		build_medialist(pi);
9856 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
9857 			fixup_link_config(pi);
9858 			apply_link_config(pi);
9859 		}
9860 		PORT_UNLOCK(pi);
9861 		end_synchronized_op(sc, LOCK_HELD);
9862 	}
9863 
9864 	ifp = vi->ifp;
9865 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
9866 		if_printf(ifp, "transceiver unplugged.\n");
9867 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
9868 		if_printf(ifp, "unknown transceiver inserted.\n");
9869 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
9870 		if_printf(ifp, "unsupported transceiver inserted.\n");
9871 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
9872 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
9873 		    port_top_speed(pi), mod_str[pi->mod_type]);
9874 	} else {
9875 		if_printf(ifp, "transceiver (type %d) inserted.\n",
9876 		    pi->mod_type);
9877 	}
9878 }
9879 
9880 void
t4_os_link_changed(struct port_info * pi)9881 t4_os_link_changed(struct port_info *pi)
9882 {
9883 	struct vi_info *vi;
9884 	struct ifnet *ifp;
9885 	struct link_config *lc;
9886 	int v;
9887 
9888 	PORT_LOCK_ASSERT_OWNED(pi);
9889 
9890 	for_each_vi(pi, v, vi) {
9891 		ifp = vi->ifp;
9892 		if (ifp == NULL)
9893 			continue;
9894 
9895 		lc = &pi->link_cfg;
9896 		if (lc->link_ok) {
9897 			ifp->if_baudrate = IF_Mbps(lc->speed);
9898 			if_link_state_change(ifp, LINK_STATE_UP);
9899 		} else {
9900 			if_link_state_change(ifp, LINK_STATE_DOWN);
9901 		}
9902 	}
9903 }
9904 
9905 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)9906 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
9907 {
9908 	struct adapter *sc;
9909 
9910 	sx_slock(&t4_list_lock);
9911 	SLIST_FOREACH(sc, &t4_list, link) {
9912 		/*
9913 		 * func should not make any assumptions about what state sc is
9914 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
9915 		 */
9916 		func(sc, arg);
9917 	}
9918 	sx_sunlock(&t4_list_lock);
9919 }
9920 
9921 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)9922 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
9923     struct thread *td)
9924 {
9925 	int rc;
9926 	struct adapter *sc = dev->si_drv1;
9927 
9928 	rc = priv_check(td, PRIV_DRIVER);
9929 	if (rc != 0)
9930 		return (rc);
9931 
9932 	switch (cmd) {
9933 	case CHELSIO_T4_GETREG: {
9934 		struct t4_reg *edata = (struct t4_reg *)data;
9935 
9936 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
9937 			return (EFAULT);
9938 
9939 		if (edata->size == 4)
9940 			edata->val = t4_read_reg(sc, edata->addr);
9941 		else if (edata->size == 8)
9942 			edata->val = t4_read_reg64(sc, edata->addr);
9943 		else
9944 			return (EINVAL);
9945 
9946 		break;
9947 	}
9948 	case CHELSIO_T4_SETREG: {
9949 		struct t4_reg *edata = (struct t4_reg *)data;
9950 
9951 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
9952 			return (EFAULT);
9953 
9954 		if (edata->size == 4) {
9955 			if (edata->val & 0xffffffff00000000)
9956 				return (EINVAL);
9957 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
9958 		} else if (edata->size == 8)
9959 			t4_write_reg64(sc, edata->addr, edata->val);
9960 		else
9961 			return (EINVAL);
9962 		break;
9963 	}
9964 	case CHELSIO_T4_REGDUMP: {
9965 		struct t4_regdump *regs = (struct t4_regdump *)data;
9966 		int reglen = t4_get_regs_len(sc);
9967 		uint8_t *buf;
9968 
9969 		if (regs->len < reglen) {
9970 			regs->len = reglen; /* hint to the caller */
9971 			return (ENOBUFS);
9972 		}
9973 
9974 		regs->len = reglen;
9975 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
9976 		get_regs(sc, regs, buf);
9977 		rc = copyout(buf, regs->data, reglen);
9978 		free(buf, M_CXGBE);
9979 		break;
9980 	}
9981 	case CHELSIO_T4_GET_FILTER_MODE:
9982 		rc = get_filter_mode(sc, (uint32_t *)data);
9983 		break;
9984 	case CHELSIO_T4_SET_FILTER_MODE:
9985 		rc = set_filter_mode(sc, *(uint32_t *)data);
9986 		break;
9987 	case CHELSIO_T4_GET_FILTER:
9988 		rc = get_filter(sc, (struct t4_filter *)data);
9989 		break;
9990 	case CHELSIO_T4_SET_FILTER:
9991 		rc = set_filter(sc, (struct t4_filter *)data);
9992 		break;
9993 	case CHELSIO_T4_DEL_FILTER:
9994 		rc = del_filter(sc, (struct t4_filter *)data);
9995 		break;
9996 	case CHELSIO_T4_GET_SGE_CONTEXT:
9997 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
9998 		break;
9999 	case CHELSIO_T4_LOAD_FW:
10000 		rc = load_fw(sc, (struct t4_data *)data);
10001 		break;
10002 	case CHELSIO_T4_GET_MEM:
10003 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
10004 		break;
10005 	case CHELSIO_T4_GET_I2C:
10006 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
10007 		break;
10008 	case CHELSIO_T4_CLEAR_STATS: {
10009 		int i, v, bg_map;
10010 		u_int port_id = *(uint32_t *)data;
10011 		struct port_info *pi;
10012 		struct vi_info *vi;
10013 
10014 		if (port_id >= sc->params.nports)
10015 			return (EINVAL);
10016 		pi = sc->port[port_id];
10017 		if (pi == NULL)
10018 			return (EIO);
10019 
10020 		/* MAC stats */
10021 		t4_clr_port_stats(sc, pi->tx_chan);
10022 		pi->tx_parse_error = 0;
10023 		pi->tnl_cong_drops = 0;
10024 		mtx_lock(&sc->reg_lock);
10025 		for_each_vi(pi, v, vi) {
10026 			if (vi->flags & VI_INIT_DONE)
10027 				t4_clr_vi_stats(sc, vi->vin);
10028 		}
10029 		bg_map = pi->mps_bg_map;
10030 		v = 0;	/* reuse */
10031 		while (bg_map) {
10032 			i = ffs(bg_map) - 1;
10033 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
10034 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
10035 			bg_map &= ~(1 << i);
10036 		}
10037 		mtx_unlock(&sc->reg_lock);
10038 
10039 		/*
10040 		 * Since this command accepts a port, clear stats for
10041 		 * all VIs on this port.
10042 		 */
10043 		for_each_vi(pi, v, vi) {
10044 			if (vi->flags & VI_INIT_DONE) {
10045 				struct sge_rxq *rxq;
10046 				struct sge_txq *txq;
10047 				struct sge_wrq *wrq;
10048 
10049 				for_each_rxq(vi, i, rxq) {
10050 #if defined(INET) || defined(INET6)
10051 					rxq->lro.lro_queued = 0;
10052 					rxq->lro.lro_flushed = 0;
10053 #endif
10054 					rxq->rxcsum = 0;
10055 					rxq->vlan_extraction = 0;
10056 				}
10057 
10058 				for_each_txq(vi, i, txq) {
10059 					txq->txcsum = 0;
10060 					txq->tso_wrs = 0;
10061 					txq->vlan_insertion = 0;
10062 					txq->imm_wrs = 0;
10063 					txq->sgl_wrs = 0;
10064 					txq->txpkt_wrs = 0;
10065 					txq->txpkts0_wrs = 0;
10066 					txq->txpkts1_wrs = 0;
10067 					txq->txpkts0_pkts = 0;
10068 					txq->txpkts1_pkts = 0;
10069 					mp_ring_reset_stats(txq->r);
10070 				}
10071 
10072 #ifdef TCP_OFFLOAD
10073 				/* nothing to clear for each ofld_rxq */
10074 
10075 				for_each_ofld_txq(vi, i, wrq) {
10076 					wrq->tx_wrs_direct = 0;
10077 					wrq->tx_wrs_copied = 0;
10078 				}
10079 #endif
10080 
10081 				if (IS_MAIN_VI(vi)) {
10082 					wrq = &sc->sge.ctrlq[pi->port_id];
10083 					wrq->tx_wrs_direct = 0;
10084 					wrq->tx_wrs_copied = 0;
10085 				}
10086 			}
10087 		}
10088 		break;
10089 	}
10090 	case CHELSIO_T4_SCHED_CLASS:
10091 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
10092 		break;
10093 	case CHELSIO_T4_SCHED_QUEUE:
10094 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
10095 		break;
10096 	case CHELSIO_T4_GET_TRACER:
10097 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
10098 		break;
10099 	case CHELSIO_T4_SET_TRACER:
10100 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
10101 		break;
10102 	case CHELSIO_T4_LOAD_CFG:
10103 		rc = load_cfg(sc, (struct t4_data *)data);
10104 		break;
10105 	case CHELSIO_T4_LOAD_BOOT:
10106 		rc = load_boot(sc, (struct t4_bootrom *)data);
10107 		break;
10108 	case CHELSIO_T4_LOAD_BOOTCFG:
10109 		rc = load_bootcfg(sc, (struct t4_data *)data);
10110 		break;
10111 	case CHELSIO_T4_CUDBG_DUMP:
10112 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
10113 		break;
10114 	case CHELSIO_T4_SET_OFLD_POLICY:
10115 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
10116 		break;
10117 	default:
10118 		rc = ENOTTY;
10119 	}
10120 
10121 	return (rc);
10122 }
10123 
10124 #ifdef TCP_OFFLOAD
10125 void
t4_iscsi_init(struct adapter * sc,u_int tag_mask,const u_int * pgsz_order)10126 t4_iscsi_init(struct adapter *sc, u_int tag_mask, const u_int *pgsz_order)
10127 {
10128 
10129 	t4_write_reg(sc, A_ULP_RX_ISCSI_TAGMASK, tag_mask);
10130 	t4_write_reg(sc, A_ULP_RX_ISCSI_PSZ, V_HPZ0(pgsz_order[0]) |
10131 		V_HPZ1(pgsz_order[1]) | V_HPZ2(pgsz_order[2]) |
10132 		V_HPZ3(pgsz_order[3]));
10133 }
10134 
10135 static int
toe_capability(struct vi_info * vi,int enable)10136 toe_capability(struct vi_info *vi, int enable)
10137 {
10138 	int rc;
10139 	struct port_info *pi = vi->pi;
10140 	struct adapter *sc = pi->adapter;
10141 
10142 	ASSERT_SYNCHRONIZED_OP(sc);
10143 
10144 	if (!is_offload(sc))
10145 		return (ENODEV);
10146 
10147 	if (enable) {
10148 		if ((vi->ifp->if_capenable & IFCAP_TOE) != 0) {
10149 			/* TOE is already enabled. */
10150 			return (0);
10151 		}
10152 
10153 		/*
10154 		 * We need the port's queues around so that we're able to send
10155 		 * and receive CPLs to/from the TOE even if the ifnet for this
10156 		 * port has never been UP'd administratively.
10157 		 */
10158 		if (!(vi->flags & VI_INIT_DONE)) {
10159 			rc = vi_full_init(vi);
10160 			if (rc)
10161 				return (rc);
10162 		}
10163 		if (!(pi->vi[0].flags & VI_INIT_DONE)) {
10164 			rc = vi_full_init(&pi->vi[0]);
10165 			if (rc)
10166 				return (rc);
10167 		}
10168 
10169 		if (isset(&sc->offload_map, pi->port_id)) {
10170 			/* TOE is enabled on another VI of this port. */
10171 			pi->uld_vis++;
10172 			return (0);
10173 		}
10174 
10175 		if (!uld_active(sc, ULD_TOM)) {
10176 			rc = t4_activate_uld(sc, ULD_TOM);
10177 			if (rc == EAGAIN) {
10178 				log(LOG_WARNING,
10179 				    "You must kldload t4_tom.ko before trying "
10180 				    "to enable TOE on a cxgbe interface.\n");
10181 			}
10182 			if (rc != 0)
10183 				return (rc);
10184 			KASSERT(sc->tom_softc != NULL,
10185 			    ("%s: TOM activated but softc NULL", __func__));
10186 			KASSERT(uld_active(sc, ULD_TOM),
10187 			    ("%s: TOM activated but flag not set", __func__));
10188 		}
10189 
10190 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
10191 		if (!uld_active(sc, ULD_IWARP))
10192 			(void) t4_activate_uld(sc, ULD_IWARP);
10193 		if (!uld_active(sc, ULD_ISCSI))
10194 			(void) t4_activate_uld(sc, ULD_ISCSI);
10195 
10196 		pi->uld_vis++;
10197 		setbit(&sc->offload_map, pi->port_id);
10198 	} else {
10199 		pi->uld_vis--;
10200 
10201 		if (!isset(&sc->offload_map, pi->port_id) || pi->uld_vis > 0)
10202 			return (0);
10203 
10204 		KASSERT(uld_active(sc, ULD_TOM),
10205 		    ("%s: TOM never initialized?", __func__));
10206 		clrbit(&sc->offload_map, pi->port_id);
10207 	}
10208 
10209 	return (0);
10210 }
10211 
10212 /*
10213  * Add an upper layer driver to the global list.
10214  */
10215 int
t4_register_uld(struct uld_info * ui)10216 t4_register_uld(struct uld_info *ui)
10217 {
10218 	int rc = 0;
10219 	struct uld_info *u;
10220 
10221 	sx_xlock(&t4_uld_list_lock);
10222 	SLIST_FOREACH(u, &t4_uld_list, link) {
10223 	    if (u->uld_id == ui->uld_id) {
10224 		    rc = EEXIST;
10225 		    goto done;
10226 	    }
10227 	}
10228 
10229 	SLIST_INSERT_HEAD(&t4_uld_list, ui, link);
10230 	ui->refcount = 0;
10231 done:
10232 	sx_xunlock(&t4_uld_list_lock);
10233 	return (rc);
10234 }
10235 
10236 int
t4_unregister_uld(struct uld_info * ui)10237 t4_unregister_uld(struct uld_info *ui)
10238 {
10239 	int rc = EINVAL;
10240 	struct uld_info *u;
10241 
10242 	sx_xlock(&t4_uld_list_lock);
10243 
10244 	SLIST_FOREACH(u, &t4_uld_list, link) {
10245 	    if (u == ui) {
10246 		    if (ui->refcount > 0) {
10247 			    rc = EBUSY;
10248 			    goto done;
10249 		    }
10250 
10251 		    SLIST_REMOVE(&t4_uld_list, ui, uld_info, link);
10252 		    rc = 0;
10253 		    goto done;
10254 	    }
10255 	}
10256 done:
10257 	sx_xunlock(&t4_uld_list_lock);
10258 	return (rc);
10259 }
10260 
10261 int
t4_activate_uld(struct adapter * sc,int id)10262 t4_activate_uld(struct adapter *sc, int id)
10263 {
10264 	int rc;
10265 	struct uld_info *ui;
10266 
10267 	ASSERT_SYNCHRONIZED_OP(sc);
10268 
10269 	if (id < 0 || id > ULD_MAX)
10270 		return (EINVAL);
10271 	rc = EAGAIN;	/* kldoad the module with this ULD and try again. */
10272 
10273 	sx_slock(&t4_uld_list_lock);
10274 
10275 	SLIST_FOREACH(ui, &t4_uld_list, link) {
10276 		if (ui->uld_id == id) {
10277 			if (!(sc->flags & FULL_INIT_DONE)) {
10278 				rc = adapter_full_init(sc);
10279 				if (rc != 0)
10280 					break;
10281 			}
10282 
10283 			rc = ui->activate(sc);
10284 			if (rc == 0) {
10285 				setbit(&sc->active_ulds, id);
10286 				ui->refcount++;
10287 			}
10288 			break;
10289 		}
10290 	}
10291 
10292 	sx_sunlock(&t4_uld_list_lock);
10293 
10294 	return (rc);
10295 }
10296 
10297 int
t4_deactivate_uld(struct adapter * sc,int id)10298 t4_deactivate_uld(struct adapter *sc, int id)
10299 {
10300 	int rc;
10301 	struct uld_info *ui;
10302 
10303 	ASSERT_SYNCHRONIZED_OP(sc);
10304 
10305 	if (id < 0 || id > ULD_MAX)
10306 		return (EINVAL);
10307 	rc = ENXIO;
10308 
10309 	sx_slock(&t4_uld_list_lock);
10310 
10311 	SLIST_FOREACH(ui, &t4_uld_list, link) {
10312 		if (ui->uld_id == id) {
10313 			rc = ui->deactivate(sc);
10314 			if (rc == 0) {
10315 				clrbit(&sc->active_ulds, id);
10316 				ui->refcount--;
10317 			}
10318 			break;
10319 		}
10320 	}
10321 
10322 	sx_sunlock(&t4_uld_list_lock);
10323 
10324 	return (rc);
10325 }
10326 
10327 int
uld_active(struct adapter * sc,int uld_id)10328 uld_active(struct adapter *sc, int uld_id)
10329 {
10330 
10331 	MPASS(uld_id >= 0 && uld_id <= ULD_MAX);
10332 
10333 	return (isset(&sc->active_ulds, uld_id));
10334 }
10335 #endif
10336 
10337 /*
10338  * t  = ptr to tunable.
10339  * nc = number of CPUs.
10340  * c  = compiled in default for that tunable.
10341  */
10342 static void
calculate_nqueues(int * t,int nc,const int c)10343 calculate_nqueues(int *t, int nc, const int c)
10344 {
10345 	int nq;
10346 
10347 	if (*t > 0)
10348 		return;
10349 	nq = *t < 0 ? -*t : c;
10350 	*t = min(nc, nq);
10351 }
10352 
10353 /*
10354  * Come up with reasonable defaults for some of the tunables, provided they're
10355  * not set by the user (in which case we'll use the values as is).
10356  */
10357 static void
tweak_tunables(void)10358 tweak_tunables(void)
10359 {
10360 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
10361 
10362 	if (t4_ntxq < 1) {
10363 #ifdef RSS
10364 		t4_ntxq = rss_getnumbuckets();
10365 #else
10366 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
10367 #endif
10368 	}
10369 
10370 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
10371 
10372 	if (t4_nrxq < 1) {
10373 #ifdef RSS
10374 		t4_nrxq = rss_getnumbuckets();
10375 #else
10376 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
10377 #endif
10378 	}
10379 
10380 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
10381 
10382 #ifdef TCP_OFFLOAD
10383 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
10384 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
10385 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
10386 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
10387 
10388 	if (t4_toecaps_allowed == -1)
10389 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
10390 
10391 	if (t4_rdmacaps_allowed == -1) {
10392 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
10393 		    FW_CAPS_CONFIG_RDMA_RDMAC;
10394 	}
10395 
10396 	if (t4_iscsicaps_allowed == -1) {
10397 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
10398 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
10399 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
10400 	}
10401 
10402 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
10403 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
10404 
10405 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
10406 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
10407 #else
10408 	if (t4_toecaps_allowed == -1)
10409 		t4_toecaps_allowed = 0;
10410 
10411 	if (t4_rdmacaps_allowed == -1)
10412 		t4_rdmacaps_allowed = 0;
10413 
10414 	if (t4_iscsicaps_allowed == -1)
10415 		t4_iscsicaps_allowed = 0;
10416 #endif
10417 
10418 #ifdef DEV_NETMAP
10419 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
10420 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
10421 #endif
10422 
10423 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
10424 		t4_tmr_idx = TMR_IDX;
10425 
10426 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
10427 		t4_pktc_idx = PKTC_IDX;
10428 
10429 	if (t4_qsize_txq < 128)
10430 		t4_qsize_txq = 128;
10431 
10432 	if (t4_qsize_rxq < 128)
10433 		t4_qsize_rxq = 128;
10434 	while (t4_qsize_rxq & 7)
10435 		t4_qsize_rxq++;
10436 
10437 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
10438 
10439 	/*
10440 	 * Number of VIs to create per-port.  The first VI is the "main" regular
10441 	 * VI for the port.  The rest are additional virtual interfaces on the
10442 	 * same physical port.  Note that the main VI does not have native
10443 	 * netmap support but the extra VIs do.
10444 	 *
10445 	 * Limit the number of VIs per port to the number of available
10446 	 * MAC addresses per port.
10447 	 */
10448 	if (t4_num_vis < 1)
10449 		t4_num_vis = 1;
10450 	if (t4_num_vis > nitems(vi_mac_funcs)) {
10451 		t4_num_vis = nitems(vi_mac_funcs);
10452 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
10453 	}
10454 
10455 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
10456 		pcie_relaxed_ordering = 1;
10457 #if defined(__i386__) || defined(__amd64__)
10458 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
10459 			pcie_relaxed_ordering = 0;
10460 #endif
10461 	}
10462 }
10463 
10464 #ifdef DDB
10465 static void
t4_dump_tcb(struct adapter * sc,int tid)10466 t4_dump_tcb(struct adapter *sc, int tid)
10467 {
10468 	uint32_t base, i, j, off, pf, reg, save, tcb_addr, win_pos;
10469 
10470 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
10471 	save = t4_read_reg(sc, reg);
10472 	base = sc->memwin[2].mw_base;
10473 
10474 	/* Dump TCB for the tid */
10475 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
10476 	tcb_addr += tid * TCB_SIZE;
10477 
10478 	if (is_t4(sc)) {
10479 		pf = 0;
10480 		win_pos = tcb_addr & ~0xf;	/* start must be 16B aligned */
10481 	} else {
10482 		pf = V_PFNUM(sc->pf);
10483 		win_pos = tcb_addr & ~0x7f;	/* start must be 128B aligned */
10484 	}
10485 	t4_write_reg(sc, reg, win_pos | pf);
10486 	t4_read_reg(sc, reg);
10487 
10488 	off = tcb_addr - win_pos;
10489 	for (i = 0; i < 4; i++) {
10490 		uint32_t buf[8];
10491 		for (j = 0; j < 8; j++, off += 4)
10492 			buf[j] = htonl(t4_read_reg(sc, base + off));
10493 
10494 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
10495 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
10496 		    buf[7]);
10497 	}
10498 
10499 	t4_write_reg(sc, reg, save);
10500 	t4_read_reg(sc, reg);
10501 }
10502 
10503 static void
t4_dump_devlog(struct adapter * sc)10504 t4_dump_devlog(struct adapter *sc)
10505 {
10506 	struct devlog_params *dparams = &sc->params.devlog;
10507 	struct fw_devlog_e e;
10508 	int i, first, j, m, nentries, rc;
10509 	uint64_t ftstamp = UINT64_MAX;
10510 
10511 	if (dparams->start == 0) {
10512 		db_printf("devlog params not valid\n");
10513 		return;
10514 	}
10515 
10516 	nentries = dparams->size / sizeof(struct fw_devlog_e);
10517 	m = fwmtype_to_hwmtype(dparams->memtype);
10518 
10519 	/* Find the first entry. */
10520 	first = -1;
10521 	for (i = 0; i < nentries && !db_pager_quit; i++) {
10522 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
10523 		    sizeof(e), (void *)&e);
10524 		if (rc != 0)
10525 			break;
10526 
10527 		if (e.timestamp == 0)
10528 			break;
10529 
10530 		e.timestamp = be64toh(e.timestamp);
10531 		if (e.timestamp < ftstamp) {
10532 			ftstamp = e.timestamp;
10533 			first = i;
10534 		}
10535 	}
10536 
10537 	if (first == -1)
10538 		return;
10539 
10540 	i = first;
10541 	do {
10542 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
10543 		    sizeof(e), (void *)&e);
10544 		if (rc != 0)
10545 			return;
10546 
10547 		if (e.timestamp == 0)
10548 			return;
10549 
10550 		e.timestamp = be64toh(e.timestamp);
10551 		e.seqno = be32toh(e.seqno);
10552 		for (j = 0; j < 8; j++)
10553 			e.params[j] = be32toh(e.params[j]);
10554 
10555 		db_printf("%10d  %15ju  %8s  %8s  ",
10556 		    e.seqno, e.timestamp,
10557 		    (e.level < nitems(devlog_level_strings) ?
10558 			devlog_level_strings[e.level] : "UNKNOWN"),
10559 		    (e.facility < nitems(devlog_facility_strings) ?
10560 			devlog_facility_strings[e.facility] : "UNKNOWN"));
10561 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
10562 		    e.params[3], e.params[4], e.params[5], e.params[6],
10563 		    e.params[7]);
10564 
10565 		if (++i == nentries)
10566 			i = 0;
10567 	} while (i != first && !db_pager_quit);
10568 }
10569 
10570 static struct command_table db_t4_table = LIST_HEAD_INITIALIZER(db_t4_table);
10571 _DB_SET(_show, t4, NULL, db_show_table, 0, &db_t4_table);
10572 
DB_FUNC(devlog,db_show_devlog,db_t4_table,CS_OWN,NULL)10573 DB_FUNC(devlog, db_show_devlog, db_t4_table, CS_OWN, NULL)
10574 {
10575 	device_t dev;
10576 	int t;
10577 	bool valid;
10578 
10579 	valid = false;
10580 	t = db_read_token();
10581 	if (t == tIDENT) {
10582 		dev = device_lookup_by_name(db_tok_string);
10583 		valid = true;
10584 	}
10585 	db_skip_to_eol();
10586 	if (!valid) {
10587 		db_printf("usage: show t4 devlog <nexus>\n");
10588 		return;
10589 	}
10590 
10591 	if (dev == NULL) {
10592 		db_printf("device not found\n");
10593 		return;
10594 	}
10595 
10596 	t4_dump_devlog(device_get_softc(dev));
10597 }
10598 
DB_FUNC(tcb,db_show_t4tcb,db_t4_table,CS_OWN,NULL)10599 DB_FUNC(tcb, db_show_t4tcb, db_t4_table, CS_OWN, NULL)
10600 {
10601 	device_t dev;
10602 	int radix, tid, t;
10603 	bool valid;
10604 
10605 	valid = false;
10606 	radix = db_radix;
10607 	db_radix = 10;
10608 	t = db_read_token();
10609 	if (t == tIDENT) {
10610 		dev = device_lookup_by_name(db_tok_string);
10611 		t = db_read_token();
10612 		if (t == tNUMBER) {
10613 			tid = db_tok_number;
10614 			valid = true;
10615 		}
10616 	}
10617 	db_radix = radix;
10618 	db_skip_to_eol();
10619 	if (!valid) {
10620 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
10621 		return;
10622 	}
10623 
10624 	if (dev == NULL) {
10625 		db_printf("device not found\n");
10626 		return;
10627 	}
10628 	if (tid < 0) {
10629 		db_printf("invalid tid\n");
10630 		return;
10631 	}
10632 
10633 	t4_dump_tcb(device_get_softc(dev), tid);
10634 }
10635 #endif
10636 
10637 /*
10638  * Borrowed from cesa_prep_aes_key().
10639  *
10640  * NB: The crypto engine wants the words in the decryption key in reverse
10641  * order.
10642  */
10643 void
t4_aes_getdeckey(void * dec_key,const void * enc_key,unsigned int kbits)10644 t4_aes_getdeckey(void *dec_key, const void *enc_key, unsigned int kbits)
10645 {
10646 	uint32_t ek[4 * (RIJNDAEL_MAXNR + 1)];
10647 	uint32_t *dkey;
10648 	int i;
10649 
10650 	rijndaelKeySetupEnc(ek, enc_key, kbits);
10651 	dkey = dec_key;
10652 	dkey += (kbits / 8) / 4;
10653 
10654 	switch (kbits) {
10655 	case 128:
10656 		for (i = 0; i < 4; i++)
10657 			*--dkey = htobe32(ek[4 * 10 + i]);
10658 		break;
10659 	case 192:
10660 		for (i = 0; i < 2; i++)
10661 			*--dkey = htobe32(ek[4 * 11 + 2 + i]);
10662 		for (i = 0; i < 4; i++)
10663 			*--dkey = htobe32(ek[4 * 12 + i]);
10664 		break;
10665 	case 256:
10666 		for (i = 0; i < 4; i++)
10667 			*--dkey = htobe32(ek[4 * 13 + i]);
10668 		for (i = 0; i < 4; i++)
10669 			*--dkey = htobe32(ek[4 * 14 + i]);
10670 		break;
10671 	}
10672 	MPASS(dkey == dec_key);
10673 }
10674 
10675 static struct sx mlu;	/* mod load unload */
10676 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
10677 
10678 static int
mod_event(module_t mod,int cmd,void * arg)10679 mod_event(module_t mod, int cmd, void *arg)
10680 {
10681 	int rc = 0;
10682 	static int loaded = 0;
10683 
10684 	switch (cmd) {
10685 	case MOD_LOAD:
10686 		sx_xlock(&mlu);
10687 		if (loaded++ == 0) {
10688 			t4_sge_modload();
10689 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
10690 			    t4_filter_rpl, CPL_COOKIE_FILTER);
10691 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
10692 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
10693 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
10694 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
10695 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
10696 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
10697 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
10698 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
10699 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
10700 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
10701 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
10702 			    do_smt_write_rpl);
10703 			sx_init(&t4_list_lock, "T4/T5 adapters");
10704 			SLIST_INIT(&t4_list);
10705 			callout_init(&fatal_callout, 1);
10706 #ifdef TCP_OFFLOAD
10707 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
10708 			SLIST_INIT(&t4_uld_list);
10709 #endif
10710 #ifdef INET6
10711 			t4_clip_modload();
10712 #endif
10713 			t4_tracer_modload();
10714 			tweak_tunables();
10715 		}
10716 		sx_xunlock(&mlu);
10717 		break;
10718 
10719 	case MOD_UNLOAD:
10720 		sx_xlock(&mlu);
10721 		if (--loaded == 0) {
10722 			int tries;
10723 
10724 			sx_slock(&t4_list_lock);
10725 			if (!SLIST_EMPTY(&t4_list)) {
10726 				rc = EBUSY;
10727 				sx_sunlock(&t4_list_lock);
10728 				goto done_unload;
10729 			}
10730 #ifdef TCP_OFFLOAD
10731 			sx_slock(&t4_uld_list_lock);
10732 			if (!SLIST_EMPTY(&t4_uld_list)) {
10733 				rc = EBUSY;
10734 				sx_sunlock(&t4_uld_list_lock);
10735 				sx_sunlock(&t4_list_lock);
10736 				goto done_unload;
10737 			}
10738 #endif
10739 			tries = 0;
10740 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
10741 				uprintf("%ju clusters with custom free routine "
10742 				    "still is use.\n", t4_sge_extfree_refs());
10743 				pause("t4unload", 2 * hz);
10744 			}
10745 #ifdef TCP_OFFLOAD
10746 			sx_sunlock(&t4_uld_list_lock);
10747 #endif
10748 			sx_sunlock(&t4_list_lock);
10749 
10750 			if (t4_sge_extfree_refs() == 0) {
10751 				t4_tracer_modunload();
10752 #ifdef INET6
10753 				t4_clip_modunload();
10754 #endif
10755 #ifdef TCP_OFFLOAD
10756 				sx_destroy(&t4_uld_list_lock);
10757 #endif
10758 				sx_destroy(&t4_list_lock);
10759 				t4_sge_modunload();
10760 				loaded = 0;
10761 			} else {
10762 				rc = EBUSY;
10763 				loaded++;	/* undo earlier decrement */
10764 			}
10765 		}
10766 done_unload:
10767 		sx_xunlock(&mlu);
10768 		break;
10769 	}
10770 
10771 	return (rc);
10772 }
10773 
10774 static devclass_t t4_devclass, t5_devclass, t6_devclass;
10775 static devclass_t cxgbe_devclass, cxl_devclass, cc_devclass;
10776 static devclass_t vcxgbe_devclass, vcxl_devclass, vcc_devclass;
10777 
10778 DRIVER_MODULE(t4nex, pci, t4_driver, t4_devclass, mod_event, 0);
10779 MODULE_VERSION(t4nex, 1);
10780 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
10781 #ifdef DEV_NETMAP
10782 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
10783 #endif /* DEV_NETMAP */
10784 
10785 DRIVER_MODULE(t5nex, pci, t5_driver, t5_devclass, mod_event, 0);
10786 MODULE_VERSION(t5nex, 1);
10787 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
10788 #ifdef DEV_NETMAP
10789 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
10790 #endif /* DEV_NETMAP */
10791 
10792 DRIVER_MODULE(t6nex, pci, t6_driver, t6_devclass, mod_event, 0);
10793 MODULE_VERSION(t6nex, 1);
10794 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
10795 #ifdef DEV_NETMAP
10796 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
10797 #endif /* DEV_NETMAP */
10798 
10799 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, cxgbe_devclass, 0, 0);
10800 MODULE_VERSION(cxgbe, 1);
10801 
10802 DRIVER_MODULE(cxl, t5nex, cxl_driver, cxl_devclass, 0, 0);
10803 MODULE_VERSION(cxl, 1);
10804 
10805 DRIVER_MODULE(cc, t6nex, cc_driver, cc_devclass, 0, 0);
10806 MODULE_VERSION(cc, 1);
10807 
10808 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, vcxgbe_devclass, 0, 0);
10809 MODULE_VERSION(vcxgbe, 1);
10810 
10811 DRIVER_MODULE(vcxl, cxl, vcxl_driver, vcxl_devclass, 0, 0);
10812 MODULE_VERSION(vcxl, 1);
10813 
10814 DRIVER_MODULE(vcc, cc, vcc_driver, vcc_devclass, 0, 0);
10815 MODULE_VERSION(vcc, 1);
10816