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