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