xref: /onnv-gate/usr/src/lib/librsm/common/rsmlib.c (revision 1914:8a8c5f225b1b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1914Scasper  * Common Development and Distribution License (the "License").
6*1914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
231219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
291219Sraf #include "c_synonyms.h"
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <stdarg.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <sys/mman.h>
400Sstevel@tonic-gate #include <sys/uio.h>
410Sstevel@tonic-gate #include <sys/sysmacros.h>
420Sstevel@tonic-gate #include <sys/resource.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <assert.h>
450Sstevel@tonic-gate #include <fcntl.h>
460Sstevel@tonic-gate #include <dlfcn.h>
470Sstevel@tonic-gate #include <sched.h>
480Sstevel@tonic-gate #include <stropts.h>
490Sstevel@tonic-gate #include <poll.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <rsmapi.h>
520Sstevel@tonic-gate #include <sys/rsm/rsmndi.h>
530Sstevel@tonic-gate #include <rsmlib_in.h>
540Sstevel@tonic-gate #include <sys/rsm/rsm.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #ifdef __STDC__
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #pragma weak rsm_get_controller = _rsm_get_controller
590Sstevel@tonic-gate #pragma weak rsm_get_controller_attr = _rsm_get_controller_attr
600Sstevel@tonic-gate #pragma weak rsm_release_controller = _rsm_release_controller
610Sstevel@tonic-gate #pragma weak rsm_get_interconnect_topology = _rsm_get_interconnect_topology
620Sstevel@tonic-gate #pragma weak rsm_free_interconnect_topology = _rsm_free_interconnect_topology
630Sstevel@tonic-gate #pragma weak rsm_memseg_export_create = _rsm_memseg_export_create
640Sstevel@tonic-gate #pragma weak rsm_memseg_export_destroy = _rsm_memseg_export_destroy
650Sstevel@tonic-gate #pragma weak rsm_memseg_export_rebind = _rsm_memseg_export_rebind
660Sstevel@tonic-gate #pragma weak rsm_memseg_export_publish = _rsm_memseg_export_publish
670Sstevel@tonic-gate #pragma weak rsm_memseg_export_unpublish = _rsm_memseg_export_unpublish
680Sstevel@tonic-gate #pragma weak rsm_memseg_export_republish = _rsm_memseg_export_republish
690Sstevel@tonic-gate #pragma weak rsm_memseg_import_connect = _rsm_memseg_import_connect
700Sstevel@tonic-gate #pragma weak rsm_memseg_import_disconnect = _rsm_memseg_import_disconnect
710Sstevel@tonic-gate #pragma weak rsm_memseg_import_get8 = _rsm_memseg_import_get8
720Sstevel@tonic-gate #pragma weak rsm_memseg_import_get16 = _rsm_memseg_import_get16
730Sstevel@tonic-gate #pragma weak rsm_memseg_import_get32 = _rsm_memseg_import_get32
740Sstevel@tonic-gate #pragma weak rsm_memseg_import_get64 = _rsm_memseg_import_get64
750Sstevel@tonic-gate #pragma weak rsm_memseg_import_get = _rsm_memseg_import_get
760Sstevel@tonic-gate #pragma weak rsm_memseg_import_getv = _rsm_memseg_import_getv
770Sstevel@tonic-gate #pragma weak rsm_memseg_import_put8 = _rsm_memseg_import_put8
780Sstevel@tonic-gate #pragma weak rsm_memseg_import_put16 = _rsm_memseg_import_put16
790Sstevel@tonic-gate #pragma weak rsm_memseg_import_put32 = _rsm_memseg_import_put32
800Sstevel@tonic-gate #pragma weak rsm_memseg_import_put64 = _rsm_memseg_import_put64
810Sstevel@tonic-gate #pragma weak rsm_memseg_import_put = _rsm_memseg_import_put
820Sstevel@tonic-gate #pragma weak rsm_memseg_import_putv = _rsm_memseg_import_putv
830Sstevel@tonic-gate #pragma weak rsm_memseg_import_map = _rsm_memseg_import_map
840Sstevel@tonic-gate #pragma weak rsm_memseg_import_unmap = _rsm_memseg_import_unmap
850Sstevel@tonic-gate #pragma weak rsm_memseg_import_init_barrier = _rsm_memseg_import_init_barrier
860Sstevel@tonic-gate #pragma weak rsm_memseg_import_open_barrier = _rsm_memseg_import_open_barrier
870Sstevel@tonic-gate #pragma weak rsm_memseg_import_close_barrier = _rsm_memseg_import_close_barrier
880Sstevel@tonic-gate #pragma weak rsm_memseg_import_order_barrier = _rsm_memseg_import_order_barrier
890Sstevel@tonic-gate #pragma weak rsm_memseg_import_destroy_barrier = \
900Sstevel@tonic-gate 				_rsm_memseg_import_destroy_barrier
910Sstevel@tonic-gate #pragma weak rsm_memseg_import_get_mode = _rsm_memseg_import_get_mode
920Sstevel@tonic-gate #pragma weak rsm_memseg_import_set_mode = _rsm_memseg_import_set_mode
930Sstevel@tonic-gate #pragma weak rsm_create_localmemory_handle = _rsm_create_localmemory_handle
940Sstevel@tonic-gate #pragma weak rsm_free_localmemory_handle = _rsm_free_localmemory_handle
950Sstevel@tonic-gate #pragma weak rsm_intr_signal_post = _rsm_intr_signal_post
960Sstevel@tonic-gate #pragma weak rsm_intr_signal_wait = _rsm_intr_signal_wait
970Sstevel@tonic-gate #pragma weak rsm_intr_signal_wait_pollfd = _rsm_intr_signal_wait_pollfd
980Sstevel@tonic-gate #pragma weak rsm_memseg_get_pollfd = _rsm_memseg_get_pollfd
990Sstevel@tonic-gate #pragma weak rsm_memseg_release_pollfd = _rsm_memseg_release_pollfd
1000Sstevel@tonic-gate #pragma weak rsm_get_segmentid_range = _rsm_get_segmentid_range
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #endif /* __STDC__ */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* lint -w2 */
1050Sstevel@tonic-gate extern void __rsmloopback_init_ops(rsm_segops_t *);
1060Sstevel@tonic-gate extern void __rsmdefault_setops(rsm_segops_t *);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate typedef void (*rsm_access_func_t)(void *, void *, rsm_access_size_t);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #ifdef DEBUG
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #define	RSMLOG_BUF_SIZE 256
1130Sstevel@tonic-gate FILE *rsmlog_fd = NULL;
1140Sstevel@tonic-gate static mutex_t rsmlog_lock;
1150Sstevel@tonic-gate int rsmlibdbg_category = RSM_LIBRARY;
1160Sstevel@tonic-gate int rsmlibdbg_level = RSM_ERR;
1170Sstevel@tonic-gate void dbg_printf(int category, int level, char *fmt, ...);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #endif /* DEBUG */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate rsm_node_id_t rsm_local_nodeid = 0;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate static rsm_controller_t *controller_list = NULL;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static rsm_segops_t loopback_ops;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	MAX_STRLEN	80
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate #define	RSM_IOTYPE_PUTGET	1
1300Sstevel@tonic-gate #define	RSM_IOTYPE_SCATGATH	2
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate #define	RSMFILE_BUFSIZE		256
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate #pragma init(_rsm_librsm_init)
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static mutex_t _rsm_lock;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static int _rsm_fd = -1;
1390Sstevel@tonic-gate static rsm_gnum_t *bar_va, bar_fixed = 0;
1400Sstevel@tonic-gate static rsm_pollfd_table_t pollfd_table;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static int _rsm_get_hwaddr(rsmapi_controller_handle_t handle,
1430Sstevel@tonic-gate rsm_node_id_t, rsm_addr_t *hwaddrp);
1440Sstevel@tonic-gate static int _rsm_get_nodeid(rsmapi_controller_handle_t,
1450Sstevel@tonic-gate rsm_addr_t, rsm_node_id_t *);
1460Sstevel@tonic-gate static int __rsm_import_implicit_map(rsmseg_handle_t *, int);
1470Sstevel@tonic-gate static int __rsm_intr_signal_wait_common(struct pollfd [], minor_t [],
1480Sstevel@tonic-gate     nfds_t, int, int *);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static	rsm_lib_funcs_t lib_functions = {
1510Sstevel@tonic-gate 	RSM_LIB_FUNCS_VERSION,
1520Sstevel@tonic-gate 	_rsm_get_hwaddr,
1530Sstevel@tonic-gate 	_rsm_get_nodeid
1540Sstevel@tonic-gate };
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate int _rsm_get_interconnect_topology(rsm_topology_t **);
1570Sstevel@tonic-gate void _rsm_free_interconnect_topology(rsm_topology_t *);
1580Sstevel@tonic-gate int _rsm_memseg_import_open_barrier(rsmapi_barrier_t *);
1590Sstevel@tonic-gate int _rsm_memseg_import_close_barrier(rsmapi_barrier_t *);
1600Sstevel@tonic-gate int _rsm_memseg_import_unmap(rsm_memseg_import_handle_t);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate rsm_topology_t *tp;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * service module function templates:
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * The _rsm_librsm_init function is called the first time an application
1720Sstevel@tonic-gate  * references the RSMAPI library
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate int
1750Sstevel@tonic-gate _rsm_librsm_init()
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	rsm_ioctlmsg_t 		msg;
1780Sstevel@tonic-gate 	int e, tmpfd;
1790Sstevel@tonic-gate 	int i;
1800Sstevel@tonic-gate 	char logname[MAXNAMELEN];
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	mutex_init(&_rsm_lock, USYNC_THREAD, NULL);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate #ifdef DEBUG
1850Sstevel@tonic-gate 	mutex_init(&rsmlog_lock, USYNC_THREAD, NULL);
1860Sstevel@tonic-gate 	sprintf(logname, "%s.%d", TRACELOG, getpid());
187*1914Scasper 	rsmlog_fd = fopen(logname, "w+F");
1880Sstevel@tonic-gate 	if (rsmlog_fd == NULL) {
1890Sstevel@tonic-gate 		fprintf(stderr, "Log file open failed\n");
1900Sstevel@tonic-gate 		return (errno);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate #endif /* DEBUG */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1960Sstevel@tonic-gate 	    "_rsm_librsm_init: enter\n"));
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/* initialize the pollfd_table */
1990Sstevel@tonic-gate 	mutex_init(&pollfd_table.lock, USYNC_THREAD, NULL);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	for (i = 0; i < RSM_MAX_BUCKETS; i++) {
2020Sstevel@tonic-gate 		pollfd_table.buckets[i] = NULL;
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/* open /dev/rsm and mmap barrier generation pages */
2060Sstevel@tonic-gate 	mutex_lock(&_rsm_lock);
2070Sstevel@tonic-gate 	_rsm_fd = open(DEVRSM, O_RDONLY);
2080Sstevel@tonic-gate 	if (_rsm_fd < 0) {
2090Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
2100Sstevel@tonic-gate 		    "unable to open /dev/rsm\n"));
2110Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
2120Sstevel@tonic-gate 		return (errno);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/*
2160Sstevel@tonic-gate 	 * DUP the opened file descriptor to something greater than
2170Sstevel@tonic-gate 	 * STDERR_FILENO so that we never use the STDIN_FILENO,
2180Sstevel@tonic-gate 	 * STDOUT_FILENO or STDERR_FILENO.
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	tmpfd = fcntl(_rsm_fd, F_DUPFD, 3);
2210Sstevel@tonic-gate 	if (tmpfd < 0) {
2220Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
2230Sstevel@tonic-gate 		    "F_DUPFD failed\n"));
2240Sstevel@tonic-gate 	} else {
2250Sstevel@tonic-gate 		(void) close(_rsm_fd);
2260Sstevel@tonic-gate 		_rsm_fd = tmpfd;
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
2300Sstevel@tonic-gate 	    "_rsm_fd is %d\n", _rsm_fd));
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (fcntl(_rsm_fd, F_SETFD, FD_CLOEXEC) < 0) {
2330Sstevel@tonic-gate 	    DBPRINTF((RSM_LIBRARY, RSM_ERR,
2340Sstevel@tonic-gate 		"F_SETFD failed\n"));
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/* get mapping generation number page info */
2380Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_BAR_INFO, &msg) < 0) {
2390Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
2400Sstevel@tonic-gate 		    "RSM_IOCTL_BAR_INFO failed\n"));
2410Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
2420Sstevel@tonic-gate 		return (errno);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * bar_va is mapped to the mapping generation number page
2470Sstevel@tonic-gate 	 * in order to support close barrier
2480Sstevel@tonic-gate 	 */
2490Sstevel@tonic-gate 	/* LINTED */
2500Sstevel@tonic-gate 	bar_va = (rsm_gnum_t *)mmap(NULL, msg.len,
2510Sstevel@tonic-gate 	    PROT_READ, MAP_SHARED, _rsm_fd, msg.off);
2520Sstevel@tonic-gate 	if (bar_va == (rsm_gnum_t *)MAP_FAILED) {
2530Sstevel@tonic-gate 		bar_va = NULL;
2540Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
2550Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
2560Sstevel@tonic-gate 		    "unable to map barrier page\n"));
2570Sstevel@tonic-gate 		return (RSMERR_MAP_FAILED);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	mutex_unlock(&_rsm_lock);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* get local nodeid */
2630Sstevel@tonic-gate 	e = rsm_get_interconnect_topology(&tp);
2640Sstevel@tonic-gate 	if (e != RSM_SUCCESS) {
2650Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
2660Sstevel@tonic-gate 		    "unable to obtain topology data\n"));
2670Sstevel@tonic-gate 		return (e);
2680Sstevel@tonic-gate 	} else
2690Sstevel@tonic-gate 	    rsm_local_nodeid = tp->topology_hdr.local_nodeid;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	rsm_free_interconnect_topology(tp);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
2740Sstevel@tonic-gate 	    "_rsm_librsm_init: exit\n"));
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	return (RSM_SUCCESS);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate static int
2800Sstevel@tonic-gate _rsm_loopbackload(caddr_t name, int unit, rsm_controller_t **chdl)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	rsm_controller_t *p;
2830Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_DEBUG_VERBOSE,
2860Sstevel@tonic-gate 	    "_rsm_loopbackload: enter\n"));
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * For now do this, but we should open some file and read the
2890Sstevel@tonic-gate 	 * list of supported controllers and there numbers.
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	p = (rsm_controller_t *)malloc(sizeof (*p) + strlen(name) + 1);
2930Sstevel@tonic-gate 	if (!p) {
2940Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_ERR,
2950Sstevel@tonic-gate 		    "not enough memory\n"));
2960Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	msg.cname = name;
3000Sstevel@tonic-gate 	msg.cname_len = strlen(name) +1;
3010Sstevel@tonic-gate 	msg.cnum = unit;
3020Sstevel@tonic-gate 	msg.arg = (caddr_t)&p->cntr_attr;
3030Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_ATTR, &msg) < 0) {
3040Sstevel@tonic-gate 		int error = errno;
3050Sstevel@tonic-gate 		free((void *)p);
3060Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_ERR,
3070Sstevel@tonic-gate 		    "RSM_IOCTL_ATTR failed\n"));
3080Sstevel@tonic-gate 		return (error);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	__rsmloopback_init_ops(&loopback_ops);
3120Sstevel@tonic-gate 	__rsmdefault_setops(&loopback_ops);
3130Sstevel@tonic-gate 	p->cntr_segops = &loopback_ops;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * Should add this entry into list
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	p->cntr_fd = _rsm_fd;
3190Sstevel@tonic-gate 	p->cntr_name = strcpy((char *)(p+1), name);
3200Sstevel@tonic-gate 	p->cntr_unit = unit;
3210Sstevel@tonic-gate 	p->cntr_refcnt = 1;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	mutex_init(&p->cntr_lock, USYNC_THREAD, NULL);
3250Sstevel@tonic-gate 	cond_init(&p->cntr_cv, USYNC_THREAD, NULL);
3260Sstevel@tonic-gate 	p->cntr_rqlist = NULL;
3270Sstevel@tonic-gate 	p->cntr_segops->rsm_get_lib_attr(&p->cntr_lib_attr);
3280Sstevel@tonic-gate 	p->cntr_next = controller_list;
3290Sstevel@tonic-gate 	controller_list = p;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	*chdl = p;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_DEBUG_VERBOSE,
3340Sstevel@tonic-gate 	    "_rsm_loopbackload: exit\n"));
3350Sstevel@tonic-gate 	return (RSM_SUCCESS);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate static int
3400Sstevel@tonic-gate _rsm_modload(caddr_t name, int unit, rsmapi_controller_handle_t *controller)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	int error = RSM_SUCCESS;
3430Sstevel@tonic-gate 	char clib[MAX_STRLEN];
3440Sstevel@tonic-gate 	rsm_controller_t *p = NULL;
3450Sstevel@tonic-gate 	void *dlh;
3460Sstevel@tonic-gate 	rsm_attach_entry_t fptr;
3470Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
3500Sstevel@tonic-gate 	    "_rsm_modload: enter\n"));
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	(void) sprintf(clib, "%s.so", name);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/* found entry, try to load library */
3550Sstevel@tonic-gate 	dlh = dlopen(clib, RTLD_LAZY);
3560Sstevel@tonic-gate 	if (dlh == NULL) {
3570Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
3580Sstevel@tonic-gate 		    "unable to find plugin library\n"));
3590Sstevel@tonic-gate 		error = RSMERR_CTLR_NOT_PRESENT;
3600Sstevel@tonic-gate 		goto skiplib;
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	(void) sprintf(clib, "%s_opendevice", name);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	fptr = (rsm_attach_entry_t)dlsym(dlh, clib); /* lint !e611 */
3660Sstevel@tonic-gate 	if (fptr != NULL) {
3670Sstevel@tonic-gate 		/* allocate new lib structure */
3680Sstevel@tonic-gate 		/* get ops handler, attr and ops */
3690Sstevel@tonic-gate 		p = (rsm_controller_t *)malloc(sizeof (*p) + strlen(name) + 1);
3700Sstevel@tonic-gate 		if (p != NULL) {
3710Sstevel@tonic-gate 			error = fptr(unit, &p->cntr_segops);
3720Sstevel@tonic-gate 		} else {
3730Sstevel@tonic-gate 			error = RSMERR_INSUFFICIENT_MEM;
3740Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
3750Sstevel@tonic-gate 			    "not enough memory\n"));
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 	} else {
3780Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
3790Sstevel@tonic-gate 		    "can't find symbol %s\n", clib));
3800Sstevel@tonic-gate 		error = RSMERR_CTLR_NOT_PRESENT;
3810Sstevel@tonic-gate 		(void) dlclose(dlh);
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate skiplib:
3850Sstevel@tonic-gate 	if ((error != RSM_SUCCESS) || (p == NULL)) {
3860Sstevel@tonic-gate 		if (p != NULL)
3870Sstevel@tonic-gate 			free((void *)p);
3880Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
3890Sstevel@tonic-gate 		    "_rsm_modload error %d\n", error));
3900Sstevel@tonic-gate 		return (error);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/* check the version number */
3940Sstevel@tonic-gate 	if (p->cntr_segops->rsm_version != RSM_LIB_VERSION) {
3950Sstevel@tonic-gate 		/* bad version number */
3960Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
3970Sstevel@tonic-gate 		    "wrong version; "
3980Sstevel@tonic-gate 		    "found %d, expected %d\n",
3990Sstevel@tonic-gate 		    p->cntr_segops->rsm_version, RSM_LIB_VERSION));
4000Sstevel@tonic-gate 		free(p);
4010Sstevel@tonic-gate 		return (RSMERR_BAD_LIBRARY_VERSION);
4020Sstevel@tonic-gate 	} else {
4030Sstevel@tonic-gate 		/* pass the fuctions to NDI library */
4040Sstevel@tonic-gate 		if ((p->cntr_segops->rsm_register_lib_funcs == NULL) ||
4050Sstevel@tonic-gate 		    (p->cntr_segops->rsm_register_lib_funcs(
4060Sstevel@tonic-gate 		    &lib_functions) != RSM_SUCCESS)) {
4070Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
4080Sstevel@tonic-gate 			    "RSMNDI library not registering lib functions\n"));
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		/* get controller attributes */
4120Sstevel@tonic-gate 		msg.cnum = unit;
4130Sstevel@tonic-gate 		msg.cname = name;
4140Sstevel@tonic-gate 		msg.cname_len = strlen(name) +1;
4150Sstevel@tonic-gate 		msg.arg = (caddr_t)&p->cntr_attr;
4160Sstevel@tonic-gate 		if (ioctl(_rsm_fd, RSM_IOCTL_ATTR, &msg) < 0) {
4170Sstevel@tonic-gate 			error = errno;
4180Sstevel@tonic-gate 			free((void *)p);
4190Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
4200Sstevel@tonic-gate 			    "RSM_IOCTL_ATTR failed\n"));
4210Sstevel@tonic-gate 			return (error);
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 		/* set controller access functions */
4250Sstevel@tonic-gate 		__rsmdefault_setops(p->cntr_segops);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		mutex_init(&p->cntr_lock, USYNC_THREAD, NULL);
4280Sstevel@tonic-gate 		cond_init(&p->cntr_cv, USYNC_THREAD, NULL);
4290Sstevel@tonic-gate 		p->cntr_rqlist = NULL;
4300Sstevel@tonic-gate 		p->cntr_segops->rsm_get_lib_attr(&p->cntr_lib_attr);
4310Sstevel@tonic-gate 		/* insert into list of controllers */
4320Sstevel@tonic-gate 		p->cntr_name = strcpy((char *)(p+1), name);
4330Sstevel@tonic-gate 		p->cntr_fd = _rsm_fd;
4340Sstevel@tonic-gate 		p->cntr_unit = unit;
4350Sstevel@tonic-gate 		p->cntr_refcnt = 1;	/* first reference */
4360Sstevel@tonic-gate 		p->cntr_next = controller_list;
4370Sstevel@tonic-gate 		controller_list = p;
4380Sstevel@tonic-gate 		*controller = (rsmapi_controller_handle_t)p;
4390Sstevel@tonic-gate 		errno = RSM_SUCCESS;
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
4430Sstevel@tonic-gate 	    "_rsm_modload: exit\n"));
4440Sstevel@tonic-gate 	return (error);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate /*
4480Sstevel@tonic-gate  * inserts a given segment handle into the pollfd table, this is called
4490Sstevel@tonic-gate  * when rsm_memseg_get_pollfd() is called the first time on a segment handle.
4500Sstevel@tonic-gate  * Returns RSM_SUCCESS if successful otherwise the error code is returned
4510Sstevel@tonic-gate  */
4520Sstevel@tonic-gate static int
4530Sstevel@tonic-gate _rsm_insert_pollfd_table(int segfd, minor_t segrnum)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	int i;
4560Sstevel@tonic-gate 	int hash;
4570Sstevel@tonic-gate 	rsm_pollfd_chunk_t *chunk;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	hash = RSM_POLLFD_HASH(segfd);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	mutex_lock(&pollfd_table.lock);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	chunk = pollfd_table.buckets[hash];
4640Sstevel@tonic-gate 	while (chunk) {
4650Sstevel@tonic-gate 		if (chunk->nfree > 0)
4660Sstevel@tonic-gate 			break;
4670Sstevel@tonic-gate 		chunk = chunk->next;
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (!chunk) { /* couldn't find a free chunk - allocate a new one */
4710Sstevel@tonic-gate 		chunk = malloc(sizeof (rsm_pollfd_chunk_t));
4720Sstevel@tonic-gate 		if (!chunk) {
4730Sstevel@tonic-gate 			mutex_unlock(&pollfd_table.lock);
4740Sstevel@tonic-gate 			return (RSMERR_INSUFFICIENT_MEM);
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 		chunk->nfree = RSM_POLLFD_PER_CHUNK - 1;
4770Sstevel@tonic-gate 		chunk->fdarray[0].fd = segfd;
4780Sstevel@tonic-gate 		chunk->fdarray[0].segrnum = segrnum;
4790Sstevel@tonic-gate 		for (i = 1; i < RSM_POLLFD_PER_CHUNK; i++) {
4800Sstevel@tonic-gate 			chunk->fdarray[i].fd = -1;
4810Sstevel@tonic-gate 			chunk->fdarray[i].segrnum = 0;
4820Sstevel@tonic-gate 		}
4830Sstevel@tonic-gate 		/* insert this into the hash table */
4840Sstevel@tonic-gate 		chunk->next = pollfd_table.buckets[hash];
4850Sstevel@tonic-gate 		pollfd_table.buckets[hash] = chunk;
4860Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
4870Sstevel@tonic-gate 		    "rsm_insert_pollfd: new chunk(%p) @ %d for %d:%d\n",
4880Sstevel@tonic-gate 		    chunk, hash, segfd, segrnum));
4890Sstevel@tonic-gate 	} else { /* a chunk with free slot was found */
4900Sstevel@tonic-gate 		for (i = 0; i < RSM_POLLFD_PER_CHUNK; i++) {
4910Sstevel@tonic-gate 			if (chunk->fdarray[i].fd == -1) {
4920Sstevel@tonic-gate 				chunk->fdarray[i].fd = segfd;
4930Sstevel@tonic-gate 				chunk->fdarray[i].segrnum = segrnum;
4940Sstevel@tonic-gate 				chunk->nfree--;
4950Sstevel@tonic-gate 				break;
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 		}
4980Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
4990Sstevel@tonic-gate 		    "rsm_insert_pollfd: inserted @ %d for %d:%d chunk(%p)\n",
5000Sstevel@tonic-gate 		    hash, segfd, segrnum, chunk));
5010Sstevel@tonic-gate 		assert(i < RSM_POLLFD_PER_CHUNK);
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	mutex_unlock(&pollfd_table.lock);
5050Sstevel@tonic-gate 	return (RSM_SUCCESS);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate  * Given a file descriptor returns the corresponding segment handles
5100Sstevel@tonic-gate  * resource number, if the fd is not found returns 0. 0 is not a valid
5110Sstevel@tonic-gate  * minor number for a rsmapi segment since it is used for the barrier
5120Sstevel@tonic-gate  * resource.
5130Sstevel@tonic-gate  */
5140Sstevel@tonic-gate static minor_t
5150Sstevel@tonic-gate _rsm_lookup_pollfd_table(int segfd)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate 	int i;
5180Sstevel@tonic-gate 	rsm_pollfd_chunk_t	*chunk;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (segfd < 0)
5210Sstevel@tonic-gate 		return (0);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	mutex_lock(&pollfd_table.lock);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	chunk = pollfd_table.buckets[RSM_POLLFD_HASH(segfd)];
5260Sstevel@tonic-gate 	while (chunk) {
5270Sstevel@tonic-gate 		assert(chunk->nfree < RSM_POLLFD_PER_CHUNK);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		for (i = 0; i < RSM_POLLFD_PER_CHUNK; i++) {
5300Sstevel@tonic-gate 			if (chunk->fdarray[i].fd == segfd) {
5310Sstevel@tonic-gate 				mutex_unlock(&pollfd_table.lock);
5320Sstevel@tonic-gate 				DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
5330Sstevel@tonic-gate 				    "rsm_lookup_pollfd: found(%d) rnum(%d)\n",
5340Sstevel@tonic-gate 				    segfd, chunk->fdarray[i].segrnum));
5350Sstevel@tonic-gate 				return (chunk->fdarray[i].segrnum);
5360Sstevel@tonic-gate 			}
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 		chunk = chunk->next;
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	mutex_unlock(&pollfd_table.lock);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
5440Sstevel@tonic-gate 	    "rsm_lookup_pollfd: not found(%d)\n", segfd));
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	return (0);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * Remove the entry corresponding to the given file descriptor from the
5510Sstevel@tonic-gate  * pollfd table.
5520Sstevel@tonic-gate  */
5530Sstevel@tonic-gate static void
5540Sstevel@tonic-gate _rsm_remove_pollfd_table(int segfd)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	int i;
5570Sstevel@tonic-gate 	int hash;
5580Sstevel@tonic-gate 	rsm_pollfd_chunk_t	*chunk;
5590Sstevel@tonic-gate 	rsm_pollfd_chunk_t	*prev_chunk;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (segfd < 0)
5620Sstevel@tonic-gate 		return;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	hash = RSM_POLLFD_HASH(segfd);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	mutex_lock(&pollfd_table.lock);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	prev_chunk = chunk = pollfd_table.buckets[hash];
5690Sstevel@tonic-gate 	while (chunk) {
5700Sstevel@tonic-gate 		assert(chunk->nfree < RSM_POLLFD_PER_CHUNK);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		for (i = 0; i < RSM_POLLFD_PER_CHUNK; i++) {
5730Sstevel@tonic-gate 			if (chunk->fdarray[i].fd == segfd) {
5740Sstevel@tonic-gate 				DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
5750Sstevel@tonic-gate 				    "rsm_remove_pollfd: %d:%d\n",
5760Sstevel@tonic-gate 				    chunk->fdarray[i].fd,
5770Sstevel@tonic-gate 				    chunk->fdarray[i].segrnum));
5780Sstevel@tonic-gate 				chunk->fdarray[i].fd = -1;
5790Sstevel@tonic-gate 				chunk->fdarray[i].segrnum = 0;
5800Sstevel@tonic-gate 				chunk->nfree++;
5810Sstevel@tonic-gate 				if (chunk->nfree == RSM_POLLFD_PER_CHUNK) {
5820Sstevel@tonic-gate 					/* chunk is empty free it */
5830Sstevel@tonic-gate 					if (prev_chunk == chunk) {
5840Sstevel@tonic-gate 						pollfd_table.buckets[hash] =
5850Sstevel@tonic-gate 						    chunk->next;
5860Sstevel@tonic-gate 					} else {
5870Sstevel@tonic-gate 						prev_chunk->next = chunk->next;
5880Sstevel@tonic-gate 					}
5890Sstevel@tonic-gate 					DBPRINTF((RSM_LIBRARY,
5900Sstevel@tonic-gate 					    RSM_DEBUG_VERBOSE,
5910Sstevel@tonic-gate 					    "rsm_remove_pollfd:free(%p)\n",
5920Sstevel@tonic-gate 					    chunk));
5930Sstevel@tonic-gate 					free(chunk);
5940Sstevel@tonic-gate 					mutex_unlock(&pollfd_table.lock);
5950Sstevel@tonic-gate 					return;
5960Sstevel@tonic-gate 				}
5970Sstevel@tonic-gate 			}
5980Sstevel@tonic-gate 		}
5990Sstevel@tonic-gate 		prev_chunk = chunk;
6000Sstevel@tonic-gate 		chunk = chunk->next;
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	mutex_unlock(&pollfd_table.lock);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate int
6070Sstevel@tonic-gate _rsm_get_controller(char *name, rsmapi_controller_handle_t *chdl)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	rsm_controller_t *p;
6100Sstevel@tonic-gate 	char	cntr_name[MAXNAMELEN];	/* cntr_name=<cntr_type><unit> */
6110Sstevel@tonic-gate 	char	*cntr_type;
6120Sstevel@tonic-gate 	int	unit = 0;
6130Sstevel@tonic-gate 	int	i, e;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6160Sstevel@tonic-gate 	    "rsm_get_controller: enter\n"));
6170Sstevel@tonic-gate 	/*
6180Sstevel@tonic-gate 	 * Lookup controller name and return ops vector and controller
6190Sstevel@tonic-gate 	 * structure
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if (!chdl) {
6230Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
6240Sstevel@tonic-gate 		    "Invalid controller handle\n"));
6250Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	if (!name) {
6280Sstevel@tonic-gate 		/* use loopback if null */
6290Sstevel@tonic-gate 		cntr_type = LOOPBACK;
6300Sstevel@tonic-gate 	} else {
6310Sstevel@tonic-gate 		(void) strcpy(cntr_name, name);
6320Sstevel@tonic-gate 		/* scan from the end till a non-digit is found */
6330Sstevel@tonic-gate 		for (i = strlen(cntr_name) - 1; i >= 0; i--) {
6340Sstevel@tonic-gate 			if (! isdigit((int)cntr_name[i]))
6350Sstevel@tonic-gate 				break;
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 		i++;
6380Sstevel@tonic-gate 		unit = atoi((char *)cntr_name+i);
6390Sstevel@tonic-gate 		cntr_name[i] = '\0';	/* null terminate the cntr_type part */
6400Sstevel@tonic-gate 		cntr_type = (char *)cntr_name;
6410Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6420Sstevel@tonic-gate 		    "cntr_type=%s, instance=%d\n",
6430Sstevel@tonic-gate 		    cntr_type, unit));
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	/* protect the controller_list by locking the device/library */
6470Sstevel@tonic-gate 	mutex_lock(&_rsm_lock);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	for (p = controller_list; p; p = p->cntr_next) {
6500Sstevel@tonic-gate 		if (!strcasecmp(p->cntr_name, cntr_type) &&
6510Sstevel@tonic-gate 		    !strcasecmp(cntr_type, LOOPBACK)) {
6520Sstevel@tonic-gate 			p->cntr_refcnt++;
6530Sstevel@tonic-gate 			*chdl = (rsmapi_controller_handle_t)p;
6540Sstevel@tonic-gate 			mutex_unlock(&_rsm_lock);
6550Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6560Sstevel@tonic-gate 			    "rsm_get_controller: exit\n"));
6570Sstevel@tonic-gate 			return (RSM_SUCCESS);
6580Sstevel@tonic-gate 		} else if (!strcasecmp(p->cntr_name, cntr_type) &&
6590Sstevel@tonic-gate 		    (p->cntr_unit == unit)) {
6600Sstevel@tonic-gate 			p->cntr_refcnt++;
6610Sstevel@tonic-gate 			*chdl = (rsmapi_controller_handle_t)p;
6620Sstevel@tonic-gate 			mutex_unlock(&_rsm_lock);
6630Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6640Sstevel@tonic-gate 			    "rsm_get_controller: exit\n"));
6650Sstevel@tonic-gate 			return (RSM_SUCCESS);
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (!strcasecmp(cntr_type, LOOPBACK)) {
6710Sstevel@tonic-gate 		e = _rsm_loopbackload(cntr_type, unit,
6720Sstevel@tonic-gate 		    (rsm_controller_t **)chdl);
6730Sstevel@tonic-gate 	} else {
6740Sstevel@tonic-gate 		e = _rsm_modload(cntr_type, unit, chdl);
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	mutex_unlock(&_rsm_lock);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6800Sstevel@tonic-gate 	    " rsm_get_controller: exit\n"));
6810Sstevel@tonic-gate 	return (e);
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate int
6850Sstevel@tonic-gate _rsm_release_controller(rsmapi_controller_handle_t cntr_handle)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	int			e = RSM_SUCCESS;
6880Sstevel@tonic-gate 	rsm_controller_t	*chdl = (rsm_controller_t *)cntr_handle;
6890Sstevel@tonic-gate 	rsm_controller_t	*curr, *prev;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6920Sstevel@tonic-gate 	    "rsm_release_controller: enter\n"));
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	mutex_lock(&_rsm_lock);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	if (chdl->cntr_refcnt == 0) {
6970Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
6980Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
6990Sstevel@tonic-gate 		    "controller reference count is zero\n"));
7000Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	chdl->cntr_refcnt--;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	if (chdl->cntr_refcnt > 0) {
7060Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
7070Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
7080Sstevel@tonic-gate 		    "rsm_release_controller: exit\n"));
7090Sstevel@tonic-gate 		return (RSM_SUCCESS);
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	e = chdl->cntr_segops->rsm_closedevice(cntr_handle);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/*
7150Sstevel@tonic-gate 	 * remove the controller in any case from the controller list
7160Sstevel@tonic-gate 	 */
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	prev = curr = controller_list;
7190Sstevel@tonic-gate 	while (curr != NULL) {
7200Sstevel@tonic-gate 		if (curr == chdl) {
7210Sstevel@tonic-gate 			if (curr == prev) {
7220Sstevel@tonic-gate 				controller_list = curr->cntr_next;
7230Sstevel@tonic-gate 			} else {
7240Sstevel@tonic-gate 				prev->cntr_next = curr->cntr_next;
7250Sstevel@tonic-gate 			}
7260Sstevel@tonic-gate 			free(curr);
7270Sstevel@tonic-gate 			break;
7280Sstevel@tonic-gate 		}
7290Sstevel@tonic-gate 		prev = curr;
7300Sstevel@tonic-gate 		curr = curr->cntr_next;
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	mutex_unlock(&_rsm_lock);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
7350Sstevel@tonic-gate 	    "rsm_release_controller: exit\n"));
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	return (e);
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate int _rsm_get_controller_attr(rsmapi_controller_handle_t chandle,
7410Sstevel@tonic-gate     rsmapi_controller_attr_t *attr)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate 	rsm_controller_t *p;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
7460Sstevel@tonic-gate 	    "rsm_get_controller_attr: enter\n"));
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if (!chandle) {
7490Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7500Sstevel@tonic-gate 		    "invalid controller handle\n"));
7510Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (!attr) {
7550Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7560Sstevel@tonic-gate 		    "invalid attribute pointer\n"));
7570Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	p = (rsm_controller_t *)chandle;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	mutex_lock(&_rsm_lock);
7630Sstevel@tonic-gate 	if (p->cntr_refcnt == 0) {
7640Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
7650Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7660Sstevel@tonic-gate 		    "cntr refcnt is 0\n"));
7670Sstevel@tonic-gate 		return (RSMERR_CTLR_NOT_PRESENT);
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	/* copy only the user part of the attr structure */
7710Sstevel@tonic-gate 	attr->attr_direct_access_sizes =
7720Sstevel@tonic-gate 	    p->cntr_attr.attr_direct_access_sizes;
7730Sstevel@tonic-gate 	attr->attr_atomic_sizes =
7740Sstevel@tonic-gate 	    p->cntr_attr.attr_atomic_sizes;
7750Sstevel@tonic-gate 	attr->attr_page_size =
7760Sstevel@tonic-gate 	    p->cntr_attr.attr_page_size;
7770Sstevel@tonic-gate 	attr->attr_max_export_segment_size =
7780Sstevel@tonic-gate 	    p->cntr_attr.attr_max_export_segment_size;
7790Sstevel@tonic-gate 	attr->attr_tot_export_segment_size =
7800Sstevel@tonic-gate 	    p->cntr_attr.attr_tot_export_segment_size;
7810Sstevel@tonic-gate 	attr->attr_max_export_segments =
7820Sstevel@tonic-gate 	    p->cntr_attr.attr_max_export_segments;
7830Sstevel@tonic-gate 	attr->attr_max_import_map_size =
7840Sstevel@tonic-gate 	    p->cntr_attr.attr_max_import_map_size;
7850Sstevel@tonic-gate 	attr->attr_tot_import_map_size =
7860Sstevel@tonic-gate 	    p->cntr_attr.attr_tot_import_map_size;
7870Sstevel@tonic-gate 	attr->attr_max_import_segments =
7880Sstevel@tonic-gate 	    p->cntr_attr.attr_max_import_segments;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	mutex_unlock(&_rsm_lock);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
7930Sstevel@tonic-gate 	    "rsm_get_controller_attr: exit\n"));
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	return (RSM_SUCCESS);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate /*
8010Sstevel@tonic-gate  * Create a segment handle for the virtual address range specified
8020Sstevel@tonic-gate  * by vaddr and size
8030Sstevel@tonic-gate  */
8040Sstevel@tonic-gate int
8050Sstevel@tonic-gate _rsm_memseg_export_create(rsmapi_controller_handle_t controller,
8060Sstevel@tonic-gate     rsm_memseg_export_handle_t *memseg,
8070Sstevel@tonic-gate     void *vaddr,
8080Sstevel@tonic-gate     size_t length,
8090Sstevel@tonic-gate     uint_t flags)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	rsm_controller_t *chdl = (rsm_controller_t *)controller;
8130Sstevel@tonic-gate 	rsmseg_handle_t *p;
8140Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
8150Sstevel@tonic-gate 	int e;
8160Sstevel@tonic-gate #ifndef	_LP64
8170Sstevel@tonic-gate 	int tmpfd;
8180Sstevel@tonic-gate #endif
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
8210Sstevel@tonic-gate 	    "rsm_memseg_export_create: enter\n"));
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	if (!controller) {
8240Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
8250Sstevel@tonic-gate 		    "invalid controller handle\n"));
8260Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
8270Sstevel@tonic-gate 	}
8280Sstevel@tonic-gate 	if (!memseg) {
8290Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
8300Sstevel@tonic-gate 		    "invalid segment handle\n"));
8310Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	*memseg = 0;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * Check vaddr and size alignment, both must be mmu page size
8380Sstevel@tonic-gate 	 * aligned
8390Sstevel@tonic-gate 	 */
8400Sstevel@tonic-gate 	if (!vaddr) {
8410Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
8420Sstevel@tonic-gate 		    "invalid arguments\n"));
8430Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	if (!length) {
8470Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
8480Sstevel@tonic-gate 		    "invalid arguments\n"));
8490Sstevel@tonic-gate 		return (RSMERR_BAD_LENGTH);
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	if (((size_t)vaddr & (PAGESIZE - 1)) ||
8530Sstevel@tonic-gate 		(length & (PAGESIZE - 1))) {
8540Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
8550Sstevel@tonic-gate 		    "invalid mem alignment for vaddr or length\n"));
8560Sstevel@tonic-gate 		return (RSMERR_BAD_MEM_ALIGNMENT);
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/*
8600Sstevel@tonic-gate 	 * The following check does not apply for loopback controller
8610Sstevel@tonic-gate 	 * since for the loopback adapter, the attr_max_export_segment_size
8620Sstevel@tonic-gate 	 * is always 0.
8630Sstevel@tonic-gate 	 */
8640Sstevel@tonic-gate 	if (strcasecmp(chdl->cntr_name, LOOPBACK)) {
8650Sstevel@tonic-gate 		if (length > chdl->cntr_attr.attr_max_export_segment_size) {
8660Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
8670Sstevel@tonic-gate 			    "length exceeds controller limits\n"));
8680Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
8690Sstevel@tonic-gate 			    "controller limits %d\n",
8700Sstevel@tonic-gate 			    chdl->cntr_attr.attr_max_export_segment_size));
8710Sstevel@tonic-gate 			return (RSMERR_BAD_LENGTH);
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	p = (rsmseg_handle_t *)malloc(sizeof (*p));
8760Sstevel@tonic-gate 	if (p == NULL) {
8770Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
8780Sstevel@tonic-gate 		    "not enough memory\n"));
8790Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	p->rsmseg_fd = open(DEVRSM, O_RDWR);
8830Sstevel@tonic-gate 	if (p->rsmseg_fd < 0) {
8840Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
8850Sstevel@tonic-gate 		    "unable to open device /dev/rsm\n"));
8860Sstevel@tonic-gate 		free((void *)p);
8870Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_RESOURCES);
8880Sstevel@tonic-gate 	}
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate #ifndef	_LP64
8910Sstevel@tonic-gate 	/*
8920Sstevel@tonic-gate 	 * libc can't handle fd's greater than 255,  in order to
8930Sstevel@tonic-gate 	 * insure that these values remain available make /dev/rsm
8940Sstevel@tonic-gate 	 * fd > 255. Note: not needed for LP64
8950Sstevel@tonic-gate 	 */
8960Sstevel@tonic-gate 	tmpfd = fcntl(p->rsmseg_fd, F_DUPFD, 256);
8970Sstevel@tonic-gate 	e = errno;
8980Sstevel@tonic-gate 	if (tmpfd < 0) {
8990Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9000Sstevel@tonic-gate 		    "F_DUPFD failed\n"));
9010Sstevel@tonic-gate 	} else {
9020Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
9030Sstevel@tonic-gate 		p->rsmseg_fd = tmpfd;
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate #endif	/*	_LP64	*/
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE, ""
9080Sstevel@tonic-gate 	    "rsmseg_fd is %d\n", p->rsmseg_fd));
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	if (fcntl(p->rsmseg_fd, F_SETFD, FD_CLOEXEC) < 0) {
9110Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9120Sstevel@tonic-gate 		    "F_SETFD failed\n"));
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	p->rsmseg_state = EXPORT_CREATE;
9160Sstevel@tonic-gate 	p->rsmseg_size = length;
9170Sstevel@tonic-gate 	/* increment controller handle */
9180Sstevel@tonic-gate 	p->rsmseg_controller = chdl;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	/* try to bind user address range */
9210Sstevel@tonic-gate 	msg.cnum = chdl->cntr_unit;
9220Sstevel@tonic-gate 	msg.cname = chdl->cntr_name;
9230Sstevel@tonic-gate 	msg.cname_len = strlen(chdl->cntr_name) +1;
9240Sstevel@tonic-gate 	msg.vaddr = vaddr;
9250Sstevel@tonic-gate 	msg.len = length;
9260Sstevel@tonic-gate 	msg.perm = flags;
9270Sstevel@tonic-gate 	msg.off = 0;
9280Sstevel@tonic-gate 	e = RSM_IOCTL_BIND;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	/* Try to bind */
9310Sstevel@tonic-gate 	if (ioctl(p->rsmseg_fd, e, &msg) < 0) {
9320Sstevel@tonic-gate 		e = errno;
9330Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
9340Sstevel@tonic-gate 		free((void *)p);
9350Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9360Sstevel@tonic-gate 		    "RSM_IOCTL_BIND failed\n"));
9370Sstevel@tonic-gate 		return (e);
9380Sstevel@tonic-gate 	}
9390Sstevel@tonic-gate 	/* OK */
9400Sstevel@tonic-gate 	p->rsmseg_type = RSM_EXPORT_SEG;
9410Sstevel@tonic-gate 	p->rsmseg_vaddr = vaddr;
9420Sstevel@tonic-gate 	p->rsmseg_size = length;
9430Sstevel@tonic-gate 	p->rsmseg_state = EXPORT_BIND;
9440Sstevel@tonic-gate 	p->rsmseg_pollfd_refcnt = 0;
9450Sstevel@tonic-gate 	p->rsmseg_rnum = msg.rnum;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	mutex_init(&p->rsmseg_lock, USYNC_THREAD, NULL);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	*memseg = (rsm_memseg_export_handle_t)p;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
9520Sstevel@tonic-gate 	    "rsm_memseg_export_create: exit\n"));
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	return (RSM_SUCCESS);
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate int
9580Sstevel@tonic-gate _rsm_memseg_export_destroy(rsm_memseg_export_handle_t memseg)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	rsmseg_handle_t *seg;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
9630Sstevel@tonic-gate 	    "rsm_memseg_export_destroy: enter\n"));
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	if (!memseg) {
9660Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9670Sstevel@tonic-gate 		    "invalid segment handle\n"));
9680Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	seg = (rsmseg_handle_t *)memseg;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
9740Sstevel@tonic-gate 	if (seg->rsmseg_pollfd_refcnt) {
9750Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
9760Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9770Sstevel@tonic-gate 		    "segment reference count not zero\n"));
9780Sstevel@tonic-gate 		return (RSMERR_POLLFD_IN_USE);
9790Sstevel@tonic-gate 	}
9800Sstevel@tonic-gate 	else
9810Sstevel@tonic-gate 		seg->rsmseg_state = EXPORT_BIND;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	(void) close(seg->rsmseg_fd);
9860Sstevel@tonic-gate 	mutex_destroy(&seg->rsmseg_lock);
9870Sstevel@tonic-gate 	free((void *)seg);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
9900Sstevel@tonic-gate 	    "rsm_memseg_export_destroy: exit\n"));
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	return (RSM_SUCCESS);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate int
9960Sstevel@tonic-gate _rsm_memseg_export_rebind(rsm_memseg_export_handle_t memseg, void *vaddr,
9970Sstevel@tonic-gate     offset_t off, size_t length)
9980Sstevel@tonic-gate {
9990Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
10000Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
10030Sstevel@tonic-gate 	    "rsm_memseg_export_rebind: enter\n"));
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	off = off;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	if (!seg) {
10080Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10090Sstevel@tonic-gate 		    "invalid segment handle\n"));
10100Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 	if (!vaddr) {
10130Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10140Sstevel@tonic-gate 		    "invalid vaddr\n"));
10150Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	/*
10190Sstevel@tonic-gate 	 * Same as bind except it's ok to have elimint in list.
10200Sstevel@tonic-gate 	 * Call into driver to remove any existing mappings.
10210Sstevel@tonic-gate 	 */
10220Sstevel@tonic-gate 	msg.vaddr = vaddr;
10230Sstevel@tonic-gate 	msg.len = length;
10240Sstevel@tonic-gate 	msg.off = 0;
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
10270Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_REBIND, &msg) < 0) {
10280Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
10290Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10300Sstevel@tonic-gate 		    "RSM_IOCTL_REBIND failed\n"));
10310Sstevel@tonic-gate 		return (errno);
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
10370Sstevel@tonic-gate 	    "rsm_memseg_export_rebind: exit\n"));
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	return (RSM_SUCCESS);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate int
10430Sstevel@tonic-gate _rsm_memseg_export_publish(rsm_memseg_export_handle_t memseg,
10440Sstevel@tonic-gate     rsm_memseg_id_t *seg_id,
10450Sstevel@tonic-gate     rsmapi_access_entry_t access_list[],
10460Sstevel@tonic-gate     uint_t access_list_length)
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
10500Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
10530Sstevel@tonic-gate 	    "rsm_memseg_export_publish: enter\n"));
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	if (seg_id == NULL) {
10560Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10570Sstevel@tonic-gate 		    "invalid segment id\n"));
10580Sstevel@tonic-gate 		return (RSMERR_BAD_SEGID);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	if (!seg) {
10620Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10630Sstevel@tonic-gate 		    "invalid segment handle\n"));
10640Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
10650Sstevel@tonic-gate 	}
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	if (access_list_length > 0 && !access_list) {
10680Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10690Sstevel@tonic-gate 		    "invalid access control list\n"));
10700Sstevel@tonic-gate 		return (RSMERR_BAD_ACL);
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
10740Sstevel@tonic-gate 	if (seg->rsmseg_state != EXPORT_BIND) {
10750Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
10760Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10770Sstevel@tonic-gate 		    "invalid segment state\n"));
10780Sstevel@tonic-gate 		return (RSMERR_SEG_ALREADY_PUBLISHED);
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * seg id < RSM_DLPI_END and in the RSM_USER_APP_ID range
10830Sstevel@tonic-gate 	 * are reserved for internal use.
10840Sstevel@tonic-gate 	 */
10850Sstevel@tonic-gate 	if ((*seg_id > 0) &&
10860Sstevel@tonic-gate 	    ((*seg_id <= RSM_DLPI_ID_END) ||
10870Sstevel@tonic-gate 	    BETWEEN (*seg_id, RSM_USER_APP_ID_BASE, RSM_USER_APP_ID_END))) {
10880Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
10890Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10900Sstevel@tonic-gate 		    "invalid segment id\n"));
10910Sstevel@tonic-gate 		return (RSMERR_RESERVED_SEGID);
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	msg.key = *seg_id;
10950Sstevel@tonic-gate 	msg.acl = access_list;
10960Sstevel@tonic-gate 	msg.acl_len = access_list_length;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_PUBLISH, &msg) < 0) {
10990Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11000Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11010Sstevel@tonic-gate 		    "RSM_IOCTL_PUBLISH failed\n"));
11020Sstevel@tonic-gate 		return (errno);
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	seg->rsmseg_keyid = msg.key;
11060Sstevel@tonic-gate 	seg->rsmseg_state = EXPORT_PUBLISH;
11070Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	if (*seg_id == 0)
11100Sstevel@tonic-gate 		*seg_id = msg.key;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
11130Sstevel@tonic-gate 	    "rsm_memseg_export_publish: exit\n"));
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	return (RSM_SUCCESS);
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate int
11200Sstevel@tonic-gate _rsm_memseg_export_unpublish(rsm_memseg_export_handle_t memseg)
11210Sstevel@tonic-gate {
11220Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
11230Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
11260Sstevel@tonic-gate 	    "rsm_memseg_export_unpublish: enter\n"));
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	if (!seg) {
11290Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11300Sstevel@tonic-gate 		    "invalid arguments\n"));
11310Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
11350Sstevel@tonic-gate 	if (seg->rsmseg_state != EXPORT_PUBLISH) {
11360Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11370Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11380Sstevel@tonic-gate 		    "segment not published %d\n",
11390Sstevel@tonic-gate 			seg->rsmseg_keyid));
11400Sstevel@tonic-gate 		return (RSMERR_SEG_NOT_PUBLISHED);
11410Sstevel@tonic-gate 	}
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	msg.key = seg->rsmseg_keyid;
11440Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_UNPUBLISH, &msg) < 0) {
11450Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11460Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11470Sstevel@tonic-gate 		    "RSM_IOCTL_UNPUBLISH failed\n"));
11480Sstevel@tonic-gate 		return (errno);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	seg->rsmseg_state = EXPORT_BIND;
11520Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
11550Sstevel@tonic-gate 	    "rsm_memseg_export_unpublish: exit\n"));
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	return (RSM_SUCCESS);
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate int
11620Sstevel@tonic-gate _rsm_memseg_export_republish(rsm_memseg_export_handle_t memseg,
11630Sstevel@tonic-gate     rsmapi_access_entry_t access_list[],
11640Sstevel@tonic-gate     uint_t access_list_length)
11650Sstevel@tonic-gate {
11660Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
11670Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
11700Sstevel@tonic-gate 	    "rsm_memseg_export_republish: enter\n"));
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	if (!seg) {
11730Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11740Sstevel@tonic-gate 		    "invalid segment or segment state\n"));
11750Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
11790Sstevel@tonic-gate 	if (seg->rsmseg_state != EXPORT_PUBLISH) {
11800Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11810Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11820Sstevel@tonic-gate 		    "segment not published\n"));
11830Sstevel@tonic-gate 		return (RSMERR_SEG_NOT_PUBLISHED);
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	if (access_list_length > 0 && !access_list) {
11870Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11880Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11890Sstevel@tonic-gate 		    "invalid access control list\n"));
11900Sstevel@tonic-gate 		return (RSMERR_BAD_ACL);
11910Sstevel@tonic-gate 	}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	msg.key = seg->rsmseg_keyid;
11940Sstevel@tonic-gate 	msg.acl = access_list;
11950Sstevel@tonic-gate 	msg.acl_len = access_list_length;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_REPUBLISH, &msg) < 0) {
11980Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11990Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
12000Sstevel@tonic-gate 		    "RSM_IOCTL_REPUBLISH failed\n"));
12010Sstevel@tonic-gate 		return (errno);
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
12060Sstevel@tonic-gate 	    "rsm_memseg_export_republish: exit\n"));
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	return (RSM_SUCCESS);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	/*
12130Sstevel@tonic-gate 	 * import side memory segment operations:
12140Sstevel@tonic-gate 	 */
12150Sstevel@tonic-gate int
12160Sstevel@tonic-gate _rsm_memseg_import_connect(rsmapi_controller_handle_t controller,
12170Sstevel@tonic-gate     rsm_node_id_t node_id,
12180Sstevel@tonic-gate     rsm_memseg_id_t segment_id,
12190Sstevel@tonic-gate     rsm_permission_t perm,
12200Sstevel@tonic-gate     rsm_memseg_import_handle_t *im_memseg)
12210Sstevel@tonic-gate {
12220Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
12230Sstevel@tonic-gate 	rsmseg_handle_t *p;
12240Sstevel@tonic-gate 	rsm_controller_t *cntr = (rsm_controller_t *)controller;
12250Sstevel@tonic-gate #ifndef	_LP64		/* added for fd > 255 fix */
12260Sstevel@tonic-gate 	int tmpfd;
12270Sstevel@tonic-gate #endif
12280Sstevel@tonic-gate 	int e;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
12310Sstevel@tonic-gate 	    "rsm_memseg_import_connect: enter\n"));
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (!cntr) {
12340Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12350Sstevel@tonic-gate 		    "invalid controller handle\n"));
12360Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
12370Sstevel@tonic-gate 	}
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	*im_memseg = 0;
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	p = (rsmseg_handle_t *)malloc(sizeof (*p));
12420Sstevel@tonic-gate 	if (!p) {
12430Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12440Sstevel@tonic-gate 		    "not enough memory\n"));
12450Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
12460Sstevel@tonic-gate 	}
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	if (perm & ~RSM_PERM_RDWR) {
12490Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12500Sstevel@tonic-gate 		    "invalid permissions\n"));
12510Sstevel@tonic-gate 		return (RSMERR_PERM_DENIED);
12520Sstevel@tonic-gate 	}
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	/*
12550Sstevel@tonic-gate 	 * Get size, va from driver
12560Sstevel@tonic-gate 	 */
12570Sstevel@tonic-gate 	msg.cnum = cntr->cntr_unit;
12580Sstevel@tonic-gate 	msg.cname = cntr->cntr_name;
12590Sstevel@tonic-gate 	msg.cname_len = strlen(cntr->cntr_name) +1;
12600Sstevel@tonic-gate 	msg.nodeid = node_id;
12610Sstevel@tonic-gate 	msg.key = segment_id;
12620Sstevel@tonic-gate 	msg.perm = perm;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	p->rsmseg_fd = open(DEVRSM, O_RDWR);
12650Sstevel@tonic-gate 	if (p->rsmseg_fd < 0) {
12660Sstevel@tonic-gate 		    DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12670Sstevel@tonic-gate 			"unable to open /dev/rsm"));
12680Sstevel@tonic-gate 		free((void *)p);
12690Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_RESOURCES);
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate #ifndef	_LP64
12730Sstevel@tonic-gate 	/*
12740Sstevel@tonic-gate 	 * libc can't handle fd's greater than 255,  in order to
12750Sstevel@tonic-gate 	 * insure that these values remain available make /dev/rsm
12760Sstevel@tonic-gate 	 * fd > 255. Note: not needed for LP64
12770Sstevel@tonic-gate 	 */
12780Sstevel@tonic-gate 	tmpfd = fcntl(p->rsmseg_fd, F_DUPFD, 256); /* make fd > 255 */
12790Sstevel@tonic-gate 	e = errno;
12800Sstevel@tonic-gate 	if (tmpfd < 0) {
12810Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12820Sstevel@tonic-gate 		    "F_DUPFD failed\n"));
12830Sstevel@tonic-gate 	} else {
12840Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
12850Sstevel@tonic-gate 		p->rsmseg_fd = tmpfd;
12860Sstevel@tonic-gate 	}
12870Sstevel@tonic-gate #endif	/* _LP64 */
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
12900Sstevel@tonic-gate 	    "rsmseg_fd is %d\n", p->rsmseg_fd));
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	if (fcntl(p->rsmseg_fd, F_SETFD, FD_CLOEXEC) < 0) {
12930Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12940Sstevel@tonic-gate 		    "F_SETFD failed\n"));
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 	if (ioctl(p->rsmseg_fd, RSM_IOCTL_CONNECT, &msg) < 0) {
12970Sstevel@tonic-gate 		e = errno;
12980Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
12990Sstevel@tonic-gate 		free((void *)p);
13000Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13010Sstevel@tonic-gate 		    "RSM_IOCTL_CONNECT failed\n"));
13020Sstevel@tonic-gate 		return (e);
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/*
13060Sstevel@tonic-gate 	 * We connected ok.
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	p->rsmseg_type = RSM_IMPORT_SEG;
13090Sstevel@tonic-gate 	p->rsmseg_state = IMPORT_CONNECT;
13100Sstevel@tonic-gate 	p->rsmseg_keyid = segment_id;
13110Sstevel@tonic-gate 	p->rsmseg_nodeid = node_id;
13120Sstevel@tonic-gate 	p->rsmseg_size = msg.len;
13130Sstevel@tonic-gate 	p->rsmseg_perm = perm;
13140Sstevel@tonic-gate 	p->rsmseg_controller = cntr;
13150Sstevel@tonic-gate 	p->rsmseg_barrier = NULL;
13160Sstevel@tonic-gate 	p->rsmseg_barmode = RSM_BARRIER_MODE_IMPLICIT;
13170Sstevel@tonic-gate 	p->rsmseg_bar = (bar_va ? bar_va + msg.off : &bar_fixed);
13180Sstevel@tonic-gate 	p->rsmseg_gnum = msg.gnum;
13190Sstevel@tonic-gate 	p->rsmseg_pollfd_refcnt = 0;
13200Sstevel@tonic-gate 	p->rsmseg_maplen = 0;    /* initialized, set in import_map */
13210Sstevel@tonic-gate 	p->rsmseg_mapoffset = 0;
13220Sstevel@tonic-gate 	p->rsmseg_flags = 0;
13230Sstevel@tonic-gate 	p->rsmseg_rnum = msg.rnum;
13240Sstevel@tonic-gate 	mutex_init(&p->rsmseg_lock, USYNC_THREAD, NULL);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	p->rsmseg_ops = cntr->cntr_segops;
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	/*
13290Sstevel@tonic-gate 	 * XXX: Based on permission and controller direct_access attribute
13300Sstevel@tonic-gate 	 * we fix the segment ops vector
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	p->rsmseg_vaddr = 0; /* defer mapping till using maps or trys to rw */
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	*im_memseg = (rsm_memseg_import_handle_t)p;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	e =  p->rsmseg_ops->rsm_memseg_import_connect(controller,
13380Sstevel@tonic-gate 	    node_id, segment_id, perm, im_memseg);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	if (e != RSM_SUCCESS) {
13410Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
13420Sstevel@tonic-gate 		mutex_destroy(&p->rsmseg_lock);
13430Sstevel@tonic-gate 		free((void *)p);
13440Sstevel@tonic-gate 	}
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
13470Sstevel@tonic-gate 	    "rsm_memseg_import_connect: exit\n"));
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	return (e);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate int
13540Sstevel@tonic-gate _rsm_memseg_import_disconnect(rsm_memseg_import_handle_t im_memseg)
13550Sstevel@tonic-gate {
13560Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
13570Sstevel@tonic-gate 	int e;
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
13600Sstevel@tonic-gate 	    "rsm_memseg_import_disconnect: enter\n"));
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	if (!seg) {
13630Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13640Sstevel@tonic-gate 		    "invalid segment handle\n"));
13650Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	if (seg->rsmseg_state != IMPORT_CONNECT) {
13690Sstevel@tonic-gate 		if (seg->rsmseg_flags & RSM_IMPLICIT_MAP) {
13700Sstevel@tonic-gate 			e = rsm_memseg_import_unmap(im_memseg);
13710Sstevel@tonic-gate 			if (e != RSM_SUCCESS) {
13720Sstevel@tonic-gate 				DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13730Sstevel@tonic-gate 				    "unmap failure\n"));
13740Sstevel@tonic-gate 				return (e);
13750Sstevel@tonic-gate 			}
13760Sstevel@tonic-gate 		} else {
13770Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13780Sstevel@tonic-gate 			    "segment busy\n"));
13790Sstevel@tonic-gate 			return (RSMERR_SEG_STILL_MAPPED);
13800Sstevel@tonic-gate 		}
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
13840Sstevel@tonic-gate 	if (seg->rsmseg_pollfd_refcnt) {
13850Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
13860Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
13870Sstevel@tonic-gate 		    "segment reference count not zero\n"));
13880Sstevel@tonic-gate 		return (RSMERR_POLLFD_IN_USE);
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	e =  seg->rsmseg_ops->rsm_memseg_import_disconnect(im_memseg);
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
13950Sstevel@tonic-gate 		(void) close(seg->rsmseg_fd);
13960Sstevel@tonic-gate 		mutex_destroy(&seg->rsmseg_lock);
13970Sstevel@tonic-gate 		free((void *)seg);
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
14010Sstevel@tonic-gate 	    "rsm_memseg_import_disconnect: exit\n"));
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	return (e);
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate /*
14070Sstevel@tonic-gate  * import side memory segment operations (read access functions):
14080Sstevel@tonic-gate  */
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate static int
14110Sstevel@tonic-gate __rsm_import_verify_access(rsmseg_handle_t *seg,
14120Sstevel@tonic-gate     off_t offset,
14130Sstevel@tonic-gate     caddr_t datap,
14140Sstevel@tonic-gate     size_t len,
14150Sstevel@tonic-gate     rsm_permission_t perm,
14160Sstevel@tonic-gate     rsm_access_size_t das)
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	int	error;
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
14210Sstevel@tonic-gate 	    " __rsm_import_verify_access: enter\n"));
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	if (!seg) {
14240Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14250Sstevel@tonic-gate 		    "invalid segment handle\n"));
14260Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
14270Sstevel@tonic-gate 	}
14280Sstevel@tonic-gate 	if (!datap) {
14290Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14300Sstevel@tonic-gate 		    "invalid data pointer\n"));
14310Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
14320Sstevel@tonic-gate 	}
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	/*
14350Sstevel@tonic-gate 	 * Check alignment of pointer
14360Sstevel@tonic-gate 	 */
14370Sstevel@tonic-gate 	if ((uintptr_t)datap & (das - 1)) {
14380Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14390Sstevel@tonic-gate 		    "invalid alignment of data pointer\n"));
14400Sstevel@tonic-gate 		return (RSMERR_BAD_MEM_ALIGNMENT);
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	if (offset & (das - 1)) {
14440Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14450Sstevel@tonic-gate 		    "invalid offset\n"));
14460Sstevel@tonic-gate 		return (RSMERR_BAD_MEM_ALIGNMENT);
14470Sstevel@tonic-gate 	}
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	/* make sure that the import seg is connected */
14500Sstevel@tonic-gate 	if (seg->rsmseg_state != IMPORT_CONNECT &&
14510Sstevel@tonic-gate 	    seg->rsmseg_state != IMPORT_MAP) {
14520Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14530Sstevel@tonic-gate 		    "incorrect segment state\n"));
14540Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
14550Sstevel@tonic-gate 	}
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	/* do an implicit map if required */
14580Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_CONNECT) {
14590Sstevel@tonic-gate 		error = __rsm_import_implicit_map(seg, RSM_IOTYPE_PUTGET);
14600Sstevel@tonic-gate 		if (error != RSM_SUCCESS) {
14610Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14620Sstevel@tonic-gate 			    "implicit map failure\n"));
14630Sstevel@tonic-gate 			return (error);
14640Sstevel@tonic-gate 		}
14650Sstevel@tonic-gate 	}
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	if ((seg->rsmseg_perm & perm) != perm) {
14680Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14690Sstevel@tonic-gate 		    "invalid permissions\n"));
14700Sstevel@tonic-gate 		return (RSMERR_PERM_DENIED);
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_MAP) {
14740Sstevel@tonic-gate 		if ((offset < seg->rsmseg_mapoffset) ||
14750Sstevel@tonic-gate 		    (offset + len > seg->rsmseg_mapoffset +
14760Sstevel@tonic-gate 		    seg->rsmseg_maplen)) {
14770Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14780Sstevel@tonic-gate 			    "incorrect offset+length\n"));
14790Sstevel@tonic-gate 			return (RSMERR_BAD_OFFSET);
14800Sstevel@tonic-gate 		}
14810Sstevel@tonic-gate 	} else { /* IMPORT_CONNECT */
14820Sstevel@tonic-gate 		if ((len + offset) > seg->rsmseg_size) {
14830Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14840Sstevel@tonic-gate 			    "incorrect offset+length\n"));
14850Sstevel@tonic-gate 			return (RSMERR_BAD_LENGTH);
14860Sstevel@tonic-gate 		}
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	if ((seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) &&
14900Sstevel@tonic-gate 	    (seg->rsmseg_barrier == NULL)) {
14910Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14920Sstevel@tonic-gate 		    "invalid barrier\n"));
14930Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
14940Sstevel@tonic-gate 	}
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
14970Sstevel@tonic-gate 	    " __rsm_import_verify_access: exit\n"));
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	return (RSM_SUCCESS);
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate static int
15030Sstevel@tonic-gate __rsm_import_implicit_map(rsmseg_handle_t *seg, int iotype)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	caddr_t va;
15060Sstevel@tonic-gate 	int flag = MAP_SHARED;
15070Sstevel@tonic-gate 	int prot = PROT_READ|PROT_WRITE;
15080Sstevel@tonic-gate 	int mapping_reqd = 0;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15110Sstevel@tonic-gate 	    " __rsm_import_implicit_map: enter\n"));
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	if (iotype == RSM_IOTYPE_PUTGET)
15140Sstevel@tonic-gate 		mapping_reqd = seg->rsmseg_controller->cntr_lib_attr->
15150Sstevel@tonic-gate 		    rsm_putget_map_reqd;
15160Sstevel@tonic-gate 	else if (iotype == RSM_IOTYPE_SCATGATH)
15170Sstevel@tonic-gate 		mapping_reqd = seg->rsmseg_controller->cntr_lib_attr->
15180Sstevel@tonic-gate 		    rsm_scatgath_map_reqd;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	if (mapping_reqd) {
15220Sstevel@tonic-gate 		va = mmap(NULL, seg->rsmseg_size, prot,
15230Sstevel@tonic-gate 		    flag, seg->rsmseg_fd, 0);
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 		if (va == MAP_FAILED) {
15260Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
15270Sstevel@tonic-gate 			    "implicit map failed\n"));
15280Sstevel@tonic-gate 			if (errno == ENOMEM || errno == ENXIO ||
15290Sstevel@tonic-gate 			    errno == EOVERFLOW)
15300Sstevel@tonic-gate 				return (RSMERR_BAD_LENGTH);
15310Sstevel@tonic-gate 			else if (errno == ENODEV)
15320Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
15330Sstevel@tonic-gate 			else if (errno == EAGAIN)
15340Sstevel@tonic-gate 				return (RSMERR_INSUFFICIENT_RESOURCES);
15350Sstevel@tonic-gate 			else if (errno == ENOTSUP)
15360Sstevel@tonic-gate 				return (RSMERR_MAP_FAILED);
15370Sstevel@tonic-gate 			else if (errno == EACCES)
15380Sstevel@tonic-gate 				return (RSMERR_BAD_PERMS);
15390Sstevel@tonic-gate 			else
15400Sstevel@tonic-gate 				return (RSMERR_MAP_FAILED);
15410Sstevel@tonic-gate 		}
15420Sstevel@tonic-gate 		seg->rsmseg_vaddr = va;
15430Sstevel@tonic-gate 		seg->rsmseg_maplen = seg->rsmseg_size;
15440Sstevel@tonic-gate 		seg->rsmseg_mapoffset = 0;
15450Sstevel@tonic-gate 		seg->rsmseg_state = IMPORT_MAP;
15460Sstevel@tonic-gate 		seg->rsmseg_flags |= RSM_IMPLICIT_MAP;
15470Sstevel@tonic-gate 	}
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15500Sstevel@tonic-gate 	    " __rsm_import_implicit_map: exit\n"));
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	return (RSM_SUCCESS);
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate int
15560Sstevel@tonic-gate _rsm_memseg_import_get8(rsm_memseg_import_handle_t im_memseg,
15570Sstevel@tonic-gate     off_t offset,
15580Sstevel@tonic-gate     uint8_t *datap,
15590Sstevel@tonic-gate     ulong_t rep_cnt)
15600Sstevel@tonic-gate {
15610Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
15620Sstevel@tonic-gate 	int e;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15650Sstevel@tonic-gate 	    "rsm_memseg_import_get8: enter\n"));
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt,
15680Sstevel@tonic-gate 	    RSM_PERM_READ,
15690Sstevel@tonic-gate 	    RSM_DAS8);
15700Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
15710Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
15720Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
15750Sstevel@tonic-gate 			/* generation number snapshot */
15760Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
15770Sstevel@tonic-gate 		}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get8(im_memseg, offset, datap,
15800Sstevel@tonic-gate 		    rep_cnt, 0);
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
15830Sstevel@tonic-gate 			/* check the generation number for force disconnects */
15840Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
15850Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
15860Sstevel@tonic-gate 			}
15870Sstevel@tonic-gate 		}
15880Sstevel@tonic-gate 	}
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15910Sstevel@tonic-gate 	    "rsm_memseg_import_get8: exit\n"));
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	return (e);
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate int
15970Sstevel@tonic-gate _rsm_memseg_import_get16(rsm_memseg_import_handle_t im_memseg,
15980Sstevel@tonic-gate     off_t offset,
15990Sstevel@tonic-gate     uint16_t *datap,
16000Sstevel@tonic-gate     ulong_t rep_cnt)
16010Sstevel@tonic-gate {
16020Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
16030Sstevel@tonic-gate 	int e;
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16060Sstevel@tonic-gate 	    "rsm_memseg_import_get16: enter\n"));
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*2,
16090Sstevel@tonic-gate 	    RSM_PERM_READ,
16100Sstevel@tonic-gate 	    RSM_DAS16);
16110Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
16120Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
16130Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16160Sstevel@tonic-gate 			/* generation number snapshot */
16170Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
16180Sstevel@tonic-gate 		}
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get16(im_memseg, offset, datap,
16210Sstevel@tonic-gate 		    rep_cnt, 0);
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16240Sstevel@tonic-gate 			/* check the generation number for force disconnects */
16250Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
16260Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
16270Sstevel@tonic-gate 			}
16280Sstevel@tonic-gate 		}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16330Sstevel@tonic-gate 	    "rsm_memseg_import_get16: exit\n"));
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	return (e);
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate int
16390Sstevel@tonic-gate _rsm_memseg_import_get32(rsm_memseg_import_handle_t im_memseg,
16400Sstevel@tonic-gate     off_t offset,
16410Sstevel@tonic-gate     uint32_t *datap,
16420Sstevel@tonic-gate     ulong_t rep_cnt)
16430Sstevel@tonic-gate {
16440Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
16450Sstevel@tonic-gate 	int e;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16480Sstevel@tonic-gate 	    "rsm_memseg_import_get32: enter\n"));
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*4,
16510Sstevel@tonic-gate 	    RSM_PERM_READ,
16520Sstevel@tonic-gate 	    RSM_DAS32);
16530Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
16540Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
16550Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16580Sstevel@tonic-gate 			/* generation number snapshot */
16590Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
16600Sstevel@tonic-gate 		}
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get32(im_memseg, offset, datap,
16630Sstevel@tonic-gate 		    rep_cnt, 0);
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16660Sstevel@tonic-gate 			/* check the generation number for force disconnects */
16670Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
16680Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
16690Sstevel@tonic-gate 			}
16700Sstevel@tonic-gate 		}
16710Sstevel@tonic-gate 	}
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16740Sstevel@tonic-gate 	    "rsm_memseg_import_get32: exit\n"));
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	return (e);
16770Sstevel@tonic-gate }
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate int
16800Sstevel@tonic-gate _rsm_memseg_import_get64(rsm_memseg_import_handle_t im_memseg,
16810Sstevel@tonic-gate     off_t offset,
16820Sstevel@tonic-gate     uint64_t *datap,
16830Sstevel@tonic-gate     ulong_t rep_cnt)
16840Sstevel@tonic-gate {
16850Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
16860Sstevel@tonic-gate 	int e;
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16890Sstevel@tonic-gate 	    "rsm_memseg_import_get64: enter\n"));
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*8,
16920Sstevel@tonic-gate 	    RSM_PERM_READ,
16930Sstevel@tonic-gate 	    RSM_DAS64);
16940Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
16950Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
16960Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16990Sstevel@tonic-gate 			/* generation number snapshot */
17000Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
17010Sstevel@tonic-gate 		}
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get64(im_memseg, offset, datap,
17040Sstevel@tonic-gate 		    rep_cnt, 0);
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
17070Sstevel@tonic-gate 			/* check the generation number for force disconnects */
17080Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
17090Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
17100Sstevel@tonic-gate 			}
17110Sstevel@tonic-gate 		}
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
17150Sstevel@tonic-gate 	    "rsm_memseg_import_get64: exit\n"));
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	return (e);
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate int
17210Sstevel@tonic-gate _rsm_memseg_import_get(rsm_memseg_import_handle_t im_memseg,
17220Sstevel@tonic-gate     off_t offset,
17230Sstevel@tonic-gate     void *dst_addr,
17240Sstevel@tonic-gate     size_t length)
17250Sstevel@tonic-gate {
17260Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
17270Sstevel@tonic-gate 	int e;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
17300Sstevel@tonic-gate 	    "rsm_memseg_import_get: enter\n"));
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)dst_addr, length,
17330Sstevel@tonic-gate 	    RSM_PERM_READ,
17340Sstevel@tonic-gate 	    RSM_DAS8);
17350Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
17360Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
17370Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
17400Sstevel@tonic-gate 			/* generation number snapshot */
17410Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
17420Sstevel@tonic-gate 		}
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get(im_memseg, offset, dst_addr,
17450Sstevel@tonic-gate 		    length);
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
17480Sstevel@tonic-gate 			/* check the generation number for force disconnects */
17490Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
17500Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
17510Sstevel@tonic-gate 			}
17520Sstevel@tonic-gate 		}
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
17560Sstevel@tonic-gate 	    "rsm_memseg_import_get: exit\n"));
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	return (e);
17590Sstevel@tonic-gate }
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate int
17630Sstevel@tonic-gate _rsm_memseg_import_getv(rsm_scat_gath_t *sg_io)
17640Sstevel@tonic-gate {
17650Sstevel@tonic-gate 	rsm_controller_t *cntrl;
17660Sstevel@tonic-gate 	rsmseg_handle_t *seg;
17670Sstevel@tonic-gate 	uint_t save_sg_io_flags;
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	int e;
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
17720Sstevel@tonic-gate 	    "rsm_memseg_import_getv: enter\n"));
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	if (sg_io == NULL) {
17750Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17760Sstevel@tonic-gate 		    "invalid sg_io structure\n"));
17770Sstevel@tonic-gate 		return (RSMERR_BAD_SGIO);
17780Sstevel@tonic-gate 	}
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	seg = (rsmseg_handle_t *)sg_io->remote_handle;
17810Sstevel@tonic-gate 	if (seg == NULL) {
17820Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17830Sstevel@tonic-gate 		    "invalid remote segment handle in sg_io\n"));
17840Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
17850Sstevel@tonic-gate 	}
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	cntrl = (rsm_controller_t *)seg->rsmseg_controller;
17880Sstevel@tonic-gate 	if (cntrl == NULL) {
17890Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17900Sstevel@tonic-gate 		    "invalid controller handle\n"));
17910Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	if ((sg_io->io_request_count > RSM_MAX_SGIOREQS) ||
17950Sstevel@tonic-gate 	    (sg_io->io_request_count == 0)) {
17960Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17970Sstevel@tonic-gate 		    "io_request_count value incorrect\n"));
17980Sstevel@tonic-gate 		return (RSMERR_BAD_SGIO);
17990Sstevel@tonic-gate 	}
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_CONNECT) {
18020Sstevel@tonic-gate 		e = __rsm_import_implicit_map(seg, RSM_IOTYPE_SCATGATH);
18030Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
18040Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
18050Sstevel@tonic-gate 			    "implicit map failure\n"));
18060Sstevel@tonic-gate 			return (e);
18070Sstevel@tonic-gate 		}
18080Sstevel@tonic-gate 	}
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	/*
18110Sstevel@tonic-gate 	 * Copy the flags field of the sg_io structure in a local
18120Sstevel@tonic-gate 	 * variable.
18130Sstevel@tonic-gate 	 * This is required since the flags field can be
18140Sstevel@tonic-gate 	 * changed by the plugin library routine to indicate that
18150Sstevel@tonic-gate 	 * the signal post was done.
18160Sstevel@tonic-gate 	 * This change in the flags field of the sg_io structure
18170Sstevel@tonic-gate 	 * should not be reflected to the user. Hence once the flags
18180Sstevel@tonic-gate 	 * field has been used for the purpose of determining whether
18190Sstevel@tonic-gate 	 * the plugin executed a signal post, it must be restored to
18200Sstevel@tonic-gate 	 * its original value which is stored in the local variable.
18210Sstevel@tonic-gate 	 */
18220Sstevel@tonic-gate 	save_sg_io_flags = sg_io->flags;
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	e = cntrl->cntr_segops->rsm_memseg_import_getv(sg_io);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	/*
18270Sstevel@tonic-gate 	 * At this point, if an implicit signal post was requested by
18280Sstevel@tonic-gate 	 * the user, there could be two possibilities that arise:
18290Sstevel@tonic-gate 	 * 1. the plugin routine has already executed the implicit
18300Sstevel@tonic-gate 	 *    signal post either successfully or unsuccessfully
18310Sstevel@tonic-gate 	 * 2. the plugin does not have the capability of doing an
18320Sstevel@tonic-gate 	 *    implicit signal post and hence the signal post needs
18330Sstevel@tonic-gate 	 *    to be done here.
18340Sstevel@tonic-gate 	 * The above two cases can be idenfied by the flags
18350Sstevel@tonic-gate 	 * field within the sg_io structure as follows:
18360Sstevel@tonic-gate 	 * In case 1, the RSM_IMPLICIT_SIGPOST bit is reset to 0 by the
18370Sstevel@tonic-gate 	 * plugin, indicating that the signal post was done.
18380Sstevel@tonic-gate 	 * In case 2, the bit remains set to a 1 as originally given
18390Sstevel@tonic-gate 	 * by the user, and hence a signal post needs to be done here.
18400Sstevel@tonic-gate 	 */
18410Sstevel@tonic-gate 	if (sg_io->flags & RSM_IMPLICIT_SIGPOST &&
18420Sstevel@tonic-gate 	    e == RSM_SUCCESS) {
18430Sstevel@tonic-gate 		/* Do the implicit signal post */
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 		/*
18460Sstevel@tonic-gate 		 * The value of the second argument to this call
18470Sstevel@tonic-gate 		 * depends on the value of the sg_io->flags field.
18480Sstevel@tonic-gate 		 * If the RSM_SIGPOST_NO_ACCUMULATE flag has been
18490Sstevel@tonic-gate 		 * ored into the sg_io->flags field, this indicates
18500Sstevel@tonic-gate 		 * that the rsm_intr_signal_post is to be done with
18510Sstevel@tonic-gate 		 * the flags argument set to RSM_SIGPOST_NO_ACCUMULATE
18520Sstevel@tonic-gate 		 * Else, the flags argument is set to 0. These
18530Sstevel@tonic-gate 		 * semantics can be achieved simply by masking off
18540Sstevel@tonic-gate 		 * all other bits in the sg_io->flags field except the
18550Sstevel@tonic-gate 		 * RSM_SIGPOST_NO_ACCUMULATE bit and using the result
18560Sstevel@tonic-gate 		 * as the flags argument for the rsm_intr_signal_post.
18570Sstevel@tonic-gate 		 */
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		int sigpost_flags = sg_io->flags & RSM_SIGPOST_NO_ACCUMULATE;
18600Sstevel@tonic-gate 		e = rsm_intr_signal_post(seg, sigpost_flags);
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	/* Restore the flags field within the users scatter gather structure */
18640Sstevel@tonic-gate 	sg_io->flags = save_sg_io_flags;
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
18670Sstevel@tonic-gate 	    "rsm_memseg_import_getv: exit\n"));
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	return (e);
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate }
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 	/*
18740Sstevel@tonic-gate 	 * import side memory segment operations (write access functions):
18750Sstevel@tonic-gate 	 */
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate int
18780Sstevel@tonic-gate _rsm_memseg_import_put8(rsm_memseg_import_handle_t im_memseg,
18790Sstevel@tonic-gate     off_t offset,
18800Sstevel@tonic-gate     uint8_t *datap,
18810Sstevel@tonic-gate     ulong_t rep_cnt)
18820Sstevel@tonic-gate {
18830Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
18840Sstevel@tonic-gate 	int e;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
18870Sstevel@tonic-gate 	    "rsm_memseg_import_put8: enter\n"));
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	/* addr of data will always pass the alignment check, avoids	*/
18900Sstevel@tonic-gate 	/* need for a special case in verify_access for PUTs		*/
18910Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt,
18920Sstevel@tonic-gate 	    RSM_PERM_WRITE,
18930Sstevel@tonic-gate 	    RSM_DAS8);
18940Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
18950Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
18960Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
18990Sstevel@tonic-gate 			/* generation number snapshot */
19000Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
19010Sstevel@tonic-gate 		}
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put8(im_memseg, offset, datap,
19040Sstevel@tonic-gate 		    rep_cnt, 0);
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19070Sstevel@tonic-gate 			/* check the generation number for force disconnects */
19080Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
19090Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
19100Sstevel@tonic-gate 			}
19110Sstevel@tonic-gate 		}
19120Sstevel@tonic-gate 	}
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19150Sstevel@tonic-gate 	    "rsm_memseg_import_put8: exit\n"));
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	return (e);
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate int
19210Sstevel@tonic-gate _rsm_memseg_import_put16(rsm_memseg_import_handle_t im_memseg,
19220Sstevel@tonic-gate     off_t offset,
19230Sstevel@tonic-gate     uint16_t *datap,
19240Sstevel@tonic-gate     ulong_t rep_cnt)
19250Sstevel@tonic-gate {
19260Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
19270Sstevel@tonic-gate 	int e;
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19300Sstevel@tonic-gate 	    "rsm_memseg_import_put16: enter\n"));
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	/* addr of data will always pass the alignment check, avoids	*/
19330Sstevel@tonic-gate 	/* need for a special case in verify_access for PUTs		*/
19340Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*2,
19350Sstevel@tonic-gate 	    RSM_PERM_WRITE,
19360Sstevel@tonic-gate 	    RSM_DAS16);
19370Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
19380Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
19390Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19420Sstevel@tonic-gate 			/* generation number snapshot */
19430Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
19440Sstevel@tonic-gate 		}
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put16(im_memseg, offset, datap,
19470Sstevel@tonic-gate 		    rep_cnt, 0);
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19500Sstevel@tonic-gate 			/* check the generation number for force disconnects */
19510Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
19520Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
19530Sstevel@tonic-gate 			}
19540Sstevel@tonic-gate 		}
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	}
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19590Sstevel@tonic-gate 	    "rsm_memseg_import_put16: exit\n"));
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	return (e);
19620Sstevel@tonic-gate }
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate int
19650Sstevel@tonic-gate _rsm_memseg_import_put32(rsm_memseg_import_handle_t im_memseg,
19660Sstevel@tonic-gate     off_t offset,
19670Sstevel@tonic-gate     uint32_t *datap,
19680Sstevel@tonic-gate     ulong_t rep_cnt)
19690Sstevel@tonic-gate {
19700Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
19710Sstevel@tonic-gate 	int e;
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19740Sstevel@tonic-gate 	    "rsm_memseg_import_put32: enter\n"));
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 	/* addr of data will always pass the alignment check, avoids	*/
19770Sstevel@tonic-gate 	/* need for a special case in verify_access for PUTs		*/
19780Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*4,
19790Sstevel@tonic-gate 	    RSM_PERM_WRITE,
19800Sstevel@tonic-gate 	    RSM_DAS32);
19810Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
19820Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
19830Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19860Sstevel@tonic-gate 			/* generation number snapshot */
19870Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
19880Sstevel@tonic-gate 		}
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put32(im_memseg, offset, datap,
19910Sstevel@tonic-gate 		    rep_cnt, 0);
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19940Sstevel@tonic-gate 			/* check the generation number for force disconnects */
19950Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
19960Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
19970Sstevel@tonic-gate 			}
19980Sstevel@tonic-gate 		}
19990Sstevel@tonic-gate 	}
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
20020Sstevel@tonic-gate 	    "rsm_memseg_import_put32: exit\n"));
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	return (e);
20050Sstevel@tonic-gate }
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate int
20080Sstevel@tonic-gate _rsm_memseg_import_put64(rsm_memseg_import_handle_t im_memseg,
20090Sstevel@tonic-gate     off_t offset,
20100Sstevel@tonic-gate     uint64_t *datap,
20110Sstevel@tonic-gate     ulong_t rep_cnt)
20120Sstevel@tonic-gate {
20130Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
20140Sstevel@tonic-gate 	int		e;
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
20170Sstevel@tonic-gate 	    "rsm_memseg_import_put64: enter\n"));
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	/* addr of data will always pass the alignment check, avoids	*/
20200Sstevel@tonic-gate 	/* need for a special case in verify_access for PUTs		*/
20210Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*8,
20220Sstevel@tonic-gate 	    RSM_PERM_WRITE,
20230Sstevel@tonic-gate 	    RSM_DAS64);
20240Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
20250Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
20260Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
20290Sstevel@tonic-gate 			/* generation number snapshot */
20300Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
20310Sstevel@tonic-gate 		}
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put64(im_memseg, offset, datap,
20340Sstevel@tonic-gate 		    rep_cnt, 0);
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
20370Sstevel@tonic-gate 			/* check the generation number for force disconnects */
20380Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
20390Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
20400Sstevel@tonic-gate 			}
20410Sstevel@tonic-gate 		}
20420Sstevel@tonic-gate 	}
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
20450Sstevel@tonic-gate 	    "rsm_memseg_import_put64: exit\n"));
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	return (e);
20480Sstevel@tonic-gate }
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate int
20510Sstevel@tonic-gate _rsm_memseg_import_put(rsm_memseg_import_handle_t im_memseg,
20520Sstevel@tonic-gate     off_t offset,
20530Sstevel@tonic-gate     void *src_addr,
20540Sstevel@tonic-gate     size_t length)
20550Sstevel@tonic-gate {
20560Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
20570Sstevel@tonic-gate 	int e;
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
20600Sstevel@tonic-gate 	    "rsm_memseg_import_put: enter\n"));
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)src_addr, length,
20630Sstevel@tonic-gate 	    RSM_PERM_WRITE,
20640Sstevel@tonic-gate 	    RSM_DAS8);
20650Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
20660Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
20670Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
20700Sstevel@tonic-gate 			/* generation number snapshot */
20710Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
20720Sstevel@tonic-gate 		}
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put(im_memseg, offset, src_addr,
20750Sstevel@tonic-gate 		    length);
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
20780Sstevel@tonic-gate 			/* check the generation number for force disconnects */
20790Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
20800Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
20810Sstevel@tonic-gate 			}
20820Sstevel@tonic-gate 		}
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
20870Sstevel@tonic-gate 	    "rsm_memseg_import_put: exit\n"));
20880Sstevel@tonic-gate 	return (e);
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate int
20930Sstevel@tonic-gate _rsm_memseg_import_putv(rsm_scat_gath_t *sg_io)
20940Sstevel@tonic-gate {
20950Sstevel@tonic-gate 	rsm_controller_t *cntrl;
20960Sstevel@tonic-gate 	rsmseg_handle_t *seg;
20970Sstevel@tonic-gate 	uint_t save_sg_io_flags;
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	int e;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
21020Sstevel@tonic-gate 	    "rsm_memseg_import_putv: enter\n"));
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	if (sg_io == NULL) {
21060Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
21070Sstevel@tonic-gate 		    "invalid sg_io structure\n"));
21080Sstevel@tonic-gate 		return (RSMERR_BAD_SGIO);
21090Sstevel@tonic-gate 	}
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	seg = (rsmseg_handle_t *)sg_io->remote_handle;
21120Sstevel@tonic-gate 	if (seg == NULL) {
21130Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
21140Sstevel@tonic-gate 		    "invalid remote segment handle in sg_io\n"));
21150Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
21160Sstevel@tonic-gate 	}
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	cntrl = (rsm_controller_t *)seg->rsmseg_controller;
21190Sstevel@tonic-gate 	if (cntrl == NULL) {
21200Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
21210Sstevel@tonic-gate 		    "invalid controller handle\n"));
21220Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	if ((sg_io->io_request_count > RSM_MAX_SGIOREQS) ||
21260Sstevel@tonic-gate 	    (sg_io->io_request_count == 0)) {
21270Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
21280Sstevel@tonic-gate 		    "io_request_count value incorrect\n"));
21290Sstevel@tonic-gate 		return (RSMERR_BAD_SGIO);
21300Sstevel@tonic-gate 	}
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	/* do an implicit map if required */
21330Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_CONNECT) {
21340Sstevel@tonic-gate 		e = __rsm_import_implicit_map(seg, RSM_IOTYPE_SCATGATH);
21350Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
21360Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
21370Sstevel@tonic-gate 			    "implicit map failed\n"));
21380Sstevel@tonic-gate 			return (e);
21390Sstevel@tonic-gate 		}
21400Sstevel@tonic-gate 	}
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	/*
21430Sstevel@tonic-gate 	 * Copy the flags field of the sg_io structure in a local
21440Sstevel@tonic-gate 	 * variable.
21450Sstevel@tonic-gate 	 * This is required since the flags field can be
21460Sstevel@tonic-gate 	 * changed by the plugin library routine to indicate that
21470Sstevel@tonic-gate 	 * the signal post was done.
21480Sstevel@tonic-gate 	 * This change in the flags field of the sg_io structure
21490Sstevel@tonic-gate 	 * should not be reflected to the user. Hence once the flags
21500Sstevel@tonic-gate 	 * field has been used for the purpose of determining whether
21510Sstevel@tonic-gate 	 * the plugin executed a signal post, it must be restored to
21520Sstevel@tonic-gate 	 * its original value which is stored in the local variable.
21530Sstevel@tonic-gate 	 */
21540Sstevel@tonic-gate 	save_sg_io_flags = sg_io->flags;
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	e = cntrl->cntr_segops->rsm_memseg_import_putv(sg_io);
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	/*
21590Sstevel@tonic-gate 	 * At this point, if an implicit signal post was requested by
21600Sstevel@tonic-gate 	 * the user, there could be two possibilities that arise:
21610Sstevel@tonic-gate 	 * 1. the plugin routine has already executed the implicit
21620Sstevel@tonic-gate 	 *    signal post either successfully or unsuccessfully
21630Sstevel@tonic-gate 	 * 2. the plugin does not have the capability of doing an
21640Sstevel@tonic-gate 	 *    implicit signal post and hence the signal post needs
21650Sstevel@tonic-gate 	 *    to be done here.
21660Sstevel@tonic-gate 	 * The above two cases can be idenfied by the flags
21670Sstevel@tonic-gate 	 * field within the sg_io structure as follows:
21680Sstevel@tonic-gate 	 * In case 1, the RSM_IMPLICIT_SIGPOST bit is reset to 0 by the
21690Sstevel@tonic-gate 	 * plugin, indicating that the signal post was done.
21700Sstevel@tonic-gate 	 * In case 2, the bit remains set to a 1 as originally given
21710Sstevel@tonic-gate 	 * by the user, and hence a signal post needs to be done here.
21720Sstevel@tonic-gate 	 */
21730Sstevel@tonic-gate 	if (sg_io->flags & RSM_IMPLICIT_SIGPOST &&
21740Sstevel@tonic-gate 		e == RSM_SUCCESS) {
21750Sstevel@tonic-gate 		/* Do the implicit signal post */
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 		/*
21780Sstevel@tonic-gate 		 * The value of the second argument to this call
21790Sstevel@tonic-gate 		 * depends on the value of the sg_io->flags field.
21800Sstevel@tonic-gate 		 * If the RSM_SIGPOST_NO_ACCUMULATE flag has been
21810Sstevel@tonic-gate 		 * ored into the sg_io->flags field, this indicates
21820Sstevel@tonic-gate 		 * that the rsm_intr_signal_post is to be done with
21830Sstevel@tonic-gate 		 * the flags argument set to RSM_SIGPOST_NO_ACCUMULATE
21840Sstevel@tonic-gate 		 * Else, the flags argument is set to 0. These
21850Sstevel@tonic-gate 		 * semantics can be achieved simply by masking off
21860Sstevel@tonic-gate 		 * all other bits in the sg_io->flags field except the
21870Sstevel@tonic-gate 		 * RSM_SIGPOST_NO_ACCUMULATE bit and using the result
21880Sstevel@tonic-gate 		 * as the flags argument for the rsm_intr_signal_post.
21890Sstevel@tonic-gate 		 */
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 		int sigpost_flags = sg_io->flags & RSM_SIGPOST_NO_ACCUMULATE;
21920Sstevel@tonic-gate 		e = rsm_intr_signal_post(seg, sigpost_flags);
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 	}
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	/* Restore the flags field within the users scatter gather structure */
21970Sstevel@tonic-gate 	sg_io->flags = save_sg_io_flags;
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
22000Sstevel@tonic-gate 	    "rsm_memseg_import_putv: exit\n"));
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	return (e);
22030Sstevel@tonic-gate }
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate 	/*
22070Sstevel@tonic-gate 	 * import side memory segment operations (mapping):
22080Sstevel@tonic-gate 	 */
22090Sstevel@tonic-gate int
22100Sstevel@tonic-gate _rsm_memseg_import_map(rsm_memseg_import_handle_t im_memseg,
22110Sstevel@tonic-gate     void **address,
22120Sstevel@tonic-gate     rsm_attribute_t attr,
22130Sstevel@tonic-gate     rsm_permission_t perm,
22140Sstevel@tonic-gate     off_t offset,
22150Sstevel@tonic-gate     size_t length)
22160Sstevel@tonic-gate {
22170Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
22180Sstevel@tonic-gate 	int flag = MAP_SHARED;
22190Sstevel@tonic-gate 	int prot;
22200Sstevel@tonic-gate 	caddr_t va;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
22230Sstevel@tonic-gate 	    "rsm_memseg_import_map: enter\n"));
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	if (!seg) {
22260Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22270Sstevel@tonic-gate 		    "invalid segment\n"));
22280Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
22290Sstevel@tonic-gate 	}
22300Sstevel@tonic-gate 	if (!address) {
22310Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22320Sstevel@tonic-gate 		    "invalid address\n"));
22330Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
22340Sstevel@tonic-gate 	}
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	/*
22370Sstevel@tonic-gate 	 * Only one map per segment handle!
22380Sstevel@tonic-gate 	 * XXX need to take a lock here
22390Sstevel@tonic-gate 	 */
22400Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_MAP) {
22430Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22440Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22450Sstevel@tonic-gate 		    "segment already mapped\n"));
22460Sstevel@tonic-gate 		return (RSMERR_SEG_ALREADY_MAPPED);
22470Sstevel@tonic-gate 	}
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 	/* Only import segments allowed to map */
22500Sstevel@tonic-gate 	if (seg->rsmseg_state != IMPORT_CONNECT) {
22510Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22520Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
22530Sstevel@tonic-gate 	}
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	/* check for permissions */
22560Sstevel@tonic-gate 	if (perm > RSM_PERM_RDWR) {
22570Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22580Sstevel@tonic-gate 		    "bad permissions when mapping\n"));
22590Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22600Sstevel@tonic-gate 		return (RSMERR_BAD_PERMS);
22610Sstevel@tonic-gate 	}
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	if (length == 0) {
22640Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22650Sstevel@tonic-gate 		    "mapping with length 0\n"));
22660Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22670Sstevel@tonic-gate 		return (RSMERR_BAD_LENGTH);
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	if (offset + length > seg->rsmseg_size) {
22710Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22720Sstevel@tonic-gate 		    "map length + offset exceed segment size\n"));
22730Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22740Sstevel@tonic-gate 		return (RSMERR_BAD_LENGTH);
22750Sstevel@tonic-gate 	}
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	if ((size_t)offset & (PAGESIZE - 1)) {
22780Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
22790Sstevel@tonic-gate 		    "bad mem alignment\n"));
22800Sstevel@tonic-gate 		return (RSMERR_BAD_MEM_ALIGNMENT);
22810Sstevel@tonic-gate 	}
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	if (attr & RSM_MAP_FIXED) {
22840Sstevel@tonic-gate 		if ((uintptr_t)(*address) & (PAGESIZE - 1)) {
22850Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
22860Sstevel@tonic-gate 			    "bad mem alignment\n"));
22870Sstevel@tonic-gate 			return (RSMERR_BAD_MEM_ALIGNMENT);
22880Sstevel@tonic-gate 		}
22890Sstevel@tonic-gate 		flag |= MAP_FIXED;
22900Sstevel@tonic-gate 	}
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 	prot = PROT_NONE;
22930Sstevel@tonic-gate 	if (perm & RSM_PERM_READ)
22940Sstevel@tonic-gate 		prot |= PROT_READ;
22950Sstevel@tonic-gate 	if (perm & RSM_PERM_WRITE)
22960Sstevel@tonic-gate 		prot |= PROT_WRITE;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	va = mmap(*address, length, prot, flag, seg->rsmseg_fd, offset);
22990Sstevel@tonic-gate 	if (va == MAP_FAILED) {
23000Sstevel@tonic-gate 		int e = errno;
23010Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
23020Sstevel@tonic-gate 		    "error %d during map\n", e));
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
23050Sstevel@tonic-gate 		if (e == ENXIO || e == EOVERFLOW ||
23060Sstevel@tonic-gate 		    e == ENOMEM)
23070Sstevel@tonic-gate 			return (RSMERR_BAD_LENGTH);
23080Sstevel@tonic-gate 		else if (e == ENODEV)
23090Sstevel@tonic-gate 			return (RSMERR_CONN_ABORTED);
23100Sstevel@tonic-gate 		else if (e == EAGAIN)
23110Sstevel@tonic-gate 			return (RSMERR_INSUFFICIENT_RESOURCES);
23120Sstevel@tonic-gate 		else if (e == ENOTSUP)
23130Sstevel@tonic-gate 			return (RSMERR_MAP_FAILED);
23140Sstevel@tonic-gate 		else if (e == EACCES)
23150Sstevel@tonic-gate 			return (RSMERR_BAD_PERMS);
23160Sstevel@tonic-gate 		else
23170Sstevel@tonic-gate 			return (RSMERR_MAP_FAILED);
23180Sstevel@tonic-gate 	}
23190Sstevel@tonic-gate 	*address = va;
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	/*
23220Sstevel@tonic-gate 	 * Fix segment ops vector to handle direct access.
23230Sstevel@tonic-gate 	 */
23240Sstevel@tonic-gate 	/*
23250Sstevel@tonic-gate 	 * XXX: Set this only for full segment mapping. Keep a list
23260Sstevel@tonic-gate 	 * of mappings to use for access functions
23270Sstevel@tonic-gate 	 */
23280Sstevel@tonic-gate 	seg->rsmseg_vaddr = va;
23290Sstevel@tonic-gate 	seg->rsmseg_maplen = length;
23300Sstevel@tonic-gate 	seg->rsmseg_mapoffset = offset;
23310Sstevel@tonic-gate 	seg->rsmseg_state = IMPORT_MAP;
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23360Sstevel@tonic-gate 	    "rsm_memseg_import_map: exit\n"));
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 	return (RSM_SUCCESS);
23390Sstevel@tonic-gate }
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate int
23420Sstevel@tonic-gate _rsm_memseg_import_unmap(rsm_memseg_import_handle_t im_memseg)
23430Sstevel@tonic-gate {
23440Sstevel@tonic-gate 	/*
23450Sstevel@tonic-gate 	 * Until we fix the rsm driver to catch unload, we unload
23460Sstevel@tonic-gate 	 * the whole segment.
23470Sstevel@tonic-gate 	 */
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23520Sstevel@tonic-gate 	    "rsm_memseg_import_unmap: enter\n"));
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 	if (!seg) {
23550Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
23560Sstevel@tonic-gate 		    "invalid segment or segment state\n"));
23570Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
23580Sstevel@tonic-gate 	}
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
23610Sstevel@tonic-gate 	if (seg->rsmseg_state != IMPORT_MAP) {
23620Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
23630Sstevel@tonic-gate 		return (RSMERR_SEG_NOT_MAPPED);
23640Sstevel@tonic-gate 	}
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 	seg->rsmseg_mapoffset = 0;   /* reset the offset */
23670Sstevel@tonic-gate 	seg->rsmseg_state = IMPORT_CONNECT;
23680Sstevel@tonic-gate 	seg->rsmseg_flags &= ~RSM_IMPLICIT_MAP;
23690Sstevel@tonic-gate 	(void) munmap(seg->rsmseg_vaddr, seg->rsmseg_maplen);
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23740Sstevel@tonic-gate 	    "rsm_memseg_import_unmap: exit\n"));
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate 	return (RSM_SUCCESS);
23770Sstevel@tonic-gate }
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	/*
23810Sstevel@tonic-gate 	 * import side memory segment operations (barriers):
23820Sstevel@tonic-gate 	 */
23830Sstevel@tonic-gate int
23840Sstevel@tonic-gate _rsm_memseg_import_init_barrier(rsm_memseg_import_handle_t im_memseg,
23850Sstevel@tonic-gate     rsm_barrier_type_t type,
23860Sstevel@tonic-gate     rsmapi_barrier_t *barrier)
23870Sstevel@tonic-gate {
23880Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
23890Sstevel@tonic-gate 	rsmbar_handle_t *bar;
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23920Sstevel@tonic-gate 	    "rsm_memseg_import_init_barrier: enter\n"));
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	if (!seg) {
23950Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
23960Sstevel@tonic-gate 		    "invalid segment or barrier\n"));
23970Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
23980Sstevel@tonic-gate 	}
23990Sstevel@tonic-gate 	if (!barrier) {
24000Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24010Sstevel@tonic-gate 		    "invalid barrier pointer\n"));
24020Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
24030Sstevel@tonic-gate 	}
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	bar = (rsmbar_handle_t *)barrier;
24060Sstevel@tonic-gate 	bar->rsmbar_seg = seg;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	seg->rsmseg_barrier = barrier;  /* used in put/get fns */
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24110Sstevel@tonic-gate 	    "rsm_memseg_import_init_barrier: exit\n"));
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 	return (seg->rsmseg_ops->rsm_memseg_import_init_barrier(im_memseg,
24140Sstevel@tonic-gate 	    type, (rsm_barrier_handle_t)barrier));
24150Sstevel@tonic-gate }
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate int
24180Sstevel@tonic-gate _rsm_memseg_import_open_barrier(rsmapi_barrier_t *barrier)
24190Sstevel@tonic-gate {
24200Sstevel@tonic-gate 	rsmbar_handle_t *bar = (rsmbar_handle_t *)barrier;
24210Sstevel@tonic-gate 	rsm_segops_t *ops;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24240Sstevel@tonic-gate 	    "rsm_memseg_import_open_barrier: enter\n"));
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 	if (!bar) {
24270Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24280Sstevel@tonic-gate 		    "invalid barrier\n"));
24290Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
24300Sstevel@tonic-gate 	}
24310Sstevel@tonic-gate 	if (!bar->rsmbar_seg) {
24320Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24330Sstevel@tonic-gate 		    "uninitialized barrier\n"));
24340Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
24350Sstevel@tonic-gate 	}
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	/* generation number snapshot */
24380Sstevel@tonic-gate 	bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum; /* bar[0] */
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	ops = bar->rsmbar_seg->rsmseg_ops;
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24430Sstevel@tonic-gate 	    "rsm_memseg_import_open_barrier: exit\n"));
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	return (ops->rsm_memseg_import_open_barrier(
24460Sstevel@tonic-gate 	    (rsm_barrier_handle_t)barrier));
24470Sstevel@tonic-gate }
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate int
24500Sstevel@tonic-gate _rsm_memseg_import_order_barrier(rsmapi_barrier_t *barrier)
24510Sstevel@tonic-gate {
24520Sstevel@tonic-gate 	rsmbar_handle_t *bar = (rsmbar_handle_t *)barrier;
24530Sstevel@tonic-gate 	rsm_segops_t *ops;
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24560Sstevel@tonic-gate 	    "rsm_memseg_import_order_barrier: enter\n"));
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 	if (!bar) {
24590Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24600Sstevel@tonic-gate 		    "invalid barrier\n"));
24610Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
24620Sstevel@tonic-gate 	}
24630Sstevel@tonic-gate 	if (!bar->rsmbar_seg) {
24640Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24650Sstevel@tonic-gate 		    "uninitialized barrier\n"));
24660Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
24670Sstevel@tonic-gate 	}
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	ops = bar->rsmbar_seg->rsmseg_ops;
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24720Sstevel@tonic-gate 	    "rsm_memseg_import_order_barrier: exit\n"));
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 	return (ops->rsm_memseg_import_order_barrier(
24750Sstevel@tonic-gate 	    (rsm_barrier_handle_t)barrier));
24760Sstevel@tonic-gate }
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate int
24790Sstevel@tonic-gate _rsm_memseg_import_close_barrier(rsmapi_barrier_t *barrier)
24800Sstevel@tonic-gate {
24810Sstevel@tonic-gate 	rsmbar_handle_t *bar = (rsmbar_handle_t *)barrier;
24820Sstevel@tonic-gate 	rsm_segops_t *ops;
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24850Sstevel@tonic-gate 	    "rsm_memseg_import_close_barrier: enter\n"));
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	if (!bar) {
24880Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24890Sstevel@tonic-gate 		    "invalid barrier\n"));
24900Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
24910Sstevel@tonic-gate 	}
24920Sstevel@tonic-gate 	if (!bar->rsmbar_seg) {
24930Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24940Sstevel@tonic-gate 		    "uninitialized barrier\n"));
24950Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
24960Sstevel@tonic-gate 	}
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	/* generation number snapshot */
24990Sstevel@tonic-gate 	if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
25000Sstevel@tonic-gate 		return (RSMERR_CONN_ABORTED);
25010Sstevel@tonic-gate 	}
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	ops = bar->rsmbar_seg->rsmseg_ops;
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25060Sstevel@tonic-gate 	    "rsm_memseg_import_close_barrier: exit\n"));
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 	return (ops->rsm_memseg_import_close_barrier(
25090Sstevel@tonic-gate 	    (rsm_barrier_handle_t)barrier));
25100Sstevel@tonic-gate }
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate int
25130Sstevel@tonic-gate _rsm_memseg_import_destroy_barrier(rsmapi_barrier_t *barrier)
25140Sstevel@tonic-gate {
25150Sstevel@tonic-gate 	rsmbar_handle_t *bar = (rsmbar_handle_t *)barrier;
25160Sstevel@tonic-gate 	rsm_segops_t *ops;
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25190Sstevel@tonic-gate 	    "rsm_memseg_import_destroy_barrier: enter\n"));
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	if (!bar) {
25220Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
25230Sstevel@tonic-gate 		    "invalid barrier\n"));
25240Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
25250Sstevel@tonic-gate 	}
25260Sstevel@tonic-gate 	if (!bar->rsmbar_seg) {
25270Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
25280Sstevel@tonic-gate 		    "uninitialized barrier\n"));
25290Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	bar->rsmbar_seg->rsmseg_barrier = NULL;
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 	ops = bar->rsmbar_seg->rsmseg_ops;
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25370Sstevel@tonic-gate 	    "rsm_memseg_import_destroy_barrier: exit\n"));
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	return (ops->rsm_memseg_import_destroy_barrier
25400Sstevel@tonic-gate 	    ((rsm_barrier_handle_t)barrier));
25410Sstevel@tonic-gate }
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate int
25440Sstevel@tonic-gate _rsm_memseg_import_get_mode(rsm_memseg_import_handle_t im_memseg,
25450Sstevel@tonic-gate     rsm_barrier_mode_t *mode)
25460Sstevel@tonic-gate {
25470Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25500Sstevel@tonic-gate 	    "rsm_memseg_import_get_mode: enter\n"));
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	if (seg) {
25530Sstevel@tonic-gate 		*mode = seg->rsmseg_barmode;
25540Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25550Sstevel@tonic-gate 		    "rsm_memseg_import_get_mode: exit\n"));
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 		return (seg->rsmseg_ops->rsm_memseg_import_get_mode(im_memseg,
25580Sstevel@tonic-gate 		    mode));
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
25620Sstevel@tonic-gate 	    "invalid arguments \n"));
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	return (RSMERR_BAD_SEG_HNDL);
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate }
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate int
25690Sstevel@tonic-gate _rsm_memseg_import_set_mode(rsm_memseg_import_handle_t im_memseg,
25700Sstevel@tonic-gate     rsm_barrier_mode_t mode)
25710Sstevel@tonic-gate {
25720Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25750Sstevel@tonic-gate 	    "rsm_memseg_import_set_mode: enter\n"));
25760Sstevel@tonic-gate 	if (seg) {
25770Sstevel@tonic-gate 		if ((mode == RSM_BARRIER_MODE_IMPLICIT ||
25780Sstevel@tonic-gate 		    mode == RSM_BARRIER_MODE_EXPLICIT)) {
25790Sstevel@tonic-gate 			seg->rsmseg_barmode = mode;
25800Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25810Sstevel@tonic-gate 			    "rsm_memseg_import_set_mode: exit\n"));
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 			return (seg->rsmseg_ops->rsm_memseg_import_set_mode(
25840Sstevel@tonic-gate 			    im_memseg,
25850Sstevel@tonic-gate 			    mode));
25860Sstevel@tonic-gate 		} else {
25870Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25880Sstevel@tonic-gate 			    "bad barrier mode\n"));
25890Sstevel@tonic-gate 			return (RSMERR_BAD_MODE);
25900Sstevel@tonic-gate 		}
25910Sstevel@tonic-gate 	}
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
25940Sstevel@tonic-gate 	    "invalid arguments\n"));
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	return (RSMERR_BAD_SEG_HNDL);
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate int
26000Sstevel@tonic-gate _rsm_intr_signal_post(void *memseg, uint_t flags)
26010Sstevel@tonic-gate {
26020Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
26030Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
26060Sstevel@tonic-gate 	    "rsm_intr_signal_post: enter\n"));
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 	flags = flags;
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	if (!seg) {
26110Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
26120Sstevel@tonic-gate 		    "invalid segment handle\n"));
26130Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
26140Sstevel@tonic-gate 	}
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_RING_BELL, &msg) < 0) {
26170Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
26180Sstevel@tonic-gate 		    "RSM_IOCTL_RING_BELL failed\n"));
26190Sstevel@tonic-gate 		return (errno);
26200Sstevel@tonic-gate 	}
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
26230Sstevel@tonic-gate 	    "rsm_intr_signal_post: exit\n"));
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 	return (RSM_SUCCESS);
26260Sstevel@tonic-gate }
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate int
26290Sstevel@tonic-gate _rsm_intr_signal_wait(void *memseg, int timeout)
26300Sstevel@tonic-gate {
26310Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
26320Sstevel@tonic-gate 	struct pollfd fds;
26330Sstevel@tonic-gate 	minor_t	rnum;
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
26360Sstevel@tonic-gate 	    "rsm_intr_signal_wait: enter\n"));
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	if (!seg) {
26390Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
26400Sstevel@tonic-gate 		    "invalid segment\n"));
26410Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
26420Sstevel@tonic-gate 	}
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	fds.fd = seg->rsmseg_fd;
26450Sstevel@tonic-gate 	fds.events = POLLRDNORM;
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	rnum = seg->rsmseg_rnum;
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	return (__rsm_intr_signal_wait_common(&fds, &rnum, 1, timeout, NULL));
26500Sstevel@tonic-gate }
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate int
26530Sstevel@tonic-gate _rsm_intr_signal_wait_pollfd(struct pollfd fds[], nfds_t nfds, int timeout,
26540Sstevel@tonic-gate 	int *numfdsp)
26550Sstevel@tonic-gate {
26560Sstevel@tonic-gate 	return (__rsm_intr_signal_wait_common(fds, NULL, nfds, timeout,
26570Sstevel@tonic-gate 	    numfdsp));
26580Sstevel@tonic-gate }
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate /*
26610Sstevel@tonic-gate  * This is the generic wait routine, it takes the following arguments
26620Sstevel@tonic-gate  *	- pollfd array
26630Sstevel@tonic-gate  *	- rnums array corresponding to the pollfd if known, if this is
26640Sstevel@tonic-gate  *	NULL then the fds are looked up from the pollfd_table.
26650Sstevel@tonic-gate  *	- number of fds in pollfd array,
26660Sstevel@tonic-gate  *	- timeout
26670Sstevel@tonic-gate  *	- pointer to a location where the number of fds with successful
26680Sstevel@tonic-gate  *	events is returned.
26690Sstevel@tonic-gate  */
26700Sstevel@tonic-gate static int
26710Sstevel@tonic-gate __rsm_intr_signal_wait_common(struct pollfd fds[], minor_t rnums[],
26720Sstevel@tonic-gate     nfds_t nfds, int timeout, int *numfdsp)
26730Sstevel@tonic-gate {
26740Sstevel@tonic-gate 	int	i;
26750Sstevel@tonic-gate 	int	numsegs = 0;
26760Sstevel@tonic-gate 	int	numfd;
26770Sstevel@tonic-gate 	int	fds_processed = 0;
26780Sstevel@tonic-gate 	minor_t	segrnum;
26790Sstevel@tonic-gate 	rsm_poll_event_t	event_arr[RSM_MAX_POLLFDS];
26800Sstevel@tonic-gate 	rsm_poll_event_t	*event_list = NULL;
26810Sstevel@tonic-gate 	rsm_poll_event_t	*events;
26820Sstevel@tonic-gate 	rsm_consume_event_msg_t msg;
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE, "wait_common enter\n"));
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate 	if (numfdsp) {
26870Sstevel@tonic-gate 		*numfdsp = 0;
26880Sstevel@tonic-gate 	}
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 	numfd = poll(fds, nfds, timeout);
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	switch (numfd) {
26930Sstevel@tonic-gate 	case -1: /* poll returned error - map to RSMERR_... */
26940Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR, "signal wait pollfd err\n"));
26950Sstevel@tonic-gate 		switch (errno) {
26960Sstevel@tonic-gate 		case EAGAIN:
26970Sstevel@tonic-gate 			return (RSMERR_INSUFFICIENT_RESOURCES);
26980Sstevel@tonic-gate 		case EFAULT:
26990Sstevel@tonic-gate 			return (RSMERR_BAD_ADDR);
27000Sstevel@tonic-gate 		case EINTR:
27010Sstevel@tonic-gate 			return (RSMERR_INTERRUPTED);
27020Sstevel@tonic-gate 		case EINVAL:
27030Sstevel@tonic-gate 		default:
27040Sstevel@tonic-gate 			return (RSMERR_BAD_ARGS_ERRORS);
27050Sstevel@tonic-gate 		}
27060Sstevel@tonic-gate 	case 0: /* timedout - return from here */
27070Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
27080Sstevel@tonic-gate 		    "signal wait timed out\n"));
27090Sstevel@tonic-gate 		return (RSMERR_TIMEOUT);
27100Sstevel@tonic-gate 	default:
27110Sstevel@tonic-gate 		break;
27120Sstevel@tonic-gate 	}
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 	if (numfd <= RSM_MAX_POLLFDS) {
27150Sstevel@tonic-gate 		/* use the event array on the stack */
27160Sstevel@tonic-gate 		events = (rsm_poll_event_t *)event_arr;
27170Sstevel@tonic-gate 	} else {
27180Sstevel@tonic-gate 		/*
27190Sstevel@tonic-gate 		 * actual number of fds corresponding to rsmapi segments might
27200Sstevel@tonic-gate 		 * be < numfd, don't want to scan the list to figure that out
27210Sstevel@tonic-gate 		 * lets just allocate on the heap
27220Sstevel@tonic-gate 		 */
27230Sstevel@tonic-gate 		event_list = (rsm_poll_event_t *)malloc(
27240Sstevel@tonic-gate 			sizeof (rsm_poll_event_t)*numfd);
27250Sstevel@tonic-gate 		if (!event_list) {
27260Sstevel@tonic-gate 			/*
27270Sstevel@tonic-gate 			 * return with error even if poll might have succeeded
27280Sstevel@tonic-gate 			 * since the application can retry and the events will
27290Sstevel@tonic-gate 			 * still be available.
27300Sstevel@tonic-gate 			 */
27310Sstevel@tonic-gate 			return (RSMERR_INSUFFICIENT_MEM);
27320Sstevel@tonic-gate 		}
27330Sstevel@tonic-gate 		events = event_list;
27340Sstevel@tonic-gate 	}
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate 	/*
27370Sstevel@tonic-gate 	 * process the fds for events and if it corresponds to an rsmapi
27380Sstevel@tonic-gate 	 * segment consume the event
27390Sstevel@tonic-gate 	 */
27400Sstevel@tonic-gate 	for (i = 0; i < nfds; i++) {
27410Sstevel@tonic-gate 		if (fds[i].revents == POLLRDNORM) {
27420Sstevel@tonic-gate 			/*
27430Sstevel@tonic-gate 			 * poll returned an event and if its POLLRDNORM, it
27440Sstevel@tonic-gate 			 * might correspond to an rsmapi segment
27450Sstevel@tonic-gate 			 */
27460Sstevel@tonic-gate 			if (rnums) { /* resource num is passed in */
27470Sstevel@tonic-gate 				segrnum = rnums[i];
27480Sstevel@tonic-gate 			} else { /* lookup pollfd table to get resource num */
27490Sstevel@tonic-gate 				segrnum = _rsm_lookup_pollfd_table(fds[i].fd);
27500Sstevel@tonic-gate 			}
27510Sstevel@tonic-gate 			if (segrnum) {
27520Sstevel@tonic-gate 				events[numsegs].rnum = segrnum;
27530Sstevel@tonic-gate 				events[numsegs].revent = 0;
27540Sstevel@tonic-gate 				events[numsegs].fdsidx = i; /* fdlist index */
27550Sstevel@tonic-gate 				numsegs++;
27560Sstevel@tonic-gate 			}
27570Sstevel@tonic-gate 		}
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 		if ((fds[i].revents) && (++fds_processed == numfd)) {
27600Sstevel@tonic-gate 			/*
27610Sstevel@tonic-gate 			 * only "numfd" events have revents field set, once we
27620Sstevel@tonic-gate 			 * process that many break out of the loop
27630Sstevel@tonic-gate 			 */
27640Sstevel@tonic-gate 			break;
27650Sstevel@tonic-gate 		}
27660Sstevel@tonic-gate 	}
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 	if (numsegs == 0) { /* No events for rsmapi segs in the fdlist */
27690Sstevel@tonic-gate 		if (event_list) {
27700Sstevel@tonic-gate 			free(event_list);
27710Sstevel@tonic-gate 		}
27720Sstevel@tonic-gate 		if (numfdsp) {
27730Sstevel@tonic-gate 			*numfdsp = numfd;
27740Sstevel@tonic-gate 		}
27750Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
27760Sstevel@tonic-gate 		    "wait_common exit: no rsmapi segs\n"));
27770Sstevel@tonic-gate 		return (RSM_SUCCESS);
27780Sstevel@tonic-gate 	}
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 	msg.seglist = (caddr_t)events;
27810Sstevel@tonic-gate 	msg.numents = numsegs;
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_CONSUMEEVENT, &msg) < 0) {
27840Sstevel@tonic-gate 		int error = errno;
27850Sstevel@tonic-gate 		if (event_list) {
27860Sstevel@tonic-gate 			free(event_list);
27870Sstevel@tonic-gate 		}
27880Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_ERR,
27890Sstevel@tonic-gate 		    "RSM_IOCTL_CONSUMEEVENT failed(%d)\n", error));
27900Sstevel@tonic-gate 		return (error);
27910Sstevel@tonic-gate 	}
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 	/* count the number of segs for which consumeevent was successful */
27940Sstevel@tonic-gate 	numfd -= numsegs;
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 	for (i = 0; i < numsegs; i++) {
27970Sstevel@tonic-gate 		if (events[i].revent != 0) {
27980Sstevel@tonic-gate 			fds[events[i].fdsidx].revents = POLLRDNORM;
27990Sstevel@tonic-gate 			numfd++;
28000Sstevel@tonic-gate 		} else { /* failed to consume event so set revents to 0 */
28010Sstevel@tonic-gate 			fds[events[i].fdsidx].revents = 0;
28020Sstevel@tonic-gate 		}
28030Sstevel@tonic-gate 	}
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 	if (event_list) {
28060Sstevel@tonic-gate 		free(event_list);
28070Sstevel@tonic-gate 	}
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 	if (numfd > 0) {
28100Sstevel@tonic-gate 		if (numfdsp) {
28110Sstevel@tonic-gate 			*numfdsp = numfd;
28120Sstevel@tonic-gate 		}
28130Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28140Sstevel@tonic-gate 		    "wait_common exit\n"));
28150Sstevel@tonic-gate 		return (RSM_SUCCESS);
28160Sstevel@tonic-gate 	} else {
28170Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28180Sstevel@tonic-gate 		    "wait_common exit\n"));
28190Sstevel@tonic-gate 		return (RSMERR_TIMEOUT);
28200Sstevel@tonic-gate 	}
28210Sstevel@tonic-gate }
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate /*
28240Sstevel@tonic-gate  * This function provides the data (file descriptor and event) for
28250Sstevel@tonic-gate  * the specified pollfd struct.  The pollfd struct may then be
28260Sstevel@tonic-gate  * subsequently used with the poll system call to wait for an event
28270Sstevel@tonic-gate  * signalled by rsm_intr_signal_post.  The memory segment must be
28280Sstevel@tonic-gate  * currently published for a successful return with a valid pollfd.
28290Sstevel@tonic-gate  * A reference count for the descriptor is incremented.
28300Sstevel@tonic-gate  */
28310Sstevel@tonic-gate int
28320Sstevel@tonic-gate _rsm_memseg_get_pollfd(void *memseg,
28330Sstevel@tonic-gate 			struct pollfd *poll_fd)
28340Sstevel@tonic-gate {
28350Sstevel@tonic-gate 	int	i;
28360Sstevel@tonic-gate 	int	err = RSM_SUCCESS;
28370Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28400Sstevel@tonic-gate 	    "rsm_memseg_get_pollfd: enter\n"));
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	if (!seg) {
28430Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
28440Sstevel@tonic-gate 		    "invalid segment\n"));
28450Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
28460Sstevel@tonic-gate 	}
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	poll_fd->fd = seg->rsmseg_fd;
28510Sstevel@tonic-gate 	poll_fd->events = POLLRDNORM;
28520Sstevel@tonic-gate 	seg->rsmseg_pollfd_refcnt++;
28530Sstevel@tonic-gate 	if (seg->rsmseg_pollfd_refcnt == 1) {
28540Sstevel@tonic-gate 		/* insert the segment into the pollfd table */
28550Sstevel@tonic-gate 		err = _rsm_insert_pollfd_table(seg->rsmseg_fd,
28560Sstevel@tonic-gate 		    seg->rsmseg_rnum);
28570Sstevel@tonic-gate 	}
28580Sstevel@tonic-gate 
28590Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28620Sstevel@tonic-gate 	    "rsm_memseg_get_pollfd: exit(%d)\n", err));
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	return (err);
28650Sstevel@tonic-gate }
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate /*
28680Sstevel@tonic-gate  * This function decrements the segment pollfd reference count.
28690Sstevel@tonic-gate  * A segment unpublish or destroy operation will fail if the reference count is
28700Sstevel@tonic-gate  * non zero.
28710Sstevel@tonic-gate  */
28720Sstevel@tonic-gate int
28730Sstevel@tonic-gate _rsm_memseg_release_pollfd(void * memseg)
28740Sstevel@tonic-gate {
28750Sstevel@tonic-gate 	int	i;
28760Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28790Sstevel@tonic-gate 	    "rsm_memseg_release_pollfd: enter\n"));
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 	if (!seg) {
28820Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
28830Sstevel@tonic-gate 		    "invalid segment handle\n"));
28840Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
28850Sstevel@tonic-gate 	}
28860Sstevel@tonic-gate 
28870Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	if (seg->rsmseg_pollfd_refcnt) {
28900Sstevel@tonic-gate 		seg->rsmseg_pollfd_refcnt--;
28910Sstevel@tonic-gate 		if (seg->rsmseg_pollfd_refcnt == 0) {
28920Sstevel@tonic-gate 			/* last reference removed - update the pollfd_table */
28930Sstevel@tonic-gate 			_rsm_remove_pollfd_table(seg->rsmseg_fd);
28940Sstevel@tonic-gate 		}
28950Sstevel@tonic-gate 	}
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
28980Sstevel@tonic-gate 
28990Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29000Sstevel@tonic-gate 	    "rsm_memseg_release_pollfd: exit\n"));
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 	return (RSM_SUCCESS);
29030Sstevel@tonic-gate }
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate /*
29060Sstevel@tonic-gate  * The interconnect topology data is obtained from the Kernel Agent
29070Sstevel@tonic-gate  * and stored in a memory buffer allocated by this function.  A pointer
29080Sstevel@tonic-gate  * to the buffer is stored in the location specified by the caller in
29090Sstevel@tonic-gate  * the function argument.  It is the callers responsibility to
29100Sstevel@tonic-gate  * call rsm_free_interconnect_topolgy() to free the allocated memory.
29110Sstevel@tonic-gate  */
29120Sstevel@tonic-gate int
29130Sstevel@tonic-gate _rsm_get_interconnect_topology(rsm_topology_t **topology_data)
29140Sstevel@tonic-gate {
29150Sstevel@tonic-gate 	uint32_t		topology_data_size;
29160Sstevel@tonic-gate 	rsm_topology_t		*topology_ptr;
29170Sstevel@tonic-gate 	int			error;
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29200Sstevel@tonic-gate 	    "rsm_get_interconnect_topology: enter\n"));
29210Sstevel@tonic-gate 
29220Sstevel@tonic-gate 	if (topology_data == NULL)
29230Sstevel@tonic-gate 		return (RSMERR_BAD_TOPOLOGY_PTR);
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate 	*topology_data = NULL;
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate again:
29280Sstevel@tonic-gate 	/* obtain the size of the topology data */
29290Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_TOPOLOGY_SIZE, &topology_data_size) < 0) {
29300Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29310Sstevel@tonic-gate 		    "RSM_IOCTL_TOPOLOGY_SIZE failed\n"));
29320Sstevel@tonic-gate 		return (errno);
29330Sstevel@tonic-gate 	}
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 	/* allocate double-word aligned memory to hold the topology data */
29360Sstevel@tonic-gate 	topology_ptr = (rsm_topology_t *)memalign(8, topology_data_size);
29370Sstevel@tonic-gate 	if (topology_ptr == NULL) {
29380Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29390Sstevel@tonic-gate 		    "not enough memory\n"));
29400Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
29410Sstevel@tonic-gate 	}
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	/*
29440Sstevel@tonic-gate 	 * Request the topology data.
29450Sstevel@tonic-gate 	 * Pass in the size to be used as a check in case
29460Sstevel@tonic-gate 	 * the data has grown since the size was obtained - if
29470Sstevel@tonic-gate 	 * it has, the errno value will be E2BIG.
29480Sstevel@tonic-gate 	 */
29490Sstevel@tonic-gate 	topology_ptr->topology_hdr.local_nodeid =
29500Sstevel@tonic-gate 	    (rsm_node_id_t)topology_data_size;
29510Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_TOPOLOGY_DATA, topology_ptr) < 0) {
29520Sstevel@tonic-gate 		error = errno;
29530Sstevel@tonic-gate 		free((void *)topology_ptr);
29540Sstevel@tonic-gate 		if (error == E2BIG)
29550Sstevel@tonic-gate 			goto again;
29560Sstevel@tonic-gate 		else {
29570Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
29580Sstevel@tonic-gate 			    "RSM_IOCTL_TOPOLOGY_DATA failed\n"));
29590Sstevel@tonic-gate 			return (error);
29600Sstevel@tonic-gate 		}
29610Sstevel@tonic-gate 	} else
29620Sstevel@tonic-gate 		*topology_data = topology_ptr;
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29650Sstevel@tonic-gate 	    " rsm_get_interconnect_topology: exit\n"));
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	return (RSM_SUCCESS);
29680Sstevel@tonic-gate }
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate void
29720Sstevel@tonic-gate _rsm_free_interconnect_topology(rsm_topology_t *topology_ptr)
29730Sstevel@tonic-gate {
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29760Sstevel@tonic-gate 	    "rsm_free_interconnect_topology: enter\n"));
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	if (topology_ptr) {
29790Sstevel@tonic-gate 		free((void *)topology_ptr);
29800Sstevel@tonic-gate 	}
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29830Sstevel@tonic-gate 	    "rsm_free_interconnect_topology: exit\n"));
29840Sstevel@tonic-gate }
29850Sstevel@tonic-gate 
29860Sstevel@tonic-gate int
29870Sstevel@tonic-gate _rsm_create_localmemory_handle(rsmapi_controller_handle_t cntrl_handle,
29880Sstevel@tonic-gate 				rsm_localmemory_handle_t *local_hndl_p,
29890Sstevel@tonic-gate 				caddr_t local_vaddr, size_t len)
29900Sstevel@tonic-gate {
29910Sstevel@tonic-gate 	int e;
29920Sstevel@tonic-gate 	rsm_controller_t *cntrl = (rsm_controller_t *)cntrl_handle;
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29950Sstevel@tonic-gate 	    "rsm_create_localmemory_handle: enter\n"));
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate 	if ((size_t)local_vaddr & (PAGESIZE - 1)) {
29980Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29990Sstevel@tonic-gate 		    "invalid arguments\n"));
30000Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
30010Sstevel@tonic-gate 	}
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	if (!cntrl_handle) {
30040Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
30050Sstevel@tonic-gate 		    "invalid controller handle\n"));
30060Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
30070Sstevel@tonic-gate 	}
30080Sstevel@tonic-gate 	if (!local_hndl_p) {
30090Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
30100Sstevel@tonic-gate 		    "invalid local memory handle pointer\n"));
30110Sstevel@tonic-gate 		return (RSMERR_BAD_LOCALMEM_HNDL);
30120Sstevel@tonic-gate 	}
30130Sstevel@tonic-gate 	if (len == 0) {
30140Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
30150Sstevel@tonic-gate 		    "invalid length\n"));
30160Sstevel@tonic-gate 		return (RSMERR_BAD_LENGTH);
30170Sstevel@tonic-gate 	}
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 	e = cntrl->cntr_segops->rsm_create_localmemory_handle(
30200Sstevel@tonic-gate 	    cntrl_handle,
30210Sstevel@tonic-gate 	    local_hndl_p,
30220Sstevel@tonic-gate 	    local_vaddr,
30230Sstevel@tonic-gate 	    len);
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
30260Sstevel@tonic-gate 	    "rsm_create_localmemory_handle: exit\n"));
30270Sstevel@tonic-gate 
30280Sstevel@tonic-gate 	return (e);
30290Sstevel@tonic-gate }
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate int
30320Sstevel@tonic-gate _rsm_free_localmemory_handle(rsmapi_controller_handle_t cntrl_handle,
30330Sstevel@tonic-gate     rsm_localmemory_handle_t local_handle)
30340Sstevel@tonic-gate {
30350Sstevel@tonic-gate 	int e;
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 	rsm_controller_t *cntrl = (rsm_controller_t *)cntrl_handle;
30380Sstevel@tonic-gate 
30390Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
30400Sstevel@tonic-gate 	    "rsm_free_localmemory_handle: enter\n"));
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 	if (!cntrl_handle) {
30440Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
30450Sstevel@tonic-gate 		    "invalid controller handle\n"));
30460Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
30470Sstevel@tonic-gate 	}
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	if (!local_handle) {
30500Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
30510Sstevel@tonic-gate 		    "invalid localmemory handle\n"));
30520Sstevel@tonic-gate 		return (RSMERR_BAD_LOCALMEM_HNDL);
30530Sstevel@tonic-gate 	}
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 	e = cntrl->cntr_segops->rsm_free_localmemory_handle(local_handle);
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
30580Sstevel@tonic-gate 	    "rsm_free_localmemory_handle: exit\n"));
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 	return (e);
30610Sstevel@tonic-gate }
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate int
30640Sstevel@tonic-gate _rsm_get_segmentid_range(const char *appid, rsm_memseg_id_t *baseid,
30650Sstevel@tonic-gate 	uint32_t *length)
30660Sstevel@tonic-gate {
30670Sstevel@tonic-gate 	char    buf[RSMFILE_BUFSIZE];
30680Sstevel@tonic-gate 	char	*s;
30690Sstevel@tonic-gate 	char	*fieldv[4];
30700Sstevel@tonic-gate 	int	fieldc = 0;
30710Sstevel@tonic-gate 	int	found = 0;
30720Sstevel@tonic-gate 	int	err = RSMERR_BAD_APPID;
30730Sstevel@tonic-gate 	FILE    *fp;
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate 	if (appid == NULL || baseid == NULL || length == NULL)
30760Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
30770Sstevel@tonic-gate 
3078*1914Scasper 	if ((fp = fopen(RSMSEGIDFILE, "rF")) == NULL) {
30790Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
30800Sstevel@tonic-gate 		    "cannot open <%s>\n", RSMSEGIDFILE));
30810Sstevel@tonic-gate 		return (RSMERR_BAD_CONF);
30820Sstevel@tonic-gate 	}
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	while (s = fgets(buf, RSMFILE_BUFSIZE, fp)) {
30850Sstevel@tonic-gate 		fieldc = 0;
30860Sstevel@tonic-gate 		while (isspace(*s))	/* skip the leading spaces */
30870Sstevel@tonic-gate 			s++;
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate 		if (*s == '#') {	/* comment line - skip it */
30900Sstevel@tonic-gate 			continue;
30910Sstevel@tonic-gate 		}
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate 		/*
30940Sstevel@tonic-gate 		 * parse the reserved segid file and
30950Sstevel@tonic-gate 		 * set the pointers appropriately.
30960Sstevel@tonic-gate 		 * fieldv[0] :  keyword
30970Sstevel@tonic-gate 		 * fieldv[1] :  application identifier
30980Sstevel@tonic-gate 		 * fieldv[2] :  baseid
30990Sstevel@tonic-gate 		 * fieldv[3] :  length
31000Sstevel@tonic-gate 		 */
31010Sstevel@tonic-gate 		while ((*s != '\n') && (*s != '\0') && (fieldc < 4)) {
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate 			while (isspace(*s)) /* skip the leading spaces */
31040Sstevel@tonic-gate 				s++;
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate 			fieldv[fieldc++] = s;
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 			if (fieldc == 4) {
31090Sstevel@tonic-gate 				if (fieldv[3][strlen(fieldv[3])-1] == '\n')
31100Sstevel@tonic-gate 					fieldv[3][strlen(fieldv[3])-1] = '\0';
31110Sstevel@tonic-gate 				break;
31120Sstevel@tonic-gate 			}
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 			while (*s && !isspace(*s))
31150Sstevel@tonic-gate 				++s;	/* move to the next white space */
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 			if (*s)
31180Sstevel@tonic-gate 				*s++ = '\0';
31190Sstevel@tonic-gate 		}
31200Sstevel@tonic-gate 
31210Sstevel@tonic-gate 		if (fieldc < 4) {	/* some fields are missing */
31220Sstevel@tonic-gate 			err = RSMERR_BAD_CONF;
31230Sstevel@tonic-gate 			break;
31240Sstevel@tonic-gate 		}
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate 		if (strcasecmp(fieldv[1], appid) == 0) { /* found a match */
31270Sstevel@tonic-gate 			if (strcasecmp(fieldv[0], RSMSEG_RESERVED) == 0) {
31280Sstevel@tonic-gate 				errno = 0;
31290Sstevel@tonic-gate 				*baseid = strtol(fieldv[2], (char **)NULL, 16);
31300Sstevel@tonic-gate 				if (errno != 0) {
31310Sstevel@tonic-gate 					err = RSMERR_BAD_CONF;
31320Sstevel@tonic-gate 					break;
31330Sstevel@tonic-gate 				}
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 				errno = 0;
31360Sstevel@tonic-gate 				*length = (int)strtol(fieldv[3],
31370Sstevel@tonic-gate 				    (char **)NULL, 10);
31380Sstevel@tonic-gate 				if (errno != 0) {
31390Sstevel@tonic-gate 					err = RSMERR_BAD_CONF;
31400Sstevel@tonic-gate 					break;
31410Sstevel@tonic-gate 				}
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 				found = 1;
31440Sstevel@tonic-gate 			} else {	/* error in format */
31450Sstevel@tonic-gate 				err = RSMERR_BAD_CONF;
31460Sstevel@tonic-gate 			}
31470Sstevel@tonic-gate 			break;
31480Sstevel@tonic-gate 		}
31490Sstevel@tonic-gate 	}
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 	(void) fclose(fp);
31520Sstevel@tonic-gate 
31530Sstevel@tonic-gate 	if (found)
31540Sstevel@tonic-gate 		return (RSM_SUCCESS);
31550Sstevel@tonic-gate 
31560Sstevel@tonic-gate 	return (err);
31570Sstevel@tonic-gate }
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate static 	int
31600Sstevel@tonic-gate _rsm_get_hwaddr(rsmapi_controller_handle_t handle, rsm_node_id_t nodeid,
31610Sstevel@tonic-gate     rsm_addr_t *hwaddrp)
31620Sstevel@tonic-gate {
31630Sstevel@tonic-gate 	rsm_ioctlmsg_t	msg = {0};
31640Sstevel@tonic-gate 	rsm_controller_t *ctrlp;
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
31670Sstevel@tonic-gate 	    "_rsm_get_hwaddr: enter\n"));
31680Sstevel@tonic-gate 
31690Sstevel@tonic-gate 	ctrlp = (rsm_controller_t *)handle;
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate 	if (ctrlp == NULL) {
31720Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
31730Sstevel@tonic-gate 		    "invalid controller handle\n"));
31740Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
31750Sstevel@tonic-gate 	}
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 	msg.cname = ctrlp->cntr_name;
31780Sstevel@tonic-gate 	msg.cname_len = strlen(ctrlp->cntr_name) +1;
31790Sstevel@tonic-gate 	msg.cnum = ctrlp->cntr_unit;
31800Sstevel@tonic-gate 	msg.nodeid = nodeid;
31810Sstevel@tonic-gate 
31820Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_MAP_TO_ADDR, &msg) < 0) {
31830Sstevel@tonic-gate 		int error = errno;
31840Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
31850Sstevel@tonic-gate 		    "RSM_IOCTL_MAP_TO_ADDR failed\n"));
31860Sstevel@tonic-gate 		return (error);
31870Sstevel@tonic-gate 	}
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate 	*hwaddrp = msg.hwaddr;
31900Sstevel@tonic-gate 
31910Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
31920Sstevel@tonic-gate 	    "_rsm_get_hwaddr: exit\n"));
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	return (RSM_SUCCESS);
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate }
31970Sstevel@tonic-gate 
31980Sstevel@tonic-gate static	int
31990Sstevel@tonic-gate _rsm_get_nodeid(rsmapi_controller_handle_t handle, rsm_addr_t hwaddr,
32000Sstevel@tonic-gate     rsm_node_id_t *nodeidp)
32010Sstevel@tonic-gate {
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate 	rsm_ioctlmsg_t	msg = {0};
32040Sstevel@tonic-gate 	rsm_controller_t *ctrlp;
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
32070Sstevel@tonic-gate 	    "_rsm_get_nodeid: enter\n"));
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 	ctrlp = (rsm_controller_t *)handle;
32100Sstevel@tonic-gate 
32110Sstevel@tonic-gate 	if (ctrlp == NULL) {
32120Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
32130Sstevel@tonic-gate 		    "invalid arguments\n"));
32140Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
32150Sstevel@tonic-gate 	}
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 	msg.cname = ctrlp->cntr_name;
32180Sstevel@tonic-gate 	msg.cname_len = strlen(ctrlp->cntr_name) +1;
32190Sstevel@tonic-gate 	msg.cnum = ctrlp->cntr_unit;
32200Sstevel@tonic-gate 	msg.hwaddr = hwaddr;
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_MAP_TO_NODEID, &msg) < 0) {
32230Sstevel@tonic-gate 		int error = errno;
32240Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
32250Sstevel@tonic-gate 		    "RSM_IOCTL_MAP_TO_NODEID failed\n"));
32260Sstevel@tonic-gate 		return (error);
32270Sstevel@tonic-gate 	}
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate 	*nodeidp = msg.nodeid;
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
32320Sstevel@tonic-gate 	    "_rsm_get_nodeid: exit\n"));
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	return (RSM_SUCCESS);
32350Sstevel@tonic-gate 
32360Sstevel@tonic-gate }
32370Sstevel@tonic-gate 
32380Sstevel@tonic-gate #ifdef DEBUG
32390Sstevel@tonic-gate void
32400Sstevel@tonic-gate dbg_printf(int msg_category, int msg_level, char *fmt, ...)
32410Sstevel@tonic-gate {
32420Sstevel@tonic-gate 	if ((msg_category & rsmlibdbg_category) &&
32430Sstevel@tonic-gate 	    (msg_level <= rsmlibdbg_level)) {
32440Sstevel@tonic-gate 		va_list arg_list;
32450Sstevel@tonic-gate 		va_start(arg_list, fmt);
32460Sstevel@tonic-gate 		mutex_lock(&rsmlog_lock);
32470Sstevel@tonic-gate 		fprintf(rsmlog_fd,
32480Sstevel@tonic-gate 			"Thread %d ", thr_self());
32490Sstevel@tonic-gate 		vfprintf(rsmlog_fd, fmt, arg_list);
32500Sstevel@tonic-gate 		fflush(rsmlog_fd);
32510Sstevel@tonic-gate 		mutex_unlock(&rsmlog_lock);
32520Sstevel@tonic-gate 		va_end(arg_list);
32530Sstevel@tonic-gate 	}
32540Sstevel@tonic-gate }
32550Sstevel@tonic-gate #endif /* DEBUG */
3256