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>
33 #include <dev/mlx5/mlx5io.h>
44 /* stolen from pciconf.c: parsesel() */
46 parse_pci_addr(const char *addrstr, struct mlx5_tool_addr *addr)
49 unsigned long selarr[4];
52 if (addrstr == NULL) {
53 warnx("no pci address specified");
56 if (strncmp(addrstr, "pci", 3) == 0) {
59 while (isdigit(*addrstr) && i < 4) {
60 selarr[i++] = strtoul(addrstr, &eppos, 10);
65 if (i > 0 && *addrstr == '\0') {
66 addr->func = (i > 2) ? selarr[--i] : 0;
67 addr->slot = (i > 0) ? selarr[--i] : 0;
68 addr->bus = (i > 0) ? selarr[--i] : 0;
69 addr->domain = (i > 0) ? selarr[--i] : 0;
73 warnx("invalid pci address %s", addrstr);
78 mlx5tool_save_dump(int ctldev, const struct mlx5_tool_addr *addr,
81 struct mlx5_fwdump_get fdg;
82 struct mlx5_fwdump_reg *rege;
90 dump = fopen(dumpname, "w");
92 warn("open %s", dumpname);
96 memset(&fdg, 0, sizeof(fdg));
98 error = ioctl(ctldev, MLX5_FWDUMP_GET, &fdg);
100 warn("MLX5_FWDUMP_GET dumpsize");
103 rege = calloc(fdg.reg_filled, sizeof(*rege));
109 fdg.reg_cnt = fdg.reg_filled;
110 error = ioctl(ctldev, MLX5_FWDUMP_GET, &fdg);
113 warnx("no dump recorded");
115 warn("MLX5_FWDUMP_GET dump fetch");
118 for (cnt = 0; cnt < fdg.reg_cnt; cnt++, rege++)
119 fprintf(dump, "0x%08x\t0x%08x\n", rege->addr, rege->val);
128 mlx5tool_dump_reset(int ctldev, const struct mlx5_tool_addr *addr)
131 if (ioctl(ctldev, MLX5_FWDUMP_RESET, addr) == -1) {
132 warn("MLX5_FWDUMP_RESET");
139 mlx5tool_dump_force(int ctldev, const struct mlx5_tool_addr *addr)
142 if (ioctl(ctldev, MLX5_FWDUMP_FORCE, addr) == -1) {
143 warn("MLX5_FWDUMP_FORCE");
150 mlx5tool_fw_update(int ctldev, const struct mlx5_tool_addr *addr,
151 const char *img_fw_path)
154 struct mlx5_fw_update fwup;
158 fd = open(img_fw_path, O_RDONLY);
160 warn("Unable to open %s", img_fw_path);
164 error = fstat(fd, &st);
166 warn("Unable to stat %s", img_fw_path);
170 memset(&fwup, 0, sizeof(fwup));
171 memcpy(&fwup.devaddr, addr, sizeof(fwup.devaddr));
172 fwup.img_fw_data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE,
174 if (fwup.img_fw_data == MAP_FAILED) {
175 warn("Unable to mmap %s", img_fw_path);
179 fwup.img_fw_data_len = st.st_size;
181 error = ioctl(ctldev, MLX5_FW_UPDATE, &fwup);
183 warn("MLX5_FW_UPDATE");
186 munmap(fwup.img_fw_data, st.st_size);
193 mlx5tool_fw_reset(int ctldev, const struct mlx5_tool_addr *addr)
196 if (ioctl(ctldev, MLX5_FW_RESET, addr) == -1) {
197 warn("MLX5_FW_RESET");
208 "Usage: mlx5tool -d pci<d:b:s:f> [-w -o dump.file | -r |"
209 " -e | -f fw.mfa2 | -z]\n");
210 fprintf(stderr, "\t-w - write firmware dump to the specified file\n");
211 fprintf(stderr, "\t-r - reset dump\n");
212 fprintf(stderr, "\t-e - force dump\n");
213 fprintf(stderr, "\t-f fw.img - flash firmware from fw.img\n");
214 fprintf(stderr, "\t-z - initiate firmware reset\n");
228 main(int argc, char *argv[])
230 struct mlx5_tool_addr addr;
235 enum mlx5_action act;
241 while ((c = getopt(argc, argv, "d:ef:ho:rwz")) != -1) {
247 if (act != ACTION_NONE)
249 act = ACTION_DUMP_GET;
252 if (act != ACTION_NONE)
254 act = ACTION_DUMP_FORCE;
260 if (act != ACTION_NONE)
262 act = ACTION_DUMP_RESET;
265 if (act != ACTION_NONE)
267 act = ACTION_FW_UPDATE;
268 img_fw_path = optarg;
271 if (act != ACTION_NONE)
273 act = ACTION_FW_RESET;
280 if (act == ACTION_NONE || (dumpname != NULL &&
281 act != ACTION_DUMP_GET) || (img_fw_path != NULL &&
282 act != ACTION_FW_UPDATE))
284 if (parse_pci_addr(addrstr, &addr) != 0)
287 ctldev = open(MLX5_DEV_PATH, O_RDWR);
289 err(1, "open "MLX5_DEV_PATH);
291 case ACTION_DUMP_GET:
292 res = mlx5tool_save_dump(ctldev, &addr, dumpname);
294 case ACTION_DUMP_RESET:
295 res = mlx5tool_dump_reset(ctldev, &addr);
297 case ACTION_DUMP_FORCE:
298 res = mlx5tool_dump_force(ctldev, &addr);
300 case ACTION_FW_UPDATE:
301 res = mlx5tool_fw_update(ctldev, &addr, img_fw_path);
303 case ACTION_FW_RESET:
304 res = mlx5tool_fw_reset(ctldev, &addr);