xref: /onnv-gate/usr/src/uts/common/rpc/svc_rdma.c (revision 12553:e64e5d843075)
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*12553SKaren.Rochford@Sun.COM  * Copyright (c) 1983, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
250Sstevel@tonic-gate /* All Rights Reserved */
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
280Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
290Sstevel@tonic-gate  * California.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Server side of RPC over RDMA in the kernel.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/user.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/proc.h>
410Sstevel@tonic-gate #include <sys/file.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate #include <sys/debug.h>
450Sstevel@tonic-gate #include <sys/systm.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/kstat.h>
480Sstevel@tonic-gate #include <sys/vtrace.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <rpc/types.h>
520Sstevel@tonic-gate #include <rpc/xdr.h>
530Sstevel@tonic-gate #include <rpc/auth.h>
540Sstevel@tonic-gate #include <rpc/clnt.h>
550Sstevel@tonic-gate #include <rpc/rpc_msg.h>
560Sstevel@tonic-gate #include <rpc/svc.h>
570Sstevel@tonic-gate #include <rpc/rpc_rdma.h>
580Sstevel@tonic-gate #include <sys/ddi.h>
590Sstevel@tonic-gate #include <sys/sunddi.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <inet/common.h>
620Sstevel@tonic-gate #include <inet/ip.h>
630Sstevel@tonic-gate #include <inet/ip6.h>
640Sstevel@tonic-gate 
657387SRobert.Gordon@Sun.COM #include <nfs/nfs.h>
667387SRobert.Gordon@Sun.COM #include <sys/sdt.h>
677387SRobert.Gordon@Sun.COM 
687387SRobert.Gordon@Sun.COM #define	SVC_RDMA_SUCCESS 0
697387SRobert.Gordon@Sun.COM #define	SVC_RDMA_FAIL -1
707387SRobert.Gordon@Sun.COM 
717387SRobert.Gordon@Sun.COM #define	SVC_CREDIT_FACTOR (0.5)
727387SRobert.Gordon@Sun.COM 
737387SRobert.Gordon@Sun.COM #define	MSG_IS_RPCSEC_GSS(msg)		\
747387SRobert.Gordon@Sun.COM 	((msg)->rm_reply.rp_acpt.ar_verf.oa_flavor == RPCSEC_GSS)
757387SRobert.Gordon@Sun.COM 
767387SRobert.Gordon@Sun.COM 
777387SRobert.Gordon@Sun.COM uint32_t rdma_bufs_granted = RDMA_BUFS_GRANT;
787387SRobert.Gordon@Sun.COM 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * RDMA transport specific data associated with SVCMASTERXPRT
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate struct rdma_data {
830Sstevel@tonic-gate 	SVCMASTERXPRT 	*rd_xprt;	/* back ptr to SVCMASTERXPRT */
840Sstevel@tonic-gate 	struct rdma_svc_data rd_data;	/* rdma data */
850Sstevel@tonic-gate 	rdma_mod_t	*r_mod;		/* RDMA module containing ops ptr */
860Sstevel@tonic-gate };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * Plugin connection specific data stashed away in clone SVCXPRT
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate struct clone_rdma_data {
9211967SKaren.Rochford@Sun.COM 	bool_t		cloned;		/* xprt cloned for thread processing */
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 
9911967SKaren.Rochford@Sun.COM 
1000Sstevel@tonic-gate #define	MAXADDRLEN	128	/* max length for address mask */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * Routines exported through ops vector.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate static bool_t		svc_rdma_krecv(SVCXPRT *, mblk_t *, struct rpc_msg *);
1060Sstevel@tonic-gate static bool_t		svc_rdma_ksend(SVCXPRT *, struct rpc_msg *);
1070Sstevel@tonic-gate static bool_t		svc_rdma_kgetargs(SVCXPRT *, xdrproc_t, caddr_t);
1080Sstevel@tonic-gate static bool_t		svc_rdma_kfreeargs(SVCXPRT *, xdrproc_t, caddr_t);
1090Sstevel@tonic-gate void			svc_rdma_kdestroy(SVCMASTERXPRT *);
1100Sstevel@tonic-gate static int		svc_rdma_kdup(struct svc_req *, caddr_t, int,
1110Sstevel@tonic-gate 				struct dupreq **, bool_t *);
1120Sstevel@tonic-gate static void		svc_rdma_kdupdone(struct dupreq *, caddr_t,
1130Sstevel@tonic-gate 				void (*)(), int, int);
1140Sstevel@tonic-gate static int32_t		*svc_rdma_kgetres(SVCXPRT *, int);
1150Sstevel@tonic-gate static void		svc_rdma_kfreeres(SVCXPRT *);
1160Sstevel@tonic-gate static void		svc_rdma_kclone_destroy(SVCXPRT *);
1170Sstevel@tonic-gate static void		svc_rdma_kstart(SVCMASTERXPRT *);
1180Sstevel@tonic-gate void			svc_rdma_kstop(SVCMASTERXPRT *);
11911967SKaren.Rochford@Sun.COM static void		svc_rdma_kclone_xprt(SVCXPRT *, SVCXPRT *);
120*12553SKaren.Rochford@Sun.COM static void		svc_rdma_ktattrs(SVCXPRT *, int, void **);
1210Sstevel@tonic-gate 
1227387SRobert.Gordon@Sun.COM static int	svc_process_long_reply(SVCXPRT *, xdrproc_t,
1237387SRobert.Gordon@Sun.COM 			caddr_t, struct rpc_msg *, bool_t, int *,
1247387SRobert.Gordon@Sun.COM 			int *, int *, unsigned int *);
1257387SRobert.Gordon@Sun.COM 
1267387SRobert.Gordon@Sun.COM static int	svc_compose_rpcmsg(SVCXPRT *, CONN *, xdrproc_t,
1277387SRobert.Gordon@Sun.COM 			caddr_t, rdma_buf_t *, XDR **, struct rpc_msg *,
1287387SRobert.Gordon@Sun.COM 			bool_t, uint_t *);
1297387SRobert.Gordon@Sun.COM static bool_t rpcmsg_length(xdrproc_t,
1307387SRobert.Gordon@Sun.COM 		caddr_t,
1317387SRobert.Gordon@Sun.COM 		struct rpc_msg *, bool_t, int);
1327387SRobert.Gordon@Sun.COM 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * Server transport operations vector.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate struct svc_ops rdma_svc_ops = {
1370Sstevel@tonic-gate 	svc_rdma_krecv,		/* Get requests */
1380Sstevel@tonic-gate 	svc_rdma_kgetargs,	/* Deserialize arguments */
1390Sstevel@tonic-gate 	svc_rdma_ksend,		/* Send reply */
1400Sstevel@tonic-gate 	svc_rdma_kfreeargs,	/* Free argument data space */
1410Sstevel@tonic-gate 	svc_rdma_kdestroy,	/* Destroy transport handle */
1420Sstevel@tonic-gate 	svc_rdma_kdup,		/* Check entry in dup req cache */
1430Sstevel@tonic-gate 	svc_rdma_kdupdone,	/* Mark entry in dup req cache as done */
1440Sstevel@tonic-gate 	svc_rdma_kgetres,	/* Get pointer to response buffer */
1450Sstevel@tonic-gate 	svc_rdma_kfreeres,	/* Destroy pre-serialized response header */
1460Sstevel@tonic-gate 	svc_rdma_kclone_destroy,	/* Destroy a clone xprt */
14711967SKaren.Rochford@Sun.COM 	svc_rdma_kstart,	/* Tell `ready-to-receive' to rpcmod */
148*12553SKaren.Rochford@Sun.COM 	svc_rdma_kclone_xprt,	/* Transport specific clone xprt */
149*12553SKaren.Rochford@Sun.COM 	svc_rdma_ktattrs	/* Get Transport Attributes */
1500Sstevel@tonic-gate };
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * Server statistics
1540Sstevel@tonic-gate  * NOTE: This structure type is duplicated in the NFS fast path.
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate struct {
1570Sstevel@tonic-gate 	kstat_named_t	rscalls;
1580Sstevel@tonic-gate 	kstat_named_t	rsbadcalls;
1590Sstevel@tonic-gate 	kstat_named_t	rsnullrecv;
1600Sstevel@tonic-gate 	kstat_named_t	rsbadlen;
1610Sstevel@tonic-gate 	kstat_named_t	rsxdrcall;
1620Sstevel@tonic-gate 	kstat_named_t	rsdupchecks;
1630Sstevel@tonic-gate 	kstat_named_t	rsdupreqs;
1640Sstevel@tonic-gate 	kstat_named_t	rslongrpcs;
1657387SRobert.Gordon@Sun.COM 	kstat_named_t	rstotalreplies;
1667387SRobert.Gordon@Sun.COM 	kstat_named_t	rstotallongreplies;
1677387SRobert.Gordon@Sun.COM 	kstat_named_t	rstotalinlinereplies;
1680Sstevel@tonic-gate } rdmarsstat = {
1690Sstevel@tonic-gate 	{ "calls",	KSTAT_DATA_UINT64 },
1700Sstevel@tonic-gate 	{ "badcalls",	KSTAT_DATA_UINT64 },
1710Sstevel@tonic-gate 	{ "nullrecv",	KSTAT_DATA_UINT64 },
1720Sstevel@tonic-gate 	{ "badlen",	KSTAT_DATA_UINT64 },
1730Sstevel@tonic-gate 	{ "xdrcall",	KSTAT_DATA_UINT64 },
1740Sstevel@tonic-gate 	{ "dupchecks",	KSTAT_DATA_UINT64 },
1750Sstevel@tonic-gate 	{ "dupreqs",	KSTAT_DATA_UINT64 },
1767387SRobert.Gordon@Sun.COM 	{ "longrpcs",	KSTAT_DATA_UINT64 },
1777387SRobert.Gordon@Sun.COM 	{ "totalreplies",	KSTAT_DATA_UINT64 },
1787387SRobert.Gordon@Sun.COM 	{ "totallongreplies",	KSTAT_DATA_UINT64 },
1797387SRobert.Gordon@Sun.COM 	{ "totalinlinereplies",	KSTAT_DATA_UINT64 },
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate kstat_named_t *rdmarsstat_ptr = (kstat_named_t *)&rdmarsstat;
1830Sstevel@tonic-gate uint_t rdmarsstat_ndata = sizeof (rdmarsstat) / sizeof (kstat_named_t);
1840Sstevel@tonic-gate 
1857387SRobert.Gordon@Sun.COM #define	RSSTAT_INCR(x)	atomic_add_64(&rdmarsstat.x.value.ui64, 1)
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * Create a transport record.
1880Sstevel@tonic-gate  * The transport record, output buffer, and private data structure
1890Sstevel@tonic-gate  * are allocated.  The output buffer is serialized into using xdrmem.
1900Sstevel@tonic-gate  * There is one transport record per user process which implements a
1910Sstevel@tonic-gate  * set of services.
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate /* ARGSUSED */
1940Sstevel@tonic-gate int
svc_rdma_kcreate(char * netid,SVC_CALLOUT_TABLE * sct,int id,rdma_xprt_group_t * started_xprts)1950Sstevel@tonic-gate svc_rdma_kcreate(char *netid, SVC_CALLOUT_TABLE *sct, int id,
1967387SRobert.Gordon@Sun.COM     rdma_xprt_group_t *started_xprts)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	int error;
1990Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
2000Sstevel@tonic-gate 	struct rdma_data *rd;
2010Sstevel@tonic-gate 	rdma_registry_t *rmod;
2020Sstevel@tonic-gate 	rdma_xprt_record_t *xprt_rec;
2030Sstevel@tonic-gate 	queue_t	*q;
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * modload the RDMA plugins is not already done.
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	if (!rdma_modloaded) {
2087387SRobert.Gordon@Sun.COM 		/*CONSTANTCONDITION*/
2097387SRobert.Gordon@Sun.COM 		ASSERT(sizeof (struct clone_rdma_data) <= SVC_P2LEN);
2107387SRobert.Gordon@Sun.COM 
2110Sstevel@tonic-gate 		mutex_enter(&rdma_modload_lock);
2120Sstevel@tonic-gate 		if (!rdma_modloaded) {
2130Sstevel@tonic-gate 			error = rdma_modload();
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 		mutex_exit(&rdma_modload_lock);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		if (error)
2180Sstevel@tonic-gate 			return (error);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * master_xprt_count is the count of master transport handles
2230Sstevel@tonic-gate 	 * that were successfully created and are ready to recieve for
2240Sstevel@tonic-gate 	 * RDMA based access.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	error = 0;
2270Sstevel@tonic-gate 	xprt_rec = NULL;
2280Sstevel@tonic-gate 	rw_enter(&rdma_lock, RW_READER);
2290Sstevel@tonic-gate 	if (rdma_mod_head == NULL) {
2300Sstevel@tonic-gate 		started_xprts->rtg_count = 0;
2310Sstevel@tonic-gate 		rw_exit(&rdma_lock);
2320Sstevel@tonic-gate 		if (rdma_dev_available)
2330Sstevel@tonic-gate 			return (EPROTONOSUPPORT);
2340Sstevel@tonic-gate 		else
2350Sstevel@tonic-gate 			return (ENODEV);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/*
2390Sstevel@tonic-gate 	 * If we have reached here, then atleast one RDMA plugin has loaded.
2400Sstevel@tonic-gate 	 * Create a master_xprt, make it start listenining on the device,
2410Sstevel@tonic-gate 	 * if an error is generated, record it, we might need to shut
2420Sstevel@tonic-gate 	 * the master_xprt.
2430Sstevel@tonic-gate 	 * SVC_START() calls svc_rdma_kstart which calls plugin binding
2440Sstevel@tonic-gate 	 * routines.
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	for (rmod = rdma_mod_head; rmod != NULL; rmod = rmod->r_next) {
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		/*
2490Sstevel@tonic-gate 		 * One SVCMASTERXPRT per RDMA plugin.
2500Sstevel@tonic-gate 		 */
2510Sstevel@tonic-gate 		xprt = kmem_zalloc(sizeof (*xprt), KM_SLEEP);
2520Sstevel@tonic-gate 		xprt->xp_ops = &rdma_svc_ops;
2530Sstevel@tonic-gate 		xprt->xp_sct = sct;
2540Sstevel@tonic-gate 		xprt->xp_type = T_RDMA;
2550Sstevel@tonic-gate 		mutex_init(&xprt->xp_req_lock, NULL, MUTEX_DEFAULT, NULL);
2560Sstevel@tonic-gate 		mutex_init(&xprt->xp_thread_lock, NULL, MUTEX_DEFAULT, NULL);
2570Sstevel@tonic-gate 		xprt->xp_req_head = (mblk_t *)0;
2580Sstevel@tonic-gate 		xprt->xp_req_tail = (mblk_t *)0;
2590Sstevel@tonic-gate 		xprt->xp_threads = 0;
2600Sstevel@tonic-gate 		xprt->xp_detached_threads = 0;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		rd = kmem_zalloc(sizeof (*rd), KM_SLEEP);
2630Sstevel@tonic-gate 		xprt->xp_p2 = (caddr_t)rd;
2640Sstevel@tonic-gate 		rd->rd_xprt = xprt;
2650Sstevel@tonic-gate 		rd->r_mod = rmod->r_mod;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 		q = &rd->rd_data.q;
2680Sstevel@tonic-gate 		xprt->xp_wq = q;
2690Sstevel@tonic-gate 		q->q_ptr = &rd->rd_xprt;
2700Sstevel@tonic-gate 		xprt->xp_netid = NULL;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		/*
2730Sstevel@tonic-gate 		 * Each of the plugins will have their own Service ID
2740Sstevel@tonic-gate 		 * to listener specific mapping, like port number for VI
2750Sstevel@tonic-gate 		 * and service name for IB.
2760Sstevel@tonic-gate 		 */
2770Sstevel@tonic-gate 		rd->rd_data.svcid = id;
2780Sstevel@tonic-gate 		error = svc_xprt_register(xprt, id);
2790Sstevel@tonic-gate 		if (error) {
2807387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__xprt__reg);
2810Sstevel@tonic-gate 			goto cleanup;
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 		SVC_START(xprt);
2850Sstevel@tonic-gate 		if (!rd->rd_data.active) {
2860Sstevel@tonic-gate 			svc_xprt_unregister(xprt);
2870Sstevel@tonic-gate 			error = rd->rd_data.err_code;
2880Sstevel@tonic-gate 			goto cleanup;
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 		/*
2920Sstevel@tonic-gate 		 * This is set only when there is atleast one or more
2930Sstevel@tonic-gate 		 * transports successfully created. We insert the pointer
2940Sstevel@tonic-gate 		 * to the created RDMA master xprt into a separately maintained
2950Sstevel@tonic-gate 		 * list. This way we can easily reference it later to cleanup,
2960Sstevel@tonic-gate 		 * when NFS kRPC service pool is going away/unregistered.
2970Sstevel@tonic-gate 		 */
2980Sstevel@tonic-gate 		started_xprts->rtg_count ++;
2990Sstevel@tonic-gate 		xprt_rec = kmem_alloc(sizeof (*xprt_rec), KM_SLEEP);
3000Sstevel@tonic-gate 		xprt_rec->rtr_xprt_ptr = xprt;
3010Sstevel@tonic-gate 		xprt_rec->rtr_next = started_xprts->rtg_listhead;
3020Sstevel@tonic-gate 		started_xprts->rtg_listhead = xprt_rec;
3030Sstevel@tonic-gate 		continue;
3040Sstevel@tonic-gate cleanup:
3050Sstevel@tonic-gate 		SVC_DESTROY(xprt);
3060Sstevel@tonic-gate 		if (error == RDMA_FAILED)
3070Sstevel@tonic-gate 			error = EPROTONOSUPPORT;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	rw_exit(&rdma_lock);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/*
3130Sstevel@tonic-gate 	 * Don't return any error even if a single plugin was started
3140Sstevel@tonic-gate 	 * successfully.
3150Sstevel@tonic-gate 	 */
3160Sstevel@tonic-gate 	if (started_xprts->rtg_count == 0)
3170Sstevel@tonic-gate 		return (error);
3180Sstevel@tonic-gate 	return (0);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate  * Cleanup routine for freeing up memory allocated by
3230Sstevel@tonic-gate  * svc_rdma_kcreate()
3240Sstevel@tonic-gate  */
3250Sstevel@tonic-gate void
svc_rdma_kdestroy(SVCMASTERXPRT * xprt)3260Sstevel@tonic-gate svc_rdma_kdestroy(SVCMASTERXPRT *xprt)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	struct rdma_data *rd = (struct rdma_data *)xprt->xp_p2;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_req_lock);
3320Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_thread_lock);
3330Sstevel@tonic-gate 	kmem_free(rd, sizeof (*rd));
3340Sstevel@tonic-gate 	kmem_free(xprt, sizeof (*xprt));
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static void
svc_rdma_kstart(SVCMASTERXPRT * xprt)3390Sstevel@tonic-gate svc_rdma_kstart(SVCMASTERXPRT *xprt)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	struct rdma_svc_data *svcdata;
3420Sstevel@tonic-gate 	rdma_mod_t *rmod;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	svcdata = &((struct rdma_data *)xprt->xp_p2)->rd_data;
3450Sstevel@tonic-gate 	rmod = ((struct rdma_data *)xprt->xp_p2)->r_mod;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*
3480Sstevel@tonic-gate 	 * Create a listener for  module at this port
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 
3518695SRajkumar.Sivaprakasam@Sun.COM 	if (rmod->rdma_count != 0)
3528695SRajkumar.Sivaprakasam@Sun.COM 		(*rmod->rdma_ops->rdma_svc_listen)(svcdata);
3538695SRajkumar.Sivaprakasam@Sun.COM 	else
3548695SRajkumar.Sivaprakasam@Sun.COM 		svcdata->err_code = RDMA_FAILED;
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate void
svc_rdma_kstop(SVCMASTERXPRT * xprt)3580Sstevel@tonic-gate svc_rdma_kstop(SVCMASTERXPRT *xprt)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	struct rdma_svc_data *svcdata;
3610Sstevel@tonic-gate 	rdma_mod_t *rmod;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	svcdata	= &((struct rdma_data *)xprt->xp_p2)->rd_data;
3640Sstevel@tonic-gate 	rmod = ((struct rdma_data *)xprt->xp_p2)->r_mod;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3678695SRajkumar.Sivaprakasam@Sun.COM 	 * Call the stop listener routine for each plugin. If rdma_count is
3688695SRajkumar.Sivaprakasam@Sun.COM 	 * already zero set active to zero.
3690Sstevel@tonic-gate 	 */
3708695SRajkumar.Sivaprakasam@Sun.COM 	if (rmod->rdma_count != 0)
3718695SRajkumar.Sivaprakasam@Sun.COM 		(*rmod->rdma_ops->rdma_svc_stop)(svcdata);
3728695SRajkumar.Sivaprakasam@Sun.COM 	else
3738695SRajkumar.Sivaprakasam@Sun.COM 		svcdata->active = 0;
3740Sstevel@tonic-gate 	if (svcdata->active)
3757387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__kstop);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /* ARGSUSED */
3790Sstevel@tonic-gate static void
svc_rdma_kclone_destroy(SVCXPRT * clone_xprt)3800Sstevel@tonic-gate svc_rdma_kclone_destroy(SVCXPRT *clone_xprt)
3810Sstevel@tonic-gate {
38211967SKaren.Rochford@Sun.COM 
38311967SKaren.Rochford@Sun.COM 	struct clone_rdma_data *cdrp;
38411967SKaren.Rochford@Sun.COM 	cdrp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
38511967SKaren.Rochford@Sun.COM 
38611967SKaren.Rochford@Sun.COM 	/*
38711967SKaren.Rochford@Sun.COM 	 * Only free buffers and release connection when cloned is set.
38811967SKaren.Rochford@Sun.COM 	 */
38911967SKaren.Rochford@Sun.COM 	if (cdrp->cloned != TRUE)
39011967SKaren.Rochford@Sun.COM 		return;
39111967SKaren.Rochford@Sun.COM 
39211967SKaren.Rochford@Sun.COM 	rdma_buf_free(cdrp->conn, &cdrp->rpcbuf);
39311967SKaren.Rochford@Sun.COM 	if (cdrp->cl_reply) {
39411967SKaren.Rochford@Sun.COM 		clist_free(cdrp->cl_reply);
39511967SKaren.Rochford@Sun.COM 		cdrp->cl_reply = NULL;
39611967SKaren.Rochford@Sun.COM 	}
39711967SKaren.Rochford@Sun.COM 	RDMA_REL_CONN(cdrp->conn);
39811967SKaren.Rochford@Sun.COM 
39911967SKaren.Rochford@Sun.COM 	cdrp->cloned = 0;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
40211967SKaren.Rochford@Sun.COM /*
40311967SKaren.Rochford@Sun.COM  * Clone the xprt specific information.  It will be freed by
40411967SKaren.Rochford@Sun.COM  * SVC_CLONE_DESTROY.
40511967SKaren.Rochford@Sun.COM  */
40611967SKaren.Rochford@Sun.COM static void
svc_rdma_kclone_xprt(SVCXPRT * src_xprt,SVCXPRT * dst_xprt)40711967SKaren.Rochford@Sun.COM svc_rdma_kclone_xprt(SVCXPRT *src_xprt, SVCXPRT *dst_xprt)
40811967SKaren.Rochford@Sun.COM {
40911967SKaren.Rochford@Sun.COM 	struct clone_rdma_data *srcp2;
41011967SKaren.Rochford@Sun.COM 	struct clone_rdma_data *dstp2;
41111967SKaren.Rochford@Sun.COM 
41211967SKaren.Rochford@Sun.COM 	srcp2 = (struct clone_rdma_data *)src_xprt->xp_p2buf;
41311967SKaren.Rochford@Sun.COM 	dstp2 = (struct clone_rdma_data *)dst_xprt->xp_p2buf;
41411967SKaren.Rochford@Sun.COM 
41511967SKaren.Rochford@Sun.COM 	if (srcp2->conn != NULL) {
41611967SKaren.Rochford@Sun.COM 		srcp2->cloned = TRUE;
41711967SKaren.Rochford@Sun.COM 		*dstp2 = *srcp2;
41811967SKaren.Rochford@Sun.COM 	}
41911967SKaren.Rochford@Sun.COM }
42011967SKaren.Rochford@Sun.COM 
421*12553SKaren.Rochford@Sun.COM static void
svc_rdma_ktattrs(SVCXPRT * clone_xprt,int attrflag,void ** tattr)422*12553SKaren.Rochford@Sun.COM svc_rdma_ktattrs(SVCXPRT *clone_xprt, int attrflag, void **tattr)
423*12553SKaren.Rochford@Sun.COM {
424*12553SKaren.Rochford@Sun.COM 	CONN	*conn;
425*12553SKaren.Rochford@Sun.COM 	*tattr = NULL;
426*12553SKaren.Rochford@Sun.COM 
427*12553SKaren.Rochford@Sun.COM 	switch (attrflag) {
428*12553SKaren.Rochford@Sun.COM 	case SVC_TATTR_ADDRMASK:
429*12553SKaren.Rochford@Sun.COM 		conn = ((struct clone_rdma_data *)clone_xprt->xp_p2buf)->conn;
430*12553SKaren.Rochford@Sun.COM 		ASSERT(conn != NULL);
431*12553SKaren.Rochford@Sun.COM 		if (conn)
432*12553SKaren.Rochford@Sun.COM 			*tattr = (void *)&conn->c_addrmask;
433*12553SKaren.Rochford@Sun.COM 	}
434*12553SKaren.Rochford@Sun.COM }
43511967SKaren.Rochford@Sun.COM 
4360Sstevel@tonic-gate static bool_t
svc_rdma_krecv(SVCXPRT * clone_xprt,mblk_t * mp,struct rpc_msg * msg)4370Sstevel@tonic-gate svc_rdma_krecv(SVCXPRT *clone_xprt, mblk_t *mp, struct rpc_msg *msg)
4380Sstevel@tonic-gate {
4397387SRobert.Gordon@Sun.COM 	XDR	*xdrs;
4407387SRobert.Gordon@Sun.COM 	CONN	*conn;
4417387SRobert.Gordon@Sun.COM 	rdma_recv_data_t	*rdp = (rdma_recv_data_t *)mp->b_rptr;
4427387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
4437387SRobert.Gordon@Sun.COM 	struct clist	*cl = NULL;
4447387SRobert.Gordon@Sun.COM 	struct clist	*wcl = NULL;
4457387SRobert.Gordon@Sun.COM 	struct clist	*cllong = NULL;
4467387SRobert.Gordon@Sun.COM 
4477387SRobert.Gordon@Sun.COM 	rdma_stat	status;
4487387SRobert.Gordon@Sun.COM 	uint32_t vers, op, pos, xid;
4497387SRobert.Gordon@Sun.COM 	uint32_t rdma_credit;
4507387SRobert.Gordon@Sun.COM 	uint32_t wcl_total_length = 0;
4517387SRobert.Gordon@Sun.COM 	bool_t	wwl = FALSE;
4527387SRobert.Gordon@Sun.COM 
4537387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
4540Sstevel@tonic-gate 	RSSTAT_INCR(rscalls);
4550Sstevel@tonic-gate 	conn = rdp->conn;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	status = rdma_svc_postrecv(conn);
4580Sstevel@tonic-gate 	if (status != RDMA_SUCCESS) {
4597387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__postrecv);
4607387SRobert.Gordon@Sun.COM 		goto badrpc_call;
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	xdrs = &clone_xprt->xp_xdrin;
4640Sstevel@tonic-gate 	xdrmem_create(xdrs, rdp->rpcmsg.addr, rdp->rpcmsg.len, XDR_DECODE);
4657387SRobert.Gordon@Sun.COM 	xid = *(uint32_t *)rdp->rpcmsg.addr;
4667387SRobert.Gordon@Sun.COM 	XDR_SETPOS(xdrs, sizeof (uint32_t));
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (! xdr_u_int(xdrs, &vers) ||
4697387SRobert.Gordon@Sun.COM 	    ! xdr_u_int(xdrs, &rdma_credit) ||
4700Sstevel@tonic-gate 	    ! xdr_u_int(xdrs, &op)) {
4717387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__uint);
4727387SRobert.Gordon@Sun.COM 		goto xdr_err;
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4757387SRobert.Gordon@Sun.COM 	/* Checking if the status of the recv operation was normal */
4767387SRobert.Gordon@Sun.COM 	if (rdp->status != 0) {
4777387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__e__svcrdma__krecv__invalid__status,
4787387SRobert.Gordon@Sun.COM 		    int, rdp->status);
4797387SRobert.Gordon@Sun.COM 		goto badrpc_call;
4807387SRobert.Gordon@Sun.COM 	}
4817387SRobert.Gordon@Sun.COM 
4820Sstevel@tonic-gate 	if (! xdr_do_clist(xdrs, &cl)) {
4837387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__do__clist);
4847387SRobert.Gordon@Sun.COM 		goto xdr_err;
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4877387SRobert.Gordon@Sun.COM 	if (!xdr_decode_wlist_svc(xdrs, &wcl, &wwl, &wcl_total_length, conn)) {
4887387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__decode__wlist);
4897387SRobert.Gordon@Sun.COM 		if (cl)
4907387SRobert.Gordon@Sun.COM 			clist_free(cl);
4917387SRobert.Gordon@Sun.COM 		goto xdr_err;
4927387SRobert.Gordon@Sun.COM 	}
4937387SRobert.Gordon@Sun.COM 	crdp->cl_wlist = wcl;
4947387SRobert.Gordon@Sun.COM 
4957387SRobert.Gordon@Sun.COM 	crdp->cl_reply = NULL;
4967387SRobert.Gordon@Sun.COM 	(void) xdr_decode_reply_wchunk(xdrs, &crdp->cl_reply);
4977387SRobert.Gordon@Sun.COM 
4980Sstevel@tonic-gate 	/*
4990Sstevel@tonic-gate 	 * A chunk at 0 offset indicates that the RPC call message
5000Sstevel@tonic-gate 	 * is in a chunk. Get the RPC call message chunk.
5010Sstevel@tonic-gate 	 */
5020Sstevel@tonic-gate 	if (cl != NULL && op == RDMA_NOMSG) {
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 		/* Remove RPC call message chunk from chunklist */
5050Sstevel@tonic-gate 		cllong = cl;
5060Sstevel@tonic-gate 		cl = cl->c_next;
5070Sstevel@tonic-gate 		cllong->c_next = NULL;
5080Sstevel@tonic-gate 
5097387SRobert.Gordon@Sun.COM 
5100Sstevel@tonic-gate 		/* Allocate and register memory for the RPC call msg chunk */
5117387SRobert.Gordon@Sun.COM 		cllong->rb_longbuf.type = RDMA_LONG_BUFFER;
5127387SRobert.Gordon@Sun.COM 		cllong->rb_longbuf.len = cllong->c_len > LONG_REPLY_LEN ?
5137387SRobert.Gordon@Sun.COM 		    cllong->c_len : LONG_REPLY_LEN;
5147387SRobert.Gordon@Sun.COM 
5157387SRobert.Gordon@Sun.COM 		if (rdma_buf_alloc(conn, &cllong->rb_longbuf)) {
5160Sstevel@tonic-gate 			clist_free(cllong);
5177387SRobert.Gordon@Sun.COM 			goto cll_malloc_err;
5180Sstevel@tonic-gate 		}
5197387SRobert.Gordon@Sun.COM 
5207387SRobert.Gordon@Sun.COM 		cllong->u.c_daddr3 = cllong->rb_longbuf.addr;
5217387SRobert.Gordon@Sun.COM 
5227387SRobert.Gordon@Sun.COM 		if (cllong->u.c_daddr == NULL) {
5237387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__krecv__nomem);
5247387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, &cllong->rb_longbuf);
5257387SRobert.Gordon@Sun.COM 			clist_free(cllong);
5267387SRobert.Gordon@Sun.COM 			goto cll_malloc_err;
5277387SRobert.Gordon@Sun.COM 		}
5287387SRobert.Gordon@Sun.COM 
5297387SRobert.Gordon@Sun.COM 		status = clist_register(conn, cllong, CLIST_REG_DST);
5300Sstevel@tonic-gate 		if (status) {
5317387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__krecv__clist__reg);
5327387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, &cllong->rb_longbuf);
5330Sstevel@tonic-gate 			clist_free(cllong);
5347387SRobert.Gordon@Sun.COM 			goto cll_malloc_err;
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		/*
5380Sstevel@tonic-gate 		 * Now read the RPC call message in
5390Sstevel@tonic-gate 		 */
5400Sstevel@tonic-gate 		status = RDMA_READ(conn, cllong, WAIT);
5410Sstevel@tonic-gate 		if (status) {
5427387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__krecv__read);
5439348SSiddheshwar.Mahesh@Sun.COM 			(void) clist_deregister(conn, cllong);
5447387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, &cllong->rb_longbuf);
5450Sstevel@tonic-gate 			clist_free(cllong);
5467387SRobert.Gordon@Sun.COM 			goto cll_malloc_err;
5470Sstevel@tonic-gate 		}
5480Sstevel@tonic-gate 
5497387SRobert.Gordon@Sun.COM 		status = clist_syncmem(conn, cllong, CLIST_REG_DST);
5509348SSiddheshwar.Mahesh@Sun.COM 		(void) clist_deregister(conn, cllong);
5510Sstevel@tonic-gate 
5527387SRobert.Gordon@Sun.COM 		xdrrdma_create(xdrs, (caddr_t)(uintptr_t)cllong->u.c_daddr3,
5530Sstevel@tonic-gate 		    cllong->c_len, 0, cl, XDR_DECODE, conn);
5540Sstevel@tonic-gate 
5557387SRobert.Gordon@Sun.COM 		crdp->rpcbuf = cllong->rb_longbuf;
5567387SRobert.Gordon@Sun.COM 		crdp->rpcbuf.len = cllong->c_len;
5570Sstevel@tonic-gate 		clist_free(cllong);
5580Sstevel@tonic-gate 		RDMA_BUF_FREE(conn, &rdp->rpcmsg);
5590Sstevel@tonic-gate 	} else {
5600Sstevel@tonic-gate 		pos = XDR_GETPOS(xdrs);
5617387SRobert.Gordon@Sun.COM 		xdrrdma_create(xdrs, rdp->rpcmsg.addr + pos,
5627387SRobert.Gordon@Sun.COM 		    rdp->rpcmsg.len - pos, 0, cl, XDR_DECODE, conn);
5637387SRobert.Gordon@Sun.COM 		crdp->rpcbuf = rdp->rpcmsg;
5640Sstevel@tonic-gate 
5657387SRobert.Gordon@Sun.COM 		/* Use xdrrdmablk_ops to indicate there is a read chunk list */
5667387SRobert.Gordon@Sun.COM 		if (cl != NULL) {
5677387SRobert.Gordon@Sun.COM 			int32_t flg = XDR_RDMA_RLIST_REG;
5687387SRobert.Gordon@Sun.COM 
5697387SRobert.Gordon@Sun.COM 			XDR_CONTROL(xdrs, XDR_RDMA_SET_FLAGS, &flg);
5707387SRobert.Gordon@Sun.COM 			xdrs->x_ops = &xdrrdmablk_ops;
5717387SRobert.Gordon@Sun.COM 		}
5720Sstevel@tonic-gate 	}
5737387SRobert.Gordon@Sun.COM 
5747387SRobert.Gordon@Sun.COM 	if (crdp->cl_wlist) {
5757387SRobert.Gordon@Sun.COM 		int32_t flg = XDR_RDMA_WLIST_REG;
5767387SRobert.Gordon@Sun.COM 
5777387SRobert.Gordon@Sun.COM 		XDR_CONTROL(xdrs, XDR_RDMA_SET_WLIST, crdp->cl_wlist);
5787387SRobert.Gordon@Sun.COM 		XDR_CONTROL(xdrs, XDR_RDMA_SET_FLAGS, &flg);
5797387SRobert.Gordon@Sun.COM 	}
5807387SRobert.Gordon@Sun.COM 
5810Sstevel@tonic-gate 	if (! xdr_callmsg(xdrs, msg)) {
5827387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__krecv__callmsg);
5830Sstevel@tonic-gate 		RSSTAT_INCR(rsxdrcall);
5847387SRobert.Gordon@Sun.COM 		goto callmsg_err;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * Point the remote transport address in the service_transport
5890Sstevel@tonic-gate 	 * handle at the address in the request.
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	clone_xprt->xp_rtaddr.buf = conn->c_raddr.buf;
5920Sstevel@tonic-gate 	clone_xprt->xp_rtaddr.len = conn->c_raddr.len;
5930Sstevel@tonic-gate 	clone_xprt->xp_rtaddr.maxlen = conn->c_raddr.len;
59410326SSiddheshwar.Mahesh@Sun.COM 
59510326SSiddheshwar.Mahesh@Sun.COM 	clone_xprt->xp_lcladdr.buf = conn->c_laddr.buf;
59610326SSiddheshwar.Mahesh@Sun.COM 	clone_xprt->xp_lcladdr.len = conn->c_laddr.len;
59710326SSiddheshwar.Mahesh@Sun.COM 	clone_xprt->xp_lcladdr.maxlen = conn->c_laddr.len;
59810326SSiddheshwar.Mahesh@Sun.COM 
59910326SSiddheshwar.Mahesh@Sun.COM 	/*
60010326SSiddheshwar.Mahesh@Sun.COM 	 * In case of RDMA, connection management is
60110326SSiddheshwar.Mahesh@Sun.COM 	 * entirely done in rpcib module and netid in the
60210326SSiddheshwar.Mahesh@Sun.COM 	 * SVCMASTERXPRT is NULL. Initialize the clone netid
60310326SSiddheshwar.Mahesh@Sun.COM 	 * from the connection.
60410326SSiddheshwar.Mahesh@Sun.COM 	 */
60510326SSiddheshwar.Mahesh@Sun.COM 
60610326SSiddheshwar.Mahesh@Sun.COM 	clone_xprt->xp_netid = conn->c_netid;
60710326SSiddheshwar.Mahesh@Sun.COM 
6087387SRobert.Gordon@Sun.COM 	clone_xprt->xp_xid = xid;
6097387SRobert.Gordon@Sun.COM 	crdp->conn = conn;
6100Sstevel@tonic-gate 
6117387SRobert.Gordon@Sun.COM 	freeb(mp);
6127387SRobert.Gordon@Sun.COM 
6137387SRobert.Gordon@Sun.COM 	return (TRUE);
6147387SRobert.Gordon@Sun.COM 
6157387SRobert.Gordon@Sun.COM callmsg_err:
6167387SRobert.Gordon@Sun.COM 	rdma_buf_free(conn, &crdp->rpcbuf);
6177387SRobert.Gordon@Sun.COM 
6187387SRobert.Gordon@Sun.COM cll_malloc_err:
6197387SRobert.Gordon@Sun.COM 	if (cl)
6207387SRobert.Gordon@Sun.COM 		clist_free(cl);
6217387SRobert.Gordon@Sun.COM xdr_err:
6227387SRobert.Gordon@Sun.COM 	XDR_DESTROY(xdrs);
6237387SRobert.Gordon@Sun.COM 
6247387SRobert.Gordon@Sun.COM badrpc_call:
6257387SRobert.Gordon@Sun.COM 	RDMA_BUF_FREE(conn, &rdp->rpcmsg);
6267387SRobert.Gordon@Sun.COM 	RDMA_REL_CONN(conn);
6277387SRobert.Gordon@Sun.COM 	freeb(mp);
6287387SRobert.Gordon@Sun.COM 	RSSTAT_INCR(rsbadcalls);
6297387SRobert.Gordon@Sun.COM 	return (FALSE);
6307387SRobert.Gordon@Sun.COM }
6317387SRobert.Gordon@Sun.COM 
6327387SRobert.Gordon@Sun.COM static int
svc_process_long_reply(SVCXPRT * clone_xprt,xdrproc_t xdr_results,caddr_t xdr_location,struct rpc_msg * msg,bool_t has_args,int * msglen,int * freelen,int * numchunks,unsigned int * final_len)6337387SRobert.Gordon@Sun.COM svc_process_long_reply(SVCXPRT * clone_xprt,
6347387SRobert.Gordon@Sun.COM     xdrproc_t xdr_results, caddr_t xdr_location,
6357387SRobert.Gordon@Sun.COM     struct rpc_msg *msg, bool_t has_args, int *msglen,
6367387SRobert.Gordon@Sun.COM     int *freelen, int *numchunks, unsigned int *final_len)
6377387SRobert.Gordon@Sun.COM {
6387387SRobert.Gordon@Sun.COM 	int status;
6397387SRobert.Gordon@Sun.COM 	XDR xdrslong;
6407387SRobert.Gordon@Sun.COM 	struct clist *wcl = NULL;
6417387SRobert.Gordon@Sun.COM 	int count = 0;
6427387SRobert.Gordon@Sun.COM 	int alloc_len;
6437387SRobert.Gordon@Sun.COM 	char  *memp;
6447387SRobert.Gordon@Sun.COM 	rdma_buf_t long_rpc = {0};
6457387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
6467387SRobert.Gordon@Sun.COM 
6477387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
6487387SRobert.Gordon@Sun.COM 
6497387SRobert.Gordon@Sun.COM 	bzero(&xdrslong, sizeof (xdrslong));
6507387SRobert.Gordon@Sun.COM 
6517387SRobert.Gordon@Sun.COM 	/* Choose a size for the long rpc response */
6527387SRobert.Gordon@Sun.COM 	if (MSG_IS_RPCSEC_GSS(msg)) {
6537387SRobert.Gordon@Sun.COM 		alloc_len = RNDUP(MAX_AUTH_BYTES + *msglen);
6547387SRobert.Gordon@Sun.COM 	} else {
6557387SRobert.Gordon@Sun.COM 		alloc_len = RNDUP(*msglen);
6567387SRobert.Gordon@Sun.COM 	}
6577387SRobert.Gordon@Sun.COM 
6587387SRobert.Gordon@Sun.COM 	if (alloc_len <= 64 * 1024) {
6597387SRobert.Gordon@Sun.COM 		if (alloc_len > 32 * 1024) {
6607387SRobert.Gordon@Sun.COM 			alloc_len = 64 * 1024;
6617387SRobert.Gordon@Sun.COM 		} else {
6627387SRobert.Gordon@Sun.COM 			if (alloc_len > 16 * 1024) {
6637387SRobert.Gordon@Sun.COM 				alloc_len = 32 * 1024;
6647387SRobert.Gordon@Sun.COM 			} else {
6657387SRobert.Gordon@Sun.COM 				alloc_len = 16 * 1024;
6667387SRobert.Gordon@Sun.COM 			}
6677387SRobert.Gordon@Sun.COM 		}
6687387SRobert.Gordon@Sun.COM 	}
6697387SRobert.Gordon@Sun.COM 
6707387SRobert.Gordon@Sun.COM 	long_rpc.type = RDMA_LONG_BUFFER;
6717387SRobert.Gordon@Sun.COM 	long_rpc.len = alloc_len;
6727387SRobert.Gordon@Sun.COM 	if (rdma_buf_alloc(crdp->conn, &long_rpc)) {
6737387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6747387SRobert.Gordon@Sun.COM 	}
6757387SRobert.Gordon@Sun.COM 
6767387SRobert.Gordon@Sun.COM 	memp = long_rpc.addr;
6777387SRobert.Gordon@Sun.COM 	xdrmem_create(&xdrslong, memp, alloc_len, XDR_ENCODE);
6787387SRobert.Gordon@Sun.COM 
6797387SRobert.Gordon@Sun.COM 	msg->rm_xid = clone_xprt->xp_xid;
6807387SRobert.Gordon@Sun.COM 
6817387SRobert.Gordon@Sun.COM 	if (!(xdr_replymsg(&xdrslong, msg) &&
6827387SRobert.Gordon@Sun.COM 	    (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, &xdrslong,
6837387SRobert.Gordon@Sun.COM 	    xdr_results, xdr_location)))) {
6847387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
6857387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__authwrap);
6867387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
6877387SRobert.Gordon@Sun.COM 	}
6887387SRobert.Gordon@Sun.COM 
6897387SRobert.Gordon@Sun.COM 	*final_len = XDR_GETPOS(&xdrslong);
6907387SRobert.Gordon@Sun.COM 
6919348SSiddheshwar.Mahesh@Sun.COM 	DTRACE_PROBE1(krpc__i__replylen, uint_t, *final_len);
6927387SRobert.Gordon@Sun.COM 	*numchunks = 0;
6937387SRobert.Gordon@Sun.COM 	*freelen = 0;
6947387SRobert.Gordon@Sun.COM 
6957387SRobert.Gordon@Sun.COM 	wcl = crdp->cl_reply;
6967387SRobert.Gordon@Sun.COM 	wcl->rb_longbuf = long_rpc;
6977387SRobert.Gordon@Sun.COM 
6987387SRobert.Gordon@Sun.COM 	count = *final_len;
6999348SSiddheshwar.Mahesh@Sun.COM 	while ((wcl != NULL) && (count > 0)) {
7009348SSiddheshwar.Mahesh@Sun.COM 
7017387SRobert.Gordon@Sun.COM 		if (wcl->c_dmemhandle.mrc_rmr == 0)
7027387SRobert.Gordon@Sun.COM 			break;
7030Sstevel@tonic-gate 
7049348SSiddheshwar.Mahesh@Sun.COM 		DTRACE_PROBE2(krpc__i__write__chunks, uint32_t, count,
7059348SSiddheshwar.Mahesh@Sun.COM 		    uint32_t, wcl->c_len);
7069348SSiddheshwar.Mahesh@Sun.COM 
7077387SRobert.Gordon@Sun.COM 		if (wcl->c_len > count) {
7087387SRobert.Gordon@Sun.COM 			wcl->c_len = count;
7097387SRobert.Gordon@Sun.COM 		}
7107387SRobert.Gordon@Sun.COM 		wcl->w.c_saddr3 = (caddr_t)memp;
7117387SRobert.Gordon@Sun.COM 
7127387SRobert.Gordon@Sun.COM 		count -= wcl->c_len;
7137387SRobert.Gordon@Sun.COM 		*numchunks +=  1;
7149348SSiddheshwar.Mahesh@Sun.COM 		memp += wcl->c_len;
7159348SSiddheshwar.Mahesh@Sun.COM 		wcl = wcl->c_next;
7169348SSiddheshwar.Mahesh@Sun.COM 	}
7179348SSiddheshwar.Mahesh@Sun.COM 
7189348SSiddheshwar.Mahesh@Sun.COM 	/*
7199348SSiddheshwar.Mahesh@Sun.COM 	 * Make rest of the chunks 0-len
7209348SSiddheshwar.Mahesh@Sun.COM 	 */
7219348SSiddheshwar.Mahesh@Sun.COM 	while (wcl != NULL) {
7229348SSiddheshwar.Mahesh@Sun.COM 		if (wcl->c_dmemhandle.mrc_rmr == 0)
7237387SRobert.Gordon@Sun.COM 			break;
7249348SSiddheshwar.Mahesh@Sun.COM 		wcl->c_len = 0;
7257387SRobert.Gordon@Sun.COM 		wcl = wcl->c_next;
7267387SRobert.Gordon@Sun.COM 	}
7277387SRobert.Gordon@Sun.COM 
7287387SRobert.Gordon@Sun.COM 	wcl = crdp->cl_reply;
7297387SRobert.Gordon@Sun.COM 
7307387SRobert.Gordon@Sun.COM 	/*
7317387SRobert.Gordon@Sun.COM 	 * MUST fail if there are still more data
7327387SRobert.Gordon@Sun.COM 	 */
7337387SRobert.Gordon@Sun.COM 	if (count > 0) {
7347387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
7357387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__dlen__clist);
7367387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
7377387SRobert.Gordon@Sun.COM 	}
7387387SRobert.Gordon@Sun.COM 
7397387SRobert.Gordon@Sun.COM 	if (clist_register(crdp->conn, wcl, CLIST_REG_SOURCE) != RDMA_SUCCESS) {
7407387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
7417387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__clistreg);
7427387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
7437387SRobert.Gordon@Sun.COM 	}
7447387SRobert.Gordon@Sun.COM 
7457387SRobert.Gordon@Sun.COM 	status = clist_syncmem(crdp->conn, wcl, CLIST_REG_SOURCE);
7467387SRobert.Gordon@Sun.COM 
7477387SRobert.Gordon@Sun.COM 	if (status) {
7489348SSiddheshwar.Mahesh@Sun.COM 		(void) clist_deregister(crdp->conn, wcl);
7497387SRobert.Gordon@Sun.COM 		rdma_buf_free(crdp->conn, &long_rpc);
7507387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__syncmem);
7517387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
7520Sstevel@tonic-gate 	}
7537387SRobert.Gordon@Sun.COM 
7547387SRobert.Gordon@Sun.COM 	status = RDMA_WRITE(crdp->conn, wcl, WAIT);
7557387SRobert.Gordon@Sun.COM 
7569348SSiddheshwar.Mahesh@Sun.COM 	(void) clist_deregister(crdp->conn, wcl);
7577387SRobert.Gordon@Sun.COM 	rdma_buf_free(crdp->conn, &wcl->rb_longbuf);
7587387SRobert.Gordon@Sun.COM 
7597387SRobert.Gordon@Sun.COM 	if (status != RDMA_SUCCESS) {
7607387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__longrep__write);
7617387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
7627387SRobert.Gordon@Sun.COM 	}
7637387SRobert.Gordon@Sun.COM 
7647387SRobert.Gordon@Sun.COM 	return (SVC_RDMA_SUCCESS);
7657387SRobert.Gordon@Sun.COM }
7667387SRobert.Gordon@Sun.COM 
7670Sstevel@tonic-gate 
7687387SRobert.Gordon@Sun.COM static int
svc_compose_rpcmsg(SVCXPRT * clone_xprt,CONN * conn,xdrproc_t xdr_results,caddr_t xdr_location,rdma_buf_t * rpcreply,XDR ** xdrs,struct rpc_msg * msg,bool_t has_args,uint_t * len)7697387SRobert.Gordon@Sun.COM svc_compose_rpcmsg(SVCXPRT * clone_xprt, CONN * conn, xdrproc_t xdr_results,
7707387SRobert.Gordon@Sun.COM     caddr_t xdr_location, rdma_buf_t *rpcreply, XDR ** xdrs,
7717387SRobert.Gordon@Sun.COM     struct rpc_msg *msg, bool_t has_args, uint_t *len)
7727387SRobert.Gordon@Sun.COM {
7737387SRobert.Gordon@Sun.COM 	/*
7747387SRobert.Gordon@Sun.COM 	 * Get a pre-allocated buffer for rpc reply
7757387SRobert.Gordon@Sun.COM 	 */
7767387SRobert.Gordon@Sun.COM 	rpcreply->type = SEND_BUFFER;
7777387SRobert.Gordon@Sun.COM 	if (rdma_buf_alloc(conn, rpcreply)) {
7787387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__rpcmsg__reply__nofreebufs);
7797387SRobert.Gordon@Sun.COM 		return (SVC_RDMA_FAIL);
7807387SRobert.Gordon@Sun.COM 	}
7817387SRobert.Gordon@Sun.COM 
7827387SRobert.Gordon@Sun.COM 	xdrrdma_create(*xdrs, rpcreply->addr, rpcreply->len,
7837387SRobert.Gordon@Sun.COM 	    0, NULL, XDR_ENCODE, conn);
7847387SRobert.Gordon@Sun.COM 
7857387SRobert.Gordon@Sun.COM 	msg->rm_xid = clone_xprt->xp_xid;
7867387SRobert.Gordon@Sun.COM 
7877387SRobert.Gordon@Sun.COM 	if (has_args) {
7887387SRobert.Gordon@Sun.COM 		if (!(xdr_replymsg(*xdrs, msg) &&
7897387SRobert.Gordon@Sun.COM 		    (!has_args ||
7907387SRobert.Gordon@Sun.COM 		    SVCAUTH_WRAP(&clone_xprt->xp_auth, *xdrs,
7917387SRobert.Gordon@Sun.COM 		    xdr_results, xdr_location)))) {
7927387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, rpcreply);
7937387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(
7947387SRobert.Gordon@Sun.COM 			    krpc__e__svcrdma__rpcmsg__reply__authwrap1);
7957387SRobert.Gordon@Sun.COM 			return (SVC_RDMA_FAIL);
7967387SRobert.Gordon@Sun.COM 		}
7977387SRobert.Gordon@Sun.COM 	} else {
7987387SRobert.Gordon@Sun.COM 		if (!xdr_replymsg(*xdrs, msg)) {
7997387SRobert.Gordon@Sun.COM 			rdma_buf_free(conn, rpcreply);
8007387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(
8017387SRobert.Gordon@Sun.COM 			    krpc__e__svcrdma__rpcmsg__reply__authwrap2);
8027387SRobert.Gordon@Sun.COM 			return (SVC_RDMA_FAIL);
8037387SRobert.Gordon@Sun.COM 		}
8047387SRobert.Gordon@Sun.COM 	}
8057387SRobert.Gordon@Sun.COM 
8067387SRobert.Gordon@Sun.COM 	*len = XDR_GETPOS(*xdrs);
8077387SRobert.Gordon@Sun.COM 
8087387SRobert.Gordon@Sun.COM 	return (SVC_RDMA_SUCCESS);
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate /*
8120Sstevel@tonic-gate  * Send rpc reply.
8130Sstevel@tonic-gate  */
8140Sstevel@tonic-gate static bool_t
svc_rdma_ksend(SVCXPRT * clone_xprt,struct rpc_msg * msg)8157387SRobert.Gordon@Sun.COM svc_rdma_ksend(SVCXPRT * clone_xprt, struct rpc_msg *msg)
8160Sstevel@tonic-gate {
8177387SRobert.Gordon@Sun.COM 	XDR *xdrs_rpc = &(clone_xprt->xp_xdrout);
8187387SRobert.Gordon@Sun.COM 	XDR xdrs_rhdr;
8197387SRobert.Gordon@Sun.COM 	CONN *conn = NULL;
8207387SRobert.Gordon@Sun.COM 	rdma_buf_t rbuf_resp = {0}, rbuf_rpc_resp = {0};
8217387SRobert.Gordon@Sun.COM 
8227387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
8237387SRobert.Gordon@Sun.COM 	struct clist *cl_read = NULL;
8247387SRobert.Gordon@Sun.COM 	struct clist *cl_send = NULL;
8257387SRobert.Gordon@Sun.COM 	struct clist *cl_write = NULL;
8267387SRobert.Gordon@Sun.COM 	xdrproc_t xdr_results;		/* results XDR encoding function */
8277387SRobert.Gordon@Sun.COM 	caddr_t xdr_location;		/* response results pointer */
8287387SRobert.Gordon@Sun.COM 
8290Sstevel@tonic-gate 	int retval = FALSE;
8307387SRobert.Gordon@Sun.COM 	int status, msglen, num_wreply_segments = 0;
8317387SRobert.Gordon@Sun.COM 	uint32_t rdma_credit = 0;
8327387SRobert.Gordon@Sun.COM 	int freelen = 0;
8337387SRobert.Gordon@Sun.COM 	bool_t has_args;
8347387SRobert.Gordon@Sun.COM 	uint_t  final_resp_len, rdma_response_op, vers;
8350Sstevel@tonic-gate 
8367387SRobert.Gordon@Sun.COM 	bzero(&xdrs_rhdr, sizeof (XDR));
8377387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
8387387SRobert.Gordon@Sun.COM 	conn = crdp->conn;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	/*
8410Sstevel@tonic-gate 	 * If there is a result procedure specified in the reply message,
8420Sstevel@tonic-gate 	 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
8430Sstevel@tonic-gate 	 * We need to make sure it won't be processed twice, so we null
8440Sstevel@tonic-gate 	 * it for xdr_replymsg here.
8450Sstevel@tonic-gate 	 */
8460Sstevel@tonic-gate 	has_args = FALSE;
8470Sstevel@tonic-gate 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
8480Sstevel@tonic-gate 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
8490Sstevel@tonic-gate 		if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
8500Sstevel@tonic-gate 			has_args = TRUE;
8510Sstevel@tonic-gate 			xdr_location = msg->acpted_rply.ar_results.where;
8520Sstevel@tonic-gate 			msg->acpted_rply.ar_results.proc = xdr_void;
8530Sstevel@tonic-gate 			msg->acpted_rply.ar_results.where = NULL;
8540Sstevel@tonic-gate 		}
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/*
8587387SRobert.Gordon@Sun.COM 	 * Given the limit on the inline response size (RPC_MSG_SZ),
8597387SRobert.Gordon@Sun.COM 	 * there is a need to make a guess as to the overall size of
8607387SRobert.Gordon@Sun.COM 	 * the response.  If the resultant size is beyond the inline
8617387SRobert.Gordon@Sun.COM 	 * size, then the server needs to use the "reply chunk list"
8627387SRobert.Gordon@Sun.COM 	 * provided by the client (if the client provided one).  An
8637387SRobert.Gordon@Sun.COM 	 * example of this type of response would be a READDIR
8647387SRobert.Gordon@Sun.COM 	 * response (e.g. a small directory read would fit in RPC_MSG_SZ
8657387SRobert.Gordon@Sun.COM 	 * and that is the preference but it may not fit)
8667387SRobert.Gordon@Sun.COM 	 *
8677387SRobert.Gordon@Sun.COM 	 * Combine the encoded size and the size of the true results
8687387SRobert.Gordon@Sun.COM 	 * and then make the decision about where to encode and send results.
8697387SRobert.Gordon@Sun.COM 	 *
8707387SRobert.Gordon@Sun.COM 	 * One important note, this calculation is ignoring the size
8717387SRobert.Gordon@Sun.COM 	 * of the encoding of the authentication overhead.  The reason
8727387SRobert.Gordon@Sun.COM 	 * for this is rooted in the complexities of access to the
8737387SRobert.Gordon@Sun.COM 	 * encoded size of RPCSEC_GSS related authentiation,
8747387SRobert.Gordon@Sun.COM 	 * integrity, and privacy.
8757387SRobert.Gordon@Sun.COM 	 *
8767387SRobert.Gordon@Sun.COM 	 * If it turns out that the encoded authentication bumps the
8777387SRobert.Gordon@Sun.COM 	 * response over the RPC_MSG_SZ limit, then it may need to
8787387SRobert.Gordon@Sun.COM 	 * attempt to encode for the reply chunk list.
8797387SRobert.Gordon@Sun.COM 	 */
8807387SRobert.Gordon@Sun.COM 
8817387SRobert.Gordon@Sun.COM 	/*
8827387SRobert.Gordon@Sun.COM 	 * Calculating the "sizeof" the RPC response header and the
8837387SRobert.Gordon@Sun.COM 	 * encoded results.
8840Sstevel@tonic-gate 	 */
8850Sstevel@tonic-gate 	msglen = xdr_sizeof(xdr_replymsg, msg);
8867387SRobert.Gordon@Sun.COM 
8877387SRobert.Gordon@Sun.COM 	if (msglen > 0) {
8887387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotalreplies);
8897387SRobert.Gordon@Sun.COM 	}
8907387SRobert.Gordon@Sun.COM 	if (has_args)
8910Sstevel@tonic-gate 		msglen += xdrrdma_sizeof(xdr_results, xdr_location,
8927387SRobert.Gordon@Sun.COM 		    rdma_minchunk, NULL, NULL);
8937387SRobert.Gordon@Sun.COM 
8947387SRobert.Gordon@Sun.COM 	DTRACE_PROBE1(krpc__i__svcrdma__ksend__msglen, int, msglen);
8950Sstevel@tonic-gate 
8967387SRobert.Gordon@Sun.COM 	status = SVC_RDMA_SUCCESS;
8970Sstevel@tonic-gate 
8987387SRobert.Gordon@Sun.COM 	if (msglen < RPC_MSG_SZ) {
8990Sstevel@tonic-gate 		/*
9007387SRobert.Gordon@Sun.COM 		 * Looks like the response will fit in the inline
9017387SRobert.Gordon@Sun.COM 		 * response; let's try
9020Sstevel@tonic-gate 		 */
9037387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotalinlinereplies);
9047387SRobert.Gordon@Sun.COM 
9057387SRobert.Gordon@Sun.COM 		rdma_response_op = RDMA_MSG;
9060Sstevel@tonic-gate 
9077387SRobert.Gordon@Sun.COM 		status = svc_compose_rpcmsg(clone_xprt, conn, xdr_results,
9087387SRobert.Gordon@Sun.COM 		    xdr_location, &rbuf_rpc_resp, &xdrs_rpc, msg,
9097387SRobert.Gordon@Sun.COM 		    has_args, &final_resp_len);
9107387SRobert.Gordon@Sun.COM 
9117387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__srdma__ksend__compose_status,
9127387SRobert.Gordon@Sun.COM 		    int, status);
9137387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__srdma__ksend__compose_len,
9147387SRobert.Gordon@Sun.COM 		    int, final_resp_len);
9157387SRobert.Gordon@Sun.COM 
9167387SRobert.Gordon@Sun.COM 		if (status == SVC_RDMA_SUCCESS && crdp->cl_reply) {
9177387SRobert.Gordon@Sun.COM 			clist_free(crdp->cl_reply);
9187387SRobert.Gordon@Sun.COM 			crdp->cl_reply = NULL;
9190Sstevel@tonic-gate 		}
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 
9227387SRobert.Gordon@Sun.COM 	/*
9237387SRobert.Gordon@Sun.COM 	 * If the encode failed (size?) or the message really is
9247387SRobert.Gordon@Sun.COM 	 * larger than what is allowed, try the response chunk list.
9257387SRobert.Gordon@Sun.COM 	 */
9267387SRobert.Gordon@Sun.COM 	if (status != SVC_RDMA_SUCCESS || msglen >= RPC_MSG_SZ) {
9270Sstevel@tonic-gate 		/*
9287387SRobert.Gordon@Sun.COM 		 * attempting to use a reply chunk list when there
9297387SRobert.Gordon@Sun.COM 		 * isn't one won't get very far...
9300Sstevel@tonic-gate 		 */
9317387SRobert.Gordon@Sun.COM 		if (crdp->cl_reply == NULL) {
9327387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__ksend__noreplycl);
9330Sstevel@tonic-gate 			goto out;
9340Sstevel@tonic-gate 		}
9350Sstevel@tonic-gate 
9367387SRobert.Gordon@Sun.COM 		RSSTAT_INCR(rstotallongreplies);
9377387SRobert.Gordon@Sun.COM 
9387387SRobert.Gordon@Sun.COM 		msglen = xdr_sizeof(xdr_replymsg, msg);
9397387SRobert.Gordon@Sun.COM 		msglen += xdrrdma_sizeof(xdr_results, xdr_location, 0,
9407387SRobert.Gordon@Sun.COM 		    NULL, NULL);
9417387SRobert.Gordon@Sun.COM 
9427387SRobert.Gordon@Sun.COM 		status = svc_process_long_reply(clone_xprt, xdr_results,
9437387SRobert.Gordon@Sun.COM 		    xdr_location, msg, has_args, &msglen, &freelen,
9447387SRobert.Gordon@Sun.COM 		    &num_wreply_segments, &final_resp_len);
9457387SRobert.Gordon@Sun.COM 
9467387SRobert.Gordon@Sun.COM 		DTRACE_PROBE1(krpc__i__svcrdma__ksend__longreplen,
9477387SRobert.Gordon@Sun.COM 		    int, final_resp_len);
9487387SRobert.Gordon@Sun.COM 
9497387SRobert.Gordon@Sun.COM 		if (status != SVC_RDMA_SUCCESS) {
9507387SRobert.Gordon@Sun.COM 			DTRACE_PROBE(krpc__e__svcrdma__ksend__compose__failed);
9517387SRobert.Gordon@Sun.COM 			goto out;
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 
9547387SRobert.Gordon@Sun.COM 		rdma_response_op = RDMA_NOMSG;
9550Sstevel@tonic-gate 	}
9560Sstevel@tonic-gate 
9577387SRobert.Gordon@Sun.COM 	DTRACE_PROBE1(krpc__i__svcrdma__ksend__rdmamsg__len,
9587387SRobert.Gordon@Sun.COM 	    int, final_resp_len);
9590Sstevel@tonic-gate 
9607387SRobert.Gordon@Sun.COM 	rbuf_resp.type = SEND_BUFFER;
9617387SRobert.Gordon@Sun.COM 	if (rdma_buf_alloc(conn, &rbuf_resp)) {
9627387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9637387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__nofreebufs);
9647387SRobert.Gordon@Sun.COM 		goto out;
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9677387SRobert.Gordon@Sun.COM 	rdma_credit = rdma_bufs_granted;
9687387SRobert.Gordon@Sun.COM 
9697387SRobert.Gordon@Sun.COM 	vers = RPCRDMA_VERS;
9707387SRobert.Gordon@Sun.COM 	xdrmem_create(&xdrs_rhdr, rbuf_resp.addr, rbuf_resp.len, XDR_ENCODE);
9717387SRobert.Gordon@Sun.COM 	(*(uint32_t *)rbuf_resp.addr) = msg->rm_xid;
9727387SRobert.Gordon@Sun.COM 	/* Skip xid and set the xdr position accordingly. */
9737387SRobert.Gordon@Sun.COM 	XDR_SETPOS(&xdrs_rhdr, sizeof (uint32_t));
9747387SRobert.Gordon@Sun.COM 	if (!xdr_u_int(&xdrs_rhdr, &vers) ||
9757387SRobert.Gordon@Sun.COM 	    !xdr_u_int(&xdrs_rhdr, &rdma_credit) ||
9767387SRobert.Gordon@Sun.COM 	    !xdr_u_int(&xdrs_rhdr, &rdma_response_op)) {
9777387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9787387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
9797387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__uint);
9800Sstevel@tonic-gate 		goto out;
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/*
9847387SRobert.Gordon@Sun.COM 	 * Now XDR the read chunk list, actually always NULL
9850Sstevel@tonic-gate 	 */
9867387SRobert.Gordon@Sun.COM 	(void) xdr_encode_rlist_svc(&xdrs_rhdr, cl_read);
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	/*
9897387SRobert.Gordon@Sun.COM 	 * encode write list -- we already drove RDMA_WRITEs
9900Sstevel@tonic-gate 	 */
9917387SRobert.Gordon@Sun.COM 	cl_write = crdp->cl_wlist;
9927387SRobert.Gordon@Sun.COM 	if (!xdr_encode_wlist(&xdrs_rhdr, cl_write)) {
9937387SRobert.Gordon@Sun.COM 		DTRACE_PROBE(krpc__e__svcrdma__ksend__enc__wlist);
9947387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
9957387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
9960Sstevel@tonic-gate 		goto out;
9970Sstevel@tonic-gate 	}
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	/*
10007387SRobert.Gordon@Sun.COM 	 * XDR encode the RDMA_REPLY write chunk
10010Sstevel@tonic-gate 	 */
10027387SRobert.Gordon@Sun.COM 	if (!xdr_encode_reply_wchunk(&xdrs_rhdr, crdp->cl_reply,
10037387SRobert.Gordon@Sun.COM 	    num_wreply_segments)) {
10047387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_rpc_resp);
10057387SRobert.Gordon@Sun.COM 		rdma_buf_free(conn, &rbuf_resp);
10067387SRobert.Gordon@Sun.COM 		goto out;
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10097387SRobert.Gordon@Sun.COM 	clist_add(&cl_send, 0, XDR_GETPOS(&xdrs_rhdr), &rbuf_resp.handle,
10107387SRobert.Gordon@Sun.COM 	    rbuf_resp.addr, NULL, NULL);
10110Sstevel@tonic-gate 
10127387SRobert.Gordon@Sun.COM 	if (rdma_response_op == RDMA_MSG) {
10137387SRobert.Gordon@Sun.COM 		clist_add(&cl_send, 0, final_resp_len, &rbuf_rpc_resp.handle,
10147387SRobert.Gordon@Sun.COM 		    rbuf_rpc_resp.addr, NULL, NULL);
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 
10177387SRobert.Gordon@Sun.COM 	status = RDMA_SEND(conn, cl_send, msg->rm_xid);
10187387SRobert.Gordon@Sun.COM 
10197387SRobert.Gordon@Sun.COM 	if (status == RDMA_SUCCESS) {
10207387SRobert.Gordon@Sun.COM 		retval = TRUE;
10210Sstevel@tonic-gate 	}
10220Sstevel@tonic-gate 
10237387SRobert.Gordon@Sun.COM out:
10240Sstevel@tonic-gate 	/*
10250Sstevel@tonic-gate 	 * Free up sendlist chunks
10260Sstevel@tonic-gate 	 */
10277387SRobert.Gordon@Sun.COM 	if (cl_send != NULL)
10287387SRobert.Gordon@Sun.COM 		clist_free(cl_send);
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/*
10310Sstevel@tonic-gate 	 * Destroy private data for xdr rdma
10320Sstevel@tonic-gate 	 */
10337387SRobert.Gordon@Sun.COM 	if (clone_xprt->xp_xdrout.x_ops != NULL) {
10347387SRobert.Gordon@Sun.COM 		XDR_DESTROY(&(clone_xprt->xp_xdrout));
10357387SRobert.Gordon@Sun.COM 	}
10367387SRobert.Gordon@Sun.COM 
10377387SRobert.Gordon@Sun.COM 	if (crdp->cl_reply) {
10387387SRobert.Gordon@Sun.COM 		clist_free(crdp->cl_reply);
10397387SRobert.Gordon@Sun.COM 		crdp->cl_reply = NULL;
10407387SRobert.Gordon@Sun.COM 	}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	/*
10430Sstevel@tonic-gate 	 * This is completely disgusting.  If public is set it is
10440Sstevel@tonic-gate 	 * a pointer to a structure whose first field is the address
10450Sstevel@tonic-gate 	 * of the function to free that structure and any related
10460Sstevel@tonic-gate 	 * stuff.  (see rrokfree in nfs_xdr.c).
10470Sstevel@tonic-gate 	 */
10487387SRobert.Gordon@Sun.COM 	if (xdrs_rpc->x_public) {
10490Sstevel@tonic-gate 		/* LINTED pointer alignment */
10507387SRobert.Gordon@Sun.COM 		(**((int (**)()) xdrs_rpc->x_public)) (xdrs_rpc->x_public);
10517387SRobert.Gordon@Sun.COM 	}
10527387SRobert.Gordon@Sun.COM 
10537387SRobert.Gordon@Sun.COM 	if (xdrs_rhdr.x_ops != NULL) {
10547387SRobert.Gordon@Sun.COM 		XDR_DESTROY(&xdrs_rhdr);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	return (retval);
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate /*
10610Sstevel@tonic-gate  * Deserialize arguments.
10620Sstevel@tonic-gate  */
10630Sstevel@tonic-gate static bool_t
svc_rdma_kgetargs(SVCXPRT * clone_xprt,xdrproc_t xdr_args,caddr_t args_ptr)10640Sstevel@tonic-gate svc_rdma_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args, caddr_t args_ptr)
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate 	if ((SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
10670Sstevel@tonic-gate 	    xdr_args, args_ptr)) != TRUE)
10680Sstevel@tonic-gate 		return (FALSE);
10690Sstevel@tonic-gate 	return (TRUE);
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate static bool_t
svc_rdma_kfreeargs(SVCXPRT * clone_xprt,xdrproc_t xdr_args,caddr_t args_ptr)10730Sstevel@tonic-gate svc_rdma_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
10740Sstevel@tonic-gate     caddr_t args_ptr)
10750Sstevel@tonic-gate {
10767387SRobert.Gordon@Sun.COM 	struct clone_rdma_data *crdp;
10770Sstevel@tonic-gate 	bool_t retval;
10780Sstevel@tonic-gate 
107911967SKaren.Rochford@Sun.COM 	/*
108011967SKaren.Rochford@Sun.COM 	 * If the cloned bit is true, then this transport specific
108111967SKaren.Rochford@Sun.COM 	 * rmda data has been duplicated into another cloned xprt. Do
108211967SKaren.Rochford@Sun.COM 	 * not free, or release the connection, it is still in use.  The
108311967SKaren.Rochford@Sun.COM 	 * buffers will be freed and the connection released later by
108411967SKaren.Rochford@Sun.COM 	 * SVC_CLONE_DESTROY().
108511967SKaren.Rochford@Sun.COM 	 */
10867387SRobert.Gordon@Sun.COM 	crdp = (struct clone_rdma_data *)clone_xprt->xp_p2buf;
108711967SKaren.Rochford@Sun.COM 	if (crdp->cloned == TRUE) {
108811967SKaren.Rochford@Sun.COM 		crdp->cloned = 0;
108911967SKaren.Rochford@Sun.COM 		return (TRUE);
109011967SKaren.Rochford@Sun.COM 	}
10917387SRobert.Gordon@Sun.COM 
10927387SRobert.Gordon@Sun.COM 	/*
10937387SRobert.Gordon@Sun.COM 	 * Free the args if needed then XDR_DESTROY
10947387SRobert.Gordon@Sun.COM 	 */
10950Sstevel@tonic-gate 	if (args_ptr) {
10960Sstevel@tonic-gate 		XDR	*xdrs = &clone_xprt->xp_xdrin;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 		xdrs->x_op = XDR_FREE;
10990Sstevel@tonic-gate 		retval = (*xdr_args)(xdrs, args_ptr);
11000Sstevel@tonic-gate 	}
11017387SRobert.Gordon@Sun.COM 
11020Sstevel@tonic-gate 	XDR_DESTROY(&(clone_xprt->xp_xdrin));
11037387SRobert.Gordon@Sun.COM 	rdma_buf_free(crdp->conn, &crdp->rpcbuf);
11047387SRobert.Gordon@Sun.COM 	if (crdp->cl_reply) {
11057387SRobert.Gordon@Sun.COM 		clist_free(crdp->cl_reply);
11067387SRobert.Gordon@Sun.COM 		crdp->cl_reply = NULL;
11077387SRobert.Gordon@Sun.COM 	}
11087387SRobert.Gordon@Sun.COM 	RDMA_REL_CONN(crdp->conn);
11097387SRobert.Gordon@Sun.COM 
11100Sstevel@tonic-gate 	return (retval);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate /* ARGSUSED */
11140Sstevel@tonic-gate static int32_t *
svc_rdma_kgetres(SVCXPRT * clone_xprt,int size)11150Sstevel@tonic-gate svc_rdma_kgetres(SVCXPRT *clone_xprt, int size)
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate 	return (NULL);
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate /* ARGSUSED */
11210Sstevel@tonic-gate static void
svc_rdma_kfreeres(SVCXPRT * clone_xprt)11220Sstevel@tonic-gate svc_rdma_kfreeres(SVCXPRT *clone_xprt)
11230Sstevel@tonic-gate {
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate /*
11270Sstevel@tonic-gate  * the dup cacheing routines below provide a cache of non-failure
11280Sstevel@tonic-gate  * transaction id's.  rpc service routines can use this to detect
11290Sstevel@tonic-gate  * retransmissions and re-send a non-failure response.
11300Sstevel@tonic-gate  */
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate /*
11330Sstevel@tonic-gate  * MAXDUPREQS is the number of cached items.  It should be adjusted
11340Sstevel@tonic-gate  * to the service load so that there is likely to be a response entry
11350Sstevel@tonic-gate  * when the first retransmission comes in.
11360Sstevel@tonic-gate  */
11370Sstevel@tonic-gate #define	MAXDUPREQS	1024
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate /*
11400Sstevel@tonic-gate  * This should be appropriately scaled to MAXDUPREQS.
11410Sstevel@tonic-gate  */
11420Sstevel@tonic-gate #define	DRHASHSZ	257
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate #if ((DRHASHSZ & (DRHASHSZ - 1)) == 0)
11450Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) & (DRHASHSZ - 1))
11460Sstevel@tonic-gate #else
11470Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) % DRHASHSZ)
11480Sstevel@tonic-gate #endif
11490Sstevel@tonic-gate #define	DRHASH(dr)	XIDHASH((dr)->dr_xid)
11500Sstevel@tonic-gate #define	REQTOXID(req)	((req)->rq_xprt->xp_xid)
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate static int	rdmandupreqs = 0;
11537803Sgt29601@anthrax int	rdmamaxdupreqs = MAXDUPREQS;
11540Sstevel@tonic-gate static kmutex_t rdmadupreq_lock;
11550Sstevel@tonic-gate static struct dupreq *rdmadrhashtbl[DRHASHSZ];
11560Sstevel@tonic-gate static int	rdmadrhashstat[DRHASHSZ];
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate static void unhash(struct dupreq *);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate /*
11610Sstevel@tonic-gate  * rdmadrmru points to the head of a circular linked list in lru order.
11620Sstevel@tonic-gate  * rdmadrmru->dr_next == drlru
11630Sstevel@tonic-gate  */
11640Sstevel@tonic-gate struct dupreq *rdmadrmru;
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate /*
11670Sstevel@tonic-gate  * svc_rdma_kdup searches the request cache and returns 0 if the
11680Sstevel@tonic-gate  * request is not found in the cache.  If it is found, then it
11690Sstevel@tonic-gate  * returns the state of the request (in progress or done) and
11700Sstevel@tonic-gate  * the status or attributes that were part of the original reply.
11710Sstevel@tonic-gate  */
11720Sstevel@tonic-gate static int
svc_rdma_kdup(struct svc_req * req,caddr_t res,int size,struct dupreq ** drpp,bool_t * dupcachedp)11730Sstevel@tonic-gate svc_rdma_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
11740Sstevel@tonic-gate 	bool_t *dupcachedp)
11750Sstevel@tonic-gate {
11760Sstevel@tonic-gate 	struct dupreq *dr;
11770Sstevel@tonic-gate 	uint32_t xid;
11780Sstevel@tonic-gate 	uint32_t drhash;
11790Sstevel@tonic-gate 	int status;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	xid = REQTOXID(req);
11820Sstevel@tonic-gate 	mutex_enter(&rdmadupreq_lock);
11830Sstevel@tonic-gate 	RSSTAT_INCR(rsdupchecks);
11840Sstevel@tonic-gate 	/*
11850Sstevel@tonic-gate 	 * Check to see whether an entry already exists in the cache.
11860Sstevel@tonic-gate 	 */
11870Sstevel@tonic-gate 	dr = rdmadrhashtbl[XIDHASH(xid)];
11880Sstevel@tonic-gate 	while (dr != NULL) {
11890Sstevel@tonic-gate 		if (dr->dr_xid == xid &&
11900Sstevel@tonic-gate 		    dr->dr_proc == req->rq_proc &&
11910Sstevel@tonic-gate 		    dr->dr_prog == req->rq_prog &&
11920Sstevel@tonic-gate 		    dr->dr_vers == req->rq_vers &&
11930Sstevel@tonic-gate 		    dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
11940Sstevel@tonic-gate 		    bcmp((caddr_t)dr->dr_addr.buf,
11950Sstevel@tonic-gate 		    (caddr_t)req->rq_xprt->xp_rtaddr.buf,
11960Sstevel@tonic-gate 		    dr->dr_addr.len) == 0) {
11970Sstevel@tonic-gate 			status = dr->dr_status;
11980Sstevel@tonic-gate 			if (status == DUP_DONE) {
11990Sstevel@tonic-gate 				bcopy(dr->dr_resp.buf, res, size);
12000Sstevel@tonic-gate 				if (dupcachedp != NULL)
12010Sstevel@tonic-gate 					*dupcachedp = (dr->dr_resfree != NULL);
12020Sstevel@tonic-gate 			} else {
12030Sstevel@tonic-gate 				dr->dr_status = DUP_INPROGRESS;
12040Sstevel@tonic-gate 				*drpp = dr;
12050Sstevel@tonic-gate 			}
12060Sstevel@tonic-gate 			RSSTAT_INCR(rsdupreqs);
12070Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
12080Sstevel@tonic-gate 			return (status);
12090Sstevel@tonic-gate 		}
12100Sstevel@tonic-gate 		dr = dr->dr_chain;
12110Sstevel@tonic-gate 	}
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	/*
12140Sstevel@tonic-gate 	 * There wasn't an entry, either allocate a new one or recycle
12150Sstevel@tonic-gate 	 * an old one.
12160Sstevel@tonic-gate 	 */
12170Sstevel@tonic-gate 	if (rdmandupreqs < rdmamaxdupreqs) {
12180Sstevel@tonic-gate 		dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
12190Sstevel@tonic-gate 		if (dr == NULL) {
12200Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
12210Sstevel@tonic-gate 			return (DUP_ERROR);
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 		dr->dr_resp.buf = NULL;
12240Sstevel@tonic-gate 		dr->dr_resp.maxlen = 0;
12250Sstevel@tonic-gate 		dr->dr_addr.buf = NULL;
12260Sstevel@tonic-gate 		dr->dr_addr.maxlen = 0;
12270Sstevel@tonic-gate 		if (rdmadrmru) {
12280Sstevel@tonic-gate 			dr->dr_next = rdmadrmru->dr_next;
12290Sstevel@tonic-gate 			rdmadrmru->dr_next = dr;
12300Sstevel@tonic-gate 		} else {
12310Sstevel@tonic-gate 			dr->dr_next = dr;
12320Sstevel@tonic-gate 		}
12330Sstevel@tonic-gate 		rdmandupreqs++;
12340Sstevel@tonic-gate 	} else {
12350Sstevel@tonic-gate 		dr = rdmadrmru->dr_next;
12360Sstevel@tonic-gate 		while (dr->dr_status == DUP_INPROGRESS) {
12370Sstevel@tonic-gate 			dr = dr->dr_next;
12380Sstevel@tonic-gate 			if (dr == rdmadrmru->dr_next) {
12390Sstevel@tonic-gate 				mutex_exit(&rdmadupreq_lock);
12400Sstevel@tonic-gate 				return (DUP_ERROR);
12410Sstevel@tonic-gate 			}
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 		unhash(dr);
12440Sstevel@tonic-gate 		if (dr->dr_resfree) {
12450Sstevel@tonic-gate 			(*dr->dr_resfree)(dr->dr_resp.buf);
12460Sstevel@tonic-gate 		}
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 	dr->dr_resfree = NULL;
12490Sstevel@tonic-gate 	rdmadrmru = dr;
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	dr->dr_xid = REQTOXID(req);
12520Sstevel@tonic-gate 	dr->dr_prog = req->rq_prog;
12530Sstevel@tonic-gate 	dr->dr_vers = req->rq_vers;
12540Sstevel@tonic-gate 	dr->dr_proc = req->rq_proc;
12550Sstevel@tonic-gate 	if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
12560Sstevel@tonic-gate 		if (dr->dr_addr.buf != NULL)
12570Sstevel@tonic-gate 			kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
12580Sstevel@tonic-gate 		dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
12590Sstevel@tonic-gate 		dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen, KM_NOSLEEP);
12600Sstevel@tonic-gate 		if (dr->dr_addr.buf == NULL) {
12610Sstevel@tonic-gate 			dr->dr_addr.maxlen = 0;
12620Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
12630Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
12640Sstevel@tonic-gate 			return (DUP_ERROR);
12650Sstevel@tonic-gate 		}
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 	dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
12680Sstevel@tonic-gate 	bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
12690Sstevel@tonic-gate 	if (dr->dr_resp.maxlen < size) {
12700Sstevel@tonic-gate 		if (dr->dr_resp.buf != NULL)
12710Sstevel@tonic-gate 			kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
12720Sstevel@tonic-gate 		dr->dr_resp.maxlen = (unsigned int)size;
12730Sstevel@tonic-gate 		dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
12740Sstevel@tonic-gate 		if (dr->dr_resp.buf == NULL) {
12750Sstevel@tonic-gate 			dr->dr_resp.maxlen = 0;
12760Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
12770Sstevel@tonic-gate 			mutex_exit(&rdmadupreq_lock);
12780Sstevel@tonic-gate 			return (DUP_ERROR);
12790Sstevel@tonic-gate 		}
12800Sstevel@tonic-gate 	}
12810Sstevel@tonic-gate 	dr->dr_status = DUP_INPROGRESS;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
12840Sstevel@tonic-gate 	dr->dr_chain = rdmadrhashtbl[drhash];
12850Sstevel@tonic-gate 	rdmadrhashtbl[drhash] = dr;
12860Sstevel@tonic-gate 	rdmadrhashstat[drhash]++;
12870Sstevel@tonic-gate 	mutex_exit(&rdmadupreq_lock);
12880Sstevel@tonic-gate 	*drpp = dr;
12890Sstevel@tonic-gate 	return (DUP_NEW);
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate /*
12930Sstevel@tonic-gate  * svc_rdma_kdupdone marks the request done (DUP_DONE or DUP_DROP)
12940Sstevel@tonic-gate  * and stores the response.
12950Sstevel@tonic-gate  */
12960Sstevel@tonic-gate static void
svc_rdma_kdupdone(struct dupreq * dr,caddr_t res,void (* dis_resfree)(),int size,int status)12970Sstevel@tonic-gate svc_rdma_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
12980Sstevel@tonic-gate 	int size, int status)
12990Sstevel@tonic-gate {
13000Sstevel@tonic-gate 	ASSERT(dr->dr_resfree == NULL);
13010Sstevel@tonic-gate 	if (status == DUP_DONE) {
13020Sstevel@tonic-gate 		bcopy(res, dr->dr_resp.buf, size);
13030Sstevel@tonic-gate 		dr->dr_resfree = dis_resfree;
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate 	dr->dr_status = status;
13060Sstevel@tonic-gate }
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate /*
13090Sstevel@tonic-gate  * This routine expects that the mutex, rdmadupreq_lock, is already held.
13100Sstevel@tonic-gate  */
13110Sstevel@tonic-gate static void
unhash(struct dupreq * dr)13120Sstevel@tonic-gate unhash(struct dupreq *dr)
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate 	struct dupreq *drt;
13150Sstevel@tonic-gate 	struct dupreq *drtprev = NULL;
13160Sstevel@tonic-gate 	uint32_t drhash;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&rdmadupreq_lock));
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
13210Sstevel@tonic-gate 	drt = rdmadrhashtbl[drhash];
13220Sstevel@tonic-gate 	while (drt != NULL) {
13230Sstevel@tonic-gate 		if (drt == dr) {
13240Sstevel@tonic-gate 			rdmadrhashstat[drhash]--;
13250Sstevel@tonic-gate 			if (drtprev == NULL) {
13260Sstevel@tonic-gate 				rdmadrhashtbl[drhash] = drt->dr_chain;
13270Sstevel@tonic-gate 			} else {
13280Sstevel@tonic-gate 				drtprev->dr_chain = drt->dr_chain;
13290Sstevel@tonic-gate 			}
13300Sstevel@tonic-gate 			return;
13310Sstevel@tonic-gate 		}
13320Sstevel@tonic-gate 		drtprev = drt;
13330Sstevel@tonic-gate 		drt = drt->dr_chain;
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate }
13367387SRobert.Gordon@Sun.COM 
13377387SRobert.Gordon@Sun.COM bool_t
rdma_get_wchunk(struct svc_req * req,iovec_t * iov,struct clist * wlist)13387387SRobert.Gordon@Sun.COM rdma_get_wchunk(struct svc_req *req, iovec_t *iov, struct clist *wlist)
13397387SRobert.Gordon@Sun.COM {
13407387SRobert.Gordon@Sun.COM 	struct clist	*clist;
13417387SRobert.Gordon@Sun.COM 	uint32_t	tlen;
13427387SRobert.Gordon@Sun.COM 
13437387SRobert.Gordon@Sun.COM 	if (req->rq_xprt->xp_type != T_RDMA) {
13447387SRobert.Gordon@Sun.COM 		return (FALSE);
13457387SRobert.Gordon@Sun.COM 	}
13467387SRobert.Gordon@Sun.COM 
13477387SRobert.Gordon@Sun.COM 	tlen = 0;
13487387SRobert.Gordon@Sun.COM 	clist = wlist;
13497387SRobert.Gordon@Sun.COM 	while (clist) {
13507387SRobert.Gordon@Sun.COM 		tlen += clist->c_len;
13517387SRobert.Gordon@Sun.COM 		clist = clist->c_next;
13527387SRobert.Gordon@Sun.COM 	}
13537387SRobert.Gordon@Sun.COM 
13547387SRobert.Gordon@Sun.COM 	/*
13557387SRobert.Gordon@Sun.COM 	 * set iov to addr+len of first segment of first wchunk of
13567387SRobert.Gordon@Sun.COM 	 * wlist sent by client.  krecv() already malloc'd a buffer
13577387SRobert.Gordon@Sun.COM 	 * large enough, but registration is deferred until we write
13587387SRobert.Gordon@Sun.COM 	 * the buffer back to (NFS) client using RDMA_WRITE.
13597387SRobert.Gordon@Sun.COM 	 */
13607387SRobert.Gordon@Sun.COM 	iov->iov_base = (caddr_t)(uintptr_t)wlist->w.c_saddr;
13617387SRobert.Gordon@Sun.COM 	iov->iov_len = tlen;
13627387SRobert.Gordon@Sun.COM 
13637387SRobert.Gordon@Sun.COM 	return (TRUE);
13647387SRobert.Gordon@Sun.COM }
13659348SSiddheshwar.Mahesh@Sun.COM 
13669348SSiddheshwar.Mahesh@Sun.COM /*
13679348SSiddheshwar.Mahesh@Sun.COM  * routine to setup the read chunk lists
13689348SSiddheshwar.Mahesh@Sun.COM  */
13699348SSiddheshwar.Mahesh@Sun.COM 
13709348SSiddheshwar.Mahesh@Sun.COM int
rdma_setup_read_chunks(struct clist * wcl,uint32_t count,int * wcl_len)13719348SSiddheshwar.Mahesh@Sun.COM rdma_setup_read_chunks(struct clist *wcl, uint32_t count, int *wcl_len)
13729348SSiddheshwar.Mahesh@Sun.COM {
13739348SSiddheshwar.Mahesh@Sun.COM 	int		data_len, avail_len;
13749348SSiddheshwar.Mahesh@Sun.COM 	uint_t		round_len;
13759348SSiddheshwar.Mahesh@Sun.COM 
13769348SSiddheshwar.Mahesh@Sun.COM 	data_len = avail_len = 0;
13779348SSiddheshwar.Mahesh@Sun.COM 
13789348SSiddheshwar.Mahesh@Sun.COM 	while (wcl != NULL && count > 0) {
13799348SSiddheshwar.Mahesh@Sun.COM 		if (wcl->c_dmemhandle.mrc_rmr == 0)
13809348SSiddheshwar.Mahesh@Sun.COM 			break;
13819348SSiddheshwar.Mahesh@Sun.COM 
13829348SSiddheshwar.Mahesh@Sun.COM 		if (wcl->c_len < count) {
13839348SSiddheshwar.Mahesh@Sun.COM 			data_len += wcl->c_len;
13849348SSiddheshwar.Mahesh@Sun.COM 			avail_len = 0;
13859348SSiddheshwar.Mahesh@Sun.COM 		} else {
13869348SSiddheshwar.Mahesh@Sun.COM 			data_len += count;
13879348SSiddheshwar.Mahesh@Sun.COM 			avail_len = wcl->c_len - count;
13889348SSiddheshwar.Mahesh@Sun.COM 			wcl->c_len = count;
13899348SSiddheshwar.Mahesh@Sun.COM 		}
13909348SSiddheshwar.Mahesh@Sun.COM 		count -= wcl->c_len;
13919348SSiddheshwar.Mahesh@Sun.COM 
13929348SSiddheshwar.Mahesh@Sun.COM 		if (count == 0)
13939348SSiddheshwar.Mahesh@Sun.COM 			break;
13949348SSiddheshwar.Mahesh@Sun.COM 
13959348SSiddheshwar.Mahesh@Sun.COM 		wcl = wcl->c_next;
13969348SSiddheshwar.Mahesh@Sun.COM 	}
13979348SSiddheshwar.Mahesh@Sun.COM 
13989348SSiddheshwar.Mahesh@Sun.COM 	/*
13999348SSiddheshwar.Mahesh@Sun.COM 	 * MUST fail if there are still more data
14009348SSiddheshwar.Mahesh@Sun.COM 	 */
14019348SSiddheshwar.Mahesh@Sun.COM 	if (count > 0) {
14029348SSiddheshwar.Mahesh@Sun.COM 		DTRACE_PROBE2(krpc__e__rdma_setup_read_chunks_clist_len,
14039348SSiddheshwar.Mahesh@Sun.COM 		    int, data_len, int, count);
14049348SSiddheshwar.Mahesh@Sun.COM 		return (FALSE);
14059348SSiddheshwar.Mahesh@Sun.COM 	}
14069348SSiddheshwar.Mahesh@Sun.COM 
14079348SSiddheshwar.Mahesh@Sun.COM 	/*
14089348SSiddheshwar.Mahesh@Sun.COM 	 * Round up the last chunk to 4-byte boundary
14099348SSiddheshwar.Mahesh@Sun.COM 	 */
14109348SSiddheshwar.Mahesh@Sun.COM 	*wcl_len = roundup(data_len, BYTES_PER_XDR_UNIT);
14119348SSiddheshwar.Mahesh@Sun.COM 	round_len = *wcl_len - data_len;
14129348SSiddheshwar.Mahesh@Sun.COM 
14139348SSiddheshwar.Mahesh@Sun.COM 	if (round_len) {
14149348SSiddheshwar.Mahesh@Sun.COM 
14159348SSiddheshwar.Mahesh@Sun.COM 		/*
14169348SSiddheshwar.Mahesh@Sun.COM 		 * If there is space in the current chunk,
14179348SSiddheshwar.Mahesh@Sun.COM 		 * add the roundup to the chunk.
14189348SSiddheshwar.Mahesh@Sun.COM 		 */
14199348SSiddheshwar.Mahesh@Sun.COM 		if (avail_len >= round_len) {
14209348SSiddheshwar.Mahesh@Sun.COM 			wcl->c_len += round_len;
14219348SSiddheshwar.Mahesh@Sun.COM 		} else  {
14229348SSiddheshwar.Mahesh@Sun.COM 			/*
14239348SSiddheshwar.Mahesh@Sun.COM 			 * try the next one.
14249348SSiddheshwar.Mahesh@Sun.COM 			 */
14259348SSiddheshwar.Mahesh@Sun.COM 			wcl = wcl->c_next;
14269348SSiddheshwar.Mahesh@Sun.COM 			if ((wcl == NULL) || (wcl->c_len < round_len)) {
14279348SSiddheshwar.Mahesh@Sun.COM 				DTRACE_PROBE1(
14289348SSiddheshwar.Mahesh@Sun.COM 				    krpc__e__rdma_setup_read_chunks_rndup,
14299348SSiddheshwar.Mahesh@Sun.COM 				    int, round_len);
14309348SSiddheshwar.Mahesh@Sun.COM 				return (FALSE);
14319348SSiddheshwar.Mahesh@Sun.COM 			}
14329348SSiddheshwar.Mahesh@Sun.COM 			wcl->c_len = round_len;
14339348SSiddheshwar.Mahesh@Sun.COM 		}
14349348SSiddheshwar.Mahesh@Sun.COM 	}
14359348SSiddheshwar.Mahesh@Sun.COM 
14369348SSiddheshwar.Mahesh@Sun.COM 	wcl = wcl->c_next;
14379348SSiddheshwar.Mahesh@Sun.COM 
14389348SSiddheshwar.Mahesh@Sun.COM 	/*
14399348SSiddheshwar.Mahesh@Sun.COM 	 * Make rest of the chunks 0-len
14409348SSiddheshwar.Mahesh@Sun.COM 	 */
14419348SSiddheshwar.Mahesh@Sun.COM 
14429348SSiddheshwar.Mahesh@Sun.COM 	clist_zero_len(wcl);
14439348SSiddheshwar.Mahesh@Sun.COM 
14449348SSiddheshwar.Mahesh@Sun.COM 	return (TRUE);
14459348SSiddheshwar.Mahesh@Sun.COM }
1446