1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include "cfga_ib.h"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #define	MAX_FORMAT	80	/* for info table */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate cfga_ib_ret_t		ib_rcm_offline(const char *, char **, char *,
33*7c478bd9Sstevel@tonic-gate 			    cfga_flags_t);
34*7c478bd9Sstevel@tonic-gate cfga_ib_ret_t		ib_rcm_online(const char *, char **, char *,
35*7c478bd9Sstevel@tonic-gate 			    cfga_flags_t);
36*7c478bd9Sstevel@tonic-gate cfga_ib_ret_t		ib_rcm_remove(const char *, char **, char *,
37*7c478bd9Sstevel@tonic-gate 			    cfga_flags_t);
38*7c478bd9Sstevel@tonic-gate static cfga_ib_ret_t 	ib_rcm_info_table(rcm_info_t *, char **);
39*7c478bd9Sstevel@tonic-gate static cfga_ib_ret_t 	ib_rcm_init(const char *, cfga_flags_t, char **,
40*7c478bd9Sstevel@tonic-gate 			    uint_t *);
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static rcm_handle_t *rcm_handle = NULL;
44*7c478bd9Sstevel@tonic-gate static mutex_t rcm_handle_lock = DEFAULTMUTEX;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * Function:
49*7c478bd9Sstevel@tonic-gate  *	ib_rcm_offline
50*7c478bd9Sstevel@tonic-gate  * Input:
51*7c478bd9Sstevel@tonic-gate  *	rsrc		- Resource name (typically an ap_id)
52*7c478bd9Sstevel@tonic-gate  *	errstring	- Error message filled in case of a failure
53*7c478bd9Sstevel@tonic-gate  *	rsrc_fixed	- Pointer to fixed path
54*7c478bd9Sstevel@tonic-gate  *	flags		- flags to RCM
55*7c478bd9Sstevel@tonic-gate  * Output:
56*7c478bd9Sstevel@tonic-gate  *	NONE
57*7c478bd9Sstevel@tonic-gate  * Returns:
58*7c478bd9Sstevel@tonic-gate  *	CFGA_IB_OK on success or an appropriate error
59*7c478bd9Sstevel@tonic-gate  * Description:
60*7c478bd9Sstevel@tonic-gate  *	Offline IB resource consumers.
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate cfga_ib_ret_t
ib_rcm_offline(const char * rsrc,char ** errstring,char * rsrc_fixed,cfga_flags_t flags)63*7c478bd9Sstevel@tonic-gate ib_rcm_offline(const char *rsrc, char **errstring, char *rsrc_fixed,
64*7c478bd9Sstevel@tonic-gate     cfga_flags_t flags)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	int		rret;
67*7c478bd9Sstevel@tonic-gate 	uint_t		rflags = 0;
68*7c478bd9Sstevel@tonic-gate 	rcm_info_t	*rinfo = NULL;
69*7c478bd9Sstevel@tonic-gate 	cfga_ib_ret_t	ret = CFGA_IB_OK;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	DPRINTF("ib_rcm_offline:\n");
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if ((ret = ib_rcm_init(rsrc, flags, errstring, &rflags)) !=
74*7c478bd9Sstevel@tonic-gate 	    CFGA_IB_OK) {
75*7c478bd9Sstevel@tonic-gate 		return (ret);
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	DPRINTF("ib_rcm_offline: rsrc_fixed: %s\n", rsrc_fixed);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if ((rret = rcm_request_offline(rcm_handle, rsrc_fixed, rflags, &rinfo))
81*7c478bd9Sstevel@tonic-gate 	    != RCM_SUCCESS) {
82*7c478bd9Sstevel@tonic-gate 		DPRINTF("ib_rcm_offline: rcm_request_offline failed\n");
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 		if (rinfo) {
85*7c478bd9Sstevel@tonic-gate 			(void) ib_rcm_info_table(rinfo, errstring);
86*7c478bd9Sstevel@tonic-gate 			rcm_free_info(rinfo);
87*7c478bd9Sstevel@tonic-gate 			rinfo = NULL;
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		DPRINTF("ib_rcm_offline: table = %s\n", *errstring);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		if (rret == RCM_FAILURE) {
93*7c478bd9Sstevel@tonic-gate 			(void) ib_rcm_online(rsrc, errstring,
94*7c478bd9Sstevel@tonic-gate 			    rsrc_fixed, flags);
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 		ret = CFGA_IB_RCM_OFFLINE_ERR;
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	return (ret);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate /*
104*7c478bd9Sstevel@tonic-gate  * Function:
105*7c478bd9Sstevel@tonic-gate  *	ib_rcm_online
106*7c478bd9Sstevel@tonic-gate  * Input:
107*7c478bd9Sstevel@tonic-gate  *	rsrc		- Resource name (typically an ap_id)
108*7c478bd9Sstevel@tonic-gate  *	errstring	- Error message filled in case of a failure
109*7c478bd9Sstevel@tonic-gate  *	rsrc_fixed	- Pointer to fixed path
110*7c478bd9Sstevel@tonic-gate  *	flags		- flags to RCM
111*7c478bd9Sstevel@tonic-gate  * Output:
112*7c478bd9Sstevel@tonic-gate  *	NONE
113*7c478bd9Sstevel@tonic-gate  * Returns:
114*7c478bd9Sstevel@tonic-gate  *	CFGA_IB_OK on success or an appropriate error
115*7c478bd9Sstevel@tonic-gate  * Description:
116*7c478bd9Sstevel@tonic-gate  *	Online IB resource consumers that were previously offlined.
117*7c478bd9Sstevel@tonic-gate  */
118*7c478bd9Sstevel@tonic-gate cfga_ib_ret_t
ib_rcm_online(const char * rsrc,char ** errstring,char * rsrc_fixed,cfga_flags_t flags)119*7c478bd9Sstevel@tonic-gate ib_rcm_online(const char *rsrc, char **errstring, char *rsrc_fixed,
120*7c478bd9Sstevel@tonic-gate     cfga_flags_t flags)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	rcm_info_t	*rinfo = NULL;
123*7c478bd9Sstevel@tonic-gate 	cfga_ib_ret_t	ret = CFGA_IB_OK;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	DPRINTF("ib_rcm_online:\n");
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if ((ret = ib_rcm_init(rsrc, flags, errstring, NULL)) != CFGA_IB_OK) {
128*7c478bd9Sstevel@tonic-gate 		return (ret);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	if (rcm_notify_online(rcm_handle, rsrc_fixed, 0, &rinfo) !=
132*7c478bd9Sstevel@tonic-gate 	    RCM_SUCCESS && (rinfo != NULL)) {
133*7c478bd9Sstevel@tonic-gate 		DPRINTF("ib_rcm_online: rcm_notify_online failed\n");
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 		(void) ib_rcm_info_table(rinfo, errstring);
136*7c478bd9Sstevel@tonic-gate 		rcm_free_info(rinfo);
137*7c478bd9Sstevel@tonic-gate 		rinfo = NULL;
138*7c478bd9Sstevel@tonic-gate 		ret = CFGA_IB_RCM_ONLINE_ERR;
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	return (ret);
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate  * Function:
147*7c478bd9Sstevel@tonic-gate  *	ib_rcm_remove
148*7c478bd9Sstevel@tonic-gate  * Input:
149*7c478bd9Sstevel@tonic-gate  *	rsrc		- Resource name (typically an ap_id)
150*7c478bd9Sstevel@tonic-gate  *	errstring	- Error message filled in case of a failure
151*7c478bd9Sstevel@tonic-gate  *	rsrc_fixed	- Pointer to fixed path
152*7c478bd9Sstevel@tonic-gate  *	flags		- flags to RCM
153*7c478bd9Sstevel@tonic-gate  * Output:
154*7c478bd9Sstevel@tonic-gate  *	NONE
155*7c478bd9Sstevel@tonic-gate  * Returns:
156*7c478bd9Sstevel@tonic-gate  *	CFGA_IB_OK on success or an appropriate error
157*7c478bd9Sstevel@tonic-gate  * Description:
158*7c478bd9Sstevel@tonic-gate  *	Remove IB resource consumers after their kernel removal.
159*7c478bd9Sstevel@tonic-gate  */
160*7c478bd9Sstevel@tonic-gate cfga_ib_ret_t
ib_rcm_remove(const char * rsrc,char ** errstring,char * rsrc_fixed,cfga_flags_t flags)161*7c478bd9Sstevel@tonic-gate ib_rcm_remove(const char *rsrc, char **errstring, char *rsrc_fixed,
162*7c478bd9Sstevel@tonic-gate     cfga_flags_t flags)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	rcm_info_t	*rinfo = NULL;
165*7c478bd9Sstevel@tonic-gate 	cfga_ib_ret_t	ret = CFGA_IB_OK;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	DPRINTF("ib_rcm_remove:\n");
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if ((ret = ib_rcm_init(rsrc, flags, errstring, NULL)) != CFGA_IB_OK) {
170*7c478bd9Sstevel@tonic-gate 		return (ret);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if (rcm_notify_remove(rcm_handle, rsrc_fixed, 0, &rinfo) !=
174*7c478bd9Sstevel@tonic-gate 	    RCM_SUCCESS && (rinfo != NULL)) {
175*7c478bd9Sstevel@tonic-gate 		DPRINTF("ib_rcm_remove: rcm_notify_remove failed\n");
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 		(void) ib_rcm_info_table(rinfo, errstring);
178*7c478bd9Sstevel@tonic-gate 		rcm_free_info(rinfo);
179*7c478bd9Sstevel@tonic-gate 		rinfo = NULL;
180*7c478bd9Sstevel@tonic-gate 		ret = CFGA_IB_RCM_ONLINE_ERR;
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	return (ret);
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate /*
188*7c478bd9Sstevel@tonic-gate  * Function:
189*7c478bd9Sstevel@tonic-gate  *	ib_rcm_init
190*7c478bd9Sstevel@tonic-gate  * Input:
191*7c478bd9Sstevel@tonic-gate  *	rsrc		- Resource name (typically an ap_id)
192*7c478bd9Sstevel@tonic-gate  *	flags		- flags to RCM
193*7c478bd9Sstevel@tonic-gate  *	errstring	- Error message filled in case of a failure
194*7c478bd9Sstevel@tonic-gate  *	rflags		- Flags filled up in case of a failure
195*7c478bd9Sstevel@tonic-gate  * Output:
196*7c478bd9Sstevel@tonic-gate  *	NONE
197*7c478bd9Sstevel@tonic-gate  * Returns:
198*7c478bd9Sstevel@tonic-gate  *	CFGA_IB_OK on success or an appropriate error
199*7c478bd9Sstevel@tonic-gate  * Description:
200*7c478bd9Sstevel@tonic-gate  *	Contains common initialization code for entering a ib_rcm_xx() routine.
201*7c478bd9Sstevel@tonic-gate  */
202*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
203*7c478bd9Sstevel@tonic-gate static cfga_ib_ret_t
ib_rcm_init(const char * rsrc,cfga_flags_t flags,char ** errstring,uint_t * rflags)204*7c478bd9Sstevel@tonic-gate ib_rcm_init(const char *rsrc, cfga_flags_t flags, char **errstring,
205*7c478bd9Sstevel@tonic-gate     uint_t *rflags)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	DPRINTF("ib_rcm_init:\n");
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	/* Validate the rsrc argument */
210*7c478bd9Sstevel@tonic-gate 	if (rsrc == NULL) {
211*7c478bd9Sstevel@tonic-gate 		DPRINTF("ib_rcm_init: rsrc is NULL\n");
212*7c478bd9Sstevel@tonic-gate 		return (CFGA_IB_INTERNAL_ERR);
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	/* Translate the cfgadm flags to RCM flags */
216*7c478bd9Sstevel@tonic-gate 	if (rflags && (flags & CFGA_FLAG_FORCE)) {
217*7c478bd9Sstevel@tonic-gate 		*rflags |= RCM_FORCE;
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	/* Get a handle for the RCM operations */
221*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_handle_lock);
222*7c478bd9Sstevel@tonic-gate 	if (rcm_handle == NULL) {
223*7c478bd9Sstevel@tonic-gate 		if (rcm_alloc_handle(NULL, RCM_NOPID, NULL, &rcm_handle) !=
224*7c478bd9Sstevel@tonic-gate 		    RCM_SUCCESS) {
225*7c478bd9Sstevel@tonic-gate 			DPRINTF("ib_rcm_init: alloc_handle failed\n");
226*7c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&rcm_handle_lock);
227*7c478bd9Sstevel@tonic-gate 			return (CFGA_IB_RCM_HANDLE_ERR);
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_handle_lock);
231*7c478bd9Sstevel@tonic-gate 	return (CFGA_IB_OK);
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate /*
236*7c478bd9Sstevel@tonic-gate  * Function:
237*7c478bd9Sstevel@tonic-gate  *	ib_rcm_info_table
238*7c478bd9Sstevel@tonic-gate  * Input:
239*7c478bd9Sstevel@tonic-gate  *	rinfo		- Resource information
240*7c478bd9Sstevel@tonic-gate  *	table		- table to be printed
241*7c478bd9Sstevel@tonic-gate  * Output:
242*7c478bd9Sstevel@tonic-gate  *	NONE
243*7c478bd9Sstevel@tonic-gate  * Returns:
244*7c478bd9Sstevel@tonic-gate  *	CFGA_IB_OK on success or an appropriate error
245*7c478bd9Sstevel@tonic-gate  * Description:
246*7c478bd9Sstevel@tonic-gate  *	Takes an opaque rcm_info_t pointer and a character pointer,
247*7c478bd9Sstevel@tonic-gate  *	and appends the rcm_info_t data in the form of a table to the
248*7c478bd9Sstevel@tonic-gate  *	given character pointer.
249*7c478bd9Sstevel@tonic-gate  */
250*7c478bd9Sstevel@tonic-gate static cfga_ib_ret_t
ib_rcm_info_table(rcm_info_t * rinfo,char ** table)251*7c478bd9Sstevel@tonic-gate ib_rcm_info_table(rcm_info_t *rinfo, char **table)
252*7c478bd9Sstevel@tonic-gate {
253*7c478bd9Sstevel@tonic-gate 	int i;
254*7c478bd9Sstevel@tonic-gate 	size_t w;
255*7c478bd9Sstevel@tonic-gate 	size_t width = 0;
256*7c478bd9Sstevel@tonic-gate 	size_t w_rsrc = 0;
257*7c478bd9Sstevel@tonic-gate 	size_t w_info = 0;
258*7c478bd9Sstevel@tonic-gate 	size_t table_size = 0;
259*7c478bd9Sstevel@tonic-gate 	uint_t tuples = 0;
260*7c478bd9Sstevel@tonic-gate 	rcm_info_tuple_t *tuple = NULL;
261*7c478bd9Sstevel@tonic-gate 	char *rsrc;
262*7c478bd9Sstevel@tonic-gate 	char *info;
263*7c478bd9Sstevel@tonic-gate 	char *newtable;
264*7c478bd9Sstevel@tonic-gate 	static char format[MAX_FORMAT];
265*7c478bd9Sstevel@tonic-gate 	const char *infostr;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	DPRINTF("ib_rcm_info_table:\n");
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	/* Protect against invalid arguments */
270*7c478bd9Sstevel@tonic-gate 	if (rinfo == NULL || table == NULL) {
271*7c478bd9Sstevel@tonic-gate 		return (CFGA_IB_INTERNAL_ERR);
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	/* Set localized table header strings */
275*7c478bd9Sstevel@tonic-gate 	rsrc = dgettext(TEXT_DOMAIN, "Resource");
276*7c478bd9Sstevel@tonic-gate 	info = dgettext(TEXT_DOMAIN, "Information");
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	/* A first pass, to size up the RCM information */
279*7c478bd9Sstevel@tonic-gate 	while (tuple = rcm_info_next(rinfo, tuple)) {
280*7c478bd9Sstevel@tonic-gate 		if ((infostr = rcm_info_info(tuple)) != NULL) {
281*7c478bd9Sstevel@tonic-gate 			tuples++;
282*7c478bd9Sstevel@tonic-gate 			if ((w = strlen(rcm_info_rsrc(tuple))) > w_rsrc)
283*7c478bd9Sstevel@tonic-gate 				w_rsrc = w;
284*7c478bd9Sstevel@tonic-gate 			if ((w = strlen(infostr)) > w_info)
285*7c478bd9Sstevel@tonic-gate 				w_info = w;
286*7c478bd9Sstevel@tonic-gate 		}
287*7c478bd9Sstevel@tonic-gate 	}
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	/* If nothing was sized up above, stop early */
290*7c478bd9Sstevel@tonic-gate 	if (tuples == 0) {
291*7c478bd9Sstevel@tonic-gate 		DPRINTF("ib_rcm_info_table: no tuples\n");
292*7c478bd9Sstevel@tonic-gate 		return (CFGA_IB_OK);
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	/* Adjust column widths for column headings */
296*7c478bd9Sstevel@tonic-gate 	if ((w = strlen(rsrc)) > w_rsrc) {
297*7c478bd9Sstevel@tonic-gate 		w_rsrc = w;
298*7c478bd9Sstevel@tonic-gate 	} else if ((w_rsrc - w) % 2) {
299*7c478bd9Sstevel@tonic-gate 		w_rsrc++;
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	if ((w = strlen(info)) > w_info) {
303*7c478bd9Sstevel@tonic-gate 		w_info = w;
304*7c478bd9Sstevel@tonic-gate 	} else if ((w_info - w) % 2) {
305*7c478bd9Sstevel@tonic-gate 		w_info++;
306*7c478bd9Sstevel@tonic-gate 	}
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	/*
309*7c478bd9Sstevel@tonic-gate 	 * Compute the total line width of each line,
310*7c478bd9Sstevel@tonic-gate 	 * accounting for intercolumn spacing.
311*7c478bd9Sstevel@tonic-gate 	 */
312*7c478bd9Sstevel@tonic-gate 	width = w_info + w_rsrc + 4;
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	/* Allocate space for the table */
315*7c478bd9Sstevel@tonic-gate 	table_size = (2 + tuples) * (width + 1) + 2;
316*7c478bd9Sstevel@tonic-gate 	if (*table == NULL) {
317*7c478bd9Sstevel@tonic-gate 		*table = malloc(table_size);
318*7c478bd9Sstevel@tonic-gate 	} else {
319*7c478bd9Sstevel@tonic-gate 		newtable = realloc(*table, strlen(*table) + table_size);
320*7c478bd9Sstevel@tonic-gate 		if (newtable != NULL)
321*7c478bd9Sstevel@tonic-gate 			*table = newtable;
322*7c478bd9Sstevel@tonic-gate 		else {
323*7c478bd9Sstevel@tonic-gate 			free(*table);
324*7c478bd9Sstevel@tonic-gate 			*table = NULL;
325*7c478bd9Sstevel@tonic-gate 			return (CFGA_IB_ALLOC_FAIL);
326*7c478bd9Sstevel@tonic-gate 		}
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	if (*table == NULL) {
330*7c478bd9Sstevel@tonic-gate 		DPRINTF("ib_rcm_info_table: no table\n");
331*7c478bd9Sstevel@tonic-gate 		return (CFGA_IB_ALLOC_FAIL);
332*7c478bd9Sstevel@tonic-gate 	}
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	/* Place a table header into the string */
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 	/* The resource header */
337*7c478bd9Sstevel@tonic-gate 	(void) strlcat(*table, "\n", sizeof (*table));
338*7c478bd9Sstevel@tonic-gate 	w = strlen(rsrc);
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ((w_rsrc - w) / 2); i++) {
341*7c478bd9Sstevel@tonic-gate 		(void) strcat(*table, " ");
342*7c478bd9Sstevel@tonic-gate 	}
343*7c478bd9Sstevel@tonic-gate 	(void) strlcat(*table, rsrc, sizeof (*table));
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ((w_rsrc - w) / 2); i++) {
346*7c478bd9Sstevel@tonic-gate 		(void) strcat(*table, " ");
347*7c478bd9Sstevel@tonic-gate 	}
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	/* The information header */
350*7c478bd9Sstevel@tonic-gate 	(void) strcat(*table, "  ");
351*7c478bd9Sstevel@tonic-gate 	w = strlen(info);
352*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ((w_info - w) / 2); i++) {
353*7c478bd9Sstevel@tonic-gate 		(void) strcat(*table, " ");
354*7c478bd9Sstevel@tonic-gate 	}
355*7c478bd9Sstevel@tonic-gate 	(void) strlcat(*table, info, sizeof (*table));
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ((w_info - w) / 2); i++) {
358*7c478bd9Sstevel@tonic-gate 		(void) strcat(*table, " ");
359*7c478bd9Sstevel@tonic-gate 	}
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	(void) strcat(*table, "\n");
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	/* Underline the headers */
364*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < w_rsrc; i++) {
365*7c478bd9Sstevel@tonic-gate 		(void) strcat(*table, "-");
366*7c478bd9Sstevel@tonic-gate 	}
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	(void) strcat(*table, "  ");
369*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < w_info; i++) {
370*7c478bd9Sstevel@tonic-gate 		(void) strcat(*table, "-");
371*7c478bd9Sstevel@tonic-gate 	}
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	(void) strcat(*table, "\n");
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	/* Construct the format string */
376*7c478bd9Sstevel@tonic-gate 	(void) snprintf(format, MAX_FORMAT, "%%-%ds  %%-%ds",
377*7c478bd9Sstevel@tonic-gate 	    (int)w_rsrc, (int)w_info);
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	/* Add the tuples to the table string */
380*7c478bd9Sstevel@tonic-gate 	tuple = NULL;
381*7c478bd9Sstevel@tonic-gate 	while ((tuple = rcm_info_next(rinfo, tuple)) != NULL) {
382*7c478bd9Sstevel@tonic-gate 		if ((infostr = rcm_info_info(tuple)) != NULL) {
383*7c478bd9Sstevel@tonic-gate 			(void) sprintf(&((*table)[strlen(*table)]),
384*7c478bd9Sstevel@tonic-gate 			    format, rcm_info_rsrc(tuple), infostr);
385*7c478bd9Sstevel@tonic-gate 			(void) strcat(*table, "\n");
386*7c478bd9Sstevel@tonic-gate 		}
387*7c478bd9Sstevel@tonic-gate 	}
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	return (CFGA_IB_OK);
390*7c478bd9Sstevel@tonic-gate }
391