xref: /onnv-gate/usr/src/lib/librsm/common/rsmgen.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "synonyms.h"
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <strings.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/stat.h>
37*0Sstevel@tonic-gate #include <sys/mman.h>
38*0Sstevel@tonic-gate #include <sys/uio.h>
39*0Sstevel@tonic-gate #include <sys/sysmacros.h>
40*0Sstevel@tonic-gate #include <unistd.h>
41*0Sstevel@tonic-gate #include <errno.h>
42*0Sstevel@tonic-gate #include <assert.h>
43*0Sstevel@tonic-gate #include <malloc.h>
44*0Sstevel@tonic-gate #include <fcntl.h>
45*0Sstevel@tonic-gate #include <dlfcn.h>
46*0Sstevel@tonic-gate #include <sched.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <rsmapi.h>
49*0Sstevel@tonic-gate #include <sys/rsm/rsmndi.h>
50*0Sstevel@tonic-gate #include <rsmlib_in.h>
51*0Sstevel@tonic-gate #include <sys/rsm/rsm.h>
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /* lint -w2 */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate extern rsm_node_id_t rsm_local_nodeid;
56*0Sstevel@tonic-gate extern int loopback_getv(rsm_scat_gath_t *);
57*0Sstevel@tonic-gate extern int loopback_putv(rsm_scat_gath_t *);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static rsm_ndlib_attr_t _rsm_genlib_attr = {
60*0Sstevel@tonic-gate 	B_TRUE,		/* mapping needed for put/get */
61*0Sstevel@tonic-gate 	B_FALSE		/* mapping needed for putv/getv */
62*0Sstevel@tonic-gate };
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static int
65*0Sstevel@tonic-gate __rsm_import_connect(
66*0Sstevel@tonic-gate     rsmapi_controller_handle_t controller, rsm_node_id_t node_id,
67*0Sstevel@tonic-gate     rsm_memseg_id_t segment_id, rsm_permission_t perm,
68*0Sstevel@tonic-gate     rsm_memseg_import_handle_t *im_memseg) {
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
71*0Sstevel@tonic-gate 	    "__rsm_import_connect: enter\n"));
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	controller = controller;
74*0Sstevel@tonic-gate 	node_id = node_id;
75*0Sstevel@tonic-gate 	segment_id = segment_id;
76*0Sstevel@tonic-gate 	perm = perm;
77*0Sstevel@tonic-gate 	im_memseg = im_memseg;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
80*0Sstevel@tonic-gate 	    "__rsm_import_connect: exit\n"));
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static int
86*0Sstevel@tonic-gate __rsm_import_disconnect(rsm_memseg_import_handle_t im_memseg) {
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
89*0Sstevel@tonic-gate 	    "__rsm_import_disconnect: enter\n"));
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	im_memseg = im_memseg;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
94*0Sstevel@tonic-gate 	    "__rsm_import_disconnect: exit\n"));
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate  * XXX: one day we ought to rewrite this stuff based on 64byte atomic access.
101*0Sstevel@tonic-gate  * We can have a new ops vector that makes that assumption.
102*0Sstevel@tonic-gate  */
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate static int
105*0Sstevel@tonic-gate __rsm_get8x8(rsm_memseg_import_handle_t im_memseg, off_t off,
106*0Sstevel@tonic-gate     uint8_t *datap,
107*0Sstevel@tonic-gate     ulong_t rep_cnt,
108*0Sstevel@tonic-gate     boolean_t swap)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
111*0Sstevel@tonic-gate 	uint8_t *data_addr =
112*0Sstevel@tonic-gate 		(uint8_t *)&seg->rsmseg_vaddr[off - seg->rsmseg_mapoffset];
113*0Sstevel@tonic-gate 	uint_t i = 0;
114*0Sstevel@tonic-gate 	int	e;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	swap = swap;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
119*0Sstevel@tonic-gate 	    "__rsm_import_get8x8: enter\n"));
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
122*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
123*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
124*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
125*0Sstevel@tonic-gate 			return (e);
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	for (i = 0; i < rep_cnt; i++) {
130*0Sstevel@tonic-gate 		datap[i] = data_addr[i];
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
134*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
135*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
136*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
137*0Sstevel@tonic-gate 			return (e);
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
142*0Sstevel@tonic-gate 	    "__rsm_import_get8x8: exit\n"));
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate static int
148*0Sstevel@tonic-gate __rsm_get16x16(rsm_memseg_import_handle_t im_memseg, off_t off,
149*0Sstevel@tonic-gate     uint16_t *datap,
150*0Sstevel@tonic-gate     ulong_t rep_cnt,
151*0Sstevel@tonic-gate     boolean_t swap)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
154*0Sstevel@tonic-gate 	uint16_t *data_addr =
155*0Sstevel@tonic-gate 	    /* LINTED */
156*0Sstevel@tonic-gate 	    (uint16_t *)&seg->rsmseg_vaddr[off - seg->rsmseg_mapoffset];
157*0Sstevel@tonic-gate 	uint_t i = 0;
158*0Sstevel@tonic-gate 	int	e;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	swap = swap;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
163*0Sstevel@tonic-gate 	    "__rsm_import_get16x16: enter\n"));
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
166*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
167*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
168*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
169*0Sstevel@tonic-gate 			return (e);
170*0Sstevel@tonic-gate 		}
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	for (i = 0; i < rep_cnt; i++) {
174*0Sstevel@tonic-gate 		datap[i] = data_addr[i];
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
178*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
179*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
180*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
181*0Sstevel@tonic-gate 			return (e);
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
186*0Sstevel@tonic-gate 	    "__rsm_import_get16x16: exit\n"));
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate static int
192*0Sstevel@tonic-gate __rsm_get32x32(rsm_memseg_import_handle_t im_memseg, off_t off,
193*0Sstevel@tonic-gate     uint32_t *datap,
194*0Sstevel@tonic-gate     ulong_t rep_cnt,
195*0Sstevel@tonic-gate     boolean_t swap)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
198*0Sstevel@tonic-gate 	uint32_t *data_addr =
199*0Sstevel@tonic-gate 	    /* LINTED */
200*0Sstevel@tonic-gate 	    (uint32_t *)&seg->rsmseg_vaddr[off - seg->rsmseg_mapoffset];
201*0Sstevel@tonic-gate 	uint_t i = 0;
202*0Sstevel@tonic-gate 	int	e;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	swap = swap;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
207*0Sstevel@tonic-gate 	    "__rsm_import_get32x32: enter\n"));
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
210*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
211*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
212*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
213*0Sstevel@tonic-gate 			return (e);
214*0Sstevel@tonic-gate 		}
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	for (i = 0; i < rep_cnt; i++) {
218*0Sstevel@tonic-gate 		datap[i] = data_addr[i];
219*0Sstevel@tonic-gate 	}
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
222*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
223*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
224*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
225*0Sstevel@tonic-gate 			return (e);
226*0Sstevel@tonic-gate 		}
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
230*0Sstevel@tonic-gate 	    "__rsm_import_get32x32: exit\n"));
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate static int
236*0Sstevel@tonic-gate __rsm_get64x64(rsm_memseg_import_handle_t im_memseg, off_t off,
237*0Sstevel@tonic-gate     uint64_t *datap,
238*0Sstevel@tonic-gate     ulong_t rep_cnt,
239*0Sstevel@tonic-gate     boolean_t swap)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
242*0Sstevel@tonic-gate 	uint64_t *data_addr =
243*0Sstevel@tonic-gate 	    /* LINTED */
244*0Sstevel@tonic-gate 	    (uint64_t *)&seg->rsmseg_vaddr[off - seg->rsmseg_mapoffset];
245*0Sstevel@tonic-gate 	uint_t i = 0;
246*0Sstevel@tonic-gate 	int	e;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	swap = swap;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
251*0Sstevel@tonic-gate 	    "__rsm_import_get64x64: enter\n"));
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
254*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
255*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
256*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
257*0Sstevel@tonic-gate 			return (e);
258*0Sstevel@tonic-gate 		}
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	for (i = 0; i < rep_cnt; i++) {
262*0Sstevel@tonic-gate 		datap[i] = data_addr[i];
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
266*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
267*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
268*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
269*0Sstevel@tonic-gate 			return (e);
270*0Sstevel@tonic-gate 		}
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
274*0Sstevel@tonic-gate 	    "__rsm_import_get64x64: exit\n"));
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	/*
280*0Sstevel@tonic-gate 	 * import side memory segment operations (write access functions):
281*0Sstevel@tonic-gate 	 */
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate /*
284*0Sstevel@tonic-gate  * XXX: Each one of the following cases ought to be a separate function loaded
285*0Sstevel@tonic-gate  * into a segment access ops vector. We determine the correct function at
286*0Sstevel@tonic-gate  * segment connect time. When a new controller is register, we can decode
287*0Sstevel@tonic-gate  * it's direct_access_size attribute and load the correct function. For
288*0Sstevel@tonic-gate  * loop back we need to create a special ops vector that bypasses all of
289*0Sstevel@tonic-gate  * this stuff.
290*0Sstevel@tonic-gate  *
291*0Sstevel@tonic-gate  * XXX: We need to create a special interrupt queue for the library to handle
292*0Sstevel@tonic-gate  * partial writes in the remote process.
293*0Sstevel@tonic-gate  */
294*0Sstevel@tonic-gate static int
295*0Sstevel@tonic-gate __rsm_put8x8(rsm_memseg_import_handle_t im_memseg, off_t off,
296*0Sstevel@tonic-gate     uint8_t *datap,
297*0Sstevel@tonic-gate     ulong_t rep_cnt,
298*0Sstevel@tonic-gate     boolean_t swap)
299*0Sstevel@tonic-gate {
300*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
301*0Sstevel@tonic-gate 	uint8_t *data_addr =
302*0Sstevel@tonic-gate 		(uint8_t *)&seg->rsmseg_vaddr[off - seg->rsmseg_mapoffset];
303*0Sstevel@tonic-gate 	uint_t i = 0;
304*0Sstevel@tonic-gate 	int	e;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	swap = swap;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
309*0Sstevel@tonic-gate 	    "__rsm_put8x8: enter\n"));
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
312*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
313*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
314*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
315*0Sstevel@tonic-gate 			return (e);
316*0Sstevel@tonic-gate 		}
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	for (i = 0; i < rep_cnt; i++) {
320*0Sstevel@tonic-gate 		data_addr[i] = datap[i];
321*0Sstevel@tonic-gate 	}
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
324*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
325*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
326*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
327*0Sstevel@tonic-gate 			return (e);
328*0Sstevel@tonic-gate 		}
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
332*0Sstevel@tonic-gate 	    "__rsm_put8x8: exit\n"));
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate static int
338*0Sstevel@tonic-gate __rsm_put16x16(rsm_memseg_import_handle_t im_memseg, off_t off,
339*0Sstevel@tonic-gate     uint16_t *datap,
340*0Sstevel@tonic-gate     ulong_t rep_cnt,
341*0Sstevel@tonic-gate     boolean_t swap)
342*0Sstevel@tonic-gate {
343*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
344*0Sstevel@tonic-gate 	uint16_t *data_addr =
345*0Sstevel@tonic-gate 	    /* LINTED */
346*0Sstevel@tonic-gate 	    (uint16_t *)&seg->rsmseg_vaddr[off - seg->rsmseg_mapoffset];
347*0Sstevel@tonic-gate 	uint_t i = 0;
348*0Sstevel@tonic-gate 	int	e;
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	swap = swap;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
353*0Sstevel@tonic-gate 	    "__rsm_put16x16: enter\n"));
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
356*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
357*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
358*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
359*0Sstevel@tonic-gate 			return (e);
360*0Sstevel@tonic-gate 		}
361*0Sstevel@tonic-gate 	}
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	for (i = 0; i < rep_cnt; i++) {
364*0Sstevel@tonic-gate 		data_addr[i] = datap[i];
365*0Sstevel@tonic-gate 	}
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
368*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
369*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
370*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
371*0Sstevel@tonic-gate 			return (e);
372*0Sstevel@tonic-gate 		}
373*0Sstevel@tonic-gate 	}
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
376*0Sstevel@tonic-gate 	    "__rsm_put16x16: exit\n"));
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate static int
382*0Sstevel@tonic-gate __rsm_put32x32(rsm_memseg_import_handle_t im_memseg, off_t off,
383*0Sstevel@tonic-gate     uint32_t *datap,
384*0Sstevel@tonic-gate     ulong_t rep_cnt,
385*0Sstevel@tonic-gate     boolean_t swap)
386*0Sstevel@tonic-gate {
387*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
388*0Sstevel@tonic-gate 	uint32_t *data_addr =
389*0Sstevel@tonic-gate 	    /* LINTED */
390*0Sstevel@tonic-gate 	    (uint32_t *)&seg->rsmseg_vaddr[off - seg->rsmseg_mapoffset];
391*0Sstevel@tonic-gate 	uint_t i = 0;
392*0Sstevel@tonic-gate 	int	e;
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	swap = swap;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
397*0Sstevel@tonic-gate 	    "__rsm_put32x32: enter\n"));
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
400*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
401*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
402*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
403*0Sstevel@tonic-gate 			return (e);
404*0Sstevel@tonic-gate 		}
405*0Sstevel@tonic-gate 	}
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	for (i = 0; i < rep_cnt; i++) {
408*0Sstevel@tonic-gate 		data_addr[i] = datap[i];
409*0Sstevel@tonic-gate 	}
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
412*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
413*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
414*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
415*0Sstevel@tonic-gate 			return (e);
416*0Sstevel@tonic-gate 		}
417*0Sstevel@tonic-gate 	}
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
420*0Sstevel@tonic-gate 	    "__rsm_put32x32: exit\n"));
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
423*0Sstevel@tonic-gate }
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate static int
426*0Sstevel@tonic-gate __rsm_put64x64(rsm_memseg_import_handle_t im_memseg, off_t off,
427*0Sstevel@tonic-gate     uint64_t *datap,
428*0Sstevel@tonic-gate     ulong_t rep_cnt,
429*0Sstevel@tonic-gate     boolean_t swap)
430*0Sstevel@tonic-gate {
431*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
432*0Sstevel@tonic-gate 	uint64_t *data_addr =
433*0Sstevel@tonic-gate 	    /* LINTED */
434*0Sstevel@tonic-gate 	    (uint64_t *)&seg->rsmseg_vaddr[off - seg->rsmseg_mapoffset];
435*0Sstevel@tonic-gate 	uint_t i = 0;
436*0Sstevel@tonic-gate 	int	e;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	swap = swap;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
441*0Sstevel@tonic-gate 	    "__rsm_put64x64: enter\n"));
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
444*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
445*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
446*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
447*0Sstevel@tonic-gate 			return (e);
448*0Sstevel@tonic-gate 		}
449*0Sstevel@tonic-gate 	}
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	for (i = 0; i < rep_cnt; i++) {
452*0Sstevel@tonic-gate 		data_addr[i] = datap[i];
453*0Sstevel@tonic-gate 	}
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
456*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
457*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
458*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
459*0Sstevel@tonic-gate 			return (e);
460*0Sstevel@tonic-gate 		}
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
464*0Sstevel@tonic-gate 	    "__rsm_put64x64: exit\n"));
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
467*0Sstevel@tonic-gate }
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate static int
470*0Sstevel@tonic-gate __rsm_get(rsm_memseg_import_handle_t im_memseg, off_t offset, void *dst_addr,
471*0Sstevel@tonic-gate     size_t length)
472*0Sstevel@tonic-gate {
473*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
474*0Sstevel@tonic-gate 	int		e;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
477*0Sstevel@tonic-gate 	    "__rsm_get: enter\n"));
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
480*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
481*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
482*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
483*0Sstevel@tonic-gate 			return (e);
484*0Sstevel@tonic-gate 		}
485*0Sstevel@tonic-gate 	}
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	(void) bcopy(seg->rsmseg_vaddr + offset - seg->rsmseg_mapoffset,
488*0Sstevel@tonic-gate 	    dst_addr, length);
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
491*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
492*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
493*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
494*0Sstevel@tonic-gate 			return (e);
495*0Sstevel@tonic-gate 		}
496*0Sstevel@tonic-gate 	}
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
499*0Sstevel@tonic-gate 	    "__rsm_get: exit\n"));
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate static int
505*0Sstevel@tonic-gate __rsm_getv(rsm_scat_gath_t *sg_io)
506*0Sstevel@tonic-gate {
507*0Sstevel@tonic-gate 	rsm_iovec_t 	*iovec = sg_io->iovec;
508*0Sstevel@tonic-gate 	rsmka_iovec_t	ka_iovec_arr[RSM_MAX_IOVLEN];
509*0Sstevel@tonic-gate 	rsmka_iovec_t	*ka_iovec, *ka_iovec_start;
510*0Sstevel@tonic-gate 	rsmka_iovec_t	l_iovec_arr[RSM_MAX_IOVLEN];
511*0Sstevel@tonic-gate 	rsmka_iovec_t	*l_iovec, *l_iovec_start;
512*0Sstevel@tonic-gate 	rsmseg_handle_t *im_seg_hndl = (rsmseg_handle_t *)sg_io->remote_handle;
513*0Sstevel@tonic-gate 	rsmseg_handle_t *seg_hndl;
514*0Sstevel@tonic-gate 	int iovec_size = sizeof (rsmka_iovec_t) * sg_io->io_request_count;
515*0Sstevel@tonic-gate 	int e, i;
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
518*0Sstevel@tonic-gate 	    "__rsm_getv: enter\n"));
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	/*
521*0Sstevel@tonic-gate 	 * Use loopback for single node operations.
522*0Sstevel@tonic-gate 	 * replace local handles with virtual addresses
523*0Sstevel@tonic-gate 	 */
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	if (im_seg_hndl->rsmseg_nodeid == rsm_local_nodeid) {
526*0Sstevel@tonic-gate 		/*
527*0Sstevel@tonic-gate 		 * To use the loopback optimization map the segment
528*0Sstevel@tonic-gate 		 * here implicitly.
529*0Sstevel@tonic-gate 		 */
530*0Sstevel@tonic-gate 		if (im_seg_hndl->rsmseg_state == IMPORT_CONNECT) {
531*0Sstevel@tonic-gate 			caddr_t	va;
532*0Sstevel@tonic-gate 			va = mmap(NULL, im_seg_hndl->rsmseg_size,
533*0Sstevel@tonic-gate 			    PROT_READ|PROT_WRITE,
534*0Sstevel@tonic-gate 			    MAP_SHARED|MAP_NORESERVE,
535*0Sstevel@tonic-gate 			    im_seg_hndl->rsmseg_fd, 0);
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 			if (va == MAP_FAILED) {
538*0Sstevel@tonic-gate 				DBPRINTF((RSM_LIBRARY, RSM_ERR,
539*0Sstevel@tonic-gate 				    "implicit map failed:%d\n", errno));
540*0Sstevel@tonic-gate 				if (errno == EINVAL)
541*0Sstevel@tonic-gate 					return (RSMERR_BAD_MEM_ALIGNMENT);
542*0Sstevel@tonic-gate 				else if (errno == ENOMEM || errno == ENXIO ||
543*0Sstevel@tonic-gate 					errno == EOVERFLOW)
544*0Sstevel@tonic-gate 						return (RSMERR_BAD_LENGTH);
545*0Sstevel@tonic-gate 				else if (errno == EAGAIN)
546*0Sstevel@tonic-gate 					return (RSMERR_INSUFFICIENT_RESOURCES);
547*0Sstevel@tonic-gate 				else
548*0Sstevel@tonic-gate 					return (errno);
549*0Sstevel@tonic-gate 			}
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_vaddr = va;
552*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_maplen = im_seg_hndl->rsmseg_size;
553*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_mapoffset = 0;
554*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_state = IMPORT_MAP;
555*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_flags |= RSM_IMPLICIT_MAP;
556*0Sstevel@tonic-gate 		}
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 		if (sg_io->io_request_count > RSM_MAX_IOVLEN)
559*0Sstevel@tonic-gate 			l_iovec_start = l_iovec = malloc(iovec_size);
560*0Sstevel@tonic-gate 		else
561*0Sstevel@tonic-gate 			l_iovec_start = l_iovec = l_iovec_arr;
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 		bcopy((caddr_t)iovec, (caddr_t)l_iovec, iovec_size);
564*0Sstevel@tonic-gate 		for (i = 0; i < sg_io->io_request_count; i++) {
565*0Sstevel@tonic-gate 			if (l_iovec->io_type == RSM_HANDLE_TYPE) {
566*0Sstevel@tonic-gate 				/* Get the surrogate export segment handle */
567*0Sstevel@tonic-gate 				seg_hndl = (rsmseg_handle_t *)
568*0Sstevel@tonic-gate 				    l_iovec->local.handle;
569*0Sstevel@tonic-gate 				l_iovec->local.vaddr = seg_hndl->rsmseg_vaddr;
570*0Sstevel@tonic-gate 				l_iovec->io_type = RSM_VA_TYPE;
571*0Sstevel@tonic-gate 			}
572*0Sstevel@tonic-gate 			l_iovec++;
573*0Sstevel@tonic-gate 		}
574*0Sstevel@tonic-gate 		sg_io->iovec = (rsm_iovec_t *)l_iovec_start;
575*0Sstevel@tonic-gate 		e = loopback_getv(sg_io);
576*0Sstevel@tonic-gate 		sg_io->iovec = iovec;
577*0Sstevel@tonic-gate 		if (sg_io->io_request_count > RSM_MAX_IOVLEN)
578*0Sstevel@tonic-gate 			free(l_iovec_start);
579*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
580*0Sstevel@tonic-gate 		    "__rsm_getv: exit\n"));
581*0Sstevel@tonic-gate 		return (e);
582*0Sstevel@tonic-gate 	}
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 	/* for the Kernel Agent, replace local handles with segment ids */
585*0Sstevel@tonic-gate 	if (sg_io->io_request_count > RSM_MAX_IOVLEN)
586*0Sstevel@tonic-gate 		ka_iovec_start = ka_iovec = malloc(iovec_size);
587*0Sstevel@tonic-gate 	else
588*0Sstevel@tonic-gate 		ka_iovec_start = ka_iovec = ka_iovec_arr;
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	bcopy((caddr_t)iovec, (caddr_t)ka_iovec, iovec_size);
591*0Sstevel@tonic-gate 	for (i = 0; i < sg_io->io_request_count; i++) {
592*0Sstevel@tonic-gate 		if (ka_iovec->io_type == RSM_HANDLE_TYPE) {
593*0Sstevel@tonic-gate 			seg_hndl = (rsmseg_handle_t *)ka_iovec->local.handle;
594*0Sstevel@tonic-gate 			ka_iovec->local.segid = seg_hndl->rsmseg_keyid;
595*0Sstevel@tonic-gate 		}
596*0Sstevel@tonic-gate 		ka_iovec++;
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	sg_io->iovec = (rsm_iovec_t *)ka_iovec_start;
600*0Sstevel@tonic-gate 	e = ioctl(im_seg_hndl->rsmseg_fd, RSM_IOCTL_GETV, sg_io);
601*0Sstevel@tonic-gate 	sg_io->iovec = iovec;
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 	if (sg_io->io_request_count > RSM_MAX_IOVLEN)
604*0Sstevel@tonic-gate 		free(ka_iovec_start);
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	if (e < 0) {
607*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
608*0Sstevel@tonic-gate 		    " RSM_IOCTL_GETV failed\n"));
609*0Sstevel@tonic-gate 		return (errno);
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
613*0Sstevel@tonic-gate 	    "__rsm_getv: exit\n"));
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
616*0Sstevel@tonic-gate }
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate static int
620*0Sstevel@tonic-gate __rsm_put(rsm_memseg_import_handle_t im_memseg, off_t offset, void *src_addr,
621*0Sstevel@tonic-gate     size_t length)
622*0Sstevel@tonic-gate {
623*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
624*0Sstevel@tonic-gate 	int		e;
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
627*0Sstevel@tonic-gate 	    "__rsm_put: enter\n"));
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
630*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_open_barrier(
631*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
632*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
633*0Sstevel@tonic-gate 			return (e);
634*0Sstevel@tonic-gate 		}
635*0Sstevel@tonic-gate 	}
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	bcopy(src_addr, seg->rsmseg_vaddr + offset - seg->rsmseg_mapoffset,
638*0Sstevel@tonic-gate 		length);
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	if (seg->rsmseg_barmode == RSM_BARRIER_MODE_IMPLICIT) {
641*0Sstevel@tonic-gate 		e = seg->rsmseg_ops->rsm_memseg_import_close_barrier(
642*0Sstevel@tonic-gate 		    (rsm_barrier_handle_t)seg->rsmseg_barrier);
643*0Sstevel@tonic-gate 		if (e != RSM_SUCCESS) {
644*0Sstevel@tonic-gate 			return (e);
645*0Sstevel@tonic-gate 		}
646*0Sstevel@tonic-gate 	}
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
649*0Sstevel@tonic-gate 	    "__rsm_put: exit\n"));
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
652*0Sstevel@tonic-gate }
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate static int
655*0Sstevel@tonic-gate __rsm_putv(rsm_scat_gath_t *sg_io)
656*0Sstevel@tonic-gate {
657*0Sstevel@tonic-gate 	rsm_iovec_t 	*iovec = sg_io->iovec;
658*0Sstevel@tonic-gate 	rsmka_iovec_t	ka_iovec_arr[RSM_MAX_IOVLEN];
659*0Sstevel@tonic-gate 	rsmka_iovec_t	*ka_iovec, *ka_iovec_start;
660*0Sstevel@tonic-gate 	rsmka_iovec_t	l_iovec_arr[RSM_MAX_IOVLEN];
661*0Sstevel@tonic-gate 	rsmka_iovec_t	*l_iovec, *l_iovec_start;
662*0Sstevel@tonic-gate 	rsmseg_handle_t *im_seg_hndl = (rsmseg_handle_t *)sg_io->remote_handle;
663*0Sstevel@tonic-gate 	rsmseg_handle_t *seg_hndl;
664*0Sstevel@tonic-gate 	int iovec_size = sizeof (rsmka_iovec_t) * sg_io->io_request_count;
665*0Sstevel@tonic-gate 	int e, i;
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
668*0Sstevel@tonic-gate 	    "__rsm_putv: enter\n"));
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate 	/*
671*0Sstevel@tonic-gate 	 * Use loopback for single node operations.
672*0Sstevel@tonic-gate 	 * replace local handles with virtual addresses
673*0Sstevel@tonic-gate 	 */
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	if (im_seg_hndl->rsmseg_nodeid == rsm_local_nodeid) {
676*0Sstevel@tonic-gate 		/*
677*0Sstevel@tonic-gate 		 * To use the loopback optimization map the segment
678*0Sstevel@tonic-gate 		 * here implicitly.
679*0Sstevel@tonic-gate 		 */
680*0Sstevel@tonic-gate 		if (im_seg_hndl->rsmseg_state == IMPORT_CONNECT) {
681*0Sstevel@tonic-gate 			caddr_t	va;
682*0Sstevel@tonic-gate 			va = mmap(NULL, im_seg_hndl->rsmseg_size,
683*0Sstevel@tonic-gate 			    PROT_READ|PROT_WRITE,
684*0Sstevel@tonic-gate 			    MAP_SHARED|MAP_NORESERVE,
685*0Sstevel@tonic-gate 			    im_seg_hndl->rsmseg_fd, 0);
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 			if (va == MAP_FAILED) {
688*0Sstevel@tonic-gate 				DBPRINTF((RSM_LIBRARY, RSM_ERR,
689*0Sstevel@tonic-gate 				    "implicit map failed:%d\n", errno));
690*0Sstevel@tonic-gate 				if (errno == EINVAL)
691*0Sstevel@tonic-gate 					return (RSMERR_BAD_MEM_ALIGNMENT);
692*0Sstevel@tonic-gate 				else if (errno == ENOMEM || errno == ENXIO ||
693*0Sstevel@tonic-gate 					errno == EOVERFLOW)
694*0Sstevel@tonic-gate 						return (RSMERR_BAD_LENGTH);
695*0Sstevel@tonic-gate 				else if (errno == EAGAIN)
696*0Sstevel@tonic-gate 					return (RSMERR_INSUFFICIENT_RESOURCES);
697*0Sstevel@tonic-gate 				else
698*0Sstevel@tonic-gate 					return (errno);
699*0Sstevel@tonic-gate 			}
700*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_vaddr = va;
701*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_maplen = im_seg_hndl->rsmseg_size;
702*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_mapoffset = 0;
703*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_state = IMPORT_MAP;
704*0Sstevel@tonic-gate 			im_seg_hndl->rsmseg_flags |= RSM_IMPLICIT_MAP;
705*0Sstevel@tonic-gate 		}
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 		if (sg_io->io_request_count > RSM_MAX_IOVLEN)
708*0Sstevel@tonic-gate 			l_iovec_start = l_iovec = malloc(iovec_size);
709*0Sstevel@tonic-gate 		else
710*0Sstevel@tonic-gate 			l_iovec_start = l_iovec = l_iovec_arr;
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 		bcopy((caddr_t)iovec, (caddr_t)l_iovec, iovec_size);
713*0Sstevel@tonic-gate 		for (i = 0; i < sg_io->io_request_count; i++) {
714*0Sstevel@tonic-gate 			if (l_iovec->io_type == RSM_HANDLE_TYPE) {
715*0Sstevel@tonic-gate 				/* Get the surrogate export segment handle */
716*0Sstevel@tonic-gate 				seg_hndl = (rsmseg_handle_t *)
717*0Sstevel@tonic-gate 							l_iovec->local.handle;
718*0Sstevel@tonic-gate 				l_iovec->local.vaddr = seg_hndl->rsmseg_vaddr;
719*0Sstevel@tonic-gate 				l_iovec->io_type = RSM_VA_TYPE;
720*0Sstevel@tonic-gate 			}
721*0Sstevel@tonic-gate 			l_iovec++;
722*0Sstevel@tonic-gate 		}
723*0Sstevel@tonic-gate 		sg_io->iovec = (rsm_iovec_t *)l_iovec_start;
724*0Sstevel@tonic-gate 		e = loopback_putv(sg_io);
725*0Sstevel@tonic-gate 		sg_io->iovec = iovec;
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 		if (sg_io->io_request_count > RSM_MAX_IOVLEN)
728*0Sstevel@tonic-gate 			free(l_iovec_start);
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
731*0Sstevel@tonic-gate 		    "__rsm_putv: exit\n"));
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 		return (e);
735*0Sstevel@tonic-gate 	}
736*0Sstevel@tonic-gate 
737*0Sstevel@tonic-gate 	/* for the Kernel Agent, replace local handles with segment ids */
738*0Sstevel@tonic-gate 	if (sg_io->io_request_count > RSM_MAX_IOVLEN)
739*0Sstevel@tonic-gate 		ka_iovec_start = ka_iovec = malloc(iovec_size);
740*0Sstevel@tonic-gate 	else
741*0Sstevel@tonic-gate 		ka_iovec_start = ka_iovec = ka_iovec_arr;
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	bcopy((caddr_t)iovec, (caddr_t)ka_iovec, iovec_size);
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	for (i = 0; i < sg_io->io_request_count; i++) {
746*0Sstevel@tonic-gate 		if (ka_iovec->io_type == RSM_HANDLE_TYPE) {
747*0Sstevel@tonic-gate 			seg_hndl = (rsmseg_handle_t *)ka_iovec->local.handle;
748*0Sstevel@tonic-gate 			ka_iovec->local.segid = seg_hndl->rsmseg_keyid;
749*0Sstevel@tonic-gate 		}
750*0Sstevel@tonic-gate 		ka_iovec++;
751*0Sstevel@tonic-gate 	}
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 	sg_io->iovec = (rsm_iovec_t *)ka_iovec_start;
754*0Sstevel@tonic-gate 	e = ioctl(im_seg_hndl->rsmseg_fd, RSM_IOCTL_PUTV, sg_io);
755*0Sstevel@tonic-gate 	sg_io->iovec = iovec;
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	if (sg_io->io_request_count > RSM_MAX_IOVLEN)
758*0Sstevel@tonic-gate 		free(ka_iovec_start);
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	if (e < 0) {
761*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
762*0Sstevel@tonic-gate 		    " RSM_IOCTL_PUTV failed\n"));
763*0Sstevel@tonic-gate 		return (errno);
764*0Sstevel@tonic-gate 	}
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
767*0Sstevel@tonic-gate 	    "__rsm_putv: exit\n"));
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
770*0Sstevel@tonic-gate }
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	/*
773*0Sstevel@tonic-gate 	 * import side memory segment operations (barriers):
774*0Sstevel@tonic-gate 	 */
775*0Sstevel@tonic-gate static int
776*0Sstevel@tonic-gate __rsm_memseg_import_init_barrier(rsm_memseg_import_handle_t im_memseg,
777*0Sstevel@tonic-gate     rsm_barrier_type_t type,
778*0Sstevel@tonic-gate     rsm_barrier_handle_t barrier)
779*0Sstevel@tonic-gate {
780*0Sstevel@tonic-gate 	rsmseg_handle_t *seg = (rsmseg_handle_t *)im_memseg;
781*0Sstevel@tonic-gate 	rsmgenbar_handle_t *bar = (rsmgenbar_handle_t *)barrier;
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
784*0Sstevel@tonic-gate 	    ""
785*0Sstevel@tonic-gate 	    "__rsm_memseg_import_init_barrier: enter\n"));
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 	type = type;
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate 	if (!seg) {
790*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
791*0Sstevel@tonic-gate 		    "invalid segment handle\n"));
792*0Sstevel@tonic-gate 		return (RSMERR_BAD_SEG_HNDL);
793*0Sstevel@tonic-gate 	}
794*0Sstevel@tonic-gate 	if (!bar) {
795*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
796*0Sstevel@tonic-gate 		    "invalid barrier handle\n"));
797*0Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
798*0Sstevel@tonic-gate 	}
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate 	/* XXX: fix later. We only support span-of-node barriers */
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 	bar->rsmgenbar_data = (rsm_barrier_t *)malloc(sizeof (rsm_barrier_t));
803*0Sstevel@tonic-gate 	if (bar->rsmgenbar_data == NULL) {
804*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
805*0Sstevel@tonic-gate 		    "not enough memory\n"));
806*0Sstevel@tonic-gate 		return (RSMERR_INSUFFICIENT_MEM);
807*0Sstevel@tonic-gate 	}
808*0Sstevel@tonic-gate 	bar->rsmgenbar_seg = seg;
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
811*0Sstevel@tonic-gate 	    "__rsm_memseg_import_init_barrier: exit\n"));
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
814*0Sstevel@tonic-gate }
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate static int
817*0Sstevel@tonic-gate __rsm_memseg_import_open_barrier(rsm_barrier_handle_t barrier)
818*0Sstevel@tonic-gate {
819*0Sstevel@tonic-gate 	rsmgenbar_handle_t *bar = (rsmgenbar_handle_t *)barrier;
820*0Sstevel@tonic-gate 	rsmseg_handle_t *seg;
821*0Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
824*0Sstevel@tonic-gate 	    "__rsm_memseg_import_open_barrier: enter\n"));
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 	if (!bar) {
827*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
828*0Sstevel@tonic-gate 		    "invalid barrier pointer\n"));
829*0Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
830*0Sstevel@tonic-gate 	}
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 	if ((seg = bar->rsmgenbar_seg) == 0) {
833*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
834*0Sstevel@tonic-gate 		    "uninitialized barrier\n"));
835*0Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
836*0Sstevel@tonic-gate 	}
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate /* lint -save -e718 -e746 */
839*0Sstevel@tonic-gate 	msg.bar = *(bar->rsmgenbar_data);
840*0Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd,
841*0Sstevel@tonic-gate 	    RSM_IOCTL_BAR_OPEN, &msg) < 0) {
842*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
843*0Sstevel@tonic-gate 		    " RSM_IOCTL_BAR_OPEN failed\n"));
844*0Sstevel@tonic-gate /* lint -restore */
845*0Sstevel@tonic-gate 		return (RSMERR_BARRIER_OPEN_FAILED);
846*0Sstevel@tonic-gate 	}
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
849*0Sstevel@tonic-gate 	    "__rsm_memseg_import_open_barrier: exit\n"));
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
852*0Sstevel@tonic-gate }
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate static int
855*0Sstevel@tonic-gate __rsm_memseg_import_order_barrier(rsm_barrier_handle_t barrier)
856*0Sstevel@tonic-gate {
857*0Sstevel@tonic-gate 	rsmgenbar_handle_t *bar = (rsmgenbar_handle_t *)barrier;
858*0Sstevel@tonic-gate 	rsmseg_handle_t *seg;
859*0Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
862*0Sstevel@tonic-gate 	    "__rsm_memseg_import_order_barrier: enter\n"));
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 	if (!bar) {
865*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
866*0Sstevel@tonic-gate 		    "invalid barrier\n"));
867*0Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
868*0Sstevel@tonic-gate 	}
869*0Sstevel@tonic-gate 	if ((seg = bar->rsmgenbar_seg) == 0) {
870*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
871*0Sstevel@tonic-gate 		    "uninitialized barrier\n"));
872*0Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
873*0Sstevel@tonic-gate 	}
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 	msg.bar = *(bar->rsmgenbar_data);
876*0Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_BAR_ORDER, &msg) < 0) {
877*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
878*0Sstevel@tonic-gate 		    "RSM_IOCTL_BAR_ORDER failed\n"));
879*0Sstevel@tonic-gate 		return (RSMERR_BARRIER_FAILURE);
880*0Sstevel@tonic-gate 	}
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
883*0Sstevel@tonic-gate 	    "__rsm_memseg_import_order_barrier: exit\n"));
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
886*0Sstevel@tonic-gate }
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate static int
889*0Sstevel@tonic-gate __rsm_memseg_import_close_barrier(rsm_barrier_handle_t barrier)
890*0Sstevel@tonic-gate {
891*0Sstevel@tonic-gate 	rsmgenbar_handle_t *bar = (rsmgenbar_handle_t *)barrier;
892*0Sstevel@tonic-gate 	rsmseg_handle_t *seg;
893*0Sstevel@tonic-gate 	rsm_ioctlmsg_t msg;
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
896*0Sstevel@tonic-gate 	    "__rsm_memseg_import_close_barrier: enter\n"));
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 	if (!bar) {
899*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
900*0Sstevel@tonic-gate 		    "invalid barrier\n"));
901*0Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
902*0Sstevel@tonic-gate 	}
903*0Sstevel@tonic-gate 	if ((seg = bar->rsmgenbar_seg) == 0) {
904*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
905*0Sstevel@tonic-gate 		    "uninitialized barrier\n"));
906*0Sstevel@tonic-gate 		return (RSMERR_BARRIER_UNINITIALIZED);
907*0Sstevel@tonic-gate 	}
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	msg.bar = *(bar->rsmgenbar_data);
910*0Sstevel@tonic-gate 	if (ioctl(seg->rsmseg_fd, RSM_IOCTL_BAR_CLOSE, &msg) < 0) {
911*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
912*0Sstevel@tonic-gate 		    " RSM_IOCTL_BAR_CLOSE failed\n"));
913*0Sstevel@tonic-gate 		return (RSMERR_BARRIER_FAILURE);
914*0Sstevel@tonic-gate 	}
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
917*0Sstevel@tonic-gate 	    "__rsm_memseg_import_close_barrier: exit\n"));
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
920*0Sstevel@tonic-gate }
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate static int
923*0Sstevel@tonic-gate __rsm_memseg_import_destroy_barrier(rsm_barrier_handle_t barrier)
924*0Sstevel@tonic-gate {
925*0Sstevel@tonic-gate 	rsmgenbar_handle_t *bar = (rsmgenbar_handle_t *)barrier;
926*0Sstevel@tonic-gate 
927*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
928*0Sstevel@tonic-gate 	    "__rsm_memseg_import_destroy_barrier: enter\n"));
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 	if (!bar) {
931*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
932*0Sstevel@tonic-gate 		    "invalid barrier\n"));
933*0Sstevel@tonic-gate 		return (RSMERR_BAD_BARRIER_PTR);
934*0Sstevel@tonic-gate 	}
935*0Sstevel@tonic-gate 
936*0Sstevel@tonic-gate 	free((void *) bar->rsmgenbar_data);
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
939*0Sstevel@tonic-gate 	    "__rsm_memseg_import_destroy_barrier: exit\n"));
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
942*0Sstevel@tonic-gate }
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate /* lint -w1 */
945*0Sstevel@tonic-gate static int
946*0Sstevel@tonic-gate __rsm_memseg_import_get_mode(rsm_memseg_import_handle_t im_memseg,
947*0Sstevel@tonic-gate     rsm_barrier_mode_t *mode)
948*0Sstevel@tonic-gate {
949*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
950*0Sstevel@tonic-gate 	    "__rsm_memseg_import_get_mode: enter\n"));
951*0Sstevel@tonic-gate 
952*0Sstevel@tonic-gate 	im_memseg = im_memseg; mode = mode;
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
955*0Sstevel@tonic-gate 	    "__rsm_memseg_import_get_mode: exit\n"));
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
958*0Sstevel@tonic-gate }
959*0Sstevel@tonic-gate static int
960*0Sstevel@tonic-gate __rsm_memseg_import_set_mode(rsm_memseg_import_handle_t im_memseg,
961*0Sstevel@tonic-gate 				rsm_barrier_mode_t mode)
962*0Sstevel@tonic-gate {
963*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
964*0Sstevel@tonic-gate 	    "__rsm_memseg_import_set_mode: enter\n"));
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate 	im_memseg = im_memseg; mode = mode;
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
969*0Sstevel@tonic-gate 	    "__rsm_memseg_import_set_mode: exit\n"));
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
972*0Sstevel@tonic-gate }
973*0Sstevel@tonic-gate 
974*0Sstevel@tonic-gate static int
975*0Sstevel@tonic-gate __rsm_create_memory_handle(rsmapi_controller_handle_t controller,
976*0Sstevel@tonic-gate     rsm_localmemory_handle_t *local_hndl_p,
977*0Sstevel@tonic-gate     caddr_t local_va, size_t len)
978*0Sstevel@tonic-gate {
979*0Sstevel@tonic-gate 	rsm_memseg_export_handle_t memseg;
980*0Sstevel@tonic-gate 	rsmapi_access_entry_t	acl[1];
981*0Sstevel@tonic-gate 	rsm_memseg_id_t segid = 0;
982*0Sstevel@tonic-gate 	size_t size;
983*0Sstevel@tonic-gate 	int e;
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate 
986*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
987*0Sstevel@tonic-gate 	    "__rsm_create_memory_handle: enter\n"));
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate 	/*
990*0Sstevel@tonic-gate 	 * create a surrogate segment (local memory will be locked down).
991*0Sstevel@tonic-gate 	 */
992*0Sstevel@tonic-gate 	size =  roundup(len, PAGESIZE);
993*0Sstevel@tonic-gate 	e = rsm_memseg_export_create(controller, &memseg,
994*0Sstevel@tonic-gate 	    (void *)local_va, size,
995*0Sstevel@tonic-gate 	    RSM_ALLOW_REBIND);
996*0Sstevel@tonic-gate 	if (e != RSM_SUCCESS) {
997*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
998*0Sstevel@tonic-gate 		    "export create failed\n"));
999*0Sstevel@tonic-gate 		return (e);
1000*0Sstevel@tonic-gate 	}
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 	/*
1003*0Sstevel@tonic-gate 	 * Publish the segment to the local node only.  If the segment
1004*0Sstevel@tonic-gate 	 * length is very large then don't publish to the adapter driver
1005*0Sstevel@tonic-gate 	 * because that will consume too much DVMA space - this is indicated
1006*0Sstevel@tonic-gate 	 * to the Kernel Agent using null permissions.  DVMA binding will
1007*0Sstevel@tonic-gate 	 * be done when the RDMA is set up.
1008*0Sstevel@tonic-gate 	 */
1009*0Sstevel@tonic-gate 	acl[0].ae_node = rsm_local_nodeid;
1010*0Sstevel@tonic-gate 	if (len > RSM_MAX_HANDLE_DVMA)
1011*0Sstevel@tonic-gate 		acl[0].ae_permission = 0;
1012*0Sstevel@tonic-gate 	else
1013*0Sstevel@tonic-gate 		acl[0].ae_permission = RSM_PERM_RDWR;
1014*0Sstevel@tonic-gate 
1015*0Sstevel@tonic-gate 	e = rsm_memseg_export_publish(memseg, &segid, acl, 1);
1016*0Sstevel@tonic-gate 	if (e != RSM_SUCCESS) {
1017*0Sstevel@tonic-gate 		DBPRINTF((RSM_LIBRARY, RSM_ERR,
1018*0Sstevel@tonic-gate 		    "export publish failed\n"));
1019*0Sstevel@tonic-gate 		rsm_memseg_export_destroy(memseg);
1020*0Sstevel@tonic-gate 		return (e);
1021*0Sstevel@tonic-gate 	}
1022*0Sstevel@tonic-gate 
1023*0Sstevel@tonic-gate 	/* Use the surrogate seghandle as the local memory handle */
1024*0Sstevel@tonic-gate 	*local_hndl_p = (rsm_localmemory_handle_t)memseg;
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1027*0Sstevel@tonic-gate 	    "__rsm_create_memory_handle: exit\n"));
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 	return (e);
1030*0Sstevel@tonic-gate }
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate static int
1033*0Sstevel@tonic-gate __rsm_free_memory_handle(rsm_localmemory_handle_t local_handle)
1034*0Sstevel@tonic-gate {
1035*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1036*0Sstevel@tonic-gate 	    "__rsm_free_memory_handle: enter\n"));
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 	rsm_memseg_export_destroy((rsm_memseg_export_handle_t)local_handle);
1039*0Sstevel@tonic-gate 
1040*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1041*0Sstevel@tonic-gate 	    "__rsm_free_memory_handle: exit\n"));
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
1044*0Sstevel@tonic-gate }
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate static int
1047*0Sstevel@tonic-gate __rsm_get_lib_attr(rsm_ndlib_attr_t **libattrp)
1048*0Sstevel@tonic-gate {
1049*0Sstevel@tonic-gate 
1050*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1051*0Sstevel@tonic-gate 	    "__rsm_get_lib_attr: enter\n"));
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 	*libattrp = &_rsm_genlib_attr;
1054*0Sstevel@tonic-gate 
1055*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1056*0Sstevel@tonic-gate 	    "__rsm_get_lib_attr: exit\n"));
1057*0Sstevel@tonic-gate 
1058*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
1059*0Sstevel@tonic-gate }
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate static int
1062*0Sstevel@tonic-gate __rsm_closedevice(rsmapi_controller_handle_t cntr_handle)
1063*0Sstevel@tonic-gate {
1064*0Sstevel@tonic-gate 
1065*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1066*0Sstevel@tonic-gate 	    "__rsm_closedevice: enter\n"));
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 	cntr_handle = cntr_handle;
1069*0Sstevel@tonic-gate 
1070*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1071*0Sstevel@tonic-gate 	    "__rsm_closedevice: exit\n"));
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate 	return (RSM_SUCCESS);
1074*0Sstevel@tonic-gate }
1075*0Sstevel@tonic-gate 
1076*0Sstevel@tonic-gate void
1077*0Sstevel@tonic-gate __rsmdefault_setops(rsm_segops_t *segops)
1078*0Sstevel@tonic-gate {
1079*0Sstevel@tonic-gate 
1080*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1081*0Sstevel@tonic-gate 	    "__rsmdefault_setops: enter\n"));
1082*0Sstevel@tonic-gate 
1083*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_connect == NULL) {
1084*0Sstevel@tonic-gate 		segops->rsm_memseg_import_connect = __rsm_import_connect;
1085*0Sstevel@tonic-gate 	}
1086*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_disconnect == NULL) {
1087*0Sstevel@tonic-gate 		segops->rsm_memseg_import_disconnect = __rsm_import_disconnect;
1088*0Sstevel@tonic-gate 	}
1089*0Sstevel@tonic-gate 
1090*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_get8 == NULL) {
1091*0Sstevel@tonic-gate 		segops->rsm_memseg_import_get8 = __rsm_get8x8;
1092*0Sstevel@tonic-gate 	}
1093*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_get16 == NULL) {
1094*0Sstevel@tonic-gate 		segops->rsm_memseg_import_get16 = __rsm_get16x16;
1095*0Sstevel@tonic-gate 	}
1096*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_get32 == NULL) {
1097*0Sstevel@tonic-gate 		segops->rsm_memseg_import_get32 = __rsm_get32x32;
1098*0Sstevel@tonic-gate 	}
1099*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_get64 == NULL) {
1100*0Sstevel@tonic-gate 		segops->rsm_memseg_import_get64 = __rsm_get64x64;
1101*0Sstevel@tonic-gate 	}
1102*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_get == NULL) {
1103*0Sstevel@tonic-gate 		segops->rsm_memseg_import_get = __rsm_get;
1104*0Sstevel@tonic-gate 	}
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_put8 == NULL) {
1107*0Sstevel@tonic-gate 		segops->rsm_memseg_import_put8 = __rsm_put8x8;
1108*0Sstevel@tonic-gate 	}
1109*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_put16 == NULL) {
1110*0Sstevel@tonic-gate 		segops->rsm_memseg_import_put16 = __rsm_put16x16;
1111*0Sstevel@tonic-gate 	}
1112*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_put32 == NULL) {
1113*0Sstevel@tonic-gate 		segops->rsm_memseg_import_put32 = __rsm_put32x32;
1114*0Sstevel@tonic-gate 	}
1115*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_put64 == NULL) {
1116*0Sstevel@tonic-gate 		segops->rsm_memseg_import_put64 = __rsm_put64x64;
1117*0Sstevel@tonic-gate 	}
1118*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_put == NULL) {
1119*0Sstevel@tonic-gate 		segops->rsm_memseg_import_put = __rsm_put;
1120*0Sstevel@tonic-gate 	}
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_putv == NULL) {
1123*0Sstevel@tonic-gate 		segops->rsm_memseg_import_putv = __rsm_putv;
1124*0Sstevel@tonic-gate 	}
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_getv == NULL) {
1127*0Sstevel@tonic-gate 		segops->rsm_memseg_import_getv = __rsm_getv;
1128*0Sstevel@tonic-gate 	}
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate 	if (segops->rsm_create_localmemory_handle == NULL) {
1131*0Sstevel@tonic-gate 		segops->rsm_create_localmemory_handle =
1132*0Sstevel@tonic-gate 		    __rsm_create_memory_handle;
1133*0Sstevel@tonic-gate 	}
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate 	if (segops->rsm_free_localmemory_handle == NULL) {
1136*0Sstevel@tonic-gate 		segops->rsm_free_localmemory_handle =
1137*0Sstevel@tonic-gate 		    __rsm_free_memory_handle;
1138*0Sstevel@tonic-gate 	}
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 	/* XXX: Need to support barrier functions */
1141*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_init_barrier == NULL) {
1142*0Sstevel@tonic-gate 		segops->rsm_memseg_import_init_barrier =
1143*0Sstevel@tonic-gate 		    __rsm_memseg_import_init_barrier;
1144*0Sstevel@tonic-gate 	}
1145*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_open_barrier == NULL) {
1146*0Sstevel@tonic-gate 		segops->rsm_memseg_import_open_barrier =
1147*0Sstevel@tonic-gate 		    __rsm_memseg_import_open_barrier;
1148*0Sstevel@tonic-gate 	}
1149*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_order_barrier == NULL) {
1150*0Sstevel@tonic-gate 		segops->rsm_memseg_import_order_barrier =
1151*0Sstevel@tonic-gate 		    __rsm_memseg_import_order_barrier;
1152*0Sstevel@tonic-gate 	}
1153*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_close_barrier == NULL) {
1154*0Sstevel@tonic-gate 		segops->rsm_memseg_import_close_barrier =
1155*0Sstevel@tonic-gate 		    __rsm_memseg_import_close_barrier;
1156*0Sstevel@tonic-gate 	}
1157*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_destroy_barrier == NULL) {
1158*0Sstevel@tonic-gate 		segops->rsm_memseg_import_destroy_barrier =
1159*0Sstevel@tonic-gate 		    __rsm_memseg_import_destroy_barrier;
1160*0Sstevel@tonic-gate 	}
1161*0Sstevel@tonic-gate 
1162*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_get_mode == NULL) {
1163*0Sstevel@tonic-gate 		segops->rsm_memseg_import_get_mode =
1164*0Sstevel@tonic-gate 		    __rsm_memseg_import_get_mode;
1165*0Sstevel@tonic-gate 	}
1166*0Sstevel@tonic-gate 	if (segops->rsm_memseg_import_set_mode == NULL) {
1167*0Sstevel@tonic-gate 		segops->rsm_memseg_import_set_mode =
1168*0Sstevel@tonic-gate 		    __rsm_memseg_import_set_mode;
1169*0Sstevel@tonic-gate 	}
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 	if (segops->rsm_get_lib_attr == NULL) {
1172*0Sstevel@tonic-gate 		segops->rsm_get_lib_attr =
1173*0Sstevel@tonic-gate 		    __rsm_get_lib_attr;
1174*0Sstevel@tonic-gate 	}
1175*0Sstevel@tonic-gate 
1176*0Sstevel@tonic-gate 	if (segops->rsm_closedevice == NULL) {
1177*0Sstevel@tonic-gate 		segops->rsm_closedevice =
1178*0Sstevel@tonic-gate 		    __rsm_closedevice;
1179*0Sstevel@tonic-gate 	}
1180*0Sstevel@tonic-gate 
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate 	DBPRINTF((RSM_LIBRARY, RSM_DEBUG_VERBOSE,
1183*0Sstevel@tonic-gate 	    "__rsmdefault_setops: exit\n"));
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate }
1186