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