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