11ae08745Sheppo /* 21ae08745Sheppo * CDDL HEADER START 31ae08745Sheppo * 41ae08745Sheppo * The contents of this file are subject to the terms of the 51ae08745Sheppo * Common Development and Distribution License (the "License"). 61ae08745Sheppo * You may not use this file except in compliance with the License. 71ae08745Sheppo * 81ae08745Sheppo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91ae08745Sheppo * or http://www.opensolaris.org/os/licensing. 101ae08745Sheppo * See the License for the specific language governing permissions 111ae08745Sheppo * and limitations under the License. 121ae08745Sheppo * 131ae08745Sheppo * When distributing Covered Code, include this CDDL HEADER in each 141ae08745Sheppo * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151ae08745Sheppo * If applicable, add the following below this CDDL HEADER, with the 161ae08745Sheppo * fields enclosed by brackets "[]" replaced with your own identifying 171ae08745Sheppo * information: Portions Copyright [yyyy] [name of copyright owner] 181ae08745Sheppo * 191ae08745Sheppo * CDDL HEADER END 201ae08745Sheppo */ 211ae08745Sheppo 221ae08745Sheppo /* 23*6567eb0aSlh195018 * Copyright 2007 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 /* 301ae08745Sheppo * LDoms virtual disk client (vdc) device driver 311ae08745Sheppo * 321ae08745Sheppo * This driver runs on a guest logical domain and communicates with the virtual 331ae08745Sheppo * disk server (vds) driver running on the service domain which is exporting 341ae08745Sheppo * virtualized "disks" to the guest logical domain. 351ae08745Sheppo * 361ae08745Sheppo * The driver can be divided into four sections: 371ae08745Sheppo * 381ae08745Sheppo * 1) generic device driver housekeeping 391ae08745Sheppo * _init, _fini, attach, detach, ops structures, etc. 401ae08745Sheppo * 411ae08745Sheppo * 2) communication channel setup 421ae08745Sheppo * Setup the communications link over the LDC channel that vdc uses to 431ae08745Sheppo * talk to the vDisk server. Initialise the descriptor ring which 441ae08745Sheppo * allows the LDC clients to transfer data via memory mappings. 451ae08745Sheppo * 461ae08745Sheppo * 3) Support exported to upper layers (filesystems, etc) 471ae08745Sheppo * The upper layers call into vdc via strategy(9E) and DKIO(7I) 481ae08745Sheppo * ioctl calls. vdc will copy the data to be written to the descriptor 491ae08745Sheppo * ring or maps the buffer to store the data read by the vDisk 501ae08745Sheppo * server into the descriptor ring. It then sends a message to the 511ae08745Sheppo * vDisk server requesting it to complete the operation. 521ae08745Sheppo * 531ae08745Sheppo * 4) Handling responses from vDisk server. 541ae08745Sheppo * The vDisk server will ACK some or all of the messages vdc sends to it 551ae08745Sheppo * (this is configured during the handshake). Upon receipt of an ACK 561ae08745Sheppo * vdc will check the descriptor ring and signal to the upper layer 571ae08745Sheppo * code waiting on the IO. 581ae08745Sheppo */ 591ae08745Sheppo 60e1ebb9ecSlm66018 #include <sys/atomic.h> 611ae08745Sheppo #include <sys/conf.h> 621ae08745Sheppo #include <sys/disp.h> 631ae08745Sheppo #include <sys/ddi.h> 641ae08745Sheppo #include <sys/dkio.h> 651ae08745Sheppo #include <sys/efi_partition.h> 661ae08745Sheppo #include <sys/fcntl.h> 671ae08745Sheppo #include <sys/file.h> 681ae08745Sheppo #include <sys/mach_descrip.h> 691ae08745Sheppo #include <sys/modctl.h> 701ae08745Sheppo #include <sys/mdeg.h> 711ae08745Sheppo #include <sys/note.h> 721ae08745Sheppo #include <sys/open.h> 73d10e4ef2Snarayan #include <sys/sdt.h> 741ae08745Sheppo #include <sys/stat.h> 751ae08745Sheppo #include <sys/sunddi.h> 761ae08745Sheppo #include <sys/types.h> 771ae08745Sheppo #include <sys/promif.h> 781ae08745Sheppo #include <sys/vtoc.h> 791ae08745Sheppo #include <sys/archsystm.h> 801ae08745Sheppo #include <sys/sysmacros.h> 811ae08745Sheppo 821ae08745Sheppo #include <sys/cdio.h> 831ae08745Sheppo #include <sys/dktp/fdisk.h> 841ae08745Sheppo #include <sys/scsi/generic/sense.h> 851ae08745Sheppo #include <sys/scsi/impl/uscsi.h> /* Needed for defn of USCSICMD ioctl */ 861ae08745Sheppo 871ae08745Sheppo #include <sys/ldoms.h> 881ae08745Sheppo #include <sys/ldc.h> 891ae08745Sheppo #include <sys/vio_common.h> 901ae08745Sheppo #include <sys/vio_mailbox.h> 911ae08745Sheppo #include <sys/vdsk_common.h> 921ae08745Sheppo #include <sys/vdsk_mailbox.h> 931ae08745Sheppo #include <sys/vdc.h> 941ae08745Sheppo 951ae08745Sheppo /* 961ae08745Sheppo * function prototypes 971ae08745Sheppo */ 981ae08745Sheppo 991ae08745Sheppo /* standard driver functions */ 1001ae08745Sheppo static int vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred); 1011ae08745Sheppo static int vdc_close(dev_t dev, int flag, int otyp, cred_t *cred); 1021ae08745Sheppo static int vdc_strategy(struct buf *buf); 1031ae08745Sheppo static int vdc_print(dev_t dev, char *str); 1041ae08745Sheppo static int vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1051ae08745Sheppo static int vdc_read(dev_t dev, struct uio *uio, cred_t *cred); 1061ae08745Sheppo static int vdc_write(dev_t dev, struct uio *uio, cred_t *cred); 1071ae08745Sheppo static int vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 1081ae08745Sheppo cred_t *credp, int *rvalp); 1091ae08745Sheppo static int vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred); 1101ae08745Sheppo static int vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred); 1111ae08745Sheppo 1121ae08745Sheppo static int vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, 1131ae08745Sheppo void *arg, void **resultp); 1141ae08745Sheppo static int vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 1151ae08745Sheppo static int vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 1161ae08745Sheppo 1171ae08745Sheppo /* setup */ 1180d0c8d4bSnarayan static void vdc_min(struct buf *bufp); 1190a55fbb7Slm66018 static int vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen); 1201ae08745Sheppo static int vdc_do_ldc_init(vdc_t *vdc); 1211ae08745Sheppo static int vdc_start_ldc_connection(vdc_t *vdc); 1221ae08745Sheppo static int vdc_create_device_nodes(vdc_t *vdc); 1234bac2208Snarayan static int vdc_create_device_nodes_efi(vdc_t *vdc); 1244bac2208Snarayan static int vdc_create_device_nodes_vtoc(vdc_t *vdc); 1251ae08745Sheppo static int vdc_create_device_nodes_props(vdc_t *vdc); 1261ae08745Sheppo static int vdc_get_ldc_id(dev_info_t *dip, uint64_t *ldc_id); 1270a55fbb7Slm66018 static int vdc_do_ldc_up(vdc_t *vdc); 1281ae08745Sheppo static void vdc_terminate_ldc(vdc_t *vdc); 1291ae08745Sheppo static int vdc_init_descriptor_ring(vdc_t *vdc); 1301ae08745Sheppo static void vdc_destroy_descriptor_ring(vdc_t *vdc); 1314bac2208Snarayan static int vdc_setup_devid(vdc_t *vdc); 1324bac2208Snarayan static void vdc_store_efi(vdc_t *vdc, struct dk_gpt *efi); 1331ae08745Sheppo 1341ae08745Sheppo /* handshake with vds */ 1350a55fbb7Slm66018 static int vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver); 1363af08d82Slm66018 static int vdc_ver_negotiation(vdc_t *vdcp); 1371ae08745Sheppo static int vdc_init_attr_negotiation(vdc_t *vdc); 1383af08d82Slm66018 static int vdc_attr_negotiation(vdc_t *vdcp); 1391ae08745Sheppo static int vdc_init_dring_negotiate(vdc_t *vdc); 1403af08d82Slm66018 static int vdc_dring_negotiation(vdc_t *vdcp); 1413af08d82Slm66018 static int vdc_send_rdx(vdc_t *vdcp); 1423af08d82Slm66018 static int vdc_rdx_exchange(vdc_t *vdcp); 1430a55fbb7Slm66018 static boolean_t vdc_is_supported_version(vio_ver_msg_t *ver_msg); 1441ae08745Sheppo 1450a55fbb7Slm66018 /* processing incoming messages from vDisk server */ 1461ae08745Sheppo static void vdc_process_msg_thread(vdc_t *vdc); 1473af08d82Slm66018 static int vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp); 1483af08d82Slm66018 1490a55fbb7Slm66018 static uint_t vdc_handle_cb(uint64_t event, caddr_t arg); 1503af08d82Slm66018 static int vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg); 1511ae08745Sheppo static int vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg); 1520a55fbb7Slm66018 static int vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg); 1530a55fbb7Slm66018 static int vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg); 1540a55fbb7Slm66018 static int vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg); 1553af08d82Slm66018 static int vdc_send_request(vdc_t *vdcp, int operation, 1563af08d82Slm66018 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 1573af08d82Slm66018 int cb_type, void *cb_arg, vio_desc_direction_t dir); 1583af08d82Slm66018 static int vdc_map_to_shared_dring(vdc_t *vdcp, int idx); 1593af08d82Slm66018 static int vdc_populate_descriptor(vdc_t *vdcp, int operation, 1603af08d82Slm66018 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 1613af08d82Slm66018 int cb_type, void *cb_arg, vio_desc_direction_t dir); 1623af08d82Slm66018 static int vdc_do_sync_op(vdc_t *vdcp, int operation, 1633af08d82Slm66018 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 1643af08d82Slm66018 int cb_type, void *cb_arg, vio_desc_direction_t dir); 1653af08d82Slm66018 1663af08d82Slm66018 static int vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp); 1673af08d82Slm66018 static int vdc_drain_response(vdc_t *vdcp); 1681ae08745Sheppo static int vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx); 1693af08d82Slm66018 static int vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep); 170e1ebb9ecSlm66018 static int vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg); 1711ae08745Sheppo 1721ae08745Sheppo /* dkio */ 1731ae08745Sheppo static int vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode); 1741ae08745Sheppo static int vdc_create_fake_geometry(vdc_t *vdc); 1750a55fbb7Slm66018 static int vdc_setup_disk_layout(vdc_t *vdc); 176d10e4ef2Snarayan static int vdc_null_copy_func(vdc_t *vdc, void *from, void *to, 177d10e4ef2Snarayan int mode, int dir); 1784bac2208Snarayan static int vdc_get_wce_convert(vdc_t *vdc, void *from, void *to, 1794bac2208Snarayan int mode, int dir); 1804bac2208Snarayan static int vdc_set_wce_convert(vdc_t *vdc, void *from, void *to, 1814bac2208Snarayan int mode, int dir); 182d10e4ef2Snarayan static int vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, 183d10e4ef2Snarayan int mode, int dir); 184d10e4ef2Snarayan static int vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, 185d10e4ef2Snarayan int mode, int dir); 186d10e4ef2Snarayan static int vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, 187d10e4ef2Snarayan int mode, int dir); 188d10e4ef2Snarayan static int vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, 189d10e4ef2Snarayan int mode, int dir); 190d10e4ef2Snarayan static int vdc_uscsicmd_convert(vdc_t *vdc, void *from, void *to, 191d10e4ef2Snarayan int mode, int dir); 1924bac2208Snarayan static int vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, 1934bac2208Snarayan int mode, int dir); 1944bac2208Snarayan static int vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, 1954bac2208Snarayan int mode, int dir); 1961ae08745Sheppo 1971ae08745Sheppo /* 1981ae08745Sheppo * Module variables 1991ae08745Sheppo */ 200e1ebb9ecSlm66018 201e1ebb9ecSlm66018 /* 202e1ebb9ecSlm66018 * Tunable variables to control how long vdc waits before timing out on 203e1ebb9ecSlm66018 * various operations 204e1ebb9ecSlm66018 */ 205e1ebb9ecSlm66018 static int vdc_retries = 10; 206e1ebb9ecSlm66018 207e1ebb9ecSlm66018 /* calculated from 'vdc_usec_timeout' during attach */ 208e1ebb9ecSlm66018 static uint64_t vdc_hz_timeout; /* units: Hz */ 209e1ebb9ecSlm66018 static uint64_t vdc_usec_timeout = 30 * MICROSEC; /* 30s units: ns */ 210e1ebb9ecSlm66018 2113af08d82Slm66018 static uint64_t vdc_hz_min_ldc_delay; 2123af08d82Slm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC; 2133af08d82Slm66018 static uint64_t vdc_hz_max_ldc_delay; 2143af08d82Slm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC; 2153af08d82Slm66018 2163af08d82Slm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC; 2173af08d82Slm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC; 218e1ebb9ecSlm66018 219e1ebb9ecSlm66018 /* values for dumping - need to run in a tighter loop */ 220e1ebb9ecSlm66018 static uint64_t vdc_usec_timeout_dump = 100 * MILLISEC; /* 0.1s units: ns */ 221e1ebb9ecSlm66018 static int vdc_dump_retries = 100; 222e1ebb9ecSlm66018 223e1ebb9ecSlm66018 /* Count of the number of vdc instances attached */ 224e1ebb9ecSlm66018 static volatile uint32_t vdc_instance_count = 0; 2251ae08745Sheppo 2261ae08745Sheppo /* Soft state pointer */ 2271ae08745Sheppo static void *vdc_state; 2281ae08745Sheppo 2293af08d82Slm66018 /* 2303af08d82Slm66018 * Controlling the verbosity of the error/debug messages 2313af08d82Slm66018 * 2323af08d82Slm66018 * vdc_msglevel - controls level of messages 2333af08d82Slm66018 * vdc_matchinst - 64-bit variable where each bit corresponds 2343af08d82Slm66018 * to the vdc instance the vdc_msglevel applies. 2353af08d82Slm66018 */ 2363af08d82Slm66018 int vdc_msglevel = 0x0; 2373af08d82Slm66018 uint64_t vdc_matchinst = 0ull; 2381ae08745Sheppo 2390a55fbb7Slm66018 /* 2400a55fbb7Slm66018 * Supported vDisk protocol version pairs. 2410a55fbb7Slm66018 * 2420a55fbb7Slm66018 * The first array entry is the latest and preferred version. 2430a55fbb7Slm66018 */ 2440a55fbb7Slm66018 static const vio_ver_t vdc_version[] = {{1, 0}}; 2451ae08745Sheppo 2461ae08745Sheppo static struct cb_ops vdc_cb_ops = { 2471ae08745Sheppo vdc_open, /* cb_open */ 2481ae08745Sheppo vdc_close, /* cb_close */ 2491ae08745Sheppo vdc_strategy, /* cb_strategy */ 2501ae08745Sheppo vdc_print, /* cb_print */ 2511ae08745Sheppo vdc_dump, /* cb_dump */ 2521ae08745Sheppo vdc_read, /* cb_read */ 2531ae08745Sheppo vdc_write, /* cb_write */ 2541ae08745Sheppo vdc_ioctl, /* cb_ioctl */ 2551ae08745Sheppo nodev, /* cb_devmap */ 2561ae08745Sheppo nodev, /* cb_mmap */ 2571ae08745Sheppo nodev, /* cb_segmap */ 2581ae08745Sheppo nochpoll, /* cb_chpoll */ 2591ae08745Sheppo ddi_prop_op, /* cb_prop_op */ 2601ae08745Sheppo NULL, /* cb_str */ 2611ae08745Sheppo D_MP | D_64BIT, /* cb_flag */ 2621ae08745Sheppo CB_REV, /* cb_rev */ 2631ae08745Sheppo vdc_aread, /* cb_aread */ 2641ae08745Sheppo vdc_awrite /* cb_awrite */ 2651ae08745Sheppo }; 2661ae08745Sheppo 2671ae08745Sheppo static struct dev_ops vdc_ops = { 2681ae08745Sheppo DEVO_REV, /* devo_rev */ 2691ae08745Sheppo 0, /* devo_refcnt */ 2701ae08745Sheppo vdc_getinfo, /* devo_getinfo */ 2711ae08745Sheppo nulldev, /* devo_identify */ 2721ae08745Sheppo nulldev, /* devo_probe */ 2731ae08745Sheppo vdc_attach, /* devo_attach */ 2741ae08745Sheppo vdc_detach, /* devo_detach */ 2751ae08745Sheppo nodev, /* devo_reset */ 2761ae08745Sheppo &vdc_cb_ops, /* devo_cb_ops */ 2771ae08745Sheppo NULL, /* devo_bus_ops */ 2781ae08745Sheppo nulldev /* devo_power */ 2791ae08745Sheppo }; 2801ae08745Sheppo 2811ae08745Sheppo static struct modldrv modldrv = { 2821ae08745Sheppo &mod_driverops, 2831ae08745Sheppo "virtual disk client %I%", 2841ae08745Sheppo &vdc_ops, 2851ae08745Sheppo }; 2861ae08745Sheppo 2871ae08745Sheppo static struct modlinkage modlinkage = { 2881ae08745Sheppo MODREV_1, 2891ae08745Sheppo &modldrv, 2901ae08745Sheppo NULL 2911ae08745Sheppo }; 2921ae08745Sheppo 2931ae08745Sheppo /* -------------------------------------------------------------------------- */ 2941ae08745Sheppo 2951ae08745Sheppo /* 2961ae08745Sheppo * Device Driver housekeeping and setup 2971ae08745Sheppo */ 2981ae08745Sheppo 2991ae08745Sheppo int 3001ae08745Sheppo _init(void) 3011ae08745Sheppo { 3021ae08745Sheppo int status; 3031ae08745Sheppo 3041ae08745Sheppo if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0) 3051ae08745Sheppo return (status); 3061ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) 3071ae08745Sheppo ddi_soft_state_fini(&vdc_state); 3084bac2208Snarayan vdc_efi_init(vd_process_ioctl); 3091ae08745Sheppo return (status); 3101ae08745Sheppo } 3111ae08745Sheppo 3121ae08745Sheppo int 3131ae08745Sheppo _info(struct modinfo *modinfop) 3141ae08745Sheppo { 3151ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 3161ae08745Sheppo } 3171ae08745Sheppo 3181ae08745Sheppo int 3191ae08745Sheppo _fini(void) 3201ae08745Sheppo { 3211ae08745Sheppo int status; 3221ae08745Sheppo 3231ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 3241ae08745Sheppo return (status); 3254bac2208Snarayan vdc_efi_fini(); 3261ae08745Sheppo ddi_soft_state_fini(&vdc_state); 3271ae08745Sheppo return (0); 3281ae08745Sheppo } 3291ae08745Sheppo 3301ae08745Sheppo static int 3311ae08745Sheppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 3321ae08745Sheppo { 3331ae08745Sheppo _NOTE(ARGUNUSED(dip)) 3341ae08745Sheppo 3350d0c8d4bSnarayan int instance = VDCUNIT((dev_t)arg); 3361ae08745Sheppo vdc_t *vdc = NULL; 3371ae08745Sheppo 3381ae08745Sheppo switch (cmd) { 3391ae08745Sheppo case DDI_INFO_DEVT2DEVINFO: 3401ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 3411ae08745Sheppo *resultp = NULL; 3421ae08745Sheppo return (DDI_FAILURE); 3431ae08745Sheppo } 3441ae08745Sheppo *resultp = vdc->dip; 3451ae08745Sheppo return (DDI_SUCCESS); 3461ae08745Sheppo case DDI_INFO_DEVT2INSTANCE: 3471ae08745Sheppo *resultp = (void *)(uintptr_t)instance; 3481ae08745Sheppo return (DDI_SUCCESS); 3491ae08745Sheppo default: 3501ae08745Sheppo *resultp = NULL; 3511ae08745Sheppo return (DDI_FAILURE); 3521ae08745Sheppo } 3531ae08745Sheppo } 3541ae08745Sheppo 3551ae08745Sheppo static int 3561ae08745Sheppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3571ae08745Sheppo { 3581ae08745Sheppo int instance; 3591ae08745Sheppo int rv; 3601ae08745Sheppo vdc_t *vdc = NULL; 3611ae08745Sheppo 3621ae08745Sheppo switch (cmd) { 3631ae08745Sheppo case DDI_DETACH: 3641ae08745Sheppo /* the real work happens below */ 3651ae08745Sheppo break; 3661ae08745Sheppo case DDI_SUSPEND: 3671ae08745Sheppo /* nothing to do for this non-device */ 3681ae08745Sheppo return (DDI_SUCCESS); 3691ae08745Sheppo default: 3701ae08745Sheppo return (DDI_FAILURE); 3711ae08745Sheppo } 3721ae08745Sheppo 3731ae08745Sheppo ASSERT(cmd == DDI_DETACH); 3741ae08745Sheppo instance = ddi_get_instance(dip); 3753af08d82Slm66018 DMSGX(1, "[%d] Entered\n", instance); 3761ae08745Sheppo 3771ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 378e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 3791ae08745Sheppo return (DDI_FAILURE); 3801ae08745Sheppo } 3811ae08745Sheppo 3823af08d82Slm66018 if (vdc->open_count) { 3833af08d82Slm66018 DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance); 3841ae08745Sheppo return (DDI_FAILURE); 3851ae08745Sheppo } 3861ae08745Sheppo 3873af08d82Slm66018 DMSG(vdc, 0, "[%d] proceeding...\n", instance); 3883af08d82Slm66018 3893af08d82Slm66018 /* mark instance as detaching */ 3903af08d82Slm66018 vdc->lifecycle = VDC_LC_DETACHING; 3911ae08745Sheppo 3921ae08745Sheppo /* 3931ae08745Sheppo * try and disable callbacks to prevent another handshake 3941ae08745Sheppo */ 3951ae08745Sheppo rv = ldc_set_cb_mode(vdc->ldc_handle, LDC_CB_DISABLE); 3963af08d82Slm66018 DMSG(vdc, 0, "callback disabled (rv=%d)\n", rv); 3971ae08745Sheppo 3981ae08745Sheppo if (vdc->initialized & VDC_THREAD) { 3993af08d82Slm66018 mutex_enter(&vdc->read_lock); 4003af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 4013af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) { 4023af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 4033af08d82Slm66018 cv_signal(&vdc->read_cv); 4041ae08745Sheppo } 4053af08d82Slm66018 4063af08d82Slm66018 mutex_exit(&vdc->read_lock); 4073af08d82Slm66018 4083af08d82Slm66018 /* wake up any thread waiting for connection to come online */ 4093af08d82Slm66018 mutex_enter(&vdc->lock); 4103af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 4113af08d82Slm66018 DMSG(vdc, 0, 4123af08d82Slm66018 "[%d] write reset - move to resetting state...\n", 4133af08d82Slm66018 instance); 4143af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 4153af08d82Slm66018 cv_signal(&vdc->initwait_cv); 4163af08d82Slm66018 } 4173af08d82Slm66018 mutex_exit(&vdc->lock); 4183af08d82Slm66018 4193af08d82Slm66018 /* now wait until state transitions to VDC_STATE_DETACH */ 4203af08d82Slm66018 thread_join(vdc->msg_proc_thr->t_did); 4213af08d82Slm66018 ASSERT(vdc->state == VDC_STATE_DETACH); 4223af08d82Slm66018 DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n", 4233af08d82Slm66018 vdc->instance); 4241ae08745Sheppo } 4251ae08745Sheppo 4261ae08745Sheppo mutex_enter(&vdc->lock); 4271ae08745Sheppo 4281ae08745Sheppo if (vdc->initialized & VDC_DRING) 4291ae08745Sheppo vdc_destroy_descriptor_ring(vdc); 4301ae08745Sheppo 4311ae08745Sheppo if (vdc->initialized & VDC_LDC) 4321ae08745Sheppo vdc_terminate_ldc(vdc); 4331ae08745Sheppo 4341ae08745Sheppo mutex_exit(&vdc->lock); 4351ae08745Sheppo 4361ae08745Sheppo if (vdc->initialized & VDC_MINOR) { 4371ae08745Sheppo ddi_prop_remove_all(dip); 4381ae08745Sheppo ddi_remove_minor_node(dip, NULL); 4391ae08745Sheppo } 4401ae08745Sheppo 4411ae08745Sheppo if (vdc->initialized & VDC_LOCKS) { 4421ae08745Sheppo mutex_destroy(&vdc->lock); 4433af08d82Slm66018 mutex_destroy(&vdc->read_lock); 4443af08d82Slm66018 cv_destroy(&vdc->initwait_cv); 4453af08d82Slm66018 cv_destroy(&vdc->dring_free_cv); 4463af08d82Slm66018 cv_destroy(&vdc->membind_cv); 4473af08d82Slm66018 cv_destroy(&vdc->sync_pending_cv); 4483af08d82Slm66018 cv_destroy(&vdc->sync_blocked_cv); 4493af08d82Slm66018 cv_destroy(&vdc->read_cv); 4503af08d82Slm66018 cv_destroy(&vdc->running_cv); 4511ae08745Sheppo } 4521ae08745Sheppo 4531ae08745Sheppo if (vdc->minfo) 4541ae08745Sheppo kmem_free(vdc->minfo, sizeof (struct dk_minfo)); 4551ae08745Sheppo 4561ae08745Sheppo if (vdc->cinfo) 4571ae08745Sheppo kmem_free(vdc->cinfo, sizeof (struct dk_cinfo)); 4581ae08745Sheppo 4591ae08745Sheppo if (vdc->vtoc) 4601ae08745Sheppo kmem_free(vdc->vtoc, sizeof (struct vtoc)); 4611ae08745Sheppo 4620a55fbb7Slm66018 if (vdc->label) 4630a55fbb7Slm66018 kmem_free(vdc->label, DK_LABEL_SIZE); 4640a55fbb7Slm66018 4654bac2208Snarayan if (vdc->devid) { 4664bac2208Snarayan ddi_devid_unregister(dip); 4674bac2208Snarayan ddi_devid_free(vdc->devid); 4684bac2208Snarayan } 4694bac2208Snarayan 4701ae08745Sheppo if (vdc->initialized & VDC_SOFT_STATE) 4711ae08745Sheppo ddi_soft_state_free(vdc_state, instance); 4721ae08745Sheppo 4733af08d82Slm66018 DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc); 4741ae08745Sheppo 4751ae08745Sheppo return (DDI_SUCCESS); 4761ae08745Sheppo } 4771ae08745Sheppo 4781ae08745Sheppo 4791ae08745Sheppo static int 4801ae08745Sheppo vdc_do_attach(dev_info_t *dip) 4811ae08745Sheppo { 4821ae08745Sheppo int instance; 4831ae08745Sheppo vdc_t *vdc = NULL; 4841ae08745Sheppo int status; 4851ae08745Sheppo 4861ae08745Sheppo ASSERT(dip != NULL); 4871ae08745Sheppo 4881ae08745Sheppo instance = ddi_get_instance(dip); 4891ae08745Sheppo if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) { 490e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure", 491e1ebb9ecSlm66018 instance); 4921ae08745Sheppo return (DDI_FAILURE); 4931ae08745Sheppo } 4941ae08745Sheppo 4951ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 496e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 4971ae08745Sheppo return (DDI_FAILURE); 4981ae08745Sheppo } 4991ae08745Sheppo 5001ae08745Sheppo /* 5011ae08745Sheppo * We assign the value to initialized in this case to zero out the 5021ae08745Sheppo * variable and then set bits in it to indicate what has been done 5031ae08745Sheppo */ 5041ae08745Sheppo vdc->initialized = VDC_SOFT_STATE; 5051ae08745Sheppo 5061ae08745Sheppo vdc_hz_timeout = drv_usectohz(vdc_usec_timeout); 5073af08d82Slm66018 5083af08d82Slm66018 vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc); 5093af08d82Slm66018 vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc); 5101ae08745Sheppo 5111ae08745Sheppo vdc->dip = dip; 5121ae08745Sheppo vdc->instance = instance; 5133af08d82Slm66018 vdc->open_count = 0; 5141ae08745Sheppo vdc->vdisk_type = VD_DISK_TYPE_UNK; 5154bac2208Snarayan vdc->vdisk_label = VD_DISK_LABEL_UNK; 5163af08d82Slm66018 vdc->state = VDC_STATE_INIT; 5173af08d82Slm66018 vdc->lifecycle = VDC_LC_ATTACHING; 5181ae08745Sheppo vdc->ldc_state = 0; 5191ae08745Sheppo vdc->session_id = 0; 5201ae08745Sheppo vdc->block_size = DEV_BSIZE; 5218e6a2a04Slm66018 vdc->max_xfer_sz = maxphys / DEV_BSIZE; 5221ae08745Sheppo 5231ae08745Sheppo vdc->vtoc = NULL; 5241ae08745Sheppo vdc->cinfo = NULL; 5251ae08745Sheppo vdc->minfo = NULL; 5261ae08745Sheppo 5271ae08745Sheppo mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL); 5283af08d82Slm66018 cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL); 5293af08d82Slm66018 cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL); 5303af08d82Slm66018 cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL); 5313af08d82Slm66018 cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL); 5323af08d82Slm66018 5333af08d82Slm66018 vdc->threads_pending = 0; 5343af08d82Slm66018 vdc->sync_op_pending = B_FALSE; 5353af08d82Slm66018 vdc->sync_op_blocked = B_FALSE; 5363af08d82Slm66018 cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL); 5373af08d82Slm66018 cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL); 5383af08d82Slm66018 5393af08d82Slm66018 /* init blocking msg read functionality */ 5403af08d82Slm66018 mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL); 5413af08d82Slm66018 cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL); 5423af08d82Slm66018 vdc->read_state = VDC_READ_IDLE; 5433af08d82Slm66018 5441ae08745Sheppo vdc->initialized |= VDC_LOCKS; 5451ae08745Sheppo 5463af08d82Slm66018 /* initialise LDC channel which will be used to communicate with vds */ 5473af08d82Slm66018 if ((status = vdc_do_ldc_init(vdc)) != 0) { 5483af08d82Slm66018 cmn_err(CE_NOTE, "[%d] Couldn't initialize LDC", instance); 5493af08d82Slm66018 goto return_status; 5503af08d82Slm66018 } 5513af08d82Slm66018 5523af08d82Slm66018 /* initialize the thread responsible for managing state with server */ 5533af08d82Slm66018 vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread, 5541ae08745Sheppo vdc, 0, &p0, TS_RUN, minclsyspri); 5553af08d82Slm66018 if (vdc->msg_proc_thr == NULL) { 5561ae08745Sheppo cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread", 5571ae08745Sheppo instance); 5581ae08745Sheppo return (DDI_FAILURE); 5591ae08745Sheppo } 5603af08d82Slm66018 5611ae08745Sheppo vdc->initialized |= VDC_THREAD; 5621ae08745Sheppo 563e1ebb9ecSlm66018 atomic_inc_32(&vdc_instance_count); 5641ae08745Sheppo 5650a55fbb7Slm66018 /* 5660a55fbb7Slm66018 * Once the handshake is complete, we can use the DRing to send 5670a55fbb7Slm66018 * requests to the vDisk server to calculate the geometry and 5680a55fbb7Slm66018 * VTOC of the "disk" 5690a55fbb7Slm66018 */ 5700a55fbb7Slm66018 status = vdc_setup_disk_layout(vdc); 5710a55fbb7Slm66018 if (status != 0) { 5723af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to discover disk layout (err%d)", 5730a55fbb7Slm66018 vdc->instance, status); 5743af08d82Slm66018 goto return_status; 5751ae08745Sheppo } 5761ae08745Sheppo 5771ae08745Sheppo /* 5781ae08745Sheppo * Now that we have the device info we can create the 5791ae08745Sheppo * device nodes and properties 5801ae08745Sheppo */ 5811ae08745Sheppo status = vdc_create_device_nodes(vdc); 5821ae08745Sheppo if (status) { 5833af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to create device nodes", 5841ae08745Sheppo instance); 5853af08d82Slm66018 goto return_status; 5861ae08745Sheppo } 5871ae08745Sheppo status = vdc_create_device_nodes_props(vdc); 5881ae08745Sheppo if (status) { 5893af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to create device nodes" 5900a55fbb7Slm66018 " properties (%d)", instance, status); 5913af08d82Slm66018 goto return_status; 5921ae08745Sheppo } 5931ae08745Sheppo 5944bac2208Snarayan /* 5954bac2208Snarayan * Setup devid 5964bac2208Snarayan */ 5974bac2208Snarayan if (vdc_setup_devid(vdc)) { 5983af08d82Slm66018 DMSG(vdc, 0, "[%d] No device id available\n", instance); 5994bac2208Snarayan } 6004bac2208Snarayan 6011ae08745Sheppo ddi_report_dev(dip); 6023af08d82Slm66018 vdc->lifecycle = VDC_LC_ONLINE; 6033af08d82Slm66018 DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance); 6041ae08745Sheppo 6053af08d82Slm66018 return_status: 6063af08d82Slm66018 DMSG(vdc, 0, "[%d] Attach completed\n", instance); 6071ae08745Sheppo return (status); 6081ae08745Sheppo } 6091ae08745Sheppo 6101ae08745Sheppo static int 6111ae08745Sheppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 6121ae08745Sheppo { 6131ae08745Sheppo int status; 6141ae08745Sheppo 6151ae08745Sheppo switch (cmd) { 6161ae08745Sheppo case DDI_ATTACH: 6171ae08745Sheppo if ((status = vdc_do_attach(dip)) != 0) 6181ae08745Sheppo (void) vdc_detach(dip, DDI_DETACH); 6191ae08745Sheppo return (status); 6201ae08745Sheppo case DDI_RESUME: 6211ae08745Sheppo /* nothing to do for this non-device */ 6221ae08745Sheppo return (DDI_SUCCESS); 6231ae08745Sheppo default: 6241ae08745Sheppo return (DDI_FAILURE); 6251ae08745Sheppo } 6261ae08745Sheppo } 6271ae08745Sheppo 6281ae08745Sheppo static int 6291ae08745Sheppo vdc_do_ldc_init(vdc_t *vdc) 6301ae08745Sheppo { 6311ae08745Sheppo int status = 0; 6321ae08745Sheppo ldc_status_t ldc_state; 6331ae08745Sheppo ldc_attr_t ldc_attr; 6341ae08745Sheppo uint64_t ldc_id = 0; 6351ae08745Sheppo dev_info_t *dip = NULL; 6361ae08745Sheppo 6371ae08745Sheppo ASSERT(vdc != NULL); 6381ae08745Sheppo 6391ae08745Sheppo dip = vdc->dip; 6401ae08745Sheppo vdc->initialized |= VDC_LDC; 6411ae08745Sheppo 6421ae08745Sheppo if ((status = vdc_get_ldc_id(dip, &ldc_id)) != 0) { 6433af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to get LDC channel ID property", 644e1ebb9ecSlm66018 vdc->instance); 6451ae08745Sheppo return (EIO); 6461ae08745Sheppo } 6471ae08745Sheppo vdc->ldc_id = ldc_id; 6481ae08745Sheppo 6491ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK; 6501ae08745Sheppo ldc_attr.instance = vdc->instance; 6511ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; /* unreliable transport */ 652e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 6531ae08745Sheppo 6541ae08745Sheppo if ((vdc->initialized & VDC_LDC_INIT) == 0) { 6551ae08745Sheppo status = ldc_init(ldc_id, &ldc_attr, &vdc->ldc_handle); 6561ae08745Sheppo if (status != 0) { 6573af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d", 6581ae08745Sheppo vdc->instance, ldc_id, status); 6591ae08745Sheppo return (status); 6601ae08745Sheppo } 6611ae08745Sheppo vdc->initialized |= VDC_LDC_INIT; 6621ae08745Sheppo } 6631ae08745Sheppo status = ldc_status(vdc->ldc_handle, &ldc_state); 6641ae08745Sheppo if (status != 0) { 6653af08d82Slm66018 DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]", 666e1ebb9ecSlm66018 vdc->instance, status); 6671ae08745Sheppo return (status); 6681ae08745Sheppo } 6691ae08745Sheppo vdc->ldc_state = ldc_state; 6701ae08745Sheppo 6711ae08745Sheppo if ((vdc->initialized & VDC_LDC_CB) == 0) { 6721ae08745Sheppo status = ldc_reg_callback(vdc->ldc_handle, vdc_handle_cb, 6731ae08745Sheppo (caddr_t)vdc); 6741ae08745Sheppo if (status != 0) { 6753af08d82Slm66018 DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)", 676e1ebb9ecSlm66018 vdc->instance, status); 6771ae08745Sheppo return (status); 6781ae08745Sheppo } 6791ae08745Sheppo vdc->initialized |= VDC_LDC_CB; 6801ae08745Sheppo } 6811ae08745Sheppo 6821ae08745Sheppo vdc->initialized |= VDC_LDC; 6831ae08745Sheppo 6841ae08745Sheppo /* 6851ae08745Sheppo * At this stage we have initialised LDC, we will now try and open 6861ae08745Sheppo * the connection. 6871ae08745Sheppo */ 6881ae08745Sheppo if (vdc->ldc_state == LDC_INIT) { 6891ae08745Sheppo status = ldc_open(vdc->ldc_handle); 6901ae08745Sheppo if (status != 0) { 6913af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d", 6921ae08745Sheppo vdc->instance, vdc->ldc_id, status); 6931ae08745Sheppo return (status); 6941ae08745Sheppo } 6951ae08745Sheppo vdc->initialized |= VDC_LDC_OPEN; 6961ae08745Sheppo } 6971ae08745Sheppo 6981ae08745Sheppo return (status); 6991ae08745Sheppo } 7001ae08745Sheppo 7011ae08745Sheppo static int 7021ae08745Sheppo vdc_start_ldc_connection(vdc_t *vdc) 7031ae08745Sheppo { 7041ae08745Sheppo int status = 0; 7051ae08745Sheppo 7061ae08745Sheppo ASSERT(vdc != NULL); 7071ae08745Sheppo 7083af08d82Slm66018 ASSERT(MUTEX_HELD(&vdc->lock)); 7091ae08745Sheppo 7100a55fbb7Slm66018 status = vdc_do_ldc_up(vdc); 7111ae08745Sheppo 7123af08d82Slm66018 DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance); 7131ae08745Sheppo 7143af08d82Slm66018 return (status); 7153af08d82Slm66018 } 7163af08d82Slm66018 7173af08d82Slm66018 static int 7183af08d82Slm66018 vdc_stop_ldc_connection(vdc_t *vdcp) 7193af08d82Slm66018 { 7203af08d82Slm66018 int status; 7213af08d82Slm66018 7223af08d82Slm66018 DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n", 7233af08d82Slm66018 vdcp->state); 7243af08d82Slm66018 7253af08d82Slm66018 status = ldc_down(vdcp->ldc_handle); 7263af08d82Slm66018 DMSG(vdcp, 0, "ldc_down() = %d\n", status); 7273af08d82Slm66018 7283af08d82Slm66018 vdcp->initialized &= ~VDC_HANDSHAKE; 7293af08d82Slm66018 DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized); 7301ae08745Sheppo 7311ae08745Sheppo return (status); 7321ae08745Sheppo } 7331ae08745Sheppo 7344bac2208Snarayan static int 7354bac2208Snarayan vdc_create_device_nodes_efi(vdc_t *vdc) 7364bac2208Snarayan { 7374bac2208Snarayan ddi_remove_minor_node(vdc->dip, "h"); 7384bac2208Snarayan ddi_remove_minor_node(vdc->dip, "h,raw"); 7394bac2208Snarayan 7404bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK, 7414bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 7424bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 7434bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'", 7444bac2208Snarayan vdc->instance); 7454bac2208Snarayan return (EIO); 7464bac2208Snarayan } 7474bac2208Snarayan 7484bac2208Snarayan /* if any device node is created we set this flag */ 7494bac2208Snarayan vdc->initialized |= VDC_MINOR; 7504bac2208Snarayan 7514bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR, 7524bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 7534bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 7544bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'", 7554bac2208Snarayan vdc->instance); 7564bac2208Snarayan return (EIO); 7574bac2208Snarayan } 7584bac2208Snarayan 7594bac2208Snarayan return (0); 7604bac2208Snarayan } 7614bac2208Snarayan 7624bac2208Snarayan static int 7634bac2208Snarayan vdc_create_device_nodes_vtoc(vdc_t *vdc) 7644bac2208Snarayan { 7654bac2208Snarayan ddi_remove_minor_node(vdc->dip, "wd"); 7664bac2208Snarayan ddi_remove_minor_node(vdc->dip, "wd,raw"); 7674bac2208Snarayan 7684bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK, 7694bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 7704bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 7714bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'", 7724bac2208Snarayan vdc->instance); 7734bac2208Snarayan return (EIO); 7744bac2208Snarayan } 7754bac2208Snarayan 7764bac2208Snarayan /* if any device node is created we set this flag */ 7774bac2208Snarayan vdc->initialized |= VDC_MINOR; 7784bac2208Snarayan 7794bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR, 7804bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 7814bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 7824bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'", 7834bac2208Snarayan vdc->instance); 7844bac2208Snarayan return (EIO); 7854bac2208Snarayan } 7864bac2208Snarayan 7874bac2208Snarayan return (0); 7884bac2208Snarayan } 7891ae08745Sheppo 7901ae08745Sheppo /* 7911ae08745Sheppo * Function: 7921ae08745Sheppo * vdc_create_device_nodes 7931ae08745Sheppo * 7941ae08745Sheppo * Description: 7951ae08745Sheppo * This function creates the block and character device nodes under 7961ae08745Sheppo * /devices along with the node properties. It is called as part of 7971ae08745Sheppo * the attach(9E) of the instance during the handshake with vds after 7981ae08745Sheppo * vds has sent the attributes to vdc. 7991ae08745Sheppo * 8001ae08745Sheppo * If the device is of type VD_DISK_TYPE_SLICE then the minor node 8011ae08745Sheppo * of 2 is used in keeping with the Solaris convention that slice 2 8021ae08745Sheppo * refers to a whole disk. Slices start at 'a' 8031ae08745Sheppo * 8041ae08745Sheppo * Parameters: 8051ae08745Sheppo * vdc - soft state pointer 8061ae08745Sheppo * 8071ae08745Sheppo * Return Values 8081ae08745Sheppo * 0 - Success 8091ae08745Sheppo * EIO - Failed to create node 8101ae08745Sheppo * EINVAL - Unknown type of disk exported 8111ae08745Sheppo */ 8121ae08745Sheppo static int 8131ae08745Sheppo vdc_create_device_nodes(vdc_t *vdc) 8141ae08745Sheppo { 8154bac2208Snarayan char name[sizeof ("s,raw")]; 8161ae08745Sheppo dev_info_t *dip = NULL; 8174bac2208Snarayan int instance, status; 8181ae08745Sheppo int num_slices = 1; 8191ae08745Sheppo int i; 8201ae08745Sheppo 8211ae08745Sheppo ASSERT(vdc != NULL); 8221ae08745Sheppo 8231ae08745Sheppo instance = vdc->instance; 8241ae08745Sheppo dip = vdc->dip; 8251ae08745Sheppo 8261ae08745Sheppo switch (vdc->vdisk_type) { 8271ae08745Sheppo case VD_DISK_TYPE_DISK: 8281ae08745Sheppo num_slices = V_NUMPAR; 8291ae08745Sheppo break; 8301ae08745Sheppo case VD_DISK_TYPE_SLICE: 8311ae08745Sheppo num_slices = 1; 8321ae08745Sheppo break; 8331ae08745Sheppo case VD_DISK_TYPE_UNK: 8341ae08745Sheppo default: 8351ae08745Sheppo return (EINVAL); 8361ae08745Sheppo } 8371ae08745Sheppo 8384bac2208Snarayan /* 8394bac2208Snarayan * Minor nodes are different for EFI disks: EFI disks do not have 8404bac2208Snarayan * a minor node 'g' for the minor number corresponding to slice 8414bac2208Snarayan * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd' 8424bac2208Snarayan * representing the whole disk. 8434bac2208Snarayan */ 8441ae08745Sheppo for (i = 0; i < num_slices; i++) { 8454bac2208Snarayan 8464bac2208Snarayan if (i == VD_EFI_WD_SLICE) { 8474bac2208Snarayan if (vdc->vdisk_label == VD_DISK_LABEL_EFI) 8484bac2208Snarayan status = vdc_create_device_nodes_efi(vdc); 8494bac2208Snarayan else 8504bac2208Snarayan status = vdc_create_device_nodes_vtoc(vdc); 8514bac2208Snarayan if (status != 0) 8524bac2208Snarayan return (status); 8534bac2208Snarayan continue; 8544bac2208Snarayan } 8554bac2208Snarayan 8561ae08745Sheppo (void) snprintf(name, sizeof (name), "%c", 'a' + i); 8571ae08745Sheppo if (ddi_create_minor_node(dip, name, S_IFBLK, 8581ae08745Sheppo VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 859e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'", 860e1ebb9ecSlm66018 instance, name); 8611ae08745Sheppo return (EIO); 8621ae08745Sheppo } 8631ae08745Sheppo 8641ae08745Sheppo /* if any device node is created we set this flag */ 8651ae08745Sheppo vdc->initialized |= VDC_MINOR; 8661ae08745Sheppo 8671ae08745Sheppo (void) snprintf(name, sizeof (name), "%c%s", 8681ae08745Sheppo 'a' + i, ",raw"); 8691ae08745Sheppo if (ddi_create_minor_node(dip, name, S_IFCHR, 8701ae08745Sheppo VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 871e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'", 872e1ebb9ecSlm66018 instance, name); 8731ae08745Sheppo return (EIO); 8741ae08745Sheppo } 8751ae08745Sheppo } 8761ae08745Sheppo 8771ae08745Sheppo return (0); 8781ae08745Sheppo } 8791ae08745Sheppo 8801ae08745Sheppo /* 8811ae08745Sheppo * Function: 8821ae08745Sheppo * vdc_create_device_nodes_props 8831ae08745Sheppo * 8841ae08745Sheppo * Description: 8851ae08745Sheppo * This function creates the block and character device nodes under 8861ae08745Sheppo * /devices along with the node properties. It is called as part of 8871ae08745Sheppo * the attach(9E) of the instance during the handshake with vds after 8881ae08745Sheppo * vds has sent the attributes to vdc. 8891ae08745Sheppo * 8901ae08745Sheppo * Parameters: 8911ae08745Sheppo * vdc - soft state pointer 8921ae08745Sheppo * 8931ae08745Sheppo * Return Values 8941ae08745Sheppo * 0 - Success 8951ae08745Sheppo * EIO - Failed to create device node property 8961ae08745Sheppo * EINVAL - Unknown type of disk exported 8971ae08745Sheppo */ 8981ae08745Sheppo static int 8991ae08745Sheppo vdc_create_device_nodes_props(vdc_t *vdc) 9001ae08745Sheppo { 9011ae08745Sheppo dev_info_t *dip = NULL; 9021ae08745Sheppo int instance; 9031ae08745Sheppo int num_slices = 1; 9041ae08745Sheppo int64_t size = 0; 9051ae08745Sheppo dev_t dev; 9061ae08745Sheppo int rv; 9071ae08745Sheppo int i; 9081ae08745Sheppo 9091ae08745Sheppo ASSERT(vdc != NULL); 9101ae08745Sheppo 9111ae08745Sheppo instance = vdc->instance; 9121ae08745Sheppo dip = vdc->dip; 9131ae08745Sheppo 9141ae08745Sheppo if ((vdc->vtoc == NULL) || (vdc->vtoc->v_sanity != VTOC_SANE)) { 9153af08d82Slm66018 DMSG(vdc, 0, "![%d] Could not create device node property." 9161ae08745Sheppo " No VTOC available", instance); 9171ae08745Sheppo return (ENXIO); 9181ae08745Sheppo } 9191ae08745Sheppo 9201ae08745Sheppo switch (vdc->vdisk_type) { 9211ae08745Sheppo case VD_DISK_TYPE_DISK: 9221ae08745Sheppo num_slices = V_NUMPAR; 9231ae08745Sheppo break; 9241ae08745Sheppo case VD_DISK_TYPE_SLICE: 9251ae08745Sheppo num_slices = 1; 9261ae08745Sheppo break; 9271ae08745Sheppo case VD_DISK_TYPE_UNK: 9281ae08745Sheppo default: 9291ae08745Sheppo return (EINVAL); 9301ae08745Sheppo } 9311ae08745Sheppo 9321ae08745Sheppo for (i = 0; i < num_slices; i++) { 9331ae08745Sheppo dev = makedevice(ddi_driver_major(dip), 9341ae08745Sheppo VD_MAKE_DEV(instance, i)); 9351ae08745Sheppo 9361ae08745Sheppo size = vdc->vtoc->v_part[i].p_size * vdc->vtoc->v_sectorsz; 9373af08d82Slm66018 DMSG(vdc, 0, "[%d] sz %ld (%ld Mb) p_size %lx\n", 938e1ebb9ecSlm66018 instance, size, size / (1024 * 1024), 9391ae08745Sheppo vdc->vtoc->v_part[i].p_size); 9401ae08745Sheppo 9411ae08745Sheppo rv = ddi_prop_update_int64(dev, dip, VDC_SIZE_PROP_NAME, size); 9421ae08745Sheppo if (rv != DDI_PROP_SUCCESS) { 943e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop of [%ld]", 944e1ebb9ecSlm66018 instance, VDC_SIZE_PROP_NAME, size); 9451ae08745Sheppo return (EIO); 9461ae08745Sheppo } 9471ae08745Sheppo 9481ae08745Sheppo rv = ddi_prop_update_int64(dev, dip, VDC_NBLOCKS_PROP_NAME, 9491ae08745Sheppo lbtodb(size)); 9501ae08745Sheppo if (rv != DDI_PROP_SUCCESS) { 951e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop [%llu]", 9521ae08745Sheppo instance, VDC_NBLOCKS_PROP_NAME, lbtodb(size)); 9531ae08745Sheppo return (EIO); 9541ae08745Sheppo } 9551ae08745Sheppo } 9561ae08745Sheppo 9571ae08745Sheppo return (0); 9581ae08745Sheppo } 9591ae08745Sheppo 9601ae08745Sheppo static int 9611ae08745Sheppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred) 9621ae08745Sheppo { 9631ae08745Sheppo _NOTE(ARGUNUSED(cred)) 9641ae08745Sheppo 9651ae08745Sheppo int instance; 9661ae08745Sheppo vdc_t *vdc; 9671ae08745Sheppo 9681ae08745Sheppo ASSERT(dev != NULL); 9690d0c8d4bSnarayan instance = VDCUNIT(*dev); 9701ae08745Sheppo 9711ae08745Sheppo if ((otyp != OTYP_CHR) && (otyp != OTYP_BLK)) 9721ae08745Sheppo return (EINVAL); 9731ae08745Sheppo 9741ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 975e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 9761ae08745Sheppo return (ENXIO); 9771ae08745Sheppo } 9781ae08745Sheppo 9793af08d82Slm66018 DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n", 9803af08d82Slm66018 getminor(*dev), flag, otyp); 9811ae08745Sheppo 9821ae08745Sheppo mutex_enter(&vdc->lock); 9833af08d82Slm66018 vdc->open_count++; 9841ae08745Sheppo mutex_exit(&vdc->lock); 9851ae08745Sheppo 9861ae08745Sheppo return (0); 9871ae08745Sheppo } 9881ae08745Sheppo 9891ae08745Sheppo static int 9901ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred) 9911ae08745Sheppo { 9921ae08745Sheppo _NOTE(ARGUNUSED(cred)) 9931ae08745Sheppo 9941ae08745Sheppo int instance; 9951ae08745Sheppo vdc_t *vdc; 9961ae08745Sheppo 9970d0c8d4bSnarayan instance = VDCUNIT(dev); 9981ae08745Sheppo 9991ae08745Sheppo if ((otyp != OTYP_CHR) && (otyp != OTYP_BLK)) 10001ae08745Sheppo return (EINVAL); 10011ae08745Sheppo 10021ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1003e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 10041ae08745Sheppo return (ENXIO); 10051ae08745Sheppo } 10061ae08745Sheppo 10073af08d82Slm66018 DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp); 10081ae08745Sheppo if (vdc->dkio_flush_pending) { 10093af08d82Slm66018 DMSG(vdc, 0, 10103af08d82Slm66018 "[%d] Cannot detach: %d outstanding DKIO flushes\n", 1011e1ebb9ecSlm66018 instance, vdc->dkio_flush_pending); 10121ae08745Sheppo return (EBUSY); 10131ae08745Sheppo } 10141ae08745Sheppo 10151ae08745Sheppo /* 10161ae08745Sheppo * Should not need the mutex here, since the framework should protect 10171ae08745Sheppo * against more opens on this device, but just in case. 10181ae08745Sheppo */ 10191ae08745Sheppo mutex_enter(&vdc->lock); 10203af08d82Slm66018 vdc->open_count--; 10211ae08745Sheppo mutex_exit(&vdc->lock); 10221ae08745Sheppo 10231ae08745Sheppo return (0); 10241ae08745Sheppo } 10251ae08745Sheppo 10261ae08745Sheppo static int 10271ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 10281ae08745Sheppo { 10291ae08745Sheppo _NOTE(ARGUNUSED(credp)) 10301ae08745Sheppo _NOTE(ARGUNUSED(rvalp)) 10311ae08745Sheppo 10321ae08745Sheppo return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode)); 10331ae08745Sheppo } 10341ae08745Sheppo 10351ae08745Sheppo static int 10361ae08745Sheppo vdc_print(dev_t dev, char *str) 10371ae08745Sheppo { 10380d0c8d4bSnarayan cmn_err(CE_NOTE, "vdc%d: %s", VDCUNIT(dev), str); 10391ae08745Sheppo return (0); 10401ae08745Sheppo } 10411ae08745Sheppo 10421ae08745Sheppo static int 10431ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 10441ae08745Sheppo { 1045d10e4ef2Snarayan int rv; 1046d10e4ef2Snarayan size_t nbytes = nblk * DEV_BSIZE; 10470d0c8d4bSnarayan int instance = VDCUNIT(dev); 1048d10e4ef2Snarayan vdc_t *vdc = NULL; 10491ae08745Sheppo 10501ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1051e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 10521ae08745Sheppo return (ENXIO); 10531ae08745Sheppo } 10541ae08745Sheppo 10553af08d82Slm66018 DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n", 10563af08d82Slm66018 instance, nbytes, blkno, (void *)addr); 10573af08d82Slm66018 rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes, 10580d0c8d4bSnarayan VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir); 10593af08d82Slm66018 if (rv) { 10603af08d82Slm66018 DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv); 10611ae08745Sheppo return (rv); 10621ae08745Sheppo } 10631ae08745Sheppo 10643af08d82Slm66018 if (ddi_in_panic()) 10653af08d82Slm66018 (void) vdc_drain_response(vdc); 10663af08d82Slm66018 10673af08d82Slm66018 DMSG(vdc, 0, "[%d] End\n", instance); 10683af08d82Slm66018 10693af08d82Slm66018 return (0); 10703af08d82Slm66018 } 10713af08d82Slm66018 10721ae08745Sheppo /* -------------------------------------------------------------------------- */ 10731ae08745Sheppo 10741ae08745Sheppo /* 10751ae08745Sheppo * Disk access routines 10761ae08745Sheppo * 10771ae08745Sheppo */ 10781ae08745Sheppo 10791ae08745Sheppo /* 10801ae08745Sheppo * vdc_strategy() 10811ae08745Sheppo * 10821ae08745Sheppo * Return Value: 10831ae08745Sheppo * 0: As per strategy(9E), the strategy() function must return 0 10841ae08745Sheppo * [ bioerror(9f) sets b_flags to the proper error code ] 10851ae08745Sheppo */ 10861ae08745Sheppo static int 10871ae08745Sheppo vdc_strategy(struct buf *buf) 10881ae08745Sheppo { 10891ae08745Sheppo int rv = -1; 10901ae08745Sheppo vdc_t *vdc = NULL; 10910d0c8d4bSnarayan int instance = VDCUNIT(buf->b_edev); 10921ae08745Sheppo int op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE; 10931ae08745Sheppo 10941ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1095e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 10961ae08745Sheppo bioerror(buf, ENXIO); 10971ae08745Sheppo biodone(buf); 10981ae08745Sheppo return (0); 10991ae08745Sheppo } 11001ae08745Sheppo 11013af08d82Slm66018 DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n", 11023af08d82Slm66018 instance, (buf->b_flags & B_READ) ? "Read" : "Write", 11033af08d82Slm66018 buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr); 1104d10e4ef2Snarayan DTRACE_IO2(vstart, buf_t *, buf, vdc_t *, vdc); 1105d10e4ef2Snarayan 11061ae08745Sheppo bp_mapin(buf); 11071ae08745Sheppo 11083af08d82Slm66018 rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr, 11090d0c8d4bSnarayan buf->b_bcount, VDCPART(buf->b_edev), buf->b_lblkno, 11103af08d82Slm66018 CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir : 11113af08d82Slm66018 VIO_write_dir); 11123af08d82Slm66018 11133af08d82Slm66018 ASSERT(rv == 0 || rv == EINVAL); 11141ae08745Sheppo 1115d10e4ef2Snarayan /* 1116d10e4ef2Snarayan * If the request was successfully sent, the strategy call returns and 1117d10e4ef2Snarayan * the ACK handler calls the bioxxx functions when the vDisk server is 1118d10e4ef2Snarayan * done. 1119d10e4ef2Snarayan */ 1120d10e4ef2Snarayan if (rv) { 11213af08d82Slm66018 DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv); 11221ae08745Sheppo bioerror(buf, rv); 11231ae08745Sheppo biodone(buf); 1124d10e4ef2Snarayan } 1125d10e4ef2Snarayan 11261ae08745Sheppo return (0); 11271ae08745Sheppo } 11281ae08745Sheppo 11290d0c8d4bSnarayan /* 11300d0c8d4bSnarayan * Function: 11310d0c8d4bSnarayan * vdc_min 11320d0c8d4bSnarayan * 11330d0c8d4bSnarayan * Description: 11340d0c8d4bSnarayan * Routine to limit the size of a data transfer. Used in 11350d0c8d4bSnarayan * conjunction with physio(9F). 11360d0c8d4bSnarayan * 11370d0c8d4bSnarayan * Arguments: 11380d0c8d4bSnarayan * bp - pointer to the indicated buf(9S) struct. 11390d0c8d4bSnarayan * 11400d0c8d4bSnarayan */ 11410d0c8d4bSnarayan static void 11420d0c8d4bSnarayan vdc_min(struct buf *bufp) 11430d0c8d4bSnarayan { 11440d0c8d4bSnarayan vdc_t *vdc = NULL; 11450d0c8d4bSnarayan int instance = VDCUNIT(bufp->b_edev); 11460d0c8d4bSnarayan 11470d0c8d4bSnarayan vdc = ddi_get_soft_state(vdc_state, instance); 11480d0c8d4bSnarayan VERIFY(vdc != NULL); 11490d0c8d4bSnarayan 11500d0c8d4bSnarayan if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) { 11510d0c8d4bSnarayan bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size; 11520d0c8d4bSnarayan } 11530d0c8d4bSnarayan } 11541ae08745Sheppo 11551ae08745Sheppo static int 11561ae08745Sheppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred) 11571ae08745Sheppo { 11581ae08745Sheppo _NOTE(ARGUNUSED(cred)) 11591ae08745Sheppo 11600d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 11610d0c8d4bSnarayan return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio)); 11621ae08745Sheppo } 11631ae08745Sheppo 11641ae08745Sheppo static int 11651ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred) 11661ae08745Sheppo { 11671ae08745Sheppo _NOTE(ARGUNUSED(cred)) 11681ae08745Sheppo 11690d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 11700d0c8d4bSnarayan return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio)); 11711ae08745Sheppo } 11721ae08745Sheppo 11731ae08745Sheppo static int 11741ae08745Sheppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred) 11751ae08745Sheppo { 11761ae08745Sheppo _NOTE(ARGUNUSED(cred)) 11771ae08745Sheppo 11780d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 11790d0c8d4bSnarayan return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio)); 11801ae08745Sheppo } 11811ae08745Sheppo 11821ae08745Sheppo static int 11831ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred) 11841ae08745Sheppo { 11851ae08745Sheppo _NOTE(ARGUNUSED(cred)) 11861ae08745Sheppo 11870d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 11880d0c8d4bSnarayan return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio)); 11891ae08745Sheppo } 11901ae08745Sheppo 11911ae08745Sheppo 11921ae08745Sheppo /* -------------------------------------------------------------------------- */ 11931ae08745Sheppo 11941ae08745Sheppo /* 11951ae08745Sheppo * Handshake support 11961ae08745Sheppo */ 11971ae08745Sheppo 11981ae08745Sheppo 11990a55fbb7Slm66018 /* 12000a55fbb7Slm66018 * Function: 12010a55fbb7Slm66018 * vdc_init_ver_negotiation() 12020a55fbb7Slm66018 * 12030a55fbb7Slm66018 * Description: 12040a55fbb7Slm66018 * 12050a55fbb7Slm66018 * Arguments: 12060a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 12070a55fbb7Slm66018 * 12080a55fbb7Slm66018 * Return Code: 12090a55fbb7Slm66018 * 0 - Success 12100a55fbb7Slm66018 */ 12111ae08745Sheppo static int 12120a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver) 12131ae08745Sheppo { 12141ae08745Sheppo vio_ver_msg_t pkt; 12151ae08745Sheppo size_t msglen = sizeof (pkt); 12161ae08745Sheppo int status = -1; 12171ae08745Sheppo 12181ae08745Sheppo ASSERT(vdc != NULL); 12191ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 12201ae08745Sheppo 12213af08d82Slm66018 DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance); 1222e1ebb9ecSlm66018 12231ae08745Sheppo /* 12241ae08745Sheppo * set the Session ID to a unique value 12251ae08745Sheppo * (the lower 32 bits of the clock tick) 12261ae08745Sheppo */ 12271ae08745Sheppo vdc->session_id = ((uint32_t)gettick() & 0xffffffff); 12283af08d82Slm66018 DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id); 12291ae08745Sheppo 12301ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 12311ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 12321ae08745Sheppo pkt.tag.vio_subtype_env = VIO_VER_INFO; 12331ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 12341ae08745Sheppo pkt.dev_class = VDEV_DISK; 12350a55fbb7Slm66018 pkt.ver_major = ver.major; 12360a55fbb7Slm66018 pkt.ver_minor = ver.minor; 12371ae08745Sheppo 12380a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 12393af08d82Slm66018 DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n", 12403af08d82Slm66018 vdc->instance, status); 12411ae08745Sheppo if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) { 12423af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: " 1243e1ebb9ecSlm66018 "id(%lx) rv(%d) size(%ld)", 1244e1ebb9ecSlm66018 vdc->instance, vdc->ldc_handle, 12451ae08745Sheppo status, msglen); 12461ae08745Sheppo if (msglen != sizeof (vio_ver_msg_t)) 12471ae08745Sheppo status = ENOMSG; 12481ae08745Sheppo } 12491ae08745Sheppo 12501ae08745Sheppo return (status); 12511ae08745Sheppo } 12521ae08745Sheppo 12530a55fbb7Slm66018 /* 12540a55fbb7Slm66018 * Function: 12553af08d82Slm66018 * vdc_ver_negotiation() 12563af08d82Slm66018 * 12573af08d82Slm66018 * Description: 12583af08d82Slm66018 * 12593af08d82Slm66018 * Arguments: 12603af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 12613af08d82Slm66018 * 12623af08d82Slm66018 * Return Code: 12633af08d82Slm66018 * 0 - Success 12643af08d82Slm66018 */ 12653af08d82Slm66018 static int 12663af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp) 12673af08d82Slm66018 { 12683af08d82Slm66018 vio_msg_t vio_msg; 12693af08d82Slm66018 int status; 12703af08d82Slm66018 12713af08d82Slm66018 if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0])) 12723af08d82Slm66018 return (status); 12733af08d82Slm66018 12743af08d82Slm66018 /* release lock and wait for response */ 12753af08d82Slm66018 mutex_exit(&vdcp->lock); 12763af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 12773af08d82Slm66018 mutex_enter(&vdcp->lock); 12783af08d82Slm66018 if (status) { 12793af08d82Slm66018 DMSG(vdcp, 0, 12803af08d82Slm66018 "[%d] Failed waiting for Ver negotiation response, rv(%d)", 12813af08d82Slm66018 vdcp->instance, status); 12823af08d82Slm66018 return (status); 12833af08d82Slm66018 } 12843af08d82Slm66018 12853af08d82Slm66018 /* check type and sub_type ... */ 12863af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 12873af08d82Slm66018 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 12883af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n", 12893af08d82Slm66018 vdcp->instance); 12903af08d82Slm66018 return (EPROTO); 12913af08d82Slm66018 } 12923af08d82Slm66018 12933af08d82Slm66018 return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg)); 12943af08d82Slm66018 } 12953af08d82Slm66018 12963af08d82Slm66018 /* 12973af08d82Slm66018 * Function: 12980a55fbb7Slm66018 * vdc_init_attr_negotiation() 12990a55fbb7Slm66018 * 13000a55fbb7Slm66018 * Description: 13010a55fbb7Slm66018 * 13020a55fbb7Slm66018 * Arguments: 13030a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 13040a55fbb7Slm66018 * 13050a55fbb7Slm66018 * Return Code: 13060a55fbb7Slm66018 * 0 - Success 13070a55fbb7Slm66018 */ 13081ae08745Sheppo static int 13091ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc) 13101ae08745Sheppo { 13111ae08745Sheppo vd_attr_msg_t pkt; 13121ae08745Sheppo size_t msglen = sizeof (pkt); 13131ae08745Sheppo int status; 13141ae08745Sheppo 13151ae08745Sheppo ASSERT(vdc != NULL); 13161ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 13171ae08745Sheppo 13183af08d82Slm66018 DMSG(vdc, 0, "[%d] entered\n", vdc->instance); 13191ae08745Sheppo 13201ae08745Sheppo /* fill in tag */ 13211ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 13221ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 13231ae08745Sheppo pkt.tag.vio_subtype_env = VIO_ATTR_INFO; 13241ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 13251ae08745Sheppo /* fill in payload */ 13261ae08745Sheppo pkt.max_xfer_sz = vdc->max_xfer_sz; 13271ae08745Sheppo pkt.vdisk_block_size = vdc->block_size; 13281ae08745Sheppo pkt.xfer_mode = VIO_DRING_MODE; 13291ae08745Sheppo pkt.operations = 0; /* server will set bits of valid operations */ 13301ae08745Sheppo pkt.vdisk_type = 0; /* server will set to valid device type */ 13311ae08745Sheppo pkt.vdisk_size = 0; /* server will set to valid size */ 13321ae08745Sheppo 13330a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 13343af08d82Slm66018 DMSG(vdc, 0, "Attr info sent (status = %d)\n", status); 13351ae08745Sheppo 13361ae08745Sheppo if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) { 13373af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: " 1338e1ebb9ecSlm66018 "id(%lx) rv(%d) size(%ld)", 1339e1ebb9ecSlm66018 vdc->instance, vdc->ldc_handle, 13401ae08745Sheppo status, msglen); 13411ae08745Sheppo if (msglen != sizeof (vio_ver_msg_t)) 13421ae08745Sheppo status = ENOMSG; 13431ae08745Sheppo } 13441ae08745Sheppo 13451ae08745Sheppo return (status); 13461ae08745Sheppo } 13471ae08745Sheppo 13480a55fbb7Slm66018 /* 13490a55fbb7Slm66018 * Function: 13503af08d82Slm66018 * vdc_attr_negotiation() 13513af08d82Slm66018 * 13523af08d82Slm66018 * Description: 13533af08d82Slm66018 * 13543af08d82Slm66018 * Arguments: 13553af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 13563af08d82Slm66018 * 13573af08d82Slm66018 * Return Code: 13583af08d82Slm66018 * 0 - Success 13593af08d82Slm66018 */ 13603af08d82Slm66018 static int 13613af08d82Slm66018 vdc_attr_negotiation(vdc_t *vdcp) 13623af08d82Slm66018 { 13633af08d82Slm66018 int status; 13643af08d82Slm66018 vio_msg_t vio_msg; 13653af08d82Slm66018 13663af08d82Slm66018 if (status = vdc_init_attr_negotiation(vdcp)) 13673af08d82Slm66018 return (status); 13683af08d82Slm66018 13693af08d82Slm66018 /* release lock and wait for response */ 13703af08d82Slm66018 mutex_exit(&vdcp->lock); 13713af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 13723af08d82Slm66018 mutex_enter(&vdcp->lock); 13733af08d82Slm66018 if (status) { 13743af08d82Slm66018 DMSG(vdcp, 0, 13753af08d82Slm66018 "[%d] Failed waiting for Attr negotiation response, rv(%d)", 13763af08d82Slm66018 vdcp->instance, status); 13773af08d82Slm66018 return (status); 13783af08d82Slm66018 } 13793af08d82Slm66018 13803af08d82Slm66018 /* check type and sub_type ... */ 13813af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 13823af08d82Slm66018 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 13833af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n", 13843af08d82Slm66018 vdcp->instance); 13853af08d82Slm66018 return (EPROTO); 13863af08d82Slm66018 } 13873af08d82Slm66018 13883af08d82Slm66018 return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg)); 13893af08d82Slm66018 } 13903af08d82Slm66018 13913af08d82Slm66018 13923af08d82Slm66018 /* 13933af08d82Slm66018 * Function: 13940a55fbb7Slm66018 * vdc_init_dring_negotiate() 13950a55fbb7Slm66018 * 13960a55fbb7Slm66018 * Description: 13970a55fbb7Slm66018 * 13980a55fbb7Slm66018 * Arguments: 13990a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 14000a55fbb7Slm66018 * 14010a55fbb7Slm66018 * Return Code: 14020a55fbb7Slm66018 * 0 - Success 14030a55fbb7Slm66018 */ 14041ae08745Sheppo static int 14051ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc) 14061ae08745Sheppo { 14071ae08745Sheppo vio_dring_reg_msg_t pkt; 14081ae08745Sheppo size_t msglen = sizeof (pkt); 14091ae08745Sheppo int status = -1; 14103af08d82Slm66018 int retry; 14113af08d82Slm66018 int nretries = 10; 14121ae08745Sheppo 14131ae08745Sheppo ASSERT(vdc != NULL); 14141ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 14151ae08745Sheppo 14163af08d82Slm66018 for (retry = 0; retry < nretries; retry++) { 14171ae08745Sheppo status = vdc_init_descriptor_ring(vdc); 14183af08d82Slm66018 if (status != EAGAIN) 14193af08d82Slm66018 break; 14203af08d82Slm66018 drv_usecwait(vdc_min_timeout_ldc); 14213af08d82Slm66018 } 14223af08d82Slm66018 14231ae08745Sheppo if (status != 0) { 14243af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n", 14251ae08745Sheppo vdc->instance, status); 14261ae08745Sheppo return (status); 14271ae08745Sheppo } 14283af08d82Slm66018 14293af08d82Slm66018 DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n", 1430e1ebb9ecSlm66018 vdc->instance, status); 14311ae08745Sheppo 14321ae08745Sheppo /* fill in tag */ 14331ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 14341ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 14351ae08745Sheppo pkt.tag.vio_subtype_env = VIO_DRING_REG; 14361ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 14371ae08745Sheppo /* fill in payload */ 14381ae08745Sheppo pkt.dring_ident = 0; 1439e1ebb9ecSlm66018 pkt.num_descriptors = vdc->dring_len; 1440e1ebb9ecSlm66018 pkt.descriptor_size = vdc->dring_entry_size; 14411ae08745Sheppo pkt.options = (VIO_TX_DRING | VIO_RX_DRING); 14421ae08745Sheppo pkt.ncookies = vdc->dring_cookie_count; 14431ae08745Sheppo pkt.cookie[0] = vdc->dring_cookie[0]; /* for now just one cookie */ 14441ae08745Sheppo 14450a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 14461ae08745Sheppo if (status != 0) { 14473af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)", 1448e1ebb9ecSlm66018 vdc->instance, status); 14491ae08745Sheppo } 14501ae08745Sheppo 14511ae08745Sheppo return (status); 14521ae08745Sheppo } 14531ae08745Sheppo 14541ae08745Sheppo 14553af08d82Slm66018 /* 14563af08d82Slm66018 * Function: 14573af08d82Slm66018 * vdc_dring_negotiation() 14583af08d82Slm66018 * 14593af08d82Slm66018 * Description: 14603af08d82Slm66018 * 14613af08d82Slm66018 * Arguments: 14623af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 14633af08d82Slm66018 * 14643af08d82Slm66018 * Return Code: 14653af08d82Slm66018 * 0 - Success 14663af08d82Slm66018 */ 14673af08d82Slm66018 static int 14683af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp) 14693af08d82Slm66018 { 14703af08d82Slm66018 int status; 14713af08d82Slm66018 vio_msg_t vio_msg; 14723af08d82Slm66018 14733af08d82Slm66018 if (status = vdc_init_dring_negotiate(vdcp)) 14743af08d82Slm66018 return (status); 14753af08d82Slm66018 14763af08d82Slm66018 /* release lock and wait for response */ 14773af08d82Slm66018 mutex_exit(&vdcp->lock); 14783af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 14793af08d82Slm66018 mutex_enter(&vdcp->lock); 14803af08d82Slm66018 if (status) { 14813af08d82Slm66018 DMSG(vdcp, 0, 14823af08d82Slm66018 "[%d] Failed waiting for Dring negotiation response," 14833af08d82Slm66018 " rv(%d)", vdcp->instance, status); 14843af08d82Slm66018 return (status); 14853af08d82Slm66018 } 14863af08d82Slm66018 14873af08d82Slm66018 /* check type and sub_type ... */ 14883af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 14893af08d82Slm66018 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 14903af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n", 14913af08d82Slm66018 vdcp->instance); 14923af08d82Slm66018 return (EPROTO); 14933af08d82Slm66018 } 14943af08d82Slm66018 14953af08d82Slm66018 return (vdc_handle_dring_reg_msg(vdcp, 14963af08d82Slm66018 (vio_dring_reg_msg_t *)&vio_msg)); 14973af08d82Slm66018 } 14983af08d82Slm66018 14993af08d82Slm66018 15003af08d82Slm66018 /* 15013af08d82Slm66018 * Function: 15023af08d82Slm66018 * vdc_send_rdx() 15033af08d82Slm66018 * 15043af08d82Slm66018 * Description: 15053af08d82Slm66018 * 15063af08d82Slm66018 * Arguments: 15073af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 15083af08d82Slm66018 * 15093af08d82Slm66018 * Return Code: 15103af08d82Slm66018 * 0 - Success 15113af08d82Slm66018 */ 15123af08d82Slm66018 static int 15133af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp) 15143af08d82Slm66018 { 15153af08d82Slm66018 vio_msg_t msg; 15163af08d82Slm66018 size_t msglen = sizeof (vio_msg_t); 15173af08d82Slm66018 int status; 15183af08d82Slm66018 15193af08d82Slm66018 /* 15203af08d82Slm66018 * Send an RDX message to vds to indicate we are ready 15213af08d82Slm66018 * to send data 15223af08d82Slm66018 */ 15233af08d82Slm66018 msg.tag.vio_msgtype = VIO_TYPE_CTRL; 15243af08d82Slm66018 msg.tag.vio_subtype = VIO_SUBTYPE_INFO; 15253af08d82Slm66018 msg.tag.vio_subtype_env = VIO_RDX; 15263af08d82Slm66018 msg.tag.vio_sid = vdcp->session_id; 15273af08d82Slm66018 status = vdc_send(vdcp, (caddr_t)&msg, &msglen); 15283af08d82Slm66018 if (status != 0) { 15293af08d82Slm66018 DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)", 15303af08d82Slm66018 vdcp->instance, status); 15313af08d82Slm66018 } 15323af08d82Slm66018 15333af08d82Slm66018 return (status); 15343af08d82Slm66018 } 15353af08d82Slm66018 15363af08d82Slm66018 /* 15373af08d82Slm66018 * Function: 15383af08d82Slm66018 * vdc_handle_rdx() 15393af08d82Slm66018 * 15403af08d82Slm66018 * Description: 15413af08d82Slm66018 * 15423af08d82Slm66018 * Arguments: 15433af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 15443af08d82Slm66018 * msgp - received msg 15453af08d82Slm66018 * 15463af08d82Slm66018 * Return Code: 15473af08d82Slm66018 * 0 - Success 15483af08d82Slm66018 */ 15493af08d82Slm66018 static int 15503af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp) 15513af08d82Slm66018 { 15523af08d82Slm66018 _NOTE(ARGUNUSED(vdcp)) 15533af08d82Slm66018 _NOTE(ARGUNUSED(msgp)) 15543af08d82Slm66018 15553af08d82Slm66018 ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL); 15563af08d82Slm66018 ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK); 15573af08d82Slm66018 ASSERT(msgp->tag.vio_subtype_env == VIO_RDX); 15583af08d82Slm66018 15593af08d82Slm66018 DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance); 15603af08d82Slm66018 15613af08d82Slm66018 return (0); 15623af08d82Slm66018 } 15633af08d82Slm66018 15643af08d82Slm66018 /* 15653af08d82Slm66018 * Function: 15663af08d82Slm66018 * vdc_rdx_exchange() 15673af08d82Slm66018 * 15683af08d82Slm66018 * Description: 15693af08d82Slm66018 * 15703af08d82Slm66018 * Arguments: 15713af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 15723af08d82Slm66018 * 15733af08d82Slm66018 * Return Code: 15743af08d82Slm66018 * 0 - Success 15753af08d82Slm66018 */ 15763af08d82Slm66018 static int 15773af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp) 15783af08d82Slm66018 { 15793af08d82Slm66018 int status; 15803af08d82Slm66018 vio_msg_t vio_msg; 15813af08d82Slm66018 15823af08d82Slm66018 if (status = vdc_send_rdx(vdcp)) 15833af08d82Slm66018 return (status); 15843af08d82Slm66018 15853af08d82Slm66018 /* release lock and wait for response */ 15863af08d82Slm66018 mutex_exit(&vdcp->lock); 15873af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 15883af08d82Slm66018 mutex_enter(&vdcp->lock); 15893af08d82Slm66018 if (status) { 15903af08d82Slm66018 DMSG(vdcp, 0, 15913af08d82Slm66018 "[%d] Failed waiting for RDX response," 15923af08d82Slm66018 " rv(%d)", vdcp->instance, status); 15933af08d82Slm66018 return (status); 15943af08d82Slm66018 } 15953af08d82Slm66018 15963af08d82Slm66018 /* check type and sub_type ... */ 15973af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 15983af08d82Slm66018 vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) { 15993af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid RDX response\n", 16003af08d82Slm66018 vdcp->instance); 16013af08d82Slm66018 return (EPROTO); 16023af08d82Slm66018 } 16033af08d82Slm66018 16043af08d82Slm66018 return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg)); 16053af08d82Slm66018 } 16063af08d82Slm66018 16073af08d82Slm66018 16081ae08745Sheppo /* -------------------------------------------------------------------------- */ 16091ae08745Sheppo 16101ae08745Sheppo /* 16111ae08745Sheppo * LDC helper routines 16121ae08745Sheppo */ 16131ae08745Sheppo 16143af08d82Slm66018 static int 16153af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp) 16163af08d82Slm66018 { 16173af08d82Slm66018 int status; 16183af08d82Slm66018 boolean_t q_has_pkts = B_FALSE; 16193af08d82Slm66018 int delay_time; 16203af08d82Slm66018 size_t len; 16213af08d82Slm66018 16223af08d82Slm66018 mutex_enter(&vdc->read_lock); 16233af08d82Slm66018 16243af08d82Slm66018 if (vdc->read_state == VDC_READ_IDLE) 16253af08d82Slm66018 vdc->read_state = VDC_READ_WAITING; 16263af08d82Slm66018 16273af08d82Slm66018 while (vdc->read_state != VDC_READ_PENDING) { 16283af08d82Slm66018 16293af08d82Slm66018 /* detect if the connection has been reset */ 16303af08d82Slm66018 if (vdc->read_state == VDC_READ_RESET) { 16313af08d82Slm66018 status = ECONNRESET; 16323af08d82Slm66018 goto done; 16333af08d82Slm66018 } 16343af08d82Slm66018 16353af08d82Slm66018 cv_wait(&vdc->read_cv, &vdc->read_lock); 16363af08d82Slm66018 } 16373af08d82Slm66018 16383af08d82Slm66018 /* 16393af08d82Slm66018 * Until we get a blocking ldc read we have to retry 16403af08d82Slm66018 * until the entire LDC message has arrived before 16413af08d82Slm66018 * ldc_read() will succeed. Note we also bail out if 16423af08d82Slm66018 * the chanel is reset or goes away. 16433af08d82Slm66018 */ 16443af08d82Slm66018 delay_time = vdc_ldc_read_init_delay; 16453af08d82Slm66018 loop: 16463af08d82Slm66018 len = *nbytesp; 16473af08d82Slm66018 status = ldc_read(vdc->ldc_handle, (caddr_t)msgp, &len); 16483af08d82Slm66018 switch (status) { 16493af08d82Slm66018 case EAGAIN: 16503af08d82Slm66018 delay_time *= 2; 16513af08d82Slm66018 if (delay_time >= vdc_ldc_read_max_delay) 16523af08d82Slm66018 delay_time = vdc_ldc_read_max_delay; 16533af08d82Slm66018 delay(delay_time); 16543af08d82Slm66018 goto loop; 16553af08d82Slm66018 16563af08d82Slm66018 case 0: 16573af08d82Slm66018 if (len == 0) { 16583af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_read returned 0 bytes with " 16593af08d82Slm66018 "no error!\n", vdc->instance); 16603af08d82Slm66018 goto loop; 16613af08d82Slm66018 } 16623af08d82Slm66018 16633af08d82Slm66018 *nbytesp = len; 16643af08d82Slm66018 16653af08d82Slm66018 /* 16663af08d82Slm66018 * If there are pending messages, leave the 16673af08d82Slm66018 * read state as pending. Otherwise, set the state 16683af08d82Slm66018 * back to idle. 16693af08d82Slm66018 */ 16703af08d82Slm66018 status = ldc_chkq(vdc->ldc_handle, &q_has_pkts); 16713af08d82Slm66018 if (status == 0 && !q_has_pkts) 16723af08d82Slm66018 vdc->read_state = VDC_READ_IDLE; 16733af08d82Slm66018 16743af08d82Slm66018 break; 16753af08d82Slm66018 default: 16763af08d82Slm66018 DMSG(vdc, 0, "ldc_read returned %d\n", status); 16773af08d82Slm66018 break; 16783af08d82Slm66018 } 16793af08d82Slm66018 16803af08d82Slm66018 done: 16813af08d82Slm66018 mutex_exit(&vdc->read_lock); 16823af08d82Slm66018 16833af08d82Slm66018 return (status); 16843af08d82Slm66018 } 16853af08d82Slm66018 16863af08d82Slm66018 16873af08d82Slm66018 16883af08d82Slm66018 #ifdef DEBUG 16893af08d82Slm66018 void 16903af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg) 16913af08d82Slm66018 { 16923af08d82Slm66018 char *ms, *ss, *ses; 16933af08d82Slm66018 switch (msg->tag.vio_msgtype) { 16943af08d82Slm66018 #define Q(_s) case _s : ms = #_s; break; 16953af08d82Slm66018 Q(VIO_TYPE_CTRL) 16963af08d82Slm66018 Q(VIO_TYPE_DATA) 16973af08d82Slm66018 Q(VIO_TYPE_ERR) 16983af08d82Slm66018 #undef Q 16993af08d82Slm66018 default: ms = "unknown"; break; 17003af08d82Slm66018 } 17013af08d82Slm66018 17023af08d82Slm66018 switch (msg->tag.vio_subtype) { 17033af08d82Slm66018 #define Q(_s) case _s : ss = #_s; break; 17043af08d82Slm66018 Q(VIO_SUBTYPE_INFO) 17053af08d82Slm66018 Q(VIO_SUBTYPE_ACK) 17063af08d82Slm66018 Q(VIO_SUBTYPE_NACK) 17073af08d82Slm66018 #undef Q 17083af08d82Slm66018 default: ss = "unknown"; break; 17093af08d82Slm66018 } 17103af08d82Slm66018 17113af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 17123af08d82Slm66018 #define Q(_s) case _s : ses = #_s; break; 17133af08d82Slm66018 Q(VIO_VER_INFO) 17143af08d82Slm66018 Q(VIO_ATTR_INFO) 17153af08d82Slm66018 Q(VIO_DRING_REG) 17163af08d82Slm66018 Q(VIO_DRING_UNREG) 17173af08d82Slm66018 Q(VIO_RDX) 17183af08d82Slm66018 Q(VIO_PKT_DATA) 17193af08d82Slm66018 Q(VIO_DESC_DATA) 17203af08d82Slm66018 Q(VIO_DRING_DATA) 17213af08d82Slm66018 #undef Q 17223af08d82Slm66018 default: ses = "unknown"; break; 17233af08d82Slm66018 } 17243af08d82Slm66018 17253af08d82Slm66018 DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n", 17263af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 17273af08d82Slm66018 msg->tag.vio_subtype_env, ms, ss, ses); 17283af08d82Slm66018 } 17293af08d82Slm66018 #endif 17303af08d82Slm66018 17311ae08745Sheppo /* 17321ae08745Sheppo * Function: 17331ae08745Sheppo * vdc_send() 17341ae08745Sheppo * 17351ae08745Sheppo * Description: 17361ae08745Sheppo * The function encapsulates the call to write a message using LDC. 17371ae08745Sheppo * If LDC indicates that the call failed due to the queue being full, 17381ae08745Sheppo * we retry the ldc_write() [ up to 'vdc_retries' time ], otherwise 17391ae08745Sheppo * we return the error returned by LDC. 17401ae08745Sheppo * 17411ae08745Sheppo * Arguments: 17421ae08745Sheppo * ldc_handle - LDC handle for the channel this instance of vdc uses 17431ae08745Sheppo * pkt - address of LDC message to be sent 17441ae08745Sheppo * msglen - the size of the message being sent. When the function 17451ae08745Sheppo * returns, this contains the number of bytes written. 17461ae08745Sheppo * 17471ae08745Sheppo * Return Code: 17481ae08745Sheppo * 0 - Success. 17491ae08745Sheppo * EINVAL - pkt or msglen were NULL 17501ae08745Sheppo * ECONNRESET - The connection was not up. 17511ae08745Sheppo * EWOULDBLOCK - LDC queue is full 17521ae08745Sheppo * xxx - other error codes returned by ldc_write 17531ae08745Sheppo */ 17541ae08745Sheppo static int 17550a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen) 17561ae08745Sheppo { 17571ae08745Sheppo size_t size = 0; 17581ae08745Sheppo int status = 0; 17593af08d82Slm66018 clock_t delay_ticks; 17601ae08745Sheppo 17610a55fbb7Slm66018 ASSERT(vdc != NULL); 17620a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 17631ae08745Sheppo ASSERT(msglen != NULL); 17641ae08745Sheppo ASSERT(*msglen != 0); 17651ae08745Sheppo 17663af08d82Slm66018 #ifdef DEBUG 17673af08d82Slm66018 vdc_decode_tag(vdc, (vio_msg_t *)pkt); 17683af08d82Slm66018 #endif 17693af08d82Slm66018 /* 17703af08d82Slm66018 * Wait indefinitely to send if channel 17713af08d82Slm66018 * is busy, but bail out if we succeed or 17723af08d82Slm66018 * if the channel closes or is reset. 17733af08d82Slm66018 */ 17743af08d82Slm66018 delay_ticks = vdc_hz_min_ldc_delay; 17751ae08745Sheppo do { 17761ae08745Sheppo size = *msglen; 17770a55fbb7Slm66018 status = ldc_write(vdc->ldc_handle, pkt, &size); 17783af08d82Slm66018 if (status == EWOULDBLOCK) { 17793af08d82Slm66018 delay(delay_ticks); 17803af08d82Slm66018 /* geometric backoff */ 17813af08d82Slm66018 delay_ticks *= 2; 17823af08d82Slm66018 if (delay_ticks > vdc_hz_max_ldc_delay) 17833af08d82Slm66018 delay_ticks = vdc_hz_max_ldc_delay; 17843af08d82Slm66018 } 17853af08d82Slm66018 } while (status == EWOULDBLOCK); 17861ae08745Sheppo 17870a55fbb7Slm66018 /* if LDC had serious issues --- reset vdc state */ 17880a55fbb7Slm66018 if (status == EIO || status == ECONNRESET) { 17893af08d82Slm66018 /* LDC had serious issues --- reset vdc state */ 17903af08d82Slm66018 mutex_enter(&vdc->read_lock); 17913af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 17923af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) 17933af08d82Slm66018 cv_signal(&vdc->read_cv); 17943af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 17953af08d82Slm66018 mutex_exit(&vdc->read_lock); 17963af08d82Slm66018 17973af08d82Slm66018 /* wake up any waiters in the reset thread */ 17983af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 17993af08d82Slm66018 DMSG(vdc, 0, "[%d] write reset - " 18003af08d82Slm66018 "vdc is resetting ..\n", vdc->instance); 18013af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 18023af08d82Slm66018 cv_signal(&vdc->initwait_cv); 18033af08d82Slm66018 } 18043af08d82Slm66018 18053af08d82Slm66018 return (ECONNRESET); 18060a55fbb7Slm66018 } 18070a55fbb7Slm66018 18081ae08745Sheppo /* return the last size written */ 18091ae08745Sheppo *msglen = size; 18101ae08745Sheppo 18111ae08745Sheppo return (status); 18121ae08745Sheppo } 18131ae08745Sheppo 18141ae08745Sheppo /* 18151ae08745Sheppo * Function: 18161ae08745Sheppo * vdc_get_ldc_id() 18171ae08745Sheppo * 18181ae08745Sheppo * Description: 18191ae08745Sheppo * This function gets the 'ldc-id' for this particular instance of vdc. 18201ae08745Sheppo * The id returned is the guest domain channel endpoint LDC uses for 18211ae08745Sheppo * communication with vds. 18221ae08745Sheppo * 18231ae08745Sheppo * Arguments: 18241ae08745Sheppo * dip - dev info pointer for this instance of the device driver. 18251ae08745Sheppo * ldc_id - pointer to variable used to return the 'ldc-id' found. 18261ae08745Sheppo * 18271ae08745Sheppo * Return Code: 18281ae08745Sheppo * 0 - Success. 18291ae08745Sheppo * ENOENT - Expected node or property did not exist. 18301ae08745Sheppo * ENXIO - Unexpected error communicating with MD framework 18311ae08745Sheppo */ 18321ae08745Sheppo static int 18331ae08745Sheppo vdc_get_ldc_id(dev_info_t *dip, uint64_t *ldc_id) 18341ae08745Sheppo { 18351ae08745Sheppo int status = ENOENT; 18361ae08745Sheppo char *node_name = NULL; 18371ae08745Sheppo md_t *mdp = NULL; 18381ae08745Sheppo int num_nodes; 18391ae08745Sheppo int num_vdevs; 18401ae08745Sheppo int num_chans; 18411ae08745Sheppo mde_cookie_t rootnode; 18421ae08745Sheppo mde_cookie_t *listp = NULL; 18431ae08745Sheppo mde_cookie_t *chanp = NULL; 18441ae08745Sheppo boolean_t found_inst = B_FALSE; 18451ae08745Sheppo int listsz; 18461ae08745Sheppo int idx; 18471ae08745Sheppo uint64_t md_inst; 18481ae08745Sheppo int obp_inst; 18491ae08745Sheppo int instance = ddi_get_instance(dip); 18501ae08745Sheppo 18511ae08745Sheppo ASSERT(ldc_id != NULL); 18521ae08745Sheppo *ldc_id = 0; 18531ae08745Sheppo 18541ae08745Sheppo /* 18551ae08745Sheppo * Get the OBP instance number for comparison with the MD instance 18561ae08745Sheppo * 18571ae08745Sheppo * The "cfg-handle" property of a vdc node in an MD contains the MD's 18581ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 18591ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 18601ae08745Sheppo * the "reg" property on the node in the device tree it builds from 18611ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 18621ae08745Sheppo * "reg" property value to uniquely identify this device instance. 18631ae08745Sheppo * If the "reg" property cannot be found, the device tree state is 18641ae08745Sheppo * presumably so broken that there is no point in continuing. 18651ae08745Sheppo */ 18661ae08745Sheppo if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) { 18671ae08745Sheppo cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG); 18681ae08745Sheppo return (ENOENT); 18691ae08745Sheppo } 18701ae08745Sheppo obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 18711ae08745Sheppo OBP_REG, -1); 18723af08d82Slm66018 DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst); 18731ae08745Sheppo 18741ae08745Sheppo /* 18751ae08745Sheppo * We now walk the MD nodes and if an instance of a vdc node matches 18761ae08745Sheppo * the instance got from OBP we get the ldc-id property. 18771ae08745Sheppo */ 18781ae08745Sheppo if ((mdp = md_get_handle()) == NULL) { 18791ae08745Sheppo cmn_err(CE_WARN, "unable to init machine description"); 18801ae08745Sheppo return (ENXIO); 18811ae08745Sheppo } 18821ae08745Sheppo 18831ae08745Sheppo num_nodes = md_node_count(mdp); 18841ae08745Sheppo ASSERT(num_nodes > 0); 18851ae08745Sheppo 18861ae08745Sheppo listsz = num_nodes * sizeof (mde_cookie_t); 18871ae08745Sheppo 18881ae08745Sheppo /* allocate memory for nodes */ 18891ae08745Sheppo listp = kmem_zalloc(listsz, KM_SLEEP); 18901ae08745Sheppo chanp = kmem_zalloc(listsz, KM_SLEEP); 18911ae08745Sheppo 18921ae08745Sheppo rootnode = md_root_node(mdp); 18931ae08745Sheppo ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE); 18941ae08745Sheppo 18951ae08745Sheppo /* 18961ae08745Sheppo * Search for all the virtual devices, we will then check to see which 18971ae08745Sheppo * ones are disk nodes. 18981ae08745Sheppo */ 18991ae08745Sheppo num_vdevs = md_scan_dag(mdp, rootnode, 19001ae08745Sheppo md_find_name(mdp, VDC_MD_VDEV_NAME), 19011ae08745Sheppo md_find_name(mdp, "fwd"), listp); 19021ae08745Sheppo 19031ae08745Sheppo if (num_vdevs <= 0) { 19041ae08745Sheppo cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME); 19051ae08745Sheppo status = ENOENT; 19061ae08745Sheppo goto done; 19071ae08745Sheppo } 19081ae08745Sheppo 19093af08d82Slm66018 DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs); 19101ae08745Sheppo for (idx = 0; idx < num_vdevs; idx++) { 19111ae08745Sheppo status = md_get_prop_str(mdp, listp[idx], "name", &node_name); 19121ae08745Sheppo if ((status != 0) || (node_name == NULL)) { 19131ae08745Sheppo cmn_err(CE_NOTE, "Unable to get name of node type '%s'" 19141ae08745Sheppo ": err %d", VDC_MD_VDEV_NAME, status); 19151ae08745Sheppo continue; 19161ae08745Sheppo } 19171ae08745Sheppo 19183af08d82Slm66018 DMSGX(1, "[%d] Found node '%s'\n", instance, node_name); 19191ae08745Sheppo if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) { 19201ae08745Sheppo status = md_get_prop_val(mdp, listp[idx], 19211ae08745Sheppo VDC_MD_CFG_HDL, &md_inst); 19223af08d82Slm66018 DMSGX(1, "[%d] vdc inst in MD=%lx\n", 19233af08d82Slm66018 instance, md_inst); 19241ae08745Sheppo if ((status == 0) && (md_inst == obp_inst)) { 19251ae08745Sheppo found_inst = B_TRUE; 19261ae08745Sheppo break; 19271ae08745Sheppo } 19281ae08745Sheppo } 19291ae08745Sheppo } 19301ae08745Sheppo 19310a55fbb7Slm66018 if (!found_inst) { 19323af08d82Slm66018 DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME); 19331ae08745Sheppo status = ENOENT; 19341ae08745Sheppo goto done; 19351ae08745Sheppo } 19363af08d82Slm66018 DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst); 19371ae08745Sheppo 19381ae08745Sheppo /* get the channels for this node */ 19391ae08745Sheppo num_chans = md_scan_dag(mdp, listp[idx], 19401ae08745Sheppo md_find_name(mdp, VDC_MD_CHAN_NAME), 19411ae08745Sheppo md_find_name(mdp, "fwd"), chanp); 19421ae08745Sheppo 19431ae08745Sheppo /* expecting at least one channel */ 19441ae08745Sheppo if (num_chans <= 0) { 19451ae08745Sheppo cmn_err(CE_NOTE, "No '%s' node for '%s' port", 19461ae08745Sheppo VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME); 19471ae08745Sheppo status = ENOENT; 19481ae08745Sheppo goto done; 19491ae08745Sheppo 19501ae08745Sheppo } else if (num_chans != 1) { 19513af08d82Slm66018 DMSGX(0, "[%d] Expected 1 '%s' node for '%s' port, found %d\n", 1952e1ebb9ecSlm66018 instance, VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME, 19531ae08745Sheppo num_chans); 19541ae08745Sheppo } 19551ae08745Sheppo 19561ae08745Sheppo /* 19571ae08745Sheppo * We use the first channel found (index 0), irrespective of how 19581ae08745Sheppo * many are there in total. 19591ae08745Sheppo */ 19601ae08745Sheppo if (md_get_prop_val(mdp, chanp[0], VDC_ID_PROP, ldc_id) != 0) { 19611ae08745Sheppo cmn_err(CE_NOTE, "Channel '%s' property not found", 19621ae08745Sheppo VDC_ID_PROP); 19631ae08745Sheppo status = ENOENT; 19641ae08745Sheppo } 19651ae08745Sheppo 19663af08d82Slm66018 DMSGX(0, "[%d] LDC id is 0x%lx\n", instance, *ldc_id); 19671ae08745Sheppo 19681ae08745Sheppo done: 19691ae08745Sheppo if (chanp) 19701ae08745Sheppo kmem_free(chanp, listsz); 19711ae08745Sheppo if (listp) 19721ae08745Sheppo kmem_free(listp, listsz); 19731ae08745Sheppo 19741ae08745Sheppo (void) md_fini_handle(mdp); 19751ae08745Sheppo 19761ae08745Sheppo return (status); 19771ae08745Sheppo } 19781ae08745Sheppo 19790a55fbb7Slm66018 static int 19800a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc) 19810a55fbb7Slm66018 { 19820a55fbb7Slm66018 int status; 19833af08d82Slm66018 ldc_status_t ldc_state; 19840a55fbb7Slm66018 19853af08d82Slm66018 DMSG(vdc, 0, "[%d] Bringing up channel %lx\n", 19863af08d82Slm66018 vdc->instance, vdc->ldc_id); 19873af08d82Slm66018 19883af08d82Slm66018 if (vdc->lifecycle == VDC_LC_DETACHING) 19893af08d82Slm66018 return (EINVAL); 19900a55fbb7Slm66018 19910a55fbb7Slm66018 if ((status = ldc_up(vdc->ldc_handle)) != 0) { 19920a55fbb7Slm66018 switch (status) { 19930a55fbb7Slm66018 case ECONNREFUSED: /* listener not ready at other end */ 19943af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n", 1995e1ebb9ecSlm66018 vdc->instance, vdc->ldc_id, status); 19960a55fbb7Slm66018 status = 0; 19970a55fbb7Slm66018 break; 19980a55fbb7Slm66018 default: 19993af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to bring up LDC: " 20003af08d82Slm66018 "channel=%ld, err=%d", vdc->instance, vdc->ldc_id, 20013af08d82Slm66018 status); 20023af08d82Slm66018 break; 20033af08d82Slm66018 } 20043af08d82Slm66018 } 20053af08d82Slm66018 20063af08d82Slm66018 if (ldc_status(vdc->ldc_handle, &ldc_state) == 0) { 20073af08d82Slm66018 vdc->ldc_state = ldc_state; 20083af08d82Slm66018 if (ldc_state == LDC_UP) { 20093af08d82Slm66018 DMSG(vdc, 0, "[%d] LDC channel already up\n", 20103af08d82Slm66018 vdc->instance); 20113af08d82Slm66018 vdc->seq_num = 1; 20123af08d82Slm66018 vdc->seq_num_reply = 0; 20130a55fbb7Slm66018 } 20140a55fbb7Slm66018 } 20150a55fbb7Slm66018 20160a55fbb7Slm66018 return (status); 20170a55fbb7Slm66018 } 20180a55fbb7Slm66018 20190a55fbb7Slm66018 /* 20200a55fbb7Slm66018 * Function: 20210a55fbb7Slm66018 * vdc_terminate_ldc() 20220a55fbb7Slm66018 * 20230a55fbb7Slm66018 * Description: 20240a55fbb7Slm66018 * 20250a55fbb7Slm66018 * Arguments: 20260a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 20270a55fbb7Slm66018 * 20280a55fbb7Slm66018 * Return Code: 20290a55fbb7Slm66018 * None 20300a55fbb7Slm66018 */ 20311ae08745Sheppo static void 20321ae08745Sheppo vdc_terminate_ldc(vdc_t *vdc) 20331ae08745Sheppo { 20341ae08745Sheppo int instance = ddi_get_instance(vdc->dip); 20351ae08745Sheppo 20361ae08745Sheppo ASSERT(vdc != NULL); 20371ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 20381ae08745Sheppo 20393af08d82Slm66018 DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized); 20401ae08745Sheppo 20411ae08745Sheppo if (vdc->initialized & VDC_LDC_OPEN) { 20423af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_close()\n", instance); 20431ae08745Sheppo (void) ldc_close(vdc->ldc_handle); 20441ae08745Sheppo } 20451ae08745Sheppo if (vdc->initialized & VDC_LDC_CB) { 20463af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance); 20471ae08745Sheppo (void) ldc_unreg_callback(vdc->ldc_handle); 20481ae08745Sheppo } 20491ae08745Sheppo if (vdc->initialized & VDC_LDC) { 20503af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_fini()\n", instance); 20511ae08745Sheppo (void) ldc_fini(vdc->ldc_handle); 20521ae08745Sheppo vdc->ldc_handle = NULL; 20531ae08745Sheppo } 20541ae08745Sheppo 20551ae08745Sheppo vdc->initialized &= ~(VDC_LDC | VDC_LDC_CB | VDC_LDC_OPEN); 20561ae08745Sheppo } 20571ae08745Sheppo 20581ae08745Sheppo /* -------------------------------------------------------------------------- */ 20591ae08745Sheppo 20601ae08745Sheppo /* 20611ae08745Sheppo * Descriptor Ring helper routines 20621ae08745Sheppo */ 20631ae08745Sheppo 20640a55fbb7Slm66018 /* 20650a55fbb7Slm66018 * Function: 20660a55fbb7Slm66018 * vdc_init_descriptor_ring() 20670a55fbb7Slm66018 * 20680a55fbb7Slm66018 * Description: 20690a55fbb7Slm66018 * 20700a55fbb7Slm66018 * Arguments: 20710a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 20720a55fbb7Slm66018 * 20730a55fbb7Slm66018 * Return Code: 20740a55fbb7Slm66018 * 0 - Success 20750a55fbb7Slm66018 */ 20761ae08745Sheppo static int 20771ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc) 20781ae08745Sheppo { 20791ae08745Sheppo vd_dring_entry_t *dep = NULL; /* DRing Entry pointer */ 20800a55fbb7Slm66018 int status = 0; 20811ae08745Sheppo int i; 20821ae08745Sheppo 20833af08d82Slm66018 DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized); 20841ae08745Sheppo 20851ae08745Sheppo ASSERT(vdc != NULL); 20861ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 20871ae08745Sheppo ASSERT(vdc->ldc_handle != NULL); 20881ae08745Sheppo 2089e1ebb9ecSlm66018 /* ensure we have enough room to store max sized block */ 2090e1ebb9ecSlm66018 ASSERT(maxphys <= VD_MAX_BLOCK_SIZE); 2091e1ebb9ecSlm66018 20920a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_INIT) == 0) { 20933af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance); 2094e1ebb9ecSlm66018 /* 2095e1ebb9ecSlm66018 * Calculate the maximum block size we can transmit using one 2096e1ebb9ecSlm66018 * Descriptor Ring entry from the attributes returned by the 2097e1ebb9ecSlm66018 * vDisk server. This is subject to a minimum of 'maxphys' 2098e1ebb9ecSlm66018 * as we do not have the capability to split requests over 2099e1ebb9ecSlm66018 * multiple DRing entries. 2100e1ebb9ecSlm66018 */ 2101e1ebb9ecSlm66018 if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) { 21023af08d82Slm66018 DMSG(vdc, 0, "[%d] using minimum DRing size\n", 2103e1ebb9ecSlm66018 vdc->instance); 2104e1ebb9ecSlm66018 vdc->dring_max_cookies = maxphys / PAGESIZE; 2105e1ebb9ecSlm66018 } else { 2106e1ebb9ecSlm66018 vdc->dring_max_cookies = 2107e1ebb9ecSlm66018 (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE; 2108e1ebb9ecSlm66018 } 2109e1ebb9ecSlm66018 vdc->dring_entry_size = (sizeof (vd_dring_entry_t) + 2110e1ebb9ecSlm66018 (sizeof (ldc_mem_cookie_t) * 2111e1ebb9ecSlm66018 (vdc->dring_max_cookies - 1))); 2112e1ebb9ecSlm66018 vdc->dring_len = VD_DRING_LEN; 2113e1ebb9ecSlm66018 2114e1ebb9ecSlm66018 status = ldc_mem_dring_create(vdc->dring_len, 2115e1ebb9ecSlm66018 vdc->dring_entry_size, &vdc->ldc_dring_hdl); 21161ae08745Sheppo if ((vdc->ldc_dring_hdl == NULL) || (status != 0)) { 21173af08d82Slm66018 DMSG(vdc, 0, "[%d] Descriptor ring creation failed", 2118e1ebb9ecSlm66018 vdc->instance); 21191ae08745Sheppo return (status); 21201ae08745Sheppo } 21210a55fbb7Slm66018 vdc->initialized |= VDC_DRING_INIT; 21220a55fbb7Slm66018 } 21231ae08745Sheppo 21240a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_BOUND) == 0) { 21253af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance); 21260a55fbb7Slm66018 vdc->dring_cookie = 21270a55fbb7Slm66018 kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP); 21281ae08745Sheppo 21291ae08745Sheppo status = ldc_mem_dring_bind(vdc->ldc_handle, vdc->ldc_dring_hdl, 21304bac2208Snarayan LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW, 21310a55fbb7Slm66018 &vdc->dring_cookie[0], 21321ae08745Sheppo &vdc->dring_cookie_count); 21331ae08745Sheppo if (status != 0) { 21343af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to bind descriptor ring " 21353af08d82Slm66018 "(%lx) to channel (%lx) status=%d\n", 21363af08d82Slm66018 vdc->instance, vdc->ldc_dring_hdl, 21373af08d82Slm66018 vdc->ldc_handle, status); 21381ae08745Sheppo return (status); 21391ae08745Sheppo } 21401ae08745Sheppo ASSERT(vdc->dring_cookie_count == 1); 21411ae08745Sheppo vdc->initialized |= VDC_DRING_BOUND; 21420a55fbb7Slm66018 } 21431ae08745Sheppo 21441ae08745Sheppo status = ldc_mem_dring_info(vdc->ldc_dring_hdl, &vdc->dring_mem_info); 21451ae08745Sheppo if (status != 0) { 21463af08d82Slm66018 DMSG(vdc, 0, 21473af08d82Slm66018 "[%d] Failed to get info for descriptor ring (%lx)\n", 2148e1ebb9ecSlm66018 vdc->instance, vdc->ldc_dring_hdl); 21491ae08745Sheppo return (status); 21501ae08745Sheppo } 21511ae08745Sheppo 21520a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_LOCAL) == 0) { 21533af08d82Slm66018 DMSG(vdc, 0, "[%d] local dring\n", vdc->instance); 21540a55fbb7Slm66018 21551ae08745Sheppo /* Allocate the local copy of this dring */ 21560a55fbb7Slm66018 vdc->local_dring = 2157e1ebb9ecSlm66018 kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t), 21581ae08745Sheppo KM_SLEEP); 21591ae08745Sheppo vdc->initialized |= VDC_DRING_LOCAL; 21600a55fbb7Slm66018 } 21611ae08745Sheppo 21621ae08745Sheppo /* 21630a55fbb7Slm66018 * Mark all DRing entries as free and initialize the private 21640a55fbb7Slm66018 * descriptor's memory handles. If any entry is initialized, 21650a55fbb7Slm66018 * we need to free it later so we set the bit in 'initialized' 21660a55fbb7Slm66018 * at the start. 21671ae08745Sheppo */ 21681ae08745Sheppo vdc->initialized |= VDC_DRING_ENTRY; 2169e1ebb9ecSlm66018 for (i = 0; i < vdc->dring_len; i++) { 21701ae08745Sheppo dep = VDC_GET_DRING_ENTRY_PTR(vdc, i); 21711ae08745Sheppo dep->hdr.dstate = VIO_DESC_FREE; 21721ae08745Sheppo 21731ae08745Sheppo status = ldc_mem_alloc_handle(vdc->ldc_handle, 21741ae08745Sheppo &vdc->local_dring[i].desc_mhdl); 21751ae08745Sheppo if (status != 0) { 21763af08d82Slm66018 DMSG(vdc, 0, "![%d] Failed to alloc mem handle for" 21771ae08745Sheppo " descriptor %d", vdc->instance, i); 21781ae08745Sheppo return (status); 21791ae08745Sheppo } 21803af08d82Slm66018 vdc->local_dring[i].is_free = B_TRUE; 21811ae08745Sheppo vdc->local_dring[i].dep = dep; 21821ae08745Sheppo } 21831ae08745Sheppo 21843af08d82Slm66018 /* Initialize the starting index */ 21853af08d82Slm66018 vdc->dring_curr_idx = 0; 21861ae08745Sheppo 21871ae08745Sheppo return (status); 21881ae08745Sheppo } 21891ae08745Sheppo 21900a55fbb7Slm66018 /* 21910a55fbb7Slm66018 * Function: 21920a55fbb7Slm66018 * vdc_destroy_descriptor_ring() 21930a55fbb7Slm66018 * 21940a55fbb7Slm66018 * Description: 21950a55fbb7Slm66018 * 21960a55fbb7Slm66018 * Arguments: 21970a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 21980a55fbb7Slm66018 * 21990a55fbb7Slm66018 * Return Code: 22000a55fbb7Slm66018 * None 22010a55fbb7Slm66018 */ 22021ae08745Sheppo static void 22031ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc) 22041ae08745Sheppo { 22050a55fbb7Slm66018 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 22061ae08745Sheppo ldc_mem_handle_t mhdl = NULL; 22073af08d82Slm66018 ldc_mem_info_t minfo; 22081ae08745Sheppo int status = -1; 22091ae08745Sheppo int i; /* loop */ 22101ae08745Sheppo 22111ae08745Sheppo ASSERT(vdc != NULL); 22121ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 22131ae08745Sheppo 22143af08d82Slm66018 DMSG(vdc, 0, "[%d] Entered\n", vdc->instance); 22151ae08745Sheppo 22161ae08745Sheppo if (vdc->initialized & VDC_DRING_ENTRY) { 22173af08d82Slm66018 DMSG(vdc, 0, 22183af08d82Slm66018 "[%d] Removing Local DRing entries\n", vdc->instance); 2219e1ebb9ecSlm66018 for (i = 0; i < vdc->dring_len; i++) { 22200a55fbb7Slm66018 ldep = &vdc->local_dring[i]; 22210a55fbb7Slm66018 mhdl = ldep->desc_mhdl; 22221ae08745Sheppo 22230a55fbb7Slm66018 if (mhdl == NULL) 22240a55fbb7Slm66018 continue; 22250a55fbb7Slm66018 22263af08d82Slm66018 if ((status = ldc_mem_info(mhdl, &minfo)) != 0) { 22273af08d82Slm66018 DMSG(vdc, 0, 22283af08d82Slm66018 "ldc_mem_info returned an error: %d\n", 22293af08d82Slm66018 status); 22303af08d82Slm66018 22313af08d82Slm66018 /* 22323af08d82Slm66018 * This must mean that the mem handle 22333af08d82Slm66018 * is not valid. Clear it out so that 22343af08d82Slm66018 * no one tries to use it. 22353af08d82Slm66018 */ 22363af08d82Slm66018 ldep->desc_mhdl = NULL; 22373af08d82Slm66018 continue; 22383af08d82Slm66018 } 22393af08d82Slm66018 22403af08d82Slm66018 if (minfo.status == LDC_BOUND) { 22413af08d82Slm66018 (void) ldc_mem_unbind_handle(mhdl); 22423af08d82Slm66018 } 22433af08d82Slm66018 22441ae08745Sheppo (void) ldc_mem_free_handle(mhdl); 22453af08d82Slm66018 22463af08d82Slm66018 ldep->desc_mhdl = NULL; 22471ae08745Sheppo } 22481ae08745Sheppo vdc->initialized &= ~VDC_DRING_ENTRY; 22491ae08745Sheppo } 22501ae08745Sheppo 22511ae08745Sheppo if (vdc->initialized & VDC_DRING_LOCAL) { 22523af08d82Slm66018 DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance); 22531ae08745Sheppo kmem_free(vdc->local_dring, 2254e1ebb9ecSlm66018 vdc->dring_len * sizeof (vdc_local_desc_t)); 22551ae08745Sheppo vdc->initialized &= ~VDC_DRING_LOCAL; 22561ae08745Sheppo } 22571ae08745Sheppo 22581ae08745Sheppo if (vdc->initialized & VDC_DRING_BOUND) { 22593af08d82Slm66018 DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance); 22601ae08745Sheppo status = ldc_mem_dring_unbind(vdc->ldc_dring_hdl); 22611ae08745Sheppo if (status == 0) { 22621ae08745Sheppo vdc->initialized &= ~VDC_DRING_BOUND; 22631ae08745Sheppo } else { 22643af08d82Slm66018 DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx", 2265e1ebb9ecSlm66018 vdc->instance, status, vdc->ldc_dring_hdl); 22661ae08745Sheppo } 22673af08d82Slm66018 kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t)); 22681ae08745Sheppo } 22691ae08745Sheppo 22701ae08745Sheppo if (vdc->initialized & VDC_DRING_INIT) { 22713af08d82Slm66018 DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance); 22721ae08745Sheppo status = ldc_mem_dring_destroy(vdc->ldc_dring_hdl); 22731ae08745Sheppo if (status == 0) { 22741ae08745Sheppo vdc->ldc_dring_hdl = NULL; 22751ae08745Sheppo bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t)); 22761ae08745Sheppo vdc->initialized &= ~VDC_DRING_INIT; 22771ae08745Sheppo } else { 22783af08d82Slm66018 DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)", 2279e1ebb9ecSlm66018 vdc->instance, status, vdc->ldc_dring_hdl); 22801ae08745Sheppo } 22811ae08745Sheppo } 22821ae08745Sheppo } 22831ae08745Sheppo 22841ae08745Sheppo /* 22853af08d82Slm66018 * Function: 22863af08d82Slm66018 * vdc_map_to_shared_ring() 22871ae08745Sheppo * 22881ae08745Sheppo * Description: 22893af08d82Slm66018 * Copy contents of the local descriptor to the shared 22903af08d82Slm66018 * memory descriptor. 22911ae08745Sheppo * 22923af08d82Slm66018 * Arguments: 22933af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 22943af08d82Slm66018 * idx - descriptor ring index 22953af08d82Slm66018 * 22963af08d82Slm66018 * Return Code: 22973af08d82Slm66018 * None 22981ae08745Sheppo */ 22991ae08745Sheppo static int 23003af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx) 23011ae08745Sheppo { 23023af08d82Slm66018 vdc_local_desc_t *ldep; 23033af08d82Slm66018 vd_dring_entry_t *dep; 23043af08d82Slm66018 int rv; 23051ae08745Sheppo 23063af08d82Slm66018 ldep = &(vdcp->local_dring[idx]); 23071ae08745Sheppo 23083af08d82Slm66018 /* for now leave in the old pop_mem_hdl stuff */ 23093af08d82Slm66018 if (ldep->nbytes > 0) { 23103af08d82Slm66018 rv = vdc_populate_mem_hdl(vdcp, ldep); 23113af08d82Slm66018 if (rv) { 23123af08d82Slm66018 DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n", 23133af08d82Slm66018 vdcp->instance); 23143af08d82Slm66018 return (rv); 23153af08d82Slm66018 } 23163af08d82Slm66018 } 23171ae08745Sheppo 23183af08d82Slm66018 /* 23193af08d82Slm66018 * fill in the data details into the DRing 23203af08d82Slm66018 */ 2321d10e4ef2Snarayan dep = ldep->dep; 23221ae08745Sheppo ASSERT(dep != NULL); 23231ae08745Sheppo 23243af08d82Slm66018 dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp); 23253af08d82Slm66018 dep->payload.operation = ldep->operation; 23263af08d82Slm66018 dep->payload.addr = ldep->offset; 23273af08d82Slm66018 dep->payload.nbytes = ldep->nbytes; 2328055d7c80Scarlsonj dep->payload.status = (uint32_t)-1; /* vds will set valid value */ 23293af08d82Slm66018 dep->payload.slice = ldep->slice; 23303af08d82Slm66018 dep->hdr.dstate = VIO_DESC_READY; 23313af08d82Slm66018 dep->hdr.ack = 1; /* request an ACK for every message */ 23321ae08745Sheppo 23333af08d82Slm66018 return (0); 23341ae08745Sheppo } 23351ae08745Sheppo 23361ae08745Sheppo /* 23371ae08745Sheppo * Function: 23383af08d82Slm66018 * vdc_send_request 23393af08d82Slm66018 * 23403af08d82Slm66018 * Description: 23413af08d82Slm66018 * This routine writes the data to be transmitted to vds into the 23423af08d82Slm66018 * descriptor, notifies vds that the ring has been updated and 23433af08d82Slm66018 * then waits for the request to be processed. 23443af08d82Slm66018 * 23453af08d82Slm66018 * Arguments: 23463af08d82Slm66018 * vdcp - the soft state pointer 23473af08d82Slm66018 * operation - operation we want vds to perform (VD_OP_XXX) 23483af08d82Slm66018 * addr - address of data buf to be read/written. 23493af08d82Slm66018 * nbytes - number of bytes to read/write 23503af08d82Slm66018 * slice - the disk slice this request is for 23513af08d82Slm66018 * offset - relative disk offset 23523af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 23533af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 23543af08d82Slm66018 * . mode for ioctl(9e) 23553af08d82Slm66018 * . LP64 diskaddr_t (block I/O) 23563af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 23573af08d82Slm66018 * 23583af08d82Slm66018 * Return Codes: 23593af08d82Slm66018 * 0 23603af08d82Slm66018 * EAGAIN 23613af08d82Slm66018 * EFAULT 23623af08d82Slm66018 * ENXIO 23633af08d82Slm66018 * EIO 23643af08d82Slm66018 */ 23653af08d82Slm66018 static int 23663af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr, 23673af08d82Slm66018 size_t nbytes, int slice, diskaddr_t offset, int cb_type, 23683af08d82Slm66018 void *cb_arg, vio_desc_direction_t dir) 23693af08d82Slm66018 { 23703af08d82Slm66018 ASSERT(vdcp != NULL); 23713af08d82Slm66018 ASSERT(slice < V_NUMPAR); 23723af08d82Slm66018 23733af08d82Slm66018 mutex_enter(&vdcp->lock); 23743af08d82Slm66018 23753af08d82Slm66018 do { 23763af08d82Slm66018 while (vdcp->state != VDC_STATE_RUNNING) 23773af08d82Slm66018 cv_wait(&vdcp->running_cv, &vdcp->lock); 23783af08d82Slm66018 23793af08d82Slm66018 } while (vdc_populate_descriptor(vdcp, operation, addr, 23803af08d82Slm66018 nbytes, slice, offset, cb_type, cb_arg, dir)); 23813af08d82Slm66018 23823af08d82Slm66018 mutex_exit(&vdcp->lock); 23833af08d82Slm66018 return (0); 23843af08d82Slm66018 } 23853af08d82Slm66018 23863af08d82Slm66018 23873af08d82Slm66018 /* 23883af08d82Slm66018 * Function: 23891ae08745Sheppo * vdc_populate_descriptor 23901ae08745Sheppo * 23911ae08745Sheppo * Description: 23921ae08745Sheppo * This routine writes the data to be transmitted to vds into the 23931ae08745Sheppo * descriptor, notifies vds that the ring has been updated and 23941ae08745Sheppo * then waits for the request to be processed. 23951ae08745Sheppo * 23961ae08745Sheppo * Arguments: 23973af08d82Slm66018 * vdcp - the soft state pointer 23981ae08745Sheppo * operation - operation we want vds to perform (VD_OP_XXX) 23993af08d82Slm66018 * addr - address of data buf to be read/written. 24003af08d82Slm66018 * nbytes - number of bytes to read/write 24013af08d82Slm66018 * slice - the disk slice this request is for 24023af08d82Slm66018 * offset - relative disk offset 24033af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 24043af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 24051ae08745Sheppo * . mode for ioctl(9e) 24061ae08745Sheppo * . LP64 diskaddr_t (block I/O) 24073af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 24081ae08745Sheppo * 24091ae08745Sheppo * Return Codes: 24101ae08745Sheppo * 0 24111ae08745Sheppo * EAGAIN 24121ae08745Sheppo * EFAULT 24131ae08745Sheppo * ENXIO 24141ae08745Sheppo * EIO 24151ae08745Sheppo */ 24161ae08745Sheppo static int 24173af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr, 24183af08d82Slm66018 size_t nbytes, int slice, diskaddr_t offset, int cb_type, 24193af08d82Slm66018 void *cb_arg, vio_desc_direction_t dir) 24201ae08745Sheppo { 24213af08d82Slm66018 vdc_local_desc_t *local_dep = NULL; /* Local Dring Pointer */ 24223af08d82Slm66018 int idx; /* Index of DRing entry used */ 24233af08d82Slm66018 int next_idx; 24241ae08745Sheppo vio_dring_msg_t dmsg; 24253af08d82Slm66018 size_t msglen; 24268e6a2a04Slm66018 int rv; 24271ae08745Sheppo 24283af08d82Slm66018 ASSERT(MUTEX_HELD(&vdcp->lock)); 24293af08d82Slm66018 vdcp->threads_pending++; 24303af08d82Slm66018 loop: 24313af08d82Slm66018 DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx); 24321ae08745Sheppo 24333af08d82Slm66018 /* Get next available D-Ring entry */ 24343af08d82Slm66018 idx = vdcp->dring_curr_idx; 24353af08d82Slm66018 local_dep = &(vdcp->local_dring[idx]); 24361ae08745Sheppo 24373af08d82Slm66018 if (!local_dep->is_free) { 24383af08d82Slm66018 DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n", 24393af08d82Slm66018 vdcp->instance); 24403af08d82Slm66018 cv_wait(&vdcp->dring_free_cv, &vdcp->lock); 24413af08d82Slm66018 if (vdcp->state == VDC_STATE_RUNNING || 24423af08d82Slm66018 vdcp->state == VDC_STATE_HANDLE_PENDING) { 24433af08d82Slm66018 goto loop; 24443af08d82Slm66018 } 24453af08d82Slm66018 vdcp->threads_pending--; 24463af08d82Slm66018 return (ECONNRESET); 24471ae08745Sheppo } 24481ae08745Sheppo 24493af08d82Slm66018 next_idx = idx + 1; 24503af08d82Slm66018 if (next_idx >= vdcp->dring_len) 24513af08d82Slm66018 next_idx = 0; 24523af08d82Slm66018 vdcp->dring_curr_idx = next_idx; 24531ae08745Sheppo 24543af08d82Slm66018 ASSERT(local_dep->is_free); 24551ae08745Sheppo 24563af08d82Slm66018 local_dep->operation = operation; 2457d10e4ef2Snarayan local_dep->addr = addr; 24583af08d82Slm66018 local_dep->nbytes = nbytes; 24593af08d82Slm66018 local_dep->slice = slice; 24603af08d82Slm66018 local_dep->offset = offset; 24613af08d82Slm66018 local_dep->cb_type = cb_type; 24623af08d82Slm66018 local_dep->cb_arg = cb_arg; 24633af08d82Slm66018 local_dep->dir = dir; 24643af08d82Slm66018 24653af08d82Slm66018 local_dep->is_free = B_FALSE; 24663af08d82Slm66018 24673af08d82Slm66018 rv = vdc_map_to_shared_dring(vdcp, idx); 24683af08d82Slm66018 if (rv) { 24693af08d82Slm66018 DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n", 24703af08d82Slm66018 vdcp->instance); 24713af08d82Slm66018 /* free the descriptor */ 24723af08d82Slm66018 local_dep->is_free = B_TRUE; 24733af08d82Slm66018 vdcp->dring_curr_idx = idx; 24743af08d82Slm66018 cv_wait(&vdcp->membind_cv, &vdcp->lock); 24753af08d82Slm66018 if (vdcp->state == VDC_STATE_RUNNING || 24763af08d82Slm66018 vdcp->state == VDC_STATE_HANDLE_PENDING) { 24773af08d82Slm66018 goto loop; 24781ae08745Sheppo } 24793af08d82Slm66018 vdcp->threads_pending--; 24803af08d82Slm66018 return (ECONNRESET); 24811ae08745Sheppo } 24821ae08745Sheppo 24831ae08745Sheppo /* 24841ae08745Sheppo * Send a msg with the DRing details to vds 24851ae08745Sheppo */ 24861ae08745Sheppo VIO_INIT_DRING_DATA_TAG(dmsg); 24873af08d82Slm66018 VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp); 24883af08d82Slm66018 dmsg.dring_ident = vdcp->dring_ident; 24891ae08745Sheppo dmsg.start_idx = idx; 24901ae08745Sheppo dmsg.end_idx = idx; 24913af08d82Slm66018 vdcp->seq_num++; 24921ae08745Sheppo 24933af08d82Slm66018 DTRACE_IO2(send, vio_dring_msg_t *, &dmsg, vdc_t *, vdcp); 2494d10e4ef2Snarayan 24953af08d82Slm66018 DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n", 24963af08d82Slm66018 vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num); 24971ae08745Sheppo 24983af08d82Slm66018 /* 24993af08d82Slm66018 * note we're still holding the lock here to 25003af08d82Slm66018 * make sure the message goes out in order !!!... 25013af08d82Slm66018 */ 25023af08d82Slm66018 msglen = sizeof (dmsg); 25033af08d82Slm66018 rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen); 25043af08d82Slm66018 switch (rv) { 25053af08d82Slm66018 case ECONNRESET: 25063af08d82Slm66018 /* 25073af08d82Slm66018 * vdc_send initiates the reset on failure. 25083af08d82Slm66018 * Since the transaction has already been put 25093af08d82Slm66018 * on the local dring, it will automatically get 25103af08d82Slm66018 * retried when the channel is reset. Given that, 25113af08d82Slm66018 * it is ok to just return success even though the 25123af08d82Slm66018 * send failed. 25133af08d82Slm66018 */ 25143af08d82Slm66018 rv = 0; 25153af08d82Slm66018 break; 2516d10e4ef2Snarayan 25173af08d82Slm66018 case 0: /* EOK */ 25183af08d82Slm66018 DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv); 25193af08d82Slm66018 break; 2520d10e4ef2Snarayan 25213af08d82Slm66018 default: 25223af08d82Slm66018 goto cleanup_and_exit; 25233af08d82Slm66018 } 2524e1ebb9ecSlm66018 25253af08d82Slm66018 vdcp->threads_pending--; 25263af08d82Slm66018 return (rv); 25273af08d82Slm66018 25283af08d82Slm66018 cleanup_and_exit: 25293af08d82Slm66018 DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv); 25303af08d82Slm66018 return (ENXIO); 25311ae08745Sheppo } 25321ae08745Sheppo 25331ae08745Sheppo /* 25343af08d82Slm66018 * Function: 25353af08d82Slm66018 * vdc_do_sync_op 25363af08d82Slm66018 * 25373af08d82Slm66018 * Description: 25383af08d82Slm66018 * Wrapper around vdc_populate_descriptor that blocks until the 25393af08d82Slm66018 * response to the message is available. 25403af08d82Slm66018 * 25413af08d82Slm66018 * Arguments: 25423af08d82Slm66018 * vdcp - the soft state pointer 25433af08d82Slm66018 * operation - operation we want vds to perform (VD_OP_XXX) 25443af08d82Slm66018 * addr - address of data buf to be read/written. 25453af08d82Slm66018 * nbytes - number of bytes to read/write 25463af08d82Slm66018 * slice - the disk slice this request is for 25473af08d82Slm66018 * offset - relative disk offset 25483af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 25493af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 25503af08d82Slm66018 * . mode for ioctl(9e) 25513af08d82Slm66018 * . LP64 diskaddr_t (block I/O) 25523af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 25533af08d82Slm66018 * 25543af08d82Slm66018 * Return Codes: 25553af08d82Slm66018 * 0 25563af08d82Slm66018 * EAGAIN 25573af08d82Slm66018 * EFAULT 25583af08d82Slm66018 * ENXIO 25593af08d82Slm66018 * EIO 25600a55fbb7Slm66018 */ 25613af08d82Slm66018 static int 25623af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes, 25633af08d82Slm66018 int slice, diskaddr_t offset, int cb_type, void *cb_arg, 25643af08d82Slm66018 vio_desc_direction_t dir) 25653af08d82Slm66018 { 25663af08d82Slm66018 int status; 25673af08d82Slm66018 25683af08d82Slm66018 ASSERT(cb_type == CB_SYNC); 25691ae08745Sheppo 25701ae08745Sheppo /* 25713af08d82Slm66018 * Grab the lock, if blocked wait until the server 25723af08d82Slm66018 * response causes us to wake up again. 25733af08d82Slm66018 */ 25743af08d82Slm66018 mutex_enter(&vdcp->lock); 25753af08d82Slm66018 vdcp->sync_op_cnt++; 25763af08d82Slm66018 while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH) 25773af08d82Slm66018 cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock); 25783af08d82Slm66018 25793af08d82Slm66018 if (vdcp->state == VDC_STATE_DETACH) { 25803af08d82Slm66018 cv_broadcast(&vdcp->sync_blocked_cv); 25813af08d82Slm66018 vdcp->sync_op_cnt--; 25823af08d82Slm66018 mutex_exit(&vdcp->lock); 25833af08d82Slm66018 return (ENXIO); 25843af08d82Slm66018 } 25853af08d82Slm66018 25863af08d82Slm66018 /* now block anyone other thread entering after us */ 25873af08d82Slm66018 vdcp->sync_op_blocked = B_TRUE; 25883af08d82Slm66018 vdcp->sync_op_pending = B_TRUE; 25893af08d82Slm66018 mutex_exit(&vdcp->lock); 25903af08d82Slm66018 25913af08d82Slm66018 /* 25923af08d82Slm66018 * No need to check return value - will return error only 25933af08d82Slm66018 * in the DETACH case and we can fall through 25943af08d82Slm66018 */ 25953af08d82Slm66018 (void) vdc_send_request(vdcp, operation, addr, 25963af08d82Slm66018 nbytes, slice, offset, cb_type, cb_arg, dir); 25973af08d82Slm66018 25983af08d82Slm66018 /* 25993af08d82Slm66018 * block until our transaction completes. 26003af08d82Slm66018 * Also anyone else waiting also gets to go next. 26013af08d82Slm66018 */ 26023af08d82Slm66018 mutex_enter(&vdcp->lock); 26033af08d82Slm66018 while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH) 26043af08d82Slm66018 cv_wait(&vdcp->sync_pending_cv, &vdcp->lock); 26053af08d82Slm66018 26063af08d82Slm66018 DMSG(vdcp, 2, ": operation returned %d\n", vdcp->sync_op_status); 26073af08d82Slm66018 if (vdcp->state == VDC_STATE_DETACH) 26083af08d82Slm66018 status = ENXIO; 26093af08d82Slm66018 else 26103af08d82Slm66018 status = vdcp->sync_op_status; 26113af08d82Slm66018 vdcp->sync_op_status = 0; 26123af08d82Slm66018 vdcp->sync_op_blocked = B_FALSE; 26133af08d82Slm66018 vdcp->sync_op_cnt--; 26143af08d82Slm66018 26153af08d82Slm66018 /* signal the next waiting thread */ 26163af08d82Slm66018 cv_signal(&vdcp->sync_blocked_cv); 26173af08d82Slm66018 mutex_exit(&vdcp->lock); 26183af08d82Slm66018 26193af08d82Slm66018 return (status); 26203af08d82Slm66018 } 26213af08d82Slm66018 26223af08d82Slm66018 26233af08d82Slm66018 /* 26243af08d82Slm66018 * Function: 26253af08d82Slm66018 * vdc_drain_response() 26263af08d82Slm66018 * 26273af08d82Slm66018 * Description: 26281ae08745Sheppo * When a guest is panicking, the completion of requests needs to be 26291ae08745Sheppo * handled differently because interrupts are disabled and vdc 26301ae08745Sheppo * will not get messages. We have to poll for the messages instead. 26313af08d82Slm66018 * 26323af08d82Slm66018 * Arguments: 26333af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 26343af08d82Slm66018 * 26353af08d82Slm66018 * Return Code: 26363af08d82Slm66018 * 0 - Success 26371ae08745Sheppo */ 26383af08d82Slm66018 static int 26393af08d82Slm66018 vdc_drain_response(vdc_t *vdc) 26403af08d82Slm66018 { 26413af08d82Slm66018 int rv, idx, retries; 26423af08d82Slm66018 size_t msglen; 26433af08d82Slm66018 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 26443af08d82Slm66018 vio_dring_msg_t dmsg; 26453af08d82Slm66018 26463af08d82Slm66018 mutex_enter(&vdc->lock); 26473af08d82Slm66018 26481ae08745Sheppo retries = 0; 26491ae08745Sheppo for (;;) { 26501ae08745Sheppo msglen = sizeof (dmsg); 26513af08d82Slm66018 rv = ldc_read(vdc->ldc_handle, (caddr_t)&dmsg, &msglen); 26528e6a2a04Slm66018 if (rv) { 26538e6a2a04Slm66018 rv = EINVAL; 26541ae08745Sheppo break; 26551ae08745Sheppo } 26561ae08745Sheppo 26571ae08745Sheppo /* 26581ae08745Sheppo * if there are no packets wait and check again 26591ae08745Sheppo */ 26608e6a2a04Slm66018 if ((rv == 0) && (msglen == 0)) { 26611ae08745Sheppo if (retries++ > vdc_dump_retries) { 26628e6a2a04Slm66018 rv = EAGAIN; 26631ae08745Sheppo break; 26641ae08745Sheppo } 26651ae08745Sheppo 2666d10e4ef2Snarayan drv_usecwait(vdc_usec_timeout_dump); 26671ae08745Sheppo continue; 26681ae08745Sheppo } 26691ae08745Sheppo 26701ae08745Sheppo /* 26711ae08745Sheppo * Ignore all messages that are not ACKs/NACKs to 26721ae08745Sheppo * DRing requests. 26731ae08745Sheppo */ 26741ae08745Sheppo if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) || 26751ae08745Sheppo (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) { 26763af08d82Slm66018 DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n", 26771ae08745Sheppo dmsg.tag.vio_msgtype, 26781ae08745Sheppo dmsg.tag.vio_subtype, 26791ae08745Sheppo dmsg.tag.vio_subtype_env); 26801ae08745Sheppo continue; 26811ae08745Sheppo } 26821ae08745Sheppo 26831ae08745Sheppo /* 26843af08d82Slm66018 * set the appropriate return value for the current request. 26851ae08745Sheppo */ 26861ae08745Sheppo switch (dmsg.tag.vio_subtype) { 26871ae08745Sheppo case VIO_SUBTYPE_ACK: 26888e6a2a04Slm66018 rv = 0; 26891ae08745Sheppo break; 26901ae08745Sheppo case VIO_SUBTYPE_NACK: 26918e6a2a04Slm66018 rv = EAGAIN; 26921ae08745Sheppo break; 26931ae08745Sheppo default: 26941ae08745Sheppo continue; 26951ae08745Sheppo } 26961ae08745Sheppo 26973af08d82Slm66018 idx = dmsg.start_idx; 26983af08d82Slm66018 if (idx >= vdc->dring_len) { 26993af08d82Slm66018 DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n", 2700e1ebb9ecSlm66018 vdc->instance, idx); 27013af08d82Slm66018 continue; 27021ae08745Sheppo } 27033af08d82Slm66018 ldep = &vdc->local_dring[idx]; 27043af08d82Slm66018 if (ldep->dep->hdr.dstate != VIO_DESC_DONE) { 27053af08d82Slm66018 DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n", 27063af08d82Slm66018 vdc->instance, idx, ldep->dep->hdr.dstate); 27071ae08745Sheppo continue; 27081ae08745Sheppo } 27091ae08745Sheppo 27103af08d82Slm66018 DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n", 27113af08d82Slm66018 vdc->instance, idx, ldep->dep->hdr.dstate); 27123af08d82Slm66018 rv = vdc_depopulate_descriptor(vdc, idx); 27133af08d82Slm66018 if (rv) { 27143af08d82Slm66018 DMSG(vdc, 0, 27153af08d82Slm66018 "[%d] Entry @ %d - depopulate failed ..\n", 27163af08d82Slm66018 vdc->instance, idx); 27171ae08745Sheppo } 27181ae08745Sheppo 27193af08d82Slm66018 /* if this is the last descriptor - break out of loop */ 27203af08d82Slm66018 if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx) 27213af08d82Slm66018 break; 27223af08d82Slm66018 } 27233af08d82Slm66018 27243af08d82Slm66018 mutex_exit(&vdc->lock); 27253af08d82Slm66018 DMSG(vdc, 0, "End idx=%d\n", idx); 27263af08d82Slm66018 27273af08d82Slm66018 return (rv); 27281ae08745Sheppo } 27291ae08745Sheppo 27301ae08745Sheppo 27310a55fbb7Slm66018 /* 27320a55fbb7Slm66018 * Function: 27330a55fbb7Slm66018 * vdc_depopulate_descriptor() 27340a55fbb7Slm66018 * 27350a55fbb7Slm66018 * Description: 27360a55fbb7Slm66018 * 27370a55fbb7Slm66018 * Arguments: 27380a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 27390a55fbb7Slm66018 * idx - Index of the Descriptor Ring entry being modified 27400a55fbb7Slm66018 * 27410a55fbb7Slm66018 * Return Code: 27420a55fbb7Slm66018 * 0 - Success 27430a55fbb7Slm66018 */ 27441ae08745Sheppo static int 27451ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx) 27461ae08745Sheppo { 27471ae08745Sheppo vd_dring_entry_t *dep = NULL; /* Dring Entry Pointer */ 27481ae08745Sheppo vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 27491ae08745Sheppo int status = ENXIO; 27508e6a2a04Slm66018 int operation; 27518e6a2a04Slm66018 int rv = 0; 27521ae08745Sheppo 27531ae08745Sheppo ASSERT(vdc != NULL); 2754e1ebb9ecSlm66018 ASSERT(idx < vdc->dring_len); 27551ae08745Sheppo ldep = &vdc->local_dring[idx]; 27561ae08745Sheppo ASSERT(ldep != NULL); 27573af08d82Slm66018 ASSERT(MUTEX_HELD(&vdc->lock)); 27583af08d82Slm66018 27593af08d82Slm66018 DMSG(vdc, 2, ": idx = %d\n", idx); 27601ae08745Sheppo dep = ldep->dep; 27611ae08745Sheppo ASSERT(dep != NULL); 2762e1ebb9ecSlm66018 ASSERT((dep->hdr.dstate == VIO_DESC_DONE) || 2763e1ebb9ecSlm66018 (dep->payload.status == ECANCELED)); 27641ae08745Sheppo 2765e1ebb9ecSlm66018 VDC_MARK_DRING_ENTRY_FREE(vdc, idx); 27663af08d82Slm66018 27673af08d82Slm66018 ldep->is_free = B_TRUE; 27683af08d82Slm66018 DMSG(vdc, 2, ": is_free = %d\n", ldep->is_free); 27691ae08745Sheppo status = dep->payload.status; 27708e6a2a04Slm66018 operation = dep->payload.operation; 27711ae08745Sheppo 27724bac2208Snarayan /* the DKIO FLUSH operation never bind handles so we can return now */ 27734bac2208Snarayan if (operation == VD_OP_FLUSH) 27748e6a2a04Slm66018 return (status); 27758e6a2a04Slm66018 27761ae08745Sheppo /* 27771ae08745Sheppo * If the upper layer passed in a misaligned address we copied the 27781ae08745Sheppo * data into an aligned buffer before sending it to LDC - we now 27791ae08745Sheppo * copy it back to the original buffer. 27801ae08745Sheppo */ 27811ae08745Sheppo if (ldep->align_addr) { 27821ae08745Sheppo ASSERT(ldep->addr != NULL); 27831ae08745Sheppo ASSERT(dep->payload.nbytes > 0); 27841ae08745Sheppo 27851ae08745Sheppo bcopy(ldep->align_addr, ldep->addr, dep->payload.nbytes); 27861ae08745Sheppo kmem_free(ldep->align_addr, 2787d10e4ef2Snarayan sizeof (caddr_t) * P2ROUNDUP(dep->payload.nbytes, 8)); 27881ae08745Sheppo ldep->align_addr = NULL; 27891ae08745Sheppo } 27901ae08745Sheppo 27918e6a2a04Slm66018 rv = ldc_mem_unbind_handle(ldep->desc_mhdl); 27928e6a2a04Slm66018 if (rv != 0) { 27933af08d82Slm66018 DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)", 27948e6a2a04Slm66018 vdc->instance, ldep->desc_mhdl, idx, rv); 27958e6a2a04Slm66018 /* 27968e6a2a04Slm66018 * The error returned by the vDisk server is more informative 27978e6a2a04Slm66018 * and thus has a higher priority but if it isn't set we ensure 27988e6a2a04Slm66018 * that this function returns an error. 27998e6a2a04Slm66018 */ 28008e6a2a04Slm66018 if (status == 0) 28018e6a2a04Slm66018 status = EINVAL; 28021ae08745Sheppo } 28031ae08745Sheppo 28043af08d82Slm66018 cv_signal(&vdc->membind_cv); 28053af08d82Slm66018 cv_signal(&vdc->dring_free_cv); 28063af08d82Slm66018 28071ae08745Sheppo return (status); 28081ae08745Sheppo } 28091ae08745Sheppo 28100a55fbb7Slm66018 /* 28110a55fbb7Slm66018 * Function: 28120a55fbb7Slm66018 * vdc_populate_mem_hdl() 28130a55fbb7Slm66018 * 28140a55fbb7Slm66018 * Description: 28150a55fbb7Slm66018 * 28160a55fbb7Slm66018 * Arguments: 28170a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 28180a55fbb7Slm66018 * idx - Index of the Descriptor Ring entry being modified 28190a55fbb7Slm66018 * addr - virtual address being mapped in 28200a55fbb7Slm66018 * nybtes - number of bytes in 'addr' 28210a55fbb7Slm66018 * operation - the vDisk operation being performed (VD_OP_xxx) 28220a55fbb7Slm66018 * 28230a55fbb7Slm66018 * Return Code: 28240a55fbb7Slm66018 * 0 - Success 28250a55fbb7Slm66018 */ 28261ae08745Sheppo static int 28273af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep) 28281ae08745Sheppo { 28291ae08745Sheppo vd_dring_entry_t *dep = NULL; 28301ae08745Sheppo ldc_mem_handle_t mhdl; 28311ae08745Sheppo caddr_t vaddr; 28323af08d82Slm66018 size_t nbytes; 28334bac2208Snarayan uint8_t perm = LDC_MEM_RW; 28344bac2208Snarayan uint8_t maptype; 28351ae08745Sheppo int rv = 0; 28361ae08745Sheppo int i; 28371ae08745Sheppo 28383af08d82Slm66018 ASSERT(vdcp != NULL); 28391ae08745Sheppo 28403af08d82Slm66018 dep = ldep->dep; 28411ae08745Sheppo mhdl = ldep->desc_mhdl; 28421ae08745Sheppo 28433af08d82Slm66018 switch (ldep->dir) { 28443af08d82Slm66018 case VIO_read_dir: 28451ae08745Sheppo perm = LDC_MEM_W; 28461ae08745Sheppo break; 28471ae08745Sheppo 28483af08d82Slm66018 case VIO_write_dir: 28491ae08745Sheppo perm = LDC_MEM_R; 28501ae08745Sheppo break; 28511ae08745Sheppo 28523af08d82Slm66018 case VIO_both_dir: 28531ae08745Sheppo perm = LDC_MEM_RW; 28541ae08745Sheppo break; 28551ae08745Sheppo 28561ae08745Sheppo default: 28571ae08745Sheppo ASSERT(0); /* catch bad programming in vdc */ 28581ae08745Sheppo } 28591ae08745Sheppo 28601ae08745Sheppo /* 28611ae08745Sheppo * LDC expects any addresses passed in to be 8-byte aligned. We need 28621ae08745Sheppo * to copy the contents of any misaligned buffers to a newly allocated 28631ae08745Sheppo * buffer and bind it instead (and copy the the contents back to the 28641ae08745Sheppo * original buffer passed in when depopulating the descriptor) 28651ae08745Sheppo */ 28663af08d82Slm66018 vaddr = ldep->addr; 28673af08d82Slm66018 nbytes = ldep->nbytes; 28683af08d82Slm66018 if (((uint64_t)vaddr & 0x7) != 0) { 2869d10e4ef2Snarayan ASSERT(ldep->align_addr == NULL); 28701ae08745Sheppo ldep->align_addr = 28713af08d82Slm66018 kmem_alloc(sizeof (caddr_t) * 28723af08d82Slm66018 P2ROUNDUP(nbytes, 8), KM_SLEEP); 28733af08d82Slm66018 DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating " 28743af08d82Slm66018 "(buf=%p nb=%ld op=%d)\n", 28753af08d82Slm66018 vdcp->instance, (void *)vaddr, (void *)ldep->align_addr, 28763af08d82Slm66018 nbytes, ldep->operation); 28773af08d82Slm66018 if (perm != LDC_MEM_W) 28783af08d82Slm66018 bcopy(vaddr, ldep->align_addr, nbytes); 28791ae08745Sheppo vaddr = ldep->align_addr; 28801ae08745Sheppo } 28811ae08745Sheppo 28824bac2208Snarayan maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP; 28831ae08745Sheppo rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8), 28844bac2208Snarayan maptype, perm, &dep->payload.cookie[0], 28851ae08745Sheppo &dep->payload.ncookies); 28863af08d82Slm66018 DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n", 28873af08d82Slm66018 vdcp->instance, dep->payload.ncookies); 28881ae08745Sheppo if (rv != 0) { 28893af08d82Slm66018 DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle " 28903af08d82Slm66018 "(mhdl=%p, buf=%p, err=%d)\n", 28913af08d82Slm66018 vdcp->instance, (void *)mhdl, (void *)vaddr, rv); 28921ae08745Sheppo if (ldep->align_addr) { 28931ae08745Sheppo kmem_free(ldep->align_addr, 2894d10e4ef2Snarayan sizeof (caddr_t) * P2ROUNDUP(nbytes, 8)); 28951ae08745Sheppo ldep->align_addr = NULL; 28961ae08745Sheppo } 28971ae08745Sheppo return (EAGAIN); 28981ae08745Sheppo } 28991ae08745Sheppo 29001ae08745Sheppo /* 29011ae08745Sheppo * Get the other cookies (if any). 29021ae08745Sheppo */ 29031ae08745Sheppo for (i = 1; i < dep->payload.ncookies; i++) { 29041ae08745Sheppo rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]); 29051ae08745Sheppo if (rv != 0) { 29061ae08745Sheppo (void) ldc_mem_unbind_handle(mhdl); 29073af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Failed to get next cookie " 2908e1ebb9ecSlm66018 "(mhdl=%lx cnum=%d), err=%d", 29093af08d82Slm66018 vdcp->instance, mhdl, i, rv); 29101ae08745Sheppo if (ldep->align_addr) { 29111ae08745Sheppo kmem_free(ldep->align_addr, 29121ae08745Sheppo sizeof (caddr_t) * dep->payload.nbytes); 29131ae08745Sheppo ldep->align_addr = NULL; 29141ae08745Sheppo } 29151ae08745Sheppo return (EAGAIN); 29161ae08745Sheppo } 29171ae08745Sheppo } 29181ae08745Sheppo 29191ae08745Sheppo return (rv); 29201ae08745Sheppo } 29211ae08745Sheppo 29221ae08745Sheppo /* 29231ae08745Sheppo * Interrupt handlers for messages from LDC 29241ae08745Sheppo */ 29251ae08745Sheppo 29260a55fbb7Slm66018 /* 29270a55fbb7Slm66018 * Function: 29280a55fbb7Slm66018 * vdc_handle_cb() 29290a55fbb7Slm66018 * 29300a55fbb7Slm66018 * Description: 29310a55fbb7Slm66018 * 29320a55fbb7Slm66018 * Arguments: 29330a55fbb7Slm66018 * event - Type of event (LDC_EVT_xxx) that triggered the callback 29340a55fbb7Slm66018 * arg - soft state pointer for this instance of the device driver. 29350a55fbb7Slm66018 * 29360a55fbb7Slm66018 * Return Code: 29370a55fbb7Slm66018 * 0 - Success 29380a55fbb7Slm66018 */ 29391ae08745Sheppo static uint_t 29401ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg) 29411ae08745Sheppo { 29421ae08745Sheppo ldc_status_t ldc_state; 29431ae08745Sheppo int rv = 0; 29441ae08745Sheppo 29451ae08745Sheppo vdc_t *vdc = (vdc_t *)(void *)arg; 29461ae08745Sheppo 29471ae08745Sheppo ASSERT(vdc != NULL); 29481ae08745Sheppo 29493af08d82Slm66018 DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num); 29501ae08745Sheppo 29511ae08745Sheppo /* 29521ae08745Sheppo * Depending on the type of event that triggered this callback, 29533af08d82Slm66018 * we modify the handshake state or read the data. 29541ae08745Sheppo * 29551ae08745Sheppo * NOTE: not done as a switch() as event could be triggered by 29561ae08745Sheppo * a state change and a read request. Also the ordering of the 29571ae08745Sheppo * check for the event types is deliberate. 29581ae08745Sheppo */ 29591ae08745Sheppo if (event & LDC_EVT_UP) { 29603af08d82Slm66018 DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance); 29613af08d82Slm66018 29623af08d82Slm66018 mutex_enter(&vdc->lock); 29631ae08745Sheppo 29641ae08745Sheppo /* get LDC state */ 29651ae08745Sheppo rv = ldc_status(vdc->ldc_handle, &ldc_state); 29661ae08745Sheppo if (rv != 0) { 29673af08d82Slm66018 DMSG(vdc, 0, "[%d] Couldn't get LDC status %d", 29681ae08745Sheppo vdc->instance, rv); 29691ae08745Sheppo return (LDC_SUCCESS); 29701ae08745Sheppo } 29713af08d82Slm66018 if (vdc->ldc_state != LDC_UP && ldc_state == LDC_UP) { 29721ae08745Sheppo /* 29733af08d82Slm66018 * Reset the transaction sequence numbers when 29743af08d82Slm66018 * LDC comes up. We then kick off the handshake 29753af08d82Slm66018 * negotiation with the vDisk server. 29761ae08745Sheppo */ 29770a55fbb7Slm66018 vdc->seq_num = 1; 29781ae08745Sheppo vdc->seq_num_reply = 0; 29791ae08745Sheppo vdc->ldc_state = ldc_state; 29803af08d82Slm66018 cv_signal(&vdc->initwait_cv); 29813af08d82Slm66018 } 29823af08d82Slm66018 29831ae08745Sheppo mutex_exit(&vdc->lock); 29841ae08745Sheppo } 29851ae08745Sheppo 29861ae08745Sheppo if (event & LDC_EVT_READ) { 29873af08d82Slm66018 DMSG(vdc, 0, "[%d] Received LDC_EVT_READ\n", vdc->instance); 29883af08d82Slm66018 mutex_enter(&vdc->read_lock); 29893af08d82Slm66018 cv_signal(&vdc->read_cv); 29903af08d82Slm66018 vdc->read_state = VDC_READ_PENDING; 29913af08d82Slm66018 mutex_exit(&vdc->read_lock); 29921ae08745Sheppo 29931ae08745Sheppo /* that's all we have to do - no need to handle DOWN/RESET */ 29941ae08745Sheppo return (LDC_SUCCESS); 29951ae08745Sheppo } 29961ae08745Sheppo 29973af08d82Slm66018 if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) { 29980a55fbb7Slm66018 29993af08d82Slm66018 DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance); 30003af08d82Slm66018 30010a55fbb7Slm66018 mutex_enter(&vdc->lock); 30023af08d82Slm66018 /* 30033af08d82Slm66018 * Need to wake up any readers so they will 30043af08d82Slm66018 * detect that a reset has occurred. 30053af08d82Slm66018 */ 30063af08d82Slm66018 mutex_enter(&vdc->read_lock); 30073af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 30083af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) 30093af08d82Slm66018 cv_signal(&vdc->read_cv); 30103af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 30113af08d82Slm66018 mutex_exit(&vdc->read_lock); 30120a55fbb7Slm66018 30133af08d82Slm66018 /* wake up any threads waiting for connection to come up */ 30143af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 30153af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 30163af08d82Slm66018 cv_signal(&vdc->initwait_cv); 30171ae08745Sheppo } 30181ae08745Sheppo 30190a55fbb7Slm66018 mutex_exit(&vdc->lock); 30201ae08745Sheppo } 30211ae08745Sheppo 30221ae08745Sheppo if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ)) 30233af08d82Slm66018 DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received", 30241ae08745Sheppo vdc->instance, event); 30251ae08745Sheppo 30261ae08745Sheppo return (LDC_SUCCESS); 30271ae08745Sheppo } 30281ae08745Sheppo 30293af08d82Slm66018 /* 30303af08d82Slm66018 * Function: 30313af08d82Slm66018 * vdc_wait_for_response() 30323af08d82Slm66018 * 30333af08d82Slm66018 * Description: 30343af08d82Slm66018 * Block waiting for a response from the server. If there is 30353af08d82Slm66018 * no data the thread block on the read_cv that is signalled 30363af08d82Slm66018 * by the callback when an EVT_READ occurs. 30373af08d82Slm66018 * 30383af08d82Slm66018 * Arguments: 30393af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 30403af08d82Slm66018 * 30413af08d82Slm66018 * Return Code: 30423af08d82Slm66018 * 0 - Success 30433af08d82Slm66018 */ 30443af08d82Slm66018 static int 30453af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp) 30463af08d82Slm66018 { 30473af08d82Slm66018 size_t nbytes = sizeof (*msgp); 30483af08d82Slm66018 int status; 30493af08d82Slm66018 30503af08d82Slm66018 ASSERT(vdcp != NULL); 30513af08d82Slm66018 30523af08d82Slm66018 DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance); 30533af08d82Slm66018 30543af08d82Slm66018 status = vdc_recv(vdcp, msgp, &nbytes); 30553af08d82Slm66018 DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n", 30563af08d82Slm66018 status, (int)nbytes); 30573af08d82Slm66018 if (status) { 30583af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n", 30593af08d82Slm66018 vdcp->instance, status); 30603af08d82Slm66018 return (status); 30613af08d82Slm66018 } 30623af08d82Slm66018 30633af08d82Slm66018 if (nbytes < sizeof (vio_msg_tag_t)) { 30643af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n", 30653af08d82Slm66018 vdcp->instance, sizeof (vio_msg_tag_t), nbytes); 30663af08d82Slm66018 return (ENOMSG); 30673af08d82Slm66018 } 30683af08d82Slm66018 30693af08d82Slm66018 DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance, 30703af08d82Slm66018 msgp->tag.vio_msgtype, 30713af08d82Slm66018 msgp->tag.vio_subtype, 30723af08d82Slm66018 msgp->tag.vio_subtype_env); 30733af08d82Slm66018 30743af08d82Slm66018 /* 30753af08d82Slm66018 * Verify the Session ID of the message 30763af08d82Slm66018 * 30773af08d82Slm66018 * Every message after the Version has been negotiated should 30783af08d82Slm66018 * have the correct session ID set. 30793af08d82Slm66018 */ 30803af08d82Slm66018 if ((msgp->tag.vio_sid != vdcp->session_id) && 30813af08d82Slm66018 (msgp->tag.vio_subtype_env != VIO_VER_INFO)) { 30823af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, " 30833af08d82Slm66018 "expected 0x%lx [seq num %lx @ %d]", 30843af08d82Slm66018 vdcp->instance, msgp->tag.vio_sid, 30853af08d82Slm66018 vdcp->session_id, 30863af08d82Slm66018 ((vio_dring_msg_t *)msgp)->seq_num, 30873af08d82Slm66018 ((vio_dring_msg_t *)msgp)->start_idx); 30883af08d82Slm66018 return (ENOMSG); 30893af08d82Slm66018 } 30903af08d82Slm66018 return (0); 30913af08d82Slm66018 } 30923af08d82Slm66018 30933af08d82Slm66018 30943af08d82Slm66018 /* 30953af08d82Slm66018 * Function: 30963af08d82Slm66018 * vdc_resubmit_backup_dring() 30973af08d82Slm66018 * 30983af08d82Slm66018 * Description: 30993af08d82Slm66018 * Resubmit each descriptor in the backed up dring to 31003af08d82Slm66018 * vDisk server. The Dring was backed up during connection 31013af08d82Slm66018 * reset. 31023af08d82Slm66018 * 31033af08d82Slm66018 * Arguments: 31043af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 31053af08d82Slm66018 * 31063af08d82Slm66018 * Return Code: 31073af08d82Slm66018 * 0 - Success 31083af08d82Slm66018 */ 31093af08d82Slm66018 static int 31103af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp) 31113af08d82Slm66018 { 31123af08d82Slm66018 int count; 31133af08d82Slm66018 int b_idx; 31143af08d82Slm66018 int rv; 31153af08d82Slm66018 int dring_size; 31163af08d82Slm66018 int status; 31173af08d82Slm66018 vio_msg_t vio_msg; 31183af08d82Slm66018 vdc_local_desc_t *curr_ldep; 31193af08d82Slm66018 31203af08d82Slm66018 ASSERT(MUTEX_NOT_HELD(&vdcp->lock)); 31213af08d82Slm66018 ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING); 31223af08d82Slm66018 31233af08d82Slm66018 DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n", 31243af08d82Slm66018 vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail); 31253af08d82Slm66018 31263af08d82Slm66018 /* 31273af08d82Slm66018 * Walk the backup copy of the local descriptor ring and 31283af08d82Slm66018 * resubmit all the outstanding transactions. 31293af08d82Slm66018 */ 31303af08d82Slm66018 b_idx = vdcp->local_dring_backup_tail; 31313af08d82Slm66018 for (count = 0; count < vdcp->local_dring_backup_len; count++) { 31323af08d82Slm66018 31333af08d82Slm66018 curr_ldep = &(vdcp->local_dring_backup[b_idx]); 31343af08d82Slm66018 31353af08d82Slm66018 /* only resubmit oustanding transactions */ 31363af08d82Slm66018 if (!curr_ldep->is_free) { 31373af08d82Slm66018 31383af08d82Slm66018 DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx); 31393af08d82Slm66018 mutex_enter(&vdcp->lock); 31403af08d82Slm66018 rv = vdc_populate_descriptor(vdcp, curr_ldep->operation, 31413af08d82Slm66018 curr_ldep->addr, curr_ldep->nbytes, 31423af08d82Slm66018 curr_ldep->slice, curr_ldep->offset, 31433af08d82Slm66018 curr_ldep->cb_type, curr_ldep->cb_arg, 31443af08d82Slm66018 curr_ldep->dir); 31453af08d82Slm66018 mutex_exit(&vdcp->lock); 31463af08d82Slm66018 if (rv) { 31473af08d82Slm66018 DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n", 31483af08d82Slm66018 vdcp->instance, b_idx); 31493af08d82Slm66018 return (rv); 31503af08d82Slm66018 } 31513af08d82Slm66018 31523af08d82Slm66018 /* Wait for the response message. */ 31533af08d82Slm66018 DMSG(vdcp, 1, "waiting for response to idx=%x\n", 31543af08d82Slm66018 b_idx); 31553af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 31563af08d82Slm66018 if (status) { 31573af08d82Slm66018 DMSG(vdcp, 1, "[%d] wait_for_response " 31583af08d82Slm66018 "returned err=%d\n", vdcp->instance, 31593af08d82Slm66018 status); 31603af08d82Slm66018 return (status); 31613af08d82Slm66018 } 31623af08d82Slm66018 31633af08d82Slm66018 DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx); 31643af08d82Slm66018 status = vdc_process_data_msg(vdcp, &vio_msg); 31653af08d82Slm66018 if (status) { 31663af08d82Slm66018 DMSG(vdcp, 1, "[%d] process_data_msg " 31673af08d82Slm66018 "returned err=%d\n", vdcp->instance, 31683af08d82Slm66018 status); 31693af08d82Slm66018 return (status); 31703af08d82Slm66018 } 31713af08d82Slm66018 } 31723af08d82Slm66018 31733af08d82Slm66018 /* get the next element to submit */ 31743af08d82Slm66018 if (++b_idx >= vdcp->local_dring_backup_len) 31753af08d82Slm66018 b_idx = 0; 31763af08d82Slm66018 } 31773af08d82Slm66018 31783af08d82Slm66018 /* all done - now clear up pending dring copy */ 31793af08d82Slm66018 dring_size = vdcp->local_dring_backup_len * 31803af08d82Slm66018 sizeof (vdcp->local_dring_backup[0]); 31813af08d82Slm66018 31823af08d82Slm66018 (void) kmem_free(vdcp->local_dring_backup, dring_size); 31833af08d82Slm66018 31843af08d82Slm66018 vdcp->local_dring_backup = NULL; 31853af08d82Slm66018 31863af08d82Slm66018 return (0); 31873af08d82Slm66018 } 31883af08d82Slm66018 31893af08d82Slm66018 /* 31903af08d82Slm66018 * Function: 31913af08d82Slm66018 * vdc_backup_local_dring() 31923af08d82Slm66018 * 31933af08d82Slm66018 * Description: 31943af08d82Slm66018 * Backup the current dring in the event of a reset. The Dring 31953af08d82Slm66018 * transactions will be resubmitted to the server when the 31963af08d82Slm66018 * connection is restored. 31973af08d82Slm66018 * 31983af08d82Slm66018 * Arguments: 31993af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 32003af08d82Slm66018 * 32013af08d82Slm66018 * Return Code: 32023af08d82Slm66018 * NONE 32033af08d82Slm66018 */ 32043af08d82Slm66018 static void 32053af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp) 32063af08d82Slm66018 { 32073af08d82Slm66018 int dring_size; 32083af08d82Slm66018 32093af08d82Slm66018 ASSERT(vdcp->state == VDC_STATE_RESETTING); 32103af08d82Slm66018 32113af08d82Slm66018 /* 32123af08d82Slm66018 * If the backup dring is stil around, it means 32133af08d82Slm66018 * that the last restore did not complete. However, 32143af08d82Slm66018 * since we never got back into the running state, 32153af08d82Slm66018 * the backup copy we have is still valid. 32163af08d82Slm66018 */ 32173af08d82Slm66018 if (vdcp->local_dring_backup != NULL) { 32183af08d82Slm66018 DMSG(vdcp, 1, "reusing local descriptor ring backup " 32193af08d82Slm66018 "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len, 32203af08d82Slm66018 vdcp->local_dring_backup_tail); 32213af08d82Slm66018 return; 32223af08d82Slm66018 } 32233af08d82Slm66018 32243af08d82Slm66018 DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, " 32253af08d82Slm66018 "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx); 32263af08d82Slm66018 32273af08d82Slm66018 dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]); 32283af08d82Slm66018 32293af08d82Slm66018 vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP); 32303af08d82Slm66018 bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size); 32313af08d82Slm66018 32323af08d82Slm66018 vdcp->local_dring_backup_tail = vdcp->dring_curr_idx; 32333af08d82Slm66018 vdcp->local_dring_backup_len = vdcp->dring_len; 32343af08d82Slm66018 } 32353af08d82Slm66018 32361ae08745Sheppo /* -------------------------------------------------------------------------- */ 32371ae08745Sheppo 32381ae08745Sheppo /* 32391ae08745Sheppo * The following functions process the incoming messages from vds 32401ae08745Sheppo */ 32411ae08745Sheppo 32420a55fbb7Slm66018 /* 32430a55fbb7Slm66018 * Function: 32440a55fbb7Slm66018 * vdc_process_msg_thread() 32450a55fbb7Slm66018 * 32460a55fbb7Slm66018 * Description: 32470a55fbb7Slm66018 * 32483af08d82Slm66018 * Main VDC message processing thread. Each vDisk instance 32493af08d82Slm66018 * consists of a copy of this thread. This thread triggers 32503af08d82Slm66018 * all the handshakes and data exchange with the server. It 32513af08d82Slm66018 * also handles all channel resets 32523af08d82Slm66018 * 32530a55fbb7Slm66018 * Arguments: 32540a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 32550a55fbb7Slm66018 * 32560a55fbb7Slm66018 * Return Code: 32570a55fbb7Slm66018 * None 32580a55fbb7Slm66018 */ 32591ae08745Sheppo static void 32603af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp) 32611ae08745Sheppo { 32621ae08745Sheppo int status; 32631ae08745Sheppo 32643af08d82Slm66018 mutex_enter(&vdcp->lock); 32651ae08745Sheppo 32661ae08745Sheppo for (;;) { 32671ae08745Sheppo 32683af08d82Slm66018 #define Q(_s) (vdcp->state == _s) ? #_s : 32693af08d82Slm66018 DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state, 32703af08d82Slm66018 Q(VDC_STATE_INIT) 32713af08d82Slm66018 Q(VDC_STATE_INIT_WAITING) 32723af08d82Slm66018 Q(VDC_STATE_NEGOTIATE) 32733af08d82Slm66018 Q(VDC_STATE_HANDLE_PENDING) 32743af08d82Slm66018 Q(VDC_STATE_RUNNING) 32753af08d82Slm66018 Q(VDC_STATE_RESETTING) 32763af08d82Slm66018 Q(VDC_STATE_DETACH) 32773af08d82Slm66018 "UNKNOWN"); 32781ae08745Sheppo 32793af08d82Slm66018 switch (vdcp->state) { 32803af08d82Slm66018 case VDC_STATE_INIT: 32813af08d82Slm66018 32823af08d82Slm66018 /* Check if have re-initializing repeatedly */ 32833af08d82Slm66018 if (vdcp->hshake_cnt++ > VDC_RETRIES) { 32843af08d82Slm66018 vdcp->state = VDC_STATE_DETACH; 32853af08d82Slm66018 break; 32863af08d82Slm66018 } 32873af08d82Slm66018 32883af08d82Slm66018 /* Bring up connection with vds via LDC */ 32893af08d82Slm66018 status = vdc_start_ldc_connection(vdcp); 32903af08d82Slm66018 switch (status) { 32913af08d82Slm66018 case EINVAL: 32923af08d82Slm66018 DMSG(vdcp, 0, "[%d] Could not start LDC", 32933af08d82Slm66018 vdcp->instance); 32943af08d82Slm66018 vdcp->state = VDC_STATE_DETACH; 32953af08d82Slm66018 break; 32963af08d82Slm66018 case 0: 32973af08d82Slm66018 vdcp->state = VDC_STATE_INIT_WAITING; 32983af08d82Slm66018 break; 32993af08d82Slm66018 default: 33003af08d82Slm66018 vdcp->state = VDC_STATE_INIT_WAITING; 33013af08d82Slm66018 break; 33023af08d82Slm66018 } 33033af08d82Slm66018 break; 33043af08d82Slm66018 33053af08d82Slm66018 case VDC_STATE_INIT_WAITING: 33063af08d82Slm66018 33073af08d82Slm66018 /* 33083af08d82Slm66018 * Let the callback event move us on 33093af08d82Slm66018 * when channel is open to server 33103af08d82Slm66018 */ 33113af08d82Slm66018 while (vdcp->ldc_state != LDC_UP) { 33123af08d82Slm66018 cv_wait(&vdcp->initwait_cv, &vdcp->lock); 33133af08d82Slm66018 if (vdcp->state != VDC_STATE_INIT_WAITING) { 33143af08d82Slm66018 DMSG(vdcp, 0, 33153af08d82Slm66018 "state moved to %d out from under us...\n", 33163af08d82Slm66018 vdcp->state); 33173af08d82Slm66018 33183af08d82Slm66018 break; 33193af08d82Slm66018 } 33203af08d82Slm66018 } 33213af08d82Slm66018 if (vdcp->state == VDC_STATE_INIT_WAITING && 33223af08d82Slm66018 vdcp->ldc_state == LDC_UP) { 33233af08d82Slm66018 vdcp->state = VDC_STATE_NEGOTIATE; 33243af08d82Slm66018 } 33253af08d82Slm66018 break; 33263af08d82Slm66018 33273af08d82Slm66018 case VDC_STATE_NEGOTIATE: 33283af08d82Slm66018 switch (status = vdc_ver_negotiation(vdcp)) { 33293af08d82Slm66018 case 0: 33303af08d82Slm66018 break; 33313af08d82Slm66018 default: 33323af08d82Slm66018 DMSG(vdcp, 0, "ver negotiate failed (%d)..\n", 33333af08d82Slm66018 status); 33343af08d82Slm66018 goto reset; 33353af08d82Slm66018 } 33363af08d82Slm66018 33373af08d82Slm66018 switch (status = vdc_attr_negotiation(vdcp)) { 33383af08d82Slm66018 case 0: 33393af08d82Slm66018 break; 33403af08d82Slm66018 default: 33413af08d82Slm66018 DMSG(vdcp, 0, "attr negotiate failed (%d)..\n", 33423af08d82Slm66018 status); 33433af08d82Slm66018 goto reset; 33443af08d82Slm66018 } 33453af08d82Slm66018 33463af08d82Slm66018 switch (status = vdc_dring_negotiation(vdcp)) { 33473af08d82Slm66018 case 0: 33483af08d82Slm66018 break; 33493af08d82Slm66018 default: 33503af08d82Slm66018 DMSG(vdcp, 0, "dring negotiate failed (%d)..\n", 33513af08d82Slm66018 status); 33523af08d82Slm66018 goto reset; 33533af08d82Slm66018 } 33543af08d82Slm66018 33553af08d82Slm66018 switch (status = vdc_rdx_exchange(vdcp)) { 33563af08d82Slm66018 case 0: 33573af08d82Slm66018 vdcp->state = VDC_STATE_HANDLE_PENDING; 33583af08d82Slm66018 goto done; 33593af08d82Slm66018 default: 33603af08d82Slm66018 DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n", 33613af08d82Slm66018 status); 33623af08d82Slm66018 goto reset; 33633af08d82Slm66018 } 33643af08d82Slm66018 reset: 33653af08d82Slm66018 DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n", 33663af08d82Slm66018 status); 33673af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 33683af08d82Slm66018 done: 33693af08d82Slm66018 DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n", 33703af08d82Slm66018 vdcp->state); 33713af08d82Slm66018 break; 33723af08d82Slm66018 33733af08d82Slm66018 case VDC_STATE_HANDLE_PENDING: 33743af08d82Slm66018 33753af08d82Slm66018 mutex_exit(&vdcp->lock); 33763af08d82Slm66018 status = vdc_resubmit_backup_dring(vdcp); 33773af08d82Slm66018 mutex_enter(&vdcp->lock); 33783af08d82Slm66018 33793af08d82Slm66018 if (status) 33803af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 33813af08d82Slm66018 else 33823af08d82Slm66018 vdcp->state = VDC_STATE_RUNNING; 33833af08d82Slm66018 33843af08d82Slm66018 break; 33853af08d82Slm66018 33863af08d82Slm66018 /* enter running state */ 33873af08d82Slm66018 case VDC_STATE_RUNNING: 33883af08d82Slm66018 /* 33893af08d82Slm66018 * Signal anyone waiting for the connection 33903af08d82Slm66018 * to come on line. 33913af08d82Slm66018 */ 33923af08d82Slm66018 vdcp->hshake_cnt = 0; 33933af08d82Slm66018 cv_broadcast(&vdcp->running_cv); 33943af08d82Slm66018 mutex_exit(&vdcp->lock); 33953af08d82Slm66018 33963af08d82Slm66018 for (;;) { 33973af08d82Slm66018 vio_msg_t msg; 33983af08d82Slm66018 status = vdc_wait_for_response(vdcp, &msg); 33993af08d82Slm66018 if (status) break; 34003af08d82Slm66018 34013af08d82Slm66018 DMSG(vdcp, 1, "[%d] new pkt(s) available\n", 34023af08d82Slm66018 vdcp->instance); 34033af08d82Slm66018 status = vdc_process_data_msg(vdcp, &msg); 34041ae08745Sheppo if (status) { 34053af08d82Slm66018 DMSG(vdcp, 1, "[%d] process_data_msg " 34063af08d82Slm66018 "returned err=%d\n", vdcp->instance, 34073af08d82Slm66018 status); 34081ae08745Sheppo break; 34091ae08745Sheppo } 34101ae08745Sheppo 34113af08d82Slm66018 } 3412e1ebb9ecSlm66018 34133af08d82Slm66018 mutex_enter(&vdcp->lock); 34143af08d82Slm66018 34153af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 34163af08d82Slm66018 break; 34173af08d82Slm66018 34183af08d82Slm66018 case VDC_STATE_RESETTING: 34193af08d82Slm66018 DMSG(vdcp, 0, "Initiating channel reset " 34203af08d82Slm66018 "(pending = %d)\n", (int)vdcp->threads_pending); 34213af08d82Slm66018 34223af08d82Slm66018 if (vdcp->self_reset) { 34233af08d82Slm66018 DMSG(vdcp, 0, 34243af08d82Slm66018 "[%d] calling stop_ldc_connection.\n", 34253af08d82Slm66018 vdcp->instance); 34263af08d82Slm66018 status = vdc_stop_ldc_connection(vdcp); 34273af08d82Slm66018 vdcp->self_reset = B_FALSE; 34281ae08745Sheppo } 34291ae08745Sheppo 34301ae08745Sheppo /* 34313af08d82Slm66018 * Wait for all threads currently waiting 34323af08d82Slm66018 * for a free dring entry to use. 34331ae08745Sheppo */ 34343af08d82Slm66018 while (vdcp->threads_pending) { 34353af08d82Slm66018 cv_broadcast(&vdcp->membind_cv); 34363af08d82Slm66018 cv_broadcast(&vdcp->dring_free_cv); 34373af08d82Slm66018 mutex_exit(&vdcp->lock); 34383af08d82Slm66018 /* let them wake up */ 34393af08d82Slm66018 drv_usecwait(vdc_min_timeout_ldc); 34403af08d82Slm66018 mutex_enter(&vdcp->lock); 34411ae08745Sheppo } 34421ae08745Sheppo 34433af08d82Slm66018 ASSERT(vdcp->threads_pending == 0); 34441ae08745Sheppo 34453af08d82Slm66018 /* Sanity check that no thread is receiving */ 34463af08d82Slm66018 ASSERT(vdcp->read_state != VDC_READ_WAITING); 34470a55fbb7Slm66018 34483af08d82Slm66018 vdcp->read_state = VDC_READ_IDLE; 34493af08d82Slm66018 34503af08d82Slm66018 vdc_backup_local_dring(vdcp); 34513af08d82Slm66018 34523af08d82Slm66018 /* cleanup the old d-ring */ 34533af08d82Slm66018 vdc_destroy_descriptor_ring(vdcp); 34543af08d82Slm66018 34553af08d82Slm66018 /* go and start again */ 34563af08d82Slm66018 vdcp->state = VDC_STATE_INIT; 34573af08d82Slm66018 34580a55fbb7Slm66018 break; 34590a55fbb7Slm66018 34603af08d82Slm66018 case VDC_STATE_DETACH: 34613af08d82Slm66018 DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n", 34623af08d82Slm66018 vdcp->instance); 34633af08d82Slm66018 34643af08d82Slm66018 while (vdcp->sync_op_pending) { 34653af08d82Slm66018 cv_signal(&vdcp->sync_pending_cv); 34663af08d82Slm66018 cv_signal(&vdcp->sync_blocked_cv); 34673af08d82Slm66018 mutex_exit(&vdcp->lock); 34683af08d82Slm66018 drv_usecwait(vdc_min_timeout_ldc); 34693af08d82Slm66018 mutex_enter(&vdcp->lock); 34700a55fbb7Slm66018 } 34711ae08745Sheppo 34723af08d82Slm66018 cv_signal(&vdcp->running_cv); 34733af08d82Slm66018 mutex_exit(&vdcp->lock); 34743af08d82Slm66018 34753af08d82Slm66018 DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n", 34763af08d82Slm66018 vdcp->instance); 34773af08d82Slm66018 thread_exit(); 34783af08d82Slm66018 break; 34793af08d82Slm66018 } 34803af08d82Slm66018 } 34810a55fbb7Slm66018 } 34820a55fbb7Slm66018 34830a55fbb7Slm66018 34840a55fbb7Slm66018 /* 34850a55fbb7Slm66018 * Function: 34860a55fbb7Slm66018 * vdc_process_data_msg() 34870a55fbb7Slm66018 * 34880a55fbb7Slm66018 * Description: 34890a55fbb7Slm66018 * This function is called by the message processing thread each time 34900a55fbb7Slm66018 * a message with a msgtype of VIO_TYPE_DATA is received. It will either 34910a55fbb7Slm66018 * be an ACK or NACK from vds[1] which vdc handles as follows. 34920a55fbb7Slm66018 * ACK - wake up the waiting thread 34930a55fbb7Slm66018 * NACK - resend any messages necessary 34940a55fbb7Slm66018 * 34950a55fbb7Slm66018 * [1] Although the message format allows it, vds should not send a 34960a55fbb7Slm66018 * VIO_SUBTYPE_INFO message to vdc asking it to read data; if for 34970a55fbb7Slm66018 * some bizarre reason it does, vdc will reset the connection. 34980a55fbb7Slm66018 * 34990a55fbb7Slm66018 * Arguments: 35000a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 35010a55fbb7Slm66018 * msg - the LDC message sent by vds 35020a55fbb7Slm66018 * 35030a55fbb7Slm66018 * Return Code: 35040a55fbb7Slm66018 * 0 - Success. 35050a55fbb7Slm66018 * > 0 - error value returned by LDC 35060a55fbb7Slm66018 */ 35070a55fbb7Slm66018 static int 35083af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg) 35090a55fbb7Slm66018 { 35100a55fbb7Slm66018 int status = 0; 35113af08d82Slm66018 vio_dring_msg_t *dring_msg; 3512d10e4ef2Snarayan vdc_local_desc_t *ldep = NULL; 35133af08d82Slm66018 int start, end; 35143af08d82Slm66018 int idx; 35150a55fbb7Slm66018 35163af08d82Slm66018 dring_msg = (vio_dring_msg_t *)msg; 35170a55fbb7Slm66018 35183af08d82Slm66018 ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA); 35193af08d82Slm66018 ASSERT(vdcp != NULL); 35203af08d82Slm66018 35213af08d82Slm66018 mutex_enter(&vdcp->lock); 35220a55fbb7Slm66018 35230a55fbb7Slm66018 /* 35240a55fbb7Slm66018 * Check to see if the message has bogus data 35250a55fbb7Slm66018 */ 3526e1ebb9ecSlm66018 idx = start = dring_msg->start_idx; 35270a55fbb7Slm66018 end = dring_msg->end_idx; 35283af08d82Slm66018 if ((start >= vdcp->dring_len) || 35293af08d82Slm66018 (end >= vdcp->dring_len) || (end < -1)) { 35303af08d82Slm66018 DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n", 35313af08d82Slm66018 vdcp->instance, start, end); 35323af08d82Slm66018 mutex_exit(&vdcp->lock); 3533e1ebb9ecSlm66018 return (EINVAL); 35340a55fbb7Slm66018 } 35350a55fbb7Slm66018 35360a55fbb7Slm66018 /* 35370a55fbb7Slm66018 * Verify that the sequence number is what vdc expects. 35380a55fbb7Slm66018 */ 35393af08d82Slm66018 switch (vdc_verify_seq_num(vdcp, dring_msg)) { 3540e1ebb9ecSlm66018 case VDC_SEQ_NUM_TODO: 3541e1ebb9ecSlm66018 break; /* keep processing this message */ 3542e1ebb9ecSlm66018 case VDC_SEQ_NUM_SKIP: 35433af08d82Slm66018 mutex_exit(&vdcp->lock); 3544e1ebb9ecSlm66018 return (0); 3545e1ebb9ecSlm66018 case VDC_SEQ_NUM_INVALID: 35463af08d82Slm66018 mutex_exit(&vdcp->lock); 35473af08d82Slm66018 DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance); 35480a55fbb7Slm66018 return (ENXIO); 35490a55fbb7Slm66018 } 35500a55fbb7Slm66018 35513af08d82Slm66018 if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) { 35523af08d82Slm66018 DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance); 3553e1ebb9ecSlm66018 VDC_DUMP_DRING_MSG(dring_msg); 35543af08d82Slm66018 mutex_exit(&vdcp->lock); 3555e1ebb9ecSlm66018 return (EIO); 35560a55fbb7Slm66018 35573af08d82Slm66018 } else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) { 35583af08d82Slm66018 mutex_exit(&vdcp->lock); 3559e1ebb9ecSlm66018 return (EPROTO); 3560e1ebb9ecSlm66018 } 3561e1ebb9ecSlm66018 35623af08d82Slm66018 DTRACE_IO2(recv, vio_dring_msg_t, dring_msg, vdc_t *, vdcp); 35633af08d82Slm66018 DMSG(vdcp, 1, ": start %d end %d\n", start, end); 35643af08d82Slm66018 ASSERT(start == end); 35653af08d82Slm66018 35663af08d82Slm66018 ldep = &vdcp->local_dring[idx]; 35673af08d82Slm66018 35683af08d82Slm66018 DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n", 35693af08d82Slm66018 ldep->dep->hdr.dstate, ldep->cb_type); 35703af08d82Slm66018 3571e1ebb9ecSlm66018 if (ldep->dep->hdr.dstate == VIO_DESC_DONE) { 35723af08d82Slm66018 struct buf *bufp; 3573e1ebb9ecSlm66018 35743af08d82Slm66018 switch (ldep->cb_type) { 35753af08d82Slm66018 case CB_SYNC: 35763af08d82Slm66018 ASSERT(vdcp->sync_op_pending); 3577d10e4ef2Snarayan 35783af08d82Slm66018 status = vdc_depopulate_descriptor(vdcp, idx); 35793af08d82Slm66018 vdcp->sync_op_status = status; 35803af08d82Slm66018 vdcp->sync_op_pending = B_FALSE; 35813af08d82Slm66018 cv_signal(&vdcp->sync_pending_cv); 35823af08d82Slm66018 break; 35834bac2208Snarayan 35843af08d82Slm66018 case CB_STRATEGY: 35853af08d82Slm66018 bufp = ldep->cb_arg; 35863af08d82Slm66018 ASSERT(bufp != NULL); 35873af08d82Slm66018 status = ldep->dep->payload.status; /* Future:ntoh */ 35883af08d82Slm66018 if (status != 0) { 35893af08d82Slm66018 DMSG(vdcp, 1, "strategy status=%d\n", status); 35903af08d82Slm66018 bioerror(bufp, status); 3591d10e4ef2Snarayan } 35923af08d82Slm66018 status = vdc_depopulate_descriptor(vdcp, idx); 35933af08d82Slm66018 biodone(bufp); 35943af08d82Slm66018 break; 35953af08d82Slm66018 35963af08d82Slm66018 default: 35973af08d82Slm66018 ASSERT(0); 35980a55fbb7Slm66018 } 35993af08d82Slm66018 } 36003af08d82Slm66018 36013af08d82Slm66018 /* let the arrival signal propogate */ 36023af08d82Slm66018 mutex_exit(&vdcp->lock); 36030a55fbb7Slm66018 3604e1ebb9ecSlm66018 /* probe gives the count of how many entries were processed */ 36053af08d82Slm66018 DTRACE_IO2(processed, int, 1, vdc_t *, vdcp); 36060a55fbb7Slm66018 36073af08d82Slm66018 return (0); 36080a55fbb7Slm66018 } 36090a55fbb7Slm66018 36100a55fbb7Slm66018 /* 36110a55fbb7Slm66018 * Function: 36120a55fbb7Slm66018 * vdc_process_err_msg() 36130a55fbb7Slm66018 * 36140a55fbb7Slm66018 * NOTE: No error messages are used as part of the vDisk protocol 36150a55fbb7Slm66018 */ 36160a55fbb7Slm66018 static int 36170a55fbb7Slm66018 vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg) 36180a55fbb7Slm66018 { 36190a55fbb7Slm66018 _NOTE(ARGUNUSED(vdc)) 36200a55fbb7Slm66018 _NOTE(ARGUNUSED(msg)) 36210a55fbb7Slm66018 36220a55fbb7Slm66018 ASSERT(msg.tag.vio_msgtype == VIO_TYPE_ERR); 36233af08d82Slm66018 DMSG(vdc, 1, "[%d] Got an ERR msg", vdc->instance); 36240a55fbb7Slm66018 36250a55fbb7Slm66018 return (ENOTSUP); 36260a55fbb7Slm66018 } 36270a55fbb7Slm66018 36280a55fbb7Slm66018 /* 36290a55fbb7Slm66018 * Function: 36300a55fbb7Slm66018 * vdc_handle_ver_msg() 36310a55fbb7Slm66018 * 36320a55fbb7Slm66018 * Description: 36330a55fbb7Slm66018 * 36340a55fbb7Slm66018 * Arguments: 36350a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 36360a55fbb7Slm66018 * ver_msg - LDC message sent by vDisk server 36370a55fbb7Slm66018 * 36380a55fbb7Slm66018 * Return Code: 36390a55fbb7Slm66018 * 0 - Success 36400a55fbb7Slm66018 */ 36410a55fbb7Slm66018 static int 36420a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg) 36430a55fbb7Slm66018 { 36440a55fbb7Slm66018 int status = 0; 36450a55fbb7Slm66018 36460a55fbb7Slm66018 ASSERT(vdc != NULL); 36470a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 36480a55fbb7Slm66018 36490a55fbb7Slm66018 if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) { 36500a55fbb7Slm66018 return (EPROTO); 36510a55fbb7Slm66018 } 36520a55fbb7Slm66018 36530a55fbb7Slm66018 if (ver_msg->dev_class != VDEV_DISK_SERVER) { 36540a55fbb7Slm66018 return (EINVAL); 36550a55fbb7Slm66018 } 36560a55fbb7Slm66018 36570a55fbb7Slm66018 switch (ver_msg->tag.vio_subtype) { 36580a55fbb7Slm66018 case VIO_SUBTYPE_ACK: 36590a55fbb7Slm66018 /* 36600a55fbb7Slm66018 * We check to see if the version returned is indeed supported 36610a55fbb7Slm66018 * (The server may have also adjusted the minor number downwards 36620a55fbb7Slm66018 * and if so 'ver_msg' will contain the actual version agreed) 36630a55fbb7Slm66018 */ 36640a55fbb7Slm66018 if (vdc_is_supported_version(ver_msg)) { 36650a55fbb7Slm66018 vdc->ver.major = ver_msg->ver_major; 36660a55fbb7Slm66018 vdc->ver.minor = ver_msg->ver_minor; 36670a55fbb7Slm66018 ASSERT(vdc->ver.major > 0); 36680a55fbb7Slm66018 } else { 36690a55fbb7Slm66018 status = EPROTO; 36700a55fbb7Slm66018 } 36710a55fbb7Slm66018 break; 36720a55fbb7Slm66018 36730a55fbb7Slm66018 case VIO_SUBTYPE_NACK: 36740a55fbb7Slm66018 /* 36750a55fbb7Slm66018 * call vdc_is_supported_version() which will return the next 36760a55fbb7Slm66018 * supported version (if any) in 'ver_msg' 36770a55fbb7Slm66018 */ 36780a55fbb7Slm66018 (void) vdc_is_supported_version(ver_msg); 36790a55fbb7Slm66018 if (ver_msg->ver_major > 0) { 36800a55fbb7Slm66018 size_t len = sizeof (*ver_msg); 36810a55fbb7Slm66018 36820a55fbb7Slm66018 ASSERT(vdc->ver.major > 0); 36830a55fbb7Slm66018 36840a55fbb7Slm66018 /* reset the necessary fields and resend */ 36850a55fbb7Slm66018 ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO; 36860a55fbb7Slm66018 ver_msg->dev_class = VDEV_DISK; 36870a55fbb7Slm66018 36880a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)ver_msg, &len); 36893af08d82Slm66018 DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n", 36900a55fbb7Slm66018 vdc->instance, status); 36910a55fbb7Slm66018 if (len != sizeof (*ver_msg)) 36920a55fbb7Slm66018 status = EBADMSG; 36930a55fbb7Slm66018 } else { 36943af08d82Slm66018 DMSG(vdc, 0, "[%d] No common version with " 36950a55fbb7Slm66018 "vDisk server", vdc->instance); 36960a55fbb7Slm66018 status = ENOTSUP; 36970a55fbb7Slm66018 } 36980a55fbb7Slm66018 36990a55fbb7Slm66018 break; 37001ae08745Sheppo case VIO_SUBTYPE_INFO: 37011ae08745Sheppo /* 37021ae08745Sheppo * Handle the case where vds starts handshake 37031ae08745Sheppo * (for now only vdc is the instigatior) 37041ae08745Sheppo */ 37051ae08745Sheppo status = ENOTSUP; 37061ae08745Sheppo break; 37071ae08745Sheppo 37081ae08745Sheppo default: 37090a55fbb7Slm66018 status = EINVAL; 37101ae08745Sheppo break; 37111ae08745Sheppo } 37121ae08745Sheppo 37130a55fbb7Slm66018 return (status); 37140a55fbb7Slm66018 } 37150a55fbb7Slm66018 37160a55fbb7Slm66018 /* 37170a55fbb7Slm66018 * Function: 37180a55fbb7Slm66018 * vdc_handle_attr_msg() 37190a55fbb7Slm66018 * 37200a55fbb7Slm66018 * Description: 37210a55fbb7Slm66018 * 37220a55fbb7Slm66018 * Arguments: 37230a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 37240a55fbb7Slm66018 * attr_msg - LDC message sent by vDisk server 37250a55fbb7Slm66018 * 37260a55fbb7Slm66018 * Return Code: 37270a55fbb7Slm66018 * 0 - Success 37280a55fbb7Slm66018 */ 37290a55fbb7Slm66018 static int 37300a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg) 37310a55fbb7Slm66018 { 37320a55fbb7Slm66018 int status = 0; 37330a55fbb7Slm66018 37340a55fbb7Slm66018 ASSERT(vdc != NULL); 37350a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 37360a55fbb7Slm66018 37370a55fbb7Slm66018 if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) { 37380a55fbb7Slm66018 return (EPROTO); 37390a55fbb7Slm66018 } 37400a55fbb7Slm66018 37410a55fbb7Slm66018 switch (attr_msg->tag.vio_subtype) { 37421ae08745Sheppo case VIO_SUBTYPE_ACK: 37431ae08745Sheppo /* 37441ae08745Sheppo * We now verify the attributes sent by vds. 37451ae08745Sheppo */ 37461ae08745Sheppo vdc->vdisk_size = attr_msg->vdisk_size; 37471ae08745Sheppo vdc->vdisk_type = attr_msg->vdisk_type; 37481ae08745Sheppo 37493af08d82Slm66018 DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n", 3750e1ebb9ecSlm66018 vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz); 37513af08d82Slm66018 DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n", 3752e1ebb9ecSlm66018 vdc->instance, vdc->block_size, 3753e1ebb9ecSlm66018 attr_msg->vdisk_block_size); 3754e1ebb9ecSlm66018 37551ae08745Sheppo /* 3756e1ebb9ecSlm66018 * We don't know at compile time what the vDisk server will 3757e1ebb9ecSlm66018 * think are good values but we apply an large (arbitrary) 3758e1ebb9ecSlm66018 * upper bound to prevent memory exhaustion in vdc if it was 3759e1ebb9ecSlm66018 * allocating a DRing based of huge values sent by the server. 3760e1ebb9ecSlm66018 * We probably will never exceed this except if the message 3761e1ebb9ecSlm66018 * was garbage. 37621ae08745Sheppo */ 3763e1ebb9ecSlm66018 if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <= 3764e1ebb9ecSlm66018 (PAGESIZE * DEV_BSIZE)) { 3765e1ebb9ecSlm66018 vdc->max_xfer_sz = attr_msg->max_xfer_sz; 3766e1ebb9ecSlm66018 vdc->block_size = attr_msg->vdisk_block_size; 3767e1ebb9ecSlm66018 } else { 37683af08d82Slm66018 DMSG(vdc, 0, "[%d] vds block transfer size too big;" 3769e1ebb9ecSlm66018 " using max supported by vdc", vdc->instance); 37701ae08745Sheppo } 37711ae08745Sheppo 37721ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DRING_MODE) || 37731ae08745Sheppo (attr_msg->vdisk_size > INT64_MAX) || 37741ae08745Sheppo (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) { 37753af08d82Slm66018 DMSG(vdc, 0, "[%d] Invalid attributes from vds", 3776e1ebb9ecSlm66018 vdc->instance); 37771ae08745Sheppo status = EINVAL; 37781ae08745Sheppo break; 37791ae08745Sheppo } 37801ae08745Sheppo 37811ae08745Sheppo break; 37821ae08745Sheppo 37831ae08745Sheppo case VIO_SUBTYPE_NACK: 37841ae08745Sheppo /* 37851ae08745Sheppo * vds could not handle the attributes we sent so we 37861ae08745Sheppo * stop negotiating. 37871ae08745Sheppo */ 37881ae08745Sheppo status = EPROTO; 37891ae08745Sheppo break; 37901ae08745Sheppo 37911ae08745Sheppo case VIO_SUBTYPE_INFO: 37921ae08745Sheppo /* 37931ae08745Sheppo * Handle the case where vds starts the handshake 37941ae08745Sheppo * (for now; vdc is the only supported instigatior) 37951ae08745Sheppo */ 37961ae08745Sheppo status = ENOTSUP; 37971ae08745Sheppo break; 37981ae08745Sheppo 37991ae08745Sheppo default: 38001ae08745Sheppo status = ENOTSUP; 38011ae08745Sheppo break; 38021ae08745Sheppo } 38031ae08745Sheppo 38040a55fbb7Slm66018 return (status); 38051ae08745Sheppo } 38061ae08745Sheppo 38070a55fbb7Slm66018 /* 38080a55fbb7Slm66018 * Function: 38090a55fbb7Slm66018 * vdc_handle_dring_reg_msg() 38100a55fbb7Slm66018 * 38110a55fbb7Slm66018 * Description: 38120a55fbb7Slm66018 * 38130a55fbb7Slm66018 * Arguments: 38140a55fbb7Slm66018 * vdc - soft state pointer for this instance of the driver. 38150a55fbb7Slm66018 * dring_msg - LDC message sent by vDisk server 38160a55fbb7Slm66018 * 38170a55fbb7Slm66018 * Return Code: 38180a55fbb7Slm66018 * 0 - Success 38190a55fbb7Slm66018 */ 38200a55fbb7Slm66018 static int 38210a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg) 38220a55fbb7Slm66018 { 38230a55fbb7Slm66018 int status = 0; 38241ae08745Sheppo 38250a55fbb7Slm66018 ASSERT(vdc != NULL); 38260a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 38270a55fbb7Slm66018 38280a55fbb7Slm66018 if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) { 38290a55fbb7Slm66018 return (EPROTO); 38300a55fbb7Slm66018 } 38310a55fbb7Slm66018 38320a55fbb7Slm66018 switch (dring_msg->tag.vio_subtype) { 38330a55fbb7Slm66018 case VIO_SUBTYPE_ACK: 38341ae08745Sheppo /* save the received dring_ident */ 38351ae08745Sheppo vdc->dring_ident = dring_msg->dring_ident; 38363af08d82Slm66018 DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n", 3837e1ebb9ecSlm66018 vdc->instance, vdc->dring_ident); 38381ae08745Sheppo break; 38391ae08745Sheppo 38401ae08745Sheppo case VIO_SUBTYPE_NACK: 38411ae08745Sheppo /* 38421ae08745Sheppo * vds could not handle the DRing info we sent so we 38431ae08745Sheppo * stop negotiating. 38441ae08745Sheppo */ 38453af08d82Slm66018 DMSG(vdc, 0, "[%d] server could not register DRing\n", 38463af08d82Slm66018 vdc->instance); 38471ae08745Sheppo status = EPROTO; 38481ae08745Sheppo break; 38491ae08745Sheppo 38501ae08745Sheppo case VIO_SUBTYPE_INFO: 38511ae08745Sheppo /* 38521ae08745Sheppo * Handle the case where vds starts handshake 38531ae08745Sheppo * (for now only vdc is the instigatior) 38541ae08745Sheppo */ 38551ae08745Sheppo status = ENOTSUP; 38561ae08745Sheppo break; 38571ae08745Sheppo default: 38581ae08745Sheppo status = ENOTSUP; 38591ae08745Sheppo } 38601ae08745Sheppo 38611ae08745Sheppo return (status); 38621ae08745Sheppo } 38631ae08745Sheppo 38641ae08745Sheppo /* 38651ae08745Sheppo * Function: 38661ae08745Sheppo * vdc_verify_seq_num() 38671ae08745Sheppo * 38681ae08745Sheppo * Description: 3869e1ebb9ecSlm66018 * This functions verifies that the sequence number sent back by the vDisk 3870e1ebb9ecSlm66018 * server with the latest message is what is expected (i.e. it is greater 3871e1ebb9ecSlm66018 * than the last seq num sent by the vDisk server and less than or equal 3872e1ebb9ecSlm66018 * to the last seq num generated by vdc). 3873e1ebb9ecSlm66018 * 3874e1ebb9ecSlm66018 * It then checks the request ID to see if any requests need processing 3875e1ebb9ecSlm66018 * in the DRing. 38761ae08745Sheppo * 38771ae08745Sheppo * Arguments: 38781ae08745Sheppo * vdc - soft state pointer for this instance of the driver. 38791ae08745Sheppo * dring_msg - pointer to the LDC message sent by vds 38801ae08745Sheppo * 38811ae08745Sheppo * Return Code: 3882e1ebb9ecSlm66018 * VDC_SEQ_NUM_TODO - Message needs to be processed 3883e1ebb9ecSlm66018 * VDC_SEQ_NUM_SKIP - Message has already been processed 3884e1ebb9ecSlm66018 * VDC_SEQ_NUM_INVALID - The seq numbers are so out of sync, 3885e1ebb9ecSlm66018 * vdc cannot deal with them 38861ae08745Sheppo */ 3887e1ebb9ecSlm66018 static int 3888e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg) 38891ae08745Sheppo { 38901ae08745Sheppo ASSERT(vdc != NULL); 38911ae08745Sheppo ASSERT(dring_msg != NULL); 3892d10e4ef2Snarayan ASSERT(mutex_owned(&vdc->lock)); 38931ae08745Sheppo 38941ae08745Sheppo /* 38951ae08745Sheppo * Check to see if the messages were responded to in the correct 3896e1ebb9ecSlm66018 * order by vds. 38971ae08745Sheppo */ 3898e1ebb9ecSlm66018 if ((dring_msg->seq_num <= vdc->seq_num_reply) || 3899e1ebb9ecSlm66018 (dring_msg->seq_num > vdc->seq_num)) { 39003af08d82Slm66018 DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: " 3901e1ebb9ecSlm66018 "%lu > expected <= %lu (last proc req %lu sent %lu)\n", 3902e1ebb9ecSlm66018 vdc->instance, dring_msg->seq_num, 3903e1ebb9ecSlm66018 vdc->seq_num_reply, vdc->seq_num, 3904e1ebb9ecSlm66018 vdc->req_id_proc, vdc->req_id); 3905e1ebb9ecSlm66018 return (VDC_SEQ_NUM_INVALID); 39061ae08745Sheppo } 3907e1ebb9ecSlm66018 vdc->seq_num_reply = dring_msg->seq_num; 39081ae08745Sheppo 3909e1ebb9ecSlm66018 if (vdc->req_id_proc < vdc->req_id) 3910e1ebb9ecSlm66018 return (VDC_SEQ_NUM_TODO); 3911e1ebb9ecSlm66018 else 3912e1ebb9ecSlm66018 return (VDC_SEQ_NUM_SKIP); 39131ae08745Sheppo } 39141ae08745Sheppo 39150a55fbb7Slm66018 39160a55fbb7Slm66018 /* 39170a55fbb7Slm66018 * Function: 39180a55fbb7Slm66018 * vdc_is_supported_version() 39190a55fbb7Slm66018 * 39200a55fbb7Slm66018 * Description: 39210a55fbb7Slm66018 * This routine checks if the major/minor version numbers specified in 39220a55fbb7Slm66018 * 'ver_msg' are supported. If not it finds the next version that is 39230a55fbb7Slm66018 * in the supported version list 'vdc_version[]' and sets the fields in 39240a55fbb7Slm66018 * 'ver_msg' to those values 39250a55fbb7Slm66018 * 39260a55fbb7Slm66018 * Arguments: 39270a55fbb7Slm66018 * ver_msg - LDC message sent by vDisk server 39280a55fbb7Slm66018 * 39290a55fbb7Slm66018 * Return Code: 39300a55fbb7Slm66018 * B_TRUE - Success 39310a55fbb7Slm66018 * B_FALSE - Version not supported 39320a55fbb7Slm66018 */ 39330a55fbb7Slm66018 static boolean_t 39340a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg) 39350a55fbb7Slm66018 { 39360a55fbb7Slm66018 int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]); 39370a55fbb7Slm66018 39380a55fbb7Slm66018 for (int i = 0; i < vdc_num_versions; i++) { 39390a55fbb7Slm66018 ASSERT(vdc_version[i].major > 0); 39400a55fbb7Slm66018 ASSERT((i == 0) || 39410a55fbb7Slm66018 (vdc_version[i].major < vdc_version[i-1].major)); 39420a55fbb7Slm66018 39430a55fbb7Slm66018 /* 39440a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 39450a55fbb7Slm66018 * necessary, down to the highest value supported by this 39460a55fbb7Slm66018 * client. The server should support all minor versions lower 39470a55fbb7Slm66018 * than the value it sent 39480a55fbb7Slm66018 */ 39490a55fbb7Slm66018 if (ver_msg->ver_major == vdc_version[i].major) { 39500a55fbb7Slm66018 if (ver_msg->ver_minor > vdc_version[i].minor) { 39513af08d82Slm66018 DMSGX(0, 39523af08d82Slm66018 "Adjusting minor version from %u to %u", 39530a55fbb7Slm66018 ver_msg->ver_minor, vdc_version[i].minor); 39540a55fbb7Slm66018 ver_msg->ver_minor = vdc_version[i].minor; 39550a55fbb7Slm66018 } 39560a55fbb7Slm66018 return (B_TRUE); 39570a55fbb7Slm66018 } 39580a55fbb7Slm66018 39590a55fbb7Slm66018 /* 39600a55fbb7Slm66018 * If the message contains a higher major version number, set 39610a55fbb7Slm66018 * the message's major/minor versions to the current values 39620a55fbb7Slm66018 * and return false, so this message will get resent with 39630a55fbb7Slm66018 * these values, and the server will potentially try again 39640a55fbb7Slm66018 * with the same or a lower version 39650a55fbb7Slm66018 */ 39660a55fbb7Slm66018 if (ver_msg->ver_major > vdc_version[i].major) { 39670a55fbb7Slm66018 ver_msg->ver_major = vdc_version[i].major; 39680a55fbb7Slm66018 ver_msg->ver_minor = vdc_version[i].minor; 39693af08d82Slm66018 DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n", 39700a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 39710a55fbb7Slm66018 39720a55fbb7Slm66018 return (B_FALSE); 39730a55fbb7Slm66018 } 39740a55fbb7Slm66018 39750a55fbb7Slm66018 /* 39760a55fbb7Slm66018 * Otherwise, the message's major version is less than the 39770a55fbb7Slm66018 * current major version, so continue the loop to the next 39780a55fbb7Slm66018 * (lower) supported version 39790a55fbb7Slm66018 */ 39800a55fbb7Slm66018 } 39810a55fbb7Slm66018 39820a55fbb7Slm66018 /* 39830a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 39840a55fbb7Slm66018 * message to terminate negotiation 39850a55fbb7Slm66018 */ 39860a55fbb7Slm66018 ver_msg->ver_major = 0; 39870a55fbb7Slm66018 ver_msg->ver_minor = 0; 39880a55fbb7Slm66018 39890a55fbb7Slm66018 return (B_FALSE); 39900a55fbb7Slm66018 } 39911ae08745Sheppo /* -------------------------------------------------------------------------- */ 39921ae08745Sheppo 39931ae08745Sheppo /* 39941ae08745Sheppo * DKIO(7) support 39951ae08745Sheppo */ 39961ae08745Sheppo 39971ae08745Sheppo typedef struct vdc_dk_arg { 39981ae08745Sheppo struct dk_callback dkc; 39991ae08745Sheppo int mode; 40001ae08745Sheppo dev_t dev; 40011ae08745Sheppo vdc_t *vdc; 40021ae08745Sheppo } vdc_dk_arg_t; 40031ae08745Sheppo 40041ae08745Sheppo /* 40051ae08745Sheppo * Function: 40061ae08745Sheppo * vdc_dkio_flush_cb() 40071ae08745Sheppo * 40081ae08745Sheppo * Description: 40091ae08745Sheppo * This routine is a callback for DKIOCFLUSHWRITECACHE which can be called 40101ae08745Sheppo * by kernel code. 40111ae08745Sheppo * 40121ae08745Sheppo * Arguments: 40131ae08745Sheppo * arg - a pointer to a vdc_dk_arg_t structure. 40141ae08745Sheppo */ 40151ae08745Sheppo void 40161ae08745Sheppo vdc_dkio_flush_cb(void *arg) 40171ae08745Sheppo { 40181ae08745Sheppo struct vdc_dk_arg *dk_arg = (struct vdc_dk_arg *)arg; 40191ae08745Sheppo struct dk_callback *dkc = NULL; 40201ae08745Sheppo vdc_t *vdc = NULL; 40211ae08745Sheppo int rv; 40221ae08745Sheppo 40231ae08745Sheppo if (dk_arg == NULL) { 40243af08d82Slm66018 cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n"); 40251ae08745Sheppo return; 40261ae08745Sheppo } 40271ae08745Sheppo dkc = &dk_arg->dkc; 40281ae08745Sheppo vdc = dk_arg->vdc; 40291ae08745Sheppo ASSERT(vdc != NULL); 40301ae08745Sheppo 40313af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0, 40320d0c8d4bSnarayan VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir); 40331ae08745Sheppo if (rv != 0) { 40343af08d82Slm66018 DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n", 4035e1ebb9ecSlm66018 vdc->instance, rv, 40361ae08745Sheppo ddi_model_convert_from(dk_arg->mode & FMODELS)); 40371ae08745Sheppo } 40381ae08745Sheppo 40391ae08745Sheppo /* 40401ae08745Sheppo * Trigger the call back to notify the caller the the ioctl call has 40411ae08745Sheppo * been completed. 40421ae08745Sheppo */ 40431ae08745Sheppo if ((dk_arg->mode & FKIOCTL) && 40441ae08745Sheppo (dkc != NULL) && 40451ae08745Sheppo (dkc->dkc_callback != NULL)) { 40461ae08745Sheppo ASSERT(dkc->dkc_cookie != NULL); 40478e6a2a04Slm66018 (*dkc->dkc_callback)(dkc->dkc_cookie, rv); 40481ae08745Sheppo } 40491ae08745Sheppo 40501ae08745Sheppo /* Indicate that one less DKIO write flush is outstanding */ 40511ae08745Sheppo mutex_enter(&vdc->lock); 40521ae08745Sheppo vdc->dkio_flush_pending--; 40531ae08745Sheppo ASSERT(vdc->dkio_flush_pending >= 0); 40541ae08745Sheppo mutex_exit(&vdc->lock); 40558e6a2a04Slm66018 40568e6a2a04Slm66018 /* free the mem that was allocated when the callback was dispatched */ 40578e6a2a04Slm66018 kmem_free(arg, sizeof (vdc_dk_arg_t)); 40581ae08745Sheppo } 40591ae08745Sheppo 40601ae08745Sheppo /* 40611ae08745Sheppo * This structure is used in the DKIO(7I) array below. 40621ae08745Sheppo */ 40631ae08745Sheppo typedef struct vdc_dk_ioctl { 40641ae08745Sheppo uint8_t op; /* VD_OP_XXX value */ 40651ae08745Sheppo int cmd; /* Solaris ioctl operation number */ 40661ae08745Sheppo size_t nbytes; /* size of structure to be copied */ 40670a55fbb7Slm66018 40680a55fbb7Slm66018 /* function to convert between vDisk and Solaris structure formats */ 4069d10e4ef2Snarayan int (*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg, 4070d10e4ef2Snarayan int mode, int dir); 40711ae08745Sheppo } vdc_dk_ioctl_t; 40721ae08745Sheppo 40731ae08745Sheppo /* 40741ae08745Sheppo * Subset of DKIO(7I) operations currently supported 40751ae08745Sheppo */ 40761ae08745Sheppo static vdc_dk_ioctl_t dk_ioctl[] = { 40770a55fbb7Slm66018 {VD_OP_FLUSH, DKIOCFLUSHWRITECACHE, sizeof (int), 40780a55fbb7Slm66018 vdc_null_copy_func}, 40790a55fbb7Slm66018 {VD_OP_GET_WCE, DKIOCGETWCE, sizeof (int), 40804bac2208Snarayan vdc_get_wce_convert}, 40810a55fbb7Slm66018 {VD_OP_SET_WCE, DKIOCSETWCE, sizeof (int), 40824bac2208Snarayan vdc_set_wce_convert}, 40830a55fbb7Slm66018 {VD_OP_GET_VTOC, DKIOCGVTOC, sizeof (vd_vtoc_t), 40840a55fbb7Slm66018 vdc_get_vtoc_convert}, 40850a55fbb7Slm66018 {VD_OP_SET_VTOC, DKIOCSVTOC, sizeof (vd_vtoc_t), 40860a55fbb7Slm66018 vdc_set_vtoc_convert}, 40870a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCGGEOM, sizeof (vd_geom_t), 40880a55fbb7Slm66018 vdc_get_geom_convert}, 40890a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCG_PHYGEOM, sizeof (vd_geom_t), 40900a55fbb7Slm66018 vdc_get_geom_convert}, 40910a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCG_VIRTGEOM, sizeof (vd_geom_t), 40920a55fbb7Slm66018 vdc_get_geom_convert}, 40930a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, DKIOCSGEOM, sizeof (vd_geom_t), 40940a55fbb7Slm66018 vdc_set_geom_convert}, 40954bac2208Snarayan {VD_OP_GET_EFI, DKIOCGETEFI, 0, 40964bac2208Snarayan vdc_get_efi_convert}, 40974bac2208Snarayan {VD_OP_SET_EFI, DKIOCSETEFI, 0, 40984bac2208Snarayan vdc_set_efi_convert}, 40990a55fbb7Slm66018 41000a55fbb7Slm66018 /* 41010a55fbb7Slm66018 * These particular ioctls are not sent to the server - vdc fakes up 41020a55fbb7Slm66018 * the necessary info. 41030a55fbb7Slm66018 */ 41040a55fbb7Slm66018 {0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func}, 41050a55fbb7Slm66018 {0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func}, 41060a55fbb7Slm66018 {0, USCSICMD, sizeof (struct uscsi_cmd), vdc_null_copy_func}, 41070a55fbb7Slm66018 {0, DKIOCREMOVABLE, 0, vdc_null_copy_func}, 41080a55fbb7Slm66018 {0, CDROMREADOFFSET, 0, vdc_null_copy_func} 41091ae08745Sheppo }; 41101ae08745Sheppo 41111ae08745Sheppo /* 41121ae08745Sheppo * Function: 41131ae08745Sheppo * vd_process_ioctl() 41141ae08745Sheppo * 41151ae08745Sheppo * Description: 41160a55fbb7Slm66018 * This routine processes disk specific ioctl calls 41171ae08745Sheppo * 41181ae08745Sheppo * Arguments: 41191ae08745Sheppo * dev - the device number 41201ae08745Sheppo * cmd - the operation [dkio(7I)] to be processed 41211ae08745Sheppo * arg - pointer to user provided structure 41221ae08745Sheppo * (contains data to be set or reference parameter for get) 41231ae08745Sheppo * mode - bit flag, indicating open settings, 32/64 bit type, etc 41241ae08745Sheppo * 41251ae08745Sheppo * Return Code: 41261ae08745Sheppo * 0 41271ae08745Sheppo * EFAULT 41281ae08745Sheppo * ENXIO 41291ae08745Sheppo * EIO 41301ae08745Sheppo * ENOTSUP 41311ae08745Sheppo */ 41321ae08745Sheppo static int 41331ae08745Sheppo vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode) 41341ae08745Sheppo { 41350d0c8d4bSnarayan int instance = VDCUNIT(dev); 41361ae08745Sheppo vdc_t *vdc = NULL; 41371ae08745Sheppo int rv = -1; 41381ae08745Sheppo int idx = 0; /* index into dk_ioctl[] */ 41391ae08745Sheppo size_t len = 0; /* #bytes to send to vds */ 41401ae08745Sheppo size_t alloc_len = 0; /* #bytes to allocate mem for */ 41411ae08745Sheppo caddr_t mem_p = NULL; 41421ae08745Sheppo size_t nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0])); 4143d10e4ef2Snarayan struct vtoc vtoc_saved; 41443af08d82Slm66018 vdc_dk_ioctl_t *iop; 41451ae08745Sheppo 41461ae08745Sheppo vdc = ddi_get_soft_state(vdc_state, instance); 41471ae08745Sheppo if (vdc == NULL) { 41481ae08745Sheppo cmn_err(CE_NOTE, "![%d] Could not get soft state structure", 41491ae08745Sheppo instance); 41501ae08745Sheppo return (ENXIO); 41511ae08745Sheppo } 41521ae08745Sheppo 41533af08d82Slm66018 DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n", 41543af08d82Slm66018 instance, cmd, dev, ddi_model_convert_from(mode & FMODELS)); 41551ae08745Sheppo 41561ae08745Sheppo /* 41571ae08745Sheppo * Validate the ioctl operation to be performed. 41581ae08745Sheppo * 41591ae08745Sheppo * If we have looped through the array without finding a match then we 41601ae08745Sheppo * don't support this ioctl. 41611ae08745Sheppo */ 41621ae08745Sheppo for (idx = 0; idx < nioctls; idx++) { 41631ae08745Sheppo if (cmd == dk_ioctl[idx].cmd) 41641ae08745Sheppo break; 41651ae08745Sheppo } 41661ae08745Sheppo 41671ae08745Sheppo if (idx >= nioctls) { 41683af08d82Slm66018 DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n", 4169e1ebb9ecSlm66018 vdc->instance, cmd); 41701ae08745Sheppo return (ENOTSUP); 41711ae08745Sheppo } 41721ae08745Sheppo 41733af08d82Slm66018 iop = &(dk_ioctl[idx]); 41743af08d82Slm66018 41754bac2208Snarayan if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) { 41764bac2208Snarayan /* size is not fixed for EFI ioctls, it depends on ioctl arg */ 41774bac2208Snarayan dk_efi_t dk_efi; 41784bac2208Snarayan 41794bac2208Snarayan rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode); 41804bac2208Snarayan if (rv != 0) 41814bac2208Snarayan return (EFAULT); 41824bac2208Snarayan 41834bac2208Snarayan len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length; 41844bac2208Snarayan } else { 41853af08d82Slm66018 len = iop->nbytes; 41864bac2208Snarayan } 41871ae08745Sheppo 41881ae08745Sheppo /* 41890a55fbb7Slm66018 * Deal with the ioctls which the server does not provide. vdc can 41900a55fbb7Slm66018 * fake these up and return immediately 41911ae08745Sheppo */ 41921ae08745Sheppo switch (cmd) { 41931ae08745Sheppo case CDROMREADOFFSET: 41941ae08745Sheppo case DKIOCREMOVABLE: 41950a55fbb7Slm66018 case USCSICMD: 41961ae08745Sheppo return (ENOTTY); 41971ae08745Sheppo 41981ae08745Sheppo case DKIOCINFO: 41991ae08745Sheppo { 42001ae08745Sheppo struct dk_cinfo cinfo; 42011ae08745Sheppo if (vdc->cinfo == NULL) 42021ae08745Sheppo return (ENXIO); 42031ae08745Sheppo 42041ae08745Sheppo bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo)); 42050d0c8d4bSnarayan cinfo.dki_partition = VDCPART(dev); 42061ae08745Sheppo 42071ae08745Sheppo rv = ddi_copyout(&cinfo, (void *)arg, 42081ae08745Sheppo sizeof (struct dk_cinfo), mode); 42091ae08745Sheppo if (rv != 0) 42101ae08745Sheppo return (EFAULT); 42111ae08745Sheppo 42121ae08745Sheppo return (0); 42131ae08745Sheppo } 42141ae08745Sheppo 42151ae08745Sheppo case DKIOCGMEDIAINFO: 42168e6a2a04Slm66018 { 42171ae08745Sheppo if (vdc->minfo == NULL) 42181ae08745Sheppo return (ENXIO); 42191ae08745Sheppo 42201ae08745Sheppo rv = ddi_copyout(vdc->minfo, (void *)arg, 42211ae08745Sheppo sizeof (struct dk_minfo), mode); 42221ae08745Sheppo if (rv != 0) 42231ae08745Sheppo return (EFAULT); 42241ae08745Sheppo 42251ae08745Sheppo return (0); 42261ae08745Sheppo } 42271ae08745Sheppo 42288e6a2a04Slm66018 case DKIOCFLUSHWRITECACHE: 42298e6a2a04Slm66018 { 42308e6a2a04Slm66018 struct dk_callback *dkc = (struct dk_callback *)arg; 42318e6a2a04Slm66018 vdc_dk_arg_t *dkarg = NULL; 42328e6a2a04Slm66018 42333af08d82Slm66018 DMSG(vdc, 1, "[%d] Flush W$: mode %x\n", 42343af08d82Slm66018 instance, mode); 42358e6a2a04Slm66018 42368e6a2a04Slm66018 /* 42378e6a2a04Slm66018 * If the backing device is not a 'real' disk then the 42388e6a2a04Slm66018 * W$ operation request to the vDisk server will fail 42398e6a2a04Slm66018 * so we might as well save the cycles and return now. 42408e6a2a04Slm66018 */ 42418e6a2a04Slm66018 if (vdc->vdisk_type != VD_DISK_TYPE_DISK) 42428e6a2a04Slm66018 return (ENOTTY); 42438e6a2a04Slm66018 42448e6a2a04Slm66018 /* 42458e6a2a04Slm66018 * If arg is NULL, then there is no callback function 42468e6a2a04Slm66018 * registered and the call operates synchronously; we 42478e6a2a04Slm66018 * break and continue with the rest of the function and 42488e6a2a04Slm66018 * wait for vds to return (i.e. after the request to 42498e6a2a04Slm66018 * vds returns successfully, all writes completed prior 42508e6a2a04Slm66018 * to the ioctl will have been flushed from the disk 42518e6a2a04Slm66018 * write cache to persistent media. 42528e6a2a04Slm66018 * 42538e6a2a04Slm66018 * If a callback function is registered, we dispatch 42548e6a2a04Slm66018 * the request on a task queue and return immediately. 42558e6a2a04Slm66018 * The callback will deal with informing the calling 42568e6a2a04Slm66018 * thread that the flush request is completed. 42578e6a2a04Slm66018 */ 42588e6a2a04Slm66018 if (dkc == NULL) 42598e6a2a04Slm66018 break; 42608e6a2a04Slm66018 42618e6a2a04Slm66018 dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP); 42628e6a2a04Slm66018 42638e6a2a04Slm66018 dkarg->mode = mode; 42648e6a2a04Slm66018 dkarg->dev = dev; 42658e6a2a04Slm66018 bcopy(dkc, &dkarg->dkc, sizeof (*dkc)); 42668e6a2a04Slm66018 42678e6a2a04Slm66018 mutex_enter(&vdc->lock); 42688e6a2a04Slm66018 vdc->dkio_flush_pending++; 42698e6a2a04Slm66018 dkarg->vdc = vdc; 42708e6a2a04Slm66018 mutex_exit(&vdc->lock); 42718e6a2a04Slm66018 42728e6a2a04Slm66018 /* put the request on a task queue */ 42738e6a2a04Slm66018 rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb, 42748e6a2a04Slm66018 (void *)dkarg, DDI_SLEEP); 42753af08d82Slm66018 if (rv == NULL) { 42763af08d82Slm66018 /* clean up if dispatch fails */ 42773af08d82Slm66018 mutex_enter(&vdc->lock); 42783af08d82Slm66018 vdc->dkio_flush_pending--; 42793af08d82Slm66018 kmem_free(dkarg, sizeof (vdc_dk_arg_t)); 42803af08d82Slm66018 } 42818e6a2a04Slm66018 42828e6a2a04Slm66018 return (rv == NULL ? ENOMEM : 0); 42838e6a2a04Slm66018 } 42848e6a2a04Slm66018 } 42858e6a2a04Slm66018 42861ae08745Sheppo /* catch programming error in vdc - should be a VD_OP_XXX ioctl */ 42873af08d82Slm66018 ASSERT(iop->op != 0); 42881ae08745Sheppo 42891ae08745Sheppo /* LDC requires that the memory being mapped is 8-byte aligned */ 42901ae08745Sheppo alloc_len = P2ROUNDUP(len, sizeof (uint64_t)); 42913af08d82Slm66018 DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n", 42923af08d82Slm66018 instance, len, alloc_len); 42931ae08745Sheppo 42940a55fbb7Slm66018 ASSERT(alloc_len != 0); /* sanity check */ 42951ae08745Sheppo mem_p = kmem_zalloc(alloc_len, KM_SLEEP); 42961ae08745Sheppo 4297d10e4ef2Snarayan if (cmd == DKIOCSVTOC) { 4298d10e4ef2Snarayan /* 4299d10e4ef2Snarayan * Save a copy of the current VTOC so that we can roll back 4300d10e4ef2Snarayan * if the setting of the new VTOC fails. 4301d10e4ef2Snarayan */ 4302d10e4ef2Snarayan bcopy(vdc->vtoc, &vtoc_saved, sizeof (struct vtoc)); 4303d10e4ef2Snarayan } 4304d10e4ef2Snarayan 43050a55fbb7Slm66018 /* 43060a55fbb7Slm66018 * Call the conversion function for this ioctl whhich if necessary 43070a55fbb7Slm66018 * converts from the Solaris format to the format ARC'ed 43080a55fbb7Slm66018 * as part of the vDisk protocol (FWARC 2006/195) 43090a55fbb7Slm66018 */ 43103af08d82Slm66018 ASSERT(iop->convert != NULL); 43113af08d82Slm66018 rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN); 43121ae08745Sheppo if (rv != 0) { 43133af08d82Slm66018 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n", 4314e1ebb9ecSlm66018 instance, rv, cmd); 43151ae08745Sheppo if (mem_p != NULL) 43161ae08745Sheppo kmem_free(mem_p, alloc_len); 43170a55fbb7Slm66018 return (rv); 43181ae08745Sheppo } 43191ae08745Sheppo 43201ae08745Sheppo /* 43211ae08745Sheppo * send request to vds to service the ioctl. 43221ae08745Sheppo */ 43233af08d82Slm66018 rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len, 43240d0c8d4bSnarayan VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode, 43253af08d82Slm66018 VIO_both_dir); 43263af08d82Slm66018 43271ae08745Sheppo if (rv != 0) { 43281ae08745Sheppo /* 43291ae08745Sheppo * This is not necessarily an error. The ioctl could 43301ae08745Sheppo * be returning a value such as ENOTTY to indicate 43311ae08745Sheppo * that the ioctl is not applicable. 43321ae08745Sheppo */ 43333af08d82Slm66018 DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n", 4334e1ebb9ecSlm66018 instance, rv, cmd); 43351ae08745Sheppo if (mem_p != NULL) 43361ae08745Sheppo kmem_free(mem_p, alloc_len); 4337d10e4ef2Snarayan 4338d10e4ef2Snarayan if (cmd == DKIOCSVTOC) { 4339d10e4ef2Snarayan /* update of the VTOC has failed, roll back */ 4340d10e4ef2Snarayan bcopy(&vtoc_saved, vdc->vtoc, sizeof (struct vtoc)); 4341d10e4ef2Snarayan } 4342d10e4ef2Snarayan 43431ae08745Sheppo return (rv); 43441ae08745Sheppo } 43451ae08745Sheppo 43461ae08745Sheppo if (cmd == DKIOCSVTOC) { 4347d10e4ef2Snarayan /* 43484bac2208Snarayan * The VTOC has been changed. We need to update the device 43494bac2208Snarayan * nodes to handle the case where an EFI label has been 43504bac2208Snarayan * changed to a VTOC label. We also try and update the device 4351d10e4ef2Snarayan * node properties. Failing to set the properties should 4352d10e4ef2Snarayan * not cause an error to be return the caller though. 4353d10e4ef2Snarayan */ 43544bac2208Snarayan vdc->vdisk_label = VD_DISK_LABEL_VTOC; 43554bac2208Snarayan (void) vdc_create_device_nodes_vtoc(vdc); 43564bac2208Snarayan 43571ae08745Sheppo if (vdc_create_device_nodes_props(vdc)) { 43583af08d82Slm66018 DMSG(vdc, 0, "![%d] Failed to update device nodes" 4359d10e4ef2Snarayan " properties", vdc->instance); 43601ae08745Sheppo } 43614bac2208Snarayan 43624bac2208Snarayan } else if (cmd == DKIOCSETEFI) { 43634bac2208Snarayan /* 43644bac2208Snarayan * The EFI has been changed. We need to update the device 43654bac2208Snarayan * nodes to handle the case where a VTOC label has been 43664bac2208Snarayan * changed to an EFI label. We also try and update the device 43674bac2208Snarayan * node properties. Failing to set the properties should 43684bac2208Snarayan * not cause an error to be return the caller though. 43694bac2208Snarayan */ 43704bac2208Snarayan struct dk_gpt *efi; 43714bac2208Snarayan size_t efi_len; 43724bac2208Snarayan 43734bac2208Snarayan vdc->vdisk_label = VD_DISK_LABEL_EFI; 43744bac2208Snarayan (void) vdc_create_device_nodes_efi(vdc); 43754bac2208Snarayan 43764bac2208Snarayan rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len); 43774bac2208Snarayan 43784bac2208Snarayan if (rv == 0) { 43794bac2208Snarayan vdc_store_efi(vdc, efi); 43804bac2208Snarayan rv = vdc_create_device_nodes_props(vdc); 43814bac2208Snarayan vd_efi_free(efi, efi_len); 43824bac2208Snarayan } 43834bac2208Snarayan 43844bac2208Snarayan if (rv) { 43853af08d82Slm66018 DMSG(vdc, 0, "![%d] Failed to update device nodes" 43864bac2208Snarayan " properties", vdc->instance); 43874bac2208Snarayan } 43881ae08745Sheppo } 43891ae08745Sheppo 43901ae08745Sheppo /* 43910a55fbb7Slm66018 * Call the conversion function (if it exists) for this ioctl 43920a55fbb7Slm66018 * which converts from the format ARC'ed as part of the vDisk 43930a55fbb7Slm66018 * protocol (FWARC 2006/195) back to a format understood by 43940a55fbb7Slm66018 * the rest of Solaris. 43951ae08745Sheppo */ 43963af08d82Slm66018 rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT); 43970a55fbb7Slm66018 if (rv != 0) { 43983af08d82Slm66018 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n", 4399e1ebb9ecSlm66018 instance, rv, cmd); 44001ae08745Sheppo if (mem_p != NULL) 44011ae08745Sheppo kmem_free(mem_p, alloc_len); 44020a55fbb7Slm66018 return (rv); 44031ae08745Sheppo } 44041ae08745Sheppo 44051ae08745Sheppo if (mem_p != NULL) 44061ae08745Sheppo kmem_free(mem_p, alloc_len); 44071ae08745Sheppo 44081ae08745Sheppo return (rv); 44091ae08745Sheppo } 44101ae08745Sheppo 44111ae08745Sheppo /* 44121ae08745Sheppo * Function: 44130a55fbb7Slm66018 * 44140a55fbb7Slm66018 * Description: 44150a55fbb7Slm66018 * This is an empty conversion function used by ioctl calls which 44160a55fbb7Slm66018 * do not need to convert the data being passed in/out to userland 44170a55fbb7Slm66018 */ 44180a55fbb7Slm66018 static int 4419d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir) 44200a55fbb7Slm66018 { 4421d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 44220a55fbb7Slm66018 _NOTE(ARGUNUSED(from)) 44230a55fbb7Slm66018 _NOTE(ARGUNUSED(to)) 44240a55fbb7Slm66018 _NOTE(ARGUNUSED(mode)) 44250a55fbb7Slm66018 _NOTE(ARGUNUSED(dir)) 44260a55fbb7Slm66018 44270a55fbb7Slm66018 return (0); 44280a55fbb7Slm66018 } 44290a55fbb7Slm66018 44304bac2208Snarayan static int 44314bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to, 44324bac2208Snarayan int mode, int dir) 44334bac2208Snarayan { 44344bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 44354bac2208Snarayan 44364bac2208Snarayan if (dir == VD_COPYIN) 44374bac2208Snarayan return (0); /* nothing to do */ 44384bac2208Snarayan 44394bac2208Snarayan if (ddi_copyout(from, to, sizeof (int), mode) != 0) 44404bac2208Snarayan return (EFAULT); 44414bac2208Snarayan 44424bac2208Snarayan return (0); 44434bac2208Snarayan } 44444bac2208Snarayan 44454bac2208Snarayan static int 44464bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to, 44474bac2208Snarayan int mode, int dir) 44484bac2208Snarayan { 44494bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 44504bac2208Snarayan 44514bac2208Snarayan if (dir == VD_COPYOUT) 44524bac2208Snarayan return (0); /* nothing to do */ 44534bac2208Snarayan 44544bac2208Snarayan if (ddi_copyin(from, to, sizeof (int), mode) != 0) 44554bac2208Snarayan return (EFAULT); 44564bac2208Snarayan 44574bac2208Snarayan return (0); 44584bac2208Snarayan } 44594bac2208Snarayan 44600a55fbb7Slm66018 /* 44610a55fbb7Slm66018 * Function: 44620a55fbb7Slm66018 * vdc_get_vtoc_convert() 44630a55fbb7Slm66018 * 44640a55fbb7Slm66018 * Description: 4465d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCGVTOC 4466d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 4467d10e4ef2Snarayan * 4468d10e4ef2Snarayan * In the struct vtoc definition, the timestamp field is marked as not 4469d10e4ef2Snarayan * supported so it is not part of vDisk protocol (FWARC 2006/195). 4470d10e4ef2Snarayan * However SVM uses that field to check it can write into the VTOC, 4471d10e4ef2Snarayan * so we fake up the info of that field. 44720a55fbb7Slm66018 * 44730a55fbb7Slm66018 * Arguments: 4474d10e4ef2Snarayan * vdc - the vDisk client 44750a55fbb7Slm66018 * from - the buffer containing the data to be copied from 44760a55fbb7Slm66018 * to - the buffer to be copied to 44770a55fbb7Slm66018 * mode - flags passed to ioctl() call 44780a55fbb7Slm66018 * dir - the "direction" of the copy - VD_COPYIN or VD_COPYOUT 44790a55fbb7Slm66018 * 44800a55fbb7Slm66018 * Return Code: 44810a55fbb7Slm66018 * 0 - Success 44820a55fbb7Slm66018 * ENXIO - incorrect buffer passed in. 4483d10e4ef2Snarayan * EFAULT - ddi_copyout routine encountered an error. 44840a55fbb7Slm66018 */ 44850a55fbb7Slm66018 static int 4486d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 44870a55fbb7Slm66018 { 4488d10e4ef2Snarayan int i; 44890a55fbb7Slm66018 void *tmp_mem = NULL; 44900a55fbb7Slm66018 void *tmp_memp; 44910a55fbb7Slm66018 struct vtoc vt; 44920a55fbb7Slm66018 struct vtoc32 vt32; 44930a55fbb7Slm66018 int copy_len = 0; 44940a55fbb7Slm66018 int rv = 0; 44950a55fbb7Slm66018 44960a55fbb7Slm66018 if (dir != VD_COPYOUT) 44970a55fbb7Slm66018 return (0); /* nothing to do */ 44980a55fbb7Slm66018 44990a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 45000a55fbb7Slm66018 return (ENXIO); 45010a55fbb7Slm66018 45020a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) 45030a55fbb7Slm66018 copy_len = sizeof (struct vtoc32); 45040a55fbb7Slm66018 else 45050a55fbb7Slm66018 copy_len = sizeof (struct vtoc); 45060a55fbb7Slm66018 45070a55fbb7Slm66018 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 45080a55fbb7Slm66018 45090a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)from, &vt); 4510d10e4ef2Snarayan 4511d10e4ef2Snarayan /* fake the VTOC timestamp field */ 4512d10e4ef2Snarayan for (i = 0; i < V_NUMPAR; i++) { 4513d10e4ef2Snarayan vt.timestamp[i] = vdc->vtoc->timestamp[i]; 4514d10e4ef2Snarayan } 4515d10e4ef2Snarayan 45160a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 45170a55fbb7Slm66018 vtoctovtoc32(vt, vt32); 45180a55fbb7Slm66018 tmp_memp = &vt32; 45190a55fbb7Slm66018 } else { 45200a55fbb7Slm66018 tmp_memp = &vt; 45210a55fbb7Slm66018 } 45220a55fbb7Slm66018 rv = ddi_copyout(tmp_memp, to, copy_len, mode); 45230a55fbb7Slm66018 if (rv != 0) 45240a55fbb7Slm66018 rv = EFAULT; 45250a55fbb7Slm66018 45260a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 45270a55fbb7Slm66018 return (rv); 45280a55fbb7Slm66018 } 45290a55fbb7Slm66018 45300a55fbb7Slm66018 /* 45310a55fbb7Slm66018 * Function: 45320a55fbb7Slm66018 * vdc_set_vtoc_convert() 45330a55fbb7Slm66018 * 45340a55fbb7Slm66018 * Description: 4535d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCSVTOC 4536d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 45370a55fbb7Slm66018 * 45380a55fbb7Slm66018 * Arguments: 4539d10e4ef2Snarayan * vdc - the vDisk client 45400a55fbb7Slm66018 * from - Buffer with data 45410a55fbb7Slm66018 * to - Buffer where data is to be copied to 45420a55fbb7Slm66018 * mode - flags passed to ioctl 45430a55fbb7Slm66018 * dir - direction of copy (in or out) 45440a55fbb7Slm66018 * 45450a55fbb7Slm66018 * Return Code: 45460a55fbb7Slm66018 * 0 - Success 45470a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 45480a55fbb7Slm66018 * EFAULT - ddi_copyin of data failed 45490a55fbb7Slm66018 */ 45500a55fbb7Slm66018 static int 4551d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 45520a55fbb7Slm66018 { 45530a55fbb7Slm66018 void *tmp_mem = NULL; 45540a55fbb7Slm66018 struct vtoc vt; 45550a55fbb7Slm66018 struct vtoc *vtp = &vt; 45560a55fbb7Slm66018 vd_vtoc_t vtvd; 45570a55fbb7Slm66018 int copy_len = 0; 45580a55fbb7Slm66018 int rv = 0; 45590a55fbb7Slm66018 45600a55fbb7Slm66018 if (dir != VD_COPYIN) 45610a55fbb7Slm66018 return (0); /* nothing to do */ 45620a55fbb7Slm66018 45630a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 45640a55fbb7Slm66018 return (ENXIO); 45650a55fbb7Slm66018 45660a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) 45670a55fbb7Slm66018 copy_len = sizeof (struct vtoc32); 45680a55fbb7Slm66018 else 45690a55fbb7Slm66018 copy_len = sizeof (struct vtoc); 45700a55fbb7Slm66018 45710a55fbb7Slm66018 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 45720a55fbb7Slm66018 45730a55fbb7Slm66018 rv = ddi_copyin(from, tmp_mem, copy_len, mode); 45740a55fbb7Slm66018 if (rv != 0) { 45750a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 45760a55fbb7Slm66018 return (EFAULT); 45770a55fbb7Slm66018 } 45780a55fbb7Slm66018 45790a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 45800a55fbb7Slm66018 vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt); 45810a55fbb7Slm66018 } else { 45820a55fbb7Slm66018 vtp = tmp_mem; 45830a55fbb7Slm66018 } 45840a55fbb7Slm66018 4585d10e4ef2Snarayan /* 4586d10e4ef2Snarayan * The VTOC is being changed, then vdc needs to update the copy 4587d10e4ef2Snarayan * it saved in the soft state structure. 4588d10e4ef2Snarayan */ 4589d10e4ef2Snarayan bcopy(vtp, vdc->vtoc, sizeof (struct vtoc)); 4590d10e4ef2Snarayan 45910a55fbb7Slm66018 VTOC2VD_VTOC(vtp, &vtvd); 45920a55fbb7Slm66018 bcopy(&vtvd, to, sizeof (vd_vtoc_t)); 45930a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 45940a55fbb7Slm66018 45950a55fbb7Slm66018 return (0); 45960a55fbb7Slm66018 } 45970a55fbb7Slm66018 45980a55fbb7Slm66018 /* 45990a55fbb7Slm66018 * Function: 46000a55fbb7Slm66018 * vdc_get_geom_convert() 46010a55fbb7Slm66018 * 46020a55fbb7Slm66018 * Description: 4603d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCGGEOM, 4604d10e4ef2Snarayan * DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format 4605d10e4ef2Snarayan * defined in FWARC 2006/195 46060a55fbb7Slm66018 * 46070a55fbb7Slm66018 * Arguments: 4608d10e4ef2Snarayan * vdc - the vDisk client 46090a55fbb7Slm66018 * from - Buffer with data 46100a55fbb7Slm66018 * to - Buffer where data is to be copied to 46110a55fbb7Slm66018 * mode - flags passed to ioctl 46120a55fbb7Slm66018 * dir - direction of copy (in or out) 46130a55fbb7Slm66018 * 46140a55fbb7Slm66018 * Return Code: 46150a55fbb7Slm66018 * 0 - Success 46160a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 4617d10e4ef2Snarayan * EFAULT - ddi_copyout of data failed 46180a55fbb7Slm66018 */ 46190a55fbb7Slm66018 static int 4620d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 46210a55fbb7Slm66018 { 4622d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 4623d10e4ef2Snarayan 46240a55fbb7Slm66018 struct dk_geom geom; 46250a55fbb7Slm66018 int copy_len = sizeof (struct dk_geom); 46260a55fbb7Slm66018 int rv = 0; 46270a55fbb7Slm66018 46280a55fbb7Slm66018 if (dir != VD_COPYOUT) 46290a55fbb7Slm66018 return (0); /* nothing to do */ 46300a55fbb7Slm66018 46310a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 46320a55fbb7Slm66018 return (ENXIO); 46330a55fbb7Slm66018 46340a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom); 46350a55fbb7Slm66018 rv = ddi_copyout(&geom, to, copy_len, mode); 46360a55fbb7Slm66018 if (rv != 0) 46370a55fbb7Slm66018 rv = EFAULT; 46380a55fbb7Slm66018 46390a55fbb7Slm66018 return (rv); 46400a55fbb7Slm66018 } 46410a55fbb7Slm66018 46420a55fbb7Slm66018 /* 46430a55fbb7Slm66018 * Function: 46440a55fbb7Slm66018 * vdc_set_geom_convert() 46450a55fbb7Slm66018 * 46460a55fbb7Slm66018 * Description: 4647d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCSGEOM 4648d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 46490a55fbb7Slm66018 * 46500a55fbb7Slm66018 * Arguments: 4651d10e4ef2Snarayan * vdc - the vDisk client 46520a55fbb7Slm66018 * from - Buffer with data 46530a55fbb7Slm66018 * to - Buffer where data is to be copied to 46540a55fbb7Slm66018 * mode - flags passed to ioctl 46550a55fbb7Slm66018 * dir - direction of copy (in or out) 46560a55fbb7Slm66018 * 46570a55fbb7Slm66018 * Return Code: 46580a55fbb7Slm66018 * 0 - Success 46590a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 46600a55fbb7Slm66018 * EFAULT - ddi_copyin of data failed 46610a55fbb7Slm66018 */ 46620a55fbb7Slm66018 static int 4663d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 46640a55fbb7Slm66018 { 4665d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 4666d10e4ef2Snarayan 46670a55fbb7Slm66018 vd_geom_t vdgeom; 46680a55fbb7Slm66018 void *tmp_mem = NULL; 46690a55fbb7Slm66018 int copy_len = sizeof (struct dk_geom); 46700a55fbb7Slm66018 int rv = 0; 46710a55fbb7Slm66018 46720a55fbb7Slm66018 if (dir != VD_COPYIN) 46730a55fbb7Slm66018 return (0); /* nothing to do */ 46740a55fbb7Slm66018 46750a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 46760a55fbb7Slm66018 return (ENXIO); 46770a55fbb7Slm66018 46780a55fbb7Slm66018 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 46790a55fbb7Slm66018 46800a55fbb7Slm66018 rv = ddi_copyin(from, tmp_mem, copy_len, mode); 46810a55fbb7Slm66018 if (rv != 0) { 46820a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 46830a55fbb7Slm66018 return (EFAULT); 46840a55fbb7Slm66018 } 46850a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom); 46860a55fbb7Slm66018 bcopy(&vdgeom, to, sizeof (vdgeom)); 46870a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 46880a55fbb7Slm66018 46890a55fbb7Slm66018 return (0); 46900a55fbb7Slm66018 } 46910a55fbb7Slm66018 46924bac2208Snarayan static int 46934bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 46944bac2208Snarayan { 46954bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 46964bac2208Snarayan 46974bac2208Snarayan vd_efi_t *vd_efi; 46984bac2208Snarayan dk_efi_t dk_efi; 46994bac2208Snarayan int rv = 0; 47004bac2208Snarayan void *uaddr; 47014bac2208Snarayan 47024bac2208Snarayan if ((from == NULL) || (to == NULL)) 47034bac2208Snarayan return (ENXIO); 47044bac2208Snarayan 47054bac2208Snarayan if (dir == VD_COPYIN) { 47064bac2208Snarayan 47074bac2208Snarayan vd_efi = (vd_efi_t *)to; 47084bac2208Snarayan 47094bac2208Snarayan rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode); 47104bac2208Snarayan if (rv != 0) 47114bac2208Snarayan return (EFAULT); 47124bac2208Snarayan 47134bac2208Snarayan vd_efi->lba = dk_efi.dki_lba; 47144bac2208Snarayan vd_efi->length = dk_efi.dki_length; 47154bac2208Snarayan bzero(vd_efi->data, vd_efi->length); 47164bac2208Snarayan 47174bac2208Snarayan } else { 47184bac2208Snarayan 47194bac2208Snarayan rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode); 47204bac2208Snarayan if (rv != 0) 47214bac2208Snarayan return (EFAULT); 47224bac2208Snarayan 47234bac2208Snarayan uaddr = dk_efi.dki_data; 47244bac2208Snarayan 47254bac2208Snarayan dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP); 47264bac2208Snarayan 47274bac2208Snarayan VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi); 47284bac2208Snarayan 47294bac2208Snarayan rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length, 47304bac2208Snarayan mode); 47314bac2208Snarayan if (rv != 0) 47324bac2208Snarayan return (EFAULT); 47334bac2208Snarayan 47344bac2208Snarayan kmem_free(dk_efi.dki_data, dk_efi.dki_length); 47354bac2208Snarayan } 47364bac2208Snarayan 47374bac2208Snarayan return (0); 47384bac2208Snarayan } 47394bac2208Snarayan 47404bac2208Snarayan static int 47414bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 47424bac2208Snarayan { 47434bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 47444bac2208Snarayan 47454bac2208Snarayan dk_efi_t dk_efi; 47464bac2208Snarayan void *uaddr; 47474bac2208Snarayan 47484bac2208Snarayan if (dir == VD_COPYOUT) 47494bac2208Snarayan return (0); /* nothing to do */ 47504bac2208Snarayan 47514bac2208Snarayan if ((from == NULL) || (to == NULL)) 47524bac2208Snarayan return (ENXIO); 47534bac2208Snarayan 47544bac2208Snarayan if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0) 47554bac2208Snarayan return (EFAULT); 47564bac2208Snarayan 47574bac2208Snarayan uaddr = dk_efi.dki_data; 47584bac2208Snarayan 47594bac2208Snarayan dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP); 47604bac2208Snarayan 47614bac2208Snarayan if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0) 47624bac2208Snarayan return (EFAULT); 47634bac2208Snarayan 47644bac2208Snarayan DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to); 47654bac2208Snarayan 47664bac2208Snarayan kmem_free(dk_efi.dki_data, dk_efi.dki_length); 47674bac2208Snarayan 47684bac2208Snarayan return (0); 47694bac2208Snarayan } 47704bac2208Snarayan 47710a55fbb7Slm66018 /* 47720a55fbb7Slm66018 * Function: 47731ae08745Sheppo * vdc_create_fake_geometry() 47741ae08745Sheppo * 47751ae08745Sheppo * Description: 47761ae08745Sheppo * This routine fakes up the disk info needed for some DKIO ioctls. 47771ae08745Sheppo * - DKIOCINFO 47781ae08745Sheppo * - DKIOCGMEDIAINFO 47791ae08745Sheppo * 47801ae08745Sheppo * [ just like lofi(7D) and ramdisk(7D) ] 47811ae08745Sheppo * 47821ae08745Sheppo * Arguments: 47831ae08745Sheppo * vdc - soft state pointer for this instance of the device driver. 47841ae08745Sheppo * 47851ae08745Sheppo * Return Code: 47861ae08745Sheppo * 0 - Success 47871ae08745Sheppo */ 47881ae08745Sheppo static int 47891ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc) 47901ae08745Sheppo { 47911ae08745Sheppo ASSERT(vdc != NULL); 47921ae08745Sheppo 47931ae08745Sheppo /* 47940d0c8d4bSnarayan * Check if max_xfer_sz and vdisk_size are valid 47950d0c8d4bSnarayan */ 47960d0c8d4bSnarayan if (vdc->vdisk_size == 0 || vdc->max_xfer_sz == 0) 47970d0c8d4bSnarayan return (EIO); 47980d0c8d4bSnarayan 47990d0c8d4bSnarayan /* 48001ae08745Sheppo * DKIOCINFO support 48011ae08745Sheppo */ 48021ae08745Sheppo vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 48031ae08745Sheppo 48041ae08745Sheppo (void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME); 48051ae08745Sheppo (void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME); 48068e6a2a04Slm66018 /* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */ 48078e6a2a04Slm66018 vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz; 48081ae08745Sheppo vdc->cinfo->dki_ctype = DKC_SCSI_CCS; 48091ae08745Sheppo vdc->cinfo->dki_flags = DKI_FMTVOL; 48101ae08745Sheppo vdc->cinfo->dki_cnum = 0; 48111ae08745Sheppo vdc->cinfo->dki_addr = 0; 48121ae08745Sheppo vdc->cinfo->dki_space = 0; 48131ae08745Sheppo vdc->cinfo->dki_prio = 0; 48141ae08745Sheppo vdc->cinfo->dki_vec = 0; 48151ae08745Sheppo vdc->cinfo->dki_unit = vdc->instance; 48161ae08745Sheppo vdc->cinfo->dki_slave = 0; 48171ae08745Sheppo /* 48181ae08745Sheppo * The partition number will be created on the fly depending on the 48191ae08745Sheppo * actual slice (i.e. minor node) that is used to request the data. 48201ae08745Sheppo */ 48211ae08745Sheppo vdc->cinfo->dki_partition = 0; 48221ae08745Sheppo 48231ae08745Sheppo /* 48241ae08745Sheppo * DKIOCGMEDIAINFO support 48251ae08745Sheppo */ 48260a55fbb7Slm66018 if (vdc->minfo == NULL) 48271ae08745Sheppo vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP); 48281ae08745Sheppo vdc->minfo->dki_media_type = DK_FIXED_DISK; 48294bac2208Snarayan vdc->minfo->dki_capacity = vdc->vdisk_size; 48301ae08745Sheppo vdc->minfo->dki_lbsize = DEV_BSIZE; 48311ae08745Sheppo 48320d0c8d4bSnarayan return (0); 48330a55fbb7Slm66018 } 48340a55fbb7Slm66018 48350a55fbb7Slm66018 /* 48360a55fbb7Slm66018 * Function: 48370a55fbb7Slm66018 * vdc_setup_disk_layout() 48380a55fbb7Slm66018 * 48390a55fbb7Slm66018 * Description: 48400a55fbb7Slm66018 * This routine discovers all the necessary details about the "disk" 48410a55fbb7Slm66018 * by requesting the data that is available from the vDisk server and by 48420a55fbb7Slm66018 * faking up the rest of the data. 48430a55fbb7Slm66018 * 48440a55fbb7Slm66018 * Arguments: 48450a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 48460a55fbb7Slm66018 * 48470a55fbb7Slm66018 * Return Code: 48480a55fbb7Slm66018 * 0 - Success 48490a55fbb7Slm66018 */ 48500a55fbb7Slm66018 static int 48510a55fbb7Slm66018 vdc_setup_disk_layout(vdc_t *vdc) 48520a55fbb7Slm66018 { 4853d10e4ef2Snarayan buf_t *buf; /* BREAD requests need to be in a buf_t structure */ 48540a55fbb7Slm66018 dev_t dev; 48550a55fbb7Slm66018 int slice = 0; 48560d0c8d4bSnarayan int rv, error; 48570a55fbb7Slm66018 48580a55fbb7Slm66018 ASSERT(vdc != NULL); 48590a55fbb7Slm66018 48600a55fbb7Slm66018 if (vdc->vtoc == NULL) 48610a55fbb7Slm66018 vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP); 48620a55fbb7Slm66018 48630a55fbb7Slm66018 dev = makedevice(ddi_driver_major(vdc->dip), 48640a55fbb7Slm66018 VD_MAKE_DEV(vdc->instance, 0)); 48650a55fbb7Slm66018 rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)vdc->vtoc, FKIOCTL); 48664bac2208Snarayan 48674bac2208Snarayan if (rv && rv != ENOTSUP) { 48683af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)", 48690a55fbb7Slm66018 vdc->instance, rv); 48700a55fbb7Slm66018 return (rv); 48710a55fbb7Slm66018 } 48720a55fbb7Slm66018 48730d0c8d4bSnarayan /* 48740d0c8d4bSnarayan * The process of attempting to read VTOC will initiate 48750d0c8d4bSnarayan * the handshake and establish a connection. Following 48760d0c8d4bSnarayan * handshake, go ahead and create geometry. 48770d0c8d4bSnarayan */ 48780d0c8d4bSnarayan error = vdc_create_fake_geometry(vdc); 48790d0c8d4bSnarayan if (error != 0) { 48800d0c8d4bSnarayan DMSG(vdc, 0, "[%d] Failed to create disk geometry (err%d)", 48810d0c8d4bSnarayan vdc->instance, error); 48820d0c8d4bSnarayan return (error); 48830d0c8d4bSnarayan } 48840d0c8d4bSnarayan 48854bac2208Snarayan if (rv == ENOTSUP) { 48864bac2208Snarayan /* 48874bac2208Snarayan * If the device does not support VTOC then we try 48884bac2208Snarayan * to read an EFI label. 48894bac2208Snarayan */ 48904bac2208Snarayan struct dk_gpt *efi; 48914bac2208Snarayan size_t efi_len; 48924bac2208Snarayan 48934bac2208Snarayan rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len); 48944bac2208Snarayan 48954bac2208Snarayan if (rv) { 48963af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)", 48974bac2208Snarayan vdc->instance, rv); 48984bac2208Snarayan return (rv); 48994bac2208Snarayan } 49004bac2208Snarayan 49014bac2208Snarayan vdc->vdisk_label = VD_DISK_LABEL_EFI; 49024bac2208Snarayan vdc_store_efi(vdc, efi); 49034bac2208Snarayan vd_efi_free(efi, efi_len); 49044bac2208Snarayan 49054bac2208Snarayan return (0); 49064bac2208Snarayan } 49074bac2208Snarayan 49084bac2208Snarayan vdc->vdisk_label = VD_DISK_LABEL_VTOC; 49094bac2208Snarayan 49100a55fbb7Slm66018 /* 49113af08d82Slm66018 * FUTURE: This could be default way for reading the VTOC 49123af08d82Slm66018 * from the disk as supposed to sending the VD_OP_GET_VTOC 49133af08d82Slm66018 * to the server. Currently this is a sanity check. 49143af08d82Slm66018 * 49150a55fbb7Slm66018 * find the slice that represents the entire "disk" and use that to 49160a55fbb7Slm66018 * read the disk label. The convention in Solaris is that slice 2 4917d10e4ef2Snarayan * represents the whole disk so we check that it is, otherwise we 49180a55fbb7Slm66018 * default to slice 0 49190a55fbb7Slm66018 */ 49200a55fbb7Slm66018 if ((vdc->vdisk_type == VD_DISK_TYPE_DISK) && 49210a55fbb7Slm66018 (vdc->vtoc->v_part[2].p_tag == V_BACKUP)) { 49220a55fbb7Slm66018 slice = 2; 49230a55fbb7Slm66018 } else { 49240a55fbb7Slm66018 slice = 0; 49250a55fbb7Slm66018 } 4926d10e4ef2Snarayan 4927d10e4ef2Snarayan /* 4928d10e4ef2Snarayan * Read disk label from start of disk 4929d10e4ef2Snarayan */ 4930d10e4ef2Snarayan vdc->label = kmem_zalloc(DK_LABEL_SIZE, KM_SLEEP); 4931d10e4ef2Snarayan buf = kmem_alloc(sizeof (buf_t), KM_SLEEP); 4932d10e4ef2Snarayan bioinit(buf); 4933d10e4ef2Snarayan buf->b_un.b_addr = (caddr_t)vdc->label; 4934d10e4ef2Snarayan buf->b_bcount = DK_LABEL_SIZE; 4935d10e4ef2Snarayan buf->b_flags = B_BUSY | B_READ; 4936d10e4ef2Snarayan buf->b_dev = dev; 49373af08d82Slm66018 rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)vdc->label, 49383af08d82Slm66018 DK_LABEL_SIZE, slice, 0, CB_STRATEGY, buf, VIO_read_dir); 49393af08d82Slm66018 if (rv) { 49403af08d82Slm66018 DMSG(vdc, 1, "[%d] Failed to read disk block 0\n", 49413af08d82Slm66018 vdc->instance); 49423af08d82Slm66018 kmem_free(buf, sizeof (buf_t)); 49433af08d82Slm66018 return (rv); 49443af08d82Slm66018 } 4945d10e4ef2Snarayan rv = biowait(buf); 4946d10e4ef2Snarayan biofini(buf); 4947d10e4ef2Snarayan kmem_free(buf, sizeof (buf_t)); 49480a55fbb7Slm66018 49490a55fbb7Slm66018 return (rv); 49501ae08745Sheppo } 49514bac2208Snarayan 49524bac2208Snarayan /* 49534bac2208Snarayan * Function: 49544bac2208Snarayan * vdc_setup_devid() 49554bac2208Snarayan * 49564bac2208Snarayan * Description: 49574bac2208Snarayan * This routine discovers the devid of a vDisk. It requests the devid of 49584bac2208Snarayan * the underlying device from the vDisk server, builds an encapsulated 49594bac2208Snarayan * devid based on the retrieved devid and registers that new devid to 49604bac2208Snarayan * the vDisk. 49614bac2208Snarayan * 49624bac2208Snarayan * Arguments: 49634bac2208Snarayan * vdc - soft state pointer for this instance of the device driver. 49644bac2208Snarayan * 49654bac2208Snarayan * Return Code: 49664bac2208Snarayan * 0 - A devid was succesfully registered for the vDisk 49674bac2208Snarayan */ 49684bac2208Snarayan static int 49694bac2208Snarayan vdc_setup_devid(vdc_t *vdc) 49704bac2208Snarayan { 49714bac2208Snarayan int rv; 49724bac2208Snarayan vd_devid_t *vd_devid; 49734bac2208Snarayan size_t bufsize, bufid_len; 49744bac2208Snarayan 49754bac2208Snarayan /* 49764bac2208Snarayan * At first sight, we don't know the size of the devid that the 49774bac2208Snarayan * server will return but this size will be encoded into the 49784bac2208Snarayan * reply. So we do a first request using a default size then we 49794bac2208Snarayan * check if this size was large enough. If not then we do a second 49804bac2208Snarayan * request with the correct size returned by the server. Note that 49814bac2208Snarayan * ldc requires size to be 8-byte aligned. 49824bac2208Snarayan */ 49834bac2208Snarayan bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN), 49844bac2208Snarayan sizeof (uint64_t)); 49854bac2208Snarayan vd_devid = kmem_zalloc(bufsize, KM_SLEEP); 49864bac2208Snarayan bufid_len = bufsize - sizeof (vd_efi_t) - 1; 49874bac2208Snarayan 49883af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid, 49893af08d82Slm66018 bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir); 49903af08d82Slm66018 49913af08d82Slm66018 DMSG(vdc, 2, "sync_op returned %d\n", rv); 49923af08d82Slm66018 49934bac2208Snarayan if (rv) { 49944bac2208Snarayan kmem_free(vd_devid, bufsize); 49954bac2208Snarayan return (rv); 49964bac2208Snarayan } 49974bac2208Snarayan 49984bac2208Snarayan if (vd_devid->length > bufid_len) { 49994bac2208Snarayan /* 50004bac2208Snarayan * The returned devid is larger than the buffer used. Try again 50014bac2208Snarayan * with a buffer with the right size. 50024bac2208Snarayan */ 50034bac2208Snarayan kmem_free(vd_devid, bufsize); 50044bac2208Snarayan bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length), 50054bac2208Snarayan sizeof (uint64_t)); 50064bac2208Snarayan vd_devid = kmem_zalloc(bufsize, KM_SLEEP); 50074bac2208Snarayan bufid_len = bufsize - sizeof (vd_efi_t) - 1; 50084bac2208Snarayan 50093af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, 50103af08d82Slm66018 (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0, 50113af08d82Slm66018 VIO_both_dir); 50123af08d82Slm66018 50134bac2208Snarayan if (rv) { 50144bac2208Snarayan kmem_free(vd_devid, bufsize); 50154bac2208Snarayan return (rv); 50164bac2208Snarayan } 50174bac2208Snarayan } 50184bac2208Snarayan 50194bac2208Snarayan /* 50204bac2208Snarayan * The virtual disk should have the same device id as the one associated 50214bac2208Snarayan * with the physical disk it is mapped on, otherwise sharing a disk 50224bac2208Snarayan * between a LDom and a non-LDom may not work (for example for a shared 50234bac2208Snarayan * SVM disk set). 50244bac2208Snarayan * 50254bac2208Snarayan * The DDI framework does not allow creating a device id with any 50264bac2208Snarayan * type so we first create a device id of type DEVID_ENCAP and then 50274bac2208Snarayan * we restore the orignal type of the physical device. 50284bac2208Snarayan */ 50294bac2208Snarayan 50303af08d82Slm66018 DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length); 50313af08d82Slm66018 50324bac2208Snarayan /* build an encapsulated devid based on the returned devid */ 50334bac2208Snarayan if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length, 50344bac2208Snarayan vd_devid->id, &vdc->devid) != DDI_SUCCESS) { 50353af08d82Slm66018 DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance); 50364bac2208Snarayan kmem_free(vd_devid, bufsize); 50374bac2208Snarayan return (1); 50384bac2208Snarayan } 50394bac2208Snarayan 50404bac2208Snarayan DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type); 50414bac2208Snarayan 50424bac2208Snarayan ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS); 50434bac2208Snarayan 50444bac2208Snarayan kmem_free(vd_devid, bufsize); 50454bac2208Snarayan 50464bac2208Snarayan if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) { 50473af08d82Slm66018 DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance); 50484bac2208Snarayan return (1); 50494bac2208Snarayan } 50504bac2208Snarayan 50514bac2208Snarayan return (0); 50524bac2208Snarayan } 50534bac2208Snarayan 50544bac2208Snarayan static void 50554bac2208Snarayan vdc_store_efi(vdc_t *vdc, struct dk_gpt *efi) 50564bac2208Snarayan { 50574bac2208Snarayan struct vtoc *vtoc = vdc->vtoc; 50584bac2208Snarayan 50594bac2208Snarayan vd_efi_to_vtoc(efi, vtoc); 50604bac2208Snarayan if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) { 50614bac2208Snarayan /* 50624bac2208Snarayan * vd_efi_to_vtoc() will store information about the EFI Sun 50634bac2208Snarayan * reserved partition (representing the entire disk) into 50644bac2208Snarayan * partition 7. However single-slice device will only have 50654bac2208Snarayan * that single partition and the vdc driver expects to find 50664bac2208Snarayan * information about that partition in slice 0. So we need 50674bac2208Snarayan * to copy information from slice 7 to slice 0. 50684bac2208Snarayan */ 50694bac2208Snarayan vtoc->v_part[0].p_tag = vtoc->v_part[VD_EFI_WD_SLICE].p_tag; 50704bac2208Snarayan vtoc->v_part[0].p_flag = vtoc->v_part[VD_EFI_WD_SLICE].p_flag; 50714bac2208Snarayan vtoc->v_part[0].p_start = vtoc->v_part[VD_EFI_WD_SLICE].p_start; 50724bac2208Snarayan vtoc->v_part[0].p_size = vtoc->v_part[VD_EFI_WD_SLICE].p_size; 50734bac2208Snarayan } 50744bac2208Snarayan } 5075