2 * Copyright (c) 2011 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
40 #include <sys/ioctl.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
46 #include <net/ethernet.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <net/sff8472.h>
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))
57 static const char *progname, *nexus;
58 static int chip_id; /* 4 for T4, 5 for T5 */
68 const struct reg_info *ri;
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 */
80 #include "reg_defs_t4.c"
81 #include "reg_defs_t4vf.c"
82 #include "reg_defs_t5.c"
87 fprintf(fp, "Usage: %s <nexus> [operation]\n", progname);
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"
112 static inline unsigned int
113 get_card_vers(unsigned int version)
115 return (version & 0x3ff);
119 real_doit(unsigned long cmd, void *data, const char *cmdstr)
127 snprintf(buf, sizeof(buf), "/dev/%s", nexus);
128 if ((fd = open(buf, O_RDWR)) < 0) {
129 warn("open(%s)", nexus);
133 chip_id = nexus[1] - '0';
136 rc = ioctl(fd, cmd, data);
144 #define doit(x, y) real_doit(x, y, #x)
147 str_to_number(const char *s, long *val, long long *vall)
152 *vall = strtoll(s, &p, 0);
154 *val = strtol(s, &p, 0);
162 read_reg(long addr, int size, long long *val)
167 reg.addr = (uint32_t) addr;
168 reg.size = (uint32_t) size;
171 rc = doit(CHELSIO_T4_GETREG, ®);
179 write_reg(long addr, int size, long long val)
183 reg.addr = (uint32_t) addr;
184 reg.size = (uint32_t) size;
185 reg.val = (uint64_t) val;
187 return doit(CHELSIO_T4_SETREG, ®);
191 register_io(int argc, const char *argv[], int size)
199 /* <reg> OR <reg>=<value> */
201 p = str_to_number(argv[0], &addr, NULL);
204 warnx("invalid register \"%s\"", argv[0]);
210 p = str_to_number(v, NULL, &val);
213 warnx("invalid value \"%s\"", v);
218 } else if (argc == 2) {
223 p = str_to_number(argv[0], &addr, NULL);
225 warnx("invalid register \"%s\"", argv[0]);
229 p = str_to_number(argv[1], NULL, &val);
231 warnx("invalid value \"%s\"", argv[1]);
235 warnx("reg: invalid number of arguments (%d)", argc);
240 rc = write_reg(addr, size, val);
242 rc = read_reg(addr, size, &val);
244 printf("0x%llx [%llu]\n", val, val);
250 static inline uint32_t
251 xtract(uint32_t val, int shift, int len)
253 return (val >> shift) & ((1 << len) - 1);
257 dump_block_regs(const struct reg_info *reg_array, const uint32_t *regs)
259 uint32_t reg_val = 0;
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);
267 uint32_t v = xtract(reg_val, reg_array->addr,
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);
280 dump_regs_table(int argc, const char *argv[], const uint32_t *regs,
281 const struct mod_regs *modtab, int nmodules)
285 for (i = 0; i < argc; i++) {
286 for (j = 0; j < nmodules; j++) {
287 if (!strcmp(argv[i], modtab[j].name))
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");
301 for ( ; nmodules; nmodules--, modtab++) {
303 match = argc == 0 ? 1 : 0;
304 for (i = 0; !match && i < argc; i++) {
305 if (!strcmp(argv[i], modtab->name))
310 dump_block_regs(modtab->ri, regs);
316 #define T4_MODREGS(name) { #name, t4_##name##_regs }
318 dump_regs_t4(int argc, const char *argv[], const uint32_t *regs)
320 static struct mod_regs t4_mod[] = {
322 { "pci", t4_pcie_regs },
326 { "edc0", t4_edc_0_regs },
327 { "edc1", t4_edc_1_regs },
332 { "pmrx", t4_pm_rx_regs },
333 { "pmtx", t4_pm_tx_regs },
335 { "cplsw", t4_cpl_switch_regs },
337 { "i2c", t4_i2cm_regs },
348 return dump_regs_table(argc, argv, regs, t4_mod, ARRAY_SIZE(t4_mod));
353 dump_regs_t4vf(int argc, const char *argv[], const uint32_t *regs)
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 },
363 return dump_regs_table(argc, argv, regs, t4vf_mod,
364 ARRAY_SIZE(t4vf_mod));
367 #define T5_MODREGS(name) { #name, t5_##name##_regs }
369 dump_regs_t5(int argc, const char *argv[], const uint32_t *regs)
371 static struct mod_regs t5_mod[] = {
373 { "pci", t5_pcie_regs },
375 { "mc0", t5_mc_0_regs },
376 { "mc1", t5_mc_1_regs },
378 { "edc0", t5_edc_t50_regs },
379 { "edc1", t5_edc_t51_regs },
382 { "ulprx", t5_ulp_rx_regs },
383 { "ulptx", t5_ulp_tx_regs },
384 { "pmrx", t5_pm_rx_regs },
385 { "pmtx", t5_pm_tx_regs },
387 { "cplsw", t5_cpl_switch_regs },
389 { "i2c", t5_i2cm_regs },
398 { "hma", t5_hma_t5_regs }
401 return dump_regs_table(argc, argv, regs, t5_mod, ARRAY_SIZE(t5_mod));
406 dump_regs(int argc, const char *argv[])
408 int vers, revision, rc;
409 struct t4_regdump regs;
412 len = max(T4_REGDUMP_SIZE, T5_REGDUMP_SIZE);
413 regs.data = calloc(1, len);
414 if (regs.data == NULL) {
415 warnc(ENOMEM, "regdump");
420 rc = doit(CHELSIO_T4_REGDUMP, ®s);
424 vers = get_card_vers(regs.version);
425 revision = (regs.version >> 10) & 0x3f;
428 if (revision == 0x3f)
429 rc = dump_regs_t4vf(argc, argv, regs.data);
431 rc = dump_regs_t4(argc, argv, regs.data);
432 } else if (vers == 5)
433 rc = dump_regs_t5(argc, argv, regs.data);
435 warnx("%s (type %d, rev %d) is not a known card.",
436 nexus, vers, revision);
445 do_show_info_header(uint32_t mode)
449 printf ("%4s %8s", "Idx", "Hits");
450 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
461 printf (" vld:VNIC");
465 printf (" vld:VLAN");
468 case T4_FILTER_IP_TOS:
472 case T4_FILTER_IP_PROTO:
476 case T4_FILTER_ETH_TYPE:
480 case T4_FILTER_MAC_IDX:
484 case T4_FILTER_MPS_HIT_TYPE:
488 case T4_FILTER_IP_FRAGMENT:
493 /* compressed filter field not enabled */
497 printf(" %20s %20s %9s %9s %s\n",
498 "DIP", "SIP", "DPORT", "SPORT", "Action");
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
510 * An error in parsing the value[:mask] will result in an error message and
511 * program termination.
514 parse_val_mask(const char *param, const char *args[], uint32_t *val,
519 if (strcmp(param, args[0]) != 0)
522 *val = strtoul(args[1], &p, 0);
529 if (p[0] == ':' && p[1] != 0) {
530 *mask = strtoul(p+1, &p, 0);
536 warnx("parameter \"%s\" has bad \"value[:mask]\" %s",
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
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.
561 * An error in parsing the value[:mask] will result in an error message and
562 * program termination.
565 parse_ipaddr(const char *param, const char *args[], int *afp, uint8_t addr[],
568 const char *colon, *afn;
572 unsigned int masksize;
575 * Is this our parameter?
577 if (strcmp(param, args[0]) != 0)
581 * Fundamental IPv4 versus IPv6 selection.
583 colon = strchr(args[1], ':');
593 if (*afp == AF_UNSPEC)
595 else if (*afp != af) {
596 warnx("address %s is not of expected family %s",
597 args[1], *afp == AF_INET ? "IP" : "IPv6");
602 * Parse address (temporarily stripping off any "/mask"
605 slash = strchr(args[1], '/');
608 ret = inet_pton(af, args[1], addr);
612 warnx("Cannot parse %s %s address %s", param, afn, args[1]);
617 * Parse optional mask specification.
621 unsigned int prefix = strtoul(slash + 1, &p, 10);
623 if (p == slash + 1) {
624 warnx("missing address prefix for %s", param);
628 warnx("%s is not a valid address prefix", slash + 1);
631 if (prefix > masksize) {
632 warnx("prefix %u is too long for an %s address",
636 memset(mask, 0, masksize / 8);
643 for (m = mask; masksize >= 8; m++, masksize -= 8)
646 *m = ~0 << (8 - masksize);
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.
659 parse_val(const char *param, const char *args[], uint32_t *val)
663 if (strcmp(param, args[0]) != 0)
666 *val = strtoul(args[1], &p, 0);
667 if (p > args[1] && p[0] == 0)
670 warnx("parameter \"%s\" has bad \"value\" %s", args[0], args[1]);
675 filters_show_ipaddr(int type, uint8_t *addr, uint8_t *addrm)
686 for (octet = 0; octet < noctets; octet++)
687 printf("%02x", addr[octet]);
689 for (octet = 0; octet < noctets; octet++)
690 printf("%02x", addrm[octet]);
694 do_show_one_filter_info(struct t4_filter *t, uint32_t mode)
698 printf("%4d", t->idx);
699 if (t->hits == UINT64_MAX)
702 printf(" %8ju", t->hits);
705 * Compressed header portion of filter.
707 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
710 printf(" %1d/%1d", t->fs.val.fcoe, t->fs.mask.fcoe);
714 printf(" %1d/%1d", t->fs.val.iport, t->fs.mask.iport);
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);
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);
731 case T4_FILTER_IP_TOS:
732 printf(" %02x/%02x", t->fs.val.tos, t->fs.mask.tos);
735 case T4_FILTER_IP_PROTO:
736 printf(" %02x/%02x", t->fs.val.proto, t->fs.mask.proto);
739 case T4_FILTER_ETH_TYPE:
740 printf(" %04x/%04x", t->fs.val.ethtype,
744 case T4_FILTER_MAC_IDX:
745 printf(" %03x/%03x", t->fs.val.macidx,
749 case T4_FILTER_MPS_HIT_TYPE:
750 printf(" %1x/%1x", t->fs.val.matchtype,
751 t->fs.mask.matchtype);
754 case T4_FILTER_IP_FRAGMENT:
755 printf(" %1d/%1d", t->fs.val.frag, t->fs.mask.frag);
759 /* compressed filter field not enabled */
765 * Fixed portion of filter.
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);
774 * Variable length filter action.
776 if (t->fs.action == FILTER_DROP)
778 else if (t->fs.action == FILTER_SWITCH) {
779 printf(" Switch: port=%d", t->fs.eport);
782 ", dmac=%02x:%02x:%02x:%02x:%02x:%02x "
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],
790 ", smac=%02x:%02x:%02x:%02x:%02x:%02x "
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],
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);
804 if (t->fs.dirsteer == 0) {
807 printf("(TCB=hash)");
809 printf("%d", t->fs.iq);
810 if (t->fs.dirsteerhash == 0)
826 uint32_t mode = 0, header = 0;
830 /* Get the global filter mode first */
831 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
836 for (t.idx = 0; ; t.idx++) {
837 rc = doit(CHELSIO_T4_GET_FILTER, &t);
838 if (rc != 0 || t.idx == 0xffffffff)
842 do_show_info_header(mode);
845 do_show_one_filter_info(&t, mode);
852 get_filter_mode(void)
857 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
861 if (mode & T4_FILTER_IPv4)
864 if (mode & T4_FILTER_IPv6)
867 if (mode & T4_FILTER_IP_SADDR)
870 if (mode & T4_FILTER_IP_DADDR)
873 if (mode & T4_FILTER_IP_SPORT)
876 if (mode & T4_FILTER_IP_DPORT)
879 if (mode & T4_FILTER_IP_FRAGMENT)
882 if (mode & T4_FILTER_MPS_HIT_TYPE)
883 printf("matchtype ");
885 if (mode & T4_FILTER_MAC_IDX)
888 if (mode & T4_FILTER_ETH_TYPE)
891 if (mode & T4_FILTER_IP_PROTO)
894 if (mode & T4_FILTER_IP_TOS)
897 if (mode & T4_FILTER_VLAN)
900 if (mode & T4_FILTER_VNIC)
901 printf("vnic/ovlan ");
903 if (mode & T4_FILTER_PORT)
906 if (mode & T4_FILTER_FCoE)
915 set_filter_mode(int argc, const char *argv[])
919 for (; argc; argc--, argv++) {
920 if (!strcmp(argv[0], "frag"))
921 mode |= T4_FILTER_IP_FRAGMENT;
923 if (!strcmp(argv[0], "matchtype"))
924 mode |= T4_FILTER_MPS_HIT_TYPE;
926 if (!strcmp(argv[0], "macidx"))
927 mode |= T4_FILTER_MAC_IDX;
929 if (!strcmp(argv[0], "ethtype"))
930 mode |= T4_FILTER_ETH_TYPE;
932 if (!strcmp(argv[0], "proto"))
933 mode |= T4_FILTER_IP_PROTO;
935 if (!strcmp(argv[0], "tos"))
936 mode |= T4_FILTER_IP_TOS;
938 if (!strcmp(argv[0], "vlan"))
939 mode |= T4_FILTER_VLAN;
941 if (!strcmp(argv[0], "ovlan") ||
942 !strcmp(argv[0], "vnic"))
943 mode |= T4_FILTER_VNIC;
945 if (!strcmp(argv[0], "iport"))
946 mode |= T4_FILTER_PORT;
948 if (!strcmp(argv[0], "fcoe"))
949 mode |= T4_FILTER_FCoE;
952 return doit(CHELSIO_T4_SET_FILTER_MODE, &mode);
956 del_filter(uint32_t idx)
962 return doit(CHELSIO_T4_DEL_FILTER, &t);
966 set_filter(uint32_t idx, int argc, const char *argv[])
968 int af = AF_UNSPEC, start_arg = 0;
972 warnc(EINVAL, "%s", __func__);
975 bzero(&t, sizeof (t));
979 for (start_arg = 0; start_arg + 2 <= argc; start_arg += 2) {
980 const char **args = &argv[start_arg];
983 if (!strcmp(argv[start_arg], "type")) {
985 if (!strcasecmp(argv[start_arg + 1], "ipv4"))
987 else if (!strcasecmp(argv[start_arg + 1], "ipv6"))
990 warnx("invalid type \"%s\"; "
991 "must be one of \"ipv4\" or \"ipv6\"",
992 argv[start_arg + 1]);
996 if (af != AF_UNSPEC && af != newaf) {
997 warnx("conflicting IPv4/IPv6 specifications.");
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)) {
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,
1049 } else if (!parse_ipaddr("sip", args, &af, t.fs.val.sip,
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;
1060 warnx("invalid action \"%s\"; must be one of"
1061 " \"pass\", \"drop\" or \"switch\"",
1062 argv[start_arg + 1]);
1065 } else if (!parse_val("hitcnts", args, &val)) {
1067 } else if (!parse_val("prio", args, &val)) {
1069 } else if (!parse_val("rpttid", args, &val)) {
1071 } else if (!parse_val("queue", args, &val)) {
1074 } else if (!parse_val("tcbhash", args, &val)) {
1076 t.fs.dirsteerhash = 1;
1077 } else if (!parse_val("eport", args, &val)) {
1079 } else if (!strcmp(argv[start_arg], "dmac")) {
1080 struct ether_addr *daddr;
1082 daddr = ether_aton(argv[start_arg + 1]);
1083 if (daddr == NULL) {
1084 warnx("invalid dmac address \"%s\"",
1085 argv[start_arg + 1]);
1088 memcpy(t.fs.dmac, daddr, ETHER_ADDR_LEN);
1090 } else if (!strcmp(argv[start_arg], "smac")) {
1091 struct ether_addr *saddr;
1093 saddr = ether_aton(argv[start_arg + 1]);
1094 if (saddr == NULL) {
1095 warnx("invalid smac address \"%s\"",
1096 argv[start_arg + 1]);
1099 memcpy(t.fs.smac, saddr, ETHER_ADDR_LEN);
1101 } else if (!strcmp(argv[start_arg], "vlan")) {
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;
1116 warnx("unknown vlan parameter \"%s\"; must"
1117 " be one of \"none\", \"=<vlan>\", "
1118 " \"+<vlan>\", or \"<vlan>\"",
1119 argv[start_arg + 1]);
1122 if (t.fs.newvlan == VLAN_REWRITE ||
1123 t.fs.newvlan == VLAN_INSERT) {
1124 t.fs.vlan = strtoul(argv[start_arg + 1] + 1,
1126 if (p == argv[start_arg + 1] + 1 || p[0] != 0) {
1127 warnx("invalid vlan \"%s\"",
1128 argv[start_arg + 1]);
1133 warnx("invalid parameter \"%s\"", argv[start_arg]);
1137 if (start_arg != argc) {
1138 warnx("no value for \"%s\"", argv[start_arg]);
1143 * Check basic sanity of option combinations.
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\"");
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\"");
1158 t.fs.type = (af == AF_INET6 ? 1 : 0); /* default IPv4 */
1159 return doit(CHELSIO_T4_SET_FILTER, &t);
1163 filter_cmd(int argc, const char *argv[])
1170 warnx("filter: no arguments.");
1175 if (strcmp(argv[0], "list") == 0) {
1177 warnx("trailing arguments after \"list\" ignored.");
1179 return show_filters();
1183 if (argc == 1 && strcmp(argv[0], "mode") == 0)
1184 return get_filter_mode();
1187 if (strcmp(argv[0], "mode") == 0)
1188 return set_filter_mode(argc - 1, argv + 1);
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.",
1197 idx = (uint32_t) val;
1199 /* <idx> delete|clear */
1201 (strcmp(argv[1], "delete") == 0 || strcmp(argv[1], "clear") == 0)) {
1202 return del_filter(idx);
1205 /* <idx> [<param> <val>] ... */
1206 return set_filter(idx, argc - 1, argv + 1);
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.
1216 show_struct(const uint32_t *words, int nwords, const struct field_desc *fd)
1219 const struct field_desc *p;
1221 for (p = fd; p->name; p++)
1222 w = max(w, strlen(p->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;
1231 data = (words[first_word] >> shift) |
1232 ((uint64_t)words[first_word + 1] << (32 - shift));
1234 data |= ((uint64_t)words[first_word + 2] << (64 - shift));
1238 printf("%-*s ", w, fd->name);
1239 printf(fd->hex ? "%#llx\n" : "%llu\n", data << fd->shift);
1244 #define FIELD(name, start, end) { name, start, end, 0, 0, 0 }
1245 #define FIELD1(name, start) FIELD(name, start, start)
1248 show_sge_context(const struct t4_sge_context *p)
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),
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),
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),
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),
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},
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},
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);
1398 get_sge_context(int argc, const char *argv[])
1403 struct t4_sge_context cntxt = {0};
1406 warnx("sge_context: incorrect number of arguments.");
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;
1419 warnx("unknown context type \"%s\"; known types are egress, "
1420 "ingress, fl, and cong.", argv[0]);
1424 p = str_to_number(argv[1], &cid, NULL);
1426 warnx("invalid context id \"%s\"", argv[1]);
1431 rc = doit(CHELSIO_T4_GET_SGE_CONTEXT, &cntxt);
1435 show_sge_context(&cntxt);
1440 loadfw(int argc, const char *argv[])
1443 struct t4_data data = {0};
1444 const char *fname = argv[0];
1445 struct stat st = {0};
1448 warnx("loadfw: incorrect number of arguments.");
1452 fd = open(fname, O_RDONLY);
1454 warn("open(%s)", fname);
1458 if (fstat(fd, &st) < 0) {
1464 data.len = st.st_size;
1465 data.data = mmap(0, data.len, PROT_READ, 0, fd, 0);
1466 if (data.data == MAP_FAILED) {
1472 rc = doit(CHELSIO_T4_LOAD_FW, &data);
1473 munmap(data.data, data.len);
1479 read_mem(uint32_t addr, uint32_t len, void (*output)(uint32_t *, uint32_t))
1482 struct t4_mem_range mr;
1486 mr.data = malloc(mr.len);
1489 warn("read_mem: malloc");
1493 rc = doit(CHELSIO_T4_GET_MEM, &mr);
1498 (*output)(mr.data, mr.len);
1505 * Display memory as list of 'n' 4-byte values per line.
1508 show_mem(uint32_t *buf, uint32_t len)
1514 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1516 printf("%s%08x", s, htonl(*buf));
1523 memdump(int argc, const char *argv[])
1530 warnx("incorrect number of arguments.");
1534 p = str_to_number(argv[0], &l, NULL);
1536 warnx("invalid address \"%s\"", argv[0]);
1541 p = str_to_number(argv[1], &l, NULL);
1543 warnx("memdump: invalid length \"%s\"", argv[1]);
1548 return (read_mem(addr, len, show_mem));
1552 * Display TCB as list of 'n' 4-byte values per line.
1555 show_tcb(uint32_t *buf, uint32_t len)
1561 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1563 printf("%s%08x", s, htonl(*buf));
1569 #define A_TP_CMM_TCB_BASE 0x7d10
1570 #define TCB_SIZE 128
1572 read_tcb(int argc, const char *argv[])
1582 warnx("incorrect number of arguments.");
1586 p = str_to_number(argv[0], &l, NULL);
1588 warnx("invalid tid \"%s\"", argv[0]);
1593 rc = read_reg(A_TP_CMM_TCB_BASE, 4, &val);
1597 addr = val + tid * TCB_SIZE;
1599 return (read_mem(addr, TCB_SIZE, show_tcb));
1603 read_i2c(int argc, const char *argv[])
1607 struct t4_i2c_data i2cd;
1610 if (argc < 3 || argc > 4) {
1611 warnx("incorrect number of arguments.");
1615 p = str_to_number(argv[0], &l, NULL);
1616 if (*p || l > UCHAR_MAX) {
1617 warnx("invalid port id \"%s\"", argv[0]);
1622 p = str_to_number(argv[1], &l, NULL);
1623 if (*p || l > UCHAR_MAX) {
1624 warnx("invalid i2c device address \"%s\"", argv[1]);
1629 p = str_to_number(argv[2], &l, NULL);
1630 if (*p || l > UCHAR_MAX) {
1631 warnx("invalid byte offset \"%s\"", argv[2]);
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]);
1646 rc = doit(CHELSIO_T4_GET_I2C, &i2cd);
1650 for (i = 0; i < i2cd.len; i++)
1651 printf("0x%x [%u]\n", i2cd.data[i], i2cd.data[i]);
1657 clearstats(int argc, const char *argv[])
1664 warnx("incorrect number of arguments.");
1668 p = str_to_number(argv[0], &l, NULL);
1670 warnx("invalid port id \"%s\"", argv[0]);
1675 return doit(CHELSIO_T4_CLEAR_STATS, &port);
1683 int rc, port_idx, i;
1686 /* Magic values: MPS_TRC_CFG = 0x9800. MPS_TRC_CFG[1:1] = TrcEn */
1687 rc = read_reg(0x9800, 4, &val);
1690 printf("tracing is %s\n", val & 2 ? "ENABLED" : "DISABLED");
1693 for (t.idx = 0; ; t.idx++) {
1694 rc = doit(CHELSIO_T4_GET_TRACER, &t);
1695 if (rc != 0 || t.idx == 0xff)
1698 if (t.tp.port < 4) {
1700 port_idx = t.tp.port;
1701 } else if (t.tp.port < 8) {
1703 port_idx = t.tp.port - 4;
1704 } else if (t.tp.port < 12) {
1706 port_idx = t.tp.port - 8;
1707 } else if (t.tp.port < 16) {
1709 port_idx = t.tp.port - 12;
1710 } else if (t.tp.port < 20) {
1712 port_idx = t.tp.port - 16;
1715 port_idx = t.tp.port;
1718 printf("\ntracer %u (currently %s) captures ", t.idx,
1719 t.enabled ? "ENABLED" : "DISABLED");
1721 printf("port %u %s, ", port_idx, s);
1723 printf("%s %u, ", s, port_idx);
1724 printf("snap length: %u, min length: %u\n", t.tp.snap_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],
1734 for (i = 0; i < t.tp.skip_ofst * 2; i += 2)
1735 printf("%08x%08x", t.tp.mask[i],
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]);
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);
1752 tracer_onoff(uint8_t idx, int enabled)
1757 t.enabled = enabled;
1760 return doit(CHELSIO_T4_SET_TRACER, &t);
1764 create_tracing_ifnet()
1767 "/sbin/ifconfig", __DECONST(char *, nexus), "create", NULL
1769 char *env[] = {NULL};
1772 close(STDERR_FILENO);
1773 execve(cmd[0], cmd, env);
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
1784 set_tracer(uint8_t idx, int argc, const char *argv[])
1789 bzero(&t, sizeof (t));
1795 warnx("must specify tx<n> or rx<n>.");
1799 len = strlen(argv[0]);
1801 warnx("argument must be 3 characters (tx<n> or rx<n>)");
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]);
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]);
1819 warnx("argument '%s' isn't tx<n> or rx<n>", argv[0]);
1823 t.tp.snap_len = 128;
1830 create_tracing_ifnet();
1831 return doit(CHELSIO_T4_SET_TRACER, &t);
1835 tracer_cmd(int argc, const char *argv[])
1842 warnx("tracer: no arguments.");
1847 if (strcmp(argv[0], "list") == 0) {
1849 warnx("trailing arguments after \"list\" ignored.");
1851 return show_tracers();
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.",
1864 if (argc == 2 && strcmp(argv[1], "disable") == 0)
1865 return tracer_onoff(idx, 0);
1868 if (argc == 2 && strcmp(argv[1], "enable") == 0)
1869 return tracer_onoff(idx, 1);
1872 return set_tracer(idx, argc - 1, argv + 1);
1876 modinfo_raw(int port_id)
1879 struct t4_i2c_data i2cd;
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);
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],
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] : '.');
1911 modinfo(int argc, const char *argv[])
1914 char string[16], *p;
1915 struct t4_i2c_data i2cd;
1917 uint16_t temp, vcc, tx_bias, tx_power, rx_power;
1920 warnx("must supply a port");
1925 warnx("too many arguments");
1929 p = str_to_number(argv[0], &port, NULL);
1930 if (*p || port > UCHAR_MAX) {
1931 warnx("invalid port id \"%s\"", argv[0]);
1936 if (!strcmp(argv[1], "raw"))
1937 return (modinfo_raw(port));
1939 warnx("second argument can only be \"raw\"");
1944 bzero(&i2cd, sizeof(i2cd));
1946 i2cd.port_id = port;
1947 i2cd.dev_addr = SFF_8472_BASE;
1949 i2cd.offset = SFF_8472_ID;
1950 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
1953 if (i2cd.data[0] > SFF_8472_ID_LAST)
1954 printf("Unknown ID\n");
1956 printf("ID: %s\n", sff_8472_id[i2cd.data[0]]);
1958 bzero(&string, sizeof(string));
1959 for (i = SFF_8472_VENDOR_START; i < SFF_8472_VENDOR_END; i++) {
1961 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
1963 string[i - SFF_8472_VENDOR_START] = i2cd.data[0];
1965 printf("Vendor %s\n", string);
1967 bzero(&string, sizeof(string));
1968 for (i = SFF_8472_SN_START; i < SFF_8472_SN_END; i++) {
1970 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
1972 string[i - SFF_8472_SN_START] = i2cd.data[0];
1974 printf("SN %s\n", string);
1976 bzero(&string, sizeof(string));
1977 for (i = SFF_8472_PN_START; i < SFF_8472_PN_END; i++) {
1979 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
1981 string[i - SFF_8472_PN_START] = i2cd.data[0];
1983 printf("PN %s\n", string);
1985 bzero(&string, sizeof(string));
1986 for (i = SFF_8472_REV_START; i < SFF_8472_REV_END; i++) {
1988 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
1990 string[i - SFF_8472_REV_START] = i2cd.data[0];
1992 printf("Rev %s\n", string);
1994 i2cd.offset = SFF_8472_DIAG_TYPE;
1995 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
1998 if ((char )i2cd.data[0] & (SFF_8472_DIAG_IMPL |
1999 SFF_8472_DIAG_INTERNAL)) {
2001 /* Switch to reading from the Diagnostic address. */
2002 i2cd.dev_addr = SFF_8472_DIAG;
2005 i2cd.offset = SFF_8472_TEMP;
2006 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
2008 temp = i2cd.data[0] << 8;
2010 if ((temp & SFF_8472_TEMP_SIGN) == SFF_8472_TEMP_SIGN)
2014 printf("%dC\n", (temp & SFF_8472_TEMP_MSK) >>
2015 SFF_8472_TEMP_SHIFT);
2017 i2cd.offset = SFF_8472_VCC;
2018 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
2020 vcc = i2cd.data[0] << 8;
2021 printf("Vcc %fV\n", vcc / SFF_8472_VCC_FACTOR);
2023 i2cd.offset = SFF_8472_TX_BIAS;
2024 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
2026 tx_bias = i2cd.data[0] << 8;
2027 printf("TX Bias %fuA\n", tx_bias / SFF_8472_BIAS_FACTOR);
2029 i2cd.offset = SFF_8472_TX_POWER;
2030 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
2032 tx_power = i2cd.data[0] << 8;
2033 printf("TX Power %fmW\n", tx_power / SFF_8472_POWER_FACTOR);
2035 i2cd.offset = SFF_8472_RX_POWER;
2036 if ((rc = doit(CHELSIO_T4_GET_I2C, &i2cd)) != 0)
2038 rx_power = i2cd.data[0] << 8;
2039 printf("RX Power %fmW\n", rx_power / SFF_8472_POWER_FACTOR);
2042 printf("Diagnostics not supported.\n");
2048 warnx("No module/cable in port %ld", port);
2053 /* XXX: pass in a low/high and do range checks as well */
2055 get_sched_param(const char *param, const char *args[], long *val)
2059 if (strcmp(param, args[0]) != 0)
2062 p = str_to_number(args[1], val, NULL);
2064 warnx("parameter \"%s\" has bad value \"%s\"", args[0],
2073 sched_class(int argc, const char *argv[])
2075 struct t4_sched_params op;
2078 memset(&op, 0xff, sizeof(op));
2082 warnx("missing scheduling sub-command");
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;
2095 warnx("invalid scheduling sub-command \"%s\"", argv[0]);
2099 /* Decode remaining arguments ... */
2101 for (i = 1; i < argc; i += 2) {
2102 const char **args = &argv[i];
2105 if (i + 1 == argc) {
2106 warnx("missing argument for \"%s\"", args[0]);
2111 if (!strcmp(args[0], "type")) {
2112 if (!strcmp(args[1], "packet"))
2113 op.type = SCHED_CLASS_TYPE_PACKET;
2115 warnx("invalid type parameter \"%s\"", args[1]);
2122 if (op.subcmd == SCHED_CLASS_SUBCMD_CONFIG) {
2123 if(!get_sched_param("minmax", args, &l))
2124 op.u.config.minmax = (int8_t)l;
2126 warnx("unknown scheduler config parameter "
2134 /* Rest applies only to SUBCMD_PARAMS */
2135 if (op.subcmd != SCHED_CLASS_SUBCMD_PARAMS)
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;
2146 warnx("invalid level parameter \"%s\"",
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;
2156 warnx("invalid mode parameter \"%s\"", args[1]);
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;
2165 warnx("invalid rate-unit parameter \"%s\"",
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;
2175 warnx("invalid rate-mode parameter \"%s\"",
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;
2192 warnx("unknown scheduler parameter \"%s\"", args[0]);
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.
2205 warnx("sched \"type\" parameter missing");
2208 if (op.subcmd == SCHED_CLASS_SUBCMD_CONFIG) {
2209 if (op.u.config.minmax < 0) {
2210 warnx("sched config \"minmax\" parameter missing");
2214 if (op.subcmd == SCHED_CLASS_SUBCMD_PARAMS) {
2215 if (op.u.params.level < 0) {
2216 warnx("sched params \"level\" parameter missing");
2219 if (op.u.params.mode < 0) {
2220 warnx("sched params \"mode\" parameter missing");
2223 if (op.u.params.rateunit < 0) {
2224 warnx("sched params \"rate-unit\" parameter missing");
2227 if (op.u.params.ratemode < 0) {
2228 warnx("sched params \"rate-mode\" parameter missing");
2231 if (op.u.params.channel < 0) {
2232 warnx("sched params \"channel\" missing");
2235 if (op.u.params.cl < 0) {
2236 warnx("sched params \"class\" missing");
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");
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");
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");
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");
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");
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");
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 "
2285 warnx("%d error%s in sched-class command", errs,
2286 errs == 1 ? "" : "s");
2290 return doit(CHELSIO_T4_SCHED_CLASS, &op);
2294 sched_queue(int argc, const char *argv[])
2296 struct t4_sched_queue op = {0};
2301 /* need "<port> <queue> <class> */
2302 warnx("incorrect number of arguments.");
2306 p = str_to_number(argv[0], &val, NULL);
2307 if (*p || val > UCHAR_MAX) {
2308 warnx("invalid port id \"%s\"", argv[0]);
2311 op.port = (uint8_t)val;
2313 if (!strcmp(argv[1], "all") || !strcmp(argv[1], "*"))
2316 p = str_to_number(argv[1], &val, NULL);
2317 if (*p || val < -1) {
2318 warnx("invalid queue \"%s\"", argv[1]);
2321 op.queue = (int8_t)val;
2324 if (!strcmp(argv[2], "unbind") || !strcmp(argv[2], "clear"))
2327 p = str_to_number(argv[2], &val, NULL);
2328 if (*p || val < -1) {
2329 warnx("invalid class \"%s\"", argv[2]);
2332 op.cl = (int8_t)val;
2335 return doit(CHELSIO_T4_SCHED_QUEUE, &op);
2339 run_cmd(int argc, const char *argv[])
2342 const char *cmd = argv[0];
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);
2378 warnx("invalid command \"%s\"", cmd);
2389 char buffer[128], *buf;
2390 const char *args[MAX_ARGS + 1];
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.
2398 fprintf(stdout, "> ");
2400 buf = fgets(buffer, sizeof(buffer), stdin);
2402 if (ferror(stdin)) {
2403 warn("stdin error");
2404 rc = errno; /* errno from fgets */
2410 while ((args[i] = strsep(&buf, " \t\n")) != NULL) {
2411 if (args[i][0] != 0 && ++i == MAX_ARGS)
2417 continue; /* skip empty line */
2419 if (!strcmp(args[0], "quit") || !strcmp(args[0], "exit"))
2422 rc = run_cmd(i, args);
2425 /* rc normally comes from the last command (not including quit/exit) */
2430 main(int argc, const char *argv[])
2437 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
2450 /* progname and nexus */
2454 if (argc == 1 && !strcmp(argv[0], "stdio"))
2455 rc = run_cmd_loop();
2457 rc = run_cmd(argc, argv);