xref: /illumos-gate/usr/src/cmd/fm/schemes/mem/mem_util.c (revision 60a3f738d56f92ae8b80e4b62a2331c6e1f2311f)
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 	if (rc > -1) {
68 		return (0);
69 	} else {
70 		errno = EIO;
71 		return (-1);
72 	}
73 #else
74 	mem_page_t mpage;
75 	char *fmribuf;
76 	size_t fmrisz;
77 	int fd, rc, err;
78 
79 	if ((fd = open("/dev/mem", O_RDONLY)) < 0)
80 		return (-1); /* errno is set for us */
81 
82 	if ((errno = nvlist_size(nvl, &fmrisz, NV_ENCODE_NATIVE)) != 0 ||
83 	    fmrisz > MEM_FMRI_MAX_BUFSIZE ||
84 	    (fmribuf = fmd_fmri_alloc(fmrisz)) == NULL) {
85 		(void) close(fd);
86 		return (-1); /* errno is set for us */
87 	}
88 
89 	if ((errno = nvlist_pack(nvl, &fmribuf, &fmrisz,
90 	    NV_ENCODE_NATIVE, 0)) != 0) {
91 		fmd_fmri_free(fmribuf, fmrisz);
92 		(void) close(fd);
93 		return (-1); /* errno is set for us */
94 	}
95 
96 	mpage.m_fmri = fmribuf;
97 	mpage.m_fmrisz = fmrisz;
98 
99 	if ((rc = ioctl(fd, cmd, &mpage)) < 0)
100 		err = errno;
101 
102 	fmd_fmri_free(fmribuf, fmrisz);
103 
104 	(void) close(fd);
105 
106 	if (rc < 0) {
107 		errno = err;
108 		return (-1);
109 	}
110 
111 	return (0);
112 #endif
113 }
114