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 /*
22*8695SRajkumar.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 
363*8695SRajkumar.Sivaprakasam@Sun.COM 	if (rmod->rdma_count != 0)
364*8695SRajkumar.Sivaprakasam@Sun.COM 		(*rmod->rdma_ops->rdma_svc_listen)(svcdata);
365*8695SRajkumar.Sivaprakasam@Sun.COM 	else
366*8695SRajkumar.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 	/*
379*8695SRajkumar.Sivaprakasam@Sun.COM 	 * Call the stop listener routine for each plugin. If rdma_count is
380*8695SRajkumar.Sivaprakasam@Sun.COM 	 * already zero set active to zero.
3810Sstevel@tonic-gate 	 */
382*8695SRajkumar.Sivaprakasam@Sun.COM 	if (rmod->rdma_count != 0)
383*8695SRajkumar.Sivaprakasam@Sun.COM 		(*rmod->rdma_ops->rdma_svc_stop)(svcdata);
384*8695SRajkumar.Sivaprakasam@Sun.COM 	else
385*8695SRajkumar.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);
5047387SRobert.Gordon@Sun.COM 			(void) clist_deregister(conn, cllong, CLIST_REG_DST);
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);
5117387SRobert.Gordon@Sun.COM 		(void) clist_deregister(conn, cllong, CLIST_REG_DST);
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 
6387387SRobert.Gordon@Sun.COM 	*numchunks = 0;
6397387SRobert.Gordon@Sun.COM 	*freelen = 0;
6407387SRobert.Gordon@Sun.COM 
6417387SRobert.Gordon@Sun.COM 	wcl = crdp->cl_reply;
6427387SRobert.Gordon@Sun.COM 	wcl->rb_longbuf = long_rpc;
6437387SRobert.Gordon@Sun.COM 
6447387SRobert.Gordon@Sun.COM 	count = *final_len;
6457387SRobert.Gordon@Sun.COM 	while (wcl != NULL) {
6467387SRobert.Gordon@Sun.COM 		if (wcl->c_dmemhandle.mrc_rmr == 0)
6477387SRobert.Gordon@Sun.COM 			break;
6480Sstevel@tonic-gate 
6497387SRobert.Gordon@Sun.COM 		if (wcl->c_len > count) {
6507387SRobert.Gordon@Sun.COM 			wcl->c_len = count;
6517387SRobert.Gordon@Sun.COM 		}
6527387SRobert.Gordon@Sun.COM 		wcl->w.c_saddr3 = (caddr_t)memp;
6537387SRobert.Gordon@Sun.COM 
6547387SRobert.Gordon@Sun.COM 		count -= wcl->c_len;
6557387SRobert.Gordon@Sun.COM 		*numchunks +=  1;
6567387SRobert.Gordon@Sun.COM 		if (count == 0)
6577387SRobert.Gordon@Sun.COM 			break;
6587387SRobert.Gordon@Sun.COM 		memp += wcl->c_len;
6597387SRobert.Gordon@Sun.COM 		wcl = wcl->c_next;
6607387SRobert.Gordon@Sun.COM 	}
6617387SRobert.Gordon@Sun.COM 
6627387SRobert.Gordon@Sun.COM 	wcl = crdp->cl_reply;
6637387SRobert.Gordon@Sun.COM 
6647387SRobert.Gordon@Sun.COM 	/*
6657387SRobert.Gordon@Sun.COM 	 * MUST fail if there are still more data
6667387SRobert.Gordon@Sun.COM 	 */
6677387SRobert.Gordon@Sun.COM 	if (count > 0) {
6687387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
6697387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__dlen__clist);
6707387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6717387SRobert.Gordon@Sun.COM 	}
6727387SRobert.Gordon@Sun.COM 
6737387SRobert.Gordon@Sun.COM 	if (clist_register(crdp->conn, wcl, CLIST_REG_SOURCE) != RDMA_SUCCESS) {
6747387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
6757387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__clistreg);
6767387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6777387SRobert.Gordon@Sun.COM 	}
6787387SRobert.Gordon@Sun.COM 
6797387SRobert.Gordon@Sun.COM 	status = clist_syncmem(crdp->conn, wcl, CLIST_REG_SOURCE);
6807387SRobert.Gordon@Sun.COM 
6817387SRobert.Gordon@Sun.COM 	if (status) {
6827387SRobert.Gordon@Sun.COM 		(void) clist_deregister(crdp->conn, wcl, CLIST_REG_SOURCE);
6837387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
6847387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__syncmem);
6857387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6860Sstevel@tonic-gate 	}
6877387SRobert.Gordon@Sun.COM 
6887387SRobert.Gordon@Sun.COM 	status = RDMA_WRITE(crdp->conn, wcl, WAIT);
6897387SRobert.Gordon@Sun.COM 
6907387SRobert.Gordon@Sun.COM 	(void) clist_deregister(crdp->conn, wcl, CLIST_REG_SOURCE);
6917387SRobert.Gordon@Sun.COM 	rdma_buf_free(crdp->conn, &wcl->rb_longbuf);
6927387SRobert.Gordon@Sun.COM 
6937387SRobert.Gordon@Sun.COM 	if (status != RDMA_SUCCESS) {
6947387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__write);
6957387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6967387SRobert.Gordon@Sun.COM 	}
6977387SRobert.Gordon@Sun.COM 
6987387SRobert.Gordon@Sun.COM 	return (SVC_RDMA_SUCCESS);
6997387SRobert.Gordon@Sun.COM }
7007387SRobert.Gordon@Sun.COM 
7010Sstevel@tonic-gate 
7027387SRobert.Gordon@Sun.COM static int
7037387SRobert.Gordon@Sun.COM svc_compose_rpcmsg(SVCXPRT * clone_xprt, CONN * conn, xdrproc_t xdr_results,
7047387SRobert.Gordon@Sun.COM     caddr_t xdr_location, rdma_buf_t *rpcreply, XDR ** xdrs,
7057387SRobert.Gordon@Sun.COM     struct rpc_msg *msg, bool_t has_args, uint_t *len)
7067387SRobert.Gordon@Sun.COM {
7077387SRobert.Gordon@Sun.COM 	/*
7087387SRobert.Gordon@Sun.COM 	 * Get a pre-allocated buffer for rpc reply
7097387SRobert.Gordon@Sun.COM 	 */
7107387SRobert.Gordon@Sun.COM 	rpcreply->type = SEND_BUFFER;
7117387SRobert.Gordon@Sun.COM 	if (rdma_buf_alloc(conn, rpcreply)) {
7127387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__rpcmsg__reply__nofreebufs);
7137387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
7147387SRobert.Gordon@Sun.COM 	}
7157387SRobert.Gordon@Sun.COM 
7167387SRobert.Gordon@Sun.COM 	xdrrdma_create(*xdrs, rpcreply->addr, rpcreply->len,
7177387SRobert.Gordon@Sun.COM 	    0, NULL, XDR_ENCODE, conn);
7187387SRobert.Gordon@Sun.COM 
7197387SRobert.Gordon@Sun.COM 	msg->rm_xid = clone_xprt->xp_xid;
7207387SRobert.Gordon@Sun.COM 
7217387SRobert.Gordon@Sun.COM 	if (has_args) {
7227387SRobert.Gordon@Sun.COM 		if (!(xdr_replymsg(*xdrs, msg) &&
7237387SRobert.Gordon@Sun.COM 		    (!has_args ||
7247387SRobert.Gordon@Sun.COM 		    SVCAUTH_WRAP(&clone_xprt->xp_auth, *xdrs,
7257387SRobert.Gordon@Sun.COM 		    xdr_results, xdr_location)))) {
7267387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, rpcreply);
7277387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(
7287387SRobert.Gordon@Sun.COM 			    krpc__e__svcrdma__rpcmsg__reply__authwrap1);
7297387SRobert.Gordon@Sun.COM 			return (SVC_RDMA_FAIL);
7307387SRobert.Gordon@Sun.COM 		}
7317387SRobert.Gordon@Sun.COM 	} else {
7327387SRobert.Gordon@Sun.COM 		if (!xdr_replymsg(*xdrs, msg)) {
7337387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, rpcreply);
7347387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(
7357387SRobert.Gordon@Sun.COM 			    krpc__e__svcrdma__rpcmsg__reply__authwrap2);
7367387SRobert.Gordon@Sun.COM 			return (SVC_RDMA_FAIL);
7377387SRobert.Gordon@Sun.COM 		}
7387387SRobert.Gordon@Sun.COM 	}
7397387SRobert.Gordon@Sun.COM 
7407387SRobert.Gordon@Sun.COM 	*len = XDR_GETPOS(*xdrs);
7417387SRobert.Gordon@Sun.COM 
7427387SRobert.Gordon@Sun.COM 	return (SVC_RDMA_SUCCESS);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate  * Send rpc reply.
7470Sstevel@tonic-gate  */
7480Sstevel@tonic-gate static bool_t
7497387SRobert.Gordon@Sun.COM svc_rdma_ksend(SVCXPRT * clone_xprt, struct rpc_msg *msg)
7500Sstevel@tonic-gate {
7517387SRobert.Gordon@Sun.COM 	XDR *xdrs_rpc = &(clone_xprt->xp_xdrout);
7527387SRobert.Gordon@Sun.COM 	XDR xdrs_rhdr;
7537387SRobert.Gordon@Sun.COM 	CONN *conn = NULL;
7547387SRobert.Gordon@Sun.COM 	rdma_buf_t rbuf_resp = {0}, rbuf_rpc_resp = {0};
7557387SRobert.Gordon@Sun.COM 
7567387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
7577387SRobert.Gordon@Sun.COM 	struct clist *cl_read = NULL;
7587387SRobert.Gordon@Sun.COM 	struct clist *cl_send = NULL;
7597387SRobert.Gordon@Sun.COM 	struct clist *cl_write = NULL;
7607387SRobert.Gordon@Sun.COM 	xdrproc_t xdr_results;		/* results XDR encoding function */
7617387SRobert.Gordon@Sun.COM 	caddr_t xdr_location;		/* response results pointer */
7627387SRobert.Gordon@Sun.COM 
7630Sstevel@tonic-gate 	int retval = FALSE;
7647387SRobert.Gordon@Sun.COM 	int status, msglen, num_wreply_segments = 0;
7657387SRobert.Gordon@Sun.COM 	uint32_t rdma_credit = 0;
7667387SRobert.Gordon@Sun.COM 	int freelen = 0;
7677387SRobert.Gordon@Sun.COM 	bool_t has_args;
7687387SRobert.Gordon@Sun.COM 	uint_t  final_resp_len, rdma_response_op, vers;
7690Sstevel@tonic-gate 
7707387SRobert.Gordon@Sun.COM 	bzero(&xdrs_rhdr, sizeof (XDR));
7717387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
7727387SRobert.Gordon@Sun.COM 	conn = crdp->conn;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	/*
7750Sstevel@tonic-gate 	 * If there is a result procedure specified in the reply message,
7760Sstevel@tonic-gate 	 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
7770Sstevel@tonic-gate 	 * We need to make sure it won't be processed twice, so we null
7780Sstevel@tonic-gate 	 * it for xdr_replymsg here.
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	has_args = FALSE;
7810Sstevel@tonic-gate 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
7820Sstevel@tonic-gate 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
7830Sstevel@tonic-gate 		if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
7840Sstevel@tonic-gate 			has_args = TRUE;
7850Sstevel@tonic-gate 			xdr_location = msg->acpted_rply.ar_results.where;
7860Sstevel@tonic-gate 			msg->acpted_rply.ar_results.proc = xdr_void;
7870Sstevel@tonic-gate 			msg->acpted_rply.ar_results.where = NULL;
7880Sstevel@tonic-gate 		}
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	/*
7927387SRobert.Gordon@Sun.COM 	 * Given the limit on the inline response size (RPC_MSG_SZ),
7937387SRobert.Gordon@Sun.COM 	 * there is a need to make a guess as to the overall size of
7947387SRobert.Gordon@Sun.COM 	 * the response.  If the resultant size is beyond the inline
7957387SRobert.Gordon@Sun.COM 	 * size, then the server needs to use the "reply chunk list"
7967387SRobert.Gordon@Sun.COM 	 * provided by the client (if the client provided one).  An
7977387SRobert.Gordon@Sun.COM 	 * example of this type of response would be a READDIR
7987387SRobert.Gordon@Sun.COM 	 * response (e.g. a small directory read would fit in RPC_MSG_SZ
7997387SRobert.Gordon@Sun.COM 	 * and that is the preference but it may not fit)
8007387SRobert.Gordon@Sun.COM 	 *
8017387SRobert.Gordon@Sun.COM 	 * Combine the encoded size and the size of the true results
8027387SRobert.Gordon@Sun.COM 	 * and then make the decision about where to encode and send results.
8037387SRobert.Gordon@Sun.COM 	 *
8047387SRobert.Gordon@Sun.COM 	 * One important note, this calculation is ignoring the size
8057387SRobert.Gordon@Sun.COM 	 * of the encoding of the authentication overhead.  The reason
8067387SRobert.Gordon@Sun.COM 	 * for this is rooted in the complexities of access to the
8077387SRobert.Gordon@Sun.COM 	 * encoded size of RPCSEC_GSS related authentiation,
8087387SRobert.Gordon@Sun.COM 	 * integrity, and privacy.
8097387SRobert.Gordon@Sun.COM 	 *
8107387SRobert.Gordon@Sun.COM 	 * If it turns out that the encoded authentication bumps the
8117387SRobert.Gordon@Sun.COM 	 * response over the RPC_MSG_SZ limit, then it may need to
8127387SRobert.Gordon@Sun.COM 	 * attempt to encode for the reply chunk list.
8137387SRobert.Gordon@Sun.COM 	 */
8147387SRobert.Gordon@Sun.COM 
8157387SRobert.Gordon@Sun.COM 	/*
8167387SRobert.Gordon@Sun.COM 	 * Calculating the "sizeof" the RPC response header and the
8177387SRobert.Gordon@Sun.COM 	 * encoded results.
8180Sstevel@tonic-gate 	 */
8190Sstevel@tonic-gate 	msglen = xdr_sizeof(xdr_replymsg, msg);
8207387SRobert.Gordon@Sun.COM 
8217387SRobert.Gordon@Sun.COM 	if (msglen > 0) {
8227387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotalreplies);
8237387SRobert.Gordon@Sun.COM 	}
8247387SRobert.Gordon@Sun.COM 	if (has_args)
8250Sstevel@tonic-gate 		msglen += xdrrdma_sizeof(xdr_results, xdr_location,
8267387SRobert.Gordon@Sun.COM 		    rdma_minchunk, NULL, NULL);
8277387SRobert.Gordon@Sun.COM 
8287387SRobert.Gordon@Sun.COM 	DTRACE_PROBE1(krpc__i__svcrdma__ksend__msglen, int, msglen);
8290Sstevel@tonic-gate 
8307387SRobert.Gordon@Sun.COM 	status = SVC_RDMA_SUCCESS;
8310Sstevel@tonic-gate 
8327387SRobert.Gordon@Sun.COM 	if (msglen < RPC_MSG_SZ) {
8330Sstevel@tonic-gate 		/*
8347387SRobert.Gordon@Sun.COM 		 * Looks like the response will fit in the inline
8357387SRobert.Gordon@Sun.COM 		 * response; let's try
8360Sstevel@tonic-gate 		 */
8377387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotalinlinereplies);
8387387SRobert.Gordon@Sun.COM 
8397387SRobert.Gordon@Sun.COM 		rdma_response_op = RDMA_MSG;
8400Sstevel@tonic-gate 
8417387SRobert.Gordon@Sun.COM 		status = svc_compose_rpcmsg(clone_xprt, conn, xdr_results,
8427387SRobert.Gordon@Sun.COM 		    xdr_location, &rbuf_rpc_resp, &xdrs_rpc, msg,
8437387SRobert.Gordon@Sun.COM 		    has_args, &final_resp_len);
8447387SRobert.Gordon@Sun.COM 
8457387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__srdma__ksend__compose_status,
8467387SRobert.Gordon@Sun.COM 		    int, status);
8477387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__srdma__ksend__compose_len,
8487387SRobert.Gordon@Sun.COM 		    int, final_resp_len);
8497387SRobert.Gordon@Sun.COM 
8507387SRobert.Gordon@Sun.COM 		if (status == SVC_RDMA_SUCCESS && crdp->cl_reply) {
8517387SRobert.Gordon@Sun.COM 			clist_free(crdp->cl_reply);
8527387SRobert.Gordon@Sun.COM 			crdp->cl_reply = NULL;
8530Sstevel@tonic-gate 		}
8540Sstevel@tonic-gate 	}
8550Sstevel@tonic-gate 
8567387SRobert.Gordon@Sun.COM 	/*
8577387SRobert.Gordon@Sun.COM 	 * If the encode failed (size?) or the message really is
8587387SRobert.Gordon@Sun.COM 	 * larger than what is allowed, try the response chunk list.
8597387SRobert.Gordon@Sun.COM 	 */
8607387SRobert.Gordon@Sun.COM 	if (status != SVC_RDMA_SUCCESS || msglen >= RPC_MSG_SZ) {
8610Sstevel@tonic-gate 		/*
8627387SRobert.Gordon@Sun.COM 		 * attempting to use a reply chunk list when there
8637387SRobert.Gordon@Sun.COM 		 * isn't one won't get very far...
8640Sstevel@tonic-gate 		 */
8657387SRobert.Gordon@Sun.COM 		if (crdp->cl_reply == NULL) {
8667387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__ksend__noreplycl);
8670Sstevel@tonic-gate 			goto out;
8680Sstevel@tonic-gate 		}
8690Sstevel@tonic-gate 
8707387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotallongreplies);
8717387SRobert.Gordon@Sun.COM 
8727387SRobert.Gordon@Sun.COM 		msglen = xdr_sizeof(xdr_replymsg, msg);
8737387SRobert.Gordon@Sun.COM 		msglen += xdrrdma_sizeof(xdr_results, xdr_location, 0,
8747387SRobert.Gordon@Sun.COM 		    NULL, NULL);
8757387SRobert.Gordon@Sun.COM 
8767387SRobert.Gordon@Sun.COM 		status = svc_process_long_reply(clone_xprt, xdr_results,
8777387SRobert.Gordon@Sun.COM 		    xdr_location, msg, has_args, &msglen, &freelen,
8787387SRobert.Gordon@Sun.COM 		    &num_wreply_segments, &final_resp_len);
8797387SRobert.Gordon@Sun.COM 
8807387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__svcrdma__ksend__longreplen,
8817387SRobert.Gordon@Sun.COM 		    int, final_resp_len);
8827387SRobert.Gordon@Sun.COM 
8837387SRobert.Gordon@Sun.COM 		if (status != SVC_RDMA_SUCCESS) {
8847387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__ksend__compose__failed);
8857387SRobert.Gordon@Sun.COM 			goto out;
8860Sstevel@tonic-gate 		}
8870Sstevel@tonic-gate 
8887387SRobert.Gordon@Sun.COM 		rdma_response_op = RDMA_NOMSG;
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8917387SRobert.Gordon@Sun.COM 	DTRACE_PROBE1(krpc__i__svcrdma__ksend__rdmamsg__len,
8927387SRobert.Gordon@Sun.COM 	    int, final_resp_len);
8930Sstevel@tonic-gate 
8947387SRobert.Gordon@Sun.COM 	rbuf_resp.type = SEND_BUFFER;
8957387SRobert.Gordon@Sun.COM 	if (rdma_buf_alloc(conn, &rbuf_resp)) {
8967387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
8977387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__nofreebufs);
8987387SRobert.Gordon@Sun.COM 		goto out;
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9017387SRobert.Gordon@Sun.COM 	rdma_credit = rdma_bufs_granted;
9027387SRobert.Gordon@Sun.COM 
9037387SRobert.Gordon@Sun.COM 	vers = RPCRDMA_VERS;
9047387SRobert.Gordon@Sun.COM 	xdrmem_create(&xdrs_rhdr, rbuf_resp.addr, rbuf_resp.len, XDR_ENCODE);
9057387SRobert.Gordon@Sun.COM 	(*(uint32_t *)rbuf_resp.addr) = msg->rm_xid;
9067387SRobert.Gordon@Sun.COM 	/* Skip xid and set the xdr position accordingly. */
9077387SRobert.Gordon@Sun.COM 	XDR_SETPOS(&xdrs_rhdr, sizeof (uint32_t));
9087387SRobert.Gordon@Sun.COM 	if (!xdr_u_int(&xdrs_rhdr, &vers) ||
9097387SRobert.Gordon@Sun.COM 	    !xdr_u_int(&xdrs_rhdr, &rdma_credit) ||
9107387SRobert.Gordon@Sun.COM 	    !xdr_u_int(&xdrs_rhdr, &rdma_response_op)) {
9117387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9127387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
9137387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__uint);
9140Sstevel@tonic-gate 		goto out;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	/*
9187387SRobert.Gordon@Sun.COM 	 * Now XDR the read chunk list, actually always NULL
9190Sstevel@tonic-gate 	 */
9207387SRobert.Gordon@Sun.COM 	(void) xdr_encode_rlist_svc(&xdrs_rhdr, cl_read);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/*
9237387SRobert.Gordon@Sun.COM 	 * encode write list -- we already drove RDMA_WRITEs
9240Sstevel@tonic-gate 	 */
9257387SRobert.Gordon@Sun.COM 	cl_write = crdp->cl_wlist;
9267387SRobert.Gordon@Sun.COM 	if (!xdr_encode_wlist(&xdrs_rhdr, cl_write)) {
9277387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__enc__wlist);
9287387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9297387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
9300Sstevel@tonic-gate 		goto out;
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	/*
9347387SRobert.Gordon@Sun.COM 	 * XDR encode the RDMA_REPLY write chunk
9350Sstevel@tonic-gate 	 */
9367387SRobert.Gordon@Sun.COM 	if (!xdr_encode_reply_wchunk(&xdrs_rhdr, crdp->cl_reply,
9377387SRobert.Gordon@Sun.COM 	    num_wreply_segments)) {
9387387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9397387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
9407387SRobert.Gordon@Sun.COM 		goto out;
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9437387SRobert.Gordon@Sun.COM 	clist_add(&cl_send, 0, XDR_GETPOS(&xdrs_rhdr), &rbuf_resp.handle,
9447387SRobert.Gordon@Sun.COM 	    rbuf_resp.addr, NULL, NULL);
9450Sstevel@tonic-gate 
9467387SRobert.Gordon@Sun.COM 	if (rdma_response_op == RDMA_MSG) {
9477387SRobert.Gordon@Sun.COM 		clist_add(&cl_send, 0, final_resp_len, &rbuf_rpc_resp.handle,
9487387SRobert.Gordon@Sun.COM 		    rbuf_rpc_resp.addr, NULL, NULL);
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9517387SRobert.Gordon@Sun.COM 	status = RDMA_SEND(conn, cl_send, msg->rm_xid);
9527387SRobert.Gordon@Sun.COM 
9537387SRobert.Gordon@Sun.COM 	if (status == RDMA_SUCCESS) {
9547387SRobert.Gordon@Sun.COM 		retval = TRUE;
9550Sstevel@tonic-gate 	}
9560Sstevel@tonic-gate 
9577387SRobert.Gordon@Sun.COM out:
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * Free up sendlist chunks
9600Sstevel@tonic-gate 	 */
9617387SRobert.Gordon@Sun.COM 	if (cl_send != NULL)
9627387SRobert.Gordon@Sun.COM 		clist_free(cl_send);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/*
9650Sstevel@tonic-gate 	 * Destroy private data for xdr rdma
9660Sstevel@tonic-gate 	 */
9677387SRobert.Gordon@Sun.COM 	if (clone_xprt->xp_xdrout.x_ops != NULL) {
9687387SRobert.Gordon@Sun.COM 		XDR_DESTROY(&(clone_xprt->xp_xdrout));
9697387SRobert.Gordon@Sun.COM 	}
9707387SRobert.Gordon@Sun.COM 
9717387SRobert.Gordon@Sun.COM 	if (crdp->cl_reply) {
9727387SRobert.Gordon@Sun.COM 		clist_free(crdp->cl_reply);
9737387SRobert.Gordon@Sun.COM 		crdp->cl_reply = NULL;
9747387SRobert.Gordon@Sun.COM 	}
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	/*
9770Sstevel@tonic-gate 	 * This is completely disgusting.  If public is set it is
9780Sstevel@tonic-gate 	 * a pointer to a structure whose first field is the address
9790Sstevel@tonic-gate 	 * of the function to free that structure and any related
9800Sstevel@tonic-gate 	 * stuff.  (see rrokfree in nfs_xdr.c).
9810Sstevel@tonic-gate 	 */
9827387SRobert.Gordon@Sun.COM 	if (xdrs_rpc->x_public) {
9830Sstevel@tonic-gate 		/* LINTED pointer alignment */
9847387SRobert.Gordon@Sun.COM 		(**((int (**)()) xdrs_rpc->x_public)) (xdrs_rpc->x_public);
9857387SRobert.Gordon@Sun.COM 	}
9867387SRobert.Gordon@Sun.COM 
9877387SRobert.Gordon@Sun.COM 	if (xdrs_rhdr.x_ops != NULL) {
9887387SRobert.Gordon@Sun.COM 		XDR_DESTROY(&xdrs_rhdr);
9890Sstevel@tonic-gate 	}
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	return (retval);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /*
9950Sstevel@tonic-gate  * Deserialize arguments.
9960Sstevel@tonic-gate  */
9970Sstevel@tonic-gate static bool_t
9980Sstevel@tonic-gate svc_rdma_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args, caddr_t args_ptr)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	if ((SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
10010Sstevel@tonic-gate 	    xdr_args, args_ptr)) != TRUE)
10020Sstevel@tonic-gate 		return (FALSE);
10030Sstevel@tonic-gate 	return (TRUE);
10040Sstevel@tonic-gate }
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate static bool_t
10070Sstevel@tonic-gate svc_rdma_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
10080Sstevel@tonic-gate     caddr_t args_ptr)
10090Sstevel@tonic-gate {
10107387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
10110Sstevel@tonic-gate 	bool_t retval;
10120Sstevel@tonic-gate 
10137387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
10147387SRobert.Gordon@Sun.COM 
10157387SRobert.Gordon@Sun.COM 	/*
10167387SRobert.Gordon@Sun.COM 	 * Free the args if needed then XDR_DESTROY
10177387SRobert.Gordon@Sun.COM 	 */
10180Sstevel@tonic-gate 	if (args_ptr) {
10190Sstevel@tonic-gate 		XDR	*xdrs = &clone_xprt->xp_xdrin;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 		xdrs->x_op = XDR_FREE;
10220Sstevel@tonic-gate 		retval = (*xdr_args)(xdrs, args_ptr);
10230Sstevel@tonic-gate 	}
10247387SRobert.Gordon@Sun.COM 
10250Sstevel@tonic-gate 	XDR_DESTROY(&(clone_xprt->xp_xdrin));
10267387SRobert.Gordon@Sun.COM 	rdma_buf_free(crdp->conn, &crdp->rpcbuf);
10277387SRobert.Gordon@Sun.COM 	if (crdp->cl_reply) {
10287387SRobert.Gordon@Sun.COM 		clist_free(crdp->cl_reply);
10297387SRobert.Gordon@Sun.COM 		crdp->cl_reply = NULL;
10307387SRobert.Gordon@Sun.COM 	}
10317387SRobert.Gordon@Sun.COM 	RDMA_REL_CONN(crdp->conn);
10327387SRobert.Gordon@Sun.COM 
10330Sstevel@tonic-gate 	return (retval);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate /* ARGSUSED */
10370Sstevel@tonic-gate static int32_t *
10380Sstevel@tonic-gate svc_rdma_kgetres(SVCXPRT *clone_xprt, int size)
10390Sstevel@tonic-gate {
10400Sstevel@tonic-gate 	return (NULL);
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate /* ARGSUSED */
10440Sstevel@tonic-gate static void
10450Sstevel@tonic-gate svc_rdma_kfreeres(SVCXPRT *clone_xprt)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate /*
10500Sstevel@tonic-gate  * the dup cacheing routines below provide a cache of non-failure
10510Sstevel@tonic-gate  * transaction id's.  rpc service routines can use this to detect
10520Sstevel@tonic-gate  * retransmissions and re-send a non-failure response.
10530Sstevel@tonic-gate  */
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate /*
10560Sstevel@tonic-gate  * MAXDUPREQS is the number of cached items.  It should be adjusted
10570Sstevel@tonic-gate  * to the service load so that there is likely to be a response entry
10580Sstevel@tonic-gate  * when the first retransmission comes in.
10590Sstevel@tonic-gate  */
10600Sstevel@tonic-gate #define	MAXDUPREQS	1024
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * This should be appropriately scaled to MAXDUPREQS.
10640Sstevel@tonic-gate  */
10650Sstevel@tonic-gate #define	DRHASHSZ	257
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate #if ((DRHASHSZ & (DRHASHSZ - 1)) == 0)
10680Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) & (DRHASHSZ - 1))
10690Sstevel@tonic-gate #else
10700Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) % DRHASHSZ)
10710Sstevel@tonic-gate #endif
10720Sstevel@tonic-gate #define	DRHASH(dr)	XIDHASH((dr)->dr_xid)
10730Sstevel@tonic-gate #define	REQTOXID(req)	((req)->rq_xprt->xp_xid)
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate static int	rdmandupreqs = 0;
10767803Sgt29601@anthrax int	rdmamaxdupreqs = MAXDUPREQS;
10770Sstevel@tonic-gate static kmutex_t rdmadupreq_lock;
10780Sstevel@tonic-gate static struct dupreq *rdmadrhashtbl[DRHASHSZ];
10790Sstevel@tonic-gate static int	rdmadrhashstat[DRHASHSZ];
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate static void unhash(struct dupreq *);
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate /*
10840Sstevel@tonic-gate  * rdmadrmru points to the head of a circular linked list in lru order.
10850Sstevel@tonic-gate  * rdmadrmru->dr_next == drlru
10860Sstevel@tonic-gate  */
10870Sstevel@tonic-gate struct dupreq *rdmadrmru;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate /*
10900Sstevel@tonic-gate  * svc_rdma_kdup searches the request cache and returns 0 if the
10910Sstevel@tonic-gate  * request is not found in the cache.  If it is found, then it
10920Sstevel@tonic-gate  * returns the state of the request (in progress or done) and
10930Sstevel@tonic-gate  * the status or attributes that were part of the original reply.
10940Sstevel@tonic-gate  */
10950Sstevel@tonic-gate static int
10960Sstevel@tonic-gate svc_rdma_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
10970Sstevel@tonic-gate 	bool_t *dupcachedp)
10980Sstevel@tonic-gate {
10990Sstevel@tonic-gate 	struct dupreq *dr;
11000Sstevel@tonic-gate 	uint32_t xid;
11010Sstevel@tonic-gate 	uint32_t drhash;
11020Sstevel@tonic-gate 	int status;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	xid = REQTOXID(req);
11050Sstevel@tonic-gate 	mutex_enter(&rdmadupreq_lock);
11060Sstevel@tonic-gate 	RSSTAT_INCR(rsdupchecks);
11070Sstevel@tonic-gate 	/*
11080Sstevel@tonic-gate 	 * Check to see whether an entry already exists in the cache.
11090Sstevel@tonic-gate 	 */
11100Sstevel@tonic-gate 	dr = rdmadrhashtbl[XIDHASH(xid)];
11110Sstevel@tonic-gate 	while (dr != NULL) {
11120Sstevel@tonic-gate 		if (dr->dr_xid == xid &&
11130Sstevel@tonic-gate 		    dr->dr_proc == req->rq_proc &&
11140Sstevel@tonic-gate 		    dr->dr_prog == req->rq_prog &&
11150Sstevel@tonic-gate 		    dr->dr_vers == req->rq_vers &&
11160Sstevel@tonic-gate 		    dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
11170Sstevel@tonic-gate 		    bcmp((caddr_t)dr->dr_addr.buf,
11180Sstevel@tonic-gate 		    (caddr_t)req->rq_xprt->xp_rtaddr.buf,
11190Sstevel@tonic-gate 		    dr->dr_addr.len) == 0) {
11200Sstevel@tonic-gate 			status = dr->dr_status;
11210Sstevel@tonic-gate 			if (status == DUP_DONE) {
11220Sstevel@tonic-gate 				bcopy(dr->dr_resp.buf, res, size);
11230Sstevel@tonic-gate 				if (dupcachedp != NULL)
11240Sstevel@tonic-gate 					*dupcachedp = (dr->dr_resfree != NULL);
11250Sstevel@tonic-gate 			} else {
11260Sstevel@tonic-gate 				dr->dr_status = DUP_INPROGRESS;
11270Sstevel@tonic-gate 				*drpp = dr;
11280Sstevel@tonic-gate 			}
11290Sstevel@tonic-gate 			RSSTAT_INCR(rsdupreqs);
11300Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
11310Sstevel@tonic-gate 			return (status);
11320Sstevel@tonic-gate 		}
11330Sstevel@tonic-gate 		dr = dr->dr_chain;
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	/*
11370Sstevel@tonic-gate 	 * There wasn't an entry, either allocate a new one or recycle
11380Sstevel@tonic-gate 	 * an old one.
11390Sstevel@tonic-gate 	 */
11400Sstevel@tonic-gate 	if (rdmandupreqs < rdmamaxdupreqs) {
11410Sstevel@tonic-gate 		dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
11420Sstevel@tonic-gate 		if (dr == NULL) {
11430Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
11440Sstevel@tonic-gate 			return (DUP_ERROR);
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 		dr->dr_resp.buf = NULL;
11470Sstevel@tonic-gate 		dr->dr_resp.maxlen = 0;
11480Sstevel@tonic-gate 		dr->dr_addr.buf = NULL;
11490Sstevel@tonic-gate 		dr->dr_addr.maxlen = 0;
11500Sstevel@tonic-gate 		if (rdmadrmru) {
11510Sstevel@tonic-gate 			dr->dr_next = rdmadrmru->dr_next;
11520Sstevel@tonic-gate 			rdmadrmru->dr_next = dr;
11530Sstevel@tonic-gate 		} else {
11540Sstevel@tonic-gate 			dr->dr_next = dr;
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate 		rdmandupreqs++;
11570Sstevel@tonic-gate 	} else {
11580Sstevel@tonic-gate 		dr = rdmadrmru->dr_next;
11590Sstevel@tonic-gate 		while (dr->dr_status == DUP_INPROGRESS) {
11600Sstevel@tonic-gate 			dr = dr->dr_next;
11610Sstevel@tonic-gate 			if (dr == rdmadrmru->dr_next) {
11620Sstevel@tonic-gate 				mutex_exit(&rdmadupreq_lock);
11630Sstevel@tonic-gate 				return (DUP_ERROR);
11640Sstevel@tonic-gate 			}
11650Sstevel@tonic-gate 		}
11660Sstevel@tonic-gate 		unhash(dr);
11670Sstevel@tonic-gate 		if (dr->dr_resfree) {
11680Sstevel@tonic-gate 			(*dr->dr_resfree)(dr->dr_resp.buf);
11690Sstevel@tonic-gate 		}
11700Sstevel@tonic-gate 	}
11710Sstevel@tonic-gate 	dr->dr_resfree = NULL;
11720Sstevel@tonic-gate 	rdmadrmru = dr;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	dr->dr_xid = REQTOXID(req);
11750Sstevel@tonic-gate 	dr->dr_prog = req->rq_prog;
11760Sstevel@tonic-gate 	dr->dr_vers = req->rq_vers;
11770Sstevel@tonic-gate 	dr->dr_proc = req->rq_proc;
11780Sstevel@tonic-gate 	if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
11790Sstevel@tonic-gate 		if (dr->dr_addr.buf != NULL)
11800Sstevel@tonic-gate 			kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
11810Sstevel@tonic-gate 		dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
11820Sstevel@tonic-gate 		dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen, KM_NOSLEEP);
11830Sstevel@tonic-gate 		if (dr->dr_addr.buf == NULL) {
11840Sstevel@tonic-gate 			dr->dr_addr.maxlen = 0;
11850Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
11860Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
11870Sstevel@tonic-gate 			return (DUP_ERROR);
11880Sstevel@tonic-gate 		}
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 	dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
11910Sstevel@tonic-gate 	bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
11920Sstevel@tonic-gate 	if (dr->dr_resp.maxlen < size) {
11930Sstevel@tonic-gate 		if (dr->dr_resp.buf != NULL)
11940Sstevel@tonic-gate 			kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
11950Sstevel@tonic-gate 		dr->dr_resp.maxlen = (unsigned int)size;
11960Sstevel@tonic-gate 		dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
11970Sstevel@tonic-gate 		if (dr->dr_resp.buf == NULL) {
11980Sstevel@tonic-gate 			dr->dr_resp.maxlen = 0;
11990Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
12000Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
12010Sstevel@tonic-gate 			return (DUP_ERROR);
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 	dr->dr_status = DUP_INPROGRESS;
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
12070Sstevel@tonic-gate 	dr->dr_chain = rdmadrhashtbl[drhash];
12080Sstevel@tonic-gate 	rdmadrhashtbl[drhash] = dr;
12090Sstevel@tonic-gate 	rdmadrhashstat[drhash]++;
12100Sstevel@tonic-gate 	mutex_exit(&rdmadupreq_lock);
12110Sstevel@tonic-gate 	*drpp = dr;
12120Sstevel@tonic-gate 	return (DUP_NEW);
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate /*
12160Sstevel@tonic-gate  * svc_rdma_kdupdone marks the request done (DUP_DONE or DUP_DROP)
12170Sstevel@tonic-gate  * and stores the response.
12180Sstevel@tonic-gate  */
12190Sstevel@tonic-gate static void
12200Sstevel@tonic-gate svc_rdma_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
12210Sstevel@tonic-gate 	int size, int status)
12220Sstevel@tonic-gate {
12230Sstevel@tonic-gate 	ASSERT(dr->dr_resfree == NULL);
12240Sstevel@tonic-gate 	if (status == DUP_DONE) {
12250Sstevel@tonic-gate 		bcopy(res, dr->dr_resp.buf, size);
12260Sstevel@tonic-gate 		dr->dr_resfree = dis_resfree;
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 	dr->dr_status = status;
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate /*
12320Sstevel@tonic-gate  * This routine expects that the mutex, rdmadupreq_lock, is already held.
12330Sstevel@tonic-gate  */
12340Sstevel@tonic-gate static void
12350Sstevel@tonic-gate unhash(struct dupreq *dr)
12360Sstevel@tonic-gate {
12370Sstevel@tonic-gate 	struct dupreq *drt;
12380Sstevel@tonic-gate 	struct dupreq *drtprev = NULL;
12390Sstevel@tonic-gate 	uint32_t drhash;
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&rdmadupreq_lock));
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
12440Sstevel@tonic-gate 	drt = rdmadrhashtbl[drhash];
12450Sstevel@tonic-gate 	while (drt != NULL) {
12460Sstevel@tonic-gate 		if (drt == dr) {
12470Sstevel@tonic-gate 			rdmadrhashstat[drhash]--;
12480Sstevel@tonic-gate 			if (drtprev == NULL) {
12490Sstevel@tonic-gate 				rdmadrhashtbl[drhash] = drt->dr_chain;
12500Sstevel@tonic-gate 			} else {
12510Sstevel@tonic-gate 				drtprev->dr_chain = drt->dr_chain;
12520Sstevel@tonic-gate 			}
12530Sstevel@tonic-gate 			return;
12540Sstevel@tonic-gate 		}
12550Sstevel@tonic-gate 		drtprev = drt;
12560Sstevel@tonic-gate 		drt = drt->dr_chain;
12570Sstevel@tonic-gate 	}
12580Sstevel@tonic-gate }
12597387SRobert.Gordon@Sun.COM 
12607387SRobert.Gordon@Sun.COM bool_t
12617387SRobert.Gordon@Sun.COM rdma_get_wchunk(struct svc_req *req, iovec_t *iov, struct clist *wlist)
12627387SRobert.Gordon@Sun.COM {
12637387SRobert.Gordon@Sun.COM 	struct clist	*clist;
12647387SRobert.Gordon@Sun.COM 	uint32_t	tlen;
12657387SRobert.Gordon@Sun.COM 
12667387SRobert.Gordon@Sun.COM 	if (req->rq_xprt->xp_type != T_RDMA) {
12677387SRobert.Gordon@Sun.COM 		return (FALSE);
12687387SRobert.Gordon@Sun.COM 	}
12697387SRobert.Gordon@Sun.COM 
12707387SRobert.Gordon@Sun.COM 	tlen = 0;
12717387SRobert.Gordon@Sun.COM 	clist = wlist;
12727387SRobert.Gordon@Sun.COM 	while (clist) {
12737387SRobert.Gordon@Sun.COM 		tlen += clist->c_len;
12747387SRobert.Gordon@Sun.COM 		clist = clist->c_next;
12757387SRobert.Gordon@Sun.COM 	}
12767387SRobert.Gordon@Sun.COM 
12777387SRobert.Gordon@Sun.COM 	/*
12787387SRobert.Gordon@Sun.COM 	 * set iov to addr+len of first segment of first wchunk of
12797387SRobert.Gordon@Sun.COM 	 * wlist sent by client.  krecv() already malloc'd a buffer
12807387SRobert.Gordon@Sun.COM 	 * large enough, but registration is deferred until we write
12817387SRobert.Gordon@Sun.COM 	 * the buffer back to (NFS) client using RDMA_WRITE.
12827387SRobert.Gordon@Sun.COM 	 */
12837387SRobert.Gordon@Sun.COM 	iov->iov_base = (caddr_t)(uintptr_t)wlist->w.c_saddr;
12847387SRobert.Gordon@Sun.COM 	iov->iov_len = tlen;
12857387SRobert.Gordon@Sun.COM 
12867387SRobert.Gordon@Sun.COM 	return (TRUE);
12877387SRobert.Gordon@Sun.COM }
1288