xref: /onnv-gate/usr/src/uts/sun4v/sys/vio_util.h (revision 12011:2377022c7a2d)
12336Snarayan /*
22336Snarayan  * CDDL HEADER START
32336Snarayan  *
42336Snarayan  * The contents of this file are subject to the terms of the
52336Snarayan  * Common Development and Distribution License (the "License").
62336Snarayan  * You may not use this file except in compliance with the License.
72336Snarayan  *
82336Snarayan  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92336Snarayan  * or http://www.opensolaris.org/os/licensing.
102336Snarayan  * See the License for the specific language governing permissions
112336Snarayan  * and limitations under the License.
122336Snarayan  *
132336Snarayan  * When distributing Covered Code, include this CDDL HEADER in each
142336Snarayan  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152336Snarayan  * If applicable, add the following below this CDDL HEADER, with the
162336Snarayan  * fields enclosed by brackets "[]" replaced with your own identifying
172336Snarayan  * information: Portions Copyright [yyyy] [name of copyright owner]
182336Snarayan  *
192336Snarayan  * CDDL HEADER END
202336Snarayan  */
212336Snarayan 
222336Snarayan /*
23*12011SSriharsha.Basavapatna@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
242336Snarayan  * Use is subject to license terms.
252336Snarayan  */
262336Snarayan 
272336Snarayan #ifndef	_VIO_UTIL_H
282336Snarayan #define	_VIO_UTIL_H
292336Snarayan 
302336Snarayan #include <sys/stream.h>
315365Slm66018 #include <sys/vio_mailbox.h>
322336Snarayan 
332336Snarayan #ifdef	__cplusplus
342336Snarayan extern "C" {
352336Snarayan #endif
362336Snarayan 
372336Snarayan /*
385365Slm66018  * Helper routines for the Logical Domains (LDoms) drivers
395365Slm66018  *
405365Slm66018  * Note: The contents of this file are private to the implementation of the
415365Slm66018  * LDoms drivers and are subject to change at any time without notice.
425365Slm66018  */
435365Slm66018 
445365Slm66018 /*
452336Snarayan  * A message is composed of three structures. A message block (mblk_t), a
462336Snarayan  * data block to which it points and a data buffer. desballoc(9F) allows
472336Snarayan  * the caller to specify the data buffer and a free function which will
482336Snarayan  * be invoked when freeb(9F) is called to free the message. This allows
492336Snarayan  * the user to reclaim and reuse the data buffer, as opposed to using
502336Snarayan  * allocb(9F) where the message block, data block and data buffer are
512336Snarayan  * all destroyed by freeb().
522336Snarayan  *
532336Snarayan  * Note that even with desballoc the message and data blocks are destroyed
542336Snarayan  * by freeb() and must be recreated. It is only the data buffer which is
552336Snarayan  * preserved.
562336Snarayan  *
572336Snarayan  * The caller first creates a pool of vio_mblk_t's by invoking
582336Snarayan  * vio_create_mblks() and specifying the number of mblks and the size of the
592336Snarayan  * associated data buffers. Each vio_mblk_t contains a pointer to the
602336Snarayan  * mblk_t, a pointer to the data buffer and a function pointer to the
612336Snarayan  * reclaim function. The caller is returned a pointer to the pool which is
622336Snarayan  * used in subsequent allocation/destroy requests.
632336Snarayan  *
642336Snarayan  * The pool is managed as a circular queue with a head and tail pointer.
652336Snarayan  * Allocation requests result in the head index being incremented, mblks
662336Snarayan  * being returned to the pool result in the tail pointer being incremented.
672336Snarayan  *
682336Snarayan  * The pool can only be destroyed when all the mblks have been returned. It
692336Snarayan  * is the responsibility of the caller to ensure that all vio_allocb()
702336Snarayan  * requests have been completed before the pool is destroyed.
712336Snarayan  *
722336Snarayan  *
732336Snarayan  * vio_mblk_pool_t
742336Snarayan  * +-------------+
752336Snarayan  * |    tail     |--------------------------------+
762336Snarayan  * +-------------+                                |
772336Snarayan  * |    head     |--------+                       |
782336Snarayan  * +-------------+        |                       |
792336Snarayan  * ...............        V                       V
802336Snarayan  * +-------------+     +-------+-------+-------+-------+
812336Snarayan  * |    quep     |---->| vmp_t | vmp_t | vmp_t | vmp_t |
822336Snarayan  * +-------------+     +-------+-------+-------+-------+
832336Snarayan  * |             |         |       |       |       |
842336Snarayan  * ...                     |       |       |       |   +------------+
852336Snarayan  *                         |       |       |       +-->| data block |
862336Snarayan  *                         |       |       |           +------------+
872336Snarayan  *                         |       |       |   +------------+
882336Snarayan  *                         |       |       +-->| data block |
892336Snarayan  *                         |       |           +------------+
902336Snarayan  *                         |       |   +------------+
912336Snarayan  *                         |       +-->| data block |
922336Snarayan  *                         |           +------------+
932336Snarayan  *                         |   +------------+
942336Snarayan  *                         +-->| data block |
952336Snarayan  *                             +------------+
962336Snarayan  *
972336Snarayan  */
982336Snarayan 
992793Slm66018 /* mblk pool flags */
1002793Slm66018 #define	VMPL_FLAG_DESTROYING	0x1	/* pool is being destroyed */
101*12011SSriharsha.Basavapatna@Sun.COM #define	VMPL_FLAG_CLIENT_DATA	0x2	/* pool data area provided by client */
1022793Slm66018 
1032336Snarayan struct vio_mblk_pool;
1042336Snarayan 
105*12011SSriharsha.Basavapatna@Sun.COM /* VIO mblk states */
106*12011SSriharsha.Basavapatna@Sun.COM typedef enum vio_mblk_state {
107*12011SSriharsha.Basavapatna@Sun.COM 	VIO_MBLK_FREE = 0x1,		/* free to use */
108*12011SSriharsha.Basavapatna@Sun.COM 	VIO_MBLK_BOUND = 0x2,		/* allocated/bound to a descriptor */
109*12011SSriharsha.Basavapatna@Sun.COM 	VIO_MBLK_HAS_DATA = 0x4		/* contains valid data */
110*12011SSriharsha.Basavapatna@Sun.COM } vio_mblk_state_t;
111*12011SSriharsha.Basavapatna@Sun.COM 
1122336Snarayan typedef struct vio_mblk {
1132336Snarayan 	uint8_t			*datap;		/* data buffer */
1142336Snarayan 	mblk_t			*mp;		/* mblk using datap */
1152336Snarayan 	frtn_t			reclaim;	/* mblk reclaim routine */
1162336Snarayan 	struct vio_mblk_pool 	*vmplp;		/* pointer to parent pool */
117*12011SSriharsha.Basavapatna@Sun.COM 	uint_t			index;		/* index in the pool */
118*12011SSriharsha.Basavapatna@Sun.COM 	vio_mblk_state_t	state;		/* state flags */
1192336Snarayan } vio_mblk_t;
1202336Snarayan 
1212336Snarayan typedef struct vio_mblk_pool {
1222336Snarayan 	struct vio_mblk_pool	*nextp;	/* next in a list */
1232336Snarayan 	kmutex_t		hlock;	/* sync access to head */
1242336Snarayan 	kmutex_t		tlock;	/* sync access to tail */
1252336Snarayan 	vio_mblk_t		*basep;	/* base pointer to pool of vio_mblks */
1262336Snarayan 	vio_mblk_t		**quep; /* queue of free vio_mblks */
1272336Snarayan 	uint8_t			*datap; /* rx data buffer area */
1282336Snarayan 	uint32_t		head;	/* queue head */
1292336Snarayan 	uint32_t		tail;	/* queue tail */
1302336Snarayan 	uint64_t		quelen;	/* queue len (# mblks) */
1312336Snarayan 	uint64_t		quemask; /* quelen - 1 */
1322336Snarayan 	size_t			mblk_size; /* data buf size of each mblk */
1332793Slm66018 	uint32_t		flag;	/* pool-related flags */
1342336Snarayan } vio_mblk_pool_t;
1352336Snarayan 
1364647Sraghuram typedef struct vio_multi_pool {
1374647Sraghuram 	uint32_t		num_pools;	/* no. of vio mblk pools */
1384647Sraghuram 	uint32_t		tbsz;		/* allocated buffer size */
1394647Sraghuram 	uint32_t		*bufsz_tbl;	/* buffer sizes table */
1404647Sraghuram 	uint32_t		*nbuf_tbl;	/* no. of buffers table */
1414647Sraghuram 	vio_mblk_pool_t		**vmpp;		/* vio mblk pools */
1424647Sraghuram } vio_multi_pool_t;
1434647Sraghuram 
144*12011SSriharsha.Basavapatna@Sun.COM #define	VIO_MBLK_DATA_OFF(vmp)	((vmp)->datap - ((vmp)->vmplp)->datap)
145*12011SSriharsha.Basavapatna@Sun.COM 
1462336Snarayan int vio_create_mblks(uint64_t num_mblks,
147*12011SSriharsha.Basavapatna@Sun.COM 			size_t mblk_size, uint8_t *mblk_datap,
148*12011SSriharsha.Basavapatna@Sun.COM 			vio_mblk_pool_t **poolp);
1492336Snarayan int vio_destroy_mblks(vio_mblk_pool_t *);
150*12011SSriharsha.Basavapatna@Sun.COM vio_mblk_t *vio_allocb(vio_mblk_pool_t *);
1512336Snarayan void vio_freeb(void *arg);
1524647Sraghuram int vio_init_multipools(vio_multi_pool_t *vmultip, int num_pools, ...);
1534647Sraghuram void vio_destroy_multipools(vio_multi_pool_t *vmultip, vio_mblk_pool_t **fvmp);
154*12011SSriharsha.Basavapatna@Sun.COM vio_mblk_t *vio_multipool_allocb(vio_multi_pool_t *vmultip, size_t size);
1559217SWentao.Yang@Sun.COM int vio_check_pending_pools(vio_multi_pool_t *vmultip);
156*12011SSriharsha.Basavapatna@Sun.COM void vio_clobber_pool(vio_mblk_pool_t *vmplp);
1572336Snarayan 
1585365Slm66018 /* VIO versioning helpers */
1595365Slm66018 #define	VIO_VER_IS_NEGOTIATED(ver, maj, min)		\
1605365Slm66018 	((ver.major == (maj)) && (ver.minor == (min)))
1615365Slm66018 
1625365Slm66018 boolean_t	vio_ver_is_supported(vio_ver_t ver, uint16_t maj, uint16_t min);
1635365Slm66018 
1642336Snarayan #ifdef	__cplusplus
1652336Snarayan }
1662336Snarayan #endif
1672336Snarayan 
1682336Snarayan #endif	/* _VIO_UTIL_H */
169