1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <mem.h> 30 #include <fm/fmd_fmri.h> 31 #include <fm/libtopo.h> 32 33 #include <string.h> 34 #include <strings.h> 35 #include <ctype.h> 36 37 #define ISHCUNUM(unum) (strncmp(unum, "hc:/", 4) == 0) 38 39 /* 40 * Given a DIMM or bank unum, mem_unum_burst will break it apart into individual 41 * DIMM names. If it's a DIMM, one name will be returned. If it's a bank, the 42 * unums for the individual DIMMs will be returned. 43 * 44 * Plain J-number DIMM and bank unums are simple. J DIMMs have one J number. J 45 * banks have multiple whitespace-separated J numbers. 46 * 47 * The others are more complex, and consist of a common portion c, a colon, and 48 * a DIMM-specific portion d. DIMMs are of the form "c: d", while banks are of 49 * the form "c: d d ...". The patterns are designed to handle the complex case, 50 * but also handle the simple ones as an afterthought. bd_pat is used to 51 * match specific styles of unum. In bd_pat, the first %n indicates the end of 52 * the common portion ("c" above). The second %n marks the beginning of the 53 * repetitive portion ("d" above). The third %n is used to determine whether or 54 * not the entire pattern matched. bd_reppat is used to match instances of the 55 * repetitive part. 56 * 57 * sscanf is your disturbingly powerful friend. 58 * 59 * The "bd_subst" element of the bank_dimm structure was added for Ontario 60 * in order to accommodate its bank string names. Previously, to convert 61 * from a bank representation <common piece> <dimm1> <dimm2> ... 62 * we concatenated the common piece with each dimm-specific piece in turn, 63 * possibly deleting some characters in between. Ontario is the first 64 * platform which requires that characters be substituted (like a vi s/1/2/) 65 * in place of characters deleted. "bd_subst" represents the character(s) 66 * to be substituted between the common piece and each dimm-specific piece 67 * as part of the bursting. For prior platforms, this value is skipped. 68 * 69 * Example: 70 * input: "MB/CMP0/CH3: R1/D0/J1901 R1/D1/J2001" 71 * outputs: "MB/CMP0/CH3/R1/D0/J1901", "MB/CMP0/CH3/R1/D1/J2001" 72 */ 73 74 typedef struct bank_dimm { 75 const char *bd_pat; 76 const char *bd_reppat; 77 const char *bd_subst; 78 } bank_dimm_t; 79 80 static const bank_dimm_t bank_dimm[] = { 81 { "%n%nJ%*4d%n", " J%*4d%n" }, 82 { "MB/P%*d/%nB%*d:%n%n", " B%*d/D%*d%n" }, 83 { "MB/P%*d/%nB%*d/D%*d:%n%n", " B%*d/D%*d%n" }, 84 { "C%*d/P%*d/%nB%*d:%n%n", " B%*d/D%*d%n" }, 85 { "C%*d/P%*d/%nB%*d/D%*d:%n%n", " B%*d/D%*d%n" }, 86 { "Slot %*c: %n%nJ%*4d%n", " J%*4d%n" }, 87 { "%n%nDIMM%*d%n", " DIMM%*d%n" }, 88 { "MB/%nDIMM%*d MB/DIMM%*d: %n%n", " DIMM%*d%n" }, 89 { "MB/%nDIMM%*d:%n%n", " DIMM%*d%n" }, 90 { "MB/CMP%*d/CH%*d%n:%n%n", " R%*d/D%*d/J%*4d%n", "/" }, 91 { "MB/CMP%*d/CH%*d%n%n%n", "/R%*d/D%*d/J%*4d%n" }, 92 { "MB/C%*d/P%*d/%nB%*d:%n%n", " B%*d/D%*d%n" }, 93 { "MB/C%*d/P%*d/%nB%*d/D%*d:%n%n", " B%*d/D%*d%n" }, 94 { "/MBU_A/MEMB%*d/%n%nMEM%*d%*1c%n", " MEM%*d%*1c%n" }, 95 { "/MBU_B/MEMB%*d/%n%nMEM%*d%*1c%n", " MEM%*d%*1c%n" }, 96 { "/CMU%*2d/%n%nMEM%*2d%*1c%n", " MEM%*2d%*1c%n" }, 97 { NULL } 98 }; 99 100 /* 101 * Burst Serengeti and Starcat-style unums. 102 * A DIMM unum string is expected to be in this form: 103 * "[/N0/]SB12/P0/B0/D2 [J13500]" 104 * A bank unum string is expected to be in this form: 105 * "[/N0/]SB12/P0/B0 [J13500, ...]" 106 */ 107 static int 108 mem_unum_burst_sgsc(const char *pat, char ***dimmsp, size_t *ndimmsp) 109 { 110 char buf[64]; 111 char **dimms; 112 char *base; 113 const char *c; 114 char *copy; 115 size_t copysz; 116 int i; 117 118 /* 119 * No expansion is required for a DIMM unum 120 */ 121 if (strchr(pat, 'D') != NULL) { 122 dimms = fmd_fmri_alloc(sizeof (char *)); 123 dimms[0] = fmd_fmri_strdup(pat); 124 *dimmsp = dimms; 125 *ndimmsp = 1; 126 return (0); 127 } 128 129 /* 130 * strtok is destructive so we need to work with 131 * a copy and keep track of the size allocated. 132 */ 133 copysz = strlen(pat) + 1; 134 copy = fmd_fmri_alloc(copysz); 135 (void) strcpy(copy, pat); 136 137 base = strtok(copy, " "); 138 139 /* There are four DIMMs in a bank */ 140 dimms = fmd_fmri_alloc(sizeof (char *) * 4); 141 142 for (i = 0; i < 4; i++) { 143 (void) snprintf(buf, sizeof (buf), "%s/D%d", base, i); 144 145 if ((c = strtok(NULL, " ")) != NULL) 146 (void) snprintf(buf, sizeof (buf), "%s %s", buf, c); 147 148 dimms[i] = fmd_fmri_strdup(buf); 149 } 150 151 fmd_fmri_free(copy, copysz); 152 153 *dimmsp = dimms; 154 *ndimmsp = 4; 155 return (0); 156 } 157 158 159 /* 160 * Returns 0 (with dimmsp and ndimmsp set) if the unum could be bursted, -1 161 * otherwise. 162 */ 163 static int 164 mem_unum_burst_pattern(const char *pat, char ***dimmsp, size_t *ndimmsp) 165 { 166 const bank_dimm_t *bd; 167 char **dimms = NULL, **newdimms; 168 size_t ndimms = 0; 169 const char *c; 170 171 172 for (bd = bank_dimm; bd->bd_pat != NULL; bd++) { 173 int replace, start, matched; 174 char dimmname[64]; 175 176 replace = start = matched = -1; 177 (void) sscanf(pat, bd->bd_pat, &replace, &start, &matched); 178 if (matched == -1) 179 continue; 180 181 (void) strlcpy(dimmname, pat, sizeof (dimmname)); 182 if (bd->bd_subst != NULL) { 183 (void) strlcpy(dimmname+replace, bd->bd_subst, 184 sizeof (dimmname) - strlen(bd->bd_subst)); 185 replace += strlen(bd->bd_subst); 186 } 187 188 c = pat + start; 189 while (*c != '\0') { 190 int dimmlen = -1; 191 192 (void) sscanf(c, bd->bd_reppat, &dimmlen); 193 if (dimmlen == -1) 194 break; 195 196 while (*c == ' ') { 197 c++; 198 dimmlen--; 199 } 200 201 if (dimmlen > sizeof (dimmname) - replace) 202 break; 203 204 (void) strlcpy(dimmname + replace, c, dimmlen + 1); 205 206 newdimms = fmd_fmri_alloc(sizeof (char *) * 207 (ndimms + 1)); 208 if (ndimms != 0) { 209 bcopy(dimms, newdimms, sizeof (char *) * 210 ndimms); 211 fmd_fmri_free(dimms, sizeof (char *) * ndimms); 212 } 213 newdimms[ndimms++] = fmd_fmri_strdup(dimmname); 214 dimms = newdimms; 215 216 c += dimmlen; 217 218 if (*c != ' ' && *c != '\0') 219 break; 220 } 221 222 if (*c != '\0') 223 break; 224 225 *dimmsp = dimms; 226 *ndimmsp = ndimms; 227 228 return (0); 229 } 230 231 mem_strarray_free(dimms, ndimms); 232 233 return (fmd_fmri_set_errno(EINVAL)); 234 } 235 236 int 237 mem_unum_burst(const char *pat, char ***dimmsp, size_t *ndimmsp) 238 { 239 const char *platform = fmd_fmri_get_platform(); 240 241 /* 242 * Call mem_unum_burst_sgsc() for Starcat, Serengeti, and 243 * Lightweight 8 platforms. Call mem_unum_burst_pattern() 244 * for all other platforms. 245 */ 246 if (strcmp(platform, "SUNW,Sun-Fire-15000") == 0 || 247 strcmp(platform, "SUNW,Sun-Fire") == 0 || 248 strcmp(platform, "SUNW,Netra-T12") == 0) 249 return (mem_unum_burst_sgsc(pat, dimmsp, ndimmsp)); 250 else 251 return (mem_unum_burst_pattern(pat, dimmsp, ndimmsp)); 252 } 253 254 /* 255 * The unum containership operation is designed to tell the caller whether a 256 * given FMRI contains another. In the case of this plugin, we tell the caller 257 * whether a given memory FMRI (usually a bank) contains another (usually a 258 * DIMM). We do this in one of two ways, depending on the platform. For most 259 * platforms, we can use the bursting routine to generate the list of member 260 * unums from the container unum. Membership can then be determined by 261 * searching the bursted list for the containee's unum. 262 * 263 * Some platforms, however, cannot be bursted, as their bank unums do not 264 * contain all of the information needed to generate the complete list of 265 * member DIMM unums. For these unums, we must make do with a substring 266 * comparison. 267 */ 268 269 static int 270 unum_contains_bypat(const char *erunum, const char *eeunum) 271 { 272 char **ernms, **eenms; 273 size_t nernms, neenms; 274 int i, j, rv = 1; 275 276 if (mem_unum_burst(erunum, &ernms, &nernms) < 0) 277 return (fmd_fmri_set_errno(EINVAL)); 278 if (mem_unum_burst(eeunum, &eenms, &neenms) < 0) { 279 mem_strarray_free(ernms, nernms); 280 return (fmd_fmri_set_errno(EINVAL)); 281 } 282 283 for (i = 0; i < neenms; i++) { 284 for (j = 0; j < nernms; j++) { 285 if (strcmp(eenms[i], ernms[j]) == 0) 286 break; 287 } 288 289 if (j == nernms) { 290 /* 291 * This DIMM was not found in the container. 292 */ 293 rv = 0; 294 break; 295 } 296 } 297 298 mem_strarray_free(ernms, nernms); 299 mem_strarray_free(eenms, neenms); 300 301 return (rv); 302 } 303 304 static int 305 unum_strip_one_jnum(const char *unum, uint_t *endp) 306 { 307 char *c; 308 int i; 309 310 if ((c = strrchr(unum, 'J')) == NULL) 311 return (0); 312 313 while (c > unum && isspace(c[-1])) 314 c--; 315 316 (void) sscanf(c, " J%*[0-9] %n", &i); 317 if (i == 0 || (uintptr_t)(c - unum) + i != strlen(unum)) 318 return (0); 319 320 *endp = (uint_t)(c - unum); 321 return (1); 322 } 323 324 325 static int 326 unum_contains_bysubstr(const char *erunum, const char *eeunum) 327 { 328 uint_t erlen, eelen; 329 int nojnumstrip = 0; 330 331 /* 332 * This comparison method is only known to work on specific types of 333 * unums. Check for those types here. 334 */ 335 if ((strncmp(erunum, "/N", 2) != 0 && strncmp(erunum, "/IO", 3) != 0 && 336 strncmp(erunum, "/SB", 3) != 0) || 337 (strncmp(eeunum, "/N", 2) != 0 && strncmp(eeunum, "/IO", 3) != 0 && 338 strncmp(eeunum, "/SB", 3) != 0)) { 339 if (ISHCUNUM(erunum) && ISHCUNUM(eeunum)) { 340 nojnumstrip = 1; 341 erlen = strlen(erunum); 342 eelen = strlen(eeunum); 343 } else { 344 return (fmd_fmri_set_errno(EINVAL)); 345 } 346 } 347 348 if (!nojnumstrip) { 349 erlen = unum_strip_one_jnum(erunum, &erlen) ? 350 erlen : strlen(erunum); 351 eelen = unum_strip_one_jnum(eeunum, &eelen) ? 352 eelen : strlen(eeunum); 353 } 354 355 return (strncmp(erunum, eeunum, MIN(erlen, eelen)) == 0); 356 } 357 358 typedef int unum_cmptor_f(const char *, const char *); 359 360 static unum_cmptor_f *const unum_cmptors[] = { 361 unum_contains_bypat, 362 unum_contains_bysubstr 363 }; 364 365 int 366 mem_unum_contains(const char *erunum, const char *eeunum) 367 { 368 static int cmptor = 0; 369 int rc; 370 371 while (isspace(*erunum)) 372 erunum++; 373 while (isspace(*eeunum)) 374 eeunum++; 375 376 if ((rc = unum_cmptors[cmptor](erunum, eeunum)) >= 0) 377 return (rc); 378 379 if ((rc = unum_cmptors[cmptor == 0](erunum, eeunum)) >= 0) { 380 /* 381 * We succeeded with the non-default comparator. Change the 382 * default so we use the correct one next time. 383 */ 384 cmptor = (cmptor == 0); 385 } 386 387 return (rc); 388 } 389 390 /* 391 * If an asru has a unum string that is an hc path string then return 392 * a new nvl (to be freed by the caller) that is a duplicate of the 393 * original but with an additional member of a reconstituted hc fmri. 394 */ 395 int 396 mem_unum_rewrite(nvlist_t *nvl, nvlist_t **rnvl) 397 { 398 int err; 399 char *unumstr; 400 nvlist_t *unum; 401 struct topo_hdl *thp; 402 403 if (nvlist_lookup_string(nvl, FM_FMRI_MEM_UNUM, &unumstr) != 0 || 404 !ISHCUNUM(unumstr)) 405 return (0); 406 407 thp = fmd_fmri_topology(TOPO_VERSION); 408 409 if (topo_fmri_str2nvl(thp, unumstr, &unum, &err) != 0) 410 return (EINVAL); 411 412 if ((err = nvlist_dup(nvl, rnvl, 0)) != 0) { 413 nvlist_free(unum); 414 return (err); 415 } 416 417 err = nvlist_add_nvlist(*rnvl, FM_FMRI_MEM_UNUM "-fmri", unum); 418 nvlist_free(unum); 419 420 if (err != 0) 421 nvlist_free(*rnvl); 422 423 return (err); 424 } 425