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> 55*4bac2208Snarayan #include <sys/mmu.h> 56*4bac2208Snarayan #include <sys/pte.h> 57*4bac2208Snarayan #include <vm/hat.h> 58*4bac2208Snarayan #include <vm/as.h> 59*4bac2208Snarayan #include <vm/hat_sfmmu.h> 60*4bac2208Snarayan #include <sys/vm_machparam.h> 61*4bac2208Snarayan #include <vm/seg_kmem.h> 62*4bac2208Snarayan #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); 741ae08745Sheppo static int i_ldc_rxq_reconf(ldc_chan_t *ldcp); 751ae08745Sheppo static void i_ldc_reset_state(ldc_chan_t *ldcp); 761ae08745Sheppo static void i_ldc_reset(ldc_chan_t *ldcp); 771ae08745Sheppo 781ae08745Sheppo static int i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail); 791ae08745Sheppo static int i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail); 801ae08745Sheppo static int i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head); 811ae08745Sheppo static int i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype, 821ae08745Sheppo uint8_t ctrlmsg); 831ae08745Sheppo 841ae08745Sheppo /* Interrupt handling functions */ 851ae08745Sheppo static uint_t i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2); 861ae08745Sheppo static uint_t i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2); 871ae08745Sheppo static void i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype); 881ae08745Sheppo 891ae08745Sheppo /* Read method functions */ 901ae08745Sheppo static int i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep); 911ae08745Sheppo static int i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, 921ae08745Sheppo size_t *sizep); 931ae08745Sheppo static int i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, 941ae08745Sheppo size_t *sizep); 951ae08745Sheppo 961ae08745Sheppo /* Write method functions */ 971ae08745Sheppo static int i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t target_bufp, 981ae08745Sheppo size_t *sizep); 991ae08745Sheppo static int i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t target_bufp, 1001ae08745Sheppo size_t *sizep); 1011ae08745Sheppo static int i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t target_bufp, 1021ae08745Sheppo size_t *sizep); 1031ae08745Sheppo 1041ae08745Sheppo /* Pkt processing internal functions */ 1051ae08745Sheppo static int i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg); 1061ae08745Sheppo static int i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *ldcmsg); 1071ae08745Sheppo static int i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg); 1081ae08745Sheppo static int i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg); 1091ae08745Sheppo static int i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg); 1101ae08745Sheppo static int i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg); 1111ae08745Sheppo static int i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg); 1121ae08745Sheppo 1131ae08745Sheppo /* Memory synchronization internal functions */ 1141ae08745Sheppo static int i_ldc_mem_acquire_release(ldc_mem_handle_t mhandle, 1151ae08745Sheppo uint8_t direction, uint64_t offset, size_t size); 1161ae08745Sheppo static int i_ldc_dring_acquire_release(ldc_dring_handle_t dhandle, 1171ae08745Sheppo uint8_t direction, uint64_t start, uint64_t end); 1181ae08745Sheppo 1191ae08745Sheppo /* LDC Version */ 1201ae08745Sheppo static ldc_ver_t ldc_versions[] = { {1, 0} }; 1211ae08745Sheppo 1221ae08745Sheppo /* number of supported versions */ 1231ae08745Sheppo #define LDC_NUM_VERS (sizeof (ldc_versions) / sizeof (ldc_versions[0])) 1241ae08745Sheppo 1251ae08745Sheppo /* Module State Pointer */ 1261ae08745Sheppo static ldc_soft_state_t *ldcssp; 1271ae08745Sheppo 1281ae08745Sheppo static struct modldrv md = { 1291ae08745Sheppo &mod_miscops, /* This is a misc module */ 1301ae08745Sheppo "sun4v LDC module v%I%", /* Name of the module */ 1311ae08745Sheppo }; 1321ae08745Sheppo 1331ae08745Sheppo static struct modlinkage ml = { 1341ae08745Sheppo MODREV_1, 1351ae08745Sheppo &md, 1361ae08745Sheppo NULL 1371ae08745Sheppo }; 1381ae08745Sheppo 1391ae08745Sheppo static uint64_t ldc_sup_minor; /* Supported minor number */ 1401ae08745Sheppo static hsvc_info_t ldc_hsvc = { 1411ae08745Sheppo HSVC_REV_1, NULL, HSVC_GROUP_LDC, 1, 0, "ldc" 1421ae08745Sheppo }; 1431ae08745Sheppo 1441ae08745Sheppo static uint64_t intr_sup_minor; /* Supported minor number */ 1451ae08745Sheppo static hsvc_info_t intr_hsvc = { 1461ae08745Sheppo HSVC_REV_1, NULL, HSVC_GROUP_INTR, 1, 0, "ldc" 1471ae08745Sheppo }; 1481ae08745Sheppo 149*4bac2208Snarayan /* 150*4bac2208Snarayan * LDC framework supports mapping remote domain's memory 151*4bac2208Snarayan * either directly or via shadow memory pages. Default 152*4bac2208Snarayan * support is currently implemented via shadow copy. 153*4bac2208Snarayan * Direct map can be enabled by setting 'ldc_shmem_enabled' 154*4bac2208Snarayan */ 155*4bac2208Snarayan int ldc_shmem_enabled = 0; 156e1ebb9ecSlm66018 1570a55fbb7Slm66018 /* 158e1ebb9ecSlm66018 * The no. of MTU size messages that can be stored in 159e1ebb9ecSlm66018 * the LDC Tx queue. The number of Tx queue entries is 160e1ebb9ecSlm66018 * then computed as (mtu * mtu_msgs)/sizeof(queue_entry) 161e1ebb9ecSlm66018 */ 162e1ebb9ecSlm66018 uint64_t ldc_mtu_msgs = LDC_MTU_MSGS; 163e1ebb9ecSlm66018 164e1ebb9ecSlm66018 /* 165e1ebb9ecSlm66018 * The minimum queue length. This is the size of the smallest 166e1ebb9ecSlm66018 * LDC queue. If the computed value is less than this default, 167e1ebb9ecSlm66018 * the queue length is rounded up to 'ldc_queue_entries'. 168e1ebb9ecSlm66018 */ 169e1ebb9ecSlm66018 uint64_t ldc_queue_entries = LDC_QUEUE_ENTRIES; 170e1ebb9ecSlm66018 171e1ebb9ecSlm66018 /* 172e1ebb9ecSlm66018 * Pages exported for remote access over each channel is 173e1ebb9ecSlm66018 * maintained in a table registered with the Hypervisor. 174e1ebb9ecSlm66018 * The default number of entries in the table is set to 175e1ebb9ecSlm66018 * 'ldc_mtbl_entries'. 176e1ebb9ecSlm66018 */ 177e1ebb9ecSlm66018 uint64_t ldc_maptable_entries = LDC_MTBL_ENTRIES; 178e1ebb9ecSlm66018 179e1ebb9ecSlm66018 /* 180e1ebb9ecSlm66018 * LDC retry count and delay - when the HV returns EWOULDBLOCK 181e1ebb9ecSlm66018 * the operation is retried 'ldc_max_retries' times with a 182e1ebb9ecSlm66018 * wait of 'ldc_delay' usecs between each retry. 1830a55fbb7Slm66018 */ 1840a55fbb7Slm66018 int ldc_max_retries = LDC_MAX_RETRIES; 1850a55fbb7Slm66018 clock_t ldc_delay = LDC_DELAY; 1860a55fbb7Slm66018 1871ae08745Sheppo #ifdef DEBUG 1881ae08745Sheppo 1891ae08745Sheppo /* 1901ae08745Sheppo * Print debug messages 1911ae08745Sheppo * 1921ae08745Sheppo * set ldcdbg to 0x7 for enabling all msgs 1931ae08745Sheppo * 0x4 - Warnings 1941ae08745Sheppo * 0x2 - All debug messages 1951ae08745Sheppo * 0x1 - Minimal debug messages 1961ae08745Sheppo * 1971ae08745Sheppo * set ldcdbgchan to the channel number you want to debug 1981ae08745Sheppo * setting it to -1 prints debug messages for all channels 1991ae08745Sheppo * NOTE: ldcdbgchan has no effect on error messages 2001ae08745Sheppo */ 2011ae08745Sheppo 2021ae08745Sheppo #define DBG_ALL_LDCS -1 2031ae08745Sheppo 2041ae08745Sheppo int ldcdbg = 0x0; 2051ae08745Sheppo int64_t ldcdbgchan = DBG_ALL_LDCS; 2061ae08745Sheppo 2071ae08745Sheppo static void 2081ae08745Sheppo ldcdebug(int64_t id, const char *fmt, ...) 2091ae08745Sheppo { 2101ae08745Sheppo char buf[512]; 2111ae08745Sheppo va_list ap; 2121ae08745Sheppo 2131ae08745Sheppo /* 2141ae08745Sheppo * Do not return if, 2151ae08745Sheppo * caller wants to print it anyway - (id == DBG_ALL_LDCS) 2161ae08745Sheppo * debug channel is set to all LDCs - (ldcdbgchan == DBG_ALL_LDCS) 2171ae08745Sheppo * debug channel = caller specified channel 2181ae08745Sheppo */ 2191ae08745Sheppo if ((id != DBG_ALL_LDCS) && 2201ae08745Sheppo (ldcdbgchan != DBG_ALL_LDCS) && 2211ae08745Sheppo (ldcdbgchan != id)) { 2221ae08745Sheppo return; 2231ae08745Sheppo } 2241ae08745Sheppo 2251ae08745Sheppo va_start(ap, fmt); 2261ae08745Sheppo (void) vsprintf(buf, fmt, ap); 2271ae08745Sheppo va_end(ap); 2281ae08745Sheppo 2291ae08745Sheppo cmn_err(CE_CONT, "?%s\n", buf); 2301ae08745Sheppo } 2311ae08745Sheppo 2321ae08745Sheppo #define D1 \ 2331ae08745Sheppo if (ldcdbg & 0x01) \ 2341ae08745Sheppo ldcdebug 2351ae08745Sheppo 2361ae08745Sheppo #define D2 \ 2371ae08745Sheppo if (ldcdbg & 0x02) \ 2381ae08745Sheppo ldcdebug 2391ae08745Sheppo 2401ae08745Sheppo #define DWARN \ 2411ae08745Sheppo if (ldcdbg & 0x04) \ 2421ae08745Sheppo ldcdebug 2431ae08745Sheppo 2441ae08745Sheppo #define DUMP_PAYLOAD(id, addr) \ 2451ae08745Sheppo { \ 2461ae08745Sheppo char buf[65*3]; \ 2471ae08745Sheppo int i; \ 2481ae08745Sheppo uint8_t *src = (uint8_t *)addr; \ 2491ae08745Sheppo for (i = 0; i < 64; i++, src++) \ 2501ae08745Sheppo (void) sprintf(&buf[i * 3], "|%02x", *src); \ 2511ae08745Sheppo (void) sprintf(&buf[i * 3], "|\n"); \ 2521ae08745Sheppo D2((id), "payload: %s", buf); \ 2531ae08745Sheppo } 2541ae08745Sheppo 2551ae08745Sheppo #define DUMP_LDC_PKT(c, s, addr) \ 2561ae08745Sheppo { \ 2571ae08745Sheppo ldc_msg_t *msg = (ldc_msg_t *)(addr); \ 2581ae08745Sheppo uint32_t mid = ((c)->mode != LDC_MODE_RAW) ? msg->seqid : 0; \ 2591ae08745Sheppo if (msg->type == LDC_DATA) { \ 2601ae08745Sheppo D2((c)->id, "%s: msg%d (/%x/%x/%x/,env[%c%c,sz=%d])", \ 2611ae08745Sheppo (s), mid, msg->type, msg->stype, msg->ctrl, \ 2621ae08745Sheppo (msg->env & LDC_FRAG_START) ? 'B' : ' ', \ 2631ae08745Sheppo (msg->env & LDC_FRAG_STOP) ? 'E' : ' ', \ 2641ae08745Sheppo (msg->env & LDC_LEN_MASK)); \ 2651ae08745Sheppo } else { \ 2661ae08745Sheppo D2((c)->id, "%s: msg%d (/%x/%x/%x/,env=%x)", (s), \ 2671ae08745Sheppo mid, msg->type, msg->stype, msg->ctrl, msg->env); \ 2681ae08745Sheppo } \ 2691ae08745Sheppo } 2701ae08745Sheppo 2711ae08745Sheppo #else 2721ae08745Sheppo 2731ae08745Sheppo #define DBG_ALL_LDCS -1 2741ae08745Sheppo 2751ae08745Sheppo #define D1 2761ae08745Sheppo #define D2 2771ae08745Sheppo #define DWARN 2781ae08745Sheppo 2791ae08745Sheppo #define DUMP_PAYLOAD(id, addr) 2801ae08745Sheppo #define DUMP_LDC_PKT(c, s, addr) 2811ae08745Sheppo 2821ae08745Sheppo #endif 2831ae08745Sheppo 2841ae08745Sheppo #define ZERO_PKT(p) \ 2851ae08745Sheppo bzero((p), sizeof (ldc_msg_t)); 2861ae08745Sheppo 2871ae08745Sheppo #define IDX2COOKIE(idx, pg_szc, pg_shift) \ 2881ae08745Sheppo (((pg_szc) << LDC_COOKIE_PGSZC_SHIFT) | ((idx) << (pg_shift))) 2891ae08745Sheppo 2901ae08745Sheppo 2911ae08745Sheppo int 2921ae08745Sheppo _init(void) 2931ae08745Sheppo { 2941ae08745Sheppo int status; 2951ae08745Sheppo 2961ae08745Sheppo status = hsvc_register(&ldc_hsvc, &ldc_sup_minor); 2971ae08745Sheppo if (status != 0) { 2981ae08745Sheppo cmn_err(CE_WARN, "%s: cannot negotiate hypervisor LDC services" 2991ae08745Sheppo " group: 0x%lx major: %ld minor: %ld errno: %d", 3001ae08745Sheppo ldc_hsvc.hsvc_modname, ldc_hsvc.hsvc_group, 3011ae08745Sheppo ldc_hsvc.hsvc_major, ldc_hsvc.hsvc_minor, status); 3021ae08745Sheppo return (-1); 3031ae08745Sheppo } 3041ae08745Sheppo 3051ae08745Sheppo status = hsvc_register(&intr_hsvc, &intr_sup_minor); 3061ae08745Sheppo if (status != 0) { 3071ae08745Sheppo cmn_err(CE_WARN, "%s: cannot negotiate hypervisor interrupt " 3081ae08745Sheppo "services group: 0x%lx major: %ld minor: %ld errno: %d", 3091ae08745Sheppo intr_hsvc.hsvc_modname, intr_hsvc.hsvc_group, 3101ae08745Sheppo intr_hsvc.hsvc_major, intr_hsvc.hsvc_minor, status); 3111ae08745Sheppo (void) hsvc_unregister(&ldc_hsvc); 3121ae08745Sheppo return (-1); 3131ae08745Sheppo } 3141ae08745Sheppo 3151ae08745Sheppo /* allocate soft state structure */ 3161ae08745Sheppo ldcssp = kmem_zalloc(sizeof (ldc_soft_state_t), KM_SLEEP); 3171ae08745Sheppo 3181ae08745Sheppo /* Link the module into the system */ 3191ae08745Sheppo status = mod_install(&ml); 3201ae08745Sheppo if (status != 0) { 3211ae08745Sheppo kmem_free(ldcssp, sizeof (ldc_soft_state_t)); 3221ae08745Sheppo return (status); 3231ae08745Sheppo } 3241ae08745Sheppo 3251ae08745Sheppo /* Initialize the LDC state structure */ 3261ae08745Sheppo mutex_init(&ldcssp->lock, NULL, MUTEX_DRIVER, NULL); 3271ae08745Sheppo 3281ae08745Sheppo mutex_enter(&ldcssp->lock); 3291ae08745Sheppo 330*4bac2208Snarayan /* Create a cache for memory handles */ 331*4bac2208Snarayan ldcssp->memhdl_cache = kmem_cache_create("ldc_memhdl_cache", 332*4bac2208Snarayan sizeof (ldc_mhdl_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 333*4bac2208Snarayan if (ldcssp->memhdl_cache == NULL) { 334*4bac2208Snarayan DWARN(DBG_ALL_LDCS, "_init: ldc_memhdl cache create failed\n"); 335*4bac2208Snarayan mutex_exit(&ldcssp->lock); 336*4bac2208Snarayan return (-1); 337*4bac2208Snarayan } 338*4bac2208Snarayan 339*4bac2208Snarayan /* Create cache for memory segment structures */ 340*4bac2208Snarayan ldcssp->memseg_cache = kmem_cache_create("ldc_memseg_cache", 341*4bac2208Snarayan sizeof (ldc_memseg_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 342*4bac2208Snarayan if (ldcssp->memseg_cache == NULL) { 343*4bac2208Snarayan DWARN(DBG_ALL_LDCS, "_init: ldc_memseg cache create failed\n"); 344*4bac2208Snarayan mutex_exit(&ldcssp->lock); 345*4bac2208Snarayan return (-1); 346*4bac2208Snarayan } 347*4bac2208Snarayan 348*4bac2208Snarayan 3491ae08745Sheppo ldcssp->channel_count = 0; 3501ae08745Sheppo ldcssp->channels_open = 0; 3511ae08745Sheppo ldcssp->chan_list = NULL; 3521ae08745Sheppo ldcssp->dring_list = NULL; 3531ae08745Sheppo 3541ae08745Sheppo mutex_exit(&ldcssp->lock); 3551ae08745Sheppo 3561ae08745Sheppo return (0); 3571ae08745Sheppo } 3581ae08745Sheppo 3591ae08745Sheppo int 3601ae08745Sheppo _info(struct modinfo *modinfop) 3611ae08745Sheppo { 3621ae08745Sheppo /* Report status of the dynamically loadable driver module */ 3631ae08745Sheppo return (mod_info(&ml, modinfop)); 3641ae08745Sheppo } 3651ae08745Sheppo 3661ae08745Sheppo int 3671ae08745Sheppo _fini(void) 3681ae08745Sheppo { 3691ae08745Sheppo int rv, status; 3701ae08745Sheppo ldc_chan_t *ldcp; 3711ae08745Sheppo ldc_dring_t *dringp; 3721ae08745Sheppo ldc_mem_info_t minfo; 3731ae08745Sheppo 3741ae08745Sheppo /* Unlink the driver module from the system */ 3751ae08745Sheppo status = mod_remove(&ml); 3761ae08745Sheppo if (status) { 3771ae08745Sheppo DWARN(DBG_ALL_LDCS, "_fini: mod_remove failed\n"); 3781ae08745Sheppo return (EIO); 3791ae08745Sheppo } 3801ae08745Sheppo 3811ae08745Sheppo /* close and finalize channels */ 3821ae08745Sheppo ldcp = ldcssp->chan_list; 3831ae08745Sheppo while (ldcp != NULL) { 3841ae08745Sheppo (void) ldc_close((ldc_handle_t)ldcp); 3851ae08745Sheppo (void) ldc_fini((ldc_handle_t)ldcp); 3861ae08745Sheppo 3871ae08745Sheppo ldcp = ldcp->next; 3881ae08745Sheppo } 3891ae08745Sheppo 3901ae08745Sheppo /* Free descriptor rings */ 3911ae08745Sheppo dringp = ldcssp->dring_list; 3921ae08745Sheppo while (dringp != NULL) { 3931ae08745Sheppo dringp = dringp->next; 3941ae08745Sheppo 3951ae08745Sheppo rv = ldc_mem_dring_info((ldc_dring_handle_t)dringp, &minfo); 3961ae08745Sheppo if (rv == 0 && minfo.status != LDC_UNBOUND) { 3971ae08745Sheppo if (minfo.status == LDC_BOUND) { 3981ae08745Sheppo (void) ldc_mem_dring_unbind( 3991ae08745Sheppo (ldc_dring_handle_t)dringp); 4001ae08745Sheppo } 4011ae08745Sheppo if (minfo.status == LDC_MAPPED) { 4021ae08745Sheppo (void) ldc_mem_dring_unmap( 4031ae08745Sheppo (ldc_dring_handle_t)dringp); 4041ae08745Sheppo } 4051ae08745Sheppo } 4061ae08745Sheppo 4071ae08745Sheppo (void) ldc_mem_dring_destroy((ldc_dring_handle_t)dringp); 4081ae08745Sheppo } 4091ae08745Sheppo ldcssp->dring_list = NULL; 4101ae08745Sheppo 411*4bac2208Snarayan /* Destroy kmem caches */ 412*4bac2208Snarayan kmem_cache_destroy(ldcssp->memhdl_cache); 413*4bac2208Snarayan kmem_cache_destroy(ldcssp->memseg_cache); 414*4bac2208Snarayan 4151ae08745Sheppo /* 4161ae08745Sheppo * We have successfully "removed" the driver. 4171ae08745Sheppo * Destroying soft states 4181ae08745Sheppo */ 4191ae08745Sheppo mutex_destroy(&ldcssp->lock); 4201ae08745Sheppo kmem_free(ldcssp, sizeof (ldc_soft_state_t)); 4211ae08745Sheppo 4221ae08745Sheppo (void) hsvc_unregister(&ldc_hsvc); 4231ae08745Sheppo (void) hsvc_unregister(&intr_hsvc); 4241ae08745Sheppo 4251ae08745Sheppo return (status); 4261ae08745Sheppo } 4271ae08745Sheppo 4281ae08745Sheppo /* -------------------------------------------------------------------------- */ 4291ae08745Sheppo 4301ae08745Sheppo /* 431e1ebb9ecSlm66018 * LDC Link Layer Internal Functions 4321ae08745Sheppo */ 4331ae08745Sheppo 4341ae08745Sheppo /* 4351ae08745Sheppo * Translate HV Errors to sun4v error codes 4361ae08745Sheppo */ 4371ae08745Sheppo static int 4381ae08745Sheppo i_ldc_h2v_error(int h_error) 4391ae08745Sheppo { 4401ae08745Sheppo switch (h_error) { 4411ae08745Sheppo 4421ae08745Sheppo case H_EOK: 4431ae08745Sheppo return (0); 4441ae08745Sheppo 4451ae08745Sheppo case H_ENORADDR: 4461ae08745Sheppo return (EFAULT); 4471ae08745Sheppo 4481ae08745Sheppo case H_EBADPGSZ: 4491ae08745Sheppo case H_EINVAL: 4501ae08745Sheppo return (EINVAL); 4511ae08745Sheppo 4521ae08745Sheppo case H_EWOULDBLOCK: 4531ae08745Sheppo return (EWOULDBLOCK); 4541ae08745Sheppo 4551ae08745Sheppo case H_ENOACCESS: 4561ae08745Sheppo case H_ENOMAP: 4571ae08745Sheppo return (EACCES); 4581ae08745Sheppo 4591ae08745Sheppo case H_EIO: 4601ae08745Sheppo case H_ECPUERROR: 4611ae08745Sheppo return (EIO); 4621ae08745Sheppo 4631ae08745Sheppo case H_ENOTSUPPORTED: 4641ae08745Sheppo return (ENOTSUP); 4651ae08745Sheppo 4661ae08745Sheppo case H_ETOOMANY: 4671ae08745Sheppo return (ENOSPC); 4681ae08745Sheppo 4691ae08745Sheppo case H_ECHANNEL: 4701ae08745Sheppo return (ECHRNG); 4711ae08745Sheppo default: 4721ae08745Sheppo break; 4731ae08745Sheppo } 4741ae08745Sheppo 4751ae08745Sheppo return (EIO); 4761ae08745Sheppo } 4771ae08745Sheppo 4781ae08745Sheppo /* 4791ae08745Sheppo * Reconfigure the transmit queue 4801ae08745Sheppo */ 4811ae08745Sheppo static int 4821ae08745Sheppo i_ldc_txq_reconf(ldc_chan_t *ldcp) 4831ae08745Sheppo { 4841ae08745Sheppo int rv; 4851ae08745Sheppo 4861ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 487d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 488d10e4ef2Snarayan 4891ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 4901ae08745Sheppo if (rv) { 4911ae08745Sheppo cmn_err(CE_WARN, 4921ae08745Sheppo "ldc_tx_qconf: (0x%lx) cannot set qconf", ldcp->id); 4931ae08745Sheppo return (EIO); 4941ae08745Sheppo } 4951ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &(ldcp->tx_head), 4961ae08745Sheppo &(ldcp->tx_tail), &(ldcp->link_state)); 4971ae08745Sheppo if (rv) { 4981ae08745Sheppo cmn_err(CE_WARN, 4991ae08745Sheppo "ldc_tx_get_state: (0x%lx) cannot get qptrs", ldcp->id); 5001ae08745Sheppo return (EIO); 5011ae08745Sheppo } 5021ae08745Sheppo D1(ldcp->id, "ldc_tx_get_state: (0x%llx) h=0x%llx,t=0x%llx," 5031ae08745Sheppo "s=0x%llx\n", ldcp->id, ldcp->tx_head, ldcp->tx_tail, 5041ae08745Sheppo ldcp->link_state); 5051ae08745Sheppo 5061ae08745Sheppo return (0); 5071ae08745Sheppo } 5081ae08745Sheppo 5091ae08745Sheppo /* 5101ae08745Sheppo * Reconfigure the receive queue 5111ae08745Sheppo */ 5121ae08745Sheppo static int 5131ae08745Sheppo i_ldc_rxq_reconf(ldc_chan_t *ldcp) 5141ae08745Sheppo { 5151ae08745Sheppo int rv; 5161ae08745Sheppo uint64_t rx_head, rx_tail; 5171ae08745Sheppo 5181ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 5191ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 5201ae08745Sheppo &(ldcp->link_state)); 5211ae08745Sheppo if (rv) { 5221ae08745Sheppo cmn_err(CE_WARN, 5231ae08745Sheppo "ldc_rx_getstate: (0x%lx) cannot get state", 5241ae08745Sheppo ldcp->id); 5251ae08745Sheppo return (EIO); 5261ae08745Sheppo } 5271ae08745Sheppo 5281ae08745Sheppo if (rx_head != rx_tail || ldcp->tstate > TS_READY) { 5291ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, 5301ae08745Sheppo ldcp->rx_q_entries); 5311ae08745Sheppo if (rv) { 5321ae08745Sheppo cmn_err(CE_WARN, 5331ae08745Sheppo "ldc_rx_qconf: (0x%lx) cannot set qconf", 5341ae08745Sheppo ldcp->id); 5351ae08745Sheppo return (EIO); 5361ae08745Sheppo } 5371ae08745Sheppo D1(ldcp->id, "ldc_rx_qconf: (0x%llx) completed qconf", 5381ae08745Sheppo ldcp->id); 5391ae08745Sheppo } 5401ae08745Sheppo 5411ae08745Sheppo return (0); 5421ae08745Sheppo } 5431ae08745Sheppo 5441ae08745Sheppo /* 5451ae08745Sheppo * Reset LDC state structure and its contents 5461ae08745Sheppo */ 5471ae08745Sheppo static void 5481ae08745Sheppo i_ldc_reset_state(ldc_chan_t *ldcp) 5491ae08745Sheppo { 5501ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 5511ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 5521ae08745Sheppo ldcp->last_ack_rcd = 0; 5531ae08745Sheppo ldcp->last_msg_rcd = 0; 5541ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 5551ae08745Sheppo ldcp->next_vidx = 0; 5561ae08745Sheppo ldcp->hstate = 0; 5571ae08745Sheppo ldcp->tstate = TS_OPEN; 5581ae08745Sheppo ldcp->status = LDC_OPEN; 5591ae08745Sheppo 5601ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 5611ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 5621ae08745Sheppo 5631ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 5641ae08745Sheppo ldcp->status = LDC_UP; 5651ae08745Sheppo ldcp->tstate = TS_UP; 5661ae08745Sheppo } else { 5671ae08745Sheppo ldcp->status = LDC_READY; 5681ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 5691ae08745Sheppo } 5701ae08745Sheppo } 5711ae08745Sheppo } 5721ae08745Sheppo 5731ae08745Sheppo /* 5741ae08745Sheppo * Reset a LDC channel 5751ae08745Sheppo */ 5761ae08745Sheppo static void 5771ae08745Sheppo i_ldc_reset(ldc_chan_t *ldcp) 5781ae08745Sheppo { 5791ae08745Sheppo D2(ldcp->id, "i_ldc_reset: (0x%llx) channel reset\n", ldcp->id); 5801ae08745Sheppo 581d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->lock)); 582d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 583d10e4ef2Snarayan 5841ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 5851ae08745Sheppo (void) i_ldc_rxq_reconf(ldcp); 5861ae08745Sheppo i_ldc_reset_state(ldcp); 5871ae08745Sheppo } 5881ae08745Sheppo 589*4bac2208Snarayan 5901ae08745Sheppo /* 5911ae08745Sheppo * Clear pending interrupts 5921ae08745Sheppo */ 5931ae08745Sheppo static void 5941ae08745Sheppo i_ldc_clear_intr(ldc_chan_t *ldcp, cnex_intrtype_t itype) 5951ae08745Sheppo { 5961ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 5971ae08745Sheppo 5981ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 599*4bac2208Snarayan 600*4bac2208Snarayan if (cinfo->dip) { 601*4bac2208Snarayan /* check Tx interrupt */ 602*4bac2208Snarayan if (itype == CNEX_TX_INTR) { 603*4bac2208Snarayan if (ldcp->tx_intr_pending) 604*4bac2208Snarayan ldcp->tx_intr_pending = B_FALSE; 605*4bac2208Snarayan else 606*4bac2208Snarayan return; 607*4bac2208Snarayan } 608*4bac2208Snarayan /* check Rx interrupt */ 609*4bac2208Snarayan if (itype == CNEX_RX_INTR) { 610*4bac2208Snarayan if (ldcp->rx_intr_pending) 611*4bac2208Snarayan ldcp->rx_intr_pending = B_FALSE; 612*4bac2208Snarayan else 613*4bac2208Snarayan return; 614*4bac2208Snarayan } 615*4bac2208Snarayan 6161ae08745Sheppo (void) cinfo->clr_intr(cinfo->dip, ldcp->id, itype); 617*4bac2208Snarayan D2(ldcp->id, 618*4bac2208Snarayan "i_ldc_clear_intr: (0x%llx) cleared 0x%x intr\n", 619*4bac2208Snarayan ldcp->id, itype); 6201ae08745Sheppo } 6211ae08745Sheppo } 6221ae08745Sheppo 6231ae08745Sheppo /* 6241ae08745Sheppo * Set the receive queue head 6250a55fbb7Slm66018 * Resets connection and returns an error if it fails. 6261ae08745Sheppo */ 6271ae08745Sheppo static int 6281ae08745Sheppo i_ldc_set_rx_head(ldc_chan_t *ldcp, uint64_t head) 6291ae08745Sheppo { 6301ae08745Sheppo int rv; 6310a55fbb7Slm66018 int retries; 6321ae08745Sheppo 6331ae08745Sheppo ASSERT(MUTEX_HELD(&ldcp->lock)); 6340a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 6350a55fbb7Slm66018 6360a55fbb7Slm66018 if ((rv = hv_ldc_rx_set_qhead(ldcp->id, head)) == 0) 6370a55fbb7Slm66018 return (0); 6380a55fbb7Slm66018 6390a55fbb7Slm66018 if (rv != H_EWOULDBLOCK) 6400a55fbb7Slm66018 break; 6410a55fbb7Slm66018 6420a55fbb7Slm66018 /* wait for ldc_delay usecs */ 6430a55fbb7Slm66018 drv_usecwait(ldc_delay); 6441ae08745Sheppo } 6451ae08745Sheppo 6460a55fbb7Slm66018 cmn_err(CE_WARN, "ldc_rx_set_qhead: (0x%lx) cannot set qhead 0x%lx", 6470a55fbb7Slm66018 ldcp->id, head); 648d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 6490a55fbb7Slm66018 i_ldc_reset(ldcp); 650d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 6510a55fbb7Slm66018 6520a55fbb7Slm66018 return (ECONNRESET); 6531ae08745Sheppo } 6541ae08745Sheppo 6551ae08745Sheppo 6561ae08745Sheppo /* 6571ae08745Sheppo * Returns the tx_tail to be used for transfer 6581ae08745Sheppo * Re-reads the TX queue ptrs if and only if the 6591ae08745Sheppo * the cached head and tail are equal (queue is full) 6601ae08745Sheppo */ 6611ae08745Sheppo static int 6621ae08745Sheppo i_ldc_get_tx_tail(ldc_chan_t *ldcp, uint64_t *tail) 6631ae08745Sheppo { 6641ae08745Sheppo int rv; 6651ae08745Sheppo uint64_t current_head, new_tail; 6661ae08745Sheppo 667d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 6681ae08745Sheppo /* Read the head and tail ptrs from HV */ 6691ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 6701ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 6711ae08745Sheppo if (rv) { 6721ae08745Sheppo cmn_err(CE_WARN, 6731ae08745Sheppo "i_ldc_get_tx_tail: (0x%lx) cannot read qptrs\n", 6741ae08745Sheppo ldcp->id); 6751ae08745Sheppo return (EIO); 6761ae08745Sheppo } 6771ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 6781ae08745Sheppo DWARN(DBG_ALL_LDCS, 6791ae08745Sheppo "i_ldc_get_tx_tail: (0x%llx) channel not ready\n", 6801ae08745Sheppo ldcp->id); 6811ae08745Sheppo return (ECONNRESET); 6821ae08745Sheppo } 6831ae08745Sheppo 6841ae08745Sheppo /* In reliable mode, check against last ACKd msg */ 6851ae08745Sheppo current_head = (ldcp->mode == LDC_MODE_RELIABLE || 6861ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 6871ae08745Sheppo ? ldcp->tx_ackd_head : ldcp->tx_head; 6881ae08745Sheppo 6891ae08745Sheppo /* increment the tail */ 6901ae08745Sheppo new_tail = (ldcp->tx_tail + LDC_PACKET_SIZE) % 6911ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 6921ae08745Sheppo 6931ae08745Sheppo if (new_tail == current_head) { 6941ae08745Sheppo DWARN(ldcp->id, 6951ae08745Sheppo "i_ldc_get_tx_tail: (0x%llx) TX queue is full\n", 6961ae08745Sheppo ldcp->id); 6971ae08745Sheppo return (EWOULDBLOCK); 6981ae08745Sheppo } 6991ae08745Sheppo 7001ae08745Sheppo D2(ldcp->id, "i_ldc_get_tx_tail: (0x%llx) head=0x%llx, tail=0x%llx\n", 7011ae08745Sheppo ldcp->id, ldcp->tx_head, ldcp->tx_tail); 7021ae08745Sheppo 7031ae08745Sheppo *tail = ldcp->tx_tail; 7041ae08745Sheppo return (0); 7051ae08745Sheppo } 7061ae08745Sheppo 7071ae08745Sheppo /* 7081ae08745Sheppo * Set the tail pointer. If HV returns EWOULDBLOCK, it will back off 7090a55fbb7Slm66018 * and retry ldc_max_retries times before returning an error. 7101ae08745Sheppo * Returns 0, EWOULDBLOCK or EIO 7111ae08745Sheppo */ 7121ae08745Sheppo static int 7131ae08745Sheppo i_ldc_set_tx_tail(ldc_chan_t *ldcp, uint64_t tail) 7141ae08745Sheppo { 7151ae08745Sheppo int rv, retval = EWOULDBLOCK; 7160a55fbb7Slm66018 int retries; 7171ae08745Sheppo 718d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 7190a55fbb7Slm66018 for (retries = 0; retries < ldc_max_retries; retries++) { 7201ae08745Sheppo 7211ae08745Sheppo if ((rv = hv_ldc_tx_set_qtail(ldcp->id, tail)) == 0) { 7221ae08745Sheppo retval = 0; 7231ae08745Sheppo break; 7241ae08745Sheppo } 7251ae08745Sheppo if (rv != H_EWOULDBLOCK) { 7261ae08745Sheppo DWARN(ldcp->id, "i_ldc_set_tx_tail: (0x%llx) set " 7271ae08745Sheppo "qtail=0x%llx failed, rv=%d\n", ldcp->id, tail, rv); 7281ae08745Sheppo retval = EIO; 7291ae08745Sheppo break; 7301ae08745Sheppo } 7311ae08745Sheppo 7320a55fbb7Slm66018 /* wait for ldc_delay usecs */ 7330a55fbb7Slm66018 drv_usecwait(ldc_delay); 7341ae08745Sheppo } 7351ae08745Sheppo return (retval); 7361ae08745Sheppo } 7371ae08745Sheppo 7381ae08745Sheppo /* 7391ae08745Sheppo * Send a LDC message 7401ae08745Sheppo */ 7411ae08745Sheppo static int 7421ae08745Sheppo i_ldc_send_pkt(ldc_chan_t *ldcp, uint8_t pkttype, uint8_t subtype, 7431ae08745Sheppo uint8_t ctrlmsg) 7441ae08745Sheppo { 7451ae08745Sheppo int rv; 7461ae08745Sheppo ldc_msg_t *pkt; 7471ae08745Sheppo uint64_t tx_tail; 7481ae08745Sheppo uint32_t curr_seqid = ldcp->last_msg_snt; 7491ae08745Sheppo 750d10e4ef2Snarayan /* Obtain Tx lock */ 751d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 752d10e4ef2Snarayan 7531ae08745Sheppo /* get the current tail for the message */ 7541ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 7551ae08745Sheppo if (rv) { 7561ae08745Sheppo DWARN(ldcp->id, 7571ae08745Sheppo "i_ldc_send_pkt: (0x%llx) error sending pkt, " 7581ae08745Sheppo "type=0x%x,subtype=0x%x,ctrl=0x%x\n", 7591ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 760d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 7611ae08745Sheppo return (rv); 7621ae08745Sheppo } 7631ae08745Sheppo 7641ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 7651ae08745Sheppo ZERO_PKT(pkt); 7661ae08745Sheppo 7671ae08745Sheppo /* Initialize the packet */ 7681ae08745Sheppo pkt->type = pkttype; 7691ae08745Sheppo pkt->stype = subtype; 7701ae08745Sheppo pkt->ctrl = ctrlmsg; 7711ae08745Sheppo 7721ae08745Sheppo /* Store ackid/seqid iff it is RELIABLE mode & not a RTS/RTR message */ 7731ae08745Sheppo if (((ctrlmsg & LDC_CTRL_MASK) != LDC_RTS) && 7741ae08745Sheppo ((ctrlmsg & LDC_CTRL_MASK) != LDC_RTR)) { 7751ae08745Sheppo curr_seqid++; 7761ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) { 7771ae08745Sheppo pkt->seqid = curr_seqid; 7781ae08745Sheppo pkt->ackid = ldcp->last_msg_rcd; 7791ae08745Sheppo } 7801ae08745Sheppo } 7811ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_send_pkt", (uint64_t)pkt); 7821ae08745Sheppo 7831ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 7841ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 7851ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 7861ae08745Sheppo 7871ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 7881ae08745Sheppo if (rv) { 7891ae08745Sheppo DWARN(ldcp->id, 7901ae08745Sheppo "i_ldc_send_pkt:(0x%llx) error sending pkt, " 7911ae08745Sheppo "type=0x%x,stype=0x%x,ctrl=0x%x\n", 7921ae08745Sheppo ldcp->id, pkttype, subtype, ctrlmsg); 793d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 7941ae08745Sheppo return (EIO); 7951ae08745Sheppo } 7961ae08745Sheppo 7971ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 7981ae08745Sheppo ldcp->tx_tail = tx_tail; 7991ae08745Sheppo 800d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 8011ae08745Sheppo return (0); 8021ae08745Sheppo } 8031ae08745Sheppo 8041ae08745Sheppo /* 8051ae08745Sheppo * Checks if packet was received in right order 806e1ebb9ecSlm66018 * in the case of a reliable link. 8071ae08745Sheppo * Returns 0 if in order, else EIO 8081ae08745Sheppo */ 8091ae08745Sheppo static int 8101ae08745Sheppo i_ldc_check_seqid(ldc_chan_t *ldcp, ldc_msg_t *msg) 8111ae08745Sheppo { 8121ae08745Sheppo /* No seqid checking for RAW mode */ 8131ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) 8141ae08745Sheppo return (0); 8151ae08745Sheppo 8161ae08745Sheppo /* No seqid checking for version, RTS, RTR message */ 8171ae08745Sheppo if (msg->ctrl == LDC_VER || 8181ae08745Sheppo msg->ctrl == LDC_RTS || 8191ae08745Sheppo msg->ctrl == LDC_RTR) 8201ae08745Sheppo return (0); 8211ae08745Sheppo 8221ae08745Sheppo /* Initial seqid to use is sent in RTS/RTR and saved in last_msg_rcd */ 8231ae08745Sheppo if (msg->seqid != (ldcp->last_msg_rcd + 1)) { 8241ae08745Sheppo DWARN(ldcp->id, 8251ae08745Sheppo "i_ldc_check_seqid: (0x%llx) out-of-order pkt, got 0x%x, " 8261ae08745Sheppo "expecting 0x%x\n", ldcp->id, msg->seqid, 8271ae08745Sheppo (ldcp->last_msg_rcd + 1)); 8281ae08745Sheppo return (EIO); 8291ae08745Sheppo } 8301ae08745Sheppo 8311ae08745Sheppo return (0); 8321ae08745Sheppo } 8331ae08745Sheppo 8341ae08745Sheppo 8351ae08745Sheppo /* 8361ae08745Sheppo * Process an incoming version ctrl message 8371ae08745Sheppo */ 8381ae08745Sheppo static int 8391ae08745Sheppo i_ldc_process_VER(ldc_chan_t *ldcp, ldc_msg_t *msg) 8401ae08745Sheppo { 8411ae08745Sheppo int rv = 0, idx = ldcp->next_vidx; 8421ae08745Sheppo ldc_msg_t *pkt; 8431ae08745Sheppo uint64_t tx_tail; 8441ae08745Sheppo ldc_ver_t *rcvd_ver; 8451ae08745Sheppo 8461ae08745Sheppo /* get the received version */ 8471ae08745Sheppo rcvd_ver = (ldc_ver_t *)((uint64_t)msg + LDC_PAYLOAD_VER_OFF); 8481ae08745Sheppo 8491ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) received VER v%u.%u\n", 8501ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 8511ae08745Sheppo 852d10e4ef2Snarayan /* Obtain Tx lock */ 853d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 854d10e4ef2Snarayan 8551ae08745Sheppo switch (msg->stype) { 8561ae08745Sheppo case LDC_INFO: 8571ae08745Sheppo 8581ae08745Sheppo /* get the current tail and pkt for the response */ 8591ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 8601ae08745Sheppo if (rv != 0) { 8611ae08745Sheppo DWARN(ldcp->id, 8621ae08745Sheppo "i_ldc_process_VER: (0x%llx) err sending " 8631ae08745Sheppo "version ACK/NACK\n", ldcp->id); 8641ae08745Sheppo i_ldc_reset(ldcp); 865d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 8661ae08745Sheppo return (ECONNRESET); 8671ae08745Sheppo } 8681ae08745Sheppo 8691ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 8701ae08745Sheppo ZERO_PKT(pkt); 8711ae08745Sheppo 8721ae08745Sheppo /* initialize the packet */ 8731ae08745Sheppo pkt->type = LDC_CTRL; 8741ae08745Sheppo pkt->ctrl = LDC_VER; 8751ae08745Sheppo 8761ae08745Sheppo for (;;) { 8771ae08745Sheppo 8781ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: got %u.%u chk %u.%u\n", 8791ae08745Sheppo rcvd_ver->major, rcvd_ver->minor, 8801ae08745Sheppo ldc_versions[idx].major, ldc_versions[idx].minor); 8811ae08745Sheppo 8821ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 8831ae08745Sheppo /* major version match - ACK version */ 8841ae08745Sheppo pkt->stype = LDC_ACK; 8851ae08745Sheppo 8861ae08745Sheppo /* 8871ae08745Sheppo * lower minor version to the one this endpt 8881ae08745Sheppo * supports, if necessary 8891ae08745Sheppo */ 8901ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 8911ae08745Sheppo rcvd_ver->minor = 8921ae08745Sheppo ldc_versions[idx].minor; 8931ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 8941ae08745Sheppo 8951ae08745Sheppo break; 8961ae08745Sheppo } 8971ae08745Sheppo 8981ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 8991ae08745Sheppo 9001ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 9011ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 9021ae08745Sheppo ldc_versions[idx].major, 9031ae08745Sheppo ldc_versions[idx].minor); 9041ae08745Sheppo 9051ae08745Sheppo /* nack with next lower version */ 9061ae08745Sheppo pkt->stype = LDC_NACK; 9071ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 9081ae08745Sheppo sizeof (ldc_versions[idx])); 9091ae08745Sheppo ldcp->next_vidx = idx; 9101ae08745Sheppo break; 9111ae08745Sheppo } 9121ae08745Sheppo 9131ae08745Sheppo /* next major version */ 9141ae08745Sheppo idx++; 9151ae08745Sheppo 9161ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 9171ae08745Sheppo 9181ae08745Sheppo if (idx == LDC_NUM_VERS) { 9191ae08745Sheppo /* no version match - send NACK */ 9201ae08745Sheppo pkt->stype = LDC_NACK; 9211ae08745Sheppo bzero(pkt->udata, sizeof (ldc_ver_t)); 9221ae08745Sheppo ldcp->next_vidx = 0; 9231ae08745Sheppo break; 9241ae08745Sheppo } 9251ae08745Sheppo } 9261ae08745Sheppo 9271ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 9281ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 9291ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 9301ae08745Sheppo 9311ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 9321ae08745Sheppo if (rv == 0) { 9331ae08745Sheppo ldcp->tx_tail = tx_tail; 9341ae08745Sheppo if (pkt->stype == LDC_ACK) { 9351ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent" 9361ae08745Sheppo " version ACK\n", ldcp->id); 9371ae08745Sheppo /* Save the ACK'd version */ 9381ae08745Sheppo ldcp->version.major = rcvd_ver->major; 9391ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 9400a55fbb7Slm66018 ldcp->hstate |= TS_RCVD_VER; 9411ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 9421ae08745Sheppo DWARN(DBG_ALL_LDCS, 9431ae08745Sheppo "(0x%llx) Agreed on version v%u.%u\n", 9441ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 9451ae08745Sheppo } 9461ae08745Sheppo } else { 9471ae08745Sheppo DWARN(ldcp->id, 9481ae08745Sheppo "i_ldc_process_VER: (0x%llx) error sending " 9491ae08745Sheppo "ACK/NACK\n", ldcp->id); 9501ae08745Sheppo i_ldc_reset(ldcp); 951d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 9521ae08745Sheppo return (ECONNRESET); 9531ae08745Sheppo } 9541ae08745Sheppo 9551ae08745Sheppo break; 9561ae08745Sheppo 9571ae08745Sheppo case LDC_ACK: 9581ae08745Sheppo /* SUCCESS - we have agreed on a version */ 9591ae08745Sheppo ldcp->version.major = rcvd_ver->major; 9601ae08745Sheppo ldcp->version.minor = rcvd_ver->minor; 9611ae08745Sheppo ldcp->tstate |= TS_VER_DONE; 9621ae08745Sheppo 9631ae08745Sheppo D1(DBG_ALL_LDCS, "(0x%llx) Agreed on version v%u.%u\n", 9641ae08745Sheppo ldcp->id, rcvd_ver->major, rcvd_ver->minor); 9651ae08745Sheppo 9661ae08745Sheppo /* initiate RTS-RTR-RDX handshake */ 9671ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 9681ae08745Sheppo if (rv) { 9691ae08745Sheppo DWARN(ldcp->id, 9701ae08745Sheppo "i_ldc_process_VER: (0x%llx) cannot send RTS\n", 9711ae08745Sheppo ldcp->id); 9721ae08745Sheppo i_ldc_reset(ldcp); 973d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 9741ae08745Sheppo return (ECONNRESET); 9751ae08745Sheppo } 9761ae08745Sheppo 9771ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 9781ae08745Sheppo ZERO_PKT(pkt); 9791ae08745Sheppo 9801ae08745Sheppo pkt->type = LDC_CTRL; 9811ae08745Sheppo pkt->stype = LDC_INFO; 9821ae08745Sheppo pkt->ctrl = LDC_RTS; 9831ae08745Sheppo pkt->env = ldcp->mode; 9841ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 9851ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 9861ae08745Sheppo 9871ae08745Sheppo ldcp->last_msg_rcd = LDC_INIT_SEQID; 9881ae08745Sheppo 9891ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_VER snd rts", (uint64_t)pkt); 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) { 9971ae08745Sheppo D2(ldcp->id, 9981ae08745Sheppo "i_ldc_process_VER: (0x%llx) no listener\n", 9991ae08745Sheppo ldcp->id); 10001ae08745Sheppo i_ldc_reset(ldcp); 1001d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10021ae08745Sheppo return (ECONNRESET); 10031ae08745Sheppo } 10041ae08745Sheppo 10051ae08745Sheppo ldcp->tx_tail = tx_tail; 10061ae08745Sheppo ldcp->hstate |= TS_SENT_RTS; 10071ae08745Sheppo 10081ae08745Sheppo break; 10091ae08745Sheppo 10101ae08745Sheppo case LDC_NACK: 10111ae08745Sheppo /* check if version in NACK is zero */ 10121ae08745Sheppo if (rcvd_ver->major == 0 && rcvd_ver->minor == 0) { 10131ae08745Sheppo /* version handshake failure */ 10141ae08745Sheppo DWARN(DBG_ALL_LDCS, 10151ae08745Sheppo "i_ldc_process_VER: (0x%llx) no version match\n", 10161ae08745Sheppo ldcp->id); 10171ae08745Sheppo i_ldc_reset(ldcp); 1018d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10191ae08745Sheppo return (ECONNRESET); 10201ae08745Sheppo } 10211ae08745Sheppo 10221ae08745Sheppo /* get the current tail and pkt for the response */ 10231ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 10241ae08745Sheppo if (rv != 0) { 10251ae08745Sheppo cmn_err(CE_NOTE, 10261ae08745Sheppo "i_ldc_process_VER: (0x%lx) err sending " 10271ae08745Sheppo "version ACK/NACK\n", ldcp->id); 10281ae08745Sheppo i_ldc_reset(ldcp); 1029d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10301ae08745Sheppo return (ECONNRESET); 10311ae08745Sheppo } 10321ae08745Sheppo 10331ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 10341ae08745Sheppo ZERO_PKT(pkt); 10351ae08745Sheppo 10361ae08745Sheppo /* initialize the packet */ 10371ae08745Sheppo pkt->type = LDC_CTRL; 10381ae08745Sheppo pkt->ctrl = LDC_VER; 10391ae08745Sheppo pkt->stype = LDC_INFO; 10401ae08745Sheppo 10411ae08745Sheppo /* check ver in NACK msg has a match */ 10421ae08745Sheppo for (;;) { 10431ae08745Sheppo if (rcvd_ver->major == ldc_versions[idx].major) { 10441ae08745Sheppo /* 10451ae08745Sheppo * major version match - resubmit request 10461ae08745Sheppo * if lower minor version to the one this endpt 10471ae08745Sheppo * supports, if necessary 10481ae08745Sheppo */ 10491ae08745Sheppo if (rcvd_ver->minor > ldc_versions[idx].minor) 10501ae08745Sheppo rcvd_ver->minor = 10511ae08745Sheppo ldc_versions[idx].minor; 10521ae08745Sheppo bcopy(rcvd_ver, pkt->udata, sizeof (*rcvd_ver)); 10531ae08745Sheppo break; 10541ae08745Sheppo 10551ae08745Sheppo } 10561ae08745Sheppo 10571ae08745Sheppo if (rcvd_ver->major > ldc_versions[idx].major) { 10581ae08745Sheppo 10591ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: using next" 10601ae08745Sheppo " lower idx=%d, v%u.%u\n", idx, 10611ae08745Sheppo ldc_versions[idx].major, 10621ae08745Sheppo ldc_versions[idx].minor); 10631ae08745Sheppo 10641ae08745Sheppo /* send next lower version */ 10651ae08745Sheppo bcopy(&ldc_versions[idx], pkt->udata, 10661ae08745Sheppo sizeof (ldc_versions[idx])); 10671ae08745Sheppo ldcp->next_vidx = idx; 10681ae08745Sheppo break; 10691ae08745Sheppo } 10701ae08745Sheppo 10711ae08745Sheppo /* next version */ 10721ae08745Sheppo idx++; 10731ae08745Sheppo 10741ae08745Sheppo D1(ldcp->id, "i_ldc_process_VER: inc idx %x\n", idx); 10751ae08745Sheppo 10761ae08745Sheppo if (idx == LDC_NUM_VERS) { 10771ae08745Sheppo /* no version match - terminate */ 10781ae08745Sheppo ldcp->next_vidx = 0; 1079d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 10801ae08745Sheppo return (ECONNRESET); 10811ae08745Sheppo } 10821ae08745Sheppo } 10831ae08745Sheppo 10841ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 10851ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 10861ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 10871ae08745Sheppo 10881ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 10891ae08745Sheppo if (rv == 0) { 10901ae08745Sheppo D2(ldcp->id, "i_ldc_process_VER: (0x%llx) sent version" 10911ae08745Sheppo "INFO v%u.%u\n", ldcp->id, ldc_versions[idx].major, 10921ae08745Sheppo ldc_versions[idx].minor); 10931ae08745Sheppo ldcp->tx_tail = tx_tail; 10941ae08745Sheppo } else { 10951ae08745Sheppo cmn_err(CE_NOTE, 10961ae08745Sheppo "i_ldc_process_VER: (0x%lx) error sending version" 10971ae08745Sheppo "INFO\n", ldcp->id); 10981ae08745Sheppo i_ldc_reset(ldcp); 1099d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11001ae08745Sheppo return (ECONNRESET); 11011ae08745Sheppo } 11021ae08745Sheppo 11031ae08745Sheppo break; 11041ae08745Sheppo } 11051ae08745Sheppo 1106d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11071ae08745Sheppo return (rv); 11081ae08745Sheppo } 11091ae08745Sheppo 11101ae08745Sheppo 11111ae08745Sheppo /* 11121ae08745Sheppo * Process an incoming RTS ctrl message 11131ae08745Sheppo */ 11141ae08745Sheppo static int 11151ae08745Sheppo i_ldc_process_RTS(ldc_chan_t *ldcp, ldc_msg_t *msg) 11161ae08745Sheppo { 11171ae08745Sheppo int rv = 0; 11181ae08745Sheppo ldc_msg_t *pkt; 11191ae08745Sheppo uint64_t tx_tail; 11201ae08745Sheppo boolean_t sent_NACK = B_FALSE; 11211ae08745Sheppo 11221ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTS: (0x%llx) received RTS\n", ldcp->id); 11231ae08745Sheppo 11241ae08745Sheppo switch (msg->stype) { 11251ae08745Sheppo case LDC_NACK: 11261ae08745Sheppo DWARN(ldcp->id, 11271ae08745Sheppo "i_ldc_process_RTS: (0x%llx) RTS NACK received\n", 11281ae08745Sheppo ldcp->id); 11291ae08745Sheppo 11301ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1131d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 11321ae08745Sheppo i_ldc_reset(ldcp); 1133d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11341ae08745Sheppo rv = ECONNRESET; 11351ae08745Sheppo break; 11361ae08745Sheppo 11371ae08745Sheppo case LDC_INFO: 11381ae08745Sheppo 11391ae08745Sheppo /* check mode */ 11401ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 11411ae08745Sheppo cmn_err(CE_NOTE, 11421ae08745Sheppo "i_ldc_process_RTS: (0x%lx) mode mismatch\n", 11431ae08745Sheppo ldcp->id); 11441ae08745Sheppo /* 11451ae08745Sheppo * send NACK in response to MODE message 11461ae08745Sheppo * get the current tail for the response 11471ae08745Sheppo */ 11481ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTS); 11491ae08745Sheppo if (rv) { 11501ae08745Sheppo /* if cannot send NACK - reset channel */ 1151d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 11521ae08745Sheppo i_ldc_reset(ldcp); 1153d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11541ae08745Sheppo rv = ECONNRESET; 11551ae08745Sheppo break; 11561ae08745Sheppo } 11571ae08745Sheppo sent_NACK = B_TRUE; 11581ae08745Sheppo } 11591ae08745Sheppo break; 11601ae08745Sheppo default: 11611ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTS: (0x%llx) unexp ACK\n", 11621ae08745Sheppo ldcp->id); 1163d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 11641ae08745Sheppo i_ldc_reset(ldcp); 1165d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11661ae08745Sheppo rv = ECONNRESET; 11671ae08745Sheppo break; 11681ae08745Sheppo } 11691ae08745Sheppo 11701ae08745Sheppo /* 11711ae08745Sheppo * If either the connection was reset (when rv != 0) or 11721ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 11731ae08745Sheppo * we dont want to consume the packet that came in but 11741ae08745Sheppo * not record that we received the RTS 11751ae08745Sheppo */ 11761ae08745Sheppo if (rv || sent_NACK) 11771ae08745Sheppo return (rv); 11781ae08745Sheppo 11791ae08745Sheppo /* record RTS received */ 11801ae08745Sheppo ldcp->hstate |= TS_RCVD_RTS; 11811ae08745Sheppo 11821ae08745Sheppo /* store initial SEQID info */ 11831ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 11841ae08745Sheppo 1185d10e4ef2Snarayan /* Obtain Tx lock */ 1186d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1187d10e4ef2Snarayan 11881ae08745Sheppo /* get the current tail for the response */ 11891ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 11901ae08745Sheppo if (rv != 0) { 11911ae08745Sheppo cmn_err(CE_NOTE, 11921ae08745Sheppo "i_ldc_process_RTS: (0x%lx) err sending RTR\n", 11931ae08745Sheppo ldcp->id); 11941ae08745Sheppo i_ldc_reset(ldcp); 1195d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 11961ae08745Sheppo return (ECONNRESET); 11971ae08745Sheppo } 11981ae08745Sheppo 11991ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 12001ae08745Sheppo ZERO_PKT(pkt); 12011ae08745Sheppo 12021ae08745Sheppo /* initialize the packet */ 12031ae08745Sheppo pkt->type = LDC_CTRL; 12041ae08745Sheppo pkt->stype = LDC_INFO; 12051ae08745Sheppo pkt->ctrl = LDC_RTR; 12061ae08745Sheppo pkt->env = ldcp->mode; 12071ae08745Sheppo if (ldcp->mode != LDC_MODE_RAW) 12081ae08745Sheppo pkt->seqid = LDC_INIT_SEQID; 12091ae08745Sheppo 12101ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 12111ae08745Sheppo 12121ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 12131ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 12141ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 12151ae08745Sheppo 12161ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 12171ae08745Sheppo if (rv == 0) { 12181ae08745Sheppo D2(ldcp->id, 12191ae08745Sheppo "i_ldc_process_RTS: (0x%llx) sent RTR\n", ldcp->id); 12201ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_process_RTS sent rtr", (uint64_t)pkt); 12211ae08745Sheppo 12221ae08745Sheppo ldcp->tx_tail = tx_tail; 12231ae08745Sheppo ldcp->hstate |= TS_SENT_RTR; 12241ae08745Sheppo 12251ae08745Sheppo } else { 12261ae08745Sheppo cmn_err(CE_NOTE, 12271ae08745Sheppo "i_ldc_process_RTS: (0x%lx) error sending RTR\n", 12281ae08745Sheppo ldcp->id); 12291ae08745Sheppo i_ldc_reset(ldcp); 1230d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12311ae08745Sheppo return (ECONNRESET); 12321ae08745Sheppo } 12331ae08745Sheppo 1234d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12351ae08745Sheppo return (0); 12361ae08745Sheppo } 12371ae08745Sheppo 12381ae08745Sheppo /* 12391ae08745Sheppo * Process an incoming RTR ctrl message 12401ae08745Sheppo */ 12411ae08745Sheppo static int 12421ae08745Sheppo i_ldc_process_RTR(ldc_chan_t *ldcp, ldc_msg_t *msg) 12431ae08745Sheppo { 12441ae08745Sheppo int rv = 0; 12451ae08745Sheppo boolean_t sent_NACK = B_FALSE; 12461ae08745Sheppo 12471ae08745Sheppo D2(ldcp->id, "i_ldc_process_RTR: (0x%llx) received RTR\n", ldcp->id); 12481ae08745Sheppo 12491ae08745Sheppo switch (msg->stype) { 12501ae08745Sheppo case LDC_NACK: 12511ae08745Sheppo /* RTR NACK received */ 12521ae08745Sheppo DWARN(ldcp->id, 12531ae08745Sheppo "i_ldc_process_RTR: (0x%llx) RTR NACK received\n", 12541ae08745Sheppo ldcp->id); 12551ae08745Sheppo 12561ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1257d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 12581ae08745Sheppo i_ldc_reset(ldcp); 1259d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12601ae08745Sheppo rv = ECONNRESET; 12611ae08745Sheppo 12621ae08745Sheppo break; 12631ae08745Sheppo 12641ae08745Sheppo case LDC_INFO: 12651ae08745Sheppo 12661ae08745Sheppo /* check mode */ 12671ae08745Sheppo if (ldcp->mode != (ldc_mode_t)msg->env) { 12681ae08745Sheppo DWARN(ldcp->id, 12691ae08745Sheppo "i_ldc_process_RTR: (0x%llx) mode mismatch\n", 12701ae08745Sheppo ldcp->id); 12711ae08745Sheppo /* 12721ae08745Sheppo * send NACK in response to MODE message 12731ae08745Sheppo * get the current tail for the response 12741ae08745Sheppo */ 12751ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, LDC_RTR); 12761ae08745Sheppo if (rv) { 12771ae08745Sheppo /* if cannot send NACK - reset channel */ 1278d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 12791ae08745Sheppo i_ldc_reset(ldcp); 1280d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12811ae08745Sheppo rv = ECONNRESET; 12821ae08745Sheppo break; 12831ae08745Sheppo } 12841ae08745Sheppo sent_NACK = B_TRUE; 12851ae08745Sheppo } 12861ae08745Sheppo break; 12871ae08745Sheppo 12881ae08745Sheppo default: 12891ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RTR: (0x%llx) unexp ACK\n", 12901ae08745Sheppo ldcp->id); 12911ae08745Sheppo 12921ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1293d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 12941ae08745Sheppo i_ldc_reset(ldcp); 1295d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 12961ae08745Sheppo rv = ECONNRESET; 12971ae08745Sheppo break; 12981ae08745Sheppo } 12991ae08745Sheppo 13001ae08745Sheppo /* 13011ae08745Sheppo * If either the connection was reset (when rv != 0) or 13021ae08745Sheppo * a NACK was sent, we return. In the case of a NACK 13031ae08745Sheppo * we dont want to consume the packet that came in but 13041ae08745Sheppo * not record that we received the RTR 13051ae08745Sheppo */ 13061ae08745Sheppo if (rv || sent_NACK) 13071ae08745Sheppo return (rv); 13081ae08745Sheppo 13091ae08745Sheppo ldcp->last_msg_snt = msg->seqid; 13101ae08745Sheppo ldcp->hstate |= TS_RCVD_RTR; 13111ae08745Sheppo 13121ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_INFO, LDC_RDX); 13131ae08745Sheppo if (rv) { 13141ae08745Sheppo cmn_err(CE_NOTE, 13151ae08745Sheppo "i_ldc_process_RTR: (0x%lx) cannot send RDX\n", 13161ae08745Sheppo ldcp->id); 1317d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 13181ae08745Sheppo i_ldc_reset(ldcp); 1319d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13201ae08745Sheppo return (ECONNRESET); 13211ae08745Sheppo } 13221ae08745Sheppo D2(ldcp->id, 13231ae08745Sheppo "i_ldc_process_RTR: (0x%llx) sent RDX\n", ldcp->id); 13241ae08745Sheppo 13251ae08745Sheppo ldcp->hstate |= TS_SENT_RDX; 13261ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 13271ae08745Sheppo ldcp->status = LDC_UP; 13281ae08745Sheppo 13291ae08745Sheppo DWARN(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id); 13301ae08745Sheppo 13311ae08745Sheppo return (0); 13321ae08745Sheppo } 13331ae08745Sheppo 13341ae08745Sheppo 13351ae08745Sheppo /* 13361ae08745Sheppo * Process an incoming RDX ctrl message 13371ae08745Sheppo */ 13381ae08745Sheppo static int 13391ae08745Sheppo i_ldc_process_RDX(ldc_chan_t *ldcp, ldc_msg_t *msg) 13401ae08745Sheppo { 13411ae08745Sheppo int rv = 0; 13421ae08745Sheppo 13431ae08745Sheppo D2(ldcp->id, "i_ldc_process_RDX: (0x%llx) received RDX\n", ldcp->id); 13441ae08745Sheppo 13451ae08745Sheppo switch (msg->stype) { 13461ae08745Sheppo case LDC_NACK: 13471ae08745Sheppo /* RDX NACK received */ 13481ae08745Sheppo DWARN(ldcp->id, 13491ae08745Sheppo "i_ldc_process_RDX: (0x%llx) RDX NACK received\n", 13501ae08745Sheppo ldcp->id); 13511ae08745Sheppo 13521ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1353d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 13541ae08745Sheppo i_ldc_reset(ldcp); 1355d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13561ae08745Sheppo rv = ECONNRESET; 13571ae08745Sheppo 13581ae08745Sheppo break; 13591ae08745Sheppo 13601ae08745Sheppo case LDC_INFO: 13611ae08745Sheppo 13621ae08745Sheppo /* 13631ae08745Sheppo * if channel is UP and a RDX received after data transmission 13641ae08745Sheppo * has commenced it is an error 13651ae08745Sheppo */ 13661ae08745Sheppo if ((ldcp->tstate == TS_UP) && (ldcp->hstate & TS_RCVD_RDX)) { 13671ae08745Sheppo DWARN(DBG_ALL_LDCS, 13681ae08745Sheppo "i_ldc_process_RDX: (0x%llx) unexpected RDX" 13691ae08745Sheppo " - LDC reset\n", ldcp->id); 1370d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 13711ae08745Sheppo i_ldc_reset(ldcp); 1372d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13731ae08745Sheppo return (ECONNRESET); 13741ae08745Sheppo } 13751ae08745Sheppo 13761ae08745Sheppo ldcp->hstate |= TS_RCVD_RDX; 13771ae08745Sheppo ldcp->tstate |= TS_HSHAKE_DONE; 13781ae08745Sheppo ldcp->status = LDC_UP; 13791ae08745Sheppo 13801ae08745Sheppo D1(DBG_ALL_LDCS, "(0x%llx) Handshake Complete\n", ldcp->id); 13811ae08745Sheppo break; 13821ae08745Sheppo 13831ae08745Sheppo default: 13841ae08745Sheppo DWARN(ldcp->id, "i_ldc_process_RDX: (0x%llx) unexp ACK\n", 13851ae08745Sheppo ldcp->id); 13861ae08745Sheppo 13871ae08745Sheppo /* Reset the channel -- as we cannot continue */ 1388d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 13891ae08745Sheppo i_ldc_reset(ldcp); 1390d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 13911ae08745Sheppo rv = ECONNRESET; 13921ae08745Sheppo break; 13931ae08745Sheppo } 13941ae08745Sheppo 13951ae08745Sheppo return (rv); 13961ae08745Sheppo } 13971ae08745Sheppo 13981ae08745Sheppo /* 13991ae08745Sheppo * Process an incoming ACK for a data packet 14001ae08745Sheppo */ 14011ae08745Sheppo static int 14021ae08745Sheppo i_ldc_process_data_ACK(ldc_chan_t *ldcp, ldc_msg_t *msg) 14031ae08745Sheppo { 14041ae08745Sheppo int rv; 14051ae08745Sheppo uint64_t tx_head; 14061ae08745Sheppo ldc_msg_t *pkt; 14071ae08745Sheppo 1408d10e4ef2Snarayan /* Obtain Tx lock */ 1409d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1410d10e4ef2Snarayan 14111ae08745Sheppo /* 1412d10e4ef2Snarayan * Read the current Tx head and tail 14131ae08745Sheppo */ 14141ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 14151ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 14161ae08745Sheppo if (rv != 0) { 14171ae08745Sheppo cmn_err(CE_WARN, 14181ae08745Sheppo "i_ldc_process_data_ACK: (0x%lx) cannot read qptrs\n", 14191ae08745Sheppo ldcp->id); 1420d10e4ef2Snarayan 1421d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 1422d10e4ef2Snarayan i_ldc_reset(ldcp); 1423d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1424d10e4ef2Snarayan return (ECONNRESET); 14251ae08745Sheppo } 14261ae08745Sheppo 14271ae08745Sheppo /* 14281ae08745Sheppo * loop from where the previous ACK location was to the 14291ae08745Sheppo * current head location. This is how far the HV has 14301ae08745Sheppo * actually send pkts. Pkts between head and tail are 14311ae08745Sheppo * yet to be sent by HV. 14321ae08745Sheppo */ 14331ae08745Sheppo tx_head = ldcp->tx_ackd_head; 14341ae08745Sheppo for (;;) { 14351ae08745Sheppo pkt = (ldc_msg_t *)(ldcp->tx_q_va + tx_head); 14361ae08745Sheppo tx_head = (tx_head + LDC_PACKET_SIZE) % 14371ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 14381ae08745Sheppo 14391ae08745Sheppo if (pkt->seqid == msg->ackid) { 14401ae08745Sheppo D2(ldcp->id, 14411ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) found packet\n", 14421ae08745Sheppo ldcp->id); 14431ae08745Sheppo ldcp->last_ack_rcd = msg->ackid; 14441ae08745Sheppo ldcp->tx_ackd_head = tx_head; 14451ae08745Sheppo break; 14461ae08745Sheppo } 14471ae08745Sheppo if (tx_head == ldcp->tx_head) { 14481ae08745Sheppo /* could not find packet */ 14491ae08745Sheppo DWARN(ldcp->id, 14501ae08745Sheppo "i_ldc_process_data_ACK: (0x%llx) invalid ACKid\n", 14511ae08745Sheppo ldcp->id); 1452d10e4ef2Snarayan 1453d10e4ef2Snarayan /* Reset the channel -- as we cannot continue */ 1454d10e4ef2Snarayan i_ldc_reset(ldcp); 1455d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1456d10e4ef2Snarayan return (ECONNRESET); 14571ae08745Sheppo } 14581ae08745Sheppo } 14591ae08745Sheppo 1460d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 14611ae08745Sheppo return (0); 14621ae08745Sheppo } 14631ae08745Sheppo 14641ae08745Sheppo /* 14651ae08745Sheppo * Process incoming control message 14661ae08745Sheppo * Return 0 - session can continue 14671ae08745Sheppo * EAGAIN - reprocess packet - state was changed 14681ae08745Sheppo * ECONNRESET - channel was reset 14691ae08745Sheppo */ 14701ae08745Sheppo static int 14711ae08745Sheppo i_ldc_ctrlmsg(ldc_chan_t *ldcp, ldc_msg_t *msg) 14721ae08745Sheppo { 14731ae08745Sheppo int rv = 0; 14741ae08745Sheppo 14751ae08745Sheppo switch (ldcp->tstate) { 14761ae08745Sheppo 14771ae08745Sheppo case TS_OPEN: 14781ae08745Sheppo case TS_READY: 14791ae08745Sheppo 14801ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 14811ae08745Sheppo case LDC_VER: 14821ae08745Sheppo /* process version message */ 14831ae08745Sheppo rv = i_ldc_process_VER(ldcp, msg); 14841ae08745Sheppo break; 14851ae08745Sheppo default: 14861ae08745Sheppo DWARN(ldcp->id, 14871ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 14881ae08745Sheppo "tstate=0x%x\n", ldcp->id, 14891ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 14901ae08745Sheppo break; 14911ae08745Sheppo } 14921ae08745Sheppo 14931ae08745Sheppo break; 14941ae08745Sheppo 14951ae08745Sheppo case TS_VREADY: 14961ae08745Sheppo 14971ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 14981ae08745Sheppo case LDC_VER: 14991ae08745Sheppo /* peer is redoing version negotiation */ 1500d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15011ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 15021ae08745Sheppo i_ldc_reset_state(ldcp); 1503d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15041ae08745Sheppo rv = EAGAIN; 15051ae08745Sheppo break; 15061ae08745Sheppo case LDC_RTS: 15071ae08745Sheppo /* process RTS message */ 15081ae08745Sheppo rv = i_ldc_process_RTS(ldcp, msg); 15091ae08745Sheppo break; 15101ae08745Sheppo case LDC_RTR: 15111ae08745Sheppo /* process RTR message */ 15121ae08745Sheppo rv = i_ldc_process_RTR(ldcp, msg); 15131ae08745Sheppo break; 15141ae08745Sheppo case LDC_RDX: 15151ae08745Sheppo /* process RDX message */ 15161ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 15171ae08745Sheppo break; 15181ae08745Sheppo default: 15191ae08745Sheppo DWARN(ldcp->id, 15201ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 15211ae08745Sheppo "tstate=0x%x\n", ldcp->id, 15221ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 15231ae08745Sheppo break; 15241ae08745Sheppo } 15251ae08745Sheppo 15261ae08745Sheppo break; 15271ae08745Sheppo 15281ae08745Sheppo case TS_UP: 15291ae08745Sheppo 15301ae08745Sheppo switch (msg->ctrl & LDC_CTRL_MASK) { 15311ae08745Sheppo case LDC_VER: 15321ae08745Sheppo DWARN(ldcp->id, 15331ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexpected VER " 15341ae08745Sheppo "- LDC reset\n", ldcp->id); 15351ae08745Sheppo /* peer is redoing version negotiation */ 1536d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 15371ae08745Sheppo (void) i_ldc_txq_reconf(ldcp); 15381ae08745Sheppo i_ldc_reset_state(ldcp); 1539d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 15401ae08745Sheppo rv = EAGAIN; 15411ae08745Sheppo break; 15421ae08745Sheppo 15431ae08745Sheppo case LDC_RDX: 15441ae08745Sheppo /* process RDX message */ 15451ae08745Sheppo rv = i_ldc_process_RDX(ldcp, msg); 15461ae08745Sheppo break; 15471ae08745Sheppo 15481ae08745Sheppo default: 15491ae08745Sheppo DWARN(ldcp->id, 15501ae08745Sheppo "i_ldc_ctrlmsg: (0x%llx) unexp ctrl 0x%x " 15511ae08745Sheppo "tstate=0x%x\n", ldcp->id, 15521ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK), ldcp->tstate); 15531ae08745Sheppo break; 15541ae08745Sheppo } 15551ae08745Sheppo } 15561ae08745Sheppo 15571ae08745Sheppo return (rv); 15581ae08745Sheppo } 15591ae08745Sheppo 15601ae08745Sheppo /* 15611ae08745Sheppo * Register channel with the channel nexus 15621ae08745Sheppo */ 15631ae08745Sheppo static int 15641ae08745Sheppo i_ldc_register_channel(ldc_chan_t *ldcp) 15651ae08745Sheppo { 15661ae08745Sheppo int rv = 0; 15671ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 15681ae08745Sheppo 15691ae08745Sheppo if (cinfo->dip == NULL) { 15701ae08745Sheppo DWARN(ldcp->id, 15711ae08745Sheppo "i_ldc_register_channel: cnex has not registered\n"); 15721ae08745Sheppo return (EAGAIN); 15731ae08745Sheppo } 15741ae08745Sheppo 15751ae08745Sheppo rv = cinfo->reg_chan(cinfo->dip, ldcp->id, ldcp->devclass); 15761ae08745Sheppo if (rv) { 15771ae08745Sheppo DWARN(ldcp->id, 15781ae08745Sheppo "i_ldc_register_channel: cannot register channel\n"); 15791ae08745Sheppo return (rv); 15801ae08745Sheppo } 15811ae08745Sheppo 15821ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR, 15831ae08745Sheppo i_ldc_tx_hdlr, ldcp, NULL); 15841ae08745Sheppo if (rv) { 15851ae08745Sheppo DWARN(ldcp->id, 15861ae08745Sheppo "i_ldc_register_channel: cannot add Tx interrupt\n"); 15871ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 15881ae08745Sheppo return (rv); 15891ae08745Sheppo } 15901ae08745Sheppo 15911ae08745Sheppo rv = cinfo->add_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR, 15921ae08745Sheppo i_ldc_rx_hdlr, ldcp, NULL); 15931ae08745Sheppo if (rv) { 15941ae08745Sheppo DWARN(ldcp->id, 15951ae08745Sheppo "i_ldc_register_channel: cannot add Rx interrupt\n"); 15961ae08745Sheppo (void) cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 15971ae08745Sheppo (void) cinfo->unreg_chan(cinfo->dip, ldcp->id); 15981ae08745Sheppo return (rv); 15991ae08745Sheppo } 16001ae08745Sheppo 16011ae08745Sheppo ldcp->tstate |= TS_CNEX_RDY; 16021ae08745Sheppo 16031ae08745Sheppo return (0); 16041ae08745Sheppo } 16051ae08745Sheppo 16061ae08745Sheppo /* 16071ae08745Sheppo * Unregister a channel with the channel nexus 16081ae08745Sheppo */ 16091ae08745Sheppo static int 16101ae08745Sheppo i_ldc_unregister_channel(ldc_chan_t *ldcp) 16111ae08745Sheppo { 16121ae08745Sheppo int rv = 0; 16131ae08745Sheppo ldc_cnex_t *cinfo = &ldcssp->cinfo; 16141ae08745Sheppo 16151ae08745Sheppo if (cinfo->dip == NULL) { 16161ae08745Sheppo DWARN(ldcp->id, 16171ae08745Sheppo "i_ldc_unregister_channel: cnex has not registered\n"); 16181ae08745Sheppo return (EAGAIN); 16191ae08745Sheppo } 16201ae08745Sheppo 16211ae08745Sheppo if (ldcp->tstate & TS_CNEX_RDY) { 16221ae08745Sheppo 1623d10e4ef2Snarayan /* Remove the Rx interrupt */ 16241ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_RX_INTR); 16251ae08745Sheppo if (rv) { 16261ae08745Sheppo DWARN(ldcp->id, 16271ae08745Sheppo "i_ldc_unregister_channel: err removing Rx intr\n"); 1628d10e4ef2Snarayan return (rv); 16291ae08745Sheppo } 1630d10e4ef2Snarayan 1631d10e4ef2Snarayan /* Remove the Tx interrupt */ 16321ae08745Sheppo rv = cinfo->rem_intr(cinfo->dip, ldcp->id, CNEX_TX_INTR); 16331ae08745Sheppo if (rv) { 16341ae08745Sheppo DWARN(ldcp->id, 16351ae08745Sheppo "i_ldc_unregister_channel: err removing Tx intr\n"); 1636d10e4ef2Snarayan return (rv); 16371ae08745Sheppo } 1638d10e4ef2Snarayan 1639d10e4ef2Snarayan /* Unregister the channel */ 16401ae08745Sheppo rv = cinfo->unreg_chan(ldcssp->cinfo.dip, ldcp->id); 16411ae08745Sheppo if (rv) { 16421ae08745Sheppo DWARN(ldcp->id, 16431ae08745Sheppo "i_ldc_unregister_channel: cannot unreg channel\n"); 1644d10e4ef2Snarayan return (rv); 16451ae08745Sheppo } 16461ae08745Sheppo 16471ae08745Sheppo ldcp->tstate &= ~TS_CNEX_RDY; 16481ae08745Sheppo } 16491ae08745Sheppo 16501ae08745Sheppo return (0); 16511ae08745Sheppo } 16521ae08745Sheppo 16531ae08745Sheppo 16541ae08745Sheppo /* 16551ae08745Sheppo * LDC transmit interrupt handler 16561ae08745Sheppo * triggered for chanel up/down/reset events 16571ae08745Sheppo * and Tx queue content changes 16581ae08745Sheppo */ 16591ae08745Sheppo static uint_t 16601ae08745Sheppo i_ldc_tx_hdlr(caddr_t arg1, caddr_t arg2) 16611ae08745Sheppo { 16621ae08745Sheppo _NOTE(ARGUNUSED(arg2)) 16631ae08745Sheppo 16641ae08745Sheppo int rv; 16651ae08745Sheppo ldc_chan_t *ldcp; 16661ae08745Sheppo boolean_t notify_client = B_FALSE; 16671ae08745Sheppo uint64_t notify_event = 0; 16681ae08745Sheppo 16691ae08745Sheppo /* Get the channel for which interrupt was received */ 16701ae08745Sheppo ASSERT(arg1 != NULL); 16711ae08745Sheppo ldcp = (ldc_chan_t *)arg1; 16721ae08745Sheppo 16731ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 16741ae08745Sheppo ldcp->id, ldcp); 16751ae08745Sheppo 16761ae08745Sheppo /* Lock channel */ 16771ae08745Sheppo mutex_enter(&ldcp->lock); 16781ae08745Sheppo 1679d10e4ef2Snarayan /* Obtain Tx lock */ 1680d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1681d10e4ef2Snarayan 1682*4bac2208Snarayan /* mark interrupt as pending */ 1683*4bac2208Snarayan ldcp->tx_intr_pending = B_TRUE; 1684*4bac2208Snarayan 16851ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, &ldcp->tx_head, &ldcp->tx_tail, 16861ae08745Sheppo &ldcp->link_state); 16871ae08745Sheppo if (rv) { 16881ae08745Sheppo cmn_err(CE_WARN, 16891ae08745Sheppo "i_ldc_tx_hdlr: (0x%lx) cannot read queue ptrs rv=0x%d\n", 16901ae08745Sheppo ldcp->id, rv); 1691*4bac2208Snarayan i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 1692d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 16931ae08745Sheppo mutex_exit(&ldcp->lock); 16941ae08745Sheppo return (DDI_INTR_CLAIMED); 16951ae08745Sheppo } 16961ae08745Sheppo 16971ae08745Sheppo /* 16981ae08745Sheppo * reset the channel state if the channel went down 16991ae08745Sheppo * (other side unconfigured queue) or channel was reset 17001ae08745Sheppo * (other side reconfigured its queue) 17011ae08745Sheppo */ 17021ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 17031ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link down\n", ldcp->id); 17041ae08745Sheppo i_ldc_reset(ldcp); 17051ae08745Sheppo notify_client = B_TRUE; 17061ae08745Sheppo notify_event = LDC_EVT_DOWN; 17071ae08745Sheppo } 17081ae08745Sheppo 17091ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_RESET) { 17101ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link reset\n", ldcp->id); 17111ae08745Sheppo i_ldc_reset(ldcp); 17121ae08745Sheppo notify_client = B_TRUE; 17131ae08745Sheppo notify_event = LDC_EVT_RESET; 17141ae08745Sheppo } 17151ae08745Sheppo 17161ae08745Sheppo if (ldcp->tstate == TS_OPEN && ldcp->link_state == LDC_CHANNEL_UP) { 17171ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: channel link up\n", ldcp->id); 17181ae08745Sheppo notify_client = B_TRUE; 17191ae08745Sheppo notify_event = LDC_EVT_RESET; 17201ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 17211ae08745Sheppo ldcp->status = LDC_READY; 17221ae08745Sheppo } 17231ae08745Sheppo 17241ae08745Sheppo /* if callbacks are disabled, do not notify */ 17251ae08745Sheppo if (!ldcp->cb_enabled) 17261ae08745Sheppo notify_client = B_FALSE; 17271ae08745Sheppo 17281ae08745Sheppo if (notify_client) 17291ae08745Sheppo ldcp->cb_inprogress = B_TRUE; 17301ae08745Sheppo 17311ae08745Sheppo /* Unlock channel */ 1732d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 17331ae08745Sheppo mutex_exit(&ldcp->lock); 17341ae08745Sheppo 17351ae08745Sheppo if (notify_client) { 17361ae08745Sheppo rv = ldcp->cb(notify_event, ldcp->cb_arg); 17371ae08745Sheppo if (rv) { 17381ae08745Sheppo DWARN(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) callback " 17391ae08745Sheppo "failure", ldcp->id); 17401ae08745Sheppo } 17411ae08745Sheppo mutex_enter(&ldcp->lock); 17421ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 17431ae08745Sheppo mutex_exit(&ldcp->lock); 17441ae08745Sheppo } 17451ae08745Sheppo 17461ae08745Sheppo mutex_enter(&ldcp->lock); 17471ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_TX_INTR); 17481ae08745Sheppo mutex_exit(&ldcp->lock); 17491ae08745Sheppo 17501ae08745Sheppo D1(ldcp->id, "i_ldc_tx_hdlr: (0x%llx) exiting handler", ldcp->id); 17511ae08745Sheppo 17521ae08745Sheppo return (DDI_INTR_CLAIMED); 17531ae08745Sheppo } 17541ae08745Sheppo 17551ae08745Sheppo /* 17561ae08745Sheppo * LDC receive interrupt handler 17571ae08745Sheppo * triggered for channel with data pending to read 17581ae08745Sheppo * i.e. Rx queue content changes 17591ae08745Sheppo */ 17601ae08745Sheppo static uint_t 17611ae08745Sheppo i_ldc_rx_hdlr(caddr_t arg1, caddr_t arg2) 17621ae08745Sheppo { 17631ae08745Sheppo _NOTE(ARGUNUSED(arg2)) 17641ae08745Sheppo 17651ae08745Sheppo int rv; 17661ae08745Sheppo uint64_t rx_head, rx_tail; 17671ae08745Sheppo ldc_msg_t *msg; 17681ae08745Sheppo ldc_chan_t *ldcp; 17691ae08745Sheppo boolean_t notify_client = B_FALSE; 17701ae08745Sheppo uint64_t notify_event = 0; 1771d10e4ef2Snarayan uint64_t first_fragment = 0; 17721ae08745Sheppo 17731ae08745Sheppo /* Get the channel for which interrupt was received */ 17741ae08745Sheppo if (arg1 == NULL) { 17751ae08745Sheppo cmn_err(CE_WARN, "i_ldc_rx_hdlr: invalid arg\n"); 17761ae08745Sheppo return (DDI_INTR_UNCLAIMED); 17771ae08745Sheppo } 17781ae08745Sheppo 17791ae08745Sheppo ldcp = (ldc_chan_t *)arg1; 17801ae08745Sheppo 17811ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) Received intr, ldcp=0x%p\n", 17821ae08745Sheppo ldcp->id, ldcp); 17831ae08745Sheppo 17841ae08745Sheppo /* Lock channel */ 17851ae08745Sheppo mutex_enter(&ldcp->lock); 17861ae08745Sheppo 17871ae08745Sheppo /* mark interrupt as pending */ 1788*4bac2208Snarayan ldcp->rx_intr_pending = B_TRUE; 17891ae08745Sheppo 17901ae08745Sheppo /* 17911ae08745Sheppo * Read packet(s) from the queue 17921ae08745Sheppo */ 17931ae08745Sheppo for (;;) { 17941ae08745Sheppo 17951ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 17961ae08745Sheppo &ldcp->link_state); 17971ae08745Sheppo if (rv) { 17981ae08745Sheppo cmn_err(CE_WARN, 17991ae08745Sheppo "i_ldc_rx_hdlr: (0x%lx) cannot read " 18001ae08745Sheppo "queue ptrs, rv=0x%d\n", ldcp->id, rv); 18011ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 18021ae08745Sheppo mutex_exit(&ldcp->lock); 18031ae08745Sheppo return (DDI_INTR_CLAIMED); 18041ae08745Sheppo } 18051ae08745Sheppo 18061ae08745Sheppo /* 18071ae08745Sheppo * reset the channel state if the channel went down 18081ae08745Sheppo * (other side unconfigured queue) or channel was reset 18091ae08745Sheppo * (other side reconfigured its queue 18101ae08745Sheppo */ 18111ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 18121ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: channel link down\n", 18131ae08745Sheppo ldcp->id); 1814d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 18151ae08745Sheppo i_ldc_reset(ldcp); 1816d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 18171ae08745Sheppo notify_client = B_TRUE; 18181ae08745Sheppo notify_event = LDC_EVT_DOWN; 18191ae08745Sheppo break; 18201ae08745Sheppo } 18211ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_RESET) { 18221ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: channel link reset\n", 18231ae08745Sheppo ldcp->id); 1824d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 18251ae08745Sheppo i_ldc_reset(ldcp); 1826d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 18271ae08745Sheppo notify_client = B_TRUE; 18281ae08745Sheppo notify_event = LDC_EVT_RESET; 18291ae08745Sheppo } 18301ae08745Sheppo 18311ae08745Sheppo if (ldcp->tstate == TS_OPEN && 18321ae08745Sheppo ldcp->link_state == LDC_CHANNEL_UP) { 18331ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: channel link up\n", 18341ae08745Sheppo ldcp->id); 18351ae08745Sheppo notify_client = B_TRUE; 18361ae08745Sheppo notify_event = LDC_EVT_RESET; 18371ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 18381ae08745Sheppo ldcp->status = LDC_READY; 18391ae08745Sheppo } 18401ae08745Sheppo 18411ae08745Sheppo if (rx_head == rx_tail) { 18421ae08745Sheppo D2(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) No packets\n", 18431ae08745Sheppo ldcp->id); 18441ae08745Sheppo break; 18451ae08745Sheppo } 18461ae08745Sheppo D2(ldcp->id, "i_ldc_rx_hdlr: head=0x%llx, tail=0x%llx\n", 18471ae08745Sheppo rx_head, rx_tail); 18481ae08745Sheppo DUMP_LDC_PKT(ldcp, "i_ldc_rx_hdlr rcd", 18491ae08745Sheppo ldcp->rx_q_va + rx_head); 18501ae08745Sheppo 18511ae08745Sheppo /* get the message */ 18521ae08745Sheppo msg = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 18531ae08745Sheppo 18541ae08745Sheppo /* if channel is in RAW mode or data pkt, notify and return */ 18551ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 18561ae08745Sheppo notify_client = B_TRUE; 18571ae08745Sheppo notify_event |= LDC_EVT_READ; 18581ae08745Sheppo break; 18591ae08745Sheppo } 18601ae08745Sheppo 18611ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 18621ae08745Sheppo 18631ae08745Sheppo /* discard packet if channel is not up */ 18641ae08745Sheppo if (ldcp->tstate != TS_UP) { 18651ae08745Sheppo 18661ae08745Sheppo /* move the head one position */ 18671ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 18681ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 18691ae08745Sheppo 18701ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, rx_head)) 18711ae08745Sheppo break; 18721ae08745Sheppo 18731ae08745Sheppo continue; 18741ae08745Sheppo } else { 18751ae08745Sheppo notify_client = B_TRUE; 18761ae08745Sheppo notify_event |= LDC_EVT_READ; 18771ae08745Sheppo break; 18781ae08745Sheppo } 18791ae08745Sheppo } 18801ae08745Sheppo 18811ae08745Sheppo /* Check the sequence ID for the message received */ 18821ae08745Sheppo if ((rv = i_ldc_check_seqid(ldcp, msg)) != 0) { 18831ae08745Sheppo 18841ae08745Sheppo DWARN(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) seqid error, " 18851ae08745Sheppo "q_ptrs=0x%lx,0x%lx", ldcp->id, rx_head, rx_tail); 18861ae08745Sheppo 18871ae08745Sheppo /* Reset last_msg_rcd to start of message */ 1888d10e4ef2Snarayan if (first_fragment != 0) { 1889d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 1890d10e4ef2Snarayan first_fragment = 0; 18911ae08745Sheppo } 1892d10e4ef2Snarayan 18931ae08745Sheppo /* 18941ae08745Sheppo * Send a NACK due to seqid mismatch 18951ae08745Sheppo */ 18961ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_CTRL, LDC_NACK, 18971ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 18981ae08745Sheppo 18991ae08745Sheppo if (rv) { 19001ae08745Sheppo cmn_err(CE_NOTE, 19011ae08745Sheppo "i_ldc_rx_hdlr: (0x%lx) err sending " 19021ae08745Sheppo "CTRL/NACK msg\n", ldcp->id); 1903d10e4ef2Snarayan 1904d10e4ef2Snarayan /* if cannot send NACK - reset channel */ 1905d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 1906d10e4ef2Snarayan i_ldc_reset(ldcp); 1907d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 1908d10e4ef2Snarayan rv = ECONNRESET; 1909d10e4ef2Snarayan break; 19101ae08745Sheppo } 19111ae08745Sheppo 19121ae08745Sheppo /* purge receive queue */ 19131ae08745Sheppo (void) i_ldc_set_rx_head(ldcp, rx_tail); 19141ae08745Sheppo break; 19151ae08745Sheppo } 19161ae08745Sheppo 19171ae08745Sheppo /* record the message ID */ 19181ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 19191ae08745Sheppo 19201ae08745Sheppo /* process control messages */ 19211ae08745Sheppo if (msg->type & LDC_CTRL) { 19221ae08745Sheppo /* save current internal state */ 19231ae08745Sheppo uint64_t tstate = ldcp->tstate; 19241ae08745Sheppo 19251ae08745Sheppo rv = i_ldc_ctrlmsg(ldcp, msg); 19261ae08745Sheppo if (rv == EAGAIN) { 19271ae08745Sheppo /* re-process pkt - state was adjusted */ 19281ae08745Sheppo continue; 19291ae08745Sheppo } 19301ae08745Sheppo if (rv == ECONNRESET) { 19311ae08745Sheppo notify_client = B_TRUE; 19321ae08745Sheppo notify_event = LDC_EVT_RESET; 19331ae08745Sheppo break; 19341ae08745Sheppo } 19351ae08745Sheppo 19361ae08745Sheppo /* 19371ae08745Sheppo * control message processing was successful 19381ae08745Sheppo * channel transitioned to ready for communication 19391ae08745Sheppo */ 19401ae08745Sheppo if (rv == 0 && ldcp->tstate == TS_UP && 19411ae08745Sheppo tstate != ldcp->tstate) { 19421ae08745Sheppo notify_client = B_TRUE; 19431ae08745Sheppo notify_event = LDC_EVT_UP; 19441ae08745Sheppo } 19451ae08745Sheppo } 19461ae08745Sheppo 19471ae08745Sheppo /* process data ACKs */ 19481ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 1949d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 1950d10e4ef2Snarayan notify_client = B_TRUE; 1951d10e4ef2Snarayan notify_event = LDC_EVT_RESET; 1952d10e4ef2Snarayan break; 1953d10e4ef2Snarayan } 19541ae08745Sheppo } 19551ae08745Sheppo 19561ae08745Sheppo /* move the head one position */ 19571ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) % 19581ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT); 19590a55fbb7Slm66018 if (rv = i_ldc_set_rx_head(ldcp, rx_head)) { 19600a55fbb7Slm66018 notify_client = B_TRUE; 19610a55fbb7Slm66018 notify_event = LDC_EVT_RESET; 19621ae08745Sheppo break; 19630a55fbb7Slm66018 } 19641ae08745Sheppo 19651ae08745Sheppo } /* for */ 19661ae08745Sheppo 19671ae08745Sheppo /* if callbacks are disabled, do not notify */ 19681ae08745Sheppo if (!ldcp->cb_enabled) 19691ae08745Sheppo notify_client = B_FALSE; 19701ae08745Sheppo 19711ae08745Sheppo if (notify_client) 19721ae08745Sheppo ldcp->cb_inprogress = B_TRUE; 19731ae08745Sheppo 19741ae08745Sheppo /* Unlock channel */ 19751ae08745Sheppo mutex_exit(&ldcp->lock); 19761ae08745Sheppo 19771ae08745Sheppo if (notify_client) { 19781ae08745Sheppo rv = ldcp->cb(notify_event, ldcp->cb_arg); 19791ae08745Sheppo if (rv) { 19801ae08745Sheppo DWARN(ldcp->id, 19811ae08745Sheppo "i_ldc_rx_hdlr: (0x%llx) callback failure", 19821ae08745Sheppo ldcp->id); 19831ae08745Sheppo } 19841ae08745Sheppo mutex_enter(&ldcp->lock); 19851ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 19861ae08745Sheppo mutex_exit(&ldcp->lock); 19871ae08745Sheppo } 19881ae08745Sheppo 19891ae08745Sheppo mutex_enter(&ldcp->lock); 19901ae08745Sheppo 19911ae08745Sheppo /* 19921ae08745Sheppo * If there are data packets in the queue, the ldc_read will 19931ae08745Sheppo * clear interrupts after draining the queue, else clear interrupts 19941ae08745Sheppo */ 19951ae08745Sheppo if ((notify_event & LDC_EVT_READ) == 0) { 19961ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 19971ae08745Sheppo } 19981ae08745Sheppo 19991ae08745Sheppo mutex_exit(&ldcp->lock); 20001ae08745Sheppo 20011ae08745Sheppo D1(ldcp->id, "i_ldc_rx_hdlr: (0x%llx) exiting handler", ldcp->id); 20021ae08745Sheppo return (DDI_INTR_CLAIMED); 20031ae08745Sheppo } 20041ae08745Sheppo 20051ae08745Sheppo 20061ae08745Sheppo /* -------------------------------------------------------------------------- */ 20071ae08745Sheppo 20081ae08745Sheppo /* 20091ae08745Sheppo * LDC API functions 20101ae08745Sheppo */ 20111ae08745Sheppo 20121ae08745Sheppo /* 20131ae08745Sheppo * Initialize the channel. Allocate internal structure and memory for 20141ae08745Sheppo * TX/RX queues, and initialize locks. 20151ae08745Sheppo */ 20161ae08745Sheppo int 20171ae08745Sheppo ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle) 20181ae08745Sheppo { 20191ae08745Sheppo ldc_chan_t *ldcp; 20201ae08745Sheppo int rv, exit_val; 20211ae08745Sheppo uint64_t ra_base, nentries; 2022e1ebb9ecSlm66018 uint64_t qlen; 20231ae08745Sheppo 20241ae08745Sheppo exit_val = EINVAL; /* guarantee an error if exit on failure */ 20251ae08745Sheppo 20261ae08745Sheppo if (attr == NULL) { 20271ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid attr\n", id); 20281ae08745Sheppo return (EINVAL); 20291ae08745Sheppo } 20301ae08745Sheppo if (handle == NULL) { 20311ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid handle\n", id); 20321ae08745Sheppo return (EINVAL); 20331ae08745Sheppo } 20341ae08745Sheppo 20351ae08745Sheppo /* check if channel is valid */ 20361ae08745Sheppo rv = hv_ldc_tx_qinfo(id, &ra_base, &nentries); 20371ae08745Sheppo if (rv == H_ECHANNEL) { 20381ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) invalid channel id\n", id); 20391ae08745Sheppo return (EINVAL); 20401ae08745Sheppo } 20411ae08745Sheppo 20421ae08745Sheppo /* check if the channel has already been initialized */ 20431ae08745Sheppo mutex_enter(&ldcssp->lock); 20441ae08745Sheppo ldcp = ldcssp->chan_list; 20451ae08745Sheppo while (ldcp != NULL) { 20461ae08745Sheppo if (ldcp->id == id) { 20471ae08745Sheppo DWARN(id, "ldc_init: (0x%llx) already initialized\n", 20481ae08745Sheppo id); 20491ae08745Sheppo mutex_exit(&ldcssp->lock); 20501ae08745Sheppo return (EADDRINUSE); 20511ae08745Sheppo } 20521ae08745Sheppo ldcp = ldcp->next; 20531ae08745Sheppo } 20541ae08745Sheppo mutex_exit(&ldcssp->lock); 20551ae08745Sheppo 20561ae08745Sheppo ASSERT(ldcp == NULL); 20571ae08745Sheppo 20581ae08745Sheppo *handle = 0; 20591ae08745Sheppo 20601ae08745Sheppo /* Allocate an ldcp structure */ 20611ae08745Sheppo ldcp = kmem_zalloc(sizeof (ldc_chan_t), KM_SLEEP); 20621ae08745Sheppo 2063d10e4ef2Snarayan /* 2064d10e4ef2Snarayan * Initialize the channel and Tx lock 2065d10e4ef2Snarayan * 2066d10e4ef2Snarayan * The channel 'lock' protects the entire channel and 2067d10e4ef2Snarayan * should be acquired before initializing, resetting, 2068d10e4ef2Snarayan * destroying or reading from a channel. 2069d10e4ef2Snarayan * 2070d10e4ef2Snarayan * The 'tx_lock' should be acquired prior to transmitting 2071d10e4ef2Snarayan * data over the channel. The lock should also be acquired 2072d10e4ef2Snarayan * prior to channel reconfiguration (in order to prevent 2073d10e4ef2Snarayan * concurrent writes). 2074d10e4ef2Snarayan * 2075d10e4ef2Snarayan * ORDERING: When both locks are being acquired, to prevent 2076d10e4ef2Snarayan * deadlocks, the channel lock should be always acquired prior 2077d10e4ef2Snarayan * to the tx_lock. 2078d10e4ef2Snarayan */ 20791ae08745Sheppo mutex_init(&ldcp->lock, NULL, MUTEX_DRIVER, NULL); 2080d10e4ef2Snarayan mutex_init(&ldcp->tx_lock, NULL, MUTEX_DRIVER, NULL); 20811ae08745Sheppo 20821ae08745Sheppo /* Initialize the channel */ 20831ae08745Sheppo ldcp->id = id; 20841ae08745Sheppo ldcp->cb = NULL; 20851ae08745Sheppo ldcp->cb_arg = NULL; 20861ae08745Sheppo ldcp->cb_inprogress = B_FALSE; 20871ae08745Sheppo ldcp->cb_enabled = B_FALSE; 20881ae08745Sheppo ldcp->next = NULL; 20891ae08745Sheppo 20901ae08745Sheppo /* Read attributes */ 20911ae08745Sheppo ldcp->mode = attr->mode; 20921ae08745Sheppo ldcp->devclass = attr->devclass; 20931ae08745Sheppo ldcp->devinst = attr->instance; 2094e1ebb9ecSlm66018 ldcp->mtu = (attr->mtu > 0) ? attr->mtu : LDC_DEFAULT_MTU; 20951ae08745Sheppo 20961ae08745Sheppo D1(ldcp->id, 20971ae08745Sheppo "ldc_init: (0x%llx) channel attributes, class=0x%x, " 2098e1ebb9ecSlm66018 "instance=0x%llx, mode=%d, mtu=%d\n", 2099e1ebb9ecSlm66018 ldcp->id, ldcp->devclass, ldcp->devinst, ldcp->mode, ldcp->mtu); 21001ae08745Sheppo 21011ae08745Sheppo ldcp->next_vidx = 0; 21021ae08745Sheppo ldcp->tstate = 0; 21031ae08745Sheppo ldcp->hstate = 0; 21041ae08745Sheppo ldcp->last_msg_snt = LDC_INIT_SEQID; 21051ae08745Sheppo ldcp->last_ack_rcd = 0; 21061ae08745Sheppo ldcp->last_msg_rcd = 0; 21071ae08745Sheppo 21081ae08745Sheppo ldcp->stream_bufferp = NULL; 21091ae08745Sheppo ldcp->exp_dring_list = NULL; 21101ae08745Sheppo ldcp->imp_dring_list = NULL; 21111ae08745Sheppo ldcp->mhdl_list = NULL; 21121ae08745Sheppo 21131ae08745Sheppo /* Initialize payload size depending on whether channel is reliable */ 21141ae08745Sheppo switch (ldcp->mode) { 21151ae08745Sheppo case LDC_MODE_RAW: 21161ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RAW; 21171ae08745Sheppo ldcp->read_p = i_ldc_read_raw; 21181ae08745Sheppo ldcp->write_p = i_ldc_write_raw; 21191ae08745Sheppo break; 21201ae08745Sheppo case LDC_MODE_UNRELIABLE: 21211ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_UNRELIABLE; 21221ae08745Sheppo ldcp->read_p = i_ldc_read_packet; 21231ae08745Sheppo ldcp->write_p = i_ldc_write_packet; 21241ae08745Sheppo break; 21251ae08745Sheppo case LDC_MODE_RELIABLE: 21261ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE; 21271ae08745Sheppo ldcp->read_p = i_ldc_read_packet; 21281ae08745Sheppo ldcp->write_p = i_ldc_write_packet; 21291ae08745Sheppo break; 21301ae08745Sheppo case LDC_MODE_STREAM: 21311ae08745Sheppo ldcp->pkt_payload = LDC_PAYLOAD_SIZE_RELIABLE; 21321ae08745Sheppo 21331ae08745Sheppo ldcp->stream_remains = 0; 21341ae08745Sheppo ldcp->stream_offset = 0; 21351ae08745Sheppo ldcp->stream_bufferp = kmem_alloc(ldcp->mtu, KM_SLEEP); 21361ae08745Sheppo ldcp->read_p = i_ldc_read_stream; 21371ae08745Sheppo ldcp->write_p = i_ldc_write_stream; 21381ae08745Sheppo break; 21391ae08745Sheppo default: 21401ae08745Sheppo exit_val = EINVAL; 21411ae08745Sheppo goto cleanup_on_exit; 21421ae08745Sheppo } 21431ae08745Sheppo 2144e1ebb9ecSlm66018 /* 2145e1ebb9ecSlm66018 * qlen is (mtu * ldc_mtu_msgs) / pkt_payload. If this 2146e1ebb9ecSlm66018 * value is smaller than default length of ldc_queue_entries, 2147e1ebb9ecSlm66018 * qlen is set to ldc_queue_entries.. 2148e1ebb9ecSlm66018 */ 2149e1ebb9ecSlm66018 qlen = (ldcp->mtu * ldc_mtu_msgs) / ldcp->pkt_payload; 2150e1ebb9ecSlm66018 ldcp->rx_q_entries = 2151e1ebb9ecSlm66018 (qlen < ldc_queue_entries) ? ldc_queue_entries : qlen; 2152e1ebb9ecSlm66018 ldcp->tx_q_entries = ldcp->rx_q_entries; 2153e1ebb9ecSlm66018 2154e1ebb9ecSlm66018 D1(ldcp->id, "ldc_init: queue length = 0x%llx\n", qlen); 2155e1ebb9ecSlm66018 21561ae08745Sheppo /* Create a transmit queue */ 21571ae08745Sheppo ldcp->tx_q_va = (uint64_t) 21581ae08745Sheppo contig_mem_alloc(ldcp->tx_q_entries << LDC_PACKET_SHIFT); 21591ae08745Sheppo if (ldcp->tx_q_va == NULL) { 21601ae08745Sheppo cmn_err(CE_WARN, 21611ae08745Sheppo "ldc_init: (0x%lx) TX queue allocation failed\n", 21621ae08745Sheppo ldcp->id); 21631ae08745Sheppo exit_val = ENOMEM; 21641ae08745Sheppo goto cleanup_on_exit; 21651ae08745Sheppo } 21661ae08745Sheppo ldcp->tx_q_ra = va_to_pa((caddr_t)ldcp->tx_q_va); 21671ae08745Sheppo 21681ae08745Sheppo D2(ldcp->id, "ldc_init: txq_va=0x%llx, txq_ra=0x%llx, entries=0x%llx\n", 21691ae08745Sheppo ldcp->tx_q_va, ldcp->tx_q_ra, ldcp->tx_q_entries); 21701ae08745Sheppo 21711ae08745Sheppo ldcp->tstate |= TS_TXQ_RDY; 21721ae08745Sheppo 21731ae08745Sheppo /* Create a receive queue */ 21741ae08745Sheppo ldcp->rx_q_va = (uint64_t) 21751ae08745Sheppo contig_mem_alloc(ldcp->rx_q_entries << LDC_PACKET_SHIFT); 21761ae08745Sheppo if (ldcp->rx_q_va == NULL) { 21771ae08745Sheppo cmn_err(CE_WARN, 21781ae08745Sheppo "ldc_init: (0x%lx) RX queue allocation failed\n", 21791ae08745Sheppo ldcp->id); 21801ae08745Sheppo exit_val = ENOMEM; 21811ae08745Sheppo goto cleanup_on_exit; 21821ae08745Sheppo } 21831ae08745Sheppo ldcp->rx_q_ra = va_to_pa((caddr_t)ldcp->rx_q_va); 21841ae08745Sheppo 21851ae08745Sheppo D2(ldcp->id, "ldc_init: rxq_va=0x%llx, rxq_ra=0x%llx, entries=0x%llx\n", 21861ae08745Sheppo ldcp->rx_q_va, ldcp->rx_q_ra, ldcp->rx_q_entries); 21871ae08745Sheppo 21881ae08745Sheppo ldcp->tstate |= TS_RXQ_RDY; 21891ae08745Sheppo 21901ae08745Sheppo /* Init descriptor ring and memory handle list lock */ 21911ae08745Sheppo mutex_init(&ldcp->exp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 21921ae08745Sheppo mutex_init(&ldcp->imp_dlist_lock, NULL, MUTEX_DRIVER, NULL); 21931ae08745Sheppo mutex_init(&ldcp->mlist_lock, NULL, MUTEX_DRIVER, NULL); 21941ae08745Sheppo 21951ae08745Sheppo /* mark status as INITialized */ 21961ae08745Sheppo ldcp->status = LDC_INIT; 21971ae08745Sheppo 21981ae08745Sheppo /* Add to channel list */ 21991ae08745Sheppo mutex_enter(&ldcssp->lock); 22001ae08745Sheppo ldcp->next = ldcssp->chan_list; 22011ae08745Sheppo ldcssp->chan_list = ldcp; 22021ae08745Sheppo ldcssp->channel_count++; 22031ae08745Sheppo mutex_exit(&ldcssp->lock); 22041ae08745Sheppo 22051ae08745Sheppo /* set the handle */ 22061ae08745Sheppo *handle = (ldc_handle_t)ldcp; 22071ae08745Sheppo 22081ae08745Sheppo D1(ldcp->id, "ldc_init: (0x%llx) channel initialized\n", ldcp->id); 22091ae08745Sheppo 22101ae08745Sheppo return (0); 22111ae08745Sheppo 22121ae08745Sheppo cleanup_on_exit: 22131ae08745Sheppo 22141ae08745Sheppo if (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_bufferp) 22151ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 22161ae08745Sheppo 22171ae08745Sheppo if (ldcp->tstate & TS_TXQ_RDY) 22181ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 22191ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 22201ae08745Sheppo 22211ae08745Sheppo if (ldcp->tstate & TS_RXQ_RDY) 22221ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 22231ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 22241ae08745Sheppo 2225d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 22261ae08745Sheppo mutex_destroy(&ldcp->lock); 22271ae08745Sheppo 22281ae08745Sheppo if (ldcp) 22291ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 22301ae08745Sheppo 22311ae08745Sheppo return (exit_val); 22321ae08745Sheppo } 22331ae08745Sheppo 22341ae08745Sheppo /* 22351ae08745Sheppo * Finalizes the LDC connection. It will return EBUSY if the 22361ae08745Sheppo * channel is open. A ldc_close() has to be done prior to 22371ae08745Sheppo * a ldc_fini operation. It frees TX/RX queues, associated 22381ae08745Sheppo * with the channel 22391ae08745Sheppo */ 22401ae08745Sheppo int 22411ae08745Sheppo ldc_fini(ldc_handle_t handle) 22421ae08745Sheppo { 22431ae08745Sheppo ldc_chan_t *ldcp; 22441ae08745Sheppo ldc_chan_t *tmp_ldcp; 22451ae08745Sheppo uint64_t id; 22461ae08745Sheppo 22471ae08745Sheppo if (handle == NULL) { 22481ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel handle\n"); 22491ae08745Sheppo return (EINVAL); 22501ae08745Sheppo } 22511ae08745Sheppo ldcp = (ldc_chan_t *)handle; 22521ae08745Sheppo id = ldcp->id; 22531ae08745Sheppo 22541ae08745Sheppo mutex_enter(&ldcp->lock); 22551ae08745Sheppo 22561ae08745Sheppo if (ldcp->tstate > TS_INIT) { 22571ae08745Sheppo DWARN(ldcp->id, "ldc_fini: (0x%llx) channel is open\n", 22581ae08745Sheppo ldcp->id); 22591ae08745Sheppo mutex_exit(&ldcp->lock); 22601ae08745Sheppo return (EBUSY); 22611ae08745Sheppo } 22621ae08745Sheppo 22631ae08745Sheppo /* Remove from the channel list */ 22641ae08745Sheppo mutex_enter(&ldcssp->lock); 22651ae08745Sheppo tmp_ldcp = ldcssp->chan_list; 22661ae08745Sheppo if (tmp_ldcp == ldcp) { 22671ae08745Sheppo ldcssp->chan_list = ldcp->next; 22681ae08745Sheppo ldcp->next = NULL; 22691ae08745Sheppo } else { 22701ae08745Sheppo while (tmp_ldcp != NULL) { 22711ae08745Sheppo if (tmp_ldcp->next == ldcp) { 22721ae08745Sheppo tmp_ldcp->next = ldcp->next; 22731ae08745Sheppo ldcp->next = NULL; 22741ae08745Sheppo break; 22751ae08745Sheppo } 22761ae08745Sheppo tmp_ldcp = tmp_ldcp->next; 22771ae08745Sheppo } 22781ae08745Sheppo if (tmp_ldcp == NULL) { 22791ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_fini: invalid channel hdl\n"); 22801ae08745Sheppo mutex_exit(&ldcssp->lock); 22811ae08745Sheppo mutex_exit(&ldcp->lock); 22821ae08745Sheppo return (EINVAL); 22831ae08745Sheppo } 22841ae08745Sheppo } 22851ae08745Sheppo 22861ae08745Sheppo ldcssp->channel_count--; 22871ae08745Sheppo 22881ae08745Sheppo mutex_exit(&ldcssp->lock); 22891ae08745Sheppo 22901ae08745Sheppo /* Free the map table for this channel */ 22911ae08745Sheppo if (ldcp->mtbl) { 22921ae08745Sheppo (void) hv_ldc_set_map_table(ldcp->id, NULL, NULL); 22931ae08745Sheppo contig_mem_free(ldcp->mtbl->table, ldcp->mtbl->size); 22941ae08745Sheppo mutex_destroy(&ldcp->mtbl->lock); 22951ae08745Sheppo kmem_free(ldcp->mtbl, sizeof (ldc_mtbl_t)); 22961ae08745Sheppo } 22971ae08745Sheppo 22981ae08745Sheppo /* Destroy descriptor ring and memory handle list lock */ 22991ae08745Sheppo mutex_destroy(&ldcp->exp_dlist_lock); 23001ae08745Sheppo mutex_destroy(&ldcp->imp_dlist_lock); 23011ae08745Sheppo mutex_destroy(&ldcp->mlist_lock); 23021ae08745Sheppo 23031ae08745Sheppo /* Free the stream buffer for STREAM_MODE */ 23041ae08745Sheppo if (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_bufferp) 23051ae08745Sheppo kmem_free(ldcp->stream_bufferp, ldcp->mtu); 23061ae08745Sheppo 23071ae08745Sheppo /* Free the RX queue */ 23081ae08745Sheppo contig_mem_free((caddr_t)ldcp->rx_q_va, 23091ae08745Sheppo (ldcp->rx_q_entries << LDC_PACKET_SHIFT)); 23101ae08745Sheppo ldcp->tstate &= ~TS_RXQ_RDY; 23111ae08745Sheppo 23121ae08745Sheppo /* Free the TX queue */ 23131ae08745Sheppo contig_mem_free((caddr_t)ldcp->tx_q_va, 23141ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 23151ae08745Sheppo ldcp->tstate &= ~TS_TXQ_RDY; 23161ae08745Sheppo 23171ae08745Sheppo 23181ae08745Sheppo mutex_exit(&ldcp->lock); 23191ae08745Sheppo 23201ae08745Sheppo /* Destroy mutex */ 2321d10e4ef2Snarayan mutex_destroy(&ldcp->tx_lock); 23221ae08745Sheppo mutex_destroy(&ldcp->lock); 23231ae08745Sheppo 23241ae08745Sheppo /* free channel structure */ 23251ae08745Sheppo kmem_free(ldcp, sizeof (ldc_chan_t)); 23261ae08745Sheppo 23271ae08745Sheppo D1(id, "ldc_fini: (0x%llx) channel finalized\n", id); 23281ae08745Sheppo 23291ae08745Sheppo return (0); 23301ae08745Sheppo } 23311ae08745Sheppo 23321ae08745Sheppo /* 23331ae08745Sheppo * Open the LDC channel for use. It registers the TX/RX queues 23341ae08745Sheppo * with the Hypervisor. It also specifies the interrupt number 23351ae08745Sheppo * and target CPU for this channel 23361ae08745Sheppo */ 23371ae08745Sheppo int 23381ae08745Sheppo ldc_open(ldc_handle_t handle) 23391ae08745Sheppo { 23401ae08745Sheppo ldc_chan_t *ldcp; 23411ae08745Sheppo int rv; 23421ae08745Sheppo 23431ae08745Sheppo if (handle == NULL) { 23441ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_open: invalid channel handle\n"); 23451ae08745Sheppo return (EINVAL); 23461ae08745Sheppo } 23471ae08745Sheppo 23481ae08745Sheppo ldcp = (ldc_chan_t *)handle; 23491ae08745Sheppo 23501ae08745Sheppo mutex_enter(&ldcp->lock); 23511ae08745Sheppo 23521ae08745Sheppo if (ldcp->tstate < TS_INIT) { 23531ae08745Sheppo DWARN(ldcp->id, 23541ae08745Sheppo "ldc_open: (0x%llx) channel not initialized\n", ldcp->id); 23551ae08745Sheppo mutex_exit(&ldcp->lock); 23561ae08745Sheppo return (EFAULT); 23571ae08745Sheppo } 23581ae08745Sheppo if (ldcp->tstate >= TS_OPEN) { 23591ae08745Sheppo DWARN(ldcp->id, 23601ae08745Sheppo "ldc_open: (0x%llx) channel is already open\n", ldcp->id); 23611ae08745Sheppo mutex_exit(&ldcp->lock); 23621ae08745Sheppo return (EFAULT); 23631ae08745Sheppo } 23641ae08745Sheppo 23651ae08745Sheppo /* 23661ae08745Sheppo * Unregister/Register the tx queue with the hypervisor 23671ae08745Sheppo */ 23681ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 23691ae08745Sheppo if (rv) { 23701ae08745Sheppo cmn_err(CE_WARN, 23711ae08745Sheppo "ldc_open: (0x%lx) channel tx queue unconf failed\n", 23721ae08745Sheppo ldcp->id); 23731ae08745Sheppo mutex_exit(&ldcp->lock); 23741ae08745Sheppo return (EIO); 23751ae08745Sheppo } 23761ae08745Sheppo 23771ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, ldcp->tx_q_ra, ldcp->tx_q_entries); 23781ae08745Sheppo if (rv) { 23791ae08745Sheppo cmn_err(CE_WARN, 23801ae08745Sheppo "ldc_open: (0x%lx) channel tx queue conf failed\n", 23811ae08745Sheppo ldcp->id); 23821ae08745Sheppo mutex_exit(&ldcp->lock); 23831ae08745Sheppo return (EIO); 23841ae08745Sheppo } 23851ae08745Sheppo 23861ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered tx queue with LDC\n", 23871ae08745Sheppo ldcp->id); 23881ae08745Sheppo 23891ae08745Sheppo /* 23901ae08745Sheppo * Unregister/Register the rx queue with the hypervisor 23911ae08745Sheppo */ 23921ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 23931ae08745Sheppo if (rv) { 23941ae08745Sheppo cmn_err(CE_WARN, 23951ae08745Sheppo "ldc_open: (0x%lx) channel rx queue unconf failed\n", 23961ae08745Sheppo ldcp->id); 23971ae08745Sheppo mutex_exit(&ldcp->lock); 23981ae08745Sheppo return (EIO); 23991ae08745Sheppo } 24001ae08745Sheppo 24011ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, ldcp->rx_q_ra, ldcp->rx_q_entries); 24021ae08745Sheppo if (rv) { 24031ae08745Sheppo cmn_err(CE_WARN, 24041ae08745Sheppo "ldc_open: (0x%lx) channel rx queue conf failed\n", 24051ae08745Sheppo ldcp->id); 24061ae08745Sheppo mutex_exit(&ldcp->lock); 24071ae08745Sheppo return (EIO); 24081ae08745Sheppo } 24091ae08745Sheppo 24101ae08745Sheppo D2(ldcp->id, "ldc_open: (0x%llx) registered rx queue with LDC\n", 24111ae08745Sheppo ldcp->id); 24121ae08745Sheppo 24131ae08745Sheppo ldcp->tstate |= TS_QCONF_RDY; 24141ae08745Sheppo 24151ae08745Sheppo /* Register the channel with the channel nexus */ 24161ae08745Sheppo rv = i_ldc_register_channel(ldcp); 24171ae08745Sheppo if (rv && rv != EAGAIN) { 24181ae08745Sheppo cmn_err(CE_WARN, 24191ae08745Sheppo "ldc_open: (0x%lx) channel register failed\n", ldcp->id); 24201ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 24211ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 24221ae08745Sheppo mutex_exit(&ldcp->lock); 24231ae08745Sheppo return (EIO); 24241ae08745Sheppo } 24251ae08745Sheppo 24261ae08745Sheppo /* mark channel in OPEN state */ 24271ae08745Sheppo ldcp->status = LDC_OPEN; 24281ae08745Sheppo 24291ae08745Sheppo /* Read channel state */ 24301ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 24311ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 24321ae08745Sheppo if (rv) { 24331ae08745Sheppo cmn_err(CE_WARN, 24341ae08745Sheppo "ldc_open: (0x%lx) cannot read channel state\n", 24351ae08745Sheppo ldcp->id); 24361ae08745Sheppo (void) i_ldc_unregister_channel(ldcp); 24371ae08745Sheppo (void) hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 24381ae08745Sheppo (void) hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 24391ae08745Sheppo mutex_exit(&ldcp->lock); 24401ae08745Sheppo return (EIO); 24411ae08745Sheppo } 24421ae08745Sheppo 24431ae08745Sheppo /* 24441ae08745Sheppo * set the ACKd head to current head location for reliable & 24451ae08745Sheppo * streaming mode 24461ae08745Sheppo */ 24471ae08745Sheppo ldcp->tx_ackd_head = ldcp->tx_head; 24481ae08745Sheppo 24491ae08745Sheppo /* mark channel ready if HV report link is UP (peer alloc'd Rx queue) */ 24501ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_UP || 24511ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 24521ae08745Sheppo ldcp->tstate |= TS_LINK_READY; 24531ae08745Sheppo ldcp->status = LDC_READY; 24541ae08745Sheppo } 24551ae08745Sheppo 24561ae08745Sheppo /* 24571ae08745Sheppo * if channel is being opened in RAW mode - no handshake is needed 24581ae08745Sheppo * switch the channel READY and UP state 24591ae08745Sheppo */ 24601ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW) { 24611ae08745Sheppo ldcp->tstate = TS_UP; /* set bits associated with LDC UP */ 24621ae08745Sheppo ldcp->status = LDC_UP; 24631ae08745Sheppo } 24641ae08745Sheppo 24651ae08745Sheppo mutex_exit(&ldcp->lock); 24661ae08745Sheppo 24671ae08745Sheppo /* 24681ae08745Sheppo * Increment number of open channels 24691ae08745Sheppo */ 24701ae08745Sheppo mutex_enter(&ldcssp->lock); 24711ae08745Sheppo ldcssp->channels_open++; 24721ae08745Sheppo mutex_exit(&ldcssp->lock); 24731ae08745Sheppo 24741ae08745Sheppo D1(ldcp->id, 24751ae08745Sheppo "ldc_open: (0x%llx) channel (0x%p) open for use (tstate=0x%x)\n", 24761ae08745Sheppo ldcp->id, ldcp, ldcp->tstate); 24771ae08745Sheppo 24781ae08745Sheppo return (0); 24791ae08745Sheppo } 24801ae08745Sheppo 24811ae08745Sheppo /* 24821ae08745Sheppo * Close the LDC connection. It will return EBUSY if there 24831ae08745Sheppo * are memory segments or descriptor rings either bound to or 24841ae08745Sheppo * mapped over the channel 24851ae08745Sheppo */ 24861ae08745Sheppo int 24871ae08745Sheppo ldc_close(ldc_handle_t handle) 24881ae08745Sheppo { 24891ae08745Sheppo ldc_chan_t *ldcp; 2490d10e4ef2Snarayan int rv = 0, retries = 0; 24911ae08745Sheppo boolean_t chk_done = B_FALSE; 24921ae08745Sheppo 24931ae08745Sheppo if (handle == NULL) { 24941ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_close: invalid channel handle\n"); 24951ae08745Sheppo return (EINVAL); 24961ae08745Sheppo } 24971ae08745Sheppo ldcp = (ldc_chan_t *)handle; 24981ae08745Sheppo 24991ae08745Sheppo mutex_enter(&ldcp->lock); 25001ae08745Sheppo 25011ae08745Sheppo /* return error if channel is not open */ 25021ae08745Sheppo if (ldcp->tstate < TS_OPEN) { 25031ae08745Sheppo DWARN(ldcp->id, 25041ae08745Sheppo "ldc_close: (0x%llx) channel is not open\n", ldcp->id); 25051ae08745Sheppo mutex_exit(&ldcp->lock); 25061ae08745Sheppo return (EFAULT); 25071ae08745Sheppo } 25081ae08745Sheppo 25091ae08745Sheppo /* if any memory handles, drings, are bound or mapped cannot close */ 25101ae08745Sheppo if (ldcp->mhdl_list != NULL) { 25111ae08745Sheppo DWARN(ldcp->id, 25121ae08745Sheppo "ldc_close: (0x%llx) channel has bound memory handles\n", 25131ae08745Sheppo ldcp->id); 25141ae08745Sheppo mutex_exit(&ldcp->lock); 25151ae08745Sheppo return (EBUSY); 25161ae08745Sheppo } 25171ae08745Sheppo if (ldcp->exp_dring_list != NULL) { 25181ae08745Sheppo DWARN(ldcp->id, 25191ae08745Sheppo "ldc_close: (0x%llx) channel has bound descriptor rings\n", 25201ae08745Sheppo ldcp->id); 25211ae08745Sheppo mutex_exit(&ldcp->lock); 25221ae08745Sheppo return (EBUSY); 25231ae08745Sheppo } 25241ae08745Sheppo if (ldcp->imp_dring_list != NULL) { 25251ae08745Sheppo DWARN(ldcp->id, 25261ae08745Sheppo "ldc_close: (0x%llx) channel has mapped descriptor rings\n", 25271ae08745Sheppo ldcp->id); 25281ae08745Sheppo mutex_exit(&ldcp->lock); 25291ae08745Sheppo return (EBUSY); 25301ae08745Sheppo } 25311ae08745Sheppo 2532d10e4ef2Snarayan /* Obtain Tx lock */ 2533d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2534d10e4ef2Snarayan 25351ae08745Sheppo /* 25361ae08745Sheppo * Wait for pending transmits to complete i.e Tx queue to drain 25371ae08745Sheppo * if there are pending pkts - wait 1 ms and retry again 25381ae08745Sheppo */ 25391ae08745Sheppo for (;;) { 25401ae08745Sheppo 25411ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 25421ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 25431ae08745Sheppo if (rv) { 25441ae08745Sheppo cmn_err(CE_WARN, 25451ae08745Sheppo "ldc_close: (0x%lx) cannot read qptrs\n", ldcp->id); 2546d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 25471ae08745Sheppo mutex_exit(&ldcp->lock); 25481ae08745Sheppo return (EIO); 25491ae08745Sheppo } 25501ae08745Sheppo 25511ae08745Sheppo if (ldcp->tx_head == ldcp->tx_tail || 25521ae08745Sheppo ldcp->link_state != LDC_CHANNEL_UP) { 25531ae08745Sheppo break; 25541ae08745Sheppo } 25551ae08745Sheppo 25561ae08745Sheppo if (chk_done) { 25571ae08745Sheppo DWARN(ldcp->id, 25581ae08745Sheppo "ldc_close: (0x%llx) Tx queue drain timeout\n", 25591ae08745Sheppo ldcp->id); 25601ae08745Sheppo break; 25611ae08745Sheppo } 25621ae08745Sheppo 25631ae08745Sheppo /* wait for one ms and try again */ 25641ae08745Sheppo delay(drv_usectohz(1000)); 25651ae08745Sheppo chk_done = B_TRUE; 25661ae08745Sheppo } 25671ae08745Sheppo 25681ae08745Sheppo /* 25691ae08745Sheppo * Unregister the channel with the nexus 25701ae08745Sheppo */ 2571d10e4ef2Snarayan while ((rv = i_ldc_unregister_channel(ldcp)) != 0) { 2572d10e4ef2Snarayan 2573d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 25741ae08745Sheppo mutex_exit(&ldcp->lock); 2575d10e4ef2Snarayan 2576d10e4ef2Snarayan /* if any error other than EAGAIN return back */ 2577d10e4ef2Snarayan if (rv != EAGAIN || retries >= LDC_MAX_RETRIES) { 2578d10e4ef2Snarayan cmn_err(CE_WARN, 2579d10e4ef2Snarayan "ldc_close: (0x%lx) unregister failed, %d\n", 2580d10e4ef2Snarayan ldcp->id, rv); 25811ae08745Sheppo return (rv); 25821ae08745Sheppo } 25831ae08745Sheppo 25841ae08745Sheppo /* 2585d10e4ef2Snarayan * As there could be pending interrupts we need 2586d10e4ef2Snarayan * to wait and try again 2587d10e4ef2Snarayan */ 2588d10e4ef2Snarayan drv_usecwait(LDC_DELAY); 2589d10e4ef2Snarayan mutex_enter(&ldcp->lock); 2590d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2591d10e4ef2Snarayan retries++; 2592d10e4ef2Snarayan } 2593d10e4ef2Snarayan 2594d10e4ef2Snarayan /* 25951ae08745Sheppo * Unregister queues 25961ae08745Sheppo */ 25971ae08745Sheppo rv = hv_ldc_tx_qconf(ldcp->id, NULL, NULL); 25981ae08745Sheppo if (rv) { 25991ae08745Sheppo cmn_err(CE_WARN, 26001ae08745Sheppo "ldc_close: (0x%lx) channel TX queue unconf failed\n", 26011ae08745Sheppo ldcp->id); 2602d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 26031ae08745Sheppo mutex_exit(&ldcp->lock); 26041ae08745Sheppo return (EIO); 26051ae08745Sheppo } 26061ae08745Sheppo rv = hv_ldc_rx_qconf(ldcp->id, NULL, NULL); 26071ae08745Sheppo if (rv) { 26081ae08745Sheppo cmn_err(CE_WARN, 26091ae08745Sheppo "ldc_close: (0x%lx) channel RX queue unconf failed\n", 26101ae08745Sheppo ldcp->id); 2611d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 26121ae08745Sheppo mutex_exit(&ldcp->lock); 26131ae08745Sheppo return (EIO); 26141ae08745Sheppo } 26151ae08745Sheppo 26161ae08745Sheppo ldcp->tstate &= ~TS_QCONF_RDY; 26171ae08745Sheppo 26181ae08745Sheppo /* Reset channel state information */ 26191ae08745Sheppo i_ldc_reset_state(ldcp); 26201ae08745Sheppo 26211ae08745Sheppo /* Mark channel as down and in initialized state */ 26221ae08745Sheppo ldcp->tx_ackd_head = 0; 26231ae08745Sheppo ldcp->tx_head = 0; 26241ae08745Sheppo ldcp->tstate = TS_INIT; 26251ae08745Sheppo ldcp->status = LDC_INIT; 26261ae08745Sheppo 2627d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 26281ae08745Sheppo mutex_exit(&ldcp->lock); 26291ae08745Sheppo 26301ae08745Sheppo /* Decrement number of open channels */ 26311ae08745Sheppo mutex_enter(&ldcssp->lock); 26321ae08745Sheppo ldcssp->channels_open--; 26331ae08745Sheppo mutex_exit(&ldcssp->lock); 26341ae08745Sheppo 26351ae08745Sheppo D1(ldcp->id, "ldc_close: (0x%llx) channel closed\n", ldcp->id); 26361ae08745Sheppo 26371ae08745Sheppo return (0); 26381ae08745Sheppo } 26391ae08745Sheppo 26401ae08745Sheppo /* 26411ae08745Sheppo * Register channel callback 26421ae08745Sheppo */ 26431ae08745Sheppo int 26441ae08745Sheppo ldc_reg_callback(ldc_handle_t handle, 26451ae08745Sheppo uint_t(*cb)(uint64_t event, caddr_t arg), caddr_t arg) 26461ae08745Sheppo { 26471ae08745Sheppo ldc_chan_t *ldcp; 26481ae08745Sheppo 26491ae08745Sheppo if (handle == NULL) { 26501ae08745Sheppo DWARN(DBG_ALL_LDCS, 26511ae08745Sheppo "ldc_reg_callback: invalid channel handle\n"); 26521ae08745Sheppo return (EINVAL); 26531ae08745Sheppo } 26541ae08745Sheppo if (((uint64_t)cb) < KERNELBASE) { 26551ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_reg_callback: invalid callback\n"); 26561ae08745Sheppo return (EINVAL); 26571ae08745Sheppo } 26581ae08745Sheppo ldcp = (ldc_chan_t *)handle; 26591ae08745Sheppo 26601ae08745Sheppo mutex_enter(&ldcp->lock); 26611ae08745Sheppo 26621ae08745Sheppo if (ldcp->cb) { 26631ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback exists\n", 26641ae08745Sheppo ldcp->id); 26651ae08745Sheppo mutex_exit(&ldcp->lock); 26661ae08745Sheppo return (EIO); 26671ae08745Sheppo } 26681ae08745Sheppo if (ldcp->cb_inprogress) { 26691ae08745Sheppo DWARN(ldcp->id, "ldc_reg_callback: (0x%llx) callback active\n", 26701ae08745Sheppo ldcp->id); 26711ae08745Sheppo mutex_exit(&ldcp->lock); 26721ae08745Sheppo return (EWOULDBLOCK); 26731ae08745Sheppo } 26741ae08745Sheppo 26751ae08745Sheppo ldcp->cb = cb; 26761ae08745Sheppo ldcp->cb_arg = arg; 26771ae08745Sheppo ldcp->cb_enabled = B_TRUE; 26781ae08745Sheppo 26791ae08745Sheppo D1(ldcp->id, 26801ae08745Sheppo "ldc_reg_callback: (0x%llx) registered callback for channel\n", 26811ae08745Sheppo ldcp->id); 26821ae08745Sheppo 26831ae08745Sheppo mutex_exit(&ldcp->lock); 26841ae08745Sheppo 26851ae08745Sheppo return (0); 26861ae08745Sheppo } 26871ae08745Sheppo 26881ae08745Sheppo /* 26891ae08745Sheppo * Unregister channel callback 26901ae08745Sheppo */ 26911ae08745Sheppo int 26921ae08745Sheppo ldc_unreg_callback(ldc_handle_t handle) 26931ae08745Sheppo { 26941ae08745Sheppo ldc_chan_t *ldcp; 26951ae08745Sheppo 26961ae08745Sheppo if (handle == NULL) { 26971ae08745Sheppo DWARN(DBG_ALL_LDCS, 26981ae08745Sheppo "ldc_unreg_callback: invalid channel handle\n"); 26991ae08745Sheppo return (EINVAL); 27001ae08745Sheppo } 27011ae08745Sheppo ldcp = (ldc_chan_t *)handle; 27021ae08745Sheppo 27031ae08745Sheppo mutex_enter(&ldcp->lock); 27041ae08745Sheppo 27051ae08745Sheppo if (ldcp->cb == NULL) { 27061ae08745Sheppo DWARN(ldcp->id, 27071ae08745Sheppo "ldc_unreg_callback: (0x%llx) no callback exists\n", 27081ae08745Sheppo ldcp->id); 27091ae08745Sheppo mutex_exit(&ldcp->lock); 27101ae08745Sheppo return (EIO); 27111ae08745Sheppo } 27121ae08745Sheppo if (ldcp->cb_inprogress) { 27131ae08745Sheppo DWARN(ldcp->id, 27141ae08745Sheppo "ldc_unreg_callback: (0x%llx) callback active\n", 27151ae08745Sheppo ldcp->id); 27161ae08745Sheppo mutex_exit(&ldcp->lock); 27171ae08745Sheppo return (EWOULDBLOCK); 27181ae08745Sheppo } 27191ae08745Sheppo 27201ae08745Sheppo ldcp->cb = NULL; 27211ae08745Sheppo ldcp->cb_arg = NULL; 27221ae08745Sheppo ldcp->cb_enabled = B_FALSE; 27231ae08745Sheppo 27241ae08745Sheppo D1(ldcp->id, 27251ae08745Sheppo "ldc_unreg_callback: (0x%llx) unregistered callback for channel\n", 27261ae08745Sheppo ldcp->id); 27271ae08745Sheppo 27281ae08745Sheppo mutex_exit(&ldcp->lock); 27291ae08745Sheppo 27301ae08745Sheppo return (0); 27311ae08745Sheppo } 27321ae08745Sheppo 27331ae08745Sheppo 27341ae08745Sheppo /* 27351ae08745Sheppo * Bring a channel up by initiating a handshake with the peer 27361ae08745Sheppo * This call is asynchronous. It will complete at a later point 27371ae08745Sheppo * in time when the peer responds back with an RTR. 27381ae08745Sheppo */ 27391ae08745Sheppo int 27401ae08745Sheppo ldc_up(ldc_handle_t handle) 27411ae08745Sheppo { 27421ae08745Sheppo int rv; 27431ae08745Sheppo ldc_chan_t *ldcp; 27441ae08745Sheppo ldc_msg_t *ldcmsg; 27451ae08745Sheppo uint64_t tx_tail; 27461ae08745Sheppo 27471ae08745Sheppo if (handle == NULL) { 27481ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_up: invalid channel handle\n"); 27491ae08745Sheppo return (EINVAL); 27501ae08745Sheppo } 27511ae08745Sheppo ldcp = (ldc_chan_t *)handle; 27521ae08745Sheppo 27531ae08745Sheppo mutex_enter(&ldcp->lock); 27541ae08745Sheppo 27551ae08745Sheppo if (ldcp->tstate == TS_UP) { 27561ae08745Sheppo D2(ldcp->id, 27571ae08745Sheppo "ldc_up: (0x%llx) channel is already in UP state\n", 27581ae08745Sheppo ldcp->id); 27591ae08745Sheppo mutex_exit(&ldcp->lock); 27601ae08745Sheppo return (0); 27611ae08745Sheppo } 27621ae08745Sheppo 27631ae08745Sheppo /* if the channel is in RAW mode - mark it as UP, if READY */ 27641ae08745Sheppo if (ldcp->mode == LDC_MODE_RAW && ldcp->tstate >= TS_READY) { 27651ae08745Sheppo ldcp->tstate = TS_UP; 27661ae08745Sheppo mutex_exit(&ldcp->lock); 27671ae08745Sheppo return (0); 27681ae08745Sheppo } 27691ae08745Sheppo 27701ae08745Sheppo /* Don't start another handshake if there is one in progress */ 27711ae08745Sheppo if (ldcp->hstate) { 27721ae08745Sheppo D2(ldcp->id, 27731ae08745Sheppo "ldc_up: (0x%llx) channel handshake in progress\n", 27741ae08745Sheppo ldcp->id); 27751ae08745Sheppo mutex_exit(&ldcp->lock); 27761ae08745Sheppo return (0); 27771ae08745Sheppo } 27781ae08745Sheppo 2779d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 2780d10e4ef2Snarayan 27811ae08745Sheppo /* get the current tail for the LDC msg */ 27821ae08745Sheppo rv = i_ldc_get_tx_tail(ldcp, &tx_tail); 27831ae08745Sheppo if (rv) { 27841ae08745Sheppo DWARN(ldcp->id, "ldc_up: (0x%llx) cannot initiate handshake\n", 27851ae08745Sheppo ldcp->id); 2786d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 27871ae08745Sheppo mutex_exit(&ldcp->lock); 27881ae08745Sheppo return (ECONNREFUSED); 27891ae08745Sheppo } 27901ae08745Sheppo 27911ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 27921ae08745Sheppo ZERO_PKT(ldcmsg); 27931ae08745Sheppo 27941ae08745Sheppo ldcmsg->type = LDC_CTRL; 27951ae08745Sheppo ldcmsg->stype = LDC_INFO; 27961ae08745Sheppo ldcmsg->ctrl = LDC_VER; 27971ae08745Sheppo ldcp->next_vidx = 0; 27981ae08745Sheppo bcopy(&ldc_versions[0], ldcmsg->udata, sizeof (ldc_versions[0])); 27991ae08745Sheppo 28001ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_up snd ver", (uint64_t)ldcmsg); 28011ae08745Sheppo 28021ae08745Sheppo /* initiate the send by calling into HV and set the new tail */ 28031ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) % 28041ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 28051ae08745Sheppo 28061ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 28071ae08745Sheppo if (rv) { 28081ae08745Sheppo DWARN(ldcp->id, 28091ae08745Sheppo "ldc_up: (0x%llx) cannot initiate handshake rv=%d\n", 28101ae08745Sheppo ldcp->id, rv); 2811d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 28121ae08745Sheppo mutex_exit(&ldcp->lock); 28131ae08745Sheppo return (rv); 28141ae08745Sheppo } 28151ae08745Sheppo 28160a55fbb7Slm66018 ldcp->hstate |= TS_SENT_VER; 28171ae08745Sheppo ldcp->tx_tail = tx_tail; 28181ae08745Sheppo D1(ldcp->id, "ldc_up: (0x%llx) channel up initiated\n", ldcp->id); 28191ae08745Sheppo 2820d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 28211ae08745Sheppo mutex_exit(&ldcp->lock); 28221ae08745Sheppo 28231ae08745Sheppo return (rv); 28241ae08745Sheppo } 28251ae08745Sheppo 28261ae08745Sheppo 28271ae08745Sheppo /* 2828e1ebb9ecSlm66018 * Bring a channel down by resetting its state and queues 28291ae08745Sheppo */ 28301ae08745Sheppo int 2831e1ebb9ecSlm66018 ldc_down(ldc_handle_t handle) 28321ae08745Sheppo { 28331ae08745Sheppo ldc_chan_t *ldcp; 28341ae08745Sheppo 28351ae08745Sheppo if (handle == NULL) { 2836e1ebb9ecSlm66018 DWARN(DBG_ALL_LDCS, "ldc_down: invalid channel handle\n"); 28371ae08745Sheppo return (EINVAL); 28381ae08745Sheppo } 28391ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28401ae08745Sheppo 28411ae08745Sheppo mutex_enter(&ldcp->lock); 2842d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 28431ae08745Sheppo i_ldc_reset(ldcp); 2844d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 28451ae08745Sheppo mutex_exit(&ldcp->lock); 28461ae08745Sheppo 28471ae08745Sheppo return (0); 28481ae08745Sheppo } 28491ae08745Sheppo 28501ae08745Sheppo /* 28511ae08745Sheppo * Get the current channel status 28521ae08745Sheppo */ 28531ae08745Sheppo int 28541ae08745Sheppo ldc_status(ldc_handle_t handle, ldc_status_t *status) 28551ae08745Sheppo { 28561ae08745Sheppo ldc_chan_t *ldcp; 28571ae08745Sheppo 28581ae08745Sheppo if (handle == NULL || status == NULL) { 28591ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_status: invalid argument\n"); 28601ae08745Sheppo return (EINVAL); 28611ae08745Sheppo } 28621ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28631ae08745Sheppo 28641ae08745Sheppo *status = ((ldc_chan_t *)handle)->status; 28651ae08745Sheppo 28661ae08745Sheppo D1(ldcp->id, 28671ae08745Sheppo "ldc_status: (0x%llx) returned status %d\n", ldcp->id, *status); 28681ae08745Sheppo return (0); 28691ae08745Sheppo } 28701ae08745Sheppo 28711ae08745Sheppo 28721ae08745Sheppo /* 28731ae08745Sheppo * Set the channel's callback mode - enable/disable callbacks 28741ae08745Sheppo */ 28751ae08745Sheppo int 28761ae08745Sheppo ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t cmode) 28771ae08745Sheppo { 28781ae08745Sheppo ldc_chan_t *ldcp; 28791ae08745Sheppo 28801ae08745Sheppo if (handle == NULL) { 28811ae08745Sheppo DWARN(DBG_ALL_LDCS, 28821ae08745Sheppo "ldc_set_intr_mode: invalid channel handle\n"); 28831ae08745Sheppo return (EINVAL); 28841ae08745Sheppo } 28851ae08745Sheppo ldcp = (ldc_chan_t *)handle; 28861ae08745Sheppo 28871ae08745Sheppo /* 28881ae08745Sheppo * Record no callbacks should be invoked 28891ae08745Sheppo */ 28901ae08745Sheppo mutex_enter(&ldcp->lock); 28911ae08745Sheppo 28921ae08745Sheppo switch (cmode) { 28931ae08745Sheppo case LDC_CB_DISABLE: 28941ae08745Sheppo if (!ldcp->cb_enabled) { 28951ae08745Sheppo DWARN(ldcp->id, 28961ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks disabled\n", 28971ae08745Sheppo ldcp->id); 28981ae08745Sheppo break; 28991ae08745Sheppo } 29001ae08745Sheppo ldcp->cb_enabled = B_FALSE; 29011ae08745Sheppo 29021ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) disabled callbacks\n", 29031ae08745Sheppo ldcp->id); 29041ae08745Sheppo break; 29051ae08745Sheppo 29061ae08745Sheppo case LDC_CB_ENABLE: 29071ae08745Sheppo if (ldcp->cb_enabled) { 29081ae08745Sheppo DWARN(ldcp->id, 29091ae08745Sheppo "ldc_set_cb_mode: (0x%llx) callbacks enabled\n", 29101ae08745Sheppo ldcp->id); 29111ae08745Sheppo break; 29121ae08745Sheppo } 29131ae08745Sheppo ldcp->cb_enabled = B_TRUE; 29141ae08745Sheppo 29151ae08745Sheppo D1(ldcp->id, "ldc_set_cb_mode: (0x%llx) enabled callbacks\n", 29161ae08745Sheppo ldcp->id); 29171ae08745Sheppo break; 29181ae08745Sheppo } 29191ae08745Sheppo 29201ae08745Sheppo mutex_exit(&ldcp->lock); 29211ae08745Sheppo 29221ae08745Sheppo return (0); 29231ae08745Sheppo } 29241ae08745Sheppo 29251ae08745Sheppo /* 29261ae08745Sheppo * Check to see if there are packets on the incoming queue 2927e1ebb9ecSlm66018 * Will return hasdata = B_FALSE if there are no packets 29281ae08745Sheppo */ 29291ae08745Sheppo int 2930e1ebb9ecSlm66018 ldc_chkq(ldc_handle_t handle, boolean_t *hasdata) 29311ae08745Sheppo { 29321ae08745Sheppo int rv; 29331ae08745Sheppo uint64_t rx_head, rx_tail; 29341ae08745Sheppo ldc_chan_t *ldcp; 29351ae08745Sheppo 29361ae08745Sheppo if (handle == NULL) { 29371ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_chkq: invalid channel handle\n"); 29381ae08745Sheppo return (EINVAL); 29391ae08745Sheppo } 29401ae08745Sheppo ldcp = (ldc_chan_t *)handle; 29411ae08745Sheppo 2942e1ebb9ecSlm66018 *hasdata = B_FALSE; 29431ae08745Sheppo 29441ae08745Sheppo mutex_enter(&ldcp->lock); 29451ae08745Sheppo 29461ae08745Sheppo if (ldcp->tstate != TS_UP) { 29471ae08745Sheppo D1(ldcp->id, 29481ae08745Sheppo "ldc_chkq: (0x%llx) channel is not up\n", ldcp->id); 29491ae08745Sheppo mutex_exit(&ldcp->lock); 29501ae08745Sheppo return (ECONNRESET); 29511ae08745Sheppo } 29521ae08745Sheppo 29531ae08745Sheppo /* Read packet(s) from the queue */ 29541ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 29551ae08745Sheppo &ldcp->link_state); 29561ae08745Sheppo if (rv != 0) { 29571ae08745Sheppo cmn_err(CE_WARN, 29581ae08745Sheppo "ldc_chkq: (0x%lx) unable to read queue ptrs", ldcp->id); 29591ae08745Sheppo mutex_exit(&ldcp->lock); 29601ae08745Sheppo return (EIO); 29611ae08745Sheppo } 29621ae08745Sheppo /* reset the channel state if the channel went down */ 29631ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 29641ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 2965d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 29661ae08745Sheppo i_ldc_reset(ldcp); 2967d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 29681ae08745Sheppo mutex_exit(&ldcp->lock); 29691ae08745Sheppo return (ECONNRESET); 29701ae08745Sheppo } 29711ae08745Sheppo 2972*4bac2208Snarayan if ((rx_head != rx_tail) || 2973*4bac2208Snarayan (ldcp->mode == LDC_MODE_STREAM && ldcp->stream_remains > 0)) { 2974*4bac2208Snarayan D1(ldcp->id, 2975*4bac2208Snarayan "ldc_chkq: (0x%llx) queue has pkt(s) or buffered data\n", 2976*4bac2208Snarayan ldcp->id); 2977e1ebb9ecSlm66018 *hasdata = B_TRUE; 29781ae08745Sheppo } 29791ae08745Sheppo 29801ae08745Sheppo mutex_exit(&ldcp->lock); 29811ae08745Sheppo 29821ae08745Sheppo return (0); 29831ae08745Sheppo } 29841ae08745Sheppo 29851ae08745Sheppo 29861ae08745Sheppo /* 29871ae08745Sheppo * Read 'size' amount of bytes or less. If incoming buffer 29881ae08745Sheppo * is more than 'size', ENOBUFS is returned. 29891ae08745Sheppo * 29901ae08745Sheppo * On return, size contains the number of bytes read. 29911ae08745Sheppo */ 29921ae08745Sheppo int 29931ae08745Sheppo ldc_read(ldc_handle_t handle, caddr_t bufp, size_t *sizep) 29941ae08745Sheppo { 29951ae08745Sheppo ldc_chan_t *ldcp; 29961ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 29971ae08745Sheppo int rv = 0, exit_val; 29981ae08745Sheppo 29991ae08745Sheppo if (handle == NULL) { 30001ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_read: invalid channel handle\n"); 30011ae08745Sheppo return (EINVAL); 30021ae08745Sheppo } 30031ae08745Sheppo 30041ae08745Sheppo ldcp = (ldc_chan_t *)handle; 30051ae08745Sheppo 30061ae08745Sheppo /* channel lock */ 30071ae08745Sheppo mutex_enter(&ldcp->lock); 30081ae08745Sheppo 30091ae08745Sheppo if (ldcp->tstate != TS_UP) { 30101ae08745Sheppo DWARN(ldcp->id, 30111ae08745Sheppo "ldc_read: (0x%llx) channel is not in UP state\n", 30121ae08745Sheppo ldcp->id); 30131ae08745Sheppo exit_val = ECONNRESET; 30141ae08745Sheppo } else { 30151ae08745Sheppo exit_val = ldcp->read_p(ldcp, bufp, sizep); 30161ae08745Sheppo } 30171ae08745Sheppo 30181ae08745Sheppo /* 30191ae08745Sheppo * if queue has been drained - clear interrupt 30201ae08745Sheppo */ 30211ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 30221ae08745Sheppo &ldcp->link_state); 30231ae08745Sheppo if (exit_val == 0 && rv == 0 && rx_head == rx_tail) { 30241ae08745Sheppo i_ldc_clear_intr(ldcp, CNEX_RX_INTR); 30251ae08745Sheppo } 30261ae08745Sheppo 30271ae08745Sheppo mutex_exit(&ldcp->lock); 30281ae08745Sheppo return (exit_val); 30291ae08745Sheppo } 30301ae08745Sheppo 30311ae08745Sheppo /* 30321ae08745Sheppo * Basic raw mondo read - 30331ae08745Sheppo * no interpretation of mondo contents at all. 30341ae08745Sheppo * 30351ae08745Sheppo * Enter and exit with ldcp->lock held by caller 30361ae08745Sheppo */ 30371ae08745Sheppo static int 30381ae08745Sheppo i_ldc_read_raw(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 30391ae08745Sheppo { 30401ae08745Sheppo uint64_t q_size_mask; 30411ae08745Sheppo ldc_msg_t *msgp; 30421ae08745Sheppo uint8_t *msgbufp; 30431ae08745Sheppo int rv = 0, space; 30441ae08745Sheppo uint64_t rx_head, rx_tail; 30451ae08745Sheppo 30461ae08745Sheppo space = *sizep; 30471ae08745Sheppo 30481ae08745Sheppo if (space < LDC_PAYLOAD_SIZE_RAW) 30491ae08745Sheppo return (ENOBUFS); 30501ae08745Sheppo 30511ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 30521ae08745Sheppo 30531ae08745Sheppo /* compute mask for increment */ 30541ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 30551ae08745Sheppo 30561ae08745Sheppo /* 30571ae08745Sheppo * Read packet(s) from the queue 30581ae08745Sheppo */ 30591ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &rx_head, &rx_tail, 30601ae08745Sheppo &ldcp->link_state); 30611ae08745Sheppo if (rv != 0) { 30621ae08745Sheppo cmn_err(CE_WARN, 30631ae08745Sheppo "ldc_read_raw: (0x%lx) unable to read queue ptrs", 30641ae08745Sheppo ldcp->id); 30651ae08745Sheppo return (EIO); 30661ae08745Sheppo } 30671ae08745Sheppo D1(ldcp->id, "ldc_read_raw: (0x%llx) rxh=0x%llx," 30681ae08745Sheppo " rxt=0x%llx, st=0x%llx\n", 30691ae08745Sheppo ldcp->id, rx_head, rx_tail, ldcp->link_state); 30701ae08745Sheppo 30711ae08745Sheppo /* reset the channel state if the channel went down */ 30721ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 3073d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 30741ae08745Sheppo i_ldc_reset(ldcp); 3075d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 30761ae08745Sheppo return (ECONNRESET); 30771ae08745Sheppo } 30781ae08745Sheppo 30791ae08745Sheppo /* 30801ae08745Sheppo * Check for empty queue 30811ae08745Sheppo */ 30821ae08745Sheppo if (rx_head == rx_tail) { 30831ae08745Sheppo *sizep = 0; 30841ae08745Sheppo return (0); 30851ae08745Sheppo } 30861ae08745Sheppo 30871ae08745Sheppo /* get the message */ 30881ae08745Sheppo msgp = (ldc_msg_t *)(ldcp->rx_q_va + rx_head); 30891ae08745Sheppo 30901ae08745Sheppo /* if channel is in RAW mode, copy data and return */ 30911ae08745Sheppo msgbufp = (uint8_t *)&(msgp->raw[0]); 30921ae08745Sheppo 30931ae08745Sheppo bcopy(msgbufp, target_bufp, LDC_PAYLOAD_SIZE_RAW); 30941ae08745Sheppo 30951ae08745Sheppo DUMP_PAYLOAD(ldcp->id, msgbufp); 30961ae08745Sheppo 30971ae08745Sheppo *sizep = LDC_PAYLOAD_SIZE_RAW; 30981ae08745Sheppo 30991ae08745Sheppo rx_head = (rx_head + LDC_PACKET_SIZE) & q_size_mask; 31000a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_head); 31011ae08745Sheppo 31021ae08745Sheppo return (rv); 31031ae08745Sheppo } 31041ae08745Sheppo 31051ae08745Sheppo /* 31061ae08745Sheppo * Process LDC mondos to build larger packets 31071ae08745Sheppo * with either un-reliable or reliable delivery. 31081ae08745Sheppo * 31091ae08745Sheppo * Enter and exit with ldcp->lock held by caller 31101ae08745Sheppo */ 31111ae08745Sheppo static int 31121ae08745Sheppo i_ldc_read_packet(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 31131ae08745Sheppo { 31141ae08745Sheppo int rv = 0; 31151ae08745Sheppo uint64_t rx_head = 0, rx_tail = 0; 31161ae08745Sheppo uint64_t curr_head = 0; 31171ae08745Sheppo ldc_msg_t *msg; 31181ae08745Sheppo caddr_t target; 31191ae08745Sheppo size_t len = 0, bytes_read = 0; 31200a55fbb7Slm66018 int retries = 0; 31211ae08745Sheppo uint64_t q_size_mask; 3122d10e4ef2Snarayan uint64_t first_fragment = 0; 31231ae08745Sheppo 31241ae08745Sheppo target = target_bufp; 31251ae08745Sheppo 31261ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 31271ae08745Sheppo 31281ae08745Sheppo /* compute mask for increment */ 31291ae08745Sheppo q_size_mask = (ldcp->rx_q_entries-1)<<LDC_PACKET_SHIFT; 31301ae08745Sheppo 31311ae08745Sheppo /* 31321ae08745Sheppo * Read packet(s) from the queue 31331ae08745Sheppo */ 31341ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, &curr_head, &rx_tail, 31351ae08745Sheppo &ldcp->link_state); 31361ae08745Sheppo if (rv != 0) { 31371ae08745Sheppo cmn_err(CE_WARN, 31381ae08745Sheppo "ldc_read: (0x%lx) unable to read queue ptrs", 31391ae08745Sheppo ldcp->id); 31401ae08745Sheppo return (EIO); 31411ae08745Sheppo } 31421ae08745Sheppo D1(ldcp->id, "ldc_read: (0x%llx) chd=0x%llx, tl=0x%llx, st=0x%llx\n", 31431ae08745Sheppo ldcp->id, curr_head, rx_tail, ldcp->link_state); 31441ae08745Sheppo 31451ae08745Sheppo /* reset the channel state if the channel went down */ 31461ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 3147d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 31481ae08745Sheppo i_ldc_reset(ldcp); 3149d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 31501ae08745Sheppo return (ECONNRESET); 31511ae08745Sheppo } 31521ae08745Sheppo 31531ae08745Sheppo for (;;) { 31541ae08745Sheppo 31551ae08745Sheppo if (curr_head == rx_tail) { 31561ae08745Sheppo rv = hv_ldc_rx_get_state(ldcp->id, 31571ae08745Sheppo &rx_head, &rx_tail, &ldcp->link_state); 31581ae08745Sheppo if (rv != 0) { 31591ae08745Sheppo cmn_err(CE_WARN, 31601ae08745Sheppo "ldc_read: (0x%lx) cannot read queue ptrs", 31611ae08745Sheppo ldcp->id); 31621ae08745Sheppo return (EIO); 31631ae08745Sheppo } 31641ae08745Sheppo /* reset the channel state if the channel went down */ 31651ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN) { 3166d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 31671ae08745Sheppo i_ldc_reset(ldcp); 3168d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 31691ae08745Sheppo return (ECONNRESET); 31701ae08745Sheppo } 31711ae08745Sheppo } 31721ae08745Sheppo 31731ae08745Sheppo if (curr_head == rx_tail) { 31741ae08745Sheppo 31751ae08745Sheppo /* If in the middle of a fragmented xfer */ 3176d10e4ef2Snarayan if (first_fragment != 0) { 31770a55fbb7Slm66018 31780a55fbb7Slm66018 /* wait for ldc_delay usecs */ 31790a55fbb7Slm66018 drv_usecwait(ldc_delay); 31800a55fbb7Slm66018 31810a55fbb7Slm66018 if (++retries < ldc_max_retries) 31821ae08745Sheppo continue; 31830a55fbb7Slm66018 31841ae08745Sheppo *sizep = 0; 3185d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 31861ae08745Sheppo DWARN(DBG_ALL_LDCS, 31871ae08745Sheppo "ldc_read: (0x%llx) read timeout", 31881ae08745Sheppo ldcp->id); 31891ae08745Sheppo return (ETIMEDOUT); 31901ae08745Sheppo } 31911ae08745Sheppo *sizep = 0; 31921ae08745Sheppo break; 31931ae08745Sheppo } 31940a55fbb7Slm66018 retries = 0; 31951ae08745Sheppo 31961ae08745Sheppo D2(ldcp->id, 31971ae08745Sheppo "ldc_read: (0x%llx) chd=0x%llx, rxhd=0x%llx, rxtl=0x%llx\n", 31981ae08745Sheppo ldcp->id, curr_head, rx_head, rx_tail); 31991ae08745Sheppo 32001ae08745Sheppo /* get the message */ 32011ae08745Sheppo msg = (ldc_msg_t *)(ldcp->rx_q_va + curr_head); 32021ae08745Sheppo 32031ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_read received pkt", 32041ae08745Sheppo ldcp->rx_q_va + curr_head); 32051ae08745Sheppo 32061ae08745Sheppo /* Check the message ID for the message received */ 32071ae08745Sheppo if ((rv = i_ldc_check_seqid(ldcp, msg)) != 0) { 32081ae08745Sheppo 32091ae08745Sheppo DWARN(ldcp->id, "ldc_read: (0x%llx) seqid error, " 32101ae08745Sheppo "q_ptrs=0x%lx,0x%lx", ldcp->id, rx_head, rx_tail); 32111ae08745Sheppo 32120a55fbb7Slm66018 /* throw away data */ 32130a55fbb7Slm66018 bytes_read = 0; 32140a55fbb7Slm66018 32151ae08745Sheppo /* Reset last_msg_rcd to start of message */ 3216d10e4ef2Snarayan if (first_fragment != 0) { 3217d10e4ef2Snarayan ldcp->last_msg_rcd = first_fragment - 1; 3218d10e4ef2Snarayan first_fragment = 0; 32191ae08745Sheppo } 32201ae08745Sheppo /* 32211ae08745Sheppo * Send a NACK -- invalid seqid 32221ae08745Sheppo * get the current tail for the response 32231ae08745Sheppo */ 32241ae08745Sheppo rv = i_ldc_send_pkt(ldcp, msg->type, LDC_NACK, 32251ae08745Sheppo (msg->ctrl & LDC_CTRL_MASK)); 32261ae08745Sheppo if (rv) { 32271ae08745Sheppo cmn_err(CE_NOTE, 32281ae08745Sheppo "ldc_read: (0x%lx) err sending " 32291ae08745Sheppo "NACK msg\n", ldcp->id); 3230d10e4ef2Snarayan 3231d10e4ef2Snarayan /* if cannot send NACK - reset channel */ 3232d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3233d10e4ef2Snarayan i_ldc_reset(ldcp); 3234d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3235d10e4ef2Snarayan rv = ECONNRESET; 3236d10e4ef2Snarayan break; 32371ae08745Sheppo } 32381ae08745Sheppo 32391ae08745Sheppo /* purge receive queue */ 32400a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 32411ae08745Sheppo 32421ae08745Sheppo break; 32431ae08745Sheppo } 32441ae08745Sheppo 32451ae08745Sheppo /* 32461ae08745Sheppo * Process any messages of type CTRL messages 3247e1ebb9ecSlm66018 * Future implementations should try to pass these 3248e1ebb9ecSlm66018 * to LDC link by resetting the intr state. 32491ae08745Sheppo * 32501ae08745Sheppo * NOTE: not done as a switch() as type can be both ctrl+data 32511ae08745Sheppo */ 32521ae08745Sheppo if (msg->type & LDC_CTRL) { 32531ae08745Sheppo if (rv = i_ldc_ctrlmsg(ldcp, msg)) { 32541ae08745Sheppo if (rv == EAGAIN) 32551ae08745Sheppo continue; 32560a55fbb7Slm66018 rv = i_ldc_set_rx_head(ldcp, rx_tail); 32571ae08745Sheppo *sizep = 0; 32581ae08745Sheppo bytes_read = 0; 32591ae08745Sheppo break; 32601ae08745Sheppo } 32611ae08745Sheppo } 32621ae08745Sheppo 32631ae08745Sheppo /* process data ACKs */ 32641ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 3265d10e4ef2Snarayan if (rv = i_ldc_process_data_ACK(ldcp, msg)) { 3266d10e4ef2Snarayan *sizep = 0; 3267d10e4ef2Snarayan bytes_read = 0; 3268d10e4ef2Snarayan break; 3269d10e4ef2Snarayan } 32701ae08745Sheppo } 32711ae08745Sheppo 32721ae08745Sheppo /* process data messages */ 32731ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_INFO)) { 32741ae08745Sheppo 32751ae08745Sheppo uint8_t *msgbuf = (uint8_t *)( 32761ae08745Sheppo (ldcp->mode == LDC_MODE_RELIABLE || 32771ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 32781ae08745Sheppo ? msg->rdata : msg->udata); 32791ae08745Sheppo 32801ae08745Sheppo D2(ldcp->id, 32811ae08745Sheppo "ldc_read: (0x%llx) received data msg\n", ldcp->id); 32821ae08745Sheppo 32831ae08745Sheppo /* get the packet length */ 32841ae08745Sheppo len = (msg->env & LDC_LEN_MASK); 32851ae08745Sheppo 32861ae08745Sheppo /* 32871ae08745Sheppo * FUTURE OPTIMIZATION: 32881ae08745Sheppo * dont need to set q head for every 32891ae08745Sheppo * packet we read just need to do this when 32901ae08745Sheppo * we are done or need to wait for more 32911ae08745Sheppo * mondos to make a full packet - this is 32921ae08745Sheppo * currently expensive. 32931ae08745Sheppo */ 32941ae08745Sheppo 3295d10e4ef2Snarayan if (first_fragment == 0) { 32961ae08745Sheppo 32971ae08745Sheppo /* 32981ae08745Sheppo * first packets should always have the start 32991ae08745Sheppo * bit set (even for a single packet). If not 33001ae08745Sheppo * throw away the packet 33011ae08745Sheppo */ 33021ae08745Sheppo if (!(msg->env & LDC_FRAG_START)) { 33031ae08745Sheppo 33041ae08745Sheppo DWARN(DBG_ALL_LDCS, 33051ae08745Sheppo "ldc_read: (0x%llx) not start - " 33061ae08745Sheppo "frag=%x\n", ldcp->id, 33071ae08745Sheppo (msg->env) & LDC_FRAG_MASK); 33081ae08745Sheppo 33091ae08745Sheppo /* toss pkt, inc head, cont reading */ 33101ae08745Sheppo bytes_read = 0; 33111ae08745Sheppo target = target_bufp; 33121ae08745Sheppo curr_head = 33131ae08745Sheppo (curr_head + LDC_PACKET_SIZE) 33141ae08745Sheppo & q_size_mask; 33151ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, 33161ae08745Sheppo curr_head)) 33171ae08745Sheppo break; 33181ae08745Sheppo 33191ae08745Sheppo continue; 33201ae08745Sheppo } 33211ae08745Sheppo 3322d10e4ef2Snarayan first_fragment = msg->seqid; 33231ae08745Sheppo } else { 33241ae08745Sheppo /* check to see if this is a pkt w/ START bit */ 33251ae08745Sheppo if (msg->env & LDC_FRAG_START) { 33261ae08745Sheppo DWARN(DBG_ALL_LDCS, 33271ae08745Sheppo "ldc_read:(0x%llx) unexpected pkt" 33281ae08745Sheppo " env=0x%x discarding %d bytes," 33291ae08745Sheppo " lastmsg=%d, currentmsg=%d\n", 33301ae08745Sheppo ldcp->id, msg->env&LDC_FRAG_MASK, 33311ae08745Sheppo bytes_read, ldcp->last_msg_rcd, 33321ae08745Sheppo msg->seqid); 33331ae08745Sheppo 33341ae08745Sheppo /* throw data we have read so far */ 33351ae08745Sheppo bytes_read = 0; 33361ae08745Sheppo target = target_bufp; 3337d10e4ef2Snarayan first_fragment = msg->seqid; 33381ae08745Sheppo 33391ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, 33401ae08745Sheppo curr_head)) 33411ae08745Sheppo break; 33421ae08745Sheppo } 33431ae08745Sheppo } 33441ae08745Sheppo 33451ae08745Sheppo /* copy (next) pkt into buffer */ 33461ae08745Sheppo if (len <= (*sizep - bytes_read)) { 33471ae08745Sheppo bcopy(msgbuf, target, len); 33481ae08745Sheppo target += len; 33491ae08745Sheppo bytes_read += len; 33501ae08745Sheppo } else { 33511ae08745Sheppo /* 33521ae08745Sheppo * there is not enough space in the buffer to 33531ae08745Sheppo * read this pkt. throw message away & continue 33541ae08745Sheppo * reading data from queue 33551ae08745Sheppo */ 33561ae08745Sheppo DWARN(DBG_ALL_LDCS, 33571ae08745Sheppo "ldc_read: (0x%llx) buffer too small, " 33581ae08745Sheppo "head=0x%lx, expect=%d, got=%d\n", ldcp->id, 33591ae08745Sheppo curr_head, *sizep, bytes_read+len); 33601ae08745Sheppo 3361d10e4ef2Snarayan first_fragment = 0; 33621ae08745Sheppo target = target_bufp; 33631ae08745Sheppo bytes_read = 0; 33641ae08745Sheppo 33651ae08745Sheppo /* throw away everything received so far */ 33661ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) 33671ae08745Sheppo break; 33681ae08745Sheppo 33691ae08745Sheppo /* continue reading remaining pkts */ 33701ae08745Sheppo continue; 33711ae08745Sheppo } 33721ae08745Sheppo } 33731ae08745Sheppo 33741ae08745Sheppo /* set the message id */ 33751ae08745Sheppo ldcp->last_msg_rcd = msg->seqid; 33761ae08745Sheppo 33771ae08745Sheppo /* move the head one position */ 33781ae08745Sheppo curr_head = (curr_head + LDC_PACKET_SIZE) & q_size_mask; 33791ae08745Sheppo 33801ae08745Sheppo if (msg->env & LDC_FRAG_STOP) { 33811ae08745Sheppo 33821ae08745Sheppo /* 33831ae08745Sheppo * All pkts that are part of this fragmented transfer 33841ae08745Sheppo * have been read or this was a single pkt read 33851ae08745Sheppo * or there was an error 33861ae08745Sheppo */ 33871ae08745Sheppo 33881ae08745Sheppo /* set the queue head */ 33891ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) 33901ae08745Sheppo bytes_read = 0; 33911ae08745Sheppo 33921ae08745Sheppo *sizep = bytes_read; 33931ae08745Sheppo 33941ae08745Sheppo break; 33951ae08745Sheppo } 33961ae08745Sheppo 33971ae08745Sheppo /* advance head if it is a DATA ACK */ 33981ae08745Sheppo if ((msg->type & LDC_DATA) && (msg->stype & LDC_ACK)) { 33991ae08745Sheppo 34001ae08745Sheppo /* set the queue head */ 34011ae08745Sheppo if (rv = i_ldc_set_rx_head(ldcp, curr_head)) { 34021ae08745Sheppo bytes_read = 0; 34031ae08745Sheppo break; 34041ae08745Sheppo } 34051ae08745Sheppo 34061ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) set ACK qhead 0x%llx", 34071ae08745Sheppo ldcp->id, curr_head); 34081ae08745Sheppo } 34091ae08745Sheppo 34101ae08745Sheppo } /* for (;;) */ 34111ae08745Sheppo 34121ae08745Sheppo 34131ae08745Sheppo /* 34141ae08745Sheppo * If useful data was read - Send msg ACK 34151ae08745Sheppo * OPTIMIZE: do not send ACK for all msgs - use some frequency 34161ae08745Sheppo */ 34171ae08745Sheppo if ((bytes_read > 0) && (ldcp->mode == LDC_MODE_RELIABLE || 34181ae08745Sheppo ldcp->mode == LDC_MODE_STREAM)) { 34191ae08745Sheppo 34201ae08745Sheppo rv = i_ldc_send_pkt(ldcp, LDC_DATA, LDC_ACK, 0); 3421d10e4ef2Snarayan if (rv) { 34221ae08745Sheppo cmn_err(CE_NOTE, 34231ae08745Sheppo "ldc_read: (0x%lx) cannot send ACK\n", ldcp->id); 3424d10e4ef2Snarayan 3425d10e4ef2Snarayan /* if cannot send ACK - reset channel */ 3426d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3427d10e4ef2Snarayan i_ldc_reset(ldcp); 3428d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3429d10e4ef2Snarayan rv = ECONNRESET; 34301ae08745Sheppo } 34311ae08745Sheppo } 34321ae08745Sheppo 34331ae08745Sheppo D2(ldcp->id, "ldc_read: (0x%llx) end size=%d", ldcp->id, *sizep); 34341ae08745Sheppo 34351ae08745Sheppo return (rv); 34361ae08745Sheppo } 34371ae08745Sheppo 34381ae08745Sheppo /* 34391ae08745Sheppo * Use underlying reliable packet mechanism to fetch 34401ae08745Sheppo * and buffer incoming packets so we can hand them back as 34411ae08745Sheppo * a basic byte stream. 34421ae08745Sheppo * 34431ae08745Sheppo * Enter and exit with ldcp->lock held by caller 34441ae08745Sheppo */ 34451ae08745Sheppo static int 34461ae08745Sheppo i_ldc_read_stream(ldc_chan_t *ldcp, caddr_t target_bufp, size_t *sizep) 34471ae08745Sheppo { 34481ae08745Sheppo int rv; 34491ae08745Sheppo size_t size; 34501ae08745Sheppo 34511ae08745Sheppo ASSERT(mutex_owned(&ldcp->lock)); 34521ae08745Sheppo 34531ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) buffer size=%d", 34541ae08745Sheppo ldcp->id, *sizep); 34551ae08745Sheppo 34561ae08745Sheppo if (ldcp->stream_remains == 0) { 34571ae08745Sheppo size = ldcp->mtu; 34581ae08745Sheppo rv = i_ldc_read_packet(ldcp, 34591ae08745Sheppo (caddr_t)ldcp->stream_bufferp, &size); 34601ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: read packet (0x%llx) size=%d", 34611ae08745Sheppo ldcp->id, size); 34621ae08745Sheppo 34631ae08745Sheppo if (rv != 0) 34641ae08745Sheppo return (rv); 34651ae08745Sheppo 34661ae08745Sheppo ldcp->stream_remains = size; 34671ae08745Sheppo ldcp->stream_offset = 0; 34681ae08745Sheppo } 34691ae08745Sheppo 34701ae08745Sheppo size = MIN(ldcp->stream_remains, *sizep); 34711ae08745Sheppo 34721ae08745Sheppo bcopy(ldcp->stream_bufferp + ldcp->stream_offset, target_bufp, size); 34731ae08745Sheppo ldcp->stream_offset += size; 34741ae08745Sheppo ldcp->stream_remains -= size; 34751ae08745Sheppo 34761ae08745Sheppo D2(ldcp->id, "i_ldc_read_stream: (0x%llx) fill from buffer size=%d", 34771ae08745Sheppo ldcp->id, size); 34781ae08745Sheppo 34791ae08745Sheppo *sizep = size; 34801ae08745Sheppo return (0); 34811ae08745Sheppo } 34821ae08745Sheppo 34831ae08745Sheppo /* 34841ae08745Sheppo * Write specified amount of bytes to the channel 34851ae08745Sheppo * in multiple pkts of pkt_payload size. Each 34861ae08745Sheppo * packet is tagged with an unique packet ID in 3487e1ebb9ecSlm66018 * the case of a reliable link. 34881ae08745Sheppo * 34891ae08745Sheppo * On return, size contains the number of bytes written. 34901ae08745Sheppo */ 34911ae08745Sheppo int 34921ae08745Sheppo ldc_write(ldc_handle_t handle, caddr_t buf, size_t *sizep) 34931ae08745Sheppo { 34941ae08745Sheppo ldc_chan_t *ldcp; 34951ae08745Sheppo int rv = 0; 34961ae08745Sheppo 34971ae08745Sheppo if (handle == NULL) { 34981ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_write: invalid channel handle\n"); 34991ae08745Sheppo return (EINVAL); 35001ae08745Sheppo } 35011ae08745Sheppo ldcp = (ldc_chan_t *)handle; 35021ae08745Sheppo 3503d10e4ef2Snarayan /* check if writes can occur */ 3504d10e4ef2Snarayan if (!mutex_tryenter(&ldcp->tx_lock)) { 3505d10e4ef2Snarayan /* 3506d10e4ef2Snarayan * Could not get the lock - channel could 3507d10e4ef2Snarayan * be in the process of being unconfigured 3508d10e4ef2Snarayan * or reader has encountered an error 3509d10e4ef2Snarayan */ 3510d10e4ef2Snarayan return (EAGAIN); 3511d10e4ef2Snarayan } 35121ae08745Sheppo 35131ae08745Sheppo /* check if non-zero data to write */ 35141ae08745Sheppo if (buf == NULL || sizep == NULL) { 35151ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) invalid data write\n", 35161ae08745Sheppo ldcp->id); 3517d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 35181ae08745Sheppo return (EINVAL); 35191ae08745Sheppo } 35201ae08745Sheppo 35211ae08745Sheppo if (*sizep == 0) { 35221ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write size of zero\n", 35231ae08745Sheppo ldcp->id); 3524d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 35251ae08745Sheppo return (0); 35261ae08745Sheppo } 35271ae08745Sheppo 35281ae08745Sheppo /* Check if channel is UP for data exchange */ 35291ae08745Sheppo if (ldcp->tstate != TS_UP) { 35301ae08745Sheppo DWARN(ldcp->id, 35311ae08745Sheppo "ldc_write: (0x%llx) channel is not in UP state\n", 35321ae08745Sheppo ldcp->id); 35331ae08745Sheppo *sizep = 0; 35341ae08745Sheppo rv = ECONNRESET; 35351ae08745Sheppo } else { 35361ae08745Sheppo rv = ldcp->write_p(ldcp, buf, sizep); 35371ae08745Sheppo } 35381ae08745Sheppo 3539d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 35401ae08745Sheppo 35411ae08745Sheppo return (rv); 35421ae08745Sheppo } 35431ae08745Sheppo 35441ae08745Sheppo /* 35451ae08745Sheppo * Write a raw packet to the channel 35461ae08745Sheppo * On return, size contains the number of bytes written. 35471ae08745Sheppo */ 35481ae08745Sheppo static int 35491ae08745Sheppo i_ldc_write_raw(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 35501ae08745Sheppo { 35511ae08745Sheppo ldc_msg_t *ldcmsg; 35521ae08745Sheppo uint64_t tx_head, tx_tail, new_tail; 35531ae08745Sheppo int rv = 0; 35541ae08745Sheppo size_t size; 35551ae08745Sheppo 3556d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 35571ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RAW); 35581ae08745Sheppo 35591ae08745Sheppo size = *sizep; 35601ae08745Sheppo 35611ae08745Sheppo /* 35621ae08745Sheppo * Check to see if the packet size is less than or 35631ae08745Sheppo * equal to packet size support in raw mode 35641ae08745Sheppo */ 35651ae08745Sheppo if (size > ldcp->pkt_payload) { 35661ae08745Sheppo DWARN(ldcp->id, 35671ae08745Sheppo "ldc_write: (0x%llx) invalid size (0x%llx) for RAW mode\n", 35681ae08745Sheppo ldcp->id, *sizep); 35691ae08745Sheppo *sizep = 0; 35701ae08745Sheppo return (EMSGSIZE); 35711ae08745Sheppo } 35721ae08745Sheppo 35731ae08745Sheppo /* get the qptrs for the tx queue */ 35741ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 35751ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 35761ae08745Sheppo if (rv != 0) { 35771ae08745Sheppo cmn_err(CE_WARN, 35781ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 35791ae08745Sheppo *sizep = 0; 35801ae08745Sheppo return (EIO); 35811ae08745Sheppo } 35821ae08745Sheppo 35831ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 35841ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 35851ae08745Sheppo DWARN(ldcp->id, 35861ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 3587d10e4ef2Snarayan 35881ae08745Sheppo *sizep = 0; 3589d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 3590d10e4ef2Snarayan i_ldc_reset(ldcp); 3591d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3592d10e4ef2Snarayan } else { 3593d10e4ef2Snarayan /* 3594d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3595d10e4ef2Snarayan * and Tx lock in correct order 3596d10e4ef2Snarayan */ 3597d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3598d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3599d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3600d10e4ef2Snarayan i_ldc_reset(ldcp); 3601d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3602d10e4ef2Snarayan } 36031ae08745Sheppo return (ECONNRESET); 36041ae08745Sheppo } 36051ae08745Sheppo 36061ae08745Sheppo tx_tail = ldcp->tx_tail; 36071ae08745Sheppo tx_head = ldcp->tx_head; 36081ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) & 36091ae08745Sheppo ((ldcp->tx_q_entries-1) << LDC_PACKET_SHIFT); 36101ae08745Sheppo 36111ae08745Sheppo if (new_tail == tx_head) { 36121ae08745Sheppo DWARN(DBG_ALL_LDCS, 36131ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 36141ae08745Sheppo *sizep = 0; 36151ae08745Sheppo return (EWOULDBLOCK); 36161ae08745Sheppo } 36171ae08745Sheppo 36181ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 36191ae08745Sheppo ldcp->id, size); 36201ae08745Sheppo 36211ae08745Sheppo /* Send the data now */ 36221ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 36231ae08745Sheppo 36241ae08745Sheppo /* copy the data into pkt */ 36251ae08745Sheppo bcopy((uint8_t *)buf, ldcmsg, size); 36261ae08745Sheppo 36271ae08745Sheppo /* increment tail */ 36281ae08745Sheppo tx_tail = new_tail; 36291ae08745Sheppo 36301ae08745Sheppo /* 36311ae08745Sheppo * All packets have been copied into the TX queue 36321ae08745Sheppo * update the tail ptr in the HV 36331ae08745Sheppo */ 36341ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 36351ae08745Sheppo if (rv) { 36361ae08745Sheppo if (rv == EWOULDBLOCK) { 36371ae08745Sheppo DWARN(ldcp->id, "ldc_write: (0x%llx) write timed out\n", 36381ae08745Sheppo ldcp->id); 36391ae08745Sheppo *sizep = 0; 36401ae08745Sheppo return (EWOULDBLOCK); 36411ae08745Sheppo } 36421ae08745Sheppo 36431ae08745Sheppo *sizep = 0; 3644d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 3645d10e4ef2Snarayan i_ldc_reset(ldcp); 3646d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3647d10e4ef2Snarayan } else { 3648d10e4ef2Snarayan /* 3649d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3650d10e4ef2Snarayan * and Tx lock in correct order 3651d10e4ef2Snarayan */ 3652d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3653d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3654d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3655d10e4ef2Snarayan i_ldc_reset(ldcp); 3656d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3657d10e4ef2Snarayan } 36581ae08745Sheppo return (ECONNRESET); 36591ae08745Sheppo } 36601ae08745Sheppo 36611ae08745Sheppo ldcp->tx_tail = tx_tail; 36621ae08745Sheppo *sizep = size; 36631ae08745Sheppo 36641ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, size); 36651ae08745Sheppo 36661ae08745Sheppo return (rv); 36671ae08745Sheppo } 36681ae08745Sheppo 36691ae08745Sheppo 36701ae08745Sheppo /* 36711ae08745Sheppo * Write specified amount of bytes to the channel 36721ae08745Sheppo * in multiple pkts of pkt_payload size. Each 36731ae08745Sheppo * packet is tagged with an unique packet ID in 3674e1ebb9ecSlm66018 * the case of a reliable link. 36751ae08745Sheppo * 36761ae08745Sheppo * On return, size contains the number of bytes written. 36771ae08745Sheppo * This function needs to ensure that the write size is < MTU size 36781ae08745Sheppo */ 36791ae08745Sheppo static int 36801ae08745Sheppo i_ldc_write_packet(ldc_chan_t *ldcp, caddr_t buf, size_t *size) 36811ae08745Sheppo { 36821ae08745Sheppo ldc_msg_t *ldcmsg; 36831ae08745Sheppo uint64_t tx_head, tx_tail, new_tail, start; 36841ae08745Sheppo uint64_t txq_size_mask, numavail; 36851ae08745Sheppo uint8_t *msgbuf, *source = (uint8_t *)buf; 36861ae08745Sheppo size_t len, bytes_written = 0, remaining; 36871ae08745Sheppo int rv; 36881ae08745Sheppo uint32_t curr_seqid; 36891ae08745Sheppo 3690d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 36911ae08745Sheppo 36921ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_RELIABLE || 36931ae08745Sheppo ldcp->mode == LDC_MODE_UNRELIABLE || 36941ae08745Sheppo ldcp->mode == LDC_MODE_STREAM); 36951ae08745Sheppo 36961ae08745Sheppo /* compute mask for increment */ 36971ae08745Sheppo txq_size_mask = (ldcp->tx_q_entries - 1) << LDC_PACKET_SHIFT; 36981ae08745Sheppo 36991ae08745Sheppo /* get the qptrs for the tx queue */ 37001ae08745Sheppo rv = hv_ldc_tx_get_state(ldcp->id, 37011ae08745Sheppo &ldcp->tx_head, &ldcp->tx_tail, &ldcp->link_state); 37021ae08745Sheppo if (rv != 0) { 37031ae08745Sheppo cmn_err(CE_WARN, 37041ae08745Sheppo "ldc_write: (0x%lx) cannot read queue ptrs\n", ldcp->id); 37051ae08745Sheppo *size = 0; 37061ae08745Sheppo return (EIO); 37071ae08745Sheppo } 37081ae08745Sheppo 37091ae08745Sheppo if (ldcp->link_state == LDC_CHANNEL_DOWN || 37101ae08745Sheppo ldcp->link_state == LDC_CHANNEL_RESET) { 37111ae08745Sheppo DWARN(ldcp->id, 37121ae08745Sheppo "ldc_write: (0x%llx) channel down/reset\n", ldcp->id); 37131ae08745Sheppo *size = 0; 3714d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 37151ae08745Sheppo i_ldc_reset(ldcp); 3716d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3717d10e4ef2Snarayan } else { 3718d10e4ef2Snarayan /* 3719d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3720d10e4ef2Snarayan * and Tx lock in correct order 3721d10e4ef2Snarayan */ 3722d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3723d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3724d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3725d10e4ef2Snarayan i_ldc_reset(ldcp); 3726d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3727d10e4ef2Snarayan } 37281ae08745Sheppo return (ECONNRESET); 37291ae08745Sheppo } 37301ae08745Sheppo 37311ae08745Sheppo tx_tail = ldcp->tx_tail; 37321ae08745Sheppo new_tail = (tx_tail + LDC_PACKET_SIZE) % 37331ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT); 37341ae08745Sheppo 37351ae08745Sheppo /* 3736e1ebb9ecSlm66018 * Link mode determines whether we use HV Tx head or the 37371ae08745Sheppo * private protocol head (corresponding to last ACKd pkt) for 37381ae08745Sheppo * determining how much we can write 37391ae08745Sheppo */ 37401ae08745Sheppo tx_head = (ldcp->mode == LDC_MODE_RELIABLE || 37411ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 37421ae08745Sheppo ? ldcp->tx_ackd_head : ldcp->tx_head; 37431ae08745Sheppo if (new_tail == tx_head) { 37441ae08745Sheppo DWARN(DBG_ALL_LDCS, 37451ae08745Sheppo "ldc_write: (0x%llx) TX queue is full\n", ldcp->id); 37461ae08745Sheppo *size = 0; 37471ae08745Sheppo return (EWOULDBLOCK); 37481ae08745Sheppo } 37491ae08745Sheppo 37501ae08745Sheppo /* 37511ae08745Sheppo * Make sure that the LDC Tx queue has enough space 37521ae08745Sheppo */ 37531ae08745Sheppo numavail = (tx_head >> LDC_PACKET_SHIFT) - (tx_tail >> LDC_PACKET_SHIFT) 37541ae08745Sheppo + ldcp->tx_q_entries - 1; 37551ae08745Sheppo numavail %= ldcp->tx_q_entries; 37561ae08745Sheppo 37571ae08745Sheppo if (*size > (numavail * ldcp->pkt_payload)) { 37581ae08745Sheppo DWARN(DBG_ALL_LDCS, 37591ae08745Sheppo "ldc_write: (0x%llx) TX queue has no space\n", ldcp->id); 37601ae08745Sheppo return (EWOULDBLOCK); 37611ae08745Sheppo } 37621ae08745Sheppo 37631ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) start xfer size=%d", 37641ae08745Sheppo ldcp->id, *size); 37651ae08745Sheppo 37661ae08745Sheppo /* Send the data now */ 37671ae08745Sheppo bytes_written = 0; 37681ae08745Sheppo curr_seqid = ldcp->last_msg_snt; 37691ae08745Sheppo start = tx_tail; 37701ae08745Sheppo 37711ae08745Sheppo while (*size > bytes_written) { 37721ae08745Sheppo 37731ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + tx_tail); 37741ae08745Sheppo 37751ae08745Sheppo msgbuf = (uint8_t *)((ldcp->mode == LDC_MODE_RELIABLE || 37761ae08745Sheppo ldcp->mode == LDC_MODE_STREAM) 37771ae08745Sheppo ? ldcmsg->rdata : ldcmsg->udata); 37781ae08745Sheppo 37791ae08745Sheppo ldcmsg->type = LDC_DATA; 37801ae08745Sheppo ldcmsg->stype = LDC_INFO; 37811ae08745Sheppo ldcmsg->ctrl = 0; 37821ae08745Sheppo 37831ae08745Sheppo remaining = *size - bytes_written; 37841ae08745Sheppo len = min(ldcp->pkt_payload, remaining); 37851ae08745Sheppo ldcmsg->env = (uint8_t)len; 37861ae08745Sheppo 37871ae08745Sheppo curr_seqid++; 37881ae08745Sheppo ldcmsg->seqid = curr_seqid; 37891ae08745Sheppo 37901ae08745Sheppo DUMP_LDC_PKT(ldcp, "ldc_write snd data", (uint64_t)ldcmsg); 37911ae08745Sheppo 37921ae08745Sheppo /* copy the data into pkt */ 37931ae08745Sheppo bcopy(source, msgbuf, len); 37941ae08745Sheppo 37951ae08745Sheppo source += len; 37961ae08745Sheppo bytes_written += len; 37971ae08745Sheppo 37981ae08745Sheppo /* increment tail */ 37991ae08745Sheppo tx_tail = (tx_tail + LDC_PACKET_SIZE) & txq_size_mask; 38001ae08745Sheppo 38011ae08745Sheppo ASSERT(tx_tail != tx_head); 38021ae08745Sheppo } 38031ae08745Sheppo 38041ae08745Sheppo /* Set the start and stop bits */ 38051ae08745Sheppo ldcmsg->env |= LDC_FRAG_STOP; 38061ae08745Sheppo ldcmsg = (ldc_msg_t *)(ldcp->tx_q_va + start); 38071ae08745Sheppo ldcmsg->env |= LDC_FRAG_START; 38081ae08745Sheppo 38091ae08745Sheppo /* 38101ae08745Sheppo * All packets have been copied into the TX queue 38111ae08745Sheppo * update the tail ptr in the HV 38121ae08745Sheppo */ 38131ae08745Sheppo rv = i_ldc_set_tx_tail(ldcp, tx_tail); 38141ae08745Sheppo if (rv == 0) { 38151ae08745Sheppo ldcp->tx_tail = tx_tail; 38161ae08745Sheppo ldcp->last_msg_snt = curr_seqid; 38171ae08745Sheppo *size = bytes_written; 38181ae08745Sheppo } else { 38191ae08745Sheppo int rv2; 38201ae08745Sheppo 38211ae08745Sheppo if (rv != EWOULDBLOCK) { 38221ae08745Sheppo *size = 0; 3823d10e4ef2Snarayan if (mutex_tryenter(&ldcp->lock)) { 3824d10e4ef2Snarayan i_ldc_reset(ldcp); 3825d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3826d10e4ef2Snarayan } else { 3827d10e4ef2Snarayan /* 3828d10e4ef2Snarayan * Release Tx lock, and then reacquire channel 3829d10e4ef2Snarayan * and Tx lock in correct order 3830d10e4ef2Snarayan */ 3831d10e4ef2Snarayan mutex_exit(&ldcp->tx_lock); 3832d10e4ef2Snarayan mutex_enter(&ldcp->lock); 3833d10e4ef2Snarayan mutex_enter(&ldcp->tx_lock); 3834d10e4ef2Snarayan i_ldc_reset(ldcp); 3835d10e4ef2Snarayan mutex_exit(&ldcp->lock); 3836d10e4ef2Snarayan } 38371ae08745Sheppo return (ECONNRESET); 38381ae08745Sheppo } 38391ae08745Sheppo 38401ae08745Sheppo DWARN(ldcp->id, "hv_tx_set_tail returns 0x%x (head 0x%x, " 38411ae08745Sheppo "old tail 0x%x, new tail 0x%x, qsize=0x%x)\n", 38421ae08745Sheppo rv, ldcp->tx_head, ldcp->tx_tail, tx_tail, 38431ae08745Sheppo (ldcp->tx_q_entries << LDC_PACKET_SHIFT)); 38441ae08745Sheppo 38451ae08745Sheppo rv2 = hv_ldc_tx_get_state(ldcp->id, 38461ae08745Sheppo &tx_head, &tx_tail, &ldcp->link_state); 38471ae08745Sheppo 38481ae08745Sheppo DWARN(ldcp->id, "hv_ldc_tx_get_state returns 0x%x " 38491ae08745Sheppo "(head 0x%x, tail 0x%x state 0x%x)\n", 38501ae08745Sheppo rv2, tx_head, tx_tail, ldcp->link_state); 38511ae08745Sheppo 38521ae08745Sheppo *size = 0; 38531ae08745Sheppo } 38541ae08745Sheppo 38551ae08745Sheppo D2(ldcp->id, "ldc_write: (0x%llx) end xfer size=%d", ldcp->id, *size); 38561ae08745Sheppo 38571ae08745Sheppo return (rv); 38581ae08745Sheppo } 38591ae08745Sheppo 38601ae08745Sheppo /* 38611ae08745Sheppo * Write specified amount of bytes to the channel 38621ae08745Sheppo * in multiple pkts of pkt_payload size. Each 38631ae08745Sheppo * packet is tagged with an unique packet ID in 3864e1ebb9ecSlm66018 * the case of a reliable link. 38651ae08745Sheppo * 38661ae08745Sheppo * On return, size contains the number of bytes written. 38671ae08745Sheppo * This function needs to ensure that the write size is < MTU size 38681ae08745Sheppo */ 38691ae08745Sheppo static int 38701ae08745Sheppo i_ldc_write_stream(ldc_chan_t *ldcp, caddr_t buf, size_t *sizep) 38711ae08745Sheppo { 3872d10e4ef2Snarayan ASSERT(MUTEX_HELD(&ldcp->tx_lock)); 38731ae08745Sheppo ASSERT(ldcp->mode == LDC_MODE_STREAM); 38741ae08745Sheppo 38751ae08745Sheppo /* Truncate packet to max of MTU size */ 38761ae08745Sheppo if (*sizep > ldcp->mtu) *sizep = ldcp->mtu; 38771ae08745Sheppo return (i_ldc_write_packet(ldcp, buf, sizep)); 38781ae08745Sheppo } 38791ae08745Sheppo 38801ae08745Sheppo 38811ae08745Sheppo /* 38821ae08745Sheppo * Interfaces for channel nexus to register/unregister with LDC module 38831ae08745Sheppo * The nexus will register functions to be used to register individual 38841ae08745Sheppo * channels with the nexus and enable interrupts for the channels 38851ae08745Sheppo */ 38861ae08745Sheppo int 38871ae08745Sheppo ldc_register(ldc_cnex_t *cinfo) 38881ae08745Sheppo { 38891ae08745Sheppo ldc_chan_t *ldcp; 38901ae08745Sheppo 38911ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL || 38921ae08745Sheppo cinfo->reg_chan == NULL || cinfo->unreg_chan == NULL || 38931ae08745Sheppo cinfo->add_intr == NULL || cinfo->rem_intr == NULL || 38941ae08745Sheppo cinfo->clr_intr == NULL) { 38951ae08745Sheppo 38961ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_register: invalid nexus info\n"); 38971ae08745Sheppo return (EINVAL); 38981ae08745Sheppo } 38991ae08745Sheppo 39001ae08745Sheppo mutex_enter(&ldcssp->lock); 39011ae08745Sheppo 39021ae08745Sheppo /* nexus registration */ 39031ae08745Sheppo ldcssp->cinfo.dip = cinfo->dip; 39041ae08745Sheppo ldcssp->cinfo.reg_chan = cinfo->reg_chan; 39051ae08745Sheppo ldcssp->cinfo.unreg_chan = cinfo->unreg_chan; 39061ae08745Sheppo ldcssp->cinfo.add_intr = cinfo->add_intr; 39071ae08745Sheppo ldcssp->cinfo.rem_intr = cinfo->rem_intr; 39081ae08745Sheppo ldcssp->cinfo.clr_intr = cinfo->clr_intr; 39091ae08745Sheppo 39101ae08745Sheppo /* register any channels that might have been previously initialized */ 39111ae08745Sheppo ldcp = ldcssp->chan_list; 39121ae08745Sheppo while (ldcp) { 39131ae08745Sheppo if ((ldcp->tstate & TS_QCONF_RDY) && 39141ae08745Sheppo (ldcp->tstate & TS_CNEX_RDY) == 0) 39151ae08745Sheppo (void) i_ldc_register_channel(ldcp); 39161ae08745Sheppo 39171ae08745Sheppo ldcp = ldcp->next; 39181ae08745Sheppo } 39191ae08745Sheppo 39201ae08745Sheppo mutex_exit(&ldcssp->lock); 39211ae08745Sheppo 39221ae08745Sheppo return (0); 39231ae08745Sheppo } 39241ae08745Sheppo 39251ae08745Sheppo int 39261ae08745Sheppo ldc_unregister(ldc_cnex_t *cinfo) 39271ae08745Sheppo { 39281ae08745Sheppo if (cinfo == NULL || cinfo->dip == NULL) { 39291ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid nexus info\n"); 39301ae08745Sheppo return (EINVAL); 39311ae08745Sheppo } 39321ae08745Sheppo 39331ae08745Sheppo mutex_enter(&ldcssp->lock); 39341ae08745Sheppo 39351ae08745Sheppo if (cinfo->dip != ldcssp->cinfo.dip) { 39361ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_unregister: invalid dip\n"); 39371ae08745Sheppo mutex_exit(&ldcssp->lock); 39381ae08745Sheppo return (EINVAL); 39391ae08745Sheppo } 39401ae08745Sheppo 39411ae08745Sheppo /* nexus unregister */ 39421ae08745Sheppo ldcssp->cinfo.dip = NULL; 39431ae08745Sheppo ldcssp->cinfo.reg_chan = NULL; 39441ae08745Sheppo ldcssp->cinfo.unreg_chan = NULL; 39451ae08745Sheppo ldcssp->cinfo.add_intr = NULL; 39461ae08745Sheppo ldcssp->cinfo.rem_intr = NULL; 39471ae08745Sheppo ldcssp->cinfo.clr_intr = NULL; 39481ae08745Sheppo 39491ae08745Sheppo mutex_exit(&ldcssp->lock); 39501ae08745Sheppo 39511ae08745Sheppo return (0); 39521ae08745Sheppo } 39531ae08745Sheppo 39541ae08745Sheppo 39551ae08745Sheppo /* ------------------------------------------------------------------------- */ 39561ae08745Sheppo 39571ae08745Sheppo /* 39581ae08745Sheppo * Allocate a memory handle for the channel and link it into the list 39591ae08745Sheppo * Also choose which memory table to use if this is the first handle 39601ae08745Sheppo * being assigned to this channel 39611ae08745Sheppo */ 39621ae08745Sheppo int 39631ae08745Sheppo ldc_mem_alloc_handle(ldc_handle_t handle, ldc_mem_handle_t *mhandle) 39641ae08745Sheppo { 39651ae08745Sheppo ldc_chan_t *ldcp; 39661ae08745Sheppo ldc_mhdl_t *mhdl; 39671ae08745Sheppo 39681ae08745Sheppo if (handle == NULL) { 39691ae08745Sheppo DWARN(DBG_ALL_LDCS, 39701ae08745Sheppo "ldc_mem_alloc_handle: invalid channel handle\n"); 39711ae08745Sheppo return (EINVAL); 39721ae08745Sheppo } 39731ae08745Sheppo ldcp = (ldc_chan_t *)handle; 39741ae08745Sheppo 39751ae08745Sheppo mutex_enter(&ldcp->lock); 39761ae08745Sheppo 39771ae08745Sheppo /* check to see if channel is initalized */ 39781ae08745Sheppo if (ldcp->tstate < TS_INIT) { 39791ae08745Sheppo DWARN(ldcp->id, 39801ae08745Sheppo "ldc_mem_alloc_handle: (0x%llx) channel not initialized\n", 39811ae08745Sheppo ldcp->id); 39821ae08745Sheppo mutex_exit(&ldcp->lock); 39831ae08745Sheppo return (EINVAL); 39841ae08745Sheppo } 39851ae08745Sheppo 39861ae08745Sheppo /* allocate handle for channel */ 3987*4bac2208Snarayan mhdl = kmem_cache_alloc(ldcssp->memhdl_cache, KM_SLEEP); 39881ae08745Sheppo 39891ae08745Sheppo /* initialize the lock */ 39901ae08745Sheppo mutex_init(&mhdl->lock, NULL, MUTEX_DRIVER, NULL); 39911ae08745Sheppo 3992*4bac2208Snarayan mhdl->myshadow = B_FALSE; 3993*4bac2208Snarayan mhdl->memseg = NULL; 39941ae08745Sheppo mhdl->ldcp = ldcp; 3995*4bac2208Snarayan mhdl->status = LDC_UNBOUND; 39961ae08745Sheppo 39971ae08745Sheppo /* insert memory handle (@ head) into list */ 39981ae08745Sheppo if (ldcp->mhdl_list == NULL) { 39991ae08745Sheppo ldcp->mhdl_list = mhdl; 40001ae08745Sheppo mhdl->next = NULL; 40011ae08745Sheppo } else { 40021ae08745Sheppo /* insert @ head */ 40031ae08745Sheppo mhdl->next = ldcp->mhdl_list; 40041ae08745Sheppo ldcp->mhdl_list = mhdl; 40051ae08745Sheppo } 40061ae08745Sheppo 40071ae08745Sheppo /* return the handle */ 40081ae08745Sheppo *mhandle = (ldc_mem_handle_t)mhdl; 40091ae08745Sheppo 40101ae08745Sheppo mutex_exit(&ldcp->lock); 40111ae08745Sheppo 40121ae08745Sheppo D1(ldcp->id, "ldc_mem_alloc_handle: (0x%llx) allocated handle 0x%llx\n", 40131ae08745Sheppo ldcp->id, mhdl); 40141ae08745Sheppo 40151ae08745Sheppo return (0); 40161ae08745Sheppo } 40171ae08745Sheppo 40181ae08745Sheppo /* 40191ae08745Sheppo * Free memory handle for the channel and unlink it from the list 40201ae08745Sheppo */ 40211ae08745Sheppo int 40221ae08745Sheppo ldc_mem_free_handle(ldc_mem_handle_t mhandle) 40231ae08745Sheppo { 40241ae08745Sheppo ldc_mhdl_t *mhdl, *phdl; 40251ae08745Sheppo ldc_chan_t *ldcp; 40261ae08745Sheppo 40271ae08745Sheppo if (mhandle == NULL) { 40281ae08745Sheppo DWARN(DBG_ALL_LDCS, 40291ae08745Sheppo "ldc_mem_free_handle: invalid memory handle\n"); 40301ae08745Sheppo return (EINVAL); 40311ae08745Sheppo } 40321ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 40331ae08745Sheppo 40341ae08745Sheppo mutex_enter(&mhdl->lock); 40351ae08745Sheppo 40361ae08745Sheppo ldcp = mhdl->ldcp; 40371ae08745Sheppo 40381ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED) { 40391ae08745Sheppo DWARN(ldcp->id, 40401ae08745Sheppo "ldc_mem_free_handle: cannot free, 0x%llx hdl bound\n", 40411ae08745Sheppo mhdl); 40421ae08745Sheppo mutex_exit(&mhdl->lock); 40431ae08745Sheppo return (EINVAL); 40441ae08745Sheppo } 40451ae08745Sheppo mutex_exit(&mhdl->lock); 40461ae08745Sheppo 40471ae08745Sheppo mutex_enter(&ldcp->mlist_lock); 40481ae08745Sheppo 40491ae08745Sheppo phdl = ldcp->mhdl_list; 40501ae08745Sheppo 40511ae08745Sheppo /* first handle */ 40521ae08745Sheppo if (phdl == mhdl) { 40531ae08745Sheppo ldcp->mhdl_list = mhdl->next; 40541ae08745Sheppo mutex_destroy(&mhdl->lock); 4055*4bac2208Snarayan kmem_cache_free(ldcssp->memhdl_cache, mhdl); 4056*4bac2208Snarayan 40571ae08745Sheppo D1(ldcp->id, 40581ae08745Sheppo "ldc_mem_free_handle: (0x%llx) freed handle 0x%llx\n", 40591ae08745Sheppo ldcp->id, mhdl); 40601ae08745Sheppo } else { 40611ae08745Sheppo /* walk the list - unlink and free */ 40621ae08745Sheppo while (phdl != NULL) { 40631ae08745Sheppo if (phdl->next == mhdl) { 40641ae08745Sheppo phdl->next = mhdl->next; 40651ae08745Sheppo mutex_destroy(&mhdl->lock); 4066*4bac2208Snarayan kmem_cache_free(ldcssp->memhdl_cache, mhdl); 40671ae08745Sheppo D1(ldcp->id, 40681ae08745Sheppo "ldc_mem_free_handle: (0x%llx) freed " 40691ae08745Sheppo "handle 0x%llx\n", ldcp->id, mhdl); 40701ae08745Sheppo break; 40711ae08745Sheppo } 40721ae08745Sheppo phdl = phdl->next; 40731ae08745Sheppo } 40741ae08745Sheppo } 40751ae08745Sheppo 40761ae08745Sheppo if (phdl == NULL) { 40771ae08745Sheppo DWARN(ldcp->id, 40781ae08745Sheppo "ldc_mem_free_handle: invalid handle 0x%llx\n", mhdl); 40791ae08745Sheppo mutex_exit(&ldcp->mlist_lock); 40801ae08745Sheppo return (EINVAL); 40811ae08745Sheppo } 40821ae08745Sheppo 40831ae08745Sheppo mutex_exit(&ldcp->mlist_lock); 40841ae08745Sheppo 40851ae08745Sheppo return (0); 40861ae08745Sheppo } 40871ae08745Sheppo 40881ae08745Sheppo /* 40891ae08745Sheppo * Bind a memory handle to a virtual address. 40901ae08745Sheppo * The virtual address is converted to the corresponding real addresses. 40911ae08745Sheppo * Returns pointer to the first ldc_mem_cookie and the total number 40921ae08745Sheppo * of cookies for this virtual address. Other cookies can be obtained 40931ae08745Sheppo * using the ldc_mem_nextcookie() call. If the pages are stored in 40941ae08745Sheppo * consecutive locations in the table, a single cookie corresponding to 40951ae08745Sheppo * the first location is returned. The cookie size spans all the entries. 40961ae08745Sheppo * 40971ae08745Sheppo * If the VA corresponds to a page that is already being exported, reuse 40981ae08745Sheppo * the page and do not export it again. Bump the page's use count. 40991ae08745Sheppo */ 41001ae08745Sheppo int 41011ae08745Sheppo ldc_mem_bind_handle(ldc_mem_handle_t mhandle, caddr_t vaddr, size_t len, 41021ae08745Sheppo uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount) 41031ae08745Sheppo { 41041ae08745Sheppo ldc_mhdl_t *mhdl; 41051ae08745Sheppo ldc_chan_t *ldcp; 41061ae08745Sheppo ldc_mtbl_t *mtbl; 41071ae08745Sheppo ldc_memseg_t *memseg; 41081ae08745Sheppo ldc_mte_t tmp_mte; 41091ae08745Sheppo uint64_t index, prev_index = 0; 41101ae08745Sheppo int64_t cookie_idx; 41111ae08745Sheppo uintptr_t raddr, ra_aligned; 41121ae08745Sheppo uint64_t psize, poffset, v_offset; 41131ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code, pg_mask; 41141ae08745Sheppo pgcnt_t npages; 41151ae08745Sheppo caddr_t v_align, addr; 4116*4bac2208Snarayan int i, rv; 41171ae08745Sheppo 41181ae08745Sheppo if (mhandle == NULL) { 41191ae08745Sheppo DWARN(DBG_ALL_LDCS, 41201ae08745Sheppo "ldc_mem_bind_handle: invalid memory handle\n"); 41211ae08745Sheppo return (EINVAL); 41221ae08745Sheppo } 41231ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 41241ae08745Sheppo ldcp = mhdl->ldcp; 41251ae08745Sheppo 41261ae08745Sheppo /* clear count */ 41271ae08745Sheppo *ccount = 0; 41281ae08745Sheppo 41291ae08745Sheppo mutex_enter(&mhdl->lock); 41301ae08745Sheppo 41311ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->memseg != NULL) { 41321ae08745Sheppo DWARN(ldcp->id, 41331ae08745Sheppo "ldc_mem_bind_handle: (0x%x) handle already bound\n", 41341ae08745Sheppo mhandle); 41351ae08745Sheppo mutex_exit(&mhdl->lock); 41361ae08745Sheppo return (EINVAL); 41371ae08745Sheppo } 41381ae08745Sheppo 41391ae08745Sheppo /* Force address and size to be 8-byte aligned */ 41401ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 41411ae08745Sheppo DWARN(ldcp->id, 41421ae08745Sheppo "ldc_mem_bind_handle: addr/size is not 8-byte aligned\n"); 41431ae08745Sheppo mutex_exit(&mhdl->lock); 41441ae08745Sheppo return (EINVAL); 41451ae08745Sheppo } 41461ae08745Sheppo 4147*4bac2208Snarayan /* 4148*4bac2208Snarayan * If this channel is binding a memory handle for the 4149*4bac2208Snarayan * first time allocate it a memory map table and initialize it 4150*4bac2208Snarayan */ 4151*4bac2208Snarayan if ((mtbl = ldcp->mtbl) == NULL) { 4152*4bac2208Snarayan 4153*4bac2208Snarayan mutex_enter(&ldcp->lock); 4154*4bac2208Snarayan 4155*4bac2208Snarayan /* Allocate and initialize the map table structure */ 4156*4bac2208Snarayan mtbl = kmem_zalloc(sizeof (ldc_mtbl_t), KM_SLEEP); 4157*4bac2208Snarayan mtbl->num_entries = mtbl->num_avail = ldc_maptable_entries; 4158*4bac2208Snarayan mtbl->size = ldc_maptable_entries * sizeof (ldc_mte_slot_t); 4159*4bac2208Snarayan mtbl->next_entry = NULL; 4160*4bac2208Snarayan 4161*4bac2208Snarayan /* Allocate the table itself */ 4162*4bac2208Snarayan mtbl->table = (ldc_mte_slot_t *) 4163*4bac2208Snarayan contig_mem_alloc_align(mtbl->size, MMU_PAGESIZE); 4164*4bac2208Snarayan if (mtbl->table == NULL) { 4165*4bac2208Snarayan cmn_err(CE_WARN, 4166*4bac2208Snarayan "ldc_mem_bind_handle: (0x%lx) error allocating " 4167*4bac2208Snarayan "table memory", ldcp->id); 4168*4bac2208Snarayan kmem_free(mtbl, sizeof (ldc_mtbl_t)); 4169*4bac2208Snarayan mutex_exit(&ldcp->lock); 4170*4bac2208Snarayan mutex_exit(&mhdl->lock); 4171*4bac2208Snarayan return (ENOMEM); 4172*4bac2208Snarayan } 4173*4bac2208Snarayan 4174*4bac2208Snarayan /* zero out the memory */ 4175*4bac2208Snarayan bzero(mtbl->table, mtbl->size); 4176*4bac2208Snarayan 4177*4bac2208Snarayan /* initialize the lock */ 4178*4bac2208Snarayan mutex_init(&mtbl->lock, NULL, MUTEX_DRIVER, NULL); 4179*4bac2208Snarayan 4180*4bac2208Snarayan /* register table for this channel */ 4181*4bac2208Snarayan rv = hv_ldc_set_map_table(ldcp->id, 4182*4bac2208Snarayan va_to_pa(mtbl->table), mtbl->num_entries); 4183*4bac2208Snarayan if (rv != 0) { 4184*4bac2208Snarayan cmn_err(CE_WARN, 4185*4bac2208Snarayan "ldc_mem_bind_handle: (0x%lx) err %d mapping tbl", 4186*4bac2208Snarayan ldcp->id, rv); 4187*4bac2208Snarayan contig_mem_free(mtbl->table, mtbl->size); 4188*4bac2208Snarayan mutex_destroy(&mtbl->lock); 4189*4bac2208Snarayan kmem_free(mtbl, sizeof (ldc_mtbl_t)); 4190*4bac2208Snarayan mutex_exit(&ldcp->lock); 4191*4bac2208Snarayan mutex_exit(&mhdl->lock); 4192*4bac2208Snarayan return (EIO); 4193*4bac2208Snarayan } 4194*4bac2208Snarayan 4195*4bac2208Snarayan ldcp->mtbl = mtbl; 4196*4bac2208Snarayan mutex_exit(&ldcp->lock); 4197*4bac2208Snarayan 4198*4bac2208Snarayan D1(ldcp->id, 4199*4bac2208Snarayan "ldc_mem_bind_handle: (0x%llx) alloc'd map table 0x%llx\n", 4200*4bac2208Snarayan ldcp->id, ldcp->mtbl->table); 4201*4bac2208Snarayan } 4202*4bac2208Snarayan 42031ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 42041ae08745Sheppo pg_size = MMU_PAGESIZE; 42051ae08745Sheppo pg_size_code = page_szc(pg_size); 42061ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 42071ae08745Sheppo pg_mask = ~(pg_size - 1); 42081ae08745Sheppo 42091ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: (0x%llx) binding " 42101ae08745Sheppo "va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 42111ae08745Sheppo ldcp->id, vaddr, pg_size, pg_size_code, pg_shift); 42121ae08745Sheppo 42131ae08745Sheppo /* aligned VA and its offset */ 42141ae08745Sheppo v_align = (caddr_t)(((uintptr_t)vaddr) & ~(pg_size - 1)); 42151ae08745Sheppo v_offset = ((uintptr_t)vaddr) & (pg_size - 1); 42161ae08745Sheppo 42171ae08745Sheppo npages = (len+v_offset)/pg_size; 42181ae08745Sheppo npages = ((len+v_offset)%pg_size == 0) ? npages : npages+1; 42191ae08745Sheppo 42201ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: binding " 42211ae08745Sheppo "(0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n", 42221ae08745Sheppo ldcp->id, vaddr, v_align, v_offset, npages); 42231ae08745Sheppo 42241ae08745Sheppo /* lock the memory table - exclusive access to channel */ 42251ae08745Sheppo mutex_enter(&mtbl->lock); 42261ae08745Sheppo 42271ae08745Sheppo if (npages > mtbl->num_avail) { 42281ae08745Sheppo DWARN(ldcp->id, 42291ae08745Sheppo "ldc_mem_bind_handle: (0x%llx) no table entries\n", 42301ae08745Sheppo ldcp->id); 42311ae08745Sheppo mutex_exit(&mtbl->lock); 42321ae08745Sheppo mutex_exit(&mhdl->lock); 42331ae08745Sheppo return (ENOMEM); 42341ae08745Sheppo } 42351ae08745Sheppo 42361ae08745Sheppo /* Allocate a memseg structure */ 4237*4bac2208Snarayan memseg = mhdl->memseg = 4238*4bac2208Snarayan kmem_cache_alloc(ldcssp->memseg_cache, KM_SLEEP); 42391ae08745Sheppo 42401ae08745Sheppo /* Allocate memory to store all pages and cookies */ 42411ae08745Sheppo memseg->pages = kmem_zalloc((sizeof (ldc_page_t) * npages), KM_SLEEP); 42421ae08745Sheppo memseg->cookies = 42431ae08745Sheppo kmem_zalloc((sizeof (ldc_mem_cookie_t) * npages), KM_SLEEP); 42441ae08745Sheppo 42451ae08745Sheppo D2(ldcp->id, "ldc_mem_bind_handle: (0x%llx) processing 0x%llx pages\n", 42461ae08745Sheppo ldcp->id, npages); 42471ae08745Sheppo 42481ae08745Sheppo addr = v_align; 42491ae08745Sheppo 42501ae08745Sheppo /* 4251*4bac2208Snarayan * Check if direct shared memory map is enabled, if not change 4252*4bac2208Snarayan * the mapping type to include SHADOW_MAP. 4253*4bac2208Snarayan */ 4254*4bac2208Snarayan if (ldc_shmem_enabled == 0) 4255*4bac2208Snarayan mtype = LDC_SHADOW_MAP; 4256*4bac2208Snarayan 4257*4bac2208Snarayan /* 42581ae08745Sheppo * Table slots are used in a round-robin manner. The algorithm permits 42591ae08745Sheppo * inserting duplicate entries. Slots allocated earlier will typically 42601ae08745Sheppo * get freed before we get back to reusing the slot.Inserting duplicate 42611ae08745Sheppo * entries should be OK as we only lookup entries using the cookie addr 42621ae08745Sheppo * i.e. tbl index, during export, unexport and copy operation. 42631ae08745Sheppo * 42641ae08745Sheppo * One implementation what was tried was to search for a duplicate 42651ae08745Sheppo * page entry first and reuse it. The search overhead is very high and 42661ae08745Sheppo * in the vnet case dropped the perf by almost half, 50 to 24 mbps. 42671ae08745Sheppo * So it does make sense to avoid searching for duplicates. 42681ae08745Sheppo * 42691ae08745Sheppo * But during the process of searching for a free slot, if we find a 42701ae08745Sheppo * duplicate entry we will go ahead and use it, and bump its use count. 42711ae08745Sheppo */ 42721ae08745Sheppo 42731ae08745Sheppo /* index to start searching from */ 42741ae08745Sheppo index = mtbl->next_entry; 42751ae08745Sheppo cookie_idx = -1; 42761ae08745Sheppo 42771ae08745Sheppo tmp_mte.ll = 0; /* initialise fields to 0 */ 42781ae08745Sheppo 42791ae08745Sheppo if (mtype & LDC_DIRECT_MAP) { 42801ae08745Sheppo tmp_mte.mte_r = (perm & LDC_MEM_R) ? 1 : 0; 42811ae08745Sheppo tmp_mte.mte_w = (perm & LDC_MEM_W) ? 1 : 0; 42821ae08745Sheppo tmp_mte.mte_x = (perm & LDC_MEM_X) ? 1 : 0; 42831ae08745Sheppo } 42841ae08745Sheppo 42851ae08745Sheppo if (mtype & LDC_SHADOW_MAP) { 42861ae08745Sheppo tmp_mte.mte_cr = (perm & LDC_MEM_R) ? 1 : 0; 42871ae08745Sheppo tmp_mte.mte_cw = (perm & LDC_MEM_W) ? 1 : 0; 42881ae08745Sheppo } 42891ae08745Sheppo 42901ae08745Sheppo if (mtype & LDC_IO_MAP) { 42911ae08745Sheppo tmp_mte.mte_ir = (perm & LDC_MEM_R) ? 1 : 0; 42921ae08745Sheppo tmp_mte.mte_iw = (perm & LDC_MEM_W) ? 1 : 0; 42931ae08745Sheppo } 42941ae08745Sheppo 42951ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle mte=0x%llx\n", tmp_mte.ll); 42961ae08745Sheppo 42971ae08745Sheppo tmp_mte.mte_pgszc = pg_size_code; 42981ae08745Sheppo 42991ae08745Sheppo /* initialize each mem table entry */ 43001ae08745Sheppo for (i = 0; i < npages; i++) { 43011ae08745Sheppo 43021ae08745Sheppo /* check if slot is available in the table */ 43031ae08745Sheppo while (mtbl->table[index].entry.ll != 0) { 43041ae08745Sheppo 43051ae08745Sheppo index = (index + 1) % mtbl->num_entries; 43061ae08745Sheppo 43071ae08745Sheppo if (index == mtbl->next_entry) { 43081ae08745Sheppo /* we have looped around */ 43091ae08745Sheppo DWARN(DBG_ALL_LDCS, 43101ae08745Sheppo "ldc_mem_bind_handle: (0x%llx) cannot find " 43111ae08745Sheppo "entry\n", ldcp->id); 43121ae08745Sheppo *ccount = 0; 43131ae08745Sheppo 43141ae08745Sheppo /* NOTE: free memory, remove previous entries */ 43151ae08745Sheppo /* this shouldnt happen as num_avail was ok */ 43161ae08745Sheppo 43171ae08745Sheppo mutex_exit(&mtbl->lock); 43181ae08745Sheppo mutex_exit(&mhdl->lock); 43191ae08745Sheppo return (ENOMEM); 43201ae08745Sheppo } 43211ae08745Sheppo } 43221ae08745Sheppo 43231ae08745Sheppo /* get the real address */ 43241ae08745Sheppo raddr = va_to_pa((void *)addr); 43251ae08745Sheppo ra_aligned = ((uintptr_t)raddr & pg_mask); 43261ae08745Sheppo 43271ae08745Sheppo /* build the mte */ 43281ae08745Sheppo tmp_mte.mte_rpfn = ra_aligned >> pg_shift; 43291ae08745Sheppo 43301ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle mte=0x%llx\n", tmp_mte.ll); 43311ae08745Sheppo 43321ae08745Sheppo /* update entry in table */ 43331ae08745Sheppo mtbl->table[index].entry = tmp_mte; 43341ae08745Sheppo 43351ae08745Sheppo D2(ldcp->id, "ldc_mem_bind_handle: (0x%llx) stored MTE 0x%llx" 43361ae08745Sheppo " into loc 0x%llx\n", ldcp->id, tmp_mte.ll, index); 43371ae08745Sheppo 43381ae08745Sheppo /* calculate the size and offset for this export range */ 43391ae08745Sheppo if (i == 0) { 43401ae08745Sheppo /* first page */ 43411ae08745Sheppo psize = min((pg_size - v_offset), len); 43421ae08745Sheppo poffset = v_offset; 43431ae08745Sheppo 43441ae08745Sheppo } else if (i == (npages - 1)) { 43451ae08745Sheppo /* last page */ 43461ae08745Sheppo psize = (((uintptr_t)(vaddr + len)) & 43471ae08745Sheppo ((uint64_t)(pg_size-1))); 43481ae08745Sheppo if (psize == 0) 43491ae08745Sheppo psize = pg_size; 43501ae08745Sheppo poffset = 0; 43511ae08745Sheppo 43521ae08745Sheppo } else { 43531ae08745Sheppo /* middle pages */ 43541ae08745Sheppo psize = pg_size; 43551ae08745Sheppo poffset = 0; 43561ae08745Sheppo } 43571ae08745Sheppo 43581ae08745Sheppo /* store entry for this page */ 43591ae08745Sheppo memseg->pages[i].index = index; 43601ae08745Sheppo memseg->pages[i].raddr = raddr; 43611ae08745Sheppo memseg->pages[i].offset = poffset; 43621ae08745Sheppo memseg->pages[i].size = psize; 43631ae08745Sheppo memseg->pages[i].mte = &(mtbl->table[index]); 43641ae08745Sheppo 43651ae08745Sheppo /* create the cookie */ 43661ae08745Sheppo if (i == 0 || (index != prev_index + 1)) { 43671ae08745Sheppo cookie_idx++; 43681ae08745Sheppo memseg->cookies[cookie_idx].addr = 43691ae08745Sheppo IDX2COOKIE(index, pg_size_code, pg_shift); 43701ae08745Sheppo memseg->cookies[cookie_idx].addr |= poffset; 43711ae08745Sheppo memseg->cookies[cookie_idx].size = psize; 43721ae08745Sheppo 43731ae08745Sheppo } else { 43741ae08745Sheppo memseg->cookies[cookie_idx].size += psize; 43751ae08745Sheppo } 43761ae08745Sheppo 43771ae08745Sheppo D1(ldcp->id, "ldc_mem_bind_handle: bound " 43781ae08745Sheppo "(0x%llx) va=0x%llx, idx=0x%llx, " 43791ae08745Sheppo "ra=0x%llx(sz=0x%x,off=0x%x)\n", 43801ae08745Sheppo ldcp->id, addr, index, raddr, psize, poffset); 43811ae08745Sheppo 43821ae08745Sheppo /* decrement number of available entries */ 43831ae08745Sheppo mtbl->num_avail--; 43841ae08745Sheppo 43851ae08745Sheppo /* increment va by page size */ 43861ae08745Sheppo addr += pg_size; 43871ae08745Sheppo 43881ae08745Sheppo /* increment index */ 43891ae08745Sheppo prev_index = index; 43901ae08745Sheppo index = (index + 1) % mtbl->num_entries; 43911ae08745Sheppo 43921ae08745Sheppo /* save the next slot */ 43931ae08745Sheppo mtbl->next_entry = index; 43941ae08745Sheppo } 43951ae08745Sheppo 43961ae08745Sheppo mutex_exit(&mtbl->lock); 43971ae08745Sheppo 43981ae08745Sheppo /* memory handle = bound */ 43991ae08745Sheppo mhdl->mtype = mtype; 44001ae08745Sheppo mhdl->perm = perm; 44011ae08745Sheppo mhdl->status = LDC_BOUND; 44021ae08745Sheppo 44031ae08745Sheppo /* update memseg_t */ 44041ae08745Sheppo memseg->vaddr = vaddr; 44051ae08745Sheppo memseg->raddr = memseg->pages[0].raddr; 44061ae08745Sheppo memseg->size = len; 44071ae08745Sheppo memseg->npages = npages; 44081ae08745Sheppo memseg->ncookies = cookie_idx + 1; 44091ae08745Sheppo memseg->next_cookie = (memseg->ncookies > 1) ? 1 : 0; 44101ae08745Sheppo 44111ae08745Sheppo /* return count and first cookie */ 44121ae08745Sheppo *ccount = memseg->ncookies; 44131ae08745Sheppo cookie->addr = memseg->cookies[0].addr; 44141ae08745Sheppo cookie->size = memseg->cookies[0].size; 44151ae08745Sheppo 44161ae08745Sheppo D1(ldcp->id, 44171ae08745Sheppo "ldc_mem_bind_handle: (0x%llx) bound 0x%llx, va=0x%llx, " 44181ae08745Sheppo "pgs=0x%llx cookies=0x%llx\n", 44191ae08745Sheppo ldcp->id, mhdl, vaddr, npages, memseg->ncookies); 44201ae08745Sheppo 44211ae08745Sheppo mutex_exit(&mhdl->lock); 44221ae08745Sheppo return (0); 44231ae08745Sheppo } 44241ae08745Sheppo 44251ae08745Sheppo /* 44261ae08745Sheppo * Return the next cookie associated with the specified memory handle 44271ae08745Sheppo */ 44281ae08745Sheppo int 44291ae08745Sheppo ldc_mem_nextcookie(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie) 44301ae08745Sheppo { 44311ae08745Sheppo ldc_mhdl_t *mhdl; 44321ae08745Sheppo ldc_chan_t *ldcp; 44331ae08745Sheppo ldc_memseg_t *memseg; 44341ae08745Sheppo 44351ae08745Sheppo if (mhandle == NULL) { 44361ae08745Sheppo DWARN(DBG_ALL_LDCS, 44371ae08745Sheppo "ldc_mem_nextcookie: invalid memory handle\n"); 44381ae08745Sheppo return (EINVAL); 44391ae08745Sheppo } 44401ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 44411ae08745Sheppo 44421ae08745Sheppo mutex_enter(&mhdl->lock); 44431ae08745Sheppo 44441ae08745Sheppo ldcp = mhdl->ldcp; 44451ae08745Sheppo memseg = mhdl->memseg; 44461ae08745Sheppo 44471ae08745Sheppo if (cookie == 0) { 44481ae08745Sheppo DWARN(ldcp->id, 44491ae08745Sheppo "ldc_mem_nextcookie:(0x%llx) invalid cookie arg\n", 44501ae08745Sheppo ldcp->id); 44511ae08745Sheppo mutex_exit(&mhdl->lock); 44521ae08745Sheppo return (EINVAL); 44531ae08745Sheppo } 44541ae08745Sheppo 44551ae08745Sheppo if (memseg->next_cookie != 0) { 44561ae08745Sheppo cookie->addr = memseg->cookies[memseg->next_cookie].addr; 44571ae08745Sheppo cookie->size = memseg->cookies[memseg->next_cookie].size; 44581ae08745Sheppo memseg->next_cookie++; 44591ae08745Sheppo if (memseg->next_cookie == memseg->ncookies) 44601ae08745Sheppo memseg->next_cookie = 0; 44611ae08745Sheppo 44621ae08745Sheppo } else { 44631ae08745Sheppo DWARN(ldcp->id, 44641ae08745Sheppo "ldc_mem_nextcookie:(0x%llx) no more cookies\n", ldcp->id); 44651ae08745Sheppo cookie->addr = 0; 44661ae08745Sheppo cookie->size = 0; 44671ae08745Sheppo mutex_exit(&mhdl->lock); 44681ae08745Sheppo return (EINVAL); 44691ae08745Sheppo } 44701ae08745Sheppo 44711ae08745Sheppo D1(ldcp->id, 44721ae08745Sheppo "ldc_mem_nextcookie: (0x%llx) cookie addr=0x%llx,sz=0x%llx\n", 44731ae08745Sheppo ldcp->id, cookie->addr, cookie->size); 44741ae08745Sheppo 44751ae08745Sheppo mutex_exit(&mhdl->lock); 44761ae08745Sheppo return (0); 44771ae08745Sheppo } 44781ae08745Sheppo 44791ae08745Sheppo /* 44801ae08745Sheppo * Unbind the virtual memory region associated with the specified 44811ae08745Sheppo * memory handle. Allassociated cookies are freed and the corresponding 44821ae08745Sheppo * RA space is no longer exported. 44831ae08745Sheppo */ 44841ae08745Sheppo int 44851ae08745Sheppo ldc_mem_unbind_handle(ldc_mem_handle_t mhandle) 44861ae08745Sheppo { 44871ae08745Sheppo ldc_mhdl_t *mhdl; 44881ae08745Sheppo ldc_chan_t *ldcp; 44891ae08745Sheppo ldc_mtbl_t *mtbl; 44901ae08745Sheppo ldc_memseg_t *memseg; 4491*4bac2208Snarayan uint64_t cookie_addr; 4492*4bac2208Snarayan uint64_t pg_shift, pg_size_code; 4493*4bac2208Snarayan int i, rv; 44941ae08745Sheppo 44951ae08745Sheppo if (mhandle == NULL) { 44961ae08745Sheppo DWARN(DBG_ALL_LDCS, 44971ae08745Sheppo "ldc_mem_unbind_handle: invalid memory handle\n"); 44981ae08745Sheppo return (EINVAL); 44991ae08745Sheppo } 45001ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 45011ae08745Sheppo 45021ae08745Sheppo mutex_enter(&mhdl->lock); 45031ae08745Sheppo 45041ae08745Sheppo if (mhdl->status == LDC_UNBOUND) { 45051ae08745Sheppo DWARN(DBG_ALL_LDCS, 45061ae08745Sheppo "ldc_mem_unbind_handle: (0x%x) handle is not bound\n", 45071ae08745Sheppo mhandle); 45081ae08745Sheppo mutex_exit(&mhdl->lock); 45091ae08745Sheppo return (EINVAL); 45101ae08745Sheppo } 45111ae08745Sheppo 45121ae08745Sheppo ldcp = mhdl->ldcp; 45131ae08745Sheppo mtbl = ldcp->mtbl; 45141ae08745Sheppo 45151ae08745Sheppo memseg = mhdl->memseg; 45161ae08745Sheppo 45171ae08745Sheppo /* lock the memory table - exclusive access to channel */ 45181ae08745Sheppo mutex_enter(&mtbl->lock); 45191ae08745Sheppo 45201ae08745Sheppo /* undo the pages exported */ 45211ae08745Sheppo for (i = 0; i < memseg->npages; i++) { 45221ae08745Sheppo 4523*4bac2208Snarayan /* check for mapped pages, revocation cookie != 0 */ 45241ae08745Sheppo if (memseg->pages[i].mte->cookie) { 4525*4bac2208Snarayan 4526*4bac2208Snarayan pg_size_code = page_szc(memseg->pages[i].size); 4527*4bac2208Snarayan pg_shift = page_get_shift(memseg->pages[i].size); 4528*4bac2208Snarayan cookie_addr = IDX2COOKIE(memseg->pages[i].index, 4529*4bac2208Snarayan pg_size_code, pg_shift); 4530*4bac2208Snarayan 4531*4bac2208Snarayan D1(ldcp->id, "ldc_mem_unbind_handle: (0x%llx) revoke " 4532*4bac2208Snarayan "cookie 0x%llx, rcookie 0x%llx\n", ldcp->id, 4533*4bac2208Snarayan cookie_addr, memseg->pages[i].mte->cookie); 4534*4bac2208Snarayan rv = hv_ldc_revoke(ldcp->id, cookie_addr, 4535*4bac2208Snarayan memseg->pages[i].mte->cookie); 4536*4bac2208Snarayan if (rv) { 4537*4bac2208Snarayan DWARN(ldcp->id, 4538*4bac2208Snarayan "ldc_mem_unbind_handle: (0x%llx) cannot " 4539*4bac2208Snarayan "revoke mapping, cookie %llx\n", ldcp->id, 4540*4bac2208Snarayan cookie_addr); 4541*4bac2208Snarayan } 45421ae08745Sheppo } 45431ae08745Sheppo 45441ae08745Sheppo /* clear the entry from the table */ 45451ae08745Sheppo memseg->pages[i].mte->entry.ll = 0; 45461ae08745Sheppo mtbl->num_avail++; 45471ae08745Sheppo } 45481ae08745Sheppo mutex_exit(&mtbl->lock); 45491ae08745Sheppo 45501ae08745Sheppo /* free the allocated memseg and page structures */ 45511ae08745Sheppo kmem_free(memseg->pages, (sizeof (ldc_page_t) * memseg->npages)); 45521ae08745Sheppo kmem_free(memseg->cookies, 45531ae08745Sheppo (sizeof (ldc_mem_cookie_t) * memseg->npages)); 4554*4bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 45551ae08745Sheppo 45561ae08745Sheppo /* uninitialize the memory handle */ 45571ae08745Sheppo mhdl->memseg = NULL; 45581ae08745Sheppo mhdl->status = LDC_UNBOUND; 45591ae08745Sheppo 45601ae08745Sheppo D1(ldcp->id, "ldc_mem_unbind_handle: (0x%llx) unbound handle 0x%llx\n", 45611ae08745Sheppo ldcp->id, mhdl); 45621ae08745Sheppo 45631ae08745Sheppo mutex_exit(&mhdl->lock); 45641ae08745Sheppo return (0); 45651ae08745Sheppo } 45661ae08745Sheppo 45671ae08745Sheppo /* 45681ae08745Sheppo * Get information about the dring. The base address of the descriptor 45691ae08745Sheppo * ring along with the type and permission are returned back. 45701ae08745Sheppo */ 45711ae08745Sheppo int 45721ae08745Sheppo ldc_mem_info(ldc_mem_handle_t mhandle, ldc_mem_info_t *minfo) 45731ae08745Sheppo { 45741ae08745Sheppo ldc_mhdl_t *mhdl; 45751ae08745Sheppo 45761ae08745Sheppo if (mhandle == NULL) { 45771ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_info: invalid memory handle\n"); 45781ae08745Sheppo return (EINVAL); 45791ae08745Sheppo } 45801ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 45811ae08745Sheppo 45821ae08745Sheppo if (minfo == NULL) { 45831ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_info: invalid args\n"); 45841ae08745Sheppo return (EINVAL); 45851ae08745Sheppo } 45861ae08745Sheppo 45871ae08745Sheppo mutex_enter(&mhdl->lock); 45881ae08745Sheppo 45891ae08745Sheppo minfo->status = mhdl->status; 45901ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED) { 45911ae08745Sheppo minfo->vaddr = mhdl->memseg->vaddr; 45921ae08745Sheppo minfo->raddr = mhdl->memseg->raddr; 45931ae08745Sheppo minfo->mtype = mhdl->mtype; 45941ae08745Sheppo minfo->perm = mhdl->perm; 45951ae08745Sheppo } 45961ae08745Sheppo mutex_exit(&mhdl->lock); 45971ae08745Sheppo 45981ae08745Sheppo return (0); 45991ae08745Sheppo } 46001ae08745Sheppo 46011ae08745Sheppo /* 46021ae08745Sheppo * Copy data either from or to the client specified virtual address 46031ae08745Sheppo * space to or from the exported memory associated with the cookies. 46041ae08745Sheppo * The direction argument determines whether the data is read from or 46051ae08745Sheppo * written to exported memory. 46061ae08745Sheppo */ 46071ae08745Sheppo int 46081ae08745Sheppo ldc_mem_copy(ldc_handle_t handle, caddr_t vaddr, uint64_t off, size_t *size, 46091ae08745Sheppo ldc_mem_cookie_t *cookies, uint32_t ccount, uint8_t direction) 46101ae08745Sheppo { 46111ae08745Sheppo ldc_chan_t *ldcp; 46121ae08745Sheppo uint64_t local_voff, local_valign; 46131ae08745Sheppo uint64_t cookie_addr, cookie_size; 46141ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code; 46151ae08745Sheppo uint64_t export_caddr, export_poff, export_psize, export_size; 46161ae08745Sheppo uint64_t local_ra, local_poff, local_psize; 46171ae08745Sheppo uint64_t copy_size, copied_len = 0, total_bal = 0, idx = 0; 46181ae08745Sheppo pgcnt_t npages; 46191ae08745Sheppo size_t len = *size; 46201ae08745Sheppo int i, rv = 0; 46211ae08745Sheppo 46221ae08745Sheppo if (handle == NULL) { 46231ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_copy: invalid channel handle\n"); 46241ae08745Sheppo return (EINVAL); 46251ae08745Sheppo } 46261ae08745Sheppo ldcp = (ldc_chan_t *)handle; 46271ae08745Sheppo 46281ae08745Sheppo mutex_enter(&ldcp->lock); 46291ae08745Sheppo 46301ae08745Sheppo /* check to see if channel is UP */ 46311ae08745Sheppo if (ldcp->tstate != TS_UP) { 46321ae08745Sheppo DWARN(ldcp->id, "ldc_mem_copy: (0x%llx) channel is not UP\n", 46331ae08745Sheppo ldcp->id); 46341ae08745Sheppo mutex_exit(&ldcp->lock); 46351ae08745Sheppo return (EINVAL); 46361ae08745Sheppo } 46371ae08745Sheppo 46381ae08745Sheppo /* Force address and size to be 8-byte aligned */ 46391ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 46401ae08745Sheppo DWARN(ldcp->id, 46411ae08745Sheppo "ldc_mem_copy: addr/sz is not 8-byte aligned\n"); 46421ae08745Sheppo mutex_exit(&ldcp->lock); 46431ae08745Sheppo return (EINVAL); 46441ae08745Sheppo } 46451ae08745Sheppo 46461ae08745Sheppo /* Find the size of the exported memory */ 46471ae08745Sheppo export_size = 0; 46481ae08745Sheppo for (i = 0; i < ccount; i++) 46491ae08745Sheppo export_size += cookies[i].size; 46501ae08745Sheppo 46511ae08745Sheppo /* check to see if offset is valid */ 46521ae08745Sheppo if (off > export_size) { 46531ae08745Sheppo DWARN(ldcp->id, 46541ae08745Sheppo "ldc_mem_copy: (0x%llx) start offset > export mem size\n", 46551ae08745Sheppo ldcp->id); 46561ae08745Sheppo mutex_exit(&ldcp->lock); 46571ae08745Sheppo return (EINVAL); 46581ae08745Sheppo } 46591ae08745Sheppo 46601ae08745Sheppo /* 46611ae08745Sheppo * Check to see if the export size is smaller than the size we 46621ae08745Sheppo * are requesting to copy - if so flag an error 46631ae08745Sheppo */ 46641ae08745Sheppo if ((export_size - off) < *size) { 46651ae08745Sheppo DWARN(ldcp->id, 46661ae08745Sheppo "ldc_mem_copy: (0x%llx) copy size > export mem size\n", 46671ae08745Sheppo ldcp->id); 46681ae08745Sheppo mutex_exit(&ldcp->lock); 46691ae08745Sheppo return (EINVAL); 46701ae08745Sheppo } 46711ae08745Sheppo 46721ae08745Sheppo total_bal = min(export_size, *size); 46731ae08745Sheppo 46741ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 46751ae08745Sheppo pg_size = MMU_PAGESIZE; 46761ae08745Sheppo pg_size_code = page_szc(pg_size); 46771ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 46781ae08745Sheppo 46791ae08745Sheppo D1(ldcp->id, "ldc_mem_copy: copying data " 46801ae08745Sheppo "(0x%llx) va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 46811ae08745Sheppo ldcp->id, vaddr, pg_size, pg_size_code, pg_shift); 46821ae08745Sheppo 46831ae08745Sheppo /* aligned VA and its offset */ 46841ae08745Sheppo local_valign = (((uintptr_t)vaddr) & ~(pg_size - 1)); 46851ae08745Sheppo local_voff = ((uintptr_t)vaddr) & (pg_size - 1); 46861ae08745Sheppo 46871ae08745Sheppo npages = (len+local_voff)/pg_size; 46881ae08745Sheppo npages = ((len+local_voff)%pg_size == 0) ? npages : npages+1; 46891ae08745Sheppo 46901ae08745Sheppo D1(ldcp->id, 46911ae08745Sheppo "ldc_mem_copy: (0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n", 46921ae08745Sheppo ldcp->id, vaddr, local_valign, local_voff, npages); 46931ae08745Sheppo 46941ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 46951ae08745Sheppo local_poff = local_voff; 46961ae08745Sheppo local_psize = min(len, (pg_size - local_voff)); 46971ae08745Sheppo 46981ae08745Sheppo len -= local_psize; 46991ae08745Sheppo 47001ae08745Sheppo /* 47011ae08745Sheppo * find the first cookie in the list of cookies 47021ae08745Sheppo * if the offset passed in is not zero 47031ae08745Sheppo */ 47041ae08745Sheppo for (idx = 0; idx < ccount; idx++) { 47051ae08745Sheppo cookie_size = cookies[idx].size; 47061ae08745Sheppo if (off < cookie_size) 47071ae08745Sheppo break; 47081ae08745Sheppo off -= cookie_size; 47091ae08745Sheppo } 47101ae08745Sheppo 47111ae08745Sheppo cookie_addr = cookies[idx].addr + off; 47121ae08745Sheppo cookie_size = cookies[idx].size - off; 47131ae08745Sheppo 47141ae08745Sheppo export_caddr = cookie_addr & ~(pg_size - 1); 47151ae08745Sheppo export_poff = cookie_addr & (pg_size - 1); 47161ae08745Sheppo export_psize = min(cookie_size, (pg_size - export_poff)); 47171ae08745Sheppo 47181ae08745Sheppo for (;;) { 47191ae08745Sheppo 47201ae08745Sheppo copy_size = min(export_psize, local_psize); 47211ae08745Sheppo 47221ae08745Sheppo D1(ldcp->id, 47231ae08745Sheppo "ldc_mem_copy:(0x%llx) dir=0x%x, caddr=0x%llx," 47241ae08745Sheppo " loc_ra=0x%llx, exp_poff=0x%llx, loc_poff=0x%llx," 47251ae08745Sheppo " exp_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx," 47261ae08745Sheppo " total_bal=0x%llx\n", 47271ae08745Sheppo ldcp->id, direction, export_caddr, local_ra, export_poff, 47281ae08745Sheppo local_poff, export_psize, local_psize, copy_size, 47291ae08745Sheppo total_bal); 47301ae08745Sheppo 47311ae08745Sheppo rv = hv_ldc_copy(ldcp->id, direction, 47321ae08745Sheppo (export_caddr + export_poff), (local_ra + local_poff), 47331ae08745Sheppo copy_size, &copied_len); 47341ae08745Sheppo 47351ae08745Sheppo if (rv != 0) { 47361ae08745Sheppo cmn_err(CE_WARN, 47371ae08745Sheppo "ldc_mem_copy: (0x%lx) err %d during copy\n", 47381ae08745Sheppo ldcp->id, rv); 4739*4bac2208Snarayan DWARN(ldcp->id, 4740*4bac2208Snarayan "ldc_mem_copy: (0x%lx) dir=0x%x, caddr=0x%lx, " 4741*4bac2208Snarayan "loc_ra=0x%lx, exp_poff=0x%lx, loc_poff=0x%lx," 4742*4bac2208Snarayan " exp_psz=0x%lx, loc_psz=0x%lx, copy_sz=0x%lx," 4743*4bac2208Snarayan " copied_len=0x%lx, total_bal=0x%lx\n", 47441ae08745Sheppo ldcp->id, direction, export_caddr, local_ra, 47451ae08745Sheppo export_poff, local_poff, export_psize, local_psize, 47461ae08745Sheppo copy_size, copied_len, total_bal); 47471ae08745Sheppo 47481ae08745Sheppo *size = *size - total_bal; 47491ae08745Sheppo mutex_exit(&ldcp->lock); 47501ae08745Sheppo return (EIO); 47511ae08745Sheppo } 47521ae08745Sheppo 47531ae08745Sheppo ASSERT(copied_len <= copy_size); 47541ae08745Sheppo 47551ae08745Sheppo D2(ldcp->id, "ldc_mem_copy: copied=0x%llx\n", copied_len); 47561ae08745Sheppo export_poff += copied_len; 47571ae08745Sheppo local_poff += copied_len; 47581ae08745Sheppo export_psize -= copied_len; 47591ae08745Sheppo local_psize -= copied_len; 47601ae08745Sheppo cookie_size -= copied_len; 47611ae08745Sheppo 47621ae08745Sheppo total_bal -= copied_len; 47631ae08745Sheppo 47641ae08745Sheppo if (copy_size != copied_len) 47651ae08745Sheppo continue; 47661ae08745Sheppo 47671ae08745Sheppo if (export_psize == 0 && total_bal != 0) { 47681ae08745Sheppo 47691ae08745Sheppo if (cookie_size == 0) { 47701ae08745Sheppo idx++; 47711ae08745Sheppo cookie_addr = cookies[idx].addr; 47721ae08745Sheppo cookie_size = cookies[idx].size; 47731ae08745Sheppo 47741ae08745Sheppo export_caddr = cookie_addr & ~(pg_size - 1); 47751ae08745Sheppo export_poff = cookie_addr & (pg_size - 1); 47761ae08745Sheppo export_psize = 47771ae08745Sheppo min(cookie_size, (pg_size-export_poff)); 47781ae08745Sheppo } else { 47791ae08745Sheppo export_caddr += pg_size; 47801ae08745Sheppo export_poff = 0; 47811ae08745Sheppo export_psize = min(cookie_size, pg_size); 47821ae08745Sheppo } 47831ae08745Sheppo } 47841ae08745Sheppo 47851ae08745Sheppo if (local_psize == 0 && total_bal != 0) { 47861ae08745Sheppo local_valign += pg_size; 47871ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 47881ae08745Sheppo local_poff = 0; 47891ae08745Sheppo local_psize = min(pg_size, len); 47901ae08745Sheppo len -= local_psize; 47911ae08745Sheppo } 47921ae08745Sheppo 47931ae08745Sheppo /* check if we are all done */ 47941ae08745Sheppo if (total_bal == 0) 47951ae08745Sheppo break; 47961ae08745Sheppo } 47971ae08745Sheppo 47981ae08745Sheppo mutex_exit(&ldcp->lock); 47991ae08745Sheppo 48001ae08745Sheppo D1(ldcp->id, 48011ae08745Sheppo "ldc_mem_copy: (0x%llx) done copying sz=0x%llx\n", 48021ae08745Sheppo ldcp->id, *size); 48031ae08745Sheppo 48041ae08745Sheppo return (0); 48051ae08745Sheppo } 48061ae08745Sheppo 48071ae08745Sheppo /* 48081ae08745Sheppo * Copy data either from or to the client specified virtual address 48091ae08745Sheppo * space to or from HV physical memory. 48101ae08745Sheppo * 48111ae08745Sheppo * The direction argument determines whether the data is read from or 48121ae08745Sheppo * written to HV memory. direction values are LDC_COPY_IN/OUT similar 48131ae08745Sheppo * to the ldc_mem_copy interface 48141ae08745Sheppo */ 48151ae08745Sheppo int 48161ae08745Sheppo ldc_mem_rdwr_pa(ldc_handle_t handle, caddr_t vaddr, size_t *size, 48171ae08745Sheppo caddr_t paddr, uint8_t direction) 48181ae08745Sheppo { 48191ae08745Sheppo ldc_chan_t *ldcp; 48201ae08745Sheppo uint64_t local_voff, local_valign; 48211ae08745Sheppo uint64_t pg_shift, pg_size, pg_size_code; 48221ae08745Sheppo uint64_t target_pa, target_poff, target_psize, target_size; 48231ae08745Sheppo uint64_t local_ra, local_poff, local_psize; 48241ae08745Sheppo uint64_t copy_size, copied_len = 0; 48251ae08745Sheppo pgcnt_t npages; 48261ae08745Sheppo size_t len = *size; 48271ae08745Sheppo int rv = 0; 48281ae08745Sheppo 48291ae08745Sheppo if (handle == NULL) { 48301ae08745Sheppo DWARN(DBG_ALL_LDCS, 48311ae08745Sheppo "ldc_mem_rdwr_pa: invalid channel handle\n"); 48321ae08745Sheppo return (EINVAL); 48331ae08745Sheppo } 48341ae08745Sheppo ldcp = (ldc_chan_t *)handle; 48351ae08745Sheppo 48361ae08745Sheppo mutex_enter(&ldcp->lock); 48371ae08745Sheppo 48381ae08745Sheppo /* check to see if channel is UP */ 48391ae08745Sheppo if (ldcp->tstate != TS_UP) { 48401ae08745Sheppo DWARN(ldcp->id, 48411ae08745Sheppo "ldc_mem_rdwr_pa: (0x%llx) channel is not UP\n", 48421ae08745Sheppo ldcp->id); 48431ae08745Sheppo mutex_exit(&ldcp->lock); 48441ae08745Sheppo return (EINVAL); 48451ae08745Sheppo } 48461ae08745Sheppo 48471ae08745Sheppo /* Force address and size to be 8-byte aligned */ 48481ae08745Sheppo if ((((uintptr_t)vaddr | len) & 0x7) != 0) { 48491ae08745Sheppo DWARN(ldcp->id, 48501ae08745Sheppo "ldc_mem_rdwr_pa: addr/size is not 8-byte aligned\n"); 48511ae08745Sheppo mutex_exit(&ldcp->lock); 48521ae08745Sheppo return (EINVAL); 48531ae08745Sheppo } 48541ae08745Sheppo 48551ae08745Sheppo target_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 48621ae08745Sheppo D1(ldcp->id, "ldc_mem_rdwr_pa: copying data " 48631ae08745Sheppo "(0x%llx) va 0x%llx pgsz=0x%llx, pgszc=0x%llx, pg_shift=0x%llx\n", 48641ae08745Sheppo ldcp->id, 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 48731ae08745Sheppo D1(ldcp->id, 48741ae08745Sheppo "ldc_mem_rdwr_pa: (0x%llx) v=0x%llx,val=0x%llx,off=0x%x,pgs=0x%x\n", 48751ae08745Sheppo ldcp->id, 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 target_pa = ((uintptr_t)paddr) & ~(pg_size - 1); 48841ae08745Sheppo target_poff = ((uintptr_t)paddr) & (pg_size - 1); 48851ae08745Sheppo target_psize = pg_size - target_poff; 48861ae08745Sheppo 48871ae08745Sheppo for (;;) { 48881ae08745Sheppo 48891ae08745Sheppo copy_size = min(target_psize, local_psize); 48901ae08745Sheppo 48911ae08745Sheppo D1(ldcp->id, 48921ae08745Sheppo "ldc_mem_rdwr_pa: (0x%llx) dir=0x%x, tar_pa=0x%llx," 48931ae08745Sheppo " loc_ra=0x%llx, tar_poff=0x%llx, loc_poff=0x%llx," 48941ae08745Sheppo " tar_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx," 48951ae08745Sheppo " total_bal=0x%llx\n", 48961ae08745Sheppo ldcp->id, direction, target_pa, local_ra, target_poff, 48971ae08745Sheppo local_poff, target_psize, local_psize, copy_size, 48981ae08745Sheppo target_size); 48991ae08745Sheppo 49001ae08745Sheppo rv = hv_ldc_copy(ldcp->id, direction, 49011ae08745Sheppo (target_pa + target_poff), (local_ra + local_poff), 49021ae08745Sheppo copy_size, &copied_len); 49031ae08745Sheppo 49041ae08745Sheppo if (rv != 0) { 49051ae08745Sheppo cmn_err(CE_WARN, 49061ae08745Sheppo "ldc_mem_rdwr_pa: (0x%lx) err %d during copy\n", 49071ae08745Sheppo ldcp->id, rv); 49081ae08745Sheppo DWARN(DBG_ALL_LDCS, 49091ae08745Sheppo "ldc_mem_rdwr_pa: (0x%llx) dir=%lld,tar_pa=0x%llx, " 49101ae08745Sheppo "loc_ra=0x%llx, tar_poff=0x%llx, loc_poff=0x%llx," 49111ae08745Sheppo " tar_psz=0x%llx, loc_psz=0x%llx, copy_sz=0x%llx," 49121ae08745Sheppo " total_bal=0x%llx\n", 49131ae08745Sheppo ldcp->id, direction, target_pa, local_ra, 49141ae08745Sheppo target_poff, local_poff, target_psize, local_psize, 49151ae08745Sheppo copy_size, target_size); 49161ae08745Sheppo 49171ae08745Sheppo *size = *size - target_size; 49181ae08745Sheppo mutex_exit(&ldcp->lock); 49191ae08745Sheppo return (i_ldc_h2v_error(rv)); 49201ae08745Sheppo } 49211ae08745Sheppo 49221ae08745Sheppo D2(ldcp->id, "ldc_mem_rdwr_pa: copied=0x%llx\n", copied_len); 49231ae08745Sheppo target_poff += copied_len; 49241ae08745Sheppo local_poff += copied_len; 49251ae08745Sheppo target_psize -= copied_len; 49261ae08745Sheppo local_psize -= copied_len; 49271ae08745Sheppo 49281ae08745Sheppo target_size -= copied_len; 49291ae08745Sheppo 49301ae08745Sheppo if (copy_size != copied_len) 49311ae08745Sheppo continue; 49321ae08745Sheppo 49331ae08745Sheppo if (target_psize == 0 && target_size != 0) { 49341ae08745Sheppo target_pa += pg_size; 49351ae08745Sheppo target_poff = 0; 49361ae08745Sheppo target_psize = min(pg_size, target_size); 49371ae08745Sheppo } 49381ae08745Sheppo 49391ae08745Sheppo if (local_psize == 0 && target_size != 0) { 49401ae08745Sheppo local_valign += pg_size; 49411ae08745Sheppo local_ra = va_to_pa((void *)local_valign); 49421ae08745Sheppo local_poff = 0; 49431ae08745Sheppo local_psize = min(pg_size, len); 49441ae08745Sheppo len -= local_psize; 49451ae08745Sheppo } 49461ae08745Sheppo 49471ae08745Sheppo /* check if we are all done */ 49481ae08745Sheppo if (target_size == 0) 49491ae08745Sheppo break; 49501ae08745Sheppo } 49511ae08745Sheppo 49521ae08745Sheppo mutex_exit(&ldcp->lock); 49531ae08745Sheppo 49541ae08745Sheppo D1(ldcp->id, "ldc_mem_rdwr_pa: (0x%llx) done copying sz=0x%llx\n", 49551ae08745Sheppo ldcp->id, *size); 49561ae08745Sheppo 49571ae08745Sheppo return (0); 49581ae08745Sheppo } 49591ae08745Sheppo 49601ae08745Sheppo /* 49611ae08745Sheppo * Map an exported memory segment into the local address space. If the 49621ae08745Sheppo * memory range was exported for direct map access, a HV call is made 49631ae08745Sheppo * to allocate a RA range. If the map is done via a shadow copy, local 49641ae08745Sheppo * shadow memory is allocated and the base VA is returned in 'vaddr'. If 49651ae08745Sheppo * the mapping is a direct map then the RA is returned in 'raddr'. 49661ae08745Sheppo */ 49671ae08745Sheppo int 49681ae08745Sheppo ldc_mem_map(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie, uint32_t ccount, 4969*4bac2208Snarayan uint8_t mtype, uint8_t perm, caddr_t *vaddr, caddr_t *raddr) 49701ae08745Sheppo { 4971*4bac2208Snarayan int i, j, idx, rv, retries; 49721ae08745Sheppo ldc_chan_t *ldcp; 49731ae08745Sheppo ldc_mhdl_t *mhdl; 49741ae08745Sheppo ldc_memseg_t *memseg; 4975*4bac2208Snarayan caddr_t tmpaddr; 4976*4bac2208Snarayan uint64_t map_perm = perm; 4977*4bac2208Snarayan uint64_t pg_size, pg_shift, pg_size_code, pg_mask; 4978*4bac2208Snarayan uint64_t exp_size = 0, base_off, map_size, npages; 4979*4bac2208Snarayan uint64_t cookie_addr, cookie_off, cookie_size; 4980*4bac2208Snarayan tte_t ldc_tte; 49811ae08745Sheppo 49821ae08745Sheppo if (mhandle == NULL) { 49831ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_map: invalid memory handle\n"); 49841ae08745Sheppo return (EINVAL); 49851ae08745Sheppo } 49861ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 49871ae08745Sheppo 49881ae08745Sheppo mutex_enter(&mhdl->lock); 49891ae08745Sheppo 49901ae08745Sheppo if (mhdl->status == LDC_BOUND || mhdl->status == LDC_MAPPED || 49911ae08745Sheppo mhdl->memseg != NULL) { 49921ae08745Sheppo DWARN(DBG_ALL_LDCS, 49931ae08745Sheppo "ldc_mem_map: (0x%llx) handle bound/mapped\n", mhandle); 49941ae08745Sheppo mutex_exit(&mhdl->lock); 49951ae08745Sheppo return (EINVAL); 49961ae08745Sheppo } 49971ae08745Sheppo 49981ae08745Sheppo ldcp = mhdl->ldcp; 49991ae08745Sheppo 50001ae08745Sheppo mutex_enter(&ldcp->lock); 50011ae08745Sheppo 50021ae08745Sheppo if (ldcp->tstate != TS_UP) { 50031ae08745Sheppo DWARN(ldcp->id, 50041ae08745Sheppo "ldc_mem_dring_map: (0x%llx) channel is not UP\n", 50051ae08745Sheppo ldcp->id); 50061ae08745Sheppo mutex_exit(&ldcp->lock); 50071ae08745Sheppo mutex_exit(&mhdl->lock); 50081ae08745Sheppo return (EINVAL); 50091ae08745Sheppo } 50101ae08745Sheppo 50111ae08745Sheppo if ((mtype & (LDC_SHADOW_MAP|LDC_DIRECT_MAP|LDC_IO_MAP)) == 0) { 50121ae08745Sheppo DWARN(ldcp->id, "ldc_mem_map: invalid map type\n"); 50131ae08745Sheppo mutex_exit(&ldcp->lock); 50141ae08745Sheppo mutex_exit(&mhdl->lock); 50151ae08745Sheppo return (EINVAL); 50161ae08745Sheppo } 50171ae08745Sheppo 50181ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) cookie = 0x%llx,0x%llx\n", 5019d10e4ef2Snarayan ldcp->id, cookie->addr, cookie->size); 50201ae08745Sheppo 50211ae08745Sheppo /* FUTURE: get the page size, pgsz code, and shift */ 50221ae08745Sheppo pg_size = MMU_PAGESIZE; 50231ae08745Sheppo pg_size_code = page_szc(pg_size); 50241ae08745Sheppo pg_shift = page_get_shift(pg_size_code); 5025*4bac2208Snarayan pg_mask = ~(pg_size - 1); 50261ae08745Sheppo 50271ae08745Sheppo /* calculate the number of pages in the exported cookie */ 5028*4bac2208Snarayan base_off = cookie[0].addr & (pg_size - 1); 5029*4bac2208Snarayan for (idx = 0; idx < ccount; idx++) 50301ae08745Sheppo exp_size += cookie[idx].size; 5031*4bac2208Snarayan map_size = P2ROUNDUP((exp_size + base_off), pg_size); 5032*4bac2208Snarayan npages = (map_size >> pg_shift); 50331ae08745Sheppo 50341ae08745Sheppo /* Allocate memseg structure */ 5035*4bac2208Snarayan memseg = mhdl->memseg = 5036*4bac2208Snarayan kmem_cache_alloc(ldcssp->memseg_cache, KM_SLEEP); 50371ae08745Sheppo 50381ae08745Sheppo /* Allocate memory to store all pages and cookies */ 50391ae08745Sheppo memseg->pages = kmem_zalloc((sizeof (ldc_page_t) * npages), KM_SLEEP); 50401ae08745Sheppo memseg->cookies = 50411ae08745Sheppo kmem_zalloc((sizeof (ldc_mem_cookie_t) * ccount), KM_SLEEP); 50421ae08745Sheppo 5043*4bac2208Snarayan D2(ldcp->id, "ldc_mem_map: (0x%llx) exp_size=0x%llx, map_size=0x%llx," 5044*4bac2208Snarayan "pages=0x%llx\n", ldcp->id, exp_size, map_size, npages); 50451ae08745Sheppo 5046*4bac2208Snarayan /* 5047*4bac2208Snarayan * Check if direct map over shared memory is enabled, if not change 5048*4bac2208Snarayan * the mapping type to SHADOW_MAP. 5049*4bac2208Snarayan */ 5050*4bac2208Snarayan if (ldc_shmem_enabled == 0) 5051*4bac2208Snarayan mtype = LDC_SHADOW_MAP; 5052*4bac2208Snarayan 5053*4bac2208Snarayan /* 5054*4bac2208Snarayan * Check to see if the client is requesting direct or shadow map 5055*4bac2208Snarayan * If direct map is requested, try to map remote memory first, 5056*4bac2208Snarayan * and if that fails, revert to shadow map 5057*4bac2208Snarayan */ 5058*4bac2208Snarayan if (mtype == LDC_DIRECT_MAP) { 5059*4bac2208Snarayan 5060*4bac2208Snarayan /* Allocate kernel virtual space for mapping */ 5061*4bac2208Snarayan memseg->vaddr = vmem_xalloc(heap_arena, map_size, 5062*4bac2208Snarayan pg_size, 0, 0, NULL, NULL, VM_NOSLEEP); 5063*4bac2208Snarayan if (memseg->vaddr == NULL) { 5064*4bac2208Snarayan cmn_err(CE_WARN, 5065*4bac2208Snarayan "ldc_mem_map: (0x%lx) memory map failed\n", 5066*4bac2208Snarayan ldcp->id); 5067*4bac2208Snarayan kmem_free(memseg->cookies, 5068*4bac2208Snarayan (sizeof (ldc_mem_cookie_t) * ccount)); 5069*4bac2208Snarayan kmem_free(memseg->pages, 5070*4bac2208Snarayan (sizeof (ldc_page_t) * npages)); 5071*4bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 5072*4bac2208Snarayan 5073*4bac2208Snarayan mutex_exit(&ldcp->lock); 5074*4bac2208Snarayan mutex_exit(&mhdl->lock); 5075*4bac2208Snarayan return (ENOMEM); 5076*4bac2208Snarayan } 5077*4bac2208Snarayan 5078*4bac2208Snarayan /* Unload previous mapping */ 5079*4bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, map_size, 5080*4bac2208Snarayan HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK); 5081*4bac2208Snarayan 5082*4bac2208Snarayan /* for each cookie passed in - map into address space */ 5083*4bac2208Snarayan idx = 0; 5084*4bac2208Snarayan cookie_size = 0; 5085*4bac2208Snarayan tmpaddr = memseg->vaddr; 5086*4bac2208Snarayan 5087*4bac2208Snarayan for (i = 0; i < npages; i++) { 5088*4bac2208Snarayan 5089*4bac2208Snarayan if (cookie_size == 0) { 5090*4bac2208Snarayan ASSERT(idx < ccount); 5091*4bac2208Snarayan cookie_addr = cookie[idx].addr & pg_mask; 5092*4bac2208Snarayan cookie_off = cookie[idx].addr & (pg_size - 1); 5093*4bac2208Snarayan cookie_size = 5094*4bac2208Snarayan P2ROUNDUP((cookie_off + cookie[idx].size), 5095*4bac2208Snarayan pg_size); 5096*4bac2208Snarayan idx++; 5097*4bac2208Snarayan } 5098*4bac2208Snarayan 5099*4bac2208Snarayan D1(ldcp->id, "ldc_mem_map: (0x%llx) mapping " 5100*4bac2208Snarayan "cookie 0x%llx, bal=0x%llx\n", ldcp->id, 5101*4bac2208Snarayan cookie_addr, cookie_size); 5102*4bac2208Snarayan 5103*4bac2208Snarayan /* map the cookie into address space */ 5104*4bac2208Snarayan for (retries = 0; retries < ldc_max_retries; 5105*4bac2208Snarayan retries++) { 5106*4bac2208Snarayan 5107*4bac2208Snarayan rv = hv_ldc_mapin(ldcp->id, cookie_addr, 5108*4bac2208Snarayan &memseg->pages[i].raddr, &map_perm); 5109*4bac2208Snarayan 5110*4bac2208Snarayan if (rv != H_EWOULDBLOCK && rv != H_ETOOMANY) 5111*4bac2208Snarayan break; 5112*4bac2208Snarayan 5113*4bac2208Snarayan drv_usecwait(ldc_delay); 5114*4bac2208Snarayan } 5115*4bac2208Snarayan 5116*4bac2208Snarayan if (rv || memseg->pages[i].raddr == 0) { 5117*4bac2208Snarayan DWARN(ldcp->id, 5118*4bac2208Snarayan "ldc_mem_map: (0x%llx) hv mapin err %d\n", 5119*4bac2208Snarayan ldcp->id, rv); 5120*4bac2208Snarayan 5121*4bac2208Snarayan /* remove previous mapins */ 5122*4bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, map_size, 5123*4bac2208Snarayan HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK); 5124*4bac2208Snarayan for (j = 0; j < i; j++) { 5125*4bac2208Snarayan rv = hv_ldc_unmap( 5126*4bac2208Snarayan memseg->pages[j].raddr); 5127*4bac2208Snarayan if (rv) { 5128*4bac2208Snarayan DWARN(ldcp->id, 5129*4bac2208Snarayan "ldc_mem_map: (0x%llx) " 5130*4bac2208Snarayan "cannot unmap ra=0x%llx\n", 5131*4bac2208Snarayan ldcp->id, 5132*4bac2208Snarayan memseg->pages[j].raddr); 5133*4bac2208Snarayan } 5134*4bac2208Snarayan } 5135*4bac2208Snarayan 5136*4bac2208Snarayan /* free kernel virtual space */ 5137*4bac2208Snarayan vmem_free(heap_arena, (void *)memseg->vaddr, 5138*4bac2208Snarayan memseg->size); 5139*4bac2208Snarayan 5140*4bac2208Snarayan /* direct map failed - revert to shadow map */ 5141*4bac2208Snarayan mtype = LDC_SHADOW_MAP; 5142*4bac2208Snarayan break; 5143*4bac2208Snarayan 5144*4bac2208Snarayan } else { 5145*4bac2208Snarayan 5146*4bac2208Snarayan D1(ldcp->id, 5147*4bac2208Snarayan "ldc_mem_map: (0x%llx) vtop map 0x%llx -> " 5148*4bac2208Snarayan "0x%llx, cookie=0x%llx, perm=0x%llx\n", 5149*4bac2208Snarayan ldcp->id, tmpaddr, memseg->pages[i].raddr, 5150*4bac2208Snarayan cookie_addr, perm); 5151*4bac2208Snarayan 5152*4bac2208Snarayan /* 5153*4bac2208Snarayan * NOTE: Calling hat_devload directly, causes it 5154*4bac2208Snarayan * to look for page_t using the pfn. Since this 5155*4bac2208Snarayan * addr is greater than the memlist, it treates 5156*4bac2208Snarayan * it as non-memory 5157*4bac2208Snarayan */ 5158*4bac2208Snarayan sfmmu_memtte(&ldc_tte, 5159*4bac2208Snarayan (pfn_t)(memseg->pages[i].raddr >> pg_shift), 5160*4bac2208Snarayan PROT_READ | PROT_WRITE | HAT_NOSYNC, TTE8K); 5161*4bac2208Snarayan 5162*4bac2208Snarayan D1(ldcp->id, 5163*4bac2208Snarayan "ldc_mem_map: (0x%llx) ra 0x%llx -> " 5164*4bac2208Snarayan "tte 0x%llx\n", ldcp->id, 5165*4bac2208Snarayan memseg->pages[i].raddr, ldc_tte); 5166*4bac2208Snarayan 5167*4bac2208Snarayan sfmmu_tteload(kas.a_hat, &ldc_tte, tmpaddr, 5168*4bac2208Snarayan NULL, HAT_LOAD_LOCK); 5169*4bac2208Snarayan 5170*4bac2208Snarayan cookie_size -= pg_size; 5171*4bac2208Snarayan cookie_addr += pg_size; 5172*4bac2208Snarayan tmpaddr += pg_size; 5173*4bac2208Snarayan } 5174*4bac2208Snarayan } 5175*4bac2208Snarayan } 5176*4bac2208Snarayan 51771ae08745Sheppo if (mtype == LDC_SHADOW_MAP) { 51781ae08745Sheppo if (*vaddr == NULL) { 5179*4bac2208Snarayan memseg->vaddr = 51801ae08745Sheppo contig_mem_alloc_align(exp_size, PAGESIZE); 5181*4bac2208Snarayan if (memseg->vaddr == NULL) { 51821ae08745Sheppo cmn_err(CE_WARN, "ldc_mem_map: shadow memory " 51831ae08745Sheppo "allocation failed\n"); 51841ae08745Sheppo kmem_free(memseg->cookies, 51851ae08745Sheppo (sizeof (ldc_mem_cookie_t) * ccount)); 51861ae08745Sheppo kmem_free(memseg->pages, 51871ae08745Sheppo (sizeof (ldc_page_t) * npages)); 5188*4bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 51891ae08745Sheppo mutex_exit(&ldcp->lock); 51901ae08745Sheppo mutex_exit(&mhdl->lock); 51911ae08745Sheppo return (ENOMEM); 51921ae08745Sheppo } 51931ae08745Sheppo 5194*4bac2208Snarayan bzero(memseg->vaddr, exp_size); 51951ae08745Sheppo mhdl->myshadow = B_TRUE; 51961ae08745Sheppo 51971ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) allocated " 5198*4bac2208Snarayan "shadow page va=0x%llx\n", ldcp->id, memseg->vaddr); 51991ae08745Sheppo } else { 52001ae08745Sheppo /* 5201*4bac2208Snarayan * Use client supplied memory for memseg->vaddr 52021ae08745Sheppo * WARNING: assuming that client mem is >= exp_size 52031ae08745Sheppo */ 5204*4bac2208Snarayan memseg->vaddr = *vaddr; 52051ae08745Sheppo } 52061ae08745Sheppo 52071ae08745Sheppo /* Save all page and cookie information */ 5208*4bac2208Snarayan for (i = 0, tmpaddr = memseg->vaddr; i < npages; i++) { 52091ae08745Sheppo memseg->pages[i].raddr = va_to_pa(tmpaddr); 52101ae08745Sheppo memseg->pages[i].size = pg_size; 52111ae08745Sheppo tmpaddr += pg_size; 52121ae08745Sheppo } 5213*4bac2208Snarayan 52141ae08745Sheppo } 52151ae08745Sheppo 5216*4bac2208Snarayan /* save all cookies */ 5217*4bac2208Snarayan bcopy(cookie, memseg->cookies, ccount * sizeof (ldc_mem_cookie_t)); 5218*4bac2208Snarayan 52191ae08745Sheppo /* update memseg_t */ 52201ae08745Sheppo memseg->raddr = memseg->pages[0].raddr; 5221*4bac2208Snarayan memseg->size = (mtype == LDC_SHADOW_MAP) ? exp_size : map_size; 52221ae08745Sheppo memseg->npages = npages; 52231ae08745Sheppo memseg->ncookies = ccount; 52241ae08745Sheppo memseg->next_cookie = 0; 52251ae08745Sheppo 52261ae08745Sheppo /* memory handle = mapped */ 52271ae08745Sheppo mhdl->mtype = mtype; 5228*4bac2208Snarayan mhdl->perm = perm; 52291ae08745Sheppo mhdl->status = LDC_MAPPED; 52301ae08745Sheppo 52311ae08745Sheppo D1(ldcp->id, "ldc_mem_map: (0x%llx) mapped 0x%llx, ra=0x%llx, " 52321ae08745Sheppo "va=0x%llx, pgs=0x%llx cookies=0x%llx\n", 52331ae08745Sheppo ldcp->id, mhdl, memseg->raddr, memseg->vaddr, 52341ae08745Sheppo memseg->npages, memseg->ncookies); 52351ae08745Sheppo 5236*4bac2208Snarayan if (mtype == LDC_SHADOW_MAP) 5237*4bac2208Snarayan base_off = 0; 52381ae08745Sheppo if (raddr) 5239*4bac2208Snarayan *raddr = (caddr_t)(memseg->raddr | base_off); 52401ae08745Sheppo if (vaddr) 5241*4bac2208Snarayan *vaddr = (caddr_t)((uintptr_t)memseg->vaddr | base_off); 52421ae08745Sheppo 52431ae08745Sheppo mutex_exit(&ldcp->lock); 52441ae08745Sheppo mutex_exit(&mhdl->lock); 52451ae08745Sheppo return (0); 52461ae08745Sheppo } 52471ae08745Sheppo 52481ae08745Sheppo /* 52491ae08745Sheppo * Unmap a memory segment. Free shadow memory (if any). 52501ae08745Sheppo */ 52511ae08745Sheppo int 52521ae08745Sheppo ldc_mem_unmap(ldc_mem_handle_t mhandle) 52531ae08745Sheppo { 5254*4bac2208Snarayan int i, rv; 52551ae08745Sheppo ldc_mhdl_t *mhdl = (ldc_mhdl_t *)mhandle; 52561ae08745Sheppo ldc_chan_t *ldcp; 52571ae08745Sheppo ldc_memseg_t *memseg; 52581ae08745Sheppo 52591ae08745Sheppo if (mhdl == 0 || mhdl->status != LDC_MAPPED) { 52601ae08745Sheppo DWARN(DBG_ALL_LDCS, 52611ae08745Sheppo "ldc_mem_unmap: (0x%llx) handle is not mapped\n", 52621ae08745Sheppo mhandle); 52631ae08745Sheppo return (EINVAL); 52641ae08745Sheppo } 52651ae08745Sheppo 52661ae08745Sheppo mutex_enter(&mhdl->lock); 52671ae08745Sheppo 52681ae08745Sheppo ldcp = mhdl->ldcp; 52691ae08745Sheppo memseg = mhdl->memseg; 52701ae08745Sheppo 52711ae08745Sheppo D1(ldcp->id, "ldc_mem_unmap: (0x%llx) unmapping handle 0x%llx\n", 52721ae08745Sheppo ldcp->id, mhdl); 52731ae08745Sheppo 52741ae08745Sheppo /* if we allocated shadow memory - free it */ 52751ae08745Sheppo if (mhdl->mtype == LDC_SHADOW_MAP && mhdl->myshadow) { 52761ae08745Sheppo contig_mem_free(memseg->vaddr, memseg->size); 5277*4bac2208Snarayan } else if (mhdl->mtype == LDC_DIRECT_MAP) { 5278*4bac2208Snarayan 5279*4bac2208Snarayan /* unmap in the case of DIRECT_MAP */ 5280*4bac2208Snarayan hat_unload(kas.a_hat, memseg->vaddr, memseg->size, 5281*4bac2208Snarayan HAT_UNLOAD_UNLOCK); 5282*4bac2208Snarayan 5283*4bac2208Snarayan for (i = 0; i < memseg->npages; i++) { 5284*4bac2208Snarayan rv = hv_ldc_unmap(memseg->pages[i].raddr); 5285*4bac2208Snarayan if (rv) { 5286*4bac2208Snarayan cmn_err(CE_WARN, 5287*4bac2208Snarayan "ldc_mem_map: (0x%lx) hv unmap err %d\n", 5288*4bac2208Snarayan ldcp->id, rv); 5289*4bac2208Snarayan } 5290*4bac2208Snarayan } 5291*4bac2208Snarayan 5292*4bac2208Snarayan vmem_free(heap_arena, (void *)memseg->vaddr, memseg->size); 52931ae08745Sheppo } 52941ae08745Sheppo 52951ae08745Sheppo /* free the allocated memseg and page structures */ 52961ae08745Sheppo kmem_free(memseg->pages, (sizeof (ldc_page_t) * memseg->npages)); 52971ae08745Sheppo kmem_free(memseg->cookies, 52981ae08745Sheppo (sizeof (ldc_mem_cookie_t) * memseg->ncookies)); 5299*4bac2208Snarayan kmem_cache_free(ldcssp->memseg_cache, memseg); 53001ae08745Sheppo 53011ae08745Sheppo /* uninitialize the memory handle */ 53021ae08745Sheppo mhdl->memseg = NULL; 53031ae08745Sheppo mhdl->status = LDC_UNBOUND; 53041ae08745Sheppo 53051ae08745Sheppo D1(ldcp->id, "ldc_mem_unmap: (0x%llx) unmapped handle 0x%llx\n", 53061ae08745Sheppo ldcp->id, mhdl); 53071ae08745Sheppo 53081ae08745Sheppo mutex_exit(&mhdl->lock); 53091ae08745Sheppo return (0); 53101ae08745Sheppo } 53111ae08745Sheppo 53121ae08745Sheppo /* 53131ae08745Sheppo * Internal entry point for LDC mapped memory entry consistency 53141ae08745Sheppo * semantics. Acquire copies the contents of the remote memory 53151ae08745Sheppo * into the local shadow copy. The release operation copies the local 53161ae08745Sheppo * contents into the remote memory. The offset and size specify the 53171ae08745Sheppo * bounds for the memory range being synchronized. 53181ae08745Sheppo */ 53191ae08745Sheppo static int 53201ae08745Sheppo i_ldc_mem_acquire_release(ldc_mem_handle_t mhandle, uint8_t direction, 53211ae08745Sheppo uint64_t offset, size_t size) 53221ae08745Sheppo { 53231ae08745Sheppo int err; 53241ae08745Sheppo ldc_mhdl_t *mhdl; 53251ae08745Sheppo ldc_chan_t *ldcp; 53261ae08745Sheppo ldc_memseg_t *memseg; 53271ae08745Sheppo caddr_t local_vaddr; 53281ae08745Sheppo size_t copy_size; 53291ae08745Sheppo 53301ae08745Sheppo if (mhandle == NULL) { 53311ae08745Sheppo DWARN(DBG_ALL_LDCS, 53321ae08745Sheppo "i_ldc_mem_acquire_release: invalid memory handle\n"); 53331ae08745Sheppo return (EINVAL); 53341ae08745Sheppo } 53351ae08745Sheppo mhdl = (ldc_mhdl_t *)mhandle; 53361ae08745Sheppo 53371ae08745Sheppo mutex_enter(&mhdl->lock); 53381ae08745Sheppo 53391ae08745Sheppo if (mhdl->status != LDC_MAPPED || mhdl->ldcp == NULL) { 53401ae08745Sheppo DWARN(DBG_ALL_LDCS, 53411ae08745Sheppo "i_ldc_mem_acquire_release: not mapped memory\n"); 53421ae08745Sheppo mutex_exit(&mhdl->lock); 53431ae08745Sheppo return (EINVAL); 53441ae08745Sheppo } 53451ae08745Sheppo 5346*4bac2208Snarayan /* do nothing for direct map */ 5347*4bac2208Snarayan if (mhdl->mtype == LDC_DIRECT_MAP) { 5348*4bac2208Snarayan mutex_exit(&mhdl->lock); 5349*4bac2208Snarayan return (0); 5350*4bac2208Snarayan } 5351*4bac2208Snarayan 5352*4bac2208Snarayan /* do nothing if COPY_IN+MEM_W and COPY_OUT+MEM_R */ 5353*4bac2208Snarayan if ((direction == LDC_COPY_IN && (mhdl->perm & LDC_MEM_R) == 0) || 5354*4bac2208Snarayan (direction == LDC_COPY_OUT && (mhdl->perm & LDC_MEM_W) == 0)) { 5355*4bac2208Snarayan mutex_exit(&mhdl->lock); 5356*4bac2208Snarayan return (0); 5357*4bac2208Snarayan } 5358*4bac2208Snarayan 53591ae08745Sheppo if (offset >= mhdl->memseg->size || 53601ae08745Sheppo (offset + size) > mhdl->memseg->size) { 53611ae08745Sheppo DWARN(DBG_ALL_LDCS, 53621ae08745Sheppo "i_ldc_mem_acquire_release: memory out of range\n"); 53631ae08745Sheppo mutex_exit(&mhdl->lock); 53641ae08745Sheppo return (EINVAL); 53651ae08745Sheppo } 53661ae08745Sheppo 53671ae08745Sheppo /* get the channel handle and memory segment */ 53681ae08745Sheppo ldcp = mhdl->ldcp; 53691ae08745Sheppo memseg = mhdl->memseg; 53701ae08745Sheppo 53711ae08745Sheppo if (mhdl->mtype == LDC_SHADOW_MAP) { 53721ae08745Sheppo 53731ae08745Sheppo local_vaddr = memseg->vaddr + offset; 53741ae08745Sheppo copy_size = size; 53751ae08745Sheppo 53761ae08745Sheppo /* copy to/from remote from/to local memory */ 53771ae08745Sheppo err = ldc_mem_copy((ldc_handle_t)ldcp, local_vaddr, offset, 53781ae08745Sheppo ©_size, memseg->cookies, memseg->ncookies, 53791ae08745Sheppo direction); 53801ae08745Sheppo if (err || copy_size != size) { 53811ae08745Sheppo cmn_err(CE_WARN, 53821ae08745Sheppo "i_ldc_mem_acquire_release: copy failed\n"); 53831ae08745Sheppo mutex_exit(&mhdl->lock); 53841ae08745Sheppo return (err); 53851ae08745Sheppo } 53861ae08745Sheppo } 53871ae08745Sheppo 53881ae08745Sheppo mutex_exit(&mhdl->lock); 53891ae08745Sheppo 53901ae08745Sheppo return (0); 53911ae08745Sheppo } 53921ae08745Sheppo 53931ae08745Sheppo /* 53941ae08745Sheppo * Ensure that the contents in the remote memory seg are consistent 53951ae08745Sheppo * with the contents if of local segment 53961ae08745Sheppo */ 53971ae08745Sheppo int 53981ae08745Sheppo ldc_mem_acquire(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size) 53991ae08745Sheppo { 54001ae08745Sheppo return (i_ldc_mem_acquire_release(mhandle, LDC_COPY_IN, offset, size)); 54011ae08745Sheppo } 54021ae08745Sheppo 54031ae08745Sheppo 54041ae08745Sheppo /* 54051ae08745Sheppo * Ensure that the contents in the local memory seg are consistent 54061ae08745Sheppo * with the contents if of remote segment 54071ae08745Sheppo */ 54081ae08745Sheppo int 54091ae08745Sheppo ldc_mem_release(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size) 54101ae08745Sheppo { 54111ae08745Sheppo return (i_ldc_mem_acquire_release(mhandle, LDC_COPY_OUT, offset, size)); 54121ae08745Sheppo } 54131ae08745Sheppo 54141ae08745Sheppo /* 54151ae08745Sheppo * Allocate a descriptor ring. The size of each each descriptor 54161ae08745Sheppo * must be 8-byte aligned and the entire ring should be a multiple 54171ae08745Sheppo * of MMU_PAGESIZE. 54181ae08745Sheppo */ 54191ae08745Sheppo int 54201ae08745Sheppo ldc_mem_dring_create(uint32_t len, uint32_t dsize, ldc_dring_handle_t *dhandle) 54211ae08745Sheppo { 54221ae08745Sheppo ldc_dring_t *dringp; 54231ae08745Sheppo size_t size = (dsize * len); 54241ae08745Sheppo 54251ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_create: len=0x%x, size=0x%x\n", 54261ae08745Sheppo len, dsize); 54271ae08745Sheppo 54281ae08745Sheppo if (dhandle == NULL) { 54291ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid dhandle\n"); 54301ae08745Sheppo return (EINVAL); 54311ae08745Sheppo } 54321ae08745Sheppo 54331ae08745Sheppo if (len == 0) { 54341ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid length\n"); 54351ae08745Sheppo return (EINVAL); 54361ae08745Sheppo } 54371ae08745Sheppo 54381ae08745Sheppo /* descriptor size should be 8-byte aligned */ 54391ae08745Sheppo if (dsize == 0 || (dsize & 0x7)) { 54401ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_create: invalid size\n"); 54411ae08745Sheppo return (EINVAL); 54421ae08745Sheppo } 54431ae08745Sheppo 54441ae08745Sheppo *dhandle = 0; 54451ae08745Sheppo 54461ae08745Sheppo /* Allocate a desc ring structure */ 54471ae08745Sheppo dringp = kmem_zalloc(sizeof (ldc_dring_t), KM_SLEEP); 54481ae08745Sheppo 54491ae08745Sheppo /* Initialize dring */ 54501ae08745Sheppo dringp->length = len; 54511ae08745Sheppo dringp->dsize = dsize; 54521ae08745Sheppo 54531ae08745Sheppo /* round off to multiple of pagesize */ 54541ae08745Sheppo dringp->size = (size & MMU_PAGEMASK); 54551ae08745Sheppo if (size & MMU_PAGEOFFSET) 54561ae08745Sheppo dringp->size += MMU_PAGESIZE; 54571ae08745Sheppo 54581ae08745Sheppo dringp->status = LDC_UNBOUND; 54591ae08745Sheppo 54601ae08745Sheppo /* allocate descriptor ring memory */ 54611ae08745Sheppo dringp->base = contig_mem_alloc_align(dringp->size, PAGESIZE); 54621ae08745Sheppo if (dringp->base == NULL) { 54631ae08745Sheppo cmn_err(CE_WARN, 54641ae08745Sheppo "ldc_mem_dring_create: unable to alloc desc\n"); 54651ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 54661ae08745Sheppo return (ENOMEM); 54671ae08745Sheppo } 54681ae08745Sheppo 54691ae08745Sheppo bzero(dringp->base, dringp->size); 54701ae08745Sheppo 54711ae08745Sheppo /* initialize the desc ring lock */ 54721ae08745Sheppo mutex_init(&dringp->lock, NULL, MUTEX_DRIVER, NULL); 54731ae08745Sheppo 54741ae08745Sheppo /* Add descriptor ring to the head of global list */ 54751ae08745Sheppo mutex_enter(&ldcssp->lock); 54761ae08745Sheppo dringp->next = ldcssp->dring_list; 54771ae08745Sheppo ldcssp->dring_list = dringp; 54781ae08745Sheppo mutex_exit(&ldcssp->lock); 54791ae08745Sheppo 54801ae08745Sheppo *dhandle = (ldc_dring_handle_t)dringp; 54811ae08745Sheppo 54821ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_create: dring allocated\n"); 54831ae08745Sheppo 54841ae08745Sheppo return (0); 54851ae08745Sheppo } 54861ae08745Sheppo 54871ae08745Sheppo 54881ae08745Sheppo /* 54891ae08745Sheppo * Destroy a descriptor ring. 54901ae08745Sheppo */ 54911ae08745Sheppo int 54921ae08745Sheppo ldc_mem_dring_destroy(ldc_dring_handle_t dhandle) 54931ae08745Sheppo { 54941ae08745Sheppo ldc_dring_t *dringp; 54951ae08745Sheppo ldc_dring_t *tmp_dringp; 54961ae08745Sheppo 54971ae08745Sheppo D1(DBG_ALL_LDCS, "ldc_mem_dring_destroy: entered\n"); 54981ae08745Sheppo 54991ae08745Sheppo if (dhandle == NULL) { 55001ae08745Sheppo DWARN(DBG_ALL_LDCS, 55011ae08745Sheppo "ldc_mem_dring_destroy: invalid desc ring handle\n"); 55021ae08745Sheppo return (EINVAL); 55031ae08745Sheppo } 55041ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 55051ae08745Sheppo 55061ae08745Sheppo if (dringp->status == LDC_BOUND) { 55071ae08745Sheppo DWARN(DBG_ALL_LDCS, 55081ae08745Sheppo "ldc_mem_dring_destroy: desc ring is bound\n"); 55091ae08745Sheppo return (EACCES); 55101ae08745Sheppo } 55111ae08745Sheppo 55121ae08745Sheppo mutex_enter(&dringp->lock); 55131ae08745Sheppo mutex_enter(&ldcssp->lock); 55141ae08745Sheppo 55151ae08745Sheppo /* remove from linked list - if not bound */ 55161ae08745Sheppo tmp_dringp = ldcssp->dring_list; 55171ae08745Sheppo if (tmp_dringp == dringp) { 55181ae08745Sheppo ldcssp->dring_list = dringp->next; 55191ae08745Sheppo dringp->next = NULL; 55201ae08745Sheppo 55211ae08745Sheppo } else { 55221ae08745Sheppo while (tmp_dringp != NULL) { 55231ae08745Sheppo if (tmp_dringp->next == dringp) { 55241ae08745Sheppo tmp_dringp->next = dringp->next; 55251ae08745Sheppo dringp->next = NULL; 55261ae08745Sheppo break; 55271ae08745Sheppo } 55281ae08745Sheppo tmp_dringp = tmp_dringp->next; 55291ae08745Sheppo } 55301ae08745Sheppo if (tmp_dringp == NULL) { 55311ae08745Sheppo DWARN(DBG_ALL_LDCS, 55321ae08745Sheppo "ldc_mem_dring_destroy: invalid descriptor\n"); 55331ae08745Sheppo mutex_exit(&ldcssp->lock); 55341ae08745Sheppo mutex_exit(&dringp->lock); 55351ae08745Sheppo return (EINVAL); 55361ae08745Sheppo } 55371ae08745Sheppo } 55381ae08745Sheppo 55391ae08745Sheppo mutex_exit(&ldcssp->lock); 55401ae08745Sheppo 55411ae08745Sheppo /* free the descriptor ring */ 55421ae08745Sheppo contig_mem_free((caddr_t)dringp->base, dringp->size); 55431ae08745Sheppo 55441ae08745Sheppo mutex_exit(&dringp->lock); 55451ae08745Sheppo 55461ae08745Sheppo /* destroy dring lock */ 55471ae08745Sheppo mutex_destroy(&dringp->lock); 55481ae08745Sheppo 55491ae08745Sheppo /* free desc ring object */ 55501ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 55511ae08745Sheppo 55521ae08745Sheppo return (0); 55531ae08745Sheppo } 55541ae08745Sheppo 55551ae08745Sheppo /* 55561ae08745Sheppo * Bind a previously allocated dring to a channel. The channel should 55571ae08745Sheppo * be OPEN in order to bind the ring to the channel. Returns back a 55581ae08745Sheppo * descriptor ring cookie. The descriptor ring is exported for remote 55591ae08745Sheppo * access by the client at the other end of the channel. An entry for 55601ae08745Sheppo * dring pages is stored in map table (via call to ldc_mem_bind_handle). 55611ae08745Sheppo */ 55621ae08745Sheppo int 55631ae08745Sheppo ldc_mem_dring_bind(ldc_handle_t handle, ldc_dring_handle_t dhandle, 55641ae08745Sheppo uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount) 55651ae08745Sheppo { 55661ae08745Sheppo int err; 55671ae08745Sheppo ldc_chan_t *ldcp; 55681ae08745Sheppo ldc_dring_t *dringp; 55691ae08745Sheppo ldc_mem_handle_t mhandle; 55701ae08745Sheppo 55711ae08745Sheppo /* check to see if channel is initalized */ 55721ae08745Sheppo if (handle == NULL) { 55731ae08745Sheppo DWARN(DBG_ALL_LDCS, 55741ae08745Sheppo "ldc_mem_dring_bind: invalid channel handle\n"); 55751ae08745Sheppo return (EINVAL); 55761ae08745Sheppo } 55771ae08745Sheppo ldcp = (ldc_chan_t *)handle; 55781ae08745Sheppo 55791ae08745Sheppo if (dhandle == NULL) { 55801ae08745Sheppo DWARN(DBG_ALL_LDCS, 55811ae08745Sheppo "ldc_mem_dring_bind: invalid desc ring handle\n"); 55821ae08745Sheppo return (EINVAL); 55831ae08745Sheppo } 55841ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 55851ae08745Sheppo 55861ae08745Sheppo if (cookie == NULL) { 55871ae08745Sheppo DWARN(ldcp->id, 55881ae08745Sheppo "ldc_mem_dring_bind: invalid cookie arg\n"); 55891ae08745Sheppo return (EINVAL); 55901ae08745Sheppo } 55911ae08745Sheppo 55921ae08745Sheppo mutex_enter(&dringp->lock); 55931ae08745Sheppo 55941ae08745Sheppo if (dringp->status == LDC_BOUND) { 55951ae08745Sheppo DWARN(DBG_ALL_LDCS, 55961ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) descriptor ring is bound\n", 55971ae08745Sheppo ldcp->id); 55981ae08745Sheppo mutex_exit(&dringp->lock); 55991ae08745Sheppo return (EINVAL); 56001ae08745Sheppo } 56011ae08745Sheppo 56021ae08745Sheppo if ((perm & LDC_MEM_RW) == 0) { 56031ae08745Sheppo DWARN(DBG_ALL_LDCS, 56041ae08745Sheppo "ldc_mem_dring_bind: invalid permissions\n"); 56051ae08745Sheppo mutex_exit(&dringp->lock); 56061ae08745Sheppo return (EINVAL); 56071ae08745Sheppo } 56081ae08745Sheppo 56091ae08745Sheppo if ((mtype & (LDC_SHADOW_MAP|LDC_DIRECT_MAP|LDC_IO_MAP)) == 0) { 56101ae08745Sheppo DWARN(DBG_ALL_LDCS, "ldc_mem_dring_bind: invalid type\n"); 56111ae08745Sheppo mutex_exit(&dringp->lock); 56121ae08745Sheppo return (EINVAL); 56131ae08745Sheppo } 56141ae08745Sheppo 56151ae08745Sheppo dringp->ldcp = ldcp; 56161ae08745Sheppo 56171ae08745Sheppo /* create an memory handle */ 56181ae08745Sheppo err = ldc_mem_alloc_handle(handle, &mhandle); 56191ae08745Sheppo if (err || mhandle == NULL) { 56201ae08745Sheppo DWARN(DBG_ALL_LDCS, 56211ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) error allocating mhandle\n", 56221ae08745Sheppo ldcp->id); 56231ae08745Sheppo mutex_exit(&dringp->lock); 56241ae08745Sheppo return (err); 56251ae08745Sheppo } 56261ae08745Sheppo dringp->mhdl = mhandle; 56271ae08745Sheppo 56281ae08745Sheppo /* bind the descriptor ring to channel */ 56291ae08745Sheppo err = ldc_mem_bind_handle(mhandle, dringp->base, dringp->size, 56301ae08745Sheppo mtype, perm, cookie, ccount); 56311ae08745Sheppo if (err) { 56321ae08745Sheppo DWARN(ldcp->id, 56331ae08745Sheppo "ldc_mem_dring_bind: (0x%llx) error binding mhandle\n", 56341ae08745Sheppo ldcp->id); 56351ae08745Sheppo mutex_exit(&dringp->lock); 56361ae08745Sheppo return (err); 56371ae08745Sheppo } 56381ae08745Sheppo 56391ae08745Sheppo /* 56401ae08745Sheppo * For now return error if we get more than one cookie 56411ae08745Sheppo * FUTURE: Return multiple cookies .. 56421ae08745Sheppo */ 56431ae08745Sheppo if (*ccount > 1) { 56441ae08745Sheppo (void) ldc_mem_unbind_handle(mhandle); 56451ae08745Sheppo (void) ldc_mem_free_handle(mhandle); 56461ae08745Sheppo 56471ae08745Sheppo dringp->ldcp = NULL; 56481ae08745Sheppo dringp->mhdl = NULL; 56491ae08745Sheppo *ccount = 0; 56501ae08745Sheppo 56511ae08745Sheppo mutex_exit(&dringp->lock); 56521ae08745Sheppo return (EAGAIN); 56531ae08745Sheppo } 56541ae08745Sheppo 56551ae08745Sheppo /* Add descriptor ring to channel's exported dring list */ 56561ae08745Sheppo mutex_enter(&ldcp->exp_dlist_lock); 56571ae08745Sheppo dringp->ch_next = ldcp->exp_dring_list; 56581ae08745Sheppo ldcp->exp_dring_list = dringp; 56591ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 56601ae08745Sheppo 56611ae08745Sheppo dringp->status = LDC_BOUND; 56621ae08745Sheppo 56631ae08745Sheppo mutex_exit(&dringp->lock); 56641ae08745Sheppo 56651ae08745Sheppo return (0); 56661ae08745Sheppo } 56671ae08745Sheppo 56681ae08745Sheppo /* 56691ae08745Sheppo * Return the next cookie associated with the specified dring handle 56701ae08745Sheppo */ 56711ae08745Sheppo int 56721ae08745Sheppo ldc_mem_dring_nextcookie(ldc_dring_handle_t dhandle, ldc_mem_cookie_t *cookie) 56731ae08745Sheppo { 56741ae08745Sheppo int rv = 0; 56751ae08745Sheppo ldc_dring_t *dringp; 56761ae08745Sheppo ldc_chan_t *ldcp; 56771ae08745Sheppo 56781ae08745Sheppo if (dhandle == NULL) { 56791ae08745Sheppo DWARN(DBG_ALL_LDCS, 56801ae08745Sheppo "ldc_mem_dring_nextcookie: invalid desc ring handle\n"); 56811ae08745Sheppo return (EINVAL); 56821ae08745Sheppo } 56831ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 56841ae08745Sheppo mutex_enter(&dringp->lock); 56851ae08745Sheppo 56861ae08745Sheppo if (dringp->status != LDC_BOUND) { 56871ae08745Sheppo DWARN(DBG_ALL_LDCS, 56881ae08745Sheppo "ldc_mem_dring_nextcookie: descriptor ring 0x%llx " 56891ae08745Sheppo "is not bound\n", dringp); 56901ae08745Sheppo mutex_exit(&dringp->lock); 56911ae08745Sheppo return (EINVAL); 56921ae08745Sheppo } 56931ae08745Sheppo 56941ae08745Sheppo ldcp = dringp->ldcp; 56951ae08745Sheppo 56961ae08745Sheppo if (cookie == NULL) { 56971ae08745Sheppo DWARN(ldcp->id, 56981ae08745Sheppo "ldc_mem_dring_nextcookie:(0x%llx) invalid cookie arg\n", 56991ae08745Sheppo ldcp->id); 57001ae08745Sheppo mutex_exit(&dringp->lock); 57011ae08745Sheppo return (EINVAL); 57021ae08745Sheppo } 57031ae08745Sheppo 57041ae08745Sheppo rv = ldc_mem_nextcookie((ldc_mem_handle_t)dringp->mhdl, cookie); 57051ae08745Sheppo mutex_exit(&dringp->lock); 57061ae08745Sheppo 57071ae08745Sheppo return (rv); 57081ae08745Sheppo } 57091ae08745Sheppo /* 57101ae08745Sheppo * Unbind a previously bound dring from a channel. 57111ae08745Sheppo */ 57121ae08745Sheppo int 57131ae08745Sheppo ldc_mem_dring_unbind(ldc_dring_handle_t dhandle) 57141ae08745Sheppo { 57151ae08745Sheppo ldc_dring_t *dringp; 57161ae08745Sheppo ldc_dring_t *tmp_dringp; 57171ae08745Sheppo ldc_chan_t *ldcp; 57181ae08745Sheppo 57191ae08745Sheppo if (dhandle == NULL) { 57201ae08745Sheppo DWARN(DBG_ALL_LDCS, 57211ae08745Sheppo "ldc_mem_dring_unbind: invalid desc ring handle\n"); 57221ae08745Sheppo return (EINVAL); 57231ae08745Sheppo } 57241ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 57251ae08745Sheppo 57261ae08745Sheppo mutex_enter(&dringp->lock); 57271ae08745Sheppo 57281ae08745Sheppo if (dringp->status == LDC_UNBOUND) { 57291ae08745Sheppo DWARN(DBG_ALL_LDCS, 57301ae08745Sheppo "ldc_mem_dring_bind: descriptor ring 0x%llx is unbound\n", 57311ae08745Sheppo dringp); 57321ae08745Sheppo mutex_exit(&dringp->lock); 57331ae08745Sheppo return (EINVAL); 57341ae08745Sheppo } 57351ae08745Sheppo ldcp = dringp->ldcp; 57361ae08745Sheppo 57371ae08745Sheppo mutex_enter(&ldcp->exp_dlist_lock); 57381ae08745Sheppo 57391ae08745Sheppo tmp_dringp = ldcp->exp_dring_list; 57401ae08745Sheppo if (tmp_dringp == dringp) { 57411ae08745Sheppo ldcp->exp_dring_list = dringp->ch_next; 57421ae08745Sheppo dringp->ch_next = NULL; 57431ae08745Sheppo 57441ae08745Sheppo } else { 57451ae08745Sheppo while (tmp_dringp != NULL) { 57461ae08745Sheppo if (tmp_dringp->ch_next == dringp) { 57471ae08745Sheppo tmp_dringp->ch_next = dringp->ch_next; 57481ae08745Sheppo dringp->ch_next = NULL; 57491ae08745Sheppo break; 57501ae08745Sheppo } 57511ae08745Sheppo tmp_dringp = tmp_dringp->ch_next; 57521ae08745Sheppo } 57531ae08745Sheppo if (tmp_dringp == NULL) { 57541ae08745Sheppo DWARN(DBG_ALL_LDCS, 57551ae08745Sheppo "ldc_mem_dring_unbind: invalid descriptor\n"); 57561ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 57571ae08745Sheppo mutex_exit(&dringp->lock); 57581ae08745Sheppo return (EINVAL); 57591ae08745Sheppo } 57601ae08745Sheppo } 57611ae08745Sheppo 57621ae08745Sheppo mutex_exit(&ldcp->exp_dlist_lock); 57631ae08745Sheppo 57641ae08745Sheppo (void) ldc_mem_unbind_handle((ldc_mem_handle_t)dringp->mhdl); 57651ae08745Sheppo (void) ldc_mem_free_handle((ldc_mem_handle_t)dringp->mhdl); 57661ae08745Sheppo 57671ae08745Sheppo dringp->ldcp = NULL; 57681ae08745Sheppo dringp->mhdl = NULL; 57691ae08745Sheppo dringp->status = LDC_UNBOUND; 57701ae08745Sheppo 57711ae08745Sheppo mutex_exit(&dringp->lock); 57721ae08745Sheppo 57731ae08745Sheppo return (0); 57741ae08745Sheppo } 57751ae08745Sheppo 57761ae08745Sheppo /* 57771ae08745Sheppo * Get information about the dring. The base address of the descriptor 57781ae08745Sheppo * ring along with the type and permission are returned back. 57791ae08745Sheppo */ 57801ae08745Sheppo int 57811ae08745Sheppo ldc_mem_dring_info(ldc_dring_handle_t dhandle, ldc_mem_info_t *minfo) 57821ae08745Sheppo { 57831ae08745Sheppo ldc_dring_t *dringp; 57841ae08745Sheppo int rv; 57851ae08745Sheppo 57861ae08745Sheppo if (dhandle == NULL) { 57871ae08745Sheppo DWARN(DBG_ALL_LDCS, 57881ae08745Sheppo "ldc_mem_dring_info: invalid desc ring handle\n"); 57891ae08745Sheppo return (EINVAL); 57901ae08745Sheppo } 57911ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 57921ae08745Sheppo 57931ae08745Sheppo mutex_enter(&dringp->lock); 57941ae08745Sheppo 57951ae08745Sheppo if (dringp->mhdl) { 57961ae08745Sheppo rv = ldc_mem_info(dringp->mhdl, minfo); 57971ae08745Sheppo if (rv) { 57981ae08745Sheppo DWARN(DBG_ALL_LDCS, 57991ae08745Sheppo "ldc_mem_dring_info: error reading mem info\n"); 58001ae08745Sheppo mutex_exit(&dringp->lock); 58011ae08745Sheppo return (rv); 58021ae08745Sheppo } 58031ae08745Sheppo } else { 58041ae08745Sheppo minfo->vaddr = dringp->base; 58051ae08745Sheppo minfo->raddr = NULL; 58061ae08745Sheppo minfo->status = dringp->status; 58071ae08745Sheppo } 58081ae08745Sheppo 58091ae08745Sheppo mutex_exit(&dringp->lock); 58101ae08745Sheppo 58111ae08745Sheppo return (0); 58121ae08745Sheppo } 58131ae08745Sheppo 58141ae08745Sheppo /* 58151ae08745Sheppo * Map an exported descriptor ring into the local address space. If the 58161ae08745Sheppo * descriptor ring was exported for direct map access, a HV call is made 58171ae08745Sheppo * to allocate a RA range. If the map is done via a shadow copy, local 58181ae08745Sheppo * shadow memory is allocated. 58191ae08745Sheppo */ 58201ae08745Sheppo int 58211ae08745Sheppo ldc_mem_dring_map(ldc_handle_t handle, ldc_mem_cookie_t *cookie, 58221ae08745Sheppo uint32_t ccount, uint32_t len, uint32_t dsize, uint8_t mtype, 58231ae08745Sheppo ldc_dring_handle_t *dhandle) 58241ae08745Sheppo { 58251ae08745Sheppo int err; 58261ae08745Sheppo ldc_chan_t *ldcp = (ldc_chan_t *)handle; 58271ae08745Sheppo ldc_mem_handle_t mhandle; 58281ae08745Sheppo ldc_dring_t *dringp; 58291ae08745Sheppo size_t dring_size; 58301ae08745Sheppo 58311ae08745Sheppo if (dhandle == NULL) { 58321ae08745Sheppo DWARN(DBG_ALL_LDCS, 58331ae08745Sheppo "ldc_mem_dring_map: invalid dhandle\n"); 58341ae08745Sheppo return (EINVAL); 58351ae08745Sheppo } 58361ae08745Sheppo 58371ae08745Sheppo /* check to see if channel is initalized */ 58381ae08745Sheppo if (handle == NULL) { 58391ae08745Sheppo DWARN(DBG_ALL_LDCS, 58401ae08745Sheppo "ldc_mem_dring_map: invalid channel handle\n"); 58411ae08745Sheppo return (EINVAL); 58421ae08745Sheppo } 58431ae08745Sheppo ldcp = (ldc_chan_t *)handle; 58441ae08745Sheppo 58451ae08745Sheppo if (cookie == NULL) { 58461ae08745Sheppo DWARN(ldcp->id, 58471ae08745Sheppo "ldc_mem_dring_map: (0x%llx) invalid cookie\n", 58481ae08745Sheppo ldcp->id); 58491ae08745Sheppo return (EINVAL); 58501ae08745Sheppo } 58511ae08745Sheppo 58521ae08745Sheppo /* FUTURE: For now we support only one cookie per dring */ 58531ae08745Sheppo ASSERT(ccount == 1); 58541ae08745Sheppo 58551ae08745Sheppo if (cookie->size < (dsize * len)) { 58561ae08745Sheppo DWARN(ldcp->id, 58571ae08745Sheppo "ldc_mem_dring_map: (0x%llx) invalid dsize/len\n", 58581ae08745Sheppo ldcp->id); 58591ae08745Sheppo return (EINVAL); 58601ae08745Sheppo } 58611ae08745Sheppo 58621ae08745Sheppo *dhandle = 0; 58631ae08745Sheppo 58641ae08745Sheppo /* Allocate an dring structure */ 58651ae08745Sheppo dringp = kmem_zalloc(sizeof (ldc_dring_t), KM_SLEEP); 58661ae08745Sheppo 58671ae08745Sheppo D1(ldcp->id, 58681ae08745Sheppo "ldc_mem_dring_map: 0x%x,0x%x,0x%x,0x%llx,0x%llx\n", 58691ae08745Sheppo mtype, len, dsize, cookie->addr, cookie->size); 58701ae08745Sheppo 58711ae08745Sheppo /* Initialize dring */ 58721ae08745Sheppo dringp->length = len; 58731ae08745Sheppo dringp->dsize = dsize; 58741ae08745Sheppo 58751ae08745Sheppo /* round of to multiple of page size */ 58761ae08745Sheppo dring_size = len * dsize; 58771ae08745Sheppo dringp->size = (dring_size & MMU_PAGEMASK); 58781ae08745Sheppo if (dring_size & MMU_PAGEOFFSET) 58791ae08745Sheppo dringp->size += MMU_PAGESIZE; 58801ae08745Sheppo 58811ae08745Sheppo dringp->ldcp = ldcp; 58821ae08745Sheppo 58831ae08745Sheppo /* create an memory handle */ 58841ae08745Sheppo err = ldc_mem_alloc_handle(handle, &mhandle); 58851ae08745Sheppo if (err || mhandle == NULL) { 58861ae08745Sheppo DWARN(DBG_ALL_LDCS, 58871ae08745Sheppo "ldc_mem_dring_map: cannot alloc hdl err=%d\n", 58881ae08745Sheppo err); 58891ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 58901ae08745Sheppo return (ENOMEM); 58911ae08745Sheppo } 58921ae08745Sheppo 58931ae08745Sheppo dringp->mhdl = mhandle; 58941ae08745Sheppo dringp->base = NULL; 58951ae08745Sheppo 58961ae08745Sheppo /* map the dring into local memory */ 5897*4bac2208Snarayan err = ldc_mem_map(mhandle, cookie, ccount, mtype, LDC_MEM_RW, 58981ae08745Sheppo &(dringp->base), NULL); 58991ae08745Sheppo if (err || dringp->base == NULL) { 59001ae08745Sheppo cmn_err(CE_WARN, 59011ae08745Sheppo "ldc_mem_dring_map: cannot map desc ring err=%d\n", err); 59021ae08745Sheppo (void) ldc_mem_free_handle(mhandle); 59031ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 59041ae08745Sheppo return (ENOMEM); 59051ae08745Sheppo } 59061ae08745Sheppo 59071ae08745Sheppo /* initialize the desc ring lock */ 59081ae08745Sheppo mutex_init(&dringp->lock, NULL, MUTEX_DRIVER, NULL); 59091ae08745Sheppo 59101ae08745Sheppo /* Add descriptor ring to channel's imported dring list */ 59111ae08745Sheppo mutex_enter(&ldcp->imp_dlist_lock); 59121ae08745Sheppo dringp->ch_next = ldcp->imp_dring_list; 59131ae08745Sheppo ldcp->imp_dring_list = dringp; 59141ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 59151ae08745Sheppo 59161ae08745Sheppo dringp->status = LDC_MAPPED; 59171ae08745Sheppo 59181ae08745Sheppo *dhandle = (ldc_dring_handle_t)dringp; 59191ae08745Sheppo 59201ae08745Sheppo return (0); 59211ae08745Sheppo } 59221ae08745Sheppo 59231ae08745Sheppo /* 59241ae08745Sheppo * Unmap a descriptor ring. Free shadow memory (if any). 59251ae08745Sheppo */ 59261ae08745Sheppo int 59271ae08745Sheppo ldc_mem_dring_unmap(ldc_dring_handle_t dhandle) 59281ae08745Sheppo { 59291ae08745Sheppo ldc_dring_t *dringp; 59301ae08745Sheppo ldc_dring_t *tmp_dringp; 59311ae08745Sheppo ldc_chan_t *ldcp; 59321ae08745Sheppo 59331ae08745Sheppo if (dhandle == NULL) { 59341ae08745Sheppo DWARN(DBG_ALL_LDCS, 59351ae08745Sheppo "ldc_mem_dring_unmap: invalid desc ring handle\n"); 59361ae08745Sheppo return (EINVAL); 59371ae08745Sheppo } 59381ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 59391ae08745Sheppo 59401ae08745Sheppo if (dringp->status != LDC_MAPPED) { 59411ae08745Sheppo DWARN(DBG_ALL_LDCS, 59421ae08745Sheppo "ldc_mem_dring_unmap: not a mapped desc ring\n"); 59431ae08745Sheppo return (EINVAL); 59441ae08745Sheppo } 59451ae08745Sheppo 59461ae08745Sheppo mutex_enter(&dringp->lock); 59471ae08745Sheppo 59481ae08745Sheppo ldcp = dringp->ldcp; 59491ae08745Sheppo 59501ae08745Sheppo mutex_enter(&ldcp->imp_dlist_lock); 59511ae08745Sheppo 59521ae08745Sheppo /* find and unlink the desc ring from channel import list */ 59531ae08745Sheppo tmp_dringp = ldcp->imp_dring_list; 59541ae08745Sheppo if (tmp_dringp == dringp) { 59551ae08745Sheppo ldcp->imp_dring_list = dringp->ch_next; 59561ae08745Sheppo dringp->ch_next = NULL; 59571ae08745Sheppo 59581ae08745Sheppo } else { 59591ae08745Sheppo while (tmp_dringp != NULL) { 59601ae08745Sheppo if (tmp_dringp->ch_next == dringp) { 59611ae08745Sheppo tmp_dringp->ch_next = dringp->ch_next; 59621ae08745Sheppo dringp->ch_next = NULL; 59631ae08745Sheppo break; 59641ae08745Sheppo } 59651ae08745Sheppo tmp_dringp = tmp_dringp->ch_next; 59661ae08745Sheppo } 59671ae08745Sheppo if (tmp_dringp == NULL) { 59681ae08745Sheppo DWARN(DBG_ALL_LDCS, 59691ae08745Sheppo "ldc_mem_dring_unmap: invalid descriptor\n"); 59701ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 59711ae08745Sheppo mutex_exit(&dringp->lock); 59721ae08745Sheppo return (EINVAL); 59731ae08745Sheppo } 59741ae08745Sheppo } 59751ae08745Sheppo 59761ae08745Sheppo mutex_exit(&ldcp->imp_dlist_lock); 59771ae08745Sheppo 59781ae08745Sheppo /* do a LDC memory handle unmap and free */ 59791ae08745Sheppo (void) ldc_mem_unmap(dringp->mhdl); 59801ae08745Sheppo (void) ldc_mem_free_handle((ldc_mem_handle_t)dringp->mhdl); 59811ae08745Sheppo 59821ae08745Sheppo dringp->status = 0; 59831ae08745Sheppo dringp->ldcp = NULL; 59841ae08745Sheppo 59851ae08745Sheppo mutex_exit(&dringp->lock); 59861ae08745Sheppo 59871ae08745Sheppo /* destroy dring lock */ 59881ae08745Sheppo mutex_destroy(&dringp->lock); 59891ae08745Sheppo 59901ae08745Sheppo /* free desc ring object */ 59911ae08745Sheppo kmem_free(dringp, sizeof (ldc_dring_t)); 59921ae08745Sheppo 59931ae08745Sheppo return (0); 59941ae08745Sheppo } 59951ae08745Sheppo 59961ae08745Sheppo /* 59971ae08745Sheppo * Internal entry point for descriptor ring access entry consistency 59981ae08745Sheppo * semantics. Acquire copies the contents of the remote descriptor ring 59991ae08745Sheppo * into the local shadow copy. The release operation copies the local 60001ae08745Sheppo * contents into the remote dring. The start and end locations specify 60011ae08745Sheppo * bounds for the entries being synchronized. 60021ae08745Sheppo */ 60031ae08745Sheppo static int 60041ae08745Sheppo i_ldc_dring_acquire_release(ldc_dring_handle_t dhandle, 60051ae08745Sheppo uint8_t direction, uint64_t start, uint64_t end) 60061ae08745Sheppo { 60071ae08745Sheppo int err; 60081ae08745Sheppo ldc_dring_t *dringp; 60091ae08745Sheppo ldc_chan_t *ldcp; 60101ae08745Sheppo uint64_t soff; 60111ae08745Sheppo size_t copy_size; 60121ae08745Sheppo 60131ae08745Sheppo if (dhandle == NULL) { 60141ae08745Sheppo DWARN(DBG_ALL_LDCS, 60151ae08745Sheppo "i_ldc_dring_acquire_release: invalid desc ring handle\n"); 60161ae08745Sheppo return (EINVAL); 60171ae08745Sheppo } 60181ae08745Sheppo dringp = (ldc_dring_t *)dhandle; 60191ae08745Sheppo mutex_enter(&dringp->lock); 60201ae08745Sheppo 60211ae08745Sheppo if (dringp->status != LDC_MAPPED || dringp->ldcp == NULL) { 60221ae08745Sheppo DWARN(DBG_ALL_LDCS, 60231ae08745Sheppo "i_ldc_dring_acquire_release: not a mapped desc ring\n"); 60241ae08745Sheppo mutex_exit(&dringp->lock); 60251ae08745Sheppo return (EINVAL); 60261ae08745Sheppo } 60271ae08745Sheppo 60281ae08745Sheppo if (start >= dringp->length || end >= dringp->length) { 60291ae08745Sheppo DWARN(DBG_ALL_LDCS, 60301ae08745Sheppo "i_ldc_dring_acquire_release: index out of range\n"); 60311ae08745Sheppo mutex_exit(&dringp->lock); 60321ae08745Sheppo return (EINVAL); 60331ae08745Sheppo } 60341ae08745Sheppo 60351ae08745Sheppo /* get the channel handle */ 60361ae08745Sheppo ldcp = dringp->ldcp; 60371ae08745Sheppo 60381ae08745Sheppo copy_size = (start <= end) ? (((end - start) + 1) * dringp->dsize) : 60391ae08745Sheppo ((dringp->length - start) * dringp->dsize); 60401ae08745Sheppo 60411ae08745Sheppo /* Calculate the relative offset for the first desc */ 60421ae08745Sheppo soff = (start * dringp->dsize); 60431ae08745Sheppo 60441ae08745Sheppo /* copy to/from remote from/to local memory */ 60451ae08745Sheppo D1(ldcp->id, "i_ldc_dring_acquire_release: c1 off=0x%llx sz=0x%llx\n", 60461ae08745Sheppo soff, copy_size); 60471ae08745Sheppo err = i_ldc_mem_acquire_release((ldc_mem_handle_t)dringp->mhdl, 60481ae08745Sheppo direction, soff, copy_size); 60491ae08745Sheppo if (err) { 60501ae08745Sheppo DWARN(ldcp->id, 60511ae08745Sheppo "i_ldc_dring_acquire_release: copy failed\n"); 60521ae08745Sheppo mutex_exit(&dringp->lock); 60531ae08745Sheppo return (err); 60541ae08745Sheppo } 60551ae08745Sheppo 60561ae08745Sheppo /* do the balance */ 60571ae08745Sheppo if (start > end) { 60581ae08745Sheppo copy_size = ((end + 1) * dringp->dsize); 60591ae08745Sheppo soff = 0; 60601ae08745Sheppo 60611ae08745Sheppo /* copy to/from remote from/to local memory */ 60621ae08745Sheppo D1(ldcp->id, "i_ldc_dring_acquire_release: c2 " 60631ae08745Sheppo "off=0x%llx sz=0x%llx\n", soff, copy_size); 60641ae08745Sheppo err = i_ldc_mem_acquire_release((ldc_mem_handle_t)dringp->mhdl, 60651ae08745Sheppo direction, soff, copy_size); 60661ae08745Sheppo if (err) { 60671ae08745Sheppo DWARN(ldcp->id, 60681ae08745Sheppo "i_ldc_dring_acquire_release: copy failed\n"); 60691ae08745Sheppo mutex_exit(&dringp->lock); 60701ae08745Sheppo return (err); 60711ae08745Sheppo } 60721ae08745Sheppo } 60731ae08745Sheppo 60741ae08745Sheppo mutex_exit(&dringp->lock); 60751ae08745Sheppo 60761ae08745Sheppo return (0); 60771ae08745Sheppo } 60781ae08745Sheppo 60791ae08745Sheppo /* 60801ae08745Sheppo * Ensure that the contents in the local dring are consistent 60811ae08745Sheppo * with the contents if of remote dring 60821ae08745Sheppo */ 60831ae08745Sheppo int 60841ae08745Sheppo ldc_mem_dring_acquire(ldc_dring_handle_t dhandle, uint64_t start, uint64_t end) 60851ae08745Sheppo { 60861ae08745Sheppo return (i_ldc_dring_acquire_release(dhandle, LDC_COPY_IN, start, end)); 60871ae08745Sheppo } 60881ae08745Sheppo 60891ae08745Sheppo /* 60901ae08745Sheppo * Ensure that the contents in the remote dring are consistent 60911ae08745Sheppo * with the contents if of local dring 60921ae08745Sheppo */ 60931ae08745Sheppo int 60941ae08745Sheppo ldc_mem_dring_release(ldc_dring_handle_t dhandle, uint64_t start, uint64_t end) 60951ae08745Sheppo { 60961ae08745Sheppo return (i_ldc_dring_acquire_release(dhandle, LDC_COPY_OUT, start, end)); 60971ae08745Sheppo } 60981ae08745Sheppo 60991ae08745Sheppo 61001ae08745Sheppo /* ------------------------------------------------------------------------- */ 6101