summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/sbp2/cfgrom.c
blob: 999c783b2f64e3df67af82a811ce53de7340bf57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * SBP2 config ROM routines
 */

#include <sys/types.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>

#include <sys/1394/ieee1212.h>
#include <sys/sbp2/impl.h>

static int	sbp2_cfgrom_rq(sbp2_tgt_t *, void *, uint64_t, uint32_t *);
static int	sbp2_cfgrom_parse_dir(sbp2_tgt_t *, void *,
		sbp2_cfgrom_parse_arg_t *);
static int	sbp2_cfgrom_read_leaf(sbp2_tgt_t *, void *,
		sbp2_cfgrom_ent_t *);
static int	sbp2_cfgrom_read_bib(sbp2_tgt_t *, void *, sbp2_cfgrom_bib_t *);
static void	sbp2_cfgrom_free_bib(sbp2_tgt_t *, sbp2_cfgrom_bib_t *);
static void	sbp2_cfgrom_dir_grow(sbp2_cfgrom_dir_t *, int);
static sbp2_cfgrom_ent_t *sbp2_cfgrom_dir_new_ent(sbp2_cfgrom_dir_t *);
static int	sbp2_cfgrom_walk_impl(sbp2_cfgrom_ent_t *,
		int (*)(void *, sbp2_cfgrom_ent_t *, int), void *, int);
static int	sbp2_cfgrom_ent_by_key_walker(void *, sbp2_cfgrom_ent_t *,
		int);
static void	sbp2_cfgrom_walk_free(sbp2_cfgrom_ent_t *);

static hrtime_t sbp2_cfgrom_read_delay = 20 * 1000000;	/* in ns */

/* imitate throwing an exception when read fails */
#define	SBP2_CFGROM_RQ(tp, cmd, addr, q) \
	if ((ret = sbp2_cfgrom_rq(tp, cmd, addr, q)) != 0) { \
		goto rq_error; \
	}

static int
sbp2_cfgrom_rq(sbp2_tgt_t *tp, void *cmd, uint64_t addr, uint32_t *q)
{
	hrtime_t	tm;	/* time since last read */
	int		berr;
	int		ret;

	tm = gethrtime() - tp->t_last_cfgrd;
	if (tm < sbp2_cfgrom_read_delay) {
		delay(drv_usectohz((sbp2_cfgrom_read_delay - tm) / 1000));
	}
	ret = SBP2_RQ(tp, cmd, addr, q, &berr);
	*q = SBP2_SWAP32(*q);
	tp->t_last_cfgrd = gethrtime();
	return (ret);
}

int
sbp2_cfgrom_parse(sbp2_tgt_t *tp, sbp2_cfgrom_t *crp)
{
	sbp2_cfgrom_ent_t *root_dir = &crp->cr_root;
	sbp2_cfgrom_bib_t *bib = &crp->cr_bib;
	void		*cmd;
	int		ret;
	sbp2_cfgrom_parse_arg_t pa;

	if ((ret = SBP2_ALLOC_CMD(tp, &cmd, 0)) != SBP2_SUCCESS) {
		return (ret);
	}

	if ((ret = sbp2_cfgrom_read_bib(tp, cmd, bib)) != SBP2_SUCCESS) {
		SBP2_FREE_CMD(tp, cmd);
		return (ret);
	}

	/* parse root directory and everything underneath */
	bzero(root_dir, sizeof (sbp2_cfgrom_ent_t));
	root_dir->ce_kt = IEEE1212_DIRECTORY_TYPE;
	root_dir->ce_offset = SBP2_CFGROM_ADDR(tp) + 4 + bib->cb_len * 4;
	pa.pa_dir = root_dir;
	pa.pa_pdir = NULL;
	pa.pa_ref = NULL;
	pa.pa_depth = 0;

	if ((ret = sbp2_cfgrom_parse_dir(tp, cmd, &pa)) != SBP2_SUCCESS) {
		sbp2_cfgrom_free(tp, crp);
	}

	SBP2_FREE_CMD(tp, cmd);
	return (ret);
}


/*
 * Caller must initialize pa and pa->pa_dir.
 */
