xref: /titanic_50/usr/src/uts/common/io/vuid_store.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 (c) 1985-2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
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  * Vuid_store.c - Implement the vuid_store.h event storage interface.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/vuid_event.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/vuid_state.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/vuid_store.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate static void vuid_destroy_seg();
43*7c478bd9Sstevel@tonic-gate static Vuid_seg * vuid_copy_seg();
44*7c478bd9Sstevel@tonic-gate static Vuid_seg * vuid_find_seg();
45*7c478bd9Sstevel@tonic-gate static Vuid_value * vuid_add_value();
46*7c478bd9Sstevel@tonic-gate static Vuid_value * vuid_find_value();
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
49*7c478bd9Sstevel@tonic-gate #define	vuid_alloc(bytes) \
50*7c478bd9Sstevel@tonic-gate 	kmem_alloc((bytes), servicing_interrupt())
51*7c478bd9Sstevel@tonic-gate #define	vuid_free(ptr, bytes)	kmem_free((ptr), (bytes))
52*7c478bd9Sstevel@tonic-gate #else
53*7c478bd9Sstevel@tonic-gate #define	vuid_alloc(bytes)	malloc((bytes))
54*7c478bd9Sstevel@tonic-gate #define	vuid_free(ptr, bytes)	free((ptr))
55*7c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate void
vuid_set_value(client_state_ptr,event)58*7c478bd9Sstevel@tonic-gate vuid_set_value(client_state_ptr, event)
59*7c478bd9Sstevel@tonic-gate 	Vuid_state *client_state_ptr;
60*7c478bd9Sstevel@tonic-gate 	register Firm_event *event;
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	Vuid_seg **state_ptr = (Vuid_seg **)client_state_ptr;
63*7c478bd9Sstevel@tonic-gate 	Vuid_seg *state = *state_ptr;
64*7c478bd9Sstevel@tonic-gate 	register Vuid_seg *seg;
65*7c478bd9Sstevel@tonic-gate 	register Vuid_value *val_node;
66*7c478bd9Sstevel@tonic-gate 	register Vuid_value *pair_val_node;
67*7c478bd9Sstevel@tonic-gate 	register ushort_t offset = vuid_id_offset(event->id);
68*7c478bd9Sstevel@tonic-gate 	register ushort_t pair = event->pair;
69*7c478bd9Sstevel@tonic-gate 	int	int_bit, val_original;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	/* Get (search for) seg from state assoicated with event */
72*7c478bd9Sstevel@tonic-gate 	if ((seg = vuid_find_seg(state, vuid_id_addr(event->id))) ==
73*7c478bd9Sstevel@tonic-gate 	    VUID_SEG_NULL) {
74*7c478bd9Sstevel@tonic-gate 		/* Allocate and initialize new seg for event */
75*7c478bd9Sstevel@tonic-gate 		seg = (Vuid_seg *) vuid_alloc(sizeof (*seg));
76*7c478bd9Sstevel@tonic-gate 		bzero((caddr_t)seg, sizeof (*seg));
77*7c478bd9Sstevel@tonic-gate 		seg->addr = vuid_id_addr(event->id);
78*7c478bd9Sstevel@tonic-gate 		/* Add the seg to state */
79*7c478bd9Sstevel@tonic-gate 		*state_ptr = seg;
80*7c478bd9Sstevel@tonic-gate 		seg->next = state;
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 	int_bit = vuid_get_int_bit(seg, offset);
83*7c478bd9Sstevel@tonic-gate 	/* See if no value node and event value is not boolean */
84*7c478bd9Sstevel@tonic-gate 	if ((!int_bit) && vuid_int_value(event->value)) {
85*7c478bd9Sstevel@tonic-gate 		(void) vuid_add_value(seg, offset);
86*7c478bd9Sstevel@tonic-gate 		int_bit = 1;
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 	/* If boolean event then set boolean bit */
89*7c478bd9Sstevel@tonic-gate 	if (!int_bit) {
90*7c478bd9Sstevel@tonic-gate 		if (event->value)
91*7c478bd9Sstevel@tonic-gate 			vuid_set_boolean_bit(seg, offset);
92*7c478bd9Sstevel@tonic-gate 		else
93*7c478bd9Sstevel@tonic-gate 			vuid_clear_boolean_bit(seg, offset);
94*7c478bd9Sstevel@tonic-gate 	} else {
95*7c478bd9Sstevel@tonic-gate 		/* Get (search for) value node (should be there) */
96*7c478bd9Sstevel@tonic-gate 		val_node = vuid_find_value(seg, offset);
97*7c478bd9Sstevel@tonic-gate 		val_original = val_node->value;
98*7c478bd9Sstevel@tonic-gate 		val_node->value = event->value;
99*7c478bd9Sstevel@tonic-gate 		switch (event->pair_type) {
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		case FE_PAIR_DELTA:
102*7c478bd9Sstevel@tonic-gate 			/* See if value node for pair */
103*7c478bd9Sstevel@tonic-gate 			if (!vuid_get_int_bit(seg, pair))
104*7c478bd9Sstevel@tonic-gate 				(void) vuid_add_value(seg, pair);
105*7c478bd9Sstevel@tonic-gate 			/* Get (search for) value node (should be there) */
106*7c478bd9Sstevel@tonic-gate 			pair_val_node = vuid_find_value(seg, pair);
107*7c478bd9Sstevel@tonic-gate 			/* Set pair value to difference */
108*7c478bd9Sstevel@tonic-gate 			pair_val_node->value = event->value - val_original;
109*7c478bd9Sstevel@tonic-gate 			break;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 		case FE_PAIR_ABSOLUTE:
112*7c478bd9Sstevel@tonic-gate 			/* See if value node for pair */
113*7c478bd9Sstevel@tonic-gate 			if (!vuid_get_int_bit(seg, pair))
114*7c478bd9Sstevel@tonic-gate 				(void) vuid_add_value(seg, pair);
115*7c478bd9Sstevel@tonic-gate 			/* Get (search for) value node (should be there) */
116*7c478bd9Sstevel@tonic-gate 			pair_val_node = vuid_find_value(seg, pair);
117*7c478bd9Sstevel@tonic-gate 			/* Add event value to pair value */
118*7c478bd9Sstevel@tonic-gate 			pair_val_node->value += event->value;
119*7c478bd9Sstevel@tonic-gate 			break;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		default:
122*7c478bd9Sstevel@tonic-gate 			{}
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	/* Recursively call vuid_set_value if there is an associated pair */
126*7c478bd9Sstevel@tonic-gate 	if (event->pair_type == FE_PAIR_SET) {
127*7c478bd9Sstevel@tonic-gate 		Firm_event pair_event;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 		pair_event = *event;
130*7c478bd9Sstevel@tonic-gate 		pair_event.id = vuid_id_addr(event->id) | pair;
131*7c478bd9Sstevel@tonic-gate 		pair_event.pair_type = FE_PAIR_NONE;
132*7c478bd9Sstevel@tonic-gate 		pair_event.pair = 0;
133*7c478bd9Sstevel@tonic-gate 		vuid_set_value(client_state_ptr, &pair_event);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate int
vuid_get_value(client_state,id)138*7c478bd9Sstevel@tonic-gate vuid_get_value(client_state, id)
139*7c478bd9Sstevel@tonic-gate 	Vuid_state client_state;
140*7c478bd9Sstevel@tonic-gate 	ushort_t id;
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate 	Vuid_seg *state = vuid_cstate_to_state(client_state);
143*7c478bd9Sstevel@tonic-gate 	register Vuid_seg *seg;
144*7c478bd9Sstevel@tonic-gate 	Vuid_value *val_node;
145*7c478bd9Sstevel@tonic-gate 	register ushort_t offset = vuid_id_offset(id);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	/* Get (search for) seg from state assoicated with id */
148*7c478bd9Sstevel@tonic-gate 	if ((seg = vuid_find_seg(state, vuid_id_addr(id))) == VUID_SEG_NULL)
149*7c478bd9Sstevel@tonic-gate 		return (0);
150*7c478bd9Sstevel@tonic-gate 	/* If boolean event (i.e., no ints bit on) then return boolean value */
151*7c478bd9Sstevel@tonic-gate 	if (!vuid_get_int_bit(seg, offset))
152*7c478bd9Sstevel@tonic-gate 		return (vuid_get_boolean_bit(seg, offset) != 0);
153*7c478bd9Sstevel@tonic-gate 	else {
154*7c478bd9Sstevel@tonic-gate 		/* Get (search for) value node and return value */
155*7c478bd9Sstevel@tonic-gate 		val_node = vuid_find_value(seg, offset);
156*7c478bd9Sstevel@tonic-gate 		return (val_node->value);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate void
vuid_destroy_state(client_state)161*7c478bd9Sstevel@tonic-gate vuid_destroy_state(client_state)
162*7c478bd9Sstevel@tonic-gate 	Vuid_state client_state;
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	Vuid_seg *state = vuid_cstate_to_state(client_state);
165*7c478bd9Sstevel@tonic-gate 	register Vuid_seg *seg;
166*7c478bd9Sstevel@tonic-gate 	Vuid_seg *seg_next;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	for (seg = state; seg; seg = seg_next) {
169*7c478bd9Sstevel@tonic-gate 		seg_next = seg->next;
170*7c478bd9Sstevel@tonic-gate 		vuid_destroy_seg(seg);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate static void
vuid_destroy_seg(seg)175*7c478bd9Sstevel@tonic-gate vuid_destroy_seg(seg)
176*7c478bd9Sstevel@tonic-gate 	Vuid_seg *seg;
177*7c478bd9Sstevel@tonic-gate {
178*7c478bd9Sstevel@tonic-gate 	register Vuid_value *val_node;
179*7c478bd9Sstevel@tonic-gate 	Vuid_value *val_node_next;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	for (val_node = seg->list; val_node; val_node = val_node_next) {
182*7c478bd9Sstevel@tonic-gate 		val_node_next = val_node->next;
183*7c478bd9Sstevel@tonic-gate 		vuid_free((caddr_t)val_node, sizeof (Vuid_value));
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 	vuid_free((caddr_t)seg, sizeof (Vuid_seg));
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate Vuid_state
vuid_copy_state(client_state)189*7c478bd9Sstevel@tonic-gate vuid_copy_state(client_state)
190*7c478bd9Sstevel@tonic-gate 	Vuid_state client_state;
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	Vuid_seg *state = vuid_cstate_to_state(client_state);
193*7c478bd9Sstevel@tonic-gate 	register Vuid_seg *seg;
194*7c478bd9Sstevel@tonic-gate 	Vuid_seg *new_first_seg = VUID_SEG_NULL;
195*7c478bd9Sstevel@tonic-gate 	register Vuid_seg *new_previous_seg = VUID_SEG_NULL;
196*7c478bd9Sstevel@tonic-gate 	register Vuid_seg *new_seg;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	for (seg = state; seg; seg = seg->next) {
199*7c478bd9Sstevel@tonic-gate 		new_seg = vuid_copy_seg(seg);
200*7c478bd9Sstevel@tonic-gate 		/* Remember first seg as state */
201*7c478bd9Sstevel@tonic-gate 		if (new_first_seg == VUID_SEG_NULL)
202*7c478bd9Sstevel@tonic-gate 			new_first_seg = new_seg;
203*7c478bd9Sstevel@tonic-gate 		/* Link segs together */
204*7c478bd9Sstevel@tonic-gate 		if (new_previous_seg != VUID_SEG_NULL)
205*7c478bd9Sstevel@tonic-gate 			new_previous_seg->next = new_seg;
206*7c478bd9Sstevel@tonic-gate 		/* Remember seg for linking later */
207*7c478bd9Sstevel@tonic-gate 		new_previous_seg = new_seg;
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 	return ((Vuid_state) new_first_seg);
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate static Vuid_seg *
vuid_copy_seg(seg)213*7c478bd9Sstevel@tonic-gate vuid_copy_seg(seg)
214*7c478bd9Sstevel@tonic-gate 	Vuid_seg *seg;
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 	register Vuid_value *val_node;
217*7c478bd9Sstevel@tonic-gate 	Vuid_seg *new_seg;
218*7c478bd9Sstevel@tonic-gate 	register Vuid_value *new_previous_val = VUID_VALUE_NULL;
219*7c478bd9Sstevel@tonic-gate 	register Vuid_value *new_val;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	/* Allocate and initialize new seg for event */
222*7c478bd9Sstevel@tonic-gate 	new_seg = (Vuid_seg *) vuid_alloc(sizeof (*seg));
223*7c478bd9Sstevel@tonic-gate 	*new_seg = *seg;
224*7c478bd9Sstevel@tonic-gate 	/* Terminate new pointer with null */
225*7c478bd9Sstevel@tonic-gate 	new_seg->next = VUID_SEG_NULL;
226*7c478bd9Sstevel@tonic-gate 	new_seg->list = VUID_VALUE_NULL;
227*7c478bd9Sstevel@tonic-gate 	/* Copy list elements */
228*7c478bd9Sstevel@tonic-gate 	for (val_node = seg->list; val_node; val_node = val_node->next) {
229*7c478bd9Sstevel@tonic-gate 		new_val = (Vuid_value *) vuid_alloc(sizeof (*new_val));
230*7c478bd9Sstevel@tonic-gate 		*new_val = *val_node;
231*7c478bd9Sstevel@tonic-gate 		new_val->next = VUID_VALUE_NULL;
232*7c478bd9Sstevel@tonic-gate 		/* Remember first value as head of list */
233*7c478bd9Sstevel@tonic-gate 		if (new_seg->list == VUID_VALUE_NULL)
234*7c478bd9Sstevel@tonic-gate 			new_seg->list = new_val;
235*7c478bd9Sstevel@tonic-gate 		/* Link vals together */
236*7c478bd9Sstevel@tonic-gate 		if (new_previous_val != VUID_VALUE_NULL)
237*7c478bd9Sstevel@tonic-gate 			new_previous_val->next = new_val;
238*7c478bd9Sstevel@tonic-gate 		/* Remember val for linking later */
239*7c478bd9Sstevel@tonic-gate 		new_previous_val = new_val;
240*7c478bd9Sstevel@tonic-gate 	}
241*7c478bd9Sstevel@tonic-gate 	return (new_seg);
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate static Vuid_seg *
vuid_find_seg(state,addr)245*7c478bd9Sstevel@tonic-gate vuid_find_seg(state, addr)
246*7c478bd9Sstevel@tonic-gate 	Vuid_seg *state;
247*7c478bd9Sstevel@tonic-gate 	ushort_t addr;
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	register Vuid_seg *seg;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	for (seg = state; seg; seg = seg->next) {
252*7c478bd9Sstevel@tonic-gate 		if (seg->addr == addr)
253*7c478bd9Sstevel@tonic-gate 			return (seg);
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate 	return (VUID_SEG_NULL);
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate static Vuid_value *
vuid_find_value(seg,offset)259*7c478bd9Sstevel@tonic-gate vuid_find_value(seg, offset)
260*7c478bd9Sstevel@tonic-gate 	Vuid_seg *seg;
261*7c478bd9Sstevel@tonic-gate 	ushort_t offset;
262*7c478bd9Sstevel@tonic-gate {
263*7c478bd9Sstevel@tonic-gate 	register Vuid_value *val_node;
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	for (val_node = seg->list; val_node; val_node = val_node->next) {
266*7c478bd9Sstevel@tonic-gate 		if (vuid_id_offset(val_node->offset) == offset)
267*7c478bd9Sstevel@tonic-gate 			return (val_node);
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 	return (VUID_VALUE_NULL);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate static Vuid_value *
vuid_add_value(seg,offset)273*7c478bd9Sstevel@tonic-gate vuid_add_value(seg, offset)
274*7c478bd9Sstevel@tonic-gate 	Vuid_seg *seg;
275*7c478bd9Sstevel@tonic-gate 	ushort_t offset;
276*7c478bd9Sstevel@tonic-gate {
277*7c478bd9Sstevel@tonic-gate 	Vuid_value *list_tmp;
278*7c478bd9Sstevel@tonic-gate 	Vuid_value *val_node;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	/* Allocate and initialize new value node for event */
281*7c478bd9Sstevel@tonic-gate 	val_node = (Vuid_value *) vuid_alloc(sizeof (*val_node));
282*7c478bd9Sstevel@tonic-gate 	bzero((caddr_t)val_node, sizeof (*val_node));
283*7c478bd9Sstevel@tonic-gate 	val_node->offset = offset;
284*7c478bd9Sstevel@tonic-gate 	/* Add the value node to list */
285*7c478bd9Sstevel@tonic-gate 	list_tmp = seg->list;
286*7c478bd9Sstevel@tonic-gate 	seg->list = val_node;
287*7c478bd9Sstevel@tonic-gate 	val_node->next = list_tmp;
288*7c478bd9Sstevel@tonic-gate 	vuid_set_int_bit(seg, offset);
289*7c478bd9Sstevel@tonic-gate 	/* Clear boolean bit for event */
290*7c478bd9Sstevel@tonic-gate 	vuid_clear_boolean_bit(seg, offset);
291*7c478bd9Sstevel@tonic-gate 	return (val_node);
292*7c478bd9Sstevel@tonic-gate }
293