xref: /onnv-gate/usr/src/uts/sun4u/ngdr/io/dr_util.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/cmn_err.h>
31*0Sstevel@tonic-gate #include <sys/param.h>		/* for NULL */
32*0Sstevel@tonic-gate #include <sys/sbd_ioctl.h>
33*0Sstevel@tonic-gate #include <sys/dr_util.h>
34*0Sstevel@tonic-gate #include <sys/varargs.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate #include <sys/systm.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /* sbd_etab[] and sbd_etab_len provided by sbdgenerr.pl */
39*0Sstevel@tonic-gate extern sbd_etab_t sbd_etab[];
40*0Sstevel@tonic-gate extern int sbd_etab_len;
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate sbd_error_t *
43*0Sstevel@tonic-gate sbd_err_new(int e_code, char *fmt, va_list args)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	sbd_error_t	*new;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 	new = GETSTRUCT(sbd_error_t, 1);
48*0Sstevel@tonic-gate 	new->e_code = e_code;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	if (fmt)
51*0Sstevel@tonic-gate 		(void) vsnprintf(new->e_rsc, sizeof (new->e_rsc), fmt, args);
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	return (new);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate void
57*0Sstevel@tonic-gate sbd_err_log(sbd_error_t *ep, int ce)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	char		 buf[32];
60*0Sstevel@tonic-gate 	char		*fmt;
61*0Sstevel@tonic-gate 	char		*txt;
62*0Sstevel@tonic-gate 	int		 i;
63*0Sstevel@tonic-gate 	sbd_etab_t	*tp;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if (!ep)
66*0Sstevel@tonic-gate 		return;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	if (ep->e_rsc[0] == '\0')
69*0Sstevel@tonic-gate 		fmt = "%s";
70*0Sstevel@tonic-gate 	else
71*0Sstevel@tonic-gate 		fmt = "%s: %s";
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	for (tp = sbd_etab, i = 0; i < sbd_etab_len; i++, tp++)
74*0Sstevel@tonic-gate 		if (ep->e_code >= tp->t_base && ep->e_code <= tp->t_bnd)
75*0Sstevel@tonic-gate 			break;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	if (i < sbd_etab_len)
78*0Sstevel@tonic-gate 		txt = tp->t_text[ep->e_code - tp->t_base];
79*0Sstevel@tonic-gate 	else {
80*0Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "error %d", ep->e_code);
81*0Sstevel@tonic-gate 		txt = buf;
82*0Sstevel@tonic-gate 	}
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	cmn_err(ce, fmt, txt, ep->e_rsc);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate void
88*0Sstevel@tonic-gate sbd_err_clear(sbd_error_t **ep)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	FREESTRUCT(*ep, sbd_error_t, 1);
91*0Sstevel@tonic-gate 	*ep = NULL;
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate void
95*0Sstevel@tonic-gate sbd_err_set_c(sbd_error_t **ep, int ce, int e_code, char *fmt, ...)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	sbd_error_t	*tmp;
98*0Sstevel@tonic-gate 	va_list		args;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	va_start(args, fmt);
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	tmp = sbd_err_new(e_code, fmt, args);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	sbd_err_log(tmp, ce);
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	if (*ep == NULL)
107*0Sstevel@tonic-gate 		*ep = tmp;
108*0Sstevel@tonic-gate 	else
109*0Sstevel@tonic-gate 		sbd_err_clear(&tmp);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	va_end(args);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate void
115*0Sstevel@tonic-gate sbd_err_set(sbd_error_t **ep, int ce, int e_code, char *fmt, ...)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	sbd_error_t	*tmp;
118*0Sstevel@tonic-gate 	va_list		args;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	va_start(args, fmt);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	tmp = sbd_err_new(e_code, fmt, args);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	sbd_err_log(tmp, ce);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	*ep = tmp;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	va_end(args);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate sbd_error_t *
132*0Sstevel@tonic-gate drerr_new_v(int e_code, char *fmt, va_list args)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	return (sbd_err_new(e_code, fmt, args));
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate sbd_error_t *
138*0Sstevel@tonic-gate drerr_new(int log, int e_code, char *fmt, ...)
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate 	sbd_error_t	*ep;
141*0Sstevel@tonic-gate 	va_list		 args;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	va_start(args, fmt);
144*0Sstevel@tonic-gate 	ep = sbd_err_new(e_code, fmt, args);
145*0Sstevel@tonic-gate 	va_end(args);
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (log)
148*0Sstevel@tonic-gate 		sbd_err_log(ep, CE_WARN);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	return (ep);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate void
154*0Sstevel@tonic-gate drerr_set_c(int log, sbd_error_t **ep, int e_code, char *fmt, ...)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	sbd_error_t	*err;
157*0Sstevel@tonic-gate 	va_list		 args;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	va_start(args, fmt);
160*0Sstevel@tonic-gate 	err = sbd_err_new(e_code, fmt, args);
161*0Sstevel@tonic-gate 	va_end(args);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	if (log)
164*0Sstevel@tonic-gate 		sbd_err_log(err, CE_WARN);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (*ep == NULL)
167*0Sstevel@tonic-gate 		*ep = err;
168*0Sstevel@tonic-gate 	else
169*0Sstevel@tonic-gate 		sbd_err_clear(&err);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate /*
174*0Sstevel@tonic-gate  * Memlist support.
175*0Sstevel@tonic-gate  */
176*0Sstevel@tonic-gate void
177*0Sstevel@tonic-gate memlist_delete(struct memlist *mlist)
178*0Sstevel@tonic-gate {
179*0Sstevel@tonic-gate 	register struct memlist	*ml;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	for (ml = mlist; ml; ml = mlist) {
182*0Sstevel@tonic-gate 		mlist = ml->next;
183*0Sstevel@tonic-gate 		FREESTRUCT(ml, struct memlist, 1);
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate int
188*0Sstevel@tonic-gate memlist_intersect(struct memlist *al, struct memlist *bl)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate 	uint64_t	astart, aend, bstart, bend;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	if ((al == NULL) || (bl == NULL))
193*0Sstevel@tonic-gate 		return (0);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	aend = al->address + al->size;
196*0Sstevel@tonic-gate 	bstart = bl->address;
197*0Sstevel@tonic-gate 	bend = bl->address + bl->size;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	while (al && bl) {
200*0Sstevel@tonic-gate 		while (al && (aend <= bstart))
201*0Sstevel@tonic-gate 			if ((al = al->next) != NULL)
202*0Sstevel@tonic-gate 				aend = al->address + al->size;
203*0Sstevel@tonic-gate 		if (al == NULL)
204*0Sstevel@tonic-gate 			return (0);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		if ((astart = al->address) <= bstart)
207*0Sstevel@tonic-gate 			return (1);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 		while (bl && (bend <= astart))
210*0Sstevel@tonic-gate 			if ((bl = bl->next) != NULL)
211*0Sstevel@tonic-gate 				bend = bl->address + bl->size;
212*0Sstevel@tonic-gate 		if (bl == NULL)
213*0Sstevel@tonic-gate 			return (0);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 		if ((bstart = bl->address) <= astart)
216*0Sstevel@tonic-gate 			return (1);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	return (0);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate void
223*0Sstevel@tonic-gate memlist_coalesce(struct memlist *mlist)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	uint64_t	end, nend;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	if ((mlist == NULL) || (mlist->next == NULL))
228*0Sstevel@tonic-gate 		return;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	while (mlist->next) {
231*0Sstevel@tonic-gate 		end = mlist->address + mlist->size;
232*0Sstevel@tonic-gate 		if (mlist->next->address <= end) {
233*0Sstevel@tonic-gate 			struct memlist 	*nl;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 			nend = mlist->next->address + mlist->next->size;
236*0Sstevel@tonic-gate 			if (nend > end)
237*0Sstevel@tonic-gate 				mlist->size += (nend - end);
238*0Sstevel@tonic-gate 			nl = mlist->next;
239*0Sstevel@tonic-gate 			mlist->next = mlist->next->next;
240*0Sstevel@tonic-gate 			if (nl) {
241*0Sstevel@tonic-gate 				FREESTRUCT(nl, struct memlist, 1);
242*0Sstevel@tonic-gate 			}
243*0Sstevel@tonic-gate 			if (mlist->next)
244*0Sstevel@tonic-gate 				mlist->next->prev = mlist;
245*0Sstevel@tonic-gate 		} else {
246*0Sstevel@tonic-gate 			mlist = mlist->next;
247*0Sstevel@tonic-gate 		}
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate #ifdef DEBUG
252*0Sstevel@tonic-gate void
253*0Sstevel@tonic-gate memlist_dump(struct memlist *mlist)
254*0Sstevel@tonic-gate {
255*0Sstevel@tonic-gate 	register struct memlist *ml;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	if (mlist == NULL)
258*0Sstevel@tonic-gate 		printf("memlist> EMPTY\n");
259*0Sstevel@tonic-gate 	else for (ml = mlist; ml; ml = ml->next)
260*0Sstevel@tonic-gate 		printf("memlist> 0x%llx, 0x%llx\n", ml->address, ml->size);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate #endif
263