1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright 2019 Joyent, Inc. 14 * Copyright 2022 Oxide Computer Company 15 */ 16 17 /* 18 * Private command to manipulate link speeds of PCIe bridges and allow 19 * retraining. This is designed to aid debugging. 20 */ 21 22 #include <unistd.h> 23 #include <stdarg.h> 24 #include <stdio.h> 25 #include <libgen.h> 26 #include <string.h> 27 #include <err.h> 28 #include <stdlib.h> 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <fcntl.h> 32 33 #include <pcieb_ioctl.h> 34 35 static const char *pcieb_progname; 36 37 static void 38 pcieb_usage(const char *fmt, ...) 39 { 40 if (fmt != NULL) { 41 va_list ap; 42 43 (void) fprintf(stderr, "%s: ", pcieb_progname); 44 va_start(ap, fmt); 45 (void) vfprintf(stderr, fmt, ap); 46 va_end(ap); 47 } 48 49 (void) fprintf(stderr, "Usage: %s [-x] [-s speed] pcie-bridge\n" 50 "\n" 51 "\t-s speed Set link to speed\n", 52 "\t-x Retrain link\n", 53 pcieb_progname); 54 55 } 56 57 static uint32_t 58 pcieb_parse_speed(const char *s) 59 { 60 if (strcasecmp(s, "2.5") == 0 || strcasecmp(s, "gen1") == 0) { 61 return (PCIEB_LINK_SPEED_GEN1); 62 } else if (strcasecmp(s, "5") == 0 || strcasecmp(s, "gen2") == 0) { 63 return (PCIEB_LINK_SPEED_GEN2); 64 } else if (strcasecmp(s, "8") == 0 || strcasecmp(s, "gen3") == 0) { 65 return (PCIEB_LINK_SPEED_GEN3); 66 } else if (strcasecmp(s, "16") == 0 || strcasecmp(s, "gen4") == 0) { 67 return (PCIEB_LINK_SPEED_GEN4); 68 } else if (strcasecmp(s, "32") == 0 || strcasecmp(s, "gen5") == 0) { 69 return (PCIEB_LINK_SPEED_GEN5); 70 } else if (strcasecmp(s, "64") == 0 || strcasecmp(s, "gen6") == 0) { 71 return (PCIEB_LINK_SPEED_GEN6); 72 } else { 73 errx(EXIT_FAILURE, "invalid speed: %s", s); 74 } 75 } 76 77 int 78 main(int argc, char *argv[]) 79 { 80 int c; 81 boolean_t retrain = B_FALSE; 82 boolean_t set = B_FALSE; 83 boolean_t get = B_TRUE; 84 uint32_t speed = PCIEB_LINK_SPEED_UNKNOWN; 85 int fd; 86 87 pcieb_progname = basename(argv[0]); 88 89 while ((c = getopt(argc, argv, ":xs:")) != -1) { 90 switch (c) { 91 case 's': 92 speed = pcieb_parse_speed(optarg); 93 set = B_TRUE; 94 get = B_FALSE; 95 break; 96 case 'x': 97 retrain = B_TRUE; 98 get = B_FALSE; 99 break; 100 case ':': 101 pcieb_usage("option -%c requires an operand\n", optopt); 102 return (2); 103 case '?': 104 default: 105 pcieb_usage("unknown option: -%c\n", optopt); 106 return (2); 107 108 } 109 } 110 111 argc -= optind; 112 argv += optind; 113 114 if (argc != 1) { 115 pcieb_usage("missing required PCIe bridge device\n"); 116 return (2); 117 } 118 119 if ((fd = open(argv[0], O_RDWR)) < 0) { 120 err(EXIT_FAILURE, "failed to open %s", argv[0]); 121 } 122 123 if (set) { 124 pcieb_ioctl_target_speed_t pits; 125 126 pits.pits_flags = 0; 127 pits.pits_speed = speed; 128 129 if (ioctl(fd, PCIEB_IOCTL_SET_TARGET_SPEED, &pits) != 0) { 130 err(EXIT_FAILURE, "failed to set target speed"); 131 } 132 } 133 134 if (retrain) { 135 if (ioctl(fd, PCIEB_IOCTL_RETRAIN) != 0) { 136 err(EXIT_FAILURE, "failed to retrain link"); 137 } 138 } 139 140 if (get) { 141 pcieb_ioctl_target_speed_t pits; 142 143 if (ioctl(fd, PCIEB_IOCTL_GET_TARGET_SPEED, &pits) != 0) { 144 err(EXIT_FAILURE, "failed to get target speed"); 145 } 146 147 (void) printf("Bridge target speed: "); 148 switch (pits.pits_speed) { 149 case PCIEB_LINK_SPEED_GEN1: 150 (void) printf("2.5 GT/s (gen1)\n"); 151 break; 152 case PCIEB_LINK_SPEED_GEN2: 153 (void) printf("5.0 GT/s (gen2)\n"); 154 break; 155 case PCIEB_LINK_SPEED_GEN3: 156 (void) printf("8.0 GT/s (gen3)\n"); 157 break; 158 case PCIEB_LINK_SPEED_GEN4: 159 (void) printf("16.0 GT/s (gen4)\n"); 160 break; 161 case PCIEB_LINK_SPEED_GEN5: 162 (void) printf("32.0 GT/s (gen5)\n"); 163 break; 164 case PCIEB_LINK_SPEED_GEN6: 165 (void) printf("64.0 GT/s (gen6)\n"); 166 break; 167 default: 168 (void) printf("Unknown Value: 0x%x\n", pits.pits_speed); 169 } 170 171 if ((pits.pits_flags & ~PCIEB_FLAGS_ADMIN_SET) != 0) { 172 (void) printf("Unknown flags: 0x%x\n", pits.pits_flags); 173 } else if ((pits.pits_flags & PCIEB_FLAGS_ADMIN_SET) != 0) { 174 (void) printf("Flags: Admin Set Speed\n"); 175 } 176 } 177 178 (void) close(fd); 179 return (0); 180 } 181