1 /*- 2 * Copyright (c) 2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 #include "diag.h" 30 31 #include "ah.h" 32 #include "ah_internal.h" 33 34 #include "dumpregs.h" 35 36 #include <getopt.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <ctype.h> 40 #include <err.h> 41 #include <errno.h> 42 43 typedef struct { 44 HAL_REVS revs; 45 #define MAXREGS 5*1024 46 struct dumpreg *regs[MAXREGS]; 47 u_int nregs; 48 } dumpregs_t; 49 static dumpregs_t state; 50 51 static uint32_t regread(int s, struct ath_diag *atd, uint32_t r); 52 static void regwrite(int s, struct ath_diag *atd, uint32_t r, uint32_t v); 53 static const struct dumpreg *reglookup(const char *v); 54 55 static void 56 usage(void) 57 { 58 fprintf(stderr, "usage: athpoke [-i interface] [reg[=value]] ...\n"); 59 exit(-1); 60 } 61 62 int 63 main(int argc, char *argv[]) 64 { 65 struct ath_diag atd; 66 const char *ifname; 67 char *eptr; 68 int c, s; 69 70 s = socket(AF_INET, SOCK_DGRAM, 0); 71 if (s < 0) 72 err(1, "socket"); 73 ifname = getenv("ATH"); 74 if (!ifname) 75 ifname = ATH_DEFAULT; 76 77 while ((c = getopt(argc, argv, "i:")) != -1) 78 switch (c) { 79 case 'i': 80 ifname = optarg; 81 break; 82 default: 83 usage(); 84 /*NOTREACHED*/ 85 } 86 strncpy(atd.ad_name, ifname, sizeof (atd.ad_name)); 87 88 atd.ad_id = HAL_DIAG_REVS; 89 atd.ad_out_data = (caddr_t) &state.revs; 90 atd.ad_out_size = sizeof(state.revs); 91 if (ioctl(s, SIOCGATHDIAG, &atd) < 0) 92 err(1, atd.ad_name); 93 94 argc -= optind; 95 argv += optind; 96 97 for (; argc > 0; argc--, argv++) { 98 char *cp; 99 const struct dumpreg *dr; 100 uint32_t reg; 101 102 cp = strchr(argv[0], '='); 103 if (cp != NULL) 104 *cp++ = '\0'; 105 dr = reglookup(argv[0]); 106 if (dr == NULL) { 107 errno = 0; 108 reg = (uint32_t) strtoul(argv[0], &eptr, 0); 109 if (argv[0] == eptr || eptr[0] != '\0') 110 errx(1, "invalid register \"%s\"", argv[0]); 111 } else 112 reg = dr->addr; 113 if (cp != NULL) 114 regwrite(s, &atd, reg, (uint32_t) strtoul(cp, NULL, 0)); 115 printf("%s = %08x\n", argv[0], regread(s, &atd, reg)); 116 } 117 return 0; 118 } 119 120 static uint32_t 121 regread(int s, struct ath_diag *atd, uint32_t r) 122 { 123 HAL_REGRANGE ra; 124 uint32_t v[3]; 125 126 ra.start = r; 127 ra.end = 0; 128 129 atd->ad_in_data = (caddr_t) &ra; 130 atd->ad_in_size = sizeof(ra); 131 atd->ad_out_data = (caddr_t) v; 132 atd->ad_out_size = sizeof(v); 133 atd->ad_id = HAL_DIAG_REGS | ATH_DIAG_IN | ATH_DIAG_DYN; 134 if (ioctl(s, SIOCGATHDIAG, atd) < 0) 135 err(1, atd->ad_name); 136 return v[2]; 137 } 138 139 static void 140 regwrite(int s, struct ath_diag *atd, uint32_t r, uint32_t v) 141 { 142 HAL_REGWRITE rw; 143 144 rw.addr = r; 145 rw.value = v; 146 atd->ad_in_data = (caddr_t) &rw; 147 atd->ad_in_size = sizeof(rw); 148 atd->ad_id = HAL_DIAG_SETREGS | ATH_DIAG_IN; 149 if (ioctl(s, SIOCGATHDIAG, atd) < 0) 150 err(1, atd->ad_name); 151 } 152 153 static int 154 regcompar(const void *a, const void *b) 155 { 156 const struct dumpreg *ra = *(const struct dumpreg **)a; 157 const struct dumpreg *rb = *(const struct dumpreg **)b; 158 return ra->addr - rb->addr; 159 } 160 161 void 162 register_regs(struct dumpreg *chipregs, u_int nchipregs, 163 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max) 164 { 165 const int existing_regs = state.nregs; 166 int i, j; 167 168 for (i = 0; i < nchipregs; i++) { 169 struct dumpreg *nr = &chipregs[i]; 170 if (nr->srevMin == 0) 171 nr->srevMin = def_srev_min; 172 if (nr->srevMax == 0) 173 nr->srevMax = def_srev_max; 174 if (nr->phyMin == 0) 175 nr->phyMin = def_phy_min; 176 if (nr->phyMax == 0) 177 nr->phyMax = def_phy_max; 178 for (j = 0; j < existing_regs; j++) { 179 struct dumpreg *r = state.regs[j]; 180 /* 181 * Check if we can just expand the mac+phy 182 * coverage for the existing entry. 183 */ 184 if (nr->addr == r->addr && 185 (nr->name == r->name || 186 nr->name != NULL && r->name != NULL && 187 strcmp(nr->name, r->name) == 0)) { 188 if (nr->srevMin < r->srevMin && 189 (r->srevMin <= nr->srevMax && 190 nr->srevMax+1 <= r->srevMax)) { 191 r->srevMin = nr->srevMin; 192 goto skip; 193 } 194 if (nr->srevMax > r->srevMax && 195 (r->srevMin <= nr->srevMin && 196 nr->srevMin <= r->srevMax)) { 197 r->srevMax = nr->srevMax; 198 goto skip; 199 } 200 } 201 if (r->addr > nr->addr) 202 break; 203 } 204 /* 205 * New item, add to the end, it'll be sorted below. 206 */ 207 if (state.nregs == MAXREGS) 208 errx(-1, "too many registers; bump MAXREGS"); 209 state.regs[state.nregs++] = nr; 210 skip: 211 ; 212 } 213 qsort(state.regs, state.nregs, sizeof(struct dumpreg *), regcompar); 214 } 215 216 void 217 register_keycache(u_int nslots, 218 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max) 219 { 220 /* discard, no use */ 221 } 222 223 void 224 register_range(u_int brange, u_int erange, int type, 225 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max) 226 { 227 /* discard, no use */ 228 } 229 230 static const struct dumpreg * 231 reglookup(const char *v) 232 { 233 const HAL_REVS *revs = &state.revs; 234 int i; 235 236 if (strncasecmp(v, "AR_", 3) == 0) 237 v += 3; 238 for (i = 0; i < state.nregs; i++) { 239 const struct dumpreg *dr = state.regs[i]; 240 if (MAC_MATCH(dr, revs->ah_macVersion, revs->ah_macRev) && 241 strcasecmp(v, dr->name) == 0) 242 return dr; 243 } 244 return NULL; 245 } 246