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 /* 231ae08745Sheppo * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 241ae08745Sheppo * Use is subject to license terms. 251ae08745Sheppo */ 261ae08745Sheppo 271ae08745Sheppo #pragma ident "%Z%%M% %I% %E% SMI" 281ae08745Sheppo 291ae08745Sheppo /* 30e1ebb9ecSlm66018 * sun4v LDC Link Layer 311ae08745Sheppo */ 321ae08745Sheppo #include <sys/types.h> 331ae08745Sheppo #include <sys/file.h> 341ae08745Sheppo #include <sys/errno.h> 351ae08745Sheppo #include <sys/open.h> 361ae08745Sheppo #include <sys/cred.h> 371ae08745Sheppo #include <sys/kmem.h> 381ae08745Sheppo #include <sys/conf.h> 391ae08745Sheppo #include <sys/cmn_err.h> 401ae08745Sheppo #include <sys/ksynch.h> 411ae08745Sheppo #include <sys/modctl.h> 421ae08745Sheppo #include <sys/stat.h> /* needed for S_IFBLK and S_IFCHR */ 431ae08745Sheppo #include <sys/debug.h> 441ae08745Sheppo #include <sys/types.h> 451ae08745Sheppo #include <sys/cred.h> 461ae08745Sheppo #include <sys/promif.h> 471ae08745Sheppo #include <sys/ddi.h> 481ae08745Sheppo #include <sys/sunddi.h> 491ae08745Sheppo #include <sys/cyclic.h> 501ae08745Sheppo #include <sys/machsystm.h> 511ae08745Sheppo #include <sys/vm.h> 521ae08745Sheppo #include <sys/cpu.h> 531ae08745Sheppo #include <sys/intreg.h> 541ae08745Sheppo #include <sys/machcpuvar.h> 554bac2208Snarayan #include <sys/mmu.h> 564bac2208Snarayan #include <sys/pte.h> 574bac2208Snarayan #include <vm/hat.h> 584bac2208Snarayan #include <vm/as.h> 594bac2208Snarayan #include <vm/hat_sfmmu.h> 604bac2208Snarayan #include <sys/vm_machparam.h> 614bac2208Snarayan #include <vm/seg_kmem.h> 624bac2208Snarayan #include <vm/seg_kpm.h> 631ae08745Sheppo #include <sys/note.h> 641ae08745Sheppo #include <sys/ivintr.h> 651ae08745Sheppo #include <sys/hypervisor_api.h> 661ae08745Sheppo #include <sys/ldc.h> 671ae08745Sheppo #include <sys/ldc_impl.h> 681ae08745Sheppo #include <sys/cnex.h> 691ae08745Sheppo #include <sys/hsvc.h> 701ae08745Sheppo 711ae08745Sheppo /* Core internal functions */ 721ae08745Sheppo static int i_ldc_h2v_error(int h_error); 731ae08745Sheppo static int i_ldc_txq_reconf(ldc_chan_t *ldcp); 74*3af08d82Slm66018 static int i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset); 751ae08745Sheppo static void i_ldc_reset_state(ldc_chan_t *ldcp); 76*3af08d82Slm66018 static void i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset); 771ae08745Sheppo 781ae08745Sheppo static int i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail); 791ae08745Sheppo static int i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail); 801ae08745Sheppo static int i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head); 811ae08745Sheppo static int i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype, 821ae08745Sheppo uint8_t ctrlmsg); 831ae08745Sheppo 841ae08745Sheppo /* Interrupt handling functions */ 851ae08745Sheppo static uint_t i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2); 861ae08745Sheppo static uint_t i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2); 871ae08745Sheppo static void i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype); 881ae08745Sheppo 891ae08745Sheppo /* Read method functions */ 901ae08745Sheppo static int i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep); 911ae08745Sheppo static int i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, 921ae08745Sheppo size_t *sizep); 931ae08745Sheppo static int i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, 941ae08745Sheppo size_t *sizep); 951ae08745Sheppo 961ae08745Sheppo /* Write method functions */ 971ae08745Sheppo static int i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t target_bufp, 981ae08745Sheppo size_t *sizep); 991ae08745Sheppo static int i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t target_bufp, 1001ae08745Sheppo size_t *sizep); 1011ae08745Sheppo static int i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t target_bufp, 1021ae08745Sheppo size_t *sizep); 1031ae08745Sheppo 1041ae08745Sheppo /* Pkt processing internal functions */ 1051ae08745Sheppo static int i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg); 1061ae08745Sheppo static int i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg); 1071ae08745Sheppo static int i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg); 1081ae08745Sheppo static int i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg); 1091ae08745Sheppo static int i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg); 1101ae08745Sheppo static int i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg); 1111ae08745Sheppo static int i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg); 1121ae08745Sheppo 1131ae08745Sheppo /* Memory synchronization internal functions */ 1141ae08745Sheppo static int i_ldc_mem_acquire_release(ldc_mem_handle_t mhandle, 1151ae08745Sheppo uint8_t direction, uint64_t offset, size_t size); 1161ae08745Sheppo static int i_ldc_dring_acquire_release(ldc_dring_handle_t dhandle, 1171ae08745Sheppo uint8_t direction, uint64_t start, uint64_t end); 1181ae08745Sheppo 1191ae08745Sheppo /* LDC Version */ 1201ae08745Sheppo static ldc_ver_t ldc_versions[] = { {1, 0} }; 1211ae08745Sheppo 1221ae08745Sheppo /* number of supported versions */ 1231ae08745Sheppo #define LDC_NUM_VERS (sizeof (ldc_versions) / sizeof (ldc_versions[0])) 1241ae08745Sheppo 1251ae08745Sheppo /* Module State Pointer */ 1261ae08745Sheppo static ldc_soft_state_t *ldcssp; 1271ae08745Sheppo 1281ae08745Sheppo static struct modldrv md = { 1291ae08745Sheppo &mod_miscops, /* This is a misc module */ 1301ae08745Sheppo "sun4v LDC module v%I%", /* Name of the module */ 1311ae08745Sheppo }; 1321ae08745Sheppo 1331ae08745Sheppo static struct modlinkage ml = { 1341ae08745Sheppo MODREV_1, 1351ae08745Sheppo &md, 1361ae08745Sheppo NULL 1371ae08745Sheppo }; 1381ae08745Sheppo 1391ae08745Sheppo static uint64_t ldc_sup_minor; /* Supported minor number */ 1401ae08745Sheppo static hsvc_info_t ldc_hsvc = { 1411ae08745Sheppo HSVC_REV_1, NULL, HSVC_GROUP_LDC, 1, 0, "ldc" 1421ae08745Sheppo }; 1431ae08745Sheppo 1441ae08745Sheppo static uint64_t intr_sup_minor; /* Supported minor number */ 1451ae08745Sheppo static hsvc_info_t intr_hsvc = { 1461ae08745Sheppo HSVC_REV_1, NULL, HSVC_GROUP_INTR, 1, 0, "ldc" 1471ae08745Sheppo }; 1481ae08745Sheppo 1494bac2208Snarayan /* 1504bac2208Snarayan * LDC framework supports mapping remote domain's memory 1514bac2208Snarayan * either directly or via shadow memory pages. Default 1524bac2208Snarayan * support is currently implemented via shadow copy. 1534bac2208Snarayan * Direct map can be enabled by setting 'ldc_shmem_enabled' 1544bac2208Snarayan */ 1554bac2208Snarayan int ldc_shmem_enabled = 0; 156e1ebb9ecSlm66018 1570a55fbb7Slm66018 /* 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 /* 172e1ebb9ecSlm66018 * Pages exported for remote access over each channel is 173e1ebb9ecSlm66018 * maintained in a table registered with the Hypervisor. 174e1ebb9ecSlm66018 * The default number of entries in the table is set to 175e1ebb9ecSlm66018 * 'ldc_mtbl_entries'. 176e1ebb9ecSlm66018 */ 177e1ebb9ecSlm66018 uint64_t ldc_maptable_entries = LDC_MTBL_ENTRIES; 178e1ebb9ecSlm66018 179e1ebb9ecSlm66018 /* 180e1ebb9ecSlm66018 * LDC retry count and delay - when the HV returns EWOULDBLOCK 181e1ebb9ecSlm66018 * the operation is retried 'ldc_max_retries' times with a 182e1ebb9ecSlm66018 * wait of 'ldc_delay' usecs between each retry. 1830a55fbb7Slm66018 */ 1840a55fbb7Slm66018 int ldc_max_retries = LDC_MAX_RETRIES; 1850a55fbb7Slm66018 clock_t ldc_delay = LDC_DELAY; 1860a55fbb7Slm66018 1871ae08745Sheppo #ifdef DEBUG 1881ae08745Sheppo 1891ae08745Sheppo /* 1901ae08745Sheppo * Print debug messages 1911ae08745Sheppo * 1921ae08745Sheppo * set ldcdbg to 0x7 for enabling all msgs 1931ae08745Sheppo * 0x4 - Warnings 1941ae08745Sheppo * 0x2 - All debug messages 1951ae08745Sheppo * 0x1 - Minimal debug messages 1961ae08745Sheppo * 1971ae08745Sheppo * set ldcdbgchan to the channel number you want to debug 1981ae08745Sheppo * setting it to -1 prints debug messages for all channels 1991ae08745Sheppo * NOTE: ldcdbgchan has no effect on error messages 2001ae08745Sheppo */ 2011ae08745Sheppo 2021ae08745Sheppo #define DBG_ALL_LDCS -1 2031ae08745Sheppo 2041ae08745Sheppo int ldcdbg = 0x0; 2051ae08745Sheppo int64_t ldcdbgchan = DBG_ALL_LDCS; 206*3af08d82Slm66018 boolean_t ldc_inject_reset_flag = B_FALSE; 2071ae08745Sheppo 2081ae08745Sheppo static void 2091ae08745Sheppo ldcdebug(int64_t id, const char *fmt, ...) 2101ae08745Sheppo { 2111ae08745Sheppo char buf[512]; 2121ae08745Sheppo va_list ap; 2131ae08745Sheppo 2141ae08745Sheppo /* 2151ae08745Sheppo * Do not return if, 2161ae08745Sheppo * caller wants to print it anyway - (id == DBG_ALL_LDCS) 2171ae08745Sheppo * debug channel is set to all LDCs - (ldcdbgchan == DBG_ALL_LDCS) 2181ae08745Sheppo * debug channel = caller specified channel 2191ae08745Sheppo */ 2201ae08745Sheppo if ((id != DBG_ALL_LDCS) && 2211ae08745Sheppo (ldcdbgchan != DBG_ALL_LDCS) && 2221ae08745Sheppo (ldcdbgchan != id)) { 2231ae08745Sheppo return; 2241ae08745Sheppo } 2251ae08745Sheppo 2261ae08745Sheppo va_start(ap, fmt); 2271ae08745Sheppo (void) vsprintf(buf, fmt, ap); 2281ae08745Sheppo va_end(ap); 2291ae08745Sheppo 230*3af08d82Slm66018 cmn_err(CE_CONT, "?%s", buf); 231*3af08d82Slm66018 } 232*3af08d82Slm66018 233*3af08d82Slm66018 static boolean_t 234*3af08d82Slm66018 ldc_inject_reset(ldc_chan_t *ldcp) 235*3af08d82Slm66018 { 236*3af08d82Slm66018 if ((ldcdbgchan != DBG_ALL_LDCS) && (ldcdbgchan != ldcp->id)) 237*3af08d82Slm66018 return (B_FALSE); 238*3af08d82Slm66018 239*3af08d82Slm66018 if (!ldc_inject_reset_flag) 240*3af08d82Slm66018 return (B_FALSE); 241*3af08d82Slm66018 242*3af08d82Slm66018 /* clear the injection state */ 243*3af08d82Slm66018 ldc_inject_reset_flag = 0; 244*3af08d82Slm66018 245*3af08d82Slm66018 return (B_TRUE); 2461ae08745Sheppo } 2471ae08745Sheppo 2481ae08745Sheppo #define D1 \ 2491ae08745Sheppo if (ldcdbg & 0x01) \ 2501ae08745Sheppo ldcdebug 2511ae08745Sheppo 2521ae08745Sheppo #define D2 \ 2531ae08745Sheppo if (ldcdbg & 0x02) \ 2541ae08745Sheppo ldcdebug 2551ae08745Sheppo 2561ae08745Sheppo #define DWARN \ 2571ae08745Sheppo if (ldcdbg & 0x04) \ 2581ae08745Sheppo ldcdebug 2591ae08745Sheppo 2601ae08745Sheppo #define DUMP_PAYLOAD(id, addr) \ 2611ae08745Sheppo { \ 2621ae08745Sheppo char buf[65*3]; \ 2631ae08745Sheppo int i; \ 2641ae08745Sheppo uint8_t *src = (uint8_t *)addr; \ 2651ae08745Sheppo for (i = 0; i < 64; i++, src++) \ 2661ae08745Sheppo (void) sprintf(&buf[i * 3], "|%02x", *src); \ 2671ae08745Sheppo (void) sprintf(&buf[i * 3], "|\n"); \ 2681ae08745Sheppo D2((id), "payload: %s", buf); \ 2691ae08745Sheppo } 2701ae08745Sheppo 2711ae08745Sheppo #define DUMP_LDC_PKT(c, s, addr) \ 2721ae08745Sheppo { \ 2731ae08745Sheppo ldc_msg_t *msg = (ldc_msg_t *)(addr); \ 2741ae08745Sheppo uint32_t mid = ((c)->mode != LDC_MODE_RAW) ? msg->seqid : 0; \ 2751ae08745Sheppo if (msg->type == LDC_DATA) { \ 2761ae08745Sheppo D2((c)->id, "%s: msg%d (/%x/%x/%x/,env[%c%c,sz=%d])", \ 2771ae08745Sheppo (s), mid, msg->type, msg->stype, msg->ctrl, \ 2781ae08745Sheppo (msg->env & LDC_FRAG_START) ? 'B' : ' ', \ 2791ae08745Sheppo (msg->env & LDC_FRAG_STOP) ? 'E' : ' ', \ 2801ae08745Sheppo (msg->env & LDC_LEN_MASK)); \ 2811ae08745Sheppo } else { \ 2821ae08745Sheppo D2((c)->id, "%s: msg%d (/%x/%x/%x/,env=%x)", (s), \ 2831ae08745Sheppo mid, msg->type, msg->stype, msg->ctrl, msg->env); \ 2841ae08745Sheppo } \ 2851ae08745Sheppo } 2861ae08745Sheppo 287*3af08d82Slm66018 #define LDC_INJECT_RESET(_ldcp) ldc_inject_reset(_ldcp) 288*3af08d82Slm66018 2891ae08745Sheppo #else 2901ae08745Sheppo 2911ae08745Sheppo #define DBG_ALL_LDCS -1 2921ae08745Sheppo 2931ae08745Sheppo #define D1 2941ae08745Sheppo #define D2 2951ae08745Sheppo #define DWARN 2961ae08745Sheppo 2971ae08745Sheppo #define DUMP_PAYLOAD(id, addr) 2981ae08745Sheppo #define DUMP_LDC_PKT(c, s, addr) 2991ae08745Sheppo 300*3af08d82Slm66018 #define LDC_INJECT_RESET(_ldcp) (B_FALSE) 301*3af08d82Slm66018 3021ae08745Sheppo #endif 3031ae08745Sheppo 3041ae08745Sheppo #define ZERO_PKT(p) \ 3051ae08745Sheppo bzero((p), sizeof (ldc_msg_t)); 3061ae08745Sheppo 3071ae08745Sheppo #define IDX2COOKIE(idx, pg_szc, pg_shift) \ 3081ae08745Sheppo (((pg_szc) << LDC_COOKIE_PGSZC_SHIFT) | ((idx) << (pg_shift))) 3091ae08745Sheppo 3101ae08745Sheppo 3111ae08745Sheppo int 3121ae08745Sheppo _init(void) 3131ae08745Sheppo { 3141ae08745Sheppo int status; 3151ae08745Sheppo 3161ae08745Sheppo status = hsvc_register(&ldc_hsvc, &ldc_sup_minor); 3171ae08745Sheppo if (status != 0) { 3181ae08745Sheppo cmn_err(CE_WARN, "%s: cannot negotiate hypervisor LDC services" 3191ae08745Sheppo " group: 0x%lx major: %ld minor: %ld errno: %d", 3201ae08745Sheppo ldc_hsvc.hsvc_modname, ldc_hsvc.hsvc_group, 3211ae08745Sheppo ldc_hsvc.hsvc_major, ldc_hsvc.hsvc_minor, status); 3221ae08745Sheppo return (-1); 3231ae08745Sheppo } 3241ae08745Sheppo 3251ae08745Sheppo status = hsvc_register(&intr_hsvc, &intr_sup_minor); 3261ae08745Sheppo if (status != 0) { 3271ae08745Sheppo cmn_err(CE_WARN, "%s: cannot negotiate hypervisor interrupt " 3281ae08745Sheppo "services group: 0x%lx major: %ld minor: %ld errno: %d", 3291ae08745Sheppo intr_hsvc.hsvc_modname, intr_hsvc.hsvc_group, 3301ae08745Sheppo intr_hsvc.hsvc_major, intr_hsvc.hsvc_minor, status); 3311ae08745Sheppo (void) hsvc_unregister(&ldc_hsvc); 3321ae08745Sheppo return (-1); 3331ae08745Sheppo } 3341ae08745Sheppo 3351ae08745Sheppo /* allocate soft state structure */ 3361ae08745Sheppo ldcssp = kmem_zalloc(sizeof (ldc_soft_state_t), KM_SLEEP); 3371ae08745Sheppo 3381ae08745Sheppo /* Link the module into the system */ 3391ae08745Sheppo status = mod_install(&ml); 3401ae08745Sheppo if (status != 0) { 3411ae08745Sheppo kmem_free(ldcssp, sizeof (ldc_soft_state_t)); 3421ae08745Sheppo return (status); 3431ae08745Sheppo } 3441ae08745Sheppo 3451ae08745Sheppo /* Initialize the LDC state structure */ 3461ae08745Sheppo mutex_init(&ldcssp->lock, NULL, MUTEX_DRIVER, NULL); 3471ae08745Sheppo 3481ae08745Sheppo mutex_enter(&ldcssp->lock); 3491ae08745Sheppo 3504bac2208Snarayan /* Create a cache for memory handles */ 3514bac2208Snarayan ldcssp->memhdl_cache = kmem_cache_create("ldc_memhdl_cache", 3524bac2208Snarayan sizeof (ldc_mhdl_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 3534bac2208Snarayan if (ldcssp->memhdl_cache == NULL) { 3544bac2208Snarayan DWARN(DBG_ALL_LDCS, "_init: ldc_memhdl cache create failed\n"); 3554bac2208Snarayan mutex_exit(&ldcssp->lock); 3564bac2208Snarayan return (-1); 3574bac2208Snarayan } 3584bac2208Snarayan 3594bac2208Snarayan /* Create cache for memory segment structures */ 3604bac2208Snarayan ldcssp->memseg_cache = kmem_cache_create("ldc_memseg_cache", 3614bac2208Snarayan sizeof (ldc_memseg_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 3624bac2208Snarayan if (ldcssp->memseg_cache == NULL) { 3634bac2208Snarayan DWARN(DBG_ALL_LDCS, "_init: ldc_memseg cache create failed\n"); 3644bac2208Snarayan mutex_exit(&ldcssp->lock); 3654bac2208Snarayan return (-1); 3664bac2208Snarayan } 3674bac2208Snarayan 3684bac2208Snarayan 3691ae08745Sheppo ldcssp->channel_count = 0; 3701ae08745Sheppo ldcssp->channels_open = 0; 3711ae08745Sheppo ldcssp->chan_list = NULL; 3721ae08745Sheppo ldcssp->dring_list = NULL; 3731ae08745Sheppo 3741ae08745Sheppo mutex_exit(&ldcssp->lock); 3751ae08745Sheppo 3761ae08745Sheppo return (0); 3771ae08745Sheppo } 3781ae08745Sheppo 3791ae08745Sheppo int 3801ae08745Sheppo _info(struct modinfo *modinfop) 3811ae08745Sheppo { 3821ae08745Sheppo /* Report status of the dynamically loadable driver module */ 3831ae08745Sheppo return (mod_info(&ml, modinfop)); 3841ae08745Sheppo } 3851ae08745Sheppo 3861ae08745Sheppo int 3871ae08745Sheppo _fini(void) 3881ae08745Sheppo { 3891ae08745Sheppo int rv, status; 3901ae08745Sheppo ldc_chan_t *ldcp; 3911ae08745Sheppo ldc_dring_t *dringp; 3921ae08745Sheppo ldc_mem_info_t minfo; 3931ae08745Sheppo 3941ae08745Sheppo /* Unlink the driver module from the system */ 3951ae08745Sheppo status = mod_remove(&ml); 3961ae08745Sheppo if (status) { 3971ae08745Sheppo DWARN(DBG_ALL_LDCS, "_fini: mod_remove failed\n"); 3981ae08745Sheppo return (EIO); 3991ae08745Sheppo } 4001ae08745Sheppo 4011ae08745Sheppo /* close and finalize channels */ 4021ae08745Sheppo ldcp = ldcssp->chan_list; 4031ae08745Sheppo while (ldcp != NULL) { 4041ae08745Sheppo (void) ldc_close((ldc_handle_t)ldcp); 4051ae08745Sheppo (void) ldc_fini((ldc_handle_t)ldcp); 4061ae08745Sheppo 4071ae08745Sheppo ldcp = ldcp->next; 4081ae08745Sheppo } 4091ae08745Sheppo 4101ae08745Sheppo /* Free descriptor rings */ 4111ae08745Sheppo dringp = ldcssp->dring_list; 4121ae08745Sheppo while (dringp != NULL) { 4131ae08745Sheppo dringp = dringp->next; 4141ae08745Sheppo 4151ae08745Sheppo rv = ldc_mem_dring_info((ldc_dring_handle_t)dringp, &minfo); 4161ae08745Sheppo if (rv == 0 && minfo.status != LDC_UNBOUND) { 4171ae08745Sheppo if (minfo.status == LDC_BOUND) { 4181ae08745Sheppo (void) ldc_mem_dring_unbind( 4191ae08745Sheppo (ldc_dring_handle_t)dringp); 4201ae08745Sheppo } 4211ae08745Sheppo if (minfo.status == LDC_MAPPED) { 4221ae08745Sheppo (void) ldc_mem_dring_unmap( 4231ae08745Sheppo (ldc_dring_handle_t)dringp); 4241ae08745Sheppo } 4251ae08745Sheppo } 4261ae08745Sheppo 4271ae08745Sheppo (void) ldc_mem_dring_destroy((ldc_dring_handle_t)dringp); 4281ae08745Sheppo } 4291ae08745Sheppo ldcssp->dring_list = NULL; 4301ae08745Sheppo 4314bac2208Snarayan /* Destroy kmem caches */ 4324bac2208Snarayan kmem_cache_destroy(ldcssp->memhdl_cache); 4334bac2208Snarayan kmem_cache_destroy(ldcssp->memseg_cache); 4344bac2208Snarayan 4351ae08745Sheppo /* 4361ae08745Sheppo * We have successfully "removed" the driver. 4371ae08745Sheppo * Destroying soft states 4381ae08745Sheppo */ 4391ae08745Sheppo mutex_destroy(&ldcssp->lock); 4401ae08745Sheppo kmem_free(ldcssp, sizeof (ldc_soft_state_t)); 4411ae08745Sheppo 4421ae08745Sheppo (void) hsvc_unregister(&ldc_hsvc); 4431ae08745Sheppo (void) hsvc_unregister(&intr_hsvc); 4441ae08745Sheppo 4451ae08745Sheppo return (status); 4461ae08745Sheppo } 4471ae08745Sheppo 4481ae08745Sheppo /* -------------------------------------------------------------------------- */ 4491ae08745Sheppo 4501ae08745Sheppo /* 451e1ebb9ecSlm66018 * LDC Link Layer Internal Functions 4521ae08745Sheppo */ 4531ae08745Sheppo 4541ae08745Sheppo /* 4551ae08745Sheppo * Translate HV Errors to sun4v error codes 4561ae08745Sheppo */ 4571ae08745Sheppo static int 4581ae08745Sheppo i_ldc_h2v_error(int h_error) 4591ae08745Sheppo { 4601ae08745Sheppo switch (h_error) { 4611ae08745Sheppo 4621ae08745Sheppo case H_EOK: 4631ae08745Sheppo return (0); 4641ae08745Sheppo 4651ae08745Sheppo case H_ENORADDR: 4661ae08745Sheppo return (EFAULT); 4671ae08745Sheppo 4681ae08745Sheppo case H_EBADPGSZ: 4691ae08745Sheppo case H_EINVAL: 4701ae08745Sheppo return (EINVAL); 4711ae08745Sheppo 4721ae08745Sheppo case H_EWOULDBLOCK: 4731ae08745Sheppo return (EWOULDBLOCK); 4741ae08745Sheppo 4751ae08745Sheppo case H_ENOACCESS: 4761ae08745Sheppo case H_ENOMAP: 4771ae08745Sheppo return (EACCES); 4781ae08745Sheppo 4791ae08745Sheppo case H_EIO: 4801ae08745Sheppo case H_ECPUERROR: 4811ae08745Sheppo return (EIO); 4821ae08745Sheppo 4831ae08745Sheppo case H_ENOTSUPPORTED: 4841ae08745Sheppo return (ENOTSUP); 4851ae08745Sheppo 4861ae08745Sheppo case H_ETOOMANY: 4871ae08745Sheppo return (ENOSPC); 4881ae08745Sheppo 4891ae08745Sheppo case H_ECHANNEL: 4901ae08745Sheppo return (ECHRNG); 4911ae08745Sheppo default: 4921ae08745Sheppo break; 4931ae08745Sheppo } 4941ae08745Sheppo 4951ae08745Sheppo return (EIO); 4961ae08745Sheppo } 4971ae08745Sheppo 4981ae08745Sheppo /* 4991ae08745Sheppo * Reconfigure the transmit queue 5001ae08745Sheppo */ 5011ae08745Sheppo static int 5021ae08745Sheppo i_ldc_txq_reconf(ldc_chan_t *ldcp) 5031ae08745Sheppo { 5041ae08745Sheppo int rv; 5051ae08745Sheppo 5061ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 507d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 508d10e4ef2Snarayan 5091ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 5101ae08745Sheppo if (rv) { 5111ae08745Sheppo cmn_err(CE_WARN, 512*3af08d82Slm66018 "i_ldc_txq_reconf: (0x%lx) cannot set qconf", ldcp->id); 5131ae08745Sheppo return (EIO); 5141ae08745Sheppo } 5151ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &(ldcp->tx_head), 5161ae08745Sheppo &(ldcp->tx_tail), &(ldcp->link_state)); 5171ae08745Sheppo if (rv) { 5181ae08745Sheppo cmn_err(CE_WARN, 519*3af08d82Slm66018 "i_ldc_txq_reconf: (0x%lx) cannot get qptrs", ldcp->id); 5201ae08745Sheppo return (EIO); 5211ae08745Sheppo } 522*3af08d82Slm66018 D1(ldcp->id, "i_ldc_txq_reconf: (0x%llx) h=0x%llx,t=0x%llx," 5231ae08745Sheppo "s=0x%llx\n", ldcp->id, ldcp->tx_head, ldcp->tx_tail, 5241ae08745Sheppo ldcp->link_state); 5251ae08745Sheppo 5261ae08745Sheppo return (0); 5271ae08745Sheppo } 5281ae08745Sheppo 5291ae08745Sheppo /* 5301ae08745Sheppo * Reconfigure the receive queue 5311ae08745Sheppo */ 5321ae08745Sheppo static int 533*3af08d82Slm66018 i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset) 5341ae08745Sheppo { 5351ae08745Sheppo int rv; 5361ae08745Sheppo uint64_t rx_head, rx_tail; 5371ae08745Sheppo 5381ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 5391ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 5401ae08745Sheppo &(ldcp->link_state)); 5411ae08745Sheppo if (rv) { 5421ae08745Sheppo cmn_err(CE_WARN, 543*3af08d82Slm66018 "i_ldc_rxq_reconf: (0x%lx) cannot get state", 5441ae08745Sheppo ldcp->id); 5451ae08745Sheppo return (EIO); 5461ae08745Sheppo } 5471ae08745Sheppo 548*3af08d82Slm66018 if (force_reset || (ldcp->tstate & ~TS_IN_RESET) == TS_UP) { 5491ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, 5501ae08745Sheppo ldcp->rx_q_entries); 5511ae08745Sheppo if (rv) { 5521ae08745Sheppo cmn_err(CE_WARN, 553*3af08d82Slm66018 "i_ldc_rxq_reconf: (0x%lx) cannot set qconf", 5541ae08745Sheppo ldcp->id); 5551ae08745Sheppo return (EIO); 5561ae08745Sheppo } 557*3af08d82Slm66018 D1(ldcp->id, "i_ldc_rxq_reconf: (0x%llx) completed q reconf", 5581ae08745Sheppo ldcp->id); 5591ae08745Sheppo } 5601ae08745Sheppo 5611ae08745Sheppo return (0); 5621ae08745Sheppo } 5631ae08745Sheppo 5641ae08745Sheppo /* 5651ae08745Sheppo * Reset LDC state structure and its contents 5661ae08745Sheppo */ 5671ae08745Sheppo static void 5681ae08745Sheppo i_ldc_reset_state(ldc_chan_t *ldcp) 5691ae08745Sheppo { 5701ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 5711ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 5721ae08745Sheppo ldcp->last_ack_rcd = 0; 5731ae08745Sheppo ldcp->last_msg_rcd = 0; 5741ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 5751ae08745Sheppo ldcp->next_vidx = 0; 5761ae08745Sheppo ldcp->hstate = 0; 5771ae08745Sheppo ldcp->tstate = TS_OPEN; 5781ae08745Sheppo ldcp->status = LDC_OPEN; 5791ae08745Sheppo 5801ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 5811ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 5821ae08745Sheppo 5831ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 5841ae08745Sheppo ldcp->status = LDC_UP; 5851ae08745Sheppo ldcp->tstate = TS_UP; 5861ae08745Sheppo } else { 5871ae08745Sheppo ldcp->status = LDC_READY; 5881ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 5891ae08745Sheppo } 5901ae08745Sheppo } 5911ae08745Sheppo } 5921ae08745Sheppo 5931ae08745Sheppo /* 5941ae08745Sheppo * Reset a LDC channel 5951ae08745Sheppo */ 5961ae08745Sheppo static void 597*3af08d82Slm66018 i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset) 5981ae08745Sheppo { 599*3af08d82Slm66018 DWARN(ldcp->id, "i_ldc_reset: (0x%llx) channel reset\n", ldcp->id); 6001ae08745Sheppo 601d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->lock)); 602d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 603d10e4ef2Snarayan 604*3af08d82Slm66018 /* reconfig Tx and Rx queues */ 6051ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 606*3af08d82Slm66018 (void) i_ldc_rxq_reconf(ldcp, force_reset); 607*3af08d82Slm66018 608*3af08d82Slm66018 /* Clear Tx and Rx interrupts */ 609*3af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 610*3af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 611*3af08d82Slm66018 612*3af08d82Slm66018 /* Reset channel state */ 6131ae08745Sheppo i_ldc_reset_state(ldcp); 614*3af08d82Slm66018 615*3af08d82Slm66018 /* Mark channel in reset */ 616*3af08d82Slm66018 ldcp->tstate |= TS_IN_RESET; 6171ae08745Sheppo } 6181ae08745Sheppo 6194bac2208Snarayan 6201ae08745Sheppo /* 6211ae08745Sheppo * Clear pending interrupts 6221ae08745Sheppo */ 6231ae08745Sheppo static void 6241ae08745Sheppo i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype) 6251ae08745Sheppo { 6261ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 6271ae08745Sheppo 6281ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 629*3af08d82Slm66018 ASSERT(cinfo->dip != NULL); 6304bac2208Snarayan 631*3af08d82Slm66018 switch (itype) { 632*3af08d82Slm66018 case CNEX_TX_INTR: 6334bac2208Snarayan /* check Tx interrupt */ 634*3af08d82Slm66018 if (ldcp->tx_intr_state) 635*3af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_NONE; 6364bac2208Snarayan else 6374bac2208Snarayan return; 638*3af08d82Slm66018 break; 639*3af08d82Slm66018 640*3af08d82Slm66018 case CNEX_RX_INTR: 6414bac2208Snarayan /* check Rx interrupt */ 642*3af08d82Slm66018 if (ldcp->rx_intr_state) 643*3af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_NONE; 6444bac2208Snarayan else 6454bac2208Snarayan return; 646*3af08d82Slm66018 break; 6474bac2208Snarayan } 6484bac2208Snarayan 6491ae08745Sheppo (void) cinfo->clr_intr(cinfo->dip, ldcp->id, itype); 6504bac2208Snarayan D2(ldcp->id, 6514bac2208Snarayan "i_ldc_clear_intr: (0x%llx) cleared 0x%x intr\n", 6524bac2208Snarayan ldcp->id, itype); 6531ae08745Sheppo } 6541ae08745Sheppo 6551ae08745Sheppo /* 6561ae08745Sheppo * Set the receive queue head 6570a55fbb7Slm66018 * Resets connection and returns an error if it fails. 6581ae08745Sheppo */ 6591ae08745Sheppo static int 6601ae08745Sheppo i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head) 6611ae08745Sheppo { 6621ae08745Sheppo int rv; 6630a55fbb7Slm66018 int retries; 6641ae08745Sheppo 6651ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 6660a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 6670a55fbb7Slm66018 6680a55fbb7Slm66018 if ((rv = hv_ldc_rx_set_qhead(ldcp->id, head)) == 0) 6690a55fbb7Slm66018 return (0); 6700a55fbb7Slm66018 6710a55fbb7Slm66018 if (rv != H_EWOULDBLOCK) 6720a55fbb7Slm66018 break; 6730a55fbb7Slm66018 6740a55fbb7Slm66018 /* wait for ldc_delay usecs */ 6750a55fbb7Slm66018 drv_usecwait(ldc_delay); 6761ae08745Sheppo } 6771ae08745Sheppo 6780a55fbb7Slm66018 cmn_err(CE_WARN, "ldc_rx_set_qhead: (0x%lx) cannot set qhead 0x%lx", 6790a55fbb7Slm66018 ldcp->id, head); 680d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 681*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 682d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 6830a55fbb7Slm66018 6840a55fbb7Slm66018 return (ECONNRESET); 6851ae08745Sheppo } 6861ae08745Sheppo 6871ae08745Sheppo 6881ae08745Sheppo /* 6891ae08745Sheppo * Returns the tx_tail to be used for transfer 6901ae08745Sheppo * Re-reads the TX queue ptrs if and only if the 6911ae08745Sheppo * the cached head and tail are equal (queue is full) 6921ae08745Sheppo */ 6931ae08745Sheppo static int 6941ae08745Sheppo i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail) 6951ae08745Sheppo { 6961ae08745Sheppo int rv; 6971ae08745Sheppo uint64_t current_head, new_tail; 6981ae08745Sheppo 699d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 7001ae08745Sheppo /* Read the head and tail ptrs from HV */ 7011ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 7021ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 7031ae08745Sheppo if (rv) { 7041ae08745Sheppo cmn_err(CE_WARN, 7051ae08745Sheppo "i_ldc_get_tx_tail: (0x%lx) cannot read qptrs\n", 7061ae08745Sheppo ldcp->id); 7071ae08745Sheppo return (EIO); 7081ae08745Sheppo } 7091ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 7101ae08745Sheppo DWARN(DBG_ALL_LDCS, 7111ae08745Sheppo "i_ldc_get_tx_tail: (0x%llx) channel not ready\n", 7121ae08745Sheppo ldcp->id); 7131ae08745Sheppo return (ECONNRESET); 7141ae08745Sheppo } 7151ae08745Sheppo 7161ae08745Sheppo /* In reliable mode, check against last ACKd msg */ 7171ae08745Sheppo current_head = (ldcp->mode == LDC_MODE_RELIABLE || 7181ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 7191ae08745Sheppo ? ldcp->tx_ackd_head : ldcp->tx_head; 7201ae08745Sheppo 7211ae08745Sheppo /* increment the tail */ 7221ae08745Sheppo new_tail = (ldcp->tx_tail + LDC_PACKET_SIZE) % 7231ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 7241ae08745Sheppo 7251ae08745Sheppo if (new_tail == current_head) { 7261ae08745Sheppo DWARN(ldcp->id, 7271ae08745Sheppo "i_ldc_get_tx_tail: (0x%llx) TX queue is full\n", 7281ae08745Sheppo ldcp->id); 7291ae08745Sheppo return (EWOULDBLOCK); 7301ae08745Sheppo } 7311ae08745Sheppo 7321ae08745Sheppo D2(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) head=0x%llx, tail=0x%llx\n", 7331ae08745Sheppo ldcp->id, ldcp->tx_head, ldcp->tx_tail); 7341ae08745Sheppo 7351ae08745Sheppo *tail = ldcp->tx_tail; 7361ae08745Sheppo return (0); 7371ae08745Sheppo } 7381ae08745Sheppo 7391ae08745Sheppo /* 7401ae08745Sheppo * Set the tail pointer. If HV returns EWOULDBLOCK, it will back off 7410a55fbb7Slm66018 * and retry ldc_max_retries times before returning an error. 7421ae08745Sheppo * Returns 0, EWOULDBLOCK or EIO 7431ae08745Sheppo */ 7441ae08745Sheppo static int 7451ae08745Sheppo i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail) 7461ae08745Sheppo { 7471ae08745Sheppo int rv, retval = EWOULDBLOCK; 7480a55fbb7Slm66018 int retries; 7491ae08745Sheppo 750d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 7510a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 7521ae08745Sheppo 7531ae08745Sheppo if ((rv = hv_ldc_tx_set_qtail(ldcp->id, tail)) == 0) { 7541ae08745Sheppo retval = 0; 7551ae08745Sheppo break; 7561ae08745Sheppo } 7571ae08745Sheppo if (rv != H_EWOULDBLOCK) { 7581ae08745Sheppo DWARN(ldcp->id, "i_ldc_set_tx_tail: (0x%llx) set " 7591ae08745Sheppo "qtail=0x%llx failed, rv=%d\n", ldcp->id, tail, rv); 7601ae08745Sheppo retval = EIO; 7611ae08745Sheppo break; 7621ae08745Sheppo } 7631ae08745Sheppo 7640a55fbb7Slm66018 /* wait for ldc_delay usecs */ 7650a55fbb7Slm66018 drv_usecwait(ldc_delay); 7661ae08745Sheppo } 7671ae08745Sheppo return (retval); 7681ae08745Sheppo } 7691ae08745Sheppo 7701ae08745Sheppo /* 7711ae08745Sheppo * Send a LDC message 7721ae08745Sheppo */ 7731ae08745Sheppo static int 7741ae08745Sheppo i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype, 7751ae08745Sheppo uint8_t ctrlmsg) 7761ae08745Sheppo { 7771ae08745Sheppo int rv; 7781ae08745Sheppo ldc_msg_t *pkt; 7791ae08745Sheppo uint64_t tx_tail; 7801ae08745Sheppo uint32_t curr_seqid = ldcp->last_msg_snt; 7811ae08745Sheppo 782d10e4ef2Snarayan /* Obtain Tx lock */ 783d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 784d10e4ef2Snarayan 7851ae08745Sheppo /* get the current tail for the message */ 7861ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 7871ae08745Sheppo if (rv) { 7881ae08745Sheppo DWARN(ldcp->id, 7891ae08745Sheppo "i_ldc_send_pkt: (0x%llx) error sending pkt, " 7901ae08745Sheppo "type=0x%x,subtype=0x%x,ctrl=0x%x\n", 7911ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 792d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 7931ae08745Sheppo return (rv); 7941ae08745Sheppo } 7951ae08745Sheppo 7961ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 7971ae08745Sheppo ZERO_PKT(pkt); 7981ae08745Sheppo 7991ae08745Sheppo /* Initialize the packet */ 8001ae08745Sheppo pkt->type = pkttype; 8011ae08745Sheppo pkt->stype = subtype; 8021ae08745Sheppo pkt->ctrl = ctrlmsg; 8031ae08745Sheppo 8041ae08745Sheppo /* Store ackid/seqid iff it is RELIABLE mode & not a RTS/RTR message */ 8051ae08745Sheppo if (((ctrlmsg & LDC_CTRL_MASK) != LDC_RTS) && 8061ae08745Sheppo ((ctrlmsg & LDC_CTRL_MASK) != LDC_RTR)) { 8071ae08745Sheppo curr_seqid++; 8081ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) { 8091ae08745Sheppo pkt->seqid = curr_seqid; 8101ae08745Sheppo pkt->ackid = ldcp->last_msg_rcd; 8111ae08745Sheppo } 8121ae08745Sheppo } 8131ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_send_pkt", (uint64_t)pkt); 8141ae08745Sheppo 8151ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 8161ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 8171ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 8181ae08745Sheppo 8191ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 8201ae08745Sheppo if (rv) { 8211ae08745Sheppo DWARN(ldcp->id, 8221ae08745Sheppo "i_ldc_send_pkt:(0x%llx) error sending pkt, " 8231ae08745Sheppo "type=0x%x,stype=0x%x,ctrl=0x%x\n", 8241ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 825d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 8261ae08745Sheppo return (EIO); 8271ae08745Sheppo } 8281ae08745Sheppo 8291ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 8301ae08745Sheppo ldcp->tx_tail = tx_tail; 8311ae08745Sheppo 832d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 8331ae08745Sheppo return (0); 8341ae08745Sheppo } 8351ae08745Sheppo 8361ae08745Sheppo /* 8371ae08745Sheppo * Checks if packet was received in right order 838e1ebb9ecSlm66018 * in the case of a reliable link. 8391ae08745Sheppo * Returns 0 if in order, else EIO 8401ae08745Sheppo */ 8411ae08745Sheppo static int 8421ae08745Sheppo i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *msg) 8431ae08745Sheppo { 8441ae08745Sheppo /* No seqid checking for RAW mode */ 8451ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) 8461ae08745Sheppo return (0); 8471ae08745Sheppo 8481ae08745Sheppo /* No seqid checking for version, RTS, RTR message */ 8491ae08745Sheppo if (msg->ctrl == LDC_VER || 8501ae08745Sheppo msg->ctrl == LDC_RTS || 8511ae08745Sheppo msg->ctrl == LDC_RTR) 8521ae08745Sheppo return (0); 8531ae08745Sheppo 8541ae08745Sheppo /* Initial seqid to use is sent in RTS/RTR and saved in last_msg_rcd */ 8551ae08745Sheppo if (msg->seqid != (ldcp->last_msg_rcd + 1)) { 8561ae08745Sheppo DWARN(ldcp->id, 8571ae08745Sheppo "i_ldc_check_seqid: (0x%llx) out-of-order pkt, got 0x%x, " 8581ae08745Sheppo "expecting 0x%x\n", ldcp->id, msg->seqid, 8591ae08745Sheppo (ldcp->last_msg_rcd + 1)); 8601ae08745Sheppo return (EIO); 8611ae08745Sheppo } 8621ae08745Sheppo 8631ae08745Sheppo return (0); 8641ae08745Sheppo } 8651ae08745Sheppo 8661ae08745Sheppo 8671ae08745Sheppo /* 8681ae08745Sheppo * Process an incoming version ctrl message 8691ae08745Sheppo */ 8701ae08745Sheppo static int 8711ae08745Sheppo i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg) 8721ae08745Sheppo { 8731ae08745Sheppo int rv = 0, idx = ldcp->next_vidx; 8741ae08745Sheppo ldc_msg_t *pkt; 8751ae08745Sheppo uint64_t tx_tail; 8761ae08745Sheppo ldc_ver_t *rcvd_ver; 8771ae08745Sheppo 8781ae08745Sheppo /* get the received version */ 8791ae08745Sheppo rcvd_ver = (ldc_ver_t *)((uint64_t)msg + LDC_PAYLOAD_VER_OFF); 8801ae08745Sheppo 8811ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) received VER v%u.%u\n", 8821ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 8831ae08745Sheppo 884d10e4ef2Snarayan /* Obtain Tx lock */ 885d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 886d10e4ef2Snarayan 8871ae08745Sheppo switch (msg->stype) { 8881ae08745Sheppo case LDC_INFO: 8891ae08745Sheppo 890*3af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) { 891*3af08d82Slm66018 (void) i_ldc_txq_reconf(ldcp); 892*3af08d82Slm66018 i_ldc_reset_state(ldcp); 893*3af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 894*3af08d82Slm66018 return (EAGAIN); 895*3af08d82Slm66018 } 896*3af08d82Slm66018 8971ae08745Sheppo /* get the current tail and pkt for the response */ 8981ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 8991ae08745Sheppo if (rv != 0) { 9001ae08745Sheppo DWARN(ldcp->id, 9011ae08745Sheppo "i_ldc_process_VER: (0x%llx) err sending " 9021ae08745Sheppo "version ACK/NACK\n", ldcp->id); 903*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 904d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 9051ae08745Sheppo return (ECONNRESET); 9061ae08745Sheppo } 9071ae08745Sheppo 9081ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 9091ae08745Sheppo ZERO_PKT(pkt); 9101ae08745Sheppo 9111ae08745Sheppo /* initialize the packet */ 9121ae08745Sheppo pkt->type = LDC_CTRL; 9131ae08745Sheppo pkt->ctrl = LDC_VER; 9141ae08745Sheppo 9151ae08745Sheppo for (;;) { 9161ae08745Sheppo 9171ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: got %u.%u chk %u.%u\n", 9181ae08745Sheppo rcvd_ver->major, rcvd_ver->minor, 9191ae08745Sheppo ldc_versions[idx].major, ldc_versions[idx].minor); 9201ae08745Sheppo 9211ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 9221ae08745Sheppo /* major version match - ACK version */ 9231ae08745Sheppo pkt->stype = LDC_ACK; 9241ae08745Sheppo 9251ae08745Sheppo /* 9261ae08745Sheppo * lower minor version to the one this endpt 9271ae08745Sheppo * supports, if necessary 9281ae08745Sheppo */ 9291ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 9301ae08745Sheppo rcvd_ver->minor = 9311ae08745Sheppo ldc_versions[idx].minor; 9321ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 9331ae08745Sheppo 9341ae08745Sheppo break; 9351ae08745Sheppo } 9361ae08745Sheppo 9371ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 9381ae08745Sheppo 9391ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 9401ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 9411ae08745Sheppo ldc_versions[idx].major, 9421ae08745Sheppo ldc_versions[idx].minor); 9431ae08745Sheppo 9441ae08745Sheppo /* nack with next lower version */ 9451ae08745Sheppo pkt->stype = LDC_NACK; 9461ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 9471ae08745Sheppo sizeof (ldc_versions[idx])); 9481ae08745Sheppo ldcp->next_vidx = idx; 9491ae08745Sheppo break; 9501ae08745Sheppo } 9511ae08745Sheppo 9521ae08745Sheppo /* next major version */ 9531ae08745Sheppo idx++; 9541ae08745Sheppo 9551ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 9561ae08745Sheppo 9571ae08745Sheppo if (idx == LDC_NUM_VERS) { 9581ae08745Sheppo /* no version match - send NACK */ 9591ae08745Sheppo pkt->stype = LDC_NACK; 9601ae08745Sheppo bzero(pkt->udata, sizeof (ldc_ver_t)); 9611ae08745Sheppo ldcp->next_vidx = 0; 9621ae08745Sheppo break; 9631ae08745Sheppo } 9641ae08745Sheppo } 9651ae08745Sheppo 9661ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 9671ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 9681ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 9691ae08745Sheppo 9701ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 9711ae08745Sheppo if (rv == 0) { 9721ae08745Sheppo ldcp->tx_tail = tx_tail; 9731ae08745Sheppo if (pkt->stype == LDC_ACK) { 9741ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent" 9751ae08745Sheppo " version ACK\n", ldcp->id); 9761ae08745Sheppo /* Save the ACK'd version */ 9771ae08745Sheppo ldcp->version.major = rcvd_ver->major; 9781ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 9790a55fbb7Slm66018 ldcp->hstate |= TS_RCVD_VER; 9801ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 9811ae08745Sheppo DWARN(DBG_ALL_LDCS, 982*3af08d82Slm66018 "(0x%llx) Sent ACK, " 983*3af08d82Slm66018 "Agreed on version v%u.%u\n", 9841ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 9851ae08745Sheppo } 9861ae08745Sheppo } else { 9871ae08745Sheppo DWARN(ldcp->id, 9881ae08745Sheppo "i_ldc_process_VER: (0x%llx) error sending " 9891ae08745Sheppo "ACK/NACK\n", ldcp->id); 990*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 991d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 9921ae08745Sheppo return (ECONNRESET); 9931ae08745Sheppo } 9941ae08745Sheppo 9951ae08745Sheppo break; 9961ae08745Sheppo 9971ae08745Sheppo case LDC_ACK: 998*3af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) { 999*3af08d82Slm66018 if (ldcp->version.major != rcvd_ver->major || 1000*3af08d82Slm66018 ldcp->version.minor != rcvd_ver->minor) { 1001*3af08d82Slm66018 1002*3af08d82Slm66018 /* mismatched version - reset connection */ 1003*3af08d82Slm66018 DWARN(ldcp->id, 1004*3af08d82Slm66018 "i_ldc_process_VER: (0x%llx) recvd" 1005*3af08d82Slm66018 " ACK ver != sent ACK ver\n", ldcp->id); 1006*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1007*3af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 1008*3af08d82Slm66018 return (ECONNRESET); 1009*3af08d82Slm66018 } 1010*3af08d82Slm66018 } else { 10111ae08745Sheppo /* SUCCESS - we have agreed on a version */ 10121ae08745Sheppo ldcp->version.major = rcvd_ver->major; 10131ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 10141ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 1015*3af08d82Slm66018 } 10161ae08745Sheppo 1017*3af08d82Slm66018 DWARN(DBG_ALL_LDCS, 1018*3af08d82Slm66018 "(0x%llx) Got ACK, Agreed on version v%u.%u\n", 10191ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 10201ae08745Sheppo 10211ae08745Sheppo /* initiate RTS-RTR-RDX handshake */ 10221ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 10231ae08745Sheppo if (rv) { 10241ae08745Sheppo DWARN(ldcp->id, 10251ae08745Sheppo "i_ldc_process_VER: (0x%llx) cannot send RTS\n", 10261ae08745Sheppo ldcp->id); 1027*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1028d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10291ae08745Sheppo return (ECONNRESET); 10301ae08745Sheppo } 10311ae08745Sheppo 10321ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 10331ae08745Sheppo ZERO_PKT(pkt); 10341ae08745Sheppo 10351ae08745Sheppo pkt->type = LDC_CTRL; 10361ae08745Sheppo pkt->stype = LDC_INFO; 10371ae08745Sheppo pkt->ctrl = LDC_RTS; 10381ae08745Sheppo pkt->env = ldcp->mode; 10391ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 10401ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 10411ae08745Sheppo 10421ae08745Sheppo ldcp->last_msg_rcd = LDC_INIT_SEQID; 10431ae08745Sheppo 10441ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_VER snd rts", (uint64_t)pkt); 10451ae08745Sheppo 10461ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 10471ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 10481ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 10491ae08745Sheppo 10501ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 10511ae08745Sheppo if (rv) { 10521ae08745Sheppo D2(ldcp->id, 10531ae08745Sheppo "i_ldc_process_VER: (0x%llx) no listener\n", 10541ae08745Sheppo ldcp->id); 1055*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1056d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10571ae08745Sheppo return (ECONNRESET); 10581ae08745Sheppo } 10591ae08745Sheppo 10601ae08745Sheppo ldcp->tx_tail = tx_tail; 10611ae08745Sheppo ldcp->hstate |= TS_SENT_RTS; 10621ae08745Sheppo 10631ae08745Sheppo break; 10641ae08745Sheppo 10651ae08745Sheppo case LDC_NACK: 10661ae08745Sheppo /* check if version in NACK is zero */ 10671ae08745Sheppo if (rcvd_ver->major == 0 && rcvd_ver->minor == 0) { 10681ae08745Sheppo /* version handshake failure */ 10691ae08745Sheppo DWARN(DBG_ALL_LDCS, 10701ae08745Sheppo "i_ldc_process_VER: (0x%llx) no version match\n", 10711ae08745Sheppo ldcp->id); 1072*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1073d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10741ae08745Sheppo return (ECONNRESET); 10751ae08745Sheppo } 10761ae08745Sheppo 10771ae08745Sheppo /* get the current tail and pkt for the response */ 10781ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 10791ae08745Sheppo if (rv != 0) { 10801ae08745Sheppo cmn_err(CE_NOTE, 10811ae08745Sheppo "i_ldc_process_VER: (0x%lx) err sending " 10821ae08745Sheppo "version ACK/NACK\n", ldcp->id); 1083*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1084d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10851ae08745Sheppo return (ECONNRESET); 10861ae08745Sheppo } 10871ae08745Sheppo 10881ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 10891ae08745Sheppo ZERO_PKT(pkt); 10901ae08745Sheppo 10911ae08745Sheppo /* initialize the packet */ 10921ae08745Sheppo pkt->type = LDC_CTRL; 10931ae08745Sheppo pkt->ctrl = LDC_VER; 10941ae08745Sheppo pkt->stype = LDC_INFO; 10951ae08745Sheppo 10961ae08745Sheppo /* check ver in NACK msg has a match */ 10971ae08745Sheppo for (;;) { 10981ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 10991ae08745Sheppo /* 11001ae08745Sheppo * major version match - resubmit request 11011ae08745Sheppo * if lower minor version to the one this endpt 11021ae08745Sheppo * supports, if necessary 11031ae08745Sheppo */ 11041ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 11051ae08745Sheppo rcvd_ver->minor = 11061ae08745Sheppo ldc_versions[idx].minor; 11071ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 11081ae08745Sheppo break; 11091ae08745Sheppo 11101ae08745Sheppo } 11111ae08745Sheppo 11121ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 11131ae08745Sheppo 11141ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 11151ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 11161ae08745Sheppo ldc_versions[idx].major, 11171ae08745Sheppo ldc_versions[idx].minor); 11181ae08745Sheppo 11191ae08745Sheppo /* send next lower version */ 11201ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 11211ae08745Sheppo sizeof (ldc_versions[idx])); 11221ae08745Sheppo ldcp->next_vidx = idx; 11231ae08745Sheppo break; 11241ae08745Sheppo } 11251ae08745Sheppo 11261ae08745Sheppo /* next version */ 11271ae08745Sheppo idx++; 11281ae08745Sheppo 11291ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 11301ae08745Sheppo 11311ae08745Sheppo if (idx == LDC_NUM_VERS) { 11321ae08745Sheppo /* no version match - terminate */ 11331ae08745Sheppo ldcp->next_vidx = 0; 1134d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11351ae08745Sheppo return (ECONNRESET); 11361ae08745Sheppo } 11371ae08745Sheppo } 11381ae08745Sheppo 11391ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 11401ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 11411ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 11421ae08745Sheppo 11431ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 11441ae08745Sheppo if (rv == 0) { 11451ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent version" 11461ae08745Sheppo "INFO v%u.%u\n", ldcp->id, ldc_versions[idx].major, 11471ae08745Sheppo ldc_versions[idx].minor); 11481ae08745Sheppo ldcp->tx_tail = tx_tail; 11491ae08745Sheppo } else { 11501ae08745Sheppo cmn_err(CE_NOTE, 11511ae08745Sheppo "i_ldc_process_VER: (0x%lx) error sending version" 11521ae08745Sheppo "INFO\n", ldcp->id); 1153*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1154d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11551ae08745Sheppo return (ECONNRESET); 11561ae08745Sheppo } 11571ae08745Sheppo 11581ae08745Sheppo break; 11591ae08745Sheppo } 11601ae08745Sheppo 1161d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11621ae08745Sheppo return (rv); 11631ae08745Sheppo } 11641ae08745Sheppo 11651ae08745Sheppo 11661ae08745Sheppo /* 11671ae08745Sheppo * Process an incoming RTS ctrl message 11681ae08745Sheppo */ 11691ae08745Sheppo static int 11701ae08745Sheppo i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg) 11711ae08745Sheppo { 11721ae08745Sheppo int rv = 0; 11731ae08745Sheppo ldc_msg_t *pkt; 11741ae08745Sheppo uint64_t tx_tail; 11751ae08745Sheppo boolean_t sent_NACK = B_FALSE; 11761ae08745Sheppo 11771ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTS: (0x%llx) received RTS\n", ldcp->id); 11781ae08745Sheppo 11791ae08745Sheppo switch (msg->stype) { 11801ae08745Sheppo case LDC_NACK: 11811ae08745Sheppo DWARN(ldcp->id, 11821ae08745Sheppo "i_ldc_process_RTS: (0x%llx) RTS NACK received\n", 11831ae08745Sheppo ldcp->id); 11841ae08745Sheppo 11851ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1186d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1187*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1188d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11891ae08745Sheppo rv = ECONNRESET; 11901ae08745Sheppo break; 11911ae08745Sheppo 11921ae08745Sheppo case LDC_INFO: 11931ae08745Sheppo 11941ae08745Sheppo /* check mode */ 11951ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 11961ae08745Sheppo cmn_err(CE_NOTE, 11971ae08745Sheppo "i_ldc_process_RTS: (0x%lx) mode mismatch\n", 11981ae08745Sheppo ldcp->id); 11991ae08745Sheppo /* 12001ae08745Sheppo * send NACK in response to MODE message 12011ae08745Sheppo * get the current tail for the response 12021ae08745Sheppo */ 12031ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTS); 12041ae08745Sheppo if (rv) { 12051ae08745Sheppo /* if cannot send NACK - reset channel */ 1206d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1207*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1208d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12091ae08745Sheppo rv = ECONNRESET; 12101ae08745Sheppo break; 12111ae08745Sheppo } 12121ae08745Sheppo sent_NACK = B_TRUE; 12131ae08745Sheppo } 12141ae08745Sheppo break; 12151ae08745Sheppo default: 12161ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTS: (0x%llx) unexp ACK\n", 12171ae08745Sheppo ldcp->id); 1218d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1219*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1220d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12211ae08745Sheppo rv = ECONNRESET; 12221ae08745Sheppo break; 12231ae08745Sheppo } 12241ae08745Sheppo 12251ae08745Sheppo /* 12261ae08745Sheppo * If either the connection was reset (when rv != 0) or 12271ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 12281ae08745Sheppo * we dont want to consume the packet that came in but 12291ae08745Sheppo * not record that we received the RTS 12301ae08745Sheppo */ 12311ae08745Sheppo if (rv || sent_NACK) 12321ae08745Sheppo return (rv); 12331ae08745Sheppo 12341ae08745Sheppo /* record RTS received */ 12351ae08745Sheppo ldcp->hstate |= TS_RCVD_RTS; 12361ae08745Sheppo 12371ae08745Sheppo /* store initial SEQID info */ 12381ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 12391ae08745Sheppo 1240d10e4ef2Snarayan /* Obtain Tx lock */ 1241d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1242d10e4ef2Snarayan 12431ae08745Sheppo /* get the current tail for the response */ 12441ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 12451ae08745Sheppo if (rv != 0) { 12461ae08745Sheppo cmn_err(CE_NOTE, 12471ae08745Sheppo "i_ldc_process_RTS: (0x%lx) err sending RTR\n", 12481ae08745Sheppo ldcp->id); 1249*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1250d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12511ae08745Sheppo return (ECONNRESET); 12521ae08745Sheppo } 12531ae08745Sheppo 12541ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 12551ae08745Sheppo ZERO_PKT(pkt); 12561ae08745Sheppo 12571ae08745Sheppo /* initialize the packet */ 12581ae08745Sheppo pkt->type = LDC_CTRL; 12591ae08745Sheppo pkt->stype = LDC_INFO; 12601ae08745Sheppo pkt->ctrl = LDC_RTR; 12611ae08745Sheppo pkt->env = ldcp->mode; 12621ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 12631ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 12641ae08745Sheppo 12651ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 12661ae08745Sheppo 12671ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 12681ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 12691ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 12701ae08745Sheppo 12711ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 12721ae08745Sheppo if (rv == 0) { 12731ae08745Sheppo D2(ldcp->id, 12741ae08745Sheppo "i_ldc_process_RTS: (0x%llx) sent RTR\n", ldcp->id); 12751ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_RTS sent rtr", (uint64_t)pkt); 12761ae08745Sheppo 12771ae08745Sheppo ldcp->tx_tail = tx_tail; 12781ae08745Sheppo ldcp->hstate |= TS_SENT_RTR; 12791ae08745Sheppo 12801ae08745Sheppo } else { 12811ae08745Sheppo cmn_err(CE_NOTE, 12821ae08745Sheppo "i_ldc_process_RTS: (0x%lx) error sending RTR\n", 12831ae08745Sheppo ldcp->id); 1284*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1285d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12861ae08745Sheppo return (ECONNRESET); 12871ae08745Sheppo } 12881ae08745Sheppo 1289d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12901ae08745Sheppo return (0); 12911ae08745Sheppo } 12921ae08745Sheppo 12931ae08745Sheppo /* 12941ae08745Sheppo * Process an incoming RTR ctrl message 12951ae08745Sheppo */ 12961ae08745Sheppo static int 12971ae08745Sheppo i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg) 12981ae08745Sheppo { 12991ae08745Sheppo int rv = 0; 13001ae08745Sheppo boolean_t sent_NACK = B_FALSE; 13011ae08745Sheppo 13021ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTR: (0x%llx) received RTR\n", ldcp->id); 13031ae08745Sheppo 13041ae08745Sheppo switch (msg->stype) { 13051ae08745Sheppo case LDC_NACK: 13061ae08745Sheppo /* RTR NACK received */ 13071ae08745Sheppo DWARN(ldcp->id, 13081ae08745Sheppo "i_ldc_process_RTR: (0x%llx) RTR NACK received\n", 13091ae08745Sheppo ldcp->id); 13101ae08745Sheppo 13111ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1312d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1313*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1314d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13151ae08745Sheppo rv = ECONNRESET; 13161ae08745Sheppo 13171ae08745Sheppo break; 13181ae08745Sheppo 13191ae08745Sheppo case LDC_INFO: 13201ae08745Sheppo 13211ae08745Sheppo /* check mode */ 13221ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 13231ae08745Sheppo DWARN(ldcp->id, 13241ae08745Sheppo "i_ldc_process_RTR: (0x%llx) mode mismatch\n", 13251ae08745Sheppo ldcp->id); 13261ae08745Sheppo /* 13271ae08745Sheppo * send NACK in response to MODE message 13281ae08745Sheppo * get the current tail for the response 13291ae08745Sheppo */ 13301ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTR); 13311ae08745Sheppo if (rv) { 13321ae08745Sheppo /* if cannot send NACK - reset channel */ 1333d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1334*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1335d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13361ae08745Sheppo rv = ECONNRESET; 13371ae08745Sheppo break; 13381ae08745Sheppo } 13391ae08745Sheppo sent_NACK = B_TRUE; 13401ae08745Sheppo } 13411ae08745Sheppo break; 13421ae08745Sheppo 13431ae08745Sheppo default: 13441ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTR: (0x%llx) unexp ACK\n", 13451ae08745Sheppo ldcp->id); 13461ae08745Sheppo 13471ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1348d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1349*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1350d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13511ae08745Sheppo rv = ECONNRESET; 13521ae08745Sheppo break; 13531ae08745Sheppo } 13541ae08745Sheppo 13551ae08745Sheppo /* 13561ae08745Sheppo * If either the connection was reset (when rv != 0) or 13571ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 13581ae08745Sheppo * we dont want to consume the packet that came in but 13591ae08745Sheppo * not record that we received the RTR 13601ae08745Sheppo */ 13611ae08745Sheppo if (rv || sent_NACK) 13621ae08745Sheppo return (rv); 13631ae08745Sheppo 13641ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 13651ae08745Sheppo ldcp->hstate |= TS_RCVD_RTR; 13661ae08745Sheppo 13671ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_INFO, LDC_RDX); 13681ae08745Sheppo if (rv) { 13691ae08745Sheppo cmn_err(CE_NOTE, 13701ae08745Sheppo "i_ldc_process_RTR: (0x%lx) cannot send RDX\n", 13711ae08745Sheppo ldcp->id); 1372d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1373*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1374d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13751ae08745Sheppo return (ECONNRESET); 13761ae08745Sheppo } 13771ae08745Sheppo D2(ldcp->id, 13781ae08745Sheppo "i_ldc_process_RTR: (0x%llx) sent RDX\n", ldcp->id); 13791ae08745Sheppo 13801ae08745Sheppo ldcp->hstate |= TS_SENT_RDX; 13811ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 1382*3af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 13831ae08745Sheppo ldcp->status = LDC_UP; 13841ae08745Sheppo 13851ae08745Sheppo DWARN(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id); 13861ae08745Sheppo 13871ae08745Sheppo return (0); 13881ae08745Sheppo } 13891ae08745Sheppo 13901ae08745Sheppo 13911ae08745Sheppo /* 13921ae08745Sheppo * Process an incoming RDX ctrl message 13931ae08745Sheppo */ 13941ae08745Sheppo static int 13951ae08745Sheppo i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg) 13961ae08745Sheppo { 13971ae08745Sheppo int rv = 0; 13981ae08745Sheppo 13991ae08745Sheppo D2(ldcp->id, "i_ldc_process_RDX: (0x%llx) received RDX\n", ldcp->id); 14001ae08745Sheppo 14011ae08745Sheppo switch (msg->stype) { 14021ae08745Sheppo case LDC_NACK: 14031ae08745Sheppo /* RDX NACK received */ 14041ae08745Sheppo DWARN(ldcp->id, 14051ae08745Sheppo "i_ldc_process_RDX: (0x%llx) RDX NACK received\n", 14061ae08745Sheppo ldcp->id); 14071ae08745Sheppo 14081ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1409d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1410*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1411d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14121ae08745Sheppo rv = ECONNRESET; 14131ae08745Sheppo 14141ae08745Sheppo break; 14151ae08745Sheppo 14161ae08745Sheppo case LDC_INFO: 14171ae08745Sheppo 14181ae08745Sheppo /* 14191ae08745Sheppo * if channel is UP and a RDX received after data transmission 14201ae08745Sheppo * has commenced it is an error 14211ae08745Sheppo */ 14221ae08745Sheppo if ((ldcp->tstate == TS_UP) && (ldcp->hstate & TS_RCVD_RDX)) { 14231ae08745Sheppo DWARN(DBG_ALL_LDCS, 14241ae08745Sheppo "i_ldc_process_RDX: (0x%llx) unexpected RDX" 14251ae08745Sheppo " - LDC reset\n", ldcp->id); 1426d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1427*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1428d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14291ae08745Sheppo return (ECONNRESET); 14301ae08745Sheppo } 14311ae08745Sheppo 14321ae08745Sheppo ldcp->hstate |= TS_RCVD_RDX; 14331ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 1434*3af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 14351ae08745Sheppo ldcp->status = LDC_UP; 14361ae08745Sheppo 14371ae08745Sheppo D1(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id); 14381ae08745Sheppo break; 14391ae08745Sheppo 14401ae08745Sheppo default: 14411ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RDX: (0x%llx) unexp ACK\n", 14421ae08745Sheppo ldcp->id); 14431ae08745Sheppo 14441ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1445d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1446*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1447d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14481ae08745Sheppo rv = ECONNRESET; 14491ae08745Sheppo break; 14501ae08745Sheppo } 14511ae08745Sheppo 14521ae08745Sheppo return (rv); 14531ae08745Sheppo } 14541ae08745Sheppo 14551ae08745Sheppo /* 14561ae08745Sheppo * Process an incoming ACK for a data packet 14571ae08745Sheppo */ 14581ae08745Sheppo static int 14591ae08745Sheppo i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg) 14601ae08745Sheppo { 14611ae08745Sheppo int rv; 14621ae08745Sheppo uint64_t tx_head; 14631ae08745Sheppo ldc_msg_t *pkt; 14641ae08745Sheppo 1465d10e4ef2Snarayan /* Obtain Tx lock */ 1466d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1467d10e4ef2Snarayan 14681ae08745Sheppo /* 1469d10e4ef2Snarayan * Read the current Tx head and tail 14701ae08745Sheppo */ 14711ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 14721ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 14731ae08745Sheppo if (rv != 0) { 14741ae08745Sheppo cmn_err(CE_WARN, 14751ae08745Sheppo "i_ldc_process_data_ACK: (0x%lx) cannot read qptrs\n", 14761ae08745Sheppo ldcp->id); 1477d10e4ef2Snarayan 1478d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 1479*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1480d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1481d10e4ef2Snarayan return (ECONNRESET); 14821ae08745Sheppo } 14831ae08745Sheppo 14841ae08745Sheppo /* 14851ae08745Sheppo * loop from where the previous ACK location was to the 14861ae08745Sheppo * current head location. This is how far the HV has 14871ae08745Sheppo * actually send pkts. Pkts between head and tail are 14881ae08745Sheppo * yet to be sent by HV. 14891ae08745Sheppo */ 14901ae08745Sheppo tx_head = ldcp->tx_ackd_head; 14911ae08745Sheppo for (;;) { 14921ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_head); 14931ae08745Sheppo tx_head = (tx_head + LDC_PACKET_SIZE) % 14941ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 14951ae08745Sheppo 14961ae08745Sheppo if (pkt->seqid == msg->ackid) { 14971ae08745Sheppo D2(ldcp->id, 14981ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) found packet\n", 14991ae08745Sheppo ldcp->id); 15001ae08745Sheppo ldcp->last_ack_rcd = msg->ackid; 15011ae08745Sheppo ldcp->tx_ackd_head = tx_head; 15021ae08745Sheppo break; 15031ae08745Sheppo } 15041ae08745Sheppo if (tx_head == ldcp->tx_head) { 15051ae08745Sheppo /* could not find packet */ 15061ae08745Sheppo DWARN(ldcp->id, 15071ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) invalid ACKid\n", 15081ae08745Sheppo ldcp->id); 1509d10e4ef2Snarayan 1510d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 1511*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1512d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1513d10e4ef2Snarayan return (ECONNRESET); 15141ae08745Sheppo } 15151ae08745Sheppo } 15161ae08745Sheppo 1517d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15181ae08745Sheppo return (0); 15191ae08745Sheppo } 15201ae08745Sheppo 15211ae08745Sheppo /* 15221ae08745Sheppo * Process incoming control message 15231ae08745Sheppo * Return 0 - session can continue 15241ae08745Sheppo * EAGAIN - reprocess packet - state was changed 15251ae08745Sheppo * ECONNRESET - channel was reset 15261ae08745Sheppo */ 15271ae08745Sheppo static int 15281ae08745Sheppo i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *msg) 15291ae08745Sheppo { 15301ae08745Sheppo int rv = 0; 15311ae08745Sheppo 1532*3af08d82Slm66018 D1(ldcp->id, "i_ldc_ctrlmsg: (%llx) tstate = %lx, hstate = %lx\n", 1533*3af08d82Slm66018 ldcp->id, ldcp->tstate, ldcp->hstate); 1534*3af08d82Slm66018 1535*3af08d82Slm66018 switch (ldcp->tstate & ~TS_IN_RESET) { 15361ae08745Sheppo 15371ae08745Sheppo case TS_OPEN: 15381ae08745Sheppo case TS_READY: 15391ae08745Sheppo 15401ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 15411ae08745Sheppo case LDC_VER: 15421ae08745Sheppo /* process version message */ 15431ae08745Sheppo rv = i_ldc_process_VER(ldcp, msg); 15441ae08745Sheppo break; 15451ae08745Sheppo default: 15461ae08745Sheppo DWARN(ldcp->id, 15471ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 15481ae08745Sheppo "tstate=0x%x\n", ldcp->id, 15491ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 15501ae08745Sheppo break; 15511ae08745Sheppo } 15521ae08745Sheppo 15531ae08745Sheppo break; 15541ae08745Sheppo 15551ae08745Sheppo case TS_VREADY: 15561ae08745Sheppo 15571ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 15581ae08745Sheppo case LDC_VER: 1559*3af08d82Slm66018 /* process version message */ 1560*3af08d82Slm66018 rv = i_ldc_process_VER(ldcp, msg); 15611ae08745Sheppo break; 15621ae08745Sheppo case LDC_RTS: 15631ae08745Sheppo /* process RTS message */ 15641ae08745Sheppo rv = i_ldc_process_RTS(ldcp, msg); 15651ae08745Sheppo break; 15661ae08745Sheppo case LDC_RTR: 15671ae08745Sheppo /* process RTR message */ 15681ae08745Sheppo rv = i_ldc_process_RTR(ldcp, msg); 15691ae08745Sheppo break; 15701ae08745Sheppo case LDC_RDX: 15711ae08745Sheppo /* process RDX message */ 15721ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 15731ae08745Sheppo break; 15741ae08745Sheppo default: 15751ae08745Sheppo DWARN(ldcp->id, 15761ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 15771ae08745Sheppo "tstate=0x%x\n", ldcp->id, 15781ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 15791ae08745Sheppo break; 15801ae08745Sheppo } 15811ae08745Sheppo 15821ae08745Sheppo break; 15831ae08745Sheppo 15841ae08745Sheppo case TS_UP: 15851ae08745Sheppo 15861ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 15871ae08745Sheppo case LDC_VER: 15881ae08745Sheppo DWARN(ldcp->id, 15891ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexpected VER " 15901ae08745Sheppo "- LDC reset\n", ldcp->id); 15911ae08745Sheppo /* peer is redoing version negotiation */ 1592d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15931ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 15941ae08745Sheppo i_ldc_reset_state(ldcp); 1595d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15961ae08745Sheppo rv = EAGAIN; 15971ae08745Sheppo break; 15981ae08745Sheppo 15991ae08745Sheppo case LDC_RDX: 16001ae08745Sheppo /* process RDX message */ 16011ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 16021ae08745Sheppo break; 16031ae08745Sheppo 16041ae08745Sheppo default: 16051ae08745Sheppo DWARN(ldcp->id, 16061ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 16071ae08745Sheppo "tstate=0x%x\n", ldcp->id, 16081ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 16091ae08745Sheppo break; 16101ae08745Sheppo } 16111ae08745Sheppo } 16121ae08745Sheppo 16131ae08745Sheppo return (rv); 16141ae08745Sheppo } 16151ae08745Sheppo 16161ae08745Sheppo /* 16171ae08745Sheppo * Register channel with the channel nexus 16181ae08745Sheppo */ 16191ae08745Sheppo static int 16201ae08745Sheppo i_ldc_register_channel(ldc_chan_t *ldcp) 16211ae08745Sheppo { 16221ae08745Sheppo int rv = 0; 16231ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 16241ae08745Sheppo 16251ae08745Sheppo if (cinfo->dip == NULL) { 16261ae08745Sheppo DWARN(ldcp->id, 16271ae08745Sheppo "i_ldc_register_channel: cnex has not registered\n"); 16281ae08745Sheppo return (EAGAIN); 16291ae08745Sheppo } 16301ae08745Sheppo 16311ae08745Sheppo rv = cinfo->reg_chan(cinfo->dip, ldcp->id, ldcp->devclass); 16321ae08745Sheppo if (rv) { 16331ae08745Sheppo DWARN(ldcp->id, 16341ae08745Sheppo "i_ldc_register_channel: cannot register channel\n"); 16351ae08745Sheppo return (rv); 16361ae08745Sheppo } 16371ae08745Sheppo 16381ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR, 16391ae08745Sheppo i_ldc_tx_hdlr, ldcp, NULL); 16401ae08745Sheppo if (rv) { 16411ae08745Sheppo DWARN(ldcp->id, 16421ae08745Sheppo "i_ldc_register_channel: cannot add Tx interrupt\n"); 16431ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 16441ae08745Sheppo return (rv); 16451ae08745Sheppo } 16461ae08745Sheppo 16471ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR, 16481ae08745Sheppo i_ldc_rx_hdlr, ldcp, NULL); 16491ae08745Sheppo if (rv) { 16501ae08745Sheppo DWARN(ldcp->id, 16511ae08745Sheppo "i_ldc_register_channel: cannot add Rx interrupt\n"); 16521ae08745Sheppo (void) cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 16531ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 16541ae08745Sheppo return (rv); 16551ae08745Sheppo } 16561ae08745Sheppo 16571ae08745Sheppo ldcp->tstate |= TS_CNEX_RDY; 16581ae08745Sheppo 16591ae08745Sheppo return (0); 16601ae08745Sheppo } 16611ae08745Sheppo 16621ae08745Sheppo /* 16631ae08745Sheppo * Unregister a channel with the channel nexus 16641ae08745Sheppo */ 16651ae08745Sheppo static int 16661ae08745Sheppo i_ldc_unregister_channel(ldc_chan_t *ldcp) 16671ae08745Sheppo { 16681ae08745Sheppo int rv = 0; 16691ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 16701ae08745Sheppo 16711ae08745Sheppo if (cinfo->dip == NULL) { 16721ae08745Sheppo DWARN(ldcp->id, 16731ae08745Sheppo "i_ldc_unregister_channel: cnex has not registered\n"); 16741ae08745Sheppo return (EAGAIN); 16751ae08745Sheppo } 16761ae08745Sheppo 16771ae08745Sheppo if (ldcp->tstate & TS_CNEX_RDY) { 16781ae08745Sheppo 1679d10e4ef2Snarayan /* Remove the Rx interrupt */ 16801ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR); 16811ae08745Sheppo if (rv) { 1682*3af08d82Slm66018 if (rv != EAGAIN) { 16831ae08745Sheppo DWARN(ldcp->id, 1684*3af08d82Slm66018 "i_ldc_unregister_channel: err removing " 1685*3af08d82Slm66018 "Rx intr\n"); 1686d10e4ef2Snarayan return (rv); 16871ae08745Sheppo } 1688d10e4ef2Snarayan 1689*3af08d82Slm66018 /* 1690*3af08d82Slm66018 * If interrupts are pending and handler has 1691*3af08d82Slm66018 * finished running, clear interrupt and try 1692*3af08d82Slm66018 * again 1693*3af08d82Slm66018 */ 1694*3af08d82Slm66018 if (ldcp->rx_intr_state != LDC_INTR_PEND) 1695*3af08d82Slm66018 return (rv); 1696*3af08d82Slm66018 1697*3af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 1698*3af08d82Slm66018 rv = cinfo->rem_intr(cinfo->dip, ldcp->id, 1699*3af08d82Slm66018 CNEX_RX_INTR); 1700*3af08d82Slm66018 if (rv) { 1701*3af08d82Slm66018 DWARN(ldcp->id, "i_ldc_unregister_channel: " 1702*3af08d82Slm66018 "err removing Rx interrupt\n"); 1703*3af08d82Slm66018 return (rv); 1704*3af08d82Slm66018 } 1705*3af08d82Slm66018 } 1706*3af08d82Slm66018 1707d10e4ef2Snarayan /* Remove the Tx interrupt */ 17081ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 17091ae08745Sheppo if (rv) { 17101ae08745Sheppo DWARN(ldcp->id, 17111ae08745Sheppo "i_ldc_unregister_channel: err removing Tx intr\n"); 1712d10e4ef2Snarayan return (rv); 17131ae08745Sheppo } 1714d10e4ef2Snarayan 1715d10e4ef2Snarayan /* Unregister the channel */ 17161ae08745Sheppo rv = cinfo->unreg_chan(ldcssp->cinfo.dip, ldcp->id); 17171ae08745Sheppo if (rv) { 17181ae08745Sheppo DWARN(ldcp->id, 17191ae08745Sheppo "i_ldc_unregister_channel: cannot unreg channel\n"); 1720d10e4ef2Snarayan return (rv); 17211ae08745Sheppo } 17221ae08745Sheppo 17231ae08745Sheppo ldcp->tstate &= ~TS_CNEX_RDY; 17241ae08745Sheppo } 17251ae08745Sheppo 17261ae08745Sheppo return (0); 17271ae08745Sheppo } 17281ae08745Sheppo 17291ae08745Sheppo 17301ae08745Sheppo /* 17311ae08745Sheppo * LDC transmit interrupt handler 17321ae08745Sheppo * triggered for chanel up/down/reset events 17331ae08745Sheppo * and Tx queue content changes 17341ae08745Sheppo */ 17351ae08745Sheppo static uint_t 17361ae08745Sheppo i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2) 17371ae08745Sheppo { 17381ae08745Sheppo _NOTE(ARGUNUSED(arg2)) 17391ae08745Sheppo 17401ae08745Sheppo int rv; 17411ae08745Sheppo ldc_chan_t *ldcp; 17421ae08745Sheppo boolean_t notify_client = B_FALSE; 1743*3af08d82Slm66018 uint64_t notify_event = 0, link_state; 17441ae08745Sheppo 17451ae08745Sheppo /* Get the channel for which interrupt was received */ 17461ae08745Sheppo ASSERT(arg1 != NULL); 17471ae08745Sheppo ldcp = (ldc_chan_t *)arg1; 17481ae08745Sheppo 17491ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 17501ae08745Sheppo ldcp->id, ldcp); 17511ae08745Sheppo 17521ae08745Sheppo /* Lock channel */ 17531ae08745Sheppo mutex_enter(&ldcp->lock); 17541ae08745Sheppo 1755d10e4ef2Snarayan /* Obtain Tx lock */ 1756d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1757d10e4ef2Snarayan 17584bac2208Snarayan /* mark interrupt as pending */ 1759*3af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_ACTIVE; 1760*3af08d82Slm66018 1761*3af08d82Slm66018 /* save current link state */ 1762*3af08d82Slm66018 link_state = ldcp->link_state; 17634bac2208Snarayan 17641ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &ldcp->tx_head, &ldcp->tx_tail, 17651ae08745Sheppo &ldcp->link_state); 17661ae08745Sheppo if (rv) { 17671ae08745Sheppo cmn_err(CE_WARN, 17681ae08745Sheppo "i_ldc_tx_hdlr: (0x%lx) cannot read queue ptrs rv=0x%d\n", 17691ae08745Sheppo ldcp->id, rv); 17704bac2208Snarayan i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 1771d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17721ae08745Sheppo mutex_exit(&ldcp->lock); 17731ae08745Sheppo return (DDI_INTR_CLAIMED); 17741ae08745Sheppo } 17751ae08745Sheppo 17761ae08745Sheppo /* 17771ae08745Sheppo * reset the channel state if the channel went down 17781ae08745Sheppo * (other side unconfigured queue) or channel was reset 17791ae08745Sheppo * (other side reconfigured its queue) 17801ae08745Sheppo */ 1781*3af08d82Slm66018 if (link_state != ldcp->link_state && 1782*3af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_DOWN) { 17831ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link down\n", ldcp->id); 1784*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 17851ae08745Sheppo notify_client = B_TRUE; 17861ae08745Sheppo notify_event = LDC_EVT_DOWN; 17871ae08745Sheppo } 17881ae08745Sheppo 1789*3af08d82Slm66018 if (link_state != ldcp->link_state && 1790*3af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 17911ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link reset\n", ldcp->id); 1792*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 17931ae08745Sheppo notify_client = B_TRUE; 17941ae08745Sheppo notify_event = LDC_EVT_RESET; 17951ae08745Sheppo } 17961ae08745Sheppo 1797*3af08d82Slm66018 if (link_state != ldcp->link_state && 1798*3af08d82Slm66018 (ldcp->tstate & ~TS_IN_RESET) == TS_OPEN && 1799*3af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_UP) { 18001ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link up\n", ldcp->id); 18011ae08745Sheppo notify_client = B_TRUE; 18021ae08745Sheppo notify_event = LDC_EVT_RESET; 18031ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 18041ae08745Sheppo ldcp->status = LDC_READY; 18051ae08745Sheppo } 18061ae08745Sheppo 18071ae08745Sheppo /* if callbacks are disabled, do not notify */ 18081ae08745Sheppo if (!ldcp->cb_enabled) 18091ae08745Sheppo notify_client = B_FALSE; 18101ae08745Sheppo 18111ae08745Sheppo /* Unlock channel */ 18121ae08745Sheppo 18131ae08745Sheppo if (notify_client) { 1814*3af08d82Slm66018 ldcp->cb_inprogress = B_TRUE; 1815*3af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 1816*3af08d82Slm66018 mutex_exit(&ldcp->lock); 18171ae08745Sheppo rv = ldcp->cb(notify_event, ldcp->cb_arg); 18181ae08745Sheppo if (rv) { 18191ae08745Sheppo DWARN(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) callback " 18201ae08745Sheppo "failure", ldcp->id); 18211ae08745Sheppo } 18221ae08745Sheppo mutex_enter(&ldcp->lock); 18231ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 18241ae08745Sheppo } 18251ae08745Sheppo 18261ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 18271ae08745Sheppo mutex_exit(&ldcp->lock); 18281ae08745Sheppo 18291ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) exiting handler", ldcp->id); 18301ae08745Sheppo 18311ae08745Sheppo return (DDI_INTR_CLAIMED); 18321ae08745Sheppo } 18331ae08745Sheppo 18341ae08745Sheppo /* 18351ae08745Sheppo * LDC receive interrupt handler 18361ae08745Sheppo * triggered for channel with data pending to read 18371ae08745Sheppo * i.e. Rx queue content changes 18381ae08745Sheppo */ 18391ae08745Sheppo static uint_t 18401ae08745Sheppo i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2) 18411ae08745Sheppo { 18421ae08745Sheppo _NOTE(ARGUNUSED(arg2)) 18431ae08745Sheppo 18441ae08745Sheppo int rv; 18451ae08745Sheppo uint64_t rx_head, rx_tail; 18461ae08745Sheppo ldc_msg_t *msg; 18471ae08745Sheppo ldc_chan_t *ldcp; 18481ae08745Sheppo boolean_t notify_client = B_FALSE; 18491ae08745Sheppo uint64_t notify_event = 0; 1850*3af08d82Slm66018 uint64_t link_state, first_fragment = 0; 1851*3af08d82Slm66018 18521ae08745Sheppo 18531ae08745Sheppo /* Get the channel for which interrupt was received */ 18541ae08745Sheppo if (arg1 == NULL) { 18551ae08745Sheppo cmn_err(CE_WARN, "i_ldc_rx_hdlr: invalid arg\n"); 18561ae08745Sheppo return (DDI_INTR_UNCLAIMED); 18571ae08745Sheppo } 18581ae08745Sheppo 18591ae08745Sheppo ldcp = (ldc_chan_t *)arg1; 18601ae08745Sheppo 18611ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 18621ae08745Sheppo ldcp->id, ldcp); 1863*3af08d82Slm66018 D1(ldcp->id, "i_ldc_rx_hdlr: (%llx) USR%lx/TS%lx/HS%lx, LSTATE=%lx\n", 1864*3af08d82Slm66018 ldcp->id, ldcp->status, ldcp->tstate, ldcp->hstate, 1865*3af08d82Slm66018 ldcp->link_state); 18661ae08745Sheppo 18671ae08745Sheppo /* Lock channel */ 18681ae08745Sheppo mutex_enter(&ldcp->lock); 18691ae08745Sheppo 18701ae08745Sheppo /* mark interrupt as pending */ 1871*3af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_ACTIVE; 18721ae08745Sheppo 18731ae08745Sheppo /* 18741ae08745Sheppo * Read packet(s) from the queue 18751ae08745Sheppo */ 18761ae08745Sheppo for (;;) { 18771ae08745Sheppo 1878*3af08d82Slm66018 link_state = ldcp->link_state; 18791ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 18801ae08745Sheppo &ldcp->link_state); 18811ae08745Sheppo if (rv) { 18821ae08745Sheppo cmn_err(CE_WARN, 18831ae08745Sheppo "i_ldc_rx_hdlr: (0x%lx) cannot read " 18841ae08745Sheppo "queue ptrs, rv=0x%d\n", ldcp->id, rv); 18851ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 18861ae08745Sheppo mutex_exit(&ldcp->lock); 18871ae08745Sheppo return (DDI_INTR_CLAIMED); 18881ae08745Sheppo } 18891ae08745Sheppo 18901ae08745Sheppo /* 18911ae08745Sheppo * reset the channel state if the channel went down 18921ae08745Sheppo * (other side unconfigured queue) or channel was reset 1893*3af08d82Slm66018 * (other side reconfigured its queue) 18941ae08745Sheppo */ 1895*3af08d82Slm66018 1896*3af08d82Slm66018 if (link_state != ldcp->link_state) { 1897*3af08d82Slm66018 switch (ldcp->link_state) { 1898*3af08d82Slm66018 case LDC_CHANNEL_DOWN: 1899*3af08d82Slm66018 D1(ldcp->id, "i_ldc_rx_hdlr: channel " 1900*3af08d82Slm66018 "link down\n", ldcp->id); 1901d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1902*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 1903d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 19041ae08745Sheppo notify_client = B_TRUE; 19051ae08745Sheppo notify_event = LDC_EVT_DOWN; 1906*3af08d82Slm66018 goto loop_exit; 19071ae08745Sheppo 1908*3af08d82Slm66018 case LDC_CHANNEL_UP: 1909*3af08d82Slm66018 D1(ldcp->id, "i_ldc_rx_hdlr: " 1910*3af08d82Slm66018 "channel link up\n", ldcp->id); 1911*3af08d82Slm66018 1912*3af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_OPEN) { 19131ae08745Sheppo notify_client = B_TRUE; 19141ae08745Sheppo notify_event = LDC_EVT_RESET; 19151ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 19161ae08745Sheppo ldcp->status = LDC_READY; 19171ae08745Sheppo } 1918*3af08d82Slm66018 break; 1919*3af08d82Slm66018 1920*3af08d82Slm66018 case LDC_CHANNEL_RESET: 1921*3af08d82Slm66018 default: 1922*3af08d82Slm66018 #ifdef DEBUG 1923*3af08d82Slm66018 force_reset: 1924*3af08d82Slm66018 #endif 1925*3af08d82Slm66018 D1(ldcp->id, "i_ldc_rx_hdlr: channel " 1926*3af08d82Slm66018 "link reset\n", ldcp->id); 1927*3af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 1928*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 1929*3af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 1930*3af08d82Slm66018 notify_client = B_TRUE; 1931*3af08d82Slm66018 notify_event = LDC_EVT_RESET; 1932*3af08d82Slm66018 break; 1933*3af08d82Slm66018 } 1934*3af08d82Slm66018 } 1935*3af08d82Slm66018 1936*3af08d82Slm66018 #ifdef DEBUG 1937*3af08d82Slm66018 if (LDC_INJECT_RESET(ldcp)) 1938*3af08d82Slm66018 goto force_reset; 1939*3af08d82Slm66018 #endif 19401ae08745Sheppo 19411ae08745Sheppo if (rx_head == rx_tail) { 19421ae08745Sheppo D2(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) No packets\n", 19431ae08745Sheppo ldcp->id); 19441ae08745Sheppo break; 19451ae08745Sheppo } 1946*3af08d82Slm66018 19471ae08745Sheppo D2(ldcp->id, "i_ldc_rx_hdlr: head=0x%llx, tail=0x%llx\n", 19481ae08745Sheppo rx_head, rx_tail); 19491ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_rx_hdlr rcd", 19501ae08745Sheppo ldcp->rx_q_va + rx_head); 19511ae08745Sheppo 19521ae08745Sheppo /* get the message */ 19531ae08745Sheppo msg = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 19541ae08745Sheppo 19551ae08745Sheppo /* if channel is in RAW mode or data pkt, notify and return */ 19561ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 19571ae08745Sheppo notify_client = B_TRUE; 19581ae08745Sheppo notify_event |= LDC_EVT_READ; 19591ae08745Sheppo break; 19601ae08745Sheppo } 19611ae08745Sheppo 19621ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 19631ae08745Sheppo 19641ae08745Sheppo /* discard packet if channel is not up */ 1965*3af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) != TS_UP) { 19661ae08745Sheppo 19671ae08745Sheppo /* move the head one position */ 19681ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 19691ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 19701ae08745Sheppo 19711ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, rx_head)) 19721ae08745Sheppo break; 19731ae08745Sheppo 19741ae08745Sheppo continue; 19751ae08745Sheppo } else { 1976*3af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 19771ae08745Sheppo notify_client = B_TRUE; 19781ae08745Sheppo notify_event |= LDC_EVT_READ; 19791ae08745Sheppo break; 19801ae08745Sheppo } 19811ae08745Sheppo } 19821ae08745Sheppo 19831ae08745Sheppo /* Check the sequence ID for the message received */ 1984*3af08d82Slm66018 rv = i_ldc_check_seqid(ldcp, msg); 1985*3af08d82Slm66018 if (rv != 0) { 19861ae08745Sheppo 19871ae08745Sheppo DWARN(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) seqid error, " 19881ae08745Sheppo "q_ptrs=0x%lx,0x%lx", ldcp->id, rx_head, rx_tail); 19891ae08745Sheppo 19901ae08745Sheppo /* Reset last_msg_rcd to start of message */ 1991d10e4ef2Snarayan if (first_fragment != 0) { 1992d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 1993d10e4ef2Snarayan first_fragment = 0; 19941ae08745Sheppo } 1995d10e4ef2Snarayan 19961ae08745Sheppo /* 19971ae08745Sheppo * Send a NACK due to seqid mismatch 19981ae08745Sheppo */ 19991ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, 20001ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 20011ae08745Sheppo 20021ae08745Sheppo if (rv) { 20031ae08745Sheppo cmn_err(CE_NOTE, 20041ae08745Sheppo "i_ldc_rx_hdlr: (0x%lx) err sending " 20051ae08745Sheppo "CTRL/NACK msg\n", ldcp->id); 2006d10e4ef2Snarayan 2007d10e4ef2Snarayan /* if cannot send NACK - reset channel */ 2008d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2009*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 2010d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 2011d10e4ef2Snarayan rv = ECONNRESET; 2012d10e4ef2Snarayan break; 20131ae08745Sheppo } 20141ae08745Sheppo 20151ae08745Sheppo /* purge receive queue */ 20161ae08745Sheppo (void) i_ldc_set_rx_head(ldcp, rx_tail); 20171ae08745Sheppo break; 20181ae08745Sheppo } 20191ae08745Sheppo 20201ae08745Sheppo /* record the message ID */ 20211ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 20221ae08745Sheppo 20231ae08745Sheppo /* process control messages */ 20241ae08745Sheppo if (msg->type & LDC_CTRL) { 20251ae08745Sheppo /* save current internal state */ 20261ae08745Sheppo uint64_t tstate = ldcp->tstate; 20271ae08745Sheppo 20281ae08745Sheppo rv = i_ldc_ctrlmsg(ldcp, msg); 20291ae08745Sheppo if (rv == EAGAIN) { 20301ae08745Sheppo /* re-process pkt - state was adjusted */ 20311ae08745Sheppo continue; 20321ae08745Sheppo } 20331ae08745Sheppo if (rv == ECONNRESET) { 20341ae08745Sheppo notify_client = B_TRUE; 20351ae08745Sheppo notify_event = LDC_EVT_RESET; 20361ae08745Sheppo break; 20371ae08745Sheppo } 20381ae08745Sheppo 20391ae08745Sheppo /* 20401ae08745Sheppo * control message processing was successful 20411ae08745Sheppo * channel transitioned to ready for communication 20421ae08745Sheppo */ 20431ae08745Sheppo if (rv == 0 && ldcp->tstate == TS_UP && 2044*3af08d82Slm66018 (tstate & ~TS_IN_RESET) != 2045*3af08d82Slm66018 (ldcp->tstate & ~TS_IN_RESET)) { 20461ae08745Sheppo notify_client = B_TRUE; 20471ae08745Sheppo notify_event = LDC_EVT_UP; 20481ae08745Sheppo } 20491ae08745Sheppo } 20501ae08745Sheppo 20511ae08745Sheppo /* process data ACKs */ 20521ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 2053d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 2054d10e4ef2Snarayan notify_client = B_TRUE; 2055d10e4ef2Snarayan notify_event = LDC_EVT_RESET; 2056d10e4ef2Snarayan break; 2057d10e4ef2Snarayan } 20581ae08745Sheppo } 20591ae08745Sheppo 20601ae08745Sheppo /* move the head one position */ 20611ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 20621ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 20630a55fbb7Slm66018 if (rv = i_ldc_set_rx_head(ldcp, rx_head)) { 20640a55fbb7Slm66018 notify_client = B_TRUE; 20650a55fbb7Slm66018 notify_event = LDC_EVT_RESET; 20661ae08745Sheppo break; 20670a55fbb7Slm66018 } 20681ae08745Sheppo 20691ae08745Sheppo } /* for */ 20701ae08745Sheppo 2071*3af08d82Slm66018 loop_exit: 2072*3af08d82Slm66018 20731ae08745Sheppo /* if callbacks are disabled, do not notify */ 20741ae08745Sheppo if (!ldcp->cb_enabled) 20751ae08745Sheppo notify_client = B_FALSE; 20761ae08745Sheppo 2077*3af08d82Slm66018 /* 2078*3af08d82Slm66018 * If there are data packets in the queue, the ldc_read will 2079*3af08d82Slm66018 * clear interrupts after draining the queue, else clear interrupts 2080*3af08d82Slm66018 */ 2081*3af08d82Slm66018 if ((notify_event & LDC_EVT_READ) == 0) { 2082*3af08d82Slm66018 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 2083*3af08d82Slm66018 } else 2084*3af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_PEND; 20851ae08745Sheppo 20861ae08745Sheppo mutex_exit(&ldcp->lock); 20871ae08745Sheppo 20881ae08745Sheppo if (notify_client) { 20891ae08745Sheppo rv = ldcp->cb(notify_event, ldcp->cb_arg); 20901ae08745Sheppo if (rv) { 20911ae08745Sheppo DWARN(ldcp->id, 20921ae08745Sheppo "i_ldc_rx_hdlr: (0x%llx) callback failure", 20931ae08745Sheppo ldcp->id); 20941ae08745Sheppo } 20951ae08745Sheppo } 20961ae08745Sheppo 20971ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) exiting handler", ldcp->id); 20981ae08745Sheppo return (DDI_INTR_CLAIMED); 20991ae08745Sheppo } 21001ae08745Sheppo 21011ae08745Sheppo 21021ae08745Sheppo /* -------------------------------------------------------------------------- */ 21031ae08745Sheppo 21041ae08745Sheppo /* 21051ae08745Sheppo * LDC API functions 21061ae08745Sheppo */ 21071ae08745Sheppo 21081ae08745Sheppo /* 21091ae08745Sheppo * Initialize the channel. Allocate internal structure and memory for 21101ae08745Sheppo * TX/RX queues, and initialize locks. 21111ae08745Sheppo */ 21121ae08745Sheppo int 21131ae08745Sheppo ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle) 21141ae08745Sheppo { 21151ae08745Sheppo ldc_chan_t *ldcp; 21161ae08745Sheppo int rv, exit_val; 21171ae08745Sheppo uint64_t ra_base, nentries; 2118e1ebb9ecSlm66018 uint64_t qlen; 21191ae08745Sheppo 21201ae08745Sheppo exit_val = EINVAL; /* guarantee an error if exit on failure */ 21211ae08745Sheppo 21221ae08745Sheppo if (attr == NULL) { 21231ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid attr\n", id); 21241ae08745Sheppo return (EINVAL); 21251ae08745Sheppo } 21261ae08745Sheppo if (handle == NULL) { 21271ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid handle\n", id); 21281ae08745Sheppo return (EINVAL); 21291ae08745Sheppo } 21301ae08745Sheppo 21311ae08745Sheppo /* check if channel is valid */ 21321ae08745Sheppo rv = hv_ldc_tx_qinfo(id, &ra_base, &nentries); 21331ae08745Sheppo if (rv == H_ECHANNEL) { 21341ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid channel id\n", id); 21351ae08745Sheppo return (EINVAL); 21361ae08745Sheppo } 21371ae08745Sheppo 21381ae08745Sheppo /* check if the channel has already been initialized */ 21391ae08745Sheppo mutex_enter(&ldcssp->lock); 21401ae08745Sheppo ldcp = ldcssp->chan_list; 21411ae08745Sheppo while (ldcp != NULL) { 21421ae08745Sheppo if (ldcp->id == id) { 21431ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) already initialized\n", 21441ae08745Sheppo id); 21451ae08745Sheppo mutex_exit(&ldcssp->lock); 21461ae08745Sheppo return (EADDRINUSE); 21471ae08745Sheppo } 21481ae08745Sheppo ldcp = ldcp->next; 21491ae08745Sheppo } 21501ae08745Sheppo mutex_exit(&ldcssp->lock); 21511ae08745Sheppo 21521ae08745Sheppo ASSERT(ldcp == NULL); 21531ae08745Sheppo 21541ae08745Sheppo *handle = 0; 21551ae08745Sheppo 21561ae08745Sheppo /* Allocate an ldcp structure */ 21571ae08745Sheppo ldcp = kmem_zalloc(sizeof (ldc_chan_t), KM_SLEEP); 21581ae08745Sheppo 2159d10e4ef2Snarayan /* 2160d10e4ef2Snarayan * Initialize the channel and Tx lock 2161d10e4ef2Snarayan * 2162d10e4ef2Snarayan * The channel 'lock' protects the entire channel and 2163d10e4ef2Snarayan * should be acquired before initializing, resetting, 2164d10e4ef2Snarayan * destroying or reading from a channel. 2165d10e4ef2Snarayan * 2166d10e4ef2Snarayan * The 'tx_lock' should be acquired prior to transmitting 2167d10e4ef2Snarayan * data over the channel. The lock should also be acquired 2168d10e4ef2Snarayan * prior to channel reconfiguration (in order to prevent 2169d10e4ef2Snarayan * concurrent writes). 2170d10e4ef2Snarayan * 2171d10e4ef2Snarayan * ORDERING: When both locks are being acquired, to prevent 2172d10e4ef2Snarayan * deadlocks, the channel lock should be always acquired prior 2173d10e4ef2Snarayan * to the tx_lock. 2174d10e4ef2Snarayan */ 21751ae08745Sheppo mutex_init(&ldcp->lock, NULL, MUTEX_DRIVER, NULL); 2176d10e4ef2Snarayan mutex_init(&ldcp->tx_lock, NULL, MUTEX_DRIVER, NULL); 21771ae08745Sheppo 21781ae08745Sheppo /* Initialize the channel */ 21791ae08745Sheppo ldcp->id = id; 21801ae08745Sheppo ldcp->cb = NULL; 21811ae08745Sheppo ldcp->cb_arg = NULL; 21821ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 21831ae08745Sheppo ldcp->cb_enabled = B_FALSE; 21841ae08745Sheppo ldcp->next = NULL; 21851ae08745Sheppo 21861ae08745Sheppo /* Read attributes */ 21871ae08745Sheppo ldcp->mode = attr->mode; 21881ae08745Sheppo ldcp->devclass = attr->devclass; 21891ae08745Sheppo ldcp->devinst = attr->instance; 2190e1ebb9ecSlm66018 ldcp->mtu = (attr->mtu > 0) ? attr->mtu : LDC_DEFAULT_MTU; 21911ae08745Sheppo 21921ae08745Sheppo D1(ldcp->id, 21931ae08745Sheppo "ldc_init: (0x%llx) channel attributes, class=0x%x, " 2194e1ebb9ecSlm66018 "instance=0x%llx, mode=%d, mtu=%d\n", 2195e1ebb9ecSlm66018 ldcp->id, ldcp->devclass, ldcp->devinst, ldcp->mode, ldcp->mtu); 21961ae08745Sheppo 21971ae08745Sheppo ldcp->next_vidx = 0; 2198*3af08d82Slm66018 ldcp->tstate = TS_IN_RESET; 21991ae08745Sheppo ldcp->hstate = 0; 22001ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 22011ae08745Sheppo ldcp->last_ack_rcd = 0; 22021ae08745Sheppo ldcp->last_msg_rcd = 0; 22031ae08745Sheppo 22041ae08745Sheppo ldcp->stream_bufferp = NULL; 22051ae08745Sheppo ldcp->exp_dring_list = NULL; 22061ae08745Sheppo ldcp->imp_dring_list = NULL; 22071ae08745Sheppo ldcp->mhdl_list = NULL; 22081ae08745Sheppo 2209*3af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_NONE; 2210*3af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_NONE; 2211*3af08d82Slm66018 22121ae08745Sheppo /* Initialize payload size depending on whether channel is reliable */ 22131ae08745Sheppo switch (ldcp->mode) { 22141ae08745Sheppo case LDC_MODE_RAW: 22151ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RAW; 22161ae08745Sheppo ldcp->read_p = i_ldc_read_raw; 22171ae08745Sheppo ldcp->write_p = i_ldc_write_raw; 22181ae08745Sheppo break; 22191ae08745Sheppo case LDC_MODE_UNRELIABLE: 22201ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_UNRELIABLE; 22211ae08745Sheppo ldcp->read_p = i_ldc_read_packet; 22221ae08745Sheppo ldcp->write_p = i_ldc_write_packet; 22231ae08745Sheppo break; 22241ae08745Sheppo case LDC_MODE_RELIABLE: 22251ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE; 22261ae08745Sheppo ldcp->read_p = i_ldc_read_packet; 22271ae08745Sheppo ldcp->write_p = i_ldc_write_packet; 22281ae08745Sheppo break; 22291ae08745Sheppo case LDC_MODE_STREAM: 22301ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE; 22311ae08745Sheppo 22321ae08745Sheppo ldcp->stream_remains = 0; 22331ae08745Sheppo ldcp->stream_offset = 0; 22341ae08745Sheppo ldcp->stream_bufferp = kmem_alloc(ldcp->mtu, KM_SLEEP); 22351ae08745Sheppo ldcp->read_p = i_ldc_read_stream; 22361ae08745Sheppo ldcp->write_p = i_ldc_write_stream; 22371ae08745Sheppo break; 22381ae08745Sheppo default: 22391ae08745Sheppo exit_val = EINVAL; 22401ae08745Sheppo goto cleanup_on_exit; 22411ae08745Sheppo } 22421ae08745Sheppo 2243e1ebb9ecSlm66018 /* 2244e1ebb9ecSlm66018 * qlen is (mtu * ldc_mtu_msgs) / pkt_payload. If this 2245e1ebb9ecSlm66018 * value is smaller than default length of ldc_queue_entries, 2246e1ebb9ecSlm66018 * qlen is set to ldc_queue_entries.. 2247e1ebb9ecSlm66018 */ 2248e1ebb9ecSlm66018 qlen = (ldcp->mtu * ldc_mtu_msgs) / ldcp->pkt_payload; 2249e1ebb9ecSlm66018 ldcp->rx_q_entries = 2250e1ebb9ecSlm66018 (qlen < ldc_queue_entries) ? ldc_queue_entries : qlen; 2251e1ebb9ecSlm66018 ldcp->tx_q_entries = ldcp->rx_q_entries; 2252e1ebb9ecSlm66018 2253e1ebb9ecSlm66018 D1(ldcp->id, "ldc_init: queue length = 0x%llx\n", qlen); 2254e1ebb9ecSlm66018 22551ae08745Sheppo /* Create a transmit queue */ 22561ae08745Sheppo ldcp->tx_q_va = (uint64_t) 22571ae08745Sheppo contig_mem_alloc(ldcp->tx_q_entries << LDC_PACKET_SHIFT); 22581ae08745Sheppo if (ldcp->tx_q_va == NULL) { 22591ae08745Sheppo cmn_err(CE_WARN, 22601ae08745Sheppo "ldc_init: (0x%lx) TX queue allocation failed\n", 22611ae08745Sheppo ldcp->id); 22621ae08745Sheppo exit_val = ENOMEM; 22631ae08745Sheppo goto cleanup_on_exit; 22641ae08745Sheppo } 22651ae08745Sheppo ldcp->tx_q_ra = va_to_pa((caddr_t)ldcp->tx_q_va); 22661ae08745Sheppo 22671ae08745Sheppo D2(ldcp->id, "ldc_init: txq_va=0x%llx, txq_ra=0x%llx, entries=0x%llx\n", 22681ae08745Sheppo ldcp->tx_q_va, ldcp->tx_q_ra, ldcp->tx_q_entries); 22691ae08745Sheppo 22701ae08745Sheppo ldcp->tstate |= TS_TXQ_RDY; 22711ae08745Sheppo 22721ae08745Sheppo /* Create a receive queue */ 22731ae08745Sheppo ldcp->rx_q_va = (uint64_t) 22741ae08745Sheppo contig_mem_alloc(ldcp->rx_q_entries << LDC_PACKET_SHIFT); 22751ae08745Sheppo if (ldcp->rx_q_va == NULL) { 22761ae08745Sheppo cmn_err(CE_WARN, 22771ae08745Sheppo "ldc_init: (0x%lx) RX queue allocation failed\n", 22781ae08745Sheppo ldcp->id); 22791ae08745Sheppo exit_val = ENOMEM; 22801ae08745Sheppo goto cleanup_on_exit; 22811ae08745Sheppo } 22821ae08745Sheppo ldcp->rx_q_ra = va_to_pa((caddr_t)ldcp->rx_q_va); 22831ae08745Sheppo 22841ae08745Sheppo D2(ldcp->id, "ldc_init: rxq_va=0x%llx, rxq_ra=0x%llx, entries=0x%llx\n", 22851ae08745Sheppo ldcp->rx_q_va, ldcp->rx_q_ra, ldcp->rx_q_entries); 22861ae08745Sheppo 22871ae08745Sheppo ldcp->tstate |= TS_RXQ_RDY; 22881ae08745Sheppo 22891ae08745Sheppo /* Init descriptor ring and memory handle list lock */ 22901ae08745Sheppo mutex_init(&ldcp->exp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 22911ae08745Sheppo mutex_init(&ldcp->imp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 22921ae08745Sheppo mutex_init(&ldcp->mlist_lock, NULL, MUTEX_DRIVER, NULL); 22931ae08745Sheppo 22941ae08745Sheppo /* mark status as INITialized */ 22951ae08745Sheppo ldcp->status = LDC_INIT; 22961ae08745Sheppo 22971ae08745Sheppo /* Add to channel list */ 22981ae08745Sheppo mutex_enter(&ldcssp->lock); 22991ae08745Sheppo ldcp->next = ldcssp->chan_list; 23001ae08745Sheppo ldcssp->chan_list = ldcp; 23011ae08745Sheppo ldcssp->channel_count++; 23021ae08745Sheppo mutex_exit(&ldcssp->lock); 23031ae08745Sheppo 23041ae08745Sheppo /* set the handle */ 23051ae08745Sheppo *handle = (ldc_handle_t)ldcp; 23061ae08745Sheppo 23071ae08745Sheppo D1(ldcp->id, "ldc_init: (0x%llx) channel initialized\n", ldcp->id); 23081ae08745Sheppo 23091ae08745Sheppo return (0); 23101ae08745Sheppo 23111ae08745Sheppo cleanup_on_exit: 23121ae08745Sheppo 23131ae08745Sheppo if (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_bufferp) 23141ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 23151ae08745Sheppo 23161ae08745Sheppo if (ldcp->tstate & TS_TXQ_RDY) 23171ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 23181ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 23191ae08745Sheppo 23201ae08745Sheppo if (ldcp->tstate & TS_RXQ_RDY) 23211ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 23221ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 23231ae08745Sheppo 2324d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 23251ae08745Sheppo mutex_destroy(&ldcp->lock); 23261ae08745Sheppo 23271ae08745Sheppo if (ldcp) 23281ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 23291ae08745Sheppo 23301ae08745Sheppo return (exit_val); 23311ae08745Sheppo } 23321ae08745Sheppo 23331ae08745Sheppo /* 23341ae08745Sheppo * Finalizes the LDC connection. It will return EBUSY if the 23351ae08745Sheppo * channel is open. A ldc_close() has to be done prior to 23361ae08745Sheppo * a ldc_fini operation. It frees TX/RX queues, associated 23371ae08745Sheppo * with the channel 23381ae08745Sheppo */ 23391ae08745Sheppo int 23401ae08745Sheppo ldc_fini(ldc_handle_t handle) 23411ae08745Sheppo { 23421ae08745Sheppo ldc_chan_t *ldcp; 23431ae08745Sheppo ldc_chan_t *tmp_ldcp; 23441ae08745Sheppo uint64_t id; 23451ae08745Sheppo 23461ae08745Sheppo if (handle == NULL) { 23471ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel handle\n"); 23481ae08745Sheppo return (EINVAL); 23491ae08745Sheppo } 23501ae08745Sheppo ldcp = (ldc_chan_t *)handle; 23511ae08745Sheppo id = ldcp->id; 23521ae08745Sheppo 23531ae08745Sheppo mutex_enter(&ldcp->lock); 23541ae08745Sheppo 2355*3af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) > TS_INIT) { 23561ae08745Sheppo DWARN(ldcp->id, "ldc_fini: (0x%llx) channel is open\n", 23571ae08745Sheppo ldcp->id); 23581ae08745Sheppo mutex_exit(&ldcp->lock); 23591ae08745Sheppo return (EBUSY); 23601ae08745Sheppo } 23611ae08745Sheppo 23621ae08745Sheppo /* Remove from the channel list */ 23631ae08745Sheppo mutex_enter(&ldcssp->lock); 23641ae08745Sheppo tmp_ldcp = ldcssp->chan_list; 23651ae08745Sheppo if (tmp_ldcp == ldcp) { 23661ae08745Sheppo ldcssp->chan_list = ldcp->next; 23671ae08745Sheppo ldcp->next = NULL; 23681ae08745Sheppo } else { 23691ae08745Sheppo while (tmp_ldcp != NULL) { 23701ae08745Sheppo if (tmp_ldcp->next == ldcp) { 23711ae08745Sheppo tmp_ldcp->next = ldcp->next; 23721ae08745Sheppo ldcp->next = NULL; 23731ae08745Sheppo break; 23741ae08745Sheppo } 23751ae08745Sheppo tmp_ldcp = tmp_ldcp->next; 23761ae08745Sheppo } 23771ae08745Sheppo if (tmp_ldcp == NULL) { 23781ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel hdl\n"); 23791ae08745Sheppo mutex_exit(&ldcssp->lock); 23801ae08745Sheppo mutex_exit(&ldcp->lock); 23811ae08745Sheppo return (EINVAL); 23821ae08745Sheppo } 23831ae08745Sheppo } 23841ae08745Sheppo 23851ae08745Sheppo ldcssp->channel_count--; 23861ae08745Sheppo 23871ae08745Sheppo mutex_exit(&ldcssp->lock); 23881ae08745Sheppo 23891ae08745Sheppo /* Free the map table for this channel */ 23901ae08745Sheppo if (ldcp->mtbl) { 23911ae08745Sheppo (void) hv_ldc_set_map_table(ldcp->id, NULL, NULL); 2392*3af08d82Slm66018 if (ldcp->mtbl->contigmem) 23931ae08745Sheppo contig_mem_free(ldcp->mtbl->table, ldcp->mtbl->size); 2394*3af08d82Slm66018 else 2395*3af08d82Slm66018 kmem_free(ldcp->mtbl->table, ldcp->mtbl->size); 23961ae08745Sheppo mutex_destroy(&ldcp->mtbl->lock); 23971ae08745Sheppo kmem_free(ldcp->mtbl, sizeof (ldc_mtbl_t)); 23981ae08745Sheppo } 23991ae08745Sheppo 24001ae08745Sheppo /* Destroy descriptor ring and memory handle list lock */ 24011ae08745Sheppo mutex_destroy(&ldcp->exp_dlist_lock); 24021ae08745Sheppo mutex_destroy(&ldcp->imp_dlist_lock); 24031ae08745Sheppo mutex_destroy(&ldcp->mlist_lock); 24041ae08745Sheppo 24051ae08745Sheppo /* Free the stream buffer for STREAM_MODE */ 24061ae08745Sheppo if (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_bufferp) 24071ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 24081ae08745Sheppo 24091ae08745Sheppo /* Free the RX queue */ 24101ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 24111ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 24121ae08745Sheppo ldcp->tstate &= ~TS_RXQ_RDY; 24131ae08745Sheppo 24141ae08745Sheppo /* Free the TX queue */ 24151ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 24161ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 24171ae08745Sheppo ldcp->tstate &= ~TS_TXQ_RDY; 24181ae08745Sheppo 24191ae08745Sheppo mutex_exit(&ldcp->lock); 24201ae08745Sheppo 24211ae08745Sheppo /* Destroy mutex */ 2422d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 24231ae08745Sheppo mutex_destroy(&ldcp->lock); 24241ae08745Sheppo 24251ae08745Sheppo /* free channel structure */ 24261ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 24271ae08745Sheppo 24281ae08745Sheppo D1(id, "ldc_fini: (0x%llx) channel finalized\n", id); 24291ae08745Sheppo 24301ae08745Sheppo return (0); 24311ae08745Sheppo } 24321ae08745Sheppo 24331ae08745Sheppo /* 24341ae08745Sheppo * Open the LDC channel for use. It registers the TX/RX queues 24351ae08745Sheppo * with the Hypervisor. It also specifies the interrupt number 24361ae08745Sheppo * and target CPU for this channel 24371ae08745Sheppo */ 24381ae08745Sheppo int 24391ae08745Sheppo ldc_open(ldc_handle_t handle) 24401ae08745Sheppo { 24411ae08745Sheppo ldc_chan_t *ldcp; 24421ae08745Sheppo int rv; 24431ae08745Sheppo 24441ae08745Sheppo if (handle == NULL) { 24451ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_open: invalid channel handle\n"); 24461ae08745Sheppo return (EINVAL); 24471ae08745Sheppo } 24481ae08745Sheppo 24491ae08745Sheppo ldcp = (ldc_chan_t *)handle; 24501ae08745Sheppo 24511ae08745Sheppo mutex_enter(&ldcp->lock); 24521ae08745Sheppo 24531ae08745Sheppo if (ldcp->tstate < TS_INIT) { 24541ae08745Sheppo DWARN(ldcp->id, 24551ae08745Sheppo "ldc_open: (0x%llx) channel not initialized\n", ldcp->id); 24561ae08745Sheppo mutex_exit(&ldcp->lock); 24571ae08745Sheppo return (EFAULT); 24581ae08745Sheppo } 2459*3af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) >= TS_OPEN) { 24601ae08745Sheppo DWARN(ldcp->id, 24611ae08745Sheppo "ldc_open: (0x%llx) channel is already open\n", ldcp->id); 24621ae08745Sheppo mutex_exit(&ldcp->lock); 24631ae08745Sheppo return (EFAULT); 24641ae08745Sheppo } 24651ae08745Sheppo 24661ae08745Sheppo /* 24671ae08745Sheppo * Unregister/Register the tx queue with the hypervisor 24681ae08745Sheppo */ 24691ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 24701ae08745Sheppo if (rv) { 24711ae08745Sheppo cmn_err(CE_WARN, 24721ae08745Sheppo "ldc_open: (0x%lx) channel tx queue unconf failed\n", 24731ae08745Sheppo ldcp->id); 24741ae08745Sheppo mutex_exit(&ldcp->lock); 24751ae08745Sheppo return (EIO); 24761ae08745Sheppo } 24771ae08745Sheppo 24781ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 24791ae08745Sheppo if (rv) { 24801ae08745Sheppo cmn_err(CE_WARN, 24811ae08745Sheppo "ldc_open: (0x%lx) channel tx queue conf failed\n", 24821ae08745Sheppo ldcp->id); 24831ae08745Sheppo mutex_exit(&ldcp->lock); 24841ae08745Sheppo return (EIO); 24851ae08745Sheppo } 24861ae08745Sheppo 24871ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered tx queue with LDC\n", 24881ae08745Sheppo ldcp->id); 24891ae08745Sheppo 24901ae08745Sheppo /* 24911ae08745Sheppo * Unregister/Register the rx queue with the hypervisor 24921ae08745Sheppo */ 24931ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 24941ae08745Sheppo if (rv) { 24951ae08745Sheppo cmn_err(CE_WARN, 24961ae08745Sheppo "ldc_open: (0x%lx) channel rx queue unconf failed\n", 24971ae08745Sheppo ldcp->id); 24981ae08745Sheppo mutex_exit(&ldcp->lock); 24991ae08745Sheppo return (EIO); 25001ae08745Sheppo } 25011ae08745Sheppo 25021ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, ldcp->rx_q_entries); 25031ae08745Sheppo if (rv) { 25041ae08745Sheppo cmn_err(CE_WARN, 25051ae08745Sheppo "ldc_open: (0x%lx) channel rx queue conf failed\n", 25061ae08745Sheppo ldcp->id); 25071ae08745Sheppo mutex_exit(&ldcp->lock); 25081ae08745Sheppo return (EIO); 25091ae08745Sheppo } 25101ae08745Sheppo 25111ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered rx queue with LDC\n", 25121ae08745Sheppo ldcp->id); 25131ae08745Sheppo 25141ae08745Sheppo ldcp->tstate |= TS_QCONF_RDY; 25151ae08745Sheppo 25161ae08745Sheppo /* Register the channel with the channel nexus */ 25171ae08745Sheppo rv = i_ldc_register_channel(ldcp); 25181ae08745Sheppo if (rv && rv != EAGAIN) { 25191ae08745Sheppo cmn_err(CE_WARN, 25201ae08745Sheppo "ldc_open: (0x%lx) channel register failed\n", ldcp->id); 25211ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 25221ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 25231ae08745Sheppo mutex_exit(&ldcp->lock); 25241ae08745Sheppo return (EIO); 25251ae08745Sheppo } 25261ae08745Sheppo 25271ae08745Sheppo /* mark channel in OPEN state */ 25281ae08745Sheppo ldcp->status = LDC_OPEN; 25291ae08745Sheppo 25301ae08745Sheppo /* Read channel state */ 25311ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 25321ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 25331ae08745Sheppo if (rv) { 25341ae08745Sheppo cmn_err(CE_WARN, 25351ae08745Sheppo "ldc_open: (0x%lx) cannot read channel state\n", 25361ae08745Sheppo ldcp->id); 25371ae08745Sheppo (void) i_ldc_unregister_channel(ldcp); 25381ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 25391ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 25401ae08745Sheppo mutex_exit(&ldcp->lock); 25411ae08745Sheppo return (EIO); 25421ae08745Sheppo } 25431ae08745Sheppo 25441ae08745Sheppo /* 25451ae08745Sheppo * set the ACKd head to current head location for reliable & 25461ae08745Sheppo * streaming mode 25471ae08745Sheppo */ 25481ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 25491ae08745Sheppo 25501ae08745Sheppo /* mark channel ready if HV report link is UP (peer alloc'd Rx queue) */ 25511ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 25521ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 25531ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 25541ae08745Sheppo ldcp->status = LDC_READY; 25551ae08745Sheppo } 25561ae08745Sheppo 25571ae08745Sheppo /* 25581ae08745Sheppo * if channel is being opened in RAW mode - no handshake is needed 25591ae08745Sheppo * switch the channel READY and UP state 25601ae08745Sheppo */ 25611ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 25621ae08745Sheppo ldcp->tstate = TS_UP; /* set bits associated with LDC UP */ 25631ae08745Sheppo ldcp->status = LDC_UP; 25641ae08745Sheppo } 25651ae08745Sheppo 25661ae08745Sheppo mutex_exit(&ldcp->lock); 25671ae08745Sheppo 25681ae08745Sheppo /* 25691ae08745Sheppo * Increment number of open channels 25701ae08745Sheppo */ 25711ae08745Sheppo mutex_enter(&ldcssp->lock); 25721ae08745Sheppo ldcssp->channels_open++; 25731ae08745Sheppo mutex_exit(&ldcssp->lock); 25741ae08745Sheppo 2575*3af08d82Slm66018 DWARN(ldcp->id, 2576*3af08d82Slm66018 "ldc_open: (0x%llx) channel (0x%p) open for use " 2577*3af08d82Slm66018 "(tstate=0x%x, status=0x%x)\n", 2578*3af08d82Slm66018 ldcp->id, ldcp, ldcp->tstate, ldcp->status); 25791ae08745Sheppo 25801ae08745Sheppo return (0); 25811ae08745Sheppo } 25821ae08745Sheppo 25831ae08745Sheppo /* 25841ae08745Sheppo * Close the LDC connection. It will return EBUSY if there 25851ae08745Sheppo * are memory segments or descriptor rings either bound to or 25861ae08745Sheppo * mapped over the channel 25871ae08745Sheppo */ 25881ae08745Sheppo int 25891ae08745Sheppo ldc_close(ldc_handle_t handle) 25901ae08745Sheppo { 25911ae08745Sheppo ldc_chan_t *ldcp; 2592d10e4ef2Snarayan int rv = 0, retries = 0; 25931ae08745Sheppo boolean_t chk_done = B_FALSE; 25941ae08745Sheppo 25951ae08745Sheppo if (handle == NULL) { 25961ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_close: invalid channel handle\n"); 25971ae08745Sheppo return (EINVAL); 25981ae08745Sheppo } 25991ae08745Sheppo ldcp = (ldc_chan_t *)handle; 26001ae08745Sheppo 26011ae08745Sheppo mutex_enter(&ldcp->lock); 26021ae08745Sheppo 26031ae08745Sheppo /* return error if channel is not open */ 2604*3af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) < TS_OPEN) { 26051ae08745Sheppo DWARN(ldcp->id, 26061ae08745Sheppo "ldc_close: (0x%llx) channel is not open\n", ldcp->id); 26071ae08745Sheppo mutex_exit(&ldcp->lock); 26081ae08745Sheppo return (EFAULT); 26091ae08745Sheppo } 26101ae08745Sheppo 26111ae08745Sheppo /* if any memory handles, drings, are bound or mapped cannot close */ 26121ae08745Sheppo if (ldcp->mhdl_list != NULL) { 26131ae08745Sheppo DWARN(ldcp->id, 26141ae08745Sheppo "ldc_close: (0x%llx) channel has bound memory handles\n", 26151ae08745Sheppo ldcp->id); 26161ae08745Sheppo mutex_exit(&ldcp->lock); 26171ae08745Sheppo return (EBUSY); 26181ae08745Sheppo } 26191ae08745Sheppo if (ldcp->exp_dring_list != NULL) { 26201ae08745Sheppo DWARN(ldcp->id, 26211ae08745Sheppo "ldc_close: (0x%llx) channel has bound descriptor rings\n", 26221ae08745Sheppo ldcp->id); 26231ae08745Sheppo mutex_exit(&ldcp->lock); 26241ae08745Sheppo return (EBUSY); 26251ae08745Sheppo } 26261ae08745Sheppo if (ldcp->imp_dring_list != NULL) { 26271ae08745Sheppo DWARN(ldcp->id, 26281ae08745Sheppo "ldc_close: (0x%llx) channel has mapped descriptor rings\n", 26291ae08745Sheppo ldcp->id); 26301ae08745Sheppo mutex_exit(&ldcp->lock); 26311ae08745Sheppo return (EBUSY); 26321ae08745Sheppo } 26331ae08745Sheppo 2634d10e4ef2Snarayan /* Obtain Tx lock */ 2635d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2636d10e4ef2Snarayan 26371ae08745Sheppo /* 26381ae08745Sheppo * Wait for pending transmits to complete i.e Tx queue to drain 26391ae08745Sheppo * if there are pending pkts - wait 1 ms and retry again 26401ae08745Sheppo */ 26411ae08745Sheppo for (;;) { 26421ae08745Sheppo 26431ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 26441ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 26451ae08745Sheppo if (rv) { 26461ae08745Sheppo cmn_err(CE_WARN, 26471ae08745Sheppo "ldc_close: (0x%lx) cannot read qptrs\n", ldcp->id); 2648d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 26491ae08745Sheppo mutex_exit(&ldcp->lock); 26501ae08745Sheppo return (EIO); 26511ae08745Sheppo } 26521ae08745Sheppo 26531ae08745Sheppo if (ldcp->tx_head == ldcp->tx_tail || 26541ae08745Sheppo ldcp->link_state != LDC_CHANNEL_UP) { 26551ae08745Sheppo break; 26561ae08745Sheppo } 26571ae08745Sheppo 26581ae08745Sheppo if (chk_done) { 26591ae08745Sheppo DWARN(ldcp->id, 26601ae08745Sheppo "ldc_close: (0x%llx) Tx queue drain timeout\n", 26611ae08745Sheppo ldcp->id); 26621ae08745Sheppo break; 26631ae08745Sheppo } 26641ae08745Sheppo 26651ae08745Sheppo /* wait for one ms and try again */ 26661ae08745Sheppo delay(drv_usectohz(1000)); 26671ae08745Sheppo chk_done = B_TRUE; 26681ae08745Sheppo } 26691ae08745Sheppo 26701ae08745Sheppo /* 2671*3af08d82Slm66018 * Drain the Tx and Rx queues 2672*3af08d82Slm66018 */ 2673*3af08d82Slm66018 (void) i_ldc_txq_reconf(ldcp); 2674*3af08d82Slm66018 (void) i_ldc_rxq_reconf(ldcp, B_TRUE); 2675*3af08d82Slm66018 2676*3af08d82Slm66018 /* 26771ae08745Sheppo * Unregister the channel with the nexus 26781ae08745Sheppo */ 2679d10e4ef2Snarayan while ((rv = i_ldc_unregister_channel(ldcp)) != 0) { 2680d10e4ef2Snarayan 2681d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 26821ae08745Sheppo mutex_exit(&ldcp->lock); 2683d10e4ef2Snarayan 2684d10e4ef2Snarayan /* if any error other than EAGAIN return back */ 2685d10e4ef2Snarayan if (rv != EAGAIN || retries >= LDC_MAX_RETRIES) { 2686d10e4ef2Snarayan cmn_err(CE_WARN, 2687d10e4ef2Snarayan "ldc_close: (0x%lx) unregister failed, %d\n", 2688d10e4ef2Snarayan ldcp->id, rv); 26891ae08745Sheppo return (rv); 26901ae08745Sheppo } 26911ae08745Sheppo 26921ae08745Sheppo /* 2693d10e4ef2Snarayan * As there could be pending interrupts we need 2694d10e4ef2Snarayan * to wait and try again 2695d10e4ef2Snarayan */ 2696d10e4ef2Snarayan drv_usecwait(LDC_DELAY); 2697d10e4ef2Snarayan mutex_enter(&ldcp->lock); 2698d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2699d10e4ef2Snarayan retries++; 2700d10e4ef2Snarayan } 2701d10e4ef2Snarayan 2702d10e4ef2Snarayan /* 27031ae08745Sheppo * Unregister queues 27041ae08745Sheppo */ 27051ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 27061ae08745Sheppo if (rv) { 27071ae08745Sheppo cmn_err(CE_WARN, 27081ae08745Sheppo "ldc_close: (0x%lx) channel TX queue unconf failed\n", 27091ae08745Sheppo ldcp->id); 2710d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 27111ae08745Sheppo mutex_exit(&ldcp->lock); 27121ae08745Sheppo return (EIO); 27131ae08745Sheppo } 27141ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 27151ae08745Sheppo if (rv) { 27161ae08745Sheppo cmn_err(CE_WARN, 27171ae08745Sheppo "ldc_close: (0x%lx) channel RX queue unconf failed\n", 27181ae08745Sheppo ldcp->id); 2719d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 27201ae08745Sheppo mutex_exit(&ldcp->lock); 27211ae08745Sheppo return (EIO); 27221ae08745Sheppo } 27231ae08745Sheppo 27241ae08745Sheppo ldcp->tstate &= ~TS_QCONF_RDY; 27251ae08745Sheppo 27261ae08745Sheppo /* Reset channel state information */ 27271ae08745Sheppo i_ldc_reset_state(ldcp); 27281ae08745Sheppo 27291ae08745Sheppo /* Mark channel as down and in initialized state */ 27301ae08745Sheppo ldcp->tx_ackd_head = 0; 27311ae08745Sheppo ldcp->tx_head = 0; 2732*3af08d82Slm66018 ldcp->tstate = TS_IN_RESET|TS_INIT; 27331ae08745Sheppo ldcp->status = LDC_INIT; 27341ae08745Sheppo 2735d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 27361ae08745Sheppo mutex_exit(&ldcp->lock); 27371ae08745Sheppo 27381ae08745Sheppo /* Decrement number of open channels */ 27391ae08745Sheppo mutex_enter(&ldcssp->lock); 27401ae08745Sheppo ldcssp->channels_open--; 27411ae08745Sheppo mutex_exit(&ldcssp->lock); 27421ae08745Sheppo 27431ae08745Sheppo D1(ldcp->id, "ldc_close: (0x%llx) channel closed\n", ldcp->id); 27441ae08745Sheppo 27451ae08745Sheppo return (0); 27461ae08745Sheppo } 27471ae08745Sheppo 27481ae08745Sheppo /* 27491ae08745Sheppo * Register channel callback 27501ae08745Sheppo */ 27511ae08745Sheppo int 27521ae08745Sheppo ldc_reg_callback(ldc_handle_t handle, 27531ae08745Sheppo uint_t(*cb)(uint64_t event, caddr_t arg), caddr_t arg) 27541ae08745Sheppo { 27551ae08745Sheppo ldc_chan_t *ldcp; 27561ae08745Sheppo 27571ae08745Sheppo if (handle == NULL) { 27581ae08745Sheppo DWARN(DBG_ALL_LDCS, 27591ae08745Sheppo "ldc_reg_callback: invalid channel handle\n"); 27601ae08745Sheppo return (EINVAL); 27611ae08745Sheppo } 27621ae08745Sheppo if (((uint64_t)cb) < KERNELBASE) { 27631ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_reg_callback: invalid callback\n"); 27641ae08745Sheppo return (EINVAL); 27651ae08745Sheppo } 27661ae08745Sheppo ldcp = (ldc_chan_t *)handle; 27671ae08745Sheppo 27681ae08745Sheppo mutex_enter(&ldcp->lock); 27691ae08745Sheppo 27701ae08745Sheppo if (ldcp->cb) { 27711ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback exists\n", 27721ae08745Sheppo ldcp->id); 27731ae08745Sheppo mutex_exit(&ldcp->lock); 27741ae08745Sheppo return (EIO); 27751ae08745Sheppo } 27761ae08745Sheppo if (ldcp->cb_inprogress) { 27771ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback active\n", 27781ae08745Sheppo ldcp->id); 27791ae08745Sheppo mutex_exit(&ldcp->lock); 27801ae08745Sheppo return (EWOULDBLOCK); 27811ae08745Sheppo } 27821ae08745Sheppo 27831ae08745Sheppo ldcp->cb = cb; 27841ae08745Sheppo ldcp->cb_arg = arg; 27851ae08745Sheppo ldcp->cb_enabled = B_TRUE; 27861ae08745Sheppo 27871ae08745Sheppo D1(ldcp->id, 27881ae08745Sheppo "ldc_reg_callback: (0x%llx) registered callback for channel\n", 27891ae08745Sheppo ldcp->id); 27901ae08745Sheppo 27911ae08745Sheppo mutex_exit(&ldcp->lock); 27921ae08745Sheppo 27931ae08745Sheppo return (0); 27941ae08745Sheppo } 27951ae08745Sheppo 27961ae08745Sheppo /* 27971ae08745Sheppo * Unregister channel callback 27981ae08745Sheppo */ 27991ae08745Sheppo int 28001ae08745Sheppo ldc_unreg_callback(ldc_handle_t handle) 28011ae08745Sheppo { 28021ae08745Sheppo ldc_chan_t *ldcp; 28031ae08745Sheppo 28041ae08745Sheppo if (handle == NULL) { 28051ae08745Sheppo DWARN(DBG_ALL_LDCS, 28061ae08745Sheppo "ldc_unreg_callback: invalid channel handle\n"); 28071ae08745Sheppo return (EINVAL); 28081ae08745Sheppo } 28091ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28101ae08745Sheppo 28111ae08745Sheppo mutex_enter(&ldcp->lock); 28121ae08745Sheppo 28131ae08745Sheppo if (ldcp->cb == NULL) { 28141ae08745Sheppo DWARN(ldcp->id, 28151ae08745Sheppo "ldc_unreg_callback: (0x%llx) no callback exists\n", 28161ae08745Sheppo ldcp->id); 28171ae08745Sheppo mutex_exit(&ldcp->lock); 28181ae08745Sheppo return (EIO); 28191ae08745Sheppo } 28201ae08745Sheppo if (ldcp->cb_inprogress) { 28211ae08745Sheppo DWARN(ldcp->id, 28221ae08745Sheppo "ldc_unreg_callback: (0x%llx) callback active\n", 28231ae08745Sheppo ldcp->id); 28241ae08745Sheppo mutex_exit(&ldcp->lock); 28251ae08745Sheppo return (EWOULDBLOCK); 28261ae08745Sheppo } 28271ae08745Sheppo 28281ae08745Sheppo ldcp->cb = NULL; 28291ae08745Sheppo ldcp->cb_arg = NULL; 28301ae08745Sheppo ldcp->cb_enabled = B_FALSE; 28311ae08745Sheppo 28321ae08745Sheppo D1(ldcp->id, 28331ae08745Sheppo "ldc_unreg_callback: (0x%llx) unregistered callback for channel\n", 28341ae08745Sheppo ldcp->id); 28351ae08745Sheppo 28361ae08745Sheppo mutex_exit(&ldcp->lock); 28371ae08745Sheppo 28381ae08745Sheppo return (0); 28391ae08745Sheppo } 28401ae08745Sheppo 28411ae08745Sheppo 28421ae08745Sheppo /* 28431ae08745Sheppo * Bring a channel up by initiating a handshake with the peer 28441ae08745Sheppo * This call is asynchronous. It will complete at a later point 28451ae08745Sheppo * in time when the peer responds back with an RTR. 28461ae08745Sheppo */ 28471ae08745Sheppo int 28481ae08745Sheppo ldc_up(ldc_handle_t handle) 28491ae08745Sheppo { 28501ae08745Sheppo int rv; 28511ae08745Sheppo ldc_chan_t *ldcp; 28521ae08745Sheppo ldc_msg_t *ldcmsg; 2853*3af08d82Slm66018 uint64_t tx_tail, tstate; 28541ae08745Sheppo 28551ae08745Sheppo if (handle == NULL) { 28561ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_up: invalid channel handle\n"); 28571ae08745Sheppo return (EINVAL); 28581ae08745Sheppo } 28591ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28601ae08745Sheppo 28611ae08745Sheppo mutex_enter(&ldcp->lock); 28621ae08745Sheppo 2863*3af08d82Slm66018 D1(ldcp->id, "ldc_up: (0x%llx) doing channel UP\n", ldcp->id); 2864*3af08d82Slm66018 2865*3af08d82Slm66018 /* clear the reset state */ 2866*3af08d82Slm66018 tstate = ldcp->tstate; 2867*3af08d82Slm66018 ldcp->tstate &= ~TS_IN_RESET; 2868*3af08d82Slm66018 28691ae08745Sheppo if (ldcp->tstate == TS_UP) { 2870*3af08d82Slm66018 DWARN(ldcp->id, 28711ae08745Sheppo "ldc_up: (0x%llx) channel is already in UP state\n", 28721ae08745Sheppo ldcp->id); 2873*3af08d82Slm66018 2874*3af08d82Slm66018 /* mark channel as up */ 2875*3af08d82Slm66018 ldcp->status = LDC_UP; 2876*3af08d82Slm66018 2877*3af08d82Slm66018 /* 2878*3af08d82Slm66018 * if channel was in reset state and there was 2879*3af08d82Slm66018 * pending data clear interrupt state. this will 2880*3af08d82Slm66018 * trigger an interrupt, causing the RX handler to 2881*3af08d82Slm66018 * to invoke the client's callback 2882*3af08d82Slm66018 */ 2883*3af08d82Slm66018 if ((tstate & TS_IN_RESET) && 2884*3af08d82Slm66018 ldcp->rx_intr_state == LDC_INTR_PEND) { 2885*3af08d82Slm66018 DWARN(ldcp->id, 2886*3af08d82Slm66018 "ldc_up: (0x%llx) channel has pending data, " 2887*3af08d82Slm66018 "clearing interrupt\n", ldcp->id); 2888*3af08d82Slm66018 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 2889*3af08d82Slm66018 } 2890*3af08d82Slm66018 28911ae08745Sheppo mutex_exit(&ldcp->lock); 28921ae08745Sheppo return (0); 28931ae08745Sheppo } 28941ae08745Sheppo 28951ae08745Sheppo /* if the channel is in RAW mode - mark it as UP, if READY */ 28961ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW && ldcp->tstate >= TS_READY) { 28971ae08745Sheppo ldcp->tstate = TS_UP; 28981ae08745Sheppo mutex_exit(&ldcp->lock); 28991ae08745Sheppo return (0); 29001ae08745Sheppo } 29011ae08745Sheppo 29021ae08745Sheppo /* Don't start another handshake if there is one in progress */ 29031ae08745Sheppo if (ldcp->hstate) { 2904*3af08d82Slm66018 D1(ldcp->id, 29051ae08745Sheppo "ldc_up: (0x%llx) channel handshake in progress\n", 29061ae08745Sheppo ldcp->id); 29071ae08745Sheppo mutex_exit(&ldcp->lock); 29081ae08745Sheppo return (0); 29091ae08745Sheppo } 29101ae08745Sheppo 2911d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2912d10e4ef2Snarayan 29131ae08745Sheppo /* get the current tail for the LDC msg */ 29141ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 29151ae08745Sheppo if (rv) { 29161ae08745Sheppo DWARN(ldcp->id, "ldc_up: (0x%llx) cannot initiate handshake\n", 29171ae08745Sheppo ldcp->id); 2918d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 29191ae08745Sheppo mutex_exit(&ldcp->lock); 29201ae08745Sheppo return (ECONNREFUSED); 29211ae08745Sheppo } 29221ae08745Sheppo 29231ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 29241ae08745Sheppo ZERO_PKT(ldcmsg); 29251ae08745Sheppo 29261ae08745Sheppo ldcmsg->type = LDC_CTRL; 29271ae08745Sheppo ldcmsg->stype = LDC_INFO; 29281ae08745Sheppo ldcmsg->ctrl = LDC_VER; 29291ae08745Sheppo ldcp->next_vidx = 0; 29301ae08745Sheppo bcopy(&ldc_versions[0], ldcmsg->udata, sizeof (ldc_versions[0])); 29311ae08745Sheppo 29321ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_up snd ver", (uint64_t)ldcmsg); 29331ae08745Sheppo 29341ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 29351ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 29361ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 29371ae08745Sheppo 29381ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 29391ae08745Sheppo if (rv) { 29401ae08745Sheppo DWARN(ldcp->id, 29411ae08745Sheppo "ldc_up: (0x%llx) cannot initiate handshake rv=%d\n", 29421ae08745Sheppo ldcp->id, rv); 2943d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 29441ae08745Sheppo mutex_exit(&ldcp->lock); 29451ae08745Sheppo return (rv); 29461ae08745Sheppo } 29471ae08745Sheppo 29480a55fbb7Slm66018 ldcp->hstate |= TS_SENT_VER; 29491ae08745Sheppo ldcp->tx_tail = tx_tail; 29501ae08745Sheppo D1(ldcp->id, "ldc_up: (0x%llx) channel up initiated\n", ldcp->id); 29511ae08745Sheppo 2952d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 29531ae08745Sheppo mutex_exit(&ldcp->lock); 29541ae08745Sheppo 29551ae08745Sheppo return (rv); 29561ae08745Sheppo } 29571ae08745Sheppo 29581ae08745Sheppo 29591ae08745Sheppo /* 2960e1ebb9ecSlm66018 * Bring a channel down by resetting its state and queues 29611ae08745Sheppo */ 29621ae08745Sheppo int 2963e1ebb9ecSlm66018 ldc_down(ldc_handle_t handle) 29641ae08745Sheppo { 29651ae08745Sheppo ldc_chan_t *ldcp; 29661ae08745Sheppo 29671ae08745Sheppo if (handle == NULL) { 2968e1ebb9ecSlm66018 DWARN(DBG_ALL_LDCS, "ldc_down: invalid channel handle\n"); 29691ae08745Sheppo return (EINVAL); 29701ae08745Sheppo } 29711ae08745Sheppo ldcp = (ldc_chan_t *)handle; 29721ae08745Sheppo mutex_enter(&ldcp->lock); 2973d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2974*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 2975d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 29761ae08745Sheppo mutex_exit(&ldcp->lock); 29771ae08745Sheppo 29781ae08745Sheppo return (0); 29791ae08745Sheppo } 29801ae08745Sheppo 29811ae08745Sheppo /* 29821ae08745Sheppo * Get the current channel status 29831ae08745Sheppo */ 29841ae08745Sheppo int 29851ae08745Sheppo ldc_status(ldc_handle_t handle, ldc_status_t *status) 29861ae08745Sheppo { 29871ae08745Sheppo ldc_chan_t *ldcp; 29881ae08745Sheppo 29891ae08745Sheppo if (handle == NULL || status == NULL) { 29901ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_status: invalid argument\n"); 29911ae08745Sheppo return (EINVAL); 29921ae08745Sheppo } 29931ae08745Sheppo ldcp = (ldc_chan_t *)handle; 29941ae08745Sheppo 29951ae08745Sheppo *status = ((ldc_chan_t *)handle)->status; 29961ae08745Sheppo 2997*3af08d82Slm66018 DWARN(ldcp->id, 29981ae08745Sheppo "ldc_status: (0x%llx) returned status %d\n", ldcp->id, *status); 29991ae08745Sheppo return (0); 30001ae08745Sheppo } 30011ae08745Sheppo 30021ae08745Sheppo 30031ae08745Sheppo /* 30041ae08745Sheppo * Set the channel's callback mode - enable/disable callbacks 30051ae08745Sheppo */ 30061ae08745Sheppo int 30071ae08745Sheppo ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t cmode) 30081ae08745Sheppo { 30091ae08745Sheppo ldc_chan_t *ldcp; 30101ae08745Sheppo 30111ae08745Sheppo if (handle == NULL) { 30121ae08745Sheppo DWARN(DBG_ALL_LDCS, 30131ae08745Sheppo "ldc_set_intr_mode: invalid channel handle\n"); 30141ae08745Sheppo return (EINVAL); 30151ae08745Sheppo } 30161ae08745Sheppo ldcp = (ldc_chan_t *)handle; 30171ae08745Sheppo 30181ae08745Sheppo /* 30191ae08745Sheppo * Record no callbacks should be invoked 30201ae08745Sheppo */ 30211ae08745Sheppo mutex_enter(&ldcp->lock); 30221ae08745Sheppo 30231ae08745Sheppo switch (cmode) { 30241ae08745Sheppo case LDC_CB_DISABLE: 30251ae08745Sheppo if (!ldcp->cb_enabled) { 30261ae08745Sheppo DWARN(ldcp->id, 30271ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks disabled\n", 30281ae08745Sheppo ldcp->id); 30291ae08745Sheppo break; 30301ae08745Sheppo } 30311ae08745Sheppo ldcp->cb_enabled = B_FALSE; 30321ae08745Sheppo 30331ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) disabled callbacks\n", 30341ae08745Sheppo ldcp->id); 30351ae08745Sheppo break; 30361ae08745Sheppo 30371ae08745Sheppo case LDC_CB_ENABLE: 30381ae08745Sheppo if (ldcp->cb_enabled) { 30391ae08745Sheppo DWARN(ldcp->id, 30401ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks enabled\n", 30411ae08745Sheppo ldcp->id); 30421ae08745Sheppo break; 30431ae08745Sheppo } 30441ae08745Sheppo ldcp->cb_enabled = B_TRUE; 30451ae08745Sheppo 30461ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) enabled callbacks\n", 30471ae08745Sheppo ldcp->id); 30481ae08745Sheppo break; 30491ae08745Sheppo } 30501ae08745Sheppo 30511ae08745Sheppo mutex_exit(&ldcp->lock); 30521ae08745Sheppo 30531ae08745Sheppo return (0); 30541ae08745Sheppo } 30551ae08745Sheppo 30561ae08745Sheppo /* 30571ae08745Sheppo * Check to see if there are packets on the incoming queue 3058e1ebb9ecSlm66018 * Will return hasdata = B_FALSE if there are no packets 30591ae08745Sheppo */ 30601ae08745Sheppo int 3061e1ebb9ecSlm66018 ldc_chkq(ldc_handle_t handle, boolean_t *hasdata) 30621ae08745Sheppo { 30631ae08745Sheppo int rv; 30641ae08745Sheppo uint64_t rx_head, rx_tail; 30651ae08745Sheppo ldc_chan_t *ldcp; 30661ae08745Sheppo 30671ae08745Sheppo if (handle == NULL) { 30681ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_chkq: invalid channel handle\n"); 30691ae08745Sheppo return (EINVAL); 30701ae08745Sheppo } 30711ae08745Sheppo ldcp = (ldc_chan_t *)handle; 30721ae08745Sheppo 3073e1ebb9ecSlm66018 *hasdata = B_FALSE; 30741ae08745Sheppo 30751ae08745Sheppo mutex_enter(&ldcp->lock); 30761ae08745Sheppo 30771ae08745Sheppo if (ldcp->tstate != TS_UP) { 30781ae08745Sheppo D1(ldcp->id, 30791ae08745Sheppo "ldc_chkq: (0x%llx) channel is not up\n", ldcp->id); 30801ae08745Sheppo mutex_exit(&ldcp->lock); 30811ae08745Sheppo return (ECONNRESET); 30821ae08745Sheppo } 30831ae08745Sheppo 30841ae08745Sheppo /* Read packet(s) from the queue */ 30851ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 30861ae08745Sheppo &ldcp->link_state); 30871ae08745Sheppo if (rv != 0) { 30881ae08745Sheppo cmn_err(CE_WARN, 30891ae08745Sheppo "ldc_chkq: (0x%lx) unable to read queue ptrs", ldcp->id); 30901ae08745Sheppo mutex_exit(&ldcp->lock); 30911ae08745Sheppo return (EIO); 30921ae08745Sheppo } 30931ae08745Sheppo /* reset the channel state if the channel went down */ 30941ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 30951ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 3096d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3097*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3098d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 30991ae08745Sheppo mutex_exit(&ldcp->lock); 31001ae08745Sheppo return (ECONNRESET); 31011ae08745Sheppo } 31021ae08745Sheppo 31034bac2208Snarayan if ((rx_head != rx_tail) || 31044bac2208Snarayan (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_remains > 0)) { 31054bac2208Snarayan D1(ldcp->id, 31064bac2208Snarayan "ldc_chkq: (0x%llx) queue has pkt(s) or buffered data\n", 31074bac2208Snarayan ldcp->id); 3108e1ebb9ecSlm66018 *hasdata = B_TRUE; 31091ae08745Sheppo } 31101ae08745Sheppo 31111ae08745Sheppo mutex_exit(&ldcp->lock); 31121ae08745Sheppo 31131ae08745Sheppo return (0); 31141ae08745Sheppo } 31151ae08745Sheppo 31161ae08745Sheppo 31171ae08745Sheppo /* 31181ae08745Sheppo * Read 'size' amount of bytes or less. If incoming buffer 31191ae08745Sheppo * is more than 'size', ENOBUFS is returned. 31201ae08745Sheppo * 31211ae08745Sheppo * On return, size contains the number of bytes read. 31221ae08745Sheppo */ 31231ae08745Sheppo int 31241ae08745Sheppo ldc_read(ldc_handle_t handle, caddr_t bufp, size_t *sizep) 31251ae08745Sheppo { 31261ae08745Sheppo ldc_chan_t *ldcp; 31271ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 31281ae08745Sheppo int rv = 0, exit_val; 31291ae08745Sheppo 31301ae08745Sheppo if (handle == NULL) { 31311ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_read: invalid channel handle\n"); 31321ae08745Sheppo return (EINVAL); 31331ae08745Sheppo } 31341ae08745Sheppo 31351ae08745Sheppo ldcp = (ldc_chan_t *)handle; 31361ae08745Sheppo 31371ae08745Sheppo /* channel lock */ 31381ae08745Sheppo mutex_enter(&ldcp->lock); 31391ae08745Sheppo 31401ae08745Sheppo if (ldcp->tstate != TS_UP) { 31411ae08745Sheppo DWARN(ldcp->id, 31421ae08745Sheppo "ldc_read: (0x%llx) channel is not in UP state\n", 31431ae08745Sheppo ldcp->id); 31441ae08745Sheppo exit_val = ECONNRESET; 31451ae08745Sheppo } else { 31461ae08745Sheppo exit_val = ldcp->read_p(ldcp, bufp, sizep); 31471ae08745Sheppo } 31481ae08745Sheppo 31491ae08745Sheppo /* 31501ae08745Sheppo * if queue has been drained - clear interrupt 31511ae08745Sheppo */ 31521ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 31531ae08745Sheppo &ldcp->link_state); 3154*3af08d82Slm66018 3155*3af08d82Slm66018 ASSERT(rv == 0); 3156*3af08d82Slm66018 3157*3af08d82Slm66018 if (exit_val == 0) { 3158*3af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 3159*3af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 3160*3af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 3161*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3162*3af08d82Slm66018 exit_val = ECONNRESET; 3163*3af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 3164*3af08d82Slm66018 } 3165*3af08d82Slm66018 if ((rv == 0) && 3166*3af08d82Slm66018 (ldcp->rx_intr_state == LDC_INTR_PEND) && 3167*3af08d82Slm66018 (rx_head == rx_tail)) { 31681ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 31691ae08745Sheppo } 3170*3af08d82Slm66018 } 31711ae08745Sheppo 31721ae08745Sheppo mutex_exit(&ldcp->lock); 31731ae08745Sheppo return (exit_val); 31741ae08745Sheppo } 31751ae08745Sheppo 31761ae08745Sheppo /* 31771ae08745Sheppo * Basic raw mondo read - 31781ae08745Sheppo * no interpretation of mondo contents at all. 31791ae08745Sheppo * 31801ae08745Sheppo * Enter and exit with ldcp->lock held by caller 31811ae08745Sheppo */ 31821ae08745Sheppo static int 31831ae08745Sheppo i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 31841ae08745Sheppo { 31851ae08745Sheppo uint64_t q_size_mask; 31861ae08745Sheppo ldc_msg_t *msgp; 31871ae08745Sheppo uint8_t *msgbufp; 31881ae08745Sheppo int rv = 0, space; 31891ae08745Sheppo uint64_t rx_head, rx_tail; 31901ae08745Sheppo 31911ae08745Sheppo space = *sizep; 31921ae08745Sheppo 31931ae08745Sheppo if (space < LDC_PAYLOAD_SIZE_RAW) 31941ae08745Sheppo return (ENOBUFS); 31951ae08745Sheppo 31961ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 31971ae08745Sheppo 31981ae08745Sheppo /* compute mask for increment */ 31991ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 32001ae08745Sheppo 32011ae08745Sheppo /* 32021ae08745Sheppo * Read packet(s) from the queue 32031ae08745Sheppo */ 32041ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 32051ae08745Sheppo &ldcp->link_state); 32061ae08745Sheppo if (rv != 0) { 32071ae08745Sheppo cmn_err(CE_WARN, 32081ae08745Sheppo "ldc_read_raw: (0x%lx) unable to read queue ptrs", 32091ae08745Sheppo ldcp->id); 32101ae08745Sheppo return (EIO); 32111ae08745Sheppo } 32121ae08745Sheppo D1(ldcp->id, "ldc_read_raw: (0x%llx) rxh=0x%llx," 32131ae08745Sheppo " rxt=0x%llx, st=0x%llx\n", 32141ae08745Sheppo ldcp->id, rx_head, rx_tail, ldcp->link_state); 32151ae08745Sheppo 32161ae08745Sheppo /* reset the channel state if the channel went down */ 3217*3af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 3218*3af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 3219d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3220*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3221d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 32221ae08745Sheppo return (ECONNRESET); 32231ae08745Sheppo } 32241ae08745Sheppo 32251ae08745Sheppo /* 32261ae08745Sheppo * Check for empty queue 32271ae08745Sheppo */ 32281ae08745Sheppo if (rx_head == rx_tail) { 32291ae08745Sheppo *sizep = 0; 32301ae08745Sheppo return (0); 32311ae08745Sheppo } 32321ae08745Sheppo 32331ae08745Sheppo /* get the message */ 32341ae08745Sheppo msgp = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 32351ae08745Sheppo 32361ae08745Sheppo /* if channel is in RAW mode, copy data and return */ 32371ae08745Sheppo msgbufp = (uint8_t *)&(msgp->raw[0]); 32381ae08745Sheppo 32391ae08745Sheppo bcopy(msgbufp, target_bufp, LDC_PAYLOAD_SIZE_RAW); 32401ae08745Sheppo 32411ae08745Sheppo DUMP_PAYLOAD(ldcp->id, msgbufp); 32421ae08745Sheppo 32431ae08745Sheppo *sizep = LDC_PAYLOAD_SIZE_RAW; 32441ae08745Sheppo 32451ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) & q_size_mask; 32460a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_head); 32471ae08745Sheppo 32481ae08745Sheppo return (rv); 32491ae08745Sheppo } 32501ae08745Sheppo 32511ae08745Sheppo /* 32521ae08745Sheppo * Process LDC mondos to build larger packets 32531ae08745Sheppo * with either un-reliable or reliable delivery. 32541ae08745Sheppo * 32551ae08745Sheppo * Enter and exit with ldcp->lock held by caller 32561ae08745Sheppo */ 32571ae08745Sheppo static int 32581ae08745Sheppo i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 32591ae08745Sheppo { 32601ae08745Sheppo int rv = 0; 32611ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 32621ae08745Sheppo uint64_t curr_head = 0; 32631ae08745Sheppo ldc_msg_t *msg; 32641ae08745Sheppo caddr_t target; 32651ae08745Sheppo size_t len = 0, bytes_read = 0; 32660a55fbb7Slm66018 int retries = 0; 32671ae08745Sheppo uint64_t q_size_mask; 3268d10e4ef2Snarayan uint64_t first_fragment = 0; 32691ae08745Sheppo 32701ae08745Sheppo target = target_bufp; 32711ae08745Sheppo 32721ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 32731ae08745Sheppo 3274*3af08d82Slm66018 /* check if the buffer and size are valid */ 3275*3af08d82Slm66018 if (target_bufp == NULL || *sizep == 0) { 3276*3af08d82Slm66018 DWARN(ldcp->id, "ldc_read: (0x%llx) invalid buffer/size\n", 3277*3af08d82Slm66018 ldcp->id); 3278*3af08d82Slm66018 return (EINVAL); 3279*3af08d82Slm66018 } 3280*3af08d82Slm66018 32811ae08745Sheppo /* compute mask for increment */ 32821ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 32831ae08745Sheppo 32841ae08745Sheppo /* 32851ae08745Sheppo * Read packet(s) from the queue 32861ae08745Sheppo */ 32871ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &curr_head, &rx_tail, 32881ae08745Sheppo &ldcp->link_state); 32891ae08745Sheppo if (rv != 0) { 3290*3af08d82Slm66018 cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs", 32911ae08745Sheppo ldcp->id); 3292*3af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 3293*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 3294*3af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 3295*3af08d82Slm66018 return (ECONNRESET); 32961ae08745Sheppo } 32971ae08745Sheppo D1(ldcp->id, "ldc_read: (0x%llx) chd=0x%llx, tl=0x%llx, st=0x%llx\n", 32981ae08745Sheppo ldcp->id, curr_head, rx_tail, ldcp->link_state); 32991ae08745Sheppo 33001ae08745Sheppo /* reset the channel state if the channel went down */ 3301*3af08d82Slm66018 if (ldcp->link_state != LDC_CHANNEL_UP) 3302*3af08d82Slm66018 goto channel_is_reset; 33031ae08745Sheppo 33041ae08745Sheppo for (;;) { 33051ae08745Sheppo 33061ae08745Sheppo if (curr_head == rx_tail) { 33071ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, 33081ae08745Sheppo &rx_head, &rx_tail, &ldcp->link_state); 33091ae08745Sheppo if (rv != 0) { 33101ae08745Sheppo cmn_err(CE_WARN, 33111ae08745Sheppo "ldc_read: (0x%lx) cannot read queue ptrs", 33121ae08745Sheppo ldcp->id); 3313d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3314*3af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 3315d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 33161ae08745Sheppo return (ECONNRESET); 33171ae08745Sheppo } 3318*3af08d82Slm66018 if (ldcp->link_state != LDC_CHANNEL_UP) 3319*3af08d82Slm66018 goto channel_is_reset; 33201ae08745Sheppo 33211ae08745Sheppo if (curr_head == rx_tail) { 33221ae08745Sheppo 33231ae08745Sheppo /* If in the middle of a fragmented xfer */ 3324d10e4ef2Snarayan if (first_fragment != 0) { 33250a55fbb7Slm66018 33260a55fbb7Slm66018 /* wait for ldc_delay usecs */ 33270a55fbb7Slm66018 drv_usecwait(ldc_delay); 33280a55fbb7Slm66018 33290a55fbb7Slm66018 if (++retries < ldc_max_retries) 33301ae08745Sheppo continue; 33310a55fbb7Slm66018 33321ae08745Sheppo *sizep = 0; 3333d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 3334*3af08d82Slm66018 DWARN(DBG_ALL_LDCS, "ldc_read: " 3335*3af08d82Slm66018 "(0x%llx) read timeout", 33361ae08745Sheppo ldcp->id); 3337*3af08d82Slm66018 return (EAGAIN); 33381ae08745Sheppo } 33391ae08745Sheppo *sizep = 0; 33401ae08745Sheppo break; 33411ae08745Sheppo } 3342*3af08d82Slm66018 } 33430a55fbb7Slm66018 retries = 0; 33441ae08745Sheppo 33451ae08745Sheppo D2(ldcp->id, 33461ae08745Sheppo "ldc_read: (0x%llx) chd=0x%llx, rxhd=0x%llx, rxtl=0x%llx\n", 33471ae08745Sheppo ldcp->id, curr_head, rx_head, rx_tail); 33481ae08745Sheppo 33491ae08745Sheppo /* get the message */ 33501ae08745Sheppo msg = (ldc_msg_t *)(ldcp->rx_q_va + curr_head); 33511ae08745Sheppo 33521ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_read received pkt", 33531ae08745Sheppo ldcp->rx_q_va + curr_head); 33541ae08745Sheppo 33551ae08745Sheppo /* Check the message ID for the message received */ 33561ae08745Sheppo if ((rv = i_ldc_check_seqid(ldcp, msg)) != 0) { 33571ae08745Sheppo 33581ae08745Sheppo DWARN(ldcp->id, "ldc_read: (0x%llx) seqid error, " 33591ae08745Sheppo "q_ptrs=0x%lx,0x%lx", ldcp->id, rx_head, rx_tail); 33601ae08745Sheppo 33610a55fbb7Slm66018 /* throw away data */ 33620a55fbb7Slm66018 bytes_read = 0; 33630a55fbb7Slm66018 33641ae08745Sheppo /* Reset last_msg_rcd to start of message */ 3365d10e4ef2Snarayan if (first_fragment != 0) { 3366d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 3367d10e4ef2Snarayan first_fragment = 0; 33681ae08745Sheppo } 33691ae08745Sheppo /* 33701ae08745Sheppo * Send a NACK -- invalid seqid 33711ae08745Sheppo * get the current tail for the response 33721ae08745Sheppo */ 33731ae08745Sheppo rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK, 33741ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 33751ae08745Sheppo if (rv) { 33761ae08745Sheppo cmn_err(CE_NOTE, 33771ae08745Sheppo "ldc_read: (0x%lx) err sending " 33781ae08745Sheppo "NACK msg\n", ldcp->id); 3379d10e4ef2Snarayan 3380d10e4ef2Snarayan /* if cannot send NACK - reset channel */ 3381d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3382*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3383d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3384d10e4ef2Snarayan rv = ECONNRESET; 3385d10e4ef2Snarayan break; 33861ae08745Sheppo } 33871ae08745Sheppo 33881ae08745Sheppo /* purge receive queue */ 33890a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 33901ae08745Sheppo 33911ae08745Sheppo break; 33921ae08745Sheppo } 33931ae08745Sheppo 33941ae08745Sheppo /* 33951ae08745Sheppo * Process any messages of type CTRL messages 3396e1ebb9ecSlm66018 * Future implementations should try to pass these 3397e1ebb9ecSlm66018 * to LDC link by resetting the intr state. 33981ae08745Sheppo * 33991ae08745Sheppo * NOTE: not done as a switch() as type can be both ctrl+data 34001ae08745Sheppo */ 34011ae08745Sheppo if (msg->type & LDC_CTRL) { 34021ae08745Sheppo if (rv = i_ldc_ctrlmsg(ldcp, msg)) { 34031ae08745Sheppo if (rv == EAGAIN) 34041ae08745Sheppo continue; 34050a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 34061ae08745Sheppo *sizep = 0; 34071ae08745Sheppo bytes_read = 0; 34081ae08745Sheppo break; 34091ae08745Sheppo } 34101ae08745Sheppo } 34111ae08745Sheppo 34121ae08745Sheppo /* process data ACKs */ 34131ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 3414d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 3415d10e4ef2Snarayan *sizep = 0; 3416d10e4ef2Snarayan bytes_read = 0; 3417d10e4ef2Snarayan break; 3418d10e4ef2Snarayan } 34191ae08745Sheppo } 34201ae08745Sheppo 34211ae08745Sheppo /* process data messages */ 34221ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 34231ae08745Sheppo 34241ae08745Sheppo uint8_t *msgbuf = (uint8_t *)( 34251ae08745Sheppo (ldcp->mode == LDC_MODE_RELIABLE || 34261ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 34271ae08745Sheppo ? msg->rdata : msg->udata); 34281ae08745Sheppo 34291ae08745Sheppo D2(ldcp->id, 34301ae08745Sheppo "ldc_read: (0x%llx) received data msg\n", ldcp->id); 34311ae08745Sheppo 34321ae08745Sheppo /* get the packet length */ 34331ae08745Sheppo len = (msg->env & LDC_LEN_MASK); 34341ae08745Sheppo 34351ae08745Sheppo /* 34361ae08745Sheppo * FUTURE OPTIMIZATION: 34371ae08745Sheppo * dont need to set q head for every 34381ae08745Sheppo * packet we read just need to do this when 34391ae08745Sheppo * we are done or need to wait for more 34401ae08745Sheppo * mondos to make a full packet - this is 34411ae08745Sheppo * currently expensive. 34421ae08745Sheppo */ 34431ae08745Sheppo 3444d10e4ef2Snarayan if (first_fragment == 0) { 34451ae08745Sheppo 34461ae08745Sheppo /* 34471ae08745Sheppo * first packets should always have the start 34481ae08745Sheppo * bit set (even for a single packet). If not 34491ae08745Sheppo * throw away the packet 34501ae08745Sheppo */ 34511ae08745Sheppo if (!(msg->env & LDC_FRAG_START)) { 34521ae08745Sheppo 34531ae08745Sheppo DWARN(DBG_ALL_LDCS, 34541ae08745Sheppo "ldc_read: (0x%llx) not start - " 34551ae08745Sheppo "frag=%x\n", ldcp->id, 34561ae08745Sheppo (msg->env) & LDC_FRAG_MASK); 34571ae08745Sheppo 34581ae08745Sheppo /* toss pkt, inc head, cont reading */ 34591ae08745Sheppo bytes_read = 0; 34601ae08745Sheppo target = target_bufp; 34611ae08745Sheppo curr_head = 34621ae08745Sheppo (curr_head + LDC_PACKET_SIZE) 34631ae08745Sheppo & q_size_mask; 34641ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, 34651ae08745Sheppo curr_head)) 34661ae08745Sheppo break; 34671ae08745Sheppo 34681ae08745Sheppo continue; 34691ae08745Sheppo } 34701ae08745Sheppo 3471d10e4ef2Snarayan first_fragment = msg->seqid; 34721ae08745Sheppo } else { 34731ae08745Sheppo /* check to see if this is a pkt w/ START bit */ 34741ae08745Sheppo if (msg->env & LDC_FRAG_START) { 34751ae08745Sheppo DWARN(DBG_ALL_LDCS, 34761ae08745Sheppo "ldc_read:(0x%llx) unexpected pkt" 34771ae08745Sheppo " env=0x%x discarding %d bytes," 34781ae08745Sheppo " lastmsg=%d, currentmsg=%d\n", 34791ae08745Sheppo ldcp->id, msg->env&LDC_FRAG_MASK, 34801ae08745Sheppo bytes_read, ldcp->last_msg_rcd, 34811ae08745Sheppo msg->seqid); 34821ae08745Sheppo 34831ae08745Sheppo /* throw data we have read so far */ 34841ae08745Sheppo bytes_read = 0; 34851ae08745Sheppo target = target_bufp; 3486d10e4ef2Snarayan first_fragment = msg->seqid; 34871ae08745Sheppo 34881ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, 34891ae08745Sheppo curr_head)) 34901ae08745Sheppo break; 34911ae08745Sheppo } 34921ae08745Sheppo } 34931ae08745Sheppo 34941ae08745Sheppo /* copy (next) pkt into buffer */ 34951ae08745Sheppo if (len <= (*sizep - bytes_read)) { 34961ae08745Sheppo bcopy(msgbuf, target, len); 34971ae08745Sheppo target += len; 34981ae08745Sheppo bytes_read += len; 34991ae08745Sheppo } else { 35001ae08745Sheppo /* 35011ae08745Sheppo * there is not enough space in the buffer to 35021ae08745Sheppo * read this pkt. throw message away & continue 35031ae08745Sheppo * reading data from queue 35041ae08745Sheppo */ 35051ae08745Sheppo DWARN(DBG_ALL_LDCS, 35061ae08745Sheppo "ldc_read: (0x%llx) buffer too small, " 35071ae08745Sheppo "head=0x%lx, expect=%d, got=%d\n", ldcp->id, 35081ae08745Sheppo curr_head, *sizep, bytes_read+len); 35091ae08745Sheppo 3510d10e4ef2Snarayan first_fragment = 0; 35111ae08745Sheppo target = target_bufp; 35121ae08745Sheppo bytes_read = 0; 35131ae08745Sheppo 35141ae08745Sheppo /* throw away everything received so far */ 35151ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) 35161ae08745Sheppo break; 35171ae08745Sheppo 35181ae08745Sheppo /* continue reading remaining pkts */ 35191ae08745Sheppo continue; 35201ae08745Sheppo } 35211ae08745Sheppo } 35221ae08745Sheppo 35231ae08745Sheppo /* set the message id */ 35241ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 35251ae08745Sheppo 35261ae08745Sheppo /* move the head one position */ 35271ae08745Sheppo curr_head = (curr_head + LDC_PACKET_SIZE) & q_size_mask; 35281ae08745Sheppo 35291ae08745Sheppo if (msg->env & LDC_FRAG_STOP) { 35301ae08745Sheppo 35311ae08745Sheppo /* 35321ae08745Sheppo * All pkts that are part of this fragmented transfer 35331ae08745Sheppo * have been read or this was a single pkt read 35341ae08745Sheppo * or there was an error 35351ae08745Sheppo */ 35361ae08745Sheppo 35371ae08745Sheppo /* set the queue head */ 35381ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) 35391ae08745Sheppo bytes_read = 0; 35401ae08745Sheppo 35411ae08745Sheppo *sizep = bytes_read; 35421ae08745Sheppo 35431ae08745Sheppo break; 35441ae08745Sheppo } 35451ae08745Sheppo 35461ae08745Sheppo /* advance head if it is a DATA ACK */ 35471ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 35481ae08745Sheppo 35491ae08745Sheppo /* set the queue head */ 35501ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) { 35511ae08745Sheppo bytes_read = 0; 35521ae08745Sheppo break; 35531ae08745Sheppo } 35541ae08745Sheppo 35551ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) set ACK qhead 0x%llx", 35561ae08745Sheppo ldcp->id, curr_head); 35571ae08745Sheppo } 35581ae08745Sheppo 35591ae08745Sheppo } /* for (;;) */ 35601ae08745Sheppo 35611ae08745Sheppo 35621ae08745Sheppo /* 35631ae08745Sheppo * If useful data was read - Send msg ACK 35641ae08745Sheppo * OPTIMIZE: do not send ACK for all msgs - use some frequency 35651ae08745Sheppo */ 35661ae08745Sheppo if ((bytes_read > 0) && (ldcp->mode == LDC_MODE_RELIABLE || 35671ae08745Sheppo ldcp->mode == LDC_MODE_STREAM)) { 35681ae08745Sheppo 35691ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_DATA, LDC_ACK, 0); 3570*3af08d82Slm66018 if (rv && rv != EWOULDBLOCK) { 35711ae08745Sheppo cmn_err(CE_NOTE, 35721ae08745Sheppo "ldc_read: (0x%lx) cannot send ACK\n", ldcp->id); 3573d10e4ef2Snarayan 3574d10e4ef2Snarayan /* if cannot send ACK - reset channel */ 3575*3af08d82Slm66018 goto channel_is_reset; 35761ae08745Sheppo } 35771ae08745Sheppo } 35781ae08745Sheppo 35791ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) end size=%d", ldcp->id, *sizep); 35801ae08745Sheppo 35811ae08745Sheppo return (rv); 3582*3af08d82Slm66018 3583*3af08d82Slm66018 channel_is_reset: 3584*3af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 3585*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3586*3af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 3587*3af08d82Slm66018 return (ECONNRESET); 35881ae08745Sheppo } 35891ae08745Sheppo 35901ae08745Sheppo /* 35911ae08745Sheppo * Use underlying reliable packet mechanism to fetch 35921ae08745Sheppo * and buffer incoming packets so we can hand them back as 35931ae08745Sheppo * a basic byte stream. 35941ae08745Sheppo * 35951ae08745Sheppo * Enter and exit with ldcp->lock held by caller 35961ae08745Sheppo */ 35971ae08745Sheppo static int 35981ae08745Sheppo i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 35991ae08745Sheppo { 36001ae08745Sheppo int rv; 36011ae08745Sheppo size_t size; 36021ae08745Sheppo 36031ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 36041ae08745Sheppo 36051ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) buffer size=%d", 36061ae08745Sheppo ldcp->id, *sizep); 36071ae08745Sheppo 36081ae08745Sheppo if (ldcp->stream_remains == 0) { 36091ae08745Sheppo size = ldcp->mtu; 36101ae08745Sheppo rv = i_ldc_read_packet(ldcp, 36111ae08745Sheppo (caddr_t)ldcp->stream_bufferp, &size); 36121ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: read packet (0x%llx) size=%d", 36131ae08745Sheppo ldcp->id, size); 36141ae08745Sheppo 36151ae08745Sheppo if (rv != 0) 36161ae08745Sheppo return (rv); 36171ae08745Sheppo 36181ae08745Sheppo ldcp->stream_remains = size; 36191ae08745Sheppo ldcp->stream_offset = 0; 36201ae08745Sheppo } 36211ae08745Sheppo 36221ae08745Sheppo size = MIN(ldcp->stream_remains, *sizep); 36231ae08745Sheppo 36241ae08745Sheppo bcopy(ldcp->stream_bufferp + ldcp->stream_offset, target_bufp, size); 36251ae08745Sheppo ldcp->stream_offset += size; 36261ae08745Sheppo ldcp->stream_remains -= size; 36271ae08745Sheppo 36281ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) fill from buffer size=%d", 36291ae08745Sheppo ldcp->id, size); 36301ae08745Sheppo 36311ae08745Sheppo *sizep = size; 36321ae08745Sheppo return (0); 36331ae08745Sheppo } 36341ae08745Sheppo 36351ae08745Sheppo /* 36361ae08745Sheppo * Write specified amount of bytes to the channel 36371ae08745Sheppo * in multiple pkts of pkt_payload size. Each 36381ae08745Sheppo * packet is tagged with an unique packet ID in 3639e1ebb9ecSlm66018 * the case of a reliable link. 36401ae08745Sheppo * 36411ae08745Sheppo * On return, size contains the number of bytes written. 36421ae08745Sheppo */ 36431ae08745Sheppo int 36441ae08745Sheppo ldc_write(ldc_handle_t handle, caddr_t buf, size_t *sizep) 36451ae08745Sheppo { 36461ae08745Sheppo ldc_chan_t *ldcp; 36471ae08745Sheppo int rv = 0; 36481ae08745Sheppo 36491ae08745Sheppo if (handle == NULL) { 36501ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_write: invalid channel handle\n"); 36511ae08745Sheppo return (EINVAL); 36521ae08745Sheppo } 36531ae08745Sheppo ldcp = (ldc_chan_t *)handle; 36541ae08745Sheppo 3655d10e4ef2Snarayan /* check if writes can occur */ 3656d10e4ef2Snarayan if (!mutex_tryenter(&ldcp->tx_lock)) { 3657d10e4ef2Snarayan /* 3658d10e4ef2Snarayan * Could not get the lock - channel could 3659d10e4ef2Snarayan * be in the process of being unconfigured 3660d10e4ef2Snarayan * or reader has encountered an error 3661d10e4ef2Snarayan */ 3662d10e4ef2Snarayan return (EAGAIN); 3663d10e4ef2Snarayan } 36641ae08745Sheppo 36651ae08745Sheppo /* check if non-zero data to write */ 36661ae08745Sheppo if (buf == NULL || sizep == NULL) { 36671ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) invalid data write\n", 36681ae08745Sheppo ldcp->id); 3669d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 36701ae08745Sheppo return (EINVAL); 36711ae08745Sheppo } 36721ae08745Sheppo 36731ae08745Sheppo if (*sizep == 0) { 36741ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write size of zero\n", 36751ae08745Sheppo ldcp->id); 3676d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 36771ae08745Sheppo return (0); 36781ae08745Sheppo } 36791ae08745Sheppo 36801ae08745Sheppo /* Check if channel is UP for data exchange */ 36811ae08745Sheppo if (ldcp->tstate != TS_UP) { 36821ae08745Sheppo DWARN(ldcp->id, 36831ae08745Sheppo "ldc_write: (0x%llx) channel is not in UP state\n", 36841ae08745Sheppo ldcp->id); 36851ae08745Sheppo *sizep = 0; 36861ae08745Sheppo rv = ECONNRESET; 36871ae08745Sheppo } else { 36881ae08745Sheppo rv = ldcp->write_p(ldcp, buf, sizep); 36891ae08745Sheppo } 36901ae08745Sheppo 3691d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 36921ae08745Sheppo 36931ae08745Sheppo return (rv); 36941ae08745Sheppo } 36951ae08745Sheppo 36961ae08745Sheppo /* 36971ae08745Sheppo * Write a raw packet to the channel 36981ae08745Sheppo * On return, size contains the number of bytes written. 36991ae08745Sheppo */ 37001ae08745Sheppo static int 37011ae08745Sheppo i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 37021ae08745Sheppo { 37031ae08745Sheppo ldc_msg_t *ldcmsg; 37041ae08745Sheppo uint64_t tx_head, tx_tail, new_tail; 37051ae08745Sheppo int rv = 0; 37061ae08745Sheppo size_t size; 37071ae08745Sheppo 3708d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 37091ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RAW); 37101ae08745Sheppo 37111ae08745Sheppo size = *sizep; 37121ae08745Sheppo 37131ae08745Sheppo /* 37141ae08745Sheppo * Check to see if the packet size is less than or 37151ae08745Sheppo * equal to packet size support in raw mode 37161ae08745Sheppo */ 37171ae08745Sheppo if (size > ldcp->pkt_payload) { 37181ae08745Sheppo DWARN(ldcp->id, 37191ae08745Sheppo "ldc_write: (0x%llx) invalid size (0x%llx) for RAW mode\n", 37201ae08745Sheppo ldcp->id, *sizep); 37211ae08745Sheppo *sizep = 0; 37221ae08745Sheppo return (EMSGSIZE); 37231ae08745Sheppo } 37241ae08745Sheppo 37251ae08745Sheppo /* get the qptrs for the tx queue */ 37261ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 37271ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 37281ae08745Sheppo if (rv != 0) { 37291ae08745Sheppo cmn_err(CE_WARN, 37301ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 37311ae08745Sheppo *sizep = 0; 37321ae08745Sheppo return (EIO); 37331ae08745Sheppo } 37341ae08745Sheppo 37351ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 37361ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 37371ae08745Sheppo DWARN(ldcp->id, 37381ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 3739d10e4ef2Snarayan 37401ae08745Sheppo *sizep = 0; 3741d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 3742*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3743d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3744d10e4ef2Snarayan } else { 3745d10e4ef2Snarayan /* 3746d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3747d10e4ef2Snarayan * and Tx lock in correct order 3748d10e4ef2Snarayan */ 3749d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3750d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3751d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3752*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3753d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3754d10e4ef2Snarayan } 37551ae08745Sheppo return (ECONNRESET); 37561ae08745Sheppo } 37571ae08745Sheppo 37581ae08745Sheppo tx_tail = ldcp->tx_tail; 37591ae08745Sheppo tx_head = ldcp->tx_head; 37601ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) & 37611ae08745Sheppo ((ldcp->tx_q_entries-1) << LDC_PACKET_SHIFT); 37621ae08745Sheppo 37631ae08745Sheppo if (new_tail == tx_head) { 37641ae08745Sheppo DWARN(DBG_ALL_LDCS, 37651ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 37661ae08745Sheppo *sizep = 0; 37671ae08745Sheppo return (EWOULDBLOCK); 37681ae08745Sheppo } 37691ae08745Sheppo 37701ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 37711ae08745Sheppo ldcp->id, size); 37721ae08745Sheppo 37731ae08745Sheppo /* Send the data now */ 37741ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 37751ae08745Sheppo 37761ae08745Sheppo /* copy the data into pkt */ 37771ae08745Sheppo bcopy((uint8_t *)buf, ldcmsg, size); 37781ae08745Sheppo 37791ae08745Sheppo /* increment tail */ 37801ae08745Sheppo tx_tail = new_tail; 37811ae08745Sheppo 37821ae08745Sheppo /* 37831ae08745Sheppo * All packets have been copied into the TX queue 37841ae08745Sheppo * update the tail ptr in the HV 37851ae08745Sheppo */ 37861ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 37871ae08745Sheppo if (rv) { 37881ae08745Sheppo if (rv == EWOULDBLOCK) { 37891ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write timed out\n", 37901ae08745Sheppo ldcp->id); 37911ae08745Sheppo *sizep = 0; 37921ae08745Sheppo return (EWOULDBLOCK); 37931ae08745Sheppo } 37941ae08745Sheppo 37951ae08745Sheppo *sizep = 0; 3796d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 3797*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3798d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3799d10e4ef2Snarayan } else { 3800d10e4ef2Snarayan /* 3801d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3802d10e4ef2Snarayan * and Tx lock in correct order 3803d10e4ef2Snarayan */ 3804d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3805d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3806d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3807*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3808d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3809d10e4ef2Snarayan } 38101ae08745Sheppo return (ECONNRESET); 38111ae08745Sheppo } 38121ae08745Sheppo 38131ae08745Sheppo ldcp->tx_tail = tx_tail; 38141ae08745Sheppo *sizep = size; 38151ae08745Sheppo 38161ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, size); 38171ae08745Sheppo 38181ae08745Sheppo return (rv); 38191ae08745Sheppo } 38201ae08745Sheppo 38211ae08745Sheppo 38221ae08745Sheppo /* 38231ae08745Sheppo * Write specified amount of bytes to the channel 38241ae08745Sheppo * in multiple pkts of pkt_payload size. Each 38251ae08745Sheppo * packet is tagged with an unique packet ID in 3826e1ebb9ecSlm66018 * the case of a reliable link. 38271ae08745Sheppo * 38281ae08745Sheppo * On return, size contains the number of bytes written. 38291ae08745Sheppo * This function needs to ensure that the write size is < MTU size 38301ae08745Sheppo */ 38311ae08745Sheppo static int 38321ae08745Sheppo i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t buf, size_t *size) 38331ae08745Sheppo { 38341ae08745Sheppo ldc_msg_t *ldcmsg; 38351ae08745Sheppo uint64_t tx_head, tx_tail, new_tail, start; 38361ae08745Sheppo uint64_t txq_size_mask, numavail; 38371ae08745Sheppo uint8_t *msgbuf, *source = (uint8_t *)buf; 38381ae08745Sheppo size_t len, bytes_written = 0, remaining; 38391ae08745Sheppo int rv; 38401ae08745Sheppo uint32_t curr_seqid; 38411ae08745Sheppo 3842d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 38431ae08745Sheppo 38441ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RELIABLE || 38451ae08745Sheppo ldcp->mode == LDC_MODE_UNRELIABLE || 38461ae08745Sheppo ldcp->mode == LDC_MODE_STREAM); 38471ae08745Sheppo 38481ae08745Sheppo /* compute mask for increment */ 38491ae08745Sheppo txq_size_mask = (ldcp->tx_q_entries - 1) << LDC_PACKET_SHIFT; 38501ae08745Sheppo 38511ae08745Sheppo /* get the qptrs for the tx queue */ 38521ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 38531ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 38541ae08745Sheppo if (rv != 0) { 38551ae08745Sheppo cmn_err(CE_WARN, 38561ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 38571ae08745Sheppo *size = 0; 38581ae08745Sheppo return (EIO); 38591ae08745Sheppo } 38601ae08745Sheppo 38611ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 38621ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 38631ae08745Sheppo DWARN(ldcp->id, 38641ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 38651ae08745Sheppo *size = 0; 3866d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 3867*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3868d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3869d10e4ef2Snarayan } else { 3870d10e4ef2Snarayan /* 3871d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3872d10e4ef2Snarayan * and Tx lock in correct order 3873d10e4ef2Snarayan */ 3874d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3875d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3876d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3877*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3878d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3879d10e4ef2Snarayan } 38801ae08745Sheppo return (ECONNRESET); 38811ae08745Sheppo } 38821ae08745Sheppo 38831ae08745Sheppo tx_tail = ldcp->tx_tail; 38841ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) % 38851ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 38861ae08745Sheppo 38871ae08745Sheppo /* 3888e1ebb9ecSlm66018 * Link mode determines whether we use HV Tx head or the 38891ae08745Sheppo * private protocol head (corresponding to last ACKd pkt) for 38901ae08745Sheppo * determining how much we can write 38911ae08745Sheppo */ 38921ae08745Sheppo tx_head = (ldcp->mode == LDC_MODE_RELIABLE || 38931ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 38941ae08745Sheppo ? ldcp->tx_ackd_head : ldcp->tx_head; 38951ae08745Sheppo if (new_tail == tx_head) { 38961ae08745Sheppo DWARN(DBG_ALL_LDCS, 38971ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 38981ae08745Sheppo *size = 0; 38991ae08745Sheppo return (EWOULDBLOCK); 39001ae08745Sheppo } 39011ae08745Sheppo 39021ae08745Sheppo /* 39031ae08745Sheppo * Make sure that the LDC Tx queue has enough space 39041ae08745Sheppo */ 39051ae08745Sheppo numavail = (tx_head >> LDC_PACKET_SHIFT) - (tx_tail >> LDC_PACKET_SHIFT) 39061ae08745Sheppo + ldcp->tx_q_entries - 1; 39071ae08745Sheppo numavail %= ldcp->tx_q_entries; 39081ae08745Sheppo 39091ae08745Sheppo if (*size > (numavail * ldcp->pkt_payload)) { 39101ae08745Sheppo DWARN(DBG_ALL_LDCS, 39111ae08745Sheppo "ldc_write: (0x%llx) TX queue has no space\n", ldcp->id); 39121ae08745Sheppo return (EWOULDBLOCK); 39131ae08745Sheppo } 39141ae08745Sheppo 39151ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 39161ae08745Sheppo ldcp->id, *size); 39171ae08745Sheppo 39181ae08745Sheppo /* Send the data now */ 39191ae08745Sheppo bytes_written = 0; 39201ae08745Sheppo curr_seqid = ldcp->last_msg_snt; 39211ae08745Sheppo start = tx_tail; 39221ae08745Sheppo 39231ae08745Sheppo while (*size > bytes_written) { 39241ae08745Sheppo 39251ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 39261ae08745Sheppo 39271ae08745Sheppo msgbuf = (uint8_t *)((ldcp->mode == LDC_MODE_RELIABLE || 39281ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 39291ae08745Sheppo ? ldcmsg->rdata : ldcmsg->udata); 39301ae08745Sheppo 39311ae08745Sheppo ldcmsg->type = LDC_DATA; 39321ae08745Sheppo ldcmsg->stype = LDC_INFO; 39331ae08745Sheppo ldcmsg->ctrl = 0; 39341ae08745Sheppo 39351ae08745Sheppo remaining = *size - bytes_written; 39361ae08745Sheppo len = min(ldcp->pkt_payload, remaining); 39371ae08745Sheppo ldcmsg->env = (uint8_t)len; 39381ae08745Sheppo 39391ae08745Sheppo curr_seqid++; 39401ae08745Sheppo ldcmsg->seqid = curr_seqid; 39411ae08745Sheppo 39421ae08745Sheppo /* copy the data into pkt */ 39431ae08745Sheppo bcopy(source, msgbuf, len); 39441ae08745Sheppo 39451ae08745Sheppo source += len; 39461ae08745Sheppo bytes_written += len; 39471ae08745Sheppo 39481ae08745Sheppo /* increment tail */ 39491ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) & txq_size_mask; 39501ae08745Sheppo 39511ae08745Sheppo ASSERT(tx_tail != tx_head); 39521ae08745Sheppo } 39531ae08745Sheppo 39541ae08745Sheppo /* Set the start and stop bits */ 39551ae08745Sheppo ldcmsg->env |= LDC_FRAG_STOP; 39561ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + start); 39571ae08745Sheppo ldcmsg->env |= LDC_FRAG_START; 39581ae08745Sheppo 39591ae08745Sheppo /* 39601ae08745Sheppo * All packets have been copied into the TX queue 39611ae08745Sheppo * update the tail ptr in the HV 39621ae08745Sheppo */ 39631ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 39641ae08745Sheppo if (rv == 0) { 39651ae08745Sheppo ldcp->tx_tail = tx_tail; 39661ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 39671ae08745Sheppo *size = bytes_written; 39681ae08745Sheppo } else { 39691ae08745Sheppo int rv2; 39701ae08745Sheppo 39711ae08745Sheppo if (rv != EWOULDBLOCK) { 39721ae08745Sheppo *size = 0; 3973d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 3974*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3975d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3976d10e4ef2Snarayan } else { 3977d10e4ef2Snarayan /* 3978d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3979d10e4ef2Snarayan * and Tx lock in correct order 3980d10e4ef2Snarayan */ 3981d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3982d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3983d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3984*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3985d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3986d10e4ef2Snarayan } 39871ae08745Sheppo return (ECONNRESET); 39881ae08745Sheppo } 39891ae08745Sheppo 39901ae08745Sheppo DWARN(ldcp->id, "hv_tx_set_tail returns 0x%x (head 0x%x, " 39911ae08745Sheppo "old tail 0x%x, new tail 0x%x, qsize=0x%x)\n", 39921ae08745Sheppo rv, ldcp->tx_head, ldcp->tx_tail, tx_tail, 39931ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 39941ae08745Sheppo 39951ae08745Sheppo rv2 = hv_ldc_tx_get_state(ldcp->id, 39961ae08745Sheppo &tx_head, &tx_tail, &ldcp->link_state); 39971ae08745Sheppo 39981ae08745Sheppo DWARN(ldcp->id, "hv_ldc_tx_get_state returns 0x%x " 39991ae08745Sheppo "(head 0x%x, tail 0x%x state 0x%x)\n", 40001ae08745Sheppo rv2, tx_head, tx_tail, ldcp->link_state); 40011ae08745Sheppo 40021ae08745Sheppo *size = 0; 40031ae08745Sheppo } 40041ae08745Sheppo 40051ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, *size); 40061ae08745Sheppo 40071ae08745Sheppo return (rv); 40081ae08745Sheppo } 40091ae08745Sheppo 40101ae08745Sheppo /* 40111ae08745Sheppo * Write specified amount of bytes to the channel 40121ae08745Sheppo * in multiple pkts of pkt_payload size. Each 40131ae08745Sheppo * packet is tagged with an unique packet ID in 4014e1ebb9ecSlm66018 * the case of a reliable link. 40151ae08745Sheppo * 40161ae08745Sheppo * On return, size contains the number of bytes written. 40171ae08745Sheppo * This function needs to ensure that the write size is < MTU size 40181ae08745Sheppo */ 40191ae08745Sheppo static int 40201ae08745Sheppo i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 40211ae08745Sheppo { 4022d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 40231ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_STREAM); 40241ae08745Sheppo 40251ae08745Sheppo /* Truncate packet to max of MTU size */ 40261ae08745Sheppo if (*sizep > ldcp->mtu) *sizep = ldcp->mtu; 40271ae08745Sheppo return (i_ldc_write_packet(ldcp, buf, sizep)); 40281ae08745Sheppo } 40291ae08745Sheppo 40301ae08745Sheppo 40311ae08745Sheppo /* 40321ae08745Sheppo * Interfaces for channel nexus to register/unregister with LDC module 40331ae08745Sheppo * The nexus will register functions to be used to register individual 40341ae08745Sheppo * channels with the nexus and enable interrupts for the channels 40351ae08745Sheppo */ 40361ae08745Sheppo int 40371ae08745Sheppo ldc_register(ldc_cnex_t *cinfo) 40381ae08745Sheppo { 40391ae08745Sheppo ldc_chan_t *ldcp; 40401ae08745Sheppo 40411ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL || 40421ae08745Sheppo cinfo->reg_chan == NULL || cinfo->unreg_chan == NULL || 40431ae08745Sheppo cinfo->add_intr == NULL || cinfo->rem_intr == NULL || 40441ae08745Sheppo cinfo->clr_intr == NULL) { 40451ae08745Sheppo 40461ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_register: invalid nexus info\n"); 40471ae08745Sheppo return (EINVAL); 40481ae08745Sheppo } 40491ae08745Sheppo 40501ae08745Sheppo mutex_enter(&ldcssp->lock); 40511ae08745Sheppo 40521ae08745Sheppo /* nexus registration */ 40531ae08745Sheppo ldcssp->cinfo.dip = cinfo->dip; 40541ae08745Sheppo ldcssp->cinfo.reg_chan = cinfo->reg_chan; 40551ae08745Sheppo ldcssp->cinfo.unreg_chan = cinfo->unreg_chan; 40561ae08745Sheppo ldcssp->cinfo.add_intr = cinfo->add_intr; 40571ae08745Sheppo ldcssp->cinfo.rem_intr = cinfo->rem_intr; 40581ae08745Sheppo ldcssp->cinfo.clr_intr = cinfo->clr_intr; 40591ae08745Sheppo 40601ae08745Sheppo /* register any channels that might have been previously initialized */ 40611ae08745Sheppo ldcp = ldcssp->chan_list; 40621ae08745Sheppo while (ldcp) { 40631ae08745Sheppo if ((ldcp->tstate & TS_QCONF_RDY) && 40641ae08745Sheppo (ldcp->tstate & TS_CNEX_RDY) == 0) 40651ae08745Sheppo (void) i_ldc_register_channel(ldcp); 40661ae08745Sheppo 40671ae08745Sheppo ldcp = ldcp->next; 40681ae08745Sheppo } 40691ae08745Sheppo 40701ae08745Sheppo mutex_exit(&ldcssp->lock); 40711ae08745Sheppo 40721ae08745Sheppo return (0); 40731ae08745Sheppo } 40741ae08745Sheppo 40751ae08745Sheppo int 40761ae08745Sheppo ldc_unregister(ldc_cnex_t *cinfo) 40771ae08745Sheppo { 40781ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL) { 40791ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid nexus info\n"); 40801ae08745Sheppo return (EINVAL); 40811ae08745Sheppo } 40821ae08745Sheppo 40831ae08745Sheppo mutex_enter(&ldcssp->lock); 40841ae08745Sheppo 40851ae08745Sheppo if (cinfo->dip != ldcssp->cinfo.dip) { 40861ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid dip\n"); 40871ae08745Sheppo mutex_exit(&ldcssp->lock); 40881ae08745Sheppo return (EINVAL); 40891ae08745Sheppo } 40901ae08745Sheppo 40911ae08745Sheppo /* nexus unregister */ 40921ae08745Sheppo ldcssp->cinfo.dip = NULL; 40931ae08745Sheppo ldcssp->cinfo.reg_chan = NULL; 40941ae08745Sheppo ldcssp->cinfo.unreg_chan = NULL; 40951ae08745Sheppo ldcssp->cinfo.add_intr = NULL; 40961ae08745Sheppo ldcssp->cinfo.rem_intr = NULL; 40971ae08745Sheppo ldcssp->cinfo.clr_intr = NULL; 40981ae08745Sheppo 40991ae08745Sheppo mutex_exit(&ldcssp->lock); 41001ae08745Sheppo 41011ae08745Sheppo return (0); 41021ae08745Sheppo } 41031ae08745Sheppo 41041ae08745Sheppo 41051ae08745Sheppo /* ------------------------------------------------------------------------- */ 41061ae08745Sheppo 41071ae08745Sheppo /* 41081ae08745Sheppo * Allocate a memory handle for the channel and link it into the list 41091ae08745Sheppo * Also choose which memory table to use if this is the first handle 41101ae08745Sheppo * being assigned to this channel 41111ae08745Sheppo */ 41121ae08745Sheppo int 41131ae08745Sheppo ldc_mem_alloc_handle(ldc_handle_t handle, ldc_mem_handle_t *mhandle) 41141ae08745Sheppo { 41151ae08745Sheppo ldc_chan_t *ldcp; 41161ae08745Sheppo ldc_mhdl_t *mhdl; 41171ae08745Sheppo 41181ae08745Sheppo if (handle == NULL) { 41191ae08745Sheppo DWARN(DBG_ALL_LDCS, 41201ae08745Sheppo "ldc_mem_alloc_handle: invalid channel handle\n"); 41211ae08745Sheppo return (EINVAL); 41221ae08745Sheppo } 41231ae08745Sheppo ldcp = (ldc_chan_t *)handle; 41241ae08745Sheppo 41251ae08745Sheppo mutex_enter(&ldcp->lock); 41261ae08745Sheppo 41271ae08745Sheppo /* check to see if channel is initalized */ 4128*3af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) < TS_INIT) { 41291ae08745Sheppo DWARN(ldcp->id, 41301ae08745Sheppo "ldc_mem_alloc_handle: (0x%llx) channel not initialized\n", 41311ae08745Sheppo ldcp->id); 41321ae08745Sheppo mutex_exit(&ldcp->lock); 41331ae08745Sheppo return (EINVAL); 41341ae08745Sheppo } 41351ae08745Sheppo 41361ae08745Sheppo /* allocate handle for channel */ 41374bac2208Snarayan mhdl = kmem_cache_alloc(ldcssp->memhdl_cache, KM_SLEEP); 41381ae08745Sheppo 41391ae08745Sheppo /* initialize the lock */ 41401ae08745Sheppo mutex_init(&mhdl->lock, NULL, MUTEX_DRIVER, NULL); 41411ae08745Sheppo 41424bac2208Snarayan mhdl->myshadow = B_FALSE; 41434bac2208Snarayan mhdl->memseg = NULL; 41441ae08745Sheppo mhdl->ldcp = ldcp; 41454bac2208Snarayan mhdl->status = LDC_UNBOUND; 41461ae08745Sheppo 41471ae08745Sheppo /* insert memory handle (@ head) into list */ 41481ae08745Sheppo if (ldcp->mhdl_list == NULL) { 41491ae08745Sheppo ldcp->mhdl_list = mhdl; 41501ae08745Sheppo mhdl->next = NULL; 41511ae08745Sheppo } else { 41521ae08745Sheppo /* insert @ head */ 41531ae08745Sheppo mhdl->next = ldcp->mhdl_list; 41541ae08745Sheppo ldcp->mhdl_list = mhdl; 41551ae08745Sheppo } 41561ae08745Sheppo 41571ae08745Sheppo /* return the handle */ 41581ae08745Sheppo *mhandle = (ldc_mem_handle_t)mhdl; 41591ae08745Sheppo 41601ae08745Sheppo mutex_exit(&ldcp->lock); 41611ae08745Sheppo 41621ae08745Sheppo D1(ldcp->id, "ldc_mem_alloc_handle: (0x%llx) allocated handle 0x%llx\n", 41631ae08745Sheppo ldcp->id, mhdl); 41641ae08745Sheppo 41651ae08745Sheppo return (0); 41661ae08745Sheppo } 41671ae08745Sheppo 41681ae08745Sheppo /* 41691ae08745Sheppo * Free memory handle for the channel and unlink it from the list 41701ae08745Sheppo */ 41711ae08745Sheppo int 41721ae08745Sheppo ldc_mem_free_handle(ldc_mem_handle_t mhandle) 41731ae08745Sheppo { 41741ae08745Sheppo ldc_mhdl_t *mhdl, *phdl; 41751ae08745Sheppo ldc_chan_t *ldcp; 41761ae08745Sheppo 41771ae08745Sheppo if (mhandle == NULL) { 41781ae08745Sheppo DWARN(DBG_ALL_LDCS, 41791ae08745Sheppo "ldc_mem_free_handle: invalid memory handle\n"); 41801ae08745Sheppo return (EINVAL); 41811ae08745Sheppo } 41821ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 41831ae08745Sheppo 41841ae08745Sheppo mutex_enter(&mhdl->lock); 41851ae08745Sheppo 41861ae08745Sheppo ldcp = mhdl->ldcp; 41871ae08745Sheppo 41881ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED) { 41891ae08745Sheppo DWARN(ldcp->id, 41901ae08745Sheppo "ldc_mem_free_handle: cannot free, 0x%llx hdl bound\n", 41911ae08745Sheppo mhdl); 41921ae08745Sheppo mutex_exit(&mhdl->lock); 41931ae08745Sheppo return (EINVAL); 41941ae08745Sheppo } 41951ae08745Sheppo mutex_exit(&mhdl->lock); 41961ae08745Sheppo 41971ae08745Sheppo mutex_enter(&ldcp->mlist_lock); 41981ae08745Sheppo 41991ae08745Sheppo phdl = ldcp->mhdl_list; 42001ae08745Sheppo 42011ae08745Sheppo /* first handle */ 42021ae08745Sheppo if (phdl == mhdl) { 42031ae08745Sheppo ldcp->mhdl_list = mhdl->next; 42041ae08745Sheppo mutex_destroy(&mhdl->lock); 42054bac2208Snarayan kmem_cache_free(ldcssp->memhdl_cache, mhdl); 42064bac2208Snarayan 42071ae08745Sheppo D1(ldcp->id, 42081ae08745Sheppo "ldc_mem_free_handle: (0x%llx) freed handle 0x%llx\n", 42091ae08745Sheppo ldcp->id, mhdl); 42101ae08745Sheppo } else { 42111ae08745Sheppo /* walk the list - unlink and free */ 42121ae08745Sheppo while (phdl != NULL) { 42131ae08745Sheppo if (phdl->next == mhdl) { 42141ae08745Sheppo phdl->next = mhdl->next; 42151ae08745Sheppo mutex_destroy(&mhdl->lock); 42164bac2208Snarayan kmem_cache_free(ldcssp->memhdl_cache, mhdl); 42171ae08745Sheppo D1(ldcp->id, 42181ae08745Sheppo "ldc_mem_free_handle: (0x%llx) freed " 42191ae08745Sheppo "handle 0x%llx\n", ldcp->id, mhdl); 42201ae08745Sheppo break; 42211ae08745Sheppo } 42221ae08745Sheppo phdl = phdl->next; 42231ae08745Sheppo } 42241ae08745Sheppo } 42251ae08745Sheppo 42261ae08745Sheppo if (phdl == NULL) { 42271ae08745Sheppo DWARN(ldcp->id, 42281ae08745Sheppo "ldc_mem_free_handle: invalid handle 0x%llx\n", mhdl); 42291ae08745Sheppo mutex_exit(&ldcp->mlist_lock); 42301ae08745Sheppo return (EINVAL); 42311ae08745Sheppo } 42321ae08745Sheppo 42331ae08745Sheppo mutex_exit(&ldcp->mlist_lock); 42341ae08745Sheppo 42351ae08745Sheppo return (0); 42361ae08745Sheppo } 42371ae08745Sheppo 42381ae08745Sheppo /* 42391ae08745Sheppo * Bind a memory handle to a virtual address. 42401ae08745Sheppo * The virtual address is converted to the corresponding real addresses. 42411ae08745Sheppo * Returns pointer to the first ldc_mem_cookie and the total number 42421ae08745Sheppo * of cookies for this virtual address. Other cookies can be obtained 42431ae08745Sheppo * using the ldc_mem_nextcookie() call. If the pages are stored in 42441ae08745Sheppo * consecutive locations in the table, a single cookie corresponding to 42451ae08745Sheppo * the first location is returned. The cookie size spans all the entries. 42461ae08745Sheppo * 42471ae08745Sheppo * If the VA corresponds to a page that is already being exported, reuse 42481ae08745Sheppo * the page and do not export it again. Bump the page's use count. 42491ae08745Sheppo */ 42501ae08745Sheppo int 42511ae08745Sheppo ldc_mem_bind_handle(ldc_mem_handle_t mhandle, caddr_t vaddr, size_t len, 42521ae08745Sheppo uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount) 42531ae08745Sheppo { 42541ae08745Sheppo ldc_mhdl_t *mhdl; 42551ae08745Sheppo ldc_chan_t *ldcp; 42561ae08745Sheppo ldc_mtbl_t *mtbl; 42571ae08745Sheppo ldc_memseg_t *memseg; 42581ae08745Sheppo ldc_mte_t tmp_mte; 42591ae08745Sheppo uint64_t index, prev_index = 0; 42601ae08745Sheppo int64_t cookie_idx; 42611ae08745Sheppo uintptr_t raddr, ra_aligned; 42621ae08745Sheppo uint64_t psize, poffset, v_offset; 42631ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code, pg_mask; 42641ae08745Sheppo pgcnt_t npages; 42651ae08745Sheppo caddr_t v_align, addr; 42664bac2208Snarayan int i, rv; 42671ae08745Sheppo 42681ae08745Sheppo if (mhandle == NULL) { 42691ae08745Sheppo DWARN(DBG_ALL_LDCS, 42701ae08745Sheppo "ldc_mem_bind_handle: invalid memory handle\n"); 42711ae08745Sheppo return (EINVAL); 42721ae08745Sheppo } 42731ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 42741ae08745Sheppo ldcp = mhdl->ldcp; 42751ae08745Sheppo 42761ae08745Sheppo /* clear count */ 42771ae08745Sheppo *ccount = 0; 42781ae08745Sheppo 42791ae08745Sheppo mutex_enter(&mhdl->lock); 42801ae08745Sheppo 42811ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->memseg != NULL) { 42821ae08745Sheppo DWARN(ldcp->id, 42831ae08745Sheppo "ldc_mem_bind_handle: (0x%x) handle already bound\n", 42841ae08745Sheppo mhandle); 42851ae08745Sheppo mutex_exit(&mhdl->lock); 42861ae08745Sheppo return (EINVAL); 42871ae08745Sheppo } 42881ae08745Sheppo 42891ae08745Sheppo /* Force address and size to be 8-byte aligned */ 42901ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 42911ae08745Sheppo DWARN(ldcp->id, 42921ae08745Sheppo "ldc_mem_bind_handle: addr/size is not 8-byte aligned\n"); 42931ae08745Sheppo mutex_exit(&mhdl->lock); 42941ae08745Sheppo return (EINVAL); 42951ae08745Sheppo } 42961ae08745Sheppo 42974bac2208Snarayan /* 42984bac2208Snarayan * If this channel is binding a memory handle for the 42994bac2208Snarayan * first time allocate it a memory map table and initialize it 43004bac2208Snarayan */ 43014bac2208Snarayan if ((mtbl = ldcp->mtbl) == NULL) { 43024bac2208Snarayan 43034bac2208Snarayan mutex_enter(&ldcp->lock); 43044bac2208Snarayan 43054bac2208Snarayan /* Allocate and initialize the map table structure */ 43064bac2208Snarayan mtbl = kmem_zalloc(sizeof (ldc_mtbl_t), KM_SLEEP); 43074bac2208Snarayan mtbl->num_entries = mtbl->num_avail = ldc_maptable_entries; 43084bac2208Snarayan mtbl->size = ldc_maptable_entries * sizeof (ldc_mte_slot_t); 43094bac2208Snarayan mtbl->next_entry = NULL; 4310*3af08d82Slm66018 mtbl->contigmem = B_TRUE; 43114bac2208Snarayan 43124bac2208Snarayan /* Allocate the table itself */ 43134bac2208Snarayan mtbl->table = (ldc_mte_slot_t *) 43144bac2208Snarayan contig_mem_alloc_align(mtbl->size, MMU_PAGESIZE); 43154bac2208Snarayan if (mtbl->table == NULL) { 4316*3af08d82Slm66018 4317*3af08d82Slm66018 /* allocate a page of memory using kmem_alloc */ 4318*3af08d82Slm66018 mtbl->table = kmem_alloc(MMU_PAGESIZE, KM_SLEEP); 4319*3af08d82Slm66018 mtbl->size = MMU_PAGESIZE; 4320*3af08d82Slm66018 mtbl->contigmem = B_FALSE; 4321*3af08d82Slm66018 mtbl->num_entries = mtbl->num_avail = 4322*3af08d82Slm66018 mtbl->size / sizeof (ldc_mte_slot_t); 4323*3af08d82Slm66018 DWARN(ldcp->id, 4324*3af08d82Slm66018 "ldc_mem_bind_handle: (0x%llx) reduced tbl size " 4325*3af08d82Slm66018 "to %lx entries\n", ldcp->id, mtbl->num_entries); 43264bac2208Snarayan } 43274bac2208Snarayan 43284bac2208Snarayan /* zero out the memory */ 43294bac2208Snarayan bzero(mtbl->table, mtbl->size); 43304bac2208Snarayan 43314bac2208Snarayan /* initialize the lock */ 43324bac2208Snarayan mutex_init(&mtbl->lock, NULL, MUTEX_DRIVER, NULL); 43334bac2208Snarayan 43344bac2208Snarayan /* register table for this channel */ 43354bac2208Snarayan rv = hv_ldc_set_map_table(ldcp->id, 43364bac2208Snarayan va_to_pa(mtbl->table), mtbl->num_entries); 43374bac2208Snarayan if (rv != 0) { 43384bac2208Snarayan cmn_err(CE_WARN, 43394bac2208Snarayan "ldc_mem_bind_handle: (0x%lx) err %d mapping tbl", 43404bac2208Snarayan ldcp->id, rv); 4341*3af08d82Slm66018 if (mtbl->contigmem) 43424bac2208Snarayan contig_mem_free(mtbl->table, mtbl->size); 4343*3af08d82Slm66018 else 4344*3af08d82Slm66018 kmem_free(mtbl->table, mtbl->size); 43454bac2208Snarayan mutex_destroy(&mtbl->lock); 43464bac2208Snarayan kmem_free(mtbl, sizeof (ldc_mtbl_t)); 43474bac2208Snarayan mutex_exit(&ldcp->lock); 43484bac2208Snarayan mutex_exit(&mhdl->lock); 43494bac2208Snarayan return (EIO); 43504bac2208Snarayan } 43514bac2208Snarayan 43524bac2208Snarayan ldcp->mtbl = mtbl; 43534bac2208Snarayan mutex_exit(&ldcp->lock); 43544bac2208Snarayan 43554bac2208Snarayan D1(ldcp->id, 43564bac2208Snarayan "ldc_mem_bind_handle: (0x%llx) alloc'd map table 0x%llx\n", 43574bac2208Snarayan ldcp->id, ldcp->mtbl->table); 43584bac2208Snarayan } 43594bac2208Snarayan 43601ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 43611ae08745Sheppo pg_size = MMU_PAGESIZE; 43621ae08745Sheppo pg_size_code = page_szc(pg_size); 43631ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 43641ae08745Sheppo pg_mask = ~(pg_size - 1); 43651ae08745Sheppo 43661ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: (0x%llx) binding " 43671ae08745Sheppo "va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 43681ae08745Sheppo ldcp->id, vaddr, pg_size, pg_size_code, pg_shift); 43691ae08745Sheppo 43701ae08745Sheppo /* aligned VA and its offset */ 43711ae08745Sheppo v_align = (caddr_t)(((uintptr_t)vaddr) & ~(pg_size - 1)); 43721ae08745Sheppo v_offset = ((uintptr_t)vaddr) & (pg_size - 1); 43731ae08745Sheppo 43741ae08745Sheppo npages = (len+v_offset)/pg_size; 43751ae08745Sheppo npages = ((len+v_offset)%pg_size == 0) ? npages : npages+1; 43761ae08745Sheppo 43771ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: binding " 43781ae08745Sheppo "(0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n", 43791ae08745Sheppo ldcp->id, vaddr, v_align, v_offset, npages); 43801ae08745Sheppo 43811ae08745Sheppo /* lock the memory table - exclusive access to channel */ 43821ae08745Sheppo mutex_enter(&mtbl->lock); 43831ae08745Sheppo 43841ae08745Sheppo if (npages > mtbl->num_avail) { 4385*3af08d82Slm66018 D1(ldcp->id, "ldc_mem_bind_handle: (0x%llx) no table entries\n", 43861ae08745Sheppo ldcp->id); 43871ae08745Sheppo mutex_exit(&mtbl->lock); 43881ae08745Sheppo mutex_exit(&mhdl->lock); 43891ae08745Sheppo return (ENOMEM); 43901ae08745Sheppo } 43911ae08745Sheppo 43921ae08745Sheppo /* Allocate a memseg structure */ 43934bac2208Snarayan memseg = mhdl->memseg = 43944bac2208Snarayan kmem_cache_alloc(ldcssp->memseg_cache, KM_SLEEP); 43951ae08745Sheppo 43961ae08745Sheppo /* Allocate memory to store all pages and cookies */ 43971ae08745Sheppo memseg->pages = kmem_zalloc((sizeof (ldc_page_t) * npages), KM_SLEEP); 43981ae08745Sheppo memseg->cookies = 43991ae08745Sheppo kmem_zalloc((sizeof (ldc_mem_cookie_t) * npages), KM_SLEEP); 44001ae08745Sheppo 44011ae08745Sheppo D2(ldcp->id, "ldc_mem_bind_handle: (0x%llx) processing 0x%llx pages\n", 44021ae08745Sheppo ldcp->id, npages); 44031ae08745Sheppo 44041ae08745Sheppo addr = v_align; 44051ae08745Sheppo 44061ae08745Sheppo /* 44074bac2208Snarayan * Check if direct shared memory map is enabled, if not change 44084bac2208Snarayan * the mapping type to include SHADOW_MAP. 44094bac2208Snarayan */ 44104bac2208Snarayan if (ldc_shmem_enabled == 0) 44114bac2208Snarayan mtype = LDC_SHADOW_MAP; 44124bac2208Snarayan 44134bac2208Snarayan /* 44141ae08745Sheppo * Table slots are used in a round-robin manner. The algorithm permits 44151ae08745Sheppo * inserting duplicate entries. Slots allocated earlier will typically 44161ae08745Sheppo * get freed before we get back to reusing the slot.Inserting duplicate 44171ae08745Sheppo * entries should be OK as we only lookup entries using the cookie addr 44181ae08745Sheppo * i.e. tbl index, during export, unexport and copy operation. 44191ae08745Sheppo * 44201ae08745Sheppo * One implementation what was tried was to search for a duplicate 44211ae08745Sheppo * page entry first and reuse it. The search overhead is very high and 44221ae08745Sheppo * in the vnet case dropped the perf by almost half, 50 to 24 mbps. 44231ae08745Sheppo * So it does make sense to avoid searching for duplicates. 44241ae08745Sheppo * 44251ae08745Sheppo * But during the process of searching for a free slot, if we find a 44261ae08745Sheppo * duplicate entry we will go ahead and use it, and bump its use count. 44271ae08745Sheppo */ 44281ae08745Sheppo 44291ae08745Sheppo /* index to start searching from */ 44301ae08745Sheppo index = mtbl->next_entry; 44311ae08745Sheppo cookie_idx = -1; 44321ae08745Sheppo 44331ae08745Sheppo tmp_mte.ll = 0; /* initialise fields to 0 */ 44341ae08745Sheppo 44351ae08745Sheppo if (mtype & LDC_DIRECT_MAP) { 44361ae08745Sheppo tmp_mte.mte_r = (perm & LDC_MEM_R) ? 1 : 0; 44371ae08745Sheppo tmp_mte.mte_w = (perm & LDC_MEM_W) ? 1 : 0; 44381ae08745Sheppo tmp_mte.mte_x = (perm & LDC_MEM_X) ? 1 : 0; 44391ae08745Sheppo } 44401ae08745Sheppo 44411ae08745Sheppo if (mtype & LDC_SHADOW_MAP) { 44421ae08745Sheppo tmp_mte.mte_cr = (perm & LDC_MEM_R) ? 1 : 0; 44431ae08745Sheppo tmp_mte.mte_cw = (perm & LDC_MEM_W) ? 1 : 0; 44441ae08745Sheppo } 44451ae08745Sheppo 44461ae08745Sheppo if (mtype & LDC_IO_MAP) { 44471ae08745Sheppo tmp_mte.mte_ir = (perm & LDC_MEM_R) ? 1 : 0; 44481ae08745Sheppo tmp_mte.mte_iw = (perm & LDC_MEM_W) ? 1 : 0; 44491ae08745Sheppo } 44501ae08745Sheppo 44511ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle mte=0x%llx\n", tmp_mte.ll); 44521ae08745Sheppo 44531ae08745Sheppo tmp_mte.mte_pgszc = pg_size_code; 44541ae08745Sheppo 44551ae08745Sheppo /* initialize each mem table entry */ 44561ae08745Sheppo for (i = 0; i < npages; i++) { 44571ae08745Sheppo 44581ae08745Sheppo /* check if slot is available in the table */ 44591ae08745Sheppo while (mtbl->table[index].entry.ll != 0) { 44601ae08745Sheppo 44611ae08745Sheppo index = (index + 1) % mtbl->num_entries; 44621ae08745Sheppo 44631ae08745Sheppo if (index == mtbl->next_entry) { 44641ae08745Sheppo /* we have looped around */ 44651ae08745Sheppo DWARN(DBG_ALL_LDCS, 44661ae08745Sheppo "ldc_mem_bind_handle: (0x%llx) cannot find " 44671ae08745Sheppo "entry\n", ldcp->id); 44681ae08745Sheppo *ccount = 0; 44691ae08745Sheppo 44701ae08745Sheppo /* NOTE: free memory, remove previous entries */ 44711ae08745Sheppo /* this shouldnt happen as num_avail was ok */ 44721ae08745Sheppo 44731ae08745Sheppo mutex_exit(&mtbl->lock); 44741ae08745Sheppo mutex_exit(&mhdl->lock); 44751ae08745Sheppo return (ENOMEM); 44761ae08745Sheppo } 44771ae08745Sheppo } 44781ae08745Sheppo 44791ae08745Sheppo /* get the real address */ 44801ae08745Sheppo raddr = va_to_pa((void *)addr); 44811ae08745Sheppo ra_aligned = ((uintptr_t)raddr & pg_mask); 44821ae08745Sheppo 44831ae08745Sheppo /* build the mte */ 44841ae08745Sheppo tmp_mte.mte_rpfn = ra_aligned >> pg_shift; 44851ae08745Sheppo 44861ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle mte=0x%llx\n", tmp_mte.ll); 44871ae08745Sheppo 44881ae08745Sheppo /* update entry in table */ 44891ae08745Sheppo mtbl->table[index].entry = tmp_mte; 44901ae08745Sheppo 44911ae08745Sheppo D2(ldcp->id, "ldc_mem_bind_handle: (0x%llx) stored MTE 0x%llx" 44921ae08745Sheppo " into loc 0x%llx\n", ldcp->id, tmp_mte.ll, index); 44931ae08745Sheppo 44941ae08745Sheppo /* calculate the size and offset for this export range */ 44951ae08745Sheppo if (i == 0) { 44961ae08745Sheppo /* first page */ 44971ae08745Sheppo psize = min((pg_size - v_offset), len); 44981ae08745Sheppo poffset = v_offset; 44991ae08745Sheppo 45001ae08745Sheppo } else if (i == (npages - 1)) { 45011ae08745Sheppo /* last page */ 45021ae08745Sheppo psize = (((uintptr_t)(vaddr + len)) & 45031ae08745Sheppo ((uint64_t)(pg_size-1))); 45041ae08745Sheppo if (psize == 0) 45051ae08745Sheppo psize = pg_size; 45061ae08745Sheppo poffset = 0; 45071ae08745Sheppo 45081ae08745Sheppo } else { 45091ae08745Sheppo /* middle pages */ 45101ae08745Sheppo psize = pg_size; 45111ae08745Sheppo poffset = 0; 45121ae08745Sheppo } 45131ae08745Sheppo 45141ae08745Sheppo /* store entry for this page */ 45151ae08745Sheppo memseg->pages[i].index = index; 45161ae08745Sheppo memseg->pages[i].raddr = raddr; 45171ae08745Sheppo memseg->pages[i].offset = poffset; 45181ae08745Sheppo memseg->pages[i].size = psize; 45191ae08745Sheppo memseg->pages[i].mte = &(mtbl->table[index]); 45201ae08745Sheppo 45211ae08745Sheppo /* create the cookie */ 45221ae08745Sheppo if (i == 0 || (index != prev_index + 1)) { 45231ae08745Sheppo cookie_idx++; 45241ae08745Sheppo memseg->cookies[cookie_idx].addr = 45251ae08745Sheppo IDX2COOKIE(index, pg_size_code, pg_shift); 45261ae08745Sheppo memseg->cookies[cookie_idx].addr |= poffset; 45271ae08745Sheppo memseg->cookies[cookie_idx].size = psize; 45281ae08745Sheppo 45291ae08745Sheppo } else { 45301ae08745Sheppo memseg->cookies[cookie_idx].size += psize; 45311ae08745Sheppo } 45321ae08745Sheppo 45331ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: bound " 45341ae08745Sheppo "(0x%llx) va=0x%llx, idx=0x%llx, " 45351ae08745Sheppo "ra=0x%llx(sz=0x%x,off=0x%x)\n", 45361ae08745Sheppo ldcp->id, addr, index, raddr, psize, poffset); 45371ae08745Sheppo 45381ae08745Sheppo /* decrement number of available entries */ 45391ae08745Sheppo mtbl->num_avail--; 45401ae08745Sheppo 45411ae08745Sheppo /* increment va by page size */ 45421ae08745Sheppo addr += pg_size; 45431ae08745Sheppo 45441ae08745Sheppo /* increment index */ 45451ae08745Sheppo prev_index = index; 45461ae08745Sheppo index = (index + 1) % mtbl->num_entries; 45471ae08745Sheppo 45481ae08745Sheppo /* save the next slot */ 45491ae08745Sheppo mtbl->next_entry = index; 45501ae08745Sheppo } 45511ae08745Sheppo 45521ae08745Sheppo mutex_exit(&mtbl->lock); 45531ae08745Sheppo 45541ae08745Sheppo /* memory handle = bound */ 45551ae08745Sheppo mhdl->mtype = mtype; 45561ae08745Sheppo mhdl->perm = perm; 45571ae08745Sheppo mhdl->status = LDC_BOUND; 45581ae08745Sheppo 45591ae08745Sheppo /* update memseg_t */ 45601ae08745Sheppo memseg->vaddr = vaddr; 45611ae08745Sheppo memseg->raddr = memseg->pages[0].raddr; 45621ae08745Sheppo memseg->size = len; 45631ae08745Sheppo memseg->npages = npages; 45641ae08745Sheppo memseg->ncookies = cookie_idx + 1; 45651ae08745Sheppo memseg->next_cookie = (memseg->ncookies > 1) ? 1 : 0; 45661ae08745Sheppo 45671ae08745Sheppo /* return count and first cookie */ 45681ae08745Sheppo *ccount = memseg->ncookies; 45691ae08745Sheppo cookie->addr = memseg->cookies[0].addr; 45701ae08745Sheppo cookie->size = memseg->cookies[0].size; 45711ae08745Sheppo 45721ae08745Sheppo D1(ldcp->id, 45731ae08745Sheppo "ldc_mem_bind_handle: (0x%llx) bound 0x%llx, va=0x%llx, " 45741ae08745Sheppo "pgs=0x%llx cookies=0x%llx\n", 45751ae08745Sheppo ldcp->id, mhdl, vaddr, npages, memseg->ncookies); 45761ae08745Sheppo 45771ae08745Sheppo mutex_exit(&mhdl->lock); 45781ae08745Sheppo return (0); 45791ae08745Sheppo } 45801ae08745Sheppo 45811ae08745Sheppo /* 45821ae08745Sheppo * Return the next cookie associated with the specified memory handle 45831ae08745Sheppo */ 45841ae08745Sheppo int 45851ae08745Sheppo ldc_mem_nextcookie(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie) 45861ae08745Sheppo { 45871ae08745Sheppo ldc_mhdl_t *mhdl; 45881ae08745Sheppo ldc_chan_t *ldcp; 45891ae08745Sheppo ldc_memseg_t *memseg; 45901ae08745Sheppo 45911ae08745Sheppo if (mhandle == NULL) { 45921ae08745Sheppo DWARN(DBG_ALL_LDCS, 45931ae08745Sheppo "ldc_mem_nextcookie: invalid memory handle\n"); 45941ae08745Sheppo return (EINVAL); 45951ae08745Sheppo } 45961ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 45971ae08745Sheppo 45981ae08745Sheppo mutex_enter(&mhdl->lock); 45991ae08745Sheppo 46001ae08745Sheppo ldcp = mhdl->ldcp; 46011ae08745Sheppo memseg = mhdl->memseg; 46021ae08745Sheppo 46031ae08745Sheppo if (cookie == 0) { 46041ae08745Sheppo DWARN(ldcp->id, 46051ae08745Sheppo "ldc_mem_nextcookie:(0x%llx) invalid cookie arg\n", 46061ae08745Sheppo ldcp->id); 46071ae08745Sheppo mutex_exit(&mhdl->lock); 46081ae08745Sheppo return (EINVAL); 46091ae08745Sheppo } 46101ae08745Sheppo 46111ae08745Sheppo if (memseg->next_cookie != 0) { 46121ae08745Sheppo cookie->addr = memseg->cookies[memseg->next_cookie].addr; 46131ae08745Sheppo cookie->size = memseg->cookies[memseg->next_cookie].size; 46141ae08745Sheppo memseg->next_cookie++; 46151ae08745Sheppo if (memseg->next_cookie == memseg->ncookies) 46161ae08745Sheppo memseg->next_cookie = 0; 46171ae08745Sheppo 46181ae08745Sheppo } else { 46191ae08745Sheppo DWARN(ldcp->id, 46201ae08745Sheppo "ldc_mem_nextcookie:(0x%llx) no more cookies\n", ldcp->id); 46211ae08745Sheppo cookie->addr = 0; 46221ae08745Sheppo cookie->size = 0; 46231ae08745Sheppo mutex_exit(&mhdl->lock); 46241ae08745Sheppo return (EINVAL); 46251ae08745Sheppo } 46261ae08745Sheppo 46271ae08745Sheppo D1(ldcp->id, 46281ae08745Sheppo "ldc_mem_nextcookie: (0x%llx) cookie addr=0x%llx,sz=0x%llx\n", 46291ae08745Sheppo ldcp->id, cookie->addr, cookie->size); 46301ae08745Sheppo 46311ae08745Sheppo mutex_exit(&mhdl->lock); 46321ae08745Sheppo return (0); 46331ae08745Sheppo } 46341ae08745Sheppo 46351ae08745Sheppo /* 46361ae08745Sheppo * Unbind the virtual memory region associated with the specified 46371ae08745Sheppo * memory handle. Allassociated cookies are freed and the corresponding 46381ae08745Sheppo * RA space is no longer exported. 46391ae08745Sheppo */ 46401ae08745Sheppo int 46411ae08745Sheppo ldc_mem_unbind_handle(ldc_mem_handle_t mhandle) 46421ae08745Sheppo { 46431ae08745Sheppo ldc_mhdl_t *mhdl; 46441ae08745Sheppo ldc_chan_t *ldcp; 46451ae08745Sheppo ldc_mtbl_t *mtbl; 46461ae08745Sheppo ldc_memseg_t *memseg; 46474bac2208Snarayan uint64_t cookie_addr; 46484bac2208Snarayan uint64_t pg_shift, pg_size_code; 46494bac2208Snarayan int i, rv; 46501ae08745Sheppo 46511ae08745Sheppo if (mhandle == NULL) { 46521ae08745Sheppo DWARN(DBG_ALL_LDCS, 46531ae08745Sheppo "ldc_mem_unbind_handle: invalid memory handle\n"); 46541ae08745Sheppo return (EINVAL); 46551ae08745Sheppo } 46561ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 46571ae08745Sheppo 46581ae08745Sheppo mutex_enter(&mhdl->lock); 46591ae08745Sheppo 46601ae08745Sheppo if (mhdl->status == LDC_UNBOUND) { 46611ae08745Sheppo DWARN(DBG_ALL_LDCS, 46621ae08745Sheppo "ldc_mem_unbind_handle: (0x%x) handle is not bound\n", 46631ae08745Sheppo mhandle); 46641ae08745Sheppo mutex_exit(&mhdl->lock); 46651ae08745Sheppo return (EINVAL); 46661ae08745Sheppo } 46671ae08745Sheppo 46681ae08745Sheppo ldcp = mhdl->ldcp; 46691ae08745Sheppo mtbl = ldcp->mtbl; 46701ae08745Sheppo 46711ae08745Sheppo memseg = mhdl->memseg; 46721ae08745Sheppo 46731ae08745Sheppo /* lock the memory table - exclusive access to channel */ 46741ae08745Sheppo mutex_enter(&mtbl->lock); 46751ae08745Sheppo 46761ae08745Sheppo /* undo the pages exported */ 46771ae08745Sheppo for (i = 0; i < memseg->npages; i++) { 46781ae08745Sheppo 46794bac2208Snarayan /* check for mapped pages, revocation cookie != 0 */ 46801ae08745Sheppo if (memseg->pages[i].mte->cookie) { 46814bac2208Snarayan 46824bac2208Snarayan pg_size_code = page_szc(memseg->pages[i].size); 46834bac2208Snarayan pg_shift = page_get_shift(memseg->pages[i].size); 46844bac2208Snarayan cookie_addr = IDX2COOKIE(memseg->pages[i].index, 46854bac2208Snarayan pg_size_code, pg_shift); 46864bac2208Snarayan 46874bac2208Snarayan D1(ldcp->id, "ldc_mem_unbind_handle: (0x%llx) revoke " 46884bac2208Snarayan "cookie 0x%llx, rcookie 0x%llx\n", ldcp->id, 46894bac2208Snarayan cookie_addr, memseg->pages[i].mte->cookie); 46904bac2208Snarayan rv = hv_ldc_revoke(ldcp->id, cookie_addr, 46914bac2208Snarayan memseg->pages[i].mte->cookie); 46924bac2208Snarayan if (rv) { 46934bac2208Snarayan DWARN(ldcp->id, 46944bac2208Snarayan "ldc_mem_unbind_handle: (0x%llx) cannot " 46954bac2208Snarayan "revoke mapping, cookie %llx\n", ldcp->id, 46964bac2208Snarayan cookie_addr); 46974bac2208Snarayan } 46981ae08745Sheppo } 46991ae08745Sheppo 47001ae08745Sheppo /* clear the entry from the table */ 47011ae08745Sheppo memseg->pages[i].mte->entry.ll = 0; 47021ae08745Sheppo mtbl->num_avail++; 47031ae08745Sheppo } 47041ae08745Sheppo mutex_exit(&mtbl->lock); 47051ae08745Sheppo 47061ae08745Sheppo /* free the allocated memseg and page structures */ 47071ae08745Sheppo kmem_free(memseg->pages, (sizeof (ldc_page_t) * memseg->npages)); 47081ae08745Sheppo kmem_free(memseg->cookies, 47091ae08745Sheppo (sizeof (ldc_mem_cookie_t) * memseg->npages)); 47104bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 47111ae08745Sheppo 47121ae08745Sheppo /* uninitialize the memory handle */ 47131ae08745Sheppo mhdl->memseg = NULL; 47141ae08745Sheppo mhdl->status = LDC_UNBOUND; 47151ae08745Sheppo 47161ae08745Sheppo D1(ldcp->id, "ldc_mem_unbind_handle: (0x%llx) unbound handle 0x%llx\n", 47171ae08745Sheppo ldcp->id, mhdl); 47181ae08745Sheppo 47191ae08745Sheppo mutex_exit(&mhdl->lock); 47201ae08745Sheppo return (0); 47211ae08745Sheppo } 47221ae08745Sheppo 47231ae08745Sheppo /* 47241ae08745Sheppo * Get information about the dring. The base address of the descriptor 47251ae08745Sheppo * ring along with the type and permission are returned back. 47261ae08745Sheppo */ 47271ae08745Sheppo int 47281ae08745Sheppo ldc_mem_info(ldc_mem_handle_t mhandle, ldc_mem_info_t *minfo) 47291ae08745Sheppo { 47301ae08745Sheppo ldc_mhdl_t *mhdl; 47311ae08745Sheppo 47321ae08745Sheppo if (mhandle == NULL) { 47331ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_info: invalid memory handle\n"); 47341ae08745Sheppo return (EINVAL); 47351ae08745Sheppo } 47361ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 47371ae08745Sheppo 47381ae08745Sheppo if (minfo == NULL) { 47391ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_info: invalid args\n"); 47401ae08745Sheppo return (EINVAL); 47411ae08745Sheppo } 47421ae08745Sheppo 47431ae08745Sheppo mutex_enter(&mhdl->lock); 47441ae08745Sheppo 47451ae08745Sheppo minfo->status = mhdl->status; 47461ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED) { 47471ae08745Sheppo minfo->vaddr = mhdl->memseg->vaddr; 47481ae08745Sheppo minfo->raddr = mhdl->memseg->raddr; 47491ae08745Sheppo minfo->mtype = mhdl->mtype; 47501ae08745Sheppo minfo->perm = mhdl->perm; 47511ae08745Sheppo } 47521ae08745Sheppo mutex_exit(&mhdl->lock); 47531ae08745Sheppo 47541ae08745Sheppo return (0); 47551ae08745Sheppo } 47561ae08745Sheppo 47571ae08745Sheppo /* 47581ae08745Sheppo * Copy data either from or to the client specified virtual address 47591ae08745Sheppo * space to or from the exported memory associated with the cookies. 47601ae08745Sheppo * The direction argument determines whether the data is read from or 47611ae08745Sheppo * written to exported memory. 47621ae08745Sheppo */ 47631ae08745Sheppo int 47641ae08745Sheppo ldc_mem_copy(ldc_handle_t handle, caddr_t vaddr, uint64_t off, size_t *size, 47651ae08745Sheppo ldc_mem_cookie_t *cookies, uint32_t ccount, uint8_t direction) 47661ae08745Sheppo { 47671ae08745Sheppo ldc_chan_t *ldcp; 47681ae08745Sheppo uint64_t local_voff, local_valign; 47691ae08745Sheppo uint64_t cookie_addr, cookie_size; 47701ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code; 47711ae08745Sheppo uint64_t export_caddr, export_poff, export_psize, export_size; 47721ae08745Sheppo uint64_t local_ra, local_poff, local_psize; 47731ae08745Sheppo uint64_t copy_size, copied_len = 0, total_bal = 0, idx = 0; 47741ae08745Sheppo pgcnt_t npages; 47751ae08745Sheppo size_t len = *size; 47761ae08745Sheppo int i, rv = 0; 47771ae08745Sheppo 4778*3af08d82Slm66018 uint64_t chid; 4779*3af08d82Slm66018 47801ae08745Sheppo if (handle == NULL) { 47811ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_copy: invalid channel handle\n"); 47821ae08745Sheppo return (EINVAL); 47831ae08745Sheppo } 47841ae08745Sheppo ldcp = (ldc_chan_t *)handle; 4785*3af08d82Slm66018 chid = ldcp->id; 47861ae08745Sheppo 47871ae08745Sheppo /* check to see if channel is UP */ 47881ae08745Sheppo if (ldcp->tstate != TS_UP) { 4789*3af08d82Slm66018 DWARN(chid, "ldc_mem_copy: (0x%llx) channel is not UP\n", 4790*3af08d82Slm66018 chid); 4791*3af08d82Slm66018 return (ECONNRESET); 47921ae08745Sheppo } 47931ae08745Sheppo 47941ae08745Sheppo /* Force address and size to be 8-byte aligned */ 47951ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 4796*3af08d82Slm66018 DWARN(chid, 47971ae08745Sheppo "ldc_mem_copy: addr/sz is not 8-byte aligned\n"); 47981ae08745Sheppo return (EINVAL); 47991ae08745Sheppo } 48001ae08745Sheppo 48011ae08745Sheppo /* Find the size of the exported memory */ 48021ae08745Sheppo export_size = 0; 48031ae08745Sheppo for (i = 0; i < ccount; i++) 48041ae08745Sheppo export_size += cookies[i].size; 48051ae08745Sheppo 48061ae08745Sheppo /* check to see if offset is valid */ 48071ae08745Sheppo if (off > export_size) { 4808*3af08d82Slm66018 DWARN(chid, 48091ae08745Sheppo "ldc_mem_copy: (0x%llx) start offset > export mem size\n", 4810*3af08d82Slm66018 chid); 48111ae08745Sheppo return (EINVAL); 48121ae08745Sheppo } 48131ae08745Sheppo 48141ae08745Sheppo /* 48151ae08745Sheppo * Check to see if the export size is smaller than the size we 48161ae08745Sheppo * are requesting to copy - if so flag an error 48171ae08745Sheppo */ 48181ae08745Sheppo if ((export_size - off) < *size) { 4819*3af08d82Slm66018 DWARN(chid, 48201ae08745Sheppo "ldc_mem_copy: (0x%llx) copy size > export mem size\n", 4821*3af08d82Slm66018 chid); 48221ae08745Sheppo return (EINVAL); 48231ae08745Sheppo } 48241ae08745Sheppo 48251ae08745Sheppo total_bal = min(export_size, *size); 48261ae08745Sheppo 48271ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 48281ae08745Sheppo pg_size = MMU_PAGESIZE; 48291ae08745Sheppo pg_size_code = page_szc(pg_size); 48301ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 48311ae08745Sheppo 4832*3af08d82Slm66018 D1(chid, "ldc_mem_copy: copying data " 48331ae08745Sheppo "(0x%llx) va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 4834*3af08d82Slm66018 chid, vaddr, pg_size, pg_size_code, pg_shift); 48351ae08745Sheppo 48361ae08745Sheppo /* aligned VA and its offset */ 48371ae08745Sheppo local_valign = (((uintptr_t)vaddr) & ~(pg_size - 1)); 48381ae08745Sheppo local_voff = ((uintptr_t)vaddr) & (pg_size - 1); 48391ae08745Sheppo 48401ae08745Sheppo npages = (len+local_voff)/pg_size; 48411ae08745Sheppo npages = ((len+local_voff)%pg_size == 0) ? npages : npages+1; 48421ae08745Sheppo 4843*3af08d82Slm66018 D1(chid, 48441ae08745Sheppo "ldc_mem_copy: (0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n", 4845*3af08d82Slm66018 chid, vaddr, local_valign, local_voff, npages); 48461ae08745Sheppo 48471ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 48481ae08745Sheppo local_poff = local_voff; 48491ae08745Sheppo local_psize = min(len, (pg_size - local_voff)); 48501ae08745Sheppo 48511ae08745Sheppo len -= local_psize; 48521ae08745Sheppo 48531ae08745Sheppo /* 48541ae08745Sheppo * find the first cookie in the list of cookies 48551ae08745Sheppo * if the offset passed in is not zero 48561ae08745Sheppo */ 48571ae08745Sheppo for (idx = 0; idx < ccount; idx++) { 48581ae08745Sheppo cookie_size = cookies[idx].size; 48591ae08745Sheppo if (off < cookie_size) 48601ae08745Sheppo break; 48611ae08745Sheppo off -= cookie_size; 48621ae08745Sheppo } 48631ae08745Sheppo 48641ae08745Sheppo cookie_addr = cookies[idx].addr + off; 48651ae08745Sheppo cookie_size = cookies[idx].size - off; 48661ae08745Sheppo 48671ae08745Sheppo export_caddr = cookie_addr & ~(pg_size - 1); 48681ae08745Sheppo export_poff = cookie_addr & (pg_size - 1); 48691ae08745Sheppo export_psize = min(cookie_size, (pg_size - export_poff)); 48701ae08745Sheppo 48711ae08745Sheppo for (;;) { 48721ae08745Sheppo 48731ae08745Sheppo copy_size = min(export_psize, local_psize); 48741ae08745Sheppo 4875*3af08d82Slm66018 D1(chid, 48761ae08745Sheppo "ldc_mem_copy:(0x%llx) dir=0x%x, caddr=0x%llx," 48771ae08745Sheppo " loc_ra=0x%llx, exp_poff=0x%llx, loc_poff=0x%llx," 48781ae08745Sheppo " exp_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx," 48791ae08745Sheppo " total_bal=0x%llx\n", 4880*3af08d82Slm66018 chid, direction, export_caddr, local_ra, export_poff, 48811ae08745Sheppo local_poff, export_psize, local_psize, copy_size, 48821ae08745Sheppo total_bal); 48831ae08745Sheppo 4884*3af08d82Slm66018 rv = hv_ldc_copy(chid, direction, 48851ae08745Sheppo (export_caddr + export_poff), (local_ra + local_poff), 48861ae08745Sheppo copy_size, &copied_len); 48871ae08745Sheppo 48881ae08745Sheppo if (rv != 0) { 4889*3af08d82Slm66018 int error = EIO; 4890*3af08d82Slm66018 uint64_t rx_hd, rx_tl; 4891*3af08d82Slm66018 4892*3af08d82Slm66018 DWARN(chid, 4893*3af08d82Slm66018 "ldc_mem_copy: (0x%llx) err %d during copy\n", 4894*3af08d82Slm66018 (unsigned long long)chid, rv); 4895*3af08d82Slm66018 DWARN(chid, 4896*3af08d82Slm66018 "ldc_mem_copy: (0x%llx) dir=0x%x, caddr=0x%lx, " 48974bac2208Snarayan "loc_ra=0x%lx, exp_poff=0x%lx, loc_poff=0x%lx," 48984bac2208Snarayan " exp_psz=0x%lx, loc_psz=0x%lx, copy_sz=0x%lx," 48994bac2208Snarayan " copied_len=0x%lx, total_bal=0x%lx\n", 4900*3af08d82Slm66018 chid, direction, export_caddr, local_ra, 49011ae08745Sheppo export_poff, local_poff, export_psize, local_psize, 49021ae08745Sheppo copy_size, copied_len, total_bal); 49031ae08745Sheppo 49041ae08745Sheppo *size = *size - total_bal; 4905*3af08d82Slm66018 4906*3af08d82Slm66018 /* 4907*3af08d82Slm66018 * check if reason for copy error was due to 4908*3af08d82Slm66018 * a channel reset. we need to grab the lock 4909*3af08d82Slm66018 * just in case we have to do a reset. 4910*3af08d82Slm66018 */ 4911*3af08d82Slm66018 mutex_enter(&ldcp->lock); 4912*3af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 4913*3af08d82Slm66018 4914*3af08d82Slm66018 rv = hv_ldc_rx_get_state(ldcp->id, 4915*3af08d82Slm66018 &rx_hd, &rx_tl, &(ldcp->link_state)); 4916*3af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 4917*3af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 4918*3af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4919*3af08d82Slm66018 error = ECONNRESET; 4920*3af08d82Slm66018 } 4921*3af08d82Slm66018 4922*3af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 49231ae08745Sheppo mutex_exit(&ldcp->lock); 4924*3af08d82Slm66018 4925*3af08d82Slm66018 return (error); 49261ae08745Sheppo } 49271ae08745Sheppo 49281ae08745Sheppo ASSERT(copied_len <= copy_size); 49291ae08745Sheppo 4930*3af08d82Slm66018 D2(chid, "ldc_mem_copy: copied=0x%llx\n", copied_len); 49311ae08745Sheppo export_poff += copied_len; 49321ae08745Sheppo local_poff += copied_len; 49331ae08745Sheppo export_psize -= copied_len; 49341ae08745Sheppo local_psize -= copied_len; 49351ae08745Sheppo cookie_size -= copied_len; 49361ae08745Sheppo 49371ae08745Sheppo total_bal -= copied_len; 49381ae08745Sheppo 49391ae08745Sheppo if (copy_size != copied_len) 49401ae08745Sheppo continue; 49411ae08745Sheppo 49421ae08745Sheppo if (export_psize == 0 && total_bal != 0) { 49431ae08745Sheppo 49441ae08745Sheppo if (cookie_size == 0) { 49451ae08745Sheppo idx++; 49461ae08745Sheppo cookie_addr = cookies[idx].addr; 49471ae08745Sheppo cookie_size = cookies[idx].size; 49481ae08745Sheppo 49491ae08745Sheppo export_caddr = cookie_addr & ~(pg_size - 1); 49501ae08745Sheppo export_poff = cookie_addr & (pg_size - 1); 49511ae08745Sheppo export_psize = 49521ae08745Sheppo min(cookie_size, (pg_size-export_poff)); 49531ae08745Sheppo } else { 49541ae08745Sheppo export_caddr += pg_size; 49551ae08745Sheppo export_poff = 0; 49561ae08745Sheppo export_psize = min(cookie_size, pg_size); 49571ae08745Sheppo } 49581ae08745Sheppo } 49591ae08745Sheppo 49601ae08745Sheppo if (local_psize == 0 && total_bal != 0) { 49611ae08745Sheppo local_valign += pg_size; 49621ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 49631ae08745Sheppo local_poff = 0; 49641ae08745Sheppo local_psize = min(pg_size, len); 49651ae08745Sheppo len -= local_psize; 49661ae08745Sheppo } 49671ae08745Sheppo 49681ae08745Sheppo /* check if we are all done */ 49691ae08745Sheppo if (total_bal == 0) 49701ae08745Sheppo break; 49711ae08745Sheppo } 49721ae08745Sheppo 49731ae08745Sheppo 4974*3af08d82Slm66018 D1(chid, 49751ae08745Sheppo "ldc_mem_copy: (0x%llx) done copying sz=0x%llx\n", 4976*3af08d82Slm66018 chid, *size); 49771ae08745Sheppo 49781ae08745Sheppo return (0); 49791ae08745Sheppo } 49801ae08745Sheppo 49811ae08745Sheppo /* 49821ae08745Sheppo * Copy data either from or to the client specified virtual address 49831ae08745Sheppo * space to or from HV physical memory. 49841ae08745Sheppo * 49851ae08745Sheppo * The direction argument determines whether the data is read from or 49861ae08745Sheppo * written to HV memory. direction values are LDC_COPY_IN/OUT similar 49871ae08745Sheppo * to the ldc_mem_copy interface 49881ae08745Sheppo */ 49891ae08745Sheppo int 4990*3af08d82Slm66018 ldc_mem_rdwr_cookie(ldc_handle_t handle, caddr_t vaddr, size_t *size, 49911ae08745Sheppo caddr_t paddr, uint8_t direction) 49921ae08745Sheppo { 49931ae08745Sheppo ldc_chan_t *ldcp; 49941ae08745Sheppo uint64_t local_voff, local_valign; 49951ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code; 49961ae08745Sheppo uint64_t target_pa, target_poff, target_psize, target_size; 49971ae08745Sheppo uint64_t local_ra, local_poff, local_psize; 49981ae08745Sheppo uint64_t copy_size, copied_len = 0; 49991ae08745Sheppo pgcnt_t npages; 50001ae08745Sheppo size_t len = *size; 50011ae08745Sheppo int rv = 0; 50021ae08745Sheppo 50031ae08745Sheppo if (handle == NULL) { 50041ae08745Sheppo DWARN(DBG_ALL_LDCS, 5005*3af08d82Slm66018 "ldc_mem_rdwr_cookie: invalid channel handle\n"); 50061ae08745Sheppo return (EINVAL); 50071ae08745Sheppo } 50081ae08745Sheppo ldcp = (ldc_chan_t *)handle; 50091ae08745Sheppo 50101ae08745Sheppo mutex_enter(&ldcp->lock); 50111ae08745Sheppo 50121ae08745Sheppo /* check to see if channel is UP */ 50131ae08745Sheppo if (ldcp->tstate != TS_UP) { 50141ae08745Sheppo DWARN(ldcp->id, 5015*3af08d82Slm66018 "ldc_mem_rdwr_cookie: (0x%llx) channel is not UP\n", 50161ae08745Sheppo ldcp->id); 50171ae08745Sheppo mutex_exit(&ldcp->lock); 5018*3af08d82Slm66018 return (ECONNRESET); 50191ae08745Sheppo } 50201ae08745Sheppo 50211ae08745Sheppo /* Force address and size to be 8-byte aligned */ 50221ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 50231ae08745Sheppo DWARN(ldcp->id, 5024*3af08d82Slm66018 "ldc_mem_rdwr_cookie: addr/size is not 8-byte aligned\n"); 50251ae08745Sheppo mutex_exit(&ldcp->lock); 50261ae08745Sheppo return (EINVAL); 50271ae08745Sheppo } 50281ae08745Sheppo 50291ae08745Sheppo target_size = *size; 50301ae08745Sheppo 50311ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 50321ae08745Sheppo pg_size = MMU_PAGESIZE; 50331ae08745Sheppo pg_size_code = page_szc(pg_size); 50341ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 50351ae08745Sheppo 5036*3af08d82Slm66018 D1(ldcp->id, "ldc_mem_rdwr_cookie: copying data " 50371ae08745Sheppo "(0x%llx) va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 50381ae08745Sheppo ldcp->id, vaddr, pg_size, pg_size_code, pg_shift); 50391ae08745Sheppo 50401ae08745Sheppo /* aligned VA and its offset */ 50411ae08745Sheppo local_valign = ((uintptr_t)vaddr) & ~(pg_size - 1); 50421ae08745Sheppo local_voff = ((uintptr_t)vaddr) & (pg_size - 1); 50431ae08745Sheppo 50441ae08745Sheppo npages = (len + local_voff) / pg_size; 50451ae08745Sheppo npages = ((len + local_voff) % pg_size == 0) ? npages : npages+1; 50461ae08745Sheppo 5047*3af08d82Slm66018 D1(ldcp->id, "ldc_mem_rdwr_cookie: (0x%llx) v=0x%llx, " 5048*3af08d82Slm66018 "val=0x%llx,off=0x%x,pgs=0x%x\n", 50491ae08745Sheppo ldcp->id, vaddr, local_valign, local_voff, npages); 50501ae08745Sheppo 50511ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 50521ae08745Sheppo local_poff = local_voff; 50531ae08745Sheppo local_psize = min(len, (pg_size - local_voff)); 50541ae08745Sheppo 50551ae08745Sheppo len -= local_psize; 50561ae08745Sheppo 50571ae08745Sheppo target_pa = ((uintptr_t)paddr) & ~(pg_size - 1); 50581ae08745Sheppo target_poff = ((uintptr_t)paddr) & (pg_size - 1); 50591ae08745Sheppo target_psize = pg_size - target_poff; 50601ae08745Sheppo 50611ae08745Sheppo for (;;) { 50621ae08745Sheppo 50631ae08745Sheppo copy_size = min(target_psize, local_psize); 50641ae08745Sheppo 50651ae08745Sheppo D1(ldcp->id, 5066*3af08d82Slm66018 "ldc_mem_rdwr_cookie: (0x%llx) dir=0x%x, tar_pa=0x%llx," 50671ae08745Sheppo " loc_ra=0x%llx, tar_poff=0x%llx, loc_poff=0x%llx," 50681ae08745Sheppo " tar_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx," 50691ae08745Sheppo " total_bal=0x%llx\n", 50701ae08745Sheppo ldcp->id, direction, target_pa, local_ra, target_poff, 50711ae08745Sheppo local_poff, target_psize, local_psize, copy_size, 50721ae08745Sheppo target_size); 50731ae08745Sheppo 50741ae08745Sheppo rv = hv_ldc_copy(ldcp->id, direction, 50751ae08745Sheppo (target_pa + target_poff), (local_ra + local_poff), 50761ae08745Sheppo copy_size, &copied_len); 50771ae08745Sheppo 50781ae08745Sheppo if (rv != 0) { 5079*3af08d82Slm66018 DWARN(DBG_ALL_LDCS, 5080*3af08d82Slm66018 "ldc_mem_rdwr_cookie: (0x%lx) err %d during copy\n", 50811ae08745Sheppo ldcp->id, rv); 50821ae08745Sheppo DWARN(DBG_ALL_LDCS, 5083*3af08d82Slm66018 "ldc_mem_rdwr_cookie: (0x%llx) dir=%lld, " 5084*3af08d82Slm66018 "tar_pa=0x%llx, loc_ra=0x%llx, tar_poff=0x%llx, " 5085*3af08d82Slm66018 "loc_poff=0x%llx, tar_psz=0x%llx, loc_psz=0x%llx, " 5086*3af08d82Slm66018 "copy_sz=0x%llx, total_bal=0x%llx\n", 50871ae08745Sheppo ldcp->id, direction, target_pa, local_ra, 50881ae08745Sheppo target_poff, local_poff, target_psize, local_psize, 50891ae08745Sheppo copy_size, target_size); 50901ae08745Sheppo 50911ae08745Sheppo *size = *size - target_size; 50921ae08745Sheppo mutex_exit(&ldcp->lock); 50931ae08745Sheppo return (i_ldc_h2v_error(rv)); 50941ae08745Sheppo } 50951ae08745Sheppo 5096*3af08d82Slm66018 D2(ldcp->id, "ldc_mem_rdwr_cookie: copied=0x%llx\n", 5097*3af08d82Slm66018 copied_len); 50981ae08745Sheppo target_poff += copied_len; 50991ae08745Sheppo local_poff += copied_len; 51001ae08745Sheppo target_psize -= copied_len; 51011ae08745Sheppo local_psize -= copied_len; 51021ae08745Sheppo 51031ae08745Sheppo target_size -= copied_len; 51041ae08745Sheppo 51051ae08745Sheppo if (copy_size != copied_len) 51061ae08745Sheppo continue; 51071ae08745Sheppo 51081ae08745Sheppo if (target_psize == 0 && target_size != 0) { 51091ae08745Sheppo target_pa += pg_size; 51101ae08745Sheppo target_poff = 0; 51111ae08745Sheppo target_psize = min(pg_size, target_size); 51121ae08745Sheppo } 51131ae08745Sheppo 51141ae08745Sheppo if (local_psize == 0 && target_size != 0) { 51151ae08745Sheppo local_valign += pg_size; 51161ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 51171ae08745Sheppo local_poff = 0; 51181ae08745Sheppo local_psize = min(pg_size, len); 51191ae08745Sheppo len -= local_psize; 51201ae08745Sheppo } 51211ae08745Sheppo 51221ae08745Sheppo /* check if we are all done */ 51231ae08745Sheppo if (target_size == 0) 51241ae08745Sheppo break; 51251ae08745Sheppo } 51261ae08745Sheppo 51271ae08745Sheppo mutex_exit(&ldcp->lock); 51281ae08745Sheppo 5129*3af08d82Slm66018 D1(ldcp->id, "ldc_mem_rdwr_cookie: (0x%llx) done copying sz=0x%llx\n", 51301ae08745Sheppo ldcp->id, *size); 51311ae08745Sheppo 51321ae08745Sheppo return (0); 51331ae08745Sheppo } 51341ae08745Sheppo 51351ae08745Sheppo /* 51361ae08745Sheppo * Map an exported memory segment into the local address space. If the 51371ae08745Sheppo * memory range was exported for direct map access, a HV call is made 51381ae08745Sheppo * to allocate a RA range. If the map is done via a shadow copy, local 51391ae08745Sheppo * shadow memory is allocated and the base VA is returned in 'vaddr'. If 51401ae08745Sheppo * the mapping is a direct map then the RA is returned in 'raddr'. 51411ae08745Sheppo */ 51421ae08745Sheppo int 51431ae08745Sheppo ldc_mem_map(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie, uint32_t ccount, 51444bac2208Snarayan uint8_t mtype, uint8_t perm, caddr_t *vaddr, caddr_t *raddr) 51451ae08745Sheppo { 51464bac2208Snarayan int i, j, idx, rv, retries; 51471ae08745Sheppo ldc_chan_t *ldcp; 51481ae08745Sheppo ldc_mhdl_t *mhdl; 51491ae08745Sheppo ldc_memseg_t *memseg; 51504bac2208Snarayan caddr_t tmpaddr; 51514bac2208Snarayan uint64_t map_perm = perm; 51524bac2208Snarayan uint64_t pg_size, pg_shift, pg_size_code, pg_mask; 51534bac2208Snarayan uint64_t exp_size = 0, base_off, map_size, npages; 51544bac2208Snarayan uint64_t cookie_addr, cookie_off, cookie_size; 51554bac2208Snarayan tte_t ldc_tte; 51561ae08745Sheppo 51571ae08745Sheppo if (mhandle == NULL) { 51581ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_map: invalid memory handle\n"); 51591ae08745Sheppo return (EINVAL); 51601ae08745Sheppo } 51611ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 51621ae08745Sheppo 51631ae08745Sheppo mutex_enter(&mhdl->lock); 51641ae08745Sheppo 51651ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED || 51661ae08745Sheppo mhdl->memseg != NULL) { 51671ae08745Sheppo DWARN(DBG_ALL_LDCS, 51681ae08745Sheppo "ldc_mem_map: (0x%llx) handle bound/mapped\n", mhandle); 51691ae08745Sheppo mutex_exit(&mhdl->lock); 51701ae08745Sheppo return (EINVAL); 51711ae08745Sheppo } 51721ae08745Sheppo 51731ae08745Sheppo ldcp = mhdl->ldcp; 51741ae08745Sheppo 51751ae08745Sheppo mutex_enter(&ldcp->lock); 51761ae08745Sheppo 51771ae08745Sheppo if (ldcp->tstate != TS_UP) { 51781ae08745Sheppo DWARN(ldcp->id, 51791ae08745Sheppo "ldc_mem_dring_map: (0x%llx) channel is not UP\n", 51801ae08745Sheppo ldcp->id); 51811ae08745Sheppo mutex_exit(&ldcp->lock); 51821ae08745Sheppo mutex_exit(&mhdl->lock); 5183*3af08d82Slm66018 return (ECONNRESET); 51841ae08745Sheppo } 51851ae08745Sheppo 51861ae08745Sheppo if ((mtype & (LDC_SHADOW_MAP|LDC_DIRECT_MAP|LDC_IO_MAP)) == 0) { 51871ae08745Sheppo DWARN(ldcp->id, "ldc_mem_map: invalid map type\n"); 51881ae08745Sheppo mutex_exit(&ldcp->lock); 51891ae08745Sheppo mutex_exit(&mhdl->lock); 51901ae08745Sheppo return (EINVAL); 51911ae08745Sheppo } 51921ae08745Sheppo 51931ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) cookie = 0x%llx,0x%llx\n", 5194d10e4ef2Snarayan ldcp->id, cookie->addr, cookie->size); 51951ae08745Sheppo 51961ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 51971ae08745Sheppo pg_size = MMU_PAGESIZE; 51981ae08745Sheppo pg_size_code = page_szc(pg_size); 51991ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 52004bac2208Snarayan pg_mask = ~(pg_size - 1); 52011ae08745Sheppo 52021ae08745Sheppo /* calculate the number of pages in the exported cookie */ 52034bac2208Snarayan base_off = cookie[0].addr & (pg_size - 1); 52044bac2208Snarayan for (idx = 0; idx < ccount; idx++) 52051ae08745Sheppo exp_size += cookie[idx].size; 52064bac2208Snarayan map_size = P2ROUNDUP((exp_size + base_off), pg_size); 52074bac2208Snarayan npages = (map_size >> pg_shift); 52081ae08745Sheppo 52091ae08745Sheppo /* Allocate memseg structure */ 52104bac2208Snarayan memseg = mhdl->memseg = 52114bac2208Snarayan kmem_cache_alloc(ldcssp->memseg_cache, KM_SLEEP); 52121ae08745Sheppo 52131ae08745Sheppo /* Allocate memory to store all pages and cookies */ 52141ae08745Sheppo memseg->pages = kmem_zalloc((sizeof (ldc_page_t) * npages), KM_SLEEP); 52151ae08745Sheppo memseg->cookies = 52161ae08745Sheppo kmem_zalloc((sizeof (ldc_mem_cookie_t) * ccount), KM_SLEEP); 52171ae08745Sheppo 52184bac2208Snarayan D2(ldcp->id, "ldc_mem_map: (0x%llx) exp_size=0x%llx, map_size=0x%llx," 52194bac2208Snarayan "pages=0x%llx\n", ldcp->id, exp_size, map_size, npages); 52201ae08745Sheppo 52214bac2208Snarayan /* 52224bac2208Snarayan * Check if direct map over shared memory is enabled, if not change 52234bac2208Snarayan * the mapping type to SHADOW_MAP. 52244bac2208Snarayan */ 52254bac2208Snarayan if (ldc_shmem_enabled == 0) 52264bac2208Snarayan mtype = LDC_SHADOW_MAP; 52274bac2208Snarayan 52284bac2208Snarayan /* 52294bac2208Snarayan * Check to see if the client is requesting direct or shadow map 52304bac2208Snarayan * If direct map is requested, try to map remote memory first, 52314bac2208Snarayan * and if that fails, revert to shadow map 52324bac2208Snarayan */ 52334bac2208Snarayan if (mtype == LDC_DIRECT_MAP) { 52344bac2208Snarayan 52354bac2208Snarayan /* Allocate kernel virtual space for mapping */ 52364bac2208Snarayan memseg->vaddr = vmem_xalloc(heap_arena, map_size, 52374bac2208Snarayan pg_size, 0, 0, NULL, NULL, VM_NOSLEEP); 52384bac2208Snarayan if (memseg->vaddr == NULL) { 52394bac2208Snarayan cmn_err(CE_WARN, 52404bac2208Snarayan "ldc_mem_map: (0x%lx) memory map failed\n", 52414bac2208Snarayan ldcp->id); 52424bac2208Snarayan kmem_free(memseg->cookies, 52434bac2208Snarayan (sizeof (ldc_mem_cookie_t) * ccount)); 52444bac2208Snarayan kmem_free(memseg->pages, 52454bac2208Snarayan (sizeof (ldc_page_t) * npages)); 52464bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 52474bac2208Snarayan 52484bac2208Snarayan mutex_exit(&ldcp->lock); 52494bac2208Snarayan mutex_exit(&mhdl->lock); 52504bac2208Snarayan return (ENOMEM); 52514bac2208Snarayan } 52524bac2208Snarayan 52534bac2208Snarayan /* Unload previous mapping */ 52544bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, map_size, 52554bac2208Snarayan HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK); 52564bac2208Snarayan 52574bac2208Snarayan /* for each cookie passed in - map into address space */ 52584bac2208Snarayan idx = 0; 52594bac2208Snarayan cookie_size = 0; 52604bac2208Snarayan tmpaddr = memseg->vaddr; 52614bac2208Snarayan 52624bac2208Snarayan for (i = 0; i < npages; i++) { 52634bac2208Snarayan 52644bac2208Snarayan if (cookie_size == 0) { 52654bac2208Snarayan ASSERT(idx < ccount); 52664bac2208Snarayan cookie_addr = cookie[idx].addr & pg_mask; 52674bac2208Snarayan cookie_off = cookie[idx].addr & (pg_size - 1); 52684bac2208Snarayan cookie_size = 52694bac2208Snarayan P2ROUNDUP((cookie_off + cookie[idx].size), 52704bac2208Snarayan pg_size); 52714bac2208Snarayan idx++; 52724bac2208Snarayan } 52734bac2208Snarayan 52744bac2208Snarayan D1(ldcp->id, "ldc_mem_map: (0x%llx) mapping " 52754bac2208Snarayan "cookie 0x%llx, bal=0x%llx\n", ldcp->id, 52764bac2208Snarayan cookie_addr, cookie_size); 52774bac2208Snarayan 52784bac2208Snarayan /* map the cookie into address space */ 52794bac2208Snarayan for (retries = 0; retries < ldc_max_retries; 52804bac2208Snarayan retries++) { 52814bac2208Snarayan 52824bac2208Snarayan rv = hv_ldc_mapin(ldcp->id, cookie_addr, 52834bac2208Snarayan &memseg->pages[i].raddr, &map_perm); 52844bac2208Snarayan if (rv != H_EWOULDBLOCK && rv != H_ETOOMANY) 52854bac2208Snarayan break; 52864bac2208Snarayan 52874bac2208Snarayan drv_usecwait(ldc_delay); 52884bac2208Snarayan } 52894bac2208Snarayan 52904bac2208Snarayan if (rv || memseg->pages[i].raddr == 0) { 52914bac2208Snarayan DWARN(ldcp->id, 52924bac2208Snarayan "ldc_mem_map: (0x%llx) hv mapin err %d\n", 52934bac2208Snarayan ldcp->id, rv); 52944bac2208Snarayan 52954bac2208Snarayan /* remove previous mapins */ 52964bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, map_size, 52974bac2208Snarayan HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK); 52984bac2208Snarayan for (j = 0; j < i; j++) { 52994bac2208Snarayan rv = hv_ldc_unmap( 53004bac2208Snarayan memseg->pages[j].raddr); 53014bac2208Snarayan if (rv) { 53024bac2208Snarayan DWARN(ldcp->id, 53034bac2208Snarayan "ldc_mem_map: (0x%llx) " 53044bac2208Snarayan "cannot unmap ra=0x%llx\n", 53054bac2208Snarayan ldcp->id, 53064bac2208Snarayan memseg->pages[j].raddr); 53074bac2208Snarayan } 53084bac2208Snarayan } 53094bac2208Snarayan 53104bac2208Snarayan /* free kernel virtual space */ 53114bac2208Snarayan vmem_free(heap_arena, (void *)memseg->vaddr, 53127636cb21Slm66018 map_size); 53134bac2208Snarayan 53144bac2208Snarayan /* direct map failed - revert to shadow map */ 53154bac2208Snarayan mtype = LDC_SHADOW_MAP; 53164bac2208Snarayan break; 53174bac2208Snarayan 53184bac2208Snarayan } else { 53194bac2208Snarayan 53204bac2208Snarayan D1(ldcp->id, 53214bac2208Snarayan "ldc_mem_map: (0x%llx) vtop map 0x%llx -> " 53224bac2208Snarayan "0x%llx, cookie=0x%llx, perm=0x%llx\n", 53234bac2208Snarayan ldcp->id, tmpaddr, memseg->pages[i].raddr, 53244bac2208Snarayan cookie_addr, perm); 53254bac2208Snarayan 53264bac2208Snarayan /* 53274bac2208Snarayan * NOTE: Calling hat_devload directly, causes it 53284bac2208Snarayan * to look for page_t using the pfn. Since this 53294bac2208Snarayan * addr is greater than the memlist, it treates 53304bac2208Snarayan * it as non-memory 53314bac2208Snarayan */ 53324bac2208Snarayan sfmmu_memtte(&ldc_tte, 53334bac2208Snarayan (pfn_t)(memseg->pages[i].raddr >> pg_shift), 53344bac2208Snarayan PROT_READ | PROT_WRITE | HAT_NOSYNC, TTE8K); 53354bac2208Snarayan 53364bac2208Snarayan D1(ldcp->id, 53374bac2208Snarayan "ldc_mem_map: (0x%llx) ra 0x%llx -> " 53384bac2208Snarayan "tte 0x%llx\n", ldcp->id, 53394bac2208Snarayan memseg->pages[i].raddr, ldc_tte); 53404bac2208Snarayan 53414bac2208Snarayan sfmmu_tteload(kas.a_hat, &ldc_tte, tmpaddr, 53424bac2208Snarayan NULL, HAT_LOAD_LOCK); 53434bac2208Snarayan 53444bac2208Snarayan cookie_size -= pg_size; 53454bac2208Snarayan cookie_addr += pg_size; 53464bac2208Snarayan tmpaddr += pg_size; 53474bac2208Snarayan } 53484bac2208Snarayan } 53494bac2208Snarayan } 53504bac2208Snarayan 53511ae08745Sheppo if (mtype == LDC_SHADOW_MAP) { 53521ae08745Sheppo if (*vaddr == NULL) { 5353*3af08d82Slm66018 memseg->vaddr = kmem_zalloc(exp_size, KM_SLEEP); 53541ae08745Sheppo mhdl->myshadow = B_TRUE; 53551ae08745Sheppo 53561ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) allocated " 53574bac2208Snarayan "shadow page va=0x%llx\n", ldcp->id, memseg->vaddr); 53581ae08745Sheppo } else { 53591ae08745Sheppo /* 53604bac2208Snarayan * Use client supplied memory for memseg->vaddr 53611ae08745Sheppo * WARNING: assuming that client mem is >= exp_size 53621ae08745Sheppo */ 53634bac2208Snarayan memseg->vaddr = *vaddr; 53641ae08745Sheppo } 53651ae08745Sheppo 53661ae08745Sheppo /* Save all page and cookie information */ 53674bac2208Snarayan for (i = 0, tmpaddr = memseg->vaddr; i < npages; i++) { 53681ae08745Sheppo memseg->pages[i].raddr = va_to_pa(tmpaddr); 53691ae08745Sheppo memseg->pages[i].size = pg_size; 53701ae08745Sheppo tmpaddr += pg_size; 53711ae08745Sheppo } 53724bac2208Snarayan 53731ae08745Sheppo } 53741ae08745Sheppo 53754bac2208Snarayan /* save all cookies */ 53764bac2208Snarayan bcopy(cookie, memseg->cookies, ccount * sizeof (ldc_mem_cookie_t)); 53774bac2208Snarayan 53781ae08745Sheppo /* update memseg_t */ 53791ae08745Sheppo memseg->raddr = memseg->pages[0].raddr; 53804bac2208Snarayan memseg->size = (mtype == LDC_SHADOW_MAP) ? exp_size : map_size; 53811ae08745Sheppo memseg->npages = npages; 53821ae08745Sheppo memseg->ncookies = ccount; 53831ae08745Sheppo memseg->next_cookie = 0; 53841ae08745Sheppo 53851ae08745Sheppo /* memory handle = mapped */ 53861ae08745Sheppo mhdl->mtype = mtype; 53874bac2208Snarayan mhdl->perm = perm; 53881ae08745Sheppo mhdl->status = LDC_MAPPED; 53891ae08745Sheppo 53901ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) mapped 0x%llx, ra=0x%llx, " 53911ae08745Sheppo "va=0x%llx, pgs=0x%llx cookies=0x%llx\n", 53921ae08745Sheppo ldcp->id, mhdl, memseg->raddr, memseg->vaddr, 53931ae08745Sheppo memseg->npages, memseg->ncookies); 53941ae08745Sheppo 53954bac2208Snarayan if (mtype == LDC_SHADOW_MAP) 53964bac2208Snarayan base_off = 0; 53971ae08745Sheppo if (raddr) 53984bac2208Snarayan *raddr = (caddr_t)(memseg->raddr | base_off); 53991ae08745Sheppo if (vaddr) 54004bac2208Snarayan *vaddr = (caddr_t)((uintptr_t)memseg->vaddr | base_off); 54011ae08745Sheppo 54021ae08745Sheppo mutex_exit(&ldcp->lock); 54031ae08745Sheppo mutex_exit(&mhdl->lock); 54041ae08745Sheppo return (0); 54051ae08745Sheppo } 54061ae08745Sheppo 54071ae08745Sheppo /* 54081ae08745Sheppo * Unmap a memory segment. Free shadow memory (if any). 54091ae08745Sheppo */ 54101ae08745Sheppo int 54111ae08745Sheppo ldc_mem_unmap(ldc_mem_handle_t mhandle) 54121ae08745Sheppo { 54134bac2208Snarayan int i, rv; 54141ae08745Sheppo ldc_mhdl_t *mhdl = (ldc_mhdl_t *)mhandle; 54151ae08745Sheppo ldc_chan_t *ldcp; 54161ae08745Sheppo ldc_memseg_t *memseg; 54171ae08745Sheppo 54181ae08745Sheppo if (mhdl == 0 || mhdl->status != LDC_MAPPED) { 54191ae08745Sheppo DWARN(DBG_ALL_LDCS, 54201ae08745Sheppo "ldc_mem_unmap: (0x%llx) handle is not mapped\n", 54211ae08745Sheppo mhandle); 54221ae08745Sheppo return (EINVAL); 54231ae08745Sheppo } 54241ae08745Sheppo 54251ae08745Sheppo mutex_enter(&mhdl->lock); 54261ae08745Sheppo 54271ae08745Sheppo ldcp = mhdl->ldcp; 54281ae08745Sheppo memseg = mhdl->memseg; 54291ae08745Sheppo 54301ae08745Sheppo D1(ldcp->id, "ldc_mem_unmap: (0x%llx) unmapping handle 0x%llx\n", 54311ae08745Sheppo ldcp->id, mhdl); 54321ae08745Sheppo 54331ae08745Sheppo /* if we allocated shadow memory - free it */ 54341ae08745Sheppo if (mhdl->mtype == LDC_SHADOW_MAP && mhdl->myshadow) { 5435*3af08d82Slm66018 kmem_free(memseg->vaddr, memseg->size); 54364bac2208Snarayan } else if (mhdl->mtype == LDC_DIRECT_MAP) { 54374bac2208Snarayan 54384bac2208Snarayan /* unmap in the case of DIRECT_MAP */ 54394bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, memseg->size, 54404bac2208Snarayan HAT_UNLOAD_UNLOCK); 54414bac2208Snarayan 54424bac2208Snarayan for (i = 0; i < memseg->npages; i++) { 54434bac2208Snarayan rv = hv_ldc_unmap(memseg->pages[i].raddr); 54444bac2208Snarayan if (rv) { 54454bac2208Snarayan cmn_err(CE_WARN, 54464bac2208Snarayan "ldc_mem_map: (0x%lx) hv unmap err %d\n", 54474bac2208Snarayan ldcp->id, rv); 54484bac2208Snarayan } 54494bac2208Snarayan } 54504bac2208Snarayan 54514bac2208Snarayan vmem_free(heap_arena, (void *)memseg->vaddr, memseg->size); 54521ae08745Sheppo } 54531ae08745Sheppo 54541ae08745Sheppo /* free the allocated memseg and page structures */ 54551ae08745Sheppo kmem_free(memseg->pages, (sizeof (ldc_page_t) * memseg->npages)); 54561ae08745Sheppo kmem_free(memseg->cookies, 54571ae08745Sheppo (sizeof (ldc_mem_cookie_t) * memseg->ncookies)); 54584bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 54591ae08745Sheppo 54601ae08745Sheppo /* uninitialize the memory handle */ 54611ae08745Sheppo mhdl->memseg = NULL; 54621ae08745Sheppo mhdl->status = LDC_UNBOUND; 54631ae08745Sheppo 54641ae08745Sheppo D1(ldcp->id, "ldc_mem_unmap: (0x%llx) unmapped handle 0x%llx\n", 54651ae08745Sheppo ldcp->id, mhdl); 54661ae08745Sheppo 54671ae08745Sheppo mutex_exit(&mhdl->lock); 54681ae08745Sheppo return (0); 54691ae08745Sheppo } 54701ae08745Sheppo 54711ae08745Sheppo /* 54721ae08745Sheppo * Internal entry point for LDC mapped memory entry consistency 54731ae08745Sheppo * semantics. Acquire copies the contents of the remote memory 54741ae08745Sheppo * into the local shadow copy. The release operation copies the local 54751ae08745Sheppo * contents into the remote memory. The offset and size specify the 54761ae08745Sheppo * bounds for the memory range being synchronized. 54771ae08745Sheppo */ 54781ae08745Sheppo static int 54791ae08745Sheppo i_ldc_mem_acquire_release(ldc_mem_handle_t mhandle, uint8_t direction, 54801ae08745Sheppo uint64_t offset, size_t size) 54811ae08745Sheppo { 54821ae08745Sheppo int err; 54831ae08745Sheppo ldc_mhdl_t *mhdl; 54841ae08745Sheppo ldc_chan_t *ldcp; 54851ae08745Sheppo ldc_memseg_t *memseg; 54861ae08745Sheppo caddr_t local_vaddr; 54871ae08745Sheppo size_t copy_size; 54881ae08745Sheppo 54891ae08745Sheppo if (mhandle == NULL) { 54901ae08745Sheppo DWARN(DBG_ALL_LDCS, 54911ae08745Sheppo "i_ldc_mem_acquire_release: invalid memory handle\n"); 54921ae08745Sheppo return (EINVAL); 54931ae08745Sheppo } 54941ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 54951ae08745Sheppo 54961ae08745Sheppo mutex_enter(&mhdl->lock); 54971ae08745Sheppo 54981ae08745Sheppo if (mhdl->status != LDC_MAPPED || mhdl->ldcp == NULL) { 54991ae08745Sheppo DWARN(DBG_ALL_LDCS, 55001ae08745Sheppo "i_ldc_mem_acquire_release: not mapped memory\n"); 55011ae08745Sheppo mutex_exit(&mhdl->lock); 55021ae08745Sheppo return (EINVAL); 55031ae08745Sheppo } 55041ae08745Sheppo 55054bac2208Snarayan /* do nothing for direct map */ 55064bac2208Snarayan if (mhdl->mtype == LDC_DIRECT_MAP) { 55074bac2208Snarayan mutex_exit(&mhdl->lock); 55084bac2208Snarayan return (0); 55094bac2208Snarayan } 55104bac2208Snarayan 55114bac2208Snarayan /* do nothing if COPY_IN+MEM_W and COPY_OUT+MEM_R */ 55124bac2208Snarayan if ((direction == LDC_COPY_IN && (mhdl->perm & LDC_MEM_R) == 0) || 55134bac2208Snarayan (direction == LDC_COPY_OUT && (mhdl->perm & LDC_MEM_W) == 0)) { 55144bac2208Snarayan mutex_exit(&mhdl->lock); 55154bac2208Snarayan return (0); 55164bac2208Snarayan } 55174bac2208Snarayan 55181ae08745Sheppo if (offset >= mhdl->memseg->size || 55191ae08745Sheppo (offset + size) > mhdl->memseg->size) { 55201ae08745Sheppo DWARN(DBG_ALL_LDCS, 55211ae08745Sheppo "i_ldc_mem_acquire_release: memory out of range\n"); 55221ae08745Sheppo mutex_exit(&mhdl->lock); 55231ae08745Sheppo return (EINVAL); 55241ae08745Sheppo } 55251ae08745Sheppo 55261ae08745Sheppo /* get the channel handle and memory segment */ 55271ae08745Sheppo ldcp = mhdl->ldcp; 55281ae08745Sheppo memseg = mhdl->memseg; 55291ae08745Sheppo 55301ae08745Sheppo if (mhdl->mtype == LDC_SHADOW_MAP) { 55311ae08745Sheppo 55321ae08745Sheppo local_vaddr = memseg->vaddr + offset; 55331ae08745Sheppo copy_size = size; 55341ae08745Sheppo 55351ae08745Sheppo /* copy to/from remote from/to local memory */ 55361ae08745Sheppo err = ldc_mem_copy((ldc_handle_t)ldcp, local_vaddr, offset, 55371ae08745Sheppo ©_size, memseg->cookies, memseg->ncookies, 55381ae08745Sheppo direction); 55391ae08745Sheppo if (err || copy_size != size) { 55401ae08745Sheppo cmn_err(CE_WARN, 55411ae08745Sheppo "i_ldc_mem_acquire_release: copy failed\n"); 55421ae08745Sheppo mutex_exit(&mhdl->lock); 55431ae08745Sheppo return (err); 55441ae08745Sheppo } 55451ae08745Sheppo } 55461ae08745Sheppo 55471ae08745Sheppo mutex_exit(&mhdl->lock); 55481ae08745Sheppo 55491ae08745Sheppo return (0); 55501ae08745Sheppo } 55511ae08745Sheppo 55521ae08745Sheppo /* 55531ae08745Sheppo * Ensure that the contents in the remote memory seg are consistent 55541ae08745Sheppo * with the contents if of local segment 55551ae08745Sheppo */ 55561ae08745Sheppo int 55571ae08745Sheppo ldc_mem_acquire(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size) 55581ae08745Sheppo { 55591ae08745Sheppo return (i_ldc_mem_acquire_release(mhandle, LDC_COPY_IN, offset, size)); 55601ae08745Sheppo } 55611ae08745Sheppo 55621ae08745Sheppo 55631ae08745Sheppo /* 55641ae08745Sheppo * Ensure that the contents in the local memory seg are consistent 55651ae08745Sheppo * with the contents if of remote segment 55661ae08745Sheppo */ 55671ae08745Sheppo int 55681ae08745Sheppo ldc_mem_release(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size) 55691ae08745Sheppo { 55701ae08745Sheppo return (i_ldc_mem_acquire_release(mhandle, LDC_COPY_OUT, offset, size)); 55711ae08745Sheppo } 55721ae08745Sheppo 55731ae08745Sheppo /* 55741ae08745Sheppo * Allocate a descriptor ring. The size of each each descriptor 55751ae08745Sheppo * must be 8-byte aligned and the entire ring should be a multiple 55761ae08745Sheppo * of MMU_PAGESIZE. 55771ae08745Sheppo */ 55781ae08745Sheppo int 55791ae08745Sheppo ldc_mem_dring_create(uint32_t len, uint32_t dsize, ldc_dring_handle_t *dhandle) 55801ae08745Sheppo { 55811ae08745Sheppo ldc_dring_t *dringp; 55821ae08745Sheppo size_t size = (dsize * len); 55831ae08745Sheppo 55841ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_create: len=0x%x, size=0x%x\n", 55851ae08745Sheppo len, dsize); 55861ae08745Sheppo 55871ae08745Sheppo if (dhandle == NULL) { 55881ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid dhandle\n"); 55891ae08745Sheppo return (EINVAL); 55901ae08745Sheppo } 55911ae08745Sheppo 55921ae08745Sheppo if (len == 0) { 55931ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid length\n"); 55941ae08745Sheppo return (EINVAL); 55951ae08745Sheppo } 55961ae08745Sheppo 55971ae08745Sheppo /* descriptor size should be 8-byte aligned */ 55981ae08745Sheppo if (dsize == 0 || (dsize & 0x7)) { 55991ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid size\n"); 56001ae08745Sheppo return (EINVAL); 56011ae08745Sheppo } 56021ae08745Sheppo 56031ae08745Sheppo *dhandle = 0; 56041ae08745Sheppo 56051ae08745Sheppo /* Allocate a desc ring structure */ 56061ae08745Sheppo dringp = kmem_zalloc(sizeof (ldc_dring_t), KM_SLEEP); 56071ae08745Sheppo 56081ae08745Sheppo /* Initialize dring */ 56091ae08745Sheppo dringp->length = len; 56101ae08745Sheppo dringp->dsize = dsize; 56111ae08745Sheppo 56121ae08745Sheppo /* round off to multiple of pagesize */ 56131ae08745Sheppo dringp->size = (size & MMU_PAGEMASK); 56141ae08745Sheppo if (size & MMU_PAGEOFFSET) 56151ae08745Sheppo dringp->size += MMU_PAGESIZE; 56161ae08745Sheppo 56171ae08745Sheppo dringp->status = LDC_UNBOUND; 56181ae08745Sheppo 56191ae08745Sheppo /* allocate descriptor ring memory */ 5620*3af08d82Slm66018 dringp->base = kmem_zalloc(dringp->size, KM_SLEEP); 56211ae08745Sheppo 56221ae08745Sheppo /* initialize the desc ring lock */ 56231ae08745Sheppo mutex_init(&dringp->lock, NULL, MUTEX_DRIVER, NULL); 56241ae08745Sheppo 56251ae08745Sheppo /* Add descriptor ring to the head of global list */ 56261ae08745Sheppo mutex_enter(&ldcssp->lock); 56271ae08745Sheppo dringp->next = ldcssp->dring_list; 56281ae08745Sheppo ldcssp->dring_list = dringp; 56291ae08745Sheppo mutex_exit(&ldcssp->lock); 56301ae08745Sheppo 56311ae08745Sheppo *dhandle = (ldc_dring_handle_t)dringp; 56321ae08745Sheppo 56331ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_create: dring allocated\n"); 56341ae08745Sheppo 56351ae08745Sheppo return (0); 56361ae08745Sheppo } 56371ae08745Sheppo 56381ae08745Sheppo 56391ae08745Sheppo /* 56401ae08745Sheppo * Destroy a descriptor ring. 56411ae08745Sheppo */ 56421ae08745Sheppo int 56431ae08745Sheppo ldc_mem_dring_destroy(ldc_dring_handle_t dhandle) 56441ae08745Sheppo { 56451ae08745Sheppo ldc_dring_t *dringp; 56461ae08745Sheppo ldc_dring_t *tmp_dringp; 56471ae08745Sheppo 56481ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_destroy: entered\n"); 56491ae08745Sheppo 56501ae08745Sheppo if (dhandle == NULL) { 56511ae08745Sheppo DWARN(DBG_ALL_LDCS, 56521ae08745Sheppo "ldc_mem_dring_destroy: invalid desc ring handle\n"); 56531ae08745Sheppo return (EINVAL); 56541ae08745Sheppo } 56551ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 56561ae08745Sheppo 56571ae08745Sheppo if (dringp->status == LDC_BOUND) { 56581ae08745Sheppo DWARN(DBG_ALL_LDCS, 56591ae08745Sheppo "ldc_mem_dring_destroy: desc ring is bound\n"); 56601ae08745Sheppo return (EACCES); 56611ae08745Sheppo } 56621ae08745Sheppo 56631ae08745Sheppo mutex_enter(&dringp->lock); 56641ae08745Sheppo mutex_enter(&ldcssp->lock); 56651ae08745Sheppo 56661ae08745Sheppo /* remove from linked list - if not bound */ 56671ae08745Sheppo tmp_dringp = ldcssp->dring_list; 56681ae08745Sheppo if (tmp_dringp == dringp) { 56691ae08745Sheppo ldcssp->dring_list = dringp->next; 56701ae08745Sheppo dringp->next = NULL; 56711ae08745Sheppo 56721ae08745Sheppo } else { 56731ae08745Sheppo while (tmp_dringp != NULL) { 56741ae08745Sheppo if (tmp_dringp->next == dringp) { 56751ae08745Sheppo tmp_dringp->next = dringp->next; 56761ae08745Sheppo dringp->next = NULL; 56771ae08745Sheppo break; 56781ae08745Sheppo } 56791ae08745Sheppo tmp_dringp = tmp_dringp->next; 56801ae08745Sheppo } 56811ae08745Sheppo if (tmp_dringp == NULL) { 56821ae08745Sheppo DWARN(DBG_ALL_LDCS, 56831ae08745Sheppo "ldc_mem_dring_destroy: invalid descriptor\n"); 56841ae08745Sheppo mutex_exit(&ldcssp->lock); 56851ae08745Sheppo mutex_exit(&dringp->lock); 56861ae08745Sheppo return (EINVAL); 56871ae08745Sheppo } 56881ae08745Sheppo } 56891ae08745Sheppo 56901ae08745Sheppo mutex_exit(&ldcssp->lock); 56911ae08745Sheppo 56921ae08745Sheppo /* free the descriptor ring */ 5693*3af08d82Slm66018 kmem_free(dringp->base, dringp->size); 56941ae08745Sheppo 56951ae08745Sheppo mutex_exit(&dringp->lock); 56961ae08745Sheppo 56971ae08745Sheppo /* destroy dring lock */ 56981ae08745Sheppo mutex_destroy(&dringp->lock); 56991ae08745Sheppo 57001ae08745Sheppo /* free desc ring object */ 57011ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 57021ae08745Sheppo 57031ae08745Sheppo return (0); 57041ae08745Sheppo } 57051ae08745Sheppo 57061ae08745Sheppo /* 57071ae08745Sheppo * Bind a previously allocated dring to a channel. The channel should 57081ae08745Sheppo * be OPEN in order to bind the ring to the channel. Returns back a 57091ae08745Sheppo * descriptor ring cookie. The descriptor ring is exported for remote 57101ae08745Sheppo * access by the client at the other end of the channel. An entry for 57111ae08745Sheppo * dring pages is stored in map table (via call to ldc_mem_bind_handle). 57121ae08745Sheppo */ 57131ae08745Sheppo int 57141ae08745Sheppo ldc_mem_dring_bind(ldc_handle_t handle, ldc_dring_handle_t dhandle, 57151ae08745Sheppo uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount) 57161ae08745Sheppo { 57171ae08745Sheppo int err; 57181ae08745Sheppo ldc_chan_t *ldcp; 57191ae08745Sheppo ldc_dring_t *dringp; 57201ae08745Sheppo ldc_mem_handle_t mhandle; 57211ae08745Sheppo 57221ae08745Sheppo /* check to see if channel is initalized */ 57231ae08745Sheppo if (handle == NULL) { 57241ae08745Sheppo DWARN(DBG_ALL_LDCS, 57251ae08745Sheppo "ldc_mem_dring_bind: invalid channel handle\n"); 57261ae08745Sheppo return (EINVAL); 57271ae08745Sheppo } 57281ae08745Sheppo ldcp = (ldc_chan_t *)handle; 57291ae08745Sheppo 57301ae08745Sheppo if (dhandle == NULL) { 57311ae08745Sheppo DWARN(DBG_ALL_LDCS, 57321ae08745Sheppo "ldc_mem_dring_bind: invalid desc ring handle\n"); 57331ae08745Sheppo return (EINVAL); 57341ae08745Sheppo } 57351ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 57361ae08745Sheppo 57371ae08745Sheppo if (cookie == NULL) { 57381ae08745Sheppo DWARN(ldcp->id, 57391ae08745Sheppo "ldc_mem_dring_bind: invalid cookie arg\n"); 57401ae08745Sheppo return (EINVAL); 57411ae08745Sheppo } 57421ae08745Sheppo 57431ae08745Sheppo mutex_enter(&dringp->lock); 57441ae08745Sheppo 57451ae08745Sheppo if (dringp->status == LDC_BOUND) { 57461ae08745Sheppo DWARN(DBG_ALL_LDCS, 57471ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) descriptor ring is bound\n", 57481ae08745Sheppo ldcp->id); 57491ae08745Sheppo mutex_exit(&dringp->lock); 57501ae08745Sheppo return (EINVAL); 57511ae08745Sheppo } 57521ae08745Sheppo 57531ae08745Sheppo if ((perm & LDC_MEM_RW) == 0) { 57541ae08745Sheppo DWARN(DBG_ALL_LDCS, 57551ae08745Sheppo "ldc_mem_dring_bind: invalid permissions\n"); 57561ae08745Sheppo mutex_exit(&dringp->lock); 57571ae08745Sheppo return (EINVAL); 57581ae08745Sheppo } 57591ae08745Sheppo 57601ae08745Sheppo if ((mtype & (LDC_SHADOW_MAP|LDC_DIRECT_MAP|LDC_IO_MAP)) == 0) { 57611ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_bind: invalid type\n"); 57621ae08745Sheppo mutex_exit(&dringp->lock); 57631ae08745Sheppo return (EINVAL); 57641ae08745Sheppo } 57651ae08745Sheppo 57661ae08745Sheppo dringp->ldcp = ldcp; 57671ae08745Sheppo 57681ae08745Sheppo /* create an memory handle */ 57691ae08745Sheppo err = ldc_mem_alloc_handle(handle, &mhandle); 57701ae08745Sheppo if (err || mhandle == NULL) { 57711ae08745Sheppo DWARN(DBG_ALL_LDCS, 57721ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) error allocating mhandle\n", 57731ae08745Sheppo ldcp->id); 57741ae08745Sheppo mutex_exit(&dringp->lock); 57751ae08745Sheppo return (err); 57761ae08745Sheppo } 57771ae08745Sheppo dringp->mhdl = mhandle; 57781ae08745Sheppo 57791ae08745Sheppo /* bind the descriptor ring to channel */ 57801ae08745Sheppo err = ldc_mem_bind_handle(mhandle, dringp->base, dringp->size, 57811ae08745Sheppo mtype, perm, cookie, ccount); 57821ae08745Sheppo if (err) { 57831ae08745Sheppo DWARN(ldcp->id, 57841ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) error binding mhandle\n", 57851ae08745Sheppo ldcp->id); 57861ae08745Sheppo mutex_exit(&dringp->lock); 57871ae08745Sheppo return (err); 57881ae08745Sheppo } 57891ae08745Sheppo 57901ae08745Sheppo /* 57911ae08745Sheppo * For now return error if we get more than one cookie 57921ae08745Sheppo * FUTURE: Return multiple cookies .. 57931ae08745Sheppo */ 57941ae08745Sheppo if (*ccount > 1) { 57951ae08745Sheppo (void) ldc_mem_unbind_handle(mhandle); 57961ae08745Sheppo (void) ldc_mem_free_handle(mhandle); 57971ae08745Sheppo 57981ae08745Sheppo dringp->ldcp = NULL; 57991ae08745Sheppo dringp->mhdl = NULL; 58001ae08745Sheppo *ccount = 0; 58011ae08745Sheppo 58021ae08745Sheppo mutex_exit(&dringp->lock); 58031ae08745Sheppo return (EAGAIN); 58041ae08745Sheppo } 58051ae08745Sheppo 58061ae08745Sheppo /* Add descriptor ring to channel's exported dring list */ 58071ae08745Sheppo mutex_enter(&ldcp->exp_dlist_lock); 58081ae08745Sheppo dringp->ch_next = ldcp->exp_dring_list; 58091ae08745Sheppo ldcp->exp_dring_list = dringp; 58101ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 58111ae08745Sheppo 58121ae08745Sheppo dringp->status = LDC_BOUND; 58131ae08745Sheppo 58141ae08745Sheppo mutex_exit(&dringp->lock); 58151ae08745Sheppo 58161ae08745Sheppo return (0); 58171ae08745Sheppo } 58181ae08745Sheppo 58191ae08745Sheppo /* 58201ae08745Sheppo * Return the next cookie associated with the specified dring handle 58211ae08745Sheppo */ 58221ae08745Sheppo int 58231ae08745Sheppo ldc_mem_dring_nextcookie(ldc_dring_handle_t dhandle, ldc_mem_cookie_t *cookie) 58241ae08745Sheppo { 58251ae08745Sheppo int rv = 0; 58261ae08745Sheppo ldc_dring_t *dringp; 58271ae08745Sheppo ldc_chan_t *ldcp; 58281ae08745Sheppo 58291ae08745Sheppo if (dhandle == NULL) { 58301ae08745Sheppo DWARN(DBG_ALL_LDCS, 58311ae08745Sheppo "ldc_mem_dring_nextcookie: invalid desc ring handle\n"); 58321ae08745Sheppo return (EINVAL); 58331ae08745Sheppo } 58341ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 58351ae08745Sheppo mutex_enter(&dringp->lock); 58361ae08745Sheppo 58371ae08745Sheppo if (dringp->status != LDC_BOUND) { 58381ae08745Sheppo DWARN(DBG_ALL_LDCS, 58391ae08745Sheppo "ldc_mem_dring_nextcookie: descriptor ring 0x%llx " 58401ae08745Sheppo "is not bound\n", dringp); 58411ae08745Sheppo mutex_exit(&dringp->lock); 58421ae08745Sheppo return (EINVAL); 58431ae08745Sheppo } 58441ae08745Sheppo 58451ae08745Sheppo ldcp = dringp->ldcp; 58461ae08745Sheppo 58471ae08745Sheppo if (cookie == NULL) { 58481ae08745Sheppo DWARN(ldcp->id, 58491ae08745Sheppo "ldc_mem_dring_nextcookie:(0x%llx) invalid cookie arg\n", 58501ae08745Sheppo ldcp->id); 58511ae08745Sheppo mutex_exit(&dringp->lock); 58521ae08745Sheppo return (EINVAL); 58531ae08745Sheppo } 58541ae08745Sheppo 58551ae08745Sheppo rv = ldc_mem_nextcookie((ldc_mem_handle_t)dringp->mhdl, cookie); 58561ae08745Sheppo mutex_exit(&dringp->lock); 58571ae08745Sheppo 58581ae08745Sheppo return (rv); 58591ae08745Sheppo } 58601ae08745Sheppo /* 58611ae08745Sheppo * Unbind a previously bound dring from a channel. 58621ae08745Sheppo */ 58631ae08745Sheppo int 58641ae08745Sheppo ldc_mem_dring_unbind(ldc_dring_handle_t dhandle) 58651ae08745Sheppo { 58661ae08745Sheppo ldc_dring_t *dringp; 58671ae08745Sheppo ldc_dring_t *tmp_dringp; 58681ae08745Sheppo ldc_chan_t *ldcp; 58691ae08745Sheppo 58701ae08745Sheppo if (dhandle == NULL) { 58711ae08745Sheppo DWARN(DBG_ALL_LDCS, 58721ae08745Sheppo "ldc_mem_dring_unbind: invalid desc ring handle\n"); 58731ae08745Sheppo return (EINVAL); 58741ae08745Sheppo } 58751ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 58761ae08745Sheppo 58771ae08745Sheppo mutex_enter(&dringp->lock); 58781ae08745Sheppo 58791ae08745Sheppo if (dringp->status == LDC_UNBOUND) { 58801ae08745Sheppo DWARN(DBG_ALL_LDCS, 58811ae08745Sheppo "ldc_mem_dring_bind: descriptor ring 0x%llx is unbound\n", 58821ae08745Sheppo dringp); 58831ae08745Sheppo mutex_exit(&dringp->lock); 58841ae08745Sheppo return (EINVAL); 58851ae08745Sheppo } 58861ae08745Sheppo ldcp = dringp->ldcp; 58871ae08745Sheppo 58881ae08745Sheppo mutex_enter(&ldcp->exp_dlist_lock); 58891ae08745Sheppo 58901ae08745Sheppo tmp_dringp = ldcp->exp_dring_list; 58911ae08745Sheppo if (tmp_dringp == dringp) { 58921ae08745Sheppo ldcp->exp_dring_list = dringp->ch_next; 58931ae08745Sheppo dringp->ch_next = NULL; 58941ae08745Sheppo 58951ae08745Sheppo } else { 58961ae08745Sheppo while (tmp_dringp != NULL) { 58971ae08745Sheppo if (tmp_dringp->ch_next == dringp) { 58981ae08745Sheppo tmp_dringp->ch_next = dringp->ch_next; 58991ae08745Sheppo dringp->ch_next = NULL; 59001ae08745Sheppo break; 59011ae08745Sheppo } 59021ae08745Sheppo tmp_dringp = tmp_dringp->ch_next; 59031ae08745Sheppo } 59041ae08745Sheppo if (tmp_dringp == NULL) { 59051ae08745Sheppo DWARN(DBG_ALL_LDCS, 59061ae08745Sheppo "ldc_mem_dring_unbind: invalid descriptor\n"); 59071ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 59081ae08745Sheppo mutex_exit(&dringp->lock); 59091ae08745Sheppo return (EINVAL); 59101ae08745Sheppo } 59111ae08745Sheppo } 59121ae08745Sheppo 59131ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 59141ae08745Sheppo 59151ae08745Sheppo (void) ldc_mem_unbind_handle((ldc_mem_handle_t)dringp->mhdl); 59161ae08745Sheppo (void) ldc_mem_free_handle((ldc_mem_handle_t)dringp->mhdl); 59171ae08745Sheppo 59181ae08745Sheppo dringp->ldcp = NULL; 59191ae08745Sheppo dringp->mhdl = NULL; 59201ae08745Sheppo dringp->status = LDC_UNBOUND; 59211ae08745Sheppo 59221ae08745Sheppo mutex_exit(&dringp->lock); 59231ae08745Sheppo 59241ae08745Sheppo return (0); 59251ae08745Sheppo } 59261ae08745Sheppo 59271ae08745Sheppo /* 59281ae08745Sheppo * Get information about the dring. The base address of the descriptor 59291ae08745Sheppo * ring along with the type and permission are returned back. 59301ae08745Sheppo */ 59311ae08745Sheppo int 59321ae08745Sheppo ldc_mem_dring_info(ldc_dring_handle_t dhandle, ldc_mem_info_t *minfo) 59331ae08745Sheppo { 59341ae08745Sheppo ldc_dring_t *dringp; 59351ae08745Sheppo int rv; 59361ae08745Sheppo 59371ae08745Sheppo if (dhandle == NULL) { 59381ae08745Sheppo DWARN(DBG_ALL_LDCS, 59391ae08745Sheppo "ldc_mem_dring_info: invalid desc ring handle\n"); 59401ae08745Sheppo return (EINVAL); 59411ae08745Sheppo } 59421ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 59431ae08745Sheppo 59441ae08745Sheppo mutex_enter(&dringp->lock); 59451ae08745Sheppo 59461ae08745Sheppo if (dringp->mhdl) { 59471ae08745Sheppo rv = ldc_mem_info(dringp->mhdl, minfo); 59481ae08745Sheppo if (rv) { 59491ae08745Sheppo DWARN(DBG_ALL_LDCS, 59501ae08745Sheppo "ldc_mem_dring_info: error reading mem info\n"); 59511ae08745Sheppo mutex_exit(&dringp->lock); 59521ae08745Sheppo return (rv); 59531ae08745Sheppo } 59541ae08745Sheppo } else { 59551ae08745Sheppo minfo->vaddr = dringp->base; 59561ae08745Sheppo minfo->raddr = NULL; 59571ae08745Sheppo minfo->status = dringp->status; 59581ae08745Sheppo } 59591ae08745Sheppo 59601ae08745Sheppo mutex_exit(&dringp->lock); 59611ae08745Sheppo 59621ae08745Sheppo return (0); 59631ae08745Sheppo } 59641ae08745Sheppo 59651ae08745Sheppo /* 59661ae08745Sheppo * Map an exported descriptor ring into the local address space. If the 59671ae08745Sheppo * descriptor ring was exported for direct map access, a HV call is made 59681ae08745Sheppo * to allocate a RA range. If the map is done via a shadow copy, local 59691ae08745Sheppo * shadow memory is allocated. 59701ae08745Sheppo */ 59711ae08745Sheppo int 59721ae08745Sheppo ldc_mem_dring_map(ldc_handle_t handle, ldc_mem_cookie_t *cookie, 59731ae08745Sheppo uint32_t ccount, uint32_t len, uint32_t dsize, uint8_t mtype, 59741ae08745Sheppo ldc_dring_handle_t *dhandle) 59751ae08745Sheppo { 59761ae08745Sheppo int err; 59771ae08745Sheppo ldc_chan_t *ldcp = (ldc_chan_t *)handle; 59781ae08745Sheppo ldc_mem_handle_t mhandle; 59791ae08745Sheppo ldc_dring_t *dringp; 59801ae08745Sheppo size_t dring_size; 59811ae08745Sheppo 59821ae08745Sheppo if (dhandle == NULL) { 59831ae08745Sheppo DWARN(DBG_ALL_LDCS, 59841ae08745Sheppo "ldc_mem_dring_map: invalid dhandle\n"); 59851ae08745Sheppo return (EINVAL); 59861ae08745Sheppo } 59871ae08745Sheppo 59881ae08745Sheppo /* check to see if channel is initalized */ 59891ae08745Sheppo if (handle == NULL) { 59901ae08745Sheppo DWARN(DBG_ALL_LDCS, 59911ae08745Sheppo "ldc_mem_dring_map: invalid channel handle\n"); 59921ae08745Sheppo return (EINVAL); 59931ae08745Sheppo } 59941ae08745Sheppo ldcp = (ldc_chan_t *)handle; 59951ae08745Sheppo 59961ae08745Sheppo if (cookie == NULL) { 59971ae08745Sheppo DWARN(ldcp->id, 59981ae08745Sheppo "ldc_mem_dring_map: (0x%llx) invalid cookie\n", 59991ae08745Sheppo ldcp->id); 60001ae08745Sheppo return (EINVAL); 60011ae08745Sheppo } 60021ae08745Sheppo 60031ae08745Sheppo /* FUTURE: For now we support only one cookie per dring */ 60041ae08745Sheppo ASSERT(ccount == 1); 60051ae08745Sheppo 60061ae08745Sheppo if (cookie->size < (dsize * len)) { 60071ae08745Sheppo DWARN(ldcp->id, 60081ae08745Sheppo "ldc_mem_dring_map: (0x%llx) invalid dsize/len\n", 60091ae08745Sheppo ldcp->id); 60101ae08745Sheppo return (EINVAL); 60111ae08745Sheppo } 60121ae08745Sheppo 60131ae08745Sheppo *dhandle = 0; 60141ae08745Sheppo 60151ae08745Sheppo /* Allocate an dring structure */ 60161ae08745Sheppo dringp = kmem_zalloc(sizeof (ldc_dring_t), KM_SLEEP); 60171ae08745Sheppo 60181ae08745Sheppo D1(ldcp->id, 60191ae08745Sheppo "ldc_mem_dring_map: 0x%x,0x%x,0x%x,0x%llx,0x%llx\n", 60201ae08745Sheppo mtype, len, dsize, cookie->addr, cookie->size); 60211ae08745Sheppo 60221ae08745Sheppo /* Initialize dring */ 60231ae08745Sheppo dringp->length = len; 60241ae08745Sheppo dringp->dsize = dsize; 60251ae08745Sheppo 60261ae08745Sheppo /* round of to multiple of page size */ 60271ae08745Sheppo dring_size = len * dsize; 60281ae08745Sheppo dringp->size = (dring_size & MMU_PAGEMASK); 60291ae08745Sheppo if (dring_size & MMU_PAGEOFFSET) 60301ae08745Sheppo dringp->size += MMU_PAGESIZE; 60311ae08745Sheppo 60321ae08745Sheppo dringp->ldcp = ldcp; 60331ae08745Sheppo 60341ae08745Sheppo /* create an memory handle */ 60351ae08745Sheppo err = ldc_mem_alloc_handle(handle, &mhandle); 60361ae08745Sheppo if (err || mhandle == NULL) { 60371ae08745Sheppo DWARN(DBG_ALL_LDCS, 60381ae08745Sheppo "ldc_mem_dring_map: cannot alloc hdl err=%d\n", 60391ae08745Sheppo err); 60401ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 60411ae08745Sheppo return (ENOMEM); 60421ae08745Sheppo } 60431ae08745Sheppo 60441ae08745Sheppo dringp->mhdl = mhandle; 60451ae08745Sheppo dringp->base = NULL; 60461ae08745Sheppo 60471ae08745Sheppo /* map the dring into local memory */ 60484bac2208Snarayan err = ldc_mem_map(mhandle, cookie, ccount, mtype, LDC_MEM_RW, 60491ae08745Sheppo &(dringp->base), NULL); 60501ae08745Sheppo if (err || dringp->base == NULL) { 60511ae08745Sheppo cmn_err(CE_WARN, 60521ae08745Sheppo "ldc_mem_dring_map: cannot map desc ring err=%d\n", err); 60531ae08745Sheppo (void) ldc_mem_free_handle(mhandle); 60541ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 60551ae08745Sheppo return (ENOMEM); 60561ae08745Sheppo } 60571ae08745Sheppo 60581ae08745Sheppo /* initialize the desc ring lock */ 60591ae08745Sheppo mutex_init(&dringp->lock, NULL, MUTEX_DRIVER, NULL); 60601ae08745Sheppo 60611ae08745Sheppo /* Add descriptor ring to channel's imported dring list */ 60621ae08745Sheppo mutex_enter(&ldcp->imp_dlist_lock); 60631ae08745Sheppo dringp->ch_next = ldcp->imp_dring_list; 60641ae08745Sheppo ldcp->imp_dring_list = dringp; 60651ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 60661ae08745Sheppo 60671ae08745Sheppo dringp->status = LDC_MAPPED; 60681ae08745Sheppo 60691ae08745Sheppo *dhandle = (ldc_dring_handle_t)dringp; 60701ae08745Sheppo 60711ae08745Sheppo return (0); 60721ae08745Sheppo } 60731ae08745Sheppo 60741ae08745Sheppo /* 60751ae08745Sheppo * Unmap a descriptor ring. Free shadow memory (if any). 60761ae08745Sheppo */ 60771ae08745Sheppo int 60781ae08745Sheppo ldc_mem_dring_unmap(ldc_dring_handle_t dhandle) 60791ae08745Sheppo { 60801ae08745Sheppo ldc_dring_t *dringp; 60811ae08745Sheppo ldc_dring_t *tmp_dringp; 60821ae08745Sheppo ldc_chan_t *ldcp; 60831ae08745Sheppo 60841ae08745Sheppo if (dhandle == NULL) { 60851ae08745Sheppo DWARN(DBG_ALL_LDCS, 60861ae08745Sheppo "ldc_mem_dring_unmap: invalid desc ring handle\n"); 60871ae08745Sheppo return (EINVAL); 60881ae08745Sheppo } 60891ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 60901ae08745Sheppo 60911ae08745Sheppo if (dringp->status != LDC_MAPPED) { 60921ae08745Sheppo DWARN(DBG_ALL_LDCS, 60931ae08745Sheppo "ldc_mem_dring_unmap: not a mapped desc ring\n"); 60941ae08745Sheppo return (EINVAL); 60951ae08745Sheppo } 60961ae08745Sheppo 60971ae08745Sheppo mutex_enter(&dringp->lock); 60981ae08745Sheppo 60991ae08745Sheppo ldcp = dringp->ldcp; 61001ae08745Sheppo 61011ae08745Sheppo mutex_enter(&ldcp->imp_dlist_lock); 61021ae08745Sheppo 61031ae08745Sheppo /* find and unlink the desc ring from channel import list */ 61041ae08745Sheppo tmp_dringp = ldcp->imp_dring_list; 61051ae08745Sheppo if (tmp_dringp == dringp) { 61061ae08745Sheppo ldcp->imp_dring_list = dringp->ch_next; 61071ae08745Sheppo dringp->ch_next = NULL; 61081ae08745Sheppo 61091ae08745Sheppo } else { 61101ae08745Sheppo while (tmp_dringp != NULL) { 61111ae08745Sheppo if (tmp_dringp->ch_next == dringp) { 61121ae08745Sheppo tmp_dringp->ch_next = dringp->ch_next; 61131ae08745Sheppo dringp->ch_next = NULL; 61141ae08745Sheppo break; 61151ae08745Sheppo } 61161ae08745Sheppo tmp_dringp = tmp_dringp->ch_next; 61171ae08745Sheppo } 61181ae08745Sheppo if (tmp_dringp == NULL) { 61191ae08745Sheppo DWARN(DBG_ALL_LDCS, 61201ae08745Sheppo "ldc_mem_dring_unmap: invalid descriptor\n"); 61211ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 61221ae08745Sheppo mutex_exit(&dringp->lock); 61231ae08745Sheppo return (EINVAL); 61241ae08745Sheppo } 61251ae08745Sheppo } 61261ae08745Sheppo 61271ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 61281ae08745Sheppo 61291ae08745Sheppo /* do a LDC memory handle unmap and free */ 61301ae08745Sheppo (void) ldc_mem_unmap(dringp->mhdl); 61311ae08745Sheppo (void) ldc_mem_free_handle((ldc_mem_handle_t)dringp->mhdl); 61321ae08745Sheppo 61331ae08745Sheppo dringp->status = 0; 61341ae08745Sheppo dringp->ldcp = NULL; 61351ae08745Sheppo 61361ae08745Sheppo mutex_exit(&dringp->lock); 61371ae08745Sheppo 61381ae08745Sheppo /* destroy dring lock */ 61391ae08745Sheppo mutex_destroy(&dringp->lock); 61401ae08745Sheppo 61411ae08745Sheppo /* free desc ring object */ 61421ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 61431ae08745Sheppo 61441ae08745Sheppo return (0); 61451ae08745Sheppo } 61461ae08745Sheppo 61471ae08745Sheppo /* 61481ae08745Sheppo * Internal entry point for descriptor ring access entry consistency 61491ae08745Sheppo * semantics. Acquire copies the contents of the remote descriptor ring 61501ae08745Sheppo * into the local shadow copy. The release operation copies the local 61511ae08745Sheppo * contents into the remote dring. The start and end locations specify 61521ae08745Sheppo * bounds for the entries being synchronized. 61531ae08745Sheppo */ 61541ae08745Sheppo static int 61551ae08745Sheppo i_ldc_dring_acquire_release(ldc_dring_handle_t dhandle, 61561ae08745Sheppo uint8_t direction, uint64_t start, uint64_t end) 61571ae08745Sheppo { 61581ae08745Sheppo int err; 61591ae08745Sheppo ldc_dring_t *dringp; 61601ae08745Sheppo ldc_chan_t *ldcp; 61611ae08745Sheppo uint64_t soff; 61621ae08745Sheppo size_t copy_size; 61631ae08745Sheppo 61641ae08745Sheppo if (dhandle == NULL) { 61651ae08745Sheppo DWARN(DBG_ALL_LDCS, 61661ae08745Sheppo "i_ldc_dring_acquire_release: invalid desc ring handle\n"); 61671ae08745Sheppo return (EINVAL); 61681ae08745Sheppo } 61691ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 61701ae08745Sheppo mutex_enter(&dringp->lock); 61711ae08745Sheppo 61721ae08745Sheppo if (dringp->status != LDC_MAPPED || dringp->ldcp == NULL) { 61731ae08745Sheppo DWARN(DBG_ALL_LDCS, 61741ae08745Sheppo "i_ldc_dring_acquire_release: not a mapped desc ring\n"); 61751ae08745Sheppo mutex_exit(&dringp->lock); 61761ae08745Sheppo return (EINVAL); 61771ae08745Sheppo } 61781ae08745Sheppo 61791ae08745Sheppo if (start >= dringp->length || end >= dringp->length) { 61801ae08745Sheppo DWARN(DBG_ALL_LDCS, 61811ae08745Sheppo "i_ldc_dring_acquire_release: index out of range\n"); 61821ae08745Sheppo mutex_exit(&dringp->lock); 61831ae08745Sheppo return (EINVAL); 61841ae08745Sheppo } 61851ae08745Sheppo 61861ae08745Sheppo /* get the channel handle */ 61871ae08745Sheppo ldcp = dringp->ldcp; 61881ae08745Sheppo 61891ae08745Sheppo copy_size = (start <= end) ? (((end - start) + 1) * dringp->dsize) : 61901ae08745Sheppo ((dringp->length - start) * dringp->dsize); 61911ae08745Sheppo 61921ae08745Sheppo /* Calculate the relative offset for the first desc */ 61931ae08745Sheppo soff = (start * dringp->dsize); 61941ae08745Sheppo 61951ae08745Sheppo /* copy to/from remote from/to local memory */ 61961ae08745Sheppo D1(ldcp->id, "i_ldc_dring_acquire_release: c1 off=0x%llx sz=0x%llx\n", 61971ae08745Sheppo soff, copy_size); 61981ae08745Sheppo err = i_ldc_mem_acquire_release((ldc_mem_handle_t)dringp->mhdl, 61991ae08745Sheppo direction, soff, copy_size); 62001ae08745Sheppo if (err) { 62011ae08745Sheppo DWARN(ldcp->id, 62021ae08745Sheppo "i_ldc_dring_acquire_release: copy failed\n"); 62031ae08745Sheppo mutex_exit(&dringp->lock); 62041ae08745Sheppo return (err); 62051ae08745Sheppo } 62061ae08745Sheppo 62071ae08745Sheppo /* do the balance */ 62081ae08745Sheppo if (start > end) { 62091ae08745Sheppo copy_size = ((end + 1) * dringp->dsize); 62101ae08745Sheppo soff = 0; 62111ae08745Sheppo 62121ae08745Sheppo /* copy to/from remote from/to local memory */ 62131ae08745Sheppo D1(ldcp->id, "i_ldc_dring_acquire_release: c2 " 62141ae08745Sheppo "off=0x%llx sz=0x%llx\n", soff, copy_size); 62151ae08745Sheppo err = i_ldc_mem_acquire_release((ldc_mem_handle_t)dringp->mhdl, 62161ae08745Sheppo direction, soff, copy_size); 62171ae08745Sheppo if (err) { 62181ae08745Sheppo DWARN(ldcp->id, 62191ae08745Sheppo "i_ldc_dring_acquire_release: copy failed\n"); 62201ae08745Sheppo mutex_exit(&dringp->lock); 62211ae08745Sheppo return (err); 62221ae08745Sheppo } 62231ae08745Sheppo } 62241ae08745Sheppo 62251ae08745Sheppo mutex_exit(&dringp->lock); 62261ae08745Sheppo 62271ae08745Sheppo return (0); 62281ae08745Sheppo } 62291ae08745Sheppo 62301ae08745Sheppo /* 62311ae08745Sheppo * Ensure that the contents in the local dring are consistent 62321ae08745Sheppo * with the contents if of remote dring 62331ae08745Sheppo */ 62341ae08745Sheppo int 62351ae08745Sheppo ldc_mem_dring_acquire(ldc_dring_handle_t dhandle, uint64_t start, uint64_t end) 62361ae08745Sheppo { 62371ae08745Sheppo return (i_ldc_dring_acquire_release(dhandle, LDC_COPY_IN, start, end)); 62381ae08745Sheppo } 62391ae08745Sheppo 62401ae08745Sheppo /* 62411ae08745Sheppo * Ensure that the contents in the remote dring are consistent 62421ae08745Sheppo * with the contents if of local dring 62431ae08745Sheppo */ 62441ae08745Sheppo int 62451ae08745Sheppo ldc_mem_dring_release(ldc_dring_handle_t dhandle, uint64_t start, uint64_t end) 62461ae08745Sheppo { 62471ae08745Sheppo return (i_ldc_dring_acquire_release(dhandle, LDC_COPY_OUT, start, end)); 62481ae08745Sheppo } 62491ae08745Sheppo 62501ae08745Sheppo 62511ae08745Sheppo /* ------------------------------------------------------------------------- */ 6252