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