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 static const char *progname, *nexus;
62 const struct reg_info *ri;
65 #include "reg_defs_t4.c"
66 #include "reg_defs_t4vf.c"
71 fprintf(fp, "Usage: %s <nexus> [operation]\n", progname);
73 "\tfilter <idx> [<param> <val>] ... set a filter\n"
74 "\tfilter <idx> delete|clear delete a filter\n"
75 "\tfilter list list all filters\n"
76 "\tfilter mode [<match>] ... get/set global filter mode\n"
77 "\tloadfw <fw-image.bin> install firmware\n"
78 "\tmemdump <addr> <len> dump a memory range\n"
79 "\treg <address>[=<val>] read/write register\n"
80 "\treg64 <address>[=<val>] read/write 64 bit register\n"
81 "\tregdump [<module>] ... dump registers\n"
82 "\tstdio interactive mode\n"
83 "\ttcb <tid> read TCB\n"
87 static inline unsigned int
88 get_card_vers(unsigned int version)
90 return (version & 0x3ff);
94 real_doit(unsigned long cmd, void *data, const char *cmdstr)
102 snprintf(buf, sizeof(buf), "/dev/%s", nexus);
103 if ((fd = open(buf, O_RDWR)) < 0) {
104 warn("open(%s)", nexus);
110 rc = ioctl(fd, cmd, data);
118 #define doit(x, y) real_doit(x, y, #x)
121 str_to_number(const char *s, long *val, long long *vall)
126 *vall = strtoll(s, &p, 0);
128 *val = strtol(s, &p, 0);
136 read_reg(long addr, int size, long long *val)
141 reg.addr = (uint32_t) addr;
142 reg.size = (uint32_t) size;
145 rc = doit(CHELSIO_T4_GETREG, ®);
153 write_reg(long addr, int size, long long val)
157 reg.addr = (uint32_t) addr;
158 reg.size = (uint32_t) size;
159 reg.val = (uint64_t) val;
161 return doit(CHELSIO_T4_SETREG, ®);
165 register_io(int argc, const char *argv[], int size)
173 /* <reg> OR <reg>=<value> */
175 p = str_to_number(argv[0], &addr, NULL);
178 warnx("invalid register \"%s\"", argv[0]);
184 p = str_to_number(v, NULL, &val);
187 warnx("invalid value \"%s\"", v);
192 } else if (argc == 2) {
197 p = str_to_number(argv[0], &addr, NULL);
199 warnx("invalid register \"%s\"", argv[0]);
203 p = str_to_number(argv[1], NULL, &val);
205 warnx("invalid value \"%s\"", argv[1]);
209 warnx("reg: invalid number of arguments (%d)", argc);
214 rc = write_reg(addr, size, val);
216 rc = read_reg(addr, size, &val);
218 printf("0x%llx [%llu]\n", val, val);
224 static inline uint32_t
225 xtract(uint32_t val, int shift, int len)
227 return (val >> shift) & ((1 << len) - 1);
231 dump_block_regs(const struct reg_info *reg_array, const uint32_t *regs)
233 uint32_t reg_val = 0;
235 for ( ; reg_array->name; ++reg_array)
236 if (!reg_array->len) {
237 reg_val = regs[reg_array->addr / 4];
238 printf("[%#7x] %-47s %#-10x %u\n", reg_array->addr,
239 reg_array->name, reg_val, reg_val);
241 uint32_t v = xtract(reg_val, reg_array->addr,
244 printf(" %*u:%u %-47s %#-10x %u\n",
245 reg_array->addr < 10 ? 3 : 2,
246 reg_array->addr + reg_array->len - 1,
247 reg_array->addr, reg_array->name, v, v);
254 dump_regs_table(int argc, const char *argv[], const uint32_t *regs,
255 const struct mod_regs *modtab, int nmodules)
259 for (i = 0; i < argc; i++) {
260 for (j = 0; j < nmodules; j++) {
261 if (!strcmp(argv[i], modtab[j].name))
266 warnx("invalid register block \"%s\"", argv[i]);
267 fprintf(stderr, "\nAvailable blocks:");
268 for ( ; nmodules; nmodules--, modtab++)
269 fprintf(stderr, " %s", modtab->name);
270 fprintf(stderr, "\n");
275 for ( ; nmodules; nmodules--, modtab++) {
277 match = argc == 0 ? 1 : 0;
278 for (i = 0; !match && i < argc; i++) {
279 if (!strcmp(argv[i], modtab->name))
284 dump_block_regs(modtab->ri, regs);
290 #define T4_MODREGS(name) { #name, t4_##name##_regs }
292 dump_regs_t4(int argc, const char *argv[], const uint32_t *regs)
294 static struct mod_regs t4_mod[] = {
296 { "pci", t4_pcie_regs },
300 { "edc0", t4_edc_0_regs },
301 { "edc1", t4_edc_1_regs },
306 { "pmrx", t4_pm_rx_regs },
307 { "pmtx", t4_pm_tx_regs },
309 { "cplsw", t4_cpl_switch_regs },
311 { "i2c", t4_i2cm_regs },
322 return dump_regs_table(argc, argv, regs, t4_mod, ARRAY_SIZE(t4_mod));
327 dump_regs_t4vf(int argc, const char *argv[], const uint32_t *regs)
329 static struct mod_regs t4vf_mod[] = {
330 { "sge", t4vf_sge_regs },
331 { "mps", t4vf_mps_regs },
332 { "pl", t4vf_pl_regs },
333 { "mbdata", t4vf_mbdata_regs },
334 { "cim", t4vf_cim_regs },
337 return dump_regs_table(argc, argv, regs, t4vf_mod,
338 ARRAY_SIZE(t4vf_mod));
342 dump_regs(int argc, const char *argv[])
344 int vers, revision, is_pcie, rc;
345 struct t4_regdump regs;
347 regs.data = calloc(1, T4_REGDUMP_SIZE);
348 if (regs.data == NULL) {
349 warnc(ENOMEM, "regdump");
353 regs.len = T4_REGDUMP_SIZE;
354 rc = doit(CHELSIO_T4_REGDUMP, ®s);
358 vers = get_card_vers(regs.version);
359 revision = (regs.version >> 10) & 0x3f;
360 is_pcie = (regs.version & 0x80000000) != 0;
363 if (revision == 0x3f)
364 rc = dump_regs_t4vf(argc, argv, regs.data);
366 rc = dump_regs_t4(argc, argv, regs.data);
368 warnx("%s (type %d, rev %d) is not a T4 card.",
369 nexus, vers, revision);
378 do_show_info_header(uint32_t mode)
382 printf ("%4s %8s", "Idx", "Hits");
383 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
394 printf (" vld:VNIC");
398 printf (" vld:VLAN");
401 case T4_FILTER_IP_TOS:
405 case T4_FILTER_IP_PROTO:
409 case T4_FILTER_ETH_TYPE:
413 case T4_FILTER_MAC_IDX:
417 case T4_FILTER_MPS_HIT_TYPE:
421 case T4_FILTER_IP_FRAGMENT:
426 /* compressed filter field not enabled */
430 printf(" %20s %20s %9s %9s %s\n",
431 "DIP", "SIP", "DPORT", "SPORT", "Action");
435 * Parse an argument sub-vector as a { <parameter name> <value>[:<mask>] }
436 * ordered tuple. If the parameter name in the argument sub-vector does not
437 * match the passed in parameter name, then a zero is returned for the
438 * function and no parsing is performed. If there is a match, then the value
439 * and optional mask are parsed and returned in the provided return value
440 * pointers. If no optional mask is specified, then a default mask of all 1s
443 * An error in parsing the value[:mask] will result in an error message and
444 * program termination.
447 parse_val_mask(const char *param, const char *args[], uint32_t *val,
452 if (strcmp(param, args[0]) != 0)
455 *val = strtoul(args[1], &p, 0);
462 if (p[0] == ':' && p[1] != 0) {
463 *mask = strtoul(p+1, &p, 0);
469 warnx("parameter \"%s\" has bad \"value[:mask]\" %s",
476 * Parse an argument sub-vector as a { <parameter name> <addr>[/<mask>] }
477 * ordered tuple. If the parameter name in the argument sub-vector does not
478 * match the passed in parameter name, then a zero is returned for the
479 * function and no parsing is performed. If there is a match, then the value
480 * and optional mask are parsed and returned in the provided return value
481 * pointers. If no optional mask is specified, then a default mask of all 1s
484 * The value return parameter "afp" is used to specify the expected address
485 * family -- IPv4 or IPv6 -- of the address[/mask] and return its actual
486 * format. A passed in value of AF_UNSPEC indicates that either IPv4 or IPv6
487 * is acceptable; AF_INET means that only IPv4 addresses are acceptable; and
488 * AF_INET6 means that only IPv6 are acceptable. AF_INET is returned for IPv4
489 * and AF_INET6 for IPv6 addresses, respectively. IPv4 address/mask pairs are
490 * returned in the first four bytes of the address and mask return values with
491 * the address A.B.C.D returned with { A, B, C, D } returned in addresses { 0,
492 * 1, 2, 3}, respectively.
494 * An error in parsing the value[:mask] will result in an error message and
495 * program termination.
498 parse_ipaddr(const char *param, const char *args[], int *afp, uint8_t addr[],
501 const char *colon, *afn;
505 unsigned int masksize;
508 * Is this our parameter?
510 if (strcmp(param, args[0]) != 0)
514 * Fundamental IPv4 versus IPv6 selection.
516 colon = strchr(args[1], ':');
526 if (*afp == AF_UNSPEC)
528 else if (*afp != af) {
529 warnx("address %s is not of expected family %s",
530 args[1], *afp == AF_INET ? "IP" : "IPv6");
535 * Parse address (temporarily stripping off any "/mask"
538 slash = strchr(args[1], '/');
541 ret = inet_pton(af, args[1], addr);
545 warnx("Cannot parse %s %s address %s", param, afn, args[1]);
550 * Parse optional mask specification.
554 unsigned int prefix = strtoul(slash + 1, &p, 10);
556 if (p == slash + 1) {
557 warnx("missing address prefix for %s", param);
561 warnx("%s is not a valid address prefix", slash + 1);
564 if (prefix > masksize) {
565 warnx("prefix %u is too long for an %s address",
569 memset(mask, 0, masksize / 8);
576 for (m = mask; masksize >= 8; m++, masksize -= 8)
579 *m = ~0 << (8 - masksize);
585 * Parse an argument sub-vector as a { <parameter name> <value> } ordered
586 * tuple. If the parameter name in the argument sub-vector does not match the
587 * passed in parameter name, then a zero is returned for the function and no
588 * parsing is performed. If there is a match, then the value is parsed and
589 * returned in the provided return value pointer.
592 parse_val(const char *param, const char *args[], uint32_t *val)
596 if (strcmp(param, args[0]) != 0)
599 *val = strtoul(args[1], &p, 0);
600 if (p > args[1] && p[0] == 0)
603 warnx("parameter \"%s\" has bad \"value\" %s", args[0], args[1]);
608 filters_show_ipaddr(int type, uint8_t *addr, uint8_t *addrm)
619 for (octet = 0; octet < noctets; octet++)
620 printf("%02x", addr[octet]);
622 for (octet = 0; octet < noctets; octet++)
623 printf("%02x", addrm[octet]);
627 do_show_one_filter_info(struct t4_filter *t, uint32_t mode)
631 printf("%4d", t->idx);
632 if (t->hits == UINT64_MAX)
635 printf(" %8ju", t->hits);
638 * Compressed header portion of filter.
640 for (i = T4_FILTER_FCoE; i <= T4_FILTER_IP_FRAGMENT; i <<= 1) {
643 printf(" %1d/%1d", t->fs.val.fcoe, t->fs.mask.fcoe);
647 printf(" %1d/%1d", t->fs.val.iport, t->fs.mask.iport);
651 printf(" %1d:%1x:%02x/%1d:%1x:%02x",
652 t->fs.val.vnic_vld, (t->fs.val.vnic >> 7) & 0x7,
653 t->fs.val.vnic & 0x7f, t->fs.mask.vnic_vld,
654 (t->fs.mask.vnic >> 7) & 0x7,
655 t->fs.mask.vnic & 0x7f);
659 printf(" %1d:%04x/%1d:%04x",
660 t->fs.val.vlan_vld, t->fs.val.vlan,
661 t->fs.mask.vlan_vld, t->fs.mask.vlan);
664 case T4_FILTER_IP_TOS:
665 printf(" %02x/%02x", t->fs.val.tos, t->fs.mask.tos);
668 case T4_FILTER_IP_PROTO:
669 printf(" %02x/%02x", t->fs.val.proto, t->fs.mask.proto);
672 case T4_FILTER_ETH_TYPE:
673 printf(" %04x/%04x", t->fs.val.ethtype,
677 case T4_FILTER_MAC_IDX:
678 printf(" %03x/%03x", t->fs.val.macidx,
682 case T4_FILTER_MPS_HIT_TYPE:
683 printf(" %1x/%1x", t->fs.val.matchtype,
684 t->fs.mask.matchtype);
687 case T4_FILTER_IP_FRAGMENT:
688 printf(" %1d/%1d", t->fs.val.frag, t->fs.mask.frag);
692 /* compressed filter field not enabled */
698 * Fixed portion of filter.
700 filters_show_ipaddr(t->fs.type, t->fs.val.dip, t->fs.mask.dip);
701 filters_show_ipaddr(t->fs.type, t->fs.val.sip, t->fs.mask.sip);
702 printf(" %04x/%04x %04x/%04x",
703 t->fs.val.dport, t->fs.mask.dport,
704 t->fs.val.sport, t->fs.mask.sport);
707 * Variable length filter action.
709 if (t->fs.action == FILTER_DROP)
711 else if (t->fs.action == FILTER_SWITCH) {
712 printf(" Switch: port=%d", t->fs.eport);
715 ", dmac=%02x:%02x:%02x:%02x:%02x:%02x "
717 t->fs.dmac[0], t->fs.dmac[1],
718 t->fs.dmac[2], t->fs.dmac[3],
719 t->fs.dmac[4], t->fs.dmac[5],
723 ", smac=%02x:%02x:%02x:%02x:%02x:%02x "
725 t->fs.smac[0], t->fs.smac[1],
726 t->fs.smac[2], t->fs.smac[3],
727 t->fs.smac[4], t->fs.smac[5],
729 if (t->fs.newvlan == VLAN_REMOVE)
730 printf(", vlan=none");
731 else if (t->fs.newvlan == VLAN_INSERT)
732 printf(", vlan=insert(%x)", t->fs.vlan);
733 else if (t->fs.newvlan == VLAN_REWRITE)
734 printf(", vlan=rewrite(%x)", t->fs.vlan);
737 if (t->fs.dirsteer == 0) {
740 printf("(TCB=hash)");
742 printf("%d", t->fs.iq);
743 if (t->fs.dirsteerhash == 0)
759 uint32_t mode = 0, header = 0;
763 /* Get the global filter mode first */
764 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
769 for (t.idx = 0; ; t.idx++) {
770 rc = doit(CHELSIO_T4_GET_FILTER, &t);
771 if (rc != 0 || t.idx == 0xffffffff)
775 do_show_info_header(mode);
778 do_show_one_filter_info(&t, mode);
785 get_filter_mode(void)
790 rc = doit(CHELSIO_T4_GET_FILTER_MODE, &mode);
794 if (mode & T4_FILTER_IPv4)
797 if (mode & T4_FILTER_IPv6)
800 if (mode & T4_FILTER_IP_SADDR)
803 if (mode & T4_FILTER_IP_DADDR)
806 if (mode & T4_FILTER_IP_SPORT)
809 if (mode & T4_FILTER_IP_DPORT)
812 if (mode & T4_FILTER_MPS_HIT_TYPE)
813 printf("matchtype ");
815 if (mode & T4_FILTER_MAC_IDX)
818 if (mode & T4_FILTER_ETH_TYPE)
821 if (mode & T4_FILTER_IP_PROTO)
824 if (mode & T4_FILTER_IP_TOS)
827 if (mode & T4_FILTER_VLAN)
830 if (mode & T4_FILTER_VNIC)
833 if (mode & T4_FILTER_PORT)
836 if (mode & T4_FILTER_FCoE)
845 set_filter_mode(int argc, const char *argv[])
849 for (; argc; argc--, argv++) {
850 if (!strcmp(argv[0], "matchtype"))
851 mode |= T4_FILTER_MPS_HIT_TYPE;
853 if (!strcmp(argv[0], "macidx"))
854 mode |= T4_FILTER_MAC_IDX;
856 if (!strcmp(argv[0], "ethtype"))
857 mode |= T4_FILTER_ETH_TYPE;
859 if (!strcmp(argv[0], "proto"))
860 mode |= T4_FILTER_IP_PROTO;
862 if (!strcmp(argv[0], "tos"))
863 mode |= T4_FILTER_IP_TOS;
865 if (!strcmp(argv[0], "vlan"))
866 mode |= T4_FILTER_VLAN;
868 if (!strcmp(argv[0], "ovlan") ||
869 !strcmp(argv[0], "vnic"))
870 mode |= T4_FILTER_VNIC;
872 if (!strcmp(argv[0], "iport"))
873 mode |= T4_FILTER_PORT;
875 if (!strcmp(argv[0], "fcoe"))
876 mode |= T4_FILTER_FCoE;
879 return doit(CHELSIO_T4_SET_FILTER_MODE, &mode);
883 del_filter(uint32_t idx)
889 return doit(CHELSIO_T4_DEL_FILTER, &t);
893 set_filter(uint32_t idx, int argc, const char *argv[])
895 int af = AF_UNSPEC, start_arg = 0;
899 warnc(EINVAL, "%s", __func__);
902 bzero(&t, sizeof (t));
905 for (start_arg = 0; start_arg + 2 <= argc; start_arg += 2) {
906 const char **args = &argv[start_arg];
909 if (!strcmp(argv[start_arg], "type")) {
911 if (!strcasecmp(argv[start_arg + 1], "ipv4"))
913 else if (!strcasecmp(argv[start_arg + 1], "ipv6"))
916 warnx("invalid type \"%s\"; "
917 "must be one of \"ipv4\" or \"ipv6\"",
918 argv[start_arg + 1]);
922 if (af != AF_UNSPEC && af != newaf) {
923 warnx("conflicting IPv4/IPv6 specifications.");
927 } else if (!parse_val_mask("fcoe", args, &val, &mask)) {
929 t.fs.mask.fcoe = mask;
930 } else if (!parse_val_mask("iport", args, &val, &mask)) {
931 t.fs.val.iport = val;
932 t.fs.mask.iport = mask;
933 } else if (!parse_val_mask("ovlan", args, &val, &mask)) {
935 t.fs.mask.vnic = mask;
936 t.fs.val.vnic_vld = 1;
937 t.fs.mask.vnic_vld = 1;
938 } else if (!parse_val_mask("vnic", args, &val, &mask)) {
940 t.fs.mask.vnic = mask;
941 t.fs.val.vnic_vld = 1;
942 t.fs.mask.vnic_vld = 1;
943 } else if (!parse_val_mask("vlan", args, &val, &mask)) {
945 t.fs.mask.vlan = mask;
946 t.fs.val.vlan_vld = 1;
947 t.fs.mask.vlan_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 loadfw(int argc, const char *argv[])
1132 struct t4_data data = {0};
1133 const char *fname = argv[0];
1134 struct stat st = {0};
1137 warnx("loadfw: incorrect number of arguments.");
1141 fd = open(fname, O_RDONLY);
1143 warn("open(%s)", fname);
1147 if (fstat(fd, &st) < 0) {
1153 data.len = st.st_size;
1154 data.data = mmap(0, data.len, PROT_READ, 0, fd, 0);
1155 if (data.data == MAP_FAILED) {
1161 rc = doit(CHELSIO_T4_LOAD_FW, &data);
1162 munmap(data.data, data.len);
1168 read_mem(uint32_t addr, uint32_t len, void (*output)(uint32_t *, uint32_t))
1171 struct t4_mem_range mr;
1175 mr.data = malloc(mr.len);
1178 warn("read_mem: malloc");
1182 rc = doit(CHELSIO_T4_GET_MEM, &mr);
1187 (*output)(mr.data, mr.len);
1194 * Display memory as list of 'n' 4-byte values per line.
1197 show_mem(uint32_t *buf, uint32_t len)
1203 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1205 printf("%s%08x", s, htonl(*buf));
1212 memdump(int argc, const char *argv[])
1219 warnx("incorrect number of arguments.");
1223 p = str_to_number(argv[0], &l, NULL);
1225 warnx("invalid address \"%s\"", argv[0]);
1230 p = str_to_number(argv[1], &l, NULL);
1232 warnx("memdump: invalid length \"%s\"", argv[1]);
1237 return (read_mem(addr, len, show_mem));
1241 * Display TCB as list of 'n' 4-byte values per line.
1244 show_tcb(uint32_t *buf, uint32_t len)
1250 for (i = 0; len && i < n; i++, buf++, len -= 4) {
1252 printf("%s%08x", s, htonl(*buf));
1258 #define A_TP_CMM_TCB_BASE 0x7d10
1259 #define TCB_SIZE 128
1261 read_tcb(int argc, const char *argv[])
1271 warnx("incorrect number of arguments.");
1275 p = str_to_number(argv[0], &l, NULL);
1277 warnx("invalid tid \"%s\"", argv[0]);
1282 rc = read_reg(A_TP_CMM_TCB_BASE, 4, &val);
1286 addr = val + tid * TCB_SIZE;
1288 return (read_mem(addr, TCB_SIZE, show_tcb));
1292 run_cmd(int argc, const char *argv[])
1295 const char *cmd = argv[0];
1301 if (!strcmp(cmd, "reg") || !strcmp(cmd, "reg32"))
1302 rc = register_io(argc, argv, 4);
1303 else if (!strcmp(cmd, "reg64"))
1304 rc = register_io(argc, argv, 8);
1305 else if (!strcmp(cmd, "regdump"))
1306 rc = dump_regs(argc, argv);
1307 else if (!strcmp(cmd, "filter"))
1308 rc = filter_cmd(argc, argv);
1309 else if (!strcmp(cmd, "loadfw"))
1310 rc = loadfw(argc, argv);
1311 else if (!strcmp(cmd, "memdump"))
1312 rc = memdump(argc, argv);
1313 else if (!strcmp(cmd, "tcb"))
1314 rc = read_tcb(argc, argv);
1317 warnx("invalid command \"%s\"", cmd);
1328 char buffer[128], *buf;
1329 const char *args[MAX_ARGS + 1];
1332 * Simple loop: displays a "> " prompt and processes any input as a
1333 * cxgbetool command. You're supposed to enter only the part after
1334 * "cxgbetool t4nexX". Use "quit" or "exit" to exit.
1337 fprintf(stdout, "> ");
1339 buf = fgets(buffer, sizeof(buffer), stdin);
1341 if (ferror(stdin)) {
1342 warn("stdin error");
1343 rc = errno; /* errno from fgets */
1349 while ((args[i] = strsep(&buf, " \t\n")) != NULL) {
1350 if (args[i][0] != 0 && ++i == MAX_ARGS)
1356 continue; /* skip empty line */
1358 if (!strcmp(args[0], "quit") || !strcmp(args[0], "exit"))
1361 rc = run_cmd(i, args);
1364 /* rc normally comes from the last command (not including quit/exit) */
1369 main(int argc, const char *argv[])
1376 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
1389 /* progname and nexus */
1393 if (argc == 1 && !strcmp(argv[0], "stdio"))
1394 rc = run_cmd_loop();
1396 rc = run_cmd(argc, argv);