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 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "cfga_sata.h"
33 #define MAX_FORMAT 80 /* for info table */
35 cfga_sata_ret_t
sata_rcm_offline(const char *, char **, char *,
37 cfga_sata_ret_t
sata_rcm_online(const char *, char **, char *,
39 cfga_sata_ret_t
sata_rcm_remove(const char *, char **, char *,
41 static cfga_sata_ret_t
sata_rcm_info_table(rcm_info_t
*, char **);
42 static cfga_sata_ret_t
sata_rcm_init(const char *, cfga_flags_t
, char **,
46 static rcm_handle_t
*rcm_handle
= NULL
;
47 static mutex_t rcm_handle_lock
= DEFAULTMUTEX
;
51 * Offline SATA resource consumers.
54 sata_rcm_offline(const char *rsrc
, char **errstring
, char *rsrc_fixed
,
59 rcm_info_t
*rinfo
= NULL
;
60 cfga_sata_ret_t ret
= CFGA_SATA_OK
;
62 if ((ret
= sata_rcm_init(rsrc
, flags
, errstring
, &rflags
)) !=
68 if ((rret
= rcm_request_offline(rcm_handle
, rsrc_fixed
, rflags
,
69 &rinfo
)) != RCM_SUCCESS
) {
71 (void) sata_rcm_info_table(rinfo
, errstring
);
76 if (rret
== RCM_FAILURE
) {
77 (void) sata_rcm_online(rsrc
, errstring
,
80 ret
= CFGA_SATA_RCM_OFFLINE
;
88 * Online SATA resource consumers that were previously offlined.
91 sata_rcm_online(const char *rsrc
, char **errstring
, char *rsrc_fixed
,
94 rcm_info_t
*rinfo
= NULL
;
95 cfga_sata_ret_t ret
= CFGA_SATA_OK
;
97 if ((ret
= sata_rcm_init(rsrc
, flags
, errstring
, NULL
)) !=
103 if (rcm_notify_online(rcm_handle
, rsrc_fixed
, 0, &rinfo
) !=
104 RCM_SUCCESS
&& (rinfo
!= NULL
)) {
105 (void) sata_rcm_info_table(rinfo
, errstring
);
106 rcm_free_info(rinfo
);
108 ret
= CFGA_SATA_RCM_ONLINE
;
116 * Remove SATA resource consumers after their kernel removal.
119 sata_rcm_remove(const char *rsrc
, char **errstring
, char *rsrc_fixed
,
122 rcm_info_t
*rinfo
= NULL
;
123 cfga_sata_ret_t ret
= CFGA_SATA_OK
;
125 if ((ret
= sata_rcm_init(rsrc
, flags
, errstring
, NULL
)) !=
131 if (rcm_notify_remove(rcm_handle
, rsrc_fixed
, 0, &rinfo
) !=
132 RCM_SUCCESS
&& (rinfo
!= NULL
)) {
134 (void) sata_rcm_info_table(rinfo
, errstring
);
135 rcm_free_info(rinfo
);
137 ret
= CFGA_SATA_RCM_ONLINE
;
146 * Contains common initialization code for entering a sata_rcm_xx() routine.
149 static cfga_sata_ret_t
150 sata_rcm_init(const char *rsrc
, cfga_flags_t flags
, char **errstring
,
153 /* Validate the rsrc argument */
155 return (CFGA_SATA_INTERNAL_ERROR
);
158 /* Translate the cfgadm flags to RCM flags */
159 if (rflags
&& (flags
& CFGA_FLAG_FORCE
)) {
160 *rflags
|= RCM_FORCE
;
163 /* Get a handle for the RCM operations */
164 (void) mutex_lock(&rcm_handle_lock
);
165 if (rcm_handle
== NULL
) {
166 if (rcm_alloc_handle(NULL
, RCM_NOPID
, NULL
, &rcm_handle
) !=
168 (void) mutex_unlock(&rcm_handle_lock
);
170 return (CFGA_SATA_RCM_HANDLE
);
173 (void) mutex_unlock(&rcm_handle_lock
);
175 return (CFGA_SATA_OK
);
180 * sata_rcm_info_table:
181 * Takes an opaque rcm_info_t pointer and a character pointer,
182 * and appends the rcm_info_t data in the form of a table to the
183 * given character pointer.
185 static cfga_sata_ret_t
186 sata_rcm_info_table(rcm_info_t
*rinfo
, char **table
)
193 size_t table_size
= 0;
195 rcm_info_tuple_t
*tuple
= NULL
;
199 static char format
[MAX_FORMAT
];
202 /* Protect against invalid arguments */
203 if (rinfo
== NULL
|| table
== NULL
) {
204 return (CFGA_SATA_INTERNAL_ERROR
);
207 /* Set localized table header strings */
208 rsrc
= dgettext(TEXT_DOMAIN
, "Resource");
209 info
= dgettext(TEXT_DOMAIN
, "Information");
212 /* A first pass, to size up the RCM information */
213 while (tuple
= rcm_info_next(rinfo
, tuple
)) {
214 if ((infostr
= rcm_info_info(tuple
)) != NULL
) {
216 if ((w
= strlen(rcm_info_rsrc(tuple
))) > w_rsrc
)
218 if ((w
= strlen(infostr
)) > w_info
)
223 /* If nothing was sized up above, stop early */
225 return (CFGA_SATA_OK
);
228 /* Adjust column widths for column headings */
229 if ((w
= strlen(rsrc
)) > w_rsrc
) {
231 } else if ((w_rsrc
- w
) % 2) {
235 if ((w
= strlen(info
)) > w_info
) {
237 } else if ((w_info
- w
) % 2) {
243 * Compute the total line width of each line,
244 * accounting for intercolumn spacing.
246 width
= w_info
+ w_rsrc
+ 4;
248 /* Allocate space for the table */
249 table_size
= (2 + tuples
) * (width
+ 1) + 2;
250 if (*table
== NULL
) {
251 /* zero fill for the strcat() call below */
252 *table
= calloc(table_size
, sizeof (char));
253 if (*table
== NULL
) {
254 return (CFGA_SATA_ALLOC_FAIL
);
257 newtable
= realloc(*table
, strlen(*table
) + table_size
);
258 if (newtable
== NULL
) {
259 return (CFGA_SATA_ALLOC_FAIL
);
265 /* Place a table header into the string */
268 /* The resource header */
269 (void) strcat(*table
, "\n");
272 for (i
= 0; i
< ((w_rsrc
- w
) / 2); i
++) {
273 (void) strcat(*table
, " ");
275 (void) strcat(*table
, rsrc
);
277 for (i
= 0; i
< ((w_rsrc
- w
) / 2); i
++) {
278 (void) strcat(*table
, " ");
281 /* The information header */
282 (void) strcat(*table
, " ");
284 for (i
= 0; i
< ((w_info
- w
) / 2); i
++) {
285 (void) strcat(*table
, " ");
287 (void) strcat(*table
, info
);
289 for (i
= 0; i
< ((w_info
- w
) / 2); i
++) {
290 (void) strcat(*table
, " ");
293 (void) strcat(*table
, "\n");
295 /* Underline the headers */
296 for (i
= 0; i
< w_rsrc
; i
++) {
297 (void) strcat(*table
, "-");
300 (void) strcat(*table
, " ");
301 for (i
= 0; i
< w_info
; i
++) {
302 (void) strcat(*table
, "-");
306 (void) strcat(*table
, "\n");
308 /* Construct the format string */
309 (void) snprintf(format
, MAX_FORMAT
, "%%-%ds %%-%ds",
310 (int)w_rsrc
, (int)w_info
);
312 /* Add the tuples to the table string */
314 while ((tuple
= rcm_info_next(rinfo
, tuple
)) != NULL
) {
315 if ((infostr
= rcm_info_info(tuple
)) != NULL
) {
316 (void) sprintf(&((*table
)[strlen(*table
)]),
317 format
, rcm_info_rsrc(tuple
), infostr
);
318 (void) strcat(*table
, "\n");
322 return (CFGA_SATA_OK
);