]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/tools/cxgbetool/cxgbetool.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / tools / cxgbetool / cxgbetool.c
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 <stdint.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <err.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <sys/ioctl.h>
41 #include <limits.h>
42 #include <sys/mman.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/stat.h>
46 #include <net/ethernet.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49
50 #include "t4_ioctl.h"
51
52 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
53
54 #define max(x, y) ((x) > (y) ? (x) : (y))
55
56 static const char *progname, *nexus;
57 static int chip_id;     /* 4 for T4, 5 for T5 */
58
59 struct reg_info {
60         const char *name;
61         uint32_t addr;
62         uint32_t len;
63 };
64
65 struct mod_regs {
66         const char *name;
67         const struct reg_info *ri;
68 };
69
70 struct field_desc {
71         const char *name;     /* Field name */
72         unsigned short start; /* Start bit position */
73         unsigned short end;   /* End bit position */
74         unsigned char shift;  /* # of low order bits omitted and implicitly 0 */
75         unsigned char hex;    /* Print field in hex instead of decimal */
76         unsigned char islog2; /* Field contains the base-2 log of the value */
77 };
78
79 #include "reg_defs_t4.c"
80 #include "reg_defs_t4vf.c"
81 #include "reg_defs_t5.c"
82
83 static void
84 usage(FILE *fp)
85 {
86         fprintf(fp, "Usage: %s <nexus> [operation]\n", progname);
87         fprintf(fp,
88             "\tclearstats <port>                   clear port statistics\n"
89             "\tcontext <type> <id>                 show an SGE context\n"
90             "\tfilter <idx> [<param> <val>] ...    set a filter\n"
91             "\tfilter <idx> delete|clear           delete a filter\n"
92             "\tfilter list                         list all filters\n"
93             "\tfilter mode [<match>] ...           get/set global filter mode\n"
94             "\ti2c <port> <devaddr> <addr> [<len>] read from i2c device\n"
95             "\tloadfw <fw-image.bin>               install firmware\n"
96             "\tmemdump <addr> <len>                dump a memory range\n"
97             "\treg <address>[=<val>]               read/write register\n"
98             "\treg64 <address>[=<val>]             read/write 64 bit register\n"
99             "\tregdump [<module>] ...              dump registers\n"
100             "\tstdio                               interactive mode\n"
101             "\ttcb <tid>                           read TCB\n"
102             "\ttracer <idx> tx<n>|rx<n>            set and enable a tracer)\n"
103             "\ttracer <idx> disable|enable         disable or enable a tracer\n"
104             "\ttracer list                         list all tracers\n"
105             );
106 }
107
108 static inline unsigned int
109 get_card_vers(unsigned int version)
110 {
111         return (version & 0x3ff);
112 }
113
114 static int
115 real_doit(unsigned long cmd, void *data, const char *cmdstr)
116 {
117         static int fd = -1;
118         int rc = 0;
119
120         if (fd == -1) {
121                 char buf[64];
122
123                 snprintf(buf, sizeof(buf), "/dev/%s", nexus);
124                 if ((fd = open(buf, O_RDWR)) < 0) {
125                         warn("open(%s)", nexus);
126                         rc = errno;
127                         return (rc);
128                 }
129                 chip_id = nexus[1] - '0';
130         }
131
132         rc = ioctl(fd, cmd, data);
133         if (rc < 0) {
134                 warn("%s", cmdstr);
135                 rc = errno;
136         }
137
138         return (rc);
139 }
140 #define doit(x, y) real_doit(x, y, #x)
141
142 static char *
143 str_to_number(const char *s, long *val, long long *vall)
144 {
145         char *p;
146
147         if (vall)
148                 *vall = strtoll(s, &p, 0);
149         else if (val)
150                 *val = strtol(s, &p, 0);
151         else
152                 p = NULL;
153
154         return (p);
155 }
156
157 static int
158 read_reg(long addr, int size, long long *val)
159 {
160         struct t4_reg reg;
161         int rc;
162
163         reg.addr = (uint32_t) addr;
164         reg.size = (uint32_t) size;
165         reg.val = 0;
166
167         rc = doit(CHELSIO_T4_GETREG, &reg);
168
169         *val = reg.val;
170
171         return (rc);
172 }
173
174 static int
175 write_reg(long addr, int size, long long val)
176 {
177         struct t4_reg reg;
178
179         reg.addr = (uint32_t) addr;
180         reg.size = (uint32_t) size;
181         reg.val = (uint64_t) val;
182
183         return doit(CHELSIO_T4_SETREG, &reg);
184 }
185
186 static int
187 register_io(int argc, const char *argv[], int size)
188 {
189         char *p, *v;
190         long addr;
191         long long val;
192         int w = 0, rc;
193
194         if (argc == 1) {
195                 /* <reg> OR <reg>=<value> */
196
197                 p = str_to_number(argv[0], &addr, NULL);
198                 if (*p) {
199                         if (*p != '=') {
200                                 warnx("invalid register \"%s\"", argv[0]);
201                                 return (EINVAL);
202                         }
203
204                         w = 1;
205                         v = p + 1;
206                         p = str_to_number(v, NULL, &val);
207
208                         if (*p) {
209                                 warnx("invalid value \"%s\"", v);
210                                 return (EINVAL);
211                         }
212                 }
213
214         } else if (argc == 2) {
215                 /* <reg> <value> */
216
217                 w = 1;
218
219                 p = str_to_number(argv[0], &addr, NULL);
220                 if (*p) {
221                         warnx("invalid register \"%s\"", argv[0]);
222                         return (EINVAL);
223                 }
224
225                 p = str_to_number(argv[1], NULL, &val);
226                 if (*p) {
227                         warnx("invalid value \"%s\"", argv[1]);
228                         return (EINVAL);
229                 }
230         } else {
231                 warnx("reg: invalid number of arguments (%d)", argc);
232                 return (EINVAL);
233         }
234
235         if (w)
236                 rc = write_reg(addr, size, val);
237         else {
238                 rc = read_reg(addr, size, &val);
239                 if (rc == 0)
240                         printf("0x%llx [%llu]\n", val, val);
241         }
242
243         return (rc);
244 }
245
246 static inline uint32_t
247 xtract(uint32_t val, int shift, int len)
248 {
249         return (val >> shift) & ((1 << len) - 1);
250 }
251
252 static int
253 dump_block_regs(const struct reg_info *reg_array, const uint32_t *regs)
254 {
255         uint32_t reg_val = 0;
256
257         for ( ; reg_array->name; ++reg_array)
258                 if (!reg_array->len) {
259                         reg_val = regs[reg_array->addr / 4];
260                         printf("[%#7x] %-47s %#-10x %u\n", reg_array->addr,
261                                reg_array->name, reg_val, reg_val);
262                 } else {
263                         uint32_t v = xtract(reg_val, reg_array->addr,
264                                             reg_array->len);
265
266                         printf("    %*u:%u %-47s %#-10x %u\n",
267                                reg_array->addr < 10 ? 3 : 2,
268                                reg_array->addr + reg_array->len - 1,
269                                reg_array->addr, reg_array->name, v, v);
270                 }
271
272         return (1);
273 }
274
275 static int
276 dump_regs_table(int argc, const char *argv[], const uint32_t *regs,
277     const struct mod_regs *modtab, int nmodules)
278 {
279         int i, j, match;
280
281         for (i = 0; i < argc; i++) {
282                 for (j = 0; j < nmodules; j++) {
283                         if (!strcmp(argv[i], modtab[j].name))
284                                 break;
285                 }
286
287                 if (j == nmodules) {
288                         warnx("invalid register block \"%s\"", argv[i]);
289                         fprintf(stderr, "\nAvailable blocks:");
290                         for ( ; nmodules; nmodules--, modtab++)
291                                 fprintf(stderr, " %s", modtab->name);
292                         fprintf(stderr, "\n");
293                         return (EINVAL);
294                 }
295         }
296
297         for ( ; nmodules; nmodules--, modtab++) {
298
299                 match = argc == 0 ? 1 : 0;
300                 for (i = 0; !match && i < argc; i++) {
301                         if (!strcmp(argv[i], modtab->name))
302                                 match = 1;
303                 }
304
305                 if (match)
306                         dump_block_regs(modtab->ri, regs);
307         }
308
309         return (0);
310 }
311
312 #define T4_MODREGS(name) { #name, t4_##name##_regs }
313 static int
314 dump_regs_t4(int argc, const char *argv[], const uint32_t *regs)
315 {
316         static struct mod_regs t4_mod[] = {
317                 T4_MODREGS(sge),
318                 { "pci", t4_pcie_regs },
319                 T4_MODREGS(dbg),
320                 T4_MODREGS(mc),
321                 T4_MODREGS(ma),
322                 { "edc0", t4_edc_0_regs },
323                 { "edc1", t4_edc_1_regs },
324                 T4_MODREGS(cim), 
325                 T4_MODREGS(tp),
326                 T4_MODREGS(ulp_rx),
327                 T4_MODREGS(ulp_tx),
328                 { "pmrx", t4_pm_rx_regs },
329                 { "pmtx", t4_pm_tx_regs },
330                 T4_MODREGS(mps),
331                 { "cplsw", t4_cpl_switch_regs },
332                 T4_MODREGS(smb),
333                 { "i2c", t4_i2cm_regs },
334                 T4_MODREGS(mi),
335                 T4_MODREGS(uart),
336                 T4_MODREGS(pmu), 
337                 T4_MODREGS(sf),
338                 T4_MODREGS(pl),
339                 T4_MODREGS(le),
340                 T4_MODREGS(ncsi),
341                 T4_MODREGS(xgmac)
342         };
343
344         return dump_regs_table(argc, argv, regs, t4_mod, ARRAY_SIZE(t4_mod));
345 }
346 #undef T4_MODREGS
347
348 static int
349 dump_regs_t4vf(int argc, const char *argv[], const uint32_t *regs)
350 {
351         static struct mod_regs t4vf_mod[] = {
352                 { "sge", t4vf_sge_regs },
353                 { "mps", t4vf_mps_regs },
354                 { "pl", t4vf_pl_regs },
355                 { "mbdata", t4vf_mbdata_regs },
356                 { "cim", t4vf_cim_regs },
357         };
358
359         return dump_regs_table(argc, argv, regs, t4vf_mod,
360             ARRAY_SIZE(t4vf_mod));
361 }
362
363 #define T5_MODREGS(name) { #name, t5_##name##_regs }
364 static int
365 dump_regs_t5(int argc, const char *argv[], const uint32_t *regs)
366 {
367         static struct mod_regs t5_mod[] = {
368                 T5_MODREGS(sge),
369                 { "pci", t5_pcie_regs },
370                 T5_MODREGS(dbg),
371                 { "mc0", t5_mc_0_regs },
372                 { "mc1", t5_mc_1_regs },
373                 T5_MODREGS(ma),
374                 { "edc0", t5_edc_t50_regs },
375                 { "edc1", t5_edc_t51_regs },
376                 T5_MODREGS(cim),
377                 T5_MODREGS(tp),
378                 { "ulprx", t5_ulp_rx_regs },
379                 { "ulptx", t5_ulp_tx_regs },
380                 { "pmrx", t5_pm_rx_regs },
381                 { "pmtx", t5_pm_tx_regs },
382                 T5_MODREGS(mps),
383                 { "cplsw", t5_cpl_switch_regs },
384                 T5_MODREGS(smb),
385                 { "i2c", t5_i2cm_regs },
386                 T5_MODREGS(mi),
387                 T5_MODREGS(uart),
388                 T5_MODREGS(pmu),
389                 T5_MODREGS(sf),
390                 T5_MODREGS(pl),
391                 T5_MODREGS(le),
392                 T5_MODREGS(ncsi),
393                 T5_MODREGS(mac),
394                 { "hma", t5_hma_t5_regs }
395         };
396
397         return dump_regs_table(argc, argv, regs, t5_mod, ARRAY_SIZE(t5_mod));
398 }
399 #undef T5_MODREGS
400
401 static int
402 dump_regs(int argc, const char *argv[])
403 {
404         int vers, revision, rc;
405         struct t4_regdump regs;
406         uint32_t len;
407
408         len = max(T4_REGDUMP_SIZE, T5_REGDUMP_SIZE);
409         regs.data = calloc(1, len);
410         if (regs.data == NULL) {
411                 warnc(ENOMEM, "regdump");
412                 return (ENOMEM);
413         }
414
415         regs.len = len;
416         rc = doit(CHELSIO_T4_REGDUMP, &regs);
417         if (rc != 0)
418                 return (rc);
419
420         vers = get_card_vers(regs.version);
421         revision = (regs.version >> 10) & 0x3f;
422
423         if (vers == 4) {
424                 if (revision == 0x3f)
425                         rc = dump_regs_t4vf(argc, argv, regs.data);
426                 else
427                         rc = dump_regs_t4(argc, argv, regs.data);
428         } else if (vers == 5)
429                 rc = dump_regs_t5(argc, argv, regs.data);
430         else {
431                 warnx("%s (type %d, rev %d) is not a known card.",
432                     nexus, vers, revision);
433                 return (ENOTSUP);
434         }
435
436         free(regs.data);
437         return (rc);
438 }
439
440 static void
441 do_show_info_header(uint32_t mode)
442 {
443         uint32_t i;
444
445         printf ("%4s %8s", "Idx", "Hits");
446         for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
447                 switch (mode & i) {
448                 case T4_FILTER_FCoE:
449                         printf (" FCoE");
450                         break;
451
452                 case T4_FILTER_PORT:
453                         printf (" Port");
454                         break;
455
456                 case T4_FILTER_VNIC:
457                         printf ("      vld:VNIC");
458                         break;
459
460                 case T4_FILTER_VLAN:
461                         printf ("      vld:VLAN");
462                         break;
463
464                 case T4_FILTER_IP_TOS:
465                         printf ("   TOS");
466                         break;
467
468                 case T4_FILTER_IP_PROTO:
469                         printf ("  Prot");
470                         break;
471
472                 case T4_FILTER_ETH_TYPE:
473                         printf ("   EthType");
474                         break;
475
476                 case T4_FILTER_MAC_IDX:
477                         printf ("  MACIdx");
478                         break;
479
480                 case T4_FILTER_MPS_HIT_TYPE:
481                         printf (" MPS");
482                         break;
483
484                 case T4_FILTER_IP_FRAGMENT:
485                         printf (" Frag");
486                         break;
487
488                 default:
489                         /* compressed filter field not enabled */
490                         break;
491                 }
492         }
493         printf(" %20s %20s %9s %9s %s\n",
494             "DIP", "SIP", "DPORT", "SPORT", "Action");
495 }
496
497 /*
498  * Parse an argument sub-vector as a { <parameter name> <value>[:<mask>] }
499  * ordered tuple.  If the parameter name in the argument sub-vector does not
500  * match the passed in parameter name, then a zero is returned for the
501  * function and no parsing is performed.  If there is a match, then the value
502  * and optional mask are parsed and returned in the provided return value
503  * pointers.  If no optional mask is specified, then a default mask of all 1s
504  * will be returned.
505  *
506  * An error in parsing the value[:mask] will result in an error message and
507  * program termination.
508  */
509 static int
510 parse_val_mask(const char *param, const char *args[], uint32_t *val,
511     uint32_t *mask)
512 {
513         char *p;
514
515         if (strcmp(param, args[0]) != 0)
516                 return (EINVAL);
517
518         *val = strtoul(args[1], &p, 0);
519         if (p > args[1]) {
520                 if (p[0] == 0) {
521                         *mask = ~0;
522                         return (0);
523                 }
524
525                 if (p[0] == ':' && p[1] != 0) {
526                         *mask = strtoul(p+1, &p, 0);
527                         if (p[0] == 0)
528                                 return (0);
529                 }
530         }
531
532         warnx("parameter \"%s\" has bad \"value[:mask]\" %s",
533             args[0], args[1]);
534
535         return (EINVAL);
536 }
537
538 /*
539  * Parse an argument sub-vector as a { <parameter name> <addr>[/<mask>] }
540  * ordered tuple.  If the parameter name in the argument sub-vector does not
541  * match the passed in parameter name, then a zero is returned for the
542  * function and no parsing is performed.  If there is a match, then the value
543  * and optional mask are parsed and returned in the provided return value
544  * pointers.  If no optional mask is specified, then a default mask of all 1s
545  * will be returned.
546  *
547  * The value return parameter "afp" is used to specify the expected address
548  * family -- IPv4 or IPv6 -- of the address[/mask] and return its actual
549  * format.  A passed in value of AF_UNSPEC indicates that either IPv4 or IPv6
550  * is acceptable; AF_INET means that only IPv4 addresses are acceptable; and
551  * AF_INET6 means that only IPv6 are acceptable.  AF_INET is returned for IPv4
552  * and AF_INET6 for IPv6 addresses, respectively.  IPv4 address/mask pairs are
553  * returned in the first four bytes of the address and mask return values with
554  * the address A.B.C.D returned with { A, B, C, D } returned in addresses { 0,
555  * 1, 2, 3}, respectively.
556  *
557  * An error in parsing the value[:mask] will result in an error message and
558  * program termination.
559  */
560 static int
561 parse_ipaddr(const char *param, const char *args[], int *afp, uint8_t addr[],
562     uint8_t mask[])
563 {
564         const char *colon, *afn;
565         char *slash;
566         uint8_t *m;
567         int af, ret;
568         unsigned int masksize;
569
570         /*
571          * Is this our parameter?
572          */
573         if (strcmp(param, args[0]) != 0)
574                 return (EINVAL);
575
576         /*
577          * Fundamental IPv4 versus IPv6 selection.
578          */
579         colon = strchr(args[1], ':');
580         if (!colon) {
581                 afn = "IPv4";
582                 af = AF_INET;
583                 masksize = 32;
584         } else {
585                 afn = "IPv6";
586                 af = AF_INET6;
587                 masksize = 128;
588         }
589         if (*afp == AF_UNSPEC)
590                 *afp = af;
591         else if (*afp != af) {
592                 warnx("address %s is not of expected family %s",
593                     args[1], *afp == AF_INET ? "IP" : "IPv6");
594                 return (EINVAL);
595         }
596
597         /*
598          * Parse address (temporarily stripping off any "/mask"
599          * specification).
600          */
601         slash = strchr(args[1], '/');
602         if (slash)
603                 *slash = 0;
604         ret = inet_pton(af, args[1], addr);
605         if (slash)
606                 *slash = '/';
607         if (ret <= 0) {
608                 warnx("Cannot parse %s %s address %s", param, afn, args[1]);
609                 return (EINVAL);
610         }
611
612         /*
613          * Parse optional mask specification.
614          */
615         if (slash) {
616                 char *p;
617                 unsigned int prefix = strtoul(slash + 1, &p, 10);
618
619                 if (p == slash + 1) {
620                         warnx("missing address prefix for %s", param);
621                         return (EINVAL);
622                 }
623                 if (*p) {
624                         warnx("%s is not a valid address prefix", slash + 1);
625                         return (EINVAL);
626                 }
627                 if (prefix > masksize) {
628                         warnx("prefix %u is too long for an %s address",
629                              prefix, afn);
630                         return (EINVAL);
631                 }
632                 memset(mask, 0, masksize / 8);
633                 masksize = prefix;
634         }
635
636         /*
637          * Fill in mask.
638          */
639         for (m = mask; masksize >= 8; m++, masksize -= 8)
640                 *m = ~0;
641         if (masksize)
642                 *m = ~0 << (8 - masksize);
643
644         return (0);
645 }
646
647 /*
648  * Parse an argument sub-vector as a { <parameter name> <value> } ordered
649  * tuple.  If the parameter name in the argument sub-vector does not match the
650  * passed in parameter name, then a zero is returned for the function and no
651  * parsing is performed.  If there is a match, then the value is parsed and
652  * returned in the provided return value pointer.
653  */
654 static int
655 parse_val(const char *param, const char *args[], uint32_t *val)
656 {
657         char *p;
658
659         if (strcmp(param, args[0]) != 0)
660                 return (EINVAL);
661
662         *val = strtoul(args[1], &p, 0);
663         if (p > args[1] && p[0] == 0)
664                 return (0);
665
666         warnx("parameter \"%s\" has bad \"value\" %s", args[0], args[1]);
667         return (EINVAL);
668 }
669
670 static void
671 filters_show_ipaddr(int type, uint8_t *addr, uint8_t *addrm)
672 {
673         int noctets, octet;
674
675         printf(" ");
676         if (type == 0) {
677                 noctets = 4;
678                 printf("%3s", " ");
679         } else
680         noctets = 16;
681
682         for (octet = 0; octet < noctets; octet++)
683                 printf("%02x", addr[octet]);
684         printf("/");
685         for (octet = 0; octet < noctets; octet++)
686                 printf("%02x", addrm[octet]);
687 }
688
689 static void
690 do_show_one_filter_info(struct t4_filter *t, uint32_t mode)
691 {
692         uint32_t i;
693
694         printf("%4d", t->idx);
695         if (t->hits == UINT64_MAX)
696                 printf(" %8s", "-");
697         else
698                 printf(" %8ju", t->hits);
699
700         /*
701          * Compressed header portion of filter.
702          */
703         for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
704                 switch (mode & i) {
705                 case T4_FILTER_FCoE:
706                         printf("  %1d/%1d", t->fs.val.fcoe, t->fs.mask.fcoe);
707                         break;
708
709                 case T4_FILTER_PORT:
710                         printf("  %1d/%1d", t->fs.val.iport, t->fs.mask.iport);
711                         break;
712
713                 case T4_FILTER_VNIC:
714                         printf(" %1d:%1x:%02x/%1d:%1x:%02x",
715                             t->fs.val.vnic_vld, (t->fs.val.vnic >> 7) & 0x7,
716                             t->fs.val.vnic & 0x7f, t->fs.mask.vnic_vld,
717                             (t->fs.mask.vnic >> 7) & 0x7,
718                             t->fs.mask.vnic & 0x7f);
719                         break;
720
721                 case T4_FILTER_VLAN:
722                         printf(" %1d:%04x/%1d:%04x",
723                             t->fs.val.vlan_vld, t->fs.val.vlan,
724                             t->fs.mask.vlan_vld, t->fs.mask.vlan);
725                         break;
726
727                 case T4_FILTER_IP_TOS:
728                         printf(" %02x/%02x", t->fs.val.tos, t->fs.mask.tos);
729                         break;
730
731                 case T4_FILTER_IP_PROTO:
732                         printf(" %02x/%02x", t->fs.val.proto, t->fs.mask.proto);
733                         break;
734
735                 case T4_FILTER_ETH_TYPE:
736                         printf(" %04x/%04x", t->fs.val.ethtype,
737                             t->fs.mask.ethtype);
738                         break;
739
740                 case T4_FILTER_MAC_IDX:
741                         printf(" %03x/%03x", t->fs.val.macidx,
742                             t->fs.mask.macidx);
743                         break;
744
745                 case T4_FILTER_MPS_HIT_TYPE:
746                         printf(" %1x/%1x", t->fs.val.matchtype,
747                             t->fs.mask.matchtype);
748                         break;
749
750                 case T4_FILTER_IP_FRAGMENT:
751                         printf("  %1d/%1d", t->fs.val.frag, t->fs.mask.frag);
752                         break;
753
754                 default:
755                         /* compressed filter field not enabled */
756                         break;
757                 }
758         }
759
760         /*
761          * Fixed portion of filter.
762          */
763         filters_show_ipaddr(t->fs.type, t->fs.val.dip, t->fs.mask.dip);
764         filters_show_ipaddr(t->fs.type, t->fs.val.sip, t->fs.mask.sip);
765         printf(" %04x/%04x %04x/%04x",
766                  t->fs.val.dport, t->fs.mask.dport,
767                  t->fs.val.sport, t->fs.mask.sport);
768
769         /*
770          * Variable length filter action.
771          */
772         if (t->fs.action == FILTER_DROP)
773                 printf(" Drop");
774         else if (t->fs.action == FILTER_SWITCH) {
775                 printf(" Switch: port=%d", t->fs.eport);
776         if (t->fs.newdmac)
777                 printf(
778                         ", dmac=%02x:%02x:%02x:%02x:%02x:%02x "
779                         ", l2tidx=%d",
780                         t->fs.dmac[0], t->fs.dmac[1],
781                         t->fs.dmac[2], t->fs.dmac[3],
782                         t->fs.dmac[4], t->fs.dmac[5],
783                         t->l2tidx);
784         if (t->fs.newsmac)
785                 printf(
786                         ", smac=%02x:%02x:%02x:%02x:%02x:%02x "
787                         ", smtidx=%d",
788                         t->fs.smac[0], t->fs.smac[1],
789                         t->fs.smac[2], t->fs.smac[3],
790                         t->fs.smac[4], t->fs.smac[5],
791                         t->smtidx);
792         if (t->fs.newvlan == VLAN_REMOVE)
793                 printf(", vlan=none");
794         else if (t->fs.newvlan == VLAN_INSERT)
795                 printf(", vlan=insert(%x)", t->fs.vlan);
796         else if (t->fs.newvlan == VLAN_REWRITE)
797                 printf(", vlan=rewrite(%x)", t->fs.vlan);
798         } else {
799                 printf(" Pass: Q=");
800                 if (t->fs.dirsteer == 0) {
801                         printf("RSS");
802                         if (t->fs.maskhash)
803                                 printf("(TCB=hash)");
804                 } else {
805                         printf("%d", t->fs.iq);
806                         if (t->fs.dirsteerhash == 0)
807                                 printf("(QID)");
808                         else
809                                 printf("(hash)");
810                 }
811         }
812         if (t->fs.prio)
813                 printf(" Prio");
814         if (t->fs.rpttid)
815                 printf(" RptTID");
816         printf("\n");
817 }
818
819 static int
820 show_filters(void)
821 {
822         uint32_t mode = 0, header = 0;
823         struct t4_filter t;
824         int rc;
825
826         /* Get the global filter mode first */
827         rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
828         if (rc != 0)
829                 return (rc);
830
831         t.idx = 0;
832         for (t.idx = 0; ; t.idx++) {
833                 rc = doit(CHELSIO_T4_GET_FILTER, &t);
834                 if (rc != 0 || t.idx == 0xffffffff)
835                         break;
836
837                 if (!header) {
838                         do_show_info_header(mode);
839                         header = 1;
840                 }
841                 do_show_one_filter_info(&t, mode);
842         };
843
844         return (rc);
845 }
846
847 static int
848 get_filter_mode(void)
849 {
850         uint32_t mode = 0;
851         int rc;
852
853         rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
854         if (rc != 0)
855                 return (rc);
856
857         if (mode & T4_FILTER_IPv4)
858                 printf("ipv4 ");
859
860         if (mode & T4_FILTER_IPv6)
861                 printf("ipv6 ");
862
863         if (mode & T4_FILTER_IP_SADDR)
864                 printf("sip ");
865         
866         if (mode & T4_FILTER_IP_DADDR)
867                 printf("dip ");
868
869         if (mode & T4_FILTER_IP_SPORT)
870                 printf("sport ");
871
872         if (mode & T4_FILTER_IP_DPORT)
873                 printf("dport ");
874
875         if (mode & T4_FILTER_IP_FRAGMENT)
876                 printf("frag ");
877
878         if (mode & T4_FILTER_MPS_HIT_TYPE)
879                 printf("matchtype ");
880
881         if (mode & T4_FILTER_MAC_IDX)
882                 printf("macidx ");
883
884         if (mode & T4_FILTER_ETH_TYPE)
885                 printf("ethtype ");
886
887         if (mode & T4_FILTER_IP_PROTO)
888                 printf("proto ");
889
890         if (mode & T4_FILTER_IP_TOS)
891                 printf("tos ");
892
893         if (mode & T4_FILTER_VLAN)
894                 printf("vlan ");
895
896         if (mode & T4_FILTER_VNIC)
897                 printf("vnic/ovlan ");
898
899         if (mode & T4_FILTER_PORT)
900                 printf("iport ");
901
902         if (mode & T4_FILTER_FCoE)
903                 printf("fcoe ");
904
905         printf("\n");
906
907         return (0);
908 }
909
910 static int
911 set_filter_mode(int argc, const char *argv[])
912 {
913         uint32_t mode = 0;
914
915         for (; argc; argc--, argv++) {
916                 if (!strcmp(argv[0], "frag"))
917                         mode |= T4_FILTER_IP_FRAGMENT;
918
919                 if (!strcmp(argv[0], "matchtype"))
920                         mode |= T4_FILTER_MPS_HIT_TYPE;
921
922                 if (!strcmp(argv[0], "macidx"))
923                         mode |= T4_FILTER_MAC_IDX;
924
925                 if (!strcmp(argv[0], "ethtype"))
926                         mode |= T4_FILTER_ETH_TYPE;
927
928                 if (!strcmp(argv[0], "proto"))
929                         mode |= T4_FILTER_IP_PROTO;
930
931                 if (!strcmp(argv[0], "tos"))
932                         mode |= T4_FILTER_IP_TOS;
933
934                 if (!strcmp(argv[0], "vlan"))
935                         mode |= T4_FILTER_VLAN;
936
937                 if (!strcmp(argv[0], "ovlan") ||
938                     !strcmp(argv[0], "vnic"))
939                         mode |= T4_FILTER_VNIC;
940
941                 if (!strcmp(argv[0], "iport"))
942                         mode |= T4_FILTER_PORT;
943
944                 if (!strcmp(argv[0], "fcoe"))
945                         mode |= T4_FILTER_FCoE;
946         }
947
948         return doit(CHELSIO_T4_SET_FILTER_MODE, &mode);
949 }
950
951 static int
952 del_filter(uint32_t idx)
953 {
954         struct t4_filter t;
955
956         t.idx = idx;
957
958         return doit(CHELSIO_T4_DEL_FILTER, &t);
959 }
960
961 static int
962 set_filter(uint32_t idx, int argc, const char *argv[])
963 {
964         int af = AF_UNSPEC, start_arg = 0;
965         struct t4_filter t;
966
967         if (argc < 2) {
968                 warnc(EINVAL, "%s", __func__);
969                 return (EINVAL);
970         };
971         bzero(&t, sizeof (t));
972         t.idx = idx;
973         t.fs.hitcnts = 1;
974
975         for (start_arg = 0; start_arg + 2 <= argc; start_arg += 2) {
976                 const char **args = &argv[start_arg];
977                 uint32_t val, mask;
978
979                 if (!strcmp(argv[start_arg], "type")) {
980                         int newaf;
981                         if (!strcasecmp(argv[start_arg + 1], "ipv4"))
982                                 newaf = AF_INET;
983                         else if (!strcasecmp(argv[start_arg + 1], "ipv6"))
984                                 newaf = AF_INET6;
985                         else {
986                                 warnx("invalid type \"%s\"; "
987                                     "must be one of \"ipv4\" or \"ipv6\"",
988                                     argv[start_arg + 1]);
989                                 return (EINVAL);
990                         }
991
992                         if (af != AF_UNSPEC && af != newaf) {
993                                 warnx("conflicting IPv4/IPv6 specifications.");
994                                 return (EINVAL);
995                         }
996                         af = newaf;
997                 } else if (!parse_val_mask("fcoe", args, &val, &mask)) {
998                         t.fs.val.fcoe = val;
999                         t.fs.mask.fcoe = mask;
1000                 } else if (!parse_val_mask("iport", args, &val, &mask)) {
1001                         t.fs.val.iport = val;
1002                         t.fs.mask.iport = mask;
1003                 } else if (!parse_val_mask("ovlan", args, &val, &mask)) {
1004                         t.fs.val.vnic = val;
1005                         t.fs.mask.vnic = mask;
1006                         t.fs.val.vnic_vld = 1;
1007                         t.fs.mask.vnic_vld = 1;
1008                 } else if (!parse_val_mask("vnic", args, &val, &mask)) {
1009                         t.fs.val.vnic = val;
1010                         t.fs.mask.vnic = mask;
1011                         t.fs.val.vnic_vld = 1;
1012                         t.fs.mask.vnic_vld = 1;
1013                 } else if (!parse_val_mask("ivlan", args, &val, &mask)) {
1014                         t.fs.val.vlan = val;
1015                         t.fs.mask.vlan = mask;
1016                         t.fs.val.vlan_vld = 1;
1017                         t.fs.mask.vlan_vld = 1;
1018                 } else if (!parse_val_mask("tos", args, &val, &mask)) {
1019                         t.fs.val.tos = val;
1020                         t.fs.mask.tos = mask;
1021                 } else if (!parse_val_mask("proto", args, &val, &mask)) {
1022                         t.fs.val.proto = val;
1023                         t.fs.mask.proto = mask;
1024                 } else if (!parse_val_mask("ethtype", args, &val, &mask)) {
1025                         t.fs.val.ethtype = val;
1026                         t.fs.mask.ethtype = mask;
1027                 } else if (!parse_val_mask("macidx", args, &val, &mask)) {
1028                         t.fs.val.macidx = val;
1029                         t.fs.mask.macidx = mask;
1030                 } else if (!parse_val_mask("matchtype", args, &val, &mask)) {
1031                         t.fs.val.matchtype = val;
1032                         t.fs.mask.matchtype = mask;
1033                 } else if (!parse_val_mask("frag", args, &val, &mask)) {
1034                         t.fs.val.frag = val;
1035                         t.fs.mask.frag = mask;
1036                 } else if (!parse_val_mask("dport", args, &val, &mask)) {
1037                         t.fs.val.dport = val;
1038                         t.fs.mask.dport = mask;
1039                 } else if (!parse_val_mask("sport", args, &val, &mask)) {
1040                         t.fs.val.sport = val;
1041                         t.fs.mask.sport = mask;
1042                 } else if (!parse_ipaddr("dip", args, &af, t.fs.val.dip,
1043                     t.fs.mask.dip)) {
1044                         /* nada */;
1045                 } else if (!parse_ipaddr("sip", args, &af, t.fs.val.sip,
1046                     t.fs.mask.sip)) {
1047                         /* nada */;
1048                 } else if (!strcmp(argv[start_arg], "action")) {
1049                         if (!strcmp(argv[start_arg + 1], "pass"))
1050                                 t.fs.action = FILTER_PASS;
1051                         else if (!strcmp(argv[start_arg + 1], "drop"))
1052                                 t.fs.action = FILTER_DROP;
1053                         else if (!strcmp(argv[start_arg + 1], "switch"))
1054                                 t.fs.action = FILTER_SWITCH;
1055                         else {
1056                                 warnx("invalid action \"%s\"; must be one of"
1057                                      " \"pass\", \"drop\" or \"switch\"",
1058                                      argv[start_arg + 1]);
1059                                 return (EINVAL);
1060                         }
1061                 } else if (!parse_val("hitcnts", args, &val)) {
1062                         t.fs.hitcnts = val;
1063                 } else if (!parse_val("prio", args, &val)) {
1064                         t.fs.prio = val;
1065                 } else if (!parse_val("rpttid", args, &val)) {
1066                         t.fs.rpttid = 1;
1067                 } else if (!parse_val("queue", args, &val)) {
1068                         t.fs.dirsteer = 1;
1069                         t.fs.iq = val;
1070                 } else if (!parse_val("tcbhash", args, &val)) {
1071                         t.fs.maskhash = 1;
1072                         t.fs.dirsteerhash = 1;
1073                 } else if (!parse_val("eport", args, &val)) {
1074                         t.fs.eport = val;
1075                 } else if (!strcmp(argv[start_arg], "dmac")) {
1076                         struct ether_addr *daddr;
1077
1078                         daddr = ether_aton(argv[start_arg + 1]);
1079                         if (daddr == NULL) {
1080                                 warnx("invalid dmac address \"%s\"",
1081                                     argv[start_arg + 1]);
1082                                 return (EINVAL);
1083                         }
1084                         memcpy(t.fs.dmac, daddr, ETHER_ADDR_LEN);
1085                         t.fs.newdmac = 1;
1086                 } else if (!strcmp(argv[start_arg], "smac")) {
1087                         struct ether_addr *saddr;
1088
1089                         saddr = ether_aton(argv[start_arg + 1]);
1090                         if (saddr == NULL) {
1091                                 warnx("invalid smac address \"%s\"",
1092                                     argv[start_arg + 1]);
1093                                 return (EINVAL);
1094                         }
1095                         memcpy(t.fs.smac, saddr, ETHER_ADDR_LEN);
1096                         t.fs.newsmac = 1;
1097                 } else if (!strcmp(argv[start_arg], "vlan")) {
1098                         char *p;
1099                         if (!strcmp(argv[start_arg + 1], "none")) {
1100                                 t.fs.newvlan = VLAN_REMOVE;
1101                         } else if (argv[start_arg + 1][0] == '=') {
1102                                 t.fs.newvlan = VLAN_REWRITE;
1103                         } else if (argv[start_arg + 1][0] == '+') {
1104                                 t.fs.newvlan = VLAN_INSERT;
1105                         } else if (isdigit(argv[start_arg + 1][0]) &&
1106                             !parse_val_mask("vlan", args, &val, &mask)) {
1107                                 t.fs.val.vlan = val;
1108                                 t.fs.mask.vlan = mask;
1109                                 t.fs.val.vlan_vld = 1;
1110                                 t.fs.mask.vlan_vld = 1;
1111                         } else {
1112                                 warnx("unknown vlan parameter \"%s\"; must"
1113                                      " be one of \"none\", \"=<vlan>\", "
1114                                      " \"+<vlan>\", or \"<vlan>\"",
1115                                      argv[start_arg + 1]);
1116                                 return (EINVAL);
1117                         }
1118                         if (t.fs.newvlan == VLAN_REWRITE ||
1119                             t.fs.newvlan == VLAN_INSERT) {
1120                                 t.fs.vlan = strtoul(argv[start_arg + 1] + 1,
1121                                     &p, 0);
1122                                 if (p == argv[start_arg + 1] + 1 || p[0] != 0) {
1123                                         warnx("invalid vlan \"%s\"",
1124                                              argv[start_arg + 1]);
1125                                         return (EINVAL);
1126                                 }
1127                         }
1128                 } else {
1129                         warnx("invalid parameter \"%s\"", argv[start_arg]);
1130                         return (EINVAL);
1131                 }
1132         }
1133         if (start_arg != argc) {
1134                 warnx("no value for \"%s\"", argv[start_arg]);
1135                 return (EINVAL);
1136         }
1137
1138         /*
1139          * Check basic sanity of option combinations.
1140          */
1141         if (t.fs.action != FILTER_SWITCH &&
1142             (t.fs.eport || t.fs.newdmac || t.fs.newsmac || t.fs.newvlan)) {
1143                 warnx("prio, port dmac, smac and vlan only make sense with"
1144                      " \"action switch\"");
1145                 return (EINVAL);
1146         }
1147         if (t.fs.action != FILTER_PASS &&
1148             (t.fs.rpttid || t.fs.dirsteer || t.fs.maskhash)) {
1149                 warnx("rpttid, queue and tcbhash don't make sense with"
1150                      " action \"drop\" or \"switch\"");
1151                 return (EINVAL);
1152         }
1153
1154         t.fs.type = (af == AF_INET6 ? 1 : 0); /* default IPv4 */
1155         return doit(CHELSIO_T4_SET_FILTER, &t);
1156 }
1157
1158 static int
1159 filter_cmd(int argc, const char *argv[])
1160 {
1161         long long val;
1162         uint32_t idx;
1163         char *s;
1164
1165         if (argc == 0) {
1166                 warnx("filter: no arguments.");
1167                 return (EINVAL);
1168         };
1169
1170         /* list */
1171         if (strcmp(argv[0], "list") == 0) {
1172                 if (argc != 1)
1173                         warnx("trailing arguments after \"list\" ignored.");
1174
1175                 return show_filters();
1176         }
1177
1178         /* mode */
1179         if (argc == 1 && strcmp(argv[0], "mode") == 0)
1180                 return get_filter_mode();
1181
1182         /* mode <mode> */
1183         if (strcmp(argv[0], "mode") == 0)
1184                 return set_filter_mode(argc - 1, argv + 1);
1185
1186         /* <idx> ... */
1187         s = str_to_number(argv[0], NULL, &val);
1188         if (*s || val > 0xffffffffU) {
1189                 warnx("\"%s\" is neither an index nor a filter subcommand.",
1190                     argv[0]);
1191                 return (EINVAL);
1192         }
1193         idx = (uint32_t) val;
1194
1195         /* <idx> delete|clear */
1196         if (argc == 2 &&
1197             (strcmp(argv[1], "delete") == 0 || strcmp(argv[1], "clear") == 0)) {
1198                 return del_filter(idx);
1199         }
1200
1201         /* <idx> [<param> <val>] ... */
1202         return set_filter(idx, argc - 1, argv + 1);
1203 }
1204
1205 /*
1206  * Shows the fields of a multi-word structure.  The structure is considered to
1207  * consist of @nwords 32-bit words (i.e, it's an (@nwords * 32)-bit structure)
1208  * whose fields are described by @fd.  The 32-bit words are given in @words
1209  * starting with the least significant 32-bit word.
1210  */
1211 static void
1212 show_struct(const uint32_t *words, int nwords, const struct field_desc *fd)
1213 {
1214         unsigned int w = 0;
1215         const struct field_desc *p;
1216
1217         for (p = fd; p->name; p++)
1218                 w = max(w, strlen(p->name));
1219
1220         while (fd->name) {
1221                 unsigned long long data;
1222                 int first_word = fd->start / 32;
1223                 int shift = fd->start % 32;
1224                 int width = fd->end - fd->start + 1;
1225                 unsigned long long mask = (1ULL << width) - 1;
1226
1227                 data = (words[first_word] >> shift) |
1228                        ((uint64_t)words[first_word + 1] << (32 - shift));
1229                 if (shift)
1230                        data |= ((uint64_t)words[first_word + 2] << (64 - shift));
1231                 data &= mask;
1232                 if (fd->islog2)
1233                         data = 1 << data;
1234                 printf("%-*s ", w, fd->name);
1235                 printf(fd->hex ? "%#llx\n" : "%llu\n", data << fd->shift);
1236                 fd++;
1237         }
1238 }
1239
1240 #define FIELD(name, start, end) { name, start, end, 0, 0, 0 }
1241 #define FIELD1(name, start) FIELD(name, start, start)
1242
1243 static void
1244 show_sge_context(const struct t4_sge_context *p)
1245 {
1246         static struct field_desc egress[] = {
1247                 FIELD1("StatusPgNS:", 180),
1248                 FIELD1("StatusPgRO:", 179),
1249                 FIELD1("FetchNS:", 178),
1250                 FIELD1("FetchRO:", 177),
1251                 FIELD1("Valid:", 176),
1252                 FIELD("PCIeDataChannel:", 174, 175),
1253                 FIELD1("DCAEgrQEn:", 173),
1254                 FIELD("DCACPUID:", 168, 172),
1255                 FIELD1("FCThreshOverride:", 167),
1256                 FIELD("WRLength:", 162, 166),
1257                 FIELD1("WRLengthKnown:", 161),
1258                 FIELD1("ReschedulePending:", 160),
1259                 FIELD1("OnChipQueue:", 159),
1260                 FIELD1("FetchSizeMode", 158),
1261                 { "FetchBurstMin:", 156, 157, 4, 0, 1 },
1262                 { "FetchBurstMax:", 153, 154, 6, 0, 1 },
1263                 FIELD("uPToken:", 133, 152),
1264                 FIELD1("uPTokenEn:", 132),
1265                 FIELD1("UserModeIO:", 131),
1266                 FIELD("uPFLCredits:", 123, 130),
1267                 FIELD1("uPFLCreditEn:", 122),
1268                 FIELD("FID:", 111, 121),
1269                 FIELD("HostFCMode:", 109, 110),
1270                 FIELD1("HostFCOwner:", 108),
1271                 { "CIDXFlushThresh:", 105, 107, 0, 0, 1 },
1272                 FIELD("CIDX:", 89, 104),
1273                 FIELD("PIDX:", 73, 88),
1274                 { "BaseAddress:", 18, 72, 9, 1 },
1275                 FIELD("QueueSize:", 2, 17),
1276                 FIELD1("QueueType:", 1),
1277                 FIELD1("CachePriority:", 0),
1278                 { NULL }
1279         };
1280         static struct field_desc fl[] = {
1281                 FIELD1("StatusPgNS:", 180),
1282                 FIELD1("StatusPgRO:", 179),
1283                 FIELD1("FetchNS:", 178),
1284                 FIELD1("FetchRO:", 177),
1285                 FIELD1("Valid:", 176),
1286                 FIELD("PCIeDataChannel:", 174, 175),
1287                 FIELD1("DCAEgrQEn:", 173),
1288                 FIELD("DCACPUID:", 168, 172),
1289                 FIELD1("FCThreshOverride:", 167),
1290                 FIELD("WRLength:", 162, 166),
1291                 FIELD1("WRLengthKnown:", 161),
1292                 FIELD1("ReschedulePending:", 160),
1293                 FIELD1("OnChipQueue:", 159),
1294                 FIELD1("FetchSizeMode", 158),
1295                 { "FetchBurstMin:", 156, 157, 4, 0, 1 },
1296                 { "FetchBurstMax:", 153, 154, 6, 0, 1 },
1297                 FIELD1("FLMcongMode:", 152),
1298                 FIELD("MaxuPFLCredits:", 144, 151),
1299                 FIELD("FLMcontextID:", 133, 143),
1300                 FIELD1("uPTokenEn:", 132),
1301                 FIELD1("UserModeIO:", 131),
1302                 FIELD("uPFLCredits:", 123, 130),
1303                 FIELD1("uPFLCreditEn:", 122),
1304                 FIELD("FID:", 111, 121),
1305                 FIELD("HostFCMode:", 109, 110),
1306                 FIELD1("HostFCOwner:", 108),
1307                 { "CIDXFlushThresh:", 105, 107, 0, 0, 1 },
1308                 FIELD("CIDX:", 89, 104),
1309                 FIELD("PIDX:", 73, 88),
1310                 { "BaseAddress:", 18, 72, 9, 1 },
1311                 FIELD("QueueSize:", 2, 17),
1312                 FIELD1("QueueType:", 1),
1313                 FIELD1("CachePriority:", 0),
1314                 { NULL }
1315         };
1316         static struct field_desc ingress[] = {
1317                 FIELD1("NoSnoop:", 145),
1318                 FIELD1("RelaxedOrdering:", 144),
1319                 FIELD1("GTSmode:", 143),
1320                 FIELD1("ISCSICoalescing:", 142),
1321                 FIELD1("Valid:", 141),
1322                 FIELD1("TimerPending:", 140),
1323                 FIELD1("DropRSS:", 139),
1324                 FIELD("PCIeChannel:", 137, 138),
1325                 FIELD1("SEInterruptArmed:", 136),
1326                 FIELD1("CongestionMgtEnable:", 135),
1327                 FIELD1("DCAIngQEnable:", 134),
1328                 FIELD("DCACPUID:", 129, 133),
1329                 FIELD1("UpdateScheduling:", 128),
1330                 FIELD("UpdateDelivery:", 126, 127),
1331                 FIELD1("InterruptSent:", 125),
1332                 FIELD("InterruptIDX:", 114, 124),
1333                 FIELD1("InterruptDestination:", 113),
1334                 FIELD1("InterruptArmed:", 112),
1335                 FIELD("RxIntCounter:", 106, 111),
1336                 FIELD("RxIntCounterThreshold:", 104, 105),
1337                 FIELD1("Generation:", 103),
1338                 { "BaseAddress:", 48, 102, 9, 1 },
1339                 FIELD("PIDX:", 32, 47),
1340                 FIELD("CIDX:", 16, 31),
1341                 { "QueueSize:", 4, 15, 4, 0 },
1342                 { "QueueEntrySize:", 2, 3, 4, 0, 1 },
1343                 FIELD1("QueueEntryOverride:", 1),
1344                 FIELD1("CachePriority:", 0),
1345                 { NULL }
1346         };
1347         static struct field_desc flm[] = {
1348                 FIELD1("NoSnoop:", 79),
1349                 FIELD1("RelaxedOrdering:", 78),
1350                 FIELD1("Valid:", 77),
1351                 FIELD("DCACPUID:", 72, 76),
1352                 FIELD1("DCAFLEn:", 71),
1353                 FIELD("EQid:", 54, 70),
1354                 FIELD("SplitEn:", 52, 53),
1355                 FIELD1("PadEn:", 51),
1356                 FIELD1("PackEn:", 50),
1357                 FIELD1("DBpriority:", 48),
1358                 FIELD("PackOffset:", 16, 47),
1359                 FIELD("CIDX:", 8, 15),
1360                 FIELD("PIDX:", 0, 7),
1361                 { NULL }
1362         };
1363         static struct field_desc conm[] = {
1364                 FIELD1("CngDBPHdr:", 6),
1365                 FIELD1("CngDBPData:", 5),
1366                 FIELD1("CngIMSG:", 4),
1367                 FIELD("CngChMap:", 0, 3),
1368                 { NULL }
1369         };
1370         static struct field_desc t5_conm[] = {
1371                 FIELD1("CngMPSEnable:", 21),
1372                 FIELD("CngTPMode:", 19, 20),
1373                 FIELD1("CngDBPHdr:", 18),
1374                 FIELD1("CngDBPData:", 17),
1375                 FIELD1("CngIMSG:", 16),
1376                 FIELD("CngChMap:", 0, 15),
1377                 { NULL }
1378         };
1379
1380         if (p->mem_id == SGE_CONTEXT_EGRESS)
1381                 show_struct(p->data, 6, (p->data[0] & 2) ? fl : egress);
1382         else if (p->mem_id == SGE_CONTEXT_FLM)
1383                 show_struct(p->data, 3, flm);
1384         else if (p->mem_id == SGE_CONTEXT_INGRESS)
1385                 show_struct(p->data, 5, ingress);
1386         else if (p->mem_id == SGE_CONTEXT_CNM)
1387                 show_struct(p->data, 1, chip_id == 5 ? t5_conm : conm);
1388 }
1389
1390 #undef FIELD
1391 #undef FIELD1
1392
1393 static int
1394 get_sge_context(int argc, const char *argv[])
1395 {
1396         int rc;
1397         char *p;
1398         long cid;
1399         struct t4_sge_context cntxt = {0};
1400
1401         if (argc != 2) {
1402                 warnx("sge_context: incorrect number of arguments.");
1403                 return (EINVAL);
1404         }
1405
1406         if (!strcmp(argv[0], "egress"))
1407                 cntxt.mem_id = SGE_CONTEXT_EGRESS;
1408         else if (!strcmp(argv[0], "ingress"))
1409                 cntxt.mem_id = SGE_CONTEXT_INGRESS;
1410         else if (!strcmp(argv[0], "fl"))
1411                 cntxt.mem_id = SGE_CONTEXT_FLM;
1412         else if (!strcmp(argv[0], "cong"))
1413                 cntxt.mem_id = SGE_CONTEXT_CNM;
1414         else {
1415                 warnx("unknown context type \"%s\"; known types are egress, "
1416                     "ingress, fl, and cong.", argv[0]);
1417                 return (EINVAL);
1418         }
1419
1420         p = str_to_number(argv[1], &cid, NULL);
1421         if (*p) {
1422                 warnx("invalid context id \"%s\"", argv[1]);
1423                 return (EINVAL);
1424         }
1425         cntxt.cid = cid;
1426
1427         rc = doit(CHELSIO_T4_GET_SGE_CONTEXT, &cntxt);
1428         if (rc != 0)
1429                 return (rc);
1430
1431         show_sge_context(&cntxt);
1432         return (0);
1433 }
1434
1435 static int
1436 loadfw(int argc, const char *argv[])
1437 {
1438         int rc, fd;
1439         struct t4_data data = {0};
1440         const char *fname = argv[0];
1441         struct stat st = {0};
1442
1443         if (argc != 1) {
1444                 warnx("loadfw: incorrect number of arguments.");
1445                 return (EINVAL);
1446         }
1447
1448         fd = open(fname, O_RDONLY);
1449         if (fd < 0) {
1450                 warn("open(%s)", fname);
1451                 return (errno);
1452         }
1453
1454         if (fstat(fd, &st) < 0) {
1455                 warn("fstat");
1456                 close(fd);
1457                 return (errno);
1458         }
1459
1460         data.len = st.st_size;
1461         data.data = mmap(0, data.len, PROT_READ, 0, fd, 0);
1462         if (data.data == MAP_FAILED) {
1463                 warn("mmap");
1464                 close(fd);
1465                 return (errno);
1466         }
1467
1468         rc = doit(CHELSIO_T4_LOAD_FW, &data);
1469         munmap(data.data, data.len);
1470         close(fd);
1471         return (rc);
1472 }
1473
1474 static int
1475 read_mem(uint32_t addr, uint32_t len, void (*output)(uint32_t *, uint32_t))
1476 {
1477         int rc;
1478         struct t4_mem_range mr;
1479
1480         mr.addr = addr;
1481         mr.len = len;
1482         mr.data = malloc(mr.len);
1483
1484         if (mr.data == 0) {
1485                 warn("read_mem: malloc");
1486                 return (errno);
1487         }
1488
1489         rc = doit(CHELSIO_T4_GET_MEM, &mr);
1490         if (rc != 0)
1491                 goto done;
1492
1493         if (output)
1494                 (*output)(mr.data, mr.len);
1495 done:
1496         free(mr.data);
1497         return (rc);
1498 }
1499
1500 /*
1501  * Display memory as list of 'n' 4-byte values per line.
1502  */
1503 static void
1504 show_mem(uint32_t *buf, uint32_t len)
1505 {
1506         const char *s;
1507         int i, n = 8;
1508
1509         while (len) {
1510                 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1511                         s = i ? " " : "";
1512                         printf("%s%08x", s, htonl(*buf));
1513                 }
1514                 printf("\n");
1515         }
1516 }
1517
1518 static int
1519 memdump(int argc, const char *argv[])
1520 {
1521         char *p;
1522         long l;
1523         uint32_t addr, len;
1524
1525         if (argc != 2) {
1526                 warnx("incorrect number of arguments.");
1527                 return (EINVAL);
1528         }
1529
1530         p = str_to_number(argv[0], &l, NULL);
1531         if (*p) {
1532                 warnx("invalid address \"%s\"", argv[0]);
1533                 return (EINVAL);
1534         }
1535         addr = l;
1536
1537         p = str_to_number(argv[1], &l, NULL);
1538         if (*p) {
1539                 warnx("memdump: invalid length \"%s\"", argv[1]);
1540                 return (EINVAL);
1541         }
1542         len = l;
1543
1544         return (read_mem(addr, len, show_mem));
1545 }
1546
1547 /*
1548  * Display TCB as list of 'n' 4-byte values per line.
1549  */
1550 static void
1551 show_tcb(uint32_t *buf, uint32_t len)
1552 {
1553         const char *s;
1554         int i, n = 8;
1555
1556         while (len) {
1557                 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1558                         s = i ? " " : "";
1559                         printf("%s%08x", s, htonl(*buf));
1560                 }
1561                 printf("\n");
1562         }
1563 }
1564
1565 #define A_TP_CMM_TCB_BASE 0x7d10
1566 #define TCB_SIZE 128
1567 static int
1568 read_tcb(int argc, const char *argv[])
1569 {
1570         char *p;
1571         long l;
1572         long long val;
1573         unsigned int tid;
1574         uint32_t addr;
1575         int rc;
1576
1577         if (argc != 1) {
1578                 warnx("incorrect number of arguments.");
1579                 return (EINVAL);
1580         }
1581
1582         p = str_to_number(argv[0], &l, NULL);
1583         if (*p) {
1584                 warnx("invalid tid \"%s\"", argv[0]);
1585                 return (EINVAL);
1586         }
1587         tid = l;
1588
1589         rc = read_reg(A_TP_CMM_TCB_BASE, 4, &val);
1590         if (rc != 0)
1591                 return (rc);
1592
1593         addr = val + tid * TCB_SIZE;
1594
1595         return (read_mem(addr, TCB_SIZE, show_tcb));
1596 }
1597
1598 static int
1599 read_i2c(int argc, const char *argv[])
1600 {
1601         char *p;
1602         long l;
1603         struct t4_i2c_data i2cd;
1604         int rc, i;
1605
1606         if (argc < 3 || argc > 4) {
1607                 warnx("incorrect number of arguments.");
1608                 return (EINVAL);
1609         }
1610
1611         p = str_to_number(argv[0], &l, NULL);
1612         if (*p || l > UCHAR_MAX) {
1613                 warnx("invalid port id \"%s\"", argv[0]);
1614                 return (EINVAL);
1615         }
1616         i2cd.port_id = l;
1617
1618         p = str_to_number(argv[1], &l, NULL);
1619         if (*p || l > UCHAR_MAX) {
1620                 warnx("invalid i2c device address \"%s\"", argv[1]);
1621                 return (EINVAL);
1622         }
1623         i2cd.dev_addr = l;
1624
1625         p = str_to_number(argv[2], &l, NULL);
1626         if (*p || l > UCHAR_MAX) {
1627                 warnx("invalid byte offset \"%s\"", argv[2]);
1628                 return (EINVAL);
1629         }
1630         i2cd.offset = l;
1631
1632         if (argc == 4) {
1633                 p = str_to_number(argv[3], &l, NULL);
1634                 if (*p || l > sizeof(i2cd.data)) {
1635                         warnx("invalid number of bytes \"%s\"", argv[3]);
1636                         return (EINVAL);
1637                 }
1638                 i2cd.len = l;
1639         } else
1640                 i2cd.len = 1;
1641
1642         rc = doit(CHELSIO_T4_GET_I2C, &i2cd);
1643         if (rc != 0)
1644                 return (rc);
1645
1646         for (i = 0; i < i2cd.len; i++)
1647                 printf("0x%x [%u]\n", i2cd.data[i], i2cd.data[i]);
1648
1649         return (0);
1650 }
1651
1652 static int
1653 clearstats(int argc, const char *argv[])
1654 {
1655         char *p;
1656         long l;
1657         uint32_t port;
1658
1659         if (argc != 1) {
1660                 warnx("incorrect number of arguments.");
1661                 return (EINVAL);
1662         }
1663
1664         p = str_to_number(argv[0], &l, NULL);
1665         if (*p) {
1666                 warnx("invalid port id \"%s\"", argv[0]);
1667                 return (EINVAL);
1668         }
1669         port = l;
1670
1671         return doit(CHELSIO_T4_CLEAR_STATS, &port);
1672 }
1673
1674 static int
1675 show_tracers(void)
1676 {
1677         struct t4_tracer t;
1678         char *s;
1679         int rc, port_idx, i;
1680         long long val;
1681
1682         /* Magic values: MPS_TRC_CFG = 0x9800. MPS_TRC_CFG[1:1] = TrcEn */
1683         rc = read_reg(0x9800, 4, &val);
1684         if (rc != 0)
1685                 return (rc);
1686         printf("tracing is %s\n", val & 2 ? "ENABLED" : "DISABLED");
1687
1688         t.idx = 0;
1689         for (t.idx = 0; ; t.idx++) {
1690                 rc = doit(CHELSIO_T4_GET_TRACER, &t);
1691                 if (rc != 0 || t.idx == 0xff)
1692                         break;
1693
1694                 if (t.tp.port < 4) {
1695                         s = "Rx";
1696                         port_idx = t.tp.port;
1697                 } else if (t.tp.port < 8) {
1698                         s = "Tx";
1699                         port_idx = t.tp.port - 4;
1700                 } else if (t.tp.port < 12) {
1701                         s = "loopback";
1702                         port_idx = t.tp.port - 8;
1703                 } else if (t.tp.port < 16) {
1704                         s = "MPS Rx";
1705                         port_idx = t.tp.port - 12;
1706                 } else if (t.tp.port < 20) {
1707                         s = "MPS Tx";
1708                         port_idx = t.tp.port - 16;
1709                 } else {
1710                         s = "unknown";
1711                         port_idx = t.tp.port;
1712                 }
1713
1714                 printf("\ntracer %u (currently %s) captures ", t.idx,
1715                     t.enabled ? "ENABLED" : "DISABLED");
1716                 if (t.tp.port < 8)
1717                         printf("port %u %s, ", port_idx, s);
1718                 else
1719                         printf("%s %u, ", s, port_idx);
1720                 printf("snap length: %u, min length: %u\n", t.tp.snap_len,
1721                     t.tp.min_len);
1722                 printf("packets captured %smatch filter\n",
1723                     t.tp.invert ? "do not " : "");
1724                 if (t.tp.skip_ofst) {
1725                         printf("filter pattern: ");
1726                         for (i = 0; i < t.tp.skip_ofst * 2; i += 2)
1727                                 printf("%08x%08x", t.tp.data[i],
1728                                     t.tp.data[i + 1]);
1729                         printf("/");
1730                         for (i = 0; i < t.tp.skip_ofst * 2; i += 2)
1731                                 printf("%08x%08x", t.tp.mask[i],
1732                                     t.tp.mask[i + 1]);
1733                         printf("@0\n");
1734                 }
1735                 printf("filter pattern: ");
1736                 for (i = t.tp.skip_ofst * 2; i < T4_TRACE_LEN / 4; i += 2)
1737                         printf("%08x%08x", t.tp.data[i], t.tp.data[i + 1]);
1738                 printf("/");
1739                 for (i = t.tp.skip_ofst * 2; i < T4_TRACE_LEN / 4; i += 2)
1740                         printf("%08x%08x", t.tp.mask[i], t.tp.mask[i + 1]);
1741                 printf("@%u\n", (t.tp.skip_ofst + t.tp.skip_len) * 8);
1742         }
1743
1744         return (rc);
1745 }
1746
1747 static int
1748 tracer_onoff(uint8_t idx, int enabled)
1749 {
1750         struct t4_tracer t;
1751
1752         t.idx = idx;
1753         t.enabled = enabled;
1754         t.valid = 0;
1755
1756         return doit(CHELSIO_T4_SET_TRACER, &t);
1757 }
1758
1759 static void
1760 create_tracing_ifnet()
1761 {
1762         char *cmd[] = {
1763                 "/sbin/ifconfig", __DECONST(char *, nexus), "create", NULL
1764         };
1765         char *env[] = {NULL};
1766
1767         if (vfork() == 0) {
1768                 close(STDERR_FILENO);
1769                 execve(cmd[0], cmd, env);
1770                 _exit(0);
1771         }
1772 }
1773
1774 /*
1775  * XXX: Allow user to specify snaplen, minlen, and pattern (including inverted
1776  * matching).  Right now this is a quick-n-dirty implementation that traces the
1777  * first 128B of all tx or rx on a port
1778  */
1779 static int
1780 set_tracer(uint8_t idx, int argc, const char *argv[])
1781 {
1782         struct t4_tracer t;
1783         int len, port;
1784
1785         bzero(&t, sizeof (t));
1786         t.idx = idx;
1787         t.enabled = 1;
1788         t.valid = 1;
1789
1790         if (argc != 1) {
1791                 warnx("must specify tx<n> or rx<n>.");
1792                 return (EINVAL);
1793         }
1794
1795         len = strlen(argv[0]);
1796         if (len != 3) {
1797                 warnx("argument must be 3 characters (tx<n> or rx<n>)");
1798                 return (EINVAL);
1799         }
1800
1801         if (strncmp(argv[0], "tx", 2) == 0) {
1802                 port = argv[0][2] - '0';
1803                 if (port < 0 || port > 3) {
1804                         warnx("'%c' in %s is invalid", argv[0][2], argv[0]);
1805                         return (EINVAL);
1806                 }
1807                 port += 4;
1808         } else if (strncmp(argv[0], "rx", 2) == 0) {
1809                 port = argv[0][2] - '0';
1810                 if (port < 0 || port > 3) {
1811                         warnx("'%c' in %s is invalid", argv[0][2], argv[0]);
1812                         return (EINVAL);
1813                 }
1814         } else {
1815                 warnx("argument '%s' isn't tx<n> or rx<n>", argv[0]);
1816                 return (EINVAL);
1817         }
1818
1819         t.tp.snap_len = 128;
1820         t.tp.min_len = 0;
1821         t.tp.skip_ofst = 0;
1822         t.tp.skip_len = 0;
1823         t.tp.invert = 0;
1824         t.tp.port = port;
1825
1826         create_tracing_ifnet();
1827         return doit(CHELSIO_T4_SET_TRACER, &t);
1828 }
1829
1830 static int
1831 tracer_cmd(int argc, const char *argv[])
1832 {
1833         long long val;
1834         uint8_t idx;
1835         char *s;
1836
1837         if (argc == 0) {
1838                 warnx("tracer: no arguments.");
1839                 return (EINVAL);
1840         };
1841
1842         /* list */
1843         if (strcmp(argv[0], "list") == 0) {
1844                 if (argc != 1)
1845                         warnx("trailing arguments after \"list\" ignored.");
1846
1847                 return show_tracers();
1848         }
1849
1850         /* <idx> ... */
1851         s = str_to_number(argv[0], NULL, &val);
1852         if (*s || val > 0xff) {
1853                 warnx("\"%s\" is neither an index nor a tracer subcommand.",
1854                     argv[0]);
1855                 return (EINVAL);
1856         }
1857         idx = (int8_t)val;
1858
1859         /* <idx> disable */
1860         if (argc == 2 && strcmp(argv[1], "disable") == 0)
1861                 return tracer_onoff(idx, 0);
1862
1863         /* <idx> enable */
1864         if (argc == 2 && strcmp(argv[1], "enable") == 0)
1865                 return tracer_onoff(idx, 1);
1866
1867         /* <idx> ... */
1868         return set_tracer(idx, argc - 1, argv + 1);
1869 }
1870
1871 static int
1872 run_cmd(int argc, const char *argv[])
1873 {
1874         int rc = -1;
1875         const char *cmd = argv[0];
1876
1877         /* command */
1878         argc--;
1879         argv++;
1880
1881         if (!strcmp(cmd, "reg") || !strcmp(cmd, "reg32"))
1882                 rc = register_io(argc, argv, 4);
1883         else if (!strcmp(cmd, "reg64"))
1884                 rc = register_io(argc, argv, 8);
1885         else if (!strcmp(cmd, "regdump"))
1886                 rc = dump_regs(argc, argv);
1887         else if (!strcmp(cmd, "filter"))
1888                 rc = filter_cmd(argc, argv);
1889         else if (!strcmp(cmd, "context"))
1890                 rc = get_sge_context(argc, argv);
1891         else if (!strcmp(cmd, "loadfw"))
1892                 rc = loadfw(argc, argv);
1893         else if (!strcmp(cmd, "memdump"))
1894                 rc = memdump(argc, argv);
1895         else if (!strcmp(cmd, "tcb"))
1896                 rc = read_tcb(argc, argv);
1897         else if (!strcmp(cmd, "i2c"))
1898                 rc = read_i2c(argc, argv);
1899         else if (!strcmp(cmd, "clearstats"))
1900                 rc = clearstats(argc, argv);
1901         else if (!strcmp(cmd, "tracer"))
1902                 rc = tracer_cmd(argc, argv);
1903         else {
1904                 rc = EINVAL;
1905                 warnx("invalid command \"%s\"", cmd);
1906         }
1907
1908         return (rc);
1909 }
1910
1911 #define MAX_ARGS 15
1912 static int
1913 run_cmd_loop(void)
1914 {
1915         int i, rc = 0;
1916         char buffer[128], *buf;
1917         const char *args[MAX_ARGS + 1];
1918
1919         /*
1920          * Simple loop: displays a "> " prompt and processes any input as a
1921          * cxgbetool command.  You're supposed to enter only the part after
1922          * "cxgbetool t4nexX".  Use "quit" or "exit" to exit.
1923          */
1924         for (;;) {
1925                 fprintf(stdout, "> ");
1926                 fflush(stdout);
1927                 buf = fgets(buffer, sizeof(buffer), stdin);
1928                 if (buf == NULL) {
1929                         if (ferror(stdin)) {
1930                                 warn("stdin error");
1931                                 rc = errno;     /* errno from fgets */
1932                         }
1933                         break;
1934                 }
1935
1936                 i = 0;
1937                 while ((args[i] = strsep(&buf, " \t\n")) != NULL) {
1938                         if (args[i][0] != 0 && ++i == MAX_ARGS)
1939                                 break;
1940                 }
1941                 args[i] = 0;
1942
1943                 if (i == 0)
1944                         continue;       /* skip empty line */
1945
1946                 if (!strcmp(args[0], "quit") || !strcmp(args[0], "exit"))
1947                         break;
1948
1949                 rc = run_cmd(i, args);
1950         }
1951
1952         /* rc normally comes from the last command (not including quit/exit) */
1953         return (rc);
1954 }
1955
1956 int
1957 main(int argc, const char *argv[])
1958 {
1959         int rc = -1;
1960
1961         progname = argv[0];
1962
1963         if (argc == 2) {
1964                 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1965                         usage(stdout);
1966                         exit(0);
1967                 }
1968         }
1969
1970         if (argc < 3) {
1971                 usage(stderr);
1972                 exit(EINVAL);
1973         }
1974
1975         nexus = argv[1];
1976
1977         /* progname and nexus */
1978         argc -= 2;
1979         argv += 2;
1980
1981         if (argc == 1 && !strcmp(argv[0], "stdio"))
1982                 rc = run_cmd_loop();
1983         else
1984                 rc = run_cmd(argc, argv);
1985
1986         return (rc);
1987 }