xref: /illumos-gate/usr/src/uts/sun4v/io/ldc.c (revision 12f80fa62ea9b3208390b1204a47ed5498881e7d)
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 /*
2358283286Sha137994  * Copyright 2008 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/cred.h>
451ae08745Sheppo #include <sys/promif.h>
461ae08745Sheppo #include <sys/ddi.h>
471ae08745Sheppo #include <sys/sunddi.h>
481ae08745Sheppo #include <sys/cyclic.h>
491ae08745Sheppo #include <sys/machsystm.h>
501ae08745Sheppo #include <sys/vm.h>
511ae08745Sheppo #include <sys/cpu.h>
521ae08745Sheppo #include <sys/intreg.h>
531ae08745Sheppo #include <sys/machcpuvar.h>
544bac2208Snarayan #include <sys/mmu.h>
554bac2208Snarayan #include <sys/pte.h>
564bac2208Snarayan #include <vm/hat.h>
574bac2208Snarayan #include <vm/as.h>
584bac2208Snarayan #include <vm/hat_sfmmu.h>
594bac2208Snarayan #include <sys/vm_machparam.h>
604bac2208Snarayan #include <vm/seg_kmem.h>
614bac2208Snarayan #include <vm/seg_kpm.h>
621ae08745Sheppo #include <sys/note.h>
631ae08745Sheppo #include <sys/ivintr.h>
641ae08745Sheppo #include <sys/hypervisor_api.h>
651ae08745Sheppo #include <sys/ldc.h>
661ae08745Sheppo #include <sys/ldc_impl.h>
671ae08745Sheppo #include <sys/cnex.h>
681ae08745Sheppo #include <sys/hsvc.h>
6958283286Sha137994 #include <sys/sdt.h>
701ae08745Sheppo 
711ae08745Sheppo /* Core internal functions */
7220ae46ebSha137994 int i_ldc_h2v_error(int h_error);
7320ae46ebSha137994 void i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset);
7420ae46ebSha137994 
751ae08745Sheppo static int i_ldc_txq_reconf(ldc_chan_t *ldcp);
763af08d82Slm66018 static int i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset);
77a8ea4edeSnarayan static int i_ldc_rxq_drain(ldc_chan_t *ldcp);
781ae08745Sheppo static void i_ldc_reset_state(ldc_chan_t *ldcp);
791ae08745Sheppo 
801ae08745Sheppo static int i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail);
8122f747efSnarayan static void i_ldc_get_tx_head(ldc_chan_t *ldcp, uint64_t *head);
821ae08745Sheppo static int i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail);
831ae08745Sheppo static int i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head);
841ae08745Sheppo static int i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype,
851ae08745Sheppo     uint8_t ctrlmsg);
861ae08745Sheppo 
8758283286Sha137994 static int  i_ldc_set_rxdq_head(ldc_chan_t *ldcp, uint64_t head);
8858283286Sha137994 static void i_ldc_rxdq_copy(ldc_chan_t *ldcp, uint64_t *head);
8958283286Sha137994 static uint64_t i_ldc_dq_rx_get_state(ldc_chan_t *ldcp, uint64_t *head,
9058283286Sha137994     uint64_t *tail, uint64_t *link_state);
9158283286Sha137994 static uint64_t i_ldc_hvq_rx_get_state(ldc_chan_t *ldcp, uint64_t *head,
9258283286Sha137994     uint64_t *tail, uint64_t *link_state);
9358283286Sha137994 static int i_ldc_rx_ackpeek(ldc_chan_t *ldcp, uint64_t rx_head,
9458283286Sha137994     uint64_t rx_tail);
9558283286Sha137994 static uint_t i_ldc_chkq(ldc_chan_t *ldcp);
9658283286Sha137994 
971ae08745Sheppo /* Interrupt handling functions */
981ae08745Sheppo static uint_t i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2);
991ae08745Sheppo static uint_t i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2);
10058283286Sha137994 static uint_t i_ldc_rx_process_hvq(ldc_chan_t *ldcp, boolean_t *notify_client,
10158283286Sha137994     uint64_t *notify_event);
1021ae08745Sheppo static void i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype);
1031ae08745Sheppo 
1041ae08745Sheppo /* Read method functions */
1051ae08745Sheppo static int i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep);
1061ae08745Sheppo static int i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp,
1071ae08745Sheppo 	size_t *sizep);
1081ae08745Sheppo static int i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp,
1091ae08745Sheppo 	size_t *sizep);
1101ae08745Sheppo 
1111ae08745Sheppo /* Write method functions */
1121ae08745Sheppo static int i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t target_bufp,
1131ae08745Sheppo 	size_t *sizep);
1141ae08745Sheppo static int i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t target_bufp,
1151ae08745Sheppo 	size_t *sizep);
1161ae08745Sheppo static int i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t target_bufp,
1171ae08745Sheppo 	size_t *sizep);
1181ae08745Sheppo 
1191ae08745Sheppo /* Pkt processing internal functions */
1201ae08745Sheppo static int i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg);
1211ae08745Sheppo static int i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg);
1221ae08745Sheppo static int i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg);
1231ae08745Sheppo static int i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg);
1241ae08745Sheppo static int i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg);
1251ae08745Sheppo static int i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg);
1261ae08745Sheppo static int i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg);
1271ae08745Sheppo 
1281ae08745Sheppo /* LDC Version */
1291ae08745Sheppo static ldc_ver_t ldc_versions[] = { {1, 0} };
1301ae08745Sheppo 
1311ae08745Sheppo /* number of supported versions */
1321ae08745Sheppo #define	LDC_NUM_VERS	(sizeof (ldc_versions) / sizeof (ldc_versions[0]))
1331ae08745Sheppo 
13458283286Sha137994 /* Invalid value for the ldc_chan_t rx_ack_head field */
13558283286Sha137994 #define	ACKPEEK_HEAD_INVALID	((uint64_t)-1)
13658283286Sha137994 
13758283286Sha137994 
1381ae08745Sheppo /* Module State Pointer */
13920ae46ebSha137994 ldc_soft_state_t *ldcssp;
1401ae08745Sheppo 
1411ae08745Sheppo static struct modldrv md = {
1421ae08745Sheppo 	&mod_miscops,			/* This is a misc module */
1431ae08745Sheppo 	"sun4v LDC module v%I%",	/* Name of the module */
1441ae08745Sheppo };
1451ae08745Sheppo 
1461ae08745Sheppo static struct modlinkage ml = {
1471ae08745Sheppo 	MODREV_1,
1481ae08745Sheppo 	&md,
1491ae08745Sheppo 	NULL
1501ae08745Sheppo };
1511ae08745Sheppo 
1521ae08745Sheppo static uint64_t ldc_sup_minor;		/* Supported minor number */
1531ae08745Sheppo static hsvc_info_t ldc_hsvc = {
154bbfa0259Sha137994 	HSVC_REV_1, NULL, HSVC_GROUP_LDC, 1, 1, "ldc"
1551ae08745Sheppo };
1561ae08745Sheppo 
1574bac2208Snarayan /*
158e1ebb9ecSlm66018  * The no. of MTU size messages that can be stored in
159e1ebb9ecSlm66018  * the LDC Tx queue. The number of Tx queue entries is
160e1ebb9ecSlm66018  * then computed as (mtu * mtu_msgs)/sizeof(queue_entry)
161e1ebb9ecSlm66018  */
162e1ebb9ecSlm66018 uint64_t ldc_mtu_msgs = LDC_MTU_MSGS;
163e1ebb9ecSlm66018 
164e1ebb9ecSlm66018 /*
165e1ebb9ecSlm66018  * The minimum queue length. This is the size of the smallest
166e1ebb9ecSlm66018  * LDC queue. If the computed value is less than this default,
167e1ebb9ecSlm66018  * the queue length is rounded up to 'ldc_queue_entries'.
168e1ebb9ecSlm66018  */
169e1ebb9ecSlm66018 uint64_t ldc_queue_entries = LDC_QUEUE_ENTRIES;
170e1ebb9ecSlm66018 
171e1ebb9ecSlm66018 /*
17258283286Sha137994  * The length of the reliable-mode data queue in terms of the LDC
17358283286Sha137994  * receive queue length. i.e., the number of times larger than the
17458283286Sha137994  * LDC receive queue that the data queue should be. The HV receive
17558283286Sha137994  * queue is required to be a power of 2 and this implementation
17658283286Sha137994  * assumes the data queue will also be a power of 2. By making the
17758283286Sha137994  * multiplier a power of 2, we ensure the data queue will be a
17858283286Sha137994  * power of 2. We use a multiplier because the receive queue is
17958283286Sha137994  * sized to be sane relative to the MTU and the same is needed for
18058283286Sha137994  * the data queue.
18158283286Sha137994  */
18258283286Sha137994 uint64_t ldc_rxdq_multiplier = LDC_RXDQ_MULTIPLIER;
18358283286Sha137994 
18458283286Sha137994 /*
185e1ebb9ecSlm66018  * LDC retry count and delay - when the HV returns EWOULDBLOCK
186e1ebb9ecSlm66018  * the operation is retried 'ldc_max_retries' times with a
187e1ebb9ecSlm66018  * wait of 'ldc_delay' usecs between each retry.
1880a55fbb7Slm66018  */
1890a55fbb7Slm66018 int ldc_max_retries = LDC_MAX_RETRIES;
1900a55fbb7Slm66018 clock_t ldc_delay = LDC_DELAY;
1910a55fbb7Slm66018 
1924d39be2bSsg70180 /*
1934d39be2bSsg70180  * delay between each retry of channel unregistration in
1944d39be2bSsg70180  * ldc_close(), to wait for pending interrupts to complete.
1954d39be2bSsg70180  */
1964d39be2bSsg70180 clock_t ldc_close_delay = LDC_CLOSE_DELAY;
1974d39be2bSsg70180 
1981ae08745Sheppo #ifdef DEBUG
1991ae08745Sheppo 
2001ae08745Sheppo /*
2011ae08745Sheppo  * Print debug messages
2021ae08745Sheppo  *
2031ae08745Sheppo  * set ldcdbg to 0x7 for enabling all msgs
2041ae08745Sheppo  * 0x4 - Warnings
2051ae08745Sheppo  * 0x2 - All debug messages
2061ae08745Sheppo  * 0x1 - Minimal debug messages
2071ae08745Sheppo  *
2081ae08745Sheppo  * set ldcdbgchan to the channel number you want to debug
2091ae08745Sheppo  * setting it to -1 prints debug messages for all channels
2101ae08745Sheppo  * NOTE: ldcdbgchan has no effect on error messages
2111ae08745Sheppo  */
2121ae08745Sheppo 
2131ae08745Sheppo int ldcdbg = 0x0;
2141ae08745Sheppo int64_t ldcdbgchan = DBG_ALL_LDCS;
21583d3bc6fSnarayan uint64_t ldc_inject_err_flag = 0;
2161ae08745Sheppo 
21720ae46ebSha137994 void
2181ae08745Sheppo ldcdebug(int64_t id, const char *fmt, ...)
2191ae08745Sheppo {
2201ae08745Sheppo 	char buf[512];
2211ae08745Sheppo 	va_list ap;
2221ae08745Sheppo 
2231ae08745Sheppo 	/*
2241ae08745Sheppo 	 * Do not return if,
2251ae08745Sheppo 	 * caller wants to print it anyway - (id == DBG_ALL_LDCS)
2261ae08745Sheppo 	 * debug channel is set to all LDCs - (ldcdbgchan == DBG_ALL_LDCS)
2271ae08745Sheppo 	 * debug channel = caller specified channel
2281ae08745Sheppo 	 */
2291ae08745Sheppo 	if ((id != DBG_ALL_LDCS) &&
2301ae08745Sheppo 	    (ldcdbgchan != DBG_ALL_LDCS) &&
2311ae08745Sheppo 	    (ldcdbgchan != id)) {
2321ae08745Sheppo 		return;
2331ae08745Sheppo 	}
2341ae08745Sheppo 
2351ae08745Sheppo 	va_start(ap, fmt);
2361ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
2371ae08745Sheppo 	va_end(ap);
2381ae08745Sheppo 
2393af08d82Slm66018 	cmn_err(CE_CONT, "?%s", buf);
2403af08d82Slm66018 }
2413af08d82Slm66018 
24283d3bc6fSnarayan #define	LDC_ERR_RESET		0x1
24383d3bc6fSnarayan #define	LDC_ERR_PKTLOSS		0x2
24458283286Sha137994 #define	LDC_ERR_DQFULL		0x4
245bbfa0259Sha137994 #define	LDC_ERR_DRNGCLEAR	0x8
24683d3bc6fSnarayan 
2473af08d82Slm66018 static boolean_t
24883d3bc6fSnarayan ldc_inject_error(ldc_chan_t *ldcp, uint64_t error)
2493af08d82Slm66018 {
2503af08d82Slm66018 	if ((ldcdbgchan != DBG_ALL_LDCS) && (ldcdbgchan != ldcp->id))
2513af08d82Slm66018 		return (B_FALSE);
2523af08d82Slm66018 
25383d3bc6fSnarayan 	if ((ldc_inject_err_flag & error) == 0)
2543af08d82Slm66018 		return (B_FALSE);
2553af08d82Slm66018 
2563af08d82Slm66018 	/* clear the injection state */
25783d3bc6fSnarayan 	ldc_inject_err_flag &= ~error;
2583af08d82Slm66018 
2593af08d82Slm66018 	return (B_TRUE);
2601ae08745Sheppo }
2611ae08745Sheppo 
2621ae08745Sheppo #define	D1		\
2631ae08745Sheppo if (ldcdbg & 0x01)	\
2641ae08745Sheppo 	ldcdebug
2651ae08745Sheppo 
2661ae08745Sheppo #define	D2		\
2671ae08745Sheppo if (ldcdbg & 0x02)	\
2681ae08745Sheppo 	ldcdebug
2691ae08745Sheppo 
2701ae08745Sheppo #define	DWARN		\
2711ae08745Sheppo if (ldcdbg & 0x04)	\
2721ae08745Sheppo 	ldcdebug
2731ae08745Sheppo 
2741ae08745Sheppo #define	DUMP_PAYLOAD(id, addr)						\
2751ae08745Sheppo {									\
2761ae08745Sheppo 	char buf[65*3];							\
2771ae08745Sheppo 	int i;								\
2781ae08745Sheppo 	uint8_t *src = (uint8_t *)addr;					\
2791ae08745Sheppo 	for (i = 0; i < 64; i++, src++)					\
2801ae08745Sheppo 		(void) sprintf(&buf[i * 3], "|%02x", *src);		\
2811ae08745Sheppo 	(void) sprintf(&buf[i * 3], "|\n");				\
2821ae08745Sheppo 	D2((id), "payload: %s", buf);					\
2831ae08745Sheppo }
2841ae08745Sheppo 
2851ae08745Sheppo #define	DUMP_LDC_PKT(c, s, addr)					\
2861ae08745Sheppo {									\
2871ae08745Sheppo 	ldc_msg_t *msg = (ldc_msg_t *)(addr);				\
2881ae08745Sheppo 	uint32_t mid = ((c)->mode != LDC_MODE_RAW) ? msg->seqid : 0;	\
2891ae08745Sheppo 	if (msg->type == LDC_DATA) {                                    \
2901ae08745Sheppo 	    D2((c)->id, "%s: msg%d (/%x/%x/%x/,env[%c%c,sz=%d])",	\
2911ae08745Sheppo 	    (s), mid, msg->type, msg->stype, msg->ctrl,			\
2921ae08745Sheppo 	    (msg->env & LDC_FRAG_START) ? 'B' : ' ',                    \
2931ae08745Sheppo 	    (msg->env & LDC_FRAG_STOP) ? 'E' : ' ',                     \
2941ae08745Sheppo 	    (msg->env & LDC_LEN_MASK));					\
2951ae08745Sheppo 	} else { 							\
2961ae08745Sheppo 	    D2((c)->id, "%s: msg%d (/%x/%x/%x/,env=%x)", (s),		\
2971ae08745Sheppo 	    mid, msg->type, msg->stype, msg->ctrl, msg->env);		\
2981ae08745Sheppo 	} 								\
2991ae08745Sheppo }
3001ae08745Sheppo 
30183d3bc6fSnarayan #define	LDC_INJECT_RESET(_ldcp)	ldc_inject_error(_ldcp, LDC_ERR_RESET)
30283d3bc6fSnarayan #define	LDC_INJECT_PKTLOSS(_ldcp) ldc_inject_error(_ldcp, LDC_ERR_PKTLOSS)
30358283286Sha137994 #define	LDC_INJECT_DQFULL(_ldcp) ldc_inject_error(_ldcp, LDC_ERR_DQFULL)
304bbfa0259Sha137994 #define	LDC_INJECT_DRNGCLEAR(_ldcp) ldc_inject_error(_ldcp, LDC_ERR_DRNGCLEAR)
305bbfa0259Sha137994 extern void i_ldc_mem_inject_dring_clear(ldc_chan_t *ldcp);
3063af08d82Slm66018 
3071ae08745Sheppo #else
3081ae08745Sheppo 
3091ae08745Sheppo #define	DBG_ALL_LDCS -1
3101ae08745Sheppo 
3111ae08745Sheppo #define	D1
3121ae08745Sheppo #define	D2
3131ae08745Sheppo #define	DWARN
3141ae08745Sheppo 
3151ae08745Sheppo #define	DUMP_PAYLOAD(id, addr)
3161ae08745Sheppo #define	DUMP_LDC_PKT(c, s, addr)
3171ae08745Sheppo 
3183af08d82Slm66018 #define	LDC_INJECT_RESET(_ldcp)	(B_FALSE)
31983d3bc6fSnarayan #define	LDC_INJECT_PKTLOSS(_ldcp) (B_FALSE)
32058283286Sha137994 #define	LDC_INJECT_DQFULL(_ldcp) (B_FALSE)
321bbfa0259Sha137994 #define	LDC_INJECT_DRNGCLEAR(_ldcp) (B_FALSE)
3223af08d82Slm66018 
3231ae08745Sheppo #endif
3241ae08745Sheppo 
32558283286Sha137994 /*
32658283286Sha137994  * dtrace SDT probes to ease tracing of the rx data queue and HV queue
32758283286Sha137994  * lengths. Just pass the head, tail, and entries values so that the
32858283286Sha137994  * length can be calculated in a dtrace script when the probe is enabled.
32958283286Sha137994  */
33058283286Sha137994 #define	TRACE_RXDQ_LENGTH(ldcp)						\
33158283286Sha137994 	DTRACE_PROBE4(rxdq__size,					\
33258283286Sha137994 	uint64_t, ldcp->id,						\
33358283286Sha137994 	uint64_t, ldcp->rx_dq_head,					\
33458283286Sha137994 	uint64_t, ldcp->rx_dq_tail,					\
33558283286Sha137994 	uint64_t, ldcp->rx_dq_entries)
33658283286Sha137994 
33758283286Sha137994 #define	TRACE_RXHVQ_LENGTH(ldcp, head, tail)				\
33858283286Sha137994 	DTRACE_PROBE4(rxhvq__size,					\
33958283286Sha137994 	uint64_t, ldcp->id,						\
34058283286Sha137994 	uint64_t, head,							\
34158283286Sha137994 	uint64_t, tail,							\
34258283286Sha137994 	uint64_t, ldcp->rx_q_entries)
34358283286Sha137994 
34458283286Sha137994 /* A dtrace SDT probe to ease tracing of data queue copy operations */
34558283286Sha137994 #define	TRACE_RXDQ_COPY(ldcp, bytes)					\
34658283286Sha137994 	DTRACE_PROBE2(rxdq__copy, uint64_t, ldcp->id, uint64_t, bytes)	\
34758283286Sha137994 
34858283286Sha137994 /* The amount of contiguous space at the tail of the queue */
34958283286Sha137994 #define	Q_CONTIG_SPACE(head, tail, size)				\
35058283286Sha137994 	((head) <= (tail) ? ((size) - (tail)) :				\
35158283286Sha137994 	((head) - (tail) - LDC_PACKET_SIZE))
35258283286Sha137994 
3531ae08745Sheppo #define	ZERO_PKT(p)			\
3541ae08745Sheppo 	bzero((p), sizeof (ldc_msg_t));
3551ae08745Sheppo 
3561ae08745Sheppo #define	IDX2COOKIE(idx, pg_szc, pg_shift)				\
3571ae08745Sheppo 	(((pg_szc) << LDC_COOKIE_PGSZC_SHIFT) | ((idx) << (pg_shift)))
3581ae08745Sheppo 
3591ae08745Sheppo int
3601ae08745Sheppo _init(void)
3611ae08745Sheppo {
3621ae08745Sheppo 	int status;
363bbfa0259Sha137994 	extern void i_ldc_mem_set_hsvc_vers(uint64_t major, uint64_t minor);
3641ae08745Sheppo 
3651ae08745Sheppo 	status = hsvc_register(&ldc_hsvc, &ldc_sup_minor);
3661ae08745Sheppo 	if (status != 0) {
367d66f8315Sjb145095 		cmn_err(CE_NOTE, "!%s: cannot negotiate hypervisor LDC services"
3681ae08745Sheppo 		    " group: 0x%lx major: %ld minor: %ld errno: %d",
3691ae08745Sheppo 		    ldc_hsvc.hsvc_modname, ldc_hsvc.hsvc_group,
3701ae08745Sheppo 		    ldc_hsvc.hsvc_major, ldc_hsvc.hsvc_minor, status);
3711ae08745Sheppo 		return (-1);
3721ae08745Sheppo 	}
3731ae08745Sheppo 
374bbfa0259Sha137994 	/* Initialize shared memory HV API version checking */
375bbfa0259Sha137994 	i_ldc_mem_set_hsvc_vers(ldc_hsvc.hsvc_major, ldc_sup_minor);
376bbfa0259Sha137994 
3771ae08745Sheppo 	/* allocate soft state structure */
3781ae08745Sheppo 	ldcssp = kmem_zalloc(sizeof (ldc_soft_state_t), KM_SLEEP);
3791ae08745Sheppo 
3801ae08745Sheppo 	/* Link the module into the system */
3811ae08745Sheppo 	status = mod_install(&ml);
3821ae08745Sheppo 	if (status != 0) {
3831ae08745Sheppo 		kmem_free(ldcssp, sizeof (ldc_soft_state_t));
3841ae08745Sheppo 		return (status);
3851ae08745Sheppo 	}
3861ae08745Sheppo 
3871ae08745Sheppo 	/* Initialize the LDC state structure */
3881ae08745Sheppo 	mutex_init(&ldcssp->lock, NULL, MUTEX_DRIVER, NULL);
3891ae08745Sheppo 
3901ae08745Sheppo 	mutex_enter(&ldcssp->lock);
3911ae08745Sheppo 
3924bac2208Snarayan 	/* Create a cache for memory handles */
3934bac2208Snarayan 	ldcssp->memhdl_cache = kmem_cache_create("ldc_memhdl_cache",
3944bac2208Snarayan 	    sizeof (ldc_mhdl_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3954bac2208Snarayan 	if (ldcssp->memhdl_cache == NULL) {
3964bac2208Snarayan 		DWARN(DBG_ALL_LDCS, "_init: ldc_memhdl cache create failed\n");
3974bac2208Snarayan 		mutex_exit(&ldcssp->lock);
3984bac2208Snarayan 		return (-1);
3994bac2208Snarayan 	}
4004bac2208Snarayan 
4014bac2208Snarayan 	/* Create cache for memory segment structures */
4024bac2208Snarayan 	ldcssp->memseg_cache = kmem_cache_create("ldc_memseg_cache",
4034bac2208Snarayan 	    sizeof (ldc_memseg_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
4044bac2208Snarayan 	if (ldcssp->memseg_cache == NULL) {
4054bac2208Snarayan 		DWARN(DBG_ALL_LDCS, "_init: ldc_memseg cache create failed\n");
4064bac2208Snarayan 		mutex_exit(&ldcssp->lock);
4074bac2208Snarayan 		return (-1);
4084bac2208Snarayan 	}
4094bac2208Snarayan 
4104bac2208Snarayan 
4111ae08745Sheppo 	ldcssp->channel_count = 0;
4121ae08745Sheppo 	ldcssp->channels_open = 0;
4131ae08745Sheppo 	ldcssp->chan_list = NULL;
4141ae08745Sheppo 	ldcssp->dring_list = NULL;
4151ae08745Sheppo 
4161ae08745Sheppo 	mutex_exit(&ldcssp->lock);
4171ae08745Sheppo 
4181ae08745Sheppo 	return (0);
4191ae08745Sheppo }
4201ae08745Sheppo 
4211ae08745Sheppo int
4221ae08745Sheppo _info(struct modinfo *modinfop)
4231ae08745Sheppo {
4241ae08745Sheppo 	/* Report status of the dynamically loadable driver module */
4251ae08745Sheppo 	return (mod_info(&ml, modinfop));
4261ae08745Sheppo }
4271ae08745Sheppo 
4281ae08745Sheppo int
4291ae08745Sheppo _fini(void)
4301ae08745Sheppo {
4311ae08745Sheppo 	int 		rv, status;
43222f747efSnarayan 	ldc_chan_t 	*tmp_ldcp, *ldcp;
43322f747efSnarayan 	ldc_dring_t 	*tmp_dringp, *dringp;
4341ae08745Sheppo 	ldc_mem_info_t 	minfo;
4351ae08745Sheppo 
4361ae08745Sheppo 	/* Unlink the driver module from the system */
4371ae08745Sheppo 	status = mod_remove(&ml);
4381ae08745Sheppo 	if (status) {
4391ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "_fini: mod_remove failed\n");
4401ae08745Sheppo 		return (EIO);
4411ae08745Sheppo 	}
4421ae08745Sheppo 
4431ae08745Sheppo 	/* Free descriptor rings */
4441ae08745Sheppo 	dringp = ldcssp->dring_list;
4451ae08745Sheppo 	while (dringp != NULL) {
44622f747efSnarayan 		tmp_dringp = dringp->next;
4471ae08745Sheppo 
4481ae08745Sheppo 		rv = ldc_mem_dring_info((ldc_dring_handle_t)dringp, &minfo);
4491ae08745Sheppo 		if (rv == 0 && minfo.status != LDC_UNBOUND) {
4501ae08745Sheppo 			if (minfo.status == LDC_BOUND) {
4511ae08745Sheppo 				(void) ldc_mem_dring_unbind(
4521ae08745Sheppo 				    (ldc_dring_handle_t)dringp);
4531ae08745Sheppo 			}
4541ae08745Sheppo 			if (minfo.status == LDC_MAPPED) {
4551ae08745Sheppo 				(void) ldc_mem_dring_unmap(
4561ae08745Sheppo 				    (ldc_dring_handle_t)dringp);
4571ae08745Sheppo 			}
4581ae08745Sheppo 		}
4591ae08745Sheppo 
4601ae08745Sheppo 		(void) ldc_mem_dring_destroy((ldc_dring_handle_t)dringp);
46122f747efSnarayan 		dringp = tmp_dringp;
4621ae08745Sheppo 	}
4631ae08745Sheppo 	ldcssp->dring_list = NULL;
4641ae08745Sheppo 
46522f747efSnarayan 	/* close and finalize channels */
46622f747efSnarayan 	ldcp = ldcssp->chan_list;
46722f747efSnarayan 	while (ldcp != NULL) {
46822f747efSnarayan 		tmp_ldcp = ldcp->next;
46922f747efSnarayan 
47022f747efSnarayan 		(void) ldc_close((ldc_handle_t)ldcp);
47122f747efSnarayan 		(void) ldc_fini((ldc_handle_t)ldcp);
47222f747efSnarayan 
47322f747efSnarayan 		ldcp = tmp_ldcp;
47422f747efSnarayan 	}
47522f747efSnarayan 	ldcssp->chan_list = NULL;
47622f747efSnarayan 
4774bac2208Snarayan 	/* Destroy kmem caches */
4784bac2208Snarayan 	kmem_cache_destroy(ldcssp->memhdl_cache);
4794bac2208Snarayan 	kmem_cache_destroy(ldcssp->memseg_cache);
4804bac2208Snarayan 
4811ae08745Sheppo 	/*
4821ae08745Sheppo 	 * We have successfully "removed" the driver.
4831ae08745Sheppo 	 * Destroying soft states
4841ae08745Sheppo 	 */
4851ae08745Sheppo 	mutex_destroy(&ldcssp->lock);
4861ae08745Sheppo 	kmem_free(ldcssp, sizeof (ldc_soft_state_t));
4871ae08745Sheppo 
4881ae08745Sheppo 	(void) hsvc_unregister(&ldc_hsvc);
4891ae08745Sheppo 
4901ae08745Sheppo 	return (status);
4911ae08745Sheppo }
4921ae08745Sheppo 
4931ae08745Sheppo /* -------------------------------------------------------------------------- */
4941ae08745Sheppo 
4951ae08745Sheppo /*
496e1ebb9ecSlm66018  * LDC Link Layer Internal Functions
4971ae08745Sheppo  */
4981ae08745Sheppo 
4991ae08745Sheppo /*
5001ae08745Sheppo  * Translate HV Errors to sun4v error codes
5011ae08745Sheppo  */
50220ae46ebSha137994 int
5031ae08745Sheppo i_ldc_h2v_error(int h_error)
5041ae08745Sheppo {
5051ae08745Sheppo 	switch (h_error) {
5061ae08745Sheppo 
5071ae08745Sheppo 	case	H_EOK:
5081ae08745Sheppo 		return (0);
5091ae08745Sheppo 
5101ae08745Sheppo 	case	H_ENORADDR:
5111ae08745Sheppo 		return (EFAULT);
5121ae08745Sheppo 
5131ae08745Sheppo 	case	H_EBADPGSZ:
5141ae08745Sheppo 	case	H_EINVAL:
5151ae08745Sheppo 		return (EINVAL);
5161ae08745Sheppo 
5171ae08745Sheppo 	case	H_EWOULDBLOCK:
5181ae08745Sheppo 		return (EWOULDBLOCK);
5191ae08745Sheppo 
5201ae08745Sheppo 	case	H_ENOACCESS:
5211ae08745Sheppo 	case	H_ENOMAP:
5221ae08745Sheppo 		return (EACCES);
5231ae08745Sheppo 
5241ae08745Sheppo 	case	H_EIO:
5251ae08745Sheppo 	case	H_ECPUERROR:
5261ae08745Sheppo 		return (EIO);
5271ae08745Sheppo 
5281ae08745Sheppo 	case	H_ENOTSUPPORTED:
5291ae08745Sheppo 		return (ENOTSUP);
5301ae08745Sheppo 
5311ae08745Sheppo 	case 	H_ETOOMANY:
5321ae08745Sheppo 		return (ENOSPC);
5331ae08745Sheppo 
5341ae08745Sheppo 	case	H_ECHANNEL:
5351ae08745Sheppo 		return (ECHRNG);
5361ae08745Sheppo 	default:
5371ae08745Sheppo 		break;
5381ae08745Sheppo 	}
5391ae08745Sheppo 
5401ae08745Sheppo 	return (EIO);
5411ae08745Sheppo }
5421ae08745Sheppo 
5431ae08745Sheppo /*
5441ae08745Sheppo  * Reconfigure the transmit queue
5451ae08745Sheppo  */
5461ae08745Sheppo static int
5471ae08745Sheppo i_ldc_txq_reconf(ldc_chan_t *ldcp)
5481ae08745Sheppo {
5491ae08745Sheppo 	int rv;
5501ae08745Sheppo 
5511ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
552d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
553d10e4ef2Snarayan 
5541ae08745Sheppo 	rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries);
5551ae08745Sheppo 	if (rv) {
5561ae08745Sheppo 		cmn_err(CE_WARN,
5573af08d82Slm66018 		    "i_ldc_txq_reconf: (0x%lx) cannot set qconf", ldcp->id);
5581ae08745Sheppo 		return (EIO);
5591ae08745Sheppo 	}
5601ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id, &(ldcp->tx_head),
5611ae08745Sheppo 	    &(ldcp->tx_tail), &(ldcp->link_state));
5621ae08745Sheppo 	if (rv) {
5631ae08745Sheppo 		cmn_err(CE_WARN,
5643af08d82Slm66018 		    "i_ldc_txq_reconf: (0x%lx) cannot get qptrs", ldcp->id);
5651ae08745Sheppo 		return (EIO);
5661ae08745Sheppo 	}
5673af08d82Slm66018 	D1(ldcp->id, "i_ldc_txq_reconf: (0x%llx) h=0x%llx,t=0x%llx,"
5681ae08745Sheppo 	    "s=0x%llx\n", ldcp->id, ldcp->tx_head, ldcp->tx_tail,
5691ae08745Sheppo 	    ldcp->link_state);
5701ae08745Sheppo 
5711ae08745Sheppo 	return (0);
5721ae08745Sheppo }
5731ae08745Sheppo 
5741ae08745Sheppo /*
5751ae08745Sheppo  * Reconfigure the receive queue
5761ae08745Sheppo  */
5771ae08745Sheppo static int
5783af08d82Slm66018 i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset)
5791ae08745Sheppo {
5801ae08745Sheppo 	int rv;
5811ae08745Sheppo 	uint64_t rx_head, rx_tail;
5821ae08745Sheppo 
5831ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
5841ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
5851ae08745Sheppo 	    &(ldcp->link_state));
5861ae08745Sheppo 	if (rv) {
5871ae08745Sheppo 		cmn_err(CE_WARN,
5883af08d82Slm66018 		    "i_ldc_rxq_reconf: (0x%lx) cannot get state",
5891ae08745Sheppo 		    ldcp->id);
5901ae08745Sheppo 		return (EIO);
5911ae08745Sheppo 	}
5921ae08745Sheppo 
5933af08d82Slm66018 	if (force_reset || (ldcp->tstate & ~TS_IN_RESET) == TS_UP) {
5941ae08745Sheppo 		rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra,
5951ae08745Sheppo 		    ldcp->rx_q_entries);
5961ae08745Sheppo 		if (rv) {
5971ae08745Sheppo 			cmn_err(CE_WARN,
5983af08d82Slm66018 			    "i_ldc_rxq_reconf: (0x%lx) cannot set qconf",
5991ae08745Sheppo 			    ldcp->id);
6001ae08745Sheppo 			return (EIO);
6011ae08745Sheppo 		}
6023af08d82Slm66018 		D1(ldcp->id, "i_ldc_rxq_reconf: (0x%llx) completed q reconf",
6031ae08745Sheppo 		    ldcp->id);
6041ae08745Sheppo 	}
6051ae08745Sheppo 
6061ae08745Sheppo 	return (0);
6071ae08745Sheppo }
6081ae08745Sheppo 
609a8ea4edeSnarayan 
610a8ea4edeSnarayan /*
611a8ea4edeSnarayan  * Drain the contents of the receive queue
612a8ea4edeSnarayan  */
613a8ea4edeSnarayan static int
614a8ea4edeSnarayan i_ldc_rxq_drain(ldc_chan_t *ldcp)
615a8ea4edeSnarayan {
616a8ea4edeSnarayan 	int rv;
617a8ea4edeSnarayan 	uint64_t rx_head, rx_tail;
618a8ea4edeSnarayan 
619a8ea4edeSnarayan 	ASSERT(MUTEX_HELD(&ldcp->lock));
620a8ea4edeSnarayan 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
621a8ea4edeSnarayan 	    &(ldcp->link_state));
622a8ea4edeSnarayan 	if (rv) {
623a8ea4edeSnarayan 		cmn_err(CE_WARN, "i_ldc_rxq_drain: (0x%lx) cannot get state",
624a8ea4edeSnarayan 		    ldcp->id);
625a8ea4edeSnarayan 		return (EIO);
626a8ea4edeSnarayan 	}
627a8ea4edeSnarayan 
628a8ea4edeSnarayan 	/* flush contents by setting the head = tail */
629a8ea4edeSnarayan 	return (i_ldc_set_rx_head(ldcp, rx_tail));
630a8ea4edeSnarayan }
631a8ea4edeSnarayan 
632a8ea4edeSnarayan 
6331ae08745Sheppo /*
6341ae08745Sheppo  * Reset LDC state structure and its contents
6351ae08745Sheppo  */
6361ae08745Sheppo static void
6371ae08745Sheppo i_ldc_reset_state(ldc_chan_t *ldcp)
6381ae08745Sheppo {
6391ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
6401ae08745Sheppo 	ldcp->last_msg_snt = LDC_INIT_SEQID;
6411ae08745Sheppo 	ldcp->last_ack_rcd = 0;
6421ae08745Sheppo 	ldcp->last_msg_rcd = 0;
6431ae08745Sheppo 	ldcp->tx_ackd_head = ldcp->tx_head;
64458283286Sha137994 	ldcp->stream_remains = 0;
6451ae08745Sheppo 	ldcp->next_vidx = 0;
6461ae08745Sheppo 	ldcp->hstate = 0;
6471ae08745Sheppo 	ldcp->tstate = TS_OPEN;
6481ae08745Sheppo 	ldcp->status = LDC_OPEN;
64958283286Sha137994 	ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID;
65058283286Sha137994 	ldcp->rx_dq_head = 0;
65158283286Sha137994 	ldcp->rx_dq_tail = 0;
6521ae08745Sheppo 
6531ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_UP ||
6541ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
6551ae08745Sheppo 
6561ae08745Sheppo 		if (ldcp->mode == LDC_MODE_RAW) {
6571ae08745Sheppo 			ldcp->status = LDC_UP;
6581ae08745Sheppo 			ldcp->tstate = TS_UP;
6591ae08745Sheppo 		} else {
6601ae08745Sheppo 			ldcp->status = LDC_READY;
6611ae08745Sheppo 			ldcp->tstate |= TS_LINK_READY;
6621ae08745Sheppo 		}
6631ae08745Sheppo 	}
6641ae08745Sheppo }
6651ae08745Sheppo 
6661ae08745Sheppo /*
6671ae08745Sheppo  * Reset a LDC channel
6681ae08745Sheppo  */
66920ae46ebSha137994 void
6703af08d82Slm66018 i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset)
6711ae08745Sheppo {
67283d3bc6fSnarayan 	DWARN(ldcp->id, "i_ldc_reset: (0x%llx) channel reset\n", ldcp->id);
6731ae08745Sheppo 
674d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->lock));
675d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
676d10e4ef2Snarayan 
6773af08d82Slm66018 	/* reconfig Tx and Rx queues */
6781ae08745Sheppo 	(void) i_ldc_txq_reconf(ldcp);
6793af08d82Slm66018 	(void) i_ldc_rxq_reconf(ldcp, force_reset);
6803af08d82Slm66018 
6813af08d82Slm66018 	/* Clear Tx and Rx interrupts */
6823af08d82Slm66018 	(void) i_ldc_clear_intr(ldcp, CNEX_TX_INTR);
6833af08d82Slm66018 	(void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
6843af08d82Slm66018 
6853af08d82Slm66018 	/* Reset channel state */
6861ae08745Sheppo 	i_ldc_reset_state(ldcp);
6873af08d82Slm66018 
6883af08d82Slm66018 	/* Mark channel in reset */
6893af08d82Slm66018 	ldcp->tstate |= TS_IN_RESET;
6901ae08745Sheppo }
6911ae08745Sheppo 
6924bac2208Snarayan 
6931ae08745Sheppo /*
6941ae08745Sheppo  * Clear pending interrupts
6951ae08745Sheppo  */
6961ae08745Sheppo static void
6971ae08745Sheppo i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype)
6981ae08745Sheppo {
6991ae08745Sheppo 	ldc_cnex_t *cinfo = &ldcssp->cinfo;
7001ae08745Sheppo 
7011ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
7023af08d82Slm66018 	ASSERT(cinfo->dip != NULL);
7034bac2208Snarayan 
7043af08d82Slm66018 	switch (itype) {
7053af08d82Slm66018 	case CNEX_TX_INTR:
7064bac2208Snarayan 		/* check Tx interrupt */
7073af08d82Slm66018 		if (ldcp->tx_intr_state)
7083af08d82Slm66018 			ldcp->tx_intr_state = LDC_INTR_NONE;
7094bac2208Snarayan 		else
7104bac2208Snarayan 			return;
7113af08d82Slm66018 		break;
7123af08d82Slm66018 
7133af08d82Slm66018 	case CNEX_RX_INTR:
7144bac2208Snarayan 		/* check Rx interrupt */
7153af08d82Slm66018 		if (ldcp->rx_intr_state)
7163af08d82Slm66018 			ldcp->rx_intr_state = LDC_INTR_NONE;
7174bac2208Snarayan 		else
7184bac2208Snarayan 			return;
7193af08d82Slm66018 		break;
7204bac2208Snarayan 	}
7214bac2208Snarayan 
7221ae08745Sheppo 	(void) cinfo->clr_intr(cinfo->dip, ldcp->id, itype);
7234bac2208Snarayan 	D2(ldcp->id,
7244bac2208Snarayan 	    "i_ldc_clear_intr: (0x%llx) cleared 0x%x intr\n",
7254bac2208Snarayan 	    ldcp->id, itype);
7261ae08745Sheppo }
7271ae08745Sheppo 
7281ae08745Sheppo /*
7291ae08745Sheppo  * Set the receive queue head
7300a55fbb7Slm66018  * Resets connection and returns an error if it fails.
7311ae08745Sheppo  */
7321ae08745Sheppo static int
7331ae08745Sheppo i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head)
7341ae08745Sheppo {
7351ae08745Sheppo 	int 	rv;
7360a55fbb7Slm66018 	int 	retries;
7371ae08745Sheppo 
7381ae08745Sheppo 	ASSERT(MUTEX_HELD(&ldcp->lock));
7390a55fbb7Slm66018 	for (retries = 0; retries < ldc_max_retries; retries++) {
7400a55fbb7Slm66018 
7410a55fbb7Slm66018 		if ((rv = hv_ldc_rx_set_qhead(ldcp->id, head)) == 0)
7420a55fbb7Slm66018 			return (0);
7430a55fbb7Slm66018 
7440a55fbb7Slm66018 		if (rv != H_EWOULDBLOCK)
7450a55fbb7Slm66018 			break;
7460a55fbb7Slm66018 
7470a55fbb7Slm66018 		/* wait for ldc_delay usecs */
7480a55fbb7Slm66018 		drv_usecwait(ldc_delay);
7491ae08745Sheppo 	}
7501ae08745Sheppo 
7510a55fbb7Slm66018 	cmn_err(CE_WARN, "ldc_rx_set_qhead: (0x%lx) cannot set qhead 0x%lx",
7520a55fbb7Slm66018 	    ldcp->id, head);
753d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
7543af08d82Slm66018 	i_ldc_reset(ldcp, B_TRUE);
755d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
7560a55fbb7Slm66018 
7570a55fbb7Slm66018 	return (ECONNRESET);
7581ae08745Sheppo }
7591ae08745Sheppo 
76022f747efSnarayan /*
76122f747efSnarayan  * Returns the tx_head to be used for transfer
76222f747efSnarayan  */
76322f747efSnarayan static void
76422f747efSnarayan i_ldc_get_tx_head(ldc_chan_t *ldcp, uint64_t *head)
76522f747efSnarayan {
76622f747efSnarayan 	ldc_msg_t 	*pkt;
76722f747efSnarayan 
76822f747efSnarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
76922f747efSnarayan 
77022f747efSnarayan 	/* get current Tx head */
77122f747efSnarayan 	*head = ldcp->tx_head;
77222f747efSnarayan 
77322f747efSnarayan 	/*
77422f747efSnarayan 	 * Reliable mode will use the ACKd head instead of the regular tx_head.
77522f747efSnarayan 	 * Also in Reliable mode, advance ackd_head for all non DATA/INFO pkts,
77622f747efSnarayan 	 * up to the current location of tx_head. This needs to be done
77722f747efSnarayan 	 * as the peer will only ACK DATA/INFO pkts.
77822f747efSnarayan 	 */
77920ae46ebSha137994 	if (ldcp->mode == LDC_MODE_RELIABLE) {
78022f747efSnarayan 		while (ldcp->tx_ackd_head != ldcp->tx_head) {
78122f747efSnarayan 			pkt = (ldc_msg_t *)(ldcp->tx_q_va + ldcp->tx_ackd_head);
78222f747efSnarayan 			if ((pkt->type & LDC_DATA) && (pkt->stype & LDC_INFO)) {
78322f747efSnarayan 				break;
78422f747efSnarayan 			}
78522f747efSnarayan 			/* advance ACKd head */
78622f747efSnarayan 			ldcp->tx_ackd_head =
78722f747efSnarayan 			    (ldcp->tx_ackd_head + LDC_PACKET_SIZE) %
78822f747efSnarayan 			    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
78922f747efSnarayan 		}
79022f747efSnarayan 		*head = ldcp->tx_ackd_head;
79122f747efSnarayan 	}
79222f747efSnarayan }
7931ae08745Sheppo 
7941ae08745Sheppo /*
7951ae08745Sheppo  * Returns the tx_tail to be used for transfer
7961ae08745Sheppo  * Re-reads the TX queue ptrs if and only if the
7971ae08745Sheppo  * the cached head and tail are equal (queue is full)
7981ae08745Sheppo  */
7991ae08745Sheppo static int
8001ae08745Sheppo i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail)
8011ae08745Sheppo {
8021ae08745Sheppo 	int 		rv;
8031ae08745Sheppo 	uint64_t 	current_head, new_tail;
8041ae08745Sheppo 
805d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
8061ae08745Sheppo 	/* Read the head and tail ptrs from HV */
8071ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
8081ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
8091ae08745Sheppo 	if (rv) {
8101ae08745Sheppo 		cmn_err(CE_WARN,
8111ae08745Sheppo 		    "i_ldc_get_tx_tail: (0x%lx) cannot read qptrs\n",
8121ae08745Sheppo 		    ldcp->id);
8131ae08745Sheppo 		return (EIO);
8141ae08745Sheppo 	}
8151ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_DOWN) {
816cb112a14Slm66018 		D1(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) channel not ready\n",
8171ae08745Sheppo 		    ldcp->id);
8181ae08745Sheppo 		return (ECONNRESET);
8191ae08745Sheppo 	}
8201ae08745Sheppo 
82122f747efSnarayan 	i_ldc_get_tx_head(ldcp, &current_head);
8221ae08745Sheppo 
8231ae08745Sheppo 	/* increment the tail */
8241ae08745Sheppo 	new_tail = (ldcp->tx_tail + LDC_PACKET_SIZE) %
8251ae08745Sheppo 	    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
8261ae08745Sheppo 
8271ae08745Sheppo 	if (new_tail == current_head) {
8281ae08745Sheppo 		DWARN(ldcp->id,
8291ae08745Sheppo 		    "i_ldc_get_tx_tail: (0x%llx) TX queue is full\n",
8301ae08745Sheppo 		    ldcp->id);
8311ae08745Sheppo 		return (EWOULDBLOCK);
8321ae08745Sheppo 	}
8331ae08745Sheppo 
8341ae08745Sheppo 	D2(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) head=0x%llx, tail=0x%llx\n",
8351ae08745Sheppo 	    ldcp->id, ldcp->tx_head, ldcp->tx_tail);
8361ae08745Sheppo 
8371ae08745Sheppo 	*tail = ldcp->tx_tail;
8381ae08745Sheppo 	return (0);
8391ae08745Sheppo }
8401ae08745Sheppo 
8411ae08745Sheppo /*
8421ae08745Sheppo  * Set the tail pointer. If HV returns EWOULDBLOCK, it will back off
8430a55fbb7Slm66018  * and retry ldc_max_retries times before returning an error.
8441ae08745Sheppo  * Returns 0, EWOULDBLOCK or EIO
8451ae08745Sheppo  */
8461ae08745Sheppo static int
8471ae08745Sheppo i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail)
8481ae08745Sheppo {
8491ae08745Sheppo 	int		rv, retval = EWOULDBLOCK;
8500a55fbb7Slm66018 	int 		retries;
8511ae08745Sheppo 
852d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
8530a55fbb7Slm66018 	for (retries = 0; retries < ldc_max_retries; retries++) {
8541ae08745Sheppo 
8551ae08745Sheppo 		if ((rv = hv_ldc_tx_set_qtail(ldcp->id, tail)) == 0) {
8561ae08745Sheppo 			retval = 0;
8571ae08745Sheppo 			break;
8581ae08745Sheppo 		}
8591ae08745Sheppo 		if (rv != H_EWOULDBLOCK) {
8601ae08745Sheppo 			DWARN(ldcp->id, "i_ldc_set_tx_tail: (0x%llx) set "
8611ae08745Sheppo 			    "qtail=0x%llx failed, rv=%d\n", ldcp->id, tail, rv);
8621ae08745Sheppo 			retval = EIO;
8631ae08745Sheppo 			break;
8641ae08745Sheppo 		}
8651ae08745Sheppo 
8660a55fbb7Slm66018 		/* wait for ldc_delay usecs */
8670a55fbb7Slm66018 		drv_usecwait(ldc_delay);
8681ae08745Sheppo 	}
8691ae08745Sheppo 	return (retval);
8701ae08745Sheppo }
8711ae08745Sheppo 
8721ae08745Sheppo /*
87358283286Sha137994  * Copy a data packet from the HV receive queue to the data queue.
87458283286Sha137994  * Caller must ensure that the data queue is not already full.
87558283286Sha137994  *
87658283286Sha137994  * The *head argument represents the current head pointer for the HV
87758283286Sha137994  * receive queue. After copying a packet from the HV receive queue,
87858283286Sha137994  * the *head pointer will be updated. This allows the caller to update
87958283286Sha137994  * the head pointer in HV using the returned *head value.
88058283286Sha137994  */
88158283286Sha137994 void
88258283286Sha137994 i_ldc_rxdq_copy(ldc_chan_t *ldcp, uint64_t *head)
88358283286Sha137994 {
88458283286Sha137994 	uint64_t	q_size, dq_size;
88558283286Sha137994 
88658283286Sha137994 	ASSERT(MUTEX_HELD(&ldcp->lock));
88758283286Sha137994 
88858283286Sha137994 	q_size  = ldcp->rx_q_entries << LDC_PACKET_SHIFT;
88958283286Sha137994 	dq_size = ldcp->rx_dq_entries << LDC_PACKET_SHIFT;
89058283286Sha137994 
89158283286Sha137994 	ASSERT(Q_CONTIG_SPACE(ldcp->rx_dq_head, ldcp->rx_dq_tail,
89258283286Sha137994 	    dq_size) >= LDC_PACKET_SIZE);
89358283286Sha137994 
89458283286Sha137994 	bcopy((void *)(ldcp->rx_q_va + *head),
89558283286Sha137994 	    (void *)(ldcp->rx_dq_va + ldcp->rx_dq_tail), LDC_PACKET_SIZE);
89658283286Sha137994 	TRACE_RXDQ_COPY(ldcp, LDC_PACKET_SIZE);
89758283286Sha137994 
89858283286Sha137994 	/* Update rx head */
89958283286Sha137994 	*head = (*head + LDC_PACKET_SIZE) % q_size;
90058283286Sha137994 
90158283286Sha137994 	/* Update dq tail */
90258283286Sha137994 	ldcp->rx_dq_tail = (ldcp->rx_dq_tail + LDC_PACKET_SIZE) % dq_size;
90358283286Sha137994 }
90458283286Sha137994 
90558283286Sha137994 /*
90658283286Sha137994  * Update the Rx data queue head pointer
90758283286Sha137994  */
90858283286Sha137994 static int
90958283286Sha137994 i_ldc_set_rxdq_head(ldc_chan_t *ldcp, uint64_t head)
91058283286Sha137994 {
91158283286Sha137994 	ldcp->rx_dq_head = head;
91258283286Sha137994 	return (0);
91358283286Sha137994 }
91458283286Sha137994 
91558283286Sha137994 /*
91658283286Sha137994  * Get the Rx data queue head and tail pointers
91758283286Sha137994  */
91858283286Sha137994 static uint64_t
91958283286Sha137994 i_ldc_dq_rx_get_state(ldc_chan_t *ldcp, uint64_t *head, uint64_t *tail,
92058283286Sha137994     uint64_t *link_state)
92158283286Sha137994 {
92258283286Sha137994 	_NOTE(ARGUNUSED(link_state))
92358283286Sha137994 	*head = ldcp->rx_dq_head;
92458283286Sha137994 	*tail = ldcp->rx_dq_tail;
92558283286Sha137994 	return (0);
92658283286Sha137994 }
92758283286Sha137994 
92858283286Sha137994 /*
92958283286Sha137994  * Wrapper for the Rx HV queue set head function. Giving the
93058283286Sha137994  * data queue and HV queue set head functions the same type.
93158283286Sha137994  */
93258283286Sha137994 static uint64_t
93358283286Sha137994 i_ldc_hvq_rx_get_state(ldc_chan_t *ldcp, uint64_t *head, uint64_t *tail,
93458283286Sha137994     uint64_t *link_state)
93558283286Sha137994 {
93658283286Sha137994 	return (i_ldc_h2v_error(hv_ldc_rx_get_state(ldcp->id, head, tail,
93758283286Sha137994 	    link_state)));
93858283286Sha137994 }
93958283286Sha137994 
94058283286Sha137994 /*
94158283286Sha137994  * LDC receive interrupt handler
94258283286Sha137994  *    triggered for channel with data pending to read
94358283286Sha137994  *    i.e. Rx queue content changes
94458283286Sha137994  */
94558283286Sha137994 static uint_t
94658283286Sha137994 i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2)
94758283286Sha137994 {
94858283286Sha137994 	_NOTE(ARGUNUSED(arg2))
94958283286Sha137994 
95058283286Sha137994 	ldc_chan_t	*ldcp;
95158283286Sha137994 	boolean_t	notify;
95258283286Sha137994 	uint64_t	event;
953*12f80fa6Sha137994 	int		rv, status;
95458283286Sha137994 
95558283286Sha137994 	/* Get the channel for which interrupt was received */
95658283286Sha137994 	if (arg1 == NULL) {
95758283286Sha137994 		cmn_err(CE_WARN, "i_ldc_rx_hdlr: invalid arg\n");
95858283286Sha137994 		return (DDI_INTR_UNCLAIMED);
95958283286Sha137994 	}
96058283286Sha137994 
96158283286Sha137994 	ldcp = (ldc_chan_t *)arg1;
96258283286Sha137994 
96358283286Sha137994 	D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n",
96458283286Sha137994 	    ldcp->id, ldcp);
96558283286Sha137994 	D1(ldcp->id, "i_ldc_rx_hdlr: (%llx) USR%lx/TS%lx/HS%lx, LSTATE=%lx\n",
96658283286Sha137994 	    ldcp->id, ldcp->status, ldcp->tstate, ldcp->hstate,
96758283286Sha137994 	    ldcp->link_state);
96858283286Sha137994 
96958283286Sha137994 	/* Lock channel */
97058283286Sha137994 	mutex_enter(&ldcp->lock);
97158283286Sha137994 
97258283286Sha137994 	/* Mark the interrupt as being actively handled */
97358283286Sha137994 	ldcp->rx_intr_state = LDC_INTR_ACTIVE;
97458283286Sha137994 
975*12f80fa6Sha137994 	status = i_ldc_rx_process_hvq(ldcp, &notify, &event);
97658283286Sha137994 
97720ae46ebSha137994 	if (ldcp->mode != LDC_MODE_RELIABLE) {
97858283286Sha137994 		/*
97958283286Sha137994 		 * If there are no data packets on the queue, clear
98058283286Sha137994 		 * the interrupt. Otherwise, the ldc_read will clear
98158283286Sha137994 		 * interrupts after draining the queue. To indicate the
98258283286Sha137994 		 * interrupt has not yet been cleared, it is marked
98358283286Sha137994 		 * as pending.
98458283286Sha137994 		 */
98558283286Sha137994 		if ((event & LDC_EVT_READ) == 0) {
98658283286Sha137994 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
98758283286Sha137994 		} else {
98858283286Sha137994 			ldcp->rx_intr_state = LDC_INTR_PEND;
98958283286Sha137994 		}
99058283286Sha137994 	}
99158283286Sha137994 
99258283286Sha137994 	/* if callbacks are disabled, do not notify */
99358283286Sha137994 	if (notify && ldcp->cb_enabled) {
99458283286Sha137994 		ldcp->cb_inprogress = B_TRUE;
99558283286Sha137994 		mutex_exit(&ldcp->lock);
99658283286Sha137994 		rv = ldcp->cb(event, ldcp->cb_arg);
99758283286Sha137994 		if (rv) {
99858283286Sha137994 			DWARN(ldcp->id,
99958283286Sha137994 			    "i_ldc_rx_hdlr: (0x%llx) callback failure",
100058283286Sha137994 			    ldcp->id);
100158283286Sha137994 		}
100258283286Sha137994 		mutex_enter(&ldcp->lock);
100358283286Sha137994 		ldcp->cb_inprogress = B_FALSE;
100458283286Sha137994 	}
100558283286Sha137994 
100620ae46ebSha137994 	if (ldcp->mode == LDC_MODE_RELIABLE) {
1007*12f80fa6Sha137994 		if (status == ENOSPC) {
100858283286Sha137994 			/*
1009*12f80fa6Sha137994 			 * Here, ENOSPC indicates the secondary data
1010*12f80fa6Sha137994 			 * queue is full and the Rx queue is non-empty.
1011*12f80fa6Sha137994 			 * Much like how reliable and raw modes are
1012*12f80fa6Sha137994 			 * handled above, since the Rx queue is non-
1013*12f80fa6Sha137994 			 * empty, we mark the interrupt as pending to
1014*12f80fa6Sha137994 			 * indicate it has not yet been cleared.
1015*12f80fa6Sha137994 			 */
1016*12f80fa6Sha137994 			ldcp->rx_intr_state = LDC_INTR_PEND;
1017*12f80fa6Sha137994 		} else {
1018*12f80fa6Sha137994 			/*
1019*12f80fa6Sha137994 			 * We have processed all CTRL packets and
1020*12f80fa6Sha137994 			 * copied all DATA packets to the secondary
1021*12f80fa6Sha137994 			 * queue. Clear the interrupt.
102258283286Sha137994 			 */
102358283286Sha137994 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
102458283286Sha137994 		}
1025*12f80fa6Sha137994 	}
102658283286Sha137994 
102758283286Sha137994 	mutex_exit(&ldcp->lock);
102858283286Sha137994 
102958283286Sha137994 	D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) exiting handler", ldcp->id);
103058283286Sha137994 
103158283286Sha137994 	return (DDI_INTR_CLAIMED);
103258283286Sha137994 }
103358283286Sha137994 
103458283286Sha137994 /*
103558283286Sha137994  * Wrapper for the Rx HV queue processing function to be used when
103658283286Sha137994  * checking the Rx HV queue for data packets. Unlike the interrupt
103758283286Sha137994  * handler code flow, the Rx interrupt is not cleared here and
103858283286Sha137994  * callbacks are not made.
103958283286Sha137994  */
104058283286Sha137994 static uint_t
104158283286Sha137994 i_ldc_chkq(ldc_chan_t *ldcp)
104258283286Sha137994 {
104358283286Sha137994 	boolean_t	notify;
104458283286Sha137994 	uint64_t	event;
104558283286Sha137994 
104658283286Sha137994 	return (i_ldc_rx_process_hvq(ldcp, &notify, &event));
104758283286Sha137994 }
104858283286Sha137994 
104958283286Sha137994 /*
10501ae08745Sheppo  * Send a LDC message
10511ae08745Sheppo  */
10521ae08745Sheppo static int
10531ae08745Sheppo i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype,
10541ae08745Sheppo     uint8_t ctrlmsg)
10551ae08745Sheppo {
10561ae08745Sheppo 	int		rv;
10571ae08745Sheppo 	ldc_msg_t 	*pkt;
10581ae08745Sheppo 	uint64_t	tx_tail;
105922f747efSnarayan 	uint32_t	curr_seqid;
10601ae08745Sheppo 
1061d10e4ef2Snarayan 	/* Obtain Tx lock */
1062d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
1063d10e4ef2Snarayan 
106422f747efSnarayan 	curr_seqid = ldcp->last_msg_snt;
106522f747efSnarayan 
10661ae08745Sheppo 	/* get the current tail for the message */
10671ae08745Sheppo 	rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
10681ae08745Sheppo 	if (rv) {
10691ae08745Sheppo 		DWARN(ldcp->id,
10701ae08745Sheppo 		    "i_ldc_send_pkt: (0x%llx) error sending pkt, "
10711ae08745Sheppo 		    "type=0x%x,subtype=0x%x,ctrl=0x%x\n",
10721ae08745Sheppo 		    ldcp->id, pkttype, subtype, ctrlmsg);
1073d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
10741ae08745Sheppo 		return (rv);
10751ae08745Sheppo 	}
10761ae08745Sheppo 
10771ae08745Sheppo 	pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
10781ae08745Sheppo 	ZERO_PKT(pkt);
10791ae08745Sheppo 
10801ae08745Sheppo 	/* Initialize the packet */
10811ae08745Sheppo 	pkt->type = pkttype;
10821ae08745Sheppo 	pkt->stype = subtype;
10831ae08745Sheppo 	pkt->ctrl = ctrlmsg;
10841ae08745Sheppo 
10851ae08745Sheppo 	/* Store ackid/seqid iff it is RELIABLE mode & not a RTS/RTR message */
10861ae08745Sheppo 	if (((ctrlmsg & LDC_CTRL_MASK) != LDC_RTS) &&
10871ae08745Sheppo 	    ((ctrlmsg & LDC_CTRL_MASK) != LDC_RTR)) {
10881ae08745Sheppo 		curr_seqid++;
10891ae08745Sheppo 		if (ldcp->mode != LDC_MODE_RAW) {
10901ae08745Sheppo 			pkt->seqid = curr_seqid;
10911ae08745Sheppo 			pkt->ackid = ldcp->last_msg_rcd;
10921ae08745Sheppo 		}
10931ae08745Sheppo 	}
10941ae08745Sheppo 	DUMP_LDC_PKT(ldcp, "i_ldc_send_pkt", (uint64_t)pkt);
10951ae08745Sheppo 
10961ae08745Sheppo 	/* initiate the send by calling into HV and set the new tail */
10971ae08745Sheppo 	tx_tail = (tx_tail + LDC_PACKET_SIZE) %
10981ae08745Sheppo 	    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
10991ae08745Sheppo 
11001ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
11011ae08745Sheppo 	if (rv) {
11021ae08745Sheppo 		DWARN(ldcp->id,
11031ae08745Sheppo 		    "i_ldc_send_pkt:(0x%llx) error sending pkt, "
11041ae08745Sheppo 		    "type=0x%x,stype=0x%x,ctrl=0x%x\n",
11051ae08745Sheppo 		    ldcp->id, pkttype, subtype, ctrlmsg);
1106d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
11071ae08745Sheppo 		return (EIO);
11081ae08745Sheppo 	}
11091ae08745Sheppo 
11101ae08745Sheppo 	ldcp->last_msg_snt = curr_seqid;
11111ae08745Sheppo 	ldcp->tx_tail = tx_tail;
11121ae08745Sheppo 
1113d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
11141ae08745Sheppo 	return (0);
11151ae08745Sheppo }
11161ae08745Sheppo 
11171ae08745Sheppo /*
11181ae08745Sheppo  * Checks if packet was received in right order
1119e1ebb9ecSlm66018  * in the case of a reliable link.
11201ae08745Sheppo  * Returns 0 if in order, else EIO
11211ae08745Sheppo  */
11221ae08745Sheppo static int
11231ae08745Sheppo i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *msg)
11241ae08745Sheppo {
11251ae08745Sheppo 	/* No seqid checking for RAW mode */
11261ae08745Sheppo 	if (ldcp->mode == LDC_MODE_RAW)
11271ae08745Sheppo 		return (0);
11281ae08745Sheppo 
11291ae08745Sheppo 	/* No seqid checking for version, RTS, RTR message */
11301ae08745Sheppo 	if (msg->ctrl == LDC_VER ||
11311ae08745Sheppo 	    msg->ctrl == LDC_RTS ||
11321ae08745Sheppo 	    msg->ctrl == LDC_RTR)
11331ae08745Sheppo 		return (0);
11341ae08745Sheppo 
11351ae08745Sheppo 	/* Initial seqid to use is sent in RTS/RTR and saved in last_msg_rcd */
11361ae08745Sheppo 	if (msg->seqid != (ldcp->last_msg_rcd + 1)) {
11371ae08745Sheppo 		DWARN(ldcp->id,
11381ae08745Sheppo 		    "i_ldc_check_seqid: (0x%llx) out-of-order pkt, got 0x%x, "
11391ae08745Sheppo 		    "expecting 0x%x\n", ldcp->id, msg->seqid,
11401ae08745Sheppo 		    (ldcp->last_msg_rcd + 1));
11411ae08745Sheppo 		return (EIO);
11421ae08745Sheppo 	}
11431ae08745Sheppo 
114483d3bc6fSnarayan #ifdef DEBUG
114583d3bc6fSnarayan 	if (LDC_INJECT_PKTLOSS(ldcp)) {
114683d3bc6fSnarayan 		DWARN(ldcp->id,
114783d3bc6fSnarayan 		    "i_ldc_check_seqid: (0x%llx) inject pkt loss\n", ldcp->id);
114883d3bc6fSnarayan 		return (EIO);
114983d3bc6fSnarayan 	}
115083d3bc6fSnarayan #endif
115183d3bc6fSnarayan 
11521ae08745Sheppo 	return (0);
11531ae08745Sheppo }
11541ae08745Sheppo 
11551ae08745Sheppo 
11561ae08745Sheppo /*
11571ae08745Sheppo  * Process an incoming version ctrl message
11581ae08745Sheppo  */
11591ae08745Sheppo static int
11601ae08745Sheppo i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg)
11611ae08745Sheppo {
11621ae08745Sheppo 	int 		rv = 0, idx = ldcp->next_vidx;
11631ae08745Sheppo 	ldc_msg_t 	*pkt;
11641ae08745Sheppo 	uint64_t	tx_tail;
11651ae08745Sheppo 	ldc_ver_t	*rcvd_ver;
11661ae08745Sheppo 
11671ae08745Sheppo 	/* get the received version */
11681ae08745Sheppo 	rcvd_ver = (ldc_ver_t *)((uint64_t)msg + LDC_PAYLOAD_VER_OFF);
11691ae08745Sheppo 
11701ae08745Sheppo 	D2(ldcp->id, "i_ldc_process_VER: (0x%llx) received VER v%u.%u\n",
11711ae08745Sheppo 	    ldcp->id, rcvd_ver->major, rcvd_ver->minor);
11721ae08745Sheppo 
1173d10e4ef2Snarayan 	/* Obtain Tx lock */
1174d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
1175d10e4ef2Snarayan 
11761ae08745Sheppo 	switch (msg->stype) {
11771ae08745Sheppo 	case LDC_INFO:
11781ae08745Sheppo 
11793af08d82Slm66018 		if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) {
11803af08d82Slm66018 			(void) i_ldc_txq_reconf(ldcp);
11813af08d82Slm66018 			i_ldc_reset_state(ldcp);
11823af08d82Slm66018 			mutex_exit(&ldcp->tx_lock);
11833af08d82Slm66018 			return (EAGAIN);
11843af08d82Slm66018 		}
11853af08d82Slm66018 
11861ae08745Sheppo 		/* get the current tail and pkt for the response */
11871ae08745Sheppo 		rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
11881ae08745Sheppo 		if (rv != 0) {
11891ae08745Sheppo 			DWARN(ldcp->id,
11901ae08745Sheppo 			    "i_ldc_process_VER: (0x%llx) err sending "
11911ae08745Sheppo 			    "version ACK/NACK\n", ldcp->id);
11923af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1193d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
11941ae08745Sheppo 			return (ECONNRESET);
11951ae08745Sheppo 		}
11961ae08745Sheppo 
11971ae08745Sheppo 		pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
11981ae08745Sheppo 		ZERO_PKT(pkt);
11991ae08745Sheppo 
12001ae08745Sheppo 		/* initialize the packet */
12011ae08745Sheppo 		pkt->type = LDC_CTRL;
12021ae08745Sheppo 		pkt->ctrl = LDC_VER;
12031ae08745Sheppo 
12041ae08745Sheppo 		for (;;) {
12051ae08745Sheppo 
12061ae08745Sheppo 			D1(ldcp->id, "i_ldc_process_VER: got %u.%u chk %u.%u\n",
12071ae08745Sheppo 			    rcvd_ver->major, rcvd_ver->minor,
12081ae08745Sheppo 			    ldc_versions[idx].major, ldc_versions[idx].minor);
12091ae08745Sheppo 
12101ae08745Sheppo 			if (rcvd_ver->major == ldc_versions[idx].major) {
12111ae08745Sheppo 				/* major version match - ACK version */
12121ae08745Sheppo 				pkt->stype = LDC_ACK;
12131ae08745Sheppo 
12141ae08745Sheppo 				/*
12151ae08745Sheppo 				 * lower minor version to the one this endpt
12161ae08745Sheppo 				 * supports, if necessary
12171ae08745Sheppo 				 */
12181ae08745Sheppo 				if (rcvd_ver->minor > ldc_versions[idx].minor)
12191ae08745Sheppo 					rcvd_ver->minor =
12201ae08745Sheppo 					    ldc_versions[idx].minor;
12211ae08745Sheppo 				bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver));
12221ae08745Sheppo 
12231ae08745Sheppo 				break;
12241ae08745Sheppo 			}
12251ae08745Sheppo 
12261ae08745Sheppo 			if (rcvd_ver->major > ldc_versions[idx].major) {
12271ae08745Sheppo 
12281ae08745Sheppo 				D1(ldcp->id, "i_ldc_process_VER: using next"
12291ae08745Sheppo 				    " lower idx=%d, v%u.%u\n", idx,
12301ae08745Sheppo 				    ldc_versions[idx].major,
12311ae08745Sheppo 				    ldc_versions[idx].minor);
12321ae08745Sheppo 
12331ae08745Sheppo 				/* nack with next lower version */
12341ae08745Sheppo 				pkt->stype = LDC_NACK;
12351ae08745Sheppo 				bcopy(&ldc_versions[idx], pkt->udata,
12361ae08745Sheppo 				    sizeof (ldc_versions[idx]));
12371ae08745Sheppo 				ldcp->next_vidx = idx;
12381ae08745Sheppo 				break;
12391ae08745Sheppo 			}
12401ae08745Sheppo 
12411ae08745Sheppo 			/* next major version */
12421ae08745Sheppo 			idx++;
12431ae08745Sheppo 
12441ae08745Sheppo 			D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx);
12451ae08745Sheppo 
12461ae08745Sheppo 			if (idx == LDC_NUM_VERS) {
12471ae08745Sheppo 				/* no version match - send NACK */
12481ae08745Sheppo 				pkt->stype = LDC_NACK;
12491ae08745Sheppo 				bzero(pkt->udata, sizeof (ldc_ver_t));
12501ae08745Sheppo 				ldcp->next_vidx = 0;
12511ae08745Sheppo 				break;
12521ae08745Sheppo 			}
12531ae08745Sheppo 		}
12541ae08745Sheppo 
12551ae08745Sheppo 		/* initiate the send by calling into HV and set the new tail */
12561ae08745Sheppo 		tx_tail = (tx_tail + LDC_PACKET_SIZE) %
12571ae08745Sheppo 		    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
12581ae08745Sheppo 
12591ae08745Sheppo 		rv = i_ldc_set_tx_tail(ldcp, tx_tail);
12601ae08745Sheppo 		if (rv == 0) {
12611ae08745Sheppo 			ldcp->tx_tail = tx_tail;
12621ae08745Sheppo 			if (pkt->stype == LDC_ACK) {
12631ae08745Sheppo 				D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent"
12641ae08745Sheppo 				    " version ACK\n", ldcp->id);
12651ae08745Sheppo 				/* Save the ACK'd version */
12661ae08745Sheppo 				ldcp->version.major = rcvd_ver->major;
12671ae08745Sheppo 				ldcp->version.minor = rcvd_ver->minor;
12680a55fbb7Slm66018 				ldcp->hstate |= TS_RCVD_VER;
12691ae08745Sheppo 				ldcp->tstate |= TS_VER_DONE;
127083d3bc6fSnarayan 				D1(DBG_ALL_LDCS,
12713af08d82Slm66018 				    "(0x%llx) Sent ACK, "
12723af08d82Slm66018 				    "Agreed on version v%u.%u\n",
12731ae08745Sheppo 				    ldcp->id, rcvd_ver->major, rcvd_ver->minor);
12741ae08745Sheppo 			}
12751ae08745Sheppo 		} else {
12761ae08745Sheppo 			DWARN(ldcp->id,
12771ae08745Sheppo 			    "i_ldc_process_VER: (0x%llx) error sending "
12781ae08745Sheppo 			    "ACK/NACK\n", ldcp->id);
12793af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1280d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
12811ae08745Sheppo 			return (ECONNRESET);
12821ae08745Sheppo 		}
12831ae08745Sheppo 
12841ae08745Sheppo 		break;
12851ae08745Sheppo 
12861ae08745Sheppo 	case LDC_ACK:
12873af08d82Slm66018 		if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) {
12883af08d82Slm66018 			if (ldcp->version.major != rcvd_ver->major ||
12893af08d82Slm66018 			    ldcp->version.minor != rcvd_ver->minor) {
12903af08d82Slm66018 
12913af08d82Slm66018 				/* mismatched version - reset connection */
12923af08d82Slm66018 				DWARN(ldcp->id,
12933af08d82Slm66018 				    "i_ldc_process_VER: (0x%llx) recvd"
12943af08d82Slm66018 				    " ACK ver != sent ACK ver\n", ldcp->id);
12953af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
12963af08d82Slm66018 				mutex_exit(&ldcp->tx_lock);
12973af08d82Slm66018 				return (ECONNRESET);
12983af08d82Slm66018 			}
12993af08d82Slm66018 		} else {
13001ae08745Sheppo 			/* SUCCESS - we have agreed on a version */
13011ae08745Sheppo 			ldcp->version.major = rcvd_ver->major;
13021ae08745Sheppo 			ldcp->version.minor = rcvd_ver->minor;
13031ae08745Sheppo 			ldcp->tstate |= TS_VER_DONE;
13043af08d82Slm66018 		}
13051ae08745Sheppo 
1306cb112a14Slm66018 		D1(ldcp->id, "(0x%llx) Got ACK, Agreed on version v%u.%u\n",
13071ae08745Sheppo 		    ldcp->id, rcvd_ver->major, rcvd_ver->minor);
13081ae08745Sheppo 
13091ae08745Sheppo 		/* initiate RTS-RTR-RDX handshake */
13101ae08745Sheppo 		rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
13111ae08745Sheppo 		if (rv) {
13121ae08745Sheppo 			DWARN(ldcp->id,
13131ae08745Sheppo 		    "i_ldc_process_VER: (0x%llx) cannot send RTS\n",
13141ae08745Sheppo 			    ldcp->id);
13153af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1316d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
13171ae08745Sheppo 			return (ECONNRESET);
13181ae08745Sheppo 		}
13191ae08745Sheppo 
13201ae08745Sheppo 		pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
13211ae08745Sheppo 		ZERO_PKT(pkt);
13221ae08745Sheppo 
13231ae08745Sheppo 		pkt->type = LDC_CTRL;
13241ae08745Sheppo 		pkt->stype = LDC_INFO;
13251ae08745Sheppo 		pkt->ctrl = LDC_RTS;
13261ae08745Sheppo 		pkt->env = ldcp->mode;
13271ae08745Sheppo 		if (ldcp->mode != LDC_MODE_RAW)
13281ae08745Sheppo 			pkt->seqid = LDC_INIT_SEQID;
13291ae08745Sheppo 
13301ae08745Sheppo 		ldcp->last_msg_rcd = LDC_INIT_SEQID;
13311ae08745Sheppo 
13321ae08745Sheppo 		DUMP_LDC_PKT(ldcp, "i_ldc_process_VER snd rts", (uint64_t)pkt);
13331ae08745Sheppo 
13341ae08745Sheppo 		/* initiate the send by calling into HV and set the new tail */
13351ae08745Sheppo 		tx_tail = (tx_tail + LDC_PACKET_SIZE) %
13361ae08745Sheppo 		    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
13371ae08745Sheppo 
13381ae08745Sheppo 		rv = i_ldc_set_tx_tail(ldcp, tx_tail);
13391ae08745Sheppo 		if (rv) {
13401ae08745Sheppo 			D2(ldcp->id,
13411ae08745Sheppo 			    "i_ldc_process_VER: (0x%llx) no listener\n",
13421ae08745Sheppo 			    ldcp->id);
13433af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1344d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
13451ae08745Sheppo 			return (ECONNRESET);
13461ae08745Sheppo 		}
13471ae08745Sheppo 
13481ae08745Sheppo 		ldcp->tx_tail = tx_tail;
13491ae08745Sheppo 		ldcp->hstate |= TS_SENT_RTS;
13501ae08745Sheppo 
13511ae08745Sheppo 		break;
13521ae08745Sheppo 
13531ae08745Sheppo 	case LDC_NACK:
13541ae08745Sheppo 		/* check if version in NACK is zero */
13551ae08745Sheppo 		if (rcvd_ver->major == 0 && rcvd_ver->minor == 0) {
13561ae08745Sheppo 			/* version handshake failure */
13571ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
13581ae08745Sheppo 			    "i_ldc_process_VER: (0x%llx) no version match\n",
13591ae08745Sheppo 			    ldcp->id);
13603af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1361d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
13621ae08745Sheppo 			return (ECONNRESET);
13631ae08745Sheppo 		}
13641ae08745Sheppo 
13651ae08745Sheppo 		/* get the current tail and pkt for the response */
13661ae08745Sheppo 		rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
13671ae08745Sheppo 		if (rv != 0) {
13681ae08745Sheppo 			cmn_err(CE_NOTE,
13691ae08745Sheppo 			    "i_ldc_process_VER: (0x%lx) err sending "
13701ae08745Sheppo 			    "version ACK/NACK\n", ldcp->id);
13713af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1372d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
13731ae08745Sheppo 			return (ECONNRESET);
13741ae08745Sheppo 		}
13751ae08745Sheppo 
13761ae08745Sheppo 		pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
13771ae08745Sheppo 		ZERO_PKT(pkt);
13781ae08745Sheppo 
13791ae08745Sheppo 		/* initialize the packet */
13801ae08745Sheppo 		pkt->type = LDC_CTRL;
13811ae08745Sheppo 		pkt->ctrl = LDC_VER;
13821ae08745Sheppo 		pkt->stype = LDC_INFO;
13831ae08745Sheppo 
13841ae08745Sheppo 		/* check ver in NACK msg has a match */
13851ae08745Sheppo 		for (;;) {
13861ae08745Sheppo 			if (rcvd_ver->major == ldc_versions[idx].major) {
13871ae08745Sheppo 				/*
13881ae08745Sheppo 				 * major version match - resubmit request
13891ae08745Sheppo 				 * if lower minor version to the one this endpt
13901ae08745Sheppo 				 * supports, if necessary
13911ae08745Sheppo 				 */
13921ae08745Sheppo 				if (rcvd_ver->minor > ldc_versions[idx].minor)
13931ae08745Sheppo 					rcvd_ver->minor =
13941ae08745Sheppo 					    ldc_versions[idx].minor;
13951ae08745Sheppo 				bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver));
13961ae08745Sheppo 				break;
13971ae08745Sheppo 			}
13981ae08745Sheppo 
13991ae08745Sheppo 			if (rcvd_ver->major > ldc_versions[idx].major) {
14001ae08745Sheppo 
14011ae08745Sheppo 				D1(ldcp->id, "i_ldc_process_VER: using next"
14021ae08745Sheppo 				    " lower idx=%d, v%u.%u\n", idx,
14031ae08745Sheppo 				    ldc_versions[idx].major,
14041ae08745Sheppo 				    ldc_versions[idx].minor);
14051ae08745Sheppo 
14061ae08745Sheppo 				/* send next lower version */
14071ae08745Sheppo 				bcopy(&ldc_versions[idx], pkt->udata,
14081ae08745Sheppo 				    sizeof (ldc_versions[idx]));
14091ae08745Sheppo 				ldcp->next_vidx = idx;
14101ae08745Sheppo 				break;
14111ae08745Sheppo 			}
14121ae08745Sheppo 
14131ae08745Sheppo 			/* next version */
14141ae08745Sheppo 			idx++;
14151ae08745Sheppo 
14161ae08745Sheppo 			D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx);
14171ae08745Sheppo 
14181ae08745Sheppo 			if (idx == LDC_NUM_VERS) {
14191ae08745Sheppo 				/* no version match - terminate */
14201ae08745Sheppo 				ldcp->next_vidx = 0;
1421d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
14221ae08745Sheppo 				return (ECONNRESET);
14231ae08745Sheppo 			}
14241ae08745Sheppo 		}
14251ae08745Sheppo 
14261ae08745Sheppo 		/* initiate the send by calling into HV and set the new tail */
14271ae08745Sheppo 		tx_tail = (tx_tail + LDC_PACKET_SIZE) %
14281ae08745Sheppo 		    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
14291ae08745Sheppo 
14301ae08745Sheppo 		rv = i_ldc_set_tx_tail(ldcp, tx_tail);
14311ae08745Sheppo 		if (rv == 0) {
14321ae08745Sheppo 			D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent version"
14331ae08745Sheppo 			    "INFO v%u.%u\n", ldcp->id, ldc_versions[idx].major,
14341ae08745Sheppo 			    ldc_versions[idx].minor);
14351ae08745Sheppo 			ldcp->tx_tail = tx_tail;
14361ae08745Sheppo 		} else {
14371ae08745Sheppo 			cmn_err(CE_NOTE,
14381ae08745Sheppo 			    "i_ldc_process_VER: (0x%lx) error sending version"
14391ae08745Sheppo 			    "INFO\n", ldcp->id);
14403af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1441d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
14421ae08745Sheppo 			return (ECONNRESET);
14431ae08745Sheppo 		}
14441ae08745Sheppo 
14451ae08745Sheppo 		break;
14461ae08745Sheppo 	}
14471ae08745Sheppo 
1448d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
14491ae08745Sheppo 	return (rv);
14501ae08745Sheppo }
14511ae08745Sheppo 
14521ae08745Sheppo 
14531ae08745Sheppo /*
14541ae08745Sheppo  * Process an incoming RTS ctrl message
14551ae08745Sheppo  */
14561ae08745Sheppo static int
14571ae08745Sheppo i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg)
14581ae08745Sheppo {
14591ae08745Sheppo 	int 		rv = 0;
14601ae08745Sheppo 	ldc_msg_t 	*pkt;
14611ae08745Sheppo 	uint64_t	tx_tail;
14621ae08745Sheppo 	boolean_t	sent_NACK = B_FALSE;
14631ae08745Sheppo 
14641ae08745Sheppo 	D2(ldcp->id, "i_ldc_process_RTS: (0x%llx) received RTS\n", ldcp->id);
14651ae08745Sheppo 
14661ae08745Sheppo 	switch (msg->stype) {
14671ae08745Sheppo 	case LDC_NACK:
14681ae08745Sheppo 		DWARN(ldcp->id,
14691ae08745Sheppo 		    "i_ldc_process_RTS: (0x%llx) RTS NACK received\n",
14701ae08745Sheppo 		    ldcp->id);
14711ae08745Sheppo 
14721ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1473d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
14743af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1475d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
14761ae08745Sheppo 		rv = ECONNRESET;
14771ae08745Sheppo 		break;
14781ae08745Sheppo 
14791ae08745Sheppo 	case LDC_INFO:
14801ae08745Sheppo 
14811ae08745Sheppo 		/* check mode */
14821ae08745Sheppo 		if (ldcp->mode != (ldc_mode_t)msg->env) {
14831ae08745Sheppo 			cmn_err(CE_NOTE,
14841ae08745Sheppo 			    "i_ldc_process_RTS: (0x%lx) mode mismatch\n",
14851ae08745Sheppo 			    ldcp->id);
14861ae08745Sheppo 			/*
14871ae08745Sheppo 			 * send NACK in response to MODE message
14881ae08745Sheppo 			 * get the current tail for the response
14891ae08745Sheppo 			 */
14901ae08745Sheppo 			rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTS);
14911ae08745Sheppo 			if (rv) {
14921ae08745Sheppo 				/* if cannot send NACK - reset channel */
1493d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
14943af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
1495d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
14961ae08745Sheppo 				rv = ECONNRESET;
14971ae08745Sheppo 				break;
14981ae08745Sheppo 			}
14991ae08745Sheppo 			sent_NACK = B_TRUE;
15001ae08745Sheppo 		}
15011ae08745Sheppo 		break;
15021ae08745Sheppo 	default:
15031ae08745Sheppo 		DWARN(ldcp->id, "i_ldc_process_RTS: (0x%llx) unexp ACK\n",
15041ae08745Sheppo 		    ldcp->id);
1505d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
15063af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1507d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
15081ae08745Sheppo 		rv = ECONNRESET;
15091ae08745Sheppo 		break;
15101ae08745Sheppo 	}
15111ae08745Sheppo 
15121ae08745Sheppo 	/*
15131ae08745Sheppo 	 * If either the connection was reset (when rv != 0) or
15141ae08745Sheppo 	 * a NACK was sent, we return. In the case of a NACK
15151ae08745Sheppo 	 * we dont want to consume the packet that came in but
15161ae08745Sheppo 	 * not record that we received the RTS
15171ae08745Sheppo 	 */
15181ae08745Sheppo 	if (rv || sent_NACK)
15191ae08745Sheppo 		return (rv);
15201ae08745Sheppo 
15211ae08745Sheppo 	/* record RTS received */
15221ae08745Sheppo 	ldcp->hstate |= TS_RCVD_RTS;
15231ae08745Sheppo 
15241ae08745Sheppo 	/* store initial SEQID info */
15251ae08745Sheppo 	ldcp->last_msg_snt = msg->seqid;
15261ae08745Sheppo 
1527d10e4ef2Snarayan 	/* Obtain Tx lock */
1528d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
1529d10e4ef2Snarayan 
15301ae08745Sheppo 	/* get the current tail for the response */
15311ae08745Sheppo 	rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
15321ae08745Sheppo 	if (rv != 0) {
15331ae08745Sheppo 		cmn_err(CE_NOTE,
15341ae08745Sheppo 		    "i_ldc_process_RTS: (0x%lx) err sending RTR\n",
15351ae08745Sheppo 		    ldcp->id);
15363af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1537d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
15381ae08745Sheppo 		return (ECONNRESET);
15391ae08745Sheppo 	}
15401ae08745Sheppo 
15411ae08745Sheppo 	pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
15421ae08745Sheppo 	ZERO_PKT(pkt);
15431ae08745Sheppo 
15441ae08745Sheppo 	/* initialize the packet */
15451ae08745Sheppo 	pkt->type = LDC_CTRL;
15461ae08745Sheppo 	pkt->stype = LDC_INFO;
15471ae08745Sheppo 	pkt->ctrl = LDC_RTR;
15481ae08745Sheppo 	pkt->env = ldcp->mode;
15491ae08745Sheppo 	if (ldcp->mode != LDC_MODE_RAW)
15501ae08745Sheppo 		pkt->seqid = LDC_INIT_SEQID;
15511ae08745Sheppo 
15521ae08745Sheppo 	ldcp->last_msg_rcd = msg->seqid;
15531ae08745Sheppo 
15541ae08745Sheppo 	/* initiate the send by calling into HV and set the new tail */
15551ae08745Sheppo 	tx_tail = (tx_tail + LDC_PACKET_SIZE) %
15561ae08745Sheppo 	    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
15571ae08745Sheppo 
15581ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
15591ae08745Sheppo 	if (rv == 0) {
15601ae08745Sheppo 		D2(ldcp->id,
15611ae08745Sheppo 		    "i_ldc_process_RTS: (0x%llx) sent RTR\n", ldcp->id);
15621ae08745Sheppo 		DUMP_LDC_PKT(ldcp, "i_ldc_process_RTS sent rtr", (uint64_t)pkt);
15631ae08745Sheppo 
15641ae08745Sheppo 		ldcp->tx_tail = tx_tail;
15651ae08745Sheppo 		ldcp->hstate |= TS_SENT_RTR;
15661ae08745Sheppo 
15671ae08745Sheppo 	} else {
15681ae08745Sheppo 		cmn_err(CE_NOTE,
15691ae08745Sheppo 		    "i_ldc_process_RTS: (0x%lx) error sending RTR\n",
15701ae08745Sheppo 		    ldcp->id);
15713af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1572d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
15731ae08745Sheppo 		return (ECONNRESET);
15741ae08745Sheppo 	}
15751ae08745Sheppo 
1576d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
15771ae08745Sheppo 	return (0);
15781ae08745Sheppo }
15791ae08745Sheppo 
15801ae08745Sheppo /*
15811ae08745Sheppo  * Process an incoming RTR ctrl message
15821ae08745Sheppo  */
15831ae08745Sheppo static int
15841ae08745Sheppo i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg)
15851ae08745Sheppo {
15861ae08745Sheppo 	int 		rv = 0;
15871ae08745Sheppo 	boolean_t	sent_NACK = B_FALSE;
15881ae08745Sheppo 
15891ae08745Sheppo 	D2(ldcp->id, "i_ldc_process_RTR: (0x%llx) received RTR\n", ldcp->id);
15901ae08745Sheppo 
15911ae08745Sheppo 	switch (msg->stype) {
15921ae08745Sheppo 	case LDC_NACK:
15931ae08745Sheppo 		/* RTR NACK received */
15941ae08745Sheppo 		DWARN(ldcp->id,
15951ae08745Sheppo 		    "i_ldc_process_RTR: (0x%llx) RTR NACK received\n",
15961ae08745Sheppo 		    ldcp->id);
15971ae08745Sheppo 
15981ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1599d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
16003af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1601d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
16021ae08745Sheppo 		rv = ECONNRESET;
16031ae08745Sheppo 
16041ae08745Sheppo 		break;
16051ae08745Sheppo 
16061ae08745Sheppo 	case LDC_INFO:
16071ae08745Sheppo 
16081ae08745Sheppo 		/* check mode */
16091ae08745Sheppo 		if (ldcp->mode != (ldc_mode_t)msg->env) {
16101ae08745Sheppo 			DWARN(ldcp->id,
1611cb112a14Slm66018 			    "i_ldc_process_RTR: (0x%llx) mode mismatch, "
1612cb112a14Slm66018 			    "expecting 0x%x, got 0x%x\n",
1613cb112a14Slm66018 			    ldcp->id, ldcp->mode, (ldc_mode_t)msg->env);
16141ae08745Sheppo 			/*
16151ae08745Sheppo 			 * send NACK in response to MODE message
16161ae08745Sheppo 			 * get the current tail for the response
16171ae08745Sheppo 			 */
16181ae08745Sheppo 			rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTR);
16191ae08745Sheppo 			if (rv) {
16201ae08745Sheppo 				/* if cannot send NACK - reset channel */
1621d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
16223af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
1623d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
16241ae08745Sheppo 				rv = ECONNRESET;
16251ae08745Sheppo 				break;
16261ae08745Sheppo 			}
16271ae08745Sheppo 			sent_NACK = B_TRUE;
16281ae08745Sheppo 		}
16291ae08745Sheppo 		break;
16301ae08745Sheppo 
16311ae08745Sheppo 	default:
16321ae08745Sheppo 		DWARN(ldcp->id, "i_ldc_process_RTR: (0x%llx) unexp ACK\n",
16331ae08745Sheppo 		    ldcp->id);
16341ae08745Sheppo 
16351ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1636d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
16373af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1638d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
16391ae08745Sheppo 		rv = ECONNRESET;
16401ae08745Sheppo 		break;
16411ae08745Sheppo 	}
16421ae08745Sheppo 
16431ae08745Sheppo 	/*
16441ae08745Sheppo 	 * If either the connection was reset (when rv != 0) or
16451ae08745Sheppo 	 * a NACK was sent, we return. In the case of a NACK
16461ae08745Sheppo 	 * we dont want to consume the packet that came in but
16471ae08745Sheppo 	 * not record that we received the RTR
16481ae08745Sheppo 	 */
16491ae08745Sheppo 	if (rv || sent_NACK)
16501ae08745Sheppo 		return (rv);
16511ae08745Sheppo 
16521ae08745Sheppo 	ldcp->last_msg_snt = msg->seqid;
16531ae08745Sheppo 	ldcp->hstate |= TS_RCVD_RTR;
16541ae08745Sheppo 
16551ae08745Sheppo 	rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_INFO, LDC_RDX);
16561ae08745Sheppo 	if (rv) {
16571ae08745Sheppo 		cmn_err(CE_NOTE,
16581ae08745Sheppo 		    "i_ldc_process_RTR: (0x%lx) cannot send RDX\n",
16591ae08745Sheppo 		    ldcp->id);
1660d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
16613af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1662d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
16631ae08745Sheppo 		return (ECONNRESET);
16641ae08745Sheppo 	}
16651ae08745Sheppo 	D2(ldcp->id,
16661ae08745Sheppo 	    "i_ldc_process_RTR: (0x%llx) sent RDX\n", ldcp->id);
16671ae08745Sheppo 
16681ae08745Sheppo 	ldcp->hstate |= TS_SENT_RDX;
16691ae08745Sheppo 	ldcp->tstate |= TS_HSHAKE_DONE;
16703af08d82Slm66018 	if ((ldcp->tstate & TS_IN_RESET) == 0)
16711ae08745Sheppo 		ldcp->status = LDC_UP;
16721ae08745Sheppo 
1673cb112a14Slm66018 	D1(ldcp->id, "(0x%llx) Handshake Complete\n", ldcp->id);
16741ae08745Sheppo 
16751ae08745Sheppo 	return (0);
16761ae08745Sheppo }
16771ae08745Sheppo 
16781ae08745Sheppo 
16791ae08745Sheppo /*
16801ae08745Sheppo  * Process an incoming RDX ctrl message
16811ae08745Sheppo  */
16821ae08745Sheppo static int
16831ae08745Sheppo i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg)
16841ae08745Sheppo {
16851ae08745Sheppo 	int	rv = 0;
16861ae08745Sheppo 
16871ae08745Sheppo 	D2(ldcp->id, "i_ldc_process_RDX: (0x%llx) received RDX\n", ldcp->id);
16881ae08745Sheppo 
16891ae08745Sheppo 	switch (msg->stype) {
16901ae08745Sheppo 	case LDC_NACK:
16911ae08745Sheppo 		/* RDX NACK received */
16921ae08745Sheppo 		DWARN(ldcp->id,
16931ae08745Sheppo 		    "i_ldc_process_RDX: (0x%llx) RDX NACK received\n",
16941ae08745Sheppo 		    ldcp->id);
16951ae08745Sheppo 
16961ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1697d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
16983af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1699d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
17001ae08745Sheppo 		rv = ECONNRESET;
17011ae08745Sheppo 
17021ae08745Sheppo 		break;
17031ae08745Sheppo 
17041ae08745Sheppo 	case LDC_INFO:
17051ae08745Sheppo 
17061ae08745Sheppo 		/*
17071ae08745Sheppo 		 * if channel is UP and a RDX received after data transmission
17081ae08745Sheppo 		 * has commenced it is an error
17091ae08745Sheppo 		 */
17101ae08745Sheppo 		if ((ldcp->tstate == TS_UP) && (ldcp->hstate & TS_RCVD_RDX)) {
17111ae08745Sheppo 			DWARN(DBG_ALL_LDCS,
17121ae08745Sheppo 			    "i_ldc_process_RDX: (0x%llx) unexpected RDX"
17131ae08745Sheppo 			    " - LDC reset\n", ldcp->id);
1714d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
17153af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1716d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
17171ae08745Sheppo 			return (ECONNRESET);
17181ae08745Sheppo 		}
17191ae08745Sheppo 
17201ae08745Sheppo 		ldcp->hstate |= TS_RCVD_RDX;
17211ae08745Sheppo 		ldcp->tstate |= TS_HSHAKE_DONE;
17223af08d82Slm66018 		if ((ldcp->tstate & TS_IN_RESET) == 0)
17231ae08745Sheppo 			ldcp->status = LDC_UP;
17241ae08745Sheppo 
17251ae08745Sheppo 		D1(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id);
17261ae08745Sheppo 		break;
17271ae08745Sheppo 
17281ae08745Sheppo 	default:
17291ae08745Sheppo 		DWARN(ldcp->id, "i_ldc_process_RDX: (0x%llx) unexp ACK\n",
17301ae08745Sheppo 		    ldcp->id);
17311ae08745Sheppo 
17321ae08745Sheppo 		/* Reset the channel -- as we cannot continue */
1733d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
17343af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1735d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
17361ae08745Sheppo 		rv = ECONNRESET;
17371ae08745Sheppo 		break;
17381ae08745Sheppo 	}
17391ae08745Sheppo 
17401ae08745Sheppo 	return (rv);
17411ae08745Sheppo }
17421ae08745Sheppo 
17431ae08745Sheppo /*
17441ae08745Sheppo  * Process an incoming ACK for a data packet
17451ae08745Sheppo  */
17461ae08745Sheppo static int
17471ae08745Sheppo i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg)
17481ae08745Sheppo {
17491ae08745Sheppo 	int		rv;
17501ae08745Sheppo 	uint64_t 	tx_head;
17511ae08745Sheppo 	ldc_msg_t	*pkt;
17521ae08745Sheppo 
1753d10e4ef2Snarayan 	/* Obtain Tx lock */
1754d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
1755d10e4ef2Snarayan 
17561ae08745Sheppo 	/*
1757d10e4ef2Snarayan 	 * Read the current Tx head and tail
17581ae08745Sheppo 	 */
17591ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
17601ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
17611ae08745Sheppo 	if (rv != 0) {
17621ae08745Sheppo 		cmn_err(CE_WARN,
17631ae08745Sheppo 		    "i_ldc_process_data_ACK: (0x%lx) cannot read qptrs\n",
17641ae08745Sheppo 		    ldcp->id);
1765d10e4ef2Snarayan 
1766d10e4ef2Snarayan 		/* Reset the channel -- as we cannot continue */
17673af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
1768d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
1769d10e4ef2Snarayan 		return (ECONNRESET);
17701ae08745Sheppo 	}
17711ae08745Sheppo 
17721ae08745Sheppo 	/*
17731ae08745Sheppo 	 * loop from where the previous ACK location was to the
17741ae08745Sheppo 	 * current head location. This is how far the HV has
17751ae08745Sheppo 	 * actually send pkts. Pkts between head and tail are
17761ae08745Sheppo 	 * yet to be sent by HV.
17771ae08745Sheppo 	 */
17781ae08745Sheppo 	tx_head = ldcp->tx_ackd_head;
17791ae08745Sheppo 	for (;;) {
17801ae08745Sheppo 		pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_head);
17811ae08745Sheppo 		tx_head = (tx_head + LDC_PACKET_SIZE) %
17821ae08745Sheppo 		    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
17831ae08745Sheppo 
17841ae08745Sheppo 		if (pkt->seqid == msg->ackid) {
17851ae08745Sheppo 			D2(ldcp->id,
17861ae08745Sheppo 			    "i_ldc_process_data_ACK: (0x%llx) found packet\n",
17871ae08745Sheppo 			    ldcp->id);
17881ae08745Sheppo 			ldcp->last_ack_rcd = msg->ackid;
17891ae08745Sheppo 			ldcp->tx_ackd_head = tx_head;
17901ae08745Sheppo 			break;
17911ae08745Sheppo 		}
17921ae08745Sheppo 		if (tx_head == ldcp->tx_head) {
17931ae08745Sheppo 			/* could not find packet */
17941ae08745Sheppo 			DWARN(ldcp->id,
17951ae08745Sheppo 			    "i_ldc_process_data_ACK: (0x%llx) invalid ACKid\n",
17961ae08745Sheppo 			    ldcp->id);
1797d10e4ef2Snarayan 
1798d10e4ef2Snarayan 			/* Reset the channel -- as we cannot continue */
17993af08d82Slm66018 			i_ldc_reset(ldcp, B_TRUE);
1800d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
1801d10e4ef2Snarayan 			return (ECONNRESET);
18021ae08745Sheppo 		}
18031ae08745Sheppo 	}
18041ae08745Sheppo 
1805d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
18061ae08745Sheppo 	return (0);
18071ae08745Sheppo }
18081ae08745Sheppo 
18091ae08745Sheppo /*
18101ae08745Sheppo  * Process incoming control message
18111ae08745Sheppo  * Return 0 - session can continue
18121ae08745Sheppo  *        EAGAIN - reprocess packet - state was changed
18131ae08745Sheppo  *	  ECONNRESET - channel was reset
18141ae08745Sheppo  */
18151ae08745Sheppo static int
18161ae08745Sheppo i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *msg)
18171ae08745Sheppo {
18181ae08745Sheppo 	int 		rv = 0;
18191ae08745Sheppo 
18203af08d82Slm66018 	D1(ldcp->id, "i_ldc_ctrlmsg: (%llx) tstate = %lx, hstate = %lx\n",
18213af08d82Slm66018 	    ldcp->id, ldcp->tstate, ldcp->hstate);
18223af08d82Slm66018 
18233af08d82Slm66018 	switch (ldcp->tstate & ~TS_IN_RESET) {
18241ae08745Sheppo 
18251ae08745Sheppo 	case TS_OPEN:
18261ae08745Sheppo 	case TS_READY:
18271ae08745Sheppo 
18281ae08745Sheppo 		switch (msg->ctrl & LDC_CTRL_MASK) {
18291ae08745Sheppo 		case LDC_VER:
18301ae08745Sheppo 			/* process version message */
18311ae08745Sheppo 			rv = i_ldc_process_VER(ldcp, msg);
18321ae08745Sheppo 			break;
18331ae08745Sheppo 		default:
18341ae08745Sheppo 			DWARN(ldcp->id,
18351ae08745Sheppo 			    "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x "
18361ae08745Sheppo 			    "tstate=0x%x\n", ldcp->id,
18371ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate);
18381ae08745Sheppo 			break;
18391ae08745Sheppo 		}
18401ae08745Sheppo 
18411ae08745Sheppo 		break;
18421ae08745Sheppo 
18431ae08745Sheppo 	case TS_VREADY:
18441ae08745Sheppo 
18451ae08745Sheppo 		switch (msg->ctrl & LDC_CTRL_MASK) {
18461ae08745Sheppo 		case LDC_VER:
18473af08d82Slm66018 			/* process version message */
18483af08d82Slm66018 			rv = i_ldc_process_VER(ldcp, msg);
18491ae08745Sheppo 			break;
18501ae08745Sheppo 		case LDC_RTS:
18511ae08745Sheppo 			/* process RTS message */
18521ae08745Sheppo 			rv = i_ldc_process_RTS(ldcp, msg);
18531ae08745Sheppo 			break;
18541ae08745Sheppo 		case LDC_RTR:
18551ae08745Sheppo 			/* process RTR message */
18561ae08745Sheppo 			rv = i_ldc_process_RTR(ldcp, msg);
18571ae08745Sheppo 			break;
18581ae08745Sheppo 		case LDC_RDX:
18591ae08745Sheppo 			/* process RDX message */
18601ae08745Sheppo 			rv = i_ldc_process_RDX(ldcp, msg);
18611ae08745Sheppo 			break;
18621ae08745Sheppo 		default:
18631ae08745Sheppo 			DWARN(ldcp->id,
18641ae08745Sheppo 			    "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x "
18651ae08745Sheppo 			    "tstate=0x%x\n", ldcp->id,
18661ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate);
18671ae08745Sheppo 			break;
18681ae08745Sheppo 		}
18691ae08745Sheppo 
18701ae08745Sheppo 		break;
18711ae08745Sheppo 
18721ae08745Sheppo 	case TS_UP:
18731ae08745Sheppo 
18741ae08745Sheppo 		switch (msg->ctrl & LDC_CTRL_MASK) {
18751ae08745Sheppo 		case LDC_VER:
18761ae08745Sheppo 			DWARN(ldcp->id,
18771ae08745Sheppo 			    "i_ldc_ctrlmsg: (0x%llx) unexpected VER "
18781ae08745Sheppo 			    "- LDC reset\n", ldcp->id);
18791ae08745Sheppo 			/* peer is redoing version negotiation */
1880d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
18811ae08745Sheppo 			(void) i_ldc_txq_reconf(ldcp);
18821ae08745Sheppo 			i_ldc_reset_state(ldcp);
1883d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
18841ae08745Sheppo 			rv = EAGAIN;
18851ae08745Sheppo 			break;
18861ae08745Sheppo 
18871ae08745Sheppo 		case LDC_RDX:
18881ae08745Sheppo 			/* process RDX message */
18891ae08745Sheppo 			rv = i_ldc_process_RDX(ldcp, msg);
18901ae08745Sheppo 			break;
18911ae08745Sheppo 
18921ae08745Sheppo 		default:
18931ae08745Sheppo 			DWARN(ldcp->id,
18941ae08745Sheppo 			    "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x "
18951ae08745Sheppo 			    "tstate=0x%x\n", ldcp->id,
18961ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate);
18971ae08745Sheppo 			break;
18981ae08745Sheppo 		}
18991ae08745Sheppo 	}
19001ae08745Sheppo 
19011ae08745Sheppo 	return (rv);
19021ae08745Sheppo }
19031ae08745Sheppo 
19041ae08745Sheppo /*
19051ae08745Sheppo  * Register channel with the channel nexus
19061ae08745Sheppo  */
19071ae08745Sheppo static int
19081ae08745Sheppo i_ldc_register_channel(ldc_chan_t *ldcp)
19091ae08745Sheppo {
19101ae08745Sheppo 	int		rv = 0;
19111ae08745Sheppo 	ldc_cnex_t	*cinfo = &ldcssp->cinfo;
19121ae08745Sheppo 
19131ae08745Sheppo 	if (cinfo->dip == NULL) {
19141ae08745Sheppo 		DWARN(ldcp->id,
19151ae08745Sheppo 		    "i_ldc_register_channel: cnex has not registered\n");
19161ae08745Sheppo 		return (EAGAIN);
19171ae08745Sheppo 	}
19181ae08745Sheppo 
19191ae08745Sheppo 	rv = cinfo->reg_chan(cinfo->dip, ldcp->id, ldcp->devclass);
19201ae08745Sheppo 	if (rv) {
19211ae08745Sheppo 		DWARN(ldcp->id,
19221ae08745Sheppo 		    "i_ldc_register_channel: cannot register channel\n");
19231ae08745Sheppo 		return (rv);
19241ae08745Sheppo 	}
19251ae08745Sheppo 
19261ae08745Sheppo 	rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR,
19271ae08745Sheppo 	    i_ldc_tx_hdlr, ldcp, NULL);
19281ae08745Sheppo 	if (rv) {
19291ae08745Sheppo 		DWARN(ldcp->id,
19301ae08745Sheppo 		    "i_ldc_register_channel: cannot add Tx interrupt\n");
19311ae08745Sheppo 		(void) cinfo->unreg_chan(cinfo->dip, ldcp->id);
19321ae08745Sheppo 		return (rv);
19331ae08745Sheppo 	}
19341ae08745Sheppo 
19351ae08745Sheppo 	rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR,
19361ae08745Sheppo 	    i_ldc_rx_hdlr, ldcp, NULL);
19371ae08745Sheppo 	if (rv) {
19381ae08745Sheppo 		DWARN(ldcp->id,
19391ae08745Sheppo 		    "i_ldc_register_channel: cannot add Rx interrupt\n");
19401ae08745Sheppo 		(void) cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR);
19411ae08745Sheppo 		(void) cinfo->unreg_chan(cinfo->dip, ldcp->id);
19421ae08745Sheppo 		return (rv);
19431ae08745Sheppo 	}
19441ae08745Sheppo 
19451ae08745Sheppo 	ldcp->tstate |= TS_CNEX_RDY;
19461ae08745Sheppo 
19471ae08745Sheppo 	return (0);
19481ae08745Sheppo }
19491ae08745Sheppo 
19501ae08745Sheppo /*
19511ae08745Sheppo  * Unregister a channel with the channel nexus
19521ae08745Sheppo  */
19531ae08745Sheppo static int
19541ae08745Sheppo i_ldc_unregister_channel(ldc_chan_t *ldcp)
19551ae08745Sheppo {
19561ae08745Sheppo 	int		rv = 0;
19571ae08745Sheppo 	ldc_cnex_t	*cinfo = &ldcssp->cinfo;
19581ae08745Sheppo 
19591ae08745Sheppo 	if (cinfo->dip == NULL) {
19601ae08745Sheppo 		DWARN(ldcp->id,
19611ae08745Sheppo 		    "i_ldc_unregister_channel: cnex has not registered\n");
19621ae08745Sheppo 		return (EAGAIN);
19631ae08745Sheppo 	}
19641ae08745Sheppo 
19651ae08745Sheppo 	if (ldcp->tstate & TS_CNEX_RDY) {
19661ae08745Sheppo 
1967d10e4ef2Snarayan 		/* Remove the Rx interrupt */
19681ae08745Sheppo 		rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR);
19691ae08745Sheppo 		if (rv) {
19703af08d82Slm66018 			if (rv != EAGAIN) {
19711ae08745Sheppo 				DWARN(ldcp->id,
19723af08d82Slm66018 				    "i_ldc_unregister_channel: err removing "
19733af08d82Slm66018 				    "Rx intr\n");
1974d10e4ef2Snarayan 				return (rv);
19751ae08745Sheppo 			}
1976d10e4ef2Snarayan 
19773af08d82Slm66018 			/*
19783af08d82Slm66018 			 * If interrupts are pending and handler has
19793af08d82Slm66018 			 * finished running, clear interrupt and try
19803af08d82Slm66018 			 * again
19813af08d82Slm66018 			 */
19823af08d82Slm66018 			if (ldcp->rx_intr_state != LDC_INTR_PEND)
19833af08d82Slm66018 				return (rv);
19843af08d82Slm66018 
19853af08d82Slm66018 			(void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
19863af08d82Slm66018 			rv = cinfo->rem_intr(cinfo->dip, ldcp->id,
19873af08d82Slm66018 			    CNEX_RX_INTR);
19883af08d82Slm66018 			if (rv) {
19893af08d82Slm66018 				DWARN(ldcp->id, "i_ldc_unregister_channel: "
19903af08d82Slm66018 				    "err removing Rx interrupt\n");
19913af08d82Slm66018 				return (rv);
19923af08d82Slm66018 			}
19933af08d82Slm66018 		}
19943af08d82Slm66018 
1995d10e4ef2Snarayan 		/* Remove the Tx interrupt */
19961ae08745Sheppo 		rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR);
19971ae08745Sheppo 		if (rv) {
19981ae08745Sheppo 			DWARN(ldcp->id,
19991ae08745Sheppo 			    "i_ldc_unregister_channel: err removing Tx intr\n");
2000d10e4ef2Snarayan 			return (rv);
20011ae08745Sheppo 		}
2002d10e4ef2Snarayan 
2003d10e4ef2Snarayan 		/* Unregister the channel */
20041ae08745Sheppo 		rv = cinfo->unreg_chan(ldcssp->cinfo.dip, ldcp->id);
20051ae08745Sheppo 		if (rv) {
20061ae08745Sheppo 			DWARN(ldcp->id,
20071ae08745Sheppo 			    "i_ldc_unregister_channel: cannot unreg channel\n");
2008d10e4ef2Snarayan 			return (rv);
20091ae08745Sheppo 		}
20101ae08745Sheppo 
20111ae08745Sheppo 		ldcp->tstate &= ~TS_CNEX_RDY;
20121ae08745Sheppo 	}
20131ae08745Sheppo 
20141ae08745Sheppo 	return (0);
20151ae08745Sheppo }
20161ae08745Sheppo 
20171ae08745Sheppo 
20181ae08745Sheppo /*
20191ae08745Sheppo  * LDC transmit interrupt handler
20201ae08745Sheppo  *    triggered for chanel up/down/reset events
20211ae08745Sheppo  *    and Tx queue content changes
20221ae08745Sheppo  */
20231ae08745Sheppo static uint_t
20241ae08745Sheppo i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2)
20251ae08745Sheppo {
20261ae08745Sheppo 	_NOTE(ARGUNUSED(arg2))
20271ae08745Sheppo 
20281ae08745Sheppo 	int 		rv;
20291ae08745Sheppo 	ldc_chan_t 	*ldcp;
20301ae08745Sheppo 	boolean_t 	notify_client = B_FALSE;
20313af08d82Slm66018 	uint64_t	notify_event = 0, link_state;
20321ae08745Sheppo 
20331ae08745Sheppo 	/* Get the channel for which interrupt was received */
20341ae08745Sheppo 	ASSERT(arg1 != NULL);
20351ae08745Sheppo 	ldcp = (ldc_chan_t *)arg1;
20361ae08745Sheppo 
20371ae08745Sheppo 	D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n",
20381ae08745Sheppo 	    ldcp->id, ldcp);
20391ae08745Sheppo 
20401ae08745Sheppo 	/* Lock channel */
20411ae08745Sheppo 	mutex_enter(&ldcp->lock);
20421ae08745Sheppo 
2043d10e4ef2Snarayan 	/* Obtain Tx lock */
2044d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
2045d10e4ef2Snarayan 
20464bac2208Snarayan 	/* mark interrupt as pending */
20473af08d82Slm66018 	ldcp->tx_intr_state = LDC_INTR_ACTIVE;
20483af08d82Slm66018 
20493af08d82Slm66018 	/* save current link state */
20503af08d82Slm66018 	link_state = ldcp->link_state;
20514bac2208Snarayan 
20521ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id, &ldcp->tx_head, &ldcp->tx_tail,
20531ae08745Sheppo 	    &ldcp->link_state);
20541ae08745Sheppo 	if (rv) {
20551ae08745Sheppo 		cmn_err(CE_WARN,
20561ae08745Sheppo 		    "i_ldc_tx_hdlr: (0x%lx) cannot read queue ptrs rv=0x%d\n",
20571ae08745Sheppo 		    ldcp->id, rv);
20584bac2208Snarayan 		i_ldc_clear_intr(ldcp, CNEX_TX_INTR);
2059d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
20601ae08745Sheppo 		mutex_exit(&ldcp->lock);
20611ae08745Sheppo 		return (DDI_INTR_CLAIMED);
20621ae08745Sheppo 	}
20631ae08745Sheppo 
20641ae08745Sheppo 	/*
20651ae08745Sheppo 	 * reset the channel state if the channel went down
20661ae08745Sheppo 	 * (other side unconfigured queue) or channel was reset
20671ae08745Sheppo 	 * (other side reconfigured its queue)
20681ae08745Sheppo 	 */
20693af08d82Slm66018 	if (link_state != ldcp->link_state &&
20703af08d82Slm66018 	    ldcp->link_state == LDC_CHANNEL_DOWN) {
20711ae08745Sheppo 		D1(ldcp->id, "i_ldc_tx_hdlr: channel link down\n", ldcp->id);
20723af08d82Slm66018 		i_ldc_reset(ldcp, B_FALSE);
20731ae08745Sheppo 		notify_client = B_TRUE;
20741ae08745Sheppo 		notify_event = LDC_EVT_DOWN;
20751ae08745Sheppo 	}
20761ae08745Sheppo 
20773af08d82Slm66018 	if (link_state != ldcp->link_state &&
20783af08d82Slm66018 	    ldcp->link_state == LDC_CHANNEL_RESET) {
20791ae08745Sheppo 		D1(ldcp->id, "i_ldc_tx_hdlr: channel link reset\n", ldcp->id);
20803af08d82Slm66018 		i_ldc_reset(ldcp, B_FALSE);
20811ae08745Sheppo 		notify_client = B_TRUE;
20821ae08745Sheppo 		notify_event = LDC_EVT_RESET;
20831ae08745Sheppo 	}
20841ae08745Sheppo 
20853af08d82Slm66018 	if (link_state != ldcp->link_state &&
20863af08d82Slm66018 	    (ldcp->tstate & ~TS_IN_RESET) == TS_OPEN &&
20873af08d82Slm66018 	    ldcp->link_state == LDC_CHANNEL_UP) {
20881ae08745Sheppo 		D1(ldcp->id, "i_ldc_tx_hdlr: channel link up\n", ldcp->id);
20891ae08745Sheppo 		notify_client = B_TRUE;
20901ae08745Sheppo 		notify_event = LDC_EVT_RESET;
20911ae08745Sheppo 		ldcp->tstate |= TS_LINK_READY;
20921ae08745Sheppo 		ldcp->status = LDC_READY;
20931ae08745Sheppo 	}
20941ae08745Sheppo 
20951ae08745Sheppo 	/* if callbacks are disabled, do not notify */
20961ae08745Sheppo 	if (!ldcp->cb_enabled)
20971ae08745Sheppo 		notify_client = B_FALSE;
20981ae08745Sheppo 
20994d39be2bSsg70180 	i_ldc_clear_intr(ldcp, CNEX_TX_INTR);
210022f747efSnarayan 	mutex_exit(&ldcp->tx_lock);
21011ae08745Sheppo 
21021ae08745Sheppo 	if (notify_client) {
21033af08d82Slm66018 		ldcp->cb_inprogress = B_TRUE;
21043af08d82Slm66018 		mutex_exit(&ldcp->lock);
21051ae08745Sheppo 		rv = ldcp->cb(notify_event, ldcp->cb_arg);
21061ae08745Sheppo 		if (rv) {
21071ae08745Sheppo 			DWARN(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) callback "
21081ae08745Sheppo 			    "failure", ldcp->id);
21091ae08745Sheppo 		}
21101ae08745Sheppo 		mutex_enter(&ldcp->lock);
21111ae08745Sheppo 		ldcp->cb_inprogress = B_FALSE;
21121ae08745Sheppo 	}
21131ae08745Sheppo 
21141ae08745Sheppo 	mutex_exit(&ldcp->lock);
21151ae08745Sheppo 
21161ae08745Sheppo 	D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) exiting handler", ldcp->id);
21171ae08745Sheppo 
21181ae08745Sheppo 	return (DDI_INTR_CLAIMED);
21191ae08745Sheppo }
21201ae08745Sheppo 
21211ae08745Sheppo /*
212258283286Sha137994  * Process the Rx HV queue.
212358283286Sha137994  *
212458283286Sha137994  * Returns 0 if data packets were found and no errors were encountered,
212558283286Sha137994  * otherwise returns an error. In either case, the *notify argument is
212658283286Sha137994  * set to indicate whether or not the client callback function should
212758283286Sha137994  * be invoked. The *event argument is set to contain the callback event.
212858283286Sha137994  *
212958283286Sha137994  * Depending on the channel mode, packets are handled differently:
213058283286Sha137994  *
213158283286Sha137994  * RAW MODE
213258283286Sha137994  * For raw mode channels, when a data packet is encountered,
213358283286Sha137994  * processing stops and all packets are left on the queue to be removed
213458283286Sha137994  * and processed by the ldc_read code path.
213558283286Sha137994  *
213658283286Sha137994  * UNRELIABLE MODE
213758283286Sha137994  * For unreliable mode, when a data packet is encountered, processing
213858283286Sha137994  * stops, and all packets are left on the queue to be removed and
213958283286Sha137994  * processed by the ldc_read code path. Control packets are processed
214058283286Sha137994  * inline if they are encountered before any data packets.
214158283286Sha137994  *
214220ae46ebSha137994  * RELIABLE MODE
214320ae46ebSha137994  * For reliable mode channels, all packets on the receive queue
214458283286Sha137994  * are processed: data packets are copied to the data queue and
214558283286Sha137994  * control packets are processed inline. Packets are only left on
214658283286Sha137994  * the receive queue when the data queue is full.
21471ae08745Sheppo  */
21481ae08745Sheppo static uint_t
214958283286Sha137994 i_ldc_rx_process_hvq(ldc_chan_t *ldcp, boolean_t *notify_client,
215058283286Sha137994     uint64_t *notify_event)
21511ae08745Sheppo {
21521ae08745Sheppo 	int		rv;
21531ae08745Sheppo 	uint64_t 	rx_head, rx_tail;
21541ae08745Sheppo 	ldc_msg_t 	*msg;
21553af08d82Slm66018 	uint64_t	link_state, first_fragment = 0;
215658283286Sha137994 	boolean_t	trace_length = B_TRUE;
21573af08d82Slm66018 
215858283286Sha137994 	ASSERT(MUTEX_HELD(&ldcp->lock));
215958283286Sha137994 	*notify_client = B_FALSE;
216058283286Sha137994 	*notify_event = 0;
21611ae08745Sheppo 
21621ae08745Sheppo 	/*
21631ae08745Sheppo 	 * Read packet(s) from the queue
21641ae08745Sheppo 	 */
21651ae08745Sheppo 	for (;;) {
21661ae08745Sheppo 
21673af08d82Slm66018 		link_state = ldcp->link_state;
21681ae08745Sheppo 		rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
21691ae08745Sheppo 		    &ldcp->link_state);
21701ae08745Sheppo 		if (rv) {
21711ae08745Sheppo 			cmn_err(CE_WARN,
217258283286Sha137994 			    "i_ldc_rx_process_hvq: (0x%lx) cannot read "
21731ae08745Sheppo 			    "queue ptrs, rv=0x%d\n", ldcp->id, rv);
21741ae08745Sheppo 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
217558283286Sha137994 			return (EIO);
21761ae08745Sheppo 		}
21771ae08745Sheppo 
21781ae08745Sheppo 		/*
21791ae08745Sheppo 		 * reset the channel state if the channel went down
21801ae08745Sheppo 		 * (other side unconfigured queue) or channel was reset
21813af08d82Slm66018 		 * (other side reconfigured its queue)
21821ae08745Sheppo 		 */
21833af08d82Slm66018 
21843af08d82Slm66018 		if (link_state != ldcp->link_state) {
2185cb112a14Slm66018 
21863af08d82Slm66018 			switch (ldcp->link_state) {
21873af08d82Slm66018 			case LDC_CHANNEL_DOWN:
218858283286Sha137994 				D1(ldcp->id, "i_ldc_rx_process_hvq: channel "
21893af08d82Slm66018 				    "link down\n", ldcp->id);
2190d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
21913af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
2192d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
219358283286Sha137994 				*notify_client = B_TRUE;
219458283286Sha137994 				*notify_event = LDC_EVT_DOWN;
21953af08d82Slm66018 				goto loop_exit;
21961ae08745Sheppo 
21973af08d82Slm66018 			case LDC_CHANNEL_UP:
219858283286Sha137994 				D1(ldcp->id, "i_ldc_rx_process_hvq: "
21993af08d82Slm66018 				    "channel link up\n", ldcp->id);
22003af08d82Slm66018 
22013af08d82Slm66018 				if ((ldcp->tstate & ~TS_IN_RESET) == TS_OPEN) {
220258283286Sha137994 					*notify_client = B_TRUE;
220358283286Sha137994 					*notify_event = LDC_EVT_RESET;
22041ae08745Sheppo 					ldcp->tstate |= TS_LINK_READY;
22051ae08745Sheppo 					ldcp->status = LDC_READY;
22061ae08745Sheppo 				}
22073af08d82Slm66018 				break;
22083af08d82Slm66018 
22093af08d82Slm66018 			case LDC_CHANNEL_RESET:
22103af08d82Slm66018 			default:
22113af08d82Slm66018 #ifdef DEBUG
22123af08d82Slm66018 force_reset:
22133af08d82Slm66018 #endif
221458283286Sha137994 				D1(ldcp->id, "i_ldc_rx_process_hvq: channel "
22153af08d82Slm66018 				    "link reset\n", ldcp->id);
22163af08d82Slm66018 				mutex_enter(&ldcp->tx_lock);
22173af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
22183af08d82Slm66018 				mutex_exit(&ldcp->tx_lock);
221958283286Sha137994 				*notify_client = B_TRUE;
222058283286Sha137994 				*notify_event = LDC_EVT_RESET;
22213af08d82Slm66018 				break;
22223af08d82Slm66018 			}
22233af08d82Slm66018 		}
22243af08d82Slm66018 
22253af08d82Slm66018 #ifdef DEBUG
22263af08d82Slm66018 		if (LDC_INJECT_RESET(ldcp))
22273af08d82Slm66018 			goto force_reset;
2228bbfa0259Sha137994 		if (LDC_INJECT_DRNGCLEAR(ldcp))
2229bbfa0259Sha137994 			i_ldc_mem_inject_dring_clear(ldcp);
22303af08d82Slm66018 #endif
223158283286Sha137994 		if (trace_length) {
223258283286Sha137994 			TRACE_RXHVQ_LENGTH(ldcp, rx_head, rx_tail);
223358283286Sha137994 			trace_length = B_FALSE;
223458283286Sha137994 		}
22351ae08745Sheppo 
22361ae08745Sheppo 		if (rx_head == rx_tail) {
223758283286Sha137994 			D2(ldcp->id, "i_ldc_rx_process_hvq: (0x%llx) "
223858283286Sha137994 			    "No packets\n", ldcp->id);
22391ae08745Sheppo 			break;
22401ae08745Sheppo 		}
22413af08d82Slm66018 
224258283286Sha137994 		D2(ldcp->id, "i_ldc_rx_process_hvq: head=0x%llx, "
224358283286Sha137994 		    "tail=0x%llx\n", rx_head, rx_tail);
224458283286Sha137994 		DUMP_LDC_PKT(ldcp, "i_ldc_rx_process_hvq rcd",
22451ae08745Sheppo 		    ldcp->rx_q_va + rx_head);
22461ae08745Sheppo 
22471ae08745Sheppo 		/* get the message */
22481ae08745Sheppo 		msg = (ldc_msg_t *)(ldcp->rx_q_va + rx_head);
22491ae08745Sheppo 
22501ae08745Sheppo 		/* if channel is in RAW mode or data pkt, notify and return */
22511ae08745Sheppo 		if (ldcp->mode == LDC_MODE_RAW) {
225258283286Sha137994 			*notify_client = B_TRUE;
225358283286Sha137994 			*notify_event |= LDC_EVT_READ;
22541ae08745Sheppo 			break;
22551ae08745Sheppo 		}
22561ae08745Sheppo 
22571ae08745Sheppo 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) {
22581ae08745Sheppo 
22591ae08745Sheppo 			/* discard packet if channel is not up */
22603af08d82Slm66018 			if ((ldcp->tstate & ~TS_IN_RESET) != TS_UP) {
22611ae08745Sheppo 
22621ae08745Sheppo 				/* move the head one position */
22631ae08745Sheppo 				rx_head = (rx_head + LDC_PACKET_SIZE) %
22641ae08745Sheppo 				    (ldcp->rx_q_entries << LDC_PACKET_SHIFT);
22651ae08745Sheppo 
22661ae08745Sheppo 				if (rv = i_ldc_set_rx_head(ldcp, rx_head))
22671ae08745Sheppo 					break;
22681ae08745Sheppo 
22691ae08745Sheppo 				continue;
22701ae08745Sheppo 			} else {
227158283286Sha137994 				uint64_t dq_head, dq_tail;
227258283286Sha137994 
227320ae46ebSha137994 				/* process only RELIABLE mode data packets */
227420ae46ebSha137994 				if (ldcp->mode != LDC_MODE_RELIABLE) {
22753af08d82Slm66018 					if ((ldcp->tstate & TS_IN_RESET) == 0)
227658283286Sha137994 						*notify_client = B_TRUE;
227758283286Sha137994 					*notify_event |= LDC_EVT_READ;
22781ae08745Sheppo 					break;
22791ae08745Sheppo 				}
228058283286Sha137994 
228158283286Sha137994 				/* don't process packet if queue full */
228258283286Sha137994 				(void) i_ldc_dq_rx_get_state(ldcp, &dq_head,
228358283286Sha137994 				    &dq_tail, NULL);
228458283286Sha137994 				dq_tail = (dq_tail + LDC_PACKET_SIZE) %
228558283286Sha137994 				    (ldcp->rx_dq_entries << LDC_PACKET_SHIFT);
228658283286Sha137994 				if (dq_tail == dq_head ||
228758283286Sha137994 				    LDC_INJECT_DQFULL(ldcp)) {
228858283286Sha137994 					rv = ENOSPC;
228958283286Sha137994 					break;
229058283286Sha137994 				}
229158283286Sha137994 			}
22921ae08745Sheppo 		}
22931ae08745Sheppo 
22941ae08745Sheppo 		/* Check the sequence ID for the message received */
22953af08d82Slm66018 		rv = i_ldc_check_seqid(ldcp, msg);
22963af08d82Slm66018 		if (rv != 0) {
22971ae08745Sheppo 
229858283286Sha137994 			DWARN(ldcp->id, "i_ldc_rx_process_hvq: (0x%llx) "
229958283286Sha137994 			    "seqid error, q_ptrs=0x%lx,0x%lx", ldcp->id,
230058283286Sha137994 			    rx_head, rx_tail);
23011ae08745Sheppo 
23021ae08745Sheppo 			/* Reset last_msg_rcd to start of message */
2303d10e4ef2Snarayan 			if (first_fragment != 0) {
2304d10e4ef2Snarayan 				ldcp->last_msg_rcd = first_fragment - 1;
2305d10e4ef2Snarayan 				first_fragment = 0;
23061ae08745Sheppo 			}
2307d10e4ef2Snarayan 
23081ae08745Sheppo 			/*
23091ae08745Sheppo 			 * Send a NACK due to seqid mismatch
23101ae08745Sheppo 			 */
231122f747efSnarayan 			rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK,
23121ae08745Sheppo 			    (msg->ctrl & LDC_CTRL_MASK));
23131ae08745Sheppo 
23141ae08745Sheppo 			if (rv) {
231558283286Sha137994 				cmn_err(CE_NOTE, "i_ldc_rx_process_hvq: "
231658283286Sha137994 				    "(0x%lx) err sending CTRL/DATA NACK msg\n",
231758283286Sha137994 				    ldcp->id);
2318d10e4ef2Snarayan 
2319d10e4ef2Snarayan 				/* if cannot send NACK - reset channel */
2320d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
23213af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
2322d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
232383d3bc6fSnarayan 
232458283286Sha137994 				*notify_client = B_TRUE;
232558283286Sha137994 				*notify_event = LDC_EVT_RESET;
2326d10e4ef2Snarayan 				break;
23271ae08745Sheppo 			}
23281ae08745Sheppo 
23291ae08745Sheppo 			/* purge receive queue */
23301ae08745Sheppo 			(void) i_ldc_set_rx_head(ldcp, rx_tail);
23311ae08745Sheppo 			break;
23321ae08745Sheppo 		}
23331ae08745Sheppo 
23341ae08745Sheppo 		/* record the message ID */
23351ae08745Sheppo 		ldcp->last_msg_rcd = msg->seqid;
23361ae08745Sheppo 
23371ae08745Sheppo 		/* process control messages */
23381ae08745Sheppo 		if (msg->type & LDC_CTRL) {
23391ae08745Sheppo 			/* save current internal state */
23401ae08745Sheppo 			uint64_t tstate = ldcp->tstate;
23411ae08745Sheppo 
23421ae08745Sheppo 			rv = i_ldc_ctrlmsg(ldcp, msg);
23431ae08745Sheppo 			if (rv == EAGAIN) {
23441ae08745Sheppo 				/* re-process pkt - state was adjusted */
23451ae08745Sheppo 				continue;
23461ae08745Sheppo 			}
23471ae08745Sheppo 			if (rv == ECONNRESET) {
234858283286Sha137994 				*notify_client = B_TRUE;
234958283286Sha137994 				*notify_event = LDC_EVT_RESET;
23501ae08745Sheppo 				break;
23511ae08745Sheppo 			}
23521ae08745Sheppo 
23531ae08745Sheppo 			/*
23541ae08745Sheppo 			 * control message processing was successful
23551ae08745Sheppo 			 * channel transitioned to ready for communication
23561ae08745Sheppo 			 */
23571ae08745Sheppo 			if (rv == 0 && ldcp->tstate == TS_UP &&
23583af08d82Slm66018 			    (tstate & ~TS_IN_RESET) !=
23593af08d82Slm66018 			    (ldcp->tstate & ~TS_IN_RESET)) {
236058283286Sha137994 				*notify_client = B_TRUE;
236158283286Sha137994 				*notify_event = LDC_EVT_UP;
23621ae08745Sheppo 			}
23631ae08745Sheppo 		}
23641ae08745Sheppo 
236583d3bc6fSnarayan 		/* process data NACKs */
236683d3bc6fSnarayan 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_NACK)) {
236783d3bc6fSnarayan 			DWARN(ldcp->id,
236858283286Sha137994 			    "i_ldc_rx_process_hvq: (0x%llx) received DATA/NACK",
236983d3bc6fSnarayan 			    ldcp->id);
237083d3bc6fSnarayan 			mutex_enter(&ldcp->tx_lock);
237183d3bc6fSnarayan 			i_ldc_reset(ldcp, B_TRUE);
237283d3bc6fSnarayan 			mutex_exit(&ldcp->tx_lock);
237358283286Sha137994 			*notify_client = B_TRUE;
237458283286Sha137994 			*notify_event = LDC_EVT_RESET;
237583d3bc6fSnarayan 			break;
237683d3bc6fSnarayan 		}
237783d3bc6fSnarayan 
23781ae08745Sheppo 		/* process data ACKs */
23791ae08745Sheppo 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) {
2380d10e4ef2Snarayan 			if (rv = i_ldc_process_data_ACK(ldcp, msg)) {
238158283286Sha137994 				*notify_client = B_TRUE;
238258283286Sha137994 				*notify_event = LDC_EVT_RESET;
2383d10e4ef2Snarayan 				break;
2384d10e4ef2Snarayan 			}
23851ae08745Sheppo 		}
23861ae08745Sheppo 
238758283286Sha137994 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) {
238820ae46ebSha137994 			ASSERT(ldcp->mode == LDC_MODE_RELIABLE);
238958283286Sha137994 
239058283286Sha137994 			/*
239158283286Sha137994 			 * Copy the data packet to the data queue. Note
239258283286Sha137994 			 * that the copy routine updates the rx_head pointer.
239358283286Sha137994 			 */
239458283286Sha137994 			i_ldc_rxdq_copy(ldcp, &rx_head);
239558283286Sha137994 
239658283286Sha137994 			if ((ldcp->tstate & TS_IN_RESET) == 0)
239758283286Sha137994 				*notify_client = B_TRUE;
239858283286Sha137994 			*notify_event |= LDC_EVT_READ;
239958283286Sha137994 		} else {
24001ae08745Sheppo 			rx_head = (rx_head + LDC_PACKET_SIZE) %
24011ae08745Sheppo 			    (ldcp->rx_q_entries << LDC_PACKET_SHIFT);
240258283286Sha137994 		}
240358283286Sha137994 
240458283286Sha137994 		/* move the head one position */
24050a55fbb7Slm66018 		if (rv = i_ldc_set_rx_head(ldcp, rx_head)) {
240658283286Sha137994 			*notify_client = B_TRUE;
240758283286Sha137994 			*notify_event = LDC_EVT_RESET;
24081ae08745Sheppo 			break;
24090a55fbb7Slm66018 		}
24101ae08745Sheppo 
24111ae08745Sheppo 	} /* for */
24121ae08745Sheppo 
24133af08d82Slm66018 loop_exit:
24143af08d82Slm66018 
241520ae46ebSha137994 	if (ldcp->mode == LDC_MODE_RELIABLE) {
241658283286Sha137994 		/* ACK data packets */
241758283286Sha137994 		if ((*notify_event &
241858283286Sha137994 		    (LDC_EVT_READ | LDC_EVT_RESET)) == LDC_EVT_READ) {
241958283286Sha137994 			int ack_rv;
242058283286Sha137994 			ack_rv = i_ldc_send_pkt(ldcp, LDC_DATA, LDC_ACK, 0);
242158283286Sha137994 			if (ack_rv && ack_rv != EWOULDBLOCK) {
242258283286Sha137994 				cmn_err(CE_NOTE,
242358283286Sha137994 				    "i_ldc_rx_process_hvq: (0x%lx) cannot "
242458283286Sha137994 				    "send ACK\n", ldcp->id);
242558283286Sha137994 
242658283286Sha137994 				mutex_enter(&ldcp->tx_lock);
242758283286Sha137994 				i_ldc_reset(ldcp, B_FALSE);
242858283286Sha137994 				mutex_exit(&ldcp->tx_lock);
242958283286Sha137994 
243058283286Sha137994 				*notify_client = B_TRUE;
243158283286Sha137994 				*notify_event = LDC_EVT_RESET;
243258283286Sha137994 				goto skip_ackpeek;
243358283286Sha137994 			}
243458283286Sha137994 		}
24351ae08745Sheppo 
24363af08d82Slm66018 		/*
243758283286Sha137994 		 * If we have no more space on the data queue, make sure
243858283286Sha137994 		 * there are no ACKs on the rx queue waiting to be processed.
24393af08d82Slm66018 		 */
244058283286Sha137994 		if (rv == ENOSPC) {
244158283286Sha137994 			if (i_ldc_rx_ackpeek(ldcp, rx_head, rx_tail) != 0) {
244258283286Sha137994 				ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID;
244358283286Sha137994 				*notify_client = B_TRUE;
244458283286Sha137994 				*notify_event = LDC_EVT_RESET;
24451ae08745Sheppo 			}
2446*12f80fa6Sha137994 			return (rv);
244758283286Sha137994 		} else {
244858283286Sha137994 			ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID;
244958283286Sha137994 		}
24501ae08745Sheppo 	}
24511ae08745Sheppo 
245258283286Sha137994 skip_ackpeek:
24534d39be2bSsg70180 
245458283286Sha137994 	/* Return, indicating whether or not data packets were found */
245558283286Sha137994 	if ((*notify_event & (LDC_EVT_READ | LDC_EVT_RESET)) == LDC_EVT_READ)
245658283286Sha137994 		return (0);
245758283286Sha137994 
245858283286Sha137994 	return (ENOMSG);
24591ae08745Sheppo }
24601ae08745Sheppo 
246158283286Sha137994 /*
246258283286Sha137994  * Process any ACK packets on the HV receive queue.
246358283286Sha137994  *
246420ae46ebSha137994  * This function is only used by RELIABLE mode channels when the
246558283286Sha137994  * secondary data queue fills up and there are packets remaining on
246658283286Sha137994  * the HV receive queue.
246758283286Sha137994  */
246858283286Sha137994 int
246958283286Sha137994 i_ldc_rx_ackpeek(ldc_chan_t *ldcp, uint64_t rx_head, uint64_t rx_tail)
247058283286Sha137994 {
247158283286Sha137994 	int		rv = 0;
247258283286Sha137994 	ldc_msg_t	*msg;
247358283286Sha137994 
247458283286Sha137994 	if (ldcp->rx_ack_head == ACKPEEK_HEAD_INVALID)
247558283286Sha137994 		ldcp->rx_ack_head = rx_head;
247658283286Sha137994 
247758283286Sha137994 	while (ldcp->rx_ack_head != rx_tail) {
247858283286Sha137994 		msg = (ldc_msg_t *)(ldcp->rx_q_va + ldcp->rx_ack_head);
247958283286Sha137994 
248058283286Sha137994 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) {
248158283286Sha137994 			if (rv = i_ldc_process_data_ACK(ldcp, msg))
248258283286Sha137994 				break;
248358283286Sha137994 			msg->stype &= ~LDC_ACK;
248458283286Sha137994 		}
248558283286Sha137994 
248658283286Sha137994 		ldcp->rx_ack_head =
248758283286Sha137994 		    (ldcp->rx_ack_head + LDC_PACKET_SIZE) %
248858283286Sha137994 		    (ldcp->rx_q_entries << LDC_PACKET_SHIFT);
248958283286Sha137994 	}
249058283286Sha137994 	return (rv);
249158283286Sha137994 }
24921ae08745Sheppo 
24931ae08745Sheppo /* -------------------------------------------------------------------------- */
24941ae08745Sheppo 
24951ae08745Sheppo /*
24961ae08745Sheppo  * LDC API functions
24971ae08745Sheppo  */
24981ae08745Sheppo 
24991ae08745Sheppo /*
25001ae08745Sheppo  * Initialize the channel. Allocate internal structure and memory for
25011ae08745Sheppo  * TX/RX queues, and initialize locks.
25021ae08745Sheppo  */
25031ae08745Sheppo int
25041ae08745Sheppo ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle)
25051ae08745Sheppo {
25061ae08745Sheppo 	ldc_chan_t 	*ldcp;
25071ae08745Sheppo 	int		rv, exit_val;
25081ae08745Sheppo 	uint64_t	ra_base, nentries;
2509e1ebb9ecSlm66018 	uint64_t	qlen;
25101ae08745Sheppo 
25111ae08745Sheppo 	exit_val = EINVAL;	/* guarantee an error if exit on failure */
25121ae08745Sheppo 
25131ae08745Sheppo 	if (attr == NULL) {
25141ae08745Sheppo 		DWARN(id, "ldc_init: (0x%llx) invalid attr\n", id);
25151ae08745Sheppo 		return (EINVAL);
25161ae08745Sheppo 	}
25171ae08745Sheppo 	if (handle == NULL) {
25181ae08745Sheppo 		DWARN(id, "ldc_init: (0x%llx) invalid handle\n", id);
25191ae08745Sheppo 		return (EINVAL);
25201ae08745Sheppo 	}
25211ae08745Sheppo 
25221ae08745Sheppo 	/* check if channel is valid */
25231ae08745Sheppo 	rv = hv_ldc_tx_qinfo(id, &ra_base, &nentries);
25241ae08745Sheppo 	if (rv == H_ECHANNEL) {
25251ae08745Sheppo 		DWARN(id, "ldc_init: (0x%llx) invalid channel id\n", id);
25261ae08745Sheppo 		return (EINVAL);
25271ae08745Sheppo 	}
25281ae08745Sheppo 
25291ae08745Sheppo 	/* check if the channel has already been initialized */
25301ae08745Sheppo 	mutex_enter(&ldcssp->lock);
25311ae08745Sheppo 	ldcp = ldcssp->chan_list;
25321ae08745Sheppo 	while (ldcp != NULL) {
25331ae08745Sheppo 		if (ldcp->id == id) {
25341ae08745Sheppo 			DWARN(id, "ldc_init: (0x%llx) already initialized\n",
25351ae08745Sheppo 			    id);
25361ae08745Sheppo 			mutex_exit(&ldcssp->lock);
25371ae08745Sheppo 			return (EADDRINUSE);
25381ae08745Sheppo 		}
25391ae08745Sheppo 		ldcp = ldcp->next;
25401ae08745Sheppo 	}
25411ae08745Sheppo 	mutex_exit(&ldcssp->lock);
25421ae08745Sheppo 
25431ae08745Sheppo 	ASSERT(ldcp == NULL);
25441ae08745Sheppo 
25451ae08745Sheppo 	*handle = 0;
25461ae08745Sheppo 
25471ae08745Sheppo 	/* Allocate an ldcp structure */
25481ae08745Sheppo 	ldcp = kmem_zalloc(sizeof (ldc_chan_t), KM_SLEEP);
25491ae08745Sheppo 
2550d10e4ef2Snarayan 	/*
2551d10e4ef2Snarayan 	 * Initialize the channel and Tx lock
2552d10e4ef2Snarayan 	 *
2553d10e4ef2Snarayan 	 * The channel 'lock' protects the entire channel and
2554d10e4ef2Snarayan 	 * should be acquired before initializing, resetting,
2555d10e4ef2Snarayan 	 * destroying or reading from a channel.
2556d10e4ef2Snarayan 	 *
2557d10e4ef2Snarayan 	 * The 'tx_lock' should be acquired prior to transmitting
2558d10e4ef2Snarayan 	 * data over the channel. The lock should also be acquired
2559d10e4ef2Snarayan 	 * prior to channel reconfiguration (in order to prevent
2560d10e4ef2Snarayan 	 * concurrent writes).
2561d10e4ef2Snarayan 	 *
2562d10e4ef2Snarayan 	 * ORDERING: When both locks are being acquired, to prevent
2563d10e4ef2Snarayan 	 * deadlocks, the channel lock should be always acquired prior
2564d10e4ef2Snarayan 	 * to the tx_lock.
2565d10e4ef2Snarayan 	 */
25661ae08745Sheppo 	mutex_init(&ldcp->lock, NULL, MUTEX_DRIVER, NULL);
2567d10e4ef2Snarayan 	mutex_init(&ldcp->tx_lock, NULL, MUTEX_DRIVER, NULL);
25681ae08745Sheppo 
25691ae08745Sheppo 	/* Initialize the channel */
25701ae08745Sheppo 	ldcp->id = id;
25711ae08745Sheppo 	ldcp->cb = NULL;
25721ae08745Sheppo 	ldcp->cb_arg = NULL;
25731ae08745Sheppo 	ldcp->cb_inprogress = B_FALSE;
25741ae08745Sheppo 	ldcp->cb_enabled = B_FALSE;
25751ae08745Sheppo 	ldcp->next = NULL;
25761ae08745Sheppo 
25771ae08745Sheppo 	/* Read attributes */
25781ae08745Sheppo 	ldcp->mode = attr->mode;
25791ae08745Sheppo 	ldcp->devclass = attr->devclass;
25801ae08745Sheppo 	ldcp->devinst = attr->instance;
2581e1ebb9ecSlm66018 	ldcp->mtu = (attr->mtu > 0) ? attr->mtu : LDC_DEFAULT_MTU;
25821ae08745Sheppo 
25831ae08745Sheppo 	D1(ldcp->id,
25841ae08745Sheppo 	    "ldc_init: (0x%llx) channel attributes, class=0x%x, "
2585e1ebb9ecSlm66018 	    "instance=0x%llx, mode=%d, mtu=%d\n",
2586e1ebb9ecSlm66018 	    ldcp->id, ldcp->devclass, ldcp->devinst, ldcp->mode, ldcp->mtu);
25871ae08745Sheppo 
25881ae08745Sheppo 	ldcp->next_vidx = 0;
25893af08d82Slm66018 	ldcp->tstate = TS_IN_RESET;
25901ae08745Sheppo 	ldcp->hstate = 0;
25911ae08745Sheppo 	ldcp->last_msg_snt = LDC_INIT_SEQID;
25921ae08745Sheppo 	ldcp->last_ack_rcd = 0;
25931ae08745Sheppo 	ldcp->last_msg_rcd = 0;
259458283286Sha137994 	ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID;
25951ae08745Sheppo 
25961ae08745Sheppo 	ldcp->stream_bufferp = NULL;
25971ae08745Sheppo 	ldcp->exp_dring_list = NULL;
25981ae08745Sheppo 	ldcp->imp_dring_list = NULL;
25991ae08745Sheppo 	ldcp->mhdl_list = NULL;
26001ae08745Sheppo 
26013af08d82Slm66018 	ldcp->tx_intr_state = LDC_INTR_NONE;
26023af08d82Slm66018 	ldcp->rx_intr_state = LDC_INTR_NONE;
26033af08d82Slm66018 
26041ae08745Sheppo 	/* Initialize payload size depending on whether channel is reliable */
26051ae08745Sheppo 	switch (ldcp->mode) {
26061ae08745Sheppo 	case LDC_MODE_RAW:
26071ae08745Sheppo 		ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RAW;
26081ae08745Sheppo 		ldcp->read_p = i_ldc_read_raw;
26091ae08745Sheppo 		ldcp->write_p = i_ldc_write_raw;
26101ae08745Sheppo 		break;
26111ae08745Sheppo 	case LDC_MODE_UNRELIABLE:
26121ae08745Sheppo 		ldcp->pkt_payload = LDC_PAYLOAD_SIZE_UNRELIABLE;
26131ae08745Sheppo 		ldcp->read_p = i_ldc_read_packet;
26141ae08745Sheppo 		ldcp->write_p = i_ldc_write_packet;
26151ae08745Sheppo 		break;
26161ae08745Sheppo 	case LDC_MODE_RELIABLE:
26171ae08745Sheppo 		ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE;
26181ae08745Sheppo 
26191ae08745Sheppo 		ldcp->stream_remains = 0;
26201ae08745Sheppo 		ldcp->stream_offset = 0;
26211ae08745Sheppo 		ldcp->stream_bufferp = kmem_alloc(ldcp->mtu, KM_SLEEP);
26221ae08745Sheppo 		ldcp->read_p = i_ldc_read_stream;
26231ae08745Sheppo 		ldcp->write_p = i_ldc_write_stream;
26241ae08745Sheppo 		break;
26251ae08745Sheppo 	default:
26261ae08745Sheppo 		exit_val = EINVAL;
26271ae08745Sheppo 		goto cleanup_on_exit;
26281ae08745Sheppo 	}
26291ae08745Sheppo 
2630e1ebb9ecSlm66018 	/*
2631e1ebb9ecSlm66018 	 * qlen is (mtu * ldc_mtu_msgs) / pkt_payload. If this
2632e1ebb9ecSlm66018 	 * value is smaller than default length of ldc_queue_entries,
263322f747efSnarayan 	 * qlen is set to ldc_queue_entries. Ensure that computed
263422f747efSnarayan 	 * length is a power-of-two value.
2635e1ebb9ecSlm66018 	 */
2636e1ebb9ecSlm66018 	qlen = (ldcp->mtu * ldc_mtu_msgs) / ldcp->pkt_payload;
263722f747efSnarayan 	if (!ISP2(qlen)) {
263822f747efSnarayan 		uint64_t	tmp = 1;
263922f747efSnarayan 		while (qlen) {
264022f747efSnarayan 			qlen >>= 1; tmp <<= 1;
264122f747efSnarayan 		}
264222f747efSnarayan 		qlen = tmp;
264322f747efSnarayan 	}
264422f747efSnarayan 
2645e1ebb9ecSlm66018 	ldcp->rx_q_entries =
2646e1ebb9ecSlm66018 	    (qlen < ldc_queue_entries) ? ldc_queue_entries : qlen;
2647e1ebb9ecSlm66018 	ldcp->tx_q_entries = ldcp->rx_q_entries;
2648e1ebb9ecSlm66018 
264922f747efSnarayan 	D1(ldcp->id, "ldc_init: queue length = 0x%llx\n", ldcp->rx_q_entries);
2650e1ebb9ecSlm66018 
26511ae08745Sheppo 	/* Create a transmit queue */
26521ae08745Sheppo 	ldcp->tx_q_va = (uint64_t)
26531ae08745Sheppo 	    contig_mem_alloc(ldcp->tx_q_entries << LDC_PACKET_SHIFT);
26541ae08745Sheppo 	if (ldcp->tx_q_va == NULL) {
26551ae08745Sheppo 		cmn_err(CE_WARN,
26561ae08745Sheppo 		    "ldc_init: (0x%lx) TX queue allocation failed\n",
26571ae08745Sheppo 		    ldcp->id);
26581ae08745Sheppo 		exit_val = ENOMEM;
26591ae08745Sheppo 		goto cleanup_on_exit;
26601ae08745Sheppo 	}
26611ae08745Sheppo 	ldcp->tx_q_ra = va_to_pa((caddr_t)ldcp->tx_q_va);
26621ae08745Sheppo 
26631ae08745Sheppo 	D2(ldcp->id, "ldc_init: txq_va=0x%llx, txq_ra=0x%llx, entries=0x%llx\n",
26641ae08745Sheppo 	    ldcp->tx_q_va, ldcp->tx_q_ra, ldcp->tx_q_entries);
26651ae08745Sheppo 
26661ae08745Sheppo 	ldcp->tstate |= TS_TXQ_RDY;
26671ae08745Sheppo 
26681ae08745Sheppo 	/* Create a receive queue */
26691ae08745Sheppo 	ldcp->rx_q_va = (uint64_t)
26701ae08745Sheppo 	    contig_mem_alloc(ldcp->rx_q_entries << LDC_PACKET_SHIFT);
26711ae08745Sheppo 	if (ldcp->rx_q_va == NULL) {
26721ae08745Sheppo 		cmn_err(CE_WARN,
26731ae08745Sheppo 		    "ldc_init: (0x%lx) RX queue allocation failed\n",
26741ae08745Sheppo 		    ldcp->id);
26751ae08745Sheppo 		exit_val = ENOMEM;
26761ae08745Sheppo 		goto cleanup_on_exit;
26771ae08745Sheppo 	}
26781ae08745Sheppo 	ldcp->rx_q_ra = va_to_pa((caddr_t)ldcp->rx_q_va);
26791ae08745Sheppo 
26801ae08745Sheppo 	D2(ldcp->id, "ldc_init: rxq_va=0x%llx, rxq_ra=0x%llx, entries=0x%llx\n",
26811ae08745Sheppo 	    ldcp->rx_q_va, ldcp->rx_q_ra, ldcp->rx_q_entries);
26821ae08745Sheppo 
26831ae08745Sheppo 	ldcp->tstate |= TS_RXQ_RDY;
26841ae08745Sheppo 
268558283286Sha137994 	/* Setup a separate read data queue */
268620ae46ebSha137994 	if (ldcp->mode == LDC_MODE_RELIABLE) {
268758283286Sha137994 		ldcp->readq_get_state = i_ldc_dq_rx_get_state;
268858283286Sha137994 		ldcp->readq_set_head  = i_ldc_set_rxdq_head;
268958283286Sha137994 
269058283286Sha137994 		/* Make sure the data queue multiplier is a power of 2 */
269158283286Sha137994 		if (!ISP2(ldc_rxdq_multiplier)) {
269258283286Sha137994 			D1(ldcp->id, "ldc_init: (0x%llx) ldc_rxdq_multiplier "
269358283286Sha137994 			    "not a power of 2, resetting", ldcp->id);
269458283286Sha137994 			ldc_rxdq_multiplier = LDC_RXDQ_MULTIPLIER;
269558283286Sha137994 		}
269658283286Sha137994 
269758283286Sha137994 		ldcp->rx_dq_entries = ldc_rxdq_multiplier * ldcp->rx_q_entries;
269858283286Sha137994 		ldcp->rx_dq_va = (uint64_t)
269958283286Sha137994 		    kmem_alloc(ldcp->rx_dq_entries << LDC_PACKET_SHIFT,
270058283286Sha137994 		    KM_SLEEP);
270158283286Sha137994 		if (ldcp->rx_dq_va == NULL) {
270258283286Sha137994 			cmn_err(CE_WARN,
270358283286Sha137994 			    "ldc_init: (0x%lx) RX data queue "
270458283286Sha137994 			    "allocation failed\n", ldcp->id);
270558283286Sha137994 			exit_val = ENOMEM;
270658283286Sha137994 			goto cleanup_on_exit;
270758283286Sha137994 		}
270858283286Sha137994 
270958283286Sha137994 		ldcp->rx_dq_head = ldcp->rx_dq_tail = 0;
271058283286Sha137994 
271158283286Sha137994 		D2(ldcp->id, "ldc_init: rx_dq_va=0x%llx, "
271258283286Sha137994 		    "rx_dq_entries=0x%llx\n", ldcp->rx_dq_va,
271358283286Sha137994 		    ldcp->rx_dq_entries);
271458283286Sha137994 	} else {
271558283286Sha137994 		ldcp->readq_get_state = i_ldc_hvq_rx_get_state;
271658283286Sha137994 		ldcp->readq_set_head  = i_ldc_set_rx_head;
271758283286Sha137994 	}
271858283286Sha137994 
27191ae08745Sheppo 	/* Init descriptor ring and memory handle list lock */
27201ae08745Sheppo 	mutex_init(&ldcp->exp_dlist_lock, NULL, MUTEX_DRIVER, NULL);
27211ae08745Sheppo 	mutex_init(&ldcp->imp_dlist_lock, NULL, MUTEX_DRIVER, NULL);
27221ae08745Sheppo 	mutex_init(&ldcp->mlist_lock, NULL, MUTEX_DRIVER, NULL);
27231ae08745Sheppo 
27241ae08745Sheppo 	/* mark status as INITialized */
27251ae08745Sheppo 	ldcp->status = LDC_INIT;
27261ae08745Sheppo 
27271ae08745Sheppo 	/* Add to channel list */
27281ae08745Sheppo 	mutex_enter(&ldcssp->lock);
27291ae08745Sheppo 	ldcp->next = ldcssp->chan_list;
27301ae08745Sheppo 	ldcssp->chan_list = ldcp;
27311ae08745Sheppo 	ldcssp->channel_count++;
27321ae08745Sheppo 	mutex_exit(&ldcssp->lock);
27331ae08745Sheppo 
27341ae08745Sheppo 	/* set the handle */
27351ae08745Sheppo 	*handle = (ldc_handle_t)ldcp;
27361ae08745Sheppo 
27371ae08745Sheppo 	D1(ldcp->id, "ldc_init: (0x%llx) channel initialized\n", ldcp->id);
27381ae08745Sheppo 
27391ae08745Sheppo 	return (0);
27401ae08745Sheppo 
27411ae08745Sheppo cleanup_on_exit:
27421ae08745Sheppo 
274320ae46ebSha137994 	if (ldcp->mode == LDC_MODE_RELIABLE && ldcp->stream_bufferp)
27441ae08745Sheppo 		kmem_free(ldcp->stream_bufferp, ldcp->mtu);
27451ae08745Sheppo 
27461ae08745Sheppo 	if (ldcp->tstate & TS_TXQ_RDY)
27471ae08745Sheppo 		contig_mem_free((caddr_t)ldcp->tx_q_va,
27481ae08745Sheppo 		    (ldcp->tx_q_entries << LDC_PACKET_SHIFT));
27491ae08745Sheppo 
27501ae08745Sheppo 	if (ldcp->tstate & TS_RXQ_RDY)
27511ae08745Sheppo 		contig_mem_free((caddr_t)ldcp->rx_q_va,
27521ae08745Sheppo 		    (ldcp->rx_q_entries << LDC_PACKET_SHIFT));
27531ae08745Sheppo 
2754d10e4ef2Snarayan 	mutex_destroy(&ldcp->tx_lock);
27551ae08745Sheppo 	mutex_destroy(&ldcp->lock);
27561ae08745Sheppo 
27571ae08745Sheppo 	if (ldcp)
27581ae08745Sheppo 		kmem_free(ldcp, sizeof (ldc_chan_t));
27591ae08745Sheppo 
27601ae08745Sheppo 	return (exit_val);
27611ae08745Sheppo }
27621ae08745Sheppo 
27631ae08745Sheppo /*
27641ae08745Sheppo  * Finalizes the LDC connection. It will return EBUSY if the
27651ae08745Sheppo  * channel is open. A ldc_close() has to be done prior to
27661ae08745Sheppo  * a ldc_fini operation. It frees TX/RX queues, associated
27671ae08745Sheppo  * with the channel
27681ae08745Sheppo  */
27691ae08745Sheppo int
27701ae08745Sheppo ldc_fini(ldc_handle_t handle)
27711ae08745Sheppo {
27721ae08745Sheppo 	ldc_chan_t 	*ldcp;
27731ae08745Sheppo 	ldc_chan_t 	*tmp_ldcp;
27741ae08745Sheppo 	uint64_t 	id;
27751ae08745Sheppo 
27761ae08745Sheppo 	if (handle == NULL) {
27771ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel handle\n");
27781ae08745Sheppo 		return (EINVAL);
27791ae08745Sheppo 	}
27801ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
27811ae08745Sheppo 	id = ldcp->id;
27821ae08745Sheppo 
27831ae08745Sheppo 	mutex_enter(&ldcp->lock);
27841ae08745Sheppo 
27853af08d82Slm66018 	if ((ldcp->tstate & ~TS_IN_RESET) > TS_INIT) {
27861ae08745Sheppo 		DWARN(ldcp->id, "ldc_fini: (0x%llx) channel is open\n",
27871ae08745Sheppo 		    ldcp->id);
27881ae08745Sheppo 		mutex_exit(&ldcp->lock);
27891ae08745Sheppo 		return (EBUSY);
27901ae08745Sheppo 	}
27911ae08745Sheppo 
27921ae08745Sheppo 	/* Remove from the channel list */
27931ae08745Sheppo 	mutex_enter(&ldcssp->lock);
27941ae08745Sheppo 	tmp_ldcp = ldcssp->chan_list;
27951ae08745Sheppo 	if (tmp_ldcp == ldcp) {
27961ae08745Sheppo 		ldcssp->chan_list = ldcp->next;
27971ae08745Sheppo 		ldcp->next = NULL;
27981ae08745Sheppo 	} else {
27991ae08745Sheppo 		while (tmp_ldcp != NULL) {
28001ae08745Sheppo 			if (tmp_ldcp->next == ldcp) {
28011ae08745Sheppo 				tmp_ldcp->next = ldcp->next;
28021ae08745Sheppo 				ldcp->next = NULL;
28031ae08745Sheppo 				break;
28041ae08745Sheppo 			}
28051ae08745Sheppo 			tmp_ldcp = tmp_ldcp->next;
28061ae08745Sheppo 		}
28071ae08745Sheppo 		if (tmp_ldcp == NULL) {
28081ae08745Sheppo 			DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel hdl\n");
28091ae08745Sheppo 			mutex_exit(&ldcssp->lock);
28101ae08745Sheppo 			mutex_exit(&ldcp->lock);
28111ae08745Sheppo 			return (EINVAL);
28121ae08745Sheppo 		}
28131ae08745Sheppo 	}
28141ae08745Sheppo 
28151ae08745Sheppo 	ldcssp->channel_count--;
28161ae08745Sheppo 
28171ae08745Sheppo 	mutex_exit(&ldcssp->lock);
28181ae08745Sheppo 
28191ae08745Sheppo 	/* Free the map table for this channel */
28201ae08745Sheppo 	if (ldcp->mtbl) {
28211ae08745Sheppo 		(void) hv_ldc_set_map_table(ldcp->id, NULL, NULL);
28223af08d82Slm66018 		if (ldcp->mtbl->contigmem)
28231ae08745Sheppo 			contig_mem_free(ldcp->mtbl->table, ldcp->mtbl->size);
28243af08d82Slm66018 		else
28253af08d82Slm66018 			kmem_free(ldcp->mtbl->table, ldcp->mtbl->size);
28261ae08745Sheppo 		mutex_destroy(&ldcp->mtbl->lock);
28271ae08745Sheppo 		kmem_free(ldcp->mtbl, sizeof (ldc_mtbl_t));
28281ae08745Sheppo 	}
28291ae08745Sheppo 
28301ae08745Sheppo 	/* Destroy descriptor ring and memory handle list lock */
28311ae08745Sheppo 	mutex_destroy(&ldcp->exp_dlist_lock);
28321ae08745Sheppo 	mutex_destroy(&ldcp->imp_dlist_lock);
28331ae08745Sheppo 	mutex_destroy(&ldcp->mlist_lock);
28341ae08745Sheppo 
283520ae46ebSha137994 	/* Free the stream buffer for RELIABLE_MODE */
283620ae46ebSha137994 	if (ldcp->mode == LDC_MODE_RELIABLE && ldcp->stream_bufferp)
28371ae08745Sheppo 		kmem_free(ldcp->stream_bufferp, ldcp->mtu);
28381ae08745Sheppo 
28391ae08745Sheppo 	/* Free the RX queue */
28401ae08745Sheppo 	contig_mem_free((caddr_t)ldcp->rx_q_va,
28411ae08745Sheppo 	    (ldcp->rx_q_entries << LDC_PACKET_SHIFT));
28421ae08745Sheppo 	ldcp->tstate &= ~TS_RXQ_RDY;
28431ae08745Sheppo 
284458283286Sha137994 	/* Free the RX data queue */
284520ae46ebSha137994 	if (ldcp->mode == LDC_MODE_RELIABLE) {
284658283286Sha137994 		kmem_free((caddr_t)ldcp->rx_dq_va,
284758283286Sha137994 		    (ldcp->rx_dq_entries << LDC_PACKET_SHIFT));
284858283286Sha137994 	}
284958283286Sha137994 
28501ae08745Sheppo 	/* Free the TX queue */
28511ae08745Sheppo 	contig_mem_free((caddr_t)ldcp->tx_q_va,
28521ae08745Sheppo 	    (ldcp->tx_q_entries << LDC_PACKET_SHIFT));
28531ae08745Sheppo 	ldcp->tstate &= ~TS_TXQ_RDY;
28541ae08745Sheppo 
28551ae08745Sheppo 	mutex_exit(&ldcp->lock);
28561ae08745Sheppo 
28571ae08745Sheppo 	/* Destroy mutex */
2858d10e4ef2Snarayan 	mutex_destroy(&ldcp->tx_lock);
28591ae08745Sheppo 	mutex_destroy(&ldcp->lock);
28601ae08745Sheppo 
28611ae08745Sheppo 	/* free channel structure */
28621ae08745Sheppo 	kmem_free(ldcp, sizeof (ldc_chan_t));
28631ae08745Sheppo 
28641ae08745Sheppo 	D1(id, "ldc_fini: (0x%llx) channel finalized\n", id);
28651ae08745Sheppo 
28661ae08745Sheppo 	return (0);
28671ae08745Sheppo }
28681ae08745Sheppo 
28691ae08745Sheppo /*
28701ae08745Sheppo  * Open the LDC channel for use. It registers the TX/RX queues
28711ae08745Sheppo  * with the Hypervisor. It also specifies the interrupt number
28721ae08745Sheppo  * and target CPU for this channel
28731ae08745Sheppo  */
28741ae08745Sheppo int
28751ae08745Sheppo ldc_open(ldc_handle_t handle)
28761ae08745Sheppo {
28771ae08745Sheppo 	ldc_chan_t 	*ldcp;
28781ae08745Sheppo 	int 		rv;
28791ae08745Sheppo 
28801ae08745Sheppo 	if (handle == NULL) {
28811ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_open: invalid channel handle\n");
28821ae08745Sheppo 		return (EINVAL);
28831ae08745Sheppo 	}
28841ae08745Sheppo 
28851ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
28861ae08745Sheppo 
28871ae08745Sheppo 	mutex_enter(&ldcp->lock);
28881ae08745Sheppo 
28891ae08745Sheppo 	if (ldcp->tstate < TS_INIT) {
28901ae08745Sheppo 		DWARN(ldcp->id,
28911ae08745Sheppo 		    "ldc_open: (0x%llx) channel not initialized\n", ldcp->id);
28921ae08745Sheppo 		mutex_exit(&ldcp->lock);
28931ae08745Sheppo 		return (EFAULT);
28941ae08745Sheppo 	}
28953af08d82Slm66018 	if ((ldcp->tstate & ~TS_IN_RESET) >= TS_OPEN) {
28961ae08745Sheppo 		DWARN(ldcp->id,
28971ae08745Sheppo 		    "ldc_open: (0x%llx) channel is already open\n", ldcp->id);
28981ae08745Sheppo 		mutex_exit(&ldcp->lock);
28991ae08745Sheppo 		return (EFAULT);
29001ae08745Sheppo 	}
29011ae08745Sheppo 
29021ae08745Sheppo 	/*
29031ae08745Sheppo 	 * Unregister/Register the tx queue with the hypervisor
29041ae08745Sheppo 	 */
29051ae08745Sheppo 	rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL);
29061ae08745Sheppo 	if (rv) {
29071ae08745Sheppo 		cmn_err(CE_WARN,
29081ae08745Sheppo 		    "ldc_open: (0x%lx) channel tx queue unconf failed\n",
29091ae08745Sheppo 		    ldcp->id);
29101ae08745Sheppo 		mutex_exit(&ldcp->lock);
29111ae08745Sheppo 		return (EIO);
29121ae08745Sheppo 	}
29131ae08745Sheppo 
29141ae08745Sheppo 	rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries);
29151ae08745Sheppo 	if (rv) {
29161ae08745Sheppo 		cmn_err(CE_WARN,
29171ae08745Sheppo 		    "ldc_open: (0x%lx) channel tx queue conf failed\n",
29181ae08745Sheppo 		    ldcp->id);
29191ae08745Sheppo 		mutex_exit(&ldcp->lock);
29201ae08745Sheppo 		return (EIO);
29211ae08745Sheppo 	}
29221ae08745Sheppo 
29231ae08745Sheppo 	D2(ldcp->id, "ldc_open: (0x%llx) registered tx queue with LDC\n",
29241ae08745Sheppo 	    ldcp->id);
29251ae08745Sheppo 
29261ae08745Sheppo 	/*
29271ae08745Sheppo 	 * Unregister/Register the rx queue with the hypervisor
29281ae08745Sheppo 	 */
29291ae08745Sheppo 	rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL);
29301ae08745Sheppo 	if (rv) {
29311ae08745Sheppo 		cmn_err(CE_WARN,
29321ae08745Sheppo 		    "ldc_open: (0x%lx) channel rx queue unconf failed\n",
29331ae08745Sheppo 		    ldcp->id);
29341ae08745Sheppo 		mutex_exit(&ldcp->lock);
29351ae08745Sheppo 		return (EIO);
29361ae08745Sheppo 	}
29371ae08745Sheppo 
29381ae08745Sheppo 	rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, ldcp->rx_q_entries);
29391ae08745Sheppo 	if (rv) {
29401ae08745Sheppo 		cmn_err(CE_WARN,
29411ae08745Sheppo 		    "ldc_open: (0x%lx) channel rx queue conf failed\n",
29421ae08745Sheppo 		    ldcp->id);
29431ae08745Sheppo 		mutex_exit(&ldcp->lock);
29441ae08745Sheppo 		return (EIO);
29451ae08745Sheppo 	}
29461ae08745Sheppo 
29471ae08745Sheppo 	D2(ldcp->id, "ldc_open: (0x%llx) registered rx queue with LDC\n",
29481ae08745Sheppo 	    ldcp->id);
29491ae08745Sheppo 
29501ae08745Sheppo 	ldcp->tstate |= TS_QCONF_RDY;
29511ae08745Sheppo 
29521ae08745Sheppo 	/* Register the channel with the channel nexus */
29531ae08745Sheppo 	rv = i_ldc_register_channel(ldcp);
29541ae08745Sheppo 	if (rv && rv != EAGAIN) {
29551ae08745Sheppo 		cmn_err(CE_WARN,
29561ae08745Sheppo 		    "ldc_open: (0x%lx) channel register failed\n", ldcp->id);
29571ae08745Sheppo 		(void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL);
29581ae08745Sheppo 		(void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL);
29591ae08745Sheppo 		mutex_exit(&ldcp->lock);
29601ae08745Sheppo 		return (EIO);
29611ae08745Sheppo 	}
29621ae08745Sheppo 
29631ae08745Sheppo 	/* mark channel in OPEN state */
29641ae08745Sheppo 	ldcp->status = LDC_OPEN;
29651ae08745Sheppo 
29661ae08745Sheppo 	/* Read channel state */
29671ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
29681ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
29691ae08745Sheppo 	if (rv) {
29701ae08745Sheppo 		cmn_err(CE_WARN,
29711ae08745Sheppo 		    "ldc_open: (0x%lx) cannot read channel state\n",
29721ae08745Sheppo 		    ldcp->id);
29731ae08745Sheppo 		(void) i_ldc_unregister_channel(ldcp);
29741ae08745Sheppo 		(void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL);
29751ae08745Sheppo 		(void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL);
29761ae08745Sheppo 		mutex_exit(&ldcp->lock);
29771ae08745Sheppo 		return (EIO);
29781ae08745Sheppo 	}
29791ae08745Sheppo 
29801ae08745Sheppo 	/*
298120ae46ebSha137994 	 * set the ACKd head to current head location for reliable
29821ae08745Sheppo 	 */
29831ae08745Sheppo 	ldcp->tx_ackd_head = ldcp->tx_head;
29841ae08745Sheppo 
29851ae08745Sheppo 	/* mark channel ready if HV report link is UP (peer alloc'd Rx queue) */
29861ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_UP ||
29871ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
29881ae08745Sheppo 		ldcp->tstate |= TS_LINK_READY;
29891ae08745Sheppo 		ldcp->status = LDC_READY;
29901ae08745Sheppo 	}
29911ae08745Sheppo 
29921ae08745Sheppo 	/*
29931ae08745Sheppo 	 * if channel is being opened in RAW mode - no handshake is needed
29941ae08745Sheppo 	 * switch the channel READY and UP state
29951ae08745Sheppo 	 */
29961ae08745Sheppo 	if (ldcp->mode == LDC_MODE_RAW) {
29971ae08745Sheppo 		ldcp->tstate = TS_UP;	/* set bits associated with LDC UP */
29981ae08745Sheppo 		ldcp->status = LDC_UP;
29991ae08745Sheppo 	}
30001ae08745Sheppo 
30011ae08745Sheppo 	mutex_exit(&ldcp->lock);
30021ae08745Sheppo 
30031ae08745Sheppo 	/*
30041ae08745Sheppo 	 * Increment number of open channels
30051ae08745Sheppo 	 */
30061ae08745Sheppo 	mutex_enter(&ldcssp->lock);
30071ae08745Sheppo 	ldcssp->channels_open++;
30081ae08745Sheppo 	mutex_exit(&ldcssp->lock);
30091ae08745Sheppo 
3010cb112a14Slm66018 	D1(ldcp->id,
30113af08d82Slm66018 	    "ldc_open: (0x%llx) channel (0x%p) open for use "
30123af08d82Slm66018 	    "(tstate=0x%x, status=0x%x)\n",
30133af08d82Slm66018 	    ldcp->id, ldcp, ldcp->tstate, ldcp->status);
30141ae08745Sheppo 
30151ae08745Sheppo 	return (0);
30161ae08745Sheppo }
30171ae08745Sheppo 
30181ae08745Sheppo /*
30191ae08745Sheppo  * Close the LDC connection. It will return EBUSY if there
30201ae08745Sheppo  * are memory segments or descriptor rings either bound to or
30211ae08745Sheppo  * mapped over the channel
30221ae08745Sheppo  */
30231ae08745Sheppo int
30241ae08745Sheppo ldc_close(ldc_handle_t handle)
30251ae08745Sheppo {
30261ae08745Sheppo 	ldc_chan_t 	*ldcp;
3027d10e4ef2Snarayan 	int		rv = 0, retries = 0;
30281ae08745Sheppo 	boolean_t	chk_done = B_FALSE;
30291ae08745Sheppo 
30301ae08745Sheppo 	if (handle == NULL) {
30311ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_close: invalid channel handle\n");
30321ae08745Sheppo 		return (EINVAL);
30331ae08745Sheppo 	}
30341ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
30351ae08745Sheppo 
30361ae08745Sheppo 	mutex_enter(&ldcp->lock);
30371ae08745Sheppo 
30381ae08745Sheppo 	/* return error if channel is not open */
30393af08d82Slm66018 	if ((ldcp->tstate & ~TS_IN_RESET) < TS_OPEN) {
30401ae08745Sheppo 		DWARN(ldcp->id,
30411ae08745Sheppo 		    "ldc_close: (0x%llx) channel is not open\n", ldcp->id);
30421ae08745Sheppo 		mutex_exit(&ldcp->lock);
30431ae08745Sheppo 		return (EFAULT);
30441ae08745Sheppo 	}
30451ae08745Sheppo 
30461ae08745Sheppo 	/* if any memory handles, drings, are bound or mapped cannot close */
30471ae08745Sheppo 	if (ldcp->mhdl_list != NULL) {
30481ae08745Sheppo 		DWARN(ldcp->id,
30491ae08745Sheppo 		    "ldc_close: (0x%llx) channel has bound memory handles\n",
30501ae08745Sheppo 		    ldcp->id);
30511ae08745Sheppo 		mutex_exit(&ldcp->lock);
30521ae08745Sheppo 		return (EBUSY);
30531ae08745Sheppo 	}
30541ae08745Sheppo 	if (ldcp->exp_dring_list != NULL) {
30551ae08745Sheppo 		DWARN(ldcp->id,
30561ae08745Sheppo 		    "ldc_close: (0x%llx) channel has bound descriptor rings\n",
30571ae08745Sheppo 		    ldcp->id);
30581ae08745Sheppo 		mutex_exit(&ldcp->lock);
30591ae08745Sheppo 		return (EBUSY);
30601ae08745Sheppo 	}
30611ae08745Sheppo 	if (ldcp->imp_dring_list != NULL) {
30621ae08745Sheppo 		DWARN(ldcp->id,
30631ae08745Sheppo 		    "ldc_close: (0x%llx) channel has mapped descriptor rings\n",
30641ae08745Sheppo 		    ldcp->id);
30651ae08745Sheppo 		mutex_exit(&ldcp->lock);
30661ae08745Sheppo 		return (EBUSY);
30671ae08745Sheppo 	}
30681ae08745Sheppo 
30694d39be2bSsg70180 	if (ldcp->cb_inprogress) {
30704d39be2bSsg70180 		DWARN(ldcp->id, "ldc_close: (0x%llx) callback active\n",
30714d39be2bSsg70180 		    ldcp->id);
30724d39be2bSsg70180 		mutex_exit(&ldcp->lock);
30734d39be2bSsg70180 		return (EWOULDBLOCK);
30744d39be2bSsg70180 	}
30754d39be2bSsg70180 
3076d10e4ef2Snarayan 	/* Obtain Tx lock */
3077d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
3078d10e4ef2Snarayan 
30791ae08745Sheppo 	/*
30801ae08745Sheppo 	 * Wait for pending transmits to complete i.e Tx queue to drain
30811ae08745Sheppo 	 * if there are pending pkts - wait 1 ms and retry again
30821ae08745Sheppo 	 */
30831ae08745Sheppo 	for (;;) {
30841ae08745Sheppo 
30851ae08745Sheppo 		rv = hv_ldc_tx_get_state(ldcp->id,
30861ae08745Sheppo 		    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
30871ae08745Sheppo 		if (rv) {
30881ae08745Sheppo 			cmn_err(CE_WARN,
30891ae08745Sheppo 			    "ldc_close: (0x%lx) cannot read qptrs\n", ldcp->id);
3090d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
30911ae08745Sheppo 			mutex_exit(&ldcp->lock);
30921ae08745Sheppo 			return (EIO);
30931ae08745Sheppo 		}
30941ae08745Sheppo 
30951ae08745Sheppo 		if (ldcp->tx_head == ldcp->tx_tail ||
30961ae08745Sheppo 		    ldcp->link_state != LDC_CHANNEL_UP) {
30971ae08745Sheppo 			break;
30981ae08745Sheppo 		}
30991ae08745Sheppo 
31001ae08745Sheppo 		if (chk_done) {
31011ae08745Sheppo 			DWARN(ldcp->id,
31021ae08745Sheppo 			    "ldc_close: (0x%llx) Tx queue drain timeout\n",
31031ae08745Sheppo 			    ldcp->id);
31041ae08745Sheppo 			break;
31051ae08745Sheppo 		}
31061ae08745Sheppo 
31071ae08745Sheppo 		/* wait for one ms and try again */
31081ae08745Sheppo 		delay(drv_usectohz(1000));
31091ae08745Sheppo 		chk_done = B_TRUE;
31101ae08745Sheppo 	}
31111ae08745Sheppo 
31121ae08745Sheppo 	/*
3113a8ea4edeSnarayan 	 * Drain the Tx and Rx queues as we are closing the
3114a8ea4edeSnarayan 	 * channel. We dont care about any pending packets.
3115a8ea4edeSnarayan 	 * We have to also drain the queue prior to clearing
3116a8ea4edeSnarayan 	 * pending interrupts, otherwise the HV will trigger
3117a8ea4edeSnarayan 	 * an interrupt the moment the interrupt state is
3118a8ea4edeSnarayan 	 * cleared.
31193af08d82Slm66018 	 */
31203af08d82Slm66018 	(void) i_ldc_txq_reconf(ldcp);
3121a8ea4edeSnarayan 	(void) i_ldc_rxq_drain(ldcp);
31223af08d82Slm66018 
31233af08d82Slm66018 	/*
31241ae08745Sheppo 	 * Unregister the channel with the nexus
31251ae08745Sheppo 	 */
3126d10e4ef2Snarayan 	while ((rv = i_ldc_unregister_channel(ldcp)) != 0) {
3127d10e4ef2Snarayan 
3128d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
31291ae08745Sheppo 		mutex_exit(&ldcp->lock);
3130d10e4ef2Snarayan 
3131d10e4ef2Snarayan 		/* if any error other than EAGAIN return back */
3132a8ea4edeSnarayan 		if (rv != EAGAIN || retries >= ldc_max_retries) {
3133d10e4ef2Snarayan 			cmn_err(CE_WARN,
3134d10e4ef2Snarayan 			    "ldc_close: (0x%lx) unregister failed, %d\n",
3135d10e4ef2Snarayan 			    ldcp->id, rv);
31361ae08745Sheppo 			return (rv);
31371ae08745Sheppo 		}
31381ae08745Sheppo 
31391ae08745Sheppo 		/*
3140d10e4ef2Snarayan 		 * As there could be pending interrupts we need
3141d10e4ef2Snarayan 		 * to wait and try again
3142d10e4ef2Snarayan 		 */
31434d39be2bSsg70180 		drv_usecwait(ldc_close_delay);
3144d10e4ef2Snarayan 		mutex_enter(&ldcp->lock);
3145d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
3146d10e4ef2Snarayan 		retries++;
3147d10e4ef2Snarayan 	}
3148d10e4ef2Snarayan 
3149d10e4ef2Snarayan 	/*
31501ae08745Sheppo 	 * Unregister queues
31511ae08745Sheppo 	 */
31521ae08745Sheppo 	rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL);
31531ae08745Sheppo 	if (rv) {
31541ae08745Sheppo 		cmn_err(CE_WARN,
31551ae08745Sheppo 		    "ldc_close: (0x%lx) channel TX queue unconf failed\n",
31561ae08745Sheppo 		    ldcp->id);
3157d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
31581ae08745Sheppo 		mutex_exit(&ldcp->lock);
31591ae08745Sheppo 		return (EIO);
31601ae08745Sheppo 	}
31611ae08745Sheppo 	rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL);
31621ae08745Sheppo 	if (rv) {
31631ae08745Sheppo 		cmn_err(CE_WARN,
31641ae08745Sheppo 		    "ldc_close: (0x%lx) channel RX queue unconf failed\n",
31651ae08745Sheppo 		    ldcp->id);
3166d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
31671ae08745Sheppo 		mutex_exit(&ldcp->lock);
31681ae08745Sheppo 		return (EIO);
31691ae08745Sheppo 	}
31701ae08745Sheppo 
31711ae08745Sheppo 	ldcp->tstate &= ~TS_QCONF_RDY;
31721ae08745Sheppo 
31731ae08745Sheppo 	/* Reset channel state information */
31741ae08745Sheppo 	i_ldc_reset_state(ldcp);
31751ae08745Sheppo 
31761ae08745Sheppo 	/* Mark channel as down and in initialized state */
31771ae08745Sheppo 	ldcp->tx_ackd_head = 0;
31781ae08745Sheppo 	ldcp->tx_head = 0;
31793af08d82Slm66018 	ldcp->tstate = TS_IN_RESET|TS_INIT;
31801ae08745Sheppo 	ldcp->status = LDC_INIT;
31811ae08745Sheppo 
3182d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
31831ae08745Sheppo 	mutex_exit(&ldcp->lock);
31841ae08745Sheppo 
31851ae08745Sheppo 	/* Decrement number of open channels */
31861ae08745Sheppo 	mutex_enter(&ldcssp->lock);
31871ae08745Sheppo 	ldcssp->channels_open--;
31881ae08745Sheppo 	mutex_exit(&ldcssp->lock);
31891ae08745Sheppo 
31901ae08745Sheppo 	D1(ldcp->id, "ldc_close: (0x%llx) channel closed\n", ldcp->id);
31911ae08745Sheppo 
31921ae08745Sheppo 	return (0);
31931ae08745Sheppo }
31941ae08745Sheppo 
31951ae08745Sheppo /*
31961ae08745Sheppo  * Register channel callback
31971ae08745Sheppo  */
31981ae08745Sheppo int
31991ae08745Sheppo ldc_reg_callback(ldc_handle_t handle,
32001ae08745Sheppo     uint_t(*cb)(uint64_t event, caddr_t arg), caddr_t arg)
32011ae08745Sheppo {
32021ae08745Sheppo 	ldc_chan_t *ldcp;
32031ae08745Sheppo 
32041ae08745Sheppo 	if (handle == NULL) {
32051ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
32061ae08745Sheppo 		    "ldc_reg_callback: invalid channel handle\n");
32071ae08745Sheppo 		return (EINVAL);
32081ae08745Sheppo 	}
32091ae08745Sheppo 	if (((uint64_t)cb) < KERNELBASE) {
32101ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_reg_callback: invalid callback\n");
32111ae08745Sheppo 		return (EINVAL);
32121ae08745Sheppo 	}
32131ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
32141ae08745Sheppo 
32151ae08745Sheppo 	mutex_enter(&ldcp->lock);
32161ae08745Sheppo 
32171ae08745Sheppo 	if (ldcp->cb) {
32181ae08745Sheppo 		DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback exists\n",
32191ae08745Sheppo 		    ldcp->id);
32201ae08745Sheppo 		mutex_exit(&ldcp->lock);
32211ae08745Sheppo 		return (EIO);
32221ae08745Sheppo 	}
32231ae08745Sheppo 	if (ldcp->cb_inprogress) {
32241ae08745Sheppo 		DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback active\n",
32251ae08745Sheppo 		    ldcp->id);
32261ae08745Sheppo 		mutex_exit(&ldcp->lock);
32271ae08745Sheppo 		return (EWOULDBLOCK);
32281ae08745Sheppo 	}
32291ae08745Sheppo 
32301ae08745Sheppo 	ldcp->cb = cb;
32311ae08745Sheppo 	ldcp->cb_arg = arg;
32321ae08745Sheppo 	ldcp->cb_enabled = B_TRUE;
32331ae08745Sheppo 
32341ae08745Sheppo 	D1(ldcp->id,
32351ae08745Sheppo 	    "ldc_reg_callback: (0x%llx) registered callback for channel\n",
32361ae08745Sheppo 	    ldcp->id);
32371ae08745Sheppo 
32381ae08745Sheppo 	mutex_exit(&ldcp->lock);
32391ae08745Sheppo 
32401ae08745Sheppo 	return (0);
32411ae08745Sheppo }
32421ae08745Sheppo 
32431ae08745Sheppo /*
32441ae08745Sheppo  * Unregister channel callback
32451ae08745Sheppo  */
32461ae08745Sheppo int
32471ae08745Sheppo ldc_unreg_callback(ldc_handle_t handle)
32481ae08745Sheppo {
32491ae08745Sheppo 	ldc_chan_t *ldcp;
32501ae08745Sheppo 
32511ae08745Sheppo 	if (handle == NULL) {
32521ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
32531ae08745Sheppo 		    "ldc_unreg_callback: invalid channel handle\n");
32541ae08745Sheppo 		return (EINVAL);
32551ae08745Sheppo 	}
32561ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
32571ae08745Sheppo 
32581ae08745Sheppo 	mutex_enter(&ldcp->lock);
32591ae08745Sheppo 
32601ae08745Sheppo 	if (ldcp->cb == NULL) {
32611ae08745Sheppo 		DWARN(ldcp->id,
32621ae08745Sheppo 		    "ldc_unreg_callback: (0x%llx) no callback exists\n",
32631ae08745Sheppo 		    ldcp->id);
32641ae08745Sheppo 		mutex_exit(&ldcp->lock);
32651ae08745Sheppo 		return (EIO);
32661ae08745Sheppo 	}
32671ae08745Sheppo 	if (ldcp->cb_inprogress) {
32681ae08745Sheppo 		DWARN(ldcp->id,
32691ae08745Sheppo 		    "ldc_unreg_callback: (0x%llx) callback active\n",
32701ae08745Sheppo 		    ldcp->id);
32711ae08745Sheppo 		mutex_exit(&ldcp->lock);
32721ae08745Sheppo 		return (EWOULDBLOCK);
32731ae08745Sheppo 	}
32741ae08745Sheppo 
32751ae08745Sheppo 	ldcp->cb = NULL;
32761ae08745Sheppo 	ldcp->cb_arg = NULL;
32771ae08745Sheppo 	ldcp->cb_enabled = B_FALSE;
32781ae08745Sheppo 
32791ae08745Sheppo 	D1(ldcp->id,
32801ae08745Sheppo 	    "ldc_unreg_callback: (0x%llx) unregistered callback for channel\n",
32811ae08745Sheppo 	    ldcp->id);
32821ae08745Sheppo 
32831ae08745Sheppo 	mutex_exit(&ldcp->lock);
32841ae08745Sheppo 
32851ae08745Sheppo 	return (0);
32861ae08745Sheppo }
32871ae08745Sheppo 
32881ae08745Sheppo 
32891ae08745Sheppo /*
32901ae08745Sheppo  * Bring a channel up by initiating a handshake with the peer
32911ae08745Sheppo  * This call is asynchronous. It will complete at a later point
32921ae08745Sheppo  * in time when the peer responds back with an RTR.
32931ae08745Sheppo  */
32941ae08745Sheppo int
32951ae08745Sheppo ldc_up(ldc_handle_t handle)
32961ae08745Sheppo {
32971ae08745Sheppo 	int 		rv;
32981ae08745Sheppo 	ldc_chan_t 	*ldcp;
32991ae08745Sheppo 	ldc_msg_t 	*ldcmsg;
330057e6a936Ssb155480 	uint64_t 	tx_tail, tstate, link_state;
33011ae08745Sheppo 
33021ae08745Sheppo 	if (handle == NULL) {
33031ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_up: invalid channel handle\n");
33041ae08745Sheppo 		return (EINVAL);
33051ae08745Sheppo 	}
33061ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
33071ae08745Sheppo 
33081ae08745Sheppo 	mutex_enter(&ldcp->lock);
33091ae08745Sheppo 
33103af08d82Slm66018 	D1(ldcp->id, "ldc_up: (0x%llx) doing channel UP\n", ldcp->id);
33113af08d82Slm66018 
33123af08d82Slm66018 	/* clear the reset state */
33133af08d82Slm66018 	tstate = ldcp->tstate;
33143af08d82Slm66018 	ldcp->tstate &= ~TS_IN_RESET;
33153af08d82Slm66018 
33161ae08745Sheppo 	if (ldcp->tstate == TS_UP) {
33173af08d82Slm66018 		DWARN(ldcp->id,
33181ae08745Sheppo 		    "ldc_up: (0x%llx) channel is already in UP state\n",
33191ae08745Sheppo 		    ldcp->id);
33203af08d82Slm66018 
33213af08d82Slm66018 		/* mark channel as up */
33223af08d82Slm66018 		ldcp->status = LDC_UP;
33233af08d82Slm66018 
33243af08d82Slm66018 		/*
33253af08d82Slm66018 		 * if channel was in reset state and there was
33263af08d82Slm66018 		 * pending data clear interrupt state. this will
33273af08d82Slm66018 		 * trigger an interrupt, causing the RX handler to
33283af08d82Slm66018 		 * to invoke the client's callback
33293af08d82Slm66018 		 */
33303af08d82Slm66018 		if ((tstate & TS_IN_RESET) &&
33313af08d82Slm66018 		    ldcp->rx_intr_state == LDC_INTR_PEND) {
3332cb112a14Slm66018 			D1(ldcp->id,
33333af08d82Slm66018 			    "ldc_up: (0x%llx) channel has pending data, "
33343af08d82Slm66018 			    "clearing interrupt\n", ldcp->id);
33353af08d82Slm66018 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
33363af08d82Slm66018 		}
33373af08d82Slm66018 
33381ae08745Sheppo 		mutex_exit(&ldcp->lock);
33391ae08745Sheppo 		return (0);
33401ae08745Sheppo 	}
33411ae08745Sheppo 
33421ae08745Sheppo 	/* if the channel is in RAW mode - mark it as UP, if READY */
33431ae08745Sheppo 	if (ldcp->mode == LDC_MODE_RAW && ldcp->tstate >= TS_READY) {
33441ae08745Sheppo 		ldcp->tstate = TS_UP;
33451ae08745Sheppo 		mutex_exit(&ldcp->lock);
33461ae08745Sheppo 		return (0);
33471ae08745Sheppo 	}
33481ae08745Sheppo 
33491ae08745Sheppo 	/* Don't start another handshake if there is one in progress */
33501ae08745Sheppo 	if (ldcp->hstate) {
33513af08d82Slm66018 		D1(ldcp->id,
33521ae08745Sheppo 		    "ldc_up: (0x%llx) channel handshake in progress\n",
33531ae08745Sheppo 		    ldcp->id);
33541ae08745Sheppo 		mutex_exit(&ldcp->lock);
33551ae08745Sheppo 		return (0);
33561ae08745Sheppo 	}
33571ae08745Sheppo 
3358d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
3359d10e4ef2Snarayan 
336057e6a936Ssb155480 	/* save current link state */
336157e6a936Ssb155480 	link_state = ldcp->link_state;
336257e6a936Ssb155480 
33631ae08745Sheppo 	/* get the current tail for the LDC msg */
33641ae08745Sheppo 	rv = i_ldc_get_tx_tail(ldcp, &tx_tail);
33651ae08745Sheppo 	if (rv) {
3366cb112a14Slm66018 		D1(ldcp->id, "ldc_up: (0x%llx) cannot initiate handshake\n",
33671ae08745Sheppo 		    ldcp->id);
3368d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
33691ae08745Sheppo 		mutex_exit(&ldcp->lock);
33701ae08745Sheppo 		return (ECONNREFUSED);
33711ae08745Sheppo 	}
33721ae08745Sheppo 
337357e6a936Ssb155480 	/*
337457e6a936Ssb155480 	 * If i_ldc_get_tx_tail() changed link_state to either RESET or UP,
337557e6a936Ssb155480 	 * from a previous state of DOWN, then mark the channel as
337657e6a936Ssb155480 	 * being ready for handshake.
337757e6a936Ssb155480 	 */
337857e6a936Ssb155480 	if ((link_state == LDC_CHANNEL_DOWN) &&
337957e6a936Ssb155480 	    (link_state != ldcp->link_state)) {
338057e6a936Ssb155480 
338157e6a936Ssb155480 		ASSERT((ldcp->link_state == LDC_CHANNEL_RESET) ||
338257e6a936Ssb155480 		    (ldcp->link_state == LDC_CHANNEL_UP));
338357e6a936Ssb155480 
338457e6a936Ssb155480 		if (ldcp->mode == LDC_MODE_RAW) {
338557e6a936Ssb155480 			ldcp->status = LDC_UP;
338657e6a936Ssb155480 			ldcp->tstate = TS_UP;
338757e6a936Ssb155480 			mutex_exit(&ldcp->tx_lock);
338857e6a936Ssb155480 			mutex_exit(&ldcp->lock);
338957e6a936Ssb155480 			return (0);
339057e6a936Ssb155480 		} else {
339157e6a936Ssb155480 			ldcp->status = LDC_READY;
339257e6a936Ssb155480 			ldcp->tstate |= TS_LINK_READY;
339357e6a936Ssb155480 		}
339457e6a936Ssb155480 
339557e6a936Ssb155480 	}
339657e6a936Ssb155480 
33971ae08745Sheppo 	ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
33981ae08745Sheppo 	ZERO_PKT(ldcmsg);
33991ae08745Sheppo 
34001ae08745Sheppo 	ldcmsg->type = LDC_CTRL;
34011ae08745Sheppo 	ldcmsg->stype = LDC_INFO;
34021ae08745Sheppo 	ldcmsg->ctrl = LDC_VER;
34031ae08745Sheppo 	ldcp->next_vidx = 0;
34041ae08745Sheppo 	bcopy(&ldc_versions[0], ldcmsg->udata, sizeof (ldc_versions[0]));
34051ae08745Sheppo 
34061ae08745Sheppo 	DUMP_LDC_PKT(ldcp, "ldc_up snd ver", (uint64_t)ldcmsg);
34071ae08745Sheppo 
34081ae08745Sheppo 	/* initiate the send by calling into HV and set the new tail */
34091ae08745Sheppo 	tx_tail = (tx_tail + LDC_PACKET_SIZE) %
34101ae08745Sheppo 	    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
34111ae08745Sheppo 
34121ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
34131ae08745Sheppo 	if (rv) {
34141ae08745Sheppo 		DWARN(ldcp->id,
34151ae08745Sheppo 		    "ldc_up: (0x%llx) cannot initiate handshake rv=%d\n",
34161ae08745Sheppo 		    ldcp->id, rv);
3417d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
34181ae08745Sheppo 		mutex_exit(&ldcp->lock);
34191ae08745Sheppo 		return (rv);
34201ae08745Sheppo 	}
34211ae08745Sheppo 
34220a55fbb7Slm66018 	ldcp->hstate |= TS_SENT_VER;
34231ae08745Sheppo 	ldcp->tx_tail = tx_tail;
34241ae08745Sheppo 	D1(ldcp->id, "ldc_up: (0x%llx) channel up initiated\n", ldcp->id);
34251ae08745Sheppo 
3426d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
34271ae08745Sheppo 	mutex_exit(&ldcp->lock);
34281ae08745Sheppo 
34291ae08745Sheppo 	return (rv);
34301ae08745Sheppo }
34311ae08745Sheppo 
34321ae08745Sheppo 
34331ae08745Sheppo /*
3434e1ebb9ecSlm66018  * Bring a channel down by resetting its state and queues
34351ae08745Sheppo  */
34361ae08745Sheppo int
3437e1ebb9ecSlm66018 ldc_down(ldc_handle_t handle)
34381ae08745Sheppo {
34391ae08745Sheppo 	ldc_chan_t 	*ldcp;
34401ae08745Sheppo 
34411ae08745Sheppo 	if (handle == NULL) {
3442e1ebb9ecSlm66018 		DWARN(DBG_ALL_LDCS, "ldc_down: invalid channel handle\n");
34431ae08745Sheppo 		return (EINVAL);
34441ae08745Sheppo 	}
34451ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
34461ae08745Sheppo 	mutex_enter(&ldcp->lock);
3447d10e4ef2Snarayan 	mutex_enter(&ldcp->tx_lock);
34483af08d82Slm66018 	i_ldc_reset(ldcp, B_TRUE);
3449d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
34501ae08745Sheppo 	mutex_exit(&ldcp->lock);
34511ae08745Sheppo 
34521ae08745Sheppo 	return (0);
34531ae08745Sheppo }
34541ae08745Sheppo 
34551ae08745Sheppo /*
34561ae08745Sheppo  * Get the current channel status
34571ae08745Sheppo  */
34581ae08745Sheppo int
34591ae08745Sheppo ldc_status(ldc_handle_t handle, ldc_status_t *status)
34601ae08745Sheppo {
34611ae08745Sheppo 	ldc_chan_t *ldcp;
34621ae08745Sheppo 
34631ae08745Sheppo 	if (handle == NULL || status == NULL) {
34641ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_status: invalid argument\n");
34651ae08745Sheppo 		return (EINVAL);
34661ae08745Sheppo 	}
34671ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
34681ae08745Sheppo 
34691ae08745Sheppo 	*status = ((ldc_chan_t *)handle)->status;
34701ae08745Sheppo 
3471cb112a14Slm66018 	D1(ldcp->id,
34721ae08745Sheppo 	    "ldc_status: (0x%llx) returned status %d\n", ldcp->id, *status);
34731ae08745Sheppo 	return (0);
34741ae08745Sheppo }
34751ae08745Sheppo 
34761ae08745Sheppo 
34771ae08745Sheppo /*
34781ae08745Sheppo  * Set the channel's callback mode - enable/disable callbacks
34791ae08745Sheppo  */
34801ae08745Sheppo int
34811ae08745Sheppo ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t cmode)
34821ae08745Sheppo {
34831ae08745Sheppo 	ldc_chan_t 	*ldcp;
34841ae08745Sheppo 
34851ae08745Sheppo 	if (handle == NULL) {
34861ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
34871ae08745Sheppo 		    "ldc_set_intr_mode: invalid channel handle\n");
34881ae08745Sheppo 		return (EINVAL);
34891ae08745Sheppo 	}
34901ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
34911ae08745Sheppo 
34921ae08745Sheppo 	/*
34931ae08745Sheppo 	 * Record no callbacks should be invoked
34941ae08745Sheppo 	 */
34951ae08745Sheppo 	mutex_enter(&ldcp->lock);
34961ae08745Sheppo 
34971ae08745Sheppo 	switch (cmode) {
34981ae08745Sheppo 	case LDC_CB_DISABLE:
34991ae08745Sheppo 		if (!ldcp->cb_enabled) {
35001ae08745Sheppo 			DWARN(ldcp->id,
35011ae08745Sheppo 			    "ldc_set_cb_mode: (0x%llx) callbacks disabled\n",
35021ae08745Sheppo 			    ldcp->id);
35031ae08745Sheppo 			break;
35041ae08745Sheppo 		}
35051ae08745Sheppo 		ldcp->cb_enabled = B_FALSE;
35061ae08745Sheppo 
35071ae08745Sheppo 		D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) disabled callbacks\n",
35081ae08745Sheppo 		    ldcp->id);
35091ae08745Sheppo 		break;
35101ae08745Sheppo 
35111ae08745Sheppo 	case LDC_CB_ENABLE:
35121ae08745Sheppo 		if (ldcp->cb_enabled) {
35131ae08745Sheppo 			DWARN(ldcp->id,
35141ae08745Sheppo 			    "ldc_set_cb_mode: (0x%llx) callbacks enabled\n",
35151ae08745Sheppo 			    ldcp->id);
35161ae08745Sheppo 			break;
35171ae08745Sheppo 		}
35181ae08745Sheppo 		ldcp->cb_enabled = B_TRUE;
35191ae08745Sheppo 
35201ae08745Sheppo 		D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) enabled callbacks\n",
35211ae08745Sheppo 		    ldcp->id);
35221ae08745Sheppo 		break;
35231ae08745Sheppo 	}
35241ae08745Sheppo 
35251ae08745Sheppo 	mutex_exit(&ldcp->lock);
35261ae08745Sheppo 
35271ae08745Sheppo 	return (0);
35281ae08745Sheppo }
35291ae08745Sheppo 
35301ae08745Sheppo /*
35311ae08745Sheppo  * Check to see if there are packets on the incoming queue
3532e1ebb9ecSlm66018  * Will return hasdata = B_FALSE if there are no packets
35331ae08745Sheppo  */
35341ae08745Sheppo int
3535e1ebb9ecSlm66018 ldc_chkq(ldc_handle_t handle, boolean_t *hasdata)
35361ae08745Sheppo {
35371ae08745Sheppo 	int 		rv;
35381ae08745Sheppo 	uint64_t 	rx_head, rx_tail;
35391ae08745Sheppo 	ldc_chan_t 	*ldcp;
35401ae08745Sheppo 
35411ae08745Sheppo 	if (handle == NULL) {
35421ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_chkq: invalid channel handle\n");
35431ae08745Sheppo 		return (EINVAL);
35441ae08745Sheppo 	}
35451ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
35461ae08745Sheppo 
3547e1ebb9ecSlm66018 	*hasdata = B_FALSE;
35481ae08745Sheppo 
35491ae08745Sheppo 	mutex_enter(&ldcp->lock);
35501ae08745Sheppo 
35511ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
35521ae08745Sheppo 		D1(ldcp->id,
35531ae08745Sheppo 		    "ldc_chkq: (0x%llx) channel is not up\n", ldcp->id);
35541ae08745Sheppo 		mutex_exit(&ldcp->lock);
35551ae08745Sheppo 		return (ECONNRESET);
35561ae08745Sheppo 	}
35571ae08745Sheppo 
35581ae08745Sheppo 	/* Read packet(s) from the queue */
35591ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
35601ae08745Sheppo 	    &ldcp->link_state);
35611ae08745Sheppo 	if (rv != 0) {
35621ae08745Sheppo 		cmn_err(CE_WARN,
35631ae08745Sheppo 		    "ldc_chkq: (0x%lx) unable to read queue ptrs", ldcp->id);
35641ae08745Sheppo 		mutex_exit(&ldcp->lock);
35651ae08745Sheppo 		return (EIO);
35661ae08745Sheppo 	}
356758283286Sha137994 
35681ae08745Sheppo 	/* reset the channel state if the channel went down */
35691ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_DOWN ||
35701ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
3571d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
35723af08d82Slm66018 		i_ldc_reset(ldcp, B_FALSE);
3573d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
35741ae08745Sheppo 		mutex_exit(&ldcp->lock);
35751ae08745Sheppo 		return (ECONNRESET);
35761ae08745Sheppo 	}
35771ae08745Sheppo 
357858283286Sha137994 	switch (ldcp->mode) {
357958283286Sha137994 	case LDC_MODE_RAW:
358058283286Sha137994 		/*
358158283286Sha137994 		 * In raw mode, there are no ctrl packets, so checking
358258283286Sha137994 		 * if the queue is non-empty is sufficient.
358358283286Sha137994 		 */
358458283286Sha137994 		*hasdata = (rx_head != rx_tail);
358558283286Sha137994 		break;
358658283286Sha137994 
358758283286Sha137994 	case LDC_MODE_UNRELIABLE:
358858283286Sha137994 		/*
358958283286Sha137994 		 * In unreliable mode, if the queue is non-empty, we need
359058283286Sha137994 		 * to check if it actually contains unread data packets.
359158283286Sha137994 		 * The queue may just contain ctrl packets.
359258283286Sha137994 		 */
3593a74caf9bSha137994 		if (rx_head != rx_tail) {
359458283286Sha137994 			*hasdata = (i_ldc_chkq(ldcp) == 0);
3595a74caf9bSha137994 			/*
3596a74caf9bSha137994 			 * If no data packets were found on the queue,
3597a74caf9bSha137994 			 * all packets must have been control packets
3598a74caf9bSha137994 			 * which will now have been processed, leaving
3599a74caf9bSha137994 			 * the queue empty. If the interrupt state
3600a74caf9bSha137994 			 * is pending, we need to clear the interrupt
3601a74caf9bSha137994 			 * here.
3602a74caf9bSha137994 			 */
3603a74caf9bSha137994 			if (*hasdata == B_FALSE &&
3604a74caf9bSha137994 			    ldcp->rx_intr_state == LDC_INTR_PEND) {
3605a74caf9bSha137994 				i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
3606a74caf9bSha137994 			}
3607a74caf9bSha137994 		}
360858283286Sha137994 		break;
360958283286Sha137994 
361020ae46ebSha137994 	case LDC_MODE_RELIABLE:
361158283286Sha137994 		/*
361220ae46ebSha137994 		 * In reliable mode, first check for 'stream_remains' > 0.
361358283286Sha137994 		 * Otherwise, if the data queue head and tail pointers
361458283286Sha137994 		 * differ, there must be data to read.
361558283286Sha137994 		 */
361658283286Sha137994 		if (ldcp->stream_remains > 0)
3617e1ebb9ecSlm66018 			*hasdata = B_TRUE;
361858283286Sha137994 		else
361958283286Sha137994 			*hasdata = (ldcp->rx_dq_head != ldcp->rx_dq_tail);
362058283286Sha137994 		break;
362158283286Sha137994 
362258283286Sha137994 	default:
362358283286Sha137994 		cmn_err(CE_WARN, "ldc_chkq: (0x%lx) unexpected channel mode "
362458283286Sha137994 		    "(0x%x)", ldcp->id, ldcp->mode);
362558283286Sha137994 		mutex_exit(&ldcp->lock);
362658283286Sha137994 		return (EIO);
36271ae08745Sheppo 	}
36281ae08745Sheppo 
36291ae08745Sheppo 	mutex_exit(&ldcp->lock);
36301ae08745Sheppo 
36311ae08745Sheppo 	return (0);
36321ae08745Sheppo }
36331ae08745Sheppo 
36341ae08745Sheppo 
36351ae08745Sheppo /*
36361ae08745Sheppo  * Read 'size' amount of bytes or less. If incoming buffer
36371ae08745Sheppo  * is more than 'size', ENOBUFS is returned.
36381ae08745Sheppo  *
36391ae08745Sheppo  * On return, size contains the number of bytes read.
36401ae08745Sheppo  */
36411ae08745Sheppo int
36421ae08745Sheppo ldc_read(ldc_handle_t handle, caddr_t bufp, size_t *sizep)
36431ae08745Sheppo {
36441ae08745Sheppo 	ldc_chan_t 	*ldcp;
36451ae08745Sheppo 	uint64_t 	rx_head = 0, rx_tail = 0;
36461ae08745Sheppo 	int		rv = 0, exit_val;
36471ae08745Sheppo 
36481ae08745Sheppo 	if (handle == NULL) {
36491ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_read: invalid channel handle\n");
36501ae08745Sheppo 		return (EINVAL);
36511ae08745Sheppo 	}
36521ae08745Sheppo 
36531ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
36541ae08745Sheppo 
36551ae08745Sheppo 	/* channel lock */
36561ae08745Sheppo 	mutex_enter(&ldcp->lock);
36571ae08745Sheppo 
36581ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
36591ae08745Sheppo 		DWARN(ldcp->id,
36601ae08745Sheppo 		    "ldc_read: (0x%llx) channel is not in UP state\n",
36611ae08745Sheppo 		    ldcp->id);
36621ae08745Sheppo 		exit_val = ECONNRESET;
366320ae46ebSha137994 	} else if (ldcp->mode == LDC_MODE_RELIABLE) {
366458283286Sha137994 		TRACE_RXDQ_LENGTH(ldcp);
366558283286Sha137994 		exit_val = ldcp->read_p(ldcp, bufp, sizep);
3666*12f80fa6Sha137994 
3667*12f80fa6Sha137994 		/*
3668*12f80fa6Sha137994 		 * For reliable mode channels, the interrupt
3669*12f80fa6Sha137994 		 * state is only set to pending during
3670*12f80fa6Sha137994 		 * interrupt handling when the secondary data
3671*12f80fa6Sha137994 		 * queue became full, leaving unprocessed
3672*12f80fa6Sha137994 		 * packets on the Rx queue. If the interrupt
3673*12f80fa6Sha137994 		 * state is pending and space is now available
3674*12f80fa6Sha137994 		 * on the data queue, clear the interrupt.
3675*12f80fa6Sha137994 		 */
3676*12f80fa6Sha137994 		if (ldcp->rx_intr_state == LDC_INTR_PEND &&
3677*12f80fa6Sha137994 		    Q_CONTIG_SPACE(ldcp->rx_dq_head, ldcp->rx_dq_tail,
3678*12f80fa6Sha137994 		    ldcp->rx_dq_entries << LDC_PACKET_SHIFT) >=
3679*12f80fa6Sha137994 		    LDC_PACKET_SIZE) {
3680*12f80fa6Sha137994 			/* data queue is not full */
3681*12f80fa6Sha137994 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
3682*12f80fa6Sha137994 		}
3683*12f80fa6Sha137994 
368458283286Sha137994 		mutex_exit(&ldcp->lock);
368558283286Sha137994 		return (exit_val);
36861ae08745Sheppo 	} else {
36871ae08745Sheppo 		exit_val = ldcp->read_p(ldcp, bufp, sizep);
36881ae08745Sheppo 	}
36891ae08745Sheppo 
36901ae08745Sheppo 	/*
36911ae08745Sheppo 	 * if queue has been drained - clear interrupt
36921ae08745Sheppo 	 */
36931ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
36941ae08745Sheppo 	    &ldcp->link_state);
3695cb112a14Slm66018 	if (rv != 0) {
3696cb112a14Slm66018 		cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs",
3697cb112a14Slm66018 		    ldcp->id);
3698cb112a14Slm66018 		mutex_enter(&ldcp->tx_lock);
3699cb112a14Slm66018 		i_ldc_reset(ldcp, B_TRUE);
3700cb112a14Slm66018 		mutex_exit(&ldcp->tx_lock);
3701bd8f0338Snarayan 		mutex_exit(&ldcp->lock);
3702cb112a14Slm66018 		return (ECONNRESET);
3703cb112a14Slm66018 	}
37043af08d82Slm66018 
37053af08d82Slm66018 	if (exit_val == 0) {
37063af08d82Slm66018 		if (ldcp->link_state == LDC_CHANNEL_DOWN ||
37073af08d82Slm66018 		    ldcp->link_state == LDC_CHANNEL_RESET) {
37083af08d82Slm66018 			mutex_enter(&ldcp->tx_lock);
37093af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
37103af08d82Slm66018 			exit_val = ECONNRESET;
37113af08d82Slm66018 			mutex_exit(&ldcp->tx_lock);
37123af08d82Slm66018 		}
37133af08d82Slm66018 		if ((rv == 0) &&
37143af08d82Slm66018 		    (ldcp->rx_intr_state == LDC_INTR_PEND) &&
37153af08d82Slm66018 		    (rx_head == rx_tail)) {
37161ae08745Sheppo 			i_ldc_clear_intr(ldcp, CNEX_RX_INTR);
37171ae08745Sheppo 		}
37183af08d82Slm66018 	}
37191ae08745Sheppo 
37201ae08745Sheppo 	mutex_exit(&ldcp->lock);
37211ae08745Sheppo 	return (exit_val);
37221ae08745Sheppo }
37231ae08745Sheppo 
37241ae08745Sheppo /*
37251ae08745Sheppo  * Basic raw mondo read -
37261ae08745Sheppo  * no interpretation of mondo contents at all.
37271ae08745Sheppo  *
37281ae08745Sheppo  * Enter and exit with ldcp->lock held by caller
37291ae08745Sheppo  */
37301ae08745Sheppo static int
37311ae08745Sheppo i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep)
37321ae08745Sheppo {
37331ae08745Sheppo 	uint64_t 	q_size_mask;
37341ae08745Sheppo 	ldc_msg_t 	*msgp;
37351ae08745Sheppo 	uint8_t		*msgbufp;
37361ae08745Sheppo 	int		rv = 0, space;
37371ae08745Sheppo 	uint64_t 	rx_head, rx_tail;
37381ae08745Sheppo 
37391ae08745Sheppo 	space = *sizep;
37401ae08745Sheppo 
37411ae08745Sheppo 	if (space < LDC_PAYLOAD_SIZE_RAW)
37421ae08745Sheppo 		return (ENOBUFS);
37431ae08745Sheppo 
37441ae08745Sheppo 	ASSERT(mutex_owned(&ldcp->lock));
37451ae08745Sheppo 
37461ae08745Sheppo 	/* compute mask for increment */
37471ae08745Sheppo 	q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT;
37481ae08745Sheppo 
37491ae08745Sheppo 	/*
37501ae08745Sheppo 	 * Read packet(s) from the queue
37511ae08745Sheppo 	 */
37521ae08745Sheppo 	rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail,
37531ae08745Sheppo 	    &ldcp->link_state);
37541ae08745Sheppo 	if (rv != 0) {
37551ae08745Sheppo 		cmn_err(CE_WARN,
37561ae08745Sheppo 		    "ldc_read_raw: (0x%lx) unable to read queue ptrs",
37571ae08745Sheppo 		    ldcp->id);
37581ae08745Sheppo 		return (EIO);
37591ae08745Sheppo 	}
37601ae08745Sheppo 	D1(ldcp->id, "ldc_read_raw: (0x%llx) rxh=0x%llx,"
37611ae08745Sheppo 	    " rxt=0x%llx, st=0x%llx\n",
37621ae08745Sheppo 	    ldcp->id, rx_head, rx_tail, ldcp->link_state);
37631ae08745Sheppo 
37641ae08745Sheppo 	/* reset the channel state if the channel went down */
37653af08d82Slm66018 	if (ldcp->link_state == LDC_CHANNEL_DOWN ||
37663af08d82Slm66018 	    ldcp->link_state == LDC_CHANNEL_RESET) {
3767d10e4ef2Snarayan 		mutex_enter(&ldcp->tx_lock);
37683af08d82Slm66018 		i_ldc_reset(ldcp, B_FALSE);
3769d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
37701ae08745Sheppo 		return (ECONNRESET);
37711ae08745Sheppo 	}
37721ae08745Sheppo 
37731ae08745Sheppo 	/*
37741ae08745Sheppo 	 * Check for empty queue
37751ae08745Sheppo 	 */
37761ae08745Sheppo 	if (rx_head == rx_tail) {
37771ae08745Sheppo 		*sizep = 0;
37781ae08745Sheppo 		return (0);
37791ae08745Sheppo 	}
37801ae08745Sheppo 
37811ae08745Sheppo 	/* get the message */
37821ae08745Sheppo 	msgp = (ldc_msg_t *)(ldcp->rx_q_va + rx_head);
37831ae08745Sheppo 
37841ae08745Sheppo 	/* if channel is in RAW mode, copy data and return */
37851ae08745Sheppo 	msgbufp = (uint8_t *)&(msgp->raw[0]);
37861ae08745Sheppo 
37871ae08745Sheppo 	bcopy(msgbufp, target_bufp, LDC_PAYLOAD_SIZE_RAW);
37881ae08745Sheppo 
37891ae08745Sheppo 	DUMP_PAYLOAD(ldcp->id, msgbufp);
37901ae08745Sheppo 
37911ae08745Sheppo 	*sizep = LDC_PAYLOAD_SIZE_RAW;
37921ae08745Sheppo 
37931ae08745Sheppo 	rx_head = (rx_head + LDC_PACKET_SIZE) & q_size_mask;
37940a55fbb7Slm66018 	rv = i_ldc_set_rx_head(ldcp, rx_head);
37951ae08745Sheppo 
37961ae08745Sheppo 	return (rv);
37971ae08745Sheppo }
37981ae08745Sheppo 
37991ae08745Sheppo /*
38001ae08745Sheppo  * Process LDC mondos to build larger packets
38011ae08745Sheppo  * with either un-reliable or reliable delivery.
38021ae08745Sheppo  *
38031ae08745Sheppo  * Enter and exit with ldcp->lock held by caller
38041ae08745Sheppo  */
38051ae08745Sheppo static int
38061ae08745Sheppo i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep)
38071ae08745Sheppo {
38081ae08745Sheppo 	int		rv = 0;
38091ae08745Sheppo 	uint64_t 	rx_head = 0, rx_tail = 0;
38101ae08745Sheppo 	uint64_t 	curr_head = 0;
38111ae08745Sheppo 	ldc_msg_t 	*msg;
38121ae08745Sheppo 	caddr_t 	target;
38131ae08745Sheppo 	size_t 		len = 0, bytes_read = 0;
38140a55fbb7Slm66018 	int 		retries = 0;
381558283286Sha137994 	uint64_t 	q_va, q_size_mask;
3816d10e4ef2Snarayan 	uint64_t	first_fragment = 0;
38171ae08745Sheppo 
38181ae08745Sheppo 	target = target_bufp;
38191ae08745Sheppo 
38201ae08745Sheppo 	ASSERT(mutex_owned(&ldcp->lock));
38211ae08745Sheppo 
38223af08d82Slm66018 	/* check if the buffer and size are valid */
38233af08d82Slm66018 	if (target_bufp == NULL || *sizep == 0) {
38243af08d82Slm66018 		DWARN(ldcp->id, "ldc_read: (0x%llx) invalid buffer/size\n",
38253af08d82Slm66018 		    ldcp->id);
38263af08d82Slm66018 		return (EINVAL);
38273af08d82Slm66018 	}
38283af08d82Slm66018 
382958283286Sha137994 	/* Set q_va and compute increment mask for the appropriate queue */
383020ae46ebSha137994 	if (ldcp->mode == LDC_MODE_RELIABLE) {
383158283286Sha137994 		q_va	    = ldcp->rx_dq_va;
383258283286Sha137994 		q_size_mask = (ldcp->rx_dq_entries-1)<<LDC_PACKET_SHIFT;
383358283286Sha137994 	} else {
383458283286Sha137994 		q_va	    = ldcp->rx_q_va;
38351ae08745Sheppo 		q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT;
383658283286Sha137994 	}
38371ae08745Sheppo 
38381ae08745Sheppo 	/*
38391ae08745Sheppo 	 * Read packet(s) from the queue
38401ae08745Sheppo 	 */
384158283286Sha137994 	rv = ldcp->readq_get_state(ldcp, &curr_head, &rx_tail,
38421ae08745Sheppo 	    &ldcp->link_state);
38431ae08745Sheppo 	if (rv != 0) {
38443af08d82Slm66018 		cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs",
38451ae08745Sheppo 		    ldcp->id);
38463af08d82Slm66018 		mutex_enter(&ldcp->tx_lock);
38473af08d82Slm66018 		i_ldc_reset(ldcp, B_TRUE);
38483af08d82Slm66018 		mutex_exit(&ldcp->tx_lock);
38493af08d82Slm66018 		return (ECONNRESET);
38501ae08745Sheppo 	}
38511ae08745Sheppo 	D1(ldcp->id, "ldc_read: (0x%llx) chd=0x%llx, tl=0x%llx, st=0x%llx\n",
38521ae08745Sheppo 	    ldcp->id, curr_head, rx_tail, ldcp->link_state);
38531ae08745Sheppo 
38541ae08745Sheppo 	/* reset the channel state if the channel went down */
38553af08d82Slm66018 	if (ldcp->link_state != LDC_CHANNEL_UP)
38563af08d82Slm66018 		goto channel_is_reset;
38571ae08745Sheppo 
38581ae08745Sheppo 	for (;;) {
38591ae08745Sheppo 
38601ae08745Sheppo 		if (curr_head == rx_tail) {
386158283286Sha137994 			/*
386258283286Sha137994 			 * If a data queue is being used, check the Rx HV
386358283286Sha137994 			 * queue. This will copy over any new data packets
386458283286Sha137994 			 * that have arrived.
386558283286Sha137994 			 */
386620ae46ebSha137994 			if (ldcp->mode == LDC_MODE_RELIABLE)
386758283286Sha137994 				(void) i_ldc_chkq(ldcp);
386858283286Sha137994 
386958283286Sha137994 			rv = ldcp->readq_get_state(ldcp,
38701ae08745Sheppo 			    &rx_head, &rx_tail, &ldcp->link_state);
38711ae08745Sheppo 			if (rv != 0) {
38721ae08745Sheppo 				cmn_err(CE_WARN,
38731ae08745Sheppo 				    "ldc_read: (0x%lx) cannot read queue ptrs",
38741ae08745Sheppo 				    ldcp->id);
3875d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
38763af08d82Slm66018 				i_ldc_reset(ldcp, B_TRUE);
3877d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
38781ae08745Sheppo 				return (ECONNRESET);
38791ae08745Sheppo 			}
388058283286Sha137994 
38813af08d82Slm66018 			if (ldcp->link_state != LDC_CHANNEL_UP)
38823af08d82Slm66018 				goto channel_is_reset;
38831ae08745Sheppo 
38841ae08745Sheppo 			if (curr_head == rx_tail) {
38851ae08745Sheppo 
38861ae08745Sheppo 				/* If in the middle of a fragmented xfer */
3887d10e4ef2Snarayan 				if (first_fragment != 0) {
38880a55fbb7Slm66018 
38890a55fbb7Slm66018 					/* wait for ldc_delay usecs */
38900a55fbb7Slm66018 					drv_usecwait(ldc_delay);
38910a55fbb7Slm66018 
38920a55fbb7Slm66018 					if (++retries < ldc_max_retries)
38931ae08745Sheppo 						continue;
38940a55fbb7Slm66018 
38951ae08745Sheppo 					*sizep = 0;
389620ae46ebSha137994 					if (ldcp->mode != LDC_MODE_RELIABLE)
389758283286Sha137994 						ldcp->last_msg_rcd =
389858283286Sha137994 						    first_fragment - 1;
38993af08d82Slm66018 					DWARN(DBG_ALL_LDCS, "ldc_read: "
390022f747efSnarayan 					    "(0x%llx) read timeout", ldcp->id);
39013af08d82Slm66018 					return (EAGAIN);
39021ae08745Sheppo 				}
39031ae08745Sheppo 				*sizep = 0;
39041ae08745Sheppo 				break;
39051ae08745Sheppo 			}
39063af08d82Slm66018 		}
39070a55fbb7Slm66018 		retries = 0;
39081ae08745Sheppo 
39091ae08745Sheppo 		D2(ldcp->id,
39101ae08745Sheppo 		    "ldc_read: (0x%llx) chd=0x%llx, rxhd=0x%llx, rxtl=0x%llx\n",
39111ae08745Sheppo 		    ldcp->id, curr_head, rx_head, rx_tail);
39121ae08745Sheppo 
39131ae08745Sheppo 		/* get the message */
391458283286Sha137994 		msg = (ldc_msg_t *)(q_va + curr_head);
39151ae08745Sheppo 
39161ae08745Sheppo 		DUMP_LDC_PKT(ldcp, "ldc_read received pkt",
39171ae08745Sheppo 		    ldcp->rx_q_va + curr_head);
39181ae08745Sheppo 
39191ae08745Sheppo 		/* Check the message ID for the message received */
392020ae46ebSha137994 		if (ldcp->mode != LDC_MODE_RELIABLE) {
39211ae08745Sheppo 			if ((rv = i_ldc_check_seqid(ldcp, msg)) != 0) {
39221ae08745Sheppo 
392358283286Sha137994 				DWARN(ldcp->id, "ldc_read: (0x%llx) seqid "
392458283286Sha137994 				    "error, q_ptrs=0x%lx,0x%lx",
392558283286Sha137994 				    ldcp->id, rx_head, rx_tail);
39261ae08745Sheppo 
39270a55fbb7Slm66018 				/* throw away data */
39280a55fbb7Slm66018 				bytes_read = 0;
39290a55fbb7Slm66018 
39301ae08745Sheppo 				/* Reset last_msg_rcd to start of message */
3931d10e4ef2Snarayan 				if (first_fragment != 0) {
3932d10e4ef2Snarayan 					ldcp->last_msg_rcd = first_fragment - 1;
3933d10e4ef2Snarayan 					first_fragment = 0;
39341ae08745Sheppo 				}
39351ae08745Sheppo 				/*
39361ae08745Sheppo 				 * Send a NACK -- invalid seqid
39371ae08745Sheppo 				 * get the current tail for the response
39381ae08745Sheppo 				 */
39391ae08745Sheppo 				rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK,
39401ae08745Sheppo 				    (msg->ctrl & LDC_CTRL_MASK));
39411ae08745Sheppo 				if (rv) {
39421ae08745Sheppo 					cmn_err(CE_NOTE,
39431ae08745Sheppo 					    "ldc_read: (0x%lx) err sending "
39441ae08745Sheppo 					    "NACK msg\n", ldcp->id);
3945d10e4ef2Snarayan 
394658283286Sha137994 					/* if cannot send NACK - reset chan */
3947d10e4ef2Snarayan 					mutex_enter(&ldcp->tx_lock);
39483af08d82Slm66018 					i_ldc_reset(ldcp, B_FALSE);
3949d10e4ef2Snarayan 					mutex_exit(&ldcp->tx_lock);
3950d10e4ef2Snarayan 					rv = ECONNRESET;
3951d10e4ef2Snarayan 					break;
39521ae08745Sheppo 				}
39531ae08745Sheppo 
39541ae08745Sheppo 				/* purge receive queue */
39550a55fbb7Slm66018 				rv = i_ldc_set_rx_head(ldcp, rx_tail);
39561ae08745Sheppo 
39571ae08745Sheppo 				break;
39581ae08745Sheppo 			}
39591ae08745Sheppo 
39601ae08745Sheppo 			/*
39611ae08745Sheppo 			 * Process any messages of type CTRL messages
3962e1ebb9ecSlm66018 			 * Future implementations should try to pass these
3963e1ebb9ecSlm66018 			 * to LDC link by resetting the intr state.
39641ae08745Sheppo 			 *
396558283286Sha137994 			 * NOTE: not done as a switch() as type can be
396658283286Sha137994 			 * both ctrl+data
39671ae08745Sheppo 			 */
39681ae08745Sheppo 			if (msg->type & LDC_CTRL) {
39691ae08745Sheppo 				if (rv = i_ldc_ctrlmsg(ldcp, msg)) {
39701ae08745Sheppo 					if (rv == EAGAIN)
39711ae08745Sheppo 						continue;
39720a55fbb7Slm66018 					rv = i_ldc_set_rx_head(ldcp, rx_tail);
39731ae08745Sheppo 					*sizep = 0;
39741ae08745Sheppo 					bytes_read = 0;
39751ae08745Sheppo 					break;
39761ae08745Sheppo 				}
39771ae08745Sheppo 			}
39781ae08745Sheppo 
39791ae08745Sheppo 			/* process data ACKs */
39801ae08745Sheppo 			if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) {
3981d10e4ef2Snarayan 				if (rv = i_ldc_process_data_ACK(ldcp, msg)) {
3982d10e4ef2Snarayan 					*sizep = 0;
3983d10e4ef2Snarayan 					bytes_read = 0;
3984d10e4ef2Snarayan 					break;
3985d10e4ef2Snarayan 				}
39861ae08745Sheppo 			}
39871ae08745Sheppo 
398883d3bc6fSnarayan 			/* process data NACKs */
398983d3bc6fSnarayan 			if ((msg->type & LDC_DATA) && (msg->stype & LDC_NACK)) {
399083d3bc6fSnarayan 				DWARN(ldcp->id,
399158283286Sha137994 				    "ldc_read: (0x%llx) received DATA/NACK",
399258283286Sha137994 				    ldcp->id);
399383d3bc6fSnarayan 				mutex_enter(&ldcp->tx_lock);
399483d3bc6fSnarayan 				i_ldc_reset(ldcp, B_TRUE);
399583d3bc6fSnarayan 				mutex_exit(&ldcp->tx_lock);
399683d3bc6fSnarayan 				return (ECONNRESET);
399783d3bc6fSnarayan 			}
399858283286Sha137994 		}
399983d3bc6fSnarayan 
40001ae08745Sheppo 		/* process data messages */
40011ae08745Sheppo 		if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) {
40021ae08745Sheppo 
40031ae08745Sheppo 			uint8_t *msgbuf = (uint8_t *)(
400420ae46ebSha137994 			    (ldcp->mode == LDC_MODE_RELIABLE) ?
400522f747efSnarayan 			    msg->rdata : msg->udata);
40061ae08745Sheppo 
40071ae08745Sheppo 			D2(ldcp->id,
40081ae08745Sheppo 			    "ldc_read: (0x%llx) received data msg\n", ldcp->id);
40091ae08745Sheppo 
40101ae08745Sheppo 			/* get the packet length */
40111ae08745Sheppo 			len = (msg->env & LDC_LEN_MASK);
40121ae08745Sheppo 
40131ae08745Sheppo 				/*
40141ae08745Sheppo 				 * FUTURE OPTIMIZATION:
40151ae08745Sheppo 				 * dont need to set q head for every
40161ae08745Sheppo 				 * packet we read just need to do this when
40171ae08745Sheppo 				 * we are done or need to wait for more
40181ae08745Sheppo 				 * mondos to make a full packet - this is
40191ae08745Sheppo 				 * currently expensive.
40201ae08745Sheppo 				 */
40211ae08745Sheppo 
4022d10e4ef2Snarayan 			if (first_fragment == 0) {
40231ae08745Sheppo 
40241ae08745Sheppo 				/*
40251ae08745Sheppo 				 * first packets should always have the start
40261ae08745Sheppo 				 * bit set (even for a single packet). If not
40271ae08745Sheppo 				 * throw away the packet
40281ae08745Sheppo 				 */
40291ae08745Sheppo 				if (!(msg->env & LDC_FRAG_START)) {
40301ae08745Sheppo 
40311ae08745Sheppo 					DWARN(DBG_ALL_LDCS,
40321ae08745Sheppo 					    "ldc_read: (0x%llx) not start - "
40331ae08745Sheppo 					    "frag=%x\n", ldcp->id,
40341ae08745Sheppo 					    (msg->env) & LDC_FRAG_MASK);
40351ae08745Sheppo 
40361ae08745Sheppo 					/* toss pkt, inc head, cont reading */
40371ae08745Sheppo 					bytes_read = 0;
40381ae08745Sheppo 					target = target_bufp;
40391ae08745Sheppo 					curr_head =
40401ae08745Sheppo 					    (curr_head + LDC_PACKET_SIZE)
40411ae08745Sheppo 					    & q_size_mask;
404258283286Sha137994 					if (rv = ldcp->readq_set_head(ldcp,
40431ae08745Sheppo 					    curr_head))
40441ae08745Sheppo 						break;
40451ae08745Sheppo 
40461ae08745Sheppo 					continue;
40471ae08745Sheppo 				}
40481ae08745Sheppo 
4049d10e4ef2Snarayan 				first_fragment = msg->seqid;
40501ae08745Sheppo 			} else {
40511ae08745Sheppo 				/* check to see if this is a pkt w/ START bit */
40521ae08745Sheppo 				if (msg->env & LDC_FRAG_START) {
40531ae08745Sheppo 					DWARN(DBG_ALL_LDCS,
40541ae08745Sheppo 					    "ldc_read:(0x%llx) unexpected pkt"
40551ae08745Sheppo 					    " env=0x%x discarding %d bytes,"
40561ae08745Sheppo 					    " lastmsg=%d, currentmsg=%d\n",
40571ae08745Sheppo 					    ldcp->id, msg->env&LDC_FRAG_MASK,
40581ae08745Sheppo 					    bytes_read, ldcp->last_msg_rcd,
40591ae08745Sheppo 					    msg->seqid);
40601ae08745Sheppo 
40611ae08745Sheppo 					/* throw data we have read so far */
40621ae08745Sheppo 					bytes_read = 0;
40631ae08745Sheppo 					target = target_bufp;
4064d10e4ef2Snarayan 					first_fragment = msg->seqid;
40651ae08745Sheppo 
406658283286Sha137994 					if (rv = ldcp->readq_set_head(ldcp,
40671ae08745Sheppo 					    curr_head))
40681ae08745Sheppo 						break;
40691ae08745Sheppo 				}
40701ae08745Sheppo 			}
40711ae08745Sheppo 
40721ae08745Sheppo 			/* copy (next) pkt into buffer */
40731ae08745Sheppo 			if (len <= (*sizep - bytes_read)) {
40741ae08745Sheppo 				bcopy(msgbuf, target, len);
40751ae08745Sheppo 				target += len;
40761ae08745Sheppo 				bytes_read += len;
40771ae08745Sheppo 			} else {
40781ae08745Sheppo 				/*
40791ae08745Sheppo 				 * there is not enough space in the buffer to
40801ae08745Sheppo 				 * read this pkt. throw message away & continue
40811ae08745Sheppo 				 * reading data from queue
40821ae08745Sheppo 				 */
40831ae08745Sheppo 				DWARN(DBG_ALL_LDCS,
40841ae08745Sheppo 				    "ldc_read: (0x%llx) buffer too small, "
40851ae08745Sheppo 				    "head=0x%lx, expect=%d, got=%d\n", ldcp->id,
40861ae08745Sheppo 				    curr_head, *sizep, bytes_read+len);
40871ae08745Sheppo 
4088d10e4ef2Snarayan 				first_fragment = 0;
40891ae08745Sheppo 				target = target_bufp;
40901ae08745Sheppo 				bytes_read = 0;
40911ae08745Sheppo 
40921ae08745Sheppo 				/* throw away everything received so far */
409358283286Sha137994 				if (rv = ldcp->readq_set_head(ldcp, curr_head))
40941ae08745Sheppo 					break;
40951ae08745Sheppo 
40961ae08745Sheppo 				/* continue reading remaining pkts */
40971ae08745Sheppo 				continue;
40981ae08745Sheppo 			}
40991ae08745Sheppo 		}
41001ae08745Sheppo 
41011ae08745Sheppo 		/* set the message id */
410220ae46ebSha137994 		if (ldcp->mode != LDC_MODE_RELIABLE)
41031ae08745Sheppo 			ldcp->last_msg_rcd = msg->seqid;
41041ae08745Sheppo 
41051ae08745Sheppo 		/* move the head one position */
41061ae08745Sheppo 		curr_head = (curr_head + LDC_PACKET_SIZE) & q_size_mask;
41071ae08745Sheppo 
41081ae08745Sheppo 		if (msg->env & LDC_FRAG_STOP) {
41091ae08745Sheppo 
41101ae08745Sheppo 			/*
41111ae08745Sheppo 			 * All pkts that are part of this fragmented transfer
41121ae08745Sheppo 			 * have been read or this was a single pkt read
41131ae08745Sheppo 			 * or there was an error
41141ae08745Sheppo 			 */
41151ae08745Sheppo 
41161ae08745Sheppo 			/* set the queue head */
411758283286Sha137994 			if (rv = ldcp->readq_set_head(ldcp, curr_head))
41181ae08745Sheppo 				bytes_read = 0;
41191ae08745Sheppo 
41201ae08745Sheppo 			*sizep = bytes_read;
41211ae08745Sheppo 
41221ae08745Sheppo 			break;
41231ae08745Sheppo 		}
41241ae08745Sheppo 
4125332608acSnarayan 		/* advance head if it is a CTRL packet or a DATA ACK packet */
4126332608acSnarayan 		if ((msg->type & LDC_CTRL) ||
4127332608acSnarayan 		    ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK))) {
41281ae08745Sheppo 
41291ae08745Sheppo 			/* set the queue head */
413058283286Sha137994 			if (rv = ldcp->readq_set_head(ldcp, curr_head)) {
41311ae08745Sheppo 				bytes_read = 0;
41321ae08745Sheppo 				break;
41331ae08745Sheppo 			}
41341ae08745Sheppo 
41351ae08745Sheppo 			D2(ldcp->id, "ldc_read: (0x%llx) set ACK qhead 0x%llx",
41361ae08745Sheppo 			    ldcp->id, curr_head);
41371ae08745Sheppo 		}
41381ae08745Sheppo 
41391ae08745Sheppo 	} /* for (;;) */
41401ae08745Sheppo 
41411ae08745Sheppo 	D2(ldcp->id, "ldc_read: (0x%llx) end size=%d", ldcp->id, *sizep);
41421ae08745Sheppo 
41431ae08745Sheppo 	return (rv);
41443af08d82Slm66018 
41453af08d82Slm66018 channel_is_reset:
41463af08d82Slm66018 	mutex_enter(&ldcp->tx_lock);
41473af08d82Slm66018 	i_ldc_reset(ldcp, B_FALSE);
41483af08d82Slm66018 	mutex_exit(&ldcp->tx_lock);
41493af08d82Slm66018 	return (ECONNRESET);
41501ae08745Sheppo }
41511ae08745Sheppo 
41521ae08745Sheppo /*
415320ae46ebSha137994  * Fetch and buffer incoming packets so we can hand them back as
41541ae08745Sheppo  * a basic byte stream.
41551ae08745Sheppo  *
41561ae08745Sheppo  * Enter and exit with ldcp->lock held by caller
41571ae08745Sheppo  */
41581ae08745Sheppo static int
41591ae08745Sheppo i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep)
41601ae08745Sheppo {
41611ae08745Sheppo 	int	rv;
41621ae08745Sheppo 	size_t	size;
41631ae08745Sheppo 
41641ae08745Sheppo 	ASSERT(mutex_owned(&ldcp->lock));
41651ae08745Sheppo 
41661ae08745Sheppo 	D2(ldcp->id, "i_ldc_read_stream: (0x%llx) buffer size=%d",
41671ae08745Sheppo 	    ldcp->id, *sizep);
41681ae08745Sheppo 
41691ae08745Sheppo 	if (ldcp->stream_remains == 0) {
41701ae08745Sheppo 		size = ldcp->mtu;
41711ae08745Sheppo 		rv = i_ldc_read_packet(ldcp,
41721ae08745Sheppo 		    (caddr_t)ldcp->stream_bufferp, &size);
41731ae08745Sheppo 		D2(ldcp->id, "i_ldc_read_stream: read packet (0x%llx) size=%d",
41741ae08745Sheppo 		    ldcp->id, size);
41751ae08745Sheppo 
41761ae08745Sheppo 		if (rv != 0)
41771ae08745Sheppo 			return (rv);
41781ae08745Sheppo 
41791ae08745Sheppo 		ldcp->stream_remains = size;
41801ae08745Sheppo 		ldcp->stream_offset = 0;
41811ae08745Sheppo 	}
41821ae08745Sheppo 
41831ae08745Sheppo 	size = MIN(ldcp->stream_remains, *sizep);
41841ae08745Sheppo 
41851ae08745Sheppo 	bcopy(ldcp->stream_bufferp + ldcp->stream_offset, target_bufp, size);
41861ae08745Sheppo 	ldcp->stream_offset += size;
41871ae08745Sheppo 	ldcp->stream_remains -= size;
41881ae08745Sheppo 
41891ae08745Sheppo 	D2(ldcp->id, "i_ldc_read_stream: (0x%llx) fill from buffer size=%d",
41901ae08745Sheppo 	    ldcp->id, size);
41911ae08745Sheppo 
41921ae08745Sheppo 	*sizep = size;
41931ae08745Sheppo 	return (0);
41941ae08745Sheppo }
41951ae08745Sheppo 
41961ae08745Sheppo /*
41971ae08745Sheppo  * Write specified amount of bytes to the channel
41981ae08745Sheppo  * in multiple pkts of pkt_payload size. Each
41991ae08745Sheppo  * packet is tagged with an unique packet ID in
4200e1ebb9ecSlm66018  * the case of a reliable link.
42011ae08745Sheppo  *
42021ae08745Sheppo  * On return, size contains the number of bytes written.
42031ae08745Sheppo  */
42041ae08745Sheppo int
42051ae08745Sheppo ldc_write(ldc_handle_t handle, caddr_t buf, size_t *sizep)
42061ae08745Sheppo {
42071ae08745Sheppo 	ldc_chan_t	*ldcp;
42081ae08745Sheppo 	int		rv = 0;
42091ae08745Sheppo 
42101ae08745Sheppo 	if (handle == NULL) {
42111ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_write: invalid channel handle\n");
42121ae08745Sheppo 		return (EINVAL);
42131ae08745Sheppo 	}
42141ae08745Sheppo 	ldcp = (ldc_chan_t *)handle;
42151ae08745Sheppo 
4216d10e4ef2Snarayan 	/* check if writes can occur */
4217d10e4ef2Snarayan 	if (!mutex_tryenter(&ldcp->tx_lock)) {
4218d10e4ef2Snarayan 		/*
4219d10e4ef2Snarayan 		 * Could not get the lock - channel could
4220d10e4ef2Snarayan 		 * be in the process of being unconfigured
4221d10e4ef2Snarayan 		 * or reader has encountered an error
4222d10e4ef2Snarayan 		 */
4223d10e4ef2Snarayan 		return (EAGAIN);
4224d10e4ef2Snarayan 	}
42251ae08745Sheppo 
42261ae08745Sheppo 	/* check if non-zero data to write */
42271ae08745Sheppo 	if (buf == NULL || sizep == NULL) {
42281ae08745Sheppo 		DWARN(ldcp->id, "ldc_write: (0x%llx) invalid data write\n",
42291ae08745Sheppo 		    ldcp->id);
4230d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
42311ae08745Sheppo 		return (EINVAL);
42321ae08745Sheppo 	}
42331ae08745Sheppo 
42341ae08745Sheppo 	if (*sizep == 0) {
42351ae08745Sheppo 		DWARN(ldcp->id, "ldc_write: (0x%llx) write size of zero\n",
42361ae08745Sheppo 		    ldcp->id);
4237d10e4ef2Snarayan 		mutex_exit(&ldcp->tx_lock);
42381ae08745Sheppo 		return (0);
42391ae08745Sheppo 	}
42401ae08745Sheppo 
42411ae08745Sheppo 	/* Check if channel is UP for data exchange */
42421ae08745Sheppo 	if (ldcp->tstate != TS_UP) {
42431ae08745Sheppo 		DWARN(ldcp->id,
42441ae08745Sheppo 		    "ldc_write: (0x%llx) channel is not in UP state\n",
42451ae08745Sheppo 		    ldcp->id);
42461ae08745Sheppo 		*sizep = 0;
42471ae08745Sheppo 		rv = ECONNRESET;
42481ae08745Sheppo 	} else {
42491ae08745Sheppo 		rv = ldcp->write_p(ldcp, buf, sizep);
42501ae08745Sheppo 	}
42511ae08745Sheppo 
4252d10e4ef2Snarayan 	mutex_exit(&ldcp->tx_lock);
42531ae08745Sheppo 
42541ae08745Sheppo 	return (rv);
42551ae08745Sheppo }
42561ae08745Sheppo 
42571ae08745Sheppo /*
42581ae08745Sheppo  * Write a raw packet to the channel
42591ae08745Sheppo  * On return, size contains the number of bytes written.
42601ae08745Sheppo  */
42611ae08745Sheppo static int
42621ae08745Sheppo i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep)
42631ae08745Sheppo {
42641ae08745Sheppo 	ldc_msg_t 	*ldcmsg;
42651ae08745Sheppo 	uint64_t 	tx_head, tx_tail, new_tail;
42661ae08745Sheppo 	int		rv = 0;
42671ae08745Sheppo 	size_t		size;
42681ae08745Sheppo 
4269d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
42701ae08745Sheppo 	ASSERT(ldcp->mode == LDC_MODE_RAW);
42711ae08745Sheppo 
42721ae08745Sheppo 	size = *sizep;
42731ae08745Sheppo 
42741ae08745Sheppo 	/*
42751ae08745Sheppo 	 * Check to see if the packet size is less than or
42761ae08745Sheppo 	 * equal to packet size support in raw mode
42771ae08745Sheppo 	 */
42781ae08745Sheppo 	if (size > ldcp->pkt_payload) {
42791ae08745Sheppo 		DWARN(ldcp->id,
42801ae08745Sheppo 		    "ldc_write: (0x%llx) invalid size (0x%llx) for RAW mode\n",
42811ae08745Sheppo 		    ldcp->id, *sizep);
42821ae08745Sheppo 		*sizep = 0;
42831ae08745Sheppo 		return (EMSGSIZE);
42841ae08745Sheppo 	}
42851ae08745Sheppo 
42861ae08745Sheppo 	/* get the qptrs for the tx queue */
42871ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
42881ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
42891ae08745Sheppo 	if (rv != 0) {
42901ae08745Sheppo 		cmn_err(CE_WARN,
42911ae08745Sheppo 		    "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id);
42921ae08745Sheppo 		*sizep = 0;
42931ae08745Sheppo 		return (EIO);
42941ae08745Sheppo 	}
42951ae08745Sheppo 
42961ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_DOWN ||
42971ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
42981ae08745Sheppo 		DWARN(ldcp->id,
42991ae08745Sheppo 		    "ldc_write: (0x%llx) channel down/reset\n", ldcp->id);
4300d10e4ef2Snarayan 
43011ae08745Sheppo 		*sizep = 0;
4302d10e4ef2Snarayan 		if (mutex_tryenter(&ldcp->lock)) {
43033af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
4304d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
4305d10e4ef2Snarayan 		} else {
4306d10e4ef2Snarayan 			/*
4307d10e4ef2Snarayan 			 * Release Tx lock, and then reacquire channel
4308d10e4ef2Snarayan 			 * and Tx lock in correct order
4309d10e4ef2Snarayan 			 */
4310d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
4311d10e4ef2Snarayan 			mutex_enter(&ldcp->lock);
4312d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
43133af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
4314d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
4315d10e4ef2Snarayan 		}
43161ae08745Sheppo 		return (ECONNRESET);
43171ae08745Sheppo 	}
43181ae08745Sheppo 
43191ae08745Sheppo 	tx_tail = ldcp->tx_tail;
43201ae08745Sheppo 	tx_head = ldcp->tx_head;
43211ae08745Sheppo 	new_tail = (tx_tail + LDC_PACKET_SIZE) &
43221ae08745Sheppo 	    ((ldcp->tx_q_entries-1) << LDC_PACKET_SHIFT);
43231ae08745Sheppo 
43241ae08745Sheppo 	if (new_tail == tx_head) {
43251ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
43261ae08745Sheppo 		    "ldc_write: (0x%llx) TX queue is full\n", ldcp->id);
43271ae08745Sheppo 		*sizep = 0;
43281ae08745Sheppo 		return (EWOULDBLOCK);
43291ae08745Sheppo 	}
43301ae08745Sheppo 
43311ae08745Sheppo 	D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d",
43321ae08745Sheppo 	    ldcp->id, size);
43331ae08745Sheppo 
43341ae08745Sheppo 	/* Send the data now */
43351ae08745Sheppo 	ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
43361ae08745Sheppo 
43371ae08745Sheppo 	/* copy the data into pkt */
43381ae08745Sheppo 	bcopy((uint8_t *)buf, ldcmsg, size);
43391ae08745Sheppo 
43401ae08745Sheppo 	/* increment tail */
43411ae08745Sheppo 	tx_tail = new_tail;
43421ae08745Sheppo 
43431ae08745Sheppo 	/*
43441ae08745Sheppo 	 * All packets have been copied into the TX queue
43451ae08745Sheppo 	 * update the tail ptr in the HV
43461ae08745Sheppo 	 */
43471ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
43481ae08745Sheppo 	if (rv) {
43491ae08745Sheppo 		if (rv == EWOULDBLOCK) {
43501ae08745Sheppo 			DWARN(ldcp->id, "ldc_write: (0x%llx) write timed out\n",
43511ae08745Sheppo 			    ldcp->id);
43521ae08745Sheppo 			*sizep = 0;
43531ae08745Sheppo 			return (EWOULDBLOCK);
43541ae08745Sheppo 		}
43551ae08745Sheppo 
43561ae08745Sheppo 		*sizep = 0;
4357d10e4ef2Snarayan 		if (mutex_tryenter(&ldcp->lock)) {
43583af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
4359d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
4360d10e4ef2Snarayan 		} else {
4361d10e4ef2Snarayan 			/*
4362d10e4ef2Snarayan 			 * Release Tx lock, and then reacquire channel
4363d10e4ef2Snarayan 			 * and Tx lock in correct order
4364d10e4ef2Snarayan 			 */
4365d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
4366d10e4ef2Snarayan 			mutex_enter(&ldcp->lock);
4367d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
43683af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
4369d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
4370d10e4ef2Snarayan 		}
43711ae08745Sheppo 		return (ECONNRESET);
43721ae08745Sheppo 	}
43731ae08745Sheppo 
43741ae08745Sheppo 	ldcp->tx_tail = tx_tail;
43751ae08745Sheppo 	*sizep = size;
43761ae08745Sheppo 
43771ae08745Sheppo 	D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, size);
43781ae08745Sheppo 
43791ae08745Sheppo 	return (rv);
43801ae08745Sheppo }
43811ae08745Sheppo 
43821ae08745Sheppo 
43831ae08745Sheppo /*
43841ae08745Sheppo  * Write specified amount of bytes to the channel
43851ae08745Sheppo  * in multiple pkts of pkt_payload size. Each
43861ae08745Sheppo  * packet is tagged with an unique packet ID in
4387e1ebb9ecSlm66018  * the case of a reliable link.
43881ae08745Sheppo  *
43891ae08745Sheppo  * On return, size contains the number of bytes written.
43901ae08745Sheppo  * This function needs to ensure that the write size is < MTU size
43911ae08745Sheppo  */
43921ae08745Sheppo static int
43931ae08745Sheppo i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t buf, size_t *size)
43941ae08745Sheppo {
43951ae08745Sheppo 	ldc_msg_t 	*ldcmsg;
43961ae08745Sheppo 	uint64_t 	tx_head, tx_tail, new_tail, start;
43971ae08745Sheppo 	uint64_t	txq_size_mask, numavail;
43981ae08745Sheppo 	uint8_t 	*msgbuf, *source = (uint8_t *)buf;
43991ae08745Sheppo 	size_t 		len, bytes_written = 0, remaining;
44001ae08745Sheppo 	int		rv;
44011ae08745Sheppo 	uint32_t	curr_seqid;
44021ae08745Sheppo 
4403d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
44041ae08745Sheppo 
44051ae08745Sheppo 	ASSERT(ldcp->mode == LDC_MODE_RELIABLE ||
440620ae46ebSha137994 	    ldcp->mode == LDC_MODE_UNRELIABLE);
44071ae08745Sheppo 
44081ae08745Sheppo 	/* compute mask for increment */
44091ae08745Sheppo 	txq_size_mask = (ldcp->tx_q_entries - 1) << LDC_PACKET_SHIFT;
44101ae08745Sheppo 
44111ae08745Sheppo 	/* get the qptrs for the tx queue */
44121ae08745Sheppo 	rv = hv_ldc_tx_get_state(ldcp->id,
44131ae08745Sheppo 	    &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state);
44141ae08745Sheppo 	if (rv != 0) {
44151ae08745Sheppo 		cmn_err(CE_WARN,
44161ae08745Sheppo 		    "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id);
44171ae08745Sheppo 		*size = 0;
44181ae08745Sheppo 		return (EIO);
44191ae08745Sheppo 	}
44201ae08745Sheppo 
44211ae08745Sheppo 	if (ldcp->link_state == LDC_CHANNEL_DOWN ||
44221ae08745Sheppo 	    ldcp->link_state == LDC_CHANNEL_RESET) {
44231ae08745Sheppo 		DWARN(ldcp->id,
44241ae08745Sheppo 		    "ldc_write: (0x%llx) channel down/reset\n", ldcp->id);
44251ae08745Sheppo 		*size = 0;
4426d10e4ef2Snarayan 		if (mutex_tryenter(&ldcp->lock)) {
44273af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
4428d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
4429d10e4ef2Snarayan 		} else {
4430d10e4ef2Snarayan 			/*
4431d10e4ef2Snarayan 			 * Release Tx lock, and then reacquire channel
4432d10e4ef2Snarayan 			 * and Tx lock in correct order
4433d10e4ef2Snarayan 			 */
4434d10e4ef2Snarayan 			mutex_exit(&ldcp->tx_lock);
4435d10e4ef2Snarayan 			mutex_enter(&ldcp->lock);
4436d10e4ef2Snarayan 			mutex_enter(&ldcp->tx_lock);
44373af08d82Slm66018 			i_ldc_reset(ldcp, B_FALSE);
4438d10e4ef2Snarayan 			mutex_exit(&ldcp->lock);
4439d10e4ef2Snarayan 		}
44401ae08745Sheppo 		return (ECONNRESET);
44411ae08745Sheppo 	}
44421ae08745Sheppo 
44431ae08745Sheppo 	tx_tail = ldcp->tx_tail;
44441ae08745Sheppo 	new_tail = (tx_tail + LDC_PACKET_SIZE) %
44451ae08745Sheppo 	    (ldcp->tx_q_entries << LDC_PACKET_SHIFT);
44461ae08745Sheppo 
44471ae08745Sheppo 	/*
444822f747efSnarayan 	 * Check to see if the queue is full. The check is done using
444922f747efSnarayan 	 * the appropriate head based on the link mode.
44501ae08745Sheppo 	 */
445122f747efSnarayan 	i_ldc_get_tx_head(ldcp, &tx_head);
445222f747efSnarayan 
44531ae08745Sheppo 	if (new_tail == tx_head) {
44541ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
44551ae08745Sheppo 		    "ldc_write: (0x%llx) TX queue is full\n", ldcp->id);
44561ae08745Sheppo 		*size = 0;
44571ae08745Sheppo 		return (EWOULDBLOCK);
44581ae08745Sheppo 	}
44591ae08745Sheppo 
44601ae08745Sheppo 	/*
44611ae08745Sheppo 	 * Make sure that the LDC Tx queue has enough space
44621ae08745Sheppo 	 */
44631ae08745Sheppo 	numavail = (tx_head >> LDC_PACKET_SHIFT) - (tx_tail >> LDC_PACKET_SHIFT)
44641ae08745Sheppo 	    + ldcp->tx_q_entries - 1;
44651ae08745Sheppo 	numavail %= ldcp->tx_q_entries;
44661ae08745Sheppo 
44671ae08745Sheppo 	if (*size > (numavail * ldcp->pkt_payload)) {
44681ae08745Sheppo 		DWARN(DBG_ALL_LDCS,
44691ae08745Sheppo 		    "ldc_write: (0x%llx) TX queue has no space\n", ldcp->id);
44701ae08745Sheppo 		return (EWOULDBLOCK);
44711ae08745Sheppo 	}
44721ae08745Sheppo 
44731ae08745Sheppo 	D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d",
44741ae08745Sheppo 	    ldcp->id, *size);
44751ae08745Sheppo 
44761ae08745Sheppo 	/* Send the data now */
44771ae08745Sheppo 	bytes_written = 0;
44781ae08745Sheppo 	curr_seqid = ldcp->last_msg_snt;
44791ae08745Sheppo 	start = tx_tail;
44801ae08745Sheppo 
44811ae08745Sheppo 	while (*size > bytes_written) {
44821ae08745Sheppo 
44831ae08745Sheppo 		ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail);
44841ae08745Sheppo 
448520ae46ebSha137994 		msgbuf = (uint8_t *)((ldcp->mode == LDC_MODE_RELIABLE) ?
448622f747efSnarayan 		    ldcmsg->rdata : ldcmsg->udata);
44871ae08745Sheppo 
44881ae08745Sheppo 		ldcmsg->type = LDC_DATA;
44891ae08745Sheppo 		ldcmsg->stype = LDC_INFO;
44901ae08745Sheppo 		ldcmsg->ctrl = 0;
44911ae08745Sheppo 
44921ae08745Sheppo 		remaining = *size - bytes_written;
44931ae08745Sheppo 		len = min(ldcp->pkt_payload, remaining);
44941ae08745Sheppo 		ldcmsg->env = (uint8_t)len;
44951ae08745Sheppo 
44961ae08745Sheppo 		curr_seqid++;
44971ae08745Sheppo 		ldcmsg->seqid = curr_seqid;
44981ae08745Sheppo 
44991ae08745Sheppo 		/* copy the data into pkt */
45001ae08745Sheppo 		bcopy(source, msgbuf, len);
45011ae08745Sheppo 
45021ae08745Sheppo 		source += len;
45031ae08745Sheppo 		bytes_written += len;
45041ae08745Sheppo 
45051ae08745Sheppo 		/* increment tail */
45061ae08745Sheppo 		tx_tail = (tx_tail + LDC_PACKET_SIZE) & txq_size_mask;
45071ae08745Sheppo 
45081ae08745Sheppo 		ASSERT(tx_tail != tx_head);
45091ae08745Sheppo 	}
45101ae08745Sheppo 
45111ae08745Sheppo 	/* Set the start and stop bits */
45121ae08745Sheppo 	ldcmsg->env |= LDC_FRAG_STOP;
45131ae08745Sheppo 	ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + start);
45141ae08745Sheppo 	ldcmsg->env |= LDC_FRAG_START;
45151ae08745Sheppo 
45161ae08745Sheppo 	/*
45171ae08745Sheppo 	 * All packets have been copied into the TX queue
45181ae08745Sheppo 	 * update the tail ptr in the HV
45191ae08745Sheppo 	 */
45201ae08745Sheppo 	rv = i_ldc_set_tx_tail(ldcp, tx_tail);
45211ae08745Sheppo 	if (rv == 0) {
45221ae08745Sheppo 		ldcp->tx_tail = tx_tail;
45231ae08745Sheppo 		ldcp->last_msg_snt = curr_seqid;
45241ae08745Sheppo 		*size = bytes_written;
45251ae08745Sheppo 	} else {
45261ae08745Sheppo 		int rv2;
45271ae08745Sheppo 
45281ae08745Sheppo 		if (rv != EWOULDBLOCK) {
45291ae08745Sheppo 			*size = 0;
4530d10e4ef2Snarayan 			if (mutex_tryenter(&ldcp->lock)) {
45313af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
4532d10e4ef2Snarayan 				mutex_exit(&ldcp->lock);
4533d10e4ef2Snarayan 			} else {
4534d10e4ef2Snarayan 				/*
4535d10e4ef2Snarayan 				 * Release Tx lock, and then reacquire channel
4536d10e4ef2Snarayan 				 * and Tx lock in correct order
4537d10e4ef2Snarayan 				 */
4538d10e4ef2Snarayan 				mutex_exit(&ldcp->tx_lock);
4539d10e4ef2Snarayan 				mutex_enter(&ldcp->lock);
4540d10e4ef2Snarayan 				mutex_enter(&ldcp->tx_lock);
45413af08d82Slm66018 				i_ldc_reset(ldcp, B_FALSE);
4542d10e4ef2Snarayan 				mutex_exit(&ldcp->lock);
4543d10e4ef2Snarayan 			}
45441ae08745Sheppo 			return (ECONNRESET);
45451ae08745Sheppo 		}
45461ae08745Sheppo 
4547cb112a14Slm66018 		D1(ldcp->id, "hv_tx_set_tail returns 0x%x (head 0x%x, "
45481ae08745Sheppo 		    "old tail 0x%x, new tail 0x%x, qsize=0x%x)\n",
45491ae08745Sheppo 		    rv, ldcp->tx_head, ldcp->tx_tail, tx_tail,
45501ae08745Sheppo 		    (ldcp->tx_q_entries << LDC_PACKET_SHIFT));
45511ae08745Sheppo 
45521ae08745Sheppo 		rv2 = hv_ldc_tx_get_state(ldcp->id,
45531ae08745Sheppo 		    &tx_head, &tx_tail, &ldcp->link_state);
45541ae08745Sheppo 
4555cb112a14Slm66018 		D1(ldcp->id, "hv_ldc_tx_get_state returns 0x%x "
45561ae08745Sheppo 		    "(head 0x%x, tail 0x%x state 0x%x)\n",
45571ae08745Sheppo 		    rv2, tx_head, tx_tail, ldcp->link_state);
45581ae08745Sheppo 
45591ae08745Sheppo 		*size = 0;
45601ae08745Sheppo 	}
45611ae08745Sheppo 
45621ae08745Sheppo 	D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, *size);
45631ae08745Sheppo 
45641ae08745Sheppo 	return (rv);
45651ae08745Sheppo }
45661ae08745Sheppo 
45671ae08745Sheppo /*
45681ae08745Sheppo  * Write specified amount of bytes to the channel
45691ae08745Sheppo  * in multiple pkts of pkt_payload size. Each
45701ae08745Sheppo  * packet is tagged with an unique packet ID in
4571e1ebb9ecSlm66018  * the case of a reliable link.
45721ae08745Sheppo  *
45731ae08745Sheppo  * On return, size contains the number of bytes written.
45741ae08745Sheppo  * This function needs to ensure that the write size is < MTU size
45751ae08745Sheppo  */
45761ae08745Sheppo static int
45771ae08745Sheppo i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep)
45781ae08745Sheppo {
4579d10e4ef2Snarayan 	ASSERT(MUTEX_HELD(&ldcp->tx_lock));
458020ae46ebSha137994 	ASSERT(ldcp->mode == LDC_MODE_RELIABLE);
45811ae08745Sheppo 
45821ae08745Sheppo 	/* Truncate packet to max of MTU size */
45831ae08745Sheppo 	if (*sizep > ldcp->mtu) *sizep = ldcp->mtu;
45841ae08745Sheppo 	return (i_ldc_write_packet(ldcp, buf, sizep));
45851ae08745Sheppo }
45861ae08745Sheppo 
45871ae08745Sheppo 
45881ae08745Sheppo /*
45891ae08745Sheppo  * Interfaces for channel nexus to register/unregister with LDC module
45901ae08745Sheppo  * The nexus will register functions to be used to register individual
45911ae08745Sheppo  * channels with the nexus and enable interrupts for the channels
45921ae08745Sheppo  */
45931ae08745Sheppo int
45941ae08745Sheppo ldc_register(ldc_cnex_t *cinfo)
45951ae08745Sheppo {
45961ae08745Sheppo 	ldc_chan_t	*ldcp;
45971ae08745Sheppo 
45981ae08745Sheppo 	if (cinfo == NULL || cinfo->dip == NULL ||
45991ae08745Sheppo 	    cinfo->reg_chan == NULL || cinfo->unreg_chan == NULL ||
46001ae08745Sheppo 	    cinfo->add_intr == NULL || cinfo->rem_intr == NULL ||
46011ae08745Sheppo 	    cinfo->clr_intr == NULL) {
46021ae08745Sheppo 
46031ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_register: invalid nexus info\n");
46041ae08745Sheppo 		return (EINVAL);
46051ae08745Sheppo 	}
46061ae08745Sheppo 
46071ae08745Sheppo 	mutex_enter(&ldcssp->lock);
46081ae08745Sheppo 
46091ae08745Sheppo 	/* nexus registration */
46101ae08745Sheppo 	ldcssp->cinfo.dip = cinfo->dip;
46111ae08745Sheppo 	ldcssp->cinfo.reg_chan = cinfo->reg_chan;
46121ae08745Sheppo 	ldcssp->cinfo.unreg_chan = cinfo->unreg_chan;
46131ae08745Sheppo 	ldcssp->cinfo.add_intr = cinfo->add_intr;
46141ae08745Sheppo 	ldcssp->cinfo.rem_intr = cinfo->rem_intr;
46151ae08745Sheppo 	ldcssp->cinfo.clr_intr = cinfo->clr_intr;
46161ae08745Sheppo 
46171ae08745Sheppo 	/* register any channels that might have been previously initialized */
46181ae08745Sheppo 	ldcp = ldcssp->chan_list;
46191ae08745Sheppo 	while (ldcp) {
46201ae08745Sheppo 		if ((ldcp->tstate & TS_QCONF_RDY) &&
46211ae08745Sheppo 		    (ldcp->tstate & TS_CNEX_RDY) == 0)
46221ae08745Sheppo 			(void) i_ldc_register_channel(ldcp);
46231ae08745Sheppo 
46241ae08745Sheppo 		ldcp = ldcp->next;
46251ae08745Sheppo 	}
46261ae08745Sheppo 
46271ae08745Sheppo 	mutex_exit(&ldcssp->lock);
46281ae08745Sheppo 
46291ae08745Sheppo 	return (0);
46301ae08745Sheppo }
46311ae08745Sheppo 
46321ae08745Sheppo int
46331ae08745Sheppo ldc_unregister(ldc_cnex_t *cinfo)
46341ae08745Sheppo {
46351ae08745Sheppo 	if (cinfo == NULL || cinfo->dip == NULL) {
46361ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid nexus info\n");
46371ae08745Sheppo 		return (EINVAL);
46381ae08745Sheppo 	}
46391ae08745Sheppo 
46401ae08745Sheppo 	mutex_enter(&ldcssp->lock);
46411ae08745Sheppo 
46421ae08745Sheppo 	if (cinfo->dip != ldcssp->cinfo.dip) {
46431ae08745Sheppo 		DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid dip\n");
46441ae08745Sheppo 		mutex_exit(&ldcssp->lock);
46451ae08745Sheppo 		return (EINVAL);
46461ae08745Sheppo 	}
46471ae08745Sheppo 
46481ae08745Sheppo 	/* nexus unregister */
46491ae08745Sheppo 	ldcssp->cinfo.dip = NULL;
46501ae08745Sheppo 	ldcssp->cinfo.reg_chan = NULL;
46511ae08745Sheppo 	ldcssp->cinfo.unreg_chan = NULL;
46521ae08745Sheppo 	ldcssp->cinfo.add_intr = NULL;
46531ae08745Sheppo 	ldcssp->cinfo.rem_intr = NULL;
46541ae08745Sheppo 	ldcssp->cinfo.clr_intr = NULL;
46551ae08745Sheppo 
46561ae08745Sheppo 	mutex_exit(&ldcssp->lock);
46571ae08745Sheppo 
46581ae08745Sheppo 	return (0);
46591ae08745Sheppo }
4660