xref: /onnv-gate/usr/src/uts/common/io/sbp2/cfgrom.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 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
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 /*
30*0Sstevel@tonic-gate  * SBP2 config ROM routines
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/ddi.h>
35*0Sstevel@tonic-gate #include <sys/sunddi.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <sys/1394/ieee1212.h>
38*0Sstevel@tonic-gate #include <sys/sbp2/impl.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static int	sbp2_cfgrom_rq(sbp2_tgt_t *, void *, uint64_t, uint32_t *);
41*0Sstevel@tonic-gate static int	sbp2_cfgrom_parse_dir(sbp2_tgt_t *, void *,
42*0Sstevel@tonic-gate 		sbp2_cfgrom_parse_arg_t *);
43*0Sstevel@tonic-gate static int	sbp2_cfgrom_read_leaf(sbp2_tgt_t *, void *,
44*0Sstevel@tonic-gate 		sbp2_cfgrom_ent_t *);
45*0Sstevel@tonic-gate static int	sbp2_cfgrom_read_bib(sbp2_tgt_t *, void *, sbp2_cfgrom_bib_t *);
46*0Sstevel@tonic-gate static void	sbp2_cfgrom_free_bib(sbp2_tgt_t *, sbp2_cfgrom_bib_t *);
47*0Sstevel@tonic-gate static void	sbp2_cfgrom_dir_grow(sbp2_cfgrom_dir_t *, int);
48*0Sstevel@tonic-gate static sbp2_cfgrom_ent_t *sbp2_cfgrom_dir_new_ent(sbp2_cfgrom_dir_t *);
49*0Sstevel@tonic-gate static int	sbp2_cfgrom_walk_impl(sbp2_cfgrom_ent_t *,
50*0Sstevel@tonic-gate 		int (*)(void *, sbp2_cfgrom_ent_t *, int), void *, int);
51*0Sstevel@tonic-gate static int	sbp2_cfgrom_ent_by_key_walker(void *, sbp2_cfgrom_ent_t *,
52*0Sstevel@tonic-gate 		int);
53*0Sstevel@tonic-gate static void	sbp2_cfgrom_walk_free(sbp2_cfgrom_ent_t *);
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static hrtime_t sbp2_cfgrom_read_delay = 20 * 1000000;	/* in ns */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /* imitate throwing an exception when read fails */
58*0Sstevel@tonic-gate #define	SBP2_CFGROM_RQ(tp, cmd, addr, q) \
59*0Sstevel@tonic-gate 	if ((ret = sbp2_cfgrom_rq(tp, cmd, addr, q)) != 0) { \
60*0Sstevel@tonic-gate 		goto rq_error; \
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate static int
sbp2_cfgrom_rq(sbp2_tgt_t * tp,void * cmd,uint64_t addr,uint32_t * q)64*0Sstevel@tonic-gate sbp2_cfgrom_rq(sbp2_tgt_t *tp, void *cmd, uint64_t addr, uint32_t *q)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	hrtime_t	tm;	/* time since last read */
67*0Sstevel@tonic-gate 	int		berr;
68*0Sstevel@tonic-gate 	int		ret;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	tm = gethrtime() - tp->t_last_cfgrd;
71*0Sstevel@tonic-gate 	if (tm < sbp2_cfgrom_read_delay) {
72*0Sstevel@tonic-gate 		delay(drv_usectohz((sbp2_cfgrom_read_delay - tm) / 1000));
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 	ret = SBP2_RQ(tp, cmd, addr, q, &berr);
75*0Sstevel@tonic-gate 	*q = SBP2_SWAP32(*q);
76*0Sstevel@tonic-gate 	tp->t_last_cfgrd = gethrtime();
77*0Sstevel@tonic-gate 	return (ret);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate int
sbp2_cfgrom_parse(sbp2_tgt_t * tp,sbp2_cfgrom_t * crp)81*0Sstevel@tonic-gate sbp2_cfgrom_parse(sbp2_tgt_t *tp, sbp2_cfgrom_t *crp)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	sbp2_cfgrom_ent_t *root_dir = &crp->cr_root;
84*0Sstevel@tonic-gate 	sbp2_cfgrom_bib_t *bib = &crp->cr_bib;
85*0Sstevel@tonic-gate 	void		*cmd;
86*0Sstevel@tonic-gate 	int		ret;
87*0Sstevel@tonic-gate 	sbp2_cfgrom_parse_arg_t pa;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if ((ret = SBP2_ALLOC_CMD(tp, &cmd, 0)) != SBP2_SUCCESS) {
90*0Sstevel@tonic-gate 		return (ret);
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	if ((ret = sbp2_cfgrom_read_bib(tp, cmd, bib)) != SBP2_SUCCESS) {
94*0Sstevel@tonic-gate 		SBP2_FREE_CMD(tp, cmd);
95*0Sstevel@tonic-gate 		return (ret);
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	/* parse root directory and everything underneath */
99*0Sstevel@tonic-gate 	bzero(root_dir, sizeof (sbp2_cfgrom_ent_t));
100*0Sstevel@tonic-gate 	root_dir->ce_kt = IEEE1212_DIRECTORY_TYPE;
101*0Sstevel@tonic-gate 	root_dir->ce_offset = SBP2_CFGROM_ADDR(tp) + 4 + bib->cb_len * 4;
102*0Sstevel@tonic-gate 	pa.pa_dir = root_dir;
103*0Sstevel@tonic-gate 	pa.pa_pdir = NULL;
104*0Sstevel@tonic-gate 	pa.pa_ref = NULL;
105*0Sstevel@tonic-gate 	pa.pa_depth = 0;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if ((ret = sbp2_cfgrom_parse_dir(tp, cmd, &pa)) != SBP2_SUCCESS) {
108*0Sstevel@tonic-gate 		sbp2_cfgrom_free(tp, crp);
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	SBP2_FREE_CMD(tp, cmd);
112*0Sstevel@tonic-gate 	return (ret);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate /*
117*0Sstevel@tonic-gate  * Caller must initialize pa and pa->pa_dir.
118*0Sstevel@tonic-gate  */
119*0Sstevel@tonic-gate static int
sbp2_cfgrom_parse_dir(sbp2_tgt_t * tp,void * cmd,sbp2_cfgrom_parse_arg_t * pa)120*0Sstevel@tonic-gate sbp2_cfgrom_parse_dir(sbp2_tgt_t *tp, void *cmd, sbp2_cfgrom_parse_arg_t *pa)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	sbp2_cfgrom_ent_t	*dir = pa->pa_dir; /* directory being parsed */
123*0Sstevel@tonic-gate 	sbp2_cfgrom_ent_t	*cep;		/* current entry structure */
124*0Sstevel@tonic-gate 	sbp2_cfgrom_ent_t	*pcep = NULL;	/* previous entry structure */
125*0Sstevel@tonic-gate 	sbp2_cfgrom_parse_arg_t	this_pa;	/* parse args */
126*0Sstevel@tonic-gate 	uint64_t		addr;		/* current address */
127*0Sstevel@tonic-gate 	uint32_t		entry;		/* current entry */
128*0Sstevel@tonic-gate 	uint8_t			t, k;		/* key type and value */
129*0Sstevel@tonic-gate 	uint32_t		v;		/* entry value */
130*0Sstevel@tonic-gate 	int			i;
131*0Sstevel@tonic-gate 	int			ret = 0;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	this_pa.pa_pdir = dir;
134*0Sstevel@tonic-gate 	this_pa.pa_ref = pa->pa_ref;
135*0Sstevel@tonic-gate 	this_pa.pa_depth = pa->pa_depth + 1;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	/* read directory entry and initialize the structure */
138*0Sstevel@tonic-gate 	SBP2_CFGROM_RQ(tp, cmd, dir->ce_offset, &entry);
139*0Sstevel@tonic-gate 	dir->ce_len = IEEE1212_DIR_LEN(entry);
140*0Sstevel@tonic-gate 	sbp2_cfgrom_dir_grow(&dir->ce_data.dir, dir->ce_len);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	/* walk directory entries */
143*0Sstevel@tonic-gate 	addr = dir->ce_offset + 4;
144*0Sstevel@tonic-gate 	for (i = 0; i < dir->ce_len; i++, addr += 4) {
145*0Sstevel@tonic-gate 		SBP2_CFGROM_RQ(tp, cmd, addr, &entry);
146*0Sstevel@tonic-gate 		CFGROM_TYPE_KEY_VALUE(entry, t, k, v);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		cep = sbp2_cfgrom_dir_new_ent(&dir->ce_data.dir);
149*0Sstevel@tonic-gate 		cep->ce_kt = t;
150*0Sstevel@tonic-gate 		cep->ce_kv = k;
151*0Sstevel@tonic-gate 		switch (t) {
152*0Sstevel@tonic-gate 		case IEEE1212_IMMEDIATE_TYPE:
153*0Sstevel@tonic-gate 			cep->ce_len = 1;
154*0Sstevel@tonic-gate 			cep->ce_offset = addr;
155*0Sstevel@tonic-gate 			cep->ce_data.imm = v;
156*0Sstevel@tonic-gate 			break;
157*0Sstevel@tonic-gate 		case IEEE1212_CSR_OFFSET_TYPE:
158*0Sstevel@tonic-gate 			cep->ce_len = 1;
159*0Sstevel@tonic-gate 			cep->ce_offset = addr;
160*0Sstevel@tonic-gate 			cep->ce_data.offset = v;
161*0Sstevel@tonic-gate 			break;
162*0Sstevel@tonic-gate 		case IEEE1212_LEAF_TYPE:
163*0Sstevel@tonic-gate 			cep->ce_offset = addr + 4 * v;
164*0Sstevel@tonic-gate 			if (dir->ce_kv != IEEE1212_TEXTUAL_DESCRIPTOR) {
165*0Sstevel@tonic-gate 				/* text leaf describes preceding entry */
166*0Sstevel@tonic-gate 				cep->ce_ref = pcep;
167*0Sstevel@tonic-gate 			} else {
168*0Sstevel@tonic-gate 				/* text directory describes preceding entry */
169*0Sstevel@tonic-gate 				cep->ce_ref = this_pa.pa_ref;
170*0Sstevel@tonic-gate 			}
171*0Sstevel@tonic-gate 			ret = sbp2_cfgrom_read_leaf(tp, cmd, cep);
172*0Sstevel@tonic-gate 			break;
173*0Sstevel@tonic-gate 		case IEEE1212_DIRECTORY_TYPE:
174*0Sstevel@tonic-gate 			cep->ce_offset = addr + 4 * v;
175*0Sstevel@tonic-gate 			this_pa.pa_dir = cep;
176*0Sstevel@tonic-gate 			this_pa.pa_ref = pcep;
177*0Sstevel@tonic-gate 			if (this_pa.pa_depth < SBP2_CFGROM_MAX_DEPTH) {
178*0Sstevel@tonic-gate 				ret = sbp2_cfgrom_parse_dir(tp, cmd, &this_pa);
179*0Sstevel@tonic-gate 			}
180*0Sstevel@tonic-gate 			break;
181*0Sstevel@tonic-gate 		default:
182*0Sstevel@tonic-gate 			ASSERT(0);
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 		pcep = cep;
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate rq_error:
188*0Sstevel@tonic-gate 	return (ret);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate static int
sbp2_cfgrom_read_leaf(sbp2_tgt_t * tp,void * cmd,sbp2_cfgrom_ent_t * cep)193*0Sstevel@tonic-gate sbp2_cfgrom_read_leaf(sbp2_tgt_t *tp, void *cmd, sbp2_cfgrom_ent_t *cep)
194*0Sstevel@tonic-gate {
195*0Sstevel@tonic-gate 	uint32_t	val;
196*0Sstevel@tonic-gate 	int		ret;
197*0Sstevel@tonic-gate 	int		i;
198*0Sstevel@tonic-gate 	uint64_t	addr = cep->ce_offset;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	/* header */
201*0Sstevel@tonic-gate 	SBP2_CFGROM_RQ(tp, cmd, addr, &val);
202*0Sstevel@tonic-gate 	addr += 4;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	/* verify data length */
205*0Sstevel@tonic-gate 	cep->ce_len = (val >> 16);
206*0Sstevel@tonic-gate 	if (cep->ce_len < 1) {
207*0Sstevel@tonic-gate 		return (SBP2_EDATA);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 	cep->ce_data.leaf = kmem_zalloc(cep->ce_len * 4, KM_SLEEP);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* data */
212*0Sstevel@tonic-gate 	for (i = 0; i < cep->ce_len; i++, addr += 4) {
213*0Sstevel@tonic-gate 		SBP2_CFGROM_RQ(tp, cmd, addr, &cep->ce_data.leaf[i]);
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	return (ret);
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate rq_error:
219*0Sstevel@tonic-gate 	if (cep->ce_data.leaf) {
220*0Sstevel@tonic-gate 		kmem_free(cep->ce_data.leaf, cep->ce_len * 4);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 	return (ret);
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate static int
sbp2_cfgrom_read_bib(sbp2_tgt_t * tp,void * cmd,sbp2_cfgrom_bib_t * cbp)227*0Sstevel@tonic-gate sbp2_cfgrom_read_bib(sbp2_tgt_t *tp, void *cmd, sbp2_cfgrom_bib_t *cbp)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate 	uint32_t	val;
230*0Sstevel@tonic-gate 	int		ret;
231*0Sstevel@tonic-gate 	int		i;
232*0Sstevel@tonic-gate 	uint64_t	addr = SBP2_CFGROM_ADDR(tp);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	/* header */
235*0Sstevel@tonic-gate 	SBP2_CFGROM_RQ(tp, cmd, addr, &val);
236*0Sstevel@tonic-gate 	addr += 4;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	/* verify data length */
239*0Sstevel@tonic-gate 	cbp->cb_len = (val >> 24);
240*0Sstevel@tonic-gate 	if (cbp->cb_len < 1) {
241*0Sstevel@tonic-gate 		return (SBP2_EDATA);
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 	cbp->cb_buf = kmem_zalloc(cbp->cb_len * 4, KM_SLEEP);
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	/* data */
246*0Sstevel@tonic-gate 	for (i = 0; i < cbp->cb_len; i++, addr += 4) {
247*0Sstevel@tonic-gate 		SBP2_CFGROM_RQ(tp, cmd, addr, &cbp->cb_buf[i]);
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate rq_error:
251*0Sstevel@tonic-gate 	sbp2_cfgrom_free_bib(tp, cbp);
252*0Sstevel@tonic-gate 	return (ret);
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate /*ARGSUSED*/
257*0Sstevel@tonic-gate static void
sbp2_cfgrom_free_bib(sbp2_tgt_t * tp,sbp2_cfgrom_bib_t * cbp)258*0Sstevel@tonic-gate sbp2_cfgrom_free_bib(sbp2_tgt_t *tp, sbp2_cfgrom_bib_t *cbp)
259*0Sstevel@tonic-gate {
260*0Sstevel@tonic-gate 	if ((cbp->cb_buf != NULL) && (cbp->cb_len > 0)) {
261*0Sstevel@tonic-gate 		kmem_free(cbp->cb_buf, cbp->cb_len * 4);
262*0Sstevel@tonic-gate 		cbp->cb_buf = NULL;
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate static void
sbp2_cfgrom_dir_grow(sbp2_cfgrom_dir_t * dir,int incr)267*0Sstevel@tonic-gate sbp2_cfgrom_dir_grow(sbp2_cfgrom_dir_t *dir, int incr)
268*0Sstevel@tonic-gate {
269*0Sstevel@tonic-gate 	int	new_size, old_size;
270*0Sstevel@tonic-gate 	void	*new_ent;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	ASSERT(incr > 0);
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	new_size = (dir->cd_size + incr) * sizeof (sbp2_cfgrom_ent_t);
275*0Sstevel@tonic-gate 	new_ent = kmem_zalloc(new_size, KM_SLEEP);
276*0Sstevel@tonic-gate 	if (dir->cd_size > 0) {
277*0Sstevel@tonic-gate 		old_size = dir->cd_size * sizeof (sbp2_cfgrom_ent_t);
278*0Sstevel@tonic-gate 		bcopy(dir->cd_ent, new_ent, old_size);
279*0Sstevel@tonic-gate 		kmem_free(dir->cd_ent, old_size);
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 	dir->cd_ent = new_ent;
282*0Sstevel@tonic-gate 	dir->cd_size += incr;
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate static sbp2_cfgrom_ent_t *
sbp2_cfgrom_dir_new_ent(sbp2_cfgrom_dir_t * dir)286*0Sstevel@tonic-gate sbp2_cfgrom_dir_new_ent(sbp2_cfgrom_dir_t *dir)
287*0Sstevel@tonic-gate {
288*0Sstevel@tonic-gate 	/* grow if out of entries */
289*0Sstevel@tonic-gate 	if (dir->cd_cnt >= dir->cd_size) {
290*0Sstevel@tonic-gate 		ASSERT(dir->cd_cnt == dir->cd_size);
291*0Sstevel@tonic-gate 		sbp2_cfgrom_dir_grow(dir, SBP2_CFGROM_GROW_INCR);
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	return (&dir->cd_ent[dir->cd_cnt++]);
295*0Sstevel@tonic-gate }
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate /*
298*0Sstevel@tonic-gate  * walk Config ROM entries calling the specified function for each
299*0Sstevel@tonic-gate  */
300*0Sstevel@tonic-gate void
sbp2_cfgrom_walk(sbp2_cfgrom_ent_t * dir,int (* func)(void *,sbp2_cfgrom_ent_t *,int),void * arg)301*0Sstevel@tonic-gate sbp2_cfgrom_walk(sbp2_cfgrom_ent_t *dir,
302*0Sstevel@tonic-gate     int (*func)(void *, sbp2_cfgrom_ent_t *, int), void *arg)
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate 	ASSERT(dir->ce_kt == IEEE1212_DIRECTORY_TYPE);
305*0Sstevel@tonic-gate 	(void) sbp2_cfgrom_walk_impl(dir, func, arg, 0);
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate static int
sbp2_cfgrom_walk_impl(sbp2_cfgrom_ent_t * dir,int (* func)(void *,sbp2_cfgrom_ent_t *,int),void * arg,int level)309*0Sstevel@tonic-gate sbp2_cfgrom_walk_impl(sbp2_cfgrom_ent_t *dir,
310*0Sstevel@tonic-gate     int (*func)(void *, sbp2_cfgrom_ent_t *, int), void *arg, int level)
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	int		i;
313*0Sstevel@tonic-gate 	sbp2_cfgrom_ent_t *ent;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	for (i = 0; i < dir->ce_data.dir.cd_cnt; i++) {
316*0Sstevel@tonic-gate 		ent = &dir->ce_data.dir.cd_ent[i];
317*0Sstevel@tonic-gate 		if (func(arg, ent, level) == SBP2_WALK_STOP) {
318*0Sstevel@tonic-gate 			return (SBP2_WALK_STOP);
319*0Sstevel@tonic-gate 		}
320*0Sstevel@tonic-gate 		if (ent->ce_kt == IEEE1212_DIRECTORY_TYPE) {
321*0Sstevel@tonic-gate 			if (sbp2_cfgrom_walk_impl(ent, func, arg, level + 1) ==
322*0Sstevel@tonic-gate 			    SBP2_WALK_STOP) {
323*0Sstevel@tonic-gate 				return (SBP2_WALK_STOP);
324*0Sstevel@tonic-gate 			}
325*0Sstevel@tonic-gate 		}
326*0Sstevel@tonic-gate 	}
327*0Sstevel@tonic-gate 	return (SBP2_WALK_CONTINUE);
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate sbp2_cfgrom_ent_t *
sbp2_cfgrom_ent_by_key(sbp2_cfgrom_ent_t * dir,int8_t kt,int8_t kv,int num)332*0Sstevel@tonic-gate sbp2_cfgrom_ent_by_key(sbp2_cfgrom_ent_t *dir, int8_t kt, int8_t kv, int num)
333*0Sstevel@tonic-gate {
334*0Sstevel@tonic-gate 	sbp2_cfgrom_ent_by_key_t ebk;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	ebk.kt = kt;
337*0Sstevel@tonic-gate 	ebk.kv = kv;
338*0Sstevel@tonic-gate 	ebk.num = num;
339*0Sstevel@tonic-gate 	ebk.ent = NULL;
340*0Sstevel@tonic-gate 	ebk.cnt = 0;
341*0Sstevel@tonic-gate 	sbp2_cfgrom_walk(dir, sbp2_cfgrom_ent_by_key_walker, &ebk);
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	return (ebk.ent);
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate /*ARGSUSED*/
347*0Sstevel@tonic-gate static int
sbp2_cfgrom_ent_by_key_walker(void * arg,sbp2_cfgrom_ent_t * ent,int level)348*0Sstevel@tonic-gate sbp2_cfgrom_ent_by_key_walker(void *arg, sbp2_cfgrom_ent_t *ent, int level)
349*0Sstevel@tonic-gate {
350*0Sstevel@tonic-gate 	sbp2_cfgrom_ent_by_key_t *ebk = arg;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	if ((ent->ce_kt == ebk->kt) && (ent->ce_kv == ebk->kv)) {
353*0Sstevel@tonic-gate 		if (ebk->cnt == ebk->num) {
354*0Sstevel@tonic-gate 			ebk->ent = ent;
355*0Sstevel@tonic-gate 			return (SBP2_WALK_STOP);
356*0Sstevel@tonic-gate 		}
357*0Sstevel@tonic-gate 		ebk->cnt++;
358*0Sstevel@tonic-gate 	}
359*0Sstevel@tonic-gate 	return (SBP2_WALK_CONTINUE);
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate void
sbp2_cfgrom_free(sbp2_tgt_t * tp,sbp2_cfgrom_t * crp)364*0Sstevel@tonic-gate sbp2_cfgrom_free(sbp2_tgt_t *tp, sbp2_cfgrom_t *crp)
365*0Sstevel@tonic-gate {
366*0Sstevel@tonic-gate 	sbp2_cfgrom_free_bib(tp, &crp->cr_bib);
367*0Sstevel@tonic-gate 	sbp2_cfgrom_walk_free(&crp->cr_root);
368*0Sstevel@tonic-gate }
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate static void
sbp2_cfgrom_walk_free(sbp2_cfgrom_ent_t * dir)371*0Sstevel@tonic-gate sbp2_cfgrom_walk_free(sbp2_cfgrom_ent_t *dir)
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	int		i;
374*0Sstevel@tonic-gate 	sbp2_cfgrom_dir_t *cdp = &dir->ce_data.dir;
375*0Sstevel@tonic-gate 	sbp2_cfgrom_ent_t *ent = cdp->cd_ent;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	for (i = 0; i < cdp->cd_cnt; i++) {
378*0Sstevel@tonic-gate 		if (ent[i].ce_kt == IEEE1212_DIRECTORY_TYPE) {
379*0Sstevel@tonic-gate 			sbp2_cfgrom_walk_free(&ent[i]);
380*0Sstevel@tonic-gate 		} else if ((ent[i].ce_kt == IEEE1212_LEAF_TYPE) &&
381*0Sstevel@tonic-gate 		    (ent[i].ce_data.leaf != NULL)) {
382*0Sstevel@tonic-gate 			kmem_free(ent[i].ce_data.leaf, ent[i].ce_len * 4);
383*0Sstevel@tonic-gate 		}
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 	if (ent) {
386*0Sstevel@tonic-gate 		kmem_free(ent, cdp->cd_size * sizeof (sbp2_cfgrom_ent_t));
387*0Sstevel@tonic-gate 	}
388*0Sstevel@tonic-gate }
389