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 /* 232336Snarayan * Copyright 2006 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 #pragma ident "%Z%%M% %I% %E% SMI" 312336Snarayan 322336Snarayan #include <sys/stream.h> 332336Snarayan 342336Snarayan #ifdef __cplusplus 352336Snarayan extern "C" { 362336Snarayan #endif 372336Snarayan 382336Snarayan /* 392336Snarayan * A message is composed of three structures. A message block (mblk_t), a 402336Snarayan * data block to which it points and a data buffer. desballoc(9F) allows 412336Snarayan * the caller to specify the data buffer and a free function which will 422336Snarayan * be invoked when freeb(9F) is called to free the message. This allows 432336Snarayan * the user to reclaim and reuse the data buffer, as opposed to using 442336Snarayan * allocb(9F) where the message block, data block and data buffer are 452336Snarayan * all destroyed by freeb(). 462336Snarayan * 472336Snarayan * Note that even with desballoc the message and data blocks are destroyed 482336Snarayan * by freeb() and must be recreated. It is only the data buffer which is 492336Snarayan * preserved. 502336Snarayan * 512336Snarayan * The caller first creates a pool of vio_mblk_t's by invoking 522336Snarayan * vio_create_mblks() and specifying the number of mblks and the size of the 532336Snarayan * associated data buffers. Each vio_mblk_t contains a pointer to the 542336Snarayan * mblk_t, a pointer to the data buffer and a function pointer to the 552336Snarayan * reclaim function. The caller is returned a pointer to the pool which is 562336Snarayan * used in subsequent allocation/destroy requests. 572336Snarayan * 582336Snarayan * The pool is managed as a circular queue with a head and tail pointer. 592336Snarayan * Allocation requests result in the head index being incremented, mblks 602336Snarayan * being returned to the pool result in the tail pointer being incremented. 612336Snarayan * 622336Snarayan * The pool can only be destroyed when all the mblks have been returned. It 632336Snarayan * is the responsibility of the caller to ensure that all vio_allocb() 642336Snarayan * requests have been completed before the pool is destroyed. 652336Snarayan * 662336Snarayan * 672336Snarayan * vio_mblk_pool_t 682336Snarayan * +-------------+ 692336Snarayan * | tail |--------------------------------+ 702336Snarayan * +-------------+ | 712336Snarayan * | head |--------+ | 722336Snarayan * +-------------+ | | 732336Snarayan * ............... V V 742336Snarayan * +-------------+ +-------+-------+-------+-------+ 752336Snarayan * | quep |---->| vmp_t | vmp_t | vmp_t | vmp_t | 762336Snarayan * +-------------+ +-------+-------+-------+-------+ 772336Snarayan * | | | | | | 782336Snarayan * ... | | | | +------------+ 792336Snarayan * | | | +-->| data block | 802336Snarayan * | | | +------------+ 812336Snarayan * | | | +------------+ 822336Snarayan * | | +-->| data block | 832336Snarayan * | | +------------+ 842336Snarayan * | | +------------+ 852336Snarayan * | +-->| data block | 862336Snarayan * | +------------+ 872336Snarayan * | +------------+ 882336Snarayan * +-->| data block | 892336Snarayan * +------------+ 902336Snarayan * 912336Snarayan */ 922336Snarayan 93*2793Slm66018 /* mblk pool flags */ 94*2793Slm66018 #define VMPL_FLAG_DESTROYING 0x1 /* pool is being destroyed */ 95*2793Slm66018 962336Snarayan struct vio_mblk_pool; 972336Snarayan 982336Snarayan typedef struct vio_mblk { 992336Snarayan uint8_t *datap; /* data buffer */ 1002336Snarayan mblk_t *mp; /* mblk using datap */ 1012336Snarayan frtn_t reclaim; /* mblk reclaim routine */ 1022336Snarayan struct vio_mblk_pool *vmplp; /* pointer to parent pool */ 1032336Snarayan } vio_mblk_t; 1042336Snarayan 1052336Snarayan typedef struct vio_mblk_pool { 1062336Snarayan struct vio_mblk_pool *nextp; /* next in a list */ 1072336Snarayan kmutex_t hlock; /* sync access to head */ 1082336Snarayan kmutex_t tlock; /* sync access to tail */ 1092336Snarayan vio_mblk_t *basep; /* base pointer to pool of vio_mblks */ 1102336Snarayan vio_mblk_t **quep; /* queue of free vio_mblks */ 1112336Snarayan uint8_t *datap; /* rx data buffer area */ 1122336Snarayan uint32_t head; /* queue head */ 1132336Snarayan uint32_t tail; /* queue tail */ 1142336Snarayan uint64_t quelen; /* queue len (# mblks) */ 1152336Snarayan uint64_t quemask; /* quelen - 1 */ 1162336Snarayan size_t mblk_size; /* data buf size of each mblk */ 117*2793Slm66018 uint32_t flag; /* pool-related flags */ 1182336Snarayan } vio_mblk_pool_t; 1192336Snarayan 1202336Snarayan int vio_create_mblks(uint64_t num_mblks, 1212336Snarayan size_t mblk_size, vio_mblk_pool_t **); 1222336Snarayan int vio_destroy_mblks(vio_mblk_pool_t *); 1232336Snarayan mblk_t *vio_allocb(vio_mblk_pool_t *); 1242336Snarayan void vio_freeb(void *arg); 1252336Snarayan 1262336Snarayan 1272336Snarayan #ifdef __cplusplus 1282336Snarayan } 1292336Snarayan #endif 1302336Snarayan 1312336Snarayan #endif /* _VIO_UTIL_H */ 132