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