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$");
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <net/ethernet.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
47 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
49 #define max(x, y) ((x) > (y) ? (x) : (y))
51 static const char *progname, *nexus;
61 const struct reg_info *ri;
65 const char *name; /* Field name */
66 unsigned short start; /* Start bit position */
67 unsigned short end; /* End bit position */
68 unsigned char shift; /* # of low order bits omitted and implicitly 0 */
69 unsigned char hex; /* Print field in hex instead of decimal */
70 unsigned char islog2; /* Field contains the base-2 log of the value */
73 #include "reg_defs_t4.c"
74 #include "reg_defs_t4vf.c"
79 fprintf(fp, "Usage: %s <nexus> [operation]\n", progname);
81 "\tcontext <type> <id> show an SGE context\n"
82 "\tfilter <idx> [<param> <val>] ... set a filter\n"
83 "\tfilter <idx> delete|clear delete a filter\n"
84 "\tfilter list list all filters\n"
85 "\tfilter mode [<match>] ... get/set global filter mode\n"
86 "\treg <address>[=<val>] read/write register\n"
87 "\treg64 <address>[=<val>] read/write 64 bit register\n"
88 "\tregdump [<module>] ... dump registers\n"
89 "\tstdio interactive mode\n"
93 static inline unsigned int
94 get_card_vers(unsigned int version)
96 return (version & 0x3ff);
100 real_doit(unsigned long cmd, void *data, const char *cmdstr)
108 snprintf(buf, sizeof(buf), "/dev/%s", nexus);
109 if ((fd = open(buf, O_RDWR)) < 0) {
110 warn("open(%s)", nexus);
116 rc = ioctl(fd, cmd, data);
124 #define doit(x, y) real_doit(x, y, #x)
127 str_to_number(const char *s, long *val, long long *vall)
132 *vall = strtoll(s, &p, 0);
134 *val = strtol(s, &p, 0);
142 read_reg(long addr, int size, long long *val)
147 reg.addr = (uint32_t) addr;
148 reg.size = (uint32_t) size;
151 rc = doit(CHELSIO_T4_GETREG, ®);
159 write_reg(long addr, int size, long long val)
163 reg.addr = (uint32_t) addr;
164 reg.size = (uint32_t) size;
165 reg.val = (uint64_t) val;
167 return doit(CHELSIO_T4_SETREG, ®);
171 register_io(int argc, const char *argv[], int size)
179 /* <reg> OR <reg>=<value> */
181 p = str_to_number(argv[0], &addr, NULL);
184 warnx("invalid register \"%s\"", argv[0]);
190 p = str_to_number(v, NULL, &val);
193 warnx("invalid value \"%s\"", v);
198 } else if (argc == 2) {
203 p = str_to_number(argv[0], &addr, NULL);
205 warnx("invalid register \"%s\"", argv[0]);
209 p = str_to_number(argv[1], NULL, &val);
211 warnx("invalid value \"%s\"", argv[1]);
215 warnx("reg: invalid number of arguments (%d)", argc);
220 rc = write_reg(addr, size, val);
222 rc = read_reg(addr, size, &val);
224 printf("0x%llx [%llu]\n", val, val);
230 static inline uint32_t
231 xtract(uint32_t val, int shift, int len)
233 return (val >> shift) & ((1 << len) - 1);
237 dump_block_regs(const struct reg_info *reg_array, const uint32_t *regs)
239 uint32_t reg_val = 0;
241 for ( ; reg_array->name; ++reg_array)
242 if (!reg_array->len) {
243 reg_val = regs[reg_array->addr / 4];
244 printf("[%#7x] %-47s %#-10x %u\n", reg_array->addr,
245 reg_array->name, reg_val, reg_val);
247 uint32_t v = xtract(reg_val, reg_array->addr,
250 printf(" %*u:%u %-47s %#-10x %u\n",
251 reg_array->addr < 10 ? 3 : 2,
252 reg_array->addr + reg_array->len - 1,
253 reg_array->addr, reg_array->name, v, v);
260 dump_regs_table(int argc, const char *argv[], const uint32_t *regs,
261 const struct mod_regs *modtab, int nmodules)
265 for (i = 0; i < argc; i++) {
266 for (j = 0; j < nmodules; j++) {
267 if (!strcmp(argv[i], modtab[j].name))
272 warnx("invalid register block \"%s\"", argv[i]);
273 fprintf(stderr, "\nAvailable blocks:");
274 for ( ; nmodules; nmodules--, modtab++)
275 fprintf(stderr, " %s", modtab->name);
276 fprintf(stderr, "\n");
281 for ( ; nmodules; nmodules--, modtab++) {
283 match = argc == 0 ? 1 : 0;
284 for (i = 0; !match && i < argc; i++) {
285 if (!strcmp(argv[i], modtab->name))
290 dump_block_regs(modtab->ri, regs);
296 #define T4_MODREGS(name) { #name, t4_##name##_regs }
298 dump_regs_t4(int argc, const char *argv[], const uint32_t *regs)
300 static struct mod_regs t4_mod[] = {
302 { "pci", t4_pcie_regs },
306 { "edc0", t4_edc_0_regs },
307 { "edc1", t4_edc_1_regs },
312 { "pmrx", t4_pm_rx_regs },
313 { "pmtx", t4_pm_tx_regs },
315 { "cplsw", t4_cpl_switch_regs },
317 { "i2c", t4_i2cm_regs },
328 return dump_regs_table(argc, argv, regs, t4_mod, ARRAY_SIZE(t4_mod));
333 dump_regs_t4vf(int argc, const char *argv[], const uint32_t *regs)
335 static struct mod_regs t4vf_mod[] = {
336 { "sge", t4vf_sge_regs },
337 { "mps", t4vf_mps_regs },
338 { "pl", t4vf_pl_regs },
339 { "mbdata", t4vf_mbdata_regs },
340 { "cim", t4vf_cim_regs },
343 return dump_regs_table(argc, argv, regs, t4vf_mod,
344 ARRAY_SIZE(t4vf_mod));
348 dump_regs(int argc, const char *argv[])
350 int vers, revision, is_pcie, rc;
351 struct t4_regdump regs;
353 regs.data = calloc(1, T4_REGDUMP_SIZE);
354 if (regs.data == NULL) {
355 warnc(ENOMEM, "regdump");
359 regs.len = T4_REGDUMP_SIZE;
360 rc = doit(CHELSIO_T4_REGDUMP, ®s);
364 vers = get_card_vers(regs.version);
365 revision = (regs.version >> 10) & 0x3f;
366 is_pcie = (regs.version & 0x80000000) != 0;
369 if (revision == 0x3f)
370 rc = dump_regs_t4vf(argc, argv, regs.data);
372 rc = dump_regs_t4(argc, argv, regs.data);
374 warnx("%s (type %d, rev %d) is not a T4 card.",
375 nexus, vers, revision);
384 do_show_info_header(uint32_t mode)
388 printf ("%4s %8s", "Idx", "Hits");
389 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
399 case T4_FILTER_OVLAN:
400 printf (" vld:oVLAN");
403 case T4_FILTER_IVLAN:
404 printf (" vld:iVLAN");
407 case T4_FILTER_IP_TOS:
411 case T4_FILTER_IP_PROTO:
415 case T4_FILTER_ETH_TYPE:
419 case T4_FILTER_MAC_IDX:
423 case T4_FILTER_MPS_HIT_TYPE:
427 case T4_FILTER_IP_FRAGMENT:
432 /* compressed filter field not enabled */
436 printf(" %20s %20s %9s %9s %s\n",
437 "DIP", "SIP", "DPORT", "SPORT", "Action");
441 * Parse an argument sub-vector as a { <parameter name> <value>[:<mask>] }
442 * ordered tuple. If the parameter name in the argument sub-vector does not
443 * match the passed in parameter name, then a zero is returned for the
444 * function and no parsing is performed. If there is a match, then the value
445 * and optional mask are parsed and returned in the provided return value
446 * pointers. If no optional mask is specified, then a default mask of all 1s
449 * An error in parsing the value[:mask] will result in an error message and
450 * program termination.
453 parse_val_mask(const char *param, const char *args[], uint32_t *val,
458 if (strcmp(param, args[0]) != 0)
461 *val = strtoul(args[1], &p, 0);
468 if (p[0] == ':' && p[1] != 0) {
469 *mask = strtoul(p+1, &p, 0);
475 warnx("parameter \"%s\" has bad \"value[:mask]\" %s",
482 * Parse an argument sub-vector as a { <parameter name> <addr>[/<mask>] }
483 * ordered tuple. If the parameter name in the argument sub-vector does not
484 * match the passed in parameter name, then a zero is returned for the
485 * function and no parsing is performed. If there is a match, then the value
486 * and optional mask are parsed and returned in the provided return value
487 * pointers. If no optional mask is specified, then a default mask of all 1s
490 * The value return parameter "afp" is used to specify the expected address
491 * family -- IPv4 or IPv6 -- of the address[/mask] and return its actual
492 * format. A passed in value of AF_UNSPEC indicates that either IPv4 or IPv6
493 * is acceptable; AF_INET means that only IPv4 addresses are acceptable; and
494 * AF_INET6 means that only IPv6 are acceptable. AF_INET is returned for IPv4
495 * and AF_INET6 for IPv6 addresses, respectively. IPv4 address/mask pairs are
496 * returned in the first four bytes of the address and mask return values with
497 * the address A.B.C.D returned with { A, B, C, D } returned in addresses { 0,
498 * 1, 2, 3}, respectively.
500 * An error in parsing the value[:mask] will result in an error message and
501 * program termination.
504 parse_ipaddr(const char *param, const char *args[], int *afp, uint8_t addr[],
507 const char *colon, *afn;
511 unsigned int masksize;
514 * Is this our parameter?
516 if (strcmp(param, args[0]) != 0)
520 * Fundamental IPv4 versus IPv6 selection.
522 colon = strchr(args[1], ':');
532 if (*afp == AF_UNSPEC)
534 else if (*afp != af) {
535 warnx("address %s is not of expected family %s",
536 args[1], *afp == AF_INET ? "IP" : "IPv6");
541 * Parse address (temporarily stripping off any "/mask"
544 slash = strchr(args[1], '/');
547 ret = inet_pton(af, args[1], addr);
551 warnx("Cannot parse %s %s address %s", param, afn, args[1]);
556 * Parse optional mask specification.
560 unsigned int prefix = strtoul(slash + 1, &p, 10);
562 if (p == slash + 1) {
563 warnx("missing address prefix for %s", param);
567 warnx("%s is not a valid address prefix", slash + 1);
570 if (prefix > masksize) {
571 warnx("prefix %u is too long for an %s address",
575 memset(mask, 0, masksize / 8);
582 for (m = mask; masksize >= 8; m++, masksize -= 8)
585 *m = ~0 << (8 - masksize);
591 * Parse an argument sub-vector as a { <parameter name> <value> } ordered
592 * tuple. If the parameter name in the argument sub-vector does not match the
593 * passed in parameter name, then a zero is returned for the function and no
594 * parsing is performed. If there is a match, then the value is parsed and
595 * returned in the provided return value pointer.
598 parse_val(const char *param, const char *args[], uint32_t *val)
602 if (strcmp(param, args[0]) != 0)
605 *val = strtoul(args[1], &p, 0);
606 if (p > args[1] && p[0] == 0)
609 warnx("parameter \"%s\" has bad \"value\" %s", args[0], args[1]);
614 filters_show_ipaddr(int type, uint8_t *addr, uint8_t *addrm)
625 for (octet = 0; octet < noctets; octet++)
626 printf("%02x", addr[octet]);
628 for (octet = 0; octet < noctets; octet++)
629 printf("%02x", addrm[octet]);
633 do_show_one_filter_info(struct t4_filter *t, uint32_t mode)
637 printf("%4d", t->idx);
638 if (t->hits == UINT64_MAX)
641 printf(" %8ju", t->hits);
644 * Compressed header portion of filter.
646 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
649 printf(" %1d/%1d", t->fs.val.fcoe, t->fs.mask.fcoe);
653 printf(" %1d/%1d", t->fs.val.iport, t->fs.mask.iport);
656 case T4_FILTER_OVLAN:
657 printf(" %1d:%1x:%02x/%1d:%1x:%02x",
658 t->fs.val.ovlan_vld, (t->fs.val.ovlan >> 7) & 0x7,
659 t->fs.val.ovlan & 0x7f, t->fs.mask.ovlan_vld,
660 (t->fs.mask.ovlan >> 7) & 0x7,
661 t->fs.mask.ovlan & 0x7f);
664 case T4_FILTER_IVLAN:
665 printf(" %1d:%04x/%1d:%04x",
666 t->fs.val.ivlan_vld, t->fs.val.ivlan,
667 t->fs.mask.ivlan_vld, t->fs.mask.ivlan);
670 case T4_FILTER_IP_TOS:
671 printf(" %02x/%02x", t->fs.val.tos, t->fs.mask.tos);
674 case T4_FILTER_IP_PROTO:
675 printf(" %02x/%02x", t->fs.val.proto, t->fs.mask.proto);
678 case T4_FILTER_ETH_TYPE:
679 printf(" %04x/%04x", t->fs.val.ethtype,
683 case T4_FILTER_MAC_IDX:
684 printf(" %03x/%03x", t->fs.val.macidx,
688 case T4_FILTER_MPS_HIT_TYPE:
689 printf(" %1x/%1x", t->fs.val.matchtype,
690 t->fs.mask.matchtype);
693 case T4_FILTER_IP_FRAGMENT:
694 printf(" %1d/%1d", t->fs.val.frag, t->fs.mask.frag);
698 /* compressed filter field not enabled */
704 * Fixed portion of filter.
706 filters_show_ipaddr(t->fs.type, t->fs.val.dip, t->fs.mask.dip);
707 filters_show_ipaddr(t->fs.type, t->fs.val.sip, t->fs.mask.sip);
708 printf(" %04x/%04x %04x/%04x",
709 t->fs.val.dport, t->fs.mask.dport,
710 t->fs.val.sport, t->fs.mask.sport);
713 * Variable length filter action.
715 if (t->fs.action == FILTER_DROP)
717 else if (t->fs.action == FILTER_SWITCH) {
718 printf(" Switch: port=%d", t->fs.eport);
721 ", dmac=%02x:%02x:%02x:%02x:%02x:%02x "
723 t->fs.dmac[0], t->fs.dmac[1],
724 t->fs.dmac[2], t->fs.dmac[3],
725 t->fs.dmac[4], t->fs.dmac[5],
729 ", smac=%02x:%02x:%02x:%02x:%02x:%02x "
731 t->fs.smac[0], t->fs.smac[1],
732 t->fs.smac[2], t->fs.smac[3],
733 t->fs.smac[4], t->fs.smac[5],
735 if (t->fs.newvlan == VLAN_REMOVE)
736 printf(", vlan=none");
737 else if (t->fs.newvlan == VLAN_INSERT)
738 printf(", vlan=insert(%x)", t->fs.vlan);
739 else if (t->fs.newvlan == VLAN_REWRITE)
740 printf(", vlan=rewrite(%x)", t->fs.vlan);
743 if (t->fs.dirsteer == 0) {
746 printf("(TCB=hash)");
748 printf("%d", t->fs.iq);
749 if (t->fs.dirsteerhash == 0)
765 uint32_t mode = 0, header = 0;
769 /* Get the global filter mode first */
770 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
775 for (t.idx = 0; ; t.idx++) {
776 rc = doit(CHELSIO_T4_GET_FILTER, &t);
777 if (rc != 0 || t.idx == 0xffffffff)
781 do_show_info_header(mode);
784 do_show_one_filter_info(&t, mode);
791 get_filter_mode(void)
796 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
800 if (mode & T4_FILTER_IPv4)
803 if (mode & T4_FILTER_IPv6)
806 if (mode & T4_FILTER_IP_SADDR)
809 if (mode & T4_FILTER_IP_DADDR)
812 if (mode & T4_FILTER_IP_SPORT)
815 if (mode & T4_FILTER_IP_DPORT)
818 if (mode & T4_FILTER_MPS_HIT_TYPE)
819 printf("matchtype ");
821 if (mode & T4_FILTER_MAC_IDX)
824 if (mode & T4_FILTER_ETH_TYPE)
827 if (mode & T4_FILTER_IP_PROTO)
830 if (mode & T4_FILTER_IP_TOS)
833 if (mode & T4_FILTER_IVLAN)
836 if (mode & T4_FILTER_OVLAN)
839 if (mode & T4_FILTER_PORT)
842 if (mode & T4_FILTER_FCoE)
851 set_filter_mode(int argc, const char *argv[])
855 for (; argc; argc--, argv++) {
856 if (!strcmp(argv[0], "matchtype"))
857 mode |= T4_FILTER_MPS_HIT_TYPE;
859 if (!strcmp(argv[0], "macidx"))
860 mode |= T4_FILTER_MAC_IDX;
862 if (!strcmp(argv[0], "ethtype"))
863 mode |= T4_FILTER_ETH_TYPE;
865 if (!strcmp(argv[0], "proto"))
866 mode |= T4_FILTER_IP_PROTO;
868 if (!strcmp(argv[0], "tos"))
869 mode |= T4_FILTER_IP_TOS;
871 if (!strcmp(argv[0], "ivlan"))
872 mode |= T4_FILTER_IVLAN;
874 if (!strcmp(argv[0], "ovlan"))
875 mode |= T4_FILTER_OVLAN;
877 if (!strcmp(argv[0], "iport"))
878 mode |= T4_FILTER_PORT;
880 if (!strcmp(argv[0], "fcoe"))
881 mode |= T4_FILTER_FCoE;
884 return doit(CHELSIO_T4_SET_FILTER_MODE, &mode);
888 del_filter(uint32_t idx)
894 return doit(CHELSIO_T4_DEL_FILTER, &t);
898 set_filter(uint32_t idx, int argc, const char *argv[])
900 int af = AF_UNSPEC, start_arg = 0;
904 warnc(EINVAL, "%s", __func__);
907 bzero(&t, sizeof (t));
910 for (start_arg = 0; start_arg + 2 <= argc; start_arg += 2) {
911 const char **args = &argv[start_arg];
914 if (!strcmp(argv[start_arg], "type")) {
916 if (!strcasecmp(argv[start_arg + 1], "ipv4"))
918 else if (!strcasecmp(argv[start_arg + 1], "ipv6"))
921 warnx("invalid type \"%s\"; "
922 "must be one of \"ipv4\" or \"ipv6\"",
923 argv[start_arg + 1]);
927 if (af != AF_UNSPEC && af != newaf) {
928 warnx("conflicting IPv4/IPv6 specifications.");
932 } else if (!parse_val_mask("fcoe", args, &val, &mask)) {
934 t.fs.mask.fcoe = mask;
935 } else if (!parse_val_mask("iport", args, &val, &mask)) {
936 t.fs.val.iport = val;
937 t.fs.mask.iport = mask;
938 } else if (!parse_val_mask("ovlan", args, &val, &mask)) {
939 t.fs.val.ovlan = val;
940 t.fs.mask.ovlan = mask;
941 t.fs.val.ovlan_vld = 1;
942 t.fs.mask.ovlan_vld = 1;
943 } else if (!parse_val_mask("ivlan", args, &val, &mask)) {
944 t.fs.val.ivlan = val;
945 t.fs.mask.ivlan = mask;
946 t.fs.val.ivlan_vld = 1;
947 t.fs.mask.ivlan_vld = 1;
948 } else if (!parse_val_mask("tos", args, &val, &mask)) {
950 t.fs.mask.tos = mask;
951 } else if (!parse_val_mask("proto", args, &val, &mask)) {
952 t.fs.val.proto = val;
953 t.fs.mask.proto = mask;
954 } else if (!parse_val_mask("ethtype", args, &val, &mask)) {
955 t.fs.val.ethtype = val;
956 t.fs.mask.ethtype = mask;
957 } else if (!parse_val_mask("macidx", args, &val, &mask)) {
958 t.fs.val.macidx = val;
959 t.fs.mask.macidx = mask;
960 } else if (!parse_val_mask("matchtype", args, &val, &mask)) {
961 t.fs.val.matchtype = val;
962 t.fs.mask.matchtype = mask;
963 } else if (!parse_val_mask("frag", args, &val, &mask)) {
965 t.fs.mask.frag = mask;
966 } else if (!parse_val_mask("dport", args, &val, &mask)) {
967 t.fs.val.dport = val;
968 t.fs.mask.dport = mask;
969 } else if (!parse_val_mask("sport", args, &val, &mask)) {
970 t.fs.val.sport = val;
971 t.fs.mask.sport = mask;
972 } else if (!parse_ipaddr("dip", args, &af, t.fs.val.dip,
975 } else if (!parse_ipaddr("sip", args, &af, t.fs.val.sip,
978 } else if (!strcmp(argv[start_arg], "action")) {
979 if (!strcmp(argv[start_arg + 1], "pass"))
980 t.fs.action = FILTER_PASS;
981 else if (!strcmp(argv[start_arg + 1], "drop"))
982 t.fs.action = FILTER_DROP;
983 else if (!strcmp(argv[start_arg + 1], "switch"))
984 t.fs.action = FILTER_SWITCH;
986 warnx("invalid action \"%s\"; must be one of"
987 " \"pass\", \"drop\" or \"switch\"",
988 argv[start_arg + 1]);
991 } else if (!parse_val("hitcnts", args, &val)) {
993 } else if (!parse_val("prio", args, &val)) {
995 } else if (!parse_val("rpttid", args, &val)) {
997 } else if (!parse_val("queue", args, &val)) {
1000 } else if (!parse_val("tcbhash", args, &val)) {
1002 t.fs.dirsteerhash = 1;
1003 } else if (!parse_val("eport", args, &val)) {
1005 } else if (!strcmp(argv[start_arg], "dmac")) {
1006 struct ether_addr *daddr;
1008 daddr = ether_aton(argv[start_arg + 1]);
1009 if (daddr == NULL) {
1010 warnx("invalid dmac address \"%s\"",
1011 argv[start_arg + 1]);
1014 memcpy(t.fs.dmac, daddr, ETHER_ADDR_LEN);
1016 } else if (!strcmp(argv[start_arg], "smac")) {
1017 struct ether_addr *saddr;
1019 saddr = ether_aton(argv[start_arg + 1]);
1020 if (saddr == NULL) {
1021 warnx("invalid smac address \"%s\"",
1022 argv[start_arg + 1]);
1025 memcpy(t.fs.smac, saddr, ETHER_ADDR_LEN);
1027 } else if (!strcmp(argv[start_arg], "vlan")) {
1029 if (!strcmp(argv[start_arg + 1], "none")) {
1030 t.fs.newvlan = VLAN_REMOVE;
1031 } else if (argv[start_arg + 1][0] == '=') {
1032 t.fs.newvlan = VLAN_REWRITE;
1033 } else if (argv[start_arg + 1][0] == '+') {
1034 t.fs.newvlan = VLAN_INSERT;
1036 warnx("unknown vlan parameter \"%s\"; must"
1037 " be one of \"none\", \"=<vlan>\" or"
1038 " \"+<vlan>\"", argv[start_arg + 1]);
1041 if (t.fs.newvlan == VLAN_REWRITE ||
1042 t.fs.newvlan == VLAN_INSERT) {
1043 t.fs.vlan = strtoul(argv[start_arg + 1] + 1,
1045 if (p == argv[start_arg + 1] + 1 || p[0] != 0) {
1046 warnx("invalid vlan \"%s\"",
1047 argv[start_arg + 1]);
1052 warnx("invalid parameter \"%s\"", argv[start_arg]);
1056 if (start_arg != argc) {
1057 warnx("no value for \"%s\"", argv[start_arg]);
1062 * Check basic sanity of option combinations.
1064 if (t.fs.action != FILTER_SWITCH &&
1065 (t.fs.eport || t.fs.newdmac || t.fs.newsmac || t.fs.newvlan)) {
1066 warnx("prio, port dmac, smac and vlan only make sense with"
1067 " \"action switch\"");
1070 if (t.fs.action != FILTER_PASS &&
1071 (t.fs.rpttid || t.fs.dirsteer || t.fs.maskhash)) {
1072 warnx("rpttid, queue and tcbhash don't make sense with"
1073 " action \"drop\" or \"switch\"");
1077 t.fs.type = (af == AF_INET6 ? 1 : 0); /* default IPv4 */
1078 return doit(CHELSIO_T4_SET_FILTER, &t);
1082 filter_cmd(int argc, const char *argv[])
1089 warnx("filter: no arguments.");
1094 if (strcmp(argv[0], "list") == 0) {
1096 warnx("trailing arguments after \"list\" ignored.");
1098 return show_filters();
1102 if (argc == 1 && strcmp(argv[0], "mode") == 0)
1103 return get_filter_mode();
1106 if (strcmp(argv[0], "mode") == 0)
1107 return set_filter_mode(argc - 1, argv + 1);
1110 s = str_to_number(argv[0], NULL, &val);
1111 if (*s || val > 0xffffffffU) {
1112 warnx("\"%s\" is neither an index nor a filter subcommand.",
1116 idx = (uint32_t) val;
1118 /* <idx> delete|clear */
1120 (strcmp(argv[1], "delete") == 0 || strcmp(argv[1], "clear") == 0)) {
1121 return del_filter(idx);
1124 /* <idx> [<param> <val>] ... */
1125 return set_filter(idx, argc - 1, argv + 1);
1129 * Shows the fields of a multi-word structure. The structure is considered to
1130 * consist of @nwords 32-bit words (i.e, it's an (@nwords * 32)-bit structure)
1131 * whose fields are described by @fd. The 32-bit words are given in @words
1132 * starting with the least significant 32-bit word.
1135 show_struct(const uint32_t *words, int nwords, const struct field_desc *fd)
1138 const struct field_desc *p;
1140 for (p = fd; p->name; p++)
1141 w = max(w, strlen(p->name));
1144 unsigned long long data;
1145 int first_word = fd->start / 32;
1146 int shift = fd->start % 32;
1147 int width = fd->end - fd->start + 1;
1148 unsigned long long mask = (1ULL << width) - 1;
1150 data = (words[first_word] >> shift) |
1151 ((uint64_t)words[first_word + 1] << (32 - shift));
1153 data |= ((uint64_t)words[first_word + 2] << (64 - shift));
1157 printf("%-*s ", w, fd->name);
1158 printf(fd->hex ? "%#llx\n" : "%llu\n", data << fd->shift);
1163 #define FIELD(name, start, end) { name, start, end, 0, 0, 0 }
1164 #define FIELD1(name, start) FIELD(name, start, start)
1167 show_sge_context(const struct t4_sge_context *p)
1169 static struct field_desc egress[] = {
1170 FIELD1("StatusPgNS:", 180),
1171 FIELD1("StatusPgRO:", 179),
1172 FIELD1("FetchNS:", 178),
1173 FIELD1("FetchRO:", 177),
1174 FIELD1("Valid:", 176),
1175 FIELD("PCIeDataChannel:", 174, 175),
1176 FIELD1("DCAEgrQEn:", 173),
1177 FIELD("DCACPUID:", 168, 172),
1178 FIELD1("FCThreshOverride:", 167),
1179 FIELD("WRLength:", 162, 166),
1180 FIELD1("WRLengthKnown:", 161),
1181 FIELD1("ReschedulePending:", 160),
1182 FIELD1("OnChipQueue:", 159),
1183 FIELD1("FetchSizeMode", 158),
1184 { "FetchBurstMin:", 156, 157, 4, 0, 1 },
1185 { "FetchBurstMax:", 153, 154, 6, 0, 1 },
1186 FIELD("uPToken:", 133, 152),
1187 FIELD1("uPTokenEn:", 132),
1188 FIELD1("UserModeIO:", 131),
1189 FIELD("uPFLCredits:", 123, 130),
1190 FIELD1("uPFLCreditEn:", 122),
1191 FIELD("FID:", 111, 121),
1192 FIELD("HostFCMode:", 109, 110),
1193 FIELD1("HostFCOwner:", 108),
1194 { "CIDXFlushThresh:", 105, 107, 0, 0, 1 },
1195 FIELD("CIDX:", 89, 104),
1196 FIELD("PIDX:", 73, 88),
1197 { "BaseAddress:", 18, 72, 9, 1 },
1198 FIELD("QueueSize:", 2, 17),
1199 FIELD1("QueueType:", 1),
1200 FIELD1("CachePriority:", 0),
1203 static struct field_desc fl[] = {
1204 FIELD1("StatusPgNS:", 180),
1205 FIELD1("StatusPgRO:", 179),
1206 FIELD1("FetchNS:", 178),
1207 FIELD1("FetchRO:", 177),
1208 FIELD1("Valid:", 176),
1209 FIELD("PCIeDataChannel:", 174, 175),
1210 FIELD1("DCAEgrQEn:", 173),
1211 FIELD("DCACPUID:", 168, 172),
1212 FIELD1("FCThreshOverride:", 167),
1213 FIELD("WRLength:", 162, 166),
1214 FIELD1("WRLengthKnown:", 161),
1215 FIELD1("ReschedulePending:", 160),
1216 FIELD1("OnChipQueue:", 159),
1217 FIELD1("FetchSizeMode", 158),
1218 { "FetchBurstMin:", 156, 157, 4, 0, 1 },
1219 { "FetchBurstMax:", 153, 154, 6, 0, 1 },
1220 FIELD1("FLMcongMode:", 152),
1221 FIELD("MaxuPFLCredits:", 144, 151),
1222 FIELD("FLMcontextID:", 133, 143),
1223 FIELD1("uPTokenEn:", 132),
1224 FIELD1("UserModeIO:", 131),
1225 FIELD("uPFLCredits:", 123, 130),
1226 FIELD1("uPFLCreditEn:", 122),
1227 FIELD("FID:", 111, 121),
1228 FIELD("HostFCMode:", 109, 110),
1229 FIELD1("HostFCOwner:", 108),
1230 { "CIDXFlushThresh:", 105, 107, 0, 0, 1 },
1231 FIELD("CIDX:", 89, 104),
1232 FIELD("PIDX:", 73, 88),
1233 { "BaseAddress:", 18, 72, 9, 1 },
1234 FIELD("QueueSize:", 2, 17),
1235 FIELD1("QueueType:", 1),
1236 FIELD1("CachePriority:", 0),
1239 static struct field_desc ingress[] = {
1240 FIELD1("NoSnoop:", 145),
1241 FIELD1("RelaxedOrdering:", 144),
1242 FIELD1("GTSmode:", 143),
1243 FIELD1("ISCSICoalescing:", 142),
1244 FIELD1("Valid:", 141),
1245 FIELD1("TimerPending:", 140),
1246 FIELD1("DropRSS:", 139),
1247 FIELD("PCIeChannel:", 137, 138),
1248 FIELD1("SEInterruptArmed:", 136),
1249 FIELD1("CongestionMgtEnable:", 135),
1250 FIELD1("DCAIngQEnable:", 134),
1251 FIELD("DCACPUID:", 129, 133),
1252 FIELD1("UpdateScheduling:", 128),
1253 FIELD("UpdateDelivery:", 126, 127),
1254 FIELD1("InterruptSent:", 125),
1255 FIELD("InterruptIDX:", 114, 124),
1256 FIELD1("InterruptDestination:", 113),
1257 FIELD1("InterruptArmed:", 112),
1258 FIELD("RxIntCounter:", 106, 111),
1259 FIELD("RxIntCounterThreshold:", 104, 105),
1260 FIELD1("Generation:", 103),
1261 { "BaseAddress:", 48, 102, 9, 1 },
1262 FIELD("PIDX:", 32, 47),
1263 FIELD("CIDX:", 16, 31),
1264 { "QueueSize:", 4, 15, 4, 0 },
1265 { "QueueEntrySize:", 2, 3, 4, 0, 1 },
1266 FIELD1("QueueEntryOverride:", 1),
1267 FIELD1("CachePriority:", 0),
1270 static struct field_desc flm[] = {
1271 FIELD1("NoSnoop:", 79),
1272 FIELD1("RelaxedOrdering:", 78),
1273 FIELD1("Valid:", 77),
1274 FIELD("DCACPUID:", 72, 76),
1275 FIELD1("DCAFLEn:", 71),
1276 FIELD("EQid:", 54, 70),
1277 FIELD("SplitEn:", 52, 53),
1278 FIELD1("PadEn:", 51),
1279 FIELD1("PackEn:", 50),
1280 FIELD1("DBpriority:", 48),
1281 FIELD("PackOffset:", 16, 47),
1282 FIELD("CIDX:", 8, 15),
1283 FIELD("PIDX:", 0, 7),
1286 static struct field_desc conm[] = {
1287 FIELD1("CngDBPHdr:", 6),
1288 FIELD1("CngDBPData:", 5),
1289 FIELD1("CngIMSG:", 4),
1290 FIELD("CngChMap:", 0, 3),
1294 if (p->mem_id == SGE_CONTEXT_EGRESS)
1295 show_struct(p->data, 6, (p->data[0] & 2) ? fl : egress);
1296 else if (p->mem_id == SGE_CONTEXT_FLM)
1297 show_struct(p->data, 3, flm);
1298 else if (p->mem_id == SGE_CONTEXT_INGRESS)
1299 show_struct(p->data, 5, ingress);
1300 else if (p->mem_id == SGE_CONTEXT_CNM)
1301 show_struct(p->data, 1, conm);
1308 get_sge_context(int argc, const char *argv[])
1313 struct t4_sge_context cntxt = {0};
1316 warnx("sge_context: incorrect number of arguments.");
1320 if (!strcmp(argv[0], "egress"))
1321 cntxt.mem_id = SGE_CONTEXT_EGRESS;
1322 else if (!strcmp(argv[0], "ingress"))
1323 cntxt.mem_id = SGE_CONTEXT_INGRESS;
1324 else if (!strcmp(argv[0], "fl"))
1325 cntxt.mem_id = SGE_CONTEXT_FLM;
1326 else if (!strcmp(argv[0], "cong"))
1327 cntxt.mem_id = SGE_CONTEXT_CNM;
1329 warnx("unknown context type \"%s\"; known types are egress, "
1330 "ingress, fl, and cong.", argv[0]);
1334 p = str_to_number(argv[1], &cid, NULL);
1336 warnx("invalid context id \"%s\"", argv[1]);
1341 rc = doit(CHELSIO_T4_GET_SGE_CONTEXT, &cntxt);
1345 show_sge_context(&cntxt);
1350 run_cmd(int argc, const char *argv[])
1353 const char *cmd = argv[0];
1359 if (!strcmp(cmd, "reg") || !strcmp(cmd, "reg32"))
1360 rc = register_io(argc, argv, 4);
1361 else if (!strcmp(cmd, "reg64"))
1362 rc = register_io(argc, argv, 8);
1363 else if (!strcmp(cmd, "regdump"))
1364 rc = dump_regs(argc, argv);
1365 else if (!strcmp(cmd, "filter"))
1366 rc = filter_cmd(argc, argv);
1367 else if (!strcmp(cmd, "context"))
1368 rc = get_sge_context(argc, argv);
1371 warnx("invalid command \"%s\"", cmd);
1382 char buffer[128], *buf;
1383 const char *args[MAX_ARGS + 1];
1386 * Simple loop: displays a "> " prompt and processes any input as a
1387 * cxgbetool command. You're supposed to enter only the part after
1388 * "cxgbetool t4nexX". Use "quit" or "exit" to exit.
1391 fprintf(stdout, "> ");
1393 buf = fgets(buffer, sizeof(buffer), stdin);
1395 if (ferror(stdin)) {
1396 warn("stdin error");
1397 rc = errno; /* errno from fgets */
1403 while ((args[i] = strsep(&buf, " \t\n")) != NULL) {
1404 if (args[i][0] != 0 && ++i == MAX_ARGS)
1410 continue; /* skip empty line */
1412 if (!strcmp(args[0], "quit") || !strcmp(args[0], "exit"))
1415 rc = run_cmd(i, args);
1418 /* rc normally comes from the last command (not including quit/exit) */
1423 main(int argc, const char *argv[])
1430 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1443 /* progname and nexus */
1447 if (argc == 1 && !strcmp(argv[0], "stdio"))
1448 rc = run_cmd_loop();
1450 rc = run_cmd(argc, argv);