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); 743af08d82Slm66018 static int i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset); 75*a8ea4edeSnarayan static int i_ldc_rxq_drain(ldc_chan_t *ldcp); 761ae08745Sheppo static void i_ldc_reset_state(ldc_chan_t *ldcp); 773af08d82Slm66018 static void i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset); 781ae08745Sheppo 791ae08745Sheppo static int i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail); 801ae08745Sheppo static int i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail); 811ae08745Sheppo static int i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head); 821ae08745Sheppo static int i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype, 831ae08745Sheppo uint8_t ctrlmsg); 841ae08745Sheppo 851ae08745Sheppo /* Interrupt handling functions */ 861ae08745Sheppo static uint_t i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2); 871ae08745Sheppo static uint_t i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2); 881ae08745Sheppo static void i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype); 891ae08745Sheppo 901ae08745Sheppo /* Read method functions */ 911ae08745Sheppo static int i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep); 921ae08745Sheppo static int i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, 931ae08745Sheppo size_t *sizep); 941ae08745Sheppo static int i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, 951ae08745Sheppo size_t *sizep); 961ae08745Sheppo 971ae08745Sheppo /* Write method functions */ 981ae08745Sheppo static int i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t target_bufp, 991ae08745Sheppo size_t *sizep); 1001ae08745Sheppo static int i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t target_bufp, 1011ae08745Sheppo size_t *sizep); 1021ae08745Sheppo static int i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t target_bufp, 1031ae08745Sheppo size_t *sizep); 1041ae08745Sheppo 1051ae08745Sheppo /* Pkt processing internal functions */ 1061ae08745Sheppo static int i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg); 1071ae08745Sheppo static int i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg); 1081ae08745Sheppo static int i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg); 1091ae08745Sheppo static int i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg); 1101ae08745Sheppo static int i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg); 1111ae08745Sheppo static int i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg); 1121ae08745Sheppo static int i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg); 1131ae08745Sheppo 1141ae08745Sheppo /* Memory synchronization internal functions */ 1151ae08745Sheppo static int i_ldc_mem_acquire_release(ldc_mem_handle_t mhandle, 1161ae08745Sheppo uint8_t direction, uint64_t offset, size_t size); 1171ae08745Sheppo static int i_ldc_dring_acquire_release(ldc_dring_handle_t dhandle, 1181ae08745Sheppo uint8_t direction, uint64_t start, uint64_t end); 1191ae08745Sheppo 1201ae08745Sheppo /* LDC Version */ 1211ae08745Sheppo static ldc_ver_t ldc_versions[] = { {1, 0} }; 1221ae08745Sheppo 1231ae08745Sheppo /* number of supported versions */ 1241ae08745Sheppo #define LDC_NUM_VERS (sizeof (ldc_versions) / sizeof (ldc_versions[0])) 1251ae08745Sheppo 1261ae08745Sheppo /* Module State Pointer */ 1271ae08745Sheppo static ldc_soft_state_t *ldcssp; 1281ae08745Sheppo 1291ae08745Sheppo static struct modldrv md = { 1301ae08745Sheppo &mod_miscops, /* This is a misc module */ 1311ae08745Sheppo "sun4v LDC module v%I%", /* Name of the module */ 1321ae08745Sheppo }; 1331ae08745Sheppo 1341ae08745Sheppo static struct modlinkage ml = { 1351ae08745Sheppo MODREV_1, 1361ae08745Sheppo &md, 1371ae08745Sheppo NULL 1381ae08745Sheppo }; 1391ae08745Sheppo 1401ae08745Sheppo static uint64_t ldc_sup_minor; /* Supported minor number */ 1411ae08745Sheppo static hsvc_info_t ldc_hsvc = { 1421ae08745Sheppo HSVC_REV_1, NULL, HSVC_GROUP_LDC, 1, 0, "ldc" 1431ae08745Sheppo }; 1441ae08745Sheppo 1451ae08745Sheppo static uint64_t intr_sup_minor; /* Supported minor number */ 1461ae08745Sheppo static hsvc_info_t intr_hsvc = { 1471ae08745Sheppo HSVC_REV_1, NULL, HSVC_GROUP_INTR, 1, 0, "ldc" 1481ae08745Sheppo }; 1491ae08745Sheppo 1504bac2208Snarayan /* 1514bac2208Snarayan * LDC framework supports mapping remote domain's memory 1524bac2208Snarayan * either directly or via shadow memory pages. Default 1534bac2208Snarayan * support is currently implemented via shadow copy. 1544bac2208Snarayan * Direct map can be enabled by setting 'ldc_shmem_enabled' 1554bac2208Snarayan */ 1564bac2208Snarayan int ldc_shmem_enabled = 0; 157e1ebb9ecSlm66018 1580a55fbb7Slm66018 /* 159e1ebb9ecSlm66018 * The no. of MTU size messages that can be stored in 160e1ebb9ecSlm66018 * the LDC Tx queue. The number of Tx queue entries is 161e1ebb9ecSlm66018 * then computed as (mtu * mtu_msgs)/sizeof(queue_entry) 162e1ebb9ecSlm66018 */ 163e1ebb9ecSlm66018 uint64_t ldc_mtu_msgs = LDC_MTU_MSGS; 164e1ebb9ecSlm66018 165e1ebb9ecSlm66018 /* 166e1ebb9ecSlm66018 * The minimum queue length. This is the size of the smallest 167e1ebb9ecSlm66018 * LDC queue. If the computed value is less than this default, 168e1ebb9ecSlm66018 * the queue length is rounded up to 'ldc_queue_entries'. 169e1ebb9ecSlm66018 */ 170e1ebb9ecSlm66018 uint64_t ldc_queue_entries = LDC_QUEUE_ENTRIES; 171e1ebb9ecSlm66018 172e1ebb9ecSlm66018 /* 173e1ebb9ecSlm66018 * Pages exported for remote access over each channel is 174e1ebb9ecSlm66018 * maintained in a table registered with the Hypervisor. 175e1ebb9ecSlm66018 * The default number of entries in the table is set to 176e1ebb9ecSlm66018 * 'ldc_mtbl_entries'. 177e1ebb9ecSlm66018 */ 178e1ebb9ecSlm66018 uint64_t ldc_maptable_entries = LDC_MTBL_ENTRIES; 179e1ebb9ecSlm66018 180e1ebb9ecSlm66018 /* 181e1ebb9ecSlm66018 * LDC retry count and delay - when the HV returns EWOULDBLOCK 182e1ebb9ecSlm66018 * the operation is retried 'ldc_max_retries' times with a 183e1ebb9ecSlm66018 * wait of 'ldc_delay' usecs between each retry. 1840a55fbb7Slm66018 */ 1850a55fbb7Slm66018 int ldc_max_retries = LDC_MAX_RETRIES; 1860a55fbb7Slm66018 clock_t ldc_delay = LDC_DELAY; 1870a55fbb7Slm66018 1881ae08745Sheppo #ifdef DEBUG 1891ae08745Sheppo 1901ae08745Sheppo /* 1911ae08745Sheppo * Print debug messages 1921ae08745Sheppo * 1931ae08745Sheppo * set ldcdbg to 0x7 for enabling all msgs 1941ae08745Sheppo * 0x4 - Warnings 1951ae08745Sheppo * 0x2 - All debug messages 1961ae08745Sheppo * 0x1 - Minimal debug messages 1971ae08745Sheppo * 1981ae08745Sheppo * set ldcdbgchan to the channel number you want to debug 1991ae08745Sheppo * setting it to -1 prints debug messages for all channels 2001ae08745Sheppo * NOTE: ldcdbgchan has no effect on error messages 2011ae08745Sheppo */ 2021ae08745Sheppo 2031ae08745Sheppo #define DBG_ALL_LDCS -1 2041ae08745Sheppo 2051ae08745Sheppo int ldcdbg = 0x0; 2061ae08745Sheppo int64_t ldcdbgchan = DBG_ALL_LDCS; 2073af08d82Slm66018 boolean_t ldc_inject_reset_flag = B_FALSE; 2081ae08745Sheppo 2091ae08745Sheppo static void 2101ae08745Sheppo ldcdebug(int64_t id, const char *fmt, ...) 2111ae08745Sheppo { 2121ae08745Sheppo char buf[512]; 2131ae08745Sheppo va_list ap; 2141ae08745Sheppo 2151ae08745Sheppo /* 2161ae08745Sheppo * Do not return if, 2171ae08745Sheppo * caller wants to print it anyway - (id == DBG_ALL_LDCS) 2181ae08745Sheppo * debug channel is set to all LDCs - (ldcdbgchan == DBG_ALL_LDCS) 2191ae08745Sheppo * debug channel = caller specified channel 2201ae08745Sheppo */ 2211ae08745Sheppo if ((id != DBG_ALL_LDCS) && 2221ae08745Sheppo (ldcdbgchan != DBG_ALL_LDCS) && 2231ae08745Sheppo (ldcdbgchan != id)) { 2241ae08745Sheppo return; 2251ae08745Sheppo } 2261ae08745Sheppo 2271ae08745Sheppo va_start(ap, fmt); 2281ae08745Sheppo (void) vsprintf(buf, fmt, ap); 2291ae08745Sheppo va_end(ap); 2301ae08745Sheppo 2313af08d82Slm66018 cmn_err(CE_CONT, "?%s", buf); 2323af08d82Slm66018 } 2333af08d82Slm66018 2343af08d82Slm66018 static boolean_t 2353af08d82Slm66018 ldc_inject_reset(ldc_chan_t *ldcp) 2363af08d82Slm66018 { 2373af08d82Slm66018 if ((ldcdbgchan != DBG_ALL_LDCS) && (ldcdbgchan != ldcp->id)) 2383af08d82Slm66018 return (B_FALSE); 2393af08d82Slm66018 2403af08d82Slm66018 if (!ldc_inject_reset_flag) 2413af08d82Slm66018 return (B_FALSE); 2423af08d82Slm66018 2433af08d82Slm66018 /* clear the injection state */ 2443af08d82Slm66018 ldc_inject_reset_flag = 0; 2453af08d82Slm66018 2463af08d82Slm66018 return (B_TRUE); 2471ae08745Sheppo } 2481ae08745Sheppo 2491ae08745Sheppo #define D1 \ 2501ae08745Sheppo if (ldcdbg & 0x01) \ 2511ae08745Sheppo ldcdebug 2521ae08745Sheppo 2531ae08745Sheppo #define D2 \ 2541ae08745Sheppo if (ldcdbg & 0x02) \ 2551ae08745Sheppo ldcdebug 2561ae08745Sheppo 2571ae08745Sheppo #define DWARN \ 2581ae08745Sheppo if (ldcdbg & 0x04) \ 2591ae08745Sheppo ldcdebug 2601ae08745Sheppo 2611ae08745Sheppo #define DUMP_PAYLOAD(id, addr) \ 2621ae08745Sheppo { \ 2631ae08745Sheppo char buf[65*3]; \ 2641ae08745Sheppo int i; \ 2651ae08745Sheppo uint8_t *src = (uint8_t *)addr; \ 2661ae08745Sheppo for (i = 0; i < 64; i++, src++) \ 2671ae08745Sheppo (void) sprintf(&buf[i * 3], "|%02x", *src); \ 2681ae08745Sheppo (void) sprintf(&buf[i * 3], "|\n"); \ 2691ae08745Sheppo D2((id), "payload: %s", buf); \ 2701ae08745Sheppo } 2711ae08745Sheppo 2721ae08745Sheppo #define DUMP_LDC_PKT(c, s, addr) \ 2731ae08745Sheppo { \ 2741ae08745Sheppo ldc_msg_t *msg = (ldc_msg_t *)(addr); \ 2751ae08745Sheppo uint32_t mid = ((c)->mode != LDC_MODE_RAW) ? msg->seqid : 0; \ 2761ae08745Sheppo if (msg->type == LDC_DATA) { \ 2771ae08745Sheppo D2((c)->id, "%s: msg%d (/%x/%x/%x/,env[%c%c,sz=%d])", \ 2781ae08745Sheppo (s), mid, msg->type, msg->stype, msg->ctrl, \ 2791ae08745Sheppo (msg->env & LDC_FRAG_START) ? 'B' : ' ', \ 2801ae08745Sheppo (msg->env & LDC_FRAG_STOP) ? 'E' : ' ', \ 2811ae08745Sheppo (msg->env & LDC_LEN_MASK)); \ 2821ae08745Sheppo } else { \ 2831ae08745Sheppo D2((c)->id, "%s: msg%d (/%x/%x/%x/,env=%x)", (s), \ 2841ae08745Sheppo mid, msg->type, msg->stype, msg->ctrl, msg->env); \ 2851ae08745Sheppo } \ 2861ae08745Sheppo } 2871ae08745Sheppo 2883af08d82Slm66018 #define LDC_INJECT_RESET(_ldcp) ldc_inject_reset(_ldcp) 2893af08d82Slm66018 2901ae08745Sheppo #else 2911ae08745Sheppo 2921ae08745Sheppo #define DBG_ALL_LDCS -1 2931ae08745Sheppo 2941ae08745Sheppo #define D1 2951ae08745Sheppo #define D2 2961ae08745Sheppo #define DWARN 2971ae08745Sheppo 2981ae08745Sheppo #define DUMP_PAYLOAD(id, addr) 2991ae08745Sheppo #define DUMP_LDC_PKT(c, s, addr) 3001ae08745Sheppo 3013af08d82Slm66018 #define LDC_INJECT_RESET(_ldcp) (B_FALSE) 3023af08d82Slm66018 3031ae08745Sheppo #endif 3041ae08745Sheppo 3051ae08745Sheppo #define ZERO_PKT(p) \ 3061ae08745Sheppo bzero((p), sizeof (ldc_msg_t)); 3071ae08745Sheppo 3081ae08745Sheppo #define IDX2COOKIE(idx, pg_szc, pg_shift) \ 3091ae08745Sheppo (((pg_szc) << LDC_COOKIE_PGSZC_SHIFT) | ((idx) << (pg_shift))) 3101ae08745Sheppo 3111ae08745Sheppo 3121ae08745Sheppo int 3131ae08745Sheppo _init(void) 3141ae08745Sheppo { 3151ae08745Sheppo int status; 3161ae08745Sheppo 3171ae08745Sheppo status = hsvc_register(&ldc_hsvc, &ldc_sup_minor); 3181ae08745Sheppo if (status != 0) { 3191ae08745Sheppo cmn_err(CE_WARN, "%s: cannot negotiate hypervisor LDC services" 3201ae08745Sheppo " group: 0x%lx major: %ld minor: %ld errno: %d", 3211ae08745Sheppo ldc_hsvc.hsvc_modname, ldc_hsvc.hsvc_group, 3221ae08745Sheppo ldc_hsvc.hsvc_major, ldc_hsvc.hsvc_minor, status); 3231ae08745Sheppo return (-1); 3241ae08745Sheppo } 3251ae08745Sheppo 3261ae08745Sheppo status = hsvc_register(&intr_hsvc, &intr_sup_minor); 3271ae08745Sheppo if (status != 0) { 3281ae08745Sheppo cmn_err(CE_WARN, "%s: cannot negotiate hypervisor interrupt " 3291ae08745Sheppo "services group: 0x%lx major: %ld minor: %ld errno: %d", 3301ae08745Sheppo intr_hsvc.hsvc_modname, intr_hsvc.hsvc_group, 3311ae08745Sheppo intr_hsvc.hsvc_major, intr_hsvc.hsvc_minor, status); 3321ae08745Sheppo (void) hsvc_unregister(&ldc_hsvc); 3331ae08745Sheppo return (-1); 3341ae08745Sheppo } 3351ae08745Sheppo 3361ae08745Sheppo /* allocate soft state structure */ 3371ae08745Sheppo ldcssp = kmem_zalloc(sizeof (ldc_soft_state_t), KM_SLEEP); 3381ae08745Sheppo 3391ae08745Sheppo /* Link the module into the system */ 3401ae08745Sheppo status = mod_install(&ml); 3411ae08745Sheppo if (status != 0) { 3421ae08745Sheppo kmem_free(ldcssp, sizeof (ldc_soft_state_t)); 3431ae08745Sheppo return (status); 3441ae08745Sheppo } 3451ae08745Sheppo 3461ae08745Sheppo /* Initialize the LDC state structure */ 3471ae08745Sheppo mutex_init(&ldcssp->lock, NULL, MUTEX_DRIVER, NULL); 3481ae08745Sheppo 3491ae08745Sheppo mutex_enter(&ldcssp->lock); 3501ae08745Sheppo 3514bac2208Snarayan /* Create a cache for memory handles */ 3524bac2208Snarayan ldcssp->memhdl_cache = kmem_cache_create("ldc_memhdl_cache", 3534bac2208Snarayan sizeof (ldc_mhdl_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 3544bac2208Snarayan if (ldcssp->memhdl_cache == NULL) { 3554bac2208Snarayan DWARN(DBG_ALL_LDCS, "_init: ldc_memhdl cache create failed\n"); 3564bac2208Snarayan mutex_exit(&ldcssp->lock); 3574bac2208Snarayan return (-1); 3584bac2208Snarayan } 3594bac2208Snarayan 3604bac2208Snarayan /* Create cache for memory segment structures */ 3614bac2208Snarayan ldcssp->memseg_cache = kmem_cache_create("ldc_memseg_cache", 3624bac2208Snarayan sizeof (ldc_memseg_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 3634bac2208Snarayan if (ldcssp->memseg_cache == NULL) { 3644bac2208Snarayan DWARN(DBG_ALL_LDCS, "_init: ldc_memseg cache create failed\n"); 3654bac2208Snarayan mutex_exit(&ldcssp->lock); 3664bac2208Snarayan return (-1); 3674bac2208Snarayan } 3684bac2208Snarayan 3694bac2208Snarayan 3701ae08745Sheppo ldcssp->channel_count = 0; 3711ae08745Sheppo ldcssp->channels_open = 0; 3721ae08745Sheppo ldcssp->chan_list = NULL; 3731ae08745Sheppo ldcssp->dring_list = NULL; 3741ae08745Sheppo 3751ae08745Sheppo mutex_exit(&ldcssp->lock); 3761ae08745Sheppo 3771ae08745Sheppo return (0); 3781ae08745Sheppo } 3791ae08745Sheppo 3801ae08745Sheppo int 3811ae08745Sheppo _info(struct modinfo *modinfop) 3821ae08745Sheppo { 3831ae08745Sheppo /* Report status of the dynamically loadable driver module */ 3841ae08745Sheppo return (mod_info(&ml, modinfop)); 3851ae08745Sheppo } 3861ae08745Sheppo 3871ae08745Sheppo int 3881ae08745Sheppo _fini(void) 3891ae08745Sheppo { 3901ae08745Sheppo int rv, status; 3911ae08745Sheppo ldc_chan_t *ldcp; 3921ae08745Sheppo ldc_dring_t *dringp; 3931ae08745Sheppo ldc_mem_info_t minfo; 3941ae08745Sheppo 3951ae08745Sheppo /* Unlink the driver module from the system */ 3961ae08745Sheppo status = mod_remove(&ml); 3971ae08745Sheppo if (status) { 3981ae08745Sheppo DWARN(DBG_ALL_LDCS, "_fini: mod_remove failed\n"); 3991ae08745Sheppo return (EIO); 4001ae08745Sheppo } 4011ae08745Sheppo 4021ae08745Sheppo /* close and finalize channels */ 4031ae08745Sheppo ldcp = ldcssp->chan_list; 4041ae08745Sheppo while (ldcp != NULL) { 4051ae08745Sheppo (void) ldc_close((ldc_handle_t)ldcp); 4061ae08745Sheppo (void) ldc_fini((ldc_handle_t)ldcp); 4071ae08745Sheppo 4081ae08745Sheppo ldcp = ldcp->next; 4091ae08745Sheppo } 4101ae08745Sheppo 4111ae08745Sheppo /* Free descriptor rings */ 4121ae08745Sheppo dringp = ldcssp->dring_list; 4131ae08745Sheppo while (dringp != NULL) { 4141ae08745Sheppo dringp = dringp->next; 4151ae08745Sheppo 4161ae08745Sheppo rv = ldc_mem_dring_info((ldc_dring_handle_t)dringp, &minfo); 4171ae08745Sheppo if (rv == 0 && minfo.status != LDC_UNBOUND) { 4181ae08745Sheppo if (minfo.status == LDC_BOUND) { 4191ae08745Sheppo (void) ldc_mem_dring_unbind( 4201ae08745Sheppo (ldc_dring_handle_t)dringp); 4211ae08745Sheppo } 4221ae08745Sheppo if (minfo.status == LDC_MAPPED) { 4231ae08745Sheppo (void) ldc_mem_dring_unmap( 4241ae08745Sheppo (ldc_dring_handle_t)dringp); 4251ae08745Sheppo } 4261ae08745Sheppo } 4271ae08745Sheppo 4281ae08745Sheppo (void) ldc_mem_dring_destroy((ldc_dring_handle_t)dringp); 4291ae08745Sheppo } 4301ae08745Sheppo ldcssp->dring_list = NULL; 4311ae08745Sheppo 4324bac2208Snarayan /* Destroy kmem caches */ 4334bac2208Snarayan kmem_cache_destroy(ldcssp->memhdl_cache); 4344bac2208Snarayan kmem_cache_destroy(ldcssp->memseg_cache); 4354bac2208Snarayan 4361ae08745Sheppo /* 4371ae08745Sheppo * We have successfully "removed" the driver. 4381ae08745Sheppo * Destroying soft states 4391ae08745Sheppo */ 4401ae08745Sheppo mutex_destroy(&ldcssp->lock); 4411ae08745Sheppo kmem_free(ldcssp, sizeof (ldc_soft_state_t)); 4421ae08745Sheppo 4431ae08745Sheppo (void) hsvc_unregister(&ldc_hsvc); 4441ae08745Sheppo (void) hsvc_unregister(&intr_hsvc); 4451ae08745Sheppo 4461ae08745Sheppo return (status); 4471ae08745Sheppo } 4481ae08745Sheppo 4491ae08745Sheppo /* -------------------------------------------------------------------------- */ 4501ae08745Sheppo 4511ae08745Sheppo /* 452e1ebb9ecSlm66018 * LDC Link Layer Internal Functions 4531ae08745Sheppo */ 4541ae08745Sheppo 4551ae08745Sheppo /* 4561ae08745Sheppo * Translate HV Errors to sun4v error codes 4571ae08745Sheppo */ 4581ae08745Sheppo static int 4591ae08745Sheppo i_ldc_h2v_error(int h_error) 4601ae08745Sheppo { 4611ae08745Sheppo switch (h_error) { 4621ae08745Sheppo 4631ae08745Sheppo case H_EOK: 4641ae08745Sheppo return (0); 4651ae08745Sheppo 4661ae08745Sheppo case H_ENORADDR: 4671ae08745Sheppo return (EFAULT); 4681ae08745Sheppo 4691ae08745Sheppo case H_EBADPGSZ: 4701ae08745Sheppo case H_EINVAL: 4711ae08745Sheppo return (EINVAL); 4721ae08745Sheppo 4731ae08745Sheppo case H_EWOULDBLOCK: 4741ae08745Sheppo return (EWOULDBLOCK); 4751ae08745Sheppo 4761ae08745Sheppo case H_ENOACCESS: 4771ae08745Sheppo case H_ENOMAP: 4781ae08745Sheppo return (EACCES); 4791ae08745Sheppo 4801ae08745Sheppo case H_EIO: 4811ae08745Sheppo case H_ECPUERROR: 4821ae08745Sheppo return (EIO); 4831ae08745Sheppo 4841ae08745Sheppo case H_ENOTSUPPORTED: 4851ae08745Sheppo return (ENOTSUP); 4861ae08745Sheppo 4871ae08745Sheppo case H_ETOOMANY: 4881ae08745Sheppo return (ENOSPC); 4891ae08745Sheppo 4901ae08745Sheppo case H_ECHANNEL: 4911ae08745Sheppo return (ECHRNG); 4921ae08745Sheppo default: 4931ae08745Sheppo break; 4941ae08745Sheppo } 4951ae08745Sheppo 4961ae08745Sheppo return (EIO); 4971ae08745Sheppo } 4981ae08745Sheppo 4991ae08745Sheppo /* 5001ae08745Sheppo * Reconfigure the transmit queue 5011ae08745Sheppo */ 5021ae08745Sheppo static int 5031ae08745Sheppo i_ldc_txq_reconf(ldc_chan_t *ldcp) 5041ae08745Sheppo { 5051ae08745Sheppo int rv; 5061ae08745Sheppo 5071ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 508d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 509d10e4ef2Snarayan 5101ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 5111ae08745Sheppo if (rv) { 5121ae08745Sheppo cmn_err(CE_WARN, 5133af08d82Slm66018 "i_ldc_txq_reconf: (0x%lx) cannot set qconf", ldcp->id); 5141ae08745Sheppo return (EIO); 5151ae08745Sheppo } 5161ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &(ldcp->tx_head), 5171ae08745Sheppo &(ldcp->tx_tail), &(ldcp->link_state)); 5181ae08745Sheppo if (rv) { 5191ae08745Sheppo cmn_err(CE_WARN, 5203af08d82Slm66018 "i_ldc_txq_reconf: (0x%lx) cannot get qptrs", ldcp->id); 5211ae08745Sheppo return (EIO); 5221ae08745Sheppo } 5233af08d82Slm66018 D1(ldcp->id, "i_ldc_txq_reconf: (0x%llx) h=0x%llx,t=0x%llx," 5241ae08745Sheppo "s=0x%llx\n", ldcp->id, ldcp->tx_head, ldcp->tx_tail, 5251ae08745Sheppo ldcp->link_state); 5261ae08745Sheppo 5271ae08745Sheppo return (0); 5281ae08745Sheppo } 5291ae08745Sheppo 5301ae08745Sheppo /* 5311ae08745Sheppo * Reconfigure the receive queue 5321ae08745Sheppo */ 5331ae08745Sheppo static int 5343af08d82Slm66018 i_ldc_rxq_reconf(ldc_chan_t *ldcp, boolean_t force_reset) 5351ae08745Sheppo { 5361ae08745Sheppo int rv; 5371ae08745Sheppo uint64_t rx_head, rx_tail; 5381ae08745Sheppo 5391ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 5401ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 5411ae08745Sheppo &(ldcp->link_state)); 5421ae08745Sheppo if (rv) { 5431ae08745Sheppo cmn_err(CE_WARN, 5443af08d82Slm66018 "i_ldc_rxq_reconf: (0x%lx) cannot get state", 5451ae08745Sheppo ldcp->id); 5461ae08745Sheppo return (EIO); 5471ae08745Sheppo } 5481ae08745Sheppo 5493af08d82Slm66018 if (force_reset || (ldcp->tstate & ~TS_IN_RESET) == TS_UP) { 5501ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, 5511ae08745Sheppo ldcp->rx_q_entries); 5521ae08745Sheppo if (rv) { 5531ae08745Sheppo cmn_err(CE_WARN, 5543af08d82Slm66018 "i_ldc_rxq_reconf: (0x%lx) cannot set qconf", 5551ae08745Sheppo ldcp->id); 5561ae08745Sheppo return (EIO); 5571ae08745Sheppo } 5583af08d82Slm66018 D1(ldcp->id, "i_ldc_rxq_reconf: (0x%llx) completed q reconf", 5591ae08745Sheppo ldcp->id); 5601ae08745Sheppo } 5611ae08745Sheppo 5621ae08745Sheppo return (0); 5631ae08745Sheppo } 5641ae08745Sheppo 565*a8ea4edeSnarayan 566*a8ea4edeSnarayan /* 567*a8ea4edeSnarayan * Drain the contents of the receive queue 568*a8ea4edeSnarayan */ 569*a8ea4edeSnarayan static int 570*a8ea4edeSnarayan i_ldc_rxq_drain(ldc_chan_t *ldcp) 571*a8ea4edeSnarayan { 572*a8ea4edeSnarayan int rv; 573*a8ea4edeSnarayan uint64_t rx_head, rx_tail; 574*a8ea4edeSnarayan 575*a8ea4edeSnarayan ASSERT(MUTEX_HELD(&ldcp->lock)); 576*a8ea4edeSnarayan rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 577*a8ea4edeSnarayan &(ldcp->link_state)); 578*a8ea4edeSnarayan if (rv) { 579*a8ea4edeSnarayan cmn_err(CE_WARN, "i_ldc_rxq_drain: (0x%lx) cannot get state", 580*a8ea4edeSnarayan ldcp->id); 581*a8ea4edeSnarayan return (EIO); 582*a8ea4edeSnarayan } 583*a8ea4edeSnarayan 584*a8ea4edeSnarayan /* flush contents by setting the head = tail */ 585*a8ea4edeSnarayan return (i_ldc_set_rx_head(ldcp, rx_tail)); 586*a8ea4edeSnarayan } 587*a8ea4edeSnarayan 588*a8ea4edeSnarayan 5891ae08745Sheppo /* 5901ae08745Sheppo * Reset LDC state structure and its contents 5911ae08745Sheppo */ 5921ae08745Sheppo static void 5931ae08745Sheppo i_ldc_reset_state(ldc_chan_t *ldcp) 5941ae08745Sheppo { 5951ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 5961ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 5971ae08745Sheppo ldcp->last_ack_rcd = 0; 5981ae08745Sheppo ldcp->last_msg_rcd = 0; 5991ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 6001ae08745Sheppo ldcp->next_vidx = 0; 6011ae08745Sheppo ldcp->hstate = 0; 6021ae08745Sheppo ldcp->tstate = TS_OPEN; 6031ae08745Sheppo ldcp->status = LDC_OPEN; 6041ae08745Sheppo 6051ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 6061ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 6071ae08745Sheppo 6081ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 6091ae08745Sheppo ldcp->status = LDC_UP; 6101ae08745Sheppo ldcp->tstate = TS_UP; 6111ae08745Sheppo } else { 6121ae08745Sheppo ldcp->status = LDC_READY; 6131ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 6141ae08745Sheppo } 6151ae08745Sheppo } 6161ae08745Sheppo } 6171ae08745Sheppo 6181ae08745Sheppo /* 6191ae08745Sheppo * Reset a LDC channel 6201ae08745Sheppo */ 6211ae08745Sheppo static void 6223af08d82Slm66018 i_ldc_reset(ldc_chan_t *ldcp, boolean_t force_reset) 6231ae08745Sheppo { 6243af08d82Slm66018 DWARN(ldcp->id, "i_ldc_reset: (0x%llx) channel reset\n", ldcp->id); 6251ae08745Sheppo 626d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->lock)); 627d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 628d10e4ef2Snarayan 6293af08d82Slm66018 /* reconfig Tx and Rx queues */ 6301ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 6313af08d82Slm66018 (void) i_ldc_rxq_reconf(ldcp, force_reset); 6323af08d82Slm66018 6333af08d82Slm66018 /* Clear Tx and Rx interrupts */ 6343af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 6353af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 6363af08d82Slm66018 6373af08d82Slm66018 /* Reset channel state */ 6381ae08745Sheppo i_ldc_reset_state(ldcp); 6393af08d82Slm66018 6403af08d82Slm66018 /* Mark channel in reset */ 6413af08d82Slm66018 ldcp->tstate |= TS_IN_RESET; 6421ae08745Sheppo } 6431ae08745Sheppo 6444bac2208Snarayan 6451ae08745Sheppo /* 6461ae08745Sheppo * Clear pending interrupts 6471ae08745Sheppo */ 6481ae08745Sheppo static void 6491ae08745Sheppo i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype) 6501ae08745Sheppo { 6511ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 6521ae08745Sheppo 6531ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 6543af08d82Slm66018 ASSERT(cinfo->dip != NULL); 6554bac2208Snarayan 6563af08d82Slm66018 switch (itype) { 6573af08d82Slm66018 case CNEX_TX_INTR: 6584bac2208Snarayan /* check Tx interrupt */ 6593af08d82Slm66018 if (ldcp->tx_intr_state) 6603af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_NONE; 6614bac2208Snarayan else 6624bac2208Snarayan return; 6633af08d82Slm66018 break; 6643af08d82Slm66018 6653af08d82Slm66018 case CNEX_RX_INTR: 6664bac2208Snarayan /* check Rx interrupt */ 6673af08d82Slm66018 if (ldcp->rx_intr_state) 6683af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_NONE; 6694bac2208Snarayan else 6704bac2208Snarayan return; 6713af08d82Slm66018 break; 6724bac2208Snarayan } 6734bac2208Snarayan 6741ae08745Sheppo (void) cinfo->clr_intr(cinfo->dip, ldcp->id, itype); 6754bac2208Snarayan D2(ldcp->id, 6764bac2208Snarayan "i_ldc_clear_intr: (0x%llx) cleared 0x%x intr\n", 6774bac2208Snarayan ldcp->id, itype); 6781ae08745Sheppo } 6791ae08745Sheppo 6801ae08745Sheppo /* 6811ae08745Sheppo * Set the receive queue head 6820a55fbb7Slm66018 * Resets connection and returns an error if it fails. 6831ae08745Sheppo */ 6841ae08745Sheppo static int 6851ae08745Sheppo i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head) 6861ae08745Sheppo { 6871ae08745Sheppo int rv; 6880a55fbb7Slm66018 int retries; 6891ae08745Sheppo 6901ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 6910a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 6920a55fbb7Slm66018 6930a55fbb7Slm66018 if ((rv = hv_ldc_rx_set_qhead(ldcp->id, head)) == 0) 6940a55fbb7Slm66018 return (0); 6950a55fbb7Slm66018 6960a55fbb7Slm66018 if (rv != H_EWOULDBLOCK) 6970a55fbb7Slm66018 break; 6980a55fbb7Slm66018 6990a55fbb7Slm66018 /* wait for ldc_delay usecs */ 7000a55fbb7Slm66018 drv_usecwait(ldc_delay); 7011ae08745Sheppo } 7021ae08745Sheppo 7030a55fbb7Slm66018 cmn_err(CE_WARN, "ldc_rx_set_qhead: (0x%lx) cannot set qhead 0x%lx", 7040a55fbb7Slm66018 ldcp->id, head); 705d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 7063af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 707d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 7080a55fbb7Slm66018 7090a55fbb7Slm66018 return (ECONNRESET); 7101ae08745Sheppo } 7111ae08745Sheppo 7121ae08745Sheppo 7131ae08745Sheppo /* 7141ae08745Sheppo * Returns the tx_tail to be used for transfer 7151ae08745Sheppo * Re-reads the TX queue ptrs if and only if the 7161ae08745Sheppo * the cached head and tail are equal (queue is full) 7171ae08745Sheppo */ 7181ae08745Sheppo static int 7191ae08745Sheppo i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail) 7201ae08745Sheppo { 7211ae08745Sheppo int rv; 7221ae08745Sheppo uint64_t current_head, new_tail; 7231ae08745Sheppo 724d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 7251ae08745Sheppo /* Read the head and tail ptrs from HV */ 7261ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 7271ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 7281ae08745Sheppo if (rv) { 7291ae08745Sheppo cmn_err(CE_WARN, 7301ae08745Sheppo "i_ldc_get_tx_tail: (0x%lx) cannot read qptrs\n", 7311ae08745Sheppo ldcp->id); 7321ae08745Sheppo return (EIO); 7331ae08745Sheppo } 7341ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 7351ae08745Sheppo DWARN(DBG_ALL_LDCS, 7361ae08745Sheppo "i_ldc_get_tx_tail: (0x%llx) channel not ready\n", 7371ae08745Sheppo ldcp->id); 7381ae08745Sheppo return (ECONNRESET); 7391ae08745Sheppo } 7401ae08745Sheppo 7411ae08745Sheppo /* In reliable mode, check against last ACKd msg */ 7421ae08745Sheppo current_head = (ldcp->mode == LDC_MODE_RELIABLE || 7431ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 7441ae08745Sheppo ? ldcp->tx_ackd_head : ldcp->tx_head; 7451ae08745Sheppo 7461ae08745Sheppo /* increment the tail */ 7471ae08745Sheppo new_tail = (ldcp->tx_tail + LDC_PACKET_SIZE) % 7481ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 7491ae08745Sheppo 7501ae08745Sheppo if (new_tail == current_head) { 7511ae08745Sheppo DWARN(ldcp->id, 7521ae08745Sheppo "i_ldc_get_tx_tail: (0x%llx) TX queue is full\n", 7531ae08745Sheppo ldcp->id); 7541ae08745Sheppo return (EWOULDBLOCK); 7551ae08745Sheppo } 7561ae08745Sheppo 7571ae08745Sheppo D2(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) head=0x%llx, tail=0x%llx\n", 7581ae08745Sheppo ldcp->id, ldcp->tx_head, ldcp->tx_tail); 7591ae08745Sheppo 7601ae08745Sheppo *tail = ldcp->tx_tail; 7611ae08745Sheppo return (0); 7621ae08745Sheppo } 7631ae08745Sheppo 7641ae08745Sheppo /* 7651ae08745Sheppo * Set the tail pointer. If HV returns EWOULDBLOCK, it will back off 7660a55fbb7Slm66018 * and retry ldc_max_retries times before returning an error. 7671ae08745Sheppo * Returns 0, EWOULDBLOCK or EIO 7681ae08745Sheppo */ 7691ae08745Sheppo static int 7701ae08745Sheppo i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail) 7711ae08745Sheppo { 7721ae08745Sheppo int rv, retval = EWOULDBLOCK; 7730a55fbb7Slm66018 int retries; 7741ae08745Sheppo 775d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 7760a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 7771ae08745Sheppo 7781ae08745Sheppo if ((rv = hv_ldc_tx_set_qtail(ldcp->id, tail)) == 0) { 7791ae08745Sheppo retval = 0; 7801ae08745Sheppo break; 7811ae08745Sheppo } 7821ae08745Sheppo if (rv != H_EWOULDBLOCK) { 7831ae08745Sheppo DWARN(ldcp->id, "i_ldc_set_tx_tail: (0x%llx) set " 7841ae08745Sheppo "qtail=0x%llx failed, rv=%d\n", ldcp->id, tail, rv); 7851ae08745Sheppo retval = EIO; 7861ae08745Sheppo break; 7871ae08745Sheppo } 7881ae08745Sheppo 7890a55fbb7Slm66018 /* wait for ldc_delay usecs */ 7900a55fbb7Slm66018 drv_usecwait(ldc_delay); 7911ae08745Sheppo } 7921ae08745Sheppo return (retval); 7931ae08745Sheppo } 7941ae08745Sheppo 7951ae08745Sheppo /* 7961ae08745Sheppo * Send a LDC message 7971ae08745Sheppo */ 7981ae08745Sheppo static int 7991ae08745Sheppo i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype, 8001ae08745Sheppo uint8_t ctrlmsg) 8011ae08745Sheppo { 8021ae08745Sheppo int rv; 8031ae08745Sheppo ldc_msg_t *pkt; 8041ae08745Sheppo uint64_t tx_tail; 8051ae08745Sheppo uint32_t curr_seqid = ldcp->last_msg_snt; 8061ae08745Sheppo 807d10e4ef2Snarayan /* Obtain Tx lock */ 808d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 809d10e4ef2Snarayan 8101ae08745Sheppo /* get the current tail for the message */ 8111ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 8121ae08745Sheppo if (rv) { 8131ae08745Sheppo DWARN(ldcp->id, 8141ae08745Sheppo "i_ldc_send_pkt: (0x%llx) error sending pkt, " 8151ae08745Sheppo "type=0x%x,subtype=0x%x,ctrl=0x%x\n", 8161ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 817d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 8181ae08745Sheppo return (rv); 8191ae08745Sheppo } 8201ae08745Sheppo 8211ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 8221ae08745Sheppo ZERO_PKT(pkt); 8231ae08745Sheppo 8241ae08745Sheppo /* Initialize the packet */ 8251ae08745Sheppo pkt->type = pkttype; 8261ae08745Sheppo pkt->stype = subtype; 8271ae08745Sheppo pkt->ctrl = ctrlmsg; 8281ae08745Sheppo 8291ae08745Sheppo /* Store ackid/seqid iff it is RELIABLE mode & not a RTS/RTR message */ 8301ae08745Sheppo if (((ctrlmsg & LDC_CTRL_MASK) != LDC_RTS) && 8311ae08745Sheppo ((ctrlmsg & LDC_CTRL_MASK) != LDC_RTR)) { 8321ae08745Sheppo curr_seqid++; 8331ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) { 8341ae08745Sheppo pkt->seqid = curr_seqid; 8351ae08745Sheppo pkt->ackid = ldcp->last_msg_rcd; 8361ae08745Sheppo } 8371ae08745Sheppo } 8381ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_send_pkt", (uint64_t)pkt); 8391ae08745Sheppo 8401ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 8411ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 8421ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 8431ae08745Sheppo 8441ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 8451ae08745Sheppo if (rv) { 8461ae08745Sheppo DWARN(ldcp->id, 8471ae08745Sheppo "i_ldc_send_pkt:(0x%llx) error sending pkt, " 8481ae08745Sheppo "type=0x%x,stype=0x%x,ctrl=0x%x\n", 8491ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 850d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 8511ae08745Sheppo return (EIO); 8521ae08745Sheppo } 8531ae08745Sheppo 8541ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 8551ae08745Sheppo ldcp->tx_tail = tx_tail; 8561ae08745Sheppo 857d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 8581ae08745Sheppo return (0); 8591ae08745Sheppo } 8601ae08745Sheppo 8611ae08745Sheppo /* 8621ae08745Sheppo * Checks if packet was received in right order 863e1ebb9ecSlm66018 * in the case of a reliable link. 8641ae08745Sheppo * Returns 0 if in order, else EIO 8651ae08745Sheppo */ 8661ae08745Sheppo static int 8671ae08745Sheppo i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *msg) 8681ae08745Sheppo { 8691ae08745Sheppo /* No seqid checking for RAW mode */ 8701ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) 8711ae08745Sheppo return (0); 8721ae08745Sheppo 8731ae08745Sheppo /* No seqid checking for version, RTS, RTR message */ 8741ae08745Sheppo if (msg->ctrl == LDC_VER || 8751ae08745Sheppo msg->ctrl == LDC_RTS || 8761ae08745Sheppo msg->ctrl == LDC_RTR) 8771ae08745Sheppo return (0); 8781ae08745Sheppo 8791ae08745Sheppo /* Initial seqid to use is sent in RTS/RTR and saved in last_msg_rcd */ 8801ae08745Sheppo if (msg->seqid != (ldcp->last_msg_rcd + 1)) { 8811ae08745Sheppo DWARN(ldcp->id, 8821ae08745Sheppo "i_ldc_check_seqid: (0x%llx) out-of-order pkt, got 0x%x, " 8831ae08745Sheppo "expecting 0x%x\n", ldcp->id, msg->seqid, 8841ae08745Sheppo (ldcp->last_msg_rcd + 1)); 8851ae08745Sheppo return (EIO); 8861ae08745Sheppo } 8871ae08745Sheppo 8881ae08745Sheppo return (0); 8891ae08745Sheppo } 8901ae08745Sheppo 8911ae08745Sheppo 8921ae08745Sheppo /* 8931ae08745Sheppo * Process an incoming version ctrl message 8941ae08745Sheppo */ 8951ae08745Sheppo static int 8961ae08745Sheppo i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg) 8971ae08745Sheppo { 8981ae08745Sheppo int rv = 0, idx = ldcp->next_vidx; 8991ae08745Sheppo ldc_msg_t *pkt; 9001ae08745Sheppo uint64_t tx_tail; 9011ae08745Sheppo ldc_ver_t *rcvd_ver; 9021ae08745Sheppo 9031ae08745Sheppo /* get the received version */ 9041ae08745Sheppo rcvd_ver = (ldc_ver_t *)((uint64_t)msg + LDC_PAYLOAD_VER_OFF); 9051ae08745Sheppo 9061ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) received VER v%u.%u\n", 9071ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 9081ae08745Sheppo 909d10e4ef2Snarayan /* Obtain Tx lock */ 910d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 911d10e4ef2Snarayan 9121ae08745Sheppo switch (msg->stype) { 9131ae08745Sheppo case LDC_INFO: 9141ae08745Sheppo 9153af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) { 9163af08d82Slm66018 (void) i_ldc_txq_reconf(ldcp); 9173af08d82Slm66018 i_ldc_reset_state(ldcp); 9183af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 9193af08d82Slm66018 return (EAGAIN); 9203af08d82Slm66018 } 9213af08d82Slm66018 9221ae08745Sheppo /* get the current tail and pkt for the response */ 9231ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 9241ae08745Sheppo if (rv != 0) { 9251ae08745Sheppo DWARN(ldcp->id, 9261ae08745Sheppo "i_ldc_process_VER: (0x%llx) err sending " 9271ae08745Sheppo "version ACK/NACK\n", ldcp->id); 9283af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 929d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 9301ae08745Sheppo return (ECONNRESET); 9311ae08745Sheppo } 9321ae08745Sheppo 9331ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 9341ae08745Sheppo ZERO_PKT(pkt); 9351ae08745Sheppo 9361ae08745Sheppo /* initialize the packet */ 9371ae08745Sheppo pkt->type = LDC_CTRL; 9381ae08745Sheppo pkt->ctrl = LDC_VER; 9391ae08745Sheppo 9401ae08745Sheppo for (;;) { 9411ae08745Sheppo 9421ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: got %u.%u chk %u.%u\n", 9431ae08745Sheppo rcvd_ver->major, rcvd_ver->minor, 9441ae08745Sheppo ldc_versions[idx].major, ldc_versions[idx].minor); 9451ae08745Sheppo 9461ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 9471ae08745Sheppo /* major version match - ACK version */ 9481ae08745Sheppo pkt->stype = LDC_ACK; 9491ae08745Sheppo 9501ae08745Sheppo /* 9511ae08745Sheppo * lower minor version to the one this endpt 9521ae08745Sheppo * supports, if necessary 9531ae08745Sheppo */ 9541ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 9551ae08745Sheppo rcvd_ver->minor = 9561ae08745Sheppo ldc_versions[idx].minor; 9571ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 9581ae08745Sheppo 9591ae08745Sheppo break; 9601ae08745Sheppo } 9611ae08745Sheppo 9621ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 9631ae08745Sheppo 9641ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 9651ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 9661ae08745Sheppo ldc_versions[idx].major, 9671ae08745Sheppo ldc_versions[idx].minor); 9681ae08745Sheppo 9691ae08745Sheppo /* nack with next lower version */ 9701ae08745Sheppo pkt->stype = LDC_NACK; 9711ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 9721ae08745Sheppo sizeof (ldc_versions[idx])); 9731ae08745Sheppo ldcp->next_vidx = idx; 9741ae08745Sheppo break; 9751ae08745Sheppo } 9761ae08745Sheppo 9771ae08745Sheppo /* next major version */ 9781ae08745Sheppo idx++; 9791ae08745Sheppo 9801ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 9811ae08745Sheppo 9821ae08745Sheppo if (idx == LDC_NUM_VERS) { 9831ae08745Sheppo /* no version match - send NACK */ 9841ae08745Sheppo pkt->stype = LDC_NACK; 9851ae08745Sheppo bzero(pkt->udata, sizeof (ldc_ver_t)); 9861ae08745Sheppo ldcp->next_vidx = 0; 9871ae08745Sheppo break; 9881ae08745Sheppo } 9891ae08745Sheppo } 9901ae08745Sheppo 9911ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 9921ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 9931ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 9941ae08745Sheppo 9951ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 9961ae08745Sheppo if (rv == 0) { 9971ae08745Sheppo ldcp->tx_tail = tx_tail; 9981ae08745Sheppo if (pkt->stype == LDC_ACK) { 9991ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent" 10001ae08745Sheppo " version ACK\n", ldcp->id); 10011ae08745Sheppo /* Save the ACK'd version */ 10021ae08745Sheppo ldcp->version.major = rcvd_ver->major; 10031ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 10040a55fbb7Slm66018 ldcp->hstate |= TS_RCVD_VER; 10051ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 10061ae08745Sheppo DWARN(DBG_ALL_LDCS, 10073af08d82Slm66018 "(0x%llx) Sent ACK, " 10083af08d82Slm66018 "Agreed on version v%u.%u\n", 10091ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 10101ae08745Sheppo } 10111ae08745Sheppo } else { 10121ae08745Sheppo DWARN(ldcp->id, 10131ae08745Sheppo "i_ldc_process_VER: (0x%llx) error sending " 10141ae08745Sheppo "ACK/NACK\n", ldcp->id); 10153af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1016d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10171ae08745Sheppo return (ECONNRESET); 10181ae08745Sheppo } 10191ae08745Sheppo 10201ae08745Sheppo break; 10211ae08745Sheppo 10221ae08745Sheppo case LDC_ACK: 10233af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_VREADY) { 10243af08d82Slm66018 if (ldcp->version.major != rcvd_ver->major || 10253af08d82Slm66018 ldcp->version.minor != rcvd_ver->minor) { 10263af08d82Slm66018 10273af08d82Slm66018 /* mismatched version - reset connection */ 10283af08d82Slm66018 DWARN(ldcp->id, 10293af08d82Slm66018 "i_ldc_process_VER: (0x%llx) recvd" 10303af08d82Slm66018 " ACK ver != sent ACK ver\n", ldcp->id); 10313af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 10323af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 10333af08d82Slm66018 return (ECONNRESET); 10343af08d82Slm66018 } 10353af08d82Slm66018 } else { 10361ae08745Sheppo /* SUCCESS - we have agreed on a version */ 10371ae08745Sheppo ldcp->version.major = rcvd_ver->major; 10381ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 10391ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 10403af08d82Slm66018 } 10411ae08745Sheppo 10423af08d82Slm66018 DWARN(DBG_ALL_LDCS, 10433af08d82Slm66018 "(0x%llx) Got ACK, Agreed on version v%u.%u\n", 10441ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 10451ae08745Sheppo 10461ae08745Sheppo /* initiate RTS-RTR-RDX handshake */ 10471ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 10481ae08745Sheppo if (rv) { 10491ae08745Sheppo DWARN(ldcp->id, 10501ae08745Sheppo "i_ldc_process_VER: (0x%llx) cannot send RTS\n", 10511ae08745Sheppo ldcp->id); 10523af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1053d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10541ae08745Sheppo return (ECONNRESET); 10551ae08745Sheppo } 10561ae08745Sheppo 10571ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 10581ae08745Sheppo ZERO_PKT(pkt); 10591ae08745Sheppo 10601ae08745Sheppo pkt->type = LDC_CTRL; 10611ae08745Sheppo pkt->stype = LDC_INFO; 10621ae08745Sheppo pkt->ctrl = LDC_RTS; 10631ae08745Sheppo pkt->env = ldcp->mode; 10641ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 10651ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 10661ae08745Sheppo 10671ae08745Sheppo ldcp->last_msg_rcd = LDC_INIT_SEQID; 10681ae08745Sheppo 10691ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_VER snd rts", (uint64_t)pkt); 10701ae08745Sheppo 10711ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 10721ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 10731ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 10741ae08745Sheppo 10751ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 10761ae08745Sheppo if (rv) { 10771ae08745Sheppo D2(ldcp->id, 10781ae08745Sheppo "i_ldc_process_VER: (0x%llx) no listener\n", 10791ae08745Sheppo ldcp->id); 10803af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1081d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10821ae08745Sheppo return (ECONNRESET); 10831ae08745Sheppo } 10841ae08745Sheppo 10851ae08745Sheppo ldcp->tx_tail = tx_tail; 10861ae08745Sheppo ldcp->hstate |= TS_SENT_RTS; 10871ae08745Sheppo 10881ae08745Sheppo break; 10891ae08745Sheppo 10901ae08745Sheppo case LDC_NACK: 10911ae08745Sheppo /* check if version in NACK is zero */ 10921ae08745Sheppo if (rcvd_ver->major == 0 && rcvd_ver->minor == 0) { 10931ae08745Sheppo /* version handshake failure */ 10941ae08745Sheppo DWARN(DBG_ALL_LDCS, 10951ae08745Sheppo "i_ldc_process_VER: (0x%llx) no version match\n", 10961ae08745Sheppo ldcp->id); 10973af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1098d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10991ae08745Sheppo return (ECONNRESET); 11001ae08745Sheppo } 11011ae08745Sheppo 11021ae08745Sheppo /* get the current tail and pkt for the response */ 11031ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 11041ae08745Sheppo if (rv != 0) { 11051ae08745Sheppo cmn_err(CE_NOTE, 11061ae08745Sheppo "i_ldc_process_VER: (0x%lx) err sending " 11071ae08745Sheppo "version ACK/NACK\n", ldcp->id); 11083af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1109d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11101ae08745Sheppo return (ECONNRESET); 11111ae08745Sheppo } 11121ae08745Sheppo 11131ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 11141ae08745Sheppo ZERO_PKT(pkt); 11151ae08745Sheppo 11161ae08745Sheppo /* initialize the packet */ 11171ae08745Sheppo pkt->type = LDC_CTRL; 11181ae08745Sheppo pkt->ctrl = LDC_VER; 11191ae08745Sheppo pkt->stype = LDC_INFO; 11201ae08745Sheppo 11211ae08745Sheppo /* check ver in NACK msg has a match */ 11221ae08745Sheppo for (;;) { 11231ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 11241ae08745Sheppo /* 11251ae08745Sheppo * major version match - resubmit request 11261ae08745Sheppo * if lower minor version to the one this endpt 11271ae08745Sheppo * supports, if necessary 11281ae08745Sheppo */ 11291ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 11301ae08745Sheppo rcvd_ver->minor = 11311ae08745Sheppo ldc_versions[idx].minor; 11321ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 11331ae08745Sheppo break; 11341ae08745Sheppo 11351ae08745Sheppo } 11361ae08745Sheppo 11371ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 11381ae08745Sheppo 11391ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 11401ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 11411ae08745Sheppo ldc_versions[idx].major, 11421ae08745Sheppo ldc_versions[idx].minor); 11431ae08745Sheppo 11441ae08745Sheppo /* send next lower version */ 11451ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 11461ae08745Sheppo sizeof (ldc_versions[idx])); 11471ae08745Sheppo ldcp->next_vidx = idx; 11481ae08745Sheppo break; 11491ae08745Sheppo } 11501ae08745Sheppo 11511ae08745Sheppo /* next version */ 11521ae08745Sheppo idx++; 11531ae08745Sheppo 11541ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 11551ae08745Sheppo 11561ae08745Sheppo if (idx == LDC_NUM_VERS) { 11571ae08745Sheppo /* no version match - terminate */ 11581ae08745Sheppo ldcp->next_vidx = 0; 1159d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11601ae08745Sheppo return (ECONNRESET); 11611ae08745Sheppo } 11621ae08745Sheppo } 11631ae08745Sheppo 11641ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 11651ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 11661ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 11671ae08745Sheppo 11681ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 11691ae08745Sheppo if (rv == 0) { 11701ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent version" 11711ae08745Sheppo "INFO v%u.%u\n", ldcp->id, ldc_versions[idx].major, 11721ae08745Sheppo ldc_versions[idx].minor); 11731ae08745Sheppo ldcp->tx_tail = tx_tail; 11741ae08745Sheppo } else { 11751ae08745Sheppo cmn_err(CE_NOTE, 11761ae08745Sheppo "i_ldc_process_VER: (0x%lx) error sending version" 11771ae08745Sheppo "INFO\n", ldcp->id); 11783af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1179d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11801ae08745Sheppo return (ECONNRESET); 11811ae08745Sheppo } 11821ae08745Sheppo 11831ae08745Sheppo break; 11841ae08745Sheppo } 11851ae08745Sheppo 1186d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11871ae08745Sheppo return (rv); 11881ae08745Sheppo } 11891ae08745Sheppo 11901ae08745Sheppo 11911ae08745Sheppo /* 11921ae08745Sheppo * Process an incoming RTS ctrl message 11931ae08745Sheppo */ 11941ae08745Sheppo static int 11951ae08745Sheppo i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg) 11961ae08745Sheppo { 11971ae08745Sheppo int rv = 0; 11981ae08745Sheppo ldc_msg_t *pkt; 11991ae08745Sheppo uint64_t tx_tail; 12001ae08745Sheppo boolean_t sent_NACK = B_FALSE; 12011ae08745Sheppo 12021ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTS: (0x%llx) received RTS\n", ldcp->id); 12031ae08745Sheppo 12041ae08745Sheppo switch (msg->stype) { 12051ae08745Sheppo case LDC_NACK: 12061ae08745Sheppo DWARN(ldcp->id, 12071ae08745Sheppo "i_ldc_process_RTS: (0x%llx) RTS NACK received\n", 12081ae08745Sheppo ldcp->id); 12091ae08745Sheppo 12101ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1211d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 12123af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1213d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12141ae08745Sheppo rv = ECONNRESET; 12151ae08745Sheppo break; 12161ae08745Sheppo 12171ae08745Sheppo case LDC_INFO: 12181ae08745Sheppo 12191ae08745Sheppo /* check mode */ 12201ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 12211ae08745Sheppo cmn_err(CE_NOTE, 12221ae08745Sheppo "i_ldc_process_RTS: (0x%lx) mode mismatch\n", 12231ae08745Sheppo ldcp->id); 12241ae08745Sheppo /* 12251ae08745Sheppo * send NACK in response to MODE message 12261ae08745Sheppo * get the current tail for the response 12271ae08745Sheppo */ 12281ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTS); 12291ae08745Sheppo if (rv) { 12301ae08745Sheppo /* if cannot send NACK - reset channel */ 1231d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 12323af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1233d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12341ae08745Sheppo rv = ECONNRESET; 12351ae08745Sheppo break; 12361ae08745Sheppo } 12371ae08745Sheppo sent_NACK = B_TRUE; 12381ae08745Sheppo } 12391ae08745Sheppo break; 12401ae08745Sheppo default: 12411ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTS: (0x%llx) unexp ACK\n", 12421ae08745Sheppo ldcp->id); 1243d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 12443af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1245d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12461ae08745Sheppo rv = ECONNRESET; 12471ae08745Sheppo break; 12481ae08745Sheppo } 12491ae08745Sheppo 12501ae08745Sheppo /* 12511ae08745Sheppo * If either the connection was reset (when rv != 0) or 12521ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 12531ae08745Sheppo * we dont want to consume the packet that came in but 12541ae08745Sheppo * not record that we received the RTS 12551ae08745Sheppo */ 12561ae08745Sheppo if (rv || sent_NACK) 12571ae08745Sheppo return (rv); 12581ae08745Sheppo 12591ae08745Sheppo /* record RTS received */ 12601ae08745Sheppo ldcp->hstate |= TS_RCVD_RTS; 12611ae08745Sheppo 12621ae08745Sheppo /* store initial SEQID info */ 12631ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 12641ae08745Sheppo 1265d10e4ef2Snarayan /* Obtain Tx lock */ 1266d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1267d10e4ef2Snarayan 12681ae08745Sheppo /* get the current tail for the response */ 12691ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 12701ae08745Sheppo if (rv != 0) { 12711ae08745Sheppo cmn_err(CE_NOTE, 12721ae08745Sheppo "i_ldc_process_RTS: (0x%lx) err sending RTR\n", 12731ae08745Sheppo ldcp->id); 12743af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1275d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12761ae08745Sheppo return (ECONNRESET); 12771ae08745Sheppo } 12781ae08745Sheppo 12791ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 12801ae08745Sheppo ZERO_PKT(pkt); 12811ae08745Sheppo 12821ae08745Sheppo /* initialize the packet */ 12831ae08745Sheppo pkt->type = LDC_CTRL; 12841ae08745Sheppo pkt->stype = LDC_INFO; 12851ae08745Sheppo pkt->ctrl = LDC_RTR; 12861ae08745Sheppo pkt->env = ldcp->mode; 12871ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 12881ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 12891ae08745Sheppo 12901ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 12911ae08745Sheppo 12921ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 12931ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 12941ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 12951ae08745Sheppo 12961ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 12971ae08745Sheppo if (rv == 0) { 12981ae08745Sheppo D2(ldcp->id, 12991ae08745Sheppo "i_ldc_process_RTS: (0x%llx) sent RTR\n", ldcp->id); 13001ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_RTS sent rtr", (uint64_t)pkt); 13011ae08745Sheppo 13021ae08745Sheppo ldcp->tx_tail = tx_tail; 13031ae08745Sheppo ldcp->hstate |= TS_SENT_RTR; 13041ae08745Sheppo 13051ae08745Sheppo } else { 13061ae08745Sheppo cmn_err(CE_NOTE, 13071ae08745Sheppo "i_ldc_process_RTS: (0x%lx) error sending RTR\n", 13081ae08745Sheppo ldcp->id); 13093af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1310d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13111ae08745Sheppo return (ECONNRESET); 13121ae08745Sheppo } 13131ae08745Sheppo 1314d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13151ae08745Sheppo return (0); 13161ae08745Sheppo } 13171ae08745Sheppo 13181ae08745Sheppo /* 13191ae08745Sheppo * Process an incoming RTR ctrl message 13201ae08745Sheppo */ 13211ae08745Sheppo static int 13221ae08745Sheppo i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg) 13231ae08745Sheppo { 13241ae08745Sheppo int rv = 0; 13251ae08745Sheppo boolean_t sent_NACK = B_FALSE; 13261ae08745Sheppo 13271ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTR: (0x%llx) received RTR\n", ldcp->id); 13281ae08745Sheppo 13291ae08745Sheppo switch (msg->stype) { 13301ae08745Sheppo case LDC_NACK: 13311ae08745Sheppo /* RTR NACK received */ 13321ae08745Sheppo DWARN(ldcp->id, 13331ae08745Sheppo "i_ldc_process_RTR: (0x%llx) RTR NACK received\n", 13341ae08745Sheppo ldcp->id); 13351ae08745Sheppo 13361ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1337d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 13383af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1339d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13401ae08745Sheppo rv = ECONNRESET; 13411ae08745Sheppo 13421ae08745Sheppo break; 13431ae08745Sheppo 13441ae08745Sheppo case LDC_INFO: 13451ae08745Sheppo 13461ae08745Sheppo /* check mode */ 13471ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 13481ae08745Sheppo DWARN(ldcp->id, 13491ae08745Sheppo "i_ldc_process_RTR: (0x%llx) mode mismatch\n", 13501ae08745Sheppo ldcp->id); 13511ae08745Sheppo /* 13521ae08745Sheppo * send NACK in response to MODE message 13531ae08745Sheppo * get the current tail for the response 13541ae08745Sheppo */ 13551ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTR); 13561ae08745Sheppo if (rv) { 13571ae08745Sheppo /* if cannot send NACK - reset channel */ 1358d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 13593af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1360d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13611ae08745Sheppo rv = ECONNRESET; 13621ae08745Sheppo break; 13631ae08745Sheppo } 13641ae08745Sheppo sent_NACK = B_TRUE; 13651ae08745Sheppo } 13661ae08745Sheppo break; 13671ae08745Sheppo 13681ae08745Sheppo default: 13691ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTR: (0x%llx) unexp ACK\n", 13701ae08745Sheppo ldcp->id); 13711ae08745Sheppo 13721ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1373d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 13743af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1375d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13761ae08745Sheppo rv = ECONNRESET; 13771ae08745Sheppo break; 13781ae08745Sheppo } 13791ae08745Sheppo 13801ae08745Sheppo /* 13811ae08745Sheppo * If either the connection was reset (when rv != 0) or 13821ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 13831ae08745Sheppo * we dont want to consume the packet that came in but 13841ae08745Sheppo * not record that we received the RTR 13851ae08745Sheppo */ 13861ae08745Sheppo if (rv || sent_NACK) 13871ae08745Sheppo return (rv); 13881ae08745Sheppo 13891ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 13901ae08745Sheppo ldcp->hstate |= TS_RCVD_RTR; 13911ae08745Sheppo 13921ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_INFO, LDC_RDX); 13931ae08745Sheppo if (rv) { 13941ae08745Sheppo cmn_err(CE_NOTE, 13951ae08745Sheppo "i_ldc_process_RTR: (0x%lx) cannot send RDX\n", 13961ae08745Sheppo ldcp->id); 1397d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 13983af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1399d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14001ae08745Sheppo return (ECONNRESET); 14011ae08745Sheppo } 14021ae08745Sheppo D2(ldcp->id, 14031ae08745Sheppo "i_ldc_process_RTR: (0x%llx) sent RDX\n", ldcp->id); 14041ae08745Sheppo 14051ae08745Sheppo ldcp->hstate |= TS_SENT_RDX; 14061ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 14073af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 14081ae08745Sheppo ldcp->status = LDC_UP; 14091ae08745Sheppo 14101ae08745Sheppo DWARN(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id); 14111ae08745Sheppo 14121ae08745Sheppo return (0); 14131ae08745Sheppo } 14141ae08745Sheppo 14151ae08745Sheppo 14161ae08745Sheppo /* 14171ae08745Sheppo * Process an incoming RDX ctrl message 14181ae08745Sheppo */ 14191ae08745Sheppo static int 14201ae08745Sheppo i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg) 14211ae08745Sheppo { 14221ae08745Sheppo int rv = 0; 14231ae08745Sheppo 14241ae08745Sheppo D2(ldcp->id, "i_ldc_process_RDX: (0x%llx) received RDX\n", ldcp->id); 14251ae08745Sheppo 14261ae08745Sheppo switch (msg->stype) { 14271ae08745Sheppo case LDC_NACK: 14281ae08745Sheppo /* RDX NACK received */ 14291ae08745Sheppo DWARN(ldcp->id, 14301ae08745Sheppo "i_ldc_process_RDX: (0x%llx) RDX NACK received\n", 14311ae08745Sheppo ldcp->id); 14321ae08745Sheppo 14331ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1434d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 14353af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1436d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14371ae08745Sheppo rv = ECONNRESET; 14381ae08745Sheppo 14391ae08745Sheppo break; 14401ae08745Sheppo 14411ae08745Sheppo case LDC_INFO: 14421ae08745Sheppo 14431ae08745Sheppo /* 14441ae08745Sheppo * if channel is UP and a RDX received after data transmission 14451ae08745Sheppo * has commenced it is an error 14461ae08745Sheppo */ 14471ae08745Sheppo if ((ldcp->tstate == TS_UP) && (ldcp->hstate & TS_RCVD_RDX)) { 14481ae08745Sheppo DWARN(DBG_ALL_LDCS, 14491ae08745Sheppo "i_ldc_process_RDX: (0x%llx) unexpected RDX" 14501ae08745Sheppo " - LDC reset\n", ldcp->id); 1451d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 14523af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1453d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14541ae08745Sheppo return (ECONNRESET); 14551ae08745Sheppo } 14561ae08745Sheppo 14571ae08745Sheppo ldcp->hstate |= TS_RCVD_RDX; 14581ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 14593af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 14601ae08745Sheppo ldcp->status = LDC_UP; 14611ae08745Sheppo 14621ae08745Sheppo D1(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id); 14631ae08745Sheppo break; 14641ae08745Sheppo 14651ae08745Sheppo default: 14661ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RDX: (0x%llx) unexp ACK\n", 14671ae08745Sheppo ldcp->id); 14681ae08745Sheppo 14691ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1470d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 14713af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1472d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14731ae08745Sheppo rv = ECONNRESET; 14741ae08745Sheppo break; 14751ae08745Sheppo } 14761ae08745Sheppo 14771ae08745Sheppo return (rv); 14781ae08745Sheppo } 14791ae08745Sheppo 14801ae08745Sheppo /* 14811ae08745Sheppo * Process an incoming ACK for a data packet 14821ae08745Sheppo */ 14831ae08745Sheppo static int 14841ae08745Sheppo i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg) 14851ae08745Sheppo { 14861ae08745Sheppo int rv; 14871ae08745Sheppo uint64_t tx_head; 14881ae08745Sheppo ldc_msg_t *pkt; 14891ae08745Sheppo 1490d10e4ef2Snarayan /* Obtain Tx lock */ 1491d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1492d10e4ef2Snarayan 14931ae08745Sheppo /* 1494d10e4ef2Snarayan * Read the current Tx head and tail 14951ae08745Sheppo */ 14961ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 14971ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 14981ae08745Sheppo if (rv != 0) { 14991ae08745Sheppo cmn_err(CE_WARN, 15001ae08745Sheppo "i_ldc_process_data_ACK: (0x%lx) cannot read qptrs\n", 15011ae08745Sheppo ldcp->id); 1502d10e4ef2Snarayan 1503d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 15043af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1505d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1506d10e4ef2Snarayan return (ECONNRESET); 15071ae08745Sheppo } 15081ae08745Sheppo 15091ae08745Sheppo /* 15101ae08745Sheppo * loop from where the previous ACK location was to the 15111ae08745Sheppo * current head location. This is how far the HV has 15121ae08745Sheppo * actually send pkts. Pkts between head and tail are 15131ae08745Sheppo * yet to be sent by HV. 15141ae08745Sheppo */ 15151ae08745Sheppo tx_head = ldcp->tx_ackd_head; 15161ae08745Sheppo for (;;) { 15171ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_head); 15181ae08745Sheppo tx_head = (tx_head + LDC_PACKET_SIZE) % 15191ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 15201ae08745Sheppo 15211ae08745Sheppo if (pkt->seqid == msg->ackid) { 15221ae08745Sheppo D2(ldcp->id, 15231ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) found packet\n", 15241ae08745Sheppo ldcp->id); 15251ae08745Sheppo ldcp->last_ack_rcd = msg->ackid; 15261ae08745Sheppo ldcp->tx_ackd_head = tx_head; 15271ae08745Sheppo break; 15281ae08745Sheppo } 15291ae08745Sheppo if (tx_head == ldcp->tx_head) { 15301ae08745Sheppo /* could not find packet */ 15311ae08745Sheppo DWARN(ldcp->id, 15321ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) invalid ACKid\n", 15331ae08745Sheppo ldcp->id); 1534d10e4ef2Snarayan 1535d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 15363af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 1537d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1538d10e4ef2Snarayan return (ECONNRESET); 15391ae08745Sheppo } 15401ae08745Sheppo } 15411ae08745Sheppo 1542d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15431ae08745Sheppo return (0); 15441ae08745Sheppo } 15451ae08745Sheppo 15461ae08745Sheppo /* 15471ae08745Sheppo * Process incoming control message 15481ae08745Sheppo * Return 0 - session can continue 15491ae08745Sheppo * EAGAIN - reprocess packet - state was changed 15501ae08745Sheppo * ECONNRESET - channel was reset 15511ae08745Sheppo */ 15521ae08745Sheppo static int 15531ae08745Sheppo i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *msg) 15541ae08745Sheppo { 15551ae08745Sheppo int rv = 0; 15561ae08745Sheppo 15573af08d82Slm66018 D1(ldcp->id, "i_ldc_ctrlmsg: (%llx) tstate = %lx, hstate = %lx\n", 15583af08d82Slm66018 ldcp->id, ldcp->tstate, ldcp->hstate); 15593af08d82Slm66018 15603af08d82Slm66018 switch (ldcp->tstate & ~TS_IN_RESET) { 15611ae08745Sheppo 15621ae08745Sheppo case TS_OPEN: 15631ae08745Sheppo case TS_READY: 15641ae08745Sheppo 15651ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 15661ae08745Sheppo case LDC_VER: 15671ae08745Sheppo /* process version message */ 15681ae08745Sheppo rv = i_ldc_process_VER(ldcp, msg); 15691ae08745Sheppo break; 15701ae08745Sheppo default: 15711ae08745Sheppo DWARN(ldcp->id, 15721ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 15731ae08745Sheppo "tstate=0x%x\n", ldcp->id, 15741ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 15751ae08745Sheppo break; 15761ae08745Sheppo } 15771ae08745Sheppo 15781ae08745Sheppo break; 15791ae08745Sheppo 15801ae08745Sheppo case TS_VREADY: 15811ae08745Sheppo 15821ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 15831ae08745Sheppo case LDC_VER: 15843af08d82Slm66018 /* process version message */ 15853af08d82Slm66018 rv = i_ldc_process_VER(ldcp, msg); 15861ae08745Sheppo break; 15871ae08745Sheppo case LDC_RTS: 15881ae08745Sheppo /* process RTS message */ 15891ae08745Sheppo rv = i_ldc_process_RTS(ldcp, msg); 15901ae08745Sheppo break; 15911ae08745Sheppo case LDC_RTR: 15921ae08745Sheppo /* process RTR message */ 15931ae08745Sheppo rv = i_ldc_process_RTR(ldcp, msg); 15941ae08745Sheppo break; 15951ae08745Sheppo case LDC_RDX: 15961ae08745Sheppo /* process RDX message */ 15971ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 15981ae08745Sheppo break; 15991ae08745Sheppo default: 16001ae08745Sheppo DWARN(ldcp->id, 16011ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 16021ae08745Sheppo "tstate=0x%x\n", ldcp->id, 16031ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 16041ae08745Sheppo break; 16051ae08745Sheppo } 16061ae08745Sheppo 16071ae08745Sheppo break; 16081ae08745Sheppo 16091ae08745Sheppo case TS_UP: 16101ae08745Sheppo 16111ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 16121ae08745Sheppo case LDC_VER: 16131ae08745Sheppo DWARN(ldcp->id, 16141ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexpected VER " 16151ae08745Sheppo "- LDC reset\n", ldcp->id); 16161ae08745Sheppo /* peer is redoing version negotiation */ 1617d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 16181ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 16191ae08745Sheppo i_ldc_reset_state(ldcp); 1620d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16211ae08745Sheppo rv = EAGAIN; 16221ae08745Sheppo break; 16231ae08745Sheppo 16241ae08745Sheppo case LDC_RDX: 16251ae08745Sheppo /* process RDX message */ 16261ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 16271ae08745Sheppo break; 16281ae08745Sheppo 16291ae08745Sheppo default: 16301ae08745Sheppo DWARN(ldcp->id, 16311ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 16321ae08745Sheppo "tstate=0x%x\n", ldcp->id, 16331ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 16341ae08745Sheppo break; 16351ae08745Sheppo } 16361ae08745Sheppo } 16371ae08745Sheppo 16381ae08745Sheppo return (rv); 16391ae08745Sheppo } 16401ae08745Sheppo 16411ae08745Sheppo /* 16421ae08745Sheppo * Register channel with the channel nexus 16431ae08745Sheppo */ 16441ae08745Sheppo static int 16451ae08745Sheppo i_ldc_register_channel(ldc_chan_t *ldcp) 16461ae08745Sheppo { 16471ae08745Sheppo int rv = 0; 16481ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 16491ae08745Sheppo 16501ae08745Sheppo if (cinfo->dip == NULL) { 16511ae08745Sheppo DWARN(ldcp->id, 16521ae08745Sheppo "i_ldc_register_channel: cnex has not registered\n"); 16531ae08745Sheppo return (EAGAIN); 16541ae08745Sheppo } 16551ae08745Sheppo 16561ae08745Sheppo rv = cinfo->reg_chan(cinfo->dip, ldcp->id, ldcp->devclass); 16571ae08745Sheppo if (rv) { 16581ae08745Sheppo DWARN(ldcp->id, 16591ae08745Sheppo "i_ldc_register_channel: cannot register channel\n"); 16601ae08745Sheppo return (rv); 16611ae08745Sheppo } 16621ae08745Sheppo 16631ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR, 16641ae08745Sheppo i_ldc_tx_hdlr, ldcp, NULL); 16651ae08745Sheppo if (rv) { 16661ae08745Sheppo DWARN(ldcp->id, 16671ae08745Sheppo "i_ldc_register_channel: cannot add Tx interrupt\n"); 16681ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 16691ae08745Sheppo return (rv); 16701ae08745Sheppo } 16711ae08745Sheppo 16721ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR, 16731ae08745Sheppo i_ldc_rx_hdlr, ldcp, NULL); 16741ae08745Sheppo if (rv) { 16751ae08745Sheppo DWARN(ldcp->id, 16761ae08745Sheppo "i_ldc_register_channel: cannot add Rx interrupt\n"); 16771ae08745Sheppo (void) cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 16781ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 16791ae08745Sheppo return (rv); 16801ae08745Sheppo } 16811ae08745Sheppo 16821ae08745Sheppo ldcp->tstate |= TS_CNEX_RDY; 16831ae08745Sheppo 16841ae08745Sheppo return (0); 16851ae08745Sheppo } 16861ae08745Sheppo 16871ae08745Sheppo /* 16881ae08745Sheppo * Unregister a channel with the channel nexus 16891ae08745Sheppo */ 16901ae08745Sheppo static int 16911ae08745Sheppo i_ldc_unregister_channel(ldc_chan_t *ldcp) 16921ae08745Sheppo { 16931ae08745Sheppo int rv = 0; 16941ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 16951ae08745Sheppo 16961ae08745Sheppo if (cinfo->dip == NULL) { 16971ae08745Sheppo DWARN(ldcp->id, 16981ae08745Sheppo "i_ldc_unregister_channel: cnex has not registered\n"); 16991ae08745Sheppo return (EAGAIN); 17001ae08745Sheppo } 17011ae08745Sheppo 17021ae08745Sheppo if (ldcp->tstate & TS_CNEX_RDY) { 17031ae08745Sheppo 1704d10e4ef2Snarayan /* Remove the Rx interrupt */ 17051ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR); 17061ae08745Sheppo if (rv) { 17073af08d82Slm66018 if (rv != EAGAIN) { 17081ae08745Sheppo DWARN(ldcp->id, 17093af08d82Slm66018 "i_ldc_unregister_channel: err removing " 17103af08d82Slm66018 "Rx intr\n"); 1711d10e4ef2Snarayan return (rv); 17121ae08745Sheppo } 1713d10e4ef2Snarayan 17143af08d82Slm66018 /* 17153af08d82Slm66018 * If interrupts are pending and handler has 17163af08d82Slm66018 * finished running, clear interrupt and try 17173af08d82Slm66018 * again 17183af08d82Slm66018 */ 17193af08d82Slm66018 if (ldcp->rx_intr_state != LDC_INTR_PEND) 17203af08d82Slm66018 return (rv); 17213af08d82Slm66018 17223af08d82Slm66018 (void) i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 17233af08d82Slm66018 rv = cinfo->rem_intr(cinfo->dip, ldcp->id, 17243af08d82Slm66018 CNEX_RX_INTR); 17253af08d82Slm66018 if (rv) { 17263af08d82Slm66018 DWARN(ldcp->id, "i_ldc_unregister_channel: " 17273af08d82Slm66018 "err removing Rx interrupt\n"); 17283af08d82Slm66018 return (rv); 17293af08d82Slm66018 } 17303af08d82Slm66018 } 17313af08d82Slm66018 1732d10e4ef2Snarayan /* Remove the Tx interrupt */ 17331ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 17341ae08745Sheppo if (rv) { 17351ae08745Sheppo DWARN(ldcp->id, 17361ae08745Sheppo "i_ldc_unregister_channel: err removing Tx intr\n"); 1737d10e4ef2Snarayan return (rv); 17381ae08745Sheppo } 1739d10e4ef2Snarayan 1740d10e4ef2Snarayan /* Unregister the channel */ 17411ae08745Sheppo rv = cinfo->unreg_chan(ldcssp->cinfo.dip, ldcp->id); 17421ae08745Sheppo if (rv) { 17431ae08745Sheppo DWARN(ldcp->id, 17441ae08745Sheppo "i_ldc_unregister_channel: cannot unreg channel\n"); 1745d10e4ef2Snarayan return (rv); 17461ae08745Sheppo } 17471ae08745Sheppo 17481ae08745Sheppo ldcp->tstate &= ~TS_CNEX_RDY; 17491ae08745Sheppo } 17501ae08745Sheppo 17511ae08745Sheppo return (0); 17521ae08745Sheppo } 17531ae08745Sheppo 17541ae08745Sheppo 17551ae08745Sheppo /* 17561ae08745Sheppo * LDC transmit interrupt handler 17571ae08745Sheppo * triggered for chanel up/down/reset events 17581ae08745Sheppo * and Tx queue content changes 17591ae08745Sheppo */ 17601ae08745Sheppo static uint_t 17611ae08745Sheppo i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2) 17621ae08745Sheppo { 17631ae08745Sheppo _NOTE(ARGUNUSED(arg2)) 17641ae08745Sheppo 17651ae08745Sheppo int rv; 17661ae08745Sheppo ldc_chan_t *ldcp; 17671ae08745Sheppo boolean_t notify_client = B_FALSE; 17683af08d82Slm66018 uint64_t notify_event = 0, link_state; 17691ae08745Sheppo 17701ae08745Sheppo /* Get the channel for which interrupt was received */ 17711ae08745Sheppo ASSERT(arg1 != NULL); 17721ae08745Sheppo ldcp = (ldc_chan_t *)arg1; 17731ae08745Sheppo 17741ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 17751ae08745Sheppo ldcp->id, ldcp); 17761ae08745Sheppo 17771ae08745Sheppo /* Lock channel */ 17781ae08745Sheppo mutex_enter(&ldcp->lock); 17791ae08745Sheppo 1780d10e4ef2Snarayan /* Obtain Tx lock */ 1781d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1782d10e4ef2Snarayan 17834bac2208Snarayan /* mark interrupt as pending */ 17843af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_ACTIVE; 17853af08d82Slm66018 17863af08d82Slm66018 /* save current link state */ 17873af08d82Slm66018 link_state = ldcp->link_state; 17884bac2208Snarayan 17891ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &ldcp->tx_head, &ldcp->tx_tail, 17901ae08745Sheppo &ldcp->link_state); 17911ae08745Sheppo if (rv) { 17921ae08745Sheppo cmn_err(CE_WARN, 17931ae08745Sheppo "i_ldc_tx_hdlr: (0x%lx) cannot read queue ptrs rv=0x%d\n", 17941ae08745Sheppo ldcp->id, rv); 17954bac2208Snarayan i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 1796d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17971ae08745Sheppo mutex_exit(&ldcp->lock); 17981ae08745Sheppo return (DDI_INTR_CLAIMED); 17991ae08745Sheppo } 18001ae08745Sheppo 18011ae08745Sheppo /* 18021ae08745Sheppo * reset the channel state if the channel went down 18031ae08745Sheppo * (other side unconfigured queue) or channel was reset 18041ae08745Sheppo * (other side reconfigured its queue) 18051ae08745Sheppo */ 18063af08d82Slm66018 if (link_state != ldcp->link_state && 18073af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_DOWN) { 18081ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link down\n", ldcp->id); 18093af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 18101ae08745Sheppo notify_client = B_TRUE; 18111ae08745Sheppo notify_event = LDC_EVT_DOWN; 18121ae08745Sheppo } 18131ae08745Sheppo 18143af08d82Slm66018 if (link_state != ldcp->link_state && 18153af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 18161ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link reset\n", ldcp->id); 18173af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 18181ae08745Sheppo notify_client = B_TRUE; 18191ae08745Sheppo notify_event = LDC_EVT_RESET; 18201ae08745Sheppo } 18211ae08745Sheppo 18223af08d82Slm66018 if (link_state != ldcp->link_state && 18233af08d82Slm66018 (ldcp->tstate & ~TS_IN_RESET) == TS_OPEN && 18243af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_UP) { 18251ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link up\n", ldcp->id); 18261ae08745Sheppo notify_client = B_TRUE; 18271ae08745Sheppo notify_event = LDC_EVT_RESET; 18281ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 18291ae08745Sheppo ldcp->status = LDC_READY; 18301ae08745Sheppo } 18311ae08745Sheppo 18321ae08745Sheppo /* if callbacks are disabled, do not notify */ 18331ae08745Sheppo if (!ldcp->cb_enabled) 18341ae08745Sheppo notify_client = B_FALSE; 18351ae08745Sheppo 18361ae08745Sheppo /* Unlock channel */ 18371ae08745Sheppo 18381ae08745Sheppo if (notify_client) { 18393af08d82Slm66018 ldcp->cb_inprogress = B_TRUE; 18403af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 18413af08d82Slm66018 mutex_exit(&ldcp->lock); 18421ae08745Sheppo rv = ldcp->cb(notify_event, ldcp->cb_arg); 18431ae08745Sheppo if (rv) { 18441ae08745Sheppo DWARN(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) callback " 18451ae08745Sheppo "failure", ldcp->id); 18461ae08745Sheppo } 18471ae08745Sheppo mutex_enter(&ldcp->lock); 18481ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 18491ae08745Sheppo } 18501ae08745Sheppo 18511ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 18521ae08745Sheppo mutex_exit(&ldcp->lock); 18531ae08745Sheppo 18541ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) exiting handler", ldcp->id); 18551ae08745Sheppo 18561ae08745Sheppo return (DDI_INTR_CLAIMED); 18571ae08745Sheppo } 18581ae08745Sheppo 18591ae08745Sheppo /* 18601ae08745Sheppo * LDC receive interrupt handler 18611ae08745Sheppo * triggered for channel with data pending to read 18621ae08745Sheppo * i.e. Rx queue content changes 18631ae08745Sheppo */ 18641ae08745Sheppo static uint_t 18651ae08745Sheppo i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2) 18661ae08745Sheppo { 18671ae08745Sheppo _NOTE(ARGUNUSED(arg2)) 18681ae08745Sheppo 18691ae08745Sheppo int rv; 18701ae08745Sheppo uint64_t rx_head, rx_tail; 18711ae08745Sheppo ldc_msg_t *msg; 18721ae08745Sheppo ldc_chan_t *ldcp; 18731ae08745Sheppo boolean_t notify_client = B_FALSE; 18741ae08745Sheppo uint64_t notify_event = 0; 18753af08d82Slm66018 uint64_t link_state, first_fragment = 0; 18763af08d82Slm66018 18771ae08745Sheppo 18781ae08745Sheppo /* Get the channel for which interrupt was received */ 18791ae08745Sheppo if (arg1 == NULL) { 18801ae08745Sheppo cmn_err(CE_WARN, "i_ldc_rx_hdlr: invalid arg\n"); 18811ae08745Sheppo return (DDI_INTR_UNCLAIMED); 18821ae08745Sheppo } 18831ae08745Sheppo 18841ae08745Sheppo ldcp = (ldc_chan_t *)arg1; 18851ae08745Sheppo 18861ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 18871ae08745Sheppo ldcp->id, ldcp); 18883af08d82Slm66018 D1(ldcp->id, "i_ldc_rx_hdlr: (%llx) USR%lx/TS%lx/HS%lx, LSTATE=%lx\n", 18893af08d82Slm66018 ldcp->id, ldcp->status, ldcp->tstate, ldcp->hstate, 18903af08d82Slm66018 ldcp->link_state); 18911ae08745Sheppo 18921ae08745Sheppo /* Lock channel */ 18931ae08745Sheppo mutex_enter(&ldcp->lock); 18941ae08745Sheppo 18951ae08745Sheppo /* mark interrupt as pending */ 18963af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_ACTIVE; 18971ae08745Sheppo 18981ae08745Sheppo /* 18991ae08745Sheppo * Read packet(s) from the queue 19001ae08745Sheppo */ 19011ae08745Sheppo for (;;) { 19021ae08745Sheppo 19033af08d82Slm66018 link_state = ldcp->link_state; 19041ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 19051ae08745Sheppo &ldcp->link_state); 19061ae08745Sheppo if (rv) { 19071ae08745Sheppo cmn_err(CE_WARN, 19081ae08745Sheppo "i_ldc_rx_hdlr: (0x%lx) cannot read " 19091ae08745Sheppo "queue ptrs, rv=0x%d\n", ldcp->id, rv); 19101ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 19111ae08745Sheppo mutex_exit(&ldcp->lock); 19121ae08745Sheppo return (DDI_INTR_CLAIMED); 19131ae08745Sheppo } 19141ae08745Sheppo 19151ae08745Sheppo /* 19161ae08745Sheppo * reset the channel state if the channel went down 19171ae08745Sheppo * (other side unconfigured queue) or channel was reset 19183af08d82Slm66018 * (other side reconfigured its queue) 19191ae08745Sheppo */ 19203af08d82Slm66018 19213af08d82Slm66018 if (link_state != ldcp->link_state) { 19223af08d82Slm66018 switch (ldcp->link_state) { 19233af08d82Slm66018 case LDC_CHANNEL_DOWN: 19243af08d82Slm66018 D1(ldcp->id, "i_ldc_rx_hdlr: channel " 19253af08d82Slm66018 "link down\n", ldcp->id); 1926d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 19273af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 1928d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 19291ae08745Sheppo notify_client = B_TRUE; 19301ae08745Sheppo notify_event = LDC_EVT_DOWN; 19313af08d82Slm66018 goto loop_exit; 19321ae08745Sheppo 19333af08d82Slm66018 case LDC_CHANNEL_UP: 19343af08d82Slm66018 D1(ldcp->id, "i_ldc_rx_hdlr: " 19353af08d82Slm66018 "channel link up\n", ldcp->id); 19363af08d82Slm66018 19373af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) == TS_OPEN) { 19381ae08745Sheppo notify_client = B_TRUE; 19391ae08745Sheppo notify_event = LDC_EVT_RESET; 19401ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 19411ae08745Sheppo ldcp->status = LDC_READY; 19421ae08745Sheppo } 19433af08d82Slm66018 break; 19443af08d82Slm66018 19453af08d82Slm66018 case LDC_CHANNEL_RESET: 19463af08d82Slm66018 default: 19473af08d82Slm66018 #ifdef DEBUG 19483af08d82Slm66018 force_reset: 19493af08d82Slm66018 #endif 19503af08d82Slm66018 D1(ldcp->id, "i_ldc_rx_hdlr: channel " 19513af08d82Slm66018 "link reset\n", ldcp->id); 19523af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 19533af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 19543af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 19553af08d82Slm66018 notify_client = B_TRUE; 19563af08d82Slm66018 notify_event = LDC_EVT_RESET; 19573af08d82Slm66018 break; 19583af08d82Slm66018 } 19593af08d82Slm66018 } 19603af08d82Slm66018 19613af08d82Slm66018 #ifdef DEBUG 19623af08d82Slm66018 if (LDC_INJECT_RESET(ldcp)) 19633af08d82Slm66018 goto force_reset; 19643af08d82Slm66018 #endif 19651ae08745Sheppo 19661ae08745Sheppo if (rx_head == rx_tail) { 19671ae08745Sheppo D2(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) No packets\n", 19681ae08745Sheppo ldcp->id); 19691ae08745Sheppo break; 19701ae08745Sheppo } 19713af08d82Slm66018 19721ae08745Sheppo D2(ldcp->id, "i_ldc_rx_hdlr: head=0x%llx, tail=0x%llx\n", 19731ae08745Sheppo rx_head, rx_tail); 19741ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_rx_hdlr rcd", 19751ae08745Sheppo ldcp->rx_q_va + rx_head); 19761ae08745Sheppo 19771ae08745Sheppo /* get the message */ 19781ae08745Sheppo msg = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 19791ae08745Sheppo 19801ae08745Sheppo /* if channel is in RAW mode or data pkt, notify and return */ 19811ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 19821ae08745Sheppo notify_client = B_TRUE; 19831ae08745Sheppo notify_event |= LDC_EVT_READ; 19841ae08745Sheppo break; 19851ae08745Sheppo } 19861ae08745Sheppo 19871ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 19881ae08745Sheppo 19891ae08745Sheppo /* discard packet if channel is not up */ 19903af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) != TS_UP) { 19911ae08745Sheppo 19921ae08745Sheppo /* move the head one position */ 19931ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 19941ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 19951ae08745Sheppo 19961ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, rx_head)) 19971ae08745Sheppo break; 19981ae08745Sheppo 19991ae08745Sheppo continue; 20001ae08745Sheppo } else { 20013af08d82Slm66018 if ((ldcp->tstate & TS_IN_RESET) == 0) 20021ae08745Sheppo notify_client = B_TRUE; 20031ae08745Sheppo notify_event |= LDC_EVT_READ; 20041ae08745Sheppo break; 20051ae08745Sheppo } 20061ae08745Sheppo } 20071ae08745Sheppo 20081ae08745Sheppo /* Check the sequence ID for the message received */ 20093af08d82Slm66018 rv = i_ldc_check_seqid(ldcp, msg); 20103af08d82Slm66018 if (rv != 0) { 20111ae08745Sheppo 20121ae08745Sheppo DWARN(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) seqid error, " 20131ae08745Sheppo "q_ptrs=0x%lx,0x%lx", ldcp->id, rx_head, rx_tail); 20141ae08745Sheppo 20151ae08745Sheppo /* Reset last_msg_rcd to start of message */ 2016d10e4ef2Snarayan if (first_fragment != 0) { 2017d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 2018d10e4ef2Snarayan first_fragment = 0; 20191ae08745Sheppo } 2020d10e4ef2Snarayan 20211ae08745Sheppo /* 20221ae08745Sheppo * Send a NACK due to seqid mismatch 20231ae08745Sheppo */ 20241ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, 20251ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 20261ae08745Sheppo 20271ae08745Sheppo if (rv) { 20281ae08745Sheppo cmn_err(CE_NOTE, 20291ae08745Sheppo "i_ldc_rx_hdlr: (0x%lx) err sending " 20301ae08745Sheppo "CTRL/NACK msg\n", ldcp->id); 2031d10e4ef2Snarayan 2032d10e4ef2Snarayan /* if cannot send NACK - reset channel */ 2033d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 20343af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 2035d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 2036d10e4ef2Snarayan rv = ECONNRESET; 2037d10e4ef2Snarayan break; 20381ae08745Sheppo } 20391ae08745Sheppo 20401ae08745Sheppo /* purge receive queue */ 20411ae08745Sheppo (void) i_ldc_set_rx_head(ldcp, rx_tail); 20421ae08745Sheppo break; 20431ae08745Sheppo } 20441ae08745Sheppo 20451ae08745Sheppo /* record the message ID */ 20461ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 20471ae08745Sheppo 20481ae08745Sheppo /* process control messages */ 20491ae08745Sheppo if (msg->type & LDC_CTRL) { 20501ae08745Sheppo /* save current internal state */ 20511ae08745Sheppo uint64_t tstate = ldcp->tstate; 20521ae08745Sheppo 20531ae08745Sheppo rv = i_ldc_ctrlmsg(ldcp, msg); 20541ae08745Sheppo if (rv == EAGAIN) { 20551ae08745Sheppo /* re-process pkt - state was adjusted */ 20561ae08745Sheppo continue; 20571ae08745Sheppo } 20581ae08745Sheppo if (rv == ECONNRESET) { 20591ae08745Sheppo notify_client = B_TRUE; 20601ae08745Sheppo notify_event = LDC_EVT_RESET; 20611ae08745Sheppo break; 20621ae08745Sheppo } 20631ae08745Sheppo 20641ae08745Sheppo /* 20651ae08745Sheppo * control message processing was successful 20661ae08745Sheppo * channel transitioned to ready for communication 20671ae08745Sheppo */ 20681ae08745Sheppo if (rv == 0 && ldcp->tstate == TS_UP && 20693af08d82Slm66018 (tstate & ~TS_IN_RESET) != 20703af08d82Slm66018 (ldcp->tstate & ~TS_IN_RESET)) { 20711ae08745Sheppo notify_client = B_TRUE; 20721ae08745Sheppo notify_event = LDC_EVT_UP; 20731ae08745Sheppo } 20741ae08745Sheppo } 20751ae08745Sheppo 20761ae08745Sheppo /* process data ACKs */ 20771ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 2078d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 2079d10e4ef2Snarayan notify_client = B_TRUE; 2080d10e4ef2Snarayan notify_event = LDC_EVT_RESET; 2081d10e4ef2Snarayan break; 2082d10e4ef2Snarayan } 20831ae08745Sheppo } 20841ae08745Sheppo 20851ae08745Sheppo /* move the head one position */ 20861ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 20871ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 20880a55fbb7Slm66018 if (rv = i_ldc_set_rx_head(ldcp, rx_head)) { 20890a55fbb7Slm66018 notify_client = B_TRUE; 20900a55fbb7Slm66018 notify_event = LDC_EVT_RESET; 20911ae08745Sheppo break; 20920a55fbb7Slm66018 } 20931ae08745Sheppo 20941ae08745Sheppo } /* for */ 20951ae08745Sheppo 20963af08d82Slm66018 loop_exit: 20973af08d82Slm66018 20981ae08745Sheppo /* if callbacks are disabled, do not notify */ 20991ae08745Sheppo if (!ldcp->cb_enabled) 21001ae08745Sheppo notify_client = B_FALSE; 21011ae08745Sheppo 21023af08d82Slm66018 /* 21033af08d82Slm66018 * If there are data packets in the queue, the ldc_read will 21043af08d82Slm66018 * clear interrupts after draining the queue, else clear interrupts 21053af08d82Slm66018 */ 21063af08d82Slm66018 if ((notify_event & LDC_EVT_READ) == 0) { 21073af08d82Slm66018 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 21083af08d82Slm66018 } else 21093af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_PEND; 21101ae08745Sheppo 21111ae08745Sheppo mutex_exit(&ldcp->lock); 21121ae08745Sheppo 21131ae08745Sheppo if (notify_client) { 21141ae08745Sheppo rv = ldcp->cb(notify_event, ldcp->cb_arg); 21151ae08745Sheppo if (rv) { 21161ae08745Sheppo DWARN(ldcp->id, 21171ae08745Sheppo "i_ldc_rx_hdlr: (0x%llx) callback failure", 21181ae08745Sheppo ldcp->id); 21191ae08745Sheppo } 21201ae08745Sheppo } 21211ae08745Sheppo 21221ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) exiting handler", ldcp->id); 21231ae08745Sheppo return (DDI_INTR_CLAIMED); 21241ae08745Sheppo } 21251ae08745Sheppo 21261ae08745Sheppo 21271ae08745Sheppo /* -------------------------------------------------------------------------- */ 21281ae08745Sheppo 21291ae08745Sheppo /* 21301ae08745Sheppo * LDC API functions 21311ae08745Sheppo */ 21321ae08745Sheppo 21331ae08745Sheppo /* 21341ae08745Sheppo * Initialize the channel. Allocate internal structure and memory for 21351ae08745Sheppo * TX/RX queues, and initialize locks. 21361ae08745Sheppo */ 21371ae08745Sheppo int 21381ae08745Sheppo ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle) 21391ae08745Sheppo { 21401ae08745Sheppo ldc_chan_t *ldcp; 21411ae08745Sheppo int rv, exit_val; 21421ae08745Sheppo uint64_t ra_base, nentries; 2143e1ebb9ecSlm66018 uint64_t qlen; 21441ae08745Sheppo 21451ae08745Sheppo exit_val = EINVAL; /* guarantee an error if exit on failure */ 21461ae08745Sheppo 21471ae08745Sheppo if (attr == NULL) { 21481ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid attr\n", id); 21491ae08745Sheppo return (EINVAL); 21501ae08745Sheppo } 21511ae08745Sheppo if (handle == NULL) { 21521ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid handle\n", id); 21531ae08745Sheppo return (EINVAL); 21541ae08745Sheppo } 21551ae08745Sheppo 21561ae08745Sheppo /* check if channel is valid */ 21571ae08745Sheppo rv = hv_ldc_tx_qinfo(id, &ra_base, &nentries); 21581ae08745Sheppo if (rv == H_ECHANNEL) { 21591ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid channel id\n", id); 21601ae08745Sheppo return (EINVAL); 21611ae08745Sheppo } 21621ae08745Sheppo 21631ae08745Sheppo /* check if the channel has already been initialized */ 21641ae08745Sheppo mutex_enter(&ldcssp->lock); 21651ae08745Sheppo ldcp = ldcssp->chan_list; 21661ae08745Sheppo while (ldcp != NULL) { 21671ae08745Sheppo if (ldcp->id == id) { 21681ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) already initialized\n", 21691ae08745Sheppo id); 21701ae08745Sheppo mutex_exit(&ldcssp->lock); 21711ae08745Sheppo return (EADDRINUSE); 21721ae08745Sheppo } 21731ae08745Sheppo ldcp = ldcp->next; 21741ae08745Sheppo } 21751ae08745Sheppo mutex_exit(&ldcssp->lock); 21761ae08745Sheppo 21771ae08745Sheppo ASSERT(ldcp == NULL); 21781ae08745Sheppo 21791ae08745Sheppo *handle = 0; 21801ae08745Sheppo 21811ae08745Sheppo /* Allocate an ldcp structure */ 21821ae08745Sheppo ldcp = kmem_zalloc(sizeof (ldc_chan_t), KM_SLEEP); 21831ae08745Sheppo 2184d10e4ef2Snarayan /* 2185d10e4ef2Snarayan * Initialize the channel and Tx lock 2186d10e4ef2Snarayan * 2187d10e4ef2Snarayan * The channel 'lock' protects the entire channel and 2188d10e4ef2Snarayan * should be acquired before initializing, resetting, 2189d10e4ef2Snarayan * destroying or reading from a channel. 2190d10e4ef2Snarayan * 2191d10e4ef2Snarayan * The 'tx_lock' should be acquired prior to transmitting 2192d10e4ef2Snarayan * data over the channel. The lock should also be acquired 2193d10e4ef2Snarayan * prior to channel reconfiguration (in order to prevent 2194d10e4ef2Snarayan * concurrent writes). 2195d10e4ef2Snarayan * 2196d10e4ef2Snarayan * ORDERING: When both locks are being acquired, to prevent 2197d10e4ef2Snarayan * deadlocks, the channel lock should be always acquired prior 2198d10e4ef2Snarayan * to the tx_lock. 2199d10e4ef2Snarayan */ 22001ae08745Sheppo mutex_init(&ldcp->lock, NULL, MUTEX_DRIVER, NULL); 2201d10e4ef2Snarayan mutex_init(&ldcp->tx_lock, NULL, MUTEX_DRIVER, NULL); 22021ae08745Sheppo 22031ae08745Sheppo /* Initialize the channel */ 22041ae08745Sheppo ldcp->id = id; 22051ae08745Sheppo ldcp->cb = NULL; 22061ae08745Sheppo ldcp->cb_arg = NULL; 22071ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 22081ae08745Sheppo ldcp->cb_enabled = B_FALSE; 22091ae08745Sheppo ldcp->next = NULL; 22101ae08745Sheppo 22111ae08745Sheppo /* Read attributes */ 22121ae08745Sheppo ldcp->mode = attr->mode; 22131ae08745Sheppo ldcp->devclass = attr->devclass; 22141ae08745Sheppo ldcp->devinst = attr->instance; 2215e1ebb9ecSlm66018 ldcp->mtu = (attr->mtu > 0) ? attr->mtu : LDC_DEFAULT_MTU; 22161ae08745Sheppo 22171ae08745Sheppo D1(ldcp->id, 22181ae08745Sheppo "ldc_init: (0x%llx) channel attributes, class=0x%x, " 2219e1ebb9ecSlm66018 "instance=0x%llx, mode=%d, mtu=%d\n", 2220e1ebb9ecSlm66018 ldcp->id, ldcp->devclass, ldcp->devinst, ldcp->mode, ldcp->mtu); 22211ae08745Sheppo 22221ae08745Sheppo ldcp->next_vidx = 0; 22233af08d82Slm66018 ldcp->tstate = TS_IN_RESET; 22241ae08745Sheppo ldcp->hstate = 0; 22251ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 22261ae08745Sheppo ldcp->last_ack_rcd = 0; 22271ae08745Sheppo ldcp->last_msg_rcd = 0; 22281ae08745Sheppo 22291ae08745Sheppo ldcp->stream_bufferp = NULL; 22301ae08745Sheppo ldcp->exp_dring_list = NULL; 22311ae08745Sheppo ldcp->imp_dring_list = NULL; 22321ae08745Sheppo ldcp->mhdl_list = NULL; 22331ae08745Sheppo 22343af08d82Slm66018 ldcp->tx_intr_state = LDC_INTR_NONE; 22353af08d82Slm66018 ldcp->rx_intr_state = LDC_INTR_NONE; 22363af08d82Slm66018 22371ae08745Sheppo /* Initialize payload size depending on whether channel is reliable */ 22381ae08745Sheppo switch (ldcp->mode) { 22391ae08745Sheppo case LDC_MODE_RAW: 22401ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RAW; 22411ae08745Sheppo ldcp->read_p = i_ldc_read_raw; 22421ae08745Sheppo ldcp->write_p = i_ldc_write_raw; 22431ae08745Sheppo break; 22441ae08745Sheppo case LDC_MODE_UNRELIABLE: 22451ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_UNRELIABLE; 22461ae08745Sheppo ldcp->read_p = i_ldc_read_packet; 22471ae08745Sheppo ldcp->write_p = i_ldc_write_packet; 22481ae08745Sheppo break; 22491ae08745Sheppo case LDC_MODE_RELIABLE: 22501ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE; 22511ae08745Sheppo ldcp->read_p = i_ldc_read_packet; 22521ae08745Sheppo ldcp->write_p = i_ldc_write_packet; 22531ae08745Sheppo break; 22541ae08745Sheppo case LDC_MODE_STREAM: 22551ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE; 22561ae08745Sheppo 22571ae08745Sheppo ldcp->stream_remains = 0; 22581ae08745Sheppo ldcp->stream_offset = 0; 22591ae08745Sheppo ldcp->stream_bufferp = kmem_alloc(ldcp->mtu, KM_SLEEP); 22601ae08745Sheppo ldcp->read_p = i_ldc_read_stream; 22611ae08745Sheppo ldcp->write_p = i_ldc_write_stream; 22621ae08745Sheppo break; 22631ae08745Sheppo default: 22641ae08745Sheppo exit_val = EINVAL; 22651ae08745Sheppo goto cleanup_on_exit; 22661ae08745Sheppo } 22671ae08745Sheppo 2268e1ebb9ecSlm66018 /* 2269e1ebb9ecSlm66018 * qlen is (mtu * ldc_mtu_msgs) / pkt_payload. If this 2270e1ebb9ecSlm66018 * value is smaller than default length of ldc_queue_entries, 2271e1ebb9ecSlm66018 * qlen is set to ldc_queue_entries.. 2272e1ebb9ecSlm66018 */ 2273e1ebb9ecSlm66018 qlen = (ldcp->mtu * ldc_mtu_msgs) / ldcp->pkt_payload; 2274e1ebb9ecSlm66018 ldcp->rx_q_entries = 2275e1ebb9ecSlm66018 (qlen < ldc_queue_entries) ? ldc_queue_entries : qlen; 2276e1ebb9ecSlm66018 ldcp->tx_q_entries = ldcp->rx_q_entries; 2277e1ebb9ecSlm66018 2278e1ebb9ecSlm66018 D1(ldcp->id, "ldc_init: queue length = 0x%llx\n", qlen); 2279e1ebb9ecSlm66018 22801ae08745Sheppo /* Create a transmit queue */ 22811ae08745Sheppo ldcp->tx_q_va = (uint64_t) 22821ae08745Sheppo contig_mem_alloc(ldcp->tx_q_entries << LDC_PACKET_SHIFT); 22831ae08745Sheppo if (ldcp->tx_q_va == NULL) { 22841ae08745Sheppo cmn_err(CE_WARN, 22851ae08745Sheppo "ldc_init: (0x%lx) TX queue allocation failed\n", 22861ae08745Sheppo ldcp->id); 22871ae08745Sheppo exit_val = ENOMEM; 22881ae08745Sheppo goto cleanup_on_exit; 22891ae08745Sheppo } 22901ae08745Sheppo ldcp->tx_q_ra = va_to_pa((caddr_t)ldcp->tx_q_va); 22911ae08745Sheppo 22921ae08745Sheppo D2(ldcp->id, "ldc_init: txq_va=0x%llx, txq_ra=0x%llx, entries=0x%llx\n", 22931ae08745Sheppo ldcp->tx_q_va, ldcp->tx_q_ra, ldcp->tx_q_entries); 22941ae08745Sheppo 22951ae08745Sheppo ldcp->tstate |= TS_TXQ_RDY; 22961ae08745Sheppo 22971ae08745Sheppo /* Create a receive queue */ 22981ae08745Sheppo ldcp->rx_q_va = (uint64_t) 22991ae08745Sheppo contig_mem_alloc(ldcp->rx_q_entries << LDC_PACKET_SHIFT); 23001ae08745Sheppo if (ldcp->rx_q_va == NULL) { 23011ae08745Sheppo cmn_err(CE_WARN, 23021ae08745Sheppo "ldc_init: (0x%lx) RX queue allocation failed\n", 23031ae08745Sheppo ldcp->id); 23041ae08745Sheppo exit_val = ENOMEM; 23051ae08745Sheppo goto cleanup_on_exit; 23061ae08745Sheppo } 23071ae08745Sheppo ldcp->rx_q_ra = va_to_pa((caddr_t)ldcp->rx_q_va); 23081ae08745Sheppo 23091ae08745Sheppo D2(ldcp->id, "ldc_init: rxq_va=0x%llx, rxq_ra=0x%llx, entries=0x%llx\n", 23101ae08745Sheppo ldcp->rx_q_va, ldcp->rx_q_ra, ldcp->rx_q_entries); 23111ae08745Sheppo 23121ae08745Sheppo ldcp->tstate |= TS_RXQ_RDY; 23131ae08745Sheppo 23141ae08745Sheppo /* Init descriptor ring and memory handle list lock */ 23151ae08745Sheppo mutex_init(&ldcp->exp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 23161ae08745Sheppo mutex_init(&ldcp->imp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 23171ae08745Sheppo mutex_init(&ldcp->mlist_lock, NULL, MUTEX_DRIVER, NULL); 23181ae08745Sheppo 23191ae08745Sheppo /* mark status as INITialized */ 23201ae08745Sheppo ldcp->status = LDC_INIT; 23211ae08745Sheppo 23221ae08745Sheppo /* Add to channel list */ 23231ae08745Sheppo mutex_enter(&ldcssp->lock); 23241ae08745Sheppo ldcp->next = ldcssp->chan_list; 23251ae08745Sheppo ldcssp->chan_list = ldcp; 23261ae08745Sheppo ldcssp->channel_count++; 23271ae08745Sheppo mutex_exit(&ldcssp->lock); 23281ae08745Sheppo 23291ae08745Sheppo /* set the handle */ 23301ae08745Sheppo *handle = (ldc_handle_t)ldcp; 23311ae08745Sheppo 23321ae08745Sheppo D1(ldcp->id, "ldc_init: (0x%llx) channel initialized\n", ldcp->id); 23331ae08745Sheppo 23341ae08745Sheppo return (0); 23351ae08745Sheppo 23361ae08745Sheppo cleanup_on_exit: 23371ae08745Sheppo 23381ae08745Sheppo if (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_bufferp) 23391ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 23401ae08745Sheppo 23411ae08745Sheppo if (ldcp->tstate & TS_TXQ_RDY) 23421ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 23431ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 23441ae08745Sheppo 23451ae08745Sheppo if (ldcp->tstate & TS_RXQ_RDY) 23461ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 23471ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 23481ae08745Sheppo 2349d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 23501ae08745Sheppo mutex_destroy(&ldcp->lock); 23511ae08745Sheppo 23521ae08745Sheppo if (ldcp) 23531ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 23541ae08745Sheppo 23551ae08745Sheppo return (exit_val); 23561ae08745Sheppo } 23571ae08745Sheppo 23581ae08745Sheppo /* 23591ae08745Sheppo * Finalizes the LDC connection. It will return EBUSY if the 23601ae08745Sheppo * channel is open. A ldc_close() has to be done prior to 23611ae08745Sheppo * a ldc_fini operation. It frees TX/RX queues, associated 23621ae08745Sheppo * with the channel 23631ae08745Sheppo */ 23641ae08745Sheppo int 23651ae08745Sheppo ldc_fini(ldc_handle_t handle) 23661ae08745Sheppo { 23671ae08745Sheppo ldc_chan_t *ldcp; 23681ae08745Sheppo ldc_chan_t *tmp_ldcp; 23691ae08745Sheppo uint64_t id; 23701ae08745Sheppo 23711ae08745Sheppo if (handle == NULL) { 23721ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel handle\n"); 23731ae08745Sheppo return (EINVAL); 23741ae08745Sheppo } 23751ae08745Sheppo ldcp = (ldc_chan_t *)handle; 23761ae08745Sheppo id = ldcp->id; 23771ae08745Sheppo 23781ae08745Sheppo mutex_enter(&ldcp->lock); 23791ae08745Sheppo 23803af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) > TS_INIT) { 23811ae08745Sheppo DWARN(ldcp->id, "ldc_fini: (0x%llx) channel is open\n", 23821ae08745Sheppo ldcp->id); 23831ae08745Sheppo mutex_exit(&ldcp->lock); 23841ae08745Sheppo return (EBUSY); 23851ae08745Sheppo } 23861ae08745Sheppo 23871ae08745Sheppo /* Remove from the channel list */ 23881ae08745Sheppo mutex_enter(&ldcssp->lock); 23891ae08745Sheppo tmp_ldcp = ldcssp->chan_list; 23901ae08745Sheppo if (tmp_ldcp == ldcp) { 23911ae08745Sheppo ldcssp->chan_list = ldcp->next; 23921ae08745Sheppo ldcp->next = NULL; 23931ae08745Sheppo } else { 23941ae08745Sheppo while (tmp_ldcp != NULL) { 23951ae08745Sheppo if (tmp_ldcp->next == ldcp) { 23961ae08745Sheppo tmp_ldcp->next = ldcp->next; 23971ae08745Sheppo ldcp->next = NULL; 23981ae08745Sheppo break; 23991ae08745Sheppo } 24001ae08745Sheppo tmp_ldcp = tmp_ldcp->next; 24011ae08745Sheppo } 24021ae08745Sheppo if (tmp_ldcp == NULL) { 24031ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel hdl\n"); 24041ae08745Sheppo mutex_exit(&ldcssp->lock); 24051ae08745Sheppo mutex_exit(&ldcp->lock); 24061ae08745Sheppo return (EINVAL); 24071ae08745Sheppo } 24081ae08745Sheppo } 24091ae08745Sheppo 24101ae08745Sheppo ldcssp->channel_count--; 24111ae08745Sheppo 24121ae08745Sheppo mutex_exit(&ldcssp->lock); 24131ae08745Sheppo 24141ae08745Sheppo /* Free the map table for this channel */ 24151ae08745Sheppo if (ldcp->mtbl) { 24161ae08745Sheppo (void) hv_ldc_set_map_table(ldcp->id, NULL, NULL); 24173af08d82Slm66018 if (ldcp->mtbl->contigmem) 24181ae08745Sheppo contig_mem_free(ldcp->mtbl->table, ldcp->mtbl->size); 24193af08d82Slm66018 else 24203af08d82Slm66018 kmem_free(ldcp->mtbl->table, ldcp->mtbl->size); 24211ae08745Sheppo mutex_destroy(&ldcp->mtbl->lock); 24221ae08745Sheppo kmem_free(ldcp->mtbl, sizeof (ldc_mtbl_t)); 24231ae08745Sheppo } 24241ae08745Sheppo 24251ae08745Sheppo /* Destroy descriptor ring and memory handle list lock */ 24261ae08745Sheppo mutex_destroy(&ldcp->exp_dlist_lock); 24271ae08745Sheppo mutex_destroy(&ldcp->imp_dlist_lock); 24281ae08745Sheppo mutex_destroy(&ldcp->mlist_lock); 24291ae08745Sheppo 24301ae08745Sheppo /* Free the stream buffer for STREAM_MODE */ 24311ae08745Sheppo if (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_bufferp) 24321ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 24331ae08745Sheppo 24341ae08745Sheppo /* Free the RX queue */ 24351ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 24361ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 24371ae08745Sheppo ldcp->tstate &= ~TS_RXQ_RDY; 24381ae08745Sheppo 24391ae08745Sheppo /* Free the TX queue */ 24401ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 24411ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 24421ae08745Sheppo ldcp->tstate &= ~TS_TXQ_RDY; 24431ae08745Sheppo 24441ae08745Sheppo mutex_exit(&ldcp->lock); 24451ae08745Sheppo 24461ae08745Sheppo /* Destroy mutex */ 2447d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 24481ae08745Sheppo mutex_destroy(&ldcp->lock); 24491ae08745Sheppo 24501ae08745Sheppo /* free channel structure */ 24511ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 24521ae08745Sheppo 24531ae08745Sheppo D1(id, "ldc_fini: (0x%llx) channel finalized\n", id); 24541ae08745Sheppo 24551ae08745Sheppo return (0); 24561ae08745Sheppo } 24571ae08745Sheppo 24581ae08745Sheppo /* 24591ae08745Sheppo * Open the LDC channel for use. It registers the TX/RX queues 24601ae08745Sheppo * with the Hypervisor. It also specifies the interrupt number 24611ae08745Sheppo * and target CPU for this channel 24621ae08745Sheppo */ 24631ae08745Sheppo int 24641ae08745Sheppo ldc_open(ldc_handle_t handle) 24651ae08745Sheppo { 24661ae08745Sheppo ldc_chan_t *ldcp; 24671ae08745Sheppo int rv; 24681ae08745Sheppo 24691ae08745Sheppo if (handle == NULL) { 24701ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_open: invalid channel handle\n"); 24711ae08745Sheppo return (EINVAL); 24721ae08745Sheppo } 24731ae08745Sheppo 24741ae08745Sheppo ldcp = (ldc_chan_t *)handle; 24751ae08745Sheppo 24761ae08745Sheppo mutex_enter(&ldcp->lock); 24771ae08745Sheppo 24781ae08745Sheppo if (ldcp->tstate < TS_INIT) { 24791ae08745Sheppo DWARN(ldcp->id, 24801ae08745Sheppo "ldc_open: (0x%llx) channel not initialized\n", ldcp->id); 24811ae08745Sheppo mutex_exit(&ldcp->lock); 24821ae08745Sheppo return (EFAULT); 24831ae08745Sheppo } 24843af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) >= TS_OPEN) { 24851ae08745Sheppo DWARN(ldcp->id, 24861ae08745Sheppo "ldc_open: (0x%llx) channel is already open\n", ldcp->id); 24871ae08745Sheppo mutex_exit(&ldcp->lock); 24881ae08745Sheppo return (EFAULT); 24891ae08745Sheppo } 24901ae08745Sheppo 24911ae08745Sheppo /* 24921ae08745Sheppo * Unregister/Register the tx queue with the hypervisor 24931ae08745Sheppo */ 24941ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 24951ae08745Sheppo if (rv) { 24961ae08745Sheppo cmn_err(CE_WARN, 24971ae08745Sheppo "ldc_open: (0x%lx) channel tx queue unconf failed\n", 24981ae08745Sheppo ldcp->id); 24991ae08745Sheppo mutex_exit(&ldcp->lock); 25001ae08745Sheppo return (EIO); 25011ae08745Sheppo } 25021ae08745Sheppo 25031ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 25041ae08745Sheppo if (rv) { 25051ae08745Sheppo cmn_err(CE_WARN, 25061ae08745Sheppo "ldc_open: (0x%lx) channel tx queue conf failed\n", 25071ae08745Sheppo ldcp->id); 25081ae08745Sheppo mutex_exit(&ldcp->lock); 25091ae08745Sheppo return (EIO); 25101ae08745Sheppo } 25111ae08745Sheppo 25121ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered tx queue with LDC\n", 25131ae08745Sheppo ldcp->id); 25141ae08745Sheppo 25151ae08745Sheppo /* 25161ae08745Sheppo * Unregister/Register the rx queue with the hypervisor 25171ae08745Sheppo */ 25181ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 25191ae08745Sheppo if (rv) { 25201ae08745Sheppo cmn_err(CE_WARN, 25211ae08745Sheppo "ldc_open: (0x%lx) channel rx queue unconf failed\n", 25221ae08745Sheppo ldcp->id); 25231ae08745Sheppo mutex_exit(&ldcp->lock); 25241ae08745Sheppo return (EIO); 25251ae08745Sheppo } 25261ae08745Sheppo 25271ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, ldcp->rx_q_entries); 25281ae08745Sheppo if (rv) { 25291ae08745Sheppo cmn_err(CE_WARN, 25301ae08745Sheppo "ldc_open: (0x%lx) channel rx queue conf failed\n", 25311ae08745Sheppo ldcp->id); 25321ae08745Sheppo mutex_exit(&ldcp->lock); 25331ae08745Sheppo return (EIO); 25341ae08745Sheppo } 25351ae08745Sheppo 25361ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered rx queue with LDC\n", 25371ae08745Sheppo ldcp->id); 25381ae08745Sheppo 25391ae08745Sheppo ldcp->tstate |= TS_QCONF_RDY; 25401ae08745Sheppo 25411ae08745Sheppo /* Register the channel with the channel nexus */ 25421ae08745Sheppo rv = i_ldc_register_channel(ldcp); 25431ae08745Sheppo if (rv && rv != EAGAIN) { 25441ae08745Sheppo cmn_err(CE_WARN, 25451ae08745Sheppo "ldc_open: (0x%lx) channel register failed\n", ldcp->id); 25461ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 25471ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 25481ae08745Sheppo mutex_exit(&ldcp->lock); 25491ae08745Sheppo return (EIO); 25501ae08745Sheppo } 25511ae08745Sheppo 25521ae08745Sheppo /* mark channel in OPEN state */ 25531ae08745Sheppo ldcp->status = LDC_OPEN; 25541ae08745Sheppo 25551ae08745Sheppo /* Read channel state */ 25561ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 25571ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 25581ae08745Sheppo if (rv) { 25591ae08745Sheppo cmn_err(CE_WARN, 25601ae08745Sheppo "ldc_open: (0x%lx) cannot read channel state\n", 25611ae08745Sheppo ldcp->id); 25621ae08745Sheppo (void) i_ldc_unregister_channel(ldcp); 25631ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 25641ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 25651ae08745Sheppo mutex_exit(&ldcp->lock); 25661ae08745Sheppo return (EIO); 25671ae08745Sheppo } 25681ae08745Sheppo 25691ae08745Sheppo /* 25701ae08745Sheppo * set the ACKd head to current head location for reliable & 25711ae08745Sheppo * streaming mode 25721ae08745Sheppo */ 25731ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 25741ae08745Sheppo 25751ae08745Sheppo /* mark channel ready if HV report link is UP (peer alloc'd Rx queue) */ 25761ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 25771ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 25781ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 25791ae08745Sheppo ldcp->status = LDC_READY; 25801ae08745Sheppo } 25811ae08745Sheppo 25821ae08745Sheppo /* 25831ae08745Sheppo * if channel is being opened in RAW mode - no handshake is needed 25841ae08745Sheppo * switch the channel READY and UP state 25851ae08745Sheppo */ 25861ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 25871ae08745Sheppo ldcp->tstate = TS_UP; /* set bits associated with LDC UP */ 25881ae08745Sheppo ldcp->status = LDC_UP; 25891ae08745Sheppo } 25901ae08745Sheppo 25911ae08745Sheppo mutex_exit(&ldcp->lock); 25921ae08745Sheppo 25931ae08745Sheppo /* 25941ae08745Sheppo * Increment number of open channels 25951ae08745Sheppo */ 25961ae08745Sheppo mutex_enter(&ldcssp->lock); 25971ae08745Sheppo ldcssp->channels_open++; 25981ae08745Sheppo mutex_exit(&ldcssp->lock); 25991ae08745Sheppo 26003af08d82Slm66018 DWARN(ldcp->id, 26013af08d82Slm66018 "ldc_open: (0x%llx) channel (0x%p) open for use " 26023af08d82Slm66018 "(tstate=0x%x, status=0x%x)\n", 26033af08d82Slm66018 ldcp->id, ldcp, ldcp->tstate, ldcp->status); 26041ae08745Sheppo 26051ae08745Sheppo return (0); 26061ae08745Sheppo } 26071ae08745Sheppo 26081ae08745Sheppo /* 26091ae08745Sheppo * Close the LDC connection. It will return EBUSY if there 26101ae08745Sheppo * are memory segments or descriptor rings either bound to or 26111ae08745Sheppo * mapped over the channel 26121ae08745Sheppo */ 26131ae08745Sheppo int 26141ae08745Sheppo ldc_close(ldc_handle_t handle) 26151ae08745Sheppo { 26161ae08745Sheppo ldc_chan_t *ldcp; 2617d10e4ef2Snarayan int rv = 0, retries = 0; 26181ae08745Sheppo boolean_t chk_done = B_FALSE; 26191ae08745Sheppo 26201ae08745Sheppo if (handle == NULL) { 26211ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_close: invalid channel handle\n"); 26221ae08745Sheppo return (EINVAL); 26231ae08745Sheppo } 26241ae08745Sheppo ldcp = (ldc_chan_t *)handle; 26251ae08745Sheppo 26261ae08745Sheppo mutex_enter(&ldcp->lock); 26271ae08745Sheppo 26281ae08745Sheppo /* return error if channel is not open */ 26293af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) < TS_OPEN) { 26301ae08745Sheppo DWARN(ldcp->id, 26311ae08745Sheppo "ldc_close: (0x%llx) channel is not open\n", ldcp->id); 26321ae08745Sheppo mutex_exit(&ldcp->lock); 26331ae08745Sheppo return (EFAULT); 26341ae08745Sheppo } 26351ae08745Sheppo 26361ae08745Sheppo /* if any memory handles, drings, are bound or mapped cannot close */ 26371ae08745Sheppo if (ldcp->mhdl_list != NULL) { 26381ae08745Sheppo DWARN(ldcp->id, 26391ae08745Sheppo "ldc_close: (0x%llx) channel has bound memory handles\n", 26401ae08745Sheppo ldcp->id); 26411ae08745Sheppo mutex_exit(&ldcp->lock); 26421ae08745Sheppo return (EBUSY); 26431ae08745Sheppo } 26441ae08745Sheppo if (ldcp->exp_dring_list != NULL) { 26451ae08745Sheppo DWARN(ldcp->id, 26461ae08745Sheppo "ldc_close: (0x%llx) channel has bound descriptor rings\n", 26471ae08745Sheppo ldcp->id); 26481ae08745Sheppo mutex_exit(&ldcp->lock); 26491ae08745Sheppo return (EBUSY); 26501ae08745Sheppo } 26511ae08745Sheppo if (ldcp->imp_dring_list != NULL) { 26521ae08745Sheppo DWARN(ldcp->id, 26531ae08745Sheppo "ldc_close: (0x%llx) channel has mapped descriptor rings\n", 26541ae08745Sheppo ldcp->id); 26551ae08745Sheppo mutex_exit(&ldcp->lock); 26561ae08745Sheppo return (EBUSY); 26571ae08745Sheppo } 26581ae08745Sheppo 2659d10e4ef2Snarayan /* Obtain Tx lock */ 2660d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2661d10e4ef2Snarayan 26621ae08745Sheppo /* 26631ae08745Sheppo * Wait for pending transmits to complete i.e Tx queue to drain 26641ae08745Sheppo * if there are pending pkts - wait 1 ms and retry again 26651ae08745Sheppo */ 26661ae08745Sheppo for (;;) { 26671ae08745Sheppo 26681ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 26691ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 26701ae08745Sheppo if (rv) { 26711ae08745Sheppo cmn_err(CE_WARN, 26721ae08745Sheppo "ldc_close: (0x%lx) cannot read qptrs\n", ldcp->id); 2673d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 26741ae08745Sheppo mutex_exit(&ldcp->lock); 26751ae08745Sheppo return (EIO); 26761ae08745Sheppo } 26771ae08745Sheppo 26781ae08745Sheppo if (ldcp->tx_head == ldcp->tx_tail || 26791ae08745Sheppo ldcp->link_state != LDC_CHANNEL_UP) { 26801ae08745Sheppo break; 26811ae08745Sheppo } 26821ae08745Sheppo 26831ae08745Sheppo if (chk_done) { 26841ae08745Sheppo DWARN(ldcp->id, 26851ae08745Sheppo "ldc_close: (0x%llx) Tx queue drain timeout\n", 26861ae08745Sheppo ldcp->id); 26871ae08745Sheppo break; 26881ae08745Sheppo } 26891ae08745Sheppo 26901ae08745Sheppo /* wait for one ms and try again */ 26911ae08745Sheppo delay(drv_usectohz(1000)); 26921ae08745Sheppo chk_done = B_TRUE; 26931ae08745Sheppo } 26941ae08745Sheppo 26951ae08745Sheppo /* 2696*a8ea4edeSnarayan * Drain the Tx and Rx queues as we are closing the 2697*a8ea4edeSnarayan * channel. We dont care about any pending packets. 2698*a8ea4edeSnarayan * We have to also drain the queue prior to clearing 2699*a8ea4edeSnarayan * pending interrupts, otherwise the HV will trigger 2700*a8ea4edeSnarayan * an interrupt the moment the interrupt state is 2701*a8ea4edeSnarayan * cleared. 27023af08d82Slm66018 */ 27033af08d82Slm66018 (void) i_ldc_txq_reconf(ldcp); 2704*a8ea4edeSnarayan (void) i_ldc_rxq_drain(ldcp); 27053af08d82Slm66018 27063af08d82Slm66018 /* 27071ae08745Sheppo * Unregister the channel with the nexus 27081ae08745Sheppo */ 2709d10e4ef2Snarayan while ((rv = i_ldc_unregister_channel(ldcp)) != 0) { 2710d10e4ef2Snarayan 2711d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 27121ae08745Sheppo mutex_exit(&ldcp->lock); 2713d10e4ef2Snarayan 2714d10e4ef2Snarayan /* if any error other than EAGAIN return back */ 2715*a8ea4edeSnarayan if (rv != EAGAIN || retries >= ldc_max_retries) { 2716d10e4ef2Snarayan cmn_err(CE_WARN, 2717d10e4ef2Snarayan "ldc_close: (0x%lx) unregister failed, %d\n", 2718d10e4ef2Snarayan ldcp->id, rv); 27191ae08745Sheppo return (rv); 27201ae08745Sheppo } 27211ae08745Sheppo 27221ae08745Sheppo /* 2723d10e4ef2Snarayan * As there could be pending interrupts we need 2724d10e4ef2Snarayan * to wait and try again 2725d10e4ef2Snarayan */ 2726*a8ea4edeSnarayan drv_usecwait(ldc_delay); 2727d10e4ef2Snarayan mutex_enter(&ldcp->lock); 2728d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2729d10e4ef2Snarayan retries++; 2730d10e4ef2Snarayan } 2731d10e4ef2Snarayan 2732d10e4ef2Snarayan /* 27331ae08745Sheppo * Unregister queues 27341ae08745Sheppo */ 27351ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 27361ae08745Sheppo if (rv) { 27371ae08745Sheppo cmn_err(CE_WARN, 27381ae08745Sheppo "ldc_close: (0x%lx) channel TX queue unconf failed\n", 27391ae08745Sheppo ldcp->id); 2740d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 27411ae08745Sheppo mutex_exit(&ldcp->lock); 27421ae08745Sheppo return (EIO); 27431ae08745Sheppo } 27441ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 27451ae08745Sheppo if (rv) { 27461ae08745Sheppo cmn_err(CE_WARN, 27471ae08745Sheppo "ldc_close: (0x%lx) channel RX queue unconf failed\n", 27481ae08745Sheppo ldcp->id); 2749d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 27501ae08745Sheppo mutex_exit(&ldcp->lock); 27511ae08745Sheppo return (EIO); 27521ae08745Sheppo } 27531ae08745Sheppo 27541ae08745Sheppo ldcp->tstate &= ~TS_QCONF_RDY; 27551ae08745Sheppo 27561ae08745Sheppo /* Reset channel state information */ 27571ae08745Sheppo i_ldc_reset_state(ldcp); 27581ae08745Sheppo 27591ae08745Sheppo /* Mark channel as down and in initialized state */ 27601ae08745Sheppo ldcp->tx_ackd_head = 0; 27611ae08745Sheppo ldcp->tx_head = 0; 27623af08d82Slm66018 ldcp->tstate = TS_IN_RESET|TS_INIT; 27631ae08745Sheppo ldcp->status = LDC_INIT; 27641ae08745Sheppo 2765d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 27661ae08745Sheppo mutex_exit(&ldcp->lock); 27671ae08745Sheppo 27681ae08745Sheppo /* Decrement number of open channels */ 27691ae08745Sheppo mutex_enter(&ldcssp->lock); 27701ae08745Sheppo ldcssp->channels_open--; 27711ae08745Sheppo mutex_exit(&ldcssp->lock); 27721ae08745Sheppo 27731ae08745Sheppo D1(ldcp->id, "ldc_close: (0x%llx) channel closed\n", ldcp->id); 27741ae08745Sheppo 27751ae08745Sheppo return (0); 27761ae08745Sheppo } 27771ae08745Sheppo 27781ae08745Sheppo /* 27791ae08745Sheppo * Register channel callback 27801ae08745Sheppo */ 27811ae08745Sheppo int 27821ae08745Sheppo ldc_reg_callback(ldc_handle_t handle, 27831ae08745Sheppo uint_t(*cb)(uint64_t event, caddr_t arg), caddr_t arg) 27841ae08745Sheppo { 27851ae08745Sheppo ldc_chan_t *ldcp; 27861ae08745Sheppo 27871ae08745Sheppo if (handle == NULL) { 27881ae08745Sheppo DWARN(DBG_ALL_LDCS, 27891ae08745Sheppo "ldc_reg_callback: invalid channel handle\n"); 27901ae08745Sheppo return (EINVAL); 27911ae08745Sheppo } 27921ae08745Sheppo if (((uint64_t)cb) < KERNELBASE) { 27931ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_reg_callback: invalid callback\n"); 27941ae08745Sheppo return (EINVAL); 27951ae08745Sheppo } 27961ae08745Sheppo ldcp = (ldc_chan_t *)handle; 27971ae08745Sheppo 27981ae08745Sheppo mutex_enter(&ldcp->lock); 27991ae08745Sheppo 28001ae08745Sheppo if (ldcp->cb) { 28011ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback exists\n", 28021ae08745Sheppo ldcp->id); 28031ae08745Sheppo mutex_exit(&ldcp->lock); 28041ae08745Sheppo return (EIO); 28051ae08745Sheppo } 28061ae08745Sheppo if (ldcp->cb_inprogress) { 28071ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback active\n", 28081ae08745Sheppo ldcp->id); 28091ae08745Sheppo mutex_exit(&ldcp->lock); 28101ae08745Sheppo return (EWOULDBLOCK); 28111ae08745Sheppo } 28121ae08745Sheppo 28131ae08745Sheppo ldcp->cb = cb; 28141ae08745Sheppo ldcp->cb_arg = arg; 28151ae08745Sheppo ldcp->cb_enabled = B_TRUE; 28161ae08745Sheppo 28171ae08745Sheppo D1(ldcp->id, 28181ae08745Sheppo "ldc_reg_callback: (0x%llx) registered callback for channel\n", 28191ae08745Sheppo ldcp->id); 28201ae08745Sheppo 28211ae08745Sheppo mutex_exit(&ldcp->lock); 28221ae08745Sheppo 28231ae08745Sheppo return (0); 28241ae08745Sheppo } 28251ae08745Sheppo 28261ae08745Sheppo /* 28271ae08745Sheppo * Unregister channel callback 28281ae08745Sheppo */ 28291ae08745Sheppo int 28301ae08745Sheppo ldc_unreg_callback(ldc_handle_t handle) 28311ae08745Sheppo { 28321ae08745Sheppo ldc_chan_t *ldcp; 28331ae08745Sheppo 28341ae08745Sheppo if (handle == NULL) { 28351ae08745Sheppo DWARN(DBG_ALL_LDCS, 28361ae08745Sheppo "ldc_unreg_callback: invalid channel handle\n"); 28371ae08745Sheppo return (EINVAL); 28381ae08745Sheppo } 28391ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28401ae08745Sheppo 28411ae08745Sheppo mutex_enter(&ldcp->lock); 28421ae08745Sheppo 28431ae08745Sheppo if (ldcp->cb == NULL) { 28441ae08745Sheppo DWARN(ldcp->id, 28451ae08745Sheppo "ldc_unreg_callback: (0x%llx) no callback exists\n", 28461ae08745Sheppo ldcp->id); 28471ae08745Sheppo mutex_exit(&ldcp->lock); 28481ae08745Sheppo return (EIO); 28491ae08745Sheppo } 28501ae08745Sheppo if (ldcp->cb_inprogress) { 28511ae08745Sheppo DWARN(ldcp->id, 28521ae08745Sheppo "ldc_unreg_callback: (0x%llx) callback active\n", 28531ae08745Sheppo ldcp->id); 28541ae08745Sheppo mutex_exit(&ldcp->lock); 28551ae08745Sheppo return (EWOULDBLOCK); 28561ae08745Sheppo } 28571ae08745Sheppo 28581ae08745Sheppo ldcp->cb = NULL; 28591ae08745Sheppo ldcp->cb_arg = NULL; 28601ae08745Sheppo ldcp->cb_enabled = B_FALSE; 28611ae08745Sheppo 28621ae08745Sheppo D1(ldcp->id, 28631ae08745Sheppo "ldc_unreg_callback: (0x%llx) unregistered callback for channel\n", 28641ae08745Sheppo ldcp->id); 28651ae08745Sheppo 28661ae08745Sheppo mutex_exit(&ldcp->lock); 28671ae08745Sheppo 28681ae08745Sheppo return (0); 28691ae08745Sheppo } 28701ae08745Sheppo 28711ae08745Sheppo 28721ae08745Sheppo /* 28731ae08745Sheppo * Bring a channel up by initiating a handshake with the peer 28741ae08745Sheppo * This call is asynchronous. It will complete at a later point 28751ae08745Sheppo * in time when the peer responds back with an RTR. 28761ae08745Sheppo */ 28771ae08745Sheppo int 28781ae08745Sheppo ldc_up(ldc_handle_t handle) 28791ae08745Sheppo { 28801ae08745Sheppo int rv; 28811ae08745Sheppo ldc_chan_t *ldcp; 28821ae08745Sheppo ldc_msg_t *ldcmsg; 28833af08d82Slm66018 uint64_t tx_tail, tstate; 28841ae08745Sheppo 28851ae08745Sheppo if (handle == NULL) { 28861ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_up: invalid channel handle\n"); 28871ae08745Sheppo return (EINVAL); 28881ae08745Sheppo } 28891ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28901ae08745Sheppo 28911ae08745Sheppo mutex_enter(&ldcp->lock); 28921ae08745Sheppo 28933af08d82Slm66018 D1(ldcp->id, "ldc_up: (0x%llx) doing channel UP\n", ldcp->id); 28943af08d82Slm66018 28953af08d82Slm66018 /* clear the reset state */ 28963af08d82Slm66018 tstate = ldcp->tstate; 28973af08d82Slm66018 ldcp->tstate &= ~TS_IN_RESET; 28983af08d82Slm66018 28991ae08745Sheppo if (ldcp->tstate == TS_UP) { 29003af08d82Slm66018 DWARN(ldcp->id, 29011ae08745Sheppo "ldc_up: (0x%llx) channel is already in UP state\n", 29021ae08745Sheppo ldcp->id); 29033af08d82Slm66018 29043af08d82Slm66018 /* mark channel as up */ 29053af08d82Slm66018 ldcp->status = LDC_UP; 29063af08d82Slm66018 29073af08d82Slm66018 /* 29083af08d82Slm66018 * if channel was in reset state and there was 29093af08d82Slm66018 * pending data clear interrupt state. this will 29103af08d82Slm66018 * trigger an interrupt, causing the RX handler to 29113af08d82Slm66018 * to invoke the client's callback 29123af08d82Slm66018 */ 29133af08d82Slm66018 if ((tstate & TS_IN_RESET) && 29143af08d82Slm66018 ldcp->rx_intr_state == LDC_INTR_PEND) { 29153af08d82Slm66018 DWARN(ldcp->id, 29163af08d82Slm66018 "ldc_up: (0x%llx) channel has pending data, " 29173af08d82Slm66018 "clearing interrupt\n", ldcp->id); 29183af08d82Slm66018 i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 29193af08d82Slm66018 } 29203af08d82Slm66018 29211ae08745Sheppo mutex_exit(&ldcp->lock); 29221ae08745Sheppo return (0); 29231ae08745Sheppo } 29241ae08745Sheppo 29251ae08745Sheppo /* if the channel is in RAW mode - mark it as UP, if READY */ 29261ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW && ldcp->tstate >= TS_READY) { 29271ae08745Sheppo ldcp->tstate = TS_UP; 29281ae08745Sheppo mutex_exit(&ldcp->lock); 29291ae08745Sheppo return (0); 29301ae08745Sheppo } 29311ae08745Sheppo 29321ae08745Sheppo /* Don't start another handshake if there is one in progress */ 29331ae08745Sheppo if (ldcp->hstate) { 29343af08d82Slm66018 D1(ldcp->id, 29351ae08745Sheppo "ldc_up: (0x%llx) channel handshake in progress\n", 29361ae08745Sheppo ldcp->id); 29371ae08745Sheppo mutex_exit(&ldcp->lock); 29381ae08745Sheppo return (0); 29391ae08745Sheppo } 29401ae08745Sheppo 2941d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2942d10e4ef2Snarayan 29431ae08745Sheppo /* get the current tail for the LDC msg */ 29441ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 29451ae08745Sheppo if (rv) { 29461ae08745Sheppo DWARN(ldcp->id, "ldc_up: (0x%llx) cannot initiate handshake\n", 29471ae08745Sheppo ldcp->id); 2948d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 29491ae08745Sheppo mutex_exit(&ldcp->lock); 29501ae08745Sheppo return (ECONNREFUSED); 29511ae08745Sheppo } 29521ae08745Sheppo 29531ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 29541ae08745Sheppo ZERO_PKT(ldcmsg); 29551ae08745Sheppo 29561ae08745Sheppo ldcmsg->type = LDC_CTRL; 29571ae08745Sheppo ldcmsg->stype = LDC_INFO; 29581ae08745Sheppo ldcmsg->ctrl = LDC_VER; 29591ae08745Sheppo ldcp->next_vidx = 0; 29601ae08745Sheppo bcopy(&ldc_versions[0], ldcmsg->udata, sizeof (ldc_versions[0])); 29611ae08745Sheppo 29621ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_up snd ver", (uint64_t)ldcmsg); 29631ae08745Sheppo 29641ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 29651ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 29661ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 29671ae08745Sheppo 29681ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 29691ae08745Sheppo if (rv) { 29701ae08745Sheppo DWARN(ldcp->id, 29711ae08745Sheppo "ldc_up: (0x%llx) cannot initiate handshake rv=%d\n", 29721ae08745Sheppo ldcp->id, rv); 2973d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 29741ae08745Sheppo mutex_exit(&ldcp->lock); 29751ae08745Sheppo return (rv); 29761ae08745Sheppo } 29771ae08745Sheppo 29780a55fbb7Slm66018 ldcp->hstate |= TS_SENT_VER; 29791ae08745Sheppo ldcp->tx_tail = tx_tail; 29801ae08745Sheppo D1(ldcp->id, "ldc_up: (0x%llx) channel up initiated\n", ldcp->id); 29811ae08745Sheppo 2982d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 29831ae08745Sheppo mutex_exit(&ldcp->lock); 29841ae08745Sheppo 29851ae08745Sheppo return (rv); 29861ae08745Sheppo } 29871ae08745Sheppo 29881ae08745Sheppo 29891ae08745Sheppo /* 2990e1ebb9ecSlm66018 * Bring a channel down by resetting its state and queues 29911ae08745Sheppo */ 29921ae08745Sheppo int 2993e1ebb9ecSlm66018 ldc_down(ldc_handle_t handle) 29941ae08745Sheppo { 29951ae08745Sheppo ldc_chan_t *ldcp; 29961ae08745Sheppo 29971ae08745Sheppo if (handle == NULL) { 2998e1ebb9ecSlm66018 DWARN(DBG_ALL_LDCS, "ldc_down: invalid channel handle\n"); 29991ae08745Sheppo return (EINVAL); 30001ae08745Sheppo } 30011ae08745Sheppo ldcp = (ldc_chan_t *)handle; 30021ae08745Sheppo mutex_enter(&ldcp->lock); 3003d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 30043af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 3005d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 30061ae08745Sheppo mutex_exit(&ldcp->lock); 30071ae08745Sheppo 30081ae08745Sheppo return (0); 30091ae08745Sheppo } 30101ae08745Sheppo 30111ae08745Sheppo /* 30121ae08745Sheppo * Get the current channel status 30131ae08745Sheppo */ 30141ae08745Sheppo int 30151ae08745Sheppo ldc_status(ldc_handle_t handle, ldc_status_t *status) 30161ae08745Sheppo { 30171ae08745Sheppo ldc_chan_t *ldcp; 30181ae08745Sheppo 30191ae08745Sheppo if (handle == NULL || status == NULL) { 30201ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_status: invalid argument\n"); 30211ae08745Sheppo return (EINVAL); 30221ae08745Sheppo } 30231ae08745Sheppo ldcp = (ldc_chan_t *)handle; 30241ae08745Sheppo 30251ae08745Sheppo *status = ((ldc_chan_t *)handle)->status; 30261ae08745Sheppo 30273af08d82Slm66018 DWARN(ldcp->id, 30281ae08745Sheppo "ldc_status: (0x%llx) returned status %d\n", ldcp->id, *status); 30291ae08745Sheppo return (0); 30301ae08745Sheppo } 30311ae08745Sheppo 30321ae08745Sheppo 30331ae08745Sheppo /* 30341ae08745Sheppo * Set the channel's callback mode - enable/disable callbacks 30351ae08745Sheppo */ 30361ae08745Sheppo int 30371ae08745Sheppo ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t cmode) 30381ae08745Sheppo { 30391ae08745Sheppo ldc_chan_t *ldcp; 30401ae08745Sheppo 30411ae08745Sheppo if (handle == NULL) { 30421ae08745Sheppo DWARN(DBG_ALL_LDCS, 30431ae08745Sheppo "ldc_set_intr_mode: invalid channel handle\n"); 30441ae08745Sheppo return (EINVAL); 30451ae08745Sheppo } 30461ae08745Sheppo ldcp = (ldc_chan_t *)handle; 30471ae08745Sheppo 30481ae08745Sheppo /* 30491ae08745Sheppo * Record no callbacks should be invoked 30501ae08745Sheppo */ 30511ae08745Sheppo mutex_enter(&ldcp->lock); 30521ae08745Sheppo 30531ae08745Sheppo switch (cmode) { 30541ae08745Sheppo case LDC_CB_DISABLE: 30551ae08745Sheppo if (!ldcp->cb_enabled) { 30561ae08745Sheppo DWARN(ldcp->id, 30571ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks disabled\n", 30581ae08745Sheppo ldcp->id); 30591ae08745Sheppo break; 30601ae08745Sheppo } 30611ae08745Sheppo ldcp->cb_enabled = B_FALSE; 30621ae08745Sheppo 30631ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) disabled callbacks\n", 30641ae08745Sheppo ldcp->id); 30651ae08745Sheppo break; 30661ae08745Sheppo 30671ae08745Sheppo case LDC_CB_ENABLE: 30681ae08745Sheppo if (ldcp->cb_enabled) { 30691ae08745Sheppo DWARN(ldcp->id, 30701ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks enabled\n", 30711ae08745Sheppo ldcp->id); 30721ae08745Sheppo break; 30731ae08745Sheppo } 30741ae08745Sheppo ldcp->cb_enabled = B_TRUE; 30751ae08745Sheppo 30761ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) enabled callbacks\n", 30771ae08745Sheppo ldcp->id); 30781ae08745Sheppo break; 30791ae08745Sheppo } 30801ae08745Sheppo 30811ae08745Sheppo mutex_exit(&ldcp->lock); 30821ae08745Sheppo 30831ae08745Sheppo return (0); 30841ae08745Sheppo } 30851ae08745Sheppo 30861ae08745Sheppo /* 30871ae08745Sheppo * Check to see if there are packets on the incoming queue 3088e1ebb9ecSlm66018 * Will return hasdata = B_FALSE if there are no packets 30891ae08745Sheppo */ 30901ae08745Sheppo int 3091e1ebb9ecSlm66018 ldc_chkq(ldc_handle_t handle, boolean_t *hasdata) 30921ae08745Sheppo { 30931ae08745Sheppo int rv; 30941ae08745Sheppo uint64_t rx_head, rx_tail; 30951ae08745Sheppo ldc_chan_t *ldcp; 30961ae08745Sheppo 30971ae08745Sheppo if (handle == NULL) { 30981ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_chkq: invalid channel handle\n"); 30991ae08745Sheppo return (EINVAL); 31001ae08745Sheppo } 31011ae08745Sheppo ldcp = (ldc_chan_t *)handle; 31021ae08745Sheppo 3103e1ebb9ecSlm66018 *hasdata = B_FALSE; 31041ae08745Sheppo 31051ae08745Sheppo mutex_enter(&ldcp->lock); 31061ae08745Sheppo 31071ae08745Sheppo if (ldcp->tstate != TS_UP) { 31081ae08745Sheppo D1(ldcp->id, 31091ae08745Sheppo "ldc_chkq: (0x%llx) channel is not up\n", ldcp->id); 31101ae08745Sheppo mutex_exit(&ldcp->lock); 31111ae08745Sheppo return (ECONNRESET); 31121ae08745Sheppo } 31131ae08745Sheppo 31141ae08745Sheppo /* Read packet(s) from the queue */ 31151ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 31161ae08745Sheppo &ldcp->link_state); 31171ae08745Sheppo if (rv != 0) { 31181ae08745Sheppo cmn_err(CE_WARN, 31191ae08745Sheppo "ldc_chkq: (0x%lx) unable to read queue ptrs", ldcp->id); 31201ae08745Sheppo mutex_exit(&ldcp->lock); 31211ae08745Sheppo return (EIO); 31221ae08745Sheppo } 31231ae08745Sheppo /* reset the channel state if the channel went down */ 31241ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 31251ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 3126d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 31273af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3128d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 31291ae08745Sheppo mutex_exit(&ldcp->lock); 31301ae08745Sheppo return (ECONNRESET); 31311ae08745Sheppo } 31321ae08745Sheppo 31334bac2208Snarayan if ((rx_head != rx_tail) || 31344bac2208Snarayan (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_remains > 0)) { 31354bac2208Snarayan D1(ldcp->id, 31364bac2208Snarayan "ldc_chkq: (0x%llx) queue has pkt(s) or buffered data\n", 31374bac2208Snarayan ldcp->id); 3138e1ebb9ecSlm66018 *hasdata = B_TRUE; 31391ae08745Sheppo } 31401ae08745Sheppo 31411ae08745Sheppo mutex_exit(&ldcp->lock); 31421ae08745Sheppo 31431ae08745Sheppo return (0); 31441ae08745Sheppo } 31451ae08745Sheppo 31461ae08745Sheppo 31471ae08745Sheppo /* 31481ae08745Sheppo * Read 'size' amount of bytes or less. If incoming buffer 31491ae08745Sheppo * is more than 'size', ENOBUFS is returned. 31501ae08745Sheppo * 31511ae08745Sheppo * On return, size contains the number of bytes read. 31521ae08745Sheppo */ 31531ae08745Sheppo int 31541ae08745Sheppo ldc_read(ldc_handle_t handle, caddr_t bufp, size_t *sizep) 31551ae08745Sheppo { 31561ae08745Sheppo ldc_chan_t *ldcp; 31571ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 31581ae08745Sheppo int rv = 0, exit_val; 31591ae08745Sheppo 31601ae08745Sheppo if (handle == NULL) { 31611ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_read: invalid channel handle\n"); 31621ae08745Sheppo return (EINVAL); 31631ae08745Sheppo } 31641ae08745Sheppo 31651ae08745Sheppo ldcp = (ldc_chan_t *)handle; 31661ae08745Sheppo 31671ae08745Sheppo /* channel lock */ 31681ae08745Sheppo mutex_enter(&ldcp->lock); 31691ae08745Sheppo 31701ae08745Sheppo if (ldcp->tstate != TS_UP) { 31711ae08745Sheppo DWARN(ldcp->id, 31721ae08745Sheppo "ldc_read: (0x%llx) channel is not in UP state\n", 31731ae08745Sheppo ldcp->id); 31741ae08745Sheppo exit_val = ECONNRESET; 31751ae08745Sheppo } else { 31761ae08745Sheppo exit_val = ldcp->read_p(ldcp, bufp, sizep); 31771ae08745Sheppo } 31781ae08745Sheppo 31791ae08745Sheppo /* 31801ae08745Sheppo * if queue has been drained - clear interrupt 31811ae08745Sheppo */ 31821ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 31831ae08745Sheppo &ldcp->link_state); 31843af08d82Slm66018 31853af08d82Slm66018 ASSERT(rv == 0); 31863af08d82Slm66018 31873af08d82Slm66018 if (exit_val == 0) { 31883af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 31893af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 31903af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 31913af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 31923af08d82Slm66018 exit_val = ECONNRESET; 31933af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 31943af08d82Slm66018 } 31953af08d82Slm66018 if ((rv == 0) && 31963af08d82Slm66018 (ldcp->rx_intr_state == LDC_INTR_PEND) && 31973af08d82Slm66018 (rx_head == rx_tail)) { 31981ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 31991ae08745Sheppo } 32003af08d82Slm66018 } 32011ae08745Sheppo 32021ae08745Sheppo mutex_exit(&ldcp->lock); 32031ae08745Sheppo return (exit_val); 32041ae08745Sheppo } 32051ae08745Sheppo 32061ae08745Sheppo /* 32071ae08745Sheppo * Basic raw mondo read - 32081ae08745Sheppo * no interpretation of mondo contents at all. 32091ae08745Sheppo * 32101ae08745Sheppo * Enter and exit with ldcp->lock held by caller 32111ae08745Sheppo */ 32121ae08745Sheppo static int 32131ae08745Sheppo i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 32141ae08745Sheppo { 32151ae08745Sheppo uint64_t q_size_mask; 32161ae08745Sheppo ldc_msg_t *msgp; 32171ae08745Sheppo uint8_t *msgbufp; 32181ae08745Sheppo int rv = 0, space; 32191ae08745Sheppo uint64_t rx_head, rx_tail; 32201ae08745Sheppo 32211ae08745Sheppo space = *sizep; 32221ae08745Sheppo 32231ae08745Sheppo if (space < LDC_PAYLOAD_SIZE_RAW) 32241ae08745Sheppo return (ENOBUFS); 32251ae08745Sheppo 32261ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 32271ae08745Sheppo 32281ae08745Sheppo /* compute mask for increment */ 32291ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 32301ae08745Sheppo 32311ae08745Sheppo /* 32321ae08745Sheppo * Read packet(s) from the queue 32331ae08745Sheppo */ 32341ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 32351ae08745Sheppo &ldcp->link_state); 32361ae08745Sheppo if (rv != 0) { 32371ae08745Sheppo cmn_err(CE_WARN, 32381ae08745Sheppo "ldc_read_raw: (0x%lx) unable to read queue ptrs", 32391ae08745Sheppo ldcp->id); 32401ae08745Sheppo return (EIO); 32411ae08745Sheppo } 32421ae08745Sheppo D1(ldcp->id, "ldc_read_raw: (0x%llx) rxh=0x%llx," 32431ae08745Sheppo " rxt=0x%llx, st=0x%llx\n", 32441ae08745Sheppo ldcp->id, rx_head, rx_tail, ldcp->link_state); 32451ae08745Sheppo 32461ae08745Sheppo /* reset the channel state if the channel went down */ 32473af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 32483af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 3249d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 32503af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3251d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 32521ae08745Sheppo return (ECONNRESET); 32531ae08745Sheppo } 32541ae08745Sheppo 32551ae08745Sheppo /* 32561ae08745Sheppo * Check for empty queue 32571ae08745Sheppo */ 32581ae08745Sheppo if (rx_head == rx_tail) { 32591ae08745Sheppo *sizep = 0; 32601ae08745Sheppo return (0); 32611ae08745Sheppo } 32621ae08745Sheppo 32631ae08745Sheppo /* get the message */ 32641ae08745Sheppo msgp = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 32651ae08745Sheppo 32661ae08745Sheppo /* if channel is in RAW mode, copy data and return */ 32671ae08745Sheppo msgbufp = (uint8_t *)&(msgp->raw[0]); 32681ae08745Sheppo 32691ae08745Sheppo bcopy(msgbufp, target_bufp, LDC_PAYLOAD_SIZE_RAW); 32701ae08745Sheppo 32711ae08745Sheppo DUMP_PAYLOAD(ldcp->id, msgbufp); 32721ae08745Sheppo 32731ae08745Sheppo *sizep = LDC_PAYLOAD_SIZE_RAW; 32741ae08745Sheppo 32751ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) & q_size_mask; 32760a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_head); 32771ae08745Sheppo 32781ae08745Sheppo return (rv); 32791ae08745Sheppo } 32801ae08745Sheppo 32811ae08745Sheppo /* 32821ae08745Sheppo * Process LDC mondos to build larger packets 32831ae08745Sheppo * with either un-reliable or reliable delivery. 32841ae08745Sheppo * 32851ae08745Sheppo * Enter and exit with ldcp->lock held by caller 32861ae08745Sheppo */ 32871ae08745Sheppo static int 32881ae08745Sheppo i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 32891ae08745Sheppo { 32901ae08745Sheppo int rv = 0; 32911ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 32921ae08745Sheppo uint64_t curr_head = 0; 32931ae08745Sheppo ldc_msg_t *msg; 32941ae08745Sheppo caddr_t target; 32951ae08745Sheppo size_t len = 0, bytes_read = 0; 32960a55fbb7Slm66018 int retries = 0; 32971ae08745Sheppo uint64_t q_size_mask; 3298d10e4ef2Snarayan uint64_t first_fragment = 0; 32991ae08745Sheppo 33001ae08745Sheppo target = target_bufp; 33011ae08745Sheppo 33021ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 33031ae08745Sheppo 33043af08d82Slm66018 /* check if the buffer and size are valid */ 33053af08d82Slm66018 if (target_bufp == NULL || *sizep == 0) { 33063af08d82Slm66018 DWARN(ldcp->id, "ldc_read: (0x%llx) invalid buffer/size\n", 33073af08d82Slm66018 ldcp->id); 33083af08d82Slm66018 return (EINVAL); 33093af08d82Slm66018 } 33103af08d82Slm66018 33111ae08745Sheppo /* compute mask for increment */ 33121ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 33131ae08745Sheppo 33141ae08745Sheppo /* 33151ae08745Sheppo * Read packet(s) from the queue 33161ae08745Sheppo */ 33171ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &curr_head, &rx_tail, 33181ae08745Sheppo &ldcp->link_state); 33191ae08745Sheppo if (rv != 0) { 33203af08d82Slm66018 cmn_err(CE_WARN, "ldc_read: (0x%lx) unable to read queue ptrs", 33211ae08745Sheppo ldcp->id); 33223af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 33233af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 33243af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 33253af08d82Slm66018 return (ECONNRESET); 33261ae08745Sheppo } 33271ae08745Sheppo D1(ldcp->id, "ldc_read: (0x%llx) chd=0x%llx, tl=0x%llx, st=0x%llx\n", 33281ae08745Sheppo ldcp->id, curr_head, rx_tail, ldcp->link_state); 33291ae08745Sheppo 33301ae08745Sheppo /* reset the channel state if the channel went down */ 33313af08d82Slm66018 if (ldcp->link_state != LDC_CHANNEL_UP) 33323af08d82Slm66018 goto channel_is_reset; 33331ae08745Sheppo 33341ae08745Sheppo for (;;) { 33351ae08745Sheppo 33361ae08745Sheppo if (curr_head == rx_tail) { 33371ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, 33381ae08745Sheppo &rx_head, &rx_tail, &ldcp->link_state); 33391ae08745Sheppo if (rv != 0) { 33401ae08745Sheppo cmn_err(CE_WARN, 33411ae08745Sheppo "ldc_read: (0x%lx) cannot read queue ptrs", 33421ae08745Sheppo ldcp->id); 3343d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 33443af08d82Slm66018 i_ldc_reset(ldcp, B_TRUE); 3345d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 33461ae08745Sheppo return (ECONNRESET); 33471ae08745Sheppo } 33483af08d82Slm66018 if (ldcp->link_state != LDC_CHANNEL_UP) 33493af08d82Slm66018 goto channel_is_reset; 33501ae08745Sheppo 33511ae08745Sheppo if (curr_head == rx_tail) { 33521ae08745Sheppo 33531ae08745Sheppo /* If in the middle of a fragmented xfer */ 3354d10e4ef2Snarayan if (first_fragment != 0) { 33550a55fbb7Slm66018 33560a55fbb7Slm66018 /* wait for ldc_delay usecs */ 33570a55fbb7Slm66018 drv_usecwait(ldc_delay); 33580a55fbb7Slm66018 33590a55fbb7Slm66018 if (++retries < ldc_max_retries) 33601ae08745Sheppo continue; 33610a55fbb7Slm66018 33621ae08745Sheppo *sizep = 0; 3363d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 33643af08d82Slm66018 DWARN(DBG_ALL_LDCS, "ldc_read: " 33653af08d82Slm66018 "(0x%llx) read timeout", 33661ae08745Sheppo ldcp->id); 33673af08d82Slm66018 return (EAGAIN); 33681ae08745Sheppo } 33691ae08745Sheppo *sizep = 0; 33701ae08745Sheppo break; 33711ae08745Sheppo } 33723af08d82Slm66018 } 33730a55fbb7Slm66018 retries = 0; 33741ae08745Sheppo 33751ae08745Sheppo D2(ldcp->id, 33761ae08745Sheppo "ldc_read: (0x%llx) chd=0x%llx, rxhd=0x%llx, rxtl=0x%llx\n", 33771ae08745Sheppo ldcp->id, curr_head, rx_head, rx_tail); 33781ae08745Sheppo 33791ae08745Sheppo /* get the message */ 33801ae08745Sheppo msg = (ldc_msg_t *)(ldcp->rx_q_va + curr_head); 33811ae08745Sheppo 33821ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_read received pkt", 33831ae08745Sheppo ldcp->rx_q_va + curr_head); 33841ae08745Sheppo 33851ae08745Sheppo /* Check the message ID for the message received */ 33861ae08745Sheppo if ((rv = i_ldc_check_seqid(ldcp, msg)) != 0) { 33871ae08745Sheppo 33881ae08745Sheppo DWARN(ldcp->id, "ldc_read: (0x%llx) seqid error, " 33891ae08745Sheppo "q_ptrs=0x%lx,0x%lx", ldcp->id, rx_head, rx_tail); 33901ae08745Sheppo 33910a55fbb7Slm66018 /* throw away data */ 33920a55fbb7Slm66018 bytes_read = 0; 33930a55fbb7Slm66018 33941ae08745Sheppo /* Reset last_msg_rcd to start of message */ 3395d10e4ef2Snarayan if (first_fragment != 0) { 3396d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 3397d10e4ef2Snarayan first_fragment = 0; 33981ae08745Sheppo } 33991ae08745Sheppo /* 34001ae08745Sheppo * Send a NACK -- invalid seqid 34011ae08745Sheppo * get the current tail for the response 34021ae08745Sheppo */ 34031ae08745Sheppo rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK, 34041ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 34051ae08745Sheppo if (rv) { 34061ae08745Sheppo cmn_err(CE_NOTE, 34071ae08745Sheppo "ldc_read: (0x%lx) err sending " 34081ae08745Sheppo "NACK msg\n", ldcp->id); 3409d10e4ef2Snarayan 3410d10e4ef2Snarayan /* if cannot send NACK - reset channel */ 3411d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 34123af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3413d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3414d10e4ef2Snarayan rv = ECONNRESET; 3415d10e4ef2Snarayan break; 34161ae08745Sheppo } 34171ae08745Sheppo 34181ae08745Sheppo /* purge receive queue */ 34190a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 34201ae08745Sheppo 34211ae08745Sheppo break; 34221ae08745Sheppo } 34231ae08745Sheppo 34241ae08745Sheppo /* 34251ae08745Sheppo * Process any messages of type CTRL messages 3426e1ebb9ecSlm66018 * Future implementations should try to pass these 3427e1ebb9ecSlm66018 * to LDC link by resetting the intr state. 34281ae08745Sheppo * 34291ae08745Sheppo * NOTE: not done as a switch() as type can be both ctrl+data 34301ae08745Sheppo */ 34311ae08745Sheppo if (msg->type & LDC_CTRL) { 34321ae08745Sheppo if (rv = i_ldc_ctrlmsg(ldcp, msg)) { 34331ae08745Sheppo if (rv == EAGAIN) 34341ae08745Sheppo continue; 34350a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 34361ae08745Sheppo *sizep = 0; 34371ae08745Sheppo bytes_read = 0; 34381ae08745Sheppo break; 34391ae08745Sheppo } 34401ae08745Sheppo } 34411ae08745Sheppo 34421ae08745Sheppo /* process data ACKs */ 34431ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 3444d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 3445d10e4ef2Snarayan *sizep = 0; 3446d10e4ef2Snarayan bytes_read = 0; 3447d10e4ef2Snarayan break; 3448d10e4ef2Snarayan } 34491ae08745Sheppo } 34501ae08745Sheppo 34511ae08745Sheppo /* process data messages */ 34521ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 34531ae08745Sheppo 34541ae08745Sheppo uint8_t *msgbuf = (uint8_t *)( 34551ae08745Sheppo (ldcp->mode == LDC_MODE_RELIABLE || 34561ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 34571ae08745Sheppo ? msg->rdata : msg->udata); 34581ae08745Sheppo 34591ae08745Sheppo D2(ldcp->id, 34601ae08745Sheppo "ldc_read: (0x%llx) received data msg\n", ldcp->id); 34611ae08745Sheppo 34621ae08745Sheppo /* get the packet length */ 34631ae08745Sheppo len = (msg->env & LDC_LEN_MASK); 34641ae08745Sheppo 34651ae08745Sheppo /* 34661ae08745Sheppo * FUTURE OPTIMIZATION: 34671ae08745Sheppo * dont need to set q head for every 34681ae08745Sheppo * packet we read just need to do this when 34691ae08745Sheppo * we are done or need to wait for more 34701ae08745Sheppo * mondos to make a full packet - this is 34711ae08745Sheppo * currently expensive. 34721ae08745Sheppo */ 34731ae08745Sheppo 3474d10e4ef2Snarayan if (first_fragment == 0) { 34751ae08745Sheppo 34761ae08745Sheppo /* 34771ae08745Sheppo * first packets should always have the start 34781ae08745Sheppo * bit set (even for a single packet). If not 34791ae08745Sheppo * throw away the packet 34801ae08745Sheppo */ 34811ae08745Sheppo if (!(msg->env & LDC_FRAG_START)) { 34821ae08745Sheppo 34831ae08745Sheppo DWARN(DBG_ALL_LDCS, 34841ae08745Sheppo "ldc_read: (0x%llx) not start - " 34851ae08745Sheppo "frag=%x\n", ldcp->id, 34861ae08745Sheppo (msg->env) & LDC_FRAG_MASK); 34871ae08745Sheppo 34881ae08745Sheppo /* toss pkt, inc head, cont reading */ 34891ae08745Sheppo bytes_read = 0; 34901ae08745Sheppo target = target_bufp; 34911ae08745Sheppo curr_head = 34921ae08745Sheppo (curr_head + LDC_PACKET_SIZE) 34931ae08745Sheppo & q_size_mask; 34941ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, 34951ae08745Sheppo curr_head)) 34961ae08745Sheppo break; 34971ae08745Sheppo 34981ae08745Sheppo continue; 34991ae08745Sheppo } 35001ae08745Sheppo 3501d10e4ef2Snarayan first_fragment = msg->seqid; 35021ae08745Sheppo } else { 35031ae08745Sheppo /* check to see if this is a pkt w/ START bit */ 35041ae08745Sheppo if (msg->env & LDC_FRAG_START) { 35051ae08745Sheppo DWARN(DBG_ALL_LDCS, 35061ae08745Sheppo "ldc_read:(0x%llx) unexpected pkt" 35071ae08745Sheppo " env=0x%x discarding %d bytes," 35081ae08745Sheppo " lastmsg=%d, currentmsg=%d\n", 35091ae08745Sheppo ldcp->id, msg->env&LDC_FRAG_MASK, 35101ae08745Sheppo bytes_read, ldcp->last_msg_rcd, 35111ae08745Sheppo msg->seqid); 35121ae08745Sheppo 35131ae08745Sheppo /* throw data we have read so far */ 35141ae08745Sheppo bytes_read = 0; 35151ae08745Sheppo target = target_bufp; 3516d10e4ef2Snarayan first_fragment = msg->seqid; 35171ae08745Sheppo 35181ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, 35191ae08745Sheppo curr_head)) 35201ae08745Sheppo break; 35211ae08745Sheppo } 35221ae08745Sheppo } 35231ae08745Sheppo 35241ae08745Sheppo /* copy (next) pkt into buffer */ 35251ae08745Sheppo if (len <= (*sizep - bytes_read)) { 35261ae08745Sheppo bcopy(msgbuf, target, len); 35271ae08745Sheppo target += len; 35281ae08745Sheppo bytes_read += len; 35291ae08745Sheppo } else { 35301ae08745Sheppo /* 35311ae08745Sheppo * there is not enough space in the buffer to 35321ae08745Sheppo * read this pkt. throw message away & continue 35331ae08745Sheppo * reading data from queue 35341ae08745Sheppo */ 35351ae08745Sheppo DWARN(DBG_ALL_LDCS, 35361ae08745Sheppo "ldc_read: (0x%llx) buffer too small, " 35371ae08745Sheppo "head=0x%lx, expect=%d, got=%d\n", ldcp->id, 35381ae08745Sheppo curr_head, *sizep, bytes_read+len); 35391ae08745Sheppo 3540d10e4ef2Snarayan first_fragment = 0; 35411ae08745Sheppo target = target_bufp; 35421ae08745Sheppo bytes_read = 0; 35431ae08745Sheppo 35441ae08745Sheppo /* throw away everything received so far */ 35451ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) 35461ae08745Sheppo break; 35471ae08745Sheppo 35481ae08745Sheppo /* continue reading remaining pkts */ 35491ae08745Sheppo continue; 35501ae08745Sheppo } 35511ae08745Sheppo } 35521ae08745Sheppo 35531ae08745Sheppo /* set the message id */ 35541ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 35551ae08745Sheppo 35561ae08745Sheppo /* move the head one position */ 35571ae08745Sheppo curr_head = (curr_head + LDC_PACKET_SIZE) & q_size_mask; 35581ae08745Sheppo 35591ae08745Sheppo if (msg->env & LDC_FRAG_STOP) { 35601ae08745Sheppo 35611ae08745Sheppo /* 35621ae08745Sheppo * All pkts that are part of this fragmented transfer 35631ae08745Sheppo * have been read or this was a single pkt read 35641ae08745Sheppo * or there was an error 35651ae08745Sheppo */ 35661ae08745Sheppo 35671ae08745Sheppo /* set the queue head */ 35681ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) 35691ae08745Sheppo bytes_read = 0; 35701ae08745Sheppo 35711ae08745Sheppo *sizep = bytes_read; 35721ae08745Sheppo 35731ae08745Sheppo break; 35741ae08745Sheppo } 35751ae08745Sheppo 35761ae08745Sheppo /* advance head if it is a DATA ACK */ 35771ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 35781ae08745Sheppo 35791ae08745Sheppo /* set the queue head */ 35801ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) { 35811ae08745Sheppo bytes_read = 0; 35821ae08745Sheppo break; 35831ae08745Sheppo } 35841ae08745Sheppo 35851ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) set ACK qhead 0x%llx", 35861ae08745Sheppo ldcp->id, curr_head); 35871ae08745Sheppo } 35881ae08745Sheppo 35891ae08745Sheppo } /* for (;;) */ 35901ae08745Sheppo 35911ae08745Sheppo 35921ae08745Sheppo /* 35931ae08745Sheppo * If useful data was read - Send msg ACK 35941ae08745Sheppo * OPTIMIZE: do not send ACK for all msgs - use some frequency 35951ae08745Sheppo */ 35961ae08745Sheppo if ((bytes_read > 0) && (ldcp->mode == LDC_MODE_RELIABLE || 35971ae08745Sheppo ldcp->mode == LDC_MODE_STREAM)) { 35981ae08745Sheppo 35991ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_DATA, LDC_ACK, 0); 36003af08d82Slm66018 if (rv && rv != EWOULDBLOCK) { 36011ae08745Sheppo cmn_err(CE_NOTE, 36021ae08745Sheppo "ldc_read: (0x%lx) cannot send ACK\n", ldcp->id); 3603d10e4ef2Snarayan 3604d10e4ef2Snarayan /* if cannot send ACK - reset channel */ 36053af08d82Slm66018 goto channel_is_reset; 36061ae08745Sheppo } 36071ae08745Sheppo } 36081ae08745Sheppo 36091ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) end size=%d", ldcp->id, *sizep); 36101ae08745Sheppo 36111ae08745Sheppo return (rv); 36123af08d82Slm66018 36133af08d82Slm66018 channel_is_reset: 36143af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 36153af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 36163af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 36173af08d82Slm66018 return (ECONNRESET); 36181ae08745Sheppo } 36191ae08745Sheppo 36201ae08745Sheppo /* 36211ae08745Sheppo * Use underlying reliable packet mechanism to fetch 36221ae08745Sheppo * and buffer incoming packets so we can hand them back as 36231ae08745Sheppo * a basic byte stream. 36241ae08745Sheppo * 36251ae08745Sheppo * Enter and exit with ldcp->lock held by caller 36261ae08745Sheppo */ 36271ae08745Sheppo static int 36281ae08745Sheppo i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 36291ae08745Sheppo { 36301ae08745Sheppo int rv; 36311ae08745Sheppo size_t size; 36321ae08745Sheppo 36331ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 36341ae08745Sheppo 36351ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) buffer size=%d", 36361ae08745Sheppo ldcp->id, *sizep); 36371ae08745Sheppo 36381ae08745Sheppo if (ldcp->stream_remains == 0) { 36391ae08745Sheppo size = ldcp->mtu; 36401ae08745Sheppo rv = i_ldc_read_packet(ldcp, 36411ae08745Sheppo (caddr_t)ldcp->stream_bufferp, &size); 36421ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: read packet (0x%llx) size=%d", 36431ae08745Sheppo ldcp->id, size); 36441ae08745Sheppo 36451ae08745Sheppo if (rv != 0) 36461ae08745Sheppo return (rv); 36471ae08745Sheppo 36481ae08745Sheppo ldcp->stream_remains = size; 36491ae08745Sheppo ldcp->stream_offset = 0; 36501ae08745Sheppo } 36511ae08745Sheppo 36521ae08745Sheppo size = MIN(ldcp->stream_remains, *sizep); 36531ae08745Sheppo 36541ae08745Sheppo bcopy(ldcp->stream_bufferp + ldcp->stream_offset, target_bufp, size); 36551ae08745Sheppo ldcp->stream_offset += size; 36561ae08745Sheppo ldcp->stream_remains -= size; 36571ae08745Sheppo 36581ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) fill from buffer size=%d", 36591ae08745Sheppo ldcp->id, size); 36601ae08745Sheppo 36611ae08745Sheppo *sizep = size; 36621ae08745Sheppo return (0); 36631ae08745Sheppo } 36641ae08745Sheppo 36651ae08745Sheppo /* 36661ae08745Sheppo * Write specified amount of bytes to the channel 36671ae08745Sheppo * in multiple pkts of pkt_payload size. Each 36681ae08745Sheppo * packet is tagged with an unique packet ID in 3669e1ebb9ecSlm66018 * the case of a reliable link. 36701ae08745Sheppo * 36711ae08745Sheppo * On return, size contains the number of bytes written. 36721ae08745Sheppo */ 36731ae08745Sheppo int 36741ae08745Sheppo ldc_write(ldc_handle_t handle, caddr_t buf, size_t *sizep) 36751ae08745Sheppo { 36761ae08745Sheppo ldc_chan_t *ldcp; 36771ae08745Sheppo int rv = 0; 36781ae08745Sheppo 36791ae08745Sheppo if (handle == NULL) { 36801ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_write: invalid channel handle\n"); 36811ae08745Sheppo return (EINVAL); 36821ae08745Sheppo } 36831ae08745Sheppo ldcp = (ldc_chan_t *)handle; 36841ae08745Sheppo 3685d10e4ef2Snarayan /* check if writes can occur */ 3686d10e4ef2Snarayan if (!mutex_tryenter(&ldcp->tx_lock)) { 3687d10e4ef2Snarayan /* 3688d10e4ef2Snarayan * Could not get the lock - channel could 3689d10e4ef2Snarayan * be in the process of being unconfigured 3690d10e4ef2Snarayan * or reader has encountered an error 3691d10e4ef2Snarayan */ 3692d10e4ef2Snarayan return (EAGAIN); 3693d10e4ef2Snarayan } 36941ae08745Sheppo 36951ae08745Sheppo /* check if non-zero data to write */ 36961ae08745Sheppo if (buf == NULL || sizep == NULL) { 36971ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) invalid data write\n", 36981ae08745Sheppo ldcp->id); 3699d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 37001ae08745Sheppo return (EINVAL); 37011ae08745Sheppo } 37021ae08745Sheppo 37031ae08745Sheppo if (*sizep == 0) { 37041ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write size of zero\n", 37051ae08745Sheppo ldcp->id); 3706d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 37071ae08745Sheppo return (0); 37081ae08745Sheppo } 37091ae08745Sheppo 37101ae08745Sheppo /* Check if channel is UP for data exchange */ 37111ae08745Sheppo if (ldcp->tstate != TS_UP) { 37121ae08745Sheppo DWARN(ldcp->id, 37131ae08745Sheppo "ldc_write: (0x%llx) channel is not in UP state\n", 37141ae08745Sheppo ldcp->id); 37151ae08745Sheppo *sizep = 0; 37161ae08745Sheppo rv = ECONNRESET; 37171ae08745Sheppo } else { 37181ae08745Sheppo rv = ldcp->write_p(ldcp, buf, sizep); 37191ae08745Sheppo } 37201ae08745Sheppo 3721d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 37221ae08745Sheppo 37231ae08745Sheppo return (rv); 37241ae08745Sheppo } 37251ae08745Sheppo 37261ae08745Sheppo /* 37271ae08745Sheppo * Write a raw packet to the channel 37281ae08745Sheppo * On return, size contains the number of bytes written. 37291ae08745Sheppo */ 37301ae08745Sheppo static int 37311ae08745Sheppo i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 37321ae08745Sheppo { 37331ae08745Sheppo ldc_msg_t *ldcmsg; 37341ae08745Sheppo uint64_t tx_head, tx_tail, new_tail; 37351ae08745Sheppo int rv = 0; 37361ae08745Sheppo size_t size; 37371ae08745Sheppo 3738d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 37391ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RAW); 37401ae08745Sheppo 37411ae08745Sheppo size = *sizep; 37421ae08745Sheppo 37431ae08745Sheppo /* 37441ae08745Sheppo * Check to see if the packet size is less than or 37451ae08745Sheppo * equal to packet size support in raw mode 37461ae08745Sheppo */ 37471ae08745Sheppo if (size > ldcp->pkt_payload) { 37481ae08745Sheppo DWARN(ldcp->id, 37491ae08745Sheppo "ldc_write: (0x%llx) invalid size (0x%llx) for RAW mode\n", 37501ae08745Sheppo ldcp->id, *sizep); 37511ae08745Sheppo *sizep = 0; 37521ae08745Sheppo return (EMSGSIZE); 37531ae08745Sheppo } 37541ae08745Sheppo 37551ae08745Sheppo /* get the qptrs for the tx queue */ 37561ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 37571ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 37581ae08745Sheppo if (rv != 0) { 37591ae08745Sheppo cmn_err(CE_WARN, 37601ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 37611ae08745Sheppo *sizep = 0; 37621ae08745Sheppo return (EIO); 37631ae08745Sheppo } 37641ae08745Sheppo 37651ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 37661ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 37671ae08745Sheppo DWARN(ldcp->id, 37681ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 3769d10e4ef2Snarayan 37701ae08745Sheppo *sizep = 0; 3771d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 37723af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3773d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3774d10e4ef2Snarayan } else { 3775d10e4ef2Snarayan /* 3776d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3777d10e4ef2Snarayan * and Tx lock in correct order 3778d10e4ef2Snarayan */ 3779d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3780d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3781d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 37823af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3783d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3784d10e4ef2Snarayan } 37851ae08745Sheppo return (ECONNRESET); 37861ae08745Sheppo } 37871ae08745Sheppo 37881ae08745Sheppo tx_tail = ldcp->tx_tail; 37891ae08745Sheppo tx_head = ldcp->tx_head; 37901ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) & 37911ae08745Sheppo ((ldcp->tx_q_entries-1) << LDC_PACKET_SHIFT); 37921ae08745Sheppo 37931ae08745Sheppo if (new_tail == tx_head) { 37941ae08745Sheppo DWARN(DBG_ALL_LDCS, 37951ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 37961ae08745Sheppo *sizep = 0; 37971ae08745Sheppo return (EWOULDBLOCK); 37981ae08745Sheppo } 37991ae08745Sheppo 38001ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 38011ae08745Sheppo ldcp->id, size); 38021ae08745Sheppo 38031ae08745Sheppo /* Send the data now */ 38041ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 38051ae08745Sheppo 38061ae08745Sheppo /* copy the data into pkt */ 38071ae08745Sheppo bcopy((uint8_t *)buf, ldcmsg, size); 38081ae08745Sheppo 38091ae08745Sheppo /* increment tail */ 38101ae08745Sheppo tx_tail = new_tail; 38111ae08745Sheppo 38121ae08745Sheppo /* 38131ae08745Sheppo * All packets have been copied into the TX queue 38141ae08745Sheppo * update the tail ptr in the HV 38151ae08745Sheppo */ 38161ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 38171ae08745Sheppo if (rv) { 38181ae08745Sheppo if (rv == EWOULDBLOCK) { 38191ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write timed out\n", 38201ae08745Sheppo ldcp->id); 38211ae08745Sheppo *sizep = 0; 38221ae08745Sheppo return (EWOULDBLOCK); 38231ae08745Sheppo } 38241ae08745Sheppo 38251ae08745Sheppo *sizep = 0; 3826d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 38273af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3828d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3829d10e4ef2Snarayan } else { 3830d10e4ef2Snarayan /* 3831d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3832d10e4ef2Snarayan * and Tx lock in correct order 3833d10e4ef2Snarayan */ 3834d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3835d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3836d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 38373af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3838d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3839d10e4ef2Snarayan } 38401ae08745Sheppo return (ECONNRESET); 38411ae08745Sheppo } 38421ae08745Sheppo 38431ae08745Sheppo ldcp->tx_tail = tx_tail; 38441ae08745Sheppo *sizep = size; 38451ae08745Sheppo 38461ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, size); 38471ae08745Sheppo 38481ae08745Sheppo return (rv); 38491ae08745Sheppo } 38501ae08745Sheppo 38511ae08745Sheppo 38521ae08745Sheppo /* 38531ae08745Sheppo * Write specified amount of bytes to the channel 38541ae08745Sheppo * in multiple pkts of pkt_payload size. Each 38551ae08745Sheppo * packet is tagged with an unique packet ID in 3856e1ebb9ecSlm66018 * the case of a reliable link. 38571ae08745Sheppo * 38581ae08745Sheppo * On return, size contains the number of bytes written. 38591ae08745Sheppo * This function needs to ensure that the write size is < MTU size 38601ae08745Sheppo */ 38611ae08745Sheppo static int 38621ae08745Sheppo i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t buf, size_t *size) 38631ae08745Sheppo { 38641ae08745Sheppo ldc_msg_t *ldcmsg; 38651ae08745Sheppo uint64_t tx_head, tx_tail, new_tail, start; 38661ae08745Sheppo uint64_t txq_size_mask, numavail; 38671ae08745Sheppo uint8_t *msgbuf, *source = (uint8_t *)buf; 38681ae08745Sheppo size_t len, bytes_written = 0, remaining; 38691ae08745Sheppo int rv; 38701ae08745Sheppo uint32_t curr_seqid; 38711ae08745Sheppo 3872d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 38731ae08745Sheppo 38741ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RELIABLE || 38751ae08745Sheppo ldcp->mode == LDC_MODE_UNRELIABLE || 38761ae08745Sheppo ldcp->mode == LDC_MODE_STREAM); 38771ae08745Sheppo 38781ae08745Sheppo /* compute mask for increment */ 38791ae08745Sheppo txq_size_mask = (ldcp->tx_q_entries - 1) << LDC_PACKET_SHIFT; 38801ae08745Sheppo 38811ae08745Sheppo /* get the qptrs for the tx queue */ 38821ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 38831ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 38841ae08745Sheppo if (rv != 0) { 38851ae08745Sheppo cmn_err(CE_WARN, 38861ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 38871ae08745Sheppo *size = 0; 38881ae08745Sheppo return (EIO); 38891ae08745Sheppo } 38901ae08745Sheppo 38911ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 38921ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 38931ae08745Sheppo DWARN(ldcp->id, 38941ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 38951ae08745Sheppo *size = 0; 3896d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 38973af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3898d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3899d10e4ef2Snarayan } else { 3900d10e4ef2Snarayan /* 3901d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3902d10e4ef2Snarayan * and Tx lock in correct order 3903d10e4ef2Snarayan */ 3904d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3905d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3906d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 39073af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 3908d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3909d10e4ef2Snarayan } 39101ae08745Sheppo return (ECONNRESET); 39111ae08745Sheppo } 39121ae08745Sheppo 39131ae08745Sheppo tx_tail = ldcp->tx_tail; 39141ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) % 39151ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 39161ae08745Sheppo 39171ae08745Sheppo /* 3918e1ebb9ecSlm66018 * Link mode determines whether we use HV Tx head or the 39191ae08745Sheppo * private protocol head (corresponding to last ACKd pkt) for 39201ae08745Sheppo * determining how much we can write 39211ae08745Sheppo */ 39221ae08745Sheppo tx_head = (ldcp->mode == LDC_MODE_RELIABLE || 39231ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 39241ae08745Sheppo ? ldcp->tx_ackd_head : ldcp->tx_head; 39251ae08745Sheppo if (new_tail == tx_head) { 39261ae08745Sheppo DWARN(DBG_ALL_LDCS, 39271ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 39281ae08745Sheppo *size = 0; 39291ae08745Sheppo return (EWOULDBLOCK); 39301ae08745Sheppo } 39311ae08745Sheppo 39321ae08745Sheppo /* 39331ae08745Sheppo * Make sure that the LDC Tx queue has enough space 39341ae08745Sheppo */ 39351ae08745Sheppo numavail = (tx_head >> LDC_PACKET_SHIFT) - (tx_tail >> LDC_PACKET_SHIFT) 39361ae08745Sheppo + ldcp->tx_q_entries - 1; 39371ae08745Sheppo numavail %= ldcp->tx_q_entries; 39381ae08745Sheppo 39391ae08745Sheppo if (*size > (numavail * ldcp->pkt_payload)) { 39401ae08745Sheppo DWARN(DBG_ALL_LDCS, 39411ae08745Sheppo "ldc_write: (0x%llx) TX queue has no space\n", ldcp->id); 39421ae08745Sheppo return (EWOULDBLOCK); 39431ae08745Sheppo } 39441ae08745Sheppo 39451ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 39461ae08745Sheppo ldcp->id, *size); 39471ae08745Sheppo 39481ae08745Sheppo /* Send the data now */ 39491ae08745Sheppo bytes_written = 0; 39501ae08745Sheppo curr_seqid = ldcp->last_msg_snt; 39511ae08745Sheppo start = tx_tail; 39521ae08745Sheppo 39531ae08745Sheppo while (*size > bytes_written) { 39541ae08745Sheppo 39551ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 39561ae08745Sheppo 39571ae08745Sheppo msgbuf = (uint8_t *)((ldcp->mode == LDC_MODE_RELIABLE || 39581ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 39591ae08745Sheppo ? ldcmsg->rdata : ldcmsg->udata); 39601ae08745Sheppo 39611ae08745Sheppo ldcmsg->type = LDC_DATA; 39621ae08745Sheppo ldcmsg->stype = LDC_INFO; 39631ae08745Sheppo ldcmsg->ctrl = 0; 39641ae08745Sheppo 39651ae08745Sheppo remaining = *size - bytes_written; 39661ae08745Sheppo len = min(ldcp->pkt_payload, remaining); 39671ae08745Sheppo ldcmsg->env = (uint8_t)len; 39681ae08745Sheppo 39691ae08745Sheppo curr_seqid++; 39701ae08745Sheppo ldcmsg->seqid = curr_seqid; 39711ae08745Sheppo 39721ae08745Sheppo /* copy the data into pkt */ 39731ae08745Sheppo bcopy(source, msgbuf, len); 39741ae08745Sheppo 39751ae08745Sheppo source += len; 39761ae08745Sheppo bytes_written += len; 39771ae08745Sheppo 39781ae08745Sheppo /* increment tail */ 39791ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) & txq_size_mask; 39801ae08745Sheppo 39811ae08745Sheppo ASSERT(tx_tail != tx_head); 39821ae08745Sheppo } 39831ae08745Sheppo 39841ae08745Sheppo /* Set the start and stop bits */ 39851ae08745Sheppo ldcmsg->env |= LDC_FRAG_STOP; 39861ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + start); 39871ae08745Sheppo ldcmsg->env |= LDC_FRAG_START; 39881ae08745Sheppo 39891ae08745Sheppo /* 39901ae08745Sheppo * All packets have been copied into the TX queue 39911ae08745Sheppo * update the tail ptr in the HV 39921ae08745Sheppo */ 39931ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 39941ae08745Sheppo if (rv == 0) { 39951ae08745Sheppo ldcp->tx_tail = tx_tail; 39961ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 39971ae08745Sheppo *size = bytes_written; 39981ae08745Sheppo } else { 39991ae08745Sheppo int rv2; 40001ae08745Sheppo 40011ae08745Sheppo if (rv != EWOULDBLOCK) { 40021ae08745Sheppo *size = 0; 4003d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 40043af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4005d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4006d10e4ef2Snarayan } else { 4007d10e4ef2Snarayan /* 4008d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 4009d10e4ef2Snarayan * and Tx lock in correct order 4010d10e4ef2Snarayan */ 4011d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 4012d10e4ef2Snarayan mutex_enter(&ldcp->lock); 4013d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 40143af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 4015d10e4ef2Snarayan mutex_exit(&ldcp->lock); 4016d10e4ef2Snarayan } 40171ae08745Sheppo return (ECONNRESET); 40181ae08745Sheppo } 40191ae08745Sheppo 40201ae08745Sheppo DWARN(ldcp->id, "hv_tx_set_tail returns 0x%x (head 0x%x, " 40211ae08745Sheppo "old tail 0x%x, new tail 0x%x, qsize=0x%x)\n", 40221ae08745Sheppo rv, ldcp->tx_head, ldcp->tx_tail, tx_tail, 40231ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 40241ae08745Sheppo 40251ae08745Sheppo rv2 = hv_ldc_tx_get_state(ldcp->id, 40261ae08745Sheppo &tx_head, &tx_tail, &ldcp->link_state); 40271ae08745Sheppo 40281ae08745Sheppo DWARN(ldcp->id, "hv_ldc_tx_get_state returns 0x%x " 40291ae08745Sheppo "(head 0x%x, tail 0x%x state 0x%x)\n", 40301ae08745Sheppo rv2, tx_head, tx_tail, ldcp->link_state); 40311ae08745Sheppo 40321ae08745Sheppo *size = 0; 40331ae08745Sheppo } 40341ae08745Sheppo 40351ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, *size); 40361ae08745Sheppo 40371ae08745Sheppo return (rv); 40381ae08745Sheppo } 40391ae08745Sheppo 40401ae08745Sheppo /* 40411ae08745Sheppo * Write specified amount of bytes to the channel 40421ae08745Sheppo * in multiple pkts of pkt_payload size. Each 40431ae08745Sheppo * packet is tagged with an unique packet ID in 4044e1ebb9ecSlm66018 * the case of a reliable link. 40451ae08745Sheppo * 40461ae08745Sheppo * On return, size contains the number of bytes written. 40471ae08745Sheppo * This function needs to ensure that the write size is < MTU size 40481ae08745Sheppo */ 40491ae08745Sheppo static int 40501ae08745Sheppo i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 40511ae08745Sheppo { 4052d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 40531ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_STREAM); 40541ae08745Sheppo 40551ae08745Sheppo /* Truncate packet to max of MTU size */ 40561ae08745Sheppo if (*sizep > ldcp->mtu) *sizep = ldcp->mtu; 40571ae08745Sheppo return (i_ldc_write_packet(ldcp, buf, sizep)); 40581ae08745Sheppo } 40591ae08745Sheppo 40601ae08745Sheppo 40611ae08745Sheppo /* 40621ae08745Sheppo * Interfaces for channel nexus to register/unregister with LDC module 40631ae08745Sheppo * The nexus will register functions to be used to register individual 40641ae08745Sheppo * channels with the nexus and enable interrupts for the channels 40651ae08745Sheppo */ 40661ae08745Sheppo int 40671ae08745Sheppo ldc_register(ldc_cnex_t *cinfo) 40681ae08745Sheppo { 40691ae08745Sheppo ldc_chan_t *ldcp; 40701ae08745Sheppo 40711ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL || 40721ae08745Sheppo cinfo->reg_chan == NULL || cinfo->unreg_chan == NULL || 40731ae08745Sheppo cinfo->add_intr == NULL || cinfo->rem_intr == NULL || 40741ae08745Sheppo cinfo->clr_intr == NULL) { 40751ae08745Sheppo 40761ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_register: invalid nexus info\n"); 40771ae08745Sheppo return (EINVAL); 40781ae08745Sheppo } 40791ae08745Sheppo 40801ae08745Sheppo mutex_enter(&ldcssp->lock); 40811ae08745Sheppo 40821ae08745Sheppo /* nexus registration */ 40831ae08745Sheppo ldcssp->cinfo.dip = cinfo->dip; 40841ae08745Sheppo ldcssp->cinfo.reg_chan = cinfo->reg_chan; 40851ae08745Sheppo ldcssp->cinfo.unreg_chan = cinfo->unreg_chan; 40861ae08745Sheppo ldcssp->cinfo.add_intr = cinfo->add_intr; 40871ae08745Sheppo ldcssp->cinfo.rem_intr = cinfo->rem_intr; 40881ae08745Sheppo ldcssp->cinfo.clr_intr = cinfo->clr_intr; 40891ae08745Sheppo 40901ae08745Sheppo /* register any channels that might have been previously initialized */ 40911ae08745Sheppo ldcp = ldcssp->chan_list; 40921ae08745Sheppo while (ldcp) { 40931ae08745Sheppo if ((ldcp->tstate & TS_QCONF_RDY) && 40941ae08745Sheppo (ldcp->tstate & TS_CNEX_RDY) == 0) 40951ae08745Sheppo (void) i_ldc_register_channel(ldcp); 40961ae08745Sheppo 40971ae08745Sheppo ldcp = ldcp->next; 40981ae08745Sheppo } 40991ae08745Sheppo 41001ae08745Sheppo mutex_exit(&ldcssp->lock); 41011ae08745Sheppo 41021ae08745Sheppo return (0); 41031ae08745Sheppo } 41041ae08745Sheppo 41051ae08745Sheppo int 41061ae08745Sheppo ldc_unregister(ldc_cnex_t *cinfo) 41071ae08745Sheppo { 41081ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL) { 41091ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid nexus info\n"); 41101ae08745Sheppo return (EINVAL); 41111ae08745Sheppo } 41121ae08745Sheppo 41131ae08745Sheppo mutex_enter(&ldcssp->lock); 41141ae08745Sheppo 41151ae08745Sheppo if (cinfo->dip != ldcssp->cinfo.dip) { 41161ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid dip\n"); 41171ae08745Sheppo mutex_exit(&ldcssp->lock); 41181ae08745Sheppo return (EINVAL); 41191ae08745Sheppo } 41201ae08745Sheppo 41211ae08745Sheppo /* nexus unregister */ 41221ae08745Sheppo ldcssp->cinfo.dip = NULL; 41231ae08745Sheppo ldcssp->cinfo.reg_chan = NULL; 41241ae08745Sheppo ldcssp->cinfo.unreg_chan = NULL; 41251ae08745Sheppo ldcssp->cinfo.add_intr = NULL; 41261ae08745Sheppo ldcssp->cinfo.rem_intr = NULL; 41271ae08745Sheppo ldcssp->cinfo.clr_intr = NULL; 41281ae08745Sheppo 41291ae08745Sheppo mutex_exit(&ldcssp->lock); 41301ae08745Sheppo 41311ae08745Sheppo return (0); 41321ae08745Sheppo } 41331ae08745Sheppo 41341ae08745Sheppo 41351ae08745Sheppo /* ------------------------------------------------------------------------- */ 41361ae08745Sheppo 41371ae08745Sheppo /* 41381ae08745Sheppo * Allocate a memory handle for the channel and link it into the list 41391ae08745Sheppo * Also choose which memory table to use if this is the first handle 41401ae08745Sheppo * being assigned to this channel 41411ae08745Sheppo */ 41421ae08745Sheppo int 41431ae08745Sheppo ldc_mem_alloc_handle(ldc_handle_t handle, ldc_mem_handle_t *mhandle) 41441ae08745Sheppo { 41451ae08745Sheppo ldc_chan_t *ldcp; 41461ae08745Sheppo ldc_mhdl_t *mhdl; 41471ae08745Sheppo 41481ae08745Sheppo if (handle == NULL) { 41491ae08745Sheppo DWARN(DBG_ALL_LDCS, 41501ae08745Sheppo "ldc_mem_alloc_handle: invalid channel handle\n"); 41511ae08745Sheppo return (EINVAL); 41521ae08745Sheppo } 41531ae08745Sheppo ldcp = (ldc_chan_t *)handle; 41541ae08745Sheppo 41551ae08745Sheppo mutex_enter(&ldcp->lock); 41561ae08745Sheppo 41571ae08745Sheppo /* check to see if channel is initalized */ 41583af08d82Slm66018 if ((ldcp->tstate & ~TS_IN_RESET) < TS_INIT) { 41591ae08745Sheppo DWARN(ldcp->id, 41601ae08745Sheppo "ldc_mem_alloc_handle: (0x%llx) channel not initialized\n", 41611ae08745Sheppo ldcp->id); 41621ae08745Sheppo mutex_exit(&ldcp->lock); 41631ae08745Sheppo return (EINVAL); 41641ae08745Sheppo } 41651ae08745Sheppo 41661ae08745Sheppo /* allocate handle for channel */ 41674bac2208Snarayan mhdl = kmem_cache_alloc(ldcssp->memhdl_cache, KM_SLEEP); 41681ae08745Sheppo 41691ae08745Sheppo /* initialize the lock */ 41701ae08745Sheppo mutex_init(&mhdl->lock, NULL, MUTEX_DRIVER, NULL); 41711ae08745Sheppo 41724bac2208Snarayan mhdl->myshadow = B_FALSE; 41734bac2208Snarayan mhdl->memseg = NULL; 41741ae08745Sheppo mhdl->ldcp = ldcp; 41754bac2208Snarayan mhdl->status = LDC_UNBOUND; 41761ae08745Sheppo 41771ae08745Sheppo /* insert memory handle (@ head) into list */ 41781ae08745Sheppo if (ldcp->mhdl_list == NULL) { 41791ae08745Sheppo ldcp->mhdl_list = mhdl; 41801ae08745Sheppo mhdl->next = NULL; 41811ae08745Sheppo } else { 41821ae08745Sheppo /* insert @ head */ 41831ae08745Sheppo mhdl->next = ldcp->mhdl_list; 41841ae08745Sheppo ldcp->mhdl_list = mhdl; 41851ae08745Sheppo } 41861ae08745Sheppo 41871ae08745Sheppo /* return the handle */ 41881ae08745Sheppo *mhandle = (ldc_mem_handle_t)mhdl; 41891ae08745Sheppo 41901ae08745Sheppo mutex_exit(&ldcp->lock); 41911ae08745Sheppo 41921ae08745Sheppo D1(ldcp->id, "ldc_mem_alloc_handle: (0x%llx) allocated handle 0x%llx\n", 41931ae08745Sheppo ldcp->id, mhdl); 41941ae08745Sheppo 41951ae08745Sheppo return (0); 41961ae08745Sheppo } 41971ae08745Sheppo 41981ae08745Sheppo /* 41991ae08745Sheppo * Free memory handle for the channel and unlink it from the list 42001ae08745Sheppo */ 42011ae08745Sheppo int 42021ae08745Sheppo ldc_mem_free_handle(ldc_mem_handle_t mhandle) 42031ae08745Sheppo { 42041ae08745Sheppo ldc_mhdl_t *mhdl, *phdl; 42051ae08745Sheppo ldc_chan_t *ldcp; 42061ae08745Sheppo 42071ae08745Sheppo if (mhandle == NULL) { 42081ae08745Sheppo DWARN(DBG_ALL_LDCS, 42091ae08745Sheppo "ldc_mem_free_handle: invalid memory handle\n"); 42101ae08745Sheppo return (EINVAL); 42111ae08745Sheppo } 42121ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 42131ae08745Sheppo 42141ae08745Sheppo mutex_enter(&mhdl->lock); 42151ae08745Sheppo 42161ae08745Sheppo ldcp = mhdl->ldcp; 42171ae08745Sheppo 42181ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED) { 42191ae08745Sheppo DWARN(ldcp->id, 42201ae08745Sheppo "ldc_mem_free_handle: cannot free, 0x%llx hdl bound\n", 42211ae08745Sheppo mhdl); 42221ae08745Sheppo mutex_exit(&mhdl->lock); 42231ae08745Sheppo return (EINVAL); 42241ae08745Sheppo } 42251ae08745Sheppo mutex_exit(&mhdl->lock); 42261ae08745Sheppo 42271ae08745Sheppo mutex_enter(&ldcp->mlist_lock); 42281ae08745Sheppo 42291ae08745Sheppo phdl = ldcp->mhdl_list; 42301ae08745Sheppo 42311ae08745Sheppo /* first handle */ 42321ae08745Sheppo if (phdl == mhdl) { 42331ae08745Sheppo ldcp->mhdl_list = mhdl->next; 42341ae08745Sheppo mutex_destroy(&mhdl->lock); 42354bac2208Snarayan kmem_cache_free(ldcssp->memhdl_cache, mhdl); 42364bac2208Snarayan 42371ae08745Sheppo D1(ldcp->id, 42381ae08745Sheppo "ldc_mem_free_handle: (0x%llx) freed handle 0x%llx\n", 42391ae08745Sheppo ldcp->id, mhdl); 42401ae08745Sheppo } else { 42411ae08745Sheppo /* walk the list - unlink and free */ 42421ae08745Sheppo while (phdl != NULL) { 42431ae08745Sheppo if (phdl->next == mhdl) { 42441ae08745Sheppo phdl->next = mhdl->next; 42451ae08745Sheppo mutex_destroy(&mhdl->lock); 42464bac2208Snarayan kmem_cache_free(ldcssp->memhdl_cache, mhdl); 42471ae08745Sheppo D1(ldcp->id, 42481ae08745Sheppo "ldc_mem_free_handle: (0x%llx) freed " 42491ae08745Sheppo "handle 0x%llx\n", ldcp->id, mhdl); 42501ae08745Sheppo break; 42511ae08745Sheppo } 42521ae08745Sheppo phdl = phdl->next; 42531ae08745Sheppo } 42541ae08745Sheppo } 42551ae08745Sheppo 42561ae08745Sheppo if (phdl == NULL) { 42571ae08745Sheppo DWARN(ldcp->id, 42581ae08745Sheppo "ldc_mem_free_handle: invalid handle 0x%llx\n", mhdl); 42591ae08745Sheppo mutex_exit(&ldcp->mlist_lock); 42601ae08745Sheppo return (EINVAL); 42611ae08745Sheppo } 42621ae08745Sheppo 42631ae08745Sheppo mutex_exit(&ldcp->mlist_lock); 42641ae08745Sheppo 42651ae08745Sheppo return (0); 42661ae08745Sheppo } 42671ae08745Sheppo 42681ae08745Sheppo /* 42691ae08745Sheppo * Bind a memory handle to a virtual address. 42701ae08745Sheppo * The virtual address is converted to the corresponding real addresses. 42711ae08745Sheppo * Returns pointer to the first ldc_mem_cookie and the total number 42721ae08745Sheppo * of cookies for this virtual address. Other cookies can be obtained 42731ae08745Sheppo * using the ldc_mem_nextcookie() call. If the pages are stored in 42741ae08745Sheppo * consecutive locations in the table, a single cookie corresponding to 42751ae08745Sheppo * the first location is returned. The cookie size spans all the entries. 42761ae08745Sheppo * 42771ae08745Sheppo * If the VA corresponds to a page that is already being exported, reuse 42781ae08745Sheppo * the page and do not export it again. Bump the page's use count. 42791ae08745Sheppo */ 42801ae08745Sheppo int 42811ae08745Sheppo ldc_mem_bind_handle(ldc_mem_handle_t mhandle, caddr_t vaddr, size_t len, 42821ae08745Sheppo uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount) 42831ae08745Sheppo { 42841ae08745Sheppo ldc_mhdl_t *mhdl; 42851ae08745Sheppo ldc_chan_t *ldcp; 42861ae08745Sheppo ldc_mtbl_t *mtbl; 42871ae08745Sheppo ldc_memseg_t *memseg; 42881ae08745Sheppo ldc_mte_t tmp_mte; 42891ae08745Sheppo uint64_t index, prev_index = 0; 42901ae08745Sheppo int64_t cookie_idx; 42911ae08745Sheppo uintptr_t raddr, ra_aligned; 42921ae08745Sheppo uint64_t psize, poffset, v_offset; 42931ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code, pg_mask; 42941ae08745Sheppo pgcnt_t npages; 42951ae08745Sheppo caddr_t v_align, addr; 42964bac2208Snarayan int i, rv; 42971ae08745Sheppo 42981ae08745Sheppo if (mhandle == NULL) { 42991ae08745Sheppo DWARN(DBG_ALL_LDCS, 43001ae08745Sheppo "ldc_mem_bind_handle: invalid memory handle\n"); 43011ae08745Sheppo return (EINVAL); 43021ae08745Sheppo } 43031ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 43041ae08745Sheppo ldcp = mhdl->ldcp; 43051ae08745Sheppo 43061ae08745Sheppo /* clear count */ 43071ae08745Sheppo *ccount = 0; 43081ae08745Sheppo 43091ae08745Sheppo mutex_enter(&mhdl->lock); 43101ae08745Sheppo 43111ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->memseg != NULL) { 43121ae08745Sheppo DWARN(ldcp->id, 43131ae08745Sheppo "ldc_mem_bind_handle: (0x%x) handle already bound\n", 43141ae08745Sheppo mhandle); 43151ae08745Sheppo mutex_exit(&mhdl->lock); 43161ae08745Sheppo return (EINVAL); 43171ae08745Sheppo } 43181ae08745Sheppo 43191ae08745Sheppo /* Force address and size to be 8-byte aligned */ 43201ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 43211ae08745Sheppo DWARN(ldcp->id, 43221ae08745Sheppo "ldc_mem_bind_handle: addr/size is not 8-byte aligned\n"); 43231ae08745Sheppo mutex_exit(&mhdl->lock); 43241ae08745Sheppo return (EINVAL); 43251ae08745Sheppo } 43261ae08745Sheppo 43274bac2208Snarayan /* 43284bac2208Snarayan * If this channel is binding a memory handle for the 43294bac2208Snarayan * first time allocate it a memory map table and initialize it 43304bac2208Snarayan */ 43314bac2208Snarayan if ((mtbl = ldcp->mtbl) == NULL) { 43324bac2208Snarayan 43334bac2208Snarayan mutex_enter(&ldcp->lock); 43344bac2208Snarayan 43354bac2208Snarayan /* Allocate and initialize the map table structure */ 43364bac2208Snarayan mtbl = kmem_zalloc(sizeof (ldc_mtbl_t), KM_SLEEP); 43374bac2208Snarayan mtbl->num_entries = mtbl->num_avail = ldc_maptable_entries; 43384bac2208Snarayan mtbl->size = ldc_maptable_entries * sizeof (ldc_mte_slot_t); 43394bac2208Snarayan mtbl->next_entry = NULL; 43403af08d82Slm66018 mtbl->contigmem = B_TRUE; 43414bac2208Snarayan 43424bac2208Snarayan /* Allocate the table itself */ 43434bac2208Snarayan mtbl->table = (ldc_mte_slot_t *) 43444bac2208Snarayan contig_mem_alloc_align(mtbl->size, MMU_PAGESIZE); 43454bac2208Snarayan if (mtbl->table == NULL) { 43463af08d82Slm66018 43473af08d82Slm66018 /* allocate a page of memory using kmem_alloc */ 43483af08d82Slm66018 mtbl->table = kmem_alloc(MMU_PAGESIZE, KM_SLEEP); 43493af08d82Slm66018 mtbl->size = MMU_PAGESIZE; 43503af08d82Slm66018 mtbl->contigmem = B_FALSE; 43513af08d82Slm66018 mtbl->num_entries = mtbl->num_avail = 43523af08d82Slm66018 mtbl->size / sizeof (ldc_mte_slot_t); 43533af08d82Slm66018 DWARN(ldcp->id, 43543af08d82Slm66018 "ldc_mem_bind_handle: (0x%llx) reduced tbl size " 43553af08d82Slm66018 "to %lx entries\n", ldcp->id, mtbl->num_entries); 43564bac2208Snarayan } 43574bac2208Snarayan 43584bac2208Snarayan /* zero out the memory */ 43594bac2208Snarayan bzero(mtbl->table, mtbl->size); 43604bac2208Snarayan 43614bac2208Snarayan /* initialize the lock */ 43624bac2208Snarayan mutex_init(&mtbl->lock, NULL, MUTEX_DRIVER, NULL); 43634bac2208Snarayan 43644bac2208Snarayan /* register table for this channel */ 43654bac2208Snarayan rv = hv_ldc_set_map_table(ldcp->id, 43664bac2208Snarayan va_to_pa(mtbl->table), mtbl->num_entries); 43674bac2208Snarayan if (rv != 0) { 43684bac2208Snarayan cmn_err(CE_WARN, 43694bac2208Snarayan "ldc_mem_bind_handle: (0x%lx) err %d mapping tbl", 43704bac2208Snarayan ldcp->id, rv); 43713af08d82Slm66018 if (mtbl->contigmem) 43724bac2208Snarayan contig_mem_free(mtbl->table, mtbl->size); 43733af08d82Slm66018 else 43743af08d82Slm66018 kmem_free(mtbl->table, mtbl->size); 43754bac2208Snarayan mutex_destroy(&mtbl->lock); 43764bac2208Snarayan kmem_free(mtbl, sizeof (ldc_mtbl_t)); 43774bac2208Snarayan mutex_exit(&ldcp->lock); 43784bac2208Snarayan mutex_exit(&mhdl->lock); 43794bac2208Snarayan return (EIO); 43804bac2208Snarayan } 43814bac2208Snarayan 43824bac2208Snarayan ldcp->mtbl = mtbl; 43834bac2208Snarayan mutex_exit(&ldcp->lock); 43844bac2208Snarayan 43854bac2208Snarayan D1(ldcp->id, 43864bac2208Snarayan "ldc_mem_bind_handle: (0x%llx) alloc'd map table 0x%llx\n", 43874bac2208Snarayan ldcp->id, ldcp->mtbl->table); 43884bac2208Snarayan } 43894bac2208Snarayan 43901ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 43911ae08745Sheppo pg_size = MMU_PAGESIZE; 43921ae08745Sheppo pg_size_code = page_szc(pg_size); 43931ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 43941ae08745Sheppo pg_mask = ~(pg_size - 1); 43951ae08745Sheppo 43961ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: (0x%llx) binding " 43971ae08745Sheppo "va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 43981ae08745Sheppo ldcp->id, vaddr, pg_size, pg_size_code, pg_shift); 43991ae08745Sheppo 44001ae08745Sheppo /* aligned VA and its offset */ 44011ae08745Sheppo v_align = (caddr_t)(((uintptr_t)vaddr) & ~(pg_size - 1)); 44021ae08745Sheppo v_offset = ((uintptr_t)vaddr) & (pg_size - 1); 44031ae08745Sheppo 44041ae08745Sheppo npages = (len+v_offset)/pg_size; 44051ae08745Sheppo npages = ((len+v_offset)%pg_size == 0) ? npages : npages+1; 44061ae08745Sheppo 44071ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: binding " 44081ae08745Sheppo "(0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n", 44091ae08745Sheppo ldcp->id, vaddr, v_align, v_offset, npages); 44101ae08745Sheppo 44111ae08745Sheppo /* lock the memory table - exclusive access to channel */ 44121ae08745Sheppo mutex_enter(&mtbl->lock); 44131ae08745Sheppo 44141ae08745Sheppo if (npages > mtbl->num_avail) { 44153af08d82Slm66018 D1(ldcp->id, "ldc_mem_bind_handle: (0x%llx) no table entries\n", 44161ae08745Sheppo ldcp->id); 44171ae08745Sheppo mutex_exit(&mtbl->lock); 44181ae08745Sheppo mutex_exit(&mhdl->lock); 44191ae08745Sheppo return (ENOMEM); 44201ae08745Sheppo } 44211ae08745Sheppo 44221ae08745Sheppo /* Allocate a memseg structure */ 44234bac2208Snarayan memseg = mhdl->memseg = 44244bac2208Snarayan kmem_cache_alloc(ldcssp->memseg_cache, KM_SLEEP); 44251ae08745Sheppo 44261ae08745Sheppo /* Allocate memory to store all pages and cookies */ 44271ae08745Sheppo memseg->pages = kmem_zalloc((sizeof (ldc_page_t) * npages), KM_SLEEP); 44281ae08745Sheppo memseg->cookies = 44291ae08745Sheppo kmem_zalloc((sizeof (ldc_mem_cookie_t) * npages), KM_SLEEP); 44301ae08745Sheppo 44311ae08745Sheppo D2(ldcp->id, "ldc_mem_bind_handle: (0x%llx) processing 0x%llx pages\n", 44321ae08745Sheppo ldcp->id, npages); 44331ae08745Sheppo 44341ae08745Sheppo addr = v_align; 44351ae08745Sheppo 44361ae08745Sheppo /* 44374bac2208Snarayan * Check if direct shared memory map is enabled, if not change 44384bac2208Snarayan * the mapping type to include SHADOW_MAP. 44394bac2208Snarayan */ 44404bac2208Snarayan if (ldc_shmem_enabled == 0) 44414bac2208Snarayan mtype = LDC_SHADOW_MAP; 44424bac2208Snarayan 44434bac2208Snarayan /* 44441ae08745Sheppo * Table slots are used in a round-robin manner. The algorithm permits 44451ae08745Sheppo * inserting duplicate entries. Slots allocated earlier will typically 44461ae08745Sheppo * get freed before we get back to reusing the slot.Inserting duplicate 44471ae08745Sheppo * entries should be OK as we only lookup entries using the cookie addr 44481ae08745Sheppo * i.e. tbl index, during export, unexport and copy operation. 44491ae08745Sheppo * 44501ae08745Sheppo * One implementation what was tried was to search for a duplicate 44511ae08745Sheppo * page entry first and reuse it. The search overhead is very high and 44521ae08745Sheppo * in the vnet case dropped the perf by almost half, 50 to 24 mbps. 44531ae08745Sheppo * So it does make sense to avoid searching for duplicates. 44541ae08745Sheppo * 44551ae08745Sheppo * But during the process of searching for a free slot, if we find a 44561ae08745Sheppo * duplicate entry we will go ahead and use it, and bump its use count. 44571ae08745Sheppo */ 44581ae08745Sheppo 44591ae08745Sheppo /* index to start searching from */ 44601ae08745Sheppo index = mtbl->next_entry; 44611ae08745Sheppo cookie_idx = -1; 44621ae08745Sheppo 44631ae08745Sheppo tmp_mte.ll = 0; /* initialise fields to 0 */ 44641ae08745Sheppo 44651ae08745Sheppo if (mtype & LDC_DIRECT_MAP) { 44661ae08745Sheppo tmp_mte.mte_r = (perm & LDC_MEM_R) ? 1 : 0; 44671ae08745Sheppo tmp_mte.mte_w = (perm & LDC_MEM_W) ? 1 : 0; 44681ae08745Sheppo tmp_mte.mte_x = (perm & LDC_MEM_X) ? 1 : 0; 44691ae08745Sheppo } 44701ae08745Sheppo 44711ae08745Sheppo if (mtype & LDC_SHADOW_MAP) { 44721ae08745Sheppo tmp_mte.mte_cr = (perm & LDC_MEM_R) ? 1 : 0; 44731ae08745Sheppo tmp_mte.mte_cw = (perm & LDC_MEM_W) ? 1 : 0; 44741ae08745Sheppo } 44751ae08745Sheppo 44761ae08745Sheppo if (mtype & LDC_IO_MAP) { 44771ae08745Sheppo tmp_mte.mte_ir = (perm & LDC_MEM_R) ? 1 : 0; 44781ae08745Sheppo tmp_mte.mte_iw = (perm & LDC_MEM_W) ? 1 : 0; 44791ae08745Sheppo } 44801ae08745Sheppo 44811ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle mte=0x%llx\n", tmp_mte.ll); 44821ae08745Sheppo 44831ae08745Sheppo tmp_mte.mte_pgszc = pg_size_code; 44841ae08745Sheppo 44851ae08745Sheppo /* initialize each mem table entry */ 44861ae08745Sheppo for (i = 0; i < npages; i++) { 44871ae08745Sheppo 44881ae08745Sheppo /* check if slot is available in the table */ 44891ae08745Sheppo while (mtbl->table[index].entry.ll != 0) { 44901ae08745Sheppo 44911ae08745Sheppo index = (index + 1) % mtbl->num_entries; 44921ae08745Sheppo 44931ae08745Sheppo if (index == mtbl->next_entry) { 44941ae08745Sheppo /* we have looped around */ 44951ae08745Sheppo DWARN(DBG_ALL_LDCS, 44961ae08745Sheppo "ldc_mem_bind_handle: (0x%llx) cannot find " 44971ae08745Sheppo "entry\n", ldcp->id); 44981ae08745Sheppo *ccount = 0; 44991ae08745Sheppo 45001ae08745Sheppo /* NOTE: free memory, remove previous entries */ 45011ae08745Sheppo /* this shouldnt happen as num_avail was ok */ 45021ae08745Sheppo 45031ae08745Sheppo mutex_exit(&mtbl->lock); 45041ae08745Sheppo mutex_exit(&mhdl->lock); 45051ae08745Sheppo return (ENOMEM); 45061ae08745Sheppo } 45071ae08745Sheppo } 45081ae08745Sheppo 45091ae08745Sheppo /* get the real address */ 45101ae08745Sheppo raddr = va_to_pa((void *)addr); 45111ae08745Sheppo ra_aligned = ((uintptr_t)raddr & pg_mask); 45121ae08745Sheppo 45131ae08745Sheppo /* build the mte */ 45141ae08745Sheppo tmp_mte.mte_rpfn = ra_aligned >> pg_shift; 45151ae08745Sheppo 45161ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle mte=0x%llx\n", tmp_mte.ll); 45171ae08745Sheppo 45181ae08745Sheppo /* update entry in table */ 45191ae08745Sheppo mtbl->table[index].entry = tmp_mte; 45201ae08745Sheppo 45211ae08745Sheppo D2(ldcp->id, "ldc_mem_bind_handle: (0x%llx) stored MTE 0x%llx" 45221ae08745Sheppo " into loc 0x%llx\n", ldcp->id, tmp_mte.ll, index); 45231ae08745Sheppo 45241ae08745Sheppo /* calculate the size and offset for this export range */ 45251ae08745Sheppo if (i == 0) { 45261ae08745Sheppo /* first page */ 45271ae08745Sheppo psize = min((pg_size - v_offset), len); 45281ae08745Sheppo poffset = v_offset; 45291ae08745Sheppo 45301ae08745Sheppo } else if (i == (npages - 1)) { 45311ae08745Sheppo /* last page */ 45321ae08745Sheppo psize = (((uintptr_t)(vaddr + len)) & 45331ae08745Sheppo ((uint64_t)(pg_size-1))); 45341ae08745Sheppo if (psize == 0) 45351ae08745Sheppo psize = pg_size; 45361ae08745Sheppo poffset = 0; 45371ae08745Sheppo 45381ae08745Sheppo } else { 45391ae08745Sheppo /* middle pages */ 45401ae08745Sheppo psize = pg_size; 45411ae08745Sheppo poffset = 0; 45421ae08745Sheppo } 45431ae08745Sheppo 45441ae08745Sheppo /* store entry for this page */ 45451ae08745Sheppo memseg->pages[i].index = index; 45461ae08745Sheppo memseg->pages[i].raddr = raddr; 45471ae08745Sheppo memseg->pages[i].offset = poffset; 45481ae08745Sheppo memseg->pages[i].size = psize; 45491ae08745Sheppo memseg->pages[i].mte = &(mtbl->table[index]); 45501ae08745Sheppo 45511ae08745Sheppo /* create the cookie */ 45521ae08745Sheppo if (i == 0 || (index != prev_index + 1)) { 45531ae08745Sheppo cookie_idx++; 45541ae08745Sheppo memseg->cookies[cookie_idx].addr = 45551ae08745Sheppo IDX2COOKIE(index, pg_size_code, pg_shift); 45561ae08745Sheppo memseg->cookies[cookie_idx].addr |= poffset; 45571ae08745Sheppo memseg->cookies[cookie_idx].size = psize; 45581ae08745Sheppo 45591ae08745Sheppo } else { 45601ae08745Sheppo memseg->cookies[cookie_idx].size += psize; 45611ae08745Sheppo } 45621ae08745Sheppo 45631ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: bound " 45641ae08745Sheppo "(0x%llx) va=0x%llx, idx=0x%llx, " 45651ae08745Sheppo "ra=0x%llx(sz=0x%x,off=0x%x)\n", 45661ae08745Sheppo ldcp->id, addr, index, raddr, psize, poffset); 45671ae08745Sheppo 45681ae08745Sheppo /* decrement number of available entries */ 45691ae08745Sheppo mtbl->num_avail--; 45701ae08745Sheppo 45711ae08745Sheppo /* increment va by page size */ 45721ae08745Sheppo addr += pg_size; 45731ae08745Sheppo 45741ae08745Sheppo /* increment index */ 45751ae08745Sheppo prev_index = index; 45761ae08745Sheppo index = (index + 1) % mtbl->num_entries; 45771ae08745Sheppo 45781ae08745Sheppo /* save the next slot */ 45791ae08745Sheppo mtbl->next_entry = index; 45801ae08745Sheppo } 45811ae08745Sheppo 45821ae08745Sheppo mutex_exit(&mtbl->lock); 45831ae08745Sheppo 45841ae08745Sheppo /* memory handle = bound */ 45851ae08745Sheppo mhdl->mtype = mtype; 45861ae08745Sheppo mhdl->perm = perm; 45871ae08745Sheppo mhdl->status = LDC_BOUND; 45881ae08745Sheppo 45891ae08745Sheppo /* update memseg_t */ 45901ae08745Sheppo memseg->vaddr = vaddr; 45911ae08745Sheppo memseg->raddr = memseg->pages[0].raddr; 45921ae08745Sheppo memseg->size = len; 45931ae08745Sheppo memseg->npages = npages; 45941ae08745Sheppo memseg->ncookies = cookie_idx + 1; 45951ae08745Sheppo memseg->next_cookie = (memseg->ncookies > 1) ? 1 : 0; 45961ae08745Sheppo 45971ae08745Sheppo /* return count and first cookie */ 45981ae08745Sheppo *ccount = memseg->ncookies; 45991ae08745Sheppo cookie->addr = memseg->cookies[0].addr; 46001ae08745Sheppo cookie->size = memseg->cookies[0].size; 46011ae08745Sheppo 46021ae08745Sheppo D1(ldcp->id, 46031ae08745Sheppo "ldc_mem_bind_handle: (0x%llx) bound 0x%llx, va=0x%llx, " 46041ae08745Sheppo "pgs=0x%llx cookies=0x%llx\n", 46051ae08745Sheppo ldcp->id, mhdl, vaddr, npages, memseg->ncookies); 46061ae08745Sheppo 46071ae08745Sheppo mutex_exit(&mhdl->lock); 46081ae08745Sheppo return (0); 46091ae08745Sheppo } 46101ae08745Sheppo 46111ae08745Sheppo /* 46121ae08745Sheppo * Return the next cookie associated with the specified memory handle 46131ae08745Sheppo */ 46141ae08745Sheppo int 46151ae08745Sheppo ldc_mem_nextcookie(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie) 46161ae08745Sheppo { 46171ae08745Sheppo ldc_mhdl_t *mhdl; 46181ae08745Sheppo ldc_chan_t *ldcp; 46191ae08745Sheppo ldc_memseg_t *memseg; 46201ae08745Sheppo 46211ae08745Sheppo if (mhandle == NULL) { 46221ae08745Sheppo DWARN(DBG_ALL_LDCS, 46231ae08745Sheppo "ldc_mem_nextcookie: invalid memory handle\n"); 46241ae08745Sheppo return (EINVAL); 46251ae08745Sheppo } 46261ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 46271ae08745Sheppo 46281ae08745Sheppo mutex_enter(&mhdl->lock); 46291ae08745Sheppo 46301ae08745Sheppo ldcp = mhdl->ldcp; 46311ae08745Sheppo memseg = mhdl->memseg; 46321ae08745Sheppo 46331ae08745Sheppo if (cookie == 0) { 46341ae08745Sheppo DWARN(ldcp->id, 46351ae08745Sheppo "ldc_mem_nextcookie:(0x%llx) invalid cookie arg\n", 46361ae08745Sheppo ldcp->id); 46371ae08745Sheppo mutex_exit(&mhdl->lock); 46381ae08745Sheppo return (EINVAL); 46391ae08745Sheppo } 46401ae08745Sheppo 46411ae08745Sheppo if (memseg->next_cookie != 0) { 46421ae08745Sheppo cookie->addr = memseg->cookies[memseg->next_cookie].addr; 46431ae08745Sheppo cookie->size = memseg->cookies[memseg->next_cookie].size; 46441ae08745Sheppo memseg->next_cookie++; 46451ae08745Sheppo if (memseg->next_cookie == memseg->ncookies) 46461ae08745Sheppo memseg->next_cookie = 0; 46471ae08745Sheppo 46481ae08745Sheppo } else { 46491ae08745Sheppo DWARN(ldcp->id, 46501ae08745Sheppo "ldc_mem_nextcookie:(0x%llx) no more cookies\n", ldcp->id); 46511ae08745Sheppo cookie->addr = 0; 46521ae08745Sheppo cookie->size = 0; 46531ae08745Sheppo mutex_exit(&mhdl->lock); 46541ae08745Sheppo return (EINVAL); 46551ae08745Sheppo } 46561ae08745Sheppo 46571ae08745Sheppo D1(ldcp->id, 46581ae08745Sheppo "ldc_mem_nextcookie: (0x%llx) cookie addr=0x%llx,sz=0x%llx\n", 46591ae08745Sheppo ldcp->id, cookie->addr, cookie->size); 46601ae08745Sheppo 46611ae08745Sheppo mutex_exit(&mhdl->lock); 46621ae08745Sheppo return (0); 46631ae08745Sheppo } 46641ae08745Sheppo 46651ae08745Sheppo /* 46661ae08745Sheppo * Unbind the virtual memory region associated with the specified 46671ae08745Sheppo * memory handle. Allassociated cookies are freed and the corresponding 46681ae08745Sheppo * RA space is no longer exported. 46691ae08745Sheppo */ 46701ae08745Sheppo int 46711ae08745Sheppo ldc_mem_unbind_handle(ldc_mem_handle_t mhandle) 46721ae08745Sheppo { 46731ae08745Sheppo ldc_mhdl_t *mhdl; 46741ae08745Sheppo ldc_chan_t *ldcp; 46751ae08745Sheppo ldc_mtbl_t *mtbl; 46761ae08745Sheppo ldc_memseg_t *memseg; 46774bac2208Snarayan uint64_t cookie_addr; 46784bac2208Snarayan uint64_t pg_shift, pg_size_code; 46794bac2208Snarayan int i, rv; 46801ae08745Sheppo 46811ae08745Sheppo if (mhandle == NULL) { 46821ae08745Sheppo DWARN(DBG_ALL_LDCS, 46831ae08745Sheppo "ldc_mem_unbind_handle: invalid memory handle\n"); 46841ae08745Sheppo return (EINVAL); 46851ae08745Sheppo } 46861ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 46871ae08745Sheppo 46881ae08745Sheppo mutex_enter(&mhdl->lock); 46891ae08745Sheppo 46901ae08745Sheppo if (mhdl->status == LDC_UNBOUND) { 46911ae08745Sheppo DWARN(DBG_ALL_LDCS, 46921ae08745Sheppo "ldc_mem_unbind_handle: (0x%x) handle is not bound\n", 46931ae08745Sheppo mhandle); 46941ae08745Sheppo mutex_exit(&mhdl->lock); 46951ae08745Sheppo return (EINVAL); 46961ae08745Sheppo } 46971ae08745Sheppo 46981ae08745Sheppo ldcp = mhdl->ldcp; 46991ae08745Sheppo mtbl = ldcp->mtbl; 47001ae08745Sheppo 47011ae08745Sheppo memseg = mhdl->memseg; 47021ae08745Sheppo 47031ae08745Sheppo /* lock the memory table - exclusive access to channel */ 47041ae08745Sheppo mutex_enter(&mtbl->lock); 47051ae08745Sheppo 47061ae08745Sheppo /* undo the pages exported */ 47071ae08745Sheppo for (i = 0; i < memseg->npages; i++) { 47081ae08745Sheppo 47094bac2208Snarayan /* check for mapped pages, revocation cookie != 0 */ 47101ae08745Sheppo if (memseg->pages[i].mte->cookie) { 47114bac2208Snarayan 47124bac2208Snarayan pg_size_code = page_szc(memseg->pages[i].size); 47134bac2208Snarayan pg_shift = page_get_shift(memseg->pages[i].size); 47144bac2208Snarayan cookie_addr = IDX2COOKIE(memseg->pages[i].index, 47154bac2208Snarayan pg_size_code, pg_shift); 47164bac2208Snarayan 47174bac2208Snarayan D1(ldcp->id, "ldc_mem_unbind_handle: (0x%llx) revoke " 47184bac2208Snarayan "cookie 0x%llx, rcookie 0x%llx\n", ldcp->id, 47194bac2208Snarayan cookie_addr, memseg->pages[i].mte->cookie); 47204bac2208Snarayan rv = hv_ldc_revoke(ldcp->id, cookie_addr, 47214bac2208Snarayan memseg->pages[i].mte->cookie); 47224bac2208Snarayan if (rv) { 47234bac2208Snarayan DWARN(ldcp->id, 47244bac2208Snarayan "ldc_mem_unbind_handle: (0x%llx) cannot " 47254bac2208Snarayan "revoke mapping, cookie %llx\n", ldcp->id, 47264bac2208Snarayan cookie_addr); 47274bac2208Snarayan } 47281ae08745Sheppo } 47291ae08745Sheppo 47301ae08745Sheppo /* clear the entry from the table */ 47311ae08745Sheppo memseg->pages[i].mte->entry.ll = 0; 47321ae08745Sheppo mtbl->num_avail++; 47331ae08745Sheppo } 47341ae08745Sheppo mutex_exit(&mtbl->lock); 47351ae08745Sheppo 47361ae08745Sheppo /* free the allocated memseg and page structures */ 47371ae08745Sheppo kmem_free(memseg->pages, (sizeof (ldc_page_t) * memseg->npages)); 47381ae08745Sheppo kmem_free(memseg->cookies, 47391ae08745Sheppo (sizeof (ldc_mem_cookie_t) * memseg->npages)); 47404bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 47411ae08745Sheppo 47421ae08745Sheppo /* uninitialize the memory handle */ 47431ae08745Sheppo mhdl->memseg = NULL; 47441ae08745Sheppo mhdl->status = LDC_UNBOUND; 47451ae08745Sheppo 47461ae08745Sheppo D1(ldcp->id, "ldc_mem_unbind_handle: (0x%llx) unbound handle 0x%llx\n", 47471ae08745Sheppo ldcp->id, mhdl); 47481ae08745Sheppo 47491ae08745Sheppo mutex_exit(&mhdl->lock); 47501ae08745Sheppo return (0); 47511ae08745Sheppo } 47521ae08745Sheppo 47531ae08745Sheppo /* 47541ae08745Sheppo * Get information about the dring. The base address of the descriptor 47551ae08745Sheppo * ring along with the type and permission are returned back. 47561ae08745Sheppo */ 47571ae08745Sheppo int 47581ae08745Sheppo ldc_mem_info(ldc_mem_handle_t mhandle, ldc_mem_info_t *minfo) 47591ae08745Sheppo { 47601ae08745Sheppo ldc_mhdl_t *mhdl; 47611ae08745Sheppo 47621ae08745Sheppo if (mhandle == NULL) { 47631ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_info: invalid memory handle\n"); 47641ae08745Sheppo return (EINVAL); 47651ae08745Sheppo } 47661ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 47671ae08745Sheppo 47681ae08745Sheppo if (minfo == NULL) { 47691ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_info: invalid args\n"); 47701ae08745Sheppo return (EINVAL); 47711ae08745Sheppo } 47721ae08745Sheppo 47731ae08745Sheppo mutex_enter(&mhdl->lock); 47741ae08745Sheppo 47751ae08745Sheppo minfo->status = mhdl->status; 47761ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED) { 47771ae08745Sheppo minfo->vaddr = mhdl->memseg->vaddr; 47781ae08745Sheppo minfo->raddr = mhdl->memseg->raddr; 47791ae08745Sheppo minfo->mtype = mhdl->mtype; 47801ae08745Sheppo minfo->perm = mhdl->perm; 47811ae08745Sheppo } 47821ae08745Sheppo mutex_exit(&mhdl->lock); 47831ae08745Sheppo 47841ae08745Sheppo return (0); 47851ae08745Sheppo } 47861ae08745Sheppo 47871ae08745Sheppo /* 47881ae08745Sheppo * Copy data either from or to the client specified virtual address 47891ae08745Sheppo * space to or from the exported memory associated with the cookies. 47901ae08745Sheppo * The direction argument determines whether the data is read from or 47911ae08745Sheppo * written to exported memory. 47921ae08745Sheppo */ 47931ae08745Sheppo int 47941ae08745Sheppo ldc_mem_copy(ldc_handle_t handle, caddr_t vaddr, uint64_t off, size_t *size, 47951ae08745Sheppo ldc_mem_cookie_t *cookies, uint32_t ccount, uint8_t direction) 47961ae08745Sheppo { 47971ae08745Sheppo ldc_chan_t *ldcp; 47981ae08745Sheppo uint64_t local_voff, local_valign; 47991ae08745Sheppo uint64_t cookie_addr, cookie_size; 48001ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code; 48011ae08745Sheppo uint64_t export_caddr, export_poff, export_psize, export_size; 48021ae08745Sheppo uint64_t local_ra, local_poff, local_psize; 48031ae08745Sheppo uint64_t copy_size, copied_len = 0, total_bal = 0, idx = 0; 48041ae08745Sheppo pgcnt_t npages; 48051ae08745Sheppo size_t len = *size; 48061ae08745Sheppo int i, rv = 0; 48071ae08745Sheppo 48083af08d82Slm66018 uint64_t chid; 48093af08d82Slm66018 48101ae08745Sheppo if (handle == NULL) { 48111ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_copy: invalid channel handle\n"); 48121ae08745Sheppo return (EINVAL); 48131ae08745Sheppo } 48141ae08745Sheppo ldcp = (ldc_chan_t *)handle; 48153af08d82Slm66018 chid = ldcp->id; 48161ae08745Sheppo 48171ae08745Sheppo /* check to see if channel is UP */ 48181ae08745Sheppo if (ldcp->tstate != TS_UP) { 48193af08d82Slm66018 DWARN(chid, "ldc_mem_copy: (0x%llx) channel is not UP\n", 48203af08d82Slm66018 chid); 48213af08d82Slm66018 return (ECONNRESET); 48221ae08745Sheppo } 48231ae08745Sheppo 48241ae08745Sheppo /* Force address and size to be 8-byte aligned */ 48251ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 48263af08d82Slm66018 DWARN(chid, 48271ae08745Sheppo "ldc_mem_copy: addr/sz is not 8-byte aligned\n"); 48281ae08745Sheppo return (EINVAL); 48291ae08745Sheppo } 48301ae08745Sheppo 48311ae08745Sheppo /* Find the size of the exported memory */ 48321ae08745Sheppo export_size = 0; 48331ae08745Sheppo for (i = 0; i < ccount; i++) 48341ae08745Sheppo export_size += cookies[i].size; 48351ae08745Sheppo 48361ae08745Sheppo /* check to see if offset is valid */ 48371ae08745Sheppo if (off > export_size) { 48383af08d82Slm66018 DWARN(chid, 48391ae08745Sheppo "ldc_mem_copy: (0x%llx) start offset > export mem size\n", 48403af08d82Slm66018 chid); 48411ae08745Sheppo return (EINVAL); 48421ae08745Sheppo } 48431ae08745Sheppo 48441ae08745Sheppo /* 48451ae08745Sheppo * Check to see if the export size is smaller than the size we 48461ae08745Sheppo * are requesting to copy - if so flag an error 48471ae08745Sheppo */ 48481ae08745Sheppo if ((export_size - off) < *size) { 48493af08d82Slm66018 DWARN(chid, 48501ae08745Sheppo "ldc_mem_copy: (0x%llx) copy size > export mem size\n", 48513af08d82Slm66018 chid); 48521ae08745Sheppo return (EINVAL); 48531ae08745Sheppo } 48541ae08745Sheppo 48551ae08745Sheppo total_bal = min(export_size, *size); 48561ae08745Sheppo 48571ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 48581ae08745Sheppo pg_size = MMU_PAGESIZE; 48591ae08745Sheppo pg_size_code = page_szc(pg_size); 48601ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 48611ae08745Sheppo 48623af08d82Slm66018 D1(chid, "ldc_mem_copy: copying data " 48631ae08745Sheppo "(0x%llx) va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 48643af08d82Slm66018 chid, vaddr, pg_size, pg_size_code, pg_shift); 48651ae08745Sheppo 48661ae08745Sheppo /* aligned VA and its offset */ 48671ae08745Sheppo local_valign = (((uintptr_t)vaddr) & ~(pg_size - 1)); 48681ae08745Sheppo local_voff = ((uintptr_t)vaddr) & (pg_size - 1); 48691ae08745Sheppo 48701ae08745Sheppo npages = (len+local_voff)/pg_size; 48711ae08745Sheppo npages = ((len+local_voff)%pg_size == 0) ? npages : npages+1; 48721ae08745Sheppo 48733af08d82Slm66018 D1(chid, 48741ae08745Sheppo "ldc_mem_copy: (0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n", 48753af08d82Slm66018 chid, vaddr, local_valign, local_voff, npages); 48761ae08745Sheppo 48771ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 48781ae08745Sheppo local_poff = local_voff; 48791ae08745Sheppo local_psize = min(len, (pg_size - local_voff)); 48801ae08745Sheppo 48811ae08745Sheppo len -= local_psize; 48821ae08745Sheppo 48831ae08745Sheppo /* 48841ae08745Sheppo * find the first cookie in the list of cookies 48851ae08745Sheppo * if the offset passed in is not zero 48861ae08745Sheppo */ 48871ae08745Sheppo for (idx = 0; idx < ccount; idx++) { 48881ae08745Sheppo cookie_size = cookies[idx].size; 48891ae08745Sheppo if (off < cookie_size) 48901ae08745Sheppo break; 48911ae08745Sheppo off -= cookie_size; 48921ae08745Sheppo } 48931ae08745Sheppo 48941ae08745Sheppo cookie_addr = cookies[idx].addr + off; 48951ae08745Sheppo cookie_size = cookies[idx].size - off; 48961ae08745Sheppo 48971ae08745Sheppo export_caddr = cookie_addr & ~(pg_size - 1); 48981ae08745Sheppo export_poff = cookie_addr & (pg_size - 1); 48991ae08745Sheppo export_psize = min(cookie_size, (pg_size - export_poff)); 49001ae08745Sheppo 49011ae08745Sheppo for (;;) { 49021ae08745Sheppo 49031ae08745Sheppo copy_size = min(export_psize, local_psize); 49041ae08745Sheppo 49053af08d82Slm66018 D1(chid, 49061ae08745Sheppo "ldc_mem_copy:(0x%llx) dir=0x%x, caddr=0x%llx," 49071ae08745Sheppo " loc_ra=0x%llx, exp_poff=0x%llx, loc_poff=0x%llx," 49081ae08745Sheppo " exp_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx," 49091ae08745Sheppo " total_bal=0x%llx\n", 49103af08d82Slm66018 chid, direction, export_caddr, local_ra, export_poff, 49111ae08745Sheppo local_poff, export_psize, local_psize, copy_size, 49121ae08745Sheppo total_bal); 49131ae08745Sheppo 49143af08d82Slm66018 rv = hv_ldc_copy(chid, direction, 49151ae08745Sheppo (export_caddr + export_poff), (local_ra + local_poff), 49161ae08745Sheppo copy_size, &copied_len); 49171ae08745Sheppo 49181ae08745Sheppo if (rv != 0) { 49193af08d82Slm66018 int error = EIO; 49203af08d82Slm66018 uint64_t rx_hd, rx_tl; 49213af08d82Slm66018 49223af08d82Slm66018 DWARN(chid, 49233af08d82Slm66018 "ldc_mem_copy: (0x%llx) err %d during copy\n", 49243af08d82Slm66018 (unsigned long long)chid, rv); 49253af08d82Slm66018 DWARN(chid, 49263af08d82Slm66018 "ldc_mem_copy: (0x%llx) dir=0x%x, caddr=0x%lx, " 49274bac2208Snarayan "loc_ra=0x%lx, exp_poff=0x%lx, loc_poff=0x%lx," 49284bac2208Snarayan " exp_psz=0x%lx, loc_psz=0x%lx, copy_sz=0x%lx," 49294bac2208Snarayan " copied_len=0x%lx, total_bal=0x%lx\n", 49303af08d82Slm66018 chid, direction, export_caddr, local_ra, 49311ae08745Sheppo export_poff, local_poff, export_psize, local_psize, 49321ae08745Sheppo copy_size, copied_len, total_bal); 49331ae08745Sheppo 49341ae08745Sheppo *size = *size - total_bal; 49353af08d82Slm66018 49363af08d82Slm66018 /* 49373af08d82Slm66018 * check if reason for copy error was due to 49383af08d82Slm66018 * a channel reset. we need to grab the lock 49393af08d82Slm66018 * just in case we have to do a reset. 49403af08d82Slm66018 */ 49413af08d82Slm66018 mutex_enter(&ldcp->lock); 49423af08d82Slm66018 mutex_enter(&ldcp->tx_lock); 49433af08d82Slm66018 49443af08d82Slm66018 rv = hv_ldc_rx_get_state(ldcp->id, 49453af08d82Slm66018 &rx_hd, &rx_tl, &(ldcp->link_state)); 49463af08d82Slm66018 if (ldcp->link_state == LDC_CHANNEL_DOWN || 49473af08d82Slm66018 ldcp->link_state == LDC_CHANNEL_RESET) { 49483af08d82Slm66018 i_ldc_reset(ldcp, B_FALSE); 49493af08d82Slm66018 error = ECONNRESET; 49503af08d82Slm66018 } 49513af08d82Slm66018 49523af08d82Slm66018 mutex_exit(&ldcp->tx_lock); 49531ae08745Sheppo mutex_exit(&ldcp->lock); 49543af08d82Slm66018 49553af08d82Slm66018 return (error); 49561ae08745Sheppo } 49571ae08745Sheppo 49581ae08745Sheppo ASSERT(copied_len <= copy_size); 49591ae08745Sheppo 49603af08d82Slm66018 D2(chid, "ldc_mem_copy: copied=0x%llx\n", copied_len); 49611ae08745Sheppo export_poff += copied_len; 49621ae08745Sheppo local_poff += copied_len; 49631ae08745Sheppo export_psize -= copied_len; 49641ae08745Sheppo local_psize -= copied_len; 49651ae08745Sheppo cookie_size -= copied_len; 49661ae08745Sheppo 49671ae08745Sheppo total_bal -= copied_len; 49681ae08745Sheppo 49691ae08745Sheppo if (copy_size != copied_len) 49701ae08745Sheppo continue; 49711ae08745Sheppo 49721ae08745Sheppo if (export_psize == 0 && total_bal != 0) { 49731ae08745Sheppo 49741ae08745Sheppo if (cookie_size == 0) { 49751ae08745Sheppo idx++; 49761ae08745Sheppo cookie_addr = cookies[idx].addr; 49771ae08745Sheppo cookie_size = cookies[idx].size; 49781ae08745Sheppo 49791ae08745Sheppo export_caddr = cookie_addr & ~(pg_size - 1); 49801ae08745Sheppo export_poff = cookie_addr & (pg_size - 1); 49811ae08745Sheppo export_psize = 49821ae08745Sheppo min(cookie_size, (pg_size-export_poff)); 49831ae08745Sheppo } else { 49841ae08745Sheppo export_caddr += pg_size; 49851ae08745Sheppo export_poff = 0; 49861ae08745Sheppo export_psize = min(cookie_size, pg_size); 49871ae08745Sheppo } 49881ae08745Sheppo } 49891ae08745Sheppo 49901ae08745Sheppo if (local_psize == 0 && total_bal != 0) { 49911ae08745Sheppo local_valign += pg_size; 49921ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 49931ae08745Sheppo local_poff = 0; 49941ae08745Sheppo local_psize = min(pg_size, len); 49951ae08745Sheppo len -= local_psize; 49961ae08745Sheppo } 49971ae08745Sheppo 49981ae08745Sheppo /* check if we are all done */ 49991ae08745Sheppo if (total_bal == 0) 50001ae08745Sheppo break; 50011ae08745Sheppo } 50021ae08745Sheppo 50031ae08745Sheppo 50043af08d82Slm66018 D1(chid, 50051ae08745Sheppo "ldc_mem_copy: (0x%llx) done copying sz=0x%llx\n", 50063af08d82Slm66018 chid, *size); 50071ae08745Sheppo 50081ae08745Sheppo return (0); 50091ae08745Sheppo } 50101ae08745Sheppo 50111ae08745Sheppo /* 50121ae08745Sheppo * Copy data either from or to the client specified virtual address 50131ae08745Sheppo * space to or from HV physical memory. 50141ae08745Sheppo * 50151ae08745Sheppo * The direction argument determines whether the data is read from or 50161ae08745Sheppo * written to HV memory. direction values are LDC_COPY_IN/OUT similar 50171ae08745Sheppo * to the ldc_mem_copy interface 50181ae08745Sheppo */ 50191ae08745Sheppo int 50203af08d82Slm66018 ldc_mem_rdwr_cookie(ldc_handle_t handle, caddr_t vaddr, size_t *size, 50211ae08745Sheppo caddr_t paddr, uint8_t direction) 50221ae08745Sheppo { 50231ae08745Sheppo ldc_chan_t *ldcp; 50241ae08745Sheppo uint64_t local_voff, local_valign; 50251ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code; 50261ae08745Sheppo uint64_t target_pa, target_poff, target_psize, target_size; 50271ae08745Sheppo uint64_t local_ra, local_poff, local_psize; 50281ae08745Sheppo uint64_t copy_size, copied_len = 0; 50291ae08745Sheppo pgcnt_t npages; 50301ae08745Sheppo size_t len = *size; 50311ae08745Sheppo int rv = 0; 50321ae08745Sheppo 50331ae08745Sheppo if (handle == NULL) { 50341ae08745Sheppo DWARN(DBG_ALL_LDCS, 50353af08d82Slm66018 "ldc_mem_rdwr_cookie: invalid channel handle\n"); 50361ae08745Sheppo return (EINVAL); 50371ae08745Sheppo } 50381ae08745Sheppo ldcp = (ldc_chan_t *)handle; 50391ae08745Sheppo 50401ae08745Sheppo mutex_enter(&ldcp->lock); 50411ae08745Sheppo 50421ae08745Sheppo /* check to see if channel is UP */ 50431ae08745Sheppo if (ldcp->tstate != TS_UP) { 50441ae08745Sheppo DWARN(ldcp->id, 50453af08d82Slm66018 "ldc_mem_rdwr_cookie: (0x%llx) channel is not UP\n", 50461ae08745Sheppo ldcp->id); 50471ae08745Sheppo mutex_exit(&ldcp->lock); 50483af08d82Slm66018 return (ECONNRESET); 50491ae08745Sheppo } 50501ae08745Sheppo 50511ae08745Sheppo /* Force address and size to be 8-byte aligned */ 50521ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 50531ae08745Sheppo DWARN(ldcp->id, 50543af08d82Slm66018 "ldc_mem_rdwr_cookie: addr/size is not 8-byte aligned\n"); 50551ae08745Sheppo mutex_exit(&ldcp->lock); 50561ae08745Sheppo return (EINVAL); 50571ae08745Sheppo } 50581ae08745Sheppo 50591ae08745Sheppo target_size = *size; 50601ae08745Sheppo 50611ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 50621ae08745Sheppo pg_size = MMU_PAGESIZE; 50631ae08745Sheppo pg_size_code = page_szc(pg_size); 50641ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 50651ae08745Sheppo 50663af08d82Slm66018 D1(ldcp->id, "ldc_mem_rdwr_cookie: copying data " 50671ae08745Sheppo "(0x%llx) va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 50681ae08745Sheppo ldcp->id, vaddr, pg_size, pg_size_code, pg_shift); 50691ae08745Sheppo 50701ae08745Sheppo /* aligned VA and its offset */ 50711ae08745Sheppo local_valign = ((uintptr_t)vaddr) & ~(pg_size - 1); 50721ae08745Sheppo local_voff = ((uintptr_t)vaddr) & (pg_size - 1); 50731ae08745Sheppo 50741ae08745Sheppo npages = (len + local_voff) / pg_size; 50751ae08745Sheppo npages = ((len + local_voff) % pg_size == 0) ? npages : npages+1; 50761ae08745Sheppo 50773af08d82Slm66018 D1(ldcp->id, "ldc_mem_rdwr_cookie: (0x%llx) v=0x%llx, " 50783af08d82Slm66018 "val=0x%llx,off=0x%x,pgs=0x%x\n", 50791ae08745Sheppo ldcp->id, vaddr, local_valign, local_voff, npages); 50801ae08745Sheppo 50811ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 50821ae08745Sheppo local_poff = local_voff; 50831ae08745Sheppo local_psize = min(len, (pg_size - local_voff)); 50841ae08745Sheppo 50851ae08745Sheppo len -= local_psize; 50861ae08745Sheppo 50871ae08745Sheppo target_pa = ((uintptr_t)paddr) & ~(pg_size - 1); 50881ae08745Sheppo target_poff = ((uintptr_t)paddr) & (pg_size - 1); 50891ae08745Sheppo target_psize = pg_size - target_poff; 50901ae08745Sheppo 50911ae08745Sheppo for (;;) { 50921ae08745Sheppo 50931ae08745Sheppo copy_size = min(target_psize, local_psize); 50941ae08745Sheppo 50951ae08745Sheppo D1(ldcp->id, 50963af08d82Slm66018 "ldc_mem_rdwr_cookie: (0x%llx) dir=0x%x, tar_pa=0x%llx," 50971ae08745Sheppo " loc_ra=0x%llx, tar_poff=0x%llx, loc_poff=0x%llx," 50981ae08745Sheppo " tar_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx," 50991ae08745Sheppo " total_bal=0x%llx\n", 51001ae08745Sheppo ldcp->id, direction, target_pa, local_ra, target_poff, 51011ae08745Sheppo local_poff, target_psize, local_psize, copy_size, 51021ae08745Sheppo target_size); 51031ae08745Sheppo 51041ae08745Sheppo rv = hv_ldc_copy(ldcp->id, direction, 51051ae08745Sheppo (target_pa + target_poff), (local_ra + local_poff), 51061ae08745Sheppo copy_size, &copied_len); 51071ae08745Sheppo 51081ae08745Sheppo if (rv != 0) { 51093af08d82Slm66018 DWARN(DBG_ALL_LDCS, 51103af08d82Slm66018 "ldc_mem_rdwr_cookie: (0x%lx) err %d during copy\n", 51111ae08745Sheppo ldcp->id, rv); 51121ae08745Sheppo DWARN(DBG_ALL_LDCS, 51133af08d82Slm66018 "ldc_mem_rdwr_cookie: (0x%llx) dir=%lld, " 51143af08d82Slm66018 "tar_pa=0x%llx, loc_ra=0x%llx, tar_poff=0x%llx, " 51153af08d82Slm66018 "loc_poff=0x%llx, tar_psz=0x%llx, loc_psz=0x%llx, " 51163af08d82Slm66018 "copy_sz=0x%llx, total_bal=0x%llx\n", 51171ae08745Sheppo ldcp->id, direction, target_pa, local_ra, 51181ae08745Sheppo target_poff, local_poff, target_psize, local_psize, 51191ae08745Sheppo copy_size, target_size); 51201ae08745Sheppo 51211ae08745Sheppo *size = *size - target_size; 51221ae08745Sheppo mutex_exit(&ldcp->lock); 51231ae08745Sheppo return (i_ldc_h2v_error(rv)); 51241ae08745Sheppo } 51251ae08745Sheppo 51263af08d82Slm66018 D2(ldcp->id, "ldc_mem_rdwr_cookie: copied=0x%llx\n", 51273af08d82Slm66018 copied_len); 51281ae08745Sheppo target_poff += copied_len; 51291ae08745Sheppo local_poff += copied_len; 51301ae08745Sheppo target_psize -= copied_len; 51311ae08745Sheppo local_psize -= copied_len; 51321ae08745Sheppo 51331ae08745Sheppo target_size -= copied_len; 51341ae08745Sheppo 51351ae08745Sheppo if (copy_size != copied_len) 51361ae08745Sheppo continue; 51371ae08745Sheppo 51381ae08745Sheppo if (target_psize == 0 && target_size != 0) { 51391ae08745Sheppo target_pa += pg_size; 51401ae08745Sheppo target_poff = 0; 51411ae08745Sheppo target_psize = min(pg_size, target_size); 51421ae08745Sheppo } 51431ae08745Sheppo 51441ae08745Sheppo if (local_psize == 0 && target_size != 0) { 51451ae08745Sheppo local_valign += pg_size; 51461ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 51471ae08745Sheppo local_poff = 0; 51481ae08745Sheppo local_psize = min(pg_size, len); 51491ae08745Sheppo len -= local_psize; 51501ae08745Sheppo } 51511ae08745Sheppo 51521ae08745Sheppo /* check if we are all done */ 51531ae08745Sheppo if (target_size == 0) 51541ae08745Sheppo break; 51551ae08745Sheppo } 51561ae08745Sheppo 51571ae08745Sheppo mutex_exit(&ldcp->lock); 51581ae08745Sheppo 51593af08d82Slm66018 D1(ldcp->id, "ldc_mem_rdwr_cookie: (0x%llx) done copying sz=0x%llx\n", 51601ae08745Sheppo ldcp->id, *size); 51611ae08745Sheppo 51621ae08745Sheppo return (0); 51631ae08745Sheppo } 51641ae08745Sheppo 51651ae08745Sheppo /* 51661ae08745Sheppo * Map an exported memory segment into the local address space. If the 51671ae08745Sheppo * memory range was exported for direct map access, a HV call is made 51681ae08745Sheppo * to allocate a RA range. If the map is done via a shadow copy, local 51691ae08745Sheppo * shadow memory is allocated and the base VA is returned in 'vaddr'. If 51701ae08745Sheppo * the mapping is a direct map then the RA is returned in 'raddr'. 51711ae08745Sheppo */ 51721ae08745Sheppo int 51731ae08745Sheppo ldc_mem_map(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie, uint32_t ccount, 51744bac2208Snarayan uint8_t mtype, uint8_t perm, caddr_t *vaddr, caddr_t *raddr) 51751ae08745Sheppo { 51764bac2208Snarayan int i, j, idx, rv, retries; 51771ae08745Sheppo ldc_chan_t *ldcp; 51781ae08745Sheppo ldc_mhdl_t *mhdl; 51791ae08745Sheppo ldc_memseg_t *memseg; 51804bac2208Snarayan caddr_t tmpaddr; 51814bac2208Snarayan uint64_t map_perm = perm; 51824bac2208Snarayan uint64_t pg_size, pg_shift, pg_size_code, pg_mask; 51834bac2208Snarayan uint64_t exp_size = 0, base_off, map_size, npages; 51844bac2208Snarayan uint64_t cookie_addr, cookie_off, cookie_size; 51854bac2208Snarayan tte_t ldc_tte; 51861ae08745Sheppo 51871ae08745Sheppo if (mhandle == NULL) { 51881ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_map: invalid memory handle\n"); 51891ae08745Sheppo return (EINVAL); 51901ae08745Sheppo } 51911ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 51921ae08745Sheppo 51931ae08745Sheppo mutex_enter(&mhdl->lock); 51941ae08745Sheppo 51951ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED || 51961ae08745Sheppo mhdl->memseg != NULL) { 51971ae08745Sheppo DWARN(DBG_ALL_LDCS, 51981ae08745Sheppo "ldc_mem_map: (0x%llx) handle bound/mapped\n", mhandle); 51991ae08745Sheppo mutex_exit(&mhdl->lock); 52001ae08745Sheppo return (EINVAL); 52011ae08745Sheppo } 52021ae08745Sheppo 52031ae08745Sheppo ldcp = mhdl->ldcp; 52041ae08745Sheppo 52051ae08745Sheppo mutex_enter(&ldcp->lock); 52061ae08745Sheppo 52071ae08745Sheppo if (ldcp->tstate != TS_UP) { 52081ae08745Sheppo DWARN(ldcp->id, 52091ae08745Sheppo "ldc_mem_dring_map: (0x%llx) channel is not UP\n", 52101ae08745Sheppo ldcp->id); 52111ae08745Sheppo mutex_exit(&ldcp->lock); 52121ae08745Sheppo mutex_exit(&mhdl->lock); 52133af08d82Slm66018 return (ECONNRESET); 52141ae08745Sheppo } 52151ae08745Sheppo 52161ae08745Sheppo if ((mtype & (LDC_SHADOW_MAP|LDC_DIRECT_MAP|LDC_IO_MAP)) == 0) { 52171ae08745Sheppo DWARN(ldcp->id, "ldc_mem_map: invalid map type\n"); 52181ae08745Sheppo mutex_exit(&ldcp->lock); 52191ae08745Sheppo mutex_exit(&mhdl->lock); 52201ae08745Sheppo return (EINVAL); 52211ae08745Sheppo } 52221ae08745Sheppo 52231ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) cookie = 0x%llx,0x%llx\n", 5224d10e4ef2Snarayan ldcp->id, cookie->addr, cookie->size); 52251ae08745Sheppo 52261ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 52271ae08745Sheppo pg_size = MMU_PAGESIZE; 52281ae08745Sheppo pg_size_code = page_szc(pg_size); 52291ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 52304bac2208Snarayan pg_mask = ~(pg_size - 1); 52311ae08745Sheppo 52321ae08745Sheppo /* calculate the number of pages in the exported cookie */ 52334bac2208Snarayan base_off = cookie[0].addr & (pg_size - 1); 52344bac2208Snarayan for (idx = 0; idx < ccount; idx++) 52351ae08745Sheppo exp_size += cookie[idx].size; 52364bac2208Snarayan map_size = P2ROUNDUP((exp_size + base_off), pg_size); 52374bac2208Snarayan npages = (map_size >> pg_shift); 52381ae08745Sheppo 52391ae08745Sheppo /* Allocate memseg structure */ 52404bac2208Snarayan memseg = mhdl->memseg = 52414bac2208Snarayan kmem_cache_alloc(ldcssp->memseg_cache, KM_SLEEP); 52421ae08745Sheppo 52431ae08745Sheppo /* Allocate memory to store all pages and cookies */ 52441ae08745Sheppo memseg->pages = kmem_zalloc((sizeof (ldc_page_t) * npages), KM_SLEEP); 52451ae08745Sheppo memseg->cookies = 52461ae08745Sheppo kmem_zalloc((sizeof (ldc_mem_cookie_t) * ccount), KM_SLEEP); 52471ae08745Sheppo 52484bac2208Snarayan D2(ldcp->id, "ldc_mem_map: (0x%llx) exp_size=0x%llx, map_size=0x%llx," 52494bac2208Snarayan "pages=0x%llx\n", ldcp->id, exp_size, map_size, npages); 52501ae08745Sheppo 52514bac2208Snarayan /* 52524bac2208Snarayan * Check if direct map over shared memory is enabled, if not change 52534bac2208Snarayan * the mapping type to SHADOW_MAP. 52544bac2208Snarayan */ 52554bac2208Snarayan if (ldc_shmem_enabled == 0) 52564bac2208Snarayan mtype = LDC_SHADOW_MAP; 52574bac2208Snarayan 52584bac2208Snarayan /* 52594bac2208Snarayan * Check to see if the client is requesting direct or shadow map 52604bac2208Snarayan * If direct map is requested, try to map remote memory first, 52614bac2208Snarayan * and if that fails, revert to shadow map 52624bac2208Snarayan */ 52634bac2208Snarayan if (mtype == LDC_DIRECT_MAP) { 52644bac2208Snarayan 52654bac2208Snarayan /* Allocate kernel virtual space for mapping */ 52664bac2208Snarayan memseg->vaddr = vmem_xalloc(heap_arena, map_size, 52674bac2208Snarayan pg_size, 0, 0, NULL, NULL, VM_NOSLEEP); 52684bac2208Snarayan if (memseg->vaddr == NULL) { 52694bac2208Snarayan cmn_err(CE_WARN, 52704bac2208Snarayan "ldc_mem_map: (0x%lx) memory map failed\n", 52714bac2208Snarayan ldcp->id); 52724bac2208Snarayan kmem_free(memseg->cookies, 52734bac2208Snarayan (sizeof (ldc_mem_cookie_t) * ccount)); 52744bac2208Snarayan kmem_free(memseg->pages, 52754bac2208Snarayan (sizeof (ldc_page_t) * npages)); 52764bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 52774bac2208Snarayan 52784bac2208Snarayan mutex_exit(&ldcp->lock); 52794bac2208Snarayan mutex_exit(&mhdl->lock); 52804bac2208Snarayan return (ENOMEM); 52814bac2208Snarayan } 52824bac2208Snarayan 52834bac2208Snarayan /* Unload previous mapping */ 52844bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, map_size, 52854bac2208Snarayan HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK); 52864bac2208Snarayan 52874bac2208Snarayan /* for each cookie passed in - map into address space */ 52884bac2208Snarayan idx = 0; 52894bac2208Snarayan cookie_size = 0; 52904bac2208Snarayan tmpaddr = memseg->vaddr; 52914bac2208Snarayan 52924bac2208Snarayan for (i = 0; i < npages; i++) { 52934bac2208Snarayan 52944bac2208Snarayan if (cookie_size == 0) { 52954bac2208Snarayan ASSERT(idx < ccount); 52964bac2208Snarayan cookie_addr = cookie[idx].addr & pg_mask; 52974bac2208Snarayan cookie_off = cookie[idx].addr & (pg_size - 1); 52984bac2208Snarayan cookie_size = 52994bac2208Snarayan P2ROUNDUP((cookie_off + cookie[idx].size), 53004bac2208Snarayan pg_size); 53014bac2208Snarayan idx++; 53024bac2208Snarayan } 53034bac2208Snarayan 53044bac2208Snarayan D1(ldcp->id, "ldc_mem_map: (0x%llx) mapping " 53054bac2208Snarayan "cookie 0x%llx, bal=0x%llx\n", ldcp->id, 53064bac2208Snarayan cookie_addr, cookie_size); 53074bac2208Snarayan 53084bac2208Snarayan /* map the cookie into address space */ 53094bac2208Snarayan for (retries = 0; retries < ldc_max_retries; 53104bac2208Snarayan retries++) { 53114bac2208Snarayan 53124bac2208Snarayan rv = hv_ldc_mapin(ldcp->id, cookie_addr, 53134bac2208Snarayan &memseg->pages[i].raddr, &map_perm); 53144bac2208Snarayan if (rv != H_EWOULDBLOCK && rv != H_ETOOMANY) 53154bac2208Snarayan break; 53164bac2208Snarayan 53174bac2208Snarayan drv_usecwait(ldc_delay); 53184bac2208Snarayan } 53194bac2208Snarayan 53204bac2208Snarayan if (rv || memseg->pages[i].raddr == 0) { 53214bac2208Snarayan DWARN(ldcp->id, 53224bac2208Snarayan "ldc_mem_map: (0x%llx) hv mapin err %d\n", 53234bac2208Snarayan ldcp->id, rv); 53244bac2208Snarayan 53254bac2208Snarayan /* remove previous mapins */ 53264bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, map_size, 53274bac2208Snarayan HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK); 53284bac2208Snarayan for (j = 0; j < i; j++) { 53294bac2208Snarayan rv = hv_ldc_unmap( 53304bac2208Snarayan memseg->pages[j].raddr); 53314bac2208Snarayan if (rv) { 53324bac2208Snarayan DWARN(ldcp->id, 53334bac2208Snarayan "ldc_mem_map: (0x%llx) " 53344bac2208Snarayan "cannot unmap ra=0x%llx\n", 53354bac2208Snarayan ldcp->id, 53364bac2208Snarayan memseg->pages[j].raddr); 53374bac2208Snarayan } 53384bac2208Snarayan } 53394bac2208Snarayan 53404bac2208Snarayan /* free kernel virtual space */ 53414bac2208Snarayan vmem_free(heap_arena, (void *)memseg->vaddr, 53427636cb21Slm66018 map_size); 53434bac2208Snarayan 53444bac2208Snarayan /* direct map failed - revert to shadow map */ 53454bac2208Snarayan mtype = LDC_SHADOW_MAP; 53464bac2208Snarayan break; 53474bac2208Snarayan 53484bac2208Snarayan } else { 53494bac2208Snarayan 53504bac2208Snarayan D1(ldcp->id, 53514bac2208Snarayan "ldc_mem_map: (0x%llx) vtop map 0x%llx -> " 53524bac2208Snarayan "0x%llx, cookie=0x%llx, perm=0x%llx\n", 53534bac2208Snarayan ldcp->id, tmpaddr, memseg->pages[i].raddr, 53544bac2208Snarayan cookie_addr, perm); 53554bac2208Snarayan 53564bac2208Snarayan /* 53574bac2208Snarayan * NOTE: Calling hat_devload directly, causes it 53584bac2208Snarayan * to look for page_t using the pfn. Since this 53594bac2208Snarayan * addr is greater than the memlist, it treates 53604bac2208Snarayan * it as non-memory 53614bac2208Snarayan */ 53624bac2208Snarayan sfmmu_memtte(&ldc_tte, 53634bac2208Snarayan (pfn_t)(memseg->pages[i].raddr >> pg_shift), 53644bac2208Snarayan PROT_READ | PROT_WRITE | HAT_NOSYNC, TTE8K); 53654bac2208Snarayan 53664bac2208Snarayan D1(ldcp->id, 53674bac2208Snarayan "ldc_mem_map: (0x%llx) ra 0x%llx -> " 53684bac2208Snarayan "tte 0x%llx\n", ldcp->id, 53694bac2208Snarayan memseg->pages[i].raddr, ldc_tte); 53704bac2208Snarayan 53714bac2208Snarayan sfmmu_tteload(kas.a_hat, &ldc_tte, tmpaddr, 53724bac2208Snarayan NULL, HAT_LOAD_LOCK); 53734bac2208Snarayan 53744bac2208Snarayan cookie_size -= pg_size; 53754bac2208Snarayan cookie_addr += pg_size; 53764bac2208Snarayan tmpaddr += pg_size; 53774bac2208Snarayan } 53784bac2208Snarayan } 53794bac2208Snarayan } 53804bac2208Snarayan 53811ae08745Sheppo if (mtype == LDC_SHADOW_MAP) { 53821ae08745Sheppo if (*vaddr == NULL) { 53833af08d82Slm66018 memseg->vaddr = kmem_zalloc(exp_size, KM_SLEEP); 53841ae08745Sheppo mhdl->myshadow = B_TRUE; 53851ae08745Sheppo 53861ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) allocated " 53874bac2208Snarayan "shadow page va=0x%llx\n", ldcp->id, memseg->vaddr); 53881ae08745Sheppo } else { 53891ae08745Sheppo /* 53904bac2208Snarayan * Use client supplied memory for memseg->vaddr 53911ae08745Sheppo * WARNING: assuming that client mem is >= exp_size 53921ae08745Sheppo */ 53934bac2208Snarayan memseg->vaddr = *vaddr; 53941ae08745Sheppo } 53951ae08745Sheppo 53961ae08745Sheppo /* Save all page and cookie information */ 53974bac2208Snarayan for (i = 0, tmpaddr = memseg->vaddr; i < npages; i++) { 53981ae08745Sheppo memseg->pages[i].raddr = va_to_pa(tmpaddr); 53991ae08745Sheppo memseg->pages[i].size = pg_size; 54001ae08745Sheppo tmpaddr += pg_size; 54011ae08745Sheppo } 54024bac2208Snarayan 54031ae08745Sheppo } 54041ae08745Sheppo 54054bac2208Snarayan /* save all cookies */ 54064bac2208Snarayan bcopy(cookie, memseg->cookies, ccount * sizeof (ldc_mem_cookie_t)); 54074bac2208Snarayan 54081ae08745Sheppo /* update memseg_t */ 54091ae08745Sheppo memseg->raddr = memseg->pages[0].raddr; 54104bac2208Snarayan memseg->size = (mtype == LDC_SHADOW_MAP) ? exp_size : map_size; 54111ae08745Sheppo memseg->npages = npages; 54121ae08745Sheppo memseg->ncookies = ccount; 54131ae08745Sheppo memseg->next_cookie = 0; 54141ae08745Sheppo 54151ae08745Sheppo /* memory handle = mapped */ 54161ae08745Sheppo mhdl->mtype = mtype; 54174bac2208Snarayan mhdl->perm = perm; 54181ae08745Sheppo mhdl->status = LDC_MAPPED; 54191ae08745Sheppo 54201ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) mapped 0x%llx, ra=0x%llx, " 54211ae08745Sheppo "va=0x%llx, pgs=0x%llx cookies=0x%llx\n", 54221ae08745Sheppo ldcp->id, mhdl, memseg->raddr, memseg->vaddr, 54231ae08745Sheppo memseg->npages, memseg->ncookies); 54241ae08745Sheppo 54254bac2208Snarayan if (mtype == LDC_SHADOW_MAP) 54264bac2208Snarayan base_off = 0; 54271ae08745Sheppo if (raddr) 54284bac2208Snarayan *raddr = (caddr_t)(memseg->raddr | base_off); 54291ae08745Sheppo if (vaddr) 54304bac2208Snarayan *vaddr = (caddr_t)((uintptr_t)memseg->vaddr | base_off); 54311ae08745Sheppo 54321ae08745Sheppo mutex_exit(&ldcp->lock); 54331ae08745Sheppo mutex_exit(&mhdl->lock); 54341ae08745Sheppo return (0); 54351ae08745Sheppo } 54361ae08745Sheppo 54371ae08745Sheppo /* 54381ae08745Sheppo * Unmap a memory segment. Free shadow memory (if any). 54391ae08745Sheppo */ 54401ae08745Sheppo int 54411ae08745Sheppo ldc_mem_unmap(ldc_mem_handle_t mhandle) 54421ae08745Sheppo { 54434bac2208Snarayan int i, rv; 54441ae08745Sheppo ldc_mhdl_t *mhdl = (ldc_mhdl_t *)mhandle; 54451ae08745Sheppo ldc_chan_t *ldcp; 54461ae08745Sheppo ldc_memseg_t *memseg; 54471ae08745Sheppo 54481ae08745Sheppo if (mhdl == 0 || mhdl->status != LDC_MAPPED) { 54491ae08745Sheppo DWARN(DBG_ALL_LDCS, 54501ae08745Sheppo "ldc_mem_unmap: (0x%llx) handle is not mapped\n", 54511ae08745Sheppo mhandle); 54521ae08745Sheppo return (EINVAL); 54531ae08745Sheppo } 54541ae08745Sheppo 54551ae08745Sheppo mutex_enter(&mhdl->lock); 54561ae08745Sheppo 54571ae08745Sheppo ldcp = mhdl->ldcp; 54581ae08745Sheppo memseg = mhdl->memseg; 54591ae08745Sheppo 54601ae08745Sheppo D1(ldcp->id, "ldc_mem_unmap: (0x%llx) unmapping handle 0x%llx\n", 54611ae08745Sheppo ldcp->id, mhdl); 54621ae08745Sheppo 54631ae08745Sheppo /* if we allocated shadow memory - free it */ 54641ae08745Sheppo if (mhdl->mtype == LDC_SHADOW_MAP && mhdl->myshadow) { 54653af08d82Slm66018 kmem_free(memseg->vaddr, memseg->size); 54664bac2208Snarayan } else if (mhdl->mtype == LDC_DIRECT_MAP) { 54674bac2208Snarayan 54684bac2208Snarayan /* unmap in the case of DIRECT_MAP */ 54694bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, memseg->size, 54704bac2208Snarayan HAT_UNLOAD_UNLOCK); 54714bac2208Snarayan 54724bac2208Snarayan for (i = 0; i < memseg->npages; i++) { 54734bac2208Snarayan rv = hv_ldc_unmap(memseg->pages[i].raddr); 54744bac2208Snarayan if (rv) { 54754bac2208Snarayan cmn_err(CE_WARN, 54764bac2208Snarayan "ldc_mem_map: (0x%lx) hv unmap err %d\n", 54774bac2208Snarayan ldcp->id, rv); 54784bac2208Snarayan } 54794bac2208Snarayan } 54804bac2208Snarayan 54814bac2208Snarayan vmem_free(heap_arena, (void *)memseg->vaddr, memseg->size); 54821ae08745Sheppo } 54831ae08745Sheppo 54841ae08745Sheppo /* free the allocated memseg and page structures */ 54851ae08745Sheppo kmem_free(memseg->pages, (sizeof (ldc_page_t) * memseg->npages)); 54861ae08745Sheppo kmem_free(memseg->cookies, 54871ae08745Sheppo (sizeof (ldc_mem_cookie_t) * memseg->ncookies)); 54884bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 54891ae08745Sheppo 54901ae08745Sheppo /* uninitialize the memory handle */ 54911ae08745Sheppo mhdl->memseg = NULL; 54921ae08745Sheppo mhdl->status = LDC_UNBOUND; 54931ae08745Sheppo 54941ae08745Sheppo D1(ldcp->id, "ldc_mem_unmap: (0x%llx) unmapped handle 0x%llx\n", 54951ae08745Sheppo ldcp->id, mhdl); 54961ae08745Sheppo 54971ae08745Sheppo mutex_exit(&mhdl->lock); 54981ae08745Sheppo return (0); 54991ae08745Sheppo } 55001ae08745Sheppo 55011ae08745Sheppo /* 55021ae08745Sheppo * Internal entry point for LDC mapped memory entry consistency 55031ae08745Sheppo * semantics. Acquire copies the contents of the remote memory 55041ae08745Sheppo * into the local shadow copy. The release operation copies the local 55051ae08745Sheppo * contents into the remote memory. The offset and size specify the 55061ae08745Sheppo * bounds for the memory range being synchronized. 55071ae08745Sheppo */ 55081ae08745Sheppo static int 55091ae08745Sheppo i_ldc_mem_acquire_release(ldc_mem_handle_t mhandle, uint8_t direction, 55101ae08745Sheppo uint64_t offset, size_t size) 55111ae08745Sheppo { 55121ae08745Sheppo int err; 55131ae08745Sheppo ldc_mhdl_t *mhdl; 55141ae08745Sheppo ldc_chan_t *ldcp; 55151ae08745Sheppo ldc_memseg_t *memseg; 55161ae08745Sheppo caddr_t local_vaddr; 55171ae08745Sheppo size_t copy_size; 55181ae08745Sheppo 55191ae08745Sheppo if (mhandle == NULL) { 55201ae08745Sheppo DWARN(DBG_ALL_LDCS, 55211ae08745Sheppo "i_ldc_mem_acquire_release: invalid memory handle\n"); 55221ae08745Sheppo return (EINVAL); 55231ae08745Sheppo } 55241ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 55251ae08745Sheppo 55261ae08745Sheppo mutex_enter(&mhdl->lock); 55271ae08745Sheppo 55281ae08745Sheppo if (mhdl->status != LDC_MAPPED || mhdl->ldcp == NULL) { 55291ae08745Sheppo DWARN(DBG_ALL_LDCS, 55301ae08745Sheppo "i_ldc_mem_acquire_release: not mapped memory\n"); 55311ae08745Sheppo mutex_exit(&mhdl->lock); 55321ae08745Sheppo return (EINVAL); 55331ae08745Sheppo } 55341ae08745Sheppo 55354bac2208Snarayan /* do nothing for direct map */ 55364bac2208Snarayan if (mhdl->mtype == LDC_DIRECT_MAP) { 55374bac2208Snarayan mutex_exit(&mhdl->lock); 55384bac2208Snarayan return (0); 55394bac2208Snarayan } 55404bac2208Snarayan 55414bac2208Snarayan /* do nothing if COPY_IN+MEM_W and COPY_OUT+MEM_R */ 55424bac2208Snarayan if ((direction == LDC_COPY_IN && (mhdl->perm & LDC_MEM_R) == 0) || 55434bac2208Snarayan (direction == LDC_COPY_OUT && (mhdl->perm & LDC_MEM_W) == 0)) { 55444bac2208Snarayan mutex_exit(&mhdl->lock); 55454bac2208Snarayan return (0); 55464bac2208Snarayan } 55474bac2208Snarayan 55481ae08745Sheppo if (offset >= mhdl->memseg->size || 55491ae08745Sheppo (offset + size) > mhdl->memseg->size) { 55501ae08745Sheppo DWARN(DBG_ALL_LDCS, 55511ae08745Sheppo "i_ldc_mem_acquire_release: memory out of range\n"); 55521ae08745Sheppo mutex_exit(&mhdl->lock); 55531ae08745Sheppo return (EINVAL); 55541ae08745Sheppo } 55551ae08745Sheppo 55561ae08745Sheppo /* get the channel handle and memory segment */ 55571ae08745Sheppo ldcp = mhdl->ldcp; 55581ae08745Sheppo memseg = mhdl->memseg; 55591ae08745Sheppo 55601ae08745Sheppo if (mhdl->mtype == LDC_SHADOW_MAP) { 55611ae08745Sheppo 55621ae08745Sheppo local_vaddr = memseg->vaddr + offset; 55631ae08745Sheppo copy_size = size; 55641ae08745Sheppo 55651ae08745Sheppo /* copy to/from remote from/to local memory */ 55661ae08745Sheppo err = ldc_mem_copy((ldc_handle_t)ldcp, local_vaddr, offset, 55671ae08745Sheppo ©_size, memseg->cookies, memseg->ncookies, 55681ae08745Sheppo direction); 55691ae08745Sheppo if (err || copy_size != size) { 55701ae08745Sheppo cmn_err(CE_WARN, 55711ae08745Sheppo "i_ldc_mem_acquire_release: copy failed\n"); 55721ae08745Sheppo mutex_exit(&mhdl->lock); 55731ae08745Sheppo return (err); 55741ae08745Sheppo } 55751ae08745Sheppo } 55761ae08745Sheppo 55771ae08745Sheppo mutex_exit(&mhdl->lock); 55781ae08745Sheppo 55791ae08745Sheppo return (0); 55801ae08745Sheppo } 55811ae08745Sheppo 55821ae08745Sheppo /* 55831ae08745Sheppo * Ensure that the contents in the remote memory seg are consistent 55841ae08745Sheppo * with the contents if of local segment 55851ae08745Sheppo */ 55861ae08745Sheppo int 55871ae08745Sheppo ldc_mem_acquire(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size) 55881ae08745Sheppo { 55891ae08745Sheppo return (i_ldc_mem_acquire_release(mhandle, LDC_COPY_IN, offset, size)); 55901ae08745Sheppo } 55911ae08745Sheppo 55921ae08745Sheppo 55931ae08745Sheppo /* 55941ae08745Sheppo * Ensure that the contents in the local memory seg are consistent 55951ae08745Sheppo * with the contents if of remote segment 55961ae08745Sheppo */ 55971ae08745Sheppo int 55981ae08745Sheppo ldc_mem_release(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size) 55991ae08745Sheppo { 56001ae08745Sheppo return (i_ldc_mem_acquire_release(mhandle, LDC_COPY_OUT, offset, size)); 56011ae08745Sheppo } 56021ae08745Sheppo 56031ae08745Sheppo /* 56041ae08745Sheppo * Allocate a descriptor ring. The size of each each descriptor 56051ae08745Sheppo * must be 8-byte aligned and the entire ring should be a multiple 56061ae08745Sheppo * of MMU_PAGESIZE. 56071ae08745Sheppo */ 56081ae08745Sheppo int 56091ae08745Sheppo ldc_mem_dring_create(uint32_t len, uint32_t dsize, ldc_dring_handle_t *dhandle) 56101ae08745Sheppo { 56111ae08745Sheppo ldc_dring_t *dringp; 56121ae08745Sheppo size_t size = (dsize * len); 56131ae08745Sheppo 56141ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_create: len=0x%x, size=0x%x\n", 56151ae08745Sheppo len, dsize); 56161ae08745Sheppo 56171ae08745Sheppo if (dhandle == NULL) { 56181ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid dhandle\n"); 56191ae08745Sheppo return (EINVAL); 56201ae08745Sheppo } 56211ae08745Sheppo 56221ae08745Sheppo if (len == 0) { 56231ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid length\n"); 56241ae08745Sheppo return (EINVAL); 56251ae08745Sheppo } 56261ae08745Sheppo 56271ae08745Sheppo /* descriptor size should be 8-byte aligned */ 56281ae08745Sheppo if (dsize == 0 || (dsize & 0x7)) { 56291ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid size\n"); 56301ae08745Sheppo return (EINVAL); 56311ae08745Sheppo } 56321ae08745Sheppo 56331ae08745Sheppo *dhandle = 0; 56341ae08745Sheppo 56351ae08745Sheppo /* Allocate a desc ring structure */ 56361ae08745Sheppo dringp = kmem_zalloc(sizeof (ldc_dring_t), KM_SLEEP); 56371ae08745Sheppo 56381ae08745Sheppo /* Initialize dring */ 56391ae08745Sheppo dringp->length = len; 56401ae08745Sheppo dringp->dsize = dsize; 56411ae08745Sheppo 56421ae08745Sheppo /* round off to multiple of pagesize */ 56431ae08745Sheppo dringp->size = (size & MMU_PAGEMASK); 56441ae08745Sheppo if (size & MMU_PAGEOFFSET) 56451ae08745Sheppo dringp->size += MMU_PAGESIZE; 56461ae08745Sheppo 56471ae08745Sheppo dringp->status = LDC_UNBOUND; 56481ae08745Sheppo 56491ae08745Sheppo /* allocate descriptor ring memory */ 56503af08d82Slm66018 dringp->base = kmem_zalloc(dringp->size, KM_SLEEP); 56511ae08745Sheppo 56521ae08745Sheppo /* initialize the desc ring lock */ 56531ae08745Sheppo mutex_init(&dringp->lock, NULL, MUTEX_DRIVER, NULL); 56541ae08745Sheppo 56551ae08745Sheppo /* Add descriptor ring to the head of global list */ 56561ae08745Sheppo mutex_enter(&ldcssp->lock); 56571ae08745Sheppo dringp->next = ldcssp->dring_list; 56581ae08745Sheppo ldcssp->dring_list = dringp; 56591ae08745Sheppo mutex_exit(&ldcssp->lock); 56601ae08745Sheppo 56611ae08745Sheppo *dhandle = (ldc_dring_handle_t)dringp; 56621ae08745Sheppo 56631ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_create: dring allocated\n"); 56641ae08745Sheppo 56651ae08745Sheppo return (0); 56661ae08745Sheppo } 56671ae08745Sheppo 56681ae08745Sheppo 56691ae08745Sheppo /* 56701ae08745Sheppo * Destroy a descriptor ring. 56711ae08745Sheppo */ 56721ae08745Sheppo int 56731ae08745Sheppo ldc_mem_dring_destroy(ldc_dring_handle_t dhandle) 56741ae08745Sheppo { 56751ae08745Sheppo ldc_dring_t *dringp; 56761ae08745Sheppo ldc_dring_t *tmp_dringp; 56771ae08745Sheppo 56781ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_destroy: entered\n"); 56791ae08745Sheppo 56801ae08745Sheppo if (dhandle == NULL) { 56811ae08745Sheppo DWARN(DBG_ALL_LDCS, 56821ae08745Sheppo "ldc_mem_dring_destroy: invalid desc ring handle\n"); 56831ae08745Sheppo return (EINVAL); 56841ae08745Sheppo } 56851ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 56861ae08745Sheppo 56871ae08745Sheppo if (dringp->status == LDC_BOUND) { 56881ae08745Sheppo DWARN(DBG_ALL_LDCS, 56891ae08745Sheppo "ldc_mem_dring_destroy: desc ring is bound\n"); 56901ae08745Sheppo return (EACCES); 56911ae08745Sheppo } 56921ae08745Sheppo 56931ae08745Sheppo mutex_enter(&dringp->lock); 56941ae08745Sheppo mutex_enter(&ldcssp->lock); 56951ae08745Sheppo 56961ae08745Sheppo /* remove from linked list - if not bound */ 56971ae08745Sheppo tmp_dringp = ldcssp->dring_list; 56981ae08745Sheppo if (tmp_dringp == dringp) { 56991ae08745Sheppo ldcssp->dring_list = dringp->next; 57001ae08745Sheppo dringp->next = NULL; 57011ae08745Sheppo 57021ae08745Sheppo } else { 57031ae08745Sheppo while (tmp_dringp != NULL) { 57041ae08745Sheppo if (tmp_dringp->next == dringp) { 57051ae08745Sheppo tmp_dringp->next = dringp->next; 57061ae08745Sheppo dringp->next = NULL; 57071ae08745Sheppo break; 57081ae08745Sheppo } 57091ae08745Sheppo tmp_dringp = tmp_dringp->next; 57101ae08745Sheppo } 57111ae08745Sheppo if (tmp_dringp == NULL) { 57121ae08745Sheppo DWARN(DBG_ALL_LDCS, 57131ae08745Sheppo "ldc_mem_dring_destroy: invalid descriptor\n"); 57141ae08745Sheppo mutex_exit(&ldcssp->lock); 57151ae08745Sheppo mutex_exit(&dringp->lock); 57161ae08745Sheppo return (EINVAL); 57171ae08745Sheppo } 57181ae08745Sheppo } 57191ae08745Sheppo 57201ae08745Sheppo mutex_exit(&ldcssp->lock); 57211ae08745Sheppo 57221ae08745Sheppo /* free the descriptor ring */ 57233af08d82Slm66018 kmem_free(dringp->base, dringp->size); 57241ae08745Sheppo 57251ae08745Sheppo mutex_exit(&dringp->lock); 57261ae08745Sheppo 57271ae08745Sheppo /* destroy dring lock */ 57281ae08745Sheppo mutex_destroy(&dringp->lock); 57291ae08745Sheppo 57301ae08745Sheppo /* free desc ring object */ 57311ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 57321ae08745Sheppo 57331ae08745Sheppo return (0); 57341ae08745Sheppo } 57351ae08745Sheppo 57361ae08745Sheppo /* 57371ae08745Sheppo * Bind a previously allocated dring to a channel. The channel should 57381ae08745Sheppo * be OPEN in order to bind the ring to the channel. Returns back a 57391ae08745Sheppo * descriptor ring cookie. The descriptor ring is exported for remote 57401ae08745Sheppo * access by the client at the other end of the channel. An entry for 57411ae08745Sheppo * dring pages is stored in map table (via call to ldc_mem_bind_handle). 57421ae08745Sheppo */ 57431ae08745Sheppo int 57441ae08745Sheppo ldc_mem_dring_bind(ldc_handle_t handle, ldc_dring_handle_t dhandle, 57451ae08745Sheppo uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount) 57461ae08745Sheppo { 57471ae08745Sheppo int err; 57481ae08745Sheppo ldc_chan_t *ldcp; 57491ae08745Sheppo ldc_dring_t *dringp; 57501ae08745Sheppo ldc_mem_handle_t mhandle; 57511ae08745Sheppo 57521ae08745Sheppo /* check to see if channel is initalized */ 57531ae08745Sheppo if (handle == NULL) { 57541ae08745Sheppo DWARN(DBG_ALL_LDCS, 57551ae08745Sheppo "ldc_mem_dring_bind: invalid channel handle\n"); 57561ae08745Sheppo return (EINVAL); 57571ae08745Sheppo } 57581ae08745Sheppo ldcp = (ldc_chan_t *)handle; 57591ae08745Sheppo 57601ae08745Sheppo if (dhandle == NULL) { 57611ae08745Sheppo DWARN(DBG_ALL_LDCS, 57621ae08745Sheppo "ldc_mem_dring_bind: invalid desc ring handle\n"); 57631ae08745Sheppo return (EINVAL); 57641ae08745Sheppo } 57651ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 57661ae08745Sheppo 57671ae08745Sheppo if (cookie == NULL) { 57681ae08745Sheppo DWARN(ldcp->id, 57691ae08745Sheppo "ldc_mem_dring_bind: invalid cookie arg\n"); 57701ae08745Sheppo return (EINVAL); 57711ae08745Sheppo } 57721ae08745Sheppo 57731ae08745Sheppo mutex_enter(&dringp->lock); 57741ae08745Sheppo 57751ae08745Sheppo if (dringp->status == LDC_BOUND) { 57761ae08745Sheppo DWARN(DBG_ALL_LDCS, 57771ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) descriptor ring is bound\n", 57781ae08745Sheppo ldcp->id); 57791ae08745Sheppo mutex_exit(&dringp->lock); 57801ae08745Sheppo return (EINVAL); 57811ae08745Sheppo } 57821ae08745Sheppo 57831ae08745Sheppo if ((perm & LDC_MEM_RW) == 0) { 57841ae08745Sheppo DWARN(DBG_ALL_LDCS, 57851ae08745Sheppo "ldc_mem_dring_bind: invalid permissions\n"); 57861ae08745Sheppo mutex_exit(&dringp->lock); 57871ae08745Sheppo return (EINVAL); 57881ae08745Sheppo } 57891ae08745Sheppo 57901ae08745Sheppo if ((mtype & (LDC_SHADOW_MAP|LDC_DIRECT_MAP|LDC_IO_MAP)) == 0) { 57911ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_bind: invalid type\n"); 57921ae08745Sheppo mutex_exit(&dringp->lock); 57931ae08745Sheppo return (EINVAL); 57941ae08745Sheppo } 57951ae08745Sheppo 57961ae08745Sheppo dringp->ldcp = ldcp; 57971ae08745Sheppo 57981ae08745Sheppo /* create an memory handle */ 57991ae08745Sheppo err = ldc_mem_alloc_handle(handle, &mhandle); 58001ae08745Sheppo if (err || mhandle == NULL) { 58011ae08745Sheppo DWARN(DBG_ALL_LDCS, 58021ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) error allocating mhandle\n", 58031ae08745Sheppo ldcp->id); 58041ae08745Sheppo mutex_exit(&dringp->lock); 58051ae08745Sheppo return (err); 58061ae08745Sheppo } 58071ae08745Sheppo dringp->mhdl = mhandle; 58081ae08745Sheppo 58091ae08745Sheppo /* bind the descriptor ring to channel */ 58101ae08745Sheppo err = ldc_mem_bind_handle(mhandle, dringp->base, dringp->size, 58111ae08745Sheppo mtype, perm, cookie, ccount); 58121ae08745Sheppo if (err) { 58131ae08745Sheppo DWARN(ldcp->id, 58141ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) error binding mhandle\n", 58151ae08745Sheppo ldcp->id); 58161ae08745Sheppo mutex_exit(&dringp->lock); 58171ae08745Sheppo return (err); 58181ae08745Sheppo } 58191ae08745Sheppo 58201ae08745Sheppo /* 58211ae08745Sheppo * For now return error if we get more than one cookie 58221ae08745Sheppo * FUTURE: Return multiple cookies .. 58231ae08745Sheppo */ 58241ae08745Sheppo if (*ccount > 1) { 58251ae08745Sheppo (void) ldc_mem_unbind_handle(mhandle); 58261ae08745Sheppo (void) ldc_mem_free_handle(mhandle); 58271ae08745Sheppo 58281ae08745Sheppo dringp->ldcp = NULL; 58291ae08745Sheppo dringp->mhdl = NULL; 58301ae08745Sheppo *ccount = 0; 58311ae08745Sheppo 58321ae08745Sheppo mutex_exit(&dringp->lock); 58331ae08745Sheppo return (EAGAIN); 58341ae08745Sheppo } 58351ae08745Sheppo 58361ae08745Sheppo /* Add descriptor ring to channel's exported dring list */ 58371ae08745Sheppo mutex_enter(&ldcp->exp_dlist_lock); 58381ae08745Sheppo dringp->ch_next = ldcp->exp_dring_list; 58391ae08745Sheppo ldcp->exp_dring_list = dringp; 58401ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 58411ae08745Sheppo 58421ae08745Sheppo dringp->status = LDC_BOUND; 58431ae08745Sheppo 58441ae08745Sheppo mutex_exit(&dringp->lock); 58451ae08745Sheppo 58461ae08745Sheppo return (0); 58471ae08745Sheppo } 58481ae08745Sheppo 58491ae08745Sheppo /* 58501ae08745Sheppo * Return the next cookie associated with the specified dring handle 58511ae08745Sheppo */ 58521ae08745Sheppo int 58531ae08745Sheppo ldc_mem_dring_nextcookie(ldc_dring_handle_t dhandle, ldc_mem_cookie_t *cookie) 58541ae08745Sheppo { 58551ae08745Sheppo int rv = 0; 58561ae08745Sheppo ldc_dring_t *dringp; 58571ae08745Sheppo ldc_chan_t *ldcp; 58581ae08745Sheppo 58591ae08745Sheppo if (dhandle == NULL) { 58601ae08745Sheppo DWARN(DBG_ALL_LDCS, 58611ae08745Sheppo "ldc_mem_dring_nextcookie: invalid desc ring handle\n"); 58621ae08745Sheppo return (EINVAL); 58631ae08745Sheppo } 58641ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 58651ae08745Sheppo mutex_enter(&dringp->lock); 58661ae08745Sheppo 58671ae08745Sheppo if (dringp->status != LDC_BOUND) { 58681ae08745Sheppo DWARN(DBG_ALL_LDCS, 58691ae08745Sheppo "ldc_mem_dring_nextcookie: descriptor ring 0x%llx " 58701ae08745Sheppo "is not bound\n", dringp); 58711ae08745Sheppo mutex_exit(&dringp->lock); 58721ae08745Sheppo return (EINVAL); 58731ae08745Sheppo } 58741ae08745Sheppo 58751ae08745Sheppo ldcp = dringp->ldcp; 58761ae08745Sheppo 58771ae08745Sheppo if (cookie == NULL) { 58781ae08745Sheppo DWARN(ldcp->id, 58791ae08745Sheppo "ldc_mem_dring_nextcookie:(0x%llx) invalid cookie arg\n", 58801ae08745Sheppo ldcp->id); 58811ae08745Sheppo mutex_exit(&dringp->lock); 58821ae08745Sheppo return (EINVAL); 58831ae08745Sheppo } 58841ae08745Sheppo 58851ae08745Sheppo rv = ldc_mem_nextcookie((ldc_mem_handle_t)dringp->mhdl, cookie); 58861ae08745Sheppo mutex_exit(&dringp->lock); 58871ae08745Sheppo 58881ae08745Sheppo return (rv); 58891ae08745Sheppo } 58901ae08745Sheppo /* 58911ae08745Sheppo * Unbind a previously bound dring from a channel. 58921ae08745Sheppo */ 58931ae08745Sheppo int 58941ae08745Sheppo ldc_mem_dring_unbind(ldc_dring_handle_t dhandle) 58951ae08745Sheppo { 58961ae08745Sheppo ldc_dring_t *dringp; 58971ae08745Sheppo ldc_dring_t *tmp_dringp; 58981ae08745Sheppo ldc_chan_t *ldcp; 58991ae08745Sheppo 59001ae08745Sheppo if (dhandle == NULL) { 59011ae08745Sheppo DWARN(DBG_ALL_LDCS, 59021ae08745Sheppo "ldc_mem_dring_unbind: invalid desc ring handle\n"); 59031ae08745Sheppo return (EINVAL); 59041ae08745Sheppo } 59051ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 59061ae08745Sheppo 59071ae08745Sheppo mutex_enter(&dringp->lock); 59081ae08745Sheppo 59091ae08745Sheppo if (dringp->status == LDC_UNBOUND) { 59101ae08745Sheppo DWARN(DBG_ALL_LDCS, 59111ae08745Sheppo "ldc_mem_dring_bind: descriptor ring 0x%llx is unbound\n", 59121ae08745Sheppo dringp); 59131ae08745Sheppo mutex_exit(&dringp->lock); 59141ae08745Sheppo return (EINVAL); 59151ae08745Sheppo } 59161ae08745Sheppo ldcp = dringp->ldcp; 59171ae08745Sheppo 59181ae08745Sheppo mutex_enter(&ldcp->exp_dlist_lock); 59191ae08745Sheppo 59201ae08745Sheppo tmp_dringp = ldcp->exp_dring_list; 59211ae08745Sheppo if (tmp_dringp == dringp) { 59221ae08745Sheppo ldcp->exp_dring_list = dringp->ch_next; 59231ae08745Sheppo dringp->ch_next = NULL; 59241ae08745Sheppo 59251ae08745Sheppo } else { 59261ae08745Sheppo while (tmp_dringp != NULL) { 59271ae08745Sheppo if (tmp_dringp->ch_next == dringp) { 59281ae08745Sheppo tmp_dringp->ch_next = dringp->ch_next; 59291ae08745Sheppo dringp->ch_next = NULL; 59301ae08745Sheppo break; 59311ae08745Sheppo } 59321ae08745Sheppo tmp_dringp = tmp_dringp->ch_next; 59331ae08745Sheppo } 59341ae08745Sheppo if (tmp_dringp == NULL) { 59351ae08745Sheppo DWARN(DBG_ALL_LDCS, 59361ae08745Sheppo "ldc_mem_dring_unbind: invalid descriptor\n"); 59371ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 59381ae08745Sheppo mutex_exit(&dringp->lock); 59391ae08745Sheppo return (EINVAL); 59401ae08745Sheppo } 59411ae08745Sheppo } 59421ae08745Sheppo 59431ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 59441ae08745Sheppo 59451ae08745Sheppo (void) ldc_mem_unbind_handle((ldc_mem_handle_t)dringp->mhdl); 59461ae08745Sheppo (void) ldc_mem_free_handle((ldc_mem_handle_t)dringp->mhdl); 59471ae08745Sheppo 59481ae08745Sheppo dringp->ldcp = NULL; 59491ae08745Sheppo dringp->mhdl = NULL; 59501ae08745Sheppo dringp->status = LDC_UNBOUND; 59511ae08745Sheppo 59521ae08745Sheppo mutex_exit(&dringp->lock); 59531ae08745Sheppo 59541ae08745Sheppo return (0); 59551ae08745Sheppo } 59561ae08745Sheppo 59571ae08745Sheppo /* 59581ae08745Sheppo * Get information about the dring. The base address of the descriptor 59591ae08745Sheppo * ring along with the type and permission are returned back. 59601ae08745Sheppo */ 59611ae08745Sheppo int 59621ae08745Sheppo ldc_mem_dring_info(ldc_dring_handle_t dhandle, ldc_mem_info_t *minfo) 59631ae08745Sheppo { 59641ae08745Sheppo ldc_dring_t *dringp; 59651ae08745Sheppo int rv; 59661ae08745Sheppo 59671ae08745Sheppo if (dhandle == NULL) { 59681ae08745Sheppo DWARN(DBG_ALL_LDCS, 59691ae08745Sheppo "ldc_mem_dring_info: invalid desc ring handle\n"); 59701ae08745Sheppo return (EINVAL); 59711ae08745Sheppo } 59721ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 59731ae08745Sheppo 59741ae08745Sheppo mutex_enter(&dringp->lock); 59751ae08745Sheppo 59761ae08745Sheppo if (dringp->mhdl) { 59771ae08745Sheppo rv = ldc_mem_info(dringp->mhdl, minfo); 59781ae08745Sheppo if (rv) { 59791ae08745Sheppo DWARN(DBG_ALL_LDCS, 59801ae08745Sheppo "ldc_mem_dring_info: error reading mem info\n"); 59811ae08745Sheppo mutex_exit(&dringp->lock); 59821ae08745Sheppo return (rv); 59831ae08745Sheppo } 59841ae08745Sheppo } else { 59851ae08745Sheppo minfo->vaddr = dringp->base; 59861ae08745Sheppo minfo->raddr = NULL; 59871ae08745Sheppo minfo->status = dringp->status; 59881ae08745Sheppo } 59891ae08745Sheppo 59901ae08745Sheppo mutex_exit(&dringp->lock); 59911ae08745Sheppo 59921ae08745Sheppo return (0); 59931ae08745Sheppo } 59941ae08745Sheppo 59951ae08745Sheppo /* 59961ae08745Sheppo * Map an exported descriptor ring into the local address space. If the 59971ae08745Sheppo * descriptor ring was exported for direct map access, a HV call is made 59981ae08745Sheppo * to allocate a RA range. If the map is done via a shadow copy, local 59991ae08745Sheppo * shadow memory is allocated. 60001ae08745Sheppo */ 60011ae08745Sheppo int 60021ae08745Sheppo ldc_mem_dring_map(ldc_handle_t handle, ldc_mem_cookie_t *cookie, 60031ae08745Sheppo uint32_t ccount, uint32_t len, uint32_t dsize, uint8_t mtype, 60041ae08745Sheppo ldc_dring_handle_t *dhandle) 60051ae08745Sheppo { 60061ae08745Sheppo int err; 60071ae08745Sheppo ldc_chan_t *ldcp = (ldc_chan_t *)handle; 60081ae08745Sheppo ldc_mem_handle_t mhandle; 60091ae08745Sheppo ldc_dring_t *dringp; 60101ae08745Sheppo size_t dring_size; 60111ae08745Sheppo 60121ae08745Sheppo if (dhandle == NULL) { 60131ae08745Sheppo DWARN(DBG_ALL_LDCS, 60141ae08745Sheppo "ldc_mem_dring_map: invalid dhandle\n"); 60151ae08745Sheppo return (EINVAL); 60161ae08745Sheppo } 60171ae08745Sheppo 60181ae08745Sheppo /* check to see if channel is initalized */ 60191ae08745Sheppo if (handle == NULL) { 60201ae08745Sheppo DWARN(DBG_ALL_LDCS, 60211ae08745Sheppo "ldc_mem_dring_map: invalid channel handle\n"); 60221ae08745Sheppo return (EINVAL); 60231ae08745Sheppo } 60241ae08745Sheppo ldcp = (ldc_chan_t *)handle; 60251ae08745Sheppo 60261ae08745Sheppo if (cookie == NULL) { 60271ae08745Sheppo DWARN(ldcp->id, 60281ae08745Sheppo "ldc_mem_dring_map: (0x%llx) invalid cookie\n", 60291ae08745Sheppo ldcp->id); 60301ae08745Sheppo return (EINVAL); 60311ae08745Sheppo } 60321ae08745Sheppo 60331ae08745Sheppo /* FUTURE: For now we support only one cookie per dring */ 60341ae08745Sheppo ASSERT(ccount == 1); 60351ae08745Sheppo 60361ae08745Sheppo if (cookie->size < (dsize * len)) { 60371ae08745Sheppo DWARN(ldcp->id, 60381ae08745Sheppo "ldc_mem_dring_map: (0x%llx) invalid dsize/len\n", 60391ae08745Sheppo ldcp->id); 60401ae08745Sheppo return (EINVAL); 60411ae08745Sheppo } 60421ae08745Sheppo 60431ae08745Sheppo *dhandle = 0; 60441ae08745Sheppo 60451ae08745Sheppo /* Allocate an dring structure */ 60461ae08745Sheppo dringp = kmem_zalloc(sizeof (ldc_dring_t), KM_SLEEP); 60471ae08745Sheppo 60481ae08745Sheppo D1(ldcp->id, 60491ae08745Sheppo "ldc_mem_dring_map: 0x%x,0x%x,0x%x,0x%llx,0x%llx\n", 60501ae08745Sheppo mtype, len, dsize, cookie->addr, cookie->size); 60511ae08745Sheppo 60521ae08745Sheppo /* Initialize dring */ 60531ae08745Sheppo dringp->length = len; 60541ae08745Sheppo dringp->dsize = dsize; 60551ae08745Sheppo 60561ae08745Sheppo /* round of to multiple of page size */ 60571ae08745Sheppo dring_size = len * dsize; 60581ae08745Sheppo dringp->size = (dring_size & MMU_PAGEMASK); 60591ae08745Sheppo if (dring_size & MMU_PAGEOFFSET) 60601ae08745Sheppo dringp->size += MMU_PAGESIZE; 60611ae08745Sheppo 60621ae08745Sheppo dringp->ldcp = ldcp; 60631ae08745Sheppo 60641ae08745Sheppo /* create an memory handle */ 60651ae08745Sheppo err = ldc_mem_alloc_handle(handle, &mhandle); 60661ae08745Sheppo if (err || mhandle == NULL) { 60671ae08745Sheppo DWARN(DBG_ALL_LDCS, 60681ae08745Sheppo "ldc_mem_dring_map: cannot alloc hdl err=%d\n", 60691ae08745Sheppo err); 60701ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 60711ae08745Sheppo return (ENOMEM); 60721ae08745Sheppo } 60731ae08745Sheppo 60741ae08745Sheppo dringp->mhdl = mhandle; 60751ae08745Sheppo dringp->base = NULL; 60761ae08745Sheppo 60771ae08745Sheppo /* map the dring into local memory */ 60784bac2208Snarayan err = ldc_mem_map(mhandle, cookie, ccount, mtype, LDC_MEM_RW, 60791ae08745Sheppo &(dringp->base), NULL); 60801ae08745Sheppo if (err || dringp->base == NULL) { 60811ae08745Sheppo cmn_err(CE_WARN, 60821ae08745Sheppo "ldc_mem_dring_map: cannot map desc ring err=%d\n", err); 60831ae08745Sheppo (void) ldc_mem_free_handle(mhandle); 60841ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 60851ae08745Sheppo return (ENOMEM); 60861ae08745Sheppo } 60871ae08745Sheppo 60881ae08745Sheppo /* initialize the desc ring lock */ 60891ae08745Sheppo mutex_init(&dringp->lock, NULL, MUTEX_DRIVER, NULL); 60901ae08745Sheppo 60911ae08745Sheppo /* Add descriptor ring to channel's imported dring list */ 60921ae08745Sheppo mutex_enter(&ldcp->imp_dlist_lock); 60931ae08745Sheppo dringp->ch_next = ldcp->imp_dring_list; 60941ae08745Sheppo ldcp->imp_dring_list = dringp; 60951ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 60961ae08745Sheppo 60971ae08745Sheppo dringp->status = LDC_MAPPED; 60981ae08745Sheppo 60991ae08745Sheppo *dhandle = (ldc_dring_handle_t)dringp; 61001ae08745Sheppo 61011ae08745Sheppo return (0); 61021ae08745Sheppo } 61031ae08745Sheppo 61041ae08745Sheppo /* 61051ae08745Sheppo * Unmap a descriptor ring. Free shadow memory (if any). 61061ae08745Sheppo */ 61071ae08745Sheppo int 61081ae08745Sheppo ldc_mem_dring_unmap(ldc_dring_handle_t dhandle) 61091ae08745Sheppo { 61101ae08745Sheppo ldc_dring_t *dringp; 61111ae08745Sheppo ldc_dring_t *tmp_dringp; 61121ae08745Sheppo ldc_chan_t *ldcp; 61131ae08745Sheppo 61141ae08745Sheppo if (dhandle == NULL) { 61151ae08745Sheppo DWARN(DBG_ALL_LDCS, 61161ae08745Sheppo "ldc_mem_dring_unmap: invalid desc ring handle\n"); 61171ae08745Sheppo return (EINVAL); 61181ae08745Sheppo } 61191ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 61201ae08745Sheppo 61211ae08745Sheppo if (dringp->status != LDC_MAPPED) { 61221ae08745Sheppo DWARN(DBG_ALL_LDCS, 61231ae08745Sheppo "ldc_mem_dring_unmap: not a mapped desc ring\n"); 61241ae08745Sheppo return (EINVAL); 61251ae08745Sheppo } 61261ae08745Sheppo 61271ae08745Sheppo mutex_enter(&dringp->lock); 61281ae08745Sheppo 61291ae08745Sheppo ldcp = dringp->ldcp; 61301ae08745Sheppo 61311ae08745Sheppo mutex_enter(&ldcp->imp_dlist_lock); 61321ae08745Sheppo 61331ae08745Sheppo /* find and unlink the desc ring from channel import list */ 61341ae08745Sheppo tmp_dringp = ldcp->imp_dring_list; 61351ae08745Sheppo if (tmp_dringp == dringp) { 61361ae08745Sheppo ldcp->imp_dring_list = dringp->ch_next; 61371ae08745Sheppo dringp->ch_next = NULL; 61381ae08745Sheppo 61391ae08745Sheppo } else { 61401ae08745Sheppo while (tmp_dringp != NULL) { 61411ae08745Sheppo if (tmp_dringp->ch_next == dringp) { 61421ae08745Sheppo tmp_dringp->ch_next = dringp->ch_next; 61431ae08745Sheppo dringp->ch_next = NULL; 61441ae08745Sheppo break; 61451ae08745Sheppo } 61461ae08745Sheppo tmp_dringp = tmp_dringp->ch_next; 61471ae08745Sheppo } 61481ae08745Sheppo if (tmp_dringp == NULL) { 61491ae08745Sheppo DWARN(DBG_ALL_LDCS, 61501ae08745Sheppo "ldc_mem_dring_unmap: invalid descriptor\n"); 61511ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 61521ae08745Sheppo mutex_exit(&dringp->lock); 61531ae08745Sheppo return (EINVAL); 61541ae08745Sheppo } 61551ae08745Sheppo } 61561ae08745Sheppo 61571ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 61581ae08745Sheppo 61591ae08745Sheppo /* do a LDC memory handle unmap and free */ 61601ae08745Sheppo (void) ldc_mem_unmap(dringp->mhdl); 61611ae08745Sheppo (void) ldc_mem_free_handle((ldc_mem_handle_t)dringp->mhdl); 61621ae08745Sheppo 61631ae08745Sheppo dringp->status = 0; 61641ae08745Sheppo dringp->ldcp = NULL; 61651ae08745Sheppo 61661ae08745Sheppo mutex_exit(&dringp->lock); 61671ae08745Sheppo 61681ae08745Sheppo /* destroy dring lock */ 61691ae08745Sheppo mutex_destroy(&dringp->lock); 61701ae08745Sheppo 61711ae08745Sheppo /* free desc ring object */ 61721ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 61731ae08745Sheppo 61741ae08745Sheppo return (0); 61751ae08745Sheppo } 61761ae08745Sheppo 61771ae08745Sheppo /* 61781ae08745Sheppo * Internal entry point for descriptor ring access entry consistency 61791ae08745Sheppo * semantics. Acquire copies the contents of the remote descriptor ring 61801ae08745Sheppo * into the local shadow copy. The release operation copies the local 61811ae08745Sheppo * contents into the remote dring. The start and end locations specify 61821ae08745Sheppo * bounds for the entries being synchronized. 61831ae08745Sheppo */ 61841ae08745Sheppo static int 61851ae08745Sheppo i_ldc_dring_acquire_release(ldc_dring_handle_t dhandle, 61861ae08745Sheppo uint8_t direction, uint64_t start, uint64_t end) 61871ae08745Sheppo { 61881ae08745Sheppo int err; 61891ae08745Sheppo ldc_dring_t *dringp; 61901ae08745Sheppo ldc_chan_t *ldcp; 61911ae08745Sheppo uint64_t soff; 61921ae08745Sheppo size_t copy_size; 61931ae08745Sheppo 61941ae08745Sheppo if (dhandle == NULL) { 61951ae08745Sheppo DWARN(DBG_ALL_LDCS, 61961ae08745Sheppo "i_ldc_dring_acquire_release: invalid desc ring handle\n"); 61971ae08745Sheppo return (EINVAL); 61981ae08745Sheppo } 61991ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 62001ae08745Sheppo mutex_enter(&dringp->lock); 62011ae08745Sheppo 62021ae08745Sheppo if (dringp->status != LDC_MAPPED || dringp->ldcp == NULL) { 62031ae08745Sheppo DWARN(DBG_ALL_LDCS, 62041ae08745Sheppo "i_ldc_dring_acquire_release: not a mapped desc ring\n"); 62051ae08745Sheppo mutex_exit(&dringp->lock); 62061ae08745Sheppo return (EINVAL); 62071ae08745Sheppo } 62081ae08745Sheppo 62091ae08745Sheppo if (start >= dringp->length || end >= dringp->length) { 62101ae08745Sheppo DWARN(DBG_ALL_LDCS, 62111ae08745Sheppo "i_ldc_dring_acquire_release: index out of range\n"); 62121ae08745Sheppo mutex_exit(&dringp->lock); 62131ae08745Sheppo return (EINVAL); 62141ae08745Sheppo } 62151ae08745Sheppo 62161ae08745Sheppo /* get the channel handle */ 62171ae08745Sheppo ldcp = dringp->ldcp; 62181ae08745Sheppo 62191ae08745Sheppo copy_size = (start <= end) ? (((end - start) + 1) * dringp->dsize) : 62201ae08745Sheppo ((dringp->length - start) * dringp->dsize); 62211ae08745Sheppo 62221ae08745Sheppo /* Calculate the relative offset for the first desc */ 62231ae08745Sheppo soff = (start * dringp->dsize); 62241ae08745Sheppo 62251ae08745Sheppo /* copy to/from remote from/to local memory */ 62261ae08745Sheppo D1(ldcp->id, "i_ldc_dring_acquire_release: c1 off=0x%llx sz=0x%llx\n", 62271ae08745Sheppo soff, copy_size); 62281ae08745Sheppo err = i_ldc_mem_acquire_release((ldc_mem_handle_t)dringp->mhdl, 62291ae08745Sheppo direction, soff, copy_size); 62301ae08745Sheppo if (err) { 62311ae08745Sheppo DWARN(ldcp->id, 62321ae08745Sheppo "i_ldc_dring_acquire_release: copy failed\n"); 62331ae08745Sheppo mutex_exit(&dringp->lock); 62341ae08745Sheppo return (err); 62351ae08745Sheppo } 62361ae08745Sheppo 62371ae08745Sheppo /* do the balance */ 62381ae08745Sheppo if (start > end) { 62391ae08745Sheppo copy_size = ((end + 1) * dringp->dsize); 62401ae08745Sheppo soff = 0; 62411ae08745Sheppo 62421ae08745Sheppo /* copy to/from remote from/to local memory */ 62431ae08745Sheppo D1(ldcp->id, "i_ldc_dring_acquire_release: c2 " 62441ae08745Sheppo "off=0x%llx sz=0x%llx\n", soff, copy_size); 62451ae08745Sheppo err = i_ldc_mem_acquire_release((ldc_mem_handle_t)dringp->mhdl, 62461ae08745Sheppo direction, soff, copy_size); 62471ae08745Sheppo if (err) { 62481ae08745Sheppo DWARN(ldcp->id, 62491ae08745Sheppo "i_ldc_dring_acquire_release: copy failed\n"); 62501ae08745Sheppo mutex_exit(&dringp->lock); 62511ae08745Sheppo return (err); 62521ae08745Sheppo } 62531ae08745Sheppo } 62541ae08745Sheppo 62551ae08745Sheppo mutex_exit(&dringp->lock); 62561ae08745Sheppo 62571ae08745Sheppo return (0); 62581ae08745Sheppo } 62591ae08745Sheppo 62601ae08745Sheppo /* 62611ae08745Sheppo * Ensure that the contents in the local dring are consistent 62621ae08745Sheppo * with the contents if of remote dring 62631ae08745Sheppo */ 62641ae08745Sheppo int 62651ae08745Sheppo ldc_mem_dring_acquire(ldc_dring_handle_t dhandle, uint64_t start, uint64_t end) 62661ae08745Sheppo { 62671ae08745Sheppo return (i_ldc_dring_acquire_release(dhandle, LDC_COPY_IN, start, end)); 62681ae08745Sheppo } 62691ae08745Sheppo 62701ae08745Sheppo /* 62711ae08745Sheppo * Ensure that the contents in the remote dring are consistent 62721ae08745Sheppo * with the contents if of local dring 62731ae08745Sheppo */ 62741ae08745Sheppo int 62751ae08745Sheppo ldc_mem_dring_release(ldc_dring_handle_t dhandle, uint64_t start, uint64_t end) 62761ae08745Sheppo { 62771ae08745Sheppo return (i_ldc_dring_acquire_release(dhandle, LDC_COPY_OUT, start, end)); 62781ae08745Sheppo } 62791ae08745Sheppo 62801ae08745Sheppo 62811ae08745Sheppo /* ------------------------------------------------------------------------- */ 6282