xref: /titanic_53/usr/src/uts/common/rpc/rpcib.c (revision 7f379ad161f54e78771cca76492a1bf75c316284)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50a701b1eSRobert Gordon  * Common Development and Distribution License (the "License").
60a701b1eSRobert Gordon  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e11c3f44Smeem  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
260a701b1eSRobert Gordon /*
270a701b1eSRobert Gordon  * Copyright (c) 2007, The Ohio State University. All rights reserved.
280a701b1eSRobert Gordon  *
290a701b1eSRobert Gordon  * Portions of this source code is developed by the team members of
300a701b1eSRobert Gordon  * The Ohio State University's Network-Based Computing Laboratory (NBCL),
310a701b1eSRobert Gordon  * headed by Professor Dhabaleswar K. (DK) Panda.
320a701b1eSRobert Gordon  *
330a701b1eSRobert Gordon  * Acknowledgements to contributions from developors:
340a701b1eSRobert Gordon  *   Ranjit Noronha: noronha@cse.ohio-state.edu
350a701b1eSRobert Gordon  *   Lei Chai      : chail@cse.ohio-state.edu
360a701b1eSRobert Gordon  *   Weikuan Yu    : yuw@cse.ohio-state.edu
370a701b1eSRobert Gordon  *
380a701b1eSRobert Gordon  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * The rpcib plugin. Implements the interface for RDMATF's
427c478bd9Sstevel@tonic-gate  * interaction with IBTF.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/user.h>
487c478bd9Sstevel@tonic-gate #include <sys/systm.h>
497c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
507c478bd9Sstevel@tonic-gate #include <sys/proc.h>
517c478bd9Sstevel@tonic-gate #include <sys/socket.h>
527c478bd9Sstevel@tonic-gate #include <sys/file.h>
537c478bd9Sstevel@tonic-gate #include <sys/stream.h>
547c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
557c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
567c478bd9Sstevel@tonic-gate #include <sys/errno.h>
577c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
587c478bd9Sstevel@tonic-gate #include <sys/debug.h>
597c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
607c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
617c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
627c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
637c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
647c478bd9Sstevel@tonic-gate #include <sys/time.h>
657c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
667c478bd9Sstevel@tonic-gate #include <sys/callb.h>
677c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
687c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
690a701b1eSRobert Gordon #include <sys/sdt.h>
707c478bd9Sstevel@tonic-gate #include <sys/ib/ibtl/ibti.h>
717c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
727c478bd9Sstevel@tonic-gate #include <rpc/ib.h>
737c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
747c478bd9Sstevel@tonic-gate #include <sys/kstr.h>
757c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
767c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
777c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
787c478bd9Sstevel@tonic-gate #include <net/if.h>
79e11c3f44Smeem #include <net/if_types.h>
807c478bd9Sstevel@tonic-gate #include <sys/cred.h>
810a701b1eSRobert Gordon #include <rpc/rpc_rdma.h>
820a701b1eSRobert Gordon #include <nfs/nfs.h>
830a701b1eSRobert Gordon #include <sys/atomic.h>
840a701b1eSRobert Gordon 
85f837ee4aSSiddheshwar Mahesh #define	NFS_RDMA_PORT	20049
86f837ee4aSSiddheshwar Mahesh 
877c478bd9Sstevel@tonic-gate 
88e11c3f44Smeem /*
89214ae7d0SSiddheshwar Mahesh  * Convenience structures for connection management
90e11c3f44Smeem  */
91e11c3f44Smeem typedef struct rpcib_ipaddrs {
92e11c3f44Smeem 	void	*ri_list;	/* pointer to list of addresses */
93e11c3f44Smeem 	uint_t	ri_count;	/* number of addresses in list */
94e11c3f44Smeem 	uint_t	ri_size;	/* size of ri_list in bytes */
95e11c3f44Smeem } rpcib_ipaddrs_t;
967c478bd9Sstevel@tonic-gate 
97214ae7d0SSiddheshwar Mahesh 
98214ae7d0SSiddheshwar Mahesh typedef struct rpcib_ping {
99214ae7d0SSiddheshwar Mahesh 	rib_hca_t  *hca;
100214ae7d0SSiddheshwar Mahesh 	ibt_path_info_t path;
101214ae7d0SSiddheshwar Mahesh 	ibt_ip_addr_t srcip;
102214ae7d0SSiddheshwar Mahesh 	ibt_ip_addr_t dstip;
103214ae7d0SSiddheshwar Mahesh } rpcib_ping_t;
104214ae7d0SSiddheshwar Mahesh 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Prototype declarations for driver ops
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static int	rpcib_attach(dev_info_t *, ddi_attach_cmd_t);
1097c478bd9Sstevel@tonic-gate static int	rpcib_getinfo(dev_info_t *, ddi_info_cmd_t,
1107c478bd9Sstevel@tonic-gate 				void *, void **);
1117c478bd9Sstevel@tonic-gate static int	rpcib_detach(dev_info_t *, ddi_detach_cmd_t);
112e11c3f44Smeem static boolean_t rpcib_rdma_capable_interface(struct lifreq *);
113e11c3f44Smeem static int	rpcib_do_ip_ioctl(int, int, void *);
114e11c3f44Smeem static boolean_t rpcib_get_ib_addresses(rpcib_ipaddrs_t *, rpcib_ipaddrs_t *);
1150a701b1eSRobert Gordon static int rpcib_cache_kstat_update(kstat_t *, int);
1160a701b1eSRobert Gordon static void rib_force_cleanup(void *);
117*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static void rib_stop_hca_services(rib_hca_t *);
118*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static void rib_attach_hca(void);
119*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static int rib_find_hca_connection(rib_hca_t *hca, struct netbuf *s_svcaddr,
120*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		struct netbuf *d_svcaddr, CONN **conn);
1217c478bd9Sstevel@tonic-gate 
1220a701b1eSRobert Gordon struct {
1230a701b1eSRobert Gordon 	kstat_named_t cache_limit;
1240a701b1eSRobert Gordon 	kstat_named_t cache_allocation;
1250a701b1eSRobert Gordon 	kstat_named_t cache_hits;
1260a701b1eSRobert Gordon 	kstat_named_t cache_misses;
1270a701b1eSRobert Gordon 	kstat_named_t cache_misses_above_the_limit;
1280a701b1eSRobert Gordon } rpcib_kstat = {
1290a701b1eSRobert Gordon 	{"cache_limit",			KSTAT_DATA_UINT64 },
1300a701b1eSRobert Gordon 	{"cache_allocation",		KSTAT_DATA_UINT64 },
1310a701b1eSRobert Gordon 	{"cache_hits",			KSTAT_DATA_UINT64 },
1320a701b1eSRobert Gordon 	{"cache_misses",		KSTAT_DATA_UINT64 },
1330a701b1eSRobert Gordon 	{"cache_misses_above_the_limit", KSTAT_DATA_UINT64 },
1340a701b1eSRobert Gordon };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* rpcib cb_ops */
1377c478bd9Sstevel@tonic-gate static struct cb_ops rpcib_cbops = {
1387c478bd9Sstevel@tonic-gate 	nulldev,		/* open */
1397c478bd9Sstevel@tonic-gate 	nulldev,		/* close */
1407c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
1417c478bd9Sstevel@tonic-gate 	nodev,			/* print */
1427c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
1437c478bd9Sstevel@tonic-gate 	nodev,			/* read */
1447c478bd9Sstevel@tonic-gate 	nodev,			/* write */
1457c478bd9Sstevel@tonic-gate 	nodev,			/* ioctl */
1467c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
1477c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
1487c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
1497c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
1507c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
1517c478bd9Sstevel@tonic-gate 	NULL,			/* stream */
1527c478bd9Sstevel@tonic-gate 	D_MP,			/* cb_flag */
1537c478bd9Sstevel@tonic-gate 	CB_REV,			/* rev */
1547c478bd9Sstevel@tonic-gate 	nodev,			/* int (*cb_aread)() */
1557c478bd9Sstevel@tonic-gate 	nodev			/* int (*cb_awrite)() */
1567c478bd9Sstevel@tonic-gate };
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * Device options
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static struct dev_ops rpcib_ops = {
1627c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1637c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
1647c478bd9Sstevel@tonic-gate 	rpcib_getinfo,		/* info */
1657c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1667c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1677c478bd9Sstevel@tonic-gate 	rpcib_attach,		/* attach */
1687c478bd9Sstevel@tonic-gate 	rpcib_detach,		/* detach */
1697c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1707c478bd9Sstevel@tonic-gate 	&rpcib_cbops,		    /* driver ops - devctl interfaces */
1717c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
17219397407SSherry Moore 	NULL,			/* power */
17319397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Module linkage information.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static struct modldrv rib_modldrv = {
1817c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Driver module */
18219397407SSherry Moore 	"RPCIB plugin driver",	/* Driver name and version */
1837c478bd9Sstevel@tonic-gate 	&rpcib_ops,		/* Driver ops */
1847c478bd9Sstevel@tonic-gate };
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static struct modlinkage rib_modlinkage = {
1877c478bd9Sstevel@tonic-gate 	MODREV_1,
1887c478bd9Sstevel@tonic-gate 	(void *)&rib_modldrv,
1897c478bd9Sstevel@tonic-gate 	NULL
1907c478bd9Sstevel@tonic-gate };
1917c478bd9Sstevel@tonic-gate 
1920a701b1eSRobert Gordon typedef struct rib_lrc_entry {
1930a701b1eSRobert Gordon 	struct rib_lrc_entry *forw;
1940a701b1eSRobert Gordon 	struct rib_lrc_entry *back;
1950a701b1eSRobert Gordon 	char *lrc_buf;
1960a701b1eSRobert Gordon 
1970a701b1eSRobert Gordon 	uint32_t lrc_len;
1980a701b1eSRobert Gordon 	void  *avl_node;
1990a701b1eSRobert Gordon 	bool_t registered;
2000a701b1eSRobert Gordon 
2010a701b1eSRobert Gordon 	struct mrc lrc_mhandle;
2020a701b1eSRobert Gordon 	bool_t lrc_on_freed_list;
2030a701b1eSRobert Gordon } rib_lrc_entry_t;
2040a701b1eSRobert Gordon 
2050a701b1eSRobert Gordon typedef	struct cache_struct	{
2060a701b1eSRobert Gordon 	rib_lrc_entry_t		r;
2070a701b1eSRobert Gordon 	uint32_t		len;
2080a701b1eSRobert Gordon 	uint32_t		elements;
2090a701b1eSRobert Gordon 	kmutex_t		node_lock;
2100a701b1eSRobert Gordon 	avl_node_t		avl_link;
2110a701b1eSRobert Gordon } cache_avl_struct_t;
2120a701b1eSRobert Gordon 
2130a701b1eSRobert Gordon uint64_t	cache_limit = 100 * 1024 * 1024;
2140a701b1eSRobert Gordon static uint64_t	cache_watermark = 80 * 1024 * 1024;
2150a701b1eSRobert Gordon static bool_t	stats_enabled = FALSE;
2160a701b1eSRobert Gordon 
2170a701b1eSRobert Gordon static uint64_t max_unsignaled_rws = 5;
218f837ee4aSSiddheshwar Mahesh int nfs_rdma_port = NFS_RDMA_PORT;
2190a701b1eSRobert Gordon 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * rib_stat: private data pointer used when registering
2227c478bd9Sstevel@tonic-gate  *	with the IBTF.  It is returned to the consumer
2237c478bd9Sstevel@tonic-gate  *	in all callbacks.
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate static rpcib_state_t *rib_stat = NULL;
2267c478bd9Sstevel@tonic-gate 
2270a701b1eSRobert Gordon #define	RNR_RETRIES	IBT_RNR_RETRY_1
2287c478bd9Sstevel@tonic-gate #define	MAX_PORTS	2
229065714dcSSiddheshwar Mahesh #define	RDMA_DUMMY_WRID	0x4D3A1D4D3A1D
230065714dcSSiddheshwar Mahesh #define	RDMA_CONN_REAP_RETRY	10	/* 10 secs */
2317c478bd9Sstevel@tonic-gate 
2320a701b1eSRobert Gordon int preposted_rbufs = RDMA_BUFS_GRANT;
2337c478bd9Sstevel@tonic-gate int send_threshold = 1;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
236065714dcSSiddheshwar Mahesh  * Old cards with Tavor driver have limited memory footprint
237065714dcSSiddheshwar Mahesh  * when booted in 32bit. The rib_max_rbufs tunable can be
238065714dcSSiddheshwar Mahesh  * tuned for more buffers if needed.
239065714dcSSiddheshwar Mahesh  */
240065714dcSSiddheshwar Mahesh 
241065714dcSSiddheshwar Mahesh #if !defined(_ELF64) && !defined(__sparc)
242065714dcSSiddheshwar Mahesh int rib_max_rbufs = MAX_BUFS;
243065714dcSSiddheshwar Mahesh #else
244065714dcSSiddheshwar Mahesh int rib_max_rbufs = 10 * MAX_BUFS;
245065714dcSSiddheshwar Mahesh #endif	/* !(_ELF64) && !(__sparc) */
246065714dcSSiddheshwar Mahesh 
247065714dcSSiddheshwar Mahesh int rib_conn_timeout = 60 * 12;		/* 12 minutes */
248065714dcSSiddheshwar Mahesh 
249065714dcSSiddheshwar Mahesh /*
2507c478bd9Sstevel@tonic-gate  * State of the plugin.
2517c478bd9Sstevel@tonic-gate  * ACCEPT = accepting new connections and requests.
2527c478bd9Sstevel@tonic-gate  * NO_ACCEPT = not accepting new connection and requests.
2537c478bd9Sstevel@tonic-gate  * This should eventually move to rpcib_state_t structure, since this
2547c478bd9Sstevel@tonic-gate  * will tell in which state the plugin is for a particular type of service
2557c478bd9Sstevel@tonic-gate  * like NFS, NLM or v4 Callback deamon. The plugin might be in accept
2567c478bd9Sstevel@tonic-gate  * state for one and in no_accept state for the other.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate int		plugin_state;
2597c478bd9Sstevel@tonic-gate kmutex_t	plugin_state_lock;
2607c478bd9Sstevel@tonic-gate 
2610a701b1eSRobert Gordon ldi_ident_t rpcib_li;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * RPCIB RDMATF operations
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate static rdma_stat rib_reachable(int addr_type, struct netbuf *, void **handle);
2677c478bd9Sstevel@tonic-gate static rdma_stat rib_disconnect(CONN *conn);
2687c478bd9Sstevel@tonic-gate static void rib_listen(struct rdma_svc_data *rd);
2697c478bd9Sstevel@tonic-gate static void rib_listen_stop(struct rdma_svc_data *rd);
2700a701b1eSRobert Gordon static rdma_stat rib_registermem(CONN *conn, caddr_t  adsp, caddr_t buf,
2710a701b1eSRobert Gordon 	uint_t buflen, struct mrc *buf_handle);
2727c478bd9Sstevel@tonic-gate static rdma_stat rib_deregistermem(CONN *conn, caddr_t buf,
2737c478bd9Sstevel@tonic-gate 	struct mrc buf_handle);
2740a701b1eSRobert Gordon static rdma_stat rib_registermem_via_hca(rib_hca_t *hca, caddr_t adsp,
2750a701b1eSRobert Gordon 		caddr_t buf, uint_t buflen, struct mrc *buf_handle);
2760a701b1eSRobert Gordon static rdma_stat rib_deregistermem_via_hca(rib_hca_t *hca, caddr_t buf,
2770a701b1eSRobert Gordon 		struct mrc buf_handle);
2780a701b1eSRobert Gordon static rdma_stat rib_registermemsync(CONN *conn,  caddr_t adsp, caddr_t buf,
2790a701b1eSRobert Gordon 	uint_t buflen, struct mrc *buf_handle, RIB_SYNCMEM_HANDLE *sync_handle,
2800a701b1eSRobert Gordon 	void *lrc);
2817c478bd9Sstevel@tonic-gate static rdma_stat rib_deregistermemsync(CONN *conn, caddr_t buf,
2820a701b1eSRobert Gordon 	struct mrc buf_handle, RIB_SYNCMEM_HANDLE sync_handle, void *);
2837c478bd9Sstevel@tonic-gate static rdma_stat rib_syncmem(CONN *conn, RIB_SYNCMEM_HANDLE shandle,
2847c478bd9Sstevel@tonic-gate 	caddr_t buf, int len, int cpu);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate static rdma_stat rib_reg_buf_alloc(CONN *conn, rdma_buf_t *rdbuf);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static void rib_reg_buf_free(CONN *conn, rdma_buf_t *rdbuf);
2897c478bd9Sstevel@tonic-gate static void *rib_rbuf_alloc(CONN *, rdma_buf_t *);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate static void rib_rbuf_free(CONN *conn, int ptype, void *buf);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate static rdma_stat rib_send(CONN *conn, struct clist *cl, uint32_t msgid);
2947c478bd9Sstevel@tonic-gate static rdma_stat rib_send_resp(CONN *conn, struct clist *cl, uint32_t msgid);
2957c478bd9Sstevel@tonic-gate static rdma_stat rib_post_resp(CONN *conn, struct clist *cl, uint32_t msgid);
2960a701b1eSRobert Gordon static rdma_stat rib_post_resp_remove(CONN *conn, uint32_t msgid);
2977c478bd9Sstevel@tonic-gate static rdma_stat rib_post_recv(CONN *conn, struct clist *cl);
2987c478bd9Sstevel@tonic-gate static rdma_stat rib_recv(CONN *conn, struct clist **clp, uint32_t msgid);
2997c478bd9Sstevel@tonic-gate static rdma_stat rib_read(CONN *conn, struct clist *cl, int wait);
3007c478bd9Sstevel@tonic-gate static rdma_stat rib_write(CONN *conn, struct clist *cl, int wait);
301214ae7d0SSiddheshwar Mahesh static rdma_stat rib_ping_srv(int addr_type, struct netbuf *, rpcib_ping_t *);
302*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static rdma_stat rib_conn_get(struct netbuf *, struct netbuf *,
303*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	int addr_type, void *, CONN **);
3047c478bd9Sstevel@tonic-gate static rdma_stat rib_conn_release(CONN *conn);
3057c478bd9Sstevel@tonic-gate static rdma_stat rib_getinfo(rdma_info_t *info);
3060a701b1eSRobert Gordon 
3070a701b1eSRobert Gordon static rib_lrc_entry_t *rib_get_cache_buf(CONN *conn, uint32_t len);
3080a701b1eSRobert Gordon static void rib_free_cache_buf(CONN *conn, rib_lrc_entry_t *buf);
3090a701b1eSRobert Gordon static void rib_destroy_cache(rib_hca_t *hca);
3100a701b1eSRobert Gordon static	void	rib_server_side_cache_reclaim(void *argp);
3110a701b1eSRobert Gordon static int avl_compare(const void *t1, const void *t2);
3120a701b1eSRobert Gordon 
3137c478bd9Sstevel@tonic-gate static void rib_stop_services(rib_hca_t *);
3140a701b1eSRobert Gordon static void rib_close_channels(rib_conn_list_t *);
315065714dcSSiddheshwar Mahesh static void rib_conn_close(void *);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * RPCIB addressing operations
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * RDMA operations the RPCIB module exports
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate static rdmaops_t rib_ops = {
3257c478bd9Sstevel@tonic-gate 	rib_reachable,
3267c478bd9Sstevel@tonic-gate 	rib_conn_get,
3277c478bd9Sstevel@tonic-gate 	rib_conn_release,
3287c478bd9Sstevel@tonic-gate 	rib_listen,
3297c478bd9Sstevel@tonic-gate 	rib_listen_stop,
3307c478bd9Sstevel@tonic-gate 	rib_registermem,
3317c478bd9Sstevel@tonic-gate 	rib_deregistermem,
3327c478bd9Sstevel@tonic-gate 	rib_registermemsync,
3337c478bd9Sstevel@tonic-gate 	rib_deregistermemsync,
3347c478bd9Sstevel@tonic-gate 	rib_syncmem,
3357c478bd9Sstevel@tonic-gate 	rib_reg_buf_alloc,
3367c478bd9Sstevel@tonic-gate 	rib_reg_buf_free,
3377c478bd9Sstevel@tonic-gate 	rib_send,
3387c478bd9Sstevel@tonic-gate 	rib_send_resp,
3397c478bd9Sstevel@tonic-gate 	rib_post_resp,
3400a701b1eSRobert Gordon 	rib_post_resp_remove,
3417c478bd9Sstevel@tonic-gate 	rib_post_recv,
3427c478bd9Sstevel@tonic-gate 	rib_recv,
3437c478bd9Sstevel@tonic-gate 	rib_read,
3447c478bd9Sstevel@tonic-gate 	rib_write,
3450a701b1eSRobert Gordon 	rib_getinfo,
3467c478bd9Sstevel@tonic-gate };
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * RDMATF RPCIB plugin details
3507c478bd9Sstevel@tonic-gate  */
3517c478bd9Sstevel@tonic-gate static rdma_mod_t rib_mod = {
3527c478bd9Sstevel@tonic-gate 	"ibtf",		/* api name */
3537c478bd9Sstevel@tonic-gate 	RDMATF_VERS_1,
3547c478bd9Sstevel@tonic-gate 	0,
3557c478bd9Sstevel@tonic-gate 	&rib_ops,	/* rdma op vector for ibtf */
3567c478bd9Sstevel@tonic-gate };
3577c478bd9Sstevel@tonic-gate 
358*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static rdma_stat rpcib_open_hcas(rpcib_state_t *);
3597c478bd9Sstevel@tonic-gate static rdma_stat rib_qp_init(rib_qp_t *, int);
3607c478bd9Sstevel@tonic-gate static void rib_svc_scq_handler(ibt_cq_hdl_t, void *);
3617c478bd9Sstevel@tonic-gate static void rib_clnt_scq_handler(ibt_cq_hdl_t, void *);
3627c478bd9Sstevel@tonic-gate static void rib_clnt_rcq_handler(ibt_cq_hdl_t, void *);
3637c478bd9Sstevel@tonic-gate static void rib_svc_rcq_handler(ibt_cq_hdl_t, void *);
3647c478bd9Sstevel@tonic-gate static rib_bufpool_t *rib_rbufpool_create(rib_hca_t *hca, int ptype, int num);
3650a701b1eSRobert Gordon static rdma_stat rib_reg_mem(rib_hca_t *, caddr_t adsp, caddr_t, uint_t,
3660a701b1eSRobert Gordon 	ibt_mr_flags_t, ibt_mr_hdl_t *, ibt_mr_desc_t *);
3670a701b1eSRobert Gordon static rdma_stat rib_reg_mem_user(rib_hca_t *, caddr_t, uint_t, ibt_mr_flags_t,
3680a701b1eSRobert Gordon 	ibt_mr_hdl_t *, ibt_mr_desc_t *, caddr_t);
369214ae7d0SSiddheshwar Mahesh static rdma_stat rib_conn_to_srv(rib_hca_t *, rib_qp_t *, rpcib_ping_t *);
3707c478bd9Sstevel@tonic-gate static rdma_stat rib_clnt_create_chan(rib_hca_t *, struct netbuf *,
3717c478bd9Sstevel@tonic-gate 	rib_qp_t **);
3727c478bd9Sstevel@tonic-gate static rdma_stat rib_svc_create_chan(rib_hca_t *, caddr_t, uint8_t,
3737c478bd9Sstevel@tonic-gate 	rib_qp_t **);
3747c478bd9Sstevel@tonic-gate static rdma_stat rib_sendwait(rib_qp_t *, struct send_wid *);
3757c478bd9Sstevel@tonic-gate static struct send_wid *rib_init_sendwait(uint32_t, int, rib_qp_t *);
3767c478bd9Sstevel@tonic-gate static int rib_free_sendwait(struct send_wid *);
3777c478bd9Sstevel@tonic-gate static struct rdma_done_list *rdma_done_add(rib_qp_t *qp, uint32_t xid);
3787c478bd9Sstevel@tonic-gate static void rdma_done_rm(rib_qp_t *qp, struct rdma_done_list *rd);
3797c478bd9Sstevel@tonic-gate static void rdma_done_rem_list(rib_qp_t *);
3807c478bd9Sstevel@tonic-gate static void rdma_done_notify(rib_qp_t *qp, uint32_t xid);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate static void rib_async_handler(void *,
3837c478bd9Sstevel@tonic-gate 	ibt_hca_hdl_t, ibt_async_code_t, ibt_async_event_t *);
3847c478bd9Sstevel@tonic-gate static rdma_stat rib_rem_rep(rib_qp_t *, struct reply *);
3857c478bd9Sstevel@tonic-gate static struct svc_recv *rib_init_svc_recv(rib_qp_t *, ibt_wr_ds_t *);
3867c478bd9Sstevel@tonic-gate static int rib_free_svc_recv(struct svc_recv *);
3877c478bd9Sstevel@tonic-gate static struct recv_wid *rib_create_wid(rib_qp_t *, ibt_wr_ds_t *, uint32_t);
3887c478bd9Sstevel@tonic-gate static void rib_free_wid(struct recv_wid *);
3897c478bd9Sstevel@tonic-gate static rdma_stat rib_disconnect_channel(CONN *, rib_conn_list_t *);
3907c478bd9Sstevel@tonic-gate static void rib_detach_hca(rib_hca_t *);
391065714dcSSiddheshwar Mahesh static void rib_close_a_channel(CONN *);
392065714dcSSiddheshwar Mahesh static void rib_send_hold(rib_qp_t *);
393065714dcSSiddheshwar Mahesh static void rib_send_rele(rib_qp_t *);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Registration with IBTF as a consumer
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate static struct ibt_clnt_modinfo_s rib_modinfo = {
39903494a98SBill Taylor 	IBTI_V_CURR,
4007c478bd9Sstevel@tonic-gate 	IBT_GENERIC,
4017c478bd9Sstevel@tonic-gate 	rib_async_handler,	/* async event handler */
4027c478bd9Sstevel@tonic-gate 	NULL,			/* Memory Region Handler */
4037c478bd9Sstevel@tonic-gate 	"nfs/ib"
4047c478bd9Sstevel@tonic-gate };
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate  * Global strucuture
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate typedef struct rpcib_s {
4117c478bd9Sstevel@tonic-gate 	dev_info_t	*rpcib_dip;
4127c478bd9Sstevel@tonic-gate 	kmutex_t	rpcib_mutex;
4137c478bd9Sstevel@tonic-gate } rpcib_t;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate rpcib_t rpcib;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * /etc/system controlled variable to control
4197c478bd9Sstevel@tonic-gate  * debugging in rpcib kernel module.
4207c478bd9Sstevel@tonic-gate  * Set it to values greater that 1 to control
4217c478bd9Sstevel@tonic-gate  * the amount of debugging messages required.
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate int rib_debug = 0;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate int
4267c478bd9Sstevel@tonic-gate _init(void)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	int error;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	error = mod_install((struct modlinkage *)&rib_modlinkage);
4317c478bd9Sstevel@tonic-gate 	if (error != 0) {
4327c478bd9Sstevel@tonic-gate 		/*
4337c478bd9Sstevel@tonic-gate 		 * Could not load module
4347c478bd9Sstevel@tonic-gate 		 */
4357c478bd9Sstevel@tonic-gate 		return (error);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	mutex_init(&plugin_state_lock, NULL, MUTEX_DRIVER, NULL);
4387c478bd9Sstevel@tonic-gate 	return (0);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate int
4427c478bd9Sstevel@tonic-gate _fini()
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int status;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/*
4477c478bd9Sstevel@tonic-gate 	 * Remove module
4487c478bd9Sstevel@tonic-gate 	 */
4497c478bd9Sstevel@tonic-gate 	if ((status = mod_remove(&rib_modlinkage)) != 0) {
4507c478bd9Sstevel@tonic-gate 		return (status);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	mutex_destroy(&plugin_state_lock);
4537c478bd9Sstevel@tonic-gate 	return (0);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate int
4577c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	return (mod_info(&rib_modlinkage, modinfop));
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * rpcib_getinfo()
4647c478bd9Sstevel@tonic-gate  * Given the device number, return the devinfo pointer or the
4657c478bd9Sstevel@tonic-gate  * instance number.
4667c478bd9Sstevel@tonic-gate  * Note: always succeed DDI_INFO_DEVT2INSTANCE, even before attach.
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4707c478bd9Sstevel@tonic-gate static int
4717c478bd9Sstevel@tonic-gate rpcib_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	switch (cmd) {
4767c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4777c478bd9Sstevel@tonic-gate 		if (rpcib.rpcib_dip != NULL)
4787c478bd9Sstevel@tonic-gate 			*result = rpcib.rpcib_dip;
4797c478bd9Sstevel@tonic-gate 		else {
4807c478bd9Sstevel@tonic-gate 			*result = NULL;
4817c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 		break;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4867c478bd9Sstevel@tonic-gate 		*result = NULL;
4877c478bd9Sstevel@tonic-gate 		break;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	default:
4907c478bd9Sstevel@tonic-gate 		ret = DDI_FAILURE;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	return (ret);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
495*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static void
496*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rpcib_free_hca_list()
497*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States {
498*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_hca_t *hca, *hcap;
499*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
500*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->hcas_list_lock, RW_WRITER);
501*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	hca = rib_stat->hcas_list;
502*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_stat->hcas_list = NULL;
503*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->hcas_list_lock);
504*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	while (hca != NULL) {
505*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_enter(&hca->state_lock, RW_WRITER);
506*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hcap = hca;
507*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca = hca->next;
508*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_stat->nhca_inited--;
509*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_mod.rdma_count--;
510*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hcap->state = HCA_DETACHED;
511*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_exit(&hcap->state_lock);
512*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_stop_hca_services(hcap);
513*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
514*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		kmem_free(hcap, sizeof (*hcap));
515*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
516*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States }
517*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
518*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static rdma_stat
519*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rpcib_free_service_list()
520*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States {
521*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_service_t *service;
522*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	ibt_status_t ret;
523*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
524*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->service_list_lock, RW_WRITER);
525*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	while (rib_stat->service_list != NULL) {
526*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		service = rib_stat->service_list;
527*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		ret = ibt_unbind_all_services(service->srv_hdl);
528*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (ret != IBT_SUCCESS) {
529*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&rib_stat->service_list_lock);
530*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States #ifdef DEBUG
531*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			cmn_err(CE_NOTE, "rpcib_free_service_list: "
532*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    "ibt_unbind_all_services failed (%d)\n", (int)ret);
533*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States #endif
534*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			return (RDMA_FAILED);
535*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
536*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		ret = ibt_deregister_service(rib_stat->ibt_clnt_hdl,
537*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    service->srv_hdl);
538*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (ret != IBT_SUCCESS) {
539*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&rib_stat->service_list_lock);
540*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States #ifdef DEBUG
541*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			cmn_err(CE_NOTE, "rpcib_free_service_list: "
542*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    "ibt_deregister_service failed (%d)\n", (int)ret);
543*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States #endif
544*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			return (RDMA_FAILED);
545*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
546*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_stat->service_list = service->next;
547*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		kmem_free(service, sizeof (rib_service_t));
548*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
549*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->service_list_lock);
550*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
551*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	return (RDMA_SUCCESS);
552*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States }
553*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
5547c478bd9Sstevel@tonic-gate static int
5557c478bd9Sstevel@tonic-gate rpcib_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
5587c478bd9Sstevel@tonic-gate 	rdma_stat	r_status;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	switch (cmd) {
5617c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
5627c478bd9Sstevel@tonic-gate 		break;
5637c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
5647c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5657c478bd9Sstevel@tonic-gate 	default:
5667c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	mutex_init(&rpcib.rpcib_mutex, NULL, MUTEX_DRIVER, NULL);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	mutex_enter(&rpcib.rpcib_mutex);
5727c478bd9Sstevel@tonic-gate 	if (rpcib.rpcib_dip != NULL) {
5737c478bd9Sstevel@tonic-gate 		mutex_exit(&rpcib.rpcib_mutex);
5747c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	rpcib.rpcib_dip = dip;
5777c478bd9Sstevel@tonic-gate 	mutex_exit(&rpcib.rpcib_mutex);
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * Create the "rpcib" minor-node.
5807c478bd9Sstevel@tonic-gate 	 */
5817c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip,
5827c478bd9Sstevel@tonic-gate 	    "rpcib", S_IFCHR, 0, DDI_PSEUDO, 0) != DDI_SUCCESS) {
5837c478bd9Sstevel@tonic-gate 		/* Error message, no cmn_err as they print on console */
5847c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (rib_stat == NULL) {
5887c478bd9Sstevel@tonic-gate 		rib_stat = kmem_zalloc(sizeof (*rib_stat), KM_SLEEP);
5897c478bd9Sstevel@tonic-gate 		mutex_init(&rib_stat->open_hca_lock, NULL, MUTEX_DRIVER, NULL);
590*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_init(&rib_stat->hcas_list_lock, NULL, RW_DRIVER, NULL);
591*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_init(&rib_stat->listen_lock, NULL, MUTEX_DRIVER, NULL);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
594*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_stat->hca_count = ibt_get_hca_list(NULL);
5957c478bd9Sstevel@tonic-gate 	if (rib_stat->hca_count < 1) {
596*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_destroy(&rib_stat->listen_lock);
597*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_destroy(&rib_stat->hcas_list_lock);
5987c478bd9Sstevel@tonic-gate 		mutex_destroy(&rib_stat->open_hca_lock);
5997c478bd9Sstevel@tonic-gate 		kmem_free(rib_stat, sizeof (*rib_stat));
6007c478bd9Sstevel@tonic-gate 		rib_stat = NULL;
6017c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	ibt_status = ibt_attach(&rib_modinfo, dip,
6057c478bd9Sstevel@tonic-gate 	    (void *)rib_stat, &rib_stat->ibt_clnt_hdl);
6060a701b1eSRobert Gordon 
6077c478bd9Sstevel@tonic-gate 	if (ibt_status != IBT_SUCCESS) {
608*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_destroy(&rib_stat->listen_lock);
609*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_destroy(&rib_stat->hcas_list_lock);
6107c478bd9Sstevel@tonic-gate 		mutex_destroy(&rib_stat->open_hca_lock);
6117c478bd9Sstevel@tonic-gate 		kmem_free(rib_stat, sizeof (*rib_stat));
6127c478bd9Sstevel@tonic-gate 		rib_stat = NULL;
6137c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
616*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_stat->service_list = NULL;
617*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_init(&rib_stat->service_list_lock, NULL, RW_DRIVER, NULL);
6187c478bd9Sstevel@tonic-gate 	mutex_enter(&rib_stat->open_hca_lock);
619*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (rpcib_open_hcas(rib_stat) != RDMA_SUCCESS) {
6207c478bd9Sstevel@tonic-gate 		mutex_exit(&rib_stat->open_hca_lock);
62151f34d4bSRajkumar Sivaprakasam 		goto open_fail;
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 	mutex_exit(&rib_stat->open_hca_lock);
6247c478bd9Sstevel@tonic-gate 
62551f34d4bSRajkumar Sivaprakasam 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
62651f34d4bSRajkumar Sivaprakasam 	    DDI_PROP_SUCCESS) {
62751f34d4bSRajkumar Sivaprakasam 		cmn_err(CE_WARN, "rpcib_attach: ddi-no-autodetach prop update "
62851f34d4bSRajkumar Sivaprakasam 		    "failed.");
62951f34d4bSRajkumar Sivaprakasam 		goto register_fail;
63051f34d4bSRajkumar Sivaprakasam 	}
63151f34d4bSRajkumar Sivaprakasam 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * Register with rdmatf
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 	r_status = rdma_register_mod(&rib_mod);
6367c478bd9Sstevel@tonic-gate 	if (r_status != RDMA_SUCCESS && r_status != RDMA_REG_EXIST) {
63751f34d4bSRajkumar Sivaprakasam 		cmn_err(CE_WARN, "rpcib_attach:rdma_register_mod failed, "
63851f34d4bSRajkumar Sivaprakasam 		    "status = %d", r_status);
63951f34d4bSRajkumar Sivaprakasam 		goto register_fail;
64051f34d4bSRajkumar Sivaprakasam 	}
64151f34d4bSRajkumar Sivaprakasam 
64251f34d4bSRajkumar Sivaprakasam 	return (DDI_SUCCESS);
64351f34d4bSRajkumar Sivaprakasam 
64451f34d4bSRajkumar Sivaprakasam register_fail:
645*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
64651f34d4bSRajkumar Sivaprakasam open_fail:
6477c478bd9Sstevel@tonic-gate 	(void) ibt_detach(rib_stat->ibt_clnt_hdl);
648*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rpcib_free_hca_list();
649*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	(void) rpcib_free_service_list();
650*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_destroy(&rib_stat->listen_lock);
651*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_destroy(&rib_stat->hcas_list_lock);
6527c478bd9Sstevel@tonic-gate 	mutex_destroy(&rib_stat->open_hca_lock);
653*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_destroy(&rib_stat->service_list_lock);
6547c478bd9Sstevel@tonic-gate 	kmem_free(rib_stat, sizeof (*rib_stat));
6557c478bd9Sstevel@tonic-gate 	rib_stat = NULL;
6567c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6607c478bd9Sstevel@tonic-gate static int
6617c478bd9Sstevel@tonic-gate rpcib_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	switch (cmd) {
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
6667c478bd9Sstevel@tonic-gate 		break;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
6697c478bd9Sstevel@tonic-gate 	default:
6707c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * Detach the hca and free resources
6757c478bd9Sstevel@tonic-gate 	 */
6767c478bd9Sstevel@tonic-gate 	mutex_enter(&plugin_state_lock);
6777c478bd9Sstevel@tonic-gate 	plugin_state = NO_ACCEPT;
6787c478bd9Sstevel@tonic-gate 	mutex_exit(&plugin_state_lock);
679*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
680*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (rpcib_free_service_list() != RDMA_SUCCESS)
681*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		return (DDI_FAILURE);
682*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rpcib_free_hca_list();
683*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
6847c478bd9Sstevel@tonic-gate 	(void) ibt_detach(rib_stat->ibt_clnt_hdl);
685*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_destroy(&rib_stat->listen_lock);
686*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_destroy(&rib_stat->hcas_list_lock);
68751f34d4bSRajkumar Sivaprakasam 	mutex_destroy(&rib_stat->open_hca_lock);
688*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_destroy(&rib_stat->service_list_lock);
689*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
69051f34d4bSRajkumar Sivaprakasam 	kmem_free(rib_stat, sizeof (*rib_stat));
69151f34d4bSRajkumar Sivaprakasam 	rib_stat = NULL;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	mutex_enter(&rpcib.rpcib_mutex);
6947c478bd9Sstevel@tonic-gate 	rpcib.rpcib_dip = NULL;
6957c478bd9Sstevel@tonic-gate 	mutex_exit(&rpcib.rpcib_mutex);
6967c478bd9Sstevel@tonic-gate 	mutex_destroy(&rpcib.rpcib_mutex);
6977c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate static void rib_rbufpool_free(rib_hca_t *, int);
7027c478bd9Sstevel@tonic-gate static void rib_rbufpool_deregister(rib_hca_t *, int);
7037c478bd9Sstevel@tonic-gate static void rib_rbufpool_destroy(rib_hca_t *hca, int ptype);
7047c478bd9Sstevel@tonic-gate static struct reply *rib_addreplylist(rib_qp_t *, uint32_t);
7057c478bd9Sstevel@tonic-gate static rdma_stat rib_rem_replylist(rib_qp_t *);
7067c478bd9Sstevel@tonic-gate static int rib_remreply(rib_qp_t *, struct reply *);
7077c478bd9Sstevel@tonic-gate static rdma_stat rib_add_connlist(CONN *, rib_conn_list_t *);
7087c478bd9Sstevel@tonic-gate static rdma_stat rib_rm_conn(CONN *, rib_conn_list_t *);
7097c478bd9Sstevel@tonic-gate 
7100a701b1eSRobert Gordon 
7117c478bd9Sstevel@tonic-gate /*
7127c478bd9Sstevel@tonic-gate  * One CQ pair per HCA
7137c478bd9Sstevel@tonic-gate  */
7147c478bd9Sstevel@tonic-gate static rdma_stat
7157c478bd9Sstevel@tonic-gate rib_create_cq(rib_hca_t *hca, uint32_t cq_size, ibt_cq_handler_t cq_handler,
716*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_cq_t **cqp)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	rib_cq_t	*cq;
7197c478bd9Sstevel@tonic-gate 	ibt_cq_attr_t	cq_attr;
7207c478bd9Sstevel@tonic-gate 	uint32_t	real_size;
7217c478bd9Sstevel@tonic-gate 	ibt_status_t	status;
7227c478bd9Sstevel@tonic-gate 	rdma_stat	error = RDMA_SUCCESS;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	cq = kmem_zalloc(sizeof (rib_cq_t), KM_SLEEP);
7257c478bd9Sstevel@tonic-gate 	cq->rib_hca = hca;
7267c478bd9Sstevel@tonic-gate 	cq_attr.cq_size = cq_size;
7277c478bd9Sstevel@tonic-gate 	cq_attr.cq_flags = IBT_CQ_NO_FLAGS;
7287c478bd9Sstevel@tonic-gate 	status = ibt_alloc_cq(hca->hca_hdl, &cq_attr, &cq->rib_cq_hdl,
7297c478bd9Sstevel@tonic-gate 	    &real_size);
7307c478bd9Sstevel@tonic-gate 	if (status != IBT_SUCCESS) {
7317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rib_create_cq: ibt_alloc_cq() failed,"
7327c478bd9Sstevel@tonic-gate 		    " status=%d", status);
7337c478bd9Sstevel@tonic-gate 		error = RDMA_FAILED;
7347c478bd9Sstevel@tonic-gate 		goto fail;
7357c478bd9Sstevel@tonic-gate 	}
736*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	ibt_set_cq_handler(cq->rib_cq_hdl, cq_handler, hca);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	/*
7397c478bd9Sstevel@tonic-gate 	 * Enable CQ callbacks. CQ Callbacks are single shot
7407c478bd9Sstevel@tonic-gate 	 * (e.g. you have to call ibt_enable_cq_notify()
7417c478bd9Sstevel@tonic-gate 	 * after each callback to get another one).
7427c478bd9Sstevel@tonic-gate 	 */
7437c478bd9Sstevel@tonic-gate 	status = ibt_enable_cq_notify(cq->rib_cq_hdl, IBT_NEXT_COMPLETION);
7447c478bd9Sstevel@tonic-gate 	if (status != IBT_SUCCESS) {
7457c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rib_create_cq: "
7467c478bd9Sstevel@tonic-gate 		    "enable_cq_notify failed, status %d", status);
7477c478bd9Sstevel@tonic-gate 		error = RDMA_FAILED;
7487c478bd9Sstevel@tonic-gate 		goto fail;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	*cqp = cq;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	return (error);
7537c478bd9Sstevel@tonic-gate fail:
7547c478bd9Sstevel@tonic-gate 	if (cq->rib_cq_hdl)
7557c478bd9Sstevel@tonic-gate 		(void) ibt_free_cq(cq->rib_cq_hdl);
7567c478bd9Sstevel@tonic-gate 	if (cq)
7577c478bd9Sstevel@tonic-gate 		kmem_free(cq, sizeof (rib_cq_t));
7587c478bd9Sstevel@tonic-gate 	return (error);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
761*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States /*
762*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * rpcib_find_hca
763*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  *
764*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * Caller should have already locked the hcas_lock before calling
765*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * this function.
766*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  */
767*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static rib_hca_t *
768*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rpcib_find_hca(rpcib_state_t *ribstat, ib_guid_t guid)
769*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States {
770*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_hca_t *hca = ribstat->hcas_list;
771*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
772*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	while (hca && hca->hca_guid != guid)
773*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca = hca->next;
774*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
775*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	return (hca);
776*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States }
777*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
7787c478bd9Sstevel@tonic-gate static rdma_stat
779*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rpcib_open_hcas(rpcib_state_t *ribstat)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	rib_hca_t		*hca;
7827c478bd9Sstevel@tonic-gate 	ibt_status_t		ibt_status;
7837c478bd9Sstevel@tonic-gate 	rdma_stat		status;
7847c478bd9Sstevel@tonic-gate 	ibt_hca_portinfo_t	*pinfop;
7857c478bd9Sstevel@tonic-gate 	ibt_pd_flags_t		pd_flags = IBT_PD_NO_FLAGS;
7867c478bd9Sstevel@tonic-gate 	uint_t			size, cq_size;
7877c478bd9Sstevel@tonic-gate 	int			i;
7880a701b1eSRobert Gordon 	kstat_t *ksp;
7890a701b1eSRobert Gordon 	cache_avl_struct_t example_avl_node;
7900a701b1eSRobert Gordon 	char rssc_name[32];
791*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	int old_nhca_inited = ribstat->nhca_inited;
792*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	ib_guid_t		*hca_guids;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ribstat->open_hca_lock));
7950a701b1eSRobert Gordon 
796*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	ribstat->hca_count = ibt_get_hca_list(&hca_guids);
797*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (ribstat->hca_count == 0)
798*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		return (RDMA_FAILED);
7997c478bd9Sstevel@tonic-gate 
800*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&ribstat->hcas_list_lock, RW_WRITER);
8017c478bd9Sstevel@tonic-gate 	/*
8027c478bd9Sstevel@tonic-gate 	 * Open a hca and setup for RDMA
8037c478bd9Sstevel@tonic-gate 	 */
8047c478bd9Sstevel@tonic-gate 	for (i = 0; i < ribstat->hca_count; i++) {
805*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (rpcib_find_hca(ribstat, hca_guids[i]))
806*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			continue;
807*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca = kmem_zalloc(sizeof (rib_hca_t), KM_SLEEP);
808*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
8097c478bd9Sstevel@tonic-gate 		ibt_status = ibt_open_hca(ribstat->ibt_clnt_hdl,
810*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    hca_guids[i], &hca->hca_hdl);
8117c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS) {
812*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			kmem_free(hca, sizeof (rib_hca_t));
8137c478bd9Sstevel@tonic-gate 			continue;
8147c478bd9Sstevel@tonic-gate 		}
815*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca->hca_guid = hca_guids[i];
8167c478bd9Sstevel@tonic-gate 		hca->ibt_clnt_hdl = ribstat->ibt_clnt_hdl;
8177c478bd9Sstevel@tonic-gate 		hca->state = HCA_INITED;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		/*
8207c478bd9Sstevel@tonic-gate 		 * query HCA info
8217c478bd9Sstevel@tonic-gate 		 */
8227c478bd9Sstevel@tonic-gate 		ibt_status = ibt_query_hca(hca->hca_hdl, &hca->hca_attrs);
8237c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS) {
8247c478bd9Sstevel@tonic-gate 			goto fail1;
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		/*
8287c478bd9Sstevel@tonic-gate 		 * One PD (Protection Domain) per HCA.
8297c478bd9Sstevel@tonic-gate 		 * A qp is allowed to access a memory region
8307c478bd9Sstevel@tonic-gate 		 * only when it's in the same PD as that of
8317c478bd9Sstevel@tonic-gate 		 * the memory region.
8327c478bd9Sstevel@tonic-gate 		 */
8337c478bd9Sstevel@tonic-gate 		ibt_status = ibt_alloc_pd(hca->hca_hdl, pd_flags, &hca->pd_hdl);
8347c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS) {
8357c478bd9Sstevel@tonic-gate 			goto fail1;
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		/*
8397c478bd9Sstevel@tonic-gate 		 * query HCA ports
8407c478bd9Sstevel@tonic-gate 		 */
8417c478bd9Sstevel@tonic-gate 		ibt_status = ibt_query_hca_ports(hca->hca_hdl,
8427c478bd9Sstevel@tonic-gate 		    0, &pinfop, &hca->hca_nports, &size);
8437c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS) {
8447c478bd9Sstevel@tonic-gate 			goto fail2;
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 		hca->hca_ports = pinfop;
8477c478bd9Sstevel@tonic-gate 		hca->hca_pinfosz = size;
8487c478bd9Sstevel@tonic-gate 		pinfop = NULL;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		cq_size = DEF_CQ_SIZE; /* default cq size */
8517c478bd9Sstevel@tonic-gate 		/*
8527c478bd9Sstevel@tonic-gate 		 * Create 2 pairs of cq's (1 pair for client
8537c478bd9Sstevel@tonic-gate 		 * and the other pair for server) on this hca.
8547c478bd9Sstevel@tonic-gate 		 * If number of qp's gets too large, then several
8557c478bd9Sstevel@tonic-gate 		 * cq's will be needed.
8567c478bd9Sstevel@tonic-gate 		 */
8577c478bd9Sstevel@tonic-gate 		status = rib_create_cq(hca, cq_size, rib_svc_rcq_handler,
858*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    &hca->svc_rcq);
8597c478bd9Sstevel@tonic-gate 		if (status != RDMA_SUCCESS) {
8607c478bd9Sstevel@tonic-gate 			goto fail3;
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		status = rib_create_cq(hca, cq_size, rib_svc_scq_handler,
864*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    &hca->svc_scq);
8657c478bd9Sstevel@tonic-gate 		if (status != RDMA_SUCCESS) {
8667c478bd9Sstevel@tonic-gate 			goto fail3;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		status = rib_create_cq(hca, cq_size, rib_clnt_rcq_handler,
870*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    &hca->clnt_rcq);
8717c478bd9Sstevel@tonic-gate 		if (status != RDMA_SUCCESS) {
8727c478bd9Sstevel@tonic-gate 			goto fail3;
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		status = rib_create_cq(hca, cq_size, rib_clnt_scq_handler,
876*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    &hca->clnt_scq);
8777c478bd9Sstevel@tonic-gate 		if (status != RDMA_SUCCESS) {
8787c478bd9Sstevel@tonic-gate 			goto fail3;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 		/*
8827c478bd9Sstevel@tonic-gate 		 * Create buffer pools.
8837c478bd9Sstevel@tonic-gate 		 * Note rib_rbuf_create also allocates memory windows.
8847c478bd9Sstevel@tonic-gate 		 */
8857c478bd9Sstevel@tonic-gate 		hca->recv_pool = rib_rbufpool_create(hca,
886065714dcSSiddheshwar Mahesh 		    RECV_BUFFER, rib_max_rbufs);
8877c478bd9Sstevel@tonic-gate 		if (hca->recv_pool == NULL) {
8887c478bd9Sstevel@tonic-gate 			goto fail3;
8897c478bd9Sstevel@tonic-gate 		}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		hca->send_pool = rib_rbufpool_create(hca,
892065714dcSSiddheshwar Mahesh 		    SEND_BUFFER, rib_max_rbufs);
8937c478bd9Sstevel@tonic-gate 		if (hca->send_pool == NULL) {
8947c478bd9Sstevel@tonic-gate 			rib_rbufpool_destroy(hca, RECV_BUFFER);
8957c478bd9Sstevel@tonic-gate 			goto fail3;
8967c478bd9Sstevel@tonic-gate 		}
8977c478bd9Sstevel@tonic-gate 
8980a701b1eSRobert Gordon 		if (hca->server_side_cache == NULL) {
8990a701b1eSRobert Gordon 			(void) sprintf(rssc_name,
900*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    "rib_srvr_cache_%llx",
901*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    (long long unsigned int) hca->hca_guid);
9020a701b1eSRobert Gordon 			hca->server_side_cache = kmem_cache_create(
9030a701b1eSRobert Gordon 			    rssc_name,
9040a701b1eSRobert Gordon 			    sizeof (cache_avl_struct_t), 0,
9050a701b1eSRobert Gordon 			    NULL,
9060a701b1eSRobert Gordon 			    NULL,
9070a701b1eSRobert Gordon 			    rib_server_side_cache_reclaim,
9080a701b1eSRobert Gordon 			    hca, NULL, 0);
9090a701b1eSRobert Gordon 		}
9100a701b1eSRobert Gordon 
9110a701b1eSRobert Gordon 		avl_create(&hca->avl_tree,
9120a701b1eSRobert Gordon 		    avl_compare,
9130a701b1eSRobert Gordon 		    sizeof (cache_avl_struct_t),
9140a701b1eSRobert Gordon 		    (uint_t)(uintptr_t)&example_avl_node.avl_link-
9150a701b1eSRobert Gordon 		    (uint_t)(uintptr_t)&example_avl_node);
9160a701b1eSRobert Gordon 
917*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_init(&hca->bound_services_lock, NULL, RW_DRIVER,
918*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    hca->iblock);
919*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_init(&hca->state_lock, NULL, RW_DRIVER, hca->iblock);
9200a701b1eSRobert Gordon 		rw_init(&hca->avl_rw_lock,
9210a701b1eSRobert Gordon 		    NULL, RW_DRIVER, hca->iblock);
922*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_init(&hca->cache_allocation_lock,
9230a701b1eSRobert Gordon 		    NULL, MUTEX_DRIVER, NULL);
9240a701b1eSRobert Gordon 		hca->avl_init = TRUE;
9250a701b1eSRobert Gordon 
9260a701b1eSRobert Gordon 		/* Create kstats for the cache */
9270a701b1eSRobert Gordon 		ASSERT(INGLOBALZONE(curproc));
9280a701b1eSRobert Gordon 
9290a701b1eSRobert Gordon 		if (!stats_enabled) {
9300a701b1eSRobert Gordon 			ksp = kstat_create_zone("unix", 0, "rpcib_cache", "rpc",
9310a701b1eSRobert Gordon 			    KSTAT_TYPE_NAMED,
9320a701b1eSRobert Gordon 			    sizeof (rpcib_kstat) / sizeof (kstat_named_t),
9330a701b1eSRobert Gordon 			    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE,
9340a701b1eSRobert Gordon 			    GLOBAL_ZONEID);
9350a701b1eSRobert Gordon 			if (ksp) {
9360a701b1eSRobert Gordon 				ksp->ks_data = (void *) &rpcib_kstat;
9370a701b1eSRobert Gordon 				ksp->ks_update = rpcib_cache_kstat_update;
9380a701b1eSRobert Gordon 				kstat_install(ksp);
9390a701b1eSRobert Gordon 				stats_enabled = TRUE;
9400a701b1eSRobert Gordon 			}
9410a701b1eSRobert Gordon 		}
942065714dcSSiddheshwar Mahesh 		if (hca->cleanup_helper == NULL) {
943*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			char tq_name[sizeof (hca->hca_guid) * 2 + 1];
9440a701b1eSRobert Gordon 
945*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			(void) snprintf(tq_name, sizeof (tq_name), "%llX",
946*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    (unsigned long long int) hca->hca_guid);
947*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			hca->cleanup_helper = ddi_taskq_create(NULL,
948*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    tq_name, 1, TASKQ_DEFAULTPRI, 0);
949*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 		mutex_init(&hca->cb_lock, NULL, MUTEX_DRIVER, hca->iblock);
9527c478bd9Sstevel@tonic-gate 		cv_init(&hca->cb_cv, NULL, CV_DRIVER, NULL);
9537c478bd9Sstevel@tonic-gate 		rw_init(&hca->cl_conn_list.conn_lock, NULL, RW_DRIVER,
9547c478bd9Sstevel@tonic-gate 		    hca->iblock);
9557c478bd9Sstevel@tonic-gate 		rw_init(&hca->srv_conn_list.conn_lock, NULL, RW_DRIVER,
9567c478bd9Sstevel@tonic-gate 		    hca->iblock);
9577c478bd9Sstevel@tonic-gate 		mutex_init(&hca->inuse_lock, NULL, MUTEX_DRIVER, hca->iblock);
9587c478bd9Sstevel@tonic-gate 		hca->inuse = TRUE;
959*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
960*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca->next = ribstat->hcas_list;
961*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		ribstat->hcas_list = hca;
9627c478bd9Sstevel@tonic-gate 		ribstat->nhca_inited++;
9637c478bd9Sstevel@tonic-gate 		ibt_free_portinfo(hca->hca_ports, hca->hca_pinfosz);
964*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		continue;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate fail3:
9677c478bd9Sstevel@tonic-gate 		ibt_free_portinfo(hca->hca_ports, hca->hca_pinfosz);
9687c478bd9Sstevel@tonic-gate fail2:
9697c478bd9Sstevel@tonic-gate 		(void) ibt_free_pd(hca->hca_hdl, hca->pd_hdl);
9707c478bd9Sstevel@tonic-gate fail1:
9717c478bd9Sstevel@tonic-gate 		(void) ibt_close_hca(hca->hca_hdl);
972*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		kmem_free(hca, sizeof (rib_hca_t));
9737c478bd9Sstevel@tonic-gate 	}
974*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&ribstat->hcas_list_lock);
975*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	ibt_free_hca_list(hca_guids, ribstat->hca_count);
976*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_mod.rdma_count = rib_stat->nhca_inited;
977*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
978*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	/*
979*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * return success if at least one new hca has been configured.
980*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 */
981*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (ribstat->nhca_inited != old_nhca_inited)
9827c478bd9Sstevel@tonic-gate 		return (RDMA_SUCCESS);
9837c478bd9Sstevel@tonic-gate 	else
9847c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate /*
9887c478bd9Sstevel@tonic-gate  * Callback routines
9897c478bd9Sstevel@tonic-gate  */
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate /*
9927c478bd9Sstevel@tonic-gate  * SCQ handlers
9937c478bd9Sstevel@tonic-gate  */
9947c478bd9Sstevel@tonic-gate /* ARGSUSED */
9957c478bd9Sstevel@tonic-gate static void
9967c478bd9Sstevel@tonic-gate rib_clnt_scq_handler(ibt_cq_hdl_t cq_hdl, void *arg)
9977c478bd9Sstevel@tonic-gate {
9987c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
9997c478bd9Sstevel@tonic-gate 	ibt_wc_t	wc;
1000065714dcSSiddheshwar Mahesh 	struct send_wid	*wd;
1001065714dcSSiddheshwar Mahesh 	CONN		*conn;
1002065714dcSSiddheshwar Mahesh 	rib_qp_t	*qp;
10037c478bd9Sstevel@tonic-gate 	int		i;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * Re-enable cq notify here to avoid missing any
10077c478bd9Sstevel@tonic-gate 	 * completion queue notification.
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate 	(void) ibt_enable_cq_notify(cq_hdl, IBT_NEXT_COMPLETION);
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	ibt_status = IBT_SUCCESS;
10127c478bd9Sstevel@tonic-gate 	while (ibt_status != IBT_CQ_EMPTY) {
10137c478bd9Sstevel@tonic-gate 		bzero(&wc, sizeof (wc));
10147c478bd9Sstevel@tonic-gate 		ibt_status = ibt_poll_cq(cq_hdl, &wc, 1, NULL);
10157c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS)
10167c478bd9Sstevel@tonic-gate 			return;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 		/*
10197c478bd9Sstevel@tonic-gate 		 * Got a send completion
10207c478bd9Sstevel@tonic-gate 		 */
1021065714dcSSiddheshwar Mahesh 		if (wc.wc_id != RDMA_DUMMY_WRID) {
1022065714dcSSiddheshwar Mahesh 			wd = (struct send_wid *)(uintptr_t)wc.wc_id;
1023065714dcSSiddheshwar Mahesh 			qp = wd->qp;
1024065714dcSSiddheshwar Mahesh 			conn = qptoc(qp);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 			mutex_enter(&wd->sendwait_lock);
10277c478bd9Sstevel@tonic-gate 			switch (wc.wc_status) {
10287c478bd9Sstevel@tonic-gate 			case IBT_WC_SUCCESS:
10297c478bd9Sstevel@tonic-gate 				wd->status = RDMA_SUCCESS;
10307c478bd9Sstevel@tonic-gate 				break;
10317c478bd9Sstevel@tonic-gate 			default:
10327c478bd9Sstevel@tonic-gate /*
10337c478bd9Sstevel@tonic-gate  *    RC Send Q Error Code		Local state     Remote State
10347c478bd9Sstevel@tonic-gate  *    ==================== 		===========     ============
10357c478bd9Sstevel@tonic-gate  *    IBT_WC_BAD_RESPONSE_ERR             ERROR           None
10367c478bd9Sstevel@tonic-gate  *    IBT_WC_LOCAL_LEN_ERR                ERROR           None
10377c478bd9Sstevel@tonic-gate  *    IBT_WC_LOCAL_CHAN_OP_ERR            ERROR           None
10387c478bd9Sstevel@tonic-gate  *    IBT_WC_LOCAL_PROTECT_ERR            ERROR           None
10397c478bd9Sstevel@tonic-gate  *    IBT_WC_MEM_WIN_BIND_ERR             ERROR           None
10407c478bd9Sstevel@tonic-gate  *    IBT_WC_REMOTE_INVALID_REQ_ERR       ERROR           ERROR
10417c478bd9Sstevel@tonic-gate  *    IBT_WC_REMOTE_ACCESS_ERR            ERROR           ERROR
10427c478bd9Sstevel@tonic-gate  *    IBT_WC_REMOTE_OP_ERR                ERROR           ERROR
10437c478bd9Sstevel@tonic-gate  *    IBT_WC_RNR_NAK_TIMEOUT_ERR          ERROR           None
10447c478bd9Sstevel@tonic-gate  *    IBT_WC_TRANS_TIMEOUT_ERR            ERROR           None
1045065714dcSSiddheshwar Mahesh  *    IBT_WC_WR_FLUSHED_ERR               ERROR           None
10467c478bd9Sstevel@tonic-gate  */
10477c478bd9Sstevel@tonic-gate 				/*
10487c478bd9Sstevel@tonic-gate 				 * Channel in error state. Set connection to
10497c478bd9Sstevel@tonic-gate 				 * ERROR and cleanup will happen either from
10507c478bd9Sstevel@tonic-gate 				 * conn_release  or from rib_conn_get
10517c478bd9Sstevel@tonic-gate 				 */
10527c478bd9Sstevel@tonic-gate 				wd->status = RDMA_FAILED;
10537c478bd9Sstevel@tonic-gate 				mutex_enter(&conn->c_lock);
10547c478bd9Sstevel@tonic-gate 				if (conn->c_state != C_DISCONN_PEND)
10550a701b1eSRobert Gordon 					conn->c_state = C_ERROR_CONN;
10567c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
10577c478bd9Sstevel@tonic-gate 				break;
10587c478bd9Sstevel@tonic-gate 			}
10590a701b1eSRobert Gordon 
10607c478bd9Sstevel@tonic-gate 			if (wd->cv_sig == 1) {
10617c478bd9Sstevel@tonic-gate 				/*
10627c478bd9Sstevel@tonic-gate 				 * Notify poster
10637c478bd9Sstevel@tonic-gate 				 */
10647c478bd9Sstevel@tonic-gate 				cv_signal(&wd->wait_cv);
10657c478bd9Sstevel@tonic-gate 				mutex_exit(&wd->sendwait_lock);
10667c478bd9Sstevel@tonic-gate 			} else {
10677c478bd9Sstevel@tonic-gate 				/*
10687c478bd9Sstevel@tonic-gate 				 * Poster not waiting for notification.
10697c478bd9Sstevel@tonic-gate 				 * Free the send buffers and send_wid
10707c478bd9Sstevel@tonic-gate 				 */
10717c478bd9Sstevel@tonic-gate 				for (i = 0; i < wd->nsbufs; i++) {
1072065714dcSSiddheshwar Mahesh 					rib_rbuf_free(qptoc(wd->qp),
1073065714dcSSiddheshwar Mahesh 					    SEND_BUFFER,
107411606941Sjwahlig 					    (void *)(uintptr_t)wd->sbufaddr[i]);
10757c478bd9Sstevel@tonic-gate 				}
1076065714dcSSiddheshwar Mahesh 
1077065714dcSSiddheshwar Mahesh 				/* decrement the send ref count */
1078065714dcSSiddheshwar Mahesh 				rib_send_rele(qp);
1079065714dcSSiddheshwar Mahesh 
10807c478bd9Sstevel@tonic-gate 				mutex_exit(&wd->sendwait_lock);
10817c478bd9Sstevel@tonic-gate 				(void) rib_free_sendwait(wd);
10827c478bd9Sstevel@tonic-gate 			}
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate /* ARGSUSED */
10887c478bd9Sstevel@tonic-gate static void
10897c478bd9Sstevel@tonic-gate rib_svc_scq_handler(ibt_cq_hdl_t cq_hdl, void *arg)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
10927c478bd9Sstevel@tonic-gate 	ibt_wc_t	wc;
1093065714dcSSiddheshwar Mahesh 	struct send_wid	*wd;
1094065714dcSSiddheshwar Mahesh 	rib_qp_t	*qp;
1095065714dcSSiddheshwar Mahesh 	CONN		*conn;
10967c478bd9Sstevel@tonic-gate 	int		i;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	/*
10997c478bd9Sstevel@tonic-gate 	 * Re-enable cq notify here to avoid missing any
11007c478bd9Sstevel@tonic-gate 	 * completion queue notification.
11017c478bd9Sstevel@tonic-gate 	 */
11027c478bd9Sstevel@tonic-gate 	(void) ibt_enable_cq_notify(cq_hdl, IBT_NEXT_COMPLETION);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	ibt_status = IBT_SUCCESS;
11057c478bd9Sstevel@tonic-gate 	while (ibt_status != IBT_CQ_EMPTY) {
11067c478bd9Sstevel@tonic-gate 		bzero(&wc, sizeof (wc));
11077c478bd9Sstevel@tonic-gate 		ibt_status = ibt_poll_cq(cq_hdl, &wc, 1, NULL);
11087c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS)
11097c478bd9Sstevel@tonic-gate 			return;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		/*
11127c478bd9Sstevel@tonic-gate 		 * Got a send completion
11137c478bd9Sstevel@tonic-gate 		 */
1114065714dcSSiddheshwar Mahesh 		if (wc.wc_id != RDMA_DUMMY_WRID) {
1115065714dcSSiddheshwar Mahesh 			wd = (struct send_wid *)(uintptr_t)wc.wc_id;
1116065714dcSSiddheshwar Mahesh 			qp = wd->qp;
1117065714dcSSiddheshwar Mahesh 			conn = qptoc(qp);
11187c478bd9Sstevel@tonic-gate 			mutex_enter(&wd->sendwait_lock);
1119065714dcSSiddheshwar Mahesh 
1120065714dcSSiddheshwar Mahesh 			switch (wc.wc_status) {
1121065714dcSSiddheshwar Mahesh 			case IBT_WC_SUCCESS:
1122065714dcSSiddheshwar Mahesh 				wd->status = RDMA_SUCCESS;
1123065714dcSSiddheshwar Mahesh 				break;
1124065714dcSSiddheshwar Mahesh 			default:
1125065714dcSSiddheshwar Mahesh 				/*
1126065714dcSSiddheshwar Mahesh 				 * Channel in error state. Set connection to
1127065714dcSSiddheshwar Mahesh 				 * ERROR and cleanup will happen either from
1128065714dcSSiddheshwar Mahesh 				 * conn_release  or conn timeout.
1129065714dcSSiddheshwar Mahesh 				 */
1130065714dcSSiddheshwar Mahesh 				wd->status = RDMA_FAILED;
1131065714dcSSiddheshwar Mahesh 				mutex_enter(&conn->c_lock);
1132065714dcSSiddheshwar Mahesh 				if (conn->c_state != C_DISCONN_PEND)
1133065714dcSSiddheshwar Mahesh 					conn->c_state = C_ERROR_CONN;
1134065714dcSSiddheshwar Mahesh 				mutex_exit(&conn->c_lock);
1135065714dcSSiddheshwar Mahesh 				break;
1136065714dcSSiddheshwar Mahesh 			}
1137065714dcSSiddheshwar Mahesh 
11387c478bd9Sstevel@tonic-gate 			if (wd->cv_sig == 1) {
11397c478bd9Sstevel@tonic-gate 				/*
11407c478bd9Sstevel@tonic-gate 				 * Update completion status and notify poster
11417c478bd9Sstevel@tonic-gate 				 */
11427c478bd9Sstevel@tonic-gate 				cv_signal(&wd->wait_cv);
11437c478bd9Sstevel@tonic-gate 				mutex_exit(&wd->sendwait_lock);
11447c478bd9Sstevel@tonic-gate 			} else {
11457c478bd9Sstevel@tonic-gate 				/*
11467c478bd9Sstevel@tonic-gate 				 * Poster not waiting for notification.
11477c478bd9Sstevel@tonic-gate 				 * Free the send buffers and send_wid
11487c478bd9Sstevel@tonic-gate 				 */
11497c478bd9Sstevel@tonic-gate 				for (i = 0; i < wd->nsbufs; i++) {
11500a701b1eSRobert Gordon 					rib_rbuf_free(qptoc(wd->qp),
11510a701b1eSRobert Gordon 					    SEND_BUFFER,
115211606941Sjwahlig 					    (void *)(uintptr_t)wd->sbufaddr[i]);
11537c478bd9Sstevel@tonic-gate 				}
1154065714dcSSiddheshwar Mahesh 
1155065714dcSSiddheshwar Mahesh 				/* decrement the send ref count */
1156065714dcSSiddheshwar Mahesh 				rib_send_rele(qp);
1157065714dcSSiddheshwar Mahesh 
11587c478bd9Sstevel@tonic-gate 				mutex_exit(&wd->sendwait_lock);
11597c478bd9Sstevel@tonic-gate 				(void) rib_free_sendwait(wd);
11607c478bd9Sstevel@tonic-gate 			}
11617c478bd9Sstevel@tonic-gate 		}
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate /*
11667c478bd9Sstevel@tonic-gate  * RCQ handler
11677c478bd9Sstevel@tonic-gate  */
11687c478bd9Sstevel@tonic-gate /* ARGSUSED */
11697c478bd9Sstevel@tonic-gate static void
11707c478bd9Sstevel@tonic-gate rib_clnt_rcq_handler(ibt_cq_hdl_t cq_hdl, void *arg)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp;
11737c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
11747c478bd9Sstevel@tonic-gate 	ibt_wc_t	wc;
11757c478bd9Sstevel@tonic-gate 	struct recv_wid	*rwid;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	/*
11787c478bd9Sstevel@tonic-gate 	 * Re-enable cq notify here to avoid missing any
11797c478bd9Sstevel@tonic-gate 	 * completion queue notification.
11807c478bd9Sstevel@tonic-gate 	 */
11817c478bd9Sstevel@tonic-gate 	(void) ibt_enable_cq_notify(cq_hdl, IBT_NEXT_COMPLETION);
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	ibt_status = IBT_SUCCESS;
11847c478bd9Sstevel@tonic-gate 	while (ibt_status != IBT_CQ_EMPTY) {
11857c478bd9Sstevel@tonic-gate 		bzero(&wc, sizeof (wc));
11867c478bd9Sstevel@tonic-gate 		ibt_status = ibt_poll_cq(cq_hdl, &wc, 1, NULL);
11877c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS)
11887c478bd9Sstevel@tonic-gate 			return;
11897c478bd9Sstevel@tonic-gate 
119011606941Sjwahlig 		rwid = (struct recv_wid *)(uintptr_t)wc.wc_id;
11917c478bd9Sstevel@tonic-gate 		qp = rwid->qp;
11927c478bd9Sstevel@tonic-gate 		if (wc.wc_status == IBT_WC_SUCCESS) {
11937c478bd9Sstevel@tonic-gate 			XDR	inxdrs, *xdrs;
11947c478bd9Sstevel@tonic-gate 			uint_t	xid, vers, op, find_xid = 0;
11957c478bd9Sstevel@tonic-gate 			struct reply	*r;
11967c478bd9Sstevel@tonic-gate 			CONN *conn = qptoc(qp);
11970a701b1eSRobert Gordon 			uint32_t rdma_credit = 0;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 			xdrs = &inxdrs;
120011606941Sjwahlig 			xdrmem_create(xdrs, (caddr_t)(uintptr_t)rwid->addr,
12017c478bd9Sstevel@tonic-gate 			    wc.wc_bytes_xfer, XDR_DECODE);
12027c478bd9Sstevel@tonic-gate 			/*
12037c478bd9Sstevel@tonic-gate 			 * Treat xid as opaque (xid is the first entity
12047c478bd9Sstevel@tonic-gate 			 * in the rpc rdma message).
12057c478bd9Sstevel@tonic-gate 			 */
120611606941Sjwahlig 			xid = *(uint32_t *)(uintptr_t)rwid->addr;
12070a701b1eSRobert Gordon 
12087c478bd9Sstevel@tonic-gate 			/* Skip xid and set the xdr position accordingly. */
12097c478bd9Sstevel@tonic-gate 			XDR_SETPOS(xdrs, sizeof (uint32_t));
12107c478bd9Sstevel@tonic-gate 			(void) xdr_u_int(xdrs, &vers);
12110a701b1eSRobert Gordon 			(void) xdr_u_int(xdrs, &rdma_credit);
12127c478bd9Sstevel@tonic-gate 			(void) xdr_u_int(xdrs, &op);
12137c478bd9Sstevel@tonic-gate 			XDR_DESTROY(xdrs);
12140a701b1eSRobert Gordon 
12157c478bd9Sstevel@tonic-gate 			if (vers != RPCRDMA_VERS) {
12167c478bd9Sstevel@tonic-gate 				/*
12170a701b1eSRobert Gordon 				 * Invalid RPC/RDMA version. Cannot
12180a701b1eSRobert Gordon 				 * interoperate.  Set connection to
12190a701b1eSRobert Gordon 				 * ERROR state and bail out.
12207c478bd9Sstevel@tonic-gate 				 */
12217c478bd9Sstevel@tonic-gate 				mutex_enter(&conn->c_lock);
12227c478bd9Sstevel@tonic-gate 				if (conn->c_state != C_DISCONN_PEND)
12230a701b1eSRobert Gordon 					conn->c_state = C_ERROR_CONN;
12247c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
122511606941Sjwahlig 				rib_rbuf_free(conn, RECV_BUFFER,
122611606941Sjwahlig 				    (void *)(uintptr_t)rwid->addr);
12277c478bd9Sstevel@tonic-gate 				rib_free_wid(rwid);
12287c478bd9Sstevel@tonic-gate 				continue;
12297c478bd9Sstevel@tonic-gate 			}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 			mutex_enter(&qp->replylist_lock);
12327c478bd9Sstevel@tonic-gate 			for (r = qp->replylist; r != NULL; r = r->next) {
12337c478bd9Sstevel@tonic-gate 				if (r->xid == xid) {
12347c478bd9Sstevel@tonic-gate 					find_xid = 1;
12357c478bd9Sstevel@tonic-gate 					switch (op) {
12367c478bd9Sstevel@tonic-gate 					case RDMA_MSG:
12377c478bd9Sstevel@tonic-gate 					case RDMA_NOMSG:
12387c478bd9Sstevel@tonic-gate 					case RDMA_MSGP:
12397c478bd9Sstevel@tonic-gate 						r->status = RDMA_SUCCESS;
12407c478bd9Sstevel@tonic-gate 						r->vaddr_cq = rwid->addr;
12410a701b1eSRobert Gordon 						r->bytes_xfer =
12420a701b1eSRobert Gordon 						    wc.wc_bytes_xfer;
12437c478bd9Sstevel@tonic-gate 						cv_signal(&r->wait_cv);
12447c478bd9Sstevel@tonic-gate 						break;
12457c478bd9Sstevel@tonic-gate 					default:
12460a701b1eSRobert Gordon 						rib_rbuf_free(qptoc(qp),
12470a701b1eSRobert Gordon 						    RECV_BUFFER,
12480a701b1eSRobert Gordon 						    (void *)(uintptr_t)
12490a701b1eSRobert Gordon 						    rwid->addr);
12507c478bd9Sstevel@tonic-gate 						break;
12517c478bd9Sstevel@tonic-gate 					}
12527c478bd9Sstevel@tonic-gate 					break;
12537c478bd9Sstevel@tonic-gate 				}
12547c478bd9Sstevel@tonic-gate 			}
12557c478bd9Sstevel@tonic-gate 			mutex_exit(&qp->replylist_lock);
12567c478bd9Sstevel@tonic-gate 			if (find_xid == 0) {
12577c478bd9Sstevel@tonic-gate 				/* RPC caller not waiting for reply */
12580a701b1eSRobert Gordon 
12590a701b1eSRobert Gordon 				DTRACE_PROBE1(rpcib__i__nomatchxid1,
12600a701b1eSRobert Gordon 				    int, xid);
12610a701b1eSRobert Gordon 
12627c478bd9Sstevel@tonic-gate 				rib_rbuf_free(qptoc(qp), RECV_BUFFER,
126311606941Sjwahlig 				    (void *)(uintptr_t)rwid->addr);
12647c478bd9Sstevel@tonic-gate 			}
12657c478bd9Sstevel@tonic-gate 		} else if (wc.wc_status == IBT_WC_WR_FLUSHED_ERR) {
12667c478bd9Sstevel@tonic-gate 			CONN *conn = qptoc(qp);
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 			/*
12697c478bd9Sstevel@tonic-gate 			 * Connection being flushed. Just free
12707c478bd9Sstevel@tonic-gate 			 * the posted buffer
12717c478bd9Sstevel@tonic-gate 			 */
127211606941Sjwahlig 			rib_rbuf_free(conn, RECV_BUFFER,
127311606941Sjwahlig 			    (void *)(uintptr_t)rwid->addr);
12747c478bd9Sstevel@tonic-gate 		} else {
12757c478bd9Sstevel@tonic-gate 			CONN *conn = qptoc(qp);
12767c478bd9Sstevel@tonic-gate /*
12777c478bd9Sstevel@tonic-gate  *  RC Recv Q Error Code		Local state     Remote State
12787c478bd9Sstevel@tonic-gate  *  ====================		===========     ============
12797c478bd9Sstevel@tonic-gate  *  IBT_WC_LOCAL_ACCESS_ERR             ERROR           ERROR when NAK recvd
12807c478bd9Sstevel@tonic-gate  *  IBT_WC_LOCAL_LEN_ERR                ERROR           ERROR when NAK recvd
12817c478bd9Sstevel@tonic-gate  *  IBT_WC_LOCAL_PROTECT_ERR            ERROR           ERROR when NAK recvd
12827c478bd9Sstevel@tonic-gate  *  IBT_WC_LOCAL_CHAN_OP_ERR            ERROR           ERROR when NAK recvd
12837c478bd9Sstevel@tonic-gate  *  IBT_WC_REMOTE_INVALID_REQ_ERR       ERROR           ERROR when NAK recvd
12847c478bd9Sstevel@tonic-gate  *  IBT_WC_WR_FLUSHED_ERR               None            None
12857c478bd9Sstevel@tonic-gate  */
12867c478bd9Sstevel@tonic-gate 			/*
12877c478bd9Sstevel@tonic-gate 			 * Channel in error state. Set connection
12887c478bd9Sstevel@tonic-gate 			 * in ERROR state.
12897c478bd9Sstevel@tonic-gate 			 */
12907c478bd9Sstevel@tonic-gate 			mutex_enter(&conn->c_lock);
12917c478bd9Sstevel@tonic-gate 			if (conn->c_state != C_DISCONN_PEND)
12920a701b1eSRobert Gordon 				conn->c_state = C_ERROR_CONN;
12937c478bd9Sstevel@tonic-gate 			mutex_exit(&conn->c_lock);
129411606941Sjwahlig 			rib_rbuf_free(conn, RECV_BUFFER,
129511606941Sjwahlig 			    (void *)(uintptr_t)rwid->addr);
12967c478bd9Sstevel@tonic-gate 		}
12977c478bd9Sstevel@tonic-gate 		rib_free_wid(rwid);
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate /* Server side */
13027c478bd9Sstevel@tonic-gate /* ARGSUSED */
13037c478bd9Sstevel@tonic-gate static void
13047c478bd9Sstevel@tonic-gate rib_svc_rcq_handler(ibt_cq_hdl_t cq_hdl, void *arg)
13057c478bd9Sstevel@tonic-gate {
13060a701b1eSRobert Gordon 	rdma_recv_data_t *rdp;
13077c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp;
13087c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
13097c478bd9Sstevel@tonic-gate 	ibt_wc_t	wc;
13107c478bd9Sstevel@tonic-gate 	struct svc_recv	*s_recvp;
13117c478bd9Sstevel@tonic-gate 	CONN		*conn;
13127c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	/*
13157c478bd9Sstevel@tonic-gate 	 * Re-enable cq notify here to avoid missing any
13167c478bd9Sstevel@tonic-gate 	 * completion queue notification.
13177c478bd9Sstevel@tonic-gate 	 */
13187c478bd9Sstevel@tonic-gate 	(void) ibt_enable_cq_notify(cq_hdl, IBT_NEXT_COMPLETION);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	ibt_status = IBT_SUCCESS;
13217c478bd9Sstevel@tonic-gate 	while (ibt_status != IBT_CQ_EMPTY) {
13227c478bd9Sstevel@tonic-gate 		bzero(&wc, sizeof (wc));
13237c478bd9Sstevel@tonic-gate 		ibt_status = ibt_poll_cq(cq_hdl, &wc, 1, NULL);
13247c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS)
13257c478bd9Sstevel@tonic-gate 			return;
13267c478bd9Sstevel@tonic-gate 
132711606941Sjwahlig 		s_recvp = (struct svc_recv *)(uintptr_t)wc.wc_id;
13287c478bd9Sstevel@tonic-gate 		qp = s_recvp->qp;
13297c478bd9Sstevel@tonic-gate 		conn = qptoc(qp);
13307c478bd9Sstevel@tonic-gate 		mutex_enter(&qp->posted_rbufs_lock);
13317c478bd9Sstevel@tonic-gate 		qp->n_posted_rbufs--;
13327c478bd9Sstevel@tonic-gate 		if (qp->n_posted_rbufs == 0)
13337c478bd9Sstevel@tonic-gate 			cv_signal(&qp->posted_rbufs_cv);
13347c478bd9Sstevel@tonic-gate 		mutex_exit(&qp->posted_rbufs_lock);
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 		if (wc.wc_status == IBT_WC_SUCCESS) {
13377c478bd9Sstevel@tonic-gate 			XDR	inxdrs, *xdrs;
13387c478bd9Sstevel@tonic-gate 			uint_t	xid, vers, op;
13390a701b1eSRobert Gordon 			uint32_t rdma_credit;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 			xdrs = &inxdrs;
13427c478bd9Sstevel@tonic-gate 			/* s_recvp->vaddr stores data */
134311606941Sjwahlig 			xdrmem_create(xdrs, (caddr_t)(uintptr_t)s_recvp->vaddr,
13447c478bd9Sstevel@tonic-gate 			    wc.wc_bytes_xfer, XDR_DECODE);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 			/*
13477c478bd9Sstevel@tonic-gate 			 * Treat xid as opaque (xid is the first entity
13487c478bd9Sstevel@tonic-gate 			 * in the rpc rdma message).
13497c478bd9Sstevel@tonic-gate 			 */
135011606941Sjwahlig 			xid = *(uint32_t *)(uintptr_t)s_recvp->vaddr;
13517c478bd9Sstevel@tonic-gate 			/* Skip xid and set the xdr position accordingly. */
13527c478bd9Sstevel@tonic-gate 			XDR_SETPOS(xdrs, sizeof (uint32_t));
13537c478bd9Sstevel@tonic-gate 			if (!xdr_u_int(xdrs, &vers) ||
13540a701b1eSRobert Gordon 			    !xdr_u_int(xdrs, &rdma_credit) ||
13557c478bd9Sstevel@tonic-gate 			    !xdr_u_int(xdrs, &op)) {
13567c478bd9Sstevel@tonic-gate 				rib_rbuf_free(conn, RECV_BUFFER,
135711606941Sjwahlig 				    (void *)(uintptr_t)s_recvp->vaddr);
13587c478bd9Sstevel@tonic-gate 				XDR_DESTROY(xdrs);
13597c478bd9Sstevel@tonic-gate 				(void) rib_free_svc_recv(s_recvp);
13607c478bd9Sstevel@tonic-gate 				continue;
13617c478bd9Sstevel@tonic-gate 			}
13627c478bd9Sstevel@tonic-gate 			XDR_DESTROY(xdrs);
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 			if (vers != RPCRDMA_VERS) {
13657c478bd9Sstevel@tonic-gate 				/*
13660a701b1eSRobert Gordon 				 * Invalid RPC/RDMA version.
13670a701b1eSRobert Gordon 				 * Drop rpc rdma message.
13687c478bd9Sstevel@tonic-gate 				 */
13697c478bd9Sstevel@tonic-gate 				rib_rbuf_free(conn, RECV_BUFFER,
137011606941Sjwahlig 				    (void *)(uintptr_t)s_recvp->vaddr);
13717c478bd9Sstevel@tonic-gate 				(void) rib_free_svc_recv(s_recvp);
13727c478bd9Sstevel@tonic-gate 				continue;
13737c478bd9Sstevel@tonic-gate 			}
13747c478bd9Sstevel@tonic-gate 			/*
13757c478bd9Sstevel@tonic-gate 			 * Is this for RDMA_DONE?
13767c478bd9Sstevel@tonic-gate 			 */
13777c478bd9Sstevel@tonic-gate 			if (op == RDMA_DONE) {
13787c478bd9Sstevel@tonic-gate 				rib_rbuf_free(conn, RECV_BUFFER,
137911606941Sjwahlig 				    (void *)(uintptr_t)s_recvp->vaddr);
13807c478bd9Sstevel@tonic-gate 				/*
13817c478bd9Sstevel@tonic-gate 				 * Wake up the thread waiting on
13827c478bd9Sstevel@tonic-gate 				 * a RDMA_DONE for xid
13837c478bd9Sstevel@tonic-gate 				 */
13847c478bd9Sstevel@tonic-gate 				mutex_enter(&qp->rdlist_lock);
13857c478bd9Sstevel@tonic-gate 				rdma_done_notify(qp, xid);
13867c478bd9Sstevel@tonic-gate 				mutex_exit(&qp->rdlist_lock);
13877c478bd9Sstevel@tonic-gate 				(void) rib_free_svc_recv(s_recvp);
13887c478bd9Sstevel@tonic-gate 				continue;
13897c478bd9Sstevel@tonic-gate 			}
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 			mutex_enter(&plugin_state_lock);
13927c478bd9Sstevel@tonic-gate 			if (plugin_state == ACCEPT) {
13930a701b1eSRobert Gordon 				while ((mp = allocb(sizeof (*rdp), BPRI_LO))
13940a701b1eSRobert Gordon 				    == NULL)
13950a701b1eSRobert Gordon 					(void) strwaitbuf(
13960a701b1eSRobert Gordon 					    sizeof (*rdp), BPRI_LO);
13977c478bd9Sstevel@tonic-gate 				/*
13987c478bd9Sstevel@tonic-gate 				 * Plugin is in accept state, hence the master
13997c478bd9Sstevel@tonic-gate 				 * transport queue for this is still accepting
14007c478bd9Sstevel@tonic-gate 				 * requests. Hence we can call svc_queuereq to
14017c478bd9Sstevel@tonic-gate 				 * queue this recieved msg.
14027c478bd9Sstevel@tonic-gate 				 */
14030a701b1eSRobert Gordon 				rdp = (rdma_recv_data_t *)mp->b_rptr;
14040a701b1eSRobert Gordon 				rdp->conn = conn;
14050a701b1eSRobert Gordon 				rdp->rpcmsg.addr =
14060a701b1eSRobert Gordon 				    (caddr_t)(uintptr_t)s_recvp->vaddr;
14070a701b1eSRobert Gordon 				rdp->rpcmsg.type = RECV_BUFFER;
14080a701b1eSRobert Gordon 				rdp->rpcmsg.len = wc.wc_bytes_xfer;
14090a701b1eSRobert Gordon 				rdp->status = wc.wc_status;
14107c478bd9Sstevel@tonic-gate 				mutex_enter(&conn->c_lock);
14117c478bd9Sstevel@tonic-gate 				conn->c_ref++;
14127c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
14130a701b1eSRobert Gordon 				mp->b_wptr += sizeof (*rdp);
14147c478bd9Sstevel@tonic-gate 				svc_queuereq((queue_t *)rib_stat->q, mp);
14157c478bd9Sstevel@tonic-gate 				mutex_exit(&plugin_state_lock);
14167c478bd9Sstevel@tonic-gate 			} else {
14177c478bd9Sstevel@tonic-gate 				/*
14187c478bd9Sstevel@tonic-gate 				 * The master transport for this is going
14197c478bd9Sstevel@tonic-gate 				 * away and the queue is not accepting anymore
14207c478bd9Sstevel@tonic-gate 				 * requests for krpc, so don't do anything, just
14217c478bd9Sstevel@tonic-gate 				 * free the msg.
14227c478bd9Sstevel@tonic-gate 				 */
14237c478bd9Sstevel@tonic-gate 				mutex_exit(&plugin_state_lock);
14247c478bd9Sstevel@tonic-gate 				rib_rbuf_free(conn, RECV_BUFFER,
142511606941Sjwahlig 				    (void *)(uintptr_t)s_recvp->vaddr);
14267c478bd9Sstevel@tonic-gate 			}
14277c478bd9Sstevel@tonic-gate 		} else {
14287c478bd9Sstevel@tonic-gate 			rib_rbuf_free(conn, RECV_BUFFER,
142911606941Sjwahlig 			    (void *)(uintptr_t)s_recvp->vaddr);
14307c478bd9Sstevel@tonic-gate 		}
14317c478bd9Sstevel@tonic-gate 		(void) rib_free_svc_recv(s_recvp);
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate 
1435*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static void
1436*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rib_attach_hca()
1437*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States {
1438*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_enter(&rib_stat->open_hca_lock);
1439*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rpcib_open_hcas(rib_stat);
1440*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_listen(NULL);
1441*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_exit(&rib_stat->open_hca_lock);
1442*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States }
1443*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
14447c478bd9Sstevel@tonic-gate /*
14457c478bd9Sstevel@tonic-gate  * Handles DR event of IBT_HCA_DETACH_EVENT.
14467c478bd9Sstevel@tonic-gate  */
14477c478bd9Sstevel@tonic-gate /* ARGSUSED */
14487c478bd9Sstevel@tonic-gate static void
14497c478bd9Sstevel@tonic-gate rib_async_handler(void *clnt_private, ibt_hca_hdl_t hca_hdl,
14507c478bd9Sstevel@tonic-gate 	ibt_async_code_t code, ibt_async_event_t *event)
14517c478bd9Sstevel@tonic-gate {
14527c478bd9Sstevel@tonic-gate 	switch (code) {
14537c478bd9Sstevel@tonic-gate 	case IBT_HCA_ATTACH_EVENT:
1454*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_attach_hca();
14557c478bd9Sstevel@tonic-gate 		break;
14567c478bd9Sstevel@tonic-gate 	case IBT_HCA_DETACH_EVENT:
14577c478bd9Sstevel@tonic-gate 	{
1458*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_hca_t *hca;
1459*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
1460*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_enter(&rib_stat->hcas_list_lock, RW_READER);
1461*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		for (hca = rib_stat->hcas_list; hca; hca = hca->next) {
1462*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_enter(&hca->state_lock, RW_READER);
1463*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			if ((hca->state != HCA_DETACHED) &&
1464*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    (hca->hca_hdl == hca_hdl)) {
1465*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				rw_exit(&hca->state_lock);
1466*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				break;
1467*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			}
1468*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&hca->state_lock);
1469*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
1470*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_exit(&rib_stat->hcas_list_lock);
1471*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
1472*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (hca == NULL)
1473*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			return;
1474*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		ASSERT(hca->hca_hdl == hca_hdl);
1475*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_detach_hca(hca);
14767c478bd9Sstevel@tonic-gate #ifdef DEBUG
14777c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): HCA being detached!\n");
14787c478bd9Sstevel@tonic-gate #endif
14797c478bd9Sstevel@tonic-gate 		break;
14807c478bd9Sstevel@tonic-gate 	}
1481*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	case IBT_EVENT_PORT_UP:
1482*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		/*
1483*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * A port is up. We should call rib_listen() since there is
1484*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * a chance that rib_listen() may have failed during
1485*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * rib_attach_hca() because the port had not been up yet.
1486*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 */
1487*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_listen(NULL);
1488*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States #ifdef DEBUG
1489*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_EVENT_PORT_UP\n");
1490*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States #endif
1491*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		break;
14927c478bd9Sstevel@tonic-gate #ifdef DEBUG
14937c478bd9Sstevel@tonic-gate 	case IBT_EVENT_PATH_MIGRATED:
14940a701b1eSRobert Gordon 		cmn_err(CE_NOTE, "rib_async_handler(): "
14950a701b1eSRobert Gordon 		    "IBT_EVENT_PATH_MIGRATED\n");
14967c478bd9Sstevel@tonic-gate 		break;
14977c478bd9Sstevel@tonic-gate 	case IBT_EVENT_SQD:
14987c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_EVENT_SQD\n");
14997c478bd9Sstevel@tonic-gate 		break;
15007c478bd9Sstevel@tonic-gate 	case IBT_EVENT_COM_EST:
15017c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_EVENT_COM_EST\n");
15027c478bd9Sstevel@tonic-gate 		break;
15037c478bd9Sstevel@tonic-gate 	case IBT_ERROR_CATASTROPHIC_CHAN:
15040a701b1eSRobert Gordon 		cmn_err(CE_NOTE, "rib_async_handler(): "
15050a701b1eSRobert Gordon 		    "IBT_ERROR_CATASTROPHIC_CHAN\n");
15067c478bd9Sstevel@tonic-gate 		break;
15077c478bd9Sstevel@tonic-gate 	case IBT_ERROR_INVALID_REQUEST_CHAN:
15087c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): "
15097c478bd9Sstevel@tonic-gate 		    "IBT_ERROR_INVALID_REQUEST_CHAN\n");
15107c478bd9Sstevel@tonic-gate 		break;
15117c478bd9Sstevel@tonic-gate 	case IBT_ERROR_ACCESS_VIOLATION_CHAN:
15127c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): "
15137c478bd9Sstevel@tonic-gate 		    "IBT_ERROR_ACCESS_VIOLATION_CHAN\n");
15147c478bd9Sstevel@tonic-gate 		break;
15157c478bd9Sstevel@tonic-gate 	case IBT_ERROR_PATH_MIGRATE_REQ:
15160a701b1eSRobert Gordon 		cmn_err(CE_NOTE, "rib_async_handler(): "
15170a701b1eSRobert Gordon 		    "IBT_ERROR_PATH_MIGRATE_REQ\n");
15187c478bd9Sstevel@tonic-gate 		break;
15197c478bd9Sstevel@tonic-gate 	case IBT_ERROR_CQ:
15207c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_ERROR_CQ\n");
15217c478bd9Sstevel@tonic-gate 		break;
15227c478bd9Sstevel@tonic-gate 	case IBT_ERROR_PORT_DOWN:
15237c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_ERROR_PORT_DOWN\n");
15247c478bd9Sstevel@tonic-gate 		break;
15257c478bd9Sstevel@tonic-gate 	case IBT_ASYNC_OPAQUE1:
15267c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_ASYNC_OPAQUE1\n");
15277c478bd9Sstevel@tonic-gate 		break;
15287c478bd9Sstevel@tonic-gate 	case IBT_ASYNC_OPAQUE2:
15297c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_ASYNC_OPAQUE2\n");
15307c478bd9Sstevel@tonic-gate 		break;
15317c478bd9Sstevel@tonic-gate 	case IBT_ASYNC_OPAQUE3:
15327c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_ASYNC_OPAQUE3\n");
15337c478bd9Sstevel@tonic-gate 		break;
15347c478bd9Sstevel@tonic-gate 	case IBT_ASYNC_OPAQUE4:
15357c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "rib_async_handler(): IBT_ASYNC_OPAQUE4\n");
15367c478bd9Sstevel@tonic-gate 		break;
15377c478bd9Sstevel@tonic-gate #endif
15387c478bd9Sstevel@tonic-gate 	default:
15397c478bd9Sstevel@tonic-gate 		break;
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate }
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate /*
15447c478bd9Sstevel@tonic-gate  * Client's reachable function.
15457c478bd9Sstevel@tonic-gate  */
15467c478bd9Sstevel@tonic-gate static rdma_stat
15477c478bd9Sstevel@tonic-gate rib_reachable(int addr_type, struct netbuf *raddr, void **handle)
15487c478bd9Sstevel@tonic-gate {
15497c478bd9Sstevel@tonic-gate 	rdma_stat	status;
1550214ae7d0SSiddheshwar Mahesh 	rpcib_ping_t	rpt;
15517c478bd9Sstevel@tonic-gate 
1552214ae7d0SSiddheshwar Mahesh 	bzero(&rpt, sizeof (rpcib_ping_t));
1553214ae7d0SSiddheshwar Mahesh 	status = rib_ping_srv(addr_type, raddr, &rpt);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	if (status == RDMA_SUCCESS) {
1556214ae7d0SSiddheshwar Mahesh 		*handle = (void *)rpt.hca;
15577c478bd9Sstevel@tonic-gate 		return (RDMA_SUCCESS);
15587c478bd9Sstevel@tonic-gate 	} else {
15597c478bd9Sstevel@tonic-gate 		*handle = NULL;
15600a701b1eSRobert Gordon 		DTRACE_PROBE(rpcib__i__pingfailed);
15617c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate /* Client side qp creation */
15667c478bd9Sstevel@tonic-gate static rdma_stat
15677c478bd9Sstevel@tonic-gate rib_clnt_create_chan(rib_hca_t *hca, struct netbuf *raddr, rib_qp_t **qp)
15687c478bd9Sstevel@tonic-gate {
15697c478bd9Sstevel@tonic-gate 	rib_qp_t	*kqp = NULL;
15707c478bd9Sstevel@tonic-gate 	CONN		*conn;
15710a701b1eSRobert Gordon 	rdma_clnt_cred_ctrl_t *cc_info;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	ASSERT(qp != NULL);
15747c478bd9Sstevel@tonic-gate 	*qp = NULL;
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	kqp = kmem_zalloc(sizeof (rib_qp_t), KM_SLEEP);
15777c478bd9Sstevel@tonic-gate 	conn = qptoc(kqp);
15787c478bd9Sstevel@tonic-gate 	kqp->hca = hca;
15797c478bd9Sstevel@tonic-gate 	kqp->rdmaconn.c_rdmamod = &rib_mod;
15807c478bd9Sstevel@tonic-gate 	kqp->rdmaconn.c_private = (caddr_t)kqp;
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	kqp->mode = RIB_CLIENT;
15837c478bd9Sstevel@tonic-gate 	kqp->chan_flags = IBT_BLOCKING;
15847c478bd9Sstevel@tonic-gate 	conn->c_raddr.buf = kmem_alloc(raddr->len, KM_SLEEP);
15857c478bd9Sstevel@tonic-gate 	bcopy(raddr->buf, conn->c_raddr.buf, raddr->len);
15867c478bd9Sstevel@tonic-gate 	conn->c_raddr.len = conn->c_raddr.maxlen = raddr->len;
15877c478bd9Sstevel@tonic-gate 	/*
15887c478bd9Sstevel@tonic-gate 	 * Initialize
15897c478bd9Sstevel@tonic-gate 	 */
15907c478bd9Sstevel@tonic-gate 	cv_init(&kqp->cb_conn_cv, NULL, CV_DEFAULT, NULL);
15917c478bd9Sstevel@tonic-gate 	cv_init(&kqp->posted_rbufs_cv, NULL, CV_DEFAULT, NULL);
15927c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->posted_rbufs_lock, NULL, MUTEX_DRIVER, hca->iblock);
1593065714dcSSiddheshwar Mahesh 	cv_init(&kqp->send_rbufs_cv, NULL, CV_DEFAULT, NULL);
1594065714dcSSiddheshwar Mahesh 	mutex_init(&kqp->send_rbufs_lock, NULL, MUTEX_DRIVER, hca->iblock);
15957c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->replylist_lock, NULL, MUTEX_DRIVER, hca->iblock);
15967c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->rdlist_lock, NULL, MUTEX_DEFAULT, hca->iblock);
15977c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->cb_lock, NULL, MUTEX_DRIVER, hca->iblock);
15987c478bd9Sstevel@tonic-gate 	cv_init(&kqp->rdmaconn.c_cv, NULL, CV_DEFAULT, NULL);
15997c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->rdmaconn.c_lock, NULL, MUTEX_DRIVER, hca->iblock);
16000a701b1eSRobert Gordon 	/*
16010a701b1eSRobert Gordon 	 * Initialize the client credit control
16020a701b1eSRobert Gordon 	 * portion of the rdmaconn struct.
16030a701b1eSRobert Gordon 	 */
16040a701b1eSRobert Gordon 	kqp->rdmaconn.c_cc_type = RDMA_CC_CLNT;
16050a701b1eSRobert Gordon 	cc_info = &kqp->rdmaconn.rdma_conn_cred_ctrl_u.c_clnt_cc;
16060a701b1eSRobert Gordon 	cc_info->clnt_cc_granted_ops = 0;
16070a701b1eSRobert Gordon 	cc_info->clnt_cc_in_flight_ops = 0;
16080a701b1eSRobert Gordon 	cv_init(&cc_info->clnt_cc_cv, NULL, CV_DEFAULT, NULL);
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	*qp = kqp;
16117c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate /* Server side qp creation */
16157c478bd9Sstevel@tonic-gate static rdma_stat
16167c478bd9Sstevel@tonic-gate rib_svc_create_chan(rib_hca_t *hca, caddr_t q, uint8_t port, rib_qp_t **qp)
16177c478bd9Sstevel@tonic-gate {
16187c478bd9Sstevel@tonic-gate 	rib_qp_t	*kqp = NULL;
16197c478bd9Sstevel@tonic-gate 	ibt_chan_sizes_t	chan_sizes;
16207c478bd9Sstevel@tonic-gate 	ibt_rc_chan_alloc_args_t	qp_attr;
16217c478bd9Sstevel@tonic-gate 	ibt_status_t		ibt_status;
16220a701b1eSRobert Gordon 	rdma_srv_cred_ctrl_t *cc_info;
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	*qp = NULL;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	kqp = kmem_zalloc(sizeof (rib_qp_t), KM_SLEEP);
16277c478bd9Sstevel@tonic-gate 	kqp->hca = hca;
16287c478bd9Sstevel@tonic-gate 	kqp->port_num = port;
16297c478bd9Sstevel@tonic-gate 	kqp->rdmaconn.c_rdmamod = &rib_mod;
16307c478bd9Sstevel@tonic-gate 	kqp->rdmaconn.c_private = (caddr_t)kqp;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	/*
16337c478bd9Sstevel@tonic-gate 	 * Create the qp handle
16347c478bd9Sstevel@tonic-gate 	 */
16357c478bd9Sstevel@tonic-gate 	bzero(&qp_attr, sizeof (ibt_rc_chan_alloc_args_t));
16367c478bd9Sstevel@tonic-gate 	qp_attr.rc_scq = hca->svc_scq->rib_cq_hdl;
16377c478bd9Sstevel@tonic-gate 	qp_attr.rc_rcq = hca->svc_rcq->rib_cq_hdl;
16387c478bd9Sstevel@tonic-gate 	qp_attr.rc_pd = hca->pd_hdl;
16397c478bd9Sstevel@tonic-gate 	qp_attr.rc_hca_port_num = port;
16407c478bd9Sstevel@tonic-gate 	qp_attr.rc_sizes.cs_sq_sgl = DSEG_MAX;
16417c478bd9Sstevel@tonic-gate 	qp_attr.rc_sizes.cs_rq_sgl = RQ_DSEG_MAX;
16427c478bd9Sstevel@tonic-gate 	qp_attr.rc_sizes.cs_sq = DEF_SQ_SIZE;
16437c478bd9Sstevel@tonic-gate 	qp_attr.rc_sizes.cs_rq = DEF_RQ_SIZE;
16447c478bd9Sstevel@tonic-gate 	qp_attr.rc_clone_chan = NULL;
16457c478bd9Sstevel@tonic-gate 	qp_attr.rc_control = IBT_CEP_RDMA_RD | IBT_CEP_RDMA_WR;
16467c478bd9Sstevel@tonic-gate 	qp_attr.rc_flags = IBT_WR_SIGNALED;
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	rw_enter(&hca->state_lock, RW_READER);
16497c478bd9Sstevel@tonic-gate 	if (hca->state != HCA_DETACHED) {
16507c478bd9Sstevel@tonic-gate 		ibt_status = ibt_alloc_rc_channel(hca->hca_hdl,
16517c478bd9Sstevel@tonic-gate 		    IBT_ACHAN_NO_FLAGS, &qp_attr, &kqp->qp_hdl,
16527c478bd9Sstevel@tonic-gate 		    &chan_sizes);
16537c478bd9Sstevel@tonic-gate 	} else {
16547c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
16557c478bd9Sstevel@tonic-gate 		goto fail;
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 	rw_exit(&hca->state_lock);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	if (ibt_status != IBT_SUCCESS) {
16600a701b1eSRobert Gordon 		DTRACE_PROBE1(rpcib__i_svccreatechanfail,
16610a701b1eSRobert Gordon 		    int, ibt_status);
16627c478bd9Sstevel@tonic-gate 		goto fail;
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	kqp->mode = RIB_SERVER;
16667c478bd9Sstevel@tonic-gate 	kqp->chan_flags = IBT_BLOCKING;
16677c478bd9Sstevel@tonic-gate 	kqp->q = q;	/* server ONLY */
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	cv_init(&kqp->cb_conn_cv, NULL, CV_DEFAULT, NULL);
16707c478bd9Sstevel@tonic-gate 	cv_init(&kqp->posted_rbufs_cv, NULL, CV_DEFAULT, NULL);
16717c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->replylist_lock, NULL, MUTEX_DEFAULT, hca->iblock);
16727c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->posted_rbufs_lock, NULL, MUTEX_DRIVER, hca->iblock);
1673065714dcSSiddheshwar Mahesh 	cv_init(&kqp->send_rbufs_cv, NULL, CV_DEFAULT, NULL);
1674065714dcSSiddheshwar Mahesh 	mutex_init(&kqp->send_rbufs_lock, NULL, MUTEX_DRIVER, hca->iblock);
16757c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->rdlist_lock, NULL, MUTEX_DEFAULT, hca->iblock);
16767c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->cb_lock, NULL, MUTEX_DRIVER, hca->iblock);
16777c478bd9Sstevel@tonic-gate 	cv_init(&kqp->rdmaconn.c_cv, NULL, CV_DEFAULT, NULL);
16787c478bd9Sstevel@tonic-gate 	mutex_init(&kqp->rdmaconn.c_lock, NULL, MUTEX_DRIVER, hca->iblock);
16797c478bd9Sstevel@tonic-gate 	/*
16807c478bd9Sstevel@tonic-gate 	 * Set the private data area to qp to be used in callbacks
16817c478bd9Sstevel@tonic-gate 	 */
16827c478bd9Sstevel@tonic-gate 	ibt_set_chan_private(kqp->qp_hdl, (void *)kqp);
16837c478bd9Sstevel@tonic-gate 	kqp->rdmaconn.c_state = C_CONNECTED;
16840a701b1eSRobert Gordon 
16850a701b1eSRobert Gordon 	/*
16860a701b1eSRobert Gordon 	 * Initialize the server credit control
16870a701b1eSRobert Gordon 	 * portion of the rdmaconn struct.
16880a701b1eSRobert Gordon 	 */
16890a701b1eSRobert Gordon 	kqp->rdmaconn.c_cc_type = RDMA_CC_SRV;
16900a701b1eSRobert Gordon 	cc_info = &kqp->rdmaconn.rdma_conn_cred_ctrl_u.c_srv_cc;
16910a701b1eSRobert Gordon 	cc_info->srv_cc_buffers_granted = preposted_rbufs;
16920a701b1eSRobert Gordon 	cc_info->srv_cc_cur_buffers_used = 0;
16930a701b1eSRobert Gordon 	cc_info->srv_cc_posted = preposted_rbufs;
16940a701b1eSRobert Gordon 
16957c478bd9Sstevel@tonic-gate 	*qp = kqp;
16960a701b1eSRobert Gordon 
16977c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
16987c478bd9Sstevel@tonic-gate fail:
16997c478bd9Sstevel@tonic-gate 	if (kqp)
17007c478bd9Sstevel@tonic-gate 		kmem_free(kqp, sizeof (rib_qp_t));
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	return (RDMA_FAILED);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate /* ARGSUSED */
17067c478bd9Sstevel@tonic-gate ibt_cm_status_t
17077c478bd9Sstevel@tonic-gate rib_clnt_cm_handler(void *clnt_hdl, ibt_cm_event_t *event,
17087c478bd9Sstevel@tonic-gate     ibt_cm_return_args_t *ret_args, void *priv_data,
17097c478bd9Sstevel@tonic-gate     ibt_priv_data_len_t len)
17107c478bd9Sstevel@tonic-gate {
17117c478bd9Sstevel@tonic-gate 	rib_hca_t	*hca;
17127c478bd9Sstevel@tonic-gate 
1713*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	hca = (rib_hca_t *)clnt_hdl;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	switch (event->cm_type) {
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	/* got a connection close event */
17187c478bd9Sstevel@tonic-gate 	case IBT_CM_EVENT_CONN_CLOSED:
17197c478bd9Sstevel@tonic-gate 	{
17207c478bd9Sstevel@tonic-gate 		CONN	*conn;
17217c478bd9Sstevel@tonic-gate 		rib_qp_t *qp;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		/* check reason why connection was closed */
17247c478bd9Sstevel@tonic-gate 		switch (event->cm_event.closed) {
17257c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_DREP_RCVD:
17267c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_DREQ_TIMEOUT:
17277c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_DUP:
17287c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_ABORT:
17297c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_ALREADY:
17307c478bd9Sstevel@tonic-gate 			/*
17317c478bd9Sstevel@tonic-gate 			 * These cases indicate the local end initiated
17327c478bd9Sstevel@tonic-gate 			 * the closing of the channel. Nothing to do here.
17337c478bd9Sstevel@tonic-gate 			 */
17347c478bd9Sstevel@tonic-gate 			break;
17357c478bd9Sstevel@tonic-gate 		default:
17367c478bd9Sstevel@tonic-gate 			/*
17377c478bd9Sstevel@tonic-gate 			 * Reason for CONN_CLOSED event must be one of
17387c478bd9Sstevel@tonic-gate 			 * IBT_CM_CLOSED_DREQ_RCVD or IBT_CM_CLOSED_REJ_RCVD
17397c478bd9Sstevel@tonic-gate 			 * or IBT_CM_CLOSED_STALE. These indicate cases were
17407c478bd9Sstevel@tonic-gate 			 * the remote end is closing the channel. In these
17417c478bd9Sstevel@tonic-gate 			 * cases free the channel and transition to error
17427c478bd9Sstevel@tonic-gate 			 * state
17437c478bd9Sstevel@tonic-gate 			 */
17447c478bd9Sstevel@tonic-gate 			qp = ibt_get_chan_private(event->cm_channel);
17457c478bd9Sstevel@tonic-gate 			conn = qptoc(qp);
17467c478bd9Sstevel@tonic-gate 			mutex_enter(&conn->c_lock);
17477c478bd9Sstevel@tonic-gate 			if (conn->c_state == C_DISCONN_PEND) {
17487c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
17497c478bd9Sstevel@tonic-gate 				break;
17507c478bd9Sstevel@tonic-gate 			}
17517c478bd9Sstevel@tonic-gate 
17520a701b1eSRobert Gordon 			conn->c_state = C_ERROR_CONN;
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 			/*
17557c478bd9Sstevel@tonic-gate 			 * Free the conn if c_ref is down to 0 already
17567c478bd9Sstevel@tonic-gate 			 */
17577c478bd9Sstevel@tonic-gate 			if (conn->c_ref == 0) {
17587c478bd9Sstevel@tonic-gate 				/*
17597c478bd9Sstevel@tonic-gate 				 * Remove from list and free conn
17607c478bd9Sstevel@tonic-gate 				 */
17617c478bd9Sstevel@tonic-gate 				conn->c_state = C_DISCONN_PEND;
17627c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
1763*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				rw_enter(&hca->state_lock, RW_READER);
1764*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				if (hca->state != HCA_DETACHED)
17657c478bd9Sstevel@tonic-gate 					(void) rib_disconnect_channel(conn,
17667c478bd9Sstevel@tonic-gate 					    &hca->cl_conn_list);
1767*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				rw_exit(&hca->state_lock);
17687c478bd9Sstevel@tonic-gate 			} else {
1769065714dcSSiddheshwar Mahesh 				/*
1770065714dcSSiddheshwar Mahesh 				 * conn will be freed when c_ref goes to 0.
1771065714dcSSiddheshwar Mahesh 				 * Indicate to cleaning thread not to close
1772065714dcSSiddheshwar Mahesh 				 * the connection, but just free the channel.
1773065714dcSSiddheshwar Mahesh 				 */
1774065714dcSSiddheshwar Mahesh 				conn->c_flags |= C_CLOSE_NOTNEEDED;
17757c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
17767c478bd9Sstevel@tonic-gate 			}
17777c478bd9Sstevel@tonic-gate #ifdef DEBUG
17787c478bd9Sstevel@tonic-gate 			if (rib_debug)
17797c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "rib_clnt_cm_handler: "
17807c478bd9Sstevel@tonic-gate 				    "(CONN_CLOSED) channel disconnected");
17817c478bd9Sstevel@tonic-gate #endif
17827c478bd9Sstevel@tonic-gate 			break;
17837c478bd9Sstevel@tonic-gate 		}
17847c478bd9Sstevel@tonic-gate 		break;
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 	default:
17877c478bd9Sstevel@tonic-gate 		break;
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 	return (IBT_CM_ACCEPT);
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate /*
17937c478bd9Sstevel@tonic-gate  * Connect to the server.
17947c478bd9Sstevel@tonic-gate  */
17957c478bd9Sstevel@tonic-gate rdma_stat
1796214ae7d0SSiddheshwar Mahesh rib_conn_to_srv(rib_hca_t *hca, rib_qp_t *qp, rpcib_ping_t *rptp)
17977c478bd9Sstevel@tonic-gate {
17987c478bd9Sstevel@tonic-gate 	ibt_chan_open_args_t	chan_args;	/* channel args */
17997c478bd9Sstevel@tonic-gate 	ibt_chan_sizes_t	chan_sizes;
18007c478bd9Sstevel@tonic-gate 	ibt_rc_chan_alloc_args_t	qp_attr;
18017c478bd9Sstevel@tonic-gate 	ibt_status_t		ibt_status;
18027c478bd9Sstevel@tonic-gate 	ibt_rc_returns_t	ret_args;   	/* conn reject info */
18037c478bd9Sstevel@tonic-gate 	int refresh = REFRESH_ATTEMPTS;	/* refresh if IBT_CM_CONN_STALE */
18040a701b1eSRobert Gordon 	ibt_ip_cm_info_t	ipcm_info;
18050a701b1eSRobert Gordon 	uint8_t cmp_ip_pvt[IBT_IP_HDR_PRIV_DATA_SZ];
18060a701b1eSRobert Gordon 
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	(void) bzero(&chan_args, sizeof (chan_args));
18097c478bd9Sstevel@tonic-gate 	(void) bzero(&qp_attr, sizeof (ibt_rc_chan_alloc_args_t));
18100a701b1eSRobert Gordon 	(void) bzero(&ipcm_info, sizeof (ibt_ip_cm_info_t));
18110a701b1eSRobert Gordon 
1812214ae7d0SSiddheshwar Mahesh 	ipcm_info.src_addr.family = rptp->srcip.family;
1813214ae7d0SSiddheshwar Mahesh 	switch (ipcm_info.src_addr.family) {
18140a701b1eSRobert Gordon 	case AF_INET:
1815214ae7d0SSiddheshwar Mahesh 		ipcm_info.src_addr.un.ip4addr = rptp->srcip.un.ip4addr;
18160a701b1eSRobert Gordon 		break;
18170a701b1eSRobert Gordon 	case AF_INET6:
1818214ae7d0SSiddheshwar Mahesh 		ipcm_info.src_addr.un.ip6addr = rptp->srcip.un.ip6addr;
18190a701b1eSRobert Gordon 		break;
18200a701b1eSRobert Gordon 	}
18210a701b1eSRobert Gordon 
1822214ae7d0SSiddheshwar Mahesh 	ipcm_info.dst_addr.family = rptp->srcip.family;
1823214ae7d0SSiddheshwar Mahesh 	switch (ipcm_info.dst_addr.family) {
18240a701b1eSRobert Gordon 	case AF_INET:
1825214ae7d0SSiddheshwar Mahesh 		ipcm_info.dst_addr.un.ip4addr = rptp->dstip.un.ip4addr;
18260a701b1eSRobert Gordon 		break;
18270a701b1eSRobert Gordon 	case AF_INET6:
1828214ae7d0SSiddheshwar Mahesh 		ipcm_info.dst_addr.un.ip6addr = rptp->dstip.un.ip6addr;
18290a701b1eSRobert Gordon 		break;
18300a701b1eSRobert Gordon 	}
18310a701b1eSRobert Gordon 
1832f837ee4aSSiddheshwar Mahesh 	ipcm_info.src_port = (in_port_t)nfs_rdma_port;
18330a701b1eSRobert Gordon 
18340a701b1eSRobert Gordon 	ibt_status = ibt_format_ip_private_data(&ipcm_info,
18350a701b1eSRobert Gordon 	    IBT_IP_HDR_PRIV_DATA_SZ, cmp_ip_pvt);
18360a701b1eSRobert Gordon 
18370a701b1eSRobert Gordon 	if (ibt_status != IBT_SUCCESS) {
18380a701b1eSRobert Gordon 		cmn_err(CE_WARN, "ibt_format_ip_private_data failed\n");
18390a701b1eSRobert Gordon 		return (-1);
18400a701b1eSRobert Gordon 	}
18417c478bd9Sstevel@tonic-gate 
1842214ae7d0SSiddheshwar Mahesh 	qp_attr.rc_hca_port_num = rptp->path.pi_prim_cep_path.cep_hca_port_num;
18437c478bd9Sstevel@tonic-gate 	/* Alloc a RC channel */
18447c478bd9Sstevel@tonic-gate 	qp_attr.rc_scq = hca->clnt_scq->rib_cq_hdl;
18457c478bd9Sstevel@tonic-gate 	qp_attr.rc_rcq = hca->clnt_rcq->rib_cq_hdl;
18467c478bd9Sstevel@tonic-gate 	qp_attr.rc_pd = hca->pd_hdl;
18477c478bd9Sstevel@tonic-gate 	qp_attr.rc_sizes.cs_sq_sgl = DSEG_MAX;
18487c478bd9Sstevel@tonic-gate 	qp_attr.rc_sizes.cs_rq_sgl = RQ_DSEG_MAX;
18497c478bd9Sstevel@tonic-gate 	qp_attr.rc_sizes.cs_sq = DEF_SQ_SIZE;
18507c478bd9Sstevel@tonic-gate 	qp_attr.rc_sizes.cs_rq = DEF_RQ_SIZE;
18517c478bd9Sstevel@tonic-gate 	qp_attr.rc_clone_chan = NULL;
18527c478bd9Sstevel@tonic-gate 	qp_attr.rc_control = IBT_CEP_RDMA_RD | IBT_CEP_RDMA_WR;
18537c478bd9Sstevel@tonic-gate 	qp_attr.rc_flags = IBT_WR_SIGNALED;
18547c478bd9Sstevel@tonic-gate 
1855f837ee4aSSiddheshwar Mahesh 	rptp->path.pi_sid = ibt_get_ip_sid(IPPROTO_TCP, nfs_rdma_port);
1856214ae7d0SSiddheshwar Mahesh 	chan_args.oc_path = &rptp->path;
1857f837ee4aSSiddheshwar Mahesh 
18587c478bd9Sstevel@tonic-gate 	chan_args.oc_cm_handler = rib_clnt_cm_handler;
1859*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	chan_args.oc_cm_clnt_private = (void *)hca;
18600a701b1eSRobert Gordon 	chan_args.oc_rdma_ra_out = 4;
18610a701b1eSRobert Gordon 	chan_args.oc_rdma_ra_in = 4;
18627c478bd9Sstevel@tonic-gate 	chan_args.oc_path_retry_cnt = 2;
18637c478bd9Sstevel@tonic-gate 	chan_args.oc_path_rnr_retry_cnt = RNR_RETRIES;
18640a701b1eSRobert Gordon 	chan_args.oc_priv_data = cmp_ip_pvt;
18650a701b1eSRobert Gordon 	chan_args.oc_priv_data_len = IBT_IP_HDR_PRIV_DATA_SZ;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate refresh:
18687c478bd9Sstevel@tonic-gate 	rw_enter(&hca->state_lock, RW_READER);
18697c478bd9Sstevel@tonic-gate 	if (hca->state != HCA_DETACHED) {
18707c478bd9Sstevel@tonic-gate 		ibt_status = ibt_alloc_rc_channel(hca->hca_hdl,
18710a701b1eSRobert Gordon 		    IBT_ACHAN_NO_FLAGS,
18720a701b1eSRobert Gordon 		    &qp_attr, &qp->qp_hdl,
18737c478bd9Sstevel@tonic-gate 		    &chan_sizes);
18747c478bd9Sstevel@tonic-gate 	} else {
18757c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
18767c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
18777c478bd9Sstevel@tonic-gate 	}
18787c478bd9Sstevel@tonic-gate 	rw_exit(&hca->state_lock);
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	if (ibt_status != IBT_SUCCESS) {
18810a701b1eSRobert Gordon 		DTRACE_PROBE1(rpcib__i_conntosrv,
18820a701b1eSRobert Gordon 		    int, ibt_status);
18837c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	/* Connect to the Server */
18877c478bd9Sstevel@tonic-gate 	(void) bzero(&ret_args, sizeof (ret_args));
18887c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->cb_lock);
18897c478bd9Sstevel@tonic-gate 	ibt_status = ibt_open_rc_channel(qp->qp_hdl, IBT_OCHAN_NO_FLAGS,
18907c478bd9Sstevel@tonic-gate 	    IBT_BLOCKING, &chan_args, &ret_args);
18917c478bd9Sstevel@tonic-gate 	if (ibt_status != IBT_SUCCESS) {
18920a701b1eSRobert Gordon 		DTRACE_PROBE2(rpcib__i_openrctosrv,
18930a701b1eSRobert Gordon 		    int, ibt_status, int, ret_args.rc_status);
18940a701b1eSRobert Gordon 
18957c478bd9Sstevel@tonic-gate 		(void) ibt_free_channel(qp->qp_hdl);
18967c478bd9Sstevel@tonic-gate 		qp->qp_hdl = NULL;
18977c478bd9Sstevel@tonic-gate 		mutex_exit(&qp->cb_lock);
18987c478bd9Sstevel@tonic-gate 		if (refresh-- && ibt_status == IBT_CM_FAILURE &&
18997c478bd9Sstevel@tonic-gate 		    ret_args.rc_status == IBT_CM_CONN_STALE) {
19007c478bd9Sstevel@tonic-gate 			/*
19017c478bd9Sstevel@tonic-gate 			 * Got IBT_CM_CONN_STALE probably because of stale
19027c478bd9Sstevel@tonic-gate 			 * data on the passive end of a channel that existed
19037c478bd9Sstevel@tonic-gate 			 * prior to reboot. Retry establishing a channel
19047c478bd9Sstevel@tonic-gate 			 * REFRESH_ATTEMPTS times, during which time the
19057c478bd9Sstevel@tonic-gate 			 * stale conditions on the server might clear up.
19067c478bd9Sstevel@tonic-gate 			 */
19077c478bd9Sstevel@tonic-gate 			goto refresh;
19087c478bd9Sstevel@tonic-gate 		}
19097c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
19107c478bd9Sstevel@tonic-gate 	}
19117c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->cb_lock);
19127c478bd9Sstevel@tonic-gate 	/*
19137c478bd9Sstevel@tonic-gate 	 * Set the private data area to qp to be used in callbacks
19147c478bd9Sstevel@tonic-gate 	 */
19157c478bd9Sstevel@tonic-gate 	ibt_set_chan_private(qp->qp_hdl, (void *)qp);
19167c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate rdma_stat
1920214ae7d0SSiddheshwar Mahesh rib_ping_srv(int addr_type, struct netbuf *raddr, rpcib_ping_t *rptp)
19217c478bd9Sstevel@tonic-gate {
1922*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	uint_t			i, addr_count;
19237c478bd9Sstevel@tonic-gate 	ibt_status_t		ibt_status;
19240a701b1eSRobert Gordon 	uint8_t			num_paths_p;
19250a701b1eSRobert Gordon 	ibt_ip_path_attr_t	ipattr;
19260a701b1eSRobert Gordon 	ibt_path_ip_src_t	srcip;
1927e11c3f44Smeem 	rpcib_ipaddrs_t		addrs4;
1928e11c3f44Smeem 	rpcib_ipaddrs_t		addrs6;
1929e11c3f44Smeem 	struct sockaddr_in	*sinp;
1930e11c3f44Smeem 	struct sockaddr_in6	*sin6p;
1931*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rdma_stat		retval = RDMA_FAILED;
1932*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_hca_t *hca;
19330a701b1eSRobert Gordon 
1934*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if ((addr_type != AF_INET) && (addr_type != AF_INET6))
1935*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		return (RDMA_INVAL);
19367c478bd9Sstevel@tonic-gate 	ASSERT(raddr->buf != NULL);
19377c478bd9Sstevel@tonic-gate 
19380a701b1eSRobert Gordon 	bzero(&ipattr, sizeof (ibt_ip_path_attr_t));
19397c478bd9Sstevel@tonic-gate 
1940e11c3f44Smeem 	if (!rpcib_get_ib_addresses(&addrs4, &addrs6) ||
1941e11c3f44Smeem 	    (addrs4.ri_count == 0 && addrs6.ri_count == 0)) {
1942e11c3f44Smeem 		retval = RDMA_FAILED;
1943*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		goto done2;
19440a701b1eSRobert Gordon 	}
19450a701b1eSRobert Gordon 
1946*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (addr_type == AF_INET) {
1947*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		addr_count = addrs4.ri_count;
1948e11c3f44Smeem 		sinp = (struct sockaddr_in *)raddr->buf;
1949214ae7d0SSiddheshwar Mahesh 		rptp->dstip.family = AF_INET;
1950214ae7d0SSiddheshwar Mahesh 		rptp->dstip.un.ip4addr = sinp->sin_addr.s_addr;
1951e11c3f44Smeem 		sinp = addrs4.ri_list;
1952*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	} else {
1953*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		addr_count = addrs6.ri_count;
1954e11c3f44Smeem 		sin6p = (struct sockaddr_in6 *)raddr->buf;
1955214ae7d0SSiddheshwar Mahesh 		rptp->dstip.family = AF_INET6;
1956214ae7d0SSiddheshwar Mahesh 		rptp->dstip.un.ip6addr = sin6p->sin6_addr;
1957e11c3f44Smeem 		sin6p = addrs6.ri_list;
1958*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
1959*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
1960*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->hcas_list_lock, RW_READER);
1961*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	for (hca = rib_stat->hcas_list; hca; hca = hca->next) {
1962*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_enter(&hca->state_lock, RW_READER);
1963*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (hca->state == HCA_DETACHED) {
1964*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&hca->state_lock);
1965*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			continue;
1966*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
19670a701b1eSRobert Gordon 
1968214ae7d0SSiddheshwar Mahesh 		ipattr.ipa_dst_ip 	= &rptp->dstip;
1969*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		ipattr.ipa_hca_guid	= hca->hca_guid;
19700a701b1eSRobert Gordon 		ipattr.ipa_ndst		= 1;
19710a701b1eSRobert Gordon 		ipattr.ipa_max_paths	= 1;
1972214ae7d0SSiddheshwar Mahesh 		ipattr.ipa_src_ip.family = rptp->dstip.family;
1973*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		for (i = 0; i < addr_count; i++) {
1974214ae7d0SSiddheshwar Mahesh 			num_paths_p = 0;
1975*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			if (addr_type == AF_INET) {
1976*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				ipattr.ipa_src_ip.un.ip4addr =
1977*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    sinp[i].sin_addr.s_addr;
1978*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			} else {
1979*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				ipattr.ipa_src_ip.un.ip6addr =
1980*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    sin6p[i].sin6_addr;
1981*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			}
1982214ae7d0SSiddheshwar Mahesh 			bzero(&srcip, sizeof (ibt_path_ip_src_t));
19830a701b1eSRobert Gordon 
19840a701b1eSRobert Gordon 			ibt_status = ibt_get_ip_paths(rib_stat->ibt_clnt_hdl,
1985214ae7d0SSiddheshwar Mahesh 			    IBT_PATH_NO_FLAGS, &ipattr, &rptp->path,
1986214ae7d0SSiddheshwar Mahesh 			    &num_paths_p, &srcip);
19870a701b1eSRobert Gordon 			if (ibt_status == IBT_SUCCESS &&
19880a701b1eSRobert Gordon 			    num_paths_p != 0 &&
1989*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    rptp->path.pi_hca_guid == hca->hca_guid) {
1990*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				rptp->hca = hca;
1991*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				rw_exit(&hca->state_lock);
1992*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				if (addr_type == AF_INET) {
1993*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					rptp->srcip.family = AF_INET;
1994*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					rptp->srcip.un.ip4addr =
1995*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					    srcip.ip_primary.un.ip4addr;
1996*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				} else {
1997214ae7d0SSiddheshwar Mahesh 					rptp->srcip.family = AF_INET6;
1998214ae7d0SSiddheshwar Mahesh 					rptp->srcip.un.ip6addr =
1999214ae7d0SSiddheshwar Mahesh 					    srcip.ip_primary.un.ip6addr;
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 				}
2002*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				retval = RDMA_SUCCESS;
2003*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				goto done1;
2004*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			}
2005*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
2006*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_exit(&hca->state_lock);
2007*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
2008*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States done1:
2009*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->hcas_list_lock);
2010*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States done2:
2011e11c3f44Smeem 	if (addrs4.ri_size > 0)
2012e11c3f44Smeem 		kmem_free(addrs4.ri_list, addrs4.ri_size);
2013e11c3f44Smeem 	if (addrs6.ri_size > 0)
2014e11c3f44Smeem 		kmem_free(addrs6.ri_list, addrs6.ri_size);
2015e11c3f44Smeem 	return (retval);
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate /*
20197c478bd9Sstevel@tonic-gate  * Close channel, remove from connection list and
20207c478bd9Sstevel@tonic-gate  * free up resources allocated for that channel.
20217c478bd9Sstevel@tonic-gate  */
20227c478bd9Sstevel@tonic-gate rdma_stat
20237c478bd9Sstevel@tonic-gate rib_disconnect_channel(CONN *conn, rib_conn_list_t *conn_list)
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp = ctoqp(conn);
20267c478bd9Sstevel@tonic-gate 	rib_hca_t	*hca;
20277c478bd9Sstevel@tonic-gate 
2028065714dcSSiddheshwar Mahesh 	mutex_enter(&conn->c_lock);
2029065714dcSSiddheshwar Mahesh 	if (conn->c_timeout != NULL) {
2030065714dcSSiddheshwar Mahesh 		mutex_exit(&conn->c_lock);
2031065714dcSSiddheshwar Mahesh 		(void) untimeout(conn->c_timeout);
2032065714dcSSiddheshwar Mahesh 		mutex_enter(&conn->c_lock);
2033065714dcSSiddheshwar Mahesh 	}
2034065714dcSSiddheshwar Mahesh 
2035065714dcSSiddheshwar Mahesh 	while (conn->c_flags & C_CLOSE_PENDING) {
2036065714dcSSiddheshwar Mahesh 		cv_wait(&conn->c_cv, &conn->c_lock);
2037065714dcSSiddheshwar Mahesh 	}
2038065714dcSSiddheshwar Mahesh 	mutex_exit(&conn->c_lock);
2039065714dcSSiddheshwar Mahesh 
20407c478bd9Sstevel@tonic-gate 	/*
20417c478bd9Sstevel@tonic-gate 	 * c_ref == 0 and connection is in C_DISCONN_PEND
20427c478bd9Sstevel@tonic-gate 	 */
20437c478bd9Sstevel@tonic-gate 	hca = qp->hca;
20447c478bd9Sstevel@tonic-gate 	if (conn_list != NULL)
20457c478bd9Sstevel@tonic-gate 		(void) rib_rm_conn(conn, conn_list);
20460a701b1eSRobert Gordon 
20477c478bd9Sstevel@tonic-gate 	/*
2048065714dcSSiddheshwar Mahesh 	 * There is only one case where we get here with
2049065714dcSSiddheshwar Mahesh 	 * qp_hdl = NULL, which is during connection setup on
2050065714dcSSiddheshwar Mahesh 	 * the client. In such a case there are no posted
2051065714dcSSiddheshwar Mahesh 	 * send/recv buffers.
20527c478bd9Sstevel@tonic-gate 	 */
2053065714dcSSiddheshwar Mahesh 	if (qp->qp_hdl != NULL) {
20547c478bd9Sstevel@tonic-gate 		mutex_enter(&qp->posted_rbufs_lock);
20557c478bd9Sstevel@tonic-gate 		while (qp->n_posted_rbufs)
20567c478bd9Sstevel@tonic-gate 			cv_wait(&qp->posted_rbufs_cv, &qp->posted_rbufs_lock);
20577c478bd9Sstevel@tonic-gate 		mutex_exit(&qp->posted_rbufs_lock);
2058065714dcSSiddheshwar Mahesh 
2059065714dcSSiddheshwar Mahesh 		mutex_enter(&qp->send_rbufs_lock);
2060065714dcSSiddheshwar Mahesh 		while (qp->n_send_rbufs)
2061065714dcSSiddheshwar Mahesh 			cv_wait(&qp->send_rbufs_cv, &qp->send_rbufs_lock);
2062065714dcSSiddheshwar Mahesh 		mutex_exit(&qp->send_rbufs_lock);
2063065714dcSSiddheshwar Mahesh 
20647c478bd9Sstevel@tonic-gate 		(void) ibt_free_channel(qp->qp_hdl);
20657c478bd9Sstevel@tonic-gate 		qp->qp_hdl = NULL;
20667c478bd9Sstevel@tonic-gate 	}
20670a701b1eSRobert Gordon 
20687c478bd9Sstevel@tonic-gate 	ASSERT(qp->rdlist == NULL);
20690a701b1eSRobert Gordon 
20707c478bd9Sstevel@tonic-gate 	if (qp->replylist != NULL) {
20717c478bd9Sstevel@tonic-gate 		(void) rib_rem_replylist(qp);
20727c478bd9Sstevel@tonic-gate 	}
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	cv_destroy(&qp->cb_conn_cv);
20757c478bd9Sstevel@tonic-gate 	cv_destroy(&qp->posted_rbufs_cv);
2076065714dcSSiddheshwar Mahesh 	cv_destroy(&qp->send_rbufs_cv);
20777c478bd9Sstevel@tonic-gate 	mutex_destroy(&qp->cb_lock);
20787c478bd9Sstevel@tonic-gate 	mutex_destroy(&qp->replylist_lock);
20797c478bd9Sstevel@tonic-gate 	mutex_destroy(&qp->posted_rbufs_lock);
2080065714dcSSiddheshwar Mahesh 	mutex_destroy(&qp->send_rbufs_lock);
20817c478bd9Sstevel@tonic-gate 	mutex_destroy(&qp->rdlist_lock);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	cv_destroy(&conn->c_cv);
20847c478bd9Sstevel@tonic-gate 	mutex_destroy(&conn->c_lock);
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	if (conn->c_raddr.buf != NULL) {
20877c478bd9Sstevel@tonic-gate 		kmem_free(conn->c_raddr.buf, conn->c_raddr.len);
20887c478bd9Sstevel@tonic-gate 	}
20897c478bd9Sstevel@tonic-gate 	if (conn->c_laddr.buf != NULL) {
20907c478bd9Sstevel@tonic-gate 		kmem_free(conn->c_laddr.buf, conn->c_laddr.len);
20917c478bd9Sstevel@tonic-gate 	}
20920a701b1eSRobert Gordon 
20930a701b1eSRobert Gordon 	/*
20940a701b1eSRobert Gordon 	 * Credit control cleanup.
20950a701b1eSRobert Gordon 	 */
20960a701b1eSRobert Gordon 	if (qp->rdmaconn.c_cc_type == RDMA_CC_CLNT) {
20970a701b1eSRobert Gordon 		rdma_clnt_cred_ctrl_t *cc_info;
20980a701b1eSRobert Gordon 		cc_info = &qp->rdmaconn.rdma_conn_cred_ctrl_u.c_clnt_cc;
20990a701b1eSRobert Gordon 		cv_destroy(&cc_info->clnt_cc_cv);
21000a701b1eSRobert Gordon 	}
21010a701b1eSRobert Gordon 
21027c478bd9Sstevel@tonic-gate 	kmem_free(qp, sizeof (rib_qp_t));
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	/*
21057c478bd9Sstevel@tonic-gate 	 * If HCA has been DETACHED and the srv/clnt_conn_list is NULL,
21067c478bd9Sstevel@tonic-gate 	 * then the hca is no longer being used.
21077c478bd9Sstevel@tonic-gate 	 */
21087c478bd9Sstevel@tonic-gate 	if (conn_list != NULL) {
21097c478bd9Sstevel@tonic-gate 		rw_enter(&hca->state_lock, RW_READER);
21107c478bd9Sstevel@tonic-gate 		if (hca->state == HCA_DETACHED) {
21117c478bd9Sstevel@tonic-gate 			rw_enter(&hca->srv_conn_list.conn_lock, RW_READER);
21127c478bd9Sstevel@tonic-gate 			if (hca->srv_conn_list.conn_hd == NULL) {
21137c478bd9Sstevel@tonic-gate 				rw_enter(&hca->cl_conn_list.conn_lock,
21147c478bd9Sstevel@tonic-gate 				    RW_READER);
21150a701b1eSRobert Gordon 
21167c478bd9Sstevel@tonic-gate 				if (hca->cl_conn_list.conn_hd == NULL) {
21177c478bd9Sstevel@tonic-gate 					mutex_enter(&hca->inuse_lock);
21187c478bd9Sstevel@tonic-gate 					hca->inuse = FALSE;
21197c478bd9Sstevel@tonic-gate 					cv_signal(&hca->cb_cv);
21207c478bd9Sstevel@tonic-gate 					mutex_exit(&hca->inuse_lock);
21217c478bd9Sstevel@tonic-gate 				}
21227c478bd9Sstevel@tonic-gate 				rw_exit(&hca->cl_conn_list.conn_lock);
21237c478bd9Sstevel@tonic-gate 			}
21247c478bd9Sstevel@tonic-gate 			rw_exit(&hca->srv_conn_list.conn_lock);
21257c478bd9Sstevel@tonic-gate 		}
21267c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
21277c478bd9Sstevel@tonic-gate 	}
21280a701b1eSRobert Gordon 
21297c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate /*
2133065714dcSSiddheshwar Mahesh  * All sends are done under the protection of
2134065714dcSSiddheshwar Mahesh  * the wdesc->sendwait_lock. n_send_rbufs count
2135065714dcSSiddheshwar Mahesh  * is protected using the send_rbufs_lock.
2136065714dcSSiddheshwar Mahesh  * lock ordering is:
2137065714dcSSiddheshwar Mahesh  * sendwait_lock -> send_rbufs_lock
2138065714dcSSiddheshwar Mahesh  */
2139065714dcSSiddheshwar Mahesh 
2140065714dcSSiddheshwar Mahesh void
2141065714dcSSiddheshwar Mahesh rib_send_hold(rib_qp_t *qp)
2142065714dcSSiddheshwar Mahesh {
2143065714dcSSiddheshwar Mahesh 	mutex_enter(&qp->send_rbufs_lock);
2144065714dcSSiddheshwar Mahesh 	qp->n_send_rbufs++;
2145065714dcSSiddheshwar Mahesh 	mutex_exit(&qp->send_rbufs_lock);
2146065714dcSSiddheshwar Mahesh }
2147065714dcSSiddheshwar Mahesh 
2148065714dcSSiddheshwar Mahesh void
2149065714dcSSiddheshwar Mahesh rib_send_rele(rib_qp_t *qp)
2150065714dcSSiddheshwar Mahesh {
2151065714dcSSiddheshwar Mahesh 	mutex_enter(&qp->send_rbufs_lock);
2152065714dcSSiddheshwar Mahesh 	qp->n_send_rbufs--;
2153065714dcSSiddheshwar Mahesh 	if (qp->n_send_rbufs == 0)
2154065714dcSSiddheshwar Mahesh 		cv_signal(&qp->send_rbufs_cv);
2155065714dcSSiddheshwar Mahesh 	mutex_exit(&qp->send_rbufs_lock);
2156065714dcSSiddheshwar Mahesh }
2157065714dcSSiddheshwar Mahesh 
2158065714dcSSiddheshwar Mahesh /*
21597c478bd9Sstevel@tonic-gate  * Wait for send completion notification. Only on receiving a
21607c478bd9Sstevel@tonic-gate  * notification be it a successful or error completion, free the
21617c478bd9Sstevel@tonic-gate  * send_wid.
21627c478bd9Sstevel@tonic-gate  */
21637c478bd9Sstevel@tonic-gate static rdma_stat
21647c478bd9Sstevel@tonic-gate rib_sendwait(rib_qp_t *qp, struct send_wid *wd)
21657c478bd9Sstevel@tonic-gate {
21667c478bd9Sstevel@tonic-gate 	clock_t timout, cv_wait_ret;
21677c478bd9Sstevel@tonic-gate 	rdma_stat error = RDMA_SUCCESS;
21687c478bd9Sstevel@tonic-gate 	int	i;
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	/*
21717c478bd9Sstevel@tonic-gate 	 * Wait for send to complete
21727c478bd9Sstevel@tonic-gate 	 */
21737c478bd9Sstevel@tonic-gate 	ASSERT(wd != NULL);
21747c478bd9Sstevel@tonic-gate 	mutex_enter(&wd->sendwait_lock);
21757c478bd9Sstevel@tonic-gate 	if (wd->status == (uint_t)SEND_WAIT) {
21767c478bd9Sstevel@tonic-gate 		timout = drv_usectohz(SEND_WAIT_TIME * 1000000) +
21777c478bd9Sstevel@tonic-gate 		    ddi_get_lbolt();
21780a701b1eSRobert Gordon 
21797c478bd9Sstevel@tonic-gate 		if (qp->mode == RIB_SERVER) {
21807c478bd9Sstevel@tonic-gate 			while ((cv_wait_ret = cv_timedwait(&wd->wait_cv,
21817c478bd9Sstevel@tonic-gate 			    &wd->sendwait_lock, timout)) > 0 &&
21827c478bd9Sstevel@tonic-gate 			    wd->status == (uint_t)SEND_WAIT)
21837c478bd9Sstevel@tonic-gate 				;
21847c478bd9Sstevel@tonic-gate 			switch (cv_wait_ret) {
21857c478bd9Sstevel@tonic-gate 			case -1:	/* timeout */
21860a701b1eSRobert Gordon 				DTRACE_PROBE(rpcib__i__srvsendwait__timeout);
21870a701b1eSRobert Gordon 
21887c478bd9Sstevel@tonic-gate 				wd->cv_sig = 0;		/* no signal needed */
21897c478bd9Sstevel@tonic-gate 				error = RDMA_TIMEDOUT;
21907c478bd9Sstevel@tonic-gate 				break;
21917c478bd9Sstevel@tonic-gate 			default:	/* got send completion */
21927c478bd9Sstevel@tonic-gate 				break;
21937c478bd9Sstevel@tonic-gate 			}
21947c478bd9Sstevel@tonic-gate 		} else {
21957c478bd9Sstevel@tonic-gate 			while ((cv_wait_ret = cv_timedwait_sig(&wd->wait_cv,
21967c478bd9Sstevel@tonic-gate 			    &wd->sendwait_lock, timout)) > 0 &&
21977c478bd9Sstevel@tonic-gate 			    wd->status == (uint_t)SEND_WAIT)
21987c478bd9Sstevel@tonic-gate 				;
21997c478bd9Sstevel@tonic-gate 			switch (cv_wait_ret) {
22007c478bd9Sstevel@tonic-gate 			case -1:	/* timeout */
22010a701b1eSRobert Gordon 				DTRACE_PROBE(rpcib__i__clntsendwait__timeout);
22020a701b1eSRobert Gordon 
22037c478bd9Sstevel@tonic-gate 				wd->cv_sig = 0;		/* no signal needed */
22047c478bd9Sstevel@tonic-gate 				error = RDMA_TIMEDOUT;
22057c478bd9Sstevel@tonic-gate 				break;
22067c478bd9Sstevel@tonic-gate 			case 0:		/* interrupted */
22070a701b1eSRobert Gordon 				DTRACE_PROBE(rpcib__i__clntsendwait__intr);
22080a701b1eSRobert Gordon 
22097c478bd9Sstevel@tonic-gate 				wd->cv_sig = 0;		/* no signal needed */
22107c478bd9Sstevel@tonic-gate 				error = RDMA_INTR;
22117c478bd9Sstevel@tonic-gate 				break;
22127c478bd9Sstevel@tonic-gate 			default:	/* got send completion */
22137c478bd9Sstevel@tonic-gate 				break;
22147c478bd9Sstevel@tonic-gate 			}
22157c478bd9Sstevel@tonic-gate 		}
22167c478bd9Sstevel@tonic-gate 	}
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 	if (wd->status != (uint_t)SEND_WAIT) {
22197c478bd9Sstevel@tonic-gate 		/* got send completion */
22207c478bd9Sstevel@tonic-gate 		if (wd->status != RDMA_SUCCESS) {
2221065714dcSSiddheshwar Mahesh 			switch (wd->status) {
2222065714dcSSiddheshwar Mahesh 			case RDMA_CONNLOST:
2223f837ee4aSSiddheshwar Mahesh 				error = RDMA_CONNLOST;
2224065714dcSSiddheshwar Mahesh 				break;
2225065714dcSSiddheshwar Mahesh 			default:
2226065714dcSSiddheshwar Mahesh 				error = RDMA_FAILED;
2227065714dcSSiddheshwar Mahesh 				break;
2228f837ee4aSSiddheshwar Mahesh 			}
22297c478bd9Sstevel@tonic-gate 		}
22307c478bd9Sstevel@tonic-gate 		for (i = 0; i < wd->nsbufs; i++) {
22317c478bd9Sstevel@tonic-gate 			rib_rbuf_free(qptoc(qp), SEND_BUFFER,
223211606941Sjwahlig 			    (void *)(uintptr_t)wd->sbufaddr[i]);
22337c478bd9Sstevel@tonic-gate 		}
2234065714dcSSiddheshwar Mahesh 
2235065714dcSSiddheshwar Mahesh 		rib_send_rele(qp);
2236065714dcSSiddheshwar Mahesh 
22377c478bd9Sstevel@tonic-gate 		mutex_exit(&wd->sendwait_lock);
22387c478bd9Sstevel@tonic-gate 		(void) rib_free_sendwait(wd);
2239065714dcSSiddheshwar Mahesh 
22407c478bd9Sstevel@tonic-gate 	} else {
22417c478bd9Sstevel@tonic-gate 		mutex_exit(&wd->sendwait_lock);
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate 	return (error);
22447c478bd9Sstevel@tonic-gate }
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate static struct send_wid *
22477c478bd9Sstevel@tonic-gate rib_init_sendwait(uint32_t xid, int cv_sig, rib_qp_t *qp)
22487c478bd9Sstevel@tonic-gate {
22497c478bd9Sstevel@tonic-gate 	struct send_wid	*wd;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	wd = kmem_zalloc(sizeof (struct send_wid), KM_SLEEP);
22527c478bd9Sstevel@tonic-gate 	wd->xid = xid;
22537c478bd9Sstevel@tonic-gate 	wd->cv_sig = cv_sig;
22547c478bd9Sstevel@tonic-gate 	wd->qp = qp;
22557c478bd9Sstevel@tonic-gate 	cv_init(&wd->wait_cv, NULL, CV_DEFAULT, NULL);
22567c478bd9Sstevel@tonic-gate 	mutex_init(&wd->sendwait_lock, NULL, MUTEX_DRIVER, NULL);
22577c478bd9Sstevel@tonic-gate 	wd->status = (uint_t)SEND_WAIT;
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	return (wd);
22607c478bd9Sstevel@tonic-gate }
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate static int
22637c478bd9Sstevel@tonic-gate rib_free_sendwait(struct send_wid *wdesc)
22647c478bd9Sstevel@tonic-gate {
22657c478bd9Sstevel@tonic-gate 	cv_destroy(&wdesc->wait_cv);
22667c478bd9Sstevel@tonic-gate 	mutex_destroy(&wdesc->sendwait_lock);
22677c478bd9Sstevel@tonic-gate 	kmem_free(wdesc, sizeof (*wdesc));
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 	return (0);
22707c478bd9Sstevel@tonic-gate }
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate static rdma_stat
22737c478bd9Sstevel@tonic-gate rib_rem_rep(rib_qp_t *qp, struct reply *rep)
22747c478bd9Sstevel@tonic-gate {
22757c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->replylist_lock);
22767c478bd9Sstevel@tonic-gate 	if (rep != NULL) {
22777c478bd9Sstevel@tonic-gate 		(void) rib_remreply(qp, rep);
22787c478bd9Sstevel@tonic-gate 		mutex_exit(&qp->replylist_lock);
22797c478bd9Sstevel@tonic-gate 		return (RDMA_SUCCESS);
22807c478bd9Sstevel@tonic-gate 	}
22817c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->replylist_lock);
22827c478bd9Sstevel@tonic-gate 	return (RDMA_FAILED);
22837c478bd9Sstevel@tonic-gate }
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate /*
22867c478bd9Sstevel@tonic-gate  * Send buffers are freed here only in case of error in posting
22877c478bd9Sstevel@tonic-gate  * on QP. If the post succeeded, the send buffers are freed upon
22887c478bd9Sstevel@tonic-gate  * send completion in rib_sendwait() or in the scq_handler.
22897c478bd9Sstevel@tonic-gate  */
22907c478bd9Sstevel@tonic-gate rdma_stat
22917c478bd9Sstevel@tonic-gate rib_send_and_wait(CONN *conn, struct clist *cl, uint32_t msgid,
22920a701b1eSRobert Gordon 	int send_sig, int cv_sig, caddr_t *swid)
22937c478bd9Sstevel@tonic-gate {
22947c478bd9Sstevel@tonic-gate 	struct send_wid	*wdesc;
22957c478bd9Sstevel@tonic-gate 	struct clist	*clp;
22967c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status = IBT_SUCCESS;
22977c478bd9Sstevel@tonic-gate 	rdma_stat	ret = RDMA_SUCCESS;
22987c478bd9Sstevel@tonic-gate 	ibt_send_wr_t	tx_wr;
22997c478bd9Sstevel@tonic-gate 	int		i, nds;
23007c478bd9Sstevel@tonic-gate 	ibt_wr_ds_t	sgl[DSEG_MAX];
23017c478bd9Sstevel@tonic-gate 	uint_t		total_msg_size;
23020a701b1eSRobert Gordon 	rib_qp_t	*qp;
23030a701b1eSRobert Gordon 
23040a701b1eSRobert Gordon 	qp = ctoqp(conn);
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	ASSERT(cl != NULL);
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 	bzero(&tx_wr, sizeof (ibt_send_wr_t));
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	nds = 0;
23117c478bd9Sstevel@tonic-gate 	total_msg_size = 0;
23127c478bd9Sstevel@tonic-gate 	clp = cl;
23137c478bd9Sstevel@tonic-gate 	while (clp != NULL) {
23147c478bd9Sstevel@tonic-gate 		if (nds >= DSEG_MAX) {
23150a701b1eSRobert Gordon 			DTRACE_PROBE(rpcib__i__sendandwait_dsegmax_exceeded);
23167c478bd9Sstevel@tonic-gate 			return (RDMA_FAILED);
23177c478bd9Sstevel@tonic-gate 		}
23180a701b1eSRobert Gordon 		sgl[nds].ds_va = clp->w.c_saddr;
23197c478bd9Sstevel@tonic-gate 		sgl[nds].ds_key = clp->c_smemhandle.mrc_lmr; /* lkey */
23207c478bd9Sstevel@tonic-gate 		sgl[nds].ds_len = clp->c_len;
23217c478bd9Sstevel@tonic-gate 		total_msg_size += clp->c_len;
23227c478bd9Sstevel@tonic-gate 		clp = clp->c_next;
23237c478bd9Sstevel@tonic-gate 		nds++;
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	if (send_sig) {
23277c478bd9Sstevel@tonic-gate 		/* Set SEND_SIGNAL flag. */
23287c478bd9Sstevel@tonic-gate 		tx_wr.wr_flags = IBT_WR_SEND_SIGNAL;
23297c478bd9Sstevel@tonic-gate 		wdesc = rib_init_sendwait(msgid, cv_sig, qp);
23300a701b1eSRobert Gordon 		*swid = (caddr_t)wdesc;
2331065714dcSSiddheshwar Mahesh 		tx_wr.wr_id = (ibt_wrid_t)(uintptr_t)wdesc;
2332065714dcSSiddheshwar Mahesh 		mutex_enter(&wdesc->sendwait_lock);
23337c478bd9Sstevel@tonic-gate 		wdesc->nsbufs = nds;
23347c478bd9Sstevel@tonic-gate 		for (i = 0; i < nds; i++) {
23357c478bd9Sstevel@tonic-gate 			wdesc->sbufaddr[i] = sgl[i].ds_va;
23367c478bd9Sstevel@tonic-gate 		}
2337065714dcSSiddheshwar Mahesh 	} else {
2338065714dcSSiddheshwar Mahesh 		tx_wr.wr_flags = IBT_WR_NO_FLAGS;
2339065714dcSSiddheshwar Mahesh 		*swid = NULL;
2340065714dcSSiddheshwar Mahesh 		tx_wr.wr_id = (ibt_wrid_t)RDMA_DUMMY_WRID;
2341065714dcSSiddheshwar Mahesh 	}
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	tx_wr.wr_opcode = IBT_WRC_SEND;
23447c478bd9Sstevel@tonic-gate 	tx_wr.wr_trans = IBT_RC_SRV;
23457c478bd9Sstevel@tonic-gate 	tx_wr.wr_nds = nds;
23467c478bd9Sstevel@tonic-gate 	tx_wr.wr_sgl = sgl;
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	mutex_enter(&conn->c_lock);
23490a701b1eSRobert Gordon 	if (conn->c_state == C_CONNECTED) {
23507c478bd9Sstevel@tonic-gate 		ibt_status = ibt_post_send(qp->qp_hdl, &tx_wr, 1, NULL);
23517c478bd9Sstevel@tonic-gate 	}
23520a701b1eSRobert Gordon 	if (conn->c_state != C_CONNECTED ||
23537c478bd9Sstevel@tonic-gate 	    ibt_status != IBT_SUCCESS) {
23540a701b1eSRobert Gordon 		if (conn->c_state != C_DISCONN_PEND)
23550a701b1eSRobert Gordon 			conn->c_state = C_ERROR_CONN;
23567c478bd9Sstevel@tonic-gate 		mutex_exit(&conn->c_lock);
2357065714dcSSiddheshwar Mahesh 		if (send_sig) {
23587c478bd9Sstevel@tonic-gate 			for (i = 0; i < nds; i++) {
23597c478bd9Sstevel@tonic-gate 				rib_rbuf_free(conn, SEND_BUFFER,
236011606941Sjwahlig 				    (void *)(uintptr_t)wdesc->sbufaddr[i]);
23617c478bd9Sstevel@tonic-gate 			}
2362065714dcSSiddheshwar Mahesh 			mutex_exit(&wdesc->sendwait_lock);
23637c478bd9Sstevel@tonic-gate 			(void) rib_free_sendwait(wdesc);
2364065714dcSSiddheshwar Mahesh 		}
23650a701b1eSRobert Gordon 		return (RDMA_CONNLOST);
23667c478bd9Sstevel@tonic-gate 	}
2367065714dcSSiddheshwar Mahesh 
23687c478bd9Sstevel@tonic-gate 	mutex_exit(&conn->c_lock);
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 	if (send_sig) {
2371065714dcSSiddheshwar Mahesh 		rib_send_hold(qp);
2372065714dcSSiddheshwar Mahesh 		mutex_exit(&wdesc->sendwait_lock);
23737c478bd9Sstevel@tonic-gate 		if (cv_sig) {
23747c478bd9Sstevel@tonic-gate 			/*
23757c478bd9Sstevel@tonic-gate 			 * cv_wait for send to complete.
23767c478bd9Sstevel@tonic-gate 			 * We can fail due to a timeout or signal or
23777c478bd9Sstevel@tonic-gate 			 * unsuccessful send.
23787c478bd9Sstevel@tonic-gate 			 */
23797c478bd9Sstevel@tonic-gate 			ret = rib_sendwait(qp, wdesc);
23800a701b1eSRobert Gordon 
23817c478bd9Sstevel@tonic-gate 			return (ret);
23827c478bd9Sstevel@tonic-gate 		}
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
23867c478bd9Sstevel@tonic-gate }
23877c478bd9Sstevel@tonic-gate 
23880a701b1eSRobert Gordon 
23897c478bd9Sstevel@tonic-gate rdma_stat
23907c478bd9Sstevel@tonic-gate rib_send(CONN *conn, struct clist *cl, uint32_t msgid)
23917c478bd9Sstevel@tonic-gate {
23927c478bd9Sstevel@tonic-gate 	rdma_stat	ret;
23930a701b1eSRobert Gordon 	caddr_t		wd;
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	/* send-wait & cv_signal */
23960a701b1eSRobert Gordon 	ret = rib_send_and_wait(conn, cl, msgid, 1, 1, &wd);
23977c478bd9Sstevel@tonic-gate 	return (ret);
23987c478bd9Sstevel@tonic-gate }
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate /*
2401065714dcSSiddheshwar Mahesh  * Deprecated/obsolete interface not used currently
2402065714dcSSiddheshwar Mahesh  * but earlier used for READ-READ protocol.
24037c478bd9Sstevel@tonic-gate  * Send RPC reply and wait for RDMA_DONE.
24047c478bd9Sstevel@tonic-gate  */
24057c478bd9Sstevel@tonic-gate rdma_stat
24067c478bd9Sstevel@tonic-gate rib_send_resp(CONN *conn, struct clist *cl, uint32_t msgid)
24077c478bd9Sstevel@tonic-gate {
24087c478bd9Sstevel@tonic-gate 	rdma_stat ret = RDMA_SUCCESS;
24097c478bd9Sstevel@tonic-gate 	struct rdma_done_list *rd;
24107c478bd9Sstevel@tonic-gate 	clock_t timout, cv_wait_ret;
24110a701b1eSRobert Gordon 	caddr_t *wid = NULL;
24127c478bd9Sstevel@tonic-gate 	rib_qp_t *qp = ctoqp(conn);
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->rdlist_lock);
24157c478bd9Sstevel@tonic-gate 	rd = rdma_done_add(qp, msgid);
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	/* No cv_signal (whether send-wait or no-send-wait) */
24180a701b1eSRobert Gordon 	ret = rib_send_and_wait(conn, cl, msgid, 1, 0, wid);
24197c478bd9Sstevel@tonic-gate 
24200a701b1eSRobert Gordon 	if (ret != RDMA_SUCCESS) {
24210a701b1eSRobert Gordon 		rdma_done_rm(qp, rd);
24220a701b1eSRobert Gordon 	} else {
24237c478bd9Sstevel@tonic-gate 		/*
24247c478bd9Sstevel@tonic-gate 		 * Wait for RDMA_DONE from remote end
24257c478bd9Sstevel@tonic-gate 		 */
24260a701b1eSRobert Gordon 		timout =
24270a701b1eSRobert Gordon 		    drv_usectohz(REPLY_WAIT_TIME * 1000000) + ddi_get_lbolt();
24280a701b1eSRobert Gordon 		cv_wait_ret = cv_timedwait(&rd->rdma_done_cv,
24290a701b1eSRobert Gordon 		    &qp->rdlist_lock,
24307c478bd9Sstevel@tonic-gate 		    timout);
24310a701b1eSRobert Gordon 
24327c478bd9Sstevel@tonic-gate 		rdma_done_rm(qp, rd);
24330a701b1eSRobert Gordon 
24347c478bd9Sstevel@tonic-gate 		if (cv_wait_ret < 0) {
24357c478bd9Sstevel@tonic-gate 			ret = RDMA_TIMEDOUT;
24360a701b1eSRobert Gordon 		}
24377c478bd9Sstevel@tonic-gate 	}
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->rdlist_lock);
24407c478bd9Sstevel@tonic-gate 	return (ret);
24417c478bd9Sstevel@tonic-gate }
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate static struct recv_wid *
24447c478bd9Sstevel@tonic-gate rib_create_wid(rib_qp_t *qp, ibt_wr_ds_t *sgl, uint32_t msgid)
24457c478bd9Sstevel@tonic-gate {
24467c478bd9Sstevel@tonic-gate 	struct recv_wid	*rwid;
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate 	rwid = kmem_zalloc(sizeof (struct recv_wid), KM_SLEEP);
24497c478bd9Sstevel@tonic-gate 	rwid->xid = msgid;
24507c478bd9Sstevel@tonic-gate 	rwid->addr = sgl->ds_va;
24517c478bd9Sstevel@tonic-gate 	rwid->qp = qp;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	return (rwid);
24547c478bd9Sstevel@tonic-gate }
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate static void
24577c478bd9Sstevel@tonic-gate rib_free_wid(struct recv_wid *rwid)
24587c478bd9Sstevel@tonic-gate {
24597c478bd9Sstevel@tonic-gate 	kmem_free(rwid, sizeof (struct recv_wid));
24607c478bd9Sstevel@tonic-gate }
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate rdma_stat
24637c478bd9Sstevel@tonic-gate rib_clnt_post(CONN* conn, struct clist *cl, uint32_t msgid)
24647c478bd9Sstevel@tonic-gate {
24657c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp = ctoqp(conn);
24667c478bd9Sstevel@tonic-gate 	struct clist	*clp = cl;
24677c478bd9Sstevel@tonic-gate 	struct reply	*rep;
24687c478bd9Sstevel@tonic-gate 	struct recv_wid	*rwid;
24697c478bd9Sstevel@tonic-gate 	int		nds;
24707c478bd9Sstevel@tonic-gate 	ibt_wr_ds_t	sgl[DSEG_MAX];
24717c478bd9Sstevel@tonic-gate 	ibt_recv_wr_t	recv_wr;
24727c478bd9Sstevel@tonic-gate 	rdma_stat	ret;
24737c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	/*
24767c478bd9Sstevel@tonic-gate 	 * rdma_clnt_postrecv uses RECV_BUFFER.
24777c478bd9Sstevel@tonic-gate 	 */
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	nds = 0;
24807c478bd9Sstevel@tonic-gate 	while (cl != NULL) {
24817c478bd9Sstevel@tonic-gate 		if (nds >= DSEG_MAX) {
24827c478bd9Sstevel@tonic-gate 			ret = RDMA_FAILED;
24837c478bd9Sstevel@tonic-gate 			goto done;
24847c478bd9Sstevel@tonic-gate 		}
24850a701b1eSRobert Gordon 		sgl[nds].ds_va = cl->w.c_saddr;
24867c478bd9Sstevel@tonic-gate 		sgl[nds].ds_key = cl->c_smemhandle.mrc_lmr; /* lkey */
24877c478bd9Sstevel@tonic-gate 		sgl[nds].ds_len = cl->c_len;
24887c478bd9Sstevel@tonic-gate 		cl = cl->c_next;
24897c478bd9Sstevel@tonic-gate 		nds++;
24907c478bd9Sstevel@tonic-gate 	}
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	if (nds != 1) {
24937c478bd9Sstevel@tonic-gate 		ret = RDMA_FAILED;
24947c478bd9Sstevel@tonic-gate 		goto done;
24957c478bd9Sstevel@tonic-gate 	}
24960a701b1eSRobert Gordon 
24977c478bd9Sstevel@tonic-gate 	bzero(&recv_wr, sizeof (ibt_recv_wr_t));
24987c478bd9Sstevel@tonic-gate 	recv_wr.wr_nds = nds;
24997c478bd9Sstevel@tonic-gate 	recv_wr.wr_sgl = sgl;
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	rwid = rib_create_wid(qp, &sgl[0], msgid);
25027c478bd9Sstevel@tonic-gate 	if (rwid) {
250311606941Sjwahlig 		recv_wr.wr_id = (ibt_wrid_t)(uintptr_t)rwid;
25047c478bd9Sstevel@tonic-gate 	} else {
25057c478bd9Sstevel@tonic-gate 		ret = RDMA_NORESOURCE;
25067c478bd9Sstevel@tonic-gate 		goto done;
25077c478bd9Sstevel@tonic-gate 	}
25087c478bd9Sstevel@tonic-gate 	rep = rib_addreplylist(qp, msgid);
25097c478bd9Sstevel@tonic-gate 	if (!rep) {
25107c478bd9Sstevel@tonic-gate 		rib_free_wid(rwid);
25117c478bd9Sstevel@tonic-gate 		ret = RDMA_NORESOURCE;
25127c478bd9Sstevel@tonic-gate 		goto done;
25137c478bd9Sstevel@tonic-gate 	}
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	mutex_enter(&conn->c_lock);
25160a701b1eSRobert Gordon 
25170a701b1eSRobert Gordon 	if (conn->c_state == C_CONNECTED) {
25187c478bd9Sstevel@tonic-gate 		ibt_status = ibt_post_recv(qp->qp_hdl, &recv_wr, 1, NULL);
25197c478bd9Sstevel@tonic-gate 	}
25200a701b1eSRobert Gordon 
25210a701b1eSRobert Gordon 	if (conn->c_state != C_CONNECTED ||
25227c478bd9Sstevel@tonic-gate 	    ibt_status != IBT_SUCCESS) {
25230a701b1eSRobert Gordon 		if (conn->c_state != C_DISCONN_PEND)
25240a701b1eSRobert Gordon 			conn->c_state = C_ERROR_CONN;
25257c478bd9Sstevel@tonic-gate 		mutex_exit(&conn->c_lock);
25267c478bd9Sstevel@tonic-gate 		rib_free_wid(rwid);
25277c478bd9Sstevel@tonic-gate 		(void) rib_rem_rep(qp, rep);
25280a701b1eSRobert Gordon 		ret = RDMA_CONNLOST;
25297c478bd9Sstevel@tonic-gate 		goto done;
25307c478bd9Sstevel@tonic-gate 	}
25317c478bd9Sstevel@tonic-gate 	mutex_exit(&conn->c_lock);
25327c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate done:
25357c478bd9Sstevel@tonic-gate 	while (clp != NULL) {
25360a701b1eSRobert Gordon 		rib_rbuf_free(conn, RECV_BUFFER,
25370a701b1eSRobert Gordon 		    (void *)(uintptr_t)clp->w.c_saddr3);
25387c478bd9Sstevel@tonic-gate 		clp = clp->c_next;
25397c478bd9Sstevel@tonic-gate 	}
25407c478bd9Sstevel@tonic-gate 	return (ret);
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate rdma_stat
25447c478bd9Sstevel@tonic-gate rib_svc_post(CONN* conn, struct clist *cl)
25457c478bd9Sstevel@tonic-gate {
25467c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp = ctoqp(conn);
25477c478bd9Sstevel@tonic-gate 	struct svc_recv	*s_recvp;
25487c478bd9Sstevel@tonic-gate 	int		nds;
25497c478bd9Sstevel@tonic-gate 	ibt_wr_ds_t	sgl[DSEG_MAX];
25507c478bd9Sstevel@tonic-gate 	ibt_recv_wr_t	recv_wr;
25517c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	nds = 0;
25547c478bd9Sstevel@tonic-gate 	while (cl != NULL) {
25557c478bd9Sstevel@tonic-gate 		if (nds >= DSEG_MAX) {
25567c478bd9Sstevel@tonic-gate 			return (RDMA_FAILED);
25577c478bd9Sstevel@tonic-gate 		}
25580a701b1eSRobert Gordon 		sgl[nds].ds_va = cl->w.c_saddr;
25597c478bd9Sstevel@tonic-gate 		sgl[nds].ds_key = cl->c_smemhandle.mrc_lmr; /* lkey */
25607c478bd9Sstevel@tonic-gate 		sgl[nds].ds_len = cl->c_len;
25617c478bd9Sstevel@tonic-gate 		cl = cl->c_next;
25627c478bd9Sstevel@tonic-gate 		nds++;
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	if (nds != 1) {
25660a701b1eSRobert Gordon 		rib_rbuf_free(conn, RECV_BUFFER,
25670a701b1eSRobert Gordon 		    (caddr_t)(uintptr_t)sgl[0].ds_va);
25680a701b1eSRobert Gordon 
25697c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
25707c478bd9Sstevel@tonic-gate 	}
25710a701b1eSRobert Gordon 
25727c478bd9Sstevel@tonic-gate 	bzero(&recv_wr, sizeof (ibt_recv_wr_t));
25737c478bd9Sstevel@tonic-gate 	recv_wr.wr_nds = nds;
25747c478bd9Sstevel@tonic-gate 	recv_wr.wr_sgl = sgl;
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 	s_recvp = rib_init_svc_recv(qp, &sgl[0]);
257711606941Sjwahlig 	/* Use s_recvp's addr as wr id */
257811606941Sjwahlig 	recv_wr.wr_id = (ibt_wrid_t)(uintptr_t)s_recvp;
25797c478bd9Sstevel@tonic-gate 	mutex_enter(&conn->c_lock);
25800a701b1eSRobert Gordon 	if (conn->c_state == C_CONNECTED) {
25817c478bd9Sstevel@tonic-gate 		ibt_status = ibt_post_recv(qp->qp_hdl, &recv_wr, 1, NULL);
25827c478bd9Sstevel@tonic-gate 	}
25830a701b1eSRobert Gordon 	if (conn->c_state != C_CONNECTED ||
25847c478bd9Sstevel@tonic-gate 	    ibt_status != IBT_SUCCESS) {
25850a701b1eSRobert Gordon 		if (conn->c_state != C_DISCONN_PEND)
25860a701b1eSRobert Gordon 			conn->c_state = C_ERROR_CONN;
25877c478bd9Sstevel@tonic-gate 		mutex_exit(&conn->c_lock);
258811606941Sjwahlig 		rib_rbuf_free(conn, RECV_BUFFER,
258911606941Sjwahlig 		    (caddr_t)(uintptr_t)sgl[0].ds_va);
25907c478bd9Sstevel@tonic-gate 		(void) rib_free_svc_recv(s_recvp);
25910a701b1eSRobert Gordon 
25920a701b1eSRobert Gordon 		return (RDMA_CONNLOST);
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate 	mutex_exit(&conn->c_lock);
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
25977c478bd9Sstevel@tonic-gate }
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate /* Client */
26007c478bd9Sstevel@tonic-gate rdma_stat
26017c478bd9Sstevel@tonic-gate rib_post_resp(CONN* conn, struct clist *cl, uint32_t msgid)
26027c478bd9Sstevel@tonic-gate {
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 	return (rib_clnt_post(conn, cl, msgid));
26057c478bd9Sstevel@tonic-gate }
26067c478bd9Sstevel@tonic-gate 
26070a701b1eSRobert Gordon /* Client */
26080a701b1eSRobert Gordon rdma_stat
26090a701b1eSRobert Gordon rib_post_resp_remove(CONN* conn, uint32_t msgid)
26100a701b1eSRobert Gordon {
26110a701b1eSRobert Gordon 	rib_qp_t	*qp = ctoqp(conn);
26120a701b1eSRobert Gordon 	struct reply	*rep;
26130a701b1eSRobert Gordon 
26140a701b1eSRobert Gordon 	mutex_enter(&qp->replylist_lock);
26150a701b1eSRobert Gordon 	for (rep = qp->replylist; rep != NULL; rep = rep->next) {
26160a701b1eSRobert Gordon 		if (rep->xid == msgid) {
26170a701b1eSRobert Gordon 			if (rep->vaddr_cq) {
26180a701b1eSRobert Gordon 				rib_rbuf_free(conn, RECV_BUFFER,
26190a701b1eSRobert Gordon 				    (caddr_t)(uintptr_t)rep->vaddr_cq);
26200a701b1eSRobert Gordon 			}
26210a701b1eSRobert Gordon 			(void) rib_remreply(qp, rep);
26220a701b1eSRobert Gordon 			break;
26230a701b1eSRobert Gordon 		}
26240a701b1eSRobert Gordon 	}
26250a701b1eSRobert Gordon 	mutex_exit(&qp->replylist_lock);
26260a701b1eSRobert Gordon 
26270a701b1eSRobert Gordon 	return (RDMA_SUCCESS);
26280a701b1eSRobert Gordon }
26290a701b1eSRobert Gordon 
26307c478bd9Sstevel@tonic-gate /* Server */
26317c478bd9Sstevel@tonic-gate rdma_stat
26327c478bd9Sstevel@tonic-gate rib_post_recv(CONN *conn, struct clist *cl)
26337c478bd9Sstevel@tonic-gate {
26347c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp = ctoqp(conn);
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	if (rib_svc_post(conn, cl) == RDMA_SUCCESS) {
26377c478bd9Sstevel@tonic-gate 		mutex_enter(&qp->posted_rbufs_lock);
26387c478bd9Sstevel@tonic-gate 		qp->n_posted_rbufs++;
26397c478bd9Sstevel@tonic-gate 		mutex_exit(&qp->posted_rbufs_lock);
26407c478bd9Sstevel@tonic-gate 		return (RDMA_SUCCESS);
26417c478bd9Sstevel@tonic-gate 	}
26427c478bd9Sstevel@tonic-gate 	return (RDMA_FAILED);
26437c478bd9Sstevel@tonic-gate }
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate /*
26467c478bd9Sstevel@tonic-gate  * Client side only interface to "recv" the rpc reply buf
26477c478bd9Sstevel@tonic-gate  * posted earlier by rib_post_resp(conn, cl, msgid).
26487c478bd9Sstevel@tonic-gate  */
26497c478bd9Sstevel@tonic-gate rdma_stat
26507c478bd9Sstevel@tonic-gate rib_recv(CONN *conn, struct clist **clp, uint32_t msgid)
26517c478bd9Sstevel@tonic-gate {
26527c478bd9Sstevel@tonic-gate 	struct reply *rep = NULL;
26537c478bd9Sstevel@tonic-gate 	clock_t timout, cv_wait_ret;
26547c478bd9Sstevel@tonic-gate 	rdma_stat ret = RDMA_SUCCESS;
26557c478bd9Sstevel@tonic-gate 	rib_qp_t *qp = ctoqp(conn);
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	/*
26587c478bd9Sstevel@tonic-gate 	 * Find the reply structure for this msgid
26597c478bd9Sstevel@tonic-gate 	 */
26607c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->replylist_lock);
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	for (rep = qp->replylist; rep != NULL; rep = rep->next) {
26637c478bd9Sstevel@tonic-gate 		if (rep->xid == msgid)
26647c478bd9Sstevel@tonic-gate 			break;
26657c478bd9Sstevel@tonic-gate 	}
26660a701b1eSRobert Gordon 
26677c478bd9Sstevel@tonic-gate 	if (rep != NULL) {
26687c478bd9Sstevel@tonic-gate 		/*
26697c478bd9Sstevel@tonic-gate 		 * If message not yet received, wait.
26707c478bd9Sstevel@tonic-gate 		 */
26717c478bd9Sstevel@tonic-gate 		if (rep->status == (uint_t)REPLY_WAIT) {
26727c478bd9Sstevel@tonic-gate 			timout = ddi_get_lbolt() +
26737c478bd9Sstevel@tonic-gate 			    drv_usectohz(REPLY_WAIT_TIME * 1000000);
26740a701b1eSRobert Gordon 
26757c478bd9Sstevel@tonic-gate 			while ((cv_wait_ret = cv_timedwait_sig(&rep->wait_cv,
26767c478bd9Sstevel@tonic-gate 			    &qp->replylist_lock, timout)) > 0 &&
26770a701b1eSRobert Gordon 			    rep->status == (uint_t)REPLY_WAIT)
26780a701b1eSRobert Gordon 				;
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 			switch (cv_wait_ret) {
26817c478bd9Sstevel@tonic-gate 			case -1:	/* timeout */
26827c478bd9Sstevel@tonic-gate 				ret = RDMA_TIMEDOUT;
26837c478bd9Sstevel@tonic-gate 				break;
26847c478bd9Sstevel@tonic-gate 			case 0:
26857c478bd9Sstevel@tonic-gate 				ret = RDMA_INTR;
26867c478bd9Sstevel@tonic-gate 				break;
26877c478bd9Sstevel@tonic-gate 			default:
26887c478bd9Sstevel@tonic-gate 				break;
26897c478bd9Sstevel@tonic-gate 			}
26907c478bd9Sstevel@tonic-gate 		}
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 		if (rep->status == RDMA_SUCCESS) {
26937c478bd9Sstevel@tonic-gate 			struct clist *cl = NULL;
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 			/*
26967c478bd9Sstevel@tonic-gate 			 * Got message successfully
26977c478bd9Sstevel@tonic-gate 			 */
26987c478bd9Sstevel@tonic-gate 			clist_add(&cl, 0, rep->bytes_xfer, NULL,
269911606941Sjwahlig 			    (caddr_t)(uintptr_t)rep->vaddr_cq, NULL, NULL);
27007c478bd9Sstevel@tonic-gate 			*clp = cl;
27017c478bd9Sstevel@tonic-gate 		} else {
27027c478bd9Sstevel@tonic-gate 			if (rep->status != (uint_t)REPLY_WAIT) {
27037c478bd9Sstevel@tonic-gate 				/*
27047c478bd9Sstevel@tonic-gate 				 * Got error in reply message. Free
27057c478bd9Sstevel@tonic-gate 				 * recv buffer here.
27067c478bd9Sstevel@tonic-gate 				 */
27077c478bd9Sstevel@tonic-gate 				ret = rep->status;
27087c478bd9Sstevel@tonic-gate 				rib_rbuf_free(conn, RECV_BUFFER,
270911606941Sjwahlig 				    (caddr_t)(uintptr_t)rep->vaddr_cq);
27107c478bd9Sstevel@tonic-gate 			}
27117c478bd9Sstevel@tonic-gate 		}
27127c478bd9Sstevel@tonic-gate 		(void) rib_remreply(qp, rep);
27137c478bd9Sstevel@tonic-gate 	} else {
27147c478bd9Sstevel@tonic-gate 		/*
27157c478bd9Sstevel@tonic-gate 		 * No matching reply structure found for given msgid on the
27167c478bd9Sstevel@tonic-gate 		 * reply wait list.
27177c478bd9Sstevel@tonic-gate 		 */
27187c478bd9Sstevel@tonic-gate 		ret = RDMA_INVAL;
27190a701b1eSRobert Gordon 		DTRACE_PROBE(rpcib__i__nomatchxid2);
27207c478bd9Sstevel@tonic-gate 	}
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	/*
27237c478bd9Sstevel@tonic-gate 	 * Done.
27247c478bd9Sstevel@tonic-gate 	 */
27257c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->replylist_lock);
27267c478bd9Sstevel@tonic-gate 	return (ret);
27277c478bd9Sstevel@tonic-gate }
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate /*
27307c478bd9Sstevel@tonic-gate  * RDMA write a buffer to the remote address.
27317c478bd9Sstevel@tonic-gate  */
27327c478bd9Sstevel@tonic-gate rdma_stat
27337c478bd9Sstevel@tonic-gate rib_write(CONN *conn, struct clist *cl, int wait)
27347c478bd9Sstevel@tonic-gate {
27357c478bd9Sstevel@tonic-gate 	ibt_send_wr_t	tx_wr;
27367c478bd9Sstevel@tonic-gate 	int		cv_sig;
27377c478bd9Sstevel@tonic-gate 	ibt_wr_ds_t	sgl[DSEG_MAX];
27387c478bd9Sstevel@tonic-gate 	struct send_wid	*wdesc;
27397c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
27407c478bd9Sstevel@tonic-gate 	rdma_stat	ret = RDMA_SUCCESS;
27417c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp = ctoqp(conn);
27420a701b1eSRobert Gordon 	uint64_t	n_writes = 0;
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	if (cl == NULL) {
27457c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27480a701b1eSRobert Gordon 	while ((cl != NULL)) {
27490a701b1eSRobert Gordon 		if (cl->c_len > 0) {
27507c478bd9Sstevel@tonic-gate 			bzero(&tx_wr, sizeof (ibt_send_wr_t));
27510a701b1eSRobert Gordon 			tx_wr.wr.rc.rcwr.rdma.rdma_raddr = cl->u.c_daddr;
27520a701b1eSRobert Gordon 			tx_wr.wr.rc.rcwr.rdma.rdma_rkey =
27530a701b1eSRobert Gordon 			    cl->c_dmemhandle.mrc_rmr; /* rkey */
27540a701b1eSRobert Gordon 			sgl[0].ds_va = cl->w.c_saddr;
27550a701b1eSRobert Gordon 			sgl[0].ds_key = cl->c_smemhandle.mrc_lmr; /* lkey */
27560a701b1eSRobert Gordon 			sgl[0].ds_len = cl->c_len;
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 			if (wait) {
27597c478bd9Sstevel@tonic-gate 				cv_sig = 1;
27607c478bd9Sstevel@tonic-gate 			} else {
27610a701b1eSRobert Gordon 				if (n_writes > max_unsignaled_rws) {
27620a701b1eSRobert Gordon 					n_writes = 0;
27630a701b1eSRobert Gordon 					cv_sig = 1;
27640a701b1eSRobert Gordon 				} else {
27657c478bd9Sstevel@tonic-gate 					cv_sig = 0;
27667c478bd9Sstevel@tonic-gate 				}
27670a701b1eSRobert Gordon 			}
27687c478bd9Sstevel@tonic-gate 
2769065714dcSSiddheshwar Mahesh 			if (cv_sig) {
2770065714dcSSiddheshwar Mahesh 				tx_wr.wr_flags = IBT_WR_SEND_SIGNAL;
27717c478bd9Sstevel@tonic-gate 				wdesc = rib_init_sendwait(0, cv_sig, qp);
277211606941Sjwahlig 				tx_wr.wr_id = (ibt_wrid_t)(uintptr_t)wdesc;
2773065714dcSSiddheshwar Mahesh 				mutex_enter(&wdesc->sendwait_lock);
2774065714dcSSiddheshwar Mahesh 			} else {
2775065714dcSSiddheshwar Mahesh 				tx_wr.wr_flags = IBT_WR_NO_FLAGS;
2776065714dcSSiddheshwar Mahesh 				tx_wr.wr_id = (ibt_wrid_t)RDMA_DUMMY_WRID;
2777065714dcSSiddheshwar Mahesh 			}
27787c478bd9Sstevel@tonic-gate 			tx_wr.wr_opcode = IBT_WRC_RDMAW;
27797c478bd9Sstevel@tonic-gate 			tx_wr.wr_trans = IBT_RC_SRV;
27800a701b1eSRobert Gordon 			tx_wr.wr_nds = 1;
27817c478bd9Sstevel@tonic-gate 			tx_wr.wr_sgl = sgl;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 			mutex_enter(&conn->c_lock);
27840a701b1eSRobert Gordon 			if (conn->c_state == C_CONNECTED) {
27850a701b1eSRobert Gordon 				ibt_status =
27860a701b1eSRobert Gordon 				    ibt_post_send(qp->qp_hdl, &tx_wr, 1, NULL);
27877c478bd9Sstevel@tonic-gate 			}
27880a701b1eSRobert Gordon 			if (conn->c_state != C_CONNECTED ||
27897c478bd9Sstevel@tonic-gate 			    ibt_status != IBT_SUCCESS) {
27900a701b1eSRobert Gordon 				if (conn->c_state != C_DISCONN_PEND)
27910a701b1eSRobert Gordon 					conn->c_state = C_ERROR_CONN;
27927c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
2793065714dcSSiddheshwar Mahesh 				if (cv_sig) {
2794065714dcSSiddheshwar Mahesh 					mutex_exit(&wdesc->sendwait_lock);
27957c478bd9Sstevel@tonic-gate 					(void) rib_free_sendwait(wdesc);
2796065714dcSSiddheshwar Mahesh 				}
27970a701b1eSRobert Gordon 				return (RDMA_CONNLOST);
27987c478bd9Sstevel@tonic-gate 			}
2799065714dcSSiddheshwar Mahesh 
28007c478bd9Sstevel@tonic-gate 			mutex_exit(&conn->c_lock);
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 			/*
28037c478bd9Sstevel@tonic-gate 			 * Wait for send to complete
28047c478bd9Sstevel@tonic-gate 			 */
2805065714dcSSiddheshwar Mahesh 			if (cv_sig) {
2806065714dcSSiddheshwar Mahesh 
2807065714dcSSiddheshwar Mahesh 				rib_send_hold(qp);
28080a701b1eSRobert Gordon 				mutex_exit(&wdesc->sendwait_lock);
2809065714dcSSiddheshwar Mahesh 
2810065714dcSSiddheshwar Mahesh 				ret = rib_sendwait(qp, wdesc);
2811065714dcSSiddheshwar Mahesh 				if (ret != 0)
2812065714dcSSiddheshwar Mahesh 					return (ret);
28130a701b1eSRobert Gordon 			}
28140a701b1eSRobert Gordon 			n_writes ++;
28150a701b1eSRobert Gordon 		}
28160a701b1eSRobert Gordon 		cl = cl->c_next;
28177c478bd9Sstevel@tonic-gate 	}
28187c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
28197c478bd9Sstevel@tonic-gate }
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate /*
28227c478bd9Sstevel@tonic-gate  * RDMA Read a buffer from the remote address.
28237c478bd9Sstevel@tonic-gate  */
28247c478bd9Sstevel@tonic-gate rdma_stat
28257c478bd9Sstevel@tonic-gate rib_read(CONN *conn, struct clist *cl, int wait)
28267c478bd9Sstevel@tonic-gate {
28277c478bd9Sstevel@tonic-gate 	ibt_send_wr_t	rx_wr;
2828065714dcSSiddheshwar Mahesh 	int		cv_sig = 0;
28290a701b1eSRobert Gordon 	ibt_wr_ds_t	sgl;
28307c478bd9Sstevel@tonic-gate 	struct send_wid	*wdesc;
28317c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status = IBT_SUCCESS;
28327c478bd9Sstevel@tonic-gate 	rdma_stat	ret = RDMA_SUCCESS;
28337c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp = ctoqp(conn);
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	if (cl == NULL) {
28367c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
28377c478bd9Sstevel@tonic-gate 	}
28387c478bd9Sstevel@tonic-gate 
28390a701b1eSRobert Gordon 	while (cl != NULL) {
28407c478bd9Sstevel@tonic-gate 		bzero(&rx_wr, sizeof (ibt_send_wr_t));
28417c478bd9Sstevel@tonic-gate 		/*
28427c478bd9Sstevel@tonic-gate 		 * Remote address is at the head chunk item in list.
28437c478bd9Sstevel@tonic-gate 		 */
28440a701b1eSRobert Gordon 		rx_wr.wr.rc.rcwr.rdma.rdma_raddr = cl->w.c_saddr;
28450a701b1eSRobert Gordon 		rx_wr.wr.rc.rcwr.rdma.rdma_rkey = cl->c_smemhandle.mrc_rmr;
28467c478bd9Sstevel@tonic-gate 
28470a701b1eSRobert Gordon 		sgl.ds_va = cl->u.c_daddr;
28480a701b1eSRobert Gordon 		sgl.ds_key = cl->c_dmemhandle.mrc_lmr; /* lkey */
28490a701b1eSRobert Gordon 		sgl.ds_len = cl->c_len;
28507c478bd9Sstevel@tonic-gate 
2851065714dcSSiddheshwar Mahesh 		/*
2852065714dcSSiddheshwar Mahesh 		 * If there are multiple chunks to be read, and
2853065714dcSSiddheshwar Mahesh 		 * wait is set, ask for signal only for the last chunk
2854065714dcSSiddheshwar Mahesh 		 * and wait only on the last chunk. The completion of
2855065714dcSSiddheshwar Mahesh 		 * RDMA_READ on last chunk ensures that reads on all
2856065714dcSSiddheshwar Mahesh 		 * previous chunks are also completed.
2857065714dcSSiddheshwar Mahesh 		 */
2858065714dcSSiddheshwar Mahesh 		if (wait && (cl->c_next == NULL)) {
28597c478bd9Sstevel@tonic-gate 			cv_sig = 1;
2860065714dcSSiddheshwar Mahesh 			wdesc = rib_init_sendwait(0, cv_sig, qp);
2861065714dcSSiddheshwar Mahesh 			rx_wr.wr_flags = IBT_WR_SEND_SIGNAL;
2862065714dcSSiddheshwar Mahesh 			rx_wr.wr_id = (ibt_wrid_t)(uintptr_t)wdesc;
2863065714dcSSiddheshwar Mahesh 			mutex_enter(&wdesc->sendwait_lock);
28647c478bd9Sstevel@tonic-gate 		} else {
28657c478bd9Sstevel@tonic-gate 			rx_wr.wr_flags = IBT_WR_NO_FLAGS;
2866065714dcSSiddheshwar Mahesh 			rx_wr.wr_id = (ibt_wrid_t)RDMA_DUMMY_WRID;
28677c478bd9Sstevel@tonic-gate 		}
28687c478bd9Sstevel@tonic-gate 		rx_wr.wr_opcode = IBT_WRC_RDMAR;
28697c478bd9Sstevel@tonic-gate 		rx_wr.wr_trans = IBT_RC_SRV;
28700a701b1eSRobert Gordon 		rx_wr.wr_nds = 1;
28710a701b1eSRobert Gordon 		rx_wr.wr_sgl = &sgl;
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 		mutex_enter(&conn->c_lock);
28740a701b1eSRobert Gordon 		if (conn->c_state == C_CONNECTED) {
28757c478bd9Sstevel@tonic-gate 			ibt_status = ibt_post_send(qp->qp_hdl, &rx_wr, 1, NULL);
28767c478bd9Sstevel@tonic-gate 		}
28770a701b1eSRobert Gordon 		if (conn->c_state != C_CONNECTED ||
28787c478bd9Sstevel@tonic-gate 		    ibt_status != IBT_SUCCESS) {
28790a701b1eSRobert Gordon 			if (conn->c_state != C_DISCONN_PEND)
28800a701b1eSRobert Gordon 				conn->c_state = C_ERROR_CONN;
28817c478bd9Sstevel@tonic-gate 			mutex_exit(&conn->c_lock);
2882065714dcSSiddheshwar Mahesh 			if (wait && (cl->c_next == NULL)) {
2883065714dcSSiddheshwar Mahesh 				mutex_exit(&wdesc->sendwait_lock);
28847c478bd9Sstevel@tonic-gate 				(void) rib_free_sendwait(wdesc);
2885065714dcSSiddheshwar Mahesh 			}
28860a701b1eSRobert Gordon 			return (RDMA_CONNLOST);
28877c478bd9Sstevel@tonic-gate 		}
2888065714dcSSiddheshwar Mahesh 
28897c478bd9Sstevel@tonic-gate 		mutex_exit(&conn->c_lock);
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 		/*
28920a701b1eSRobert Gordon 		 * Wait for send to complete if this is the
28930a701b1eSRobert Gordon 		 * last item in the list.
28947c478bd9Sstevel@tonic-gate 		 */
28950a701b1eSRobert Gordon 		if (wait && cl->c_next == NULL) {
2896065714dcSSiddheshwar Mahesh 			rib_send_hold(qp);
28970a701b1eSRobert Gordon 			mutex_exit(&wdesc->sendwait_lock);
2898065714dcSSiddheshwar Mahesh 
2899065714dcSSiddheshwar Mahesh 			ret = rib_sendwait(qp, wdesc);
2900065714dcSSiddheshwar Mahesh 
2901065714dcSSiddheshwar Mahesh 			if (ret != 0)
2902065714dcSSiddheshwar Mahesh 				return (ret);
29030a701b1eSRobert Gordon 		}
29040a701b1eSRobert Gordon 		cl = cl->c_next;
29050a701b1eSRobert Gordon 	}
29067c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate /*
29107c478bd9Sstevel@tonic-gate  * rib_srv_cm_handler()
29117c478bd9Sstevel@tonic-gate  *    Connection Manager callback to handle RC connection requests.
29127c478bd9Sstevel@tonic-gate  */
29137c478bd9Sstevel@tonic-gate /* ARGSUSED */
29147c478bd9Sstevel@tonic-gate static ibt_cm_status_t
29157c478bd9Sstevel@tonic-gate rib_srv_cm_handler(void *any, ibt_cm_event_t *event,
29167c478bd9Sstevel@tonic-gate 	ibt_cm_return_args_t *ret_args, void *priv_data,
29177c478bd9Sstevel@tonic-gate 	ibt_priv_data_len_t len)
29187c478bd9Sstevel@tonic-gate {
29197c478bd9Sstevel@tonic-gate 	queue_t		*q;
29207c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp;
29217c478bd9Sstevel@tonic-gate 	rib_hca_t	*hca;
29227c478bd9Sstevel@tonic-gate 	rdma_stat	status = RDMA_SUCCESS;
29237c478bd9Sstevel@tonic-gate 	int		i;
29247c478bd9Sstevel@tonic-gate 	struct clist	cl;
29250a701b1eSRobert Gordon 	rdma_buf_t	rdbuf = {0};
29267c478bd9Sstevel@tonic-gate 	void		*buf = NULL;
29277c478bd9Sstevel@tonic-gate 	CONN		*conn;
29280a701b1eSRobert Gordon 	ibt_ip_cm_info_t	ipinfo;
29290a701b1eSRobert Gordon 	struct sockaddr_in *s;
29300a701b1eSRobert Gordon 	struct sockaddr_in6 *s6;
29310a701b1eSRobert Gordon 	int sin_size = sizeof (struct sockaddr_in);
29320a701b1eSRobert Gordon 	int in_size = sizeof (struct in_addr);
29330a701b1eSRobert Gordon 	int sin6_size = sizeof (struct sockaddr_in6);
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	ASSERT(any != NULL);
29367c478bd9Sstevel@tonic-gate 	ASSERT(event != NULL);
29377c478bd9Sstevel@tonic-gate 
2938*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	hca = (rib_hca_t *)any;
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	/* got a connection request */
29417c478bd9Sstevel@tonic-gate 	switch (event->cm_type) {
29427c478bd9Sstevel@tonic-gate 	case IBT_CM_EVENT_REQ_RCV:
29437c478bd9Sstevel@tonic-gate 		/*
29447c478bd9Sstevel@tonic-gate 		 * If the plugin is in the NO_ACCEPT state, bail out.
29457c478bd9Sstevel@tonic-gate 		 */
29467c478bd9Sstevel@tonic-gate 		mutex_enter(&plugin_state_lock);
29477c478bd9Sstevel@tonic-gate 		if (plugin_state == NO_ACCEPT) {
29487c478bd9Sstevel@tonic-gate 			mutex_exit(&plugin_state_lock);
29497c478bd9Sstevel@tonic-gate 			return (IBT_CM_REJECT);
29507c478bd9Sstevel@tonic-gate 		}
29517c478bd9Sstevel@tonic-gate 		mutex_exit(&plugin_state_lock);
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 		/*
29547c478bd9Sstevel@tonic-gate 		 * Need to send a MRA MAD to CM so that it does not
29557c478bd9Sstevel@tonic-gate 		 * timeout on us.
29567c478bd9Sstevel@tonic-gate 		 */
29577c478bd9Sstevel@tonic-gate 		(void) ibt_cm_delay(IBT_CM_DELAY_REQ, event->cm_session_id,
29587c478bd9Sstevel@tonic-gate 		    event->cm_event.req.req_timeout * 8, NULL, 0);
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 		mutex_enter(&rib_stat->open_hca_lock);
29617c478bd9Sstevel@tonic-gate 		q = rib_stat->q;
29627c478bd9Sstevel@tonic-gate 		mutex_exit(&rib_stat->open_hca_lock);
29630a701b1eSRobert Gordon 
29647c478bd9Sstevel@tonic-gate 		status = rib_svc_create_chan(hca, (caddr_t)q,
29657c478bd9Sstevel@tonic-gate 		    event->cm_event.req.req_prim_hca_port, &qp);
29660a701b1eSRobert Gordon 
29677c478bd9Sstevel@tonic-gate 		if (status) {
29687c478bd9Sstevel@tonic-gate 			return (IBT_CM_REJECT);
29697c478bd9Sstevel@tonic-gate 		}
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 		ret_args->cm_ret.rep.cm_channel = qp->qp_hdl;
29720a701b1eSRobert Gordon 		ret_args->cm_ret.rep.cm_rdma_ra_out = 4;
29730a701b1eSRobert Gordon 		ret_args->cm_ret.rep.cm_rdma_ra_in = 4;
29747c478bd9Sstevel@tonic-gate 		ret_args->cm_ret.rep.cm_rnr_retry_cnt = RNR_RETRIES;
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate 		/*
29777c478bd9Sstevel@tonic-gate 		 * Pre-posts RECV buffers
29787c478bd9Sstevel@tonic-gate 		 */
29797c478bd9Sstevel@tonic-gate 		conn = qptoc(qp);
29807c478bd9Sstevel@tonic-gate 		for (i = 0; i < preposted_rbufs; i++) {
29817c478bd9Sstevel@tonic-gate 			bzero(&rdbuf, sizeof (rdbuf));
29827c478bd9Sstevel@tonic-gate 			rdbuf.type = RECV_BUFFER;
29837c478bd9Sstevel@tonic-gate 			buf = rib_rbuf_alloc(conn, &rdbuf);
29847c478bd9Sstevel@tonic-gate 			if (buf == NULL) {
2985065714dcSSiddheshwar Mahesh 				/*
2986065714dcSSiddheshwar Mahesh 				 * A connection is not established yet.
2987065714dcSSiddheshwar Mahesh 				 * Just flush the channel. Buffers
2988065714dcSSiddheshwar Mahesh 				 * posted till now will error out with
2989065714dcSSiddheshwar Mahesh 				 * IBT_WC_WR_FLUSHED_ERR.
2990065714dcSSiddheshwar Mahesh 				 */
2991065714dcSSiddheshwar Mahesh 				(void) ibt_flush_channel(qp->qp_hdl);
29927c478bd9Sstevel@tonic-gate 				(void) rib_disconnect_channel(conn, NULL);
29937c478bd9Sstevel@tonic-gate 				return (IBT_CM_REJECT);
29947c478bd9Sstevel@tonic-gate 			}
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 			bzero(&cl, sizeof (cl));
29970a701b1eSRobert Gordon 			cl.w.c_saddr3 = (caddr_t)rdbuf.addr;
29987c478bd9Sstevel@tonic-gate 			cl.c_len = rdbuf.len;
29990a701b1eSRobert Gordon 			cl.c_smemhandle.mrc_lmr =
30000a701b1eSRobert Gordon 			    rdbuf.handle.mrc_lmr; /* lkey */
30017c478bd9Sstevel@tonic-gate 			cl.c_next = NULL;
30027c478bd9Sstevel@tonic-gate 			status = rib_post_recv(conn, &cl);
30037c478bd9Sstevel@tonic-gate 			if (status != RDMA_SUCCESS) {
3004065714dcSSiddheshwar Mahesh 				/*
3005065714dcSSiddheshwar Mahesh 				 * A connection is not established yet.
3006065714dcSSiddheshwar Mahesh 				 * Just flush the channel. Buffers
3007065714dcSSiddheshwar Mahesh 				 * posted till now will error out with
3008065714dcSSiddheshwar Mahesh 				 * IBT_WC_WR_FLUSHED_ERR.
3009065714dcSSiddheshwar Mahesh 				 */
3010065714dcSSiddheshwar Mahesh 				(void) ibt_flush_channel(qp->qp_hdl);
30117c478bd9Sstevel@tonic-gate 				(void) rib_disconnect_channel(conn, NULL);
30127c478bd9Sstevel@tonic-gate 				return (IBT_CM_REJECT);
30137c478bd9Sstevel@tonic-gate 			}
30147c478bd9Sstevel@tonic-gate 		}
30157c478bd9Sstevel@tonic-gate 		(void) rib_add_connlist(conn, &hca->srv_conn_list);
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 		/*
30180a701b1eSRobert Gordon 		 * Get the address translation
30197c478bd9Sstevel@tonic-gate 		 */
30207c478bd9Sstevel@tonic-gate 		rw_enter(&hca->state_lock, RW_READER);
30217c478bd9Sstevel@tonic-gate 		if (hca->state == HCA_DETACHED) {
30227c478bd9Sstevel@tonic-gate 			rw_exit(&hca->state_lock);
30237c478bd9Sstevel@tonic-gate 			return (IBT_CM_REJECT);
30247c478bd9Sstevel@tonic-gate 		}
30257c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
30267c478bd9Sstevel@tonic-gate 
30270a701b1eSRobert Gordon 		bzero(&ipinfo, sizeof (ibt_ip_cm_info_t));
30287c478bd9Sstevel@tonic-gate 
30290a701b1eSRobert Gordon 		if (ibt_get_ip_data(event->cm_priv_data_len,
30300a701b1eSRobert Gordon 		    event->cm_priv_data,
30310a701b1eSRobert Gordon 		    &ipinfo) != IBT_SUCCESS) {
30320a701b1eSRobert Gordon 
30330a701b1eSRobert Gordon 			return (IBT_CM_REJECT);
30340a701b1eSRobert Gordon 		}
30350a701b1eSRobert Gordon 
30360a701b1eSRobert Gordon 		switch (ipinfo.src_addr.family) {
30370a701b1eSRobert Gordon 		case AF_INET:
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate 			conn->c_raddr.maxlen =
30407c478bd9Sstevel@tonic-gate 			    conn->c_raddr.len = sin_size;
30410a701b1eSRobert Gordon 			conn->c_raddr.buf = kmem_zalloc(sin_size, KM_SLEEP);
30420a701b1eSRobert Gordon 
30437c478bd9Sstevel@tonic-gate 			s = (struct sockaddr_in *)conn->c_raddr.buf;
30447c478bd9Sstevel@tonic-gate 			s->sin_family = AF_INET;
30457c478bd9Sstevel@tonic-gate 
30460a701b1eSRobert Gordon 			bcopy((void *)&ipinfo.src_addr.un.ip4addr,
30470a701b1eSRobert Gordon 			    &s->sin_addr, in_size);
30480a701b1eSRobert Gordon 
30490a701b1eSRobert Gordon 			break;
30500a701b1eSRobert Gordon 
30510a701b1eSRobert Gordon 		case AF_INET6:
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 			conn->c_raddr.maxlen =
30547c478bd9Sstevel@tonic-gate 			    conn->c_raddr.len = sin6_size;
30550a701b1eSRobert Gordon 			conn->c_raddr.buf = kmem_zalloc(sin6_size, KM_SLEEP);
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 			s6 = (struct sockaddr_in6 *)conn->c_raddr.buf;
30587c478bd9Sstevel@tonic-gate 			s6->sin6_family = AF_INET6;
30590a701b1eSRobert Gordon 			bcopy((void *)&ipinfo.src_addr.un.ip6addr,
30600a701b1eSRobert Gordon 			    &s6->sin6_addr,
30617c478bd9Sstevel@tonic-gate 			    sizeof (struct in6_addr));
30627c478bd9Sstevel@tonic-gate 
30630a701b1eSRobert Gordon 			break;
30640a701b1eSRobert Gordon 
30650a701b1eSRobert Gordon 		default:
30660a701b1eSRobert Gordon 			return (IBT_CM_REJECT);
30677c478bd9Sstevel@tonic-gate 		}
30680a701b1eSRobert Gordon 
30697c478bd9Sstevel@tonic-gate 		break;
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate 	case IBT_CM_EVENT_CONN_CLOSED:
30727c478bd9Sstevel@tonic-gate 	{
30737c478bd9Sstevel@tonic-gate 		CONN		*conn;
30747c478bd9Sstevel@tonic-gate 		rib_qp_t	*qp;
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 		switch (event->cm_event.closed) {
30777c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_DREP_RCVD:
30787c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_DREQ_TIMEOUT:
30797c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_DUP:
30807c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_ABORT:
30817c478bd9Sstevel@tonic-gate 		case IBT_CM_CLOSED_ALREADY:
30827c478bd9Sstevel@tonic-gate 			/*
30837c478bd9Sstevel@tonic-gate 			 * These cases indicate the local end initiated
30847c478bd9Sstevel@tonic-gate 			 * the closing of the channel. Nothing to do here.
30857c478bd9Sstevel@tonic-gate 			 */
30867c478bd9Sstevel@tonic-gate 			break;
30877c478bd9Sstevel@tonic-gate 		default:
30887c478bd9Sstevel@tonic-gate 			/*
30897c478bd9Sstevel@tonic-gate 			 * Reason for CONN_CLOSED event must be one of
30907c478bd9Sstevel@tonic-gate 			 * IBT_CM_CLOSED_DREQ_RCVD or IBT_CM_CLOSED_REJ_RCVD
30917c478bd9Sstevel@tonic-gate 			 * or IBT_CM_CLOSED_STALE. These indicate cases were
30927c478bd9Sstevel@tonic-gate 			 * the remote end is closing the channel. In these
30937c478bd9Sstevel@tonic-gate 			 * cases free the channel and transition to error
30947c478bd9Sstevel@tonic-gate 			 * state
30957c478bd9Sstevel@tonic-gate 			 */
30967c478bd9Sstevel@tonic-gate 			qp = ibt_get_chan_private(event->cm_channel);
30977c478bd9Sstevel@tonic-gate 			conn = qptoc(qp);
30987c478bd9Sstevel@tonic-gate 			mutex_enter(&conn->c_lock);
30997c478bd9Sstevel@tonic-gate 			if (conn->c_state == C_DISCONN_PEND) {
31007c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
31017c478bd9Sstevel@tonic-gate 				break;
31027c478bd9Sstevel@tonic-gate 			}
31030a701b1eSRobert Gordon 			conn->c_state = C_ERROR_CONN;
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate 			/*
31067c478bd9Sstevel@tonic-gate 			 * Free the conn if c_ref goes down to 0
31077c478bd9Sstevel@tonic-gate 			 */
31087c478bd9Sstevel@tonic-gate 			if (conn->c_ref == 0) {
31097c478bd9Sstevel@tonic-gate 				/*
31107c478bd9Sstevel@tonic-gate 				 * Remove from list and free conn
31117c478bd9Sstevel@tonic-gate 				 */
31127c478bd9Sstevel@tonic-gate 				conn->c_state = C_DISCONN_PEND;
31137c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
31147c478bd9Sstevel@tonic-gate 				(void) rib_disconnect_channel(conn,
31157c478bd9Sstevel@tonic-gate 				    &hca->srv_conn_list);
31167c478bd9Sstevel@tonic-gate 			} else {
3117065714dcSSiddheshwar Mahesh 				/*
3118065714dcSSiddheshwar Mahesh 				 * conn will be freed when c_ref goes to 0.
3119065714dcSSiddheshwar Mahesh 				 * Indicate to cleaning thread not to close
3120065714dcSSiddheshwar Mahesh 				 * the connection, but just free the channel.
3121065714dcSSiddheshwar Mahesh 				 */
3122065714dcSSiddheshwar Mahesh 				conn->c_flags |= C_CLOSE_NOTNEEDED;
31237c478bd9Sstevel@tonic-gate 				mutex_exit(&conn->c_lock);
31247c478bd9Sstevel@tonic-gate 			}
31250a701b1eSRobert Gordon 			DTRACE_PROBE(rpcib__i__srvcm_chandisconnect);
31267c478bd9Sstevel@tonic-gate 			break;
31277c478bd9Sstevel@tonic-gate 		}
31287c478bd9Sstevel@tonic-gate 		break;
31297c478bd9Sstevel@tonic-gate 	}
31307c478bd9Sstevel@tonic-gate 	case IBT_CM_EVENT_CONN_EST:
31317c478bd9Sstevel@tonic-gate 		/*
31327c478bd9Sstevel@tonic-gate 		 * RTU received, hence connection established.
31337c478bd9Sstevel@tonic-gate 		 */
31347c478bd9Sstevel@tonic-gate 		if (rib_debug > 1)
31357c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rib_srv_cm_handler: "
31367c478bd9Sstevel@tonic-gate 			    "(CONN_EST) channel established");
31377c478bd9Sstevel@tonic-gate 		break;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	default:
31407c478bd9Sstevel@tonic-gate 		if (rib_debug > 2) {
31417c478bd9Sstevel@tonic-gate 			/* Let CM handle the following events. */
31427c478bd9Sstevel@tonic-gate 			if (event->cm_type == IBT_CM_EVENT_REP_RCV) {
31437c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "rib_srv_cm_handler: "
31447c478bd9Sstevel@tonic-gate 				    "server recv'ed IBT_CM_EVENT_REP_RCV\n");
31457c478bd9Sstevel@tonic-gate 			} else if (event->cm_type == IBT_CM_EVENT_LAP_RCV) {
31467c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "rib_srv_cm_handler: "
31477c478bd9Sstevel@tonic-gate 				    "server recv'ed IBT_CM_EVENT_LAP_RCV\n");
31487c478bd9Sstevel@tonic-gate 			} else if (event->cm_type == IBT_CM_EVENT_MRA_RCV) {
31497c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "rib_srv_cm_handler: "
31507c478bd9Sstevel@tonic-gate 				    "server recv'ed IBT_CM_EVENT_MRA_RCV\n");
31517c478bd9Sstevel@tonic-gate 			} else if (event->cm_type == IBT_CM_EVENT_APR_RCV) {
31527c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "rib_srv_cm_handler: "
31537c478bd9Sstevel@tonic-gate 				    "server recv'ed IBT_CM_EVENT_APR_RCV\n");
31547c478bd9Sstevel@tonic-gate 			} else if (event->cm_type == IBT_CM_EVENT_FAILURE) {
31557c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "rib_srv_cm_handler: "
31567c478bd9Sstevel@tonic-gate 				    "server recv'ed IBT_CM_EVENT_FAILURE\n");
31577c478bd9Sstevel@tonic-gate 			}
31587c478bd9Sstevel@tonic-gate 		}
31590a701b1eSRobert Gordon 		return (IBT_CM_DEFAULT);
31607c478bd9Sstevel@tonic-gate 	}
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	/* accept all other CM messages (i.e. let the CM handle them) */
31637c478bd9Sstevel@tonic-gate 	return (IBT_CM_ACCEPT);
31647c478bd9Sstevel@tonic-gate }
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate static rdma_stat
3167*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rib_register_service(rib_hca_t *hca, int service_type,
3168*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	uint8_t protocol_num, in_port_t dst_port)
31697c478bd9Sstevel@tonic-gate {
31707c478bd9Sstevel@tonic-gate 	ibt_srv_desc_t		sdesc;
31717c478bd9Sstevel@tonic-gate 	ibt_hca_portinfo_t	*port_infop;
31727c478bd9Sstevel@tonic-gate 	ib_svc_id_t		srv_id;
31737c478bd9Sstevel@tonic-gate 	ibt_srv_hdl_t		srv_hdl;
31747c478bd9Sstevel@tonic-gate 	uint_t			port_size;
31750a701b1eSRobert Gordon 	uint_t			pki, i, num_ports, nbinds;
31767c478bd9Sstevel@tonic-gate 	ibt_status_t		ibt_status;
3177*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_service_t		*service;
31787c478bd9Sstevel@tonic-gate 	ib_pkey_t		pkey;
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	/*
31817c478bd9Sstevel@tonic-gate 	 * Query all ports for the given HCA
31827c478bd9Sstevel@tonic-gate 	 */
31837c478bd9Sstevel@tonic-gate 	rw_enter(&hca->state_lock, RW_READER);
31847c478bd9Sstevel@tonic-gate 	if (hca->state != HCA_DETACHED) {
31857c478bd9Sstevel@tonic-gate 		ibt_status = ibt_query_hca_ports(hca->hca_hdl, 0, &port_infop,
31867c478bd9Sstevel@tonic-gate 		    &num_ports, &port_size);
31877c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
31887c478bd9Sstevel@tonic-gate 	} else {
31897c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
31907c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
31917c478bd9Sstevel@tonic-gate 	}
31927c478bd9Sstevel@tonic-gate 	if (ibt_status != IBT_SUCCESS) {
31937c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
31947c478bd9Sstevel@tonic-gate 	}
31957c478bd9Sstevel@tonic-gate 
31960a701b1eSRobert Gordon 	DTRACE_PROBE1(rpcib__i__regservice_numports,
31970a701b1eSRobert Gordon 	    int, num_ports);
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_ports; i++) {
32007c478bd9Sstevel@tonic-gate 		if (port_infop[i].p_linkstate != IBT_PORT_ACTIVE) {
32010a701b1eSRobert Gordon 			DTRACE_PROBE1(rpcib__i__regservice__portinactive,
32020a701b1eSRobert Gordon 			    int, i+1);
32030a701b1eSRobert Gordon 		} else if (port_infop[i].p_linkstate == IBT_PORT_ACTIVE) {
32040a701b1eSRobert Gordon 			DTRACE_PROBE1(rpcib__i__regservice__portactive,
32050a701b1eSRobert Gordon 			    int, i+1);
32067c478bd9Sstevel@tonic-gate 		}
32077c478bd9Sstevel@tonic-gate 	}
32080a701b1eSRobert Gordon 
32097c478bd9Sstevel@tonic-gate 	/*
32107c478bd9Sstevel@tonic-gate 	 * Get all the IP addresses on this system to register the
32117c478bd9Sstevel@tonic-gate 	 * given "service type" on all DNS recognized IP addrs.
32127c478bd9Sstevel@tonic-gate 	 * Each service type such as NFS will have all the systems
32137c478bd9Sstevel@tonic-gate 	 * IP addresses as its different names. For now the only
32147c478bd9Sstevel@tonic-gate 	 * type of service we support in RPCIB is NFS.
32157c478bd9Sstevel@tonic-gate 	 */
3216*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->service_list_lock, RW_WRITER);
32177c478bd9Sstevel@tonic-gate 	/*
32187c478bd9Sstevel@tonic-gate 	 * Start registering and binding service to active
32197c478bd9Sstevel@tonic-gate 	 * on active ports on this HCA.
32207c478bd9Sstevel@tonic-gate 	 */
32217c478bd9Sstevel@tonic-gate 	nbinds = 0;
3222*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	for (service = rib_stat->service_list;
3223*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	    service && (service->srv_type != service_type);
3224*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	    service = service->next)
3225*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		;
32267c478bd9Sstevel@tonic-gate 
3227*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (service == NULL) {
32287c478bd9Sstevel@tonic-gate 		/*
32297c478bd9Sstevel@tonic-gate 		 * We use IP addresses as the service names for
32307c478bd9Sstevel@tonic-gate 		 * service registration.  Register each of them
32317c478bd9Sstevel@tonic-gate 		 * with CM to obtain a svc_id and svc_hdl.  We do not
32327c478bd9Sstevel@tonic-gate 		 * register the service with machine's loopback address.
32337c478bd9Sstevel@tonic-gate 		 */
32347c478bd9Sstevel@tonic-gate 		(void) bzero(&srv_id, sizeof (ib_svc_id_t));
32357c478bd9Sstevel@tonic-gate 		(void) bzero(&srv_hdl, sizeof (ibt_srv_hdl_t));
32367c478bd9Sstevel@tonic-gate 		(void) bzero(&sdesc, sizeof (ibt_srv_desc_t));
32377c478bd9Sstevel@tonic-gate 		sdesc.sd_handler = rib_srv_cm_handler;
32387c478bd9Sstevel@tonic-gate 		sdesc.sd_flags = 0;
32397c478bd9Sstevel@tonic-gate 		ibt_status = ibt_register_service(hca->ibt_clnt_hdl,
3240*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    &sdesc, ibt_get_ip_sid(protocol_num, dst_port),
32410a701b1eSRobert Gordon 		    1, &srv_hdl, &srv_id);
3242*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if ((ibt_status != IBT_SUCCESS) &&
3243*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    (ibt_status != IBT_CM_SERVICE_EXISTS)) {
3244*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&rib_stat->service_list_lock);
3245*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			DTRACE_PROBE1(rpcib__i__regservice__ibtres,
3246*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    int, ibt_status);
3247*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			ibt_free_portinfo(port_infop, port_size);
3248*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			return (RDMA_FAILED);
3249*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
3250*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
3251*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		/*
3252*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * Allocate and prepare a service entry
3253*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 */
3254*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		service = kmem_zalloc(sizeof (rib_service_t), KM_SLEEP);
3255*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
3256*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		service->srv_type = service_type;
3257*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		service->srv_hdl = srv_hdl;
3258*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		service->srv_id = srv_id;
3259*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
3260*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		service->next = rib_stat->service_list;
3261*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_stat->service_list = service;
3262*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		DTRACE_PROBE1(rpcib__i__regservice__new__service,
3263*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    int, service->srv_type);
3264*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	} else {
3265*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		srv_hdl = service->srv_hdl;
3266*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		srv_id = service->srv_id;
3267*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		DTRACE_PROBE1(rpcib__i__regservice__existing__service,
3268*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    int, service->srv_type);
3269*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
32700a701b1eSRobert Gordon 
32717c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_ports; i++) {
3272*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		ibt_sbind_hdl_t		sbp;
3273*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_hca_service_t	*hca_srv;
3274*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		ib_gid_t		gid;
3275*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
32767c478bd9Sstevel@tonic-gate 		if (port_infop[i].p_linkstate != IBT_PORT_ACTIVE)
32777c478bd9Sstevel@tonic-gate 			continue;
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate 		for (pki = 0; pki < port_infop[i].p_pkey_tbl_sz; pki++) {
32807c478bd9Sstevel@tonic-gate 			pkey = port_infop[i].p_pkey_tbl[pki];
32817c478bd9Sstevel@tonic-gate 
3282*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_enter(&hca->bound_services_lock, RW_READER);
3283*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			gid = port_infop[i].p_sgid_tbl[0];
3284*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			for (hca_srv = hca->bound_services; hca_srv;
3285*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    hca_srv = hca_srv->next) {
3286*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				if ((hca_srv->srv_id == service->srv_id) &&
3287*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    (hca_srv->gid.gid_prefix ==
3288*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    gid.gid_prefix) &&
3289*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    (hca_srv->gid.gid_guid == gid.gid_guid))
3290*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					break;
3291*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			}
3292*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&hca->bound_services_lock);
3293*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			if (hca_srv != NULL) {
32947c478bd9Sstevel@tonic-gate 				/*
3295*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				 * port is alreay bound the the service
32967c478bd9Sstevel@tonic-gate 				 */
3297*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				DTRACE_PROBE1(
3298*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    rpcib__i__regservice__already__bound,
3299*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    int, i+1);
3300*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				nbinds++;
33017c478bd9Sstevel@tonic-gate 				continue;
33027c478bd9Sstevel@tonic-gate 			}
33030a701b1eSRobert Gordon 
3304*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			if ((pkey & IBSRM_HB) &&
3305*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    (pkey != IB_PKEY_INVALID_FULL)) {
3306*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
3307*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				sbp = NULL;
3308*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				ibt_status = ibt_bind_service(srv_hdl,
3309*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    gid, NULL, hca, &sbp);
3310*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
3311*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				if (ibt_status == IBT_SUCCESS) {
3312*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					hca_srv = kmem_zalloc(
3313*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					    sizeof (rib_hca_service_t),
3314*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					    KM_SLEEP);
3315*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					hca_srv->srv_id = srv_id;
3316*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					hca_srv->gid = gid;
3317*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					hca_srv->sbind_hdl = sbp;
3318*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
3319*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					rw_enter(&hca->bound_services_lock,
3320*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					    RW_WRITER);
3321*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					hca_srv->next = hca->bound_services;
3322*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					hca->bound_services = hca_srv;
3323*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					rw_exit(&hca->bound_services_lock);
33247c478bd9Sstevel@tonic-gate 					nbinds++;
33257c478bd9Sstevel@tonic-gate 				}
3326*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
3327*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				DTRACE_PROBE1(rpcib__i__regservice__bindres,
3328*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				    int, ibt_status);
33297c478bd9Sstevel@tonic-gate 			}
33307c478bd9Sstevel@tonic-gate 		}
3331*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
3332*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->service_list_lock);
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	ibt_free_portinfo(port_infop, port_size);
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 	if (nbinds == 0) {
33377c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
33387c478bd9Sstevel@tonic-gate 	} else {
33397c478bd9Sstevel@tonic-gate 		/*
33407c478bd9Sstevel@tonic-gate 		 * Put this plugin into accept state, since atleast
33417c478bd9Sstevel@tonic-gate 		 * one registration was successful.
33427c478bd9Sstevel@tonic-gate 		 */
33437c478bd9Sstevel@tonic-gate 		mutex_enter(&plugin_state_lock);
33447c478bd9Sstevel@tonic-gate 		plugin_state = ACCEPT;
33457c478bd9Sstevel@tonic-gate 		mutex_exit(&plugin_state_lock);
33467c478bd9Sstevel@tonic-gate 		return (RDMA_SUCCESS);
33477c478bd9Sstevel@tonic-gate 	}
33487c478bd9Sstevel@tonic-gate }
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate void
33517c478bd9Sstevel@tonic-gate rib_listen(struct rdma_svc_data *rd)
33527c478bd9Sstevel@tonic-gate {
3353*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rdma_stat status;
3354*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	int n_listening = 0;
3355*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_hca_t *hca;
33567c478bd9Sstevel@tonic-gate 
3357*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_enter(&rib_stat->listen_lock);
3358*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	/*
3359*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * if rd parameter is NULL then it means that rib_stat->q is
3360*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * already initialized by a call from RDMA and we just want to
3361*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * add a newly attached HCA to the same listening state as other
3362*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * HCAs.
3363*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 */
3364*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (rd == NULL) {
3365*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (rib_stat->q == NULL) {
3366*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			mutex_exit(&rib_stat->listen_lock);
3367*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			return;
3368*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
3369*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	} else {
3370*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_stat->q = &rd->q;
3371*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
3372*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->hcas_list_lock, RW_READER);
3373*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	for (hca = rib_stat->hcas_list; hca; hca = hca->next) {
33747c478bd9Sstevel@tonic-gate 		/*
33757c478bd9Sstevel@tonic-gate 		 * First check if a hca is still attached
33767c478bd9Sstevel@tonic-gate 		 */
3377*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_enter(&hca->state_lock, RW_READER);
3378*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (hca->state != HCA_INITED) {
3379*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&hca->state_lock);
3380*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			continue;
33817c478bd9Sstevel@tonic-gate 		}
3382*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_exit(&hca->state_lock);
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 		/*
3385*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * Right now the only service type is NFS. Hence
3386*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * force feed this value. Ideally to communicate
3387*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * the service type it should be passed down in
3388*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * rdma_svc_data.
33897c478bd9Sstevel@tonic-gate 		 */
3390*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		status = rib_register_service(hca, NFS,
3391*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    IPPROTO_TCP, nfs_rdma_port);
3392*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (status == RDMA_SUCCESS)
3393*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			n_listening++;
33947c478bd9Sstevel@tonic-gate 	}
3395*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->hcas_list_lock);
3396*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
33977c478bd9Sstevel@tonic-gate 	/*
33987c478bd9Sstevel@tonic-gate 	 * Service active on an HCA, check rd->err_code for more
33997c478bd9Sstevel@tonic-gate 	 * explainable errors.
34007c478bd9Sstevel@tonic-gate 	 */
3401*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (rd) {
3402*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (n_listening > 0) {
34037c478bd9Sstevel@tonic-gate 			rd->active = 1;
3404*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rd->err_code = RDMA_SUCCESS;
3405*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		} else {
3406*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rd->active = 0;
3407*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rd->err_code = RDMA_FAILED;
3408*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		}
3409*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
3410*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_exit(&rib_stat->listen_lock);
34117c478bd9Sstevel@tonic-gate }
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate /* XXXX */
34147c478bd9Sstevel@tonic-gate /* ARGSUSED */
34157c478bd9Sstevel@tonic-gate static void
34167c478bd9Sstevel@tonic-gate rib_listen_stop(struct rdma_svc_data *svcdata)
34177c478bd9Sstevel@tonic-gate {
34187c478bd9Sstevel@tonic-gate 	rib_hca_t		*hca;
34197c478bd9Sstevel@tonic-gate 
3420*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_enter(&rib_stat->listen_lock);
34217c478bd9Sstevel@tonic-gate 	/*
34227c478bd9Sstevel@tonic-gate 	 * KRPC called the RDMATF to stop the listeners, this means
34237c478bd9Sstevel@tonic-gate 	 * stop sending incomming or recieved requests to KRPC master
34247c478bd9Sstevel@tonic-gate 	 * transport handle for RDMA-IB. This is also means that the
34257c478bd9Sstevel@tonic-gate 	 * master transport handle, responsible for us, is going away.
34267c478bd9Sstevel@tonic-gate 	 */
34277c478bd9Sstevel@tonic-gate 	mutex_enter(&plugin_state_lock);
34287c478bd9Sstevel@tonic-gate 	plugin_state = NO_ACCEPT;
34297c478bd9Sstevel@tonic-gate 	if (svcdata != NULL)
34307c478bd9Sstevel@tonic-gate 		svcdata->active = 0;
34317c478bd9Sstevel@tonic-gate 	mutex_exit(&plugin_state_lock);
34327c478bd9Sstevel@tonic-gate 
3433*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->hcas_list_lock, RW_READER);
3434*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	for (hca = rib_stat->hcas_list; hca; hca = hca->next) {
34357c478bd9Sstevel@tonic-gate 		/*
34367c478bd9Sstevel@tonic-gate 		 * First check if a hca is still attached
34377c478bd9Sstevel@tonic-gate 		 */
34387c478bd9Sstevel@tonic-gate 		rw_enter(&hca->state_lock, RW_READER);
3439*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (hca->state == HCA_DETACHED) {
34407c478bd9Sstevel@tonic-gate 			rw_exit(&hca->state_lock);
3441*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			continue;
34427c478bd9Sstevel@tonic-gate 		}
34430a701b1eSRobert Gordon 		rib_close_channels(&hca->srv_conn_list);
34447c478bd9Sstevel@tonic-gate 		rib_stop_services(hca);
34457c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
34467c478bd9Sstevel@tonic-gate 	}
3447*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->hcas_list_lock);
3448*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
3449*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	/*
3450*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * Avoid rib_listen() using the stale q field.
3451*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * This could happen if a port goes up after all services
3452*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * are already unregistered.
3453*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 */
3454*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_stat->q = NULL;
3455*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_exit(&rib_stat->listen_lock);
3456*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States }
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate /*
34597c478bd9Sstevel@tonic-gate  * Traverse the HCA's service list to unbind and deregister services.
3460*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * For each bound service of HCA to be removed, first find the corresponding
3461*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * service handle (srv_hdl) and then unbind the service by calling
3462*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * ibt_unbind_service().
34637c478bd9Sstevel@tonic-gate  */
34647c478bd9Sstevel@tonic-gate static void
34657c478bd9Sstevel@tonic-gate rib_stop_services(rib_hca_t *hca)
34667c478bd9Sstevel@tonic-gate {
3467*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_hca_service_t *srv_list, *to_remove;
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	/*
34707c478bd9Sstevel@tonic-gate 	 * unbind and deregister the services for this service type.
34717c478bd9Sstevel@tonic-gate 	 * Right now there is only one service type. In future it will
34727c478bd9Sstevel@tonic-gate 	 * be passed down to this function.
34737c478bd9Sstevel@tonic-gate 	 */
3474*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&hca->bound_services_lock, RW_READER);
3475*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	srv_list = hca->bound_services;
3476*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	hca->bound_services = NULL;
3477*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&hca->bound_services_lock);
3478*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
34797c478bd9Sstevel@tonic-gate 	while (srv_list != NULL) {
3480*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rib_service_t *sc;
3481*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
34827c478bd9Sstevel@tonic-gate 		to_remove = srv_list;
3483*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		srv_list = to_remove->next;
3484*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_enter(&rib_stat->service_list_lock, RW_READER);
3485*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		for (sc = rib_stat->service_list;
3486*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    sc && (sc->srv_id != to_remove->srv_id);
3487*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    sc = sc->next)
3488*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			;
3489*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		/*
3490*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * if sc is NULL then the service doesn't exist anymore,
3491*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * probably just removed completely through rib_stat.
3492*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 */
3493*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (sc != NULL)
3494*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			(void) ibt_unbind_service(sc->srv_hdl,
3495*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    to_remove->sbind_hdl);
3496*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_exit(&rib_stat->service_list_lock);
3497*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		kmem_free(to_remove, sizeof (rib_hca_service_t));
34987c478bd9Sstevel@tonic-gate 	}
34997c478bd9Sstevel@tonic-gate }
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate static struct svc_recv *
35027c478bd9Sstevel@tonic-gate rib_init_svc_recv(rib_qp_t *qp, ibt_wr_ds_t *sgl)
35037c478bd9Sstevel@tonic-gate {
35047c478bd9Sstevel@tonic-gate 	struct svc_recv	*recvp;
35057c478bd9Sstevel@tonic-gate 
35067c478bd9Sstevel@tonic-gate 	recvp = kmem_zalloc(sizeof (struct svc_recv), KM_SLEEP);
35077c478bd9Sstevel@tonic-gate 	recvp->vaddr = sgl->ds_va;
35087c478bd9Sstevel@tonic-gate 	recvp->qp = qp;
35097c478bd9Sstevel@tonic-gate 	recvp->bytes_xfer = 0;
35107c478bd9Sstevel@tonic-gate 	return (recvp);
35117c478bd9Sstevel@tonic-gate }
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate static int
35147c478bd9Sstevel@tonic-gate rib_free_svc_recv(struct svc_recv *recvp)
35157c478bd9Sstevel@tonic-gate {
35167c478bd9Sstevel@tonic-gate 	kmem_free(recvp, sizeof (*recvp));
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate 	return (0);
35197c478bd9Sstevel@tonic-gate }
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate static struct reply *
35227c478bd9Sstevel@tonic-gate rib_addreplylist(rib_qp_t *qp, uint32_t msgid)
35237c478bd9Sstevel@tonic-gate {
35247c478bd9Sstevel@tonic-gate 	struct reply	*rep;
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	rep = kmem_zalloc(sizeof (struct reply), KM_NOSLEEP);
35287c478bd9Sstevel@tonic-gate 	if (rep == NULL) {
35290a701b1eSRobert Gordon 		DTRACE_PROBE(rpcib__i__addrreply__nomem);
35307c478bd9Sstevel@tonic-gate 		return (NULL);
35317c478bd9Sstevel@tonic-gate 	}
35327c478bd9Sstevel@tonic-gate 	rep->xid = msgid;
35337c478bd9Sstevel@tonic-gate 	rep->vaddr_cq = NULL;
35347c478bd9Sstevel@tonic-gate 	rep->bytes_xfer = 0;
35357c478bd9Sstevel@tonic-gate 	rep->status = (uint_t)REPLY_WAIT;
35367c478bd9Sstevel@tonic-gate 	rep->prev = NULL;
35377c478bd9Sstevel@tonic-gate 	cv_init(&rep->wait_cv, NULL, CV_DEFAULT, NULL);
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->replylist_lock);
35407c478bd9Sstevel@tonic-gate 	if (qp->replylist) {
35417c478bd9Sstevel@tonic-gate 		rep->next = qp->replylist;
35427c478bd9Sstevel@tonic-gate 		qp->replylist->prev = rep;
35437c478bd9Sstevel@tonic-gate 	}
35447c478bd9Sstevel@tonic-gate 	qp->rep_list_size++;
35450a701b1eSRobert Gordon 
35460a701b1eSRobert Gordon 	DTRACE_PROBE1(rpcib__i__addrreply__listsize,
35470a701b1eSRobert Gordon 	    int, qp->rep_list_size);
35480a701b1eSRobert Gordon 
35497c478bd9Sstevel@tonic-gate 	qp->replylist = rep;
35507c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->replylist_lock);
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate 	return (rep);
35537c478bd9Sstevel@tonic-gate }
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate static rdma_stat
35567c478bd9Sstevel@tonic-gate rib_rem_replylist(rib_qp_t *qp)
35577c478bd9Sstevel@tonic-gate {
35587c478bd9Sstevel@tonic-gate 	struct reply	*r, *n;
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->replylist_lock);
35617c478bd9Sstevel@tonic-gate 	for (r = qp->replylist; r != NULL; r = n) {
35627c478bd9Sstevel@tonic-gate 		n = r->next;
35637c478bd9Sstevel@tonic-gate 		(void) rib_remreply(qp, r);
35647c478bd9Sstevel@tonic-gate 	}
35657c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->replylist_lock);
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
35687c478bd9Sstevel@tonic-gate }
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate static int
35717c478bd9Sstevel@tonic-gate rib_remreply(rib_qp_t *qp, struct reply *rep)
35727c478bd9Sstevel@tonic-gate {
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qp->replylist_lock));
35757c478bd9Sstevel@tonic-gate 	if (rep->prev) {
35767c478bd9Sstevel@tonic-gate 		rep->prev->next = rep->next;
35777c478bd9Sstevel@tonic-gate 	}
35787c478bd9Sstevel@tonic-gate 	if (rep->next) {
35797c478bd9Sstevel@tonic-gate 		rep->next->prev = rep->prev;
35807c478bd9Sstevel@tonic-gate 	}
35817c478bd9Sstevel@tonic-gate 	if (qp->replylist == rep)
35827c478bd9Sstevel@tonic-gate 		qp->replylist = rep->next;
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 	cv_destroy(&rep->wait_cv);
35857c478bd9Sstevel@tonic-gate 	qp->rep_list_size--;
35860a701b1eSRobert Gordon 
35870a701b1eSRobert Gordon 	DTRACE_PROBE1(rpcib__i__remreply__listsize,
35880a701b1eSRobert Gordon 	    int, qp->rep_list_size);
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	kmem_free(rep, sizeof (*rep));
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	return (0);
35937c478bd9Sstevel@tonic-gate }
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate rdma_stat
35960a701b1eSRobert Gordon rib_registermem(CONN *conn,  caddr_t adsp, caddr_t buf, uint_t buflen,
35977c478bd9Sstevel@tonic-gate 	struct mrc *buf_handle)
35987c478bd9Sstevel@tonic-gate {
35997c478bd9Sstevel@tonic-gate 	ibt_mr_hdl_t	mr_hdl = NULL;	/* memory region handle */
36007c478bd9Sstevel@tonic-gate 	ibt_mr_desc_t	mr_desc;	/* vaddr, lkey, rkey */
36017c478bd9Sstevel@tonic-gate 	rdma_stat	status;
36027c478bd9Sstevel@tonic-gate 	rib_hca_t	*hca = (ctoqp(conn))->hca;
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	/*
36057c478bd9Sstevel@tonic-gate 	 * Note: ALL buffer pools use the same memory type RDMARW.
36067c478bd9Sstevel@tonic-gate 	 */
36070a701b1eSRobert Gordon 	status = rib_reg_mem(hca, adsp, buf, buflen, 0, &mr_hdl, &mr_desc);
36087c478bd9Sstevel@tonic-gate 	if (status == RDMA_SUCCESS) {
360911606941Sjwahlig 		buf_handle->mrc_linfo = (uintptr_t)mr_hdl;
36107c478bd9Sstevel@tonic-gate 		buf_handle->mrc_lmr = (uint32_t)mr_desc.md_lkey;
36117c478bd9Sstevel@tonic-gate 		buf_handle->mrc_rmr = (uint32_t)mr_desc.md_rkey;
36127c478bd9Sstevel@tonic-gate 	} else {
36137c478bd9Sstevel@tonic-gate 		buf_handle->mrc_linfo = NULL;
36147c478bd9Sstevel@tonic-gate 		buf_handle->mrc_lmr = 0;
36157c478bd9Sstevel@tonic-gate 		buf_handle->mrc_rmr = 0;
36167c478bd9Sstevel@tonic-gate 	}
36177c478bd9Sstevel@tonic-gate 	return (status);
36187c478bd9Sstevel@tonic-gate }
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate static rdma_stat
36210a701b1eSRobert Gordon rib_reg_mem(rib_hca_t *hca, caddr_t adsp, caddr_t buf, uint_t size,
36220a701b1eSRobert Gordon 	ibt_mr_flags_t spec,
36237c478bd9Sstevel@tonic-gate 	ibt_mr_hdl_t *mr_hdlp, ibt_mr_desc_t *mr_descp)
36247c478bd9Sstevel@tonic-gate {
36257c478bd9Sstevel@tonic-gate 	ibt_mr_attr_t	mem_attr;
36267c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
362711606941Sjwahlig 	mem_attr.mr_vaddr = (uintptr_t)buf;
36287c478bd9Sstevel@tonic-gate 	mem_attr.mr_len = (ib_msglen_t)size;
36290a701b1eSRobert Gordon 	mem_attr.mr_as = (struct as *)(caddr_t)adsp;
36307c478bd9Sstevel@tonic-gate 	mem_attr.mr_flags = IBT_MR_SLEEP | IBT_MR_ENABLE_LOCAL_WRITE |
36317c478bd9Sstevel@tonic-gate 	    IBT_MR_ENABLE_REMOTE_READ | IBT_MR_ENABLE_REMOTE_WRITE |
36327c478bd9Sstevel@tonic-gate 	    IBT_MR_ENABLE_WINDOW_BIND | spec;
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 	rw_enter(&hca->state_lock, RW_READER);
3635*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (hca->state != HCA_DETACHED) {
36367c478bd9Sstevel@tonic-gate 		ibt_status = ibt_register_mr(hca->hca_hdl, hca->pd_hdl,
36377c478bd9Sstevel@tonic-gate 		    &mem_attr, mr_hdlp, mr_descp);
36387c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
36397c478bd9Sstevel@tonic-gate 	} else {
36407c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
36417c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
36427c478bd9Sstevel@tonic-gate 	}
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate 	if (ibt_status != IBT_SUCCESS) {
36457c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
36467c478bd9Sstevel@tonic-gate 	}
36477c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
36487c478bd9Sstevel@tonic-gate }
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate rdma_stat
36510a701b1eSRobert Gordon rib_registermemsync(CONN *conn,  caddr_t adsp, caddr_t buf, uint_t buflen,
36520a701b1eSRobert Gordon 	struct mrc *buf_handle, RIB_SYNCMEM_HANDLE *sync_handle, void *lrc)
36537c478bd9Sstevel@tonic-gate {
36547c478bd9Sstevel@tonic-gate 	ibt_mr_hdl_t	mr_hdl = NULL;	/* memory region handle */
36550a701b1eSRobert Gordon 	rib_lrc_entry_t *l;
36567c478bd9Sstevel@tonic-gate 	ibt_mr_desc_t	mr_desc;	/* vaddr, lkey, rkey */
36577c478bd9Sstevel@tonic-gate 	rdma_stat	status;
36587c478bd9Sstevel@tonic-gate 	rib_hca_t	*hca = (ctoqp(conn))->hca;
36597c478bd9Sstevel@tonic-gate 
36607c478bd9Sstevel@tonic-gate 	/*
36617c478bd9Sstevel@tonic-gate 	 * Non-coherent memory registration.
36627c478bd9Sstevel@tonic-gate 	 */
36630a701b1eSRobert Gordon 	l = (rib_lrc_entry_t *)lrc;
36640a701b1eSRobert Gordon 	if (l) {
36650a701b1eSRobert Gordon 		if (l->registered) {
36660a701b1eSRobert Gordon 			buf_handle->mrc_linfo =
36670a701b1eSRobert Gordon 			    (uintptr_t)l->lrc_mhandle.mrc_linfo;
36680a701b1eSRobert Gordon 			buf_handle->mrc_lmr =
36690a701b1eSRobert Gordon 			    (uint32_t)l->lrc_mhandle.mrc_lmr;
36700a701b1eSRobert Gordon 			buf_handle->mrc_rmr =
36710a701b1eSRobert Gordon 			    (uint32_t)l->lrc_mhandle.mrc_rmr;
36720a701b1eSRobert Gordon 			*sync_handle = (RIB_SYNCMEM_HANDLE)
36730a701b1eSRobert Gordon 			    (uintptr_t)l->lrc_mhandle.mrc_linfo;
36740a701b1eSRobert Gordon 			return (RDMA_SUCCESS);
36750a701b1eSRobert Gordon 		} else {
36760a701b1eSRobert Gordon 			/* Always register the whole buffer */
36770a701b1eSRobert Gordon 			buf = (caddr_t)l->lrc_buf;
36780a701b1eSRobert Gordon 			buflen = l->lrc_len;
36790a701b1eSRobert Gordon 		}
36800a701b1eSRobert Gordon 	}
36810a701b1eSRobert Gordon 	status = rib_reg_mem(hca, adsp, buf, buflen, 0, &mr_hdl, &mr_desc);
36820a701b1eSRobert Gordon 
36837c478bd9Sstevel@tonic-gate 	if (status == RDMA_SUCCESS) {
36840a701b1eSRobert Gordon 		if (l) {
36850a701b1eSRobert Gordon 			l->lrc_mhandle.mrc_linfo = (uintptr_t)mr_hdl;
36860a701b1eSRobert Gordon 			l->lrc_mhandle.mrc_lmr   = (uint32_t)mr_desc.md_lkey;
36870a701b1eSRobert Gordon 			l->lrc_mhandle.mrc_rmr   = (uint32_t)mr_desc.md_rkey;
36880a701b1eSRobert Gordon 			l->registered		 = TRUE;
36890a701b1eSRobert Gordon 		}
369011606941Sjwahlig 		buf_handle->mrc_linfo = (uintptr_t)mr_hdl;
36917c478bd9Sstevel@tonic-gate 		buf_handle->mrc_lmr = (uint32_t)mr_desc.md_lkey;
36927c478bd9Sstevel@tonic-gate 		buf_handle->mrc_rmr = (uint32_t)mr_desc.md_rkey;
36937c478bd9Sstevel@tonic-gate 		*sync_handle = (RIB_SYNCMEM_HANDLE)mr_hdl;
36947c478bd9Sstevel@tonic-gate 	} else {
36957c478bd9Sstevel@tonic-gate 		buf_handle->mrc_linfo = NULL;
36967c478bd9Sstevel@tonic-gate 		buf_handle->mrc_lmr = 0;
36977c478bd9Sstevel@tonic-gate 		buf_handle->mrc_rmr = 0;
36987c478bd9Sstevel@tonic-gate 	}
36997c478bd9Sstevel@tonic-gate 	return (status);
37007c478bd9Sstevel@tonic-gate }
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate /* ARGSUSED */
37037c478bd9Sstevel@tonic-gate rdma_stat
37047c478bd9Sstevel@tonic-gate rib_deregistermem(CONN *conn, caddr_t buf, struct mrc buf_handle)
37057c478bd9Sstevel@tonic-gate {
37067c478bd9Sstevel@tonic-gate 	rib_hca_t *hca = (ctoqp(conn))->hca;
37077c478bd9Sstevel@tonic-gate 	/*
37087c478bd9Sstevel@tonic-gate 	 * Allow memory deregistration even if HCA is
37097c478bd9Sstevel@tonic-gate 	 * getting detached. Need all outstanding
37107c478bd9Sstevel@tonic-gate 	 * memory registrations to be deregistered
37117c478bd9Sstevel@tonic-gate 	 * before HCA_DETACH_EVENT can be accepted.
37127c478bd9Sstevel@tonic-gate 	 */
37137c478bd9Sstevel@tonic-gate 	(void) ibt_deregister_mr(hca->hca_hdl,
371411606941Sjwahlig 	    (ibt_mr_hdl_t)(uintptr_t)buf_handle.mrc_linfo);
37157c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
37167c478bd9Sstevel@tonic-gate }
37177c478bd9Sstevel@tonic-gate 
37187c478bd9Sstevel@tonic-gate /* ARGSUSED */
37197c478bd9Sstevel@tonic-gate rdma_stat
37207c478bd9Sstevel@tonic-gate rib_deregistermemsync(CONN *conn, caddr_t buf, struct mrc buf_handle,
37210a701b1eSRobert Gordon 		RIB_SYNCMEM_HANDLE sync_handle, void *lrc)
37227c478bd9Sstevel@tonic-gate {
37230a701b1eSRobert Gordon 	rib_lrc_entry_t *l;
37240a701b1eSRobert Gordon 	l = (rib_lrc_entry_t *)lrc;
37250a701b1eSRobert Gordon 	if (l)
37260a701b1eSRobert Gordon 		if (l->registered)
37270a701b1eSRobert Gordon 			return (RDMA_SUCCESS);
37280a701b1eSRobert Gordon 
37297c478bd9Sstevel@tonic-gate 	(void) rib_deregistermem(conn, buf, buf_handle);
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
37327c478bd9Sstevel@tonic-gate }
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate /* ARGSUSED */
37357c478bd9Sstevel@tonic-gate rdma_stat
37367c478bd9Sstevel@tonic-gate rib_syncmem(CONN *conn, RIB_SYNCMEM_HANDLE shandle, caddr_t buf,
37377c478bd9Sstevel@tonic-gate 		int len, int cpu)
37387c478bd9Sstevel@tonic-gate {
37397c478bd9Sstevel@tonic-gate 	ibt_status_t	status;
37407c478bd9Sstevel@tonic-gate 	rib_hca_t *hca = (ctoqp(conn))->hca;
37417c478bd9Sstevel@tonic-gate 	ibt_mr_sync_t	mr_segment;
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 	mr_segment.ms_handle = (ibt_mr_hdl_t)shandle;
374411606941Sjwahlig 	mr_segment.ms_vaddr = (ib_vaddr_t)(uintptr_t)buf;
37457c478bd9Sstevel@tonic-gate 	mr_segment.ms_len = (ib_memlen_t)len;
37467c478bd9Sstevel@tonic-gate 	if (cpu) {
37477c478bd9Sstevel@tonic-gate 		/* make incoming data visible to memory */
37487c478bd9Sstevel@tonic-gate 		mr_segment.ms_flags = IBT_SYNC_WRITE;
37497c478bd9Sstevel@tonic-gate 	} else {
37507c478bd9Sstevel@tonic-gate 		/* make memory changes visible to IO */
37517c478bd9Sstevel@tonic-gate 		mr_segment.ms_flags = IBT_SYNC_READ;
37527c478bd9Sstevel@tonic-gate 	}
37537c478bd9Sstevel@tonic-gate 	rw_enter(&hca->state_lock, RW_READER);
3754*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (hca->state != HCA_DETACHED) {
37557c478bd9Sstevel@tonic-gate 		status = ibt_sync_mr(hca->hca_hdl, &mr_segment, 1);
37567c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
37577c478bd9Sstevel@tonic-gate 	} else {
37587c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
37597c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
37607c478bd9Sstevel@tonic-gate 	}
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate 	if (status == IBT_SUCCESS)
37637c478bd9Sstevel@tonic-gate 		return (RDMA_SUCCESS);
37647c478bd9Sstevel@tonic-gate 	else {
37657c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
37667c478bd9Sstevel@tonic-gate 	}
37677c478bd9Sstevel@tonic-gate }
37687c478bd9Sstevel@tonic-gate 
37697c478bd9Sstevel@tonic-gate /*
37707c478bd9Sstevel@tonic-gate  * XXXX	????
37717c478bd9Sstevel@tonic-gate  */
37727c478bd9Sstevel@tonic-gate static rdma_stat
37737c478bd9Sstevel@tonic-gate rib_getinfo(rdma_info_t *info)
37747c478bd9Sstevel@tonic-gate {
37757c478bd9Sstevel@tonic-gate 	/*
37767c478bd9Sstevel@tonic-gate 	 * XXXX	Hack!
37777c478bd9Sstevel@tonic-gate 	 */
37787c478bd9Sstevel@tonic-gate 	info->addrlen = 16;
37797c478bd9Sstevel@tonic-gate 	info->mts = 1000000;
37807c478bd9Sstevel@tonic-gate 	info->mtu = 1000000;
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
37837c478bd9Sstevel@tonic-gate }
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate rib_bufpool_t *
37867c478bd9Sstevel@tonic-gate rib_rbufpool_create(rib_hca_t *hca, int ptype, int num)
37877c478bd9Sstevel@tonic-gate {
37887c478bd9Sstevel@tonic-gate 	rib_bufpool_t	*rbp = NULL;
37897c478bd9Sstevel@tonic-gate 	bufpool_t	*bp = NULL;
37907c478bd9Sstevel@tonic-gate 	caddr_t		buf;
37917c478bd9Sstevel@tonic-gate 	ibt_mr_attr_t	mem_attr;
37927c478bd9Sstevel@tonic-gate 	ibt_status_t	ibt_status;
37937c478bd9Sstevel@tonic-gate 	int		i, j;
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	rbp = (rib_bufpool_t *)kmem_zalloc(sizeof (rib_bufpool_t), KM_SLEEP);
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate 	bp = (bufpool_t *)kmem_zalloc(sizeof (bufpool_t) +
37987c478bd9Sstevel@tonic-gate 	    num * sizeof (void *), KM_SLEEP);
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	mutex_init(&bp->buflock, NULL, MUTEX_DRIVER, hca->iblock);
38017c478bd9Sstevel@tonic-gate 	bp->numelems = num;
38027c478bd9Sstevel@tonic-gate 
38030a701b1eSRobert Gordon 
38047c478bd9Sstevel@tonic-gate 	switch (ptype) {
38057c478bd9Sstevel@tonic-gate 	case SEND_BUFFER:
38067c478bd9Sstevel@tonic-gate 		mem_attr.mr_flags = IBT_MR_SLEEP | IBT_MR_ENABLE_LOCAL_WRITE;
38077c478bd9Sstevel@tonic-gate 		bp->rsize = RPC_MSG_SZ;
38087c478bd9Sstevel@tonic-gate 		break;
38097c478bd9Sstevel@tonic-gate 	case RECV_BUFFER:
38107c478bd9Sstevel@tonic-gate 		mem_attr.mr_flags = IBT_MR_SLEEP | IBT_MR_ENABLE_LOCAL_WRITE;
38117c478bd9Sstevel@tonic-gate 		bp->rsize = RPC_BUF_SIZE;
38127c478bd9Sstevel@tonic-gate 		break;
38137c478bd9Sstevel@tonic-gate 	default:
38147c478bd9Sstevel@tonic-gate 		goto fail;
38157c478bd9Sstevel@tonic-gate 	}
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate 	/*
38187c478bd9Sstevel@tonic-gate 	 * Register the pool.
38197c478bd9Sstevel@tonic-gate 	 */
38207c478bd9Sstevel@tonic-gate 	bp->bufsize = num * bp->rsize;
38217c478bd9Sstevel@tonic-gate 	bp->buf = kmem_zalloc(bp->bufsize, KM_SLEEP);
38227c478bd9Sstevel@tonic-gate 	rbp->mr_hdl = (ibt_mr_hdl_t *)kmem_zalloc(num *
38237c478bd9Sstevel@tonic-gate 	    sizeof (ibt_mr_hdl_t), KM_SLEEP);
38247c478bd9Sstevel@tonic-gate 	rbp->mr_desc = (ibt_mr_desc_t *)kmem_zalloc(num *
38257c478bd9Sstevel@tonic-gate 	    sizeof (ibt_mr_desc_t), KM_SLEEP);
38267c478bd9Sstevel@tonic-gate 	rw_enter(&hca->state_lock, RW_READER);
38270a701b1eSRobert Gordon 
3828*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (hca->state == HCA_DETACHED) {
38297c478bd9Sstevel@tonic-gate 		rw_exit(&hca->state_lock);
38307c478bd9Sstevel@tonic-gate 		goto fail;
38317c478bd9Sstevel@tonic-gate 	}
38320a701b1eSRobert Gordon 
38337c478bd9Sstevel@tonic-gate 	for (i = 0, buf = bp->buf; i < num; i++, buf += bp->rsize) {
38347c478bd9Sstevel@tonic-gate 		bzero(&rbp->mr_desc[i], sizeof (ibt_mr_desc_t));
383511606941Sjwahlig 		mem_attr.mr_vaddr = (uintptr_t)buf;
38367c478bd9Sstevel@tonic-gate 		mem_attr.mr_len = (ib_msglen_t)bp->rsize;
38377c478bd9Sstevel@tonic-gate 		mem_attr.mr_as = NULL;
38387c478bd9Sstevel@tonic-gate 		ibt_status = ibt_register_mr(hca->hca_hdl,
38390a701b1eSRobert Gordon 		    hca->pd_hdl, &mem_attr,
38400a701b1eSRobert Gordon 		    &rbp->mr_hdl[i],
38417c478bd9Sstevel@tonic-gate 		    &rbp->mr_desc[i]);
38427c478bd9Sstevel@tonic-gate 		if (ibt_status != IBT_SUCCESS) {
38437c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; j++) {
38440a701b1eSRobert Gordon 				(void) ibt_deregister_mr(hca->hca_hdl,
38450a701b1eSRobert Gordon 				    rbp->mr_hdl[j]);
38467c478bd9Sstevel@tonic-gate 			}
38477c478bd9Sstevel@tonic-gate 			rw_exit(&hca->state_lock);
38487c478bd9Sstevel@tonic-gate 			goto fail;
38497c478bd9Sstevel@tonic-gate 		}
38507c478bd9Sstevel@tonic-gate 	}
38517c478bd9Sstevel@tonic-gate 	rw_exit(&hca->state_lock);
38527c478bd9Sstevel@tonic-gate 	buf = (caddr_t)bp->buf;
38537c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, buf += bp->rsize) {
38547c478bd9Sstevel@tonic-gate 		bp->buflist[i] = (void *)buf;
38557c478bd9Sstevel@tonic-gate 	}
38567c478bd9Sstevel@tonic-gate 	bp->buffree = num - 1;	/* no. of free buffers */
38577c478bd9Sstevel@tonic-gate 	rbp->bpool = bp;
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate 	return (rbp);
38607c478bd9Sstevel@tonic-gate fail:
38617c478bd9Sstevel@tonic-gate 	if (bp) {
38627c478bd9Sstevel@tonic-gate 		if (bp->buf)
38637c478bd9Sstevel@tonic-gate 			kmem_free(bp->buf, bp->bufsize);
38647c478bd9Sstevel@tonic-gate 		kmem_free(bp, sizeof (bufpool_t) + num*sizeof (void *));
38657c478bd9Sstevel@tonic-gate 	}
38667c478bd9Sstevel@tonic-gate 	if (rbp) {
38677c478bd9Sstevel@tonic-gate 		if (rbp->mr_hdl)
38687c478bd9Sstevel@tonic-gate 			kmem_free(rbp->mr_hdl, num*sizeof (ibt_mr_hdl_t));
38697c478bd9Sstevel@tonic-gate 		if (rbp->mr_desc)
38707c478bd9Sstevel@tonic-gate 			kmem_free(rbp->mr_desc, num*sizeof (ibt_mr_desc_t));
38717c478bd9Sstevel@tonic-gate 		kmem_free(rbp, sizeof (rib_bufpool_t));
38727c478bd9Sstevel@tonic-gate 	}
38737c478bd9Sstevel@tonic-gate 	return (NULL);
38747c478bd9Sstevel@tonic-gate }
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate static void
38777c478bd9Sstevel@tonic-gate rib_rbufpool_deregister(rib_hca_t *hca, int ptype)
38787c478bd9Sstevel@tonic-gate {
38797c478bd9Sstevel@tonic-gate 	int i;
38807c478bd9Sstevel@tonic-gate 	rib_bufpool_t *rbp = NULL;
38817c478bd9Sstevel@tonic-gate 	bufpool_t *bp;
38827c478bd9Sstevel@tonic-gate 
38837c478bd9Sstevel@tonic-gate 	/*
38847c478bd9Sstevel@tonic-gate 	 * Obtain pool address based on type of pool
38857c478bd9Sstevel@tonic-gate 	 */
38867c478bd9Sstevel@tonic-gate 	switch (ptype) {
38877c478bd9Sstevel@tonic-gate 		case SEND_BUFFER:
38887c478bd9Sstevel@tonic-gate 			rbp = hca->send_pool;
38897c478bd9Sstevel@tonic-gate 			break;
38907c478bd9Sstevel@tonic-gate 		case RECV_BUFFER:
38917c478bd9Sstevel@tonic-gate 			rbp = hca->recv_pool;
38927c478bd9Sstevel@tonic-gate 			break;
38937c478bd9Sstevel@tonic-gate 		default:
38947c478bd9Sstevel@tonic-gate 			return;
38957c478bd9Sstevel@tonic-gate 	}
38967c478bd9Sstevel@tonic-gate 	if (rbp == NULL)
38977c478bd9Sstevel@tonic-gate 		return;
38987c478bd9Sstevel@tonic-gate 
38997c478bd9Sstevel@tonic-gate 	bp = rbp->bpool;
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 	/*
39027c478bd9Sstevel@tonic-gate 	 * Deregister the pool memory and free it.
39037c478bd9Sstevel@tonic-gate 	 */
39047c478bd9Sstevel@tonic-gate 	for (i = 0; i < bp->numelems; i++) {
39057c478bd9Sstevel@tonic-gate 		(void) ibt_deregister_mr(hca->hca_hdl, rbp->mr_hdl[i]);
39067c478bd9Sstevel@tonic-gate 	}
39077c478bd9Sstevel@tonic-gate }
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate static void
39107c478bd9Sstevel@tonic-gate rib_rbufpool_free(rib_hca_t *hca, int ptype)
39117c478bd9Sstevel@tonic-gate {
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate 	rib_bufpool_t *rbp = NULL;
39147c478bd9Sstevel@tonic-gate 	bufpool_t *bp;
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	/*
39177c478bd9Sstevel@tonic-gate 	 * Obtain pool address based on type of pool
39187c478bd9Sstevel@tonic-gate 	 */
39197c478bd9Sstevel@tonic-gate 	switch (ptype) {
39207c478bd9Sstevel@tonic-gate 		case SEND_BUFFER:
39217c478bd9Sstevel@tonic-gate 			rbp = hca->send_pool;
39227c478bd9Sstevel@tonic-gate 			break;
39237c478bd9Sstevel@tonic-gate 		case RECV_BUFFER:
39247c478bd9Sstevel@tonic-gate 			rbp = hca->recv_pool;
39257c478bd9Sstevel@tonic-gate 			break;
39267c478bd9Sstevel@tonic-gate 		default:
39277c478bd9Sstevel@tonic-gate 			return;
39287c478bd9Sstevel@tonic-gate 	}
39297c478bd9Sstevel@tonic-gate 	if (rbp == NULL)
39307c478bd9Sstevel@tonic-gate 		return;
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 	bp = rbp->bpool;
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 	/*
39357c478bd9Sstevel@tonic-gate 	 * Free the pool memory.
39367c478bd9Sstevel@tonic-gate 	 */
39377c478bd9Sstevel@tonic-gate 	if (rbp->mr_hdl)
39387c478bd9Sstevel@tonic-gate 		kmem_free(rbp->mr_hdl, bp->numelems*sizeof (ibt_mr_hdl_t));
39397c478bd9Sstevel@tonic-gate 
39407c478bd9Sstevel@tonic-gate 	if (rbp->mr_desc)
39417c478bd9Sstevel@tonic-gate 		kmem_free(rbp->mr_desc, bp->numelems*sizeof (ibt_mr_desc_t));
39427c478bd9Sstevel@tonic-gate 	if (bp->buf)
39437c478bd9Sstevel@tonic-gate 		kmem_free(bp->buf, bp->bufsize);
39447c478bd9Sstevel@tonic-gate 	mutex_destroy(&bp->buflock);
39457c478bd9Sstevel@tonic-gate 	kmem_free(bp, sizeof (bufpool_t) + bp->numelems*sizeof (void *));
39467c478bd9Sstevel@tonic-gate 	kmem_free(rbp, sizeof (rib_bufpool_t));
39477c478bd9Sstevel@tonic-gate }
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate void
39507c478bd9Sstevel@tonic-gate rib_rbufpool_destroy(rib_hca_t *hca, int ptype)
39517c478bd9Sstevel@tonic-gate {
39527c478bd9Sstevel@tonic-gate 	/*
39537c478bd9Sstevel@tonic-gate 	 * Deregister the pool memory and free it.
39547c478bd9Sstevel@tonic-gate 	 */
39557c478bd9Sstevel@tonic-gate 	rib_rbufpool_deregister(hca, ptype);
39567c478bd9Sstevel@tonic-gate 	rib_rbufpool_free(hca, ptype);
39577c478bd9Sstevel@tonic-gate }
39587c478bd9Sstevel@tonic-gate 
39597c478bd9Sstevel@tonic-gate /*
39607c478bd9Sstevel@tonic-gate  * Fetch a buffer from the pool of type specified in rdbuf->type.
39617c478bd9Sstevel@tonic-gate  */
39627c478bd9Sstevel@tonic-gate static rdma_stat
39637c478bd9Sstevel@tonic-gate rib_reg_buf_alloc(CONN *conn, rdma_buf_t *rdbuf)
39647c478bd9Sstevel@tonic-gate {
39650a701b1eSRobert Gordon 	rib_lrc_entry_t *rlep;
39660a701b1eSRobert Gordon 
39670a701b1eSRobert Gordon 	if (rdbuf->type ==  RDMA_LONG_BUFFER) {
39680a701b1eSRobert Gordon 		rlep = rib_get_cache_buf(conn, rdbuf->len);
39690a701b1eSRobert Gordon 		rdbuf->rb_private =  (caddr_t)rlep;
39700a701b1eSRobert Gordon 		rdbuf->addr = rlep->lrc_buf;
39710a701b1eSRobert Gordon 		rdbuf->handle = rlep->lrc_mhandle;
39720a701b1eSRobert Gordon 		return (RDMA_SUCCESS);
39730a701b1eSRobert Gordon 	}
39747c478bd9Sstevel@tonic-gate 
39757c478bd9Sstevel@tonic-gate 	rdbuf->addr = rib_rbuf_alloc(conn, rdbuf);
39767c478bd9Sstevel@tonic-gate 	if (rdbuf->addr) {
39777c478bd9Sstevel@tonic-gate 		switch (rdbuf->type) {
39787c478bd9Sstevel@tonic-gate 		case SEND_BUFFER:
39797c478bd9Sstevel@tonic-gate 			rdbuf->len = RPC_MSG_SZ;	/* 1K */
39807c478bd9Sstevel@tonic-gate 			break;
39817c478bd9Sstevel@tonic-gate 		case RECV_BUFFER:
39827c478bd9Sstevel@tonic-gate 			rdbuf->len = RPC_BUF_SIZE; /* 2K */
39837c478bd9Sstevel@tonic-gate 			break;
39847c478bd9Sstevel@tonic-gate 		default:
39857c478bd9Sstevel@tonic-gate 			rdbuf->len = 0;
39867c478bd9Sstevel@tonic-gate 		}
39877c478bd9Sstevel@tonic-gate 		return (RDMA_SUCCESS);
39887c478bd9Sstevel@tonic-gate 	} else
39897c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
39907c478bd9Sstevel@tonic-gate }
39917c478bd9Sstevel@tonic-gate 
39927c478bd9Sstevel@tonic-gate /*
39937c478bd9Sstevel@tonic-gate  * Fetch a buffer of specified type.
39947c478bd9Sstevel@tonic-gate  * Note that rdbuf->handle is mw's rkey.
39957c478bd9Sstevel@tonic-gate  */
39967c478bd9Sstevel@tonic-gate static void *
39977c478bd9Sstevel@tonic-gate rib_rbuf_alloc(CONN *conn, rdma_buf_t *rdbuf)
39987c478bd9Sstevel@tonic-gate {
39997c478bd9Sstevel@tonic-gate 	rib_qp_t	*qp = ctoqp(conn);
40007c478bd9Sstevel@tonic-gate 	rib_hca_t	*hca = qp->hca;
40017c478bd9Sstevel@tonic-gate 	rdma_btype	ptype = rdbuf->type;
40027c478bd9Sstevel@tonic-gate 	void		*buf;
40037c478bd9Sstevel@tonic-gate 	rib_bufpool_t	*rbp = NULL;
40047c478bd9Sstevel@tonic-gate 	bufpool_t	*bp;
40057c478bd9Sstevel@tonic-gate 	int		i;
40067c478bd9Sstevel@tonic-gate 
40077c478bd9Sstevel@tonic-gate 	/*
40087c478bd9Sstevel@tonic-gate 	 * Obtain pool address based on type of pool
40097c478bd9Sstevel@tonic-gate 	 */
40107c478bd9Sstevel@tonic-gate 	switch (ptype) {
40117c478bd9Sstevel@tonic-gate 	case SEND_BUFFER:
40127c478bd9Sstevel@tonic-gate 		rbp = hca->send_pool;
40137c478bd9Sstevel@tonic-gate 		break;
40147c478bd9Sstevel@tonic-gate 	case RECV_BUFFER:
40157c478bd9Sstevel@tonic-gate 		rbp = hca->recv_pool;
40167c478bd9Sstevel@tonic-gate 		break;
40177c478bd9Sstevel@tonic-gate 	default:
40187c478bd9Sstevel@tonic-gate 		return (NULL);
40197c478bd9Sstevel@tonic-gate 	}
40207c478bd9Sstevel@tonic-gate 	if (rbp == NULL)
40217c478bd9Sstevel@tonic-gate 		return (NULL);
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	bp = rbp->bpool;
40247c478bd9Sstevel@tonic-gate 
40257c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->buflock);
40267c478bd9Sstevel@tonic-gate 	if (bp->buffree < 0) {
40277c478bd9Sstevel@tonic-gate 		mutex_exit(&bp->buflock);
40287c478bd9Sstevel@tonic-gate 		return (NULL);
40297c478bd9Sstevel@tonic-gate 	}
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate 	/* XXXX put buf, rdbuf->handle.mrc_rmr, ... in one place. */
40327c478bd9Sstevel@tonic-gate 	buf = bp->buflist[bp->buffree];
40337c478bd9Sstevel@tonic-gate 	rdbuf->addr = buf;
40347c478bd9Sstevel@tonic-gate 	rdbuf->len = bp->rsize;
40357c478bd9Sstevel@tonic-gate 	for (i = bp->numelems - 1; i >= 0; i--) {
403611606941Sjwahlig 		if ((ib_vaddr_t)(uintptr_t)buf == rbp->mr_desc[i].md_vaddr) {
40370a701b1eSRobert Gordon 			rdbuf->handle.mrc_rmr =
40380a701b1eSRobert Gordon 			    (uint32_t)rbp->mr_desc[i].md_rkey;
40390a701b1eSRobert Gordon 			rdbuf->handle.mrc_linfo =
40400a701b1eSRobert Gordon 			    (uintptr_t)rbp->mr_hdl[i];
40410a701b1eSRobert Gordon 			rdbuf->handle.mrc_lmr =
40420a701b1eSRobert Gordon 			    (uint32_t)rbp->mr_desc[i].md_lkey;
40437c478bd9Sstevel@tonic-gate 			bp->buffree--;
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->buflock);
40467c478bd9Sstevel@tonic-gate 
40477c478bd9Sstevel@tonic-gate 			return (buf);
40487c478bd9Sstevel@tonic-gate 		}
40497c478bd9Sstevel@tonic-gate 	}
40500a701b1eSRobert Gordon 
40517c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->buflock);
40527c478bd9Sstevel@tonic-gate 
40537c478bd9Sstevel@tonic-gate 	return (NULL);
40547c478bd9Sstevel@tonic-gate }
40557c478bd9Sstevel@tonic-gate 
40567c478bd9Sstevel@tonic-gate static void
40577c478bd9Sstevel@tonic-gate rib_reg_buf_free(CONN *conn, rdma_buf_t *rdbuf)
40587c478bd9Sstevel@tonic-gate {
40597c478bd9Sstevel@tonic-gate 
40600a701b1eSRobert Gordon 	if (rdbuf->type == RDMA_LONG_BUFFER) {
40610a701b1eSRobert Gordon 		rib_free_cache_buf(conn, (rib_lrc_entry_t *)rdbuf->rb_private);
40620a701b1eSRobert Gordon 		rdbuf->rb_private = NULL;
40630a701b1eSRobert Gordon 		return;
40640a701b1eSRobert Gordon 	}
40657c478bd9Sstevel@tonic-gate 	rib_rbuf_free(conn, rdbuf->type, rdbuf->addr);
40667c478bd9Sstevel@tonic-gate }
40677c478bd9Sstevel@tonic-gate 
40687c478bd9Sstevel@tonic-gate static void
40697c478bd9Sstevel@tonic-gate rib_rbuf_free(CONN *conn, int ptype, void *buf)
40707c478bd9Sstevel@tonic-gate {
40717c478bd9Sstevel@tonic-gate 	rib_qp_t *qp = ctoqp(conn);
40727c478bd9Sstevel@tonic-gate 	rib_hca_t *hca = qp->hca;
40737c478bd9Sstevel@tonic-gate 	rib_bufpool_t *rbp = NULL;
40747c478bd9Sstevel@tonic-gate 	bufpool_t *bp;
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate 	/*
40777c478bd9Sstevel@tonic-gate 	 * Obtain pool address based on type of pool
40787c478bd9Sstevel@tonic-gate 	 */
40797c478bd9Sstevel@tonic-gate 	switch (ptype) {
40807c478bd9Sstevel@tonic-gate 	case SEND_BUFFER:
40817c478bd9Sstevel@tonic-gate 		rbp = hca->send_pool;
40827c478bd9Sstevel@tonic-gate 		break;
40837c478bd9Sstevel@tonic-gate 	case RECV_BUFFER:
40847c478bd9Sstevel@tonic-gate 		rbp = hca->recv_pool;
40857c478bd9Sstevel@tonic-gate 		break;
40867c478bd9Sstevel@tonic-gate 	default:
40877c478bd9Sstevel@tonic-gate 		return;
40887c478bd9Sstevel@tonic-gate 	}
40897c478bd9Sstevel@tonic-gate 	if (rbp == NULL)
40907c478bd9Sstevel@tonic-gate 		return;
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate 	bp = rbp->bpool;
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->buflock);
40957c478bd9Sstevel@tonic-gate 	if (++bp->buffree >= bp->numelems) {
40967c478bd9Sstevel@tonic-gate 		/*
40977c478bd9Sstevel@tonic-gate 		 * Should never happen
40987c478bd9Sstevel@tonic-gate 		 */
40997c478bd9Sstevel@tonic-gate 		bp->buffree--;
41007c478bd9Sstevel@tonic-gate 	} else {
41017c478bd9Sstevel@tonic-gate 		bp->buflist[bp->buffree] = buf;
41027c478bd9Sstevel@tonic-gate 	}
41037c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->buflock);
41047c478bd9Sstevel@tonic-gate }
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate static rdma_stat
41077c478bd9Sstevel@tonic-gate rib_add_connlist(CONN *cn, rib_conn_list_t *connlist)
41087c478bd9Sstevel@tonic-gate {
41097c478bd9Sstevel@tonic-gate 	rw_enter(&connlist->conn_lock, RW_WRITER);
41107c478bd9Sstevel@tonic-gate 	if (connlist->conn_hd) {
41117c478bd9Sstevel@tonic-gate 		cn->c_next = connlist->conn_hd;
41127c478bd9Sstevel@tonic-gate 		connlist->conn_hd->c_prev = cn;
41137c478bd9Sstevel@tonic-gate 	}
41147c478bd9Sstevel@tonic-gate 	connlist->conn_hd = cn;
41157c478bd9Sstevel@tonic-gate 	rw_exit(&connlist->conn_lock);
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
41187c478bd9Sstevel@tonic-gate }
41197c478bd9Sstevel@tonic-gate 
41207c478bd9Sstevel@tonic-gate static rdma_stat
41217c478bd9Sstevel@tonic-gate rib_rm_conn(CONN *cn, rib_conn_list_t *connlist)
41227c478bd9Sstevel@tonic-gate {
41237c478bd9Sstevel@tonic-gate 	rw_enter(&connlist->conn_lock, RW_WRITER);
41247c478bd9Sstevel@tonic-gate 	if (cn->c_prev) {
41257c478bd9Sstevel@tonic-gate 		cn->c_prev->c_next = cn->c_next;
41267c478bd9Sstevel@tonic-gate 	}
41277c478bd9Sstevel@tonic-gate 	if (cn->c_next) {
41287c478bd9Sstevel@tonic-gate 		cn->c_next->c_prev = cn->c_prev;
41297c478bd9Sstevel@tonic-gate 	}
41307c478bd9Sstevel@tonic-gate 	if (connlist->conn_hd == cn)
41317c478bd9Sstevel@tonic-gate 		connlist->conn_hd = cn->c_next;
41327c478bd9Sstevel@tonic-gate 	rw_exit(&connlist->conn_lock);
41337c478bd9Sstevel@tonic-gate 
41347c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
41357c478bd9Sstevel@tonic-gate }
41367c478bd9Sstevel@tonic-gate 
41377c478bd9Sstevel@tonic-gate /*
4138*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * rib_find_hca_connection
4139*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  *
4140*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * if there is an existing connection to the specified address then
4141*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * it will be returned in conn, otherwise conn will be set to NULL.
4142*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * Also cleans up any connection that is in error state.
41437c478bd9Sstevel@tonic-gate  */
4144*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static int
4145*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rib_find_hca_connection(rib_hca_t *hca, struct netbuf *s_svcaddr,
4146*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States     struct netbuf *d_svcaddr, CONN **conn)
41477c478bd9Sstevel@tonic-gate {
41487c478bd9Sstevel@tonic-gate 	CONN *cn;
41497c478bd9Sstevel@tonic-gate 	clock_t cv_stat, timout;
41507c478bd9Sstevel@tonic-gate 
4151*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	*conn = NULL;
41527c478bd9Sstevel@tonic-gate again:
41537c478bd9Sstevel@tonic-gate 	rw_enter(&hca->cl_conn_list.conn_lock, RW_READER);
41547c478bd9Sstevel@tonic-gate 	cn = hca->cl_conn_list.conn_hd;
41557c478bd9Sstevel@tonic-gate 	while (cn != NULL) {
41567c478bd9Sstevel@tonic-gate 		/*
41577c478bd9Sstevel@tonic-gate 		 * First, clear up any connection in the ERROR state
41587c478bd9Sstevel@tonic-gate 		 */
41597c478bd9Sstevel@tonic-gate 		mutex_enter(&cn->c_lock);
41600a701b1eSRobert Gordon 		if (cn->c_state == C_ERROR_CONN) {
41617c478bd9Sstevel@tonic-gate 			if (cn->c_ref == 0) {
41627c478bd9Sstevel@tonic-gate 				/*
41637c478bd9Sstevel@tonic-gate 				 * Remove connection from list and destroy it.
41647c478bd9Sstevel@tonic-gate 				 */
41657c478bd9Sstevel@tonic-gate 				cn->c_state = C_DISCONN_PEND;
41667c478bd9Sstevel@tonic-gate 				mutex_exit(&cn->c_lock);
41677c478bd9Sstevel@tonic-gate 				rw_exit(&hca->cl_conn_list.conn_lock);
4168065714dcSSiddheshwar Mahesh 				rib_conn_close((void *)cn);
41697c478bd9Sstevel@tonic-gate 				goto again;
41707c478bd9Sstevel@tonic-gate 			}
41717c478bd9Sstevel@tonic-gate 			mutex_exit(&cn->c_lock);
41727c478bd9Sstevel@tonic-gate 			cn = cn->c_next;
41737c478bd9Sstevel@tonic-gate 			continue;
41740a701b1eSRobert Gordon 		}
41750a701b1eSRobert Gordon 		if (cn->c_state == C_DISCONN_PEND) {
41767c478bd9Sstevel@tonic-gate 			mutex_exit(&cn->c_lock);
41777c478bd9Sstevel@tonic-gate 			cn = cn->c_next;
41787c478bd9Sstevel@tonic-gate 			continue;
41797c478bd9Sstevel@tonic-gate 		}
4180*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4181*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		/*
4182*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * source address is only checked for if there is one,
4183*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 * this is the case for retries.
4184*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		 */
4185*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if ((cn->c_raddr.len == d_svcaddr->len) &&
4186*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    (bcmp(d_svcaddr->buf, cn->c_raddr.buf,
4187*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    d_svcaddr->len) == 0) &&
4188*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    ((s_svcaddr->len == 0) ||
4189*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    ((cn->c_laddr.len == s_svcaddr->len) &&
4190*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    (bcmp(s_svcaddr->buf, cn->c_laddr.buf,
4191*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    s_svcaddr->len) == 0)))) {
41927c478bd9Sstevel@tonic-gate 			/*
41937c478bd9Sstevel@tonic-gate 			 * Our connection. Give up conn list lock
41947c478bd9Sstevel@tonic-gate 			 * as we are done traversing the list.
41957c478bd9Sstevel@tonic-gate 			 */
41967c478bd9Sstevel@tonic-gate 			rw_exit(&hca->cl_conn_list.conn_lock);
41977c478bd9Sstevel@tonic-gate 			if (cn->c_state == C_CONNECTED) {
41987c478bd9Sstevel@tonic-gate 				cn->c_ref++;	/* sharing a conn */
41997c478bd9Sstevel@tonic-gate 				mutex_exit(&cn->c_lock);
42007c478bd9Sstevel@tonic-gate 				*conn = cn;
4201*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				return (RDMA_SUCCESS);
42027c478bd9Sstevel@tonic-gate 			}
42037c478bd9Sstevel@tonic-gate 			if (cn->c_state == C_CONN_PEND) {
42047c478bd9Sstevel@tonic-gate 				/*
42057c478bd9Sstevel@tonic-gate 				 * Hold a reference to this conn before
42067c478bd9Sstevel@tonic-gate 				 * we give up the lock.
42077c478bd9Sstevel@tonic-gate 				 */
42087c478bd9Sstevel@tonic-gate 				cn->c_ref++;
42097c478bd9Sstevel@tonic-gate 				timout =  ddi_get_lbolt() +
42107c478bd9Sstevel@tonic-gate 				    drv_usectohz(CONN_WAIT_TIME * 1000000);
42117c478bd9Sstevel@tonic-gate 				while ((cv_stat = cv_timedwait_sig(&cn->c_cv,
42127c478bd9Sstevel@tonic-gate 				    &cn->c_lock, timout)) > 0 &&
42137c478bd9Sstevel@tonic-gate 				    cn->c_state == C_CONN_PEND)
42147c478bd9Sstevel@tonic-gate 					;
42157c478bd9Sstevel@tonic-gate 				if (cv_stat == 0) {
42167c478bd9Sstevel@tonic-gate 					cn->c_ref--;
42177c478bd9Sstevel@tonic-gate 					mutex_exit(&cn->c_lock);
42187c478bd9Sstevel@tonic-gate 					return (RDMA_INTR);
42197c478bd9Sstevel@tonic-gate 				}
42207c478bd9Sstevel@tonic-gate 				if (cv_stat < 0) {
42217c478bd9Sstevel@tonic-gate 					cn->c_ref--;
42227c478bd9Sstevel@tonic-gate 					mutex_exit(&cn->c_lock);
42237c478bd9Sstevel@tonic-gate 					return (RDMA_TIMEDOUT);
42247c478bd9Sstevel@tonic-gate 				}
42257c478bd9Sstevel@tonic-gate 				if (cn->c_state == C_CONNECTED) {
42267c478bd9Sstevel@tonic-gate 					*conn = cn;
42277c478bd9Sstevel@tonic-gate 					mutex_exit(&cn->c_lock);
4228*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 					return (RDMA_SUCCESS);
42297c478bd9Sstevel@tonic-gate 				} else {
42307c478bd9Sstevel@tonic-gate 					cn->c_ref--;
42317c478bd9Sstevel@tonic-gate 					mutex_exit(&cn->c_lock);
42327c478bd9Sstevel@tonic-gate 					return (RDMA_TIMEDOUT);
42337c478bd9Sstevel@tonic-gate 				}
42347c478bd9Sstevel@tonic-gate 			}
42357c478bd9Sstevel@tonic-gate 		}
42367c478bd9Sstevel@tonic-gate 		mutex_exit(&cn->c_lock);
42377c478bd9Sstevel@tonic-gate 		cn = cn->c_next;
42387c478bd9Sstevel@tonic-gate 	}
42397c478bd9Sstevel@tonic-gate 	rw_exit(&hca->cl_conn_list.conn_lock);
4240*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	*conn = NULL;
42417c478bd9Sstevel@tonic-gate 	return (RDMA_FAILED);
42427c478bd9Sstevel@tonic-gate }
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate /*
4245*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * Connection management.
4246*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * IBTF does not support recycling of channels. So connections are only
4247*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * in four states - C_CONN_PEND, or C_CONNECTED, or C_ERROR_CONN or
4248*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * C_DISCONN_PEND state. No C_IDLE state.
4249*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * C_CONN_PEND state: Connection establishment in progress to the server.
4250*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * C_CONNECTED state: A connection when created is in C_CONNECTED state.
4251*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * It has an RC channel associated with it. ibt_post_send/recv are allowed
4252*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * only in this state.
4253*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * C_ERROR_CONN state: A connection transitions to this state when WRs on the
4254*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * channel are completed in error or an IBT_CM_EVENT_CONN_CLOSED event
4255*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * happens on the channel or a IBT_HCA_DETACH_EVENT occurs on the HCA.
4256*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * C_DISCONN_PEND state: When a connection is in C_ERROR_CONN state and when
4257*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * c_ref drops to 0 (this indicates that RPC has no more references to this
4258*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * connection), the connection should be destroyed. A connection transitions
4259*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * into this state when it is being destroyed.
4260*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  */
4261*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States /* ARGSUSED */
4262*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static rdma_stat
4263*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rib_conn_get(struct netbuf *s_svcaddr, struct netbuf *d_svcaddr,
4264*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States     int addr_type, void *handle, CONN **conn)
4265*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States {
4266*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	CONN *cn;
4267*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	int status;
4268*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_hca_t *hca;
4269*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_qp_t *qp;
4270*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rpcib_ping_t rpt;
4271*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	int s_addr_len;
4272*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	char *s_addr_buf;
4273*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4274*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->hcas_list_lock, RW_READER);
4275*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	for (hca = rib_stat->hcas_list; hca; hca = hca->next) {
4276*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_enter(&hca->state_lock, RW_READER);
4277*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (hca->state != HCA_DETACHED) {
4278*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			status = rib_find_hca_connection(hca, s_svcaddr,
4279*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			    d_svcaddr, conn);
4280*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&hca->state_lock);
4281*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			if ((status == RDMA_INTR) || (status == RDMA_SUCCESS)) {
4282*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				rw_exit(&rib_stat->hcas_list_lock);
4283*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 				return (status);
4284*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			}
4285*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		} else
4286*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			rw_exit(&hca->state_lock);
4287*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
4288*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->hcas_list_lock);
4289*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4290*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	/*
4291*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * No existing connection found, establish a new connection.
4292*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 */
4293*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	bzero(&rpt, sizeof (rpcib_ping_t));
4294*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4295*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	status = rib_ping_srv(addr_type, d_svcaddr, &rpt);
4296*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (status != RDMA_SUCCESS) {
4297*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		return (RDMA_FAILED);
4298*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
4299*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	hca = rpt.hca;
4300*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4301*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (rpt.srcip.family == AF_INET) {
4302*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		s_addr_len = sizeof (rpt.srcip.un.ip4addr);
4303*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		s_addr_buf = (char *)&rpt.srcip.un.ip4addr;
4304*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	} else if (rpt.srcip.family == AF_INET6) {
4305*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		s_addr_len = sizeof (rpt.srcip.un.ip6addr);
4306*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		s_addr_buf = (char *)&rpt.srcip.un.ip6addr;
4307*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	} else
4308*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		return (RDMA_FAILED);
4309*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4310*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	/*
43117c478bd9Sstevel@tonic-gate 	 * Channel to server doesn't exist yet, create one.
43127c478bd9Sstevel@tonic-gate 	 */
4313*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (rib_clnt_create_chan(hca, d_svcaddr, &qp) != RDMA_SUCCESS) {
43147c478bd9Sstevel@tonic-gate 		return (RDMA_FAILED);
43157c478bd9Sstevel@tonic-gate 	}
43167c478bd9Sstevel@tonic-gate 	cn = qptoc(qp);
43177c478bd9Sstevel@tonic-gate 	cn->c_state = C_CONN_PEND;
43187c478bd9Sstevel@tonic-gate 	cn->c_ref = 1;
43197c478bd9Sstevel@tonic-gate 
4320*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	cn->c_laddr.buf = kmem_alloc(s_addr_len, KM_SLEEP);
4321*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	bcopy(s_addr_buf, cn->c_laddr.buf, s_addr_len);
4322*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	cn->c_laddr.len = cn->c_laddr.maxlen = s_addr_len;
4323*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
43247c478bd9Sstevel@tonic-gate 	/*
43257c478bd9Sstevel@tonic-gate 	 * Add to conn list.
43267c478bd9Sstevel@tonic-gate 	 * We had given up the READER lock. In the time since then,
43277c478bd9Sstevel@tonic-gate 	 * another thread might have created the connection we are
43287c478bd9Sstevel@tonic-gate 	 * trying here. But for now, that is quiet alright - there
43297c478bd9Sstevel@tonic-gate 	 * might be two connections between a pair of hosts instead
43307c478bd9Sstevel@tonic-gate 	 * of one. If we really want to close that window,
43317c478bd9Sstevel@tonic-gate 	 * then need to check the list after acquiring the
43327c478bd9Sstevel@tonic-gate 	 * WRITER lock.
43337c478bd9Sstevel@tonic-gate 	 */
43347c478bd9Sstevel@tonic-gate 	(void) rib_add_connlist(cn, &hca->cl_conn_list);
4335214ae7d0SSiddheshwar Mahesh 	status = rib_conn_to_srv(hca, qp, &rpt);
43367c478bd9Sstevel@tonic-gate 	mutex_enter(&cn->c_lock);
43377c478bd9Sstevel@tonic-gate 	if (status == RDMA_SUCCESS) {
43387c478bd9Sstevel@tonic-gate 		cn->c_state = C_CONNECTED;
43397c478bd9Sstevel@tonic-gate 		*conn = cn;
43407c478bd9Sstevel@tonic-gate 	} else {
43410a701b1eSRobert Gordon 		cn->c_state = C_ERROR_CONN;
43427c478bd9Sstevel@tonic-gate 		cn->c_ref--;
43437c478bd9Sstevel@tonic-gate 	}
43447c478bd9Sstevel@tonic-gate 	cv_broadcast(&cn->c_cv);
43457c478bd9Sstevel@tonic-gate 	mutex_exit(&cn->c_lock);
43467c478bd9Sstevel@tonic-gate 	return (status);
43477c478bd9Sstevel@tonic-gate }
43487c478bd9Sstevel@tonic-gate 
4349065714dcSSiddheshwar Mahesh static void
4350065714dcSSiddheshwar Mahesh rib_conn_close(void *rarg)
43517c478bd9Sstevel@tonic-gate {
4352065714dcSSiddheshwar Mahesh 	CONN *conn = (CONN *)rarg;
43537c478bd9Sstevel@tonic-gate 	rib_qp_t *qp = ctoqp(conn);
43547c478bd9Sstevel@tonic-gate 
43557c478bd9Sstevel@tonic-gate 	mutex_enter(&conn->c_lock);
4356065714dcSSiddheshwar Mahesh 	if (!(conn->c_flags & C_CLOSE_NOTNEEDED)) {
43577c478bd9Sstevel@tonic-gate 
4358065714dcSSiddheshwar Mahesh 		conn->c_flags |= (C_CLOSE_NOTNEEDED | C_CLOSE_PENDING);
43597c478bd9Sstevel@tonic-gate 		/*
4360065714dcSSiddheshwar Mahesh 		 * Live connection in CONNECTED state.
43617c478bd9Sstevel@tonic-gate 		 */
4362065714dcSSiddheshwar Mahesh 		if (conn->c_state == C_CONNECTED) {
4363065714dcSSiddheshwar Mahesh 			conn->c_state = C_ERROR_CONN;
4364065714dcSSiddheshwar Mahesh 		}
43657c478bd9Sstevel@tonic-gate 		mutex_exit(&conn->c_lock);
4366065714dcSSiddheshwar Mahesh 
4367065714dcSSiddheshwar Mahesh 		rib_close_a_channel(conn);
4368065714dcSSiddheshwar Mahesh 
4369065714dcSSiddheshwar Mahesh 		mutex_enter(&conn->c_lock);
4370065714dcSSiddheshwar Mahesh 		conn->c_flags &= ~C_CLOSE_PENDING;
4371065714dcSSiddheshwar Mahesh 		cv_signal(&conn->c_cv);
4372065714dcSSiddheshwar Mahesh 	}
4373065714dcSSiddheshwar Mahesh 
4374065714dcSSiddheshwar Mahesh 	mutex_exit(&conn->c_lock);
4375065714dcSSiddheshwar Mahesh 
43767c478bd9Sstevel@tonic-gate 	if (qp->mode == RIB_SERVER)
43777c478bd9Sstevel@tonic-gate 		(void) rib_disconnect_channel(conn,
43787c478bd9Sstevel@tonic-gate 		    &qp->hca->srv_conn_list);
43797c478bd9Sstevel@tonic-gate 	else
43807c478bd9Sstevel@tonic-gate 		(void) rib_disconnect_channel(conn,
43817c478bd9Sstevel@tonic-gate 		    &qp->hca->cl_conn_list);
4382065714dcSSiddheshwar Mahesh }
4383065714dcSSiddheshwar Mahesh 
4384065714dcSSiddheshwar Mahesh static void
4385065714dcSSiddheshwar Mahesh rib_conn_timeout_call(void *carg)
4386065714dcSSiddheshwar Mahesh {
4387065714dcSSiddheshwar Mahesh 	time_t idle_time;
4388065714dcSSiddheshwar Mahesh 	CONN *conn = (CONN *)carg;
4389065714dcSSiddheshwar Mahesh 	rib_hca_t *hca = ctoqp(conn)->hca;
4390065714dcSSiddheshwar Mahesh 	int error;
4391065714dcSSiddheshwar Mahesh 
4392065714dcSSiddheshwar Mahesh 	mutex_enter(&conn->c_lock);
4393065714dcSSiddheshwar Mahesh 	if ((conn->c_ref > 0) ||
4394065714dcSSiddheshwar Mahesh 	    (conn->c_state == C_DISCONN_PEND)) {
4395065714dcSSiddheshwar Mahesh 		conn->c_timeout = NULL;
4396065714dcSSiddheshwar Mahesh 		mutex_exit(&conn->c_lock);
4397065714dcSSiddheshwar Mahesh 		return;
4398065714dcSSiddheshwar Mahesh 	}
4399065714dcSSiddheshwar Mahesh 
4400065714dcSSiddheshwar Mahesh 	idle_time = (gethrestime_sec() - conn->c_last_used);
4401065714dcSSiddheshwar Mahesh 
4402065714dcSSiddheshwar Mahesh 	if ((idle_time <= rib_conn_timeout) &&
4403065714dcSSiddheshwar Mahesh 	    (conn->c_state != C_ERROR_CONN)) {
4404065714dcSSiddheshwar Mahesh 		/*
4405065714dcSSiddheshwar Mahesh 		 * There was activity after the last timeout.
4406065714dcSSiddheshwar Mahesh 		 * Extend the conn life. Unless the conn is
4407065714dcSSiddheshwar Mahesh 		 * already in error state.
4408065714dcSSiddheshwar Mahesh 		 */
4409065714dcSSiddheshwar Mahesh 		conn->c_timeout = timeout(rib_conn_timeout_call, conn,
4410065714dcSSiddheshwar Mahesh 		    SEC_TO_TICK(rib_conn_timeout - idle_time));
4411065714dcSSiddheshwar Mahesh 		mutex_exit(&conn->c_lock);
4412065714dcSSiddheshwar Mahesh 		return;
4413065714dcSSiddheshwar Mahesh 	}
4414065714dcSSiddheshwar Mahesh 
4415065714dcSSiddheshwar Mahesh 	error = ddi_taskq_dispatch(hca->cleanup_helper, rib_conn_close,
4416065714dcSSiddheshwar Mahesh 	    (void *)conn, DDI_NOSLEEP);
4417065714dcSSiddheshwar Mahesh 
4418065714dcSSiddheshwar Mahesh 	/*
4419065714dcSSiddheshwar Mahesh 	 * If taskq dispatch fails above, then reset the timeout
4420065714dcSSiddheshwar Mahesh 	 * to try again after 10 secs.
4421065714dcSSiddheshwar Mahesh 	 */
4422065714dcSSiddheshwar Mahesh 
4423065714dcSSiddheshwar Mahesh 	if (error != DDI_SUCCESS) {
4424065714dcSSiddheshwar Mahesh 		conn->c_timeout = timeout(rib_conn_timeout_call, conn,
4425065714dcSSiddheshwar Mahesh 		    SEC_TO_TICK(RDMA_CONN_REAP_RETRY));
4426065714dcSSiddheshwar Mahesh 		mutex_exit(&conn->c_lock);
4427065714dcSSiddheshwar Mahesh 		return;
4428065714dcSSiddheshwar Mahesh 	}
4429065714dcSSiddheshwar Mahesh 
4430065714dcSSiddheshwar Mahesh 	conn->c_state = C_DISCONN_PEND;
4431065714dcSSiddheshwar Mahesh 	mutex_exit(&conn->c_lock);
4432065714dcSSiddheshwar Mahesh }
4433065714dcSSiddheshwar Mahesh 
4434065714dcSSiddheshwar Mahesh static rdma_stat
4435065714dcSSiddheshwar Mahesh rib_conn_release(CONN *conn)
4436065714dcSSiddheshwar Mahesh {
4437065714dcSSiddheshwar Mahesh 
4438065714dcSSiddheshwar Mahesh 	mutex_enter(&conn->c_lock);
4439065714dcSSiddheshwar Mahesh 	conn->c_ref--;
4440065714dcSSiddheshwar Mahesh 
4441065714dcSSiddheshwar Mahesh 	conn->c_last_used = gethrestime_sec();
4442065714dcSSiddheshwar Mahesh 	if (conn->c_ref > 0) {
4443065714dcSSiddheshwar Mahesh 		mutex_exit(&conn->c_lock);
44447c478bd9Sstevel@tonic-gate 		return (RDMA_SUCCESS);
44457c478bd9Sstevel@tonic-gate 	}
4446065714dcSSiddheshwar Mahesh 
4447065714dcSSiddheshwar Mahesh 	/*
4448065714dcSSiddheshwar Mahesh 	 * If a conn is C_ERROR_CONN, close the channel.
4449065714dcSSiddheshwar Mahesh 	 */
4450065714dcSSiddheshwar Mahesh 	if (conn->c_ref == 0 && conn->c_state == C_ERROR_CONN) {
4451065714dcSSiddheshwar Mahesh 		conn->c_state = C_DISCONN_PEND;
4452065714dcSSiddheshwar Mahesh 		mutex_exit(&conn->c_lock);
4453065714dcSSiddheshwar Mahesh 		rib_conn_close((void *)conn);
4454065714dcSSiddheshwar Mahesh 		return (RDMA_SUCCESS);
4455065714dcSSiddheshwar Mahesh 	}
4456065714dcSSiddheshwar Mahesh 
4457065714dcSSiddheshwar Mahesh 	/*
4458065714dcSSiddheshwar Mahesh 	 * c_ref == 0, set a timeout for conn release
4459065714dcSSiddheshwar Mahesh 	 */
4460065714dcSSiddheshwar Mahesh 
4461065714dcSSiddheshwar Mahesh 	if (conn->c_timeout == NULL) {
4462065714dcSSiddheshwar Mahesh 		conn->c_timeout = timeout(rib_conn_timeout_call, conn,
4463065714dcSSiddheshwar Mahesh 		    SEC_TO_TICK(rib_conn_timeout));
4464065714dcSSiddheshwar Mahesh 	}
4465065714dcSSiddheshwar Mahesh 
44667c478bd9Sstevel@tonic-gate 	mutex_exit(&conn->c_lock);
44677c478bd9Sstevel@tonic-gate 	return (RDMA_SUCCESS);
44687c478bd9Sstevel@tonic-gate }
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate /*
44717c478bd9Sstevel@tonic-gate  * Add at front of list
44727c478bd9Sstevel@tonic-gate  */
44737c478bd9Sstevel@tonic-gate static struct rdma_done_list *
44747c478bd9Sstevel@tonic-gate rdma_done_add(rib_qp_t *qp, uint32_t xid)
44757c478bd9Sstevel@tonic-gate {
44767c478bd9Sstevel@tonic-gate 	struct rdma_done_list *rd;
44777c478bd9Sstevel@tonic-gate 
44787c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qp->rdlist_lock));
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate 	rd = kmem_alloc(sizeof (*rd), KM_SLEEP);
44817c478bd9Sstevel@tonic-gate 	rd->xid = xid;
44827c478bd9Sstevel@tonic-gate 	cv_init(&rd->rdma_done_cv, NULL, CV_DEFAULT, NULL);
44837c478bd9Sstevel@tonic-gate 
44847c478bd9Sstevel@tonic-gate 	rd->prev = NULL;
44857c478bd9Sstevel@tonic-gate 	rd->next = qp->rdlist;
44867c478bd9Sstevel@tonic-gate 	if (qp->rdlist != NULL)
44877c478bd9Sstevel@tonic-gate 		qp->rdlist->prev = rd;
44887c478bd9Sstevel@tonic-gate 	qp->rdlist = rd;
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 	return (rd);
44917c478bd9Sstevel@tonic-gate }
44927c478bd9Sstevel@tonic-gate 
44937c478bd9Sstevel@tonic-gate static void
44947c478bd9Sstevel@tonic-gate rdma_done_rm(rib_qp_t *qp, struct rdma_done_list *rd)
44957c478bd9Sstevel@tonic-gate {
44967c478bd9Sstevel@tonic-gate 	struct rdma_done_list *r;
44977c478bd9Sstevel@tonic-gate 
44987c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qp->rdlist_lock));
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	r = rd->next;
45017c478bd9Sstevel@tonic-gate 	if (r != NULL) {
45027c478bd9Sstevel@tonic-gate 		r->prev = rd->prev;
45037c478bd9Sstevel@tonic-gate 	}
45047c478bd9Sstevel@tonic-gate 
45057c478bd9Sstevel@tonic-gate 	r = rd->prev;
45067c478bd9Sstevel@tonic-gate 	if (r != NULL) {
45077c478bd9Sstevel@tonic-gate 		r->next = rd->next;
45087c478bd9Sstevel@tonic-gate 	} else {
45097c478bd9Sstevel@tonic-gate 		qp->rdlist = rd->next;
45107c478bd9Sstevel@tonic-gate 	}
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate 	cv_destroy(&rd->rdma_done_cv);
45137c478bd9Sstevel@tonic-gate 	kmem_free(rd, sizeof (*rd));
45147c478bd9Sstevel@tonic-gate }
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate static void
45177c478bd9Sstevel@tonic-gate rdma_done_rem_list(rib_qp_t *qp)
45187c478bd9Sstevel@tonic-gate {
45197c478bd9Sstevel@tonic-gate 	struct rdma_done_list	*r, *n;
45207c478bd9Sstevel@tonic-gate 
45217c478bd9Sstevel@tonic-gate 	mutex_enter(&qp->rdlist_lock);
45227c478bd9Sstevel@tonic-gate 	for (r = qp->rdlist; r != NULL; r = n) {
45237c478bd9Sstevel@tonic-gate 		n = r->next;
45247c478bd9Sstevel@tonic-gate 		rdma_done_rm(qp, r);
45257c478bd9Sstevel@tonic-gate 	}
45267c478bd9Sstevel@tonic-gate 	mutex_exit(&qp->rdlist_lock);
45277c478bd9Sstevel@tonic-gate }
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate static void
45307c478bd9Sstevel@tonic-gate rdma_done_notify(rib_qp_t *qp, uint32_t xid)
45317c478bd9Sstevel@tonic-gate {
45327c478bd9Sstevel@tonic-gate 	struct rdma_done_list *r = qp->rdlist;
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qp->rdlist_lock));
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate 	while (r) {
45377c478bd9Sstevel@tonic-gate 		if (r->xid == xid) {
45387c478bd9Sstevel@tonic-gate 			cv_signal(&r->rdma_done_cv);
45397c478bd9Sstevel@tonic-gate 			return;
45407c478bd9Sstevel@tonic-gate 		} else {
45417c478bd9Sstevel@tonic-gate 			r = r->next;
45427c478bd9Sstevel@tonic-gate 		}
45437c478bd9Sstevel@tonic-gate 	}
45440a701b1eSRobert Gordon 	DTRACE_PROBE1(rpcib__i__donenotify__nomatchxid,
45450a701b1eSRobert Gordon 	    int, xid);
45467c478bd9Sstevel@tonic-gate }
45477c478bd9Sstevel@tonic-gate 
4548065714dcSSiddheshwar Mahesh /*
4549065714dcSSiddheshwar Mahesh  * Expects conn->c_lock to be held by the caller.
4550065714dcSSiddheshwar Mahesh  */
4551065714dcSSiddheshwar Mahesh 
4552065714dcSSiddheshwar Mahesh static void
4553065714dcSSiddheshwar Mahesh rib_close_a_channel(CONN *conn)
4554065714dcSSiddheshwar Mahesh {
4555065714dcSSiddheshwar Mahesh 	rib_qp_t	*qp;
4556065714dcSSiddheshwar Mahesh 	qp = ctoqp(conn);
4557065714dcSSiddheshwar Mahesh 
4558065714dcSSiddheshwar Mahesh 	if (qp->qp_hdl == NULL) {
4559065714dcSSiddheshwar Mahesh 		/* channel already freed */
4560065714dcSSiddheshwar Mahesh 		return;
4561065714dcSSiddheshwar Mahesh 	}
4562065714dcSSiddheshwar Mahesh 
4563065714dcSSiddheshwar Mahesh 	/*
4564065714dcSSiddheshwar Mahesh 	 * Call ibt_close_rc_channel in blocking mode
4565065714dcSSiddheshwar Mahesh 	 * with no callbacks.
4566065714dcSSiddheshwar Mahesh 	 */
4567065714dcSSiddheshwar Mahesh 	(void) ibt_close_rc_channel(qp->qp_hdl, IBT_NOCALLBACKS,
4568065714dcSSiddheshwar Mahesh 	    NULL, 0, NULL, NULL, 0);
4569065714dcSSiddheshwar Mahesh }
45707c478bd9Sstevel@tonic-gate 
45717c478bd9Sstevel@tonic-gate /*
45727c478bd9Sstevel@tonic-gate  * Goes through all connections and closes the channel
45737c478bd9Sstevel@tonic-gate  * This will cause all the WRs on those channels to be
45747c478bd9Sstevel@tonic-gate  * flushed.
45757c478bd9Sstevel@tonic-gate  */
45767c478bd9Sstevel@tonic-gate static void
45777c478bd9Sstevel@tonic-gate rib_close_channels(rib_conn_list_t *connlist)
45787c478bd9Sstevel@tonic-gate {
4579065714dcSSiddheshwar Mahesh 	CONN 		*conn, *tmp;
45807c478bd9Sstevel@tonic-gate 
45817c478bd9Sstevel@tonic-gate 	rw_enter(&connlist->conn_lock, RW_READER);
45827c478bd9Sstevel@tonic-gate 	conn = connlist->conn_hd;
45837c478bd9Sstevel@tonic-gate 	while (conn != NULL) {
45847c478bd9Sstevel@tonic-gate 		mutex_enter(&conn->c_lock);
4585065714dcSSiddheshwar Mahesh 		tmp = conn->c_next;
4586065714dcSSiddheshwar Mahesh 		if (!(conn->c_flags & C_CLOSE_NOTNEEDED)) {
4587065714dcSSiddheshwar Mahesh 
4588065714dcSSiddheshwar Mahesh 			conn->c_flags |= (C_CLOSE_NOTNEEDED | C_CLOSE_PENDING);
4589065714dcSSiddheshwar Mahesh 
45907c478bd9Sstevel@tonic-gate 			/*
45917c478bd9Sstevel@tonic-gate 			 * Live connection in CONNECTED state.
45927c478bd9Sstevel@tonic-gate 			 */
4593065714dcSSiddheshwar Mahesh 			if (conn->c_state == C_CONNECTED)
45940a701b1eSRobert Gordon 				conn->c_state = C_ERROR_CONN;
4595065714dcSSiddheshwar Mahesh 			mutex_exit(&conn->c_lock);
4596065714dcSSiddheshwar Mahesh 
4597065714dcSSiddheshwar Mahesh 			rib_close_a_channel(conn);
4598065714dcSSiddheshwar Mahesh 
4599065714dcSSiddheshwar Mahesh 			mutex_enter(&conn->c_lock);
4600065714dcSSiddheshwar Mahesh 			conn->c_flags &= ~C_CLOSE_PENDING;
4601065714dcSSiddheshwar Mahesh 			/* Signal a pending rib_disconnect_channel() */
4602065714dcSSiddheshwar Mahesh 			cv_signal(&conn->c_cv);
46037c478bd9Sstevel@tonic-gate 		}
46047c478bd9Sstevel@tonic-gate 		mutex_exit(&conn->c_lock);
4605065714dcSSiddheshwar Mahesh 		conn = tmp;
46067c478bd9Sstevel@tonic-gate 	}
46077c478bd9Sstevel@tonic-gate 	rw_exit(&connlist->conn_lock);
46087c478bd9Sstevel@tonic-gate }
46097c478bd9Sstevel@tonic-gate 
46107c478bd9Sstevel@tonic-gate /*
46117c478bd9Sstevel@tonic-gate  * Frees up all connections that are no longer being referenced
46127c478bd9Sstevel@tonic-gate  */
46137c478bd9Sstevel@tonic-gate static void
46147c478bd9Sstevel@tonic-gate rib_purge_connlist(rib_conn_list_t *connlist)
46157c478bd9Sstevel@tonic-gate {
46167c478bd9Sstevel@tonic-gate 	CONN 		*conn;
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate top:
46197c478bd9Sstevel@tonic-gate 	rw_enter(&connlist->conn_lock, RW_READER);
46207c478bd9Sstevel@tonic-gate 	conn = connlist->conn_hd;
46217c478bd9Sstevel@tonic-gate 	while (conn != NULL) {
46227c478bd9Sstevel@tonic-gate 		mutex_enter(&conn->c_lock);
46237c478bd9Sstevel@tonic-gate 
46247c478bd9Sstevel@tonic-gate 		/*
46257c478bd9Sstevel@tonic-gate 		 * At this point connection is either in ERROR
46267c478bd9Sstevel@tonic-gate 		 * or DISCONN_PEND state. If in DISCONN_PEND state
46277c478bd9Sstevel@tonic-gate 		 * then some other thread is culling that connection.
46287c478bd9Sstevel@tonic-gate 		 * If not and if c_ref is 0, then destroy the connection.
46297c478bd9Sstevel@tonic-gate 		 */
46307c478bd9Sstevel@tonic-gate 		if (conn->c_ref == 0 &&
46317c478bd9Sstevel@tonic-gate 		    conn->c_state != C_DISCONN_PEND) {
46327c478bd9Sstevel@tonic-gate 			/*
46337c478bd9Sstevel@tonic-gate 			 * Cull the connection
46347c478bd9Sstevel@tonic-gate 			 */
46357c478bd9Sstevel@tonic-gate 			conn->c_state = C_DISCONN_PEND;
46367c478bd9Sstevel@tonic-gate 			mutex_exit(&conn->c_lock);
46377c478bd9Sstevel@tonic-gate 			rw_exit(&connlist->conn_lock);
46387c478bd9Sstevel@tonic-gate 			(void) rib_disconnect_channel(conn, connlist);
46397c478bd9Sstevel@tonic-gate 			goto top;
46407c478bd9Sstevel@tonic-gate 		} else {
46417c478bd9Sstevel@tonic-gate 			/*
46427c478bd9Sstevel@tonic-gate 			 * conn disconnect already scheduled or will
46437c478bd9Sstevel@tonic-gate 			 * happen from conn_release when c_ref drops to 0.
46447c478bd9Sstevel@tonic-gate 			 */
46457c478bd9Sstevel@tonic-gate 			mutex_exit(&conn->c_lock);
46467c478bd9Sstevel@tonic-gate 		}
46477c478bd9Sstevel@tonic-gate 		conn = conn->c_next;
46487c478bd9Sstevel@tonic-gate 	}
46497c478bd9Sstevel@tonic-gate 	rw_exit(&connlist->conn_lock);
46507c478bd9Sstevel@tonic-gate 
46517c478bd9Sstevel@tonic-gate 	/*
46527c478bd9Sstevel@tonic-gate 	 * At this point, only connections with c_ref != 0 are on the list
46537c478bd9Sstevel@tonic-gate 	 */
46547c478bd9Sstevel@tonic-gate }
46557c478bd9Sstevel@tonic-gate 
46567c478bd9Sstevel@tonic-gate /*
4657065714dcSSiddheshwar Mahesh  * Free all the HCA resources and close
4658065714dcSSiddheshwar Mahesh  * the hca.
4659065714dcSSiddheshwar Mahesh  */
4660065714dcSSiddheshwar Mahesh 
4661065714dcSSiddheshwar Mahesh static void
4662065714dcSSiddheshwar Mahesh rib_free_hca(rib_hca_t *hca)
4663065714dcSSiddheshwar Mahesh {
4664065714dcSSiddheshwar Mahesh 	(void) ibt_free_cq(hca->clnt_rcq->rib_cq_hdl);
4665065714dcSSiddheshwar Mahesh 	(void) ibt_free_cq(hca->clnt_scq->rib_cq_hdl);
4666065714dcSSiddheshwar Mahesh 	(void) ibt_free_cq(hca->svc_rcq->rib_cq_hdl);
4667065714dcSSiddheshwar Mahesh 	(void) ibt_free_cq(hca->svc_scq->rib_cq_hdl);
4668065714dcSSiddheshwar Mahesh 
4669065714dcSSiddheshwar Mahesh 	kmem_free(hca->clnt_rcq, sizeof (rib_cq_t));
4670065714dcSSiddheshwar Mahesh 	kmem_free(hca->clnt_scq, sizeof (rib_cq_t));
4671065714dcSSiddheshwar Mahesh 	kmem_free(hca->svc_rcq, sizeof (rib_cq_t));
4672065714dcSSiddheshwar Mahesh 	kmem_free(hca->svc_scq, sizeof (rib_cq_t));
4673065714dcSSiddheshwar Mahesh 
4674065714dcSSiddheshwar Mahesh 	rib_rbufpool_destroy(hca, RECV_BUFFER);
4675065714dcSSiddheshwar Mahesh 	rib_rbufpool_destroy(hca, SEND_BUFFER);
4676065714dcSSiddheshwar Mahesh 	rib_destroy_cache(hca);
4677065714dcSSiddheshwar Mahesh 	if (rib_mod.rdma_count == 0)
4678065714dcSSiddheshwar Mahesh 		rdma_unregister_mod(&rib_mod);
4679065714dcSSiddheshwar Mahesh 	(void) ibt_free_pd(hca->hca_hdl, hca->pd_hdl);
4680065714dcSSiddheshwar Mahesh 	(void) ibt_close_hca(hca->hca_hdl);
4681065714dcSSiddheshwar Mahesh 	hca->hca_hdl = NULL;
4682065714dcSSiddheshwar Mahesh }
4683065714dcSSiddheshwar Mahesh 
4684*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
46857c478bd9Sstevel@tonic-gate static void
4686*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rib_stop_hca_services(rib_hca_t *hca)
46877c478bd9Sstevel@tonic-gate {
46887c478bd9Sstevel@tonic-gate 	rib_stop_services(hca);
46897c478bd9Sstevel@tonic-gate 	rib_close_channels(&hca->cl_conn_list);
46907c478bd9Sstevel@tonic-gate 	rib_close_channels(&hca->srv_conn_list);
469151f34d4bSRajkumar Sivaprakasam 
469251f34d4bSRajkumar Sivaprakasam 	rib_purge_connlist(&hca->cl_conn_list);
469351f34d4bSRajkumar Sivaprakasam 	rib_purge_connlist(&hca->srv_conn_list);
469451f34d4bSRajkumar Sivaprakasam 
4695*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if ((rib_stat->hcas_list == NULL) && stats_enabled) {
469651f34d4bSRajkumar Sivaprakasam 		kstat_delete_byname_zone("unix", 0, "rpcib_cache",
469751f34d4bSRajkumar Sivaprakasam 		    GLOBAL_ZONEID);
4698*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		stats_enabled = FALSE;
469951f34d4bSRajkumar Sivaprakasam 	}
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate 	rw_enter(&hca->srv_conn_list.conn_lock, RW_READER);
47027c478bd9Sstevel@tonic-gate 	rw_enter(&hca->cl_conn_list.conn_lock, RW_READER);
47037c478bd9Sstevel@tonic-gate 	if (hca->srv_conn_list.conn_hd == NULL &&
47047c478bd9Sstevel@tonic-gate 	    hca->cl_conn_list.conn_hd == NULL) {
47057c478bd9Sstevel@tonic-gate 		/*
47067c478bd9Sstevel@tonic-gate 		 * conn_lists are NULL, so destroy
47077c478bd9Sstevel@tonic-gate 		 * buffers, close hca and be done.
47087c478bd9Sstevel@tonic-gate 		 */
4709065714dcSSiddheshwar Mahesh 		rib_free_hca(hca);
47107c478bd9Sstevel@tonic-gate 	}
47117c478bd9Sstevel@tonic-gate 	rw_exit(&hca->cl_conn_list.conn_lock);
47127c478bd9Sstevel@tonic-gate 	rw_exit(&hca->srv_conn_list.conn_lock);
47137c478bd9Sstevel@tonic-gate 
47147c478bd9Sstevel@tonic-gate 	if (hca->hca_hdl != NULL) {
47157c478bd9Sstevel@tonic-gate 		mutex_enter(&hca->inuse_lock);
47167c478bd9Sstevel@tonic-gate 		while (hca->inuse)
47177c478bd9Sstevel@tonic-gate 			cv_wait(&hca->cb_cv, &hca->inuse_lock);
47187c478bd9Sstevel@tonic-gate 		mutex_exit(&hca->inuse_lock);
471951f34d4bSRajkumar Sivaprakasam 
4720065714dcSSiddheshwar Mahesh 		rib_free_hca(hca);
4721065714dcSSiddheshwar Mahesh 	}
4722*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_destroy(&hca->bound_services_lock);
472351f34d4bSRajkumar Sivaprakasam 
4724065714dcSSiddheshwar Mahesh 	if (hca->cleanup_helper != NULL) {
4725065714dcSSiddheshwar Mahesh 		ddi_taskq_destroy(hca->cleanup_helper);
4726065714dcSSiddheshwar Mahesh 		hca->cleanup_helper = NULL;
47277c478bd9Sstevel@tonic-gate 	}
47287c478bd9Sstevel@tonic-gate }
47290a701b1eSRobert Gordon 
4730*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States /*
4731*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  * Cleans and closes up all uses of the HCA
4732*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States  */
4733*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static void
4734*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rib_detach_hca(rib_hca_t *hca)
4735*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States {
4736*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_hca_t **hcap;
4737*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4738*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	/*
4739*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * Stop all services on the HCA
4740*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * Go through cl_conn_list and close all rc_channels
4741*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * Go through svr_conn_list and close all rc_channels
4742*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * Free connections whose c_ref has dropped to 0
4743*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * Destroy all CQs
4744*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * Deregister and released all buffer pool memory after all
4745*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * connections are destroyed
4746*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * Free the protection domain
4747*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 * ibt_close_hca()
4748*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	 */
4749*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&hca->state_lock, RW_WRITER);
4750*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (hca->state == HCA_DETACHED) {
4751*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rw_exit(&hca->state_lock);
4752*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		return;
4753*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
4754*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4755*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	hca->state = HCA_DETACHED;
4756*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->hcas_list_lock, RW_WRITER);
4757*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	for (hcap = &rib_stat->hcas_list; *hcap && (*hcap != hca);
4758*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	    hcap = &(*hcap)->next)
4759*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		;
4760*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	ASSERT(*hcap == hca);
4761*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	*hcap = hca->next;
4762*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_stat->nhca_inited--;
4763*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_mod.rdma_count--;
4764*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->hcas_list_lock);
4765*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&hca->state_lock);
4766*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4767*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_stop_hca_services(hca);
4768*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4769*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	kmem_free(hca, sizeof (*hca));
4770*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States }
4771*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
47720a701b1eSRobert Gordon static void
47730a701b1eSRobert Gordon rib_server_side_cache_reclaim(void *argp)
47740a701b1eSRobert Gordon {
47750a701b1eSRobert Gordon 	cache_avl_struct_t    *rcas;
47760a701b1eSRobert Gordon 	rib_lrc_entry_t		*rb;
47770a701b1eSRobert Gordon 	rib_hca_t *hca = (rib_hca_t *)argp;
47780a701b1eSRobert Gordon 
47790a701b1eSRobert Gordon 	rw_enter(&hca->avl_rw_lock, RW_WRITER);
47800a701b1eSRobert Gordon 	rcas = avl_first(&hca->avl_tree);
47810a701b1eSRobert Gordon 	if (rcas != NULL)
47820a701b1eSRobert Gordon 		avl_remove(&hca->avl_tree, rcas);
47830a701b1eSRobert Gordon 
47840a701b1eSRobert Gordon 	while (rcas != NULL) {
47850a701b1eSRobert Gordon 		while (rcas->r.forw != &rcas->r) {
47860a701b1eSRobert Gordon 			rcas->elements--;
47870a701b1eSRobert Gordon 			rb = rcas->r.forw;
47880a701b1eSRobert Gordon 			remque(rb);
47890a701b1eSRobert Gordon 			if (rb->registered)
47900a701b1eSRobert Gordon 				(void) rib_deregistermem_via_hca(hca,
47910a701b1eSRobert Gordon 				    rb->lrc_buf, rb->lrc_mhandle);
4792*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4793*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			hca->cache_allocation -= rb->lrc_len;
47940a701b1eSRobert Gordon 			kmem_free(rb->lrc_buf, rb->lrc_len);
47950a701b1eSRobert Gordon 			kmem_free(rb, sizeof (rib_lrc_entry_t));
47960a701b1eSRobert Gordon 		}
47970a701b1eSRobert Gordon 		mutex_destroy(&rcas->node_lock);
47980a701b1eSRobert Gordon 		kmem_cache_free(hca->server_side_cache, rcas);
47990a701b1eSRobert Gordon 		rcas = avl_first(&hca->avl_tree);
48000a701b1eSRobert Gordon 		if (rcas != NULL)
48010a701b1eSRobert Gordon 			avl_remove(&hca->avl_tree, rcas);
48020a701b1eSRobert Gordon 	}
48030a701b1eSRobert Gordon 	rw_exit(&hca->avl_rw_lock);
48040a701b1eSRobert Gordon }
48050a701b1eSRobert Gordon 
48060a701b1eSRobert Gordon static void
48070a701b1eSRobert Gordon rib_server_side_cache_cleanup(void *argp)
48080a701b1eSRobert Gordon {
48090a701b1eSRobert Gordon 	cache_avl_struct_t    *rcas;
48100a701b1eSRobert Gordon 	rib_lrc_entry_t		*rb;
48110a701b1eSRobert Gordon 	rib_hca_t *hca = (rib_hca_t *)argp;
48120a701b1eSRobert Gordon 
4813*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_enter(&hca->cache_allocation_lock);
4814*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	if (hca->cache_allocation < cache_limit) {
4815*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_exit(&hca->cache_allocation_lock);
48160a701b1eSRobert Gordon 		return;
48170a701b1eSRobert Gordon 	}
4818*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_exit(&hca->cache_allocation_lock);
48190a701b1eSRobert Gordon 
48200a701b1eSRobert Gordon 	rw_enter(&hca->avl_rw_lock, RW_WRITER);
48210a701b1eSRobert Gordon 	rcas = avl_last(&hca->avl_tree);
48220a701b1eSRobert Gordon 	if (rcas != NULL)
48230a701b1eSRobert Gordon 		avl_remove(&hca->avl_tree, rcas);
48240a701b1eSRobert Gordon 
48250a701b1eSRobert Gordon 	while (rcas != NULL) {
48260a701b1eSRobert Gordon 		while (rcas->r.forw != &rcas->r) {
48270a701b1eSRobert Gordon 			rcas->elements--;
48280a701b1eSRobert Gordon 			rb = rcas->r.forw;
48290a701b1eSRobert Gordon 			remque(rb);
48300a701b1eSRobert Gordon 			if (rb->registered)
48310a701b1eSRobert Gordon 				(void) rib_deregistermem_via_hca(hca,
48320a701b1eSRobert Gordon 				    rb->lrc_buf, rb->lrc_mhandle);
4833*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4834*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			hca->cache_allocation -= rb->lrc_len;
4835*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
48360a701b1eSRobert Gordon 			kmem_free(rb->lrc_buf, rb->lrc_len);
48370a701b1eSRobert Gordon 			kmem_free(rb, sizeof (rib_lrc_entry_t));
48380a701b1eSRobert Gordon 		}
48390a701b1eSRobert Gordon 		mutex_destroy(&rcas->node_lock);
484051f34d4bSRajkumar Sivaprakasam 		if (hca->server_side_cache) {
48410a701b1eSRobert Gordon 			kmem_cache_free(hca->server_side_cache, rcas);
484251f34d4bSRajkumar Sivaprakasam 		}
4843*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4844*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		if (hca->cache_allocation < cache_limit) {
48450a701b1eSRobert Gordon 			rw_exit(&hca->avl_rw_lock);
48460a701b1eSRobert Gordon 			return;
48470a701b1eSRobert Gordon 		}
48480a701b1eSRobert Gordon 
48490a701b1eSRobert Gordon 		rcas = avl_last(&hca->avl_tree);
48500a701b1eSRobert Gordon 		if (rcas != NULL)
48510a701b1eSRobert Gordon 			avl_remove(&hca->avl_tree, rcas);
48520a701b1eSRobert Gordon 	}
48530a701b1eSRobert Gordon 	rw_exit(&hca->avl_rw_lock);
48540a701b1eSRobert Gordon }
48550a701b1eSRobert Gordon 
48560a701b1eSRobert Gordon static int
48570a701b1eSRobert Gordon avl_compare(const void *t1, const void *t2)
48580a701b1eSRobert Gordon {
48590a701b1eSRobert Gordon 	if (((cache_avl_struct_t *)t1)->len == ((cache_avl_struct_t *)t2)->len)
48600a701b1eSRobert Gordon 		return (0);
48610a701b1eSRobert Gordon 
48620a701b1eSRobert Gordon 	if (((cache_avl_struct_t *)t1)->len < ((cache_avl_struct_t *)t2)->len)
48630a701b1eSRobert Gordon 		return (-1);
48640a701b1eSRobert Gordon 
48650a701b1eSRobert Gordon 	return (1);
48660a701b1eSRobert Gordon }
48670a701b1eSRobert Gordon 
48680a701b1eSRobert Gordon static void
48690a701b1eSRobert Gordon rib_destroy_cache(rib_hca_t *hca)
48700a701b1eSRobert Gordon {
487151f34d4bSRajkumar Sivaprakasam 	if (hca->avl_init) {
487251f34d4bSRajkumar Sivaprakasam 		rib_server_side_cache_reclaim((void *)hca);
487351f34d4bSRajkumar Sivaprakasam 		if (hca->server_side_cache) {
48740a701b1eSRobert Gordon 			kmem_cache_destroy(hca->server_side_cache);
487551f34d4bSRajkumar Sivaprakasam 			hca->server_side_cache = NULL;
487651f34d4bSRajkumar Sivaprakasam 		}
48770a701b1eSRobert Gordon 		avl_destroy(&hca->avl_tree);
4878*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_destroy(&hca->cache_allocation_lock);
48790a701b1eSRobert Gordon 		rw_destroy(&hca->avl_rw_lock);
48800a701b1eSRobert Gordon 	}
48810a701b1eSRobert Gordon 	hca->avl_init = FALSE;
48820a701b1eSRobert Gordon }
48830a701b1eSRobert Gordon 
48840a701b1eSRobert Gordon static void
48850a701b1eSRobert Gordon rib_force_cleanup(void *hca)
48860a701b1eSRobert Gordon {
4887065714dcSSiddheshwar Mahesh 	if (((rib_hca_t *)hca)->cleanup_helper != NULL)
48880a701b1eSRobert Gordon 		(void) ddi_taskq_dispatch(
4889065714dcSSiddheshwar Mahesh 		    ((rib_hca_t *)hca)->cleanup_helper,
48900a701b1eSRobert Gordon 		    rib_server_side_cache_cleanup,
48910a701b1eSRobert Gordon 		    (void *)hca, DDI_NOSLEEP);
48920a701b1eSRobert Gordon }
48930a701b1eSRobert Gordon 
48940a701b1eSRobert Gordon static rib_lrc_entry_t *
48950a701b1eSRobert Gordon rib_get_cache_buf(CONN *conn, uint32_t len)
48960a701b1eSRobert Gordon {
48970a701b1eSRobert Gordon 	cache_avl_struct_t	cas, *rcas;
48980a701b1eSRobert Gordon 	rib_hca_t	*hca = (ctoqp(conn))->hca;
48990a701b1eSRobert Gordon 	rib_lrc_entry_t *reply_buf;
49000a701b1eSRobert Gordon 	avl_index_t where = NULL;
49010a701b1eSRobert Gordon 	uint64_t c_alloc = 0;
49020a701b1eSRobert Gordon 
49030a701b1eSRobert Gordon 	if (!hca->avl_init)
49040a701b1eSRobert Gordon 		goto  error_alloc;
49050a701b1eSRobert Gordon 
49060a701b1eSRobert Gordon 	cas.len = len;
49070a701b1eSRobert Gordon 
49080a701b1eSRobert Gordon 	rw_enter(&hca->avl_rw_lock, RW_READER);
49090a701b1eSRobert Gordon 
4910*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_enter(&hca->cache_allocation_lock);
4911*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	c_alloc = hca->cache_allocation;
4912*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	mutex_exit(&hca->cache_allocation_lock);
49130a701b1eSRobert Gordon 
49140a701b1eSRobert Gordon 	if ((rcas = (cache_avl_struct_t *)avl_find(&hca->avl_tree, &cas,
49150a701b1eSRobert Gordon 	    &where)) == NULL) {
49160a701b1eSRobert Gordon 		/* Am I above the cache limit */
49170a701b1eSRobert Gordon 		if ((c_alloc + len) >= cache_limit) {
49180a701b1eSRobert Gordon 			rib_force_cleanup((void *)hca);
49190a701b1eSRobert Gordon 			rw_exit(&hca->avl_rw_lock);
4920*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			mutex_enter(&hca->cache_allocation_lock);
4921*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			hca->cache_misses_above_the_limit ++;
4922*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			mutex_exit(&hca->cache_allocation_lock);
49230a701b1eSRobert Gordon 
49240a701b1eSRobert Gordon 			/* Allocate and register the buffer directly */
49250a701b1eSRobert Gordon 			goto error_alloc;
49260a701b1eSRobert Gordon 		}
49270a701b1eSRobert Gordon 
49280a701b1eSRobert Gordon 		rw_exit(&hca->avl_rw_lock);
49290a701b1eSRobert Gordon 		rw_enter(&hca->avl_rw_lock, RW_WRITER);
49300a701b1eSRobert Gordon 
49310a701b1eSRobert Gordon 		/* Recheck to make sure no other thread added the entry in */
49320a701b1eSRobert Gordon 		if ((rcas = (cache_avl_struct_t *)avl_find(&hca->avl_tree,
49330a701b1eSRobert Gordon 		    &cas, &where)) == NULL) {
49340a701b1eSRobert Gordon 			/* Allocate an avl tree entry */
49350a701b1eSRobert Gordon 			rcas = (cache_avl_struct_t *)
49360a701b1eSRobert Gordon 			    kmem_cache_alloc(hca->server_side_cache, KM_SLEEP);
49370a701b1eSRobert Gordon 
49380a701b1eSRobert Gordon 			bzero(rcas, sizeof (cache_avl_struct_t));
49390a701b1eSRobert Gordon 			rcas->elements = 0;
49400a701b1eSRobert Gordon 			rcas->r.forw = &rcas->r;
49410a701b1eSRobert Gordon 			rcas->r.back = &rcas->r;
49420a701b1eSRobert Gordon 			rcas->len = len;
49430a701b1eSRobert Gordon 			mutex_init(&rcas->node_lock, NULL, MUTEX_DEFAULT, NULL);
49440a701b1eSRobert Gordon 			avl_insert(&hca->avl_tree, rcas, where);
49450a701b1eSRobert Gordon 		}
49460a701b1eSRobert Gordon 	}
49470a701b1eSRobert Gordon 
49480a701b1eSRobert Gordon 	mutex_enter(&rcas->node_lock);
49490a701b1eSRobert Gordon 
49500a701b1eSRobert Gordon 	if (rcas->r.forw != &rcas->r && rcas->elements > 0) {
49510a701b1eSRobert Gordon 		reply_buf = rcas->r.forw;
49520a701b1eSRobert Gordon 		remque(reply_buf);
49530a701b1eSRobert Gordon 		rcas->elements--;
49540a701b1eSRobert Gordon 		mutex_exit(&rcas->node_lock);
49550a701b1eSRobert Gordon 		rw_exit(&hca->avl_rw_lock);
4956*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4957*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_enter(&hca->cache_allocation_lock);
4958*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca->cache_hits++;
4959*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca->cache_allocation -= len;
4960*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_exit(&hca->cache_allocation_lock);
49610a701b1eSRobert Gordon 	} else {
49620a701b1eSRobert Gordon 		/* Am I above the cache limit */
49630a701b1eSRobert Gordon 		mutex_exit(&rcas->node_lock);
49640a701b1eSRobert Gordon 		if ((c_alloc + len) >= cache_limit) {
49650a701b1eSRobert Gordon 			rib_force_cleanup((void *)hca);
49660a701b1eSRobert Gordon 			rw_exit(&hca->avl_rw_lock);
4967*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
4968*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			mutex_enter(&hca->cache_allocation_lock);
4969*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			hca->cache_misses_above_the_limit++;
4970*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 			mutex_exit(&hca->cache_allocation_lock);
49710a701b1eSRobert Gordon 			/* Allocate and register the buffer directly */
49720a701b1eSRobert Gordon 			goto error_alloc;
49730a701b1eSRobert Gordon 		}
49740a701b1eSRobert Gordon 		rw_exit(&hca->avl_rw_lock);
4975*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_enter(&hca->cache_allocation_lock);
4976*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca->cache_misses++;
4977*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_exit(&hca->cache_allocation_lock);
49780a701b1eSRobert Gordon 		/* Allocate a reply_buf entry */
49790a701b1eSRobert Gordon 		reply_buf = (rib_lrc_entry_t *)
49800a701b1eSRobert Gordon 		    kmem_zalloc(sizeof (rib_lrc_entry_t), KM_SLEEP);
49810a701b1eSRobert Gordon 		bzero(reply_buf, sizeof (rib_lrc_entry_t));
49820a701b1eSRobert Gordon 		reply_buf->lrc_buf  = kmem_alloc(len, KM_SLEEP);
49830a701b1eSRobert Gordon 		reply_buf->lrc_len  = len;
49840a701b1eSRobert Gordon 		reply_buf->registered = FALSE;
49850a701b1eSRobert Gordon 		reply_buf->avl_node = (void *)rcas;
49860a701b1eSRobert Gordon 	}
49870a701b1eSRobert Gordon 
49880a701b1eSRobert Gordon 	return (reply_buf);
49890a701b1eSRobert Gordon 
49900a701b1eSRobert Gordon error_alloc:
49910a701b1eSRobert Gordon 	reply_buf = (rib_lrc_entry_t *)
49920a701b1eSRobert Gordon 	    kmem_zalloc(sizeof (rib_lrc_entry_t), KM_SLEEP);
49930a701b1eSRobert Gordon 	bzero(reply_buf, sizeof (rib_lrc_entry_t));
49940a701b1eSRobert Gordon 	reply_buf->lrc_buf = kmem_alloc(len, KM_SLEEP);
49950a701b1eSRobert Gordon 	reply_buf->lrc_len = len;
49960a701b1eSRobert Gordon 	reply_buf->registered = FALSE;
49970a701b1eSRobert Gordon 	reply_buf->avl_node = NULL;
49980a701b1eSRobert Gordon 
49990a701b1eSRobert Gordon 	return (reply_buf);
50000a701b1eSRobert Gordon }
50010a701b1eSRobert Gordon 
50020a701b1eSRobert Gordon /*
50030a701b1eSRobert Gordon  * Return a pre-registered back to the cache (without
50040a701b1eSRobert Gordon  * unregistering the buffer)..
50050a701b1eSRobert Gordon  */
50060a701b1eSRobert Gordon 
50070a701b1eSRobert Gordon static void
50080a701b1eSRobert Gordon rib_free_cache_buf(CONN *conn, rib_lrc_entry_t *reg_buf)
50090a701b1eSRobert Gordon {
50100a701b1eSRobert Gordon 	cache_avl_struct_t    cas, *rcas;
50110a701b1eSRobert Gordon 	avl_index_t where = NULL;
50120a701b1eSRobert Gordon 	rib_hca_t	*hca = (ctoqp(conn))->hca;
50130a701b1eSRobert Gordon 
50140a701b1eSRobert Gordon 	if (!hca->avl_init)
50150a701b1eSRobert Gordon 		goto  error_free;
50160a701b1eSRobert Gordon 
50170a701b1eSRobert Gordon 	cas.len = reg_buf->lrc_len;
50180a701b1eSRobert Gordon 	rw_enter(&hca->avl_rw_lock, RW_READER);
50190a701b1eSRobert Gordon 	if ((rcas = (cache_avl_struct_t *)
50200a701b1eSRobert Gordon 	    avl_find(&hca->avl_tree, &cas, &where)) == NULL) {
50210a701b1eSRobert Gordon 		rw_exit(&hca->avl_rw_lock);
50220a701b1eSRobert Gordon 		goto error_free;
50230a701b1eSRobert Gordon 	} else {
50240a701b1eSRobert Gordon 		cas.len = reg_buf->lrc_len;
50250a701b1eSRobert Gordon 		mutex_enter(&rcas->node_lock);
50260a701b1eSRobert Gordon 		insque(reg_buf, &rcas->r);
50270a701b1eSRobert Gordon 		rcas->elements ++;
50280a701b1eSRobert Gordon 		mutex_exit(&rcas->node_lock);
50290a701b1eSRobert Gordon 		rw_exit(&hca->avl_rw_lock);
5030*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_enter(&hca->cache_allocation_lock);
5031*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		hca->cache_allocation += cas.len;
5032*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		mutex_exit(&hca->cache_allocation_lock);
50330a701b1eSRobert Gordon 	}
50340a701b1eSRobert Gordon 
50350a701b1eSRobert Gordon 	return;
50360a701b1eSRobert Gordon 
50370a701b1eSRobert Gordon error_free:
50380a701b1eSRobert Gordon 
50390a701b1eSRobert Gordon 	if (reg_buf->registered)
50400a701b1eSRobert Gordon 		(void) rib_deregistermem_via_hca(hca,
50410a701b1eSRobert Gordon 		    reg_buf->lrc_buf, reg_buf->lrc_mhandle);
50420a701b1eSRobert Gordon 	kmem_free(reg_buf->lrc_buf, reg_buf->lrc_len);
50430a701b1eSRobert Gordon 	kmem_free(reg_buf, sizeof (rib_lrc_entry_t));
50440a701b1eSRobert Gordon }
50450a701b1eSRobert Gordon 
50460a701b1eSRobert Gordon static rdma_stat
50470a701b1eSRobert Gordon rib_registermem_via_hca(rib_hca_t *hca, caddr_t adsp, caddr_t buf,
50480a701b1eSRobert Gordon 	uint_t buflen, struct mrc *buf_handle)
50490a701b1eSRobert Gordon {
50500a701b1eSRobert Gordon 	ibt_mr_hdl_t	mr_hdl = NULL;	/* memory region handle */
50510a701b1eSRobert Gordon 	ibt_mr_desc_t	mr_desc;	/* vaddr, lkey, rkey */
50520a701b1eSRobert Gordon 	rdma_stat	status;
50530a701b1eSRobert Gordon 
50540a701b1eSRobert Gordon 
50550a701b1eSRobert Gordon 	/*
50560a701b1eSRobert Gordon 	 * Note: ALL buffer pools use the same memory type RDMARW.
50570a701b1eSRobert Gordon 	 */
50580a701b1eSRobert Gordon 	status = rib_reg_mem(hca, adsp, buf, buflen, 0, &mr_hdl, &mr_desc);
50590a701b1eSRobert Gordon 	if (status == RDMA_SUCCESS) {
50600a701b1eSRobert Gordon 		buf_handle->mrc_linfo = (uint64_t)(uintptr_t)mr_hdl;
50610a701b1eSRobert Gordon 		buf_handle->mrc_lmr = (uint32_t)mr_desc.md_lkey;
50620a701b1eSRobert Gordon 		buf_handle->mrc_rmr = (uint32_t)mr_desc.md_rkey;
50630a701b1eSRobert Gordon 	} else {
50640a701b1eSRobert Gordon 		buf_handle->mrc_linfo = NULL;
50650a701b1eSRobert Gordon 		buf_handle->mrc_lmr = 0;
50660a701b1eSRobert Gordon 		buf_handle->mrc_rmr = 0;
50670a701b1eSRobert Gordon 	}
50680a701b1eSRobert Gordon 	return (status);
50690a701b1eSRobert Gordon }
50700a701b1eSRobert Gordon 
50710a701b1eSRobert Gordon /* ARGSUSED */
50720a701b1eSRobert Gordon static rdma_stat
50730a701b1eSRobert Gordon rib_deregistermemsync_via_hca(rib_hca_t *hca, caddr_t buf,
50740a701b1eSRobert Gordon     struct mrc buf_handle, RIB_SYNCMEM_HANDLE sync_handle)
50750a701b1eSRobert Gordon {
50760a701b1eSRobert Gordon 
50770a701b1eSRobert Gordon 	(void) rib_deregistermem_via_hca(hca, buf, buf_handle);
50780a701b1eSRobert Gordon 	return (RDMA_SUCCESS);
50790a701b1eSRobert Gordon }
50800a701b1eSRobert Gordon 
50810a701b1eSRobert Gordon /* ARGSUSED */
50820a701b1eSRobert Gordon static rdma_stat
50830a701b1eSRobert Gordon rib_deregistermem_via_hca(rib_hca_t *hca, caddr_t buf, struct mrc buf_handle)
50840a701b1eSRobert Gordon {
50850a701b1eSRobert Gordon 
50860a701b1eSRobert Gordon 	(void) ibt_deregister_mr(hca->hca_hdl,
50870a701b1eSRobert Gordon 	    (ibt_mr_hdl_t)(uintptr_t)buf_handle.mrc_linfo);
50880a701b1eSRobert Gordon 	return (RDMA_SUCCESS);
50890a701b1eSRobert Gordon }
50900a701b1eSRobert Gordon 
50910a701b1eSRobert Gordon /*
5092e11c3f44Smeem  * Check if the IP interface named by `lifrp' is RDMA-capable.
50930a701b1eSRobert Gordon  */
5094e11c3f44Smeem static boolean_t
5095e11c3f44Smeem rpcib_rdma_capable_interface(struct lifreq *lifrp)
50960a701b1eSRobert Gordon {
5097e11c3f44Smeem 	char ifname[LIFNAMSIZ];
5098e11c3f44Smeem 	char *cp;
50990a701b1eSRobert Gordon 
5100e11c3f44Smeem 	if (lifrp->lifr_type == IFT_IB)
5101e11c3f44Smeem 		return (B_TRUE);
51020a701b1eSRobert Gordon 
51030a701b1eSRobert Gordon 	/*
5104e11c3f44Smeem 	 * Strip off the logical interface portion before getting
5105e11c3f44Smeem 	 * intimate with the name.
51060a701b1eSRobert Gordon 	 */
5107e11c3f44Smeem 	(void) strlcpy(ifname, lifrp->lifr_name, LIFNAMSIZ);
5108e11c3f44Smeem 	if ((cp = strchr(ifname, ':')) != NULL)
5109e11c3f44Smeem 		*cp = '\0';
51100a701b1eSRobert Gordon 
5111e11c3f44Smeem 	return (strcmp("lo0", ifname) == 0);
51120a701b1eSRobert Gordon }
51130a701b1eSRobert Gordon 
51140a701b1eSRobert Gordon static int
5115e11c3f44Smeem rpcib_do_ip_ioctl(int cmd, int len, void *arg)
51160a701b1eSRobert Gordon {
51170a701b1eSRobert Gordon 	vnode_t *kvp, *vp;
51180a701b1eSRobert Gordon 	TIUSER  *tiptr;
51190a701b1eSRobert Gordon 	struct  strioctl iocb;
51200a701b1eSRobert Gordon 	k_sigset_t smask;
51210a701b1eSRobert Gordon 	int	err = 0;
51220a701b1eSRobert Gordon 
5123e11c3f44Smeem 	if (lookupname("/dev/udp", UIO_SYSSPACE, FOLLOW, NULLVPP, &kvp) == 0) {
5124e11c3f44Smeem 		if (t_kopen(NULL, kvp->v_rdev, FREAD|FWRITE,
51250a701b1eSRobert Gordon 		    &tiptr, CRED()) == 0) {
51260a701b1eSRobert Gordon 			vp = tiptr->fp->f_vnode;
51270a701b1eSRobert Gordon 		} else {
51280a701b1eSRobert Gordon 			VN_RELE(kvp);
51290a701b1eSRobert Gordon 			return (EPROTO);
51300a701b1eSRobert Gordon 		}
51310a701b1eSRobert Gordon 	} else {
51320a701b1eSRobert Gordon 		return (EPROTO);
51330a701b1eSRobert Gordon 	}
51340a701b1eSRobert Gordon 
51350a701b1eSRobert Gordon 	iocb.ic_cmd = cmd;
51360a701b1eSRobert Gordon 	iocb.ic_timout = 0;
51370a701b1eSRobert Gordon 	iocb.ic_len = len;
5138e11c3f44Smeem 	iocb.ic_dp = (caddr_t)arg;
51390a701b1eSRobert Gordon 	sigintr(&smask, 0);
51400a701b1eSRobert Gordon 	err = kstr_ioctl(vp, I_STR, (intptr_t)&iocb);
51410a701b1eSRobert Gordon 	sigunintr(&smask);
51420a701b1eSRobert Gordon 	(void) t_kclose(tiptr, 0);
51430a701b1eSRobert Gordon 	VN_RELE(kvp);
51440a701b1eSRobert Gordon 	return (err);
51450a701b1eSRobert Gordon }
51460a701b1eSRobert Gordon 
5147e11c3f44Smeem /*
5148e11c3f44Smeem  * Issue an SIOCGLIFCONF down to IP and return the result in `lifcp'.
5149e11c3f44Smeem  * lifcp->lifc_buf is dynamically allocated to be *bufsizep bytes.
5150e11c3f44Smeem  */
5151e11c3f44Smeem static int
5152e11c3f44Smeem rpcib_do_lifconf(struct lifconf *lifcp, uint_t *bufsizep)
5153e11c3f44Smeem {
5154e11c3f44Smeem 	int err;
5155e11c3f44Smeem 	struct lifnum lifn;
5156e11c3f44Smeem 
5157e11c3f44Smeem 	bzero(&lifn, sizeof (struct lifnum));
5158e11c3f44Smeem 	lifn.lifn_family = AF_UNSPEC;
5159e11c3f44Smeem 
5160e11c3f44Smeem 	err = rpcib_do_ip_ioctl(SIOCGLIFNUM, sizeof (struct lifnum), &lifn);
5161e11c3f44Smeem 	if (err != 0)
5162e11c3f44Smeem 		return (err);
5163e11c3f44Smeem 
5164e11c3f44Smeem 	/*
5165e11c3f44Smeem 	 * Pad the interface count to account for additional interfaces that
5166e11c3f44Smeem 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
5167e11c3f44Smeem 	 */
5168e11c3f44Smeem 	lifn.lifn_count += 4;
5169e11c3f44Smeem 
5170e11c3f44Smeem 	bzero(lifcp, sizeof (struct lifconf));
5171e11c3f44Smeem 	lifcp->lifc_family = AF_UNSPEC;
5172e11c3f44Smeem 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
5173e11c3f44Smeem 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
5174e11c3f44Smeem 
5175e11c3f44Smeem 	err = rpcib_do_ip_ioctl(SIOCGLIFCONF, sizeof (struct lifconf), lifcp);
5176e11c3f44Smeem 	if (err != 0) {
5177e11c3f44Smeem 		kmem_free(lifcp->lifc_buf, *bufsizep);
5178e11c3f44Smeem 		return (err);
51790a701b1eSRobert Gordon 	}
5180e11c3f44Smeem 	return (0);
51810a701b1eSRobert Gordon }
51820a701b1eSRobert Gordon 
51830a701b1eSRobert Gordon static boolean_t
5184e11c3f44Smeem rpcib_get_ib_addresses(rpcib_ipaddrs_t *addrs4, rpcib_ipaddrs_t *addrs6)
51850a701b1eSRobert Gordon {
5186e11c3f44Smeem 	uint_t i, nifs;
5187e11c3f44Smeem 	uint_t bufsize;
5188e11c3f44Smeem 	struct lifconf lifc;
5189e11c3f44Smeem 	struct lifreq *lifrp;
5190e11c3f44Smeem 	struct sockaddr_in *sinp;
5191e11c3f44Smeem 	struct sockaddr_in6 *sin6p;
51920a701b1eSRobert Gordon 
5193e11c3f44Smeem 	bzero(addrs4, sizeof (rpcib_ipaddrs_t));
5194e11c3f44Smeem 	bzero(addrs6, sizeof (rpcib_ipaddrs_t));
51950a701b1eSRobert Gordon 
5196e11c3f44Smeem 	if (rpcib_do_lifconf(&lifc, &bufsize) != 0)
5197e11c3f44Smeem 		return (B_FALSE);
5198e11c3f44Smeem 
5199e11c3f44Smeem 	if ((nifs = lifc.lifc_len / sizeof (struct lifreq)) == 0) {
5200e11c3f44Smeem 		kmem_free(lifc.lifc_buf, bufsize);
5201e11c3f44Smeem 		return (B_FALSE);
52020a701b1eSRobert Gordon 	}
52030a701b1eSRobert Gordon 
5204e11c3f44Smeem 	/*
5205e11c3f44Smeem 	 * Worst case is that all of the addresses are IB-capable and have
5206e11c3f44Smeem 	 * the same address family, so size our buffers accordingly.
5207e11c3f44Smeem 	 */
5208e11c3f44Smeem 	addrs4->ri_size = nifs * sizeof (struct sockaddr_in);
5209e11c3f44Smeem 	addrs4->ri_list = kmem_zalloc(addrs4->ri_size, KM_SLEEP);
5210e11c3f44Smeem 	addrs6->ri_size = nifs * sizeof (struct sockaddr_in6);
5211e11c3f44Smeem 	addrs6->ri_list = kmem_zalloc(addrs6->ri_size, KM_SLEEP);
52120a701b1eSRobert Gordon 
5213e11c3f44Smeem 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
5214e11c3f44Smeem 		if (!rpcib_rdma_capable_interface(lifrp))
5215e11c3f44Smeem 			continue;
5216e11c3f44Smeem 
5217e11c3f44Smeem 		if (lifrp->lifr_addr.ss_family == AF_INET) {
5218e11c3f44Smeem 			sinp = addrs4->ri_list;
5219e11c3f44Smeem 			bcopy(&lifrp->lifr_addr, &sinp[addrs4->ri_count++],
5220e11c3f44Smeem 			    sizeof (struct sockaddr_in));
5221e11c3f44Smeem 		} else if (lifrp->lifr_addr.ss_family == AF_INET6) {
5222e11c3f44Smeem 			sin6p = addrs6->ri_list;
5223e11c3f44Smeem 			bcopy(&lifrp->lifr_addr, &sin6p[addrs6->ri_count++],
5224e11c3f44Smeem 			    sizeof (struct sockaddr_in6));
5225e11c3f44Smeem 		}
52260a701b1eSRobert Gordon 	}
52270a701b1eSRobert Gordon 
5228e11c3f44Smeem 	kmem_free(lifc.lifc_buf, bufsize);
5229e11c3f44Smeem 	return (B_TRUE);
52300a701b1eSRobert Gordon }
52310a701b1eSRobert Gordon 
52320a701b1eSRobert Gordon /* ARGSUSED */
5233*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States static int
5234*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States rpcib_cache_kstat_update(kstat_t *ksp, int rw)
5235*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States {
5236*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rib_hca_t *hca;
52370a701b1eSRobert Gordon 
52380a701b1eSRobert Gordon 	if (KSTAT_WRITE == rw) {
52390a701b1eSRobert Gordon 		return (EACCES);
52400a701b1eSRobert Gordon 	}
5241*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 
52420a701b1eSRobert Gordon 	rpcib_kstat.cache_limit.value.ui64 =
52430a701b1eSRobert Gordon 	    (uint64_t)cache_limit;
5244*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_enter(&rib_stat->hcas_list_lock, RW_READER);
5245*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	for (hca = rib_stat->hcas_list; hca; hca = hca->next) {
5246*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rpcib_kstat.cache_allocation.value.ui64 +=
5247*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    (uint64_t)hca->cache_allocation;
5248*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rpcib_kstat.cache_hits.value.ui64 +=
5249*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    (uint64_t)hca->cache_hits;
5250*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rpcib_kstat.cache_misses.value.ui64 +=
5251*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    (uint64_t)hca->cache_misses;
5252*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		rpcib_kstat.cache_misses_above_the_limit.value.ui64 +=
5253*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 		    (uint64_t)hca->cache_misses_above_the_limit;
5254*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	}
5255*7f379ad1SFaramarz Jalalian - Sun Microsystems - Irvine United States 	rw_exit(&rib_stat->hcas_list_lock);
52560a701b1eSRobert Gordon 	return (0);
52570a701b1eSRobert Gordon }
5258