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 <sys/types.h> 30 #include <sys/processor.h> 31 #include <fm/fmd_fmri.h> 32 33 #include <strings.h> 34 #include <errno.h> 35 #include <kstat.h> 36 37 #ifdef sparc 38 #include <cpu_mdesc.h> 39 #include <sys/fm/ldom.h> 40 #endif 41 42 /* 43 * The scheme plugin for cpu FMRIs. 44 */ 45 46 #ifdef sparc 47 cpu_t cpu; 48 static ldom_hdl_t *cpu_scheme_lhp; 49 #endif /* sparc */ 50 51 ssize_t 52 fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen) 53 { 54 int err; 55 uint8_t version; 56 uint32_t cpuid; 57 uint64_t serint; 58 char *serstr; 59 60 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0) 61 return (fmd_fmri_set_errno(EINVAL)); 62 63 if (version == CPU_SCHEME_VERSION0) { 64 if (nvlist_lookup_uint32(nvl, FM_FMRI_CPU_ID, &cpuid) != 0 || 65 nvlist_lookup_uint64(nvl, FM_FMRI_CPU_SERIAL_ID, &serint) 66 != 0) 67 return (fmd_fmri_set_errno(EINVAL)); 68 69 return (snprintf(buf, buflen, "cpu:///%s=%u/%s=%llX", 70 FM_FMRI_CPU_ID, cpuid, FM_FMRI_CPU_SERIAL_ID, 71 (u_longlong_t)serint)); 72 73 } else if (version == CPU_SCHEME_VERSION1) { 74 if (nvlist_lookup_uint32(nvl, FM_FMRI_CPU_ID, &cpuid) != 0) 75 return (fmd_fmri_set_errno(EINVAL)); 76 77 /* 78 * Serial number is an optional element 79 */ 80 if ((err = nvlist_lookup_string(nvl, FM_FMRI_CPU_SERIAL_ID, 81 &serstr)) != 0) 82 if (err == ENOENT) 83 return (snprintf(buf, buflen, "cpu:///%s=%u", 84 FM_FMRI_CPU_ID, cpuid)); 85 else 86 return (fmd_fmri_set_errno(EINVAL)); 87 else 88 return (snprintf(buf, buflen, "cpu:///%s=%u/%s=%s", 89 FM_FMRI_CPU_ID, cpuid, FM_FMRI_CPU_SERIAL_ID, 90 serstr)); 91 92 } else { 93 return (fmd_fmri_set_errno(EINVAL)); 94 } 95 } 96 97 static int 98 cpu_get_serialid_kstat(uint32_t cpuid, uint64_t *serialidp) 99 { 100 kstat_named_t *kn; 101 kstat_ctl_t *kc; 102 kstat_t *ksp; 103 int i; 104 105 if ((kc = kstat_open()) == NULL) 106 return (-1); /* errno is set for us */ 107 108 if ((ksp = kstat_lookup(kc, "cpu_info", cpuid, NULL)) == NULL) { 109 (void) kstat_close(kc); 110 return (fmd_fmri_set_errno(ENOENT)); 111 } 112 113 if (kstat_read(kc, ksp, NULL) == -1) { 114 int oserr = errno; 115 (void) kstat_close(kc); 116 return (fmd_fmri_set_errno(oserr)); 117 } 118 119 for (kn = ksp->ks_data, i = 0; i < ksp->ks_ndata; i++, kn++) { 120 if (strcmp(kn->name, "device_ID") == 0) { 121 *serialidp = kn->value.ui64; 122 (void) kstat_close(kc); 123 return (0); 124 } 125 } 126 127 (void) kstat_close(kc); 128 return (fmd_fmri_set_errno(ENOENT)); 129 } 130 131 static int 132 cpu_get_serialid_V1(uint32_t cpuid, char *serbuf, size_t len) 133 { 134 int err; 135 uint64_t serial = 0; 136 137 #ifdef sparc 138 if (cpu.cpu_mdesc_cpus != NULL) 139 err = cpu_get_serialid_mdesc(cpuid, &serial); 140 else 141 #endif /* sparc */ 142 err = cpu_get_serialid_kstat(cpuid, &serial); 143 144 (void) snprintf(serbuf, len, "%llX", (u_longlong_t)serial); 145 return (err); 146 } 147 148 static int 149 cpu_get_serialid_V0(uint32_t cpuid, uint64_t *serialidp) 150 { 151 #ifdef sparc 152 if (cpu.cpu_mdesc_cpus != NULL) 153 return (cpu_get_serialid_mdesc(cpuid, serialidp)); 154 else 155 #endif /* sparc */ 156 return (cpu_get_serialid_kstat(cpuid, serialidp)); 157 } 158 159 int 160 fmd_fmri_expand(nvlist_t *nvl) 161 { 162 uint8_t version; 163 uint32_t cpuid; 164 uint64_t serialid; 165 char *serstr, serbuf[21]; /* sizeof (UINT64_MAX) + '\0' */ 166 int rc; 167 168 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 169 nvlist_lookup_uint32(nvl, FM_FMRI_CPU_ID, &cpuid) != 0) 170 return (fmd_fmri_set_errno(EINVAL)); 171 172 if (version == CPU_SCHEME_VERSION0) { 173 if ((rc = nvlist_lookup_uint64(nvl, FM_FMRI_CPU_SERIAL_ID, 174 &serialid)) != 0) { 175 if (rc != ENOENT) 176 return (fmd_fmri_set_errno(rc)); 177 178 if (cpu_get_serialid_V0(cpuid, &serialid) != 0) 179 return (-1); /* errno is set for us */ 180 181 if ((rc = nvlist_add_uint64(nvl, FM_FMRI_CPU_SERIAL_ID, 182 serialid)) != 0) 183 return (fmd_fmri_set_errno(rc)); 184 #ifdef sparc 185 if (cpu.cpu_mdesc_cpus != NULL) { 186 md_cpumap_t *mcmp = cpu_find_cpumap(cpuid); 187 (void) nvlist_add_string(nvl, 188 FM_FMRI_CPU_CPUFRU, mcmp->cpumap_cpufru); 189 (void) nvlist_add_string(nvl, 190 FM_FMRI_HC_PART, mcmp->cpumap_cpufrupn); 191 (void) nvlist_add_string(nvl, 192 FM_FMRI_HC_SERIAL_ID, 193 mcmp->cpumap_cpufrusn); 194 } 195 #endif /* sparc */ 196 } 197 } else if (version == CPU_SCHEME_VERSION1) { 198 if ((rc = nvlist_lookup_string(nvl, FM_FMRI_CPU_SERIAL_ID, 199 &serstr)) != 0) { 200 if (rc != ENOENT) 201 return (fmd_fmri_set_errno(rc)); 202 203 if (cpu_get_serialid_V1(cpuid, serbuf, 21) != 0) 204 return (0); /* Serial number is optional */ 205 206 if ((rc = nvlist_add_string(nvl, FM_FMRI_CPU_SERIAL_ID, 207 serbuf)) != 0) 208 return (fmd_fmri_set_errno(rc)); 209 } 210 } else { 211 return (fmd_fmri_set_errno(EINVAL)); 212 } 213 214 return (0); 215 } 216 217 int 218 fmd_fmri_present(nvlist_t *nvl) 219 { 220 int rc; 221 uint8_t version; 222 uint32_t cpuid; 223 uint64_t nvlserid, curserid; 224 char *nvlserstr, curserbuf[21]; /* sizeof (UINT64_MAX) + '\0' */ 225 226 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 227 nvlist_lookup_uint32(nvl, FM_FMRI_CPU_ID, &cpuid) != 0) 228 return (fmd_fmri_set_errno(EINVAL)); 229 230 if (version == CPU_SCHEME_VERSION0) { 231 if (nvlist_lookup_uint64(nvl, FM_FMRI_CPU_SERIAL_ID, 232 &nvlserid) != 0) 233 return (fmd_fmri_set_errno(EINVAL)); 234 if (cpu_get_serialid_V0(cpuid, &curserid) != 0) 235 return (errno == ENOENT ? 0 : -1); 236 237 return (curserid == nvlserid); 238 239 } else if (version == CPU_SCHEME_VERSION1) { 240 if ((rc = nvlist_lookup_string(nvl, FM_FMRI_CPU_SERIAL_ID, 241 &nvlserstr)) != 0) 242 if (rc != ENOENT) 243 return (fmd_fmri_set_errno(EINVAL)); 244 245 /* 246 * Serial id may not be available, return true 247 */ 248 if (cpu_get_serialid_V1(cpuid, curserbuf, 21) != 0) 249 return (1); 250 251 return (strcmp(curserbuf, nvlserstr) == 0 ? 1 : 0); 252 253 } else { 254 return (fmd_fmri_set_errno(EINVAL)); 255 } 256 } 257 258 int 259 fmd_fmri_unusable(nvlist_t *nvl) 260 { 261 uint8_t version; 262 uint32_t cpuid; 263 264 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 265 version > FM_CPU_SCHEME_VERSION || 266 nvlist_lookup_uint32(nvl, FM_FMRI_CPU_ID, &cpuid) != 0) 267 return (fmd_fmri_set_errno(EINVAL)); 268 269 #ifdef sparc 270 { 271 int cpustatus = ldom_fmri_status(cpu_scheme_lhp, nvl); 272 273 return (cpustatus == P_FAULTED || (cpustatus == P_OFFLINE && 274 ldom_major_version(cpu_scheme_lhp) == 1)); 275 } 276 #else 277 return (p_online(cpuid, P_STATUS) == P_FAULTED); 278 #endif 279 } 280 281 #ifdef sparc 282 int 283 fmd_fmri_init(void) 284 { 285 cpu_scheme_lhp = ldom_init(fmd_fmri_alloc, fmd_fmri_free); 286 return (cpu_mdesc_init(cpu_scheme_lhp)); 287 } 288 289 void 290 fmd_fmri_fini(void) 291 { 292 cpu_mdesc_fini(); 293 ldom_fini(cpu_scheme_lhp); 294 } 295 #endif /* sparc */ 296