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 /* 236567eb0aSlh195018 * 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> 8487a7269eSachartre #include <sys/dktp/dadkio.h> 851ae08745Sheppo #include <sys/scsi/generic/sense.h> 861ae08745Sheppo #include <sys/scsi/impl/uscsi.h> /* Needed for defn of USCSICMD ioctl */ 871ae08745Sheppo 881ae08745Sheppo #include <sys/ldoms.h> 891ae08745Sheppo #include <sys/ldc.h> 901ae08745Sheppo #include <sys/vio_common.h> 911ae08745Sheppo #include <sys/vio_mailbox.h> 92*17cadca8Slm66018 #include <sys/vio_util.h> 931ae08745Sheppo #include <sys/vdsk_common.h> 941ae08745Sheppo #include <sys/vdsk_mailbox.h> 951ae08745Sheppo #include <sys/vdc.h> 961ae08745Sheppo 971ae08745Sheppo /* 981ae08745Sheppo * function prototypes 991ae08745Sheppo */ 1001ae08745Sheppo 1011ae08745Sheppo /* standard driver functions */ 1021ae08745Sheppo static int vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred); 1031ae08745Sheppo static int vdc_close(dev_t dev, int flag, int otyp, cred_t *cred); 1041ae08745Sheppo static int vdc_strategy(struct buf *buf); 1051ae08745Sheppo static int vdc_print(dev_t dev, char *str); 1061ae08745Sheppo static int vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1071ae08745Sheppo static int vdc_read(dev_t dev, struct uio *uio, cred_t *cred); 1081ae08745Sheppo static int vdc_write(dev_t dev, struct uio *uio, cred_t *cred); 1091ae08745Sheppo static int vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 1101ae08745Sheppo cred_t *credp, int *rvalp); 1111ae08745Sheppo static int vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred); 1121ae08745Sheppo static int vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred); 1131ae08745Sheppo 1141ae08745Sheppo static int vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, 1151ae08745Sheppo void *arg, void **resultp); 1161ae08745Sheppo static int vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 1171ae08745Sheppo static int vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 1181ae08745Sheppo 1191ae08745Sheppo /* setup */ 1200d0c8d4bSnarayan static void vdc_min(struct buf *bufp); 1210a55fbb7Slm66018 static int vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen); 122655fd6a9Sachartre static int vdc_do_ldc_init(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_node); 1231ae08745Sheppo static int vdc_start_ldc_connection(vdc_t *vdc); 1241ae08745Sheppo static int vdc_create_device_nodes(vdc_t *vdc); 1254bac2208Snarayan static int vdc_create_device_nodes_efi(vdc_t *vdc); 1264bac2208Snarayan static int vdc_create_device_nodes_vtoc(vdc_t *vdc); 1271ae08745Sheppo static int vdc_create_device_nodes_props(vdc_t *vdc); 128655fd6a9Sachartre static int vdc_get_md_node(dev_info_t *dip, md_t **mdpp, 129655fd6a9Sachartre mde_cookie_t *vd_nodep, mde_cookie_t *vd_portp); 130655fd6a9Sachartre static int vdc_get_ldc_id(md_t *, mde_cookie_t, uint64_t *); 1310a55fbb7Slm66018 static int vdc_do_ldc_up(vdc_t *vdc); 1321ae08745Sheppo static void vdc_terminate_ldc(vdc_t *vdc); 1331ae08745Sheppo static int vdc_init_descriptor_ring(vdc_t *vdc); 1341ae08745Sheppo static void vdc_destroy_descriptor_ring(vdc_t *vdc); 1354bac2208Snarayan static int vdc_setup_devid(vdc_t *vdc); 13678fcd0a1Sachartre static void vdc_store_label_efi(vdc_t *vdc, struct dk_gpt *efi); 13778fcd0a1Sachartre static void vdc_store_label_vtoc(vdc_t *, struct dk_geom *, struct vtoc *); 13878fcd0a1Sachartre static void vdc_store_label_unk(vdc_t *vdc); 13978fcd0a1Sachartre static boolean_t vdc_is_opened(vdc_t *vdc); 1401ae08745Sheppo 1411ae08745Sheppo /* handshake with vds */ 1420a55fbb7Slm66018 static int vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver); 1433af08d82Slm66018 static int vdc_ver_negotiation(vdc_t *vdcp); 1441ae08745Sheppo static int vdc_init_attr_negotiation(vdc_t *vdc); 1453af08d82Slm66018 static int vdc_attr_negotiation(vdc_t *vdcp); 1461ae08745Sheppo static int vdc_init_dring_negotiate(vdc_t *vdc); 1473af08d82Slm66018 static int vdc_dring_negotiation(vdc_t *vdcp); 1483af08d82Slm66018 static int vdc_send_rdx(vdc_t *vdcp); 1493af08d82Slm66018 static int vdc_rdx_exchange(vdc_t *vdcp); 1500a55fbb7Slm66018 static boolean_t vdc_is_supported_version(vio_ver_msg_t *ver_msg); 1511ae08745Sheppo 1520a55fbb7Slm66018 /* processing incoming messages from vDisk server */ 1531ae08745Sheppo static void vdc_process_msg_thread(vdc_t *vdc); 1543af08d82Slm66018 static int vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp); 1553af08d82Slm66018 1560a55fbb7Slm66018 static uint_t vdc_handle_cb(uint64_t event, caddr_t arg); 1573af08d82Slm66018 static int vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg); 1580a55fbb7Slm66018 static int vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg); 1590a55fbb7Slm66018 static int vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg); 1600a55fbb7Slm66018 static int vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg); 1613af08d82Slm66018 static int vdc_send_request(vdc_t *vdcp, int operation, 1623af08d82Slm66018 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 1633af08d82Slm66018 int cb_type, void *cb_arg, vio_desc_direction_t dir); 1643af08d82Slm66018 static int vdc_map_to_shared_dring(vdc_t *vdcp, int idx); 1653af08d82Slm66018 static int vdc_populate_descriptor(vdc_t *vdcp, int operation, 1663af08d82Slm66018 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 1673af08d82Slm66018 int cb_type, void *cb_arg, vio_desc_direction_t dir); 1683af08d82Slm66018 static int vdc_do_sync_op(vdc_t *vdcp, int operation, 1693af08d82Slm66018 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 1703af08d82Slm66018 int cb_type, void *cb_arg, vio_desc_direction_t dir); 1713af08d82Slm66018 1723af08d82Slm66018 static int vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp); 1733af08d82Slm66018 static int vdc_drain_response(vdc_t *vdcp); 1741ae08745Sheppo static int vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx); 1753af08d82Slm66018 static int vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep); 176e1ebb9ecSlm66018 static int vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg); 1771ae08745Sheppo 1781ae08745Sheppo /* dkio */ 1791ae08745Sheppo static int vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode); 18078fcd0a1Sachartre static void vdc_create_fake_geometry(vdc_t *vdc); 18178fcd0a1Sachartre static int vdc_validate_geometry(vdc_t *vdc); 18278fcd0a1Sachartre static void vdc_validate(vdc_t *vdc); 18378fcd0a1Sachartre static void vdc_validate_task(void *arg); 184d10e4ef2Snarayan static int vdc_null_copy_func(vdc_t *vdc, void *from, void *to, 185d10e4ef2Snarayan int mode, int dir); 1864bac2208Snarayan static int vdc_get_wce_convert(vdc_t *vdc, void *from, void *to, 1874bac2208Snarayan int mode, int dir); 1884bac2208Snarayan static int vdc_set_wce_convert(vdc_t *vdc, void *from, void *to, 1894bac2208Snarayan int mode, int dir); 190d10e4ef2Snarayan static int vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, 191d10e4ef2Snarayan int mode, int dir); 192d10e4ef2Snarayan static int vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, 193d10e4ef2Snarayan int mode, int dir); 194d10e4ef2Snarayan static int vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, 195d10e4ef2Snarayan int mode, int dir); 196d10e4ef2Snarayan static int vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, 197d10e4ef2Snarayan int mode, int dir); 1984bac2208Snarayan static int vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, 1994bac2208Snarayan int mode, int dir); 2004bac2208Snarayan static int vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, 2014bac2208Snarayan int mode, int dir); 2021ae08745Sheppo 2031ae08745Sheppo /* 2041ae08745Sheppo * Module variables 2051ae08745Sheppo */ 206e1ebb9ecSlm66018 207e1ebb9ecSlm66018 /* 208e1ebb9ecSlm66018 * Tunable variables to control how long vdc waits before timing out on 209e1ebb9ecSlm66018 * various operations 210e1ebb9ecSlm66018 */ 2113c96341aSnarayan static int vdc_hshake_retries = 3; 212e1ebb9ecSlm66018 213655fd6a9Sachartre static int vdc_timeout = 0; /* units: seconds */ 214655fd6a9Sachartre 2153af08d82Slm66018 static uint64_t vdc_hz_min_ldc_delay; 2163af08d82Slm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC; 2173af08d82Slm66018 static uint64_t vdc_hz_max_ldc_delay; 2183af08d82Slm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC; 2193af08d82Slm66018 2203af08d82Slm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC; 2213af08d82Slm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC; 222e1ebb9ecSlm66018 223e1ebb9ecSlm66018 /* values for dumping - need to run in a tighter loop */ 224e1ebb9ecSlm66018 static uint64_t vdc_usec_timeout_dump = 100 * MILLISEC; /* 0.1s units: ns */ 225e1ebb9ecSlm66018 static int vdc_dump_retries = 100; 226e1ebb9ecSlm66018 227e1ebb9ecSlm66018 /* Count of the number of vdc instances attached */ 228e1ebb9ecSlm66018 static volatile uint32_t vdc_instance_count = 0; 2291ae08745Sheppo 2301ae08745Sheppo /* Soft state pointer */ 2311ae08745Sheppo static void *vdc_state; 2321ae08745Sheppo 2333af08d82Slm66018 /* 2343af08d82Slm66018 * Controlling the verbosity of the error/debug messages 2353af08d82Slm66018 * 2363af08d82Slm66018 * vdc_msglevel - controls level of messages 2373af08d82Slm66018 * vdc_matchinst - 64-bit variable where each bit corresponds 2383af08d82Slm66018 * to the vdc instance the vdc_msglevel applies. 2393af08d82Slm66018 */ 2403af08d82Slm66018 int vdc_msglevel = 0x0; 2413af08d82Slm66018 uint64_t vdc_matchinst = 0ull; 2421ae08745Sheppo 2430a55fbb7Slm66018 /* 2440a55fbb7Slm66018 * Supported vDisk protocol version pairs. 2450a55fbb7Slm66018 * 2460a55fbb7Slm66018 * The first array entry is the latest and preferred version. 2470a55fbb7Slm66018 */ 248*17cadca8Slm66018 static const vio_ver_t vdc_version[] = {{1, 1}}; 2491ae08745Sheppo 2501ae08745Sheppo static struct cb_ops vdc_cb_ops = { 2511ae08745Sheppo vdc_open, /* cb_open */ 2521ae08745Sheppo vdc_close, /* cb_close */ 2531ae08745Sheppo vdc_strategy, /* cb_strategy */ 2541ae08745Sheppo vdc_print, /* cb_print */ 2551ae08745Sheppo vdc_dump, /* cb_dump */ 2561ae08745Sheppo vdc_read, /* cb_read */ 2571ae08745Sheppo vdc_write, /* cb_write */ 2581ae08745Sheppo vdc_ioctl, /* cb_ioctl */ 2591ae08745Sheppo nodev, /* cb_devmap */ 2601ae08745Sheppo nodev, /* cb_mmap */ 2611ae08745Sheppo nodev, /* cb_segmap */ 2621ae08745Sheppo nochpoll, /* cb_chpoll */ 2631ae08745Sheppo ddi_prop_op, /* cb_prop_op */ 2641ae08745Sheppo NULL, /* cb_str */ 2651ae08745Sheppo D_MP | D_64BIT, /* cb_flag */ 2661ae08745Sheppo CB_REV, /* cb_rev */ 2671ae08745Sheppo vdc_aread, /* cb_aread */ 2681ae08745Sheppo vdc_awrite /* cb_awrite */ 2691ae08745Sheppo }; 2701ae08745Sheppo 2711ae08745Sheppo static struct dev_ops vdc_ops = { 2721ae08745Sheppo DEVO_REV, /* devo_rev */ 2731ae08745Sheppo 0, /* devo_refcnt */ 2741ae08745Sheppo vdc_getinfo, /* devo_getinfo */ 2751ae08745Sheppo nulldev, /* devo_identify */ 2761ae08745Sheppo nulldev, /* devo_probe */ 2771ae08745Sheppo vdc_attach, /* devo_attach */ 2781ae08745Sheppo vdc_detach, /* devo_detach */ 2791ae08745Sheppo nodev, /* devo_reset */ 2801ae08745Sheppo &vdc_cb_ops, /* devo_cb_ops */ 2811ae08745Sheppo NULL, /* devo_bus_ops */ 2821ae08745Sheppo nulldev /* devo_power */ 2831ae08745Sheppo }; 2841ae08745Sheppo 2851ae08745Sheppo static struct modldrv modldrv = { 2861ae08745Sheppo &mod_driverops, 287205eeb1aSlm66018 "virtual disk client", 2881ae08745Sheppo &vdc_ops, 2891ae08745Sheppo }; 2901ae08745Sheppo 2911ae08745Sheppo static struct modlinkage modlinkage = { 2921ae08745Sheppo MODREV_1, 2931ae08745Sheppo &modldrv, 2941ae08745Sheppo NULL 2951ae08745Sheppo }; 2961ae08745Sheppo 2971ae08745Sheppo /* -------------------------------------------------------------------------- */ 2981ae08745Sheppo 2991ae08745Sheppo /* 3001ae08745Sheppo * Device Driver housekeeping and setup 3011ae08745Sheppo */ 3021ae08745Sheppo 3031ae08745Sheppo int 3041ae08745Sheppo _init(void) 3051ae08745Sheppo { 3061ae08745Sheppo int status; 3071ae08745Sheppo 3081ae08745Sheppo if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0) 3091ae08745Sheppo return (status); 3101ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) 3111ae08745Sheppo ddi_soft_state_fini(&vdc_state); 3124bac2208Snarayan vdc_efi_init(vd_process_ioctl); 3131ae08745Sheppo return (status); 3141ae08745Sheppo } 3151ae08745Sheppo 3161ae08745Sheppo int 3171ae08745Sheppo _info(struct modinfo *modinfop) 3181ae08745Sheppo { 3191ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 3201ae08745Sheppo } 3211ae08745Sheppo 3221ae08745Sheppo int 3231ae08745Sheppo _fini(void) 3241ae08745Sheppo { 3251ae08745Sheppo int status; 3261ae08745Sheppo 3271ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 3281ae08745Sheppo return (status); 3294bac2208Snarayan vdc_efi_fini(); 3301ae08745Sheppo ddi_soft_state_fini(&vdc_state); 3311ae08745Sheppo return (0); 3321ae08745Sheppo } 3331ae08745Sheppo 3341ae08745Sheppo static int 3351ae08745Sheppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 3361ae08745Sheppo { 3371ae08745Sheppo _NOTE(ARGUNUSED(dip)) 3381ae08745Sheppo 3390d0c8d4bSnarayan int instance = VDCUNIT((dev_t)arg); 3401ae08745Sheppo vdc_t *vdc = NULL; 3411ae08745Sheppo 3421ae08745Sheppo switch (cmd) { 3431ae08745Sheppo case DDI_INFO_DEVT2DEVINFO: 3441ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 3451ae08745Sheppo *resultp = NULL; 3461ae08745Sheppo return (DDI_FAILURE); 3471ae08745Sheppo } 3481ae08745Sheppo *resultp = vdc->dip; 3491ae08745Sheppo return (DDI_SUCCESS); 3501ae08745Sheppo case DDI_INFO_DEVT2INSTANCE: 3511ae08745Sheppo *resultp = (void *)(uintptr_t)instance; 3521ae08745Sheppo return (DDI_SUCCESS); 3531ae08745Sheppo default: 3541ae08745Sheppo *resultp = NULL; 3551ae08745Sheppo return (DDI_FAILURE); 3561ae08745Sheppo } 3571ae08745Sheppo } 3581ae08745Sheppo 3591ae08745Sheppo static int 3601ae08745Sheppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3611ae08745Sheppo { 3621ae08745Sheppo int instance; 3631ae08745Sheppo int rv; 3641ae08745Sheppo vdc_t *vdc = NULL; 3651ae08745Sheppo 3661ae08745Sheppo switch (cmd) { 3671ae08745Sheppo case DDI_DETACH: 3681ae08745Sheppo /* the real work happens below */ 3691ae08745Sheppo break; 3701ae08745Sheppo case DDI_SUSPEND: 3711ae08745Sheppo /* nothing to do for this non-device */ 3721ae08745Sheppo return (DDI_SUCCESS); 3731ae08745Sheppo default: 3741ae08745Sheppo return (DDI_FAILURE); 3751ae08745Sheppo } 3761ae08745Sheppo 3771ae08745Sheppo ASSERT(cmd == DDI_DETACH); 3781ae08745Sheppo instance = ddi_get_instance(dip); 3793af08d82Slm66018 DMSGX(1, "[%d] Entered\n", instance); 3801ae08745Sheppo 3811ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 382e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 3831ae08745Sheppo return (DDI_FAILURE); 3841ae08745Sheppo } 3851ae08745Sheppo 38678fcd0a1Sachartre if (vdc_is_opened(vdc)) { 3873af08d82Slm66018 DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance); 3881ae08745Sheppo return (DDI_FAILURE); 3891ae08745Sheppo } 3901ae08745Sheppo 39178fcd0a1Sachartre if (vdc->dkio_flush_pending) { 39278fcd0a1Sachartre DMSG(vdc, 0, 39378fcd0a1Sachartre "[%d] Cannot detach: %d outstanding DKIO flushes\n", 39478fcd0a1Sachartre instance, vdc->dkio_flush_pending); 39578fcd0a1Sachartre return (DDI_FAILURE); 39678fcd0a1Sachartre } 39778fcd0a1Sachartre 39878fcd0a1Sachartre if (vdc->validate_pending) { 39978fcd0a1Sachartre DMSG(vdc, 0, 40078fcd0a1Sachartre "[%d] Cannot detach: %d outstanding validate request\n", 40178fcd0a1Sachartre instance, vdc->validate_pending); 40278fcd0a1Sachartre return (DDI_FAILURE); 40378fcd0a1Sachartre } 40478fcd0a1Sachartre 4053af08d82Slm66018 DMSG(vdc, 0, "[%d] proceeding...\n", instance); 4063af08d82Slm66018 4073af08d82Slm66018 /* mark instance as detaching */ 4083af08d82Slm66018 vdc->lifecycle = VDC_LC_DETACHING; 4091ae08745Sheppo 4101ae08745Sheppo /* 4111ae08745Sheppo * try and disable callbacks to prevent another handshake 4121ae08745Sheppo */ 4131ae08745Sheppo rv = ldc_set_cb_mode(vdc->ldc_handle, LDC_CB_DISABLE); 4143af08d82Slm66018 DMSG(vdc, 0, "callback disabled (rv=%d)\n", rv); 4151ae08745Sheppo 4161ae08745Sheppo if (vdc->initialized & VDC_THREAD) { 4173af08d82Slm66018 mutex_enter(&vdc->read_lock); 4183af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 4193af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) { 4203af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 4213af08d82Slm66018 cv_signal(&vdc->read_cv); 4221ae08745Sheppo } 4233af08d82Slm66018 4243af08d82Slm66018 mutex_exit(&vdc->read_lock); 4253af08d82Slm66018 4263af08d82Slm66018 /* wake up any thread waiting for connection to come online */ 4273af08d82Slm66018 mutex_enter(&vdc->lock); 4283af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 4293af08d82Slm66018 DMSG(vdc, 0, 4303af08d82Slm66018 "[%d] write reset - move to resetting state...\n", 4313af08d82Slm66018 instance); 4323af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 4333af08d82Slm66018 cv_signal(&vdc->initwait_cv); 4343af08d82Slm66018 } 4353af08d82Slm66018 mutex_exit(&vdc->lock); 4363af08d82Slm66018 4373af08d82Slm66018 /* now wait until state transitions to VDC_STATE_DETACH */ 4383af08d82Slm66018 thread_join(vdc->msg_proc_thr->t_did); 4393af08d82Slm66018 ASSERT(vdc->state == VDC_STATE_DETACH); 4403af08d82Slm66018 DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n", 4413af08d82Slm66018 vdc->instance); 4421ae08745Sheppo } 4431ae08745Sheppo 4441ae08745Sheppo mutex_enter(&vdc->lock); 4451ae08745Sheppo 4461ae08745Sheppo if (vdc->initialized & VDC_DRING) 4471ae08745Sheppo vdc_destroy_descriptor_ring(vdc); 4481ae08745Sheppo 4491ae08745Sheppo if (vdc->initialized & VDC_LDC) 4501ae08745Sheppo vdc_terminate_ldc(vdc); 4511ae08745Sheppo 4521ae08745Sheppo mutex_exit(&vdc->lock); 4531ae08745Sheppo 4541ae08745Sheppo if (vdc->initialized & VDC_MINOR) { 4551ae08745Sheppo ddi_prop_remove_all(dip); 4561ae08745Sheppo ddi_remove_minor_node(dip, NULL); 4571ae08745Sheppo } 4581ae08745Sheppo 4591ae08745Sheppo if (vdc->initialized & VDC_LOCKS) { 4601ae08745Sheppo mutex_destroy(&vdc->lock); 4613af08d82Slm66018 mutex_destroy(&vdc->read_lock); 4623af08d82Slm66018 cv_destroy(&vdc->initwait_cv); 4633af08d82Slm66018 cv_destroy(&vdc->dring_free_cv); 4643af08d82Slm66018 cv_destroy(&vdc->membind_cv); 4653af08d82Slm66018 cv_destroy(&vdc->sync_pending_cv); 4663af08d82Slm66018 cv_destroy(&vdc->sync_blocked_cv); 4673af08d82Slm66018 cv_destroy(&vdc->read_cv); 4683af08d82Slm66018 cv_destroy(&vdc->running_cv); 4691ae08745Sheppo } 4701ae08745Sheppo 4711ae08745Sheppo if (vdc->minfo) 4721ae08745Sheppo kmem_free(vdc->minfo, sizeof (struct dk_minfo)); 4731ae08745Sheppo 4741ae08745Sheppo if (vdc->cinfo) 4751ae08745Sheppo kmem_free(vdc->cinfo, sizeof (struct dk_cinfo)); 4761ae08745Sheppo 4771ae08745Sheppo if (vdc->vtoc) 4781ae08745Sheppo kmem_free(vdc->vtoc, sizeof (struct vtoc)); 4791ae08745Sheppo 48078fcd0a1Sachartre if (vdc->geom) 48178fcd0a1Sachartre kmem_free(vdc->geom, sizeof (struct dk_geom)); 4820a55fbb7Slm66018 4834bac2208Snarayan if (vdc->devid) { 4844bac2208Snarayan ddi_devid_unregister(dip); 4854bac2208Snarayan ddi_devid_free(vdc->devid); 4864bac2208Snarayan } 4874bac2208Snarayan 4881ae08745Sheppo if (vdc->initialized & VDC_SOFT_STATE) 4891ae08745Sheppo ddi_soft_state_free(vdc_state, instance); 4901ae08745Sheppo 4913af08d82Slm66018 DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc); 4921ae08745Sheppo 4931ae08745Sheppo return (DDI_SUCCESS); 4941ae08745Sheppo } 4951ae08745Sheppo 4961ae08745Sheppo 4971ae08745Sheppo static int 4981ae08745Sheppo vdc_do_attach(dev_info_t *dip) 4991ae08745Sheppo { 5001ae08745Sheppo int instance; 5011ae08745Sheppo vdc_t *vdc = NULL; 5021ae08745Sheppo int status; 503655fd6a9Sachartre md_t *mdp; 504655fd6a9Sachartre mde_cookie_t vd_node, vd_port; 5051ae08745Sheppo 5061ae08745Sheppo ASSERT(dip != NULL); 5071ae08745Sheppo 5081ae08745Sheppo instance = ddi_get_instance(dip); 5091ae08745Sheppo if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) { 510e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure", 511e1ebb9ecSlm66018 instance); 5121ae08745Sheppo return (DDI_FAILURE); 5131ae08745Sheppo } 5141ae08745Sheppo 5151ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 516e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 5171ae08745Sheppo return (DDI_FAILURE); 5181ae08745Sheppo } 5191ae08745Sheppo 5201ae08745Sheppo /* 5211ae08745Sheppo * We assign the value to initialized in this case to zero out the 5221ae08745Sheppo * variable and then set bits in it to indicate what has been done 5231ae08745Sheppo */ 5241ae08745Sheppo vdc->initialized = VDC_SOFT_STATE; 5251ae08745Sheppo 5263af08d82Slm66018 vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc); 5273af08d82Slm66018 vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc); 5281ae08745Sheppo 5291ae08745Sheppo vdc->dip = dip; 5301ae08745Sheppo vdc->instance = instance; 5311ae08745Sheppo vdc->vdisk_type = VD_DISK_TYPE_UNK; 5324bac2208Snarayan vdc->vdisk_label = VD_DISK_LABEL_UNK; 5333af08d82Slm66018 vdc->state = VDC_STATE_INIT; 5343af08d82Slm66018 vdc->lifecycle = VDC_LC_ATTACHING; 5351ae08745Sheppo vdc->ldc_state = 0; 5361ae08745Sheppo vdc->session_id = 0; 5371ae08745Sheppo vdc->block_size = DEV_BSIZE; 5388e6a2a04Slm66018 vdc->max_xfer_sz = maxphys / DEV_BSIZE; 5391ae08745Sheppo 540*17cadca8Slm66018 /* 541*17cadca8Slm66018 * We assume, for now, that the vDisk server will export 'read' 542*17cadca8Slm66018 * operations to us at a minimum (this is needed because of checks 543*17cadca8Slm66018 * in vdc for supported operations early in the handshake process). 544*17cadca8Slm66018 * The vDisk server will return ENOTSUP if this is not the case. 545*17cadca8Slm66018 * The value will be overwritten during the attribute exchange with 546*17cadca8Slm66018 * the bitmask of operations exported by server. 547*17cadca8Slm66018 */ 548*17cadca8Slm66018 vdc->operations = VD_OP_MASK_READ; 549*17cadca8Slm66018 5501ae08745Sheppo vdc->vtoc = NULL; 55178fcd0a1Sachartre vdc->geom = NULL; 5521ae08745Sheppo vdc->cinfo = NULL; 5531ae08745Sheppo vdc->minfo = NULL; 5541ae08745Sheppo 5551ae08745Sheppo mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL); 5563af08d82Slm66018 cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL); 5573af08d82Slm66018 cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL); 5583af08d82Slm66018 cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL); 5593af08d82Slm66018 cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL); 5603af08d82Slm66018 5613af08d82Slm66018 vdc->threads_pending = 0; 5623af08d82Slm66018 vdc->sync_op_pending = B_FALSE; 5633af08d82Slm66018 vdc->sync_op_blocked = B_FALSE; 5643af08d82Slm66018 cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL); 5653af08d82Slm66018 cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL); 5663af08d82Slm66018 5673af08d82Slm66018 /* init blocking msg read functionality */ 5683af08d82Slm66018 mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL); 5693af08d82Slm66018 cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL); 5703af08d82Slm66018 vdc->read_state = VDC_READ_IDLE; 5713af08d82Slm66018 5721ae08745Sheppo vdc->initialized |= VDC_LOCKS; 5731ae08745Sheppo 574655fd6a9Sachartre /* get device and port MD node for this disk instance */ 575655fd6a9Sachartre if (vdc_get_md_node(dip, &mdp, &vd_node, &vd_port) != 0) { 576655fd6a9Sachartre cmn_err(CE_NOTE, "[%d] Could not get machine description node", 577655fd6a9Sachartre instance); 578655fd6a9Sachartre return (DDI_FAILURE); 579655fd6a9Sachartre } 580655fd6a9Sachartre 581655fd6a9Sachartre /* set the connection timeout */ 582655fd6a9Sachartre if (vd_port == NULL || (md_get_prop_val(mdp, vd_port, 583655fd6a9Sachartre VDC_MD_TIMEOUT, &vdc->ctimeout) != 0)) { 584655fd6a9Sachartre vdc->ctimeout = 0; 585655fd6a9Sachartre } 586655fd6a9Sachartre 5873af08d82Slm66018 /* initialise LDC channel which will be used to communicate with vds */ 588655fd6a9Sachartre status = vdc_do_ldc_init(vdc, mdp, vd_node); 589655fd6a9Sachartre 590655fd6a9Sachartre (void) md_fini_handle(mdp); 591655fd6a9Sachartre 592655fd6a9Sachartre if (status != 0) { 5933af08d82Slm66018 cmn_err(CE_NOTE, "[%d] Couldn't initialize LDC", instance); 5943af08d82Slm66018 goto return_status; 5953af08d82Slm66018 } 5963af08d82Slm66018 5973af08d82Slm66018 /* initialize the thread responsible for managing state with server */ 5983af08d82Slm66018 vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread, 5991ae08745Sheppo vdc, 0, &p0, TS_RUN, minclsyspri); 6003af08d82Slm66018 if (vdc->msg_proc_thr == NULL) { 6011ae08745Sheppo cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread", 6021ae08745Sheppo instance); 6031ae08745Sheppo return (DDI_FAILURE); 6041ae08745Sheppo } 6053af08d82Slm66018 6061ae08745Sheppo vdc->initialized |= VDC_THREAD; 6071ae08745Sheppo 608e1ebb9ecSlm66018 atomic_inc_32(&vdc_instance_count); 6091ae08745Sheppo 6100a55fbb7Slm66018 /* 61178fcd0a1Sachartre * Check the disk label. This will send requests and do the handshake. 61278fcd0a1Sachartre * We don't really care about the disk label now. What we really need is 61378fcd0a1Sachartre * the handshake do be done so that we know the type of the disk (slice 61478fcd0a1Sachartre * or full disk) and the appropriate device nodes can be created. 6150a55fbb7Slm66018 */ 61678fcd0a1Sachartre vdc->vdisk_label = VD_DISK_LABEL_UNK; 61778fcd0a1Sachartre vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP); 61878fcd0a1Sachartre vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 619*17cadca8Slm66018 vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP); 62078fcd0a1Sachartre 62178fcd0a1Sachartre mutex_enter(&vdc->lock); 62278fcd0a1Sachartre (void) vdc_validate_geometry(vdc); 62378fcd0a1Sachartre mutex_exit(&vdc->lock); 6241ae08745Sheppo 6251ae08745Sheppo /* 6261ae08745Sheppo * Now that we have the device info we can create the 6271ae08745Sheppo * device nodes and properties 6281ae08745Sheppo */ 6291ae08745Sheppo status = vdc_create_device_nodes(vdc); 6301ae08745Sheppo if (status) { 6313af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to create device nodes", 6321ae08745Sheppo instance); 6333af08d82Slm66018 goto return_status; 6341ae08745Sheppo } 6351ae08745Sheppo status = vdc_create_device_nodes_props(vdc); 6361ae08745Sheppo if (status) { 6373af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to create device nodes" 6380a55fbb7Slm66018 " properties (%d)", instance, status); 6393af08d82Slm66018 goto return_status; 6401ae08745Sheppo } 6411ae08745Sheppo 6424bac2208Snarayan /* 6434bac2208Snarayan * Setup devid 6444bac2208Snarayan */ 6454bac2208Snarayan if (vdc_setup_devid(vdc)) { 6463af08d82Slm66018 DMSG(vdc, 0, "[%d] No device id available\n", instance); 6474bac2208Snarayan } 6484bac2208Snarayan 6491ae08745Sheppo ddi_report_dev(dip); 6503af08d82Slm66018 vdc->lifecycle = VDC_LC_ONLINE; 6513af08d82Slm66018 DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance); 6521ae08745Sheppo 6533af08d82Slm66018 return_status: 6543af08d82Slm66018 DMSG(vdc, 0, "[%d] Attach completed\n", instance); 6551ae08745Sheppo return (status); 6561ae08745Sheppo } 6571ae08745Sheppo 6581ae08745Sheppo static int 6591ae08745Sheppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 6601ae08745Sheppo { 6611ae08745Sheppo int status; 6621ae08745Sheppo 6631ae08745Sheppo switch (cmd) { 6641ae08745Sheppo case DDI_ATTACH: 6651ae08745Sheppo if ((status = vdc_do_attach(dip)) != 0) 6661ae08745Sheppo (void) vdc_detach(dip, DDI_DETACH); 6671ae08745Sheppo return (status); 6681ae08745Sheppo case DDI_RESUME: 6691ae08745Sheppo /* nothing to do for this non-device */ 6701ae08745Sheppo return (DDI_SUCCESS); 6711ae08745Sheppo default: 6721ae08745Sheppo return (DDI_FAILURE); 6731ae08745Sheppo } 6741ae08745Sheppo } 6751ae08745Sheppo 6761ae08745Sheppo static int 677655fd6a9Sachartre vdc_do_ldc_init(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_node) 6781ae08745Sheppo { 6791ae08745Sheppo int status = 0; 6801ae08745Sheppo ldc_status_t ldc_state; 6811ae08745Sheppo ldc_attr_t ldc_attr; 6821ae08745Sheppo uint64_t ldc_id = 0; 6831ae08745Sheppo 6841ae08745Sheppo ASSERT(vdc != NULL); 6851ae08745Sheppo 6861ae08745Sheppo vdc->initialized |= VDC_LDC; 6871ae08745Sheppo 688655fd6a9Sachartre if ((status = vdc_get_ldc_id(mdp, vd_node, &ldc_id)) != 0) { 6893af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to get LDC channel ID property", 690e1ebb9ecSlm66018 vdc->instance); 6911ae08745Sheppo return (EIO); 6921ae08745Sheppo } 693655fd6a9Sachartre 694655fd6a9Sachartre DMSGX(0, "[%d] LDC id is 0x%lx\n", vdc->instance, ldc_id); 695655fd6a9Sachartre 6961ae08745Sheppo vdc->ldc_id = ldc_id; 6971ae08745Sheppo 6981ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK; 6991ae08745Sheppo ldc_attr.instance = vdc->instance; 7001ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; /* unreliable transport */ 701e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 7021ae08745Sheppo 7031ae08745Sheppo if ((vdc->initialized & VDC_LDC_INIT) == 0) { 7041ae08745Sheppo status = ldc_init(ldc_id, &ldc_attr, &vdc->ldc_handle); 7051ae08745Sheppo if (status != 0) { 7063af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d", 7071ae08745Sheppo vdc->instance, ldc_id, status); 7081ae08745Sheppo return (status); 7091ae08745Sheppo } 7101ae08745Sheppo vdc->initialized |= VDC_LDC_INIT; 7111ae08745Sheppo } 7121ae08745Sheppo status = ldc_status(vdc->ldc_handle, &ldc_state); 7131ae08745Sheppo if (status != 0) { 7143af08d82Slm66018 DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]", 715e1ebb9ecSlm66018 vdc->instance, status); 7161ae08745Sheppo return (status); 7171ae08745Sheppo } 7181ae08745Sheppo vdc->ldc_state = ldc_state; 7191ae08745Sheppo 7201ae08745Sheppo if ((vdc->initialized & VDC_LDC_CB) == 0) { 7211ae08745Sheppo status = ldc_reg_callback(vdc->ldc_handle, vdc_handle_cb, 7221ae08745Sheppo (caddr_t)vdc); 7231ae08745Sheppo if (status != 0) { 7243af08d82Slm66018 DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)", 725e1ebb9ecSlm66018 vdc->instance, status); 7261ae08745Sheppo return (status); 7271ae08745Sheppo } 7281ae08745Sheppo vdc->initialized |= VDC_LDC_CB; 7291ae08745Sheppo } 7301ae08745Sheppo 7311ae08745Sheppo vdc->initialized |= VDC_LDC; 7321ae08745Sheppo 7331ae08745Sheppo /* 7341ae08745Sheppo * At this stage we have initialised LDC, we will now try and open 7351ae08745Sheppo * the connection. 7361ae08745Sheppo */ 7371ae08745Sheppo if (vdc->ldc_state == LDC_INIT) { 7381ae08745Sheppo status = ldc_open(vdc->ldc_handle); 7391ae08745Sheppo if (status != 0) { 7403af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d", 7411ae08745Sheppo vdc->instance, vdc->ldc_id, status); 7421ae08745Sheppo return (status); 7431ae08745Sheppo } 7441ae08745Sheppo vdc->initialized |= VDC_LDC_OPEN; 7451ae08745Sheppo } 7461ae08745Sheppo 7471ae08745Sheppo return (status); 7481ae08745Sheppo } 7491ae08745Sheppo 7501ae08745Sheppo static int 7511ae08745Sheppo vdc_start_ldc_connection(vdc_t *vdc) 7521ae08745Sheppo { 7531ae08745Sheppo int status = 0; 7541ae08745Sheppo 7551ae08745Sheppo ASSERT(vdc != NULL); 7561ae08745Sheppo 7573af08d82Slm66018 ASSERT(MUTEX_HELD(&vdc->lock)); 7581ae08745Sheppo 7590a55fbb7Slm66018 status = vdc_do_ldc_up(vdc); 7601ae08745Sheppo 7613af08d82Slm66018 DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance); 7621ae08745Sheppo 7633af08d82Slm66018 return (status); 7643af08d82Slm66018 } 7653af08d82Slm66018 7663af08d82Slm66018 static int 7673af08d82Slm66018 vdc_stop_ldc_connection(vdc_t *vdcp) 7683af08d82Slm66018 { 7693af08d82Slm66018 int status; 7703af08d82Slm66018 7713af08d82Slm66018 DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n", 7723af08d82Slm66018 vdcp->state); 7733af08d82Slm66018 7743af08d82Slm66018 status = ldc_down(vdcp->ldc_handle); 7753af08d82Slm66018 DMSG(vdcp, 0, "ldc_down() = %d\n", status); 7763af08d82Slm66018 7773af08d82Slm66018 vdcp->initialized &= ~VDC_HANDSHAKE; 7783af08d82Slm66018 DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized); 7791ae08745Sheppo 7801ae08745Sheppo return (status); 7811ae08745Sheppo } 7821ae08745Sheppo 7834bac2208Snarayan static int 7844bac2208Snarayan vdc_create_device_nodes_efi(vdc_t *vdc) 7854bac2208Snarayan { 7864bac2208Snarayan ddi_remove_minor_node(vdc->dip, "h"); 7874bac2208Snarayan ddi_remove_minor_node(vdc->dip, "h,raw"); 7884bac2208Snarayan 7894bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK, 7904bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 7914bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 7924bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'", 7934bac2208Snarayan vdc->instance); 7944bac2208Snarayan return (EIO); 7954bac2208Snarayan } 7964bac2208Snarayan 7974bac2208Snarayan /* if any device node is created we set this flag */ 7984bac2208Snarayan vdc->initialized |= VDC_MINOR; 7994bac2208Snarayan 8004bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR, 8014bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 8024bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 8034bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'", 8044bac2208Snarayan vdc->instance); 8054bac2208Snarayan return (EIO); 8064bac2208Snarayan } 8074bac2208Snarayan 8084bac2208Snarayan return (0); 8094bac2208Snarayan } 8104bac2208Snarayan 8114bac2208Snarayan static int 8124bac2208Snarayan vdc_create_device_nodes_vtoc(vdc_t *vdc) 8134bac2208Snarayan { 8144bac2208Snarayan ddi_remove_minor_node(vdc->dip, "wd"); 8154bac2208Snarayan ddi_remove_minor_node(vdc->dip, "wd,raw"); 8164bac2208Snarayan 8174bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK, 8184bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 8194bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 8204bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'", 8214bac2208Snarayan vdc->instance); 8224bac2208Snarayan return (EIO); 8234bac2208Snarayan } 8244bac2208Snarayan 8254bac2208Snarayan /* if any device node is created we set this flag */ 8264bac2208Snarayan vdc->initialized |= VDC_MINOR; 8274bac2208Snarayan 8284bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR, 8294bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 8304bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 8314bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'", 8324bac2208Snarayan vdc->instance); 8334bac2208Snarayan return (EIO); 8344bac2208Snarayan } 8354bac2208Snarayan 8364bac2208Snarayan return (0); 8374bac2208Snarayan } 8381ae08745Sheppo 8391ae08745Sheppo /* 8401ae08745Sheppo * Function: 8411ae08745Sheppo * vdc_create_device_nodes 8421ae08745Sheppo * 8431ae08745Sheppo * Description: 8441ae08745Sheppo * This function creates the block and character device nodes under 8451ae08745Sheppo * /devices along with the node properties. It is called as part of 8461ae08745Sheppo * the attach(9E) of the instance during the handshake with vds after 8471ae08745Sheppo * vds has sent the attributes to vdc. 8481ae08745Sheppo * 8491ae08745Sheppo * If the device is of type VD_DISK_TYPE_SLICE then the minor node 8501ae08745Sheppo * of 2 is used in keeping with the Solaris convention that slice 2 8511ae08745Sheppo * refers to a whole disk. Slices start at 'a' 8521ae08745Sheppo * 8531ae08745Sheppo * Parameters: 8541ae08745Sheppo * vdc - soft state pointer 8551ae08745Sheppo * 8561ae08745Sheppo * Return Values 8571ae08745Sheppo * 0 - Success 8581ae08745Sheppo * EIO - Failed to create node 8591ae08745Sheppo * EINVAL - Unknown type of disk exported 8601ae08745Sheppo */ 8611ae08745Sheppo static int 8621ae08745Sheppo vdc_create_device_nodes(vdc_t *vdc) 8631ae08745Sheppo { 8644bac2208Snarayan char name[sizeof ("s,raw")]; 8651ae08745Sheppo dev_info_t *dip = NULL; 8664bac2208Snarayan int instance, status; 8671ae08745Sheppo int num_slices = 1; 8681ae08745Sheppo int i; 8691ae08745Sheppo 8701ae08745Sheppo ASSERT(vdc != NULL); 8711ae08745Sheppo 8721ae08745Sheppo instance = vdc->instance; 8731ae08745Sheppo dip = vdc->dip; 8741ae08745Sheppo 8751ae08745Sheppo switch (vdc->vdisk_type) { 8761ae08745Sheppo case VD_DISK_TYPE_DISK: 8771ae08745Sheppo num_slices = V_NUMPAR; 8781ae08745Sheppo break; 8791ae08745Sheppo case VD_DISK_TYPE_SLICE: 8801ae08745Sheppo num_slices = 1; 8811ae08745Sheppo break; 8821ae08745Sheppo case VD_DISK_TYPE_UNK: 8831ae08745Sheppo default: 8841ae08745Sheppo return (EINVAL); 8851ae08745Sheppo } 8861ae08745Sheppo 8874bac2208Snarayan /* 8884bac2208Snarayan * Minor nodes are different for EFI disks: EFI disks do not have 8894bac2208Snarayan * a minor node 'g' for the minor number corresponding to slice 8904bac2208Snarayan * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd' 8914bac2208Snarayan * representing the whole disk. 8924bac2208Snarayan */ 8931ae08745Sheppo for (i = 0; i < num_slices; i++) { 8944bac2208Snarayan 8954bac2208Snarayan if (i == VD_EFI_WD_SLICE) { 8964bac2208Snarayan if (vdc->vdisk_label == VD_DISK_LABEL_EFI) 8974bac2208Snarayan status = vdc_create_device_nodes_efi(vdc); 8984bac2208Snarayan else 8994bac2208Snarayan status = vdc_create_device_nodes_vtoc(vdc); 9004bac2208Snarayan if (status != 0) 9014bac2208Snarayan return (status); 9024bac2208Snarayan continue; 9034bac2208Snarayan } 9044bac2208Snarayan 9051ae08745Sheppo (void) snprintf(name, sizeof (name), "%c", 'a' + i); 9061ae08745Sheppo if (ddi_create_minor_node(dip, name, S_IFBLK, 9071ae08745Sheppo VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 908e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'", 909e1ebb9ecSlm66018 instance, name); 9101ae08745Sheppo return (EIO); 9111ae08745Sheppo } 9121ae08745Sheppo 9131ae08745Sheppo /* if any device node is created we set this flag */ 9141ae08745Sheppo vdc->initialized |= VDC_MINOR; 9151ae08745Sheppo 91687a7269eSachartre (void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw"); 91787a7269eSachartre 9181ae08745Sheppo if (ddi_create_minor_node(dip, name, S_IFCHR, 9191ae08745Sheppo VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 920e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'", 921e1ebb9ecSlm66018 instance, name); 9221ae08745Sheppo return (EIO); 9231ae08745Sheppo } 9241ae08745Sheppo } 9251ae08745Sheppo 9261ae08745Sheppo return (0); 9271ae08745Sheppo } 9281ae08745Sheppo 9291ae08745Sheppo /* 9301ae08745Sheppo * Function: 9311ae08745Sheppo * vdc_create_device_nodes_props 9321ae08745Sheppo * 9331ae08745Sheppo * Description: 9341ae08745Sheppo * This function creates the block and character device nodes under 9351ae08745Sheppo * /devices along with the node properties. It is called as part of 9361ae08745Sheppo * the attach(9E) of the instance during the handshake with vds after 9371ae08745Sheppo * vds has sent the attributes to vdc. 9381ae08745Sheppo * 9391ae08745Sheppo * Parameters: 9401ae08745Sheppo * vdc - soft state pointer 9411ae08745Sheppo * 9421ae08745Sheppo * Return Values 9431ae08745Sheppo * 0 - Success 9441ae08745Sheppo * EIO - Failed to create device node property 9451ae08745Sheppo * EINVAL - Unknown type of disk exported 9461ae08745Sheppo */ 9471ae08745Sheppo static int 9481ae08745Sheppo vdc_create_device_nodes_props(vdc_t *vdc) 9491ae08745Sheppo { 9501ae08745Sheppo dev_info_t *dip = NULL; 9511ae08745Sheppo int instance; 9521ae08745Sheppo int num_slices = 1; 9531ae08745Sheppo int64_t size = 0; 9541ae08745Sheppo dev_t dev; 9551ae08745Sheppo int rv; 9561ae08745Sheppo int i; 9571ae08745Sheppo 9581ae08745Sheppo ASSERT(vdc != NULL); 95978fcd0a1Sachartre ASSERT(vdc->vtoc != NULL); 9601ae08745Sheppo 9611ae08745Sheppo instance = vdc->instance; 9621ae08745Sheppo dip = vdc->dip; 9631ae08745Sheppo 9641ae08745Sheppo switch (vdc->vdisk_type) { 9651ae08745Sheppo case VD_DISK_TYPE_DISK: 9661ae08745Sheppo num_slices = V_NUMPAR; 9671ae08745Sheppo break; 9681ae08745Sheppo case VD_DISK_TYPE_SLICE: 9691ae08745Sheppo num_slices = 1; 9701ae08745Sheppo break; 9711ae08745Sheppo case VD_DISK_TYPE_UNK: 9721ae08745Sheppo default: 9731ae08745Sheppo return (EINVAL); 9741ae08745Sheppo } 9751ae08745Sheppo 97678fcd0a1Sachartre if (vdc->vdisk_label == VD_DISK_LABEL_UNK) { 97778fcd0a1Sachartre /* remove all properties */ 97878fcd0a1Sachartre for (i = 0; i < num_slices; i++) { 97978fcd0a1Sachartre dev = makedevice(ddi_driver_major(dip), 98078fcd0a1Sachartre VD_MAKE_DEV(instance, i)); 98178fcd0a1Sachartre (void) ddi_prop_remove(dev, dip, VDC_SIZE_PROP_NAME); 98278fcd0a1Sachartre (void) ddi_prop_remove(dev, dip, VDC_NBLOCKS_PROP_NAME); 98378fcd0a1Sachartre } 98478fcd0a1Sachartre return (0); 98578fcd0a1Sachartre } 98678fcd0a1Sachartre 9871ae08745Sheppo for (i = 0; i < num_slices; i++) { 9881ae08745Sheppo dev = makedevice(ddi_driver_major(dip), 9891ae08745Sheppo VD_MAKE_DEV(instance, i)); 9901ae08745Sheppo 9911ae08745Sheppo size = vdc->vtoc->v_part[i].p_size * vdc->vtoc->v_sectorsz; 9923af08d82Slm66018 DMSG(vdc, 0, "[%d] sz %ld (%ld Mb) p_size %lx\n", 993e1ebb9ecSlm66018 instance, size, size / (1024 * 1024), 9941ae08745Sheppo vdc->vtoc->v_part[i].p_size); 9951ae08745Sheppo 9961ae08745Sheppo rv = ddi_prop_update_int64(dev, dip, VDC_SIZE_PROP_NAME, size); 9971ae08745Sheppo if (rv != DDI_PROP_SUCCESS) { 998e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop of [%ld]", 999e1ebb9ecSlm66018 instance, VDC_SIZE_PROP_NAME, size); 10001ae08745Sheppo return (EIO); 10011ae08745Sheppo } 10021ae08745Sheppo 10031ae08745Sheppo rv = ddi_prop_update_int64(dev, dip, VDC_NBLOCKS_PROP_NAME, 10041ae08745Sheppo lbtodb(size)); 10051ae08745Sheppo if (rv != DDI_PROP_SUCCESS) { 1006e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop [%llu]", 10071ae08745Sheppo instance, VDC_NBLOCKS_PROP_NAME, lbtodb(size)); 10081ae08745Sheppo return (EIO); 10091ae08745Sheppo } 10101ae08745Sheppo } 10111ae08745Sheppo 10121ae08745Sheppo return (0); 10131ae08745Sheppo } 10141ae08745Sheppo 101578fcd0a1Sachartre /* 101678fcd0a1Sachartre * Function: 101778fcd0a1Sachartre * vdc_is_opened 101878fcd0a1Sachartre * 101978fcd0a1Sachartre * Description: 102078fcd0a1Sachartre * This function checks if any slice of a given virtual disk is 102178fcd0a1Sachartre * currently opened. 102278fcd0a1Sachartre * 102378fcd0a1Sachartre * Parameters: 102478fcd0a1Sachartre * vdc - soft state pointer 102578fcd0a1Sachartre * 102678fcd0a1Sachartre * Return Values 102778fcd0a1Sachartre * B_TRUE - at least one slice is opened. 102878fcd0a1Sachartre * B_FALSE - no slice is opened. 102978fcd0a1Sachartre */ 103078fcd0a1Sachartre static boolean_t 103178fcd0a1Sachartre vdc_is_opened(vdc_t *vdc) 103278fcd0a1Sachartre { 103378fcd0a1Sachartre int i, nslices; 103478fcd0a1Sachartre 103578fcd0a1Sachartre switch (vdc->vdisk_type) { 103678fcd0a1Sachartre case VD_DISK_TYPE_DISK: 103778fcd0a1Sachartre nslices = V_NUMPAR; 103878fcd0a1Sachartre break; 103978fcd0a1Sachartre case VD_DISK_TYPE_SLICE: 104078fcd0a1Sachartre nslices = 1; 104178fcd0a1Sachartre break; 104278fcd0a1Sachartre case VD_DISK_TYPE_UNK: 104378fcd0a1Sachartre default: 104478fcd0a1Sachartre ASSERT(0); 104578fcd0a1Sachartre } 104678fcd0a1Sachartre 104778fcd0a1Sachartre /* check if there's any layered open */ 104878fcd0a1Sachartre for (i = 0; i < nslices; i++) { 104978fcd0a1Sachartre if (vdc->open_lyr[i] > 0) 105078fcd0a1Sachartre return (B_TRUE); 105178fcd0a1Sachartre } 105278fcd0a1Sachartre 105378fcd0a1Sachartre /* check if there is any other kind of open */ 105478fcd0a1Sachartre for (i = 0; i < OTYPCNT; i++) { 105578fcd0a1Sachartre if (vdc->open[i] != 0) 105678fcd0a1Sachartre return (B_TRUE); 105778fcd0a1Sachartre } 105878fcd0a1Sachartre 105978fcd0a1Sachartre return (B_FALSE); 106078fcd0a1Sachartre } 106178fcd0a1Sachartre 106278fcd0a1Sachartre static int 106378fcd0a1Sachartre vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp) 106478fcd0a1Sachartre { 106578fcd0a1Sachartre uint8_t slicemask; 106678fcd0a1Sachartre int i; 106778fcd0a1Sachartre 106878fcd0a1Sachartre ASSERT(otyp < OTYPCNT); 106978fcd0a1Sachartre ASSERT(slice < V_NUMPAR); 107078fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 107178fcd0a1Sachartre 107278fcd0a1Sachartre slicemask = 1 << slice; 107378fcd0a1Sachartre 107478fcd0a1Sachartre /* check if slice is already exclusively opened */ 107578fcd0a1Sachartre if (vdc->open_excl & slicemask) 107678fcd0a1Sachartre return (EBUSY); 107778fcd0a1Sachartre 107878fcd0a1Sachartre /* if open exclusive, check if slice is already opened */ 107978fcd0a1Sachartre if (flag & FEXCL) { 108078fcd0a1Sachartre if (vdc->open_lyr[slice] > 0) 108178fcd0a1Sachartre return (EBUSY); 108278fcd0a1Sachartre for (i = 0; i < OTYPCNT; i++) { 108378fcd0a1Sachartre if (vdc->open[i] & slicemask) 108478fcd0a1Sachartre return (EBUSY); 108578fcd0a1Sachartre } 108678fcd0a1Sachartre vdc->open_excl |= slicemask; 108778fcd0a1Sachartre } 108878fcd0a1Sachartre 108978fcd0a1Sachartre /* mark slice as opened */ 109078fcd0a1Sachartre if (otyp == OTYP_LYR) { 109178fcd0a1Sachartre vdc->open_lyr[slice]++; 109278fcd0a1Sachartre } else { 109378fcd0a1Sachartre vdc->open[otyp] |= slicemask; 109478fcd0a1Sachartre } 109578fcd0a1Sachartre 109678fcd0a1Sachartre return (0); 109778fcd0a1Sachartre } 109878fcd0a1Sachartre 109978fcd0a1Sachartre static void 110078fcd0a1Sachartre vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp) 110178fcd0a1Sachartre { 110278fcd0a1Sachartre uint8_t slicemask; 110378fcd0a1Sachartre 110478fcd0a1Sachartre ASSERT(otyp < OTYPCNT); 110578fcd0a1Sachartre ASSERT(slice < V_NUMPAR); 110678fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 110778fcd0a1Sachartre 110878fcd0a1Sachartre slicemask = 1 << slice; 110978fcd0a1Sachartre 111078fcd0a1Sachartre if (otyp == OTYP_LYR) { 111178fcd0a1Sachartre ASSERT(vdc->open_lyr[slice] > 0); 111278fcd0a1Sachartre vdc->open_lyr[slice]--; 111378fcd0a1Sachartre } else { 111478fcd0a1Sachartre vdc->open[otyp] &= ~slicemask; 111578fcd0a1Sachartre } 111678fcd0a1Sachartre 111778fcd0a1Sachartre if (flag & FEXCL) 111878fcd0a1Sachartre vdc->open_excl &= ~slicemask; 111978fcd0a1Sachartre } 112078fcd0a1Sachartre 11211ae08745Sheppo static int 11221ae08745Sheppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred) 11231ae08745Sheppo { 11241ae08745Sheppo _NOTE(ARGUNUSED(cred)) 11251ae08745Sheppo 11261ae08745Sheppo int instance; 112778fcd0a1Sachartre int slice, status = 0; 11281ae08745Sheppo vdc_t *vdc; 11291ae08745Sheppo 11301ae08745Sheppo ASSERT(dev != NULL); 11310d0c8d4bSnarayan instance = VDCUNIT(*dev); 11321ae08745Sheppo 113378fcd0a1Sachartre if (otyp >= OTYPCNT) 11341ae08745Sheppo return (EINVAL); 11351ae08745Sheppo 11361ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1137e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 11381ae08745Sheppo return (ENXIO); 11391ae08745Sheppo } 11401ae08745Sheppo 11413af08d82Slm66018 DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n", 11423af08d82Slm66018 getminor(*dev), flag, otyp); 11431ae08745Sheppo 114478fcd0a1Sachartre slice = VDCPART(*dev); 114578fcd0a1Sachartre 11461ae08745Sheppo mutex_enter(&vdc->lock); 114778fcd0a1Sachartre 114878fcd0a1Sachartre status = vdc_mark_opened(vdc, slice, flag, otyp); 114978fcd0a1Sachartre 115078fcd0a1Sachartre if (status != 0) { 115178fcd0a1Sachartre mutex_exit(&vdc->lock); 115278fcd0a1Sachartre return (status); 115378fcd0a1Sachartre } 115478fcd0a1Sachartre 115578fcd0a1Sachartre if (flag & (FNDELAY | FNONBLOCK)) { 115678fcd0a1Sachartre 115778fcd0a1Sachartre /* don't resubmit a validate request if there's already one */ 115878fcd0a1Sachartre if (vdc->validate_pending > 0) { 115978fcd0a1Sachartre mutex_exit(&vdc->lock); 116078fcd0a1Sachartre return (0); 116178fcd0a1Sachartre } 116278fcd0a1Sachartre 116378fcd0a1Sachartre /* call vdc_validate() asynchronously to avoid blocking */ 116478fcd0a1Sachartre if (taskq_dispatch(system_taskq, vdc_validate_task, 116578fcd0a1Sachartre (void *)vdc, TQ_NOSLEEP) == NULL) { 116678fcd0a1Sachartre vdc_mark_closed(vdc, slice, flag, otyp); 116778fcd0a1Sachartre mutex_exit(&vdc->lock); 116878fcd0a1Sachartre return (ENXIO); 116978fcd0a1Sachartre } 117078fcd0a1Sachartre 117178fcd0a1Sachartre vdc->validate_pending++; 117278fcd0a1Sachartre mutex_exit(&vdc->lock); 117378fcd0a1Sachartre return (0); 117478fcd0a1Sachartre } 117578fcd0a1Sachartre 11761ae08745Sheppo mutex_exit(&vdc->lock); 11771ae08745Sheppo 117878fcd0a1Sachartre vdc_validate(vdc); 117978fcd0a1Sachartre 118078fcd0a1Sachartre mutex_enter(&vdc->lock); 118178fcd0a1Sachartre 118278fcd0a1Sachartre if (vdc->vdisk_label == VD_DISK_LABEL_UNK || 118378fcd0a1Sachartre vdc->vtoc->v_part[slice].p_size == 0) { 118478fcd0a1Sachartre vdc_mark_closed(vdc, slice, flag, otyp); 118578fcd0a1Sachartre status = EIO; 118678fcd0a1Sachartre } 118778fcd0a1Sachartre 118878fcd0a1Sachartre mutex_exit(&vdc->lock); 118978fcd0a1Sachartre 119078fcd0a1Sachartre return (status); 11911ae08745Sheppo } 11921ae08745Sheppo 11931ae08745Sheppo static int 11941ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred) 11951ae08745Sheppo { 11961ae08745Sheppo _NOTE(ARGUNUSED(cred)) 11971ae08745Sheppo 11981ae08745Sheppo int instance; 119978fcd0a1Sachartre int slice; 12008259acd8Szk194757 int rv; 12011ae08745Sheppo vdc_t *vdc; 12021ae08745Sheppo 12030d0c8d4bSnarayan instance = VDCUNIT(dev); 12041ae08745Sheppo 120578fcd0a1Sachartre if (otyp >= OTYPCNT) 12061ae08745Sheppo return (EINVAL); 12071ae08745Sheppo 12081ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1209e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 12101ae08745Sheppo return (ENXIO); 12111ae08745Sheppo } 12121ae08745Sheppo 12133af08d82Slm66018 DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp); 12141ae08745Sheppo 121578fcd0a1Sachartre slice = VDCPART(dev); 121678fcd0a1Sachartre 12178259acd8Szk194757 /* 12188259acd8Szk194757 * Attempt to flush the W$ on a close operation. If this is 12198259acd8Szk194757 * not a supported IOCTL command or the backing device is read-only 12208259acd8Szk194757 * do not fail the close operation. 12218259acd8Szk194757 */ 12228259acd8Szk194757 rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL); 12238259acd8Szk194757 12248259acd8Szk194757 if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) { 12258259acd8Szk194757 DMSG(vdc, 0, "[%d] flush failed with error %d on close\n", 12268259acd8Szk194757 instance, rv); 12278259acd8Szk194757 return (EIO); 12288259acd8Szk194757 } 12298259acd8Szk194757 12301ae08745Sheppo mutex_enter(&vdc->lock); 123178fcd0a1Sachartre vdc_mark_closed(vdc, slice, flag, otyp); 12321ae08745Sheppo mutex_exit(&vdc->lock); 12331ae08745Sheppo 12341ae08745Sheppo return (0); 12351ae08745Sheppo } 12361ae08745Sheppo 12371ae08745Sheppo static int 12381ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 12391ae08745Sheppo { 12401ae08745Sheppo _NOTE(ARGUNUSED(credp)) 12411ae08745Sheppo _NOTE(ARGUNUSED(rvalp)) 12421ae08745Sheppo 12431ae08745Sheppo return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode)); 12441ae08745Sheppo } 12451ae08745Sheppo 12461ae08745Sheppo static int 12471ae08745Sheppo vdc_print(dev_t dev, char *str) 12481ae08745Sheppo { 12490d0c8d4bSnarayan cmn_err(CE_NOTE, "vdc%d: %s", VDCUNIT(dev), str); 12501ae08745Sheppo return (0); 12511ae08745Sheppo } 12521ae08745Sheppo 12531ae08745Sheppo static int 12541ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 12551ae08745Sheppo { 1256d10e4ef2Snarayan int rv; 1257d10e4ef2Snarayan size_t nbytes = nblk * DEV_BSIZE; 12580d0c8d4bSnarayan int instance = VDCUNIT(dev); 1259d10e4ef2Snarayan vdc_t *vdc = NULL; 12601ae08745Sheppo 12611ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1262e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 12631ae08745Sheppo return (ENXIO); 12641ae08745Sheppo } 12651ae08745Sheppo 12663af08d82Slm66018 DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n", 12673af08d82Slm66018 instance, nbytes, blkno, (void *)addr); 12683af08d82Slm66018 rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes, 12690d0c8d4bSnarayan VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir); 12703af08d82Slm66018 if (rv) { 12713af08d82Slm66018 DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv); 12721ae08745Sheppo return (rv); 12731ae08745Sheppo } 12741ae08745Sheppo 12753af08d82Slm66018 if (ddi_in_panic()) 12763af08d82Slm66018 (void) vdc_drain_response(vdc); 12773af08d82Slm66018 12783af08d82Slm66018 DMSG(vdc, 0, "[%d] End\n", instance); 12793af08d82Slm66018 12803af08d82Slm66018 return (0); 12813af08d82Slm66018 } 12823af08d82Slm66018 12831ae08745Sheppo /* -------------------------------------------------------------------------- */ 12841ae08745Sheppo 12851ae08745Sheppo /* 12861ae08745Sheppo * Disk access routines 12871ae08745Sheppo * 12881ae08745Sheppo */ 12891ae08745Sheppo 12901ae08745Sheppo /* 12911ae08745Sheppo * vdc_strategy() 12921ae08745Sheppo * 12931ae08745Sheppo * Return Value: 12941ae08745Sheppo * 0: As per strategy(9E), the strategy() function must return 0 12951ae08745Sheppo * [ bioerror(9f) sets b_flags to the proper error code ] 12961ae08745Sheppo */ 12971ae08745Sheppo static int 12981ae08745Sheppo vdc_strategy(struct buf *buf) 12991ae08745Sheppo { 13001ae08745Sheppo int rv = -1; 13011ae08745Sheppo vdc_t *vdc = NULL; 13020d0c8d4bSnarayan int instance = VDCUNIT(buf->b_edev); 13031ae08745Sheppo int op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE; 130487a7269eSachartre int slice; 13051ae08745Sheppo 13061ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1307e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 13081ae08745Sheppo bioerror(buf, ENXIO); 13091ae08745Sheppo biodone(buf); 13101ae08745Sheppo return (0); 13111ae08745Sheppo } 13121ae08745Sheppo 13133af08d82Slm66018 DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n", 13143af08d82Slm66018 instance, (buf->b_flags & B_READ) ? "Read" : "Write", 13153af08d82Slm66018 buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr); 1316d10e4ef2Snarayan DTRACE_IO2(vstart, buf_t *, buf, vdc_t *, vdc); 1317d10e4ef2Snarayan 13181ae08745Sheppo bp_mapin(buf); 13191ae08745Sheppo 132087a7269eSachartre if ((long)buf->b_private == VD_SLICE_NONE) { 132187a7269eSachartre /* I/O using an absolute disk offset */ 132287a7269eSachartre slice = VD_SLICE_NONE; 132387a7269eSachartre } else { 132487a7269eSachartre slice = VDCPART(buf->b_edev); 132587a7269eSachartre } 132687a7269eSachartre 13273af08d82Slm66018 rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr, 132887a7269eSachartre buf->b_bcount, slice, buf->b_lblkno, 13293af08d82Slm66018 CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir : 13303af08d82Slm66018 VIO_write_dir); 13313af08d82Slm66018 1332d10e4ef2Snarayan /* 1333d10e4ef2Snarayan * If the request was successfully sent, the strategy call returns and 1334d10e4ef2Snarayan * the ACK handler calls the bioxxx functions when the vDisk server is 1335d10e4ef2Snarayan * done. 1336d10e4ef2Snarayan */ 1337d10e4ef2Snarayan if (rv) { 13383af08d82Slm66018 DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv); 13391ae08745Sheppo bioerror(buf, rv); 13401ae08745Sheppo biodone(buf); 1341d10e4ef2Snarayan } 1342d10e4ef2Snarayan 13431ae08745Sheppo return (0); 13441ae08745Sheppo } 13451ae08745Sheppo 13460d0c8d4bSnarayan /* 13470d0c8d4bSnarayan * Function: 13480d0c8d4bSnarayan * vdc_min 13490d0c8d4bSnarayan * 13500d0c8d4bSnarayan * Description: 13510d0c8d4bSnarayan * Routine to limit the size of a data transfer. Used in 13520d0c8d4bSnarayan * conjunction with physio(9F). 13530d0c8d4bSnarayan * 13540d0c8d4bSnarayan * Arguments: 13550d0c8d4bSnarayan * bp - pointer to the indicated buf(9S) struct. 13560d0c8d4bSnarayan * 13570d0c8d4bSnarayan */ 13580d0c8d4bSnarayan static void 13590d0c8d4bSnarayan vdc_min(struct buf *bufp) 13600d0c8d4bSnarayan { 13610d0c8d4bSnarayan vdc_t *vdc = NULL; 13620d0c8d4bSnarayan int instance = VDCUNIT(bufp->b_edev); 13630d0c8d4bSnarayan 13640d0c8d4bSnarayan vdc = ddi_get_soft_state(vdc_state, instance); 13650d0c8d4bSnarayan VERIFY(vdc != NULL); 13660d0c8d4bSnarayan 13670d0c8d4bSnarayan if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) { 13680d0c8d4bSnarayan bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size; 13690d0c8d4bSnarayan } 13700d0c8d4bSnarayan } 13711ae08745Sheppo 13721ae08745Sheppo static int 13731ae08745Sheppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred) 13741ae08745Sheppo { 13751ae08745Sheppo _NOTE(ARGUNUSED(cred)) 13761ae08745Sheppo 13770d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 13780d0c8d4bSnarayan return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio)); 13791ae08745Sheppo } 13801ae08745Sheppo 13811ae08745Sheppo static int 13821ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred) 13831ae08745Sheppo { 13841ae08745Sheppo _NOTE(ARGUNUSED(cred)) 13851ae08745Sheppo 13860d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 13870d0c8d4bSnarayan return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio)); 13881ae08745Sheppo } 13891ae08745Sheppo 13901ae08745Sheppo static int 13911ae08745Sheppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred) 13921ae08745Sheppo { 13931ae08745Sheppo _NOTE(ARGUNUSED(cred)) 13941ae08745Sheppo 13950d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 13960d0c8d4bSnarayan return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio)); 13971ae08745Sheppo } 13981ae08745Sheppo 13991ae08745Sheppo static int 14001ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred) 14011ae08745Sheppo { 14021ae08745Sheppo _NOTE(ARGUNUSED(cred)) 14031ae08745Sheppo 14040d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 14050d0c8d4bSnarayan return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio)); 14061ae08745Sheppo } 14071ae08745Sheppo 14081ae08745Sheppo 14091ae08745Sheppo /* -------------------------------------------------------------------------- */ 14101ae08745Sheppo 14111ae08745Sheppo /* 14121ae08745Sheppo * Handshake support 14131ae08745Sheppo */ 14141ae08745Sheppo 14151ae08745Sheppo 14160a55fbb7Slm66018 /* 14170a55fbb7Slm66018 * Function: 14180a55fbb7Slm66018 * vdc_init_ver_negotiation() 14190a55fbb7Slm66018 * 14200a55fbb7Slm66018 * Description: 14210a55fbb7Slm66018 * 14220a55fbb7Slm66018 * Arguments: 14230a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 14240a55fbb7Slm66018 * 14250a55fbb7Slm66018 * Return Code: 14260a55fbb7Slm66018 * 0 - Success 14270a55fbb7Slm66018 */ 14281ae08745Sheppo static int 14290a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver) 14301ae08745Sheppo { 14311ae08745Sheppo vio_ver_msg_t pkt; 14321ae08745Sheppo size_t msglen = sizeof (pkt); 14331ae08745Sheppo int status = -1; 14341ae08745Sheppo 14351ae08745Sheppo ASSERT(vdc != NULL); 14361ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 14371ae08745Sheppo 14383af08d82Slm66018 DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance); 1439e1ebb9ecSlm66018 14401ae08745Sheppo /* 14411ae08745Sheppo * set the Session ID to a unique value 14421ae08745Sheppo * (the lower 32 bits of the clock tick) 14431ae08745Sheppo */ 14441ae08745Sheppo vdc->session_id = ((uint32_t)gettick() & 0xffffffff); 14453af08d82Slm66018 DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id); 14461ae08745Sheppo 14471ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 14481ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 14491ae08745Sheppo pkt.tag.vio_subtype_env = VIO_VER_INFO; 14501ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 14511ae08745Sheppo pkt.dev_class = VDEV_DISK; 14520a55fbb7Slm66018 pkt.ver_major = ver.major; 14530a55fbb7Slm66018 pkt.ver_minor = ver.minor; 14541ae08745Sheppo 14550a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 14563af08d82Slm66018 DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n", 14573af08d82Slm66018 vdc->instance, status); 14581ae08745Sheppo if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) { 14593af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: " 146087a7269eSachartre "id(%lx) rv(%d) size(%ld)", vdc->instance, vdc->ldc_handle, 14611ae08745Sheppo status, msglen); 14621ae08745Sheppo if (msglen != sizeof (vio_ver_msg_t)) 14631ae08745Sheppo status = ENOMSG; 14641ae08745Sheppo } 14651ae08745Sheppo 14661ae08745Sheppo return (status); 14671ae08745Sheppo } 14681ae08745Sheppo 14690a55fbb7Slm66018 /* 14700a55fbb7Slm66018 * Function: 14713af08d82Slm66018 * vdc_ver_negotiation() 14723af08d82Slm66018 * 14733af08d82Slm66018 * Description: 14743af08d82Slm66018 * 14753af08d82Slm66018 * Arguments: 14763af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 14773af08d82Slm66018 * 14783af08d82Slm66018 * Return Code: 14793af08d82Slm66018 * 0 - Success 14803af08d82Slm66018 */ 14813af08d82Slm66018 static int 14823af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp) 14833af08d82Slm66018 { 14843af08d82Slm66018 vio_msg_t vio_msg; 14853af08d82Slm66018 int status; 14863af08d82Slm66018 14873af08d82Slm66018 if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0])) 14883af08d82Slm66018 return (status); 14893af08d82Slm66018 14903af08d82Slm66018 /* release lock and wait for response */ 14913af08d82Slm66018 mutex_exit(&vdcp->lock); 14923af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 14933af08d82Slm66018 mutex_enter(&vdcp->lock); 14943af08d82Slm66018 if (status) { 14953af08d82Slm66018 DMSG(vdcp, 0, 14963af08d82Slm66018 "[%d] Failed waiting for Ver negotiation response, rv(%d)", 14973af08d82Slm66018 vdcp->instance, status); 14983af08d82Slm66018 return (status); 14993af08d82Slm66018 } 15003af08d82Slm66018 15013af08d82Slm66018 /* check type and sub_type ... */ 15023af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 15033af08d82Slm66018 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 15043af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n", 15053af08d82Slm66018 vdcp->instance); 15063af08d82Slm66018 return (EPROTO); 15073af08d82Slm66018 } 15083af08d82Slm66018 15093af08d82Slm66018 return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg)); 15103af08d82Slm66018 } 15113af08d82Slm66018 15123af08d82Slm66018 /* 15133af08d82Slm66018 * Function: 15140a55fbb7Slm66018 * vdc_init_attr_negotiation() 15150a55fbb7Slm66018 * 15160a55fbb7Slm66018 * Description: 15170a55fbb7Slm66018 * 15180a55fbb7Slm66018 * Arguments: 15190a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 15200a55fbb7Slm66018 * 15210a55fbb7Slm66018 * Return Code: 15220a55fbb7Slm66018 * 0 - Success 15230a55fbb7Slm66018 */ 15241ae08745Sheppo static int 15251ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc) 15261ae08745Sheppo { 15271ae08745Sheppo vd_attr_msg_t pkt; 15281ae08745Sheppo size_t msglen = sizeof (pkt); 15291ae08745Sheppo int status; 15301ae08745Sheppo 15311ae08745Sheppo ASSERT(vdc != NULL); 15321ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 15331ae08745Sheppo 15343af08d82Slm66018 DMSG(vdc, 0, "[%d] entered\n", vdc->instance); 15351ae08745Sheppo 15361ae08745Sheppo /* fill in tag */ 15371ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 15381ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 15391ae08745Sheppo pkt.tag.vio_subtype_env = VIO_ATTR_INFO; 15401ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 15411ae08745Sheppo /* fill in payload */ 15421ae08745Sheppo pkt.max_xfer_sz = vdc->max_xfer_sz; 15431ae08745Sheppo pkt.vdisk_block_size = vdc->block_size; 15441ae08745Sheppo pkt.xfer_mode = VIO_DRING_MODE; 15451ae08745Sheppo pkt.operations = 0; /* server will set bits of valid operations */ 15461ae08745Sheppo pkt.vdisk_type = 0; /* server will set to valid device type */ 1547*17cadca8Slm66018 pkt.vdisk_media = 0; /* server will set to valid media type */ 15481ae08745Sheppo pkt.vdisk_size = 0; /* server will set to valid size */ 15491ae08745Sheppo 15500a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 15513af08d82Slm66018 DMSG(vdc, 0, "Attr info sent (status = %d)\n", status); 15521ae08745Sheppo 15531ae08745Sheppo if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) { 15543af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: " 155587a7269eSachartre "id(%lx) rv(%d) size(%ld)", vdc->instance, vdc->ldc_handle, 15561ae08745Sheppo status, msglen); 15571ae08745Sheppo if (msglen != sizeof (vio_ver_msg_t)) 15581ae08745Sheppo status = ENOMSG; 15591ae08745Sheppo } 15601ae08745Sheppo 15611ae08745Sheppo return (status); 15621ae08745Sheppo } 15631ae08745Sheppo 15640a55fbb7Slm66018 /* 15650a55fbb7Slm66018 * Function: 15663af08d82Slm66018 * vdc_attr_negotiation() 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_attr_negotiation(vdc_t *vdcp) 15783af08d82Slm66018 { 15793af08d82Slm66018 int status; 15803af08d82Slm66018 vio_msg_t vio_msg; 15813af08d82Slm66018 15823af08d82Slm66018 if (status = vdc_init_attr_negotiation(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 Attr negotiation response, rv(%d)", 15923af08d82Slm66018 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_INFO) { 15993af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n", 16003af08d82Slm66018 vdcp->instance); 16013af08d82Slm66018 return (EPROTO); 16023af08d82Slm66018 } 16033af08d82Slm66018 16043af08d82Slm66018 return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg)); 16053af08d82Slm66018 } 16063af08d82Slm66018 16073af08d82Slm66018 16083af08d82Slm66018 /* 16093af08d82Slm66018 * Function: 16100a55fbb7Slm66018 * vdc_init_dring_negotiate() 16110a55fbb7Slm66018 * 16120a55fbb7Slm66018 * Description: 16130a55fbb7Slm66018 * 16140a55fbb7Slm66018 * Arguments: 16150a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 16160a55fbb7Slm66018 * 16170a55fbb7Slm66018 * Return Code: 16180a55fbb7Slm66018 * 0 - Success 16190a55fbb7Slm66018 */ 16201ae08745Sheppo static int 16211ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc) 16221ae08745Sheppo { 16231ae08745Sheppo vio_dring_reg_msg_t pkt; 16241ae08745Sheppo size_t msglen = sizeof (pkt); 16251ae08745Sheppo int status = -1; 16263af08d82Slm66018 int retry; 16273af08d82Slm66018 int nretries = 10; 16281ae08745Sheppo 16291ae08745Sheppo ASSERT(vdc != NULL); 16301ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 16311ae08745Sheppo 16323af08d82Slm66018 for (retry = 0; retry < nretries; retry++) { 16331ae08745Sheppo status = vdc_init_descriptor_ring(vdc); 16343af08d82Slm66018 if (status != EAGAIN) 16353af08d82Slm66018 break; 16363af08d82Slm66018 drv_usecwait(vdc_min_timeout_ldc); 16373af08d82Slm66018 } 16383af08d82Slm66018 16391ae08745Sheppo if (status != 0) { 16403af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n", 16411ae08745Sheppo vdc->instance, status); 16421ae08745Sheppo return (status); 16431ae08745Sheppo } 16443af08d82Slm66018 16453af08d82Slm66018 DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n", 1646e1ebb9ecSlm66018 vdc->instance, status); 16471ae08745Sheppo 16481ae08745Sheppo /* fill in tag */ 16491ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 16501ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 16511ae08745Sheppo pkt.tag.vio_subtype_env = VIO_DRING_REG; 16521ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 16531ae08745Sheppo /* fill in payload */ 16541ae08745Sheppo pkt.dring_ident = 0; 1655e1ebb9ecSlm66018 pkt.num_descriptors = vdc->dring_len; 1656e1ebb9ecSlm66018 pkt.descriptor_size = vdc->dring_entry_size; 16571ae08745Sheppo pkt.options = (VIO_TX_DRING | VIO_RX_DRING); 16581ae08745Sheppo pkt.ncookies = vdc->dring_cookie_count; 16591ae08745Sheppo pkt.cookie[0] = vdc->dring_cookie[0]; /* for now just one cookie */ 16601ae08745Sheppo 16610a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 16621ae08745Sheppo if (status != 0) { 16633af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)", 1664e1ebb9ecSlm66018 vdc->instance, status); 16651ae08745Sheppo } 16661ae08745Sheppo 16671ae08745Sheppo return (status); 16681ae08745Sheppo } 16691ae08745Sheppo 16701ae08745Sheppo 16713af08d82Slm66018 /* 16723af08d82Slm66018 * Function: 16733af08d82Slm66018 * vdc_dring_negotiation() 16743af08d82Slm66018 * 16753af08d82Slm66018 * Description: 16763af08d82Slm66018 * 16773af08d82Slm66018 * Arguments: 16783af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 16793af08d82Slm66018 * 16803af08d82Slm66018 * Return Code: 16813af08d82Slm66018 * 0 - Success 16823af08d82Slm66018 */ 16833af08d82Slm66018 static int 16843af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp) 16853af08d82Slm66018 { 16863af08d82Slm66018 int status; 16873af08d82Slm66018 vio_msg_t vio_msg; 16883af08d82Slm66018 16893af08d82Slm66018 if (status = vdc_init_dring_negotiate(vdcp)) 16903af08d82Slm66018 return (status); 16913af08d82Slm66018 16923af08d82Slm66018 /* release lock and wait for response */ 16933af08d82Slm66018 mutex_exit(&vdcp->lock); 16943af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 16953af08d82Slm66018 mutex_enter(&vdcp->lock); 16963af08d82Slm66018 if (status) { 16973af08d82Slm66018 DMSG(vdcp, 0, 16983af08d82Slm66018 "[%d] Failed waiting for Dring negotiation response," 16993af08d82Slm66018 " rv(%d)", vdcp->instance, status); 17003af08d82Slm66018 return (status); 17013af08d82Slm66018 } 17023af08d82Slm66018 17033af08d82Slm66018 /* check type and sub_type ... */ 17043af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 17053af08d82Slm66018 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 17063af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n", 17073af08d82Slm66018 vdcp->instance); 17083af08d82Slm66018 return (EPROTO); 17093af08d82Slm66018 } 17103af08d82Slm66018 17113af08d82Slm66018 return (vdc_handle_dring_reg_msg(vdcp, 17123af08d82Slm66018 (vio_dring_reg_msg_t *)&vio_msg)); 17133af08d82Slm66018 } 17143af08d82Slm66018 17153af08d82Slm66018 17163af08d82Slm66018 /* 17173af08d82Slm66018 * Function: 17183af08d82Slm66018 * vdc_send_rdx() 17193af08d82Slm66018 * 17203af08d82Slm66018 * Description: 17213af08d82Slm66018 * 17223af08d82Slm66018 * Arguments: 17233af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 17243af08d82Slm66018 * 17253af08d82Slm66018 * Return Code: 17263af08d82Slm66018 * 0 - Success 17273af08d82Slm66018 */ 17283af08d82Slm66018 static int 17293af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp) 17303af08d82Slm66018 { 17313af08d82Slm66018 vio_msg_t msg; 17323af08d82Slm66018 size_t msglen = sizeof (vio_msg_t); 17333af08d82Slm66018 int status; 17343af08d82Slm66018 17353af08d82Slm66018 /* 17363af08d82Slm66018 * Send an RDX message to vds to indicate we are ready 17373af08d82Slm66018 * to send data 17383af08d82Slm66018 */ 17393af08d82Slm66018 msg.tag.vio_msgtype = VIO_TYPE_CTRL; 17403af08d82Slm66018 msg.tag.vio_subtype = VIO_SUBTYPE_INFO; 17413af08d82Slm66018 msg.tag.vio_subtype_env = VIO_RDX; 17423af08d82Slm66018 msg.tag.vio_sid = vdcp->session_id; 17433af08d82Slm66018 status = vdc_send(vdcp, (caddr_t)&msg, &msglen); 17443af08d82Slm66018 if (status != 0) { 17453af08d82Slm66018 DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)", 17463af08d82Slm66018 vdcp->instance, status); 17473af08d82Slm66018 } 17483af08d82Slm66018 17493af08d82Slm66018 return (status); 17503af08d82Slm66018 } 17513af08d82Slm66018 17523af08d82Slm66018 /* 17533af08d82Slm66018 * Function: 17543af08d82Slm66018 * vdc_handle_rdx() 17553af08d82Slm66018 * 17563af08d82Slm66018 * Description: 17573af08d82Slm66018 * 17583af08d82Slm66018 * Arguments: 17593af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 17603af08d82Slm66018 * msgp - received msg 17613af08d82Slm66018 * 17623af08d82Slm66018 * Return Code: 17633af08d82Slm66018 * 0 - Success 17643af08d82Slm66018 */ 17653af08d82Slm66018 static int 17663af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp) 17673af08d82Slm66018 { 17683af08d82Slm66018 _NOTE(ARGUNUSED(vdcp)) 17693af08d82Slm66018 _NOTE(ARGUNUSED(msgp)) 17703af08d82Slm66018 17713af08d82Slm66018 ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL); 17723af08d82Slm66018 ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK); 17733af08d82Slm66018 ASSERT(msgp->tag.vio_subtype_env == VIO_RDX); 17743af08d82Slm66018 17753af08d82Slm66018 DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance); 17763af08d82Slm66018 17773af08d82Slm66018 return (0); 17783af08d82Slm66018 } 17793af08d82Slm66018 17803af08d82Slm66018 /* 17813af08d82Slm66018 * Function: 17823af08d82Slm66018 * vdc_rdx_exchange() 17833af08d82Slm66018 * 17843af08d82Slm66018 * Description: 17853af08d82Slm66018 * 17863af08d82Slm66018 * Arguments: 17873af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 17883af08d82Slm66018 * 17893af08d82Slm66018 * Return Code: 17903af08d82Slm66018 * 0 - Success 17913af08d82Slm66018 */ 17923af08d82Slm66018 static int 17933af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp) 17943af08d82Slm66018 { 17953af08d82Slm66018 int status; 17963af08d82Slm66018 vio_msg_t vio_msg; 17973af08d82Slm66018 17983af08d82Slm66018 if (status = vdc_send_rdx(vdcp)) 17993af08d82Slm66018 return (status); 18003af08d82Slm66018 18013af08d82Slm66018 /* release lock and wait for response */ 18023af08d82Slm66018 mutex_exit(&vdcp->lock); 18033af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 18043af08d82Slm66018 mutex_enter(&vdcp->lock); 18053af08d82Slm66018 if (status) { 180687a7269eSachartre DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)", 180787a7269eSachartre vdcp->instance, status); 18083af08d82Slm66018 return (status); 18093af08d82Slm66018 } 18103af08d82Slm66018 18113af08d82Slm66018 /* check type and sub_type ... */ 18123af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 18133af08d82Slm66018 vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) { 181487a7269eSachartre DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance); 18153af08d82Slm66018 return (EPROTO); 18163af08d82Slm66018 } 18173af08d82Slm66018 18183af08d82Slm66018 return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg)); 18193af08d82Slm66018 } 18203af08d82Slm66018 18213af08d82Slm66018 18221ae08745Sheppo /* -------------------------------------------------------------------------- */ 18231ae08745Sheppo 18241ae08745Sheppo /* 18251ae08745Sheppo * LDC helper routines 18261ae08745Sheppo */ 18271ae08745Sheppo 18283af08d82Slm66018 static int 18293af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp) 18303af08d82Slm66018 { 18313af08d82Slm66018 int status; 18323af08d82Slm66018 boolean_t q_has_pkts = B_FALSE; 1833*17cadca8Slm66018 uint64_t delay_time; 18343af08d82Slm66018 size_t len; 18353af08d82Slm66018 18363af08d82Slm66018 mutex_enter(&vdc->read_lock); 18373af08d82Slm66018 18383af08d82Slm66018 if (vdc->read_state == VDC_READ_IDLE) 18393af08d82Slm66018 vdc->read_state = VDC_READ_WAITING; 18403af08d82Slm66018 18413af08d82Slm66018 while (vdc->read_state != VDC_READ_PENDING) { 18423af08d82Slm66018 18433af08d82Slm66018 /* detect if the connection has been reset */ 18443af08d82Slm66018 if (vdc->read_state == VDC_READ_RESET) { 18453af08d82Slm66018 status = ECONNRESET; 18463af08d82Slm66018 goto done; 18473af08d82Slm66018 } 18483af08d82Slm66018 18493af08d82Slm66018 cv_wait(&vdc->read_cv, &vdc->read_lock); 18503af08d82Slm66018 } 18513af08d82Slm66018 18523af08d82Slm66018 /* 18533af08d82Slm66018 * Until we get a blocking ldc read we have to retry 18543af08d82Slm66018 * until the entire LDC message has arrived before 18553af08d82Slm66018 * ldc_read() will succeed. Note we also bail out if 1856eff7243fSlm66018 * the channel is reset or goes away. 18573af08d82Slm66018 */ 18583af08d82Slm66018 delay_time = vdc_ldc_read_init_delay; 18593af08d82Slm66018 loop: 18603af08d82Slm66018 len = *nbytesp; 18613af08d82Slm66018 status = ldc_read(vdc->ldc_handle, (caddr_t)msgp, &len); 18623af08d82Slm66018 switch (status) { 18633af08d82Slm66018 case EAGAIN: 18643af08d82Slm66018 delay_time *= 2; 18653af08d82Slm66018 if (delay_time >= vdc_ldc_read_max_delay) 18663af08d82Slm66018 delay_time = vdc_ldc_read_max_delay; 18673af08d82Slm66018 delay(delay_time); 18683af08d82Slm66018 goto loop; 18693af08d82Slm66018 18703af08d82Slm66018 case 0: 18713af08d82Slm66018 if (len == 0) { 1872*17cadca8Slm66018 DMSG(vdc, 1, "[%d] ldc_read returned 0 bytes with " 18733af08d82Slm66018 "no error!\n", vdc->instance); 18743af08d82Slm66018 goto loop; 18753af08d82Slm66018 } 18763af08d82Slm66018 18773af08d82Slm66018 *nbytesp = len; 18783af08d82Slm66018 18793af08d82Slm66018 /* 18803af08d82Slm66018 * If there are pending messages, leave the 18813af08d82Slm66018 * read state as pending. Otherwise, set the state 18823af08d82Slm66018 * back to idle. 18833af08d82Slm66018 */ 18843af08d82Slm66018 status = ldc_chkq(vdc->ldc_handle, &q_has_pkts); 18853af08d82Slm66018 if (status == 0 && !q_has_pkts) 18863af08d82Slm66018 vdc->read_state = VDC_READ_IDLE; 18873af08d82Slm66018 18883af08d82Slm66018 break; 18893af08d82Slm66018 default: 18903af08d82Slm66018 DMSG(vdc, 0, "ldc_read returned %d\n", status); 18913af08d82Slm66018 break; 18923af08d82Slm66018 } 18933af08d82Slm66018 18943af08d82Slm66018 done: 18953af08d82Slm66018 mutex_exit(&vdc->read_lock); 18963af08d82Slm66018 18973af08d82Slm66018 return (status); 18983af08d82Slm66018 } 18993af08d82Slm66018 19003af08d82Slm66018 19013af08d82Slm66018 19023af08d82Slm66018 #ifdef DEBUG 19033af08d82Slm66018 void 19043af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg) 19053af08d82Slm66018 { 19063af08d82Slm66018 char *ms, *ss, *ses; 19073af08d82Slm66018 switch (msg->tag.vio_msgtype) { 19083af08d82Slm66018 #define Q(_s) case _s : ms = #_s; break; 19093af08d82Slm66018 Q(VIO_TYPE_CTRL) 19103af08d82Slm66018 Q(VIO_TYPE_DATA) 19113af08d82Slm66018 Q(VIO_TYPE_ERR) 19123af08d82Slm66018 #undef Q 19133af08d82Slm66018 default: ms = "unknown"; break; 19143af08d82Slm66018 } 19153af08d82Slm66018 19163af08d82Slm66018 switch (msg->tag.vio_subtype) { 19173af08d82Slm66018 #define Q(_s) case _s : ss = #_s; break; 19183af08d82Slm66018 Q(VIO_SUBTYPE_INFO) 19193af08d82Slm66018 Q(VIO_SUBTYPE_ACK) 19203af08d82Slm66018 Q(VIO_SUBTYPE_NACK) 19213af08d82Slm66018 #undef Q 19223af08d82Slm66018 default: ss = "unknown"; break; 19233af08d82Slm66018 } 19243af08d82Slm66018 19253af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 19263af08d82Slm66018 #define Q(_s) case _s : ses = #_s; break; 19273af08d82Slm66018 Q(VIO_VER_INFO) 19283af08d82Slm66018 Q(VIO_ATTR_INFO) 19293af08d82Slm66018 Q(VIO_DRING_REG) 19303af08d82Slm66018 Q(VIO_DRING_UNREG) 19313af08d82Slm66018 Q(VIO_RDX) 19323af08d82Slm66018 Q(VIO_PKT_DATA) 19333af08d82Slm66018 Q(VIO_DESC_DATA) 19343af08d82Slm66018 Q(VIO_DRING_DATA) 19353af08d82Slm66018 #undef Q 19363af08d82Slm66018 default: ses = "unknown"; break; 19373af08d82Slm66018 } 19383af08d82Slm66018 19393af08d82Slm66018 DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n", 19403af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 19413af08d82Slm66018 msg->tag.vio_subtype_env, ms, ss, ses); 19423af08d82Slm66018 } 19433af08d82Slm66018 #endif 19443af08d82Slm66018 19451ae08745Sheppo /* 19461ae08745Sheppo * Function: 19471ae08745Sheppo * vdc_send() 19481ae08745Sheppo * 19491ae08745Sheppo * Description: 19501ae08745Sheppo * The function encapsulates the call to write a message using LDC. 19511ae08745Sheppo * If LDC indicates that the call failed due to the queue being full, 1952*17cadca8Slm66018 * we retry the ldc_write(), otherwise we return the error returned by LDC. 19531ae08745Sheppo * 19541ae08745Sheppo * Arguments: 19551ae08745Sheppo * ldc_handle - LDC handle for the channel this instance of vdc uses 19561ae08745Sheppo * pkt - address of LDC message to be sent 19571ae08745Sheppo * msglen - the size of the message being sent. When the function 19581ae08745Sheppo * returns, this contains the number of bytes written. 19591ae08745Sheppo * 19601ae08745Sheppo * Return Code: 19611ae08745Sheppo * 0 - Success. 19621ae08745Sheppo * EINVAL - pkt or msglen were NULL 19631ae08745Sheppo * ECONNRESET - The connection was not up. 19641ae08745Sheppo * EWOULDBLOCK - LDC queue is full 19651ae08745Sheppo * xxx - other error codes returned by ldc_write 19661ae08745Sheppo */ 19671ae08745Sheppo static int 19680a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen) 19691ae08745Sheppo { 19701ae08745Sheppo size_t size = 0; 19711ae08745Sheppo int status = 0; 19723af08d82Slm66018 clock_t delay_ticks; 19731ae08745Sheppo 19740a55fbb7Slm66018 ASSERT(vdc != NULL); 19750a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 19761ae08745Sheppo ASSERT(msglen != NULL); 19771ae08745Sheppo ASSERT(*msglen != 0); 19781ae08745Sheppo 19793af08d82Slm66018 #ifdef DEBUG 1980*17cadca8Slm66018 vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt); 19813af08d82Slm66018 #endif 19823af08d82Slm66018 /* 19833af08d82Slm66018 * Wait indefinitely to send if channel 19843af08d82Slm66018 * is busy, but bail out if we succeed or 19853af08d82Slm66018 * if the channel closes or is reset. 19863af08d82Slm66018 */ 19873af08d82Slm66018 delay_ticks = vdc_hz_min_ldc_delay; 19881ae08745Sheppo do { 19891ae08745Sheppo size = *msglen; 19900a55fbb7Slm66018 status = ldc_write(vdc->ldc_handle, pkt, &size); 19913af08d82Slm66018 if (status == EWOULDBLOCK) { 19923af08d82Slm66018 delay(delay_ticks); 19933af08d82Slm66018 /* geometric backoff */ 19943af08d82Slm66018 delay_ticks *= 2; 19953af08d82Slm66018 if (delay_ticks > vdc_hz_max_ldc_delay) 19963af08d82Slm66018 delay_ticks = vdc_hz_max_ldc_delay; 19973af08d82Slm66018 } 19983af08d82Slm66018 } while (status == EWOULDBLOCK); 19991ae08745Sheppo 20000a55fbb7Slm66018 /* if LDC had serious issues --- reset vdc state */ 20010a55fbb7Slm66018 if (status == EIO || status == ECONNRESET) { 20023af08d82Slm66018 /* LDC had serious issues --- reset vdc state */ 20033af08d82Slm66018 mutex_enter(&vdc->read_lock); 20043af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 20053af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) 20063af08d82Slm66018 cv_signal(&vdc->read_cv); 20073af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 20083af08d82Slm66018 mutex_exit(&vdc->read_lock); 20093af08d82Slm66018 20103af08d82Slm66018 /* wake up any waiters in the reset thread */ 20113af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 20123af08d82Slm66018 DMSG(vdc, 0, "[%d] write reset - " 20133af08d82Slm66018 "vdc is resetting ..\n", vdc->instance); 20143af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 20153af08d82Slm66018 cv_signal(&vdc->initwait_cv); 20163af08d82Slm66018 } 20173af08d82Slm66018 20183af08d82Slm66018 return (ECONNRESET); 20190a55fbb7Slm66018 } 20200a55fbb7Slm66018 20211ae08745Sheppo /* return the last size written */ 20221ae08745Sheppo *msglen = size; 20231ae08745Sheppo 20241ae08745Sheppo return (status); 20251ae08745Sheppo } 20261ae08745Sheppo 20271ae08745Sheppo /* 20281ae08745Sheppo * Function: 2029655fd6a9Sachartre * vdc_get_md_node 20301ae08745Sheppo * 20311ae08745Sheppo * Description: 2032655fd6a9Sachartre * Get the MD, the device node and the port node for the given 2033655fd6a9Sachartre * disk instance. The caller is responsible for cleaning up the 2034655fd6a9Sachartre * reference to the returned MD (mdpp) by calling md_fini_handle(). 20351ae08745Sheppo * 20361ae08745Sheppo * Arguments: 20371ae08745Sheppo * dip - dev info pointer for this instance of the device driver. 2038655fd6a9Sachartre * mdpp - the returned MD. 2039655fd6a9Sachartre * vd_nodep - the returned device node. 2040655fd6a9Sachartre * vd_portp - the returned port node. The returned port node is NULL 2041655fd6a9Sachartre * if no port node is found. 20421ae08745Sheppo * 20431ae08745Sheppo * Return Code: 20441ae08745Sheppo * 0 - Success. 20451ae08745Sheppo * ENOENT - Expected node or property did not exist. 20461ae08745Sheppo * ENXIO - Unexpected error communicating with MD framework 20471ae08745Sheppo */ 20481ae08745Sheppo static int 2049655fd6a9Sachartre vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep, 2050655fd6a9Sachartre mde_cookie_t *vd_portp) 20511ae08745Sheppo { 20521ae08745Sheppo int status = ENOENT; 20531ae08745Sheppo char *node_name = NULL; 20541ae08745Sheppo md_t *mdp = NULL; 20551ae08745Sheppo int num_nodes; 20561ae08745Sheppo int num_vdevs; 2057655fd6a9Sachartre int num_vports; 20581ae08745Sheppo mde_cookie_t rootnode; 20591ae08745Sheppo mde_cookie_t *listp = NULL; 20601ae08745Sheppo boolean_t found_inst = B_FALSE; 20611ae08745Sheppo int listsz; 20621ae08745Sheppo int idx; 20631ae08745Sheppo uint64_t md_inst; 20641ae08745Sheppo int obp_inst; 20651ae08745Sheppo int instance = ddi_get_instance(dip); 20661ae08745Sheppo 20671ae08745Sheppo /* 20681ae08745Sheppo * Get the OBP instance number for comparison with the MD instance 20691ae08745Sheppo * 20701ae08745Sheppo * The "cfg-handle" property of a vdc node in an MD contains the MD's 20711ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 20721ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 20731ae08745Sheppo * the "reg" property on the node in the device tree it builds from 20741ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 20751ae08745Sheppo * "reg" property value to uniquely identify this device instance. 20761ae08745Sheppo * If the "reg" property cannot be found, the device tree state is 20771ae08745Sheppo * presumably so broken that there is no point in continuing. 20781ae08745Sheppo */ 20791ae08745Sheppo if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) { 20801ae08745Sheppo cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG); 20811ae08745Sheppo return (ENOENT); 20821ae08745Sheppo } 20831ae08745Sheppo obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 20841ae08745Sheppo OBP_REG, -1); 20853af08d82Slm66018 DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst); 20861ae08745Sheppo 20871ae08745Sheppo /* 2088655fd6a9Sachartre * We now walk the MD nodes to find the node for this vdisk. 20891ae08745Sheppo */ 20901ae08745Sheppo if ((mdp = md_get_handle()) == NULL) { 20911ae08745Sheppo cmn_err(CE_WARN, "unable to init machine description"); 20921ae08745Sheppo return (ENXIO); 20931ae08745Sheppo } 20941ae08745Sheppo 20951ae08745Sheppo num_nodes = md_node_count(mdp); 20961ae08745Sheppo ASSERT(num_nodes > 0); 20971ae08745Sheppo 20981ae08745Sheppo listsz = num_nodes * sizeof (mde_cookie_t); 20991ae08745Sheppo 21001ae08745Sheppo /* allocate memory for nodes */ 21011ae08745Sheppo listp = kmem_zalloc(listsz, KM_SLEEP); 21021ae08745Sheppo 21031ae08745Sheppo rootnode = md_root_node(mdp); 21041ae08745Sheppo ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE); 21051ae08745Sheppo 21061ae08745Sheppo /* 21071ae08745Sheppo * Search for all the virtual devices, we will then check to see which 21081ae08745Sheppo * ones are disk nodes. 21091ae08745Sheppo */ 21101ae08745Sheppo num_vdevs = md_scan_dag(mdp, rootnode, 21111ae08745Sheppo md_find_name(mdp, VDC_MD_VDEV_NAME), 21121ae08745Sheppo md_find_name(mdp, "fwd"), listp); 21131ae08745Sheppo 21141ae08745Sheppo if (num_vdevs <= 0) { 21151ae08745Sheppo cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME); 21161ae08745Sheppo status = ENOENT; 21171ae08745Sheppo goto done; 21181ae08745Sheppo } 21191ae08745Sheppo 21203af08d82Slm66018 DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs); 21211ae08745Sheppo for (idx = 0; idx < num_vdevs; idx++) { 21221ae08745Sheppo status = md_get_prop_str(mdp, listp[idx], "name", &node_name); 21231ae08745Sheppo if ((status != 0) || (node_name == NULL)) { 21241ae08745Sheppo cmn_err(CE_NOTE, "Unable to get name of node type '%s'" 21251ae08745Sheppo ": err %d", VDC_MD_VDEV_NAME, status); 21261ae08745Sheppo continue; 21271ae08745Sheppo } 21281ae08745Sheppo 21293af08d82Slm66018 DMSGX(1, "[%d] Found node '%s'\n", instance, node_name); 21301ae08745Sheppo if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) { 21311ae08745Sheppo status = md_get_prop_val(mdp, listp[idx], 21321ae08745Sheppo VDC_MD_CFG_HDL, &md_inst); 21333af08d82Slm66018 DMSGX(1, "[%d] vdc inst in MD=%lx\n", 21343af08d82Slm66018 instance, md_inst); 21351ae08745Sheppo if ((status == 0) && (md_inst == obp_inst)) { 21361ae08745Sheppo found_inst = B_TRUE; 21371ae08745Sheppo break; 21381ae08745Sheppo } 21391ae08745Sheppo } 21401ae08745Sheppo } 21411ae08745Sheppo 21420a55fbb7Slm66018 if (!found_inst) { 21433af08d82Slm66018 DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME); 21441ae08745Sheppo status = ENOENT; 21451ae08745Sheppo goto done; 21461ae08745Sheppo } 21473af08d82Slm66018 DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst); 21481ae08745Sheppo 2149655fd6a9Sachartre *vd_nodep = listp[idx]; 2150655fd6a9Sachartre *mdpp = mdp; 2151655fd6a9Sachartre 2152655fd6a9Sachartre num_vports = md_scan_dag(mdp, *vd_nodep, 2153655fd6a9Sachartre md_find_name(mdp, VDC_MD_PORT_NAME), 2154655fd6a9Sachartre md_find_name(mdp, "fwd"), listp); 2155655fd6a9Sachartre 2156655fd6a9Sachartre if (num_vports != 1) { 2157655fd6a9Sachartre DMSGX(0, "Expected 1 '%s' node for '%s' port, found %d\n", 2158655fd6a9Sachartre VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME, num_vports); 2159655fd6a9Sachartre } 2160655fd6a9Sachartre 2161655fd6a9Sachartre *vd_portp = (num_vports == 0)? NULL: listp[0]; 2162655fd6a9Sachartre 2163655fd6a9Sachartre done: 2164655fd6a9Sachartre kmem_free(listp, listsz); 2165655fd6a9Sachartre return (status); 2166655fd6a9Sachartre } 2167655fd6a9Sachartre 2168655fd6a9Sachartre /* 2169655fd6a9Sachartre * Function: 2170655fd6a9Sachartre * vdc_get_ldc_id() 2171655fd6a9Sachartre * 2172655fd6a9Sachartre * Description: 2173655fd6a9Sachartre * This function gets the 'ldc-id' for this particular instance of vdc. 2174655fd6a9Sachartre * The id returned is the guest domain channel endpoint LDC uses for 2175655fd6a9Sachartre * communication with vds. 2176655fd6a9Sachartre * 2177655fd6a9Sachartre * Arguments: 2178655fd6a9Sachartre * mdp - pointer to the machine description. 2179655fd6a9Sachartre * vd_node - the vdisk element from the MD. 2180655fd6a9Sachartre * ldc_id - pointer to variable used to return the 'ldc-id' found. 2181655fd6a9Sachartre * 2182655fd6a9Sachartre * Return Code: 2183655fd6a9Sachartre * 0 - Success. 2184655fd6a9Sachartre * ENOENT - Expected node or property did not exist. 2185655fd6a9Sachartre */ 2186655fd6a9Sachartre static int 2187655fd6a9Sachartre vdc_get_ldc_id(md_t *mdp, mde_cookie_t vd_node, uint64_t *ldc_id) 2188655fd6a9Sachartre { 2189655fd6a9Sachartre mde_cookie_t *chanp = NULL; 2190655fd6a9Sachartre int listsz; 2191655fd6a9Sachartre int num_chans; 2192655fd6a9Sachartre int num_nodes; 2193655fd6a9Sachartre int status = 0; 2194655fd6a9Sachartre 2195655fd6a9Sachartre num_nodes = md_node_count(mdp); 2196655fd6a9Sachartre ASSERT(num_nodes > 0); 2197655fd6a9Sachartre 2198655fd6a9Sachartre listsz = num_nodes * sizeof (mde_cookie_t); 2199655fd6a9Sachartre 2200655fd6a9Sachartre /* allocate memory for nodes */ 2201655fd6a9Sachartre chanp = kmem_zalloc(listsz, KM_SLEEP); 2202655fd6a9Sachartre 22031ae08745Sheppo /* get the channels for this node */ 2204655fd6a9Sachartre num_chans = md_scan_dag(mdp, vd_node, 22051ae08745Sheppo md_find_name(mdp, VDC_MD_CHAN_NAME), 22061ae08745Sheppo md_find_name(mdp, "fwd"), chanp); 22071ae08745Sheppo 22081ae08745Sheppo /* expecting at least one channel */ 22091ae08745Sheppo if (num_chans <= 0) { 22101ae08745Sheppo cmn_err(CE_NOTE, "No '%s' node for '%s' port", 22111ae08745Sheppo VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME); 22121ae08745Sheppo status = ENOENT; 22131ae08745Sheppo goto done; 22141ae08745Sheppo 22151ae08745Sheppo } else if (num_chans != 1) { 2216655fd6a9Sachartre DMSGX(0, "Expected 1 '%s' node for '%s' port, found %d\n", 2217655fd6a9Sachartre VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME, num_chans); 22181ae08745Sheppo } 22191ae08745Sheppo 22201ae08745Sheppo /* 22211ae08745Sheppo * We use the first channel found (index 0), irrespective of how 22221ae08745Sheppo * many are there in total. 22231ae08745Sheppo */ 2224655fd6a9Sachartre if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID, ldc_id) != 0) { 2225655fd6a9Sachartre cmn_err(CE_NOTE, "Channel '%s' property not found", VDC_MD_ID); 22261ae08745Sheppo status = ENOENT; 22271ae08745Sheppo } 22281ae08745Sheppo 22291ae08745Sheppo done: 22301ae08745Sheppo kmem_free(chanp, listsz); 22311ae08745Sheppo return (status); 22321ae08745Sheppo } 22331ae08745Sheppo 22340a55fbb7Slm66018 static int 22350a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc) 22360a55fbb7Slm66018 { 22370a55fbb7Slm66018 int status; 22383af08d82Slm66018 ldc_status_t ldc_state; 22390a55fbb7Slm66018 22403af08d82Slm66018 DMSG(vdc, 0, "[%d] Bringing up channel %lx\n", 22413af08d82Slm66018 vdc->instance, vdc->ldc_id); 22423af08d82Slm66018 22433af08d82Slm66018 if (vdc->lifecycle == VDC_LC_DETACHING) 22443af08d82Slm66018 return (EINVAL); 22450a55fbb7Slm66018 22460a55fbb7Slm66018 if ((status = ldc_up(vdc->ldc_handle)) != 0) { 22470a55fbb7Slm66018 switch (status) { 22480a55fbb7Slm66018 case ECONNREFUSED: /* listener not ready at other end */ 22493af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n", 2250e1ebb9ecSlm66018 vdc->instance, vdc->ldc_id, status); 22510a55fbb7Slm66018 status = 0; 22520a55fbb7Slm66018 break; 22530a55fbb7Slm66018 default: 22543af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to bring up LDC: " 22553af08d82Slm66018 "channel=%ld, err=%d", vdc->instance, vdc->ldc_id, 22563af08d82Slm66018 status); 22573af08d82Slm66018 break; 22583af08d82Slm66018 } 22593af08d82Slm66018 } 22603af08d82Slm66018 22613af08d82Slm66018 if (ldc_status(vdc->ldc_handle, &ldc_state) == 0) { 22623af08d82Slm66018 vdc->ldc_state = ldc_state; 22633af08d82Slm66018 if (ldc_state == LDC_UP) { 22643af08d82Slm66018 DMSG(vdc, 0, "[%d] LDC channel already up\n", 22653af08d82Slm66018 vdc->instance); 22663af08d82Slm66018 vdc->seq_num = 1; 22673af08d82Slm66018 vdc->seq_num_reply = 0; 22680a55fbb7Slm66018 } 22690a55fbb7Slm66018 } 22700a55fbb7Slm66018 22710a55fbb7Slm66018 return (status); 22720a55fbb7Slm66018 } 22730a55fbb7Slm66018 22740a55fbb7Slm66018 /* 22750a55fbb7Slm66018 * Function: 22760a55fbb7Slm66018 * vdc_terminate_ldc() 22770a55fbb7Slm66018 * 22780a55fbb7Slm66018 * Description: 22790a55fbb7Slm66018 * 22800a55fbb7Slm66018 * Arguments: 22810a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 22820a55fbb7Slm66018 * 22830a55fbb7Slm66018 * Return Code: 22840a55fbb7Slm66018 * None 22850a55fbb7Slm66018 */ 22861ae08745Sheppo static void 22871ae08745Sheppo vdc_terminate_ldc(vdc_t *vdc) 22881ae08745Sheppo { 22891ae08745Sheppo int instance = ddi_get_instance(vdc->dip); 22901ae08745Sheppo 22911ae08745Sheppo ASSERT(vdc != NULL); 22921ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 22931ae08745Sheppo 22943af08d82Slm66018 DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized); 22951ae08745Sheppo 22961ae08745Sheppo if (vdc->initialized & VDC_LDC_OPEN) { 22973af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_close()\n", instance); 22981ae08745Sheppo (void) ldc_close(vdc->ldc_handle); 22991ae08745Sheppo } 23001ae08745Sheppo if (vdc->initialized & VDC_LDC_CB) { 23013af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance); 23021ae08745Sheppo (void) ldc_unreg_callback(vdc->ldc_handle); 23031ae08745Sheppo } 23041ae08745Sheppo if (vdc->initialized & VDC_LDC) { 23053af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_fini()\n", instance); 23061ae08745Sheppo (void) ldc_fini(vdc->ldc_handle); 23071ae08745Sheppo vdc->ldc_handle = NULL; 23081ae08745Sheppo } 23091ae08745Sheppo 23101ae08745Sheppo vdc->initialized &= ~(VDC_LDC | VDC_LDC_CB | VDC_LDC_OPEN); 23111ae08745Sheppo } 23121ae08745Sheppo 23131ae08745Sheppo /* -------------------------------------------------------------------------- */ 23141ae08745Sheppo 23151ae08745Sheppo /* 23161ae08745Sheppo * Descriptor Ring helper routines 23171ae08745Sheppo */ 23181ae08745Sheppo 23190a55fbb7Slm66018 /* 23200a55fbb7Slm66018 * Function: 23210a55fbb7Slm66018 * vdc_init_descriptor_ring() 23220a55fbb7Slm66018 * 23230a55fbb7Slm66018 * Description: 23240a55fbb7Slm66018 * 23250a55fbb7Slm66018 * Arguments: 23260a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 23270a55fbb7Slm66018 * 23280a55fbb7Slm66018 * Return Code: 23290a55fbb7Slm66018 * 0 - Success 23300a55fbb7Slm66018 */ 23311ae08745Sheppo static int 23321ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc) 23331ae08745Sheppo { 23341ae08745Sheppo vd_dring_entry_t *dep = NULL; /* DRing Entry pointer */ 23350a55fbb7Slm66018 int status = 0; 23361ae08745Sheppo int i; 23371ae08745Sheppo 23383af08d82Slm66018 DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized); 23391ae08745Sheppo 23401ae08745Sheppo ASSERT(vdc != NULL); 23411ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 23421ae08745Sheppo ASSERT(vdc->ldc_handle != NULL); 23431ae08745Sheppo 2344e1ebb9ecSlm66018 /* ensure we have enough room to store max sized block */ 2345e1ebb9ecSlm66018 ASSERT(maxphys <= VD_MAX_BLOCK_SIZE); 2346e1ebb9ecSlm66018 23470a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_INIT) == 0) { 23483af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance); 2349e1ebb9ecSlm66018 /* 2350e1ebb9ecSlm66018 * Calculate the maximum block size we can transmit using one 2351e1ebb9ecSlm66018 * Descriptor Ring entry from the attributes returned by the 2352e1ebb9ecSlm66018 * vDisk server. This is subject to a minimum of 'maxphys' 2353e1ebb9ecSlm66018 * as we do not have the capability to split requests over 2354e1ebb9ecSlm66018 * multiple DRing entries. 2355e1ebb9ecSlm66018 */ 2356e1ebb9ecSlm66018 if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) { 23573af08d82Slm66018 DMSG(vdc, 0, "[%d] using minimum DRing size\n", 2358e1ebb9ecSlm66018 vdc->instance); 2359e1ebb9ecSlm66018 vdc->dring_max_cookies = maxphys / PAGESIZE; 2360e1ebb9ecSlm66018 } else { 2361e1ebb9ecSlm66018 vdc->dring_max_cookies = 2362e1ebb9ecSlm66018 (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE; 2363e1ebb9ecSlm66018 } 2364e1ebb9ecSlm66018 vdc->dring_entry_size = (sizeof (vd_dring_entry_t) + 2365e1ebb9ecSlm66018 (sizeof (ldc_mem_cookie_t) * 2366e1ebb9ecSlm66018 (vdc->dring_max_cookies - 1))); 2367e1ebb9ecSlm66018 vdc->dring_len = VD_DRING_LEN; 2368e1ebb9ecSlm66018 2369e1ebb9ecSlm66018 status = ldc_mem_dring_create(vdc->dring_len, 2370e1ebb9ecSlm66018 vdc->dring_entry_size, &vdc->ldc_dring_hdl); 23711ae08745Sheppo if ((vdc->ldc_dring_hdl == NULL) || (status != 0)) { 23723af08d82Slm66018 DMSG(vdc, 0, "[%d] Descriptor ring creation failed", 2373e1ebb9ecSlm66018 vdc->instance); 23741ae08745Sheppo return (status); 23751ae08745Sheppo } 23760a55fbb7Slm66018 vdc->initialized |= VDC_DRING_INIT; 23770a55fbb7Slm66018 } 23781ae08745Sheppo 23790a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_BOUND) == 0) { 23803af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance); 23810a55fbb7Slm66018 vdc->dring_cookie = 23820a55fbb7Slm66018 kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP); 23831ae08745Sheppo 23841ae08745Sheppo status = ldc_mem_dring_bind(vdc->ldc_handle, vdc->ldc_dring_hdl, 23854bac2208Snarayan LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW, 23860a55fbb7Slm66018 &vdc->dring_cookie[0], 23871ae08745Sheppo &vdc->dring_cookie_count); 23881ae08745Sheppo if (status != 0) { 23893af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to bind descriptor ring " 23903af08d82Slm66018 "(%lx) to channel (%lx) status=%d\n", 23913af08d82Slm66018 vdc->instance, vdc->ldc_dring_hdl, 23923af08d82Slm66018 vdc->ldc_handle, status); 23931ae08745Sheppo return (status); 23941ae08745Sheppo } 23951ae08745Sheppo ASSERT(vdc->dring_cookie_count == 1); 23961ae08745Sheppo vdc->initialized |= VDC_DRING_BOUND; 23970a55fbb7Slm66018 } 23981ae08745Sheppo 23991ae08745Sheppo status = ldc_mem_dring_info(vdc->ldc_dring_hdl, &vdc->dring_mem_info); 24001ae08745Sheppo if (status != 0) { 24013af08d82Slm66018 DMSG(vdc, 0, 24023af08d82Slm66018 "[%d] Failed to get info for descriptor ring (%lx)\n", 2403e1ebb9ecSlm66018 vdc->instance, vdc->ldc_dring_hdl); 24041ae08745Sheppo return (status); 24051ae08745Sheppo } 24061ae08745Sheppo 24070a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_LOCAL) == 0) { 24083af08d82Slm66018 DMSG(vdc, 0, "[%d] local dring\n", vdc->instance); 24090a55fbb7Slm66018 24101ae08745Sheppo /* Allocate the local copy of this dring */ 24110a55fbb7Slm66018 vdc->local_dring = 2412e1ebb9ecSlm66018 kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t), 24131ae08745Sheppo KM_SLEEP); 24141ae08745Sheppo vdc->initialized |= VDC_DRING_LOCAL; 24150a55fbb7Slm66018 } 24161ae08745Sheppo 24171ae08745Sheppo /* 24180a55fbb7Slm66018 * Mark all DRing entries as free and initialize the private 24190a55fbb7Slm66018 * descriptor's memory handles. If any entry is initialized, 24200a55fbb7Slm66018 * we need to free it later so we set the bit in 'initialized' 24210a55fbb7Slm66018 * at the start. 24221ae08745Sheppo */ 24231ae08745Sheppo vdc->initialized |= VDC_DRING_ENTRY; 2424e1ebb9ecSlm66018 for (i = 0; i < vdc->dring_len; i++) { 24251ae08745Sheppo dep = VDC_GET_DRING_ENTRY_PTR(vdc, i); 24261ae08745Sheppo dep->hdr.dstate = VIO_DESC_FREE; 24271ae08745Sheppo 24281ae08745Sheppo status = ldc_mem_alloc_handle(vdc->ldc_handle, 24291ae08745Sheppo &vdc->local_dring[i].desc_mhdl); 24301ae08745Sheppo if (status != 0) { 24313af08d82Slm66018 DMSG(vdc, 0, "![%d] Failed to alloc mem handle for" 24321ae08745Sheppo " descriptor %d", vdc->instance, i); 24331ae08745Sheppo return (status); 24341ae08745Sheppo } 24353af08d82Slm66018 vdc->local_dring[i].is_free = B_TRUE; 24361ae08745Sheppo vdc->local_dring[i].dep = dep; 24371ae08745Sheppo } 24381ae08745Sheppo 24393af08d82Slm66018 /* Initialize the starting index */ 24403af08d82Slm66018 vdc->dring_curr_idx = 0; 24411ae08745Sheppo 24421ae08745Sheppo return (status); 24431ae08745Sheppo } 24441ae08745Sheppo 24450a55fbb7Slm66018 /* 24460a55fbb7Slm66018 * Function: 24470a55fbb7Slm66018 * vdc_destroy_descriptor_ring() 24480a55fbb7Slm66018 * 24490a55fbb7Slm66018 * Description: 24500a55fbb7Slm66018 * 24510a55fbb7Slm66018 * Arguments: 24520a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 24530a55fbb7Slm66018 * 24540a55fbb7Slm66018 * Return Code: 24550a55fbb7Slm66018 * None 24560a55fbb7Slm66018 */ 24571ae08745Sheppo static void 24581ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc) 24591ae08745Sheppo { 24600a55fbb7Slm66018 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 24611ae08745Sheppo ldc_mem_handle_t mhdl = NULL; 24623af08d82Slm66018 ldc_mem_info_t minfo; 24631ae08745Sheppo int status = -1; 24641ae08745Sheppo int i; /* loop */ 24651ae08745Sheppo 24661ae08745Sheppo ASSERT(vdc != NULL); 24671ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 24681ae08745Sheppo 24693af08d82Slm66018 DMSG(vdc, 0, "[%d] Entered\n", vdc->instance); 24701ae08745Sheppo 24711ae08745Sheppo if (vdc->initialized & VDC_DRING_ENTRY) { 24723af08d82Slm66018 DMSG(vdc, 0, 24733af08d82Slm66018 "[%d] Removing Local DRing entries\n", vdc->instance); 2474e1ebb9ecSlm66018 for (i = 0; i < vdc->dring_len; i++) { 24750a55fbb7Slm66018 ldep = &vdc->local_dring[i]; 24760a55fbb7Slm66018 mhdl = ldep->desc_mhdl; 24771ae08745Sheppo 24780a55fbb7Slm66018 if (mhdl == NULL) 24790a55fbb7Slm66018 continue; 24800a55fbb7Slm66018 24813af08d82Slm66018 if ((status = ldc_mem_info(mhdl, &minfo)) != 0) { 24823af08d82Slm66018 DMSG(vdc, 0, 24833af08d82Slm66018 "ldc_mem_info returned an error: %d\n", 24843af08d82Slm66018 status); 24853af08d82Slm66018 24863af08d82Slm66018 /* 24873af08d82Slm66018 * This must mean that the mem handle 24883af08d82Slm66018 * is not valid. Clear it out so that 24893af08d82Slm66018 * no one tries to use it. 24903af08d82Slm66018 */ 24913af08d82Slm66018 ldep->desc_mhdl = NULL; 24923af08d82Slm66018 continue; 24933af08d82Slm66018 } 24943af08d82Slm66018 24953af08d82Slm66018 if (minfo.status == LDC_BOUND) { 24963af08d82Slm66018 (void) ldc_mem_unbind_handle(mhdl); 24973af08d82Slm66018 } 24983af08d82Slm66018 24991ae08745Sheppo (void) ldc_mem_free_handle(mhdl); 25003af08d82Slm66018 25013af08d82Slm66018 ldep->desc_mhdl = NULL; 25021ae08745Sheppo } 25031ae08745Sheppo vdc->initialized &= ~VDC_DRING_ENTRY; 25041ae08745Sheppo } 25051ae08745Sheppo 25061ae08745Sheppo if (vdc->initialized & VDC_DRING_LOCAL) { 25073af08d82Slm66018 DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance); 25081ae08745Sheppo kmem_free(vdc->local_dring, 2509e1ebb9ecSlm66018 vdc->dring_len * sizeof (vdc_local_desc_t)); 25101ae08745Sheppo vdc->initialized &= ~VDC_DRING_LOCAL; 25111ae08745Sheppo } 25121ae08745Sheppo 25131ae08745Sheppo if (vdc->initialized & VDC_DRING_BOUND) { 25143af08d82Slm66018 DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance); 25151ae08745Sheppo status = ldc_mem_dring_unbind(vdc->ldc_dring_hdl); 25161ae08745Sheppo if (status == 0) { 25171ae08745Sheppo vdc->initialized &= ~VDC_DRING_BOUND; 25181ae08745Sheppo } else { 25193af08d82Slm66018 DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx", 2520e1ebb9ecSlm66018 vdc->instance, status, vdc->ldc_dring_hdl); 25211ae08745Sheppo } 25223af08d82Slm66018 kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t)); 25231ae08745Sheppo } 25241ae08745Sheppo 25251ae08745Sheppo if (vdc->initialized & VDC_DRING_INIT) { 25263af08d82Slm66018 DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance); 25271ae08745Sheppo status = ldc_mem_dring_destroy(vdc->ldc_dring_hdl); 25281ae08745Sheppo if (status == 0) { 25291ae08745Sheppo vdc->ldc_dring_hdl = NULL; 25301ae08745Sheppo bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t)); 25311ae08745Sheppo vdc->initialized &= ~VDC_DRING_INIT; 25321ae08745Sheppo } else { 25333af08d82Slm66018 DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)", 2534e1ebb9ecSlm66018 vdc->instance, status, vdc->ldc_dring_hdl); 25351ae08745Sheppo } 25361ae08745Sheppo } 25371ae08745Sheppo } 25381ae08745Sheppo 25391ae08745Sheppo /* 25403af08d82Slm66018 * Function: 25413af08d82Slm66018 * vdc_map_to_shared_ring() 25421ae08745Sheppo * 25431ae08745Sheppo * Description: 25443af08d82Slm66018 * Copy contents of the local descriptor to the shared 25453af08d82Slm66018 * memory descriptor. 25461ae08745Sheppo * 25473af08d82Slm66018 * Arguments: 25483af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 25493af08d82Slm66018 * idx - descriptor ring index 25503af08d82Slm66018 * 25513af08d82Slm66018 * Return Code: 25523af08d82Slm66018 * None 25531ae08745Sheppo */ 25541ae08745Sheppo static int 25553af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx) 25561ae08745Sheppo { 25573af08d82Slm66018 vdc_local_desc_t *ldep; 25583af08d82Slm66018 vd_dring_entry_t *dep; 25593af08d82Slm66018 int rv; 25601ae08745Sheppo 25613af08d82Slm66018 ldep = &(vdcp->local_dring[idx]); 25621ae08745Sheppo 25633af08d82Slm66018 /* for now leave in the old pop_mem_hdl stuff */ 25643af08d82Slm66018 if (ldep->nbytes > 0) { 25653af08d82Slm66018 rv = vdc_populate_mem_hdl(vdcp, ldep); 25663af08d82Slm66018 if (rv) { 25673af08d82Slm66018 DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n", 25683af08d82Slm66018 vdcp->instance); 25693af08d82Slm66018 return (rv); 25703af08d82Slm66018 } 25713af08d82Slm66018 } 25721ae08745Sheppo 25733af08d82Slm66018 /* 25743af08d82Slm66018 * fill in the data details into the DRing 25753af08d82Slm66018 */ 2576d10e4ef2Snarayan dep = ldep->dep; 25771ae08745Sheppo ASSERT(dep != NULL); 25781ae08745Sheppo 25793af08d82Slm66018 dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp); 25803af08d82Slm66018 dep->payload.operation = ldep->operation; 25813af08d82Slm66018 dep->payload.addr = ldep->offset; 25823af08d82Slm66018 dep->payload.nbytes = ldep->nbytes; 2583055d7c80Scarlsonj dep->payload.status = (uint32_t)-1; /* vds will set valid value */ 25843af08d82Slm66018 dep->payload.slice = ldep->slice; 25853af08d82Slm66018 dep->hdr.dstate = VIO_DESC_READY; 25863af08d82Slm66018 dep->hdr.ack = 1; /* request an ACK for every message */ 25871ae08745Sheppo 25883af08d82Slm66018 return (0); 25891ae08745Sheppo } 25901ae08745Sheppo 25911ae08745Sheppo /* 25921ae08745Sheppo * Function: 25933af08d82Slm66018 * vdc_send_request 25943af08d82Slm66018 * 25953af08d82Slm66018 * Description: 25963af08d82Slm66018 * This routine writes the data to be transmitted to vds into the 25973af08d82Slm66018 * descriptor, notifies vds that the ring has been updated and 25983af08d82Slm66018 * then waits for the request to be processed. 25993af08d82Slm66018 * 26003af08d82Slm66018 * Arguments: 26013af08d82Slm66018 * vdcp - the soft state pointer 26023af08d82Slm66018 * operation - operation we want vds to perform (VD_OP_XXX) 26033af08d82Slm66018 * addr - address of data buf to be read/written. 26043af08d82Slm66018 * nbytes - number of bytes to read/write 26053af08d82Slm66018 * slice - the disk slice this request is for 26063af08d82Slm66018 * offset - relative disk offset 26073af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 26083af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 26093af08d82Slm66018 * . mode for ioctl(9e) 26103af08d82Slm66018 * . LP64 diskaddr_t (block I/O) 26113af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 26123af08d82Slm66018 * 26133af08d82Slm66018 * Return Codes: 26143af08d82Slm66018 * 0 26153af08d82Slm66018 * ENXIO 26163af08d82Slm66018 */ 26173af08d82Slm66018 static int 26183af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr, 26193af08d82Slm66018 size_t nbytes, int slice, diskaddr_t offset, int cb_type, 26203af08d82Slm66018 void *cb_arg, vio_desc_direction_t dir) 26213af08d82Slm66018 { 26223af08d82Slm66018 ASSERT(vdcp != NULL); 262387a7269eSachartre ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR); 26243af08d82Slm66018 26253af08d82Slm66018 mutex_enter(&vdcp->lock); 26263af08d82Slm66018 26273af08d82Slm66018 do { 26283c96341aSnarayan while (vdcp->state != VDC_STATE_RUNNING) { 26293af08d82Slm66018 26303c96341aSnarayan /* return error if detaching */ 26313c96341aSnarayan if (vdcp->state == VDC_STATE_DETACH) { 26323c96341aSnarayan mutex_exit(&vdcp->lock); 26333c96341aSnarayan return (ENXIO); 26343c96341aSnarayan } 2635655fd6a9Sachartre 2636655fd6a9Sachartre /* fail request if connection timeout is reached */ 2637655fd6a9Sachartre if (vdcp->ctimeout_reached) { 2638655fd6a9Sachartre mutex_exit(&vdcp->lock); 2639655fd6a9Sachartre return (EIO); 2640655fd6a9Sachartre } 2641655fd6a9Sachartre 2642655fd6a9Sachartre cv_wait(&vdcp->running_cv, &vdcp->lock); 26433c96341aSnarayan } 26443c96341aSnarayan 26453af08d82Slm66018 } while (vdc_populate_descriptor(vdcp, operation, addr, 26463af08d82Slm66018 nbytes, slice, offset, cb_type, cb_arg, dir)); 26473af08d82Slm66018 26483af08d82Slm66018 mutex_exit(&vdcp->lock); 26493af08d82Slm66018 return (0); 26503af08d82Slm66018 } 26513af08d82Slm66018 26523af08d82Slm66018 26533af08d82Slm66018 /* 26543af08d82Slm66018 * Function: 26551ae08745Sheppo * vdc_populate_descriptor 26561ae08745Sheppo * 26571ae08745Sheppo * Description: 26581ae08745Sheppo * This routine writes the data to be transmitted to vds into the 26591ae08745Sheppo * descriptor, notifies vds that the ring has been updated and 26601ae08745Sheppo * then waits for the request to be processed. 26611ae08745Sheppo * 26621ae08745Sheppo * Arguments: 26633af08d82Slm66018 * vdcp - the soft state pointer 26641ae08745Sheppo * operation - operation we want vds to perform (VD_OP_XXX) 26653af08d82Slm66018 * addr - address of data buf to be read/written. 26663af08d82Slm66018 * nbytes - number of bytes to read/write 26673af08d82Slm66018 * slice - the disk slice this request is for 26683af08d82Slm66018 * offset - relative disk offset 26693af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 26703af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 26711ae08745Sheppo * . mode for ioctl(9e) 26721ae08745Sheppo * . LP64 diskaddr_t (block I/O) 26733af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 26741ae08745Sheppo * 26751ae08745Sheppo * Return Codes: 26761ae08745Sheppo * 0 26771ae08745Sheppo * EAGAIN 2678*17cadca8Slm66018 * ECONNRESET 26791ae08745Sheppo * ENXIO 26801ae08745Sheppo */ 26811ae08745Sheppo static int 26823af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr, 26833af08d82Slm66018 size_t nbytes, int slice, diskaddr_t offset, int cb_type, 26843af08d82Slm66018 void *cb_arg, vio_desc_direction_t dir) 26851ae08745Sheppo { 26863af08d82Slm66018 vdc_local_desc_t *local_dep = NULL; /* Local Dring Pointer */ 26873af08d82Slm66018 int idx; /* Index of DRing entry used */ 26883af08d82Slm66018 int next_idx; 26891ae08745Sheppo vio_dring_msg_t dmsg; 26903af08d82Slm66018 size_t msglen; 26918e6a2a04Slm66018 int rv; 26921ae08745Sheppo 26933af08d82Slm66018 ASSERT(MUTEX_HELD(&vdcp->lock)); 26943af08d82Slm66018 vdcp->threads_pending++; 26953af08d82Slm66018 loop: 26963af08d82Slm66018 DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx); 26971ae08745Sheppo 26983af08d82Slm66018 /* Get next available D-Ring entry */ 26993af08d82Slm66018 idx = vdcp->dring_curr_idx; 27003af08d82Slm66018 local_dep = &(vdcp->local_dring[idx]); 27011ae08745Sheppo 27023af08d82Slm66018 if (!local_dep->is_free) { 27033af08d82Slm66018 DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n", 27043af08d82Slm66018 vdcp->instance); 27053af08d82Slm66018 cv_wait(&vdcp->dring_free_cv, &vdcp->lock); 27063af08d82Slm66018 if (vdcp->state == VDC_STATE_RUNNING || 27073af08d82Slm66018 vdcp->state == VDC_STATE_HANDLE_PENDING) { 27083af08d82Slm66018 goto loop; 27093af08d82Slm66018 } 27103af08d82Slm66018 vdcp->threads_pending--; 27113af08d82Slm66018 return (ECONNRESET); 27121ae08745Sheppo } 27131ae08745Sheppo 27143af08d82Slm66018 next_idx = idx + 1; 27153af08d82Slm66018 if (next_idx >= vdcp->dring_len) 27163af08d82Slm66018 next_idx = 0; 27173af08d82Slm66018 vdcp->dring_curr_idx = next_idx; 27181ae08745Sheppo 27193af08d82Slm66018 ASSERT(local_dep->is_free); 27201ae08745Sheppo 27213af08d82Slm66018 local_dep->operation = operation; 2722d10e4ef2Snarayan local_dep->addr = addr; 27233af08d82Slm66018 local_dep->nbytes = nbytes; 27243af08d82Slm66018 local_dep->slice = slice; 27253af08d82Slm66018 local_dep->offset = offset; 27263af08d82Slm66018 local_dep->cb_type = cb_type; 27273af08d82Slm66018 local_dep->cb_arg = cb_arg; 27283af08d82Slm66018 local_dep->dir = dir; 27293af08d82Slm66018 27303af08d82Slm66018 local_dep->is_free = B_FALSE; 27313af08d82Slm66018 27323af08d82Slm66018 rv = vdc_map_to_shared_dring(vdcp, idx); 27333af08d82Slm66018 if (rv) { 27343af08d82Slm66018 DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n", 27353af08d82Slm66018 vdcp->instance); 27363af08d82Slm66018 /* free the descriptor */ 27373af08d82Slm66018 local_dep->is_free = B_TRUE; 27383af08d82Slm66018 vdcp->dring_curr_idx = idx; 27393af08d82Slm66018 cv_wait(&vdcp->membind_cv, &vdcp->lock); 27403af08d82Slm66018 if (vdcp->state == VDC_STATE_RUNNING || 27413af08d82Slm66018 vdcp->state == VDC_STATE_HANDLE_PENDING) { 27423af08d82Slm66018 goto loop; 27431ae08745Sheppo } 27443af08d82Slm66018 vdcp->threads_pending--; 27453af08d82Slm66018 return (ECONNRESET); 27461ae08745Sheppo } 27471ae08745Sheppo 27481ae08745Sheppo /* 27491ae08745Sheppo * Send a msg with the DRing details to vds 27501ae08745Sheppo */ 27511ae08745Sheppo VIO_INIT_DRING_DATA_TAG(dmsg); 27523af08d82Slm66018 VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp); 27533af08d82Slm66018 dmsg.dring_ident = vdcp->dring_ident; 27541ae08745Sheppo dmsg.start_idx = idx; 27551ae08745Sheppo dmsg.end_idx = idx; 27563af08d82Slm66018 vdcp->seq_num++; 27571ae08745Sheppo 27583af08d82Slm66018 DTRACE_IO2(send, vio_dring_msg_t *, &dmsg, vdc_t *, vdcp); 2759d10e4ef2Snarayan 27603af08d82Slm66018 DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n", 27613af08d82Slm66018 vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num); 27621ae08745Sheppo 27633af08d82Slm66018 /* 27643af08d82Slm66018 * note we're still holding the lock here to 27653af08d82Slm66018 * make sure the message goes out in order !!!... 27663af08d82Slm66018 */ 27673af08d82Slm66018 msglen = sizeof (dmsg); 27683af08d82Slm66018 rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen); 27693af08d82Slm66018 switch (rv) { 27703af08d82Slm66018 case ECONNRESET: 27713af08d82Slm66018 /* 27723af08d82Slm66018 * vdc_send initiates the reset on failure. 27733af08d82Slm66018 * Since the transaction has already been put 27743af08d82Slm66018 * on the local dring, it will automatically get 27753af08d82Slm66018 * retried when the channel is reset. Given that, 27763af08d82Slm66018 * it is ok to just return success even though the 27773af08d82Slm66018 * send failed. 27783af08d82Slm66018 */ 27793af08d82Slm66018 rv = 0; 27803af08d82Slm66018 break; 2781d10e4ef2Snarayan 27823af08d82Slm66018 case 0: /* EOK */ 27833af08d82Slm66018 DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv); 27843af08d82Slm66018 break; 2785d10e4ef2Snarayan 27863af08d82Slm66018 default: 27873af08d82Slm66018 goto cleanup_and_exit; 27883af08d82Slm66018 } 2789e1ebb9ecSlm66018 27903af08d82Slm66018 vdcp->threads_pending--; 27913af08d82Slm66018 return (rv); 27923af08d82Slm66018 27933af08d82Slm66018 cleanup_and_exit: 27943af08d82Slm66018 DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv); 27953af08d82Slm66018 return (ENXIO); 27961ae08745Sheppo } 27971ae08745Sheppo 27981ae08745Sheppo /* 27993af08d82Slm66018 * Function: 28003af08d82Slm66018 * vdc_do_sync_op 28013af08d82Slm66018 * 28023af08d82Slm66018 * Description: 28033af08d82Slm66018 * Wrapper around vdc_populate_descriptor that blocks until the 28043af08d82Slm66018 * response to the message is available. 28053af08d82Slm66018 * 28063af08d82Slm66018 * Arguments: 28073af08d82Slm66018 * vdcp - the soft state pointer 28083af08d82Slm66018 * operation - operation we want vds to perform (VD_OP_XXX) 28093af08d82Slm66018 * addr - address of data buf to be read/written. 28103af08d82Slm66018 * nbytes - number of bytes to read/write 28113af08d82Slm66018 * slice - the disk slice this request is for 28123af08d82Slm66018 * offset - relative disk offset 28133af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 28143af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 28153af08d82Slm66018 * . mode for ioctl(9e) 28163af08d82Slm66018 * . LP64 diskaddr_t (block I/O) 28173af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 28183af08d82Slm66018 * 28193af08d82Slm66018 * Return Codes: 28203af08d82Slm66018 * 0 28213af08d82Slm66018 * EAGAIN 28223af08d82Slm66018 * EFAULT 28233af08d82Slm66018 * ENXIO 28243af08d82Slm66018 * EIO 28250a55fbb7Slm66018 */ 28263af08d82Slm66018 static int 28273af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes, 28283af08d82Slm66018 int slice, diskaddr_t offset, int cb_type, void *cb_arg, 28293af08d82Slm66018 vio_desc_direction_t dir) 28303af08d82Slm66018 { 28313af08d82Slm66018 int status; 28323af08d82Slm66018 28333af08d82Slm66018 ASSERT(cb_type == CB_SYNC); 28341ae08745Sheppo 28351ae08745Sheppo /* 28363af08d82Slm66018 * Grab the lock, if blocked wait until the server 28373af08d82Slm66018 * response causes us to wake up again. 28383af08d82Slm66018 */ 28393af08d82Slm66018 mutex_enter(&vdcp->lock); 28403af08d82Slm66018 vdcp->sync_op_cnt++; 28413af08d82Slm66018 while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH) 28423af08d82Slm66018 cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock); 28433af08d82Slm66018 28443af08d82Slm66018 if (vdcp->state == VDC_STATE_DETACH) { 28453af08d82Slm66018 cv_broadcast(&vdcp->sync_blocked_cv); 28463af08d82Slm66018 vdcp->sync_op_cnt--; 28473af08d82Slm66018 mutex_exit(&vdcp->lock); 28483af08d82Slm66018 return (ENXIO); 28493af08d82Slm66018 } 28503af08d82Slm66018 28513af08d82Slm66018 /* now block anyone other thread entering after us */ 28523af08d82Slm66018 vdcp->sync_op_blocked = B_TRUE; 28533af08d82Slm66018 vdcp->sync_op_pending = B_TRUE; 28543af08d82Slm66018 mutex_exit(&vdcp->lock); 28553af08d82Slm66018 2856655fd6a9Sachartre status = vdc_send_request(vdcp, operation, addr, 28573af08d82Slm66018 nbytes, slice, offset, cb_type, cb_arg, dir); 28583af08d82Slm66018 2859655fd6a9Sachartre mutex_enter(&vdcp->lock); 2860655fd6a9Sachartre 2861655fd6a9Sachartre if (status != 0) { 2862655fd6a9Sachartre vdcp->sync_op_pending = B_FALSE; 2863655fd6a9Sachartre } else { 28643af08d82Slm66018 /* 28653af08d82Slm66018 * block until our transaction completes. 28663af08d82Slm66018 * Also anyone else waiting also gets to go next. 28673af08d82Slm66018 */ 28683af08d82Slm66018 while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH) 28693af08d82Slm66018 cv_wait(&vdcp->sync_pending_cv, &vdcp->lock); 28703af08d82Slm66018 2871655fd6a9Sachartre DMSG(vdcp, 2, ": operation returned %d\n", 2872655fd6a9Sachartre vdcp->sync_op_status); 28733c96341aSnarayan if (vdcp->state == VDC_STATE_DETACH) { 28743c96341aSnarayan vdcp->sync_op_pending = B_FALSE; 28753af08d82Slm66018 status = ENXIO; 28763c96341aSnarayan } else { 28773af08d82Slm66018 status = vdcp->sync_op_status; 28783c96341aSnarayan } 2879655fd6a9Sachartre } 28803c96341aSnarayan 28813af08d82Slm66018 vdcp->sync_op_status = 0; 28823af08d82Slm66018 vdcp->sync_op_blocked = B_FALSE; 28833af08d82Slm66018 vdcp->sync_op_cnt--; 28843af08d82Slm66018 28853af08d82Slm66018 /* signal the next waiting thread */ 28863af08d82Slm66018 cv_signal(&vdcp->sync_blocked_cv); 28873af08d82Slm66018 mutex_exit(&vdcp->lock); 28883af08d82Slm66018 28893af08d82Slm66018 return (status); 28903af08d82Slm66018 } 28913af08d82Slm66018 28923af08d82Slm66018 28933af08d82Slm66018 /* 28943af08d82Slm66018 * Function: 28953af08d82Slm66018 * vdc_drain_response() 28963af08d82Slm66018 * 28973af08d82Slm66018 * Description: 28981ae08745Sheppo * When a guest is panicking, the completion of requests needs to be 28991ae08745Sheppo * handled differently because interrupts are disabled and vdc 29001ae08745Sheppo * will not get messages. We have to poll for the messages instead. 29013af08d82Slm66018 * 29023af08d82Slm66018 * Arguments: 29033af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 29043af08d82Slm66018 * 29053af08d82Slm66018 * Return Code: 29063af08d82Slm66018 * 0 - Success 29071ae08745Sheppo */ 29083af08d82Slm66018 static int 29093af08d82Slm66018 vdc_drain_response(vdc_t *vdc) 29103af08d82Slm66018 { 29113af08d82Slm66018 int rv, idx, retries; 29123af08d82Slm66018 size_t msglen; 29133af08d82Slm66018 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 29143af08d82Slm66018 vio_dring_msg_t dmsg; 29153af08d82Slm66018 29163af08d82Slm66018 mutex_enter(&vdc->lock); 29173af08d82Slm66018 29181ae08745Sheppo retries = 0; 29191ae08745Sheppo for (;;) { 29201ae08745Sheppo msglen = sizeof (dmsg); 29213af08d82Slm66018 rv = ldc_read(vdc->ldc_handle, (caddr_t)&dmsg, &msglen); 29228e6a2a04Slm66018 if (rv) { 29238e6a2a04Slm66018 rv = EINVAL; 29241ae08745Sheppo break; 29251ae08745Sheppo } 29261ae08745Sheppo 29271ae08745Sheppo /* 29281ae08745Sheppo * if there are no packets wait and check again 29291ae08745Sheppo */ 29308e6a2a04Slm66018 if ((rv == 0) && (msglen == 0)) { 29311ae08745Sheppo if (retries++ > vdc_dump_retries) { 29328e6a2a04Slm66018 rv = EAGAIN; 29331ae08745Sheppo break; 29341ae08745Sheppo } 29351ae08745Sheppo 2936d10e4ef2Snarayan drv_usecwait(vdc_usec_timeout_dump); 29371ae08745Sheppo continue; 29381ae08745Sheppo } 29391ae08745Sheppo 29401ae08745Sheppo /* 29411ae08745Sheppo * Ignore all messages that are not ACKs/NACKs to 29421ae08745Sheppo * DRing requests. 29431ae08745Sheppo */ 29441ae08745Sheppo if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) || 29451ae08745Sheppo (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) { 29463af08d82Slm66018 DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n", 29471ae08745Sheppo dmsg.tag.vio_msgtype, 29481ae08745Sheppo dmsg.tag.vio_subtype, 29491ae08745Sheppo dmsg.tag.vio_subtype_env); 29501ae08745Sheppo continue; 29511ae08745Sheppo } 29521ae08745Sheppo 29531ae08745Sheppo /* 29543af08d82Slm66018 * set the appropriate return value for the current request. 29551ae08745Sheppo */ 29561ae08745Sheppo switch (dmsg.tag.vio_subtype) { 29571ae08745Sheppo case VIO_SUBTYPE_ACK: 29588e6a2a04Slm66018 rv = 0; 29591ae08745Sheppo break; 29601ae08745Sheppo case VIO_SUBTYPE_NACK: 29618e6a2a04Slm66018 rv = EAGAIN; 29621ae08745Sheppo break; 29631ae08745Sheppo default: 29641ae08745Sheppo continue; 29651ae08745Sheppo } 29661ae08745Sheppo 29673af08d82Slm66018 idx = dmsg.start_idx; 29683af08d82Slm66018 if (idx >= vdc->dring_len) { 29693af08d82Slm66018 DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n", 2970e1ebb9ecSlm66018 vdc->instance, idx); 29713af08d82Slm66018 continue; 29721ae08745Sheppo } 29733af08d82Slm66018 ldep = &vdc->local_dring[idx]; 29743af08d82Slm66018 if (ldep->dep->hdr.dstate != VIO_DESC_DONE) { 29753af08d82Slm66018 DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n", 29763af08d82Slm66018 vdc->instance, idx, ldep->dep->hdr.dstate); 29771ae08745Sheppo continue; 29781ae08745Sheppo } 29791ae08745Sheppo 29803af08d82Slm66018 DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n", 29813af08d82Slm66018 vdc->instance, idx, ldep->dep->hdr.dstate); 29823af08d82Slm66018 rv = vdc_depopulate_descriptor(vdc, idx); 29833af08d82Slm66018 if (rv) { 29843af08d82Slm66018 DMSG(vdc, 0, 29853af08d82Slm66018 "[%d] Entry @ %d - depopulate failed ..\n", 29863af08d82Slm66018 vdc->instance, idx); 29871ae08745Sheppo } 29881ae08745Sheppo 29893af08d82Slm66018 /* if this is the last descriptor - break out of loop */ 29903af08d82Slm66018 if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx) 29913af08d82Slm66018 break; 29923af08d82Slm66018 } 29933af08d82Slm66018 29943af08d82Slm66018 mutex_exit(&vdc->lock); 29953af08d82Slm66018 DMSG(vdc, 0, "End idx=%d\n", idx); 29963af08d82Slm66018 29973af08d82Slm66018 return (rv); 29981ae08745Sheppo } 29991ae08745Sheppo 30001ae08745Sheppo 30010a55fbb7Slm66018 /* 30020a55fbb7Slm66018 * Function: 30030a55fbb7Slm66018 * vdc_depopulate_descriptor() 30040a55fbb7Slm66018 * 30050a55fbb7Slm66018 * Description: 30060a55fbb7Slm66018 * 30070a55fbb7Slm66018 * Arguments: 30080a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 30090a55fbb7Slm66018 * idx - Index of the Descriptor Ring entry being modified 30100a55fbb7Slm66018 * 30110a55fbb7Slm66018 * Return Code: 30120a55fbb7Slm66018 * 0 - Success 30130a55fbb7Slm66018 */ 30141ae08745Sheppo static int 30151ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx) 30161ae08745Sheppo { 30171ae08745Sheppo vd_dring_entry_t *dep = NULL; /* Dring Entry Pointer */ 30181ae08745Sheppo vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 30191ae08745Sheppo int status = ENXIO; 30208e6a2a04Slm66018 int rv = 0; 30211ae08745Sheppo 30221ae08745Sheppo ASSERT(vdc != NULL); 3023e1ebb9ecSlm66018 ASSERT(idx < vdc->dring_len); 30241ae08745Sheppo ldep = &vdc->local_dring[idx]; 30251ae08745Sheppo ASSERT(ldep != NULL); 30263af08d82Slm66018 ASSERT(MUTEX_HELD(&vdc->lock)); 30273af08d82Slm66018 30283af08d82Slm66018 DMSG(vdc, 2, ": idx = %d\n", idx); 30291ae08745Sheppo dep = ldep->dep; 30301ae08745Sheppo ASSERT(dep != NULL); 3031e1ebb9ecSlm66018 ASSERT((dep->hdr.dstate == VIO_DESC_DONE) || 3032e1ebb9ecSlm66018 (dep->payload.status == ECANCELED)); 30331ae08745Sheppo 3034e1ebb9ecSlm66018 VDC_MARK_DRING_ENTRY_FREE(vdc, idx); 30353af08d82Slm66018 30363af08d82Slm66018 ldep->is_free = B_TRUE; 30371ae08745Sheppo status = dep->payload.status; 3038205eeb1aSlm66018 DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status); 30391ae08745Sheppo 3040eff7243fSlm66018 /* 3041eff7243fSlm66018 * If no buffers were used to transfer information to the server when 3042eff7243fSlm66018 * populating the descriptor then no memory handles need to be unbound 3043eff7243fSlm66018 * and we can return now. 3044eff7243fSlm66018 */ 3045eff7243fSlm66018 if (ldep->nbytes == 0) { 3046eff7243fSlm66018 cv_signal(&vdc->dring_free_cv); 30478e6a2a04Slm66018 return (status); 3048eff7243fSlm66018 } 30498e6a2a04Slm66018 30501ae08745Sheppo /* 30511ae08745Sheppo * If the upper layer passed in a misaligned address we copied the 30521ae08745Sheppo * data into an aligned buffer before sending it to LDC - we now 30531ae08745Sheppo * copy it back to the original buffer. 30541ae08745Sheppo */ 30551ae08745Sheppo if (ldep->align_addr) { 30561ae08745Sheppo ASSERT(ldep->addr != NULL); 30571ae08745Sheppo 30583c96341aSnarayan if (dep->payload.nbytes > 0) 30593c96341aSnarayan bcopy(ldep->align_addr, ldep->addr, 30603c96341aSnarayan dep->payload.nbytes); 30611ae08745Sheppo kmem_free(ldep->align_addr, 30623c96341aSnarayan sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8)); 30631ae08745Sheppo ldep->align_addr = NULL; 30641ae08745Sheppo } 30651ae08745Sheppo 30668e6a2a04Slm66018 rv = ldc_mem_unbind_handle(ldep->desc_mhdl); 30678e6a2a04Slm66018 if (rv != 0) { 30683af08d82Slm66018 DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)", 30698e6a2a04Slm66018 vdc->instance, ldep->desc_mhdl, idx, rv); 30708e6a2a04Slm66018 /* 30718e6a2a04Slm66018 * The error returned by the vDisk server is more informative 30728e6a2a04Slm66018 * and thus has a higher priority but if it isn't set we ensure 30738e6a2a04Slm66018 * that this function returns an error. 30748e6a2a04Slm66018 */ 30758e6a2a04Slm66018 if (status == 0) 30768e6a2a04Slm66018 status = EINVAL; 30771ae08745Sheppo } 30781ae08745Sheppo 30793af08d82Slm66018 cv_signal(&vdc->membind_cv); 30803af08d82Slm66018 cv_signal(&vdc->dring_free_cv); 30813af08d82Slm66018 30821ae08745Sheppo return (status); 30831ae08745Sheppo } 30841ae08745Sheppo 30850a55fbb7Slm66018 /* 30860a55fbb7Slm66018 * Function: 30870a55fbb7Slm66018 * vdc_populate_mem_hdl() 30880a55fbb7Slm66018 * 30890a55fbb7Slm66018 * Description: 30900a55fbb7Slm66018 * 30910a55fbb7Slm66018 * Arguments: 30920a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 30930a55fbb7Slm66018 * idx - Index of the Descriptor Ring entry being modified 30940a55fbb7Slm66018 * addr - virtual address being mapped in 30950a55fbb7Slm66018 * nybtes - number of bytes in 'addr' 30960a55fbb7Slm66018 * operation - the vDisk operation being performed (VD_OP_xxx) 30970a55fbb7Slm66018 * 30980a55fbb7Slm66018 * Return Code: 30990a55fbb7Slm66018 * 0 - Success 31000a55fbb7Slm66018 */ 31011ae08745Sheppo static int 31023af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep) 31031ae08745Sheppo { 31041ae08745Sheppo vd_dring_entry_t *dep = NULL; 31051ae08745Sheppo ldc_mem_handle_t mhdl; 31061ae08745Sheppo caddr_t vaddr; 31073af08d82Slm66018 size_t nbytes; 31084bac2208Snarayan uint8_t perm = LDC_MEM_RW; 31094bac2208Snarayan uint8_t maptype; 31101ae08745Sheppo int rv = 0; 31111ae08745Sheppo int i; 31121ae08745Sheppo 31133af08d82Slm66018 ASSERT(vdcp != NULL); 31141ae08745Sheppo 31153af08d82Slm66018 dep = ldep->dep; 31161ae08745Sheppo mhdl = ldep->desc_mhdl; 31171ae08745Sheppo 31183af08d82Slm66018 switch (ldep->dir) { 31193af08d82Slm66018 case VIO_read_dir: 31201ae08745Sheppo perm = LDC_MEM_W; 31211ae08745Sheppo break; 31221ae08745Sheppo 31233af08d82Slm66018 case VIO_write_dir: 31241ae08745Sheppo perm = LDC_MEM_R; 31251ae08745Sheppo break; 31261ae08745Sheppo 31273af08d82Slm66018 case VIO_both_dir: 31281ae08745Sheppo perm = LDC_MEM_RW; 31291ae08745Sheppo break; 31301ae08745Sheppo 31311ae08745Sheppo default: 31321ae08745Sheppo ASSERT(0); /* catch bad programming in vdc */ 31331ae08745Sheppo } 31341ae08745Sheppo 31351ae08745Sheppo /* 31361ae08745Sheppo * LDC expects any addresses passed in to be 8-byte aligned. We need 31371ae08745Sheppo * to copy the contents of any misaligned buffers to a newly allocated 31381ae08745Sheppo * buffer and bind it instead (and copy the the contents back to the 31391ae08745Sheppo * original buffer passed in when depopulating the descriptor) 31401ae08745Sheppo */ 31413af08d82Slm66018 vaddr = ldep->addr; 31423af08d82Slm66018 nbytes = ldep->nbytes; 31433af08d82Slm66018 if (((uint64_t)vaddr & 0x7) != 0) { 3144d10e4ef2Snarayan ASSERT(ldep->align_addr == NULL); 31451ae08745Sheppo ldep->align_addr = 31463af08d82Slm66018 kmem_alloc(sizeof (caddr_t) * 31473af08d82Slm66018 P2ROUNDUP(nbytes, 8), KM_SLEEP); 31483af08d82Slm66018 DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating " 31493af08d82Slm66018 "(buf=%p nb=%ld op=%d)\n", 31503af08d82Slm66018 vdcp->instance, (void *)vaddr, (void *)ldep->align_addr, 31513af08d82Slm66018 nbytes, ldep->operation); 31523af08d82Slm66018 if (perm != LDC_MEM_W) 31533af08d82Slm66018 bcopy(vaddr, ldep->align_addr, nbytes); 31541ae08745Sheppo vaddr = ldep->align_addr; 31551ae08745Sheppo } 31561ae08745Sheppo 31574bac2208Snarayan maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP; 31581ae08745Sheppo rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8), 315987a7269eSachartre maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies); 31603af08d82Slm66018 DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n", 31613af08d82Slm66018 vdcp->instance, dep->payload.ncookies); 31621ae08745Sheppo if (rv != 0) { 31633af08d82Slm66018 DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle " 31643af08d82Slm66018 "(mhdl=%p, buf=%p, err=%d)\n", 31653af08d82Slm66018 vdcp->instance, (void *)mhdl, (void *)vaddr, rv); 31661ae08745Sheppo if (ldep->align_addr) { 31671ae08745Sheppo kmem_free(ldep->align_addr, 3168d10e4ef2Snarayan sizeof (caddr_t) * P2ROUNDUP(nbytes, 8)); 31691ae08745Sheppo ldep->align_addr = NULL; 31701ae08745Sheppo } 31711ae08745Sheppo return (EAGAIN); 31721ae08745Sheppo } 31731ae08745Sheppo 31741ae08745Sheppo /* 31751ae08745Sheppo * Get the other cookies (if any). 31761ae08745Sheppo */ 31771ae08745Sheppo for (i = 1; i < dep->payload.ncookies; i++) { 31781ae08745Sheppo rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]); 31791ae08745Sheppo if (rv != 0) { 31801ae08745Sheppo (void) ldc_mem_unbind_handle(mhdl); 31813af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Failed to get next cookie " 3182e1ebb9ecSlm66018 "(mhdl=%lx cnum=%d), err=%d", 31833af08d82Slm66018 vdcp->instance, mhdl, i, rv); 31841ae08745Sheppo if (ldep->align_addr) { 31851ae08745Sheppo kmem_free(ldep->align_addr, 31863c96341aSnarayan sizeof (caddr_t) * ldep->nbytes); 31871ae08745Sheppo ldep->align_addr = NULL; 31881ae08745Sheppo } 31891ae08745Sheppo return (EAGAIN); 31901ae08745Sheppo } 31911ae08745Sheppo } 31921ae08745Sheppo 31931ae08745Sheppo return (rv); 31941ae08745Sheppo } 31951ae08745Sheppo 31961ae08745Sheppo /* 31971ae08745Sheppo * Interrupt handlers for messages from LDC 31981ae08745Sheppo */ 31991ae08745Sheppo 32000a55fbb7Slm66018 /* 32010a55fbb7Slm66018 * Function: 32020a55fbb7Slm66018 * vdc_handle_cb() 32030a55fbb7Slm66018 * 32040a55fbb7Slm66018 * Description: 32050a55fbb7Slm66018 * 32060a55fbb7Slm66018 * Arguments: 32070a55fbb7Slm66018 * event - Type of event (LDC_EVT_xxx) that triggered the callback 32080a55fbb7Slm66018 * arg - soft state pointer for this instance of the device driver. 32090a55fbb7Slm66018 * 32100a55fbb7Slm66018 * Return Code: 32110a55fbb7Slm66018 * 0 - Success 32120a55fbb7Slm66018 */ 32131ae08745Sheppo static uint_t 32141ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg) 32151ae08745Sheppo { 32161ae08745Sheppo ldc_status_t ldc_state; 32171ae08745Sheppo int rv = 0; 32181ae08745Sheppo 32191ae08745Sheppo vdc_t *vdc = (vdc_t *)(void *)arg; 32201ae08745Sheppo 32211ae08745Sheppo ASSERT(vdc != NULL); 32221ae08745Sheppo 32233af08d82Slm66018 DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num); 32241ae08745Sheppo 32251ae08745Sheppo /* 32261ae08745Sheppo * Depending on the type of event that triggered this callback, 32273af08d82Slm66018 * we modify the handshake state or read the data. 32281ae08745Sheppo * 32291ae08745Sheppo * NOTE: not done as a switch() as event could be triggered by 32301ae08745Sheppo * a state change and a read request. Also the ordering of the 32311ae08745Sheppo * check for the event types is deliberate. 32321ae08745Sheppo */ 32331ae08745Sheppo if (event & LDC_EVT_UP) { 32343af08d82Slm66018 DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance); 32353af08d82Slm66018 32363af08d82Slm66018 mutex_enter(&vdc->lock); 32371ae08745Sheppo 32381ae08745Sheppo /* get LDC state */ 32391ae08745Sheppo rv = ldc_status(vdc->ldc_handle, &ldc_state); 32401ae08745Sheppo if (rv != 0) { 32413af08d82Slm66018 DMSG(vdc, 0, "[%d] Couldn't get LDC status %d", 32421ae08745Sheppo vdc->instance, rv); 32431ae08745Sheppo return (LDC_SUCCESS); 32441ae08745Sheppo } 32453af08d82Slm66018 if (vdc->ldc_state != LDC_UP && ldc_state == LDC_UP) { 32461ae08745Sheppo /* 32473af08d82Slm66018 * Reset the transaction sequence numbers when 32483af08d82Slm66018 * LDC comes up. We then kick off the handshake 32493af08d82Slm66018 * negotiation with the vDisk server. 32501ae08745Sheppo */ 32510a55fbb7Slm66018 vdc->seq_num = 1; 32521ae08745Sheppo vdc->seq_num_reply = 0; 32531ae08745Sheppo vdc->ldc_state = ldc_state; 32543af08d82Slm66018 cv_signal(&vdc->initwait_cv); 32553af08d82Slm66018 } 32563af08d82Slm66018 32571ae08745Sheppo mutex_exit(&vdc->lock); 32581ae08745Sheppo } 32591ae08745Sheppo 32601ae08745Sheppo if (event & LDC_EVT_READ) { 3261*17cadca8Slm66018 DMSG(vdc, 1, "[%d] Received LDC_EVT_READ\n", vdc->instance); 32623af08d82Slm66018 mutex_enter(&vdc->read_lock); 32633af08d82Slm66018 cv_signal(&vdc->read_cv); 32643af08d82Slm66018 vdc->read_state = VDC_READ_PENDING; 32653af08d82Slm66018 mutex_exit(&vdc->read_lock); 32661ae08745Sheppo 32671ae08745Sheppo /* that's all we have to do - no need to handle DOWN/RESET */ 32681ae08745Sheppo return (LDC_SUCCESS); 32691ae08745Sheppo } 32701ae08745Sheppo 32713af08d82Slm66018 if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) { 32720a55fbb7Slm66018 32733af08d82Slm66018 DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance); 32743af08d82Slm66018 32750a55fbb7Slm66018 mutex_enter(&vdc->lock); 32763af08d82Slm66018 /* 32773af08d82Slm66018 * Need to wake up any readers so they will 32783af08d82Slm66018 * detect that a reset has occurred. 32793af08d82Slm66018 */ 32803af08d82Slm66018 mutex_enter(&vdc->read_lock); 32813af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 32823af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) 32833af08d82Slm66018 cv_signal(&vdc->read_cv); 32843af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 32853af08d82Slm66018 mutex_exit(&vdc->read_lock); 32860a55fbb7Slm66018 32873af08d82Slm66018 /* wake up any threads waiting for connection to come up */ 32883af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 32893af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 32903af08d82Slm66018 cv_signal(&vdc->initwait_cv); 32911ae08745Sheppo } 32921ae08745Sheppo 32930a55fbb7Slm66018 mutex_exit(&vdc->lock); 32941ae08745Sheppo } 32951ae08745Sheppo 32961ae08745Sheppo if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ)) 32973af08d82Slm66018 DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received", 32981ae08745Sheppo vdc->instance, event); 32991ae08745Sheppo 33001ae08745Sheppo return (LDC_SUCCESS); 33011ae08745Sheppo } 33021ae08745Sheppo 33033af08d82Slm66018 /* 33043af08d82Slm66018 * Function: 33053af08d82Slm66018 * vdc_wait_for_response() 33063af08d82Slm66018 * 33073af08d82Slm66018 * Description: 33083af08d82Slm66018 * Block waiting for a response from the server. If there is 33093af08d82Slm66018 * no data the thread block on the read_cv that is signalled 33103af08d82Slm66018 * by the callback when an EVT_READ occurs. 33113af08d82Slm66018 * 33123af08d82Slm66018 * Arguments: 33133af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 33143af08d82Slm66018 * 33153af08d82Slm66018 * Return Code: 33163af08d82Slm66018 * 0 - Success 33173af08d82Slm66018 */ 33183af08d82Slm66018 static int 33193af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp) 33203af08d82Slm66018 { 33213af08d82Slm66018 size_t nbytes = sizeof (*msgp); 33223af08d82Slm66018 int status; 33233af08d82Slm66018 33243af08d82Slm66018 ASSERT(vdcp != NULL); 33253af08d82Slm66018 33263af08d82Slm66018 DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance); 33273af08d82Slm66018 33283af08d82Slm66018 status = vdc_recv(vdcp, msgp, &nbytes); 33293af08d82Slm66018 DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n", 33303af08d82Slm66018 status, (int)nbytes); 33313af08d82Slm66018 if (status) { 33323af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n", 33333af08d82Slm66018 vdcp->instance, status); 33343af08d82Slm66018 return (status); 33353af08d82Slm66018 } 33363af08d82Slm66018 33373af08d82Slm66018 if (nbytes < sizeof (vio_msg_tag_t)) { 33383af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n", 33393af08d82Slm66018 vdcp->instance, sizeof (vio_msg_tag_t), nbytes); 33403af08d82Slm66018 return (ENOMSG); 33413af08d82Slm66018 } 33423af08d82Slm66018 33433af08d82Slm66018 DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance, 33443af08d82Slm66018 msgp->tag.vio_msgtype, 33453af08d82Slm66018 msgp->tag.vio_subtype, 33463af08d82Slm66018 msgp->tag.vio_subtype_env); 33473af08d82Slm66018 33483af08d82Slm66018 /* 33493af08d82Slm66018 * Verify the Session ID of the message 33503af08d82Slm66018 * 33513af08d82Slm66018 * Every message after the Version has been negotiated should 33523af08d82Slm66018 * have the correct session ID set. 33533af08d82Slm66018 */ 33543af08d82Slm66018 if ((msgp->tag.vio_sid != vdcp->session_id) && 33553af08d82Slm66018 (msgp->tag.vio_subtype_env != VIO_VER_INFO)) { 33563af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, " 33573af08d82Slm66018 "expected 0x%lx [seq num %lx @ %d]", 33583af08d82Slm66018 vdcp->instance, msgp->tag.vio_sid, 33593af08d82Slm66018 vdcp->session_id, 33603af08d82Slm66018 ((vio_dring_msg_t *)msgp)->seq_num, 33613af08d82Slm66018 ((vio_dring_msg_t *)msgp)->start_idx); 33623af08d82Slm66018 return (ENOMSG); 33633af08d82Slm66018 } 33643af08d82Slm66018 return (0); 33653af08d82Slm66018 } 33663af08d82Slm66018 33673af08d82Slm66018 33683af08d82Slm66018 /* 33693af08d82Slm66018 * Function: 33703af08d82Slm66018 * vdc_resubmit_backup_dring() 33713af08d82Slm66018 * 33723af08d82Slm66018 * Description: 33733af08d82Slm66018 * Resubmit each descriptor in the backed up dring to 33743af08d82Slm66018 * vDisk server. The Dring was backed up during connection 33753af08d82Slm66018 * reset. 33763af08d82Slm66018 * 33773af08d82Slm66018 * Arguments: 33783af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 33793af08d82Slm66018 * 33803af08d82Slm66018 * Return Code: 33813af08d82Slm66018 * 0 - Success 33823af08d82Slm66018 */ 33833af08d82Slm66018 static int 33843af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp) 33853af08d82Slm66018 { 33863af08d82Slm66018 int count; 33873af08d82Slm66018 int b_idx; 33883af08d82Slm66018 int rv; 33893af08d82Slm66018 int dring_size; 33903af08d82Slm66018 int status; 33913af08d82Slm66018 vio_msg_t vio_msg; 33923af08d82Slm66018 vdc_local_desc_t *curr_ldep; 33933af08d82Slm66018 33943af08d82Slm66018 ASSERT(MUTEX_NOT_HELD(&vdcp->lock)); 33953af08d82Slm66018 ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING); 33963af08d82Slm66018 3397655fd6a9Sachartre if (vdcp->local_dring_backup == NULL) { 3398655fd6a9Sachartre /* the pending requests have already been processed */ 3399655fd6a9Sachartre return (0); 3400655fd6a9Sachartre } 3401655fd6a9Sachartre 34023af08d82Slm66018 DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n", 34033af08d82Slm66018 vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail); 34043af08d82Slm66018 34053af08d82Slm66018 /* 34063af08d82Slm66018 * Walk the backup copy of the local descriptor ring and 34073af08d82Slm66018 * resubmit all the outstanding transactions. 34083af08d82Slm66018 */ 34093af08d82Slm66018 b_idx = vdcp->local_dring_backup_tail; 34103af08d82Slm66018 for (count = 0; count < vdcp->local_dring_backup_len; count++) { 34113af08d82Slm66018 34123af08d82Slm66018 curr_ldep = &(vdcp->local_dring_backup[b_idx]); 34133af08d82Slm66018 3414eff7243fSlm66018 /* only resubmit outstanding transactions */ 34153af08d82Slm66018 if (!curr_ldep->is_free) { 34163af08d82Slm66018 34173af08d82Slm66018 DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx); 34183af08d82Slm66018 mutex_enter(&vdcp->lock); 34193af08d82Slm66018 rv = vdc_populate_descriptor(vdcp, curr_ldep->operation, 34203af08d82Slm66018 curr_ldep->addr, curr_ldep->nbytes, 34213af08d82Slm66018 curr_ldep->slice, curr_ldep->offset, 34223af08d82Slm66018 curr_ldep->cb_type, curr_ldep->cb_arg, 34233af08d82Slm66018 curr_ldep->dir); 34243af08d82Slm66018 mutex_exit(&vdcp->lock); 34253af08d82Slm66018 if (rv) { 34263af08d82Slm66018 DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n", 34273af08d82Slm66018 vdcp->instance, b_idx); 34283af08d82Slm66018 return (rv); 34293af08d82Slm66018 } 34303af08d82Slm66018 34313af08d82Slm66018 /* Wait for the response message. */ 34323af08d82Slm66018 DMSG(vdcp, 1, "waiting for response to idx=%x\n", 34333af08d82Slm66018 b_idx); 34343af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 34353af08d82Slm66018 if (status) { 34363af08d82Slm66018 DMSG(vdcp, 1, "[%d] wait_for_response " 34373af08d82Slm66018 "returned err=%d\n", vdcp->instance, 34383af08d82Slm66018 status); 34393af08d82Slm66018 return (status); 34403af08d82Slm66018 } 34413af08d82Slm66018 34423af08d82Slm66018 DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx); 34433af08d82Slm66018 status = vdc_process_data_msg(vdcp, &vio_msg); 34443af08d82Slm66018 if (status) { 34453af08d82Slm66018 DMSG(vdcp, 1, "[%d] process_data_msg " 34463af08d82Slm66018 "returned err=%d\n", vdcp->instance, 34473af08d82Slm66018 status); 34483af08d82Slm66018 return (status); 34493af08d82Slm66018 } 34503af08d82Slm66018 } 34513af08d82Slm66018 34523af08d82Slm66018 /* get the next element to submit */ 34533af08d82Slm66018 if (++b_idx >= vdcp->local_dring_backup_len) 34543af08d82Slm66018 b_idx = 0; 34553af08d82Slm66018 } 34563af08d82Slm66018 34573af08d82Slm66018 /* all done - now clear up pending dring copy */ 34583af08d82Slm66018 dring_size = vdcp->local_dring_backup_len * 34593af08d82Slm66018 sizeof (vdcp->local_dring_backup[0]); 34603af08d82Slm66018 34613af08d82Slm66018 (void) kmem_free(vdcp->local_dring_backup, dring_size); 34623af08d82Slm66018 34633af08d82Slm66018 vdcp->local_dring_backup = NULL; 34643af08d82Slm66018 34653af08d82Slm66018 return (0); 34663af08d82Slm66018 } 34673af08d82Slm66018 34683af08d82Slm66018 /* 34693af08d82Slm66018 * Function: 3470655fd6a9Sachartre * vdc_cancel_backup_dring 3471655fd6a9Sachartre * 3472655fd6a9Sachartre * Description: 3473655fd6a9Sachartre * Cancel each descriptor in the backed up dring to vDisk server. 3474655fd6a9Sachartre * The Dring was backed up during connection reset. 3475655fd6a9Sachartre * 3476655fd6a9Sachartre * Arguments: 3477655fd6a9Sachartre * vdcp - soft state pointer for this instance of the device driver. 3478655fd6a9Sachartre * 3479655fd6a9Sachartre * Return Code: 3480655fd6a9Sachartre * None 3481655fd6a9Sachartre */ 3482655fd6a9Sachartre void 3483655fd6a9Sachartre vdc_cancel_backup_ring(vdc_t *vdcp) 3484655fd6a9Sachartre { 3485655fd6a9Sachartre vdc_local_desc_t *ldep; 3486655fd6a9Sachartre struct buf *bufp; 3487655fd6a9Sachartre int count; 3488655fd6a9Sachartre int b_idx; 3489655fd6a9Sachartre int dring_size; 3490655fd6a9Sachartre 3491655fd6a9Sachartre ASSERT(MUTEX_HELD(&vdcp->lock)); 3492655fd6a9Sachartre ASSERT(vdcp->state == VDC_STATE_INIT || 3493655fd6a9Sachartre vdcp->state == VDC_STATE_INIT_WAITING || 3494655fd6a9Sachartre vdcp->state == VDC_STATE_NEGOTIATE || 3495655fd6a9Sachartre vdcp->state == VDC_STATE_RESETTING); 3496655fd6a9Sachartre 3497655fd6a9Sachartre if (vdcp->local_dring_backup == NULL) { 3498655fd6a9Sachartre /* the pending requests have already been processed */ 3499655fd6a9Sachartre return; 3500655fd6a9Sachartre } 3501655fd6a9Sachartre 3502655fd6a9Sachartre DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n", 3503655fd6a9Sachartre vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail); 3504655fd6a9Sachartre 3505655fd6a9Sachartre /* 3506655fd6a9Sachartre * Walk the backup copy of the local descriptor ring and 3507655fd6a9Sachartre * cancel all the outstanding transactions. 3508655fd6a9Sachartre */ 3509655fd6a9Sachartre b_idx = vdcp->local_dring_backup_tail; 3510655fd6a9Sachartre for (count = 0; count < vdcp->local_dring_backup_len; count++) { 3511655fd6a9Sachartre 3512655fd6a9Sachartre ldep = &(vdcp->local_dring_backup[b_idx]); 3513655fd6a9Sachartre 3514655fd6a9Sachartre /* only cancel outstanding transactions */ 3515655fd6a9Sachartre if (!ldep->is_free) { 3516655fd6a9Sachartre 3517655fd6a9Sachartre DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx); 3518655fd6a9Sachartre 3519655fd6a9Sachartre /* 3520655fd6a9Sachartre * All requests have already been cleared from the 3521655fd6a9Sachartre * local descriptor ring and the LDC channel has been 3522655fd6a9Sachartre * reset so we will never get any reply for these 3523655fd6a9Sachartre * requests. Now we just have to notify threads waiting 3524655fd6a9Sachartre * for replies that the request has failed. 3525655fd6a9Sachartre */ 3526655fd6a9Sachartre switch (ldep->cb_type) { 3527655fd6a9Sachartre case CB_SYNC: 3528655fd6a9Sachartre ASSERT(vdcp->sync_op_pending); 3529655fd6a9Sachartre vdcp->sync_op_status = EIO; 3530655fd6a9Sachartre vdcp->sync_op_pending = B_FALSE; 3531655fd6a9Sachartre cv_signal(&vdcp->sync_pending_cv); 3532655fd6a9Sachartre break; 3533655fd6a9Sachartre 3534655fd6a9Sachartre case CB_STRATEGY: 3535655fd6a9Sachartre bufp = ldep->cb_arg; 3536655fd6a9Sachartre ASSERT(bufp != NULL); 3537655fd6a9Sachartre bufp->b_resid = bufp->b_bcount; 3538655fd6a9Sachartre bioerror(bufp, EIO); 3539655fd6a9Sachartre biodone(bufp); 3540655fd6a9Sachartre break; 3541655fd6a9Sachartre 3542655fd6a9Sachartre default: 3543655fd6a9Sachartre ASSERT(0); 3544655fd6a9Sachartre } 3545655fd6a9Sachartre 3546655fd6a9Sachartre } 3547655fd6a9Sachartre 3548655fd6a9Sachartre /* get the next element to cancel */ 3549655fd6a9Sachartre if (++b_idx >= vdcp->local_dring_backup_len) 3550655fd6a9Sachartre b_idx = 0; 3551655fd6a9Sachartre } 3552655fd6a9Sachartre 3553655fd6a9Sachartre /* all done - now clear up pending dring copy */ 3554655fd6a9Sachartre dring_size = vdcp->local_dring_backup_len * 3555655fd6a9Sachartre sizeof (vdcp->local_dring_backup[0]); 3556655fd6a9Sachartre 3557655fd6a9Sachartre (void) kmem_free(vdcp->local_dring_backup, dring_size); 3558655fd6a9Sachartre 3559655fd6a9Sachartre vdcp->local_dring_backup = NULL; 3560655fd6a9Sachartre 3561655fd6a9Sachartre DTRACE_IO2(processed, int, count, vdc_t *, vdcp); 3562655fd6a9Sachartre } 3563655fd6a9Sachartre 3564655fd6a9Sachartre /* 3565655fd6a9Sachartre * Function: 3566655fd6a9Sachartre * vdc_connection_timeout 3567655fd6a9Sachartre * 3568655fd6a9Sachartre * Description: 3569655fd6a9Sachartre * This function is invoked if the timeout set to establish the connection 3570655fd6a9Sachartre * with vds expires. This will happen if we spend too much time in the 3571655fd6a9Sachartre * VDC_STATE_INIT_WAITING or VDC_STATE_NEGOTIATE states. Then we will 3572655fd6a9Sachartre * cancel any pending request and mark them as failed. 3573655fd6a9Sachartre * 3574655fd6a9Sachartre * If the timeout does not expire, it will be cancelled when we reach the 3575655fd6a9Sachartre * VDC_STATE_HANDLE_PENDING or VDC_STATE_RESETTING state. This function can 3576655fd6a9Sachartre * be invoked while we are in the VDC_STATE_HANDLE_PENDING or 3577655fd6a9Sachartre * VDC_STATE_RESETTING state in which case we do nothing because the 3578655fd6a9Sachartre * timeout is being cancelled. 3579655fd6a9Sachartre * 3580655fd6a9Sachartre * Arguments: 3581655fd6a9Sachartre * arg - argument of the timeout function actually a soft state 3582655fd6a9Sachartre * pointer for the instance of the device driver. 3583655fd6a9Sachartre * 3584655fd6a9Sachartre * Return Code: 3585655fd6a9Sachartre * None 3586655fd6a9Sachartre */ 3587655fd6a9Sachartre void 3588655fd6a9Sachartre vdc_connection_timeout(void *arg) 3589655fd6a9Sachartre { 3590655fd6a9Sachartre vdc_t *vdcp = (vdc_t *)arg; 3591655fd6a9Sachartre 3592655fd6a9Sachartre mutex_enter(&vdcp->lock); 3593655fd6a9Sachartre 3594655fd6a9Sachartre if (vdcp->state == VDC_STATE_HANDLE_PENDING || 3595655fd6a9Sachartre vdcp->state == VDC_STATE_DETACH) { 3596655fd6a9Sachartre /* 3597655fd6a9Sachartre * The connection has just been re-established or 3598655fd6a9Sachartre * we are detaching. 3599655fd6a9Sachartre */ 3600655fd6a9Sachartre vdcp->ctimeout_reached = B_FALSE; 3601655fd6a9Sachartre mutex_exit(&vdcp->lock); 3602655fd6a9Sachartre return; 3603655fd6a9Sachartre } 3604655fd6a9Sachartre 3605655fd6a9Sachartre vdcp->ctimeout_reached = B_TRUE; 3606655fd6a9Sachartre 3607655fd6a9Sachartre /* notify requests waiting for sending */ 3608655fd6a9Sachartre cv_broadcast(&vdcp->running_cv); 3609655fd6a9Sachartre 3610655fd6a9Sachartre /* cancel requests waiting for a result */ 3611655fd6a9Sachartre vdc_cancel_backup_ring(vdcp); 3612655fd6a9Sachartre 3613655fd6a9Sachartre mutex_exit(&vdcp->lock); 3614655fd6a9Sachartre 3615655fd6a9Sachartre cmn_err(CE_NOTE, "[%d] connection to service domain timeout", 3616655fd6a9Sachartre vdcp->instance); 3617655fd6a9Sachartre } 3618655fd6a9Sachartre 3619655fd6a9Sachartre /* 3620655fd6a9Sachartre * Function: 36213af08d82Slm66018 * vdc_backup_local_dring() 36223af08d82Slm66018 * 36233af08d82Slm66018 * Description: 36243af08d82Slm66018 * Backup the current dring in the event of a reset. The Dring 36253af08d82Slm66018 * transactions will be resubmitted to the server when the 36263af08d82Slm66018 * connection is restored. 36273af08d82Slm66018 * 36283af08d82Slm66018 * Arguments: 36293af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 36303af08d82Slm66018 * 36313af08d82Slm66018 * Return Code: 36323af08d82Slm66018 * NONE 36333af08d82Slm66018 */ 36343af08d82Slm66018 static void 36353af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp) 36363af08d82Slm66018 { 36373af08d82Slm66018 int dring_size; 36383af08d82Slm66018 3639655fd6a9Sachartre ASSERT(MUTEX_HELD(&vdcp->lock)); 36403af08d82Slm66018 ASSERT(vdcp->state == VDC_STATE_RESETTING); 36413af08d82Slm66018 36423af08d82Slm66018 /* 36433af08d82Slm66018 * If the backup dring is stil around, it means 36443af08d82Slm66018 * that the last restore did not complete. However, 36453af08d82Slm66018 * since we never got back into the running state, 36463af08d82Slm66018 * the backup copy we have is still valid. 36473af08d82Slm66018 */ 36483af08d82Slm66018 if (vdcp->local_dring_backup != NULL) { 36493af08d82Slm66018 DMSG(vdcp, 1, "reusing local descriptor ring backup " 36503af08d82Slm66018 "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len, 36513af08d82Slm66018 vdcp->local_dring_backup_tail); 36523af08d82Slm66018 return; 36533af08d82Slm66018 } 36543af08d82Slm66018 3655655fd6a9Sachartre /* 3656655fd6a9Sachartre * The backup dring can be NULL and the local dring may not be 3657655fd6a9Sachartre * initialized. This can happen if we had a reset while establishing 3658655fd6a9Sachartre * a new connection but after the connection has timed out. In that 3659655fd6a9Sachartre * case the backup dring is NULL because the requests have been 3660655fd6a9Sachartre * cancelled and the request occured before the local dring is 3661655fd6a9Sachartre * initialized. 3662655fd6a9Sachartre */ 3663655fd6a9Sachartre if (!(vdcp->initialized & VDC_DRING_LOCAL)) 3664655fd6a9Sachartre return; 3665655fd6a9Sachartre 36663af08d82Slm66018 DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, " 36673af08d82Slm66018 "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx); 36683af08d82Slm66018 36693af08d82Slm66018 dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]); 36703af08d82Slm66018 36713af08d82Slm66018 vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP); 36723af08d82Slm66018 bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size); 36733af08d82Slm66018 36743af08d82Slm66018 vdcp->local_dring_backup_tail = vdcp->dring_curr_idx; 36753af08d82Slm66018 vdcp->local_dring_backup_len = vdcp->dring_len; 36763af08d82Slm66018 } 36773af08d82Slm66018 36781ae08745Sheppo /* -------------------------------------------------------------------------- */ 36791ae08745Sheppo 36801ae08745Sheppo /* 36811ae08745Sheppo * The following functions process the incoming messages from vds 36821ae08745Sheppo */ 36831ae08745Sheppo 36840a55fbb7Slm66018 /* 36850a55fbb7Slm66018 * Function: 36860a55fbb7Slm66018 * vdc_process_msg_thread() 36870a55fbb7Slm66018 * 36880a55fbb7Slm66018 * Description: 36890a55fbb7Slm66018 * 36903af08d82Slm66018 * Main VDC message processing thread. Each vDisk instance 36913af08d82Slm66018 * consists of a copy of this thread. This thread triggers 36923af08d82Slm66018 * all the handshakes and data exchange with the server. It 36933af08d82Slm66018 * also handles all channel resets 36943af08d82Slm66018 * 36950a55fbb7Slm66018 * Arguments: 36960a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 36970a55fbb7Slm66018 * 36980a55fbb7Slm66018 * Return Code: 36990a55fbb7Slm66018 * None 37000a55fbb7Slm66018 */ 37011ae08745Sheppo static void 37023af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp) 37031ae08745Sheppo { 37041ae08745Sheppo int status; 3705655fd6a9Sachartre int ctimeout; 3706655fd6a9Sachartre timeout_id_t tmid = 0; 37071ae08745Sheppo 37083af08d82Slm66018 mutex_enter(&vdcp->lock); 37091ae08745Sheppo 37101ae08745Sheppo for (;;) { 37111ae08745Sheppo 37123af08d82Slm66018 #define Q(_s) (vdcp->state == _s) ? #_s : 37133af08d82Slm66018 DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state, 37143af08d82Slm66018 Q(VDC_STATE_INIT) 37153af08d82Slm66018 Q(VDC_STATE_INIT_WAITING) 37163af08d82Slm66018 Q(VDC_STATE_NEGOTIATE) 37173af08d82Slm66018 Q(VDC_STATE_HANDLE_PENDING) 37183af08d82Slm66018 Q(VDC_STATE_RUNNING) 37193af08d82Slm66018 Q(VDC_STATE_RESETTING) 37203af08d82Slm66018 Q(VDC_STATE_DETACH) 37213af08d82Slm66018 "UNKNOWN"); 37221ae08745Sheppo 37233af08d82Slm66018 switch (vdcp->state) { 37243af08d82Slm66018 case VDC_STATE_INIT: 37253af08d82Slm66018 3726655fd6a9Sachartre /* 3727655fd6a9Sachartre * If requested, start a timeout to check if the 3728655fd6a9Sachartre * connection with vds is established in the 3729655fd6a9Sachartre * specified delay. If the timeout expires, we 3730655fd6a9Sachartre * will cancel any pending request. 3731655fd6a9Sachartre * 3732655fd6a9Sachartre * If some reset have occurred while establishing 3733655fd6a9Sachartre * the connection, we already have a timeout armed 3734655fd6a9Sachartre * and in that case we don't need to arm a new one. 3735655fd6a9Sachartre */ 3736655fd6a9Sachartre ctimeout = (vdc_timeout != 0)? 3737655fd6a9Sachartre vdc_timeout : vdcp->ctimeout; 3738655fd6a9Sachartre 3739655fd6a9Sachartre if (ctimeout != 0 && tmid == 0) { 3740655fd6a9Sachartre tmid = timeout(vdc_connection_timeout, vdcp, 3741655fd6a9Sachartre ctimeout * drv_usectohz(1000000)); 3742655fd6a9Sachartre } 3743655fd6a9Sachartre 37443af08d82Slm66018 /* Check if have re-initializing repeatedly */ 3745655fd6a9Sachartre if (vdcp->hshake_cnt++ > vdc_hshake_retries && 3746655fd6a9Sachartre vdcp->lifecycle != VDC_LC_ONLINE) { 37473c96341aSnarayan cmn_err(CE_NOTE, "[%d] disk access failed.\n", 37483c96341aSnarayan vdcp->instance); 37493af08d82Slm66018 vdcp->state = VDC_STATE_DETACH; 37503af08d82Slm66018 break; 37513af08d82Slm66018 } 37523af08d82Slm66018 37533af08d82Slm66018 /* Bring up connection with vds via LDC */ 37543af08d82Slm66018 status = vdc_start_ldc_connection(vdcp); 3755655fd6a9Sachartre if (status == EINVAL) { 37563af08d82Slm66018 DMSG(vdcp, 0, "[%d] Could not start LDC", 37573af08d82Slm66018 vdcp->instance); 37583af08d82Slm66018 vdcp->state = VDC_STATE_DETACH; 3759655fd6a9Sachartre } else { 37603af08d82Slm66018 vdcp->state = VDC_STATE_INIT_WAITING; 37613af08d82Slm66018 } 37623af08d82Slm66018 break; 37633af08d82Slm66018 37643af08d82Slm66018 case VDC_STATE_INIT_WAITING: 37653af08d82Slm66018 37663af08d82Slm66018 /* 37673af08d82Slm66018 * Let the callback event move us on 37683af08d82Slm66018 * when channel is open to server 37693af08d82Slm66018 */ 37703af08d82Slm66018 while (vdcp->ldc_state != LDC_UP) { 37713af08d82Slm66018 cv_wait(&vdcp->initwait_cv, &vdcp->lock); 37723af08d82Slm66018 if (vdcp->state != VDC_STATE_INIT_WAITING) { 37733af08d82Slm66018 DMSG(vdcp, 0, 37743af08d82Slm66018 "state moved to %d out from under us...\n", 37753af08d82Slm66018 vdcp->state); 37763af08d82Slm66018 37773af08d82Slm66018 break; 37783af08d82Slm66018 } 37793af08d82Slm66018 } 37803af08d82Slm66018 if (vdcp->state == VDC_STATE_INIT_WAITING && 37813af08d82Slm66018 vdcp->ldc_state == LDC_UP) { 37823af08d82Slm66018 vdcp->state = VDC_STATE_NEGOTIATE; 37833af08d82Slm66018 } 37843af08d82Slm66018 break; 37853af08d82Slm66018 37863af08d82Slm66018 case VDC_STATE_NEGOTIATE: 37873af08d82Slm66018 switch (status = vdc_ver_negotiation(vdcp)) { 37883af08d82Slm66018 case 0: 37893af08d82Slm66018 break; 37903af08d82Slm66018 default: 37913af08d82Slm66018 DMSG(vdcp, 0, "ver negotiate failed (%d)..\n", 37923af08d82Slm66018 status); 37933af08d82Slm66018 goto reset; 37943af08d82Slm66018 } 37953af08d82Slm66018 37963af08d82Slm66018 switch (status = vdc_attr_negotiation(vdcp)) { 37973af08d82Slm66018 case 0: 37983af08d82Slm66018 break; 37993af08d82Slm66018 default: 38003af08d82Slm66018 DMSG(vdcp, 0, "attr negotiate failed (%d)..\n", 38013af08d82Slm66018 status); 38023af08d82Slm66018 goto reset; 38033af08d82Slm66018 } 38043af08d82Slm66018 38053af08d82Slm66018 switch (status = vdc_dring_negotiation(vdcp)) { 38063af08d82Slm66018 case 0: 38073af08d82Slm66018 break; 38083af08d82Slm66018 default: 38093af08d82Slm66018 DMSG(vdcp, 0, "dring negotiate failed (%d)..\n", 38103af08d82Slm66018 status); 38113af08d82Slm66018 goto reset; 38123af08d82Slm66018 } 38133af08d82Slm66018 38143af08d82Slm66018 switch (status = vdc_rdx_exchange(vdcp)) { 38153af08d82Slm66018 case 0: 38163af08d82Slm66018 vdcp->state = VDC_STATE_HANDLE_PENDING; 38173af08d82Slm66018 goto done; 38183af08d82Slm66018 default: 38193af08d82Slm66018 DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n", 38203af08d82Slm66018 status); 38213af08d82Slm66018 goto reset; 38223af08d82Slm66018 } 38233af08d82Slm66018 reset: 38243af08d82Slm66018 DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n", 38253af08d82Slm66018 status); 38263af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 3827655fd6a9Sachartre vdcp->self_reset = B_TRUE; 38283af08d82Slm66018 done: 38293af08d82Slm66018 DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n", 38303af08d82Slm66018 vdcp->state); 38313af08d82Slm66018 break; 38323af08d82Slm66018 38333af08d82Slm66018 case VDC_STATE_HANDLE_PENDING: 38343af08d82Slm66018 3835655fd6a9Sachartre if (vdcp->ctimeout_reached) { 3836655fd6a9Sachartre /* 3837655fd6a9Sachartre * The connection timeout had been reached so 3838655fd6a9Sachartre * pending requests have been cancelled. Now 3839655fd6a9Sachartre * that the connection is back we can reset 3840655fd6a9Sachartre * the timeout. 3841655fd6a9Sachartre */ 3842655fd6a9Sachartre ASSERT(vdcp->local_dring_backup == NULL); 3843655fd6a9Sachartre ASSERT(tmid != 0); 3844655fd6a9Sachartre tmid = 0; 3845655fd6a9Sachartre vdcp->ctimeout_reached = B_FALSE; 3846655fd6a9Sachartre vdcp->state = VDC_STATE_RUNNING; 3847655fd6a9Sachartre DMSG(vdcp, 0, "[%d] connection to service " 3848655fd6a9Sachartre "domain is up", vdcp->instance); 3849655fd6a9Sachartre break; 3850655fd6a9Sachartre } 3851655fd6a9Sachartre 38523af08d82Slm66018 mutex_exit(&vdcp->lock); 3853655fd6a9Sachartre if (tmid != 0) { 3854655fd6a9Sachartre (void) untimeout(tmid); 3855655fd6a9Sachartre tmid = 0; 3856655fd6a9Sachartre } 38573af08d82Slm66018 status = vdc_resubmit_backup_dring(vdcp); 38583af08d82Slm66018 mutex_enter(&vdcp->lock); 38593af08d82Slm66018 38603af08d82Slm66018 if (status) 38613af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 38623af08d82Slm66018 else 38633af08d82Slm66018 vdcp->state = VDC_STATE_RUNNING; 38643af08d82Slm66018 38653af08d82Slm66018 break; 38663af08d82Slm66018 38673af08d82Slm66018 /* enter running state */ 38683af08d82Slm66018 case VDC_STATE_RUNNING: 38693af08d82Slm66018 /* 38703af08d82Slm66018 * Signal anyone waiting for the connection 38713af08d82Slm66018 * to come on line. 38723af08d82Slm66018 */ 38733af08d82Slm66018 vdcp->hshake_cnt = 0; 38743af08d82Slm66018 cv_broadcast(&vdcp->running_cv); 38753af08d82Slm66018 mutex_exit(&vdcp->lock); 38763af08d82Slm66018 38773af08d82Slm66018 for (;;) { 38783af08d82Slm66018 vio_msg_t msg; 38793af08d82Slm66018 status = vdc_wait_for_response(vdcp, &msg); 38803af08d82Slm66018 if (status) break; 38813af08d82Slm66018 38823af08d82Slm66018 DMSG(vdcp, 1, "[%d] new pkt(s) available\n", 38833af08d82Slm66018 vdcp->instance); 38843af08d82Slm66018 status = vdc_process_data_msg(vdcp, &msg); 38851ae08745Sheppo if (status) { 38863af08d82Slm66018 DMSG(vdcp, 1, "[%d] process_data_msg " 38873af08d82Slm66018 "returned err=%d\n", vdcp->instance, 38883af08d82Slm66018 status); 38891ae08745Sheppo break; 38901ae08745Sheppo } 38911ae08745Sheppo 38923af08d82Slm66018 } 3893e1ebb9ecSlm66018 38943af08d82Slm66018 mutex_enter(&vdcp->lock); 38953af08d82Slm66018 38963af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 3897690555a1Sachartre vdcp->self_reset = B_TRUE; 38983af08d82Slm66018 break; 38993af08d82Slm66018 39003af08d82Slm66018 case VDC_STATE_RESETTING: 3901655fd6a9Sachartre /* 3902655fd6a9Sachartre * When we reach this state, we either come from the 3903655fd6a9Sachartre * VDC_STATE_RUNNING state and we can have pending 3904655fd6a9Sachartre * request but no timeout is armed; or we come from 3905655fd6a9Sachartre * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or 3906655fd6a9Sachartre * VDC_HANDLE_PENDING state and there is no pending 3907655fd6a9Sachartre * request or pending requests have already been copied 3908655fd6a9Sachartre * into the backup dring. So we can safely keep the 3909655fd6a9Sachartre * connection timeout armed while we are in this state. 3910655fd6a9Sachartre */ 3911655fd6a9Sachartre 39123af08d82Slm66018 DMSG(vdcp, 0, "Initiating channel reset " 39133af08d82Slm66018 "(pending = %d)\n", (int)vdcp->threads_pending); 39143af08d82Slm66018 39153af08d82Slm66018 if (vdcp->self_reset) { 39163af08d82Slm66018 DMSG(vdcp, 0, 39173af08d82Slm66018 "[%d] calling stop_ldc_connection.\n", 39183af08d82Slm66018 vdcp->instance); 39193af08d82Slm66018 status = vdc_stop_ldc_connection(vdcp); 39203af08d82Slm66018 vdcp->self_reset = B_FALSE; 39211ae08745Sheppo } 39221ae08745Sheppo 39231ae08745Sheppo /* 39243af08d82Slm66018 * Wait for all threads currently waiting 39253af08d82Slm66018 * for a free dring entry to use. 39261ae08745Sheppo */ 39273af08d82Slm66018 while (vdcp->threads_pending) { 39283af08d82Slm66018 cv_broadcast(&vdcp->membind_cv); 39293af08d82Slm66018 cv_broadcast(&vdcp->dring_free_cv); 39303af08d82Slm66018 mutex_exit(&vdcp->lock); 3931205eeb1aSlm66018 /* give the waiters enough time to wake up */ 3932205eeb1aSlm66018 delay(vdc_hz_min_ldc_delay); 39333af08d82Slm66018 mutex_enter(&vdcp->lock); 39341ae08745Sheppo } 39351ae08745Sheppo 39363af08d82Slm66018 ASSERT(vdcp->threads_pending == 0); 39371ae08745Sheppo 39383af08d82Slm66018 /* Sanity check that no thread is receiving */ 39393af08d82Slm66018 ASSERT(vdcp->read_state != VDC_READ_WAITING); 39400a55fbb7Slm66018 39413af08d82Slm66018 vdcp->read_state = VDC_READ_IDLE; 39423af08d82Slm66018 39433af08d82Slm66018 vdc_backup_local_dring(vdcp); 39443af08d82Slm66018 39453af08d82Slm66018 /* cleanup the old d-ring */ 39463af08d82Slm66018 vdc_destroy_descriptor_ring(vdcp); 39473af08d82Slm66018 39483af08d82Slm66018 /* go and start again */ 39493af08d82Slm66018 vdcp->state = VDC_STATE_INIT; 39503af08d82Slm66018 39510a55fbb7Slm66018 break; 39520a55fbb7Slm66018 39533af08d82Slm66018 case VDC_STATE_DETACH: 39543af08d82Slm66018 DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n", 39553af08d82Slm66018 vdcp->instance); 39563af08d82Slm66018 3957655fd6a9Sachartre /* cancel any pending timeout */ 3958655fd6a9Sachartre mutex_exit(&vdcp->lock); 3959655fd6a9Sachartre if (tmid != 0) { 3960655fd6a9Sachartre (void) untimeout(tmid); 3961655fd6a9Sachartre tmid = 0; 3962655fd6a9Sachartre } 3963655fd6a9Sachartre mutex_enter(&vdcp->lock); 3964655fd6a9Sachartre 39653c96341aSnarayan /* 39663c96341aSnarayan * Signal anyone waiting for connection 39673c96341aSnarayan * to come online 39683c96341aSnarayan */ 39693c96341aSnarayan cv_broadcast(&vdcp->running_cv); 39703c96341aSnarayan 39713af08d82Slm66018 while (vdcp->sync_op_pending) { 39723af08d82Slm66018 cv_signal(&vdcp->sync_pending_cv); 39733af08d82Slm66018 cv_signal(&vdcp->sync_blocked_cv); 39743af08d82Slm66018 mutex_exit(&vdcp->lock); 3975205eeb1aSlm66018 /* give the waiters enough time to wake up */ 3976205eeb1aSlm66018 delay(vdc_hz_min_ldc_delay); 39773af08d82Slm66018 mutex_enter(&vdcp->lock); 39780a55fbb7Slm66018 } 39791ae08745Sheppo 39803af08d82Slm66018 mutex_exit(&vdcp->lock); 39813af08d82Slm66018 39823af08d82Slm66018 DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n", 39833af08d82Slm66018 vdcp->instance); 39843af08d82Slm66018 thread_exit(); 39853af08d82Slm66018 break; 39863af08d82Slm66018 } 39873af08d82Slm66018 } 39880a55fbb7Slm66018 } 39890a55fbb7Slm66018 39900a55fbb7Slm66018 39910a55fbb7Slm66018 /* 39920a55fbb7Slm66018 * Function: 39930a55fbb7Slm66018 * vdc_process_data_msg() 39940a55fbb7Slm66018 * 39950a55fbb7Slm66018 * Description: 39960a55fbb7Slm66018 * This function is called by the message processing thread each time 39970a55fbb7Slm66018 * a message with a msgtype of VIO_TYPE_DATA is received. It will either 39980a55fbb7Slm66018 * be an ACK or NACK from vds[1] which vdc handles as follows. 39990a55fbb7Slm66018 * ACK - wake up the waiting thread 40000a55fbb7Slm66018 * NACK - resend any messages necessary 40010a55fbb7Slm66018 * 40020a55fbb7Slm66018 * [1] Although the message format allows it, vds should not send a 40030a55fbb7Slm66018 * VIO_SUBTYPE_INFO message to vdc asking it to read data; if for 40040a55fbb7Slm66018 * some bizarre reason it does, vdc will reset the connection. 40050a55fbb7Slm66018 * 40060a55fbb7Slm66018 * Arguments: 40070a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 40080a55fbb7Slm66018 * msg - the LDC message sent by vds 40090a55fbb7Slm66018 * 40100a55fbb7Slm66018 * Return Code: 40110a55fbb7Slm66018 * 0 - Success. 40120a55fbb7Slm66018 * > 0 - error value returned by LDC 40130a55fbb7Slm66018 */ 40140a55fbb7Slm66018 static int 40153af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg) 40160a55fbb7Slm66018 { 40170a55fbb7Slm66018 int status = 0; 40183af08d82Slm66018 vio_dring_msg_t *dring_msg; 4019d10e4ef2Snarayan vdc_local_desc_t *ldep = NULL; 40203af08d82Slm66018 int start, end; 40213af08d82Slm66018 int idx; 40220a55fbb7Slm66018 40233af08d82Slm66018 dring_msg = (vio_dring_msg_t *)msg; 40240a55fbb7Slm66018 40253af08d82Slm66018 ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA); 40263af08d82Slm66018 ASSERT(vdcp != NULL); 40273af08d82Slm66018 40283af08d82Slm66018 mutex_enter(&vdcp->lock); 40290a55fbb7Slm66018 40300a55fbb7Slm66018 /* 40310a55fbb7Slm66018 * Check to see if the message has bogus data 40320a55fbb7Slm66018 */ 4033e1ebb9ecSlm66018 idx = start = dring_msg->start_idx; 40340a55fbb7Slm66018 end = dring_msg->end_idx; 40353af08d82Slm66018 if ((start >= vdcp->dring_len) || 40363af08d82Slm66018 (end >= vdcp->dring_len) || (end < -1)) { 40373af08d82Slm66018 DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n", 40383af08d82Slm66018 vdcp->instance, start, end); 40393af08d82Slm66018 mutex_exit(&vdcp->lock); 4040e1ebb9ecSlm66018 return (EINVAL); 40410a55fbb7Slm66018 } 40420a55fbb7Slm66018 40430a55fbb7Slm66018 /* 40440a55fbb7Slm66018 * Verify that the sequence number is what vdc expects. 40450a55fbb7Slm66018 */ 40463af08d82Slm66018 switch (vdc_verify_seq_num(vdcp, dring_msg)) { 4047e1ebb9ecSlm66018 case VDC_SEQ_NUM_TODO: 4048e1ebb9ecSlm66018 break; /* keep processing this message */ 4049e1ebb9ecSlm66018 case VDC_SEQ_NUM_SKIP: 40503af08d82Slm66018 mutex_exit(&vdcp->lock); 4051e1ebb9ecSlm66018 return (0); 4052e1ebb9ecSlm66018 case VDC_SEQ_NUM_INVALID: 40533af08d82Slm66018 mutex_exit(&vdcp->lock); 40543af08d82Slm66018 DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance); 40550a55fbb7Slm66018 return (ENXIO); 40560a55fbb7Slm66018 } 40570a55fbb7Slm66018 40583af08d82Slm66018 if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) { 40593af08d82Slm66018 DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance); 4060e1ebb9ecSlm66018 VDC_DUMP_DRING_MSG(dring_msg); 40613af08d82Slm66018 mutex_exit(&vdcp->lock); 4062e1ebb9ecSlm66018 return (EIO); 40630a55fbb7Slm66018 40643af08d82Slm66018 } else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) { 40653af08d82Slm66018 mutex_exit(&vdcp->lock); 4066e1ebb9ecSlm66018 return (EPROTO); 4067e1ebb9ecSlm66018 } 4068e1ebb9ecSlm66018 40693af08d82Slm66018 DTRACE_IO2(recv, vio_dring_msg_t, dring_msg, vdc_t *, vdcp); 40703af08d82Slm66018 DMSG(vdcp, 1, ": start %d end %d\n", start, end); 40713af08d82Slm66018 ASSERT(start == end); 40723af08d82Slm66018 40733af08d82Slm66018 ldep = &vdcp->local_dring[idx]; 40743af08d82Slm66018 40753af08d82Slm66018 DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n", 40763af08d82Slm66018 ldep->dep->hdr.dstate, ldep->cb_type); 40773af08d82Slm66018 4078e1ebb9ecSlm66018 if (ldep->dep->hdr.dstate == VIO_DESC_DONE) { 40793af08d82Slm66018 struct buf *bufp; 4080e1ebb9ecSlm66018 40813af08d82Slm66018 switch (ldep->cb_type) { 40823af08d82Slm66018 case CB_SYNC: 40833af08d82Slm66018 ASSERT(vdcp->sync_op_pending); 4084d10e4ef2Snarayan 40853af08d82Slm66018 status = vdc_depopulate_descriptor(vdcp, idx); 40863af08d82Slm66018 vdcp->sync_op_status = status; 40873af08d82Slm66018 vdcp->sync_op_pending = B_FALSE; 40883af08d82Slm66018 cv_signal(&vdcp->sync_pending_cv); 40893af08d82Slm66018 break; 40904bac2208Snarayan 40913af08d82Slm66018 case CB_STRATEGY: 40923af08d82Slm66018 bufp = ldep->cb_arg; 40933af08d82Slm66018 ASSERT(bufp != NULL); 40943c96341aSnarayan bufp->b_resid = 40953c96341aSnarayan bufp->b_bcount - ldep->dep->payload.nbytes; 40963af08d82Slm66018 status = ldep->dep->payload.status; /* Future:ntoh */ 40973af08d82Slm66018 if (status != 0) { 40983af08d82Slm66018 DMSG(vdcp, 1, "strategy status=%d\n", status); 40993af08d82Slm66018 bioerror(bufp, status); 4100d10e4ef2Snarayan } 41013af08d82Slm66018 status = vdc_depopulate_descriptor(vdcp, idx); 41023af08d82Slm66018 biodone(bufp); 41033c96341aSnarayan 41043c96341aSnarayan DMSG(vdcp, 1, 41053c96341aSnarayan "strategy complete req=%ld bytes resp=%ld bytes\n", 41063c96341aSnarayan bufp->b_bcount, ldep->dep->payload.nbytes); 41073af08d82Slm66018 break; 41083af08d82Slm66018 41093af08d82Slm66018 default: 41103af08d82Slm66018 ASSERT(0); 41110a55fbb7Slm66018 } 41123af08d82Slm66018 } 41133af08d82Slm66018 41143af08d82Slm66018 /* let the arrival signal propogate */ 41153af08d82Slm66018 mutex_exit(&vdcp->lock); 41160a55fbb7Slm66018 4117e1ebb9ecSlm66018 /* probe gives the count of how many entries were processed */ 41183af08d82Slm66018 DTRACE_IO2(processed, int, 1, vdc_t *, vdcp); 41190a55fbb7Slm66018 41203af08d82Slm66018 return (0); 41210a55fbb7Slm66018 } 41220a55fbb7Slm66018 41230a55fbb7Slm66018 41240a55fbb7Slm66018 /* 41250a55fbb7Slm66018 * Function: 41260a55fbb7Slm66018 * vdc_handle_ver_msg() 41270a55fbb7Slm66018 * 41280a55fbb7Slm66018 * Description: 41290a55fbb7Slm66018 * 41300a55fbb7Slm66018 * Arguments: 41310a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 41320a55fbb7Slm66018 * ver_msg - LDC message sent by vDisk server 41330a55fbb7Slm66018 * 41340a55fbb7Slm66018 * Return Code: 41350a55fbb7Slm66018 * 0 - Success 41360a55fbb7Slm66018 */ 41370a55fbb7Slm66018 static int 41380a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg) 41390a55fbb7Slm66018 { 41400a55fbb7Slm66018 int status = 0; 41410a55fbb7Slm66018 41420a55fbb7Slm66018 ASSERT(vdc != NULL); 41430a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 41440a55fbb7Slm66018 41450a55fbb7Slm66018 if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) { 41460a55fbb7Slm66018 return (EPROTO); 41470a55fbb7Slm66018 } 41480a55fbb7Slm66018 41490a55fbb7Slm66018 if (ver_msg->dev_class != VDEV_DISK_SERVER) { 41500a55fbb7Slm66018 return (EINVAL); 41510a55fbb7Slm66018 } 41520a55fbb7Slm66018 41530a55fbb7Slm66018 switch (ver_msg->tag.vio_subtype) { 41540a55fbb7Slm66018 case VIO_SUBTYPE_ACK: 41550a55fbb7Slm66018 /* 41560a55fbb7Slm66018 * We check to see if the version returned is indeed supported 41570a55fbb7Slm66018 * (The server may have also adjusted the minor number downwards 41580a55fbb7Slm66018 * and if so 'ver_msg' will contain the actual version agreed) 41590a55fbb7Slm66018 */ 41600a55fbb7Slm66018 if (vdc_is_supported_version(ver_msg)) { 41610a55fbb7Slm66018 vdc->ver.major = ver_msg->ver_major; 41620a55fbb7Slm66018 vdc->ver.minor = ver_msg->ver_minor; 41630a55fbb7Slm66018 ASSERT(vdc->ver.major > 0); 41640a55fbb7Slm66018 } else { 41650a55fbb7Slm66018 status = EPROTO; 41660a55fbb7Slm66018 } 41670a55fbb7Slm66018 break; 41680a55fbb7Slm66018 41690a55fbb7Slm66018 case VIO_SUBTYPE_NACK: 41700a55fbb7Slm66018 /* 41710a55fbb7Slm66018 * call vdc_is_supported_version() which will return the next 41720a55fbb7Slm66018 * supported version (if any) in 'ver_msg' 41730a55fbb7Slm66018 */ 41740a55fbb7Slm66018 (void) vdc_is_supported_version(ver_msg); 41750a55fbb7Slm66018 if (ver_msg->ver_major > 0) { 41760a55fbb7Slm66018 size_t len = sizeof (*ver_msg); 41770a55fbb7Slm66018 41780a55fbb7Slm66018 ASSERT(vdc->ver.major > 0); 41790a55fbb7Slm66018 41800a55fbb7Slm66018 /* reset the necessary fields and resend */ 41810a55fbb7Slm66018 ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO; 41820a55fbb7Slm66018 ver_msg->dev_class = VDEV_DISK; 41830a55fbb7Slm66018 41840a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)ver_msg, &len); 41853af08d82Slm66018 DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n", 41860a55fbb7Slm66018 vdc->instance, status); 41870a55fbb7Slm66018 if (len != sizeof (*ver_msg)) 41880a55fbb7Slm66018 status = EBADMSG; 41890a55fbb7Slm66018 } else { 419087a7269eSachartre DMSG(vdc, 0, "[%d] No common version with vDisk server", 419187a7269eSachartre vdc->instance); 41920a55fbb7Slm66018 status = ENOTSUP; 41930a55fbb7Slm66018 } 41940a55fbb7Slm66018 41950a55fbb7Slm66018 break; 41961ae08745Sheppo case VIO_SUBTYPE_INFO: 41971ae08745Sheppo /* 41981ae08745Sheppo * Handle the case where vds starts handshake 4199eff7243fSlm66018 * (for now only vdc is the instigator) 42001ae08745Sheppo */ 42011ae08745Sheppo status = ENOTSUP; 42021ae08745Sheppo break; 42031ae08745Sheppo 42041ae08745Sheppo default: 42050a55fbb7Slm66018 status = EINVAL; 42061ae08745Sheppo break; 42071ae08745Sheppo } 42081ae08745Sheppo 42090a55fbb7Slm66018 return (status); 42100a55fbb7Slm66018 } 42110a55fbb7Slm66018 42120a55fbb7Slm66018 /* 42130a55fbb7Slm66018 * Function: 42140a55fbb7Slm66018 * vdc_handle_attr_msg() 42150a55fbb7Slm66018 * 42160a55fbb7Slm66018 * Description: 42170a55fbb7Slm66018 * 42180a55fbb7Slm66018 * Arguments: 42190a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 42200a55fbb7Slm66018 * attr_msg - LDC message sent by vDisk server 42210a55fbb7Slm66018 * 42220a55fbb7Slm66018 * Return Code: 42230a55fbb7Slm66018 * 0 - Success 42240a55fbb7Slm66018 */ 42250a55fbb7Slm66018 static int 42260a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg) 42270a55fbb7Slm66018 { 42280a55fbb7Slm66018 int status = 0; 42290a55fbb7Slm66018 42300a55fbb7Slm66018 ASSERT(vdc != NULL); 42310a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 42320a55fbb7Slm66018 42330a55fbb7Slm66018 if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) { 42340a55fbb7Slm66018 return (EPROTO); 42350a55fbb7Slm66018 } 42360a55fbb7Slm66018 42370a55fbb7Slm66018 switch (attr_msg->tag.vio_subtype) { 42381ae08745Sheppo case VIO_SUBTYPE_ACK: 42391ae08745Sheppo /* 42401ae08745Sheppo * We now verify the attributes sent by vds. 42411ae08745Sheppo */ 424278fcd0a1Sachartre if (attr_msg->vdisk_size == 0) { 424378fcd0a1Sachartre DMSG(vdc, 0, "[%d] Invalid disk size from vds", 424478fcd0a1Sachartre vdc->instance); 424578fcd0a1Sachartre status = EINVAL; 424678fcd0a1Sachartre break; 424778fcd0a1Sachartre } 424878fcd0a1Sachartre 424978fcd0a1Sachartre if (attr_msg->max_xfer_sz == 0) { 425078fcd0a1Sachartre DMSG(vdc, 0, "[%d] Invalid transfer size from vds", 425178fcd0a1Sachartre vdc->instance); 425278fcd0a1Sachartre status = EINVAL; 425378fcd0a1Sachartre break; 425478fcd0a1Sachartre } 425578fcd0a1Sachartre 425678fcd0a1Sachartre /* 425778fcd0a1Sachartre * If the disk size is already set check that it hasn't changed. 425878fcd0a1Sachartre */ 425978fcd0a1Sachartre if ((vdc->vdisk_size != 0) && 426078fcd0a1Sachartre (vdc->vdisk_size != attr_msg->vdisk_size)) { 426178fcd0a1Sachartre DMSG(vdc, 0, "[%d] Different disk size from vds " 426278fcd0a1Sachartre "(old=0x%lx - new=0x%lx", vdc->instance, 426378fcd0a1Sachartre vdc->vdisk_size, attr_msg->vdisk_size) 426478fcd0a1Sachartre status = EINVAL; 426578fcd0a1Sachartre break; 426678fcd0a1Sachartre } 426778fcd0a1Sachartre 42681ae08745Sheppo vdc->vdisk_size = attr_msg->vdisk_size; 42691ae08745Sheppo vdc->vdisk_type = attr_msg->vdisk_type; 4270*17cadca8Slm66018 vdc->operations = attr_msg->operations; 4271*17cadca8Slm66018 if (vio_ver_is_supported(vdc->ver, 1, 1)) 4272*17cadca8Slm66018 vdc->vdisk_media = attr_msg->vdisk_media; 4273*17cadca8Slm66018 else 4274*17cadca8Slm66018 vdc->vdisk_media = 0; 42751ae08745Sheppo 42763af08d82Slm66018 DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n", 4277e1ebb9ecSlm66018 vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz); 42783af08d82Slm66018 DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n", 4279e1ebb9ecSlm66018 vdc->instance, vdc->block_size, 4280e1ebb9ecSlm66018 attr_msg->vdisk_block_size); 4281e1ebb9ecSlm66018 42821ae08745Sheppo /* 4283e1ebb9ecSlm66018 * We don't know at compile time what the vDisk server will 4284*17cadca8Slm66018 * think are good values but we apply a large (arbitrary) 4285e1ebb9ecSlm66018 * upper bound to prevent memory exhaustion in vdc if it was 4286e1ebb9ecSlm66018 * allocating a DRing based of huge values sent by the server. 4287e1ebb9ecSlm66018 * We probably will never exceed this except if the message 4288e1ebb9ecSlm66018 * was garbage. 42891ae08745Sheppo */ 4290e1ebb9ecSlm66018 if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <= 4291e1ebb9ecSlm66018 (PAGESIZE * DEV_BSIZE)) { 4292e1ebb9ecSlm66018 vdc->max_xfer_sz = attr_msg->max_xfer_sz; 4293e1ebb9ecSlm66018 vdc->block_size = attr_msg->vdisk_block_size; 4294e1ebb9ecSlm66018 } else { 42953af08d82Slm66018 DMSG(vdc, 0, "[%d] vds block transfer size too big;" 4296e1ebb9ecSlm66018 " using max supported by vdc", vdc->instance); 42971ae08745Sheppo } 42981ae08745Sheppo 42991ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DRING_MODE) || 43001ae08745Sheppo (attr_msg->vdisk_size > INT64_MAX) || 4301*17cadca8Slm66018 (attr_msg->operations == 0) || 43021ae08745Sheppo (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) { 43033af08d82Slm66018 DMSG(vdc, 0, "[%d] Invalid attributes from vds", 4304e1ebb9ecSlm66018 vdc->instance); 43051ae08745Sheppo status = EINVAL; 43061ae08745Sheppo break; 43071ae08745Sheppo } 43081ae08745Sheppo 430978fcd0a1Sachartre /* 431078fcd0a1Sachartre * Now that we have received all attributes we can create a 431178fcd0a1Sachartre * fake geometry for the disk. 431278fcd0a1Sachartre */ 431378fcd0a1Sachartre vdc_create_fake_geometry(vdc); 43141ae08745Sheppo break; 43151ae08745Sheppo 43161ae08745Sheppo case VIO_SUBTYPE_NACK: 43171ae08745Sheppo /* 43181ae08745Sheppo * vds could not handle the attributes we sent so we 43191ae08745Sheppo * stop negotiating. 43201ae08745Sheppo */ 43211ae08745Sheppo status = EPROTO; 43221ae08745Sheppo break; 43231ae08745Sheppo 43241ae08745Sheppo case VIO_SUBTYPE_INFO: 43251ae08745Sheppo /* 43261ae08745Sheppo * Handle the case where vds starts the handshake 43271ae08745Sheppo * (for now; vdc is the only supported instigatior) 43281ae08745Sheppo */ 43291ae08745Sheppo status = ENOTSUP; 43301ae08745Sheppo break; 43311ae08745Sheppo 43321ae08745Sheppo default: 43331ae08745Sheppo status = ENOTSUP; 43341ae08745Sheppo break; 43351ae08745Sheppo } 43361ae08745Sheppo 43370a55fbb7Slm66018 return (status); 43381ae08745Sheppo } 43391ae08745Sheppo 43400a55fbb7Slm66018 /* 43410a55fbb7Slm66018 * Function: 43420a55fbb7Slm66018 * vdc_handle_dring_reg_msg() 43430a55fbb7Slm66018 * 43440a55fbb7Slm66018 * Description: 43450a55fbb7Slm66018 * 43460a55fbb7Slm66018 * Arguments: 43470a55fbb7Slm66018 * vdc - soft state pointer for this instance of the driver. 43480a55fbb7Slm66018 * dring_msg - LDC message sent by vDisk server 43490a55fbb7Slm66018 * 43500a55fbb7Slm66018 * Return Code: 43510a55fbb7Slm66018 * 0 - Success 43520a55fbb7Slm66018 */ 43530a55fbb7Slm66018 static int 43540a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg) 43550a55fbb7Slm66018 { 43560a55fbb7Slm66018 int status = 0; 43571ae08745Sheppo 43580a55fbb7Slm66018 ASSERT(vdc != NULL); 43590a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 43600a55fbb7Slm66018 43610a55fbb7Slm66018 if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) { 43620a55fbb7Slm66018 return (EPROTO); 43630a55fbb7Slm66018 } 43640a55fbb7Slm66018 43650a55fbb7Slm66018 switch (dring_msg->tag.vio_subtype) { 43660a55fbb7Slm66018 case VIO_SUBTYPE_ACK: 43671ae08745Sheppo /* save the received dring_ident */ 43681ae08745Sheppo vdc->dring_ident = dring_msg->dring_ident; 43693af08d82Slm66018 DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n", 4370e1ebb9ecSlm66018 vdc->instance, vdc->dring_ident); 43711ae08745Sheppo break; 43721ae08745Sheppo 43731ae08745Sheppo case VIO_SUBTYPE_NACK: 43741ae08745Sheppo /* 43751ae08745Sheppo * vds could not handle the DRing info we sent so we 43761ae08745Sheppo * stop negotiating. 43771ae08745Sheppo */ 43783af08d82Slm66018 DMSG(vdc, 0, "[%d] server could not register DRing\n", 43793af08d82Slm66018 vdc->instance); 43801ae08745Sheppo status = EPROTO; 43811ae08745Sheppo break; 43821ae08745Sheppo 43831ae08745Sheppo case VIO_SUBTYPE_INFO: 43841ae08745Sheppo /* 43851ae08745Sheppo * Handle the case where vds starts handshake 43861ae08745Sheppo * (for now only vdc is the instigatior) 43871ae08745Sheppo */ 43881ae08745Sheppo status = ENOTSUP; 43891ae08745Sheppo break; 43901ae08745Sheppo default: 43911ae08745Sheppo status = ENOTSUP; 43921ae08745Sheppo } 43931ae08745Sheppo 43941ae08745Sheppo return (status); 43951ae08745Sheppo } 43961ae08745Sheppo 43971ae08745Sheppo /* 43981ae08745Sheppo * Function: 43991ae08745Sheppo * vdc_verify_seq_num() 44001ae08745Sheppo * 44011ae08745Sheppo * Description: 4402e1ebb9ecSlm66018 * This functions verifies that the sequence number sent back by the vDisk 4403e1ebb9ecSlm66018 * server with the latest message is what is expected (i.e. it is greater 4404e1ebb9ecSlm66018 * than the last seq num sent by the vDisk server and less than or equal 4405e1ebb9ecSlm66018 * to the last seq num generated by vdc). 4406e1ebb9ecSlm66018 * 4407e1ebb9ecSlm66018 * It then checks the request ID to see if any requests need processing 4408e1ebb9ecSlm66018 * in the DRing. 44091ae08745Sheppo * 44101ae08745Sheppo * Arguments: 44111ae08745Sheppo * vdc - soft state pointer for this instance of the driver. 44121ae08745Sheppo * dring_msg - pointer to the LDC message sent by vds 44131ae08745Sheppo * 44141ae08745Sheppo * Return Code: 4415e1ebb9ecSlm66018 * VDC_SEQ_NUM_TODO - Message needs to be processed 4416e1ebb9ecSlm66018 * VDC_SEQ_NUM_SKIP - Message has already been processed 4417e1ebb9ecSlm66018 * VDC_SEQ_NUM_INVALID - The seq numbers are so out of sync, 4418e1ebb9ecSlm66018 * vdc cannot deal with them 44191ae08745Sheppo */ 4420e1ebb9ecSlm66018 static int 4421e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg) 44221ae08745Sheppo { 44231ae08745Sheppo ASSERT(vdc != NULL); 44241ae08745Sheppo ASSERT(dring_msg != NULL); 4425d10e4ef2Snarayan ASSERT(mutex_owned(&vdc->lock)); 44261ae08745Sheppo 44271ae08745Sheppo /* 44281ae08745Sheppo * Check to see if the messages were responded to in the correct 4429e1ebb9ecSlm66018 * order by vds. 44301ae08745Sheppo */ 4431e1ebb9ecSlm66018 if ((dring_msg->seq_num <= vdc->seq_num_reply) || 4432e1ebb9ecSlm66018 (dring_msg->seq_num > vdc->seq_num)) { 44333af08d82Slm66018 DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: " 4434e1ebb9ecSlm66018 "%lu > expected <= %lu (last proc req %lu sent %lu)\n", 4435e1ebb9ecSlm66018 vdc->instance, dring_msg->seq_num, 4436e1ebb9ecSlm66018 vdc->seq_num_reply, vdc->seq_num, 4437e1ebb9ecSlm66018 vdc->req_id_proc, vdc->req_id); 4438e1ebb9ecSlm66018 return (VDC_SEQ_NUM_INVALID); 44391ae08745Sheppo } 4440e1ebb9ecSlm66018 vdc->seq_num_reply = dring_msg->seq_num; 44411ae08745Sheppo 4442e1ebb9ecSlm66018 if (vdc->req_id_proc < vdc->req_id) 4443e1ebb9ecSlm66018 return (VDC_SEQ_NUM_TODO); 4444e1ebb9ecSlm66018 else 4445e1ebb9ecSlm66018 return (VDC_SEQ_NUM_SKIP); 44461ae08745Sheppo } 44471ae08745Sheppo 44480a55fbb7Slm66018 44490a55fbb7Slm66018 /* 44500a55fbb7Slm66018 * Function: 44510a55fbb7Slm66018 * vdc_is_supported_version() 44520a55fbb7Slm66018 * 44530a55fbb7Slm66018 * Description: 44540a55fbb7Slm66018 * This routine checks if the major/minor version numbers specified in 44550a55fbb7Slm66018 * 'ver_msg' are supported. If not it finds the next version that is 44560a55fbb7Slm66018 * in the supported version list 'vdc_version[]' and sets the fields in 44570a55fbb7Slm66018 * 'ver_msg' to those values 44580a55fbb7Slm66018 * 44590a55fbb7Slm66018 * Arguments: 44600a55fbb7Slm66018 * ver_msg - LDC message sent by vDisk server 44610a55fbb7Slm66018 * 44620a55fbb7Slm66018 * Return Code: 44630a55fbb7Slm66018 * B_TRUE - Success 44640a55fbb7Slm66018 * B_FALSE - Version not supported 44650a55fbb7Slm66018 */ 44660a55fbb7Slm66018 static boolean_t 44670a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg) 44680a55fbb7Slm66018 { 44690a55fbb7Slm66018 int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]); 44700a55fbb7Slm66018 44710a55fbb7Slm66018 for (int i = 0; i < vdc_num_versions; i++) { 44720a55fbb7Slm66018 ASSERT(vdc_version[i].major > 0); 44730a55fbb7Slm66018 ASSERT((i == 0) || 44740a55fbb7Slm66018 (vdc_version[i].major < vdc_version[i-1].major)); 44750a55fbb7Slm66018 44760a55fbb7Slm66018 /* 44770a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 44780a55fbb7Slm66018 * necessary, down to the highest value supported by this 44790a55fbb7Slm66018 * client. The server should support all minor versions lower 44800a55fbb7Slm66018 * than the value it sent 44810a55fbb7Slm66018 */ 44820a55fbb7Slm66018 if (ver_msg->ver_major == vdc_version[i].major) { 44830a55fbb7Slm66018 if (ver_msg->ver_minor > vdc_version[i].minor) { 44843af08d82Slm66018 DMSGX(0, 44853af08d82Slm66018 "Adjusting minor version from %u to %u", 44860a55fbb7Slm66018 ver_msg->ver_minor, vdc_version[i].minor); 44870a55fbb7Slm66018 ver_msg->ver_minor = vdc_version[i].minor; 44880a55fbb7Slm66018 } 44890a55fbb7Slm66018 return (B_TRUE); 44900a55fbb7Slm66018 } 44910a55fbb7Slm66018 44920a55fbb7Slm66018 /* 44930a55fbb7Slm66018 * If the message contains a higher major version number, set 44940a55fbb7Slm66018 * the message's major/minor versions to the current values 44950a55fbb7Slm66018 * and return false, so this message will get resent with 44960a55fbb7Slm66018 * these values, and the server will potentially try again 44970a55fbb7Slm66018 * with the same or a lower version 44980a55fbb7Slm66018 */ 44990a55fbb7Slm66018 if (ver_msg->ver_major > vdc_version[i].major) { 45000a55fbb7Slm66018 ver_msg->ver_major = vdc_version[i].major; 45010a55fbb7Slm66018 ver_msg->ver_minor = vdc_version[i].minor; 45023af08d82Slm66018 DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n", 45030a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 45040a55fbb7Slm66018 45050a55fbb7Slm66018 return (B_FALSE); 45060a55fbb7Slm66018 } 45070a55fbb7Slm66018 45080a55fbb7Slm66018 /* 45090a55fbb7Slm66018 * Otherwise, the message's major version is less than the 45100a55fbb7Slm66018 * current major version, so continue the loop to the next 45110a55fbb7Slm66018 * (lower) supported version 45120a55fbb7Slm66018 */ 45130a55fbb7Slm66018 } 45140a55fbb7Slm66018 45150a55fbb7Slm66018 /* 45160a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 45170a55fbb7Slm66018 * message to terminate negotiation 45180a55fbb7Slm66018 */ 45190a55fbb7Slm66018 ver_msg->ver_major = 0; 45200a55fbb7Slm66018 ver_msg->ver_minor = 0; 45210a55fbb7Slm66018 45220a55fbb7Slm66018 return (B_FALSE); 45230a55fbb7Slm66018 } 45241ae08745Sheppo /* -------------------------------------------------------------------------- */ 45251ae08745Sheppo 45261ae08745Sheppo /* 45271ae08745Sheppo * DKIO(7) support 45281ae08745Sheppo */ 45291ae08745Sheppo 45301ae08745Sheppo typedef struct vdc_dk_arg { 45311ae08745Sheppo struct dk_callback dkc; 45321ae08745Sheppo int mode; 45331ae08745Sheppo dev_t dev; 45341ae08745Sheppo vdc_t *vdc; 45351ae08745Sheppo } vdc_dk_arg_t; 45361ae08745Sheppo 45371ae08745Sheppo /* 45381ae08745Sheppo * Function: 45391ae08745Sheppo * vdc_dkio_flush_cb() 45401ae08745Sheppo * 45411ae08745Sheppo * Description: 45421ae08745Sheppo * This routine is a callback for DKIOCFLUSHWRITECACHE which can be called 45431ae08745Sheppo * by kernel code. 45441ae08745Sheppo * 45451ae08745Sheppo * Arguments: 45461ae08745Sheppo * arg - a pointer to a vdc_dk_arg_t structure. 45471ae08745Sheppo */ 45481ae08745Sheppo void 45491ae08745Sheppo vdc_dkio_flush_cb(void *arg) 45501ae08745Sheppo { 45511ae08745Sheppo struct vdc_dk_arg *dk_arg = (struct vdc_dk_arg *)arg; 45521ae08745Sheppo struct dk_callback *dkc = NULL; 45531ae08745Sheppo vdc_t *vdc = NULL; 45541ae08745Sheppo int rv; 45551ae08745Sheppo 45561ae08745Sheppo if (dk_arg == NULL) { 45573af08d82Slm66018 cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n"); 45581ae08745Sheppo return; 45591ae08745Sheppo } 45601ae08745Sheppo dkc = &dk_arg->dkc; 45611ae08745Sheppo vdc = dk_arg->vdc; 45621ae08745Sheppo ASSERT(vdc != NULL); 45631ae08745Sheppo 45643af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0, 45650d0c8d4bSnarayan VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir); 45661ae08745Sheppo if (rv != 0) { 45673af08d82Slm66018 DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n", 4568e1ebb9ecSlm66018 vdc->instance, rv, 45691ae08745Sheppo ddi_model_convert_from(dk_arg->mode & FMODELS)); 45701ae08745Sheppo } 45711ae08745Sheppo 45721ae08745Sheppo /* 45731ae08745Sheppo * Trigger the call back to notify the caller the the ioctl call has 45741ae08745Sheppo * been completed. 45751ae08745Sheppo */ 45761ae08745Sheppo if ((dk_arg->mode & FKIOCTL) && 45771ae08745Sheppo (dkc != NULL) && 45781ae08745Sheppo (dkc->dkc_callback != NULL)) { 45791ae08745Sheppo ASSERT(dkc->dkc_cookie != NULL); 45808e6a2a04Slm66018 (*dkc->dkc_callback)(dkc->dkc_cookie, rv); 45811ae08745Sheppo } 45821ae08745Sheppo 45831ae08745Sheppo /* Indicate that one less DKIO write flush is outstanding */ 45841ae08745Sheppo mutex_enter(&vdc->lock); 45851ae08745Sheppo vdc->dkio_flush_pending--; 45861ae08745Sheppo ASSERT(vdc->dkio_flush_pending >= 0); 45871ae08745Sheppo mutex_exit(&vdc->lock); 45888e6a2a04Slm66018 45898e6a2a04Slm66018 /* free the mem that was allocated when the callback was dispatched */ 45908e6a2a04Slm66018 kmem_free(arg, sizeof (vdc_dk_arg_t)); 45911ae08745Sheppo } 45921ae08745Sheppo 45931ae08745Sheppo /* 459487a7269eSachartre * Function: 459587a7269eSachartre * vdc_dkio_get_partition() 459687a7269eSachartre * 459787a7269eSachartre * Description: 459887a7269eSachartre * This function implements the DKIOCGAPART ioctl. 459987a7269eSachartre * 460087a7269eSachartre * Arguments: 460178fcd0a1Sachartre * vdc - soft state pointer 460287a7269eSachartre * arg - a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure 460387a7269eSachartre * flag - ioctl flags 460487a7269eSachartre */ 460587a7269eSachartre static int 460678fcd0a1Sachartre vdc_dkio_get_partition(vdc_t *vdc, caddr_t arg, int flag) 460787a7269eSachartre { 460878fcd0a1Sachartre struct dk_geom *geom; 460978fcd0a1Sachartre struct vtoc *vtoc; 461087a7269eSachartre union { 461187a7269eSachartre struct dk_map map[NDKMAP]; 461287a7269eSachartre struct dk_map32 map32[NDKMAP]; 461387a7269eSachartre } data; 461487a7269eSachartre int i, rv, size; 461587a7269eSachartre 461678fcd0a1Sachartre mutex_enter(&vdc->lock); 461787a7269eSachartre 461878fcd0a1Sachartre if ((rv = vdc_validate_geometry(vdc)) != 0) { 461978fcd0a1Sachartre mutex_exit(&vdc->lock); 462087a7269eSachartre return (rv); 462178fcd0a1Sachartre } 462287a7269eSachartre 462378fcd0a1Sachartre vtoc = vdc->vtoc; 462478fcd0a1Sachartre geom = vdc->geom; 462587a7269eSachartre 462687a7269eSachartre if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) { 462787a7269eSachartre 462878fcd0a1Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 462978fcd0a1Sachartre data.map32[i].dkl_cylno = vtoc->v_part[i].p_start / 463078fcd0a1Sachartre (geom->dkg_nhead * geom->dkg_nsect); 463178fcd0a1Sachartre data.map32[i].dkl_nblk = vtoc->v_part[i].p_size; 463287a7269eSachartre } 463387a7269eSachartre size = NDKMAP * sizeof (struct dk_map32); 463487a7269eSachartre 463587a7269eSachartre } else { 463687a7269eSachartre 463778fcd0a1Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 463878fcd0a1Sachartre data.map[i].dkl_cylno = vtoc->v_part[i].p_start / 463978fcd0a1Sachartre (geom->dkg_nhead * geom->dkg_nsect); 464078fcd0a1Sachartre data.map[i].dkl_nblk = vtoc->v_part[i].p_size; 464187a7269eSachartre } 464287a7269eSachartre size = NDKMAP * sizeof (struct dk_map); 464387a7269eSachartre 464487a7269eSachartre } 464587a7269eSachartre 464678fcd0a1Sachartre mutex_exit(&vdc->lock); 464778fcd0a1Sachartre 464887a7269eSachartre if (ddi_copyout(&data, arg, size, flag) != 0) 464987a7269eSachartre return (EFAULT); 465087a7269eSachartre 465187a7269eSachartre return (0); 465287a7269eSachartre } 465387a7269eSachartre 465487a7269eSachartre /* 465587a7269eSachartre * Function: 465687a7269eSachartre * vdc_dioctl_rwcmd() 465787a7269eSachartre * 465887a7269eSachartre * Description: 465987a7269eSachartre * This function implements the DIOCTL_RWCMD ioctl. This ioctl is used 466087a7269eSachartre * for DKC_DIRECT disks to read or write at an absolute disk offset. 466187a7269eSachartre * 466287a7269eSachartre * Arguments: 466387a7269eSachartre * dev - device 466487a7269eSachartre * arg - a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure 466587a7269eSachartre * flag - ioctl flags 466687a7269eSachartre */ 466787a7269eSachartre static int 466887a7269eSachartre vdc_dioctl_rwcmd(dev_t dev, caddr_t arg, int flag) 466987a7269eSachartre { 467087a7269eSachartre struct dadkio_rwcmd32 rwcmd32; 467187a7269eSachartre struct dadkio_rwcmd rwcmd; 467287a7269eSachartre struct iovec aiov; 467387a7269eSachartre struct uio auio; 467487a7269eSachartre int rw, status; 467587a7269eSachartre struct buf *buf; 467687a7269eSachartre 467787a7269eSachartre if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) { 467887a7269eSachartre if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32, 467987a7269eSachartre sizeof (struct dadkio_rwcmd32), flag)) { 468087a7269eSachartre return (EFAULT); 468187a7269eSachartre } 468287a7269eSachartre rwcmd.cmd = rwcmd32.cmd; 468387a7269eSachartre rwcmd.flags = rwcmd32.flags; 468487a7269eSachartre rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr; 468587a7269eSachartre rwcmd.buflen = rwcmd32.buflen; 468687a7269eSachartre rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr; 468787a7269eSachartre } else { 468887a7269eSachartre if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd, 468987a7269eSachartre sizeof (struct dadkio_rwcmd), flag)) { 469087a7269eSachartre return (EFAULT); 469187a7269eSachartre } 469287a7269eSachartre } 469387a7269eSachartre 469487a7269eSachartre switch (rwcmd.cmd) { 469587a7269eSachartre case DADKIO_RWCMD_READ: 469687a7269eSachartre rw = B_READ; 469787a7269eSachartre break; 469887a7269eSachartre case DADKIO_RWCMD_WRITE: 469987a7269eSachartre rw = B_WRITE; 470087a7269eSachartre break; 470187a7269eSachartre default: 470287a7269eSachartre return (EINVAL); 470387a7269eSachartre } 470487a7269eSachartre 470587a7269eSachartre bzero((caddr_t)&aiov, sizeof (struct iovec)); 470687a7269eSachartre aiov.iov_base = rwcmd.bufaddr; 470787a7269eSachartre aiov.iov_len = rwcmd.buflen; 470887a7269eSachartre 470987a7269eSachartre bzero((caddr_t)&auio, sizeof (struct uio)); 471087a7269eSachartre auio.uio_iov = &aiov; 471187a7269eSachartre auio.uio_iovcnt = 1; 471287a7269eSachartre auio.uio_loffset = rwcmd.blkaddr * DEV_BSIZE; 471387a7269eSachartre auio.uio_resid = rwcmd.buflen; 471487a7269eSachartre auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE; 471587a7269eSachartre 471687a7269eSachartre buf = kmem_alloc(sizeof (buf_t), KM_SLEEP); 471787a7269eSachartre bioinit(buf); 471887a7269eSachartre /* 471987a7269eSachartre * We use the private field of buf to specify that this is an 472087a7269eSachartre * I/O using an absolute offset. 472187a7269eSachartre */ 472287a7269eSachartre buf->b_private = (void *)VD_SLICE_NONE; 472387a7269eSachartre 472487a7269eSachartre status = physio(vdc_strategy, buf, dev, rw, vdc_min, &auio); 472587a7269eSachartre 472687a7269eSachartre biofini(buf); 472787a7269eSachartre kmem_free(buf, sizeof (buf_t)); 472887a7269eSachartre 472987a7269eSachartre return (status); 473087a7269eSachartre } 473187a7269eSachartre 473287a7269eSachartre /* 47331ae08745Sheppo * This structure is used in the DKIO(7I) array below. 47341ae08745Sheppo */ 47351ae08745Sheppo typedef struct vdc_dk_ioctl { 47361ae08745Sheppo uint8_t op; /* VD_OP_XXX value */ 47371ae08745Sheppo int cmd; /* Solaris ioctl operation number */ 47381ae08745Sheppo size_t nbytes; /* size of structure to be copied */ 47390a55fbb7Slm66018 47400a55fbb7Slm66018 /* function to convert between vDisk and Solaris structure formats */ 4741d10e4ef2Snarayan int (*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg, 4742d10e4ef2Snarayan int mode, int dir); 47431ae08745Sheppo } vdc_dk_ioctl_t; 47441ae08745Sheppo 47451ae08745Sheppo /* 47461ae08745Sheppo * Subset of DKIO(7I) operations currently supported 47471ae08745Sheppo */ 47481ae08745Sheppo static vdc_dk_ioctl_t dk_ioctl[] = { 4749eff7243fSlm66018 {VD_OP_FLUSH, DKIOCFLUSHWRITECACHE, 0, 47500a55fbb7Slm66018 vdc_null_copy_func}, 47510a55fbb7Slm66018 {VD_OP_GET_WCE, DKIOCGETWCE, sizeof (int), 47524bac2208Snarayan vdc_get_wce_convert}, 47530a55fbb7Slm66018 {VD_OP_SET_WCE, DKIOCSETWCE, sizeof (int), 47544bac2208Snarayan vdc_set_wce_convert}, 47550a55fbb7Slm66018 {VD_OP_GET_VTOC, DKIOCGVTOC, sizeof (vd_vtoc_t), 47560a55fbb7Slm66018 vdc_get_vtoc_convert}, 47570a55fbb7Slm66018 {VD_OP_SET_VTOC, DKIOCSVTOC, sizeof (vd_vtoc_t), 47580a55fbb7Slm66018 vdc_set_vtoc_convert}, 47590a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCGGEOM, sizeof (vd_geom_t), 47600a55fbb7Slm66018 vdc_get_geom_convert}, 47610a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCG_PHYGEOM, sizeof (vd_geom_t), 47620a55fbb7Slm66018 vdc_get_geom_convert}, 47630a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCG_VIRTGEOM, sizeof (vd_geom_t), 47640a55fbb7Slm66018 vdc_get_geom_convert}, 47650a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, DKIOCSGEOM, sizeof (vd_geom_t), 47660a55fbb7Slm66018 vdc_set_geom_convert}, 47674bac2208Snarayan {VD_OP_GET_EFI, DKIOCGETEFI, 0, 47684bac2208Snarayan vdc_get_efi_convert}, 47694bac2208Snarayan {VD_OP_SET_EFI, DKIOCSETEFI, 0, 47704bac2208Snarayan vdc_set_efi_convert}, 47710a55fbb7Slm66018 477287a7269eSachartre /* DIOCTL_RWCMD is converted to a read or a write */ 477387a7269eSachartre {0, DIOCTL_RWCMD, sizeof (struct dadkio_rwcmd), NULL}, 477487a7269eSachartre 47750a55fbb7Slm66018 /* 47760a55fbb7Slm66018 * These particular ioctls are not sent to the server - vdc fakes up 47770a55fbb7Slm66018 * the necessary info. 47780a55fbb7Slm66018 */ 47790a55fbb7Slm66018 {0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func}, 47800a55fbb7Slm66018 {0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func}, 47810a55fbb7Slm66018 {0, USCSICMD, sizeof (struct uscsi_cmd), vdc_null_copy_func}, 478287a7269eSachartre {0, DKIOCGAPART, 0, vdc_null_copy_func }, 47830a55fbb7Slm66018 {0, DKIOCREMOVABLE, 0, vdc_null_copy_func}, 47840a55fbb7Slm66018 {0, CDROMREADOFFSET, 0, vdc_null_copy_func} 47851ae08745Sheppo }; 47861ae08745Sheppo 47871ae08745Sheppo /* 47881ae08745Sheppo * Function: 47891ae08745Sheppo * vd_process_ioctl() 47901ae08745Sheppo * 47911ae08745Sheppo * Description: 47920a55fbb7Slm66018 * This routine processes disk specific ioctl calls 47931ae08745Sheppo * 47941ae08745Sheppo * Arguments: 47951ae08745Sheppo * dev - the device number 47961ae08745Sheppo * cmd - the operation [dkio(7I)] to be processed 47971ae08745Sheppo * arg - pointer to user provided structure 47981ae08745Sheppo * (contains data to be set or reference parameter for get) 47991ae08745Sheppo * mode - bit flag, indicating open settings, 32/64 bit type, etc 48001ae08745Sheppo * 48011ae08745Sheppo * Return Code: 48021ae08745Sheppo * 0 48031ae08745Sheppo * EFAULT 48041ae08745Sheppo * ENXIO 48051ae08745Sheppo * EIO 48061ae08745Sheppo * ENOTSUP 48071ae08745Sheppo */ 48081ae08745Sheppo static int 48091ae08745Sheppo vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode) 48101ae08745Sheppo { 48110d0c8d4bSnarayan int instance = VDCUNIT(dev); 48121ae08745Sheppo vdc_t *vdc = NULL; 48131ae08745Sheppo int rv = -1; 48141ae08745Sheppo int idx = 0; /* index into dk_ioctl[] */ 48151ae08745Sheppo size_t len = 0; /* #bytes to send to vds */ 48161ae08745Sheppo size_t alloc_len = 0; /* #bytes to allocate mem for */ 48171ae08745Sheppo caddr_t mem_p = NULL; 48181ae08745Sheppo size_t nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0])); 48193af08d82Slm66018 vdc_dk_ioctl_t *iop; 48201ae08745Sheppo 48211ae08745Sheppo vdc = ddi_get_soft_state(vdc_state, instance); 48221ae08745Sheppo if (vdc == NULL) { 48231ae08745Sheppo cmn_err(CE_NOTE, "![%d] Could not get soft state structure", 48241ae08745Sheppo instance); 48251ae08745Sheppo return (ENXIO); 48261ae08745Sheppo } 48271ae08745Sheppo 48283af08d82Slm66018 DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n", 48293af08d82Slm66018 instance, cmd, dev, ddi_model_convert_from(mode & FMODELS)); 48301ae08745Sheppo 48311ae08745Sheppo /* 48321ae08745Sheppo * Validate the ioctl operation to be performed. 48331ae08745Sheppo * 48341ae08745Sheppo * If we have looped through the array without finding a match then we 48351ae08745Sheppo * don't support this ioctl. 48361ae08745Sheppo */ 48371ae08745Sheppo for (idx = 0; idx < nioctls; idx++) { 48381ae08745Sheppo if (cmd == dk_ioctl[idx].cmd) 48391ae08745Sheppo break; 48401ae08745Sheppo } 48411ae08745Sheppo 48421ae08745Sheppo if (idx >= nioctls) { 48433af08d82Slm66018 DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n", 4844e1ebb9ecSlm66018 vdc->instance, cmd); 48451ae08745Sheppo return (ENOTSUP); 48461ae08745Sheppo } 48471ae08745Sheppo 48483af08d82Slm66018 iop = &(dk_ioctl[idx]); 48493af08d82Slm66018 48504bac2208Snarayan if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) { 48514bac2208Snarayan /* size is not fixed for EFI ioctls, it depends on ioctl arg */ 48524bac2208Snarayan dk_efi_t dk_efi; 48534bac2208Snarayan 48544bac2208Snarayan rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode); 48554bac2208Snarayan if (rv != 0) 48564bac2208Snarayan return (EFAULT); 48574bac2208Snarayan 48584bac2208Snarayan len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length; 48594bac2208Snarayan } else { 48603af08d82Slm66018 len = iop->nbytes; 48614bac2208Snarayan } 48621ae08745Sheppo 48631ae08745Sheppo /* 48640a55fbb7Slm66018 * Deal with the ioctls which the server does not provide. vdc can 48650a55fbb7Slm66018 * fake these up and return immediately 48661ae08745Sheppo */ 48671ae08745Sheppo switch (cmd) { 48681ae08745Sheppo case CDROMREADOFFSET: 48691ae08745Sheppo case DKIOCREMOVABLE: 48700a55fbb7Slm66018 case USCSICMD: 48711ae08745Sheppo return (ENOTTY); 48721ae08745Sheppo 487387a7269eSachartre case DIOCTL_RWCMD: 487487a7269eSachartre { 487578fcd0a1Sachartre if (vdc->cinfo == NULL) 487678fcd0a1Sachartre return (ENXIO); 487778fcd0a1Sachartre 487887a7269eSachartre if (vdc->cinfo->dki_ctype != DKC_DIRECT) 487987a7269eSachartre return (ENOTTY); 488087a7269eSachartre 488187a7269eSachartre return (vdc_dioctl_rwcmd(dev, arg, mode)); 488287a7269eSachartre } 488387a7269eSachartre 488487a7269eSachartre case DKIOCGAPART: 488587a7269eSachartre { 488678fcd0a1Sachartre return (vdc_dkio_get_partition(vdc, arg, mode)); 488787a7269eSachartre } 488887a7269eSachartre 48891ae08745Sheppo case DKIOCINFO: 48901ae08745Sheppo { 48911ae08745Sheppo struct dk_cinfo cinfo; 48921ae08745Sheppo if (vdc->cinfo == NULL) 48931ae08745Sheppo return (ENXIO); 48941ae08745Sheppo 48951ae08745Sheppo bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo)); 48960d0c8d4bSnarayan cinfo.dki_partition = VDCPART(dev); 48971ae08745Sheppo 48981ae08745Sheppo rv = ddi_copyout(&cinfo, (void *)arg, 48991ae08745Sheppo sizeof (struct dk_cinfo), mode); 49001ae08745Sheppo if (rv != 0) 49011ae08745Sheppo return (EFAULT); 49021ae08745Sheppo 49031ae08745Sheppo return (0); 49041ae08745Sheppo } 49051ae08745Sheppo 49061ae08745Sheppo case DKIOCGMEDIAINFO: 49078e6a2a04Slm66018 { 49081ae08745Sheppo if (vdc->minfo == NULL) 49091ae08745Sheppo return (ENXIO); 49101ae08745Sheppo 49111ae08745Sheppo rv = ddi_copyout(vdc->minfo, (void *)arg, 49121ae08745Sheppo sizeof (struct dk_minfo), mode); 49131ae08745Sheppo if (rv != 0) 49141ae08745Sheppo return (EFAULT); 49151ae08745Sheppo 49161ae08745Sheppo return (0); 49171ae08745Sheppo } 49181ae08745Sheppo 49198e6a2a04Slm66018 case DKIOCFLUSHWRITECACHE: 49208e6a2a04Slm66018 { 4921*17cadca8Slm66018 struct dk_callback *dkc = 4922*17cadca8Slm66018 (struct dk_callback *)(uintptr_t)arg; 49238e6a2a04Slm66018 vdc_dk_arg_t *dkarg = NULL; 49248e6a2a04Slm66018 49253af08d82Slm66018 DMSG(vdc, 1, "[%d] Flush W$: mode %x\n", 49263af08d82Slm66018 instance, mode); 49278e6a2a04Slm66018 49288e6a2a04Slm66018 /* 49298e6a2a04Slm66018 * If arg is NULL, then there is no callback function 49308e6a2a04Slm66018 * registered and the call operates synchronously; we 49318e6a2a04Slm66018 * break and continue with the rest of the function and 49328e6a2a04Slm66018 * wait for vds to return (i.e. after the request to 49338e6a2a04Slm66018 * vds returns successfully, all writes completed prior 49348e6a2a04Slm66018 * to the ioctl will have been flushed from the disk 49358e6a2a04Slm66018 * write cache to persistent media. 49368e6a2a04Slm66018 * 49378e6a2a04Slm66018 * If a callback function is registered, we dispatch 49388e6a2a04Slm66018 * the request on a task queue and return immediately. 49398e6a2a04Slm66018 * The callback will deal with informing the calling 49408e6a2a04Slm66018 * thread that the flush request is completed. 49418e6a2a04Slm66018 */ 49428e6a2a04Slm66018 if (dkc == NULL) 49438e6a2a04Slm66018 break; 49448e6a2a04Slm66018 4945eff7243fSlm66018 /* 4946eff7243fSlm66018 * the asynchronous callback is only supported if 4947eff7243fSlm66018 * invoked from within the kernel 4948eff7243fSlm66018 */ 4949eff7243fSlm66018 if ((mode & FKIOCTL) == 0) 4950eff7243fSlm66018 return (ENOTSUP); 4951eff7243fSlm66018 49528e6a2a04Slm66018 dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP); 49538e6a2a04Slm66018 49548e6a2a04Slm66018 dkarg->mode = mode; 49558e6a2a04Slm66018 dkarg->dev = dev; 49568e6a2a04Slm66018 bcopy(dkc, &dkarg->dkc, sizeof (*dkc)); 49578e6a2a04Slm66018 49588e6a2a04Slm66018 mutex_enter(&vdc->lock); 49598e6a2a04Slm66018 vdc->dkio_flush_pending++; 49608e6a2a04Slm66018 dkarg->vdc = vdc; 49618e6a2a04Slm66018 mutex_exit(&vdc->lock); 49628e6a2a04Slm66018 49638e6a2a04Slm66018 /* put the request on a task queue */ 49648e6a2a04Slm66018 rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb, 49658e6a2a04Slm66018 (void *)dkarg, DDI_SLEEP); 49663af08d82Slm66018 if (rv == NULL) { 49673af08d82Slm66018 /* clean up if dispatch fails */ 49683af08d82Slm66018 mutex_enter(&vdc->lock); 49693af08d82Slm66018 vdc->dkio_flush_pending--; 497078fcd0a1Sachartre mutex_exit(&vdc->lock); 49713af08d82Slm66018 kmem_free(dkarg, sizeof (vdc_dk_arg_t)); 49723af08d82Slm66018 } 49738e6a2a04Slm66018 49748e6a2a04Slm66018 return (rv == NULL ? ENOMEM : 0); 49758e6a2a04Slm66018 } 49768e6a2a04Slm66018 } 49778e6a2a04Slm66018 49781ae08745Sheppo /* catch programming error in vdc - should be a VD_OP_XXX ioctl */ 49793af08d82Slm66018 ASSERT(iop->op != 0); 49801ae08745Sheppo 4981*17cadca8Slm66018 /* check if the vDisk server handles the operation for this vDisk */ 4982*17cadca8Slm66018 if (VD_OP_SUPPORTED(vdc->operations, iop->op) == B_FALSE) { 4983*17cadca8Slm66018 DMSG(vdc, 0, "[%d] Unsupported VD_OP operation (0x%x)\n", 4984*17cadca8Slm66018 vdc->instance, iop->op); 4985*17cadca8Slm66018 return (ENOTSUP); 4986*17cadca8Slm66018 } 4987*17cadca8Slm66018 49881ae08745Sheppo /* LDC requires that the memory being mapped is 8-byte aligned */ 49891ae08745Sheppo alloc_len = P2ROUNDUP(len, sizeof (uint64_t)); 49903af08d82Slm66018 DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n", 49913af08d82Slm66018 instance, len, alloc_len); 49921ae08745Sheppo 4993eff7243fSlm66018 if (alloc_len > 0) 49941ae08745Sheppo mem_p = kmem_zalloc(alloc_len, KM_SLEEP); 49951ae08745Sheppo 49960a55fbb7Slm66018 /* 4997eff7243fSlm66018 * Call the conversion function for this ioctl which, if necessary, 49980a55fbb7Slm66018 * converts from the Solaris format to the format ARC'ed 49990a55fbb7Slm66018 * as part of the vDisk protocol (FWARC 2006/195) 50000a55fbb7Slm66018 */ 50013af08d82Slm66018 ASSERT(iop->convert != NULL); 50023af08d82Slm66018 rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN); 50031ae08745Sheppo if (rv != 0) { 50043af08d82Slm66018 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n", 5005e1ebb9ecSlm66018 instance, rv, cmd); 50061ae08745Sheppo if (mem_p != NULL) 50071ae08745Sheppo kmem_free(mem_p, alloc_len); 50080a55fbb7Slm66018 return (rv); 50091ae08745Sheppo } 50101ae08745Sheppo 50111ae08745Sheppo /* 50121ae08745Sheppo * send request to vds to service the ioctl. 50131ae08745Sheppo */ 50143af08d82Slm66018 rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len, 50150d0c8d4bSnarayan VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode, 50163af08d82Slm66018 VIO_both_dir); 50173af08d82Slm66018 501878fcd0a1Sachartre if (cmd == DKIOCSVTOC || cmd == DKIOCSETEFI) { 501978fcd0a1Sachartre /* 502078fcd0a1Sachartre * The disk label may have changed. Revalidate the disk 502178fcd0a1Sachartre * geometry. This will also update the device nodes and 502278fcd0a1Sachartre * properties. 502378fcd0a1Sachartre */ 502478fcd0a1Sachartre vdc_validate(vdc); 502578fcd0a1Sachartre } 502678fcd0a1Sachartre 50271ae08745Sheppo if (rv != 0) { 50281ae08745Sheppo /* 50291ae08745Sheppo * This is not necessarily an error. The ioctl could 50301ae08745Sheppo * be returning a value such as ENOTTY to indicate 50311ae08745Sheppo * that the ioctl is not applicable. 50321ae08745Sheppo */ 50333af08d82Slm66018 DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n", 5034e1ebb9ecSlm66018 instance, rv, cmd); 50351ae08745Sheppo if (mem_p != NULL) 50361ae08745Sheppo kmem_free(mem_p, alloc_len); 5037d10e4ef2Snarayan 50381ae08745Sheppo return (rv); 50391ae08745Sheppo } 50401ae08745Sheppo 50411ae08745Sheppo /* 50420a55fbb7Slm66018 * Call the conversion function (if it exists) for this ioctl 50430a55fbb7Slm66018 * which converts from the format ARC'ed as part of the vDisk 50440a55fbb7Slm66018 * protocol (FWARC 2006/195) back to a format understood by 50450a55fbb7Slm66018 * the rest of Solaris. 50461ae08745Sheppo */ 50473af08d82Slm66018 rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT); 50480a55fbb7Slm66018 if (rv != 0) { 50493af08d82Slm66018 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n", 5050e1ebb9ecSlm66018 instance, rv, cmd); 50511ae08745Sheppo if (mem_p != NULL) 50521ae08745Sheppo kmem_free(mem_p, alloc_len); 50530a55fbb7Slm66018 return (rv); 50541ae08745Sheppo } 50551ae08745Sheppo 50561ae08745Sheppo if (mem_p != NULL) 50571ae08745Sheppo kmem_free(mem_p, alloc_len); 50581ae08745Sheppo 50591ae08745Sheppo return (rv); 50601ae08745Sheppo } 50611ae08745Sheppo 50621ae08745Sheppo /* 50631ae08745Sheppo * Function: 50640a55fbb7Slm66018 * 50650a55fbb7Slm66018 * Description: 50660a55fbb7Slm66018 * This is an empty conversion function used by ioctl calls which 50670a55fbb7Slm66018 * do not need to convert the data being passed in/out to userland 50680a55fbb7Slm66018 */ 50690a55fbb7Slm66018 static int 5070d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir) 50710a55fbb7Slm66018 { 5072d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 50730a55fbb7Slm66018 _NOTE(ARGUNUSED(from)) 50740a55fbb7Slm66018 _NOTE(ARGUNUSED(to)) 50750a55fbb7Slm66018 _NOTE(ARGUNUSED(mode)) 50760a55fbb7Slm66018 _NOTE(ARGUNUSED(dir)) 50770a55fbb7Slm66018 50780a55fbb7Slm66018 return (0); 50790a55fbb7Slm66018 } 50800a55fbb7Slm66018 50814bac2208Snarayan static int 50824bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to, 50834bac2208Snarayan int mode, int dir) 50844bac2208Snarayan { 50854bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 50864bac2208Snarayan 50874bac2208Snarayan if (dir == VD_COPYIN) 50884bac2208Snarayan return (0); /* nothing to do */ 50894bac2208Snarayan 50904bac2208Snarayan if (ddi_copyout(from, to, sizeof (int), mode) != 0) 50914bac2208Snarayan return (EFAULT); 50924bac2208Snarayan 50934bac2208Snarayan return (0); 50944bac2208Snarayan } 50954bac2208Snarayan 50964bac2208Snarayan static int 50974bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to, 50984bac2208Snarayan int mode, int dir) 50994bac2208Snarayan { 51004bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 51014bac2208Snarayan 51024bac2208Snarayan if (dir == VD_COPYOUT) 51034bac2208Snarayan return (0); /* nothing to do */ 51044bac2208Snarayan 51054bac2208Snarayan if (ddi_copyin(from, to, sizeof (int), mode) != 0) 51064bac2208Snarayan return (EFAULT); 51074bac2208Snarayan 51084bac2208Snarayan return (0); 51094bac2208Snarayan } 51104bac2208Snarayan 51110a55fbb7Slm66018 /* 51120a55fbb7Slm66018 * Function: 51130a55fbb7Slm66018 * vdc_get_vtoc_convert() 51140a55fbb7Slm66018 * 51150a55fbb7Slm66018 * Description: 5116d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCGVTOC 5117d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 5118d10e4ef2Snarayan * 5119d10e4ef2Snarayan * In the struct vtoc definition, the timestamp field is marked as not 5120d10e4ef2Snarayan * supported so it is not part of vDisk protocol (FWARC 2006/195). 5121d10e4ef2Snarayan * However SVM uses that field to check it can write into the VTOC, 5122d10e4ef2Snarayan * so we fake up the info of that field. 51230a55fbb7Slm66018 * 51240a55fbb7Slm66018 * Arguments: 5125d10e4ef2Snarayan * vdc - the vDisk client 51260a55fbb7Slm66018 * from - the buffer containing the data to be copied from 51270a55fbb7Slm66018 * to - the buffer to be copied to 51280a55fbb7Slm66018 * mode - flags passed to ioctl() call 51290a55fbb7Slm66018 * dir - the "direction" of the copy - VD_COPYIN or VD_COPYOUT 51300a55fbb7Slm66018 * 51310a55fbb7Slm66018 * Return Code: 51320a55fbb7Slm66018 * 0 - Success 51330a55fbb7Slm66018 * ENXIO - incorrect buffer passed in. 5134d10e4ef2Snarayan * EFAULT - ddi_copyout routine encountered an error. 51350a55fbb7Slm66018 */ 51360a55fbb7Slm66018 static int 5137d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 51380a55fbb7Slm66018 { 5139d10e4ef2Snarayan int i; 51400a55fbb7Slm66018 void *tmp_mem = NULL; 51410a55fbb7Slm66018 void *tmp_memp; 51420a55fbb7Slm66018 struct vtoc vt; 51430a55fbb7Slm66018 struct vtoc32 vt32; 51440a55fbb7Slm66018 int copy_len = 0; 51450a55fbb7Slm66018 int rv = 0; 51460a55fbb7Slm66018 51470a55fbb7Slm66018 if (dir != VD_COPYOUT) 51480a55fbb7Slm66018 return (0); /* nothing to do */ 51490a55fbb7Slm66018 51500a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 51510a55fbb7Slm66018 return (ENXIO); 51520a55fbb7Slm66018 51530a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) 51540a55fbb7Slm66018 copy_len = sizeof (struct vtoc32); 51550a55fbb7Slm66018 else 51560a55fbb7Slm66018 copy_len = sizeof (struct vtoc); 51570a55fbb7Slm66018 51580a55fbb7Slm66018 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 51590a55fbb7Slm66018 51600a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)from, &vt); 5161d10e4ef2Snarayan 5162d10e4ef2Snarayan /* fake the VTOC timestamp field */ 5163d10e4ef2Snarayan for (i = 0; i < V_NUMPAR; i++) { 5164d10e4ef2Snarayan vt.timestamp[i] = vdc->vtoc->timestamp[i]; 5165d10e4ef2Snarayan } 5166d10e4ef2Snarayan 51670a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 5168*17cadca8Slm66018 /* LINTED E_ASSIGN_NARROW_CONV */ 51690a55fbb7Slm66018 vtoctovtoc32(vt, vt32); 51700a55fbb7Slm66018 tmp_memp = &vt32; 51710a55fbb7Slm66018 } else { 51720a55fbb7Slm66018 tmp_memp = &vt; 51730a55fbb7Slm66018 } 51740a55fbb7Slm66018 rv = ddi_copyout(tmp_memp, to, copy_len, mode); 51750a55fbb7Slm66018 if (rv != 0) 51760a55fbb7Slm66018 rv = EFAULT; 51770a55fbb7Slm66018 51780a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 51790a55fbb7Slm66018 return (rv); 51800a55fbb7Slm66018 } 51810a55fbb7Slm66018 51820a55fbb7Slm66018 /* 51830a55fbb7Slm66018 * Function: 51840a55fbb7Slm66018 * vdc_set_vtoc_convert() 51850a55fbb7Slm66018 * 51860a55fbb7Slm66018 * Description: 5187d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCSVTOC 5188d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 51890a55fbb7Slm66018 * 51900a55fbb7Slm66018 * Arguments: 5191d10e4ef2Snarayan * vdc - the vDisk client 51920a55fbb7Slm66018 * from - Buffer with data 51930a55fbb7Slm66018 * to - Buffer where data is to be copied to 51940a55fbb7Slm66018 * mode - flags passed to ioctl 51950a55fbb7Slm66018 * dir - direction of copy (in or out) 51960a55fbb7Slm66018 * 51970a55fbb7Slm66018 * Return Code: 51980a55fbb7Slm66018 * 0 - Success 51990a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 52000a55fbb7Slm66018 * EFAULT - ddi_copyin of data failed 52010a55fbb7Slm66018 */ 52020a55fbb7Slm66018 static int 5203d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 52040a55fbb7Slm66018 { 520578fcd0a1Sachartre _NOTE(ARGUNUSED(vdc)) 520678fcd0a1Sachartre 52070a55fbb7Slm66018 void *tmp_mem = NULL; 52080a55fbb7Slm66018 struct vtoc vt; 52090a55fbb7Slm66018 struct vtoc *vtp = &vt; 52100a55fbb7Slm66018 vd_vtoc_t vtvd; 52110a55fbb7Slm66018 int copy_len = 0; 52120a55fbb7Slm66018 int rv = 0; 52130a55fbb7Slm66018 52140a55fbb7Slm66018 if (dir != VD_COPYIN) 52150a55fbb7Slm66018 return (0); /* nothing to do */ 52160a55fbb7Slm66018 52170a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 52180a55fbb7Slm66018 return (ENXIO); 52190a55fbb7Slm66018 52200a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) 52210a55fbb7Slm66018 copy_len = sizeof (struct vtoc32); 52220a55fbb7Slm66018 else 52230a55fbb7Slm66018 copy_len = sizeof (struct vtoc); 52240a55fbb7Slm66018 52250a55fbb7Slm66018 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 52260a55fbb7Slm66018 52270a55fbb7Slm66018 rv = ddi_copyin(from, tmp_mem, copy_len, mode); 52280a55fbb7Slm66018 if (rv != 0) { 52290a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 52300a55fbb7Slm66018 return (EFAULT); 52310a55fbb7Slm66018 } 52320a55fbb7Slm66018 52330a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 52340a55fbb7Slm66018 vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt); 52350a55fbb7Slm66018 } else { 52360a55fbb7Slm66018 vtp = tmp_mem; 52370a55fbb7Slm66018 } 52380a55fbb7Slm66018 52390a55fbb7Slm66018 VTOC2VD_VTOC(vtp, &vtvd); 52400a55fbb7Slm66018 bcopy(&vtvd, to, sizeof (vd_vtoc_t)); 52410a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 52420a55fbb7Slm66018 52430a55fbb7Slm66018 return (0); 52440a55fbb7Slm66018 } 52450a55fbb7Slm66018 52460a55fbb7Slm66018 /* 52470a55fbb7Slm66018 * Function: 52480a55fbb7Slm66018 * vdc_get_geom_convert() 52490a55fbb7Slm66018 * 52500a55fbb7Slm66018 * Description: 5251d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCGGEOM, 5252d10e4ef2Snarayan * DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format 5253d10e4ef2Snarayan * defined in FWARC 2006/195 52540a55fbb7Slm66018 * 52550a55fbb7Slm66018 * Arguments: 5256d10e4ef2Snarayan * vdc - the vDisk client 52570a55fbb7Slm66018 * from - Buffer with data 52580a55fbb7Slm66018 * to - Buffer where data is to be copied to 52590a55fbb7Slm66018 * mode - flags passed to ioctl 52600a55fbb7Slm66018 * dir - direction of copy (in or out) 52610a55fbb7Slm66018 * 52620a55fbb7Slm66018 * Return Code: 52630a55fbb7Slm66018 * 0 - Success 52640a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 5265d10e4ef2Snarayan * EFAULT - ddi_copyout of data failed 52660a55fbb7Slm66018 */ 52670a55fbb7Slm66018 static int 5268d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 52690a55fbb7Slm66018 { 5270d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 5271d10e4ef2Snarayan 52720a55fbb7Slm66018 struct dk_geom geom; 52730a55fbb7Slm66018 int copy_len = sizeof (struct dk_geom); 52740a55fbb7Slm66018 int rv = 0; 52750a55fbb7Slm66018 52760a55fbb7Slm66018 if (dir != VD_COPYOUT) 52770a55fbb7Slm66018 return (0); /* nothing to do */ 52780a55fbb7Slm66018 52790a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 52800a55fbb7Slm66018 return (ENXIO); 52810a55fbb7Slm66018 52820a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom); 52830a55fbb7Slm66018 rv = ddi_copyout(&geom, to, copy_len, mode); 52840a55fbb7Slm66018 if (rv != 0) 52850a55fbb7Slm66018 rv = EFAULT; 52860a55fbb7Slm66018 52870a55fbb7Slm66018 return (rv); 52880a55fbb7Slm66018 } 52890a55fbb7Slm66018 52900a55fbb7Slm66018 /* 52910a55fbb7Slm66018 * Function: 52920a55fbb7Slm66018 * vdc_set_geom_convert() 52930a55fbb7Slm66018 * 52940a55fbb7Slm66018 * Description: 5295d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCSGEOM 5296d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 52970a55fbb7Slm66018 * 52980a55fbb7Slm66018 * Arguments: 5299d10e4ef2Snarayan * vdc - the vDisk client 53000a55fbb7Slm66018 * from - Buffer with data 53010a55fbb7Slm66018 * to - Buffer where data is to be copied to 53020a55fbb7Slm66018 * mode - flags passed to ioctl 53030a55fbb7Slm66018 * dir - direction of copy (in or out) 53040a55fbb7Slm66018 * 53050a55fbb7Slm66018 * Return Code: 53060a55fbb7Slm66018 * 0 - Success 53070a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 53080a55fbb7Slm66018 * EFAULT - ddi_copyin of data failed 53090a55fbb7Slm66018 */ 53100a55fbb7Slm66018 static int 5311d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 53120a55fbb7Slm66018 { 5313d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 5314d10e4ef2Snarayan 53150a55fbb7Slm66018 vd_geom_t vdgeom; 53160a55fbb7Slm66018 void *tmp_mem = NULL; 53170a55fbb7Slm66018 int copy_len = sizeof (struct dk_geom); 53180a55fbb7Slm66018 int rv = 0; 53190a55fbb7Slm66018 53200a55fbb7Slm66018 if (dir != VD_COPYIN) 53210a55fbb7Slm66018 return (0); /* nothing to do */ 53220a55fbb7Slm66018 53230a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 53240a55fbb7Slm66018 return (ENXIO); 53250a55fbb7Slm66018 53260a55fbb7Slm66018 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 53270a55fbb7Slm66018 53280a55fbb7Slm66018 rv = ddi_copyin(from, tmp_mem, copy_len, mode); 53290a55fbb7Slm66018 if (rv != 0) { 53300a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 53310a55fbb7Slm66018 return (EFAULT); 53320a55fbb7Slm66018 } 53330a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom); 53340a55fbb7Slm66018 bcopy(&vdgeom, to, sizeof (vdgeom)); 53350a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 53360a55fbb7Slm66018 53370a55fbb7Slm66018 return (0); 53380a55fbb7Slm66018 } 53390a55fbb7Slm66018 53404bac2208Snarayan static int 53414bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 53424bac2208Snarayan { 53434bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 53444bac2208Snarayan 53454bac2208Snarayan vd_efi_t *vd_efi; 53464bac2208Snarayan dk_efi_t dk_efi; 53474bac2208Snarayan int rv = 0; 53484bac2208Snarayan void *uaddr; 53494bac2208Snarayan 53504bac2208Snarayan if ((from == NULL) || (to == NULL)) 53514bac2208Snarayan return (ENXIO); 53524bac2208Snarayan 53534bac2208Snarayan if (dir == VD_COPYIN) { 53544bac2208Snarayan 53554bac2208Snarayan vd_efi = (vd_efi_t *)to; 53564bac2208Snarayan 53574bac2208Snarayan rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode); 53584bac2208Snarayan if (rv != 0) 53594bac2208Snarayan return (EFAULT); 53604bac2208Snarayan 53614bac2208Snarayan vd_efi->lba = dk_efi.dki_lba; 53624bac2208Snarayan vd_efi->length = dk_efi.dki_length; 53634bac2208Snarayan bzero(vd_efi->data, vd_efi->length); 53644bac2208Snarayan 53654bac2208Snarayan } else { 53664bac2208Snarayan 53674bac2208Snarayan rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode); 53684bac2208Snarayan if (rv != 0) 53694bac2208Snarayan return (EFAULT); 53704bac2208Snarayan 53714bac2208Snarayan uaddr = dk_efi.dki_data; 53724bac2208Snarayan 53734bac2208Snarayan dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP); 53744bac2208Snarayan 53754bac2208Snarayan VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi); 53764bac2208Snarayan 53774bac2208Snarayan rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length, 53784bac2208Snarayan mode); 53794bac2208Snarayan if (rv != 0) 53804bac2208Snarayan return (EFAULT); 53814bac2208Snarayan 53824bac2208Snarayan kmem_free(dk_efi.dki_data, dk_efi.dki_length); 53834bac2208Snarayan } 53844bac2208Snarayan 53854bac2208Snarayan return (0); 53864bac2208Snarayan } 53874bac2208Snarayan 53884bac2208Snarayan static int 53894bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 53904bac2208Snarayan { 53914bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 53924bac2208Snarayan 53934bac2208Snarayan dk_efi_t dk_efi; 53944bac2208Snarayan void *uaddr; 53954bac2208Snarayan 53964bac2208Snarayan if (dir == VD_COPYOUT) 53974bac2208Snarayan return (0); /* nothing to do */ 53984bac2208Snarayan 53994bac2208Snarayan if ((from == NULL) || (to == NULL)) 54004bac2208Snarayan return (ENXIO); 54014bac2208Snarayan 54024bac2208Snarayan if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0) 54034bac2208Snarayan return (EFAULT); 54044bac2208Snarayan 54054bac2208Snarayan uaddr = dk_efi.dki_data; 54064bac2208Snarayan 54074bac2208Snarayan dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP); 54084bac2208Snarayan 54094bac2208Snarayan if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0) 54104bac2208Snarayan return (EFAULT); 54114bac2208Snarayan 54124bac2208Snarayan DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to); 54134bac2208Snarayan 54144bac2208Snarayan kmem_free(dk_efi.dki_data, dk_efi.dki_length); 54154bac2208Snarayan 54164bac2208Snarayan return (0); 54174bac2208Snarayan } 54184bac2208Snarayan 5419*17cadca8Slm66018 5420*17cadca8Slm66018 /* -------------------------------------------------------------------------- */ 5421*17cadca8Slm66018 54220a55fbb7Slm66018 /* 54230a55fbb7Slm66018 * Function: 54241ae08745Sheppo * vdc_create_fake_geometry() 54251ae08745Sheppo * 54261ae08745Sheppo * Description: 5427*17cadca8Slm66018 * This routine fakes up the disk info needed for some DKIO ioctls such 5428*17cadca8Slm66018 * as DKIOCINFO and DKIOCGMEDIAINFO [just like lofi(7D) and ramdisk(7D) do] 54291ae08745Sheppo * 5430*17cadca8Slm66018 * Note: This function must not be called until the vDisk attributes have 5431*17cadca8Slm66018 * been exchanged as part of the handshake with the vDisk server. 54321ae08745Sheppo * 54331ae08745Sheppo * Arguments: 54341ae08745Sheppo * vdc - soft state pointer for this instance of the device driver. 54351ae08745Sheppo * 54361ae08745Sheppo * Return Code: 543778fcd0a1Sachartre * none. 54381ae08745Sheppo */ 543978fcd0a1Sachartre static void 54401ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc) 54411ae08745Sheppo { 54421ae08745Sheppo ASSERT(vdc != NULL); 544378fcd0a1Sachartre ASSERT(vdc->vdisk_size != 0); 544478fcd0a1Sachartre ASSERT(vdc->max_xfer_sz != 0); 54450d0c8d4bSnarayan 54460d0c8d4bSnarayan /* 54471ae08745Sheppo * DKIOCINFO support 54481ae08745Sheppo */ 544978fcd0a1Sachartre if (vdc->cinfo == NULL) 54501ae08745Sheppo vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 54511ae08745Sheppo 54521ae08745Sheppo (void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME); 54531ae08745Sheppo (void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME); 54548e6a2a04Slm66018 /* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */ 54558e6a2a04Slm66018 vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz; 545687a7269eSachartre /* 545787a7269eSachartre * We currently set the controller type to DKC_DIRECT for any disk. 545887a7269eSachartre * When SCSI support is implemented, we will eventually change this 545987a7269eSachartre * type to DKC_SCSI_CCS for disks supporting the SCSI protocol. 5460*17cadca8Slm66018 * If the virtual disk is backed by a physical CD/DVD device or 5461*17cadca8Slm66018 * an ISO image, modify the controller type to indicate this 546287a7269eSachartre */ 5463*17cadca8Slm66018 switch (vdc->vdisk_media) { 5464*17cadca8Slm66018 case VD_MEDIA_CD: 5465*17cadca8Slm66018 case VD_MEDIA_DVD: 5466*17cadca8Slm66018 vdc->cinfo->dki_ctype = DKC_CDROM; 5467*17cadca8Slm66018 break; 5468*17cadca8Slm66018 case VD_MEDIA_FIXED: 546987a7269eSachartre vdc->cinfo->dki_ctype = DKC_DIRECT; 5470*17cadca8Slm66018 break; 5471*17cadca8Slm66018 default: 5472*17cadca8Slm66018 /* in the case of v1.0 we default to a fixed disk */ 5473*17cadca8Slm66018 vdc->cinfo->dki_ctype = DKC_DIRECT; 5474*17cadca8Slm66018 break; 5475*17cadca8Slm66018 } 54761ae08745Sheppo vdc->cinfo->dki_flags = DKI_FMTVOL; 54771ae08745Sheppo vdc->cinfo->dki_cnum = 0; 54781ae08745Sheppo vdc->cinfo->dki_addr = 0; 54791ae08745Sheppo vdc->cinfo->dki_space = 0; 54801ae08745Sheppo vdc->cinfo->dki_prio = 0; 54811ae08745Sheppo vdc->cinfo->dki_vec = 0; 54821ae08745Sheppo vdc->cinfo->dki_unit = vdc->instance; 54831ae08745Sheppo vdc->cinfo->dki_slave = 0; 54841ae08745Sheppo /* 54851ae08745Sheppo * The partition number will be created on the fly depending on the 54861ae08745Sheppo * actual slice (i.e. minor node) that is used to request the data. 54871ae08745Sheppo */ 54881ae08745Sheppo vdc->cinfo->dki_partition = 0; 54891ae08745Sheppo 54901ae08745Sheppo /* 54911ae08745Sheppo * DKIOCGMEDIAINFO support 54921ae08745Sheppo */ 54930a55fbb7Slm66018 if (vdc->minfo == NULL) 54941ae08745Sheppo vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP); 5495*17cadca8Slm66018 5496*17cadca8Slm66018 if (vio_ver_is_supported(vdc->ver, 1, 1)) { 5497*17cadca8Slm66018 vdc->minfo->dki_media_type = 5498*17cadca8Slm66018 VD_MEDIATYPE2DK_MEDIATYPE(vdc->vdisk_media); 5499*17cadca8Slm66018 } else { 55001ae08745Sheppo vdc->minfo->dki_media_type = DK_FIXED_DISK; 5501*17cadca8Slm66018 } 5502*17cadca8Slm66018 55034bac2208Snarayan vdc->minfo->dki_capacity = vdc->vdisk_size; 5504*17cadca8Slm66018 vdc->minfo->dki_lbsize = vdc->block_size; 550578fcd0a1Sachartre } 55061ae08745Sheppo 550778fcd0a1Sachartre static ushort_t 550878fcd0a1Sachartre vdc_lbl2cksum(struct dk_label *label) 550978fcd0a1Sachartre { 551078fcd0a1Sachartre int count; 551178fcd0a1Sachartre ushort_t sum, *sp; 551278fcd0a1Sachartre 551378fcd0a1Sachartre count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 551478fcd0a1Sachartre sp = (ushort_t *)label; 551578fcd0a1Sachartre sum = 0; 551678fcd0a1Sachartre while (count--) { 551778fcd0a1Sachartre sum ^= *sp++; 551878fcd0a1Sachartre } 551978fcd0a1Sachartre 552078fcd0a1Sachartre return (sum); 55210a55fbb7Slm66018 } 55220a55fbb7Slm66018 55230a55fbb7Slm66018 /* 55240a55fbb7Slm66018 * Function: 552578fcd0a1Sachartre * vdc_validate_geometry 55260a55fbb7Slm66018 * 55270a55fbb7Slm66018 * Description: 552878fcd0a1Sachartre * This routine discovers the label and geometry of the disk. It stores 552978fcd0a1Sachartre * the disk label and related information in the vdc structure. If it 553078fcd0a1Sachartre * fails to validate the geometry or to discover the disk label then 553178fcd0a1Sachartre * the label is marked as unknown (VD_DISK_LABEL_UNK). 55320a55fbb7Slm66018 * 55330a55fbb7Slm66018 * Arguments: 55340a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 55350a55fbb7Slm66018 * 55360a55fbb7Slm66018 * Return Code: 553778fcd0a1Sachartre * 0 - success. 553878fcd0a1Sachartre * EINVAL - unknown disk label. 553978fcd0a1Sachartre * ENOTSUP - geometry not applicable (EFI label). 554078fcd0a1Sachartre * EIO - error accessing the disk. 55410a55fbb7Slm66018 */ 55420a55fbb7Slm66018 static int 554378fcd0a1Sachartre vdc_validate_geometry(vdc_t *vdc) 55440a55fbb7Slm66018 { 5545d10e4ef2Snarayan buf_t *buf; /* BREAD requests need to be in a buf_t structure */ 55460a55fbb7Slm66018 dev_t dev; 554778fcd0a1Sachartre int rv; 554878fcd0a1Sachartre struct dk_label label; 554978fcd0a1Sachartre struct dk_geom geom; 555078fcd0a1Sachartre struct vtoc vtoc; 55510a55fbb7Slm66018 55520a55fbb7Slm66018 ASSERT(vdc != NULL); 555378fcd0a1Sachartre ASSERT(vdc->vtoc != NULL && vdc->geom != NULL); 555478fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 55550a55fbb7Slm66018 555678fcd0a1Sachartre mutex_exit(&vdc->lock); 55570a55fbb7Slm66018 55580a55fbb7Slm66018 dev = makedevice(ddi_driver_major(vdc->dip), 55590a55fbb7Slm66018 VD_MAKE_DEV(vdc->instance, 0)); 55604bac2208Snarayan 556178fcd0a1Sachartre rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL); 556278fcd0a1Sachartre if (rv == 0) 556378fcd0a1Sachartre rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)&vtoc, FKIOCTL); 55640d0c8d4bSnarayan 55654bac2208Snarayan if (rv == ENOTSUP) { 55664bac2208Snarayan /* 55674bac2208Snarayan * If the device does not support VTOC then we try 55684bac2208Snarayan * to read an EFI label. 55694bac2208Snarayan */ 55704bac2208Snarayan struct dk_gpt *efi; 55714bac2208Snarayan size_t efi_len; 55724bac2208Snarayan 55734bac2208Snarayan rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len); 55744bac2208Snarayan 55754bac2208Snarayan if (rv) { 55763af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)", 55774bac2208Snarayan vdc->instance, rv); 557878fcd0a1Sachartre mutex_enter(&vdc->lock); 557978fcd0a1Sachartre vdc_store_label_unk(vdc); 558078fcd0a1Sachartre return (EIO); 558178fcd0a1Sachartre } 558278fcd0a1Sachartre 558378fcd0a1Sachartre mutex_enter(&vdc->lock); 558478fcd0a1Sachartre vdc_store_label_efi(vdc, efi); 558578fcd0a1Sachartre vd_efi_free(efi, efi_len); 558678fcd0a1Sachartre return (ENOTSUP); 558778fcd0a1Sachartre } 558878fcd0a1Sachartre 558978fcd0a1Sachartre if (rv != 0) { 559078fcd0a1Sachartre DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)", 559178fcd0a1Sachartre vdc->instance, rv); 559278fcd0a1Sachartre mutex_enter(&vdc->lock); 559378fcd0a1Sachartre vdc_store_label_unk(vdc); 559478fcd0a1Sachartre if (rv != EINVAL) 559578fcd0a1Sachartre rv = EIO; 55964bac2208Snarayan return (rv); 55974bac2208Snarayan } 55984bac2208Snarayan 559978fcd0a1Sachartre /* check that geometry and vtoc are valid */ 560078fcd0a1Sachartre if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 || 560178fcd0a1Sachartre vtoc.v_sanity != VTOC_SANE) { 560278fcd0a1Sachartre mutex_enter(&vdc->lock); 560378fcd0a1Sachartre vdc_store_label_unk(vdc); 560478fcd0a1Sachartre return (EINVAL); 560578fcd0a1Sachartre } 56064bac2208Snarayan 560778fcd0a1Sachartre /* 560878fcd0a1Sachartre * We have a disk and a valid VTOC. However this does not mean 560978fcd0a1Sachartre * that the disk currently have a VTOC label. The returned VTOC may 561078fcd0a1Sachartre * be a default VTOC to be used for configuring the disk (this is 561178fcd0a1Sachartre * what is done for disk image). So we read the label from the 561278fcd0a1Sachartre * beginning of the disk to ensure we really have a VTOC label. 561378fcd0a1Sachartre * 561478fcd0a1Sachartre * FUTURE: This could be the default way for reading the VTOC 561578fcd0a1Sachartre * from the disk as opposed to sending the VD_OP_GET_VTOC 561678fcd0a1Sachartre * to the server. This will be the default if vdc is implemented 561778fcd0a1Sachartre * ontop of cmlb. 561878fcd0a1Sachartre */ 561978fcd0a1Sachartre 562078fcd0a1Sachartre /* 562178fcd0a1Sachartre * Single slice disk does not support read using an absolute disk 562278fcd0a1Sachartre * offset so we just rely on the DKIOCGVTOC ioctl in that case. 562378fcd0a1Sachartre */ 562478fcd0a1Sachartre if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) { 562578fcd0a1Sachartre mutex_enter(&vdc->lock); 562678fcd0a1Sachartre if (vtoc.v_nparts != 1) { 562778fcd0a1Sachartre vdc_store_label_unk(vdc); 562878fcd0a1Sachartre return (EINVAL); 562978fcd0a1Sachartre } 563078fcd0a1Sachartre vdc_store_label_vtoc(vdc, &geom, &vtoc); 56314bac2208Snarayan return (0); 56324bac2208Snarayan } 56334bac2208Snarayan 563478fcd0a1Sachartre if (vtoc.v_nparts != V_NUMPAR) { 563578fcd0a1Sachartre mutex_enter(&vdc->lock); 563678fcd0a1Sachartre vdc_store_label_unk(vdc); 563778fcd0a1Sachartre return (EINVAL); 56380a55fbb7Slm66018 } 5639d10e4ef2Snarayan 5640d10e4ef2Snarayan /* 5641d10e4ef2Snarayan * Read disk label from start of disk 5642d10e4ef2Snarayan */ 5643d10e4ef2Snarayan buf = kmem_alloc(sizeof (buf_t), KM_SLEEP); 5644d10e4ef2Snarayan bioinit(buf); 564578fcd0a1Sachartre buf->b_un.b_addr = (caddr_t)&label; 5646d10e4ef2Snarayan buf->b_bcount = DK_LABEL_SIZE; 5647d10e4ef2Snarayan buf->b_flags = B_BUSY | B_READ; 5648*17cadca8Slm66018 buf->b_dev = cmpdev(dev); 564978fcd0a1Sachartre rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)&label, 565078fcd0a1Sachartre DK_LABEL_SIZE, VD_SLICE_NONE, 0, CB_STRATEGY, buf, VIO_read_dir); 56513af08d82Slm66018 if (rv) { 56523af08d82Slm66018 DMSG(vdc, 1, "[%d] Failed to read disk block 0\n", 56533af08d82Slm66018 vdc->instance); 565478fcd0a1Sachartre } else { 5655d10e4ef2Snarayan rv = biowait(buf); 5656d10e4ef2Snarayan biofini(buf); 565778fcd0a1Sachartre } 5658d10e4ef2Snarayan kmem_free(buf, sizeof (buf_t)); 56590a55fbb7Slm66018 566078fcd0a1Sachartre if (rv != 0 || label.dkl_magic != DKL_MAGIC || 566178fcd0a1Sachartre label.dkl_cksum != vdc_lbl2cksum(&label)) { 566278fcd0a1Sachartre DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n", 566378fcd0a1Sachartre vdc->instance); 566478fcd0a1Sachartre mutex_enter(&vdc->lock); 566578fcd0a1Sachartre vdc_store_label_unk(vdc); 566678fcd0a1Sachartre return (EINVAL); 566778fcd0a1Sachartre } 566878fcd0a1Sachartre 566978fcd0a1Sachartre mutex_enter(&vdc->lock); 567078fcd0a1Sachartre vdc_store_label_vtoc(vdc, &geom, &vtoc); 567178fcd0a1Sachartre return (0); 567278fcd0a1Sachartre } 567378fcd0a1Sachartre 567478fcd0a1Sachartre /* 567578fcd0a1Sachartre * Function: 567678fcd0a1Sachartre * vdc_validate 567778fcd0a1Sachartre * 567878fcd0a1Sachartre * Description: 567978fcd0a1Sachartre * This routine discovers the label of the disk and create the 568078fcd0a1Sachartre * appropriate device nodes if the label has changed. 568178fcd0a1Sachartre * 568278fcd0a1Sachartre * Arguments: 568378fcd0a1Sachartre * vdc - soft state pointer for this instance of the device driver. 568478fcd0a1Sachartre * 568578fcd0a1Sachartre * Return Code: 568678fcd0a1Sachartre * none. 568778fcd0a1Sachartre */ 568878fcd0a1Sachartre static void 568978fcd0a1Sachartre vdc_validate(vdc_t *vdc) 569078fcd0a1Sachartre { 569178fcd0a1Sachartre vd_disk_label_t old_label; 569278fcd0a1Sachartre struct vtoc old_vtoc; 569378fcd0a1Sachartre int rv; 569478fcd0a1Sachartre 569578fcd0a1Sachartre ASSERT(!MUTEX_HELD(&vdc->lock)); 569678fcd0a1Sachartre 569778fcd0a1Sachartre mutex_enter(&vdc->lock); 569878fcd0a1Sachartre 569978fcd0a1Sachartre /* save the current label and vtoc */ 570078fcd0a1Sachartre old_label = vdc->vdisk_label; 570178fcd0a1Sachartre bcopy(vdc->vtoc, &old_vtoc, sizeof (struct vtoc)); 570278fcd0a1Sachartre 570378fcd0a1Sachartre /* check the geometry */ 570478fcd0a1Sachartre (void) vdc_validate_geometry(vdc); 570578fcd0a1Sachartre 570678fcd0a1Sachartre /* if the disk label has changed, update device nodes */ 570778fcd0a1Sachartre if (vdc->vdisk_label != old_label) { 570878fcd0a1Sachartre 570978fcd0a1Sachartre if (vdc->vdisk_label == VD_DISK_LABEL_EFI) 571078fcd0a1Sachartre rv = vdc_create_device_nodes_efi(vdc); 571178fcd0a1Sachartre else 571278fcd0a1Sachartre rv = vdc_create_device_nodes_vtoc(vdc); 571378fcd0a1Sachartre 571478fcd0a1Sachartre if (rv != 0) { 571578fcd0a1Sachartre DMSG(vdc, 0, "![%d] Failed to update device nodes", 571678fcd0a1Sachartre vdc->instance); 571778fcd0a1Sachartre } 571878fcd0a1Sachartre } 571978fcd0a1Sachartre 572078fcd0a1Sachartre /* if the vtoc has changed, update device nodes properties */ 572178fcd0a1Sachartre if (bcmp(vdc->vtoc, &old_vtoc, sizeof (struct vtoc)) != 0) { 572278fcd0a1Sachartre 572378fcd0a1Sachartre if (vdc_create_device_nodes_props(vdc) != 0) { 572478fcd0a1Sachartre DMSG(vdc, 0, "![%d] Failed to update device nodes" 572578fcd0a1Sachartre " properties", vdc->instance); 572678fcd0a1Sachartre } 572778fcd0a1Sachartre } 572878fcd0a1Sachartre 572978fcd0a1Sachartre mutex_exit(&vdc->lock); 573078fcd0a1Sachartre } 573178fcd0a1Sachartre 573278fcd0a1Sachartre static void 573378fcd0a1Sachartre vdc_validate_task(void *arg) 573478fcd0a1Sachartre { 573578fcd0a1Sachartre vdc_t *vdc = (vdc_t *)arg; 573678fcd0a1Sachartre 573778fcd0a1Sachartre vdc_validate(vdc); 573878fcd0a1Sachartre 573978fcd0a1Sachartre mutex_enter(&vdc->lock); 574078fcd0a1Sachartre ASSERT(vdc->validate_pending > 0); 574178fcd0a1Sachartre vdc->validate_pending--; 574278fcd0a1Sachartre mutex_exit(&vdc->lock); 57431ae08745Sheppo } 57444bac2208Snarayan 57454bac2208Snarayan /* 57464bac2208Snarayan * Function: 57474bac2208Snarayan * vdc_setup_devid() 57484bac2208Snarayan * 57494bac2208Snarayan * Description: 57504bac2208Snarayan * This routine discovers the devid of a vDisk. It requests the devid of 57514bac2208Snarayan * the underlying device from the vDisk server, builds an encapsulated 57524bac2208Snarayan * devid based on the retrieved devid and registers that new devid to 57534bac2208Snarayan * the vDisk. 57544bac2208Snarayan * 57554bac2208Snarayan * Arguments: 57564bac2208Snarayan * vdc - soft state pointer for this instance of the device driver. 57574bac2208Snarayan * 57584bac2208Snarayan * Return Code: 57594bac2208Snarayan * 0 - A devid was succesfully registered for the vDisk 57604bac2208Snarayan */ 57614bac2208Snarayan static int 57624bac2208Snarayan vdc_setup_devid(vdc_t *vdc) 57634bac2208Snarayan { 57644bac2208Snarayan int rv; 57654bac2208Snarayan vd_devid_t *vd_devid; 57664bac2208Snarayan size_t bufsize, bufid_len; 57674bac2208Snarayan 57684bac2208Snarayan /* 57694bac2208Snarayan * At first sight, we don't know the size of the devid that the 57704bac2208Snarayan * server will return but this size will be encoded into the 57714bac2208Snarayan * reply. So we do a first request using a default size then we 57724bac2208Snarayan * check if this size was large enough. If not then we do a second 57734bac2208Snarayan * request with the correct size returned by the server. Note that 57744bac2208Snarayan * ldc requires size to be 8-byte aligned. 57754bac2208Snarayan */ 57764bac2208Snarayan bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN), 57774bac2208Snarayan sizeof (uint64_t)); 57784bac2208Snarayan vd_devid = kmem_zalloc(bufsize, KM_SLEEP); 57794bac2208Snarayan bufid_len = bufsize - sizeof (vd_efi_t) - 1; 57804bac2208Snarayan 57813af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid, 57823af08d82Slm66018 bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir); 57833af08d82Slm66018 57843af08d82Slm66018 DMSG(vdc, 2, "sync_op returned %d\n", rv); 57853af08d82Slm66018 57864bac2208Snarayan if (rv) { 57874bac2208Snarayan kmem_free(vd_devid, bufsize); 57884bac2208Snarayan return (rv); 57894bac2208Snarayan } 57904bac2208Snarayan 57914bac2208Snarayan if (vd_devid->length > bufid_len) { 57924bac2208Snarayan /* 57934bac2208Snarayan * The returned devid is larger than the buffer used. Try again 57944bac2208Snarayan * with a buffer with the right size. 57954bac2208Snarayan */ 57964bac2208Snarayan kmem_free(vd_devid, bufsize); 57974bac2208Snarayan bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length), 57984bac2208Snarayan sizeof (uint64_t)); 57994bac2208Snarayan vd_devid = kmem_zalloc(bufsize, KM_SLEEP); 58004bac2208Snarayan bufid_len = bufsize - sizeof (vd_efi_t) - 1; 58014bac2208Snarayan 58023af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, 58033af08d82Slm66018 (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0, 58043af08d82Slm66018 VIO_both_dir); 58053af08d82Slm66018 58064bac2208Snarayan if (rv) { 58074bac2208Snarayan kmem_free(vd_devid, bufsize); 58084bac2208Snarayan return (rv); 58094bac2208Snarayan } 58104bac2208Snarayan } 58114bac2208Snarayan 58124bac2208Snarayan /* 58134bac2208Snarayan * The virtual disk should have the same device id as the one associated 58144bac2208Snarayan * with the physical disk it is mapped on, otherwise sharing a disk 58154bac2208Snarayan * between a LDom and a non-LDom may not work (for example for a shared 58164bac2208Snarayan * SVM disk set). 58174bac2208Snarayan * 58184bac2208Snarayan * The DDI framework does not allow creating a device id with any 58194bac2208Snarayan * type so we first create a device id of type DEVID_ENCAP and then 58204bac2208Snarayan * we restore the orignal type of the physical device. 58214bac2208Snarayan */ 58224bac2208Snarayan 58233af08d82Slm66018 DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length); 58243af08d82Slm66018 58254bac2208Snarayan /* build an encapsulated devid based on the returned devid */ 58264bac2208Snarayan if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length, 58274bac2208Snarayan vd_devid->id, &vdc->devid) != DDI_SUCCESS) { 58283af08d82Slm66018 DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance); 58294bac2208Snarayan kmem_free(vd_devid, bufsize); 58304bac2208Snarayan return (1); 58314bac2208Snarayan } 58324bac2208Snarayan 58334bac2208Snarayan DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type); 58344bac2208Snarayan 58354bac2208Snarayan ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS); 58364bac2208Snarayan 58374bac2208Snarayan kmem_free(vd_devid, bufsize); 58384bac2208Snarayan 58394bac2208Snarayan if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) { 58403af08d82Slm66018 DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance); 58414bac2208Snarayan return (1); 58424bac2208Snarayan } 58434bac2208Snarayan 58444bac2208Snarayan return (0); 58454bac2208Snarayan } 58464bac2208Snarayan 58474bac2208Snarayan static void 584878fcd0a1Sachartre vdc_store_label_efi(vdc_t *vdc, struct dk_gpt *efi) 58494bac2208Snarayan { 58504bac2208Snarayan struct vtoc *vtoc = vdc->vtoc; 58514bac2208Snarayan 585278fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 585378fcd0a1Sachartre 585478fcd0a1Sachartre vdc->vdisk_label = VD_DISK_LABEL_EFI; 585578fcd0a1Sachartre bzero(vdc->geom, sizeof (struct dk_geom)); 58564bac2208Snarayan vd_efi_to_vtoc(efi, vtoc); 58574bac2208Snarayan if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) { 58584bac2208Snarayan /* 58594bac2208Snarayan * vd_efi_to_vtoc() will store information about the EFI Sun 58604bac2208Snarayan * reserved partition (representing the entire disk) into 58614bac2208Snarayan * partition 7. However single-slice device will only have 58624bac2208Snarayan * that single partition and the vdc driver expects to find 58634bac2208Snarayan * information about that partition in slice 0. So we need 58644bac2208Snarayan * to copy information from slice 7 to slice 0. 58654bac2208Snarayan */ 58664bac2208Snarayan vtoc->v_part[0].p_tag = vtoc->v_part[VD_EFI_WD_SLICE].p_tag; 58674bac2208Snarayan vtoc->v_part[0].p_flag = vtoc->v_part[VD_EFI_WD_SLICE].p_flag; 58684bac2208Snarayan vtoc->v_part[0].p_start = vtoc->v_part[VD_EFI_WD_SLICE].p_start; 58694bac2208Snarayan vtoc->v_part[0].p_size = vtoc->v_part[VD_EFI_WD_SLICE].p_size; 58704bac2208Snarayan } 58714bac2208Snarayan } 587278fcd0a1Sachartre 587378fcd0a1Sachartre static void 587478fcd0a1Sachartre vdc_store_label_vtoc(vdc_t *vdc, struct dk_geom *geom, struct vtoc *vtoc) 587578fcd0a1Sachartre { 587678fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 587778fcd0a1Sachartre 587878fcd0a1Sachartre vdc->vdisk_label = VD_DISK_LABEL_VTOC; 587978fcd0a1Sachartre bcopy(vtoc, vdc->vtoc, sizeof (struct vtoc)); 588078fcd0a1Sachartre bcopy(geom, vdc->geom, sizeof (struct dk_geom)); 588178fcd0a1Sachartre } 588278fcd0a1Sachartre 588378fcd0a1Sachartre static void 588478fcd0a1Sachartre vdc_store_label_unk(vdc_t *vdc) 588578fcd0a1Sachartre { 588678fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 588778fcd0a1Sachartre 588878fcd0a1Sachartre vdc->vdisk_label = VD_DISK_LABEL_UNK; 588978fcd0a1Sachartre bzero(vdc->vtoc, sizeof (struct vtoc)); 589078fcd0a1Sachartre bzero(vdc->geom, sizeof (struct dk_geom)); 589178fcd0a1Sachartre } 5892