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