2 * Copyright (c) 2018, Mellanox Technologies, Ltd. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <dev/mlx5/mlx5io.h>
42 /* stolen from pciconf.c: parsesel() */
44 parse_pci_addr(const char *addrstr, struct mlx5_fwdump_addr *addr)
47 unsigned long selarr[4];
50 if (strncmp(addrstr, "pci", 3) == 0) {
53 while (isdigit(*addrstr) && i < 4) {
54 selarr[i++] = strtoul(addrstr, &eppos, 10);
59 if (i > 0 && *addrstr == '\0') {
60 addr->func = (i > 2) ? selarr[--i] : 0;
61 addr->slot = (i > 0) ? selarr[--i] : 0;
62 addr->bus = (i > 0) ? selarr[--i] : 0;
63 addr->domain = (i > 0) ? selarr[--i] : 0;
67 warnx("invalid pci address %s", addrstr);
72 mlx5tool_save_dump(int ctldev, const struct mlx5_fwdump_addr *addr,
75 struct mlx5_fwdump_get fdg;
76 struct mlx5_fwdump_reg *rege;
84 dump = fopen(dumpname, "w");
86 warn("open %s", dumpname);
90 memset(&fdg, 0, sizeof(fdg));
92 error = ioctl(ctldev, MLX5_FWDUMP_GET, &fdg);
94 warn("MLX5_FWDUMP_GET dumpsize");
97 rege = calloc(fdg.reg_filled, sizeof(*rege));
103 fdg.reg_cnt = fdg.reg_filled;
104 error = ioctl(ctldev, MLX5_FWDUMP_GET, &fdg);
107 warnx("no dump recorded");
109 warn("MLX5_FWDUMP_GET dump fetch");
112 for (cnt = 0; cnt < fdg.reg_cnt; cnt++, rege++)
113 fprintf(dump, "0x%08x\t0x%08x\n", rege->addr, rege->val);
122 mlx5tool_dump_reset(int ctldev, const struct mlx5_fwdump_addr *addr)
125 if (ioctl(ctldev, MLX5_FWDUMP_RESET, addr) == -1) {
126 warn("MLX5_FWDUMP_RESET");
133 mlx5tool_dump_force(int ctldev, const struct mlx5_fwdump_addr *addr)
136 if (ioctl(ctldev, MLX5_FWDUMP_FORCE, addr) == -1) {
137 warn("MLX5_FWDUMP_FORCE");
148 "Usage: mlx5tool -d pci<d:b:s:f> [-w -o dump.file | -r | -e]\n");
149 fprintf(stderr, "\t-w - write firmware dump to the specified file\n");
150 fprintf(stderr, "\t-r - reset dump\n");
151 fprintf(stderr, "\t-e - force dump\n");
163 main(int argc, char *argv[])
165 struct mlx5_fwdump_addr addr;
169 enum mlx5_action act;
174 while ((c = getopt(argc, argv, "d:eho:rw")) != -1) {
180 act = ACTION_DUMP_GET;
183 act= ACTION_DUMP_FORCE;
189 act = ACTION_DUMP_RESET;
196 if (act == ACTION_NONE || (dumpname != NULL && act != ACTION_DUMP_GET))
198 if (parse_pci_addr(addrstr, &addr) != 0)
201 ctldev = open(MLX5_DEV_PATH, O_RDWR);
203 err(1, "open "MLX5_DEV_PATH);
205 case ACTION_DUMP_GET:
206 res = mlx5tool_save_dump(ctldev, &addr, dumpname);
208 case ACTION_DUMP_RESET:
209 res = mlx5tool_dump_reset(ctldev, &addr);
211 case ACTION_DUMP_FORCE:
212 res = mlx5tool_dump_force(ctldev, &addr);