xref: /onnv-gate/usr/src/uts/common/io/1394/s1394_addr.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 1999-2002 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
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  * s1394_addr.c
31*0Sstevel@tonic-gate  *    1394 Address Space Routines
32*0Sstevel@tonic-gate  *    Implements all the routines necessary for alloc/free and lookup
33*0Sstevel@tonic-gate  *    of the 1394 address space
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/conf.h>
37*0Sstevel@tonic-gate #include <sys/ddi.h>
38*0Sstevel@tonic-gate #include <sys/sunddi.h>
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/kmem.h>
41*0Sstevel@tonic-gate #include <sys/tnf_probe.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <sys/1394/t1394.h>
44*0Sstevel@tonic-gate #include <sys/1394/s1394.h>
45*0Sstevel@tonic-gate #include <sys/1394/h1394.h>
46*0Sstevel@tonic-gate #include <sys/1394/ieee1394.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static s1394_addr_space_blk_t *s1394_free_list_search(s1394_hal_t *hal,
49*0Sstevel@tonic-gate     uint64_t addr);
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate static s1394_addr_space_blk_t *s1394_free_list_find(s1394_hal_t *hal,
52*0Sstevel@tonic-gate     uint32_t type, uint32_t length);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static s1394_addr_space_blk_t *s1394_free_list_delete(s1394_hal_t *hal,
55*0Sstevel@tonic-gate     s1394_addr_space_blk_t *del_blk);
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static void s1394_used_tree_insert(s1394_hal_t *hal, s1394_addr_space_blk_t *x);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static void s1394_tree_insert(s1394_addr_space_blk_t **root,
60*0Sstevel@tonic-gate     s1394_addr_space_blk_t *z);
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static s1394_addr_space_blk_t *s1394_tree_search(s1394_addr_space_blk_t *x,
63*0Sstevel@tonic-gate     uint64_t address);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static void s1394_used_tree_delete_fixup(s1394_addr_space_blk_t **root,
66*0Sstevel@tonic-gate     s1394_addr_space_blk_t *p, s1394_addr_space_blk_t *x,
67*0Sstevel@tonic-gate     s1394_addr_space_blk_t *w, int side_of_x);
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate static void s1394_left_rotate(s1394_addr_space_blk_t **root,
70*0Sstevel@tonic-gate     s1394_addr_space_blk_t *x);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static void s1394_right_rotate(s1394_addr_space_blk_t **root,
73*0Sstevel@tonic-gate     s1394_addr_space_blk_t *x);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static s1394_addr_space_blk_t *s1394_tree_minimum(s1394_addr_space_blk_t *x);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static s1394_addr_space_blk_t *s1394_tree_successor(s1394_addr_space_blk_t *x);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate  * s1394_request_addr_blk()
81*0Sstevel@tonic-gate  *    is called when a target driver is requesting a block of 1394 Address
82*0Sstevel@tonic-gate  *    Space of a particular type without regard for its exact location.  It
83*0Sstevel@tonic-gate  *    searches the free list for a block that's big enough and of the specified
84*0Sstevel@tonic-gate  *    type, and it inserts it into the used tree.
85*0Sstevel@tonic-gate  */
86*0Sstevel@tonic-gate int
s1394_request_addr_blk(s1394_hal_t * hal,t1394_alloc_addr_t * addr_allocp)87*0Sstevel@tonic-gate s1394_request_addr_blk(s1394_hal_t *hal, t1394_alloc_addr_t *addr_allocp)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*curr_blk;
90*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*new_blk;
91*0Sstevel@tonic-gate 	uint64_t		amount_free;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_request_addr_blk_enter,
94*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	ASSERT(hal != NULL);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	/* Lock the address space "free" list */
99*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_free_mutex);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	curr_blk = s1394_free_list_find(hal, addr_allocp->aa_type,
102*0Sstevel@tonic-gate 	    addr_allocp->aa_length);
103*0Sstevel@tonic-gate 	if (curr_blk == NULL) {
104*0Sstevel@tonic-gate 		/* Unlock the address space "free" list */
105*0Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_free_mutex);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 		TNF_PROBE_1(s1394_request_addr_blk_error,
108*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_ERROR, "", tnf_string, msg,
109*0Sstevel@tonic-gate 		    "1394 address space - no more memory");
110*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_request_addr_blk_exit,
111*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
112*0Sstevel@tonic-gate 		return (DDI_FAILURE);
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	amount_free = (curr_blk->addr_hi - curr_blk->addr_lo) + 1;
116*0Sstevel@tonic-gate 	/* Does it fit exact? */
117*0Sstevel@tonic-gate 	if (amount_free == addr_allocp->aa_length) {
118*0Sstevel@tonic-gate 		/* Take it out of the "free" list */
119*0Sstevel@tonic-gate 		curr_blk = s1394_free_list_delete(hal, curr_blk);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 		/* Unlock the address space "free" list */
122*0Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_free_mutex);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 		curr_blk->addr_enable = addr_allocp->aa_enable;
125*0Sstevel@tonic-gate 		curr_blk->kmem_bufp = addr_allocp->aa_kmem_bufp;
126*0Sstevel@tonic-gate 		curr_blk->addr_arg = addr_allocp->aa_arg;
127*0Sstevel@tonic-gate 		curr_blk->addr_events = addr_allocp->aa_evts;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 		addr_allocp->aa_address = curr_blk->addr_lo;
130*0Sstevel@tonic-gate 		addr_allocp->aa_hdl = (t1394_addr_handle_t)curr_blk;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		/* Put it into the "used" tree */
133*0Sstevel@tonic-gate 		s1394_used_tree_insert(hal, curr_blk);
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 		s1394_addr_alloc_kstat(hal, addr_allocp->aa_address);
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_request_addr_blk_exit,
138*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
139*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	} else {
142*0Sstevel@tonic-gate 		/* Needs to be broken up */
143*0Sstevel@tonic-gate 		new_blk = (s1394_addr_space_blk_t *)
144*0Sstevel@tonic-gate 		    kmem_zalloc(sizeof (s1394_addr_space_blk_t), KM_NOSLEEP);
145*0Sstevel@tonic-gate 		if (new_blk == NULL) {
146*0Sstevel@tonic-gate 			/* Unlock the address space "free" list */
147*0Sstevel@tonic-gate 			mutex_exit(&hal->addr_space_free_mutex);
148*0Sstevel@tonic-gate 			TNF_PROBE_0(s1394_request_addr_blk_error,
149*0Sstevel@tonic-gate 			    S1394_TNF_SL_ARREQ_ERROR, "");
150*0Sstevel@tonic-gate 			TNF_PROBE_0_DEBUG(s1394_request_addr_blk_exit,
151*0Sstevel@tonic-gate 			    S1394_TNF_SL_ARREQ_STACK, "");
152*0Sstevel@tonic-gate 			return (DDI_FAILURE);
153*0Sstevel@tonic-gate 		}
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 		new_blk->addr_lo = curr_blk->addr_lo;
156*0Sstevel@tonic-gate 		new_blk->addr_hi = curr_blk->addr_lo +
157*0Sstevel@tonic-gate 		    (addr_allocp->aa_length - 1);
158*0Sstevel@tonic-gate 		new_blk->addr_type = curr_blk->addr_type;
159*0Sstevel@tonic-gate 		new_blk->addr_enable = addr_allocp->aa_enable;
160*0Sstevel@tonic-gate 		new_blk->kmem_bufp = addr_allocp->aa_kmem_bufp;
161*0Sstevel@tonic-gate 		new_blk->addr_arg = addr_allocp->aa_arg;
162*0Sstevel@tonic-gate 		new_blk->addr_events = addr_allocp->aa_evts;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		curr_blk->addr_lo = new_blk->addr_hi + 1;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 		addr_allocp->aa_address = new_blk->addr_lo;
167*0Sstevel@tonic-gate 		addr_allocp->aa_hdl = (t1394_addr_handle_t)new_blk;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 		/* Unlock the address space "free" list */
170*0Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_free_mutex);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 		/* Put it into the "used" tree */
173*0Sstevel@tonic-gate 		s1394_used_tree_insert(hal, new_blk);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		s1394_addr_alloc_kstat(hal, addr_allocp->aa_address);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_request_addr_blk_exit,
178*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
179*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate /*
184*0Sstevel@tonic-gate  * s1394_claim_addr_blk()
185*0Sstevel@tonic-gate  *    is called when a target driver is requesting a block of 1394 Address
186*0Sstevel@tonic-gate  *    Space with a specific address.  If the block containing that address
187*0Sstevel@tonic-gate  *    is not in the free list, or if the block is too small, then
188*0Sstevel@tonic-gate  *    s1394_claim_addr_blk() returns failure.  If the block is found,
189*0Sstevel@tonic-gate  *    however, it is inserted into the used tree.
190*0Sstevel@tonic-gate  */
191*0Sstevel@tonic-gate int
s1394_claim_addr_blk(s1394_hal_t * hal,t1394_alloc_addr_t * addr_allocp)192*0Sstevel@tonic-gate s1394_claim_addr_blk(s1394_hal_t *hal, t1394_alloc_addr_t *addr_allocp)
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*curr_blk;
195*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*new_blk;
196*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*middle_blk;
197*0Sstevel@tonic-gate 	uint64_t		upper_bound;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_claim_addr_blk_enter,
200*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	ASSERT(hal != NULL);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	/* Lock the address space "free" list */
205*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_free_mutex);
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	/* Find the block in the "free" list */
208*0Sstevel@tonic-gate 	curr_blk = s1394_free_list_search(hal, addr_allocp->aa_address);
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	/* If it wasn't found, it isn't free... */
211*0Sstevel@tonic-gate 	if (curr_blk == NULL) {
212*0Sstevel@tonic-gate 		/* Unlock the address space free list */
213*0Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_free_mutex);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 		TNF_PROBE_1(s1394_claim_addr_blk_error,
216*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_ERROR, "", tnf_string, msg,
217*0Sstevel@tonic-gate 		    "1394 address space - address unavailable");
218*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_claim_addr_blk_exit,
219*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
220*0Sstevel@tonic-gate 		return (DDI_FAILURE);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	/* Does the request fit in the block? */
224*0Sstevel@tonic-gate 	upper_bound = (addr_allocp->aa_address + addr_allocp->aa_length) - 1;
225*0Sstevel@tonic-gate 	if ((upper_bound >= curr_blk->addr_lo) &&
226*0Sstevel@tonic-gate 	    (upper_bound <= curr_blk->addr_hi)) {
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		/* How does the requested range fit in the current range? */
229*0Sstevel@tonic-gate 		if (addr_allocp->aa_address == curr_blk->addr_lo) {
230*0Sstevel@tonic-gate 			if (upper_bound == curr_blk->addr_hi) {
231*0Sstevel@tonic-gate 				/* Exact fit */
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 				/* Take it out of the "free" list */
234*0Sstevel@tonic-gate 				curr_blk = s1394_free_list_delete(hal,
235*0Sstevel@tonic-gate 				    curr_blk);
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 				/* Unlock the address space "free" list */
238*0Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_free_mutex);
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 				curr_blk->addr_enable = addr_allocp->aa_enable;
241*0Sstevel@tonic-gate 				curr_blk->kmem_bufp = addr_allocp->aa_kmem_bufp;
242*0Sstevel@tonic-gate 				curr_blk->addr_arg = addr_allocp->aa_arg;
243*0Sstevel@tonic-gate 				curr_blk->addr_events = addr_allocp->aa_evts;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 				addr_allocp->aa_hdl =
246*0Sstevel@tonic-gate 				    (t1394_addr_handle_t)curr_blk;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 				/* Put it into the "used" tree */
249*0Sstevel@tonic-gate 				s1394_used_tree_insert(hal, curr_blk);
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 				s1394_addr_alloc_kstat(hal,
252*0Sstevel@tonic-gate 				    addr_allocp->aa_address);
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 				TNF_PROBE_0_DEBUG(s1394_claim_addr_blk_exit,
255*0Sstevel@tonic-gate 				    S1394_TNF_SL_ARREQ_STACK, "");
256*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 			} else {
259*0Sstevel@tonic-gate 				/* If space is reserved, must claim it all */
260*0Sstevel@tonic-gate 				if (curr_blk->addr_reserved == ADDR_RESERVED) {
261*0Sstevel@tonic-gate 					goto claim_error;
262*0Sstevel@tonic-gate 				}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 				/* Front part of range */
265*0Sstevel@tonic-gate 				new_blk = (s1394_addr_space_blk_t *)
266*0Sstevel@tonic-gate 				    kmem_zalloc(sizeof (s1394_addr_space_blk_t),
267*0Sstevel@tonic-gate 				    KM_NOSLEEP);
268*0Sstevel@tonic-gate 				if (new_blk == NULL) {
269*0Sstevel@tonic-gate 					/* Unlock the addr space "free" list */
270*0Sstevel@tonic-gate 					mutex_exit(&hal->addr_space_free_mutex);
271*0Sstevel@tonic-gate 					TNF_PROBE_0(s1394_claim_addr_blk_error,
272*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_ERROR, "");
273*0Sstevel@tonic-gate 					TNF_PROBE_0_DEBUG(
274*0Sstevel@tonic-gate 					    s1394_claim_addr_blk_exit,
275*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_STACK, "");
276*0Sstevel@tonic-gate 					return (DDI_FAILURE);
277*0Sstevel@tonic-gate 				}
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 				new_blk->addr_lo = curr_blk->addr_lo;
280*0Sstevel@tonic-gate 				new_blk->addr_hi = upper_bound;
281*0Sstevel@tonic-gate 				new_blk->addr_type = curr_blk->addr_type;
282*0Sstevel@tonic-gate 				new_blk->addr_enable = addr_allocp->aa_enable;
283*0Sstevel@tonic-gate 				new_blk->kmem_bufp = addr_allocp->aa_kmem_bufp;
284*0Sstevel@tonic-gate 				new_blk->addr_arg = addr_allocp->aa_arg;
285*0Sstevel@tonic-gate 				new_blk->addr_events = addr_allocp->aa_evts;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 				curr_blk->addr_lo = new_blk->addr_hi + 1;
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 				addr_allocp->aa_hdl =
290*0Sstevel@tonic-gate 				    (t1394_addr_handle_t)new_blk;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 				/* Unlock the address space free list */
293*0Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_free_mutex);
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 				/* Put it into the "used" tree */
296*0Sstevel@tonic-gate 				s1394_used_tree_insert(hal, new_blk);
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 				s1394_addr_alloc_kstat(hal,
299*0Sstevel@tonic-gate 				    addr_allocp->aa_address);
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 				TNF_PROBE_0_DEBUG(s1394_claim_addr_blk_exit,
302*0Sstevel@tonic-gate 				    S1394_TNF_SL_ARREQ_STACK, "");
303*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
304*0Sstevel@tonic-gate 			}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 		} else {
307*0Sstevel@tonic-gate 			if (upper_bound == curr_blk->addr_hi) {
308*0Sstevel@tonic-gate 				/* If space is reserved, must claim it all */
309*0Sstevel@tonic-gate 				if (curr_blk->addr_reserved == ADDR_RESERVED) {
310*0Sstevel@tonic-gate 					goto claim_error;
311*0Sstevel@tonic-gate 				}
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 				/* End part of range */
314*0Sstevel@tonic-gate 				new_blk = (s1394_addr_space_blk_t *)
315*0Sstevel@tonic-gate 				    kmem_zalloc(sizeof (s1394_addr_space_blk_t),
316*0Sstevel@tonic-gate 				    KM_NOSLEEP);
317*0Sstevel@tonic-gate 				if (new_blk == NULL) {
318*0Sstevel@tonic-gate 					/* Unlock the addr space "free" list */
319*0Sstevel@tonic-gate 					mutex_exit(&hal->addr_space_free_mutex);
320*0Sstevel@tonic-gate 					TNF_PROBE_0(s1394_claim_addr_blk_error,
321*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_ERROR, "");
322*0Sstevel@tonic-gate 					TNF_PROBE_0_DEBUG
323*0Sstevel@tonic-gate 					    (s1394_claim_addr_blk_exit,
324*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_STACK, "");
325*0Sstevel@tonic-gate 					return (DDI_FAILURE);
326*0Sstevel@tonic-gate 				}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 				new_blk->addr_lo = addr_allocp->aa_address;
329*0Sstevel@tonic-gate 				new_blk->addr_hi = upper_bound;
330*0Sstevel@tonic-gate 				new_blk->addr_type = curr_blk->addr_type;
331*0Sstevel@tonic-gate 				new_blk->addr_enable = addr_allocp->aa_enable;
332*0Sstevel@tonic-gate 				new_blk->kmem_bufp = addr_allocp->aa_kmem_bufp;
333*0Sstevel@tonic-gate 				new_blk->addr_arg = addr_allocp->aa_arg;
334*0Sstevel@tonic-gate 				new_blk->addr_events = addr_allocp->aa_evts;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 				curr_blk->addr_hi = addr_allocp->aa_address - 1;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 				addr_allocp->aa_hdl =
339*0Sstevel@tonic-gate 				    (t1394_addr_handle_t)new_blk;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 				/* Unlock the address space free list */
342*0Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_free_mutex);
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 				/* Put it into the "used" tree */
345*0Sstevel@tonic-gate 				s1394_used_tree_insert(hal, new_blk);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 				s1394_addr_alloc_kstat(hal,
348*0Sstevel@tonic-gate 				    addr_allocp->aa_address);
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 				TNF_PROBE_0_DEBUG(s1394_claim_addr_blk_exit,
351*0Sstevel@tonic-gate 				    S1394_TNF_SL_ARREQ_STACK, "");
352*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 			} else {
355*0Sstevel@tonic-gate 				/* If space is reserved, must claim it all */
356*0Sstevel@tonic-gate 				if (curr_blk->addr_reserved == ADDR_RESERVED) {
357*0Sstevel@tonic-gate 					goto claim_error;
358*0Sstevel@tonic-gate 				}
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 				/* Middle part of range */
361*0Sstevel@tonic-gate 				new_blk = (s1394_addr_space_blk_t *)
362*0Sstevel@tonic-gate 				    kmem_zalloc(sizeof (s1394_addr_space_blk_t),
363*0Sstevel@tonic-gate 				    KM_NOSLEEP);
364*0Sstevel@tonic-gate 				if (new_blk == NULL) {
365*0Sstevel@tonic-gate 					/* Unlock the addr space "free" list */
366*0Sstevel@tonic-gate 					mutex_exit(&hal->addr_space_free_mutex);
367*0Sstevel@tonic-gate 					TNF_PROBE_0(s1394_claim_addr_blk_error,
368*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_ERROR, "");
369*0Sstevel@tonic-gate 					TNF_PROBE_0_DEBUG(
370*0Sstevel@tonic-gate 					    s1394_claim_addr_blk_exit,
371*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_STACK, "");
372*0Sstevel@tonic-gate 					return (DDI_FAILURE);
373*0Sstevel@tonic-gate 				}
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 				middle_blk = (s1394_addr_space_blk_t *)
376*0Sstevel@tonic-gate 				    kmem_zalloc(sizeof (s1394_addr_space_blk_t),
377*0Sstevel@tonic-gate 				    KM_NOSLEEP);
378*0Sstevel@tonic-gate 				if (middle_blk == NULL) {
379*0Sstevel@tonic-gate 					/* Unlock the addr space "free" list */
380*0Sstevel@tonic-gate 					mutex_exit(&hal->addr_space_free_mutex);
381*0Sstevel@tonic-gate 					kmem_free(new_blk,
382*0Sstevel@tonic-gate 					    sizeof (s1394_addr_space_blk_t));
383*0Sstevel@tonic-gate 					TNF_PROBE_0(s1394_claim_addr_blk_error,
384*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_ERROR, "");
385*0Sstevel@tonic-gate 					TNF_PROBE_0_DEBUG
386*0Sstevel@tonic-gate 					    (s1394_claim_addr_blk_exit,
387*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_STACK, "");
388*0Sstevel@tonic-gate 					return (DDI_FAILURE);
389*0Sstevel@tonic-gate 				}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 				middle_blk->addr_lo = addr_allocp->aa_address;
392*0Sstevel@tonic-gate 				middle_blk->addr_hi = upper_bound;
393*0Sstevel@tonic-gate 				new_blk->addr_lo = upper_bound + 1;
394*0Sstevel@tonic-gate 				new_blk->addr_hi = curr_blk->addr_hi;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 				new_blk->addr_type = curr_blk->addr_type;
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 				middle_blk->addr_type = curr_blk->addr_type;
399*0Sstevel@tonic-gate 				middle_blk->addr_enable =
400*0Sstevel@tonic-gate 				    addr_allocp->aa_enable;
401*0Sstevel@tonic-gate 				middle_blk->kmem_bufp =
402*0Sstevel@tonic-gate 				    addr_allocp->aa_kmem_bufp;
403*0Sstevel@tonic-gate 				middle_blk->addr_arg = addr_allocp->aa_arg;
404*0Sstevel@tonic-gate 				middle_blk->addr_events = addr_allocp->aa_evts;
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 				curr_blk->addr_hi = addr_allocp->aa_address - 1;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 				addr_allocp->aa_hdl =
409*0Sstevel@tonic-gate 				    (t1394_addr_handle_t)middle_blk;
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 				/* Put part back into the "free" tree */
412*0Sstevel@tonic-gate 				s1394_free_list_insert(hal, new_blk);
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 				/* Unlock the address space free list */
415*0Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_free_mutex);
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 				/* Put it into the "used" tree */
418*0Sstevel@tonic-gate 				s1394_used_tree_insert(hal, middle_blk);
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 				s1394_addr_alloc_kstat(hal,
421*0Sstevel@tonic-gate 				    addr_allocp->aa_address);
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 				TNF_PROBE_0_DEBUG(s1394_claim_addr_blk_exit,
424*0Sstevel@tonic-gate 				    S1394_TNF_SL_ARREQ_STACK, "");
425*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
426*0Sstevel@tonic-gate 			}
427*0Sstevel@tonic-gate 		}
428*0Sstevel@tonic-gate 	}
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate claim_error:
431*0Sstevel@tonic-gate 	/* Unlock the address space free list */
432*0Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_free_mutex);
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_claim_addr_blk_exit,
435*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
436*0Sstevel@tonic-gate 	return (DDI_FAILURE);
437*0Sstevel@tonic-gate }
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate /*
440*0Sstevel@tonic-gate  * s1394_free_addr_blk()
441*0Sstevel@tonic-gate  *    An opposite of s1394_claim_addr_blk(): takes the address block
442*0Sstevel@tonic-gate  *    out of the "used" tree and puts it into the "free" tree.
443*0Sstevel@tonic-gate  */
444*0Sstevel@tonic-gate int
s1394_free_addr_blk(s1394_hal_t * hal,s1394_addr_space_blk_t * blk)445*0Sstevel@tonic-gate s1394_free_addr_blk(s1394_hal_t *hal, s1394_addr_space_blk_t *blk)
446*0Sstevel@tonic-gate {
447*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_addr_blk_enter, S1394_TNF_SL_ARREQ_STACK,
448*0Sstevel@tonic-gate 	    "");
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	/* Lock the address space "free" list */
451*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_free_mutex);
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	/* Take it out of the "used" tree */
454*0Sstevel@tonic-gate 	blk = s1394_used_tree_delete(hal, blk);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	if (blk == NULL) {
457*0Sstevel@tonic-gate 		/* Unlock the address space "free" list */
458*0Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_free_mutex);
459*0Sstevel@tonic-gate 		TNF_PROBE_1(s1394_free_addr_blk_error,
460*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_ERROR, "", tnf_string, msg,
461*0Sstevel@tonic-gate 		    "Can't free block not found in used list");
462*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_free_addr_blk_exit,
463*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
464*0Sstevel@tonic-gate 		return (DDI_FAILURE);
465*0Sstevel@tonic-gate 	}
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	/* Put it into the "free" tree */
468*0Sstevel@tonic-gate 	s1394_free_list_insert(hal, blk);
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	/* Unlock the address space "free" list */
471*0Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_free_mutex);
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_addr_blk_exit, S1394_TNF_SL_ARREQ_STACK,
474*0Sstevel@tonic-gate 	    "");
475*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
476*0Sstevel@tonic-gate }
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate /*
479*0Sstevel@tonic-gate  * s1394_reserve_addr_blk()
480*0Sstevel@tonic-gate  *    is similar to s1394_claim_addr_blk(), with the difference being that
481*0Sstevel@tonic-gate  *    after the address block is found, it is marked as "reserved" rather
482*0Sstevel@tonic-gate  *    than inserted into the used tree.  Blocks of data that are marked
483*0Sstevel@tonic-gate  *    "reserved" cannot be unintentionally allocated by a target, they must
484*0Sstevel@tonic-gate  *    be specifically requested by specifying the exact address and size of
485*0Sstevel@tonic-gate  *    the "reserved" block.
486*0Sstevel@tonic-gate  */
487*0Sstevel@tonic-gate int
s1394_reserve_addr_blk(s1394_hal_t * hal,t1394_alloc_addr_t * addr_allocp)488*0Sstevel@tonic-gate s1394_reserve_addr_blk(s1394_hal_t *hal, t1394_alloc_addr_t *addr_allocp)
489*0Sstevel@tonic-gate {
490*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*curr_blk;
491*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*new_blk;
492*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*middle_blk;
493*0Sstevel@tonic-gate 	uint64_t		upper_bound;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_reserve_addr_blk_enter,
496*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	ASSERT(hal != NULL);
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	/* Lock the address space "free" list */
501*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_free_mutex);
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 	/* Find the block in the "free" list */
504*0Sstevel@tonic-gate 	curr_blk = s1394_free_list_search(hal, addr_allocp->aa_address);
505*0Sstevel@tonic-gate 	/* If it wasn't found, it isn't free... */
506*0Sstevel@tonic-gate 	if (curr_blk == NULL) {
507*0Sstevel@tonic-gate 		/* Unlock the address space free list */
508*0Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_free_mutex);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 		TNF_PROBE_1(s1394_reserve_addr_blk_error,
511*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_ERROR, "", tnf_string, msg,
512*0Sstevel@tonic-gate 		    "1394 address space - address unavailable");
513*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_reserve_addr_blk_exit,
514*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
515*0Sstevel@tonic-gate 		return (DDI_FAILURE);
516*0Sstevel@tonic-gate 	}
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 	/* Is this block already reserved? */
519*0Sstevel@tonic-gate 	if (curr_blk->addr_reserved == ADDR_RESERVED) {
520*0Sstevel@tonic-gate 		/* Unlock the address space free list */
521*0Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_free_mutex);
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_reserve_addr_blk_exit,
524*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
525*0Sstevel@tonic-gate 		return (DDI_FAILURE);
526*0Sstevel@tonic-gate 	}
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	/* Does the request fit in the block? */
529*0Sstevel@tonic-gate 	upper_bound = (addr_allocp->aa_address + addr_allocp->aa_length) - 1;
530*0Sstevel@tonic-gate 	if ((upper_bound >= curr_blk->addr_lo) &&
531*0Sstevel@tonic-gate 	    (upper_bound <= curr_blk->addr_hi)) {
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 		/* How does the requested range fit in the current range? */
534*0Sstevel@tonic-gate 		if (addr_allocp->aa_address == curr_blk->addr_lo) {
535*0Sstevel@tonic-gate 			if (upper_bound == curr_blk->addr_hi) {
536*0Sstevel@tonic-gate 				/* Exact fit */
537*0Sstevel@tonic-gate 				curr_blk->addr_reserved = ADDR_RESERVED;
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 				/* Unlock the address space "free" list */
540*0Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_free_mutex);
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 				TNF_PROBE_0_DEBUG(s1394_reserve_addr_blk_exit,
543*0Sstevel@tonic-gate 				    S1394_TNF_SL_ARREQ_STACK, "");
544*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 			} else {
547*0Sstevel@tonic-gate 				/* Front part of range */
548*0Sstevel@tonic-gate 				new_blk = (s1394_addr_space_blk_t *)
549*0Sstevel@tonic-gate 				    kmem_zalloc(sizeof (s1394_addr_space_blk_t),
550*0Sstevel@tonic-gate 				    KM_NOSLEEP);
551*0Sstevel@tonic-gate 				if (new_blk == NULL) {
552*0Sstevel@tonic-gate 					/* Unlock the addr space "free" list */
553*0Sstevel@tonic-gate 					mutex_exit(&hal->addr_space_free_mutex);
554*0Sstevel@tonic-gate 					TNF_PROBE_0(
555*0Sstevel@tonic-gate 					    s1394_reserve_addr_blk_error,
556*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_ERROR, "");
557*0Sstevel@tonic-gate 					TNF_PROBE_0_DEBUG(
558*0Sstevel@tonic-gate 					    s1394_reserve_addr_blk_exit,
559*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_STACK, "");
560*0Sstevel@tonic-gate 					return (DDI_FAILURE);
561*0Sstevel@tonic-gate 				}
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 				new_blk->addr_lo = curr_blk->addr_lo;
564*0Sstevel@tonic-gate 				new_blk->addr_hi = upper_bound;
565*0Sstevel@tonic-gate 				new_blk->addr_type = curr_blk->addr_type;
566*0Sstevel@tonic-gate 				new_blk->addr_reserved = ADDR_RESERVED;
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 				curr_blk->addr_lo = new_blk->addr_hi + 1;
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 				/* Put it back into the "free" list */
571*0Sstevel@tonic-gate 				s1394_free_list_insert(hal, new_blk);
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 				/* Unlock the address space free list */
574*0Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_free_mutex);
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 				TNF_PROBE_0_DEBUG(s1394_reserve_addr_blk_exit,
577*0Sstevel@tonic-gate 				    "stacktrace 1394 s1394 arreq", "");
578*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
579*0Sstevel@tonic-gate 			}
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 		} else {
582*0Sstevel@tonic-gate 			if (upper_bound == curr_blk->addr_hi) {
583*0Sstevel@tonic-gate 				/* End part of range */
584*0Sstevel@tonic-gate 				new_blk = (s1394_addr_space_blk_t *)
585*0Sstevel@tonic-gate 				    kmem_zalloc(sizeof (s1394_addr_space_blk_t),
586*0Sstevel@tonic-gate 				    KM_NOSLEEP);
587*0Sstevel@tonic-gate 				if (new_blk == NULL) {
588*0Sstevel@tonic-gate 					/* Unlock the addr space "free" list */
589*0Sstevel@tonic-gate 					mutex_exit(&hal->addr_space_free_mutex);
590*0Sstevel@tonic-gate 					TNF_PROBE_0(
591*0Sstevel@tonic-gate 					    s1394_reserve_addr_blk_error,
592*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_ERROR, "");
593*0Sstevel@tonic-gate 					TNF_PROBE_0_DEBUG(
594*0Sstevel@tonic-gate 					    s1394_reserve_addr_blk_exit,
595*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_STACK, "");
596*0Sstevel@tonic-gate 					return (DDI_FAILURE);
597*0Sstevel@tonic-gate 				}
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 				new_blk->addr_lo = addr_allocp->aa_address;
600*0Sstevel@tonic-gate 				new_blk->addr_hi = upper_bound;
601*0Sstevel@tonic-gate 				new_blk->addr_type = curr_blk->addr_type;
602*0Sstevel@tonic-gate 				new_blk->addr_reserved = ADDR_RESERVED;
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 				curr_blk->addr_hi = addr_allocp->aa_address - 1;
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 				/* Put it back into the "free" list */
607*0Sstevel@tonic-gate 				s1394_free_list_insert(hal, new_blk);
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 				/* Unlock the address space free list */
610*0Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_free_mutex);
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 				TNF_PROBE_0_DEBUG(s1394_reserve_addr_blk_exit,
613*0Sstevel@tonic-gate 				    S1394_TNF_SL_ARREQ_STACK, "");
614*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate 			} else {
617*0Sstevel@tonic-gate 				/* Middle part of range */
618*0Sstevel@tonic-gate 				new_blk = (s1394_addr_space_blk_t *)
619*0Sstevel@tonic-gate 				    kmem_zalloc(sizeof (s1394_addr_space_blk_t),
620*0Sstevel@tonic-gate 				    KM_NOSLEEP);
621*0Sstevel@tonic-gate 				if (new_blk == NULL) {
622*0Sstevel@tonic-gate 					/* Unlock the addr space "free" list */
623*0Sstevel@tonic-gate 					mutex_exit(&hal->addr_space_free_mutex);
624*0Sstevel@tonic-gate 					TNF_PROBE_0(
625*0Sstevel@tonic-gate 					    s1394_reserve_addr_blk_error,
626*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_ERROR, "");
627*0Sstevel@tonic-gate 					TNF_PROBE_0_DEBUG(
628*0Sstevel@tonic-gate 					    s1394_reserve_addr_blk_exit,
629*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_STACK, "");
630*0Sstevel@tonic-gate 					return (DDI_FAILURE);
631*0Sstevel@tonic-gate 				}
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 				middle_blk = (s1394_addr_space_blk_t *)
634*0Sstevel@tonic-gate 				    kmem_zalloc(sizeof (s1394_addr_space_blk_t),
635*0Sstevel@tonic-gate 				    KM_NOSLEEP);
636*0Sstevel@tonic-gate 				if (middle_blk == NULL) {
637*0Sstevel@tonic-gate 					/* Unlock the addr space "free" list */
638*0Sstevel@tonic-gate 					mutex_exit(&hal->addr_space_free_mutex);
639*0Sstevel@tonic-gate 					kmem_free(new_blk,
640*0Sstevel@tonic-gate 					    sizeof (s1394_addr_space_blk_t));
641*0Sstevel@tonic-gate 					TNF_PROBE_0(
642*0Sstevel@tonic-gate 					    s1394_reserve_addr_blk_error,
643*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_ERROR, "");
644*0Sstevel@tonic-gate 					TNF_PROBE_0_DEBUG(
645*0Sstevel@tonic-gate 					    s1394_reserve_addr_blk_exit,
646*0Sstevel@tonic-gate 					    S1394_TNF_SL_ARREQ_STACK, "");
647*0Sstevel@tonic-gate 					return (DDI_FAILURE);
648*0Sstevel@tonic-gate 				}
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 				middle_blk->addr_lo = addr_allocp->aa_address;
651*0Sstevel@tonic-gate 				middle_blk->addr_hi = upper_bound;
652*0Sstevel@tonic-gate 				new_blk->addr_lo = upper_bound + 1;
653*0Sstevel@tonic-gate 				new_blk->addr_hi = curr_blk->addr_hi;
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 				new_blk->addr_type = curr_blk->addr_type;
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 				middle_blk->addr_type = curr_blk->addr_type;
658*0Sstevel@tonic-gate 				middle_blk->addr_reserved = ADDR_RESERVED;
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 				curr_blk->addr_hi = addr_allocp->aa_address - 1;
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 				/* Put pieces back into the "free" list */
663*0Sstevel@tonic-gate 				s1394_free_list_insert(hal, middle_blk);
664*0Sstevel@tonic-gate 				s1394_free_list_insert(hal, new_blk);
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 				/* Unlock the address space free list */
667*0Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_free_mutex);
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 				TNF_PROBE_0_DEBUG(s1394_reserve_addr_blk_exit,
670*0Sstevel@tonic-gate 				    S1394_TNF_SL_ARREQ_STACK, "");
671*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
672*0Sstevel@tonic-gate 			}
673*0Sstevel@tonic-gate 		}
674*0Sstevel@tonic-gate 	}
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 	/* Unlock the address space free list */
677*0Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_free_mutex);
678*0Sstevel@tonic-gate 
679*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_reserve_addr_blk_exit,
680*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
681*0Sstevel@tonic-gate 	return (DDI_FAILURE);
682*0Sstevel@tonic-gate }
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate /*
685*0Sstevel@tonic-gate  * s1394_init_addr_space()
686*0Sstevel@tonic-gate  *    is called in the HAL attach routine - h1394_attach() - to setup the
687*0Sstevel@tonic-gate  *    initial address space with the appropriate ranges, etc.  At attach,
688*0Sstevel@tonic-gate  *    the HAL specifies not only the type and bounds for each kind of 1394
689*0Sstevel@tonic-gate  *    address space, but also a list of the blocks that are to be marked
690*0Sstevel@tonic-gate  *    �reserved".  Prior to marking the "reserved" ranges the local hosts
691*0Sstevel@tonic-gate  *    CSR registers are allocated/setup in s1394_setup_CSR_space().
692*0Sstevel@tonic-gate  */
693*0Sstevel@tonic-gate int
s1394_init_addr_space(s1394_hal_t * hal)694*0Sstevel@tonic-gate s1394_init_addr_space(s1394_hal_t *hal)
695*0Sstevel@tonic-gate {
696*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*addr_blk;
697*0Sstevel@tonic-gate 	t1394_alloc_addr_t	addr_alloc;
698*0Sstevel@tonic-gate 	h1394_addr_map_t	*addr_map;
699*0Sstevel@tonic-gate 	h1394_addr_map_t	*resv_map;
700*0Sstevel@tonic-gate 	uint_t			num_blks;
701*0Sstevel@tonic-gate 	uint64_t		lo;
702*0Sstevel@tonic-gate 	uint64_t		hi;
703*0Sstevel@tonic-gate 	int			i;
704*0Sstevel@tonic-gate 	int			ret;
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_init_addr_space_enter,
707*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 	/* Setup Address Space */
710*0Sstevel@tonic-gate 	mutex_init(&hal->addr_space_free_mutex,
711*0Sstevel@tonic-gate 	    NULL, MUTEX_DRIVER, NULL);
712*0Sstevel@tonic-gate 	mutex_init(&hal->addr_space_used_mutex,
713*0Sstevel@tonic-gate 	    NULL, MUTEX_DRIVER, hal->halinfo.hw_interrupt);
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 	/* Set address space to NULL (empty) */
716*0Sstevel@tonic-gate 	hal->addr_space_free_list = NULL;
717*0Sstevel@tonic-gate 	hal->addr_space_used_tree = NULL;
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	/* Initialize the 1394 Address Space from HAL's description */
720*0Sstevel@tonic-gate 	num_blks = hal->halinfo.addr_map_num_entries;
721*0Sstevel@tonic-gate 	addr_map = hal->halinfo.addr_map;
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	/* Lock the address space free list */
724*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_free_mutex);
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate 	/* Default to NO posted write space */
727*0Sstevel@tonic-gate 	hal->posted_write_addr_lo = ADDR_LO_INVALID;
728*0Sstevel@tonic-gate 	hal->posted_write_addr_hi = ADDR_HI_INVALID;
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	/* Default to NO physical space */
731*0Sstevel@tonic-gate 	hal->physical_addr_lo = ADDR_LO_INVALID;
732*0Sstevel@tonic-gate 	hal->physical_addr_hi = ADDR_HI_INVALID;
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 	/* Default to NO CSR space */
735*0Sstevel@tonic-gate 	hal->csr_addr_lo = ADDR_LO_INVALID;
736*0Sstevel@tonic-gate 	hal->csr_addr_hi = ADDR_HI_INVALID;
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	/* Default to NO normal space */
739*0Sstevel@tonic-gate 	hal->normal_addr_lo = ADDR_LO_INVALID;
740*0Sstevel@tonic-gate 	hal->normal_addr_hi = ADDR_HI_INVALID;
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	for (i = 0; i < num_blks; i++) {
743*0Sstevel@tonic-gate 		if (addr_map[i].length == 0)
744*0Sstevel@tonic-gate 			continue;
745*0Sstevel@tonic-gate 		addr_blk = kmem_zalloc(sizeof (s1394_addr_space_blk_t),
746*0Sstevel@tonic-gate 		    KM_SLEEP);
747*0Sstevel@tonic-gate 		addr_blk->addr_lo = addr_map[i].address;
748*0Sstevel@tonic-gate 		addr_blk->addr_hi =
749*0Sstevel@tonic-gate 		    (addr_blk->addr_lo + addr_map[i].length) - 1;
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 		switch (addr_map[i].addr_type) {
752*0Sstevel@tonic-gate 		case H1394_ADDR_POSTED_WRITE:
753*0Sstevel@tonic-gate 			addr_blk->addr_type = T1394_ADDR_POSTED_WRITE;
754*0Sstevel@tonic-gate 			hal->posted_write_addr_lo = addr_blk->addr_lo;
755*0Sstevel@tonic-gate 			hal->posted_write_addr_hi = addr_blk->addr_hi;
756*0Sstevel@tonic-gate 			break;
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 		case H1394_ADDR_NORMAL:
759*0Sstevel@tonic-gate 			addr_blk->addr_type = T1394_ADDR_NORMAL;
760*0Sstevel@tonic-gate 			hal->normal_addr_lo = addr_blk->addr_lo;
761*0Sstevel@tonic-gate 			hal->normal_addr_hi = addr_blk->addr_hi;
762*0Sstevel@tonic-gate 			break;
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 		case H1394_ADDR_CSR:
765*0Sstevel@tonic-gate 			addr_blk->addr_type = T1394_ADDR_CSR;
766*0Sstevel@tonic-gate 			hal->csr_addr_lo = addr_blk->addr_lo;
767*0Sstevel@tonic-gate 			hal->csr_addr_hi = addr_blk->addr_hi;
768*0Sstevel@tonic-gate 			break;
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 		case H1394_ADDR_PHYSICAL:
771*0Sstevel@tonic-gate 			addr_blk->addr_type = T1394_ADDR_FIXED;
772*0Sstevel@tonic-gate 			hal->physical_addr_lo = addr_blk->addr_lo;
773*0Sstevel@tonic-gate 			hal->physical_addr_hi = addr_blk->addr_hi;
774*0Sstevel@tonic-gate 			break;
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate 		default:
777*0Sstevel@tonic-gate 			/* Unlock the address space free list */
778*0Sstevel@tonic-gate 			mutex_exit(&hal->addr_space_free_mutex);
779*0Sstevel@tonic-gate 			s1394_destroy_addr_space(hal);
780*0Sstevel@tonic-gate 			TNF_PROBE_1(s1394_init_addr_space_error,
781*0Sstevel@tonic-gate 			    S1394_TNF_SL_ARREQ_ERROR, "", tnf_string, msg,
782*0Sstevel@tonic-gate 			    "Invalid addr_type specified");
783*0Sstevel@tonic-gate 			TNF_PROBE_0_DEBUG(s1394_init_addr_space_exit,
784*0Sstevel@tonic-gate 			    S1394_TNF_SL_ARREQ_STACK, "");
785*0Sstevel@tonic-gate 			return (DDI_FAILURE);
786*0Sstevel@tonic-gate 		}
787*0Sstevel@tonic-gate 		s1394_free_list_insert(hal, addr_blk);
788*0Sstevel@tonic-gate 	}
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	/* Unlock the address space free list */
791*0Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_free_mutex);
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	/* Setup the necessary CSR space */
794*0Sstevel@tonic-gate 	if (s1394_setup_CSR_space(hal) != DDI_SUCCESS) {
795*0Sstevel@tonic-gate 		s1394_destroy_addr_space(hal);
796*0Sstevel@tonic-gate 		TNF_PROBE_1(s1394_init_addr_space_error,
797*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_ERROR, "", tnf_string, msg,
798*0Sstevel@tonic-gate 		    "Failed in s1394_setup_CSR_space()");
799*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_init_addr_space_exit,
800*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
801*0Sstevel@tonic-gate 		return (DDI_FAILURE);
802*0Sstevel@tonic-gate 	}
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate 	/* Handle all the HAL's reserved spaces */
806*0Sstevel@tonic-gate 	num_blks = hal->halinfo.resv_map_num_entries;
807*0Sstevel@tonic-gate 	resv_map = hal->halinfo.resv_map;
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate 	for (i = 0; i < num_blks; i++) {
810*0Sstevel@tonic-gate 		/* Can't reserve physical addresses */
811*0Sstevel@tonic-gate 		lo = resv_map[i].address;
812*0Sstevel@tonic-gate 		hi = (lo + resv_map[i].length) - 1;
813*0Sstevel@tonic-gate 		if ((lo >= hal->physical_addr_lo) &&
814*0Sstevel@tonic-gate 		    (hi <= hal->physical_addr_hi)) {
815*0Sstevel@tonic-gate 			s1394_destroy_addr_space(hal);
816*0Sstevel@tonic-gate 			TNF_PROBE_1(s1394_init_addr_space_error,
817*0Sstevel@tonic-gate 			    S1394_TNF_SL_ARREQ_ERROR, "", tnf_string, msg,
818*0Sstevel@tonic-gate 			    "Attempted to reserve physical memory");
819*0Sstevel@tonic-gate 			TNF_PROBE_0_DEBUG(s1394_init_addr_space_exit,
820*0Sstevel@tonic-gate 			    S1394_TNF_SL_ARREQ_STACK, "");
821*0Sstevel@tonic-gate 			return (DDI_FAILURE);
822*0Sstevel@tonic-gate 		}
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate 		addr_alloc.aa_address = resv_map[i].address;
825*0Sstevel@tonic-gate 		addr_alloc.aa_length = resv_map[i].length;
826*0Sstevel@tonic-gate 		ret = s1394_reserve_addr_blk(hal, &addr_alloc);
827*0Sstevel@tonic-gate 		if (ret != DDI_SUCCESS) {
828*0Sstevel@tonic-gate 			s1394_destroy_addr_space(hal);
829*0Sstevel@tonic-gate 			TNF_PROBE_1(s1394_init_addr_space_error,
830*0Sstevel@tonic-gate 			    S1394_TNF_SL_ARREQ_ERROR, "", tnf_string, msg,
831*0Sstevel@tonic-gate 			    "Unable to reserve 1394 address");
832*0Sstevel@tonic-gate 			TNF_PROBE_0_DEBUG(s1394_init_addr_space_exit,
833*0Sstevel@tonic-gate 			    S1394_TNF_SL_ARREQ_STACK, "");
834*0Sstevel@tonic-gate 			return (DDI_FAILURE);
835*0Sstevel@tonic-gate 		}
836*0Sstevel@tonic-gate 	}
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_init_addr_space_exit, S1394_TNF_SL_ARREQ_STACK,
839*0Sstevel@tonic-gate 	    "");
840*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
841*0Sstevel@tonic-gate }
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate /*
844*0Sstevel@tonic-gate  * s1394_destroy_addr_space()
845*0Sstevel@tonic-gate  *    is necessary for h1394_detach().  It undoes all the work that
846*0Sstevel@tonic-gate  *    s1394_init_addr_space() had setup and more.  By pulling everything out
847*0Sstevel@tonic-gate  *    of the used tree and free list and then freeing the structures,
848*0Sstevel@tonic-gate  *    mutexes, and (if necessary) any backing store memory, the 1394 address
849*0Sstevel@tonic-gate  *    space is completely dismantled.
850*0Sstevel@tonic-gate  */
851*0Sstevel@tonic-gate void
s1394_destroy_addr_space(s1394_hal_t * hal)852*0Sstevel@tonic-gate s1394_destroy_addr_space(s1394_hal_t *hal)
853*0Sstevel@tonic-gate {
854*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*addr_blk;
855*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*next_blk;
856*0Sstevel@tonic-gate 	uint64_t		lo;
857*0Sstevel@tonic-gate 	uint64_t		hi;
858*0Sstevel@tonic-gate 	uint_t			length;
859*0Sstevel@tonic-gate 
860*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_destroy_addr_space_enter,
861*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	/* Lock the address space "used" tree */
864*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_used_mutex);
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate 	addr_blk = hal->addr_space_used_tree;
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	while (addr_blk != NULL) {
869*0Sstevel@tonic-gate 		if (addr_blk->asb_left != NULL) {
870*0Sstevel@tonic-gate 			addr_blk = addr_blk->asb_left;
871*0Sstevel@tonic-gate 		} else if (addr_blk->asb_right != NULL) {
872*0Sstevel@tonic-gate 			addr_blk = addr_blk->asb_right;
873*0Sstevel@tonic-gate 		} else {
874*0Sstevel@tonic-gate 			/* Free any of our own backing store (if necessary) */
875*0Sstevel@tonic-gate 			if ((addr_blk->free_kmem_bufp == B_TRUE) &&
876*0Sstevel@tonic-gate 			    (addr_blk->kmem_bufp != NULL)) {
877*0Sstevel@tonic-gate 				lo = addr_blk->addr_lo;
878*0Sstevel@tonic-gate 				hi = addr_blk->addr_hi;
879*0Sstevel@tonic-gate 				length = (uint_t)((hi - lo) + 1);
880*0Sstevel@tonic-gate 				kmem_free((void *)addr_blk->kmem_bufp, length);
881*0Sstevel@tonic-gate 			}
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 			next_blk = addr_blk->asb_parent;
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 			/* Free the s1394_addr_space_blk_t structure */
886*0Sstevel@tonic-gate 			kmem_free((void *)addr_blk,
887*0Sstevel@tonic-gate 			    sizeof (s1394_addr_space_blk_t));
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 			if (next_blk != NULL) {
890*0Sstevel@tonic-gate 				if (next_blk->asb_left != NULL)
891*0Sstevel@tonic-gate 					next_blk->asb_left = NULL;
892*0Sstevel@tonic-gate 				else
893*0Sstevel@tonic-gate 					next_blk->asb_right = NULL;
894*0Sstevel@tonic-gate 			}
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate 			addr_blk = next_blk;
897*0Sstevel@tonic-gate 		}
898*0Sstevel@tonic-gate 	}
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 	/* Unlock and destroy the address space "used" tree */
901*0Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_used_mutex);
902*0Sstevel@tonic-gate 	mutex_destroy(&hal->addr_space_used_mutex);
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 	/* Lock the address space "free" list */
905*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_free_mutex);
906*0Sstevel@tonic-gate 
907*0Sstevel@tonic-gate 	addr_blk = hal->addr_space_free_list;
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	while (addr_blk != NULL) {
910*0Sstevel@tonic-gate 		next_blk = addr_blk->asb_right;
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate 		/* Free the s1394_addr_space_blk_t structure */
913*0Sstevel@tonic-gate 		kmem_free((void *)addr_blk, sizeof (s1394_addr_space_blk_t));
914*0Sstevel@tonic-gate 		addr_blk = next_blk;
915*0Sstevel@tonic-gate 	}
916*0Sstevel@tonic-gate 
917*0Sstevel@tonic-gate 	/* Unlock & destroy the address space "free" list */
918*0Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_free_mutex);
919*0Sstevel@tonic-gate 	mutex_destroy(&hal->addr_space_free_mutex);
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_destroy_addr_space_exit,
922*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
923*0Sstevel@tonic-gate }
924*0Sstevel@tonic-gate 
925*0Sstevel@tonic-gate /*
926*0Sstevel@tonic-gate  * s1394_free_list_insert()
927*0Sstevel@tonic-gate  *    takes an s1394_addr_space_blk_t and inserts it into the free list in the
928*0Sstevel@tonic-gate  *    appropriate place.  It will concatenate into a single structure on the
929*0Sstevel@tonic-gate  *    list any two neighboring blocks that can be joined (same type,
930*0Sstevel@tonic-gate  *    consecutive addresses, neither is "reserved", etc.)
931*0Sstevel@tonic-gate  */
932*0Sstevel@tonic-gate void
s1394_free_list_insert(s1394_hal_t * hal,s1394_addr_space_blk_t * new_blk)933*0Sstevel@tonic-gate s1394_free_list_insert(s1394_hal_t *hal, s1394_addr_space_blk_t *new_blk)
934*0Sstevel@tonic-gate {
935*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*curr_blk;
936*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*left_blk;
937*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*right_blk;
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_list_insert_enter,
940*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
941*0Sstevel@tonic-gate 
942*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hal->addr_space_free_mutex));
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 	/* Start at the head of the "free" list */
945*0Sstevel@tonic-gate 	curr_blk = hal->addr_space_free_list;
946*0Sstevel@tonic-gate 
947*0Sstevel@tonic-gate 	if (curr_blk != NULL)
948*0Sstevel@tonic-gate 		left_blk = curr_blk->asb_left;
949*0Sstevel@tonic-gate 	else
950*0Sstevel@tonic-gate 		left_blk = NULL;
951*0Sstevel@tonic-gate 
952*0Sstevel@tonic-gate 	while (curr_blk != NULL) {
953*0Sstevel@tonic-gate 		if (new_blk->addr_lo < curr_blk->addr_lo)
954*0Sstevel@tonic-gate 			break;
955*0Sstevel@tonic-gate 		/* Go to the next element in the list */
956*0Sstevel@tonic-gate 		left_blk = curr_blk;
957*0Sstevel@tonic-gate 		curr_blk = curr_blk->asb_right;
958*0Sstevel@tonic-gate 	}
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 	new_blk->asb_left = left_blk;
961*0Sstevel@tonic-gate 	new_blk->asb_right = curr_blk;
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate 	if (left_blk != NULL)
964*0Sstevel@tonic-gate 		left_blk->asb_right = new_blk;
965*0Sstevel@tonic-gate 	else
966*0Sstevel@tonic-gate 		hal->addr_space_free_list = new_blk;
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate 	if (curr_blk != NULL)
969*0Sstevel@tonic-gate 		curr_blk->asb_left = new_blk;
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	right_blk = new_blk->asb_right;
972*0Sstevel@tonic-gate 	left_blk = new_blk->asb_left;
973*0Sstevel@tonic-gate 
974*0Sstevel@tonic-gate 	/* Can we merge with block to the left? */
975*0Sstevel@tonic-gate 	if ((left_blk != NULL) &&
976*0Sstevel@tonic-gate 	    (new_blk->addr_type == left_blk->addr_type) &&
977*0Sstevel@tonic-gate 	    (new_blk->addr_reserved != ADDR_RESERVED) &&
978*0Sstevel@tonic-gate 	    (left_blk->addr_reserved != ADDR_RESERVED) &&
979*0Sstevel@tonic-gate 	    (new_blk->addr_lo == left_blk->addr_hi + 1)) {
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 		new_blk->addr_lo = left_blk->addr_lo;
982*0Sstevel@tonic-gate 		new_blk->asb_left = left_blk->asb_left;
983*0Sstevel@tonic-gate 
984*0Sstevel@tonic-gate 		if (left_blk->asb_left != NULL)
985*0Sstevel@tonic-gate 			left_blk->asb_left->asb_right = new_blk;
986*0Sstevel@tonic-gate 		if (hal->addr_space_free_list == left_blk)
987*0Sstevel@tonic-gate 			hal->addr_space_free_list = new_blk;
988*0Sstevel@tonic-gate 		kmem_free((void *)left_blk, sizeof (s1394_addr_space_blk_t));
989*0Sstevel@tonic-gate 	}
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 	/* Can we merge with block to the right? */
992*0Sstevel@tonic-gate 	if ((right_blk != NULL) &&
993*0Sstevel@tonic-gate 	    (new_blk->addr_type == right_blk->addr_type) &&
994*0Sstevel@tonic-gate 	    (new_blk->addr_reserved != ADDR_RESERVED) &&
995*0Sstevel@tonic-gate 	    (right_blk->addr_reserved != ADDR_RESERVED) &&
996*0Sstevel@tonic-gate 	    (new_blk->addr_hi + 1 == right_blk->addr_lo)) {
997*0Sstevel@tonic-gate 
998*0Sstevel@tonic-gate 		new_blk->addr_hi = right_blk->addr_hi;
999*0Sstevel@tonic-gate 		new_blk->asb_right = right_blk->asb_right;
1000*0Sstevel@tonic-gate 
1001*0Sstevel@tonic-gate 		if (right_blk->asb_right != NULL)
1002*0Sstevel@tonic-gate 			right_blk->asb_right->asb_left = new_blk;
1003*0Sstevel@tonic-gate 		kmem_free((void *)right_blk, sizeof (s1394_addr_space_blk_t));
1004*0Sstevel@tonic-gate 	}
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 	new_blk->addr_enable = 0;
1007*0Sstevel@tonic-gate 	new_blk->kmem_bufp = NULL;
1008*0Sstevel@tonic-gate 	new_blk->addr_arg = NULL;
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_list_insert_exit,
1011*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1012*0Sstevel@tonic-gate }
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate /*
1015*0Sstevel@tonic-gate  * s1394_free_list_search()
1016*0Sstevel@tonic-gate  *    attempts to find a block in the free list that contains the address
1017*0Sstevel@tonic-gate  *    specified.  If none is found, it returns NULL.
1018*0Sstevel@tonic-gate  */
1019*0Sstevel@tonic-gate static s1394_addr_space_blk_t *
s1394_free_list_search(s1394_hal_t * hal,uint64_t addr)1020*0Sstevel@tonic-gate s1394_free_list_search(s1394_hal_t *hal, uint64_t addr)
1021*0Sstevel@tonic-gate {
1022*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*curr_blk;
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_list_search_enter,
1025*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1026*0Sstevel@tonic-gate 
1027*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hal->addr_space_free_mutex));
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 	/* Start at the head of the list */
1030*0Sstevel@tonic-gate 	curr_blk = hal->addr_space_free_list;
1031*0Sstevel@tonic-gate 	while (curr_blk != NULL) {
1032*0Sstevel@tonic-gate 		if ((addr >= curr_blk->addr_lo) && (addr <= curr_blk->addr_hi))
1033*0Sstevel@tonic-gate 			break;
1034*0Sstevel@tonic-gate 		else
1035*0Sstevel@tonic-gate 			curr_blk = curr_blk->asb_right;
1036*0Sstevel@tonic-gate 	}
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_list_search_exit,
1039*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1040*0Sstevel@tonic-gate 	return (curr_blk);
1041*0Sstevel@tonic-gate }
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate /*
1044*0Sstevel@tonic-gate  * s1394_free_list_find()
1045*0Sstevel@tonic-gate  *    attempts to find a block in the free list that is of the specified
1046*0Sstevel@tonic-gate  *    type and size.  It will ignore any blocks marked "reserved".
1047*0Sstevel@tonic-gate  */
1048*0Sstevel@tonic-gate static s1394_addr_space_blk_t *
s1394_free_list_find(s1394_hal_t * hal,uint32_t type,uint32_t length)1049*0Sstevel@tonic-gate s1394_free_list_find(s1394_hal_t *hal, uint32_t type, uint32_t length)
1050*0Sstevel@tonic-gate {
1051*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*curr_blk;
1052*0Sstevel@tonic-gate 	uint64_t		size;
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_list_find_enter, S1394_TNF_SL_ARREQ_STACK,
1055*0Sstevel@tonic-gate 	    "");
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hal->addr_space_free_mutex));
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 	/* Start at the head of the list */
1060*0Sstevel@tonic-gate 	curr_blk = hal->addr_space_free_list;
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate 	while (curr_blk != NULL) {
1063*0Sstevel@tonic-gate 		/* Find block of right "type" - that isn't "reserved" */
1064*0Sstevel@tonic-gate 		if ((curr_blk->addr_type == type) &&
1065*0Sstevel@tonic-gate 		    (curr_blk->addr_reserved != ADDR_RESERVED)) {
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate 			/* CSR allocs above IEEE1394_UCSR_RESERVED_BOUNDARY */
1068*0Sstevel@tonic-gate 			if ((type == T1394_ADDR_CSR) &&
1069*0Sstevel@tonic-gate 			    (curr_blk->addr_lo <
1070*0Sstevel@tonic-gate 				IEEE1394_UCSR_RESERVED_BOUNDARY)) {
1071*0Sstevel@tonic-gate 				curr_blk = curr_blk->asb_right;
1072*0Sstevel@tonic-gate 				continue;
1073*0Sstevel@tonic-gate 			}
1074*0Sstevel@tonic-gate 
1075*0Sstevel@tonic-gate 			size = (curr_blk->addr_hi - curr_blk->addr_lo) + 1;
1076*0Sstevel@tonic-gate 			if (size >= (uint64_t)length)
1077*0Sstevel@tonic-gate 				break;
1078*0Sstevel@tonic-gate 		}
1079*0Sstevel@tonic-gate 		curr_blk = curr_blk->asb_right;
1080*0Sstevel@tonic-gate 	}
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_list_find_exit, S1394_TNF_SL_ARREQ_STACK,
1083*0Sstevel@tonic-gate 	    "");
1084*0Sstevel@tonic-gate 	return (curr_blk);
1085*0Sstevel@tonic-gate }
1086*0Sstevel@tonic-gate 
1087*0Sstevel@tonic-gate /*
1088*0Sstevel@tonic-gate  * s1394_free_list_delete()
1089*0Sstevel@tonic-gate  *    will remove the block pointed to by del_blk from the free list.
1090*0Sstevel@tonic-gate  *    Typically, this is done so that it may be inserted into the used tree.
1091*0Sstevel@tonic-gate  */
1092*0Sstevel@tonic-gate static s1394_addr_space_blk_t *
s1394_free_list_delete(s1394_hal_t * hal,s1394_addr_space_blk_t * del_blk)1093*0Sstevel@tonic-gate s1394_free_list_delete(s1394_hal_t *hal, s1394_addr_space_blk_t *del_blk)
1094*0Sstevel@tonic-gate {
1095*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*left_blk;
1096*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*right_blk;
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_list_delete_enter,
1099*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1100*0Sstevel@tonic-gate 
1101*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hal->addr_space_free_mutex));
1102*0Sstevel@tonic-gate 
1103*0Sstevel@tonic-gate 	left_blk = del_blk->asb_left;
1104*0Sstevel@tonic-gate 	right_blk = del_blk->asb_right;
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 	del_blk->asb_left = NULL;
1107*0Sstevel@tonic-gate 	del_blk->asb_right = NULL;
1108*0Sstevel@tonic-gate 
1109*0Sstevel@tonic-gate 	if (left_blk != NULL)
1110*0Sstevel@tonic-gate 		left_blk->asb_right = right_blk;
1111*0Sstevel@tonic-gate 	else
1112*0Sstevel@tonic-gate 		hal->addr_space_free_list = right_blk;
1113*0Sstevel@tonic-gate 
1114*0Sstevel@tonic-gate 	if (right_blk != NULL)
1115*0Sstevel@tonic-gate 		right_blk->asb_left = left_blk;
1116*0Sstevel@tonic-gate 
1117*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_free_list_delete_exit,
1118*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1119*0Sstevel@tonic-gate 	return (del_blk);
1120*0Sstevel@tonic-gate }
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate /*
1123*0Sstevel@tonic-gate  * s1394_used_tree_insert()
1124*0Sstevel@tonic-gate  *    is used to insert a 1394 address block that has been removed from the
1125*0Sstevel@tonic-gate  *    free list into the used tree.  In the used tree it will be possible
1126*0Sstevel@tonic-gate  *    to search for a given address when an AR request arrives.  Since the
1127*0Sstevel@tonic-gate  *    used tree is implemented as a red-black tree, the insertion is done
1128*0Sstevel@tonic-gate  *    with s1394_tree_insert() which does a simple binary tree insertion.
1129*0Sstevel@tonic-gate  *    It is then followed by cleanup of links and red-black coloring.  This
1130*0Sstevel@tonic-gate  *    particulat implementation of the red-black tree is modified from code
1131*0Sstevel@tonic-gate  *    included in "Introduction to Algorithms" - Cormen, Leiserson, and Rivest,
1132*0Sstevel@tonic-gate  *    pp. 263 - 277.
1133*0Sstevel@tonic-gate  */
1134*0Sstevel@tonic-gate static void
s1394_used_tree_insert(s1394_hal_t * hal,s1394_addr_space_blk_t * x)1135*0Sstevel@tonic-gate s1394_used_tree_insert(s1394_hal_t *hal, s1394_addr_space_blk_t *x)
1136*0Sstevel@tonic-gate {
1137*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*y;
1138*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	**root;
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_used_tree_insert_enter,
1141*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate 	/* Lock the "used" tree */
1144*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_used_mutex);
1145*0Sstevel@tonic-gate 
1146*0Sstevel@tonic-gate 	/* Get the head of the "used" tree */
1147*0Sstevel@tonic-gate 	root = &hal->addr_space_used_tree;
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 	s1394_tree_insert(root, x);
1150*0Sstevel@tonic-gate 
1151*0Sstevel@tonic-gate 	x->asb_color = RED;
1152*0Sstevel@tonic-gate 	while ((x != *root) && (x->asb_parent->asb_color == RED)) {
1153*0Sstevel@tonic-gate 		/* Is x's parent the "left-child" or the "right-child"? */
1154*0Sstevel@tonic-gate 		if (x->asb_parent == x->asb_parent->asb_parent->asb_left) {
1155*0Sstevel@tonic-gate 			/* Left-child, set y to the sibling */
1156*0Sstevel@tonic-gate 			y = x->asb_parent->asb_parent->asb_right;
1157*0Sstevel@tonic-gate 			if ((y != NULL) && (y->asb_color == RED)) {
1158*0Sstevel@tonic-gate 				x->asb_parent->asb_color = BLACK;
1159*0Sstevel@tonic-gate 				y->asb_color = BLACK;
1160*0Sstevel@tonic-gate 				x->asb_parent->asb_parent->asb_color = RED;
1161*0Sstevel@tonic-gate 				x = x->asb_parent->asb_parent;
1162*0Sstevel@tonic-gate 
1163*0Sstevel@tonic-gate 			} else {
1164*0Sstevel@tonic-gate 				if (x == x->asb_parent->asb_right) {
1165*0Sstevel@tonic-gate 					x = x->asb_parent;
1166*0Sstevel@tonic-gate 					s1394_left_rotate(root, x);
1167*0Sstevel@tonic-gate 				}
1168*0Sstevel@tonic-gate 				x->asb_parent->asb_color = BLACK;
1169*0Sstevel@tonic-gate 				x->asb_parent->asb_parent->asb_color = RED;
1170*0Sstevel@tonic-gate 				s1394_right_rotate(root,
1171*0Sstevel@tonic-gate 				    x->asb_parent->asb_parent);
1172*0Sstevel@tonic-gate 			}
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 		} else {
1175*0Sstevel@tonic-gate 			/* Right-child, set y to the sibling */
1176*0Sstevel@tonic-gate 			y = x->asb_parent->asb_parent->asb_left;
1177*0Sstevel@tonic-gate 			if ((y != NULL) && (y->asb_color == RED)) {
1178*0Sstevel@tonic-gate 				x->asb_parent->asb_color = BLACK;
1179*0Sstevel@tonic-gate 				y->asb_color = BLACK;
1180*0Sstevel@tonic-gate 				x->asb_parent->asb_parent->asb_color = RED;
1181*0Sstevel@tonic-gate 				x = x->asb_parent->asb_parent;
1182*0Sstevel@tonic-gate 
1183*0Sstevel@tonic-gate 			} else {
1184*0Sstevel@tonic-gate 				if (x == x->asb_parent->asb_left) {
1185*0Sstevel@tonic-gate 					x = x->asb_parent;
1186*0Sstevel@tonic-gate 					s1394_right_rotate(root, x);
1187*0Sstevel@tonic-gate 				}
1188*0Sstevel@tonic-gate 				x->asb_parent->asb_color = BLACK;
1189*0Sstevel@tonic-gate 				x->asb_parent->asb_parent->asb_color = RED;
1190*0Sstevel@tonic-gate 				s1394_left_rotate(root,
1191*0Sstevel@tonic-gate 				    x->asb_parent->asb_parent);
1192*0Sstevel@tonic-gate 			}
1193*0Sstevel@tonic-gate 		}
1194*0Sstevel@tonic-gate 	}
1195*0Sstevel@tonic-gate 
1196*0Sstevel@tonic-gate 	(*root)->asb_color = BLACK;
1197*0Sstevel@tonic-gate 
1198*0Sstevel@tonic-gate 	/* Unlock the "used" tree */
1199*0Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_used_mutex);
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_used_tree_insert_exit,
1202*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1203*0Sstevel@tonic-gate }
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate /*
1206*0Sstevel@tonic-gate  * s1394_tree_insert()
1207*0Sstevel@tonic-gate  *    is a "helper" function for s1394_used_tree_insert().  It inserts an
1208*0Sstevel@tonic-gate  *    address block into a binary tree (red-black tree), and
1209*0Sstevel@tonic-gate  *    s1394_used_tree_insert() then cleans up the links and colorings, etc.
1210*0Sstevel@tonic-gate  */
1211*0Sstevel@tonic-gate static void
s1394_tree_insert(s1394_addr_space_blk_t ** root,s1394_addr_space_blk_t * z)1212*0Sstevel@tonic-gate s1394_tree_insert(s1394_addr_space_blk_t **root, s1394_addr_space_blk_t *z)
1213*0Sstevel@tonic-gate {
1214*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*y = NULL;
1215*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*x = *root;
1216*0Sstevel@tonic-gate 
1217*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_tree_insert_enter, S1394_TNF_SL_ARREQ_STACK,
1218*0Sstevel@tonic-gate 	    "");
1219*0Sstevel@tonic-gate 
1220*0Sstevel@tonic-gate 	while (x != NULL) {
1221*0Sstevel@tonic-gate 		y = x;
1222*0Sstevel@tonic-gate 		if (z->addr_lo < x->addr_lo)
1223*0Sstevel@tonic-gate 			x = x->asb_left;
1224*0Sstevel@tonic-gate 		else
1225*0Sstevel@tonic-gate 			x = x->asb_right;
1226*0Sstevel@tonic-gate 	}
1227*0Sstevel@tonic-gate 
1228*0Sstevel@tonic-gate 	z->asb_parent = y;
1229*0Sstevel@tonic-gate 	z->asb_right = NULL;
1230*0Sstevel@tonic-gate 	z->asb_left = NULL;
1231*0Sstevel@tonic-gate 
1232*0Sstevel@tonic-gate 	if (y == NULL)
1233*0Sstevel@tonic-gate 		*root = z;
1234*0Sstevel@tonic-gate 	else if (z->addr_lo < y->addr_lo)
1235*0Sstevel@tonic-gate 		y->asb_left = z;
1236*0Sstevel@tonic-gate 	else
1237*0Sstevel@tonic-gate 		y->asb_right = z;
1238*0Sstevel@tonic-gate 
1239*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_tree_insert_exit, S1394_TNF_SL_ARREQ_STACK,
1240*0Sstevel@tonic-gate 	    "");
1241*0Sstevel@tonic-gate }
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate /*
1244*0Sstevel@tonic-gate  * s1394_used_tree_search()
1245*0Sstevel@tonic-gate  *    is called when an AR request arrives.  By calling s1394_tree_search()
1246*0Sstevel@tonic-gate  *    with the destination address, it can quickly find a block for that
1247*0Sstevel@tonic-gate  *    address (if one exists in the used tree) and return a pointer to it.
1248*0Sstevel@tonic-gate  */
1249*0Sstevel@tonic-gate s1394_addr_space_blk_t *
s1394_used_tree_search(s1394_hal_t * hal,uint64_t addr)1250*0Sstevel@tonic-gate s1394_used_tree_search(s1394_hal_t *hal, uint64_t addr)
1251*0Sstevel@tonic-gate {
1252*0Sstevel@tonic-gate 	s1394_addr_space_blk_t *curr_blk;
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_used_tree_search_enter,
1255*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1256*0Sstevel@tonic-gate 
1257*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hal->addr_space_used_mutex));
1258*0Sstevel@tonic-gate 
1259*0Sstevel@tonic-gate 	/* Search the HAL's "used" tree for this address */
1260*0Sstevel@tonic-gate 	curr_blk = s1394_tree_search(hal->addr_space_used_tree, addr);
1261*0Sstevel@tonic-gate 
1262*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_used_tree_search_exit,
1263*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1264*0Sstevel@tonic-gate 	return (curr_blk);
1265*0Sstevel@tonic-gate }
1266*0Sstevel@tonic-gate 
1267*0Sstevel@tonic-gate /*
1268*0Sstevel@tonic-gate  * s1394_tree_search()
1269*0Sstevel@tonic-gate  *    is a "helper" function for s1394_used_tree_search().  It implements a
1270*0Sstevel@tonic-gate  *    typical binary tree search with the address as the search key.
1271*0Sstevel@tonic-gate  */
1272*0Sstevel@tonic-gate static s1394_addr_space_blk_t *
s1394_tree_search(s1394_addr_space_blk_t * x,uint64_t address)1273*0Sstevel@tonic-gate s1394_tree_search(s1394_addr_space_blk_t *x, uint64_t address)
1274*0Sstevel@tonic-gate {
1275*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_tree_search_enter, S1394_TNF_SL_ARREQ_STACK,
1276*0Sstevel@tonic-gate 	    "");
1277*0Sstevel@tonic-gate 
1278*0Sstevel@tonic-gate 	while (x != NULL) {
1279*0Sstevel@tonic-gate 		if (x->addr_lo > address)
1280*0Sstevel@tonic-gate 			x = x->asb_left;
1281*0Sstevel@tonic-gate 		else if (x->addr_hi < address)
1282*0Sstevel@tonic-gate 			x = x->asb_right;
1283*0Sstevel@tonic-gate 		else
1284*0Sstevel@tonic-gate 			break;
1285*0Sstevel@tonic-gate 	}
1286*0Sstevel@tonic-gate 
1287*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_tree_search_exit, S1394_TNF_SL_ARREQ_STACK,
1288*0Sstevel@tonic-gate 	    "");
1289*0Sstevel@tonic-gate 	return (x);
1290*0Sstevel@tonic-gate }
1291*0Sstevel@tonic-gate 
1292*0Sstevel@tonic-gate /*
1293*0Sstevel@tonic-gate  * s1394_used_tree_delete()
1294*0Sstevel@tonic-gate  *    is used to remove an address block from the used tree.  This is
1295*0Sstevel@tonic-gate  *    necessary when address spaces are freed.  The removal is accomplished
1296*0Sstevel@tonic-gate  *    in two steps, the removal done by this function and the cleanup done
1297*0Sstevel@tonic-gate  *    by s1394_used_tree_delete_fixup().
1298*0Sstevel@tonic-gate  */
1299*0Sstevel@tonic-gate s1394_addr_space_blk_t *
s1394_used_tree_delete(s1394_hal_t * hal,s1394_addr_space_blk_t * z)1300*0Sstevel@tonic-gate s1394_used_tree_delete(s1394_hal_t *hal, s1394_addr_space_blk_t *z)
1301*0Sstevel@tonic-gate {
1302*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*y;
1303*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*x;
1304*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*w;
1305*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*p;
1306*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	**root;
1307*0Sstevel@tonic-gate 	int			old_color;
1308*0Sstevel@tonic-gate 	int			side_of_x;
1309*0Sstevel@tonic-gate 
1310*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_used_tree_delete_enter,
1311*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1312*0Sstevel@tonic-gate 
1313*0Sstevel@tonic-gate 	/* Lock the "used" tree */
1314*0Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_used_mutex);
1315*0Sstevel@tonic-gate 
1316*0Sstevel@tonic-gate 	/* Get the head of the "used" tree */
1317*0Sstevel@tonic-gate 	root = &hal->addr_space_used_tree;
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 	if ((z->asb_left == NULL) || (z->asb_right == NULL))
1320*0Sstevel@tonic-gate 		y = z;
1321*0Sstevel@tonic-gate 	else
1322*0Sstevel@tonic-gate 		y = s1394_tree_successor(z);
1323*0Sstevel@tonic-gate 
1324*0Sstevel@tonic-gate 	if (y->asb_parent == z)
1325*0Sstevel@tonic-gate 		p = y;
1326*0Sstevel@tonic-gate 	else
1327*0Sstevel@tonic-gate 		p = y->asb_parent;
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate 	if (y->asb_left != NULL) {
1330*0Sstevel@tonic-gate 		x = y->asb_left;
1331*0Sstevel@tonic-gate 		if ((y != *root) && (y == y->asb_parent->asb_left)) {
1332*0Sstevel@tonic-gate 			w = y->asb_parent->asb_right;
1333*0Sstevel@tonic-gate 			side_of_x = LEFT;
1334*0Sstevel@tonic-gate 		}
1335*0Sstevel@tonic-gate 
1336*0Sstevel@tonic-gate 		if ((y != *root) && (y == y->asb_parent->asb_right)) {
1337*0Sstevel@tonic-gate 			w = y->asb_parent->asb_left;
1338*0Sstevel@tonic-gate 			side_of_x = RIGHT;
1339*0Sstevel@tonic-gate 		}
1340*0Sstevel@tonic-gate 
1341*0Sstevel@tonic-gate 	} else {
1342*0Sstevel@tonic-gate 		x = y->asb_right;
1343*0Sstevel@tonic-gate 		if ((y != *root) && (y == y->asb_parent->asb_left)) {
1344*0Sstevel@tonic-gate 			w = y->asb_parent->asb_right;
1345*0Sstevel@tonic-gate 			side_of_x = LEFT;
1346*0Sstevel@tonic-gate 		}
1347*0Sstevel@tonic-gate 
1348*0Sstevel@tonic-gate 		if ((y != *root) && (y == y->asb_parent->asb_right)) {
1349*0Sstevel@tonic-gate 			w = y->asb_parent->asb_left;
1350*0Sstevel@tonic-gate 			side_of_x = RIGHT;
1351*0Sstevel@tonic-gate 		}
1352*0Sstevel@tonic-gate 
1353*0Sstevel@tonic-gate 	}
1354*0Sstevel@tonic-gate 
1355*0Sstevel@tonic-gate 	if (x != NULL)
1356*0Sstevel@tonic-gate 		x->asb_parent = y->asb_parent;
1357*0Sstevel@tonic-gate 
1358*0Sstevel@tonic-gate 	if (y->asb_parent == NULL)
1359*0Sstevel@tonic-gate 		*root = x;
1360*0Sstevel@tonic-gate 	else if (y == y->asb_parent->asb_left)
1361*0Sstevel@tonic-gate 		y->asb_parent->asb_left = x;
1362*0Sstevel@tonic-gate 	else
1363*0Sstevel@tonic-gate 		y->asb_parent->asb_right = x;
1364*0Sstevel@tonic-gate 
1365*0Sstevel@tonic-gate 	old_color = y->asb_color;
1366*0Sstevel@tonic-gate 
1367*0Sstevel@tonic-gate 	/* Substitute the y-node for the z-node (deleted) */
1368*0Sstevel@tonic-gate 	if (y != z) {
1369*0Sstevel@tonic-gate 		y->asb_color = z->asb_color;
1370*0Sstevel@tonic-gate 		y->asb_parent = z->asb_parent;
1371*0Sstevel@tonic-gate 		if (z->asb_parent != NULL) {
1372*0Sstevel@tonic-gate 			if (z->asb_parent->asb_left == z)
1373*0Sstevel@tonic-gate 				z->asb_parent->asb_left = y;
1374*0Sstevel@tonic-gate 			if (z->asb_parent->asb_right == z)
1375*0Sstevel@tonic-gate 				z->asb_parent->asb_right = y;
1376*0Sstevel@tonic-gate 		}
1377*0Sstevel@tonic-gate 
1378*0Sstevel@tonic-gate 		y->asb_left = z->asb_left;
1379*0Sstevel@tonic-gate 		if (z->asb_left != NULL)
1380*0Sstevel@tonic-gate 			z->asb_left->asb_parent = y;
1381*0Sstevel@tonic-gate 		y->asb_right = z->asb_right;
1382*0Sstevel@tonic-gate 		if (z->asb_right != NULL)
1383*0Sstevel@tonic-gate 			z->asb_right->asb_parent = y;
1384*0Sstevel@tonic-gate 
1385*0Sstevel@tonic-gate 		if (z == *root)
1386*0Sstevel@tonic-gate 			*root = y;
1387*0Sstevel@tonic-gate 	}
1388*0Sstevel@tonic-gate 
1389*0Sstevel@tonic-gate 	z->asb_parent = NULL;
1390*0Sstevel@tonic-gate 	z->asb_right = NULL;
1391*0Sstevel@tonic-gate 	z->asb_left = NULL;
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 	if (old_color == BLACK)
1394*0Sstevel@tonic-gate 		s1394_used_tree_delete_fixup(root, p, x, w, side_of_x);
1395*0Sstevel@tonic-gate 
1396*0Sstevel@tonic-gate 	/* Unlock the "used" tree */
1397*0Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_used_mutex);
1398*0Sstevel@tonic-gate 
1399*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_used_tree_delete_exit,
1400*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1401*0Sstevel@tonic-gate 	return (z);
1402*0Sstevel@tonic-gate }
1403*0Sstevel@tonic-gate 
1404*0Sstevel@tonic-gate /*
1405*0Sstevel@tonic-gate  * s1394_used_tree_delete_fixup()
1406*0Sstevel@tonic-gate  *    is the "helper" function for s1394_used_tree_delete().  It is used to
1407*0Sstevel@tonic-gate  *    cleanup/enforce the red-black coloring in the tree.
1408*0Sstevel@tonic-gate  */
1409*0Sstevel@tonic-gate static void
s1394_used_tree_delete_fixup(s1394_addr_space_blk_t ** root,s1394_addr_space_blk_t * p,s1394_addr_space_blk_t * x,s1394_addr_space_blk_t * w,int side_of_x)1410*0Sstevel@tonic-gate s1394_used_tree_delete_fixup(s1394_addr_space_blk_t **root,
1411*0Sstevel@tonic-gate     s1394_addr_space_blk_t *p, s1394_addr_space_blk_t *x,
1412*0Sstevel@tonic-gate     s1394_addr_space_blk_t *w, int side_of_x)
1413*0Sstevel@tonic-gate {
1414*0Sstevel@tonic-gate 	boolean_t	first_time;
1415*0Sstevel@tonic-gate 
1416*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_used_tree_delete_fixup_enter,
1417*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1418*0Sstevel@tonic-gate 
1419*0Sstevel@tonic-gate 	first_time = B_TRUE;
1420*0Sstevel@tonic-gate 	while ((x != *root) && ((x == NULL) || (x->asb_color == BLACK))) {
1421*0Sstevel@tonic-gate 		if (((first_time == B_TRUE) && (side_of_x == LEFT)) ||
1422*0Sstevel@tonic-gate 		    ((first_time == B_FALSE) && (x == p->asb_left))) {
1423*0Sstevel@tonic-gate 
1424*0Sstevel@tonic-gate 			if (first_time != B_TRUE)
1425*0Sstevel@tonic-gate 				w = p->asb_right;
1426*0Sstevel@tonic-gate 
1427*0Sstevel@tonic-gate 			if ((w != NULL) && (w->asb_color == RED)) {
1428*0Sstevel@tonic-gate 				w->asb_color = BLACK;
1429*0Sstevel@tonic-gate 				p->asb_color = RED;
1430*0Sstevel@tonic-gate 				s1394_left_rotate(root, p);
1431*0Sstevel@tonic-gate 				w = p->asb_right;
1432*0Sstevel@tonic-gate 			}
1433*0Sstevel@tonic-gate 
1434*0Sstevel@tonic-gate 			if (w == NULL) {
1435*0Sstevel@tonic-gate 				x = p;
1436*0Sstevel@tonic-gate 				p = p->asb_parent;
1437*0Sstevel@tonic-gate 				first_time = B_FALSE;
1438*0Sstevel@tonic-gate 
1439*0Sstevel@tonic-gate 			} else if (((w->asb_left == NULL) ||
1440*0Sstevel@tonic-gate 			    (w->asb_left->asb_color == BLACK)) &&
1441*0Sstevel@tonic-gate 			    ((w->asb_right == NULL) ||
1442*0Sstevel@tonic-gate 			    (w->asb_right->asb_color == BLACK))) {
1443*0Sstevel@tonic-gate 				w->asb_color = RED;
1444*0Sstevel@tonic-gate 				x = p;
1445*0Sstevel@tonic-gate 				p = p->asb_parent;
1446*0Sstevel@tonic-gate 				first_time = B_FALSE;
1447*0Sstevel@tonic-gate 
1448*0Sstevel@tonic-gate 			} else {
1449*0Sstevel@tonic-gate 				if ((w->asb_right == NULL) ||
1450*0Sstevel@tonic-gate 				    (w->asb_right->asb_color == BLACK)) {
1451*0Sstevel@tonic-gate 					w->asb_left->asb_color = BLACK;
1452*0Sstevel@tonic-gate 					w->asb_color = RED;
1453*0Sstevel@tonic-gate 					s1394_right_rotate(root, w);
1454*0Sstevel@tonic-gate 					w = p->asb_right;
1455*0Sstevel@tonic-gate 				}
1456*0Sstevel@tonic-gate 
1457*0Sstevel@tonic-gate 				w->asb_color = p->asb_color;
1458*0Sstevel@tonic-gate 				p->asb_color = BLACK;
1459*0Sstevel@tonic-gate 				if (w->asb_right != NULL)
1460*0Sstevel@tonic-gate 					w->asb_right->asb_color = BLACK;
1461*0Sstevel@tonic-gate 				s1394_left_rotate(root, p);
1462*0Sstevel@tonic-gate 				x = *root;
1463*0Sstevel@tonic-gate 				first_time = B_FALSE;
1464*0Sstevel@tonic-gate 			}
1465*0Sstevel@tonic-gate 
1466*0Sstevel@tonic-gate 		} else {
1467*0Sstevel@tonic-gate 			if (first_time == B_FALSE)
1468*0Sstevel@tonic-gate 				w = p->asb_left;
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 			if ((w != NULL) && (w->asb_color == RED)) {
1471*0Sstevel@tonic-gate 				w->asb_color = BLACK;
1472*0Sstevel@tonic-gate 				p->asb_color = RED;
1473*0Sstevel@tonic-gate 				s1394_right_rotate(root, p);
1474*0Sstevel@tonic-gate 				w = p->asb_left;
1475*0Sstevel@tonic-gate 			}
1476*0Sstevel@tonic-gate 
1477*0Sstevel@tonic-gate 			if (w == NULL) {
1478*0Sstevel@tonic-gate 				x = p;
1479*0Sstevel@tonic-gate 				p = p->asb_parent;
1480*0Sstevel@tonic-gate 				first_time = B_FALSE;
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 			} else if (((w->asb_left == NULL) ||
1483*0Sstevel@tonic-gate 			    (w->asb_left->asb_color == BLACK)) &&
1484*0Sstevel@tonic-gate 			    ((w->asb_right == NULL) ||
1485*0Sstevel@tonic-gate 			    (w->asb_right->asb_color == BLACK))) {
1486*0Sstevel@tonic-gate 				w->asb_color = RED;
1487*0Sstevel@tonic-gate 				x = p;
1488*0Sstevel@tonic-gate 				p = p->asb_parent;
1489*0Sstevel@tonic-gate 				first_time = B_FALSE;
1490*0Sstevel@tonic-gate 
1491*0Sstevel@tonic-gate 			} else {
1492*0Sstevel@tonic-gate 				if ((w->asb_left == NULL) ||
1493*0Sstevel@tonic-gate 				    (w->asb_left->asb_color == BLACK)) {
1494*0Sstevel@tonic-gate 
1495*0Sstevel@tonic-gate 					w->asb_right->asb_color = BLACK;
1496*0Sstevel@tonic-gate 					w->asb_color = RED;
1497*0Sstevel@tonic-gate 					s1394_left_rotate(root, w);
1498*0Sstevel@tonic-gate 					w = p->asb_left;
1499*0Sstevel@tonic-gate 				}
1500*0Sstevel@tonic-gate 
1501*0Sstevel@tonic-gate 				w->asb_color = p->asb_color;
1502*0Sstevel@tonic-gate 				p->asb_color = BLACK;
1503*0Sstevel@tonic-gate 				if (w->asb_left != NULL)
1504*0Sstevel@tonic-gate 					w->asb_left->asb_color = BLACK;
1505*0Sstevel@tonic-gate 				s1394_right_rotate(root, p);
1506*0Sstevel@tonic-gate 				x = *root;
1507*0Sstevel@tonic-gate 				first_time = B_FALSE;
1508*0Sstevel@tonic-gate 			}
1509*0Sstevel@tonic-gate 		}
1510*0Sstevel@tonic-gate 	}
1511*0Sstevel@tonic-gate 	if (x != NULL)
1512*0Sstevel@tonic-gate 		x->asb_color = BLACK;
1513*0Sstevel@tonic-gate 
1514*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_used_tree_delete_fixup_exit,
1515*0Sstevel@tonic-gate 	    S1394_TNF_SL_ARREQ_STACK, "");
1516*0Sstevel@tonic-gate }
1517*0Sstevel@tonic-gate 
1518*0Sstevel@tonic-gate /*
1519*0Sstevel@tonic-gate  * s1394_left_rotate()
1520*0Sstevel@tonic-gate  *    is necessary with a red-black tree to help maintain the coloring in the
1521*0Sstevel@tonic-gate  *    tree as items are inserted and removed.  Its operation, the opposite of
1522*0Sstevel@tonic-gate  *    s1394_right_rotate(), is a fundamental operation on the red-black tree.
1523*0Sstevel@tonic-gate  */
1524*0Sstevel@tonic-gate static void
s1394_left_rotate(s1394_addr_space_blk_t ** root,s1394_addr_space_blk_t * x)1525*0Sstevel@tonic-gate s1394_left_rotate(s1394_addr_space_blk_t **root, s1394_addr_space_blk_t *x)
1526*0Sstevel@tonic-gate {
1527*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*y;
1528*0Sstevel@tonic-gate 
1529*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_left_rotate_enter, S1394_TNF_SL_ARREQ_STACK,
1530*0Sstevel@tonic-gate 	    "");
1531*0Sstevel@tonic-gate 
1532*0Sstevel@tonic-gate 	y = x->asb_right;
1533*0Sstevel@tonic-gate 	x->asb_right = y->asb_left;
1534*0Sstevel@tonic-gate 
1535*0Sstevel@tonic-gate 	if (y->asb_left != NULL)
1536*0Sstevel@tonic-gate 		y->asb_left->asb_parent = x;
1537*0Sstevel@tonic-gate 
1538*0Sstevel@tonic-gate 	y->asb_parent = x->asb_parent;
1539*0Sstevel@tonic-gate 	if (x->asb_parent == NULL)
1540*0Sstevel@tonic-gate 		*root = y;
1541*0Sstevel@tonic-gate 	else if (x == x->asb_parent->asb_left)
1542*0Sstevel@tonic-gate 		x->asb_parent->asb_left = y;
1543*0Sstevel@tonic-gate 	else
1544*0Sstevel@tonic-gate 		x->asb_parent->asb_right = y;
1545*0Sstevel@tonic-gate 
1546*0Sstevel@tonic-gate 	y->asb_left = x;
1547*0Sstevel@tonic-gate 	x->asb_parent = y;
1548*0Sstevel@tonic-gate 
1549*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_left_rotate_exit, S1394_TNF_SL_ARREQ_STACK,
1550*0Sstevel@tonic-gate 	    "");
1551*0Sstevel@tonic-gate }
1552*0Sstevel@tonic-gate 
1553*0Sstevel@tonic-gate /*
1554*0Sstevel@tonic-gate  * s1394_right_rotate()
1555*0Sstevel@tonic-gate  *    is necessary with a red-black tree to help maintain the coloring in the
1556*0Sstevel@tonic-gate  *    tree as items are inserted and removed.  Its operation, the opposite of
1557*0Sstevel@tonic-gate  *    s1394_left_rotate(), is a fundamental operation on the red-black tree.
1558*0Sstevel@tonic-gate  */
1559*0Sstevel@tonic-gate static void
s1394_right_rotate(s1394_addr_space_blk_t ** root,s1394_addr_space_blk_t * x)1560*0Sstevel@tonic-gate s1394_right_rotate(s1394_addr_space_blk_t **root, s1394_addr_space_blk_t *x)
1561*0Sstevel@tonic-gate {
1562*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*y;
1563*0Sstevel@tonic-gate 
1564*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_right_rotate_enter, S1394_TNF_SL_ARREQ_STACK,
1565*0Sstevel@tonic-gate 	    "");
1566*0Sstevel@tonic-gate 
1567*0Sstevel@tonic-gate 	y = x->asb_left;
1568*0Sstevel@tonic-gate 	x->asb_left = y->asb_right;
1569*0Sstevel@tonic-gate 
1570*0Sstevel@tonic-gate 	if (y->asb_right != NULL)
1571*0Sstevel@tonic-gate 		y->asb_right->asb_parent = x;
1572*0Sstevel@tonic-gate 
1573*0Sstevel@tonic-gate 	y->asb_parent = x->asb_parent;
1574*0Sstevel@tonic-gate 	if (x->asb_parent == NULL)
1575*0Sstevel@tonic-gate 		*root = y;
1576*0Sstevel@tonic-gate 	else if (x == x->asb_parent->asb_right)
1577*0Sstevel@tonic-gate 		x->asb_parent->asb_right = y;
1578*0Sstevel@tonic-gate 	else
1579*0Sstevel@tonic-gate 		x->asb_parent->asb_left = y;
1580*0Sstevel@tonic-gate 
1581*0Sstevel@tonic-gate 	y->asb_right = x;
1582*0Sstevel@tonic-gate 	x->asb_parent = y;
1583*0Sstevel@tonic-gate 
1584*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_right_rotate_exit, S1394_TNF_SL_ARREQ_STACK,
1585*0Sstevel@tonic-gate 	    "");
1586*0Sstevel@tonic-gate }
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate /*
1589*0Sstevel@tonic-gate  * s1394_tree_minimum()
1590*0Sstevel@tonic-gate  *    is used to find the smallest key in a binary tree.
1591*0Sstevel@tonic-gate  */
1592*0Sstevel@tonic-gate static s1394_addr_space_blk_t *
s1394_tree_minimum(s1394_addr_space_blk_t * x)1593*0Sstevel@tonic-gate s1394_tree_minimum(s1394_addr_space_blk_t *x)
1594*0Sstevel@tonic-gate {
1595*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_tree_minimum_enter, S1394_TNF_SL_ARREQ_STACK,
1596*0Sstevel@tonic-gate 	    "");
1597*0Sstevel@tonic-gate 
1598*0Sstevel@tonic-gate 	while (x->asb_left != NULL)
1599*0Sstevel@tonic-gate 		x = x->asb_left;
1600*0Sstevel@tonic-gate 
1601*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_tree_minimum_exit, S1394_TNF_SL_ARREQ_STACK,
1602*0Sstevel@tonic-gate 	    "");
1603*0Sstevel@tonic-gate 	return (x);
1604*0Sstevel@tonic-gate }
1605*0Sstevel@tonic-gate 
1606*0Sstevel@tonic-gate /*
1607*0Sstevel@tonic-gate  * s1394_tree_successor()
1608*0Sstevel@tonic-gate  *    is used to find the next largest key is a binary tree, given a starting
1609*0Sstevel@tonic-gate  *    point.
1610*0Sstevel@tonic-gate  */
1611*0Sstevel@tonic-gate static s1394_addr_space_blk_t *
s1394_tree_successor(s1394_addr_space_blk_t * x)1612*0Sstevel@tonic-gate s1394_tree_successor(s1394_addr_space_blk_t *x)
1613*0Sstevel@tonic-gate {
1614*0Sstevel@tonic-gate 	s1394_addr_space_blk_t	*y;
1615*0Sstevel@tonic-gate 
1616*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_tree_successor_enter, S1394_TNF_SL_ARREQ_STACK,
1617*0Sstevel@tonic-gate 	    "");
1618*0Sstevel@tonic-gate 
1619*0Sstevel@tonic-gate 	if (x->asb_right != NULL) {
1620*0Sstevel@tonic-gate 		y = s1394_tree_minimum(x->asb_right);
1621*0Sstevel@tonic-gate 
1622*0Sstevel@tonic-gate 		TNF_PROBE_0_DEBUG(s1394_tree_successor_exit,
1623*0Sstevel@tonic-gate 		    S1394_TNF_SL_ARREQ_STACK, "");
1624*0Sstevel@tonic-gate 		return (y);
1625*0Sstevel@tonic-gate 	}
1626*0Sstevel@tonic-gate 
1627*0Sstevel@tonic-gate 	y = x->asb_parent;
1628*0Sstevel@tonic-gate 	while ((y != NULL) && (x == y->asb_right)) {
1629*0Sstevel@tonic-gate 		x = y;
1630*0Sstevel@tonic-gate 		y = y->asb_parent;
1631*0Sstevel@tonic-gate 	}
1632*0Sstevel@tonic-gate 
1633*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_tree_successor_exit, S1394_TNF_SL_ARREQ_STACK,
1634*0Sstevel@tonic-gate 	    "");
1635*0Sstevel@tonic-gate 	return (y);
1636*0Sstevel@tonic-gate }
1637*0Sstevel@tonic-gate 
1638*0Sstevel@tonic-gate /*
1639*0Sstevel@tonic-gate  * s1394_is_posted_write()
1640*0Sstevel@tonic-gate  *    returns a B_TRUE if the given address is in the "posted write" range
1641*0Sstevel@tonic-gate  *    of the given HAL's 1394 address space and B_FALSE if it isn't.
1642*0Sstevel@tonic-gate  */
1643*0Sstevel@tonic-gate boolean_t
s1394_is_posted_write(s1394_hal_t * hal,uint64_t addr)1644*0Sstevel@tonic-gate s1394_is_posted_write(s1394_hal_t *hal, uint64_t addr)
1645*0Sstevel@tonic-gate {
1646*0Sstevel@tonic-gate 	addr = addr & IEEE1394_ADDR_OFFSET_MASK;
1647*0Sstevel@tonic-gate 
1648*0Sstevel@tonic-gate 	if ((addr >= hal->posted_write_addr_lo) &&
1649*0Sstevel@tonic-gate 	    (addr <= hal->posted_write_addr_hi))
1650*0Sstevel@tonic-gate 		return (B_TRUE);
1651*0Sstevel@tonic-gate 	else
1652*0Sstevel@tonic-gate 		return (B_FALSE);
1653*0Sstevel@tonic-gate }
1654*0Sstevel@tonic-gate 
1655*0Sstevel@tonic-gate /*
1656*0Sstevel@tonic-gate  * s1394_is_physical_addr()
1657*0Sstevel@tonic-gate  *    returns a B_TRUE if the given address is in the "physical" range of
1658*0Sstevel@tonic-gate  *    the given HAL's 1394 address space and B_FALSE if it isn't.
1659*0Sstevel@tonic-gate  */
1660*0Sstevel@tonic-gate boolean_t
s1394_is_physical_addr(s1394_hal_t * hal,uint64_t addr)1661*0Sstevel@tonic-gate s1394_is_physical_addr(s1394_hal_t *hal, uint64_t addr)
1662*0Sstevel@tonic-gate {
1663*0Sstevel@tonic-gate 	addr = addr & IEEE1394_ADDR_OFFSET_MASK;
1664*0Sstevel@tonic-gate 
1665*0Sstevel@tonic-gate 	if ((addr >= hal->physical_addr_lo) &&
1666*0Sstevel@tonic-gate 	    (addr <= hal->physical_addr_hi))
1667*0Sstevel@tonic-gate 		return (B_TRUE);
1668*0Sstevel@tonic-gate 	else
1669*0Sstevel@tonic-gate 		return (B_FALSE);
1670*0Sstevel@tonic-gate }
1671*0Sstevel@tonic-gate 
1672*0Sstevel@tonic-gate /*
1673*0Sstevel@tonic-gate  * s1394_is_csr_addr()
1674*0Sstevel@tonic-gate  *    returns a B_TRUE if the given address is in the "CSR" range of the
1675*0Sstevel@tonic-gate  *    given HAL's 1394 address space and B_FALSE if it isn't.
1676*0Sstevel@tonic-gate  */
1677*0Sstevel@tonic-gate boolean_t
s1394_is_csr_addr(s1394_hal_t * hal,uint64_t addr)1678*0Sstevel@tonic-gate s1394_is_csr_addr(s1394_hal_t *hal, uint64_t addr)
1679*0Sstevel@tonic-gate {
1680*0Sstevel@tonic-gate 	addr = addr & IEEE1394_ADDR_OFFSET_MASK;
1681*0Sstevel@tonic-gate 
1682*0Sstevel@tonic-gate 	if ((addr >= hal->csr_addr_lo) &&
1683*0Sstevel@tonic-gate 	    (addr <= hal->csr_addr_hi))
1684*0Sstevel@tonic-gate 		return (B_TRUE);
1685*0Sstevel@tonic-gate 	else
1686*0Sstevel@tonic-gate 		return (B_FALSE);
1687*0Sstevel@tonic-gate }
1688*0Sstevel@tonic-gate 
1689*0Sstevel@tonic-gate /*
1690*0Sstevel@tonic-gate  * s1394_is_normal_addr()
1691*0Sstevel@tonic-gate  *    returns a B_TRUE if the given address is in the "normal" range of
1692*0Sstevel@tonic-gate  *    the given HAL's 1394 address space and B_FALSE if it isn't.
1693*0Sstevel@tonic-gate  */
1694*0Sstevel@tonic-gate boolean_t
s1394_is_normal_addr(s1394_hal_t * hal,uint64_t addr)1695*0Sstevel@tonic-gate s1394_is_normal_addr(s1394_hal_t *hal, uint64_t addr)
1696*0Sstevel@tonic-gate {
1697*0Sstevel@tonic-gate 	addr = addr & IEEE1394_ADDR_OFFSET_MASK;
1698*0Sstevel@tonic-gate 
1699*0Sstevel@tonic-gate 	if ((addr >= hal->normal_addr_lo) &&
1700*0Sstevel@tonic-gate 	    (addr <= hal->normal_addr_hi))
1701*0Sstevel@tonic-gate 		return (B_TRUE);
1702*0Sstevel@tonic-gate 	else
1703*0Sstevel@tonic-gate 		return (B_FALSE);
1704*0Sstevel@tonic-gate }
1705