xref: /titanic_51/usr/src/lib/librsm/common/rsmlib.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e8031f0aSraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/mman.h>
397c478bd9Sstevel@tonic-gate #include <sys/uio.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <sys/resource.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <assert.h>
447c478bd9Sstevel@tonic-gate #include <fcntl.h>
457c478bd9Sstevel@tonic-gate #include <dlfcn.h>
467c478bd9Sstevel@tonic-gate #include <sched.h>
477c478bd9Sstevel@tonic-gate #include <stropts.h>
487c478bd9Sstevel@tonic-gate #include <poll.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <rsmapi.h>
517c478bd9Sstevel@tonic-gate #include <sys/rsm/rsmndi.h>
527c478bd9Sstevel@tonic-gate #include <rsmlib_in.h>
537c478bd9Sstevel@tonic-gate #include <sys/rsm/rsm.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* lint -w2 */
567c478bd9Sstevel@tonic-gate extern void __rsmloopback_init_ops(rsm_segops_t *);
577c478bd9Sstevel@tonic-gate extern void __rsmdefault_setops(rsm_segops_t *);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate typedef void (*rsm_access_func_t)(void *, void *, rsm_access_size_t);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #ifdef DEBUG
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	RSMLOG_BUF_SIZE 256
647c478bd9Sstevel@tonic-gate FILE *rsmlog_fd = NULL;
657c478bd9Sstevel@tonic-gate static mutex_t rsmlog_lock;
667c478bd9Sstevel@tonic-gate int rsmlibdbg_category = RSM_LIBRARY;
677c478bd9Sstevel@tonic-gate int rsmlibdbg_level = RSM_ERR;
687c478bd9Sstevel@tonic-gate void dbg_printf(int category, int level, char *fmt, ...);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #endif /* DEBUG */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate rsm_node_id_t rsm_local_nodeid = 0;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static rsm_controller_t *controller_list = NULL;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static rsm_segops_t loopback_ops;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	MAX_STRLEN	80
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	RSM_IOTYPE_PUTGET	1
817c478bd9Sstevel@tonic-gate #define	RSM_IOTYPE_SCATGATH	2
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	RSMFILE_BUFSIZE		256
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #pragma init(_rsm_librsm_init)
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static mutex_t _rsm_lock;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static int _rsm_fd = -1;
907c478bd9Sstevel@tonic-gate static rsm_gnum_t *bar_va, bar_fixed = 0;
917c478bd9Sstevel@tonic-gate static rsm_pollfd_table_t pollfd_table;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static int _rsm_get_hwaddr(rsmapi_controller_handle_t handle,
947c478bd9Sstevel@tonic-gate rsm_node_id_t, rsm_addr_t *hwaddrp);
957c478bd9Sstevel@tonic-gate static int _rsm_get_nodeid(rsmapi_controller_handle_t,
967c478bd9Sstevel@tonic-gate rsm_addr_t, rsm_node_id_t *);
977c478bd9Sstevel@tonic-gate static int __rsm_import_implicit_map(rsmseg_handle_t *, int);
987c478bd9Sstevel@tonic-gate static int __rsm_intr_signal_wait_common(struct pollfd [], minor_t [],
997c478bd9Sstevel@tonic-gate     nfds_t, int, int *);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static	rsm_lib_funcs_t lib_functions = {
1027c478bd9Sstevel@tonic-gate 	RSM_LIB_FUNCS_VERSION,
1037c478bd9Sstevel@tonic-gate 	_rsm_get_hwaddr,
1047c478bd9Sstevel@tonic-gate 	_rsm_get_nodeid
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate rsm_topology_t *tp;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * service module function templates:
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * The _rsm_librsm_init function is called the first time an application
1167c478bd9Sstevel@tonic-gate  * references the RSMAPI library
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate int
1197c478bd9Sstevel@tonic-gate _rsm_librsm_init()
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t 		msg;
1227c478bd9Sstevel@tonic-gate 	int e, tmpfd;
1237c478bd9Sstevel@tonic-gate 	int i;
1247c478bd9Sstevel@tonic-gate 	char logname[MAXNAMELEN];
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	mutex_init(&_rsm_lock, USYNC_THREAD, NULL);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #ifdef DEBUG
1297c478bd9Sstevel@tonic-gate 	mutex_init(&rsmlog_lock, USYNC_THREAD, NULL);
1307c478bd9Sstevel@tonic-gate 	sprintf(logname, "%s.%d", TRACELOG, getpid());
131004388ebScasper 	rsmlog_fd = fopen(logname, "w+F");
1327c478bd9Sstevel@tonic-gate 	if (rsmlog_fd == NULL) {
1337c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Log file open failed\n");
1347c478bd9Sstevel@tonic-gate 		return (errno);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1407c478bd9Sstevel@tonic-gate 	    "_rsm_librsm_init: enter\n"));
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/* initialize the pollfd_table */
1437c478bd9Sstevel@tonic-gate 	mutex_init(&pollfd_table.lock, USYNC_THREAD, NULL);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	for (i = 0; i < RSM_MAX_BUCKETS; i++) {
1467c478bd9Sstevel@tonic-gate 		pollfd_table.buckets[i] = NULL;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/* open /dev/rsm and mmap barrier generation pages */
1507c478bd9Sstevel@tonic-gate 	mutex_lock(&_rsm_lock);
1517c478bd9Sstevel@tonic-gate 	_rsm_fd = open(DEVRSM, O_RDONLY);
1527c478bd9Sstevel@tonic-gate 	if (_rsm_fd < 0) {
1537c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
1547c478bd9Sstevel@tonic-gate 		    "unable to open /dev/rsm\n"));
1557c478bd9Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
1567c478bd9Sstevel@tonic-gate 		return (errno);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * DUP the opened file descriptor to something greater than
1617c478bd9Sstevel@tonic-gate 	 * STDERR_FILENO so that we never use the STDIN_FILENO,
1627c478bd9Sstevel@tonic-gate 	 * STDOUT_FILENO or STDERR_FILENO.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	tmpfd = fcntl(_rsm_fd, F_DUPFD, 3);
1657c478bd9Sstevel@tonic-gate 	if (tmpfd < 0) {
1667c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
1677c478bd9Sstevel@tonic-gate 		    "F_DUPFD failed\n"));
1687c478bd9Sstevel@tonic-gate 	} else {
1697c478bd9Sstevel@tonic-gate 		(void) close(_rsm_fd);
1707c478bd9Sstevel@tonic-gate 		_rsm_fd = tmpfd;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1747c478bd9Sstevel@tonic-gate 	    "_rsm_fd is %d\n", _rsm_fd));
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (fcntl(_rsm_fd, F_SETFD, FD_CLOEXEC) < 0) {
1777c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
1787c478bd9Sstevel@tonic-gate 		"F_SETFD failed\n"));
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* get mapping generation number page info */
1827c478bd9Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_BAR_INFO, &msg) < 0) {
1837c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
1847c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_BAR_INFO failed\n"));
1857c478bd9Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
1867c478bd9Sstevel@tonic-gate 		return (errno);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * bar_va is mapped to the mapping generation number page
1917c478bd9Sstevel@tonic-gate 	 * in order to support close barrier
1927c478bd9Sstevel@tonic-gate 	 */
1937c478bd9Sstevel@tonic-gate 	/* LINTED */
1947c478bd9Sstevel@tonic-gate 	bar_va = (rsm_gnum_t *)mmap(NULL, msg.len,
1957c478bd9Sstevel@tonic-gate 	    PROT_READ, MAP_SHARED, _rsm_fd, msg.off);
1967c478bd9Sstevel@tonic-gate 	if (bar_va == (rsm_gnum_t *)MAP_FAILED) {
1977c478bd9Sstevel@tonic-gate 		bar_va = NULL;
1987c478bd9Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
1997c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
2007c478bd9Sstevel@tonic-gate 		    "unable to map barrier page\n"));
2017c478bd9Sstevel@tonic-gate 		return (RSMERR_MAP_FAILED);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	mutex_unlock(&_rsm_lock);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/* get local nodeid */
2077c478bd9Sstevel@tonic-gate 	e = rsm_get_interconnect_topology(&tp);
2087c478bd9Sstevel@tonic-gate 	if (e != RSM_SUCCESS) {
2097c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
2107c478bd9Sstevel@tonic-gate 		    "unable to obtain topology data\n"));
2117c478bd9Sstevel@tonic-gate 		return (e);
2127c478bd9Sstevel@tonic-gate 	} else
2137c478bd9Sstevel@tonic-gate 		rsm_local_nodeid = tp->topology_hdr.local_nodeid;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	rsm_free_interconnect_topology(tp);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
2187c478bd9Sstevel@tonic-gate 	    "_rsm_librsm_init: exit\n"));
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static int
2247c478bd9Sstevel@tonic-gate _rsm_loopbackload(caddr_t name, int unit, rsm_controller_t **chdl)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	rsm_controller_t *p;
2277c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_DEBUG_VERBOSE,
2307c478bd9Sstevel@tonic-gate 	    "_rsm_loopbackload: enter\n"));
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * For now do this, but we should open some file and read the
2337c478bd9Sstevel@tonic-gate 	 * list of supported controllers and there numbers.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	p = (rsm_controller_t *)malloc(sizeof (*p) + strlen(name) + 1);
2377c478bd9Sstevel@tonic-gate 	if (!p) {
2387c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_ERR,
2397c478bd9Sstevel@tonic-gate 		    "not enough memory\n"));
2407c478bd9Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	msg.cname = name;
2447c478bd9Sstevel@tonic-gate 	msg.cname_len = strlen(name) +1;
2457c478bd9Sstevel@tonic-gate 	msg.cnum = unit;
2467c478bd9Sstevel@tonic-gate 	msg.arg = (caddr_t)&p->cntr_attr;
2477c478bd9Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_ATTR, &msg) < 0) {
2487c478bd9Sstevel@tonic-gate 		int error = errno;
2497c478bd9Sstevel@tonic-gate 		free((void *)p);
2507c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_ERR,
2517c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_ATTR failed\n"));
2527c478bd9Sstevel@tonic-gate 		return (error);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	__rsmloopback_init_ops(&loopback_ops);
2567c478bd9Sstevel@tonic-gate 	__rsmdefault_setops(&loopback_ops);
2577c478bd9Sstevel@tonic-gate 	p->cntr_segops = &loopback_ops;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * Should add this entry into list
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	p->cntr_fd = _rsm_fd;
2637c478bd9Sstevel@tonic-gate 	p->cntr_name = strcpy((char *)(p+1), name);
2647c478bd9Sstevel@tonic-gate 	p->cntr_unit = unit;
2657c478bd9Sstevel@tonic-gate 	p->cntr_refcnt = 1;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	mutex_init(&p->cntr_lock, USYNC_THREAD, NULL);
2697c478bd9Sstevel@tonic-gate 	cond_init(&p->cntr_cv, USYNC_THREAD, NULL);
2707c478bd9Sstevel@tonic-gate 	p->cntr_rqlist = NULL;
2717c478bd9Sstevel@tonic-gate 	p->cntr_segops->rsm_get_lib_attr(&p->cntr_lib_attr);
2727c478bd9Sstevel@tonic-gate 	p->cntr_next = controller_list;
2737c478bd9Sstevel@tonic-gate 	controller_list = p;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	*chdl = p;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_DEBUG_VERBOSE,
2787c478bd9Sstevel@tonic-gate 	    "_rsm_loopbackload: exit\n"));
2797c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate static int
2847c478bd9Sstevel@tonic-gate _rsm_modload(caddr_t name, int unit, rsmapi_controller_handle_t *controller)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	int error = RSM_SUCCESS;
2877c478bd9Sstevel@tonic-gate 	char clib[MAX_STRLEN];
2887c478bd9Sstevel@tonic-gate 	rsm_controller_t *p = NULL;
2897c478bd9Sstevel@tonic-gate 	void *dlh;
2907c478bd9Sstevel@tonic-gate 	rsm_attach_entry_t fptr;
2917c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
2947c478bd9Sstevel@tonic-gate 	    "_rsm_modload: enter\n"));
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	(void) sprintf(clib, "%s.so", name);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/* found entry, try to load library */
2997c478bd9Sstevel@tonic-gate 	dlh = dlopen(clib, RTLD_LAZY);
3007c478bd9Sstevel@tonic-gate 	if (dlh == NULL) {
3017c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
3027c478bd9Sstevel@tonic-gate 		    "unable to find plugin library\n"));
3037c478bd9Sstevel@tonic-gate 		error = RSMERR_CTLR_NOT_PRESENT;
3047c478bd9Sstevel@tonic-gate 		goto skiplib;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	(void) sprintf(clib, "%s_opendevice", name);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	fptr = (rsm_attach_entry_t)dlsym(dlh, clib); /* lint !e611 */
3107c478bd9Sstevel@tonic-gate 	if (fptr != NULL) {
3117c478bd9Sstevel@tonic-gate 		/* allocate new lib structure */
3127c478bd9Sstevel@tonic-gate 		/* get ops handler, attr and ops */
3137c478bd9Sstevel@tonic-gate 		p = (rsm_controller_t *)malloc(sizeof (*p) + strlen(name) + 1);
3147c478bd9Sstevel@tonic-gate 		if (p != NULL) {
3157c478bd9Sstevel@tonic-gate 			error = fptr(unit, &p->cntr_segops);
3167c478bd9Sstevel@tonic-gate 		} else {
3177c478bd9Sstevel@tonic-gate 			error = RSMERR_INSUFFICIENT_MEM;
3187c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
3197c478bd9Sstevel@tonic-gate 			    "not enough memory\n"));
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	} else {
3227c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
3237c478bd9Sstevel@tonic-gate 		    "can't find symbol %s\n", clib));
3247c478bd9Sstevel@tonic-gate 		error = RSMERR_CTLR_NOT_PRESENT;
3257c478bd9Sstevel@tonic-gate 		(void) dlclose(dlh);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate skiplib:
3297c478bd9Sstevel@tonic-gate 	if ((error != RSM_SUCCESS) || (p == NULL)) {
3307c478bd9Sstevel@tonic-gate 		if (p != NULL)
3317c478bd9Sstevel@tonic-gate 			free((void *)p);
3327c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
3337c478bd9Sstevel@tonic-gate 		    "_rsm_modload error %d\n", error));
3347c478bd9Sstevel@tonic-gate 		return (error);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* check the version number */
3387c478bd9Sstevel@tonic-gate 	if (p->cntr_segops->rsm_version != RSM_LIB_VERSION) {
3397c478bd9Sstevel@tonic-gate 		/* bad version number */
3407c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
3417c478bd9Sstevel@tonic-gate 		    "wrong version; "
3427c478bd9Sstevel@tonic-gate 		    "found %d, expected %d\n",
3437c478bd9Sstevel@tonic-gate 		    p->cntr_segops->rsm_version, RSM_LIB_VERSION));
3447c478bd9Sstevel@tonic-gate 		free(p);
3457c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_LIBRARY_VERSION);
3467c478bd9Sstevel@tonic-gate 	} else {
3477c478bd9Sstevel@tonic-gate 		/* pass the fuctions to NDI library */
3487c478bd9Sstevel@tonic-gate 		if ((p->cntr_segops->rsm_register_lib_funcs == NULL) ||
3497c478bd9Sstevel@tonic-gate 		    (p->cntr_segops->rsm_register_lib_funcs(
3507c478bd9Sstevel@tonic-gate 		    &lib_functions) != RSM_SUCCESS)) {
3517c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
3527c478bd9Sstevel@tonic-gate 			    "RSMNDI library not registering lib functions\n"));
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		/* get controller attributes */
3567c478bd9Sstevel@tonic-gate 		msg.cnum = unit;
3577c478bd9Sstevel@tonic-gate 		msg.cname = name;
3587c478bd9Sstevel@tonic-gate 		msg.cname_len = strlen(name) +1;
3597c478bd9Sstevel@tonic-gate 		msg.arg = (caddr_t)&p->cntr_attr;
3607c478bd9Sstevel@tonic-gate 		if (ioctl(_rsm_fd, RSM_IOCTL_ATTR, &msg) < 0) {
3617c478bd9Sstevel@tonic-gate 			error = errno;
3627c478bd9Sstevel@tonic-gate 			free((void *)p);
3637c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
3647c478bd9Sstevel@tonic-gate 			    "RSM_IOCTL_ATTR failed\n"));
3657c478bd9Sstevel@tonic-gate 			return (error);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		/* set controller access functions */
3697c478bd9Sstevel@tonic-gate 		__rsmdefault_setops(p->cntr_segops);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		mutex_init(&p->cntr_lock, USYNC_THREAD, NULL);
3727c478bd9Sstevel@tonic-gate 		cond_init(&p->cntr_cv, USYNC_THREAD, NULL);
3737c478bd9Sstevel@tonic-gate 		p->cntr_rqlist = NULL;
3747c478bd9Sstevel@tonic-gate 		p->cntr_segops->rsm_get_lib_attr(&p->cntr_lib_attr);
3757c478bd9Sstevel@tonic-gate 		/* insert into list of controllers */
3767c478bd9Sstevel@tonic-gate 		p->cntr_name = strcpy((char *)(p+1), name);
3777c478bd9Sstevel@tonic-gate 		p->cntr_fd = _rsm_fd;
3787c478bd9Sstevel@tonic-gate 		p->cntr_unit = unit;
3797c478bd9Sstevel@tonic-gate 		p->cntr_refcnt = 1;	/* first reference */
3807c478bd9Sstevel@tonic-gate 		p->cntr_next = controller_list;
3817c478bd9Sstevel@tonic-gate 		controller_list = p;
3827c478bd9Sstevel@tonic-gate 		*controller = (rsmapi_controller_handle_t)p;
3837c478bd9Sstevel@tonic-gate 		errno = RSM_SUCCESS;
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
3877c478bd9Sstevel@tonic-gate 	    "_rsm_modload: exit\n"));
3887c478bd9Sstevel@tonic-gate 	return (error);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * inserts a given segment handle into the pollfd table, this is called
3937c478bd9Sstevel@tonic-gate  * when rsm_memseg_get_pollfd() is called the first time on a segment handle.
3947c478bd9Sstevel@tonic-gate  * Returns RSM_SUCCESS if successful otherwise the error code is returned
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate static int
3977c478bd9Sstevel@tonic-gate _rsm_insert_pollfd_table(int segfd, minor_t segrnum)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	int i;
4007c478bd9Sstevel@tonic-gate 	int hash;
4017c478bd9Sstevel@tonic-gate 	rsm_pollfd_chunk_t *chunk;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	hash = RSM_POLLFD_HASH(segfd);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	mutex_lock(&pollfd_table.lock);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	chunk = pollfd_table.buckets[hash];
4087c478bd9Sstevel@tonic-gate 	while (chunk) {
4097c478bd9Sstevel@tonic-gate 		if (chunk->nfree > 0)
4107c478bd9Sstevel@tonic-gate 			break;
4117c478bd9Sstevel@tonic-gate 		chunk = chunk->next;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (!chunk) { /* couldn't find a free chunk - allocate a new one */
4157c478bd9Sstevel@tonic-gate 		chunk = malloc(sizeof (rsm_pollfd_chunk_t));
4167c478bd9Sstevel@tonic-gate 		if (!chunk) {
4177c478bd9Sstevel@tonic-gate 			mutex_unlock(&pollfd_table.lock);
4187c478bd9Sstevel@tonic-gate 			return (RSMERR_INSUFFICIENT_MEM);
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 		chunk->nfree = RSM_POLLFD_PER_CHUNK - 1;
4217c478bd9Sstevel@tonic-gate 		chunk->fdarray[0].fd = segfd;
4227c478bd9Sstevel@tonic-gate 		chunk->fdarray[0].segrnum = segrnum;
4237c478bd9Sstevel@tonic-gate 		for (i = 1; i < RSM_POLLFD_PER_CHUNK; i++) {
4247c478bd9Sstevel@tonic-gate 			chunk->fdarray[i].fd = -1;
4257c478bd9Sstevel@tonic-gate 			chunk->fdarray[i].segrnum = 0;
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 		/* insert this into the hash table */
4287c478bd9Sstevel@tonic-gate 		chunk->next = pollfd_table.buckets[hash];
4297c478bd9Sstevel@tonic-gate 		pollfd_table.buckets[hash] = chunk;
4307c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
4317c478bd9Sstevel@tonic-gate 		    "rsm_insert_pollfd: new chunk(%p) @ %d for %d:%d\n",
4327c478bd9Sstevel@tonic-gate 		    chunk, hash, segfd, segrnum));
4337c478bd9Sstevel@tonic-gate 	} else { /* a chunk with free slot was found */
4347c478bd9Sstevel@tonic-gate 		for (i = 0; i < RSM_POLLFD_PER_CHUNK; i++) {
4357c478bd9Sstevel@tonic-gate 			if (chunk->fdarray[i].fd == -1) {
4367c478bd9Sstevel@tonic-gate 				chunk->fdarray[i].fd = segfd;
4377c478bd9Sstevel@tonic-gate 				chunk->fdarray[i].segrnum = segrnum;
4387c478bd9Sstevel@tonic-gate 				chunk->nfree--;
4397c478bd9Sstevel@tonic-gate 				break;
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
4437c478bd9Sstevel@tonic-gate 		    "rsm_insert_pollfd: inserted @ %d for %d:%d chunk(%p)\n",
4447c478bd9Sstevel@tonic-gate 		    hash, segfd, segrnum, chunk));
4457c478bd9Sstevel@tonic-gate 		assert(i < RSM_POLLFD_PER_CHUNK);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	mutex_unlock(&pollfd_table.lock);
4497c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Given a file descriptor returns the corresponding segment handles
4547c478bd9Sstevel@tonic-gate  * resource number, if the fd is not found returns 0. 0 is not a valid
4557c478bd9Sstevel@tonic-gate  * minor number for a rsmapi segment since it is used for the barrier
4567c478bd9Sstevel@tonic-gate  * resource.
4577c478bd9Sstevel@tonic-gate  */
4587c478bd9Sstevel@tonic-gate static minor_t
4597c478bd9Sstevel@tonic-gate _rsm_lookup_pollfd_table(int segfd)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	int i;
4627c478bd9Sstevel@tonic-gate 	rsm_pollfd_chunk_t	*chunk;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (segfd < 0)
4657c478bd9Sstevel@tonic-gate 		return (0);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	mutex_lock(&pollfd_table.lock);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	chunk = pollfd_table.buckets[RSM_POLLFD_HASH(segfd)];
4707c478bd9Sstevel@tonic-gate 	while (chunk) {
4717c478bd9Sstevel@tonic-gate 		assert(chunk->nfree < RSM_POLLFD_PER_CHUNK);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		for (i = 0; i < RSM_POLLFD_PER_CHUNK; i++) {
4747c478bd9Sstevel@tonic-gate 			if (chunk->fdarray[i].fd == segfd) {
4757c478bd9Sstevel@tonic-gate 				mutex_unlock(&pollfd_table.lock);
4767c478bd9Sstevel@tonic-gate 				DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
4777c478bd9Sstevel@tonic-gate 				    "rsm_lookup_pollfd: found(%d) rnum(%d)\n",
4787c478bd9Sstevel@tonic-gate 				    segfd, chunk->fdarray[i].segrnum));
4797c478bd9Sstevel@tonic-gate 				return (chunk->fdarray[i].segrnum);
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 		chunk = chunk->next;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	mutex_unlock(&pollfd_table.lock);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
4887c478bd9Sstevel@tonic-gate 	    "rsm_lookup_pollfd: not found(%d)\n", segfd));
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	return (0);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate  * Remove the entry corresponding to the given file descriptor from the
4957c478bd9Sstevel@tonic-gate  * pollfd table.
4967c478bd9Sstevel@tonic-gate  */
4977c478bd9Sstevel@tonic-gate static void
4987c478bd9Sstevel@tonic-gate _rsm_remove_pollfd_table(int segfd)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	int i;
5017c478bd9Sstevel@tonic-gate 	int hash;
5027c478bd9Sstevel@tonic-gate 	rsm_pollfd_chunk_t	*chunk;
5037c478bd9Sstevel@tonic-gate 	rsm_pollfd_chunk_t	*prev_chunk;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (segfd < 0)
5067c478bd9Sstevel@tonic-gate 		return;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	hash = RSM_POLLFD_HASH(segfd);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	mutex_lock(&pollfd_table.lock);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	prev_chunk = chunk = pollfd_table.buckets[hash];
5137c478bd9Sstevel@tonic-gate 	while (chunk) {
5147c478bd9Sstevel@tonic-gate 		assert(chunk->nfree < RSM_POLLFD_PER_CHUNK);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		for (i = 0; i < RSM_POLLFD_PER_CHUNK; i++) {
5177c478bd9Sstevel@tonic-gate 			if (chunk->fdarray[i].fd == segfd) {
5187c478bd9Sstevel@tonic-gate 				DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
5197c478bd9Sstevel@tonic-gate 				    "rsm_remove_pollfd: %d:%d\n",
5207c478bd9Sstevel@tonic-gate 				    chunk->fdarray[i].fd,
5217c478bd9Sstevel@tonic-gate 				    chunk->fdarray[i].segrnum));
5227c478bd9Sstevel@tonic-gate 				chunk->fdarray[i].fd = -1;
5237c478bd9Sstevel@tonic-gate 				chunk->fdarray[i].segrnum = 0;
5247c478bd9Sstevel@tonic-gate 				chunk->nfree++;
5257c478bd9Sstevel@tonic-gate 				if (chunk->nfree == RSM_POLLFD_PER_CHUNK) {
5267c478bd9Sstevel@tonic-gate 					/* chunk is empty free it */
5277c478bd9Sstevel@tonic-gate 					if (prev_chunk == chunk) {
5287c478bd9Sstevel@tonic-gate 						pollfd_table.buckets[hash] =
5297c478bd9Sstevel@tonic-gate 						    chunk->next;
5307c478bd9Sstevel@tonic-gate 					} else {
5317c478bd9Sstevel@tonic-gate 						prev_chunk->next = chunk->next;
5327c478bd9Sstevel@tonic-gate 					}
5337c478bd9Sstevel@tonic-gate 					DBPRINTF((RSM_LIBRARY,
5347c478bd9Sstevel@tonic-gate 					    RSM_DEBUG_VERBOSE,
5357c478bd9Sstevel@tonic-gate 					    "rsm_remove_pollfd:free(%p)\n",
5367c478bd9Sstevel@tonic-gate 					    chunk));
5377c478bd9Sstevel@tonic-gate 					free(chunk);
5387c478bd9Sstevel@tonic-gate 					mutex_unlock(&pollfd_table.lock);
5397c478bd9Sstevel@tonic-gate 					return;
5407c478bd9Sstevel@tonic-gate 				}
5417c478bd9Sstevel@tonic-gate 			}
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		prev_chunk = chunk;
5447c478bd9Sstevel@tonic-gate 		chunk = chunk->next;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	mutex_unlock(&pollfd_table.lock);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate int
551*7257d1b4Sraf rsm_get_controller(char *name, rsmapi_controller_handle_t *chdl)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	rsm_controller_t *p;
5547c478bd9Sstevel@tonic-gate 	char	cntr_name[MAXNAMELEN];	/* cntr_name=<cntr_type><unit> */
5557c478bd9Sstevel@tonic-gate 	char	*cntr_type;
5567c478bd9Sstevel@tonic-gate 	int	unit = 0;
5577c478bd9Sstevel@tonic-gate 	int	i, e;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
5607c478bd9Sstevel@tonic-gate 	    "rsm_get_controller: enter\n"));
5617c478bd9Sstevel@tonic-gate 	/*
5627c478bd9Sstevel@tonic-gate 	 * Lookup controller name and return ops vector and controller
5637c478bd9Sstevel@tonic-gate 	 * structure
5647c478bd9Sstevel@tonic-gate 	 */
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (!chdl) {
5677c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
5687c478bd9Sstevel@tonic-gate 		    "Invalid controller handle\n"));
5697c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	if (!name) {
5727c478bd9Sstevel@tonic-gate 		/* use loopback if null */
5737c478bd9Sstevel@tonic-gate 		cntr_type = LOOPBACK;
5747c478bd9Sstevel@tonic-gate 	} else {
5757c478bd9Sstevel@tonic-gate 		(void) strcpy(cntr_name, name);
5767c478bd9Sstevel@tonic-gate 		/* scan from the end till a non-digit is found */
5777c478bd9Sstevel@tonic-gate 		for (i = strlen(cntr_name) - 1; i >= 0; i--) {
5787c478bd9Sstevel@tonic-gate 			if (! isdigit((int)cntr_name[i]))
5797c478bd9Sstevel@tonic-gate 				break;
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 		i++;
5827c478bd9Sstevel@tonic-gate 		unit = atoi((char *)cntr_name+i);
5837c478bd9Sstevel@tonic-gate 		cntr_name[i] = '\0';	/* null terminate the cntr_type part */
5847c478bd9Sstevel@tonic-gate 		cntr_type = (char *)cntr_name;
5857c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
5867c478bd9Sstevel@tonic-gate 		    "cntr_type=%s, instance=%d\n",
5877c478bd9Sstevel@tonic-gate 		    cntr_type, unit));
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/* protect the controller_list by locking the device/library */
5917c478bd9Sstevel@tonic-gate 	mutex_lock(&_rsm_lock);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	for (p = controller_list; p; p = p->cntr_next) {
5947c478bd9Sstevel@tonic-gate 		if (!strcasecmp(p->cntr_name, cntr_type) &&
5957c478bd9Sstevel@tonic-gate 		    !strcasecmp(cntr_type, LOOPBACK)) {
5967c478bd9Sstevel@tonic-gate 			p->cntr_refcnt++;
5977c478bd9Sstevel@tonic-gate 			*chdl = (rsmapi_controller_handle_t)p;
5987c478bd9Sstevel@tonic-gate 			mutex_unlock(&_rsm_lock);
5997c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6007c478bd9Sstevel@tonic-gate 			    "rsm_get_controller: exit\n"));
6017c478bd9Sstevel@tonic-gate 			return (RSM_SUCCESS);
6027c478bd9Sstevel@tonic-gate 		} else if (!strcasecmp(p->cntr_name, cntr_type) &&
6037c478bd9Sstevel@tonic-gate 		    (p->cntr_unit == unit)) {
6047c478bd9Sstevel@tonic-gate 			p->cntr_refcnt++;
6057c478bd9Sstevel@tonic-gate 			*chdl = (rsmapi_controller_handle_t)p;
6067c478bd9Sstevel@tonic-gate 			mutex_unlock(&_rsm_lock);
6077c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6087c478bd9Sstevel@tonic-gate 			    "rsm_get_controller: exit\n"));
6097c478bd9Sstevel@tonic-gate 			return (RSM_SUCCESS);
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (!strcasecmp(cntr_type, LOOPBACK)) {
6157c478bd9Sstevel@tonic-gate 		e = _rsm_loopbackload(cntr_type, unit,
6167c478bd9Sstevel@tonic-gate 		    (rsm_controller_t **)chdl);
6177c478bd9Sstevel@tonic-gate 	} else {
6187c478bd9Sstevel@tonic-gate 		e = _rsm_modload(cntr_type, unit, chdl);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	mutex_unlock(&_rsm_lock);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6247c478bd9Sstevel@tonic-gate 	    " rsm_get_controller: exit\n"));
6257c478bd9Sstevel@tonic-gate 	return (e);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate int
629*7257d1b4Sraf rsm_release_controller(rsmapi_controller_handle_t cntr_handle)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	int			e = RSM_SUCCESS;
6327c478bd9Sstevel@tonic-gate 	rsm_controller_t	*chdl = (rsm_controller_t *)cntr_handle;
6337c478bd9Sstevel@tonic-gate 	rsm_controller_t	*curr, *prev;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6367c478bd9Sstevel@tonic-gate 	    "rsm_release_controller: enter\n"));
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	mutex_lock(&_rsm_lock);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (chdl->cntr_refcnt == 0) {
6417c478bd9Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
6427c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
6437c478bd9Sstevel@tonic-gate 		    "controller reference count is zero\n"));
6447c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	chdl->cntr_refcnt--;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	if (chdl->cntr_refcnt > 0) {
6507c478bd9Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
6517c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6527c478bd9Sstevel@tonic-gate 		    "rsm_release_controller: exit\n"));
6537c478bd9Sstevel@tonic-gate 		return (RSM_SUCCESS);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	e = chdl->cntr_segops->rsm_closedevice(cntr_handle);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	/*
6597c478bd9Sstevel@tonic-gate 	 * remove the controller in any case from the controller list
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	prev = curr = controller_list;
6637c478bd9Sstevel@tonic-gate 	while (curr != NULL) {
6647c478bd9Sstevel@tonic-gate 		if (curr == chdl) {
6657c478bd9Sstevel@tonic-gate 			if (curr == prev) {
6667c478bd9Sstevel@tonic-gate 				controller_list = curr->cntr_next;
6677c478bd9Sstevel@tonic-gate 			} else {
6687c478bd9Sstevel@tonic-gate 				prev->cntr_next = curr->cntr_next;
6697c478bd9Sstevel@tonic-gate 			}
6707c478bd9Sstevel@tonic-gate 			free(curr);
6717c478bd9Sstevel@tonic-gate 			break;
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 		prev = curr;
6747c478bd9Sstevel@tonic-gate 		curr = curr->cntr_next;
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	mutex_unlock(&_rsm_lock);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6797c478bd9Sstevel@tonic-gate 	    "rsm_release_controller: exit\n"));
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	return (e);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate 
684*7257d1b4Sraf int
685*7257d1b4Sraf rsm_get_controller_attr(rsmapi_controller_handle_t chandle,
6867c478bd9Sstevel@tonic-gate     rsmapi_controller_attr_t *attr)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	rsm_controller_t *p;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
6917c478bd9Sstevel@tonic-gate 	    "rsm_get_controller_attr: enter\n"));
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	if (!chandle) {
6947c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
6957c478bd9Sstevel@tonic-gate 		    "invalid controller handle\n"));
6967c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (!attr) {
7007c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7017c478bd9Sstevel@tonic-gate 		    "invalid attribute pointer\n"));
7027c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	p = (rsm_controller_t *)chandle;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	mutex_lock(&_rsm_lock);
7087c478bd9Sstevel@tonic-gate 	if (p->cntr_refcnt == 0) {
7097c478bd9Sstevel@tonic-gate 		mutex_unlock(&_rsm_lock);
7107c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7117c478bd9Sstevel@tonic-gate 		    "cntr refcnt is 0\n"));
7127c478bd9Sstevel@tonic-gate 		return (RSMERR_CTLR_NOT_PRESENT);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/* copy only the user part of the attr structure */
7167c478bd9Sstevel@tonic-gate 	attr->attr_direct_access_sizes =
7177c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_direct_access_sizes;
7187c478bd9Sstevel@tonic-gate 	attr->attr_atomic_sizes =
7197c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_atomic_sizes;
7207c478bd9Sstevel@tonic-gate 	attr->attr_page_size =
7217c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_page_size;
7227c478bd9Sstevel@tonic-gate 	attr->attr_max_export_segment_size =
7237c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_max_export_segment_size;
7247c478bd9Sstevel@tonic-gate 	attr->attr_tot_export_segment_size =
7257c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_tot_export_segment_size;
7267c478bd9Sstevel@tonic-gate 	attr->attr_max_export_segments =
7277c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_max_export_segments;
7287c478bd9Sstevel@tonic-gate 	attr->attr_max_import_map_size =
7297c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_max_import_map_size;
7307c478bd9Sstevel@tonic-gate 	attr->attr_tot_import_map_size =
7317c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_tot_import_map_size;
7327c478bd9Sstevel@tonic-gate 	attr->attr_max_import_segments =
7337c478bd9Sstevel@tonic-gate 	    p->cntr_attr.attr_max_import_segments;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	mutex_unlock(&_rsm_lock);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
7387c478bd9Sstevel@tonic-gate 	    "rsm_get_controller_attr: exit\n"));
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate  * Create a segment handle for the virtual address range specified
7477c478bd9Sstevel@tonic-gate  * by vaddr and size
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate int
750*7257d1b4Sraf rsm_memseg_export_create(rsmapi_controller_handle_t controller,
7517c478bd9Sstevel@tonic-gate     rsm_memseg_export_handle_t *memseg,
7527c478bd9Sstevel@tonic-gate     void *vaddr,
7537c478bd9Sstevel@tonic-gate     size_t length,
7547c478bd9Sstevel@tonic-gate     uint_t flags)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	rsm_controller_t *chdl = (rsm_controller_t *)controller;
7587c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *p;
7597c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
7607c478bd9Sstevel@tonic-gate 	int e;
7617c478bd9Sstevel@tonic-gate #ifndef	_LP64
7627c478bd9Sstevel@tonic-gate 	int tmpfd;
7637c478bd9Sstevel@tonic-gate #endif
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
7667c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_create: enter\n"));
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	if (!controller) {
7697c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7707c478bd9Sstevel@tonic-gate 		    "invalid controller handle\n"));
7717c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 	if (!memseg) {
7747c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7757c478bd9Sstevel@tonic-gate 		    "invalid segment handle\n"));
7767c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	*memseg = 0;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	/*
7827c478bd9Sstevel@tonic-gate 	 * Check vaddr and size alignment, both must be mmu page size
7837c478bd9Sstevel@tonic-gate 	 * aligned
7847c478bd9Sstevel@tonic-gate 	 */
7857c478bd9Sstevel@tonic-gate 	if (!vaddr) {
7867c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7877c478bd9Sstevel@tonic-gate 		    "invalid arguments\n"));
7887c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	if (!length) {
7927c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
7937c478bd9Sstevel@tonic-gate 		    "invalid arguments\n"));
7947c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_LENGTH);
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (((size_t)vaddr & (PAGESIZE - 1)) ||
7987c478bd9Sstevel@tonic-gate 	    (length & (PAGESIZE - 1))) {
7997c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
8007c478bd9Sstevel@tonic-gate 		    "invalid mem alignment for vaddr or length\n"));
8017c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_MEM_ALIGNMENT);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * The following check does not apply for loopback controller
8067c478bd9Sstevel@tonic-gate 	 * since for the loopback adapter, the attr_max_export_segment_size
8077c478bd9Sstevel@tonic-gate 	 * is always 0.
8087c478bd9Sstevel@tonic-gate 	 */
8097c478bd9Sstevel@tonic-gate 	if (strcasecmp(chdl->cntr_name, LOOPBACK)) {
8107c478bd9Sstevel@tonic-gate 		if (length > chdl->cntr_attr.attr_max_export_segment_size) {
8117c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
8127c478bd9Sstevel@tonic-gate 			    "length exceeds controller limits\n"));
8137c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
8147c478bd9Sstevel@tonic-gate 			    "controller limits %d\n",
8157c478bd9Sstevel@tonic-gate 			    chdl->cntr_attr.attr_max_export_segment_size));
8167c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_LENGTH);
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	p = (rsmseg_handle_t *)malloc(sizeof (*p));
8217c478bd9Sstevel@tonic-gate 	if (p == NULL) {
8227c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
8237c478bd9Sstevel@tonic-gate 		    "not enough memory\n"));
8247c478bd9Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	p->rsmseg_fd = open(DEVRSM, O_RDWR);
8287c478bd9Sstevel@tonic-gate 	if (p->rsmseg_fd < 0) {
8297c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
8307c478bd9Sstevel@tonic-gate 		    "unable to open device /dev/rsm\n"));
8317c478bd9Sstevel@tonic-gate 		free((void *)p);
8327c478bd9Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_RESOURCES);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate #ifndef	_LP64
8367c478bd9Sstevel@tonic-gate 	/*
8377c478bd9Sstevel@tonic-gate 	 * libc can't handle fd's greater than 255,  in order to
8387c478bd9Sstevel@tonic-gate 	 * insure that these values remain available make /dev/rsm
8397c478bd9Sstevel@tonic-gate 	 * fd > 255. Note: not needed for LP64
8407c478bd9Sstevel@tonic-gate 	 */
8417c478bd9Sstevel@tonic-gate 	tmpfd = fcntl(p->rsmseg_fd, F_DUPFD, 256);
8427c478bd9Sstevel@tonic-gate 	e = errno;
8437c478bd9Sstevel@tonic-gate 	if (tmpfd < 0) {
8447c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
8457c478bd9Sstevel@tonic-gate 		    "F_DUPFD failed\n"));
8467c478bd9Sstevel@tonic-gate 	} else {
8477c478bd9Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
8487c478bd9Sstevel@tonic-gate 		p->rsmseg_fd = tmpfd;
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE, ""
8537c478bd9Sstevel@tonic-gate 	    "rsmseg_fd is %d\n", p->rsmseg_fd));
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (fcntl(p->rsmseg_fd, F_SETFD, FD_CLOEXEC) < 0) {
8567c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
8577c478bd9Sstevel@tonic-gate 		    "F_SETFD failed\n"));
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	p->rsmseg_state = EXPORT_CREATE;
8617c478bd9Sstevel@tonic-gate 	p->rsmseg_size = length;
8627c478bd9Sstevel@tonic-gate 	/* increment controller handle */
8637c478bd9Sstevel@tonic-gate 	p->rsmseg_controller = chdl;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/* try to bind user address range */
8667c478bd9Sstevel@tonic-gate 	msg.cnum = chdl->cntr_unit;
8677c478bd9Sstevel@tonic-gate 	msg.cname = chdl->cntr_name;
8687c478bd9Sstevel@tonic-gate 	msg.cname_len = strlen(chdl->cntr_name) +1;
8697c478bd9Sstevel@tonic-gate 	msg.vaddr = vaddr;
8707c478bd9Sstevel@tonic-gate 	msg.len = length;
8717c478bd9Sstevel@tonic-gate 	msg.perm = flags;
8727c478bd9Sstevel@tonic-gate 	msg.off = 0;
8737c478bd9Sstevel@tonic-gate 	e = RSM_IOCTL_BIND;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/* Try to bind */
8767c478bd9Sstevel@tonic-gate 	if (ioctl(p->rsmseg_fd, e, &msg) < 0) {
8777c478bd9Sstevel@tonic-gate 		e = errno;
8787c478bd9Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
8797c478bd9Sstevel@tonic-gate 		free((void *)p);
8807c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
8817c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_BIND failed\n"));
8827c478bd9Sstevel@tonic-gate 		return (e);
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 	/* OK */
8857c478bd9Sstevel@tonic-gate 	p->rsmseg_type = RSM_EXPORT_SEG;
8867c478bd9Sstevel@tonic-gate 	p->rsmseg_vaddr = vaddr;
8877c478bd9Sstevel@tonic-gate 	p->rsmseg_size = length;
8887c478bd9Sstevel@tonic-gate 	p->rsmseg_state = EXPORT_BIND;
8897c478bd9Sstevel@tonic-gate 	p->rsmseg_pollfd_refcnt = 0;
8907c478bd9Sstevel@tonic-gate 	p->rsmseg_rnum = msg.rnum;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	mutex_init(&p->rsmseg_lock, USYNC_THREAD, NULL);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	*memseg = (rsm_memseg_export_handle_t)p;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
8977c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_create: exit\n"));
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate int
903*7257d1b4Sraf rsm_memseg_export_destroy(rsm_memseg_export_handle_t memseg)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
9087c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_destroy: enter\n"));
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	if (!memseg) {
9117c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9127c478bd9Sstevel@tonic-gate 		    "invalid segment handle\n"));
9137c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	seg = (rsmseg_handle_t *)memseg;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
9197c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_pollfd_refcnt) {
9207c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
9217c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9227c478bd9Sstevel@tonic-gate 		    "segment reference count not zero\n"));
9237c478bd9Sstevel@tonic-gate 		return (RSMERR_POLLFD_IN_USE);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 	else
9267c478bd9Sstevel@tonic-gate 		seg->rsmseg_state = EXPORT_BIND;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	(void) close(seg->rsmseg_fd);
9317c478bd9Sstevel@tonic-gate 	mutex_destroy(&seg->rsmseg_lock);
9327c478bd9Sstevel@tonic-gate 	free((void *)seg);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
9357c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_destroy: exit\n"));
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate int
941*7257d1b4Sraf rsm_memseg_export_rebind(rsm_memseg_export_handle_t memseg, void *vaddr,
9427c478bd9Sstevel@tonic-gate     offset_t off, size_t length)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
9457c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
9487c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_rebind: enter\n"));
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	off = off;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (!seg) {
9537c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9547c478bd9Sstevel@tonic-gate 		    "invalid segment handle\n"));
9557c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 	if (!vaddr) {
9587c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9597c478bd9Sstevel@tonic-gate 		    "invalid vaddr\n"));
9607c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	/*
9647c478bd9Sstevel@tonic-gate 	 * Same as bind except it's ok to have elimint in list.
9657c478bd9Sstevel@tonic-gate 	 * Call into driver to remove any existing mappings.
9667c478bd9Sstevel@tonic-gate 	 */
9677c478bd9Sstevel@tonic-gate 	msg.vaddr = vaddr;
9687c478bd9Sstevel@tonic-gate 	msg.len = length;
9697c478bd9Sstevel@tonic-gate 	msg.off = 0;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
9727c478bd9Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_REBIND, &msg) < 0) {
9737c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
9747c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
9757c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_REBIND failed\n"));
9767c478bd9Sstevel@tonic-gate 		return (errno);
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
9827c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_rebind: exit\n"));
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate int
988*7257d1b4Sraf rsm_memseg_export_publish(rsm_memseg_export_handle_t memseg,
9897c478bd9Sstevel@tonic-gate     rsm_memseg_id_t *seg_id,
9907c478bd9Sstevel@tonic-gate     rsmapi_access_entry_t access_list[],
9917c478bd9Sstevel@tonic-gate     uint_t access_list_length)
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
9957c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
9987c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_publish: enter\n"));
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if (seg_id == NULL) {
10017c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10027c478bd9Sstevel@tonic-gate 		    "invalid segment id\n"));
10037c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEGID);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	if (!seg) {
10077c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10087c478bd9Sstevel@tonic-gate 		    "invalid segment handle\n"));
10097c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	if (access_list_length > 0 && !access_list) {
10137c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10147c478bd9Sstevel@tonic-gate 		    "invalid access control list\n"));
10157c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ACL);
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
10197c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state != EXPORT_BIND) {
10207c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
10217c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10227c478bd9Sstevel@tonic-gate 		    "invalid segment state\n"));
10237c478bd9Sstevel@tonic-gate 		return (RSMERR_SEG_ALREADY_PUBLISHED);
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/*
10277c478bd9Sstevel@tonic-gate 	 * seg id < RSM_DLPI_END and in the RSM_USER_APP_ID range
10287c478bd9Sstevel@tonic-gate 	 * are reserved for internal use.
10297c478bd9Sstevel@tonic-gate 	 */
10307c478bd9Sstevel@tonic-gate 	if ((*seg_id > 0) &&
10317c478bd9Sstevel@tonic-gate 	    ((*seg_id <= RSM_DLPI_ID_END) ||
10327c478bd9Sstevel@tonic-gate 	    BETWEEN (*seg_id, RSM_USER_APP_ID_BASE, RSM_USER_APP_ID_END))) {
10337c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
10347c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10357c478bd9Sstevel@tonic-gate 		    "invalid segment id\n"));
10367c478bd9Sstevel@tonic-gate 		return (RSMERR_RESERVED_SEGID);
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	msg.key = *seg_id;
10407c478bd9Sstevel@tonic-gate 	msg.acl = access_list;
10417c478bd9Sstevel@tonic-gate 	msg.acl_len = access_list_length;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_PUBLISH, &msg) < 0) {
10447c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
10457c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10467c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_PUBLISH failed\n"));
10477c478bd9Sstevel@tonic-gate 		return (errno);
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	seg->rsmseg_keyid = msg.key;
10517c478bd9Sstevel@tonic-gate 	seg->rsmseg_state = EXPORT_PUBLISH;
10527c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (*seg_id == 0)
10557c478bd9Sstevel@tonic-gate 		*seg_id = msg.key;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
10587c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_publish: exit\n"));
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate int
1065*7257d1b4Sraf rsm_memseg_export_unpublish(rsm_memseg_export_handle_t memseg)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
10687c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
10717c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_unpublish: enter\n"));
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	if (!seg) {
10747c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10757c478bd9Sstevel@tonic-gate 		    "invalid arguments\n"));
10767c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
10807c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state != EXPORT_PUBLISH) {
10817c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
10827c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10837c478bd9Sstevel@tonic-gate 		    "segment not published %d\n",
10847c478bd9Sstevel@tonic-gate 		    seg->rsmseg_keyid));
10857c478bd9Sstevel@tonic-gate 		return (RSMERR_SEG_NOT_PUBLISHED);
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	msg.key = seg->rsmseg_keyid;
10897c478bd9Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_UNPUBLISH, &msg) < 0) {
10907c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
10917c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
10927c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_UNPUBLISH failed\n"));
10937c478bd9Sstevel@tonic-gate 		return (errno);
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	seg->rsmseg_state = EXPORT_BIND;
10977c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
11007c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_unpublish: exit\n"));
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate int
1107*7257d1b4Sraf rsm_memseg_export_republish(rsm_memseg_export_handle_t memseg,
11087c478bd9Sstevel@tonic-gate     rsmapi_access_entry_t access_list[],
11097c478bd9Sstevel@tonic-gate     uint_t access_list_length)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
11127c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
11157c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_republish: enter\n"));
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	if (!seg) {
11187c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11197c478bd9Sstevel@tonic-gate 		    "invalid segment or segment state\n"));
11207c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
11247c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state != EXPORT_PUBLISH) {
11257c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11267c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11277c478bd9Sstevel@tonic-gate 		    "segment not published\n"));
11287c478bd9Sstevel@tonic-gate 		return (RSMERR_SEG_NOT_PUBLISHED);
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	if (access_list_length > 0 && !access_list) {
11327c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11337c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11347c478bd9Sstevel@tonic-gate 		    "invalid access control list\n"));
11357c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ACL);
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	msg.key = seg->rsmseg_keyid;
11397c478bd9Sstevel@tonic-gate 	msg.acl = access_list;
11407c478bd9Sstevel@tonic-gate 	msg.acl_len = access_list_length;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_REPUBLISH, &msg) < 0) {
11437c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
11447c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
11457c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_REPUBLISH failed\n"));
11467c478bd9Sstevel@tonic-gate 		return (errno);
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_DEBUG_VERBOSE,
11517c478bd9Sstevel@tonic-gate 	    "rsm_memseg_export_republish: exit\n"));
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/*
11587c478bd9Sstevel@tonic-gate 	 * import side memory segment operations:
11597c478bd9Sstevel@tonic-gate 	 */
11607c478bd9Sstevel@tonic-gate int
1161*7257d1b4Sraf rsm_memseg_import_connect(rsmapi_controller_handle_t controller,
11627c478bd9Sstevel@tonic-gate     rsm_node_id_t node_id,
11637c478bd9Sstevel@tonic-gate     rsm_memseg_id_t segment_id,
11647c478bd9Sstevel@tonic-gate     rsm_permission_t perm,
11657c478bd9Sstevel@tonic-gate     rsm_memseg_import_handle_t *im_memseg)
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
11687c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *p;
11697c478bd9Sstevel@tonic-gate 	rsm_controller_t *cntr = (rsm_controller_t *)controller;
11707c478bd9Sstevel@tonic-gate #ifndef	_LP64		/* added for fd > 255 fix */
11717c478bd9Sstevel@tonic-gate 	int tmpfd;
11727c478bd9Sstevel@tonic-gate #endif
11737c478bd9Sstevel@tonic-gate 	int e;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
11767c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_connect: enter\n"));
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if (!cntr) {
11797c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
11807c478bd9Sstevel@tonic-gate 		    "invalid controller handle\n"));
11817c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	*im_memseg = 0;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	p = (rsmseg_handle_t *)malloc(sizeof (*p));
11877c478bd9Sstevel@tonic-gate 	if (!p) {
11887c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
11897c478bd9Sstevel@tonic-gate 		    "not enough memory\n"));
11907c478bd9Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if (perm & ~RSM_PERM_RDWR) {
11947c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
11957c478bd9Sstevel@tonic-gate 		    "invalid permissions\n"));
11967c478bd9Sstevel@tonic-gate 		return (RSMERR_PERM_DENIED);
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	/*
12007c478bd9Sstevel@tonic-gate 	 * Get size, va from driver
12017c478bd9Sstevel@tonic-gate 	 */
12027c478bd9Sstevel@tonic-gate 	msg.cnum = cntr->cntr_unit;
12037c478bd9Sstevel@tonic-gate 	msg.cname = cntr->cntr_name;
12047c478bd9Sstevel@tonic-gate 	msg.cname_len = strlen(cntr->cntr_name) +1;
12057c478bd9Sstevel@tonic-gate 	msg.nodeid = node_id;
12067c478bd9Sstevel@tonic-gate 	msg.key = segment_id;
12077c478bd9Sstevel@tonic-gate 	msg.perm = perm;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	p->rsmseg_fd = open(DEVRSM, O_RDWR);
12107c478bd9Sstevel@tonic-gate 	if (p->rsmseg_fd < 0) {
12117c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12127c478bd9Sstevel@tonic-gate 		    "unable to open /dev/rsm"));
12137c478bd9Sstevel@tonic-gate 		free((void *)p);
12147c478bd9Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_RESOURCES);
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate #ifndef	_LP64
12187c478bd9Sstevel@tonic-gate 	/*
12197c478bd9Sstevel@tonic-gate 	 * libc can't handle fd's greater than 255,  in order to
12207c478bd9Sstevel@tonic-gate 	 * insure that these values remain available make /dev/rsm
12217c478bd9Sstevel@tonic-gate 	 * fd > 255. Note: not needed for LP64
12227c478bd9Sstevel@tonic-gate 	 */
12237c478bd9Sstevel@tonic-gate 	tmpfd = fcntl(p->rsmseg_fd, F_DUPFD, 256); /* make fd > 255 */
12247c478bd9Sstevel@tonic-gate 	e = errno;
12257c478bd9Sstevel@tonic-gate 	if (tmpfd < 0) {
12267c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12277c478bd9Sstevel@tonic-gate 		    "F_DUPFD failed\n"));
12287c478bd9Sstevel@tonic-gate 	} else {
12297c478bd9Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
12307c478bd9Sstevel@tonic-gate 		p->rsmseg_fd = tmpfd;
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
12357c478bd9Sstevel@tonic-gate 	    "rsmseg_fd is %d\n", p->rsmseg_fd));
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	if (fcntl(p->rsmseg_fd, F_SETFD, FD_CLOEXEC) < 0) {
12387c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12397c478bd9Sstevel@tonic-gate 		    "F_SETFD failed\n"));
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 	if (ioctl(p->rsmseg_fd, RSM_IOCTL_CONNECT, &msg) < 0) {
12427c478bd9Sstevel@tonic-gate 		e = errno;
12437c478bd9Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
12447c478bd9Sstevel@tonic-gate 		free((void *)p);
12457c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
12467c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_CONNECT failed\n"));
12477c478bd9Sstevel@tonic-gate 		return (e);
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/*
12517c478bd9Sstevel@tonic-gate 	 * We connected ok.
12527c478bd9Sstevel@tonic-gate 	 */
12537c478bd9Sstevel@tonic-gate 	p->rsmseg_type = RSM_IMPORT_SEG;
12547c478bd9Sstevel@tonic-gate 	p->rsmseg_state = IMPORT_CONNECT;
12557c478bd9Sstevel@tonic-gate 	p->rsmseg_keyid = segment_id;
12567c478bd9Sstevel@tonic-gate 	p->rsmseg_nodeid = node_id;
12577c478bd9Sstevel@tonic-gate 	p->rsmseg_size = msg.len;
12587c478bd9Sstevel@tonic-gate 	p->rsmseg_perm = perm;
12597c478bd9Sstevel@tonic-gate 	p->rsmseg_controller = cntr;
12607c478bd9Sstevel@tonic-gate 	p->rsmseg_barrier = NULL;
12617c478bd9Sstevel@tonic-gate 	p->rsmseg_barmode = RSM_BARRIER_MODE_IMPLICIT;
12627c478bd9Sstevel@tonic-gate 	p->rsmseg_bar = (bar_va ? bar_va + msg.off : &bar_fixed);
12637c478bd9Sstevel@tonic-gate 	p->rsmseg_gnum = msg.gnum;
12647c478bd9Sstevel@tonic-gate 	p->rsmseg_pollfd_refcnt = 0;
12657c478bd9Sstevel@tonic-gate 	p->rsmseg_maplen = 0;    /* initialized, set in import_map */
12667c478bd9Sstevel@tonic-gate 	p->rsmseg_mapoffset = 0;
12677c478bd9Sstevel@tonic-gate 	p->rsmseg_flags = 0;
12687c478bd9Sstevel@tonic-gate 	p->rsmseg_rnum = msg.rnum;
12697c478bd9Sstevel@tonic-gate 	mutex_init(&p->rsmseg_lock, USYNC_THREAD, NULL);
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	p->rsmseg_ops = cntr->cntr_segops;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	/*
12747c478bd9Sstevel@tonic-gate 	 * XXX: Based on permission and controller direct_access attribute
12757c478bd9Sstevel@tonic-gate 	 * we fix the segment ops vector
12767c478bd9Sstevel@tonic-gate 	 */
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	p->rsmseg_vaddr = 0; /* defer mapping till using maps or trys to rw */
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	*im_memseg = (rsm_memseg_import_handle_t)p;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	e =  p->rsmseg_ops->rsm_memseg_import_connect(controller,
12837c478bd9Sstevel@tonic-gate 	    node_id, segment_id, perm, im_memseg);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	if (e != RSM_SUCCESS) {
12867c478bd9Sstevel@tonic-gate 		(void) close(p->rsmseg_fd);
12877c478bd9Sstevel@tonic-gate 		mutex_destroy(&p->rsmseg_lock);
12887c478bd9Sstevel@tonic-gate 		free((void *)p);
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
12927c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_connect: exit\n"));
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	return (e);
12957c478bd9Sstevel@tonic-gate }
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate int
1299*7257d1b4Sraf rsm_memseg_import_disconnect(rsm_memseg_import_handle_t im_memseg)
13007c478bd9Sstevel@tonic-gate {
13017c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
13027c478bd9Sstevel@tonic-gate 	int e;
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
13057c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_disconnect: enter\n"));
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	if (!seg) {
13087c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13097c478bd9Sstevel@tonic-gate 		    "invalid segment handle\n"));
13107c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state != IMPORT_CONNECT) {
13147c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_flags & RSM_IMPLICIT_MAP) {
13157c478bd9Sstevel@tonic-gate 			e = rsm_memseg_import_unmap(im_memseg);
13167c478bd9Sstevel@tonic-gate 			if (e != RSM_SUCCESS) {
13177c478bd9Sstevel@tonic-gate 				DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13187c478bd9Sstevel@tonic-gate 				    "unmap failure\n"));
13197c478bd9Sstevel@tonic-gate 				return (e);
13207c478bd9Sstevel@tonic-gate 			}
13217c478bd9Sstevel@tonic-gate 		} else {
13227c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13237c478bd9Sstevel@tonic-gate 			    "segment busy\n"));
13247c478bd9Sstevel@tonic-gate 			return (RSMERR_SEG_STILL_MAPPED);
13257c478bd9Sstevel@tonic-gate 		}
13267c478bd9Sstevel@tonic-gate 	}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
13297c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_pollfd_refcnt) {
13307c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
13317c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_EXPORT, RSM_ERR,
13327c478bd9Sstevel@tonic-gate 		    "segment reference count not zero\n"));
13337c478bd9Sstevel@tonic-gate 		return (RSMERR_POLLFD_IN_USE);
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	e =  seg->rsmseg_ops->rsm_memseg_import_disconnect(im_memseg);
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
13407c478bd9Sstevel@tonic-gate 		(void) close(seg->rsmseg_fd);
13417c478bd9Sstevel@tonic-gate 		mutex_destroy(&seg->rsmseg_lock);
13427c478bd9Sstevel@tonic-gate 		free((void *)seg);
13437c478bd9Sstevel@tonic-gate 	}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
13467c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_disconnect: exit\n"));
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	return (e);
13497c478bd9Sstevel@tonic-gate }
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate /*
13527c478bd9Sstevel@tonic-gate  * import side memory segment operations (read access functions):
13537c478bd9Sstevel@tonic-gate  */
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate static int
13567c478bd9Sstevel@tonic-gate __rsm_import_verify_access(rsmseg_handle_t *seg,
13577c478bd9Sstevel@tonic-gate     off_t offset,
13587c478bd9Sstevel@tonic-gate     caddr_t datap,
13597c478bd9Sstevel@tonic-gate     size_t len,
13607c478bd9Sstevel@tonic-gate     rsm_permission_t perm,
13617c478bd9Sstevel@tonic-gate     rsm_access_size_t das)
13627c478bd9Sstevel@tonic-gate {
13637c478bd9Sstevel@tonic-gate 	int	error;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
13667c478bd9Sstevel@tonic-gate 	    " __rsm_import_verify_access: enter\n"));
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	if (!seg) {
13697c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13707c478bd9Sstevel@tonic-gate 		    "invalid segment handle\n"));
13717c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 	if (!datap) {
13747c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13757c478bd9Sstevel@tonic-gate 		    "invalid data pointer\n"));
13767c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	/*
13807c478bd9Sstevel@tonic-gate 	 * Check alignment of pointer
13817c478bd9Sstevel@tonic-gate 	 */
13827c478bd9Sstevel@tonic-gate 	if ((uintptr_t)datap & (das - 1)) {
13837c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13847c478bd9Sstevel@tonic-gate 		    "invalid alignment of data pointer\n"));
13857c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_MEM_ALIGNMENT);
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	if (offset & (das - 1)) {
13897c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13907c478bd9Sstevel@tonic-gate 		    "invalid offset\n"));
13917c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_MEM_ALIGNMENT);
13927c478bd9Sstevel@tonic-gate 	}
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	/* make sure that the import seg is connected */
13957c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state != IMPORT_CONNECT &&
13967c478bd9Sstevel@tonic-gate 	    seg->rsmseg_state != IMPORT_MAP) {
13977c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
13987c478bd9Sstevel@tonic-gate 		    "incorrect segment state\n"));
13997c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	/* do an implicit map if required */
14037c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_CONNECT) {
14047c478bd9Sstevel@tonic-gate 		error = __rsm_import_implicit_map(seg, RSM_IOTYPE_PUTGET);
14057c478bd9Sstevel@tonic-gate 		if (error != RSM_SUCCESS) {
14067c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14077c478bd9Sstevel@tonic-gate 			    "implicit map failure\n"));
14087c478bd9Sstevel@tonic-gate 			return (error);
14097c478bd9Sstevel@tonic-gate 		}
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	if ((seg->rsmseg_perm & perm) != perm) {
14137c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14147c478bd9Sstevel@tonic-gate 		    "invalid permissions\n"));
14157c478bd9Sstevel@tonic-gate 		return (RSMERR_PERM_DENIED);
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_MAP) {
14197c478bd9Sstevel@tonic-gate 		if ((offset < seg->rsmseg_mapoffset) ||
14207c478bd9Sstevel@tonic-gate 		    (offset + len > seg->rsmseg_mapoffset +
14217c478bd9Sstevel@tonic-gate 		    seg->rsmseg_maplen)) {
14227c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14237c478bd9Sstevel@tonic-gate 			    "incorrect offset+length\n"));
14247c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_OFFSET);
14257c478bd9Sstevel@tonic-gate 		}
14267c478bd9Sstevel@tonic-gate 	} else { /* IMPORT_CONNECT */
14277c478bd9Sstevel@tonic-gate 		if ((len + offset) > seg->rsmseg_size) {
14287c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14297c478bd9Sstevel@tonic-gate 			    "incorrect offset+length\n"));
14307c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_LENGTH);
14317c478bd9Sstevel@tonic-gate 		}
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if ((seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) &&
14357c478bd9Sstevel@tonic-gate 	    (seg->rsmseg_barrier == NULL)) {
14367c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14377c478bd9Sstevel@tonic-gate 		    "invalid barrier\n"));
14387c478bd9Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
14397c478bd9Sstevel@tonic-gate 	}
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
14427c478bd9Sstevel@tonic-gate 	    " __rsm_import_verify_access: exit\n"));
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate static int
14487c478bd9Sstevel@tonic-gate __rsm_import_implicit_map(rsmseg_handle_t *seg, int iotype)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	caddr_t va;
14517c478bd9Sstevel@tonic-gate 	int flag = MAP_SHARED;
14527c478bd9Sstevel@tonic-gate 	int prot = PROT_READ|PROT_WRITE;
14537c478bd9Sstevel@tonic-gate 	int mapping_reqd = 0;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
14567c478bd9Sstevel@tonic-gate 	    " __rsm_import_implicit_map: enter\n"));
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	if (iotype == RSM_IOTYPE_PUTGET)
14597c478bd9Sstevel@tonic-gate 		mapping_reqd = seg->rsmseg_controller->cntr_lib_attr->
14607c478bd9Sstevel@tonic-gate 		    rsm_putget_map_reqd;
14617c478bd9Sstevel@tonic-gate 	else if (iotype == RSM_IOTYPE_SCATGATH)
14627c478bd9Sstevel@tonic-gate 		mapping_reqd = seg->rsmseg_controller->cntr_lib_attr->
14637c478bd9Sstevel@tonic-gate 		    rsm_scatgath_map_reqd;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if (mapping_reqd) {
14677c478bd9Sstevel@tonic-gate 		va = mmap(NULL, seg->rsmseg_size, prot,
14687c478bd9Sstevel@tonic-gate 		    flag, seg->rsmseg_fd, 0);
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 		if (va == MAP_FAILED) {
14717c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
14727c478bd9Sstevel@tonic-gate 			    "implicit map failed\n"));
14737c478bd9Sstevel@tonic-gate 			if (errno == ENOMEM || errno == ENXIO ||
14747c478bd9Sstevel@tonic-gate 			    errno == EOVERFLOW)
14757c478bd9Sstevel@tonic-gate 				return (RSMERR_BAD_LENGTH);
14767c478bd9Sstevel@tonic-gate 			else if (errno == ENODEV)
14777c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
14787c478bd9Sstevel@tonic-gate 			else if (errno == EAGAIN)
14797c478bd9Sstevel@tonic-gate 				return (RSMERR_INSUFFICIENT_RESOURCES);
14807c478bd9Sstevel@tonic-gate 			else if (errno == ENOTSUP)
14817c478bd9Sstevel@tonic-gate 				return (RSMERR_MAP_FAILED);
14827c478bd9Sstevel@tonic-gate 			else if (errno == EACCES)
14837c478bd9Sstevel@tonic-gate 				return (RSMERR_BAD_PERMS);
14847c478bd9Sstevel@tonic-gate 			else
14857c478bd9Sstevel@tonic-gate 				return (RSMERR_MAP_FAILED);
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 		seg->rsmseg_vaddr = va;
14887c478bd9Sstevel@tonic-gate 		seg->rsmseg_maplen = seg->rsmseg_size;
14897c478bd9Sstevel@tonic-gate 		seg->rsmseg_mapoffset = 0;
14907c478bd9Sstevel@tonic-gate 		seg->rsmseg_state = IMPORT_MAP;
14917c478bd9Sstevel@tonic-gate 		seg->rsmseg_flags |= RSM_IMPLICIT_MAP;
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
14957c478bd9Sstevel@tonic-gate 	    " __rsm_import_implicit_map: exit\n"));
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate int
1501*7257d1b4Sraf rsm_memseg_import_get8(rsm_memseg_import_handle_t im_memseg,
15027c478bd9Sstevel@tonic-gate     off_t offset,
15037c478bd9Sstevel@tonic-gate     uint8_t *datap,
15047c478bd9Sstevel@tonic-gate     ulong_t rep_cnt)
15057c478bd9Sstevel@tonic-gate {
15067c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
15077c478bd9Sstevel@tonic-gate 	int e;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15107c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get8: enter\n"));
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt,
15137c478bd9Sstevel@tonic-gate 	    RSM_PERM_READ,
15147c478bd9Sstevel@tonic-gate 	    RSM_DAS8);
15157c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
15167c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
15177c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
15207c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
15217c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
15227c478bd9Sstevel@tonic-gate 		}
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get8(im_memseg, offset, datap,
15257c478bd9Sstevel@tonic-gate 		    rep_cnt, 0);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
15287c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
15297c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
15307c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
15317c478bd9Sstevel@tonic-gate 			}
15327c478bd9Sstevel@tonic-gate 		}
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15367c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get8: exit\n"));
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	return (e);
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate int
1542*7257d1b4Sraf rsm_memseg_import_get16(rsm_memseg_import_handle_t im_memseg,
15437c478bd9Sstevel@tonic-gate     off_t offset,
15447c478bd9Sstevel@tonic-gate     uint16_t *datap,
15457c478bd9Sstevel@tonic-gate     ulong_t rep_cnt)
15467c478bd9Sstevel@tonic-gate {
15477c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
15487c478bd9Sstevel@tonic-gate 	int e;
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15517c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get16: enter\n"));
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*2,
15547c478bd9Sstevel@tonic-gate 	    RSM_PERM_READ,
15557c478bd9Sstevel@tonic-gate 	    RSM_DAS16);
15567c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
15577c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
15587c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
15617c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
15627c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
15637c478bd9Sstevel@tonic-gate 		}
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get16(im_memseg, offset, datap,
15667c478bd9Sstevel@tonic-gate 		    rep_cnt, 0);
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
15697c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
15707c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
15717c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
15727c478bd9Sstevel@tonic-gate 			}
15737c478bd9Sstevel@tonic-gate 		}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15787c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get16: exit\n"));
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	return (e);
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate int
1584*7257d1b4Sraf rsm_memseg_import_get32(rsm_memseg_import_handle_t im_memseg,
15857c478bd9Sstevel@tonic-gate     off_t offset,
15867c478bd9Sstevel@tonic-gate     uint32_t *datap,
15877c478bd9Sstevel@tonic-gate     ulong_t rep_cnt)
15887c478bd9Sstevel@tonic-gate {
15897c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
15907c478bd9Sstevel@tonic-gate 	int e;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
15937c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get32: enter\n"));
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*4,
15967c478bd9Sstevel@tonic-gate 	    RSM_PERM_READ,
15977c478bd9Sstevel@tonic-gate 	    RSM_DAS32);
15987c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
15997c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
16007c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16037c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
16047c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
16057c478bd9Sstevel@tonic-gate 		}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get32(im_memseg, offset, datap,
16087c478bd9Sstevel@tonic-gate 		    rep_cnt, 0);
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16117c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
16127c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
16137c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
16147c478bd9Sstevel@tonic-gate 			}
16157c478bd9Sstevel@tonic-gate 		}
16167c478bd9Sstevel@tonic-gate 	}
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16197c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get32: exit\n"));
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	return (e);
16227c478bd9Sstevel@tonic-gate }
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate int
1625*7257d1b4Sraf rsm_memseg_import_get64(rsm_memseg_import_handle_t im_memseg,
16267c478bd9Sstevel@tonic-gate     off_t offset,
16277c478bd9Sstevel@tonic-gate     uint64_t *datap,
16287c478bd9Sstevel@tonic-gate     ulong_t rep_cnt)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
16317c478bd9Sstevel@tonic-gate 	int e;
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16347c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get64: enter\n"));
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*8,
16377c478bd9Sstevel@tonic-gate 	    RSM_PERM_READ,
16387c478bd9Sstevel@tonic-gate 	    RSM_DAS64);
16397c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
16407c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
16417c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16447c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
16457c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
16467c478bd9Sstevel@tonic-gate 		}
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get64(im_memseg, offset, datap,
16497c478bd9Sstevel@tonic-gate 		    rep_cnt, 0);
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16527c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
16537c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
16547c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
16557c478bd9Sstevel@tonic-gate 			}
16567c478bd9Sstevel@tonic-gate 		}
16577c478bd9Sstevel@tonic-gate 	}
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16607c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get64: exit\n"));
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	return (e);
16637c478bd9Sstevel@tonic-gate }
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate int
1666*7257d1b4Sraf rsm_memseg_import_get(rsm_memseg_import_handle_t im_memseg,
16677c478bd9Sstevel@tonic-gate     off_t offset,
16687c478bd9Sstevel@tonic-gate     void *dst_addr,
16697c478bd9Sstevel@tonic-gate     size_t length)
16707c478bd9Sstevel@tonic-gate {
16717c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
16727c478bd9Sstevel@tonic-gate 	int e;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
16757c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get: enter\n"));
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)dst_addr, length,
16787c478bd9Sstevel@tonic-gate 	    RSM_PERM_READ,
16797c478bd9Sstevel@tonic-gate 	    RSM_DAS8);
16807c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
16817c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
16827c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16857c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
16867c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
16877c478bd9Sstevel@tonic-gate 		}
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_get(im_memseg, offset, dst_addr,
16907c478bd9Sstevel@tonic-gate 		    length);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
16937c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
16947c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
16957c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
16967c478bd9Sstevel@tonic-gate 			}
16977c478bd9Sstevel@tonic-gate 		}
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
17017c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get: exit\n"));
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	return (e);
17047c478bd9Sstevel@tonic-gate }
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate int
1708*7257d1b4Sraf rsm_memseg_import_getv(rsm_scat_gath_t *sg_io)
17097c478bd9Sstevel@tonic-gate {
17107c478bd9Sstevel@tonic-gate 	rsm_controller_t *cntrl;
17117c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg;
17127c478bd9Sstevel@tonic-gate 	uint_t save_sg_io_flags;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	int e;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
17177c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_getv: enter\n"));
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	if (sg_io == NULL) {
17207c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17217c478bd9Sstevel@tonic-gate 		    "invalid sg_io structure\n"));
17227c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SGIO);
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	seg = (rsmseg_handle_t *)sg_io->remote_handle;
17267c478bd9Sstevel@tonic-gate 	if (seg == NULL) {
17277c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17287c478bd9Sstevel@tonic-gate 		    "invalid remote segment handle in sg_io\n"));
17297c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
17307c478bd9Sstevel@tonic-gate 	}
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	cntrl = (rsm_controller_t *)seg->rsmseg_controller;
17337c478bd9Sstevel@tonic-gate 	if (cntrl == NULL) {
17347c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17357c478bd9Sstevel@tonic-gate 		    "invalid controller handle\n"));
17367c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
17377c478bd9Sstevel@tonic-gate 	}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	if ((sg_io->io_request_count > RSM_MAX_SGIOREQS) ||
17407c478bd9Sstevel@tonic-gate 	    (sg_io->io_request_count == 0)) {
17417c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17427c478bd9Sstevel@tonic-gate 		    "io_request_count value incorrect\n"));
17437c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SGIO);
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_CONNECT) {
17477c478bd9Sstevel@tonic-gate 		e = __rsm_import_implicit_map(seg, RSM_IOTYPE_SCATGATH);
17487c478bd9Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
17497c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
17507c478bd9Sstevel@tonic-gate 			    "implicit map failure\n"));
17517c478bd9Sstevel@tonic-gate 			return (e);
17527c478bd9Sstevel@tonic-gate 		}
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	/*
17567c478bd9Sstevel@tonic-gate 	 * Copy the flags field of the sg_io structure in a local
17577c478bd9Sstevel@tonic-gate 	 * variable.
17587c478bd9Sstevel@tonic-gate 	 * This is required since the flags field can be
17597c478bd9Sstevel@tonic-gate 	 * changed by the plugin library routine to indicate that
17607c478bd9Sstevel@tonic-gate 	 * the signal post was done.
17617c478bd9Sstevel@tonic-gate 	 * This change in the flags field of the sg_io structure
17627c478bd9Sstevel@tonic-gate 	 * should not be reflected to the user. Hence once the flags
17637c478bd9Sstevel@tonic-gate 	 * field has been used for the purpose of determining whether
17647c478bd9Sstevel@tonic-gate 	 * the plugin executed a signal post, it must be restored to
17657c478bd9Sstevel@tonic-gate 	 * its original value which is stored in the local variable.
17667c478bd9Sstevel@tonic-gate 	 */
17677c478bd9Sstevel@tonic-gate 	save_sg_io_flags = sg_io->flags;
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	e = cntrl->cntr_segops->rsm_memseg_import_getv(sg_io);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	/*
17727c478bd9Sstevel@tonic-gate 	 * At this point, if an implicit signal post was requested by
17737c478bd9Sstevel@tonic-gate 	 * the user, there could be two possibilities that arise:
17747c478bd9Sstevel@tonic-gate 	 * 1. the plugin routine has already executed the implicit
17757c478bd9Sstevel@tonic-gate 	 *    signal post either successfully or unsuccessfully
17767c478bd9Sstevel@tonic-gate 	 * 2. the plugin does not have the capability of doing an
17777c478bd9Sstevel@tonic-gate 	 *    implicit signal post and hence the signal post needs
17787c478bd9Sstevel@tonic-gate 	 *    to be done here.
17797c478bd9Sstevel@tonic-gate 	 * The above two cases can be idenfied by the flags
17807c478bd9Sstevel@tonic-gate 	 * field within the sg_io structure as follows:
17817c478bd9Sstevel@tonic-gate 	 * In case 1, the RSM_IMPLICIT_SIGPOST bit is reset to 0 by the
17827c478bd9Sstevel@tonic-gate 	 * plugin, indicating that the signal post was done.
17837c478bd9Sstevel@tonic-gate 	 * In case 2, the bit remains set to a 1 as originally given
17847c478bd9Sstevel@tonic-gate 	 * by the user, and hence a signal post needs to be done here.
17857c478bd9Sstevel@tonic-gate 	 */
17867c478bd9Sstevel@tonic-gate 	if (sg_io->flags & RSM_IMPLICIT_SIGPOST &&
17877c478bd9Sstevel@tonic-gate 	    e == RSM_SUCCESS) {
17887c478bd9Sstevel@tonic-gate 		/* Do the implicit signal post */
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 		/*
17917c478bd9Sstevel@tonic-gate 		 * The value of the second argument to this call
17927c478bd9Sstevel@tonic-gate 		 * depends on the value of the sg_io->flags field.
17937c478bd9Sstevel@tonic-gate 		 * If the RSM_SIGPOST_NO_ACCUMULATE flag has been
17947c478bd9Sstevel@tonic-gate 		 * ored into the sg_io->flags field, this indicates
17957c478bd9Sstevel@tonic-gate 		 * that the rsm_intr_signal_post is to be done with
17967c478bd9Sstevel@tonic-gate 		 * the flags argument set to RSM_SIGPOST_NO_ACCUMULATE
17977c478bd9Sstevel@tonic-gate 		 * Else, the flags argument is set to 0. These
17987c478bd9Sstevel@tonic-gate 		 * semantics can be achieved simply by masking off
17997c478bd9Sstevel@tonic-gate 		 * all other bits in the sg_io->flags field except the
18007c478bd9Sstevel@tonic-gate 		 * RSM_SIGPOST_NO_ACCUMULATE bit and using the result
18017c478bd9Sstevel@tonic-gate 		 * as the flags argument for the rsm_intr_signal_post.
18027c478bd9Sstevel@tonic-gate 		 */
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 		int sigpost_flags = sg_io->flags & RSM_SIGPOST_NO_ACCUMULATE;
18057c478bd9Sstevel@tonic-gate 		e = rsm_intr_signal_post(seg, sigpost_flags);
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	/* Restore the flags field within the users scatter gather structure */
18097c478bd9Sstevel@tonic-gate 	sg_io->flags = save_sg_io_flags;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
18127c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_getv: exit\n"));
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	return (e);
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate }
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	/*
18197c478bd9Sstevel@tonic-gate 	 * import side memory segment operations (write access functions):
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate int
1823*7257d1b4Sraf rsm_memseg_import_put8(rsm_memseg_import_handle_t im_memseg,
18247c478bd9Sstevel@tonic-gate     off_t offset,
18257c478bd9Sstevel@tonic-gate     uint8_t *datap,
18267c478bd9Sstevel@tonic-gate     ulong_t rep_cnt)
18277c478bd9Sstevel@tonic-gate {
18287c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
18297c478bd9Sstevel@tonic-gate 	int e;
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
18327c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put8: enter\n"));
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/* addr of data will always pass the alignment check, avoids	*/
18357c478bd9Sstevel@tonic-gate 	/* need for a special case in verify_access for PUTs		*/
18367c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt,
18377c478bd9Sstevel@tonic-gate 	    RSM_PERM_WRITE,
18387c478bd9Sstevel@tonic-gate 	    RSM_DAS8);
18397c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
18407c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
18417c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
18447c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
18457c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
18467c478bd9Sstevel@tonic-gate 		}
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put8(im_memseg, offset, datap,
18497c478bd9Sstevel@tonic-gate 		    rep_cnt, 0);
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
18527c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
18537c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
18547c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
18557c478bd9Sstevel@tonic-gate 			}
18567c478bd9Sstevel@tonic-gate 		}
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
18607c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put8: exit\n"));
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	return (e);
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate int
1866*7257d1b4Sraf rsm_memseg_import_put16(rsm_memseg_import_handle_t im_memseg,
18677c478bd9Sstevel@tonic-gate     off_t offset,
18687c478bd9Sstevel@tonic-gate     uint16_t *datap,
18697c478bd9Sstevel@tonic-gate     ulong_t rep_cnt)
18707c478bd9Sstevel@tonic-gate {
18717c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
18727c478bd9Sstevel@tonic-gate 	int e;
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
18757c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put16: enter\n"));
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	/* addr of data will always pass the alignment check, avoids	*/
18787c478bd9Sstevel@tonic-gate 	/* need for a special case in verify_access for PUTs		*/
18797c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*2,
18807c478bd9Sstevel@tonic-gate 	    RSM_PERM_WRITE,
18817c478bd9Sstevel@tonic-gate 	    RSM_DAS16);
18827c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
18837c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
18847c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
18877c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
18887c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
18897c478bd9Sstevel@tonic-gate 		}
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put16(im_memseg, offset, datap,
18927c478bd9Sstevel@tonic-gate 		    rep_cnt, 0);
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
18957c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
18967c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
18977c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
18987c478bd9Sstevel@tonic-gate 			}
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	}
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19047c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put16: exit\n"));
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	return (e);
19077c478bd9Sstevel@tonic-gate }
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate int
1910*7257d1b4Sraf rsm_memseg_import_put32(rsm_memseg_import_handle_t im_memseg,
19117c478bd9Sstevel@tonic-gate     off_t offset,
19127c478bd9Sstevel@tonic-gate     uint32_t *datap,
19137c478bd9Sstevel@tonic-gate     ulong_t rep_cnt)
19147c478bd9Sstevel@tonic-gate {
19157c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
19167c478bd9Sstevel@tonic-gate 	int e;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19197c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put32: enter\n"));
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	/* addr of data will always pass the alignment check, avoids	*/
19227c478bd9Sstevel@tonic-gate 	/* need for a special case in verify_access for PUTs		*/
19237c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*4,
19247c478bd9Sstevel@tonic-gate 	    RSM_PERM_WRITE,
19257c478bd9Sstevel@tonic-gate 	    RSM_DAS32);
19267c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
19277c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
19287c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19317c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
19327c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
19337c478bd9Sstevel@tonic-gate 		}
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put32(im_memseg, offset, datap,
19367c478bd9Sstevel@tonic-gate 		    rep_cnt, 0);
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19397c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
19407c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
19417c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
19427c478bd9Sstevel@tonic-gate 			}
19437c478bd9Sstevel@tonic-gate 		}
19447c478bd9Sstevel@tonic-gate 	}
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19477c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put32: exit\n"));
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	return (e);
19507c478bd9Sstevel@tonic-gate }
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate int
1953*7257d1b4Sraf rsm_memseg_import_put64(rsm_memseg_import_handle_t im_memseg,
19547c478bd9Sstevel@tonic-gate     off_t offset,
19557c478bd9Sstevel@tonic-gate     uint64_t *datap,
19567c478bd9Sstevel@tonic-gate     ulong_t rep_cnt)
19577c478bd9Sstevel@tonic-gate {
19587c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
19597c478bd9Sstevel@tonic-gate 	int		e;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19627c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put64: enter\n"));
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	/* addr of data will always pass the alignment check, avoids	*/
19657c478bd9Sstevel@tonic-gate 	/* need for a special case in verify_access for PUTs		*/
19667c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)datap, rep_cnt*8,
19677c478bd9Sstevel@tonic-gate 	    RSM_PERM_WRITE,
19687c478bd9Sstevel@tonic-gate 	    RSM_DAS64);
19697c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
19707c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
19717c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19747c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
19757c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
19767c478bd9Sstevel@tonic-gate 		}
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put64(im_memseg, offset, datap,
19797c478bd9Sstevel@tonic-gate 		    rep_cnt, 0);
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
19827c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
19837c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
19847c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
19857c478bd9Sstevel@tonic-gate 			}
19867c478bd9Sstevel@tonic-gate 		}
19877c478bd9Sstevel@tonic-gate 	}
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
19907c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put64: exit\n"));
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	return (e);
19937c478bd9Sstevel@tonic-gate }
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate int
1996*7257d1b4Sraf rsm_memseg_import_put(rsm_memseg_import_handle_t im_memseg,
19977c478bd9Sstevel@tonic-gate     off_t offset,
19987c478bd9Sstevel@tonic-gate     void *src_addr,
19997c478bd9Sstevel@tonic-gate     size_t length)
20007c478bd9Sstevel@tonic-gate {
20017c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
20027c478bd9Sstevel@tonic-gate 	int e;
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
20057c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put: enter\n"));
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	e = __rsm_import_verify_access(seg, offset, (caddr_t)src_addr, length,
20087c478bd9Sstevel@tonic-gate 	    RSM_PERM_WRITE,
20097c478bd9Sstevel@tonic-gate 	    RSM_DAS8);
20107c478bd9Sstevel@tonic-gate 	if (e == RSM_SUCCESS) {
20117c478bd9Sstevel@tonic-gate 		rsm_segops_t *ops = seg->rsmseg_ops;
20127c478bd9Sstevel@tonic-gate 		rsmbar_handle_t *bar = (rsmbar_handle_t *)seg->rsmseg_barrier;
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
20157c478bd9Sstevel@tonic-gate 			/* generation number snapshot */
20167c478bd9Sstevel@tonic-gate 			bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum;
20177c478bd9Sstevel@tonic-gate 		}
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 		e = ops->rsm_memseg_import_put(im_memseg, offset, src_addr,
20207c478bd9Sstevel@tonic-gate 		    length);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
20237c478bd9Sstevel@tonic-gate 			/* check the generation number for force disconnects */
20247c478bd9Sstevel@tonic-gate 			if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
20257c478bd9Sstevel@tonic-gate 				return (RSMERR_CONN_ABORTED);
20267c478bd9Sstevel@tonic-gate 			}
20277c478bd9Sstevel@tonic-gate 		}
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	}
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
20327c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_put: exit\n"));
20337c478bd9Sstevel@tonic-gate 	return (e);
20347c478bd9Sstevel@tonic-gate }
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate int
2038*7257d1b4Sraf rsm_memseg_import_putv(rsm_scat_gath_t *sg_io)
20397c478bd9Sstevel@tonic-gate {
20407c478bd9Sstevel@tonic-gate 	rsm_controller_t *cntrl;
20417c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg;
20427c478bd9Sstevel@tonic-gate 	uint_t save_sg_io_flags;
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	int e;
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
20477c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_putv: enter\n"));
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	if (sg_io == NULL) {
20517c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
20527c478bd9Sstevel@tonic-gate 		    "invalid sg_io structure\n"));
20537c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SGIO);
20547c478bd9Sstevel@tonic-gate 	}
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	seg = (rsmseg_handle_t *)sg_io->remote_handle;
20577c478bd9Sstevel@tonic-gate 	if (seg == NULL) {
20587c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
20597c478bd9Sstevel@tonic-gate 		    "invalid remote segment handle in sg_io\n"));
20607c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
20617c478bd9Sstevel@tonic-gate 	}
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	cntrl = (rsm_controller_t *)seg->rsmseg_controller;
20647c478bd9Sstevel@tonic-gate 	if (cntrl == NULL) {
20657c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
20667c478bd9Sstevel@tonic-gate 		    "invalid controller handle\n"));
20677c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
20687c478bd9Sstevel@tonic-gate 	}
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	if ((sg_io->io_request_count > RSM_MAX_SGIOREQS) ||
20717c478bd9Sstevel@tonic-gate 	    (sg_io->io_request_count == 0)) {
20727c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
20737c478bd9Sstevel@tonic-gate 		    "io_request_count value incorrect\n"));
20747c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SGIO);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/* do an implicit map if required */
20787c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_CONNECT) {
20797c478bd9Sstevel@tonic-gate 		e = __rsm_import_implicit_map(seg, RSM_IOTYPE_SCATGATH);
20807c478bd9Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
20817c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
20827c478bd9Sstevel@tonic-gate 			    "implicit map failed\n"));
20837c478bd9Sstevel@tonic-gate 			return (e);
20847c478bd9Sstevel@tonic-gate 		}
20857c478bd9Sstevel@tonic-gate 	}
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	/*
20887c478bd9Sstevel@tonic-gate 	 * Copy the flags field of the sg_io structure in a local
20897c478bd9Sstevel@tonic-gate 	 * variable.
20907c478bd9Sstevel@tonic-gate 	 * This is required since the flags field can be
20917c478bd9Sstevel@tonic-gate 	 * changed by the plugin library routine to indicate that
20927c478bd9Sstevel@tonic-gate 	 * the signal post was done.
20937c478bd9Sstevel@tonic-gate 	 * This change in the flags field of the sg_io structure
20947c478bd9Sstevel@tonic-gate 	 * should not be reflected to the user. Hence once the flags
20957c478bd9Sstevel@tonic-gate 	 * field has been used for the purpose of determining whether
20967c478bd9Sstevel@tonic-gate 	 * the plugin executed a signal post, it must be restored to
20977c478bd9Sstevel@tonic-gate 	 * its original value which is stored in the local variable.
20987c478bd9Sstevel@tonic-gate 	 */
20997c478bd9Sstevel@tonic-gate 	save_sg_io_flags = sg_io->flags;
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	e = cntrl->cntr_segops->rsm_memseg_import_putv(sg_io);
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	/*
21047c478bd9Sstevel@tonic-gate 	 * At this point, if an implicit signal post was requested by
21057c478bd9Sstevel@tonic-gate 	 * the user, there could be two possibilities that arise:
21067c478bd9Sstevel@tonic-gate 	 * 1. the plugin routine has already executed the implicit
21077c478bd9Sstevel@tonic-gate 	 *    signal post either successfully or unsuccessfully
21087c478bd9Sstevel@tonic-gate 	 * 2. the plugin does not have the capability of doing an
21097c478bd9Sstevel@tonic-gate 	 *    implicit signal post and hence the signal post needs
21107c478bd9Sstevel@tonic-gate 	 *    to be done here.
21117c478bd9Sstevel@tonic-gate 	 * The above two cases can be idenfied by the flags
21127c478bd9Sstevel@tonic-gate 	 * field within the sg_io structure as follows:
21137c478bd9Sstevel@tonic-gate 	 * In case 1, the RSM_IMPLICIT_SIGPOST bit is reset to 0 by the
21147c478bd9Sstevel@tonic-gate 	 * plugin, indicating that the signal post was done.
21157c478bd9Sstevel@tonic-gate 	 * In case 2, the bit remains set to a 1 as originally given
21167c478bd9Sstevel@tonic-gate 	 * by the user, and hence a signal post needs to be done here.
21177c478bd9Sstevel@tonic-gate 	 */
21187c478bd9Sstevel@tonic-gate 	if (sg_io->flags & RSM_IMPLICIT_SIGPOST &&
21197c478bd9Sstevel@tonic-gate 	    e == RSM_SUCCESS) {
21207c478bd9Sstevel@tonic-gate 		/* Do the implicit signal post */
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 		/*
21237c478bd9Sstevel@tonic-gate 		 * The value of the second argument to this call
21247c478bd9Sstevel@tonic-gate 		 * depends on the value of the sg_io->flags field.
21257c478bd9Sstevel@tonic-gate 		 * If the RSM_SIGPOST_NO_ACCUMULATE flag has been
21267c478bd9Sstevel@tonic-gate 		 * ored into the sg_io->flags field, this indicates
21277c478bd9Sstevel@tonic-gate 		 * that the rsm_intr_signal_post is to be done with
21287c478bd9Sstevel@tonic-gate 		 * the flags argument set to RSM_SIGPOST_NO_ACCUMULATE
21297c478bd9Sstevel@tonic-gate 		 * Else, the flags argument is set to 0. These
21307c478bd9Sstevel@tonic-gate 		 * semantics can be achieved simply by masking off
21317c478bd9Sstevel@tonic-gate 		 * all other bits in the sg_io->flags field except the
21327c478bd9Sstevel@tonic-gate 		 * RSM_SIGPOST_NO_ACCUMULATE bit and using the result
21337c478bd9Sstevel@tonic-gate 		 * as the flags argument for the rsm_intr_signal_post.
21347c478bd9Sstevel@tonic-gate 		 */
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 		int sigpost_flags = sg_io->flags & RSM_SIGPOST_NO_ACCUMULATE;
21377c478bd9Sstevel@tonic-gate 		e = rsm_intr_signal_post(seg, sigpost_flags);
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	/* Restore the flags field within the users scatter gather structure */
21427c478bd9Sstevel@tonic-gate 	sg_io->flags = save_sg_io_flags;
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
21457c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_putv: exit\n"));
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	return (e);
21487c478bd9Sstevel@tonic-gate }
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 	/*
21527c478bd9Sstevel@tonic-gate 	 * import side memory segment operations (mapping):
21537c478bd9Sstevel@tonic-gate 	 */
21547c478bd9Sstevel@tonic-gate int
2155*7257d1b4Sraf rsm_memseg_import_map(rsm_memseg_import_handle_t im_memseg,
21567c478bd9Sstevel@tonic-gate     void **address,
21577c478bd9Sstevel@tonic-gate     rsm_attribute_t attr,
21587c478bd9Sstevel@tonic-gate     rsm_permission_t perm,
21597c478bd9Sstevel@tonic-gate     off_t offset,
21607c478bd9Sstevel@tonic-gate     size_t length)
21617c478bd9Sstevel@tonic-gate {
21627c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
21637c478bd9Sstevel@tonic-gate 	int flag = MAP_SHARED;
21647c478bd9Sstevel@tonic-gate 	int prot;
21657c478bd9Sstevel@tonic-gate 	caddr_t va;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
21687c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_map: enter\n"));
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	if (!seg) {
21717c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
21727c478bd9Sstevel@tonic-gate 		    "invalid segment\n"));
21737c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
21747c478bd9Sstevel@tonic-gate 	}
21757c478bd9Sstevel@tonic-gate 	if (!address) {
21767c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
21777c478bd9Sstevel@tonic-gate 		    "invalid address\n"));
21787c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
21797c478bd9Sstevel@tonic-gate 	}
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	/*
21827c478bd9Sstevel@tonic-gate 	 * Only one map per segment handle!
21837c478bd9Sstevel@tonic-gate 	 * XXX need to take a lock here
21847c478bd9Sstevel@tonic-gate 	 */
21857c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state == IMPORT_MAP) {
21887c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
21897c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
21907c478bd9Sstevel@tonic-gate 		    "segment already mapped\n"));
21917c478bd9Sstevel@tonic-gate 		return (RSMERR_SEG_ALREADY_MAPPED);
21927c478bd9Sstevel@tonic-gate 	}
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	/* Only import segments allowed to map */
21957c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state != IMPORT_CONNECT) {
21967c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
21977c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
21987c478bd9Sstevel@tonic-gate 	}
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	/* check for permissions */
22017c478bd9Sstevel@tonic-gate 	if (perm > RSM_PERM_RDWR) {
22027c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22037c478bd9Sstevel@tonic-gate 		    "bad permissions when mapping\n"));
22047c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22057c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_PERMS);
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	if (length == 0) {
22097c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22107c478bd9Sstevel@tonic-gate 		    "mapping with length 0\n"));
22117c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22127c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_LENGTH);
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	if (offset + length > seg->rsmseg_size) {
22167c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22177c478bd9Sstevel@tonic-gate 		    "map length + offset exceed segment size\n"));
22187c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22197c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_LENGTH);
22207c478bd9Sstevel@tonic-gate 	}
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	if ((size_t)offset & (PAGESIZE - 1)) {
22237c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
22247c478bd9Sstevel@tonic-gate 		    "bad mem alignment\n"));
22257c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_MEM_ALIGNMENT);
22267c478bd9Sstevel@tonic-gate 	}
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	if (attr & RSM_MAP_FIXED) {
22297c478bd9Sstevel@tonic-gate 		if ((uintptr_t)(*address) & (PAGESIZE - 1)) {
22307c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
22317c478bd9Sstevel@tonic-gate 			    "bad mem alignment\n"));
22327c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_MEM_ALIGNMENT);
22337c478bd9Sstevel@tonic-gate 		}
22347c478bd9Sstevel@tonic-gate 		flag |= MAP_FIXED;
22357c478bd9Sstevel@tonic-gate 	}
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 	prot = PROT_NONE;
22387c478bd9Sstevel@tonic-gate 	if (perm & RSM_PERM_READ)
22397c478bd9Sstevel@tonic-gate 		prot |= PROT_READ;
22407c478bd9Sstevel@tonic-gate 	if (perm & RSM_PERM_WRITE)
22417c478bd9Sstevel@tonic-gate 		prot |= PROT_WRITE;
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	va = mmap(*address, length, prot, flag, seg->rsmseg_fd, offset);
22447c478bd9Sstevel@tonic-gate 	if (va == MAP_FAILED) {
22457c478bd9Sstevel@tonic-gate 		int e = errno;
22467c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
22477c478bd9Sstevel@tonic-gate 		    "error %d during map\n", e));
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
22507c478bd9Sstevel@tonic-gate 		if (e == ENXIO || e == EOVERFLOW ||
22517c478bd9Sstevel@tonic-gate 		    e == ENOMEM)
22527c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_LENGTH);
22537c478bd9Sstevel@tonic-gate 		else if (e == ENODEV)
22547c478bd9Sstevel@tonic-gate 			return (RSMERR_CONN_ABORTED);
22557c478bd9Sstevel@tonic-gate 		else if (e == EAGAIN)
22567c478bd9Sstevel@tonic-gate 			return (RSMERR_INSUFFICIENT_RESOURCES);
22577c478bd9Sstevel@tonic-gate 		else if (e == ENOTSUP)
22587c478bd9Sstevel@tonic-gate 			return (RSMERR_MAP_FAILED);
22597c478bd9Sstevel@tonic-gate 		else if (e == EACCES)
22607c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_PERMS);
22617c478bd9Sstevel@tonic-gate 		else
22627c478bd9Sstevel@tonic-gate 			return (RSMERR_MAP_FAILED);
22637c478bd9Sstevel@tonic-gate 	}
22647c478bd9Sstevel@tonic-gate 	*address = va;
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	/*
22677c478bd9Sstevel@tonic-gate 	 * Fix segment ops vector to handle direct access.
22687c478bd9Sstevel@tonic-gate 	 */
22697c478bd9Sstevel@tonic-gate 	/*
22707c478bd9Sstevel@tonic-gate 	 * XXX: Set this only for full segment mapping. Keep a list
22717c478bd9Sstevel@tonic-gate 	 * of mappings to use for access functions
22727c478bd9Sstevel@tonic-gate 	 */
22737c478bd9Sstevel@tonic-gate 	seg->rsmseg_vaddr = va;
22747c478bd9Sstevel@tonic-gate 	seg->rsmseg_maplen = length;
22757c478bd9Sstevel@tonic-gate 	seg->rsmseg_mapoffset = offset;
22767c478bd9Sstevel@tonic-gate 	seg->rsmseg_state = IMPORT_MAP;
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
22817c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_map: exit\n"));
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate int
2287*7257d1b4Sraf rsm_memseg_import_unmap(rsm_memseg_import_handle_t im_memseg)
22887c478bd9Sstevel@tonic-gate {
22897c478bd9Sstevel@tonic-gate 	/*
22907c478bd9Sstevel@tonic-gate 	 * Until we fix the rsm driver to catch unload, we unload
22917c478bd9Sstevel@tonic-gate 	 * the whole segment.
22927c478bd9Sstevel@tonic-gate 	 */
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
22977c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_unmap: enter\n"));
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	if (!seg) {
23007c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
23017c478bd9Sstevel@tonic-gate 		    "invalid segment or segment state\n"));
23027c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
23037c478bd9Sstevel@tonic-gate 	}
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
23067c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_state != IMPORT_MAP) {
23077c478bd9Sstevel@tonic-gate 		mutex_unlock(&seg->rsmseg_lock);
23087c478bd9Sstevel@tonic-gate 		return (RSMERR_SEG_NOT_MAPPED);
23097c478bd9Sstevel@tonic-gate 	}
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	seg->rsmseg_mapoffset = 0;   /* reset the offset */
23127c478bd9Sstevel@tonic-gate 	seg->rsmseg_state = IMPORT_CONNECT;
23137c478bd9Sstevel@tonic-gate 	seg->rsmseg_flags &= ~RSM_IMPLICIT_MAP;
23147c478bd9Sstevel@tonic-gate 	(void) munmap(seg->rsmseg_vaddr, seg->rsmseg_maplen);
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23197c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_unmap: exit\n"));
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	/*
23267c478bd9Sstevel@tonic-gate 	 * import side memory segment operations (barriers):
23277c478bd9Sstevel@tonic-gate 	 */
23287c478bd9Sstevel@tonic-gate int
2329*7257d1b4Sraf rsm_memseg_import_init_barrier(rsm_memseg_import_handle_t im_memseg,
23307c478bd9Sstevel@tonic-gate     rsm_barrier_type_t type,
23317c478bd9Sstevel@tonic-gate     rsmapi_barrier_t *barrier)
23327c478bd9Sstevel@tonic-gate {
23337c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
23347c478bd9Sstevel@tonic-gate 	rsmbar_handle_t *bar;
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23377c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_init_barrier: enter\n"));
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	if (!seg) {
23407c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
23417c478bd9Sstevel@tonic-gate 		    "invalid segment or barrier\n"));
23427c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
23437c478bd9Sstevel@tonic-gate 	}
23447c478bd9Sstevel@tonic-gate 	if (!barrier) {
23457c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
23467c478bd9Sstevel@tonic-gate 		    "invalid barrier pointer\n"));
23477c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
23487c478bd9Sstevel@tonic-gate 	}
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	bar = (rsmbar_handle_t *)barrier;
23517c478bd9Sstevel@tonic-gate 	bar->rsmbar_seg = seg;
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	seg->rsmseg_barrier = barrier;  /* used in put/get fns */
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23567c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_init_barrier: exit\n"));
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	return (seg->rsmseg_ops->rsm_memseg_import_init_barrier(im_memseg,
23597c478bd9Sstevel@tonic-gate 	    type, (rsm_barrier_handle_t)barrier));
23607c478bd9Sstevel@tonic-gate }
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate int
2363*7257d1b4Sraf rsm_memseg_import_open_barrier(rsmapi_barrier_t *barrier)
23647c478bd9Sstevel@tonic-gate {
23657c478bd9Sstevel@tonic-gate 	rsmbar_handle_t *bar = (rsmbar_handle_t *)barrier;
23667c478bd9Sstevel@tonic-gate 	rsm_segops_t *ops;
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23697c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_open_barrier: enter\n"));
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	if (!bar) {
23727c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
23737c478bd9Sstevel@tonic-gate 		    "invalid barrier\n"));
23747c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
23757c478bd9Sstevel@tonic-gate 	}
23767c478bd9Sstevel@tonic-gate 	if (!bar->rsmbar_seg) {
23777c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
23787c478bd9Sstevel@tonic-gate 		    "uninitialized barrier\n"));
23797c478bd9Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
23807c478bd9Sstevel@tonic-gate 	}
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	/* generation number snapshot */
23837c478bd9Sstevel@tonic-gate 	bar->rsmbar_gen = bar->rsmbar_seg->rsmseg_gnum; /* bar[0] */
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	ops = bar->rsmbar_seg->rsmseg_ops;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
23887c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_open_barrier: exit\n"));
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	return (ops->rsm_memseg_import_open_barrier(
23917c478bd9Sstevel@tonic-gate 	    (rsm_barrier_handle_t)barrier));
23927c478bd9Sstevel@tonic-gate }
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate int
2395*7257d1b4Sraf rsm_memseg_import_order_barrier(rsmapi_barrier_t *barrier)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate 	rsmbar_handle_t *bar = (rsmbar_handle_t *)barrier;
23987c478bd9Sstevel@tonic-gate 	rsm_segops_t *ops;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24017c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_order_barrier: enter\n"));
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	if (!bar) {
24047c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24057c478bd9Sstevel@tonic-gate 		    "invalid barrier\n"));
24067c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
24077c478bd9Sstevel@tonic-gate 	}
24087c478bd9Sstevel@tonic-gate 	if (!bar->rsmbar_seg) {
24097c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24107c478bd9Sstevel@tonic-gate 		    "uninitialized barrier\n"));
24117c478bd9Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	ops = bar->rsmbar_seg->rsmseg_ops;
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24177c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_order_barrier: exit\n"));
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	return (ops->rsm_memseg_import_order_barrier(
24207c478bd9Sstevel@tonic-gate 	    (rsm_barrier_handle_t)barrier));
24217c478bd9Sstevel@tonic-gate }
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate int
2424*7257d1b4Sraf rsm_memseg_import_close_barrier(rsmapi_barrier_t *barrier)
24257c478bd9Sstevel@tonic-gate {
24267c478bd9Sstevel@tonic-gate 	rsmbar_handle_t *bar = (rsmbar_handle_t *)barrier;
24277c478bd9Sstevel@tonic-gate 	rsm_segops_t *ops;
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24307c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_close_barrier: enter\n"));
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	if (!bar) {
24337c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24347c478bd9Sstevel@tonic-gate 		    "invalid barrier\n"));
24357c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
24367c478bd9Sstevel@tonic-gate 	}
24377c478bd9Sstevel@tonic-gate 	if (!bar->rsmbar_seg) {
24387c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24397c478bd9Sstevel@tonic-gate 		    "uninitialized barrier\n"));
24407c478bd9Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
24417c478bd9Sstevel@tonic-gate 	}
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	/* generation number snapshot */
24447c478bd9Sstevel@tonic-gate 	if (bar->rsmbar_gen != bar->rsmbar_seg->rsmseg_bar[0]) {
24457c478bd9Sstevel@tonic-gate 		return (RSMERR_CONN_ABORTED);
24467c478bd9Sstevel@tonic-gate 	}
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate 	ops = bar->rsmbar_seg->rsmseg_ops;
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24517c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_close_barrier: exit\n"));
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	return (ops->rsm_memseg_import_close_barrier(
24547c478bd9Sstevel@tonic-gate 	    (rsm_barrier_handle_t)barrier));
24557c478bd9Sstevel@tonic-gate }
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate int
2458*7257d1b4Sraf rsm_memseg_import_destroy_barrier(rsmapi_barrier_t *barrier)
24597c478bd9Sstevel@tonic-gate {
24607c478bd9Sstevel@tonic-gate 	rsmbar_handle_t *bar = (rsmbar_handle_t *)barrier;
24617c478bd9Sstevel@tonic-gate 	rsm_segops_t *ops;
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24647c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_destroy_barrier: enter\n"));
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 	if (!bar) {
24677c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24687c478bd9Sstevel@tonic-gate 		    "invalid barrier\n"));
24697c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
24707c478bd9Sstevel@tonic-gate 	}
24717c478bd9Sstevel@tonic-gate 	if (!bar->rsmbar_seg) {
24727c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
24737c478bd9Sstevel@tonic-gate 		    "uninitialized barrier\n"));
24747c478bd9Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
24757c478bd9Sstevel@tonic-gate 	}
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	bar->rsmbar_seg->rsmseg_barrier = NULL;
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	ops = bar->rsmbar_seg->rsmseg_ops;
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24827c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_destroy_barrier: exit\n"));
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	return (ops->rsm_memseg_import_destroy_barrier
24857c478bd9Sstevel@tonic-gate 	    ((rsm_barrier_handle_t)barrier));
24867c478bd9Sstevel@tonic-gate }
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate int
2489*7257d1b4Sraf rsm_memseg_import_get_mode(rsm_memseg_import_handle_t im_memseg,
24907c478bd9Sstevel@tonic-gate     rsm_barrier_mode_t *mode)
24917c478bd9Sstevel@tonic-gate {
24927c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
24957c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_get_mode: enter\n"));
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 	if (seg) {
24987c478bd9Sstevel@tonic-gate 		*mode = seg->rsmseg_barmode;
24997c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25007c478bd9Sstevel@tonic-gate 		    "rsm_memseg_import_get_mode: exit\n"));
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 		return (seg->rsmseg_ops->rsm_memseg_import_get_mode(im_memseg,
25037c478bd9Sstevel@tonic-gate 		    mode));
25047c478bd9Sstevel@tonic-gate 	}
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
25077c478bd9Sstevel@tonic-gate 	    "invalid arguments \n"));
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	return (RSMERR_BAD_SEG_HNDL);
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate }
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate int
2514*7257d1b4Sraf rsm_memseg_import_set_mode(rsm_memseg_import_handle_t im_memseg,
25157c478bd9Sstevel@tonic-gate     rsm_barrier_mode_t mode)
25167c478bd9Sstevel@tonic-gate {
25177c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25207c478bd9Sstevel@tonic-gate 	    "rsm_memseg_import_set_mode: enter\n"));
25217c478bd9Sstevel@tonic-gate 	if (seg) {
25227c478bd9Sstevel@tonic-gate 		if ((mode == RSM_BARRIER_MODE_IMPLICIT ||
25237c478bd9Sstevel@tonic-gate 		    mode == RSM_BARRIER_MODE_EXPLICIT)) {
25247c478bd9Sstevel@tonic-gate 			seg->rsmseg_barmode = mode;
25257c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25267c478bd9Sstevel@tonic-gate 			    "rsm_memseg_import_set_mode: exit\n"));
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 			return (seg->rsmseg_ops->rsm_memseg_import_set_mode(
25297c478bd9Sstevel@tonic-gate 			    im_memseg,
25307c478bd9Sstevel@tonic-gate 			    mode));
25317c478bd9Sstevel@tonic-gate 		} else {
25327c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_DEBUG_VERBOSE,
25337c478bd9Sstevel@tonic-gate 			    "bad barrier mode\n"));
25347c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_MODE);
25357c478bd9Sstevel@tonic-gate 		}
25367c478bd9Sstevel@tonic-gate 	}
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY|RSM_IMPORT, RSM_ERR,
25397c478bd9Sstevel@tonic-gate 	    "invalid arguments\n"));
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	return (RSMERR_BAD_SEG_HNDL);
25427c478bd9Sstevel@tonic-gate }
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate int
2545*7257d1b4Sraf rsm_intr_signal_post(void *memseg, uint_t flags)
25467c478bd9Sstevel@tonic-gate {
25477c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
25487c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
25517c478bd9Sstevel@tonic-gate 	    "rsm_intr_signal_post: enter\n"));
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	flags = flags;
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	if (!seg) {
25567c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
25577c478bd9Sstevel@tonic-gate 		    "invalid segment handle\n"));
25587c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
25597c478bd9Sstevel@tonic-gate 	}
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_RING_BELL, &msg) < 0) {
25627c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
25637c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_RING_BELL failed\n"));
25647c478bd9Sstevel@tonic-gate 		return (errno);
25657c478bd9Sstevel@tonic-gate 	}
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
25687c478bd9Sstevel@tonic-gate 	    "rsm_intr_signal_post: exit\n"));
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
25717c478bd9Sstevel@tonic-gate }
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate int
2574*7257d1b4Sraf rsm_intr_signal_wait(void *memseg, int timeout)
25757c478bd9Sstevel@tonic-gate {
25767c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
25777c478bd9Sstevel@tonic-gate 	struct pollfd fds;
25787c478bd9Sstevel@tonic-gate 	minor_t	rnum;
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
25817c478bd9Sstevel@tonic-gate 	    "rsm_intr_signal_wait: enter\n"));
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 	if (!seg) {
25847c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
25857c478bd9Sstevel@tonic-gate 		    "invalid segment\n"));
25867c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
25877c478bd9Sstevel@tonic-gate 	}
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	fds.fd = seg->rsmseg_fd;
25907c478bd9Sstevel@tonic-gate 	fds.events = POLLRDNORM;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	rnum = seg->rsmseg_rnum;
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate 	return (__rsm_intr_signal_wait_common(&fds, &rnum, 1, timeout, NULL));
25957c478bd9Sstevel@tonic-gate }
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate int
2598*7257d1b4Sraf rsm_intr_signal_wait_pollfd(struct pollfd fds[], nfds_t nfds, int timeout,
25997c478bd9Sstevel@tonic-gate 	int *numfdsp)
26007c478bd9Sstevel@tonic-gate {
26017c478bd9Sstevel@tonic-gate 	return (__rsm_intr_signal_wait_common(fds, NULL, nfds, timeout,
26027c478bd9Sstevel@tonic-gate 	    numfdsp));
26037c478bd9Sstevel@tonic-gate }
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate /*
26067c478bd9Sstevel@tonic-gate  * This is the generic wait routine, it takes the following arguments
26077c478bd9Sstevel@tonic-gate  *	- pollfd array
26087c478bd9Sstevel@tonic-gate  *	- rnums array corresponding to the pollfd if known, if this is
26097c478bd9Sstevel@tonic-gate  *	NULL then the fds are looked up from the pollfd_table.
26107c478bd9Sstevel@tonic-gate  *	- number of fds in pollfd array,
26117c478bd9Sstevel@tonic-gate  *	- timeout
26127c478bd9Sstevel@tonic-gate  *	- pointer to a location where the number of fds with successful
26137c478bd9Sstevel@tonic-gate  *	events is returned.
26147c478bd9Sstevel@tonic-gate  */
26157c478bd9Sstevel@tonic-gate static int
26167c478bd9Sstevel@tonic-gate __rsm_intr_signal_wait_common(struct pollfd fds[], minor_t rnums[],
26177c478bd9Sstevel@tonic-gate     nfds_t nfds, int timeout, int *numfdsp)
26187c478bd9Sstevel@tonic-gate {
26197c478bd9Sstevel@tonic-gate 	int	i;
26207c478bd9Sstevel@tonic-gate 	int	numsegs = 0;
26217c478bd9Sstevel@tonic-gate 	int	numfd;
26227c478bd9Sstevel@tonic-gate 	int	fds_processed = 0;
26237c478bd9Sstevel@tonic-gate 	minor_t	segrnum;
26247c478bd9Sstevel@tonic-gate 	rsm_poll_event_t	event_arr[RSM_MAX_POLLFDS];
26257c478bd9Sstevel@tonic-gate 	rsm_poll_event_t	*event_list = NULL;
26267c478bd9Sstevel@tonic-gate 	rsm_poll_event_t	*events;
26277c478bd9Sstevel@tonic-gate 	rsm_consume_event_msg_t msg;
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE, "wait_common enter\n"));
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	if (numfdsp) {
26327c478bd9Sstevel@tonic-gate 		*numfdsp = 0;
26337c478bd9Sstevel@tonic-gate 	}
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	numfd = poll(fds, nfds, timeout);
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	switch (numfd) {
26387c478bd9Sstevel@tonic-gate 	case -1: /* poll returned error - map to RSMERR_... */
26397c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR, "signal wait pollfd err\n"));
26407c478bd9Sstevel@tonic-gate 		switch (errno) {
26417c478bd9Sstevel@tonic-gate 		case EAGAIN:
26427c478bd9Sstevel@tonic-gate 			return (RSMERR_INSUFFICIENT_RESOURCES);
26437c478bd9Sstevel@tonic-gate 		case EFAULT:
26447c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_ADDR);
26457c478bd9Sstevel@tonic-gate 		case EINTR:
26467c478bd9Sstevel@tonic-gate 			return (RSMERR_INTERRUPTED);
26477c478bd9Sstevel@tonic-gate 		case EINVAL:
26487c478bd9Sstevel@tonic-gate 		default:
26497c478bd9Sstevel@tonic-gate 			return (RSMERR_BAD_ARGS_ERRORS);
26507c478bd9Sstevel@tonic-gate 		}
26517c478bd9Sstevel@tonic-gate 	case 0: /* timedout - return from here */
26527c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
26537c478bd9Sstevel@tonic-gate 		    "signal wait timed out\n"));
26547c478bd9Sstevel@tonic-gate 		return (RSMERR_TIMEOUT);
26557c478bd9Sstevel@tonic-gate 	default:
26567c478bd9Sstevel@tonic-gate 		break;
26577c478bd9Sstevel@tonic-gate 	}
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	if (numfd <= RSM_MAX_POLLFDS) {
26607c478bd9Sstevel@tonic-gate 		/* use the event array on the stack */
26617c478bd9Sstevel@tonic-gate 		events = (rsm_poll_event_t *)event_arr;
26627c478bd9Sstevel@tonic-gate 	} else {
26637c478bd9Sstevel@tonic-gate 		/*
26647c478bd9Sstevel@tonic-gate 		 * actual number of fds corresponding to rsmapi segments might
26657c478bd9Sstevel@tonic-gate 		 * be < numfd, don't want to scan the list to figure that out
26667c478bd9Sstevel@tonic-gate 		 * lets just allocate on the heap
26677c478bd9Sstevel@tonic-gate 		 */
26687c478bd9Sstevel@tonic-gate 		event_list = (rsm_poll_event_t *)malloc(
26697c478bd9Sstevel@tonic-gate 		    sizeof (rsm_poll_event_t)*numfd);
26707c478bd9Sstevel@tonic-gate 		if (!event_list) {
26717c478bd9Sstevel@tonic-gate 			/*
26727c478bd9Sstevel@tonic-gate 			 * return with error even if poll might have succeeded
26737c478bd9Sstevel@tonic-gate 			 * since the application can retry and the events will
26747c478bd9Sstevel@tonic-gate 			 * still be available.
26757c478bd9Sstevel@tonic-gate 			 */
26767c478bd9Sstevel@tonic-gate 			return (RSMERR_INSUFFICIENT_MEM);
26777c478bd9Sstevel@tonic-gate 		}
26787c478bd9Sstevel@tonic-gate 		events = event_list;
26797c478bd9Sstevel@tonic-gate 	}
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	/*
26827c478bd9Sstevel@tonic-gate 	 * process the fds for events and if it corresponds to an rsmapi
26837c478bd9Sstevel@tonic-gate 	 * segment consume the event
26847c478bd9Sstevel@tonic-gate 	 */
26857c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfds; i++) {
26867c478bd9Sstevel@tonic-gate 		if (fds[i].revents == POLLRDNORM) {
26877c478bd9Sstevel@tonic-gate 			/*
26887c478bd9Sstevel@tonic-gate 			 * poll returned an event and if its POLLRDNORM, it
26897c478bd9Sstevel@tonic-gate 			 * might correspond to an rsmapi segment
26907c478bd9Sstevel@tonic-gate 			 */
26917c478bd9Sstevel@tonic-gate 			if (rnums) { /* resource num is passed in */
26927c478bd9Sstevel@tonic-gate 				segrnum = rnums[i];
26937c478bd9Sstevel@tonic-gate 			} else { /* lookup pollfd table to get resource num */
26947c478bd9Sstevel@tonic-gate 				segrnum = _rsm_lookup_pollfd_table(fds[i].fd);
26957c478bd9Sstevel@tonic-gate 			}
26967c478bd9Sstevel@tonic-gate 			if (segrnum) {
26977c478bd9Sstevel@tonic-gate 				events[numsegs].rnum = segrnum;
26987c478bd9Sstevel@tonic-gate 				events[numsegs].revent = 0;
26997c478bd9Sstevel@tonic-gate 				events[numsegs].fdsidx = i; /* fdlist index */
27007c478bd9Sstevel@tonic-gate 				numsegs++;
27017c478bd9Sstevel@tonic-gate 			}
27027c478bd9Sstevel@tonic-gate 		}
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 		if ((fds[i].revents) && (++fds_processed == numfd)) {
27057c478bd9Sstevel@tonic-gate 			/*
27067c478bd9Sstevel@tonic-gate 			 * only "numfd" events have revents field set, once we
27077c478bd9Sstevel@tonic-gate 			 * process that many break out of the loop
27087c478bd9Sstevel@tonic-gate 			 */
27097c478bd9Sstevel@tonic-gate 			break;
27107c478bd9Sstevel@tonic-gate 		}
27117c478bd9Sstevel@tonic-gate 	}
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	if (numsegs == 0) { /* No events for rsmapi segs in the fdlist */
27147c478bd9Sstevel@tonic-gate 		if (event_list) {
27157c478bd9Sstevel@tonic-gate 			free(event_list);
27167c478bd9Sstevel@tonic-gate 		}
27177c478bd9Sstevel@tonic-gate 		if (numfdsp) {
27187c478bd9Sstevel@tonic-gate 			*numfdsp = numfd;
27197c478bd9Sstevel@tonic-gate 		}
27207c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
27217c478bd9Sstevel@tonic-gate 		    "wait_common exit: no rsmapi segs\n"));
27227c478bd9Sstevel@tonic-gate 		return (RSM_SUCCESS);
27237c478bd9Sstevel@tonic-gate 	}
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	msg.seglist = (caddr_t)events;
27267c478bd9Sstevel@tonic-gate 	msg.numents = numsegs;
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_CONSUMEEVENT, &msg) < 0) {
27297c478bd9Sstevel@tonic-gate 		int error = errno;
27307c478bd9Sstevel@tonic-gate 		if (event_list) {
27317c478bd9Sstevel@tonic-gate 			free(event_list);
27327c478bd9Sstevel@tonic-gate 		}
27337c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY|RSM_LOOPBACK, RSM_ERR,
27347c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_CONSUMEEVENT failed(%d)\n", error));
27357c478bd9Sstevel@tonic-gate 		return (error);
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	/* count the number of segs for which consumeevent was successful */
27397c478bd9Sstevel@tonic-gate 	numfd -= numsegs;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	for (i = 0; i < numsegs; i++) {
27427c478bd9Sstevel@tonic-gate 		if (events[i].revent != 0) {
27437c478bd9Sstevel@tonic-gate 			fds[events[i].fdsidx].revents = POLLRDNORM;
27447c478bd9Sstevel@tonic-gate 			numfd++;
27457c478bd9Sstevel@tonic-gate 		} else { /* failed to consume event so set revents to 0 */
27467c478bd9Sstevel@tonic-gate 			fds[events[i].fdsidx].revents = 0;
27477c478bd9Sstevel@tonic-gate 		}
27487c478bd9Sstevel@tonic-gate 	}
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	if (event_list) {
27517c478bd9Sstevel@tonic-gate 		free(event_list);
27527c478bd9Sstevel@tonic-gate 	}
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	if (numfd > 0) {
27557c478bd9Sstevel@tonic-gate 		if (numfdsp) {
27567c478bd9Sstevel@tonic-gate 			*numfdsp = numfd;
27577c478bd9Sstevel@tonic-gate 		}
27587c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
27597c478bd9Sstevel@tonic-gate 		    "wait_common exit\n"));
27607c478bd9Sstevel@tonic-gate 		return (RSM_SUCCESS);
27617c478bd9Sstevel@tonic-gate 	} else {
27627c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
27637c478bd9Sstevel@tonic-gate 		    "wait_common exit\n"));
27647c478bd9Sstevel@tonic-gate 		return (RSMERR_TIMEOUT);
27657c478bd9Sstevel@tonic-gate 	}
27667c478bd9Sstevel@tonic-gate }
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate /*
27697c478bd9Sstevel@tonic-gate  * This function provides the data (file descriptor and event) for
27707c478bd9Sstevel@tonic-gate  * the specified pollfd struct.  The pollfd struct may then be
27717c478bd9Sstevel@tonic-gate  * subsequently used with the poll system call to wait for an event
27727c478bd9Sstevel@tonic-gate  * signalled by rsm_intr_signal_post.  The memory segment must be
27737c478bd9Sstevel@tonic-gate  * currently published for a successful return with a valid pollfd.
27747c478bd9Sstevel@tonic-gate  * A reference count for the descriptor is incremented.
27757c478bd9Sstevel@tonic-gate  */
27767c478bd9Sstevel@tonic-gate int
2777*7257d1b4Sraf rsm_memseg_get_pollfd(void *memseg,
27787c478bd9Sstevel@tonic-gate 			struct pollfd *poll_fd)
27797c478bd9Sstevel@tonic-gate {
27807c478bd9Sstevel@tonic-gate 	int	i;
27817c478bd9Sstevel@tonic-gate 	int	err = RSM_SUCCESS;
27827c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
27857c478bd9Sstevel@tonic-gate 	    "rsm_memseg_get_pollfd: enter\n"));
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	if (!seg) {
27887c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
27897c478bd9Sstevel@tonic-gate 		    "invalid segment\n"));
27907c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
27917c478bd9Sstevel@tonic-gate 	}
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	poll_fd->fd = seg->rsmseg_fd;
27967c478bd9Sstevel@tonic-gate 	poll_fd->events = POLLRDNORM;
27977c478bd9Sstevel@tonic-gate 	seg->rsmseg_pollfd_refcnt++;
27987c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_pollfd_refcnt == 1) {
27997c478bd9Sstevel@tonic-gate 		/* insert the segment into the pollfd table */
28007c478bd9Sstevel@tonic-gate 		err = _rsm_insert_pollfd_table(seg->rsmseg_fd,
28017c478bd9Sstevel@tonic-gate 		    seg->rsmseg_rnum);
28027c478bd9Sstevel@tonic-gate 	}
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28077c478bd9Sstevel@tonic-gate 	    "rsm_memseg_get_pollfd: exit(%d)\n", err));
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	return (err);
28107c478bd9Sstevel@tonic-gate }
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate /*
28137c478bd9Sstevel@tonic-gate  * This function decrements the segment pollfd reference count.
28147c478bd9Sstevel@tonic-gate  * A segment unpublish or destroy operation will fail if the reference count is
28157c478bd9Sstevel@tonic-gate  * non zero.
28167c478bd9Sstevel@tonic-gate  */
28177c478bd9Sstevel@tonic-gate int
2818*7257d1b4Sraf rsm_memseg_release_pollfd(void * memseg)
28197c478bd9Sstevel@tonic-gate {
28207c478bd9Sstevel@tonic-gate 	int	i;
28217c478bd9Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)memseg;
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28247c478bd9Sstevel@tonic-gate 	    "rsm_memseg_release_pollfd: enter\n"));
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	if (!seg) {
28277c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
28287c478bd9Sstevel@tonic-gate 		    "invalid segment handle\n"));
28297c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
28307c478bd9Sstevel@tonic-gate 	}
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	mutex_lock(&seg->rsmseg_lock);
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	if (seg->rsmseg_pollfd_refcnt) {
28357c478bd9Sstevel@tonic-gate 		seg->rsmseg_pollfd_refcnt--;
28367c478bd9Sstevel@tonic-gate 		if (seg->rsmseg_pollfd_refcnt == 0) {
28377c478bd9Sstevel@tonic-gate 			/* last reference removed - update the pollfd_table */
28387c478bd9Sstevel@tonic-gate 			_rsm_remove_pollfd_table(seg->rsmseg_fd);
28397c478bd9Sstevel@tonic-gate 		}
28407c478bd9Sstevel@tonic-gate 	}
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	mutex_unlock(&seg->rsmseg_lock);
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28457c478bd9Sstevel@tonic-gate 	    "rsm_memseg_release_pollfd: exit\n"));
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
28487c478bd9Sstevel@tonic-gate }
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate /*
28517c478bd9Sstevel@tonic-gate  * The interconnect topology data is obtained from the Kernel Agent
28527c478bd9Sstevel@tonic-gate  * and stored in a memory buffer allocated by this function.  A pointer
28537c478bd9Sstevel@tonic-gate  * to the buffer is stored in the location specified by the caller in
28547c478bd9Sstevel@tonic-gate  * the function argument.  It is the callers responsibility to
28557c478bd9Sstevel@tonic-gate  * call rsm_free_interconnect_topolgy() to free the allocated memory.
28567c478bd9Sstevel@tonic-gate  */
28577c478bd9Sstevel@tonic-gate int
2858*7257d1b4Sraf rsm_get_interconnect_topology(rsm_topology_t **topology_data)
28597c478bd9Sstevel@tonic-gate {
28607c478bd9Sstevel@tonic-gate 	uint32_t		topology_data_size;
28617c478bd9Sstevel@tonic-gate 	rsm_topology_t		*topology_ptr;
28627c478bd9Sstevel@tonic-gate 	int			error;
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
28657c478bd9Sstevel@tonic-gate 	    "rsm_get_interconnect_topology: enter\n"));
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	if (topology_data == NULL)
28687c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_TOPOLOGY_PTR);
28697c478bd9Sstevel@tonic-gate 
28707c478bd9Sstevel@tonic-gate 	*topology_data = NULL;
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate again:
28737c478bd9Sstevel@tonic-gate 	/* obtain the size of the topology data */
28747c478bd9Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_TOPOLOGY_SIZE, &topology_data_size) < 0) {
28757c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
28767c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_TOPOLOGY_SIZE failed\n"));
28777c478bd9Sstevel@tonic-gate 		return (errno);
28787c478bd9Sstevel@tonic-gate 	}
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	/* allocate double-word aligned memory to hold the topology data */
28817c478bd9Sstevel@tonic-gate 	topology_ptr = (rsm_topology_t *)memalign(8, topology_data_size);
28827c478bd9Sstevel@tonic-gate 	if (topology_ptr == NULL) {
28837c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
28847c478bd9Sstevel@tonic-gate 		    "not enough memory\n"));
28857c478bd9Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
28867c478bd9Sstevel@tonic-gate 	}
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	/*
28897c478bd9Sstevel@tonic-gate 	 * Request the topology data.
28907c478bd9Sstevel@tonic-gate 	 * Pass in the size to be used as a check in case
28917c478bd9Sstevel@tonic-gate 	 * the data has grown since the size was obtained - if
28927c478bd9Sstevel@tonic-gate 	 * it has, the errno value will be E2BIG.
28937c478bd9Sstevel@tonic-gate 	 */
28947c478bd9Sstevel@tonic-gate 	topology_ptr->topology_hdr.local_nodeid =
28957c478bd9Sstevel@tonic-gate 	    (rsm_node_id_t)topology_data_size;
28967c478bd9Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_TOPOLOGY_DATA, topology_ptr) < 0) {
28977c478bd9Sstevel@tonic-gate 		error = errno;
28987c478bd9Sstevel@tonic-gate 		free((void *)topology_ptr);
28997c478bd9Sstevel@tonic-gate 		if (error == E2BIG)
29007c478bd9Sstevel@tonic-gate 			goto again;
29017c478bd9Sstevel@tonic-gate 		else {
29027c478bd9Sstevel@tonic-gate 			DBPRINTF((RSM_LIBRARY, RSM_ERR,
29037c478bd9Sstevel@tonic-gate 			    "RSM_IOCTL_TOPOLOGY_DATA failed\n"));
29047c478bd9Sstevel@tonic-gate 			return (error);
29057c478bd9Sstevel@tonic-gate 		}
29067c478bd9Sstevel@tonic-gate 	} else
29077c478bd9Sstevel@tonic-gate 		*topology_data = topology_ptr;
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29107c478bd9Sstevel@tonic-gate 	    " rsm_get_interconnect_topology: exit\n"));
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
29137c478bd9Sstevel@tonic-gate }
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate void
2917*7257d1b4Sraf rsm_free_interconnect_topology(rsm_topology_t *topology_ptr)
29187c478bd9Sstevel@tonic-gate {
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29217c478bd9Sstevel@tonic-gate 	    "rsm_free_interconnect_topology: enter\n"));
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	if (topology_ptr) {
29247c478bd9Sstevel@tonic-gate 		free((void *)topology_ptr);
29257c478bd9Sstevel@tonic-gate 	}
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29287c478bd9Sstevel@tonic-gate 	    "rsm_free_interconnect_topology: exit\n"));
29297c478bd9Sstevel@tonic-gate }
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate int
2932*7257d1b4Sraf rsm_create_localmemory_handle(rsmapi_controller_handle_t cntrl_handle,
29337c478bd9Sstevel@tonic-gate 				rsm_localmemory_handle_t *local_hndl_p,
29347c478bd9Sstevel@tonic-gate 				caddr_t local_vaddr, size_t len)
29357c478bd9Sstevel@tonic-gate {
29367c478bd9Sstevel@tonic-gate 	int e;
29377c478bd9Sstevel@tonic-gate 	rsm_controller_t *cntrl = (rsm_controller_t *)cntrl_handle;
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29407c478bd9Sstevel@tonic-gate 	    "rsm_create_localmemory_handle: enter\n"));
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	if ((size_t)local_vaddr & (PAGESIZE - 1)) {
29437c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29447c478bd9Sstevel@tonic-gate 		    "invalid arguments\n"));
29457c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
29467c478bd9Sstevel@tonic-gate 	}
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	if (!cntrl_handle) {
29497c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29507c478bd9Sstevel@tonic-gate 		    "invalid controller handle\n"));
29517c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
29527c478bd9Sstevel@tonic-gate 	}
29537c478bd9Sstevel@tonic-gate 	if (!local_hndl_p) {
29547c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29557c478bd9Sstevel@tonic-gate 		    "invalid local memory handle pointer\n"));
29567c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_LOCALMEM_HNDL);
29577c478bd9Sstevel@tonic-gate 	}
29587c478bd9Sstevel@tonic-gate 	if (len == 0) {
29597c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29607c478bd9Sstevel@tonic-gate 		    "invalid length\n"));
29617c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_LENGTH);
29627c478bd9Sstevel@tonic-gate 	}
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 	e = cntrl->cntr_segops->rsm_create_localmemory_handle(
29657c478bd9Sstevel@tonic-gate 	    cntrl_handle,
29667c478bd9Sstevel@tonic-gate 	    local_hndl_p,
29677c478bd9Sstevel@tonic-gate 	    local_vaddr,
29687c478bd9Sstevel@tonic-gate 	    len);
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29717c478bd9Sstevel@tonic-gate 	    "rsm_create_localmemory_handle: exit\n"));
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 	return (e);
29747c478bd9Sstevel@tonic-gate }
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate int
2977*7257d1b4Sraf rsm_free_localmemory_handle(rsmapi_controller_handle_t cntrl_handle,
29787c478bd9Sstevel@tonic-gate     rsm_localmemory_handle_t local_handle)
29797c478bd9Sstevel@tonic-gate {
29807c478bd9Sstevel@tonic-gate 	int e;
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate 	rsm_controller_t *cntrl = (rsm_controller_t *)cntrl_handle;
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
29857c478bd9Sstevel@tonic-gate 	    "rsm_free_localmemory_handle: enter\n"));
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 	if (!cntrl_handle) {
29897c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29907c478bd9Sstevel@tonic-gate 		    "invalid controller handle\n"));
29917c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
29927c478bd9Sstevel@tonic-gate 	}
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 	if (!local_handle) {
29957c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
29967c478bd9Sstevel@tonic-gate 		    "invalid localmemory handle\n"));
29977c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_LOCALMEM_HNDL);
29987c478bd9Sstevel@tonic-gate 	}
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	e = cntrl->cntr_segops->rsm_free_localmemory_handle(local_handle);
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
30037c478bd9Sstevel@tonic-gate 	    "rsm_free_localmemory_handle: exit\n"));
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	return (e);
30067c478bd9Sstevel@tonic-gate }
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate int
3009*7257d1b4Sraf rsm_get_segmentid_range(const char *appid, rsm_memseg_id_t *baseid,
30107c478bd9Sstevel@tonic-gate 	uint32_t *length)
30117c478bd9Sstevel@tonic-gate {
30127c478bd9Sstevel@tonic-gate 	char    buf[RSMFILE_BUFSIZE];
30137c478bd9Sstevel@tonic-gate 	char	*s;
30147c478bd9Sstevel@tonic-gate 	char	*fieldv[4];
30157c478bd9Sstevel@tonic-gate 	int	fieldc = 0;
30167c478bd9Sstevel@tonic-gate 	int	found = 0;
30177c478bd9Sstevel@tonic-gate 	int	err = RSMERR_BAD_APPID;
30187c478bd9Sstevel@tonic-gate 	FILE    *fp;
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	if (appid == NULL || baseid == NULL || length == NULL)
30217c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_ADDR);
30227c478bd9Sstevel@tonic-gate 
3023004388ebScasper 	if ((fp = fopen(RSMSEGIDFILE, "rF")) == NULL) {
30247c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
30257c478bd9Sstevel@tonic-gate 		    "cannot open <%s>\n", RSMSEGIDFILE));
30267c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CONF);
30277c478bd9Sstevel@tonic-gate 	}
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	while (s = fgets(buf, RSMFILE_BUFSIZE, fp)) {
30307c478bd9Sstevel@tonic-gate 		fieldc = 0;
30317c478bd9Sstevel@tonic-gate 		while (isspace(*s))	/* skip the leading spaces */
30327c478bd9Sstevel@tonic-gate 			s++;
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate 		if (*s == '#') {	/* comment line - skip it */
30357c478bd9Sstevel@tonic-gate 			continue;
30367c478bd9Sstevel@tonic-gate 		}
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 		/*
30397c478bd9Sstevel@tonic-gate 		 * parse the reserved segid file and
30407c478bd9Sstevel@tonic-gate 		 * set the pointers appropriately.
30417c478bd9Sstevel@tonic-gate 		 * fieldv[0] :  keyword
30427c478bd9Sstevel@tonic-gate 		 * fieldv[1] :  application identifier
30437c478bd9Sstevel@tonic-gate 		 * fieldv[2] :  baseid
30447c478bd9Sstevel@tonic-gate 		 * fieldv[3] :  length
30457c478bd9Sstevel@tonic-gate 		 */
30467c478bd9Sstevel@tonic-gate 		while ((*s != '\n') && (*s != '\0') && (fieldc < 4)) {
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 			while (isspace(*s)) /* skip the leading spaces */
30497c478bd9Sstevel@tonic-gate 				s++;
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 			fieldv[fieldc++] = s;
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 			if (fieldc == 4) {
30547c478bd9Sstevel@tonic-gate 				if (fieldv[3][strlen(fieldv[3])-1] == '\n')
30557c478bd9Sstevel@tonic-gate 					fieldv[3][strlen(fieldv[3])-1] = '\0';
30567c478bd9Sstevel@tonic-gate 				break;
30577c478bd9Sstevel@tonic-gate 			}
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate 			while (*s && !isspace(*s))
30607c478bd9Sstevel@tonic-gate 				++s;	/* move to the next white space */
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 			if (*s)
30637c478bd9Sstevel@tonic-gate 				*s++ = '\0';
30647c478bd9Sstevel@tonic-gate 		}
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 		if (fieldc < 4) {	/* some fields are missing */
30677c478bd9Sstevel@tonic-gate 			err = RSMERR_BAD_CONF;
30687c478bd9Sstevel@tonic-gate 			break;
30697c478bd9Sstevel@tonic-gate 		}
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate 		if (strcasecmp(fieldv[1], appid) == 0) { /* found a match */
30727c478bd9Sstevel@tonic-gate 			if (strcasecmp(fieldv[0], RSMSEG_RESERVED) == 0) {
30737c478bd9Sstevel@tonic-gate 				errno = 0;
30747c478bd9Sstevel@tonic-gate 				*baseid = strtol(fieldv[2], (char **)NULL, 16);
30757c478bd9Sstevel@tonic-gate 				if (errno != 0) {
30767c478bd9Sstevel@tonic-gate 					err = RSMERR_BAD_CONF;
30777c478bd9Sstevel@tonic-gate 					break;
30787c478bd9Sstevel@tonic-gate 				}
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 				errno = 0;
30817c478bd9Sstevel@tonic-gate 				*length = (int)strtol(fieldv[3],
30827c478bd9Sstevel@tonic-gate 				    (char **)NULL, 10);
30837c478bd9Sstevel@tonic-gate 				if (errno != 0) {
30847c478bd9Sstevel@tonic-gate 					err = RSMERR_BAD_CONF;
30857c478bd9Sstevel@tonic-gate 					break;
30867c478bd9Sstevel@tonic-gate 				}
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 				found = 1;
30897c478bd9Sstevel@tonic-gate 			} else {	/* error in format */
30907c478bd9Sstevel@tonic-gate 				err = RSMERR_BAD_CONF;
30917c478bd9Sstevel@tonic-gate 			}
30927c478bd9Sstevel@tonic-gate 			break;
30937c478bd9Sstevel@tonic-gate 		}
30947c478bd9Sstevel@tonic-gate 	}
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 	if (found)
30997c478bd9Sstevel@tonic-gate 		return (RSM_SUCCESS);
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	return (err);
31027c478bd9Sstevel@tonic-gate }
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate static 	int
31057c478bd9Sstevel@tonic-gate _rsm_get_hwaddr(rsmapi_controller_handle_t handle, rsm_node_id_t nodeid,
31067c478bd9Sstevel@tonic-gate     rsm_addr_t *hwaddrp)
31077c478bd9Sstevel@tonic-gate {
31087c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t	msg = {0};
31097c478bd9Sstevel@tonic-gate 	rsm_controller_t *ctrlp;
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
31127c478bd9Sstevel@tonic-gate 	    "_rsm_get_hwaddr: enter\n"));
31137c478bd9Sstevel@tonic-gate 
31147c478bd9Sstevel@tonic-gate 	ctrlp = (rsm_controller_t *)handle;
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	if (ctrlp == NULL) {
31177c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
31187c478bd9Sstevel@tonic-gate 		    "invalid controller handle\n"));
31197c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
31207c478bd9Sstevel@tonic-gate 	}
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	msg.cname = ctrlp->cntr_name;
31237c478bd9Sstevel@tonic-gate 	msg.cname_len = strlen(ctrlp->cntr_name) +1;
31247c478bd9Sstevel@tonic-gate 	msg.cnum = ctrlp->cntr_unit;
31257c478bd9Sstevel@tonic-gate 	msg.nodeid = nodeid;
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_MAP_TO_ADDR, &msg) < 0) {
31287c478bd9Sstevel@tonic-gate 		int error = errno;
31297c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
31307c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_MAP_TO_ADDR failed\n"));
31317c478bd9Sstevel@tonic-gate 		return (error);
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 	*hwaddrp = msg.hwaddr;
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
31377c478bd9Sstevel@tonic-gate 	    "_rsm_get_hwaddr: exit\n"));
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate }
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate static	int
31447c478bd9Sstevel@tonic-gate _rsm_get_nodeid(rsmapi_controller_handle_t handle, rsm_addr_t hwaddr,
31457c478bd9Sstevel@tonic-gate     rsm_node_id_t *nodeidp)
31467c478bd9Sstevel@tonic-gate {
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate 	rsm_ioctlmsg_t	msg = {0};
31497c478bd9Sstevel@tonic-gate 	rsm_controller_t *ctrlp;
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
31527c478bd9Sstevel@tonic-gate 	    "_rsm_get_nodeid: enter\n"));
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	ctrlp = (rsm_controller_t *)handle;
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	if (ctrlp == NULL) {
31577c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
31587c478bd9Sstevel@tonic-gate 		    "invalid arguments\n"));
31597c478bd9Sstevel@tonic-gate 		return (RSMERR_BAD_CTLR_HNDL);
31607c478bd9Sstevel@tonic-gate 	}
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	msg.cname = ctrlp->cntr_name;
31637c478bd9Sstevel@tonic-gate 	msg.cname_len = strlen(ctrlp->cntr_name) +1;
31647c478bd9Sstevel@tonic-gate 	msg.cnum = ctrlp->cntr_unit;
31657c478bd9Sstevel@tonic-gate 	msg.hwaddr = hwaddr;
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	if (ioctl(_rsm_fd, RSM_IOCTL_MAP_TO_NODEID, &msg) < 0) {
31687c478bd9Sstevel@tonic-gate 		int error = errno;
31697c478bd9Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
31707c478bd9Sstevel@tonic-gate 		    "RSM_IOCTL_MAP_TO_NODEID failed\n"));
31717c478bd9Sstevel@tonic-gate 		return (error);
31727c478bd9Sstevel@tonic-gate 	}
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 	*nodeidp = msg.nodeid;
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
31777c478bd9Sstevel@tonic-gate 	    "_rsm_get_nodeid: exit\n"));
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 	return (RSM_SUCCESS);
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate }
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate #ifdef DEBUG
31847c478bd9Sstevel@tonic-gate void
31857c478bd9Sstevel@tonic-gate dbg_printf(int msg_category, int msg_level, char *fmt, ...)
31867c478bd9Sstevel@tonic-gate {
31877c478bd9Sstevel@tonic-gate 	if ((msg_category & rsmlibdbg_category) &&
31887c478bd9Sstevel@tonic-gate 	    (msg_level <= rsmlibdbg_level)) {
31897c478bd9Sstevel@tonic-gate 		va_list arg_list;
31907c478bd9Sstevel@tonic-gate 		va_start(arg_list, fmt);
31917c478bd9Sstevel@tonic-gate 		mutex_lock(&rsmlog_lock);
3192*7257d1b4Sraf 		fprintf(rsmlog_fd, "Thread %d ", thr_self());
31937c478bd9Sstevel@tonic-gate 		vfprintf(rsmlog_fd, fmt, arg_list);
31947c478bd9Sstevel@tonic-gate 		fflush(rsmlog_fd);
31957c478bd9Sstevel@tonic-gate 		mutex_unlock(&rsmlog_lock);
31967c478bd9Sstevel@tonic-gate 		va_end(arg_list);
31977c478bd9Sstevel@tonic-gate 	}
31987c478bd9Sstevel@tonic-gate }
31997c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3200