static int
sbp2_cfgrom_parse_dir(sbp2_tgt_t *tp, void *cmd, sbp2_cfgrom_parse_arg_t *pa)
{
	sbp2_cfgrom_ent_t	*dir = pa->pa_dir; /* directory being parsed */
	sbp2_cfgrom_ent_t	*cep;		/* current entry structure */
	sbp2_cfgrom_ent_t	*pcep = NULL;	/* previous entry structure */
	sbp2_cfgrom_parse_arg_t	this_pa;	/* parse args */
	uint64_t		addr;		/* current address */
	uint32_t		entry;		/* current entry */
	uint8_t			t, k;		/* key type and value */
	uint32_t		v;		/* entry value */
	int			i;
	int			ret = 0;

	this_pa.pa_pdir = dir;
	this_pa.pa_ref = pa->pa_ref;
	this_pa.pa_depth = pa->pa_depth + 1;

	/* read directory entry and initialize the structure */
	SBP2_CFGROM_RQ(tp, cmd, dir->ce_offset, &entry);
	dir->ce_len = IEEE1212_DIR_LEN(entry);
	sbp2_cfgrom_dir_grow(&dir->ce_data.dir, dir->ce_len);

	/* walk directory entries */
	addr = dir->ce_offset + 4;
	for (i = 0; i < dir->ce_len; i++, addr += 4) {
		SBP2_CFGROM_RQ(tp, cmd, addr, &entry);
		CFGROM_TYPE_KEY_VALUE(entry, t, k, v);

		cep = sbp2_cfgrom_dir_new_ent(&dir->ce_data.dir);
		cep->ce_kt = t;
		cep->ce_kv = k;
		switch (t) {
		case IEEE1212_IMMEDIATE_TYPE:
			cep->ce_len = 1;
			cep->ce_offset = addr;
			cep->ce_data.imm = v;
			break;
		case IEEE1212_CSR_OFFSET_TYPE:
			cep->ce_len = 1;
			cep->ce_offset = addr;
			cep->ce_data.offset = v;
			break;
		case IEEE1212_LEAF_TYPE:
			cep->ce_offset = addr + 4 * v;
			if (dir->ce_kv != IEEE1212_TEXTUAL_DESCRIPTOR) {
				/* text leaf describes preceding entry */
				cep->ce_ref = pcep;
			} else {
				/* text directory describes preceding entry */
				cep->ce_ref = this_pa.pa_ref;
			}
			ret = sbp2_cfgrom_read_leaf(tp, cmd, cep);
			break;
		case IEEE1212_DIRECTORY_TYPE:
			cep->ce_offset = addr + 4 * v;
			this_pa.pa_dir = cep;
			this_pa.pa_ref = pcep;
			if (this_pa.pa_depth < SBP2_CFGROM_MAX_DEPTH) {
				ret = sbp2_cfgrom_parse_dir(tp, cmd, &this_pa);
			}
			break;
		default:
			ASSERT(0);
		}
		pcep = cep;
	}

rq_error:
	return (ret);
}


static int
sbp2_cfgrom_read_leaf(sbp2_tgt_t *tp, void *cmd, sbp2_cfgrom_ent_t *cep)
{
	uint32_t	val;
	int		ret;
	int		i;
	uint64_t	addr = cep->ce_offset;

	/* header */
	SBP2_CFGROM_RQ(tp, cmd, addr, &val);
	addr += 4;

	/* verify data length */
	cep->ce_len = (val >> 16);
	if (cep->ce_len < 1) {
		return (SBP2_EDATA);
	}
	cep->ce_data.leaf = kmem_zalloc(cep->ce_len * 4, KM_SLEEP);

	/* data */
	for (i = 0; i < cep->ce_len; i++, addr += 4) {
		SBP2_CFGROM_RQ(tp, cmd, addr, &cep->ce_data.leaf[i]);
	}

	return (ret);

rq_error:
	if (cep->ce_data.leaf) {
		kmem_free(cep->ce_data.leaf, cep->ce_len * 4);
	}
	return (ret);
}


static int
sbp2_cfgrom_read_bib(sbp2_tgt_t *tp, void *cmd, sbp2_cfgrom_bib_t *cbp)
{
	uint32_t	val;
	int		ret;
	int		i;
	uint64_t	addr = SBP2_CFGROM_ADDR(tp);

	/* header */
	SBP2_CFGROM_RQ(tp, cmd, addr, &val);
	addr += 4;

	/* verify data length */
	cbp->cb_len = (val >> 24);
	if (cbp->cb_len < 1) {
		return (SBP2_EDATA);
	}
	cbp->cb_buf = kmem_zalloc(cbp->cb_len * 4, KM_SLEEP);

	/* data */
	for (i = 0; i < cbp->cb_len; i++, addr += 4) {
		SBP2_CFGROM_RQ(tp, cmd, addr, &cbp->cb_buf[i]);
	}

rq_error:
	sbp2_cfgrom_free_bib(tp, cbp);
	return (ret);
}


/*ARGSUSED*/
static void
sbp2_cfgrom_free_bib(sbp2_tgt_t *tp, sbp2_cfgrom_bib_t *cbp)
{
	if ((cbp->cb_buf != NULL) && (cbp->cb_len > 0)) {
		kmem_free(cbp->cb_buf, cbp->cb_len * 4);
		cbp->cb_buf = NULL;
	}
}

