mlx5tool.c revision 347841
1/*-
2 * Copyright (c) 2018, Mellanox Technologies, Ltd.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: stable/11/usr.sbin/mlx5tool/mlx5tool.c 347841 2019-05-16 17:50:52Z hselasky $");
28
29#include <sys/param.h>
30#include <sys/ioctl.h>
31#include <sys/mman.h>
32#include <sys/stat.h>
33#include <dev/mlx5/mlx5io.h>
34#include <ctype.h>
35#include <err.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <paths.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44/* stolen from pciconf.c: parsesel() */
45static int
46parse_pci_addr(const char *addrstr, struct mlx5_tool_addr *addr)
47{
48	char *eppos;
49	unsigned long selarr[4];
50	int i;
51
52	if (addrstr == NULL) {
53		warnx("no pci address specified");
54		return (1);
55	}
56	if (strncmp(addrstr, "pci", 3) == 0) {
57		addrstr += 3;
58		i = 0;
59		while (isdigit(*addrstr) && i < 4) {
60			selarr[i++] = strtoul(addrstr, &eppos, 10);
61			addrstr = eppos;
62			if (*addrstr == ':')
63				addrstr++;
64		}
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;
70			return (0);
71		}
72	}
73	warnx("invalid pci address %s", addrstr);
74	return (1);
75}
76
77static int
78mlx5tool_save_dump(int ctldev, const struct mlx5_tool_addr *addr,
79    const char *dumpname)
80{
81	struct mlx5_fwdump_get fdg;
82	struct mlx5_fwdump_reg *rege;
83	FILE *dump;
84	size_t cnt;
85	int error, res;
86
87	if (dumpname == NULL)
88		dump = stdout;
89	else
90		dump = fopen(dumpname, "w");
91	if (dump == NULL) {
92		warn("open %s", dumpname);
93		return (1);
94	}
95	res = 1;
96	memset(&fdg, 0, sizeof(fdg));
97	fdg.devaddr = *addr;
98	error = ioctl(ctldev, MLX5_FWDUMP_GET, &fdg);
99	if (error != 0) {
100		warn("MLX5_FWDUMP_GET dumpsize");
101		goto out;
102	}
103	rege = calloc(fdg.reg_filled, sizeof(*rege));
104	if (rege == NULL) {
105		warn("alloc rege");
106		goto out;
107	}
108	fdg.buf = rege;
109	fdg.reg_cnt = fdg.reg_filled;
110	error = ioctl(ctldev, MLX5_FWDUMP_GET, &fdg);
111	if (error != 0) {
112		if (errno == ENOENT)
113			warnx("no dump recorded");
114		else
115			warn("MLX5_FWDUMP_GET dump fetch");
116		goto out;
117	}
118	for (cnt = 0; cnt < fdg.reg_cnt; cnt++, rege++)
119		fprintf(dump, "0x%08x\t0x%08x\n", rege->addr, rege->val);
120	res = 0;
121out:
122	if (dump != stdout)
123		fclose(dump);
124	return (res);
125}
126
127static int
128mlx5tool_dump_reset(int ctldev, const struct mlx5_tool_addr *addr)
129{
130
131	if (ioctl(ctldev, MLX5_FWDUMP_RESET, addr) == -1) {
132		warn("MLX5_FWDUMP_RESET");
133		return (1);
134	}
135	return (0);
136}
137
138static int
139mlx5tool_dump_force(int ctldev, const struct mlx5_tool_addr *addr)
140{
141
142	if (ioctl(ctldev, MLX5_FWDUMP_FORCE, addr) == -1) {
143		warn("MLX5_FWDUMP_FORCE");
144		return (1);
145	}
146	return (0);
147}
148
149static int
150mlx5tool_fw_update(int ctldev, const struct mlx5_tool_addr *addr,
151    const char *img_fw_path)
152{
153	struct stat st;
154	struct mlx5_fw_update fwup;
155	int error, fd, res;
156
157	res = 0;
158	fd = open(img_fw_path, O_RDONLY);
159	if (fd == -1) {
160		warn("Unable to open %s", img_fw_path);
161		res = 1;
162		goto close_fd;
163	}
164	error = fstat(fd, &st);
165	if (error != 0) {
166		warn("Unable to stat %s", img_fw_path);
167		res = 1;
168		goto close_fd;
169	}
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,
173	    fd, 0);
174	if (fwup.img_fw_data == MAP_FAILED) {
175		warn("Unable to mmap %s", img_fw_path);
176		res = 1;
177		goto close_fd;
178	}
179	fwup.img_fw_data_len = st.st_size;
180
181	error = ioctl(ctldev, MLX5_FW_UPDATE, &fwup);
182	if (error == -1) {
183		warn("MLX5_FW_UPDATE");
184	}
185
186	munmap(fwup.img_fw_data, st.st_size);
187close_fd:
188	close(fd);
189	return (res);
190}
191
192static void
193usage(void)
194{
195
196	fprintf(stderr,
197	    "Usage: mlx5tool -d pci<d:b:s:f> [-w -o dump.file | -r |"
198	    "    -e | -f fw.mfa2]\n");
199	fprintf(stderr, "\t-w - write firmware dump to the specified file\n");
200	fprintf(stderr, "\t-r - reset dump\n");
201	fprintf(stderr, "\t-e - force dump\n");
202	fprintf(stderr, "\t-f fw.img - flash firmware from fw.img\n");
203	exit(1);
204}
205
206enum mlx5_action {
207	ACTION_DUMP_GET,
208	ACTION_DUMP_RESET,
209	ACTION_DUMP_FORCE,
210	ACTION_FW_UPDATE,
211	ACTION_NONE,
212};
213
214int
215main(int argc, char *argv[])
216{
217	struct mlx5_tool_addr addr;
218	char *dumpname;
219	char *addrstr;
220	char *img_fw_path;
221	int c, ctldev, res;
222	enum mlx5_action act;
223
224	act = ACTION_NONE;
225	addrstr = NULL;
226	dumpname = NULL;
227	img_fw_path = NULL;
228	while ((c = getopt(argc, argv, "d:ef:ho:rw")) != -1) {
229		switch (c) {
230		case 'd':
231			addrstr = optarg;
232			break;
233		case 'w':
234			act = ACTION_DUMP_GET;
235			break;
236		case 'e':
237			act = ACTION_DUMP_FORCE;
238			break;
239		case 'o':
240			dumpname = optarg;
241			break;
242		case 'r':
243			act = ACTION_DUMP_RESET;
244			break;
245		case 'f':
246			act = ACTION_FW_UPDATE;
247			img_fw_path = optarg;
248			break;
249		case 'h':
250		default:
251			usage();
252		}
253	}
254	if (act == ACTION_NONE || (dumpname != NULL &&
255	    act != ACTION_DUMP_GET) || (img_fw_path != NULL &&
256	    act != ACTION_FW_UPDATE))
257		usage();
258	if (parse_pci_addr(addrstr, &addr) != 0)
259		exit(1);
260
261	ctldev = open(MLX5_DEV_PATH, O_RDWR);
262	if (ctldev == -1)
263		err(1, "open "MLX5_DEV_PATH);
264	switch (act) {
265	case ACTION_DUMP_GET:
266		res = mlx5tool_save_dump(ctldev, &addr, dumpname);
267		break;
268	case ACTION_DUMP_RESET:
269		res = mlx5tool_dump_reset(ctldev, &addr);
270		break;
271	case ACTION_DUMP_FORCE:
272		res = mlx5tool_dump_force(ctldev, &addr);
273		break;
274	case ACTION_FW_UPDATE:
275		res = mlx5tool_fw_update(ctldev, &addr, img_fw_path);
276		break;
277	default:
278		res = 0;
279		break;
280	}
281	close(ctldev);
282	exit(res);
283}
284