11ae08745Sheppo /* 21ae08745Sheppo * CDDL HEADER START 31ae08745Sheppo * 41ae08745Sheppo * The contents of this file are subject to the terms of the 51ae08745Sheppo * Common Development and Distribution License (the "License"). 61ae08745Sheppo * You may not use this file except in compliance with the License. 71ae08745Sheppo * 81ae08745Sheppo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91ae08745Sheppo * or http://www.opensolaris.org/os/licensing. 101ae08745Sheppo * See the License for the specific language governing permissions 111ae08745Sheppo * and limitations under the License. 121ae08745Sheppo * 131ae08745Sheppo * When distributing Covered Code, include this CDDL HEADER in each 141ae08745Sheppo * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151ae08745Sheppo * If applicable, add the following below this CDDL HEADER, with the 161ae08745Sheppo * fields enclosed by brackets "[]" replaced with your own identifying 171ae08745Sheppo * information: Portions Copyright [yyyy] [name of copyright owner] 181ae08745Sheppo * 191ae08745Sheppo * CDDL HEADER END 201ae08745Sheppo */ 211ae08745Sheppo 221ae08745Sheppo /* 23*5699897cSHaik Aftandilian * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 241ae08745Sheppo * Use is subject to license terms. 251ae08745Sheppo */ 261ae08745Sheppo 271ae08745Sheppo /* 28e1ebb9ecSlm66018 * sun4v LDC Link Layer 291ae08745Sheppo */ 301ae08745Sheppo #include <sys/types.h> 311ae08745Sheppo #include <sys/file.h> 321ae08745Sheppo #include <sys/errno.h> 331ae08745Sheppo #include <sys/open.h> 341ae08745Sheppo #include <sys/cred.h> 351ae08745Sheppo #include <sys/kmem.h> 361ae08745Sheppo #include <sys/conf.h> 371ae08745Sheppo #include <sys/cmn_err.h> 381ae08745Sheppo #include <sys/ksynch.h> 391ae08745Sheppo #include <sys/modctl.h> 401ae08745Sheppo #include <sys/stat.h> /* needed for S_IFBLK and S_IFCHR */ 411ae08745Sheppo #include <sys/debug.h> 421ae08745Sheppo #include <sys/cred.h> 431ae08745Sheppo #include <sys/promif.h> 441ae08745Sheppo #include <sys/ddi.h> 451ae08745Sheppo #include <sys/sunddi.h> 461ae08745Sheppo #include <sys/cyclic.h> 471ae08745Sheppo #include <sys/machsystm.h> 481ae08745Sheppo #include <sys/vm.h> 491ae08745Sheppo #include <sys/cpu.h> 501ae08745Sheppo #include <sys/intreg.h> 511ae08745Sheppo #include <sys/machcpuvar.h> 524bac2208Snarayan #include <sys/mmu.h> 534bac2208Snarayan #include <sys/pte.h> 544bac2208Snarayan #include <vm/hat.h> 554bac2208Snarayan #include <vm/as.h> 564bac2208Snarayan #include <vm/hat_sfmmu.h> 574bac2208Snarayan #include <sys/vm_machparam.h> 584bac2208Snarayan #include <vm/seg_kmem.h> 594bac2208Snarayan #include <vm/seg_kpm.h> 601ae08745Sheppo #include <sys/note.h> 611ae08745Sheppo #include <sys/ivintr.h> 621ae08745Sheppo #include <sys/hypervisor_api.h> 631ae08745Sheppo #include <sys/ldc.h> 641ae08745Sheppo #include <sys/ldc_impl.h> 651ae08745Sheppo #include <sys/cnex.h> 661ae08745Sheppo #include <sys/hsvc.h> 6758283286Sha137994 #include <sys/sdt.h> 68*5699897cSHaik Aftandilian #include <sys/kldc.h> 691ae08745Sheppo 701ae08745Sheppo /* Core internal functions */ 7120ae46ebSha137994 int i_ldc_h2v_error(int h_error); 7220ae46ebSha137994 void i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset); 7320ae46ebSha137994 741ae08745Sheppo static int i_ldc_txq_reconf(ldc_chan_t *ldcp); 753af08d82Slm66018 static int i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset); 76a8ea4edeSnarayan static int i_ldc_rxq_drain(ldc_chan_t *ldcp); 771ae08745Sheppo static void i_ldc_reset_state(ldc_chan_t *ldcp); 78*5699897cSHaik Aftandilian static void i_ldc_debug_enter(void); 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 */ 143f500b196SRichard Bean "sun4v LDC module", /* 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 /* 193*5699897cSHaik Aftandilian * Channels which have a devclass satisfying the following 194*5699897cSHaik Aftandilian * will be reset when entering the prom or kmdb. 195*5699897cSHaik Aftandilian * 196*5699897cSHaik Aftandilian * LDC_DEVCLASS_PROM_RESET(devclass) != 0 197*5699897cSHaik Aftandilian * 198*5699897cSHaik Aftandilian * By default, only block device service channels are reset. 199*5699897cSHaik Aftandilian */ 200*5699897cSHaik Aftandilian #define LDC_DEVCLASS_BIT(dc) (0x1 << (dc)) 201*5699897cSHaik Aftandilian #define LDC_DEVCLASS_PROM_RESET(dc) \ 202*5699897cSHaik Aftandilian (LDC_DEVCLASS_BIT(dc) & ldc_debug_reset_mask) 203*5699897cSHaik Aftandilian static uint64_t ldc_debug_reset_mask = LDC_DEVCLASS_BIT(LDC_DEV_BLK_SVC); 204*5699897cSHaik Aftandilian 205*5699897cSHaik Aftandilian /* 2064d39be2bSsg70180 * delay between each retry of channel unregistration in 2074d39be2bSsg70180 * ldc_close(), to wait for pending interrupts to complete. 2084d39be2bSsg70180 */ 2094d39be2bSsg70180 clock_t ldc_close_delay = LDC_CLOSE_DELAY; 2104d39be2bSsg70180 2111ae08745Sheppo #ifdef DEBUG 2121ae08745Sheppo 2131ae08745Sheppo /* 2141ae08745Sheppo * Print debug messages 2151ae08745Sheppo * 2161ae08745Sheppo * set ldcdbg to 0x7 for enabling all msgs 2171ae08745Sheppo * 0x4 - Warnings 2181ae08745Sheppo * 0x2 - All debug messages 2191ae08745Sheppo * 0x1 - Minimal debug messages 2201ae08745Sheppo * 2211ae08745Sheppo * set ldcdbgchan to the channel number you want to debug 2221ae08745Sheppo * setting it to -1 prints debug messages for all channels 2231ae08745Sheppo * NOTE: ldcdbgchan has no effect on error messages 2241ae08745Sheppo */ 2251ae08745Sheppo 2261ae08745Sheppo int ldcdbg = 0x0; 2271ae08745Sheppo int64_t ldcdbgchan = DBG_ALL_LDCS; 22883d3bc6fSnarayan uint64_t ldc_inject_err_flag = 0; 2291ae08745Sheppo 23020ae46ebSha137994 void 2311ae08745Sheppo ldcdebug(int64_t id, const char *fmt, ...) 2321ae08745Sheppo { 2331ae08745Sheppo char buf[512]; 2341ae08745Sheppo va_list ap; 2351ae08745Sheppo 2361ae08745Sheppo /* 2371ae08745Sheppo * Do not return if, 2381ae08745Sheppo * caller wants to print it anyway - (id == DBG_ALL_LDCS) 2391ae08745Sheppo * debug channel is set to all LDCs - (ldcdbgchan == DBG_ALL_LDCS) 2401ae08745Sheppo * debug channel = caller specified channel 2411ae08745Sheppo */ 2421ae08745Sheppo if ((id != DBG_ALL_LDCS) && 2431ae08745Sheppo (ldcdbgchan != DBG_ALL_LDCS) && 2441ae08745Sheppo (ldcdbgchan != id)) { 2451ae08745Sheppo return; 2461ae08745Sheppo } 2471ae08745Sheppo 2481ae08745Sheppo va_start(ap, fmt); 2491ae08745Sheppo (void) vsprintf(buf, fmt, ap); 2501ae08745Sheppo va_end(ap); 2511ae08745Sheppo 2523af08d82Slm66018 cmn_err(CE_CONT, "?%s", buf); 2533af08d82Slm66018 } 2543af08d82Slm66018 25583d3bc6fSnarayan #define LDC_ERR_RESET 0x1 25683d3bc6fSnarayan #define LDC_ERR_PKTLOSS 0x2 25758283286Sha137994 #define LDC_ERR_DQFULL 0x4 258bbfa0259Sha137994 #define LDC_ERR_DRNGCLEAR 0x8 25983d3bc6fSnarayan 2603af08d82Slm66018 static boolean_t 26183d3bc6fSnarayan ldc_inject_error(ldc_chan_t *ldcp, uint64_t error) 2623af08d82Slm66018 { 2633af08d82Slm66018 if ((ldcdbgchan != DBG_ALL_LDCS) && (ldcdbgchan != ldcp->id)) 2643af08d82Slm66018 return (B_FALSE); 2653af08d82Slm66018 26683d3bc6fSnarayan if ((ldc_inject_err_flag & error) == 0) 2673af08d82Slm66018 return (B_FALSE); 2683af08d82Slm66018 2693af08d82Slm66018 /* clear the injection state */ 27083d3bc6fSnarayan ldc_inject_err_flag &= ~error; 2713af08d82Slm66018 2723af08d82Slm66018 return (B_TRUE); 2731ae08745Sheppo } 2741ae08745Sheppo 2751ae08745Sheppo #define D1 \ 2761ae08745Sheppo if (ldcdbg & 0x01) \ 2771ae08745Sheppo ldcdebug 2781ae08745Sheppo 2791ae08745Sheppo #define D2 \ 2801ae08745Sheppo if (ldcdbg & 0x02) \ 2811ae08745Sheppo ldcdebug 2821ae08745Sheppo 2831ae08745Sheppo #define DWARN \ 2841ae08745Sheppo if (ldcdbg & 0x04) \ 2851ae08745Sheppo ldcdebug 2861ae08745Sheppo 2871ae08745Sheppo #define DUMP_PAYLOAD(id, addr) \ 2881ae08745Sheppo { \ 2891ae08745Sheppo char buf[65*3]; \ 2901ae08745Sheppo int i; \ 2911ae08745Sheppo uint8_t *src = (uint8_t *)addr; \ 2921ae08745Sheppo for (i = 0; i < 64; i++, src++) \ 2931ae08745Sheppo (void) sprintf(&buf[i * 3], "|%02x", *src); \ 2941ae08745Sheppo (void) sprintf(&buf[i * 3], "|\n"); \ 2951ae08745Sheppo D2((id), "payload: %s", buf); \ 2961ae08745Sheppo } 2971ae08745Sheppo 2981ae08745Sheppo #define DUMP_LDC_PKT(c, s, addr) \ 2991ae08745Sheppo { \ 3001ae08745Sheppo ldc_msg_t *msg = (ldc_msg_t *)(addr); \ 3011ae08745Sheppo uint32_t mid = ((c)->mode != LDC_MODE_RAW) ? msg->seqid : 0; \ 3021ae08745Sheppo if (msg->type == LDC_DATA) { \ 3031ae08745Sheppo D2((c)->id, "%s: msg%d (/%x/%x/%x/,env[%c%c,sz=%d])", \ 3041ae08745Sheppo (s), mid, msg->type, msg->stype, msg->ctrl, \ 3051ae08745Sheppo (msg->env & LDC_FRAG_START) ? 'B' : ' ', \ 3061ae08745Sheppo (msg->env & LDC_FRAG_STOP) ? 'E' : ' ', \ 3071ae08745Sheppo (msg->env & LDC_LEN_MASK)); \ 3081ae08745Sheppo } else { \ 3091ae08745Sheppo D2((c)->id, "%s: msg%d (/%x/%x/%x/,env=%x)", (s), \ 3101ae08745Sheppo mid, msg->type, msg->stype, msg->ctrl, msg->env); \ 3111ae08745Sheppo } \ 3121ae08745Sheppo } 3131ae08745Sheppo 31483d3bc6fSnarayan #define LDC_INJECT_RESET(_ldcp) ldc_inject_error(_ldcp, LDC_ERR_RESET) 31583d3bc6fSnarayan #define LDC_INJECT_PKTLOSS(_ldcp) ldc_inject_error(_ldcp, LDC_ERR_PKTLOSS) 31658283286Sha137994 #define LDC_INJECT_DQFULL(_ldcp) ldc_inject_error(_ldcp, LDC_ERR_DQFULL) 317bbfa0259Sha137994 #define LDC_INJECT_DRNGCLEAR(_ldcp) ldc_inject_error(_ldcp, LDC_ERR_DRNGCLEAR) 318bbfa0259Sha137994 extern void i_ldc_mem_inject_dring_clear(ldc_chan_t *ldcp); 3193af08d82Slm66018 3201ae08745Sheppo #else 3211ae08745Sheppo 3221ae08745Sheppo #define DBG_ALL_LDCS -1 3231ae08745Sheppo 3241ae08745Sheppo #define D1 3251ae08745Sheppo #define D2 3261ae08745Sheppo #define DWARN 3271ae08745Sheppo 3281ae08745Sheppo #define DUMP_PAYLOAD(id, addr) 3291ae08745Sheppo #define DUMP_LDC_PKT(c, s, addr) 3301ae08745Sheppo 3313af08d82Slm66018 #define LDC_INJECT_RESET(_ldcp) (B_FALSE) 33283d3bc6fSnarayan #define LDC_INJECT_PKTLOSS(_ldcp) (B_FALSE) 33358283286Sha137994 #define LDC_INJECT_DQFULL(_ldcp) (B_FALSE) 334bbfa0259Sha137994 #define LDC_INJECT_DRNGCLEAR(_ldcp) (B_FALSE) 3353af08d82Slm66018 3361ae08745Sheppo #endif 3371ae08745Sheppo 33858283286Sha137994 /* 33958283286Sha137994 * dtrace SDT probes to ease tracing of the rx data queue and HV queue 34058283286Sha137994 * lengths. Just pass the head, tail, and entries values so that the 34158283286Sha137994 * length can be calculated in a dtrace script when the probe is enabled. 34258283286Sha137994 */ 34358283286Sha137994 #define TRACE_RXDQ_LENGTH(ldcp) \ 34458283286Sha137994 DTRACE_PROBE4(rxdq__size, \ 34558283286Sha137994 uint64_t, ldcp->id, \ 34658283286Sha137994 uint64_t, ldcp->rx_dq_head, \ 34758283286Sha137994 uint64_t, ldcp->rx_dq_tail, \ 34858283286Sha137994 uint64_t, ldcp->rx_dq_entries) 34958283286Sha137994 35058283286Sha137994 #define TRACE_RXHVQ_LENGTH(ldcp, head, tail) \ 35158283286Sha137994 DTRACE_PROBE4(rxhvq__size, \ 35258283286Sha137994 uint64_t, ldcp->id, \ 35358283286Sha137994 uint64_t, head, \ 35458283286Sha137994 uint64_t, tail, \ 35558283286Sha137994 uint64_t, ldcp->rx_q_entries) 35658283286Sha137994 35758283286Sha137994 /* A dtrace SDT probe to ease tracing of data queue copy operations */ 35858283286Sha137994 #define TRACE_RXDQ_COPY(ldcp, bytes) \ 35958283286Sha137994 DTRACE_PROBE2(rxdq__copy, uint64_t, ldcp->id, uint64_t, bytes) \ 36058283286Sha137994 36158283286Sha137994 /* The amount of contiguous space at the tail of the queue */ 36258283286Sha137994 #define Q_CONTIG_SPACE(head, tail, size) \ 36358283286Sha137994 ((head) <= (tail) ? ((size) - (tail)) : \ 36458283286Sha137994 ((head) - (tail) - LDC_PACKET_SIZE)) 36558283286Sha137994 3661ae08745Sheppo #define ZERO_PKT(p) \ 3671ae08745Sheppo bzero((p), sizeof (ldc_msg_t)); 3681ae08745Sheppo 3691ae08745Sheppo #define IDX2COOKIE(idx, pg_szc, pg_shift) \ 3701ae08745Sheppo (((pg_szc) << LDC_COOKIE_PGSZC_SHIFT) | ((idx) << (pg_shift))) 3711ae08745Sheppo 3721ae08745Sheppo int 3731ae08745Sheppo _init(void) 3741ae08745Sheppo { 3751ae08745Sheppo int status; 376bbfa0259Sha137994 extern void i_ldc_mem_set_hsvc_vers(uint64_t major, uint64_t minor); 3771ae08745Sheppo 3781ae08745Sheppo status = hsvc_register(&ldc_hsvc, &ldc_sup_minor); 3791ae08745Sheppo if (status != 0) { 380d66f8315Sjb145095 cmn_err(CE_NOTE, "!%s: cannot negotiate hypervisor LDC services" 3811ae08745Sheppo " group: 0x%lx major: %ld minor: %ld errno: %d", 3821ae08745Sheppo ldc_hsvc.hsvc_modname, ldc_hsvc.hsvc_group, 3831ae08745Sheppo ldc_hsvc.hsvc_major, ldc_hsvc.hsvc_minor, status); 3841ae08745Sheppo return (-1); 3851ae08745Sheppo } 3861ae08745Sheppo 387bbfa0259Sha137994 /* Initialize shared memory HV API version checking */ 388bbfa0259Sha137994 i_ldc_mem_set_hsvc_vers(ldc_hsvc.hsvc_major, ldc_sup_minor); 389bbfa0259Sha137994 3901ae08745Sheppo /* allocate soft state structure */ 3911ae08745Sheppo ldcssp = kmem_zalloc(sizeof (ldc_soft_state_t), KM_SLEEP); 3921ae08745Sheppo 3931ae08745Sheppo /* Link the module into the system */ 3941ae08745Sheppo status = mod_install(&ml); 3951ae08745Sheppo if (status != 0) { 3961ae08745Sheppo kmem_free(ldcssp, sizeof (ldc_soft_state_t)); 3971ae08745Sheppo return (status); 3981ae08745Sheppo } 3991ae08745Sheppo 4001ae08745Sheppo /* Initialize the LDC state structure */ 4011ae08745Sheppo mutex_init(&ldcssp->lock, NULL, MUTEX_DRIVER, NULL); 4021ae08745Sheppo 4031ae08745Sheppo mutex_enter(&ldcssp->lock); 4041ae08745Sheppo 4054bac2208Snarayan /* Create a cache for memory handles */ 4064bac2208Snarayan ldcssp->memhdl_cache = kmem_cache_create("ldc_memhdl_cache", 4074bac2208Snarayan sizeof (ldc_mhdl_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 4084bac2208Snarayan if (ldcssp->memhdl_cache == NULL) { 4094bac2208Snarayan DWARN(DBG_ALL_LDCS, "_init: ldc_memhdl cache create failed\n"); 4104bac2208Snarayan mutex_exit(&ldcssp->lock); 4114bac2208Snarayan return (-1); 4124bac2208Snarayan } 4134bac2208Snarayan 4144bac2208Snarayan /* Create cache for memory segment structures */ 4154bac2208Snarayan ldcssp->memseg_cache = kmem_cache_create("ldc_memseg_cache", 4164bac2208Snarayan sizeof (ldc_memseg_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 4174bac2208Snarayan if (ldcssp->memseg_cache == NULL) { 4184bac2208Snarayan DWARN(DBG_ALL_LDCS, "_init: ldc_memseg cache create failed\n"); 4194bac2208Snarayan mutex_exit(&ldcssp->lock); 4204bac2208Snarayan return (-1); 4214bac2208Snarayan } 4224bac2208Snarayan 4234bac2208Snarayan 4241ae08745Sheppo ldcssp->channel_count = 0; 4251ae08745Sheppo ldcssp->channels_open = 0; 4261ae08745Sheppo ldcssp->chan_list = NULL; 4271ae08745Sheppo ldcssp->dring_list = NULL; 4281ae08745Sheppo 429*5699897cSHaik Aftandilian /* Register debug_enter callback */ 430*5699897cSHaik Aftandilian kldc_set_debug_cb(&i_ldc_debug_enter); 431*5699897cSHaik Aftandilian 4321ae08745Sheppo mutex_exit(&ldcssp->lock); 4331ae08745Sheppo 4341ae08745Sheppo return (0); 4351ae08745Sheppo } 4361ae08745Sheppo 4371ae08745Sheppo int 4381ae08745Sheppo _info(struct modinfo *modinfop) 4391ae08745Sheppo { 4401ae08745Sheppo /* Report status of the dynamically loadable driver module */ 4411ae08745Sheppo return (mod_info(&ml, modinfop)); 4421ae08745Sheppo } 4431ae08745Sheppo 4441ae08745Sheppo int 4451ae08745Sheppo _fini(void) 4461ae08745Sheppo { 4471ae08745Sheppo int rv, status; 44822f747efSnarayan ldc_chan_t *tmp_ldcp, *ldcp; 44922f747efSnarayan ldc_dring_t *tmp_dringp, *dringp; 4501ae08745Sheppo ldc_mem_info_t minfo; 4511ae08745Sheppo 4521ae08745Sheppo /* Unlink the driver module from the system */ 4531ae08745Sheppo status = mod_remove(&ml); 4541ae08745Sheppo if (status) { 4551ae08745Sheppo DWARN(DBG_ALL_LDCS, "_fini: mod_remove failed\n"); 4561ae08745Sheppo return (EIO); 4571ae08745Sheppo } 4581ae08745Sheppo 459*5699897cSHaik Aftandilian /* Unregister debug_enter callback */ 460*5699897cSHaik Aftandilian kldc_set_debug_cb(NULL); 461*5699897cSHaik Aftandilian 4621ae08745Sheppo /* Free descriptor rings */ 4631ae08745Sheppo dringp = ldcssp->dring_list; 4641ae08745Sheppo while (dringp != NULL) { 46522f747efSnarayan tmp_dringp = dringp->next; 4661ae08745Sheppo 4671ae08745Sheppo rv = ldc_mem_dring_info((ldc_dring_handle_t)dringp, &minfo); 4681ae08745Sheppo if (rv == 0 && minfo.status != LDC_UNBOUND) { 4691ae08745Sheppo if (minfo.status == LDC_BOUND) { 4701ae08745Sheppo (void) ldc_mem_dring_unbind( 4711ae08745Sheppo (ldc_dring_handle_t)dringp); 4721ae08745Sheppo } 4731ae08745Sheppo if (minfo.status == LDC_MAPPED) { 4741ae08745Sheppo (void) ldc_mem_dring_unmap( 4751ae08745Sheppo (ldc_dring_handle_t)dringp); 4761ae08745Sheppo } 4771ae08745Sheppo } 4781ae08745Sheppo 4791ae08745Sheppo (void) ldc_mem_dring_destroy((ldc_dring_handle_t)dringp); 48022f747efSnarayan dringp = tmp_dringp; 4811ae08745Sheppo } 4821ae08745Sheppo ldcssp->dring_list = NULL; 4831ae08745Sheppo 48422f747efSnarayan /* close and finalize channels */ 48522f747efSnarayan ldcp = ldcssp->chan_list; 48622f747efSnarayan while (ldcp != NULL) { 48722f747efSnarayan tmp_ldcp = ldcp->next; 48822f747efSnarayan 48922f747efSnarayan (void) ldc_close((ldc_handle_t)ldcp); 49022f747efSnarayan (void) ldc_fini((ldc_handle_t)ldcp); 49122f747efSnarayan 49222f747efSnarayan ldcp = tmp_ldcp; 49322f747efSnarayan } 49422f747efSnarayan ldcssp->chan_list = NULL; 49522f747efSnarayan 4964bac2208Snarayan /* Destroy kmem caches */ 4974bac2208Snarayan kmem_cache_destroy(ldcssp->memhdl_cache); 4984bac2208Snarayan kmem_cache_destroy(ldcssp->memseg_cache); 4994bac2208Snarayan 5001ae08745Sheppo /* 5011ae08745Sheppo * We have successfully "removed" the driver. 5021ae08745Sheppo * Destroying soft states 5031ae08745Sheppo */ 5041ae08745Sheppo mutex_destroy(&ldcssp->lock); 5051ae08745Sheppo kmem_free(ldcssp, sizeof (ldc_soft_state_t)); 5061ae08745Sheppo 5071ae08745Sheppo (void) hsvc_unregister(&ldc_hsvc); 5081ae08745Sheppo 5091ae08745Sheppo return (status); 5101ae08745Sheppo } 5111ae08745Sheppo 5121ae08745Sheppo /* -------------------------------------------------------------------------- */ 5131ae08745Sheppo 5141ae08745Sheppo /* 515e1ebb9ecSlm66018 * LDC Link Layer Internal Functions 5161ae08745Sheppo */ 5171ae08745Sheppo 5181ae08745Sheppo /* 5191ae08745Sheppo * Translate HV Errors to sun4v error codes 5201ae08745Sheppo */ 52120ae46ebSha137994 int 5221ae08745Sheppo i_ldc_h2v_error(int h_error) 5231ae08745Sheppo { 5241ae08745Sheppo switch (h_error) { 5251ae08745Sheppo 5261ae08745Sheppo case H_EOK: 5271ae08745Sheppo return (0); 5281ae08745Sheppo 5291ae08745Sheppo case H_ENORADDR: 5301ae08745Sheppo return (EFAULT); 5311ae08745Sheppo 5321ae08745Sheppo case H_EBADPGSZ: 5331ae08745Sheppo case H_EINVAL: 5341ae08745Sheppo return (EINVAL); 5351ae08745Sheppo 5361ae08745Sheppo case H_EWOULDBLOCK: 5371ae08745Sheppo return (EWOULDBLOCK); 5381ae08745Sheppo 5391ae08745Sheppo case H_ENOACCESS: 5401ae08745Sheppo case H_ENOMAP: 5411ae08745Sheppo return (EACCES); 5421ae08745Sheppo 5431ae08745Sheppo case H_EIO: 5441ae08745Sheppo case H_ECPUERROR: 5451ae08745Sheppo return (EIO); 5461ae08745Sheppo 5471ae08745Sheppo case H_ENOTSUPPORTED: 5481ae08745Sheppo return (ENOTSUP); 5491ae08745Sheppo 5501ae08745Sheppo case H_ETOOMANY: 5511ae08745Sheppo return (ENOSPC); 5521ae08745Sheppo 5531ae08745Sheppo case H_ECHANNEL: 5541ae08745Sheppo return (ECHRNG); 5551ae08745Sheppo default: 5561ae08745Sheppo break; 5571ae08745Sheppo } 5581ae08745Sheppo 5591ae08745Sheppo return (EIO); 5601ae08745Sheppo } 5611ae08745Sheppo 5621ae08745Sheppo /* 5631ae08745Sheppo * Reconfigure the transmit queue 5641ae08745Sheppo */ 5651ae08745Sheppo static int 5661ae08745Sheppo i_ldc_txq_reconf(ldc_chan_t *ldcp) 5671ae08745Sheppo { 5681ae08745Sheppo int rv; 5691ae08745Sheppo 5701ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 571d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 572d10e4ef2Snarayan 5731ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 5741ae08745Sheppo if (rv) { 5751ae08745Sheppo cmn_err(CE_WARN, 5763af08d82Slm66018 "i_ldc_txq_reconf: (0x%lx) cannot set qconf", ldcp->id); 5771ae08745Sheppo return (EIO); 5781ae08745Sheppo } 5791ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &(ldcp->tx_head), 5801ae08745Sheppo &(ldcp->tx_tail), &(ldcp->link_state)); 5811ae08745Sheppo if (rv) { 5821ae08745Sheppo cmn_err(CE_WARN, 5833af08d82Slm66018 "i_ldc_txq_reconf: (0x%lx) cannot get qptrs", ldcp->id); 5841ae08745Sheppo return (EIO); 5851ae08745Sheppo } 5863af08d82Slm66018 D1(ldcp->id, "i_ldc_txq_reconf: (0x%llx) h=0x%llx,t=0x%llx," 5871ae08745Sheppo "s=0x%llx\n", ldcp->id, ldcp->tx_head, ldcp->tx_tail, 5881ae08745Sheppo ldcp->link_state); 5891ae08745Sheppo 5901ae08745Sheppo return (0); 5911ae08745Sheppo } 5921ae08745Sheppo 5931ae08745Sheppo /* 5941ae08745Sheppo * Reconfigure the receive queue 5951ae08745Sheppo */ 5961ae08745Sheppo static int 5973af08d82Slm66018 i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset) 5981ae08745Sheppo { 5991ae08745Sheppo int rv; 6001ae08745Sheppo uint64_t rx_head, rx_tail; 6011ae08745Sheppo 6021ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 6031ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 6041ae08745Sheppo &(ldcp->link_state)); 6051ae08745Sheppo if (rv) { 6061ae08745Sheppo cmn_err(CE_WARN, 6073af08d82Slm66018 "i_ldc_rxq_reconf: (0x%lx) cannot get state", 6081ae08745Sheppo ldcp->id); 6091ae08745Sheppo return (EIO); 6101ae08745Sheppo } 6111ae08745Sheppo 6123af08d82Slm66018 if (force_reset || (ldcp->tstate & ~TS_IN_RESET) == TS_UP) { 6131ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, 6141ae08745Sheppo ldcp->rx_q_entries); 6151ae08745Sheppo if (rv) { 6161ae08745Sheppo cmn_err(CE_WARN, 6173af08d82Slm66018 "i_ldc_rxq_reconf: (0x%lx) cannot set qconf", 6181ae08745Sheppo ldcp->id); 6191ae08745Sheppo return (EIO); 6201ae08745Sheppo } 6213af08d82Slm66018 D1(ldcp->id, "i_ldc_rxq_reconf: (0x%llx) completed q reconf", 6221ae08745Sheppo ldcp->id); 6231ae08745Sheppo } 6241ae08745Sheppo 6251ae08745Sheppo return (0); 6261ae08745Sheppo } 6271ae08745Sheppo 628a8ea4edeSnarayan 629a8ea4edeSnarayan /* 630a8ea4edeSnarayan * Drain the contents of the receive queue 631a8ea4edeSnarayan */ 632a8ea4edeSnarayan static int 633a8ea4edeSnarayan i_ldc_rxq_drain(ldc_chan_t *ldcp) 634a8ea4edeSnarayan { 635a8ea4edeSnarayan int rv; 636a8ea4edeSnarayan uint64_t rx_head, rx_tail; 637a8ea4edeSnarayan 638a8ea4edeSnarayan ASSERT(MUTEX_HELD(&ldcp->lock)); 639a8ea4edeSnarayan rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 640a8ea4edeSnarayan &(ldcp->link_state)); 641a8ea4edeSnarayan if (rv) { 642a8ea4edeSnarayan cmn_err(CE_WARN, "i_ldc_rxq_drain: (0x%lx) cannot get state", 643a8ea4edeSnarayan ldcp->id); 644a8ea4edeSnarayan return (EIO); 645a8ea4edeSnarayan } 646a8ea4edeSnarayan 647a8ea4edeSnarayan /* flush contents by setting the head = tail */ 648a8ea4edeSnarayan return (i_ldc_set_rx_head(ldcp, rx_tail)); 649a8ea4edeSnarayan } 650a8ea4edeSnarayan 651a8ea4edeSnarayan 6521ae08745Sheppo /* 6531ae08745Sheppo * Reset LDC state structure and its contents 6541ae08745Sheppo */ 6551ae08745Sheppo static void 6561ae08745Sheppo i_ldc_reset_state(ldc_chan_t *ldcp) 6571ae08745Sheppo { 6581ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 6591ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 6601ae08745Sheppo ldcp->last_ack_rcd = 0; 6611ae08745Sheppo ldcp->last_msg_rcd = 0; 6621ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 66358283286Sha137994 ldcp->stream_remains = 0; 6641ae08745Sheppo ldcp->next_vidx = 0; 6651ae08745Sheppo ldcp->hstate = 0; 6661ae08745Sheppo ldcp->tstate = TS_OPEN; 6671ae08745Sheppo ldcp->status = LDC_OPEN; 66858283286Sha137994 ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID; 66958283286Sha137994 ldcp->rx_dq_head = 0; 67058283286Sha137994 ldcp->rx_dq_tail = 0; 6711ae08745Sheppo 6721ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 6731ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 6741ae08745Sheppo 6751ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 6761ae08745Sheppo ldcp->status = LDC_UP; 6771ae08745Sheppo ldcp->tstate = TS_UP; 6781ae08745Sheppo } else { 6791ae08745Sheppo ldcp->status = LDC_READY; 6801ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 6811ae08745Sheppo } 6821ae08745Sheppo } 6831ae08745Sheppo } 6841ae08745Sheppo 6851ae08745Sheppo /* 6861ae08745Sheppo * Reset a LDC channel 6871ae08745Sheppo */ 68820ae46ebSha137994 void 6893af08d82Slm66018 i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset) 6901ae08745Sheppo { 69183d3bc6fSnarayan DWARN(ldcp->id, "i_ldc_reset: (0x%llx) channel reset\n", ldcp->id); 6921ae08745Sheppo 693d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->lock)); 694d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 695d10e4ef2Snarayan 6963af08d82Slm66018 /* reconfig Tx and Rx queues */ 6971ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 6983af08d82Slm66018 (void) i_ldc_rxq_reconf(ldcp, force_reset); 6993af08d82Slm66018 7003af08d82Slm66018 /* Clear Tx and Rx interrupts */ 7013af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 7023af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 7033af08d82Slm66018 7043af08d82Slm66018 /* Reset channel state */ 7051ae08745Sheppo i_ldc_reset_state(ldcp); 7063af08d82Slm66018 7073af08d82Slm66018 /* Mark channel in reset */ 7083af08d82Slm66018 ldcp->tstate |= TS_IN_RESET; 7091ae08745Sheppo } 7101ae08745Sheppo 711*5699897cSHaik Aftandilian /* 712*5699897cSHaik Aftandilian * Walk the channel list and reset channels if they are of the right 713*5699897cSHaik Aftandilian * devclass and their Rx queues have been configured. No locks are 714*5699897cSHaik Aftandilian * taken because the function is only invoked by the kernel just before 715*5699897cSHaik Aftandilian * entering the prom or debugger when the system is single-threaded. 716*5699897cSHaik Aftandilian */ 717*5699897cSHaik Aftandilian static void 718*5699897cSHaik Aftandilian i_ldc_debug_enter(void) 719*5699897cSHaik Aftandilian { 720*5699897cSHaik Aftandilian ldc_chan_t *ldcp; 721*5699897cSHaik Aftandilian 722*5699897cSHaik Aftandilian ldcp = ldcssp->chan_list; 723*5699897cSHaik Aftandilian while (ldcp != NULL) { 724*5699897cSHaik Aftandilian if (((ldcp->tstate & TS_QCONF_RDY) == TS_QCONF_RDY) && 725*5699897cSHaik Aftandilian (LDC_DEVCLASS_PROM_RESET(ldcp->devclass) != 0)) { 726*5699897cSHaik Aftandilian (void) hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, 727*5699897cSHaik Aftandilian ldcp->rx_q_entries); 728*5699897cSHaik Aftandilian } 729*5699897cSHaik Aftandilian ldcp = ldcp->next; 730*5699897cSHaik Aftandilian } 731*5699897cSHaik Aftandilian } 7324bac2208Snarayan 7331ae08745Sheppo /* 7341ae08745Sheppo * Clear pending interrupts 7351ae08745Sheppo */ 7361ae08745Sheppo static void 7371ae08745Sheppo i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype) 7381ae08745Sheppo { 7391ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 7401ae08745Sheppo 7411ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 7423af08d82Slm66018 ASSERT(cinfo->dip != NULL); 7434bac2208Snarayan 7443af08d82Slm66018 switch (itype) { 7453af08d82Slm66018 case CNEX_TX_INTR: 7464bac2208Snarayan /* check Tx interrupt */ 7473af08d82Slm66018 if (ldcp->tx_intr_state) 7483af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_NONE; 7494bac2208Snarayan else 7504bac2208Snarayan return; 7513af08d82Slm66018 break; 7523af08d82Slm66018 7533af08d82Slm66018 case CNEX_RX_INTR: 7544bac2208Snarayan /* check Rx interrupt */ 7553af08d82Slm66018 if (ldcp->rx_intr_state) 7563af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_NONE; 7574bac2208Snarayan else 7584bac2208Snarayan return; 7593af08d82Slm66018 break; 7604bac2208Snarayan } 7614bac2208Snarayan 7621ae08745Sheppo (void) cinfo->clr_intr(cinfo->dip, ldcp->id, itype); 7634bac2208Snarayan D2(ldcp->id, 7644bac2208Snarayan "i_ldc_clear_intr: (0x%llx) cleared 0x%x intr\n", 7654bac2208Snarayan ldcp->id, itype); 7661ae08745Sheppo } 7671ae08745Sheppo 7681ae08745Sheppo /* 7691ae08745Sheppo * Set the receive queue head 7700a55fbb7Slm66018 * Resets connection and returns an error if it fails. 7711ae08745Sheppo */ 7721ae08745Sheppo static int 7731ae08745Sheppo i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head) 7741ae08745Sheppo { 7751ae08745Sheppo int rv; 7760a55fbb7Slm66018 int retries; 7771ae08745Sheppo 7781ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 7790a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 7800a55fbb7Slm66018 7810a55fbb7Slm66018 if ((rv = hv_ldc_rx_set_qhead(ldcp->id, head)) == 0) 7820a55fbb7Slm66018 return (0); 7830a55fbb7Slm66018 7840a55fbb7Slm66018 if (rv != H_EWOULDBLOCK) 7850a55fbb7Slm66018 break; 7860a55fbb7Slm66018 7870a55fbb7Slm66018 /* wait for ldc_delay usecs */ 7880a55fbb7Slm66018 drv_usecwait(ldc_delay); 7891ae08745Sheppo } 7901ae08745Sheppo 7910a55fbb7Slm66018 cmn_err(CE_WARN, "ldc_rx_set_qhead: (0x%lx) cannot set qhead 0x%lx", 7920a55fbb7Slm66018 ldcp->id, head); 793d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 7943af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 795d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 7960a55fbb7Slm66018 7970a55fbb7Slm66018 return (ECONNRESET); 7981ae08745Sheppo } 7991ae08745Sheppo 80022f747efSnarayan /* 80122f747efSnarayan * Returns the tx_head to be used for transfer 80222f747efSnarayan */ 80322f747efSnarayan static void 80422f747efSnarayan i_ldc_get_tx_head(ldc_chan_t *ldcp, uint64_t *head) 80522f747efSnarayan { 80622f747efSnarayan ldc_msg_t *pkt; 80722f747efSnarayan 80822f747efSnarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 80922f747efSnarayan 81022f747efSnarayan /* get current Tx head */ 81122f747efSnarayan *head = ldcp->tx_head; 81222f747efSnarayan 81322f747efSnarayan /* 81422f747efSnarayan * Reliable mode will use the ACKd head instead of the regular tx_head. 81522f747efSnarayan * Also in Reliable mode, advance ackd_head for all non DATA/INFO pkts, 81622f747efSnarayan * up to the current location of tx_head. This needs to be done 81722f747efSnarayan * as the peer will only ACK DATA/INFO pkts. 81822f747efSnarayan */ 81920ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 82022f747efSnarayan while (ldcp->tx_ackd_head != ldcp->tx_head) { 82122f747efSnarayan pkt = (ldc_msg_t *)(ldcp->tx_q_va + ldcp->tx_ackd_head); 82222f747efSnarayan if ((pkt->type & LDC_DATA) && (pkt->stype & LDC_INFO)) { 82322f747efSnarayan break; 82422f747efSnarayan } 82522f747efSnarayan /* advance ACKd head */ 82622f747efSnarayan ldcp->tx_ackd_head = 82722f747efSnarayan (ldcp->tx_ackd_head + LDC_PACKET_SIZE) % 82822f747efSnarayan (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 82922f747efSnarayan } 83022f747efSnarayan *head = ldcp->tx_ackd_head; 83122f747efSnarayan } 83222f747efSnarayan } 8331ae08745Sheppo 8341ae08745Sheppo /* 8351ae08745Sheppo * Returns the tx_tail to be used for transfer 8361ae08745Sheppo * Re-reads the TX queue ptrs if and only if the 8371ae08745Sheppo * the cached head and tail are equal (queue is full) 8381ae08745Sheppo */ 8391ae08745Sheppo static int 8401ae08745Sheppo i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail) 8411ae08745Sheppo { 8421ae08745Sheppo int rv; 8431ae08745Sheppo uint64_t current_head, new_tail; 8441ae08745Sheppo 845d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 8461ae08745Sheppo /* Read the head and tail ptrs from HV */ 8471ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 8481ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 8491ae08745Sheppo if (rv) { 8501ae08745Sheppo cmn_err(CE_WARN, 8511ae08745Sheppo "i_ldc_get_tx_tail: (0x%lx) cannot read qptrs\n", 8521ae08745Sheppo ldcp->id); 8531ae08745Sheppo return (EIO); 8541ae08745Sheppo } 8551ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 856cb112a14Slm66018 D1(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) channel not ready\n", 8571ae08745Sheppo ldcp->id); 8581ae08745Sheppo return (ECONNRESET); 8591ae08745Sheppo } 8601ae08745Sheppo 86122f747efSnarayan i_ldc_get_tx_head(ldcp, ¤t_head); 8621ae08745Sheppo 8631ae08745Sheppo /* increment the tail */ 8641ae08745Sheppo new_tail = (ldcp->tx_tail + LDC_PACKET_SIZE) % 8651ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 8661ae08745Sheppo 8671ae08745Sheppo if (new_tail == current_head) { 8681ae08745Sheppo DWARN(ldcp->id, 8691ae08745Sheppo "i_ldc_get_tx_tail: (0x%llx) TX queue is full\n", 8701ae08745Sheppo ldcp->id); 8711ae08745Sheppo return (EWOULDBLOCK); 8721ae08745Sheppo } 8731ae08745Sheppo 8741ae08745Sheppo D2(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) head=0x%llx, tail=0x%llx\n", 8751ae08745Sheppo ldcp->id, ldcp->tx_head, ldcp->tx_tail); 8761ae08745Sheppo 8771ae08745Sheppo *tail = ldcp->tx_tail; 8781ae08745Sheppo return (0); 8791ae08745Sheppo } 8801ae08745Sheppo 8811ae08745Sheppo /* 8821ae08745Sheppo * Set the tail pointer. If HV returns EWOULDBLOCK, it will back off 8830a55fbb7Slm66018 * and retry ldc_max_retries times before returning an error. 8841ae08745Sheppo * Returns 0, EWOULDBLOCK or EIO 8851ae08745Sheppo */ 8861ae08745Sheppo static int 8871ae08745Sheppo i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail) 8881ae08745Sheppo { 8891ae08745Sheppo int rv, retval = EWOULDBLOCK; 8900a55fbb7Slm66018 int retries; 8911ae08745Sheppo 892d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 8930a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 8941ae08745Sheppo 8951ae08745Sheppo if ((rv = hv_ldc_tx_set_qtail(ldcp->id, tail)) == 0) { 8961ae08745Sheppo retval = 0; 8971ae08745Sheppo break; 8981ae08745Sheppo } 8991ae08745Sheppo if (rv != H_EWOULDBLOCK) { 9001ae08745Sheppo DWARN(ldcp->id, "i_ldc_set_tx_tail: (0x%llx) set " 9011ae08745Sheppo "qtail=0x%llx failed, rv=%d\n", ldcp->id, tail, rv); 9021ae08745Sheppo retval = EIO; 9031ae08745Sheppo break; 9041ae08745Sheppo } 9051ae08745Sheppo 9060a55fbb7Slm66018 /* wait for ldc_delay usecs */ 9070a55fbb7Slm66018 drv_usecwait(ldc_delay); 9081ae08745Sheppo } 9091ae08745Sheppo return (retval); 9101ae08745Sheppo } 9111ae08745Sheppo 9121ae08745Sheppo /* 91358283286Sha137994 * Copy a data packet from the HV receive queue to the data queue. 91458283286Sha137994 * Caller must ensure that the data queue is not already full. 91558283286Sha137994 * 91658283286Sha137994 * The *head argument represents the current head pointer for the HV 91758283286Sha137994 * receive queue. After copying a packet from the HV receive queue, 91858283286Sha137994 * the *head pointer will be updated. This allows the caller to update 91958283286Sha137994 * the head pointer in HV using the returned *head value. 92058283286Sha137994 */ 92158283286Sha137994 void 92258283286Sha137994 i_ldc_rxdq_copy(ldc_chan_t *ldcp, uint64_t *head) 92358283286Sha137994 { 92458283286Sha137994 uint64_t q_size, dq_size; 92558283286Sha137994 92658283286Sha137994 ASSERT(MUTEX_HELD(&ldcp->lock)); 92758283286Sha137994 92858283286Sha137994 q_size = ldcp->rx_q_entries << LDC_PACKET_SHIFT; 92958283286Sha137994 dq_size = ldcp->rx_dq_entries << LDC_PACKET_SHIFT; 93058283286Sha137994 93158283286Sha137994 ASSERT(Q_CONTIG_SPACE(ldcp->rx_dq_head, ldcp->rx_dq_tail, 93258283286Sha137994 dq_size) >= LDC_PACKET_SIZE); 93358283286Sha137994 93458283286Sha137994 bcopy((void *)(ldcp->rx_q_va + *head), 93558283286Sha137994 (void *)(ldcp->rx_dq_va + ldcp->rx_dq_tail), LDC_PACKET_SIZE); 93658283286Sha137994 TRACE_RXDQ_COPY(ldcp, LDC_PACKET_SIZE); 93758283286Sha137994 93858283286Sha137994 /* Update rx head */ 93958283286Sha137994 *head = (*head + LDC_PACKET_SIZE) % q_size; 94058283286Sha137994 94158283286Sha137994 /* Update dq tail */ 94258283286Sha137994 ldcp->rx_dq_tail = (ldcp->rx_dq_tail + LDC_PACKET_SIZE) % dq_size; 94358283286Sha137994 } 94458283286Sha137994 94558283286Sha137994 /* 94658283286Sha137994 * Update the Rx data queue head pointer 94758283286Sha137994 */ 94858283286Sha137994 static int 94958283286Sha137994 i_ldc_set_rxdq_head(ldc_chan_t *ldcp, uint64_t head) 95058283286Sha137994 { 95158283286Sha137994 ldcp->rx_dq_head = head; 95258283286Sha137994 return (0); 95358283286Sha137994 } 95458283286Sha137994 95558283286Sha137994 /* 95658283286Sha137994 * Get the Rx data queue head and tail pointers 95758283286Sha137994 */ 95858283286Sha137994 static uint64_t 95958283286Sha137994 i_ldc_dq_rx_get_state(ldc_chan_t *ldcp, uint64_t *head, uint64_t *tail, 96058283286Sha137994 uint64_t *link_state) 96158283286Sha137994 { 96258283286Sha137994 _NOTE(ARGUNUSED(link_state)) 96358283286Sha137994 *head = ldcp->rx_dq_head; 96458283286Sha137994 *tail = ldcp->rx_dq_tail; 96558283286Sha137994 return (0); 96658283286Sha137994 } 96758283286Sha137994 96858283286Sha137994 /* 96958283286Sha137994 * Wrapper for the Rx HV queue set head function. Giving the 97058283286Sha137994 * data queue and HV queue set head functions the same type. 97158283286Sha137994 */ 97258283286Sha137994 static uint64_t 97358283286Sha137994 i_ldc_hvq_rx_get_state(ldc_chan_t *ldcp, uint64_t *head, uint64_t *tail, 97458283286Sha137994 uint64_t *link_state) 97558283286Sha137994 { 97658283286Sha137994 return (i_ldc_h2v_error(hv_ldc_rx_get_state(ldcp->id, head, tail, 97758283286Sha137994 link_state))); 97858283286Sha137994 } 97958283286Sha137994 98058283286Sha137994 /* 98158283286Sha137994 * LDC receive interrupt handler 98258283286Sha137994 * triggered for channel with data pending to read 98358283286Sha137994 * i.e. Rx queue content changes 98458283286Sha137994 */ 98558283286Sha137994 static uint_t 98658283286Sha137994 i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2) 98758283286Sha137994 { 98858283286Sha137994 _NOTE(ARGUNUSED(arg2)) 98958283286Sha137994 99058283286Sha137994 ldc_chan_t *ldcp; 99158283286Sha137994 boolean_t notify; 99258283286Sha137994 uint64_t event; 99312f80fa6Sha137994 int rv, status; 99458283286Sha137994 99558283286Sha137994 /* Get the channel for which interrupt was received */ 99658283286Sha137994 if (arg1 == NULL) { 99758283286Sha137994 cmn_err(CE_WARN, "i_ldc_rx_hdlr: invalid arg\n"); 99858283286Sha137994 return (DDI_INTR_UNCLAIMED); 99958283286Sha137994 } 100058283286Sha137994 100158283286Sha137994 ldcp = (ldc_chan_t *)arg1; 100258283286Sha137994 100358283286Sha137994 D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 100458283286Sha137994 ldcp->id, ldcp); 100558283286Sha137994 D1(ldcp->id, "i_ldc_rx_hdlr: (%llx) USR%lx/TS%lx/HS%lx, LSTATE=%lx\n", 100658283286Sha137994 ldcp->id, ldcp->status, ldcp->tstate, ldcp->hstate, 100758283286Sha137994 ldcp->link_state); 100858283286Sha137994 100958283286Sha137994 /* Lock channel */ 101058283286Sha137994 mutex_enter(&ldcp->lock); 101158283286Sha137994 101258283286Sha137994 /* Mark the interrupt as being actively handled */ 101358283286Sha137994 ldcp->rx_intr_state = LDC_INTR_ACTIVE; 101458283286Sha137994 101512f80fa6Sha137994 status = i_ldc_rx_process_hvq(ldcp, ¬ify, &event); 101658283286Sha137994 101720ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) { 101858283286Sha137994 /* 101958283286Sha137994 * If there are no data packets on the queue, clear 102058283286Sha137994 * the interrupt. Otherwise, the ldc_read will clear 102158283286Sha137994 * interrupts after draining the queue. To indicate the 102258283286Sha137994 * interrupt has not yet been cleared, it is marked 102358283286Sha137994 * as pending. 102458283286Sha137994 */ 102558283286Sha137994 if ((event & LDC_EVT_READ) == 0) { 102658283286Sha137994 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 102758283286Sha137994 } else { 102858283286Sha137994 ldcp->rx_intr_state = LDC_INTR_PEND; 102958283286Sha137994 } 103058283286Sha137994 } 103158283286Sha137994 103258283286Sha137994 /* if callbacks are disabled, do not notify */ 103358283286Sha137994 if (notify && ldcp->cb_enabled) { 103458283286Sha137994 ldcp->cb_inprogress = B_TRUE; 103558283286Sha137994 mutex_exit(&ldcp->lock); 103658283286Sha137994 rv = ldcp->cb(event, ldcp->cb_arg); 103758283286Sha137994 if (rv) { 103858283286Sha137994 DWARN(ldcp->id, 103958283286Sha137994 "i_ldc_rx_hdlr: (0x%llx) callback failure", 104058283286Sha137994 ldcp->id); 104158283286Sha137994 } 104258283286Sha137994 mutex_enter(&ldcp->lock); 104358283286Sha137994 ldcp->cb_inprogress = B_FALSE; 104458283286Sha137994 } 104558283286Sha137994 104620ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 104712f80fa6Sha137994 if (status == ENOSPC) { 104858283286Sha137994 /* 104912f80fa6Sha137994 * Here, ENOSPC indicates the secondary data 105012f80fa6Sha137994 * queue is full and the Rx queue is non-empty. 105112f80fa6Sha137994 * Much like how reliable and raw modes are 105212f80fa6Sha137994 * handled above, since the Rx queue is non- 105312f80fa6Sha137994 * empty, we mark the interrupt as pending to 105412f80fa6Sha137994 * indicate it has not yet been cleared. 105512f80fa6Sha137994 */ 105612f80fa6Sha137994 ldcp->rx_intr_state = LDC_INTR_PEND; 105712f80fa6Sha137994 } else { 105812f80fa6Sha137994 /* 105912f80fa6Sha137994 * We have processed all CTRL packets and 106012f80fa6Sha137994 * copied all DATA packets to the secondary 106112f80fa6Sha137994 * queue. Clear the interrupt. 106258283286Sha137994 */ 106358283286Sha137994 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 106458283286Sha137994 } 106512f80fa6Sha137994 } 106658283286Sha137994 106758283286Sha137994 mutex_exit(&ldcp->lock); 106858283286Sha137994 106958283286Sha137994 D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) exiting handler", ldcp->id); 107058283286Sha137994 107158283286Sha137994 return (DDI_INTR_CLAIMED); 107258283286Sha137994 } 107358283286Sha137994 107458283286Sha137994 /* 107558283286Sha137994 * Wrapper for the Rx HV queue processing function to be used when 107658283286Sha137994 * checking the Rx HV queue for data packets. Unlike the interrupt 107758283286Sha137994 * handler code flow, the Rx interrupt is not cleared here and 107858283286Sha137994 * callbacks are not made. 107958283286Sha137994 */ 108058283286Sha137994 static uint_t 108158283286Sha137994 i_ldc_chkq(ldc_chan_t *ldcp) 108258283286Sha137994 { 108358283286Sha137994 boolean_t notify; 108458283286Sha137994 uint64_t event; 108558283286Sha137994 108658283286Sha137994 return (i_ldc_rx_process_hvq(ldcp, ¬ify, &event)); 108758283286Sha137994 } 108858283286Sha137994 108958283286Sha137994 /* 10901ae08745Sheppo * Send a LDC message 10911ae08745Sheppo */ 10921ae08745Sheppo static int 10931ae08745Sheppo i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype, 10941ae08745Sheppo uint8_t ctrlmsg) 10951ae08745Sheppo { 10961ae08745Sheppo int rv; 10971ae08745Sheppo ldc_msg_t *pkt; 10981ae08745Sheppo uint64_t tx_tail; 109922f747efSnarayan uint32_t curr_seqid; 11001ae08745Sheppo 1101d10e4ef2Snarayan /* Obtain Tx lock */ 1102d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1103d10e4ef2Snarayan 110422f747efSnarayan curr_seqid = ldcp->last_msg_snt; 110522f747efSnarayan 11061ae08745Sheppo /* get the current tail for the message */ 11071ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 11081ae08745Sheppo if (rv) { 11091ae08745Sheppo DWARN(ldcp->id, 11101ae08745Sheppo "i_ldc_send_pkt: (0x%llx) error sending pkt, " 11111ae08745Sheppo "type=0x%x,subtype=0x%x,ctrl=0x%x\n", 11121ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 1113d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11141ae08745Sheppo return (rv); 11151ae08745Sheppo } 11161ae08745Sheppo 11171ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 11181ae08745Sheppo ZERO_PKT(pkt); 11191ae08745Sheppo 11201ae08745Sheppo /* Initialize the packet */ 11211ae08745Sheppo pkt->type = pkttype; 11221ae08745Sheppo pkt->stype = subtype; 11231ae08745Sheppo pkt->ctrl = ctrlmsg; 11241ae08745Sheppo 11251ae08745Sheppo /* Store ackid/seqid iff it is RELIABLE mode & not a RTS/RTR message */ 11261ae08745Sheppo if (((ctrlmsg & LDC_CTRL_MASK) != LDC_RTS) && 11271ae08745Sheppo ((ctrlmsg & LDC_CTRL_MASK) != LDC_RTR)) { 11281ae08745Sheppo curr_seqid++; 11291ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) { 11301ae08745Sheppo pkt->seqid = curr_seqid; 11311ae08745Sheppo pkt->ackid = ldcp->last_msg_rcd; 11321ae08745Sheppo } 11331ae08745Sheppo } 11341ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_send_pkt", (uint64_t)pkt); 11351ae08745Sheppo 11361ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 11371ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 11381ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 11391ae08745Sheppo 11401ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 11411ae08745Sheppo if (rv) { 11421ae08745Sheppo DWARN(ldcp->id, 11431ae08745Sheppo "i_ldc_send_pkt:(0x%llx) error sending pkt, " 11441ae08745Sheppo "type=0x%x,stype=0x%x,ctrl=0x%x\n", 11451ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 1146d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11471ae08745Sheppo return (EIO); 11481ae08745Sheppo } 11491ae08745Sheppo 11501ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 11511ae08745Sheppo ldcp->tx_tail = tx_tail; 11521ae08745Sheppo 1153d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11541ae08745Sheppo return (0); 11551ae08745Sheppo } 11561ae08745Sheppo 11571ae08745Sheppo /* 11581ae08745Sheppo * Checks if packet was received in right order 1159e1ebb9ecSlm66018 * in the case of a reliable link. 11601ae08745Sheppo * Returns 0 if in order, else EIO 11611ae08745Sheppo */ 11621ae08745Sheppo static int 11631ae08745Sheppo i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *msg) 11641ae08745Sheppo { 11651ae08745Sheppo /* No seqid checking for RAW mode */ 11661ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) 11671ae08745Sheppo return (0); 11681ae08745Sheppo 11691ae08745Sheppo /* No seqid checking for version, RTS, RTR message */ 11701ae08745Sheppo if (msg->ctrl == LDC_VER || 11711ae08745Sheppo msg->ctrl == LDC_RTS || 11721ae08745Sheppo msg->ctrl == LDC_RTR) 11731ae08745Sheppo return (0); 11741ae08745Sheppo 11751ae08745Sheppo /* Initial seqid to use is sent in RTS/RTR and saved in last_msg_rcd */ 11761ae08745Sheppo if (msg->seqid != (ldcp->last_msg_rcd + 1)) { 11771ae08745Sheppo DWARN(ldcp->id, 11781ae08745Sheppo "i_ldc_check_seqid: (0x%llx) out-of-order pkt, got 0x%x, " 11791ae08745Sheppo "expecting 0x%x\n", ldcp->id, msg->seqid, 11801ae08745Sheppo (ldcp->last_msg_rcd + 1)); 11811ae08745Sheppo return (EIO); 11821ae08745Sheppo } 11831ae08745Sheppo 118483d3bc6fSnarayan #ifdef DEBUG 118583d3bc6fSnarayan if (LDC_INJECT_PKTLOSS(ldcp)) { 118683d3bc6fSnarayan DWARN(ldcp->id, 118783d3bc6fSnarayan "i_ldc_check_seqid: (0x%llx) inject pkt loss\n", ldcp->id); 118883d3bc6fSnarayan return (EIO); 118983d3bc6fSnarayan } 119083d3bc6fSnarayan #endif 119183d3bc6fSnarayan 11921ae08745Sheppo return (0); 11931ae08745Sheppo } 11941ae08745Sheppo 11951ae08745Sheppo 11961ae08745Sheppo /* 11971ae08745Sheppo * Process an incoming version ctrl message 11981ae08745Sheppo */ 11991ae08745Sheppo static int 12001ae08745Sheppo i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg) 12011ae08745Sheppo { 12021ae08745Sheppo int rv = 0, idx = ldcp->next_vidx; 12031ae08745Sheppo ldc_msg_t *pkt; 12041ae08745Sheppo uint64_t tx_tail; 12051ae08745Sheppo ldc_ver_t *rcvd_ver; 12061ae08745Sheppo 12071ae08745Sheppo /* get the received version */ 12081ae08745Sheppo rcvd_ver = (ldc_ver_t *)((uint64_t)msg + LDC_PAYLOAD_VER_OFF); 12091ae08745Sheppo 12101ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) received VER v%u.%u\n", 12111ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 12121ae08745Sheppo 1213d10e4ef2Snarayan /* Obtain Tx lock */ 1214d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1215d10e4ef2Snarayan 12161ae08745Sheppo switch (msg->stype) { 12171ae08745Sheppo case LDC_INFO: 12181ae08745Sheppo 12193af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) { 12203af08d82Slm66018 (void) i_ldc_txq_reconf(ldcp); 12213af08d82Slm66018 i_ldc_reset_state(ldcp); 12223af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 12233af08d82Slm66018 return (EAGAIN); 12243af08d82Slm66018 } 12253af08d82Slm66018 12261ae08745Sheppo /* get the current tail and pkt for the response */ 12271ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 12281ae08745Sheppo if (rv != 0) { 12291ae08745Sheppo DWARN(ldcp->id, 12301ae08745Sheppo "i_ldc_process_VER: (0x%llx) err sending " 12311ae08745Sheppo "version ACK/NACK\n", ldcp->id); 12323af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1233d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12341ae08745Sheppo return (ECONNRESET); 12351ae08745Sheppo } 12361ae08745Sheppo 12371ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 12381ae08745Sheppo ZERO_PKT(pkt); 12391ae08745Sheppo 12401ae08745Sheppo /* initialize the packet */ 12411ae08745Sheppo pkt->type = LDC_CTRL; 12421ae08745Sheppo pkt->ctrl = LDC_VER; 12431ae08745Sheppo 12441ae08745Sheppo for (;;) { 12451ae08745Sheppo 12461ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: got %u.%u chk %u.%u\n", 12471ae08745Sheppo rcvd_ver->major, rcvd_ver->minor, 12481ae08745Sheppo ldc_versions[idx].major, ldc_versions[idx].minor); 12491ae08745Sheppo 12501ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 12511ae08745Sheppo /* major version match - ACK version */ 12521ae08745Sheppo pkt->stype = LDC_ACK; 12531ae08745Sheppo 12541ae08745Sheppo /* 12551ae08745Sheppo * lower minor version to the one this endpt 12561ae08745Sheppo * supports, if necessary 12571ae08745Sheppo */ 12581ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 12591ae08745Sheppo rcvd_ver->minor = 12601ae08745Sheppo ldc_versions[idx].minor; 12611ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 12621ae08745Sheppo 12631ae08745Sheppo break; 12641ae08745Sheppo } 12651ae08745Sheppo 12661ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 12671ae08745Sheppo 12681ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 12691ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 12701ae08745Sheppo ldc_versions[idx].major, 12711ae08745Sheppo ldc_versions[idx].minor); 12721ae08745Sheppo 12731ae08745Sheppo /* nack with next lower version */ 12741ae08745Sheppo pkt->stype = LDC_NACK; 12751ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 12761ae08745Sheppo sizeof (ldc_versions[idx])); 12771ae08745Sheppo ldcp->next_vidx = idx; 12781ae08745Sheppo break; 12791ae08745Sheppo } 12801ae08745Sheppo 12811ae08745Sheppo /* next major version */ 12821ae08745Sheppo idx++; 12831ae08745Sheppo 12841ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 12851ae08745Sheppo 12861ae08745Sheppo if (idx == LDC_NUM_VERS) { 12871ae08745Sheppo /* no version match - send NACK */ 12881ae08745Sheppo pkt->stype = LDC_NACK; 12891ae08745Sheppo bzero(pkt->udata, sizeof (ldc_ver_t)); 12901ae08745Sheppo ldcp->next_vidx = 0; 12911ae08745Sheppo break; 12921ae08745Sheppo } 12931ae08745Sheppo } 12941ae08745Sheppo 12951ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 12961ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 12971ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 12981ae08745Sheppo 12991ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 13001ae08745Sheppo if (rv == 0) { 13011ae08745Sheppo ldcp->tx_tail = tx_tail; 13021ae08745Sheppo if (pkt->stype == LDC_ACK) { 13031ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent" 13041ae08745Sheppo " version ACK\n", ldcp->id); 13051ae08745Sheppo /* Save the ACK'd version */ 13061ae08745Sheppo ldcp->version.major = rcvd_ver->major; 13071ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 13080a55fbb7Slm66018 ldcp->hstate |= TS_RCVD_VER; 13091ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 131083d3bc6fSnarayan D1(DBG_ALL_LDCS, 13113af08d82Slm66018 "(0x%llx) Sent ACK, " 13123af08d82Slm66018 "Agreed on version v%u.%u\n", 13131ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 13141ae08745Sheppo } 13151ae08745Sheppo } else { 13161ae08745Sheppo DWARN(ldcp->id, 13171ae08745Sheppo "i_ldc_process_VER: (0x%llx) error sending " 13181ae08745Sheppo "ACK/NACK\n", ldcp->id); 13193af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1320d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13211ae08745Sheppo return (ECONNRESET); 13221ae08745Sheppo } 13231ae08745Sheppo 13241ae08745Sheppo break; 13251ae08745Sheppo 13261ae08745Sheppo case LDC_ACK: 13273af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) { 13283af08d82Slm66018 if (ldcp->version.major != rcvd_ver->major || 13293af08d82Slm66018 ldcp->version.minor != rcvd_ver->minor) { 13303af08d82Slm66018 13313af08d82Slm66018 /* mismatched version - reset connection */ 13323af08d82Slm66018 DWARN(ldcp->id, 13333af08d82Slm66018 "i_ldc_process_VER: (0x%llx) recvd" 13343af08d82Slm66018 " ACK ver != sent ACK ver\n", ldcp->id); 13353af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 13363af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 13373af08d82Slm66018 return (ECONNRESET); 13383af08d82Slm66018 } 13393af08d82Slm66018 } else { 13401ae08745Sheppo /* SUCCESS - we have agreed on a version */ 13411ae08745Sheppo ldcp->version.major = rcvd_ver->major; 13421ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 13431ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 13443af08d82Slm66018 } 13451ae08745Sheppo 1346cb112a14Slm66018 D1(ldcp->id, "(0x%llx) Got ACK, Agreed on version v%u.%u\n", 13471ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 13481ae08745Sheppo 13491ae08745Sheppo /* initiate RTS-RTR-RDX handshake */ 13501ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 13511ae08745Sheppo if (rv) { 13521ae08745Sheppo DWARN(ldcp->id, 13531ae08745Sheppo "i_ldc_process_VER: (0x%llx) cannot send RTS\n", 13541ae08745Sheppo ldcp->id); 13553af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1356d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13571ae08745Sheppo return (ECONNRESET); 13581ae08745Sheppo } 13591ae08745Sheppo 13601ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 13611ae08745Sheppo ZERO_PKT(pkt); 13621ae08745Sheppo 13631ae08745Sheppo pkt->type = LDC_CTRL; 13641ae08745Sheppo pkt->stype = LDC_INFO; 13651ae08745Sheppo pkt->ctrl = LDC_RTS; 13661ae08745Sheppo pkt->env = ldcp->mode; 13671ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 13681ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 13691ae08745Sheppo 13701ae08745Sheppo ldcp->last_msg_rcd = LDC_INIT_SEQID; 13711ae08745Sheppo 13721ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_VER snd rts", (uint64_t)pkt); 13731ae08745Sheppo 13741ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 13751ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 13761ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 13771ae08745Sheppo 13781ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 13791ae08745Sheppo if (rv) { 13801ae08745Sheppo D2(ldcp->id, 13811ae08745Sheppo "i_ldc_process_VER: (0x%llx) no listener\n", 13821ae08745Sheppo ldcp->id); 13833af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1384d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13851ae08745Sheppo return (ECONNRESET); 13861ae08745Sheppo } 13871ae08745Sheppo 13881ae08745Sheppo ldcp->tx_tail = tx_tail; 13891ae08745Sheppo ldcp->hstate |= TS_SENT_RTS; 13901ae08745Sheppo 13911ae08745Sheppo break; 13921ae08745Sheppo 13931ae08745Sheppo case LDC_NACK: 13941ae08745Sheppo /* check if version in NACK is zero */ 13951ae08745Sheppo if (rcvd_ver->major == 0 && rcvd_ver->minor == 0) { 13961ae08745Sheppo /* version handshake failure */ 13971ae08745Sheppo DWARN(DBG_ALL_LDCS, 13981ae08745Sheppo "i_ldc_process_VER: (0x%llx) no version match\n", 13991ae08745Sheppo ldcp->id); 14003af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1401d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14021ae08745Sheppo return (ECONNRESET); 14031ae08745Sheppo } 14041ae08745Sheppo 14051ae08745Sheppo /* get the current tail and pkt for the response */ 14061ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 14071ae08745Sheppo if (rv != 0) { 14081ae08745Sheppo cmn_err(CE_NOTE, 14091ae08745Sheppo "i_ldc_process_VER: (0x%lx) err sending " 14101ae08745Sheppo "version ACK/NACK\n", ldcp->id); 14113af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1412d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14131ae08745Sheppo return (ECONNRESET); 14141ae08745Sheppo } 14151ae08745Sheppo 14161ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 14171ae08745Sheppo ZERO_PKT(pkt); 14181ae08745Sheppo 14191ae08745Sheppo /* initialize the packet */ 14201ae08745Sheppo pkt->type = LDC_CTRL; 14211ae08745Sheppo pkt->ctrl = LDC_VER; 14221ae08745Sheppo pkt->stype = LDC_INFO; 14231ae08745Sheppo 14241ae08745Sheppo /* check ver in NACK msg has a match */ 14251ae08745Sheppo for (;;) { 14261ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 14271ae08745Sheppo /* 14281ae08745Sheppo * major version match - resubmit request 14291ae08745Sheppo * if lower minor version to the one this endpt 14301ae08745Sheppo * supports, if necessary 14311ae08745Sheppo */ 14321ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 14331ae08745Sheppo rcvd_ver->minor = 14341ae08745Sheppo ldc_versions[idx].minor; 14351ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 14361ae08745Sheppo break; 14371ae08745Sheppo } 14381ae08745Sheppo 14391ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 14401ae08745Sheppo 14411ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 14421ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 14431ae08745Sheppo ldc_versions[idx].major, 14441ae08745Sheppo ldc_versions[idx].minor); 14451ae08745Sheppo 14461ae08745Sheppo /* send next lower version */ 14471ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 14481ae08745Sheppo sizeof (ldc_versions[idx])); 14491ae08745Sheppo ldcp->next_vidx = idx; 14501ae08745Sheppo break; 14511ae08745Sheppo } 14521ae08745Sheppo 14531ae08745Sheppo /* next version */ 14541ae08745Sheppo idx++; 14551ae08745Sheppo 14561ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 14571ae08745Sheppo 14581ae08745Sheppo if (idx == LDC_NUM_VERS) { 14591ae08745Sheppo /* no version match - terminate */ 14601ae08745Sheppo ldcp->next_vidx = 0; 1461d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14621ae08745Sheppo return (ECONNRESET); 14631ae08745Sheppo } 14641ae08745Sheppo } 14651ae08745Sheppo 14661ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 14671ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 14681ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 14691ae08745Sheppo 14701ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 14711ae08745Sheppo if (rv == 0) { 14721ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent version" 14731ae08745Sheppo "INFO v%u.%u\n", ldcp->id, ldc_versions[idx].major, 14741ae08745Sheppo ldc_versions[idx].minor); 14751ae08745Sheppo ldcp->tx_tail = tx_tail; 14761ae08745Sheppo } else { 14771ae08745Sheppo cmn_err(CE_NOTE, 14781ae08745Sheppo "i_ldc_process_VER: (0x%lx) error sending version" 14791ae08745Sheppo "INFO\n", ldcp->id); 14803af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1481d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14821ae08745Sheppo return (ECONNRESET); 14831ae08745Sheppo } 14841ae08745Sheppo 14851ae08745Sheppo break; 14861ae08745Sheppo } 14871ae08745Sheppo 1488d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14891ae08745Sheppo return (rv); 14901ae08745Sheppo } 14911ae08745Sheppo 14921ae08745Sheppo 14931ae08745Sheppo /* 14941ae08745Sheppo * Process an incoming RTS ctrl message 14951ae08745Sheppo */ 14961ae08745Sheppo static int 14971ae08745Sheppo i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg) 14981ae08745Sheppo { 14991ae08745Sheppo int rv = 0; 15001ae08745Sheppo ldc_msg_t *pkt; 15011ae08745Sheppo uint64_t tx_tail; 15021ae08745Sheppo boolean_t sent_NACK = B_FALSE; 15031ae08745Sheppo 15041ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTS: (0x%llx) received RTS\n", ldcp->id); 15051ae08745Sheppo 15061ae08745Sheppo switch (msg->stype) { 15071ae08745Sheppo case LDC_NACK: 15081ae08745Sheppo DWARN(ldcp->id, 15091ae08745Sheppo "i_ldc_process_RTS: (0x%llx) RTS NACK received\n", 15101ae08745Sheppo ldcp->id); 15111ae08745Sheppo 15121ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1513d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15143af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1515d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15161ae08745Sheppo rv = ECONNRESET; 15171ae08745Sheppo break; 15181ae08745Sheppo 15191ae08745Sheppo case LDC_INFO: 15201ae08745Sheppo 15211ae08745Sheppo /* check mode */ 15221ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 15231ae08745Sheppo cmn_err(CE_NOTE, 15241ae08745Sheppo "i_ldc_process_RTS: (0x%lx) mode mismatch\n", 15251ae08745Sheppo ldcp->id); 15261ae08745Sheppo /* 15271ae08745Sheppo * send NACK in response to MODE message 15281ae08745Sheppo * get the current tail for the response 15291ae08745Sheppo */ 15301ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTS); 15311ae08745Sheppo if (rv) { 15321ae08745Sheppo /* if cannot send NACK - reset channel */ 1533d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15343af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1535d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15361ae08745Sheppo rv = ECONNRESET; 15371ae08745Sheppo break; 15381ae08745Sheppo } 15391ae08745Sheppo sent_NACK = B_TRUE; 15401ae08745Sheppo } 15411ae08745Sheppo break; 15421ae08745Sheppo default: 15431ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTS: (0x%llx) unexp ACK\n", 15441ae08745Sheppo ldcp->id); 1545d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15463af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1547d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15481ae08745Sheppo rv = ECONNRESET; 15491ae08745Sheppo break; 15501ae08745Sheppo } 15511ae08745Sheppo 15521ae08745Sheppo /* 15531ae08745Sheppo * If either the connection was reset (when rv != 0) or 15541ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 15551ae08745Sheppo * we dont want to consume the packet that came in but 15561ae08745Sheppo * not record that we received the RTS 15571ae08745Sheppo */ 15581ae08745Sheppo if (rv || sent_NACK) 15591ae08745Sheppo return (rv); 15601ae08745Sheppo 15611ae08745Sheppo /* record RTS received */ 15621ae08745Sheppo ldcp->hstate |= TS_RCVD_RTS; 15631ae08745Sheppo 15641ae08745Sheppo /* store initial SEQID info */ 15651ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 15661ae08745Sheppo 1567d10e4ef2Snarayan /* Obtain Tx lock */ 1568d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1569d10e4ef2Snarayan 15701ae08745Sheppo /* get the current tail for the response */ 15711ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 15721ae08745Sheppo if (rv != 0) { 15731ae08745Sheppo cmn_err(CE_NOTE, 15741ae08745Sheppo "i_ldc_process_RTS: (0x%lx) err sending RTR\n", 15751ae08745Sheppo ldcp->id); 15763af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1577d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15781ae08745Sheppo return (ECONNRESET); 15791ae08745Sheppo } 15801ae08745Sheppo 15811ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 15821ae08745Sheppo ZERO_PKT(pkt); 15831ae08745Sheppo 15841ae08745Sheppo /* initialize the packet */ 15851ae08745Sheppo pkt->type = LDC_CTRL; 15861ae08745Sheppo pkt->stype = LDC_INFO; 15871ae08745Sheppo pkt->ctrl = LDC_RTR; 15881ae08745Sheppo pkt->env = ldcp->mode; 15891ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 15901ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 15911ae08745Sheppo 15921ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 15931ae08745Sheppo 15941ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 15951ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 15961ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 15971ae08745Sheppo 15981ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 15991ae08745Sheppo if (rv == 0) { 16001ae08745Sheppo D2(ldcp->id, 16011ae08745Sheppo "i_ldc_process_RTS: (0x%llx) sent RTR\n", ldcp->id); 16021ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_RTS sent rtr", (uint64_t)pkt); 16031ae08745Sheppo 16041ae08745Sheppo ldcp->tx_tail = tx_tail; 16051ae08745Sheppo ldcp->hstate |= TS_SENT_RTR; 16061ae08745Sheppo 16071ae08745Sheppo } else { 16081ae08745Sheppo cmn_err(CE_NOTE, 16091ae08745Sheppo "i_ldc_process_RTS: (0x%lx) error sending RTR\n", 16101ae08745Sheppo ldcp->id); 16113af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1612d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16131ae08745Sheppo return (ECONNRESET); 16141ae08745Sheppo } 16151ae08745Sheppo 1616d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16171ae08745Sheppo return (0); 16181ae08745Sheppo } 16191ae08745Sheppo 16201ae08745Sheppo /* 16211ae08745Sheppo * Process an incoming RTR ctrl message 16221ae08745Sheppo */ 16231ae08745Sheppo static int 16241ae08745Sheppo i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg) 16251ae08745Sheppo { 16261ae08745Sheppo int rv = 0; 16271ae08745Sheppo boolean_t sent_NACK = B_FALSE; 16281ae08745Sheppo 16291ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTR: (0x%llx) received RTR\n", ldcp->id); 16301ae08745Sheppo 16311ae08745Sheppo switch (msg->stype) { 16321ae08745Sheppo case LDC_NACK: 16331ae08745Sheppo /* RTR NACK received */ 16341ae08745Sheppo DWARN(ldcp->id, 16351ae08745Sheppo "i_ldc_process_RTR: (0x%llx) RTR NACK received\n", 16361ae08745Sheppo ldcp->id); 16371ae08745Sheppo 16381ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1639d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 16403af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1641d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16421ae08745Sheppo rv = ECONNRESET; 16431ae08745Sheppo 16441ae08745Sheppo break; 16451ae08745Sheppo 16461ae08745Sheppo case LDC_INFO: 16471ae08745Sheppo 16481ae08745Sheppo /* check mode */ 16491ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 16501ae08745Sheppo DWARN(ldcp->id, 1651cb112a14Slm66018 "i_ldc_process_RTR: (0x%llx) mode mismatch, " 1652cb112a14Slm66018 "expecting 0x%x, got 0x%x\n", 1653cb112a14Slm66018 ldcp->id, ldcp->mode, (ldc_mode_t)msg->env); 16541ae08745Sheppo /* 16551ae08745Sheppo * send NACK in response to MODE message 16561ae08745Sheppo * get the current tail for the response 16571ae08745Sheppo */ 16581ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTR); 16591ae08745Sheppo if (rv) { 16601ae08745Sheppo /* if cannot send NACK - reset channel */ 1661d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 16623af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1663d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16641ae08745Sheppo rv = ECONNRESET; 16651ae08745Sheppo break; 16661ae08745Sheppo } 16671ae08745Sheppo sent_NACK = B_TRUE; 16681ae08745Sheppo } 16691ae08745Sheppo break; 16701ae08745Sheppo 16711ae08745Sheppo default: 16721ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTR: (0x%llx) unexp ACK\n", 16731ae08745Sheppo ldcp->id); 16741ae08745Sheppo 16751ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1676d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 16773af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1678d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16791ae08745Sheppo rv = ECONNRESET; 16801ae08745Sheppo break; 16811ae08745Sheppo } 16821ae08745Sheppo 16831ae08745Sheppo /* 16841ae08745Sheppo * If either the connection was reset (when rv != 0) or 16851ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 16861ae08745Sheppo * we dont want to consume the packet that came in but 16871ae08745Sheppo * not record that we received the RTR 16881ae08745Sheppo */ 16891ae08745Sheppo if (rv || sent_NACK) 16901ae08745Sheppo return (rv); 16911ae08745Sheppo 16921ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 16931ae08745Sheppo ldcp->hstate |= TS_RCVD_RTR; 16941ae08745Sheppo 16951ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_INFO, LDC_RDX); 16961ae08745Sheppo if (rv) { 16971ae08745Sheppo cmn_err(CE_NOTE, 16981ae08745Sheppo "i_ldc_process_RTR: (0x%lx) cannot send RDX\n", 16991ae08745Sheppo ldcp->id); 1700d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 17013af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1702d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17031ae08745Sheppo return (ECONNRESET); 17041ae08745Sheppo } 17051ae08745Sheppo D2(ldcp->id, 17061ae08745Sheppo "i_ldc_process_RTR: (0x%llx) sent RDX\n", ldcp->id); 17071ae08745Sheppo 17081ae08745Sheppo ldcp->hstate |= TS_SENT_RDX; 17091ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 17103af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 17111ae08745Sheppo ldcp->status = LDC_UP; 17121ae08745Sheppo 1713cb112a14Slm66018 D1(ldcp->id, "(0x%llx) Handshake Complete\n", ldcp->id); 17141ae08745Sheppo 17151ae08745Sheppo return (0); 17161ae08745Sheppo } 17171ae08745Sheppo 17181ae08745Sheppo 17191ae08745Sheppo /* 17201ae08745Sheppo * Process an incoming RDX ctrl message 17211ae08745Sheppo */ 17221ae08745Sheppo static int 17231ae08745Sheppo i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg) 17241ae08745Sheppo { 17251ae08745Sheppo int rv = 0; 17261ae08745Sheppo 17271ae08745Sheppo D2(ldcp->id, "i_ldc_process_RDX: (0x%llx) received RDX\n", ldcp->id); 17281ae08745Sheppo 17291ae08745Sheppo switch (msg->stype) { 17301ae08745Sheppo case LDC_NACK: 17311ae08745Sheppo /* RDX NACK received */ 17321ae08745Sheppo DWARN(ldcp->id, 17331ae08745Sheppo "i_ldc_process_RDX: (0x%llx) RDX NACK received\n", 17341ae08745Sheppo ldcp->id); 17351ae08745Sheppo 17361ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1737d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 17383af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1739d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17401ae08745Sheppo rv = ECONNRESET; 17411ae08745Sheppo 17421ae08745Sheppo break; 17431ae08745Sheppo 17441ae08745Sheppo case LDC_INFO: 17451ae08745Sheppo 17461ae08745Sheppo /* 17471ae08745Sheppo * if channel is UP and a RDX received after data transmission 17481ae08745Sheppo * has commenced it is an error 17491ae08745Sheppo */ 17501ae08745Sheppo if ((ldcp->tstate == TS_UP) && (ldcp->hstate & TS_RCVD_RDX)) { 17511ae08745Sheppo DWARN(DBG_ALL_LDCS, 17521ae08745Sheppo "i_ldc_process_RDX: (0x%llx) unexpected RDX" 17531ae08745Sheppo " - LDC reset\n", ldcp->id); 1754d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 17553af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1756d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17571ae08745Sheppo return (ECONNRESET); 17581ae08745Sheppo } 17591ae08745Sheppo 17601ae08745Sheppo ldcp->hstate |= TS_RCVD_RDX; 17611ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 17623af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 17631ae08745Sheppo ldcp->status = LDC_UP; 17641ae08745Sheppo 17651ae08745Sheppo D1(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id); 17661ae08745Sheppo break; 17671ae08745Sheppo 17681ae08745Sheppo default: 17691ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RDX: (0x%llx) unexp ACK\n", 17701ae08745Sheppo ldcp->id); 17711ae08745Sheppo 17721ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1773d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 17743af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1775d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17761ae08745Sheppo rv = ECONNRESET; 17771ae08745Sheppo break; 17781ae08745Sheppo } 17791ae08745Sheppo 17801ae08745Sheppo return (rv); 17811ae08745Sheppo } 17821ae08745Sheppo 17831ae08745Sheppo /* 17841ae08745Sheppo * Process an incoming ACK for a data packet 17851ae08745Sheppo */ 17861ae08745Sheppo static int 17871ae08745Sheppo i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg) 17881ae08745Sheppo { 17891ae08745Sheppo int rv; 17901ae08745Sheppo uint64_t tx_head; 17911ae08745Sheppo ldc_msg_t *pkt; 17921ae08745Sheppo 1793d10e4ef2Snarayan /* Obtain Tx lock */ 1794d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1795d10e4ef2Snarayan 17961ae08745Sheppo /* 1797d10e4ef2Snarayan * Read the current Tx head and tail 17981ae08745Sheppo */ 17991ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 18001ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 18011ae08745Sheppo if (rv != 0) { 18021ae08745Sheppo cmn_err(CE_WARN, 18031ae08745Sheppo "i_ldc_process_data_ACK: (0x%lx) cannot read qptrs\n", 18041ae08745Sheppo ldcp->id); 1805d10e4ef2Snarayan 1806d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 18073af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1808d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1809d10e4ef2Snarayan return (ECONNRESET); 18101ae08745Sheppo } 18111ae08745Sheppo 18121ae08745Sheppo /* 18131ae08745Sheppo * loop from where the previous ACK location was to the 18141ae08745Sheppo * current head location. This is how far the HV has 18151ae08745Sheppo * actually send pkts. Pkts between head and tail are 18161ae08745Sheppo * yet to be sent by HV. 18171ae08745Sheppo */ 18181ae08745Sheppo tx_head = ldcp->tx_ackd_head; 18191ae08745Sheppo for (;;) { 18201ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_head); 18211ae08745Sheppo tx_head = (tx_head + LDC_PACKET_SIZE) % 18221ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 18231ae08745Sheppo 18241ae08745Sheppo if (pkt->seqid == msg->ackid) { 18251ae08745Sheppo D2(ldcp->id, 18261ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) found packet\n", 18271ae08745Sheppo ldcp->id); 18281ae08745Sheppo ldcp->last_ack_rcd = msg->ackid; 18291ae08745Sheppo ldcp->tx_ackd_head = tx_head; 18301ae08745Sheppo break; 18311ae08745Sheppo } 18321ae08745Sheppo if (tx_head == ldcp->tx_head) { 18331ae08745Sheppo /* could not find packet */ 18341ae08745Sheppo DWARN(ldcp->id, 18351ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) invalid ACKid\n", 18361ae08745Sheppo ldcp->id); 1837d10e4ef2Snarayan 1838d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 18393af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1840d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1841d10e4ef2Snarayan return (ECONNRESET); 18421ae08745Sheppo } 18431ae08745Sheppo } 18441ae08745Sheppo 1845d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 18461ae08745Sheppo return (0); 18471ae08745Sheppo } 18481ae08745Sheppo 18491ae08745Sheppo /* 18501ae08745Sheppo * Process incoming control message 18511ae08745Sheppo * Return 0 - session can continue 18521ae08745Sheppo * EAGAIN - reprocess packet - state was changed 18531ae08745Sheppo * ECONNRESET - channel was reset 18541ae08745Sheppo */ 18551ae08745Sheppo static int 18561ae08745Sheppo i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *msg) 18571ae08745Sheppo { 18581ae08745Sheppo int rv = 0; 18591ae08745Sheppo 18603af08d82Slm66018 D1(ldcp->id, "i_ldc_ctrlmsg: (%llx) tstate = %lx, hstate = %lx\n", 18613af08d82Slm66018 ldcp->id, ldcp->tstate, ldcp->hstate); 18623af08d82Slm66018 18633af08d82Slm66018 switch (ldcp->tstate & ~TS_IN_RESET) { 18641ae08745Sheppo 18651ae08745Sheppo case TS_OPEN: 18661ae08745Sheppo case TS_READY: 18671ae08745Sheppo 18681ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 18691ae08745Sheppo case LDC_VER: 18701ae08745Sheppo /* process version message */ 18711ae08745Sheppo rv = i_ldc_process_VER(ldcp, msg); 18721ae08745Sheppo break; 18731ae08745Sheppo default: 18741ae08745Sheppo DWARN(ldcp->id, 18751ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 18761ae08745Sheppo "tstate=0x%x\n", ldcp->id, 18771ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 18781ae08745Sheppo break; 18791ae08745Sheppo } 18801ae08745Sheppo 18811ae08745Sheppo break; 18821ae08745Sheppo 18831ae08745Sheppo case TS_VREADY: 18841ae08745Sheppo 18851ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 18861ae08745Sheppo case LDC_VER: 18873af08d82Slm66018 /* process version message */ 18883af08d82Slm66018 rv = i_ldc_process_VER(ldcp, msg); 18891ae08745Sheppo break; 18901ae08745Sheppo case LDC_RTS: 18911ae08745Sheppo /* process RTS message */ 18921ae08745Sheppo rv = i_ldc_process_RTS(ldcp, msg); 18931ae08745Sheppo break; 18941ae08745Sheppo case LDC_RTR: 18951ae08745Sheppo /* process RTR message */ 18961ae08745Sheppo rv = i_ldc_process_RTR(ldcp, msg); 18971ae08745Sheppo break; 18981ae08745Sheppo case LDC_RDX: 18991ae08745Sheppo /* process RDX message */ 19001ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 19011ae08745Sheppo break; 19021ae08745Sheppo default: 19031ae08745Sheppo DWARN(ldcp->id, 19041ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 19051ae08745Sheppo "tstate=0x%x\n", ldcp->id, 19061ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 19071ae08745Sheppo break; 19081ae08745Sheppo } 19091ae08745Sheppo 19101ae08745Sheppo break; 19111ae08745Sheppo 19121ae08745Sheppo case TS_UP: 19131ae08745Sheppo 19141ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 19151ae08745Sheppo case LDC_VER: 19161ae08745Sheppo DWARN(ldcp->id, 19171ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexpected VER " 19181ae08745Sheppo "- LDC reset\n", ldcp->id); 19191ae08745Sheppo /* peer is redoing version negotiation */ 1920d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 19211ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 19221ae08745Sheppo i_ldc_reset_state(ldcp); 1923d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 19241ae08745Sheppo rv = EAGAIN; 19251ae08745Sheppo break; 19261ae08745Sheppo 19271ae08745Sheppo case LDC_RDX: 19281ae08745Sheppo /* process RDX message */ 19291ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 19301ae08745Sheppo break; 19311ae08745Sheppo 19321ae08745Sheppo default: 19331ae08745Sheppo DWARN(ldcp->id, 19341ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 19351ae08745Sheppo "tstate=0x%x\n", ldcp->id, 19361ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 19371ae08745Sheppo break; 19381ae08745Sheppo } 19391ae08745Sheppo } 19401ae08745Sheppo 19411ae08745Sheppo return (rv); 19421ae08745Sheppo } 19431ae08745Sheppo 19441ae08745Sheppo /* 19451ae08745Sheppo * Register channel with the channel nexus 19461ae08745Sheppo */ 19471ae08745Sheppo static int 19481ae08745Sheppo i_ldc_register_channel(ldc_chan_t *ldcp) 19491ae08745Sheppo { 19501ae08745Sheppo int rv = 0; 19511ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 19521ae08745Sheppo 19531ae08745Sheppo if (cinfo->dip == NULL) { 19541ae08745Sheppo DWARN(ldcp->id, 19551ae08745Sheppo "i_ldc_register_channel: cnex has not registered\n"); 19561ae08745Sheppo return (EAGAIN); 19571ae08745Sheppo } 19581ae08745Sheppo 19591ae08745Sheppo rv = cinfo->reg_chan(cinfo->dip, ldcp->id, ldcp->devclass); 19601ae08745Sheppo if (rv) { 19611ae08745Sheppo DWARN(ldcp->id, 19621ae08745Sheppo "i_ldc_register_channel: cannot register channel\n"); 19631ae08745Sheppo return (rv); 19641ae08745Sheppo } 19651ae08745Sheppo 19661ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR, 19671ae08745Sheppo i_ldc_tx_hdlr, ldcp, NULL); 19681ae08745Sheppo if (rv) { 19691ae08745Sheppo DWARN(ldcp->id, 19701ae08745Sheppo "i_ldc_register_channel: cannot add Tx interrupt\n"); 19711ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 19721ae08745Sheppo return (rv); 19731ae08745Sheppo } 19741ae08745Sheppo 19751ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR, 19761ae08745Sheppo i_ldc_rx_hdlr, ldcp, NULL); 19771ae08745Sheppo if (rv) { 19781ae08745Sheppo DWARN(ldcp->id, 19791ae08745Sheppo "i_ldc_register_channel: cannot add Rx interrupt\n"); 19801ae08745Sheppo (void) cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 19811ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 19821ae08745Sheppo return (rv); 19831ae08745Sheppo } 19841ae08745Sheppo 19851ae08745Sheppo ldcp->tstate |= TS_CNEX_RDY; 19861ae08745Sheppo 19871ae08745Sheppo return (0); 19881ae08745Sheppo } 19891ae08745Sheppo 19901ae08745Sheppo /* 19911ae08745Sheppo * Unregister a channel with the channel nexus 19921ae08745Sheppo */ 19931ae08745Sheppo static int 19941ae08745Sheppo i_ldc_unregister_channel(ldc_chan_t *ldcp) 19951ae08745Sheppo { 19961ae08745Sheppo int rv = 0; 19971ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 19981ae08745Sheppo 19991ae08745Sheppo if (cinfo->dip == NULL) { 20001ae08745Sheppo DWARN(ldcp->id, 20011ae08745Sheppo "i_ldc_unregister_channel: cnex has not registered\n"); 20021ae08745Sheppo return (EAGAIN); 20031ae08745Sheppo } 20041ae08745Sheppo 20051ae08745Sheppo if (ldcp->tstate & TS_CNEX_RDY) { 20061ae08745Sheppo 2007d10e4ef2Snarayan /* Remove the Rx interrupt */ 20081ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR); 20091ae08745Sheppo if (rv) { 20103af08d82Slm66018 if (rv != EAGAIN) { 20111ae08745Sheppo DWARN(ldcp->id, 20123af08d82Slm66018 "i_ldc_unregister_channel: err removing " 20133af08d82Slm66018 "Rx intr\n"); 2014d10e4ef2Snarayan return (rv); 20151ae08745Sheppo } 2016d10e4ef2Snarayan 20173af08d82Slm66018 /* 20183af08d82Slm66018 * If interrupts are pending and handler has 20193af08d82Slm66018 * finished running, clear interrupt and try 20203af08d82Slm66018 * again 20213af08d82Slm66018 */ 20223af08d82Slm66018 if (ldcp->rx_intr_state != LDC_INTR_PEND) 20233af08d82Slm66018 return (rv); 20243af08d82Slm66018 20253af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 20263af08d82Slm66018 rv = cinfo->rem_intr(cinfo->dip, ldcp->id, 20273af08d82Slm66018 CNEX_RX_INTR); 20283af08d82Slm66018 if (rv) { 20293af08d82Slm66018 DWARN(ldcp->id, "i_ldc_unregister_channel: " 20303af08d82Slm66018 "err removing Rx interrupt\n"); 20313af08d82Slm66018 return (rv); 20323af08d82Slm66018 } 20333af08d82Slm66018 } 20343af08d82Slm66018 2035d10e4ef2Snarayan /* Remove the Tx interrupt */ 20361ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 20371ae08745Sheppo if (rv) { 20381ae08745Sheppo DWARN(ldcp->id, 20391ae08745Sheppo "i_ldc_unregister_channel: err removing Tx intr\n"); 2040d10e4ef2Snarayan return (rv); 20411ae08745Sheppo } 2042d10e4ef2Snarayan 2043d10e4ef2Snarayan /* Unregister the channel */ 20441ae08745Sheppo rv = cinfo->unreg_chan(ldcssp->cinfo.dip, ldcp->id); 20451ae08745Sheppo if (rv) { 20461ae08745Sheppo DWARN(ldcp->id, 20471ae08745Sheppo "i_ldc_unregister_channel: cannot unreg channel\n"); 2048d10e4ef2Snarayan return (rv); 20491ae08745Sheppo } 20501ae08745Sheppo 20511ae08745Sheppo ldcp->tstate &= ~TS_CNEX_RDY; 20521ae08745Sheppo } 20531ae08745Sheppo 20541ae08745Sheppo return (0); 20551ae08745Sheppo } 20561ae08745Sheppo 20571ae08745Sheppo 20581ae08745Sheppo /* 20591ae08745Sheppo * LDC transmit interrupt handler 20601ae08745Sheppo * triggered for chanel up/down/reset events 20611ae08745Sheppo * and Tx queue content changes 20621ae08745Sheppo */ 20631ae08745Sheppo static uint_t 20641ae08745Sheppo i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2) 20651ae08745Sheppo { 20661ae08745Sheppo _NOTE(ARGUNUSED(arg2)) 20671ae08745Sheppo 20681ae08745Sheppo int rv; 20691ae08745Sheppo ldc_chan_t *ldcp; 20701ae08745Sheppo boolean_t notify_client = B_FALSE; 20713af08d82Slm66018 uint64_t notify_event = 0, link_state; 20721ae08745Sheppo 20731ae08745Sheppo /* Get the channel for which interrupt was received */ 20741ae08745Sheppo ASSERT(arg1 != NULL); 20751ae08745Sheppo ldcp = (ldc_chan_t *)arg1; 20761ae08745Sheppo 20771ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 20781ae08745Sheppo ldcp->id, ldcp); 20791ae08745Sheppo 20801ae08745Sheppo /* Lock channel */ 20811ae08745Sheppo mutex_enter(&ldcp->lock); 20821ae08745Sheppo 2083d10e4ef2Snarayan /* Obtain Tx lock */ 2084d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2085d10e4ef2Snarayan 20864bac2208Snarayan /* mark interrupt as pending */ 20873af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_ACTIVE; 20883af08d82Slm66018 20893af08d82Slm66018 /* save current link state */ 20903af08d82Slm66018 link_state = ldcp->link_state; 20914bac2208Snarayan 20921ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &ldcp->tx_head, &ldcp->tx_tail, 20931ae08745Sheppo &ldcp->link_state); 20941ae08745Sheppo if (rv) { 20951ae08745Sheppo cmn_err(CE_WARN, 20961ae08745Sheppo "i_ldc_tx_hdlr: (0x%lx) cannot read queue ptrs rv=0x%d\n", 20971ae08745Sheppo ldcp->id, rv); 20984bac2208Snarayan i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 2099d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 21001ae08745Sheppo mutex_exit(&ldcp->lock); 21011ae08745Sheppo return (DDI_INTR_CLAIMED); 21021ae08745Sheppo } 21031ae08745Sheppo 21041ae08745Sheppo /* 21051ae08745Sheppo * reset the channel state if the channel went down 21061ae08745Sheppo * (other side unconfigured queue) or channel was reset 21071ae08745Sheppo * (other side reconfigured its queue) 21081ae08745Sheppo */ 21093af08d82Slm66018 if (link_state != ldcp->link_state && 21103af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_DOWN) { 21111ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link down\n", ldcp->id); 21123af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 21131ae08745Sheppo notify_client = B_TRUE; 21141ae08745Sheppo notify_event = LDC_EVT_DOWN; 21151ae08745Sheppo } 21161ae08745Sheppo 21173af08d82Slm66018 if (link_state != ldcp->link_state && 21183af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 21191ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link reset\n", ldcp->id); 21203af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 21211ae08745Sheppo notify_client = B_TRUE; 21221ae08745Sheppo notify_event = LDC_EVT_RESET; 21231ae08745Sheppo } 21241ae08745Sheppo 21253af08d82Slm66018 if (link_state != ldcp->link_state && 21263af08d82Slm66018 (ldcp->tstate & ~TS_IN_RESET) == TS_OPEN && 21273af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_UP) { 21281ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link up\n", ldcp->id); 21291ae08745Sheppo notify_client = B_TRUE; 21301ae08745Sheppo notify_event = LDC_EVT_RESET; 21311ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 21321ae08745Sheppo ldcp->status = LDC_READY; 21331ae08745Sheppo } 21341ae08745Sheppo 21351ae08745Sheppo /* if callbacks are disabled, do not notify */ 21361ae08745Sheppo if (!ldcp->cb_enabled) 21371ae08745Sheppo notify_client = B_FALSE; 21381ae08745Sheppo 21394d39be2bSsg70180 i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 214022f747efSnarayan mutex_exit(&ldcp->tx_lock); 21411ae08745Sheppo 21421ae08745Sheppo if (notify_client) { 21433af08d82Slm66018 ldcp->cb_inprogress = B_TRUE; 21443af08d82Slm66018 mutex_exit(&ldcp->lock); 21451ae08745Sheppo rv = ldcp->cb(notify_event, ldcp->cb_arg); 21461ae08745Sheppo if (rv) { 21471ae08745Sheppo DWARN(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) callback " 21481ae08745Sheppo "failure", ldcp->id); 21491ae08745Sheppo } 21501ae08745Sheppo mutex_enter(&ldcp->lock); 21511ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 21521ae08745Sheppo } 21531ae08745Sheppo 21541ae08745Sheppo mutex_exit(&ldcp->lock); 21551ae08745Sheppo 21561ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) exiting handler", ldcp->id); 21571ae08745Sheppo 21581ae08745Sheppo return (DDI_INTR_CLAIMED); 21591ae08745Sheppo } 21601ae08745Sheppo 21611ae08745Sheppo /* 216258283286Sha137994 * Process the Rx HV queue. 216358283286Sha137994 * 216458283286Sha137994 * Returns 0 if data packets were found and no errors were encountered, 216558283286Sha137994 * otherwise returns an error. In either case, the *notify argument is 216658283286Sha137994 * set to indicate whether or not the client callback function should 216758283286Sha137994 * be invoked. The *event argument is set to contain the callback event. 216858283286Sha137994 * 216958283286Sha137994 * Depending on the channel mode, packets are handled differently: 217058283286Sha137994 * 217158283286Sha137994 * RAW MODE 217258283286Sha137994 * For raw mode channels, when a data packet is encountered, 217358283286Sha137994 * processing stops and all packets are left on the queue to be removed 217458283286Sha137994 * and processed by the ldc_read code path. 217558283286Sha137994 * 217658283286Sha137994 * UNRELIABLE MODE 217758283286Sha137994 * For unreliable mode, when a data packet is encountered, processing 217858283286Sha137994 * stops, and all packets are left on the queue to be removed and 217958283286Sha137994 * processed by the ldc_read code path. Control packets are processed 218058283286Sha137994 * inline if they are encountered before any data packets. 218158283286Sha137994 * 218220ae46ebSha137994 * RELIABLE MODE 218320ae46ebSha137994 * For reliable mode channels, all packets on the receive queue 218458283286Sha137994 * are processed: data packets are copied to the data queue and 218558283286Sha137994 * control packets are processed inline. Packets are only left on 218658283286Sha137994 * the receive queue when the data queue is full. 21871ae08745Sheppo */ 21881ae08745Sheppo static uint_t 218958283286Sha137994 i_ldc_rx_process_hvq(ldc_chan_t *ldcp, boolean_t *notify_client, 219058283286Sha137994 uint64_t *notify_event) 21911ae08745Sheppo { 21921ae08745Sheppo int rv; 21931ae08745Sheppo uint64_t rx_head, rx_tail; 21941ae08745Sheppo ldc_msg_t *msg; 21953af08d82Slm66018 uint64_t link_state, first_fragment = 0; 219658283286Sha137994 boolean_t trace_length = B_TRUE; 21973af08d82Slm66018 219858283286Sha137994 ASSERT(MUTEX_HELD(&ldcp->lock)); 219958283286Sha137994 *notify_client = B_FALSE; 220058283286Sha137994 *notify_event = 0; 22011ae08745Sheppo 22021ae08745Sheppo /* 22031ae08745Sheppo * Read packet(s) from the queue 22041ae08745Sheppo */ 22051ae08745Sheppo for (;;) { 22061ae08745Sheppo 22073af08d82Slm66018 link_state = ldcp->link_state; 22081ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 22091ae08745Sheppo &ldcp->link_state); 22101ae08745Sheppo if (rv) { 22111ae08745Sheppo cmn_err(CE_WARN, 221258283286Sha137994 "i_ldc_rx_process_hvq: (0x%lx) cannot read " 22131ae08745Sheppo "queue ptrs, rv=0x%d\n", ldcp->id, rv); 22141ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 221558283286Sha137994 return (EIO); 22161ae08745Sheppo } 22171ae08745Sheppo 22181ae08745Sheppo /* 22191ae08745Sheppo * reset the channel state if the channel went down 22201ae08745Sheppo * (other side unconfigured queue) or channel was reset 22213af08d82Slm66018 * (other side reconfigured its queue) 22221ae08745Sheppo */ 22233af08d82Slm66018 22243af08d82Slm66018 if (link_state != ldcp->link_state) { 2225cb112a14Slm66018 22263af08d82Slm66018 switch (ldcp->link_state) { 22273af08d82Slm66018 case LDC_CHANNEL_DOWN: 222858283286Sha137994 D1(ldcp->id, "i_ldc_rx_process_hvq: channel " 22293af08d82Slm66018 "link down\n", ldcp->id); 2230d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 22313af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 2232d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 223358283286Sha137994 *notify_client = B_TRUE; 223458283286Sha137994 *notify_event = LDC_EVT_DOWN; 22353af08d82Slm66018 goto loop_exit; 22361ae08745Sheppo 22373af08d82Slm66018 case LDC_CHANNEL_UP: 223858283286Sha137994 D1(ldcp->id, "i_ldc_rx_process_hvq: " 22393af08d82Slm66018 "channel link up\n", ldcp->id); 22403af08d82Slm66018 22413af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_OPEN) { 224258283286Sha137994 *notify_client = B_TRUE; 224358283286Sha137994 *notify_event = LDC_EVT_RESET; 22441ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 22451ae08745Sheppo ldcp->status = LDC_READY; 22461ae08745Sheppo } 22473af08d82Slm66018 break; 22483af08d82Slm66018 22493af08d82Slm66018 case LDC_CHANNEL_RESET: 22503af08d82Slm66018 default: 22513af08d82Slm66018 #ifdef DEBUG 22523af08d82Slm66018 force_reset: 22533af08d82Slm66018 #endif 225458283286Sha137994 D1(ldcp->id, "i_ldc_rx_process_hvq: channel " 22553af08d82Slm66018 "link reset\n", ldcp->id); 22563af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 22573af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 22583af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 225958283286Sha137994 *notify_client = B_TRUE; 226058283286Sha137994 *notify_event = LDC_EVT_RESET; 22613af08d82Slm66018 break; 22623af08d82Slm66018 } 22633af08d82Slm66018 } 22643af08d82Slm66018 22653af08d82Slm66018 #ifdef DEBUG 22663af08d82Slm66018 if (LDC_INJECT_RESET(ldcp)) 22673af08d82Slm66018 goto force_reset; 2268bbfa0259Sha137994 if (LDC_INJECT_DRNGCLEAR(ldcp)) 2269bbfa0259Sha137994 i_ldc_mem_inject_dring_clear(ldcp); 22703af08d82Slm66018 #endif 227158283286Sha137994 if (trace_length) { 227258283286Sha137994 TRACE_RXHVQ_LENGTH(ldcp, rx_head, rx_tail); 227358283286Sha137994 trace_length = B_FALSE; 227458283286Sha137994 } 22751ae08745Sheppo 22761ae08745Sheppo if (rx_head == rx_tail) { 227758283286Sha137994 D2(ldcp->id, "i_ldc_rx_process_hvq: (0x%llx) " 227858283286Sha137994 "No packets\n", ldcp->id); 22791ae08745Sheppo break; 22801ae08745Sheppo } 22813af08d82Slm66018 228258283286Sha137994 D2(ldcp->id, "i_ldc_rx_process_hvq: head=0x%llx, " 228358283286Sha137994 "tail=0x%llx\n", rx_head, rx_tail); 228458283286Sha137994 DUMP_LDC_PKT(ldcp, "i_ldc_rx_process_hvq rcd", 22851ae08745Sheppo ldcp->rx_q_va + rx_head); 22861ae08745Sheppo 22871ae08745Sheppo /* get the message */ 22881ae08745Sheppo msg = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 22891ae08745Sheppo 22901ae08745Sheppo /* if channel is in RAW mode or data pkt, notify and return */ 22911ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 229258283286Sha137994 *notify_client = B_TRUE; 229358283286Sha137994 *notify_event |= LDC_EVT_READ; 22941ae08745Sheppo break; 22951ae08745Sheppo } 22961ae08745Sheppo 22971ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 22981ae08745Sheppo 22991ae08745Sheppo /* discard packet if channel is not up */ 23003af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) != TS_UP) { 23011ae08745Sheppo 23021ae08745Sheppo /* move the head one position */ 23031ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 23041ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 23051ae08745Sheppo 23061ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, rx_head)) 23071ae08745Sheppo break; 23081ae08745Sheppo 23091ae08745Sheppo continue; 23101ae08745Sheppo } else { 231158283286Sha137994 uint64_t dq_head, dq_tail; 231258283286Sha137994 231320ae46ebSha137994 /* process only RELIABLE mode data packets */ 231420ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) { 23153af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 231658283286Sha137994 *notify_client = B_TRUE; 231758283286Sha137994 *notify_event |= LDC_EVT_READ; 23181ae08745Sheppo break; 23191ae08745Sheppo } 232058283286Sha137994 232158283286Sha137994 /* don't process packet if queue full */ 232258283286Sha137994 (void) i_ldc_dq_rx_get_state(ldcp, &dq_head, 232358283286Sha137994 &dq_tail, NULL); 232458283286Sha137994 dq_tail = (dq_tail + LDC_PACKET_SIZE) % 232558283286Sha137994 (ldcp->rx_dq_entries << LDC_PACKET_SHIFT); 232658283286Sha137994 if (dq_tail == dq_head || 232758283286Sha137994 LDC_INJECT_DQFULL(ldcp)) { 232858283286Sha137994 rv = ENOSPC; 232958283286Sha137994 break; 233058283286Sha137994 } 233158283286Sha137994 } 23321ae08745Sheppo } 23331ae08745Sheppo 23341ae08745Sheppo /* Check the sequence ID for the message received */ 23353af08d82Slm66018 rv = i_ldc_check_seqid(ldcp, msg); 23363af08d82Slm66018 if (rv != 0) { 23371ae08745Sheppo 233858283286Sha137994 DWARN(ldcp->id, "i_ldc_rx_process_hvq: (0x%llx) " 233958283286Sha137994 "seqid error, q_ptrs=0x%lx,0x%lx", ldcp->id, 234058283286Sha137994 rx_head, rx_tail); 23411ae08745Sheppo 23421ae08745Sheppo /* Reset last_msg_rcd to start of message */ 2343d10e4ef2Snarayan if (first_fragment != 0) { 2344d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 2345d10e4ef2Snarayan first_fragment = 0; 23461ae08745Sheppo } 2347d10e4ef2Snarayan 23481ae08745Sheppo /* 23491ae08745Sheppo * Send a NACK due to seqid mismatch 23501ae08745Sheppo */ 235122f747efSnarayan rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK, 23521ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 23531ae08745Sheppo 23541ae08745Sheppo if (rv) { 235558283286Sha137994 cmn_err(CE_NOTE, "i_ldc_rx_process_hvq: " 235658283286Sha137994 "(0x%lx) err sending CTRL/DATA NACK msg\n", 235758283286Sha137994 ldcp->id); 2358d10e4ef2Snarayan 2359d10e4ef2Snarayan /* if cannot send NACK - reset channel */ 2360d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 23613af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 2362d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 236383d3bc6fSnarayan 236458283286Sha137994 *notify_client = B_TRUE; 236558283286Sha137994 *notify_event = LDC_EVT_RESET; 2366d10e4ef2Snarayan break; 23671ae08745Sheppo } 23681ae08745Sheppo 23691ae08745Sheppo /* purge receive queue */ 23701ae08745Sheppo (void) i_ldc_set_rx_head(ldcp, rx_tail); 23711ae08745Sheppo break; 23721ae08745Sheppo } 23731ae08745Sheppo 23741ae08745Sheppo /* record the message ID */ 23751ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 23761ae08745Sheppo 23771ae08745Sheppo /* process control messages */ 23781ae08745Sheppo if (msg->type & LDC_CTRL) { 23791ae08745Sheppo /* save current internal state */ 23801ae08745Sheppo uint64_t tstate = ldcp->tstate; 23811ae08745Sheppo 23821ae08745Sheppo rv = i_ldc_ctrlmsg(ldcp, msg); 23831ae08745Sheppo if (rv == EAGAIN) { 23841ae08745Sheppo /* re-process pkt - state was adjusted */ 23851ae08745Sheppo continue; 23861ae08745Sheppo } 23871ae08745Sheppo if (rv == ECONNRESET) { 238858283286Sha137994 *notify_client = B_TRUE; 238958283286Sha137994 *notify_event = LDC_EVT_RESET; 23901ae08745Sheppo break; 23911ae08745Sheppo } 23921ae08745Sheppo 23931ae08745Sheppo /* 23941ae08745Sheppo * control message processing was successful 23951ae08745Sheppo * channel transitioned to ready for communication 23961ae08745Sheppo */ 23971ae08745Sheppo if (rv == 0 && ldcp->tstate == TS_UP && 23983af08d82Slm66018 (tstate & ~TS_IN_RESET) != 23993af08d82Slm66018 (ldcp->tstate & ~TS_IN_RESET)) { 240058283286Sha137994 *notify_client = B_TRUE; 240158283286Sha137994 *notify_event = LDC_EVT_UP; 24021ae08745Sheppo } 24031ae08745Sheppo } 24041ae08745Sheppo 240583d3bc6fSnarayan /* process data NACKs */ 240683d3bc6fSnarayan if ((msg->type & LDC_DATA) && (msg->stype & LDC_NACK)) { 240783d3bc6fSnarayan DWARN(ldcp->id, 240858283286Sha137994 "i_ldc_rx_process_hvq: (0x%llx) received DATA/NACK", 240983d3bc6fSnarayan ldcp->id); 241083d3bc6fSnarayan mutex_enter(&ldcp->tx_lock); 241183d3bc6fSnarayan i_ldc_reset(ldcp, B_TRUE); 241283d3bc6fSnarayan mutex_exit(&ldcp->tx_lock); 241358283286Sha137994 *notify_client = B_TRUE; 241458283286Sha137994 *notify_event = LDC_EVT_RESET; 241583d3bc6fSnarayan break; 241683d3bc6fSnarayan } 241783d3bc6fSnarayan 24181ae08745Sheppo /* process data ACKs */ 24191ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 2420d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 242158283286Sha137994 *notify_client = B_TRUE; 242258283286Sha137994 *notify_event = LDC_EVT_RESET; 2423d10e4ef2Snarayan break; 2424d10e4ef2Snarayan } 24251ae08745Sheppo } 24261ae08745Sheppo 242758283286Sha137994 if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 242820ae46ebSha137994 ASSERT(ldcp->mode == LDC_MODE_RELIABLE); 242958283286Sha137994 243058283286Sha137994 /* 243158283286Sha137994 * Copy the data packet to the data queue. Note 243258283286Sha137994 * that the copy routine updates the rx_head pointer. 243358283286Sha137994 */ 243458283286Sha137994 i_ldc_rxdq_copy(ldcp, &rx_head); 243558283286Sha137994 243658283286Sha137994 if ((ldcp->tstate & TS_IN_RESET) == 0) 243758283286Sha137994 *notify_client = B_TRUE; 243858283286Sha137994 *notify_event |= LDC_EVT_READ; 243958283286Sha137994 } else { 24401ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 24411ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 244258283286Sha137994 } 244358283286Sha137994 244458283286Sha137994 /* move the head one position */ 24450a55fbb7Slm66018 if (rv = i_ldc_set_rx_head(ldcp, rx_head)) { 244658283286Sha137994 *notify_client = B_TRUE; 244758283286Sha137994 *notify_event = LDC_EVT_RESET; 24481ae08745Sheppo break; 24490a55fbb7Slm66018 } 24501ae08745Sheppo 24511ae08745Sheppo } /* for */ 24521ae08745Sheppo 24533af08d82Slm66018 loop_exit: 24543af08d82Slm66018 245520ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 245658283286Sha137994 /* ACK data packets */ 245758283286Sha137994 if ((*notify_event & 245858283286Sha137994 (LDC_EVT_READ | LDC_EVT_RESET)) == LDC_EVT_READ) { 245958283286Sha137994 int ack_rv; 246058283286Sha137994 ack_rv = i_ldc_send_pkt(ldcp, LDC_DATA, LDC_ACK, 0); 246158283286Sha137994 if (ack_rv && ack_rv != EWOULDBLOCK) { 246258283286Sha137994 cmn_err(CE_NOTE, 246358283286Sha137994 "i_ldc_rx_process_hvq: (0x%lx) cannot " 246458283286Sha137994 "send ACK\n", ldcp->id); 246558283286Sha137994 246658283286Sha137994 mutex_enter(&ldcp->tx_lock); 246758283286Sha137994 i_ldc_reset(ldcp, B_FALSE); 246858283286Sha137994 mutex_exit(&ldcp->tx_lock); 246958283286Sha137994 247058283286Sha137994 *notify_client = B_TRUE; 247158283286Sha137994 *notify_event = LDC_EVT_RESET; 247258283286Sha137994 goto skip_ackpeek; 247358283286Sha137994 } 247458283286Sha137994 } 24751ae08745Sheppo 24763af08d82Slm66018 /* 247758283286Sha137994 * If we have no more space on the data queue, make sure 247858283286Sha137994 * there are no ACKs on the rx queue waiting to be processed. 24793af08d82Slm66018 */ 248058283286Sha137994 if (rv == ENOSPC) { 248158283286Sha137994 if (i_ldc_rx_ackpeek(ldcp, rx_head, rx_tail) != 0) { 248258283286Sha137994 ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID; 248358283286Sha137994 *notify_client = B_TRUE; 248458283286Sha137994 *notify_event = LDC_EVT_RESET; 24851ae08745Sheppo } 248612f80fa6Sha137994 return (rv); 248758283286Sha137994 } else { 248858283286Sha137994 ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID; 248958283286Sha137994 } 24901ae08745Sheppo } 24911ae08745Sheppo 249258283286Sha137994 skip_ackpeek: 24934d39be2bSsg70180 249458283286Sha137994 /* Return, indicating whether or not data packets were found */ 249558283286Sha137994 if ((*notify_event & (LDC_EVT_READ | LDC_EVT_RESET)) == LDC_EVT_READ) 249658283286Sha137994 return (0); 249758283286Sha137994 249858283286Sha137994 return (ENOMSG); 24991ae08745Sheppo } 25001ae08745Sheppo 250158283286Sha137994 /* 250258283286Sha137994 * Process any ACK packets on the HV receive queue. 250358283286Sha137994 * 250420ae46ebSha137994 * This function is only used by RELIABLE mode channels when the 250558283286Sha137994 * secondary data queue fills up and there are packets remaining on 250658283286Sha137994 * the HV receive queue. 250758283286Sha137994 */ 250858283286Sha137994 int 250958283286Sha137994 i_ldc_rx_ackpeek(ldc_chan_t *ldcp, uint64_t rx_head, uint64_t rx_tail) 251058283286Sha137994 { 251158283286Sha137994 int rv = 0; 251258283286Sha137994 ldc_msg_t *msg; 251358283286Sha137994 251458283286Sha137994 if (ldcp->rx_ack_head == ACKPEEK_HEAD_INVALID) 251558283286Sha137994 ldcp->rx_ack_head = rx_head; 251658283286Sha137994 251758283286Sha137994 while (ldcp->rx_ack_head != rx_tail) { 251858283286Sha137994 msg = (ldc_msg_t *)(ldcp->rx_q_va + ldcp->rx_ack_head); 251958283286Sha137994 252058283286Sha137994 if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 252158283286Sha137994 if (rv = i_ldc_process_data_ACK(ldcp, msg)) 252258283286Sha137994 break; 252358283286Sha137994 msg->stype &= ~LDC_ACK; 252458283286Sha137994 } 252558283286Sha137994 252658283286Sha137994 ldcp->rx_ack_head = 252758283286Sha137994 (ldcp->rx_ack_head + LDC_PACKET_SIZE) % 252858283286Sha137994 (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 252958283286Sha137994 } 253058283286Sha137994 return (rv); 253158283286Sha137994 } 25321ae08745Sheppo 25331ae08745Sheppo /* -------------------------------------------------------------------------- */ 25341ae08745Sheppo 25351ae08745Sheppo /* 25361ae08745Sheppo * LDC API functions 25371ae08745Sheppo */ 25381ae08745Sheppo 25391ae08745Sheppo /* 25401ae08745Sheppo * Initialize the channel. Allocate internal structure and memory for 25411ae08745Sheppo * TX/RX queues, and initialize locks. 25421ae08745Sheppo */ 25431ae08745Sheppo int 25441ae08745Sheppo ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle) 25451ae08745Sheppo { 25461ae08745Sheppo ldc_chan_t *ldcp; 25471ae08745Sheppo int rv, exit_val; 25481ae08745Sheppo uint64_t ra_base, nentries; 2549e1ebb9ecSlm66018 uint64_t qlen; 25501ae08745Sheppo 25511ae08745Sheppo exit_val = EINVAL; /* guarantee an error if exit on failure */ 25521ae08745Sheppo 25531ae08745Sheppo if (attr == NULL) { 25541ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid attr\n", id); 25551ae08745Sheppo return (EINVAL); 25561ae08745Sheppo } 25571ae08745Sheppo if (handle == NULL) { 25581ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid handle\n", id); 25591ae08745Sheppo return (EINVAL); 25601ae08745Sheppo } 25611ae08745Sheppo 25621ae08745Sheppo /* check if channel is valid */ 25631ae08745Sheppo rv = hv_ldc_tx_qinfo(id, &ra_base, &nentries); 25641ae08745Sheppo if (rv == H_ECHANNEL) { 25651ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid channel id\n", id); 25661ae08745Sheppo return (EINVAL); 25671ae08745Sheppo } 25681ae08745Sheppo 25691ae08745Sheppo /* check if the channel has already been initialized */ 25701ae08745Sheppo mutex_enter(&ldcssp->lock); 25711ae08745Sheppo ldcp = ldcssp->chan_list; 25721ae08745Sheppo while (ldcp != NULL) { 25731ae08745Sheppo if (ldcp->id == id) { 25741ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) already initialized\n", 25751ae08745Sheppo id); 25761ae08745Sheppo mutex_exit(&ldcssp->lock); 25771ae08745Sheppo return (EADDRINUSE); 25781ae08745Sheppo } 25791ae08745Sheppo ldcp = ldcp->next; 25801ae08745Sheppo } 25811ae08745Sheppo mutex_exit(&ldcssp->lock); 25821ae08745Sheppo 25831ae08745Sheppo ASSERT(ldcp == NULL); 25841ae08745Sheppo 25851ae08745Sheppo *handle = 0; 25861ae08745Sheppo 25871ae08745Sheppo /* Allocate an ldcp structure */ 25881ae08745Sheppo ldcp = kmem_zalloc(sizeof (ldc_chan_t), KM_SLEEP); 25891ae08745Sheppo 2590d10e4ef2Snarayan /* 2591d10e4ef2Snarayan * Initialize the channel and Tx lock 2592d10e4ef2Snarayan * 2593d10e4ef2Snarayan * The channel 'lock' protects the entire channel and 2594d10e4ef2Snarayan * should be acquired before initializing, resetting, 2595d10e4ef2Snarayan * destroying or reading from a channel. 2596d10e4ef2Snarayan * 2597d10e4ef2Snarayan * The 'tx_lock' should be acquired prior to transmitting 2598d10e4ef2Snarayan * data over the channel. The lock should also be acquired 2599d10e4ef2Snarayan * prior to channel reconfiguration (in order to prevent 2600d10e4ef2Snarayan * concurrent writes). 2601d10e4ef2Snarayan * 2602d10e4ef2Snarayan * ORDERING: When both locks are being acquired, to prevent 2603d10e4ef2Snarayan * deadlocks, the channel lock should be always acquired prior 2604d10e4ef2Snarayan * to the tx_lock. 2605d10e4ef2Snarayan */ 26061ae08745Sheppo mutex_init(&ldcp->lock, NULL, MUTEX_DRIVER, NULL); 2607d10e4ef2Snarayan mutex_init(&ldcp->tx_lock, NULL, MUTEX_DRIVER, NULL); 26081ae08745Sheppo 26091ae08745Sheppo /* Initialize the channel */ 26101ae08745Sheppo ldcp->id = id; 26111ae08745Sheppo ldcp->cb = NULL; 26121ae08745Sheppo ldcp->cb_arg = NULL; 26131ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 26141ae08745Sheppo ldcp->cb_enabled = B_FALSE; 26151ae08745Sheppo ldcp->next = NULL; 26161ae08745Sheppo 26171ae08745Sheppo /* Read attributes */ 26181ae08745Sheppo ldcp->mode = attr->mode; 26191ae08745Sheppo ldcp->devclass = attr->devclass; 26201ae08745Sheppo ldcp->devinst = attr->instance; 2621e1ebb9ecSlm66018 ldcp->mtu = (attr->mtu > 0) ? attr->mtu : LDC_DEFAULT_MTU; 26221ae08745Sheppo 26231ae08745Sheppo D1(ldcp->id, 26241ae08745Sheppo "ldc_init: (0x%llx) channel attributes, class=0x%x, " 2625e1ebb9ecSlm66018 "instance=0x%llx, mode=%d, mtu=%d\n", 2626e1ebb9ecSlm66018 ldcp->id, ldcp->devclass, ldcp->devinst, ldcp->mode, ldcp->mtu); 26271ae08745Sheppo 26281ae08745Sheppo ldcp->next_vidx = 0; 26293af08d82Slm66018 ldcp->tstate = TS_IN_RESET; 26301ae08745Sheppo ldcp->hstate = 0; 26311ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 26321ae08745Sheppo ldcp->last_ack_rcd = 0; 26331ae08745Sheppo ldcp->last_msg_rcd = 0; 263458283286Sha137994 ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID; 26351ae08745Sheppo 26361ae08745Sheppo ldcp->stream_bufferp = NULL; 26371ae08745Sheppo ldcp->exp_dring_list = NULL; 26381ae08745Sheppo ldcp->imp_dring_list = NULL; 26391ae08745Sheppo ldcp->mhdl_list = NULL; 26401ae08745Sheppo 26413af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_NONE; 26423af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_NONE; 26433af08d82Slm66018 26441ae08745Sheppo /* Initialize payload size depending on whether channel is reliable */ 26451ae08745Sheppo switch (ldcp->mode) { 26461ae08745Sheppo case LDC_MODE_RAW: 26471ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RAW; 26481ae08745Sheppo ldcp->read_p = i_ldc_read_raw; 26491ae08745Sheppo ldcp->write_p = i_ldc_write_raw; 26501ae08745Sheppo break; 26511ae08745Sheppo case LDC_MODE_UNRELIABLE: 26521ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_UNRELIABLE; 26531ae08745Sheppo ldcp->read_p = i_ldc_read_packet; 26541ae08745Sheppo ldcp->write_p = i_ldc_write_packet; 26551ae08745Sheppo break; 26561ae08745Sheppo case LDC_MODE_RELIABLE: 26571ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE; 26581ae08745Sheppo 26591ae08745Sheppo ldcp->stream_remains = 0; 26601ae08745Sheppo ldcp->stream_offset = 0; 26611ae08745Sheppo ldcp->stream_bufferp = kmem_alloc(ldcp->mtu, KM_SLEEP); 26621ae08745Sheppo ldcp->read_p = i_ldc_read_stream; 26631ae08745Sheppo ldcp->write_p = i_ldc_write_stream; 26641ae08745Sheppo break; 26651ae08745Sheppo default: 26661ae08745Sheppo exit_val = EINVAL; 26671ae08745Sheppo goto cleanup_on_exit; 26681ae08745Sheppo } 26691ae08745Sheppo 2670e1ebb9ecSlm66018 /* 2671e1ebb9ecSlm66018 * qlen is (mtu * ldc_mtu_msgs) / pkt_payload. If this 2672e1ebb9ecSlm66018 * value is smaller than default length of ldc_queue_entries, 267322f747efSnarayan * qlen is set to ldc_queue_entries. Ensure that computed 267422f747efSnarayan * length is a power-of-two value. 2675e1ebb9ecSlm66018 */ 2676e1ebb9ecSlm66018 qlen = (ldcp->mtu * ldc_mtu_msgs) / ldcp->pkt_payload; 267722f747efSnarayan if (!ISP2(qlen)) { 267822f747efSnarayan uint64_t tmp = 1; 267922f747efSnarayan while (qlen) { 268022f747efSnarayan qlen >>= 1; tmp <<= 1; 268122f747efSnarayan } 268222f747efSnarayan qlen = tmp; 268322f747efSnarayan } 268422f747efSnarayan 2685e1ebb9ecSlm66018 ldcp->rx_q_entries = 2686e1ebb9ecSlm66018 (qlen < ldc_queue_entries) ? ldc_queue_entries : qlen; 2687e1ebb9ecSlm66018 ldcp->tx_q_entries = ldcp->rx_q_entries; 2688e1ebb9ecSlm66018 268922f747efSnarayan D1(ldcp->id, "ldc_init: queue length = 0x%llx\n", ldcp->rx_q_entries); 2690e1ebb9ecSlm66018 26911ae08745Sheppo /* Create a transmit queue */ 26921ae08745Sheppo ldcp->tx_q_va = (uint64_t) 26931ae08745Sheppo contig_mem_alloc(ldcp->tx_q_entries << LDC_PACKET_SHIFT); 26941ae08745Sheppo if (ldcp->tx_q_va == NULL) { 26951ae08745Sheppo cmn_err(CE_WARN, 26961ae08745Sheppo "ldc_init: (0x%lx) TX queue allocation failed\n", 26971ae08745Sheppo ldcp->id); 26981ae08745Sheppo exit_val = ENOMEM; 26991ae08745Sheppo goto cleanup_on_exit; 27001ae08745Sheppo } 27011ae08745Sheppo ldcp->tx_q_ra = va_to_pa((caddr_t)ldcp->tx_q_va); 27021ae08745Sheppo 27031ae08745Sheppo D2(ldcp->id, "ldc_init: txq_va=0x%llx, txq_ra=0x%llx, entries=0x%llx\n", 27041ae08745Sheppo ldcp->tx_q_va, ldcp->tx_q_ra, ldcp->tx_q_entries); 27051ae08745Sheppo 27061ae08745Sheppo ldcp->tstate |= TS_TXQ_RDY; 27071ae08745Sheppo 27081ae08745Sheppo /* Create a receive queue */ 27091ae08745Sheppo ldcp->rx_q_va = (uint64_t) 27101ae08745Sheppo contig_mem_alloc(ldcp->rx_q_entries << LDC_PACKET_SHIFT); 27111ae08745Sheppo if (ldcp->rx_q_va == NULL) { 27121ae08745Sheppo cmn_err(CE_WARN, 27131ae08745Sheppo "ldc_init: (0x%lx) RX queue allocation failed\n", 27141ae08745Sheppo ldcp->id); 27151ae08745Sheppo exit_val = ENOMEM; 27161ae08745Sheppo goto cleanup_on_exit; 27171ae08745Sheppo } 27181ae08745Sheppo ldcp->rx_q_ra = va_to_pa((caddr_t)ldcp->rx_q_va); 27191ae08745Sheppo 27201ae08745Sheppo D2(ldcp->id, "ldc_init: rxq_va=0x%llx, rxq_ra=0x%llx, entries=0x%llx\n", 27211ae08745Sheppo ldcp->rx_q_va, ldcp->rx_q_ra, ldcp->rx_q_entries); 27221ae08745Sheppo 27231ae08745Sheppo ldcp->tstate |= TS_RXQ_RDY; 27241ae08745Sheppo 272558283286Sha137994 /* Setup a separate read data queue */ 272620ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 272758283286Sha137994 ldcp->readq_get_state = i_ldc_dq_rx_get_state; 272858283286Sha137994 ldcp->readq_set_head = i_ldc_set_rxdq_head; 272958283286Sha137994 273058283286Sha137994 /* Make sure the data queue multiplier is a power of 2 */ 273158283286Sha137994 if (!ISP2(ldc_rxdq_multiplier)) { 273258283286Sha137994 D1(ldcp->id, "ldc_init: (0x%llx) ldc_rxdq_multiplier " 273358283286Sha137994 "not a power of 2, resetting", ldcp->id); 273458283286Sha137994 ldc_rxdq_multiplier = LDC_RXDQ_MULTIPLIER; 273558283286Sha137994 } 273658283286Sha137994 273758283286Sha137994 ldcp->rx_dq_entries = ldc_rxdq_multiplier * ldcp->rx_q_entries; 273858283286Sha137994 ldcp->rx_dq_va = (uint64_t) 273958283286Sha137994 kmem_alloc(ldcp->rx_dq_entries << LDC_PACKET_SHIFT, 274058283286Sha137994 KM_SLEEP); 274158283286Sha137994 if (ldcp->rx_dq_va == NULL) { 274258283286Sha137994 cmn_err(CE_WARN, 274358283286Sha137994 "ldc_init: (0x%lx) RX data queue " 274458283286Sha137994 "allocation failed\n", ldcp->id); 274558283286Sha137994 exit_val = ENOMEM; 274658283286Sha137994 goto cleanup_on_exit; 274758283286Sha137994 } 274858283286Sha137994 274958283286Sha137994 ldcp->rx_dq_head = ldcp->rx_dq_tail = 0; 275058283286Sha137994 275158283286Sha137994 D2(ldcp->id, "ldc_init: rx_dq_va=0x%llx, " 275258283286Sha137994 "rx_dq_entries=0x%llx\n", ldcp->rx_dq_va, 275358283286Sha137994 ldcp->rx_dq_entries); 275458283286Sha137994 } else { 275558283286Sha137994 ldcp->readq_get_state = i_ldc_hvq_rx_get_state; 275658283286Sha137994 ldcp->readq_set_head = i_ldc_set_rx_head; 275758283286Sha137994 } 275858283286Sha137994 27591ae08745Sheppo /* Init descriptor ring and memory handle list lock */ 27601ae08745Sheppo mutex_init(&ldcp->exp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 27611ae08745Sheppo mutex_init(&ldcp->imp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 27621ae08745Sheppo mutex_init(&ldcp->mlist_lock, NULL, MUTEX_DRIVER, NULL); 27631ae08745Sheppo 27641ae08745Sheppo /* mark status as INITialized */ 27651ae08745Sheppo ldcp->status = LDC_INIT; 27661ae08745Sheppo 27671ae08745Sheppo /* Add to channel list */ 27681ae08745Sheppo mutex_enter(&ldcssp->lock); 27691ae08745Sheppo ldcp->next = ldcssp->chan_list; 27701ae08745Sheppo ldcssp->chan_list = ldcp; 27711ae08745Sheppo ldcssp->channel_count++; 27721ae08745Sheppo mutex_exit(&ldcssp->lock); 27731ae08745Sheppo 27741ae08745Sheppo /* set the handle */ 27751ae08745Sheppo *handle = (ldc_handle_t)ldcp; 27761ae08745Sheppo 27771ae08745Sheppo D1(ldcp->id, "ldc_init: (0x%llx) channel initialized\n", ldcp->id); 27781ae08745Sheppo 27791ae08745Sheppo return (0); 27801ae08745Sheppo 27811ae08745Sheppo cleanup_on_exit: 27821ae08745Sheppo 278320ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE && ldcp->stream_bufferp) 27841ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 27851ae08745Sheppo 27861ae08745Sheppo if (ldcp->tstate & TS_TXQ_RDY) 27871ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 27881ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 27891ae08745Sheppo 27901ae08745Sheppo if (ldcp->tstate & TS_RXQ_RDY) 27911ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 27921ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 27931ae08745Sheppo 2794d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 27951ae08745Sheppo mutex_destroy(&ldcp->lock); 27961ae08745Sheppo 27971ae08745Sheppo if (ldcp) 27981ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 27991ae08745Sheppo 28001ae08745Sheppo return (exit_val); 28011ae08745Sheppo } 28021ae08745Sheppo 28031ae08745Sheppo /* 28041ae08745Sheppo * Finalizes the LDC connection. It will return EBUSY if the 28051ae08745Sheppo * channel is open. A ldc_close() has to be done prior to 28061ae08745Sheppo * a ldc_fini operation. It frees TX/RX queues, associated 28071ae08745Sheppo * with the channel 28081ae08745Sheppo */ 28091ae08745Sheppo int 28101ae08745Sheppo ldc_fini(ldc_handle_t handle) 28111ae08745Sheppo { 28121ae08745Sheppo ldc_chan_t *ldcp; 28131ae08745Sheppo ldc_chan_t *tmp_ldcp; 28141ae08745Sheppo uint64_t id; 28151ae08745Sheppo 28161ae08745Sheppo if (handle == NULL) { 28171ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel handle\n"); 28181ae08745Sheppo return (EINVAL); 28191ae08745Sheppo } 28201ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28211ae08745Sheppo id = ldcp->id; 28221ae08745Sheppo 28231ae08745Sheppo mutex_enter(&ldcp->lock); 28241ae08745Sheppo 28253af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) > TS_INIT) { 28261ae08745Sheppo DWARN(ldcp->id, "ldc_fini: (0x%llx) channel is open\n", 28271ae08745Sheppo ldcp->id); 28281ae08745Sheppo mutex_exit(&ldcp->lock); 28291ae08745Sheppo return (EBUSY); 28301ae08745Sheppo } 28311ae08745Sheppo 28321ae08745Sheppo /* Remove from the channel list */ 28331ae08745Sheppo mutex_enter(&ldcssp->lock); 28341ae08745Sheppo tmp_ldcp = ldcssp->chan_list; 28351ae08745Sheppo if (tmp_ldcp == ldcp) { 28361ae08745Sheppo ldcssp->chan_list = ldcp->next; 28371ae08745Sheppo ldcp->next = NULL; 28381ae08745Sheppo } else { 28391ae08745Sheppo while (tmp_ldcp != NULL) { 28401ae08745Sheppo if (tmp_ldcp->next == ldcp) { 28411ae08745Sheppo tmp_ldcp->next = ldcp->next; 28421ae08745Sheppo ldcp->next = NULL; 28431ae08745Sheppo break; 28441ae08745Sheppo } 28451ae08745Sheppo tmp_ldcp = tmp_ldcp->next; 28461ae08745Sheppo } 28471ae08745Sheppo if (tmp_ldcp == NULL) { 28481ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel hdl\n"); 28491ae08745Sheppo mutex_exit(&ldcssp->lock); 28501ae08745Sheppo mutex_exit(&ldcp->lock); 28511ae08745Sheppo return (EINVAL); 28521ae08745Sheppo } 28531ae08745Sheppo } 28541ae08745Sheppo 28551ae08745Sheppo ldcssp->channel_count--; 28561ae08745Sheppo 28571ae08745Sheppo mutex_exit(&ldcssp->lock); 28581ae08745Sheppo 28591ae08745Sheppo /* Free the map table for this channel */ 28601ae08745Sheppo if (ldcp->mtbl) { 28611ae08745Sheppo (void) hv_ldc_set_map_table(ldcp->id, NULL, NULL); 28623af08d82Slm66018 if (ldcp->mtbl->contigmem) 28631ae08745Sheppo contig_mem_free(ldcp->mtbl->table, ldcp->mtbl->size); 28643af08d82Slm66018 else 28653af08d82Slm66018 kmem_free(ldcp->mtbl->table, ldcp->mtbl->size); 28661ae08745Sheppo mutex_destroy(&ldcp->mtbl->lock); 28671ae08745Sheppo kmem_free(ldcp->mtbl, sizeof (ldc_mtbl_t)); 28681ae08745Sheppo } 28691ae08745Sheppo 28701ae08745Sheppo /* Destroy descriptor ring and memory handle list lock */ 28711ae08745Sheppo mutex_destroy(&ldcp->exp_dlist_lock); 28721ae08745Sheppo mutex_destroy(&ldcp->imp_dlist_lock); 28731ae08745Sheppo mutex_destroy(&ldcp->mlist_lock); 28741ae08745Sheppo 287520ae46ebSha137994 /* Free the stream buffer for RELIABLE_MODE */ 287620ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE && ldcp->stream_bufferp) 28771ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 28781ae08745Sheppo 28791ae08745Sheppo /* Free the RX queue */ 28801ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 28811ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 28821ae08745Sheppo ldcp->tstate &= ~TS_RXQ_RDY; 28831ae08745Sheppo 288458283286Sha137994 /* Free the RX data queue */ 288520ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 288658283286Sha137994 kmem_free((caddr_t)ldcp->rx_dq_va, 288758283286Sha137994 (ldcp->rx_dq_entries << LDC_PACKET_SHIFT)); 288858283286Sha137994 } 288958283286Sha137994 28901ae08745Sheppo /* Free the TX queue */ 28911ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 28921ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 28931ae08745Sheppo ldcp->tstate &= ~TS_TXQ_RDY; 28941ae08745Sheppo 28951ae08745Sheppo mutex_exit(&ldcp->lock); 28961ae08745Sheppo 28971ae08745Sheppo /* Destroy mutex */ 2898d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 28991ae08745Sheppo mutex_destroy(&ldcp->lock); 29001ae08745Sheppo 29011ae08745Sheppo /* free channel structure */ 29021ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 29031ae08745Sheppo 29041ae08745Sheppo D1(id, "ldc_fini: (0x%llx) channel finalized\n", id); 29051ae08745Sheppo 29061ae08745Sheppo return (0); 29071ae08745Sheppo } 29081ae08745Sheppo 29091ae08745Sheppo /* 29101ae08745Sheppo * Open the LDC channel for use. It registers the TX/RX queues 29111ae08745Sheppo * with the Hypervisor. It also specifies the interrupt number 29121ae08745Sheppo * and target CPU for this channel 29131ae08745Sheppo */ 29141ae08745Sheppo int 29151ae08745Sheppo ldc_open(ldc_handle_t handle) 29161ae08745Sheppo { 29171ae08745Sheppo ldc_chan_t *ldcp; 29181ae08745Sheppo int rv; 29191ae08745Sheppo 29201ae08745Sheppo if (handle == NULL) { 29211ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_open: invalid channel handle\n"); 29221ae08745Sheppo return (EINVAL); 29231ae08745Sheppo } 29241ae08745Sheppo 29251ae08745Sheppo ldcp = (ldc_chan_t *)handle; 29261ae08745Sheppo 29271ae08745Sheppo mutex_enter(&ldcp->lock); 29281ae08745Sheppo 29291ae08745Sheppo if (ldcp->tstate < TS_INIT) { 29301ae08745Sheppo DWARN(ldcp->id, 29311ae08745Sheppo "ldc_open: (0x%llx) channel not initialized\n", ldcp->id); 29321ae08745Sheppo mutex_exit(&ldcp->lock); 29331ae08745Sheppo return (EFAULT); 29341ae08745Sheppo } 29353af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) >= TS_OPEN) { 29361ae08745Sheppo DWARN(ldcp->id, 29371ae08745Sheppo "ldc_open: (0x%llx) channel is already open\n", ldcp->id); 29381ae08745Sheppo mutex_exit(&ldcp->lock); 29391ae08745Sheppo return (EFAULT); 29401ae08745Sheppo } 29411ae08745Sheppo 29421ae08745Sheppo /* 29431ae08745Sheppo * Unregister/Register the tx queue with the hypervisor 29441ae08745Sheppo */ 29451ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 29461ae08745Sheppo if (rv) { 29471ae08745Sheppo cmn_err(CE_WARN, 29481ae08745Sheppo "ldc_open: (0x%lx) channel tx queue unconf failed\n", 29491ae08745Sheppo ldcp->id); 29501ae08745Sheppo mutex_exit(&ldcp->lock); 29511ae08745Sheppo return (EIO); 29521ae08745Sheppo } 29531ae08745Sheppo 29541ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 29551ae08745Sheppo if (rv) { 29561ae08745Sheppo cmn_err(CE_WARN, 29571ae08745Sheppo "ldc_open: (0x%lx) channel tx queue conf failed\n", 29581ae08745Sheppo ldcp->id); 29591ae08745Sheppo mutex_exit(&ldcp->lock); 29601ae08745Sheppo return (EIO); 29611ae08745Sheppo } 29621ae08745Sheppo 29631ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered tx queue with LDC\n", 29641ae08745Sheppo ldcp->id); 29651ae08745Sheppo 29661ae08745Sheppo /* 29671ae08745Sheppo * Unregister/Register the rx queue with the hypervisor 29681ae08745Sheppo */ 29691ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 29701ae08745Sheppo if (rv) { 29711ae08745Sheppo cmn_err(CE_WARN, 29721ae08745Sheppo "ldc_open: (0x%lx) channel rx queue unconf failed\n", 29731ae08745Sheppo ldcp->id); 29741ae08745Sheppo mutex_exit(&ldcp->lock); 29751ae08745Sheppo return (EIO); 29761ae08745Sheppo } 29771ae08745Sheppo 29781ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, ldcp->rx_q_entries); 29791ae08745Sheppo if (rv) { 29801ae08745Sheppo cmn_err(CE_WARN, 29811ae08745Sheppo "ldc_open: (0x%lx) channel rx queue conf failed\n", 29821ae08745Sheppo ldcp->id); 29831ae08745Sheppo mutex_exit(&ldcp->lock); 29841ae08745Sheppo return (EIO); 29851ae08745Sheppo } 29861ae08745Sheppo 29871ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered rx queue with LDC\n", 29881ae08745Sheppo ldcp->id); 29891ae08745Sheppo 29901ae08745Sheppo ldcp->tstate |= TS_QCONF_RDY; 29911ae08745Sheppo 29921ae08745Sheppo /* Register the channel with the channel nexus */ 29931ae08745Sheppo rv = i_ldc_register_channel(ldcp); 29941ae08745Sheppo if (rv && rv != EAGAIN) { 29951ae08745Sheppo cmn_err(CE_WARN, 29961ae08745Sheppo "ldc_open: (0x%lx) channel register failed\n", ldcp->id); 2997*5699897cSHaik Aftandilian ldcp->tstate &= ~TS_QCONF_RDY; 29981ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 29991ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 30001ae08745Sheppo mutex_exit(&ldcp->lock); 30011ae08745Sheppo return (EIO); 30021ae08745Sheppo } 30031ae08745Sheppo 30041ae08745Sheppo /* mark channel in OPEN state */ 30051ae08745Sheppo ldcp->status = LDC_OPEN; 30061ae08745Sheppo 30071ae08745Sheppo /* Read channel state */ 30081ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 30091ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 30101ae08745Sheppo if (rv) { 30111ae08745Sheppo cmn_err(CE_WARN, 30121ae08745Sheppo "ldc_open: (0x%lx) cannot read channel state\n", 30131ae08745Sheppo ldcp->id); 30141ae08745Sheppo (void) i_ldc_unregister_channel(ldcp); 3015*5699897cSHaik Aftandilian ldcp->tstate &= ~TS_QCONF_RDY; 30161ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 30171ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 30181ae08745Sheppo mutex_exit(&ldcp->lock); 30191ae08745Sheppo return (EIO); 30201ae08745Sheppo } 30211ae08745Sheppo 30221ae08745Sheppo /* 302320ae46ebSha137994 * set the ACKd head to current head location for reliable 30241ae08745Sheppo */ 30251ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 30261ae08745Sheppo 30271ae08745Sheppo /* mark channel ready if HV report link is UP (peer alloc'd Rx queue) */ 30281ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 30291ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 30301ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 30311ae08745Sheppo ldcp->status = LDC_READY; 30321ae08745Sheppo } 30331ae08745Sheppo 30341ae08745Sheppo /* 30351ae08745Sheppo * if channel is being opened in RAW mode - no handshake is needed 30361ae08745Sheppo * switch the channel READY and UP state 30371ae08745Sheppo */ 30381ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 30391ae08745Sheppo ldcp->tstate = TS_UP; /* set bits associated with LDC UP */ 30401ae08745Sheppo ldcp->status = LDC_UP; 30411ae08745Sheppo } 30421ae08745Sheppo 30431ae08745Sheppo mutex_exit(&ldcp->lock); 30441ae08745Sheppo 30451ae08745Sheppo /* 30461ae08745Sheppo * Increment number of open channels 30471ae08745Sheppo */ 30481ae08745Sheppo mutex_enter(&ldcssp->lock); 30491ae08745Sheppo ldcssp->channels_open++; 30501ae08745Sheppo mutex_exit(&ldcssp->lock); 30511ae08745Sheppo 3052cb112a14Slm66018 D1(ldcp->id, 30533af08d82Slm66018 "ldc_open: (0x%llx) channel (0x%p) open for use " 30543af08d82Slm66018 "(tstate=0x%x, status=0x%x)\n", 30553af08d82Slm66018 ldcp->id, ldcp, ldcp->tstate, ldcp->status); 30561ae08745Sheppo 30571ae08745Sheppo return (0); 30581ae08745Sheppo } 30591ae08745Sheppo 30601ae08745Sheppo /* 30611ae08745Sheppo * Close the LDC connection. It will return EBUSY if there 30621ae08745Sheppo * are memory segments or descriptor rings either bound to or 30631ae08745Sheppo * mapped over the channel 30641ae08745Sheppo */ 30651ae08745Sheppo int 30661ae08745Sheppo ldc_close(ldc_handle_t handle) 30671ae08745Sheppo { 30681ae08745Sheppo ldc_chan_t *ldcp; 3069d10e4ef2Snarayan int rv = 0, retries = 0; 30701ae08745Sheppo boolean_t chk_done = B_FALSE; 30711ae08745Sheppo 30721ae08745Sheppo if (handle == NULL) { 30731ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_close: invalid channel handle\n"); 30741ae08745Sheppo return (EINVAL); 30751ae08745Sheppo } 30761ae08745Sheppo ldcp = (ldc_chan_t *)handle; 30771ae08745Sheppo 30781ae08745Sheppo mutex_enter(&ldcp->lock); 30791ae08745Sheppo 30801ae08745Sheppo /* return error if channel is not open */ 30813af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) < TS_OPEN) { 30821ae08745Sheppo DWARN(ldcp->id, 30831ae08745Sheppo "ldc_close: (0x%llx) channel is not open\n", ldcp->id); 30841ae08745Sheppo mutex_exit(&ldcp->lock); 30851ae08745Sheppo return (EFAULT); 30861ae08745Sheppo } 30871ae08745Sheppo 30881ae08745Sheppo /* if any memory handles, drings, are bound or mapped cannot close */ 30891ae08745Sheppo if (ldcp->mhdl_list != NULL) { 30901ae08745Sheppo DWARN(ldcp->id, 30911ae08745Sheppo "ldc_close: (0x%llx) channel has bound memory handles\n", 30921ae08745Sheppo ldcp->id); 30931ae08745Sheppo mutex_exit(&ldcp->lock); 30941ae08745Sheppo return (EBUSY); 30951ae08745Sheppo } 30961ae08745Sheppo if (ldcp->exp_dring_list != NULL) { 30971ae08745Sheppo DWARN(ldcp->id, 30981ae08745Sheppo "ldc_close: (0x%llx) channel has bound descriptor rings\n", 30991ae08745Sheppo ldcp->id); 31001ae08745Sheppo mutex_exit(&ldcp->lock); 31011ae08745Sheppo return (EBUSY); 31021ae08745Sheppo } 31031ae08745Sheppo if (ldcp->imp_dring_list != NULL) { 31041ae08745Sheppo DWARN(ldcp->id, 31051ae08745Sheppo "ldc_close: (0x%llx) channel has mapped descriptor rings\n", 31061ae08745Sheppo ldcp->id); 31071ae08745Sheppo mutex_exit(&ldcp->lock); 31081ae08745Sheppo return (EBUSY); 31091ae08745Sheppo } 31101ae08745Sheppo 31114d39be2bSsg70180 if (ldcp->cb_inprogress) { 31124d39be2bSsg70180 DWARN(ldcp->id, "ldc_close: (0x%llx) callback active\n", 31134d39be2bSsg70180 ldcp->id); 31144d39be2bSsg70180 mutex_exit(&ldcp->lock); 31154d39be2bSsg70180 return (EWOULDBLOCK); 31164d39be2bSsg70180 } 31174d39be2bSsg70180 3118d10e4ef2Snarayan /* Obtain Tx lock */ 3119d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3120d10e4ef2Snarayan 31211ae08745Sheppo /* 31221ae08745Sheppo * Wait for pending transmits to complete i.e Tx queue to drain 31231ae08745Sheppo * if there are pending pkts - wait 1 ms and retry again 31241ae08745Sheppo */ 31251ae08745Sheppo for (;;) { 31261ae08745Sheppo 31271ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 31281ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 31291ae08745Sheppo if (rv) { 31301ae08745Sheppo cmn_err(CE_WARN, 31311ae08745Sheppo "ldc_close: (0x%lx) cannot read qptrs\n", ldcp->id); 3132d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 31331ae08745Sheppo mutex_exit(&ldcp->lock); 31341ae08745Sheppo return (EIO); 31351ae08745Sheppo } 31361ae08745Sheppo 31371ae08745Sheppo if (ldcp->tx_head == ldcp->tx_tail || 31381ae08745Sheppo ldcp->link_state != LDC_CHANNEL_UP) { 31391ae08745Sheppo break; 31401ae08745Sheppo } 31411ae08745Sheppo 31421ae08745Sheppo if (chk_done) { 31431ae08745Sheppo DWARN(ldcp->id, 31441ae08745Sheppo "ldc_close: (0x%llx) Tx queue drain timeout\n", 31451ae08745Sheppo ldcp->id); 31461ae08745Sheppo break; 31471ae08745Sheppo } 31481ae08745Sheppo 31491ae08745Sheppo /* wait for one ms and try again */ 31501ae08745Sheppo delay(drv_usectohz(1000)); 31511ae08745Sheppo chk_done = B_TRUE; 31521ae08745Sheppo } 31531ae08745Sheppo 31541ae08745Sheppo /* 3155a8ea4edeSnarayan * Drain the Tx and Rx queues as we are closing the 3156a8ea4edeSnarayan * channel. We dont care about any pending packets. 3157a8ea4edeSnarayan * We have to also drain the queue prior to clearing 3158a8ea4edeSnarayan * pending interrupts, otherwise the HV will trigger 3159a8ea4edeSnarayan * an interrupt the moment the interrupt state is 3160a8ea4edeSnarayan * cleared. 31613af08d82Slm66018 */ 31623af08d82Slm66018 (void) i_ldc_txq_reconf(ldcp); 3163a8ea4edeSnarayan (void) i_ldc_rxq_drain(ldcp); 31643af08d82Slm66018 31653af08d82Slm66018 /* 31661ae08745Sheppo * Unregister the channel with the nexus 31671ae08745Sheppo */ 3168d10e4ef2Snarayan while ((rv = i_ldc_unregister_channel(ldcp)) != 0) { 3169d10e4ef2Snarayan 3170d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 31711ae08745Sheppo mutex_exit(&ldcp->lock); 3172d10e4ef2Snarayan 3173d10e4ef2Snarayan /* if any error other than EAGAIN return back */ 3174a8ea4edeSnarayan if (rv != EAGAIN || retries >= ldc_max_retries) { 3175d10e4ef2Snarayan cmn_err(CE_WARN, 3176d10e4ef2Snarayan "ldc_close: (0x%lx) unregister failed, %d\n", 3177d10e4ef2Snarayan ldcp->id, rv); 31781ae08745Sheppo return (rv); 31791ae08745Sheppo } 31801ae08745Sheppo 31811ae08745Sheppo /* 3182d10e4ef2Snarayan * As there could be pending interrupts we need 3183d10e4ef2Snarayan * to wait and try again 3184d10e4ef2Snarayan */ 31854d39be2bSsg70180 drv_usecwait(ldc_close_delay); 3186d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3187d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3188d10e4ef2Snarayan retries++; 3189d10e4ef2Snarayan } 3190d10e4ef2Snarayan 3191*5699897cSHaik Aftandilian ldcp->tstate &= ~TS_QCONF_RDY; 3192*5699897cSHaik Aftandilian 3193d10e4ef2Snarayan /* 31941ae08745Sheppo * Unregister queues 31951ae08745Sheppo */ 31961ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 31971ae08745Sheppo if (rv) { 31981ae08745Sheppo cmn_err(CE_WARN, 31991ae08745Sheppo "ldc_close: (0x%lx) channel TX queue unconf failed\n", 32001ae08745Sheppo ldcp->id); 3201d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 32021ae08745Sheppo mutex_exit(&ldcp->lock); 32031ae08745Sheppo return (EIO); 32041ae08745Sheppo } 32051ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 32061ae08745Sheppo if (rv) { 32071ae08745Sheppo cmn_err(CE_WARN, 32081ae08745Sheppo "ldc_close: (0x%lx) channel RX queue unconf failed\n", 32091ae08745Sheppo ldcp->id); 3210d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 32111ae08745Sheppo mutex_exit(&ldcp->lock); 32121ae08745Sheppo return (EIO); 32131ae08745Sheppo } 32141ae08745Sheppo 32151ae08745Sheppo /* Reset channel state information */ 32161ae08745Sheppo i_ldc_reset_state(ldcp); 32171ae08745Sheppo 32181ae08745Sheppo /* Mark channel as down and in initialized state */ 32191ae08745Sheppo ldcp->tx_ackd_head = 0; 32201ae08745Sheppo ldcp->tx_head = 0; 32213af08d82Slm66018 ldcp->tstate = TS_IN_RESET|TS_INIT; 32221ae08745Sheppo ldcp->status = LDC_INIT; 32231ae08745Sheppo 3224d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 32251ae08745Sheppo mutex_exit(&ldcp->lock); 32261ae08745Sheppo 32271ae08745Sheppo /* Decrement number of open channels */ 32281ae08745Sheppo mutex_enter(&ldcssp->lock); 32291ae08745Sheppo ldcssp->channels_open--; 32301ae08745Sheppo mutex_exit(&ldcssp->lock); 32311ae08745Sheppo 32321ae08745Sheppo D1(ldcp->id, "ldc_close: (0x%llx) channel closed\n", ldcp->id); 32331ae08745Sheppo 32341ae08745Sheppo return (0); 32351ae08745Sheppo } 32361ae08745Sheppo 32371ae08745Sheppo /* 32381ae08745Sheppo * Register channel callback 32391ae08745Sheppo */ 32401ae08745Sheppo int 32411ae08745Sheppo ldc_reg_callback(ldc_handle_t handle, 32421ae08745Sheppo uint_t(*cb)(uint64_t event, caddr_t arg), caddr_t arg) 32431ae08745Sheppo { 32441ae08745Sheppo ldc_chan_t *ldcp; 32451ae08745Sheppo 32461ae08745Sheppo if (handle == NULL) { 32471ae08745Sheppo DWARN(DBG_ALL_LDCS, 32481ae08745Sheppo "ldc_reg_callback: invalid channel handle\n"); 32491ae08745Sheppo return (EINVAL); 32501ae08745Sheppo } 32511ae08745Sheppo if (((uint64_t)cb) < KERNELBASE) { 32521ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_reg_callback: invalid callback\n"); 32531ae08745Sheppo return (EINVAL); 32541ae08745Sheppo } 32551ae08745Sheppo ldcp = (ldc_chan_t *)handle; 32561ae08745Sheppo 32571ae08745Sheppo mutex_enter(&ldcp->lock); 32581ae08745Sheppo 32591ae08745Sheppo if (ldcp->cb) { 32601ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback exists\n", 32611ae08745Sheppo ldcp->id); 32621ae08745Sheppo mutex_exit(&ldcp->lock); 32631ae08745Sheppo return (EIO); 32641ae08745Sheppo } 32651ae08745Sheppo if (ldcp->cb_inprogress) { 32661ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback active\n", 32671ae08745Sheppo ldcp->id); 32681ae08745Sheppo mutex_exit(&ldcp->lock); 32691ae08745Sheppo return (EWOULDBLOCK); 32701ae08745Sheppo } 32711ae08745Sheppo 32721ae08745Sheppo ldcp->cb = cb; 32731ae08745Sheppo ldcp->cb_arg = arg; 32741ae08745Sheppo ldcp->cb_enabled = B_TRUE; 32751ae08745Sheppo 32761ae08745Sheppo D1(ldcp->id, 32771ae08745Sheppo "ldc_reg_callback: (0x%llx) registered callback for channel\n", 32781ae08745Sheppo ldcp->id); 32791ae08745Sheppo 32801ae08745Sheppo mutex_exit(&ldcp->lock); 32811ae08745Sheppo 32821ae08745Sheppo return (0); 32831ae08745Sheppo } 32841ae08745Sheppo 32851ae08745Sheppo /* 32861ae08745Sheppo * Unregister channel callback 32871ae08745Sheppo */ 32881ae08745Sheppo int 32891ae08745Sheppo ldc_unreg_callback(ldc_handle_t handle) 32901ae08745Sheppo { 32911ae08745Sheppo ldc_chan_t *ldcp; 32921ae08745Sheppo 32931ae08745Sheppo if (handle == NULL) { 32941ae08745Sheppo DWARN(DBG_ALL_LDCS, 32951ae08745Sheppo "ldc_unreg_callback: invalid channel handle\n"); 32961ae08745Sheppo return (EINVAL); 32971ae08745Sheppo } 32981ae08745Sheppo ldcp = (ldc_chan_t *)handle; 32991ae08745Sheppo 33001ae08745Sheppo mutex_enter(&ldcp->lock); 33011ae08745Sheppo 33021ae08745Sheppo if (ldcp->cb == NULL) { 33031ae08745Sheppo DWARN(ldcp->id, 33041ae08745Sheppo "ldc_unreg_callback: (0x%llx) no callback exists\n", 33051ae08745Sheppo ldcp->id); 33061ae08745Sheppo mutex_exit(&ldcp->lock); 33071ae08745Sheppo return (EIO); 33081ae08745Sheppo } 33091ae08745Sheppo if (ldcp->cb_inprogress) { 33101ae08745Sheppo DWARN(ldcp->id, 33111ae08745Sheppo "ldc_unreg_callback: (0x%llx) callback active\n", 33121ae08745Sheppo ldcp->id); 33131ae08745Sheppo mutex_exit(&ldcp->lock); 33141ae08745Sheppo return (EWOULDBLOCK); 33151ae08745Sheppo } 33161ae08745Sheppo 33171ae08745Sheppo ldcp->cb = NULL; 33181ae08745Sheppo ldcp->cb_arg = NULL; 33191ae08745Sheppo ldcp->cb_enabled = B_FALSE; 33201ae08745Sheppo 33211ae08745Sheppo D1(ldcp->id, 33221ae08745Sheppo "ldc_unreg_callback: (0x%llx) unregistered callback for channel\n", 33231ae08745Sheppo ldcp->id); 33241ae08745Sheppo 33251ae08745Sheppo mutex_exit(&ldcp->lock); 33261ae08745Sheppo 33271ae08745Sheppo return (0); 33281ae08745Sheppo } 33291ae08745Sheppo 33301ae08745Sheppo 33311ae08745Sheppo /* 33321ae08745Sheppo * Bring a channel up by initiating a handshake with the peer 33331ae08745Sheppo * This call is asynchronous. It will complete at a later point 33341ae08745Sheppo * in time when the peer responds back with an RTR. 33351ae08745Sheppo */ 33361ae08745Sheppo int 33371ae08745Sheppo ldc_up(ldc_handle_t handle) 33381ae08745Sheppo { 33391ae08745Sheppo int rv; 33401ae08745Sheppo ldc_chan_t *ldcp; 33411ae08745Sheppo ldc_msg_t *ldcmsg; 334257e6a936Ssb155480 uint64_t tx_tail, tstate, link_state; 33431ae08745Sheppo 33441ae08745Sheppo if (handle == NULL) { 33451ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_up: invalid channel handle\n"); 33461ae08745Sheppo return (EINVAL); 33471ae08745Sheppo } 33481ae08745Sheppo ldcp = (ldc_chan_t *)handle; 33491ae08745Sheppo 33501ae08745Sheppo mutex_enter(&ldcp->lock); 33511ae08745Sheppo 33523af08d82Slm66018 D1(ldcp->id, "ldc_up: (0x%llx) doing channel UP\n", ldcp->id); 33533af08d82Slm66018 33543af08d82Slm66018 /* clear the reset state */ 33553af08d82Slm66018 tstate = ldcp->tstate; 33563af08d82Slm66018 ldcp->tstate &= ~TS_IN_RESET; 33573af08d82Slm66018 33581ae08745Sheppo if (ldcp->tstate == TS_UP) { 33593af08d82Slm66018 DWARN(ldcp->id, 33601ae08745Sheppo "ldc_up: (0x%llx) channel is already in UP state\n", 33611ae08745Sheppo ldcp->id); 33623af08d82Slm66018 33633af08d82Slm66018 /* mark channel as up */ 33643af08d82Slm66018 ldcp->status = LDC_UP; 33653af08d82Slm66018 33663af08d82Slm66018 /* 33673af08d82Slm66018 * if channel was in reset state and there was 33683af08d82Slm66018 * pending data clear interrupt state. this will 33693af08d82Slm66018 * trigger an interrupt, causing the RX handler to 33703af08d82Slm66018 * to invoke the client's callback 33713af08d82Slm66018 */ 33723af08d82Slm66018 if ((tstate & TS_IN_RESET) && 33733af08d82Slm66018 ldcp->rx_intr_state == LDC_INTR_PEND) { 3374cb112a14Slm66018 D1(ldcp->id, 33753af08d82Slm66018 "ldc_up: (0x%llx) channel has pending data, " 33763af08d82Slm66018 "clearing interrupt\n", ldcp->id); 33773af08d82Slm66018 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 33783af08d82Slm66018 } 33793af08d82Slm66018 33801ae08745Sheppo mutex_exit(&ldcp->lock); 33811ae08745Sheppo return (0); 33821ae08745Sheppo } 33831ae08745Sheppo 33841ae08745Sheppo /* if the channel is in RAW mode - mark it as UP, if READY */ 33851ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW && ldcp->tstate >= TS_READY) { 33861ae08745Sheppo ldcp->tstate = TS_UP; 33871ae08745Sheppo mutex_exit(&ldcp->lock); 33881ae08745Sheppo return (0); 33891ae08745Sheppo } 33901ae08745Sheppo 33911ae08745Sheppo /* Don't start another handshake if there is one in progress */ 33921ae08745Sheppo if (ldcp->hstate) { 33933af08d82Slm66018 D1(ldcp->id, 33941ae08745Sheppo "ldc_up: (0x%llx) channel handshake in progress\n", 33951ae08745Sheppo ldcp->id); 33961ae08745Sheppo mutex_exit(&ldcp->lock); 33971ae08745Sheppo return (0); 33981ae08745Sheppo } 33991ae08745Sheppo 3400d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3401d10e4ef2Snarayan 340257e6a936Ssb155480 /* save current link state */ 340357e6a936Ssb155480 link_state = ldcp->link_state; 340457e6a936Ssb155480 34051ae08745Sheppo /* get the current tail for the LDC msg */ 34061ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 34071ae08745Sheppo if (rv) { 3408cb112a14Slm66018 D1(ldcp->id, "ldc_up: (0x%llx) cannot initiate handshake\n", 34091ae08745Sheppo ldcp->id); 3410d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 34111ae08745Sheppo mutex_exit(&ldcp->lock); 34121ae08745Sheppo return (ECONNREFUSED); 34131ae08745Sheppo } 34141ae08745Sheppo 341557e6a936Ssb155480 /* 341657e6a936Ssb155480 * If i_ldc_get_tx_tail() changed link_state to either RESET or UP, 341757e6a936Ssb155480 * from a previous state of DOWN, then mark the channel as 341857e6a936Ssb155480 * being ready for handshake. 341957e6a936Ssb155480 */ 342057e6a936Ssb155480 if ((link_state == LDC_CHANNEL_DOWN) && 342157e6a936Ssb155480 (link_state != ldcp->link_state)) { 342257e6a936Ssb155480 342357e6a936Ssb155480 ASSERT((ldcp->link_state == LDC_CHANNEL_RESET) || 342457e6a936Ssb155480 (ldcp->link_state == LDC_CHANNEL_UP)); 342557e6a936Ssb155480 342657e6a936Ssb155480 if (ldcp->mode == LDC_MODE_RAW) { 342757e6a936Ssb155480 ldcp->status = LDC_UP; 342857e6a936Ssb155480 ldcp->tstate = TS_UP; 342957e6a936Ssb155480 mutex_exit(&ldcp->tx_lock); 343057e6a936Ssb155480 mutex_exit(&ldcp->lock); 343157e6a936Ssb155480 return (0); 343257e6a936Ssb155480 } else { 343357e6a936Ssb155480 ldcp->status = LDC_READY; 343457e6a936Ssb155480 ldcp->tstate |= TS_LINK_READY; 343557e6a936Ssb155480 } 343657e6a936Ssb155480 343757e6a936Ssb155480 } 343857e6a936Ssb155480 34391ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 34401ae08745Sheppo ZERO_PKT(ldcmsg); 34411ae08745Sheppo 34421ae08745Sheppo ldcmsg->type = LDC_CTRL; 34431ae08745Sheppo ldcmsg->stype = LDC_INFO; 34441ae08745Sheppo ldcmsg->ctrl = LDC_VER; 34451ae08745Sheppo ldcp->next_vidx = 0; 34461ae08745Sheppo bcopy(&ldc_versions[0], ldcmsg->udata, sizeof (ldc_versions[0])); 34471ae08745Sheppo 34481ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_up snd ver", (uint64_t)ldcmsg); 34491ae08745Sheppo 34501ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 34511ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 34521ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 34531ae08745Sheppo 34541ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 34551ae08745Sheppo if (rv) { 34561ae08745Sheppo DWARN(ldcp->id, 34571ae08745Sheppo "ldc_up: (0x%llx) cannot initiate handshake rv=%d\n", 34581ae08745Sheppo ldcp->id, rv); 3459d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 34601ae08745Sheppo mutex_exit(&ldcp->lock); 34611ae08745Sheppo return (rv); 34621ae08745Sheppo } 34631ae08745Sheppo 34640a55fbb7Slm66018 ldcp->hstate |= TS_SENT_VER; 34651ae08745Sheppo ldcp->tx_tail = tx_tail; 34661ae08745Sheppo D1(ldcp->id, "ldc_up: (0x%llx) channel up initiated\n", ldcp->id); 34671ae08745Sheppo 3468d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 34691ae08745Sheppo mutex_exit(&ldcp->lock); 34701ae08745Sheppo 34711ae08745Sheppo return (rv); 34721ae08745Sheppo } 34731ae08745Sheppo 34741ae08745Sheppo 34751ae08745Sheppo /* 3476e1ebb9ecSlm66018 * Bring a channel down by resetting its state and queues 34771ae08745Sheppo */ 34781ae08745Sheppo int 3479e1ebb9ecSlm66018 ldc_down(ldc_handle_t handle) 34801ae08745Sheppo { 34811ae08745Sheppo ldc_chan_t *ldcp; 34821ae08745Sheppo 34831ae08745Sheppo if (handle == NULL) { 3484e1ebb9ecSlm66018 DWARN(DBG_ALL_LDCS, "ldc_down: invalid channel handle\n"); 34851ae08745Sheppo return (EINVAL); 34861ae08745Sheppo } 34871ae08745Sheppo ldcp = (ldc_chan_t *)handle; 34881ae08745Sheppo mutex_enter(&ldcp->lock); 3489d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 34903af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 3491d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 34921ae08745Sheppo mutex_exit(&ldcp->lock); 34931ae08745Sheppo 34941ae08745Sheppo return (0); 34951ae08745Sheppo } 34961ae08745Sheppo 34971ae08745Sheppo /* 34981ae08745Sheppo * Get the current channel status 34991ae08745Sheppo */ 35001ae08745Sheppo int 35011ae08745Sheppo ldc_status(ldc_handle_t handle, ldc_status_t *status) 35021ae08745Sheppo { 35031ae08745Sheppo ldc_chan_t *ldcp; 35041ae08745Sheppo 35051ae08745Sheppo if (handle == NULL || status == NULL) { 35061ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_status: invalid argument\n"); 35071ae08745Sheppo return (EINVAL); 35081ae08745Sheppo } 35091ae08745Sheppo ldcp = (ldc_chan_t *)handle; 35101ae08745Sheppo 35111ae08745Sheppo *status = ((ldc_chan_t *)handle)->status; 35121ae08745Sheppo 3513cb112a14Slm66018 D1(ldcp->id, 35141ae08745Sheppo "ldc_status: (0x%llx) returned status %d\n", ldcp->id, *status); 35151ae08745Sheppo return (0); 35161ae08745Sheppo } 35171ae08745Sheppo 35181ae08745Sheppo 35191ae08745Sheppo /* 35201ae08745Sheppo * Set the channel's callback mode - enable/disable callbacks 35211ae08745Sheppo */ 35221ae08745Sheppo int 35231ae08745Sheppo ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t cmode) 35241ae08745Sheppo { 35251ae08745Sheppo ldc_chan_t *ldcp; 35261ae08745Sheppo 35271ae08745Sheppo if (handle == NULL) { 35281ae08745Sheppo DWARN(DBG_ALL_LDCS, 35291ae08745Sheppo "ldc_set_intr_mode: invalid channel handle\n"); 35301ae08745Sheppo return (EINVAL); 35311ae08745Sheppo } 35321ae08745Sheppo ldcp = (ldc_chan_t *)handle; 35331ae08745Sheppo 35341ae08745Sheppo /* 35351ae08745Sheppo * Record no callbacks should be invoked 35361ae08745Sheppo */ 35371ae08745Sheppo mutex_enter(&ldcp->lock); 35381ae08745Sheppo 35391ae08745Sheppo switch (cmode) { 35401ae08745Sheppo case LDC_CB_DISABLE: 35411ae08745Sheppo if (!ldcp->cb_enabled) { 35421ae08745Sheppo DWARN(ldcp->id, 35431ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks disabled\n", 35441ae08745Sheppo ldcp->id); 35451ae08745Sheppo break; 35461ae08745Sheppo } 35471ae08745Sheppo ldcp->cb_enabled = B_FALSE; 35481ae08745Sheppo 35491ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) disabled callbacks\n", 35501ae08745Sheppo ldcp->id); 35511ae08745Sheppo break; 35521ae08745Sheppo 35531ae08745Sheppo case LDC_CB_ENABLE: 35541ae08745Sheppo if (ldcp->cb_enabled) { 35551ae08745Sheppo DWARN(ldcp->id, 35561ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks enabled\n", 35571ae08745Sheppo ldcp->id); 35581ae08745Sheppo break; 35591ae08745Sheppo } 35601ae08745Sheppo ldcp->cb_enabled = B_TRUE; 35611ae08745Sheppo 35621ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) enabled callbacks\n", 35631ae08745Sheppo ldcp->id); 35641ae08745Sheppo break; 35651ae08745Sheppo } 35661ae08745Sheppo 35671ae08745Sheppo mutex_exit(&ldcp->lock); 35681ae08745Sheppo 35691ae08745Sheppo return (0); 35701ae08745Sheppo } 35711ae08745Sheppo 35721ae08745Sheppo /* 35731ae08745Sheppo * Check to see if there are packets on the incoming queue 3574e1ebb9ecSlm66018 * Will return hasdata = B_FALSE if there are no packets 35751ae08745Sheppo */ 35761ae08745Sheppo int 3577e1ebb9ecSlm66018 ldc_chkq(ldc_handle_t handle, boolean_t *hasdata) 35781ae08745Sheppo { 35791ae08745Sheppo int rv; 35801ae08745Sheppo uint64_t rx_head, rx_tail; 35811ae08745Sheppo ldc_chan_t *ldcp; 35821ae08745Sheppo 35831ae08745Sheppo if (handle == NULL) { 35841ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_chkq: invalid channel handle\n"); 35851ae08745Sheppo return (EINVAL); 35861ae08745Sheppo } 35871ae08745Sheppo ldcp = (ldc_chan_t *)handle; 35881ae08745Sheppo 3589e1ebb9ecSlm66018 *hasdata = B_FALSE; 35901ae08745Sheppo 35911ae08745Sheppo mutex_enter(&ldcp->lock); 35921ae08745Sheppo 35931ae08745Sheppo if (ldcp->tstate != TS_UP) { 35941ae08745Sheppo D1(ldcp->id, 35951ae08745Sheppo "ldc_chkq: (0x%llx) channel is not up\n", ldcp->id); 35961ae08745Sheppo mutex_exit(&ldcp->lock); 35971ae08745Sheppo return (ECONNRESET); 35981ae08745Sheppo } 35991ae08745Sheppo 36001ae08745Sheppo /* Read packet(s) from the queue */ 36011ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 36021ae08745Sheppo &ldcp->link_state); 36031ae08745Sheppo if (rv != 0) { 36041ae08745Sheppo cmn_err(CE_WARN, 36051ae08745Sheppo "ldc_chkq: (0x%lx) unable to read queue ptrs", ldcp->id); 36061ae08745Sheppo mutex_exit(&ldcp->lock); 36071ae08745Sheppo return (EIO); 36081ae08745Sheppo } 360958283286Sha137994 36101ae08745Sheppo /* reset the channel state if the channel went down */ 36111ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 36121ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 3613d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 36143af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3615d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 36161ae08745Sheppo mutex_exit(&ldcp->lock); 36171ae08745Sheppo return (ECONNRESET); 36181ae08745Sheppo } 36191ae08745Sheppo 362058283286Sha137994 switch (ldcp->mode) { 362158283286Sha137994 case LDC_MODE_RAW: 362258283286Sha137994 /* 362358283286Sha137994 * In raw mode, there are no ctrl packets, so checking 362458283286Sha137994 * if the queue is non-empty is sufficient. 362558283286Sha137994 */ 362658283286Sha137994 *hasdata = (rx_head != rx_tail); 362758283286Sha137994 break; 362858283286Sha137994 362958283286Sha137994 case LDC_MODE_UNRELIABLE: 363058283286Sha137994 /* 363158283286Sha137994 * In unreliable mode, if the queue is non-empty, we need 363258283286Sha137994 * to check if it actually contains unread data packets. 363358283286Sha137994 * The queue may just contain ctrl packets. 363458283286Sha137994 */ 3635a74caf9bSha137994 if (rx_head != rx_tail) { 363658283286Sha137994 *hasdata = (i_ldc_chkq(ldcp) == 0); 3637a74caf9bSha137994 /* 3638a74caf9bSha137994 * If no data packets were found on the queue, 3639a74caf9bSha137994 * all packets must have been control packets 3640a74caf9bSha137994 * which will now have been processed, leaving 3641a74caf9bSha137994 * the queue empty. If the interrupt state 3642a74caf9bSha137994 * is pending, we need to clear the interrupt 3643a74caf9bSha137994 * here. 3644a74caf9bSha137994 */ 3645a74caf9bSha137994 if (*hasdata == B_FALSE && 3646a74caf9bSha137994 ldcp->rx_intr_state == LDC_INTR_PEND) { 3647a74caf9bSha137994 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 3648a74caf9bSha137994 } 3649a74caf9bSha137994 } 365058283286Sha137994 break; 365158283286Sha137994 365220ae46ebSha137994 case LDC_MODE_RELIABLE: 365358283286Sha137994 /* 365420ae46ebSha137994 * In reliable mode, first check for 'stream_remains' > 0. 365558283286Sha137994 * Otherwise, if the data queue head and tail pointers 365658283286Sha137994 * differ, there must be data to read. 365758283286Sha137994 */ 365858283286Sha137994 if (ldcp->stream_remains > 0) 3659e1ebb9ecSlm66018 *hasdata = B_TRUE; 366058283286Sha137994 else 366158283286Sha137994 *hasdata = (ldcp->rx_dq_head != ldcp->rx_dq_tail); 366258283286Sha137994 break; 366358283286Sha137994 366458283286Sha137994 default: 366558283286Sha137994 cmn_err(CE_WARN, "ldc_chkq: (0x%lx) unexpected channel mode " 366658283286Sha137994 "(0x%x)", ldcp->id, ldcp->mode); 366758283286Sha137994 mutex_exit(&ldcp->lock); 366858283286Sha137994 return (EIO); 36691ae08745Sheppo } 36701ae08745Sheppo 36711ae08745Sheppo mutex_exit(&ldcp->lock); 36721ae08745Sheppo 36731ae08745Sheppo return (0); 36741ae08745Sheppo } 36751ae08745Sheppo 36761ae08745Sheppo 36771ae08745Sheppo /* 36781ae08745Sheppo * Read 'size' amount of bytes or less. If incoming buffer 36791ae08745Sheppo * is more than 'size', ENOBUFS is returned. 36801ae08745Sheppo * 36811ae08745Sheppo * On return, size contains the number of bytes read. 36821ae08745Sheppo */ 36831ae08745Sheppo int 36841ae08745Sheppo ldc_read(ldc_handle_t handle, caddr_t bufp, size_t *sizep) 36851ae08745Sheppo { 36861ae08745Sheppo ldc_chan_t *ldcp; 36871ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 36881ae08745Sheppo int rv = 0, exit_val; 36891ae08745Sheppo 36901ae08745Sheppo if (handle == NULL) { 36911ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_read: invalid channel handle\n"); 36921ae08745Sheppo return (EINVAL); 36931ae08745Sheppo } 36941ae08745Sheppo 36951ae08745Sheppo ldcp = (ldc_chan_t *)handle; 36961ae08745Sheppo 36971ae08745Sheppo /* channel lock */ 36981ae08745Sheppo mutex_enter(&ldcp->lock); 36991ae08745Sheppo 37001ae08745Sheppo if (ldcp->tstate != TS_UP) { 37011ae08745Sheppo DWARN(ldcp->id, 37021ae08745Sheppo "ldc_read: (0x%llx) channel is not in UP state\n", 37031ae08745Sheppo ldcp->id); 37041ae08745Sheppo exit_val = ECONNRESET; 370520ae46ebSha137994 } else if (ldcp->mode == LDC_MODE_RELIABLE) { 370658283286Sha137994 TRACE_RXDQ_LENGTH(ldcp); 370758283286Sha137994 exit_val = ldcp->read_p(ldcp, bufp, sizep); 370812f80fa6Sha137994 370912f80fa6Sha137994 /* 371012f80fa6Sha137994 * For reliable mode channels, the interrupt 371112f80fa6Sha137994 * state is only set to pending during 371212f80fa6Sha137994 * interrupt handling when the secondary data 371312f80fa6Sha137994 * queue became full, leaving unprocessed 371412f80fa6Sha137994 * packets on the Rx queue. If the interrupt 371512f80fa6Sha137994 * state is pending and space is now available 371612f80fa6Sha137994 * on the data queue, clear the interrupt. 371712f80fa6Sha137994 */ 371812f80fa6Sha137994 if (ldcp->rx_intr_state == LDC_INTR_PEND && 371912f80fa6Sha137994 Q_CONTIG_SPACE(ldcp->rx_dq_head, ldcp->rx_dq_tail, 372012f80fa6Sha137994 ldcp->rx_dq_entries << LDC_PACKET_SHIFT) >= 372112f80fa6Sha137994 LDC_PACKET_SIZE) { 372212f80fa6Sha137994 /* data queue is not full */ 372312f80fa6Sha137994 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 372412f80fa6Sha137994 } 372512f80fa6Sha137994 372658283286Sha137994 mutex_exit(&ldcp->lock); 372758283286Sha137994 return (exit_val); 37281ae08745Sheppo } else { 37291ae08745Sheppo exit_val = ldcp->read_p(ldcp, bufp, sizep); 37301ae08745Sheppo } 37311ae08745Sheppo 37321ae08745Sheppo /* 37331ae08745Sheppo * if queue has been drained - clear interrupt 37341ae08745Sheppo */ 37351ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 37361ae08745Sheppo &ldcp->link_state); 3737cb112a14Slm66018 if (rv != 0) { 3738cb112a14Slm66018 cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs", 3739cb112a14Slm66018 ldcp->id); 3740cb112a14Slm66018 mutex_enter(&ldcp->tx_lock); 3741cb112a14Slm66018 i_ldc_reset(ldcp, B_TRUE); 3742cb112a14Slm66018 mutex_exit(&ldcp->tx_lock); 3743bd8f0338Snarayan mutex_exit(&ldcp->lock); 3744cb112a14Slm66018 return (ECONNRESET); 3745cb112a14Slm66018 } 37463af08d82Slm66018 37473af08d82Slm66018 if (exit_val == 0) { 37483af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 37493af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 37503af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 37513af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 37523af08d82Slm66018 exit_val = ECONNRESET; 37533af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 37543af08d82Slm66018 } 37553af08d82Slm66018 if ((rv == 0) && 37563af08d82Slm66018 (ldcp->rx_intr_state == LDC_INTR_PEND) && 37573af08d82Slm66018 (rx_head == rx_tail)) { 37581ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 37591ae08745Sheppo } 37603af08d82Slm66018 } 37611ae08745Sheppo 37621ae08745Sheppo mutex_exit(&ldcp->lock); 37631ae08745Sheppo return (exit_val); 37641ae08745Sheppo } 37651ae08745Sheppo 37661ae08745Sheppo /* 37671ae08745Sheppo * Basic raw mondo read - 37681ae08745Sheppo * no interpretation of mondo contents at all. 37691ae08745Sheppo * 37701ae08745Sheppo * Enter and exit with ldcp->lock held by caller 37711ae08745Sheppo */ 37721ae08745Sheppo static int 37731ae08745Sheppo i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 37741ae08745Sheppo { 37751ae08745Sheppo uint64_t q_size_mask; 37761ae08745Sheppo ldc_msg_t *msgp; 37771ae08745Sheppo uint8_t *msgbufp; 37781ae08745Sheppo int rv = 0, space; 37791ae08745Sheppo uint64_t rx_head, rx_tail; 37801ae08745Sheppo 37811ae08745Sheppo space = *sizep; 37821ae08745Sheppo 37831ae08745Sheppo if (space < LDC_PAYLOAD_SIZE_RAW) 37841ae08745Sheppo return (ENOBUFS); 37851ae08745Sheppo 37861ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 37871ae08745Sheppo 37881ae08745Sheppo /* compute mask for increment */ 37891ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 37901ae08745Sheppo 37911ae08745Sheppo /* 37921ae08745Sheppo * Read packet(s) from the queue 37931ae08745Sheppo */ 37941ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 37951ae08745Sheppo &ldcp->link_state); 37961ae08745Sheppo if (rv != 0) { 37971ae08745Sheppo cmn_err(CE_WARN, 37981ae08745Sheppo "ldc_read_raw: (0x%lx) unable to read queue ptrs", 37991ae08745Sheppo ldcp->id); 38001ae08745Sheppo return (EIO); 38011ae08745Sheppo } 38021ae08745Sheppo D1(ldcp->id, "ldc_read_raw: (0x%llx) rxh=0x%llx," 38031ae08745Sheppo " rxt=0x%llx, st=0x%llx\n", 38041ae08745Sheppo ldcp->id, rx_head, rx_tail, ldcp->link_state); 38051ae08745Sheppo 38061ae08745Sheppo /* reset the channel state if the channel went down */ 38073af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 38083af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 3809d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 38103af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3811d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 38121ae08745Sheppo return (ECONNRESET); 38131ae08745Sheppo } 38141ae08745Sheppo 38151ae08745Sheppo /* 38161ae08745Sheppo * Check for empty queue 38171ae08745Sheppo */ 38181ae08745Sheppo if (rx_head == rx_tail) { 38191ae08745Sheppo *sizep = 0; 38201ae08745Sheppo return (0); 38211ae08745Sheppo } 38221ae08745Sheppo 38231ae08745Sheppo /* get the message */ 38241ae08745Sheppo msgp = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 38251ae08745Sheppo 38261ae08745Sheppo /* if channel is in RAW mode, copy data and return */ 38271ae08745Sheppo msgbufp = (uint8_t *)&(msgp->raw[0]); 38281ae08745Sheppo 38291ae08745Sheppo bcopy(msgbufp, target_bufp, LDC_PAYLOAD_SIZE_RAW); 38301ae08745Sheppo 38311ae08745Sheppo DUMP_PAYLOAD(ldcp->id, msgbufp); 38321ae08745Sheppo 38331ae08745Sheppo *sizep = LDC_PAYLOAD_SIZE_RAW; 38341ae08745Sheppo 38351ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) & q_size_mask; 38360a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_head); 38371ae08745Sheppo 38381ae08745Sheppo return (rv); 38391ae08745Sheppo } 38401ae08745Sheppo 38411ae08745Sheppo /* 38421ae08745Sheppo * Process LDC mondos to build larger packets 38431ae08745Sheppo * with either un-reliable or reliable delivery. 38441ae08745Sheppo * 38451ae08745Sheppo * Enter and exit with ldcp->lock held by caller 38461ae08745Sheppo */ 38471ae08745Sheppo static int 38481ae08745Sheppo i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 38491ae08745Sheppo { 38501ae08745Sheppo int rv = 0; 38511ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 38521ae08745Sheppo uint64_t curr_head = 0; 38531ae08745Sheppo ldc_msg_t *msg; 38541ae08745Sheppo caddr_t target; 38551ae08745Sheppo size_t len = 0, bytes_read = 0; 38560a55fbb7Slm66018 int retries = 0; 385758283286Sha137994 uint64_t q_va, q_size_mask; 3858d10e4ef2Snarayan uint64_t first_fragment = 0; 38591ae08745Sheppo 38601ae08745Sheppo target = target_bufp; 38611ae08745Sheppo 38621ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 38631ae08745Sheppo 38643af08d82Slm66018 /* check if the buffer and size are valid */ 38653af08d82Slm66018 if (target_bufp == NULL || *sizep == 0) { 38663af08d82Slm66018 DWARN(ldcp->id, "ldc_read: (0x%llx) invalid buffer/size\n", 38673af08d82Slm66018 ldcp->id); 38683af08d82Slm66018 return (EINVAL); 38693af08d82Slm66018 } 38703af08d82Slm66018 387158283286Sha137994 /* Set q_va and compute increment mask for the appropriate queue */ 387220ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 387358283286Sha137994 q_va = ldcp->rx_dq_va; 387458283286Sha137994 q_size_mask = (ldcp->rx_dq_entries-1)<<LDC_PACKET_SHIFT; 387558283286Sha137994 } else { 387658283286Sha137994 q_va = ldcp->rx_q_va; 38771ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 387858283286Sha137994 } 38791ae08745Sheppo 38801ae08745Sheppo /* 38811ae08745Sheppo * Read packet(s) from the queue 38821ae08745Sheppo */ 388358283286Sha137994 rv = ldcp->readq_get_state(ldcp, &curr_head, &rx_tail, 38841ae08745Sheppo &ldcp->link_state); 38851ae08745Sheppo if (rv != 0) { 38863af08d82Slm66018 cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs", 38871ae08745Sheppo ldcp->id); 38883af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 38893af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 38903af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 38913af08d82Slm66018 return (ECONNRESET); 38921ae08745Sheppo } 38931ae08745Sheppo D1(ldcp->id, "ldc_read: (0x%llx) chd=0x%llx, tl=0x%llx, st=0x%llx\n", 38941ae08745Sheppo ldcp->id, curr_head, rx_tail, ldcp->link_state); 38951ae08745Sheppo 38961ae08745Sheppo /* reset the channel state if the channel went down */ 38973af08d82Slm66018 if (ldcp->link_state != LDC_CHANNEL_UP) 38983af08d82Slm66018 goto channel_is_reset; 38991ae08745Sheppo 39001ae08745Sheppo for (;;) { 39011ae08745Sheppo 39021ae08745Sheppo if (curr_head == rx_tail) { 390358283286Sha137994 /* 390458283286Sha137994 * If a data queue is being used, check the Rx HV 390558283286Sha137994 * queue. This will copy over any new data packets 390658283286Sha137994 * that have arrived. 390758283286Sha137994 */ 390820ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) 390958283286Sha137994 (void) i_ldc_chkq(ldcp); 391058283286Sha137994 391158283286Sha137994 rv = ldcp->readq_get_state(ldcp, 39121ae08745Sheppo &rx_head, &rx_tail, &ldcp->link_state); 39131ae08745Sheppo if (rv != 0) { 39141ae08745Sheppo cmn_err(CE_WARN, 39151ae08745Sheppo "ldc_read: (0x%lx) cannot read queue ptrs", 39161ae08745Sheppo ldcp->id); 3917d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 39183af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 3919d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 39201ae08745Sheppo return (ECONNRESET); 39211ae08745Sheppo } 392258283286Sha137994 39233af08d82Slm66018 if (ldcp->link_state != LDC_CHANNEL_UP) 39243af08d82Slm66018 goto channel_is_reset; 39251ae08745Sheppo 39261ae08745Sheppo if (curr_head == rx_tail) { 39271ae08745Sheppo 39281ae08745Sheppo /* If in the middle of a fragmented xfer */ 3929d10e4ef2Snarayan if (first_fragment != 0) { 39300a55fbb7Slm66018 39310a55fbb7Slm66018 /* wait for ldc_delay usecs */ 39320a55fbb7Slm66018 drv_usecwait(ldc_delay); 39330a55fbb7Slm66018 39340a55fbb7Slm66018 if (++retries < ldc_max_retries) 39351ae08745Sheppo continue; 39360a55fbb7Slm66018 39371ae08745Sheppo *sizep = 0; 393820ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) 393958283286Sha137994 ldcp->last_msg_rcd = 394058283286Sha137994 first_fragment - 1; 39413af08d82Slm66018 DWARN(DBG_ALL_LDCS, "ldc_read: " 394222f747efSnarayan "(0x%llx) read timeout", ldcp->id); 39433af08d82Slm66018 return (EAGAIN); 39441ae08745Sheppo } 39451ae08745Sheppo *sizep = 0; 39461ae08745Sheppo break; 39471ae08745Sheppo } 39483af08d82Slm66018 } 39490a55fbb7Slm66018 retries = 0; 39501ae08745Sheppo 39511ae08745Sheppo D2(ldcp->id, 39521ae08745Sheppo "ldc_read: (0x%llx) chd=0x%llx, rxhd=0x%llx, rxtl=0x%llx\n", 39531ae08745Sheppo ldcp->id, curr_head, rx_head, rx_tail); 39541ae08745Sheppo 39551ae08745Sheppo /* get the message */ 395658283286Sha137994 msg = (ldc_msg_t *)(q_va + curr_head); 39571ae08745Sheppo 39581ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_read received pkt", 39591ae08745Sheppo ldcp->rx_q_va + curr_head); 39601ae08745Sheppo 39611ae08745Sheppo /* Check the message ID for the message received */ 396220ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) { 39631ae08745Sheppo if ((rv = i_ldc_check_seqid(ldcp, msg)) != 0) { 39641ae08745Sheppo 396558283286Sha137994 DWARN(ldcp->id, "ldc_read: (0x%llx) seqid " 396658283286Sha137994 "error, q_ptrs=0x%lx,0x%lx", 396758283286Sha137994 ldcp->id, rx_head, rx_tail); 39681ae08745Sheppo 39690a55fbb7Slm66018 /* throw away data */ 39700a55fbb7Slm66018 bytes_read = 0; 39710a55fbb7Slm66018 39721ae08745Sheppo /* Reset last_msg_rcd to start of message */ 3973d10e4ef2Snarayan if (first_fragment != 0) { 3974d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 3975d10e4ef2Snarayan first_fragment = 0; 39761ae08745Sheppo } 39771ae08745Sheppo /* 39781ae08745Sheppo * Send a NACK -- invalid seqid 39791ae08745Sheppo * get the current tail for the response 39801ae08745Sheppo */ 39811ae08745Sheppo rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK, 39821ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 39831ae08745Sheppo if (rv) { 39841ae08745Sheppo cmn_err(CE_NOTE, 39851ae08745Sheppo "ldc_read: (0x%lx) err sending " 39861ae08745Sheppo "NACK msg\n", ldcp->id); 3987d10e4ef2Snarayan 398858283286Sha137994 /* if cannot send NACK - reset chan */ 3989d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 39903af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3991d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3992d10e4ef2Snarayan rv = ECONNRESET; 3993d10e4ef2Snarayan break; 39941ae08745Sheppo } 39951ae08745Sheppo 39961ae08745Sheppo /* purge receive queue */ 39970a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 39981ae08745Sheppo 39991ae08745Sheppo break; 40001ae08745Sheppo } 40011ae08745Sheppo 40021ae08745Sheppo /* 40031ae08745Sheppo * Process any messages of type CTRL messages 4004e1ebb9ecSlm66018 * Future implementations should try to pass these 4005e1ebb9ecSlm66018 * to LDC link by resetting the intr state. 40061ae08745Sheppo * 400758283286Sha137994 * NOTE: not done as a switch() as type can be 400858283286Sha137994 * both ctrl+data 40091ae08745Sheppo */ 40101ae08745Sheppo if (msg->type & LDC_CTRL) { 40111ae08745Sheppo if (rv = i_ldc_ctrlmsg(ldcp, msg)) { 40121ae08745Sheppo if (rv == EAGAIN) 40131ae08745Sheppo continue; 40140a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 40151ae08745Sheppo *sizep = 0; 40161ae08745Sheppo bytes_read = 0; 40171ae08745Sheppo break; 40181ae08745Sheppo } 40191ae08745Sheppo } 40201ae08745Sheppo 40211ae08745Sheppo /* process data ACKs */ 40221ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 4023d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 4024d10e4ef2Snarayan *sizep = 0; 4025d10e4ef2Snarayan bytes_read = 0; 4026d10e4ef2Snarayan break; 4027d10e4ef2Snarayan } 40281ae08745Sheppo } 40291ae08745Sheppo 403083d3bc6fSnarayan /* process data NACKs */ 403183d3bc6fSnarayan if ((msg->type & LDC_DATA) && (msg->stype & LDC_NACK)) { 403283d3bc6fSnarayan DWARN(ldcp->id, 403358283286Sha137994 "ldc_read: (0x%llx) received DATA/NACK", 403458283286Sha137994 ldcp->id); 403583d3bc6fSnarayan mutex_enter(&ldcp->tx_lock); 403683d3bc6fSnarayan i_ldc_reset(ldcp, B_TRUE); 403783d3bc6fSnarayan mutex_exit(&ldcp->tx_lock); 403883d3bc6fSnarayan return (ECONNRESET); 403983d3bc6fSnarayan } 404058283286Sha137994 } 404183d3bc6fSnarayan 40421ae08745Sheppo /* process data messages */ 40431ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 40441ae08745Sheppo 40451ae08745Sheppo uint8_t *msgbuf = (uint8_t *)( 404620ae46ebSha137994 (ldcp->mode == LDC_MODE_RELIABLE) ? 404722f747efSnarayan msg->rdata : msg->udata); 40481ae08745Sheppo 40491ae08745Sheppo D2(ldcp->id, 40501ae08745Sheppo "ldc_read: (0x%llx) received data msg\n", ldcp->id); 40511ae08745Sheppo 40521ae08745Sheppo /* get the packet length */ 40531ae08745Sheppo len = (msg->env & LDC_LEN_MASK); 40541ae08745Sheppo 40551ae08745Sheppo /* 40561ae08745Sheppo * FUTURE OPTIMIZATION: 40571ae08745Sheppo * dont need to set q head for every 40581ae08745Sheppo * packet we read just need to do this when 40591ae08745Sheppo * we are done or need to wait for more 40601ae08745Sheppo * mondos to make a full packet - this is 40611ae08745Sheppo * currently expensive. 40621ae08745Sheppo */ 40631ae08745Sheppo 4064d10e4ef2Snarayan if (first_fragment == 0) { 40651ae08745Sheppo 40661ae08745Sheppo /* 40671ae08745Sheppo * first packets should always have the start 40681ae08745Sheppo * bit set (even for a single packet). If not 40691ae08745Sheppo * throw away the packet 40701ae08745Sheppo */ 40711ae08745Sheppo if (!(msg->env & LDC_FRAG_START)) { 40721ae08745Sheppo 40731ae08745Sheppo DWARN(DBG_ALL_LDCS, 40741ae08745Sheppo "ldc_read: (0x%llx) not start - " 40751ae08745Sheppo "frag=%x\n", ldcp->id, 40761ae08745Sheppo (msg->env) & LDC_FRAG_MASK); 40771ae08745Sheppo 40781ae08745Sheppo /* toss pkt, inc head, cont reading */ 40791ae08745Sheppo bytes_read = 0; 40801ae08745Sheppo target = target_bufp; 40811ae08745Sheppo curr_head = 40821ae08745Sheppo (curr_head + LDC_PACKET_SIZE) 40831ae08745Sheppo & q_size_mask; 408458283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, 40851ae08745Sheppo curr_head)) 40861ae08745Sheppo break; 40871ae08745Sheppo 40881ae08745Sheppo continue; 40891ae08745Sheppo } 40901ae08745Sheppo 4091d10e4ef2Snarayan first_fragment = msg->seqid; 40921ae08745Sheppo } else { 40931ae08745Sheppo /* check to see if this is a pkt w/ START bit */ 40941ae08745Sheppo if (msg->env & LDC_FRAG_START) { 40951ae08745Sheppo DWARN(DBG_ALL_LDCS, 40961ae08745Sheppo "ldc_read:(0x%llx) unexpected pkt" 40971ae08745Sheppo " env=0x%x discarding %d bytes," 40981ae08745Sheppo " lastmsg=%d, currentmsg=%d\n", 40991ae08745Sheppo ldcp->id, msg->env&LDC_FRAG_MASK, 41001ae08745Sheppo bytes_read, ldcp->last_msg_rcd, 41011ae08745Sheppo msg->seqid); 41021ae08745Sheppo 41031ae08745Sheppo /* throw data we have read so far */ 41041ae08745Sheppo bytes_read = 0; 41051ae08745Sheppo target = target_bufp; 4106d10e4ef2Snarayan first_fragment = msg->seqid; 41071ae08745Sheppo 410858283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, 41091ae08745Sheppo curr_head)) 41101ae08745Sheppo break; 41111ae08745Sheppo } 41121ae08745Sheppo } 41131ae08745Sheppo 41141ae08745Sheppo /* copy (next) pkt into buffer */ 41151ae08745Sheppo if (len <= (*sizep - bytes_read)) { 41161ae08745Sheppo bcopy(msgbuf, target, len); 41171ae08745Sheppo target += len; 41181ae08745Sheppo bytes_read += len; 41191ae08745Sheppo } else { 41201ae08745Sheppo /* 41211ae08745Sheppo * there is not enough space in the buffer to 41221ae08745Sheppo * read this pkt. throw message away & continue 41231ae08745Sheppo * reading data from queue 41241ae08745Sheppo */ 41251ae08745Sheppo DWARN(DBG_ALL_LDCS, 41261ae08745Sheppo "ldc_read: (0x%llx) buffer too small, " 41271ae08745Sheppo "head=0x%lx, expect=%d, got=%d\n", ldcp->id, 41281ae08745Sheppo curr_head, *sizep, bytes_read+len); 41291ae08745Sheppo 4130d10e4ef2Snarayan first_fragment = 0; 41311ae08745Sheppo target = target_bufp; 41321ae08745Sheppo bytes_read = 0; 41331ae08745Sheppo 41341ae08745Sheppo /* throw away everything received so far */ 413558283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, curr_head)) 41361ae08745Sheppo break; 41371ae08745Sheppo 41381ae08745Sheppo /* continue reading remaining pkts */ 41391ae08745Sheppo continue; 41401ae08745Sheppo } 41411ae08745Sheppo } 41421ae08745Sheppo 41431ae08745Sheppo /* set the message id */ 414420ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) 41451ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 41461ae08745Sheppo 41471ae08745Sheppo /* move the head one position */ 41481ae08745Sheppo curr_head = (curr_head + LDC_PACKET_SIZE) & q_size_mask; 41491ae08745Sheppo 41501ae08745Sheppo if (msg->env & LDC_FRAG_STOP) { 41511ae08745Sheppo 41521ae08745Sheppo /* 41531ae08745Sheppo * All pkts that are part of this fragmented transfer 41541ae08745Sheppo * have been read or this was a single pkt read 41551ae08745Sheppo * or there was an error 41561ae08745Sheppo */ 41571ae08745Sheppo 41581ae08745Sheppo /* set the queue head */ 415958283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, curr_head)) 41601ae08745Sheppo bytes_read = 0; 41611ae08745Sheppo 41621ae08745Sheppo *sizep = bytes_read; 41631ae08745Sheppo 41641ae08745Sheppo break; 41651ae08745Sheppo } 41661ae08745Sheppo 4167332608acSnarayan /* advance head if it is a CTRL packet or a DATA ACK packet */ 4168332608acSnarayan if ((msg->type & LDC_CTRL) || 4169332608acSnarayan ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK))) { 41701ae08745Sheppo 41711ae08745Sheppo /* set the queue head */ 417258283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, curr_head)) { 41731ae08745Sheppo bytes_read = 0; 41741ae08745Sheppo break; 41751ae08745Sheppo } 41761ae08745Sheppo 41771ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) set ACK qhead 0x%llx", 41781ae08745Sheppo ldcp->id, curr_head); 41791ae08745Sheppo } 41801ae08745Sheppo 41811ae08745Sheppo } /* for (;;) */ 41821ae08745Sheppo 41831ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) end size=%d", ldcp->id, *sizep); 41841ae08745Sheppo 41851ae08745Sheppo return (rv); 41863af08d82Slm66018 41873af08d82Slm66018 channel_is_reset: 41883af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 41893af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 41903af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 41913af08d82Slm66018 return (ECONNRESET); 41921ae08745Sheppo } 41931ae08745Sheppo 41941ae08745Sheppo /* 419520ae46ebSha137994 * Fetch and buffer incoming packets so we can hand them back as 41961ae08745Sheppo * a basic byte stream. 41971ae08745Sheppo * 41981ae08745Sheppo * Enter and exit with ldcp->lock held by caller 41991ae08745Sheppo */ 42001ae08745Sheppo static int 42011ae08745Sheppo i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 42021ae08745Sheppo { 42031ae08745Sheppo int rv; 42041ae08745Sheppo size_t size; 42051ae08745Sheppo 42061ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 42071ae08745Sheppo 42081ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) buffer size=%d", 42091ae08745Sheppo ldcp->id, *sizep); 42101ae08745Sheppo 42111ae08745Sheppo if (ldcp->stream_remains == 0) { 42121ae08745Sheppo size = ldcp->mtu; 42131ae08745Sheppo rv = i_ldc_read_packet(ldcp, 42141ae08745Sheppo (caddr_t)ldcp->stream_bufferp, &size); 42151ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: read packet (0x%llx) size=%d", 42161ae08745Sheppo ldcp->id, size); 42171ae08745Sheppo 42181ae08745Sheppo if (rv != 0) 42191ae08745Sheppo return (rv); 42201ae08745Sheppo 42211ae08745Sheppo ldcp->stream_remains = size; 42221ae08745Sheppo ldcp->stream_offset = 0; 42231ae08745Sheppo } 42241ae08745Sheppo 42251ae08745Sheppo size = MIN(ldcp->stream_remains, *sizep); 42261ae08745Sheppo 42271ae08745Sheppo bcopy(ldcp->stream_bufferp + ldcp->stream_offset, target_bufp, size); 42281ae08745Sheppo ldcp->stream_offset += size; 42291ae08745Sheppo ldcp->stream_remains -= size; 42301ae08745Sheppo 42311ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) fill from buffer size=%d", 42321ae08745Sheppo ldcp->id, size); 42331ae08745Sheppo 42341ae08745Sheppo *sizep = size; 42351ae08745Sheppo return (0); 42361ae08745Sheppo } 42371ae08745Sheppo 42381ae08745Sheppo /* 42391ae08745Sheppo * Write specified amount of bytes to the channel 42401ae08745Sheppo * in multiple pkts of pkt_payload size. Each 42411ae08745Sheppo * packet is tagged with an unique packet ID in 4242e1ebb9ecSlm66018 * the case of a reliable link. 42431ae08745Sheppo * 42441ae08745Sheppo * On return, size contains the number of bytes written. 42451ae08745Sheppo */ 42461ae08745Sheppo int 42471ae08745Sheppo ldc_write(ldc_handle_t handle, caddr_t buf, size_t *sizep) 42481ae08745Sheppo { 42491ae08745Sheppo ldc_chan_t *ldcp; 42501ae08745Sheppo int rv = 0; 42511ae08745Sheppo 42521ae08745Sheppo if (handle == NULL) { 42531ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_write: invalid channel handle\n"); 42541ae08745Sheppo return (EINVAL); 42551ae08745Sheppo } 42561ae08745Sheppo ldcp = (ldc_chan_t *)handle; 42571ae08745Sheppo 4258d10e4ef2Snarayan /* check if writes can occur */ 4259d10e4ef2Snarayan if (!mutex_tryenter(&ldcp->tx_lock)) { 4260d10e4ef2Snarayan /* 4261d10e4ef2Snarayan * Could not get the lock - channel could 4262d10e4ef2Snarayan * be in the process of being unconfigured 4263d10e4ef2Snarayan * or reader has encountered an error 4264d10e4ef2Snarayan */ 4265d10e4ef2Snarayan return (EAGAIN); 4266d10e4ef2Snarayan } 42671ae08745Sheppo 42681ae08745Sheppo /* check if non-zero data to write */ 42691ae08745Sheppo if (buf == NULL || sizep == NULL) { 42701ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) invalid data write\n", 42711ae08745Sheppo ldcp->id); 4272d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 42731ae08745Sheppo return (EINVAL); 42741ae08745Sheppo } 42751ae08745Sheppo 42761ae08745Sheppo if (*sizep == 0) { 42771ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write size of zero\n", 42781ae08745Sheppo ldcp->id); 4279d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 42801ae08745Sheppo return (0); 42811ae08745Sheppo } 42821ae08745Sheppo 42831ae08745Sheppo /* Check if channel is UP for data exchange */ 42841ae08745Sheppo if (ldcp->tstate != TS_UP) { 42851ae08745Sheppo DWARN(ldcp->id, 42861ae08745Sheppo "ldc_write: (0x%llx) channel is not in UP state\n", 42871ae08745Sheppo ldcp->id); 42881ae08745Sheppo *sizep = 0; 42891ae08745Sheppo rv = ECONNRESET; 42901ae08745Sheppo } else { 42911ae08745Sheppo rv = ldcp->write_p(ldcp, buf, sizep); 42921ae08745Sheppo } 42931ae08745Sheppo 4294d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 42951ae08745Sheppo 42961ae08745Sheppo return (rv); 42971ae08745Sheppo } 42981ae08745Sheppo 42991ae08745Sheppo /* 43001ae08745Sheppo * Write a raw packet to the channel 43011ae08745Sheppo * On return, size contains the number of bytes written. 43021ae08745Sheppo */ 43031ae08745Sheppo static int 43041ae08745Sheppo i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 43051ae08745Sheppo { 43061ae08745Sheppo ldc_msg_t *ldcmsg; 43071ae08745Sheppo uint64_t tx_head, tx_tail, new_tail; 43081ae08745Sheppo int rv = 0; 43091ae08745Sheppo size_t size; 43101ae08745Sheppo 4311d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 43121ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RAW); 43131ae08745Sheppo 43141ae08745Sheppo size = *sizep; 43151ae08745Sheppo 43161ae08745Sheppo /* 43171ae08745Sheppo * Check to see if the packet size is less than or 43181ae08745Sheppo * equal to packet size support in raw mode 43191ae08745Sheppo */ 43201ae08745Sheppo if (size > ldcp->pkt_payload) { 43211ae08745Sheppo DWARN(ldcp->id, 43221ae08745Sheppo "ldc_write: (0x%llx) invalid size (0x%llx) for RAW mode\n", 43231ae08745Sheppo ldcp->id, *sizep); 43241ae08745Sheppo *sizep = 0; 43251ae08745Sheppo return (EMSGSIZE); 43261ae08745Sheppo } 43271ae08745Sheppo 43281ae08745Sheppo /* get the qptrs for the tx queue */ 43291ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 43301ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 43311ae08745Sheppo if (rv != 0) { 43321ae08745Sheppo cmn_err(CE_WARN, 43331ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 43341ae08745Sheppo *sizep = 0; 43351ae08745Sheppo return (EIO); 43361ae08745Sheppo } 43371ae08745Sheppo 43381ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 43391ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 43401ae08745Sheppo DWARN(ldcp->id, 43411ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 4342d10e4ef2Snarayan 43431ae08745Sheppo *sizep = 0; 4344d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 43453af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4346d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4347d10e4ef2Snarayan } else { 4348d10e4ef2Snarayan /* 4349d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4350d10e4ef2Snarayan * and Tx lock in correct order 4351d10e4ef2Snarayan */ 4352d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4353d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4354d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 43553af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4356d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4357d10e4ef2Snarayan } 43581ae08745Sheppo return (ECONNRESET); 43591ae08745Sheppo } 43601ae08745Sheppo 43611ae08745Sheppo tx_tail = ldcp->tx_tail; 43621ae08745Sheppo tx_head = ldcp->tx_head; 43631ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) & 43641ae08745Sheppo ((ldcp->tx_q_entries-1) << LDC_PACKET_SHIFT); 43651ae08745Sheppo 43661ae08745Sheppo if (new_tail == tx_head) { 43671ae08745Sheppo DWARN(DBG_ALL_LDCS, 43681ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 43691ae08745Sheppo *sizep = 0; 43701ae08745Sheppo return (EWOULDBLOCK); 43711ae08745Sheppo } 43721ae08745Sheppo 43731ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 43741ae08745Sheppo ldcp->id, size); 43751ae08745Sheppo 43761ae08745Sheppo /* Send the data now */ 43771ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 43781ae08745Sheppo 43791ae08745Sheppo /* copy the data into pkt */ 43801ae08745Sheppo bcopy((uint8_t *)buf, ldcmsg, size); 43811ae08745Sheppo 43821ae08745Sheppo /* increment tail */ 43831ae08745Sheppo tx_tail = new_tail; 43841ae08745Sheppo 43851ae08745Sheppo /* 43861ae08745Sheppo * All packets have been copied into the TX queue 43871ae08745Sheppo * update the tail ptr in the HV 43881ae08745Sheppo */ 43891ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 43901ae08745Sheppo if (rv) { 43911ae08745Sheppo if (rv == EWOULDBLOCK) { 43921ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write timed out\n", 43931ae08745Sheppo ldcp->id); 43941ae08745Sheppo *sizep = 0; 43951ae08745Sheppo return (EWOULDBLOCK); 43961ae08745Sheppo } 43971ae08745Sheppo 43981ae08745Sheppo *sizep = 0; 4399d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 44003af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4401d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4402d10e4ef2Snarayan } else { 4403d10e4ef2Snarayan /* 4404d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4405d10e4ef2Snarayan * and Tx lock in correct order 4406d10e4ef2Snarayan */ 4407d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4408d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4409d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 44103af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4411d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4412d10e4ef2Snarayan } 44131ae08745Sheppo return (ECONNRESET); 44141ae08745Sheppo } 44151ae08745Sheppo 44161ae08745Sheppo ldcp->tx_tail = tx_tail; 44171ae08745Sheppo *sizep = size; 44181ae08745Sheppo 44191ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, size); 44201ae08745Sheppo 44211ae08745Sheppo return (rv); 44221ae08745Sheppo } 44231ae08745Sheppo 44241ae08745Sheppo 44251ae08745Sheppo /* 44261ae08745Sheppo * Write specified amount of bytes to the channel 44271ae08745Sheppo * in multiple pkts of pkt_payload size. Each 44281ae08745Sheppo * packet is tagged with an unique packet ID in 4429e1ebb9ecSlm66018 * the case of a reliable link. 44301ae08745Sheppo * 44311ae08745Sheppo * On return, size contains the number of bytes written. 44321ae08745Sheppo * This function needs to ensure that the write size is < MTU size 44331ae08745Sheppo */ 44341ae08745Sheppo static int 44351ae08745Sheppo i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t buf, size_t *size) 44361ae08745Sheppo { 44371ae08745Sheppo ldc_msg_t *ldcmsg; 44381ae08745Sheppo uint64_t tx_head, tx_tail, new_tail, start; 44391ae08745Sheppo uint64_t txq_size_mask, numavail; 44401ae08745Sheppo uint8_t *msgbuf, *source = (uint8_t *)buf; 44411ae08745Sheppo size_t len, bytes_written = 0, remaining; 44421ae08745Sheppo int rv; 44431ae08745Sheppo uint32_t curr_seqid; 44441ae08745Sheppo 4445d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 44461ae08745Sheppo 44471ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RELIABLE || 444820ae46ebSha137994 ldcp->mode == LDC_MODE_UNRELIABLE); 44491ae08745Sheppo 44501ae08745Sheppo /* compute mask for increment */ 44511ae08745Sheppo txq_size_mask = (ldcp->tx_q_entries - 1) << LDC_PACKET_SHIFT; 44521ae08745Sheppo 44531ae08745Sheppo /* get the qptrs for the tx queue */ 44541ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 44551ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 44561ae08745Sheppo if (rv != 0) { 44571ae08745Sheppo cmn_err(CE_WARN, 44581ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 44591ae08745Sheppo *size = 0; 44601ae08745Sheppo return (EIO); 44611ae08745Sheppo } 44621ae08745Sheppo 44631ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 44641ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 44651ae08745Sheppo DWARN(ldcp->id, 44661ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 44671ae08745Sheppo *size = 0; 4468d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 44693af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4470d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4471d10e4ef2Snarayan } else { 4472d10e4ef2Snarayan /* 4473d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4474d10e4ef2Snarayan * and Tx lock in correct order 4475d10e4ef2Snarayan */ 4476d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4477d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4478d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 44793af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4480d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4481d10e4ef2Snarayan } 44821ae08745Sheppo return (ECONNRESET); 44831ae08745Sheppo } 44841ae08745Sheppo 44851ae08745Sheppo tx_tail = ldcp->tx_tail; 44861ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) % 44871ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 44881ae08745Sheppo 44891ae08745Sheppo /* 449022f747efSnarayan * Check to see if the queue is full. The check is done using 449122f747efSnarayan * the appropriate head based on the link mode. 44921ae08745Sheppo */ 449322f747efSnarayan i_ldc_get_tx_head(ldcp, &tx_head); 449422f747efSnarayan 44951ae08745Sheppo if (new_tail == tx_head) { 44961ae08745Sheppo DWARN(DBG_ALL_LDCS, 44971ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 44981ae08745Sheppo *size = 0; 44991ae08745Sheppo return (EWOULDBLOCK); 45001ae08745Sheppo } 45011ae08745Sheppo 45021ae08745Sheppo /* 45031ae08745Sheppo * Make sure that the LDC Tx queue has enough space 45041ae08745Sheppo */ 45051ae08745Sheppo numavail = (tx_head >> LDC_PACKET_SHIFT) - (tx_tail >> LDC_PACKET_SHIFT) 45061ae08745Sheppo + ldcp->tx_q_entries - 1; 45071ae08745Sheppo numavail %= ldcp->tx_q_entries; 45081ae08745Sheppo 45091ae08745Sheppo if (*size > (numavail * ldcp->pkt_payload)) { 45101ae08745Sheppo DWARN(DBG_ALL_LDCS, 45111ae08745Sheppo "ldc_write: (0x%llx) TX queue has no space\n", ldcp->id); 45121ae08745Sheppo return (EWOULDBLOCK); 45131ae08745Sheppo } 45141ae08745Sheppo 45151ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 45161ae08745Sheppo ldcp->id, *size); 45171ae08745Sheppo 45181ae08745Sheppo /* Send the data now */ 45191ae08745Sheppo bytes_written = 0; 45201ae08745Sheppo curr_seqid = ldcp->last_msg_snt; 45211ae08745Sheppo start = tx_tail; 45221ae08745Sheppo 45231ae08745Sheppo while (*size > bytes_written) { 45241ae08745Sheppo 45251ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 45261ae08745Sheppo 452720ae46ebSha137994 msgbuf = (uint8_t *)((ldcp->mode == LDC_MODE_RELIABLE) ? 452822f747efSnarayan ldcmsg->rdata : ldcmsg->udata); 45291ae08745Sheppo 45301ae08745Sheppo ldcmsg->type = LDC_DATA; 45311ae08745Sheppo ldcmsg->stype = LDC_INFO; 45321ae08745Sheppo ldcmsg->ctrl = 0; 45331ae08745Sheppo 45341ae08745Sheppo remaining = *size - bytes_written; 45351ae08745Sheppo len = min(ldcp->pkt_payload, remaining); 45361ae08745Sheppo ldcmsg->env = (uint8_t)len; 45371ae08745Sheppo 45381ae08745Sheppo curr_seqid++; 45391ae08745Sheppo ldcmsg->seqid = curr_seqid; 45401ae08745Sheppo 45411ae08745Sheppo /* copy the data into pkt */ 45421ae08745Sheppo bcopy(source, msgbuf, len); 45431ae08745Sheppo 45441ae08745Sheppo source += len; 45451ae08745Sheppo bytes_written += len; 45461ae08745Sheppo 45471ae08745Sheppo /* increment tail */ 45481ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) & txq_size_mask; 45491ae08745Sheppo 45501ae08745Sheppo ASSERT(tx_tail != tx_head); 45511ae08745Sheppo } 45521ae08745Sheppo 45531ae08745Sheppo /* Set the start and stop bits */ 45541ae08745Sheppo ldcmsg->env |= LDC_FRAG_STOP; 45551ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + start); 45561ae08745Sheppo ldcmsg->env |= LDC_FRAG_START; 45571ae08745Sheppo 45581ae08745Sheppo /* 45591ae08745Sheppo * All packets have been copied into the TX queue 45601ae08745Sheppo * update the tail ptr in the HV 45611ae08745Sheppo */ 45621ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 45631ae08745Sheppo if (rv == 0) { 45641ae08745Sheppo ldcp->tx_tail = tx_tail; 45651ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 45661ae08745Sheppo *size = bytes_written; 45671ae08745Sheppo } else { 45681ae08745Sheppo int rv2; 45691ae08745Sheppo 45701ae08745Sheppo if (rv != EWOULDBLOCK) { 45711ae08745Sheppo *size = 0; 4572d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 45733af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4574d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4575d10e4ef2Snarayan } else { 4576d10e4ef2Snarayan /* 4577d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4578d10e4ef2Snarayan * and Tx lock in correct order 4579d10e4ef2Snarayan */ 4580d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4581d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4582d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 45833af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4584d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4585d10e4ef2Snarayan } 45861ae08745Sheppo return (ECONNRESET); 45871ae08745Sheppo } 45881ae08745Sheppo 4589cb112a14Slm66018 D1(ldcp->id, "hv_tx_set_tail returns 0x%x (head 0x%x, " 45901ae08745Sheppo "old tail 0x%x, new tail 0x%x, qsize=0x%x)\n", 45911ae08745Sheppo rv, ldcp->tx_head, ldcp->tx_tail, tx_tail, 45921ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 45931ae08745Sheppo 45941ae08745Sheppo rv2 = hv_ldc_tx_get_state(ldcp->id, 45951ae08745Sheppo &tx_head, &tx_tail, &ldcp->link_state); 45961ae08745Sheppo 4597cb112a14Slm66018 D1(ldcp->id, "hv_ldc_tx_get_state returns 0x%x " 45981ae08745Sheppo "(head 0x%x, tail 0x%x state 0x%x)\n", 45991ae08745Sheppo rv2, tx_head, tx_tail, ldcp->link_state); 46001ae08745Sheppo 46011ae08745Sheppo *size = 0; 46021ae08745Sheppo } 46031ae08745Sheppo 46041ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, *size); 46051ae08745Sheppo 46061ae08745Sheppo return (rv); 46071ae08745Sheppo } 46081ae08745Sheppo 46091ae08745Sheppo /* 46101ae08745Sheppo * Write specified amount of bytes to the channel 46111ae08745Sheppo * in multiple pkts of pkt_payload size. Each 46121ae08745Sheppo * packet is tagged with an unique packet ID in 4613e1ebb9ecSlm66018 * the case of a reliable link. 46141ae08745Sheppo * 46151ae08745Sheppo * On return, size contains the number of bytes written. 46161ae08745Sheppo * This function needs to ensure that the write size is < MTU size 46171ae08745Sheppo */ 46181ae08745Sheppo static int 46191ae08745Sheppo i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 46201ae08745Sheppo { 4621d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 462220ae46ebSha137994 ASSERT(ldcp->mode == LDC_MODE_RELIABLE); 46231ae08745Sheppo 46241ae08745Sheppo /* Truncate packet to max of MTU size */ 46251ae08745Sheppo if (*sizep > ldcp->mtu) *sizep = ldcp->mtu; 46261ae08745Sheppo return (i_ldc_write_packet(ldcp, buf, sizep)); 46271ae08745Sheppo } 46281ae08745Sheppo 46291ae08745Sheppo 46301ae08745Sheppo /* 46311ae08745Sheppo * Interfaces for channel nexus to register/unregister with LDC module 46321ae08745Sheppo * The nexus will register functions to be used to register individual 46331ae08745Sheppo * channels with the nexus and enable interrupts for the channels 46341ae08745Sheppo */ 46351ae08745Sheppo int 46361ae08745Sheppo ldc_register(ldc_cnex_t *cinfo) 46371ae08745Sheppo { 46381ae08745Sheppo ldc_chan_t *ldcp; 46391ae08745Sheppo 46401ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL || 46411ae08745Sheppo cinfo->reg_chan == NULL || cinfo->unreg_chan == NULL || 46421ae08745Sheppo cinfo->add_intr == NULL || cinfo->rem_intr == NULL || 46431ae08745Sheppo cinfo->clr_intr == NULL) { 46441ae08745Sheppo 46451ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_register: invalid nexus info\n"); 46461ae08745Sheppo return (EINVAL); 46471ae08745Sheppo } 46481ae08745Sheppo 46491ae08745Sheppo mutex_enter(&ldcssp->lock); 46501ae08745Sheppo 46511ae08745Sheppo /* nexus registration */ 46521ae08745Sheppo ldcssp->cinfo.dip = cinfo->dip; 46531ae08745Sheppo ldcssp->cinfo.reg_chan = cinfo->reg_chan; 46541ae08745Sheppo ldcssp->cinfo.unreg_chan = cinfo->unreg_chan; 46551ae08745Sheppo ldcssp->cinfo.add_intr = cinfo->add_intr; 46561ae08745Sheppo ldcssp->cinfo.rem_intr = cinfo->rem_intr; 46571ae08745Sheppo ldcssp->cinfo.clr_intr = cinfo->clr_intr; 46581ae08745Sheppo 46591ae08745Sheppo /* register any channels that might have been previously initialized */ 46601ae08745Sheppo ldcp = ldcssp->chan_list; 46611ae08745Sheppo while (ldcp) { 46621ae08745Sheppo if ((ldcp->tstate & TS_QCONF_RDY) && 46631ae08745Sheppo (ldcp->tstate & TS_CNEX_RDY) == 0) 46641ae08745Sheppo (void) i_ldc_register_channel(ldcp); 46651ae08745Sheppo 46661ae08745Sheppo ldcp = ldcp->next; 46671ae08745Sheppo } 46681ae08745Sheppo 46691ae08745Sheppo mutex_exit(&ldcssp->lock); 46701ae08745Sheppo 46711ae08745Sheppo return (0); 46721ae08745Sheppo } 46731ae08745Sheppo 46741ae08745Sheppo int 46751ae08745Sheppo ldc_unregister(ldc_cnex_t *cinfo) 46761ae08745Sheppo { 46771ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL) { 46781ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid nexus info\n"); 46791ae08745Sheppo return (EINVAL); 46801ae08745Sheppo } 46811ae08745Sheppo 46821ae08745Sheppo mutex_enter(&ldcssp->lock); 46831ae08745Sheppo 46841ae08745Sheppo if (cinfo->dip != ldcssp->cinfo.dip) { 46851ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid dip\n"); 46861ae08745Sheppo mutex_exit(&ldcssp->lock); 46871ae08745Sheppo return (EINVAL); 46881ae08745Sheppo } 46891ae08745Sheppo 46901ae08745Sheppo /* nexus unregister */ 46911ae08745Sheppo ldcssp->cinfo.dip = NULL; 46921ae08745Sheppo ldcssp->cinfo.reg_chan = NULL; 46931ae08745Sheppo ldcssp->cinfo.unreg_chan = NULL; 46941ae08745Sheppo ldcssp->cinfo.add_intr = NULL; 46951ae08745Sheppo ldcssp->cinfo.rem_intr = NULL; 46961ae08745Sheppo ldcssp->cinfo.clr_intr = NULL; 46971ae08745Sheppo 46981ae08745Sheppo mutex_exit(&ldcssp->lock); 46991ae08745Sheppo 47001ae08745Sheppo return (0); 47011ae08745Sheppo } 4702