mlx5tool.c revision 353242
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 353242 2019-10-07 09:49:03Z 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 int 193mlx5tool_fw_reset(int ctldev, const struct mlx5_tool_addr *addr) 194{ 195 196 if (ioctl(ctldev, MLX5_FW_RESET, addr) == -1) { 197 warn("MLX5_FW_RESET"); 198 return (1); 199 } 200 return (0); 201} 202 203#define MLX5_EEPROM_HIGH_PAGE_OFFSET 128 204#define MLX5_EEPROM_PAGE_LENGTH 256 205 206static void 207mlx5tool_eeprom_print(struct mlx5_eeprom_get *eeprom_info) 208{ 209 unsigned int byte_to_write, index_in_row, line_length, row; 210 211 byte_to_write = 0; 212 line_length = 16; 213 214 printf("\nOffset\t\tValues\n"); 215 printf("------\t\t------"); 216 while (byte_to_write < eeprom_info->eeprom_info_out_len) { 217 printf("\n0x%04X\t\t", byte_to_write); 218 for (index_in_row = 0; index_in_row < line_length; 219 index_in_row++) { 220 printf("%02X ", 221 ((uint8_t *)eeprom_info->eeprom_info_buf)[ 222 byte_to_write]); 223 byte_to_write++; 224 } 225 } 226 227 if (eeprom_info->eeprom_info_page_valid) { 228 row = MLX5_EEPROM_HIGH_PAGE_OFFSET; 229 printf("\n\nUpper Page 0x03\n"); 230 printf("\nOffset\t\tValues\n"); 231 printf("------\t\t------"); 232 for (row = MLX5_EEPROM_HIGH_PAGE_OFFSET; 233 row < MLX5_EEPROM_PAGE_LENGTH;) { 234 printf("\n0x%04X\t\t", row); 235 for (index_in_row = 0; 236 index_in_row < line_length; 237 index_in_row++) { 238 printf("%02X ", 239 ((uint8_t *)eeprom_info-> 240 eeprom_info_buf)[byte_to_write]); 241 byte_to_write++; 242 row++; 243 } 244 } 245 } 246 printf("\n"); 247} 248 249static int 250mlx5tool_get_eeprom_info(int ctldev, const struct mlx5_tool_addr *addr) 251{ 252 struct mlx5_eeprom_get eeprom_info; 253 int error; 254 255 memset(&eeprom_info, 0, sizeof(eeprom_info)); 256 eeprom_info.devaddr = *addr; 257 258 error = ioctl(ctldev, MLX5_EEPROM_GET, &eeprom_info); 259 if (error != 0) { 260 warn("MLX5_EEPROM_GET"); 261 return (error); 262 } 263 eeprom_info.eeprom_info_buf = 264 malloc(eeprom_info.eeprom_info_out_len + MLX5_EEPROM_PAGE_LENGTH); 265 if (eeprom_info.eeprom_info_buf == NULL) { 266 warn("alloc eeprom_info.eeprom_info_buf "); 267 return (ENOMEM); 268 } 269 error = ioctl(ctldev, MLX5_EEPROM_GET, &eeprom_info); 270 if (error != 0) { 271 warn("MLX5_EEPROM_GET"); 272 free(eeprom_info.eeprom_info_buf); 273 return (error); 274 } 275 276 mlx5tool_eeprom_print(&eeprom_info); 277 278 free(eeprom_info.eeprom_info_buf); 279 return (0); 280} 281 282static void 283usage(void) 284{ 285 286 fprintf(stderr, 287 "Usage: mlx5tool -d pci<d:b:s:f> [-w -o dump.file | -r |" 288 " -e | -f fw.mfa2 | -z]\n"); 289 fprintf(stderr, "\t-w - write firmware dump to the specified file\n"); 290 fprintf(stderr, "\t-r - reset dump\n"); 291 fprintf(stderr, "\t-E - get eeprom info\n"); 292 fprintf(stderr, "\t-e - force dump\n"); 293 fprintf(stderr, "\t-f fw.img - flash firmware from fw.img\n"); 294 fprintf(stderr, "\t-z - initiate firmware reset\n"); 295 exit(1); 296} 297 298enum mlx5_action { 299 ACTION_DUMP_GET, 300 ACTION_DUMP_RESET, 301 ACTION_DUMP_FORCE, 302 ACTION_FW_UPDATE, 303 ACTION_FW_RESET, 304 ACTION_GET_EEPROM_INFO, 305 ACTION_NONE, 306}; 307 308int 309main(int argc, char *argv[]) 310{ 311 struct mlx5_tool_addr addr; 312 char *dumpname; 313 char *addrstr; 314 char *img_fw_path; 315 int c, ctldev, res; 316 enum mlx5_action act; 317 318 act = ACTION_NONE; 319 addrstr = NULL; 320 dumpname = NULL; 321 img_fw_path = NULL; 322 while ((c = getopt(argc, argv, "d:Eef:ho:rwz")) != -1) { 323 switch (c) { 324 case 'd': 325 addrstr = optarg; 326 break; 327 case 'w': 328 if (act != ACTION_NONE) 329 usage(); 330 act = ACTION_DUMP_GET; 331 break; 332 case 'E': 333 if (act != ACTION_NONE) 334 usage(); 335 act = ACTION_GET_EEPROM_INFO; 336 break; 337 case 'e': 338 if (act != ACTION_NONE) 339 usage(); 340 act = ACTION_DUMP_FORCE; 341 break; 342 case 'o': 343 dumpname = optarg; 344 break; 345 case 'r': 346 if (act != ACTION_NONE) 347 usage(); 348 act = ACTION_DUMP_RESET; 349 break; 350 case 'f': 351 if (act != ACTION_NONE) 352 usage(); 353 act = ACTION_FW_UPDATE; 354 img_fw_path = optarg; 355 break; 356 case 'z': 357 if (act != ACTION_NONE) 358 usage(); 359 act = ACTION_FW_RESET; 360 break; 361 case 'h': 362 default: 363 usage(); 364 } 365 } 366 if (act == ACTION_NONE || (dumpname != NULL && 367 act != ACTION_DUMP_GET) || (img_fw_path != NULL && 368 act != ACTION_FW_UPDATE)) 369 usage(); 370 if (parse_pci_addr(addrstr, &addr) != 0) 371 exit(1); 372 373 ctldev = open(MLX5_DEV_PATH, O_RDWR); 374 if (ctldev == -1) 375 err(1, "open "MLX5_DEV_PATH); 376 switch (act) { 377 case ACTION_DUMP_GET: 378 res = mlx5tool_save_dump(ctldev, &addr, dumpname); 379 break; 380 case ACTION_DUMP_RESET: 381 res = mlx5tool_dump_reset(ctldev, &addr); 382 break; 383 case ACTION_DUMP_FORCE: 384 res = mlx5tool_dump_force(ctldev, &addr); 385 break; 386 case ACTION_FW_UPDATE: 387 res = mlx5tool_fw_update(ctldev, &addr, img_fw_path); 388 break; 389 case ACTION_FW_RESET: 390 res = mlx5tool_fw_reset(ctldev, &addr); 391 break; 392 case ACTION_GET_EEPROM_INFO: 393 res = mlx5tool_get_eeprom_info(ctldev, &addr); 394 break; 395 default: 396 res = 0; 397 break; 398 } 399 close(ctldev); 400 exit(res); 401} 402