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>
52 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
54 #define max(x, y) ((x) > (y) ? (x) : (y))
56 static const char *progname, *nexus;
66 const struct reg_info *ri;
70 const char *name; /* Field name */
71 unsigned short start; /* Start bit position */
72 unsigned short end; /* End bit position */
73 unsigned char shift; /* # of low order bits omitted and implicitly 0 */
74 unsigned char hex; /* Print field in hex instead of decimal */
75 unsigned char islog2; /* Field contains the base-2 log of the value */
78 #include "reg_defs_t4.c"
79 #include "reg_defs_t4vf.c"
80 #include "reg_defs_t5.c"
85 fprintf(fp, "Usage: %s <nexus> [operation]\n", progname);
87 "\tclearstats <port> clear port statistics\n"
88 "\tcontext <type> <id> show an SGE context\n"
89 "\tfilter <idx> [<param> <val>] ... set a filter\n"
90 "\tfilter <idx> delete|clear delete a filter\n"
91 "\tfilter list list all filters\n"
92 "\tfilter mode [<match>] ... get/set global filter mode\n"
93 "\ti2c <port> <devaddr> <addr> [<len>] read from i2c device\n"
94 "\tloadfw <fw-image.bin> install firmware\n"
95 "\tmemdump <addr> <len> dump a memory range\n"
96 "\treg <address>[=<val>] read/write register\n"
97 "\treg64 <address>[=<val>] read/write 64 bit register\n"
98 "\tregdump [<module>] ... dump registers\n"
99 "\tstdio interactive mode\n"
100 "\ttcb <tid> read TCB\n"
104 static inline unsigned int
105 get_card_vers(unsigned int version)
107 return (version & 0x3ff);
111 real_doit(unsigned long cmd, void *data, const char *cmdstr)
119 snprintf(buf, sizeof(buf), "/dev/%s", nexus);
120 if ((fd = open(buf, O_RDWR)) < 0) {
121 warn("open(%s)", nexus);
127 rc = ioctl(fd, cmd, data);
135 #define doit(x, y) real_doit(x, y, #x)
138 str_to_number(const char *s, long *val, long long *vall)
143 *vall = strtoll(s, &p, 0);
145 *val = strtol(s, &p, 0);
153 read_reg(long addr, int size, long long *val)
158 reg.addr = (uint32_t) addr;
159 reg.size = (uint32_t) size;
162 rc = doit(CHELSIO_T4_GETREG, ®);
170 write_reg(long addr, int size, long long val)
174 reg.addr = (uint32_t) addr;
175 reg.size = (uint32_t) size;
176 reg.val = (uint64_t) val;
178 return doit(CHELSIO_T4_SETREG, ®);
182 register_io(int argc, const char *argv[], int size)
190 /* <reg> OR <reg>=<value> */
192 p = str_to_number(argv[0], &addr, NULL);
195 warnx("invalid register \"%s\"", argv[0]);
201 p = str_to_number(v, NULL, &val);
204 warnx("invalid value \"%s\"", v);
209 } else if (argc == 2) {
214 p = str_to_number(argv[0], &addr, NULL);
216 warnx("invalid register \"%s\"", argv[0]);
220 p = str_to_number(argv[1], NULL, &val);
222 warnx("invalid value \"%s\"", argv[1]);
226 warnx("reg: invalid number of arguments (%d)", argc);
231 rc = write_reg(addr, size, val);
233 rc = read_reg(addr, size, &val);
235 printf("0x%llx [%llu]\n", val, val);
241 static inline uint32_t
242 xtract(uint32_t val, int shift, int len)
244 return (val >> shift) & ((1 << len) - 1);
248 dump_block_regs(const struct reg_info *reg_array, const uint32_t *regs)
250 uint32_t reg_val = 0;
252 for ( ; reg_array->name; ++reg_array)
253 if (!reg_array->len) {
254 reg_val = regs[reg_array->addr / 4];
255 printf("[%#7x] %-47s %#-10x %u\n", reg_array->addr,
256 reg_array->name, reg_val, reg_val);
258 uint32_t v = xtract(reg_val, reg_array->addr,
261 printf(" %*u:%u %-47s %#-10x %u\n",
262 reg_array->addr < 10 ? 3 : 2,
263 reg_array->addr + reg_array->len - 1,
264 reg_array->addr, reg_array->name, v, v);
271 dump_regs_table(int argc, const char *argv[], const uint32_t *regs,
272 const struct mod_regs *modtab, int nmodules)
276 for (i = 0; i < argc; i++) {
277 for (j = 0; j < nmodules; j++) {
278 if (!strcmp(argv[i], modtab[j].name))
283 warnx("invalid register block \"%s\"", argv[i]);
284 fprintf(stderr, "\nAvailable blocks:");
285 for ( ; nmodules; nmodules--, modtab++)
286 fprintf(stderr, " %s", modtab->name);
287 fprintf(stderr, "\n");
292 for ( ; nmodules; nmodules--, modtab++) {
294 match = argc == 0 ? 1 : 0;
295 for (i = 0; !match && i < argc; i++) {
296 if (!strcmp(argv[i], modtab->name))
301 dump_block_regs(modtab->ri, regs);
307 #define T4_MODREGS(name) { #name, t4_##name##_regs }
309 dump_regs_t4(int argc, const char *argv[], const uint32_t *regs)
311 static struct mod_regs t4_mod[] = {
313 { "pci", t4_pcie_regs },
317 { "edc0", t4_edc_0_regs },
318 { "edc1", t4_edc_1_regs },
323 { "pmrx", t4_pm_rx_regs },
324 { "pmtx", t4_pm_tx_regs },
326 { "cplsw", t4_cpl_switch_regs },
328 { "i2c", t4_i2cm_regs },
339 return dump_regs_table(argc, argv, regs, t4_mod, ARRAY_SIZE(t4_mod));
344 dump_regs_t4vf(int argc, const char *argv[], const uint32_t *regs)
346 static struct mod_regs t4vf_mod[] = {
347 { "sge", t4vf_sge_regs },
348 { "mps", t4vf_mps_regs },
349 { "pl", t4vf_pl_regs },
350 { "mbdata", t4vf_mbdata_regs },
351 { "cim", t4vf_cim_regs },
354 return dump_regs_table(argc, argv, regs, t4vf_mod,
355 ARRAY_SIZE(t4vf_mod));
358 #define T5_MODREGS(name) { #name, t5_##name##_regs }
360 dump_regs_t5(int argc, const char *argv[], const uint32_t *regs)
362 static struct mod_regs t5_mod[] = {
364 { "pci", t5_pcie_regs },
366 { "mc0", t5_mc_0_regs },
367 { "mc1", t5_mc_1_regs },
369 { "edc0", t5_edc_t50_regs },
370 { "edc1", t5_edc_t51_regs },
373 { "ulprx", t5_ulp_rx_regs },
374 { "ulptx", t5_ulp_tx_regs },
375 { "pmrx", t5_pm_rx_regs },
376 { "pmtx", t5_pm_tx_regs },
378 { "cplsw", t5_cpl_switch_regs },
380 { "i2c", t5_i2cm_regs },
389 { "hma", t5_hma_t5_regs }
392 return dump_regs_table(argc, argv, regs, t5_mod, ARRAY_SIZE(t5_mod));
397 dump_regs(int argc, const char *argv[])
399 int vers, revision, rc;
400 struct t4_regdump regs;
403 len = max(T4_REGDUMP_SIZE, T5_REGDUMP_SIZE);
404 regs.data = calloc(1, len);
405 if (regs.data == NULL) {
406 warnc(ENOMEM, "regdump");
411 rc = doit(CHELSIO_T4_REGDUMP, ®s);
415 vers = get_card_vers(regs.version);
416 revision = (regs.version >> 10) & 0x3f;
419 if (revision == 0x3f)
420 rc = dump_regs_t4vf(argc, argv, regs.data);
422 rc = dump_regs_t4(argc, argv, regs.data);
423 } else if (vers == 5)
424 rc = dump_regs_t5(argc, argv, regs.data);
426 warnx("%s (type %d, rev %d) is not a known card.",
427 nexus, vers, revision);
436 do_show_info_header(uint32_t mode)
440 printf ("%4s %8s", "Idx", "Hits");
441 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
452 printf (" vld:VNIC");
456 printf (" vld:VLAN");
459 case T4_FILTER_IP_TOS:
463 case T4_FILTER_IP_PROTO:
467 case T4_FILTER_ETH_TYPE:
471 case T4_FILTER_MAC_IDX:
475 case T4_FILTER_MPS_HIT_TYPE:
479 case T4_FILTER_IP_FRAGMENT:
484 /* compressed filter field not enabled */
488 printf(" %20s %20s %9s %9s %s\n",
489 "DIP", "SIP", "DPORT", "SPORT", "Action");
493 * Parse an argument sub-vector as a { <parameter name> <value>[:<mask>] }
494 * ordered tuple. If the parameter name in the argument sub-vector does not
495 * match the passed in parameter name, then a zero is returned for the
496 * function and no parsing is performed. If there is a match, then the value
497 * and optional mask are parsed and returned in the provided return value
498 * pointers. If no optional mask is specified, then a default mask of all 1s
501 * An error in parsing the value[:mask] will result in an error message and
502 * program termination.
505 parse_val_mask(const char *param, const char *args[], uint32_t *val,
510 if (strcmp(param, args[0]) != 0)
513 *val = strtoul(args[1], &p, 0);
520 if (p[0] == ':' && p[1] != 0) {
521 *mask = strtoul(p+1, &p, 0);
527 warnx("parameter \"%s\" has bad \"value[:mask]\" %s",
534 * Parse an argument sub-vector as a { <parameter name> <addr>[/<mask>] }
535 * ordered tuple. If the parameter name in the argument sub-vector does not
536 * match the passed in parameter name, then a zero is returned for the
537 * function and no parsing is performed. If there is a match, then the value
538 * and optional mask are parsed and returned in the provided return value
539 * pointers. If no optional mask is specified, then a default mask of all 1s
542 * The value return parameter "afp" is used to specify the expected address
543 * family -- IPv4 or IPv6 -- of the address[/mask] and return its actual
544 * format. A passed in value of AF_UNSPEC indicates that either IPv4 or IPv6
545 * is acceptable; AF_INET means that only IPv4 addresses are acceptable; and
546 * AF_INET6 means that only IPv6 are acceptable. AF_INET is returned for IPv4
547 * and AF_INET6 for IPv6 addresses, respectively. IPv4 address/mask pairs are
548 * returned in the first four bytes of the address and mask return values with
549 * the address A.B.C.D returned with { A, B, C, D } returned in addresses { 0,
550 * 1, 2, 3}, respectively.
552 * An error in parsing the value[:mask] will result in an error message and
553 * program termination.
556 parse_ipaddr(const char *param, const char *args[], int *afp, uint8_t addr[],
559 const char *colon, *afn;
563 unsigned int masksize;
566 * Is this our parameter?
568 if (strcmp(param, args[0]) != 0)
572 * Fundamental IPv4 versus IPv6 selection.
574 colon = strchr(args[1], ':');
584 if (*afp == AF_UNSPEC)
586 else if (*afp != af) {
587 warnx("address %s is not of expected family %s",
588 args[1], *afp == AF_INET ? "IP" : "IPv6");
593 * Parse address (temporarily stripping off any "/mask"
596 slash = strchr(args[1], '/');
599 ret = inet_pton(af, args[1], addr);
603 warnx("Cannot parse %s %s address %s", param, afn, args[1]);
608 * Parse optional mask specification.
612 unsigned int prefix = strtoul(slash + 1, &p, 10);
614 if (p == slash + 1) {
615 warnx("missing address prefix for %s", param);
619 warnx("%s is not a valid address prefix", slash + 1);
622 if (prefix > masksize) {
623 warnx("prefix %u is too long for an %s address",
627 memset(mask, 0, masksize / 8);
634 for (m = mask; masksize >= 8; m++, masksize -= 8)
637 *m = ~0 << (8 - masksize);
643 * Parse an argument sub-vector as a { <parameter name> <value> } ordered
644 * tuple. If the parameter name in the argument sub-vector does not match the
645 * passed in parameter name, then a zero is returned for the function and no
646 * parsing is performed. If there is a match, then the value is parsed and
647 * returned in the provided return value pointer.
650 parse_val(const char *param, const char *args[], uint32_t *val)
654 if (strcmp(param, args[0]) != 0)
657 *val = strtoul(args[1], &p, 0);
658 if (p > args[1] && p[0] == 0)
661 warnx("parameter \"%s\" has bad \"value\" %s", args[0], args[1]);
666 filters_show_ipaddr(int type, uint8_t *addr, uint8_t *addrm)
677 for (octet = 0; octet < noctets; octet++)
678 printf("%02x", addr[octet]);
680 for (octet = 0; octet < noctets; octet++)
681 printf("%02x", addrm[octet]);
685 do_show_one_filter_info(struct t4_filter *t, uint32_t mode)
689 printf("%4d", t->idx);
690 if (t->hits == UINT64_MAX)
693 printf(" %8ju", t->hits);
696 * Compressed header portion of filter.
698 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
701 printf(" %1d/%1d", t->fs.val.fcoe, t->fs.mask.fcoe);
705 printf(" %1d/%1d", t->fs.val.iport, t->fs.mask.iport);
709 printf(" %1d:%1x:%02x/%1d:%1x:%02x",
710 t->fs.val.vnic_vld, (t->fs.val.vnic >> 7) & 0x7,
711 t->fs.val.vnic & 0x7f, t->fs.mask.vnic_vld,
712 (t->fs.mask.vnic >> 7) & 0x7,
713 t->fs.mask.vnic & 0x7f);
717 printf(" %1d:%04x/%1d:%04x",
718 t->fs.val.vlan_vld, t->fs.val.vlan,
719 t->fs.mask.vlan_vld, t->fs.mask.vlan);
722 case T4_FILTER_IP_TOS:
723 printf(" %02x/%02x", t->fs.val.tos, t->fs.mask.tos);
726 case T4_FILTER_IP_PROTO:
727 printf(" %02x/%02x", t->fs.val.proto, t->fs.mask.proto);
730 case T4_FILTER_ETH_TYPE:
731 printf(" %04x/%04x", t->fs.val.ethtype,
735 case T4_FILTER_MAC_IDX:
736 printf(" %03x/%03x", t->fs.val.macidx,
740 case T4_FILTER_MPS_HIT_TYPE:
741 printf(" %1x/%1x", t->fs.val.matchtype,
742 t->fs.mask.matchtype);
745 case T4_FILTER_IP_FRAGMENT:
746 printf(" %1d/%1d", t->fs.val.frag, t->fs.mask.frag);
750 /* compressed filter field not enabled */
756 * Fixed portion of filter.
758 filters_show_ipaddr(t->fs.type, t->fs.val.dip, t->fs.mask.dip);
759 filters_show_ipaddr(t->fs.type, t->fs.val.sip, t->fs.mask.sip);
760 printf(" %04x/%04x %04x/%04x",
761 t->fs.val.dport, t->fs.mask.dport,
762 t->fs.val.sport, t->fs.mask.sport);
765 * Variable length filter action.
767 if (t->fs.action == FILTER_DROP)
769 else if (t->fs.action == FILTER_SWITCH) {
770 printf(" Switch: port=%d", t->fs.eport);
773 ", dmac=%02x:%02x:%02x:%02x:%02x:%02x "
775 t->fs.dmac[0], t->fs.dmac[1],
776 t->fs.dmac[2], t->fs.dmac[3],
777 t->fs.dmac[4], t->fs.dmac[5],
781 ", smac=%02x:%02x:%02x:%02x:%02x:%02x "
783 t->fs.smac[0], t->fs.smac[1],
784 t->fs.smac[2], t->fs.smac[3],
785 t->fs.smac[4], t->fs.smac[5],
787 if (t->fs.newvlan == VLAN_REMOVE)
788 printf(", vlan=none");
789 else if (t->fs.newvlan == VLAN_INSERT)
790 printf(", vlan=insert(%x)", t->fs.vlan);
791 else if (t->fs.newvlan == VLAN_REWRITE)
792 printf(", vlan=rewrite(%x)", t->fs.vlan);
795 if (t->fs.dirsteer == 0) {
798 printf("(TCB=hash)");
800 printf("%d", t->fs.iq);
801 if (t->fs.dirsteerhash == 0)
817 uint32_t mode = 0, header = 0;
821 /* Get the global filter mode first */
822 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
827 for (t.idx = 0; ; t.idx++) {
828 rc = doit(CHELSIO_T4_GET_FILTER, &t);
829 if (rc != 0 || t.idx == 0xffffffff)
833 do_show_info_header(mode);
836 do_show_one_filter_info(&t, mode);
843 get_filter_mode(void)
848 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
852 if (mode & T4_FILTER_IPv4)
855 if (mode & T4_FILTER_IPv6)
858 if (mode & T4_FILTER_IP_SADDR)
861 if (mode & T4_FILTER_IP_DADDR)
864 if (mode & T4_FILTER_IP_SPORT)
867 if (mode & T4_FILTER_IP_DPORT)
870 if (mode & T4_FILTER_IP_FRAGMENT)
873 if (mode & T4_FILTER_MPS_HIT_TYPE)
874 printf("matchtype ");
876 if (mode & T4_FILTER_MAC_IDX)
879 if (mode & T4_FILTER_ETH_TYPE)
882 if (mode & T4_FILTER_IP_PROTO)
885 if (mode & T4_FILTER_IP_TOS)
888 if (mode & T4_FILTER_VLAN)
891 if (mode & T4_FILTER_VNIC)
892 printf("vnic/ovlan ");
894 if (mode & T4_FILTER_PORT)
897 if (mode & T4_FILTER_FCoE)
906 set_filter_mode(int argc, const char *argv[])
910 for (; argc; argc--, argv++) {
911 if (!strcmp(argv[0], "frag"))
912 mode |= T4_FILTER_IP_FRAGMENT;
914 if (!strcmp(argv[0], "matchtype"))
915 mode |= T4_FILTER_MPS_HIT_TYPE;
917 if (!strcmp(argv[0], "macidx"))
918 mode |= T4_FILTER_MAC_IDX;
920 if (!strcmp(argv[0], "ethtype"))
921 mode |= T4_FILTER_ETH_TYPE;
923 if (!strcmp(argv[0], "proto"))
924 mode |= T4_FILTER_IP_PROTO;
926 if (!strcmp(argv[0], "tos"))
927 mode |= T4_FILTER_IP_TOS;
929 if (!strcmp(argv[0], "vlan"))
930 mode |= T4_FILTER_VLAN;
932 if (!strcmp(argv[0], "ovlan") ||
933 !strcmp(argv[0], "vnic"))
934 mode |= T4_FILTER_VNIC;
936 if (!strcmp(argv[0], "iport"))
937 mode |= T4_FILTER_PORT;
939 if (!strcmp(argv[0], "fcoe"))
940 mode |= T4_FILTER_FCoE;
943 return doit(CHELSIO_T4_SET_FILTER_MODE, &mode);
947 del_filter(uint32_t idx)
953 return doit(CHELSIO_T4_DEL_FILTER, &t);
957 set_filter(uint32_t idx, int argc, const char *argv[])
959 int af = AF_UNSPEC, start_arg = 0;
963 warnc(EINVAL, "%s", __func__);
966 bzero(&t, sizeof (t));
970 for (start_arg = 0; start_arg + 2 <= argc; start_arg += 2) {
971 const char **args = &argv[start_arg];
974 if (!strcmp(argv[start_arg], "type")) {
976 if (!strcasecmp(argv[start_arg + 1], "ipv4"))
978 else if (!strcasecmp(argv[start_arg + 1], "ipv6"))
981 warnx("invalid type \"%s\"; "
982 "must be one of \"ipv4\" or \"ipv6\"",
983 argv[start_arg + 1]);
987 if (af != AF_UNSPEC && af != newaf) {
988 warnx("conflicting IPv4/IPv6 specifications.");
992 } else if (!parse_val_mask("fcoe", args, &val, &mask)) {
994 t.fs.mask.fcoe = mask;
995 } else if (!parse_val_mask("iport", args, &val, &mask)) {
996 t.fs.val.iport = val;
997 t.fs.mask.iport = mask;
998 } else if (!parse_val_mask("ovlan", args, &val, &mask)) {
1000 t.fs.mask.vnic = mask;
1001 t.fs.val.vnic_vld = 1;
1002 t.fs.mask.vnic_vld = 1;
1003 } else if (!parse_val_mask("vnic", args, &val, &mask)) {
1004 t.fs.val.vnic = val;
1005 t.fs.mask.vnic = mask;
1006 t.fs.val.vnic_vld = 1;
1007 t.fs.mask.vnic_vld = 1;
1008 } else if (!parse_val_mask("ivlan", args, &val, &mask)) {
1009 t.fs.val.vlan = val;
1010 t.fs.mask.vlan = mask;
1011 t.fs.val.vlan_vld = 1;
1012 t.fs.mask.vlan_vld = 1;
1013 } else if (!parse_val_mask("tos", args, &val, &mask)) {
1015 t.fs.mask.tos = mask;
1016 } else if (!parse_val_mask("proto", args, &val, &mask)) {
1017 t.fs.val.proto = val;
1018 t.fs.mask.proto = mask;
1019 } else if (!parse_val_mask("ethtype", args, &val, &mask)) {
1020 t.fs.val.ethtype = val;
1021 t.fs.mask.ethtype = mask;
1022 } else if (!parse_val_mask("macidx", args, &val, &mask)) {
1023 t.fs.val.macidx = val;
1024 t.fs.mask.macidx = mask;
1025 } else if (!parse_val_mask("matchtype", args, &val, &mask)) {
1026 t.fs.val.matchtype = val;
1027 t.fs.mask.matchtype = mask;
1028 } else if (!parse_val_mask("frag", args, &val, &mask)) {
1029 t.fs.val.frag = val;
1030 t.fs.mask.frag = mask;
1031 } else if (!parse_val_mask("dport", args, &val, &mask)) {
1032 t.fs.val.dport = val;
1033 t.fs.mask.dport = mask;
1034 } else if (!parse_val_mask("sport", args, &val, &mask)) {
1035 t.fs.val.sport = val;
1036 t.fs.mask.sport = mask;
1037 } else if (!parse_ipaddr("dip", args, &af, t.fs.val.dip,
1040 } else if (!parse_ipaddr("sip", args, &af, t.fs.val.sip,
1043 } else if (!strcmp(argv[start_arg], "action")) {
1044 if (!strcmp(argv[start_arg + 1], "pass"))
1045 t.fs.action = FILTER_PASS;
1046 else if (!strcmp(argv[start_arg + 1], "drop"))
1047 t.fs.action = FILTER_DROP;
1048 else if (!strcmp(argv[start_arg + 1], "switch"))
1049 t.fs.action = FILTER_SWITCH;
1051 warnx("invalid action \"%s\"; must be one of"
1052 " \"pass\", \"drop\" or \"switch\"",
1053 argv[start_arg + 1]);
1056 } else if (!parse_val("hitcnts", args, &val)) {
1058 } else if (!parse_val("prio", args, &val)) {
1060 } else if (!parse_val("rpttid", args, &val)) {
1062 } else if (!parse_val("queue", args, &val)) {
1065 } else if (!parse_val("tcbhash", args, &val)) {
1067 t.fs.dirsteerhash = 1;
1068 } else if (!parse_val("eport", args, &val)) {
1070 } else if (!strcmp(argv[start_arg], "dmac")) {
1071 struct ether_addr *daddr;
1073 daddr = ether_aton(argv[start_arg + 1]);
1074 if (daddr == NULL) {
1075 warnx("invalid dmac address \"%s\"",
1076 argv[start_arg + 1]);
1079 memcpy(t.fs.dmac, daddr, ETHER_ADDR_LEN);
1081 } else if (!strcmp(argv[start_arg], "smac")) {
1082 struct ether_addr *saddr;
1084 saddr = ether_aton(argv[start_arg + 1]);
1085 if (saddr == NULL) {
1086 warnx("invalid smac address \"%s\"",
1087 argv[start_arg + 1]);
1090 memcpy(t.fs.smac, saddr, ETHER_ADDR_LEN);
1092 } else if (!strcmp(argv[start_arg], "vlan")) {
1094 if (!strcmp(argv[start_arg + 1], "none")) {
1095 t.fs.newvlan = VLAN_REMOVE;
1096 } else if (argv[start_arg + 1][0] == '=') {
1097 t.fs.newvlan = VLAN_REWRITE;
1098 } else if (argv[start_arg + 1][0] == '+') {
1099 t.fs.newvlan = VLAN_INSERT;
1100 } else if (isdigit(argv[start_arg + 1][0]) &&
1101 !parse_val_mask("vlan", args, &val, &mask)) {
1102 t.fs.val.vlan = val;
1103 t.fs.mask.vlan = mask;
1104 t.fs.val.vlan_vld = 1;
1105 t.fs.mask.vlan_vld = 1;
1107 warnx("unknown vlan parameter \"%s\"; must"
1108 " be one of \"none\", \"=<vlan>\", "
1109 " \"+<vlan>\", or \"<vlan>\"",
1110 argv[start_arg + 1]);
1113 if (t.fs.newvlan == VLAN_REWRITE ||
1114 t.fs.newvlan == VLAN_INSERT) {
1115 t.fs.vlan = strtoul(argv[start_arg + 1] + 1,
1117 if (p == argv[start_arg + 1] + 1 || p[0] != 0) {
1118 warnx("invalid vlan \"%s\"",
1119 argv[start_arg + 1]);
1124 warnx("invalid parameter \"%s\"", argv[start_arg]);
1128 if (start_arg != argc) {
1129 warnx("no value for \"%s\"", argv[start_arg]);
1134 * Check basic sanity of option combinations.
1136 if (t.fs.action != FILTER_SWITCH &&
1137 (t.fs.eport || t.fs.newdmac || t.fs.newsmac || t.fs.newvlan)) {
1138 warnx("prio, port dmac, smac and vlan only make sense with"
1139 " \"action switch\"");
1142 if (t.fs.action != FILTER_PASS &&
1143 (t.fs.rpttid || t.fs.dirsteer || t.fs.maskhash)) {
1144 warnx("rpttid, queue and tcbhash don't make sense with"
1145 " action \"drop\" or \"switch\"");
1149 t.fs.type = (af == AF_INET6 ? 1 : 0); /* default IPv4 */
1150 return doit(CHELSIO_T4_SET_FILTER, &t);
1154 filter_cmd(int argc, const char *argv[])
1161 warnx("filter: no arguments.");
1166 if (strcmp(argv[0], "list") == 0) {
1168 warnx("trailing arguments after \"list\" ignored.");
1170 return show_filters();
1174 if (argc == 1 && strcmp(argv[0], "mode") == 0)
1175 return get_filter_mode();
1178 if (strcmp(argv[0], "mode") == 0)
1179 return set_filter_mode(argc - 1, argv + 1);
1182 s = str_to_number(argv[0], NULL, &val);
1183 if (*s || val > 0xffffffffU) {
1184 warnx("\"%s\" is neither an index nor a filter subcommand.",
1188 idx = (uint32_t) val;
1190 /* <idx> delete|clear */
1192 (strcmp(argv[1], "delete") == 0 || strcmp(argv[1], "clear") == 0)) {
1193 return del_filter(idx);
1196 /* <idx> [<param> <val>] ... */
1197 return set_filter(idx, argc - 1, argv + 1);
1201 * Shows the fields of a multi-word structure. The structure is considered to
1202 * consist of @nwords 32-bit words (i.e, it's an (@nwords * 32)-bit structure)
1203 * whose fields are described by @fd. The 32-bit words are given in @words
1204 * starting with the least significant 32-bit word.
1207 show_struct(const uint32_t *words, int nwords, const struct field_desc *fd)
1210 const struct field_desc *p;
1212 for (p = fd; p->name; p++)
1213 w = max(w, strlen(p->name));
1216 unsigned long long data;
1217 int first_word = fd->start / 32;
1218 int shift = fd->start % 32;
1219 int width = fd->end - fd->start + 1;
1220 unsigned long long mask = (1ULL << width) - 1;
1222 data = (words[first_word] >> shift) |
1223 ((uint64_t)words[first_word + 1] << (32 - shift));
1225 data |= ((uint64_t)words[first_word + 2] << (64 - shift));
1229 printf("%-*s ", w, fd->name);
1230 printf(fd->hex ? "%#llx\n" : "%llu\n", data << fd->shift);
1235 #define FIELD(name, start, end) { name, start, end, 0, 0, 0 }
1236 #define FIELD1(name, start) FIELD(name, start, start)
1239 show_sge_context(const struct t4_sge_context *p)
1241 static struct field_desc egress[] = {
1242 FIELD1("StatusPgNS:", 180),
1243 FIELD1("StatusPgRO:", 179),
1244 FIELD1("FetchNS:", 178),
1245 FIELD1("FetchRO:", 177),
1246 FIELD1("Valid:", 176),
1247 FIELD("PCIeDataChannel:", 174, 175),
1248 FIELD1("DCAEgrQEn:", 173),
1249 FIELD("DCACPUID:", 168, 172),
1250 FIELD1("FCThreshOverride:", 167),
1251 FIELD("WRLength:", 162, 166),
1252 FIELD1("WRLengthKnown:", 161),
1253 FIELD1("ReschedulePending:", 160),
1254 FIELD1("OnChipQueue:", 159),
1255 FIELD1("FetchSizeMode", 158),
1256 { "FetchBurstMin:", 156, 157, 4, 0, 1 },
1257 { "FetchBurstMax:", 153, 154, 6, 0, 1 },
1258 FIELD("uPToken:", 133, 152),
1259 FIELD1("uPTokenEn:", 132),
1260 FIELD1("UserModeIO:", 131),
1261 FIELD("uPFLCredits:", 123, 130),
1262 FIELD1("uPFLCreditEn:", 122),
1263 FIELD("FID:", 111, 121),
1264 FIELD("HostFCMode:", 109, 110),
1265 FIELD1("HostFCOwner:", 108),
1266 { "CIDXFlushThresh:", 105, 107, 0, 0, 1 },
1267 FIELD("CIDX:", 89, 104),
1268 FIELD("PIDX:", 73, 88),
1269 { "BaseAddress:", 18, 72, 9, 1 },
1270 FIELD("QueueSize:", 2, 17),
1271 FIELD1("QueueType:", 1),
1272 FIELD1("CachePriority:", 0),
1275 static struct field_desc fl[] = {
1276 FIELD1("StatusPgNS:", 180),
1277 FIELD1("StatusPgRO:", 179),
1278 FIELD1("FetchNS:", 178),
1279 FIELD1("FetchRO:", 177),
1280 FIELD1("Valid:", 176),
1281 FIELD("PCIeDataChannel:", 174, 175),
1282 FIELD1("DCAEgrQEn:", 173),
1283 FIELD("DCACPUID:", 168, 172),
1284 FIELD1("FCThreshOverride:", 167),
1285 FIELD("WRLength:", 162, 166),
1286 FIELD1("WRLengthKnown:", 161),
1287 FIELD1("ReschedulePending:", 160),
1288 FIELD1("OnChipQueue:", 159),
1289 FIELD1("FetchSizeMode", 158),
1290 { "FetchBurstMin:", 156, 157, 4, 0, 1 },
1291 { "FetchBurstMax:", 153, 154, 6, 0, 1 },
1292 FIELD1("FLMcongMode:", 152),
1293 FIELD("MaxuPFLCredits:", 144, 151),
1294 FIELD("FLMcontextID:", 133, 143),
1295 FIELD1("uPTokenEn:", 132),
1296 FIELD1("UserModeIO:", 131),
1297 FIELD("uPFLCredits:", 123, 130),
1298 FIELD1("uPFLCreditEn:", 122),
1299 FIELD("FID:", 111, 121),
1300 FIELD("HostFCMode:", 109, 110),
1301 FIELD1("HostFCOwner:", 108),
1302 { "CIDXFlushThresh:", 105, 107, 0, 0, 1 },
1303 FIELD("CIDX:", 89, 104),
1304 FIELD("PIDX:", 73, 88),
1305 { "BaseAddress:", 18, 72, 9, 1 },
1306 FIELD("QueueSize:", 2, 17),
1307 FIELD1("QueueType:", 1),
1308 FIELD1("CachePriority:", 0),
1311 static struct field_desc ingress[] = {
1312 FIELD1("NoSnoop:", 145),
1313 FIELD1("RelaxedOrdering:", 144),
1314 FIELD1("GTSmode:", 143),
1315 FIELD1("ISCSICoalescing:", 142),
1316 FIELD1("Valid:", 141),
1317 FIELD1("TimerPending:", 140),
1318 FIELD1("DropRSS:", 139),
1319 FIELD("PCIeChannel:", 137, 138),
1320 FIELD1("SEInterruptArmed:", 136),
1321 FIELD1("CongestionMgtEnable:", 135),
1322 FIELD1("DCAIngQEnable:", 134),
1323 FIELD("DCACPUID:", 129, 133),
1324 FIELD1("UpdateScheduling:", 128),
1325 FIELD("UpdateDelivery:", 126, 127),
1326 FIELD1("InterruptSent:", 125),
1327 FIELD("InterruptIDX:", 114, 124),
1328 FIELD1("InterruptDestination:", 113),
1329 FIELD1("InterruptArmed:", 112),
1330 FIELD("RxIntCounter:", 106, 111),
1331 FIELD("RxIntCounterThreshold:", 104, 105),
1332 FIELD1("Generation:", 103),
1333 { "BaseAddress:", 48, 102, 9, 1 },
1334 FIELD("PIDX:", 32, 47),
1335 FIELD("CIDX:", 16, 31),
1336 { "QueueSize:", 4, 15, 4, 0 },
1337 { "QueueEntrySize:", 2, 3, 4, 0, 1 },
1338 FIELD1("QueueEntryOverride:", 1),
1339 FIELD1("CachePriority:", 0),
1342 static struct field_desc flm[] = {
1343 FIELD1("NoSnoop:", 79),
1344 FIELD1("RelaxedOrdering:", 78),
1345 FIELD1("Valid:", 77),
1346 FIELD("DCACPUID:", 72, 76),
1347 FIELD1("DCAFLEn:", 71),
1348 FIELD("EQid:", 54, 70),
1349 FIELD("SplitEn:", 52, 53),
1350 FIELD1("PadEn:", 51),
1351 FIELD1("PackEn:", 50),
1352 FIELD1("DBpriority:", 48),
1353 FIELD("PackOffset:", 16, 47),
1354 FIELD("CIDX:", 8, 15),
1355 FIELD("PIDX:", 0, 7),
1358 static struct field_desc conm[] = {
1359 FIELD1("CngDBPHdr:", 6),
1360 FIELD1("CngDBPData:", 5),
1361 FIELD1("CngIMSG:", 4),
1362 FIELD("CngChMap:", 0, 3),
1366 if (p->mem_id == SGE_CONTEXT_EGRESS)
1367 show_struct(p->data, 6, (p->data[0] & 2) ? fl : egress);
1368 else if (p->mem_id == SGE_CONTEXT_FLM)
1369 show_struct(p->data, 3, flm);
1370 else if (p->mem_id == SGE_CONTEXT_INGRESS)
1371 show_struct(p->data, 5, ingress);
1372 else if (p->mem_id == SGE_CONTEXT_CNM)
1373 show_struct(p->data, 1, conm);
1380 get_sge_context(int argc, const char *argv[])
1385 struct t4_sge_context cntxt = {0};
1388 warnx("sge_context: incorrect number of arguments.");
1392 if (!strcmp(argv[0], "egress"))
1393 cntxt.mem_id = SGE_CONTEXT_EGRESS;
1394 else if (!strcmp(argv[0], "ingress"))
1395 cntxt.mem_id = SGE_CONTEXT_INGRESS;
1396 else if (!strcmp(argv[0], "fl"))
1397 cntxt.mem_id = SGE_CONTEXT_FLM;
1398 else if (!strcmp(argv[0], "cong"))
1399 cntxt.mem_id = SGE_CONTEXT_CNM;
1401 warnx("unknown context type \"%s\"; known types are egress, "
1402 "ingress, fl, and cong.", argv[0]);
1406 p = str_to_number(argv[1], &cid, NULL);
1408 warnx("invalid context id \"%s\"", argv[1]);
1413 rc = doit(CHELSIO_T4_GET_SGE_CONTEXT, &cntxt);
1417 show_sge_context(&cntxt);
1422 loadfw(int argc, const char *argv[])
1425 struct t4_data data = {0};
1426 const char *fname = argv[0];
1427 struct stat st = {0};
1430 warnx("loadfw: incorrect number of arguments.");
1434 fd = open(fname, O_RDONLY);
1436 warn("open(%s)", fname);
1440 if (fstat(fd, &st) < 0) {
1446 data.len = st.st_size;
1447 data.data = mmap(0, data.len, PROT_READ, 0, fd, 0);
1448 if (data.data == MAP_FAILED) {
1454 rc = doit(CHELSIO_T4_LOAD_FW, &data);
1455 munmap(data.data, data.len);
1461 read_mem(uint32_t addr, uint32_t len, void (*output)(uint32_t *, uint32_t))
1464 struct t4_mem_range mr;
1468 mr.data = malloc(mr.len);
1471 warn("read_mem: malloc");
1475 rc = doit(CHELSIO_T4_GET_MEM, &mr);
1480 (*output)(mr.data, mr.len);
1487 * Display memory as list of 'n' 4-byte values per line.
1490 show_mem(uint32_t *buf, uint32_t len)
1496 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1498 printf("%s%08x", s, htonl(*buf));
1505 memdump(int argc, const char *argv[])
1512 warnx("incorrect number of arguments.");
1516 p = str_to_number(argv[0], &l, NULL);
1518 warnx("invalid address \"%s\"", argv[0]);
1523 p = str_to_number(argv[1], &l, NULL);
1525 warnx("memdump: invalid length \"%s\"", argv[1]);
1530 return (read_mem(addr, len, show_mem));
1534 * Display TCB as list of 'n' 4-byte values per line.
1537 show_tcb(uint32_t *buf, uint32_t len)
1543 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1545 printf("%s%08x", s, htonl(*buf));
1551 #define A_TP_CMM_TCB_BASE 0x7d10
1552 #define TCB_SIZE 128
1554 read_tcb(int argc, const char *argv[])
1564 warnx("incorrect number of arguments.");
1568 p = str_to_number(argv[0], &l, NULL);
1570 warnx("invalid tid \"%s\"", argv[0]);
1575 rc = read_reg(A_TP_CMM_TCB_BASE, 4, &val);
1579 addr = val + tid * TCB_SIZE;
1581 return (read_mem(addr, TCB_SIZE, show_tcb));
1585 read_i2c(int argc, const char *argv[])
1589 struct t4_i2c_data i2cd;
1592 if (argc < 3 || argc > 4) {
1593 warnx("incorrect number of arguments.");
1597 p = str_to_number(argv[0], &l, NULL);
1598 if (*p || l > UCHAR_MAX) {
1599 warnx("invalid port id \"%s\"", argv[0]);
1604 p = str_to_number(argv[1], &l, NULL);
1605 if (*p || l > UCHAR_MAX) {
1606 warnx("invalid i2c device address \"%s\"", argv[1]);
1611 p = str_to_number(argv[2], &l, NULL);
1612 if (*p || l > UCHAR_MAX) {
1613 warnx("invalid byte offset \"%s\"", argv[2]);
1619 p = str_to_number(argv[3], &l, NULL);
1620 if (*p || l > sizeof(i2cd.data)) {
1621 warnx("invalid number of bytes \"%s\"", argv[3]);
1628 rc = doit(CHELSIO_T4_GET_I2C, &i2cd);
1632 for (i = 0; i < i2cd.len; i++)
1633 printf("0x%x [%u]\n", i2cd.data[i], i2cd.data[i]);
1639 clearstats(int argc, const char *argv[])
1646 warnx("incorrect number of arguments.");
1650 p = str_to_number(argv[0], &l, NULL);
1652 warnx("invalid port id \"%s\"", argv[0]);
1657 return doit(CHELSIO_T4_CLEAR_STATS, &port);
1661 run_cmd(int argc, const char *argv[])
1664 const char *cmd = argv[0];
1670 if (!strcmp(cmd, "reg") || !strcmp(cmd, "reg32"))
1671 rc = register_io(argc, argv, 4);
1672 else if (!strcmp(cmd, "reg64"))
1673 rc = register_io(argc, argv, 8);
1674 else if (!strcmp(cmd, "regdump"))
1675 rc = dump_regs(argc, argv);
1676 else if (!strcmp(cmd, "filter"))
1677 rc = filter_cmd(argc, argv);
1678 else if (!strcmp(cmd, "context"))
1679 rc = get_sge_context(argc, argv);
1680 else if (!strcmp(cmd, "loadfw"))
1681 rc = loadfw(argc, argv);
1682 else if (!strcmp(cmd, "memdump"))
1683 rc = memdump(argc, argv);
1684 else if (!strcmp(cmd, "tcb"))
1685 rc = read_tcb(argc, argv);
1686 else if (!strcmp(cmd, "i2c"))
1687 rc = read_i2c(argc, argv);
1688 else if (!strcmp(cmd, "clearstats"))
1689 rc = clearstats(argc, argv);
1692 warnx("invalid command \"%s\"", cmd);
1703 char buffer[128], *buf;
1704 const char *args[MAX_ARGS + 1];
1707 * Simple loop: displays a "> " prompt and processes any input as a
1708 * cxgbetool command. You're supposed to enter only the part after
1709 * "cxgbetool t4nexX". Use "quit" or "exit" to exit.
1712 fprintf(stdout, "> ");
1714 buf = fgets(buffer, sizeof(buffer), stdin);
1716 if (ferror(stdin)) {
1717 warn("stdin error");
1718 rc = errno; /* errno from fgets */
1724 while ((args[i] = strsep(&buf, " \t\n")) != NULL) {
1725 if (args[i][0] != 0 && ++i == MAX_ARGS)
1731 continue; /* skip empty line */
1733 if (!strcmp(args[0], "quit") || !strcmp(args[0], "exit"))
1736 rc = run_cmd(i, args);
1739 /* rc normally comes from the last command (not including quit/exit) */
1744 main(int argc, const char *argv[])
1751 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1764 /* progname and nexus */
1768 if (argc == 1 && !strcmp(argv[0], "stdio"))
1769 rc = run_cmd_loop();
1771 rc = run_cmd(argc, argv);