4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1999 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * DDI nodeid management ...
33 #include <sys/types.h>
34 #include <sys/ksynch.h>
36 #include <sys/cmn_err.h>
38 #include <sys/sunddi.h>
39 #include <sys/ddi_impldefs.h>
40 #include <sys/ddi_implfuncs.h>
41 #include <sys/debug.h>
44 * Keep a sorted free list of available nodeids.
45 * Allocating a nodeid won't cause memory allocation.
46 * Freeing a nodeid does cause memory allocation.
52 struct available
*next
;
53 struct available
*prev
;
57 * The initial seed of available nodeids: 1 .. 0x10000000
58 * 0, -1 (DEVI_PSEUDO_NODEID) and -2 (DEVI_SID_NODEID) are illegal values
59 * and may not be used. Although this code is fully capable of dealing
60 * with a full 32-bit range of nodeids, we use a low numeric range of
61 * nodeids as an optimization to avoid overlap with promif nodeids.
63 #define OUR_NODEID_MIN ((uint32_t)1)
64 #define OUR_NODEID_MAX ((uint32_t)0x10000000)
65 #define OUR_NODEID_COUNT ((uint32_t)(OUR_NODEID_MAX - OUR_NODEID_MIN))
67 static struct available seed
= {
68 OUR_NODEID_MIN
, OUR_NODEID_COUNT
, NULL
, NULL
72 * head of the available list ...
74 static struct available
*nhead
;
77 * A single lock for the list ...
79 static kmutex_t nodeid_lock
;
82 * Helper functions to manage the list ...
84 static struct available
*
87 return (kmem_zalloc(sizeof (struct available
), kmflag
));
91 np_free(struct available
*np
)
93 kmem_free(np
, sizeof (struct available
));
97 * Unlink a node from the list ... the lock must be held.
100 np_unlink(struct available
*np
)
103 np
->prev
->next
= np
->next
;
108 np
->next
->prev
= np
->prev
;
112 * Insert fp before np ... the lock must be held.
115 np_insert(struct available
*fp
, struct available
*np
)
128 * Add fp to the end of the list ... the lock must be held.
131 np_add(struct available
*fp
)
133 struct available
*np
;
140 for (np
= nhead
; np
->next
!= NULL
; np
= np
->next
)
148 * If this entry and the next entry are consecutive, coalesce the
149 * two entries into a single entry ... the lock must be held.
150 * If the entry can be coalesced, the extra entry is freed.
153 np_coalesce(struct available
*np
)
155 struct available
*xp
;
161 if ((np
->nodeid
+ np
->count
) == xp
->nodeid
) {
162 np
->count
+= xp
->count
;
169 impl_ddi_init_nodeid(void)
171 struct available
*np
;
173 mutex_init(&nodeid_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
176 * Copy the seed into kmem_alloc-ed memory so we don't have to
177 * worry about not freeing it later.
179 np
= np_alloc(KM_SLEEP
);
185 impl_ddi_alloc_nodeid(int *nodeid
)
187 struct available
*np
;
191 mutex_enter(&nodeid_lock
);
194 mutex_exit(&nodeid_lock
);
196 return (DDI_FAILURE
);
200 x
= (int)((unsigned int)np
->nodeid
);
203 if (np
->count
== 0) {
207 mutex_exit(&nodeid_lock
);
213 ASSERT(x
!= DEVI_PSEUDO_NODEID
);
214 ASSERT(x
!= DEVI_SID_NODEID
);
217 return (DDI_SUCCESS
);
221 impl_ddi_free_nodeid(int n
)
223 uint32_t nodeid
= (uint32_t)n
;
224 struct available
*np
, *fp
;
227 ASSERT(n
!= DEVI_PSEUDO_NODEID
);
228 ASSERT(n
!= DEVI_SID_NODEID
);
231 * Allocate memory wihout holding the lock in case we need it.
232 * If we don't use it, we'll free it.
234 fp
= np_alloc(KM_SLEEP
);
236 mutex_enter(&nodeid_lock
);
239 * Insert nodeid in the appropriate place in our sorted available
240 * list. Maintain the list as we do it.
242 for (np
= nhead
; np
!= NULL
; np
= np
->next
) {
244 * Add to the beginning of this entry?
246 if ((nodeid
+ 1) == np
->nodeid
) {
249 mutex_exit(&nodeid_lock
);
254 * Add to end of this entry? (If yes, try to coalesce
255 * this entry with the next entry.)
257 if (nodeid
== (np
->nodeid
+ np
->count
)) {
260 mutex_exit(&nodeid_lock
);
265 * Does it belong before this entry? (new entry)
267 if (nodeid
< np
->nodeid
) {
271 mutex_exit(&nodeid_lock
);
274 if (nodeid
< (np
->nodeid
+ np
->count
))
275 cmn_err(CE_PANIC
, "impl_ddi_free_nodeid: "
276 "nodeid %x already free", n
);
280 * Add a new list item to the end of the list ...
285 mutex_exit(&nodeid_lock
);
289 * Remove (take) nodeid n off of the available list.
290 * Returns 0 if successful or -1 if it fails.
292 * A failure indicates we were called with KM_NOSLEEP and we
293 * couldn't allocate memory when we needed to.
296 impl_ddi_take_nodeid(int n
, int kmflag
)
298 uint32_t nodeid
= (uint32_t)n
;
299 struct available
*np
, *fp
;
303 ASSERT(n
!= DEVI_PSEUDO_NODEID
);
304 ASSERT(n
!= DEVI_SID_NODEID
);
307 * If this nodeid is not within the range of nodeids we
308 * manage, we simply succeed. The initial seed may be
309 * setup so that promif nodeids fall outside our range.
311 if ((nodeid
< OUR_NODEID_MIN
) || (nodeid
> OUR_NODEID_MAX
))
315 * Allocate memory wihout holding the lock in case we need it.
316 * If we don't use it, we'll free it.
318 fp
= np_alloc(kmflag
); /* if KM_NOSLEEP, fp may be NULL */
320 mutex_enter(&nodeid_lock
);
323 * Find nodeid in our list, if it exists, 'take' it.
325 for (np
= nhead
; np
!= NULL
; np
= np
->next
) {
328 * If it's less than this entry, it's not available...
330 if (nodeid
< np
->nodeid
)
334 * If it's the first entry in this list item, take it ...
336 if ((nodeid
) == np
->nodeid
) {
339 if (np
->count
== 0) {
343 mutex_exit(&nodeid_lock
);
352 * If it's the last entry in this list item, take it ...
353 * The count can't be 1 otherwise it would have matched
354 * the beginning of list case, above.
356 if (nodeid
== (np
->nodeid
+ np
->count
- 1)) {
358 ASSERT(np
->count
!= 0);
359 mutex_exit(&nodeid_lock
);
366 * Is it in the middle of this entry? If it is, we'll
367 * have to split np into two items, removing nodeid
368 * from the middle of the list item.
370 if (nodeid
< (np
->nodeid
+ np
->count
- 1)) {
373 * We were called with KM_NOSLEEP and
374 * were unable to allocate memory.
376 mutex_exit(&nodeid_lock
);
380 * Split np, removing nodeid from the middle of
381 * this entry. We already know it isn't on either
382 * end of of this entry, so we know we have to split it.
384 fp
->nodeid
= np
->nodeid
;
385 fp
->count
= nodeid
- np
->nodeid
;
386 np
->nodeid
= nodeid
+ 1;
387 np
->count
= np
->count
- fp
->count
- 1;
388 ASSERT((fp
->count
!= 0) && (np
->count
!= 0));
389 ASSERT(np
->nodeid
== (fp
->nodeid
+ fp
->count
+ 1));
391 mutex_exit(&nodeid_lock
);
397 * Apparently the nodeid is not available ...
399 mutex_exit(&nodeid_lock
);
403 cmn_err(CE_CONT
, "?impl_ddi_take_nodeid: nodeid %x may not "
404 "be unique\n", nodeid
);