10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57387SRobert.Gordon@Sun.COM  * Common Development and Distribution License (the "License").
67387SRobert.Gordon@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228695SRajkumar.Sivaprakasam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
260Sstevel@tonic-gate /* All Rights Reserved */
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
290Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
300Sstevel@tonic-gate  * California.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Server side of RPC over RDMA in the kernel.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/param.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/user.h>
400Sstevel@tonic-gate #include <sys/sysmacros.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/errno.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/debug.h>
460Sstevel@tonic-gate #include <sys/systm.h>
470Sstevel@tonic-gate #include <sys/cmn_err.h>
480Sstevel@tonic-gate #include <sys/kstat.h>
490Sstevel@tonic-gate #include <sys/vtrace.h>
500Sstevel@tonic-gate #include <sys/debug.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <rpc/types.h>
530Sstevel@tonic-gate #include <rpc/xdr.h>
540Sstevel@tonic-gate #include <rpc/auth.h>
550Sstevel@tonic-gate #include <rpc/clnt.h>
560Sstevel@tonic-gate #include <rpc/rpc_msg.h>
570Sstevel@tonic-gate #include <rpc/svc.h>
580Sstevel@tonic-gate #include <rpc/rpc_rdma.h>
590Sstevel@tonic-gate #include <sys/ddi.h>
600Sstevel@tonic-gate #include <sys/sunddi.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include <inet/common.h>
630Sstevel@tonic-gate #include <inet/ip.h>
640Sstevel@tonic-gate #include <inet/ip6.h>
650Sstevel@tonic-gate 
667387SRobert.Gordon@Sun.COM #include <nfs/nfs.h>
677387SRobert.Gordon@Sun.COM #include <sys/sdt.h>
687387SRobert.Gordon@Sun.COM 
697387SRobert.Gordon@Sun.COM #define	SVC_RDMA_SUCCESS 0
707387SRobert.Gordon@Sun.COM #define	SVC_RDMA_FAIL -1
717387SRobert.Gordon@Sun.COM 
727387SRobert.Gordon@Sun.COM #define	SVC_CREDIT_FACTOR (0.5)
737387SRobert.Gordon@Sun.COM 
747387SRobert.Gordon@Sun.COM #define	MSG_IS_RPCSEC_GSS(msg)		\
757387SRobert.Gordon@Sun.COM 	((msg)->rm_reply.rp_acpt.ar_verf.oa_flavor == RPCSEC_GSS)
767387SRobert.Gordon@Sun.COM 
777387SRobert.Gordon@Sun.COM 
787387SRobert.Gordon@Sun.COM uint32_t rdma_bufs_granted = RDMA_BUFS_GRANT;
797387SRobert.Gordon@Sun.COM 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * RDMA transport specific data associated with SVCMASTERXPRT
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate struct rdma_data {
840Sstevel@tonic-gate 	SVCMASTERXPRT 	*rd_xprt;	/* back ptr to SVCMASTERXPRT */
850Sstevel@tonic-gate 	struct rdma_svc_data rd_data;	/* rdma data */
860Sstevel@tonic-gate 	rdma_mod_t	*r_mod;		/* RDMA module containing ops ptr */
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Plugin connection specific data stashed away in clone SVCXPRT
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate struct clone_rdma_data {
930Sstevel@tonic-gate 	CONN		*conn;		/* RDMA connection */
940Sstevel@tonic-gate 	rdma_buf_t	rpcbuf;		/* RPC req/resp buffer */
957387SRobert.Gordon@Sun.COM 	struct clist	*cl_reply;	/* reply chunk buffer info */
967387SRobert.Gordon@Sun.COM 	struct clist	*cl_wlist;		/* write list clist */
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define	MAXADDRLEN	128	/* max length for address mask */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * Routines exported through ops vector.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate static bool_t		svc_rdma_krecv(SVCXPRT *, mblk_t *, struct rpc_msg *);
1050Sstevel@tonic-gate static bool_t		svc_rdma_ksend(SVCXPRT *, struct rpc_msg *);
1060Sstevel@tonic-gate static bool_t		svc_rdma_kgetargs(SVCXPRT *, xdrproc_t, caddr_t);
1070Sstevel@tonic-gate static bool_t		svc_rdma_kfreeargs(SVCXPRT *, xdrproc_t, caddr_t);
1080Sstevel@tonic-gate void			svc_rdma_kdestroy(SVCMASTERXPRT *);
1090Sstevel@tonic-gate static int		svc_rdma_kdup(struct svc_req *, caddr_t, int,
1100Sstevel@tonic-gate 				struct dupreq **, bool_t *);
1110Sstevel@tonic-gate static void		svc_rdma_kdupdone(struct dupreq *, caddr_t,
1120Sstevel@tonic-gate 				void (*)(), int, int);
1130Sstevel@tonic-gate static int32_t		*svc_rdma_kgetres(SVCXPRT *, int);
1140Sstevel@tonic-gate static void		svc_rdma_kfreeres(SVCXPRT *);
1150Sstevel@tonic-gate static void		svc_rdma_kclone_destroy(SVCXPRT *);
1160Sstevel@tonic-gate static void		svc_rdma_kstart(SVCMASTERXPRT *);
1170Sstevel@tonic-gate void			svc_rdma_kstop(SVCMASTERXPRT *);
1180Sstevel@tonic-gate 
1197387SRobert.Gordon@Sun.COM static int	svc_process_long_reply(SVCXPRT *, xdrproc_t,
1207387SRobert.Gordon@Sun.COM 			caddr_t, struct rpc_msg *, bool_t, int *,
1217387SRobert.Gordon@Sun.COM 			int *, int *, unsigned int *);
1227387SRobert.Gordon@Sun.COM 
1237387SRobert.Gordon@Sun.COM static int	svc_compose_rpcmsg(SVCXPRT *, CONN *, xdrproc_t,
1247387SRobert.Gordon@Sun.COM 			caddr_t, rdma_buf_t *, XDR **, struct rpc_msg *,
1257387SRobert.Gordon@Sun.COM 			bool_t, uint_t *);
1267387SRobert.Gordon@Sun.COM static bool_t rpcmsg_length(xdrproc_t,
1277387SRobert.Gordon@Sun.COM 		caddr_t,
1287387SRobert.Gordon@Sun.COM 		struct rpc_msg *, bool_t, int);
1297387SRobert.Gordon@Sun.COM 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * Server transport operations vector.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate struct svc_ops rdma_svc_ops = {
1340Sstevel@tonic-gate 	svc_rdma_krecv,		/* Get requests */
1350Sstevel@tonic-gate 	svc_rdma_kgetargs,	/* Deserialize arguments */
1360Sstevel@tonic-gate 	svc_rdma_ksend,		/* Send reply */
1370Sstevel@tonic-gate 	svc_rdma_kfreeargs,	/* Free argument data space */
1380Sstevel@tonic-gate 	svc_rdma_kdestroy,	/* Destroy transport handle */
1390Sstevel@tonic-gate 	svc_rdma_kdup,		/* Check entry in dup req cache */
1400Sstevel@tonic-gate 	svc_rdma_kdupdone,	/* Mark entry in dup req cache as done */
1410Sstevel@tonic-gate 	svc_rdma_kgetres,	/* Get pointer to response buffer */
1420Sstevel@tonic-gate 	svc_rdma_kfreeres,	/* Destroy pre-serialized response header */
1430Sstevel@tonic-gate 	svc_rdma_kclone_destroy,	/* Destroy a clone xprt */
1440Sstevel@tonic-gate 	svc_rdma_kstart		/* Tell `ready-to-receive' to rpcmod */
1450Sstevel@tonic-gate };
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * Server statistics
1490Sstevel@tonic-gate  * NOTE: This structure type is duplicated in the NFS fast path.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate struct {
1520Sstevel@tonic-gate 	kstat_named_t	rscalls;
1530Sstevel@tonic-gate 	kstat_named_t	rsbadcalls;
1540Sstevel@tonic-gate 	kstat_named_t	rsnullrecv;
1550Sstevel@tonic-gate 	kstat_named_t	rsbadlen;
1560Sstevel@tonic-gate 	kstat_named_t	rsxdrcall;
1570Sstevel@tonic-gate 	kstat_named_t	rsdupchecks;
1580Sstevel@tonic-gate 	kstat_named_t	rsdupreqs;
1590Sstevel@tonic-gate 	kstat_named_t	rslongrpcs;
1607387SRobert.Gordon@Sun.COM 	kstat_named_t	rstotalreplies;
1617387SRobert.Gordon@Sun.COM 	kstat_named_t	rstotallongreplies;
1627387SRobert.Gordon@Sun.COM 	kstat_named_t	rstotalinlinereplies;
1630Sstevel@tonic-gate } rdmarsstat = {
1640Sstevel@tonic-gate 	{ "calls",	KSTAT_DATA_UINT64 },
1650Sstevel@tonic-gate 	{ "badcalls",	KSTAT_DATA_UINT64 },
1660Sstevel@tonic-gate 	{ "nullrecv",	KSTAT_DATA_UINT64 },
1670Sstevel@tonic-gate 	{ "badlen",	KSTAT_DATA_UINT64 },
1680Sstevel@tonic-gate 	{ "xdrcall",	KSTAT_DATA_UINT64 },
1690Sstevel@tonic-gate 	{ "dupchecks",	KSTAT_DATA_UINT64 },
1700Sstevel@tonic-gate 	{ "dupreqs",	KSTAT_DATA_UINT64 },
1717387SRobert.Gordon@Sun.COM 	{ "longrpcs",	KSTAT_DATA_UINT64 },
1727387SRobert.Gordon@Sun.COM 	{ "totalreplies",	KSTAT_DATA_UINT64 },
1737387SRobert.Gordon@Sun.COM 	{ "totallongreplies",	KSTAT_DATA_UINT64 },
1747387SRobert.Gordon@Sun.COM 	{ "totalinlinereplies",	KSTAT_DATA_UINT64 },
1750Sstevel@tonic-gate };
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate kstat_named_t *rdmarsstat_ptr = (kstat_named_t *)&rdmarsstat;
1780Sstevel@tonic-gate uint_t rdmarsstat_ndata = sizeof (rdmarsstat) / sizeof (kstat_named_t);
1790Sstevel@tonic-gate 
1807387SRobert.Gordon@Sun.COM #define	RSSTAT_INCR(x)	atomic_add_64(&rdmarsstat.x.value.ui64, 1)
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * Create a transport record.
1830Sstevel@tonic-gate  * The transport record, output buffer, and private data structure
1840Sstevel@tonic-gate  * are allocated.  The output buffer is serialized into using xdrmem.
1850Sstevel@tonic-gate  * There is one transport record per user process which implements a
1860Sstevel@tonic-gate  * set of services.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate /* ARGSUSED */
1890Sstevel@tonic-gate int
1900Sstevel@tonic-gate svc_rdma_kcreate(char *netid, SVC_CALLOUT_TABLE *sct, int id,
1917387SRobert.Gordon@Sun.COM     rdma_xprt_group_t *started_xprts)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	int error;
1940Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
1950Sstevel@tonic-gate 	struct rdma_data *rd;
1960Sstevel@tonic-gate 	rdma_registry_t *rmod;
1970Sstevel@tonic-gate 	rdma_xprt_record_t *xprt_rec;
1980Sstevel@tonic-gate 	queue_t	*q;
1990Sstevel@tonic-gate 	/*
2000Sstevel@tonic-gate 	 * modload the RDMA plugins is not already done.
2010Sstevel@tonic-gate 	 */
2020Sstevel@tonic-gate 	if (!rdma_modloaded) {
2037387SRobert.Gordon@Sun.COM 		/*CONSTANTCONDITION*/
2047387SRobert.Gordon@Sun.COM 		ASSERT(sizeof (struct clone_rdma_data) <= SVC_P2LEN);
2057387SRobert.Gordon@Sun.COM 
2060Sstevel@tonic-gate 		mutex_enter(&rdma_modload_lock);
2070Sstevel@tonic-gate 		if (!rdma_modloaded) {
2080Sstevel@tonic-gate 			error = rdma_modload();
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 		mutex_exit(&rdma_modload_lock);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		if (error)
2130Sstevel@tonic-gate 			return (error);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * master_xprt_count is the count of master transport handles
2180Sstevel@tonic-gate 	 * that were successfully created and are ready to recieve for
2190Sstevel@tonic-gate 	 * RDMA based access.
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	error = 0;
2220Sstevel@tonic-gate 	xprt_rec = NULL;
2230Sstevel@tonic-gate 	rw_enter(&rdma_lock, RW_READER);
2240Sstevel@tonic-gate 	if (rdma_mod_head == NULL) {
2250Sstevel@tonic-gate 		started_xprts->rtg_count = 0;
2260Sstevel@tonic-gate 		rw_exit(&rdma_lock);
2270Sstevel@tonic-gate 		if (rdma_dev_available)
2280Sstevel@tonic-gate 			return (EPROTONOSUPPORT);
2290Sstevel@tonic-gate 		else
2300Sstevel@tonic-gate 			return (ENODEV);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * If we have reached here, then atleast one RDMA plugin has loaded.
2350Sstevel@tonic-gate 	 * Create a master_xprt, make it start listenining on the device,
2360Sstevel@tonic-gate 	 * if an error is generated, record it, we might need to shut
2370Sstevel@tonic-gate 	 * the master_xprt.
2380Sstevel@tonic-gate 	 * SVC_START() calls svc_rdma_kstart which calls plugin binding
2390Sstevel@tonic-gate 	 * routines.
2400Sstevel@tonic-gate 	 */
2410Sstevel@tonic-gate 	for (rmod = rdma_mod_head; rmod != NULL; rmod = rmod->r_next) {
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		/*
2440Sstevel@tonic-gate 		 * One SVCMASTERXPRT per RDMA plugin.
2450Sstevel@tonic-gate 		 */
2460Sstevel@tonic-gate 		xprt = kmem_zalloc(sizeof (*xprt), KM_SLEEP);
2470Sstevel@tonic-gate 		xprt->xp_ops = &rdma_svc_ops;
2480Sstevel@tonic-gate 		xprt->xp_sct = sct;
2490Sstevel@tonic-gate 		xprt->xp_type = T_RDMA;
2500Sstevel@tonic-gate 		mutex_init(&xprt->xp_req_lock, NULL, MUTEX_DEFAULT, NULL);
2510Sstevel@tonic-gate 		mutex_init(&xprt->xp_thread_lock, NULL, MUTEX_DEFAULT, NULL);
2520Sstevel@tonic-gate 		xprt->xp_req_head = (mblk_t *)0;
2530Sstevel@tonic-gate 		xprt->xp_req_tail = (mblk_t *)0;
2540Sstevel@tonic-gate 		xprt->xp_threads = 0;
2550Sstevel@tonic-gate 		xprt->xp_detached_threads = 0;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		rd = kmem_zalloc(sizeof (*rd), KM_SLEEP);
2580Sstevel@tonic-gate 		xprt->xp_p2 = (caddr_t)rd;
2590Sstevel@tonic-gate 		rd->rd_xprt = xprt;
2600Sstevel@tonic-gate 		rd->r_mod = rmod->r_mod;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		q = &rd->rd_data.q;
2630Sstevel@tonic-gate 		xprt->xp_wq = q;
2640Sstevel@tonic-gate 		q->q_ptr = &rd->rd_xprt;
2650Sstevel@tonic-gate 		xprt->xp_netid = NULL;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 		if (netid != NULL) {
2680Sstevel@tonic-gate 			xprt->xp_netid = kmem_alloc(strlen(netid) + 1,
2697387SRobert.Gordon@Sun.COM 			    KM_SLEEP);
2700Sstevel@tonic-gate 			(void) strcpy(xprt->xp_netid, netid);
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 		xprt->xp_addrmask.maxlen =
2740Sstevel@tonic-gate 		    xprt->xp_addrmask.len = sizeof (struct sockaddr_in);
2750Sstevel@tonic-gate 		xprt->xp_addrmask.buf =
2760Sstevel@tonic-gate 		    kmem_zalloc(xprt->xp_addrmask.len, KM_SLEEP);
2770Sstevel@tonic-gate 		((struct sockaddr_in *)xprt->xp_addrmask.buf)->sin_addr.s_addr =
2780Sstevel@tonic-gate 		    (uint32_t)~0;
2790Sstevel@tonic-gate 		((struct sockaddr_in *)xprt->xp_addrmask.buf)->sin_family =
2800Sstevel@tonic-gate 		    (ushort_t)~0;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		/*
2830Sstevel@tonic-gate 		 * Each of the plugins will have their own Service ID
2840Sstevel@tonic-gate 		 * to listener specific mapping, like port number for VI
2850Sstevel@tonic-gate 		 * and service name for IB.
2860Sstevel@tonic-gate 		 */
2870Sstevel@tonic-gate 		rd->rd_data.svcid = id;
2880Sstevel@tonic-gate 		error = svc_xprt_register(xprt, id);
2890Sstevel@tonic-gate 		if (error) {
2907387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__xprt__reg);
2910Sstevel@tonic-gate 			goto cleanup;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		SVC_START(xprt);
2950Sstevel@tonic-gate 		if (!rd->rd_data.active) {
2960Sstevel@tonic-gate 			svc_xprt_unregister(xprt);
2970Sstevel@tonic-gate 			error = rd->rd_data.err_code;
2980Sstevel@tonic-gate 			goto cleanup;
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		/*
3020Sstevel@tonic-gate 		 * This is set only when there is atleast one or more
3030Sstevel@tonic-gate 		 * transports successfully created. We insert the pointer
3040Sstevel@tonic-gate 		 * to the created RDMA master xprt into a separately maintained
3050Sstevel@tonic-gate 		 * list. This way we can easily reference it later to cleanup,
3060Sstevel@tonic-gate 		 * when NFS kRPC service pool is going away/unregistered.
3070Sstevel@tonic-gate 		 */
3080Sstevel@tonic-gate 		started_xprts->rtg_count ++;
3090Sstevel@tonic-gate 		xprt_rec = kmem_alloc(sizeof (*xprt_rec), KM_SLEEP);
3100Sstevel@tonic-gate 		xprt_rec->rtr_xprt_ptr = xprt;
3110Sstevel@tonic-gate 		xprt_rec->rtr_next = started_xprts->rtg_listhead;
3120Sstevel@tonic-gate 		started_xprts->rtg_listhead = xprt_rec;
3130Sstevel@tonic-gate 		continue;
3140Sstevel@tonic-gate cleanup:
3150Sstevel@tonic-gate 		SVC_DESTROY(xprt);
3160Sstevel@tonic-gate 		if (error == RDMA_FAILED)
3170Sstevel@tonic-gate 			error = EPROTONOSUPPORT;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	rw_exit(&rdma_lock);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * Don't return any error even if a single plugin was started
3240Sstevel@tonic-gate 	 * successfully.
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	if (started_xprts->rtg_count == 0)
3270Sstevel@tonic-gate 		return (error);
3280Sstevel@tonic-gate 	return (0);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate  * Cleanup routine for freeing up memory allocated by
3330Sstevel@tonic-gate  * svc_rdma_kcreate()
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate void
3360Sstevel@tonic-gate svc_rdma_kdestroy(SVCMASTERXPRT *xprt)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	struct rdma_data *rd = (struct rdma_data *)xprt->xp_p2;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_req_lock);
3420Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_thread_lock);
3430Sstevel@tonic-gate 	kmem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
3440Sstevel@tonic-gate 	kmem_free(rd, sizeof (*rd));
3450Sstevel@tonic-gate 	kmem_free(xprt->xp_addrmask.buf, xprt->xp_addrmask.maxlen);
3460Sstevel@tonic-gate 	kmem_free(xprt, sizeof (*xprt));
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate static void
3510Sstevel@tonic-gate svc_rdma_kstart(SVCMASTERXPRT *xprt)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	struct rdma_svc_data *svcdata;
3540Sstevel@tonic-gate 	rdma_mod_t *rmod;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	svcdata = &((struct rdma_data *)xprt->xp_p2)->rd_data;
3570Sstevel@tonic-gate 	rmod = ((struct rdma_data *)xprt->xp_p2)->r_mod;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * Create a listener for  module at this port
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 
3638695SRajkumar.Sivaprakasam@Sun.COM 	if (rmod->rdma_count != 0)
3648695SRajkumar.Sivaprakasam@Sun.COM 		(*rmod->rdma_ops->rdma_svc_listen)(svcdata);
3658695SRajkumar.Sivaprakasam@Sun.COM 	else
3668695SRajkumar.Sivaprakasam@Sun.COM 		svcdata->err_code = RDMA_FAILED;
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate void
3700Sstevel@tonic-gate svc_rdma_kstop(SVCMASTERXPRT *xprt)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	struct rdma_svc_data *svcdata;
3730Sstevel@tonic-gate 	rdma_mod_t *rmod;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	svcdata	= &((struct rdma_data *)xprt->xp_p2)->rd_data;
3760Sstevel@tonic-gate 	rmod = ((struct rdma_data *)xprt->xp_p2)->r_mod;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/*
3798695SRajkumar.Sivaprakasam@Sun.COM 	 * Call the stop listener routine for each plugin. If rdma_count is
3808695SRajkumar.Sivaprakasam@Sun.COM 	 * already zero set active to zero.
3810Sstevel@tonic-gate 	 */
3828695SRajkumar.Sivaprakasam@Sun.COM 	if (rmod->rdma_count != 0)
3838695SRajkumar.Sivaprakasam@Sun.COM 		(*rmod->rdma_ops->rdma_svc_stop)(svcdata);
3848695SRajkumar.Sivaprakasam@Sun.COM 	else
3858695SRajkumar.Sivaprakasam@Sun.COM 		svcdata->active = 0;
3860Sstevel@tonic-gate 	if (svcdata->active)
3877387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__kstop);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /* ARGSUSED */
3910Sstevel@tonic-gate static void
3920Sstevel@tonic-gate svc_rdma_kclone_destroy(SVCXPRT *clone_xprt)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate static bool_t
3970Sstevel@tonic-gate svc_rdma_krecv(SVCXPRT *clone_xprt, mblk_t *mp, struct rpc_msg *msg)
3980Sstevel@tonic-gate {
3997387SRobert.Gordon@Sun.COM 	XDR	*xdrs;
4007387SRobert.Gordon@Sun.COM 	CONN	*conn;
4010Sstevel@tonic-gate 
4027387SRobert.Gordon@Sun.COM 	rdma_recv_data_t	*rdp = (rdma_recv_data_t *)mp->b_rptr;
4037387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
4047387SRobert.Gordon@Sun.COM 	struct clist	*cl = NULL;
4057387SRobert.Gordon@Sun.COM 	struct clist	*wcl = NULL;
4067387SRobert.Gordon@Sun.COM 	struct clist	*cllong = NULL;
4077387SRobert.Gordon@Sun.COM 
4087387SRobert.Gordon@Sun.COM 	rdma_stat	status;
4097387SRobert.Gordon@Sun.COM 	uint32_t vers, op, pos, xid;
4107387SRobert.Gordon@Sun.COM 	uint32_t rdma_credit;
4117387SRobert.Gordon@Sun.COM 	uint32_t wcl_total_length = 0;
4127387SRobert.Gordon@Sun.COM 	bool_t	wwl = FALSE;
4137387SRobert.Gordon@Sun.COM 
4147387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
4150Sstevel@tonic-gate 	RSSTAT_INCR(rscalls);
4160Sstevel@tonic-gate 	conn = rdp->conn;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	status = rdma_svc_postrecv(conn);
4190Sstevel@tonic-gate 	if (status != RDMA_SUCCESS) {
4207387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__postrecv);
4217387SRobert.Gordon@Sun.COM 		goto badrpc_call;
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	xdrs = &clone_xprt->xp_xdrin;
4250Sstevel@tonic-gate 	xdrmem_create(xdrs, rdp->rpcmsg.addr, rdp->rpcmsg.len, XDR_DECODE);
4267387SRobert.Gordon@Sun.COM 	xid = *(uint32_t *)rdp->rpcmsg.addr;
4277387SRobert.Gordon@Sun.COM 	XDR_SETPOS(xdrs, sizeof (uint32_t));
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (! xdr_u_int(xdrs, &vers) ||
4307387SRobert.Gordon@Sun.COM 	    ! xdr_u_int(xdrs, &rdma_credit) ||
4310Sstevel@tonic-gate 	    ! xdr_u_int(xdrs, &op)) {
4327387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__uint);
4337387SRobert.Gordon@Sun.COM 		goto xdr_err;
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4367387SRobert.Gordon@Sun.COM 	/* Checking if the status of the recv operation was normal */
4377387SRobert.Gordon@Sun.COM 	if (rdp->status != 0) {
4387387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__e__svcrdma__krecv__invalid__status,
4397387SRobert.Gordon@Sun.COM 		    int, rdp->status);
4407387SRobert.Gordon@Sun.COM 		goto badrpc_call;
4417387SRobert.Gordon@Sun.COM 	}
4427387SRobert.Gordon@Sun.COM 
4430Sstevel@tonic-gate 	if (! xdr_do_clist(xdrs, &cl)) {
4447387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__do__clist);
4457387SRobert.Gordon@Sun.COM 		goto xdr_err;
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4487387SRobert.Gordon@Sun.COM 	if (!xdr_decode_wlist_svc(xdrs, &wcl, &wwl, &wcl_total_length, conn)) {
4497387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__decode__wlist);
4507387SRobert.Gordon@Sun.COM 		if (cl)
4517387SRobert.Gordon@Sun.COM 			clist_free(cl);
4527387SRobert.Gordon@Sun.COM 		goto xdr_err;
4537387SRobert.Gordon@Sun.COM 	}
4547387SRobert.Gordon@Sun.COM 	crdp->cl_wlist = wcl;
4557387SRobert.Gordon@Sun.COM 
4567387SRobert.Gordon@Sun.COM 	crdp->cl_reply = NULL;
4577387SRobert.Gordon@Sun.COM 	(void) xdr_decode_reply_wchunk(xdrs, &crdp->cl_reply);
4587387SRobert.Gordon@Sun.COM 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * A chunk at 0 offset indicates that the RPC call message
4610Sstevel@tonic-gate 	 * is in a chunk. Get the RPC call message chunk.
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	if (cl != NULL && op == RDMA_NOMSG) {
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		/* Remove RPC call message chunk from chunklist */
4660Sstevel@tonic-gate 		cllong = cl;
4670Sstevel@tonic-gate 		cl = cl->c_next;
4680Sstevel@tonic-gate 		cllong->c_next = NULL;
4690Sstevel@tonic-gate 
4707387SRobert.Gordon@Sun.COM 
4710Sstevel@tonic-gate 		/* Allocate and register memory for the RPC call msg chunk */
4727387SRobert.Gordon@Sun.COM 		cllong->rb_longbuf.type = RDMA_LONG_BUFFER;
4737387SRobert.Gordon@Sun.COM 		cllong->rb_longbuf.len = cllong->c_len > LONG_REPLY_LEN ?
4747387SRobert.Gordon@Sun.COM 		    cllong->c_len : LONG_REPLY_LEN;
4757387SRobert.Gordon@Sun.COM 
4767387SRobert.Gordon@Sun.COM 		if (rdma_buf_alloc(conn, &cllong->rb_longbuf)) {
4770Sstevel@tonic-gate 			clist_free(cllong);
4787387SRobert.Gordon@Sun.COM 			goto cll_malloc_err;
4790Sstevel@tonic-gate 		}
4807387SRobert.Gordon@Sun.COM 
4817387SRobert.Gordon@Sun.COM 		cllong->u.c_daddr3 = cllong->rb_longbuf.addr;
4827387SRobert.Gordon@Sun.COM 
4837387SRobert.Gordon@Sun.COM 		if (cllong->u.c_daddr == NULL) {
4847387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__krecv__nomem);
4857387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, &cllong->rb_longbuf);
4867387SRobert.Gordon@Sun.COM 			clist_free(cllong);
4877387SRobert.Gordon@Sun.COM 			goto cll_malloc_err;
4887387SRobert.Gordon@Sun.COM 		}
4897387SRobert.Gordon@Sun.COM 
4907387SRobert.Gordon@Sun.COM 		status = clist_register(conn, cllong, CLIST_REG_DST);
4910Sstevel@tonic-gate 		if (status) {
4927387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__krecv__clist__reg);
4937387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, &cllong->rb_longbuf);
4940Sstevel@tonic-gate 			clist_free(cllong);
4957387SRobert.Gordon@Sun.COM 			goto cll_malloc_err;
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		/*
4990Sstevel@tonic-gate 		 * Now read the RPC call message in
5000Sstevel@tonic-gate 		 */
5010Sstevel@tonic-gate 		status = RDMA_READ(conn, cllong, WAIT);
5020Sstevel@tonic-gate 		if (status) {
5037387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__krecv__read);
504*9348SSiddheshwar.Mahesh@Sun.COM 			(void) clist_deregister(conn, cllong);
5057387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, &cllong->rb_longbuf);
5060Sstevel@tonic-gate 			clist_free(cllong);
5077387SRobert.Gordon@Sun.COM 			goto cll_malloc_err;
5080Sstevel@tonic-gate 		}
5090Sstevel@tonic-gate 
5107387SRobert.Gordon@Sun.COM 		status = clist_syncmem(conn, cllong, CLIST_REG_DST);
511*9348SSiddheshwar.Mahesh@Sun.COM 		(void) clist_deregister(conn, cllong);
5120Sstevel@tonic-gate 
5137387SRobert.Gordon@Sun.COM 		xdrrdma_create(xdrs, (caddr_t)(uintptr_t)cllong->u.c_daddr3,
5140Sstevel@tonic-gate 		    cllong->c_len, 0, cl, XDR_DECODE, conn);
5150Sstevel@tonic-gate 
5167387SRobert.Gordon@Sun.COM 		crdp->rpcbuf = cllong->rb_longbuf;
5177387SRobert.Gordon@Sun.COM 		crdp->rpcbuf.len = cllong->c_len;
5180Sstevel@tonic-gate 		clist_free(cllong);
5190Sstevel@tonic-gate 		RDMA_BUF_FREE(conn, &rdp->rpcmsg);
5200Sstevel@tonic-gate 	} else {
5210Sstevel@tonic-gate 		pos = XDR_GETPOS(xdrs);
5227387SRobert.Gordon@Sun.COM 		xdrrdma_create(xdrs, rdp->rpcmsg.addr + pos,
5237387SRobert.Gordon@Sun.COM 		    rdp->rpcmsg.len - pos, 0, cl, XDR_DECODE, conn);
5247387SRobert.Gordon@Sun.COM 		crdp->rpcbuf = rdp->rpcmsg;
5250Sstevel@tonic-gate 
5267387SRobert.Gordon@Sun.COM 		/* Use xdrrdmablk_ops to indicate there is a read chunk list */
5277387SRobert.Gordon@Sun.COM 		if (cl != NULL) {
5287387SRobert.Gordon@Sun.COM 			int32_t flg = XDR_RDMA_RLIST_REG;
5297387SRobert.Gordon@Sun.COM 
5307387SRobert.Gordon@Sun.COM 			XDR_CONTROL(xdrs, XDR_RDMA_SET_FLAGS, &flg);
5317387SRobert.Gordon@Sun.COM 			xdrs->x_ops = &xdrrdmablk_ops;
5327387SRobert.Gordon@Sun.COM 		}
5330Sstevel@tonic-gate 	}
5347387SRobert.Gordon@Sun.COM 
5357387SRobert.Gordon@Sun.COM 	if (crdp->cl_wlist) {
5367387SRobert.Gordon@Sun.COM 		int32_t flg = XDR_RDMA_WLIST_REG;
5377387SRobert.Gordon@Sun.COM 
5387387SRobert.Gordon@Sun.COM 		XDR_CONTROL(xdrs, XDR_RDMA_SET_WLIST, crdp->cl_wlist);
5397387SRobert.Gordon@Sun.COM 		XDR_CONTROL(xdrs, XDR_RDMA_SET_FLAGS, &flg);
5407387SRobert.Gordon@Sun.COM 	}
5417387SRobert.Gordon@Sun.COM 
5420Sstevel@tonic-gate 	if (! xdr_callmsg(xdrs, msg)) {
5437387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__callmsg);
5440Sstevel@tonic-gate 		RSSTAT_INCR(rsxdrcall);
5457387SRobert.Gordon@Sun.COM 		goto callmsg_err;
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * Point the remote transport address in the service_transport
5500Sstevel@tonic-gate 	 * handle at the address in the request.
5510Sstevel@tonic-gate 	 */
5520Sstevel@tonic-gate 	clone_xprt->xp_rtaddr.buf = conn->c_raddr.buf;
5530Sstevel@tonic-gate 	clone_xprt->xp_rtaddr.len = conn->c_raddr.len;
5540Sstevel@tonic-gate 	clone_xprt->xp_rtaddr.maxlen = conn->c_raddr.len;
5557387SRobert.Gordon@Sun.COM 	clone_xprt->xp_xid = xid;
5567387SRobert.Gordon@Sun.COM 	crdp->conn = conn;
5570Sstevel@tonic-gate 
5587387SRobert.Gordon@Sun.COM 	freeb(mp);
5597387SRobert.Gordon@Sun.COM 
5607387SRobert.Gordon@Sun.COM 	return (TRUE);
5617387SRobert.Gordon@Sun.COM 
5627387SRobert.Gordon@Sun.COM callmsg_err:
5637387SRobert.Gordon@Sun.COM 	rdma_buf_free(conn, &crdp->rpcbuf);
5647387SRobert.Gordon@Sun.COM 
5657387SRobert.Gordon@Sun.COM cll_malloc_err:
5667387SRobert.Gordon@Sun.COM 	if (cl)
5677387SRobert.Gordon@Sun.COM 		clist_free(cl);
5687387SRobert.Gordon@Sun.COM xdr_err:
5697387SRobert.Gordon@Sun.COM 	XDR_DESTROY(xdrs);
5707387SRobert.Gordon@Sun.COM 
5717387SRobert.Gordon@Sun.COM badrpc_call:
5727387SRobert.Gordon@Sun.COM 	RDMA_BUF_FREE(conn, &rdp->rpcmsg);
5737387SRobert.Gordon@Sun.COM 	RDMA_REL_CONN(conn);
5747387SRobert.Gordon@Sun.COM 	freeb(mp);
5757387SRobert.Gordon@Sun.COM 	RSSTAT_INCR(rsbadcalls);
5767387SRobert.Gordon@Sun.COM 	return (FALSE);
5777387SRobert.Gordon@Sun.COM }
5787387SRobert.Gordon@Sun.COM 
5797387SRobert.Gordon@Sun.COM static int
5807387SRobert.Gordon@Sun.COM svc_process_long_reply(SVCXPRT * clone_xprt,
5817387SRobert.Gordon@Sun.COM     xdrproc_t xdr_results, caddr_t xdr_location,
5827387SRobert.Gordon@Sun.COM     struct rpc_msg *msg, bool_t has_args, int *msglen,
5837387SRobert.Gordon@Sun.COM     int *freelen, int *numchunks, unsigned int *final_len)
5847387SRobert.Gordon@Sun.COM {
5857387SRobert.Gordon@Sun.COM 	int status;
5867387SRobert.Gordon@Sun.COM 	XDR xdrslong;
5877387SRobert.Gordon@Sun.COM 	struct clist *wcl = NULL;
5887387SRobert.Gordon@Sun.COM 	int count = 0;
5897387SRobert.Gordon@Sun.COM 	int alloc_len;
5907387SRobert.Gordon@Sun.COM 	char  *memp;
5917387SRobert.Gordon@Sun.COM 	rdma_buf_t long_rpc = {0};
5927387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
5937387SRobert.Gordon@Sun.COM 
5947387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
5957387SRobert.Gordon@Sun.COM 
5967387SRobert.Gordon@Sun.COM 	bzero(&xdrslong, sizeof (xdrslong));
5977387SRobert.Gordon@Sun.COM 
5987387SRobert.Gordon@Sun.COM 	/* Choose a size for the long rpc response */
5997387SRobert.Gordon@Sun.COM 	if (MSG_IS_RPCSEC_GSS(msg)) {
6007387SRobert.Gordon@Sun.COM 		alloc_len = RNDUP(MAX_AUTH_BYTES + *msglen);
6017387SRobert.Gordon@Sun.COM 	} else {
6027387SRobert.Gordon@Sun.COM 		alloc_len = RNDUP(*msglen);
6037387SRobert.Gordon@Sun.COM 	}
6047387SRobert.Gordon@Sun.COM 
6057387SRobert.Gordon@Sun.COM 	if (alloc_len <= 64 * 1024) {
6067387SRobert.Gordon@Sun.COM 		if (alloc_len > 32 * 1024) {
6077387SRobert.Gordon@Sun.COM 			alloc_len = 64 * 1024;
6087387SRobert.Gordon@Sun.COM 		} else {
6097387SRobert.Gordon@Sun.COM 			if (alloc_len > 16 * 1024) {
6107387SRobert.Gordon@Sun.COM 				alloc_len = 32 * 1024;
6117387SRobert.Gordon@Sun.COM 			} else {
6127387SRobert.Gordon@Sun.COM 				alloc_len = 16 * 1024;
6137387SRobert.Gordon@Sun.COM 			}
6147387SRobert.Gordon@Sun.COM 		}
6157387SRobert.Gordon@Sun.COM 	}
6167387SRobert.Gordon@Sun.COM 
6177387SRobert.Gordon@Sun.COM 	long_rpc.type = RDMA_LONG_BUFFER;
6187387SRobert.Gordon@Sun.COM 	long_rpc.len = alloc_len;
6197387SRobert.Gordon@Sun.COM 	if (rdma_buf_alloc(crdp->conn, &long_rpc)) {
6207387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6217387SRobert.Gordon@Sun.COM 	}
6227387SRobert.Gordon@Sun.COM 
6237387SRobert.Gordon@Sun.COM 	memp = long_rpc.addr;
6247387SRobert.Gordon@Sun.COM 	xdrmem_create(&xdrslong, memp, alloc_len, XDR_ENCODE);
6257387SRobert.Gordon@Sun.COM 
6267387SRobert.Gordon@Sun.COM 	msg->rm_xid = clone_xprt->xp_xid;
6277387SRobert.Gordon@Sun.COM 
6287387SRobert.Gordon@Sun.COM 	if (!(xdr_replymsg(&xdrslong, msg) &&
6297387SRobert.Gordon@Sun.COM 	    (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, &xdrslong,
6307387SRobert.Gordon@Sun.COM 	    xdr_results, xdr_location)))) {
6317387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
6327387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__authwrap);
6337387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6347387SRobert.Gordon@Sun.COM 	}
6357387SRobert.Gordon@Sun.COM 
6367387SRobert.Gordon@Sun.COM 	*final_len = XDR_GETPOS(&xdrslong);
6377387SRobert.Gordon@Sun.COM 
638*9348SSiddheshwar.Mahesh@Sun.COM 	DTRACE_PROBE1(krpc__i__replylen, uint_t, *final_len);
6397387SRobert.Gordon@Sun.COM 	*numchunks = 0;
6407387SRobert.Gordon@Sun.COM 	*freelen = 0;
6417387SRobert.Gordon@Sun.COM 
6427387SRobert.Gordon@Sun.COM 	wcl = crdp->cl_reply;
6437387SRobert.Gordon@Sun.COM 	wcl->rb_longbuf = long_rpc;
6447387SRobert.Gordon@Sun.COM 
6457387SRobert.Gordon@Sun.COM 	count = *final_len;
646*9348SSiddheshwar.Mahesh@Sun.COM 	while ((wcl != NULL) && (count > 0)) {
647*9348SSiddheshwar.Mahesh@Sun.COM 
6487387SRobert.Gordon@Sun.COM 		if (wcl->c_dmemhandle.mrc_rmr == 0)
6497387SRobert.Gordon@Sun.COM 			break;
6500Sstevel@tonic-gate 
651*9348SSiddheshwar.Mahesh@Sun.COM 		DTRACE_PROBE2(krpc__i__write__chunks, uint32_t, count,
652*9348SSiddheshwar.Mahesh@Sun.COM 		    uint32_t, wcl->c_len);
653*9348SSiddheshwar.Mahesh@Sun.COM 
6547387SRobert.Gordon@Sun.COM 		if (wcl->c_len > count) {
6557387SRobert.Gordon@Sun.COM 			wcl->c_len = count;
6567387SRobert.Gordon@Sun.COM 		}
6577387SRobert.Gordon@Sun.COM 		wcl->w.c_saddr3 = (caddr_t)memp;
6587387SRobert.Gordon@Sun.COM 
6597387SRobert.Gordon@Sun.COM 		count -= wcl->c_len;
6607387SRobert.Gordon@Sun.COM 		*numchunks +=  1;
661*9348SSiddheshwar.Mahesh@Sun.COM 		memp += wcl->c_len;
662*9348SSiddheshwar.Mahesh@Sun.COM 		wcl = wcl->c_next;
663*9348SSiddheshwar.Mahesh@Sun.COM 	}
664*9348SSiddheshwar.Mahesh@Sun.COM 
665*9348SSiddheshwar.Mahesh@Sun.COM 	/*
666*9348SSiddheshwar.Mahesh@Sun.COM 	 * Make rest of the chunks 0-len
667*9348SSiddheshwar.Mahesh@Sun.COM 	 */
668*9348SSiddheshwar.Mahesh@Sun.COM 	while (wcl != NULL) {
669*9348SSiddheshwar.Mahesh@Sun.COM 		if (wcl->c_dmemhandle.mrc_rmr == 0)
6707387SRobert.Gordon@Sun.COM 			break;
671*9348SSiddheshwar.Mahesh@Sun.COM 		wcl->c_len = 0;
6727387SRobert.Gordon@Sun.COM 		wcl = wcl->c_next;
6737387SRobert.Gordon@Sun.COM 	}
6747387SRobert.Gordon@Sun.COM 
6757387SRobert.Gordon@Sun.COM 	wcl = crdp->cl_reply;
6767387SRobert.Gordon@Sun.COM 
6777387SRobert.Gordon@Sun.COM 	/*
6787387SRobert.Gordon@Sun.COM 	 * MUST fail if there are still more data
6797387SRobert.Gordon@Sun.COM 	 */
6807387SRobert.Gordon@Sun.COM 	if (count > 0) {
6817387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
6827387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__dlen__clist);
6837387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6847387SRobert.Gordon@Sun.COM 	}
6857387SRobert.Gordon@Sun.COM 
6867387SRobert.Gordon@Sun.COM 	if (clist_register(crdp->conn, wcl, CLIST_REG_SOURCE) != RDMA_SUCCESS) {
6877387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
6887387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__clistreg);
6897387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6907387SRobert.Gordon@Sun.COM 	}
6917387SRobert.Gordon@Sun.COM 
6927387SRobert.Gordon@Sun.COM 	status = clist_syncmem(crdp->conn, wcl, CLIST_REG_SOURCE);
6937387SRobert.Gordon@Sun.COM 
6947387SRobert.Gordon@Sun.COM 	if (status) {
695*9348SSiddheshwar.Mahesh@Sun.COM 		(void) clist_deregister(crdp->conn, wcl);
6967387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
6977387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__syncmem);
6987387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6990Sstevel@tonic-gate 	}
7007387SRobert.Gordon@Sun.COM 
7017387SRobert.Gordon@Sun.COM 	status = RDMA_WRITE(crdp->conn, wcl, WAIT);
7027387SRobert.Gordon@Sun.COM 
703*9348SSiddheshwar.Mahesh@Sun.COM 	(void) clist_deregister(crdp->conn, wcl);
7047387SRobert.Gordon@Sun.COM 	rdma_buf_free(crdp->conn, &wcl->rb_longbuf);
7057387SRobert.Gordon@Sun.COM 
7067387SRobert.Gordon@Sun.COM 	if (status != RDMA_SUCCESS) {
7077387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__write);
7087387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
7097387SRobert.Gordon@Sun.COM 	}
7107387SRobert.Gordon@Sun.COM 
7117387SRobert.Gordon@Sun.COM 	return (SVC_RDMA_SUCCESS);
7127387SRobert.Gordon@Sun.COM }
7137387SRobert.Gordon@Sun.COM 
7140Sstevel@tonic-gate 
7157387SRobert.Gordon@Sun.COM static int
7167387SRobert.Gordon@Sun.COM svc_compose_rpcmsg(SVCXPRT * clone_xprt, CONN * conn, xdrproc_t xdr_results,
7177387SRobert.Gordon@Sun.COM     caddr_t xdr_location, rdma_buf_t *rpcreply, XDR ** xdrs,
7187387SRobert.Gordon@Sun.COM     struct rpc_msg *msg, bool_t has_args, uint_t *len)
7197387SRobert.Gordon@Sun.COM {
7207387SRobert.Gordon@Sun.COM 	/*
7217387SRobert.Gordon@Sun.COM 	 * Get a pre-allocated buffer for rpc reply
7227387SRobert.Gordon@Sun.COM 	 */
7237387SRobert.Gordon@Sun.COM 	rpcreply->type = SEND_BUFFER;
7247387SRobert.Gordon@Sun.COM 	if (rdma_buf_alloc(conn, rpcreply)) {
7257387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__rpcmsg__reply__nofreebufs);
7267387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
7277387SRobert.Gordon@Sun.COM 	}
7287387SRobert.Gordon@Sun.COM 
7297387SRobert.Gordon@Sun.COM 	xdrrdma_create(*xdrs, rpcreply->addr, rpcreply->len,
7307387SRobert.Gordon@Sun.COM 	    0, NULL, XDR_ENCODE, conn);
7317387SRobert.Gordon@Sun.COM 
7327387SRobert.Gordon@Sun.COM 	msg->rm_xid = clone_xprt->xp_xid;
7337387SRobert.Gordon@Sun.COM 
7347387SRobert.Gordon@Sun.COM 	if (has_args) {
7357387SRobert.Gordon@Sun.COM 		if (!(xdr_replymsg(*xdrs, msg) &&
7367387SRobert.Gordon@Sun.COM 		    (!has_args ||
7377387SRobert.Gordon@Sun.COM 		    SVCAUTH_WRAP(&clone_xprt->xp_auth, *xdrs,
7387387SRobert.Gordon@Sun.COM 		    xdr_results, xdr_location)))) {
7397387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, rpcreply);
7407387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(
7417387SRobert.Gordon@Sun.COM 			    krpc__e__svcrdma__rpcmsg__reply__authwrap1);
7427387SRobert.Gordon@Sun.COM 			return (SVC_RDMA_FAIL);
7437387SRobert.Gordon@Sun.COM 		}
7447387SRobert.Gordon@Sun.COM 	} else {
7457387SRobert.Gordon@Sun.COM 		if (!xdr_replymsg(*xdrs, msg)) {
7467387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, rpcreply);
7477387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(
7487387SRobert.Gordon@Sun.COM 			    krpc__e__svcrdma__rpcmsg__reply__authwrap2);
7497387SRobert.Gordon@Sun.COM 			return (SVC_RDMA_FAIL);
7507387SRobert.Gordon@Sun.COM 		}
7517387SRobert.Gordon@Sun.COM 	}
7527387SRobert.Gordon@Sun.COM 
7537387SRobert.Gordon@Sun.COM 	*len = XDR_GETPOS(*xdrs);
7547387SRobert.Gordon@Sun.COM 
7557387SRobert.Gordon@Sun.COM 	return (SVC_RDMA_SUCCESS);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate  * Send rpc reply.
7600Sstevel@tonic-gate  */
7610Sstevel@tonic-gate static bool_t
7627387SRobert.Gordon@Sun.COM svc_rdma_ksend(SVCXPRT * clone_xprt, struct rpc_msg *msg)
7630Sstevel@tonic-gate {
7647387SRobert.Gordon@Sun.COM 	XDR *xdrs_rpc = &(clone_xprt->xp_xdrout);
7657387SRobert.Gordon@Sun.COM 	XDR xdrs_rhdr;
7667387SRobert.Gordon@Sun.COM 	CONN *conn = NULL;
7677387SRobert.Gordon@Sun.COM 	rdma_buf_t rbuf_resp = {0}, rbuf_rpc_resp = {0};
7687387SRobert.Gordon@Sun.COM 
7697387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
7707387SRobert.Gordon@Sun.COM 	struct clist *cl_read = NULL;
7717387SRobert.Gordon@Sun.COM 	struct clist *cl_send = NULL;
7727387SRobert.Gordon@Sun.COM 	struct clist *cl_write = NULL;
7737387SRobert.Gordon@Sun.COM 	xdrproc_t xdr_results;		/* results XDR encoding function */
7747387SRobert.Gordon@Sun.COM 	caddr_t xdr_location;		/* response results pointer */
7757387SRobert.Gordon@Sun.COM 
7760Sstevel@tonic-gate 	int retval = FALSE;
7777387SRobert.Gordon@Sun.COM 	int status, msglen, num_wreply_segments = 0;
7787387SRobert.Gordon@Sun.COM 	uint32_t rdma_credit = 0;
7797387SRobert.Gordon@Sun.COM 	int freelen = 0;
7807387SRobert.Gordon@Sun.COM 	bool_t has_args;
7817387SRobert.Gordon@Sun.COM 	uint_t  final_resp_len, rdma_response_op, vers;
7820Sstevel@tonic-gate 
7837387SRobert.Gordon@Sun.COM 	bzero(&xdrs_rhdr, sizeof (XDR));
7847387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
7857387SRobert.Gordon@Sun.COM 	conn = crdp->conn;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	 * If there is a result procedure specified in the reply message,
7890Sstevel@tonic-gate 	 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
7900Sstevel@tonic-gate 	 * We need to make sure it won't be processed twice, so we null
7910Sstevel@tonic-gate 	 * it for xdr_replymsg here.
7920Sstevel@tonic-gate 	 */
7930Sstevel@tonic-gate 	has_args = FALSE;
7940Sstevel@tonic-gate 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
7950Sstevel@tonic-gate 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
7960Sstevel@tonic-gate 		if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
7970Sstevel@tonic-gate 			has_args = TRUE;
7980Sstevel@tonic-gate 			xdr_location = msg->acpted_rply.ar_results.where;
7990Sstevel@tonic-gate 			msg->acpted_rply.ar_results.proc = xdr_void;
8000Sstevel@tonic-gate 			msg->acpted_rply.ar_results.where = NULL;
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	/*
8057387SRobert.Gordon@Sun.COM 	 * Given the limit on the inline response size (RPC_MSG_SZ),
8067387SRobert.Gordon@Sun.COM 	 * there is a need to make a guess as to the overall size of
8077387SRobert.Gordon@Sun.COM 	 * the response.  If the resultant size is beyond the inline
8087387SRobert.Gordon@Sun.COM 	 * size, then the server needs to use the "reply chunk list"
8097387SRobert.Gordon@Sun.COM 	 * provided by the client (if the client provided one).  An
8107387SRobert.Gordon@Sun.COM 	 * example of this type of response would be a READDIR
8117387SRobert.Gordon@Sun.COM 	 * response (e.g. a small directory read would fit in RPC_MSG_SZ
8127387SRobert.Gordon@Sun.COM 	 * and that is the preference but it may not fit)
8137387SRobert.Gordon@Sun.COM 	 *
8147387SRobert.Gordon@Sun.COM 	 * Combine the encoded size and the size of the true results
8157387SRobert.Gordon@Sun.COM 	 * and then make the decision about where to encode and send results.
8167387SRobert.Gordon@Sun.COM 	 *
8177387SRobert.Gordon@Sun.COM 	 * One important note, this calculation is ignoring the size
8187387SRobert.Gordon@Sun.COM 	 * of the encoding of the authentication overhead.  The reason
8197387SRobert.Gordon@Sun.COM 	 * for this is rooted in the complexities of access to the
8207387SRobert.Gordon@Sun.COM 	 * encoded size of RPCSEC_GSS related authentiation,
8217387SRobert.Gordon@Sun.COM 	 * integrity, and privacy.
8227387SRobert.Gordon@Sun.COM 	 *
8237387SRobert.Gordon@Sun.COM 	 * If it turns out that the encoded authentication bumps the
8247387SRobert.Gordon@Sun.COM 	 * response over the RPC_MSG_SZ limit, then it may need to
8257387SRobert.Gordon@Sun.COM 	 * attempt to encode for the reply chunk list.
8267387SRobert.Gordon@Sun.COM 	 */
8277387SRobert.Gordon@Sun.COM 
8287387SRobert.Gordon@Sun.COM 	/*
8297387SRobert.Gordon@Sun.COM 	 * Calculating the "sizeof" the RPC response header and the
8307387SRobert.Gordon@Sun.COM 	 * encoded results.
8310Sstevel@tonic-gate 	 */
8320Sstevel@tonic-gate 	msglen = xdr_sizeof(xdr_replymsg, msg);
8337387SRobert.Gordon@Sun.COM 
8347387SRobert.Gordon@Sun.COM 	if (msglen > 0) {
8357387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotalreplies);
8367387SRobert.Gordon@Sun.COM 	}
8377387SRobert.Gordon@Sun.COM 	if (has_args)
8380Sstevel@tonic-gate 		msglen += xdrrdma_sizeof(xdr_results, xdr_location,
8397387SRobert.Gordon@Sun.COM 		    rdma_minchunk, NULL, NULL);
8407387SRobert.Gordon@Sun.COM 
8417387SRobert.Gordon@Sun.COM 	DTRACE_PROBE1(krpc__i__svcrdma__ksend__msglen, int, msglen);
8420Sstevel@tonic-gate 
8437387SRobert.Gordon@Sun.COM 	status = SVC_RDMA_SUCCESS;
8440Sstevel@tonic-gate 
8457387SRobert.Gordon@Sun.COM 	if (msglen < RPC_MSG_SZ) {
8460Sstevel@tonic-gate 		/*
8477387SRobert.Gordon@Sun.COM 		 * Looks like the response will fit in the inline
8487387SRobert.Gordon@Sun.COM 		 * response; let's try
8490Sstevel@tonic-gate 		 */
8507387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotalinlinereplies);
8517387SRobert.Gordon@Sun.COM 
8527387SRobert.Gordon@Sun.COM 		rdma_response_op = RDMA_MSG;
8530Sstevel@tonic-gate 
8547387SRobert.Gordon@Sun.COM 		status = svc_compose_rpcmsg(clone_xprt, conn, xdr_results,
8557387SRobert.Gordon@Sun.COM 		    xdr_location, &rbuf_rpc_resp, &xdrs_rpc, msg,
8567387SRobert.Gordon@Sun.COM 		    has_args, &final_resp_len);
8577387SRobert.Gordon@Sun.COM 
8587387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__srdma__ksend__compose_status,
8597387SRobert.Gordon@Sun.COM 		    int, status);
8607387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__srdma__ksend__compose_len,
8617387SRobert.Gordon@Sun.COM 		    int, final_resp_len);
8627387SRobert.Gordon@Sun.COM 
8637387SRobert.Gordon@Sun.COM 		if (status == SVC_RDMA_SUCCESS && crdp->cl_reply) {
8647387SRobert.Gordon@Sun.COM 			clist_free(crdp->cl_reply);
8657387SRobert.Gordon@Sun.COM 			crdp->cl_reply = NULL;
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8697387SRobert.Gordon@Sun.COM 	/*
8707387SRobert.Gordon@Sun.COM 	 * If the encode failed (size?) or the message really is
8717387SRobert.Gordon@Sun.COM 	 * larger than what is allowed, try the response chunk list.
8727387SRobert.Gordon@Sun.COM 	 */
8737387SRobert.Gordon@Sun.COM 	if (status != SVC_RDMA_SUCCESS || msglen >= RPC_MSG_SZ) {
8740Sstevel@tonic-gate 		/*
8757387SRobert.Gordon@Sun.COM 		 * attempting to use a reply chunk list when there
8767387SRobert.Gordon@Sun.COM 		 * isn't one won't get very far...
8770Sstevel@tonic-gate 		 */
8787387SRobert.Gordon@Sun.COM 		if (crdp->cl_reply == NULL) {
8797387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__ksend__noreplycl);
8800Sstevel@tonic-gate 			goto out;
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 
8837387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotallongreplies);
8847387SRobert.Gordon@Sun.COM 
8857387SRobert.Gordon@Sun.COM 		msglen = xdr_sizeof(xdr_replymsg, msg);
8867387SRobert.Gordon@Sun.COM 		msglen += xdrrdma_sizeof(xdr_results, xdr_location, 0,
8877387SRobert.Gordon@Sun.COM 		    NULL, NULL);
8887387SRobert.Gordon@Sun.COM 
8897387SRobert.Gordon@Sun.COM 		status = svc_process_long_reply(clone_xprt, xdr_results,
8907387SRobert.Gordon@Sun.COM 		    xdr_location, msg, has_args, &msglen, &freelen,
8917387SRobert.Gordon@Sun.COM 		    &num_wreply_segments, &final_resp_len);
8927387SRobert.Gordon@Sun.COM 
8937387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__svcrdma__ksend__longreplen,
8947387SRobert.Gordon@Sun.COM 		    int, final_resp_len);
8957387SRobert.Gordon@Sun.COM 
8967387SRobert.Gordon@Sun.COM 		if (status != SVC_RDMA_SUCCESS) {
8977387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__ksend__compose__failed);
8987387SRobert.Gordon@Sun.COM 			goto out;
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 
9017387SRobert.Gordon@Sun.COM 		rdma_response_op = RDMA_NOMSG;
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9047387SRobert.Gordon@Sun.COM 	DTRACE_PROBE1(krpc__i__svcrdma__ksend__rdmamsg__len,
9057387SRobert.Gordon@Sun.COM 	    int, final_resp_len);
9060Sstevel@tonic-gate 
9077387SRobert.Gordon@Sun.COM 	rbuf_resp.type = SEND_BUFFER;
9087387SRobert.Gordon@Sun.COM 	if (rdma_buf_alloc(conn, &rbuf_resp)) {
9097387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9107387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__nofreebufs);
9117387SRobert.Gordon@Sun.COM 		goto out;
9120Sstevel@tonic-gate 	}
9130Sstevel@tonic-gate 
9147387SRobert.Gordon@Sun.COM 	rdma_credit = rdma_bufs_granted;
9157387SRobert.Gordon@Sun.COM 
9167387SRobert.Gordon@Sun.COM 	vers = RPCRDMA_VERS;
9177387SRobert.Gordon@Sun.COM 	xdrmem_create(&xdrs_rhdr, rbuf_resp.addr, rbuf_resp.len, XDR_ENCODE);
9187387SRobert.Gordon@Sun.COM 	(*(uint32_t *)rbuf_resp.addr) = msg->rm_xid;
9197387SRobert.Gordon@Sun.COM 	/* Skip xid and set the xdr position accordingly. */
9207387SRobert.Gordon@Sun.COM 	XDR_SETPOS(&xdrs_rhdr, sizeof (uint32_t));
9217387SRobert.Gordon@Sun.COM 	if (!xdr_u_int(&xdrs_rhdr, &vers) ||
9227387SRobert.Gordon@Sun.COM 	    !xdr_u_int(&xdrs_rhdr, &rdma_credit) ||
9237387SRobert.Gordon@Sun.COM 	    !xdr_u_int(&xdrs_rhdr, &rdma_response_op)) {
9247387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9257387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
9267387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__uint);
9270Sstevel@tonic-gate 		goto out;
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	/*
9317387SRobert.Gordon@Sun.COM 	 * Now XDR the read chunk list, actually always NULL
9320Sstevel@tonic-gate 	 */
9337387SRobert.Gordon@Sun.COM 	(void) xdr_encode_rlist_svc(&xdrs_rhdr, cl_read);
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	/*
9367387SRobert.Gordon@Sun.COM 	 * encode write list -- we already drove RDMA_WRITEs
9370Sstevel@tonic-gate 	 */
9387387SRobert.Gordon@Sun.COM 	cl_write = crdp->cl_wlist;
9397387SRobert.Gordon@Sun.COM 	if (!xdr_encode_wlist(&xdrs_rhdr, cl_write)) {
9407387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__enc__wlist);
9417387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9427387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
9430Sstevel@tonic-gate 		goto out;
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9477387SRobert.Gordon@Sun.COM 	 * XDR encode the RDMA_REPLY write chunk
9480Sstevel@tonic-gate 	 */
9497387SRobert.Gordon@Sun.COM 	if (!xdr_encode_reply_wchunk(&xdrs_rhdr, crdp->cl_reply,
9507387SRobert.Gordon@Sun.COM 	    num_wreply_segments)) {
9517387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9527387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
9537387SRobert.Gordon@Sun.COM 		goto out;
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9567387SRobert.Gordon@Sun.COM 	clist_add(&cl_send, 0, XDR_GETPOS(&xdrs_rhdr), &rbuf_resp.handle,
9577387SRobert.Gordon@Sun.COM 	    rbuf_resp.addr, NULL, NULL);
9580Sstevel@tonic-gate 
9597387SRobert.Gordon@Sun.COM 	if (rdma_response_op == RDMA_MSG) {
9607387SRobert.Gordon@Sun.COM 		clist_add(&cl_send, 0, final_resp_len, &rbuf_rpc_resp.handle,
9617387SRobert.Gordon@Sun.COM 		    rbuf_rpc_resp.addr, NULL, NULL);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9647387SRobert.Gordon@Sun.COM 	status = RDMA_SEND(conn, cl_send, msg->rm_xid);
9657387SRobert.Gordon@Sun.COM 
9667387SRobert.Gordon@Sun.COM 	if (status == RDMA_SUCCESS) {
9677387SRobert.Gordon@Sun.COM 		retval = TRUE;
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 
9707387SRobert.Gordon@Sun.COM out:
9710Sstevel@tonic-gate 	/*
9720Sstevel@tonic-gate 	 * Free up sendlist chunks
9730Sstevel@tonic-gate 	 */
9747387SRobert.Gordon@Sun.COM 	if (cl_send != NULL)
9757387SRobert.Gordon@Sun.COM 		clist_free(cl_send);
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	/*
9780Sstevel@tonic-gate 	 * Destroy private data for xdr rdma
9790Sstevel@tonic-gate 	 */
9807387SRobert.Gordon@Sun.COM 	if (clone_xprt->xp_xdrout.x_ops != NULL) {
9817387SRobert.Gordon@Sun.COM 		XDR_DESTROY(&(clone_xprt->xp_xdrout));
9827387SRobert.Gordon@Sun.COM 	}
9837387SRobert.Gordon@Sun.COM 
9847387SRobert.Gordon@Sun.COM 	if (crdp->cl_reply) {
9857387SRobert.Gordon@Sun.COM 		clist_free(crdp->cl_reply);
9867387SRobert.Gordon@Sun.COM 		crdp->cl_reply = NULL;
9877387SRobert.Gordon@Sun.COM 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	/*
9900Sstevel@tonic-gate 	 * This is completely disgusting.  If public is set it is
9910Sstevel@tonic-gate 	 * a pointer to a structure whose first field is the address
9920Sstevel@tonic-gate 	 * of the function to free that structure and any related
9930Sstevel@tonic-gate 	 * stuff.  (see rrokfree in nfs_xdr.c).
9940Sstevel@tonic-gate 	 */
9957387SRobert.Gordon@Sun.COM 	if (xdrs_rpc->x_public) {
9960Sstevel@tonic-gate 		/* LINTED pointer alignment */
9977387SRobert.Gordon@Sun.COM 		(**((int (**)()) xdrs_rpc->x_public)) (xdrs_rpc->x_public);
9987387SRobert.Gordon@Sun.COM 	}
9997387SRobert.Gordon@Sun.COM 
10007387SRobert.Gordon@Sun.COM 	if (xdrs_rhdr.x_ops != NULL) {
10017387SRobert.Gordon@Sun.COM 		XDR_DESTROY(&xdrs_rhdr);
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	return (retval);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate /*
10080Sstevel@tonic-gate  * Deserialize arguments.
10090Sstevel@tonic-gate  */
10100Sstevel@tonic-gate static bool_t
10110Sstevel@tonic-gate svc_rdma_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args, caddr_t args_ptr)
10120Sstevel@tonic-gate {
10130Sstevel@tonic-gate 	if ((SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
10140Sstevel@tonic-gate 	    xdr_args, args_ptr)) != TRUE)
10150Sstevel@tonic-gate 		return (FALSE);
10160Sstevel@tonic-gate 	return (TRUE);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate static bool_t
10200Sstevel@tonic-gate svc_rdma_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
10210Sstevel@tonic-gate     caddr_t args_ptr)
10220Sstevel@tonic-gate {
10237387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
10240Sstevel@tonic-gate 	bool_t retval;
10250Sstevel@tonic-gate 
10267387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
10277387SRobert.Gordon@Sun.COM 
10287387SRobert.Gordon@Sun.COM 	/*
10297387SRobert.Gordon@Sun.COM 	 * Free the args if needed then XDR_DESTROY
10307387SRobert.Gordon@Sun.COM 	 */
10310Sstevel@tonic-gate 	if (args_ptr) {
10320Sstevel@tonic-gate 		XDR	*xdrs = &clone_xprt->xp_xdrin;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 		xdrs->x_op = XDR_FREE;
10350Sstevel@tonic-gate 		retval = (*xdr_args)(xdrs, args_ptr);
10360Sstevel@tonic-gate 	}
10377387SRobert.Gordon@Sun.COM 
10380Sstevel@tonic-gate 	XDR_DESTROY(&(clone_xprt->xp_xdrin));
10397387SRobert.Gordon@Sun.COM 	rdma_buf_free(crdp->conn, &crdp->rpcbuf);
10407387SRobert.Gordon@Sun.COM 	if (crdp->cl_reply) {
10417387SRobert.Gordon@Sun.COM 		clist_free(crdp->cl_reply);
10427387SRobert.Gordon@Sun.COM 		crdp->cl_reply = NULL;
10437387SRobert.Gordon@Sun.COM 	}
10447387SRobert.Gordon@Sun.COM 	RDMA_REL_CONN(crdp->conn);
10457387SRobert.Gordon@Sun.COM 
10460Sstevel@tonic-gate 	return (retval);
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate /* ARGSUSED */
10500Sstevel@tonic-gate static int32_t *
10510Sstevel@tonic-gate svc_rdma_kgetres(SVCXPRT *clone_xprt, int size)
10520Sstevel@tonic-gate {
10530Sstevel@tonic-gate 	return (NULL);
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate /* ARGSUSED */
10570Sstevel@tonic-gate static void
10580Sstevel@tonic-gate svc_rdma_kfreeres(SVCXPRT *clone_xprt)
10590Sstevel@tonic-gate {
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * the dup cacheing routines below provide a cache of non-failure
10640Sstevel@tonic-gate  * transaction id's.  rpc service routines can use this to detect
10650Sstevel@tonic-gate  * retransmissions and re-send a non-failure response.
10660Sstevel@tonic-gate  */
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate /*
10690Sstevel@tonic-gate  * MAXDUPREQS is the number of cached items.  It should be adjusted
10700Sstevel@tonic-gate  * to the service load so that there is likely to be a response entry
10710Sstevel@tonic-gate  * when the first retransmission comes in.
10720Sstevel@tonic-gate  */
10730Sstevel@tonic-gate #define	MAXDUPREQS	1024
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate /*
10760Sstevel@tonic-gate  * This should be appropriately scaled to MAXDUPREQS.
10770Sstevel@tonic-gate  */
10780Sstevel@tonic-gate #define	DRHASHSZ	257
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate #if ((DRHASHSZ & (DRHASHSZ - 1)) == 0)
10810Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) & (DRHASHSZ - 1))
10820Sstevel@tonic-gate #else
10830Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) % DRHASHSZ)
10840Sstevel@tonic-gate #endif
10850Sstevel@tonic-gate #define	DRHASH(dr)	XIDHASH((dr)->dr_xid)
10860Sstevel@tonic-gate #define	REQTOXID(req)	((req)->rq_xprt->xp_xid)
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate static int	rdmandupreqs = 0;
10897803Sgt29601@anthrax int	rdmamaxdupreqs = MAXDUPREQS;
10900Sstevel@tonic-gate static kmutex_t rdmadupreq_lock;
10910Sstevel@tonic-gate static struct dupreq *rdmadrhashtbl[DRHASHSZ];
10920Sstevel@tonic-gate static int	rdmadrhashstat[DRHASHSZ];
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate static void unhash(struct dupreq *);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate /*
10970Sstevel@tonic-gate  * rdmadrmru points to the head of a circular linked list in lru order.
10980Sstevel@tonic-gate  * rdmadrmru->dr_next == drlru
10990Sstevel@tonic-gate  */
11000Sstevel@tonic-gate struct dupreq *rdmadrmru;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate  * svc_rdma_kdup searches the request cache and returns 0 if the
11040Sstevel@tonic-gate  * request is not found in the cache.  If it is found, then it
11050Sstevel@tonic-gate  * returns the state of the request (in progress or done) and
11060Sstevel@tonic-gate  * the status or attributes that were part of the original reply.
11070Sstevel@tonic-gate  */
11080Sstevel@tonic-gate static int
11090Sstevel@tonic-gate svc_rdma_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
11100Sstevel@tonic-gate 	bool_t *dupcachedp)
11110Sstevel@tonic-gate {
11120Sstevel@tonic-gate 	struct dupreq *dr;
11130Sstevel@tonic-gate 	uint32_t xid;
11140Sstevel@tonic-gate 	uint32_t drhash;
11150Sstevel@tonic-gate 	int status;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	xid = REQTOXID(req);
11180Sstevel@tonic-gate 	mutex_enter(&rdmadupreq_lock);
11190Sstevel@tonic-gate 	RSSTAT_INCR(rsdupchecks);
11200Sstevel@tonic-gate 	/*
11210Sstevel@tonic-gate 	 * Check to see whether an entry already exists in the cache.
11220Sstevel@tonic-gate 	 */
11230Sstevel@tonic-gate 	dr = rdmadrhashtbl[XIDHASH(xid)];
11240Sstevel@tonic-gate 	while (dr != NULL) {
11250Sstevel@tonic-gate 		if (dr->dr_xid == xid &&
11260Sstevel@tonic-gate 		    dr->dr_proc == req->rq_proc &&
11270Sstevel@tonic-gate 		    dr->dr_prog == req->rq_prog &&
11280Sstevel@tonic-gate 		    dr->dr_vers == req->rq_vers &&
11290Sstevel@tonic-gate 		    dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
11300Sstevel@tonic-gate 		    bcmp((caddr_t)dr->dr_addr.buf,
11310Sstevel@tonic-gate 		    (caddr_t)req->rq_xprt->xp_rtaddr.buf,
11320Sstevel@tonic-gate 		    dr->dr_addr.len) == 0) {
11330Sstevel@tonic-gate 			status = dr->dr_status;
11340Sstevel@tonic-gate 			if (status == DUP_DONE) {
11350Sstevel@tonic-gate 				bcopy(dr->dr_resp.buf, res, size);
11360Sstevel@tonic-gate 				if (dupcachedp != NULL)
11370Sstevel@tonic-gate 					*dupcachedp = (dr->dr_resfree != NULL);
11380Sstevel@tonic-gate 			} else {
11390Sstevel@tonic-gate 				dr->dr_status = DUP_INPROGRESS;
11400Sstevel@tonic-gate 				*drpp = dr;
11410Sstevel@tonic-gate 			}
11420Sstevel@tonic-gate 			RSSTAT_INCR(rsdupreqs);
11430Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
11440Sstevel@tonic-gate 			return (status);
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 		dr = dr->dr_chain;
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	/*
11500Sstevel@tonic-gate 	 * There wasn't an entry, either allocate a new one or recycle
11510Sstevel@tonic-gate 	 * an old one.
11520Sstevel@tonic-gate 	 */
11530Sstevel@tonic-gate 	if (rdmandupreqs < rdmamaxdupreqs) {
11540Sstevel@tonic-gate 		dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
11550Sstevel@tonic-gate 		if (dr == NULL) {
11560Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
11570Sstevel@tonic-gate 			return (DUP_ERROR);
11580Sstevel@tonic-gate 		}
11590Sstevel@tonic-gate 		dr->dr_resp.buf = NULL;
11600Sstevel@tonic-gate 		dr->dr_resp.maxlen = 0;
11610Sstevel@tonic-gate 		dr->dr_addr.buf = NULL;
11620Sstevel@tonic-gate 		dr->dr_addr.maxlen = 0;
11630Sstevel@tonic-gate 		if (rdmadrmru) {
11640Sstevel@tonic-gate 			dr->dr_next = rdmadrmru->dr_next;
11650Sstevel@tonic-gate 			rdmadrmru->dr_next = dr;
11660Sstevel@tonic-gate 		} else {
11670Sstevel@tonic-gate 			dr->dr_next = dr;
11680Sstevel@tonic-gate 		}
11690Sstevel@tonic-gate 		rdmandupreqs++;
11700Sstevel@tonic-gate 	} else {
11710Sstevel@tonic-gate 		dr = rdmadrmru->dr_next;
11720Sstevel@tonic-gate 		while (dr->dr_status == DUP_INPROGRESS) {
11730Sstevel@tonic-gate 			dr = dr->dr_next;
11740Sstevel@tonic-gate 			if (dr == rdmadrmru->dr_next) {
11750Sstevel@tonic-gate 				mutex_exit(&rdmadupreq_lock);
11760Sstevel@tonic-gate 				return (DUP_ERROR);
11770Sstevel@tonic-gate 			}
11780Sstevel@tonic-gate 		}
11790Sstevel@tonic-gate 		unhash(dr);
11800Sstevel@tonic-gate 		if (dr->dr_resfree) {
11810Sstevel@tonic-gate 			(*dr->dr_resfree)(dr->dr_resp.buf);
11820Sstevel@tonic-gate 		}
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 	dr->dr_resfree = NULL;
11850Sstevel@tonic-gate 	rdmadrmru = dr;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	dr->dr_xid = REQTOXID(req);
11880Sstevel@tonic-gate 	dr->dr_prog = req->rq_prog;
11890Sstevel@tonic-gate 	dr->dr_vers = req->rq_vers;
11900Sstevel@tonic-gate 	dr->dr_proc = req->rq_proc;
11910Sstevel@tonic-gate 	if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
11920Sstevel@tonic-gate 		if (dr->dr_addr.buf != NULL)
11930Sstevel@tonic-gate 			kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
11940Sstevel@tonic-gate 		dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
11950Sstevel@tonic-gate 		dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen, KM_NOSLEEP);
11960Sstevel@tonic-gate 		if (dr->dr_addr.buf == NULL) {
11970Sstevel@tonic-gate 			dr->dr_addr.maxlen = 0;
11980Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
11990Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
12000Sstevel@tonic-gate 			return (DUP_ERROR);
12010Sstevel@tonic-gate 		}
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 	dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
12040Sstevel@tonic-gate 	bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
12050Sstevel@tonic-gate 	if (dr->dr_resp.maxlen < size) {
12060Sstevel@tonic-gate 		if (dr->dr_resp.buf != NULL)
12070Sstevel@tonic-gate 			kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
12080Sstevel@tonic-gate 		dr->dr_resp.maxlen = (unsigned int)size;
12090Sstevel@tonic-gate 		dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
12100Sstevel@tonic-gate 		if (dr->dr_resp.buf == NULL) {
12110Sstevel@tonic-gate 			dr->dr_resp.maxlen = 0;
12120Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
12130Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
12140Sstevel@tonic-gate 			return (DUP_ERROR);
12150Sstevel@tonic-gate 		}
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 	dr->dr_status = DUP_INPROGRESS;
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
12200Sstevel@tonic-gate 	dr->dr_chain = rdmadrhashtbl[drhash];
12210Sstevel@tonic-gate 	rdmadrhashtbl[drhash] = dr;
12220Sstevel@tonic-gate 	rdmadrhashstat[drhash]++;
12230Sstevel@tonic-gate 	mutex_exit(&rdmadupreq_lock);
12240Sstevel@tonic-gate 	*drpp = dr;
12250Sstevel@tonic-gate 	return (DUP_NEW);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate /*
12290Sstevel@tonic-gate  * svc_rdma_kdupdone marks the request done (DUP_DONE or DUP_DROP)
12300Sstevel@tonic-gate  * and stores the response.
12310Sstevel@tonic-gate  */
12320Sstevel@tonic-gate static void
12330Sstevel@tonic-gate svc_rdma_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
12340Sstevel@tonic-gate 	int size, int status)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	ASSERT(dr->dr_resfree == NULL);
12370Sstevel@tonic-gate 	if (status == DUP_DONE) {
12380Sstevel@tonic-gate 		bcopy(res, dr->dr_resp.buf, size);
12390Sstevel@tonic-gate 		dr->dr_resfree = dis_resfree;
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 	dr->dr_status = status;
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate /*
12450Sstevel@tonic-gate  * This routine expects that the mutex, rdmadupreq_lock, is already held.
12460Sstevel@tonic-gate  */
12470Sstevel@tonic-gate static void
12480Sstevel@tonic-gate unhash(struct dupreq *dr)
12490Sstevel@tonic-gate {
12500Sstevel@tonic-gate 	struct dupreq *drt;
12510Sstevel@tonic-gate 	struct dupreq *drtprev = NULL;
12520Sstevel@tonic-gate 	uint32_t drhash;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&rdmadupreq_lock));
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
12570Sstevel@tonic-gate 	drt = rdmadrhashtbl[drhash];
12580Sstevel@tonic-gate 	while (drt != NULL) {
12590Sstevel@tonic-gate 		if (drt == dr) {
12600Sstevel@tonic-gate 			rdmadrhashstat[drhash]--;
12610Sstevel@tonic-gate 			if (drtprev == NULL) {
12620Sstevel@tonic-gate 				rdmadrhashtbl[drhash] = drt->dr_chain;
12630Sstevel@tonic-gate 			} else {
12640Sstevel@tonic-gate 				drtprev->dr_chain = drt->dr_chain;
12650Sstevel@tonic-gate 			}
12660Sstevel@tonic-gate 			return;
12670Sstevel@tonic-gate 		}
12680Sstevel@tonic-gate 		drtprev = drt;
12690Sstevel@tonic-gate 		drt = drt->dr_chain;
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate }
12727387SRobert.Gordon@Sun.COM 
12737387SRobert.Gordon@Sun.COM bool_t
12747387SRobert.Gordon@Sun.COM rdma_get_wchunk(struct svc_req *req, iovec_t *iov, struct clist *wlist)
12757387SRobert.Gordon@Sun.COM {
12767387SRobert.Gordon@Sun.COM 	struct clist	*clist;
12777387SRobert.Gordon@Sun.COM 	uint32_t	tlen;
12787387SRobert.Gordon@Sun.COM 
12797387SRobert.Gordon@Sun.COM 	if (req->rq_xprt->xp_type != T_RDMA) {
12807387SRobert.Gordon@Sun.COM 		return (FALSE);
12817387SRobert.Gordon@Sun.COM 	}
12827387SRobert.Gordon@Sun.COM 
12837387SRobert.Gordon@Sun.COM 	tlen = 0;
12847387SRobert.Gordon@Sun.COM 	clist = wlist;
12857387SRobert.Gordon@Sun.COM 	while (clist) {
12867387SRobert.Gordon@Sun.COM 		tlen += clist->c_len;
12877387SRobert.Gordon@Sun.COM 		clist = clist->c_next;
12887387SRobert.Gordon@Sun.COM 	}
12897387SRobert.Gordon@Sun.COM 
12907387SRobert.Gordon@Sun.COM 	/*
12917387SRobert.Gordon@Sun.COM 	 * set iov to addr+len of first segment of first wchunk of
12927387SRobert.Gordon@Sun.COM 	 * wlist sent by client.  krecv() already malloc'd a buffer
12937387SRobert.Gordon@Sun.COM 	 * large enough, but registration is deferred until we write
12947387SRobert.Gordon@Sun.COM 	 * the buffer back to (NFS) client using RDMA_WRITE.
12957387SRobert.Gordon@Sun.COM 	 */
12967387SRobert.Gordon@Sun.COM 	iov->iov_base = (caddr_t)(uintptr_t)wlist->w.c_saddr;
12977387SRobert.Gordon@Sun.COM 	iov->iov_len = tlen;
12987387SRobert.Gordon@Sun.COM 
12997387SRobert.Gordon@Sun.COM 	return (TRUE);
13007387SRobert.Gordon@Sun.COM }
1301*9348SSiddheshwar.Mahesh@Sun.COM 
1302*9348SSiddheshwar.Mahesh@Sun.COM /*
1303*9348SSiddheshwar.Mahesh@Sun.COM  * routine to setup the read chunk lists
1304*9348SSiddheshwar.Mahesh@Sun.COM  */
1305*9348SSiddheshwar.Mahesh@Sun.COM 
1306*9348SSiddheshwar.Mahesh@Sun.COM int
1307*9348SSiddheshwar.Mahesh@Sun.COM rdma_setup_read_chunks(struct clist *wcl, uint32_t count, int *wcl_len)
1308*9348SSiddheshwar.Mahesh@Sun.COM {
1309*9348SSiddheshwar.Mahesh@Sun.COM 	int		data_len, avail_len;
1310*9348SSiddheshwar.Mahesh@Sun.COM 	uint_t		round_len;
1311*9348SSiddheshwar.Mahesh@Sun.COM 
1312*9348SSiddheshwar.Mahesh@Sun.COM 	data_len = avail_len = 0;
1313*9348SSiddheshwar.Mahesh@Sun.COM 
1314*9348SSiddheshwar.Mahesh@Sun.COM 	while (wcl != NULL && count > 0) {
1315*9348SSiddheshwar.Mahesh@Sun.COM 		if (wcl->c_dmemhandle.mrc_rmr == 0)
1316*9348SSiddheshwar.Mahesh@Sun.COM 			break;
1317*9348SSiddheshwar.Mahesh@Sun.COM 
1318*9348SSiddheshwar.Mahesh@Sun.COM 		if (wcl->c_len < count) {
1319*9348SSiddheshwar.Mahesh@Sun.COM 			data_len += wcl->c_len;
1320*9348SSiddheshwar.Mahesh@Sun.COM 			avail_len = 0;
1321*9348SSiddheshwar.Mahesh@Sun.COM 		} else {
1322*9348SSiddheshwar.Mahesh@Sun.COM 			data_len += count;
1323*9348SSiddheshwar.Mahesh@Sun.COM 			avail_len = wcl->c_len - count;
1324*9348SSiddheshwar.Mahesh@Sun.COM 			wcl->c_len = count;
1325*9348SSiddheshwar.Mahesh@Sun.COM 		}
1326*9348SSiddheshwar.Mahesh@Sun.COM 		count -= wcl->c_len;
1327*9348SSiddheshwar.Mahesh@Sun.COM 
1328*9348SSiddheshwar.Mahesh@Sun.COM 		if (count == 0)
1329*9348SSiddheshwar.Mahesh@Sun.COM 			break;
1330*9348SSiddheshwar.Mahesh@Sun.COM 
1331*9348SSiddheshwar.Mahesh@Sun.COM 		wcl = wcl->c_next;
1332*9348SSiddheshwar.Mahesh@Sun.COM 	}
1333*9348SSiddheshwar.Mahesh@Sun.COM 
1334*9348SSiddheshwar.Mahesh@Sun.COM 	/*
1335*9348SSiddheshwar.Mahesh@Sun.COM 	 * MUST fail if there are still more data
1336*9348SSiddheshwar.Mahesh@Sun.COM 	 */
1337*9348SSiddheshwar.Mahesh@Sun.COM 	if (count > 0) {
1338*9348SSiddheshwar.Mahesh@Sun.COM 		DTRACE_PROBE2(krpc__e__rdma_setup_read_chunks_clist_len,
1339*9348SSiddheshwar.Mahesh@Sun.COM 		    int, data_len, int, count);
1340*9348SSiddheshwar.Mahesh@Sun.COM 		return (FALSE);
1341*9348SSiddheshwar.Mahesh@Sun.COM 	}
1342*9348SSiddheshwar.Mahesh@Sun.COM 
1343*9348SSiddheshwar.Mahesh@Sun.COM 	/*
1344*9348SSiddheshwar.Mahesh@Sun.COM 	 * Round up the last chunk to 4-byte boundary
1345*9348SSiddheshwar.Mahesh@Sun.COM 	 */
1346*9348SSiddheshwar.Mahesh@Sun.COM 	*wcl_len = roundup(data_len, BYTES_PER_XDR_UNIT);
1347*9348SSiddheshwar.Mahesh@Sun.COM 	round_len = *wcl_len - data_len;
1348*9348SSiddheshwar.Mahesh@Sun.COM 
1349*9348SSiddheshwar.Mahesh@Sun.COM 	if (round_len) {
1350*9348SSiddheshwar.Mahesh@Sun.COM 
1351*9348SSiddheshwar.Mahesh@Sun.COM 		/*
1352*9348SSiddheshwar.Mahesh@Sun.COM 		 * If there is space in the current chunk,
1353*9348SSiddheshwar.Mahesh@Sun.COM 		 * add the roundup to the chunk.
1354*9348SSiddheshwar.Mahesh@Sun.COM 		 */
1355*9348SSiddheshwar.Mahesh@Sun.COM 		if (avail_len >= round_len) {
1356*9348SSiddheshwar.Mahesh@Sun.COM 			wcl->c_len += round_len;
1357*9348SSiddheshwar.Mahesh@Sun.COM 		} else  {
1358*9348SSiddheshwar.Mahesh@Sun.COM 			/*
1359*9348SSiddheshwar.Mahesh@Sun.COM 			 * try the next one.
1360*9348SSiddheshwar.Mahesh@Sun.COM 			 */
1361*9348SSiddheshwar.Mahesh@Sun.COM 			wcl = wcl->c_next;
1362*9348SSiddheshwar.Mahesh@Sun.COM 			if ((wcl == NULL) || (wcl->c_len < round_len)) {
1363*9348SSiddheshwar.Mahesh@Sun.COM 				DTRACE_PROBE1(
1364*9348SSiddheshwar.Mahesh@Sun.COM 				    krpc__e__rdma_setup_read_chunks_rndup,
1365*9348SSiddheshwar.Mahesh@Sun.COM 				    int, round_len);
1366*9348SSiddheshwar.Mahesh@Sun.COM 				return (FALSE);
1367*9348SSiddheshwar.Mahesh@Sun.COM 			}
1368*9348SSiddheshwar.Mahesh@Sun.COM 			wcl->c_len = round_len;
1369*9348SSiddheshwar.Mahesh@Sun.COM 		}
1370*9348SSiddheshwar.Mahesh@Sun.COM 	}
1371*9348SSiddheshwar.Mahesh@Sun.COM 
1372*9348SSiddheshwar.Mahesh@Sun.COM 	wcl = wcl->c_next;
1373*9348SSiddheshwar.Mahesh@Sun.COM 
1374*9348SSiddheshwar.Mahesh@Sun.COM 	/*
1375*9348SSiddheshwar.Mahesh@Sun.COM 	 * Make rest of the chunks 0-len
1376*9348SSiddheshwar.Mahesh@Sun.COM 	 */
1377*9348SSiddheshwar.Mahesh@Sun.COM 
1378*9348SSiddheshwar.Mahesh@Sun.COM 	clist_zero_len(wcl);
1379*9348SSiddheshwar.Mahesh@Sun.COM 
1380*9348SSiddheshwar.Mahesh@Sun.COM 	return (TRUE);
1381*9348SSiddheshwar.Mahesh@Sun.COM }
1382