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