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*6f09f0feSWENTAO YANG * Copyright 2009 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 */ 1013af08d82Slm66018 102d10e4ef2Snarayan struct vio_mblk_pool; 103d10e4ef2Snarayan 104d10e4ef2Snarayan typedef struct vio_mblk { 105d10e4ef2Snarayan uint8_t *datap; /* data buffer */ 106d10e4ef2Snarayan mblk_t *mp; /* mblk using datap */ 107d10e4ef2Snarayan frtn_t reclaim; /* mblk reclaim routine */ 108d10e4ef2Snarayan struct vio_mblk_pool *vmplp; /* pointer to parent pool */ 109d10e4ef2Snarayan } vio_mblk_t; 110d10e4ef2Snarayan 111d10e4ef2Snarayan typedef struct vio_mblk_pool { 112d10e4ef2Snarayan struct vio_mblk_pool *nextp; /* next in a list */ 113d10e4ef2Snarayan kmutex_t hlock; /* sync access to head */ 114d10e4ef2Snarayan kmutex_t tlock; /* sync access to tail */ 115d10e4ef2Snarayan vio_mblk_t *basep; /* base pointer to pool of vio_mblks */ 116d10e4ef2Snarayan vio_mblk_t **quep; /* queue of free vio_mblks */ 117d10e4ef2Snarayan uint8_t *datap; /* rx data buffer area */ 118d10e4ef2Snarayan uint32_t head; /* queue head */ 119d10e4ef2Snarayan uint32_t tail; /* queue tail */ 120d10e4ef2Snarayan uint64_t quelen; /* queue len (# mblks) */ 121d10e4ef2Snarayan uint64_t quemask; /* quelen - 1 */ 122d10e4ef2Snarayan size_t mblk_size; /* data buf size of each mblk */ 1233af08d82Slm66018 uint32_t flag; /* pool-related flags */ 124d10e4ef2Snarayan } vio_mblk_pool_t; 125d10e4ef2Snarayan 126844e62a3Sraghuram typedef struct vio_multi_pool { 127844e62a3Sraghuram uint32_t num_pools; /* no. of vio mblk pools */ 128844e62a3Sraghuram uint32_t tbsz; /* allocated buffer size */ 129844e62a3Sraghuram uint32_t *bufsz_tbl; /* buffer sizes table */ 130844e62a3Sraghuram uint32_t *nbuf_tbl; /* no. of buffers table */ 131844e62a3Sraghuram vio_mblk_pool_t **vmpp; /* vio mblk pools */ 132844e62a3Sraghuram } vio_multi_pool_t; 133844e62a3Sraghuram 134d10e4ef2Snarayan int vio_create_mblks(uint64_t num_mblks, 135d10e4ef2Snarayan size_t mblk_size, vio_mblk_pool_t **); 136d10e4ef2Snarayan int vio_destroy_mblks(vio_mblk_pool_t *); 137d10e4ef2Snarayan mblk_t *vio_allocb(vio_mblk_pool_t *); 138d10e4ef2Snarayan void vio_freeb(void *arg); 139844e62a3Sraghuram int vio_init_multipools(vio_multi_pool_t *vmultip, int num_pools, ...); 140844e62a3Sraghuram void vio_destroy_multipools(vio_multi_pool_t *vmultip, vio_mblk_pool_t **fvmp); 141844e62a3Sraghuram mblk_t *vio_multipool_allocb(vio_multi_pool_t *vmultip, size_t size); 142*6f09f0feSWENTAO YANG int vio_check_pending_pools(vio_multi_pool_t *vmultip); 143d10e4ef2Snarayan 14417cadca8Slm66018 /* VIO versioning helpers */ 14517cadca8Slm66018 #define VIO_VER_IS_NEGOTIATED(ver, maj, min) \ 14617cadca8Slm66018 ((ver.major == (maj)) && (ver.minor == (min))) 14717cadca8Slm66018 14817cadca8Slm66018 boolean_t vio_ver_is_supported(vio_ver_t ver, uint16_t maj, uint16_t min); 14917cadca8Slm66018 150d10e4ef2Snarayan #ifdef __cplusplus 151d10e4ef2Snarayan } 152d10e4ef2Snarayan #endif 153d10e4ef2Snarayan 154d10e4ef2Snarayan #endif /* _VIO_UTIL_H */ 155