17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*aa92d85bSgt145670 * Common Development and Distribution License (the "License").
6*aa92d85bSgt145670 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*aa92d85bSgt145670 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * Minor number allocation for various protocol modules.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <inet/common.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate typedef struct inet_arena {
427c478bd9Sstevel@tonic-gate vmem_t *ineta_arena; /* Minor number arena */
437c478bd9Sstevel@tonic-gate minor_t ineta_maxminor; /* max minor number in the arena */
447c478bd9Sstevel@tonic-gate } inet_arena_t;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate void *
inet_minor_create(char * name,dev_t min_dev,dev_t max_dev,int kmflags)47*aa92d85bSgt145670 inet_minor_create(char *name, dev_t min_dev, dev_t max_dev, int kmflags)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate inet_arena_t *arena = kmem_alloc(sizeof (inet_arena_t), kmflags);
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate if (arena != NULL) {
52*aa92d85bSgt145670 arena->ineta_maxminor = max_dev;
537c478bd9Sstevel@tonic-gate arena->ineta_arena = vmem_create(name,
547c478bd9Sstevel@tonic-gate (void *)min_dev, arena->ineta_maxminor - min_dev + 1,
557c478bd9Sstevel@tonic-gate 1, NULL, NULL, NULL, 1, kmflags | VMC_IDENTIFIER);
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate if (arena->ineta_arena == NULL) {
587c478bd9Sstevel@tonic-gate kmem_free(arena, sizeof (inet_arena_t));
597c478bd9Sstevel@tonic-gate arena = NULL;
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate return (arena);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate void
inet_minor_destroy(void * a)677c478bd9Sstevel@tonic-gate inet_minor_destroy(void *a)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate inet_arena_t *arena = (inet_arena_t *)a;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate if (arena != NULL) {
727c478bd9Sstevel@tonic-gate vmem_destroy(arena->ineta_arena);
737c478bd9Sstevel@tonic-gate kmem_free(arena, sizeof (inet_arena_t));
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate dev_t
inet_minor_alloc(void * arena)78*aa92d85bSgt145670 inet_minor_alloc(void *arena)
797c478bd9Sstevel@tonic-gate {
80*aa92d85bSgt145670 return ((dev_t)vmem_alloc(((inet_arena_t *)arena)->ineta_arena,
81*aa92d85bSgt145670 1, VM_NOSLEEP));
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate void
inet_minor_free(void * arena,dev_t dev)85*aa92d85bSgt145670 inet_minor_free(void *arena, dev_t dev)
867c478bd9Sstevel@tonic-gate {
87*aa92d85bSgt145670 ASSERT((dev != OPENFAIL) && (dev != 0) && (dev <= MAXMIN));
88*aa92d85bSgt145670 vmem_free(((inet_arena_t *)arena)->ineta_arena, (void *)dev, 1);
897c478bd9Sstevel@tonic-gate }
90ff550d0eSmasputra
91ff550d0eSmasputra /*
92ff550d0eSmasputra * This function is used to free a message that has gone through
93ff550d0eSmasputra * mi_copyin processing which modifies the M_IOCTL mblk's b_next
94ff550d0eSmasputra * and b_prev pointers. We use this function to set b_next/b_prev
95ff550d0eSmasputra * to NULL and free them.
96ff550d0eSmasputra */
97ff550d0eSmasputra void
inet_freemsg(mblk_t * mp)98ff550d0eSmasputra inet_freemsg(mblk_t *mp)
99ff550d0eSmasputra {
100ff550d0eSmasputra mblk_t *bp = mp;
101ff550d0eSmasputra
102ff550d0eSmasputra for (; bp != NULL; bp = bp->b_cont) {
103ff550d0eSmasputra bp->b_prev = NULL;
104ff550d0eSmasputra bp->b_next = NULL;
105ff550d0eSmasputra }
106ff550d0eSmasputra freemsg(mp);
107ff550d0eSmasputra }
108