xref: /illumos-gate/usr/src/uts/sun4v/sys/vio_util.h (revision 844e62a3ec8c8ff5175bb35d1c38446e060730f6)
1d10e4ef2Snarayan /*
2d10e4ef2Snarayan  * CDDL HEADER START
3d10e4ef2Snarayan  *
4d10e4ef2Snarayan  * The contents of this file are subject to the terms of the
5d10e4ef2Snarayan  * Common Development and Distribution License (the "License").
6d10e4ef2Snarayan  * You may not use this file except in compliance with the License.
7d10e4ef2Snarayan  *
8d10e4ef2Snarayan  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9d10e4ef2Snarayan  * or http://www.opensolaris.org/os/licensing.
10d10e4ef2Snarayan  * See the License for the specific language governing permissions
11d10e4ef2Snarayan  * and limitations under the License.
12d10e4ef2Snarayan  *
13d10e4ef2Snarayan  * When distributing Covered Code, include this CDDL HEADER in each
14d10e4ef2Snarayan  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15d10e4ef2Snarayan  * If applicable, add the following below this CDDL HEADER, with the
16d10e4ef2Snarayan  * fields enclosed by brackets "[]" replaced with your own identifying
17d10e4ef2Snarayan  * information: Portions Copyright [yyyy] [name of copyright owner]
18d10e4ef2Snarayan  *
19d10e4ef2Snarayan  * CDDL HEADER END
20d10e4ef2Snarayan  */
21d10e4ef2Snarayan 
22d10e4ef2Snarayan /*
23*844e62a3Sraghuram  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24d10e4ef2Snarayan  * Use is subject to license terms.
25d10e4ef2Snarayan  */
26d10e4ef2Snarayan 
27d10e4ef2Snarayan #ifndef	_VIO_UTIL_H
28d10e4ef2Snarayan #define	_VIO_UTIL_H
29d10e4ef2Snarayan 
30d10e4ef2Snarayan #pragma ident	"%Z%%M%	%I%	%E% SMI"
31d10e4ef2Snarayan 
32d10e4ef2Snarayan #include <sys/stream.h>
33d10e4ef2Snarayan 
34d10e4ef2Snarayan #ifdef	__cplusplus
35d10e4ef2Snarayan extern "C" {
36d10e4ef2Snarayan #endif
37d10e4ef2Snarayan 
38d10e4ef2Snarayan /*
39d10e4ef2Snarayan  * A message is composed of three structures. A message block (mblk_t), a
40d10e4ef2Snarayan  * data block to which it points and a data buffer. desballoc(9F) allows
41d10e4ef2Snarayan  * the caller to specify the data buffer and a free function which will
42d10e4ef2Snarayan  * be invoked when freeb(9F) is called to free the message. This allows
43d10e4ef2Snarayan  * the user to reclaim and reuse the data buffer, as opposed to using
44d10e4ef2Snarayan  * allocb(9F) where the message block, data block and data buffer are
45d10e4ef2Snarayan  * all destroyed by freeb().
46d10e4ef2Snarayan  *
47d10e4ef2Snarayan  * Note that even with desballoc the message and data blocks are destroyed
48d10e4ef2Snarayan  * by freeb() and must be recreated. It is only the data buffer which is
49d10e4ef2Snarayan  * preserved.
50d10e4ef2Snarayan  *
51d10e4ef2Snarayan  * The caller first creates a pool of vio_mblk_t's by invoking
52d10e4ef2Snarayan  * vio_create_mblks() and specifying the number of mblks and the size of the
53d10e4ef2Snarayan  * associated data buffers. Each vio_mblk_t contains a pointer to the
54d10e4ef2Snarayan  * mblk_t, a pointer to the data buffer and a function pointer to the
55d10e4ef2Snarayan  * reclaim function. The caller is returned a pointer to the pool which is
56d10e4ef2Snarayan  * used in subsequent allocation/destroy requests.
57d10e4ef2Snarayan  *
58d10e4ef2Snarayan  * The pool is managed as a circular queue with a head and tail pointer.
59d10e4ef2Snarayan  * Allocation requests result in the head index being incremented, mblks
60d10e4ef2Snarayan  * being returned to the pool result in the tail pointer being incremented.
61d10e4ef2Snarayan  *
62d10e4ef2Snarayan  * The pool can only be destroyed when all the mblks have been returned. It
63d10e4ef2Snarayan  * is the responsibility of the caller to ensure that all vio_allocb()
64d10e4ef2Snarayan  * requests have been completed before the pool is destroyed.
65d10e4ef2Snarayan  *
66d10e4ef2Snarayan  *
67d10e4ef2Snarayan  * vio_mblk_pool_t
68d10e4ef2Snarayan  * +-------------+
69d10e4ef2Snarayan  * |    tail     |--------------------------------+
70d10e4ef2Snarayan  * +-------------+                                |
71d10e4ef2Snarayan  * |    head     |--------+                       |
72d10e4ef2Snarayan  * +-------------+        |                       |
73d10e4ef2Snarayan  * ...............        V                       V
74d10e4ef2Snarayan  * +-------------+     +-------+-------+-------+-------+
75d10e4ef2Snarayan  * |    quep     |---->| vmp_t | vmp_t | vmp_t | vmp_t |
76d10e4ef2Snarayan  * +-------------+     +-------+-------+-------+-------+
77d10e4ef2Snarayan  * |             |         |       |       |       |
78d10e4ef2Snarayan  * ...                     |       |       |       |   +------------+
79d10e4ef2Snarayan  *                         |       |       |       +-->| data block |
80d10e4ef2Snarayan  *                         |       |       |           +------------+
81d10e4ef2Snarayan  *                         |       |       |   +------------+
82d10e4ef2Snarayan  *                         |       |       +-->| data block |
83d10e4ef2Snarayan  *                         |       |           +------------+
84d10e4ef2Snarayan  *                         |       |   +------------+
85d10e4ef2Snarayan  *                         |       +-->| data block |
86d10e4ef2Snarayan  *                         |           +------------+
87d10e4ef2Snarayan  *                         |   +------------+
88d10e4ef2Snarayan  *                         +-->| data block |
89d10e4ef2Snarayan  *                             +------------+
90d10e4ef2Snarayan  *
91d10e4ef2Snarayan  */
92d10e4ef2Snarayan 
933af08d82Slm66018 /* mblk pool flags */
943af08d82Slm66018 #define	VMPL_FLAG_DESTROYING	0x1	/* pool is being destroyed */
953af08d82Slm66018 
96d10e4ef2Snarayan struct vio_mblk_pool;
97d10e4ef2Snarayan 
98d10e4ef2Snarayan typedef struct vio_mblk {
99d10e4ef2Snarayan 	uint8_t			*datap;		/* data buffer */
100d10e4ef2Snarayan 	mblk_t			*mp;		/* mblk using datap */
101d10e4ef2Snarayan 	frtn_t			reclaim;	/* mblk reclaim routine */
102d10e4ef2Snarayan 	struct vio_mblk_pool 	*vmplp;		/* pointer to parent pool */
103d10e4ef2Snarayan } vio_mblk_t;
104d10e4ef2Snarayan 
105d10e4ef2Snarayan typedef struct vio_mblk_pool {
106d10e4ef2Snarayan 	struct vio_mblk_pool	*nextp;	/* next in a list */
107d10e4ef2Snarayan 	kmutex_t		hlock;	/* sync access to head */
108d10e4ef2Snarayan 	kmutex_t		tlock;	/* sync access to tail */
109d10e4ef2Snarayan 	vio_mblk_t		*basep;	/* base pointer to pool of vio_mblks */
110d10e4ef2Snarayan 	vio_mblk_t		**quep; /* queue of free vio_mblks */
111d10e4ef2Snarayan 	uint8_t			*datap; /* rx data buffer area */
112d10e4ef2Snarayan 	uint32_t		head;	/* queue head */
113d10e4ef2Snarayan 	uint32_t		tail;	/* queue tail */
114d10e4ef2Snarayan 	uint64_t		quelen;	/* queue len (# mblks) */
115d10e4ef2Snarayan 	uint64_t		quemask; /* quelen - 1 */
116d10e4ef2Snarayan 	size_t			mblk_size; /* data buf size of each mblk */
1173af08d82Slm66018 	uint32_t		flag;	/* pool-related flags */
118d10e4ef2Snarayan } vio_mblk_pool_t;
119d10e4ef2Snarayan 
120*844e62a3Sraghuram typedef struct vio_multi_pool {
121*844e62a3Sraghuram 	uint32_t		num_pools;	/* no. of vio mblk pools */
122*844e62a3Sraghuram 	uint32_t		tbsz;		/* allocated buffer size */
123*844e62a3Sraghuram 	uint32_t		*bufsz_tbl;	/* buffer sizes table */
124*844e62a3Sraghuram 	uint32_t		*nbuf_tbl;	/* no. of buffers table */
125*844e62a3Sraghuram 	vio_mblk_pool_t		**vmpp;		/* vio mblk pools */
126*844e62a3Sraghuram } vio_multi_pool_t;
127*844e62a3Sraghuram 
128d10e4ef2Snarayan int vio_create_mblks(uint64_t num_mblks,
129d10e4ef2Snarayan 			size_t mblk_size, vio_mblk_pool_t **);
130d10e4ef2Snarayan int vio_destroy_mblks(vio_mblk_pool_t *);
131d10e4ef2Snarayan mblk_t *vio_allocb(vio_mblk_pool_t *);
132d10e4ef2Snarayan void vio_freeb(void *arg);
133*844e62a3Sraghuram int vio_init_multipools(vio_multi_pool_t *vmultip, int num_pools, ...);
134*844e62a3Sraghuram void vio_destroy_multipools(vio_multi_pool_t *vmultip, vio_mblk_pool_t **fvmp);
135*844e62a3Sraghuram mblk_t *vio_multipool_allocb(vio_multi_pool_t *vmultip, size_t size);
136d10e4ef2Snarayan 
137d10e4ef2Snarayan #ifdef	__cplusplus
138d10e4ef2Snarayan }
139d10e4ef2Snarayan #endif
140d10e4ef2Snarayan 
141d10e4ef2Snarayan #endif	/* _VIO_UTIL_H */
142