xref: /titanic_52/usr/src/cmd/fm/schemes/mem/mem_util.c (revision 2a9459bdd821c1cf59590a7a9069ac9c591e8a6b)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <mem.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/mem.h>
33 #include <fm/fmd_fmri.h>
34 
35 #ifdef sparc
36 #include <sys/fm/ldom.h>
37 extern ldom_hdl_t *mem_scheme_lhp;
38 #endif
39 
40 void
41 mem_strarray_free(char **arr, size_t dim)
42 {
43 	int i;
44 
45 	for (i = 0; i < dim; i++) {
46 		if (arr[i] != NULL)
47 			fmd_fmri_strfree(arr[i]);
48 	}
49 	fmd_fmri_free(arr, sizeof (char *) * dim);
50 }
51 
52 int
53 mem_page_cmd(int cmd, nvlist_t *nvl)
54 {
55 #ifdef sparc
56 	int rc;
57 
58 	if (cmd == MEM_PAGE_ISRETIRED || cmd == MEM_PAGE_FMRI_ISRETIRED) {
59 		rc = ldom_fmri_status(mem_scheme_lhp, nvl);
60 	} else if (cmd == MEM_PAGE_RETIRE || cmd == MEM_PAGE_FMRI_RETIRE) {
61 		rc = ldom_fmri_retire(mem_scheme_lhp, nvl);
62 	} else {
63 		errno = ENOTSUP;
64 		return (-1);
65 	}
66 
67 	/* Make the return value and errno value similar to the ioctl() call */
68 	if (rc > 0) {
69 		errno = rc;
70 		return (-1);
71 	}
72 
73 	return (0);
74 #else
75 	mem_page_t mpage;
76 	char *fmribuf;
77 	size_t fmrisz;
78 	int fd, rc, err;
79 
80 	if ((fd = open("/dev/mem", O_RDONLY)) < 0)
81 		return (-1); /* errno is set for us */
82 
83 	if ((errno = nvlist_size(nvl, &fmrisz, NV_ENCODE_NATIVE)) != 0 ||
84 	    fmrisz > MEM_FMRI_MAX_BUFSIZE ||
85 	    (fmribuf = fmd_fmri_alloc(fmrisz)) == NULL) {
86 		(void) close(fd);
87 		return (-1); /* errno is set for us */
88 	}
89 
90 	if ((errno = nvlist_pack(nvl, &fmribuf, &fmrisz,
91 	    NV_ENCODE_NATIVE, 0)) != 0) {
92 		fmd_fmri_free(fmribuf, fmrisz);
93 		(void) close(fd);
94 		return (-1); /* errno is set for us */
95 	}
96 
97 	mpage.m_fmri = fmribuf;
98 	mpage.m_fmrisz = fmrisz;
99 
100 	if ((rc = ioctl(fd, cmd, &mpage)) < 0)
101 		err = errno;
102 
103 	fmd_fmri_free(fmribuf, fmrisz);
104 
105 	(void) close(fd);
106 
107 	if (rc < 0) {
108 		errno = err;
109 		return (-1);
110 	}
111 
112 	return (0);
113 #endif
114 }
115