xref: /titanic_53/usr/src/uts/sun4v/io/ldc.c (revision 83d3bc6f9969f95595135be43a57b8066f5e3273)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
23*83d3bc6fSnarayan  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
281ae08745Sheppo 
291ae08745Sheppo /*
30e1ebb9ecSlm66018  * sun4v LDC Link Layer
311ae08745Sheppo  */
321ae08745Sheppo #include <sys/types.h>
331ae08745Sheppo #include <sys/file.h>
341ae08745Sheppo #include <sys/errno.h>
351ae08745Sheppo #include <sys/open.h>
361ae08745Sheppo #include <sys/cred.h>
371ae08745Sheppo #include <sys/kmem.h>
381ae08745Sheppo #include <sys/conf.h>
391ae08745Sheppo #include <sys/cmn_err.h>
401ae08745Sheppo #include <sys/ksynch.h>
411ae08745Sheppo #include <sys/modctl.h>
421ae08745Sheppo #include <sys/stat.h> /* needed for S_IFBLK and S_IFCHR */
431ae08745Sheppo #include <sys/debug.h>
441ae08745Sheppo #include <sys/types.h>
451ae08745Sheppo #include <sys/cred.h>
461ae08745Sheppo #include <sys/promif.h>
471ae08745Sheppo #include <sys/ddi.h>
481ae08745Sheppo #include <sys/sunddi.h>
491ae08745Sheppo #include <sys/cyclic.h>
501ae08745Sheppo #include <sys/machsystm.h>
511ae08745Sheppo #include <sys/vm.h>
521ae08745Sheppo #include <sys/cpu.h>
531ae08745Sheppo #include <sys/intreg.h>
541ae08745Sheppo #include <sys/machcpuvar.h>
554bac2208Snarayan #include <sys/mmu.h>
564bac2208Snarayan #include <sys/pte.h>
574bac2208Snarayan #include <vm/hat.h>
584bac2208Snarayan #include <vm/as.h>
594bac2208Snarayan #include <vm/hat_sfmmu.h>
604bac2208Snarayan #include <sys/vm_machparam.h>
614bac2208Snarayan #include <vm/seg_kmem.h>
624bac2208Snarayan #include <vm/seg_kpm.h>
631ae08745Sheppo #include <sys/note.h>
641ae08745Sheppo #include <sys/ivintr.h>
651ae08745Sheppo #include <sys/hypervisor_api.h>
661ae08745Sheppo #include <sys/ldc.h>
671ae08745Sheppo #include <sys/ldc_impl.h>
681ae08745Sheppo #include <sys/cnex.h>
691ae08745Sheppo #include <sys/hsvc.h>
701ae08745Sheppo 
711ae08745Sheppo /* Core internal functions */
721ae08745Sheppo static int i_ldc_h2v_error(int h_error);
731ae08745Sheppo static int i_ldc_txq_reconf(ldc_chan_t *ldcp);
743af08d82Slm66018 static int i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset);
75a8ea4edeSnarayan static int i_ldc_rxq_drain(ldc_chan_t *ldcp);
761ae08745Sheppo static void i_ldc_reset_state(ldc_chan_t *ldcp);
773af08d82Slm66018 static void i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset);
781ae08745Sheppo 
791ae08745Sheppo static int i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail);
801ae08745Sheppo static int i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail);
811ae08745Sheppo static int i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head);
821ae08745Sheppo static int i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype,
831ae08745Sheppo     uint8_t ctrlmsg);
841ae08745Sheppo 
851ae08745Sheppo /* Interrupt handling functions */
861ae08745Sheppo static uint_t i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2);
871ae08745Sheppo static uint_t i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2);
881ae08745Sheppo static void i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype);
891ae08745Sheppo 
901ae08745Sheppo /* Read method functions */
911ae08745Sheppo static int i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep);
921ae08745Sheppo static int i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp,
931ae08745Sheppo 	size_t *sizep);
941ae08745Sheppo static int i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp,
951ae08745Sheppo 	size_t *sizep);
961ae08745Sheppo 
971ae08745Sheppo /* Write method functions */
981ae08745Sheppo static int i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t target_bufp,
991ae08745Sheppo 	size_t *sizep);
1001ae08745Sheppo static int i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t target_bufp,
1011ae08745Sheppo 	size_t *sizep);
1021ae08745Sheppo static int i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t target_bufp,
1031ae08745Sheppo 	size_t *sizep);
1041ae08745Sheppo 
1051ae08745Sheppo /* Pkt processing internal functions */
1061ae08745Sheppo static int i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg);
1071ae08745Sheppo static int i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg);
1081ae08745Sheppo static int i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg);
1091ae08745Sheppo static int i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg);
1101ae08745Sheppo static int i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg);
1111ae08745Sheppo static int i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg);
1121ae08745Sheppo static int i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg);
1131ae08745Sheppo 
1141ae08745Sheppo /* Memory synchronization internal functions */
1151ae08745Sheppo static int i_ldc_mem_acquire_release(ldc_mem_handle_t mhandle,
1161ae08745Sheppo     uint8_t direction, uint64_t offset, size_t size);
1171ae08745Sheppo static int i_ldc_dring_acquire_release(ldc_dring_handle_t dhandle,
1181ae08745Sheppo     uint8_t direction, uint64_t start, uint64_t end);
1191ae08745Sheppo 
1201ae08745Sheppo /* LDC Version */
1211ae08745Sheppo static ldc_ver_t ldc_versions[] = { {1, 0} };
1221ae08745Sheppo 
1231ae08745Sheppo /* number of supported versions */
1241ae08745Sheppo #define	LDC_NUM_VERS	(sizeof (ldc_versions) / sizeof (ldc_versions[0]))
1251ae08745Sheppo 
1261ae08745Sheppo /* Module State Pointer */
1271ae08745Sheppo static ldc_soft_state_t *ldcssp;
1281ae08745Sheppo 
1291ae08745Sheppo static struct modldrv md = {
1301ae08745Sheppo 	&mod_miscops,			/* This is a misc module */
1311ae08745Sheppo 	"sun4v LDC module v%I%",	/* Name of the module */
1321ae08745Sheppo };
1331ae08745Sheppo 
1341ae08745Sheppo static struct modlinkage ml = {
1351ae08745Sheppo 	MODREV_1,
1361ae08745Sheppo 	&md,
1371ae08745Sheppo 	NULL
1381ae08745Sheppo };
1391ae08745Sheppo 
1401ae08745Sheppo static uint64_t ldc_sup_minor;		/* Supported minor number */
1411ae08745Sheppo static hsvc_info_t ldc_hsvc = {
1421ae08745Sheppo 	HSVC_REV_1, NULL, HSVC_GROUP_LDC, 1, 0, "ldc"
1431ae08745Sheppo };
1441ae08745Sheppo 
1451ae08745Sheppo static uint64_t intr_sup_minor;		/* Supported minor number */
1461ae08745Sheppo static hsvc_info_t intr_hsvc = {
1471ae08745Sheppo 	HSVC_REV_1, NULL, HSVC_GROUP_INTR, 1, 0, "ldc"
1481ae08745Sheppo };
1491ae08745Sheppo 
1504bac2208Snarayan /*
1514bac2208Snarayan  * LDC framework supports mapping remote domain's memory
1524bac2208Snarayan  * either directly or via shadow memory pages. Default
1534bac2208Snarayan  * support is currently implemented via shadow copy.
1544bac2208Snarayan  * Direct map can be enabled by setting 'ldc_shmem_enabled'
1554bac2208Snarayan  */
1564bac2208Snarayan int ldc_shmem_enabled = 0;
157e1ebb9ecSlm66018 
1580a55fbb7Slm66018 /*
159e1ebb9ecSlm66018  * The no. of MTU size messages that can be stored in
160e1ebb9ecSlm66018  * the LDC Tx queue. The number of Tx queue entries is
161e1ebb9ecSlm66018  * then computed as (mtu * mtu_msgs)/sizeof(queue_entry)
162e1ebb9ecSlm66018  */
163e1ebb9ecSlm66018 uint64_t ldc_mtu_msgs = LDC_MTU_MSGS;
164e1ebb9ecSlm66018 
165e1ebb9ecSlm66018 /*
166e1ebb9ecSlm66018  * The minimum queue length. This is the size of the smallest
167e1ebb9ecSlm66018  * LDC queue. If the computed value is less than this default,
168e1ebb9ecSlm66018  * the queue length is rounded up to 'ldc_queue_entries'.
169e1ebb9ecSlm66018  */
170e1ebb9ecSlm66018 uint64_t ldc_queue_entries = LDC_QUEUE_ENTRIES;
171e1ebb9ecSlm66018 
172e1ebb9ecSlm66018 /*
173e1ebb9ecSlm66018  * Pages exported for remote access over each channel is
174e1ebb9ecSlm66018  * maintained in a table registered with the Hypervisor.
175e1ebb9ecSlm66018  * The default number of entries in the table is set to
176e1ebb9ecSlm66018  * 'ldc_mtbl_entries'.
177e1ebb9ecSlm66018  */
178e1ebb9ecSlm66018 uint64_t ldc_maptable_entries = LDC_MTBL_ENTRIES;
179e1ebb9ecSlm66018 
180e1ebb9ecSlm66018 /*
181e1ebb9ecSlm66018  * LDC retry count and delay - when the HV returns EWOULDBLOCK
182e1ebb9ecSlm66018  * the operation is retried 'ldc_max_retries' times with a
183e1ebb9ecSlm66018  * wait of 'ldc_delay' usecs between each retry.
1840a55fbb7Slm66018  */
1850a55fbb7Slm66018 int ldc_max_retries = LDC_MAX_RETRIES;
1860a55fbb7Slm66018 clock_t ldc_delay = LDC_DELAY;
1870a55fbb7Slm66018 
1884d39be2bSsg70180 /*
1894d39be2bSsg70180  * delay between each retry of channel unregistration in
1904d39be2bSsg70180  * ldc_close(), to wait for pending interrupts to complete.
1914d39be2bSsg70180  */
1924d39be2bSsg70180 clock_t ldc_close_delay = LDC_CLOSE_DELAY;
1934d39be2bSsg70180 
1941ae08745Sheppo #ifdef DEBUG
1951ae08745Sheppo 
1961ae08745Sheppo /*
1971ae08745Sheppo  * Print debug messages
1981ae08745Sheppo  *
1991ae08745Sheppo  * set ldcdbg to 0x7 for enabling all msgs
2001ae08745Sheppo  * 0x4 - Warnings
2011ae08745Sheppo  * 0x2 - All debug messages
2021ae08745Sheppo  * 0x1 - Minimal debug messages
2031ae08745Sheppo  *
2041ae08745Sheppo  * set ldcdbgchan to the channel number you want to debug
2051ae08745Sheppo  * setting it to -1 prints debug messages for all channels
2061ae08745Sheppo  * NOTE: ldcdbgchan has no effect on error messages
2071ae08745Sheppo  */
2081ae08745Sheppo 
2091ae08745Sheppo #define	DBG_ALL_LDCS -1
2101ae08745Sheppo 
2111ae08745Sheppo int ldcdbg = 0x0;
2121ae08745Sheppo int64_t ldcdbgchan = DBG_ALL_LDCS;
213*83d3bc6fSnarayan uint64_t ldc_inject_err_flag = 0;
2141ae08745Sheppo 
2151ae08745Sheppo static void
2161ae08745Sheppo ldcdebug(int64_t id, const char *fmt, ...)
2171ae08745Sheppo {
2181ae08745Sheppo 	char buf[512];
2191ae08745Sheppo 	va_list ap;
2201ae08745Sheppo 
2211ae08745Sheppo 	/*
2221ae08745Sheppo 	 * Do not return if,
2231ae08745Sheppo 	 * caller wants to print it anyway - (id == DBG_ALL_LDCS)
2241ae08745Sheppo 	 * debug channel is set to all LDCs - (ldcdbgchan == DBG_ALL_LDCS)
2251ae08745Sheppo 	 * debug channel = caller specified channel
2261ae08745Sheppo 	 */
2271ae08745Sheppo 	if ((id != DBG_ALL_LDCS) &&
2281ae08745Sheppo 	    (ldcdbgchan != DBG_ALL_LDCS) &&
2291ae08745Sheppo 	    (ldcdbgchan != id)) {
2301ae08745Sheppo 		return;
2311ae08745Sheppo 	}
2321ae08745Sheppo 
2331ae08745Sheppo 	va_start(ap, fmt);
2341ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
2351ae08745Sheppo 	va_end(ap);
2361ae08745Sheppo 
2373af08d82Slm66018 	cmn_err(CE_CONT, "?%s", buf);
2383af08d82Slm66018 }
2393af08d82Slm66018 
240*83d3bc6fSnarayan #define	LDC_ERR_RESET	0x1
241*83d3bc6fSnarayan #define	LDC_ERR_PKTLOSS	0x2
242*83d3bc6fSnarayan 
2433af08d82Slm66018 static boolean_t
244*83d3bc6fSnarayan ldc_inject_error(ldc_chan_t *ldcp, uint64_t error)
2453af08d82Slm66018 {
2463af08d82Slm66018 	if ((ldcdbgchan != DBG_ALL_LDCS) && (ldcdbgchan != ldcp->id))
2473af08d82Slm66018 		return (B_FALSE);
2483af08d82Slm66018 
249*83d3bc6fSnarayan 	if ((ldc_inject_err_flag & error) == 0)
2503af08d82Slm66018 		return (B_FALSE);
2513af08d82Slm66018 
2523af08d82Slm66018 	/* clear the injection state */
253*83d3bc6fSnarayan 	ldc_inject_err_flag &= ~error;
2543af08d82Slm66018 
2553af08d82Slm66018 	return (B_TRUE);
2561ae08745Sheppo }
2571ae08745Sheppo 
2581ae08745Sheppo #define	D1		\
2591ae08745Sheppo if (ldcdbg & 0x01)	\
2601ae08745Sheppo 	ldcdebug
2611ae08745Sheppo 
2621ae08745Sheppo #define	D2		\
2631ae08745Sheppo if (ldcdbg & 0x02)	\
2641ae08745Sheppo 	ldcdebug
2651ae08745Sheppo 
2661ae08745Sheppo #define	DWARN		\
2671ae08745Sheppo if (ldcdbg & 0x04)	\
2681ae08745Sheppo 	ldcdebug
2691ae08745Sheppo 
2701ae08745Sheppo #define	DUMP_PAYLOAD(id, addr)						\
2711ae08745Sheppo {									\
2721ae08745Sheppo 	char buf[65*3];							\
2731ae08745Sheppo 	int i;								\
2741ae08745Sheppo 	uint8_t *src = (uint8_t *)addr;					\
2751ae08745Sheppo 	for (i = 0; i < 64; i++, src++)					\
2761ae08745Sheppo 		(void) sprintf(&buf[i * 3], "|%02x", *src);		\
2771ae08745Sheppo 	(void) sprintf(&buf[i * 3], "|\n");				\
2781ae08745Sheppo 	D2((id), "payload: %s", buf);					\
2791ae08745Sheppo }
2801ae08745Sheppo 
2811ae08745Sheppo #define	DUMP_LDC_PKT(c, s, addr)					\
2821ae08745Sheppo {									\
2831ae08745Sheppo 	ldc_msg_t *msg = (ldc_msg_t *)(addr);				\
2841ae08745Sheppo 	uint32_t mid = ((c)->mode != LDC_MODE_RAW) ? msg->seqid : 0;	\
2851ae08745Sheppo 	if (msg->type == LDC_DATA) {                                    \
2861ae08745Sheppo 	    D2((c)->id, "%s: msg%d (/%x/%x/%x/,env[%c%c,sz=%d])",	\
2871ae08745Sheppo 	    (s), mid, msg->type, msg->stype, msg->ctrl,			\
2881ae08745Sheppo 	    (msg->env & LDC_FRAG_START) ? 'B' : ' ',                    \
2891ae08745Sheppo 	    (msg->env & LDC_FRAG_STOP) ? 'E' : ' ',                     \
2901ae08745Sheppo 	    (msg->env & LDC_LEN_MASK));					\
2911ae08745Sheppo 	} else { 							\
2921ae08745Sheppo 	    D2((c)->id, "%s: msg%d (/%x/%x/%x/,env=%x)", (s),		\
2931ae08745Sheppo 	    mid, msg->type, msg->stype, msg->ctrl, msg->env);		\
2941ae08745Sheppo 	} 								\
2951ae08745Sheppo }
2961ae08745Sheppo 
297*83d3bc6fSnarayan #define	LDC_INJECT_RESET(_ldcp)	ldc_inject_error(_ldcp, LDC_ERR_RESET)
298*83d3bc6fSnarayan #define	LDC_INJECT_PKTLOSS(_ldcp) ldc_inject_error(_ldcp, LDC_ERR_PKTLOSS)
2993af08d82Slm66018 
3001ae08745Sheppo #else
3011ae08745Sheppo 
3021ae08745Sheppo #define	DBG_ALL_LDCS -1
3031ae08745Sheppo 
3041ae08745Sheppo #define	D1
3051ae08745Sheppo #define	D2
3061ae08745Sheppo #define	DWARN
3071ae08745Sheppo 
3081ae08745Sheppo #define	DUMP_PAYLOAD(id, addr)
3091ae08745Sheppo #define	DUMP_LDC_PKT(c, s, addr)
3101ae08745Sheppo 
3113af08d82Slm66018 #define	LDC_INJECT_RESET(_ldcp)	(B_FALSE)
312*83d3bc6fSnarayan #define	LDC_INJECT_PKTLOSS(_ldcp) (B_FALSE)
3133af08d82Slm66018 
3141ae08745Sheppo #endif
3151ae08745Sheppo 
3161ae08745Sheppo #define	ZERO_PKT(p)			\
3171ae08745Sheppo 	bzero((p), sizeof (ldc_msg_t));
3181ae08745Sheppo 
3191ae08745Sheppo #define	IDX2COOKIE(idx, pg_szc, pg_shift)				\
3201ae08745Sheppo 	(((pg_szc) << LDC_COOKIE_PGSZC_SHIFT) | ((idx) << (pg_shift)))
3211ae08745Sheppo 
3221ae08745Sheppo 
3231ae08745Sheppo int
3241ae08745Sheppo _init(void)
3251ae08745Sheppo {
3261ae08745Sheppo 	int status;
3271ae08745Sheppo 
3281ae08745Sheppo 	status = hsvc_register(&ldc_hsvc, &ldc_sup_minor);
3291ae08745Sheppo 	if (status != 0) {
3301ae08745Sheppo 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor LDC services"
3311ae08745Sheppo 		    " group: 0x%lx major: %ld minor: %ld errno: %d",
3321ae08745Sheppo 		    ldc_hsvc.hsvc_modname, ldc_hsvc.hsvc_group,
3331ae08745Sheppo 		    ldc_hsvc.hsvc_major, ldc_hsvc.hsvc_minor, status);
3341ae08745Sheppo 		return (-1);
3351ae08745Sheppo 	}
3361ae08745Sheppo 
3371ae08745Sheppo 	status = hsvc_register(&intr_hsvc, &intr_sup_minor);
3381ae08745Sheppo 	if (status != 0) {
3391ae08745Sheppo 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor interrupt "
3401ae08745Sheppo 		    "services group: 0x%lx major: %ld minor: %ld errno: %d",
3411ae08745Sheppo 		    intr_hsvc.hsvc_modname, intr_hsvc.hsvc_group,
3421ae08745Sheppo 		    intr_hsvc.hsvc_major, intr_hsvc.hsvc_minor, status);
3431ae08745Sheppo 		(void) hsvc_unregister(&ldc_hsvc);
3441ae08745Sheppo 		return (-1);
3451ae08745Sheppo 	}
3461ae08745Sheppo 
3471ae08745Sheppo 	/* allocate soft state structure */
3481ae08745Sheppo 	ldcssp = kmem_zalloc(sizeof (ldc_soft_state_t), KM_SLEEP);
3491ae08745Sheppo 
3501ae08745Sheppo 	/* Link the module into the system */
3511ae08745Sheppo 	status = mod_install(&ml);
3521ae08745Sheppo 	if (status != 0) {
3531ae08745Sheppo 		kmem_free(ldcssp, sizeof (ldc_soft_state_t));
3541ae08745Sheppo 		return (status);
3551ae08745Sheppo 	}
3561ae08745Sheppo 
3571ae08745Sheppo 	/* Initialize the LDC state structure */
3581ae08745Sheppo 	mutex_init(&ldcssp->lock, NULL, MUTEX_DRIVER, NULL);
3591ae08745Sheppo 
3601ae08745Sheppo 	mutex_enter(&ldcssp->lock);
3611ae08745Sheppo 
3624bac2208Snarayan 	/* Create a cache for memory handles */
3634bac2208Snarayan 	ldcssp->memhdl_cache = kmem_cache_create("ldc_memhdl_cache",
3644bac2208Snarayan 	    sizeof (ldc_mhdl_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3654bac2208Snarayan 	if (ldcssp->memhdl_cache == NULL) {
3664bac2208Snarayan 		DWARN(DBG_ALL_LDCS, "_init: ldc_memhdl cache create failed\n");
3674bac2208Snarayan 		mutex_exit(&ldcssp->lock);
3684bac2208Snarayan 		return (-1);
3694bac2208Snarayan 	}
3704bac2208Snarayan 
3714bac2208Snarayan 	/* Create cache for memory segment structures */
3724bac2208Snarayan 	ldcssp->memseg_cache = kmem_cache_create("ldc_memseg_cache",
3734bac2208Snarayan 	    sizeof (ldc_memseg_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3744bac2208Snarayan 	if (ldcssp->memseg_cache == NULL) {
3754bac2208Snarayan 		DWARN(DBG_ALL_LDCS, "_init: ldc_memseg cache create failed\n");
3764bac2208Snarayan 		mutex_exit(&ldcssp->lock);
3774bac2208Snarayan 		return (-1);
3784bac2208Snarayan 	}
3794bac2208Snarayan 
3804bac2208Snarayan 
3811ae08745Sheppo 	ldcssp->channel_count = 0;
3821ae08745Sheppo 	ldcssp->channels_open = 0;
3831ae08745Sheppo 	ldcssp->chan_list = NULL;
3841ae08745Sheppo 	ldcssp->dring_list = NULL;
3851ae08745Sheppo 
3861ae08745Sheppo 	mutex_exit(&ldcssp->lock);
3871ae08745Sheppo 
3881ae08745Sheppo 	return (0);
3891ae08745Sheppo }
3901ae08745Sheppo 
3911ae08745Sheppo int
3921ae08745Sheppo _info(struct modinfo *modinfop)
3931ae08745Sheppo {
3941ae08745Sheppo 	/* Report status of the dynamically loadable driver module */
3951ae08745Sheppo 	return (mod_info(&ml, modinfop));
3961ae08745Sheppo }
3971ae08745Sheppo 
3981ae08745Sheppo int
3991ae08745Sheppo _fini(void)
4001ae08745Sheppo {
4011ae08745Sheppo 	int 		rv, status;
4021ae08745Sheppo 	ldc_chan_t 	*ldcp;
4031ae08745Sheppo 	ldc_dring_t 	*dringp;
4041ae08745Sheppo 	ldc_mem_info_t 	minfo;
4051ae08745Sheppo 
4061ae08745Sheppo 	/* Unlink the driver module from the system */
4071ae08745Sheppo 	status = mod_remove(&ml);
4081ae08745Sheppo 	if (status) {
4091ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "_fini: mod_remove failed\n");
4101ae08745Sheppo 		return (EIO);
4111ae08745Sheppo 	}
4121ae08745Sheppo 
4131ae08745Sheppo 	/* close and finalize channels */
4141ae08745Sheppo 	ldcp = ldcssp->chan_list;
4151ae08745Sheppo 	while (ldcp != NULL) {
4161ae08745Sheppo 		(void) ldc_close((ldc_handle_t)ldcp);
4171ae08745Sheppo 		(void) ldc_fini((ldc_handle_t)ldcp);
4181ae08745Sheppo 
4191ae08745Sheppo 		ldcp = ldcp->next;
4201ae08745Sheppo 	}
4211ae08745Sheppo 
4221ae08745Sheppo 	/* Free descriptor rings */
4231ae08745Sheppo 	dringp = ldcssp->dring_list;
4241ae08745Sheppo 	while (dringp != NULL) {
4251ae08745Sheppo 		dringp = dringp->next;
4261ae08745Sheppo 
4271ae08745Sheppo 		rv = ldc_mem_dring_info((ldc_dring_handle_t)dringp, &minfo);
4281ae08745Sheppo 		if (rv == 0 && minfo.status != LDC_UNBOUND) {
4291ae08745Sheppo 			if (minfo.status == LDC_BOUND) {
4301ae08745Sheppo 				(void) ldc_mem_dring_unbind(
4311ae08745Sheppo 						(ldc_dring_handle_t)dringp);
4321ae08745Sheppo 			}
4331ae08745Sheppo 			if (minfo.status == LDC_MAPPED) {
4341ae08745Sheppo 				(void) ldc_mem_dring_unmap(
4351ae08745Sheppo 						(ldc_dring_handle_t)dringp);
4361ae08745Sheppo 			}
4371ae08745Sheppo 		}
4381ae08745Sheppo 
4391ae08745Sheppo 		(void) ldc_mem_dring_destroy((ldc_dring_handle_t)dringp);
4401ae08745Sheppo 	}
4411ae08745Sheppo 	ldcssp->dring_list = NULL;
4421ae08745Sheppo 
4434bac2208Snarayan 	/* Destroy kmem caches */
4444bac2208Snarayan 	kmem_cache_destroy(ldcssp->memhdl_cache);
4454bac2208Snarayan 	kmem_cache_destroy(ldcssp->memseg_cache);
4464bac2208Snarayan 
4471ae08745Sheppo 	/*
4481ae08745Sheppo 	 * We have successfully "removed" the driver.
4491ae08745Sheppo 	 * Destroying soft states
4501ae08745Sheppo 	 */
4511ae08745Sheppo 	mutex_destroy(&ldcssp->lock);
4521ae08745Sheppo 	kmem_free(ldcssp, sizeof (ldc_soft_state_t));
4531ae08745Sheppo 
4541ae08745Sheppo 	(void) hsvc_unregister(&ldc_hsvc);
4551ae08745Sheppo 	(void) hsvc_unregister(&intr_hsvc);
4561ae08745Sheppo 
4571ae08745Sheppo 	return (status);
4581ae08745Sheppo }
4591ae08745Sheppo 
4601ae08745Sheppo /* -------------------------------------------------------------------------- */
4611ae08745Sheppo 
4621ae08745Sheppo /*
463e1ebb9ecSlm66018  * LDC Link Layer Internal Functions
4641ae08745Sheppo  */
4651ae08745Sheppo 
4661ae08745Sheppo /*
4671ae08745Sheppo  * Translate HV Errors to sun4v error codes
4681ae08745Sheppo  */
4691ae08745Sheppo static int
4701ae08745Sheppo i_ldc_h2v_error(int h_error)
4711ae08745Sheppo {
4721ae08745Sheppo 	switch (h_error) {
4731ae08745Sheppo 
4741ae08745Sheppo 	case	H_EOK:
4751ae08745Sheppo 		return (0);
4761ae08745Sheppo 
4771ae08745Sheppo 	case	H_ENORADDR:
4781ae08745Sheppo 		return (EFAULT);
4791ae08745Sheppo 
4801ae08745Sheppo 	case	H_EBADPGSZ:
4811ae08745Sheppo 	case	H_EINVAL:
4821ae08745Sheppo 		return (EINVAL);
4831ae08745Sheppo 
4841ae08745Sheppo 	case	H_EWOULDBLOCK:
4851ae08745Sheppo 		return (EWOULDBLOCK);
4861ae08745Sheppo 
4871ae08745Sheppo 	case	H_ENOACCESS:
4881ae08745Sheppo 	case	H_ENOMAP:
4891ae08745Sheppo 		return (EACCES);
4901ae08745Sheppo 
4911ae08745Sheppo 	case	H_EIO:
4921ae08745Sheppo 	case	H_ECPUERROR:
4931ae08745Sheppo 		return (EIO);
4941ae08745Sheppo 
4951ae08745Sheppo 	case	H_ENOTSUPPORTED:
4961ae08745Sheppo 		return (ENOTSUP);
4971ae08745Sheppo 
4981ae08745Sheppo 	case 	H_ETOOMANY:
4991ae08745Sheppo 		return (ENOSPC);
5001ae08745Sheppo 
5011ae08745Sheppo 	case	H_ECHANNEL:
5021ae08745Sheppo 		return (ECHRNG);
5031ae08745Sheppo 	default:
5041ae08745Sheppo 		break;
5051ae08745Sheppo 	}
5061ae08745Sheppo 
5071ae08745Sheppo 	return (EIO);
5081ae08745Sheppo }
5091ae08745Sheppo 
5101ae08745Sheppo /*
5111ae08745Sheppo  * Reconfigure the transmit queue
5121ae08745Sheppo  */
5131ae08745Sheppo static int
5141ae08745Sheppo i_ldc_txq_reconf(ldc_chan_t *ldcp)
5151ae08745Sheppo {
5161ae08745Sheppo 	int rv;
5171ae08745Sheppo 
5181ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
519d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
520d10e4ef2Snarayan 
5211ae08745Sheppo 	rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries);
5221ae08745Sheppo 	if (rv) {
5231ae08745Sheppo 		cmn_err(CE_WARN,
5243af08d82Slm66018 		    "i_ldc_txq_reconf: (0x%lx) cannot set qconf", ldcp->id);
5251ae08745Sheppo 		return (EIO);
5261ae08745Sheppo 	}
5271ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id, &(ldcp->tx_head),
5281ae08745Sheppo 	    &(ldcp->tx_tail), &(ldcp->link_state));
5291ae08745Sheppo 	if (rv) {
5301ae08745Sheppo 		cmn_err(CE_WARN,
5313af08d82Slm66018 		    "i_ldc_txq_reconf: (0x%lx) cannot get qptrs", ldcp->id);
5321ae08745Sheppo 		return (EIO);
5331ae08745Sheppo 	}
5343af08d82Slm66018 	D1(ldcp->id, "i_ldc_txq_reconf: (0x%llx) h=0x%llx,t=0x%llx,"
5351ae08745Sheppo 	    "s=0x%llx\n", ldcp->id, ldcp->tx_head, ldcp->tx_tail,
5361ae08745Sheppo 	    ldcp->link_state);
5371ae08745Sheppo 
5381ae08745Sheppo 	return (0);
5391ae08745Sheppo }
5401ae08745Sheppo 
5411ae08745Sheppo /*
5421ae08745Sheppo  * Reconfigure the receive queue
5431ae08745Sheppo  */
5441ae08745Sheppo static int
5453af08d82Slm66018 i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset)
5461ae08745Sheppo {
5471ae08745Sheppo 	int rv;
5481ae08745Sheppo 	uint64_t rx_head, rx_tail;
5491ae08745Sheppo 
5501ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
5511ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
5521ae08745Sheppo 	    &(ldcp->link_state));
5531ae08745Sheppo 	if (rv) {
5541ae08745Sheppo 		cmn_err(CE_WARN,
5553af08d82Slm66018 		    "i_ldc_rxq_reconf: (0x%lx) cannot get state",
5561ae08745Sheppo 		    ldcp->id);
5571ae08745Sheppo 		return (EIO);
5581ae08745Sheppo 	}
5591ae08745Sheppo 
5603af08d82Slm66018 	if (force_reset || (ldcp->tstate & ~TS_IN_RESET) == TS_UP) {
5611ae08745Sheppo 		rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra,
5621ae08745Sheppo 			ldcp->rx_q_entries);
5631ae08745Sheppo 		if (rv) {
5641ae08745Sheppo 			cmn_err(CE_WARN,
5653af08d82Slm66018 			    "i_ldc_rxq_reconf: (0x%lx) cannot set qconf",
5661ae08745Sheppo 			    ldcp->id);
5671ae08745Sheppo 			return (EIO);
5681ae08745Sheppo 		}
5693af08d82Slm66018 		D1(ldcp->id, "i_ldc_rxq_reconf: (0x%llx) completed q reconf",
5701ae08745Sheppo 		    ldcp->id);
5711ae08745Sheppo 	}
5721ae08745Sheppo 
5731ae08745Sheppo 	return (0);
5741ae08745Sheppo }
5751ae08745Sheppo 
576a8ea4edeSnarayan 
577a8ea4edeSnarayan /*
578a8ea4edeSnarayan  * Drain the contents of the receive queue
579a8ea4edeSnarayan  */
580a8ea4edeSnarayan static int
581a8ea4edeSnarayan i_ldc_rxq_drain(ldc_chan_t *ldcp)
582a8ea4edeSnarayan {
583a8ea4edeSnarayan 	int rv;
584a8ea4edeSnarayan 	uint64_t rx_head, rx_tail;
585a8ea4edeSnarayan 
586a8ea4edeSnarayan 	ASSERT(MUTEX_HELD(&ldcp->lock));
587a8ea4edeSnarayan 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
588a8ea4edeSnarayan 	    &(ldcp->link_state));
589a8ea4edeSnarayan 	if (rv) {
590a8ea4edeSnarayan 		cmn_err(CE_WARN, "i_ldc_rxq_drain: (0x%lx) cannot get state",
591a8ea4edeSnarayan 		    ldcp->id);
592a8ea4edeSnarayan 		return (EIO);
593a8ea4edeSnarayan 	}
594a8ea4edeSnarayan 
595a8ea4edeSnarayan 	/* flush contents by setting the head = tail */
596a8ea4edeSnarayan 	return (i_ldc_set_rx_head(ldcp, rx_tail));
597a8ea4edeSnarayan }
598a8ea4edeSnarayan 
599a8ea4edeSnarayan 
6001ae08745Sheppo /*
6011ae08745Sheppo  * Reset LDC state structure and its contents
6021ae08745Sheppo  */
6031ae08745Sheppo static void
6041ae08745Sheppo i_ldc_reset_state(ldc_chan_t *ldcp)
6051ae08745Sheppo {
6061ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
6071ae08745Sheppo 	ldcp->last_msg_snt = LDC_INIT_SEQID;
6081ae08745Sheppo 	ldcp->last_ack_rcd = 0;
6091ae08745Sheppo 	ldcp->last_msg_rcd = 0;
6101ae08745Sheppo 	ldcp->tx_ackd_head = ldcp->tx_head;
6111ae08745Sheppo 	ldcp->next_vidx = 0;
6121ae08745Sheppo 	ldcp->hstate = 0;
6131ae08745Sheppo 	ldcp->tstate = TS_OPEN;
6141ae08745Sheppo 	ldcp->status = LDC_OPEN;
6151ae08745Sheppo 
6161ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_UP ||
6171ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
6181ae08745Sheppo 
6191ae08745Sheppo 		if (ldcp->mode == LDC_MODE_RAW) {
6201ae08745Sheppo 			ldcp->status = LDC_UP;
6211ae08745Sheppo 			ldcp->tstate = TS_UP;
6221ae08745Sheppo 		} else {
6231ae08745Sheppo 			ldcp->status = LDC_READY;
6241ae08745Sheppo 			ldcp->tstate |= TS_LINK_READY;
6251ae08745Sheppo 		}
6261ae08745Sheppo 	}
6271ae08745Sheppo }
6281ae08745Sheppo 
6291ae08745Sheppo /*
6301ae08745Sheppo  * Reset a LDC channel
6311ae08745Sheppo  */
6321ae08745Sheppo static void
6333af08d82Slm66018 i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset)
6341ae08745Sheppo {
635*83d3bc6fSnarayan 	DWARN(ldcp->id, "i_ldc_reset: (0x%llx) channel reset\n", ldcp->id);
6361ae08745Sheppo 
637d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->lock));
638d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
639d10e4ef2Snarayan 
6403af08d82Slm66018 	/* reconfig Tx and Rx queues */
6411ae08745Sheppo 	(void) i_ldc_txq_reconf(ldcp);
6423af08d82Slm66018 	(void) i_ldc_rxq_reconf(ldcp, force_reset);
6433af08d82Slm66018 
6443af08d82Slm66018 	/* Clear Tx and Rx interrupts */
6453af08d82Slm66018 	(void) i_ldc_clear_intr(ldcp, CNEX_TX_INTR);
6463af08d82Slm66018 	(void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
6473af08d82Slm66018 
6483af08d82Slm66018 	/* Reset channel state */
6491ae08745Sheppo 	i_ldc_reset_state(ldcp);
6503af08d82Slm66018 
6513af08d82Slm66018 	/* Mark channel in reset */
6523af08d82Slm66018 	ldcp->tstate |= TS_IN_RESET;
6531ae08745Sheppo }
6541ae08745Sheppo 
6554bac2208Snarayan 
6561ae08745Sheppo /*
6571ae08745Sheppo  * Clear pending interrupts
6581ae08745Sheppo  */
6591ae08745Sheppo static void
6601ae08745Sheppo i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype)
6611ae08745Sheppo {
6621ae08745Sheppo 	ldc_cnex_t *cinfo = &ldcssp->cinfo;
6631ae08745Sheppo 
6641ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
6653af08d82Slm66018 	ASSERT(cinfo->dip != NULL);
6664bac2208Snarayan 
6673af08d82Slm66018 	switch (itype) {
6683af08d82Slm66018 	case CNEX_TX_INTR:
6694bac2208Snarayan 		/* check Tx interrupt */
6703af08d82Slm66018 		if (ldcp->tx_intr_state)
6713af08d82Slm66018 			ldcp->tx_intr_state = LDC_INTR_NONE;
6724bac2208Snarayan 		else
6734bac2208Snarayan 			return;
6743af08d82Slm66018 		break;
6753af08d82Slm66018 
6763af08d82Slm66018 	case CNEX_RX_INTR:
6774bac2208Snarayan 		/* check Rx interrupt */
6783af08d82Slm66018 		if (ldcp->rx_intr_state)
6793af08d82Slm66018 			ldcp->rx_intr_state = LDC_INTR_NONE;
6804bac2208Snarayan 		else
6814bac2208Snarayan 			return;
6823af08d82Slm66018 		break;
6834bac2208Snarayan 	}
6844bac2208Snarayan 
6851ae08745Sheppo 	(void) cinfo->clr_intr(cinfo->dip, ldcp->id, itype);
6864bac2208Snarayan 	D2(ldcp->id,
6874bac2208Snarayan 	    "i_ldc_clear_intr: (0x%llx) cleared 0x%x intr\n",
6884bac2208Snarayan 	    ldcp->id, itype);
6891ae08745Sheppo }
6901ae08745Sheppo 
6911ae08745Sheppo /*
6921ae08745Sheppo  * Set the receive queue head
6930a55fbb7Slm66018  * Resets connection and returns an error if it fails.
6941ae08745Sheppo  */
6951ae08745Sheppo static int
6961ae08745Sheppo i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head)
6971ae08745Sheppo {
6981ae08745Sheppo 	int 	rv;
6990a55fbb7Slm66018 	int 	retries;
7001ae08745Sheppo 
7011ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
7020a55fbb7Slm66018 	for (retries = 0; retries < ldc_max_retries; retries++) {
7030a55fbb7Slm66018 
7040a55fbb7Slm66018 		if ((rv = hv_ldc_rx_set_qhead(ldcp->id, head)) == 0)
7050a55fbb7Slm66018 			return (0);
7060a55fbb7Slm66018 
7070a55fbb7Slm66018 		if (rv != H_EWOULDBLOCK)
7080a55fbb7Slm66018 			break;
7090a55fbb7Slm66018 
7100a55fbb7Slm66018 		/* wait for ldc_delay usecs */
7110a55fbb7Slm66018 		drv_usecwait(ldc_delay);
7121ae08745Sheppo 	}
7131ae08745Sheppo 
7140a55fbb7Slm66018 	cmn_err(CE_WARN, "ldc_rx_set_qhead: (0x%lx) cannot set qhead 0x%lx",
7150a55fbb7Slm66018 		ldcp->id, head);
716d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
7173af08d82Slm66018 	i_ldc_reset(ldcp, B_TRUE);
718d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
7190a55fbb7Slm66018 
7200a55fbb7Slm66018 	return (ECONNRESET);
7211ae08745Sheppo }
7221ae08745Sheppo 
7231ae08745Sheppo 
7241ae08745Sheppo /*
7251ae08745Sheppo  * Returns the tx_tail to be used for transfer
7261ae08745Sheppo  * Re-reads the TX queue ptrs if and only if the
7271ae08745Sheppo  * the cached head and tail are equal (queue is full)
7281ae08745Sheppo  */
7291ae08745Sheppo static int
7301ae08745Sheppo i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail)
7311ae08745Sheppo {
7321ae08745Sheppo 	int 		rv;
7331ae08745Sheppo 	uint64_t 	current_head, new_tail;
7341ae08745Sheppo 
735d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
7361ae08745Sheppo 	/* Read the head and tail ptrs from HV */
7371ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
7381ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
7391ae08745Sheppo 	if (rv) {
7401ae08745Sheppo 		cmn_err(CE_WARN,
7411ae08745Sheppo 		    "i_ldc_get_tx_tail: (0x%lx) cannot read qptrs\n",
7421ae08745Sheppo 		    ldcp->id);
7431ae08745Sheppo 		return (EIO);
7441ae08745Sheppo 	}
7451ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_DOWN) {
746cb112a14Slm66018 		D1(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) channel not ready\n",
7471ae08745Sheppo 		    ldcp->id);
7481ae08745Sheppo 		return (ECONNRESET);
7491ae08745Sheppo 	}
7501ae08745Sheppo 
7511ae08745Sheppo 	/* In reliable mode, check against last ACKd msg */
7521ae08745Sheppo 	current_head = (ldcp->mode == LDC_MODE_RELIABLE ||
7531ae08745Sheppo 		ldcp->mode == LDC_MODE_STREAM)
7541ae08745Sheppo 		? ldcp->tx_ackd_head : ldcp->tx_head;
7551ae08745Sheppo 
7561ae08745Sheppo 	/* increment the tail */
7571ae08745Sheppo 	new_tail = (ldcp->tx_tail + LDC_PACKET_SIZE) %
7581ae08745Sheppo 		(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
7591ae08745Sheppo 
7601ae08745Sheppo 	if (new_tail == current_head) {
7611ae08745Sheppo 		DWARN(ldcp->id,
7621ae08745Sheppo 		    "i_ldc_get_tx_tail: (0x%llx) TX queue is full\n",
7631ae08745Sheppo 		    ldcp->id);
7641ae08745Sheppo 		return (EWOULDBLOCK);
7651ae08745Sheppo 	}
7661ae08745Sheppo 
7671ae08745Sheppo 	D2(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) head=0x%llx, tail=0x%llx\n",
7681ae08745Sheppo 	    ldcp->id, ldcp->tx_head, ldcp->tx_tail);
7691ae08745Sheppo 
7701ae08745Sheppo 	*tail = ldcp->tx_tail;
7711ae08745Sheppo 	return (0);
7721ae08745Sheppo }
7731ae08745Sheppo 
7741ae08745Sheppo /*
7751ae08745Sheppo  * Set the tail pointer. If HV returns EWOULDBLOCK, it will back off
7760a55fbb7Slm66018  * and retry ldc_max_retries times before returning an error.
7771ae08745Sheppo  * Returns 0, EWOULDBLOCK or EIO
7781ae08745Sheppo  */
7791ae08745Sheppo static int
7801ae08745Sheppo i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail)
7811ae08745Sheppo {
7821ae08745Sheppo 	int		rv, retval = EWOULDBLOCK;
7830a55fbb7Slm66018 	int 		retries;
7841ae08745Sheppo 
785d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
7860a55fbb7Slm66018 	for (retries = 0; retries < ldc_max_retries; retries++) {
7871ae08745Sheppo 
7881ae08745Sheppo 		if ((rv = hv_ldc_tx_set_qtail(ldcp->id, tail)) == 0) {
7891ae08745Sheppo 			retval = 0;
7901ae08745Sheppo 			break;
7911ae08745Sheppo 		}
7921ae08745Sheppo 		if (rv != H_EWOULDBLOCK) {
7931ae08745Sheppo 			DWARN(ldcp->id, "i_ldc_set_tx_tail: (0x%llx) set "
7941ae08745Sheppo 			    "qtail=0x%llx failed, rv=%d\n", ldcp->id, tail, rv);
7951ae08745Sheppo 			retval = EIO;
7961ae08745Sheppo 			break;
7971ae08745Sheppo 		}
7981ae08745Sheppo 
7990a55fbb7Slm66018 		/* wait for ldc_delay usecs */
8000a55fbb7Slm66018 		drv_usecwait(ldc_delay);
8011ae08745Sheppo 	}
8021ae08745Sheppo 	return (retval);
8031ae08745Sheppo }
8041ae08745Sheppo 
8051ae08745Sheppo /*
8061ae08745Sheppo  * Send a LDC message
8071ae08745Sheppo  */
8081ae08745Sheppo static int
8091ae08745Sheppo i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype,
8101ae08745Sheppo     uint8_t ctrlmsg)
8111ae08745Sheppo {
8121ae08745Sheppo 	int		rv;
8131ae08745Sheppo 	ldc_msg_t 	*pkt;
8141ae08745Sheppo 	uint64_t	tx_tail;
8151ae08745Sheppo 	uint32_t	curr_seqid = ldcp->last_msg_snt;
8161ae08745Sheppo 
817d10e4ef2Snarayan 	/* Obtain Tx lock */
818d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
819d10e4ef2Snarayan 
8201ae08745Sheppo 	/* get the current tail for the message */
8211ae08745Sheppo 	rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
8221ae08745Sheppo 	if (rv) {
8231ae08745Sheppo 		DWARN(ldcp->id,
8241ae08745Sheppo 		    "i_ldc_send_pkt: (0x%llx) error sending pkt, "
8251ae08745Sheppo 		    "type=0x%x,subtype=0x%x,ctrl=0x%x\n",
8261ae08745Sheppo 		    ldcp->id, pkttype, subtype, ctrlmsg);
827d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
8281ae08745Sheppo 		return (rv);
8291ae08745Sheppo 	}
8301ae08745Sheppo 
8311ae08745Sheppo 	pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
8321ae08745Sheppo 	ZERO_PKT(pkt);
8331ae08745Sheppo 
8341ae08745Sheppo 	/* Initialize the packet */
8351ae08745Sheppo 	pkt->type = pkttype;
8361ae08745Sheppo 	pkt->stype = subtype;
8371ae08745Sheppo 	pkt->ctrl = ctrlmsg;
8381ae08745Sheppo 
8391ae08745Sheppo 	/* Store ackid/seqid iff it is RELIABLE mode & not a RTS/RTR message */
8401ae08745Sheppo 	if (((ctrlmsg & LDC_CTRL_MASK) != LDC_RTS) &&
8411ae08745Sheppo 	    ((ctrlmsg & LDC_CTRL_MASK) != LDC_RTR)) {
8421ae08745Sheppo 		curr_seqid++;
8431ae08745Sheppo 		if (ldcp->mode != LDC_MODE_RAW) {
8441ae08745Sheppo 			pkt->seqid = curr_seqid;
8451ae08745Sheppo 			pkt->ackid = ldcp->last_msg_rcd;
8461ae08745Sheppo 		}
8471ae08745Sheppo 	}
8481ae08745Sheppo 	DUMP_LDC_PKT(ldcp, "i_ldc_send_pkt", (uint64_t)pkt);
8491ae08745Sheppo 
8501ae08745Sheppo 	/* initiate the send by calling into HV and set the new tail */
8511ae08745Sheppo 	tx_tail = (tx_tail + LDC_PACKET_SIZE) %
8521ae08745Sheppo 		(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
8531ae08745Sheppo 
8541ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
8551ae08745Sheppo 	if (rv) {
8561ae08745Sheppo 		DWARN(ldcp->id,
8571ae08745Sheppo 		    "i_ldc_send_pkt:(0x%llx) error sending pkt, "
8581ae08745Sheppo 		    "type=0x%x,stype=0x%x,ctrl=0x%x\n",
8591ae08745Sheppo 		    ldcp->id, pkttype, subtype, ctrlmsg);
860d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
8611ae08745Sheppo 		return (EIO);
8621ae08745Sheppo 	}
8631ae08745Sheppo 
8641ae08745Sheppo 	ldcp->last_msg_snt = curr_seqid;
8651ae08745Sheppo 	ldcp->tx_tail = tx_tail;
8661ae08745Sheppo 
867d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
8681ae08745Sheppo 	return (0);
8691ae08745Sheppo }
8701ae08745Sheppo 
8711ae08745Sheppo /*
8721ae08745Sheppo  * Checks if packet was received in right order
873e1ebb9ecSlm66018  * in the case of a reliable link.
8741ae08745Sheppo  * Returns 0 if in order, else EIO
8751ae08745Sheppo  */
8761ae08745Sheppo static int
8771ae08745Sheppo i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *msg)
8781ae08745Sheppo {
8791ae08745Sheppo 	/* No seqid checking for RAW mode */
8801ae08745Sheppo 	if (ldcp->mode == LDC_MODE_RAW)
8811ae08745Sheppo 		return (0);
8821ae08745Sheppo 
8831ae08745Sheppo 	/* No seqid checking for version, RTS, RTR message */
8841ae08745Sheppo 	if (msg->ctrl == LDC_VER ||
8851ae08745Sheppo 	    msg->ctrl == LDC_RTS ||
8861ae08745Sheppo 	    msg->ctrl == LDC_RTR)
8871ae08745Sheppo 		return (0);
8881ae08745Sheppo 
8891ae08745Sheppo 	/* Initial seqid to use is sent in RTS/RTR and saved in last_msg_rcd */
8901ae08745Sheppo 	if (msg->seqid != (ldcp->last_msg_rcd + 1)) {
8911ae08745Sheppo 		DWARN(ldcp->id,
8921ae08745Sheppo 		    "i_ldc_check_seqid: (0x%llx) out-of-order pkt, got 0x%x, "
8931ae08745Sheppo 		    "expecting 0x%x\n", ldcp->id, msg->seqid,
8941ae08745Sheppo 		    (ldcp->last_msg_rcd + 1));
8951ae08745Sheppo 		return (EIO);
8961ae08745Sheppo 	}
8971ae08745Sheppo 
898*83d3bc6fSnarayan #ifdef DEBUG
899*83d3bc6fSnarayan 	if (LDC_INJECT_PKTLOSS(ldcp)) {
900*83d3bc6fSnarayan 		DWARN(ldcp->id,
901*83d3bc6fSnarayan 		    "i_ldc_check_seqid: (0x%llx) inject pkt loss\n", ldcp->id);
902*83d3bc6fSnarayan 		return (EIO);
903*83d3bc6fSnarayan 	}
904*83d3bc6fSnarayan #endif
905*83d3bc6fSnarayan 
9061ae08745Sheppo 	return (0);
9071ae08745Sheppo }
9081ae08745Sheppo 
9091ae08745Sheppo 
9101ae08745Sheppo /*
9111ae08745Sheppo  * Process an incoming version ctrl message
9121ae08745Sheppo  */
9131ae08745Sheppo static int
9141ae08745Sheppo i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg)
9151ae08745Sheppo {
9161ae08745Sheppo 	int 		rv = 0, idx = ldcp->next_vidx;
9171ae08745Sheppo 	ldc_msg_t 	*pkt;
9181ae08745Sheppo 	uint64_t	tx_tail;
9191ae08745Sheppo 	ldc_ver_t	*rcvd_ver;
9201ae08745Sheppo 
9211ae08745Sheppo 	/* get the received version */
9221ae08745Sheppo 	rcvd_ver = (ldc_ver_t *)((uint64_t)msg + LDC_PAYLOAD_VER_OFF);
9231ae08745Sheppo 
9241ae08745Sheppo 	D2(ldcp->id, "i_ldc_process_VER: (0x%llx) received VER v%u.%u\n",
9251ae08745Sheppo 	    ldcp->id, rcvd_ver->major, rcvd_ver->minor);
9261ae08745Sheppo 
927d10e4ef2Snarayan 	/* Obtain Tx lock */
928d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
929d10e4ef2Snarayan 
9301ae08745Sheppo 	switch (msg->stype) {
9311ae08745Sheppo 	case LDC_INFO:
9321ae08745Sheppo 
9333af08d82Slm66018 		if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) {
9343af08d82Slm66018 			(void) i_ldc_txq_reconf(ldcp);
9353af08d82Slm66018 			i_ldc_reset_state(ldcp);
9363af08d82Slm66018 			mutex_exit(&ldcp->tx_lock);
9373af08d82Slm66018 			return (EAGAIN);
9383af08d82Slm66018 		}
9393af08d82Slm66018 
9401ae08745Sheppo 		/* get the current tail and pkt for the response */
9411ae08745Sheppo 		rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
9421ae08745Sheppo 		if (rv != 0) {
9431ae08745Sheppo 			DWARN(ldcp->id,
9441ae08745Sheppo 			    "i_ldc_process_VER: (0x%llx) err sending "
9451ae08745Sheppo 			    "version ACK/NACK\n", ldcp->id);
9463af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
947d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
9481ae08745Sheppo 			return (ECONNRESET);
9491ae08745Sheppo 		}
9501ae08745Sheppo 
9511ae08745Sheppo 		pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
9521ae08745Sheppo 		ZERO_PKT(pkt);
9531ae08745Sheppo 
9541ae08745Sheppo 		/* initialize the packet */
9551ae08745Sheppo 		pkt->type = LDC_CTRL;
9561ae08745Sheppo 		pkt->ctrl = LDC_VER;
9571ae08745Sheppo 
9581ae08745Sheppo 		for (;;) {
9591ae08745Sheppo 
9601ae08745Sheppo 			D1(ldcp->id, "i_ldc_process_VER: got %u.%u chk %u.%u\n",
9611ae08745Sheppo 			    rcvd_ver->major, rcvd_ver->minor,
9621ae08745Sheppo 			    ldc_versions[idx].major, ldc_versions[idx].minor);
9631ae08745Sheppo 
9641ae08745Sheppo 			if (rcvd_ver->major == ldc_versions[idx].major) {
9651ae08745Sheppo 				/* major version match - ACK version */
9661ae08745Sheppo 				pkt->stype = LDC_ACK;
9671ae08745Sheppo 
9681ae08745Sheppo 				/*
9691ae08745Sheppo 				 * lower minor version to the one this endpt
9701ae08745Sheppo 				 * supports, if necessary
9711ae08745Sheppo 				 */
9721ae08745Sheppo 				if (rcvd_ver->minor > ldc_versions[idx].minor)
9731ae08745Sheppo 					rcvd_ver->minor =
9741ae08745Sheppo 						ldc_versions[idx].minor;
9751ae08745Sheppo 				bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver));
9761ae08745Sheppo 
9771ae08745Sheppo 				break;
9781ae08745Sheppo 			}
9791ae08745Sheppo 
9801ae08745Sheppo 			if (rcvd_ver->major > ldc_versions[idx].major) {
9811ae08745Sheppo 
9821ae08745Sheppo 				D1(ldcp->id, "i_ldc_process_VER: using next"
9831ae08745Sheppo 				    " lower idx=%d, v%u.%u\n", idx,
9841ae08745Sheppo 				    ldc_versions[idx].major,
9851ae08745Sheppo 				    ldc_versions[idx].minor);
9861ae08745Sheppo 
9871ae08745Sheppo 				/* nack with next lower version */
9881ae08745Sheppo 				pkt->stype = LDC_NACK;
9891ae08745Sheppo 				bcopy(&ldc_versions[idx], pkt->udata,
9901ae08745Sheppo 				    sizeof (ldc_versions[idx]));
9911ae08745Sheppo 				ldcp->next_vidx = idx;
9921ae08745Sheppo 				break;
9931ae08745Sheppo 			}
9941ae08745Sheppo 
9951ae08745Sheppo 			/* next major version */
9961ae08745Sheppo 			idx++;
9971ae08745Sheppo 
9981ae08745Sheppo 			D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx);
9991ae08745Sheppo 
10001ae08745Sheppo 			if (idx == LDC_NUM_VERS) {
10011ae08745Sheppo 				/* no version match - send NACK */
10021ae08745Sheppo 				pkt->stype = LDC_NACK;
10031ae08745Sheppo 				bzero(pkt->udata, sizeof (ldc_ver_t));
10041ae08745Sheppo 				ldcp->next_vidx = 0;
10051ae08745Sheppo 				break;
10061ae08745Sheppo 			}
10071ae08745Sheppo 		}
10081ae08745Sheppo 
10091ae08745Sheppo 		/* initiate the send by calling into HV and set the new tail */
10101ae08745Sheppo 		tx_tail = (tx_tail + LDC_PACKET_SIZE) %
10111ae08745Sheppo 			(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
10121ae08745Sheppo 
10131ae08745Sheppo 		rv = i_ldc_set_tx_tail(ldcp, tx_tail);
10141ae08745Sheppo 		if (rv == 0) {
10151ae08745Sheppo 			ldcp->tx_tail = tx_tail;
10161ae08745Sheppo 			if (pkt->stype == LDC_ACK) {
10171ae08745Sheppo 				D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent"
10181ae08745Sheppo 				    " version ACK\n", ldcp->id);
10191ae08745Sheppo 				/* Save the ACK'd version */
10201ae08745Sheppo 				ldcp->version.major = rcvd_ver->major;
10211ae08745Sheppo 				ldcp->version.minor = rcvd_ver->minor;
10220a55fbb7Slm66018 				ldcp->hstate |= TS_RCVD_VER;
10231ae08745Sheppo 				ldcp->tstate |= TS_VER_DONE;
1024*83d3bc6fSnarayan 				D1(DBG_ALL_LDCS,
10253af08d82Slm66018 				    "(0x%llx) Sent ACK, "
10263af08d82Slm66018 				    "Agreed on version v%u.%u\n",
10271ae08745Sheppo 				    ldcp->id, rcvd_ver->major, rcvd_ver->minor);
10281ae08745Sheppo 			}
10291ae08745Sheppo 		} else {
10301ae08745Sheppo 			DWARN(ldcp->id,
10311ae08745Sheppo 			    "i_ldc_process_VER: (0x%llx) error sending "
10321ae08745Sheppo 			    "ACK/NACK\n", ldcp->id);
10333af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1034d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
10351ae08745Sheppo 			return (ECONNRESET);
10361ae08745Sheppo 		}
10371ae08745Sheppo 
10381ae08745Sheppo 		break;
10391ae08745Sheppo 
10401ae08745Sheppo 	case LDC_ACK:
10413af08d82Slm66018 		if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) {
10423af08d82Slm66018 			if (ldcp->version.major != rcvd_ver->major ||
10433af08d82Slm66018 				ldcp->version.minor != rcvd_ver->minor) {
10443af08d82Slm66018 
10453af08d82Slm66018 				/* mismatched version - reset connection */
10463af08d82Slm66018 				DWARN(ldcp->id,
10473af08d82Slm66018 					"i_ldc_process_VER: (0x%llx) recvd"
10483af08d82Slm66018 					" ACK ver != sent ACK ver\n", ldcp->id);
10493af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
10503af08d82Slm66018 				mutex_exit(&ldcp->tx_lock);
10513af08d82Slm66018 				return (ECONNRESET);
10523af08d82Slm66018 			}
10533af08d82Slm66018 		} else {
10541ae08745Sheppo 			/* SUCCESS - we have agreed on a version */
10551ae08745Sheppo 			ldcp->version.major = rcvd_ver->major;
10561ae08745Sheppo 			ldcp->version.minor = rcvd_ver->minor;
10571ae08745Sheppo 			ldcp->tstate |= TS_VER_DONE;
10583af08d82Slm66018 		}
10591ae08745Sheppo 
1060cb112a14Slm66018 		D1(ldcp->id, "(0x%llx) Got ACK, Agreed on version v%u.%u\n",
10611ae08745Sheppo 		    ldcp->id, rcvd_ver->major, rcvd_ver->minor);
10621ae08745Sheppo 
10631ae08745Sheppo 		/* initiate RTS-RTR-RDX handshake */
10641ae08745Sheppo 		rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
10651ae08745Sheppo 		if (rv) {
10661ae08745Sheppo 			DWARN(ldcp->id,
10671ae08745Sheppo 		    "i_ldc_process_VER: (0x%llx) cannot send RTS\n",
10681ae08745Sheppo 			    ldcp->id);
10693af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1070d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
10711ae08745Sheppo 			return (ECONNRESET);
10721ae08745Sheppo 		}
10731ae08745Sheppo 
10741ae08745Sheppo 		pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
10751ae08745Sheppo 		ZERO_PKT(pkt);
10761ae08745Sheppo 
10771ae08745Sheppo 		pkt->type = LDC_CTRL;
10781ae08745Sheppo 		pkt->stype = LDC_INFO;
10791ae08745Sheppo 		pkt->ctrl = LDC_RTS;
10801ae08745Sheppo 		pkt->env = ldcp->mode;
10811ae08745Sheppo 		if (ldcp->mode != LDC_MODE_RAW)
10821ae08745Sheppo 			pkt->seqid = LDC_INIT_SEQID;
10831ae08745Sheppo 
10841ae08745Sheppo 		ldcp->last_msg_rcd = LDC_INIT_SEQID;
10851ae08745Sheppo 
10861ae08745Sheppo 		DUMP_LDC_PKT(ldcp, "i_ldc_process_VER snd rts", (uint64_t)pkt);
10871ae08745Sheppo 
10881ae08745Sheppo 		/* initiate the send by calling into HV and set the new tail */
10891ae08745Sheppo 		tx_tail = (tx_tail + LDC_PACKET_SIZE) %
10901ae08745Sheppo 			(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
10911ae08745Sheppo 
10921ae08745Sheppo 		rv = i_ldc_set_tx_tail(ldcp, tx_tail);
10931ae08745Sheppo 		if (rv) {
10941ae08745Sheppo 			D2(ldcp->id,
10951ae08745Sheppo 			    "i_ldc_process_VER: (0x%llx) no listener\n",
10961ae08745Sheppo 			    ldcp->id);
10973af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1098d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
10991ae08745Sheppo 			return (ECONNRESET);
11001ae08745Sheppo 		}
11011ae08745Sheppo 
11021ae08745Sheppo 		ldcp->tx_tail = tx_tail;
11031ae08745Sheppo 		ldcp->hstate |= TS_SENT_RTS;
11041ae08745Sheppo 
11051ae08745Sheppo 		break;
11061ae08745Sheppo 
11071ae08745Sheppo 	case LDC_NACK:
11081ae08745Sheppo 		/* check if version in NACK is zero */
11091ae08745Sheppo 		if (rcvd_ver->major == 0 && rcvd_ver->minor == 0) {
11101ae08745Sheppo 			/* version handshake failure */
11111ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
11121ae08745Sheppo 			    "i_ldc_process_VER: (0x%llx) no version match\n",
11131ae08745Sheppo 			    ldcp->id);
11143af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1115d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
11161ae08745Sheppo 			return (ECONNRESET);
11171ae08745Sheppo 		}
11181ae08745Sheppo 
11191ae08745Sheppo 		/* get the current tail and pkt for the response */
11201ae08745Sheppo 		rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
11211ae08745Sheppo 		if (rv != 0) {
11221ae08745Sheppo 			cmn_err(CE_NOTE,
11231ae08745Sheppo 			    "i_ldc_process_VER: (0x%lx) err sending "
11241ae08745Sheppo 			    "version ACK/NACK\n", ldcp->id);
11253af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1126d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
11271ae08745Sheppo 			return (ECONNRESET);
11281ae08745Sheppo 		}
11291ae08745Sheppo 
11301ae08745Sheppo 		pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
11311ae08745Sheppo 		ZERO_PKT(pkt);
11321ae08745Sheppo 
11331ae08745Sheppo 		/* initialize the packet */
11341ae08745Sheppo 		pkt->type = LDC_CTRL;
11351ae08745Sheppo 		pkt->ctrl = LDC_VER;
11361ae08745Sheppo 		pkt->stype = LDC_INFO;
11371ae08745Sheppo 
11381ae08745Sheppo 		/* check ver in NACK msg has a match */
11391ae08745Sheppo 		for (;;) {
11401ae08745Sheppo 			if (rcvd_ver->major == ldc_versions[idx].major) {
11411ae08745Sheppo 				/*
11421ae08745Sheppo 				 * major version match - resubmit request
11431ae08745Sheppo 				 * if lower minor version to the one this endpt
11441ae08745Sheppo 				 * supports, if necessary
11451ae08745Sheppo 				 */
11461ae08745Sheppo 				if (rcvd_ver->minor > ldc_versions[idx].minor)
11471ae08745Sheppo 					rcvd_ver->minor =
11481ae08745Sheppo 						ldc_versions[idx].minor;
11491ae08745Sheppo 				bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver));
11501ae08745Sheppo 				break;
11511ae08745Sheppo 
11521ae08745Sheppo 			}
11531ae08745Sheppo 
11541ae08745Sheppo 			if (rcvd_ver->major > ldc_versions[idx].major) {
11551ae08745Sheppo 
11561ae08745Sheppo 				D1(ldcp->id, "i_ldc_process_VER: using next"
11571ae08745Sheppo 				    " lower idx=%d, v%u.%u\n", idx,
11581ae08745Sheppo 				    ldc_versions[idx].major,
11591ae08745Sheppo 				    ldc_versions[idx].minor);
11601ae08745Sheppo 
11611ae08745Sheppo 				/* send next lower version */
11621ae08745Sheppo 				bcopy(&ldc_versions[idx], pkt->udata,
11631ae08745Sheppo 				    sizeof (ldc_versions[idx]));
11641ae08745Sheppo 				ldcp->next_vidx = idx;
11651ae08745Sheppo 				break;
11661ae08745Sheppo 			}
11671ae08745Sheppo 
11681ae08745Sheppo 			/* next version */
11691ae08745Sheppo 			idx++;
11701ae08745Sheppo 
11711ae08745Sheppo 			D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx);
11721ae08745Sheppo 
11731ae08745Sheppo 			if (idx == LDC_NUM_VERS) {
11741ae08745Sheppo 				/* no version match - terminate */
11751ae08745Sheppo 				ldcp->next_vidx = 0;
1176d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
11771ae08745Sheppo 				return (ECONNRESET);
11781ae08745Sheppo 			}
11791ae08745Sheppo 		}
11801ae08745Sheppo 
11811ae08745Sheppo 		/* initiate the send by calling into HV and set the new tail */
11821ae08745Sheppo 		tx_tail = (tx_tail + LDC_PACKET_SIZE) %
11831ae08745Sheppo 			(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
11841ae08745Sheppo 
11851ae08745Sheppo 		rv = i_ldc_set_tx_tail(ldcp, tx_tail);
11861ae08745Sheppo 		if (rv == 0) {
11871ae08745Sheppo 			D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent version"
11881ae08745Sheppo 			    "INFO v%u.%u\n", ldcp->id, ldc_versions[idx].major,
11891ae08745Sheppo 			    ldc_versions[idx].minor);
11901ae08745Sheppo 			ldcp->tx_tail = tx_tail;
11911ae08745Sheppo 		} else {
11921ae08745Sheppo 			cmn_err(CE_NOTE,
11931ae08745Sheppo 			    "i_ldc_process_VER: (0x%lx) error sending version"
11941ae08745Sheppo 			    "INFO\n", ldcp->id);
11953af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1196d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
11971ae08745Sheppo 			return (ECONNRESET);
11981ae08745Sheppo 		}
11991ae08745Sheppo 
12001ae08745Sheppo 		break;
12011ae08745Sheppo 	}
12021ae08745Sheppo 
1203d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
12041ae08745Sheppo 	return (rv);
12051ae08745Sheppo }
12061ae08745Sheppo 
12071ae08745Sheppo 
12081ae08745Sheppo /*
12091ae08745Sheppo  * Process an incoming RTS ctrl message
12101ae08745Sheppo  */
12111ae08745Sheppo static int
12121ae08745Sheppo i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg)
12131ae08745Sheppo {
12141ae08745Sheppo 	int 		rv = 0;
12151ae08745Sheppo 	ldc_msg_t 	*pkt;
12161ae08745Sheppo 	uint64_t	tx_tail;
12171ae08745Sheppo 	boolean_t	sent_NACK = B_FALSE;
12181ae08745Sheppo 
12191ae08745Sheppo 	D2(ldcp->id, "i_ldc_process_RTS: (0x%llx) received RTS\n", ldcp->id);
12201ae08745Sheppo 
12211ae08745Sheppo 	switch (msg->stype) {
12221ae08745Sheppo 	case LDC_NACK:
12231ae08745Sheppo 		DWARN(ldcp->id,
12241ae08745Sheppo 		    "i_ldc_process_RTS: (0x%llx) RTS NACK received\n",
12251ae08745Sheppo 		    ldcp->id);
12261ae08745Sheppo 
12271ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1228d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
12293af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1230d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
12311ae08745Sheppo 		rv = ECONNRESET;
12321ae08745Sheppo 		break;
12331ae08745Sheppo 
12341ae08745Sheppo 	case LDC_INFO:
12351ae08745Sheppo 
12361ae08745Sheppo 		/* check mode */
12371ae08745Sheppo 		if (ldcp->mode != (ldc_mode_t)msg->env) {
12381ae08745Sheppo 			cmn_err(CE_NOTE,
12391ae08745Sheppo 			    "i_ldc_process_RTS: (0x%lx) mode mismatch\n",
12401ae08745Sheppo 			    ldcp->id);
12411ae08745Sheppo 			/*
12421ae08745Sheppo 			 * send NACK in response to MODE message
12431ae08745Sheppo 			 * get the current tail for the response
12441ae08745Sheppo 			 */
12451ae08745Sheppo 			rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTS);
12461ae08745Sheppo 			if (rv) {
12471ae08745Sheppo 				/* if cannot send NACK - reset channel */
1248d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
12493af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
1250d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
12511ae08745Sheppo 				rv = ECONNRESET;
12521ae08745Sheppo 				break;
12531ae08745Sheppo 			}
12541ae08745Sheppo 			sent_NACK = B_TRUE;
12551ae08745Sheppo 		}
12561ae08745Sheppo 		break;
12571ae08745Sheppo 	default:
12581ae08745Sheppo 		DWARN(ldcp->id, "i_ldc_process_RTS: (0x%llx) unexp ACK\n",
12591ae08745Sheppo 		    ldcp->id);
1260d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
12613af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1262d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
12631ae08745Sheppo 		rv = ECONNRESET;
12641ae08745Sheppo 		break;
12651ae08745Sheppo 	}
12661ae08745Sheppo 
12671ae08745Sheppo 	/*
12681ae08745Sheppo 	 * If either the connection was reset (when rv != 0) or
12691ae08745Sheppo 	 * a NACK was sent, we return. In the case of a NACK
12701ae08745Sheppo 	 * we dont want to consume the packet that came in but
12711ae08745Sheppo 	 * not record that we received the RTS
12721ae08745Sheppo 	 */
12731ae08745Sheppo 	if (rv || sent_NACK)
12741ae08745Sheppo 		return (rv);
12751ae08745Sheppo 
12761ae08745Sheppo 	/* record RTS received */
12771ae08745Sheppo 	ldcp->hstate |= TS_RCVD_RTS;
12781ae08745Sheppo 
12791ae08745Sheppo 	/* store initial SEQID info */
12801ae08745Sheppo 	ldcp->last_msg_snt = msg->seqid;
12811ae08745Sheppo 
1282d10e4ef2Snarayan 	/* Obtain Tx lock */
1283d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
1284d10e4ef2Snarayan 
12851ae08745Sheppo 	/* get the current tail for the response */
12861ae08745Sheppo 	rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
12871ae08745Sheppo 	if (rv != 0) {
12881ae08745Sheppo 		cmn_err(CE_NOTE,
12891ae08745Sheppo 		    "i_ldc_process_RTS: (0x%lx) err sending RTR\n",
12901ae08745Sheppo 		    ldcp->id);
12913af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1292d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
12931ae08745Sheppo 		return (ECONNRESET);
12941ae08745Sheppo 	}
12951ae08745Sheppo 
12961ae08745Sheppo 	pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
12971ae08745Sheppo 	ZERO_PKT(pkt);
12981ae08745Sheppo 
12991ae08745Sheppo 	/* initialize the packet */
13001ae08745Sheppo 	pkt->type = LDC_CTRL;
13011ae08745Sheppo 	pkt->stype = LDC_INFO;
13021ae08745Sheppo 	pkt->ctrl = LDC_RTR;
13031ae08745Sheppo 	pkt->env = ldcp->mode;
13041ae08745Sheppo 	if (ldcp->mode != LDC_MODE_RAW)
13051ae08745Sheppo 		pkt->seqid = LDC_INIT_SEQID;
13061ae08745Sheppo 
13071ae08745Sheppo 	ldcp->last_msg_rcd = msg->seqid;
13081ae08745Sheppo 
13091ae08745Sheppo 	/* initiate the send by calling into HV and set the new tail */
13101ae08745Sheppo 	tx_tail = (tx_tail + LDC_PACKET_SIZE) %
13111ae08745Sheppo 		(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
13121ae08745Sheppo 
13131ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
13141ae08745Sheppo 	if (rv == 0) {
13151ae08745Sheppo 		D2(ldcp->id,
13161ae08745Sheppo 		    "i_ldc_process_RTS: (0x%llx) sent RTR\n", ldcp->id);
13171ae08745Sheppo 		DUMP_LDC_PKT(ldcp, "i_ldc_process_RTS sent rtr", (uint64_t)pkt);
13181ae08745Sheppo 
13191ae08745Sheppo 		ldcp->tx_tail = tx_tail;
13201ae08745Sheppo 		ldcp->hstate |= TS_SENT_RTR;
13211ae08745Sheppo 
13221ae08745Sheppo 	} else {
13231ae08745Sheppo 		cmn_err(CE_NOTE,
13241ae08745Sheppo 		    "i_ldc_process_RTS: (0x%lx) error sending RTR\n",
13251ae08745Sheppo 		    ldcp->id);
13263af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1327d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
13281ae08745Sheppo 		return (ECONNRESET);
13291ae08745Sheppo 	}
13301ae08745Sheppo 
1331d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
13321ae08745Sheppo 	return (0);
13331ae08745Sheppo }
13341ae08745Sheppo 
13351ae08745Sheppo /*
13361ae08745Sheppo  * Process an incoming RTR ctrl message
13371ae08745Sheppo  */
13381ae08745Sheppo static int
13391ae08745Sheppo i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg)
13401ae08745Sheppo {
13411ae08745Sheppo 	int 		rv = 0;
13421ae08745Sheppo 	boolean_t	sent_NACK = B_FALSE;
13431ae08745Sheppo 
13441ae08745Sheppo 	D2(ldcp->id, "i_ldc_process_RTR: (0x%llx) received RTR\n", ldcp->id);
13451ae08745Sheppo 
13461ae08745Sheppo 	switch (msg->stype) {
13471ae08745Sheppo 	case LDC_NACK:
13481ae08745Sheppo 		/* RTR NACK received */
13491ae08745Sheppo 		DWARN(ldcp->id,
13501ae08745Sheppo 		    "i_ldc_process_RTR: (0x%llx) RTR NACK received\n",
13511ae08745Sheppo 		    ldcp->id);
13521ae08745Sheppo 
13531ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1354d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
13553af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1356d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
13571ae08745Sheppo 		rv = ECONNRESET;
13581ae08745Sheppo 
13591ae08745Sheppo 		break;
13601ae08745Sheppo 
13611ae08745Sheppo 	case LDC_INFO:
13621ae08745Sheppo 
13631ae08745Sheppo 		/* check mode */
13641ae08745Sheppo 		if (ldcp->mode != (ldc_mode_t)msg->env) {
13651ae08745Sheppo 			DWARN(ldcp->id,
1366cb112a14Slm66018 			    "i_ldc_process_RTR: (0x%llx) mode mismatch, "
1367cb112a14Slm66018 			    "expecting 0x%x, got 0x%x\n",
1368cb112a14Slm66018 			    ldcp->id, ldcp->mode, (ldc_mode_t)msg->env);
13691ae08745Sheppo 			/*
13701ae08745Sheppo 			 * send NACK in response to MODE message
13711ae08745Sheppo 			 * get the current tail for the response
13721ae08745Sheppo 			 */
13731ae08745Sheppo 			rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTR);
13741ae08745Sheppo 			if (rv) {
13751ae08745Sheppo 				/* if cannot send NACK - reset channel */
1376d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
13773af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
1378d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
13791ae08745Sheppo 				rv = ECONNRESET;
13801ae08745Sheppo 				break;
13811ae08745Sheppo 			}
13821ae08745Sheppo 			sent_NACK = B_TRUE;
13831ae08745Sheppo 		}
13841ae08745Sheppo 		break;
13851ae08745Sheppo 
13861ae08745Sheppo 	default:
13871ae08745Sheppo 		DWARN(ldcp->id, "i_ldc_process_RTR: (0x%llx) unexp ACK\n",
13881ae08745Sheppo 		    ldcp->id);
13891ae08745Sheppo 
13901ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1391d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
13923af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1393d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
13941ae08745Sheppo 		rv = ECONNRESET;
13951ae08745Sheppo 		break;
13961ae08745Sheppo 	}
13971ae08745Sheppo 
13981ae08745Sheppo 	/*
13991ae08745Sheppo 	 * If either the connection was reset (when rv != 0) or
14001ae08745Sheppo 	 * a NACK was sent, we return. In the case of a NACK
14011ae08745Sheppo 	 * we dont want to consume the packet that came in but
14021ae08745Sheppo 	 * not record that we received the RTR
14031ae08745Sheppo 	 */
14041ae08745Sheppo 	if (rv || sent_NACK)
14051ae08745Sheppo 		return (rv);
14061ae08745Sheppo 
14071ae08745Sheppo 	ldcp->last_msg_snt = msg->seqid;
14081ae08745Sheppo 	ldcp->hstate |= TS_RCVD_RTR;
14091ae08745Sheppo 
14101ae08745Sheppo 	rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_INFO, LDC_RDX);
14111ae08745Sheppo 	if (rv) {
14121ae08745Sheppo 		cmn_err(CE_NOTE,
14131ae08745Sheppo 		    "i_ldc_process_RTR: (0x%lx) cannot send RDX\n",
14141ae08745Sheppo 		    ldcp->id);
1415d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
14163af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1417d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
14181ae08745Sheppo 		return (ECONNRESET);
14191ae08745Sheppo 	}
14201ae08745Sheppo 	D2(ldcp->id,
14211ae08745Sheppo 	    "i_ldc_process_RTR: (0x%llx) sent RDX\n", ldcp->id);
14221ae08745Sheppo 
14231ae08745Sheppo 	ldcp->hstate |= TS_SENT_RDX;
14241ae08745Sheppo 	ldcp->tstate |= TS_HSHAKE_DONE;
14253af08d82Slm66018 	if ((ldcp->tstate & TS_IN_RESET) == 0)
14261ae08745Sheppo 		ldcp->status = LDC_UP;
14271ae08745Sheppo 
1428cb112a14Slm66018 	D1(ldcp->id, "(0x%llx) Handshake Complete\n", ldcp->id);
14291ae08745Sheppo 
14301ae08745Sheppo 	return (0);
14311ae08745Sheppo }
14321ae08745Sheppo 
14331ae08745Sheppo 
14341ae08745Sheppo /*
14351ae08745Sheppo  * Process an incoming RDX ctrl message
14361ae08745Sheppo  */
14371ae08745Sheppo static int
14381ae08745Sheppo i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg)
14391ae08745Sheppo {
14401ae08745Sheppo 	int	rv = 0;
14411ae08745Sheppo 
14421ae08745Sheppo 	D2(ldcp->id, "i_ldc_process_RDX: (0x%llx) received RDX\n", ldcp->id);
14431ae08745Sheppo 
14441ae08745Sheppo 	switch (msg->stype) {
14451ae08745Sheppo 	case LDC_NACK:
14461ae08745Sheppo 		/* RDX NACK received */
14471ae08745Sheppo 		DWARN(ldcp->id,
14481ae08745Sheppo 		    "i_ldc_process_RDX: (0x%llx) RDX NACK received\n",
14491ae08745Sheppo 		    ldcp->id);
14501ae08745Sheppo 
14511ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1452d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
14533af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1454d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
14551ae08745Sheppo 		rv = ECONNRESET;
14561ae08745Sheppo 
14571ae08745Sheppo 		break;
14581ae08745Sheppo 
14591ae08745Sheppo 	case LDC_INFO:
14601ae08745Sheppo 
14611ae08745Sheppo 		/*
14621ae08745Sheppo 		 * if channel is UP and a RDX received after data transmission
14631ae08745Sheppo 		 * has commenced it is an error
14641ae08745Sheppo 		 */
14651ae08745Sheppo 		if ((ldcp->tstate == TS_UP) && (ldcp->hstate & TS_RCVD_RDX)) {
14661ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
14671ae08745Sheppo 			    "i_ldc_process_RDX: (0x%llx) unexpected RDX"
14681ae08745Sheppo 			    " - LDC reset\n", ldcp->id);
1469d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
14703af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1471d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
14721ae08745Sheppo 			return (ECONNRESET);
14731ae08745Sheppo 		}
14741ae08745Sheppo 
14751ae08745Sheppo 		ldcp->hstate |= TS_RCVD_RDX;
14761ae08745Sheppo 		ldcp->tstate |= TS_HSHAKE_DONE;
14773af08d82Slm66018 		if ((ldcp->tstate & TS_IN_RESET) == 0)
14781ae08745Sheppo 			ldcp->status = LDC_UP;
14791ae08745Sheppo 
14801ae08745Sheppo 		D1(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id);
14811ae08745Sheppo 		break;
14821ae08745Sheppo 
14831ae08745Sheppo 	default:
14841ae08745Sheppo 		DWARN(ldcp->id, "i_ldc_process_RDX: (0x%llx) unexp ACK\n",
14851ae08745Sheppo 		    ldcp->id);
14861ae08745Sheppo 
14871ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1488d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
14893af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1490d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
14911ae08745Sheppo 		rv = ECONNRESET;
14921ae08745Sheppo 		break;
14931ae08745Sheppo 	}
14941ae08745Sheppo 
14951ae08745Sheppo 	return (rv);
14961ae08745Sheppo }
14971ae08745Sheppo 
14981ae08745Sheppo /*
14991ae08745Sheppo  * Process an incoming ACK for a data packet
15001ae08745Sheppo  */
15011ae08745Sheppo static int
15021ae08745Sheppo i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg)
15031ae08745Sheppo {
15041ae08745Sheppo 	int		rv;
15051ae08745Sheppo 	uint64_t 	tx_head;
15061ae08745Sheppo 	ldc_msg_t	*pkt;
15071ae08745Sheppo 
1508d10e4ef2Snarayan 	/* Obtain Tx lock */
1509d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
1510d10e4ef2Snarayan 
15111ae08745Sheppo 	/*
1512d10e4ef2Snarayan 	 * Read the current Tx head and tail
15131ae08745Sheppo 	 */
15141ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
15151ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
15161ae08745Sheppo 	if (rv != 0) {
15171ae08745Sheppo 		cmn_err(CE_WARN,
15181ae08745Sheppo 		    "i_ldc_process_data_ACK: (0x%lx) cannot read qptrs\n",
15191ae08745Sheppo 		    ldcp->id);
1520d10e4ef2Snarayan 
1521d10e4ef2Snarayan 		/* Reset the channel -- as we cannot continue */
15223af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1523d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
1524d10e4ef2Snarayan 		return (ECONNRESET);
15251ae08745Sheppo 	}
15261ae08745Sheppo 
15271ae08745Sheppo 	/*
15281ae08745Sheppo 	 * loop from where the previous ACK location was to the
15291ae08745Sheppo 	 * current head location. This is how far the HV has
15301ae08745Sheppo 	 * actually send pkts. Pkts between head and tail are
15311ae08745Sheppo 	 * yet to be sent by HV.
15321ae08745Sheppo 	 */
15331ae08745Sheppo 	tx_head = ldcp->tx_ackd_head;
15341ae08745Sheppo 	for (;;) {
15351ae08745Sheppo 		pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_head);
15361ae08745Sheppo 		tx_head = (tx_head + LDC_PACKET_SIZE) %
15371ae08745Sheppo 			(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
15381ae08745Sheppo 
15391ae08745Sheppo 		if (pkt->seqid == msg->ackid) {
15401ae08745Sheppo 			D2(ldcp->id,
15411ae08745Sheppo 			    "i_ldc_process_data_ACK: (0x%llx) found packet\n",
15421ae08745Sheppo 			    ldcp->id);
15431ae08745Sheppo 			ldcp->last_ack_rcd = msg->ackid;
15441ae08745Sheppo 			ldcp->tx_ackd_head = tx_head;
15451ae08745Sheppo 			break;
15461ae08745Sheppo 		}
15471ae08745Sheppo 		if (tx_head == ldcp->tx_head) {
15481ae08745Sheppo 			/* could not find packet */
15491ae08745Sheppo 			DWARN(ldcp->id,
15501ae08745Sheppo 			    "i_ldc_process_data_ACK: (0x%llx) invalid ACKid\n",
15511ae08745Sheppo 			    ldcp->id);
1552d10e4ef2Snarayan 
1553d10e4ef2Snarayan 			/* Reset the channel -- as we cannot continue */
15543af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1555d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
1556d10e4ef2Snarayan 			return (ECONNRESET);
15571ae08745Sheppo 		}
15581ae08745Sheppo 	}
15591ae08745Sheppo 
1560d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
15611ae08745Sheppo 	return (0);
15621ae08745Sheppo }
15631ae08745Sheppo 
15641ae08745Sheppo /*
15651ae08745Sheppo  * Process incoming control message
15661ae08745Sheppo  * Return 0 - session can continue
15671ae08745Sheppo  *        EAGAIN - reprocess packet - state was changed
15681ae08745Sheppo  *	  ECONNRESET - channel was reset
15691ae08745Sheppo  */
15701ae08745Sheppo static int
15711ae08745Sheppo i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *msg)
15721ae08745Sheppo {
15731ae08745Sheppo 	int 		rv = 0;
15741ae08745Sheppo 
15753af08d82Slm66018 	D1(ldcp->id, "i_ldc_ctrlmsg: (%llx) tstate = %lx, hstate = %lx\n",
15763af08d82Slm66018 	    ldcp->id, ldcp->tstate, ldcp->hstate);
15773af08d82Slm66018 
15783af08d82Slm66018 	switch (ldcp->tstate & ~TS_IN_RESET) {
15791ae08745Sheppo 
15801ae08745Sheppo 	case TS_OPEN:
15811ae08745Sheppo 	case TS_READY:
15821ae08745Sheppo 
15831ae08745Sheppo 		switch (msg->ctrl & LDC_CTRL_MASK) {
15841ae08745Sheppo 		case LDC_VER:
15851ae08745Sheppo 			/* process version message */
15861ae08745Sheppo 			rv = i_ldc_process_VER(ldcp, msg);
15871ae08745Sheppo 			break;
15881ae08745Sheppo 		default:
15891ae08745Sheppo 			DWARN(ldcp->id,
15901ae08745Sheppo 			    "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x "
15911ae08745Sheppo 			    "tstate=0x%x\n", ldcp->id,
15921ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate);
15931ae08745Sheppo 			break;
15941ae08745Sheppo 		}
15951ae08745Sheppo 
15961ae08745Sheppo 		break;
15971ae08745Sheppo 
15981ae08745Sheppo 	case TS_VREADY:
15991ae08745Sheppo 
16001ae08745Sheppo 		switch (msg->ctrl & LDC_CTRL_MASK) {
16011ae08745Sheppo 		case LDC_VER:
16023af08d82Slm66018 			/* process version message */
16033af08d82Slm66018 			rv = i_ldc_process_VER(ldcp, msg);
16041ae08745Sheppo 			break;
16051ae08745Sheppo 		case LDC_RTS:
16061ae08745Sheppo 			/* process RTS message */
16071ae08745Sheppo 			rv = i_ldc_process_RTS(ldcp, msg);
16081ae08745Sheppo 			break;
16091ae08745Sheppo 		case LDC_RTR:
16101ae08745Sheppo 			/* process RTR message */
16111ae08745Sheppo 			rv = i_ldc_process_RTR(ldcp, msg);
16121ae08745Sheppo 			break;
16131ae08745Sheppo 		case LDC_RDX:
16141ae08745Sheppo 			/* process RDX message */
16151ae08745Sheppo 			rv = i_ldc_process_RDX(ldcp, msg);
16161ae08745Sheppo 			break;
16171ae08745Sheppo 		default:
16181ae08745Sheppo 			DWARN(ldcp->id,
16191ae08745Sheppo 			    "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x "
16201ae08745Sheppo 			    "tstate=0x%x\n", ldcp->id,
16211ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate);
16221ae08745Sheppo 			break;
16231ae08745Sheppo 		}
16241ae08745Sheppo 
16251ae08745Sheppo 		break;
16261ae08745Sheppo 
16271ae08745Sheppo 	case TS_UP:
16281ae08745Sheppo 
16291ae08745Sheppo 		switch (msg->ctrl & LDC_CTRL_MASK) {
16301ae08745Sheppo 		case LDC_VER:
16311ae08745Sheppo 			DWARN(ldcp->id,
16321ae08745Sheppo 			    "i_ldc_ctrlmsg: (0x%llx) unexpected VER "
16331ae08745Sheppo 			    "- LDC reset\n", ldcp->id);
16341ae08745Sheppo 			/* peer is redoing version negotiation */
1635d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
16361ae08745Sheppo 			(void) i_ldc_txq_reconf(ldcp);
16371ae08745Sheppo 			i_ldc_reset_state(ldcp);
1638d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
16391ae08745Sheppo 			rv = EAGAIN;
16401ae08745Sheppo 			break;
16411ae08745Sheppo 
16421ae08745Sheppo 		case LDC_RDX:
16431ae08745Sheppo 			/* process RDX message */
16441ae08745Sheppo 			rv = i_ldc_process_RDX(ldcp, msg);
16451ae08745Sheppo 			break;
16461ae08745Sheppo 
16471ae08745Sheppo 		default:
16481ae08745Sheppo 			DWARN(ldcp->id,
16491ae08745Sheppo 			    "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x "
16501ae08745Sheppo 			    "tstate=0x%x\n", ldcp->id,
16511ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate);
16521ae08745Sheppo 			break;
16531ae08745Sheppo 		}
16541ae08745Sheppo 	}
16551ae08745Sheppo 
16561ae08745Sheppo 	return (rv);
16571ae08745Sheppo }
16581ae08745Sheppo 
16591ae08745Sheppo /*
16601ae08745Sheppo  * Register channel with the channel nexus
16611ae08745Sheppo  */
16621ae08745Sheppo static int
16631ae08745Sheppo i_ldc_register_channel(ldc_chan_t *ldcp)
16641ae08745Sheppo {
16651ae08745Sheppo 	int		rv = 0;
16661ae08745Sheppo 	ldc_cnex_t	*cinfo = &ldcssp->cinfo;
16671ae08745Sheppo 
16681ae08745Sheppo 	if (cinfo->dip == NULL) {
16691ae08745Sheppo 		DWARN(ldcp->id,
16701ae08745Sheppo 		    "i_ldc_register_channel: cnex has not registered\n");
16711ae08745Sheppo 		return (EAGAIN);
16721ae08745Sheppo 	}
16731ae08745Sheppo 
16741ae08745Sheppo 	rv = cinfo->reg_chan(cinfo->dip, ldcp->id, ldcp->devclass);
16751ae08745Sheppo 	if (rv) {
16761ae08745Sheppo 		DWARN(ldcp->id,
16771ae08745Sheppo 		    "i_ldc_register_channel: cannot register channel\n");
16781ae08745Sheppo 		return (rv);
16791ae08745Sheppo 	}
16801ae08745Sheppo 
16811ae08745Sheppo 	rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR,
16821ae08745Sheppo 	    i_ldc_tx_hdlr, ldcp, NULL);
16831ae08745Sheppo 	if (rv) {
16841ae08745Sheppo 		DWARN(ldcp->id,
16851ae08745Sheppo 		    "i_ldc_register_channel: cannot add Tx interrupt\n");
16861ae08745Sheppo 		(void) cinfo->unreg_chan(cinfo->dip, ldcp->id);
16871ae08745Sheppo 		return (rv);
16881ae08745Sheppo 	}
16891ae08745Sheppo 
16901ae08745Sheppo 	rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR,
16911ae08745Sheppo 	    i_ldc_rx_hdlr, ldcp, NULL);
16921ae08745Sheppo 	if (rv) {
16931ae08745Sheppo 		DWARN(ldcp->id,
16941ae08745Sheppo 		    "i_ldc_register_channel: cannot add Rx interrupt\n");
16951ae08745Sheppo 		(void) cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR);
16961ae08745Sheppo 		(void) cinfo->unreg_chan(cinfo->dip, ldcp->id);
16971ae08745Sheppo 		return (rv);
16981ae08745Sheppo 	}
16991ae08745Sheppo 
17001ae08745Sheppo 	ldcp->tstate |= TS_CNEX_RDY;
17011ae08745Sheppo 
17021ae08745Sheppo 	return (0);
17031ae08745Sheppo }
17041ae08745Sheppo 
17051ae08745Sheppo /*
17061ae08745Sheppo  * Unregister a channel with the channel nexus
17071ae08745Sheppo  */
17081ae08745Sheppo static int
17091ae08745Sheppo i_ldc_unregister_channel(ldc_chan_t *ldcp)
17101ae08745Sheppo {
17111ae08745Sheppo 	int		rv = 0;
17121ae08745Sheppo 	ldc_cnex_t	*cinfo = &ldcssp->cinfo;
17131ae08745Sheppo 
17141ae08745Sheppo 	if (cinfo->dip == NULL) {
17151ae08745Sheppo 		DWARN(ldcp->id,
17161ae08745Sheppo 		    "i_ldc_unregister_channel: cnex has not registered\n");
17171ae08745Sheppo 		return (EAGAIN);
17181ae08745Sheppo 	}
17191ae08745Sheppo 
17201ae08745Sheppo 	if (ldcp->tstate & TS_CNEX_RDY) {
17211ae08745Sheppo 
1722d10e4ef2Snarayan 		/* Remove the Rx interrupt */
17231ae08745Sheppo 		rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR);
17241ae08745Sheppo 		if (rv) {
17253af08d82Slm66018 			if (rv != EAGAIN) {
17261ae08745Sheppo 				DWARN(ldcp->id,
17273af08d82Slm66018 				    "i_ldc_unregister_channel: err removing "
17283af08d82Slm66018 				    "Rx intr\n");
1729d10e4ef2Snarayan 				return (rv);
17301ae08745Sheppo 			}
1731d10e4ef2Snarayan 
17323af08d82Slm66018 			/*
17333af08d82Slm66018 			 * If interrupts are pending and handler has
17343af08d82Slm66018 			 * finished running, clear interrupt and try
17353af08d82Slm66018 			 * again
17363af08d82Slm66018 			 */
17373af08d82Slm66018 			if (ldcp->rx_intr_state != LDC_INTR_PEND)
17383af08d82Slm66018 				return (rv);
17393af08d82Slm66018 
17403af08d82Slm66018 			(void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
17413af08d82Slm66018 			rv = cinfo->rem_intr(cinfo->dip, ldcp->id,
17423af08d82Slm66018 			    CNEX_RX_INTR);
17433af08d82Slm66018 			if (rv) {
17443af08d82Slm66018 				DWARN(ldcp->id, "i_ldc_unregister_channel: "
17453af08d82Slm66018 				    "err removing Rx interrupt\n");
17463af08d82Slm66018 				return (rv);
17473af08d82Slm66018 			}
17483af08d82Slm66018 		}
17493af08d82Slm66018 
1750d10e4ef2Snarayan 		/* Remove the Tx interrupt */
17511ae08745Sheppo 		rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR);
17521ae08745Sheppo 		if (rv) {
17531ae08745Sheppo 			DWARN(ldcp->id,
17541ae08745Sheppo 			    "i_ldc_unregister_channel: err removing Tx intr\n");
1755d10e4ef2Snarayan 			return (rv);
17561ae08745Sheppo 		}
1757d10e4ef2Snarayan 
1758d10e4ef2Snarayan 		/* Unregister the channel */
17591ae08745Sheppo 		rv = cinfo->unreg_chan(ldcssp->cinfo.dip, ldcp->id);
17601ae08745Sheppo 		if (rv) {
17611ae08745Sheppo 			DWARN(ldcp->id,
17621ae08745Sheppo 			    "i_ldc_unregister_channel: cannot unreg channel\n");
1763d10e4ef2Snarayan 			return (rv);
17641ae08745Sheppo 		}
17651ae08745Sheppo 
17661ae08745Sheppo 		ldcp->tstate &= ~TS_CNEX_RDY;
17671ae08745Sheppo 	}
17681ae08745Sheppo 
17691ae08745Sheppo 	return (0);
17701ae08745Sheppo }
17711ae08745Sheppo 
17721ae08745Sheppo 
17731ae08745Sheppo /*
17741ae08745Sheppo  * LDC transmit interrupt handler
17751ae08745Sheppo  *    triggered for chanel up/down/reset events
17761ae08745Sheppo  *    and Tx queue content changes
17771ae08745Sheppo  */
17781ae08745Sheppo static uint_t
17791ae08745Sheppo i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2)
17801ae08745Sheppo {
17811ae08745Sheppo 	_NOTE(ARGUNUSED(arg2))
17821ae08745Sheppo 
17831ae08745Sheppo 	int 		rv;
17841ae08745Sheppo 	ldc_chan_t 	*ldcp;
17851ae08745Sheppo 	boolean_t 	notify_client = B_FALSE;
17863af08d82Slm66018 	uint64_t	notify_event = 0, link_state;
17871ae08745Sheppo 
17881ae08745Sheppo 	/* Get the channel for which interrupt was received */
17891ae08745Sheppo 	ASSERT(arg1 != NULL);
17901ae08745Sheppo 	ldcp = (ldc_chan_t *)arg1;
17911ae08745Sheppo 
17921ae08745Sheppo 	D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n",
17931ae08745Sheppo 	    ldcp->id, ldcp);
17941ae08745Sheppo 
17951ae08745Sheppo 	/* Lock channel */
17961ae08745Sheppo 	mutex_enter(&ldcp->lock);
17971ae08745Sheppo 
1798d10e4ef2Snarayan 	/* Obtain Tx lock */
1799d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
1800d10e4ef2Snarayan 
18014bac2208Snarayan 	/* mark interrupt as pending */
18023af08d82Slm66018 	ldcp->tx_intr_state = LDC_INTR_ACTIVE;
18033af08d82Slm66018 
18043af08d82Slm66018 	/* save current link state */
18053af08d82Slm66018 	link_state = ldcp->link_state;
18064bac2208Snarayan 
18071ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id, &ldcp->tx_head, &ldcp->tx_tail,
18081ae08745Sheppo 	    &ldcp->link_state);
18091ae08745Sheppo 	if (rv) {
18101ae08745Sheppo 		cmn_err(CE_WARN,
18111ae08745Sheppo 		    "i_ldc_tx_hdlr: (0x%lx) cannot read queue ptrs rv=0x%d\n",
18121ae08745Sheppo 		    ldcp->id, rv);
18134bac2208Snarayan 		i_ldc_clear_intr(ldcp, CNEX_TX_INTR);
1814d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
18151ae08745Sheppo 		mutex_exit(&ldcp->lock);
18161ae08745Sheppo 		return (DDI_INTR_CLAIMED);
18171ae08745Sheppo 	}
18181ae08745Sheppo 
18191ae08745Sheppo 	/*
18201ae08745Sheppo 	 * reset the channel state if the channel went down
18211ae08745Sheppo 	 * (other side unconfigured queue) or channel was reset
18221ae08745Sheppo 	 * (other side reconfigured its queue)
18231ae08745Sheppo 	 */
18243af08d82Slm66018 	if (link_state != ldcp->link_state &&
18253af08d82Slm66018 	    ldcp->link_state == LDC_CHANNEL_DOWN) {
18261ae08745Sheppo 		D1(ldcp->id, "i_ldc_tx_hdlr: channel link down\n", ldcp->id);
18273af08d82Slm66018 		i_ldc_reset(ldcp, B_FALSE);
18281ae08745Sheppo 		notify_client = B_TRUE;
18291ae08745Sheppo 		notify_event = LDC_EVT_DOWN;
18301ae08745Sheppo 	}
18311ae08745Sheppo 
18323af08d82Slm66018 	if (link_state != ldcp->link_state &&
18333af08d82Slm66018 	    ldcp->link_state == LDC_CHANNEL_RESET) {
18341ae08745Sheppo 		D1(ldcp->id, "i_ldc_tx_hdlr: channel link reset\n", ldcp->id);
18353af08d82Slm66018 		i_ldc_reset(ldcp, B_FALSE);
18361ae08745Sheppo 		notify_client = B_TRUE;
18371ae08745Sheppo 		notify_event = LDC_EVT_RESET;
18381ae08745Sheppo 	}
18391ae08745Sheppo 
18403af08d82Slm66018 	if (link_state != ldcp->link_state &&
18413af08d82Slm66018 	    (ldcp->tstate & ~TS_IN_RESET) == TS_OPEN &&
18423af08d82Slm66018 	    ldcp->link_state == LDC_CHANNEL_UP) {
18431ae08745Sheppo 		D1(ldcp->id, "i_ldc_tx_hdlr: channel link up\n", ldcp->id);
18441ae08745Sheppo 		notify_client = B_TRUE;
18451ae08745Sheppo 		notify_event = LDC_EVT_RESET;
18461ae08745Sheppo 		ldcp->tstate |= TS_LINK_READY;
18471ae08745Sheppo 		ldcp->status = LDC_READY;
18481ae08745Sheppo 	}
18491ae08745Sheppo 
18501ae08745Sheppo 	/* if callbacks are disabled, do not notify */
18511ae08745Sheppo 	if (!ldcp->cb_enabled)
18521ae08745Sheppo 		notify_client = B_FALSE;
18531ae08745Sheppo 
18544d39be2bSsg70180 	i_ldc_clear_intr(ldcp, CNEX_TX_INTR);
18551ae08745Sheppo 
18561ae08745Sheppo 	if (notify_client) {
18573af08d82Slm66018 		ldcp->cb_inprogress = B_TRUE;
18583af08d82Slm66018 		mutex_exit(&ldcp->tx_lock);
18593af08d82Slm66018 		mutex_exit(&ldcp->lock);
18601ae08745Sheppo 		rv = ldcp->cb(notify_event, ldcp->cb_arg);
18611ae08745Sheppo 		if (rv) {
18621ae08745Sheppo 			DWARN(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) callback "
18631ae08745Sheppo 			    "failure", ldcp->id);
18641ae08745Sheppo 		}
18651ae08745Sheppo 		mutex_enter(&ldcp->lock);
18661ae08745Sheppo 		ldcp->cb_inprogress = B_FALSE;
18671ae08745Sheppo 	}
18681ae08745Sheppo 
18691ae08745Sheppo 	mutex_exit(&ldcp->lock);
18701ae08745Sheppo 
18711ae08745Sheppo 	D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) exiting handler", ldcp->id);
18721ae08745Sheppo 
18731ae08745Sheppo 	return (DDI_INTR_CLAIMED);
18741ae08745Sheppo }
18751ae08745Sheppo 
18761ae08745Sheppo /*
18771ae08745Sheppo  * LDC receive interrupt handler
18781ae08745Sheppo  *    triggered for channel with data pending to read
18791ae08745Sheppo  *    i.e. Rx queue content changes
18801ae08745Sheppo  */
18811ae08745Sheppo static uint_t
18821ae08745Sheppo i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2)
18831ae08745Sheppo {
18841ae08745Sheppo 	_NOTE(ARGUNUSED(arg2))
18851ae08745Sheppo 
18861ae08745Sheppo 	int		rv;
18871ae08745Sheppo 	uint64_t 	rx_head, rx_tail;
18881ae08745Sheppo 	ldc_msg_t 	*msg;
18891ae08745Sheppo 	ldc_chan_t 	*ldcp;
18901ae08745Sheppo 	boolean_t 	notify_client = B_FALSE;
18911ae08745Sheppo 	uint64_t	notify_event = 0;
18923af08d82Slm66018 	uint64_t	link_state, first_fragment = 0;
18933af08d82Slm66018 
18941ae08745Sheppo 
18951ae08745Sheppo 	/* Get the channel for which interrupt was received */
18961ae08745Sheppo 	if (arg1 == NULL) {
18971ae08745Sheppo 		cmn_err(CE_WARN, "i_ldc_rx_hdlr: invalid arg\n");
18981ae08745Sheppo 		return (DDI_INTR_UNCLAIMED);
18991ae08745Sheppo 	}
19001ae08745Sheppo 
19011ae08745Sheppo 	ldcp = (ldc_chan_t *)arg1;
19021ae08745Sheppo 
19031ae08745Sheppo 	D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n",
19041ae08745Sheppo 	    ldcp->id, ldcp);
19053af08d82Slm66018 	D1(ldcp->id, "i_ldc_rx_hdlr: (%llx) USR%lx/TS%lx/HS%lx, LSTATE=%lx\n",
19063af08d82Slm66018 	    ldcp->id, ldcp->status, ldcp->tstate, ldcp->hstate,
19073af08d82Slm66018 	    ldcp->link_state);
19081ae08745Sheppo 
19091ae08745Sheppo 	/* Lock channel */
19101ae08745Sheppo 	mutex_enter(&ldcp->lock);
19111ae08745Sheppo 
19121ae08745Sheppo 	/* mark interrupt as pending */
19133af08d82Slm66018 	ldcp->rx_intr_state = LDC_INTR_ACTIVE;
19141ae08745Sheppo 
19151ae08745Sheppo 	/*
19161ae08745Sheppo 	 * Read packet(s) from the queue
19171ae08745Sheppo 	 */
19181ae08745Sheppo 	for (;;) {
19191ae08745Sheppo 
19203af08d82Slm66018 		link_state = ldcp->link_state;
19211ae08745Sheppo 		rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
19221ae08745Sheppo 		    &ldcp->link_state);
19231ae08745Sheppo 		if (rv) {
19241ae08745Sheppo 			cmn_err(CE_WARN,
19251ae08745Sheppo 			    "i_ldc_rx_hdlr: (0x%lx) cannot read "
19261ae08745Sheppo 			    "queue ptrs, rv=0x%d\n", ldcp->id, rv);
19271ae08745Sheppo 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
19281ae08745Sheppo 			mutex_exit(&ldcp->lock);
19291ae08745Sheppo 			return (DDI_INTR_CLAIMED);
19301ae08745Sheppo 		}
19311ae08745Sheppo 
19321ae08745Sheppo 		/*
19331ae08745Sheppo 		 * reset the channel state if the channel went down
19341ae08745Sheppo 		 * (other side unconfigured queue) or channel was reset
19353af08d82Slm66018 		 * (other side reconfigured its queue)
19361ae08745Sheppo 		 */
19373af08d82Slm66018 
19383af08d82Slm66018 		if (link_state != ldcp->link_state) {
1939cb112a14Slm66018 
19403af08d82Slm66018 			switch (ldcp->link_state) {
19413af08d82Slm66018 			case LDC_CHANNEL_DOWN:
19423af08d82Slm66018 				D1(ldcp->id, "i_ldc_rx_hdlr: channel "
19433af08d82Slm66018 				    "link down\n", ldcp->id);
1944d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
19453af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
1946d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
19471ae08745Sheppo 				notify_client = B_TRUE;
19481ae08745Sheppo 				notify_event = LDC_EVT_DOWN;
19493af08d82Slm66018 				goto loop_exit;
19501ae08745Sheppo 
19513af08d82Slm66018 			case LDC_CHANNEL_UP:
19523af08d82Slm66018 				D1(ldcp->id, "i_ldc_rx_hdlr: "
19533af08d82Slm66018 				    "channel link up\n", ldcp->id);
19543af08d82Slm66018 
19553af08d82Slm66018 				if ((ldcp->tstate & ~TS_IN_RESET) == TS_OPEN) {
19561ae08745Sheppo 					notify_client = B_TRUE;
19571ae08745Sheppo 					notify_event = LDC_EVT_RESET;
19581ae08745Sheppo 					ldcp->tstate |= TS_LINK_READY;
19591ae08745Sheppo 					ldcp->status = LDC_READY;
19601ae08745Sheppo 				}
19613af08d82Slm66018 				break;
19623af08d82Slm66018 
19633af08d82Slm66018 			case LDC_CHANNEL_RESET:
19643af08d82Slm66018 			default:
19653af08d82Slm66018 #ifdef DEBUG
19663af08d82Slm66018 force_reset:
19673af08d82Slm66018 #endif
19683af08d82Slm66018 				D1(ldcp->id, "i_ldc_rx_hdlr: channel "
19693af08d82Slm66018 				    "link reset\n", ldcp->id);
19703af08d82Slm66018 				mutex_enter(&ldcp->tx_lock);
19713af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
19723af08d82Slm66018 				mutex_exit(&ldcp->tx_lock);
19733af08d82Slm66018 				notify_client = B_TRUE;
19743af08d82Slm66018 				notify_event = LDC_EVT_RESET;
19753af08d82Slm66018 				break;
19763af08d82Slm66018 			}
19773af08d82Slm66018 		}
19783af08d82Slm66018 
19793af08d82Slm66018 #ifdef DEBUG
19803af08d82Slm66018 		if (LDC_INJECT_RESET(ldcp))
19813af08d82Slm66018 			goto force_reset;
19823af08d82Slm66018 #endif
19831ae08745Sheppo 
19841ae08745Sheppo 		if (rx_head == rx_tail) {
19851ae08745Sheppo 			D2(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) No packets\n",
19861ae08745Sheppo 			    ldcp->id);
19871ae08745Sheppo 			break;
19881ae08745Sheppo 		}
19893af08d82Slm66018 
19901ae08745Sheppo 		D2(ldcp->id, "i_ldc_rx_hdlr: head=0x%llx, tail=0x%llx\n",
19911ae08745Sheppo 		    rx_head, rx_tail);
19921ae08745Sheppo 		DUMP_LDC_PKT(ldcp, "i_ldc_rx_hdlr rcd",
19931ae08745Sheppo 		    ldcp->rx_q_va + rx_head);
19941ae08745Sheppo 
19951ae08745Sheppo 		/* get the message */
19961ae08745Sheppo 		msg = (ldc_msg_t *)(ldcp->rx_q_va + rx_head);
19971ae08745Sheppo 
19981ae08745Sheppo 		/* if channel is in RAW mode or data pkt, notify and return */
19991ae08745Sheppo 		if (ldcp->mode == LDC_MODE_RAW) {
20001ae08745Sheppo 			notify_client = B_TRUE;
20011ae08745Sheppo 			notify_event |= LDC_EVT_READ;
20021ae08745Sheppo 			break;
20031ae08745Sheppo 		}
20041ae08745Sheppo 
20051ae08745Sheppo 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) {
20061ae08745Sheppo 
20071ae08745Sheppo 			/* discard packet if channel is not up */
20083af08d82Slm66018 			if ((ldcp->tstate & ~TS_IN_RESET) != TS_UP) {
20091ae08745Sheppo 
20101ae08745Sheppo 				/* move the head one position */
20111ae08745Sheppo 				rx_head = (rx_head + LDC_PACKET_SIZE) %
20121ae08745Sheppo 				(ldcp->rx_q_entries << LDC_PACKET_SHIFT);
20131ae08745Sheppo 
20141ae08745Sheppo 				if (rv = i_ldc_set_rx_head(ldcp, rx_head))
20151ae08745Sheppo 					break;
20161ae08745Sheppo 
20171ae08745Sheppo 				continue;
20181ae08745Sheppo 			} else {
20193af08d82Slm66018 				if ((ldcp->tstate & TS_IN_RESET) == 0)
20201ae08745Sheppo 					notify_client = B_TRUE;
20211ae08745Sheppo 				notify_event |= LDC_EVT_READ;
20221ae08745Sheppo 				break;
20231ae08745Sheppo 			}
20241ae08745Sheppo 		}
20251ae08745Sheppo 
20261ae08745Sheppo 		/* Check the sequence ID for the message received */
20273af08d82Slm66018 		rv = i_ldc_check_seqid(ldcp, msg);
20283af08d82Slm66018 		if (rv != 0) {
20291ae08745Sheppo 
20301ae08745Sheppo 			DWARN(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) seqid error, "
20311ae08745Sheppo 			    "q_ptrs=0x%lx,0x%lx", ldcp->id, rx_head, rx_tail);
20321ae08745Sheppo 
20331ae08745Sheppo 			/* Reset last_msg_rcd to start of message */
2034d10e4ef2Snarayan 			if (first_fragment != 0) {
2035d10e4ef2Snarayan 				ldcp->last_msg_rcd = first_fragment - 1;
2036d10e4ef2Snarayan 				first_fragment = 0;
20371ae08745Sheppo 			}
2038d10e4ef2Snarayan 
20391ae08745Sheppo 			/*
20401ae08745Sheppo 			 * Send a NACK due to seqid mismatch
20411ae08745Sheppo 			 */
20421ae08745Sheppo 			rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK,
20431ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK));
20441ae08745Sheppo 
20451ae08745Sheppo 			if (rv) {
20461ae08745Sheppo 				cmn_err(CE_NOTE,
20471ae08745Sheppo 				    "i_ldc_rx_hdlr: (0x%lx) err sending "
20481ae08745Sheppo 				    "CTRL/NACK msg\n", ldcp->id);
2049d10e4ef2Snarayan 
2050d10e4ef2Snarayan 				/* if cannot send NACK - reset channel */
2051d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
20523af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
2053d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
2054*83d3bc6fSnarayan 
2055*83d3bc6fSnarayan 				notify_client = B_TRUE;
2056*83d3bc6fSnarayan 				notify_event = LDC_EVT_RESET;
2057d10e4ef2Snarayan 				break;
20581ae08745Sheppo 			}
20591ae08745Sheppo 
20601ae08745Sheppo 			/* purge receive queue */
20611ae08745Sheppo 			(void) i_ldc_set_rx_head(ldcp, rx_tail);
20621ae08745Sheppo 			break;
20631ae08745Sheppo 		}
20641ae08745Sheppo 
20651ae08745Sheppo 		/* record the message ID */
20661ae08745Sheppo 		ldcp->last_msg_rcd = msg->seqid;
20671ae08745Sheppo 
20681ae08745Sheppo 		/* process control messages */
20691ae08745Sheppo 		if (msg->type & LDC_CTRL) {
20701ae08745Sheppo 			/* save current internal state */
20711ae08745Sheppo 			uint64_t tstate = ldcp->tstate;
20721ae08745Sheppo 
20731ae08745Sheppo 			rv = i_ldc_ctrlmsg(ldcp, msg);
20741ae08745Sheppo 			if (rv == EAGAIN) {
20751ae08745Sheppo 				/* re-process pkt - state was adjusted */
20761ae08745Sheppo 				continue;
20771ae08745Sheppo 			}
20781ae08745Sheppo 			if (rv == ECONNRESET) {
20791ae08745Sheppo 				notify_client = B_TRUE;
20801ae08745Sheppo 				notify_event = LDC_EVT_RESET;
20811ae08745Sheppo 				break;
20821ae08745Sheppo 			}
20831ae08745Sheppo 
20841ae08745Sheppo 			/*
20851ae08745Sheppo 			 * control message processing was successful
20861ae08745Sheppo 			 * channel transitioned to ready for communication
20871ae08745Sheppo 			 */
20881ae08745Sheppo 			if (rv == 0 && ldcp->tstate == TS_UP &&
20893af08d82Slm66018 			    (tstate & ~TS_IN_RESET) !=
20903af08d82Slm66018 			    (ldcp->tstate & ~TS_IN_RESET)) {
20911ae08745Sheppo 				notify_client = B_TRUE;
20921ae08745Sheppo 				notify_event = LDC_EVT_UP;
20931ae08745Sheppo 			}
20941ae08745Sheppo 		}
20951ae08745Sheppo 
2096*83d3bc6fSnarayan 		/* process data NACKs */
2097*83d3bc6fSnarayan 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_NACK)) {
2098*83d3bc6fSnarayan 			DWARN(ldcp->id,
2099*83d3bc6fSnarayan 			    "i_ldc_rx_hdlr: (0x%llx) received DATA/NACK",
2100*83d3bc6fSnarayan 			    ldcp->id);
2101*83d3bc6fSnarayan 			mutex_enter(&ldcp->tx_lock);
2102*83d3bc6fSnarayan 			i_ldc_reset(ldcp, B_TRUE);
2103*83d3bc6fSnarayan 			mutex_exit(&ldcp->tx_lock);
2104*83d3bc6fSnarayan 			notify_client = B_TRUE;
2105*83d3bc6fSnarayan 			notify_event = LDC_EVT_RESET;
2106*83d3bc6fSnarayan 			break;
2107*83d3bc6fSnarayan 		}
2108*83d3bc6fSnarayan 
21091ae08745Sheppo 		/* process data ACKs */
21101ae08745Sheppo 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) {
2111d10e4ef2Snarayan 			if (rv = i_ldc_process_data_ACK(ldcp, msg)) {
2112d10e4ef2Snarayan 				notify_client = B_TRUE;
2113d10e4ef2Snarayan 				notify_event = LDC_EVT_RESET;
2114d10e4ef2Snarayan 				break;
2115d10e4ef2Snarayan 			}
21161ae08745Sheppo 		}
21171ae08745Sheppo 
21181ae08745Sheppo 		/* move the head one position */
21191ae08745Sheppo 		rx_head = (rx_head + LDC_PACKET_SIZE) %
21201ae08745Sheppo 			(ldcp->rx_q_entries << LDC_PACKET_SHIFT);
21210a55fbb7Slm66018 		if (rv = i_ldc_set_rx_head(ldcp, rx_head)) {
21220a55fbb7Slm66018 			notify_client = B_TRUE;
21230a55fbb7Slm66018 			notify_event = LDC_EVT_RESET;
21241ae08745Sheppo 			break;
21250a55fbb7Slm66018 		}
21261ae08745Sheppo 
21271ae08745Sheppo 	} /* for */
21281ae08745Sheppo 
21293af08d82Slm66018 loop_exit:
21303af08d82Slm66018 
21311ae08745Sheppo 	/* if callbacks are disabled, do not notify */
21321ae08745Sheppo 	if (!ldcp->cb_enabled)
21331ae08745Sheppo 		notify_client = B_FALSE;
21341ae08745Sheppo 
21353af08d82Slm66018 	/*
21363af08d82Slm66018 	 * If there are data packets in the queue, the ldc_read will
21373af08d82Slm66018 	 * clear interrupts after draining the queue, else clear interrupts
21383af08d82Slm66018 	 */
21393af08d82Slm66018 	if ((notify_event & LDC_EVT_READ) == 0) {
21403af08d82Slm66018 		i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
21413af08d82Slm66018 	} else
21423af08d82Slm66018 		ldcp->rx_intr_state = LDC_INTR_PEND;
21431ae08745Sheppo 
21441ae08745Sheppo 
21451ae08745Sheppo 	if (notify_client) {
21464d39be2bSsg70180 		ldcp->cb_inprogress = B_TRUE;
21474d39be2bSsg70180 		mutex_exit(&ldcp->lock);
21481ae08745Sheppo 		rv = ldcp->cb(notify_event, ldcp->cb_arg);
21491ae08745Sheppo 		if (rv) {
21501ae08745Sheppo 			DWARN(ldcp->id,
21511ae08745Sheppo 			    "i_ldc_rx_hdlr: (0x%llx) callback failure",
21521ae08745Sheppo 			    ldcp->id);
21531ae08745Sheppo 		}
21544d39be2bSsg70180 		mutex_enter(&ldcp->lock);
21554d39be2bSsg70180 		ldcp->cb_inprogress = B_FALSE;
21561ae08745Sheppo 	}
21571ae08745Sheppo 
21584d39be2bSsg70180 	mutex_exit(&ldcp->lock);
21594d39be2bSsg70180 
21601ae08745Sheppo 	D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) exiting handler", ldcp->id);
21611ae08745Sheppo 	return (DDI_INTR_CLAIMED);
21621ae08745Sheppo }
21631ae08745Sheppo 
21641ae08745Sheppo 
21651ae08745Sheppo /* -------------------------------------------------------------------------- */
21661ae08745Sheppo 
21671ae08745Sheppo /*
21681ae08745Sheppo  * LDC API functions
21691ae08745Sheppo  */
21701ae08745Sheppo 
21711ae08745Sheppo /*
21721ae08745Sheppo  * Initialize the channel. Allocate internal structure and memory for
21731ae08745Sheppo  * TX/RX queues, and initialize locks.
21741ae08745Sheppo  */
21751ae08745Sheppo int
21761ae08745Sheppo ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle)
21771ae08745Sheppo {
21781ae08745Sheppo 	ldc_chan_t 	*ldcp;
21791ae08745Sheppo 	int		rv, exit_val;
21801ae08745Sheppo 	uint64_t	ra_base, nentries;
2181e1ebb9ecSlm66018 	uint64_t	qlen;
21821ae08745Sheppo 
21831ae08745Sheppo 	exit_val = EINVAL;	/* guarantee an error if exit on failure */
21841ae08745Sheppo 
21851ae08745Sheppo 	if (attr == NULL) {
21861ae08745Sheppo 		DWARN(id, "ldc_init: (0x%llx) invalid attr\n", id);
21871ae08745Sheppo 		return (EINVAL);
21881ae08745Sheppo 	}
21891ae08745Sheppo 	if (handle == NULL) {
21901ae08745Sheppo 		DWARN(id, "ldc_init: (0x%llx) invalid handle\n", id);
21911ae08745Sheppo 		return (EINVAL);
21921ae08745Sheppo 	}
21931ae08745Sheppo 
21941ae08745Sheppo 	/* check if channel is valid */
21951ae08745Sheppo 	rv = hv_ldc_tx_qinfo(id, &ra_base, &nentries);
21961ae08745Sheppo 	if (rv == H_ECHANNEL) {
21971ae08745Sheppo 		DWARN(id, "ldc_init: (0x%llx) invalid channel id\n", id);
21981ae08745Sheppo 		return (EINVAL);
21991ae08745Sheppo 	}
22001ae08745Sheppo 
22011ae08745Sheppo 	/* check if the channel has already been initialized */
22021ae08745Sheppo 	mutex_enter(&ldcssp->lock);
22031ae08745Sheppo 	ldcp = ldcssp->chan_list;
22041ae08745Sheppo 	while (ldcp != NULL) {
22051ae08745Sheppo 		if (ldcp->id == id) {
22061ae08745Sheppo 			DWARN(id, "ldc_init: (0x%llx) already initialized\n",
22071ae08745Sheppo 			    id);
22081ae08745Sheppo 			mutex_exit(&ldcssp->lock);
22091ae08745Sheppo 			return (EADDRINUSE);
22101ae08745Sheppo 		}
22111ae08745Sheppo 		ldcp = ldcp->next;
22121ae08745Sheppo 	}
22131ae08745Sheppo 	mutex_exit(&ldcssp->lock);
22141ae08745Sheppo 
22151ae08745Sheppo 	ASSERT(ldcp == NULL);
22161ae08745Sheppo 
22171ae08745Sheppo 	*handle = 0;
22181ae08745Sheppo 
22191ae08745Sheppo 	/* Allocate an ldcp structure */
22201ae08745Sheppo 	ldcp = kmem_zalloc(sizeof (ldc_chan_t), KM_SLEEP);
22211ae08745Sheppo 
2222d10e4ef2Snarayan 	/*
2223d10e4ef2Snarayan 	 * Initialize the channel and Tx lock
2224d10e4ef2Snarayan 	 *
2225d10e4ef2Snarayan 	 * The channel 'lock' protects the entire channel and
2226d10e4ef2Snarayan 	 * should be acquired before initializing, resetting,
2227d10e4ef2Snarayan 	 * destroying or reading from a channel.
2228d10e4ef2Snarayan 	 *
2229d10e4ef2Snarayan 	 * The 'tx_lock' should be acquired prior to transmitting
2230d10e4ef2Snarayan 	 * data over the channel. The lock should also be acquired
2231d10e4ef2Snarayan 	 * prior to channel reconfiguration (in order to prevent
2232d10e4ef2Snarayan 	 * concurrent writes).
2233d10e4ef2Snarayan 	 *
2234d10e4ef2Snarayan 	 * ORDERING: When both locks are being acquired, to prevent
2235d10e4ef2Snarayan 	 * deadlocks, the channel lock should be always acquired prior
2236d10e4ef2Snarayan 	 * to the tx_lock.
2237d10e4ef2Snarayan 	 */
22381ae08745Sheppo 	mutex_init(&ldcp->lock, NULL, MUTEX_DRIVER, NULL);
2239d10e4ef2Snarayan 	mutex_init(&ldcp->tx_lock, NULL, MUTEX_DRIVER, NULL);
22401ae08745Sheppo 
22411ae08745Sheppo 	/* Initialize the channel */
22421ae08745Sheppo 	ldcp->id = id;
22431ae08745Sheppo 	ldcp->cb = NULL;
22441ae08745Sheppo 	ldcp->cb_arg = NULL;
22451ae08745Sheppo 	ldcp->cb_inprogress = B_FALSE;
22461ae08745Sheppo 	ldcp->cb_enabled = B_FALSE;
22471ae08745Sheppo 	ldcp->next = NULL;
22481ae08745Sheppo 
22491ae08745Sheppo 	/* Read attributes */
22501ae08745Sheppo 	ldcp->mode = attr->mode;
22511ae08745Sheppo 	ldcp->devclass = attr->devclass;
22521ae08745Sheppo 	ldcp->devinst = attr->instance;
2253e1ebb9ecSlm66018 	ldcp->mtu = (attr->mtu > 0) ? attr->mtu : LDC_DEFAULT_MTU;
22541ae08745Sheppo 
22551ae08745Sheppo 	D1(ldcp->id,
22561ae08745Sheppo 	    "ldc_init: (0x%llx) channel attributes, class=0x%x, "
2257e1ebb9ecSlm66018 	    "instance=0x%llx, mode=%d, mtu=%d\n",
2258e1ebb9ecSlm66018 	    ldcp->id, ldcp->devclass, ldcp->devinst, ldcp->mode, ldcp->mtu);
22591ae08745Sheppo 
22601ae08745Sheppo 	ldcp->next_vidx = 0;
22613af08d82Slm66018 	ldcp->tstate = TS_IN_RESET;
22621ae08745Sheppo 	ldcp->hstate = 0;
22631ae08745Sheppo 	ldcp->last_msg_snt = LDC_INIT_SEQID;
22641ae08745Sheppo 	ldcp->last_ack_rcd = 0;
22651ae08745Sheppo 	ldcp->last_msg_rcd = 0;
22661ae08745Sheppo 
22671ae08745Sheppo 	ldcp->stream_bufferp = NULL;
22681ae08745Sheppo 	ldcp->exp_dring_list = NULL;
22691ae08745Sheppo 	ldcp->imp_dring_list = NULL;
22701ae08745Sheppo 	ldcp->mhdl_list = NULL;
22711ae08745Sheppo 
22723af08d82Slm66018 	ldcp->tx_intr_state = LDC_INTR_NONE;
22733af08d82Slm66018 	ldcp->rx_intr_state = LDC_INTR_NONE;
22743af08d82Slm66018 
22751ae08745Sheppo 	/* Initialize payload size depending on whether channel is reliable */
22761ae08745Sheppo 	switch (ldcp->mode) {
22771ae08745Sheppo 	case LDC_MODE_RAW:
22781ae08745Sheppo 		ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RAW;
22791ae08745Sheppo 		ldcp->read_p = i_ldc_read_raw;
22801ae08745Sheppo 		ldcp->write_p = i_ldc_write_raw;
22811ae08745Sheppo 		break;
22821ae08745Sheppo 	case LDC_MODE_UNRELIABLE:
22831ae08745Sheppo 		ldcp->pkt_payload = LDC_PAYLOAD_SIZE_UNRELIABLE;
22841ae08745Sheppo 		ldcp->read_p = i_ldc_read_packet;
22851ae08745Sheppo 		ldcp->write_p = i_ldc_write_packet;
22861ae08745Sheppo 		break;
22871ae08745Sheppo 	case LDC_MODE_RELIABLE:
22881ae08745Sheppo 		ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE;
22891ae08745Sheppo 		ldcp->read_p = i_ldc_read_packet;
22901ae08745Sheppo 		ldcp->write_p = i_ldc_write_packet;
22911ae08745Sheppo 		break;
22921ae08745Sheppo 	case LDC_MODE_STREAM:
22931ae08745Sheppo 		ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE;
22941ae08745Sheppo 
22951ae08745Sheppo 		ldcp->stream_remains = 0;
22961ae08745Sheppo 		ldcp->stream_offset = 0;
22971ae08745Sheppo 		ldcp->stream_bufferp = kmem_alloc(ldcp->mtu, KM_SLEEP);
22981ae08745Sheppo 		ldcp->read_p = i_ldc_read_stream;
22991ae08745Sheppo 		ldcp->write_p = i_ldc_write_stream;
23001ae08745Sheppo 		break;
23011ae08745Sheppo 	default:
23021ae08745Sheppo 		exit_val = EINVAL;
23031ae08745Sheppo 		goto cleanup_on_exit;
23041ae08745Sheppo 	}
23051ae08745Sheppo 
2306e1ebb9ecSlm66018 	/*
2307e1ebb9ecSlm66018 	 * qlen is (mtu * ldc_mtu_msgs) / pkt_payload. If this
2308e1ebb9ecSlm66018 	 * value is smaller than default length of ldc_queue_entries,
2309e1ebb9ecSlm66018 	 * qlen is set to ldc_queue_entries..
2310e1ebb9ecSlm66018 	 */
2311e1ebb9ecSlm66018 	qlen = (ldcp->mtu * ldc_mtu_msgs) / ldcp->pkt_payload;
2312e1ebb9ecSlm66018 	ldcp->rx_q_entries =
2313e1ebb9ecSlm66018 		(qlen < ldc_queue_entries) ? ldc_queue_entries : qlen;
2314e1ebb9ecSlm66018 	ldcp->tx_q_entries = ldcp->rx_q_entries;
2315e1ebb9ecSlm66018 
2316e1ebb9ecSlm66018 	D1(ldcp->id, "ldc_init: queue length = 0x%llx\n", qlen);
2317e1ebb9ecSlm66018 
23181ae08745Sheppo 	/* Create a transmit queue */
23191ae08745Sheppo 	ldcp->tx_q_va = (uint64_t)
23201ae08745Sheppo 		contig_mem_alloc(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
23211ae08745Sheppo 	if (ldcp->tx_q_va == NULL) {
23221ae08745Sheppo 		cmn_err(CE_WARN,
23231ae08745Sheppo 		    "ldc_init: (0x%lx) TX queue allocation failed\n",
23241ae08745Sheppo 		    ldcp->id);
23251ae08745Sheppo 		exit_val = ENOMEM;
23261ae08745Sheppo 		goto cleanup_on_exit;
23271ae08745Sheppo 	}
23281ae08745Sheppo 	ldcp->tx_q_ra = va_to_pa((caddr_t)ldcp->tx_q_va);
23291ae08745Sheppo 
23301ae08745Sheppo 	D2(ldcp->id, "ldc_init: txq_va=0x%llx, txq_ra=0x%llx, entries=0x%llx\n",
23311ae08745Sheppo 	    ldcp->tx_q_va, ldcp->tx_q_ra, ldcp->tx_q_entries);
23321ae08745Sheppo 
23331ae08745Sheppo 	ldcp->tstate |= TS_TXQ_RDY;
23341ae08745Sheppo 
23351ae08745Sheppo 	/* Create a receive queue */
23361ae08745Sheppo 	ldcp->rx_q_va = (uint64_t)
23371ae08745Sheppo 		contig_mem_alloc(ldcp->rx_q_entries << LDC_PACKET_SHIFT);
23381ae08745Sheppo 	if (ldcp->rx_q_va == NULL) {
23391ae08745Sheppo 		cmn_err(CE_WARN,
23401ae08745Sheppo 		    "ldc_init: (0x%lx) RX queue allocation failed\n",
23411ae08745Sheppo 		    ldcp->id);
23421ae08745Sheppo 		exit_val = ENOMEM;
23431ae08745Sheppo 		goto cleanup_on_exit;
23441ae08745Sheppo 	}
23451ae08745Sheppo 	ldcp->rx_q_ra = va_to_pa((caddr_t)ldcp->rx_q_va);
23461ae08745Sheppo 
23471ae08745Sheppo 	D2(ldcp->id, "ldc_init: rxq_va=0x%llx, rxq_ra=0x%llx, entries=0x%llx\n",
23481ae08745Sheppo 	    ldcp->rx_q_va, ldcp->rx_q_ra, ldcp->rx_q_entries);
23491ae08745Sheppo 
23501ae08745Sheppo 	ldcp->tstate |= TS_RXQ_RDY;
23511ae08745Sheppo 
23521ae08745Sheppo 	/* Init descriptor ring and memory handle list lock */
23531ae08745Sheppo 	mutex_init(&ldcp->exp_dlist_lock, NULL, MUTEX_DRIVER, NULL);
23541ae08745Sheppo 	mutex_init(&ldcp->imp_dlist_lock, NULL, MUTEX_DRIVER, NULL);
23551ae08745Sheppo 	mutex_init(&ldcp->mlist_lock, NULL, MUTEX_DRIVER, NULL);
23561ae08745Sheppo 
23571ae08745Sheppo 	/* mark status as INITialized */
23581ae08745Sheppo 	ldcp->status = LDC_INIT;
23591ae08745Sheppo 
23601ae08745Sheppo 	/* Add to channel list */
23611ae08745Sheppo 	mutex_enter(&ldcssp->lock);
23621ae08745Sheppo 	ldcp->next = ldcssp->chan_list;
23631ae08745Sheppo 	ldcssp->chan_list = ldcp;
23641ae08745Sheppo 	ldcssp->channel_count++;
23651ae08745Sheppo 	mutex_exit(&ldcssp->lock);
23661ae08745Sheppo 
23671ae08745Sheppo 	/* set the handle */
23681ae08745Sheppo 	*handle = (ldc_handle_t)ldcp;
23691ae08745Sheppo 
23701ae08745Sheppo 	D1(ldcp->id, "ldc_init: (0x%llx) channel initialized\n", ldcp->id);
23711ae08745Sheppo 
23721ae08745Sheppo 	return (0);
23731ae08745Sheppo 
23741ae08745Sheppo cleanup_on_exit:
23751ae08745Sheppo 
23761ae08745Sheppo 	if (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_bufferp)
23771ae08745Sheppo 		kmem_free(ldcp->stream_bufferp, ldcp->mtu);
23781ae08745Sheppo 
23791ae08745Sheppo 	if (ldcp->tstate & TS_TXQ_RDY)
23801ae08745Sheppo 		contig_mem_free((caddr_t)ldcp->tx_q_va,
23811ae08745Sheppo 		    (ldcp->tx_q_entries << LDC_PACKET_SHIFT));
23821ae08745Sheppo 
23831ae08745Sheppo 	if (ldcp->tstate & TS_RXQ_RDY)
23841ae08745Sheppo 		contig_mem_free((caddr_t)ldcp->rx_q_va,
23851ae08745Sheppo 		    (ldcp->rx_q_entries << LDC_PACKET_SHIFT));
23861ae08745Sheppo 
2387d10e4ef2Snarayan 	mutex_destroy(&ldcp->tx_lock);
23881ae08745Sheppo 	mutex_destroy(&ldcp->lock);
23891ae08745Sheppo 
23901ae08745Sheppo 	if (ldcp)
23911ae08745Sheppo 		kmem_free(ldcp, sizeof (ldc_chan_t));
23921ae08745Sheppo 
23931ae08745Sheppo 	return (exit_val);
23941ae08745Sheppo }
23951ae08745Sheppo 
23961ae08745Sheppo /*
23971ae08745Sheppo  * Finalizes the LDC connection. It will return EBUSY if the
23981ae08745Sheppo  * channel is open. A ldc_close() has to be done prior to
23991ae08745Sheppo  * a ldc_fini operation. It frees TX/RX queues, associated
24001ae08745Sheppo  * with the channel
24011ae08745Sheppo  */
24021ae08745Sheppo int
24031ae08745Sheppo ldc_fini(ldc_handle_t handle)
24041ae08745Sheppo {
24051ae08745Sheppo 	ldc_chan_t 	*ldcp;
24061ae08745Sheppo 	ldc_chan_t 	*tmp_ldcp;
24071ae08745Sheppo 	uint64_t 	id;
24081ae08745Sheppo 
24091ae08745Sheppo 	if (handle == NULL) {
24101ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel handle\n");
24111ae08745Sheppo 		return (EINVAL);
24121ae08745Sheppo 	}
24131ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
24141ae08745Sheppo 	id = ldcp->id;
24151ae08745Sheppo 
24161ae08745Sheppo 	mutex_enter(&ldcp->lock);
24171ae08745Sheppo 
24183af08d82Slm66018 	if ((ldcp->tstate & ~TS_IN_RESET) > TS_INIT) {
24191ae08745Sheppo 		DWARN(ldcp->id, "ldc_fini: (0x%llx) channel is open\n",
24201ae08745Sheppo 		    ldcp->id);
24211ae08745Sheppo 		mutex_exit(&ldcp->lock);
24221ae08745Sheppo 		return (EBUSY);
24231ae08745Sheppo 	}
24241ae08745Sheppo 
24251ae08745Sheppo 	/* Remove from the channel list */
24261ae08745Sheppo 	mutex_enter(&ldcssp->lock);
24271ae08745Sheppo 	tmp_ldcp = ldcssp->chan_list;
24281ae08745Sheppo 	if (tmp_ldcp == ldcp) {
24291ae08745Sheppo 		ldcssp->chan_list = ldcp->next;
24301ae08745Sheppo 		ldcp->next = NULL;
24311ae08745Sheppo 	} else {
24321ae08745Sheppo 		while (tmp_ldcp != NULL) {
24331ae08745Sheppo 			if (tmp_ldcp->next == ldcp) {
24341ae08745Sheppo 				tmp_ldcp->next = ldcp->next;
24351ae08745Sheppo 				ldcp->next = NULL;
24361ae08745Sheppo 				break;
24371ae08745Sheppo 			}
24381ae08745Sheppo 			tmp_ldcp = tmp_ldcp->next;
24391ae08745Sheppo 		}
24401ae08745Sheppo 		if (tmp_ldcp == NULL) {
24411ae08745Sheppo 			DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel hdl\n");
24421ae08745Sheppo 			mutex_exit(&ldcssp->lock);
24431ae08745Sheppo 			mutex_exit(&ldcp->lock);
24441ae08745Sheppo 			return (EINVAL);
24451ae08745Sheppo 		}
24461ae08745Sheppo 	}
24471ae08745Sheppo 
24481ae08745Sheppo 	ldcssp->channel_count--;
24491ae08745Sheppo 
24501ae08745Sheppo 	mutex_exit(&ldcssp->lock);
24511ae08745Sheppo 
24521ae08745Sheppo 	/* Free the map table for this channel */
24531ae08745Sheppo 	if (ldcp->mtbl) {
24541ae08745Sheppo 		(void) hv_ldc_set_map_table(ldcp->id, NULL, NULL);
24553af08d82Slm66018 		if (ldcp->mtbl->contigmem)
24561ae08745Sheppo 			contig_mem_free(ldcp->mtbl->table, ldcp->mtbl->size);
24573af08d82Slm66018 		else
24583af08d82Slm66018 			kmem_free(ldcp->mtbl->table, ldcp->mtbl->size);
24591ae08745Sheppo 		mutex_destroy(&ldcp->mtbl->lock);
24601ae08745Sheppo 		kmem_free(ldcp->mtbl, sizeof (ldc_mtbl_t));
24611ae08745Sheppo 	}
24621ae08745Sheppo 
24631ae08745Sheppo 	/* Destroy descriptor ring and memory handle list lock */
24641ae08745Sheppo 	mutex_destroy(&ldcp->exp_dlist_lock);
24651ae08745Sheppo 	mutex_destroy(&ldcp->imp_dlist_lock);
24661ae08745Sheppo 	mutex_destroy(&ldcp->mlist_lock);
24671ae08745Sheppo 
24681ae08745Sheppo 	/* Free the stream buffer for STREAM_MODE */
24691ae08745Sheppo 	if (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_bufferp)
24701ae08745Sheppo 		kmem_free(ldcp->stream_bufferp, ldcp->mtu);
24711ae08745Sheppo 
24721ae08745Sheppo 	/* Free the RX queue */
24731ae08745Sheppo 	contig_mem_free((caddr_t)ldcp->rx_q_va,
24741ae08745Sheppo 	    (ldcp->rx_q_entries << LDC_PACKET_SHIFT));
24751ae08745Sheppo 	ldcp->tstate &= ~TS_RXQ_RDY;
24761ae08745Sheppo 
24771ae08745Sheppo 	/* Free the TX queue */
24781ae08745Sheppo 	contig_mem_free((caddr_t)ldcp->tx_q_va,
24791ae08745Sheppo 	    (ldcp->tx_q_entries << LDC_PACKET_SHIFT));
24801ae08745Sheppo 	ldcp->tstate &= ~TS_TXQ_RDY;
24811ae08745Sheppo 
24821ae08745Sheppo 	mutex_exit(&ldcp->lock);
24831ae08745Sheppo 
24841ae08745Sheppo 	/* Destroy mutex */
2485d10e4ef2Snarayan 	mutex_destroy(&ldcp->tx_lock);
24861ae08745Sheppo 	mutex_destroy(&ldcp->lock);
24871ae08745Sheppo 
24881ae08745Sheppo 	/* free channel structure */
24891ae08745Sheppo 	kmem_free(ldcp, sizeof (ldc_chan_t));
24901ae08745Sheppo 
24911ae08745Sheppo 	D1(id, "ldc_fini: (0x%llx) channel finalized\n", id);
24921ae08745Sheppo 
24931ae08745Sheppo 	return (0);
24941ae08745Sheppo }
24951ae08745Sheppo 
24961ae08745Sheppo /*
24971ae08745Sheppo  * Open the LDC channel for use. It registers the TX/RX queues
24981ae08745Sheppo  * with the Hypervisor. It also specifies the interrupt number
24991ae08745Sheppo  * and target CPU for this channel
25001ae08745Sheppo  */
25011ae08745Sheppo int
25021ae08745Sheppo ldc_open(ldc_handle_t handle)
25031ae08745Sheppo {
25041ae08745Sheppo 	ldc_chan_t 	*ldcp;
25051ae08745Sheppo 	int 		rv;
25061ae08745Sheppo 
25071ae08745Sheppo 	if (handle == NULL) {
25081ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_open: invalid channel handle\n");
25091ae08745Sheppo 		return (EINVAL);
25101ae08745Sheppo 	}
25111ae08745Sheppo 
25121ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
25131ae08745Sheppo 
25141ae08745Sheppo 	mutex_enter(&ldcp->lock);
25151ae08745Sheppo 
25161ae08745Sheppo 	if (ldcp->tstate < TS_INIT) {
25171ae08745Sheppo 		DWARN(ldcp->id,
25181ae08745Sheppo 		    "ldc_open: (0x%llx) channel not initialized\n", ldcp->id);
25191ae08745Sheppo 		mutex_exit(&ldcp->lock);
25201ae08745Sheppo 		return (EFAULT);
25211ae08745Sheppo 	}
25223af08d82Slm66018 	if ((ldcp->tstate & ~TS_IN_RESET) >= TS_OPEN) {
25231ae08745Sheppo 		DWARN(ldcp->id,
25241ae08745Sheppo 		    "ldc_open: (0x%llx) channel is already open\n", ldcp->id);
25251ae08745Sheppo 		mutex_exit(&ldcp->lock);
25261ae08745Sheppo 		return (EFAULT);
25271ae08745Sheppo 	}
25281ae08745Sheppo 
25291ae08745Sheppo 	/*
25301ae08745Sheppo 	 * Unregister/Register the tx queue with the hypervisor
25311ae08745Sheppo 	 */
25321ae08745Sheppo 	rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL);
25331ae08745Sheppo 	if (rv) {
25341ae08745Sheppo 		cmn_err(CE_WARN,
25351ae08745Sheppo 		    "ldc_open: (0x%lx) channel tx queue unconf failed\n",
25361ae08745Sheppo 		    ldcp->id);
25371ae08745Sheppo 		mutex_exit(&ldcp->lock);
25381ae08745Sheppo 		return (EIO);
25391ae08745Sheppo 	}
25401ae08745Sheppo 
25411ae08745Sheppo 	rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries);
25421ae08745Sheppo 	if (rv) {
25431ae08745Sheppo 		cmn_err(CE_WARN,
25441ae08745Sheppo 		    "ldc_open: (0x%lx) channel tx queue conf failed\n",
25451ae08745Sheppo 		    ldcp->id);
25461ae08745Sheppo 		mutex_exit(&ldcp->lock);
25471ae08745Sheppo 		return (EIO);
25481ae08745Sheppo 	}
25491ae08745Sheppo 
25501ae08745Sheppo 	D2(ldcp->id, "ldc_open: (0x%llx) registered tx queue with LDC\n",
25511ae08745Sheppo 	    ldcp->id);
25521ae08745Sheppo 
25531ae08745Sheppo 	/*
25541ae08745Sheppo 	 * Unregister/Register the rx queue with the hypervisor
25551ae08745Sheppo 	 */
25561ae08745Sheppo 	rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL);
25571ae08745Sheppo 	if (rv) {
25581ae08745Sheppo 		cmn_err(CE_WARN,
25591ae08745Sheppo 		    "ldc_open: (0x%lx) channel rx queue unconf failed\n",
25601ae08745Sheppo 		    ldcp->id);
25611ae08745Sheppo 		mutex_exit(&ldcp->lock);
25621ae08745Sheppo 		return (EIO);
25631ae08745Sheppo 	}
25641ae08745Sheppo 
25651ae08745Sheppo 	rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, ldcp->rx_q_entries);
25661ae08745Sheppo 	if (rv) {
25671ae08745Sheppo 		cmn_err(CE_WARN,
25681ae08745Sheppo 		    "ldc_open: (0x%lx) channel rx queue conf failed\n",
25691ae08745Sheppo 		    ldcp->id);
25701ae08745Sheppo 		mutex_exit(&ldcp->lock);
25711ae08745Sheppo 		return (EIO);
25721ae08745Sheppo 	}
25731ae08745Sheppo 
25741ae08745Sheppo 	D2(ldcp->id, "ldc_open: (0x%llx) registered rx queue with LDC\n",
25751ae08745Sheppo 	    ldcp->id);
25761ae08745Sheppo 
25771ae08745Sheppo 	ldcp->tstate |= TS_QCONF_RDY;
25781ae08745Sheppo 
25791ae08745Sheppo 	/* Register the channel with the channel nexus */
25801ae08745Sheppo 	rv = i_ldc_register_channel(ldcp);
25811ae08745Sheppo 	if (rv && rv != EAGAIN) {
25821ae08745Sheppo 		cmn_err(CE_WARN,
25831ae08745Sheppo 		    "ldc_open: (0x%lx) channel register failed\n", ldcp->id);
25841ae08745Sheppo 		(void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL);
25851ae08745Sheppo 		(void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL);
25861ae08745Sheppo 		mutex_exit(&ldcp->lock);
25871ae08745Sheppo 		return (EIO);
25881ae08745Sheppo 	}
25891ae08745Sheppo 
25901ae08745Sheppo 	/* mark channel in OPEN state */
25911ae08745Sheppo 	ldcp->status = LDC_OPEN;
25921ae08745Sheppo 
25931ae08745Sheppo 	/* Read channel state */
25941ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
25951ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
25961ae08745Sheppo 	if (rv) {
25971ae08745Sheppo 		cmn_err(CE_WARN,
25981ae08745Sheppo 		    "ldc_open: (0x%lx) cannot read channel state\n",
25991ae08745Sheppo 		    ldcp->id);
26001ae08745Sheppo 		(void) i_ldc_unregister_channel(ldcp);
26011ae08745Sheppo 		(void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL);
26021ae08745Sheppo 		(void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL);
26031ae08745Sheppo 		mutex_exit(&ldcp->lock);
26041ae08745Sheppo 		return (EIO);
26051ae08745Sheppo 	}
26061ae08745Sheppo 
26071ae08745Sheppo 	/*
26081ae08745Sheppo 	 * set the ACKd head to current head location for reliable &
26091ae08745Sheppo 	 * streaming mode
26101ae08745Sheppo 	 */
26111ae08745Sheppo 	ldcp->tx_ackd_head = ldcp->tx_head;
26121ae08745Sheppo 
26131ae08745Sheppo 	/* mark channel ready if HV report link is UP (peer alloc'd Rx queue) */
26141ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_UP ||
26151ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
26161ae08745Sheppo 		ldcp->tstate |= TS_LINK_READY;
26171ae08745Sheppo 		ldcp->status = LDC_READY;
26181ae08745Sheppo 	}
26191ae08745Sheppo 
26201ae08745Sheppo 	/*
26211ae08745Sheppo 	 * if channel is being opened in RAW mode - no handshake is needed
26221ae08745Sheppo 	 * switch the channel READY and UP state
26231ae08745Sheppo 	 */
26241ae08745Sheppo 	if (ldcp->mode == LDC_MODE_RAW) {
26251ae08745Sheppo 		ldcp->tstate = TS_UP;	/* set bits associated with LDC UP */
26261ae08745Sheppo 		ldcp->status = LDC_UP;
26271ae08745Sheppo 	}
26281ae08745Sheppo 
26291ae08745Sheppo 	mutex_exit(&ldcp->lock);
26301ae08745Sheppo 
26311ae08745Sheppo 	/*
26321ae08745Sheppo 	 * Increment number of open channels
26331ae08745Sheppo 	 */
26341ae08745Sheppo 	mutex_enter(&ldcssp->lock);
26351ae08745Sheppo 	ldcssp->channels_open++;
26361ae08745Sheppo 	mutex_exit(&ldcssp->lock);
26371ae08745Sheppo 
2638cb112a14Slm66018 	D1(ldcp->id,
26393af08d82Slm66018 	    "ldc_open: (0x%llx) channel (0x%p) open for use "
26403af08d82Slm66018 	    "(tstate=0x%x, status=0x%x)\n",
26413af08d82Slm66018 	    ldcp->id, ldcp, ldcp->tstate, ldcp->status);
26421ae08745Sheppo 
26431ae08745Sheppo 	return (0);
26441ae08745Sheppo }
26451ae08745Sheppo 
26461ae08745Sheppo /*
26471ae08745Sheppo  * Close the LDC connection. It will return EBUSY if there
26481ae08745Sheppo  * are memory segments or descriptor rings either bound to or
26491ae08745Sheppo  * mapped over the channel
26501ae08745Sheppo  */
26511ae08745Sheppo int
26521ae08745Sheppo ldc_close(ldc_handle_t handle)
26531ae08745Sheppo {
26541ae08745Sheppo 	ldc_chan_t 	*ldcp;
2655d10e4ef2Snarayan 	int		rv = 0, retries = 0;
26561ae08745Sheppo 	boolean_t	chk_done = B_FALSE;
26571ae08745Sheppo 
26581ae08745Sheppo 	if (handle == NULL) {
26591ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_close: invalid channel handle\n");
26601ae08745Sheppo 		return (EINVAL);
26611ae08745Sheppo 	}
26621ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
26631ae08745Sheppo 
26641ae08745Sheppo 	mutex_enter(&ldcp->lock);
26651ae08745Sheppo 
26661ae08745Sheppo 	/* return error if channel is not open */
26673af08d82Slm66018 	if ((ldcp->tstate & ~TS_IN_RESET) < TS_OPEN) {
26681ae08745Sheppo 		DWARN(ldcp->id,
26691ae08745Sheppo 		    "ldc_close: (0x%llx) channel is not open\n", ldcp->id);
26701ae08745Sheppo 		mutex_exit(&ldcp->lock);
26711ae08745Sheppo 		return (EFAULT);
26721ae08745Sheppo 	}
26731ae08745Sheppo 
26741ae08745Sheppo 	/* if any memory handles, drings, are bound or mapped cannot close */
26751ae08745Sheppo 	if (ldcp->mhdl_list != NULL) {
26761ae08745Sheppo 		DWARN(ldcp->id,
26771ae08745Sheppo 		    "ldc_close: (0x%llx) channel has bound memory handles\n",
26781ae08745Sheppo 		    ldcp->id);
26791ae08745Sheppo 		mutex_exit(&ldcp->lock);
26801ae08745Sheppo 		return (EBUSY);
26811ae08745Sheppo 	}
26821ae08745Sheppo 	if (ldcp->exp_dring_list != NULL) {
26831ae08745Sheppo 		DWARN(ldcp->id,
26841ae08745Sheppo 		    "ldc_close: (0x%llx) channel has bound descriptor rings\n",
26851ae08745Sheppo 		    ldcp->id);
26861ae08745Sheppo 		mutex_exit(&ldcp->lock);
26871ae08745Sheppo 		return (EBUSY);
26881ae08745Sheppo 	}
26891ae08745Sheppo 	if (ldcp->imp_dring_list != NULL) {
26901ae08745Sheppo 		DWARN(ldcp->id,
26911ae08745Sheppo 		    "ldc_close: (0x%llx) channel has mapped descriptor rings\n",
26921ae08745Sheppo 		    ldcp->id);
26931ae08745Sheppo 		mutex_exit(&ldcp->lock);
26941ae08745Sheppo 		return (EBUSY);
26951ae08745Sheppo 	}
26961ae08745Sheppo 
26974d39be2bSsg70180 	if (ldcp->cb_inprogress) {
26984d39be2bSsg70180 		DWARN(ldcp->id, "ldc_close: (0x%llx) callback active\n",
26994d39be2bSsg70180 		    ldcp->id);
27004d39be2bSsg70180 		mutex_exit(&ldcp->lock);
27014d39be2bSsg70180 		return (EWOULDBLOCK);
27024d39be2bSsg70180 	}
27034d39be2bSsg70180 
2704d10e4ef2Snarayan 	/* Obtain Tx lock */
2705d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
2706d10e4ef2Snarayan 
27071ae08745Sheppo 	/*
27081ae08745Sheppo 	 * Wait for pending transmits to complete i.e Tx queue to drain
27091ae08745Sheppo 	 * if there are pending pkts - wait 1 ms and retry again
27101ae08745Sheppo 	 */
27111ae08745Sheppo 	for (;;) {
27121ae08745Sheppo 
27131ae08745Sheppo 		rv = hv_ldc_tx_get_state(ldcp->id,
27141ae08745Sheppo 		    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
27151ae08745Sheppo 		if (rv) {
27161ae08745Sheppo 			cmn_err(CE_WARN,
27171ae08745Sheppo 			    "ldc_close: (0x%lx) cannot read qptrs\n", ldcp->id);
2718d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
27191ae08745Sheppo 			mutex_exit(&ldcp->lock);
27201ae08745Sheppo 			return (EIO);
27211ae08745Sheppo 		}
27221ae08745Sheppo 
27231ae08745Sheppo 		if (ldcp->tx_head == ldcp->tx_tail ||
27241ae08745Sheppo 		    ldcp->link_state != LDC_CHANNEL_UP) {
27251ae08745Sheppo 			break;
27261ae08745Sheppo 		}
27271ae08745Sheppo 
27281ae08745Sheppo 		if (chk_done) {
27291ae08745Sheppo 			DWARN(ldcp->id,
27301ae08745Sheppo 			    "ldc_close: (0x%llx) Tx queue drain timeout\n",
27311ae08745Sheppo 			    ldcp->id);
27321ae08745Sheppo 			break;
27331ae08745Sheppo 		}
27341ae08745Sheppo 
27351ae08745Sheppo 		/* wait for one ms and try again */
27361ae08745Sheppo 		delay(drv_usectohz(1000));
27371ae08745Sheppo 		chk_done = B_TRUE;
27381ae08745Sheppo 	}
27391ae08745Sheppo 
27401ae08745Sheppo 	/*
2741a8ea4edeSnarayan 	 * Drain the Tx and Rx queues as we are closing the
2742a8ea4edeSnarayan 	 * channel. We dont care about any pending packets.
2743a8ea4edeSnarayan 	 * We have to also drain the queue prior to clearing
2744a8ea4edeSnarayan 	 * pending interrupts, otherwise the HV will trigger
2745a8ea4edeSnarayan 	 * an interrupt the moment the interrupt state is
2746a8ea4edeSnarayan 	 * cleared.
27473af08d82Slm66018 	 */
27483af08d82Slm66018 	(void) i_ldc_txq_reconf(ldcp);
2749a8ea4edeSnarayan 	(void) i_ldc_rxq_drain(ldcp);
27503af08d82Slm66018 
27513af08d82Slm66018 	/*
27521ae08745Sheppo 	 * Unregister the channel with the nexus
27531ae08745Sheppo 	 */
2754d10e4ef2Snarayan 	while ((rv = i_ldc_unregister_channel(ldcp)) != 0) {
2755d10e4ef2Snarayan 
2756d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
27571ae08745Sheppo 		mutex_exit(&ldcp->lock);
2758d10e4ef2Snarayan 
2759d10e4ef2Snarayan 		/* if any error other than EAGAIN return back */
2760a8ea4edeSnarayan 		if (rv != EAGAIN || retries >= ldc_max_retries) {
2761d10e4ef2Snarayan 			cmn_err(CE_WARN,
2762d10e4ef2Snarayan 			    "ldc_close: (0x%lx) unregister failed, %d\n",
2763d10e4ef2Snarayan 			    ldcp->id, rv);
27641ae08745Sheppo 			return (rv);
27651ae08745Sheppo 		}
27661ae08745Sheppo 
27671ae08745Sheppo 		/*
2768d10e4ef2Snarayan 		 * As there could be pending interrupts we need
2769d10e4ef2Snarayan 		 * to wait and try again
2770d10e4ef2Snarayan 		 */
27714d39be2bSsg70180 		drv_usecwait(ldc_close_delay);
2772d10e4ef2Snarayan 		mutex_enter(&ldcp->lock);
2773d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
2774d10e4ef2Snarayan 		retries++;
2775d10e4ef2Snarayan 	}
2776d10e4ef2Snarayan 
2777d10e4ef2Snarayan 	/*
27781ae08745Sheppo 	 * Unregister queues
27791ae08745Sheppo 	 */
27801ae08745Sheppo 	rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL);
27811ae08745Sheppo 	if (rv) {
27821ae08745Sheppo 		cmn_err(CE_WARN,
27831ae08745Sheppo 		    "ldc_close: (0x%lx) channel TX queue unconf failed\n",
27841ae08745Sheppo 		    ldcp->id);
2785d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
27861ae08745Sheppo 		mutex_exit(&ldcp->lock);
27871ae08745Sheppo 		return (EIO);
27881ae08745Sheppo 	}
27891ae08745Sheppo 	rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL);
27901ae08745Sheppo 	if (rv) {
27911ae08745Sheppo 		cmn_err(CE_WARN,
27921ae08745Sheppo 		    "ldc_close: (0x%lx) channel RX queue unconf failed\n",
27931ae08745Sheppo 		    ldcp->id);
2794d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
27951ae08745Sheppo 		mutex_exit(&ldcp->lock);
27961ae08745Sheppo 		return (EIO);
27971ae08745Sheppo 	}
27981ae08745Sheppo 
27991ae08745Sheppo 	ldcp->tstate &= ~TS_QCONF_RDY;
28001ae08745Sheppo 
28011ae08745Sheppo 	/* Reset channel state information */
28021ae08745Sheppo 	i_ldc_reset_state(ldcp);
28031ae08745Sheppo 
28041ae08745Sheppo 	/* Mark channel as down and in initialized state */
28051ae08745Sheppo 	ldcp->tx_ackd_head = 0;
28061ae08745Sheppo 	ldcp->tx_head = 0;
28073af08d82Slm66018 	ldcp->tstate = TS_IN_RESET|TS_INIT;
28081ae08745Sheppo 	ldcp->status = LDC_INIT;
28091ae08745Sheppo 
2810d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
28111ae08745Sheppo 	mutex_exit(&ldcp->lock);
28121ae08745Sheppo 
28131ae08745Sheppo 	/* Decrement number of open channels */
28141ae08745Sheppo 	mutex_enter(&ldcssp->lock);
28151ae08745Sheppo 	ldcssp->channels_open--;
28161ae08745Sheppo 	mutex_exit(&ldcssp->lock);
28171ae08745Sheppo 
28181ae08745Sheppo 	D1(ldcp->id, "ldc_close: (0x%llx) channel closed\n", ldcp->id);
28191ae08745Sheppo 
28201ae08745Sheppo 	return (0);
28211ae08745Sheppo }
28221ae08745Sheppo 
28231ae08745Sheppo /*
28241ae08745Sheppo  * Register channel callback
28251ae08745Sheppo  */
28261ae08745Sheppo int
28271ae08745Sheppo ldc_reg_callback(ldc_handle_t handle,
28281ae08745Sheppo     uint_t(*cb)(uint64_t event, caddr_t arg), caddr_t arg)
28291ae08745Sheppo {
28301ae08745Sheppo 	ldc_chan_t *ldcp;
28311ae08745Sheppo 
28321ae08745Sheppo 	if (handle == NULL) {
28331ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
28341ae08745Sheppo 		    "ldc_reg_callback: invalid channel handle\n");
28351ae08745Sheppo 		return (EINVAL);
28361ae08745Sheppo 	}
28371ae08745Sheppo 	if (((uint64_t)cb) < KERNELBASE) {
28381ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_reg_callback: invalid callback\n");
28391ae08745Sheppo 		return (EINVAL);
28401ae08745Sheppo 	}
28411ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
28421ae08745Sheppo 
28431ae08745Sheppo 	mutex_enter(&ldcp->lock);
28441ae08745Sheppo 
28451ae08745Sheppo 	if (ldcp->cb) {
28461ae08745Sheppo 		DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback exists\n",
28471ae08745Sheppo 		    ldcp->id);
28481ae08745Sheppo 		mutex_exit(&ldcp->lock);
28491ae08745Sheppo 		return (EIO);
28501ae08745Sheppo 	}
28511ae08745Sheppo 	if (ldcp->cb_inprogress) {
28521ae08745Sheppo 		DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback active\n",
28531ae08745Sheppo 		    ldcp->id);
28541ae08745Sheppo 		mutex_exit(&ldcp->lock);
28551ae08745Sheppo 		return (EWOULDBLOCK);
28561ae08745Sheppo 	}
28571ae08745Sheppo 
28581ae08745Sheppo 	ldcp->cb = cb;
28591ae08745Sheppo 	ldcp->cb_arg = arg;
28601ae08745Sheppo 	ldcp->cb_enabled = B_TRUE;
28611ae08745Sheppo 
28621ae08745Sheppo 	D1(ldcp->id,
28631ae08745Sheppo 	    "ldc_reg_callback: (0x%llx) registered callback for channel\n",
28641ae08745Sheppo 	    ldcp->id);
28651ae08745Sheppo 
28661ae08745Sheppo 	mutex_exit(&ldcp->lock);
28671ae08745Sheppo 
28681ae08745Sheppo 	return (0);
28691ae08745Sheppo }
28701ae08745Sheppo 
28711ae08745Sheppo /*
28721ae08745Sheppo  * Unregister channel callback
28731ae08745Sheppo  */
28741ae08745Sheppo int
28751ae08745Sheppo ldc_unreg_callback(ldc_handle_t handle)
28761ae08745Sheppo {
28771ae08745Sheppo 	ldc_chan_t *ldcp;
28781ae08745Sheppo 
28791ae08745Sheppo 	if (handle == NULL) {
28801ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
28811ae08745Sheppo 		    "ldc_unreg_callback: invalid channel handle\n");
28821ae08745Sheppo 		return (EINVAL);
28831ae08745Sheppo 	}
28841ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
28851ae08745Sheppo 
28861ae08745Sheppo 	mutex_enter(&ldcp->lock);
28871ae08745Sheppo 
28881ae08745Sheppo 	if (ldcp->cb == NULL) {
28891ae08745Sheppo 		DWARN(ldcp->id,
28901ae08745Sheppo 		    "ldc_unreg_callback: (0x%llx) no callback exists\n",
28911ae08745Sheppo 		    ldcp->id);
28921ae08745Sheppo 		mutex_exit(&ldcp->lock);
28931ae08745Sheppo 		return (EIO);
28941ae08745Sheppo 	}
28951ae08745Sheppo 	if (ldcp->cb_inprogress) {
28961ae08745Sheppo 		DWARN(ldcp->id,
28971ae08745Sheppo 		    "ldc_unreg_callback: (0x%llx) callback active\n",
28981ae08745Sheppo 		    ldcp->id);
28991ae08745Sheppo 		mutex_exit(&ldcp->lock);
29001ae08745Sheppo 		return (EWOULDBLOCK);
29011ae08745Sheppo 	}
29021ae08745Sheppo 
29031ae08745Sheppo 	ldcp->cb = NULL;
29041ae08745Sheppo 	ldcp->cb_arg = NULL;
29051ae08745Sheppo 	ldcp->cb_enabled = B_FALSE;
29061ae08745Sheppo 
29071ae08745Sheppo 	D1(ldcp->id,
29081ae08745Sheppo 	    "ldc_unreg_callback: (0x%llx) unregistered callback for channel\n",
29091ae08745Sheppo 	    ldcp->id);
29101ae08745Sheppo 
29111ae08745Sheppo 	mutex_exit(&ldcp->lock);
29121ae08745Sheppo 
29131ae08745Sheppo 	return (0);
29141ae08745Sheppo }
29151ae08745Sheppo 
29161ae08745Sheppo 
29171ae08745Sheppo /*
29181ae08745Sheppo  * Bring a channel up by initiating a handshake with the peer
29191ae08745Sheppo  * This call is asynchronous. It will complete at a later point
29201ae08745Sheppo  * in time when the peer responds back with an RTR.
29211ae08745Sheppo  */
29221ae08745Sheppo int
29231ae08745Sheppo ldc_up(ldc_handle_t handle)
29241ae08745Sheppo {
29251ae08745Sheppo 	int 		rv;
29261ae08745Sheppo 	ldc_chan_t 	*ldcp;
29271ae08745Sheppo 	ldc_msg_t 	*ldcmsg;
29283af08d82Slm66018 	uint64_t 	tx_tail, tstate;
29291ae08745Sheppo 
29301ae08745Sheppo 	if (handle == NULL) {
29311ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_up: invalid channel handle\n");
29321ae08745Sheppo 		return (EINVAL);
29331ae08745Sheppo 	}
29341ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
29351ae08745Sheppo 
29361ae08745Sheppo 	mutex_enter(&ldcp->lock);
29371ae08745Sheppo 
29383af08d82Slm66018 	D1(ldcp->id, "ldc_up: (0x%llx) doing channel UP\n", ldcp->id);
29393af08d82Slm66018 
29403af08d82Slm66018 	/* clear the reset state */
29413af08d82Slm66018 	tstate = ldcp->tstate;
29423af08d82Slm66018 	ldcp->tstate &= ~TS_IN_RESET;
29433af08d82Slm66018 
29441ae08745Sheppo 	if (ldcp->tstate == TS_UP) {
29453af08d82Slm66018 		DWARN(ldcp->id,
29461ae08745Sheppo 		    "ldc_up: (0x%llx) channel is already in UP state\n",
29471ae08745Sheppo 		    ldcp->id);
29483af08d82Slm66018 
29493af08d82Slm66018 		/* mark channel as up */
29503af08d82Slm66018 		ldcp->status = LDC_UP;
29513af08d82Slm66018 
29523af08d82Slm66018 		/*
29533af08d82Slm66018 		 * if channel was in reset state and there was
29543af08d82Slm66018 		 * pending data clear interrupt state. this will
29553af08d82Slm66018 		 * trigger an interrupt, causing the RX handler to
29563af08d82Slm66018 		 * to invoke the client's callback
29573af08d82Slm66018 		 */
29583af08d82Slm66018 		if ((tstate & TS_IN_RESET) &&
29593af08d82Slm66018 		    ldcp->rx_intr_state == LDC_INTR_PEND) {
2960cb112a14Slm66018 			D1(ldcp->id,
29613af08d82Slm66018 			    "ldc_up: (0x%llx) channel has pending data, "
29623af08d82Slm66018 			    "clearing interrupt\n", ldcp->id);
29633af08d82Slm66018 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
29643af08d82Slm66018 		}
29653af08d82Slm66018 
29661ae08745Sheppo 		mutex_exit(&ldcp->lock);
29671ae08745Sheppo 		return (0);
29681ae08745Sheppo 	}
29691ae08745Sheppo 
29701ae08745Sheppo 	/* if the channel is in RAW mode - mark it as UP, if READY */
29711ae08745Sheppo 	if (ldcp->mode == LDC_MODE_RAW && ldcp->tstate >= TS_READY) {
29721ae08745Sheppo 		ldcp->tstate = TS_UP;
29731ae08745Sheppo 		mutex_exit(&ldcp->lock);
29741ae08745Sheppo 		return (0);
29751ae08745Sheppo 	}
29761ae08745Sheppo 
29771ae08745Sheppo 	/* Don't start another handshake if there is one in progress */
29781ae08745Sheppo 	if (ldcp->hstate) {
29793af08d82Slm66018 		D1(ldcp->id,
29801ae08745Sheppo 		    "ldc_up: (0x%llx) channel handshake in progress\n",
29811ae08745Sheppo 		    ldcp->id);
29821ae08745Sheppo 		mutex_exit(&ldcp->lock);
29831ae08745Sheppo 		return (0);
29841ae08745Sheppo 	}
29851ae08745Sheppo 
2986d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
2987d10e4ef2Snarayan 
29881ae08745Sheppo 	/* get the current tail for the LDC msg */
29891ae08745Sheppo 	rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
29901ae08745Sheppo 	if (rv) {
2991cb112a14Slm66018 		D1(ldcp->id, "ldc_up: (0x%llx) cannot initiate handshake\n",
29921ae08745Sheppo 		    ldcp->id);
2993d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
29941ae08745Sheppo 		mutex_exit(&ldcp->lock);
29951ae08745Sheppo 		return (ECONNREFUSED);
29961ae08745Sheppo 	}
29971ae08745Sheppo 
29981ae08745Sheppo 	ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
29991ae08745Sheppo 	ZERO_PKT(ldcmsg);
30001ae08745Sheppo 
30011ae08745Sheppo 	ldcmsg->type = LDC_CTRL;
30021ae08745Sheppo 	ldcmsg->stype = LDC_INFO;
30031ae08745Sheppo 	ldcmsg->ctrl = LDC_VER;
30041ae08745Sheppo 	ldcp->next_vidx = 0;
30051ae08745Sheppo 	bcopy(&ldc_versions[0], ldcmsg->udata, sizeof (ldc_versions[0]));
30061ae08745Sheppo 
30071ae08745Sheppo 	DUMP_LDC_PKT(ldcp, "ldc_up snd ver", (uint64_t)ldcmsg);
30081ae08745Sheppo 
30091ae08745Sheppo 	/* initiate the send by calling into HV and set the new tail */
30101ae08745Sheppo 	tx_tail = (tx_tail + LDC_PACKET_SIZE) %
30111ae08745Sheppo 		(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
30121ae08745Sheppo 
30131ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
30141ae08745Sheppo 	if (rv) {
30151ae08745Sheppo 		DWARN(ldcp->id,
30161ae08745Sheppo 		    "ldc_up: (0x%llx) cannot initiate handshake rv=%d\n",
30171ae08745Sheppo 		    ldcp->id, rv);
3018d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
30191ae08745Sheppo 		mutex_exit(&ldcp->lock);
30201ae08745Sheppo 		return (rv);
30211ae08745Sheppo 	}
30221ae08745Sheppo 
30230a55fbb7Slm66018 	ldcp->hstate |= TS_SENT_VER;
30241ae08745Sheppo 	ldcp->tx_tail = tx_tail;
30251ae08745Sheppo 	D1(ldcp->id, "ldc_up: (0x%llx) channel up initiated\n", ldcp->id);
30261ae08745Sheppo 
3027d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
30281ae08745Sheppo 	mutex_exit(&ldcp->lock);
30291ae08745Sheppo 
30301ae08745Sheppo 	return (rv);
30311ae08745Sheppo }
30321ae08745Sheppo 
30331ae08745Sheppo 
30341ae08745Sheppo /*
3035e1ebb9ecSlm66018  * Bring a channel down by resetting its state and queues
30361ae08745Sheppo  */
30371ae08745Sheppo int
3038e1ebb9ecSlm66018 ldc_down(ldc_handle_t handle)
30391ae08745Sheppo {
30401ae08745Sheppo 	ldc_chan_t 	*ldcp;
30411ae08745Sheppo 
30421ae08745Sheppo 	if (handle == NULL) {
3043e1ebb9ecSlm66018 		DWARN(DBG_ALL_LDCS, "ldc_down: invalid channel handle\n");
30441ae08745Sheppo 		return (EINVAL);
30451ae08745Sheppo 	}
30461ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
30471ae08745Sheppo 	mutex_enter(&ldcp->lock);
3048d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
30493af08d82Slm66018 	i_ldc_reset(ldcp, B_TRUE);
3050d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
30511ae08745Sheppo 	mutex_exit(&ldcp->lock);
30521ae08745Sheppo 
30531ae08745Sheppo 	return (0);
30541ae08745Sheppo }
30551ae08745Sheppo 
30561ae08745Sheppo /*
30571ae08745Sheppo  * Get the current channel status
30581ae08745Sheppo  */
30591ae08745Sheppo int
30601ae08745Sheppo ldc_status(ldc_handle_t handle, ldc_status_t *status)
30611ae08745Sheppo {
30621ae08745Sheppo 	ldc_chan_t *ldcp;
30631ae08745Sheppo 
30641ae08745Sheppo 	if (handle == NULL || status == NULL) {
30651ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_status: invalid argument\n");
30661ae08745Sheppo 		return (EINVAL);
30671ae08745Sheppo 	}
30681ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
30691ae08745Sheppo 
30701ae08745Sheppo 	*status = ((ldc_chan_t *)handle)->status;
30711ae08745Sheppo 
3072cb112a14Slm66018 	D1(ldcp->id,
30731ae08745Sheppo 	    "ldc_status: (0x%llx) returned status %d\n", ldcp->id, *status);
30741ae08745Sheppo 	return (0);
30751ae08745Sheppo }
30761ae08745Sheppo 
30771ae08745Sheppo 
30781ae08745Sheppo /*
30791ae08745Sheppo  * Set the channel's callback mode - enable/disable callbacks
30801ae08745Sheppo  */
30811ae08745Sheppo int
30821ae08745Sheppo ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t cmode)
30831ae08745Sheppo {
30841ae08745Sheppo 	ldc_chan_t 	*ldcp;
30851ae08745Sheppo 
30861ae08745Sheppo 	if (handle == NULL) {
30871ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
30881ae08745Sheppo 		    "ldc_set_intr_mode: invalid channel handle\n");
30891ae08745Sheppo 		return (EINVAL);
30901ae08745Sheppo 	}
30911ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
30921ae08745Sheppo 
30931ae08745Sheppo 	/*
30941ae08745Sheppo 	 * Record no callbacks should be invoked
30951ae08745Sheppo 	 */
30961ae08745Sheppo 	mutex_enter(&ldcp->lock);
30971ae08745Sheppo 
30981ae08745Sheppo 	switch (cmode) {
30991ae08745Sheppo 	case LDC_CB_DISABLE:
31001ae08745Sheppo 		if (!ldcp->cb_enabled) {
31011ae08745Sheppo 			DWARN(ldcp->id,
31021ae08745Sheppo 			    "ldc_set_cb_mode: (0x%llx) callbacks disabled\n",
31031ae08745Sheppo 			    ldcp->id);
31041ae08745Sheppo 			break;
31051ae08745Sheppo 		}
31061ae08745Sheppo 		ldcp->cb_enabled = B_FALSE;
31071ae08745Sheppo 
31081ae08745Sheppo 		D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) disabled callbacks\n",
31091ae08745Sheppo 		    ldcp->id);
31101ae08745Sheppo 		break;
31111ae08745Sheppo 
31121ae08745Sheppo 	case LDC_CB_ENABLE:
31131ae08745Sheppo 		if (ldcp->cb_enabled) {
31141ae08745Sheppo 			DWARN(ldcp->id,
31151ae08745Sheppo 			    "ldc_set_cb_mode: (0x%llx) callbacks enabled\n",
31161ae08745Sheppo 			    ldcp->id);
31171ae08745Sheppo 			break;
31181ae08745Sheppo 		}
31191ae08745Sheppo 		ldcp->cb_enabled = B_TRUE;
31201ae08745Sheppo 
31211ae08745Sheppo 		D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) enabled callbacks\n",
31221ae08745Sheppo 		    ldcp->id);
31231ae08745Sheppo 		break;
31241ae08745Sheppo 	}
31251ae08745Sheppo 
31261ae08745Sheppo 	mutex_exit(&ldcp->lock);
31271ae08745Sheppo 
31281ae08745Sheppo 	return (0);
31291ae08745Sheppo }
31301ae08745Sheppo 
31311ae08745Sheppo /*
31321ae08745Sheppo  * Check to see if there are packets on the incoming queue
3133e1ebb9ecSlm66018  * Will return hasdata = B_FALSE if there are no packets
31341ae08745Sheppo  */
31351ae08745Sheppo int
3136e1ebb9ecSlm66018 ldc_chkq(ldc_handle_t handle, boolean_t *hasdata)
31371ae08745Sheppo {
31381ae08745Sheppo 	int 		rv;
31391ae08745Sheppo 	uint64_t 	rx_head, rx_tail;
31401ae08745Sheppo 	ldc_chan_t 	*ldcp;
31411ae08745Sheppo 
31421ae08745Sheppo 	if (handle == NULL) {
31431ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_chkq: invalid channel handle\n");
31441ae08745Sheppo 		return (EINVAL);
31451ae08745Sheppo 	}
31461ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
31471ae08745Sheppo 
3148e1ebb9ecSlm66018 	*hasdata = B_FALSE;
31491ae08745Sheppo 
31501ae08745Sheppo 	mutex_enter(&ldcp->lock);
31511ae08745Sheppo 
31521ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
31531ae08745Sheppo 		D1(ldcp->id,
31541ae08745Sheppo 		    "ldc_chkq: (0x%llx) channel is not up\n", ldcp->id);
31551ae08745Sheppo 		mutex_exit(&ldcp->lock);
31561ae08745Sheppo 		return (ECONNRESET);
31571ae08745Sheppo 	}
31581ae08745Sheppo 
31591ae08745Sheppo 	/* Read packet(s) from the queue */
31601ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
31611ae08745Sheppo 	    &ldcp->link_state);
31621ae08745Sheppo 	if (rv != 0) {
31631ae08745Sheppo 		cmn_err(CE_WARN,
31641ae08745Sheppo 		    "ldc_chkq: (0x%lx) unable to read queue ptrs", ldcp->id);
31651ae08745Sheppo 		mutex_exit(&ldcp->lock);
31661ae08745Sheppo 		return (EIO);
31671ae08745Sheppo 	}
31681ae08745Sheppo 	/* reset the channel state if the channel went down */
31691ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_DOWN ||
31701ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
3171d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
31723af08d82Slm66018 		i_ldc_reset(ldcp, B_FALSE);
3173d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
31741ae08745Sheppo 		mutex_exit(&ldcp->lock);
31751ae08745Sheppo 		return (ECONNRESET);
31761ae08745Sheppo 	}
31771ae08745Sheppo 
31784bac2208Snarayan 	if ((rx_head != rx_tail) ||
31794bac2208Snarayan 	    (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_remains > 0)) {
31804bac2208Snarayan 		D1(ldcp->id,
31814bac2208Snarayan 		    "ldc_chkq: (0x%llx) queue has pkt(s) or buffered data\n",
31824bac2208Snarayan 		    ldcp->id);
3183e1ebb9ecSlm66018 		*hasdata = B_TRUE;
31841ae08745Sheppo 	}
31851ae08745Sheppo 
31861ae08745Sheppo 	mutex_exit(&ldcp->lock);
31871ae08745Sheppo 
31881ae08745Sheppo 	return (0);
31891ae08745Sheppo }
31901ae08745Sheppo 
31911ae08745Sheppo 
31921ae08745Sheppo /*
31931ae08745Sheppo  * Read 'size' amount of bytes or less. If incoming buffer
31941ae08745Sheppo  * is more than 'size', ENOBUFS is returned.
31951ae08745Sheppo  *
31961ae08745Sheppo  * On return, size contains the number of bytes read.
31971ae08745Sheppo  */
31981ae08745Sheppo int
31991ae08745Sheppo ldc_read(ldc_handle_t handle, caddr_t bufp, size_t *sizep)
32001ae08745Sheppo {
32011ae08745Sheppo 	ldc_chan_t 	*ldcp;
32021ae08745Sheppo 	uint64_t 	rx_head = 0, rx_tail = 0;
32031ae08745Sheppo 	int		rv = 0, exit_val;
32041ae08745Sheppo 
32051ae08745Sheppo 	if (handle == NULL) {
32061ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_read: invalid channel handle\n");
32071ae08745Sheppo 		return (EINVAL);
32081ae08745Sheppo 	}
32091ae08745Sheppo 
32101ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
32111ae08745Sheppo 
32121ae08745Sheppo 	/* channel lock */
32131ae08745Sheppo 	mutex_enter(&ldcp->lock);
32141ae08745Sheppo 
32151ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
32161ae08745Sheppo 		DWARN(ldcp->id,
32171ae08745Sheppo 		    "ldc_read: (0x%llx) channel is not in UP state\n",
32181ae08745Sheppo 		    ldcp->id);
32191ae08745Sheppo 		exit_val = ECONNRESET;
32201ae08745Sheppo 	} else {
32211ae08745Sheppo 		exit_val = ldcp->read_p(ldcp, bufp, sizep);
32221ae08745Sheppo 	}
32231ae08745Sheppo 
32241ae08745Sheppo 	/*
32251ae08745Sheppo 	 * if queue has been drained - clear interrupt
32261ae08745Sheppo 	 */
32271ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
32281ae08745Sheppo 	    &ldcp->link_state);
3229cb112a14Slm66018 	if (rv != 0) {
3230cb112a14Slm66018 		cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs",
3231cb112a14Slm66018 		    ldcp->id);
3232cb112a14Slm66018 		mutex_enter(&ldcp->tx_lock);
3233cb112a14Slm66018 		i_ldc_reset(ldcp, B_TRUE);
3234cb112a14Slm66018 		mutex_exit(&ldcp->tx_lock);
3235cb112a14Slm66018 		return (ECONNRESET);
3236cb112a14Slm66018 	}
32373af08d82Slm66018 
32383af08d82Slm66018 	if (exit_val == 0) {
32393af08d82Slm66018 		if (ldcp->link_state == LDC_CHANNEL_DOWN ||
32403af08d82Slm66018 		    ldcp->link_state == LDC_CHANNEL_RESET) {
32413af08d82Slm66018 			mutex_enter(&ldcp->tx_lock);
32423af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
32433af08d82Slm66018 			exit_val = ECONNRESET;
32443af08d82Slm66018 			mutex_exit(&ldcp->tx_lock);
32453af08d82Slm66018 		}
32463af08d82Slm66018 		if ((rv == 0) &&
32473af08d82Slm66018 		    (ldcp->rx_intr_state == LDC_INTR_PEND) &&
32483af08d82Slm66018 		    (rx_head == rx_tail)) {
32491ae08745Sheppo 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
32501ae08745Sheppo 		}
32513af08d82Slm66018 	}
32521ae08745Sheppo 
32531ae08745Sheppo 	mutex_exit(&ldcp->lock);
32541ae08745Sheppo 	return (exit_val);
32551ae08745Sheppo }
32561ae08745Sheppo 
32571ae08745Sheppo /*
32581ae08745Sheppo  * Basic raw mondo read -
32591ae08745Sheppo  * no interpretation of mondo contents at all.
32601ae08745Sheppo  *
32611ae08745Sheppo  * Enter and exit with ldcp->lock held by caller
32621ae08745Sheppo  */
32631ae08745Sheppo static int
32641ae08745Sheppo i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep)
32651ae08745Sheppo {
32661ae08745Sheppo 	uint64_t 	q_size_mask;
32671ae08745Sheppo 	ldc_msg_t 	*msgp;
32681ae08745Sheppo 	uint8_t		*msgbufp;
32691ae08745Sheppo 	int		rv = 0, space;
32701ae08745Sheppo 	uint64_t 	rx_head, rx_tail;
32711ae08745Sheppo 
32721ae08745Sheppo 	space = *sizep;
32731ae08745Sheppo 
32741ae08745Sheppo 	if (space < LDC_PAYLOAD_SIZE_RAW)
32751ae08745Sheppo 		return (ENOBUFS);
32761ae08745Sheppo 
32771ae08745Sheppo 	ASSERT(mutex_owned(&ldcp->lock));
32781ae08745Sheppo 
32791ae08745Sheppo 	/* compute mask for increment */
32801ae08745Sheppo 	q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT;
32811ae08745Sheppo 
32821ae08745Sheppo 	/*
32831ae08745Sheppo 	 * Read packet(s) from the queue
32841ae08745Sheppo 	 */
32851ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
32861ae08745Sheppo 	    &ldcp->link_state);
32871ae08745Sheppo 	if (rv != 0) {
32881ae08745Sheppo 		cmn_err(CE_WARN,
32891ae08745Sheppo 		    "ldc_read_raw: (0x%lx) unable to read queue ptrs",
32901ae08745Sheppo 		    ldcp->id);
32911ae08745Sheppo 		return (EIO);
32921ae08745Sheppo 	}
32931ae08745Sheppo 	D1(ldcp->id, "ldc_read_raw: (0x%llx) rxh=0x%llx,"
32941ae08745Sheppo 		" rxt=0x%llx, st=0x%llx\n",
32951ae08745Sheppo 		ldcp->id, rx_head, rx_tail, ldcp->link_state);
32961ae08745Sheppo 
32971ae08745Sheppo 	/* reset the channel state if the channel went down */
32983af08d82Slm66018 	if (ldcp->link_state == LDC_CHANNEL_DOWN ||
32993af08d82Slm66018 	    ldcp->link_state == LDC_CHANNEL_RESET) {
3300d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
33013af08d82Slm66018 		i_ldc_reset(ldcp, B_FALSE);
3302d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
33031ae08745Sheppo 		return (ECONNRESET);
33041ae08745Sheppo 	}
33051ae08745Sheppo 
33061ae08745Sheppo 	/*
33071ae08745Sheppo 	 * Check for empty queue
33081ae08745Sheppo 	 */
33091ae08745Sheppo 	if (rx_head == rx_tail) {
33101ae08745Sheppo 		*sizep = 0;
33111ae08745Sheppo 		return (0);
33121ae08745Sheppo 	}
33131ae08745Sheppo 
33141ae08745Sheppo 	/* get the message */
33151ae08745Sheppo 	msgp = (ldc_msg_t *)(ldcp->rx_q_va + rx_head);
33161ae08745Sheppo 
33171ae08745Sheppo 	/* if channel is in RAW mode, copy data and return */
33181ae08745Sheppo 	msgbufp = (uint8_t *)&(msgp->raw[0]);
33191ae08745Sheppo 
33201ae08745Sheppo 	bcopy(msgbufp, target_bufp, LDC_PAYLOAD_SIZE_RAW);
33211ae08745Sheppo 
33221ae08745Sheppo 	DUMP_PAYLOAD(ldcp->id, msgbufp);
33231ae08745Sheppo 
33241ae08745Sheppo 	*sizep = LDC_PAYLOAD_SIZE_RAW;
33251ae08745Sheppo 
33261ae08745Sheppo 	rx_head = (rx_head + LDC_PACKET_SIZE) & q_size_mask;
33270a55fbb7Slm66018 	rv = i_ldc_set_rx_head(ldcp, rx_head);
33281ae08745Sheppo 
33291ae08745Sheppo 	return (rv);
33301ae08745Sheppo }
33311ae08745Sheppo 
33321ae08745Sheppo /*
33331ae08745Sheppo  * Process LDC mondos to build larger packets
33341ae08745Sheppo  * with either un-reliable or reliable delivery.
33351ae08745Sheppo  *
33361ae08745Sheppo  * Enter and exit with ldcp->lock held by caller
33371ae08745Sheppo  */
33381ae08745Sheppo static int
33391ae08745Sheppo i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep)
33401ae08745Sheppo {
33411ae08745Sheppo 	int		rv = 0;
33421ae08745Sheppo 	uint64_t 	rx_head = 0, rx_tail = 0;
33431ae08745Sheppo 	uint64_t 	curr_head = 0;
33441ae08745Sheppo 	ldc_msg_t 	*msg;
33451ae08745Sheppo 	caddr_t 	target;
33461ae08745Sheppo 	size_t 		len = 0, bytes_read = 0;
33470a55fbb7Slm66018 	int 		retries = 0;
33481ae08745Sheppo 	uint64_t 	q_size_mask;
3349d10e4ef2Snarayan 	uint64_t	first_fragment = 0;
33501ae08745Sheppo 
33511ae08745Sheppo 	target = target_bufp;
33521ae08745Sheppo 
33531ae08745Sheppo 	ASSERT(mutex_owned(&ldcp->lock));
33541ae08745Sheppo 
33553af08d82Slm66018 	/* check if the buffer and size are valid */
33563af08d82Slm66018 	if (target_bufp == NULL || *sizep == 0) {
33573af08d82Slm66018 		DWARN(ldcp->id, "ldc_read: (0x%llx) invalid buffer/size\n",
33583af08d82Slm66018 		    ldcp->id);
33593af08d82Slm66018 		return (EINVAL);
33603af08d82Slm66018 	}
33613af08d82Slm66018 
33621ae08745Sheppo 	/* compute mask for increment */
33631ae08745Sheppo 	q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT;
33641ae08745Sheppo 
33651ae08745Sheppo 	/*
33661ae08745Sheppo 	 * Read packet(s) from the queue
33671ae08745Sheppo 	 */
33681ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &curr_head, &rx_tail,
33691ae08745Sheppo 	    &ldcp->link_state);
33701ae08745Sheppo 	if (rv != 0) {
33713af08d82Slm66018 		cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs",
33721ae08745Sheppo 		    ldcp->id);
33733af08d82Slm66018 		mutex_enter(&ldcp->tx_lock);
33743af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
33753af08d82Slm66018 		mutex_exit(&ldcp->tx_lock);
33763af08d82Slm66018 		return (ECONNRESET);
33771ae08745Sheppo 	}
33781ae08745Sheppo 	D1(ldcp->id, "ldc_read: (0x%llx) chd=0x%llx, tl=0x%llx, st=0x%llx\n",
33791ae08745Sheppo 	    ldcp->id, curr_head, rx_tail, ldcp->link_state);
33801ae08745Sheppo 
33811ae08745Sheppo 	/* reset the channel state if the channel went down */
33823af08d82Slm66018 	if (ldcp->link_state != LDC_CHANNEL_UP)
33833af08d82Slm66018 		goto channel_is_reset;
33841ae08745Sheppo 
33851ae08745Sheppo 	for (;;) {
33861ae08745Sheppo 
33871ae08745Sheppo 		if (curr_head == rx_tail) {
33881ae08745Sheppo 			rv = hv_ldc_rx_get_state(ldcp->id,
33891ae08745Sheppo 			    &rx_head, &rx_tail, &ldcp->link_state);
33901ae08745Sheppo 			if (rv != 0) {
33911ae08745Sheppo 				cmn_err(CE_WARN,
33921ae08745Sheppo 				    "ldc_read: (0x%lx) cannot read queue ptrs",
33931ae08745Sheppo 				    ldcp->id);
3394d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
33953af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
3396d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
33971ae08745Sheppo 				return (ECONNRESET);
33981ae08745Sheppo 			}
33993af08d82Slm66018 			if (ldcp->link_state != LDC_CHANNEL_UP)
34003af08d82Slm66018 				goto channel_is_reset;
34011ae08745Sheppo 
34021ae08745Sheppo 			if (curr_head == rx_tail) {
34031ae08745Sheppo 
34041ae08745Sheppo 				/* If in the middle of a fragmented xfer */
3405d10e4ef2Snarayan 				if (first_fragment != 0) {
34060a55fbb7Slm66018 
34070a55fbb7Slm66018 					/* wait for ldc_delay usecs */
34080a55fbb7Slm66018 					drv_usecwait(ldc_delay);
34090a55fbb7Slm66018 
34100a55fbb7Slm66018 					if (++retries < ldc_max_retries)
34111ae08745Sheppo 						continue;
34120a55fbb7Slm66018 
34131ae08745Sheppo 					*sizep = 0;
3414d10e4ef2Snarayan 					ldcp->last_msg_rcd = first_fragment - 1;
34153af08d82Slm66018 					DWARN(DBG_ALL_LDCS, "ldc_read: "
34163af08d82Slm66018 						"(0x%llx) read timeout",
34171ae08745Sheppo 						ldcp->id);
34183af08d82Slm66018 					return (EAGAIN);
34191ae08745Sheppo 				}
34201ae08745Sheppo 				*sizep = 0;
34211ae08745Sheppo 				break;
34221ae08745Sheppo 			}
34233af08d82Slm66018 		}
34240a55fbb7Slm66018 		retries = 0;
34251ae08745Sheppo 
34261ae08745Sheppo 		D2(ldcp->id,
34271ae08745Sheppo 		    "ldc_read: (0x%llx) chd=0x%llx, rxhd=0x%llx, rxtl=0x%llx\n",
34281ae08745Sheppo 		    ldcp->id, curr_head, rx_head, rx_tail);
34291ae08745Sheppo 
34301ae08745Sheppo 		/* get the message */
34311ae08745Sheppo 		msg = (ldc_msg_t *)(ldcp->rx_q_va + curr_head);
34321ae08745Sheppo 
34331ae08745Sheppo 		DUMP_LDC_PKT(ldcp, "ldc_read received pkt",
34341ae08745Sheppo 		    ldcp->rx_q_va + curr_head);
34351ae08745Sheppo 
34361ae08745Sheppo 		/* Check the message ID for the message received */
34371ae08745Sheppo 		if ((rv = i_ldc_check_seqid(ldcp, msg)) != 0) {
34381ae08745Sheppo 
34391ae08745Sheppo 			DWARN(ldcp->id, "ldc_read: (0x%llx) seqid error, "
34401ae08745Sheppo 			    "q_ptrs=0x%lx,0x%lx", ldcp->id, rx_head, rx_tail);
34411ae08745Sheppo 
34420a55fbb7Slm66018 			/* throw away data */
34430a55fbb7Slm66018 			bytes_read = 0;
34440a55fbb7Slm66018 
34451ae08745Sheppo 			/* Reset last_msg_rcd to start of message */
3446d10e4ef2Snarayan 			if (first_fragment != 0) {
3447d10e4ef2Snarayan 				ldcp->last_msg_rcd = first_fragment - 1;
3448d10e4ef2Snarayan 				first_fragment = 0;
34491ae08745Sheppo 			}
34501ae08745Sheppo 			/*
34511ae08745Sheppo 			 * Send a NACK -- invalid seqid
34521ae08745Sheppo 			 * get the current tail for the response
34531ae08745Sheppo 			 */
34541ae08745Sheppo 			rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK,
34551ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK));
34561ae08745Sheppo 			if (rv) {
34571ae08745Sheppo 				cmn_err(CE_NOTE,
34581ae08745Sheppo 				    "ldc_read: (0x%lx) err sending "
34591ae08745Sheppo 				    "NACK msg\n", ldcp->id);
3460d10e4ef2Snarayan 
3461d10e4ef2Snarayan 				/* if cannot send NACK - reset channel */
3462d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
34633af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
3464d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
3465d10e4ef2Snarayan 				rv = ECONNRESET;
3466d10e4ef2Snarayan 				break;
34671ae08745Sheppo 			}
34681ae08745Sheppo 
34691ae08745Sheppo 			/* purge receive queue */
34700a55fbb7Slm66018 			rv = i_ldc_set_rx_head(ldcp, rx_tail);
34711ae08745Sheppo 
34721ae08745Sheppo 			break;
34731ae08745Sheppo 		}
34741ae08745Sheppo 
34751ae08745Sheppo 		/*
34761ae08745Sheppo 		 * Process any messages of type CTRL messages
3477e1ebb9ecSlm66018 		 * Future implementations should try to pass these
3478e1ebb9ecSlm66018 		 * to LDC link by resetting the intr state.
34791ae08745Sheppo 		 *
34801ae08745Sheppo 		 * NOTE: not done as a switch() as type can be both ctrl+data
34811ae08745Sheppo 		 */
34821ae08745Sheppo 		if (msg->type & LDC_CTRL) {
34831ae08745Sheppo 			if (rv = i_ldc_ctrlmsg(ldcp, msg)) {
34841ae08745Sheppo 				if (rv == EAGAIN)
34851ae08745Sheppo 					continue;
34860a55fbb7Slm66018 				rv = i_ldc_set_rx_head(ldcp, rx_tail);
34871ae08745Sheppo 				*sizep = 0;
34881ae08745Sheppo 				bytes_read = 0;
34891ae08745Sheppo 				break;
34901ae08745Sheppo 			}
34911ae08745Sheppo 		}
34921ae08745Sheppo 
34931ae08745Sheppo 		/* process data ACKs */
34941ae08745Sheppo 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) {
3495d10e4ef2Snarayan 			if (rv = i_ldc_process_data_ACK(ldcp, msg)) {
3496d10e4ef2Snarayan 				*sizep = 0;
3497d10e4ef2Snarayan 				bytes_read = 0;
3498d10e4ef2Snarayan 				break;
3499d10e4ef2Snarayan 			}
35001ae08745Sheppo 		}
35011ae08745Sheppo 
3502*83d3bc6fSnarayan 		/* process data NACKs */
3503*83d3bc6fSnarayan 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_NACK)) {
3504*83d3bc6fSnarayan 			DWARN(ldcp->id,
3505*83d3bc6fSnarayan 			    "ldc_read: (0x%llx) received DATA/NACK", ldcp->id);
3506*83d3bc6fSnarayan 			mutex_enter(&ldcp->tx_lock);
3507*83d3bc6fSnarayan 			i_ldc_reset(ldcp, B_TRUE);
3508*83d3bc6fSnarayan 			mutex_exit(&ldcp->tx_lock);
3509*83d3bc6fSnarayan 			return (ECONNRESET);
3510*83d3bc6fSnarayan 		}
3511*83d3bc6fSnarayan 
35121ae08745Sheppo 		/* process data messages */
35131ae08745Sheppo 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) {
35141ae08745Sheppo 
35151ae08745Sheppo 			uint8_t *msgbuf = (uint8_t *)(
35161ae08745Sheppo 				(ldcp->mode == LDC_MODE_RELIABLE ||
35171ae08745Sheppo 				ldcp->mode == LDC_MODE_STREAM)
35181ae08745Sheppo 				? msg->rdata : msg->udata);
35191ae08745Sheppo 
35201ae08745Sheppo 			D2(ldcp->id,
35211ae08745Sheppo 			    "ldc_read: (0x%llx) received data msg\n", ldcp->id);
35221ae08745Sheppo 
35231ae08745Sheppo 			/* get the packet length */
35241ae08745Sheppo 			len = (msg->env & LDC_LEN_MASK);
35251ae08745Sheppo 
35261ae08745Sheppo 				/*
35271ae08745Sheppo 				 * FUTURE OPTIMIZATION:
35281ae08745Sheppo 				 * dont need to set q head for every
35291ae08745Sheppo 				 * packet we read just need to do this when
35301ae08745Sheppo 				 * we are done or need to wait for more
35311ae08745Sheppo 				 * mondos to make a full packet - this is
35321ae08745Sheppo 				 * currently expensive.
35331ae08745Sheppo 				 */
35341ae08745Sheppo 
3535d10e4ef2Snarayan 			if (first_fragment == 0) {
35361ae08745Sheppo 
35371ae08745Sheppo 				/*
35381ae08745Sheppo 				 * first packets should always have the start
35391ae08745Sheppo 				 * bit set (even for a single packet). If not
35401ae08745Sheppo 				 * throw away the packet
35411ae08745Sheppo 				 */
35421ae08745Sheppo 				if (!(msg->env & LDC_FRAG_START)) {
35431ae08745Sheppo 
35441ae08745Sheppo 					DWARN(DBG_ALL_LDCS,
35451ae08745Sheppo 					    "ldc_read: (0x%llx) not start - "
35461ae08745Sheppo 					    "frag=%x\n", ldcp->id,
35471ae08745Sheppo 					    (msg->env) & LDC_FRAG_MASK);
35481ae08745Sheppo 
35491ae08745Sheppo 					/* toss pkt, inc head, cont reading */
35501ae08745Sheppo 					bytes_read = 0;
35511ae08745Sheppo 					target = target_bufp;
35521ae08745Sheppo 					curr_head =
35531ae08745Sheppo 						(curr_head + LDC_PACKET_SIZE)
35541ae08745Sheppo 						& q_size_mask;
35551ae08745Sheppo 					if (rv = i_ldc_set_rx_head(ldcp,
35561ae08745Sheppo 						curr_head))
35571ae08745Sheppo 						break;
35581ae08745Sheppo 
35591ae08745Sheppo 					continue;
35601ae08745Sheppo 				}
35611ae08745Sheppo 
3562d10e4ef2Snarayan 				first_fragment = msg->seqid;
35631ae08745Sheppo 			} else {
35641ae08745Sheppo 				/* check to see if this is a pkt w/ START bit */
35651ae08745Sheppo 				if (msg->env & LDC_FRAG_START) {
35661ae08745Sheppo 					DWARN(DBG_ALL_LDCS,
35671ae08745Sheppo 					    "ldc_read:(0x%llx) unexpected pkt"
35681ae08745Sheppo 					    " env=0x%x discarding %d bytes,"
35691ae08745Sheppo 					    " lastmsg=%d, currentmsg=%d\n",
35701ae08745Sheppo 					    ldcp->id, msg->env&LDC_FRAG_MASK,
35711ae08745Sheppo 					    bytes_read, ldcp->last_msg_rcd,
35721ae08745Sheppo 					    msg->seqid);
35731ae08745Sheppo 
35741ae08745Sheppo 					/* throw data we have read so far */
35751ae08745Sheppo 					bytes_read = 0;
35761ae08745Sheppo 					target = target_bufp;
3577d10e4ef2Snarayan 					first_fragment = msg->seqid;
35781ae08745Sheppo 
35791ae08745Sheppo 					if (rv = i_ldc_set_rx_head(ldcp,
35801ae08745Sheppo 						curr_head))
35811ae08745Sheppo 						break;
35821ae08745Sheppo 				}
35831ae08745Sheppo 			}
35841ae08745Sheppo 
35851ae08745Sheppo 			/* copy (next) pkt into buffer */
35861ae08745Sheppo 			if (len <= (*sizep - bytes_read)) {
35871ae08745Sheppo 				bcopy(msgbuf, target, len);
35881ae08745Sheppo 				target += len;
35891ae08745Sheppo 				bytes_read += len;
35901ae08745Sheppo 			} else {
35911ae08745Sheppo 				/*
35921ae08745Sheppo 				 * there is not enough space in the buffer to
35931ae08745Sheppo 				 * read this pkt. throw message away & continue
35941ae08745Sheppo 				 * reading data from queue
35951ae08745Sheppo 				 */
35961ae08745Sheppo 				DWARN(DBG_ALL_LDCS,
35971ae08745Sheppo 				    "ldc_read: (0x%llx) buffer too small, "
35981ae08745Sheppo 				    "head=0x%lx, expect=%d, got=%d\n", ldcp->id,
35991ae08745Sheppo 				    curr_head, *sizep, bytes_read+len);
36001ae08745Sheppo 
3601d10e4ef2Snarayan 				first_fragment = 0;
36021ae08745Sheppo 				target = target_bufp;
36031ae08745Sheppo 				bytes_read = 0;
36041ae08745Sheppo 
36051ae08745Sheppo 				/* throw away everything received so far */
36061ae08745Sheppo 				if (rv = i_ldc_set_rx_head(ldcp, curr_head))
36071ae08745Sheppo 					break;
36081ae08745Sheppo 
36091ae08745Sheppo 				/* continue reading remaining pkts */
36101ae08745Sheppo 				continue;
36111ae08745Sheppo 			}
36121ae08745Sheppo 		}
36131ae08745Sheppo 
36141ae08745Sheppo 		/* set the message id */
36151ae08745Sheppo 		ldcp->last_msg_rcd = msg->seqid;
36161ae08745Sheppo 
36171ae08745Sheppo 		/* move the head one position */
36181ae08745Sheppo 		curr_head = (curr_head + LDC_PACKET_SIZE) & q_size_mask;
36191ae08745Sheppo 
36201ae08745Sheppo 		if (msg->env & LDC_FRAG_STOP) {
36211ae08745Sheppo 
36221ae08745Sheppo 			/*
36231ae08745Sheppo 			 * All pkts that are part of this fragmented transfer
36241ae08745Sheppo 			 * have been read or this was a single pkt read
36251ae08745Sheppo 			 * or there was an error
36261ae08745Sheppo 			 */
36271ae08745Sheppo 
36281ae08745Sheppo 			/* set the queue head */
36291ae08745Sheppo 			if (rv = i_ldc_set_rx_head(ldcp, curr_head))
36301ae08745Sheppo 				bytes_read = 0;
36311ae08745Sheppo 
36321ae08745Sheppo 			*sizep = bytes_read;
36331ae08745Sheppo 
36341ae08745Sheppo 			break;
36351ae08745Sheppo 		}
36361ae08745Sheppo 
36371ae08745Sheppo 		/* advance head if it is a DATA ACK */
36381ae08745Sheppo 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) {
36391ae08745Sheppo 
36401ae08745Sheppo 			/* set the queue head */
36411ae08745Sheppo 			if (rv = i_ldc_set_rx_head(ldcp, curr_head)) {
36421ae08745Sheppo 				bytes_read = 0;
36431ae08745Sheppo 				break;
36441ae08745Sheppo 			}
36451ae08745Sheppo 
36461ae08745Sheppo 			D2(ldcp->id, "ldc_read: (0x%llx) set ACK qhead 0x%llx",
36471ae08745Sheppo 			    ldcp->id, curr_head);
36481ae08745Sheppo 		}
36491ae08745Sheppo 
36501ae08745Sheppo 	} /* for (;;) */
36511ae08745Sheppo 
36521ae08745Sheppo 
36531ae08745Sheppo 	/*
36541ae08745Sheppo 	 * If useful data was read - Send msg ACK
36551ae08745Sheppo 	 * OPTIMIZE: do not send ACK for all msgs - use some frequency
36561ae08745Sheppo 	 */
36571ae08745Sheppo 	if ((bytes_read > 0) && (ldcp->mode == LDC_MODE_RELIABLE ||
36581ae08745Sheppo 		ldcp->mode == LDC_MODE_STREAM)) {
36591ae08745Sheppo 
36601ae08745Sheppo 		rv = i_ldc_send_pkt(ldcp, LDC_DATA, LDC_ACK, 0);
36613af08d82Slm66018 		if (rv && rv != EWOULDBLOCK) {
36621ae08745Sheppo 			cmn_err(CE_NOTE,
36631ae08745Sheppo 			    "ldc_read: (0x%lx) cannot send ACK\n", ldcp->id);
3664d10e4ef2Snarayan 
3665d10e4ef2Snarayan 			/* if cannot send ACK - reset channel */
36663af08d82Slm66018 			goto channel_is_reset;
36671ae08745Sheppo 		}
36681ae08745Sheppo 	}
36691ae08745Sheppo 
36701ae08745Sheppo 	D2(ldcp->id, "ldc_read: (0x%llx) end size=%d", ldcp->id, *sizep);
36711ae08745Sheppo 
36721ae08745Sheppo 	return (rv);
36733af08d82Slm66018 
36743af08d82Slm66018 channel_is_reset:
36753af08d82Slm66018 	mutex_enter(&ldcp->tx_lock);
36763af08d82Slm66018 	i_ldc_reset(ldcp, B_FALSE);
36773af08d82Slm66018 	mutex_exit(&ldcp->tx_lock);
36783af08d82Slm66018 	return (ECONNRESET);
36791ae08745Sheppo }
36801ae08745Sheppo 
36811ae08745Sheppo /*
36821ae08745Sheppo  * Use underlying reliable packet mechanism to fetch
36831ae08745Sheppo  * and buffer incoming packets so we can hand them back as
36841ae08745Sheppo  * a basic byte stream.
36851ae08745Sheppo  *
36861ae08745Sheppo  * Enter and exit with ldcp->lock held by caller
36871ae08745Sheppo  */
36881ae08745Sheppo static int
36891ae08745Sheppo i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep)
36901ae08745Sheppo {
36911ae08745Sheppo 	int	rv;
36921ae08745Sheppo 	size_t	size;
36931ae08745Sheppo 
36941ae08745Sheppo 	ASSERT(mutex_owned(&ldcp->lock));
36951ae08745Sheppo 
36961ae08745Sheppo 	D2(ldcp->id, "i_ldc_read_stream: (0x%llx) buffer size=%d",
36971ae08745Sheppo 		ldcp->id, *sizep);
36981ae08745Sheppo 
36991ae08745Sheppo 	if (ldcp->stream_remains == 0) {
37001ae08745Sheppo 		size = ldcp->mtu;
37011ae08745Sheppo 		rv = i_ldc_read_packet(ldcp,
37021ae08745Sheppo 			(caddr_t)ldcp->stream_bufferp, &size);
37031ae08745Sheppo 		D2(ldcp->id, "i_ldc_read_stream: read packet (0x%llx) size=%d",
37041ae08745Sheppo 			ldcp->id, size);
37051ae08745Sheppo 
37061ae08745Sheppo 		if (rv != 0)
37071ae08745Sheppo 			return (rv);
37081ae08745Sheppo 
37091ae08745Sheppo 		ldcp->stream_remains = size;
37101ae08745Sheppo 		ldcp->stream_offset = 0;
37111ae08745Sheppo 	}
37121ae08745Sheppo 
37131ae08745Sheppo 	size = MIN(ldcp->stream_remains, *sizep);
37141ae08745Sheppo 
37151ae08745Sheppo 	bcopy(ldcp->stream_bufferp + ldcp->stream_offset, target_bufp, size);
37161ae08745Sheppo 	ldcp->stream_offset += size;
37171ae08745Sheppo 	ldcp->stream_remains -= size;
37181ae08745Sheppo 
37191ae08745Sheppo 	D2(ldcp->id, "i_ldc_read_stream: (0x%llx) fill from buffer size=%d",
37201ae08745Sheppo 		ldcp->id, size);
37211ae08745Sheppo 
37221ae08745Sheppo 	*sizep = size;
37231ae08745Sheppo 	return (0);
37241ae08745Sheppo }
37251ae08745Sheppo 
37261ae08745Sheppo /*
37271ae08745Sheppo  * Write specified amount of bytes to the channel
37281ae08745Sheppo  * in multiple pkts of pkt_payload size. Each
37291ae08745Sheppo  * packet is tagged with an unique packet ID in
3730e1ebb9ecSlm66018  * the case of a reliable link.
37311ae08745Sheppo  *
37321ae08745Sheppo  * On return, size contains the number of bytes written.
37331ae08745Sheppo  */
37341ae08745Sheppo int
37351ae08745Sheppo ldc_write(ldc_handle_t handle, caddr_t buf, size_t *sizep)
37361ae08745Sheppo {
37371ae08745Sheppo 	ldc_chan_t	*ldcp;
37381ae08745Sheppo 	int		rv = 0;
37391ae08745Sheppo 
37401ae08745Sheppo 	if (handle == NULL) {
37411ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_write: invalid channel handle\n");
37421ae08745Sheppo 		return (EINVAL);
37431ae08745Sheppo 	}
37441ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
37451ae08745Sheppo 
3746d10e4ef2Snarayan 	/* check if writes can occur */
3747d10e4ef2Snarayan 	if (!mutex_tryenter(&ldcp->tx_lock)) {
3748d10e4ef2Snarayan 		/*
3749d10e4ef2Snarayan 		 * Could not get the lock - channel could
3750d10e4ef2Snarayan 		 * be in the process of being unconfigured
3751d10e4ef2Snarayan 		 * or reader has encountered an error
3752d10e4ef2Snarayan 		 */
3753d10e4ef2Snarayan 		return (EAGAIN);
3754d10e4ef2Snarayan 	}
37551ae08745Sheppo 
37561ae08745Sheppo 	/* check if non-zero data to write */
37571ae08745Sheppo 	if (buf == NULL || sizep == NULL) {
37581ae08745Sheppo 		DWARN(ldcp->id, "ldc_write: (0x%llx) invalid data write\n",
37591ae08745Sheppo 		    ldcp->id);
3760d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
37611ae08745Sheppo 		return (EINVAL);
37621ae08745Sheppo 	}
37631ae08745Sheppo 
37641ae08745Sheppo 	if (*sizep == 0) {
37651ae08745Sheppo 		DWARN(ldcp->id, "ldc_write: (0x%llx) write size of zero\n",
37661ae08745Sheppo 		    ldcp->id);
3767d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
37681ae08745Sheppo 		return (0);
37691ae08745Sheppo 	}
37701ae08745Sheppo 
37711ae08745Sheppo 	/* Check if channel is UP for data exchange */
37721ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
37731ae08745Sheppo 		DWARN(ldcp->id,
37741ae08745Sheppo 		    "ldc_write: (0x%llx) channel is not in UP state\n",
37751ae08745Sheppo 		    ldcp->id);
37761ae08745Sheppo 		*sizep = 0;
37771ae08745Sheppo 		rv = ECONNRESET;
37781ae08745Sheppo 	} else {
37791ae08745Sheppo 		rv = ldcp->write_p(ldcp, buf, sizep);
37801ae08745Sheppo 	}
37811ae08745Sheppo 
3782d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
37831ae08745Sheppo 
37841ae08745Sheppo 	return (rv);
37851ae08745Sheppo }
37861ae08745Sheppo 
37871ae08745Sheppo /*
37881ae08745Sheppo  * Write a raw packet to the channel
37891ae08745Sheppo  * On return, size contains the number of bytes written.
37901ae08745Sheppo  */
37911ae08745Sheppo static int
37921ae08745Sheppo i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep)
37931ae08745Sheppo {
37941ae08745Sheppo 	ldc_msg_t 	*ldcmsg;
37951ae08745Sheppo 	uint64_t 	tx_head, tx_tail, new_tail;
37961ae08745Sheppo 	int		rv = 0;
37971ae08745Sheppo 	size_t		size;
37981ae08745Sheppo 
3799d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
38001ae08745Sheppo 	ASSERT(ldcp->mode == LDC_MODE_RAW);
38011ae08745Sheppo 
38021ae08745Sheppo 	size = *sizep;
38031ae08745Sheppo 
38041ae08745Sheppo 	/*
38051ae08745Sheppo 	 * Check to see if the packet size is less than or
38061ae08745Sheppo 	 * equal to packet size support in raw mode
38071ae08745Sheppo 	 */
38081ae08745Sheppo 	if (size > ldcp->pkt_payload) {
38091ae08745Sheppo 		DWARN(ldcp->id,
38101ae08745Sheppo 		    "ldc_write: (0x%llx) invalid size (0x%llx) for RAW mode\n",
38111ae08745Sheppo 		    ldcp->id, *sizep);
38121ae08745Sheppo 		*sizep = 0;
38131ae08745Sheppo 		return (EMSGSIZE);
38141ae08745Sheppo 	}
38151ae08745Sheppo 
38161ae08745Sheppo 	/* get the qptrs for the tx queue */
38171ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
38181ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
38191ae08745Sheppo 	if (rv != 0) {
38201ae08745Sheppo 		cmn_err(CE_WARN,
38211ae08745Sheppo 		    "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id);
38221ae08745Sheppo 		*sizep = 0;
38231ae08745Sheppo 		return (EIO);
38241ae08745Sheppo 	}
38251ae08745Sheppo 
38261ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_DOWN ||
38271ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
38281ae08745Sheppo 		DWARN(ldcp->id,
38291ae08745Sheppo 		    "ldc_write: (0x%llx) channel down/reset\n", ldcp->id);
3830d10e4ef2Snarayan 
38311ae08745Sheppo 		*sizep = 0;
3832d10e4ef2Snarayan 		if (mutex_tryenter(&ldcp->lock)) {
38333af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
3834d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
3835d10e4ef2Snarayan 		} else {
3836d10e4ef2Snarayan 			/*
3837d10e4ef2Snarayan 			 * Release Tx lock, and then reacquire channel
3838d10e4ef2Snarayan 			 * and Tx lock in correct order
3839d10e4ef2Snarayan 			 */
3840d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
3841d10e4ef2Snarayan 			mutex_enter(&ldcp->lock);
3842d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
38433af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
3844d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
3845d10e4ef2Snarayan 		}
38461ae08745Sheppo 		return (ECONNRESET);
38471ae08745Sheppo 	}
38481ae08745Sheppo 
38491ae08745Sheppo 	tx_tail = ldcp->tx_tail;
38501ae08745Sheppo 	tx_head = ldcp->tx_head;
38511ae08745Sheppo 	new_tail = (tx_tail + LDC_PACKET_SIZE) &
38521ae08745Sheppo 		((ldcp->tx_q_entries-1) << LDC_PACKET_SHIFT);
38531ae08745Sheppo 
38541ae08745Sheppo 	if (new_tail == tx_head) {
38551ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
38561ae08745Sheppo 		    "ldc_write: (0x%llx) TX queue is full\n", ldcp->id);
38571ae08745Sheppo 		*sizep = 0;
38581ae08745Sheppo 		return (EWOULDBLOCK);
38591ae08745Sheppo 	}
38601ae08745Sheppo 
38611ae08745Sheppo 	D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d",
38621ae08745Sheppo 	    ldcp->id, size);
38631ae08745Sheppo 
38641ae08745Sheppo 	/* Send the data now */
38651ae08745Sheppo 	ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
38661ae08745Sheppo 
38671ae08745Sheppo 	/* copy the data into pkt */
38681ae08745Sheppo 	bcopy((uint8_t *)buf, ldcmsg, size);
38691ae08745Sheppo 
38701ae08745Sheppo 	/* increment tail */
38711ae08745Sheppo 	tx_tail = new_tail;
38721ae08745Sheppo 
38731ae08745Sheppo 	/*
38741ae08745Sheppo 	 * All packets have been copied into the TX queue
38751ae08745Sheppo 	 * update the tail ptr in the HV
38761ae08745Sheppo 	 */
38771ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
38781ae08745Sheppo 	if (rv) {
38791ae08745Sheppo 		if (rv == EWOULDBLOCK) {
38801ae08745Sheppo 			DWARN(ldcp->id, "ldc_write: (0x%llx) write timed out\n",
38811ae08745Sheppo 			    ldcp->id);
38821ae08745Sheppo 			*sizep = 0;
38831ae08745Sheppo 			return (EWOULDBLOCK);
38841ae08745Sheppo 		}
38851ae08745Sheppo 
38861ae08745Sheppo 		*sizep = 0;
3887d10e4ef2Snarayan 		if (mutex_tryenter(&ldcp->lock)) {
38883af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
3889d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
3890d10e4ef2Snarayan 		} else {
3891d10e4ef2Snarayan 			/*
3892d10e4ef2Snarayan 			 * Release Tx lock, and then reacquire channel
3893d10e4ef2Snarayan 			 * and Tx lock in correct order
3894d10e4ef2Snarayan 			 */
3895d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
3896d10e4ef2Snarayan 			mutex_enter(&ldcp->lock);
3897d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
38983af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
3899d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
3900d10e4ef2Snarayan 		}
39011ae08745Sheppo 		return (ECONNRESET);
39021ae08745Sheppo 	}
39031ae08745Sheppo 
39041ae08745Sheppo 	ldcp->tx_tail = tx_tail;
39051ae08745Sheppo 	*sizep = size;
39061ae08745Sheppo 
39071ae08745Sheppo 	D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, size);
39081ae08745Sheppo 
39091ae08745Sheppo 	return (rv);
39101ae08745Sheppo }
39111ae08745Sheppo 
39121ae08745Sheppo 
39131ae08745Sheppo /*
39141ae08745Sheppo  * Write specified amount of bytes to the channel
39151ae08745Sheppo  * in multiple pkts of pkt_payload size. Each
39161ae08745Sheppo  * packet is tagged with an unique packet ID in
3917e1ebb9ecSlm66018  * the case of a reliable link.
39181ae08745Sheppo  *
39191ae08745Sheppo  * On return, size contains the number of bytes written.
39201ae08745Sheppo  * This function needs to ensure that the write size is < MTU size
39211ae08745Sheppo  */
39221ae08745Sheppo static int
39231ae08745Sheppo i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t buf, size_t *size)
39241ae08745Sheppo {
39251ae08745Sheppo 	ldc_msg_t 	*ldcmsg;
39261ae08745Sheppo 	uint64_t 	tx_head, tx_tail, new_tail, start;
39271ae08745Sheppo 	uint64_t	txq_size_mask, numavail;
39281ae08745Sheppo 	uint8_t 	*msgbuf, *source = (uint8_t *)buf;
39291ae08745Sheppo 	size_t 		len, bytes_written = 0, remaining;
39301ae08745Sheppo 	int		rv;
39311ae08745Sheppo 	uint32_t	curr_seqid;
39321ae08745Sheppo 
3933d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
39341ae08745Sheppo 
39351ae08745Sheppo 	ASSERT(ldcp->mode == LDC_MODE_RELIABLE ||
39361ae08745Sheppo 		ldcp->mode == LDC_MODE_UNRELIABLE ||
39371ae08745Sheppo 		ldcp->mode == LDC_MODE_STREAM);
39381ae08745Sheppo 
39391ae08745Sheppo 	/* compute mask for increment */
39401ae08745Sheppo 	txq_size_mask = (ldcp->tx_q_entries - 1) << LDC_PACKET_SHIFT;
39411ae08745Sheppo 
39421ae08745Sheppo 	/* get the qptrs for the tx queue */
39431ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
39441ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
39451ae08745Sheppo 	if (rv != 0) {
39461ae08745Sheppo 		cmn_err(CE_WARN,
39471ae08745Sheppo 		    "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id);
39481ae08745Sheppo 		*size = 0;
39491ae08745Sheppo 		return (EIO);
39501ae08745Sheppo 	}
39511ae08745Sheppo 
39521ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_DOWN ||
39531ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
39541ae08745Sheppo 		DWARN(ldcp->id,
39551ae08745Sheppo 		    "ldc_write: (0x%llx) channel down/reset\n", ldcp->id);
39561ae08745Sheppo 		*size = 0;
3957d10e4ef2Snarayan 		if (mutex_tryenter(&ldcp->lock)) {
39583af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
3959d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
3960d10e4ef2Snarayan 		} else {
3961d10e4ef2Snarayan 			/*
3962d10e4ef2Snarayan 			 * Release Tx lock, and then reacquire channel
3963d10e4ef2Snarayan 			 * and Tx lock in correct order
3964d10e4ef2Snarayan 			 */
3965d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
3966d10e4ef2Snarayan 			mutex_enter(&ldcp->lock);
3967d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
39683af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
3969d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
3970d10e4ef2Snarayan 		}
39711ae08745Sheppo 		return (ECONNRESET);
39721ae08745Sheppo 	}
39731ae08745Sheppo 
39741ae08745Sheppo 	tx_tail = ldcp->tx_tail;
39751ae08745Sheppo 	new_tail = (tx_tail + LDC_PACKET_SIZE) %
39761ae08745Sheppo 		(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
39771ae08745Sheppo 
39781ae08745Sheppo 	/*
3979e1ebb9ecSlm66018 	 * Link mode determines whether we use HV Tx head or the
39801ae08745Sheppo 	 * private protocol head (corresponding to last ACKd pkt) for
39811ae08745Sheppo 	 * determining how much we can write
39821ae08745Sheppo 	 */
39831ae08745Sheppo 	tx_head = (ldcp->mode == LDC_MODE_RELIABLE ||
39841ae08745Sheppo 		ldcp->mode == LDC_MODE_STREAM)
39851ae08745Sheppo 		? ldcp->tx_ackd_head : ldcp->tx_head;
39861ae08745Sheppo 	if (new_tail == tx_head) {
39871ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
39881ae08745Sheppo 		    "ldc_write: (0x%llx) TX queue is full\n", ldcp->id);
39891ae08745Sheppo 		*size = 0;
39901ae08745Sheppo 		return (EWOULDBLOCK);
39911ae08745Sheppo 	}
39921ae08745Sheppo 
39931ae08745Sheppo 	/*
39941ae08745Sheppo 	 * Make sure that the LDC Tx queue has enough space
39951ae08745Sheppo 	 */
39961ae08745Sheppo 	numavail = (tx_head >> LDC_PACKET_SHIFT) - (tx_tail >> LDC_PACKET_SHIFT)
39971ae08745Sheppo 		+ ldcp->tx_q_entries - 1;
39981ae08745Sheppo 	numavail %= ldcp->tx_q_entries;
39991ae08745Sheppo 
40001ae08745Sheppo 	if (*size > (numavail * ldcp->pkt_payload)) {
40011ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
40021ae08745Sheppo 		    "ldc_write: (0x%llx) TX queue has no space\n", ldcp->id);
40031ae08745Sheppo 		return (EWOULDBLOCK);
40041ae08745Sheppo 	}
40051ae08745Sheppo 
40061ae08745Sheppo 	D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d",
40071ae08745Sheppo 	    ldcp->id, *size);
40081ae08745Sheppo 
40091ae08745Sheppo 	/* Send the data now */
40101ae08745Sheppo 	bytes_written = 0;
40111ae08745Sheppo 	curr_seqid = ldcp->last_msg_snt;
40121ae08745Sheppo 	start = tx_tail;
40131ae08745Sheppo 
40141ae08745Sheppo 	while (*size > bytes_written) {
40151ae08745Sheppo 
40161ae08745Sheppo 		ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
40171ae08745Sheppo 
40181ae08745Sheppo 		msgbuf = (uint8_t *)((ldcp->mode == LDC_MODE_RELIABLE ||
40191ae08745Sheppo 			ldcp->mode == LDC_MODE_STREAM)
40201ae08745Sheppo 			? ldcmsg->rdata : ldcmsg->udata);
40211ae08745Sheppo 
40221ae08745Sheppo 		ldcmsg->type = LDC_DATA;
40231ae08745Sheppo 		ldcmsg->stype = LDC_INFO;
40241ae08745Sheppo 		ldcmsg->ctrl = 0;
40251ae08745Sheppo 
40261ae08745Sheppo 		remaining = *size - bytes_written;
40271ae08745Sheppo 		len = min(ldcp->pkt_payload, remaining);
40281ae08745Sheppo 		ldcmsg->env = (uint8_t)len;
40291ae08745Sheppo 
40301ae08745Sheppo 		curr_seqid++;
40311ae08745Sheppo 		ldcmsg->seqid = curr_seqid;
40321ae08745Sheppo 
40331ae08745Sheppo 		/* copy the data into pkt */
40341ae08745Sheppo 		bcopy(source, msgbuf, len);
40351ae08745Sheppo 
40361ae08745Sheppo 		source += len;
40371ae08745Sheppo 		bytes_written += len;
40381ae08745Sheppo 
40391ae08745Sheppo 		/* increment tail */
40401ae08745Sheppo 		tx_tail = (tx_tail + LDC_PACKET_SIZE) & txq_size_mask;
40411ae08745Sheppo 
40421ae08745Sheppo 		ASSERT(tx_tail != tx_head);
40431ae08745Sheppo 	}
40441ae08745Sheppo 
40451ae08745Sheppo 	/* Set the start and stop bits */
40461ae08745Sheppo 	ldcmsg->env |= LDC_FRAG_STOP;
40471ae08745Sheppo 	ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + start);
40481ae08745Sheppo 	ldcmsg->env |= LDC_FRAG_START;
40491ae08745Sheppo 
40501ae08745Sheppo 	/*
40511ae08745Sheppo 	 * All packets have been copied into the TX queue
40521ae08745Sheppo 	 * update the tail ptr in the HV
40531ae08745Sheppo 	 */
40541ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
40551ae08745Sheppo 	if (rv == 0) {
40561ae08745Sheppo 		ldcp->tx_tail = tx_tail;
40571ae08745Sheppo 		ldcp->last_msg_snt = curr_seqid;
40581ae08745Sheppo 		*size = bytes_written;
40591ae08745Sheppo 	} else {
40601ae08745Sheppo 		int rv2;
40611ae08745Sheppo 
40621ae08745Sheppo 		if (rv != EWOULDBLOCK) {
40631ae08745Sheppo 			*size = 0;
4064d10e4ef2Snarayan 			if (mutex_tryenter(&ldcp->lock)) {
40653af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
4066d10e4ef2Snarayan 				mutex_exit(&ldcp->lock);
4067d10e4ef2Snarayan 			} else {
4068d10e4ef2Snarayan 				/*
4069d10e4ef2Snarayan 				 * Release Tx lock, and then reacquire channel
4070d10e4ef2Snarayan 				 * and Tx lock in correct order
4071d10e4ef2Snarayan 				 */
4072d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
4073d10e4ef2Snarayan 				mutex_enter(&ldcp->lock);
4074d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
40753af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
4076d10e4ef2Snarayan 				mutex_exit(&ldcp->lock);
4077d10e4ef2Snarayan 			}
40781ae08745Sheppo 			return (ECONNRESET);
40791ae08745Sheppo 		}
40801ae08745Sheppo 
4081cb112a14Slm66018 		D1(ldcp->id, "hv_tx_set_tail returns 0x%x (head 0x%x, "
40821ae08745Sheppo 			"old tail 0x%x, new tail 0x%x, qsize=0x%x)\n",
40831ae08745Sheppo 			rv, ldcp->tx_head, ldcp->tx_tail, tx_tail,
40841ae08745Sheppo 			(ldcp->tx_q_entries << LDC_PACKET_SHIFT));
40851ae08745Sheppo 
40861ae08745Sheppo 		rv2 = hv_ldc_tx_get_state(ldcp->id,
40871ae08745Sheppo 		    &tx_head, &tx_tail, &ldcp->link_state);
40881ae08745Sheppo 
4089cb112a14Slm66018 		D1(ldcp->id, "hv_ldc_tx_get_state returns 0x%x "
40901ae08745Sheppo 			"(head 0x%x, tail 0x%x state 0x%x)\n",
40911ae08745Sheppo 			rv2, tx_head, tx_tail, ldcp->link_state);
40921ae08745Sheppo 
40931ae08745Sheppo 		*size = 0;
40941ae08745Sheppo 	}
40951ae08745Sheppo 
40961ae08745Sheppo 	D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, *size);
40971ae08745Sheppo 
40981ae08745Sheppo 	return (rv);
40991ae08745Sheppo }
41001ae08745Sheppo 
41011ae08745Sheppo /*
41021ae08745Sheppo  * Write specified amount of bytes to the channel
41031ae08745Sheppo  * in multiple pkts of pkt_payload size. Each
41041ae08745Sheppo  * packet is tagged with an unique packet ID in
4105e1ebb9ecSlm66018  * the case of a reliable link.
41061ae08745Sheppo  *
41071ae08745Sheppo  * On return, size contains the number of bytes written.
41081ae08745Sheppo  * This function needs to ensure that the write size is < MTU size
41091ae08745Sheppo  */
41101ae08745Sheppo static int
41111ae08745Sheppo i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep)
41121ae08745Sheppo {
4113d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
41141ae08745Sheppo 	ASSERT(ldcp->mode == LDC_MODE_STREAM);
41151ae08745Sheppo 
41161ae08745Sheppo 	/* Truncate packet to max of MTU size */
41171ae08745Sheppo 	if (*sizep > ldcp->mtu) *sizep = ldcp->mtu;
41181ae08745Sheppo 	return (i_ldc_write_packet(ldcp, buf, sizep));
41191ae08745Sheppo }
41201ae08745Sheppo 
41211ae08745Sheppo 
41221ae08745Sheppo /*
41231ae08745Sheppo  * Interfaces for channel nexus to register/unregister with LDC module
41241ae08745Sheppo  * The nexus will register functions to be used to register individual
41251ae08745Sheppo  * channels with the nexus and enable interrupts for the channels
41261ae08745Sheppo  */
41271ae08745Sheppo int
41281ae08745Sheppo ldc_register(ldc_cnex_t *cinfo)
41291ae08745Sheppo {
41301ae08745Sheppo 	ldc_chan_t	*ldcp;
41311ae08745Sheppo 
41321ae08745Sheppo 	if (cinfo == NULL || cinfo->dip == NULL ||
41331ae08745Sheppo 	    cinfo->reg_chan == NULL || cinfo->unreg_chan == NULL ||
41341ae08745Sheppo 	    cinfo->add_intr == NULL || cinfo->rem_intr == NULL ||
41351ae08745Sheppo 	    cinfo->clr_intr == NULL) {
41361ae08745Sheppo 
41371ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_register: invalid nexus info\n");
41381ae08745Sheppo 		return (EINVAL);
41391ae08745Sheppo 	}
41401ae08745Sheppo 
41411ae08745Sheppo 	mutex_enter(&ldcssp->lock);
41421ae08745Sheppo 
41431ae08745Sheppo 	/* nexus registration */
41441ae08745Sheppo 	ldcssp->cinfo.dip = cinfo->dip;
41451ae08745Sheppo 	ldcssp->cinfo.reg_chan = cinfo->reg_chan;
41461ae08745Sheppo 	ldcssp->cinfo.unreg_chan = cinfo->unreg_chan;
41471ae08745Sheppo 	ldcssp->cinfo.add_intr = cinfo->add_intr;
41481ae08745Sheppo 	ldcssp->cinfo.rem_intr = cinfo->rem_intr;
41491ae08745Sheppo 	ldcssp->cinfo.clr_intr = cinfo->clr_intr;
41501ae08745Sheppo 
41511ae08745Sheppo 	/* register any channels that might have been previously initialized */
41521ae08745Sheppo 	ldcp = ldcssp->chan_list;
41531ae08745Sheppo 	while (ldcp) {
41541ae08745Sheppo 		if ((ldcp->tstate & TS_QCONF_RDY) &&
41551ae08745Sheppo 		    (ldcp->tstate & TS_CNEX_RDY) == 0)
41561ae08745Sheppo 			(void) i_ldc_register_channel(ldcp);
41571ae08745Sheppo 
41581ae08745Sheppo 		ldcp = ldcp->next;
41591ae08745Sheppo 	}
41601ae08745Sheppo 
41611ae08745Sheppo 	mutex_exit(&ldcssp->lock);
41621ae08745Sheppo 
41631ae08745Sheppo 	return (0);
41641ae08745Sheppo }
41651ae08745Sheppo 
41661ae08745Sheppo int
41671ae08745Sheppo ldc_unregister(ldc_cnex_t *cinfo)
41681ae08745Sheppo {
41691ae08745Sheppo 	if (cinfo == NULL || cinfo->dip == NULL) {
41701ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid nexus info\n");
41711ae08745Sheppo 		return (EINVAL);
41721ae08745Sheppo 	}
41731ae08745Sheppo 
41741ae08745Sheppo 	mutex_enter(&ldcssp->lock);
41751ae08745Sheppo 
41761ae08745Sheppo 	if (cinfo->dip != ldcssp->cinfo.dip) {
41771ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid dip\n");
41781ae08745Sheppo 		mutex_exit(&ldcssp->lock);
41791ae08745Sheppo 		return (EINVAL);
41801ae08745Sheppo 	}
41811ae08745Sheppo 
41821ae08745Sheppo 	/* nexus unregister */
41831ae08745Sheppo 	ldcssp->cinfo.dip = NULL;
41841ae08745Sheppo 	ldcssp->cinfo.reg_chan = NULL;
41851ae08745Sheppo 	ldcssp->cinfo.unreg_chan = NULL;
41861ae08745Sheppo 	ldcssp->cinfo.add_intr = NULL;
41871ae08745Sheppo 	ldcssp->cinfo.rem_intr = NULL;
41881ae08745Sheppo 	ldcssp->cinfo.clr_intr = NULL;
41891ae08745Sheppo 
41901ae08745Sheppo 	mutex_exit(&ldcssp->lock);
41911ae08745Sheppo 
41921ae08745Sheppo 	return (0);
41931ae08745Sheppo }
41941ae08745Sheppo 
41951ae08745Sheppo 
41961ae08745Sheppo /* ------------------------------------------------------------------------- */
41971ae08745Sheppo 
41981ae08745Sheppo /*
41991ae08745Sheppo  * Allocate a memory handle for the channel and link it into the list
42001ae08745Sheppo  * Also choose which memory table to use if this is the first handle
42011ae08745Sheppo  * being assigned to this channel
42021ae08745Sheppo  */
42031ae08745Sheppo int
42041ae08745Sheppo ldc_mem_alloc_handle(ldc_handle_t handle, ldc_mem_handle_t *mhandle)
42051ae08745Sheppo {
42061ae08745Sheppo 	ldc_chan_t 	*ldcp;
42071ae08745Sheppo 	ldc_mhdl_t	*mhdl;
42081ae08745Sheppo 
42091ae08745Sheppo 	if (handle == NULL) {
42101ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
42111ae08745Sheppo 		    "ldc_mem_alloc_handle: invalid channel handle\n");
42121ae08745Sheppo 		return (EINVAL);
42131ae08745Sheppo 	}
42141ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
42151ae08745Sheppo 
42161ae08745Sheppo 	mutex_enter(&ldcp->lock);
42171ae08745Sheppo 
42181ae08745Sheppo 	/* check to see if channel is initalized */
42193af08d82Slm66018 	if ((ldcp->tstate & ~TS_IN_RESET) < TS_INIT) {
42201ae08745Sheppo 		DWARN(ldcp->id,
42211ae08745Sheppo 		    "ldc_mem_alloc_handle: (0x%llx) channel not initialized\n",
42221ae08745Sheppo 		    ldcp->id);
42231ae08745Sheppo 		mutex_exit(&ldcp->lock);
42241ae08745Sheppo 		return (EINVAL);
42251ae08745Sheppo 	}
42261ae08745Sheppo 
42271ae08745Sheppo 	/* allocate handle for channel */
42284bac2208Snarayan 	mhdl = kmem_cache_alloc(ldcssp->memhdl_cache, KM_SLEEP);
42291ae08745Sheppo 
42301ae08745Sheppo 	/* initialize the lock */
42311ae08745Sheppo 	mutex_init(&mhdl->lock, NULL, MUTEX_DRIVER, NULL);
42321ae08745Sheppo 
42334bac2208Snarayan 	mhdl->myshadow = B_FALSE;
42344bac2208Snarayan 	mhdl->memseg = NULL;
42351ae08745Sheppo 	mhdl->ldcp = ldcp;
42364bac2208Snarayan 	mhdl->status = LDC_UNBOUND;
42371ae08745Sheppo 
42381ae08745Sheppo 	/* insert memory handle (@ head) into list */
42391ae08745Sheppo 	if (ldcp->mhdl_list == NULL) {
42401ae08745Sheppo 		ldcp->mhdl_list = mhdl;
42411ae08745Sheppo 		mhdl->next = NULL;
42421ae08745Sheppo 	} else {
42431ae08745Sheppo 		/* insert @ head */
42441ae08745Sheppo 		mhdl->next = ldcp->mhdl_list;
42451ae08745Sheppo 		ldcp->mhdl_list = mhdl;
42461ae08745Sheppo 	}
42471ae08745Sheppo 
42481ae08745Sheppo 	/* return the handle */
42491ae08745Sheppo 	*mhandle = (ldc_mem_handle_t)mhdl;
42501ae08745Sheppo 
42511ae08745Sheppo 	mutex_exit(&ldcp->lock);
42521ae08745Sheppo 
42531ae08745Sheppo 	D1(ldcp->id, "ldc_mem_alloc_handle: (0x%llx) allocated handle 0x%llx\n",
42541ae08745Sheppo 	    ldcp->id, mhdl);
42551ae08745Sheppo 
42561ae08745Sheppo 	return (0);
42571ae08745Sheppo }
42581ae08745Sheppo 
42591ae08745Sheppo /*
42601ae08745Sheppo  * Free memory handle for the channel and unlink it from the list
42611ae08745Sheppo  */
42621ae08745Sheppo int
42631ae08745Sheppo ldc_mem_free_handle(ldc_mem_handle_t mhandle)
42641ae08745Sheppo {
42651ae08745Sheppo 	ldc_mhdl_t 	*mhdl, *phdl;
42661ae08745Sheppo 	ldc_chan_t 	*ldcp;
42671ae08745Sheppo 
42681ae08745Sheppo 	if (mhandle == NULL) {
42691ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
42701ae08745Sheppo 		    "ldc_mem_free_handle: invalid memory handle\n");
42711ae08745Sheppo 		return (EINVAL);
42721ae08745Sheppo 	}
42731ae08745Sheppo 	mhdl = (ldc_mhdl_t *)mhandle;
42741ae08745Sheppo 
42751ae08745Sheppo 	mutex_enter(&mhdl->lock);
42761ae08745Sheppo 
42771ae08745Sheppo 	ldcp = mhdl->ldcp;
42781ae08745Sheppo 
42791ae08745Sheppo 	if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED) {
42801ae08745Sheppo 		DWARN(ldcp->id,
42811ae08745Sheppo 		    "ldc_mem_free_handle: cannot free, 0x%llx hdl bound\n",
42821ae08745Sheppo 		    mhdl);
42831ae08745Sheppo 		mutex_exit(&mhdl->lock);
42841ae08745Sheppo 		return (EINVAL);
42851ae08745Sheppo 	}
42861ae08745Sheppo 	mutex_exit(&mhdl->lock);
42871ae08745Sheppo 
42881ae08745Sheppo 	mutex_enter(&ldcp->mlist_lock);
42891ae08745Sheppo 
42901ae08745Sheppo 	phdl = ldcp->mhdl_list;
42911ae08745Sheppo 
42921ae08745Sheppo 	/* first handle */
42931ae08745Sheppo 	if (phdl == mhdl) {
42941ae08745Sheppo 		ldcp->mhdl_list = mhdl->next;
42951ae08745Sheppo 		mutex_destroy(&mhdl->lock);
42964bac2208Snarayan 		kmem_cache_free(ldcssp->memhdl_cache, mhdl);
42974bac2208Snarayan 
42981ae08745Sheppo 		D1(ldcp->id,
42991ae08745Sheppo 		    "ldc_mem_free_handle: (0x%llx) freed handle 0x%llx\n",
43001ae08745Sheppo 		    ldcp->id, mhdl);
43011ae08745Sheppo 	} else {
43021ae08745Sheppo 		/* walk the list - unlink and free */
43031ae08745Sheppo 		while (phdl != NULL) {
43041ae08745Sheppo 			if (phdl->next == mhdl) {
43051ae08745Sheppo 				phdl->next = mhdl->next;
43061ae08745Sheppo 				mutex_destroy(&mhdl->lock);
43074bac2208Snarayan 				kmem_cache_free(ldcssp->memhdl_cache, mhdl);
43081ae08745Sheppo 				D1(ldcp->id,
43091ae08745Sheppo 				    "ldc_mem_free_handle: (0x%llx) freed "
43101ae08745Sheppo 				    "handle 0x%llx\n", ldcp->id, mhdl);
43111ae08745Sheppo 				break;
43121ae08745Sheppo 			}
43131ae08745Sheppo 			phdl = phdl->next;
43141ae08745Sheppo 		}
43151ae08745Sheppo 	}
43161ae08745Sheppo 
43171ae08745Sheppo 	if (phdl == NULL) {
43181ae08745Sheppo 		DWARN(ldcp->id,
43191ae08745Sheppo 		    "ldc_mem_free_handle: invalid handle 0x%llx\n", mhdl);
43201ae08745Sheppo 		mutex_exit(&ldcp->mlist_lock);
43211ae08745Sheppo 		return (EINVAL);
43221ae08745Sheppo 	}
43231ae08745Sheppo 
43241ae08745Sheppo 	mutex_exit(&ldcp->mlist_lock);
43251ae08745Sheppo 
43261ae08745Sheppo 	return (0);
43271ae08745Sheppo }
43281ae08745Sheppo 
43291ae08745Sheppo /*
43301ae08745Sheppo  * Bind a memory handle to a virtual address.
43311ae08745Sheppo  * The virtual address is converted to the corresponding real addresses.
43321ae08745Sheppo  * Returns pointer to the first ldc_mem_cookie and the total number
43331ae08745Sheppo  * of cookies for this virtual address. Other cookies can be obtained
43341ae08745Sheppo  * using the ldc_mem_nextcookie() call. If the pages are stored in
43351ae08745Sheppo  * consecutive locations in the table, a single cookie corresponding to
43361ae08745Sheppo  * the first location is returned. The cookie size spans all the entries.
43371ae08745Sheppo  *
43381ae08745Sheppo  * If the VA corresponds to a page that is already being exported, reuse
43391ae08745Sheppo  * the page and do not export it again. Bump the page's use count.
43401ae08745Sheppo  */
43411ae08745Sheppo int
43421ae08745Sheppo ldc_mem_bind_handle(ldc_mem_handle_t mhandle, caddr_t vaddr, size_t len,
43431ae08745Sheppo     uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount)
43441ae08745Sheppo {
43451ae08745Sheppo 	ldc_mhdl_t	*mhdl;
43461ae08745Sheppo 	ldc_chan_t 	*ldcp;
43471ae08745Sheppo 	ldc_mtbl_t	*mtbl;
43481ae08745Sheppo 	ldc_memseg_t	*memseg;
43491ae08745Sheppo 	ldc_mte_t	tmp_mte;
43501ae08745Sheppo 	uint64_t	index, prev_index = 0;
43511ae08745Sheppo 	int64_t		cookie_idx;
43521ae08745Sheppo 	uintptr_t	raddr, ra_aligned;
43531ae08745Sheppo 	uint64_t	psize, poffset, v_offset;
43541ae08745Sheppo 	uint64_t	pg_shift, pg_size, pg_size_code, pg_mask;
43551ae08745Sheppo 	pgcnt_t		npages;
43561ae08745Sheppo 	caddr_t		v_align, addr;
43574bac2208Snarayan 	int 		i, rv;
43581ae08745Sheppo 
43591ae08745Sheppo 	if (mhandle == NULL) {
43601ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
43611ae08745Sheppo 		    "ldc_mem_bind_handle: invalid memory handle\n");
43621ae08745Sheppo 		return (EINVAL);
43631ae08745Sheppo 	}
43641ae08745Sheppo 	mhdl = (ldc_mhdl_t *)mhandle;
43651ae08745Sheppo 	ldcp = mhdl->ldcp;
43661ae08745Sheppo 
43671ae08745Sheppo 	/* clear count */
43681ae08745Sheppo 	*ccount = 0;
43691ae08745Sheppo 
43701ae08745Sheppo 	mutex_enter(&mhdl->lock);
43711ae08745Sheppo 
43721ae08745Sheppo 	if (mhdl->status == LDC_BOUND || mhdl->memseg != NULL) {
43731ae08745Sheppo 		DWARN(ldcp->id,
43741ae08745Sheppo 		    "ldc_mem_bind_handle: (0x%x) handle already bound\n",
43751ae08745Sheppo 		    mhandle);
43761ae08745Sheppo 		mutex_exit(&mhdl->lock);
43771ae08745Sheppo 		return (EINVAL);
43781ae08745Sheppo 	}
43791ae08745Sheppo 
43801ae08745Sheppo 	/* Force address and size to be 8-byte aligned */
43811ae08745Sheppo 	if ((((uintptr_t)vaddr | len) & 0x7) != 0) {
43821ae08745Sheppo 		DWARN(ldcp->id,
43831ae08745Sheppo 		    "ldc_mem_bind_handle: addr/size is not 8-byte aligned\n");
43841ae08745Sheppo 		mutex_exit(&mhdl->lock);
43851ae08745Sheppo 		return (EINVAL);
43861ae08745Sheppo 	}
43871ae08745Sheppo 
43884bac2208Snarayan 	/*
43894bac2208Snarayan 	 * If this channel is binding a memory handle for the
43904bac2208Snarayan 	 * first time allocate it a memory map table and initialize it
43914bac2208Snarayan 	 */
43924bac2208Snarayan 	if ((mtbl = ldcp->mtbl) == NULL) {
43934bac2208Snarayan 
43944bac2208Snarayan 		mutex_enter(&ldcp->lock);
43954bac2208Snarayan 
43964bac2208Snarayan 		/* Allocate and initialize the map table structure */
43974bac2208Snarayan 		mtbl = kmem_zalloc(sizeof (ldc_mtbl_t), KM_SLEEP);
43984bac2208Snarayan 		mtbl->num_entries = mtbl->num_avail = ldc_maptable_entries;
43994bac2208Snarayan 		mtbl->size = ldc_maptable_entries * sizeof (ldc_mte_slot_t);
44004bac2208Snarayan 		mtbl->next_entry = NULL;
44013af08d82Slm66018 		mtbl->contigmem = B_TRUE;
44024bac2208Snarayan 
44034bac2208Snarayan 		/* Allocate the table itself */
44044bac2208Snarayan 		mtbl->table = (ldc_mte_slot_t *)
44054bac2208Snarayan 			contig_mem_alloc_align(mtbl->size, MMU_PAGESIZE);
44064bac2208Snarayan 		if (mtbl->table == NULL) {
44073af08d82Slm66018 
44083af08d82Slm66018 			/* allocate a page of memory using kmem_alloc */
44093af08d82Slm66018 			mtbl->table = kmem_alloc(MMU_PAGESIZE, KM_SLEEP);
44103af08d82Slm66018 			mtbl->size = MMU_PAGESIZE;
44113af08d82Slm66018 			mtbl->contigmem = B_FALSE;
44123af08d82Slm66018 			mtbl->num_entries = mtbl->num_avail =
44133af08d82Slm66018 				mtbl->size / sizeof (ldc_mte_slot_t);
44143af08d82Slm66018 			DWARN(ldcp->id,
44153af08d82Slm66018 			    "ldc_mem_bind_handle: (0x%llx) reduced tbl size "
44163af08d82Slm66018 			    "to %lx entries\n", ldcp->id, mtbl->num_entries);
44174bac2208Snarayan 		}
44184bac2208Snarayan 
44194bac2208Snarayan 		/* zero out the memory */
44204bac2208Snarayan 		bzero(mtbl->table, mtbl->size);
44214bac2208Snarayan 
44224bac2208Snarayan 		/* initialize the lock */
44234bac2208Snarayan 		mutex_init(&mtbl->lock, NULL, MUTEX_DRIVER, NULL);
44244bac2208Snarayan 
44254bac2208Snarayan 		/* register table for this channel */
44264bac2208Snarayan 		rv = hv_ldc_set_map_table(ldcp->id,
44274bac2208Snarayan 		    va_to_pa(mtbl->table), mtbl->num_entries);
44284bac2208Snarayan 		if (rv != 0) {
44294bac2208Snarayan 			cmn_err(CE_WARN,
44304bac2208Snarayan 			    "ldc_mem_bind_handle: (0x%lx) err %d mapping tbl",
44314bac2208Snarayan 			    ldcp->id, rv);
44323af08d82Slm66018 			if (mtbl->contigmem)
44334bac2208Snarayan 				contig_mem_free(mtbl->table, mtbl->size);
44343af08d82Slm66018 			else
44353af08d82Slm66018 				kmem_free(mtbl->table, mtbl->size);
44364bac2208Snarayan 			mutex_destroy(&mtbl->lock);
44374bac2208Snarayan 			kmem_free(mtbl, sizeof (ldc_mtbl_t));
44384bac2208Snarayan 			mutex_exit(&ldcp->lock);
44394bac2208Snarayan 			mutex_exit(&mhdl->lock);
44404bac2208Snarayan 			return (EIO);
44414bac2208Snarayan 		}
44424bac2208Snarayan 
44434bac2208Snarayan 		ldcp->mtbl = mtbl;
44444bac2208Snarayan 		mutex_exit(&ldcp->lock);
44454bac2208Snarayan 
44464bac2208Snarayan 		D1(ldcp->id,
44474bac2208Snarayan 		    "ldc_mem_bind_handle: (0x%llx) alloc'd map table 0x%llx\n",
44484bac2208Snarayan 		    ldcp->id, ldcp->mtbl->table);
44494bac2208Snarayan 	}
44504bac2208Snarayan 
44511ae08745Sheppo 	/* FUTURE: get the page size, pgsz code, and shift */
44521ae08745Sheppo 	pg_size = MMU_PAGESIZE;
44531ae08745Sheppo 	pg_size_code = page_szc(pg_size);
44541ae08745Sheppo 	pg_shift = page_get_shift(pg_size_code);
44551ae08745Sheppo 	pg_mask = ~(pg_size - 1);
44561ae08745Sheppo 
44571ae08745Sheppo 	D1(ldcp->id, "ldc_mem_bind_handle: (0x%llx) binding "
44581ae08745Sheppo 	    "va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n",
44591ae08745Sheppo 	    ldcp->id, vaddr, pg_size, pg_size_code, pg_shift);
44601ae08745Sheppo 
44611ae08745Sheppo 	/* aligned VA and its offset */
44621ae08745Sheppo 	v_align = (caddr_t)(((uintptr_t)vaddr) & ~(pg_size - 1));
44631ae08745Sheppo 	v_offset = ((uintptr_t)vaddr) & (pg_size - 1);
44641ae08745Sheppo 
44651ae08745Sheppo 	npages = (len+v_offset)/pg_size;
44661ae08745Sheppo 	npages = ((len+v_offset)%pg_size == 0) ? npages : npages+1;
44671ae08745Sheppo 
44681ae08745Sheppo 	D1(ldcp->id, "ldc_mem_bind_handle: binding "
44691ae08745Sheppo 	    "(0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n",
44701ae08745Sheppo 	    ldcp->id, vaddr, v_align, v_offset, npages);
44711ae08745Sheppo 
44721ae08745Sheppo 	/* lock the memory table - exclusive access to channel */
44731ae08745Sheppo 	mutex_enter(&mtbl->lock);
44741ae08745Sheppo 
44751ae08745Sheppo 	if (npages > mtbl->num_avail) {
44763af08d82Slm66018 		D1(ldcp->id, "ldc_mem_bind_handle: (0x%llx) no table entries\n",
44771ae08745Sheppo 		    ldcp->id);
44781ae08745Sheppo 		mutex_exit(&mtbl->lock);
44791ae08745Sheppo 		mutex_exit(&mhdl->lock);
44801ae08745Sheppo 		return (ENOMEM);
44811ae08745Sheppo 	}
44821ae08745Sheppo 
44831ae08745Sheppo 	/* Allocate a memseg structure */
44844bac2208Snarayan 	memseg = mhdl->memseg =
44854bac2208Snarayan 		kmem_cache_alloc(ldcssp->memseg_cache, KM_SLEEP);
44861ae08745Sheppo 
44871ae08745Sheppo 	/* Allocate memory to store all pages and cookies */
44881ae08745Sheppo 	memseg->pages = kmem_zalloc((sizeof (ldc_page_t) * npages), KM_SLEEP);
44891ae08745Sheppo 	memseg->cookies =
44901ae08745Sheppo 		kmem_zalloc((sizeof (ldc_mem_cookie_t) * npages), KM_SLEEP);
44911ae08745Sheppo 
44921ae08745Sheppo 	D2(ldcp->id, "ldc_mem_bind_handle: (0x%llx) processing 0x%llx pages\n",
44931ae08745Sheppo 	    ldcp->id, npages);
44941ae08745Sheppo 
44951ae08745Sheppo 	addr = v_align;
44961ae08745Sheppo 
44971ae08745Sheppo 	/*
44984bac2208Snarayan 	 * Check if direct shared memory map is enabled, if not change
44994bac2208Snarayan 	 * the mapping type to include SHADOW_MAP.
45004bac2208Snarayan 	 */
45014bac2208Snarayan 	if (ldc_shmem_enabled == 0)
45024bac2208Snarayan 		mtype = LDC_SHADOW_MAP;
45034bac2208Snarayan 
45044bac2208Snarayan 	/*
45051ae08745Sheppo 	 * Table slots are used in a round-robin manner. The algorithm permits
45061ae08745Sheppo 	 * inserting duplicate entries. Slots allocated earlier will typically
45071ae08745Sheppo 	 * get freed before we get back to reusing the slot.Inserting duplicate
45081ae08745Sheppo 	 * entries should be OK as we only lookup entries using the cookie addr
45091ae08745Sheppo 	 * i.e. tbl index, during export, unexport and copy operation.
45101ae08745Sheppo 	 *
45111ae08745Sheppo 	 * One implementation what was tried was to search for a duplicate
45121ae08745Sheppo 	 * page entry first and reuse it. The search overhead is very high and
45131ae08745Sheppo 	 * in the vnet case dropped the perf by almost half, 50 to 24 mbps.
45141ae08745Sheppo 	 * So it does make sense to avoid searching for duplicates.
45151ae08745Sheppo 	 *
45161ae08745Sheppo 	 * But during the process of searching for a free slot, if we find a
45171ae08745Sheppo 	 * duplicate entry we will go ahead and use it, and bump its use count.
45181ae08745Sheppo 	 */
45191ae08745Sheppo 
45201ae08745Sheppo 	/* index to start searching from */
45211ae08745Sheppo 	index = mtbl->next_entry;
45221ae08745Sheppo 	cookie_idx = -1;
45231ae08745Sheppo 
45241ae08745Sheppo 	tmp_mte.ll = 0;	/* initialise fields to 0 */
45251ae08745Sheppo 
45261ae08745Sheppo 	if (mtype & LDC_DIRECT_MAP) {
45271ae08745Sheppo 		tmp_mte.mte_r = (perm & LDC_MEM_R) ? 1 : 0;
45281ae08745Sheppo 		tmp_mte.mte_w = (perm & LDC_MEM_W) ? 1 : 0;
45291ae08745Sheppo 		tmp_mte.mte_x = (perm & LDC_MEM_X) ? 1 : 0;
45301ae08745Sheppo 	}
45311ae08745Sheppo 
45321ae08745Sheppo 	if (mtype & LDC_SHADOW_MAP) {
45331ae08745Sheppo 		tmp_mte.mte_cr = (perm & LDC_MEM_R) ? 1 : 0;
45341ae08745Sheppo 		tmp_mte.mte_cw = (perm & LDC_MEM_W) ? 1 : 0;
45351ae08745Sheppo 	}
45361ae08745Sheppo 
45371ae08745Sheppo 	if (mtype & LDC_IO_MAP) {
45381ae08745Sheppo 		tmp_mte.mte_ir = (perm & LDC_MEM_R) ? 1 : 0;
45391ae08745Sheppo 		tmp_mte.mte_iw = (perm & LDC_MEM_W) ? 1 : 0;
45401ae08745Sheppo 	}
45411ae08745Sheppo 
45421ae08745Sheppo 	D1(ldcp->id, "ldc_mem_bind_handle mte=0x%llx\n", tmp_mte.ll);
45431ae08745Sheppo 
45441ae08745Sheppo 	tmp_mte.mte_pgszc = pg_size_code;
45451ae08745Sheppo 
45461ae08745Sheppo 	/* initialize each mem table entry */
45471ae08745Sheppo 	for (i = 0; i < npages; i++) {
45481ae08745Sheppo 
45491ae08745Sheppo 		/* check if slot is available in the table */
45501ae08745Sheppo 		while (mtbl->table[index].entry.ll != 0) {
45511ae08745Sheppo 
45521ae08745Sheppo 			index = (index + 1) % mtbl->num_entries;
45531ae08745Sheppo 
45541ae08745Sheppo 			if (index == mtbl->next_entry) {
45551ae08745Sheppo 				/* we have looped around */
45561ae08745Sheppo 				DWARN(DBG_ALL_LDCS,
45571ae08745Sheppo 				    "ldc_mem_bind_handle: (0x%llx) cannot find "
45581ae08745Sheppo 				    "entry\n", ldcp->id);
45591ae08745Sheppo 				*ccount = 0;
45601ae08745Sheppo 
45611ae08745Sheppo 				/* NOTE: free memory, remove previous entries */
45621ae08745Sheppo 				/* this shouldnt happen as num_avail was ok */
45631ae08745Sheppo 
45641ae08745Sheppo 				mutex_exit(&mtbl->lock);
45651ae08745Sheppo 				mutex_exit(&mhdl->lock);
45661ae08745Sheppo 				return (ENOMEM);
45671ae08745Sheppo 			}
45681ae08745Sheppo 		}
45691ae08745Sheppo 
45701ae08745Sheppo 		/* get the real address */
45711ae08745Sheppo 		raddr = va_to_pa((void *)addr);
45721ae08745Sheppo 		ra_aligned = ((uintptr_t)raddr & pg_mask);
45731ae08745Sheppo 
45741ae08745Sheppo 		/* build the mte */
45751ae08745Sheppo 		tmp_mte.mte_rpfn = ra_aligned >> pg_shift;
45761ae08745Sheppo 
45771ae08745Sheppo 		D1(ldcp->id, "ldc_mem_bind_handle mte=0x%llx\n", tmp_mte.ll);
45781ae08745Sheppo 
45791ae08745Sheppo 		/* update entry in table */
45801ae08745Sheppo 		mtbl->table[index].entry = tmp_mte;
45811ae08745Sheppo 
45821ae08745Sheppo 		D2(ldcp->id, "ldc_mem_bind_handle: (0x%llx) stored MTE 0x%llx"
45831ae08745Sheppo 		    " into loc 0x%llx\n", ldcp->id, tmp_mte.ll, index);
45841ae08745Sheppo 
45851ae08745Sheppo 		/* calculate the size and offset for this export range */
45861ae08745Sheppo 		if (i == 0) {
45871ae08745Sheppo 			/* first page */
45881ae08745Sheppo 			psize = min((pg_size - v_offset), len);
45891ae08745Sheppo 			poffset = v_offset;
45901ae08745Sheppo 
45911ae08745Sheppo 		} else if (i == (npages - 1)) {
45921ae08745Sheppo 			/* last page */
45931ae08745Sheppo 			psize =	(((uintptr_t)(vaddr + len)) &
45941ae08745Sheppo 				    ((uint64_t)(pg_size-1)));
45951ae08745Sheppo 			if (psize == 0)
45961ae08745Sheppo 				psize = pg_size;
45971ae08745Sheppo 			poffset = 0;
45981ae08745Sheppo 
45991ae08745Sheppo 		} else {
46001ae08745Sheppo 			/* middle pages */
46011ae08745Sheppo 			psize = pg_size;
46021ae08745Sheppo 			poffset = 0;
46031ae08745Sheppo 		}
46041ae08745Sheppo 
46051ae08745Sheppo 		/* store entry for this page */
46061ae08745Sheppo 		memseg->pages[i].index = index;
46071ae08745Sheppo 		memseg->pages[i].raddr = raddr;
46081ae08745Sheppo 		memseg->pages[i].offset = poffset;
46091ae08745Sheppo 		memseg->pages[i].size = psize;
46101ae08745Sheppo 		memseg->pages[i].mte = &(mtbl->table[index]);
46111ae08745Sheppo 
46121ae08745Sheppo 		/* create the cookie */
46131ae08745Sheppo 		if (i == 0 || (index != prev_index + 1)) {
46141ae08745Sheppo 			cookie_idx++;
46151ae08745Sheppo 			memseg->cookies[cookie_idx].addr =
46161ae08745Sheppo 				IDX2COOKIE(index, pg_size_code, pg_shift);
46171ae08745Sheppo 			memseg->cookies[cookie_idx].addr |= poffset;
46181ae08745Sheppo 			memseg->cookies[cookie_idx].size = psize;
46191ae08745Sheppo 
46201ae08745Sheppo 		} else {
46211ae08745Sheppo 			memseg->cookies[cookie_idx].size += psize;
46221ae08745Sheppo 		}
46231ae08745Sheppo 
46241ae08745Sheppo 		D1(ldcp->id, "ldc_mem_bind_handle: bound "
46251ae08745Sheppo 		    "(0x%llx) va=0x%llx, idx=0x%llx, "
46261ae08745Sheppo 		    "ra=0x%llx(sz=0x%x,off=0x%x)\n",
46271ae08745Sheppo 		    ldcp->id, addr, index, raddr, psize, poffset);
46281ae08745Sheppo 
46291ae08745Sheppo 		/* decrement number of available entries */
46301ae08745Sheppo 		mtbl->num_avail--;
46311ae08745Sheppo 
46321ae08745Sheppo 		/* increment va by page size */
46331ae08745Sheppo 		addr += pg_size;
46341ae08745Sheppo 
46351ae08745Sheppo 		/* increment index */
46361ae08745Sheppo 		prev_index = index;
46371ae08745Sheppo 		index = (index + 1) % mtbl->num_entries;
46381ae08745Sheppo 
46391ae08745Sheppo 		/* save the next slot */
46401ae08745Sheppo 		mtbl->next_entry = index;
46411ae08745Sheppo 	}
46421ae08745Sheppo 
46431ae08745Sheppo 	mutex_exit(&mtbl->lock);
46441ae08745Sheppo 
46451ae08745Sheppo 	/* memory handle = bound */
46461ae08745Sheppo 	mhdl->mtype = mtype;
46471ae08745Sheppo 	mhdl->perm = perm;
46481ae08745Sheppo 	mhdl->status = LDC_BOUND;
46491ae08745Sheppo 
46501ae08745Sheppo 	/* update memseg_t */
46511ae08745Sheppo 	memseg->vaddr = vaddr;
46521ae08745Sheppo 	memseg->raddr = memseg->pages[0].raddr;
46531ae08745Sheppo 	memseg->size = len;
46541ae08745Sheppo 	memseg->npages = npages;
46551ae08745Sheppo 	memseg->ncookies = cookie_idx + 1;
46561ae08745Sheppo 	memseg->next_cookie = (memseg->ncookies > 1) ? 1 : 0;
46571ae08745Sheppo 
46581ae08745Sheppo 	/* return count and first cookie */
46591ae08745Sheppo 	*ccount = memseg->ncookies;
46601ae08745Sheppo 	cookie->addr = memseg->cookies[0].addr;
46611ae08745Sheppo 	cookie->size = memseg->cookies[0].size;
46621ae08745Sheppo 
46631ae08745Sheppo 	D1(ldcp->id,
46641ae08745Sheppo 	    "ldc_mem_bind_handle: (0x%llx) bound 0x%llx, va=0x%llx, "
46651ae08745Sheppo 	    "pgs=0x%llx cookies=0x%llx\n",
46661ae08745Sheppo 	    ldcp->id, mhdl, vaddr, npages, memseg->ncookies);
46671ae08745Sheppo 
46681ae08745Sheppo 	mutex_exit(&mhdl->lock);
46691ae08745Sheppo 	return (0);
46701ae08745Sheppo }
46711ae08745Sheppo 
46721ae08745Sheppo /*
46731ae08745Sheppo  * Return the next cookie associated with the specified memory handle
46741ae08745Sheppo  */
46751ae08745Sheppo int
46761ae08745Sheppo ldc_mem_nextcookie(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie)
46771ae08745Sheppo {
46781ae08745Sheppo 	ldc_mhdl_t	*mhdl;
46791ae08745Sheppo 	ldc_chan_t 	*ldcp;
46801ae08745Sheppo 	ldc_memseg_t	*memseg;
46811ae08745Sheppo 
46821ae08745Sheppo 	if (mhandle == NULL) {
46831ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
46841ae08745Sheppo 		    "ldc_mem_nextcookie: invalid memory handle\n");
46851ae08745Sheppo 		return (EINVAL);
46861ae08745Sheppo 	}
46871ae08745Sheppo 	mhdl = (ldc_mhdl_t *)mhandle;
46881ae08745Sheppo 
46891ae08745Sheppo 	mutex_enter(&mhdl->lock);
46901ae08745Sheppo 
46911ae08745Sheppo 	ldcp = mhdl->ldcp;
46921ae08745Sheppo 	memseg = mhdl->memseg;
46931ae08745Sheppo 
46941ae08745Sheppo 	if (cookie == 0) {
46951ae08745Sheppo 		DWARN(ldcp->id,
46961ae08745Sheppo 		    "ldc_mem_nextcookie:(0x%llx) invalid cookie arg\n",
46971ae08745Sheppo 		    ldcp->id);
46981ae08745Sheppo 		mutex_exit(&mhdl->lock);
46991ae08745Sheppo 		return (EINVAL);
47001ae08745Sheppo 	}
47011ae08745Sheppo 
47021ae08745Sheppo 	if (memseg->next_cookie != 0) {
47031ae08745Sheppo 		cookie->addr = memseg->cookies[memseg->next_cookie].addr;
47041ae08745Sheppo 		cookie->size = memseg->cookies[memseg->next_cookie].size;
47051ae08745Sheppo 		memseg->next_cookie++;
47061ae08745Sheppo 		if (memseg->next_cookie == memseg->ncookies)
47071ae08745Sheppo 			memseg->next_cookie = 0;
47081ae08745Sheppo 
47091ae08745Sheppo 	} else {
47101ae08745Sheppo 		DWARN(ldcp->id,
47111ae08745Sheppo 		    "ldc_mem_nextcookie:(0x%llx) no more cookies\n", ldcp->id);
47121ae08745Sheppo 		cookie->addr = 0;
47131ae08745Sheppo 		cookie->size = 0;
47141ae08745Sheppo 		mutex_exit(&mhdl->lock);
47151ae08745Sheppo 		return (EINVAL);
47161ae08745Sheppo 	}
47171ae08745Sheppo 
47181ae08745Sheppo 	D1(ldcp->id,
47191ae08745Sheppo 	    "ldc_mem_nextcookie: (0x%llx) cookie addr=0x%llx,sz=0x%llx\n",
47201ae08745Sheppo 	    ldcp->id, cookie->addr, cookie->size);
47211ae08745Sheppo 
47221ae08745Sheppo 	mutex_exit(&mhdl->lock);
47231ae08745Sheppo 	return (0);
47241ae08745Sheppo }
47251ae08745Sheppo 
47261ae08745Sheppo /*
47271ae08745Sheppo  * Unbind the virtual memory region associated with the specified
47281ae08745Sheppo  * memory handle. Allassociated cookies are freed and the corresponding
47291ae08745Sheppo  * RA space is no longer exported.
47301ae08745Sheppo  */
47311ae08745Sheppo int
47321ae08745Sheppo ldc_mem_unbind_handle(ldc_mem_handle_t mhandle)
47331ae08745Sheppo {
47341ae08745Sheppo 	ldc_mhdl_t	*mhdl;
47351ae08745Sheppo 	ldc_chan_t 	*ldcp;
47361ae08745Sheppo 	ldc_mtbl_t	*mtbl;
47371ae08745Sheppo 	ldc_memseg_t	*memseg;
47384bac2208Snarayan 	uint64_t	cookie_addr;
47394bac2208Snarayan 	uint64_t	pg_shift, pg_size_code;
47404bac2208Snarayan 	int		i, rv;
47411ae08745Sheppo 
47421ae08745Sheppo 	if (mhandle == NULL) {
47431ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
47441ae08745Sheppo 		    "ldc_mem_unbind_handle: invalid memory handle\n");
47451ae08745Sheppo 		return (EINVAL);
47461ae08745Sheppo 	}
47471ae08745Sheppo 	mhdl = (ldc_mhdl_t *)mhandle;
47481ae08745Sheppo 
47491ae08745Sheppo 	mutex_enter(&mhdl->lock);
47501ae08745Sheppo 
47511ae08745Sheppo 	if (mhdl->status == LDC_UNBOUND) {
47521ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
47531ae08745Sheppo 		    "ldc_mem_unbind_handle: (0x%x) handle is not bound\n",
47541ae08745Sheppo 		    mhandle);
47551ae08745Sheppo 		mutex_exit(&mhdl->lock);
47561ae08745Sheppo 		return (EINVAL);
47571ae08745Sheppo 	}
47581ae08745Sheppo 
47591ae08745Sheppo 	ldcp = mhdl->ldcp;
47601ae08745Sheppo 	mtbl = ldcp->mtbl;
47611ae08745Sheppo 
47621ae08745Sheppo 	memseg = mhdl->memseg;
47631ae08745Sheppo 
47641ae08745Sheppo 	/* lock the memory table - exclusive access to channel */
47651ae08745Sheppo 	mutex_enter(&mtbl->lock);
47661ae08745Sheppo 
47671ae08745Sheppo 	/* undo the pages exported */
47681ae08745Sheppo 	for (i = 0; i < memseg->npages; i++) {
47691ae08745Sheppo 
47704bac2208Snarayan 		/* check for mapped pages, revocation cookie != 0 */
47711ae08745Sheppo 		if (memseg->pages[i].mte->cookie) {
47724bac2208Snarayan 
47734bac2208Snarayan 			pg_size_code = page_szc(memseg->pages[i].size);
47744bac2208Snarayan 			pg_shift = page_get_shift(memseg->pages[i].size);
47754bac2208Snarayan 			cookie_addr = IDX2COOKIE(memseg->pages[i].index,
47764bac2208Snarayan 			    pg_size_code, pg_shift);
47774bac2208Snarayan 
47784bac2208Snarayan 			D1(ldcp->id, "ldc_mem_unbind_handle: (0x%llx) revoke "
47794bac2208Snarayan 			    "cookie 0x%llx, rcookie 0x%llx\n", ldcp->id,
47804bac2208Snarayan 			    cookie_addr, memseg->pages[i].mte->cookie);
47814bac2208Snarayan 			rv = hv_ldc_revoke(ldcp->id, cookie_addr,
47824bac2208Snarayan 			    memseg->pages[i].mte->cookie);
47834bac2208Snarayan 			if (rv) {
47844bac2208Snarayan 				DWARN(ldcp->id,
47854bac2208Snarayan 				    "ldc_mem_unbind_handle: (0x%llx) cannot "
47864bac2208Snarayan 				    "revoke mapping, cookie %llx\n", ldcp->id,
47874bac2208Snarayan 				    cookie_addr);
47884bac2208Snarayan 			}
47891ae08745Sheppo 		}
47901ae08745Sheppo 
47911ae08745Sheppo 		/* clear the entry from the table */
47921ae08745Sheppo 		memseg->pages[i].mte->entry.ll = 0;
47931ae08745Sheppo 		mtbl->num_avail++;
47941ae08745Sheppo 	}
47951ae08745Sheppo 	mutex_exit(&mtbl->lock);
47961ae08745Sheppo 
47971ae08745Sheppo 	/* free the allocated memseg and page structures */
47981ae08745Sheppo 	kmem_free(memseg->pages, (sizeof (ldc_page_t) * memseg->npages));
47991ae08745Sheppo 	kmem_free(memseg->cookies,
48001ae08745Sheppo 	    (sizeof (ldc_mem_cookie_t) * memseg->npages));
48014bac2208Snarayan 	kmem_cache_free(ldcssp->memseg_cache, memseg);
48021ae08745Sheppo 
48031ae08745Sheppo 	/* uninitialize the memory handle */
48041ae08745Sheppo 	mhdl->memseg = NULL;
48051ae08745Sheppo 	mhdl->status = LDC_UNBOUND;
48061ae08745Sheppo 
48071ae08745Sheppo 	D1(ldcp->id, "ldc_mem_unbind_handle: (0x%llx) unbound handle 0x%llx\n",
48081ae08745Sheppo 	    ldcp->id, mhdl);
48091ae08745Sheppo 
48101ae08745Sheppo 	mutex_exit(&mhdl->lock);
48111ae08745Sheppo 	return (0);
48121ae08745Sheppo }
48131ae08745Sheppo 
48141ae08745Sheppo /*
48151ae08745Sheppo  * Get information about the dring. The base address of the descriptor
48161ae08745Sheppo  * ring along with the type and permission are returned back.
48171ae08745Sheppo  */
48181ae08745Sheppo int
48191ae08745Sheppo ldc_mem_info(ldc_mem_handle_t mhandle, ldc_mem_info_t *minfo)
48201ae08745Sheppo {
48211ae08745Sheppo 	ldc_mhdl_t	*mhdl;
48221ae08745Sheppo 
48231ae08745Sheppo 	if (mhandle == NULL) {
48241ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_mem_info: invalid memory handle\n");
48251ae08745Sheppo 		return (EINVAL);
48261ae08745Sheppo 	}
48271ae08745Sheppo 	mhdl = (ldc_mhdl_t *)mhandle;
48281ae08745Sheppo 
48291ae08745Sheppo 	if (minfo == NULL) {
48301ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_mem_info: invalid args\n");
48311ae08745Sheppo 		return (EINVAL);
48321ae08745Sheppo 	}
48331ae08745Sheppo 
48341ae08745Sheppo 	mutex_enter(&mhdl->lock);
48351ae08745Sheppo 
48361ae08745Sheppo 	minfo->status = mhdl->status;
48371ae08745Sheppo 	if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED) {
48381ae08745Sheppo 		minfo->vaddr = mhdl->memseg->vaddr;
48391ae08745Sheppo 		minfo->raddr = mhdl->memseg->raddr;
48401ae08745Sheppo 		minfo->mtype = mhdl->mtype;
48411ae08745Sheppo 		minfo->perm = mhdl->perm;
48421ae08745Sheppo 	}
48431ae08745Sheppo 	mutex_exit(&mhdl->lock);
48441ae08745Sheppo 
48451ae08745Sheppo 	return (0);
48461ae08745Sheppo }
48471ae08745Sheppo 
48481ae08745Sheppo /*
48491ae08745Sheppo  * Copy data either from or to the client specified virtual address
48501ae08745Sheppo  * space to or from the exported memory associated with the cookies.
48511ae08745Sheppo  * The direction argument determines whether the data is read from or
48521ae08745Sheppo  * written to exported memory.
48531ae08745Sheppo  */
48541ae08745Sheppo int
48551ae08745Sheppo ldc_mem_copy(ldc_handle_t handle, caddr_t vaddr, uint64_t off, size_t *size,
48561ae08745Sheppo     ldc_mem_cookie_t *cookies, uint32_t ccount, uint8_t direction)
48571ae08745Sheppo {
48581ae08745Sheppo 	ldc_chan_t 	*ldcp;
48591ae08745Sheppo 	uint64_t	local_voff, local_valign;
48601ae08745Sheppo 	uint64_t	cookie_addr, cookie_size;
48611ae08745Sheppo 	uint64_t	pg_shift, pg_size, pg_size_code;
48621ae08745Sheppo 	uint64_t 	export_caddr, export_poff, export_psize, export_size;
48631ae08745Sheppo 	uint64_t	local_ra, local_poff, local_psize;
48641ae08745Sheppo 	uint64_t	copy_size, copied_len = 0, total_bal = 0, idx = 0;
48651ae08745Sheppo 	pgcnt_t		npages;
48661ae08745Sheppo 	size_t		len = *size;
48671ae08745Sheppo 	int 		i, rv = 0;
48681ae08745Sheppo 
48693af08d82Slm66018 	uint64_t	chid;
48703af08d82Slm66018 
48711ae08745Sheppo 	if (handle == NULL) {
48721ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_mem_copy: invalid channel handle\n");
48731ae08745Sheppo 		return (EINVAL);
48741ae08745Sheppo 	}
48751ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
48763af08d82Slm66018 	chid = ldcp->id;
48771ae08745Sheppo 
48781ae08745Sheppo 	/* check to see if channel is UP */
48791ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
48803af08d82Slm66018 		DWARN(chid, "ldc_mem_copy: (0x%llx) channel is not UP\n",
48813af08d82Slm66018 		    chid);
48823af08d82Slm66018 		return (ECONNRESET);
48831ae08745Sheppo 	}
48841ae08745Sheppo 
48851ae08745Sheppo 	/* Force address and size to be 8-byte aligned */
48861ae08745Sheppo 	if ((((uintptr_t)vaddr | len) & 0x7) != 0) {
48873af08d82Slm66018 		DWARN(chid,
48881ae08745Sheppo 		    "ldc_mem_copy: addr/sz is not 8-byte aligned\n");
48891ae08745Sheppo 		return (EINVAL);
48901ae08745Sheppo 	}
48911ae08745Sheppo 
48921ae08745Sheppo 	/* Find the size of the exported memory */
48931ae08745Sheppo 	export_size = 0;
48941ae08745Sheppo 	for (i = 0; i < ccount; i++)
48951ae08745Sheppo 		export_size += cookies[i].size;
48961ae08745Sheppo 
48971ae08745Sheppo 	/* check to see if offset is valid */
48981ae08745Sheppo 	if (off > export_size) {
48993af08d82Slm66018 		DWARN(chid,
49001ae08745Sheppo 		    "ldc_mem_copy: (0x%llx) start offset > export mem size\n",
49013af08d82Slm66018 		    chid);
49021ae08745Sheppo 		return (EINVAL);
49031ae08745Sheppo 	}
49041ae08745Sheppo 
49051ae08745Sheppo 	/*
49061ae08745Sheppo 	 * Check to see if the export size is smaller than the size we
49071ae08745Sheppo 	 * are requesting to copy - if so flag an error
49081ae08745Sheppo 	 */
49091ae08745Sheppo 	if ((export_size - off) < *size) {
49103af08d82Slm66018 		DWARN(chid,
49111ae08745Sheppo 		    "ldc_mem_copy: (0x%llx) copy size > export mem size\n",
49123af08d82Slm66018 		    chid);
49131ae08745Sheppo 		return (EINVAL);
49141ae08745Sheppo 	}
49151ae08745Sheppo 
49161ae08745Sheppo 	total_bal = min(export_size, *size);
49171ae08745Sheppo 
49181ae08745Sheppo 	/* FUTURE: get the page size, pgsz code, and shift */
49191ae08745Sheppo 	pg_size = MMU_PAGESIZE;
49201ae08745Sheppo 	pg_size_code = page_szc(pg_size);
49211ae08745Sheppo 	pg_shift = page_get_shift(pg_size_code);
49221ae08745Sheppo 
49233af08d82Slm66018 	D1(chid, "ldc_mem_copy: copying data "
49241ae08745Sheppo 	    "(0x%llx) va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n",
49253af08d82Slm66018 	    chid, vaddr, pg_size, pg_size_code, pg_shift);
49261ae08745Sheppo 
49271ae08745Sheppo 	/* aligned VA and its offset */
49281ae08745Sheppo 	local_valign = (((uintptr_t)vaddr) & ~(pg_size - 1));
49291ae08745Sheppo 	local_voff = ((uintptr_t)vaddr) & (pg_size - 1);
49301ae08745Sheppo 
49311ae08745Sheppo 	npages = (len+local_voff)/pg_size;
49321ae08745Sheppo 	npages = ((len+local_voff)%pg_size == 0) ? npages : npages+1;
49331ae08745Sheppo 
49343af08d82Slm66018 	D1(chid,
49351ae08745Sheppo 	    "ldc_mem_copy: (0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n",
49363af08d82Slm66018 	    chid, vaddr, local_valign, local_voff, npages);
49371ae08745Sheppo 
49381ae08745Sheppo 	local_ra = va_to_pa((void *)local_valign);
49391ae08745Sheppo 	local_poff = local_voff;
49401ae08745Sheppo 	local_psize = min(len, (pg_size - local_voff));
49411ae08745Sheppo 
49421ae08745Sheppo 	len -= local_psize;
49431ae08745Sheppo 
49441ae08745Sheppo 	/*
49451ae08745Sheppo 	 * find the first cookie in the list of cookies
49461ae08745Sheppo 	 * if the offset passed in is not zero
49471ae08745Sheppo 	 */
49481ae08745Sheppo 	for (idx = 0; idx < ccount; idx++) {
49491ae08745Sheppo 		cookie_size = cookies[idx].size;
49501ae08745Sheppo 		if (off < cookie_size)
49511ae08745Sheppo 			break;
49521ae08745Sheppo 		off -= cookie_size;
49531ae08745Sheppo 	}
49541ae08745Sheppo 
49551ae08745Sheppo 	cookie_addr = cookies[idx].addr + off;
49561ae08745Sheppo 	cookie_size = cookies[idx].size - off;
49571ae08745Sheppo 
49581ae08745Sheppo 	export_caddr = cookie_addr & ~(pg_size - 1);
49591ae08745Sheppo 	export_poff = cookie_addr & (pg_size - 1);
49601ae08745Sheppo 	export_psize = min(cookie_size, (pg_size - export_poff));
49611ae08745Sheppo 
49621ae08745Sheppo 	for (;;) {
49631ae08745Sheppo 
49641ae08745Sheppo 		copy_size = min(export_psize, local_psize);
49651ae08745Sheppo 
49663af08d82Slm66018 		D1(chid,
49671ae08745Sheppo 		    "ldc_mem_copy:(0x%llx) dir=0x%x, caddr=0x%llx,"
49681ae08745Sheppo 		    " loc_ra=0x%llx, exp_poff=0x%llx, loc_poff=0x%llx,"
49691ae08745Sheppo 		    " exp_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx,"
49701ae08745Sheppo 		    " total_bal=0x%llx\n",
49713af08d82Slm66018 		    chid, direction, export_caddr, local_ra, export_poff,
49721ae08745Sheppo 		    local_poff, export_psize, local_psize, copy_size,
49731ae08745Sheppo 		    total_bal);
49741ae08745Sheppo 
49753af08d82Slm66018 		rv = hv_ldc_copy(chid, direction,
49761ae08745Sheppo 		    (export_caddr + export_poff), (local_ra + local_poff),
49771ae08745Sheppo 		    copy_size, &copied_len);
49781ae08745Sheppo 
49791ae08745Sheppo 		if (rv != 0) {
49803af08d82Slm66018 			int 		error = EIO;
49813af08d82Slm66018 			uint64_t	rx_hd, rx_tl;
49823af08d82Slm66018 
49833af08d82Slm66018 			DWARN(chid,
49843af08d82Slm66018 			    "ldc_mem_copy: (0x%llx) err %d during copy\n",
49853af08d82Slm66018 			    (unsigned long long)chid, rv);
49863af08d82Slm66018 			DWARN(chid,
49873af08d82Slm66018 			    "ldc_mem_copy: (0x%llx) dir=0x%x, caddr=0x%lx, "
49884bac2208Snarayan 			    "loc_ra=0x%lx, exp_poff=0x%lx, loc_poff=0x%lx,"
49894bac2208Snarayan 			    " exp_psz=0x%lx, loc_psz=0x%lx, copy_sz=0x%lx,"
49904bac2208Snarayan 			    " copied_len=0x%lx, total_bal=0x%lx\n",
49913af08d82Slm66018 			    chid, direction, export_caddr, local_ra,
49921ae08745Sheppo 			    export_poff, local_poff, export_psize, local_psize,
49931ae08745Sheppo 			    copy_size, copied_len, total_bal);
49941ae08745Sheppo 
49951ae08745Sheppo 			*size = *size - total_bal;
49963af08d82Slm66018 
49973af08d82Slm66018 			/*
49983af08d82Slm66018 			 * check if reason for copy error was due to
49993af08d82Slm66018 			 * a channel reset. we need to grab the lock
50003af08d82Slm66018 			 * just in case we have to do a reset.
50013af08d82Slm66018 			 */
50023af08d82Slm66018 			mutex_enter(&ldcp->lock);
50033af08d82Slm66018 			mutex_enter(&ldcp->tx_lock);
50043af08d82Slm66018 
50053af08d82Slm66018 			rv = hv_ldc_rx_get_state(ldcp->id,
50063af08d82Slm66018 			    &rx_hd, &rx_tl, &(ldcp->link_state));
50073af08d82Slm66018 			if (ldcp->link_state == LDC_CHANNEL_DOWN ||
50083af08d82Slm66018 			    ldcp->link_state == LDC_CHANNEL_RESET) {
50093af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
50103af08d82Slm66018 				error = ECONNRESET;
50113af08d82Slm66018 			}
50123af08d82Slm66018 
50133af08d82Slm66018 			mutex_exit(&ldcp->tx_lock);
50141ae08745Sheppo 			mutex_exit(&ldcp->lock);
50153af08d82Slm66018 
50163af08d82Slm66018 			return (error);
50171ae08745Sheppo 		}
50181ae08745Sheppo 
50191ae08745Sheppo 		ASSERT(copied_len <= copy_size);
50201ae08745Sheppo 
50213af08d82Slm66018 		D2(chid, "ldc_mem_copy: copied=0x%llx\n", copied_len);
50221ae08745Sheppo 		export_poff += copied_len;
50231ae08745Sheppo 		local_poff += copied_len;
50241ae08745Sheppo 		export_psize -= copied_len;
50251ae08745Sheppo 		local_psize -= copied_len;
50261ae08745Sheppo 		cookie_size -= copied_len;
50271ae08745Sheppo 
50281ae08745Sheppo 		total_bal -= copied_len;
50291ae08745Sheppo 
50301ae08745Sheppo 		if (copy_size != copied_len)
50311ae08745Sheppo 			continue;
50321ae08745Sheppo 
50331ae08745Sheppo 		if (export_psize == 0 && total_bal != 0) {
50341ae08745Sheppo 
50351ae08745Sheppo 			if (cookie_size == 0) {
50361ae08745Sheppo 				idx++;
50371ae08745Sheppo 				cookie_addr = cookies[idx].addr;
50381ae08745Sheppo 				cookie_size = cookies[idx].size;
50391ae08745Sheppo 
50401ae08745Sheppo 				export_caddr = cookie_addr & ~(pg_size - 1);
50411ae08745Sheppo 				export_poff = cookie_addr & (pg_size - 1);
50421ae08745Sheppo 				export_psize =
50431ae08745Sheppo 					min(cookie_size, (pg_size-export_poff));
50441ae08745Sheppo 			} else {
50451ae08745Sheppo 				export_caddr += pg_size;
50461ae08745Sheppo 				export_poff = 0;
50471ae08745Sheppo 				export_psize = min(cookie_size, pg_size);
50481ae08745Sheppo 			}
50491ae08745Sheppo 		}
50501ae08745Sheppo 
50511ae08745Sheppo 		if (local_psize == 0 && total_bal != 0) {
50521ae08745Sheppo 			local_valign += pg_size;
50531ae08745Sheppo 			local_ra = va_to_pa((void *)local_valign);
50541ae08745Sheppo 			local_poff = 0;
50551ae08745Sheppo 			local_psize = min(pg_size, len);
50561ae08745Sheppo 			len -= local_psize;
50571ae08745Sheppo 		}
50581ae08745Sheppo 
50591ae08745Sheppo 		/* check if we are all done */
50601ae08745Sheppo 		if (total_bal == 0)
50611ae08745Sheppo 			break;
50621ae08745Sheppo 	}
50631ae08745Sheppo 
50641ae08745Sheppo 
50653af08d82Slm66018 	D1(chid,
50661ae08745Sheppo 	    "ldc_mem_copy: (0x%llx) done copying sz=0x%llx\n",
50673af08d82Slm66018 	    chid, *size);
50681ae08745Sheppo 
50691ae08745Sheppo 	return (0);
50701ae08745Sheppo }
50711ae08745Sheppo 
50721ae08745Sheppo /*
50731ae08745Sheppo  * Copy data either from or to the client specified virtual address
50741ae08745Sheppo  * space to or from HV physical memory.
50751ae08745Sheppo  *
50761ae08745Sheppo  * The direction argument determines whether the data is read from or
50771ae08745Sheppo  * written to HV memory. direction values are LDC_COPY_IN/OUT similar
50781ae08745Sheppo  * to the ldc_mem_copy interface
50791ae08745Sheppo  */
50801ae08745Sheppo int
50813af08d82Slm66018 ldc_mem_rdwr_cookie(ldc_handle_t handle, caddr_t vaddr, size_t *size,
50821ae08745Sheppo     caddr_t paddr, uint8_t direction)
50831ae08745Sheppo {
50841ae08745Sheppo 	ldc_chan_t 	*ldcp;
50851ae08745Sheppo 	uint64_t	local_voff, local_valign;
50861ae08745Sheppo 	uint64_t	pg_shift, pg_size, pg_size_code;
50871ae08745Sheppo 	uint64_t 	target_pa, target_poff, target_psize, target_size;
50881ae08745Sheppo 	uint64_t	local_ra, local_poff, local_psize;
50891ae08745Sheppo 	uint64_t	copy_size, copied_len = 0;
50901ae08745Sheppo 	pgcnt_t		npages;
50911ae08745Sheppo 	size_t		len = *size;
50921ae08745Sheppo 	int 		rv = 0;
50931ae08745Sheppo 
50941ae08745Sheppo 	if (handle == NULL) {
50951ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
50963af08d82Slm66018 		    "ldc_mem_rdwr_cookie: invalid channel handle\n");
50971ae08745Sheppo 		return (EINVAL);
50981ae08745Sheppo 	}
50991ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
51001ae08745Sheppo 
51011ae08745Sheppo 	mutex_enter(&ldcp->lock);
51021ae08745Sheppo 
51031ae08745Sheppo 	/* check to see if channel is UP */
51041ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
51051ae08745Sheppo 		DWARN(ldcp->id,
51063af08d82Slm66018 		    "ldc_mem_rdwr_cookie: (0x%llx) channel is not UP\n",
51071ae08745Sheppo 		    ldcp->id);
51081ae08745Sheppo 		mutex_exit(&ldcp->lock);
51093af08d82Slm66018 		return (ECONNRESET);
51101ae08745Sheppo 	}
51111ae08745Sheppo 
51121ae08745Sheppo 	/* Force address and size to be 8-byte aligned */
51131ae08745Sheppo 	if ((((uintptr_t)vaddr | len) & 0x7) != 0) {
51141ae08745Sheppo 		DWARN(ldcp->id,
51153af08d82Slm66018 		    "ldc_mem_rdwr_cookie: addr/size is not 8-byte aligned\n");
51161ae08745Sheppo 		mutex_exit(&ldcp->lock);
51171ae08745Sheppo 		return (EINVAL);
51181ae08745Sheppo 	}
51191ae08745Sheppo 
51201ae08745Sheppo 	target_size = *size;
51211ae08745Sheppo 
51221ae08745Sheppo 	/* FUTURE: get the page size, pgsz code, and shift */
51231ae08745Sheppo 	pg_size = MMU_PAGESIZE;
51241ae08745Sheppo 	pg_size_code = page_szc(pg_size);
51251ae08745Sheppo 	pg_shift = page_get_shift(pg_size_code);
51261ae08745Sheppo 
51273af08d82Slm66018 	D1(ldcp->id, "ldc_mem_rdwr_cookie: copying data "
51281ae08745Sheppo 	    "(0x%llx) va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n",
51291ae08745Sheppo 	    ldcp->id, vaddr, pg_size, pg_size_code, pg_shift);
51301ae08745Sheppo 
51311ae08745Sheppo 	/* aligned VA and its offset */
51321ae08745Sheppo 	local_valign = ((uintptr_t)vaddr) & ~(pg_size - 1);
51331ae08745Sheppo 	local_voff = ((uintptr_t)vaddr) & (pg_size - 1);
51341ae08745Sheppo 
51351ae08745Sheppo 	npages = (len + local_voff) / pg_size;
51361ae08745Sheppo 	npages = ((len + local_voff) % pg_size == 0) ? npages : npages+1;
51371ae08745Sheppo 
51383af08d82Slm66018 	D1(ldcp->id, "ldc_mem_rdwr_cookie: (0x%llx) v=0x%llx, "
51393af08d82Slm66018 	    "val=0x%llx,off=0x%x,pgs=0x%x\n",
51401ae08745Sheppo 	    ldcp->id, vaddr, local_valign, local_voff, npages);
51411ae08745Sheppo 
51421ae08745Sheppo 	local_ra = va_to_pa((void *)local_valign);
51431ae08745Sheppo 	local_poff = local_voff;
51441ae08745Sheppo 	local_psize = min(len, (pg_size - local_voff));
51451ae08745Sheppo 
51461ae08745Sheppo 	len -= local_psize;
51471ae08745Sheppo 
51481ae08745Sheppo 	target_pa = ((uintptr_t)paddr) & ~(pg_size - 1);
51491ae08745Sheppo 	target_poff = ((uintptr_t)paddr) & (pg_size - 1);
51501ae08745Sheppo 	target_psize = pg_size - target_poff;
51511ae08745Sheppo 
51521ae08745Sheppo 	for (;;) {
51531ae08745Sheppo 
51541ae08745Sheppo 		copy_size = min(target_psize, local_psize);
51551ae08745Sheppo 
51561ae08745Sheppo 		D1(ldcp->id,
51573af08d82Slm66018 		    "ldc_mem_rdwr_cookie: (0x%llx) dir=0x%x, tar_pa=0x%llx,"
51581ae08745Sheppo 		    " loc_ra=0x%llx, tar_poff=0x%llx, loc_poff=0x%llx,"
51591ae08745Sheppo 		    " tar_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx,"
51601ae08745Sheppo 		    " total_bal=0x%llx\n",
51611ae08745Sheppo 		    ldcp->id, direction, target_pa, local_ra, target_poff,
51621ae08745Sheppo 		    local_poff, target_psize, local_psize, copy_size,
51631ae08745Sheppo 		    target_size);
51641ae08745Sheppo 
51651ae08745Sheppo 		rv = hv_ldc_copy(ldcp->id, direction,
51661ae08745Sheppo 		    (target_pa + target_poff), (local_ra + local_poff),
51671ae08745Sheppo 		    copy_size, &copied_len);
51681ae08745Sheppo 
51691ae08745Sheppo 		if (rv != 0) {
51703af08d82Slm66018 			DWARN(DBG_ALL_LDCS,
51713af08d82Slm66018 			    "ldc_mem_rdwr_cookie: (0x%lx) err %d during copy\n",
51721ae08745Sheppo 			    ldcp->id, rv);
51731ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
51743af08d82Slm66018 			    "ldc_mem_rdwr_cookie: (0x%llx) dir=%lld, "
51753af08d82Slm66018 			    "tar_pa=0x%llx, loc_ra=0x%llx, tar_poff=0x%llx, "
51763af08d82Slm66018 			    "loc_poff=0x%llx, tar_psz=0x%llx, loc_psz=0x%llx, "
51773af08d82Slm66018 			    "copy_sz=0x%llx, total_bal=0x%llx\n",
51781ae08745Sheppo 			    ldcp->id, direction, target_pa, local_ra,
51791ae08745Sheppo 			    target_poff, local_poff, target_psize, local_psize,
51801ae08745Sheppo 			    copy_size, target_size);
51811ae08745Sheppo 
51821ae08745Sheppo 			*size = *size - target_size;
51831ae08745Sheppo 			mutex_exit(&ldcp->lock);
51841ae08745Sheppo 			return (i_ldc_h2v_error(rv));
51851ae08745Sheppo 		}
51861ae08745Sheppo 
51873af08d82Slm66018 		D2(ldcp->id, "ldc_mem_rdwr_cookie: copied=0x%llx\n",
51883af08d82Slm66018 		    copied_len);
51891ae08745Sheppo 		target_poff += copied_len;
51901ae08745Sheppo 		local_poff += copied_len;
51911ae08745Sheppo 		target_psize -= copied_len;
51921ae08745Sheppo 		local_psize -= copied_len;
51931ae08745Sheppo 
51941ae08745Sheppo 		target_size -= copied_len;
51951ae08745Sheppo 
51961ae08745Sheppo 		if (copy_size != copied_len)
51971ae08745Sheppo 			continue;
51981ae08745Sheppo 
51991ae08745Sheppo 		if (target_psize == 0 && target_size != 0) {
52001ae08745Sheppo 			target_pa += pg_size;
52011ae08745Sheppo 			target_poff = 0;
52021ae08745Sheppo 			target_psize = min(pg_size, target_size);
52031ae08745Sheppo 		}
52041ae08745Sheppo 
52051ae08745Sheppo 		if (local_psize == 0 && target_size != 0) {
52061ae08745Sheppo 			local_valign += pg_size;
52071ae08745Sheppo 			local_ra = va_to_pa((void *)local_valign);
52081ae08745Sheppo 			local_poff = 0;
52091ae08745Sheppo 			local_psize = min(pg_size, len);
52101ae08745Sheppo 			len -= local_psize;
52111ae08745Sheppo 		}
52121ae08745Sheppo 
52131ae08745Sheppo 		/* check if we are all done */
52141ae08745Sheppo 		if (target_size == 0)
52151ae08745Sheppo 			break;
52161ae08745Sheppo 	}
52171ae08745Sheppo 
52181ae08745Sheppo 	mutex_exit(&ldcp->lock);
52191ae08745Sheppo 
52203af08d82Slm66018 	D1(ldcp->id, "ldc_mem_rdwr_cookie: (0x%llx) done copying sz=0x%llx\n",
52211ae08745Sheppo 	    ldcp->id, *size);
52221ae08745Sheppo 
52231ae08745Sheppo 	return (0);
52241ae08745Sheppo }
52251ae08745Sheppo 
52261ae08745Sheppo /*
52271ae08745Sheppo  * Map an exported memory segment into the local address space. If the
52281ae08745Sheppo  * memory range was exported for direct map access, a HV call is made
52291ae08745Sheppo  * to allocate a RA range. If the map is done via a shadow copy, local
52301ae08745Sheppo  * shadow memory is allocated and the base VA is returned in 'vaddr'. If
52311ae08745Sheppo  * the mapping is a direct map then the RA is returned in 'raddr'.
52321ae08745Sheppo  */
52331ae08745Sheppo int
52341ae08745Sheppo ldc_mem_map(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie, uint32_t ccount,
52354bac2208Snarayan     uint8_t mtype, uint8_t perm, caddr_t *vaddr, caddr_t *raddr)
52361ae08745Sheppo {
52374bac2208Snarayan 	int		i, j, idx, rv, retries;
52381ae08745Sheppo 	ldc_chan_t 	*ldcp;
52391ae08745Sheppo 	ldc_mhdl_t	*mhdl;
52401ae08745Sheppo 	ldc_memseg_t	*memseg;
52414bac2208Snarayan 	caddr_t		tmpaddr;
52424bac2208Snarayan 	uint64_t	map_perm = perm;
52434bac2208Snarayan 	uint64_t	pg_size, pg_shift, pg_size_code, pg_mask;
52444bac2208Snarayan 	uint64_t	exp_size = 0, base_off, map_size, npages;
52454bac2208Snarayan 	uint64_t	cookie_addr, cookie_off, cookie_size;
52464bac2208Snarayan 	tte_t		ldc_tte;
52471ae08745Sheppo 
52481ae08745Sheppo 	if (mhandle == NULL) {
52491ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_mem_map: invalid memory handle\n");
52501ae08745Sheppo 		return (EINVAL);
52511ae08745Sheppo 	}
52521ae08745Sheppo 	mhdl = (ldc_mhdl_t *)mhandle;
52531ae08745Sheppo 
52541ae08745Sheppo 	mutex_enter(&mhdl->lock);
52551ae08745Sheppo 
52561ae08745Sheppo 	if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED ||
52571ae08745Sheppo 	    mhdl->memseg != NULL) {
52581ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
52591ae08745Sheppo 		    "ldc_mem_map: (0x%llx) handle bound/mapped\n", mhandle);
52601ae08745Sheppo 		mutex_exit(&mhdl->lock);
52611ae08745Sheppo 		return (EINVAL);
52621ae08745Sheppo 	}
52631ae08745Sheppo 
52641ae08745Sheppo 	ldcp = mhdl->ldcp;
52651ae08745Sheppo 
52661ae08745Sheppo 	mutex_enter(&ldcp->lock);
52671ae08745Sheppo 
52681ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
52691ae08745Sheppo 		DWARN(ldcp->id,
52701ae08745Sheppo 		    "ldc_mem_dring_map: (0x%llx) channel is not UP\n",
52711ae08745Sheppo 		    ldcp->id);
52721ae08745Sheppo 		mutex_exit(&ldcp->lock);
52731ae08745Sheppo 		mutex_exit(&mhdl->lock);
52743af08d82Slm66018 		return (ECONNRESET);
52751ae08745Sheppo 	}
52761ae08745Sheppo 
52771ae08745Sheppo 	if ((mtype & (LDC_SHADOW_MAP|LDC_DIRECT_MAP|LDC_IO_MAP)) == 0) {
52781ae08745Sheppo 		DWARN(ldcp->id, "ldc_mem_map: invalid map type\n");
52791ae08745Sheppo 		mutex_exit(&ldcp->lock);
52801ae08745Sheppo 		mutex_exit(&mhdl->lock);
52811ae08745Sheppo 		return (EINVAL);
52821ae08745Sheppo 	}
52831ae08745Sheppo 
52841ae08745Sheppo 	D1(ldcp->id, "ldc_mem_map: (0x%llx) cookie = 0x%llx,0x%llx\n",
5285d10e4ef2Snarayan 	    ldcp->id, cookie->addr, cookie->size);
52861ae08745Sheppo 
52871ae08745Sheppo 	/* FUTURE: get the page size, pgsz code, and shift */
52881ae08745Sheppo 	pg_size = MMU_PAGESIZE;
52891ae08745Sheppo 	pg_size_code = page_szc(pg_size);
52901ae08745Sheppo 	pg_shift = page_get_shift(pg_size_code);
52914bac2208Snarayan 	pg_mask = ~(pg_size - 1);
52921ae08745Sheppo 
52931ae08745Sheppo 	/* calculate the number of pages in the exported cookie */
52944bac2208Snarayan 	base_off = cookie[0].addr & (pg_size - 1);
52954bac2208Snarayan 	for (idx = 0; idx < ccount; idx++)
52961ae08745Sheppo 		exp_size += cookie[idx].size;
52974bac2208Snarayan 	map_size = P2ROUNDUP((exp_size + base_off), pg_size);
52984bac2208Snarayan 	npages = (map_size >> pg_shift);
52991ae08745Sheppo 
53001ae08745Sheppo 	/* Allocate memseg structure */
53014bac2208Snarayan 	memseg = mhdl->memseg =
53024bac2208Snarayan 		kmem_cache_alloc(ldcssp->memseg_cache, KM_SLEEP);
53031ae08745Sheppo 
53041ae08745Sheppo 	/* Allocate memory to store all pages and cookies */
53051ae08745Sheppo 	memseg->pages =	kmem_zalloc((sizeof (ldc_page_t) * npages), KM_SLEEP);
53061ae08745Sheppo 	memseg->cookies =
53071ae08745Sheppo 		kmem_zalloc((sizeof (ldc_mem_cookie_t) * ccount), KM_SLEEP);
53081ae08745Sheppo 
53094bac2208Snarayan 	D2(ldcp->id, "ldc_mem_map: (0x%llx) exp_size=0x%llx, map_size=0x%llx,"
53104bac2208Snarayan 	    "pages=0x%llx\n", ldcp->id, exp_size, map_size, npages);
53111ae08745Sheppo 
53124bac2208Snarayan 	/*
53134bac2208Snarayan 	 * Check if direct map over shared memory is enabled, if not change
53144bac2208Snarayan 	 * the mapping type to SHADOW_MAP.
53154bac2208Snarayan 	 */
53164bac2208Snarayan 	if (ldc_shmem_enabled == 0)
53174bac2208Snarayan 		mtype = LDC_SHADOW_MAP;
53184bac2208Snarayan 
53194bac2208Snarayan 	/*
53204bac2208Snarayan 	 * Check to see if the client is requesting direct or shadow map
53214bac2208Snarayan 	 * If direct map is requested, try to map remote memory first,
53224bac2208Snarayan 	 * and if that fails, revert to shadow map
53234bac2208Snarayan 	 */
53244bac2208Snarayan 	if (mtype == LDC_DIRECT_MAP) {
53254bac2208Snarayan 
53264bac2208Snarayan 		/* Allocate kernel virtual space for mapping */
53274bac2208Snarayan 		memseg->vaddr = vmem_xalloc(heap_arena, map_size,
53284bac2208Snarayan 		    pg_size, 0, 0, NULL, NULL, VM_NOSLEEP);
53294bac2208Snarayan 		if (memseg->vaddr == NULL) {
53304bac2208Snarayan 			cmn_err(CE_WARN,
53314bac2208Snarayan 			    "ldc_mem_map: (0x%lx) memory map failed\n",
53324bac2208Snarayan 			    ldcp->id);
53334bac2208Snarayan 			kmem_free(memseg->cookies,
53344bac2208Snarayan 			    (sizeof (ldc_mem_cookie_t) * ccount));
53354bac2208Snarayan 			kmem_free(memseg->pages,
53364bac2208Snarayan 			    (sizeof (ldc_page_t) * npages));
53374bac2208Snarayan 			kmem_cache_free(ldcssp->memseg_cache, memseg);
53384bac2208Snarayan 
53394bac2208Snarayan 			mutex_exit(&ldcp->lock);
53404bac2208Snarayan 			mutex_exit(&mhdl->lock);
53414bac2208Snarayan 			return (ENOMEM);
53424bac2208Snarayan 		}
53434bac2208Snarayan 
53444bac2208Snarayan 		/* Unload previous mapping */
53454bac2208Snarayan 		hat_unload(kas.a_hat, memseg->vaddr, map_size,
53464bac2208Snarayan 		    HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK);
53474bac2208Snarayan 
53484bac2208Snarayan 		/* for each cookie passed in - map into address space */
53494bac2208Snarayan 		idx = 0;
53504bac2208Snarayan 		cookie_size = 0;
53514bac2208Snarayan 		tmpaddr = memseg->vaddr;
53524bac2208Snarayan 
53534bac2208Snarayan 		for (i = 0; i < npages; i++) {
53544bac2208Snarayan 
53554bac2208Snarayan 			if (cookie_size == 0) {
53564bac2208Snarayan 				ASSERT(idx < ccount);
53574bac2208Snarayan 				cookie_addr = cookie[idx].addr & pg_mask;
53584bac2208Snarayan 				cookie_off = cookie[idx].addr & (pg_size - 1);
53594bac2208Snarayan 				cookie_size =
53604bac2208Snarayan 				    P2ROUNDUP((cookie_off + cookie[idx].size),
53614bac2208Snarayan 					pg_size);
53624bac2208Snarayan 				idx++;
53634bac2208Snarayan 			}
53644bac2208Snarayan 
53654bac2208Snarayan 			D1(ldcp->id, "ldc_mem_map: (0x%llx) mapping "
53664bac2208Snarayan 			    "cookie 0x%llx, bal=0x%llx\n", ldcp->id,
53674bac2208Snarayan 			    cookie_addr, cookie_size);
53684bac2208Snarayan 
53694bac2208Snarayan 			/* map the cookie into address space */
53704bac2208Snarayan 			for (retries = 0; retries < ldc_max_retries;
53714bac2208Snarayan 			    retries++) {
53724bac2208Snarayan 
53734bac2208Snarayan 				rv = hv_ldc_mapin(ldcp->id, cookie_addr,
53744bac2208Snarayan 				    &memseg->pages[i].raddr, &map_perm);
53754bac2208Snarayan 				if (rv != H_EWOULDBLOCK && rv != H_ETOOMANY)
53764bac2208Snarayan 					break;
53774bac2208Snarayan 
53784bac2208Snarayan 				drv_usecwait(ldc_delay);
53794bac2208Snarayan 			}
53804bac2208Snarayan 
53814bac2208Snarayan 			if (rv || memseg->pages[i].raddr == 0) {
53824bac2208Snarayan 				DWARN(ldcp->id,
53834bac2208Snarayan 				    "ldc_mem_map: (0x%llx) hv mapin err %d\n",
53844bac2208Snarayan 				    ldcp->id, rv);
53854bac2208Snarayan 
53864bac2208Snarayan 				/* remove previous mapins */
53874bac2208Snarayan 				hat_unload(kas.a_hat, memseg->vaddr, map_size,
53884bac2208Snarayan 				    HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK);
53894bac2208Snarayan 				for (j = 0; j < i; j++) {
53904bac2208Snarayan 					rv = hv_ldc_unmap(
53914bac2208Snarayan 							memseg->pages[j].raddr);
53924bac2208Snarayan 					if (rv) {
53934bac2208Snarayan 						DWARN(ldcp->id,
53944bac2208Snarayan 						    "ldc_mem_map: (0x%llx) "
53954bac2208Snarayan 						    "cannot unmap ra=0x%llx\n",
53964bac2208Snarayan 					    ldcp->id,
53974bac2208Snarayan 						    memseg->pages[j].raddr);
53984bac2208Snarayan 					}
53994bac2208Snarayan 				}
54004bac2208Snarayan 
54014bac2208Snarayan 				/* free kernel virtual space */
54024bac2208Snarayan 				vmem_free(heap_arena, (void *)memseg->vaddr,
54037636cb21Slm66018 				    map_size);
54044bac2208Snarayan 
54054bac2208Snarayan 				/* direct map failed - revert to shadow map */
54064bac2208Snarayan 				mtype = LDC_SHADOW_MAP;
54074bac2208Snarayan 				break;
54084bac2208Snarayan 
54094bac2208Snarayan 			} else {
54104bac2208Snarayan 
54114bac2208Snarayan 				D1(ldcp->id,
54124bac2208Snarayan 				    "ldc_mem_map: (0x%llx) vtop map 0x%llx -> "
54134bac2208Snarayan 				    "0x%llx, cookie=0x%llx, perm=0x%llx\n",
54144bac2208Snarayan 				    ldcp->id, tmpaddr, memseg->pages[i].raddr,
54154bac2208Snarayan 				    cookie_addr, perm);
54164bac2208Snarayan 
54174bac2208Snarayan 				/*
54184bac2208Snarayan 				 * NOTE: Calling hat_devload directly, causes it
54194bac2208Snarayan 				 * to look for page_t using the pfn. Since this
54204bac2208Snarayan 				 * addr is greater than the memlist, it treates
54214bac2208Snarayan 				 * it as non-memory
54224bac2208Snarayan 				 */
54234bac2208Snarayan 				sfmmu_memtte(&ldc_tte,
54244bac2208Snarayan 				    (pfn_t)(memseg->pages[i].raddr >> pg_shift),
54254bac2208Snarayan 				    PROT_READ | PROT_WRITE | HAT_NOSYNC, TTE8K);
54264bac2208Snarayan 
54274bac2208Snarayan 				D1(ldcp->id,
54284bac2208Snarayan 				    "ldc_mem_map: (0x%llx) ra 0x%llx -> "
54294bac2208Snarayan 				    "tte 0x%llx\n", ldcp->id,
54304bac2208Snarayan 				    memseg->pages[i].raddr, ldc_tte);
54314bac2208Snarayan 
54324bac2208Snarayan 				sfmmu_tteload(kas.a_hat, &ldc_tte, tmpaddr,
54334bac2208Snarayan 				    NULL, HAT_LOAD_LOCK);
54344bac2208Snarayan 
54354bac2208Snarayan 				cookie_size -= pg_size;
54364bac2208Snarayan 				cookie_addr += pg_size;
54374bac2208Snarayan 				tmpaddr += pg_size;
54384bac2208Snarayan 			}
54394bac2208Snarayan 		}
54404bac2208Snarayan 	}
54414bac2208Snarayan 
54421ae08745Sheppo 	if (mtype == LDC_SHADOW_MAP) {
54431ae08745Sheppo 		if (*vaddr == NULL) {
54443af08d82Slm66018 			memseg->vaddr = kmem_zalloc(exp_size, KM_SLEEP);
54451ae08745Sheppo 			mhdl->myshadow = B_TRUE;
54461ae08745Sheppo 
54471ae08745Sheppo 			D1(ldcp->id, "ldc_mem_map: (0x%llx) allocated "
54484bac2208Snarayan 			    "shadow page va=0x%llx\n", ldcp->id, memseg->vaddr);
54491ae08745Sheppo 		} else {
54501ae08745Sheppo 			/*
54514bac2208Snarayan 			 * Use client supplied memory for memseg->vaddr
54521ae08745Sheppo 			 * WARNING: assuming that client mem is >= exp_size
54531ae08745Sheppo 			 */
54544bac2208Snarayan 			memseg->vaddr = *vaddr;
54551ae08745Sheppo 		}
54561ae08745Sheppo 
54571ae08745Sheppo 		/* Save all page and cookie information */
54584bac2208Snarayan 		for (i = 0, tmpaddr = memseg->vaddr; i < npages; i++) {
54591ae08745Sheppo 			memseg->pages[i].raddr = va_to_pa(tmpaddr);
54601ae08745Sheppo 			memseg->pages[i].size = pg_size;
54611ae08745Sheppo 			tmpaddr += pg_size;
54621ae08745Sheppo 		}
54634bac2208Snarayan 
54641ae08745Sheppo 	}
54651ae08745Sheppo 
54664bac2208Snarayan 	/* save all cookies */
54674bac2208Snarayan 	bcopy(cookie, memseg->cookies, ccount * sizeof (ldc_mem_cookie_t));
54684bac2208Snarayan 
54691ae08745Sheppo 	/* update memseg_t */
54701ae08745Sheppo 	memseg->raddr = memseg->pages[0].raddr;
54714bac2208Snarayan 	memseg->size = (mtype == LDC_SHADOW_MAP) ? exp_size : map_size;
54721ae08745Sheppo 	memseg->npages = npages;
54731ae08745Sheppo 	memseg->ncookies = ccount;
54741ae08745Sheppo 	memseg->next_cookie = 0;
54751ae08745Sheppo 
54761ae08745Sheppo 	/* memory handle = mapped */
54771ae08745Sheppo 	mhdl->mtype = mtype;
54784bac2208Snarayan 	mhdl->perm = perm;
54791ae08745Sheppo 	mhdl->status = LDC_MAPPED;
54801ae08745Sheppo 
54811ae08745Sheppo 	D1(ldcp->id, "ldc_mem_map: (0x%llx) mapped 0x%llx, ra=0x%llx, "
54821ae08745Sheppo 	    "va=0x%llx, pgs=0x%llx cookies=0x%llx\n",
54831ae08745Sheppo 	    ldcp->id, mhdl, memseg->raddr, memseg->vaddr,
54841ae08745Sheppo 	    memseg->npages, memseg->ncookies);
54851ae08745Sheppo 
54864bac2208Snarayan 	if (mtype == LDC_SHADOW_MAP)
54874bac2208Snarayan 		base_off = 0;
54881ae08745Sheppo 	if (raddr)
54894bac2208Snarayan 		*raddr = (caddr_t)(memseg->raddr | base_off);
54901ae08745Sheppo 	if (vaddr)
54914bac2208Snarayan 		*vaddr = (caddr_t)((uintptr_t)memseg->vaddr | base_off);
54921ae08745Sheppo 
54931ae08745Sheppo 	mutex_exit(&ldcp->lock);
54941ae08745Sheppo 	mutex_exit(&mhdl->lock);
54951ae08745Sheppo 	return (0);
54961ae08745Sheppo }
54971ae08745Sheppo 
54981ae08745Sheppo /*
54991ae08745Sheppo  * Unmap a memory segment. Free shadow memory (if any).
55001ae08745Sheppo  */
55011ae08745Sheppo int
55021ae08745Sheppo ldc_mem_unmap(ldc_mem_handle_t mhandle)
55031ae08745Sheppo {
55044bac2208Snarayan 	int		i, rv;
55051ae08745Sheppo 	ldc_mhdl_t	*mhdl = (ldc_mhdl_t *)mhandle;
55061ae08745Sheppo 	ldc_chan_t 	*ldcp;
55071ae08745Sheppo 	ldc_memseg_t	*memseg;
55081ae08745Sheppo 
55091ae08745Sheppo 	if (mhdl == 0 || mhdl->status != LDC_MAPPED) {
55101ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
55111ae08745Sheppo 		    "ldc_mem_unmap: (0x%llx) handle is not mapped\n",
55121ae08745Sheppo 		    mhandle);
55131ae08745Sheppo 		return (EINVAL);
55141ae08745Sheppo 	}
55151ae08745Sheppo 
55161ae08745Sheppo 	mutex_enter(&mhdl->lock);
55171ae08745Sheppo 
55181ae08745Sheppo 	ldcp = mhdl->ldcp;
55191ae08745Sheppo 	memseg = mhdl->memseg;
55201ae08745Sheppo 
55211ae08745Sheppo 	D1(ldcp->id, "ldc_mem_unmap: (0x%llx) unmapping handle 0x%llx\n",
55221ae08745Sheppo 	    ldcp->id, mhdl);
55231ae08745Sheppo 
55241ae08745Sheppo 	/* if we allocated shadow memory - free it */
55251ae08745Sheppo 	if (mhdl->mtype == LDC_SHADOW_MAP && mhdl->myshadow) {
55263af08d82Slm66018 		kmem_free(memseg->vaddr, memseg->size);
55274bac2208Snarayan 	} else if (mhdl->mtype == LDC_DIRECT_MAP) {
55284bac2208Snarayan 
55294bac2208Snarayan 		/* unmap in the case of DIRECT_MAP */
55304bac2208Snarayan 		hat_unload(kas.a_hat, memseg->vaddr, memseg->size,
55314bac2208Snarayan 		    HAT_UNLOAD_UNLOCK);
55324bac2208Snarayan 
55334bac2208Snarayan 		for (i = 0; i < memseg->npages; i++) {
55344bac2208Snarayan 			rv = hv_ldc_unmap(memseg->pages[i].raddr);
55354bac2208Snarayan 			if (rv) {
55364bac2208Snarayan 				cmn_err(CE_WARN,
55374bac2208Snarayan 				    "ldc_mem_map: (0x%lx) hv unmap err %d\n",
55384bac2208Snarayan 				    ldcp->id, rv);
55394bac2208Snarayan 			}
55404bac2208Snarayan 		}
55414bac2208Snarayan 
55424bac2208Snarayan 		vmem_free(heap_arena, (void *)memseg->vaddr, memseg->size);
55431ae08745Sheppo 	}
55441ae08745Sheppo 
55451ae08745Sheppo 	/* free the allocated memseg and page structures */
55461ae08745Sheppo 	kmem_free(memseg->pages, (sizeof (ldc_page_t) * memseg->npages));
55471ae08745Sheppo 	kmem_free(memseg->cookies,
55481ae08745Sheppo 	    (sizeof (ldc_mem_cookie_t) * memseg->ncookies));
55494bac2208Snarayan 	kmem_cache_free(ldcssp->memseg_cache, memseg);
55501ae08745Sheppo 
55511ae08745Sheppo 	/* uninitialize the memory handle */
55521ae08745Sheppo 	mhdl->memseg = NULL;
55531ae08745Sheppo 	mhdl->status = LDC_UNBOUND;
55541ae08745Sheppo 
55551ae08745Sheppo 	D1(ldcp->id, "ldc_mem_unmap: (0x%llx) unmapped handle 0x%llx\n",
55561ae08745Sheppo 	    ldcp->id, mhdl);
55571ae08745Sheppo 
55581ae08745Sheppo 	mutex_exit(&mhdl->lock);
55591ae08745Sheppo 	return (0);
55601ae08745Sheppo }
55611ae08745Sheppo 
55621ae08745Sheppo /*
55631ae08745Sheppo  * Internal entry point for LDC mapped memory entry consistency
55641ae08745Sheppo  * semantics. Acquire copies the contents of the remote memory
55651ae08745Sheppo  * into the local shadow copy. The release operation copies the local
55661ae08745Sheppo  * contents into the remote memory. The offset and size specify the
55671ae08745Sheppo  * bounds for the memory range being synchronized.
55681ae08745Sheppo  */
55691ae08745Sheppo static int
55701ae08745Sheppo i_ldc_mem_acquire_release(ldc_mem_handle_t mhandle, uint8_t direction,
55711ae08745Sheppo     uint64_t offset, size_t size)
55721ae08745Sheppo {
55731ae08745Sheppo 	int 		err;
55741ae08745Sheppo 	ldc_mhdl_t	*mhdl;
55751ae08745Sheppo 	ldc_chan_t	*ldcp;
55761ae08745Sheppo 	ldc_memseg_t	*memseg;
55771ae08745Sheppo 	caddr_t		local_vaddr;
55781ae08745Sheppo 	size_t		copy_size;
55791ae08745Sheppo 
55801ae08745Sheppo 	if (mhandle == NULL) {
55811ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
55821ae08745Sheppo 		    "i_ldc_mem_acquire_release: invalid memory handle\n");
55831ae08745Sheppo 		return (EINVAL);
55841ae08745Sheppo 	}
55851ae08745Sheppo 	mhdl = (ldc_mhdl_t *)mhandle;
55861ae08745Sheppo 
55871ae08745Sheppo 	mutex_enter(&mhdl->lock);
55881ae08745Sheppo 
55891ae08745Sheppo 	if (mhdl->status != LDC_MAPPED || mhdl->ldcp == NULL) {
55901ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
55911ae08745Sheppo 		    "i_ldc_mem_acquire_release: not mapped memory\n");
55921ae08745Sheppo 		mutex_exit(&mhdl->lock);
55931ae08745Sheppo 		return (EINVAL);
55941ae08745Sheppo 	}
55951ae08745Sheppo 
55964bac2208Snarayan 	/* do nothing for direct map */
55974bac2208Snarayan 	if (mhdl->mtype == LDC_DIRECT_MAP) {
55984bac2208Snarayan 		mutex_exit(&mhdl->lock);
55994bac2208Snarayan 		return (0);
56004bac2208Snarayan 	}
56014bac2208Snarayan 
56024bac2208Snarayan 	/* do nothing if COPY_IN+MEM_W and COPY_OUT+MEM_R */
56034bac2208Snarayan 	if ((direction == LDC_COPY_IN && (mhdl->perm & LDC_MEM_R) == 0) ||
56044bac2208Snarayan 	    (direction == LDC_COPY_OUT && (mhdl->perm & LDC_MEM_W) == 0)) {
56054bac2208Snarayan 		mutex_exit(&mhdl->lock);
56064bac2208Snarayan 		return (0);
56074bac2208Snarayan 	}
56084bac2208Snarayan 
56091ae08745Sheppo 	if (offset >= mhdl->memseg->size ||
56101ae08745Sheppo 	    (offset + size) > mhdl->memseg->size) {
56111ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
56121ae08745Sheppo 		    "i_ldc_mem_acquire_release: memory out of range\n");
56131ae08745Sheppo 		mutex_exit(&mhdl->lock);
56141ae08745Sheppo 		return (EINVAL);
56151ae08745Sheppo 	}
56161ae08745Sheppo 
56171ae08745Sheppo 	/* get the channel handle and memory segment */
56181ae08745Sheppo 	ldcp = mhdl->ldcp;
56191ae08745Sheppo 	memseg = mhdl->memseg;
56201ae08745Sheppo 
56211ae08745Sheppo 	if (mhdl->mtype == LDC_SHADOW_MAP) {
56221ae08745Sheppo 
56231ae08745Sheppo 		local_vaddr = memseg->vaddr + offset;
56241ae08745Sheppo 		copy_size = size;
56251ae08745Sheppo 
56261ae08745Sheppo 		/* copy to/from remote from/to local memory */
56271ae08745Sheppo 		err = ldc_mem_copy((ldc_handle_t)ldcp, local_vaddr, offset,
56281ae08745Sheppo 		    &copy_size, memseg->cookies, memseg->ncookies,
56291ae08745Sheppo 		    direction);
56301ae08745Sheppo 		if (err || copy_size != size) {
5631cb112a14Slm66018 			DWARN(ldcp->id,
56321ae08745Sheppo 			    "i_ldc_mem_acquire_release: copy failed\n");
56331ae08745Sheppo 			mutex_exit(&mhdl->lock);
56341ae08745Sheppo 			return (err);
56351ae08745Sheppo 		}
56361ae08745Sheppo 	}
56371ae08745Sheppo 
56381ae08745Sheppo 	mutex_exit(&mhdl->lock);
56391ae08745Sheppo 
56401ae08745Sheppo 	return (0);
56411ae08745Sheppo }
56421ae08745Sheppo 
56431ae08745Sheppo /*
56441ae08745Sheppo  * Ensure that the contents in the remote memory seg are consistent
56451ae08745Sheppo  * with the contents if of local segment
56461ae08745Sheppo  */
56471ae08745Sheppo int
56481ae08745Sheppo ldc_mem_acquire(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size)
56491ae08745Sheppo {
56501ae08745Sheppo 	return (i_ldc_mem_acquire_release(mhandle, LDC_COPY_IN, offset, size));
56511ae08745Sheppo }
56521ae08745Sheppo 
56531ae08745Sheppo 
56541ae08745Sheppo /*
56551ae08745Sheppo  * Ensure that the contents in the local memory seg are consistent
56561ae08745Sheppo  * with the contents if of remote segment
56571ae08745Sheppo  */
56581ae08745Sheppo int
56591ae08745Sheppo ldc_mem_release(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size)
56601ae08745Sheppo {
56611ae08745Sheppo 	return (i_ldc_mem_acquire_release(mhandle, LDC_COPY_OUT, offset, size));
56621ae08745Sheppo }
56631ae08745Sheppo 
56641ae08745Sheppo /*
56651ae08745Sheppo  * Allocate a descriptor ring. The size of each each descriptor
56661ae08745Sheppo  * must be 8-byte aligned and the entire ring should be a multiple
56671ae08745Sheppo  * of MMU_PAGESIZE.
56681ae08745Sheppo  */
56691ae08745Sheppo int
56701ae08745Sheppo ldc_mem_dring_create(uint32_t len, uint32_t dsize, ldc_dring_handle_t *dhandle)
56711ae08745Sheppo {
56721ae08745Sheppo 	ldc_dring_t *dringp;
56731ae08745Sheppo 	size_t size = (dsize * len);
56741ae08745Sheppo 
56751ae08745Sheppo 	D1(DBG_ALL_LDCS, "ldc_mem_dring_create: len=0x%x, size=0x%x\n",
56761ae08745Sheppo 	    len, dsize);
56771ae08745Sheppo 
56781ae08745Sheppo 	if (dhandle == NULL) {
56791ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid dhandle\n");
56801ae08745Sheppo 		return (EINVAL);
56811ae08745Sheppo 	}
56821ae08745Sheppo 
56831ae08745Sheppo 	if (len == 0) {
56841ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid length\n");
56851ae08745Sheppo 		return (EINVAL);
56861ae08745Sheppo 	}
56871ae08745Sheppo 
56881ae08745Sheppo 	/* descriptor size should be 8-byte aligned */
56891ae08745Sheppo 	if (dsize == 0 || (dsize & 0x7)) {
56901ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid size\n");
56911ae08745Sheppo 		return (EINVAL);
56921ae08745Sheppo 	}
56931ae08745Sheppo 
56941ae08745Sheppo 	*dhandle = 0;
56951ae08745Sheppo 
56961ae08745Sheppo 	/* Allocate a desc ring structure */
56971ae08745Sheppo 	dringp = kmem_zalloc(sizeof (ldc_dring_t), KM_SLEEP);
56981ae08745Sheppo 
56991ae08745Sheppo 	/* Initialize dring */
57001ae08745Sheppo 	dringp->length = len;
57011ae08745Sheppo 	dringp->dsize = dsize;
57021ae08745Sheppo 
57031ae08745Sheppo 	/* round off to multiple of pagesize */
57041ae08745Sheppo 	dringp->size = (size & MMU_PAGEMASK);
57051ae08745Sheppo 	if (size & MMU_PAGEOFFSET)
57061ae08745Sheppo 		dringp->size += MMU_PAGESIZE;
57071ae08745Sheppo 
57081ae08745Sheppo 	dringp->status = LDC_UNBOUND;
57091ae08745Sheppo 
57101ae08745Sheppo 	/* allocate descriptor ring memory */
57113af08d82Slm66018 	dringp->base = kmem_zalloc(dringp->size, KM_SLEEP);
57121ae08745Sheppo 
57131ae08745Sheppo 	/* initialize the desc ring lock */
57141ae08745Sheppo 	mutex_init(&dringp->lock, NULL, MUTEX_DRIVER, NULL);
57151ae08745Sheppo 
57161ae08745Sheppo 	/* Add descriptor ring to the head of global list */
57171ae08745Sheppo 	mutex_enter(&ldcssp->lock);
57181ae08745Sheppo 	dringp->next = ldcssp->dring_list;
57191ae08745Sheppo 	ldcssp->dring_list = dringp;
57201ae08745Sheppo 	mutex_exit(&ldcssp->lock);
57211ae08745Sheppo 
57221ae08745Sheppo 	*dhandle = (ldc_dring_handle_t)dringp;
57231ae08745Sheppo 
57241ae08745Sheppo 	D1(DBG_ALL_LDCS, "ldc_mem_dring_create: dring allocated\n");
57251ae08745Sheppo 
57261ae08745Sheppo 	return (0);
57271ae08745Sheppo }
57281ae08745Sheppo 
57291ae08745Sheppo 
57301ae08745Sheppo /*
57311ae08745Sheppo  * Destroy a descriptor ring.
57321ae08745Sheppo  */
57331ae08745Sheppo int
57341ae08745Sheppo ldc_mem_dring_destroy(ldc_dring_handle_t dhandle)
57351ae08745Sheppo {
57361ae08745Sheppo 	ldc_dring_t *dringp;
57371ae08745Sheppo 	ldc_dring_t *tmp_dringp;
57381ae08745Sheppo 
57391ae08745Sheppo 	D1(DBG_ALL_LDCS, "ldc_mem_dring_destroy: entered\n");
57401ae08745Sheppo 
57411ae08745Sheppo 	if (dhandle == NULL) {
57421ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
57431ae08745Sheppo 		    "ldc_mem_dring_destroy: invalid desc ring handle\n");
57441ae08745Sheppo 		return (EINVAL);
57451ae08745Sheppo 	}
57461ae08745Sheppo 	dringp = (ldc_dring_t *)dhandle;
57471ae08745Sheppo 
57481ae08745Sheppo 	if (dringp->status == LDC_BOUND) {
57491ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
57501ae08745Sheppo 		    "ldc_mem_dring_destroy: desc ring is bound\n");
57511ae08745Sheppo 		return (EACCES);
57521ae08745Sheppo 	}
57531ae08745Sheppo 
57541ae08745Sheppo 	mutex_enter(&dringp->lock);
57551ae08745Sheppo 	mutex_enter(&ldcssp->lock);
57561ae08745Sheppo 
57571ae08745Sheppo 	/* remove from linked list - if not bound */
57581ae08745Sheppo 	tmp_dringp = ldcssp->dring_list;
57591ae08745Sheppo 	if (tmp_dringp == dringp) {
57601ae08745Sheppo 		ldcssp->dring_list = dringp->next;
57611ae08745Sheppo 		dringp->next = NULL;
57621ae08745Sheppo 
57631ae08745Sheppo 	} else {
57641ae08745Sheppo 		while (tmp_dringp != NULL) {
57651ae08745Sheppo 			if (tmp_dringp->next == dringp) {
57661ae08745Sheppo 				tmp_dringp->next = dringp->next;
57671ae08745Sheppo 				dringp->next = NULL;
57681ae08745Sheppo 				break;
57691ae08745Sheppo 			}
57701ae08745Sheppo 			tmp_dringp = tmp_dringp->next;
57711ae08745Sheppo 		}
57721ae08745Sheppo 		if (tmp_dringp == NULL) {
57731ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
57741ae08745Sheppo 			    "ldc_mem_dring_destroy: invalid descriptor\n");
57751ae08745Sheppo 			mutex_exit(&ldcssp->lock);
57761ae08745Sheppo 			mutex_exit(&dringp->lock);
57771ae08745Sheppo 			return (EINVAL);
57781ae08745Sheppo 		}
57791ae08745Sheppo 	}
57801ae08745Sheppo 
57811ae08745Sheppo 	mutex_exit(&ldcssp->lock);
57821ae08745Sheppo 
57831ae08745Sheppo 	/* free the descriptor ring */
57843af08d82Slm66018 	kmem_free(dringp->base, dringp->size);
57851ae08745Sheppo 
57861ae08745Sheppo 	mutex_exit(&dringp->lock);
57871ae08745Sheppo 
57881ae08745Sheppo 	/* destroy dring lock */
57891ae08745Sheppo 	mutex_destroy(&dringp->lock);
57901ae08745Sheppo 
57911ae08745Sheppo 	/* free desc ring object */
57921ae08745Sheppo 	kmem_free(dringp, sizeof (ldc_dring_t));
57931ae08745Sheppo 
57941ae08745Sheppo 	return (0);
57951ae08745Sheppo }
57961ae08745Sheppo 
57971ae08745Sheppo /*
57981ae08745Sheppo  * Bind a previously allocated dring to a channel. The channel should
57991ae08745Sheppo  * be OPEN in order to bind the ring to the channel. Returns back a
58001ae08745Sheppo  * descriptor ring cookie. The descriptor ring is exported for remote
58011ae08745Sheppo  * access by the client at the other end of the channel. An entry for
58021ae08745Sheppo  * dring pages is stored in map table (via call to ldc_mem_bind_handle).
58031ae08745Sheppo  */
58041ae08745Sheppo int
58051ae08745Sheppo ldc_mem_dring_bind(ldc_handle_t handle, ldc_dring_handle_t dhandle,
58061ae08745Sheppo     uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount)
58071ae08745Sheppo {
58081ae08745Sheppo 	int		err;
58091ae08745Sheppo 	ldc_chan_t 	*ldcp;
58101ae08745Sheppo 	ldc_dring_t	*dringp;
58111ae08745Sheppo 	ldc_mem_handle_t mhandle;
58121ae08745Sheppo 
58131ae08745Sheppo 	/* check to see if channel is initalized */
58141ae08745Sheppo 	if (handle == NULL) {
58151ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
58161ae08745Sheppo 		    "ldc_mem_dring_bind: invalid channel handle\n");
58171ae08745Sheppo 		return (EINVAL);
58181ae08745Sheppo 	}
58191ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
58201ae08745Sheppo 
58211ae08745Sheppo 	if (dhandle == NULL) {
58221ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
58231ae08745Sheppo 		    "ldc_mem_dring_bind: invalid desc ring handle\n");
58241ae08745Sheppo 		return (EINVAL);
58251ae08745Sheppo 	}
58261ae08745Sheppo 	dringp = (ldc_dring_t *)dhandle;
58271ae08745Sheppo 
58281ae08745Sheppo 	if (cookie == NULL) {
58291ae08745Sheppo 		DWARN(ldcp->id,
58301ae08745Sheppo 		    "ldc_mem_dring_bind: invalid cookie arg\n");
58311ae08745Sheppo 		return (EINVAL);
58321ae08745Sheppo 	}
58331ae08745Sheppo 
58341ae08745Sheppo 	mutex_enter(&dringp->lock);
58351ae08745Sheppo 
58361ae08745Sheppo 	if (dringp->status == LDC_BOUND) {
58371ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
58381ae08745Sheppo 		    "ldc_mem_dring_bind: (0x%llx) descriptor ring is bound\n",
58391ae08745Sheppo 		    ldcp->id);
58401ae08745Sheppo 		mutex_exit(&dringp->lock);
58411ae08745Sheppo 		return (EINVAL);
58421ae08745Sheppo 	}
58431ae08745Sheppo 
58441ae08745Sheppo 	if ((perm & LDC_MEM_RW) == 0) {
58451ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
58461ae08745Sheppo 		    "ldc_mem_dring_bind: invalid permissions\n");
58471ae08745Sheppo 		mutex_exit(&dringp->lock);
58481ae08745Sheppo 		return (EINVAL);
58491ae08745Sheppo 	}
58501ae08745Sheppo 
58511ae08745Sheppo 	if ((mtype & (LDC_SHADOW_MAP|LDC_DIRECT_MAP|LDC_IO_MAP)) == 0) {
58521ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_mem_dring_bind: invalid type\n");
58531ae08745Sheppo 		mutex_exit(&dringp->lock);
58541ae08745Sheppo 		return (EINVAL);
58551ae08745Sheppo 	}
58561ae08745Sheppo 
58571ae08745Sheppo 	dringp->ldcp = ldcp;
58581ae08745Sheppo 
58591ae08745Sheppo 	/* create an memory handle */
58601ae08745Sheppo 	err = ldc_mem_alloc_handle(handle, &mhandle);
58611ae08745Sheppo 	if (err || mhandle == NULL) {
58621ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
58631ae08745Sheppo 		    "ldc_mem_dring_bind: (0x%llx) error allocating mhandle\n",
58641ae08745Sheppo 		    ldcp->id);
58651ae08745Sheppo 		mutex_exit(&dringp->lock);
58661ae08745Sheppo 		return (err);
58671ae08745Sheppo 	}
58681ae08745Sheppo 	dringp->mhdl = mhandle;
58691ae08745Sheppo 
58701ae08745Sheppo 	/* bind the descriptor ring to channel */
58711ae08745Sheppo 	err = ldc_mem_bind_handle(mhandle, dringp->base, dringp->size,
58721ae08745Sheppo 	    mtype, perm, cookie, ccount);
58731ae08745Sheppo 	if (err) {
58741ae08745Sheppo 		DWARN(ldcp->id,
58751ae08745Sheppo 		    "ldc_mem_dring_bind: (0x%llx) error binding mhandle\n",
58761ae08745Sheppo 		    ldcp->id);
58771ae08745Sheppo 		mutex_exit(&dringp->lock);
58781ae08745Sheppo 		return (err);
58791ae08745Sheppo 	}
58801ae08745Sheppo 
58811ae08745Sheppo 	/*
58821ae08745Sheppo 	 * For now return error if we get more than one cookie
58831ae08745Sheppo 	 * FUTURE: Return multiple cookies ..
58841ae08745Sheppo 	 */
58851ae08745Sheppo 	if (*ccount > 1) {
58861ae08745Sheppo 		(void) ldc_mem_unbind_handle(mhandle);
58871ae08745Sheppo 		(void) ldc_mem_free_handle(mhandle);
58881ae08745Sheppo 
58891ae08745Sheppo 		dringp->ldcp = NULL;
58901ae08745Sheppo 		dringp->mhdl = NULL;
58911ae08745Sheppo 		*ccount = 0;
58921ae08745Sheppo 
58931ae08745Sheppo 		mutex_exit(&dringp->lock);
58941ae08745Sheppo 		return (EAGAIN);
58951ae08745Sheppo 	}
58961ae08745Sheppo 
58971ae08745Sheppo 	/* Add descriptor ring to channel's exported dring list */
58981ae08745Sheppo 	mutex_enter(&ldcp->exp_dlist_lock);
58991ae08745Sheppo 	dringp->ch_next = ldcp->exp_dring_list;
59001ae08745Sheppo 	ldcp->exp_dring_list = dringp;
59011ae08745Sheppo 	mutex_exit(&ldcp->exp_dlist_lock);
59021ae08745Sheppo 
59031ae08745Sheppo 	dringp->status = LDC_BOUND;
59041ae08745Sheppo 
59051ae08745Sheppo 	mutex_exit(&dringp->lock);
59061ae08745Sheppo 
59071ae08745Sheppo 	return (0);
59081ae08745Sheppo }
59091ae08745Sheppo 
59101ae08745Sheppo /*
59111ae08745Sheppo  * Return the next cookie associated with the specified dring handle
59121ae08745Sheppo  */
59131ae08745Sheppo int
59141ae08745Sheppo ldc_mem_dring_nextcookie(ldc_dring_handle_t dhandle, ldc_mem_cookie_t *cookie)
59151ae08745Sheppo {
59161ae08745Sheppo 	int		rv = 0;
59171ae08745Sheppo 	ldc_dring_t 	*dringp;
59181ae08745Sheppo 	ldc_chan_t	*ldcp;
59191ae08745Sheppo 
59201ae08745Sheppo 	if (dhandle == NULL) {
59211ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
59221ae08745Sheppo 		    "ldc_mem_dring_nextcookie: invalid desc ring handle\n");
59231ae08745Sheppo 		return (EINVAL);
59241ae08745Sheppo 	}
59251ae08745Sheppo 	dringp = (ldc_dring_t *)dhandle;
59261ae08745Sheppo 	mutex_enter(&dringp->lock);
59271ae08745Sheppo 
59281ae08745Sheppo 	if (dringp->status != LDC_BOUND) {
59291ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
59301ae08745Sheppo 		    "ldc_mem_dring_nextcookie: descriptor ring 0x%llx "
59311ae08745Sheppo 		    "is not bound\n", dringp);
59321ae08745Sheppo 		mutex_exit(&dringp->lock);
59331ae08745Sheppo 		return (EINVAL);
59341ae08745Sheppo 	}
59351ae08745Sheppo 
59361ae08745Sheppo 	ldcp = dringp->ldcp;
59371ae08745Sheppo 
59381ae08745Sheppo 	if (cookie == NULL) {
59391ae08745Sheppo 		DWARN(ldcp->id,
59401ae08745Sheppo 		    "ldc_mem_dring_nextcookie:(0x%llx) invalid cookie arg\n",
59411ae08745Sheppo 		    ldcp->id);
59421ae08745Sheppo 		mutex_exit(&dringp->lock);
59431ae08745Sheppo 		return (EINVAL);
59441ae08745Sheppo 	}
59451ae08745Sheppo 
59461ae08745Sheppo 	rv = ldc_mem_nextcookie((ldc_mem_handle_t)dringp->mhdl, cookie);
59471ae08745Sheppo 	mutex_exit(&dringp->lock);
59481ae08745Sheppo 
59491ae08745Sheppo 	return (rv);
59501ae08745Sheppo }
59511ae08745Sheppo /*
59521ae08745Sheppo  * Unbind a previously bound dring from a channel.
59531ae08745Sheppo  */
59541ae08745Sheppo int
59551ae08745Sheppo ldc_mem_dring_unbind(ldc_dring_handle_t dhandle)
59561ae08745Sheppo {
59571ae08745Sheppo 	ldc_dring_t 	*dringp;
59581ae08745Sheppo 	ldc_dring_t	*tmp_dringp;
59591ae08745Sheppo 	ldc_chan_t	*ldcp;
59601ae08745Sheppo 
59611ae08745Sheppo 	if (dhandle == NULL) {
59621ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
59631ae08745Sheppo 		    "ldc_mem_dring_unbind: invalid desc ring handle\n");
59641ae08745Sheppo 		return (EINVAL);
59651ae08745Sheppo 	}
59661ae08745Sheppo 	dringp = (ldc_dring_t *)dhandle;
59671ae08745Sheppo 
59681ae08745Sheppo 	mutex_enter(&dringp->lock);
59691ae08745Sheppo 
59701ae08745Sheppo 	if (dringp->status == LDC_UNBOUND) {
59711ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
59721ae08745Sheppo 		    "ldc_mem_dring_bind: descriptor ring 0x%llx is unbound\n",
59731ae08745Sheppo 		    dringp);
59741ae08745Sheppo 		mutex_exit(&dringp->lock);
59751ae08745Sheppo 		return (EINVAL);
59761ae08745Sheppo 	}
59771ae08745Sheppo 	ldcp = dringp->ldcp;
59781ae08745Sheppo 
59791ae08745Sheppo 	mutex_enter(&ldcp->exp_dlist_lock);
59801ae08745Sheppo 
59811ae08745Sheppo 	tmp_dringp = ldcp->exp_dring_list;
59821ae08745Sheppo 	if (tmp_dringp == dringp) {
59831ae08745Sheppo 		ldcp->exp_dring_list = dringp->ch_next;
59841ae08745Sheppo 		dringp->ch_next = NULL;
59851ae08745Sheppo 
59861ae08745Sheppo 	} else {
59871ae08745Sheppo 		while (tmp_dringp != NULL) {
59881ae08745Sheppo 			if (tmp_dringp->ch_next == dringp) {
59891ae08745Sheppo 				tmp_dringp->ch_next = dringp->ch_next;
59901ae08745Sheppo 				dringp->ch_next = NULL;
59911ae08745Sheppo 				break;
59921ae08745Sheppo 			}
59931ae08745Sheppo 			tmp_dringp = tmp_dringp->ch_next;
59941ae08745Sheppo 		}
59951ae08745Sheppo 		if (tmp_dringp == NULL) {
59961ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
59971ae08745Sheppo 			    "ldc_mem_dring_unbind: invalid descriptor\n");
59981ae08745Sheppo 			mutex_exit(&ldcp->exp_dlist_lock);
59991ae08745Sheppo 			mutex_exit(&dringp->lock);
60001ae08745Sheppo 			return (EINVAL);
60011ae08745Sheppo 		}
60021ae08745Sheppo 	}
60031ae08745Sheppo 
60041ae08745Sheppo 	mutex_exit(&ldcp->exp_dlist_lock);
60051ae08745Sheppo 
60061ae08745Sheppo 	(void) ldc_mem_unbind_handle((ldc_mem_handle_t)dringp->mhdl);
60071ae08745Sheppo 	(void) ldc_mem_free_handle((ldc_mem_handle_t)dringp->mhdl);
60081ae08745Sheppo 
60091ae08745Sheppo 	dringp->ldcp = NULL;
60101ae08745Sheppo 	dringp->mhdl = NULL;
60111ae08745Sheppo 	dringp->status = LDC_UNBOUND;
60121ae08745Sheppo 
60131ae08745Sheppo 	mutex_exit(&dringp->lock);
60141ae08745Sheppo 
60151ae08745Sheppo 	return (0);
60161ae08745Sheppo }
60171ae08745Sheppo 
60181ae08745Sheppo /*
60191ae08745Sheppo  * Get information about the dring. The base address of the descriptor
60201ae08745Sheppo  * ring along with the type and permission are returned back.
60211ae08745Sheppo  */
60221ae08745Sheppo int
60231ae08745Sheppo ldc_mem_dring_info(ldc_dring_handle_t dhandle, ldc_mem_info_t *minfo)
60241ae08745Sheppo {
60251ae08745Sheppo 	ldc_dring_t	*dringp;
60261ae08745Sheppo 	int		rv;
60271ae08745Sheppo 
60281ae08745Sheppo 	if (dhandle == NULL) {
60291ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
60301ae08745Sheppo 		    "ldc_mem_dring_info: invalid desc ring handle\n");
60311ae08745Sheppo 		return (EINVAL);
60321ae08745Sheppo 	}
60331ae08745Sheppo 	dringp = (ldc_dring_t *)dhandle;
60341ae08745Sheppo 
60351ae08745Sheppo 	mutex_enter(&dringp->lock);
60361ae08745Sheppo 
60371ae08745Sheppo 	if (dringp->mhdl) {
60381ae08745Sheppo 		rv = ldc_mem_info(dringp->mhdl, minfo);
60391ae08745Sheppo 		if (rv) {
60401ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
60411ae08745Sheppo 			    "ldc_mem_dring_info: error reading mem info\n");
60421ae08745Sheppo 			mutex_exit(&dringp->lock);
60431ae08745Sheppo 			return (rv);
60441ae08745Sheppo 		}
60451ae08745Sheppo 	} else {
60461ae08745Sheppo 		minfo->vaddr = dringp->base;
60471ae08745Sheppo 		minfo->raddr = NULL;
60481ae08745Sheppo 		minfo->status = dringp->status;
60491ae08745Sheppo 	}
60501ae08745Sheppo 
60511ae08745Sheppo 	mutex_exit(&dringp->lock);
60521ae08745Sheppo 
60531ae08745Sheppo 	return (0);
60541ae08745Sheppo }
60551ae08745Sheppo 
60561ae08745Sheppo /*
60571ae08745Sheppo  * Map an exported descriptor ring into the local address space. If the
60581ae08745Sheppo  * descriptor ring was exported for direct map access, a HV call is made
60591ae08745Sheppo  * to allocate a RA range. If the map is done via a shadow copy, local
60601ae08745Sheppo  * shadow memory is allocated.
60611ae08745Sheppo  */
60621ae08745Sheppo int
60631ae08745Sheppo ldc_mem_dring_map(ldc_handle_t handle, ldc_mem_cookie_t *cookie,
60641ae08745Sheppo     uint32_t ccount, uint32_t len, uint32_t dsize, uint8_t mtype,
60651ae08745Sheppo     ldc_dring_handle_t *dhandle)
60661ae08745Sheppo {
60671ae08745Sheppo 	int		err;
60681ae08745Sheppo 	ldc_chan_t 	*ldcp = (ldc_chan_t *)handle;
60691ae08745Sheppo 	ldc_mem_handle_t mhandle;
60701ae08745Sheppo 	ldc_dring_t	*dringp;
60711ae08745Sheppo 	size_t		dring_size;
60721ae08745Sheppo 
60731ae08745Sheppo 	if (dhandle == NULL) {
60741ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
60751ae08745Sheppo 		    "ldc_mem_dring_map: invalid dhandle\n");
60761ae08745Sheppo 		return (EINVAL);
60771ae08745Sheppo 	}
60781ae08745Sheppo 
60791ae08745Sheppo 	/* check to see if channel is initalized */
60801ae08745Sheppo 	if (handle == NULL) {
60811ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
60821ae08745Sheppo 		    "ldc_mem_dring_map: invalid channel handle\n");
60831ae08745Sheppo 		return (EINVAL);
60841ae08745Sheppo 	}
60851ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
60861ae08745Sheppo 
60871ae08745Sheppo 	if (cookie == NULL) {
60881ae08745Sheppo 		DWARN(ldcp->id,
60891ae08745Sheppo 		    "ldc_mem_dring_map: (0x%llx) invalid cookie\n",
60901ae08745Sheppo 		    ldcp->id);
60911ae08745Sheppo 		return (EINVAL);
60921ae08745Sheppo 	}
60931ae08745Sheppo 
60941ae08745Sheppo 	/* FUTURE: For now we support only one cookie per dring */
60951ae08745Sheppo 	ASSERT(ccount == 1);
60961ae08745Sheppo 
60971ae08745Sheppo 	if (cookie->size < (dsize * len)) {
60981ae08745Sheppo 		DWARN(ldcp->id,
60991ae08745Sheppo 		    "ldc_mem_dring_map: (0x%llx) invalid dsize/len\n",
61001ae08745Sheppo 		    ldcp->id);
61011ae08745Sheppo 		return (EINVAL);
61021ae08745Sheppo 	}
61031ae08745Sheppo 
61041ae08745Sheppo 	*dhandle = 0;
61051ae08745Sheppo 
61061ae08745Sheppo 	/* Allocate an dring structure */
61071ae08745Sheppo 	dringp = kmem_zalloc(sizeof (ldc_dring_t), KM_SLEEP);
61081ae08745Sheppo 
61091ae08745Sheppo 	D1(ldcp->id,
61101ae08745Sheppo 	    "ldc_mem_dring_map: 0x%x,0x%x,0x%x,0x%llx,0x%llx\n",
61111ae08745Sheppo 	    mtype, len, dsize, cookie->addr, cookie->size);
61121ae08745Sheppo 
61131ae08745Sheppo 	/* Initialize dring */
61141ae08745Sheppo 	dringp->length = len;
61151ae08745Sheppo 	dringp->dsize = dsize;
61161ae08745Sheppo 
61171ae08745Sheppo 	/* round of to multiple of page size */
61181ae08745Sheppo 	dring_size = len * dsize;
61191ae08745Sheppo 	dringp->size = (dring_size & MMU_PAGEMASK);
61201ae08745Sheppo 	if (dring_size & MMU_PAGEOFFSET)
61211ae08745Sheppo 		dringp->size += MMU_PAGESIZE;
61221ae08745Sheppo 
61231ae08745Sheppo 	dringp->ldcp = ldcp;
61241ae08745Sheppo 
61251ae08745Sheppo 	/* create an memory handle */
61261ae08745Sheppo 	err = ldc_mem_alloc_handle(handle, &mhandle);
61271ae08745Sheppo 	if (err || mhandle == NULL) {
61281ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
61291ae08745Sheppo 		    "ldc_mem_dring_map: cannot alloc hdl err=%d\n",
61301ae08745Sheppo 		    err);
61311ae08745Sheppo 		kmem_free(dringp, sizeof (ldc_dring_t));
61321ae08745Sheppo 		return (ENOMEM);
61331ae08745Sheppo 	}
61341ae08745Sheppo 
61351ae08745Sheppo 	dringp->mhdl = mhandle;
61361ae08745Sheppo 	dringp->base = NULL;
61371ae08745Sheppo 
61381ae08745Sheppo 	/* map the dring into local memory */
61394bac2208Snarayan 	err = ldc_mem_map(mhandle, cookie, ccount, mtype, LDC_MEM_RW,
61401ae08745Sheppo 	    &(dringp->base), NULL);
61411ae08745Sheppo 	if (err || dringp->base == NULL) {
61421ae08745Sheppo 		cmn_err(CE_WARN,
61431ae08745Sheppo 		    "ldc_mem_dring_map: cannot map desc ring err=%d\n", err);
61441ae08745Sheppo 		(void) ldc_mem_free_handle(mhandle);
61451ae08745Sheppo 		kmem_free(dringp, sizeof (ldc_dring_t));
61461ae08745Sheppo 		return (ENOMEM);
61471ae08745Sheppo 	}
61481ae08745Sheppo 
61491ae08745Sheppo 	/* initialize the desc ring lock */
61501ae08745Sheppo 	mutex_init(&dringp->lock, NULL, MUTEX_DRIVER, NULL);
61511ae08745Sheppo 
61521ae08745Sheppo 	/* Add descriptor ring to channel's imported dring list */
61531ae08745Sheppo 	mutex_enter(&ldcp->imp_dlist_lock);
61541ae08745Sheppo 	dringp->ch_next = ldcp->imp_dring_list;
61551ae08745Sheppo 	ldcp->imp_dring_list = dringp;
61561ae08745Sheppo 	mutex_exit(&ldcp->imp_dlist_lock);
61571ae08745Sheppo 
61581ae08745Sheppo 	dringp->status = LDC_MAPPED;
61591ae08745Sheppo 
61601ae08745Sheppo 	*dhandle = (ldc_dring_handle_t)dringp;
61611ae08745Sheppo 
61621ae08745Sheppo 	return (0);
61631ae08745Sheppo }
61641ae08745Sheppo 
61651ae08745Sheppo /*
61661ae08745Sheppo  * Unmap a descriptor ring. Free shadow memory (if any).
61671ae08745Sheppo  */
61681ae08745Sheppo int
61691ae08745Sheppo ldc_mem_dring_unmap(ldc_dring_handle_t dhandle)
61701ae08745Sheppo {
61711ae08745Sheppo 	ldc_dring_t 	*dringp;
61721ae08745Sheppo 	ldc_dring_t	*tmp_dringp;
61731ae08745Sheppo 	ldc_chan_t	*ldcp;
61741ae08745Sheppo 
61751ae08745Sheppo 	if (dhandle == NULL) {
61761ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
61771ae08745Sheppo 		    "ldc_mem_dring_unmap: invalid desc ring handle\n");
61781ae08745Sheppo 		return (EINVAL);
61791ae08745Sheppo 	}
61801ae08745Sheppo 	dringp = (ldc_dring_t *)dhandle;
61811ae08745Sheppo 
61821ae08745Sheppo 	if (dringp->status != LDC_MAPPED) {
61831ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
61841ae08745Sheppo 		    "ldc_mem_dring_unmap: not a mapped desc ring\n");
61851ae08745Sheppo 		return (EINVAL);
61861ae08745Sheppo 	}
61871ae08745Sheppo 
61881ae08745Sheppo 	mutex_enter(&dringp->lock);
61891ae08745Sheppo 
61901ae08745Sheppo 	ldcp = dringp->ldcp;
61911ae08745Sheppo 
61921ae08745Sheppo 	mutex_enter(&ldcp->imp_dlist_lock);
61931ae08745Sheppo 
61941ae08745Sheppo 	/* find and unlink the desc ring from channel import list */
61951ae08745Sheppo 	tmp_dringp = ldcp->imp_dring_list;
61961ae08745Sheppo 	if (tmp_dringp == dringp) {
61971ae08745Sheppo 		ldcp->imp_dring_list = dringp->ch_next;
61981ae08745Sheppo 		dringp->ch_next = NULL;
61991ae08745Sheppo 
62001ae08745Sheppo 	} else {
62011ae08745Sheppo 		while (tmp_dringp != NULL) {
62021ae08745Sheppo 			if (tmp_dringp->ch_next == dringp) {
62031ae08745Sheppo 				tmp_dringp->ch_next = dringp->ch_next;
62041ae08745Sheppo 				dringp->ch_next = NULL;
62051ae08745Sheppo 				break;
62061ae08745Sheppo 			}
62071ae08745Sheppo 			tmp_dringp = tmp_dringp->ch_next;
62081ae08745Sheppo 		}
62091ae08745Sheppo 		if (tmp_dringp == NULL) {
62101ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
62111ae08745Sheppo 			    "ldc_mem_dring_unmap: invalid descriptor\n");
62121ae08745Sheppo 			mutex_exit(&ldcp->imp_dlist_lock);
62131ae08745Sheppo 			mutex_exit(&dringp->lock);
62141ae08745Sheppo 			return (EINVAL);
62151ae08745Sheppo 		}
62161ae08745Sheppo 	}
62171ae08745Sheppo 
62181ae08745Sheppo 	mutex_exit(&ldcp->imp_dlist_lock);
62191ae08745Sheppo 
62201ae08745Sheppo 	/* do a LDC memory handle unmap and free */
62211ae08745Sheppo 	(void) ldc_mem_unmap(dringp->mhdl);
62221ae08745Sheppo 	(void) ldc_mem_free_handle((ldc_mem_handle_t)dringp->mhdl);
62231ae08745Sheppo 
62241ae08745Sheppo 	dringp->status = 0;
62251ae08745Sheppo 	dringp->ldcp = NULL;
62261ae08745Sheppo 
62271ae08745Sheppo 	mutex_exit(&dringp->lock);
62281ae08745Sheppo 
62291ae08745Sheppo 	/* destroy dring lock */
62301ae08745Sheppo 	mutex_destroy(&dringp->lock);
62311ae08745Sheppo 
62321ae08745Sheppo 	/* free desc ring object */
62331ae08745Sheppo 	kmem_free(dringp, sizeof (ldc_dring_t));
62341ae08745Sheppo 
62351ae08745Sheppo 	return (0);
62361ae08745Sheppo }
62371ae08745Sheppo 
62381ae08745Sheppo /*
62391ae08745Sheppo  * Internal entry point for descriptor ring access entry consistency
62401ae08745Sheppo  * semantics. Acquire copies the contents of the remote descriptor ring
62411ae08745Sheppo  * into the local shadow copy. The release operation copies the local
62421ae08745Sheppo  * contents into the remote dring. The start and end locations specify
62431ae08745Sheppo  * bounds for the entries being synchronized.
62441ae08745Sheppo  */
62451ae08745Sheppo static int
62461ae08745Sheppo i_ldc_dring_acquire_release(ldc_dring_handle_t dhandle,
62471ae08745Sheppo     uint8_t direction, uint64_t start, uint64_t end)
62481ae08745Sheppo {
62491ae08745Sheppo 	int 			err;
62501ae08745Sheppo 	ldc_dring_t		*dringp;
62511ae08745Sheppo 	ldc_chan_t		*ldcp;
62521ae08745Sheppo 	uint64_t		soff;
62531ae08745Sheppo 	size_t			copy_size;
62541ae08745Sheppo 
62551ae08745Sheppo 	if (dhandle == NULL) {
62561ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
62571ae08745Sheppo 		    "i_ldc_dring_acquire_release: invalid desc ring handle\n");
62581ae08745Sheppo 		return (EINVAL);
62591ae08745Sheppo 	}
62601ae08745Sheppo 	dringp = (ldc_dring_t *)dhandle;
62611ae08745Sheppo 	mutex_enter(&dringp->lock);
62621ae08745Sheppo 
62631ae08745Sheppo 	if (dringp->status != LDC_MAPPED || dringp->ldcp == NULL) {
62641ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
62651ae08745Sheppo 		    "i_ldc_dring_acquire_release: not a mapped desc ring\n");
62661ae08745Sheppo 		mutex_exit(&dringp->lock);
62671ae08745Sheppo 		return (EINVAL);
62681ae08745Sheppo 	}
62691ae08745Sheppo 
62701ae08745Sheppo 	if (start >= dringp->length || end >= dringp->length) {
62711ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
62721ae08745Sheppo 		    "i_ldc_dring_acquire_release: index out of range\n");
62731ae08745Sheppo 		mutex_exit(&dringp->lock);
62741ae08745Sheppo 		return (EINVAL);
62751ae08745Sheppo 	}
62761ae08745Sheppo 
62771ae08745Sheppo 	/* get the channel handle */
62781ae08745Sheppo 	ldcp = dringp->ldcp;
62791ae08745Sheppo 
62801ae08745Sheppo 	copy_size = (start <= end) ? (((end - start) + 1) * dringp->dsize) :
62811ae08745Sheppo 		((dringp->length - start) * dringp->dsize);
62821ae08745Sheppo 
62831ae08745Sheppo 	/* Calculate the relative offset for the first desc */
62841ae08745Sheppo 	soff = (start * dringp->dsize);
62851ae08745Sheppo 
62861ae08745Sheppo 	/* copy to/from remote from/to local memory */
62871ae08745Sheppo 	D1(ldcp->id, "i_ldc_dring_acquire_release: c1 off=0x%llx sz=0x%llx\n",
62881ae08745Sheppo 	    soff, copy_size);
62891ae08745Sheppo 	err = i_ldc_mem_acquire_release((ldc_mem_handle_t)dringp->mhdl,
62901ae08745Sheppo 	    direction, soff, copy_size);
62911ae08745Sheppo 	if (err) {
62921ae08745Sheppo 		DWARN(ldcp->id,
62931ae08745Sheppo 		    "i_ldc_dring_acquire_release: copy failed\n");
62941ae08745Sheppo 		mutex_exit(&dringp->lock);
62951ae08745Sheppo 		return (err);
62961ae08745Sheppo 	}
62971ae08745Sheppo 
62981ae08745Sheppo 	/* do the balance */
62991ae08745Sheppo 	if (start > end) {
63001ae08745Sheppo 		copy_size = ((end + 1) * dringp->dsize);
63011ae08745Sheppo 		soff = 0;
63021ae08745Sheppo 
63031ae08745Sheppo 		/* copy to/from remote from/to local memory */
63041ae08745Sheppo 		D1(ldcp->id, "i_ldc_dring_acquire_release: c2 "
63051ae08745Sheppo 		    "off=0x%llx sz=0x%llx\n", soff, copy_size);
63061ae08745Sheppo 		err = i_ldc_mem_acquire_release((ldc_mem_handle_t)dringp->mhdl,
63071ae08745Sheppo 		    direction, soff, copy_size);
63081ae08745Sheppo 		if (err) {
63091ae08745Sheppo 			DWARN(ldcp->id,
63101ae08745Sheppo 			    "i_ldc_dring_acquire_release: copy failed\n");
63111ae08745Sheppo 			mutex_exit(&dringp->lock);
63121ae08745Sheppo 			return (err);
63131ae08745Sheppo 		}
63141ae08745Sheppo 	}
63151ae08745Sheppo 
63161ae08745Sheppo 	mutex_exit(&dringp->lock);
63171ae08745Sheppo 
63181ae08745Sheppo 	return (0);
63191ae08745Sheppo }
63201ae08745Sheppo 
63211ae08745Sheppo /*
63221ae08745Sheppo  * Ensure that the contents in the local dring are consistent
63231ae08745Sheppo  * with the contents if of remote dring
63241ae08745Sheppo  */
63251ae08745Sheppo int
63261ae08745Sheppo ldc_mem_dring_acquire(ldc_dring_handle_t dhandle, uint64_t start, uint64_t end)
63271ae08745Sheppo {
63281ae08745Sheppo 	return (i_ldc_dring_acquire_release(dhandle, LDC_COPY_IN, start, end));
63291ae08745Sheppo }
63301ae08745Sheppo 
63311ae08745Sheppo /*
63321ae08745Sheppo  * Ensure that the contents in the remote dring are consistent
63331ae08745Sheppo  * with the contents if of local dring
63341ae08745Sheppo  */
63351ae08745Sheppo int
63361ae08745Sheppo ldc_mem_dring_release(ldc_dring_handle_t dhandle, uint64_t start, uint64_t end)
63371ae08745Sheppo {
63381ae08745Sheppo 	return (i_ldc_dring_acquire_release(dhandle, LDC_COPY_OUT, start, end));
63391ae08745Sheppo }
63401ae08745Sheppo 
63411ae08745Sheppo 
63421ae08745Sheppo /* ------------------------------------------------------------------------- */
6343