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