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.
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.
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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #include <fm/fmd_fmri.h>
29 #include <fm/libtopo.h>
30 #include <fm/fmd_agent.h>
39 mem_fmri_get_unum(nvlist_t
*nvl
, char **unump
)
44 if (nvlist_lookup_uint8(nvl
, FM_VERSION
, &version
) != 0 ||
45 version
> FM_MEM_SCHEME_VERSION
||
46 nvlist_lookup_string(nvl
, FM_FMRI_MEM_UNUM
, &unum
) != 0)
47 return (fmd_fmri_set_errno(EINVAL
));
55 page_isretired(nvlist_t
*fmri
, int *errp
)
60 if ((hdl
= fmd_agent_open(FMD_AGENT_VERSION
)) == NULL
)
62 rc
= fmd_agent_page_isretired(hdl
, fmri
);
63 err
= fmd_agent_errno(hdl
);
72 fmd_fmri_nvl2str(nvlist_t
*nvl
, char *buf
, size_t buflen
)
76 char *rawunum
, *preunum
, *escunum
, *prefix
;
80 if (mem_fmri_get_unum(nvl
, &rawunum
) < 0)
81 return (-1); /* errno is set for us */
84 * If we have a well-formed unum (hc-FMRI), use the string verbatim
85 * to form the initial mem:/// components. Otherwise use unum=%s.
87 if (strncmp(rawunum
, "hc://", 5) != 0)
88 prefix
= FM_FMRI_MEM_UNUM
"=";
93 * If we have a DIMM offset, include it in the string. If we have a PA
94 * then use that. Otherwise just format the unum element.
96 if (nvlist_lookup_uint64(nvl
, FM_FMRI_MEM_OFFSET
, &val
) == 0) {
97 (void) snprintf(format
, sizeof (format
),
98 "%s:///%s%%1$s/%s=%%2$llx",
99 FM_FMRI_SCHEME_MEM
, prefix
, FM_FMRI_MEM_OFFSET
);
100 } else if (nvlist_lookup_uint64(nvl
, FM_FMRI_MEM_PHYSADDR
, &val
) == 0) {
101 (void) snprintf(format
, sizeof (format
),
102 "%s:///%s%%1$s/%s=%%2$llx",
103 FM_FMRI_SCHEME_MEM
, prefix
, FM_FMRI_MEM_PHYSADDR
);
105 (void) snprintf(format
, sizeof (format
),
106 "%s:///%s%%1$s", FM_FMRI_SCHEME_MEM
, prefix
);
110 * If we have a well-formed unum (hc-FMRI), we skip over the
111 * the scheme and authority prefix.
112 * Otherwise, the spaces and colons will be escaped,
113 * rendering the resulting FMRI pretty much unreadable.
114 * We're therefore going to do some escaping of our own first.
116 if (strncmp(rawunum
, "hc://", 5) == 0) {
118 rawunum
= strchr(rawunum
, '/');
120 /* LINTED: variable format specifier */
121 size
= snprintf(buf
, buflen
, format
, rawunum
, val
);
123 preunum
= fmd_fmri_strdup(rawunum
);
124 presz
= strlen(preunum
) + 1;
126 for (i
= 0; i
< presz
- 1; i
++) {
127 if (preunum
[i
] == ':' && preunum
[i
+ 1] == ' ') {
128 bcopy(preunum
+ i
+ 2, preunum
+ i
+ 1,
130 } else if (preunum
[i
] == ' ') {
135 escunum
= fmd_fmri_strescape(preunum
);
136 fmd_fmri_free(preunum
, presz
);
138 /* LINTED: variable format specifier */
139 size
= snprintf(buf
, buflen
, format
, escunum
, val
);
140 fmd_fmri_strfree(escunum
);
147 fmd_fmri_expand(nvlist_t
*nvl
)
149 char *unum
, **serids
;
155 if ((mem_fmri_get_unum(nvl
, &unum
) < 0) || (*unum
== '\0'))
156 return (fmd_fmri_set_errno(EINVAL
));
159 * If the mem-scheme topology exports this method expand(), invoke it.
161 if ((thp
= fmd_fmri_topo_hold(TOPO_VERSION
)) == NULL
)
162 return (fmd_fmri_set_errno(EINVAL
));
163 rc
= topo_fmri_expand(thp
, nvl
, &err
);
164 fmd_fmri_topo_rele(thp
);
165 if (err
!= ETOPO_METHOD_NOTSUP
)
168 if ((rc
= nvlist_lookup_string_array(nvl
, FM_FMRI_MEM_SERIAL_ID
,
169 &serids
, &nnvlserids
)) == 0) { /* already have serial #s */
171 } else if (rc
!= ENOENT
)
172 return (fmd_fmri_set_errno(EINVAL
));
174 if (mem_get_serids_by_unum(unum
, &serids
, &nserids
) < 0) {
175 /* errno is set for us */
176 if (errno
== ENOTSUP
)
177 return (0); /* nothing to add - no s/n support */
182 rc
= nvlist_add_string_array(nvl
, FM_FMRI_MEM_SERIAL_ID
, serids
,
185 mem_strarray_free(serids
, nserids
);
188 return (fmd_fmri_set_errno(EINVAL
));
195 serids_eq(char **serids1
, uint_t nserids1
, char **serids2
, uint_t nserids2
)
199 if (nserids1
!= nserids2
)
202 for (i
= 0; i
< nserids1
; i
++) {
203 if (strcmp(serids1
[i
], serids2
[i
]) != 0)
212 fmd_fmri_present(nvlist_t
*nvl
)
216 struct topo_hdl
*thp
;
218 char **nvlserids
, **serids
;
223 nvlist_t
*nvlcp
= NULL
;
227 if (mem_fmri_get_unum(nvl
, &unum
) < 0)
228 return (-1); /* errno is set for us */
232 * If the mem-scheme topology exports this method present(), invoke it.
234 if ((thp
= fmd_fmri_topo_hold(TOPO_VERSION
)) == NULL
)
235 return (fmd_fmri_set_errno(EINVAL
));
236 rc
= topo_fmri_present(thp
, nvl
, &err
);
237 fmd_fmri_topo_rele(thp
);
238 if (err
!= ETOPO_METHOD_NOTSUP
)
241 if (nvlist_lookup_string_array(nvl
, FM_FMRI_MEM_SERIAL_ID
, &nvlserids
,
244 * Some mem scheme FMRIs don't have serial ids because
245 * either the platform does not support them, or because
246 * the FMRI was created before support for serial ids was
247 * introduced. If this is the case, assume it is there.
249 if (mem
.mem_dm
== NULL
)
252 return (fmd_fmri_set_errno(EINVAL
));
255 if (mem_get_serids_by_unum(unum
, &serids
, &nserids
) < 0) {
256 if (errno
== ENOTSUP
)
257 return (1); /* assume it's there, no s/n support here */
258 if (errno
!= ENOENT
) {
260 * Errors are only signalled to the caller if they're
261 * the caller's fault. This isn't - it's a failure on
262 * our part to burst or read the serial numbers. We'll
263 * whine about it, and tell the caller the named
264 * module(s) isn't/aren't there.
266 fmd_fmri_warn("failed to retrieve serial number for "
272 rc
= serids_eq(serids
, nserids
, nvlserids
, nnvlserids
);
274 mem_strarray_free(serids
, nserids
);
277 * On X86 we will invoke the topo is_present method passing in the
278 * unum, which is in hc scheme. The libtopo hc-scheme is_present method
279 * will invoke the node-specific is_present method, which is implemented
280 * by the chip enumerator for rank nodes. The rank node's is_present
281 * method will compare the serial number in the unum with the current
282 * serial to determine if the same DIMM is present.
284 if ((thp
= fmd_fmri_topo_hold(TOPO_VERSION
)) == NULL
) {
285 fmd_fmri_warn("failed to get handle to topology");
288 if (topo_fmri_str2nvl(thp
, unum
, &unum_nvl
, &err
) == 0) {
289 rc
= topo_fmri_present(thp
, unum_nvl
, &err
);
290 nvlist_free(unum_nvl
);
292 rc
= fmd_fmri_set_errno(EINVAL
);
293 fmd_fmri_topo_rele(thp
);
296 * Need to check if this is a valid page too. if "isretired" returns
297 * EINVAL, assume page invalid and return not_present.
299 if (rc
== 1 && nvlist_lookup_uint64(nvl
, FM_FMRI_MEM_OFFSET
, &val
) ==
300 0 && nvlist_lookup_uint64(nvl
, FM_FMRI_MEM_PHYSADDR
, &val
) == 0 &&
301 mem_unum_rewrite(nvl
, &nvlcp
) == 0 && nvlcp
!= NULL
) {
302 int page_err
, rval
= page_isretired(nvlcp
, &page_err
);
303 if (rval
== FMD_AGENT_RETIRE_DONE
&& page_err
== EINVAL
)
312 fmd_fmri_replaced(nvlist_t
*nvl
)
316 struct topo_hdl
*thp
;
318 char **nvlserids
, **serids
;
323 nvlist_t
*nvlcp
= NULL
;
327 if (mem_fmri_get_unum(nvl
, &unum
) < 0)
328 return (-1); /* errno is set for us */
332 * If the mem-scheme topology exports this method replaced(), invoke it.
334 if ((thp
= fmd_fmri_topo_hold(TOPO_VERSION
)) == NULL
)
335 return (fmd_fmri_set_errno(EINVAL
));
336 rc
= topo_fmri_replaced(thp
, nvl
, &err
);
337 fmd_fmri_topo_rele(thp
);
338 if (err
!= ETOPO_METHOD_NOTSUP
)
341 if (nvlist_lookup_string_array(nvl
, FM_FMRI_MEM_SERIAL_ID
, &nvlserids
,
344 * Some mem scheme FMRIs don't have serial ids because
345 * either the platform does not support them, or because
346 * the FMRI was created before support for serial ids was
347 * introduced. If this is the case, assume it is there.
349 if (mem
.mem_dm
== NULL
)
350 return (FMD_OBJ_STATE_UNKNOWN
);
352 return (fmd_fmri_set_errno(EINVAL
));
355 if (mem_get_serids_by_unum(unum
, &serids
, &nserids
) < 0) {
356 if (errno
== ENOTSUP
)
357 return (FMD_OBJ_STATE_UNKNOWN
);
358 if (errno
!= ENOENT
) {
360 * Errors are only signalled to the caller if they're
361 * the caller's fault. This isn't - it's a failure on
362 * our part to burst or read the serial numbers. We'll
363 * whine about it, and tell the caller the named
364 * module(s) isn't/aren't there.
366 fmd_fmri_warn("failed to retrieve serial number for "
369 return (FMD_OBJ_STATE_NOT_PRESENT
);
372 rc
= serids_eq(serids
, nserids
, nvlserids
, nnvlserids
) ?
373 FMD_OBJ_STATE_STILL_PRESENT
: FMD_OBJ_STATE_REPLACED
;
375 mem_strarray_free(serids
, nserids
);
378 * On X86 we will invoke the topo is_replaced method passing in the
379 * unum, which is in hc scheme. The libtopo hc-scheme is_replaced
380 * method will invoke the node-specific is_replaced method, which is
381 * implemented by the chip enumerator for rank nodes. The rank node's
382 * is_replaced method will compare the serial number in the unum with
383 * the current serial to determine if the same DIMM is replaced.
385 if ((thp
= fmd_fmri_topo_hold(TOPO_VERSION
)) == NULL
) {
386 fmd_fmri_warn("failed to get handle to topology");
389 if (topo_fmri_str2nvl(thp
, unum
, &unum_nvl
, &err
) == 0) {
390 rc
= topo_fmri_replaced(thp
, unum_nvl
, &err
);
391 nvlist_free(unum_nvl
);
393 rc
= fmd_fmri_set_errno(EINVAL
);
394 fmd_fmri_topo_rele(thp
);
397 * Need to check if this is a valid page too. if "isretired" returns
398 * EINVAL, assume page invalid and return not_present.
400 if ((rc
== FMD_OBJ_STATE_STILL_PRESENT
||
401 rc
== FMD_OBJ_STATE_UNKNOWN
) &&
402 nvlist_lookup_uint64(nvl
, FM_FMRI_MEM_OFFSET
, &val
) == 0 &&
403 nvlist_lookup_uint64(nvl
, FM_FMRI_MEM_PHYSADDR
, &val
) == 0 &&
404 mem_unum_rewrite(nvl
, &nvlcp
) == 0 && nvlcp
!= NULL
) {
405 int page_err
, rval
= page_isretired(nvlcp
, &page_err
);
406 if (rval
== FMD_AGENT_RETIRE_DONE
&& page_err
== EINVAL
)
407 rc
= FMD_OBJ_STATE_NOT_PRESENT
;
415 fmd_fmri_contains(nvlist_t
*er
, nvlist_t
*ee
)
418 struct topo_hdl
*thp
;
419 char *erunum
, *eeunum
;
420 uint64_t erval
= 0, eeval
= 0;
423 * If the mem-scheme topology exports this method contains(), invoke it.
425 if ((thp
= fmd_fmri_topo_hold(TOPO_VERSION
)) == NULL
)
426 return (fmd_fmri_set_errno(EINVAL
));
427 rc
= topo_fmri_contains(thp
, er
, ee
, &err
);
428 fmd_fmri_topo_rele(thp
);
429 if (err
!= ETOPO_METHOD_NOTSUP
)
432 if (mem_fmri_get_unum(er
, &erunum
) < 0 ||
433 mem_fmri_get_unum(ee
, &eeunum
) < 0)
434 return (-1); /* errno is set for us */
436 if (mem_unum_contains(erunum
, eeunum
) <= 0)
437 return (0); /* can't parse/match, so assume no containment */
439 if (nvlist_lookup_uint64(er
, FM_FMRI_MEM_OFFSET
, &erval
) == 0) {
440 return (nvlist_lookup_uint64(ee
,
441 FM_FMRI_MEM_OFFSET
, &eeval
) == 0 && erval
== eeval
);
444 if (nvlist_lookup_uint64(er
, FM_FMRI_MEM_PHYSADDR
, &erval
) == 0) {
445 return (nvlist_lookup_uint64(ee
,
446 FM_FMRI_MEM_PHYSADDR
, &eeval
) == 0 && erval
== eeval
);
453 * We can only make a usable/unusable determination for pages. Mem FMRIs
454 * without page addresses will be reported as usable since Solaris has no
455 * way at present to dynamically disable an entire DIMM or DIMM pair.
458 fmd_fmri_unusable(nvlist_t
*nvl
)
462 int rc
, err1
= 0, err2
;
463 nvlist_t
*nvlcp
= NULL
;
467 if (nvlist_lookup_uint8(nvl
, FM_VERSION
, &version
) != 0 ||
468 version
> FM_MEM_SCHEME_VERSION
)
469 return (fmd_fmri_set_errno(EINVAL
));
472 * If the mem-scheme topology exports this method unusable(), invoke it.
474 if ((thp
= fmd_fmri_topo_hold(TOPO_VERSION
)) == NULL
)
475 return (fmd_fmri_set_errno(EINVAL
));
476 rc
= topo_fmri_unusable(thp
, nvl
, &err1
);
477 fmd_fmri_topo_rele(thp
);
478 if (err1
!= ETOPO_METHOD_NOTSUP
)
481 err1
= nvlist_lookup_uint64(nvl
, FM_FMRI_MEM_OFFSET
, &val1
);
482 err2
= nvlist_lookup_uint64(nvl
, FM_FMRI_MEM_PHYSADDR
, &val2
);
484 if (err1
== ENOENT
&& err2
== ENOENT
)
485 return (0); /* no page, so assume it's still usable */
487 if ((err1
!= 0 && err1
!= ENOENT
) || (err2
!= 0 && err2
!= ENOENT
))
488 return (fmd_fmri_set_errno(EINVAL
));
490 if ((rc
= mem_unum_rewrite(nvl
, &nvlcp
)) != 0)
491 return (fmd_fmri_set_errno(rc
));
494 * Ask the kernel if the page is retired, using either the rewritten
495 * hc FMRI or the original mem FMRI with the specified offset or PA.
496 * Refer to the kernel's page_retire_check() for the error codes.
498 rc
= page_isretired(nvlcp
? nvlcp
: nvl
, NULL
);
500 if (rc
== FMD_AGENT_RETIRE_FAIL
) {
502 * The page is not retired and is not scheduled for retirement
503 * (i.e. no request pending and has not seen any errors)
506 } else if (rc
== FMD_AGENT_RETIRE_DONE
||
507 rc
== FMD_AGENT_RETIRE_ASYNC
) {
509 * The page has been retired, is in the process of being
510 * retired, or doesn't exist. The latter is valid if the page
511 * existed in the past but has been DR'd out.
516 * Errors are only signalled to the caller if they're the
517 * caller's fault. This isn't - it's a failure of the
518 * retirement-check code. We'll whine about it and tell
519 * the caller the page is unusable.
521 fmd_fmri_warn("failed to determine page %s=%llx usability: "
522 "rc=%d errno=%d\n", err1
== 0 ? FM_FMRI_MEM_OFFSET
:
523 FM_FMRI_MEM_PHYSADDR
, err1
== 0 ? (u_longlong_t
)val1
:
524 (u_longlong_t
)val2
, rc
, errno
);
536 return (mem_discover());
542 mem_dimm_map_t
*dm
, *em
;
543 mem_bank_map_t
*bm
, *cm
;
545 mem_seg_map_t
*sm
, *tm
;
547 for (dm
= mem
.mem_dm
; dm
!= NULL
; dm
= em
) {
549 fmd_fmri_strfree(dm
->dm_label
);
550 fmd_fmri_strfree(dm
->dm_part
);
551 fmd_fmri_strfree(dm
->dm_device
);
552 fmd_fmri_free(dm
, sizeof (mem_dimm_map_t
));
554 for (bm
= mem
.mem_bank
; bm
!= NULL
; bm
= cm
) {
556 fmd_fmri_free(bm
, sizeof (mem_bank_map_t
));
558 for (gm
= mem
.mem_group
; gm
!= NULL
; gm
= hm
) {
560 fmd_fmri_free(gm
, sizeof (mem_grp_t
));
562 for (sm
= mem
.mem_seg
; sm
!= NULL
; sm
= tm
) {
564 fmd_fmri_free(sm
, sizeof (mem_seg_map_t
));