1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate * All rights reserved.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * DDI nodeid management ...
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate * Keep a sorted free list of available nodeids.
43*7c478bd9Sstevel@tonic-gate * Allocating a nodeid won't cause memory allocation.
44*7c478bd9Sstevel@tonic-gate * Freeing a nodeid does cause memory allocation.
45*7c478bd9Sstevel@tonic-gate */
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate struct available {
48*7c478bd9Sstevel@tonic-gate uint32_t nodeid;
49*7c478bd9Sstevel@tonic-gate uint32_t count;
50*7c478bd9Sstevel@tonic-gate struct available *next;
51*7c478bd9Sstevel@tonic-gate struct available *prev;
52*7c478bd9Sstevel@tonic-gate };
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate * The initial seed of available nodeids: 1 .. 0x10000000
56*7c478bd9Sstevel@tonic-gate * 0, -1 (DEVI_PSEUDO_NODEID) and -2 (DEVI_SID_NODEID) are illegal values
57*7c478bd9Sstevel@tonic-gate * and may not be used. Although this code is fully capable of dealing
58*7c478bd9Sstevel@tonic-gate * with a full 32-bit range of nodeids, we use a low numeric range of
59*7c478bd9Sstevel@tonic-gate * nodeids as an optimization to avoid overlap with promif nodeids.
60*7c478bd9Sstevel@tonic-gate */
61*7c478bd9Sstevel@tonic-gate #define OUR_NODEID_MIN ((uint32_t)1)
62*7c478bd9Sstevel@tonic-gate #define OUR_NODEID_MAX ((uint32_t)0x10000000)
63*7c478bd9Sstevel@tonic-gate #define OUR_NODEID_COUNT ((uint32_t)(OUR_NODEID_MAX - OUR_NODEID_MIN))
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate static struct available seed = {
66*7c478bd9Sstevel@tonic-gate OUR_NODEID_MIN, OUR_NODEID_COUNT, NULL, NULL
67*7c478bd9Sstevel@tonic-gate };
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate /*
70*7c478bd9Sstevel@tonic-gate * head of the available list ...
71*7c478bd9Sstevel@tonic-gate */
72*7c478bd9Sstevel@tonic-gate static struct available *nhead;
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate * A single lock for the list ...
76*7c478bd9Sstevel@tonic-gate */
77*7c478bd9Sstevel@tonic-gate static kmutex_t nodeid_lock;
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate * Helper functions to manage the list ...
81*7c478bd9Sstevel@tonic-gate */
82*7c478bd9Sstevel@tonic-gate static struct available *
np_alloc(int kmflag)83*7c478bd9Sstevel@tonic-gate np_alloc(int kmflag)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate return (kmem_zalloc(sizeof (struct available), kmflag));
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate static void
np_free(struct available * np)89*7c478bd9Sstevel@tonic-gate np_free(struct available *np)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate kmem_free(np, sizeof (struct available));
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate * Unlink a node from the list ... the lock must be held.
96*7c478bd9Sstevel@tonic-gate */
97*7c478bd9Sstevel@tonic-gate static void
np_unlink(struct available * np)98*7c478bd9Sstevel@tonic-gate np_unlink(struct available *np)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate if (np->prev)
101*7c478bd9Sstevel@tonic-gate np->prev->next = np->next;
102*7c478bd9Sstevel@tonic-gate else
103*7c478bd9Sstevel@tonic-gate nhead = np->next;
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate if (np->next)
106*7c478bd9Sstevel@tonic-gate np->next->prev = np->prev;
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate /*
110*7c478bd9Sstevel@tonic-gate * Insert fp before np ... the lock must be held.
111*7c478bd9Sstevel@tonic-gate */
112*7c478bd9Sstevel@tonic-gate static void
np_insert(struct available * fp,struct available * np)113*7c478bd9Sstevel@tonic-gate np_insert(struct available *fp, struct available *np)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate fp->prev = np->prev;
116*7c478bd9Sstevel@tonic-gate fp->next = np;
117*7c478bd9Sstevel@tonic-gate
118*7c478bd9Sstevel@tonic-gate if (np->prev)
119*7c478bd9Sstevel@tonic-gate np->prev->next = fp;
120*7c478bd9Sstevel@tonic-gate else
121*7c478bd9Sstevel@tonic-gate nhead = fp;
122*7c478bd9Sstevel@tonic-gate np->prev = fp;
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate * Add fp to the end of the list ... the lock must be held.
127*7c478bd9Sstevel@tonic-gate */
128*7c478bd9Sstevel@tonic-gate static void
np_add(struct available * fp)129*7c478bd9Sstevel@tonic-gate np_add(struct available *fp)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate struct available *np;
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate if (nhead == NULL) {
134*7c478bd9Sstevel@tonic-gate nhead = fp;
135*7c478bd9Sstevel@tonic-gate return;
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate for (np = nhead; np->next != NULL; np = np->next)
139*7c478bd9Sstevel@tonic-gate /* empty */;
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate np->next = fp;
142*7c478bd9Sstevel@tonic-gate fp->prev = np;
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate * If this entry and the next entry are consecutive, coalesce the
147*7c478bd9Sstevel@tonic-gate * two entries into a single entry ... the lock must be held.
148*7c478bd9Sstevel@tonic-gate * If the entry can be coalesced, the extra entry is freed.
149*7c478bd9Sstevel@tonic-gate */
150*7c478bd9Sstevel@tonic-gate static void
np_coalesce(struct available * np)151*7c478bd9Sstevel@tonic-gate np_coalesce(struct available *np)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate struct available *xp;
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate xp = np->next;
156*7c478bd9Sstevel@tonic-gate if (xp == NULL)
157*7c478bd9Sstevel@tonic-gate return;
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate if ((np->nodeid + np->count) == xp->nodeid) {
160*7c478bd9Sstevel@tonic-gate np->count += xp->count;
161*7c478bd9Sstevel@tonic-gate np_unlink(xp);
162*7c478bd9Sstevel@tonic-gate np_free(xp);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate void
impl_ddi_init_nodeid(void)167*7c478bd9Sstevel@tonic-gate impl_ddi_init_nodeid(void)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate struct available *np;
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate mutex_init(&nodeid_lock, NULL, MUTEX_DEFAULT, NULL);
172*7c478bd9Sstevel@tonic-gate
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate * Copy the seed into kmem_alloc-ed memory so we don't have to
175*7c478bd9Sstevel@tonic-gate * worry about not freeing it later.
176*7c478bd9Sstevel@tonic-gate */
177*7c478bd9Sstevel@tonic-gate np = np_alloc(KM_SLEEP);
178*7c478bd9Sstevel@tonic-gate *np = seed;
179*7c478bd9Sstevel@tonic-gate nhead = np;
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate int
impl_ddi_alloc_nodeid(int * nodeid)183*7c478bd9Sstevel@tonic-gate impl_ddi_alloc_nodeid(int *nodeid)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate struct available *np;
186*7c478bd9Sstevel@tonic-gate int x;
187*7c478bd9Sstevel@tonic-gate int unlinked = 0;
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate mutex_enter(&nodeid_lock);
190*7c478bd9Sstevel@tonic-gate
191*7c478bd9Sstevel@tonic-gate if (nhead == NULL) {
192*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
193*7c478bd9Sstevel@tonic-gate *nodeid = 0;
194*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate
197*7c478bd9Sstevel@tonic-gate np = nhead;
198*7c478bd9Sstevel@tonic-gate x = (int)((unsigned int)np->nodeid);
199*7c478bd9Sstevel@tonic-gate ++np->nodeid;
200*7c478bd9Sstevel@tonic-gate --np->count;
201*7c478bd9Sstevel@tonic-gate if (np->count == 0) {
202*7c478bd9Sstevel@tonic-gate np_unlink(np);
203*7c478bd9Sstevel@tonic-gate unlinked = 1;
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate if (unlinked)
208*7c478bd9Sstevel@tonic-gate np_free(np);
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate ASSERT(x != 0);
211*7c478bd9Sstevel@tonic-gate ASSERT(x != DEVI_PSEUDO_NODEID);
212*7c478bd9Sstevel@tonic-gate ASSERT(x != DEVI_SID_NODEID);
213*7c478bd9Sstevel@tonic-gate
214*7c478bd9Sstevel@tonic-gate *nodeid = x;
215*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate
218*7c478bd9Sstevel@tonic-gate void
impl_ddi_free_nodeid(int n)219*7c478bd9Sstevel@tonic-gate impl_ddi_free_nodeid(int n)
220*7c478bd9Sstevel@tonic-gate {
221*7c478bd9Sstevel@tonic-gate uint32_t nodeid = (uint32_t)n;
222*7c478bd9Sstevel@tonic-gate struct available *np, *fp;
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate ASSERT(n != 0);
225*7c478bd9Sstevel@tonic-gate ASSERT(n != DEVI_PSEUDO_NODEID);
226*7c478bd9Sstevel@tonic-gate ASSERT(n != DEVI_SID_NODEID);
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate /*
229*7c478bd9Sstevel@tonic-gate * Allocate memory wihout holding the lock in case we need it.
230*7c478bd9Sstevel@tonic-gate * If we don't use it, we'll free it.
231*7c478bd9Sstevel@tonic-gate */
232*7c478bd9Sstevel@tonic-gate fp = np_alloc(KM_SLEEP);
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate mutex_enter(&nodeid_lock);
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate * Insert nodeid in the appropriate place in our sorted available
238*7c478bd9Sstevel@tonic-gate * list. Maintain the list as we do it.
239*7c478bd9Sstevel@tonic-gate */
240*7c478bd9Sstevel@tonic-gate for (np = nhead; np != NULL; np = np->next) {
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate * Add to the beginning of this entry?
243*7c478bd9Sstevel@tonic-gate */
244*7c478bd9Sstevel@tonic-gate if ((nodeid + 1) == np->nodeid) {
245*7c478bd9Sstevel@tonic-gate np->nodeid = nodeid;
246*7c478bd9Sstevel@tonic-gate ++np->count;
247*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
248*7c478bd9Sstevel@tonic-gate np_free(fp);
249*7c478bd9Sstevel@tonic-gate return;
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate /*
252*7c478bd9Sstevel@tonic-gate * Add to end of this entry? (If yes, try to coalesce
253*7c478bd9Sstevel@tonic-gate * this entry with the next entry.)
254*7c478bd9Sstevel@tonic-gate */
255*7c478bd9Sstevel@tonic-gate if (nodeid == (np->nodeid + np->count)) {
256*7c478bd9Sstevel@tonic-gate ++np->count;
257*7c478bd9Sstevel@tonic-gate np_coalesce(np);
258*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
259*7c478bd9Sstevel@tonic-gate np_free(fp);
260*7c478bd9Sstevel@tonic-gate return;
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate /*
263*7c478bd9Sstevel@tonic-gate * Does it belong before this entry? (new entry)
264*7c478bd9Sstevel@tonic-gate */
265*7c478bd9Sstevel@tonic-gate if (nodeid < np->nodeid) {
266*7c478bd9Sstevel@tonic-gate fp->nodeid = nodeid;
267*7c478bd9Sstevel@tonic-gate fp->count = 1;
268*7c478bd9Sstevel@tonic-gate np_insert(fp, np);
269*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
270*7c478bd9Sstevel@tonic-gate return;
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate if (nodeid < (np->nodeid + np->count))
273*7c478bd9Sstevel@tonic-gate cmn_err(CE_PANIC, "impl_ddi_free_nodeid: "
274*7c478bd9Sstevel@tonic-gate "nodeid %x already free", n);
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate
277*7c478bd9Sstevel@tonic-gate /*
278*7c478bd9Sstevel@tonic-gate * Add a new list item to the end of the list ...
279*7c478bd9Sstevel@tonic-gate */
280*7c478bd9Sstevel@tonic-gate fp->nodeid = nodeid;
281*7c478bd9Sstevel@tonic-gate fp->count = 1;
282*7c478bd9Sstevel@tonic-gate np_add(fp);
283*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
284*7c478bd9Sstevel@tonic-gate }
285*7c478bd9Sstevel@tonic-gate
286*7c478bd9Sstevel@tonic-gate /*
287*7c478bd9Sstevel@tonic-gate * Remove (take) nodeid n off of the available list.
288*7c478bd9Sstevel@tonic-gate * Returns 0 if successful or -1 if it fails.
289*7c478bd9Sstevel@tonic-gate *
290*7c478bd9Sstevel@tonic-gate * A failure indicates we were called with KM_NOSLEEP and we
291*7c478bd9Sstevel@tonic-gate * couldn't allocate memory when we needed to.
292*7c478bd9Sstevel@tonic-gate */
293*7c478bd9Sstevel@tonic-gate int
impl_ddi_take_nodeid(int n,int kmflag)294*7c478bd9Sstevel@tonic-gate impl_ddi_take_nodeid(int n, int kmflag)
295*7c478bd9Sstevel@tonic-gate {
296*7c478bd9Sstevel@tonic-gate uint32_t nodeid = (uint32_t)n;
297*7c478bd9Sstevel@tonic-gate struct available *np, *fp;
298*7c478bd9Sstevel@tonic-gate int unlinked = 0;
299*7c478bd9Sstevel@tonic-gate
300*7c478bd9Sstevel@tonic-gate ASSERT(n != 0);
301*7c478bd9Sstevel@tonic-gate ASSERT(n != DEVI_PSEUDO_NODEID);
302*7c478bd9Sstevel@tonic-gate ASSERT(n != DEVI_SID_NODEID);
303*7c478bd9Sstevel@tonic-gate
304*7c478bd9Sstevel@tonic-gate /*
305*7c478bd9Sstevel@tonic-gate * If this nodeid is not within the range of nodeids we
306*7c478bd9Sstevel@tonic-gate * manage, we simply succeed. The initial seed may be
307*7c478bd9Sstevel@tonic-gate * setup so that promif nodeids fall outside our range.
308*7c478bd9Sstevel@tonic-gate */
309*7c478bd9Sstevel@tonic-gate if ((nodeid < OUR_NODEID_MIN) || (nodeid > OUR_NODEID_MAX))
310*7c478bd9Sstevel@tonic-gate return (0);
311*7c478bd9Sstevel@tonic-gate
312*7c478bd9Sstevel@tonic-gate /*
313*7c478bd9Sstevel@tonic-gate * Allocate memory wihout holding the lock in case we need it.
314*7c478bd9Sstevel@tonic-gate * If we don't use it, we'll free it.
315*7c478bd9Sstevel@tonic-gate */
316*7c478bd9Sstevel@tonic-gate fp = np_alloc(kmflag); /* if KM_NOSLEEP, fp may be NULL */
317*7c478bd9Sstevel@tonic-gate
318*7c478bd9Sstevel@tonic-gate mutex_enter(&nodeid_lock);
319*7c478bd9Sstevel@tonic-gate
320*7c478bd9Sstevel@tonic-gate /*
321*7c478bd9Sstevel@tonic-gate * Find nodeid in our list, if it exists, 'take' it.
322*7c478bd9Sstevel@tonic-gate */
323*7c478bd9Sstevel@tonic-gate for (np = nhead; np != NULL; np = np->next) {
324*7c478bd9Sstevel@tonic-gate
325*7c478bd9Sstevel@tonic-gate /*
326*7c478bd9Sstevel@tonic-gate * If it's less than this entry, it's not available...
327*7c478bd9Sstevel@tonic-gate */
328*7c478bd9Sstevel@tonic-gate if (nodeid < np->nodeid)
329*7c478bd9Sstevel@tonic-gate break;
330*7c478bd9Sstevel@tonic-gate
331*7c478bd9Sstevel@tonic-gate /*
332*7c478bd9Sstevel@tonic-gate * If it's the first entry in this list item, take it ...
333*7c478bd9Sstevel@tonic-gate */
334*7c478bd9Sstevel@tonic-gate if ((nodeid) == np->nodeid) {
335*7c478bd9Sstevel@tonic-gate ++np->nodeid;
336*7c478bd9Sstevel@tonic-gate --np->count;
337*7c478bd9Sstevel@tonic-gate if (np->count == 0) {
338*7c478bd9Sstevel@tonic-gate np_unlink(np);
339*7c478bd9Sstevel@tonic-gate ++unlinked;
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
342*7c478bd9Sstevel@tonic-gate if (fp)
343*7c478bd9Sstevel@tonic-gate np_free(fp);
344*7c478bd9Sstevel@tonic-gate if (unlinked)
345*7c478bd9Sstevel@tonic-gate np_free(np);
346*7c478bd9Sstevel@tonic-gate return (0);
347*7c478bd9Sstevel@tonic-gate }
348*7c478bd9Sstevel@tonic-gate
349*7c478bd9Sstevel@tonic-gate /*
350*7c478bd9Sstevel@tonic-gate * If it's the last entry in this list item, take it ...
351*7c478bd9Sstevel@tonic-gate * The count can't be 1 otherwise it would have matched
352*7c478bd9Sstevel@tonic-gate * the beginning of list case, above.
353*7c478bd9Sstevel@tonic-gate */
354*7c478bd9Sstevel@tonic-gate if (nodeid == (np->nodeid + np->count - 1)) {
355*7c478bd9Sstevel@tonic-gate --np->count;
356*7c478bd9Sstevel@tonic-gate ASSERT(np->count != 0);
357*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
358*7c478bd9Sstevel@tonic-gate if (fp)
359*7c478bd9Sstevel@tonic-gate np_free(fp);
360*7c478bd9Sstevel@tonic-gate return (0);
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate
363*7c478bd9Sstevel@tonic-gate /*
364*7c478bd9Sstevel@tonic-gate * Is it in the middle of this entry? If it is, we'll
365*7c478bd9Sstevel@tonic-gate * have to split np into two items, removing nodeid
366*7c478bd9Sstevel@tonic-gate * from the middle of the list item.
367*7c478bd9Sstevel@tonic-gate */
368*7c478bd9Sstevel@tonic-gate if (nodeid < (np->nodeid + np->count - 1)) {
369*7c478bd9Sstevel@tonic-gate if (fp == NULL) {
370*7c478bd9Sstevel@tonic-gate /*
371*7c478bd9Sstevel@tonic-gate * We were called with KM_NOSLEEP and
372*7c478bd9Sstevel@tonic-gate * were unable to allocate memory.
373*7c478bd9Sstevel@tonic-gate */
374*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
375*7c478bd9Sstevel@tonic-gate return (-1);
376*7c478bd9Sstevel@tonic-gate }
377*7c478bd9Sstevel@tonic-gate /*
378*7c478bd9Sstevel@tonic-gate * Split np, removing nodeid from the middle of
379*7c478bd9Sstevel@tonic-gate * this entry. We already know it isn't on either
380*7c478bd9Sstevel@tonic-gate * end of of this entry, so we know we have to split it.
381*7c478bd9Sstevel@tonic-gate */
382*7c478bd9Sstevel@tonic-gate fp->nodeid = np->nodeid;
383*7c478bd9Sstevel@tonic-gate fp->count = nodeid - np->nodeid;
384*7c478bd9Sstevel@tonic-gate np->nodeid = nodeid + 1;
385*7c478bd9Sstevel@tonic-gate np->count = np->count - fp->count - 1;
386*7c478bd9Sstevel@tonic-gate ASSERT((fp->count != 0) && (np->count != 0));
387*7c478bd9Sstevel@tonic-gate ASSERT(np->nodeid == (fp->nodeid + fp->count + 1));
388*7c478bd9Sstevel@tonic-gate np_insert(fp, np);
389*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
390*7c478bd9Sstevel@tonic-gate return (0);
391*7c478bd9Sstevel@tonic-gate }
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate
394*7c478bd9Sstevel@tonic-gate /*
395*7c478bd9Sstevel@tonic-gate * Apparently the nodeid is not available ...
396*7c478bd9Sstevel@tonic-gate */
397*7c478bd9Sstevel@tonic-gate mutex_exit(&nodeid_lock);
398*7c478bd9Sstevel@tonic-gate
399*7c478bd9Sstevel@tonic-gate if (fp)
400*7c478bd9Sstevel@tonic-gate np_free(fp);
401*7c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?impl_ddi_take_nodeid: nodeid %x may not "
402*7c478bd9Sstevel@tonic-gate "be unique\n", nodeid);
403*7c478bd9Sstevel@tonic-gate return (0);
404*7c478bd9Sstevel@tonic-gate }
405