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 /* 235699897cSHaik 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> 685699897cSHaik 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); 785699897cSHaik 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 /* 1935699897cSHaik Aftandilian * Channels which have a devclass satisfying the following 1945699897cSHaik Aftandilian * will be reset when entering the prom or kmdb. 1955699897cSHaik Aftandilian * 1965699897cSHaik Aftandilian * LDC_DEVCLASS_PROM_RESET(devclass) != 0 1975699897cSHaik Aftandilian * 1985699897cSHaik Aftandilian * By default, only block device service channels are reset. 1995699897cSHaik Aftandilian */ 2005699897cSHaik Aftandilian #define LDC_DEVCLASS_BIT(dc) (0x1 << (dc)) 2015699897cSHaik Aftandilian #define LDC_DEVCLASS_PROM_RESET(dc) \ 2025699897cSHaik Aftandilian (LDC_DEVCLASS_BIT(dc) & ldc_debug_reset_mask) 2035699897cSHaik Aftandilian static uint64_t ldc_debug_reset_mask = LDC_DEVCLASS_BIT(LDC_DEV_BLK_SVC); 2045699897cSHaik Aftandilian 2055699897cSHaik 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 4295699897cSHaik Aftandilian /* Register debug_enter callback */ 4305699897cSHaik Aftandilian kldc_set_debug_cb(&i_ldc_debug_enter); 4315699897cSHaik 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 4595699897cSHaik Aftandilian /* Unregister debug_enter callback */ 4605699897cSHaik Aftandilian kldc_set_debug_cb(NULL); 4615699897cSHaik 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 647*32225099SZach Kissel /* If the queue is already empty just return success. */ 648*32225099SZach Kissel if (rx_head == rx_tail) 649*32225099SZach Kissel return (0); 650*32225099SZach Kissel 651a8ea4edeSnarayan /* flush contents by setting the head = tail */ 652a8ea4edeSnarayan return (i_ldc_set_rx_head(ldcp, rx_tail)); 653a8ea4edeSnarayan } 654a8ea4edeSnarayan 655a8ea4edeSnarayan 6561ae08745Sheppo /* 6571ae08745Sheppo * Reset LDC state structure and its contents 6581ae08745Sheppo */ 6591ae08745Sheppo static void 6601ae08745Sheppo i_ldc_reset_state(ldc_chan_t *ldcp) 6611ae08745Sheppo { 6621ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 6631ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 6641ae08745Sheppo ldcp->last_ack_rcd = 0; 6651ae08745Sheppo ldcp->last_msg_rcd = 0; 6661ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 66758283286Sha137994 ldcp->stream_remains = 0; 6681ae08745Sheppo ldcp->next_vidx = 0; 6691ae08745Sheppo ldcp->hstate = 0; 6701ae08745Sheppo ldcp->tstate = TS_OPEN; 6711ae08745Sheppo ldcp->status = LDC_OPEN; 67258283286Sha137994 ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID; 67358283286Sha137994 ldcp->rx_dq_head = 0; 67458283286Sha137994 ldcp->rx_dq_tail = 0; 6751ae08745Sheppo 6761ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 6771ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 6781ae08745Sheppo 6791ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 6801ae08745Sheppo ldcp->status = LDC_UP; 6811ae08745Sheppo ldcp->tstate = TS_UP; 6821ae08745Sheppo } else { 6831ae08745Sheppo ldcp->status = LDC_READY; 6841ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 6851ae08745Sheppo } 6861ae08745Sheppo } 6871ae08745Sheppo } 6881ae08745Sheppo 6891ae08745Sheppo /* 6901ae08745Sheppo * Reset a LDC channel 6911ae08745Sheppo */ 69220ae46ebSha137994 void 6933af08d82Slm66018 i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset) 6941ae08745Sheppo { 69583d3bc6fSnarayan DWARN(ldcp->id, "i_ldc_reset: (0x%llx) channel reset\n", ldcp->id); 6961ae08745Sheppo 697d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->lock)); 698d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 699d10e4ef2Snarayan 7003af08d82Slm66018 /* reconfig Tx and Rx queues */ 7011ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 7023af08d82Slm66018 (void) i_ldc_rxq_reconf(ldcp, force_reset); 7033af08d82Slm66018 7043af08d82Slm66018 /* Clear Tx and Rx interrupts */ 7053af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 7063af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 7073af08d82Slm66018 7083af08d82Slm66018 /* Reset channel state */ 7091ae08745Sheppo i_ldc_reset_state(ldcp); 7103af08d82Slm66018 7113af08d82Slm66018 /* Mark channel in reset */ 7123af08d82Slm66018 ldcp->tstate |= TS_IN_RESET; 7131ae08745Sheppo } 7141ae08745Sheppo 7155699897cSHaik Aftandilian /* 7165699897cSHaik Aftandilian * Walk the channel list and reset channels if they are of the right 7175699897cSHaik Aftandilian * devclass and their Rx queues have been configured. No locks are 7185699897cSHaik Aftandilian * taken because the function is only invoked by the kernel just before 7195699897cSHaik Aftandilian * entering the prom or debugger when the system is single-threaded. 7205699897cSHaik Aftandilian */ 7215699897cSHaik Aftandilian static void 7225699897cSHaik Aftandilian i_ldc_debug_enter(void) 7235699897cSHaik Aftandilian { 7245699897cSHaik Aftandilian ldc_chan_t *ldcp; 7255699897cSHaik Aftandilian 7265699897cSHaik Aftandilian ldcp = ldcssp->chan_list; 7275699897cSHaik Aftandilian while (ldcp != NULL) { 7285699897cSHaik Aftandilian if (((ldcp->tstate & TS_QCONF_RDY) == TS_QCONF_RDY) && 7295699897cSHaik Aftandilian (LDC_DEVCLASS_PROM_RESET(ldcp->devclass) != 0)) { 7305699897cSHaik Aftandilian (void) hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, 7315699897cSHaik Aftandilian ldcp->rx_q_entries); 7325699897cSHaik Aftandilian } 7335699897cSHaik Aftandilian ldcp = ldcp->next; 7345699897cSHaik Aftandilian } 7355699897cSHaik Aftandilian } 7364bac2208Snarayan 7371ae08745Sheppo /* 7381ae08745Sheppo * Clear pending interrupts 7391ae08745Sheppo */ 7401ae08745Sheppo static void 7411ae08745Sheppo i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype) 7421ae08745Sheppo { 7431ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 7441ae08745Sheppo 7451ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 7463af08d82Slm66018 ASSERT(cinfo->dip != NULL); 7474bac2208Snarayan 7483af08d82Slm66018 switch (itype) { 7493af08d82Slm66018 case CNEX_TX_INTR: 7504bac2208Snarayan /* check Tx interrupt */ 7513af08d82Slm66018 if (ldcp->tx_intr_state) 7523af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_NONE; 7534bac2208Snarayan else 7544bac2208Snarayan return; 7553af08d82Slm66018 break; 7563af08d82Slm66018 7573af08d82Slm66018 case CNEX_RX_INTR: 7584bac2208Snarayan /* check Rx interrupt */ 7593af08d82Slm66018 if (ldcp->rx_intr_state) 7603af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_NONE; 7614bac2208Snarayan else 7624bac2208Snarayan return; 7633af08d82Slm66018 break; 7644bac2208Snarayan } 7654bac2208Snarayan 7661ae08745Sheppo (void) cinfo->clr_intr(cinfo->dip, ldcp->id, itype); 7674bac2208Snarayan D2(ldcp->id, 7684bac2208Snarayan "i_ldc_clear_intr: (0x%llx) cleared 0x%x intr\n", 7694bac2208Snarayan ldcp->id, itype); 7701ae08745Sheppo } 7711ae08745Sheppo 7721ae08745Sheppo /* 7731ae08745Sheppo * Set the receive queue head 7740a55fbb7Slm66018 * Resets connection and returns an error if it fails. 7751ae08745Sheppo */ 7761ae08745Sheppo static int 7771ae08745Sheppo i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head) 7781ae08745Sheppo { 7791ae08745Sheppo int rv; 7800a55fbb7Slm66018 int retries; 7811ae08745Sheppo 7821ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 7830a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 7840a55fbb7Slm66018 7850a55fbb7Slm66018 if ((rv = hv_ldc_rx_set_qhead(ldcp->id, head)) == 0) 7860a55fbb7Slm66018 return (0); 7870a55fbb7Slm66018 7880a55fbb7Slm66018 if (rv != H_EWOULDBLOCK) 7890a55fbb7Slm66018 break; 7900a55fbb7Slm66018 7910a55fbb7Slm66018 /* wait for ldc_delay usecs */ 7920a55fbb7Slm66018 drv_usecwait(ldc_delay); 7931ae08745Sheppo } 7941ae08745Sheppo 795*32225099SZach Kissel cmn_err(CE_WARN, "ldc_set_rx_qhead: (0x%lx) cannot set qhead 0x%lx", 7960a55fbb7Slm66018 ldcp->id, head); 797d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 7983af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 799d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 8000a55fbb7Slm66018 8010a55fbb7Slm66018 return (ECONNRESET); 8021ae08745Sheppo } 8031ae08745Sheppo 80422f747efSnarayan /* 80522f747efSnarayan * Returns the tx_head to be used for transfer 80622f747efSnarayan */ 80722f747efSnarayan static void 80822f747efSnarayan i_ldc_get_tx_head(ldc_chan_t *ldcp, uint64_t *head) 80922f747efSnarayan { 81022f747efSnarayan ldc_msg_t *pkt; 81122f747efSnarayan 81222f747efSnarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 81322f747efSnarayan 81422f747efSnarayan /* get current Tx head */ 81522f747efSnarayan *head = ldcp->tx_head; 81622f747efSnarayan 81722f747efSnarayan /* 81822f747efSnarayan * Reliable mode will use the ACKd head instead of the regular tx_head. 81922f747efSnarayan * Also in Reliable mode, advance ackd_head for all non DATA/INFO pkts, 82022f747efSnarayan * up to the current location of tx_head. This needs to be done 82122f747efSnarayan * as the peer will only ACK DATA/INFO pkts. 82222f747efSnarayan */ 82320ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 82422f747efSnarayan while (ldcp->tx_ackd_head != ldcp->tx_head) { 82522f747efSnarayan pkt = (ldc_msg_t *)(ldcp->tx_q_va + ldcp->tx_ackd_head); 82622f747efSnarayan if ((pkt->type & LDC_DATA) && (pkt->stype & LDC_INFO)) { 82722f747efSnarayan break; 82822f747efSnarayan } 82922f747efSnarayan /* advance ACKd head */ 83022f747efSnarayan ldcp->tx_ackd_head = 83122f747efSnarayan (ldcp->tx_ackd_head + LDC_PACKET_SIZE) % 83222f747efSnarayan (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 83322f747efSnarayan } 83422f747efSnarayan *head = ldcp->tx_ackd_head; 83522f747efSnarayan } 83622f747efSnarayan } 8371ae08745Sheppo 8381ae08745Sheppo /* 8391ae08745Sheppo * Returns the tx_tail to be used for transfer 8401ae08745Sheppo * Re-reads the TX queue ptrs if and only if the 8411ae08745Sheppo * the cached head and tail are equal (queue is full) 8421ae08745Sheppo */ 8431ae08745Sheppo static int 8441ae08745Sheppo i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail) 8451ae08745Sheppo { 8461ae08745Sheppo int rv; 8471ae08745Sheppo uint64_t current_head, new_tail; 8481ae08745Sheppo 849d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 8501ae08745Sheppo /* Read the head and tail ptrs from HV */ 8511ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 8521ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 8531ae08745Sheppo if (rv) { 8541ae08745Sheppo cmn_err(CE_WARN, 8551ae08745Sheppo "i_ldc_get_tx_tail: (0x%lx) cannot read qptrs\n", 8561ae08745Sheppo ldcp->id); 8571ae08745Sheppo return (EIO); 8581ae08745Sheppo } 8591ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 860cb112a14Slm66018 D1(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) channel not ready\n", 8611ae08745Sheppo ldcp->id); 8621ae08745Sheppo return (ECONNRESET); 8631ae08745Sheppo } 8641ae08745Sheppo 86522f747efSnarayan i_ldc_get_tx_head(ldcp, ¤t_head); 8661ae08745Sheppo 8671ae08745Sheppo /* increment the tail */ 8681ae08745Sheppo new_tail = (ldcp->tx_tail + LDC_PACKET_SIZE) % 8691ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 8701ae08745Sheppo 8711ae08745Sheppo if (new_tail == current_head) { 8721ae08745Sheppo DWARN(ldcp->id, 8731ae08745Sheppo "i_ldc_get_tx_tail: (0x%llx) TX queue is full\n", 8741ae08745Sheppo ldcp->id); 8751ae08745Sheppo return (EWOULDBLOCK); 8761ae08745Sheppo } 8771ae08745Sheppo 8781ae08745Sheppo D2(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) head=0x%llx, tail=0x%llx\n", 8791ae08745Sheppo ldcp->id, ldcp->tx_head, ldcp->tx_tail); 8801ae08745Sheppo 8811ae08745Sheppo *tail = ldcp->tx_tail; 8821ae08745Sheppo return (0); 8831ae08745Sheppo } 8841ae08745Sheppo 8851ae08745Sheppo /* 8861ae08745Sheppo * Set the tail pointer. If HV returns EWOULDBLOCK, it will back off 8870a55fbb7Slm66018 * and retry ldc_max_retries times before returning an error. 8881ae08745Sheppo * Returns 0, EWOULDBLOCK or EIO 8891ae08745Sheppo */ 8901ae08745Sheppo static int 8911ae08745Sheppo i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail) 8921ae08745Sheppo { 8931ae08745Sheppo int rv, retval = EWOULDBLOCK; 8940a55fbb7Slm66018 int retries; 8951ae08745Sheppo 896d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 8970a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 8981ae08745Sheppo 8991ae08745Sheppo if ((rv = hv_ldc_tx_set_qtail(ldcp->id, tail)) == 0) { 9001ae08745Sheppo retval = 0; 9011ae08745Sheppo break; 9021ae08745Sheppo } 9031ae08745Sheppo if (rv != H_EWOULDBLOCK) { 9041ae08745Sheppo DWARN(ldcp->id, "i_ldc_set_tx_tail: (0x%llx) set " 9051ae08745Sheppo "qtail=0x%llx failed, rv=%d\n", ldcp->id, tail, rv); 9061ae08745Sheppo retval = EIO; 9071ae08745Sheppo break; 9081ae08745Sheppo } 9091ae08745Sheppo 9100a55fbb7Slm66018 /* wait for ldc_delay usecs */ 9110a55fbb7Slm66018 drv_usecwait(ldc_delay); 9121ae08745Sheppo } 9131ae08745Sheppo return (retval); 9141ae08745Sheppo } 9151ae08745Sheppo 9161ae08745Sheppo /* 91758283286Sha137994 * Copy a data packet from the HV receive queue to the data queue. 91858283286Sha137994 * Caller must ensure that the data queue is not already full. 91958283286Sha137994 * 92058283286Sha137994 * The *head argument represents the current head pointer for the HV 92158283286Sha137994 * receive queue. After copying a packet from the HV receive queue, 92258283286Sha137994 * the *head pointer will be updated. This allows the caller to update 92358283286Sha137994 * the head pointer in HV using the returned *head value. 92458283286Sha137994 */ 92558283286Sha137994 void 92658283286Sha137994 i_ldc_rxdq_copy(ldc_chan_t *ldcp, uint64_t *head) 92758283286Sha137994 { 92858283286Sha137994 uint64_t q_size, dq_size; 92958283286Sha137994 93058283286Sha137994 ASSERT(MUTEX_HELD(&ldcp->lock)); 93158283286Sha137994 93258283286Sha137994 q_size = ldcp->rx_q_entries << LDC_PACKET_SHIFT; 93358283286Sha137994 dq_size = ldcp->rx_dq_entries << LDC_PACKET_SHIFT; 93458283286Sha137994 93558283286Sha137994 ASSERT(Q_CONTIG_SPACE(ldcp->rx_dq_head, ldcp->rx_dq_tail, 93658283286Sha137994 dq_size) >= LDC_PACKET_SIZE); 93758283286Sha137994 93858283286Sha137994 bcopy((void *)(ldcp->rx_q_va + *head), 93958283286Sha137994 (void *)(ldcp->rx_dq_va + ldcp->rx_dq_tail), LDC_PACKET_SIZE); 94058283286Sha137994 TRACE_RXDQ_COPY(ldcp, LDC_PACKET_SIZE); 94158283286Sha137994 94258283286Sha137994 /* Update rx head */ 94358283286Sha137994 *head = (*head + LDC_PACKET_SIZE) % q_size; 94458283286Sha137994 94558283286Sha137994 /* Update dq tail */ 94658283286Sha137994 ldcp->rx_dq_tail = (ldcp->rx_dq_tail + LDC_PACKET_SIZE) % dq_size; 94758283286Sha137994 } 94858283286Sha137994 94958283286Sha137994 /* 95058283286Sha137994 * Update the Rx data queue head pointer 95158283286Sha137994 */ 95258283286Sha137994 static int 95358283286Sha137994 i_ldc_set_rxdq_head(ldc_chan_t *ldcp, uint64_t head) 95458283286Sha137994 { 95558283286Sha137994 ldcp->rx_dq_head = head; 95658283286Sha137994 return (0); 95758283286Sha137994 } 95858283286Sha137994 95958283286Sha137994 /* 96058283286Sha137994 * Get the Rx data queue head and tail pointers 96158283286Sha137994 */ 96258283286Sha137994 static uint64_t 96358283286Sha137994 i_ldc_dq_rx_get_state(ldc_chan_t *ldcp, uint64_t *head, uint64_t *tail, 96458283286Sha137994 uint64_t *link_state) 96558283286Sha137994 { 96658283286Sha137994 _NOTE(ARGUNUSED(link_state)) 96758283286Sha137994 *head = ldcp->rx_dq_head; 96858283286Sha137994 *tail = ldcp->rx_dq_tail; 96958283286Sha137994 return (0); 97058283286Sha137994 } 97158283286Sha137994 97258283286Sha137994 /* 97358283286Sha137994 * Wrapper for the Rx HV queue set head function. Giving the 97458283286Sha137994 * data queue and HV queue set head functions the same type. 97558283286Sha137994 */ 97658283286Sha137994 static uint64_t 97758283286Sha137994 i_ldc_hvq_rx_get_state(ldc_chan_t *ldcp, uint64_t *head, uint64_t *tail, 97858283286Sha137994 uint64_t *link_state) 97958283286Sha137994 { 98058283286Sha137994 return (i_ldc_h2v_error(hv_ldc_rx_get_state(ldcp->id, head, tail, 98158283286Sha137994 link_state))); 98258283286Sha137994 } 98358283286Sha137994 98458283286Sha137994 /* 98558283286Sha137994 * LDC receive interrupt handler 98658283286Sha137994 * triggered for channel with data pending to read 98758283286Sha137994 * i.e. Rx queue content changes 98858283286Sha137994 */ 98958283286Sha137994 static uint_t 99058283286Sha137994 i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2) 99158283286Sha137994 { 99258283286Sha137994 _NOTE(ARGUNUSED(arg2)) 99358283286Sha137994 99458283286Sha137994 ldc_chan_t *ldcp; 99558283286Sha137994 boolean_t notify; 99658283286Sha137994 uint64_t event; 99712f80fa6Sha137994 int rv, status; 99858283286Sha137994 99958283286Sha137994 /* Get the channel for which interrupt was received */ 100058283286Sha137994 if (arg1 == NULL) { 100158283286Sha137994 cmn_err(CE_WARN, "i_ldc_rx_hdlr: invalid arg\n"); 100258283286Sha137994 return (DDI_INTR_UNCLAIMED); 100358283286Sha137994 } 100458283286Sha137994 100558283286Sha137994 ldcp = (ldc_chan_t *)arg1; 100658283286Sha137994 100758283286Sha137994 D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 100858283286Sha137994 ldcp->id, ldcp); 100958283286Sha137994 D1(ldcp->id, "i_ldc_rx_hdlr: (%llx) USR%lx/TS%lx/HS%lx, LSTATE=%lx\n", 101058283286Sha137994 ldcp->id, ldcp->status, ldcp->tstate, ldcp->hstate, 101158283286Sha137994 ldcp->link_state); 101258283286Sha137994 101358283286Sha137994 /* Lock channel */ 101458283286Sha137994 mutex_enter(&ldcp->lock); 101558283286Sha137994 101658283286Sha137994 /* Mark the interrupt as being actively handled */ 101758283286Sha137994 ldcp->rx_intr_state = LDC_INTR_ACTIVE; 101858283286Sha137994 101912f80fa6Sha137994 status = i_ldc_rx_process_hvq(ldcp, ¬ify, &event); 102058283286Sha137994 102120ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) { 102258283286Sha137994 /* 102358283286Sha137994 * If there are no data packets on the queue, clear 102458283286Sha137994 * the interrupt. Otherwise, the ldc_read will clear 102558283286Sha137994 * interrupts after draining the queue. To indicate the 102658283286Sha137994 * interrupt has not yet been cleared, it is marked 102758283286Sha137994 * as pending. 102858283286Sha137994 */ 102958283286Sha137994 if ((event & LDC_EVT_READ) == 0) { 103058283286Sha137994 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 103158283286Sha137994 } else { 103258283286Sha137994 ldcp->rx_intr_state = LDC_INTR_PEND; 103358283286Sha137994 } 103458283286Sha137994 } 103558283286Sha137994 103658283286Sha137994 /* if callbacks are disabled, do not notify */ 103758283286Sha137994 if (notify && ldcp->cb_enabled) { 103858283286Sha137994 ldcp->cb_inprogress = B_TRUE; 103958283286Sha137994 mutex_exit(&ldcp->lock); 104058283286Sha137994 rv = ldcp->cb(event, ldcp->cb_arg); 104158283286Sha137994 if (rv) { 104258283286Sha137994 DWARN(ldcp->id, 104358283286Sha137994 "i_ldc_rx_hdlr: (0x%llx) callback failure", 104458283286Sha137994 ldcp->id); 104558283286Sha137994 } 104658283286Sha137994 mutex_enter(&ldcp->lock); 104758283286Sha137994 ldcp->cb_inprogress = B_FALSE; 104858283286Sha137994 } 104958283286Sha137994 105020ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 105112f80fa6Sha137994 if (status == ENOSPC) { 105258283286Sha137994 /* 105312f80fa6Sha137994 * Here, ENOSPC indicates the secondary data 105412f80fa6Sha137994 * queue is full and the Rx queue is non-empty. 105512f80fa6Sha137994 * Much like how reliable and raw modes are 105612f80fa6Sha137994 * handled above, since the Rx queue is non- 105712f80fa6Sha137994 * empty, we mark the interrupt as pending to 105812f80fa6Sha137994 * indicate it has not yet been cleared. 105912f80fa6Sha137994 */ 106012f80fa6Sha137994 ldcp->rx_intr_state = LDC_INTR_PEND; 106112f80fa6Sha137994 } else { 106212f80fa6Sha137994 /* 106312f80fa6Sha137994 * We have processed all CTRL packets and 106412f80fa6Sha137994 * copied all DATA packets to the secondary 106512f80fa6Sha137994 * queue. Clear the interrupt. 106658283286Sha137994 */ 106758283286Sha137994 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 106858283286Sha137994 } 106912f80fa6Sha137994 } 107058283286Sha137994 107158283286Sha137994 mutex_exit(&ldcp->lock); 107258283286Sha137994 107358283286Sha137994 D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) exiting handler", ldcp->id); 107458283286Sha137994 107558283286Sha137994 return (DDI_INTR_CLAIMED); 107658283286Sha137994 } 107758283286Sha137994 107858283286Sha137994 /* 107958283286Sha137994 * Wrapper for the Rx HV queue processing function to be used when 108058283286Sha137994 * checking the Rx HV queue for data packets. Unlike the interrupt 108158283286Sha137994 * handler code flow, the Rx interrupt is not cleared here and 108258283286Sha137994 * callbacks are not made. 108358283286Sha137994 */ 108458283286Sha137994 static uint_t 108558283286Sha137994 i_ldc_chkq(ldc_chan_t *ldcp) 108658283286Sha137994 { 108758283286Sha137994 boolean_t notify; 108858283286Sha137994 uint64_t event; 108958283286Sha137994 109058283286Sha137994 return (i_ldc_rx_process_hvq(ldcp, ¬ify, &event)); 109158283286Sha137994 } 109258283286Sha137994 109358283286Sha137994 /* 10941ae08745Sheppo * Send a LDC message 10951ae08745Sheppo */ 10961ae08745Sheppo static int 10971ae08745Sheppo i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype, 10981ae08745Sheppo uint8_t ctrlmsg) 10991ae08745Sheppo { 11001ae08745Sheppo int rv; 11011ae08745Sheppo ldc_msg_t *pkt; 11021ae08745Sheppo uint64_t tx_tail; 110322f747efSnarayan uint32_t curr_seqid; 11041ae08745Sheppo 1105d10e4ef2Snarayan /* Obtain Tx lock */ 1106d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1107d10e4ef2Snarayan 110822f747efSnarayan curr_seqid = ldcp->last_msg_snt; 110922f747efSnarayan 11101ae08745Sheppo /* get the current tail for the message */ 11111ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 11121ae08745Sheppo if (rv) { 11131ae08745Sheppo DWARN(ldcp->id, 11141ae08745Sheppo "i_ldc_send_pkt: (0x%llx) error sending pkt, " 11151ae08745Sheppo "type=0x%x,subtype=0x%x,ctrl=0x%x\n", 11161ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 1117d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11181ae08745Sheppo return (rv); 11191ae08745Sheppo } 11201ae08745Sheppo 11211ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 11221ae08745Sheppo ZERO_PKT(pkt); 11231ae08745Sheppo 11241ae08745Sheppo /* Initialize the packet */ 11251ae08745Sheppo pkt->type = pkttype; 11261ae08745Sheppo pkt->stype = subtype; 11271ae08745Sheppo pkt->ctrl = ctrlmsg; 11281ae08745Sheppo 11291ae08745Sheppo /* Store ackid/seqid iff it is RELIABLE mode & not a RTS/RTR message */ 11301ae08745Sheppo if (((ctrlmsg & LDC_CTRL_MASK) != LDC_RTS) && 11311ae08745Sheppo ((ctrlmsg & LDC_CTRL_MASK) != LDC_RTR)) { 11321ae08745Sheppo curr_seqid++; 11331ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) { 11341ae08745Sheppo pkt->seqid = curr_seqid; 11351ae08745Sheppo pkt->ackid = ldcp->last_msg_rcd; 11361ae08745Sheppo } 11371ae08745Sheppo } 11381ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_send_pkt", (uint64_t)pkt); 11391ae08745Sheppo 11401ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 11411ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 11421ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 11431ae08745Sheppo 11441ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 11451ae08745Sheppo if (rv) { 11461ae08745Sheppo DWARN(ldcp->id, 11471ae08745Sheppo "i_ldc_send_pkt:(0x%llx) error sending pkt, " 11481ae08745Sheppo "type=0x%x,stype=0x%x,ctrl=0x%x\n", 11491ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 1150d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11511ae08745Sheppo return (EIO); 11521ae08745Sheppo } 11531ae08745Sheppo 11541ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 11551ae08745Sheppo ldcp->tx_tail = tx_tail; 11561ae08745Sheppo 1157d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11581ae08745Sheppo return (0); 11591ae08745Sheppo } 11601ae08745Sheppo 11611ae08745Sheppo /* 11621ae08745Sheppo * Checks if packet was received in right order 1163e1ebb9ecSlm66018 * in the case of a reliable link. 11641ae08745Sheppo * Returns 0 if in order, else EIO 11651ae08745Sheppo */ 11661ae08745Sheppo static int 11671ae08745Sheppo i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *msg) 11681ae08745Sheppo { 11691ae08745Sheppo /* No seqid checking for RAW mode */ 11701ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) 11711ae08745Sheppo return (0); 11721ae08745Sheppo 11731ae08745Sheppo /* No seqid checking for version, RTS, RTR message */ 11741ae08745Sheppo if (msg->ctrl == LDC_VER || 11751ae08745Sheppo msg->ctrl == LDC_RTS || 11761ae08745Sheppo msg->ctrl == LDC_RTR) 11771ae08745Sheppo return (0); 11781ae08745Sheppo 11791ae08745Sheppo /* Initial seqid to use is sent in RTS/RTR and saved in last_msg_rcd */ 11801ae08745Sheppo if (msg->seqid != (ldcp->last_msg_rcd + 1)) { 11811ae08745Sheppo DWARN(ldcp->id, 11821ae08745Sheppo "i_ldc_check_seqid: (0x%llx) out-of-order pkt, got 0x%x, " 11831ae08745Sheppo "expecting 0x%x\n", ldcp->id, msg->seqid, 11841ae08745Sheppo (ldcp->last_msg_rcd + 1)); 11851ae08745Sheppo return (EIO); 11861ae08745Sheppo } 11871ae08745Sheppo 118883d3bc6fSnarayan #ifdef DEBUG 118983d3bc6fSnarayan if (LDC_INJECT_PKTLOSS(ldcp)) { 119083d3bc6fSnarayan DWARN(ldcp->id, 119183d3bc6fSnarayan "i_ldc_check_seqid: (0x%llx) inject pkt loss\n", ldcp->id); 119283d3bc6fSnarayan return (EIO); 119383d3bc6fSnarayan } 119483d3bc6fSnarayan #endif 119583d3bc6fSnarayan 11961ae08745Sheppo return (0); 11971ae08745Sheppo } 11981ae08745Sheppo 11991ae08745Sheppo 12001ae08745Sheppo /* 12011ae08745Sheppo * Process an incoming version ctrl message 12021ae08745Sheppo */ 12031ae08745Sheppo static int 12041ae08745Sheppo i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg) 12051ae08745Sheppo { 12061ae08745Sheppo int rv = 0, idx = ldcp->next_vidx; 12071ae08745Sheppo ldc_msg_t *pkt; 12081ae08745Sheppo uint64_t tx_tail; 12091ae08745Sheppo ldc_ver_t *rcvd_ver; 12101ae08745Sheppo 12111ae08745Sheppo /* get the received version */ 12121ae08745Sheppo rcvd_ver = (ldc_ver_t *)((uint64_t)msg + LDC_PAYLOAD_VER_OFF); 12131ae08745Sheppo 12141ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) received VER v%u.%u\n", 12151ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 12161ae08745Sheppo 1217d10e4ef2Snarayan /* Obtain Tx lock */ 1218d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1219d10e4ef2Snarayan 12201ae08745Sheppo switch (msg->stype) { 12211ae08745Sheppo case LDC_INFO: 12221ae08745Sheppo 12233af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) { 12243af08d82Slm66018 (void) i_ldc_txq_reconf(ldcp); 12253af08d82Slm66018 i_ldc_reset_state(ldcp); 12263af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 12273af08d82Slm66018 return (EAGAIN); 12283af08d82Slm66018 } 12293af08d82Slm66018 12301ae08745Sheppo /* get the current tail and pkt for the response */ 12311ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 12321ae08745Sheppo if (rv != 0) { 12331ae08745Sheppo DWARN(ldcp->id, 12341ae08745Sheppo "i_ldc_process_VER: (0x%llx) err sending " 12351ae08745Sheppo "version ACK/NACK\n", ldcp->id); 12363af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1237d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12381ae08745Sheppo return (ECONNRESET); 12391ae08745Sheppo } 12401ae08745Sheppo 12411ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 12421ae08745Sheppo ZERO_PKT(pkt); 12431ae08745Sheppo 12441ae08745Sheppo /* initialize the packet */ 12451ae08745Sheppo pkt->type = LDC_CTRL; 12461ae08745Sheppo pkt->ctrl = LDC_VER; 12471ae08745Sheppo 12481ae08745Sheppo for (;;) { 12491ae08745Sheppo 12501ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: got %u.%u chk %u.%u\n", 12511ae08745Sheppo rcvd_ver->major, rcvd_ver->minor, 12521ae08745Sheppo ldc_versions[idx].major, ldc_versions[idx].minor); 12531ae08745Sheppo 12541ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 12551ae08745Sheppo /* major version match - ACK version */ 12561ae08745Sheppo pkt->stype = LDC_ACK; 12571ae08745Sheppo 12581ae08745Sheppo /* 12591ae08745Sheppo * lower minor version to the one this endpt 12601ae08745Sheppo * supports, if necessary 12611ae08745Sheppo */ 12621ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 12631ae08745Sheppo rcvd_ver->minor = 12641ae08745Sheppo ldc_versions[idx].minor; 12651ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 12661ae08745Sheppo 12671ae08745Sheppo break; 12681ae08745Sheppo } 12691ae08745Sheppo 12701ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 12711ae08745Sheppo 12721ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 12731ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 12741ae08745Sheppo ldc_versions[idx].major, 12751ae08745Sheppo ldc_versions[idx].minor); 12761ae08745Sheppo 12771ae08745Sheppo /* nack with next lower version */ 12781ae08745Sheppo pkt->stype = LDC_NACK; 12791ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 12801ae08745Sheppo sizeof (ldc_versions[idx])); 12811ae08745Sheppo ldcp->next_vidx = idx; 12821ae08745Sheppo break; 12831ae08745Sheppo } 12841ae08745Sheppo 12851ae08745Sheppo /* next major version */ 12861ae08745Sheppo idx++; 12871ae08745Sheppo 12881ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 12891ae08745Sheppo 12901ae08745Sheppo if (idx == LDC_NUM_VERS) { 12911ae08745Sheppo /* no version match - send NACK */ 12921ae08745Sheppo pkt->stype = LDC_NACK; 12931ae08745Sheppo bzero(pkt->udata, sizeof (ldc_ver_t)); 12941ae08745Sheppo ldcp->next_vidx = 0; 12951ae08745Sheppo break; 12961ae08745Sheppo } 12971ae08745Sheppo } 12981ae08745Sheppo 12991ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 13001ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 13011ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 13021ae08745Sheppo 13031ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 13041ae08745Sheppo if (rv == 0) { 13051ae08745Sheppo ldcp->tx_tail = tx_tail; 13061ae08745Sheppo if (pkt->stype == LDC_ACK) { 13071ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent" 13081ae08745Sheppo " version ACK\n", ldcp->id); 13091ae08745Sheppo /* Save the ACK'd version */ 13101ae08745Sheppo ldcp->version.major = rcvd_ver->major; 13111ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 13120a55fbb7Slm66018 ldcp->hstate |= TS_RCVD_VER; 13131ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 131483d3bc6fSnarayan D1(DBG_ALL_LDCS, 13153af08d82Slm66018 "(0x%llx) Sent ACK, " 13163af08d82Slm66018 "Agreed on version v%u.%u\n", 13171ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 13181ae08745Sheppo } 13191ae08745Sheppo } else { 13201ae08745Sheppo DWARN(ldcp->id, 13211ae08745Sheppo "i_ldc_process_VER: (0x%llx) error sending " 13221ae08745Sheppo "ACK/NACK\n", ldcp->id); 13233af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1324d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13251ae08745Sheppo return (ECONNRESET); 13261ae08745Sheppo } 13271ae08745Sheppo 13281ae08745Sheppo break; 13291ae08745Sheppo 13301ae08745Sheppo case LDC_ACK: 13313af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) { 13323af08d82Slm66018 if (ldcp->version.major != rcvd_ver->major || 13333af08d82Slm66018 ldcp->version.minor != rcvd_ver->minor) { 13343af08d82Slm66018 13353af08d82Slm66018 /* mismatched version - reset connection */ 13363af08d82Slm66018 DWARN(ldcp->id, 13373af08d82Slm66018 "i_ldc_process_VER: (0x%llx) recvd" 13383af08d82Slm66018 " ACK ver != sent ACK ver\n", ldcp->id); 13393af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 13403af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 13413af08d82Slm66018 return (ECONNRESET); 13423af08d82Slm66018 } 13433af08d82Slm66018 } else { 13441ae08745Sheppo /* SUCCESS - we have agreed on a version */ 13451ae08745Sheppo ldcp->version.major = rcvd_ver->major; 13461ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 13471ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 13483af08d82Slm66018 } 13491ae08745Sheppo 1350cb112a14Slm66018 D1(ldcp->id, "(0x%llx) Got ACK, Agreed on version v%u.%u\n", 13511ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 13521ae08745Sheppo 13531ae08745Sheppo /* initiate RTS-RTR-RDX handshake */ 13541ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 13551ae08745Sheppo if (rv) { 13561ae08745Sheppo DWARN(ldcp->id, 13571ae08745Sheppo "i_ldc_process_VER: (0x%llx) cannot send RTS\n", 13581ae08745Sheppo ldcp->id); 13593af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1360d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13611ae08745Sheppo return (ECONNRESET); 13621ae08745Sheppo } 13631ae08745Sheppo 13641ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 13651ae08745Sheppo ZERO_PKT(pkt); 13661ae08745Sheppo 13671ae08745Sheppo pkt->type = LDC_CTRL; 13681ae08745Sheppo pkt->stype = LDC_INFO; 13691ae08745Sheppo pkt->ctrl = LDC_RTS; 13701ae08745Sheppo pkt->env = ldcp->mode; 13711ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 13721ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 13731ae08745Sheppo 13741ae08745Sheppo ldcp->last_msg_rcd = LDC_INIT_SEQID; 13751ae08745Sheppo 13761ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_VER snd rts", (uint64_t)pkt); 13771ae08745Sheppo 13781ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 13791ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 13801ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 13811ae08745Sheppo 13821ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 13831ae08745Sheppo if (rv) { 13841ae08745Sheppo D2(ldcp->id, 13851ae08745Sheppo "i_ldc_process_VER: (0x%llx) no listener\n", 13861ae08745Sheppo ldcp->id); 13873af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1388d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13891ae08745Sheppo return (ECONNRESET); 13901ae08745Sheppo } 13911ae08745Sheppo 13921ae08745Sheppo ldcp->tx_tail = tx_tail; 13931ae08745Sheppo ldcp->hstate |= TS_SENT_RTS; 13941ae08745Sheppo 13951ae08745Sheppo break; 13961ae08745Sheppo 13971ae08745Sheppo case LDC_NACK: 13981ae08745Sheppo /* check if version in NACK is zero */ 13991ae08745Sheppo if (rcvd_ver->major == 0 && rcvd_ver->minor == 0) { 14001ae08745Sheppo /* version handshake failure */ 14011ae08745Sheppo DWARN(DBG_ALL_LDCS, 14021ae08745Sheppo "i_ldc_process_VER: (0x%llx) no version match\n", 14031ae08745Sheppo ldcp->id); 14043af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1405d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14061ae08745Sheppo return (ECONNRESET); 14071ae08745Sheppo } 14081ae08745Sheppo 14091ae08745Sheppo /* get the current tail and pkt for the response */ 14101ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 14111ae08745Sheppo if (rv != 0) { 14121ae08745Sheppo cmn_err(CE_NOTE, 14131ae08745Sheppo "i_ldc_process_VER: (0x%lx) err sending " 14141ae08745Sheppo "version ACK/NACK\n", ldcp->id); 14153af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1416d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14171ae08745Sheppo return (ECONNRESET); 14181ae08745Sheppo } 14191ae08745Sheppo 14201ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 14211ae08745Sheppo ZERO_PKT(pkt); 14221ae08745Sheppo 14231ae08745Sheppo /* initialize the packet */ 14241ae08745Sheppo pkt->type = LDC_CTRL; 14251ae08745Sheppo pkt->ctrl = LDC_VER; 14261ae08745Sheppo pkt->stype = LDC_INFO; 14271ae08745Sheppo 14281ae08745Sheppo /* check ver in NACK msg has a match */ 14291ae08745Sheppo for (;;) { 14301ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 14311ae08745Sheppo /* 14321ae08745Sheppo * major version match - resubmit request 14331ae08745Sheppo * if lower minor version to the one this endpt 14341ae08745Sheppo * supports, if necessary 14351ae08745Sheppo */ 14361ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 14371ae08745Sheppo rcvd_ver->minor = 14381ae08745Sheppo ldc_versions[idx].minor; 14391ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 14401ae08745Sheppo break; 14411ae08745Sheppo } 14421ae08745Sheppo 14431ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 14441ae08745Sheppo 14451ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 14461ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 14471ae08745Sheppo ldc_versions[idx].major, 14481ae08745Sheppo ldc_versions[idx].minor); 14491ae08745Sheppo 14501ae08745Sheppo /* send next lower version */ 14511ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 14521ae08745Sheppo sizeof (ldc_versions[idx])); 14531ae08745Sheppo ldcp->next_vidx = idx; 14541ae08745Sheppo break; 14551ae08745Sheppo } 14561ae08745Sheppo 14571ae08745Sheppo /* next version */ 14581ae08745Sheppo idx++; 14591ae08745Sheppo 14601ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 14611ae08745Sheppo 14621ae08745Sheppo if (idx == LDC_NUM_VERS) { 14631ae08745Sheppo /* no version match - terminate */ 14641ae08745Sheppo ldcp->next_vidx = 0; 1465d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14661ae08745Sheppo return (ECONNRESET); 14671ae08745Sheppo } 14681ae08745Sheppo } 14691ae08745Sheppo 14701ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 14711ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 14721ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 14731ae08745Sheppo 14741ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 14751ae08745Sheppo if (rv == 0) { 14761ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent version" 14771ae08745Sheppo "INFO v%u.%u\n", ldcp->id, ldc_versions[idx].major, 14781ae08745Sheppo ldc_versions[idx].minor); 14791ae08745Sheppo ldcp->tx_tail = tx_tail; 14801ae08745Sheppo } else { 14811ae08745Sheppo cmn_err(CE_NOTE, 14821ae08745Sheppo "i_ldc_process_VER: (0x%lx) error sending version" 14831ae08745Sheppo "INFO\n", ldcp->id); 14843af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1485d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14861ae08745Sheppo return (ECONNRESET); 14871ae08745Sheppo } 14881ae08745Sheppo 14891ae08745Sheppo break; 14901ae08745Sheppo } 14911ae08745Sheppo 1492d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14931ae08745Sheppo return (rv); 14941ae08745Sheppo } 14951ae08745Sheppo 14961ae08745Sheppo 14971ae08745Sheppo /* 14981ae08745Sheppo * Process an incoming RTS ctrl message 14991ae08745Sheppo */ 15001ae08745Sheppo static int 15011ae08745Sheppo i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg) 15021ae08745Sheppo { 15031ae08745Sheppo int rv = 0; 15041ae08745Sheppo ldc_msg_t *pkt; 15051ae08745Sheppo uint64_t tx_tail; 15061ae08745Sheppo boolean_t sent_NACK = B_FALSE; 15071ae08745Sheppo 15081ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTS: (0x%llx) received RTS\n", ldcp->id); 15091ae08745Sheppo 15101ae08745Sheppo switch (msg->stype) { 15111ae08745Sheppo case LDC_NACK: 15121ae08745Sheppo DWARN(ldcp->id, 15131ae08745Sheppo "i_ldc_process_RTS: (0x%llx) RTS NACK received\n", 15141ae08745Sheppo ldcp->id); 15151ae08745Sheppo 15161ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1517d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15183af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1519d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15201ae08745Sheppo rv = ECONNRESET; 15211ae08745Sheppo break; 15221ae08745Sheppo 15231ae08745Sheppo case LDC_INFO: 15241ae08745Sheppo 15251ae08745Sheppo /* check mode */ 15261ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 15271ae08745Sheppo cmn_err(CE_NOTE, 15281ae08745Sheppo "i_ldc_process_RTS: (0x%lx) mode mismatch\n", 15291ae08745Sheppo ldcp->id); 15301ae08745Sheppo /* 15311ae08745Sheppo * send NACK in response to MODE message 15321ae08745Sheppo * get the current tail for the response 15331ae08745Sheppo */ 15341ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTS); 15351ae08745Sheppo if (rv) { 15361ae08745Sheppo /* if cannot send NACK - reset channel */ 1537d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15383af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1539d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15401ae08745Sheppo rv = ECONNRESET; 15411ae08745Sheppo break; 15421ae08745Sheppo } 15431ae08745Sheppo sent_NACK = B_TRUE; 15441ae08745Sheppo } 15451ae08745Sheppo break; 15461ae08745Sheppo default: 15471ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTS: (0x%llx) unexp ACK\n", 15481ae08745Sheppo ldcp->id); 1549d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15503af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1551d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15521ae08745Sheppo rv = ECONNRESET; 15531ae08745Sheppo break; 15541ae08745Sheppo } 15551ae08745Sheppo 15561ae08745Sheppo /* 15571ae08745Sheppo * If either the connection was reset (when rv != 0) or 15581ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 15591ae08745Sheppo * we dont want to consume the packet that came in but 15601ae08745Sheppo * not record that we received the RTS 15611ae08745Sheppo */ 15621ae08745Sheppo if (rv || sent_NACK) 15631ae08745Sheppo return (rv); 15641ae08745Sheppo 15651ae08745Sheppo /* record RTS received */ 15661ae08745Sheppo ldcp->hstate |= TS_RCVD_RTS; 15671ae08745Sheppo 15681ae08745Sheppo /* store initial SEQID info */ 15691ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 15701ae08745Sheppo 1571d10e4ef2Snarayan /* Obtain Tx lock */ 1572d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1573d10e4ef2Snarayan 15741ae08745Sheppo /* get the current tail for the response */ 15751ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 15761ae08745Sheppo if (rv != 0) { 15771ae08745Sheppo cmn_err(CE_NOTE, 15781ae08745Sheppo "i_ldc_process_RTS: (0x%lx) err sending RTR\n", 15791ae08745Sheppo ldcp->id); 15803af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1581d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15821ae08745Sheppo return (ECONNRESET); 15831ae08745Sheppo } 15841ae08745Sheppo 15851ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 15861ae08745Sheppo ZERO_PKT(pkt); 15871ae08745Sheppo 15881ae08745Sheppo /* initialize the packet */ 15891ae08745Sheppo pkt->type = LDC_CTRL; 15901ae08745Sheppo pkt->stype = LDC_INFO; 15911ae08745Sheppo pkt->ctrl = LDC_RTR; 15921ae08745Sheppo pkt->env = ldcp->mode; 15931ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 15941ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 15951ae08745Sheppo 15961ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 15971ae08745Sheppo 15981ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 15991ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 16001ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 16011ae08745Sheppo 16021ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 16031ae08745Sheppo if (rv == 0) { 16041ae08745Sheppo D2(ldcp->id, 16051ae08745Sheppo "i_ldc_process_RTS: (0x%llx) sent RTR\n", ldcp->id); 16061ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_RTS sent rtr", (uint64_t)pkt); 16071ae08745Sheppo 16081ae08745Sheppo ldcp->tx_tail = tx_tail; 16091ae08745Sheppo ldcp->hstate |= TS_SENT_RTR; 16101ae08745Sheppo 16111ae08745Sheppo } else { 16121ae08745Sheppo cmn_err(CE_NOTE, 16131ae08745Sheppo "i_ldc_process_RTS: (0x%lx) error sending RTR\n", 16141ae08745Sheppo ldcp->id); 16153af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1616d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16171ae08745Sheppo return (ECONNRESET); 16181ae08745Sheppo } 16191ae08745Sheppo 1620d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16211ae08745Sheppo return (0); 16221ae08745Sheppo } 16231ae08745Sheppo 16241ae08745Sheppo /* 16251ae08745Sheppo * Process an incoming RTR ctrl message 16261ae08745Sheppo */ 16271ae08745Sheppo static int 16281ae08745Sheppo i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg) 16291ae08745Sheppo { 16301ae08745Sheppo int rv = 0; 16311ae08745Sheppo boolean_t sent_NACK = B_FALSE; 16321ae08745Sheppo 16331ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTR: (0x%llx) received RTR\n", ldcp->id); 16341ae08745Sheppo 16351ae08745Sheppo switch (msg->stype) { 16361ae08745Sheppo case LDC_NACK: 16371ae08745Sheppo /* RTR NACK received */ 16381ae08745Sheppo DWARN(ldcp->id, 16391ae08745Sheppo "i_ldc_process_RTR: (0x%llx) RTR NACK received\n", 16401ae08745Sheppo ldcp->id); 16411ae08745Sheppo 16421ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1643d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 16443af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1645d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16461ae08745Sheppo rv = ECONNRESET; 16471ae08745Sheppo 16481ae08745Sheppo break; 16491ae08745Sheppo 16501ae08745Sheppo case LDC_INFO: 16511ae08745Sheppo 16521ae08745Sheppo /* check mode */ 16531ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 16541ae08745Sheppo DWARN(ldcp->id, 1655cb112a14Slm66018 "i_ldc_process_RTR: (0x%llx) mode mismatch, " 1656cb112a14Slm66018 "expecting 0x%x, got 0x%x\n", 1657cb112a14Slm66018 ldcp->id, ldcp->mode, (ldc_mode_t)msg->env); 16581ae08745Sheppo /* 16591ae08745Sheppo * send NACK in response to MODE message 16601ae08745Sheppo * get the current tail for the response 16611ae08745Sheppo */ 16621ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTR); 16631ae08745Sheppo if (rv) { 16641ae08745Sheppo /* if cannot send NACK - reset channel */ 1665d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 16663af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1667d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16681ae08745Sheppo rv = ECONNRESET; 16691ae08745Sheppo break; 16701ae08745Sheppo } 16711ae08745Sheppo sent_NACK = B_TRUE; 16721ae08745Sheppo } 16731ae08745Sheppo break; 16741ae08745Sheppo 16751ae08745Sheppo default: 16761ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTR: (0x%llx) unexp ACK\n", 16771ae08745Sheppo ldcp->id); 16781ae08745Sheppo 16791ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1680d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 16813af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1682d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16831ae08745Sheppo rv = ECONNRESET; 16841ae08745Sheppo break; 16851ae08745Sheppo } 16861ae08745Sheppo 16871ae08745Sheppo /* 16881ae08745Sheppo * If either the connection was reset (when rv != 0) or 16891ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 16901ae08745Sheppo * we dont want to consume the packet that came in but 16911ae08745Sheppo * not record that we received the RTR 16921ae08745Sheppo */ 16931ae08745Sheppo if (rv || sent_NACK) 16941ae08745Sheppo return (rv); 16951ae08745Sheppo 16961ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 16971ae08745Sheppo ldcp->hstate |= TS_RCVD_RTR; 16981ae08745Sheppo 16991ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_INFO, LDC_RDX); 17001ae08745Sheppo if (rv) { 17011ae08745Sheppo cmn_err(CE_NOTE, 17021ae08745Sheppo "i_ldc_process_RTR: (0x%lx) cannot send RDX\n", 17031ae08745Sheppo ldcp->id); 1704d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 17053af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1706d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17071ae08745Sheppo return (ECONNRESET); 17081ae08745Sheppo } 17091ae08745Sheppo D2(ldcp->id, 17101ae08745Sheppo "i_ldc_process_RTR: (0x%llx) sent RDX\n", ldcp->id); 17111ae08745Sheppo 17121ae08745Sheppo ldcp->hstate |= TS_SENT_RDX; 17131ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 17143af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 17151ae08745Sheppo ldcp->status = LDC_UP; 17161ae08745Sheppo 1717cb112a14Slm66018 D1(ldcp->id, "(0x%llx) Handshake Complete\n", ldcp->id); 17181ae08745Sheppo 17191ae08745Sheppo return (0); 17201ae08745Sheppo } 17211ae08745Sheppo 17221ae08745Sheppo 17231ae08745Sheppo /* 17241ae08745Sheppo * Process an incoming RDX ctrl message 17251ae08745Sheppo */ 17261ae08745Sheppo static int 17271ae08745Sheppo i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg) 17281ae08745Sheppo { 17291ae08745Sheppo int rv = 0; 17301ae08745Sheppo 17311ae08745Sheppo D2(ldcp->id, "i_ldc_process_RDX: (0x%llx) received RDX\n", ldcp->id); 17321ae08745Sheppo 17331ae08745Sheppo switch (msg->stype) { 17341ae08745Sheppo case LDC_NACK: 17351ae08745Sheppo /* RDX NACK received */ 17361ae08745Sheppo DWARN(ldcp->id, 17371ae08745Sheppo "i_ldc_process_RDX: (0x%llx) RDX NACK received\n", 17381ae08745Sheppo ldcp->id); 17391ae08745Sheppo 17401ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1741d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 17423af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1743d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17441ae08745Sheppo rv = ECONNRESET; 17451ae08745Sheppo 17461ae08745Sheppo break; 17471ae08745Sheppo 17481ae08745Sheppo case LDC_INFO: 17491ae08745Sheppo 17501ae08745Sheppo /* 17511ae08745Sheppo * if channel is UP and a RDX received after data transmission 17521ae08745Sheppo * has commenced it is an error 17531ae08745Sheppo */ 17541ae08745Sheppo if ((ldcp->tstate == TS_UP) && (ldcp->hstate & TS_RCVD_RDX)) { 17551ae08745Sheppo DWARN(DBG_ALL_LDCS, 17561ae08745Sheppo "i_ldc_process_RDX: (0x%llx) unexpected RDX" 17571ae08745Sheppo " - LDC reset\n", ldcp->id); 1758d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 17593af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1760d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17611ae08745Sheppo return (ECONNRESET); 17621ae08745Sheppo } 17631ae08745Sheppo 17641ae08745Sheppo ldcp->hstate |= TS_RCVD_RDX; 17651ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 17663af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 17671ae08745Sheppo ldcp->status = LDC_UP; 17681ae08745Sheppo 17691ae08745Sheppo D1(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id); 17701ae08745Sheppo break; 17711ae08745Sheppo 17721ae08745Sheppo default: 17731ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RDX: (0x%llx) unexp ACK\n", 17741ae08745Sheppo ldcp->id); 17751ae08745Sheppo 17761ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1777d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 17783af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1779d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17801ae08745Sheppo rv = ECONNRESET; 17811ae08745Sheppo break; 17821ae08745Sheppo } 17831ae08745Sheppo 17841ae08745Sheppo return (rv); 17851ae08745Sheppo } 17861ae08745Sheppo 17871ae08745Sheppo /* 17881ae08745Sheppo * Process an incoming ACK for a data packet 17891ae08745Sheppo */ 17901ae08745Sheppo static int 17911ae08745Sheppo i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg) 17921ae08745Sheppo { 17931ae08745Sheppo int rv; 17941ae08745Sheppo uint64_t tx_head; 17951ae08745Sheppo ldc_msg_t *pkt; 17961ae08745Sheppo 1797d10e4ef2Snarayan /* Obtain Tx lock */ 1798d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1799d10e4ef2Snarayan 18001ae08745Sheppo /* 1801d10e4ef2Snarayan * Read the current Tx head and tail 18021ae08745Sheppo */ 18031ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 18041ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 18051ae08745Sheppo if (rv != 0) { 18061ae08745Sheppo cmn_err(CE_WARN, 18071ae08745Sheppo "i_ldc_process_data_ACK: (0x%lx) cannot read qptrs\n", 18081ae08745Sheppo ldcp->id); 1809d10e4ef2Snarayan 1810d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 18113af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1812d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1813d10e4ef2Snarayan return (ECONNRESET); 18141ae08745Sheppo } 18151ae08745Sheppo 18161ae08745Sheppo /* 18171ae08745Sheppo * loop from where the previous ACK location was to the 18181ae08745Sheppo * current head location. This is how far the HV has 18191ae08745Sheppo * actually send pkts. Pkts between head and tail are 18201ae08745Sheppo * yet to be sent by HV. 18211ae08745Sheppo */ 18221ae08745Sheppo tx_head = ldcp->tx_ackd_head; 18231ae08745Sheppo for (;;) { 18241ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_head); 18251ae08745Sheppo tx_head = (tx_head + LDC_PACKET_SIZE) % 18261ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 18271ae08745Sheppo 18281ae08745Sheppo if (pkt->seqid == msg->ackid) { 18291ae08745Sheppo D2(ldcp->id, 18301ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) found packet\n", 18311ae08745Sheppo ldcp->id); 18321ae08745Sheppo ldcp->last_ack_rcd = msg->ackid; 18331ae08745Sheppo ldcp->tx_ackd_head = tx_head; 18341ae08745Sheppo break; 18351ae08745Sheppo } 18361ae08745Sheppo if (tx_head == ldcp->tx_head) { 18371ae08745Sheppo /* could not find packet */ 18381ae08745Sheppo DWARN(ldcp->id, 18391ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) invalid ACKid\n", 18401ae08745Sheppo ldcp->id); 1841d10e4ef2Snarayan 1842d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 18433af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1844d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1845d10e4ef2Snarayan return (ECONNRESET); 18461ae08745Sheppo } 18471ae08745Sheppo } 18481ae08745Sheppo 1849d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 18501ae08745Sheppo return (0); 18511ae08745Sheppo } 18521ae08745Sheppo 18531ae08745Sheppo /* 18541ae08745Sheppo * Process incoming control message 18551ae08745Sheppo * Return 0 - session can continue 18561ae08745Sheppo * EAGAIN - reprocess packet - state was changed 18571ae08745Sheppo * ECONNRESET - channel was reset 18581ae08745Sheppo */ 18591ae08745Sheppo static int 18601ae08745Sheppo i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *msg) 18611ae08745Sheppo { 18621ae08745Sheppo int rv = 0; 18631ae08745Sheppo 18643af08d82Slm66018 D1(ldcp->id, "i_ldc_ctrlmsg: (%llx) tstate = %lx, hstate = %lx\n", 18653af08d82Slm66018 ldcp->id, ldcp->tstate, ldcp->hstate); 18663af08d82Slm66018 18673af08d82Slm66018 switch (ldcp->tstate & ~TS_IN_RESET) { 18681ae08745Sheppo 18691ae08745Sheppo case TS_OPEN: 18701ae08745Sheppo case TS_READY: 18711ae08745Sheppo 18721ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 18731ae08745Sheppo case LDC_VER: 18741ae08745Sheppo /* process version message */ 18751ae08745Sheppo rv = i_ldc_process_VER(ldcp, msg); 18761ae08745Sheppo break; 18771ae08745Sheppo default: 18781ae08745Sheppo DWARN(ldcp->id, 18791ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 18801ae08745Sheppo "tstate=0x%x\n", ldcp->id, 18811ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 18821ae08745Sheppo break; 18831ae08745Sheppo } 18841ae08745Sheppo 18851ae08745Sheppo break; 18861ae08745Sheppo 18871ae08745Sheppo case TS_VREADY: 18881ae08745Sheppo 18891ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 18901ae08745Sheppo case LDC_VER: 18913af08d82Slm66018 /* process version message */ 18923af08d82Slm66018 rv = i_ldc_process_VER(ldcp, msg); 18931ae08745Sheppo break; 18941ae08745Sheppo case LDC_RTS: 18951ae08745Sheppo /* process RTS message */ 18961ae08745Sheppo rv = i_ldc_process_RTS(ldcp, msg); 18971ae08745Sheppo break; 18981ae08745Sheppo case LDC_RTR: 18991ae08745Sheppo /* process RTR message */ 19001ae08745Sheppo rv = i_ldc_process_RTR(ldcp, msg); 19011ae08745Sheppo break; 19021ae08745Sheppo case LDC_RDX: 19031ae08745Sheppo /* process RDX message */ 19041ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 19051ae08745Sheppo break; 19061ae08745Sheppo default: 19071ae08745Sheppo DWARN(ldcp->id, 19081ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 19091ae08745Sheppo "tstate=0x%x\n", ldcp->id, 19101ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 19111ae08745Sheppo break; 19121ae08745Sheppo } 19131ae08745Sheppo 19141ae08745Sheppo break; 19151ae08745Sheppo 19161ae08745Sheppo case TS_UP: 19171ae08745Sheppo 19181ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 19191ae08745Sheppo case LDC_VER: 19201ae08745Sheppo DWARN(ldcp->id, 19211ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexpected VER " 19221ae08745Sheppo "- LDC reset\n", ldcp->id); 19231ae08745Sheppo /* peer is redoing version negotiation */ 1924d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 19251ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 19261ae08745Sheppo i_ldc_reset_state(ldcp); 1927d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 19281ae08745Sheppo rv = EAGAIN; 19291ae08745Sheppo break; 19301ae08745Sheppo 19311ae08745Sheppo case LDC_RDX: 19321ae08745Sheppo /* process RDX message */ 19331ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 19341ae08745Sheppo break; 19351ae08745Sheppo 19361ae08745Sheppo default: 19371ae08745Sheppo DWARN(ldcp->id, 19381ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 19391ae08745Sheppo "tstate=0x%x\n", ldcp->id, 19401ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 19411ae08745Sheppo break; 19421ae08745Sheppo } 19431ae08745Sheppo } 19441ae08745Sheppo 19451ae08745Sheppo return (rv); 19461ae08745Sheppo } 19471ae08745Sheppo 19481ae08745Sheppo /* 19491ae08745Sheppo * Register channel with the channel nexus 19501ae08745Sheppo */ 19511ae08745Sheppo static int 19521ae08745Sheppo i_ldc_register_channel(ldc_chan_t *ldcp) 19531ae08745Sheppo { 19541ae08745Sheppo int rv = 0; 19551ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 19561ae08745Sheppo 19571ae08745Sheppo if (cinfo->dip == NULL) { 19581ae08745Sheppo DWARN(ldcp->id, 19591ae08745Sheppo "i_ldc_register_channel: cnex has not registered\n"); 19601ae08745Sheppo return (EAGAIN); 19611ae08745Sheppo } 19621ae08745Sheppo 19631ae08745Sheppo rv = cinfo->reg_chan(cinfo->dip, ldcp->id, ldcp->devclass); 19641ae08745Sheppo if (rv) { 19651ae08745Sheppo DWARN(ldcp->id, 19661ae08745Sheppo "i_ldc_register_channel: cannot register channel\n"); 19671ae08745Sheppo return (rv); 19681ae08745Sheppo } 19691ae08745Sheppo 19701ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR, 19711ae08745Sheppo i_ldc_tx_hdlr, ldcp, NULL); 19721ae08745Sheppo if (rv) { 19731ae08745Sheppo DWARN(ldcp->id, 19741ae08745Sheppo "i_ldc_register_channel: cannot add Tx interrupt\n"); 19751ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 19761ae08745Sheppo return (rv); 19771ae08745Sheppo } 19781ae08745Sheppo 19791ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR, 19801ae08745Sheppo i_ldc_rx_hdlr, ldcp, NULL); 19811ae08745Sheppo if (rv) { 19821ae08745Sheppo DWARN(ldcp->id, 19831ae08745Sheppo "i_ldc_register_channel: cannot add Rx interrupt\n"); 19841ae08745Sheppo (void) cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 19851ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 19861ae08745Sheppo return (rv); 19871ae08745Sheppo } 19881ae08745Sheppo 19891ae08745Sheppo ldcp->tstate |= TS_CNEX_RDY; 19901ae08745Sheppo 19911ae08745Sheppo return (0); 19921ae08745Sheppo } 19931ae08745Sheppo 19941ae08745Sheppo /* 19951ae08745Sheppo * Unregister a channel with the channel nexus 19961ae08745Sheppo */ 19971ae08745Sheppo static int 19981ae08745Sheppo i_ldc_unregister_channel(ldc_chan_t *ldcp) 19991ae08745Sheppo { 20001ae08745Sheppo int rv = 0; 20011ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 20021ae08745Sheppo 20031ae08745Sheppo if (cinfo->dip == NULL) { 20041ae08745Sheppo DWARN(ldcp->id, 20051ae08745Sheppo "i_ldc_unregister_channel: cnex has not registered\n"); 20061ae08745Sheppo return (EAGAIN); 20071ae08745Sheppo } 20081ae08745Sheppo 20091ae08745Sheppo if (ldcp->tstate & TS_CNEX_RDY) { 20101ae08745Sheppo 2011d10e4ef2Snarayan /* Remove the Rx interrupt */ 20121ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR); 20131ae08745Sheppo if (rv) { 20143af08d82Slm66018 if (rv != EAGAIN) { 20151ae08745Sheppo DWARN(ldcp->id, 20163af08d82Slm66018 "i_ldc_unregister_channel: err removing " 20173af08d82Slm66018 "Rx intr\n"); 2018d10e4ef2Snarayan return (rv); 20191ae08745Sheppo } 2020d10e4ef2Snarayan 20213af08d82Slm66018 /* 20223af08d82Slm66018 * If interrupts are pending and handler has 20233af08d82Slm66018 * finished running, clear interrupt and try 20243af08d82Slm66018 * again 20253af08d82Slm66018 */ 20263af08d82Slm66018 if (ldcp->rx_intr_state != LDC_INTR_PEND) 20273af08d82Slm66018 return (rv); 20283af08d82Slm66018 20293af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 20303af08d82Slm66018 rv = cinfo->rem_intr(cinfo->dip, ldcp->id, 20313af08d82Slm66018 CNEX_RX_INTR); 20323af08d82Slm66018 if (rv) { 20333af08d82Slm66018 DWARN(ldcp->id, "i_ldc_unregister_channel: " 20343af08d82Slm66018 "err removing Rx interrupt\n"); 20353af08d82Slm66018 return (rv); 20363af08d82Slm66018 } 20373af08d82Slm66018 } 20383af08d82Slm66018 2039d10e4ef2Snarayan /* Remove the Tx interrupt */ 20401ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 20411ae08745Sheppo if (rv) { 20421ae08745Sheppo DWARN(ldcp->id, 20431ae08745Sheppo "i_ldc_unregister_channel: err removing Tx intr\n"); 2044d10e4ef2Snarayan return (rv); 20451ae08745Sheppo } 2046d10e4ef2Snarayan 2047d10e4ef2Snarayan /* Unregister the channel */ 20481ae08745Sheppo rv = cinfo->unreg_chan(ldcssp->cinfo.dip, ldcp->id); 20491ae08745Sheppo if (rv) { 20501ae08745Sheppo DWARN(ldcp->id, 20511ae08745Sheppo "i_ldc_unregister_channel: cannot unreg channel\n"); 2052d10e4ef2Snarayan return (rv); 20531ae08745Sheppo } 20541ae08745Sheppo 20551ae08745Sheppo ldcp->tstate &= ~TS_CNEX_RDY; 20561ae08745Sheppo } 20571ae08745Sheppo 20581ae08745Sheppo return (0); 20591ae08745Sheppo } 20601ae08745Sheppo 20611ae08745Sheppo 20621ae08745Sheppo /* 20631ae08745Sheppo * LDC transmit interrupt handler 20641ae08745Sheppo * triggered for chanel up/down/reset events 20651ae08745Sheppo * and Tx queue content changes 20661ae08745Sheppo */ 20671ae08745Sheppo static uint_t 20681ae08745Sheppo i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2) 20691ae08745Sheppo { 20701ae08745Sheppo _NOTE(ARGUNUSED(arg2)) 20711ae08745Sheppo 20721ae08745Sheppo int rv; 20731ae08745Sheppo ldc_chan_t *ldcp; 20741ae08745Sheppo boolean_t notify_client = B_FALSE; 20753af08d82Slm66018 uint64_t notify_event = 0, link_state; 20761ae08745Sheppo 20771ae08745Sheppo /* Get the channel for which interrupt was received */ 20781ae08745Sheppo ASSERT(arg1 != NULL); 20791ae08745Sheppo ldcp = (ldc_chan_t *)arg1; 20801ae08745Sheppo 20811ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 20821ae08745Sheppo ldcp->id, ldcp); 20831ae08745Sheppo 20841ae08745Sheppo /* Lock channel */ 20851ae08745Sheppo mutex_enter(&ldcp->lock); 20861ae08745Sheppo 2087d10e4ef2Snarayan /* Obtain Tx lock */ 2088d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2089d10e4ef2Snarayan 20904bac2208Snarayan /* mark interrupt as pending */ 20913af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_ACTIVE; 20923af08d82Slm66018 20933af08d82Slm66018 /* save current link state */ 20943af08d82Slm66018 link_state = ldcp->link_state; 20954bac2208Snarayan 20961ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &ldcp->tx_head, &ldcp->tx_tail, 20971ae08745Sheppo &ldcp->link_state); 20981ae08745Sheppo if (rv) { 20991ae08745Sheppo cmn_err(CE_WARN, 21001ae08745Sheppo "i_ldc_tx_hdlr: (0x%lx) cannot read queue ptrs rv=0x%d\n", 21011ae08745Sheppo ldcp->id, rv); 21024bac2208Snarayan i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 2103d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 21041ae08745Sheppo mutex_exit(&ldcp->lock); 21051ae08745Sheppo return (DDI_INTR_CLAIMED); 21061ae08745Sheppo } 21071ae08745Sheppo 21081ae08745Sheppo /* 21091ae08745Sheppo * reset the channel state if the channel went down 21101ae08745Sheppo * (other side unconfigured queue) or channel was reset 21111ae08745Sheppo * (other side reconfigured its queue) 21121ae08745Sheppo */ 21133af08d82Slm66018 if (link_state != ldcp->link_state && 21143af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_DOWN) { 21151ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link down\n", ldcp->id); 21163af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 21171ae08745Sheppo notify_client = B_TRUE; 21181ae08745Sheppo notify_event = LDC_EVT_DOWN; 21191ae08745Sheppo } 21201ae08745Sheppo 21213af08d82Slm66018 if (link_state != ldcp->link_state && 21223af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 21231ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link reset\n", ldcp->id); 21243af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 21251ae08745Sheppo notify_client = B_TRUE; 21261ae08745Sheppo notify_event = LDC_EVT_RESET; 21271ae08745Sheppo } 21281ae08745Sheppo 21293af08d82Slm66018 if (link_state != ldcp->link_state && 21303af08d82Slm66018 (ldcp->tstate & ~TS_IN_RESET) == TS_OPEN && 21313af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_UP) { 21321ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link up\n", ldcp->id); 21331ae08745Sheppo notify_client = B_TRUE; 21341ae08745Sheppo notify_event = LDC_EVT_RESET; 21351ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 21361ae08745Sheppo ldcp->status = LDC_READY; 21371ae08745Sheppo } 21381ae08745Sheppo 21391ae08745Sheppo /* if callbacks are disabled, do not notify */ 21401ae08745Sheppo if (!ldcp->cb_enabled) 21411ae08745Sheppo notify_client = B_FALSE; 21421ae08745Sheppo 21434d39be2bSsg70180 i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 214422f747efSnarayan mutex_exit(&ldcp->tx_lock); 21451ae08745Sheppo 21461ae08745Sheppo if (notify_client) { 21473af08d82Slm66018 ldcp->cb_inprogress = B_TRUE; 21483af08d82Slm66018 mutex_exit(&ldcp->lock); 21491ae08745Sheppo rv = ldcp->cb(notify_event, ldcp->cb_arg); 21501ae08745Sheppo if (rv) { 21511ae08745Sheppo DWARN(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) callback " 21521ae08745Sheppo "failure", ldcp->id); 21531ae08745Sheppo } 21541ae08745Sheppo mutex_enter(&ldcp->lock); 21551ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 21561ae08745Sheppo } 21571ae08745Sheppo 21581ae08745Sheppo mutex_exit(&ldcp->lock); 21591ae08745Sheppo 21601ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) exiting handler", ldcp->id); 21611ae08745Sheppo 21621ae08745Sheppo return (DDI_INTR_CLAIMED); 21631ae08745Sheppo } 21641ae08745Sheppo 21651ae08745Sheppo /* 216658283286Sha137994 * Process the Rx HV queue. 216758283286Sha137994 * 216858283286Sha137994 * Returns 0 if data packets were found and no errors were encountered, 216958283286Sha137994 * otherwise returns an error. In either case, the *notify argument is 217058283286Sha137994 * set to indicate whether or not the client callback function should 217158283286Sha137994 * be invoked. The *event argument is set to contain the callback event. 217258283286Sha137994 * 217358283286Sha137994 * Depending on the channel mode, packets are handled differently: 217458283286Sha137994 * 217558283286Sha137994 * RAW MODE 217658283286Sha137994 * For raw mode channels, when a data packet is encountered, 217758283286Sha137994 * processing stops and all packets are left on the queue to be removed 217858283286Sha137994 * and processed by the ldc_read code path. 217958283286Sha137994 * 218058283286Sha137994 * UNRELIABLE MODE 218158283286Sha137994 * For unreliable mode, when a data packet is encountered, processing 218258283286Sha137994 * stops, and all packets are left on the queue to be removed and 218358283286Sha137994 * processed by the ldc_read code path. Control packets are processed 218458283286Sha137994 * inline if they are encountered before any data packets. 218558283286Sha137994 * 218620ae46ebSha137994 * RELIABLE MODE 218720ae46ebSha137994 * For reliable mode channels, all packets on the receive queue 218858283286Sha137994 * are processed: data packets are copied to the data queue and 218958283286Sha137994 * control packets are processed inline. Packets are only left on 219058283286Sha137994 * the receive queue when the data queue is full. 21911ae08745Sheppo */ 21921ae08745Sheppo static uint_t 219358283286Sha137994 i_ldc_rx_process_hvq(ldc_chan_t *ldcp, boolean_t *notify_client, 219458283286Sha137994 uint64_t *notify_event) 21951ae08745Sheppo { 21961ae08745Sheppo int rv; 21971ae08745Sheppo uint64_t rx_head, rx_tail; 21981ae08745Sheppo ldc_msg_t *msg; 21993af08d82Slm66018 uint64_t link_state, first_fragment = 0; 220058283286Sha137994 boolean_t trace_length = B_TRUE; 22013af08d82Slm66018 220258283286Sha137994 ASSERT(MUTEX_HELD(&ldcp->lock)); 220358283286Sha137994 *notify_client = B_FALSE; 220458283286Sha137994 *notify_event = 0; 22051ae08745Sheppo 22061ae08745Sheppo /* 22071ae08745Sheppo * Read packet(s) from the queue 22081ae08745Sheppo */ 22091ae08745Sheppo for (;;) { 22101ae08745Sheppo 22113af08d82Slm66018 link_state = ldcp->link_state; 22121ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 22131ae08745Sheppo &ldcp->link_state); 22141ae08745Sheppo if (rv) { 22151ae08745Sheppo cmn_err(CE_WARN, 221658283286Sha137994 "i_ldc_rx_process_hvq: (0x%lx) cannot read " 22171ae08745Sheppo "queue ptrs, rv=0x%d\n", ldcp->id, rv); 22181ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 221958283286Sha137994 return (EIO); 22201ae08745Sheppo } 22211ae08745Sheppo 22221ae08745Sheppo /* 22231ae08745Sheppo * reset the channel state if the channel went down 22241ae08745Sheppo * (other side unconfigured queue) or channel was reset 22253af08d82Slm66018 * (other side reconfigured its queue) 22261ae08745Sheppo */ 22273af08d82Slm66018 22283af08d82Slm66018 if (link_state != ldcp->link_state) { 2229cb112a14Slm66018 22303af08d82Slm66018 switch (ldcp->link_state) { 22313af08d82Slm66018 case LDC_CHANNEL_DOWN: 223258283286Sha137994 D1(ldcp->id, "i_ldc_rx_process_hvq: channel " 22333af08d82Slm66018 "link down\n", ldcp->id); 2234d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 22353af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 2236d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 223758283286Sha137994 *notify_client = B_TRUE; 223858283286Sha137994 *notify_event = LDC_EVT_DOWN; 22393af08d82Slm66018 goto loop_exit; 22401ae08745Sheppo 22413af08d82Slm66018 case LDC_CHANNEL_UP: 224258283286Sha137994 D1(ldcp->id, "i_ldc_rx_process_hvq: " 22433af08d82Slm66018 "channel link up\n", ldcp->id); 22443af08d82Slm66018 22453af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_OPEN) { 224658283286Sha137994 *notify_client = B_TRUE; 224758283286Sha137994 *notify_event = LDC_EVT_RESET; 22481ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 22491ae08745Sheppo ldcp->status = LDC_READY; 22501ae08745Sheppo } 22513af08d82Slm66018 break; 22523af08d82Slm66018 22533af08d82Slm66018 case LDC_CHANNEL_RESET: 22543af08d82Slm66018 default: 22553af08d82Slm66018 #ifdef DEBUG 22563af08d82Slm66018 force_reset: 22573af08d82Slm66018 #endif 225858283286Sha137994 D1(ldcp->id, "i_ldc_rx_process_hvq: channel " 22593af08d82Slm66018 "link reset\n", ldcp->id); 22603af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 22613af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 22623af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 226358283286Sha137994 *notify_client = B_TRUE; 226458283286Sha137994 *notify_event = LDC_EVT_RESET; 22653af08d82Slm66018 break; 22663af08d82Slm66018 } 22673af08d82Slm66018 } 22683af08d82Slm66018 22693af08d82Slm66018 #ifdef DEBUG 22703af08d82Slm66018 if (LDC_INJECT_RESET(ldcp)) 22713af08d82Slm66018 goto force_reset; 2272bbfa0259Sha137994 if (LDC_INJECT_DRNGCLEAR(ldcp)) 2273bbfa0259Sha137994 i_ldc_mem_inject_dring_clear(ldcp); 22743af08d82Slm66018 #endif 227558283286Sha137994 if (trace_length) { 227658283286Sha137994 TRACE_RXHVQ_LENGTH(ldcp, rx_head, rx_tail); 227758283286Sha137994 trace_length = B_FALSE; 227858283286Sha137994 } 22791ae08745Sheppo 22801ae08745Sheppo if (rx_head == rx_tail) { 228158283286Sha137994 D2(ldcp->id, "i_ldc_rx_process_hvq: (0x%llx) " 228258283286Sha137994 "No packets\n", ldcp->id); 22831ae08745Sheppo break; 22841ae08745Sheppo } 22853af08d82Slm66018 228658283286Sha137994 D2(ldcp->id, "i_ldc_rx_process_hvq: head=0x%llx, " 228758283286Sha137994 "tail=0x%llx\n", rx_head, rx_tail); 228858283286Sha137994 DUMP_LDC_PKT(ldcp, "i_ldc_rx_process_hvq rcd", 22891ae08745Sheppo ldcp->rx_q_va + rx_head); 22901ae08745Sheppo 22911ae08745Sheppo /* get the message */ 22921ae08745Sheppo msg = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 22931ae08745Sheppo 22941ae08745Sheppo /* if channel is in RAW mode or data pkt, notify and return */ 22951ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 229658283286Sha137994 *notify_client = B_TRUE; 229758283286Sha137994 *notify_event |= LDC_EVT_READ; 22981ae08745Sheppo break; 22991ae08745Sheppo } 23001ae08745Sheppo 23011ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 23021ae08745Sheppo 23031ae08745Sheppo /* discard packet if channel is not up */ 23043af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) != TS_UP) { 23051ae08745Sheppo 23061ae08745Sheppo /* move the head one position */ 23071ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 23081ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 23091ae08745Sheppo 23101ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, rx_head)) 23111ae08745Sheppo break; 23121ae08745Sheppo 23131ae08745Sheppo continue; 23141ae08745Sheppo } else { 231558283286Sha137994 uint64_t dq_head, dq_tail; 231658283286Sha137994 231720ae46ebSha137994 /* process only RELIABLE mode data packets */ 231820ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) { 23193af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 232058283286Sha137994 *notify_client = B_TRUE; 232158283286Sha137994 *notify_event |= LDC_EVT_READ; 23221ae08745Sheppo break; 23231ae08745Sheppo } 232458283286Sha137994 232558283286Sha137994 /* don't process packet if queue full */ 232658283286Sha137994 (void) i_ldc_dq_rx_get_state(ldcp, &dq_head, 232758283286Sha137994 &dq_tail, NULL); 232858283286Sha137994 dq_tail = (dq_tail + LDC_PACKET_SIZE) % 232958283286Sha137994 (ldcp->rx_dq_entries << LDC_PACKET_SHIFT); 233058283286Sha137994 if (dq_tail == dq_head || 233158283286Sha137994 LDC_INJECT_DQFULL(ldcp)) { 233258283286Sha137994 rv = ENOSPC; 233358283286Sha137994 break; 233458283286Sha137994 } 233558283286Sha137994 } 23361ae08745Sheppo } 23371ae08745Sheppo 23381ae08745Sheppo /* Check the sequence ID for the message received */ 23393af08d82Slm66018 rv = i_ldc_check_seqid(ldcp, msg); 23403af08d82Slm66018 if (rv != 0) { 23411ae08745Sheppo 234258283286Sha137994 DWARN(ldcp->id, "i_ldc_rx_process_hvq: (0x%llx) " 234358283286Sha137994 "seqid error, q_ptrs=0x%lx,0x%lx", ldcp->id, 234458283286Sha137994 rx_head, rx_tail); 23451ae08745Sheppo 23461ae08745Sheppo /* Reset last_msg_rcd to start of message */ 2347d10e4ef2Snarayan if (first_fragment != 0) { 2348d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 2349d10e4ef2Snarayan first_fragment = 0; 23501ae08745Sheppo } 2351d10e4ef2Snarayan 23521ae08745Sheppo /* 23531ae08745Sheppo * Send a NACK due to seqid mismatch 23541ae08745Sheppo */ 235522f747efSnarayan rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK, 23561ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 23571ae08745Sheppo 23581ae08745Sheppo if (rv) { 235958283286Sha137994 cmn_err(CE_NOTE, "i_ldc_rx_process_hvq: " 236058283286Sha137994 "(0x%lx) err sending CTRL/DATA NACK msg\n", 236158283286Sha137994 ldcp->id); 2362d10e4ef2Snarayan 2363d10e4ef2Snarayan /* if cannot send NACK - reset channel */ 2364d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 23653af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 2366d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 236783d3bc6fSnarayan 236858283286Sha137994 *notify_client = B_TRUE; 236958283286Sha137994 *notify_event = LDC_EVT_RESET; 2370d10e4ef2Snarayan break; 23711ae08745Sheppo } 23721ae08745Sheppo 23731ae08745Sheppo /* purge receive queue */ 23741ae08745Sheppo (void) i_ldc_set_rx_head(ldcp, rx_tail); 23751ae08745Sheppo break; 23761ae08745Sheppo } 23771ae08745Sheppo 23781ae08745Sheppo /* record the message ID */ 23791ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 23801ae08745Sheppo 23811ae08745Sheppo /* process control messages */ 23821ae08745Sheppo if (msg->type & LDC_CTRL) { 23831ae08745Sheppo /* save current internal state */ 23841ae08745Sheppo uint64_t tstate = ldcp->tstate; 23851ae08745Sheppo 23861ae08745Sheppo rv = i_ldc_ctrlmsg(ldcp, msg); 23871ae08745Sheppo if (rv == EAGAIN) { 23881ae08745Sheppo /* re-process pkt - state was adjusted */ 23891ae08745Sheppo continue; 23901ae08745Sheppo } 23911ae08745Sheppo if (rv == ECONNRESET) { 239258283286Sha137994 *notify_client = B_TRUE; 239358283286Sha137994 *notify_event = LDC_EVT_RESET; 23941ae08745Sheppo break; 23951ae08745Sheppo } 23961ae08745Sheppo 23971ae08745Sheppo /* 23981ae08745Sheppo * control message processing was successful 23991ae08745Sheppo * channel transitioned to ready for communication 24001ae08745Sheppo */ 24011ae08745Sheppo if (rv == 0 && ldcp->tstate == TS_UP && 24023af08d82Slm66018 (tstate & ~TS_IN_RESET) != 24033af08d82Slm66018 (ldcp->tstate & ~TS_IN_RESET)) { 240458283286Sha137994 *notify_client = B_TRUE; 240558283286Sha137994 *notify_event = LDC_EVT_UP; 24061ae08745Sheppo } 24071ae08745Sheppo } 24081ae08745Sheppo 240983d3bc6fSnarayan /* process data NACKs */ 241083d3bc6fSnarayan if ((msg->type & LDC_DATA) && (msg->stype & LDC_NACK)) { 241183d3bc6fSnarayan DWARN(ldcp->id, 241258283286Sha137994 "i_ldc_rx_process_hvq: (0x%llx) received DATA/NACK", 241383d3bc6fSnarayan ldcp->id); 241483d3bc6fSnarayan mutex_enter(&ldcp->tx_lock); 241583d3bc6fSnarayan i_ldc_reset(ldcp, B_TRUE); 241683d3bc6fSnarayan mutex_exit(&ldcp->tx_lock); 241758283286Sha137994 *notify_client = B_TRUE; 241858283286Sha137994 *notify_event = LDC_EVT_RESET; 241983d3bc6fSnarayan break; 242083d3bc6fSnarayan } 242183d3bc6fSnarayan 24221ae08745Sheppo /* process data ACKs */ 24231ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 2424d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 242558283286Sha137994 *notify_client = B_TRUE; 242658283286Sha137994 *notify_event = LDC_EVT_RESET; 2427d10e4ef2Snarayan break; 2428d10e4ef2Snarayan } 24291ae08745Sheppo } 24301ae08745Sheppo 243158283286Sha137994 if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 243220ae46ebSha137994 ASSERT(ldcp->mode == LDC_MODE_RELIABLE); 243358283286Sha137994 243458283286Sha137994 /* 243558283286Sha137994 * Copy the data packet to the data queue. Note 243658283286Sha137994 * that the copy routine updates the rx_head pointer. 243758283286Sha137994 */ 243858283286Sha137994 i_ldc_rxdq_copy(ldcp, &rx_head); 243958283286Sha137994 244058283286Sha137994 if ((ldcp->tstate & TS_IN_RESET) == 0) 244158283286Sha137994 *notify_client = B_TRUE; 244258283286Sha137994 *notify_event |= LDC_EVT_READ; 244358283286Sha137994 } else { 24441ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 24451ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 244658283286Sha137994 } 244758283286Sha137994 244858283286Sha137994 /* move the head one position */ 24490a55fbb7Slm66018 if (rv = i_ldc_set_rx_head(ldcp, rx_head)) { 245058283286Sha137994 *notify_client = B_TRUE; 245158283286Sha137994 *notify_event = LDC_EVT_RESET; 24521ae08745Sheppo break; 24530a55fbb7Slm66018 } 24541ae08745Sheppo 24551ae08745Sheppo } /* for */ 24561ae08745Sheppo 24573af08d82Slm66018 loop_exit: 24583af08d82Slm66018 245920ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 246058283286Sha137994 /* ACK data packets */ 246158283286Sha137994 if ((*notify_event & 246258283286Sha137994 (LDC_EVT_READ | LDC_EVT_RESET)) == LDC_EVT_READ) { 246358283286Sha137994 int ack_rv; 246458283286Sha137994 ack_rv = i_ldc_send_pkt(ldcp, LDC_DATA, LDC_ACK, 0); 246558283286Sha137994 if (ack_rv && ack_rv != EWOULDBLOCK) { 246658283286Sha137994 cmn_err(CE_NOTE, 246758283286Sha137994 "i_ldc_rx_process_hvq: (0x%lx) cannot " 246858283286Sha137994 "send ACK\n", ldcp->id); 246958283286Sha137994 247058283286Sha137994 mutex_enter(&ldcp->tx_lock); 247158283286Sha137994 i_ldc_reset(ldcp, B_FALSE); 247258283286Sha137994 mutex_exit(&ldcp->tx_lock); 247358283286Sha137994 247458283286Sha137994 *notify_client = B_TRUE; 247558283286Sha137994 *notify_event = LDC_EVT_RESET; 247658283286Sha137994 goto skip_ackpeek; 247758283286Sha137994 } 247858283286Sha137994 } 24791ae08745Sheppo 24803af08d82Slm66018 /* 248158283286Sha137994 * If we have no more space on the data queue, make sure 248258283286Sha137994 * there are no ACKs on the rx queue waiting to be processed. 24833af08d82Slm66018 */ 248458283286Sha137994 if (rv == ENOSPC) { 248558283286Sha137994 if (i_ldc_rx_ackpeek(ldcp, rx_head, rx_tail) != 0) { 248658283286Sha137994 ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID; 248758283286Sha137994 *notify_client = B_TRUE; 248858283286Sha137994 *notify_event = LDC_EVT_RESET; 24891ae08745Sheppo } 249012f80fa6Sha137994 return (rv); 249158283286Sha137994 } else { 249258283286Sha137994 ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID; 249358283286Sha137994 } 24941ae08745Sheppo } 24951ae08745Sheppo 249658283286Sha137994 skip_ackpeek: 24974d39be2bSsg70180 249858283286Sha137994 /* Return, indicating whether or not data packets were found */ 249958283286Sha137994 if ((*notify_event & (LDC_EVT_READ | LDC_EVT_RESET)) == LDC_EVT_READ) 250058283286Sha137994 return (0); 250158283286Sha137994 250258283286Sha137994 return (ENOMSG); 25031ae08745Sheppo } 25041ae08745Sheppo 250558283286Sha137994 /* 250658283286Sha137994 * Process any ACK packets on the HV receive queue. 250758283286Sha137994 * 250820ae46ebSha137994 * This function is only used by RELIABLE mode channels when the 250958283286Sha137994 * secondary data queue fills up and there are packets remaining on 251058283286Sha137994 * the HV receive queue. 251158283286Sha137994 */ 251258283286Sha137994 int 251358283286Sha137994 i_ldc_rx_ackpeek(ldc_chan_t *ldcp, uint64_t rx_head, uint64_t rx_tail) 251458283286Sha137994 { 251558283286Sha137994 int rv = 0; 251658283286Sha137994 ldc_msg_t *msg; 251758283286Sha137994 251858283286Sha137994 if (ldcp->rx_ack_head == ACKPEEK_HEAD_INVALID) 251958283286Sha137994 ldcp->rx_ack_head = rx_head; 252058283286Sha137994 252158283286Sha137994 while (ldcp->rx_ack_head != rx_tail) { 252258283286Sha137994 msg = (ldc_msg_t *)(ldcp->rx_q_va + ldcp->rx_ack_head); 252358283286Sha137994 252458283286Sha137994 if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 252558283286Sha137994 if (rv = i_ldc_process_data_ACK(ldcp, msg)) 252658283286Sha137994 break; 252758283286Sha137994 msg->stype &= ~LDC_ACK; 252858283286Sha137994 } 252958283286Sha137994 253058283286Sha137994 ldcp->rx_ack_head = 253158283286Sha137994 (ldcp->rx_ack_head + LDC_PACKET_SIZE) % 253258283286Sha137994 (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 253358283286Sha137994 } 253458283286Sha137994 return (rv); 253558283286Sha137994 } 25361ae08745Sheppo 25371ae08745Sheppo /* -------------------------------------------------------------------------- */ 25381ae08745Sheppo 25391ae08745Sheppo /* 25401ae08745Sheppo * LDC API functions 25411ae08745Sheppo */ 25421ae08745Sheppo 25431ae08745Sheppo /* 25441ae08745Sheppo * Initialize the channel. Allocate internal structure and memory for 25451ae08745Sheppo * TX/RX queues, and initialize locks. 25461ae08745Sheppo */ 25471ae08745Sheppo int 25481ae08745Sheppo ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle) 25491ae08745Sheppo { 25501ae08745Sheppo ldc_chan_t *ldcp; 25511ae08745Sheppo int rv, exit_val; 25521ae08745Sheppo uint64_t ra_base, nentries; 2553e1ebb9ecSlm66018 uint64_t qlen; 25541ae08745Sheppo 25551ae08745Sheppo exit_val = EINVAL; /* guarantee an error if exit on failure */ 25561ae08745Sheppo 25571ae08745Sheppo if (attr == NULL) { 25581ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid attr\n", id); 25591ae08745Sheppo return (EINVAL); 25601ae08745Sheppo } 25611ae08745Sheppo if (handle == NULL) { 25621ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid handle\n", id); 25631ae08745Sheppo return (EINVAL); 25641ae08745Sheppo } 25651ae08745Sheppo 25661ae08745Sheppo /* check if channel is valid */ 25671ae08745Sheppo rv = hv_ldc_tx_qinfo(id, &ra_base, &nentries); 25681ae08745Sheppo if (rv == H_ECHANNEL) { 25691ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid channel id\n", id); 25701ae08745Sheppo return (EINVAL); 25711ae08745Sheppo } 25721ae08745Sheppo 25731ae08745Sheppo /* check if the channel has already been initialized */ 25741ae08745Sheppo mutex_enter(&ldcssp->lock); 25751ae08745Sheppo ldcp = ldcssp->chan_list; 25761ae08745Sheppo while (ldcp != NULL) { 25771ae08745Sheppo if (ldcp->id == id) { 25781ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) already initialized\n", 25791ae08745Sheppo id); 25801ae08745Sheppo mutex_exit(&ldcssp->lock); 25811ae08745Sheppo return (EADDRINUSE); 25821ae08745Sheppo } 25831ae08745Sheppo ldcp = ldcp->next; 25841ae08745Sheppo } 25851ae08745Sheppo mutex_exit(&ldcssp->lock); 25861ae08745Sheppo 25871ae08745Sheppo ASSERT(ldcp == NULL); 25881ae08745Sheppo 25891ae08745Sheppo *handle = 0; 25901ae08745Sheppo 25911ae08745Sheppo /* Allocate an ldcp structure */ 25921ae08745Sheppo ldcp = kmem_zalloc(sizeof (ldc_chan_t), KM_SLEEP); 25931ae08745Sheppo 2594d10e4ef2Snarayan /* 2595d10e4ef2Snarayan * Initialize the channel and Tx lock 2596d10e4ef2Snarayan * 2597d10e4ef2Snarayan * The channel 'lock' protects the entire channel and 2598d10e4ef2Snarayan * should be acquired before initializing, resetting, 2599d10e4ef2Snarayan * destroying or reading from a channel. 2600d10e4ef2Snarayan * 2601d10e4ef2Snarayan * The 'tx_lock' should be acquired prior to transmitting 2602d10e4ef2Snarayan * data over the channel. The lock should also be acquired 2603d10e4ef2Snarayan * prior to channel reconfiguration (in order to prevent 2604d10e4ef2Snarayan * concurrent writes). 2605d10e4ef2Snarayan * 2606d10e4ef2Snarayan * ORDERING: When both locks are being acquired, to prevent 2607d10e4ef2Snarayan * deadlocks, the channel lock should be always acquired prior 2608d10e4ef2Snarayan * to the tx_lock. 2609d10e4ef2Snarayan */ 26101ae08745Sheppo mutex_init(&ldcp->lock, NULL, MUTEX_DRIVER, NULL); 2611d10e4ef2Snarayan mutex_init(&ldcp->tx_lock, NULL, MUTEX_DRIVER, NULL); 26121ae08745Sheppo 26131ae08745Sheppo /* Initialize the channel */ 26141ae08745Sheppo ldcp->id = id; 26151ae08745Sheppo ldcp->cb = NULL; 26161ae08745Sheppo ldcp->cb_arg = NULL; 26171ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 26181ae08745Sheppo ldcp->cb_enabled = B_FALSE; 26191ae08745Sheppo ldcp->next = NULL; 26201ae08745Sheppo 26211ae08745Sheppo /* Read attributes */ 26221ae08745Sheppo ldcp->mode = attr->mode; 26231ae08745Sheppo ldcp->devclass = attr->devclass; 26241ae08745Sheppo ldcp->devinst = attr->instance; 2625e1ebb9ecSlm66018 ldcp->mtu = (attr->mtu > 0) ? attr->mtu : LDC_DEFAULT_MTU; 26261ae08745Sheppo 26271ae08745Sheppo D1(ldcp->id, 26281ae08745Sheppo "ldc_init: (0x%llx) channel attributes, class=0x%x, " 2629e1ebb9ecSlm66018 "instance=0x%llx, mode=%d, mtu=%d\n", 2630e1ebb9ecSlm66018 ldcp->id, ldcp->devclass, ldcp->devinst, ldcp->mode, ldcp->mtu); 26311ae08745Sheppo 26321ae08745Sheppo ldcp->next_vidx = 0; 26333af08d82Slm66018 ldcp->tstate = TS_IN_RESET; 26341ae08745Sheppo ldcp->hstate = 0; 26351ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 26361ae08745Sheppo ldcp->last_ack_rcd = 0; 26371ae08745Sheppo ldcp->last_msg_rcd = 0; 263858283286Sha137994 ldcp->rx_ack_head = ACKPEEK_HEAD_INVALID; 26391ae08745Sheppo 26401ae08745Sheppo ldcp->stream_bufferp = NULL; 26411ae08745Sheppo ldcp->exp_dring_list = NULL; 26421ae08745Sheppo ldcp->imp_dring_list = NULL; 26431ae08745Sheppo ldcp->mhdl_list = NULL; 26441ae08745Sheppo 26453af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_NONE; 26463af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_NONE; 26473af08d82Slm66018 26481ae08745Sheppo /* Initialize payload size depending on whether channel is reliable */ 26491ae08745Sheppo switch (ldcp->mode) { 26501ae08745Sheppo case LDC_MODE_RAW: 26511ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RAW; 26521ae08745Sheppo ldcp->read_p = i_ldc_read_raw; 26531ae08745Sheppo ldcp->write_p = i_ldc_write_raw; 26541ae08745Sheppo break; 26551ae08745Sheppo case LDC_MODE_UNRELIABLE: 26561ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_UNRELIABLE; 26571ae08745Sheppo ldcp->read_p = i_ldc_read_packet; 26581ae08745Sheppo ldcp->write_p = i_ldc_write_packet; 26591ae08745Sheppo break; 26601ae08745Sheppo case LDC_MODE_RELIABLE: 26611ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE; 26621ae08745Sheppo 26631ae08745Sheppo ldcp->stream_remains = 0; 26641ae08745Sheppo ldcp->stream_offset = 0; 26651ae08745Sheppo ldcp->stream_bufferp = kmem_alloc(ldcp->mtu, KM_SLEEP); 26661ae08745Sheppo ldcp->read_p = i_ldc_read_stream; 26671ae08745Sheppo ldcp->write_p = i_ldc_write_stream; 26681ae08745Sheppo break; 26691ae08745Sheppo default: 26701ae08745Sheppo exit_val = EINVAL; 26711ae08745Sheppo goto cleanup_on_exit; 26721ae08745Sheppo } 26731ae08745Sheppo 2674e1ebb9ecSlm66018 /* 2675e1ebb9ecSlm66018 * qlen is (mtu * ldc_mtu_msgs) / pkt_payload. If this 2676e1ebb9ecSlm66018 * value is smaller than default length of ldc_queue_entries, 267722f747efSnarayan * qlen is set to ldc_queue_entries. Ensure that computed 267822f747efSnarayan * length is a power-of-two value. 2679e1ebb9ecSlm66018 */ 2680e1ebb9ecSlm66018 qlen = (ldcp->mtu * ldc_mtu_msgs) / ldcp->pkt_payload; 268122f747efSnarayan if (!ISP2(qlen)) { 268222f747efSnarayan uint64_t tmp = 1; 268322f747efSnarayan while (qlen) { 268422f747efSnarayan qlen >>= 1; tmp <<= 1; 268522f747efSnarayan } 268622f747efSnarayan qlen = tmp; 268722f747efSnarayan } 268822f747efSnarayan 2689e1ebb9ecSlm66018 ldcp->rx_q_entries = 2690e1ebb9ecSlm66018 (qlen < ldc_queue_entries) ? ldc_queue_entries : qlen; 2691e1ebb9ecSlm66018 ldcp->tx_q_entries = ldcp->rx_q_entries; 2692e1ebb9ecSlm66018 269322f747efSnarayan D1(ldcp->id, "ldc_init: queue length = 0x%llx\n", ldcp->rx_q_entries); 2694e1ebb9ecSlm66018 26951ae08745Sheppo /* Create a transmit queue */ 26961ae08745Sheppo ldcp->tx_q_va = (uint64_t) 26971ae08745Sheppo contig_mem_alloc(ldcp->tx_q_entries << LDC_PACKET_SHIFT); 26981ae08745Sheppo if (ldcp->tx_q_va == NULL) { 26991ae08745Sheppo cmn_err(CE_WARN, 27001ae08745Sheppo "ldc_init: (0x%lx) TX queue allocation failed\n", 27011ae08745Sheppo ldcp->id); 27021ae08745Sheppo exit_val = ENOMEM; 27031ae08745Sheppo goto cleanup_on_exit; 27041ae08745Sheppo } 27051ae08745Sheppo ldcp->tx_q_ra = va_to_pa((caddr_t)ldcp->tx_q_va); 27061ae08745Sheppo 27071ae08745Sheppo D2(ldcp->id, "ldc_init: txq_va=0x%llx, txq_ra=0x%llx, entries=0x%llx\n", 27081ae08745Sheppo ldcp->tx_q_va, ldcp->tx_q_ra, ldcp->tx_q_entries); 27091ae08745Sheppo 27101ae08745Sheppo ldcp->tstate |= TS_TXQ_RDY; 27111ae08745Sheppo 27121ae08745Sheppo /* Create a receive queue */ 27131ae08745Sheppo ldcp->rx_q_va = (uint64_t) 27141ae08745Sheppo contig_mem_alloc(ldcp->rx_q_entries << LDC_PACKET_SHIFT); 27151ae08745Sheppo if (ldcp->rx_q_va == NULL) { 27161ae08745Sheppo cmn_err(CE_WARN, 27171ae08745Sheppo "ldc_init: (0x%lx) RX queue allocation failed\n", 27181ae08745Sheppo ldcp->id); 27191ae08745Sheppo exit_val = ENOMEM; 27201ae08745Sheppo goto cleanup_on_exit; 27211ae08745Sheppo } 27221ae08745Sheppo ldcp->rx_q_ra = va_to_pa((caddr_t)ldcp->rx_q_va); 27231ae08745Sheppo 27241ae08745Sheppo D2(ldcp->id, "ldc_init: rxq_va=0x%llx, rxq_ra=0x%llx, entries=0x%llx\n", 27251ae08745Sheppo ldcp->rx_q_va, ldcp->rx_q_ra, ldcp->rx_q_entries); 27261ae08745Sheppo 27271ae08745Sheppo ldcp->tstate |= TS_RXQ_RDY; 27281ae08745Sheppo 272958283286Sha137994 /* Setup a separate read data queue */ 273020ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 273158283286Sha137994 ldcp->readq_get_state = i_ldc_dq_rx_get_state; 273258283286Sha137994 ldcp->readq_set_head = i_ldc_set_rxdq_head; 273358283286Sha137994 273458283286Sha137994 /* Make sure the data queue multiplier is a power of 2 */ 273558283286Sha137994 if (!ISP2(ldc_rxdq_multiplier)) { 273658283286Sha137994 D1(ldcp->id, "ldc_init: (0x%llx) ldc_rxdq_multiplier " 273758283286Sha137994 "not a power of 2, resetting", ldcp->id); 273858283286Sha137994 ldc_rxdq_multiplier = LDC_RXDQ_MULTIPLIER; 273958283286Sha137994 } 274058283286Sha137994 274158283286Sha137994 ldcp->rx_dq_entries = ldc_rxdq_multiplier * ldcp->rx_q_entries; 274258283286Sha137994 ldcp->rx_dq_va = (uint64_t) 274358283286Sha137994 kmem_alloc(ldcp->rx_dq_entries << LDC_PACKET_SHIFT, 274458283286Sha137994 KM_SLEEP); 274558283286Sha137994 if (ldcp->rx_dq_va == NULL) { 274658283286Sha137994 cmn_err(CE_WARN, 274758283286Sha137994 "ldc_init: (0x%lx) RX data queue " 274858283286Sha137994 "allocation failed\n", ldcp->id); 274958283286Sha137994 exit_val = ENOMEM; 275058283286Sha137994 goto cleanup_on_exit; 275158283286Sha137994 } 275258283286Sha137994 275358283286Sha137994 ldcp->rx_dq_head = ldcp->rx_dq_tail = 0; 275458283286Sha137994 275558283286Sha137994 D2(ldcp->id, "ldc_init: rx_dq_va=0x%llx, " 275658283286Sha137994 "rx_dq_entries=0x%llx\n", ldcp->rx_dq_va, 275758283286Sha137994 ldcp->rx_dq_entries); 275858283286Sha137994 } else { 275958283286Sha137994 ldcp->readq_get_state = i_ldc_hvq_rx_get_state; 276058283286Sha137994 ldcp->readq_set_head = i_ldc_set_rx_head; 276158283286Sha137994 } 276258283286Sha137994 27631ae08745Sheppo /* Init descriptor ring and memory handle list lock */ 27641ae08745Sheppo mutex_init(&ldcp->exp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 27651ae08745Sheppo mutex_init(&ldcp->imp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 27661ae08745Sheppo mutex_init(&ldcp->mlist_lock, NULL, MUTEX_DRIVER, NULL); 27671ae08745Sheppo 27681ae08745Sheppo /* mark status as INITialized */ 27691ae08745Sheppo ldcp->status = LDC_INIT; 27701ae08745Sheppo 27711ae08745Sheppo /* Add to channel list */ 27721ae08745Sheppo mutex_enter(&ldcssp->lock); 27731ae08745Sheppo ldcp->next = ldcssp->chan_list; 27741ae08745Sheppo ldcssp->chan_list = ldcp; 27751ae08745Sheppo ldcssp->channel_count++; 27761ae08745Sheppo mutex_exit(&ldcssp->lock); 27771ae08745Sheppo 27781ae08745Sheppo /* set the handle */ 27791ae08745Sheppo *handle = (ldc_handle_t)ldcp; 27801ae08745Sheppo 27811ae08745Sheppo D1(ldcp->id, "ldc_init: (0x%llx) channel initialized\n", ldcp->id); 27821ae08745Sheppo 27831ae08745Sheppo return (0); 27841ae08745Sheppo 27851ae08745Sheppo cleanup_on_exit: 27861ae08745Sheppo 278720ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE && ldcp->stream_bufferp) 27881ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 27891ae08745Sheppo 27901ae08745Sheppo if (ldcp->tstate & TS_TXQ_RDY) 27911ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 27921ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 27931ae08745Sheppo 27941ae08745Sheppo if (ldcp->tstate & TS_RXQ_RDY) 27951ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 27961ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 27971ae08745Sheppo 2798d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 27991ae08745Sheppo mutex_destroy(&ldcp->lock); 28001ae08745Sheppo 28011ae08745Sheppo if (ldcp) 28021ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 28031ae08745Sheppo 28041ae08745Sheppo return (exit_val); 28051ae08745Sheppo } 28061ae08745Sheppo 28071ae08745Sheppo /* 28081ae08745Sheppo * Finalizes the LDC connection. It will return EBUSY if the 28091ae08745Sheppo * channel is open. A ldc_close() has to be done prior to 28101ae08745Sheppo * a ldc_fini operation. It frees TX/RX queues, associated 28111ae08745Sheppo * with the channel 28121ae08745Sheppo */ 28131ae08745Sheppo int 28141ae08745Sheppo ldc_fini(ldc_handle_t handle) 28151ae08745Sheppo { 28161ae08745Sheppo ldc_chan_t *ldcp; 28171ae08745Sheppo ldc_chan_t *tmp_ldcp; 28181ae08745Sheppo uint64_t id; 28191ae08745Sheppo 28201ae08745Sheppo if (handle == NULL) { 28211ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel handle\n"); 28221ae08745Sheppo return (EINVAL); 28231ae08745Sheppo } 28241ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28251ae08745Sheppo id = ldcp->id; 28261ae08745Sheppo 28271ae08745Sheppo mutex_enter(&ldcp->lock); 28281ae08745Sheppo 28293af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) > TS_INIT) { 28301ae08745Sheppo DWARN(ldcp->id, "ldc_fini: (0x%llx) channel is open\n", 28311ae08745Sheppo ldcp->id); 28321ae08745Sheppo mutex_exit(&ldcp->lock); 28331ae08745Sheppo return (EBUSY); 28341ae08745Sheppo } 28351ae08745Sheppo 28361ae08745Sheppo /* Remove from the channel list */ 28371ae08745Sheppo mutex_enter(&ldcssp->lock); 28381ae08745Sheppo tmp_ldcp = ldcssp->chan_list; 28391ae08745Sheppo if (tmp_ldcp == ldcp) { 28401ae08745Sheppo ldcssp->chan_list = ldcp->next; 28411ae08745Sheppo ldcp->next = NULL; 28421ae08745Sheppo } else { 28431ae08745Sheppo while (tmp_ldcp != NULL) { 28441ae08745Sheppo if (tmp_ldcp->next == ldcp) { 28451ae08745Sheppo tmp_ldcp->next = ldcp->next; 28461ae08745Sheppo ldcp->next = NULL; 28471ae08745Sheppo break; 28481ae08745Sheppo } 28491ae08745Sheppo tmp_ldcp = tmp_ldcp->next; 28501ae08745Sheppo } 28511ae08745Sheppo if (tmp_ldcp == NULL) { 28521ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel hdl\n"); 28531ae08745Sheppo mutex_exit(&ldcssp->lock); 28541ae08745Sheppo mutex_exit(&ldcp->lock); 28551ae08745Sheppo return (EINVAL); 28561ae08745Sheppo } 28571ae08745Sheppo } 28581ae08745Sheppo 28591ae08745Sheppo ldcssp->channel_count--; 28601ae08745Sheppo 28611ae08745Sheppo mutex_exit(&ldcssp->lock); 28621ae08745Sheppo 28631ae08745Sheppo /* Free the map table for this channel */ 28641ae08745Sheppo if (ldcp->mtbl) { 28651ae08745Sheppo (void) hv_ldc_set_map_table(ldcp->id, NULL, NULL); 28663af08d82Slm66018 if (ldcp->mtbl->contigmem) 28671ae08745Sheppo contig_mem_free(ldcp->mtbl->table, ldcp->mtbl->size); 28683af08d82Slm66018 else 28693af08d82Slm66018 kmem_free(ldcp->mtbl->table, ldcp->mtbl->size); 28701ae08745Sheppo mutex_destroy(&ldcp->mtbl->lock); 28711ae08745Sheppo kmem_free(ldcp->mtbl, sizeof (ldc_mtbl_t)); 28721ae08745Sheppo } 28731ae08745Sheppo 28741ae08745Sheppo /* Destroy descriptor ring and memory handle list lock */ 28751ae08745Sheppo mutex_destroy(&ldcp->exp_dlist_lock); 28761ae08745Sheppo mutex_destroy(&ldcp->imp_dlist_lock); 28771ae08745Sheppo mutex_destroy(&ldcp->mlist_lock); 28781ae08745Sheppo 287920ae46ebSha137994 /* Free the stream buffer for RELIABLE_MODE */ 288020ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE && ldcp->stream_bufferp) 28811ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 28821ae08745Sheppo 28831ae08745Sheppo /* Free the RX queue */ 28841ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 28851ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 28861ae08745Sheppo ldcp->tstate &= ~TS_RXQ_RDY; 28871ae08745Sheppo 288858283286Sha137994 /* Free the RX data queue */ 288920ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 289058283286Sha137994 kmem_free((caddr_t)ldcp->rx_dq_va, 289158283286Sha137994 (ldcp->rx_dq_entries << LDC_PACKET_SHIFT)); 289258283286Sha137994 } 289358283286Sha137994 28941ae08745Sheppo /* Free the TX queue */ 28951ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 28961ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 28971ae08745Sheppo ldcp->tstate &= ~TS_TXQ_RDY; 28981ae08745Sheppo 28991ae08745Sheppo mutex_exit(&ldcp->lock); 29001ae08745Sheppo 29011ae08745Sheppo /* Destroy mutex */ 2902d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 29031ae08745Sheppo mutex_destroy(&ldcp->lock); 29041ae08745Sheppo 29051ae08745Sheppo /* free channel structure */ 29061ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 29071ae08745Sheppo 29081ae08745Sheppo D1(id, "ldc_fini: (0x%llx) channel finalized\n", id); 29091ae08745Sheppo 29101ae08745Sheppo return (0); 29111ae08745Sheppo } 29121ae08745Sheppo 29131ae08745Sheppo /* 29141ae08745Sheppo * Open the LDC channel for use. It registers the TX/RX queues 29151ae08745Sheppo * with the Hypervisor. It also specifies the interrupt number 29161ae08745Sheppo * and target CPU for this channel 29171ae08745Sheppo */ 29181ae08745Sheppo int 29191ae08745Sheppo ldc_open(ldc_handle_t handle) 29201ae08745Sheppo { 29211ae08745Sheppo ldc_chan_t *ldcp; 29221ae08745Sheppo int rv; 29231ae08745Sheppo 29241ae08745Sheppo if (handle == NULL) { 29251ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_open: invalid channel handle\n"); 29261ae08745Sheppo return (EINVAL); 29271ae08745Sheppo } 29281ae08745Sheppo 29291ae08745Sheppo ldcp = (ldc_chan_t *)handle; 29301ae08745Sheppo 29311ae08745Sheppo mutex_enter(&ldcp->lock); 29321ae08745Sheppo 29331ae08745Sheppo if (ldcp->tstate < TS_INIT) { 29341ae08745Sheppo DWARN(ldcp->id, 29351ae08745Sheppo "ldc_open: (0x%llx) channel not initialized\n", ldcp->id); 29361ae08745Sheppo mutex_exit(&ldcp->lock); 29371ae08745Sheppo return (EFAULT); 29381ae08745Sheppo } 29393af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) >= TS_OPEN) { 29401ae08745Sheppo DWARN(ldcp->id, 29411ae08745Sheppo "ldc_open: (0x%llx) channel is already open\n", ldcp->id); 29421ae08745Sheppo mutex_exit(&ldcp->lock); 29431ae08745Sheppo return (EFAULT); 29441ae08745Sheppo } 29451ae08745Sheppo 29461ae08745Sheppo /* 29471ae08745Sheppo * Unregister/Register the tx queue with the hypervisor 29481ae08745Sheppo */ 29491ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 29501ae08745Sheppo if (rv) { 29511ae08745Sheppo cmn_err(CE_WARN, 29521ae08745Sheppo "ldc_open: (0x%lx) channel tx queue unconf failed\n", 29531ae08745Sheppo ldcp->id); 29541ae08745Sheppo mutex_exit(&ldcp->lock); 29551ae08745Sheppo return (EIO); 29561ae08745Sheppo } 29571ae08745Sheppo 29581ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 29591ae08745Sheppo if (rv) { 29601ae08745Sheppo cmn_err(CE_WARN, 29611ae08745Sheppo "ldc_open: (0x%lx) channel tx queue conf failed\n", 29621ae08745Sheppo ldcp->id); 29631ae08745Sheppo mutex_exit(&ldcp->lock); 29641ae08745Sheppo return (EIO); 29651ae08745Sheppo } 29661ae08745Sheppo 29671ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered tx queue with LDC\n", 29681ae08745Sheppo ldcp->id); 29691ae08745Sheppo 29701ae08745Sheppo /* 29711ae08745Sheppo * Unregister/Register the rx queue with the hypervisor 29721ae08745Sheppo */ 29731ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 29741ae08745Sheppo if (rv) { 29751ae08745Sheppo cmn_err(CE_WARN, 29761ae08745Sheppo "ldc_open: (0x%lx) channel rx queue unconf failed\n", 29771ae08745Sheppo ldcp->id); 29781ae08745Sheppo mutex_exit(&ldcp->lock); 29791ae08745Sheppo return (EIO); 29801ae08745Sheppo } 29811ae08745Sheppo 29821ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, ldcp->rx_q_entries); 29831ae08745Sheppo if (rv) { 29841ae08745Sheppo cmn_err(CE_WARN, 29851ae08745Sheppo "ldc_open: (0x%lx) channel rx queue conf failed\n", 29861ae08745Sheppo ldcp->id); 29871ae08745Sheppo mutex_exit(&ldcp->lock); 29881ae08745Sheppo return (EIO); 29891ae08745Sheppo } 29901ae08745Sheppo 29911ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered rx queue with LDC\n", 29921ae08745Sheppo ldcp->id); 29931ae08745Sheppo 29941ae08745Sheppo ldcp->tstate |= TS_QCONF_RDY; 29951ae08745Sheppo 29961ae08745Sheppo /* Register the channel with the channel nexus */ 29971ae08745Sheppo rv = i_ldc_register_channel(ldcp); 29981ae08745Sheppo if (rv && rv != EAGAIN) { 29991ae08745Sheppo cmn_err(CE_WARN, 30001ae08745Sheppo "ldc_open: (0x%lx) channel register failed\n", ldcp->id); 30015699897cSHaik Aftandilian ldcp->tstate &= ~TS_QCONF_RDY; 30021ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 30031ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 30041ae08745Sheppo mutex_exit(&ldcp->lock); 30051ae08745Sheppo return (EIO); 30061ae08745Sheppo } 30071ae08745Sheppo 30081ae08745Sheppo /* mark channel in OPEN state */ 30091ae08745Sheppo ldcp->status = LDC_OPEN; 30101ae08745Sheppo 30111ae08745Sheppo /* Read channel state */ 30121ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 30131ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 30141ae08745Sheppo if (rv) { 30151ae08745Sheppo cmn_err(CE_WARN, 30161ae08745Sheppo "ldc_open: (0x%lx) cannot read channel state\n", 30171ae08745Sheppo ldcp->id); 30181ae08745Sheppo (void) i_ldc_unregister_channel(ldcp); 30195699897cSHaik Aftandilian ldcp->tstate &= ~TS_QCONF_RDY; 30201ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 30211ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 30221ae08745Sheppo mutex_exit(&ldcp->lock); 30231ae08745Sheppo return (EIO); 30241ae08745Sheppo } 30251ae08745Sheppo 30261ae08745Sheppo /* 302720ae46ebSha137994 * set the ACKd head to current head location for reliable 30281ae08745Sheppo */ 30291ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 30301ae08745Sheppo 30311ae08745Sheppo /* mark channel ready if HV report link is UP (peer alloc'd Rx queue) */ 30321ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 30331ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 30341ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 30351ae08745Sheppo ldcp->status = LDC_READY; 30361ae08745Sheppo } 30371ae08745Sheppo 30381ae08745Sheppo /* 30391ae08745Sheppo * if channel is being opened in RAW mode - no handshake is needed 30401ae08745Sheppo * switch the channel READY and UP state 30411ae08745Sheppo */ 30421ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 30431ae08745Sheppo ldcp->tstate = TS_UP; /* set bits associated with LDC UP */ 30441ae08745Sheppo ldcp->status = LDC_UP; 30451ae08745Sheppo } 30461ae08745Sheppo 30471ae08745Sheppo mutex_exit(&ldcp->lock); 30481ae08745Sheppo 30491ae08745Sheppo /* 30501ae08745Sheppo * Increment number of open channels 30511ae08745Sheppo */ 30521ae08745Sheppo mutex_enter(&ldcssp->lock); 30531ae08745Sheppo ldcssp->channels_open++; 30541ae08745Sheppo mutex_exit(&ldcssp->lock); 30551ae08745Sheppo 3056cb112a14Slm66018 D1(ldcp->id, 30573af08d82Slm66018 "ldc_open: (0x%llx) channel (0x%p) open for use " 30583af08d82Slm66018 "(tstate=0x%x, status=0x%x)\n", 30593af08d82Slm66018 ldcp->id, ldcp, ldcp->tstate, ldcp->status); 30601ae08745Sheppo 30611ae08745Sheppo return (0); 30621ae08745Sheppo } 30631ae08745Sheppo 30641ae08745Sheppo /* 30651ae08745Sheppo * Close the LDC connection. It will return EBUSY if there 30661ae08745Sheppo * are memory segments or descriptor rings either bound to or 30671ae08745Sheppo * mapped over the channel 30681ae08745Sheppo */ 30691ae08745Sheppo int 30701ae08745Sheppo ldc_close(ldc_handle_t handle) 30711ae08745Sheppo { 30721ae08745Sheppo ldc_chan_t *ldcp; 3073d10e4ef2Snarayan int rv = 0, retries = 0; 30741ae08745Sheppo boolean_t chk_done = B_FALSE; 30751ae08745Sheppo 30761ae08745Sheppo if (handle == NULL) { 30771ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_close: invalid channel handle\n"); 30781ae08745Sheppo return (EINVAL); 30791ae08745Sheppo } 30801ae08745Sheppo ldcp = (ldc_chan_t *)handle; 30811ae08745Sheppo 30821ae08745Sheppo mutex_enter(&ldcp->lock); 30831ae08745Sheppo 30841ae08745Sheppo /* return error if channel is not open */ 30853af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) < TS_OPEN) { 30861ae08745Sheppo DWARN(ldcp->id, 30871ae08745Sheppo "ldc_close: (0x%llx) channel is not open\n", ldcp->id); 30881ae08745Sheppo mutex_exit(&ldcp->lock); 30891ae08745Sheppo return (EFAULT); 30901ae08745Sheppo } 30911ae08745Sheppo 30921ae08745Sheppo /* if any memory handles, drings, are bound or mapped cannot close */ 30931ae08745Sheppo if (ldcp->mhdl_list != NULL) { 30941ae08745Sheppo DWARN(ldcp->id, 30951ae08745Sheppo "ldc_close: (0x%llx) channel has bound memory handles\n", 30961ae08745Sheppo ldcp->id); 30971ae08745Sheppo mutex_exit(&ldcp->lock); 30981ae08745Sheppo return (EBUSY); 30991ae08745Sheppo } 31001ae08745Sheppo if (ldcp->exp_dring_list != NULL) { 31011ae08745Sheppo DWARN(ldcp->id, 31021ae08745Sheppo "ldc_close: (0x%llx) channel has bound descriptor rings\n", 31031ae08745Sheppo ldcp->id); 31041ae08745Sheppo mutex_exit(&ldcp->lock); 31051ae08745Sheppo return (EBUSY); 31061ae08745Sheppo } 31071ae08745Sheppo if (ldcp->imp_dring_list != NULL) { 31081ae08745Sheppo DWARN(ldcp->id, 31091ae08745Sheppo "ldc_close: (0x%llx) channel has mapped descriptor rings\n", 31101ae08745Sheppo ldcp->id); 31111ae08745Sheppo mutex_exit(&ldcp->lock); 31121ae08745Sheppo return (EBUSY); 31131ae08745Sheppo } 31141ae08745Sheppo 31154d39be2bSsg70180 if (ldcp->cb_inprogress) { 31164d39be2bSsg70180 DWARN(ldcp->id, "ldc_close: (0x%llx) callback active\n", 31174d39be2bSsg70180 ldcp->id); 31184d39be2bSsg70180 mutex_exit(&ldcp->lock); 31194d39be2bSsg70180 return (EWOULDBLOCK); 31204d39be2bSsg70180 } 31214d39be2bSsg70180 3122d10e4ef2Snarayan /* Obtain Tx lock */ 3123d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3124d10e4ef2Snarayan 31251ae08745Sheppo /* 31261ae08745Sheppo * Wait for pending transmits to complete i.e Tx queue to drain 31271ae08745Sheppo * if there are pending pkts - wait 1 ms and retry again 31281ae08745Sheppo */ 31291ae08745Sheppo for (;;) { 31301ae08745Sheppo 31311ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 31321ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 31331ae08745Sheppo if (rv) { 31341ae08745Sheppo cmn_err(CE_WARN, 31351ae08745Sheppo "ldc_close: (0x%lx) cannot read qptrs\n", ldcp->id); 3136d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 31371ae08745Sheppo mutex_exit(&ldcp->lock); 31381ae08745Sheppo return (EIO); 31391ae08745Sheppo } 31401ae08745Sheppo 31411ae08745Sheppo if (ldcp->tx_head == ldcp->tx_tail || 31421ae08745Sheppo ldcp->link_state != LDC_CHANNEL_UP) { 31431ae08745Sheppo break; 31441ae08745Sheppo } 31451ae08745Sheppo 31461ae08745Sheppo if (chk_done) { 31471ae08745Sheppo DWARN(ldcp->id, 31481ae08745Sheppo "ldc_close: (0x%llx) Tx queue drain timeout\n", 31491ae08745Sheppo ldcp->id); 31501ae08745Sheppo break; 31511ae08745Sheppo } 31521ae08745Sheppo 31531ae08745Sheppo /* wait for one ms and try again */ 31541ae08745Sheppo delay(drv_usectohz(1000)); 31551ae08745Sheppo chk_done = B_TRUE; 31561ae08745Sheppo } 31571ae08745Sheppo 31581ae08745Sheppo /* 3159a8ea4edeSnarayan * Drain the Tx and Rx queues as we are closing the 3160a8ea4edeSnarayan * channel. We dont care about any pending packets. 3161a8ea4edeSnarayan * We have to also drain the queue prior to clearing 3162a8ea4edeSnarayan * pending interrupts, otherwise the HV will trigger 3163a8ea4edeSnarayan * an interrupt the moment the interrupt state is 3164a8ea4edeSnarayan * cleared. 31653af08d82Slm66018 */ 31663af08d82Slm66018 (void) i_ldc_txq_reconf(ldcp); 3167a8ea4edeSnarayan (void) i_ldc_rxq_drain(ldcp); 31683af08d82Slm66018 31693af08d82Slm66018 /* 31701ae08745Sheppo * Unregister the channel with the nexus 31711ae08745Sheppo */ 3172d10e4ef2Snarayan while ((rv = i_ldc_unregister_channel(ldcp)) != 0) { 3173d10e4ef2Snarayan 3174d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 31751ae08745Sheppo mutex_exit(&ldcp->lock); 3176d10e4ef2Snarayan 3177d10e4ef2Snarayan /* if any error other than EAGAIN return back */ 3178a8ea4edeSnarayan if (rv != EAGAIN || retries >= ldc_max_retries) { 3179d10e4ef2Snarayan cmn_err(CE_WARN, 3180d10e4ef2Snarayan "ldc_close: (0x%lx) unregister failed, %d\n", 3181d10e4ef2Snarayan ldcp->id, rv); 31821ae08745Sheppo return (rv); 31831ae08745Sheppo } 31841ae08745Sheppo 31851ae08745Sheppo /* 3186d10e4ef2Snarayan * As there could be pending interrupts we need 3187d10e4ef2Snarayan * to wait and try again 3188d10e4ef2Snarayan */ 31894d39be2bSsg70180 drv_usecwait(ldc_close_delay); 3190d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3191d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3192d10e4ef2Snarayan retries++; 3193d10e4ef2Snarayan } 3194d10e4ef2Snarayan 31955699897cSHaik Aftandilian ldcp->tstate &= ~TS_QCONF_RDY; 31965699897cSHaik Aftandilian 3197d10e4ef2Snarayan /* 31981ae08745Sheppo * Unregister queues 31991ae08745Sheppo */ 32001ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 32011ae08745Sheppo if (rv) { 32021ae08745Sheppo cmn_err(CE_WARN, 32031ae08745Sheppo "ldc_close: (0x%lx) channel TX queue unconf failed\n", 32041ae08745Sheppo ldcp->id); 3205d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 32061ae08745Sheppo mutex_exit(&ldcp->lock); 32071ae08745Sheppo return (EIO); 32081ae08745Sheppo } 32091ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 32101ae08745Sheppo if (rv) { 32111ae08745Sheppo cmn_err(CE_WARN, 32121ae08745Sheppo "ldc_close: (0x%lx) channel RX queue unconf failed\n", 32131ae08745Sheppo ldcp->id); 3214d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 32151ae08745Sheppo mutex_exit(&ldcp->lock); 32161ae08745Sheppo return (EIO); 32171ae08745Sheppo } 32181ae08745Sheppo 32191ae08745Sheppo /* Reset channel state information */ 32201ae08745Sheppo i_ldc_reset_state(ldcp); 32211ae08745Sheppo 32221ae08745Sheppo /* Mark channel as down and in initialized state */ 32231ae08745Sheppo ldcp->tx_ackd_head = 0; 32241ae08745Sheppo ldcp->tx_head = 0; 32253af08d82Slm66018 ldcp->tstate = TS_IN_RESET|TS_INIT; 32261ae08745Sheppo ldcp->status = LDC_INIT; 32271ae08745Sheppo 3228d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 32291ae08745Sheppo mutex_exit(&ldcp->lock); 32301ae08745Sheppo 32311ae08745Sheppo /* Decrement number of open channels */ 32321ae08745Sheppo mutex_enter(&ldcssp->lock); 32331ae08745Sheppo ldcssp->channels_open--; 32341ae08745Sheppo mutex_exit(&ldcssp->lock); 32351ae08745Sheppo 32361ae08745Sheppo D1(ldcp->id, "ldc_close: (0x%llx) channel closed\n", ldcp->id); 32371ae08745Sheppo 32381ae08745Sheppo return (0); 32391ae08745Sheppo } 32401ae08745Sheppo 32411ae08745Sheppo /* 32421ae08745Sheppo * Register channel callback 32431ae08745Sheppo */ 32441ae08745Sheppo int 32451ae08745Sheppo ldc_reg_callback(ldc_handle_t handle, 32461ae08745Sheppo uint_t(*cb)(uint64_t event, caddr_t arg), caddr_t arg) 32471ae08745Sheppo { 32481ae08745Sheppo ldc_chan_t *ldcp; 32491ae08745Sheppo 32501ae08745Sheppo if (handle == NULL) { 32511ae08745Sheppo DWARN(DBG_ALL_LDCS, 32521ae08745Sheppo "ldc_reg_callback: invalid channel handle\n"); 32531ae08745Sheppo return (EINVAL); 32541ae08745Sheppo } 32551ae08745Sheppo if (((uint64_t)cb) < KERNELBASE) { 32561ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_reg_callback: invalid callback\n"); 32571ae08745Sheppo return (EINVAL); 32581ae08745Sheppo } 32591ae08745Sheppo ldcp = (ldc_chan_t *)handle; 32601ae08745Sheppo 32611ae08745Sheppo mutex_enter(&ldcp->lock); 32621ae08745Sheppo 32631ae08745Sheppo if (ldcp->cb) { 32641ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback exists\n", 32651ae08745Sheppo ldcp->id); 32661ae08745Sheppo mutex_exit(&ldcp->lock); 32671ae08745Sheppo return (EIO); 32681ae08745Sheppo } 32691ae08745Sheppo if (ldcp->cb_inprogress) { 32701ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback active\n", 32711ae08745Sheppo ldcp->id); 32721ae08745Sheppo mutex_exit(&ldcp->lock); 32731ae08745Sheppo return (EWOULDBLOCK); 32741ae08745Sheppo } 32751ae08745Sheppo 32761ae08745Sheppo ldcp->cb = cb; 32771ae08745Sheppo ldcp->cb_arg = arg; 32781ae08745Sheppo ldcp->cb_enabled = B_TRUE; 32791ae08745Sheppo 32801ae08745Sheppo D1(ldcp->id, 32811ae08745Sheppo "ldc_reg_callback: (0x%llx) registered callback for channel\n", 32821ae08745Sheppo ldcp->id); 32831ae08745Sheppo 32841ae08745Sheppo mutex_exit(&ldcp->lock); 32851ae08745Sheppo 32861ae08745Sheppo return (0); 32871ae08745Sheppo } 32881ae08745Sheppo 32891ae08745Sheppo /* 32901ae08745Sheppo * Unregister channel callback 32911ae08745Sheppo */ 32921ae08745Sheppo int 32931ae08745Sheppo ldc_unreg_callback(ldc_handle_t handle) 32941ae08745Sheppo { 32951ae08745Sheppo ldc_chan_t *ldcp; 32961ae08745Sheppo 32971ae08745Sheppo if (handle == NULL) { 32981ae08745Sheppo DWARN(DBG_ALL_LDCS, 32991ae08745Sheppo "ldc_unreg_callback: invalid channel handle\n"); 33001ae08745Sheppo return (EINVAL); 33011ae08745Sheppo } 33021ae08745Sheppo ldcp = (ldc_chan_t *)handle; 33031ae08745Sheppo 33041ae08745Sheppo mutex_enter(&ldcp->lock); 33051ae08745Sheppo 33061ae08745Sheppo if (ldcp->cb == NULL) { 33071ae08745Sheppo DWARN(ldcp->id, 33081ae08745Sheppo "ldc_unreg_callback: (0x%llx) no callback exists\n", 33091ae08745Sheppo ldcp->id); 33101ae08745Sheppo mutex_exit(&ldcp->lock); 33111ae08745Sheppo return (EIO); 33121ae08745Sheppo } 33131ae08745Sheppo if (ldcp->cb_inprogress) { 33141ae08745Sheppo DWARN(ldcp->id, 33151ae08745Sheppo "ldc_unreg_callback: (0x%llx) callback active\n", 33161ae08745Sheppo ldcp->id); 33171ae08745Sheppo mutex_exit(&ldcp->lock); 33181ae08745Sheppo return (EWOULDBLOCK); 33191ae08745Sheppo } 33201ae08745Sheppo 33211ae08745Sheppo ldcp->cb = NULL; 33221ae08745Sheppo ldcp->cb_arg = NULL; 33231ae08745Sheppo ldcp->cb_enabled = B_FALSE; 33241ae08745Sheppo 33251ae08745Sheppo D1(ldcp->id, 33261ae08745Sheppo "ldc_unreg_callback: (0x%llx) unregistered callback for channel\n", 33271ae08745Sheppo ldcp->id); 33281ae08745Sheppo 33291ae08745Sheppo mutex_exit(&ldcp->lock); 33301ae08745Sheppo 33311ae08745Sheppo return (0); 33321ae08745Sheppo } 33331ae08745Sheppo 33341ae08745Sheppo 33351ae08745Sheppo /* 33361ae08745Sheppo * Bring a channel up by initiating a handshake with the peer 33371ae08745Sheppo * This call is asynchronous. It will complete at a later point 33381ae08745Sheppo * in time when the peer responds back with an RTR. 33391ae08745Sheppo */ 33401ae08745Sheppo int 33411ae08745Sheppo ldc_up(ldc_handle_t handle) 33421ae08745Sheppo { 33431ae08745Sheppo int rv; 33441ae08745Sheppo ldc_chan_t *ldcp; 33451ae08745Sheppo ldc_msg_t *ldcmsg; 334657e6a936Ssb155480 uint64_t tx_tail, tstate, link_state; 33471ae08745Sheppo 33481ae08745Sheppo if (handle == NULL) { 33491ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_up: invalid channel handle\n"); 33501ae08745Sheppo return (EINVAL); 33511ae08745Sheppo } 33521ae08745Sheppo ldcp = (ldc_chan_t *)handle; 33531ae08745Sheppo 33541ae08745Sheppo mutex_enter(&ldcp->lock); 33551ae08745Sheppo 33563af08d82Slm66018 D1(ldcp->id, "ldc_up: (0x%llx) doing channel UP\n", ldcp->id); 33573af08d82Slm66018 33583af08d82Slm66018 /* clear the reset state */ 33593af08d82Slm66018 tstate = ldcp->tstate; 33603af08d82Slm66018 ldcp->tstate &= ~TS_IN_RESET; 33613af08d82Slm66018 33621ae08745Sheppo if (ldcp->tstate == TS_UP) { 33633af08d82Slm66018 DWARN(ldcp->id, 33641ae08745Sheppo "ldc_up: (0x%llx) channel is already in UP state\n", 33651ae08745Sheppo ldcp->id); 33663af08d82Slm66018 33673af08d82Slm66018 /* mark channel as up */ 33683af08d82Slm66018 ldcp->status = LDC_UP; 33693af08d82Slm66018 33703af08d82Slm66018 /* 33713af08d82Slm66018 * if channel was in reset state and there was 33723af08d82Slm66018 * pending data clear interrupt state. this will 33733af08d82Slm66018 * trigger an interrupt, causing the RX handler to 33743af08d82Slm66018 * to invoke the client's callback 33753af08d82Slm66018 */ 33763af08d82Slm66018 if ((tstate & TS_IN_RESET) && 33773af08d82Slm66018 ldcp->rx_intr_state == LDC_INTR_PEND) { 3378cb112a14Slm66018 D1(ldcp->id, 33793af08d82Slm66018 "ldc_up: (0x%llx) channel has pending data, " 33803af08d82Slm66018 "clearing interrupt\n", ldcp->id); 33813af08d82Slm66018 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 33823af08d82Slm66018 } 33833af08d82Slm66018 33841ae08745Sheppo mutex_exit(&ldcp->lock); 33851ae08745Sheppo return (0); 33861ae08745Sheppo } 33871ae08745Sheppo 33881ae08745Sheppo /* if the channel is in RAW mode - mark it as UP, if READY */ 33891ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW && ldcp->tstate >= TS_READY) { 33901ae08745Sheppo ldcp->tstate = TS_UP; 33911ae08745Sheppo mutex_exit(&ldcp->lock); 33921ae08745Sheppo return (0); 33931ae08745Sheppo } 33941ae08745Sheppo 33951ae08745Sheppo /* Don't start another handshake if there is one in progress */ 33961ae08745Sheppo if (ldcp->hstate) { 33973af08d82Slm66018 D1(ldcp->id, 33981ae08745Sheppo "ldc_up: (0x%llx) channel handshake in progress\n", 33991ae08745Sheppo ldcp->id); 34001ae08745Sheppo mutex_exit(&ldcp->lock); 34011ae08745Sheppo return (0); 34021ae08745Sheppo } 34031ae08745Sheppo 3404d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3405d10e4ef2Snarayan 340657e6a936Ssb155480 /* save current link state */ 340757e6a936Ssb155480 link_state = ldcp->link_state; 340857e6a936Ssb155480 34091ae08745Sheppo /* get the current tail for the LDC msg */ 34101ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 34111ae08745Sheppo if (rv) { 3412cb112a14Slm66018 D1(ldcp->id, "ldc_up: (0x%llx) cannot initiate handshake\n", 34131ae08745Sheppo ldcp->id); 3414d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 34151ae08745Sheppo mutex_exit(&ldcp->lock); 34161ae08745Sheppo return (ECONNREFUSED); 34171ae08745Sheppo } 34181ae08745Sheppo 341957e6a936Ssb155480 /* 342057e6a936Ssb155480 * If i_ldc_get_tx_tail() changed link_state to either RESET or UP, 342157e6a936Ssb155480 * from a previous state of DOWN, then mark the channel as 342257e6a936Ssb155480 * being ready for handshake. 342357e6a936Ssb155480 */ 342457e6a936Ssb155480 if ((link_state == LDC_CHANNEL_DOWN) && 342557e6a936Ssb155480 (link_state != ldcp->link_state)) { 342657e6a936Ssb155480 342757e6a936Ssb155480 ASSERT((ldcp->link_state == LDC_CHANNEL_RESET) || 342857e6a936Ssb155480 (ldcp->link_state == LDC_CHANNEL_UP)); 342957e6a936Ssb155480 343057e6a936Ssb155480 if (ldcp->mode == LDC_MODE_RAW) { 343157e6a936Ssb155480 ldcp->status = LDC_UP; 343257e6a936Ssb155480 ldcp->tstate = TS_UP; 343357e6a936Ssb155480 mutex_exit(&ldcp->tx_lock); 343457e6a936Ssb155480 mutex_exit(&ldcp->lock); 343557e6a936Ssb155480 return (0); 343657e6a936Ssb155480 } else { 343757e6a936Ssb155480 ldcp->status = LDC_READY; 343857e6a936Ssb155480 ldcp->tstate |= TS_LINK_READY; 343957e6a936Ssb155480 } 344057e6a936Ssb155480 344157e6a936Ssb155480 } 344257e6a936Ssb155480 34431ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 34441ae08745Sheppo ZERO_PKT(ldcmsg); 34451ae08745Sheppo 34461ae08745Sheppo ldcmsg->type = LDC_CTRL; 34471ae08745Sheppo ldcmsg->stype = LDC_INFO; 34481ae08745Sheppo ldcmsg->ctrl = LDC_VER; 34491ae08745Sheppo ldcp->next_vidx = 0; 34501ae08745Sheppo bcopy(&ldc_versions[0], ldcmsg->udata, sizeof (ldc_versions[0])); 34511ae08745Sheppo 34521ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_up snd ver", (uint64_t)ldcmsg); 34531ae08745Sheppo 34541ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 34551ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 34561ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 34571ae08745Sheppo 34581ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 34591ae08745Sheppo if (rv) { 34601ae08745Sheppo DWARN(ldcp->id, 34611ae08745Sheppo "ldc_up: (0x%llx) cannot initiate handshake rv=%d\n", 34621ae08745Sheppo ldcp->id, rv); 3463d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 34641ae08745Sheppo mutex_exit(&ldcp->lock); 34651ae08745Sheppo return (rv); 34661ae08745Sheppo } 34671ae08745Sheppo 34680a55fbb7Slm66018 ldcp->hstate |= TS_SENT_VER; 34691ae08745Sheppo ldcp->tx_tail = tx_tail; 34701ae08745Sheppo D1(ldcp->id, "ldc_up: (0x%llx) channel up initiated\n", ldcp->id); 34711ae08745Sheppo 3472d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 34731ae08745Sheppo mutex_exit(&ldcp->lock); 34741ae08745Sheppo 34751ae08745Sheppo return (rv); 34761ae08745Sheppo } 34771ae08745Sheppo 34781ae08745Sheppo 34791ae08745Sheppo /* 3480e1ebb9ecSlm66018 * Bring a channel down by resetting its state and queues 34811ae08745Sheppo */ 34821ae08745Sheppo int 3483e1ebb9ecSlm66018 ldc_down(ldc_handle_t handle) 34841ae08745Sheppo { 34851ae08745Sheppo ldc_chan_t *ldcp; 34861ae08745Sheppo 34871ae08745Sheppo if (handle == NULL) { 3488e1ebb9ecSlm66018 DWARN(DBG_ALL_LDCS, "ldc_down: invalid channel handle\n"); 34891ae08745Sheppo return (EINVAL); 34901ae08745Sheppo } 34911ae08745Sheppo ldcp = (ldc_chan_t *)handle; 34921ae08745Sheppo mutex_enter(&ldcp->lock); 3493d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 34943af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 3495d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 34961ae08745Sheppo mutex_exit(&ldcp->lock); 34971ae08745Sheppo 34981ae08745Sheppo return (0); 34991ae08745Sheppo } 35001ae08745Sheppo 35011ae08745Sheppo /* 35021ae08745Sheppo * Get the current channel status 35031ae08745Sheppo */ 35041ae08745Sheppo int 35051ae08745Sheppo ldc_status(ldc_handle_t handle, ldc_status_t *status) 35061ae08745Sheppo { 35071ae08745Sheppo ldc_chan_t *ldcp; 35081ae08745Sheppo 35091ae08745Sheppo if (handle == NULL || status == NULL) { 35101ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_status: invalid argument\n"); 35111ae08745Sheppo return (EINVAL); 35121ae08745Sheppo } 35131ae08745Sheppo ldcp = (ldc_chan_t *)handle; 35141ae08745Sheppo 35151ae08745Sheppo *status = ((ldc_chan_t *)handle)->status; 35161ae08745Sheppo 3517cb112a14Slm66018 D1(ldcp->id, 35181ae08745Sheppo "ldc_status: (0x%llx) returned status %d\n", ldcp->id, *status); 35191ae08745Sheppo return (0); 35201ae08745Sheppo } 35211ae08745Sheppo 35221ae08745Sheppo 35231ae08745Sheppo /* 35241ae08745Sheppo * Set the channel's callback mode - enable/disable callbacks 35251ae08745Sheppo */ 35261ae08745Sheppo int 35271ae08745Sheppo ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t cmode) 35281ae08745Sheppo { 35291ae08745Sheppo ldc_chan_t *ldcp; 35301ae08745Sheppo 35311ae08745Sheppo if (handle == NULL) { 35321ae08745Sheppo DWARN(DBG_ALL_LDCS, 35331ae08745Sheppo "ldc_set_intr_mode: invalid channel handle\n"); 35341ae08745Sheppo return (EINVAL); 35351ae08745Sheppo } 35361ae08745Sheppo ldcp = (ldc_chan_t *)handle; 35371ae08745Sheppo 35381ae08745Sheppo /* 35391ae08745Sheppo * Record no callbacks should be invoked 35401ae08745Sheppo */ 35411ae08745Sheppo mutex_enter(&ldcp->lock); 35421ae08745Sheppo 35431ae08745Sheppo switch (cmode) { 35441ae08745Sheppo case LDC_CB_DISABLE: 35451ae08745Sheppo if (!ldcp->cb_enabled) { 35461ae08745Sheppo DWARN(ldcp->id, 35471ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks disabled\n", 35481ae08745Sheppo ldcp->id); 35491ae08745Sheppo break; 35501ae08745Sheppo } 35511ae08745Sheppo ldcp->cb_enabled = B_FALSE; 35521ae08745Sheppo 35531ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) disabled callbacks\n", 35541ae08745Sheppo ldcp->id); 35551ae08745Sheppo break; 35561ae08745Sheppo 35571ae08745Sheppo case LDC_CB_ENABLE: 35581ae08745Sheppo if (ldcp->cb_enabled) { 35591ae08745Sheppo DWARN(ldcp->id, 35601ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks enabled\n", 35611ae08745Sheppo ldcp->id); 35621ae08745Sheppo break; 35631ae08745Sheppo } 35641ae08745Sheppo ldcp->cb_enabled = B_TRUE; 35651ae08745Sheppo 35661ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) enabled callbacks\n", 35671ae08745Sheppo ldcp->id); 35681ae08745Sheppo break; 35691ae08745Sheppo } 35701ae08745Sheppo 35711ae08745Sheppo mutex_exit(&ldcp->lock); 35721ae08745Sheppo 35731ae08745Sheppo return (0); 35741ae08745Sheppo } 35751ae08745Sheppo 35761ae08745Sheppo /* 35771ae08745Sheppo * Check to see if there are packets on the incoming queue 3578e1ebb9ecSlm66018 * Will return hasdata = B_FALSE if there are no packets 35791ae08745Sheppo */ 35801ae08745Sheppo int 3581e1ebb9ecSlm66018 ldc_chkq(ldc_handle_t handle, boolean_t *hasdata) 35821ae08745Sheppo { 35831ae08745Sheppo int rv; 35841ae08745Sheppo uint64_t rx_head, rx_tail; 35851ae08745Sheppo ldc_chan_t *ldcp; 35861ae08745Sheppo 35871ae08745Sheppo if (handle == NULL) { 35881ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_chkq: invalid channel handle\n"); 35891ae08745Sheppo return (EINVAL); 35901ae08745Sheppo } 35911ae08745Sheppo ldcp = (ldc_chan_t *)handle; 35921ae08745Sheppo 3593e1ebb9ecSlm66018 *hasdata = B_FALSE; 35941ae08745Sheppo 35951ae08745Sheppo mutex_enter(&ldcp->lock); 35961ae08745Sheppo 35971ae08745Sheppo if (ldcp->tstate != TS_UP) { 35981ae08745Sheppo D1(ldcp->id, 35991ae08745Sheppo "ldc_chkq: (0x%llx) channel is not up\n", ldcp->id); 36001ae08745Sheppo mutex_exit(&ldcp->lock); 36011ae08745Sheppo return (ECONNRESET); 36021ae08745Sheppo } 36031ae08745Sheppo 36041ae08745Sheppo /* Read packet(s) from the queue */ 36051ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 36061ae08745Sheppo &ldcp->link_state); 36071ae08745Sheppo if (rv != 0) { 36081ae08745Sheppo cmn_err(CE_WARN, 36091ae08745Sheppo "ldc_chkq: (0x%lx) unable to read queue ptrs", ldcp->id); 36101ae08745Sheppo mutex_exit(&ldcp->lock); 36111ae08745Sheppo return (EIO); 36121ae08745Sheppo } 361358283286Sha137994 36141ae08745Sheppo /* reset the channel state if the channel went down */ 36151ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 36161ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 3617d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 36183af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3619d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 36201ae08745Sheppo mutex_exit(&ldcp->lock); 36211ae08745Sheppo return (ECONNRESET); 36221ae08745Sheppo } 36231ae08745Sheppo 362458283286Sha137994 switch (ldcp->mode) { 362558283286Sha137994 case LDC_MODE_RAW: 362658283286Sha137994 /* 362758283286Sha137994 * In raw mode, there are no ctrl packets, so checking 362858283286Sha137994 * if the queue is non-empty is sufficient. 362958283286Sha137994 */ 363058283286Sha137994 *hasdata = (rx_head != rx_tail); 363158283286Sha137994 break; 363258283286Sha137994 363358283286Sha137994 case LDC_MODE_UNRELIABLE: 363458283286Sha137994 /* 363558283286Sha137994 * In unreliable mode, if the queue is non-empty, we need 363658283286Sha137994 * to check if it actually contains unread data packets. 363758283286Sha137994 * The queue may just contain ctrl packets. 363858283286Sha137994 */ 3639a74caf9bSha137994 if (rx_head != rx_tail) { 364058283286Sha137994 *hasdata = (i_ldc_chkq(ldcp) == 0); 3641a74caf9bSha137994 /* 3642a74caf9bSha137994 * If no data packets were found on the queue, 3643a74caf9bSha137994 * all packets must have been control packets 3644a74caf9bSha137994 * which will now have been processed, leaving 3645a74caf9bSha137994 * the queue empty. If the interrupt state 3646a74caf9bSha137994 * is pending, we need to clear the interrupt 3647a74caf9bSha137994 * here. 3648a74caf9bSha137994 */ 3649a74caf9bSha137994 if (*hasdata == B_FALSE && 3650a74caf9bSha137994 ldcp->rx_intr_state == LDC_INTR_PEND) { 3651a74caf9bSha137994 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 3652a74caf9bSha137994 } 3653a74caf9bSha137994 } 365458283286Sha137994 break; 365558283286Sha137994 365620ae46ebSha137994 case LDC_MODE_RELIABLE: 365758283286Sha137994 /* 365820ae46ebSha137994 * In reliable mode, first check for 'stream_remains' > 0. 365958283286Sha137994 * Otherwise, if the data queue head and tail pointers 366058283286Sha137994 * differ, there must be data to read. 366158283286Sha137994 */ 366258283286Sha137994 if (ldcp->stream_remains > 0) 3663e1ebb9ecSlm66018 *hasdata = B_TRUE; 366458283286Sha137994 else 366558283286Sha137994 *hasdata = (ldcp->rx_dq_head != ldcp->rx_dq_tail); 366658283286Sha137994 break; 366758283286Sha137994 366858283286Sha137994 default: 366958283286Sha137994 cmn_err(CE_WARN, "ldc_chkq: (0x%lx) unexpected channel mode " 367058283286Sha137994 "(0x%x)", ldcp->id, ldcp->mode); 367158283286Sha137994 mutex_exit(&ldcp->lock); 367258283286Sha137994 return (EIO); 36731ae08745Sheppo } 36741ae08745Sheppo 36751ae08745Sheppo mutex_exit(&ldcp->lock); 36761ae08745Sheppo 36771ae08745Sheppo return (0); 36781ae08745Sheppo } 36791ae08745Sheppo 36801ae08745Sheppo 36811ae08745Sheppo /* 36821ae08745Sheppo * Read 'size' amount of bytes or less. If incoming buffer 36831ae08745Sheppo * is more than 'size', ENOBUFS is returned. 36841ae08745Sheppo * 36851ae08745Sheppo * On return, size contains the number of bytes read. 36861ae08745Sheppo */ 36871ae08745Sheppo int 36881ae08745Sheppo ldc_read(ldc_handle_t handle, caddr_t bufp, size_t *sizep) 36891ae08745Sheppo { 36901ae08745Sheppo ldc_chan_t *ldcp; 36911ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 36921ae08745Sheppo int rv = 0, exit_val; 36931ae08745Sheppo 36941ae08745Sheppo if (handle == NULL) { 36951ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_read: invalid channel handle\n"); 36961ae08745Sheppo return (EINVAL); 36971ae08745Sheppo } 36981ae08745Sheppo 36991ae08745Sheppo ldcp = (ldc_chan_t *)handle; 37001ae08745Sheppo 37011ae08745Sheppo /* channel lock */ 37021ae08745Sheppo mutex_enter(&ldcp->lock); 37031ae08745Sheppo 37041ae08745Sheppo if (ldcp->tstate != TS_UP) { 37051ae08745Sheppo DWARN(ldcp->id, 37061ae08745Sheppo "ldc_read: (0x%llx) channel is not in UP state\n", 37071ae08745Sheppo ldcp->id); 37081ae08745Sheppo exit_val = ECONNRESET; 370920ae46ebSha137994 } else if (ldcp->mode == LDC_MODE_RELIABLE) { 371058283286Sha137994 TRACE_RXDQ_LENGTH(ldcp); 371158283286Sha137994 exit_val = ldcp->read_p(ldcp, bufp, sizep); 371212f80fa6Sha137994 371312f80fa6Sha137994 /* 371412f80fa6Sha137994 * For reliable mode channels, the interrupt 371512f80fa6Sha137994 * state is only set to pending during 371612f80fa6Sha137994 * interrupt handling when the secondary data 371712f80fa6Sha137994 * queue became full, leaving unprocessed 371812f80fa6Sha137994 * packets on the Rx queue. If the interrupt 371912f80fa6Sha137994 * state is pending and space is now available 372012f80fa6Sha137994 * on the data queue, clear the interrupt. 372112f80fa6Sha137994 */ 372212f80fa6Sha137994 if (ldcp->rx_intr_state == LDC_INTR_PEND && 372312f80fa6Sha137994 Q_CONTIG_SPACE(ldcp->rx_dq_head, ldcp->rx_dq_tail, 372412f80fa6Sha137994 ldcp->rx_dq_entries << LDC_PACKET_SHIFT) >= 372512f80fa6Sha137994 LDC_PACKET_SIZE) { 372612f80fa6Sha137994 /* data queue is not full */ 372712f80fa6Sha137994 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 372812f80fa6Sha137994 } 372912f80fa6Sha137994 373058283286Sha137994 mutex_exit(&ldcp->lock); 373158283286Sha137994 return (exit_val); 37321ae08745Sheppo } else { 37331ae08745Sheppo exit_val = ldcp->read_p(ldcp, bufp, sizep); 37341ae08745Sheppo } 37351ae08745Sheppo 37361ae08745Sheppo /* 37371ae08745Sheppo * if queue has been drained - clear interrupt 37381ae08745Sheppo */ 37391ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 37401ae08745Sheppo &ldcp->link_state); 3741cb112a14Slm66018 if (rv != 0) { 3742cb112a14Slm66018 cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs", 3743cb112a14Slm66018 ldcp->id); 3744cb112a14Slm66018 mutex_enter(&ldcp->tx_lock); 3745cb112a14Slm66018 i_ldc_reset(ldcp, B_TRUE); 3746cb112a14Slm66018 mutex_exit(&ldcp->tx_lock); 3747bd8f0338Snarayan mutex_exit(&ldcp->lock); 3748cb112a14Slm66018 return (ECONNRESET); 3749cb112a14Slm66018 } 37503af08d82Slm66018 37513af08d82Slm66018 if (exit_val == 0) { 37523af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 37533af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 37543af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 37553af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 37563af08d82Slm66018 exit_val = ECONNRESET; 37573af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 37583af08d82Slm66018 } 37593af08d82Slm66018 if ((rv == 0) && 37603af08d82Slm66018 (ldcp->rx_intr_state == LDC_INTR_PEND) && 37613af08d82Slm66018 (rx_head == rx_tail)) { 37621ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 37631ae08745Sheppo } 37643af08d82Slm66018 } 37651ae08745Sheppo 37661ae08745Sheppo mutex_exit(&ldcp->lock); 37671ae08745Sheppo return (exit_val); 37681ae08745Sheppo } 37691ae08745Sheppo 37701ae08745Sheppo /* 37711ae08745Sheppo * Basic raw mondo read - 37721ae08745Sheppo * no interpretation of mondo contents at all. 37731ae08745Sheppo * 37741ae08745Sheppo * Enter and exit with ldcp->lock held by caller 37751ae08745Sheppo */ 37761ae08745Sheppo static int 37771ae08745Sheppo i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 37781ae08745Sheppo { 37791ae08745Sheppo uint64_t q_size_mask; 37801ae08745Sheppo ldc_msg_t *msgp; 37811ae08745Sheppo uint8_t *msgbufp; 37821ae08745Sheppo int rv = 0, space; 37831ae08745Sheppo uint64_t rx_head, rx_tail; 37841ae08745Sheppo 37851ae08745Sheppo space = *sizep; 37861ae08745Sheppo 37871ae08745Sheppo if (space < LDC_PAYLOAD_SIZE_RAW) 37881ae08745Sheppo return (ENOBUFS); 37891ae08745Sheppo 37901ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 37911ae08745Sheppo 37921ae08745Sheppo /* compute mask for increment */ 37931ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 37941ae08745Sheppo 37951ae08745Sheppo /* 37961ae08745Sheppo * Read packet(s) from the queue 37971ae08745Sheppo */ 37981ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 37991ae08745Sheppo &ldcp->link_state); 38001ae08745Sheppo if (rv != 0) { 38011ae08745Sheppo cmn_err(CE_WARN, 38021ae08745Sheppo "ldc_read_raw: (0x%lx) unable to read queue ptrs", 38031ae08745Sheppo ldcp->id); 38041ae08745Sheppo return (EIO); 38051ae08745Sheppo } 38061ae08745Sheppo D1(ldcp->id, "ldc_read_raw: (0x%llx) rxh=0x%llx," 38071ae08745Sheppo " rxt=0x%llx, st=0x%llx\n", 38081ae08745Sheppo ldcp->id, rx_head, rx_tail, ldcp->link_state); 38091ae08745Sheppo 38101ae08745Sheppo /* reset the channel state if the channel went down */ 38113af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 38123af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 3813d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 38143af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3815d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 38161ae08745Sheppo return (ECONNRESET); 38171ae08745Sheppo } 38181ae08745Sheppo 38191ae08745Sheppo /* 38201ae08745Sheppo * Check for empty queue 38211ae08745Sheppo */ 38221ae08745Sheppo if (rx_head == rx_tail) { 38231ae08745Sheppo *sizep = 0; 38241ae08745Sheppo return (0); 38251ae08745Sheppo } 38261ae08745Sheppo 38271ae08745Sheppo /* get the message */ 38281ae08745Sheppo msgp = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 38291ae08745Sheppo 38301ae08745Sheppo /* if channel is in RAW mode, copy data and return */ 38311ae08745Sheppo msgbufp = (uint8_t *)&(msgp->raw[0]); 38321ae08745Sheppo 38331ae08745Sheppo bcopy(msgbufp, target_bufp, LDC_PAYLOAD_SIZE_RAW); 38341ae08745Sheppo 38351ae08745Sheppo DUMP_PAYLOAD(ldcp->id, msgbufp); 38361ae08745Sheppo 38371ae08745Sheppo *sizep = LDC_PAYLOAD_SIZE_RAW; 38381ae08745Sheppo 38391ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) & q_size_mask; 38400a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_head); 38411ae08745Sheppo 38421ae08745Sheppo return (rv); 38431ae08745Sheppo } 38441ae08745Sheppo 38451ae08745Sheppo /* 38461ae08745Sheppo * Process LDC mondos to build larger packets 38471ae08745Sheppo * with either un-reliable or reliable delivery. 38481ae08745Sheppo * 38491ae08745Sheppo * Enter and exit with ldcp->lock held by caller 38501ae08745Sheppo */ 38511ae08745Sheppo static int 38521ae08745Sheppo i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 38531ae08745Sheppo { 38541ae08745Sheppo int rv = 0; 38551ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 38561ae08745Sheppo uint64_t curr_head = 0; 38571ae08745Sheppo ldc_msg_t *msg; 38581ae08745Sheppo caddr_t target; 38591ae08745Sheppo size_t len = 0, bytes_read = 0; 38600a55fbb7Slm66018 int retries = 0; 386158283286Sha137994 uint64_t q_va, q_size_mask; 3862d10e4ef2Snarayan uint64_t first_fragment = 0; 38631ae08745Sheppo 38641ae08745Sheppo target = target_bufp; 38651ae08745Sheppo 38661ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 38671ae08745Sheppo 38683af08d82Slm66018 /* check if the buffer and size are valid */ 38693af08d82Slm66018 if (target_bufp == NULL || *sizep == 0) { 38703af08d82Slm66018 DWARN(ldcp->id, "ldc_read: (0x%llx) invalid buffer/size\n", 38713af08d82Slm66018 ldcp->id); 38723af08d82Slm66018 return (EINVAL); 38733af08d82Slm66018 } 38743af08d82Slm66018 387558283286Sha137994 /* Set q_va and compute increment mask for the appropriate queue */ 387620ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) { 387758283286Sha137994 q_va = ldcp->rx_dq_va; 387858283286Sha137994 q_size_mask = (ldcp->rx_dq_entries-1)<<LDC_PACKET_SHIFT; 387958283286Sha137994 } else { 388058283286Sha137994 q_va = ldcp->rx_q_va; 38811ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 388258283286Sha137994 } 38831ae08745Sheppo 38841ae08745Sheppo /* 38851ae08745Sheppo * Read packet(s) from the queue 38861ae08745Sheppo */ 388758283286Sha137994 rv = ldcp->readq_get_state(ldcp, &curr_head, &rx_tail, 38881ae08745Sheppo &ldcp->link_state); 38891ae08745Sheppo if (rv != 0) { 38903af08d82Slm66018 cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs", 38911ae08745Sheppo ldcp->id); 38923af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 38933af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 38943af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 38953af08d82Slm66018 return (ECONNRESET); 38961ae08745Sheppo } 38971ae08745Sheppo D1(ldcp->id, "ldc_read: (0x%llx) chd=0x%llx, tl=0x%llx, st=0x%llx\n", 38981ae08745Sheppo ldcp->id, curr_head, rx_tail, ldcp->link_state); 38991ae08745Sheppo 39001ae08745Sheppo /* reset the channel state if the channel went down */ 39013af08d82Slm66018 if (ldcp->link_state != LDC_CHANNEL_UP) 39023af08d82Slm66018 goto channel_is_reset; 39031ae08745Sheppo 39041ae08745Sheppo for (;;) { 39051ae08745Sheppo 39061ae08745Sheppo if (curr_head == rx_tail) { 390758283286Sha137994 /* 390858283286Sha137994 * If a data queue is being used, check the Rx HV 390958283286Sha137994 * queue. This will copy over any new data packets 391058283286Sha137994 * that have arrived. 391158283286Sha137994 */ 391220ae46ebSha137994 if (ldcp->mode == LDC_MODE_RELIABLE) 391358283286Sha137994 (void) i_ldc_chkq(ldcp); 391458283286Sha137994 391558283286Sha137994 rv = ldcp->readq_get_state(ldcp, 39161ae08745Sheppo &rx_head, &rx_tail, &ldcp->link_state); 39171ae08745Sheppo if (rv != 0) { 39181ae08745Sheppo cmn_err(CE_WARN, 39191ae08745Sheppo "ldc_read: (0x%lx) cannot read queue ptrs", 39201ae08745Sheppo ldcp->id); 3921d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 39223af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 3923d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 39241ae08745Sheppo return (ECONNRESET); 39251ae08745Sheppo } 392658283286Sha137994 39273af08d82Slm66018 if (ldcp->link_state != LDC_CHANNEL_UP) 39283af08d82Slm66018 goto channel_is_reset; 39291ae08745Sheppo 39301ae08745Sheppo if (curr_head == rx_tail) { 39311ae08745Sheppo 39321ae08745Sheppo /* If in the middle of a fragmented xfer */ 3933d10e4ef2Snarayan if (first_fragment != 0) { 39340a55fbb7Slm66018 39350a55fbb7Slm66018 /* wait for ldc_delay usecs */ 39360a55fbb7Slm66018 drv_usecwait(ldc_delay); 39370a55fbb7Slm66018 39380a55fbb7Slm66018 if (++retries < ldc_max_retries) 39391ae08745Sheppo continue; 39400a55fbb7Slm66018 39411ae08745Sheppo *sizep = 0; 394220ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) 394358283286Sha137994 ldcp->last_msg_rcd = 394458283286Sha137994 first_fragment - 1; 39453af08d82Slm66018 DWARN(DBG_ALL_LDCS, "ldc_read: " 394622f747efSnarayan "(0x%llx) read timeout", ldcp->id); 39473af08d82Slm66018 return (EAGAIN); 39481ae08745Sheppo } 39491ae08745Sheppo *sizep = 0; 39501ae08745Sheppo break; 39511ae08745Sheppo } 39523af08d82Slm66018 } 39530a55fbb7Slm66018 retries = 0; 39541ae08745Sheppo 39551ae08745Sheppo D2(ldcp->id, 39561ae08745Sheppo "ldc_read: (0x%llx) chd=0x%llx, rxhd=0x%llx, rxtl=0x%llx\n", 39571ae08745Sheppo ldcp->id, curr_head, rx_head, rx_tail); 39581ae08745Sheppo 39591ae08745Sheppo /* get the message */ 396058283286Sha137994 msg = (ldc_msg_t *)(q_va + curr_head); 39611ae08745Sheppo 39621ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_read received pkt", 39631ae08745Sheppo ldcp->rx_q_va + curr_head); 39641ae08745Sheppo 39651ae08745Sheppo /* Check the message ID for the message received */ 396620ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) { 39671ae08745Sheppo if ((rv = i_ldc_check_seqid(ldcp, msg)) != 0) { 39681ae08745Sheppo 396958283286Sha137994 DWARN(ldcp->id, "ldc_read: (0x%llx) seqid " 397058283286Sha137994 "error, q_ptrs=0x%lx,0x%lx", 397158283286Sha137994 ldcp->id, rx_head, rx_tail); 39721ae08745Sheppo 39730a55fbb7Slm66018 /* throw away data */ 39740a55fbb7Slm66018 bytes_read = 0; 39750a55fbb7Slm66018 39761ae08745Sheppo /* Reset last_msg_rcd to start of message */ 3977d10e4ef2Snarayan if (first_fragment != 0) { 3978d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 3979d10e4ef2Snarayan first_fragment = 0; 39801ae08745Sheppo } 39811ae08745Sheppo /* 39821ae08745Sheppo * Send a NACK -- invalid seqid 39831ae08745Sheppo * get the current tail for the response 39841ae08745Sheppo */ 39851ae08745Sheppo rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK, 39861ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 39871ae08745Sheppo if (rv) { 39881ae08745Sheppo cmn_err(CE_NOTE, 39891ae08745Sheppo "ldc_read: (0x%lx) err sending " 39901ae08745Sheppo "NACK msg\n", ldcp->id); 3991d10e4ef2Snarayan 399258283286Sha137994 /* if cannot send NACK - reset chan */ 3993d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 39943af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3995d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3996d10e4ef2Snarayan rv = ECONNRESET; 3997d10e4ef2Snarayan break; 39981ae08745Sheppo } 39991ae08745Sheppo 40001ae08745Sheppo /* purge receive queue */ 40010a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 40021ae08745Sheppo 40031ae08745Sheppo break; 40041ae08745Sheppo } 40051ae08745Sheppo 40061ae08745Sheppo /* 40071ae08745Sheppo * Process any messages of type CTRL messages 4008e1ebb9ecSlm66018 * Future implementations should try to pass these 4009e1ebb9ecSlm66018 * to LDC link by resetting the intr state. 40101ae08745Sheppo * 401158283286Sha137994 * NOTE: not done as a switch() as type can be 401258283286Sha137994 * both ctrl+data 40131ae08745Sheppo */ 40141ae08745Sheppo if (msg->type & LDC_CTRL) { 40151ae08745Sheppo if (rv = i_ldc_ctrlmsg(ldcp, msg)) { 40161ae08745Sheppo if (rv == EAGAIN) 40171ae08745Sheppo continue; 40180a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 40191ae08745Sheppo *sizep = 0; 40201ae08745Sheppo bytes_read = 0; 40211ae08745Sheppo break; 40221ae08745Sheppo } 40231ae08745Sheppo } 40241ae08745Sheppo 40251ae08745Sheppo /* process data ACKs */ 40261ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 4027d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 4028d10e4ef2Snarayan *sizep = 0; 4029d10e4ef2Snarayan bytes_read = 0; 4030d10e4ef2Snarayan break; 4031d10e4ef2Snarayan } 40321ae08745Sheppo } 40331ae08745Sheppo 403483d3bc6fSnarayan /* process data NACKs */ 403583d3bc6fSnarayan if ((msg->type & LDC_DATA) && (msg->stype & LDC_NACK)) { 403683d3bc6fSnarayan DWARN(ldcp->id, 403758283286Sha137994 "ldc_read: (0x%llx) received DATA/NACK", 403858283286Sha137994 ldcp->id); 403983d3bc6fSnarayan mutex_enter(&ldcp->tx_lock); 404083d3bc6fSnarayan i_ldc_reset(ldcp, B_TRUE); 404183d3bc6fSnarayan mutex_exit(&ldcp->tx_lock); 404283d3bc6fSnarayan return (ECONNRESET); 404383d3bc6fSnarayan } 404458283286Sha137994 } 404583d3bc6fSnarayan 40461ae08745Sheppo /* process data messages */ 40471ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 40481ae08745Sheppo 40491ae08745Sheppo uint8_t *msgbuf = (uint8_t *)( 405020ae46ebSha137994 (ldcp->mode == LDC_MODE_RELIABLE) ? 405122f747efSnarayan msg->rdata : msg->udata); 40521ae08745Sheppo 40531ae08745Sheppo D2(ldcp->id, 40541ae08745Sheppo "ldc_read: (0x%llx) received data msg\n", ldcp->id); 40551ae08745Sheppo 40561ae08745Sheppo /* get the packet length */ 40571ae08745Sheppo len = (msg->env & LDC_LEN_MASK); 40581ae08745Sheppo 40591ae08745Sheppo /* 40601ae08745Sheppo * FUTURE OPTIMIZATION: 40611ae08745Sheppo * dont need to set q head for every 40621ae08745Sheppo * packet we read just need to do this when 40631ae08745Sheppo * we are done or need to wait for more 40641ae08745Sheppo * mondos to make a full packet - this is 40651ae08745Sheppo * currently expensive. 40661ae08745Sheppo */ 40671ae08745Sheppo 4068d10e4ef2Snarayan if (first_fragment == 0) { 40691ae08745Sheppo 40701ae08745Sheppo /* 40711ae08745Sheppo * first packets should always have the start 40721ae08745Sheppo * bit set (even for a single packet). If not 40731ae08745Sheppo * throw away the packet 40741ae08745Sheppo */ 40751ae08745Sheppo if (!(msg->env & LDC_FRAG_START)) { 40761ae08745Sheppo 40771ae08745Sheppo DWARN(DBG_ALL_LDCS, 40781ae08745Sheppo "ldc_read: (0x%llx) not start - " 40791ae08745Sheppo "frag=%x\n", ldcp->id, 40801ae08745Sheppo (msg->env) & LDC_FRAG_MASK); 40811ae08745Sheppo 40821ae08745Sheppo /* toss pkt, inc head, cont reading */ 40831ae08745Sheppo bytes_read = 0; 40841ae08745Sheppo target = target_bufp; 40851ae08745Sheppo curr_head = 40861ae08745Sheppo (curr_head + LDC_PACKET_SIZE) 40871ae08745Sheppo & q_size_mask; 408858283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, 40891ae08745Sheppo curr_head)) 40901ae08745Sheppo break; 40911ae08745Sheppo 40921ae08745Sheppo continue; 40931ae08745Sheppo } 40941ae08745Sheppo 4095d10e4ef2Snarayan first_fragment = msg->seqid; 40961ae08745Sheppo } else { 40971ae08745Sheppo /* check to see if this is a pkt w/ START bit */ 40981ae08745Sheppo if (msg->env & LDC_FRAG_START) { 40991ae08745Sheppo DWARN(DBG_ALL_LDCS, 41001ae08745Sheppo "ldc_read:(0x%llx) unexpected pkt" 41011ae08745Sheppo " env=0x%x discarding %d bytes," 41021ae08745Sheppo " lastmsg=%d, currentmsg=%d\n", 41031ae08745Sheppo ldcp->id, msg->env&LDC_FRAG_MASK, 41041ae08745Sheppo bytes_read, ldcp->last_msg_rcd, 41051ae08745Sheppo msg->seqid); 41061ae08745Sheppo 41071ae08745Sheppo /* throw data we have read so far */ 41081ae08745Sheppo bytes_read = 0; 41091ae08745Sheppo target = target_bufp; 4110d10e4ef2Snarayan first_fragment = msg->seqid; 41111ae08745Sheppo 411258283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, 41131ae08745Sheppo curr_head)) 41141ae08745Sheppo break; 41151ae08745Sheppo } 41161ae08745Sheppo } 41171ae08745Sheppo 41181ae08745Sheppo /* copy (next) pkt into buffer */ 41191ae08745Sheppo if (len <= (*sizep - bytes_read)) { 41201ae08745Sheppo bcopy(msgbuf, target, len); 41211ae08745Sheppo target += len; 41221ae08745Sheppo bytes_read += len; 41231ae08745Sheppo } else { 41241ae08745Sheppo /* 41251ae08745Sheppo * there is not enough space in the buffer to 41261ae08745Sheppo * read this pkt. throw message away & continue 41271ae08745Sheppo * reading data from queue 41281ae08745Sheppo */ 41291ae08745Sheppo DWARN(DBG_ALL_LDCS, 41301ae08745Sheppo "ldc_read: (0x%llx) buffer too small, " 41311ae08745Sheppo "head=0x%lx, expect=%d, got=%d\n", ldcp->id, 41321ae08745Sheppo curr_head, *sizep, bytes_read+len); 41331ae08745Sheppo 4134d10e4ef2Snarayan first_fragment = 0; 41351ae08745Sheppo target = target_bufp; 41361ae08745Sheppo bytes_read = 0; 41371ae08745Sheppo 41381ae08745Sheppo /* throw away everything received so far */ 413958283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, curr_head)) 41401ae08745Sheppo break; 41411ae08745Sheppo 41421ae08745Sheppo /* continue reading remaining pkts */ 41431ae08745Sheppo continue; 41441ae08745Sheppo } 41451ae08745Sheppo } 41461ae08745Sheppo 41471ae08745Sheppo /* set the message id */ 414820ae46ebSha137994 if (ldcp->mode != LDC_MODE_RELIABLE) 41491ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 41501ae08745Sheppo 41511ae08745Sheppo /* move the head one position */ 41521ae08745Sheppo curr_head = (curr_head + LDC_PACKET_SIZE) & q_size_mask; 41531ae08745Sheppo 41541ae08745Sheppo if (msg->env & LDC_FRAG_STOP) { 41551ae08745Sheppo 41561ae08745Sheppo /* 41571ae08745Sheppo * All pkts that are part of this fragmented transfer 41581ae08745Sheppo * have been read or this was a single pkt read 41591ae08745Sheppo * or there was an error 41601ae08745Sheppo */ 41611ae08745Sheppo 41621ae08745Sheppo /* set the queue head */ 416358283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, curr_head)) 41641ae08745Sheppo bytes_read = 0; 41651ae08745Sheppo 41661ae08745Sheppo *sizep = bytes_read; 41671ae08745Sheppo 41681ae08745Sheppo break; 41691ae08745Sheppo } 41701ae08745Sheppo 4171332608acSnarayan /* advance head if it is a CTRL packet or a DATA ACK packet */ 4172332608acSnarayan if ((msg->type & LDC_CTRL) || 4173332608acSnarayan ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK))) { 41741ae08745Sheppo 41751ae08745Sheppo /* set the queue head */ 417658283286Sha137994 if (rv = ldcp->readq_set_head(ldcp, curr_head)) { 41771ae08745Sheppo bytes_read = 0; 41781ae08745Sheppo break; 41791ae08745Sheppo } 41801ae08745Sheppo 41811ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) set ACK qhead 0x%llx", 41821ae08745Sheppo ldcp->id, curr_head); 41831ae08745Sheppo } 41841ae08745Sheppo 41851ae08745Sheppo } /* for (;;) */ 41861ae08745Sheppo 41871ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) end size=%d", ldcp->id, *sizep); 41881ae08745Sheppo 41891ae08745Sheppo return (rv); 41903af08d82Slm66018 41913af08d82Slm66018 channel_is_reset: 41923af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 41933af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 41943af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 41953af08d82Slm66018 return (ECONNRESET); 41961ae08745Sheppo } 41971ae08745Sheppo 41981ae08745Sheppo /* 419920ae46ebSha137994 * Fetch and buffer incoming packets so we can hand them back as 42001ae08745Sheppo * a basic byte stream. 42011ae08745Sheppo * 42021ae08745Sheppo * Enter and exit with ldcp->lock held by caller 42031ae08745Sheppo */ 42041ae08745Sheppo static int 42051ae08745Sheppo i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 42061ae08745Sheppo { 42071ae08745Sheppo int rv; 42081ae08745Sheppo size_t size; 42091ae08745Sheppo 42101ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 42111ae08745Sheppo 42121ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) buffer size=%d", 42131ae08745Sheppo ldcp->id, *sizep); 42141ae08745Sheppo 42151ae08745Sheppo if (ldcp->stream_remains == 0) { 42161ae08745Sheppo size = ldcp->mtu; 42171ae08745Sheppo rv = i_ldc_read_packet(ldcp, 42181ae08745Sheppo (caddr_t)ldcp->stream_bufferp, &size); 42191ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: read packet (0x%llx) size=%d", 42201ae08745Sheppo ldcp->id, size); 42211ae08745Sheppo 42221ae08745Sheppo if (rv != 0) 42231ae08745Sheppo return (rv); 42241ae08745Sheppo 42251ae08745Sheppo ldcp->stream_remains = size; 42261ae08745Sheppo ldcp->stream_offset = 0; 42271ae08745Sheppo } 42281ae08745Sheppo 42291ae08745Sheppo size = MIN(ldcp->stream_remains, *sizep); 42301ae08745Sheppo 42311ae08745Sheppo bcopy(ldcp->stream_bufferp + ldcp->stream_offset, target_bufp, size); 42321ae08745Sheppo ldcp->stream_offset += size; 42331ae08745Sheppo ldcp->stream_remains -= size; 42341ae08745Sheppo 42351ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) fill from buffer size=%d", 42361ae08745Sheppo ldcp->id, size); 42371ae08745Sheppo 42381ae08745Sheppo *sizep = size; 42391ae08745Sheppo return (0); 42401ae08745Sheppo } 42411ae08745Sheppo 42421ae08745Sheppo /* 42431ae08745Sheppo * Write specified amount of bytes to the channel 42441ae08745Sheppo * in multiple pkts of pkt_payload size. Each 42451ae08745Sheppo * packet is tagged with an unique packet ID in 4246e1ebb9ecSlm66018 * the case of a reliable link. 42471ae08745Sheppo * 42481ae08745Sheppo * On return, size contains the number of bytes written. 42491ae08745Sheppo */ 42501ae08745Sheppo int 42511ae08745Sheppo ldc_write(ldc_handle_t handle, caddr_t buf, size_t *sizep) 42521ae08745Sheppo { 42531ae08745Sheppo ldc_chan_t *ldcp; 42541ae08745Sheppo int rv = 0; 42551ae08745Sheppo 42561ae08745Sheppo if (handle == NULL) { 42571ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_write: invalid channel handle\n"); 42581ae08745Sheppo return (EINVAL); 42591ae08745Sheppo } 42601ae08745Sheppo ldcp = (ldc_chan_t *)handle; 42611ae08745Sheppo 4262d10e4ef2Snarayan /* check if writes can occur */ 4263d10e4ef2Snarayan if (!mutex_tryenter(&ldcp->tx_lock)) { 4264d10e4ef2Snarayan /* 4265d10e4ef2Snarayan * Could not get the lock - channel could 4266d10e4ef2Snarayan * be in the process of being unconfigured 4267d10e4ef2Snarayan * or reader has encountered an error 4268d10e4ef2Snarayan */ 4269d10e4ef2Snarayan return (EAGAIN); 4270d10e4ef2Snarayan } 42711ae08745Sheppo 42721ae08745Sheppo /* check if non-zero data to write */ 42731ae08745Sheppo if (buf == NULL || sizep == NULL) { 42741ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) invalid data write\n", 42751ae08745Sheppo ldcp->id); 4276d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 42771ae08745Sheppo return (EINVAL); 42781ae08745Sheppo } 42791ae08745Sheppo 42801ae08745Sheppo if (*sizep == 0) { 42811ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write size of zero\n", 42821ae08745Sheppo ldcp->id); 4283d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 42841ae08745Sheppo return (0); 42851ae08745Sheppo } 42861ae08745Sheppo 42871ae08745Sheppo /* Check if channel is UP for data exchange */ 42881ae08745Sheppo if (ldcp->tstate != TS_UP) { 42891ae08745Sheppo DWARN(ldcp->id, 42901ae08745Sheppo "ldc_write: (0x%llx) channel is not in UP state\n", 42911ae08745Sheppo ldcp->id); 42921ae08745Sheppo *sizep = 0; 42931ae08745Sheppo rv = ECONNRESET; 42941ae08745Sheppo } else { 42951ae08745Sheppo rv = ldcp->write_p(ldcp, buf, sizep); 42961ae08745Sheppo } 42971ae08745Sheppo 4298d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 42991ae08745Sheppo 43001ae08745Sheppo return (rv); 43011ae08745Sheppo } 43021ae08745Sheppo 43031ae08745Sheppo /* 43041ae08745Sheppo * Write a raw packet to the channel 43051ae08745Sheppo * On return, size contains the number of bytes written. 43061ae08745Sheppo */ 43071ae08745Sheppo static int 43081ae08745Sheppo i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 43091ae08745Sheppo { 43101ae08745Sheppo ldc_msg_t *ldcmsg; 43111ae08745Sheppo uint64_t tx_head, tx_tail, new_tail; 43121ae08745Sheppo int rv = 0; 43131ae08745Sheppo size_t size; 43141ae08745Sheppo 4315d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 43161ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RAW); 43171ae08745Sheppo 43181ae08745Sheppo size = *sizep; 43191ae08745Sheppo 43201ae08745Sheppo /* 43211ae08745Sheppo * Check to see if the packet size is less than or 43221ae08745Sheppo * equal to packet size support in raw mode 43231ae08745Sheppo */ 43241ae08745Sheppo if (size > ldcp->pkt_payload) { 43251ae08745Sheppo DWARN(ldcp->id, 43261ae08745Sheppo "ldc_write: (0x%llx) invalid size (0x%llx) for RAW mode\n", 43271ae08745Sheppo ldcp->id, *sizep); 43281ae08745Sheppo *sizep = 0; 43291ae08745Sheppo return (EMSGSIZE); 43301ae08745Sheppo } 43311ae08745Sheppo 43321ae08745Sheppo /* get the qptrs for the tx queue */ 43331ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 43341ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 43351ae08745Sheppo if (rv != 0) { 43361ae08745Sheppo cmn_err(CE_WARN, 43371ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 43381ae08745Sheppo *sizep = 0; 43391ae08745Sheppo return (EIO); 43401ae08745Sheppo } 43411ae08745Sheppo 43421ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 43431ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 43441ae08745Sheppo DWARN(ldcp->id, 43451ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 4346d10e4ef2Snarayan 43471ae08745Sheppo *sizep = 0; 4348d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 43493af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4350d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4351d10e4ef2Snarayan } else { 4352d10e4ef2Snarayan /* 4353d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4354d10e4ef2Snarayan * and Tx lock in correct order 4355d10e4ef2Snarayan */ 4356d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4357d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4358d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 43593af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4360d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4361d10e4ef2Snarayan } 43621ae08745Sheppo return (ECONNRESET); 43631ae08745Sheppo } 43641ae08745Sheppo 43651ae08745Sheppo tx_tail = ldcp->tx_tail; 43661ae08745Sheppo tx_head = ldcp->tx_head; 43671ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) & 43681ae08745Sheppo ((ldcp->tx_q_entries-1) << LDC_PACKET_SHIFT); 43691ae08745Sheppo 43701ae08745Sheppo if (new_tail == tx_head) { 43711ae08745Sheppo DWARN(DBG_ALL_LDCS, 43721ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 43731ae08745Sheppo *sizep = 0; 43741ae08745Sheppo return (EWOULDBLOCK); 43751ae08745Sheppo } 43761ae08745Sheppo 43771ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 43781ae08745Sheppo ldcp->id, size); 43791ae08745Sheppo 43801ae08745Sheppo /* Send the data now */ 43811ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 43821ae08745Sheppo 43831ae08745Sheppo /* copy the data into pkt */ 43841ae08745Sheppo bcopy((uint8_t *)buf, ldcmsg, size); 43851ae08745Sheppo 43861ae08745Sheppo /* increment tail */ 43871ae08745Sheppo tx_tail = new_tail; 43881ae08745Sheppo 43891ae08745Sheppo /* 43901ae08745Sheppo * All packets have been copied into the TX queue 43911ae08745Sheppo * update the tail ptr in the HV 43921ae08745Sheppo */ 43931ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 43941ae08745Sheppo if (rv) { 43951ae08745Sheppo if (rv == EWOULDBLOCK) { 43961ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write timed out\n", 43971ae08745Sheppo ldcp->id); 43981ae08745Sheppo *sizep = 0; 43991ae08745Sheppo return (EWOULDBLOCK); 44001ae08745Sheppo } 44011ae08745Sheppo 44021ae08745Sheppo *sizep = 0; 4403d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 44043af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4405d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4406d10e4ef2Snarayan } else { 4407d10e4ef2Snarayan /* 4408d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4409d10e4ef2Snarayan * and Tx lock in correct order 4410d10e4ef2Snarayan */ 4411d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4412d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4413d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 44143af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4415d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4416d10e4ef2Snarayan } 44171ae08745Sheppo return (ECONNRESET); 44181ae08745Sheppo } 44191ae08745Sheppo 44201ae08745Sheppo ldcp->tx_tail = tx_tail; 44211ae08745Sheppo *sizep = size; 44221ae08745Sheppo 44231ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, size); 44241ae08745Sheppo 44251ae08745Sheppo return (rv); 44261ae08745Sheppo } 44271ae08745Sheppo 44281ae08745Sheppo 44291ae08745Sheppo /* 44301ae08745Sheppo * Write specified amount of bytes to the channel 44311ae08745Sheppo * in multiple pkts of pkt_payload size. Each 44321ae08745Sheppo * packet is tagged with an unique packet ID in 4433e1ebb9ecSlm66018 * the case of a reliable link. 44341ae08745Sheppo * 44351ae08745Sheppo * On return, size contains the number of bytes written. 44361ae08745Sheppo * This function needs to ensure that the write size is < MTU size 44371ae08745Sheppo */ 44381ae08745Sheppo static int 44391ae08745Sheppo i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t buf, size_t *size) 44401ae08745Sheppo { 44411ae08745Sheppo ldc_msg_t *ldcmsg; 44421ae08745Sheppo uint64_t tx_head, tx_tail, new_tail, start; 44431ae08745Sheppo uint64_t txq_size_mask, numavail; 44441ae08745Sheppo uint8_t *msgbuf, *source = (uint8_t *)buf; 44451ae08745Sheppo size_t len, bytes_written = 0, remaining; 44461ae08745Sheppo int rv; 44471ae08745Sheppo uint32_t curr_seqid; 44481ae08745Sheppo 4449d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 44501ae08745Sheppo 44511ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RELIABLE || 445220ae46ebSha137994 ldcp->mode == LDC_MODE_UNRELIABLE); 44531ae08745Sheppo 44541ae08745Sheppo /* compute mask for increment */ 44551ae08745Sheppo txq_size_mask = (ldcp->tx_q_entries - 1) << LDC_PACKET_SHIFT; 44561ae08745Sheppo 44571ae08745Sheppo /* get the qptrs for the tx queue */ 44581ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 44591ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 44601ae08745Sheppo if (rv != 0) { 44611ae08745Sheppo cmn_err(CE_WARN, 44621ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 44631ae08745Sheppo *size = 0; 44641ae08745Sheppo return (EIO); 44651ae08745Sheppo } 44661ae08745Sheppo 44671ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 44681ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 44691ae08745Sheppo DWARN(ldcp->id, 44701ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 44711ae08745Sheppo *size = 0; 4472d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 44733af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4474d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4475d10e4ef2Snarayan } else { 4476d10e4ef2Snarayan /* 4477d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4478d10e4ef2Snarayan * and Tx lock in correct order 4479d10e4ef2Snarayan */ 4480d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4481d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4482d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 44833af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4484d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4485d10e4ef2Snarayan } 44861ae08745Sheppo return (ECONNRESET); 44871ae08745Sheppo } 44881ae08745Sheppo 44891ae08745Sheppo tx_tail = ldcp->tx_tail; 44901ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) % 44911ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 44921ae08745Sheppo 44931ae08745Sheppo /* 449422f747efSnarayan * Check to see if the queue is full. The check is done using 449522f747efSnarayan * the appropriate head based on the link mode. 44961ae08745Sheppo */ 449722f747efSnarayan i_ldc_get_tx_head(ldcp, &tx_head); 449822f747efSnarayan 44991ae08745Sheppo if (new_tail == tx_head) { 45001ae08745Sheppo DWARN(DBG_ALL_LDCS, 45011ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 45021ae08745Sheppo *size = 0; 45031ae08745Sheppo return (EWOULDBLOCK); 45041ae08745Sheppo } 45051ae08745Sheppo 45061ae08745Sheppo /* 45071ae08745Sheppo * Make sure that the LDC Tx queue has enough space 45081ae08745Sheppo */ 45091ae08745Sheppo numavail = (tx_head >> LDC_PACKET_SHIFT) - (tx_tail >> LDC_PACKET_SHIFT) 45101ae08745Sheppo + ldcp->tx_q_entries - 1; 45111ae08745Sheppo numavail %= ldcp->tx_q_entries; 45121ae08745Sheppo 45131ae08745Sheppo if (*size > (numavail * ldcp->pkt_payload)) { 45141ae08745Sheppo DWARN(DBG_ALL_LDCS, 45151ae08745Sheppo "ldc_write: (0x%llx) TX queue has no space\n", ldcp->id); 45161ae08745Sheppo return (EWOULDBLOCK); 45171ae08745Sheppo } 45181ae08745Sheppo 45191ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 45201ae08745Sheppo ldcp->id, *size); 45211ae08745Sheppo 45221ae08745Sheppo /* Send the data now */ 45231ae08745Sheppo bytes_written = 0; 45241ae08745Sheppo curr_seqid = ldcp->last_msg_snt; 45251ae08745Sheppo start = tx_tail; 45261ae08745Sheppo 45271ae08745Sheppo while (*size > bytes_written) { 45281ae08745Sheppo 45291ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 45301ae08745Sheppo 453120ae46ebSha137994 msgbuf = (uint8_t *)((ldcp->mode == LDC_MODE_RELIABLE) ? 453222f747efSnarayan ldcmsg->rdata : ldcmsg->udata); 45331ae08745Sheppo 45341ae08745Sheppo ldcmsg->type = LDC_DATA; 45351ae08745Sheppo ldcmsg->stype = LDC_INFO; 45361ae08745Sheppo ldcmsg->ctrl = 0; 45371ae08745Sheppo 45381ae08745Sheppo remaining = *size - bytes_written; 45391ae08745Sheppo len = min(ldcp->pkt_payload, remaining); 45401ae08745Sheppo ldcmsg->env = (uint8_t)len; 45411ae08745Sheppo 45421ae08745Sheppo curr_seqid++; 45431ae08745Sheppo ldcmsg->seqid = curr_seqid; 45441ae08745Sheppo 45451ae08745Sheppo /* copy the data into pkt */ 45461ae08745Sheppo bcopy(source, msgbuf, len); 45471ae08745Sheppo 45481ae08745Sheppo source += len; 45491ae08745Sheppo bytes_written += len; 45501ae08745Sheppo 45511ae08745Sheppo /* increment tail */ 45521ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) & txq_size_mask; 45531ae08745Sheppo 45541ae08745Sheppo ASSERT(tx_tail != tx_head); 45551ae08745Sheppo } 45561ae08745Sheppo 45571ae08745Sheppo /* Set the start and stop bits */ 45581ae08745Sheppo ldcmsg->env |= LDC_FRAG_STOP; 45591ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + start); 45601ae08745Sheppo ldcmsg->env |= LDC_FRAG_START; 45611ae08745Sheppo 45621ae08745Sheppo /* 45631ae08745Sheppo * All packets have been copied into the TX queue 45641ae08745Sheppo * update the tail ptr in the HV 45651ae08745Sheppo */ 45661ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 45671ae08745Sheppo if (rv == 0) { 45681ae08745Sheppo ldcp->tx_tail = tx_tail; 45691ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 45701ae08745Sheppo *size = bytes_written; 45711ae08745Sheppo } else { 45721ae08745Sheppo int rv2; 45731ae08745Sheppo 45741ae08745Sheppo if (rv != EWOULDBLOCK) { 45751ae08745Sheppo *size = 0; 4576d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 45773af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4578d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4579d10e4ef2Snarayan } else { 4580d10e4ef2Snarayan /* 4581d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4582d10e4ef2Snarayan * and Tx lock in correct order 4583d10e4ef2Snarayan */ 4584d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4585d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4586d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 45873af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4588d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4589d10e4ef2Snarayan } 45901ae08745Sheppo return (ECONNRESET); 45911ae08745Sheppo } 45921ae08745Sheppo 4593cb112a14Slm66018 D1(ldcp->id, "hv_tx_set_tail returns 0x%x (head 0x%x, " 45941ae08745Sheppo "old tail 0x%x, new tail 0x%x, qsize=0x%x)\n", 45951ae08745Sheppo rv, ldcp->tx_head, ldcp->tx_tail, tx_tail, 45961ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 45971ae08745Sheppo 45981ae08745Sheppo rv2 = hv_ldc_tx_get_state(ldcp->id, 45991ae08745Sheppo &tx_head, &tx_tail, &ldcp->link_state); 46001ae08745Sheppo 4601cb112a14Slm66018 D1(ldcp->id, "hv_ldc_tx_get_state returns 0x%x " 46021ae08745Sheppo "(head 0x%x, tail 0x%x state 0x%x)\n", 46031ae08745Sheppo rv2, tx_head, tx_tail, ldcp->link_state); 46041ae08745Sheppo 46051ae08745Sheppo *size = 0; 46061ae08745Sheppo } 46071ae08745Sheppo 46081ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, *size); 46091ae08745Sheppo 46101ae08745Sheppo return (rv); 46111ae08745Sheppo } 46121ae08745Sheppo 46131ae08745Sheppo /* 46141ae08745Sheppo * Write specified amount of bytes to the channel 46151ae08745Sheppo * in multiple pkts of pkt_payload size. Each 46161ae08745Sheppo * packet is tagged with an unique packet ID in 4617e1ebb9ecSlm66018 * the case of a reliable link. 46181ae08745Sheppo * 46191ae08745Sheppo * On return, size contains the number of bytes written. 46201ae08745Sheppo * This function needs to ensure that the write size is < MTU size 46211ae08745Sheppo */ 46221ae08745Sheppo static int 46231ae08745Sheppo i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 46241ae08745Sheppo { 4625d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 462620ae46ebSha137994 ASSERT(ldcp->mode == LDC_MODE_RELIABLE); 46271ae08745Sheppo 46281ae08745Sheppo /* Truncate packet to max of MTU size */ 46291ae08745Sheppo if (*sizep > ldcp->mtu) *sizep = ldcp->mtu; 46301ae08745Sheppo return (i_ldc_write_packet(ldcp, buf, sizep)); 46311ae08745Sheppo } 46321ae08745Sheppo 46331ae08745Sheppo 46341ae08745Sheppo /* 46351ae08745Sheppo * Interfaces for channel nexus to register/unregister with LDC module 46361ae08745Sheppo * The nexus will register functions to be used to register individual 46371ae08745Sheppo * channels with the nexus and enable interrupts for the channels 46381ae08745Sheppo */ 46391ae08745Sheppo int 46401ae08745Sheppo ldc_register(ldc_cnex_t *cinfo) 46411ae08745Sheppo { 46421ae08745Sheppo ldc_chan_t *ldcp; 46431ae08745Sheppo 46441ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL || 46451ae08745Sheppo cinfo->reg_chan == NULL || cinfo->unreg_chan == NULL || 46461ae08745Sheppo cinfo->add_intr == NULL || cinfo->rem_intr == NULL || 46471ae08745Sheppo cinfo->clr_intr == NULL) { 46481ae08745Sheppo 46491ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_register: invalid nexus info\n"); 46501ae08745Sheppo return (EINVAL); 46511ae08745Sheppo } 46521ae08745Sheppo 46531ae08745Sheppo mutex_enter(&ldcssp->lock); 46541ae08745Sheppo 46551ae08745Sheppo /* nexus registration */ 46561ae08745Sheppo ldcssp->cinfo.dip = cinfo->dip; 46571ae08745Sheppo ldcssp->cinfo.reg_chan = cinfo->reg_chan; 46581ae08745Sheppo ldcssp->cinfo.unreg_chan = cinfo->unreg_chan; 46591ae08745Sheppo ldcssp->cinfo.add_intr = cinfo->add_intr; 46601ae08745Sheppo ldcssp->cinfo.rem_intr = cinfo->rem_intr; 46611ae08745Sheppo ldcssp->cinfo.clr_intr = cinfo->clr_intr; 46621ae08745Sheppo 46631ae08745Sheppo /* register any channels that might have been previously initialized */ 46641ae08745Sheppo ldcp = ldcssp->chan_list; 46651ae08745Sheppo while (ldcp) { 46661ae08745Sheppo if ((ldcp->tstate & TS_QCONF_RDY) && 46671ae08745Sheppo (ldcp->tstate & TS_CNEX_RDY) == 0) 46681ae08745Sheppo (void) i_ldc_register_channel(ldcp); 46691ae08745Sheppo 46701ae08745Sheppo ldcp = ldcp->next; 46711ae08745Sheppo } 46721ae08745Sheppo 46731ae08745Sheppo mutex_exit(&ldcssp->lock); 46741ae08745Sheppo 46751ae08745Sheppo return (0); 46761ae08745Sheppo } 46771ae08745Sheppo 46781ae08745Sheppo int 46791ae08745Sheppo ldc_unregister(ldc_cnex_t *cinfo) 46801ae08745Sheppo { 46811ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL) { 46821ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid nexus info\n"); 46831ae08745Sheppo return (EINVAL); 46841ae08745Sheppo } 46851ae08745Sheppo 46861ae08745Sheppo mutex_enter(&ldcssp->lock); 46871ae08745Sheppo 46881ae08745Sheppo if (cinfo->dip != ldcssp->cinfo.dip) { 46891ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid dip\n"); 46901ae08745Sheppo mutex_exit(&ldcssp->lock); 46911ae08745Sheppo return (EINVAL); 46921ae08745Sheppo } 46931ae08745Sheppo 46941ae08745Sheppo /* nexus unregister */ 46951ae08745Sheppo ldcssp->cinfo.dip = NULL; 46961ae08745Sheppo ldcssp->cinfo.reg_chan = NULL; 46971ae08745Sheppo ldcssp->cinfo.unreg_chan = NULL; 46981ae08745Sheppo ldcssp->cinfo.add_intr = NULL; 46991ae08745Sheppo ldcssp->cinfo.rem_intr = NULL; 47001ae08745Sheppo ldcssp->cinfo.clr_intr = NULL; 47011ae08745Sheppo 47021ae08745Sheppo mutex_exit(&ldcssp->lock); 47031ae08745Sheppo 47041ae08745Sheppo return (0); 47051ae08745Sheppo } 4706