static void
sbp2_cfgrom_dir_grow(sbp2_cfgrom_dir_t *dir, int incr)
{
	int	new_size, old_size;
	void	*new_ent;

	ASSERT(incr > 0);

	new_size = (dir->cd_size + incr) * sizeof (sbp2_cfgrom_ent_t);
	new_ent = kmem_zalloc(new_size, KM_SLEEP);
	if (dir->cd_size > 0) {
		old_size = dir->cd_size * sizeof (sbp2_cfgrom_ent_t);
		bcopy(dir->cd_ent, new_ent, old_size);
		kmem_free(dir->cd_ent, old_size);
	}
	dir->cd_ent = new_ent;
	dir->cd_size += incr;
}

static sbp2_cfgrom_ent_t *
sbp2_cfgrom_dir_new_ent(sbp2_cfgrom_dir_t *dir)
{
	/* grow if out of entries */
	if (dir->cd_cnt >= dir->cd_size) {
		ASSERT(dir->cd_cnt == dir->cd_size);
		sbp2_cfgrom_dir_grow(dir, SBP2_CFGROM_GROW_INCR);
	}

	return (&dir->cd_ent[dir->cd_cnt++]);
}

/*
 * walk Config ROM entries calling the specified function for each
 */
void
sbp2_cfgrom_walk(sbp2_cfgrom_ent_t *dir,
    int (*func)(void *, sbp2_cfgrom_ent_t *, int), void *arg)
{
	ASSERT(dir->ce_kt == IEEE1212_DIRECTORY_TYPE);
	(void) sbp2_cfgrom_walk_impl(dir, func, arg, 0);
}

static int
sbp2_cfgrom_walk_impl(sbp2_cfgrom_ent_t *dir,
    int (*func)(void *, sbp2_cfgrom_ent_t *, int), void *arg, int level)
{
	int		i;
	sbp2_cfgrom_ent_t *ent;

	for (i = 0; i < dir->ce_data.dir.cd_cnt; i++) {
		ent = &dir->ce_data.dir.cd_ent[i];
		if (func(arg, ent, level) == SBP2_WALK_STOP) {
			return (SBP2_WALK_STOP);
		}
		if (ent->ce_kt == IEEE1212_DIRECTORY_TYPE) {
			if (sbp2_cfgrom_walk_impl(ent, func, arg, level + 1) ==
			    SBP2_WALK_STOP) {
				return (SBP2_WALK_STOP);
			}
		}
	}
	return (SBP2_WALK_CONTINUE);
}


sbp2_cfgrom_ent_t *
sbp2_cfgrom_ent_by_key(sbp2_cfgrom_ent_t *dir, int8_t kt, int8_t kv, int num)
{
	sbp2_cfgrom_ent_by_key_t ebk;

	ebk.kt = kt;
	ebk.kv = kv;
	ebk.num = num;
	ebk.ent = NULL;
	ebk.cnt = 0;
	sbp2_cfgrom_walk(dir, sbp2_cfgrom_ent_by_key_walker, &ebk);

	return (ebk.ent);
}

/*ARGSUSED*/
static int
sbp2_cfgrom_ent_by_key_walker(void *arg, sbp2_cfgrom_ent_t *ent, int level)
{
	sbp2_cfgrom_ent_by_key_t *ebk = arg;

	if ((ent->ce_kt == ebk->kt) && (ent->ce_kv == ebk->kv)) {
		if (ebk->cnt == ebk->num) {
			ebk->ent = ent;
			return (SBP2_WALK_STOP);
		}
		ebk->cnt++;
	}
	return (SBP2_WALK_CONTINUE);
}


void
sbp2_cfgrom_free(sbp2_tgt_t *tp, sbp2_cfgrom_t *crp)
{
	sbp2_cfgrom_free_bib(tp, &crp->cr_bib);
	sbp2_cfgrom_walk_free(&crp->cr_root);
}

static void
sbp2_cfgrom_walk_free(sbp2_cfgrom_ent_t *dir)
{
	int		i;
	sbp2_cfgrom_dir_t *cdp = &dir->ce_data.dir;
	sbp2_cfgrom_ent_t *ent = cdp->cd_ent;

	for (i = 0; i < cdp->cd_cnt; i++) {
		if (ent[i].ce_kt == IEEE1212_DIRECTORY_TYPE) {
			sbp2_cfgrom_walk_free(&ent[i]);
		} else if ((ent[i].ce_kt == IEEE1212_LEAF_TYPE) &&
		    (ent[i].ce_data.leaf != NULL)) {
			kmem_free(ent[i].ce_data.leaf, ent[i].ce_len * 4);
		}
	}
	if (ent) {
		kmem_free(ent, cdp->cd_size * sizeof (sbp2_cfgrom_ent_t));
	}
}