xref: /illumos-gate/usr/src/uts/sun4v/sys/vio_util.h (revision 7bd3a2e26cc8569257b88c1691d559138e1d32d0)
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*7bd3a2e2SSriharsha Basavapatna  * Copyright 2010 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 #include <sys/stream.h>
3117cadca8Slm66018 #include <sys/vio_mailbox.h>
32d10e4ef2Snarayan 
33d10e4ef2Snarayan #ifdef	__cplusplus
34d10e4ef2Snarayan extern "C" {
35d10e4ef2Snarayan #endif
36d10e4ef2Snarayan 
37d10e4ef2Snarayan /*
3817cadca8Slm66018  * Helper routines for the Logical Domains (LDoms) drivers
3917cadca8Slm66018  *
4017cadca8Slm66018  * Note: The contents of this file are private to the implementation of the
4117cadca8Slm66018  * LDoms drivers and are subject to change at any time without notice.
4217cadca8Slm66018  */
4317cadca8Slm66018 
4417cadca8Slm66018 /*
45d10e4ef2Snarayan  * A message is composed of three structures. A message block (mblk_t), a
46d10e4ef2Snarayan  * data block to which it points and a data buffer. desballoc(9F) allows
47d10e4ef2Snarayan  * the caller to specify the data buffer and a free function which will
48d10e4ef2Snarayan  * be invoked when freeb(9F) is called to free the message. This allows
49d10e4ef2Snarayan  * the user to reclaim and reuse the data buffer, as opposed to using
50d10e4ef2Snarayan  * allocb(9F) where the message block, data block and data buffer are
51d10e4ef2Snarayan  * all destroyed by freeb().
52d10e4ef2Snarayan  *
53d10e4ef2Snarayan  * Note that even with desballoc the message and data blocks are destroyed
54d10e4ef2Snarayan  * by freeb() and must be recreated. It is only the data buffer which is
55d10e4ef2Snarayan  * preserved.
56d10e4ef2Snarayan  *
57d10e4ef2Snarayan  * The caller first creates a pool of vio_mblk_t's by invoking
58d10e4ef2Snarayan  * vio_create_mblks() and specifying the number of mblks and the size of the
59d10e4ef2Snarayan  * associated data buffers. Each vio_mblk_t contains a pointer to the
60d10e4ef2Snarayan  * mblk_t, a pointer to the data buffer and a function pointer to the
61d10e4ef2Snarayan  * reclaim function. The caller is returned a pointer to the pool which is
62d10e4ef2Snarayan  * used in subsequent allocation/destroy requests.
63d10e4ef2Snarayan  *
64d10e4ef2Snarayan  * The pool is managed as a circular queue with a head and tail pointer.
65d10e4ef2Snarayan  * Allocation requests result in the head index being incremented, mblks
66d10e4ef2Snarayan  * being returned to the pool result in the tail pointer being incremented.
67d10e4ef2Snarayan  *
68d10e4ef2Snarayan  * The pool can only be destroyed when all the mblks have been returned. It
69d10e4ef2Snarayan  * is the responsibility of the caller to ensure that all vio_allocb()
70d10e4ef2Snarayan  * requests have been completed before the pool is destroyed.
71d10e4ef2Snarayan  *
72d10e4ef2Snarayan  *
73d10e4ef2Snarayan  * vio_mblk_pool_t
74d10e4ef2Snarayan  * +-------------+
75d10e4ef2Snarayan  * |    tail     |--------------------------------+
76d10e4ef2Snarayan  * +-------------+                                |
77d10e4ef2Snarayan  * |    head     |--------+                       |
78d10e4ef2Snarayan  * +-------------+        |                       |
79d10e4ef2Snarayan  * ...............        V                       V
80d10e4ef2Snarayan  * +-------------+     +-------+-------+-------+-------+
81d10e4ef2Snarayan  * |    quep     |---->| vmp_t | vmp_t | vmp_t | vmp_t |
82d10e4ef2Snarayan  * +-------------+     +-------+-------+-------+-------+
83d10e4ef2Snarayan  * |             |         |       |       |       |
84d10e4ef2Snarayan  * ...                     |       |       |       |   +------------+
85d10e4ef2Snarayan  *                         |       |       |       +-->| data block |
86d10e4ef2Snarayan  *                         |       |       |           +------------+
87d10e4ef2Snarayan  *                         |       |       |   +------------+
88d10e4ef2Snarayan  *                         |       |       +-->| data block |
89d10e4ef2Snarayan  *                         |       |           +------------+
90d10e4ef2Snarayan  *                         |       |   +------------+
91d10e4ef2Snarayan  *                         |       +-->| data block |
92d10e4ef2Snarayan  *                         |           +------------+
93d10e4ef2Snarayan  *                         |   +------------+
94d10e4ef2Snarayan  *                         +-->| data block |
95d10e4ef2Snarayan  *                             +------------+
96d10e4ef2Snarayan  *
97d10e4ef2Snarayan  */
98d10e4ef2Snarayan 
993af08d82Slm66018 /* mblk pool flags */
1003af08d82Slm66018 #define	VMPL_FLAG_DESTROYING	0x1	/* pool is being destroyed */
101*7bd3a2e2SSriharsha Basavapatna #define	VMPL_FLAG_CLIENT_DATA	0x2	/* pool data area provided by client */
1023af08d82Slm66018 
103d10e4ef2Snarayan struct vio_mblk_pool;
104d10e4ef2Snarayan 
105*7bd3a2e2SSriharsha Basavapatna /* VIO mblk states */
106*7bd3a2e2SSriharsha Basavapatna typedef enum vio_mblk_state {
107*7bd3a2e2SSriharsha Basavapatna 	VIO_MBLK_FREE = 0x1,		/* free to use */
108*7bd3a2e2SSriharsha Basavapatna 	VIO_MBLK_BOUND = 0x2,		/* allocated/bound to a descriptor */
109*7bd3a2e2SSriharsha Basavapatna 	VIO_MBLK_HAS_DATA = 0x4		/* contains valid data */
110*7bd3a2e2SSriharsha Basavapatna } vio_mblk_state_t;
111*7bd3a2e2SSriharsha Basavapatna 
112d10e4ef2Snarayan typedef struct vio_mblk {
113d10e4ef2Snarayan 	uint8_t			*datap;		/* data buffer */
114d10e4ef2Snarayan 	mblk_t			*mp;		/* mblk using datap */
115d10e4ef2Snarayan 	frtn_t			reclaim;	/* mblk reclaim routine */
116d10e4ef2Snarayan 	struct vio_mblk_pool 	*vmplp;		/* pointer to parent pool */
117*7bd3a2e2SSriharsha Basavapatna 	uint_t			index;		/* index in the pool */
118*7bd3a2e2SSriharsha Basavapatna 	vio_mblk_state_t	state;		/* state flags */
119d10e4ef2Snarayan } vio_mblk_t;
120d10e4ef2Snarayan 
121d10e4ef2Snarayan typedef struct vio_mblk_pool {
122d10e4ef2Snarayan 	struct vio_mblk_pool	*nextp;	/* next in a list */
123d10e4ef2Snarayan 	kmutex_t		hlock;	/* sync access to head */
124d10e4ef2Snarayan 	kmutex_t		tlock;	/* sync access to tail */
125d10e4ef2Snarayan 	vio_mblk_t		*basep;	/* base pointer to pool of vio_mblks */
126d10e4ef2Snarayan 	vio_mblk_t		**quep; /* queue of free vio_mblks */
127d10e4ef2Snarayan 	uint8_t			*datap; /* rx data buffer area */
128d10e4ef2Snarayan 	uint32_t		head;	/* queue head */
129d10e4ef2Snarayan 	uint32_t		tail;	/* queue tail */
130d10e4ef2Snarayan 	uint64_t		quelen;	/* queue len (# mblks) */
131d10e4ef2Snarayan 	uint64_t		quemask; /* quelen - 1 */
132d10e4ef2Snarayan 	size_t			mblk_size; /* data buf size of each mblk */
1333af08d82Slm66018 	uint32_t		flag;	/* pool-related flags */
134d10e4ef2Snarayan } vio_mblk_pool_t;
135d10e4ef2Snarayan 
136844e62a3Sraghuram typedef struct vio_multi_pool {
137844e62a3Sraghuram 	uint32_t		num_pools;	/* no. of vio mblk pools */
138844e62a3Sraghuram 	uint32_t		tbsz;		/* allocated buffer size */
139844e62a3Sraghuram 	uint32_t		*bufsz_tbl;	/* buffer sizes table */
140844e62a3Sraghuram 	uint32_t		*nbuf_tbl;	/* no. of buffers table */
141844e62a3Sraghuram 	vio_mblk_pool_t		**vmpp;		/* vio mblk pools */
142844e62a3Sraghuram } vio_multi_pool_t;
143844e62a3Sraghuram 
144*7bd3a2e2SSriharsha Basavapatna #define	VIO_MBLK_DATA_OFF(vmp)	((vmp)->datap - ((vmp)->vmplp)->datap)
145*7bd3a2e2SSriharsha Basavapatna 
146d10e4ef2Snarayan int vio_create_mblks(uint64_t num_mblks,
147*7bd3a2e2SSriharsha Basavapatna 			size_t mblk_size, uint8_t *mblk_datap,
148*7bd3a2e2SSriharsha Basavapatna 			vio_mblk_pool_t **poolp);
149d10e4ef2Snarayan int vio_destroy_mblks(vio_mblk_pool_t *);
150*7bd3a2e2SSriharsha Basavapatna vio_mblk_t *vio_allocb(vio_mblk_pool_t *);
151d10e4ef2Snarayan void vio_freeb(void *arg);
152844e62a3Sraghuram int vio_init_multipools(vio_multi_pool_t *vmultip, int num_pools, ...);
153844e62a3Sraghuram void vio_destroy_multipools(vio_multi_pool_t *vmultip, vio_mblk_pool_t **fvmp);
154*7bd3a2e2SSriharsha Basavapatna vio_mblk_t *vio_multipool_allocb(vio_multi_pool_t *vmultip, size_t size);
1556f09f0feSWENTAO YANG int vio_check_pending_pools(vio_multi_pool_t *vmultip);
156*7bd3a2e2SSriharsha Basavapatna void vio_clobber_pool(vio_mblk_pool_t *vmplp);
157d10e4ef2Snarayan 
15817cadca8Slm66018 /* VIO versioning helpers */
15917cadca8Slm66018 #define	VIO_VER_IS_NEGOTIATED(ver, maj, min)		\
16017cadca8Slm66018 	((ver.major == (maj)) && (ver.minor == (min)))
16117cadca8Slm66018 
16217cadca8Slm66018 boolean_t	vio_ver_is_supported(vio_ver_t ver, uint16_t maj, uint16_t min);
16317cadca8Slm66018 
164d10e4ef2Snarayan #ifdef	__cplusplus
165d10e4ef2Snarayan }
166d10e4ef2Snarayan #endif
167d10e4ef2Snarayan 
168d10e4ef2Snarayan #endif	/* _VIO_UTIL_H */
169