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$");
39 #include <sys/ioctl.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <net/ethernet.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
50 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
52 #define max(x, y) ((x) > (y) ? (x) : (y))
54 static const char *progname, *nexus;
64 const struct reg_info *ri;
68 const char *name; /* Field name */
69 unsigned short start; /* Start bit position */
70 unsigned short end; /* End bit position */
71 unsigned char shift; /* # of low order bits omitted and implicitly 0 */
72 unsigned char hex; /* Print field in hex instead of decimal */
73 unsigned char islog2; /* Field contains the base-2 log of the value */
76 #include "reg_defs_t4.c"
77 #include "reg_defs_t4vf.c"
82 fprintf(fp, "Usage: %s <nexus> [operation]\n", progname);
84 "\tcontext <type> <id> show an SGE context\n"
85 "\tfilter <idx> [<param> <val>] ... set a filter\n"
86 "\tfilter <idx> delete|clear delete a filter\n"
87 "\tfilter list list all filters\n"
88 "\tfilter mode [<match>] ... get/set global filter mode\n"
89 "\tloadfw <fw-image.bin> install firmware\n"
90 "\tmemdump <addr> <len> dump a memory range\n"
91 "\treg <address>[=<val>] read/write register\n"
92 "\treg64 <address>[=<val>] read/write 64 bit register\n"
93 "\tregdump [<module>] ... dump registers\n"
94 "\tstdio interactive mode\n"
95 "\ttcb <tid> read TCB\n"
99 static inline unsigned int
100 get_card_vers(unsigned int version)
102 return (version & 0x3ff);
106 real_doit(unsigned long cmd, void *data, const char *cmdstr)
114 snprintf(buf, sizeof(buf), "/dev/%s", nexus);
115 if ((fd = open(buf, O_RDWR)) < 0) {
116 warn("open(%s)", nexus);
122 rc = ioctl(fd, cmd, data);
130 #define doit(x, y) real_doit(x, y, #x)
133 str_to_number(const char *s, long *val, long long *vall)
138 *vall = strtoll(s, &p, 0);
140 *val = strtol(s, &p, 0);
148 read_reg(long addr, int size, long long *val)
153 reg.addr = (uint32_t) addr;
154 reg.size = (uint32_t) size;
157 rc = doit(CHELSIO_T4_GETREG, ®);
165 write_reg(long addr, int size, long long val)
169 reg.addr = (uint32_t) addr;
170 reg.size = (uint32_t) size;
171 reg.val = (uint64_t) val;
173 return doit(CHELSIO_T4_SETREG, ®);
177 register_io(int argc, const char *argv[], int size)
185 /* <reg> OR <reg>=<value> */
187 p = str_to_number(argv[0], &addr, NULL);
190 warnx("invalid register \"%s\"", argv[0]);
196 p = str_to_number(v, NULL, &val);
199 warnx("invalid value \"%s\"", v);
204 } else if (argc == 2) {
209 p = str_to_number(argv[0], &addr, NULL);
211 warnx("invalid register \"%s\"", argv[0]);
215 p = str_to_number(argv[1], NULL, &val);
217 warnx("invalid value \"%s\"", argv[1]);
221 warnx("reg: invalid number of arguments (%d)", argc);
226 rc = write_reg(addr, size, val);
228 rc = read_reg(addr, size, &val);
230 printf("0x%llx [%llu]\n", val, val);
236 static inline uint32_t
237 xtract(uint32_t val, int shift, int len)
239 return (val >> shift) & ((1 << len) - 1);
243 dump_block_regs(const struct reg_info *reg_array, const uint32_t *regs)
245 uint32_t reg_val = 0;
247 for ( ; reg_array->name; ++reg_array)
248 if (!reg_array->len) {
249 reg_val = regs[reg_array->addr / 4];
250 printf("[%#7x] %-47s %#-10x %u\n", reg_array->addr,
251 reg_array->name, reg_val, reg_val);
253 uint32_t v = xtract(reg_val, reg_array->addr,
256 printf(" %*u:%u %-47s %#-10x %u\n",
257 reg_array->addr < 10 ? 3 : 2,
258 reg_array->addr + reg_array->len - 1,
259 reg_array->addr, reg_array->name, v, v);
266 dump_regs_table(int argc, const char *argv[], const uint32_t *regs,
267 const struct mod_regs *modtab, int nmodules)
271 for (i = 0; i < argc; i++) {
272 for (j = 0; j < nmodules; j++) {
273 if (!strcmp(argv[i], modtab[j].name))
278 warnx("invalid register block \"%s\"", argv[i]);
279 fprintf(stderr, "\nAvailable blocks:");
280 for ( ; nmodules; nmodules--, modtab++)
281 fprintf(stderr, " %s", modtab->name);
282 fprintf(stderr, "\n");
287 for ( ; nmodules; nmodules--, modtab++) {
289 match = argc == 0 ? 1 : 0;
290 for (i = 0; !match && i < argc; i++) {
291 if (!strcmp(argv[i], modtab->name))
296 dump_block_regs(modtab->ri, regs);
302 #define T4_MODREGS(name) { #name, t4_##name##_regs }
304 dump_regs_t4(int argc, const char *argv[], const uint32_t *regs)
306 static struct mod_regs t4_mod[] = {
308 { "pci", t4_pcie_regs },
312 { "edc0", t4_edc_0_regs },
313 { "edc1", t4_edc_1_regs },
318 { "pmrx", t4_pm_rx_regs },
319 { "pmtx", t4_pm_tx_regs },
321 { "cplsw", t4_cpl_switch_regs },
323 { "i2c", t4_i2cm_regs },
334 return dump_regs_table(argc, argv, regs, t4_mod, ARRAY_SIZE(t4_mod));
339 dump_regs_t4vf(int argc, const char *argv[], const uint32_t *regs)
341 static struct mod_regs t4vf_mod[] = {
342 { "sge", t4vf_sge_regs },
343 { "mps", t4vf_mps_regs },
344 { "pl", t4vf_pl_regs },
345 { "mbdata", t4vf_mbdata_regs },
346 { "cim", t4vf_cim_regs },
349 return dump_regs_table(argc, argv, regs, t4vf_mod,
350 ARRAY_SIZE(t4vf_mod));
354 dump_regs(int argc, const char *argv[])
356 int vers, revision, is_pcie, rc;
357 struct t4_regdump regs;
359 regs.data = calloc(1, T4_REGDUMP_SIZE);
360 if (regs.data == NULL) {
361 warnc(ENOMEM, "regdump");
365 regs.len = T4_REGDUMP_SIZE;
366 rc = doit(CHELSIO_T4_REGDUMP, ®s);
370 vers = get_card_vers(regs.version);
371 revision = (regs.version >> 10) & 0x3f;
372 is_pcie = (regs.version & 0x80000000) != 0;
375 if (revision == 0x3f)
376 rc = dump_regs_t4vf(argc, argv, regs.data);
378 rc = dump_regs_t4(argc, argv, regs.data);
380 warnx("%s (type %d, rev %d) is not a T4 card.",
381 nexus, vers, revision);
390 do_show_info_header(uint32_t mode)
394 printf ("%4s %8s", "Idx", "Hits");
395 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
406 printf (" vld:VNIC");
410 printf (" vld:VLAN");
413 case T4_FILTER_IP_TOS:
417 case T4_FILTER_IP_PROTO:
421 case T4_FILTER_ETH_TYPE:
425 case T4_FILTER_MAC_IDX:
429 case T4_FILTER_MPS_HIT_TYPE:
433 case T4_FILTER_IP_FRAGMENT:
438 /* compressed filter field not enabled */
442 printf(" %20s %20s %9s %9s %s\n",
443 "DIP", "SIP", "DPORT", "SPORT", "Action");
447 * Parse an argument sub-vector as a { <parameter name> <value>[:<mask>] }
448 * ordered tuple. If the parameter name in the argument sub-vector does not
449 * match the passed in parameter name, then a zero is returned for the
450 * function and no parsing is performed. If there is a match, then the value
451 * and optional mask are parsed and returned in the provided return value
452 * pointers. If no optional mask is specified, then a default mask of all 1s
455 * An error in parsing the value[:mask] will result in an error message and
456 * program termination.
459 parse_val_mask(const char *param, const char *args[], uint32_t *val,
464 if (strcmp(param, args[0]) != 0)
467 *val = strtoul(args[1], &p, 0);
474 if (p[0] == ':' && p[1] != 0) {
475 *mask = strtoul(p+1, &p, 0);
481 warnx("parameter \"%s\" has bad \"value[:mask]\" %s",
488 * Parse an argument sub-vector as a { <parameter name> <addr>[/<mask>] }
489 * ordered tuple. If the parameter name in the argument sub-vector does not
490 * match the passed in parameter name, then a zero is returned for the
491 * function and no parsing is performed. If there is a match, then the value
492 * and optional mask are parsed and returned in the provided return value
493 * pointers. If no optional mask is specified, then a default mask of all 1s
496 * The value return parameter "afp" is used to specify the expected address
497 * family -- IPv4 or IPv6 -- of the address[/mask] and return its actual
498 * format. A passed in value of AF_UNSPEC indicates that either IPv4 or IPv6
499 * is acceptable; AF_INET means that only IPv4 addresses are acceptable; and
500 * AF_INET6 means that only IPv6 are acceptable. AF_INET is returned for IPv4
501 * and AF_INET6 for IPv6 addresses, respectively. IPv4 address/mask pairs are
502 * returned in the first four bytes of the address and mask return values with
503 * the address A.B.C.D returned with { A, B, C, D } returned in addresses { 0,
504 * 1, 2, 3}, respectively.
506 * An error in parsing the value[:mask] will result in an error message and
507 * program termination.
510 parse_ipaddr(const char *param, const char *args[], int *afp, uint8_t addr[],
513 const char *colon, *afn;
517 unsigned int masksize;
520 * Is this our parameter?
522 if (strcmp(param, args[0]) != 0)
526 * Fundamental IPv4 versus IPv6 selection.
528 colon = strchr(args[1], ':');
538 if (*afp == AF_UNSPEC)
540 else if (*afp != af) {
541 warnx("address %s is not of expected family %s",
542 args[1], *afp == AF_INET ? "IP" : "IPv6");
547 * Parse address (temporarily stripping off any "/mask"
550 slash = strchr(args[1], '/');
553 ret = inet_pton(af, args[1], addr);
557 warnx("Cannot parse %s %s address %s", param, afn, args[1]);
562 * Parse optional mask specification.
566 unsigned int prefix = strtoul(slash + 1, &p, 10);
568 if (p == slash + 1) {
569 warnx("missing address prefix for %s", param);
573 warnx("%s is not a valid address prefix", slash + 1);
576 if (prefix > masksize) {
577 warnx("prefix %u is too long for an %s address",
581 memset(mask, 0, masksize / 8);
588 for (m = mask; masksize >= 8; m++, masksize -= 8)
591 *m = ~0 << (8 - masksize);
597 * Parse an argument sub-vector as a { <parameter name> <value> } ordered
598 * tuple. If the parameter name in the argument sub-vector does not match the
599 * passed in parameter name, then a zero is returned for the function and no
600 * parsing is performed. If there is a match, then the value is parsed and
601 * returned in the provided return value pointer.
604 parse_val(const char *param, const char *args[], uint32_t *val)
608 if (strcmp(param, args[0]) != 0)
611 *val = strtoul(args[1], &p, 0);
612 if (p > args[1] && p[0] == 0)
615 warnx("parameter \"%s\" has bad \"value\" %s", args[0], args[1]);
620 filters_show_ipaddr(int type, uint8_t *addr, uint8_t *addrm)
631 for (octet = 0; octet < noctets; octet++)
632 printf("%02x", addr[octet]);
634 for (octet = 0; octet < noctets; octet++)
635 printf("%02x", addrm[octet]);
639 do_show_one_filter_info(struct t4_filter *t, uint32_t mode)
643 printf("%4d", t->idx);
644 if (t->hits == UINT64_MAX)
647 printf(" %8ju", t->hits);
650 * Compressed header portion of filter.
652 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
655 printf(" %1d/%1d", t->fs.val.fcoe, t->fs.mask.fcoe);
659 printf(" %1d/%1d", t->fs.val.iport, t->fs.mask.iport);
663 printf(" %1d:%1x:%02x/%1d:%1x:%02x",
664 t->fs.val.vnic_vld, (t->fs.val.vnic >> 7) & 0x7,
665 t->fs.val.vnic & 0x7f, t->fs.mask.vnic_vld,
666 (t->fs.mask.vnic >> 7) & 0x7,
667 t->fs.mask.vnic & 0x7f);
671 printf(" %1d:%04x/%1d:%04x",
672 t->fs.val.vlan_vld, t->fs.val.vlan,
673 t->fs.mask.vlan_vld, t->fs.mask.vlan);
676 case T4_FILTER_IP_TOS:
677 printf(" %02x/%02x", t->fs.val.tos, t->fs.mask.tos);
680 case T4_FILTER_IP_PROTO:
681 printf(" %02x/%02x", t->fs.val.proto, t->fs.mask.proto);
684 case T4_FILTER_ETH_TYPE:
685 printf(" %04x/%04x", t->fs.val.ethtype,
689 case T4_FILTER_MAC_IDX:
690 printf(" %03x/%03x", t->fs.val.macidx,
694 case T4_FILTER_MPS_HIT_TYPE:
695 printf(" %1x/%1x", t->fs.val.matchtype,
696 t->fs.mask.matchtype);
699 case T4_FILTER_IP_FRAGMENT:
700 printf(" %1d/%1d", t->fs.val.frag, t->fs.mask.frag);
704 /* compressed filter field not enabled */
710 * Fixed portion of filter.
712 filters_show_ipaddr(t->fs.type, t->fs.val.dip, t->fs.mask.dip);
713 filters_show_ipaddr(t->fs.type, t->fs.val.sip, t->fs.mask.sip);
714 printf(" %04x/%04x %04x/%04x",
715 t->fs.val.dport, t->fs.mask.dport,
716 t->fs.val.sport, t->fs.mask.sport);
719 * Variable length filter action.
721 if (t->fs.action == FILTER_DROP)
723 else if (t->fs.action == FILTER_SWITCH) {
724 printf(" Switch: port=%d", t->fs.eport);
727 ", dmac=%02x:%02x:%02x:%02x:%02x:%02x "
729 t->fs.dmac[0], t->fs.dmac[1],
730 t->fs.dmac[2], t->fs.dmac[3],
731 t->fs.dmac[4], t->fs.dmac[5],
735 ", smac=%02x:%02x:%02x:%02x:%02x:%02x "
737 t->fs.smac[0], t->fs.smac[1],
738 t->fs.smac[2], t->fs.smac[3],
739 t->fs.smac[4], t->fs.smac[5],
741 if (t->fs.newvlan == VLAN_REMOVE)
742 printf(", vlan=none");
743 else if (t->fs.newvlan == VLAN_INSERT)
744 printf(", vlan=insert(%x)", t->fs.vlan);
745 else if (t->fs.newvlan == VLAN_REWRITE)
746 printf(", vlan=rewrite(%x)", t->fs.vlan);
749 if (t->fs.dirsteer == 0) {
752 printf("(TCB=hash)");
754 printf("%d", t->fs.iq);
755 if (t->fs.dirsteerhash == 0)
771 uint32_t mode = 0, header = 0;
775 /* Get the global filter mode first */
776 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
781 for (t.idx = 0; ; t.idx++) {
782 rc = doit(CHELSIO_T4_GET_FILTER, &t);
783 if (rc != 0 || t.idx == 0xffffffff)
787 do_show_info_header(mode);
790 do_show_one_filter_info(&t, mode);
797 get_filter_mode(void)
802 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
806 if (mode & T4_FILTER_IPv4)
809 if (mode & T4_FILTER_IPv6)
812 if (mode & T4_FILTER_IP_SADDR)
815 if (mode & T4_FILTER_IP_DADDR)
818 if (mode & T4_FILTER_IP_SPORT)
821 if (mode & T4_FILTER_IP_DPORT)
824 if (mode & T4_FILTER_MPS_HIT_TYPE)
825 printf("matchtype ");
827 if (mode & T4_FILTER_MAC_IDX)
830 if (mode & T4_FILTER_ETH_TYPE)
833 if (mode & T4_FILTER_IP_PROTO)
836 if (mode & T4_FILTER_IP_TOS)
839 if (mode & T4_FILTER_VLAN)
842 if (mode & T4_FILTER_VNIC)
845 if (mode & T4_FILTER_PORT)
848 if (mode & T4_FILTER_FCoE)
857 set_filter_mode(int argc, const char *argv[])
861 for (; argc; argc--, argv++) {
862 if (!strcmp(argv[0], "matchtype"))
863 mode |= T4_FILTER_MPS_HIT_TYPE;
865 if (!strcmp(argv[0], "macidx"))
866 mode |= T4_FILTER_MAC_IDX;
868 if (!strcmp(argv[0], "ethtype"))
869 mode |= T4_FILTER_ETH_TYPE;
871 if (!strcmp(argv[0], "proto"))
872 mode |= T4_FILTER_IP_PROTO;
874 if (!strcmp(argv[0], "tos"))
875 mode |= T4_FILTER_IP_TOS;
877 if (!strcmp(argv[0], "vlan"))
878 mode |= T4_FILTER_VLAN;
880 if (!strcmp(argv[0], "ovlan") ||
881 !strcmp(argv[0], "vnic"))
882 mode |= T4_FILTER_VNIC;
884 if (!strcmp(argv[0], "iport"))
885 mode |= T4_FILTER_PORT;
887 if (!strcmp(argv[0], "fcoe"))
888 mode |= T4_FILTER_FCoE;
891 return doit(CHELSIO_T4_SET_FILTER_MODE, &mode);
895 del_filter(uint32_t idx)
901 return doit(CHELSIO_T4_DEL_FILTER, &t);
905 set_filter(uint32_t idx, int argc, const char *argv[])
907 int af = AF_UNSPEC, start_arg = 0;
911 warnc(EINVAL, "%s", __func__);
914 bzero(&t, sizeof (t));
917 for (start_arg = 0; start_arg + 2 <= argc; start_arg += 2) {
918 const char **args = &argv[start_arg];
921 if (!strcmp(argv[start_arg], "type")) {
923 if (!strcasecmp(argv[start_arg + 1], "ipv4"))
925 else if (!strcasecmp(argv[start_arg + 1], "ipv6"))
928 warnx("invalid type \"%s\"; "
929 "must be one of \"ipv4\" or \"ipv6\"",
930 argv[start_arg + 1]);
934 if (af != AF_UNSPEC && af != newaf) {
935 warnx("conflicting IPv4/IPv6 specifications.");
939 } else if (!parse_val_mask("fcoe", args, &val, &mask)) {
941 t.fs.mask.fcoe = mask;
942 } else if (!parse_val_mask("iport", args, &val, &mask)) {
943 t.fs.val.iport = val;
944 t.fs.mask.iport = mask;
945 } else if (!parse_val_mask("ovlan", args, &val, &mask)) {
947 t.fs.mask.vnic = mask;
948 t.fs.val.vnic_vld = 1;
949 t.fs.mask.vnic_vld = 1;
950 } else if (!parse_val_mask("vnic", args, &val, &mask)) {
952 t.fs.mask.vnic = mask;
953 t.fs.val.vnic_vld = 1;
954 t.fs.mask.vnic_vld = 1;
955 } else if (!parse_val_mask("vlan", args, &val, &mask)) {
957 t.fs.mask.vlan = mask;
958 t.fs.val.vlan_vld = 1;
959 t.fs.mask.vlan_vld = 1;
960 } else if (!parse_val_mask("tos", args, &val, &mask)) {
962 t.fs.mask.tos = mask;
963 } else if (!parse_val_mask("proto", args, &val, &mask)) {
964 t.fs.val.proto = val;
965 t.fs.mask.proto = mask;
966 } else if (!parse_val_mask("ethtype", args, &val, &mask)) {
967 t.fs.val.ethtype = val;
968 t.fs.mask.ethtype = mask;
969 } else if (!parse_val_mask("macidx", args, &val, &mask)) {
970 t.fs.val.macidx = val;
971 t.fs.mask.macidx = mask;
972 } else if (!parse_val_mask("matchtype", args, &val, &mask)) {
973 t.fs.val.matchtype = val;
974 t.fs.mask.matchtype = mask;
975 } else if (!parse_val_mask("frag", args, &val, &mask)) {
977 t.fs.mask.frag = mask;
978 } else if (!parse_val_mask("dport", args, &val, &mask)) {
979 t.fs.val.dport = val;
980 t.fs.mask.dport = mask;
981 } else if (!parse_val_mask("sport", args, &val, &mask)) {
982 t.fs.val.sport = val;
983 t.fs.mask.sport = mask;
984 } else if (!parse_ipaddr("dip", args, &af, t.fs.val.dip,
987 } else if (!parse_ipaddr("sip", args, &af, t.fs.val.sip,
990 } else if (!strcmp(argv[start_arg], "action")) {
991 if (!strcmp(argv[start_arg + 1], "pass"))
992 t.fs.action = FILTER_PASS;
993 else if (!strcmp(argv[start_arg + 1], "drop"))
994 t.fs.action = FILTER_DROP;
995 else if (!strcmp(argv[start_arg + 1], "switch"))
996 t.fs.action = FILTER_SWITCH;
998 warnx("invalid action \"%s\"; must be one of"
999 " \"pass\", \"drop\" or \"switch\"",
1000 argv[start_arg + 1]);
1003 } else if (!parse_val("hitcnts", args, &val)) {
1005 } else if (!parse_val("prio", args, &val)) {
1007 } else if (!parse_val("rpttid", args, &val)) {
1009 } else if (!parse_val("queue", args, &val)) {
1012 } else if (!parse_val("tcbhash", args, &val)) {
1014 t.fs.dirsteerhash = 1;
1015 } else if (!parse_val("eport", args, &val)) {
1017 } else if (!strcmp(argv[start_arg], "dmac")) {
1018 struct ether_addr *daddr;
1020 daddr = ether_aton(argv[start_arg + 1]);
1021 if (daddr == NULL) {
1022 warnx("invalid dmac address \"%s\"",
1023 argv[start_arg + 1]);
1026 memcpy(t.fs.dmac, daddr, ETHER_ADDR_LEN);
1028 } else if (!strcmp(argv[start_arg], "smac")) {
1029 struct ether_addr *saddr;
1031 saddr = ether_aton(argv[start_arg + 1]);
1032 if (saddr == NULL) {
1033 warnx("invalid smac address \"%s\"",
1034 argv[start_arg + 1]);
1037 memcpy(t.fs.smac, saddr, ETHER_ADDR_LEN);
1039 } else if (!strcmp(argv[start_arg], "vlan")) {
1041 if (!strcmp(argv[start_arg + 1], "none")) {
1042 t.fs.newvlan = VLAN_REMOVE;
1043 } else if (argv[start_arg + 1][0] == '=') {
1044 t.fs.newvlan = VLAN_REWRITE;
1045 } else if (argv[start_arg + 1][0] == '+') {
1046 t.fs.newvlan = VLAN_INSERT;
1048 warnx("unknown vlan parameter \"%s\"; must"
1049 " be one of \"none\", \"=<vlan>\" or"
1050 " \"+<vlan>\"", argv[start_arg + 1]);
1053 if (t.fs.newvlan == VLAN_REWRITE ||
1054 t.fs.newvlan == VLAN_INSERT) {
1055 t.fs.vlan = strtoul(argv[start_arg + 1] + 1,
1057 if (p == argv[start_arg + 1] + 1 || p[0] != 0) {
1058 warnx("invalid vlan \"%s\"",
1059 argv[start_arg + 1]);
1064 warnx("invalid parameter \"%s\"", argv[start_arg]);
1068 if (start_arg != argc) {
1069 warnx("no value for \"%s\"", argv[start_arg]);
1074 * Check basic sanity of option combinations.
1076 if (t.fs.action != FILTER_SWITCH &&
1077 (t.fs.eport || t.fs.newdmac || t.fs.newsmac || t.fs.newvlan)) {
1078 warnx("prio, port dmac, smac and vlan only make sense with"
1079 " \"action switch\"");
1082 if (t.fs.action != FILTER_PASS &&
1083 (t.fs.rpttid || t.fs.dirsteer || t.fs.maskhash)) {
1084 warnx("rpttid, queue and tcbhash don't make sense with"
1085 " action \"drop\" or \"switch\"");
1089 t.fs.type = (af == AF_INET6 ? 1 : 0); /* default IPv4 */
1090 return doit(CHELSIO_T4_SET_FILTER, &t);
1094 filter_cmd(int argc, const char *argv[])
1101 warnx("filter: no arguments.");
1106 if (strcmp(argv[0], "list") == 0) {
1108 warnx("trailing arguments after \"list\" ignored.");
1110 return show_filters();
1114 if (argc == 1 && strcmp(argv[0], "mode") == 0)
1115 return get_filter_mode();
1118 if (strcmp(argv[0], "mode") == 0)
1119 return set_filter_mode(argc - 1, argv + 1);
1122 s = str_to_number(argv[0], NULL, &val);
1123 if (*s || val > 0xffffffffU) {
1124 warnx("\"%s\" is neither an index nor a filter subcommand.",
1128 idx = (uint32_t) val;
1130 /* <idx> delete|clear */
1132 (strcmp(argv[1], "delete") == 0 || strcmp(argv[1], "clear") == 0)) {
1133 return del_filter(idx);
1136 /* <idx> [<param> <val>] ... */
1137 return set_filter(idx, argc - 1, argv + 1);
1141 * Shows the fields of a multi-word structure. The structure is considered to
1142 * consist of @nwords 32-bit words (i.e, it's an (@nwords * 32)-bit structure)
1143 * whose fields are described by @fd. The 32-bit words are given in @words
1144 * starting with the least significant 32-bit word.
1147 show_struct(const uint32_t *words, int nwords, const struct field_desc *fd)
1150 const struct field_desc *p;
1152 for (p = fd; p->name; p++)
1153 w = max(w, strlen(p->name));
1156 unsigned long long data;
1157 int first_word = fd->start / 32;
1158 int shift = fd->start % 32;
1159 int width = fd->end - fd->start + 1;
1160 unsigned long long mask = (1ULL << width) - 1;
1162 data = (words[first_word] >> shift) |
1163 ((uint64_t)words[first_word + 1] << (32 - shift));
1165 data |= ((uint64_t)words[first_word + 2] << (64 - shift));
1169 printf("%-*s ", w, fd->name);
1170 printf(fd->hex ? "%#llx\n" : "%llu\n", data << fd->shift);
1175 #define FIELD(name, start, end) { name, start, end, 0, 0, 0 }
1176 #define FIELD1(name, start) FIELD(name, start, start)
1179 show_sge_context(const struct t4_sge_context *p)
1181 static struct field_desc egress[] = {
1182 FIELD1("StatusPgNS:", 180),
1183 FIELD1("StatusPgRO:", 179),
1184 FIELD1("FetchNS:", 178),
1185 FIELD1("FetchRO:", 177),
1186 FIELD1("Valid:", 176),
1187 FIELD("PCIeDataChannel:", 174, 175),
1188 FIELD1("DCAEgrQEn:", 173),
1189 FIELD("DCACPUID:", 168, 172),
1190 FIELD1("FCThreshOverride:", 167),
1191 FIELD("WRLength:", 162, 166),
1192 FIELD1("WRLengthKnown:", 161),
1193 FIELD1("ReschedulePending:", 160),
1194 FIELD1("OnChipQueue:", 159),
1195 FIELD1("FetchSizeMode", 158),
1196 { "FetchBurstMin:", 156, 157, 4, 0, 1 },
1197 { "FetchBurstMax:", 153, 154, 6, 0, 1 },
1198 FIELD("uPToken:", 133, 152),
1199 FIELD1("uPTokenEn:", 132),
1200 FIELD1("UserModeIO:", 131),
1201 FIELD("uPFLCredits:", 123, 130),
1202 FIELD1("uPFLCreditEn:", 122),
1203 FIELD("FID:", 111, 121),
1204 FIELD("HostFCMode:", 109, 110),
1205 FIELD1("HostFCOwner:", 108),
1206 { "CIDXFlushThresh:", 105, 107, 0, 0, 1 },
1207 FIELD("CIDX:", 89, 104),
1208 FIELD("PIDX:", 73, 88),
1209 { "BaseAddress:", 18, 72, 9, 1 },
1210 FIELD("QueueSize:", 2, 17),
1211 FIELD1("QueueType:", 1),
1212 FIELD1("CachePriority:", 0),
1215 static struct field_desc fl[] = {
1216 FIELD1("StatusPgNS:", 180),
1217 FIELD1("StatusPgRO:", 179),
1218 FIELD1("FetchNS:", 178),
1219 FIELD1("FetchRO:", 177),
1220 FIELD1("Valid:", 176),
1221 FIELD("PCIeDataChannel:", 174, 175),
1222 FIELD1("DCAEgrQEn:", 173),
1223 FIELD("DCACPUID:", 168, 172),
1224 FIELD1("FCThreshOverride:", 167),
1225 FIELD("WRLength:", 162, 166),
1226 FIELD1("WRLengthKnown:", 161),
1227 FIELD1("ReschedulePending:", 160),
1228 FIELD1("OnChipQueue:", 159),
1229 FIELD1("FetchSizeMode", 158),
1230 { "FetchBurstMin:", 156, 157, 4, 0, 1 },
1231 { "FetchBurstMax:", 153, 154, 6, 0, 1 },
1232 FIELD1("FLMcongMode:", 152),
1233 FIELD("MaxuPFLCredits:", 144, 151),
1234 FIELD("FLMcontextID:", 133, 143),
1235 FIELD1("uPTokenEn:", 132),
1236 FIELD1("UserModeIO:", 131),
1237 FIELD("uPFLCredits:", 123, 130),
1238 FIELD1("uPFLCreditEn:", 122),
1239 FIELD("FID:", 111, 121),
1240 FIELD("HostFCMode:", 109, 110),
1241 FIELD1("HostFCOwner:", 108),
1242 { "CIDXFlushThresh:", 105, 107, 0, 0, 1 },
1243 FIELD("CIDX:", 89, 104),
1244 FIELD("PIDX:", 73, 88),
1245 { "BaseAddress:", 18, 72, 9, 1 },
1246 FIELD("QueueSize:", 2, 17),
1247 FIELD1("QueueType:", 1),
1248 FIELD1("CachePriority:", 0),
1251 static struct field_desc ingress[] = {
1252 FIELD1("NoSnoop:", 145),
1253 FIELD1("RelaxedOrdering:", 144),
1254 FIELD1("GTSmode:", 143),
1255 FIELD1("ISCSICoalescing:", 142),
1256 FIELD1("Valid:", 141),
1257 FIELD1("TimerPending:", 140),
1258 FIELD1("DropRSS:", 139),
1259 FIELD("PCIeChannel:", 137, 138),
1260 FIELD1("SEInterruptArmed:", 136),
1261 FIELD1("CongestionMgtEnable:", 135),
1262 FIELD1("DCAIngQEnable:", 134),
1263 FIELD("DCACPUID:", 129, 133),
1264 FIELD1("UpdateScheduling:", 128),
1265 FIELD("UpdateDelivery:", 126, 127),
1266 FIELD1("InterruptSent:", 125),
1267 FIELD("InterruptIDX:", 114, 124),
1268 FIELD1("InterruptDestination:", 113),
1269 FIELD1("InterruptArmed:", 112),
1270 FIELD("RxIntCounter:", 106, 111),
1271 FIELD("RxIntCounterThreshold:", 104, 105),
1272 FIELD1("Generation:", 103),
1273 { "BaseAddress:", 48, 102, 9, 1 },
1274 FIELD("PIDX:", 32, 47),
1275 FIELD("CIDX:", 16, 31),
1276 { "QueueSize:", 4, 15, 4, 0 },
1277 { "QueueEntrySize:", 2, 3, 4, 0, 1 },
1278 FIELD1("QueueEntryOverride:", 1),
1279 FIELD1("CachePriority:", 0),
1282 static struct field_desc flm[] = {
1283 FIELD1("NoSnoop:", 79),
1284 FIELD1("RelaxedOrdering:", 78),
1285 FIELD1("Valid:", 77),
1286 FIELD("DCACPUID:", 72, 76),
1287 FIELD1("DCAFLEn:", 71),
1288 FIELD("EQid:", 54, 70),
1289 FIELD("SplitEn:", 52, 53),
1290 FIELD1("PadEn:", 51),
1291 FIELD1("PackEn:", 50),
1292 FIELD1("DBpriority:", 48),
1293 FIELD("PackOffset:", 16, 47),
1294 FIELD("CIDX:", 8, 15),
1295 FIELD("PIDX:", 0, 7),
1298 static struct field_desc conm[] = {
1299 FIELD1("CngDBPHdr:", 6),
1300 FIELD1("CngDBPData:", 5),
1301 FIELD1("CngIMSG:", 4),
1302 FIELD("CngChMap:", 0, 3),
1306 if (p->mem_id == SGE_CONTEXT_EGRESS)
1307 show_struct(p->data, 6, (p->data[0] & 2) ? fl : egress);
1308 else if (p->mem_id == SGE_CONTEXT_FLM)
1309 show_struct(p->data, 3, flm);
1310 else if (p->mem_id == SGE_CONTEXT_INGRESS)
1311 show_struct(p->data, 5, ingress);
1312 else if (p->mem_id == SGE_CONTEXT_CNM)
1313 show_struct(p->data, 1, conm);
1320 get_sge_context(int argc, const char *argv[])
1325 struct t4_sge_context cntxt = {0};
1328 warnx("sge_context: incorrect number of arguments.");
1332 if (!strcmp(argv[0], "egress"))
1333 cntxt.mem_id = SGE_CONTEXT_EGRESS;
1334 else if (!strcmp(argv[0], "ingress"))
1335 cntxt.mem_id = SGE_CONTEXT_INGRESS;
1336 else if (!strcmp(argv[0], "fl"))
1337 cntxt.mem_id = SGE_CONTEXT_FLM;
1338 else if (!strcmp(argv[0], "cong"))
1339 cntxt.mem_id = SGE_CONTEXT_CNM;
1341 warnx("unknown context type \"%s\"; known types are egress, "
1342 "ingress, fl, and cong.", argv[0]);
1346 p = str_to_number(argv[1], &cid, NULL);
1348 warnx("invalid context id \"%s\"", argv[1]);
1353 rc = doit(CHELSIO_T4_GET_SGE_CONTEXT, &cntxt);
1357 show_sge_context(&cntxt);
1362 loadfw(int argc, const char *argv[])
1365 struct t4_data data = {0};
1366 const char *fname = argv[0];
1367 struct stat st = {0};
1370 warnx("loadfw: incorrect number of arguments.");
1374 fd = open(fname, O_RDONLY);
1376 warn("open(%s)", fname);
1380 if (fstat(fd, &st) < 0) {
1386 data.len = st.st_size;
1387 data.data = mmap(0, data.len, PROT_READ, 0, fd, 0);
1388 if (data.data == MAP_FAILED) {
1394 rc = doit(CHELSIO_T4_LOAD_FW, &data);
1395 munmap(data.data, data.len);
1401 read_mem(uint32_t addr, uint32_t len, void (*output)(uint32_t *, uint32_t))
1404 struct t4_mem_range mr;
1408 mr.data = malloc(mr.len);
1411 warn("read_mem: malloc");
1415 rc = doit(CHELSIO_T4_GET_MEM, &mr);
1420 (*output)(mr.data, mr.len);
1427 * Display memory as list of 'n' 4-byte values per line.
1430 show_mem(uint32_t *buf, uint32_t len)
1436 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1438 printf("%s%08x", s, htonl(*buf));
1445 memdump(int argc, const char *argv[])
1452 warnx("incorrect number of arguments.");
1456 p = str_to_number(argv[0], &l, NULL);
1458 warnx("invalid address \"%s\"", argv[0]);
1463 p = str_to_number(argv[1], &l, NULL);
1465 warnx("memdump: invalid length \"%s\"", argv[1]);
1470 return (read_mem(addr, len, show_mem));
1474 * Display TCB as list of 'n' 4-byte values per line.
1477 show_tcb(uint32_t *buf, uint32_t len)
1483 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1485 printf("%s%08x", s, htonl(*buf));
1491 #define A_TP_CMM_TCB_BASE 0x7d10
1492 #define TCB_SIZE 128
1494 read_tcb(int argc, const char *argv[])
1504 warnx("incorrect number of arguments.");
1508 p = str_to_number(argv[0], &l, NULL);
1510 warnx("invalid tid \"%s\"", argv[0]);
1515 rc = read_reg(A_TP_CMM_TCB_BASE, 4, &val);
1519 addr = val + tid * TCB_SIZE;
1521 return (read_mem(addr, TCB_SIZE, show_tcb));
1525 run_cmd(int argc, const char *argv[])
1528 const char *cmd = argv[0];
1534 if (!strcmp(cmd, "reg") || !strcmp(cmd, "reg32"))
1535 rc = register_io(argc, argv, 4);
1536 else if (!strcmp(cmd, "reg64"))
1537 rc = register_io(argc, argv, 8);
1538 else if (!strcmp(cmd, "regdump"))
1539 rc = dump_regs(argc, argv);
1540 else if (!strcmp(cmd, "filter"))
1541 rc = filter_cmd(argc, argv);
1542 else if (!strcmp(cmd, "context"))
1543 rc = get_sge_context(argc, argv);
1544 else if (!strcmp(cmd, "loadfw"))
1545 rc = loadfw(argc, argv);
1546 else if (!strcmp(cmd, "memdump"))
1547 rc = memdump(argc, argv);
1548 else if (!strcmp(cmd, "tcb"))
1549 rc = read_tcb(argc, argv);
1552 warnx("invalid command \"%s\"", cmd);
1563 char buffer[128], *buf;
1564 const char *args[MAX_ARGS + 1];
1567 * Simple loop: displays a "> " prompt and processes any input as a
1568 * cxgbetool command. You're supposed to enter only the part after
1569 * "cxgbetool t4nexX". Use "quit" or "exit" to exit.
1572 fprintf(stdout, "> ");
1574 buf = fgets(buffer, sizeof(buffer), stdin);
1576 if (ferror(stdin)) {
1577 warn("stdin error");
1578 rc = errno; /* errno from fgets */
1584 while ((args[i] = strsep(&buf, " \t\n")) != NULL) {
1585 if (args[i][0] != 0 && ++i == MAX_ARGS)
1591 continue; /* skip empty line */
1593 if (!strcmp(args[0], "quit") || !strcmp(args[0], "exit"))
1596 rc = run_cmd(i, args);
1599 /* rc normally comes from the last command (not including quit/exit) */
1604 main(int argc, const char *argv[])
1611 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1624 /* progname and nexus */
1628 if (argc == 1 && !strcmp(argv[0], "stdio"))
1629 rc = run_cmd_loop();
1631 rc = run_cmd(argc, argv);