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 /* 23edcc0754Sachartre * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 241ae08745Sheppo * Use is subject to license terms. 251ae08745Sheppo */ 261ae08745Sheppo 271ae08745Sheppo /* 281ae08745Sheppo * LDoms virtual disk client (vdc) device driver 291ae08745Sheppo * 301ae08745Sheppo * This driver runs on a guest logical domain and communicates with the virtual 311ae08745Sheppo * disk server (vds) driver running on the service domain which is exporting 321ae08745Sheppo * virtualized "disks" to the guest logical domain. 331ae08745Sheppo * 341ae08745Sheppo * The driver can be divided into four sections: 351ae08745Sheppo * 361ae08745Sheppo * 1) generic device driver housekeeping 371ae08745Sheppo * _init, _fini, attach, detach, ops structures, etc. 381ae08745Sheppo * 391ae08745Sheppo * 2) communication channel setup 401ae08745Sheppo * Setup the communications link over the LDC channel that vdc uses to 411ae08745Sheppo * talk to the vDisk server. Initialise the descriptor ring which 421ae08745Sheppo * allows the LDC clients to transfer data via memory mappings. 431ae08745Sheppo * 441ae08745Sheppo * 3) Support exported to upper layers (filesystems, etc) 451ae08745Sheppo * The upper layers call into vdc via strategy(9E) and DKIO(7I) 461ae08745Sheppo * ioctl calls. vdc will copy the data to be written to the descriptor 471ae08745Sheppo * ring or maps the buffer to store the data read by the vDisk 481ae08745Sheppo * server into the descriptor ring. It then sends a message to the 491ae08745Sheppo * vDisk server requesting it to complete the operation. 501ae08745Sheppo * 511ae08745Sheppo * 4) Handling responses from vDisk server. 521ae08745Sheppo * The vDisk server will ACK some or all of the messages vdc sends to it 531ae08745Sheppo * (this is configured during the handshake). Upon receipt of an ACK 541ae08745Sheppo * vdc will check the descriptor ring and signal to the upper layer 551ae08745Sheppo * code waiting on the IO. 561ae08745Sheppo */ 571ae08745Sheppo 58e1ebb9ecSlm66018 #include <sys/atomic.h> 591ae08745Sheppo #include <sys/conf.h> 601ae08745Sheppo #include <sys/disp.h> 611ae08745Sheppo #include <sys/ddi.h> 621ae08745Sheppo #include <sys/dkio.h> 631ae08745Sheppo #include <sys/efi_partition.h> 641ae08745Sheppo #include <sys/fcntl.h> 651ae08745Sheppo #include <sys/file.h> 66366a92acSlm66018 #include <sys/kstat.h> 671ae08745Sheppo #include <sys/mach_descrip.h> 681ae08745Sheppo #include <sys/modctl.h> 691ae08745Sheppo #include <sys/mdeg.h> 701ae08745Sheppo #include <sys/note.h> 711ae08745Sheppo #include <sys/open.h> 72d10e4ef2Snarayan #include <sys/sdt.h> 731ae08745Sheppo #include <sys/stat.h> 741ae08745Sheppo #include <sys/sunddi.h> 751ae08745Sheppo #include <sys/types.h> 761ae08745Sheppo #include <sys/promif.h> 772f5224aeSachartre #include <sys/var.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> 852f5224aeSachartre #include <sys/mhd.h> 861ae08745Sheppo #include <sys/scsi/generic/sense.h> 872f5224aeSachartre #include <sys/scsi/impl/uscsi.h> 882f5224aeSachartre #include <sys/scsi/impl/services.h> 892f5224aeSachartre #include <sys/scsi/targets/sddef.h> 901ae08745Sheppo 911ae08745Sheppo #include <sys/ldoms.h> 921ae08745Sheppo #include <sys/ldc.h> 931ae08745Sheppo #include <sys/vio_common.h> 941ae08745Sheppo #include <sys/vio_mailbox.h> 9517cadca8Slm66018 #include <sys/vio_util.h> 961ae08745Sheppo #include <sys/vdsk_common.h> 971ae08745Sheppo #include <sys/vdsk_mailbox.h> 981ae08745Sheppo #include <sys/vdc.h> 991ae08745Sheppo 100342440ecSPrasad Singamsetty #define VD_OLDVTOC_LIMIT 0x7fffffff 101342440ecSPrasad Singamsetty 1021ae08745Sheppo /* 1031ae08745Sheppo * function prototypes 1041ae08745Sheppo */ 1051ae08745Sheppo 1061ae08745Sheppo /* standard driver functions */ 1071ae08745Sheppo static int vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred); 1081ae08745Sheppo static int vdc_close(dev_t dev, int flag, int otyp, cred_t *cred); 1091ae08745Sheppo static int vdc_strategy(struct buf *buf); 1101ae08745Sheppo static int vdc_print(dev_t dev, char *str); 1111ae08745Sheppo static int vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1121ae08745Sheppo static int vdc_read(dev_t dev, struct uio *uio, cred_t *cred); 1131ae08745Sheppo static int vdc_write(dev_t dev, struct uio *uio, cred_t *cred); 1141ae08745Sheppo static int vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 1151ae08745Sheppo cred_t *credp, int *rvalp); 1161ae08745Sheppo static int vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred); 1171ae08745Sheppo static int vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred); 1181ae08745Sheppo 1191ae08745Sheppo static int vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, 1201ae08745Sheppo void *arg, void **resultp); 1211ae08745Sheppo static int vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 1221ae08745Sheppo static int vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 1235b98b509Sachartre static int vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1245b98b509Sachartre int mod_flags, char *name, caddr_t valuep, int *lengthp); 1251ae08745Sheppo 1261ae08745Sheppo /* setup */ 1270d0c8d4bSnarayan static void vdc_min(struct buf *bufp); 1280a55fbb7Slm66018 static int vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen); 1298cd10891Snarayan static int vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr); 1301ae08745Sheppo static int vdc_start_ldc_connection(vdc_t *vdc); 1311ae08745Sheppo static int vdc_create_device_nodes(vdc_t *vdc); 1324bac2208Snarayan static int vdc_create_device_nodes_efi(vdc_t *vdc); 1334bac2208Snarayan static int vdc_create_device_nodes_vtoc(vdc_t *vdc); 134366a92acSlm66018 static void vdc_create_io_kstats(vdc_t *vdc); 135366a92acSlm66018 static void vdc_create_err_kstats(vdc_t *vdc); 136366a92acSlm66018 static void vdc_set_err_kstats(vdc_t *vdc); 137655fd6a9Sachartre static int vdc_get_md_node(dev_info_t *dip, md_t **mdpp, 1388cd10891Snarayan mde_cookie_t *vd_nodep); 1398cd10891Snarayan static int vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep); 1408cd10891Snarayan static void vdc_fini_ports(vdc_t *vdc); 1418cd10891Snarayan static void vdc_switch_server(vdc_t *vdcp); 1420a55fbb7Slm66018 static int vdc_do_ldc_up(vdc_t *vdc); 1438cd10891Snarayan static void vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr); 1441ae08745Sheppo static int vdc_init_descriptor_ring(vdc_t *vdc); 1451ae08745Sheppo static void vdc_destroy_descriptor_ring(vdc_t *vdc); 1464bac2208Snarayan static int vdc_setup_devid(vdc_t *vdc); 147edcc0754Sachartre static void vdc_store_label_efi(vdc_t *, efi_gpt_t *, efi_gpe_t *); 148342440ecSPrasad Singamsetty static void vdc_store_label_vtoc(vdc_t *, struct dk_geom *, 149342440ecSPrasad Singamsetty struct extvtoc *); 15078fcd0a1Sachartre static void vdc_store_label_unk(vdc_t *vdc); 15178fcd0a1Sachartre static boolean_t vdc_is_opened(vdc_t *vdc); 152de3a5331SRamesh Chitrothu static void vdc_update_size(vdc_t *vdc, size_t, size_t, size_t); 1531ae08745Sheppo 1541ae08745Sheppo /* handshake with vds */ 1550a55fbb7Slm66018 static int vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver); 1563af08d82Slm66018 static int vdc_ver_negotiation(vdc_t *vdcp); 1571ae08745Sheppo static int vdc_init_attr_negotiation(vdc_t *vdc); 1583af08d82Slm66018 static int vdc_attr_negotiation(vdc_t *vdcp); 1591ae08745Sheppo static int vdc_init_dring_negotiate(vdc_t *vdc); 1603af08d82Slm66018 static int vdc_dring_negotiation(vdc_t *vdcp); 1613af08d82Slm66018 static int vdc_send_rdx(vdc_t *vdcp); 1623af08d82Slm66018 static int vdc_rdx_exchange(vdc_t *vdcp); 1630a55fbb7Slm66018 static boolean_t vdc_is_supported_version(vio_ver_msg_t *ver_msg); 1641ae08745Sheppo 1650a55fbb7Slm66018 /* processing incoming messages from vDisk server */ 1661ae08745Sheppo static void vdc_process_msg_thread(vdc_t *vdc); 1673af08d82Slm66018 static int vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp); 1683af08d82Slm66018 1690a55fbb7Slm66018 static uint_t vdc_handle_cb(uint64_t event, caddr_t arg); 1703af08d82Slm66018 static int vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg); 1710a55fbb7Slm66018 static int vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg); 1720a55fbb7Slm66018 static int vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg); 1730a55fbb7Slm66018 static int vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg); 1743af08d82Slm66018 static int vdc_send_request(vdc_t *vdcp, int operation, 1753af08d82Slm66018 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 1763af08d82Slm66018 int cb_type, void *cb_arg, vio_desc_direction_t dir); 1773af08d82Slm66018 static int vdc_map_to_shared_dring(vdc_t *vdcp, int idx); 1783af08d82Slm66018 static int vdc_populate_descriptor(vdc_t *vdcp, int operation, 1793af08d82Slm66018 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 1803af08d82Slm66018 int cb_type, void *cb_arg, vio_desc_direction_t dir); 1812f5224aeSachartre static int vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, 1822f5224aeSachartre size_t nbytes, int slice, diskaddr_t offset, int cb_type, 1832f5224aeSachartre void *cb_arg, vio_desc_direction_t dir, boolean_t); 1843af08d82Slm66018 1853af08d82Slm66018 static int vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp); 186*11f54b6eSAlexandre Chartre static int vdc_drain_response(vdc_t *vdcp, vio_cb_type_t cb_type, 187*11f54b6eSAlexandre Chartre struct buf *buf); 1881ae08745Sheppo static int vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx); 1893af08d82Slm66018 static int vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep); 190e1ebb9ecSlm66018 static int vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg); 1911ae08745Sheppo 1921ae08745Sheppo /* dkio */ 1932f5224aeSachartre static int vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, 1942f5224aeSachartre int *rvalp); 195edcc0754Sachartre static int vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg); 19678fcd0a1Sachartre static void vdc_create_fake_geometry(vdc_t *vdc); 19778fcd0a1Sachartre static int vdc_validate_geometry(vdc_t *vdc); 19878fcd0a1Sachartre static void vdc_validate(vdc_t *vdc); 19978fcd0a1Sachartre static void vdc_validate_task(void *arg); 200d10e4ef2Snarayan static int vdc_null_copy_func(vdc_t *vdc, void *from, void *to, 201d10e4ef2Snarayan int mode, int dir); 2024bac2208Snarayan static int vdc_get_wce_convert(vdc_t *vdc, void *from, void *to, 2034bac2208Snarayan int mode, int dir); 2044bac2208Snarayan static int vdc_set_wce_convert(vdc_t *vdc, void *from, void *to, 2054bac2208Snarayan int mode, int dir); 206d10e4ef2Snarayan static int vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, 207d10e4ef2Snarayan int mode, int dir); 208d10e4ef2Snarayan static int vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, 209d10e4ef2Snarayan int mode, int dir); 210342440ecSPrasad Singamsetty static int vdc_get_extvtoc_convert(vdc_t *vdc, void *from, void *to, 211342440ecSPrasad Singamsetty int mode, int dir); 212342440ecSPrasad Singamsetty static int vdc_set_extvtoc_convert(vdc_t *vdc, void *from, void *to, 213342440ecSPrasad Singamsetty int mode, int dir); 214d10e4ef2Snarayan static int vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, 215d10e4ef2Snarayan int mode, int dir); 216d10e4ef2Snarayan static int vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, 217d10e4ef2Snarayan int mode, int dir); 2184bac2208Snarayan static int vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, 2194bac2208Snarayan int mode, int dir); 2204bac2208Snarayan static int vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, 2214bac2208Snarayan int mode, int dir); 2221ae08745Sheppo 2232f5224aeSachartre static void vdc_ownership_update(vdc_t *vdc, int ownership_flags); 2242f5224aeSachartre static int vdc_access_set(vdc_t *vdc, uint64_t flags, int mode); 2252f5224aeSachartre static vdc_io_t *vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf); 2262f5224aeSachartre static int vdc_failfast_check_resv(vdc_t *vdc); 2272f5224aeSachartre 2281ae08745Sheppo /* 2291ae08745Sheppo * Module variables 2301ae08745Sheppo */ 231e1ebb9ecSlm66018 232e1ebb9ecSlm66018 /* 233e1ebb9ecSlm66018 * Tunable variables to control how long vdc waits before timing out on 234e1ebb9ecSlm66018 * various operations 235e1ebb9ecSlm66018 */ 2363c96341aSnarayan static int vdc_hshake_retries = 3; 237e1ebb9ecSlm66018 238655fd6a9Sachartre static int vdc_timeout = 0; /* units: seconds */ 2398cd10891Snarayan static int vdc_ldcup_timeout = 1; /* units: seconds */ 240655fd6a9Sachartre 2413af08d82Slm66018 static uint64_t vdc_hz_min_ldc_delay; 2423af08d82Slm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC; 2433af08d82Slm66018 static uint64_t vdc_hz_max_ldc_delay; 2443af08d82Slm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC; 2453af08d82Slm66018 2463af08d82Slm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC; 2473af08d82Slm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC; 248e1ebb9ecSlm66018 249e1ebb9ecSlm66018 /* values for dumping - need to run in a tighter loop */ 250e1ebb9ecSlm66018 static uint64_t vdc_usec_timeout_dump = 100 * MILLISEC; /* 0.1s units: ns */ 251e1ebb9ecSlm66018 static int vdc_dump_retries = 100; 252e1ebb9ecSlm66018 2532f5224aeSachartre static uint16_t vdc_scsi_timeout = 60; /* 60s units: seconds */ 2542f5224aeSachartre 2552f5224aeSachartre static uint64_t vdc_ownership_delay = 6 * MICROSEC; /* 6s units: usec */ 2562f5224aeSachartre 257e1ebb9ecSlm66018 /* Count of the number of vdc instances attached */ 258e1ebb9ecSlm66018 static volatile uint32_t vdc_instance_count = 0; 2591ae08745Sheppo 2602f5224aeSachartre /* Tunable to log all SCSI errors */ 2612f5224aeSachartre static boolean_t vdc_scsi_log_error = B_FALSE; 2622f5224aeSachartre 2631ae08745Sheppo /* Soft state pointer */ 2641ae08745Sheppo static void *vdc_state; 2651ae08745Sheppo 2663af08d82Slm66018 /* 2673af08d82Slm66018 * Controlling the verbosity of the error/debug messages 2683af08d82Slm66018 * 2693af08d82Slm66018 * vdc_msglevel - controls level of messages 2703af08d82Slm66018 * vdc_matchinst - 64-bit variable where each bit corresponds 2713af08d82Slm66018 * to the vdc instance the vdc_msglevel applies. 2723af08d82Slm66018 */ 2733af08d82Slm66018 int vdc_msglevel = 0x0; 2743af08d82Slm66018 uint64_t vdc_matchinst = 0ull; 2751ae08745Sheppo 2760a55fbb7Slm66018 /* 2770a55fbb7Slm66018 * Supported vDisk protocol version pairs. 2780a55fbb7Slm66018 * 2790a55fbb7Slm66018 * The first array entry is the latest and preferred version. 2800a55fbb7Slm66018 */ 28117cadca8Slm66018 static const vio_ver_t vdc_version[] = {{1, 1}}; 2821ae08745Sheppo 2831ae08745Sheppo static struct cb_ops vdc_cb_ops = { 2841ae08745Sheppo vdc_open, /* cb_open */ 2851ae08745Sheppo vdc_close, /* cb_close */ 2861ae08745Sheppo vdc_strategy, /* cb_strategy */ 2871ae08745Sheppo vdc_print, /* cb_print */ 2881ae08745Sheppo vdc_dump, /* cb_dump */ 2891ae08745Sheppo vdc_read, /* cb_read */ 2901ae08745Sheppo vdc_write, /* cb_write */ 2911ae08745Sheppo vdc_ioctl, /* cb_ioctl */ 2921ae08745Sheppo nodev, /* cb_devmap */ 2931ae08745Sheppo nodev, /* cb_mmap */ 2941ae08745Sheppo nodev, /* cb_segmap */ 2951ae08745Sheppo nochpoll, /* cb_chpoll */ 2965b98b509Sachartre vdc_prop_op, /* cb_prop_op */ 2971ae08745Sheppo NULL, /* cb_str */ 2981ae08745Sheppo D_MP | D_64BIT, /* cb_flag */ 2991ae08745Sheppo CB_REV, /* cb_rev */ 3001ae08745Sheppo vdc_aread, /* cb_aread */ 3011ae08745Sheppo vdc_awrite /* cb_awrite */ 3021ae08745Sheppo }; 3031ae08745Sheppo 3041ae08745Sheppo static struct dev_ops vdc_ops = { 3051ae08745Sheppo DEVO_REV, /* devo_rev */ 3061ae08745Sheppo 0, /* devo_refcnt */ 3071ae08745Sheppo vdc_getinfo, /* devo_getinfo */ 3081ae08745Sheppo nulldev, /* devo_identify */ 3091ae08745Sheppo nulldev, /* devo_probe */ 3101ae08745Sheppo vdc_attach, /* devo_attach */ 3111ae08745Sheppo vdc_detach, /* devo_detach */ 3121ae08745Sheppo nodev, /* devo_reset */ 3131ae08745Sheppo &vdc_cb_ops, /* devo_cb_ops */ 3141ae08745Sheppo NULL, /* devo_bus_ops */ 31519397407SSherry Moore nulldev, /* devo_power */ 31619397407SSherry Moore ddi_quiesce_not_needed, /* devo_quiesce */ 3171ae08745Sheppo }; 3181ae08745Sheppo 3191ae08745Sheppo static struct modldrv modldrv = { 3201ae08745Sheppo &mod_driverops, 321205eeb1aSlm66018 "virtual disk client", 3221ae08745Sheppo &vdc_ops, 3231ae08745Sheppo }; 3241ae08745Sheppo 3251ae08745Sheppo static struct modlinkage modlinkage = { 3261ae08745Sheppo MODREV_1, 3271ae08745Sheppo &modldrv, 3281ae08745Sheppo NULL 3291ae08745Sheppo }; 3301ae08745Sheppo 3311ae08745Sheppo /* -------------------------------------------------------------------------- */ 3321ae08745Sheppo 3331ae08745Sheppo /* 3341ae08745Sheppo * Device Driver housekeeping and setup 3351ae08745Sheppo */ 3361ae08745Sheppo 3371ae08745Sheppo int 3381ae08745Sheppo _init(void) 3391ae08745Sheppo { 3401ae08745Sheppo int status; 3411ae08745Sheppo 3421ae08745Sheppo if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0) 3431ae08745Sheppo return (status); 3441ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) 3451ae08745Sheppo ddi_soft_state_fini(&vdc_state); 3461ae08745Sheppo return (status); 3471ae08745Sheppo } 3481ae08745Sheppo 3491ae08745Sheppo int 3501ae08745Sheppo _info(struct modinfo *modinfop) 3511ae08745Sheppo { 3521ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 3531ae08745Sheppo } 3541ae08745Sheppo 3551ae08745Sheppo int 3561ae08745Sheppo _fini(void) 3571ae08745Sheppo { 3581ae08745Sheppo int status; 3591ae08745Sheppo 3601ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 3611ae08745Sheppo return (status); 3621ae08745Sheppo ddi_soft_state_fini(&vdc_state); 3631ae08745Sheppo return (0); 3641ae08745Sheppo } 3651ae08745Sheppo 3661ae08745Sheppo static int 3671ae08745Sheppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 3681ae08745Sheppo { 3691ae08745Sheppo _NOTE(ARGUNUSED(dip)) 3701ae08745Sheppo 3710d0c8d4bSnarayan int instance = VDCUNIT((dev_t)arg); 3721ae08745Sheppo vdc_t *vdc = NULL; 3731ae08745Sheppo 3741ae08745Sheppo switch (cmd) { 3751ae08745Sheppo case DDI_INFO_DEVT2DEVINFO: 3761ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 3771ae08745Sheppo *resultp = NULL; 3781ae08745Sheppo return (DDI_FAILURE); 3791ae08745Sheppo } 3801ae08745Sheppo *resultp = vdc->dip; 3811ae08745Sheppo return (DDI_SUCCESS); 3821ae08745Sheppo case DDI_INFO_DEVT2INSTANCE: 3831ae08745Sheppo *resultp = (void *)(uintptr_t)instance; 3841ae08745Sheppo return (DDI_SUCCESS); 3851ae08745Sheppo default: 3861ae08745Sheppo *resultp = NULL; 3871ae08745Sheppo return (DDI_FAILURE); 3881ae08745Sheppo } 3891ae08745Sheppo } 3901ae08745Sheppo 3911ae08745Sheppo static int 3921ae08745Sheppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3931ae08745Sheppo { 3942f5224aeSachartre kt_did_t failfast_tid, ownership_tid; 3951ae08745Sheppo int instance; 3961ae08745Sheppo int rv; 397d7400d00Sachartre vdc_server_t *srvr; 3981ae08745Sheppo vdc_t *vdc = NULL; 3991ae08745Sheppo 4001ae08745Sheppo switch (cmd) { 4011ae08745Sheppo case DDI_DETACH: 4021ae08745Sheppo /* the real work happens below */ 4031ae08745Sheppo break; 4041ae08745Sheppo case DDI_SUSPEND: 4051ae08745Sheppo /* nothing to do for this non-device */ 4061ae08745Sheppo return (DDI_SUCCESS); 4071ae08745Sheppo default: 4081ae08745Sheppo return (DDI_FAILURE); 4091ae08745Sheppo } 4101ae08745Sheppo 4111ae08745Sheppo ASSERT(cmd == DDI_DETACH); 4121ae08745Sheppo instance = ddi_get_instance(dip); 4133af08d82Slm66018 DMSGX(1, "[%d] Entered\n", instance); 4141ae08745Sheppo 4151ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 416e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 4171ae08745Sheppo return (DDI_FAILURE); 4181ae08745Sheppo } 4191ae08745Sheppo 4202f5224aeSachartre /* 4212f5224aeSachartre * This function is called when vdc is detached or if it has failed to 4222f5224aeSachartre * attach. In that case, the attach may have fail before the vdisk type 4232f5224aeSachartre * has been set so we can't call vdc_is_opened(). However as the attach 4242f5224aeSachartre * has failed, we know that the vdisk is not opened and we can safely 4252f5224aeSachartre * detach. 4262f5224aeSachartre */ 4272f5224aeSachartre if (vdc->vdisk_type != VD_DISK_TYPE_UNK && vdc_is_opened(vdc)) { 4283af08d82Slm66018 DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance); 4291ae08745Sheppo return (DDI_FAILURE); 4301ae08745Sheppo } 4311ae08745Sheppo 43278fcd0a1Sachartre if (vdc->dkio_flush_pending) { 43378fcd0a1Sachartre DMSG(vdc, 0, 43478fcd0a1Sachartre "[%d] Cannot detach: %d outstanding DKIO flushes\n", 43578fcd0a1Sachartre instance, vdc->dkio_flush_pending); 43678fcd0a1Sachartre return (DDI_FAILURE); 43778fcd0a1Sachartre } 43878fcd0a1Sachartre 43978fcd0a1Sachartre if (vdc->validate_pending) { 44078fcd0a1Sachartre DMSG(vdc, 0, 44178fcd0a1Sachartre "[%d] Cannot detach: %d outstanding validate request\n", 44278fcd0a1Sachartre instance, vdc->validate_pending); 44378fcd0a1Sachartre return (DDI_FAILURE); 44478fcd0a1Sachartre } 44578fcd0a1Sachartre 4463af08d82Slm66018 DMSG(vdc, 0, "[%d] proceeding...\n", instance); 4473af08d82Slm66018 4482f5224aeSachartre /* If we took ownership, release ownership */ 4492f5224aeSachartre mutex_enter(&vdc->ownership_lock); 4502f5224aeSachartre if (vdc->ownership & VDC_OWNERSHIP_GRANTED) { 4512f5224aeSachartre rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, FKIOCTL); 4522f5224aeSachartre if (rv == 0) { 4532f5224aeSachartre vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE); 4542f5224aeSachartre } 4552f5224aeSachartre } 4562f5224aeSachartre mutex_exit(&vdc->ownership_lock); 4572f5224aeSachartre 4583af08d82Slm66018 /* mark instance as detaching */ 4593af08d82Slm66018 vdc->lifecycle = VDC_LC_DETACHING; 4601ae08745Sheppo 4611ae08745Sheppo /* 462d7400d00Sachartre * Try and disable callbacks to prevent another handshake. We have to 463d7400d00Sachartre * disable callbacks for all servers. 4641ae08745Sheppo */ 465d7400d00Sachartre for (srvr = vdc->server_list; srvr != NULL; srvr = srvr->next) { 466d7400d00Sachartre rv = ldc_set_cb_mode(srvr->ldc_handle, LDC_CB_DISABLE); 467d7400d00Sachartre DMSG(vdc, 0, "callback disabled (ldc=%lu, rv=%d)\n", 468d7400d00Sachartre srvr->ldc_id, rv); 4698cd10891Snarayan } 4701ae08745Sheppo 4711ae08745Sheppo if (vdc->initialized & VDC_THREAD) { 4723af08d82Slm66018 mutex_enter(&vdc->read_lock); 4733af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 4743af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) { 4753af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 4763af08d82Slm66018 cv_signal(&vdc->read_cv); 4771ae08745Sheppo } 4783af08d82Slm66018 4793af08d82Slm66018 mutex_exit(&vdc->read_lock); 4803af08d82Slm66018 4813af08d82Slm66018 /* wake up any thread waiting for connection to come online */ 4823af08d82Slm66018 mutex_enter(&vdc->lock); 4833af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 4843af08d82Slm66018 DMSG(vdc, 0, 4853af08d82Slm66018 "[%d] write reset - move to resetting state...\n", 4863af08d82Slm66018 instance); 4873af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 4883af08d82Slm66018 cv_signal(&vdc->initwait_cv); 4893af08d82Slm66018 } 4903af08d82Slm66018 mutex_exit(&vdc->lock); 4913af08d82Slm66018 4923af08d82Slm66018 /* now wait until state transitions to VDC_STATE_DETACH */ 4933af08d82Slm66018 thread_join(vdc->msg_proc_thr->t_did); 4943af08d82Slm66018 ASSERT(vdc->state == VDC_STATE_DETACH); 4953af08d82Slm66018 DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n", 4963af08d82Slm66018 vdc->instance); 4971ae08745Sheppo } 4981ae08745Sheppo 4991ae08745Sheppo mutex_enter(&vdc->lock); 5001ae08745Sheppo 5011ae08745Sheppo if (vdc->initialized & VDC_DRING) 5021ae08745Sheppo vdc_destroy_descriptor_ring(vdc); 5031ae08745Sheppo 5048cd10891Snarayan vdc_fini_ports(vdc); 5051ae08745Sheppo 5062f5224aeSachartre if (vdc->failfast_thread) { 5072f5224aeSachartre failfast_tid = vdc->failfast_thread->t_did; 5082f5224aeSachartre vdc->failfast_interval = 0; 5092f5224aeSachartre cv_signal(&vdc->failfast_cv); 5102f5224aeSachartre } else { 5112f5224aeSachartre failfast_tid = 0; 5122f5224aeSachartre } 5132f5224aeSachartre 5142f5224aeSachartre if (vdc->ownership & VDC_OWNERSHIP_WANTED) { 5152f5224aeSachartre ownership_tid = vdc->ownership_thread->t_did; 5162f5224aeSachartre vdc->ownership = VDC_OWNERSHIP_NONE; 5172f5224aeSachartre cv_signal(&vdc->ownership_cv); 5182f5224aeSachartre } else { 5192f5224aeSachartre ownership_tid = 0; 5202f5224aeSachartre } 5212f5224aeSachartre 5221ae08745Sheppo mutex_exit(&vdc->lock); 5231ae08745Sheppo 5242f5224aeSachartre if (failfast_tid != 0) 5252f5224aeSachartre thread_join(failfast_tid); 5262f5224aeSachartre 5272f5224aeSachartre if (ownership_tid != 0) 5282f5224aeSachartre thread_join(ownership_tid); 5292f5224aeSachartre 5305b98b509Sachartre if (vdc->initialized & VDC_MINOR) 5311ae08745Sheppo ddi_remove_minor_node(dip, NULL); 5321ae08745Sheppo 533366a92acSlm66018 if (vdc->io_stats) { 534366a92acSlm66018 kstat_delete(vdc->io_stats); 535366a92acSlm66018 vdc->io_stats = NULL; 536366a92acSlm66018 } 537366a92acSlm66018 538366a92acSlm66018 if (vdc->err_stats) { 539366a92acSlm66018 kstat_delete(vdc->err_stats); 540366a92acSlm66018 vdc->err_stats = NULL; 541366a92acSlm66018 } 542366a92acSlm66018 5431ae08745Sheppo if (vdc->initialized & VDC_LOCKS) { 5441ae08745Sheppo mutex_destroy(&vdc->lock); 5453af08d82Slm66018 mutex_destroy(&vdc->read_lock); 5462f5224aeSachartre mutex_destroy(&vdc->ownership_lock); 5473af08d82Slm66018 cv_destroy(&vdc->initwait_cv); 5483af08d82Slm66018 cv_destroy(&vdc->dring_free_cv); 5493af08d82Slm66018 cv_destroy(&vdc->membind_cv); 5503af08d82Slm66018 cv_destroy(&vdc->sync_pending_cv); 5513af08d82Slm66018 cv_destroy(&vdc->sync_blocked_cv); 5523af08d82Slm66018 cv_destroy(&vdc->read_cv); 5533af08d82Slm66018 cv_destroy(&vdc->running_cv); 5542f5224aeSachartre cv_destroy(&vdc->ownership_cv); 5552f5224aeSachartre cv_destroy(&vdc->failfast_cv); 5562f5224aeSachartre cv_destroy(&vdc->failfast_io_cv); 5571ae08745Sheppo } 5581ae08745Sheppo 5591ae08745Sheppo if (vdc->minfo) 5601ae08745Sheppo kmem_free(vdc->minfo, sizeof (struct dk_minfo)); 5611ae08745Sheppo 5621ae08745Sheppo if (vdc->cinfo) 5631ae08745Sheppo kmem_free(vdc->cinfo, sizeof (struct dk_cinfo)); 5641ae08745Sheppo 5651ae08745Sheppo if (vdc->vtoc) 566342440ecSPrasad Singamsetty kmem_free(vdc->vtoc, sizeof (struct extvtoc)); 5671ae08745Sheppo 56878fcd0a1Sachartre if (vdc->geom) 56978fcd0a1Sachartre kmem_free(vdc->geom, sizeof (struct dk_geom)); 5700a55fbb7Slm66018 5714bac2208Snarayan if (vdc->devid) { 5724bac2208Snarayan ddi_devid_unregister(dip); 5734bac2208Snarayan ddi_devid_free(vdc->devid); 5744bac2208Snarayan } 5754bac2208Snarayan 5761ae08745Sheppo if (vdc->initialized & VDC_SOFT_STATE) 5771ae08745Sheppo ddi_soft_state_free(vdc_state, instance); 5781ae08745Sheppo 5793af08d82Slm66018 DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc); 5801ae08745Sheppo 5811ae08745Sheppo return (DDI_SUCCESS); 5821ae08745Sheppo } 5831ae08745Sheppo 5841ae08745Sheppo 5851ae08745Sheppo static int 5861ae08745Sheppo vdc_do_attach(dev_info_t *dip) 5871ae08745Sheppo { 5881ae08745Sheppo int instance; 5891ae08745Sheppo vdc_t *vdc = NULL; 5901ae08745Sheppo int status; 591655fd6a9Sachartre md_t *mdp; 5928cd10891Snarayan mde_cookie_t vd_node; 5931ae08745Sheppo 5941ae08745Sheppo ASSERT(dip != NULL); 5951ae08745Sheppo 5961ae08745Sheppo instance = ddi_get_instance(dip); 5971ae08745Sheppo if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) { 598e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure", 599e1ebb9ecSlm66018 instance); 6001ae08745Sheppo return (DDI_FAILURE); 6011ae08745Sheppo } 6021ae08745Sheppo 6031ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 604e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 6051ae08745Sheppo return (DDI_FAILURE); 6061ae08745Sheppo } 6071ae08745Sheppo 6081ae08745Sheppo /* 6091ae08745Sheppo * We assign the value to initialized in this case to zero out the 6101ae08745Sheppo * variable and then set bits in it to indicate what has been done 6111ae08745Sheppo */ 6121ae08745Sheppo vdc->initialized = VDC_SOFT_STATE; 6131ae08745Sheppo 6143af08d82Slm66018 vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc); 6153af08d82Slm66018 vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc); 6161ae08745Sheppo 6171ae08745Sheppo vdc->dip = dip; 6181ae08745Sheppo vdc->instance = instance; 6191ae08745Sheppo vdc->vdisk_type = VD_DISK_TYPE_UNK; 6204bac2208Snarayan vdc->vdisk_label = VD_DISK_LABEL_UNK; 6213af08d82Slm66018 vdc->state = VDC_STATE_INIT; 6223af08d82Slm66018 vdc->lifecycle = VDC_LC_ATTACHING; 6231ae08745Sheppo vdc->session_id = 0; 6241ae08745Sheppo vdc->block_size = DEV_BSIZE; 6258e6a2a04Slm66018 vdc->max_xfer_sz = maxphys / DEV_BSIZE; 6261ae08745Sheppo 62717cadca8Slm66018 /* 62817cadca8Slm66018 * We assume, for now, that the vDisk server will export 'read' 62917cadca8Slm66018 * operations to us at a minimum (this is needed because of checks 63017cadca8Slm66018 * in vdc for supported operations early in the handshake process). 63117cadca8Slm66018 * The vDisk server will return ENOTSUP if this is not the case. 63217cadca8Slm66018 * The value will be overwritten during the attribute exchange with 63317cadca8Slm66018 * the bitmask of operations exported by server. 63417cadca8Slm66018 */ 63517cadca8Slm66018 vdc->operations = VD_OP_MASK_READ; 63617cadca8Slm66018 6371ae08745Sheppo vdc->vtoc = NULL; 63878fcd0a1Sachartre vdc->geom = NULL; 6391ae08745Sheppo vdc->cinfo = NULL; 6401ae08745Sheppo vdc->minfo = NULL; 6411ae08745Sheppo 6421ae08745Sheppo mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL); 6433af08d82Slm66018 cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL); 6443af08d82Slm66018 cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL); 6453af08d82Slm66018 cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL); 6463af08d82Slm66018 cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL); 6473af08d82Slm66018 6483af08d82Slm66018 vdc->threads_pending = 0; 6493af08d82Slm66018 vdc->sync_op_pending = B_FALSE; 6503af08d82Slm66018 vdc->sync_op_blocked = B_FALSE; 6513af08d82Slm66018 cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL); 6523af08d82Slm66018 cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL); 6533af08d82Slm66018 6542f5224aeSachartre mutex_init(&vdc->ownership_lock, NULL, MUTEX_DRIVER, NULL); 6552f5224aeSachartre cv_init(&vdc->ownership_cv, NULL, CV_DRIVER, NULL); 6562f5224aeSachartre cv_init(&vdc->failfast_cv, NULL, CV_DRIVER, NULL); 6572f5224aeSachartre cv_init(&vdc->failfast_io_cv, NULL, CV_DRIVER, NULL); 6582f5224aeSachartre 6593af08d82Slm66018 /* init blocking msg read functionality */ 6603af08d82Slm66018 mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL); 6613af08d82Slm66018 cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL); 6623af08d82Slm66018 vdc->read_state = VDC_READ_IDLE; 6633af08d82Slm66018 6641ae08745Sheppo vdc->initialized |= VDC_LOCKS; 6651ae08745Sheppo 666655fd6a9Sachartre /* get device and port MD node for this disk instance */ 6678cd10891Snarayan if (vdc_get_md_node(dip, &mdp, &vd_node) != 0) { 668655fd6a9Sachartre cmn_err(CE_NOTE, "[%d] Could not get machine description node", 669655fd6a9Sachartre instance); 670655fd6a9Sachartre return (DDI_FAILURE); 671655fd6a9Sachartre } 672655fd6a9Sachartre 6738cd10891Snarayan if (vdc_init_ports(vdc, mdp, vd_node) != 0) { 6748cd10891Snarayan cmn_err(CE_NOTE, "[%d] Error initialising ports", instance); 6758cd10891Snarayan return (DDI_FAILURE); 676655fd6a9Sachartre } 677655fd6a9Sachartre 678655fd6a9Sachartre (void) md_fini_handle(mdp); 679655fd6a9Sachartre 680de3a5331SRamesh Chitrothu /* Create the kstats for saving the I/O statistics used by iostat(1M) */ 681de3a5331SRamesh Chitrothu vdc_create_io_kstats(vdc); 682de3a5331SRamesh Chitrothu vdc_create_err_kstats(vdc); 683de3a5331SRamesh Chitrothu 684de3a5331SRamesh Chitrothu /* Initialize remaining structures before starting the msg thread */ 685de3a5331SRamesh Chitrothu vdc->vdisk_label = VD_DISK_LABEL_UNK; 686342440ecSPrasad Singamsetty vdc->vtoc = kmem_zalloc(sizeof (struct extvtoc), KM_SLEEP); 687de3a5331SRamesh Chitrothu vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 688de3a5331SRamesh Chitrothu vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP); 689de3a5331SRamesh Chitrothu 6903af08d82Slm66018 /* initialize the thread responsible for managing state with server */ 6913af08d82Slm66018 vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread, 6921ae08745Sheppo vdc, 0, &p0, TS_RUN, minclsyspri); 6933af08d82Slm66018 if (vdc->msg_proc_thr == NULL) { 6941ae08745Sheppo cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread", 6951ae08745Sheppo instance); 6961ae08745Sheppo return (DDI_FAILURE); 6971ae08745Sheppo } 6983af08d82Slm66018 6991ae08745Sheppo vdc->initialized |= VDC_THREAD; 7001ae08745Sheppo 701e1ebb9ecSlm66018 atomic_inc_32(&vdc_instance_count); 7021ae08745Sheppo 7030a55fbb7Slm66018 /* 70478fcd0a1Sachartre * Check the disk label. This will send requests and do the handshake. 70578fcd0a1Sachartre * We don't really care about the disk label now. What we really need is 70678fcd0a1Sachartre * the handshake do be done so that we know the type of the disk (slice 70778fcd0a1Sachartre * or full disk) and the appropriate device nodes can be created. 7080a55fbb7Slm66018 */ 70978fcd0a1Sachartre 71078fcd0a1Sachartre mutex_enter(&vdc->lock); 71178fcd0a1Sachartre (void) vdc_validate_geometry(vdc); 71278fcd0a1Sachartre mutex_exit(&vdc->lock); 7131ae08745Sheppo 7141ae08745Sheppo /* 7155b98b509Sachartre * Now that we have the device info we can create the device nodes 7161ae08745Sheppo */ 7171ae08745Sheppo status = vdc_create_device_nodes(vdc); 7181ae08745Sheppo if (status) { 7193af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to create device nodes", 7201ae08745Sheppo instance); 7213af08d82Slm66018 goto return_status; 7221ae08745Sheppo } 7231ae08745Sheppo 7244bac2208Snarayan /* 7254bac2208Snarayan * Setup devid 7264bac2208Snarayan */ 7274bac2208Snarayan if (vdc_setup_devid(vdc)) { 7283af08d82Slm66018 DMSG(vdc, 0, "[%d] No device id available\n", instance); 7294bac2208Snarayan } 7304bac2208Snarayan 731366a92acSlm66018 /* 732366a92acSlm66018 * Fill in the fields of the error statistics kstat that were not 733366a92acSlm66018 * available when creating the kstat 734366a92acSlm66018 */ 735366a92acSlm66018 vdc_set_err_kstats(vdc); 736366a92acSlm66018 7371ae08745Sheppo ddi_report_dev(dip); 7383af08d82Slm66018 vdc->lifecycle = VDC_LC_ONLINE; 7393af08d82Slm66018 DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance); 7401ae08745Sheppo 7413af08d82Slm66018 return_status: 7423af08d82Slm66018 DMSG(vdc, 0, "[%d] Attach completed\n", instance); 7431ae08745Sheppo return (status); 7441ae08745Sheppo } 7451ae08745Sheppo 7461ae08745Sheppo static int 7471ae08745Sheppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 7481ae08745Sheppo { 7491ae08745Sheppo int status; 7501ae08745Sheppo 7511ae08745Sheppo switch (cmd) { 7521ae08745Sheppo case DDI_ATTACH: 7531ae08745Sheppo if ((status = vdc_do_attach(dip)) != 0) 7541ae08745Sheppo (void) vdc_detach(dip, DDI_DETACH); 7551ae08745Sheppo return (status); 7561ae08745Sheppo case DDI_RESUME: 7571ae08745Sheppo /* nothing to do for this non-device */ 7581ae08745Sheppo return (DDI_SUCCESS); 7591ae08745Sheppo default: 7601ae08745Sheppo return (DDI_FAILURE); 7611ae08745Sheppo } 7621ae08745Sheppo } 7631ae08745Sheppo 7641ae08745Sheppo static int 7658cd10891Snarayan vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr) 7661ae08745Sheppo { 7671ae08745Sheppo int status = 0; 7681ae08745Sheppo ldc_status_t ldc_state; 7691ae08745Sheppo ldc_attr_t ldc_attr; 7701ae08745Sheppo 7711ae08745Sheppo ASSERT(vdc != NULL); 7728cd10891Snarayan ASSERT(srvr != NULL); 7731ae08745Sheppo 7741ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK; 7751ae08745Sheppo ldc_attr.instance = vdc->instance; 7761ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; /* unreliable transport */ 777e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 7781ae08745Sheppo 7798cd10891Snarayan if ((srvr->state & VDC_LDC_INIT) == 0) { 7808cd10891Snarayan status = ldc_init(srvr->ldc_id, &ldc_attr, 7818cd10891Snarayan &srvr->ldc_handle); 7821ae08745Sheppo if (status != 0) { 7833af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d", 7848cd10891Snarayan vdc->instance, srvr->ldc_id, status); 7851ae08745Sheppo return (status); 7861ae08745Sheppo } 7878cd10891Snarayan srvr->state |= VDC_LDC_INIT; 7881ae08745Sheppo } 7898cd10891Snarayan status = ldc_status(srvr->ldc_handle, &ldc_state); 7901ae08745Sheppo if (status != 0) { 7913af08d82Slm66018 DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]", 792e1ebb9ecSlm66018 vdc->instance, status); 7938cd10891Snarayan goto init_exit; 7941ae08745Sheppo } 7958cd10891Snarayan srvr->ldc_state = ldc_state; 7961ae08745Sheppo 7978cd10891Snarayan if ((srvr->state & VDC_LDC_CB) == 0) { 7988cd10891Snarayan status = ldc_reg_callback(srvr->ldc_handle, vdc_handle_cb, 7998cd10891Snarayan (caddr_t)srvr); 8001ae08745Sheppo if (status != 0) { 8013af08d82Slm66018 DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)", 802e1ebb9ecSlm66018 vdc->instance, status); 8038cd10891Snarayan goto init_exit; 8041ae08745Sheppo } 8058cd10891Snarayan srvr->state |= VDC_LDC_CB; 8061ae08745Sheppo } 8071ae08745Sheppo 8081ae08745Sheppo /* 8091ae08745Sheppo * At this stage we have initialised LDC, we will now try and open 8101ae08745Sheppo * the connection. 8111ae08745Sheppo */ 8128cd10891Snarayan if (srvr->ldc_state == LDC_INIT) { 8138cd10891Snarayan status = ldc_open(srvr->ldc_handle); 8141ae08745Sheppo if (status != 0) { 8153af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d", 8168cd10891Snarayan vdc->instance, srvr->ldc_id, status); 8178cd10891Snarayan goto init_exit; 8181ae08745Sheppo } 8198cd10891Snarayan srvr->state |= VDC_LDC_OPEN; 8208cd10891Snarayan } 8218cd10891Snarayan 8228cd10891Snarayan init_exit: 8238cd10891Snarayan if (status) { 8248cd10891Snarayan vdc_terminate_ldc(vdc, srvr); 8251ae08745Sheppo } 8261ae08745Sheppo 8271ae08745Sheppo return (status); 8281ae08745Sheppo } 8291ae08745Sheppo 8301ae08745Sheppo static int 8311ae08745Sheppo vdc_start_ldc_connection(vdc_t *vdc) 8321ae08745Sheppo { 8331ae08745Sheppo int status = 0; 8341ae08745Sheppo 8351ae08745Sheppo ASSERT(vdc != NULL); 8361ae08745Sheppo 8373af08d82Slm66018 ASSERT(MUTEX_HELD(&vdc->lock)); 8381ae08745Sheppo 8390a55fbb7Slm66018 status = vdc_do_ldc_up(vdc); 8401ae08745Sheppo 8413af08d82Slm66018 DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance); 8421ae08745Sheppo 8433af08d82Slm66018 return (status); 8443af08d82Slm66018 } 8453af08d82Slm66018 8463af08d82Slm66018 static int 8473af08d82Slm66018 vdc_stop_ldc_connection(vdc_t *vdcp) 8483af08d82Slm66018 { 8493af08d82Slm66018 int status; 8503af08d82Slm66018 8518cd10891Snarayan ASSERT(vdcp != NULL); 8528cd10891Snarayan 8538cd10891Snarayan ASSERT(MUTEX_HELD(&vdcp->lock)); 8548cd10891Snarayan 8553af08d82Slm66018 DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n", 8563af08d82Slm66018 vdcp->state); 8573af08d82Slm66018 8588cd10891Snarayan status = ldc_down(vdcp->curr_server->ldc_handle); 8593af08d82Slm66018 DMSG(vdcp, 0, "ldc_down() = %d\n", status); 8603af08d82Slm66018 8613af08d82Slm66018 vdcp->initialized &= ~VDC_HANDSHAKE; 8623af08d82Slm66018 DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized); 8631ae08745Sheppo 8641ae08745Sheppo return (status); 8651ae08745Sheppo } 8661ae08745Sheppo 867366a92acSlm66018 static void 868366a92acSlm66018 vdc_create_io_kstats(vdc_t *vdc) 869366a92acSlm66018 { 870366a92acSlm66018 if (vdc->io_stats != NULL) { 871366a92acSlm66018 DMSG(vdc, 0, "[%d] I/O kstat already exists\n", vdc->instance); 872366a92acSlm66018 return; 873366a92acSlm66018 } 874366a92acSlm66018 875366a92acSlm66018 vdc->io_stats = kstat_create(VDC_DRIVER_NAME, vdc->instance, NULL, 876366a92acSlm66018 "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 877366a92acSlm66018 if (vdc->io_stats != NULL) { 878366a92acSlm66018 vdc->io_stats->ks_lock = &vdc->lock; 879366a92acSlm66018 kstat_install(vdc->io_stats); 880366a92acSlm66018 } else { 881366a92acSlm66018 cmn_err(CE_NOTE, "[%d] Failed to create kstat: I/O statistics" 882366a92acSlm66018 " will not be gathered", vdc->instance); 883366a92acSlm66018 } 884366a92acSlm66018 } 885366a92acSlm66018 886366a92acSlm66018 static void 887366a92acSlm66018 vdc_create_err_kstats(vdc_t *vdc) 888366a92acSlm66018 { 889366a92acSlm66018 vd_err_stats_t *stp; 890366a92acSlm66018 char kstatmodule_err[KSTAT_STRLEN]; 891366a92acSlm66018 char kstatname[KSTAT_STRLEN]; 892366a92acSlm66018 int ndata = (sizeof (vd_err_stats_t) / sizeof (kstat_named_t)); 893366a92acSlm66018 int instance = vdc->instance; 894366a92acSlm66018 895366a92acSlm66018 if (vdc->err_stats != NULL) { 896366a92acSlm66018 DMSG(vdc, 0, "[%d] ERR kstat already exists\n", vdc->instance); 897366a92acSlm66018 return; 898366a92acSlm66018 } 899366a92acSlm66018 900366a92acSlm66018 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 901366a92acSlm66018 "%serr", VDC_DRIVER_NAME); 902366a92acSlm66018 (void) snprintf(kstatname, sizeof (kstatname), 903366a92acSlm66018 "%s%d,err", VDC_DRIVER_NAME, instance); 904366a92acSlm66018 905366a92acSlm66018 vdc->err_stats = kstat_create(kstatmodule_err, instance, kstatname, 906366a92acSlm66018 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 907366a92acSlm66018 908366a92acSlm66018 if (vdc->err_stats == NULL) { 909366a92acSlm66018 cmn_err(CE_NOTE, "[%d] Failed to create kstat: Error statistics" 910366a92acSlm66018 " will not be gathered", instance); 911366a92acSlm66018 return; 912366a92acSlm66018 } 913366a92acSlm66018 914366a92acSlm66018 stp = (vd_err_stats_t *)vdc->err_stats->ks_data; 915366a92acSlm66018 kstat_named_init(&stp->vd_softerrs, "Soft Errors", 916366a92acSlm66018 KSTAT_DATA_UINT32); 917366a92acSlm66018 kstat_named_init(&stp->vd_transerrs, "Transport Errors", 918366a92acSlm66018 KSTAT_DATA_UINT32); 919366a92acSlm66018 kstat_named_init(&stp->vd_protoerrs, "Protocol Errors", 920366a92acSlm66018 KSTAT_DATA_UINT32); 921366a92acSlm66018 kstat_named_init(&stp->vd_vid, "Vendor", 922366a92acSlm66018 KSTAT_DATA_CHAR); 923366a92acSlm66018 kstat_named_init(&stp->vd_pid, "Product", 924366a92acSlm66018 KSTAT_DATA_CHAR); 925366a92acSlm66018 kstat_named_init(&stp->vd_capacity, "Size", 926366a92acSlm66018 KSTAT_DATA_ULONGLONG); 927366a92acSlm66018 928366a92acSlm66018 vdc->err_stats->ks_update = nulldev; 929366a92acSlm66018 930366a92acSlm66018 kstat_install(vdc->err_stats); 931366a92acSlm66018 } 932366a92acSlm66018 933366a92acSlm66018 static void 934366a92acSlm66018 vdc_set_err_kstats(vdc_t *vdc) 935366a92acSlm66018 { 936366a92acSlm66018 vd_err_stats_t *stp; 937366a92acSlm66018 938366a92acSlm66018 if (vdc->err_stats == NULL) 939366a92acSlm66018 return; 940366a92acSlm66018 941366a92acSlm66018 mutex_enter(&vdc->lock); 942366a92acSlm66018 943366a92acSlm66018 stp = (vd_err_stats_t *)vdc->err_stats->ks_data; 944366a92acSlm66018 ASSERT(stp != NULL); 945366a92acSlm66018 946366a92acSlm66018 stp->vd_capacity.value.ui64 = vdc->vdisk_size * vdc->block_size; 947366a92acSlm66018 (void) strcpy(stp->vd_vid.value.c, "SUN"); 948366a92acSlm66018 (void) strcpy(stp->vd_pid.value.c, "VDSK"); 949366a92acSlm66018 950366a92acSlm66018 mutex_exit(&vdc->lock); 951366a92acSlm66018 } 952366a92acSlm66018 9534bac2208Snarayan static int 9544bac2208Snarayan vdc_create_device_nodes_efi(vdc_t *vdc) 9554bac2208Snarayan { 9564bac2208Snarayan ddi_remove_minor_node(vdc->dip, "h"); 9574bac2208Snarayan ddi_remove_minor_node(vdc->dip, "h,raw"); 9584bac2208Snarayan 9594bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK, 9604bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 9614bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 9624bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'", 9634bac2208Snarayan vdc->instance); 9644bac2208Snarayan return (EIO); 9654bac2208Snarayan } 9664bac2208Snarayan 9674bac2208Snarayan /* if any device node is created we set this flag */ 9684bac2208Snarayan vdc->initialized |= VDC_MINOR; 9694bac2208Snarayan 9704bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR, 9714bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 9724bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 9734bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'", 9744bac2208Snarayan vdc->instance); 9754bac2208Snarayan return (EIO); 9764bac2208Snarayan } 9774bac2208Snarayan 9784bac2208Snarayan return (0); 9794bac2208Snarayan } 9804bac2208Snarayan 9814bac2208Snarayan static int 9824bac2208Snarayan vdc_create_device_nodes_vtoc(vdc_t *vdc) 9834bac2208Snarayan { 9844bac2208Snarayan ddi_remove_minor_node(vdc->dip, "wd"); 9854bac2208Snarayan ddi_remove_minor_node(vdc->dip, "wd,raw"); 9864bac2208Snarayan 9874bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK, 9884bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 9894bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 9904bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'", 9914bac2208Snarayan vdc->instance); 9924bac2208Snarayan return (EIO); 9934bac2208Snarayan } 9944bac2208Snarayan 9954bac2208Snarayan /* if any device node is created we set this flag */ 9964bac2208Snarayan vdc->initialized |= VDC_MINOR; 9974bac2208Snarayan 9984bac2208Snarayan if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR, 9994bac2208Snarayan VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 10004bac2208Snarayan DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 10014bac2208Snarayan cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'", 10024bac2208Snarayan vdc->instance); 10034bac2208Snarayan return (EIO); 10044bac2208Snarayan } 10054bac2208Snarayan 10064bac2208Snarayan return (0); 10074bac2208Snarayan } 10081ae08745Sheppo 10091ae08745Sheppo /* 10101ae08745Sheppo * Function: 10111ae08745Sheppo * vdc_create_device_nodes 10121ae08745Sheppo * 10131ae08745Sheppo * Description: 10141ae08745Sheppo * This function creates the block and character device nodes under 10155b98b509Sachartre * /devices. It is called as part of the attach(9E) of the instance 10165b98b509Sachartre * during the handshake with vds after vds has sent the attributes 10175b98b509Sachartre * to vdc. 10181ae08745Sheppo * 10191ae08745Sheppo * If the device is of type VD_DISK_TYPE_SLICE then the minor node 10201ae08745Sheppo * of 2 is used in keeping with the Solaris convention that slice 2 10211ae08745Sheppo * refers to a whole disk. Slices start at 'a' 10221ae08745Sheppo * 10231ae08745Sheppo * Parameters: 10241ae08745Sheppo * vdc - soft state pointer 10251ae08745Sheppo * 10261ae08745Sheppo * Return Values 10271ae08745Sheppo * 0 - Success 10281ae08745Sheppo * EIO - Failed to create node 10291ae08745Sheppo * EINVAL - Unknown type of disk exported 10301ae08745Sheppo */ 10311ae08745Sheppo static int 10321ae08745Sheppo vdc_create_device_nodes(vdc_t *vdc) 10331ae08745Sheppo { 10344bac2208Snarayan char name[sizeof ("s,raw")]; 10351ae08745Sheppo dev_info_t *dip = NULL; 10364bac2208Snarayan int instance, status; 10371ae08745Sheppo int num_slices = 1; 10381ae08745Sheppo int i; 10391ae08745Sheppo 10401ae08745Sheppo ASSERT(vdc != NULL); 10411ae08745Sheppo 10421ae08745Sheppo instance = vdc->instance; 10431ae08745Sheppo dip = vdc->dip; 10441ae08745Sheppo 10451ae08745Sheppo switch (vdc->vdisk_type) { 10461ae08745Sheppo case VD_DISK_TYPE_DISK: 10471ae08745Sheppo num_slices = V_NUMPAR; 10481ae08745Sheppo break; 10491ae08745Sheppo case VD_DISK_TYPE_SLICE: 10501ae08745Sheppo num_slices = 1; 10511ae08745Sheppo break; 10521ae08745Sheppo case VD_DISK_TYPE_UNK: 10531ae08745Sheppo default: 10541ae08745Sheppo return (EINVAL); 10551ae08745Sheppo } 10561ae08745Sheppo 10574bac2208Snarayan /* 10584bac2208Snarayan * Minor nodes are different for EFI disks: EFI disks do not have 10594bac2208Snarayan * a minor node 'g' for the minor number corresponding to slice 10604bac2208Snarayan * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd' 10614bac2208Snarayan * representing the whole disk. 10624bac2208Snarayan */ 10631ae08745Sheppo for (i = 0; i < num_slices; i++) { 10644bac2208Snarayan 10654bac2208Snarayan if (i == VD_EFI_WD_SLICE) { 10664bac2208Snarayan if (vdc->vdisk_label == VD_DISK_LABEL_EFI) 10674bac2208Snarayan status = vdc_create_device_nodes_efi(vdc); 10684bac2208Snarayan else 10694bac2208Snarayan status = vdc_create_device_nodes_vtoc(vdc); 10704bac2208Snarayan if (status != 0) 10714bac2208Snarayan return (status); 10724bac2208Snarayan continue; 10734bac2208Snarayan } 10744bac2208Snarayan 10751ae08745Sheppo (void) snprintf(name, sizeof (name), "%c", 'a' + i); 10761ae08745Sheppo if (ddi_create_minor_node(dip, name, S_IFBLK, 10771ae08745Sheppo VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 1078e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'", 1079e1ebb9ecSlm66018 instance, name); 10801ae08745Sheppo return (EIO); 10811ae08745Sheppo } 10821ae08745Sheppo 10831ae08745Sheppo /* if any device node is created we set this flag */ 10841ae08745Sheppo vdc->initialized |= VDC_MINOR; 10851ae08745Sheppo 108687a7269eSachartre (void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw"); 108787a7269eSachartre 10881ae08745Sheppo if (ddi_create_minor_node(dip, name, S_IFCHR, 10891ae08745Sheppo VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 1090e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'", 1091e1ebb9ecSlm66018 instance, name); 10921ae08745Sheppo return (EIO); 10931ae08745Sheppo } 10941ae08745Sheppo } 10951ae08745Sheppo 10961ae08745Sheppo return (0); 10971ae08745Sheppo } 10981ae08745Sheppo 10991ae08745Sheppo /* 11005b98b509Sachartre * Driver prop_op(9e) entry point function. Return the number of blocks for 11015b98b509Sachartre * the partition in question or forward the request to the property facilities. 11021ae08745Sheppo */ 11031ae08745Sheppo static int 11045b98b509Sachartre vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 11055b98b509Sachartre char *name, caddr_t valuep, int *lengthp) 11061ae08745Sheppo { 11075b98b509Sachartre int instance = ddi_get_instance(dip); 11085b98b509Sachartre vdc_t *vdc; 11095b98b509Sachartre uint64_t nblocks; 11105b98b509Sachartre uint_t blksize; 11111ae08745Sheppo 11125b98b509Sachartre vdc = ddi_get_soft_state(vdc_state, instance); 11131ae08745Sheppo 11145b98b509Sachartre if (dev == DDI_DEV_T_ANY || vdc == NULL) { 11155b98b509Sachartre return (ddi_prop_op(dev, dip, prop_op, mod_flags, 11165b98b509Sachartre name, valuep, lengthp)); 11171ae08745Sheppo } 11181ae08745Sheppo 11195b98b509Sachartre mutex_enter(&vdc->lock); 11205b98b509Sachartre (void) vdc_validate_geometry(vdc); 112178fcd0a1Sachartre if (vdc->vdisk_label == VD_DISK_LABEL_UNK) { 11225b98b509Sachartre mutex_exit(&vdc->lock); 11235b98b509Sachartre return (ddi_prop_op(dev, dip, prop_op, mod_flags, 11245b98b509Sachartre name, valuep, lengthp)); 112578fcd0a1Sachartre } 11265b98b509Sachartre nblocks = vdc->slice[VDCPART(dev)].nblocks; 11275b98b509Sachartre blksize = vdc->block_size; 11285b98b509Sachartre mutex_exit(&vdc->lock); 112978fcd0a1Sachartre 11305b98b509Sachartre return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags, 11315b98b509Sachartre name, valuep, lengthp, nblocks, blksize)); 11321ae08745Sheppo } 11331ae08745Sheppo 113478fcd0a1Sachartre /* 113578fcd0a1Sachartre * Function: 113678fcd0a1Sachartre * vdc_is_opened 113778fcd0a1Sachartre * 113878fcd0a1Sachartre * Description: 113978fcd0a1Sachartre * This function checks if any slice of a given virtual disk is 114078fcd0a1Sachartre * currently opened. 114178fcd0a1Sachartre * 114278fcd0a1Sachartre * Parameters: 114378fcd0a1Sachartre * vdc - soft state pointer 114478fcd0a1Sachartre * 114578fcd0a1Sachartre * Return Values 114678fcd0a1Sachartre * B_TRUE - at least one slice is opened. 114778fcd0a1Sachartre * B_FALSE - no slice is opened. 114878fcd0a1Sachartre */ 114978fcd0a1Sachartre static boolean_t 115078fcd0a1Sachartre vdc_is_opened(vdc_t *vdc) 115178fcd0a1Sachartre { 115278fcd0a1Sachartre int i, nslices; 115378fcd0a1Sachartre 115478fcd0a1Sachartre switch (vdc->vdisk_type) { 115578fcd0a1Sachartre case VD_DISK_TYPE_DISK: 115678fcd0a1Sachartre nslices = V_NUMPAR; 115778fcd0a1Sachartre break; 115878fcd0a1Sachartre case VD_DISK_TYPE_SLICE: 115978fcd0a1Sachartre nslices = 1; 116078fcd0a1Sachartre break; 116178fcd0a1Sachartre case VD_DISK_TYPE_UNK: 116278fcd0a1Sachartre default: 116378fcd0a1Sachartre ASSERT(0); 116478fcd0a1Sachartre } 116578fcd0a1Sachartre 116678fcd0a1Sachartre /* check if there's any layered open */ 116778fcd0a1Sachartre for (i = 0; i < nslices; i++) { 116878fcd0a1Sachartre if (vdc->open_lyr[i] > 0) 116978fcd0a1Sachartre return (B_TRUE); 117078fcd0a1Sachartre } 117178fcd0a1Sachartre 117278fcd0a1Sachartre /* check if there is any other kind of open */ 117378fcd0a1Sachartre for (i = 0; i < OTYPCNT; i++) { 117478fcd0a1Sachartre if (vdc->open[i] != 0) 117578fcd0a1Sachartre return (B_TRUE); 117678fcd0a1Sachartre } 117778fcd0a1Sachartre 117878fcd0a1Sachartre return (B_FALSE); 117978fcd0a1Sachartre } 118078fcd0a1Sachartre 118178fcd0a1Sachartre static int 118278fcd0a1Sachartre vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp) 118378fcd0a1Sachartre { 118478fcd0a1Sachartre uint8_t slicemask; 118578fcd0a1Sachartre int i; 118678fcd0a1Sachartre 118778fcd0a1Sachartre ASSERT(otyp < OTYPCNT); 118878fcd0a1Sachartre ASSERT(slice < V_NUMPAR); 118978fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 119078fcd0a1Sachartre 119178fcd0a1Sachartre slicemask = 1 << slice; 119278fcd0a1Sachartre 119378fcd0a1Sachartre /* check if slice is already exclusively opened */ 119478fcd0a1Sachartre if (vdc->open_excl & slicemask) 119578fcd0a1Sachartre return (EBUSY); 119678fcd0a1Sachartre 119778fcd0a1Sachartre /* if open exclusive, check if slice is already opened */ 119878fcd0a1Sachartre if (flag & FEXCL) { 119978fcd0a1Sachartre if (vdc->open_lyr[slice] > 0) 120078fcd0a1Sachartre return (EBUSY); 120178fcd0a1Sachartre for (i = 0; i < OTYPCNT; i++) { 120278fcd0a1Sachartre if (vdc->open[i] & slicemask) 120378fcd0a1Sachartre return (EBUSY); 120478fcd0a1Sachartre } 120578fcd0a1Sachartre vdc->open_excl |= slicemask; 120678fcd0a1Sachartre } 120778fcd0a1Sachartre 120878fcd0a1Sachartre /* mark slice as opened */ 120978fcd0a1Sachartre if (otyp == OTYP_LYR) { 121078fcd0a1Sachartre vdc->open_lyr[slice]++; 121178fcd0a1Sachartre } else { 121278fcd0a1Sachartre vdc->open[otyp] |= slicemask; 121378fcd0a1Sachartre } 121478fcd0a1Sachartre 121578fcd0a1Sachartre return (0); 121678fcd0a1Sachartre } 121778fcd0a1Sachartre 121878fcd0a1Sachartre static void 121978fcd0a1Sachartre vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp) 122078fcd0a1Sachartre { 122178fcd0a1Sachartre uint8_t slicemask; 122278fcd0a1Sachartre 122378fcd0a1Sachartre ASSERT(otyp < OTYPCNT); 122478fcd0a1Sachartre ASSERT(slice < V_NUMPAR); 122578fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 122678fcd0a1Sachartre 122778fcd0a1Sachartre slicemask = 1 << slice; 122878fcd0a1Sachartre 122978fcd0a1Sachartre if (otyp == OTYP_LYR) { 123078fcd0a1Sachartre ASSERT(vdc->open_lyr[slice] > 0); 123178fcd0a1Sachartre vdc->open_lyr[slice]--; 123278fcd0a1Sachartre } else { 123378fcd0a1Sachartre vdc->open[otyp] &= ~slicemask; 123478fcd0a1Sachartre } 123578fcd0a1Sachartre 123678fcd0a1Sachartre if (flag & FEXCL) 123778fcd0a1Sachartre vdc->open_excl &= ~slicemask; 123878fcd0a1Sachartre } 123978fcd0a1Sachartre 12401ae08745Sheppo static int 12411ae08745Sheppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred) 12421ae08745Sheppo { 12431ae08745Sheppo _NOTE(ARGUNUSED(cred)) 12441ae08745Sheppo 1245179e09c2Sachartre int instance, nodelay; 124678fcd0a1Sachartre int slice, status = 0; 12471ae08745Sheppo vdc_t *vdc; 12481ae08745Sheppo 12491ae08745Sheppo ASSERT(dev != NULL); 12500d0c8d4bSnarayan instance = VDCUNIT(*dev); 12511ae08745Sheppo 125278fcd0a1Sachartre if (otyp >= OTYPCNT) 12531ae08745Sheppo return (EINVAL); 12541ae08745Sheppo 12551ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1256e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 12571ae08745Sheppo return (ENXIO); 12581ae08745Sheppo } 12591ae08745Sheppo 12603af08d82Slm66018 DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n", 12613af08d82Slm66018 getminor(*dev), flag, otyp); 12621ae08745Sheppo 126378fcd0a1Sachartre slice = VDCPART(*dev); 126478fcd0a1Sachartre 1265179e09c2Sachartre nodelay = flag & (FNDELAY | FNONBLOCK); 1266179e09c2Sachartre 1267179e09c2Sachartre if ((flag & FWRITE) && (!nodelay) && 1268179e09c2Sachartre !(VD_OP_SUPPORTED(vdc->operations, VD_OP_BWRITE))) { 1269179e09c2Sachartre return (EROFS); 1270179e09c2Sachartre } 1271179e09c2Sachartre 12721ae08745Sheppo mutex_enter(&vdc->lock); 127378fcd0a1Sachartre 127478fcd0a1Sachartre status = vdc_mark_opened(vdc, slice, flag, otyp); 127578fcd0a1Sachartre 127678fcd0a1Sachartre if (status != 0) { 127778fcd0a1Sachartre mutex_exit(&vdc->lock); 127878fcd0a1Sachartre return (status); 127978fcd0a1Sachartre } 128078fcd0a1Sachartre 1281179e09c2Sachartre if (nodelay) { 128278fcd0a1Sachartre 128378fcd0a1Sachartre /* don't resubmit a validate request if there's already one */ 128478fcd0a1Sachartre if (vdc->validate_pending > 0) { 128578fcd0a1Sachartre mutex_exit(&vdc->lock); 128678fcd0a1Sachartre return (0); 128778fcd0a1Sachartre } 128878fcd0a1Sachartre 128978fcd0a1Sachartre /* call vdc_validate() asynchronously to avoid blocking */ 129078fcd0a1Sachartre if (taskq_dispatch(system_taskq, vdc_validate_task, 129178fcd0a1Sachartre (void *)vdc, TQ_NOSLEEP) == NULL) { 129278fcd0a1Sachartre vdc_mark_closed(vdc, slice, flag, otyp); 129378fcd0a1Sachartre mutex_exit(&vdc->lock); 129478fcd0a1Sachartre return (ENXIO); 129578fcd0a1Sachartre } 129678fcd0a1Sachartre 129778fcd0a1Sachartre vdc->validate_pending++; 129878fcd0a1Sachartre mutex_exit(&vdc->lock); 129978fcd0a1Sachartre return (0); 130078fcd0a1Sachartre } 130178fcd0a1Sachartre 13021ae08745Sheppo mutex_exit(&vdc->lock); 13031ae08745Sheppo 130478fcd0a1Sachartre vdc_validate(vdc); 130578fcd0a1Sachartre 130678fcd0a1Sachartre mutex_enter(&vdc->lock); 130778fcd0a1Sachartre 130878fcd0a1Sachartre if (vdc->vdisk_label == VD_DISK_LABEL_UNK || 1309edcc0754Sachartre vdc->slice[slice].nblocks == 0) { 131078fcd0a1Sachartre vdc_mark_closed(vdc, slice, flag, otyp); 131178fcd0a1Sachartre status = EIO; 131278fcd0a1Sachartre } 131378fcd0a1Sachartre 131478fcd0a1Sachartre mutex_exit(&vdc->lock); 131578fcd0a1Sachartre 131678fcd0a1Sachartre return (status); 13171ae08745Sheppo } 13181ae08745Sheppo 13191ae08745Sheppo static int 13201ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred) 13211ae08745Sheppo { 13221ae08745Sheppo _NOTE(ARGUNUSED(cred)) 13231ae08745Sheppo 13241ae08745Sheppo int instance; 132578fcd0a1Sachartre int slice; 13262f5224aeSachartre int rv, rval; 13271ae08745Sheppo vdc_t *vdc; 13281ae08745Sheppo 13290d0c8d4bSnarayan instance = VDCUNIT(dev); 13301ae08745Sheppo 133178fcd0a1Sachartre if (otyp >= OTYPCNT) 13321ae08745Sheppo return (EINVAL); 13331ae08745Sheppo 13341ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1335e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 13361ae08745Sheppo return (ENXIO); 13371ae08745Sheppo } 13381ae08745Sheppo 13393af08d82Slm66018 DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp); 13401ae08745Sheppo 134178fcd0a1Sachartre slice = VDCPART(dev); 134278fcd0a1Sachartre 13438259acd8Szk194757 /* 13448259acd8Szk194757 * Attempt to flush the W$ on a close operation. If this is 13458259acd8Szk194757 * not a supported IOCTL command or the backing device is read-only 13468259acd8Szk194757 * do not fail the close operation. 13478259acd8Szk194757 */ 13482f5224aeSachartre rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, &rval); 13498259acd8Szk194757 13508259acd8Szk194757 if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) { 13518259acd8Szk194757 DMSG(vdc, 0, "[%d] flush failed with error %d on close\n", 13528259acd8Szk194757 instance, rv); 13538259acd8Szk194757 return (EIO); 13548259acd8Szk194757 } 13558259acd8Szk194757 13561ae08745Sheppo mutex_enter(&vdc->lock); 135778fcd0a1Sachartre vdc_mark_closed(vdc, slice, flag, otyp); 13581ae08745Sheppo mutex_exit(&vdc->lock); 13591ae08745Sheppo 13601ae08745Sheppo return (0); 13611ae08745Sheppo } 13621ae08745Sheppo 13631ae08745Sheppo static int 13641ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 13651ae08745Sheppo { 13661ae08745Sheppo _NOTE(ARGUNUSED(credp)) 13671ae08745Sheppo 13682f5224aeSachartre return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode, rvalp)); 13691ae08745Sheppo } 13701ae08745Sheppo 13711ae08745Sheppo static int 13721ae08745Sheppo vdc_print(dev_t dev, char *str) 13731ae08745Sheppo { 13740d0c8d4bSnarayan cmn_err(CE_NOTE, "vdc%d: %s", VDCUNIT(dev), str); 13751ae08745Sheppo return (0); 13761ae08745Sheppo } 13771ae08745Sheppo 13781ae08745Sheppo static int 13791ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 13801ae08745Sheppo { 1381d10e4ef2Snarayan int rv; 1382d10e4ef2Snarayan size_t nbytes = nblk * DEV_BSIZE; 13830d0c8d4bSnarayan int instance = VDCUNIT(dev); 1384d10e4ef2Snarayan vdc_t *vdc = NULL; 13851ae08745Sheppo 13861ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1387e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 13881ae08745Sheppo return (ENXIO); 13891ae08745Sheppo } 13901ae08745Sheppo 13913af08d82Slm66018 DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n", 13923af08d82Slm66018 instance, nbytes, blkno, (void *)addr); 13933af08d82Slm66018 rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes, 13940d0c8d4bSnarayan VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir); 13953af08d82Slm66018 if (rv) { 13963af08d82Slm66018 DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv); 13971ae08745Sheppo return (rv); 13981ae08745Sheppo } 13991ae08745Sheppo 14003af08d82Slm66018 if (ddi_in_panic()) 1401*11f54b6eSAlexandre Chartre (void) vdc_drain_response(vdc, CB_STRATEGY, NULL); 14023af08d82Slm66018 14033af08d82Slm66018 DMSG(vdc, 0, "[%d] End\n", instance); 14043af08d82Slm66018 14053af08d82Slm66018 return (0); 14063af08d82Slm66018 } 14073af08d82Slm66018 14081ae08745Sheppo /* -------------------------------------------------------------------------- */ 14091ae08745Sheppo 14101ae08745Sheppo /* 14111ae08745Sheppo * Disk access routines 14121ae08745Sheppo * 14131ae08745Sheppo */ 14141ae08745Sheppo 14151ae08745Sheppo /* 14161ae08745Sheppo * vdc_strategy() 14171ae08745Sheppo * 14181ae08745Sheppo * Return Value: 14191ae08745Sheppo * 0: As per strategy(9E), the strategy() function must return 0 14201ae08745Sheppo * [ bioerror(9f) sets b_flags to the proper error code ] 14211ae08745Sheppo */ 14221ae08745Sheppo static int 14231ae08745Sheppo vdc_strategy(struct buf *buf) 14241ae08745Sheppo { 14251ae08745Sheppo int rv = -1; 14261ae08745Sheppo vdc_t *vdc = NULL; 14270d0c8d4bSnarayan int instance = VDCUNIT(buf->b_edev); 14281ae08745Sheppo int op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE; 142987a7269eSachartre int slice; 14301ae08745Sheppo 14311ae08745Sheppo if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1432e1ebb9ecSlm66018 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 14331ae08745Sheppo bioerror(buf, ENXIO); 14341ae08745Sheppo biodone(buf); 14351ae08745Sheppo return (0); 14361ae08745Sheppo } 14371ae08745Sheppo 14383af08d82Slm66018 DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n", 14393af08d82Slm66018 instance, (buf->b_flags & B_READ) ? "Read" : "Write", 14403af08d82Slm66018 buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr); 1441d10e4ef2Snarayan 14421ae08745Sheppo bp_mapin(buf); 14431ae08745Sheppo 144487a7269eSachartre if ((long)buf->b_private == VD_SLICE_NONE) { 144587a7269eSachartre /* I/O using an absolute disk offset */ 144687a7269eSachartre slice = VD_SLICE_NONE; 144787a7269eSachartre } else { 144887a7269eSachartre slice = VDCPART(buf->b_edev); 144987a7269eSachartre } 145087a7269eSachartre 14513af08d82Slm66018 rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr, 145287a7269eSachartre buf->b_bcount, slice, buf->b_lblkno, 14533af08d82Slm66018 CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir : 14543af08d82Slm66018 VIO_write_dir); 14553af08d82Slm66018 1456d10e4ef2Snarayan /* 1457d10e4ef2Snarayan * If the request was successfully sent, the strategy call returns and 1458d10e4ef2Snarayan * the ACK handler calls the bioxxx functions when the vDisk server is 1459366a92acSlm66018 * done otherwise we handle the error here. 1460d10e4ef2Snarayan */ 1461d10e4ef2Snarayan if (rv) { 14623af08d82Slm66018 DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv); 14631ae08745Sheppo bioerror(buf, rv); 14641ae08745Sheppo biodone(buf); 14653c2ebf09Sachartre } else if (ddi_in_panic()) { 1466*11f54b6eSAlexandre Chartre rv = vdc_drain_response(vdc, CB_STRATEGY, buf); 1467*11f54b6eSAlexandre Chartre if (rv != 0) { 1468*11f54b6eSAlexandre Chartre bioerror(buf, EIO); 1469*11f54b6eSAlexandre Chartre biodone(buf); 1470*11f54b6eSAlexandre Chartre } 1471d10e4ef2Snarayan } 1472d10e4ef2Snarayan 14731ae08745Sheppo return (0); 14741ae08745Sheppo } 14751ae08745Sheppo 14760d0c8d4bSnarayan /* 14770d0c8d4bSnarayan * Function: 14780d0c8d4bSnarayan * vdc_min 14790d0c8d4bSnarayan * 14800d0c8d4bSnarayan * Description: 14810d0c8d4bSnarayan * Routine to limit the size of a data transfer. Used in 14820d0c8d4bSnarayan * conjunction with physio(9F). 14830d0c8d4bSnarayan * 14840d0c8d4bSnarayan * Arguments: 14850d0c8d4bSnarayan * bp - pointer to the indicated buf(9S) struct. 14860d0c8d4bSnarayan * 14870d0c8d4bSnarayan */ 14880d0c8d4bSnarayan static void 14890d0c8d4bSnarayan vdc_min(struct buf *bufp) 14900d0c8d4bSnarayan { 14910d0c8d4bSnarayan vdc_t *vdc = NULL; 14920d0c8d4bSnarayan int instance = VDCUNIT(bufp->b_edev); 14930d0c8d4bSnarayan 14940d0c8d4bSnarayan vdc = ddi_get_soft_state(vdc_state, instance); 14950d0c8d4bSnarayan VERIFY(vdc != NULL); 14960d0c8d4bSnarayan 14970d0c8d4bSnarayan if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) { 14980d0c8d4bSnarayan bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size; 14990d0c8d4bSnarayan } 15000d0c8d4bSnarayan } 15011ae08745Sheppo 15021ae08745Sheppo static int 15031ae08745Sheppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred) 15041ae08745Sheppo { 15051ae08745Sheppo _NOTE(ARGUNUSED(cred)) 15061ae08745Sheppo 15070d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 15080d0c8d4bSnarayan return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio)); 15091ae08745Sheppo } 15101ae08745Sheppo 15111ae08745Sheppo static int 15121ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred) 15131ae08745Sheppo { 15141ae08745Sheppo _NOTE(ARGUNUSED(cred)) 15151ae08745Sheppo 15160d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 15170d0c8d4bSnarayan return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio)); 15181ae08745Sheppo } 15191ae08745Sheppo 15201ae08745Sheppo static int 15211ae08745Sheppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred) 15221ae08745Sheppo { 15231ae08745Sheppo _NOTE(ARGUNUSED(cred)) 15241ae08745Sheppo 15250d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 15260d0c8d4bSnarayan return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio)); 15271ae08745Sheppo } 15281ae08745Sheppo 15291ae08745Sheppo static int 15301ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred) 15311ae08745Sheppo { 15321ae08745Sheppo _NOTE(ARGUNUSED(cred)) 15331ae08745Sheppo 15340d0c8d4bSnarayan DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 15350d0c8d4bSnarayan return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio)); 15361ae08745Sheppo } 15371ae08745Sheppo 15381ae08745Sheppo 15391ae08745Sheppo /* -------------------------------------------------------------------------- */ 15401ae08745Sheppo 15411ae08745Sheppo /* 15421ae08745Sheppo * Handshake support 15431ae08745Sheppo */ 15441ae08745Sheppo 15451ae08745Sheppo 15460a55fbb7Slm66018 /* 15470a55fbb7Slm66018 * Function: 15480a55fbb7Slm66018 * vdc_init_ver_negotiation() 15490a55fbb7Slm66018 * 15500a55fbb7Slm66018 * Description: 15510a55fbb7Slm66018 * 15520a55fbb7Slm66018 * Arguments: 15530a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 15540a55fbb7Slm66018 * 15550a55fbb7Slm66018 * Return Code: 15560a55fbb7Slm66018 * 0 - Success 15570a55fbb7Slm66018 */ 15581ae08745Sheppo static int 15590a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver) 15601ae08745Sheppo { 15611ae08745Sheppo vio_ver_msg_t pkt; 15621ae08745Sheppo size_t msglen = sizeof (pkt); 15631ae08745Sheppo int status = -1; 15641ae08745Sheppo 15651ae08745Sheppo ASSERT(vdc != NULL); 15661ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 15671ae08745Sheppo 15683af08d82Slm66018 DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance); 1569e1ebb9ecSlm66018 15701ae08745Sheppo /* 15711ae08745Sheppo * set the Session ID to a unique value 15721ae08745Sheppo * (the lower 32 bits of the clock tick) 15731ae08745Sheppo */ 15741ae08745Sheppo vdc->session_id = ((uint32_t)gettick() & 0xffffffff); 15753af08d82Slm66018 DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id); 15761ae08745Sheppo 15771ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 15781ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 15791ae08745Sheppo pkt.tag.vio_subtype_env = VIO_VER_INFO; 15801ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 15811ae08745Sheppo pkt.dev_class = VDEV_DISK; 15820a55fbb7Slm66018 pkt.ver_major = ver.major; 15830a55fbb7Slm66018 pkt.ver_minor = ver.minor; 15841ae08745Sheppo 15850a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 15863af08d82Slm66018 DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n", 15873af08d82Slm66018 vdc->instance, status); 15881ae08745Sheppo if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) { 15893af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: " 15908cd10891Snarayan "id(%lx) rv(%d) size(%ld)", vdc->instance, 15918cd10891Snarayan vdc->curr_server->ldc_handle, status, msglen); 15921ae08745Sheppo if (msglen != sizeof (vio_ver_msg_t)) 15931ae08745Sheppo status = ENOMSG; 15941ae08745Sheppo } 15951ae08745Sheppo 15961ae08745Sheppo return (status); 15971ae08745Sheppo } 15981ae08745Sheppo 15990a55fbb7Slm66018 /* 16000a55fbb7Slm66018 * Function: 16013af08d82Slm66018 * vdc_ver_negotiation() 16023af08d82Slm66018 * 16033af08d82Slm66018 * Description: 16043af08d82Slm66018 * 16053af08d82Slm66018 * Arguments: 16063af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 16073af08d82Slm66018 * 16083af08d82Slm66018 * Return Code: 16093af08d82Slm66018 * 0 - Success 16103af08d82Slm66018 */ 16113af08d82Slm66018 static int 16123af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp) 16133af08d82Slm66018 { 16143af08d82Slm66018 vio_msg_t vio_msg; 16153af08d82Slm66018 int status; 16163af08d82Slm66018 16173af08d82Slm66018 if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0])) 16183af08d82Slm66018 return (status); 16193af08d82Slm66018 16203af08d82Slm66018 /* release lock and wait for response */ 16213af08d82Slm66018 mutex_exit(&vdcp->lock); 16223af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 16233af08d82Slm66018 mutex_enter(&vdcp->lock); 16243af08d82Slm66018 if (status) { 16253af08d82Slm66018 DMSG(vdcp, 0, 16263af08d82Slm66018 "[%d] Failed waiting for Ver negotiation response, rv(%d)", 16273af08d82Slm66018 vdcp->instance, status); 16283af08d82Slm66018 return (status); 16293af08d82Slm66018 } 16303af08d82Slm66018 16313af08d82Slm66018 /* check type and sub_type ... */ 16323af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 16333af08d82Slm66018 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 16343af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n", 16353af08d82Slm66018 vdcp->instance); 16363af08d82Slm66018 return (EPROTO); 16373af08d82Slm66018 } 16383af08d82Slm66018 16393af08d82Slm66018 return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg)); 16403af08d82Slm66018 } 16413af08d82Slm66018 16423af08d82Slm66018 /* 16433af08d82Slm66018 * Function: 16440a55fbb7Slm66018 * vdc_init_attr_negotiation() 16450a55fbb7Slm66018 * 16460a55fbb7Slm66018 * Description: 16470a55fbb7Slm66018 * 16480a55fbb7Slm66018 * Arguments: 16490a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 16500a55fbb7Slm66018 * 16510a55fbb7Slm66018 * Return Code: 16520a55fbb7Slm66018 * 0 - Success 16530a55fbb7Slm66018 */ 16541ae08745Sheppo static int 16551ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc) 16561ae08745Sheppo { 16571ae08745Sheppo vd_attr_msg_t pkt; 16581ae08745Sheppo size_t msglen = sizeof (pkt); 16591ae08745Sheppo int status; 16601ae08745Sheppo 16611ae08745Sheppo ASSERT(vdc != NULL); 16621ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 16631ae08745Sheppo 16643af08d82Slm66018 DMSG(vdc, 0, "[%d] entered\n", vdc->instance); 16651ae08745Sheppo 16661ae08745Sheppo /* fill in tag */ 16671ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 16681ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 16691ae08745Sheppo pkt.tag.vio_subtype_env = VIO_ATTR_INFO; 16701ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 16711ae08745Sheppo /* fill in payload */ 16721ae08745Sheppo pkt.max_xfer_sz = vdc->max_xfer_sz; 16731ae08745Sheppo pkt.vdisk_block_size = vdc->block_size; 1674f0ca1d9aSsb155480 pkt.xfer_mode = VIO_DRING_MODE_V1_0; 16751ae08745Sheppo pkt.operations = 0; /* server will set bits of valid operations */ 16761ae08745Sheppo pkt.vdisk_type = 0; /* server will set to valid device type */ 167717cadca8Slm66018 pkt.vdisk_media = 0; /* server will set to valid media type */ 16781ae08745Sheppo pkt.vdisk_size = 0; /* server will set to valid size */ 16791ae08745Sheppo 16800a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 16813af08d82Slm66018 DMSG(vdc, 0, "Attr info sent (status = %d)\n", status); 16821ae08745Sheppo 1683f3241e46Sanbui if ((status != 0) || (msglen != sizeof (vd_attr_msg_t))) { 16843af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: " 16858cd10891Snarayan "id(%lx) rv(%d) size(%ld)", vdc->instance, 16868cd10891Snarayan vdc->curr_server->ldc_handle, status, msglen); 1687f3241e46Sanbui if (msglen != sizeof (vd_attr_msg_t)) 16881ae08745Sheppo status = ENOMSG; 16891ae08745Sheppo } 16901ae08745Sheppo 16911ae08745Sheppo return (status); 16921ae08745Sheppo } 16931ae08745Sheppo 16940a55fbb7Slm66018 /* 16950a55fbb7Slm66018 * Function: 16963af08d82Slm66018 * vdc_attr_negotiation() 16973af08d82Slm66018 * 16983af08d82Slm66018 * Description: 16993af08d82Slm66018 * 17003af08d82Slm66018 * Arguments: 17013af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 17023af08d82Slm66018 * 17033af08d82Slm66018 * Return Code: 17043af08d82Slm66018 * 0 - Success 17053af08d82Slm66018 */ 17063af08d82Slm66018 static int 17073af08d82Slm66018 vdc_attr_negotiation(vdc_t *vdcp) 17083af08d82Slm66018 { 17093af08d82Slm66018 int status; 17103af08d82Slm66018 vio_msg_t vio_msg; 17113af08d82Slm66018 17123af08d82Slm66018 if (status = vdc_init_attr_negotiation(vdcp)) 17133af08d82Slm66018 return (status); 17143af08d82Slm66018 17153af08d82Slm66018 /* release lock and wait for response */ 17163af08d82Slm66018 mutex_exit(&vdcp->lock); 17173af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 17183af08d82Slm66018 mutex_enter(&vdcp->lock); 17193af08d82Slm66018 if (status) { 17203af08d82Slm66018 DMSG(vdcp, 0, 17213af08d82Slm66018 "[%d] Failed waiting for Attr negotiation response, rv(%d)", 17223af08d82Slm66018 vdcp->instance, status); 17233af08d82Slm66018 return (status); 17243af08d82Slm66018 } 17253af08d82Slm66018 17263af08d82Slm66018 /* check type and sub_type ... */ 17273af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 17283af08d82Slm66018 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 17293af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n", 17303af08d82Slm66018 vdcp->instance); 17313af08d82Slm66018 return (EPROTO); 17323af08d82Slm66018 } 17333af08d82Slm66018 17343af08d82Slm66018 return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg)); 17353af08d82Slm66018 } 17363af08d82Slm66018 17373af08d82Slm66018 17383af08d82Slm66018 /* 17393af08d82Slm66018 * Function: 17400a55fbb7Slm66018 * vdc_init_dring_negotiate() 17410a55fbb7Slm66018 * 17420a55fbb7Slm66018 * Description: 17430a55fbb7Slm66018 * 17440a55fbb7Slm66018 * Arguments: 17450a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 17460a55fbb7Slm66018 * 17470a55fbb7Slm66018 * Return Code: 17480a55fbb7Slm66018 * 0 - Success 17490a55fbb7Slm66018 */ 17501ae08745Sheppo static int 17511ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc) 17521ae08745Sheppo { 17531ae08745Sheppo vio_dring_reg_msg_t pkt; 17541ae08745Sheppo size_t msglen = sizeof (pkt); 17551ae08745Sheppo int status = -1; 17563af08d82Slm66018 int retry; 17573af08d82Slm66018 int nretries = 10; 17581ae08745Sheppo 17591ae08745Sheppo ASSERT(vdc != NULL); 17601ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 17611ae08745Sheppo 17623af08d82Slm66018 for (retry = 0; retry < nretries; retry++) { 17631ae08745Sheppo status = vdc_init_descriptor_ring(vdc); 17643af08d82Slm66018 if (status != EAGAIN) 17653af08d82Slm66018 break; 17663af08d82Slm66018 drv_usecwait(vdc_min_timeout_ldc); 17673af08d82Slm66018 } 17683af08d82Slm66018 17691ae08745Sheppo if (status != 0) { 17703af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n", 17711ae08745Sheppo vdc->instance, status); 17721ae08745Sheppo return (status); 17731ae08745Sheppo } 17743af08d82Slm66018 17753af08d82Slm66018 DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n", 1776e1ebb9ecSlm66018 vdc->instance, status); 17771ae08745Sheppo 17781ae08745Sheppo /* fill in tag */ 17791ae08745Sheppo pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 17801ae08745Sheppo pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 17811ae08745Sheppo pkt.tag.vio_subtype_env = VIO_DRING_REG; 17821ae08745Sheppo pkt.tag.vio_sid = vdc->session_id; 17831ae08745Sheppo /* fill in payload */ 17841ae08745Sheppo pkt.dring_ident = 0; 1785e1ebb9ecSlm66018 pkt.num_descriptors = vdc->dring_len; 1786e1ebb9ecSlm66018 pkt.descriptor_size = vdc->dring_entry_size; 17871ae08745Sheppo pkt.options = (VIO_TX_DRING | VIO_RX_DRING); 17881ae08745Sheppo pkt.ncookies = vdc->dring_cookie_count; 17891ae08745Sheppo pkt.cookie[0] = vdc->dring_cookie[0]; /* for now just one cookie */ 17901ae08745Sheppo 17910a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 17921ae08745Sheppo if (status != 0) { 17933af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)", 1794e1ebb9ecSlm66018 vdc->instance, status); 17951ae08745Sheppo } 17961ae08745Sheppo 17971ae08745Sheppo return (status); 17981ae08745Sheppo } 17991ae08745Sheppo 18001ae08745Sheppo 18013af08d82Slm66018 /* 18023af08d82Slm66018 * Function: 18033af08d82Slm66018 * vdc_dring_negotiation() 18043af08d82Slm66018 * 18053af08d82Slm66018 * Description: 18063af08d82Slm66018 * 18073af08d82Slm66018 * Arguments: 18083af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 18093af08d82Slm66018 * 18103af08d82Slm66018 * Return Code: 18113af08d82Slm66018 * 0 - Success 18123af08d82Slm66018 */ 18133af08d82Slm66018 static int 18143af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp) 18153af08d82Slm66018 { 18163af08d82Slm66018 int status; 18173af08d82Slm66018 vio_msg_t vio_msg; 18183af08d82Slm66018 18193af08d82Slm66018 if (status = vdc_init_dring_negotiate(vdcp)) 18203af08d82Slm66018 return (status); 18213af08d82Slm66018 18223af08d82Slm66018 /* release lock and wait for response */ 18233af08d82Slm66018 mutex_exit(&vdcp->lock); 18243af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 18253af08d82Slm66018 mutex_enter(&vdcp->lock); 18263af08d82Slm66018 if (status) { 18273af08d82Slm66018 DMSG(vdcp, 0, 18283af08d82Slm66018 "[%d] Failed waiting for Dring negotiation response," 18293af08d82Slm66018 " rv(%d)", vdcp->instance, status); 18303af08d82Slm66018 return (status); 18313af08d82Slm66018 } 18323af08d82Slm66018 18333af08d82Slm66018 /* check type and sub_type ... */ 18343af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 18353af08d82Slm66018 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 18363af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n", 18373af08d82Slm66018 vdcp->instance); 18383af08d82Slm66018 return (EPROTO); 18393af08d82Slm66018 } 18403af08d82Slm66018 18413af08d82Slm66018 return (vdc_handle_dring_reg_msg(vdcp, 18423af08d82Slm66018 (vio_dring_reg_msg_t *)&vio_msg)); 18433af08d82Slm66018 } 18443af08d82Slm66018 18453af08d82Slm66018 18463af08d82Slm66018 /* 18473af08d82Slm66018 * Function: 18483af08d82Slm66018 * vdc_send_rdx() 18493af08d82Slm66018 * 18503af08d82Slm66018 * Description: 18513af08d82Slm66018 * 18523af08d82Slm66018 * Arguments: 18533af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 18543af08d82Slm66018 * 18553af08d82Slm66018 * Return Code: 18563af08d82Slm66018 * 0 - Success 18573af08d82Slm66018 */ 18583af08d82Slm66018 static int 18593af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp) 18603af08d82Slm66018 { 18613af08d82Slm66018 vio_msg_t msg; 18623af08d82Slm66018 size_t msglen = sizeof (vio_msg_t); 18633af08d82Slm66018 int status; 18643af08d82Slm66018 18653af08d82Slm66018 /* 18663af08d82Slm66018 * Send an RDX message to vds to indicate we are ready 18673af08d82Slm66018 * to send data 18683af08d82Slm66018 */ 18693af08d82Slm66018 msg.tag.vio_msgtype = VIO_TYPE_CTRL; 18703af08d82Slm66018 msg.tag.vio_subtype = VIO_SUBTYPE_INFO; 18713af08d82Slm66018 msg.tag.vio_subtype_env = VIO_RDX; 18723af08d82Slm66018 msg.tag.vio_sid = vdcp->session_id; 18733af08d82Slm66018 status = vdc_send(vdcp, (caddr_t)&msg, &msglen); 18743af08d82Slm66018 if (status != 0) { 18753af08d82Slm66018 DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)", 18763af08d82Slm66018 vdcp->instance, status); 18773af08d82Slm66018 } 18783af08d82Slm66018 18793af08d82Slm66018 return (status); 18803af08d82Slm66018 } 18813af08d82Slm66018 18823af08d82Slm66018 /* 18833af08d82Slm66018 * Function: 18843af08d82Slm66018 * vdc_handle_rdx() 18853af08d82Slm66018 * 18863af08d82Slm66018 * Description: 18873af08d82Slm66018 * 18883af08d82Slm66018 * Arguments: 18893af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 18903af08d82Slm66018 * msgp - received msg 18913af08d82Slm66018 * 18923af08d82Slm66018 * Return Code: 18933af08d82Slm66018 * 0 - Success 18943af08d82Slm66018 */ 18953af08d82Slm66018 static int 18963af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp) 18973af08d82Slm66018 { 18983af08d82Slm66018 _NOTE(ARGUNUSED(vdcp)) 18993af08d82Slm66018 _NOTE(ARGUNUSED(msgp)) 19003af08d82Slm66018 19013af08d82Slm66018 ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL); 19023af08d82Slm66018 ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK); 19033af08d82Slm66018 ASSERT(msgp->tag.vio_subtype_env == VIO_RDX); 19043af08d82Slm66018 19053af08d82Slm66018 DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance); 19063af08d82Slm66018 19073af08d82Slm66018 return (0); 19083af08d82Slm66018 } 19093af08d82Slm66018 19103af08d82Slm66018 /* 19113af08d82Slm66018 * Function: 19123af08d82Slm66018 * vdc_rdx_exchange() 19133af08d82Slm66018 * 19143af08d82Slm66018 * Description: 19153af08d82Slm66018 * 19163af08d82Slm66018 * Arguments: 19173af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 19183af08d82Slm66018 * 19193af08d82Slm66018 * Return Code: 19203af08d82Slm66018 * 0 - Success 19213af08d82Slm66018 */ 19223af08d82Slm66018 static int 19233af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp) 19243af08d82Slm66018 { 19253af08d82Slm66018 int status; 19263af08d82Slm66018 vio_msg_t vio_msg; 19273af08d82Slm66018 19283af08d82Slm66018 if (status = vdc_send_rdx(vdcp)) 19293af08d82Slm66018 return (status); 19303af08d82Slm66018 19313af08d82Slm66018 /* release lock and wait for response */ 19323af08d82Slm66018 mutex_exit(&vdcp->lock); 19333af08d82Slm66018 status = vdc_wait_for_response(vdcp, &vio_msg); 19343af08d82Slm66018 mutex_enter(&vdcp->lock); 19353af08d82Slm66018 if (status) { 193687a7269eSachartre DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)", 193787a7269eSachartre vdcp->instance, status); 19383af08d82Slm66018 return (status); 19393af08d82Slm66018 } 19403af08d82Slm66018 19413af08d82Slm66018 /* check type and sub_type ... */ 19423af08d82Slm66018 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 19433af08d82Slm66018 vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) { 194487a7269eSachartre DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance); 19453af08d82Slm66018 return (EPROTO); 19463af08d82Slm66018 } 19473af08d82Slm66018 19483af08d82Slm66018 return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg)); 19493af08d82Slm66018 } 19503af08d82Slm66018 19513af08d82Slm66018 19521ae08745Sheppo /* -------------------------------------------------------------------------- */ 19531ae08745Sheppo 19541ae08745Sheppo /* 19551ae08745Sheppo * LDC helper routines 19561ae08745Sheppo */ 19571ae08745Sheppo 19583af08d82Slm66018 static int 19593af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp) 19603af08d82Slm66018 { 19613af08d82Slm66018 int status; 19623af08d82Slm66018 boolean_t q_has_pkts = B_FALSE; 196317cadca8Slm66018 uint64_t delay_time; 19643af08d82Slm66018 size_t len; 19653af08d82Slm66018 19663af08d82Slm66018 mutex_enter(&vdc->read_lock); 19673af08d82Slm66018 19683af08d82Slm66018 if (vdc->read_state == VDC_READ_IDLE) 19693af08d82Slm66018 vdc->read_state = VDC_READ_WAITING; 19703af08d82Slm66018 19713af08d82Slm66018 while (vdc->read_state != VDC_READ_PENDING) { 19723af08d82Slm66018 19733af08d82Slm66018 /* detect if the connection has been reset */ 19743af08d82Slm66018 if (vdc->read_state == VDC_READ_RESET) { 19753af08d82Slm66018 status = ECONNRESET; 19763af08d82Slm66018 goto done; 19773af08d82Slm66018 } 19783af08d82Slm66018 19793af08d82Slm66018 cv_wait(&vdc->read_cv, &vdc->read_lock); 19803af08d82Slm66018 } 19813af08d82Slm66018 19823af08d82Slm66018 /* 19833af08d82Slm66018 * Until we get a blocking ldc read we have to retry 19843af08d82Slm66018 * until the entire LDC message has arrived before 19853af08d82Slm66018 * ldc_read() will succeed. Note we also bail out if 1986eff7243fSlm66018 * the channel is reset or goes away. 19873af08d82Slm66018 */ 19883af08d82Slm66018 delay_time = vdc_ldc_read_init_delay; 19893af08d82Slm66018 loop: 19903af08d82Slm66018 len = *nbytesp; 19918cd10891Snarayan status = ldc_read(vdc->curr_server->ldc_handle, (caddr_t)msgp, &len); 19923af08d82Slm66018 switch (status) { 19933af08d82Slm66018 case EAGAIN: 19943af08d82Slm66018 delay_time *= 2; 19953af08d82Slm66018 if (delay_time >= vdc_ldc_read_max_delay) 19963af08d82Slm66018 delay_time = vdc_ldc_read_max_delay; 19973af08d82Slm66018 delay(delay_time); 19983af08d82Slm66018 goto loop; 19993af08d82Slm66018 20003af08d82Slm66018 case 0: 20013af08d82Slm66018 if (len == 0) { 200217cadca8Slm66018 DMSG(vdc, 1, "[%d] ldc_read returned 0 bytes with " 20033af08d82Slm66018 "no error!\n", vdc->instance); 20043af08d82Slm66018 goto loop; 20053af08d82Slm66018 } 20063af08d82Slm66018 20073af08d82Slm66018 *nbytesp = len; 20083af08d82Slm66018 20093af08d82Slm66018 /* 20103af08d82Slm66018 * If there are pending messages, leave the 20113af08d82Slm66018 * read state as pending. Otherwise, set the state 20123af08d82Slm66018 * back to idle. 20133af08d82Slm66018 */ 20148cd10891Snarayan status = ldc_chkq(vdc->curr_server->ldc_handle, &q_has_pkts); 20153af08d82Slm66018 if (status == 0 && !q_has_pkts) 20163af08d82Slm66018 vdc->read_state = VDC_READ_IDLE; 20173af08d82Slm66018 20183af08d82Slm66018 break; 20193af08d82Slm66018 default: 20203af08d82Slm66018 DMSG(vdc, 0, "ldc_read returned %d\n", status); 20213af08d82Slm66018 break; 20223af08d82Slm66018 } 20233af08d82Slm66018 20243af08d82Slm66018 done: 20253af08d82Slm66018 mutex_exit(&vdc->read_lock); 20263af08d82Slm66018 20273af08d82Slm66018 return (status); 20283af08d82Slm66018 } 20293af08d82Slm66018 20303af08d82Slm66018 20313af08d82Slm66018 20323af08d82Slm66018 #ifdef DEBUG 20333af08d82Slm66018 void 20343af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg) 20353af08d82Slm66018 { 20363af08d82Slm66018 char *ms, *ss, *ses; 20373af08d82Slm66018 switch (msg->tag.vio_msgtype) { 20383af08d82Slm66018 #define Q(_s) case _s : ms = #_s; break; 20393af08d82Slm66018 Q(VIO_TYPE_CTRL) 20403af08d82Slm66018 Q(VIO_TYPE_DATA) 20413af08d82Slm66018 Q(VIO_TYPE_ERR) 20423af08d82Slm66018 #undef Q 20433af08d82Slm66018 default: ms = "unknown"; break; 20443af08d82Slm66018 } 20453af08d82Slm66018 20463af08d82Slm66018 switch (msg->tag.vio_subtype) { 20473af08d82Slm66018 #define Q(_s) case _s : ss = #_s; break; 20483af08d82Slm66018 Q(VIO_SUBTYPE_INFO) 20493af08d82Slm66018 Q(VIO_SUBTYPE_ACK) 20503af08d82Slm66018 Q(VIO_SUBTYPE_NACK) 20513af08d82Slm66018 #undef Q 20523af08d82Slm66018 default: ss = "unknown"; break; 20533af08d82Slm66018 } 20543af08d82Slm66018 20553af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 20563af08d82Slm66018 #define Q(_s) case _s : ses = #_s; break; 20573af08d82Slm66018 Q(VIO_VER_INFO) 20583af08d82Slm66018 Q(VIO_ATTR_INFO) 20593af08d82Slm66018 Q(VIO_DRING_REG) 20603af08d82Slm66018 Q(VIO_DRING_UNREG) 20613af08d82Slm66018 Q(VIO_RDX) 20623af08d82Slm66018 Q(VIO_PKT_DATA) 20633af08d82Slm66018 Q(VIO_DESC_DATA) 20643af08d82Slm66018 Q(VIO_DRING_DATA) 20653af08d82Slm66018 #undef Q 20663af08d82Slm66018 default: ses = "unknown"; break; 20673af08d82Slm66018 } 20683af08d82Slm66018 20693af08d82Slm66018 DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n", 20703af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 20713af08d82Slm66018 msg->tag.vio_subtype_env, ms, ss, ses); 20723af08d82Slm66018 } 20733af08d82Slm66018 #endif 20743af08d82Slm66018 20751ae08745Sheppo /* 20761ae08745Sheppo * Function: 20771ae08745Sheppo * vdc_send() 20781ae08745Sheppo * 20791ae08745Sheppo * Description: 20801ae08745Sheppo * The function encapsulates the call to write a message using LDC. 20811ae08745Sheppo * If LDC indicates that the call failed due to the queue being full, 208217cadca8Slm66018 * we retry the ldc_write(), otherwise we return the error returned by LDC. 20831ae08745Sheppo * 20841ae08745Sheppo * Arguments: 20851ae08745Sheppo * ldc_handle - LDC handle for the channel this instance of vdc uses 20861ae08745Sheppo * pkt - address of LDC message to be sent 20871ae08745Sheppo * msglen - the size of the message being sent. When the function 20881ae08745Sheppo * returns, this contains the number of bytes written. 20891ae08745Sheppo * 20901ae08745Sheppo * Return Code: 20911ae08745Sheppo * 0 - Success. 20921ae08745Sheppo * EINVAL - pkt or msglen were NULL 20931ae08745Sheppo * ECONNRESET - The connection was not up. 20941ae08745Sheppo * EWOULDBLOCK - LDC queue is full 20951ae08745Sheppo * xxx - other error codes returned by ldc_write 20961ae08745Sheppo */ 20971ae08745Sheppo static int 20980a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen) 20991ae08745Sheppo { 21001ae08745Sheppo size_t size = 0; 21011ae08745Sheppo int status = 0; 21023af08d82Slm66018 clock_t delay_ticks; 21031ae08745Sheppo 21040a55fbb7Slm66018 ASSERT(vdc != NULL); 21050a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 21061ae08745Sheppo ASSERT(msglen != NULL); 21071ae08745Sheppo ASSERT(*msglen != 0); 21081ae08745Sheppo 21093af08d82Slm66018 #ifdef DEBUG 211017cadca8Slm66018 vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt); 21113af08d82Slm66018 #endif 21123af08d82Slm66018 /* 21133af08d82Slm66018 * Wait indefinitely to send if channel 21143af08d82Slm66018 * is busy, but bail out if we succeed or 21153af08d82Slm66018 * if the channel closes or is reset. 21163af08d82Slm66018 */ 21173af08d82Slm66018 delay_ticks = vdc_hz_min_ldc_delay; 21181ae08745Sheppo do { 21191ae08745Sheppo size = *msglen; 21208cd10891Snarayan status = ldc_write(vdc->curr_server->ldc_handle, pkt, &size); 21213af08d82Slm66018 if (status == EWOULDBLOCK) { 21223af08d82Slm66018 delay(delay_ticks); 21233af08d82Slm66018 /* geometric backoff */ 21243af08d82Slm66018 delay_ticks *= 2; 21253af08d82Slm66018 if (delay_ticks > vdc_hz_max_ldc_delay) 21263af08d82Slm66018 delay_ticks = vdc_hz_max_ldc_delay; 21273af08d82Slm66018 } 21283af08d82Slm66018 } while (status == EWOULDBLOCK); 21291ae08745Sheppo 21300a55fbb7Slm66018 /* if LDC had serious issues --- reset vdc state */ 21310a55fbb7Slm66018 if (status == EIO || status == ECONNRESET) { 21323af08d82Slm66018 /* LDC had serious issues --- reset vdc state */ 21333af08d82Slm66018 mutex_enter(&vdc->read_lock); 21343af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 21353af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) 21363af08d82Slm66018 cv_signal(&vdc->read_cv); 21373af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 21383af08d82Slm66018 mutex_exit(&vdc->read_lock); 21393af08d82Slm66018 21403af08d82Slm66018 /* wake up any waiters in the reset thread */ 21413af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 21423af08d82Slm66018 DMSG(vdc, 0, "[%d] write reset - " 21433af08d82Slm66018 "vdc is resetting ..\n", vdc->instance); 21443af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 21453af08d82Slm66018 cv_signal(&vdc->initwait_cv); 21463af08d82Slm66018 } 21473af08d82Slm66018 21483af08d82Slm66018 return (ECONNRESET); 21490a55fbb7Slm66018 } 21500a55fbb7Slm66018 21511ae08745Sheppo /* return the last size written */ 21521ae08745Sheppo *msglen = size; 21531ae08745Sheppo 21541ae08745Sheppo return (status); 21551ae08745Sheppo } 21561ae08745Sheppo 21571ae08745Sheppo /* 21581ae08745Sheppo * Function: 2159655fd6a9Sachartre * vdc_get_md_node 21601ae08745Sheppo * 21611ae08745Sheppo * Description: 21628cd10891Snarayan * Get the MD, the device node for the given disk instance. The 21638cd10891Snarayan * caller is responsible for cleaning up the reference to the 21648cd10891Snarayan * returned MD (mdpp) by calling md_fini_handle(). 21651ae08745Sheppo * 21661ae08745Sheppo * Arguments: 21671ae08745Sheppo * dip - dev info pointer for this instance of the device driver. 2168655fd6a9Sachartre * mdpp - the returned MD. 2169655fd6a9Sachartre * vd_nodep - the returned device node. 21701ae08745Sheppo * 21711ae08745Sheppo * Return Code: 21721ae08745Sheppo * 0 - Success. 21731ae08745Sheppo * ENOENT - Expected node or property did not exist. 21741ae08745Sheppo * ENXIO - Unexpected error communicating with MD framework 21751ae08745Sheppo */ 21761ae08745Sheppo static int 21778cd10891Snarayan vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep) 21781ae08745Sheppo { 21791ae08745Sheppo int status = ENOENT; 21801ae08745Sheppo char *node_name = NULL; 21811ae08745Sheppo md_t *mdp = NULL; 21821ae08745Sheppo int num_nodes; 21831ae08745Sheppo int num_vdevs; 21841ae08745Sheppo mde_cookie_t rootnode; 21851ae08745Sheppo mde_cookie_t *listp = NULL; 21861ae08745Sheppo boolean_t found_inst = B_FALSE; 21871ae08745Sheppo int listsz; 21881ae08745Sheppo int idx; 21891ae08745Sheppo uint64_t md_inst; 21901ae08745Sheppo int obp_inst; 21911ae08745Sheppo int instance = ddi_get_instance(dip); 21921ae08745Sheppo 21931ae08745Sheppo /* 21941ae08745Sheppo * Get the OBP instance number for comparison with the MD instance 21951ae08745Sheppo * 21961ae08745Sheppo * The "cfg-handle" property of a vdc node in an MD contains the MD's 21971ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 21981ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 21991ae08745Sheppo * the "reg" property on the node in the device tree it builds from 22001ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 22011ae08745Sheppo * "reg" property value to uniquely identify this device instance. 22021ae08745Sheppo * If the "reg" property cannot be found, the device tree state is 22031ae08745Sheppo * presumably so broken that there is no point in continuing. 22041ae08745Sheppo */ 22051ae08745Sheppo if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) { 22061ae08745Sheppo cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG); 22071ae08745Sheppo return (ENOENT); 22081ae08745Sheppo } 22091ae08745Sheppo obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 22101ae08745Sheppo OBP_REG, -1); 22113af08d82Slm66018 DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst); 22121ae08745Sheppo 22131ae08745Sheppo /* 2214655fd6a9Sachartre * We now walk the MD nodes to find the node for this vdisk. 22151ae08745Sheppo */ 22161ae08745Sheppo if ((mdp = md_get_handle()) == NULL) { 22171ae08745Sheppo cmn_err(CE_WARN, "unable to init machine description"); 22181ae08745Sheppo return (ENXIO); 22191ae08745Sheppo } 22201ae08745Sheppo 22211ae08745Sheppo num_nodes = md_node_count(mdp); 22221ae08745Sheppo ASSERT(num_nodes > 0); 22231ae08745Sheppo 22241ae08745Sheppo listsz = num_nodes * sizeof (mde_cookie_t); 22251ae08745Sheppo 22261ae08745Sheppo /* allocate memory for nodes */ 22271ae08745Sheppo listp = kmem_zalloc(listsz, KM_SLEEP); 22281ae08745Sheppo 22291ae08745Sheppo rootnode = md_root_node(mdp); 22301ae08745Sheppo ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE); 22311ae08745Sheppo 22321ae08745Sheppo /* 22331ae08745Sheppo * Search for all the virtual devices, we will then check to see which 22341ae08745Sheppo * ones are disk nodes. 22351ae08745Sheppo */ 22361ae08745Sheppo num_vdevs = md_scan_dag(mdp, rootnode, 22371ae08745Sheppo md_find_name(mdp, VDC_MD_VDEV_NAME), 22381ae08745Sheppo md_find_name(mdp, "fwd"), listp); 22391ae08745Sheppo 22401ae08745Sheppo if (num_vdevs <= 0) { 22411ae08745Sheppo cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME); 22421ae08745Sheppo status = ENOENT; 22431ae08745Sheppo goto done; 22441ae08745Sheppo } 22451ae08745Sheppo 22463af08d82Slm66018 DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs); 22471ae08745Sheppo for (idx = 0; idx < num_vdevs; idx++) { 22481ae08745Sheppo status = md_get_prop_str(mdp, listp[idx], "name", &node_name); 22491ae08745Sheppo if ((status != 0) || (node_name == NULL)) { 22501ae08745Sheppo cmn_err(CE_NOTE, "Unable to get name of node type '%s'" 22511ae08745Sheppo ": err %d", VDC_MD_VDEV_NAME, status); 22521ae08745Sheppo continue; 22531ae08745Sheppo } 22541ae08745Sheppo 22553af08d82Slm66018 DMSGX(1, "[%d] Found node '%s'\n", instance, node_name); 22561ae08745Sheppo if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) { 22571ae08745Sheppo status = md_get_prop_val(mdp, listp[idx], 22581ae08745Sheppo VDC_MD_CFG_HDL, &md_inst); 22593af08d82Slm66018 DMSGX(1, "[%d] vdc inst in MD=%lx\n", 22603af08d82Slm66018 instance, md_inst); 22611ae08745Sheppo if ((status == 0) && (md_inst == obp_inst)) { 22621ae08745Sheppo found_inst = B_TRUE; 22631ae08745Sheppo break; 22641ae08745Sheppo } 22651ae08745Sheppo } 22661ae08745Sheppo } 22671ae08745Sheppo 22680a55fbb7Slm66018 if (!found_inst) { 22693af08d82Slm66018 DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME); 22701ae08745Sheppo status = ENOENT; 22711ae08745Sheppo goto done; 22721ae08745Sheppo } 22733af08d82Slm66018 DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst); 22741ae08745Sheppo 2275655fd6a9Sachartre *vd_nodep = listp[idx]; 2276655fd6a9Sachartre *mdpp = mdp; 2277655fd6a9Sachartre done: 2278655fd6a9Sachartre kmem_free(listp, listsz); 2279655fd6a9Sachartre return (status); 2280655fd6a9Sachartre } 2281655fd6a9Sachartre 2282655fd6a9Sachartre /* 2283655fd6a9Sachartre * Function: 22848cd10891Snarayan * vdc_init_ports 2285655fd6a9Sachartre * 2286655fd6a9Sachartre * Description: 22878cd10891Snarayan * Initialize all the ports for this vdisk instance. 2288655fd6a9Sachartre * 2289655fd6a9Sachartre * Arguments: 22908cd10891Snarayan * vdc - soft state pointer for this instance of the device driver. 22918cd10891Snarayan * mdp - md pointer 22928cd10891Snarayan * vd_nodep - device md node. 2293655fd6a9Sachartre * 2294655fd6a9Sachartre * Return Code: 2295655fd6a9Sachartre * 0 - Success. 2296655fd6a9Sachartre * ENOENT - Expected node or property did not exist. 2297655fd6a9Sachartre */ 2298655fd6a9Sachartre static int 22998cd10891Snarayan vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep) 2300655fd6a9Sachartre { 2301655fd6a9Sachartre int status = 0; 23028cd10891Snarayan int idx; 23038cd10891Snarayan int num_nodes; 23048cd10891Snarayan int num_vports; 23058cd10891Snarayan int num_chans; 23068cd10891Snarayan int listsz; 23078cd10891Snarayan mde_cookie_t vd_port; 23088cd10891Snarayan mde_cookie_t *chanp = NULL; 23098cd10891Snarayan mde_cookie_t *portp = NULL; 23108cd10891Snarayan vdc_server_t *srvr; 23118cd10891Snarayan vdc_server_t *prev_srvr = NULL; 2312655fd6a9Sachartre 23138cd10891Snarayan /* 23148cd10891Snarayan * We now walk the MD nodes to find the port nodes for this vdisk. 23158cd10891Snarayan */ 2316655fd6a9Sachartre num_nodes = md_node_count(mdp); 2317655fd6a9Sachartre ASSERT(num_nodes > 0); 2318655fd6a9Sachartre 2319655fd6a9Sachartre listsz = num_nodes * sizeof (mde_cookie_t); 2320655fd6a9Sachartre 2321655fd6a9Sachartre /* allocate memory for nodes */ 23228cd10891Snarayan portp = kmem_zalloc(listsz, KM_SLEEP); 2323655fd6a9Sachartre chanp = kmem_zalloc(listsz, KM_SLEEP); 2324655fd6a9Sachartre 23258cd10891Snarayan num_vports = md_scan_dag(mdp, vd_nodep, 23268cd10891Snarayan md_find_name(mdp, VDC_MD_PORT_NAME), 23278cd10891Snarayan md_find_name(mdp, "fwd"), portp); 23288cd10891Snarayan if (num_vports == 0) { 23298cd10891Snarayan DMSGX(0, "Found no '%s' node for '%s' port\n", 23308cd10891Snarayan VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME); 23318cd10891Snarayan status = ENOENT; 23328cd10891Snarayan goto done; 23338cd10891Snarayan } 23348cd10891Snarayan 23358cd10891Snarayan DMSGX(1, "Found %d '%s' node(s) for '%s' port\n", 23368cd10891Snarayan num_vports, VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME); 23378cd10891Snarayan 23388cd10891Snarayan vdc->num_servers = 0; 23398cd10891Snarayan for (idx = 0; idx < num_vports; idx++) { 23408cd10891Snarayan 23418cd10891Snarayan /* initialize this port */ 23428cd10891Snarayan vd_port = portp[idx]; 23438cd10891Snarayan srvr = kmem_zalloc(sizeof (vdc_server_t), KM_SLEEP); 23448cd10891Snarayan srvr->vdcp = vdc; 23458cd10891Snarayan 23468cd10891Snarayan /* get port id */ 23478cd10891Snarayan if (md_get_prop_val(mdp, vd_port, VDC_MD_ID, &srvr->id) != 0) { 23488cd10891Snarayan cmn_err(CE_NOTE, "vDisk port '%s' property not found", 23498cd10891Snarayan VDC_MD_ID); 23508cd10891Snarayan kmem_free(srvr, sizeof (vdc_server_t)); 23518cd10891Snarayan continue; 23528cd10891Snarayan } 23538cd10891Snarayan 23548cd10891Snarayan /* set the connection timeout */ 23558cd10891Snarayan if (md_get_prop_val(mdp, vd_port, VDC_MD_TIMEOUT, 23568cd10891Snarayan &srvr->ctimeout) != 0) { 23578cd10891Snarayan srvr->ctimeout = 0; 23588cd10891Snarayan } 23598cd10891Snarayan 23608cd10891Snarayan /* get the ldc id */ 23618cd10891Snarayan num_chans = md_scan_dag(mdp, vd_port, 23621ae08745Sheppo md_find_name(mdp, VDC_MD_CHAN_NAME), 23631ae08745Sheppo md_find_name(mdp, "fwd"), chanp); 23641ae08745Sheppo 23651ae08745Sheppo /* expecting at least one channel */ 23661ae08745Sheppo if (num_chans <= 0) { 23671ae08745Sheppo cmn_err(CE_NOTE, "No '%s' node for '%s' port", 23681ae08745Sheppo VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME); 23698cd10891Snarayan kmem_free(srvr, sizeof (vdc_server_t)); 23708cd10891Snarayan continue; 23711ae08745Sheppo } else if (num_chans != 1) { 23728cd10891Snarayan DMSGX(0, "Expected 1 '%s' node for '%s' port, " 23738cd10891Snarayan "found %d\n", VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME, 23748cd10891Snarayan num_chans); 23751ae08745Sheppo } 23761ae08745Sheppo 23771ae08745Sheppo /* 23781ae08745Sheppo * We use the first channel found (index 0), irrespective of how 23791ae08745Sheppo * many are there in total. 23801ae08745Sheppo */ 23818cd10891Snarayan if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID, 23828cd10891Snarayan &srvr->ldc_id) != 0) { 23838cd10891Snarayan cmn_err(CE_NOTE, "Channel '%s' property not found", 23848cd10891Snarayan VDC_MD_ID); 23858cd10891Snarayan kmem_free(srvr, sizeof (vdc_server_t)); 23868cd10891Snarayan continue; 23878cd10891Snarayan } 23888cd10891Snarayan 23898cd10891Snarayan /* 23908cd10891Snarayan * now initialise LDC channel which will be used to 23918cd10891Snarayan * communicate with this server 23928cd10891Snarayan */ 23938cd10891Snarayan if (vdc_do_ldc_init(vdc, srvr) != 0) { 23948cd10891Snarayan kmem_free(srvr, sizeof (vdc_server_t)); 23958cd10891Snarayan continue; 23968cd10891Snarayan } 23978cd10891Snarayan 23988cd10891Snarayan /* add server to list */ 2399d7400d00Sachartre if (prev_srvr) 24008cd10891Snarayan prev_srvr->next = srvr; 2401d7400d00Sachartre else 24028cd10891Snarayan vdc->server_list = srvr; 2403d7400d00Sachartre 24048cd10891Snarayan prev_srvr = srvr; 24058cd10891Snarayan 24068cd10891Snarayan /* inc numbers of servers */ 24078cd10891Snarayan vdc->num_servers++; 24088cd10891Snarayan } 24098cd10891Snarayan 24108cd10891Snarayan /* 24118cd10891Snarayan * Adjust the max number of handshake retries to match 24128cd10891Snarayan * the number of vdisk servers. 24138cd10891Snarayan */ 24148cd10891Snarayan if (vdc_hshake_retries < vdc->num_servers) 24158cd10891Snarayan vdc_hshake_retries = vdc->num_servers; 24168cd10891Snarayan 24178cd10891Snarayan /* pick first server as current server */ 24188cd10891Snarayan if (vdc->server_list != NULL) { 24198cd10891Snarayan vdc->curr_server = vdc->server_list; 24208cd10891Snarayan status = 0; 24218cd10891Snarayan } else { 24221ae08745Sheppo status = ENOENT; 24231ae08745Sheppo } 24241ae08745Sheppo 24251ae08745Sheppo done: 24261ae08745Sheppo kmem_free(chanp, listsz); 24278cd10891Snarayan kmem_free(portp, listsz); 24281ae08745Sheppo return (status); 24291ae08745Sheppo } 24301ae08745Sheppo 24318cd10891Snarayan 24328cd10891Snarayan /* 24338cd10891Snarayan * Function: 24348cd10891Snarayan * vdc_do_ldc_up 24358cd10891Snarayan * 24368cd10891Snarayan * Description: 24378cd10891Snarayan * Bring the channel for the current server up. 24388cd10891Snarayan * 24398cd10891Snarayan * Arguments: 24408cd10891Snarayan * vdc - soft state pointer for this instance of the device driver. 24418cd10891Snarayan * 24428cd10891Snarayan * Return Code: 24438cd10891Snarayan * 0 - Success. 24448cd10891Snarayan * EINVAL - Driver is detaching / LDC error 24458cd10891Snarayan * ECONNREFUSED - Other end is not listening 24468cd10891Snarayan */ 24470a55fbb7Slm66018 static int 24480a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc) 24490a55fbb7Slm66018 { 24500a55fbb7Slm66018 int status; 24513af08d82Slm66018 ldc_status_t ldc_state; 24520a55fbb7Slm66018 24538cd10891Snarayan ASSERT(MUTEX_HELD(&vdc->lock)); 24548cd10891Snarayan 24553af08d82Slm66018 DMSG(vdc, 0, "[%d] Bringing up channel %lx\n", 24568cd10891Snarayan vdc->instance, vdc->curr_server->ldc_id); 24573af08d82Slm66018 24583af08d82Slm66018 if (vdc->lifecycle == VDC_LC_DETACHING) 24593af08d82Slm66018 return (EINVAL); 24600a55fbb7Slm66018 24618cd10891Snarayan if ((status = ldc_up(vdc->curr_server->ldc_handle)) != 0) { 24620a55fbb7Slm66018 switch (status) { 24630a55fbb7Slm66018 case ECONNREFUSED: /* listener not ready at other end */ 24643af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n", 24658cd10891Snarayan vdc->instance, vdc->curr_server->ldc_id, status); 24660a55fbb7Slm66018 status = 0; 24670a55fbb7Slm66018 break; 24680a55fbb7Slm66018 default: 24693af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to bring up LDC: " 24708cd10891Snarayan "channel=%ld, err=%d", vdc->instance, 24718cd10891Snarayan vdc->curr_server->ldc_id, status); 24723af08d82Slm66018 break; 24733af08d82Slm66018 } 24743af08d82Slm66018 } 24753af08d82Slm66018 24768cd10891Snarayan if (ldc_status(vdc->curr_server->ldc_handle, &ldc_state) == 0) { 24778cd10891Snarayan vdc->curr_server->ldc_state = ldc_state; 24783af08d82Slm66018 if (ldc_state == LDC_UP) { 24793af08d82Slm66018 DMSG(vdc, 0, "[%d] LDC channel already up\n", 24803af08d82Slm66018 vdc->instance); 24813af08d82Slm66018 vdc->seq_num = 1; 24823af08d82Slm66018 vdc->seq_num_reply = 0; 24830a55fbb7Slm66018 } 24840a55fbb7Slm66018 } 24850a55fbb7Slm66018 24860a55fbb7Slm66018 return (status); 24870a55fbb7Slm66018 } 24880a55fbb7Slm66018 24890a55fbb7Slm66018 /* 24900a55fbb7Slm66018 * Function: 24910a55fbb7Slm66018 * vdc_terminate_ldc() 24920a55fbb7Slm66018 * 24930a55fbb7Slm66018 * Description: 24940a55fbb7Slm66018 * 24950a55fbb7Slm66018 * Arguments: 24960a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 24978cd10891Snarayan * srvr - vdc per-server info structure 24980a55fbb7Slm66018 * 24990a55fbb7Slm66018 * Return Code: 25000a55fbb7Slm66018 * None 25010a55fbb7Slm66018 */ 25021ae08745Sheppo static void 25038cd10891Snarayan vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr) 25041ae08745Sheppo { 25051ae08745Sheppo int instance = ddi_get_instance(vdc->dip); 25061ae08745Sheppo 25078cd10891Snarayan if (srvr->state & VDC_LDC_OPEN) { 25088cd10891Snarayan DMSG(vdc, 0, "[%d] ldc_close()\n", instance); 25098cd10891Snarayan (void) ldc_close(srvr->ldc_handle); 25108cd10891Snarayan } 25118cd10891Snarayan if (srvr->state & VDC_LDC_CB) { 25128cd10891Snarayan DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance); 25138cd10891Snarayan (void) ldc_unreg_callback(srvr->ldc_handle); 25148cd10891Snarayan } 25158cd10891Snarayan if (srvr->state & VDC_LDC_INIT) { 25168cd10891Snarayan DMSG(vdc, 0, "[%d] ldc_fini()\n", instance); 25178cd10891Snarayan (void) ldc_fini(srvr->ldc_handle); 25188cd10891Snarayan srvr->ldc_handle = NULL; 25198cd10891Snarayan } 25208cd10891Snarayan 25218cd10891Snarayan srvr->state &= ~(VDC_LDC_INIT | VDC_LDC_CB | VDC_LDC_OPEN); 25228cd10891Snarayan } 25238cd10891Snarayan 25248cd10891Snarayan /* 25258cd10891Snarayan * Function: 25268cd10891Snarayan * vdc_fini_ports() 25278cd10891Snarayan * 25288cd10891Snarayan * Description: 25298cd10891Snarayan * Finalize all ports by closing the channel associated with each 25308cd10891Snarayan * port and also freeing the server structure. 25318cd10891Snarayan * 25328cd10891Snarayan * Arguments: 25338cd10891Snarayan * vdc - soft state pointer for this instance of the device driver. 25348cd10891Snarayan * 25358cd10891Snarayan * Return Code: 25368cd10891Snarayan * None 25378cd10891Snarayan */ 25388cd10891Snarayan static void 25398cd10891Snarayan vdc_fini_ports(vdc_t *vdc) 25408cd10891Snarayan { 25418cd10891Snarayan int instance = ddi_get_instance(vdc->dip); 25428cd10891Snarayan vdc_server_t *srvr, *prev_srvr; 25438cd10891Snarayan 25441ae08745Sheppo ASSERT(vdc != NULL); 25451ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 25461ae08745Sheppo 25473af08d82Slm66018 DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized); 25481ae08745Sheppo 25498cd10891Snarayan srvr = vdc->server_list; 25508cd10891Snarayan 25518cd10891Snarayan while (srvr) { 25528cd10891Snarayan 25538cd10891Snarayan vdc_terminate_ldc(vdc, srvr); 25548cd10891Snarayan 25558cd10891Snarayan /* next server */ 25568cd10891Snarayan prev_srvr = srvr; 25578cd10891Snarayan srvr = srvr->next; 25588cd10891Snarayan 25598cd10891Snarayan /* free server */ 25608cd10891Snarayan kmem_free(prev_srvr, sizeof (vdc_server_t)); 25611ae08745Sheppo } 25621ae08745Sheppo 25638cd10891Snarayan vdc->server_list = NULL; 25641ae08745Sheppo } 25651ae08745Sheppo 25661ae08745Sheppo /* -------------------------------------------------------------------------- */ 25671ae08745Sheppo 25681ae08745Sheppo /* 25691ae08745Sheppo * Descriptor Ring helper routines 25701ae08745Sheppo */ 25711ae08745Sheppo 25720a55fbb7Slm66018 /* 25730a55fbb7Slm66018 * Function: 25740a55fbb7Slm66018 * vdc_init_descriptor_ring() 25750a55fbb7Slm66018 * 25760a55fbb7Slm66018 * Description: 25770a55fbb7Slm66018 * 25780a55fbb7Slm66018 * Arguments: 25790a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 25800a55fbb7Slm66018 * 25810a55fbb7Slm66018 * Return Code: 25820a55fbb7Slm66018 * 0 - Success 25830a55fbb7Slm66018 */ 25841ae08745Sheppo static int 25851ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc) 25861ae08745Sheppo { 25871ae08745Sheppo vd_dring_entry_t *dep = NULL; /* DRing Entry pointer */ 25880a55fbb7Slm66018 int status = 0; 25891ae08745Sheppo int i; 25901ae08745Sheppo 25913af08d82Slm66018 DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized); 25921ae08745Sheppo 25931ae08745Sheppo ASSERT(vdc != NULL); 25941ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 25951ae08745Sheppo 2596e1ebb9ecSlm66018 /* ensure we have enough room to store max sized block */ 2597e1ebb9ecSlm66018 ASSERT(maxphys <= VD_MAX_BLOCK_SIZE); 2598e1ebb9ecSlm66018 25990a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_INIT) == 0) { 26003af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance); 2601e1ebb9ecSlm66018 /* 2602e1ebb9ecSlm66018 * Calculate the maximum block size we can transmit using one 2603e1ebb9ecSlm66018 * Descriptor Ring entry from the attributes returned by the 2604e1ebb9ecSlm66018 * vDisk server. This is subject to a minimum of 'maxphys' 2605e1ebb9ecSlm66018 * as we do not have the capability to split requests over 2606e1ebb9ecSlm66018 * multiple DRing entries. 2607e1ebb9ecSlm66018 */ 2608e1ebb9ecSlm66018 if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) { 26093af08d82Slm66018 DMSG(vdc, 0, "[%d] using minimum DRing size\n", 2610e1ebb9ecSlm66018 vdc->instance); 2611e1ebb9ecSlm66018 vdc->dring_max_cookies = maxphys / PAGESIZE; 2612e1ebb9ecSlm66018 } else { 2613e1ebb9ecSlm66018 vdc->dring_max_cookies = 2614e1ebb9ecSlm66018 (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE; 2615e1ebb9ecSlm66018 } 2616e1ebb9ecSlm66018 vdc->dring_entry_size = (sizeof (vd_dring_entry_t) + 2617e1ebb9ecSlm66018 (sizeof (ldc_mem_cookie_t) * 2618e1ebb9ecSlm66018 (vdc->dring_max_cookies - 1))); 2619e1ebb9ecSlm66018 vdc->dring_len = VD_DRING_LEN; 2620e1ebb9ecSlm66018 2621e1ebb9ecSlm66018 status = ldc_mem_dring_create(vdc->dring_len, 26228cd10891Snarayan vdc->dring_entry_size, &vdc->dring_hdl); 26238cd10891Snarayan if ((vdc->dring_hdl == NULL) || (status != 0)) { 26243af08d82Slm66018 DMSG(vdc, 0, "[%d] Descriptor ring creation failed", 2625e1ebb9ecSlm66018 vdc->instance); 26261ae08745Sheppo return (status); 26271ae08745Sheppo } 26280a55fbb7Slm66018 vdc->initialized |= VDC_DRING_INIT; 26290a55fbb7Slm66018 } 26301ae08745Sheppo 26310a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_BOUND) == 0) { 26323af08d82Slm66018 DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance); 26330a55fbb7Slm66018 vdc->dring_cookie = 26340a55fbb7Slm66018 kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP); 26351ae08745Sheppo 26368cd10891Snarayan status = ldc_mem_dring_bind(vdc->curr_server->ldc_handle, 26378cd10891Snarayan vdc->dring_hdl, 26384bac2208Snarayan LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW, 26390a55fbb7Slm66018 &vdc->dring_cookie[0], 26401ae08745Sheppo &vdc->dring_cookie_count); 26411ae08745Sheppo if (status != 0) { 26423af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to bind descriptor ring " 26433af08d82Slm66018 "(%lx) to channel (%lx) status=%d\n", 26448cd10891Snarayan vdc->instance, vdc->dring_hdl, 26458cd10891Snarayan vdc->curr_server->ldc_handle, status); 26461ae08745Sheppo return (status); 26471ae08745Sheppo } 26481ae08745Sheppo ASSERT(vdc->dring_cookie_count == 1); 26491ae08745Sheppo vdc->initialized |= VDC_DRING_BOUND; 26500a55fbb7Slm66018 } 26511ae08745Sheppo 26528cd10891Snarayan status = ldc_mem_dring_info(vdc->dring_hdl, &vdc->dring_mem_info); 26531ae08745Sheppo if (status != 0) { 26543af08d82Slm66018 DMSG(vdc, 0, 26553af08d82Slm66018 "[%d] Failed to get info for descriptor ring (%lx)\n", 26568cd10891Snarayan vdc->instance, vdc->dring_hdl); 26571ae08745Sheppo return (status); 26581ae08745Sheppo } 26591ae08745Sheppo 26600a55fbb7Slm66018 if ((vdc->initialized & VDC_DRING_LOCAL) == 0) { 26613af08d82Slm66018 DMSG(vdc, 0, "[%d] local dring\n", vdc->instance); 26620a55fbb7Slm66018 26631ae08745Sheppo /* Allocate the local copy of this dring */ 26640a55fbb7Slm66018 vdc->local_dring = 2665e1ebb9ecSlm66018 kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t), 26661ae08745Sheppo KM_SLEEP); 26671ae08745Sheppo vdc->initialized |= VDC_DRING_LOCAL; 26680a55fbb7Slm66018 } 26691ae08745Sheppo 26701ae08745Sheppo /* 26710a55fbb7Slm66018 * Mark all DRing entries as free and initialize the private 26720a55fbb7Slm66018 * descriptor's memory handles. If any entry is initialized, 26730a55fbb7Slm66018 * we need to free it later so we set the bit in 'initialized' 26740a55fbb7Slm66018 * at the start. 26751ae08745Sheppo */ 26761ae08745Sheppo vdc->initialized |= VDC_DRING_ENTRY; 2677e1ebb9ecSlm66018 for (i = 0; i < vdc->dring_len; i++) { 26781ae08745Sheppo dep = VDC_GET_DRING_ENTRY_PTR(vdc, i); 26791ae08745Sheppo dep->hdr.dstate = VIO_DESC_FREE; 26801ae08745Sheppo 26818cd10891Snarayan status = ldc_mem_alloc_handle(vdc->curr_server->ldc_handle, 26821ae08745Sheppo &vdc->local_dring[i].desc_mhdl); 26831ae08745Sheppo if (status != 0) { 26843af08d82Slm66018 DMSG(vdc, 0, "![%d] Failed to alloc mem handle for" 26851ae08745Sheppo " descriptor %d", vdc->instance, i); 26861ae08745Sheppo return (status); 26871ae08745Sheppo } 26883af08d82Slm66018 vdc->local_dring[i].is_free = B_TRUE; 26891ae08745Sheppo vdc->local_dring[i].dep = dep; 26901ae08745Sheppo } 26911ae08745Sheppo 26923af08d82Slm66018 /* Initialize the starting index */ 26933af08d82Slm66018 vdc->dring_curr_idx = 0; 26941ae08745Sheppo 26951ae08745Sheppo return (status); 26961ae08745Sheppo } 26971ae08745Sheppo 26980a55fbb7Slm66018 /* 26990a55fbb7Slm66018 * Function: 27000a55fbb7Slm66018 * vdc_destroy_descriptor_ring() 27010a55fbb7Slm66018 * 27020a55fbb7Slm66018 * Description: 27030a55fbb7Slm66018 * 27040a55fbb7Slm66018 * Arguments: 27050a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 27060a55fbb7Slm66018 * 27070a55fbb7Slm66018 * Return Code: 27080a55fbb7Slm66018 * None 27090a55fbb7Slm66018 */ 27101ae08745Sheppo static void 27111ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc) 27121ae08745Sheppo { 27130a55fbb7Slm66018 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 27141ae08745Sheppo ldc_mem_handle_t mhdl = NULL; 27153af08d82Slm66018 ldc_mem_info_t minfo; 27161ae08745Sheppo int status = -1; 27171ae08745Sheppo int i; /* loop */ 27181ae08745Sheppo 27191ae08745Sheppo ASSERT(vdc != NULL); 27201ae08745Sheppo ASSERT(mutex_owned(&vdc->lock)); 27211ae08745Sheppo 27223af08d82Slm66018 DMSG(vdc, 0, "[%d] Entered\n", vdc->instance); 27231ae08745Sheppo 27241ae08745Sheppo if (vdc->initialized & VDC_DRING_ENTRY) { 27253af08d82Slm66018 DMSG(vdc, 0, 27263af08d82Slm66018 "[%d] Removing Local DRing entries\n", vdc->instance); 2727e1ebb9ecSlm66018 for (i = 0; i < vdc->dring_len; i++) { 27280a55fbb7Slm66018 ldep = &vdc->local_dring[i]; 27290a55fbb7Slm66018 mhdl = ldep->desc_mhdl; 27301ae08745Sheppo 27310a55fbb7Slm66018 if (mhdl == NULL) 27320a55fbb7Slm66018 continue; 27330a55fbb7Slm66018 27343af08d82Slm66018 if ((status = ldc_mem_info(mhdl, &minfo)) != 0) { 27353af08d82Slm66018 DMSG(vdc, 0, 27363af08d82Slm66018 "ldc_mem_info returned an error: %d\n", 27373af08d82Slm66018 status); 27383af08d82Slm66018 27393af08d82Slm66018 /* 27403af08d82Slm66018 * This must mean that the mem handle 27413af08d82Slm66018 * is not valid. Clear it out so that 27423af08d82Slm66018 * no one tries to use it. 27433af08d82Slm66018 */ 27443af08d82Slm66018 ldep->desc_mhdl = NULL; 27453af08d82Slm66018 continue; 27463af08d82Slm66018 } 27473af08d82Slm66018 27483af08d82Slm66018 if (minfo.status == LDC_BOUND) { 27493af08d82Slm66018 (void) ldc_mem_unbind_handle(mhdl); 27503af08d82Slm66018 } 27513af08d82Slm66018 27521ae08745Sheppo (void) ldc_mem_free_handle(mhdl); 27533af08d82Slm66018 27543af08d82Slm66018 ldep->desc_mhdl = NULL; 27551ae08745Sheppo } 27561ae08745Sheppo vdc->initialized &= ~VDC_DRING_ENTRY; 27571ae08745Sheppo } 27581ae08745Sheppo 27591ae08745Sheppo if (vdc->initialized & VDC_DRING_LOCAL) { 27603af08d82Slm66018 DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance); 27611ae08745Sheppo kmem_free(vdc->local_dring, 2762e1ebb9ecSlm66018 vdc->dring_len * sizeof (vdc_local_desc_t)); 27631ae08745Sheppo vdc->initialized &= ~VDC_DRING_LOCAL; 27641ae08745Sheppo } 27651ae08745Sheppo 27661ae08745Sheppo if (vdc->initialized & VDC_DRING_BOUND) { 27673af08d82Slm66018 DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance); 27688cd10891Snarayan status = ldc_mem_dring_unbind(vdc->dring_hdl); 27691ae08745Sheppo if (status == 0) { 27701ae08745Sheppo vdc->initialized &= ~VDC_DRING_BOUND; 27711ae08745Sheppo } else { 27723af08d82Slm66018 DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx", 27738cd10891Snarayan vdc->instance, status, vdc->dring_hdl); 27741ae08745Sheppo } 27753af08d82Slm66018 kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t)); 27761ae08745Sheppo } 27771ae08745Sheppo 27781ae08745Sheppo if (vdc->initialized & VDC_DRING_INIT) { 27793af08d82Slm66018 DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance); 27808cd10891Snarayan status = ldc_mem_dring_destroy(vdc->dring_hdl); 27811ae08745Sheppo if (status == 0) { 27828cd10891Snarayan vdc->dring_hdl = NULL; 27831ae08745Sheppo bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t)); 27841ae08745Sheppo vdc->initialized &= ~VDC_DRING_INIT; 27851ae08745Sheppo } else { 27863af08d82Slm66018 DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)", 27878cd10891Snarayan vdc->instance, status, vdc->dring_hdl); 27881ae08745Sheppo } 27891ae08745Sheppo } 27901ae08745Sheppo } 27911ae08745Sheppo 27921ae08745Sheppo /* 27933af08d82Slm66018 * Function: 279490e2f9dcSlm66018 * vdc_map_to_shared_dring() 27951ae08745Sheppo * 27961ae08745Sheppo * Description: 27973af08d82Slm66018 * Copy contents of the local descriptor to the shared 27983af08d82Slm66018 * memory descriptor. 27991ae08745Sheppo * 28003af08d82Slm66018 * Arguments: 28013af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 28023af08d82Slm66018 * idx - descriptor ring index 28033af08d82Slm66018 * 28043af08d82Slm66018 * Return Code: 28053af08d82Slm66018 * None 28061ae08745Sheppo */ 28071ae08745Sheppo static int 28083af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx) 28091ae08745Sheppo { 28103af08d82Slm66018 vdc_local_desc_t *ldep; 28113af08d82Slm66018 vd_dring_entry_t *dep; 28123af08d82Slm66018 int rv; 28131ae08745Sheppo 28143af08d82Slm66018 ldep = &(vdcp->local_dring[idx]); 28151ae08745Sheppo 28163af08d82Slm66018 /* for now leave in the old pop_mem_hdl stuff */ 28173af08d82Slm66018 if (ldep->nbytes > 0) { 28183af08d82Slm66018 rv = vdc_populate_mem_hdl(vdcp, ldep); 28193af08d82Slm66018 if (rv) { 28203af08d82Slm66018 DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n", 28213af08d82Slm66018 vdcp->instance); 28223af08d82Slm66018 return (rv); 28233af08d82Slm66018 } 28243af08d82Slm66018 } 28251ae08745Sheppo 28263af08d82Slm66018 /* 28273af08d82Slm66018 * fill in the data details into the DRing 28283af08d82Slm66018 */ 2829d10e4ef2Snarayan dep = ldep->dep; 28301ae08745Sheppo ASSERT(dep != NULL); 28311ae08745Sheppo 28323af08d82Slm66018 dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp); 28333af08d82Slm66018 dep->payload.operation = ldep->operation; 28343af08d82Slm66018 dep->payload.addr = ldep->offset; 28353af08d82Slm66018 dep->payload.nbytes = ldep->nbytes; 2836055d7c80Scarlsonj dep->payload.status = (uint32_t)-1; /* vds will set valid value */ 28373af08d82Slm66018 dep->payload.slice = ldep->slice; 28383af08d82Slm66018 dep->hdr.dstate = VIO_DESC_READY; 28393af08d82Slm66018 dep->hdr.ack = 1; /* request an ACK for every message */ 28401ae08745Sheppo 28413af08d82Slm66018 return (0); 28421ae08745Sheppo } 28431ae08745Sheppo 28441ae08745Sheppo /* 28451ae08745Sheppo * Function: 28463af08d82Slm66018 * vdc_send_request 28473af08d82Slm66018 * 28483af08d82Slm66018 * Description: 28493af08d82Slm66018 * This routine writes the data to be transmitted to vds into the 28503af08d82Slm66018 * descriptor, notifies vds that the ring has been updated and 28513af08d82Slm66018 * then waits for the request to be processed. 28523af08d82Slm66018 * 28533af08d82Slm66018 * Arguments: 28543af08d82Slm66018 * vdcp - the soft state pointer 28553af08d82Slm66018 * operation - operation we want vds to perform (VD_OP_XXX) 28563af08d82Slm66018 * addr - address of data buf to be read/written. 28573af08d82Slm66018 * nbytes - number of bytes to read/write 28583af08d82Slm66018 * slice - the disk slice this request is for 28593af08d82Slm66018 * offset - relative disk offset 28603af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 28613af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 28623af08d82Slm66018 * . mode for ioctl(9e) 28633af08d82Slm66018 * . LP64 diskaddr_t (block I/O) 28643af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 28653af08d82Slm66018 * 28663af08d82Slm66018 * Return Codes: 28673af08d82Slm66018 * 0 28683af08d82Slm66018 * ENXIO 28693af08d82Slm66018 */ 28703af08d82Slm66018 static int 28713af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr, 28723af08d82Slm66018 size_t nbytes, int slice, diskaddr_t offset, int cb_type, 28733af08d82Slm66018 void *cb_arg, vio_desc_direction_t dir) 28743af08d82Slm66018 { 2875366a92acSlm66018 int rv = 0; 2876366a92acSlm66018 28773af08d82Slm66018 ASSERT(vdcp != NULL); 287887a7269eSachartre ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR); 28793af08d82Slm66018 28803af08d82Slm66018 mutex_enter(&vdcp->lock); 28813af08d82Slm66018 2882366a92acSlm66018 /* 2883366a92acSlm66018 * If this is a block read/write operation we update the I/O statistics 2884366a92acSlm66018 * to indicate that the request is being put on the waitq to be 2885366a92acSlm66018 * serviced. 2886366a92acSlm66018 * 2887366a92acSlm66018 * We do it here (a common routine for both synchronous and strategy 2888366a92acSlm66018 * calls) for performance reasons - we are already holding vdc->lock 2889366a92acSlm66018 * so there is no extra locking overhead. We would have to explicitly 2890366a92acSlm66018 * grab the 'lock' mutex to update the stats if we were to do this 2891366a92acSlm66018 * higher up the stack in vdc_strategy() et. al. 2892366a92acSlm66018 */ 2893366a92acSlm66018 if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) { 2894366a92acSlm66018 DTRACE_IO1(start, buf_t *, cb_arg); 289590e2f9dcSlm66018 VD_KSTAT_WAITQ_ENTER(vdcp); 2896366a92acSlm66018 } 2897366a92acSlm66018 28983af08d82Slm66018 do { 28993c96341aSnarayan while (vdcp->state != VDC_STATE_RUNNING) { 29003af08d82Slm66018 29013c96341aSnarayan /* return error if detaching */ 29023c96341aSnarayan if (vdcp->state == VDC_STATE_DETACH) { 2903366a92acSlm66018 rv = ENXIO; 2904366a92acSlm66018 goto done; 29053c96341aSnarayan } 2906655fd6a9Sachartre 2907655fd6a9Sachartre /* fail request if connection timeout is reached */ 2908655fd6a9Sachartre if (vdcp->ctimeout_reached) { 2909366a92acSlm66018 rv = EIO; 2910366a92acSlm66018 goto done; 2911655fd6a9Sachartre } 2912655fd6a9Sachartre 29132f5224aeSachartre /* 29142f5224aeSachartre * If we are panicking and the disk is not ready then 29152f5224aeSachartre * we can't send any request because we can't complete 29162f5224aeSachartre * the handshake now. 29172f5224aeSachartre */ 29182f5224aeSachartre if (ddi_in_panic()) { 2919366a92acSlm66018 rv = EIO; 2920366a92acSlm66018 goto done; 29212f5224aeSachartre } 29222f5224aeSachartre 2923655fd6a9Sachartre cv_wait(&vdcp->running_cv, &vdcp->lock); 29243c96341aSnarayan } 29253c96341aSnarayan 29263af08d82Slm66018 } while (vdc_populate_descriptor(vdcp, operation, addr, 29273af08d82Slm66018 nbytes, slice, offset, cb_type, cb_arg, dir)); 29283af08d82Slm66018 2929366a92acSlm66018 done: 2930366a92acSlm66018 /* 2931366a92acSlm66018 * If this is a block read/write we update the I/O statistics kstat 2932366a92acSlm66018 * to indicate that this request has been placed on the queue for 2933366a92acSlm66018 * processing (i.e sent to the vDisk server) - iostat(1M) will 2934366a92acSlm66018 * report the time waiting for the vDisk server under the %b column 2935366a92acSlm66018 * In the case of an error we simply take it off the wait queue. 2936366a92acSlm66018 */ 2937366a92acSlm66018 if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) { 2938366a92acSlm66018 if (rv == 0) { 293990e2f9dcSlm66018 VD_KSTAT_WAITQ_TO_RUNQ(vdcp); 2940366a92acSlm66018 DTRACE_PROBE1(send, buf_t *, cb_arg); 2941366a92acSlm66018 } else { 2942366a92acSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_transerrs); 294390e2f9dcSlm66018 VD_KSTAT_WAITQ_EXIT(vdcp); 2944366a92acSlm66018 DTRACE_IO1(done, buf_t *, cb_arg); 2945366a92acSlm66018 } 2946366a92acSlm66018 } 2947366a92acSlm66018 29483af08d82Slm66018 mutex_exit(&vdcp->lock); 2949366a92acSlm66018 2950366a92acSlm66018 return (rv); 29513af08d82Slm66018 } 29523af08d82Slm66018 29533af08d82Slm66018 29543af08d82Slm66018 /* 29553af08d82Slm66018 * Function: 29561ae08745Sheppo * vdc_populate_descriptor 29571ae08745Sheppo * 29581ae08745Sheppo * Description: 29591ae08745Sheppo * This routine writes the data to be transmitted to vds into the 29601ae08745Sheppo * descriptor, notifies vds that the ring has been updated and 29611ae08745Sheppo * then waits for the request to be processed. 29621ae08745Sheppo * 29631ae08745Sheppo * Arguments: 29643af08d82Slm66018 * vdcp - the soft state pointer 29651ae08745Sheppo * operation - operation we want vds to perform (VD_OP_XXX) 29663af08d82Slm66018 * addr - address of data buf to be read/written. 29673af08d82Slm66018 * nbytes - number of bytes to read/write 29683af08d82Slm66018 * slice - the disk slice this request is for 29693af08d82Slm66018 * offset - relative disk offset 29703af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 29713af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 29721ae08745Sheppo * . mode for ioctl(9e) 29731ae08745Sheppo * . LP64 diskaddr_t (block I/O) 29743af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 29751ae08745Sheppo * 29761ae08745Sheppo * Return Codes: 29771ae08745Sheppo * 0 29781ae08745Sheppo * EAGAIN 297917cadca8Slm66018 * ECONNRESET 29801ae08745Sheppo * ENXIO 29811ae08745Sheppo */ 29821ae08745Sheppo static int 29833af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr, 29843af08d82Slm66018 size_t nbytes, int slice, diskaddr_t offset, int cb_type, 29853af08d82Slm66018 void *cb_arg, vio_desc_direction_t dir) 29861ae08745Sheppo { 29873af08d82Slm66018 vdc_local_desc_t *local_dep = NULL; /* Local Dring Pointer */ 29883af08d82Slm66018 int idx; /* Index of DRing entry used */ 29893af08d82Slm66018 int next_idx; 29901ae08745Sheppo vio_dring_msg_t dmsg; 29913af08d82Slm66018 size_t msglen; 29928e6a2a04Slm66018 int rv; 29931ae08745Sheppo 29943af08d82Slm66018 ASSERT(MUTEX_HELD(&vdcp->lock)); 29953af08d82Slm66018 vdcp->threads_pending++; 29963af08d82Slm66018 loop: 29973af08d82Slm66018 DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx); 29981ae08745Sheppo 29993af08d82Slm66018 /* Get next available D-Ring entry */ 30003af08d82Slm66018 idx = vdcp->dring_curr_idx; 30013af08d82Slm66018 local_dep = &(vdcp->local_dring[idx]); 30021ae08745Sheppo 30033af08d82Slm66018 if (!local_dep->is_free) { 30043af08d82Slm66018 DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n", 30053af08d82Slm66018 vdcp->instance); 30063af08d82Slm66018 cv_wait(&vdcp->dring_free_cv, &vdcp->lock); 30073af08d82Slm66018 if (vdcp->state == VDC_STATE_RUNNING || 30083af08d82Slm66018 vdcp->state == VDC_STATE_HANDLE_PENDING) { 30093af08d82Slm66018 goto loop; 30103af08d82Slm66018 } 30113af08d82Slm66018 vdcp->threads_pending--; 30123af08d82Slm66018 return (ECONNRESET); 30131ae08745Sheppo } 30141ae08745Sheppo 30153af08d82Slm66018 next_idx = idx + 1; 30163af08d82Slm66018 if (next_idx >= vdcp->dring_len) 30173af08d82Slm66018 next_idx = 0; 30183af08d82Slm66018 vdcp->dring_curr_idx = next_idx; 30191ae08745Sheppo 30203af08d82Slm66018 ASSERT(local_dep->is_free); 30211ae08745Sheppo 30223af08d82Slm66018 local_dep->operation = operation; 3023d10e4ef2Snarayan local_dep->addr = addr; 30243af08d82Slm66018 local_dep->nbytes = nbytes; 30253af08d82Slm66018 local_dep->slice = slice; 30263af08d82Slm66018 local_dep->offset = offset; 30273af08d82Slm66018 local_dep->cb_type = cb_type; 30283af08d82Slm66018 local_dep->cb_arg = cb_arg; 30293af08d82Slm66018 local_dep->dir = dir; 30303af08d82Slm66018 30313af08d82Slm66018 local_dep->is_free = B_FALSE; 30323af08d82Slm66018 30333af08d82Slm66018 rv = vdc_map_to_shared_dring(vdcp, idx); 30343af08d82Slm66018 if (rv) { 30353af08d82Slm66018 DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n", 30363af08d82Slm66018 vdcp->instance); 30373af08d82Slm66018 /* free the descriptor */ 30383af08d82Slm66018 local_dep->is_free = B_TRUE; 30393af08d82Slm66018 vdcp->dring_curr_idx = idx; 30403af08d82Slm66018 cv_wait(&vdcp->membind_cv, &vdcp->lock); 30413af08d82Slm66018 if (vdcp->state == VDC_STATE_RUNNING || 30423af08d82Slm66018 vdcp->state == VDC_STATE_HANDLE_PENDING) { 30433af08d82Slm66018 goto loop; 30441ae08745Sheppo } 30453af08d82Slm66018 vdcp->threads_pending--; 30463af08d82Slm66018 return (ECONNRESET); 30471ae08745Sheppo } 30481ae08745Sheppo 30491ae08745Sheppo /* 30501ae08745Sheppo * Send a msg with the DRing details to vds 30511ae08745Sheppo */ 30521ae08745Sheppo VIO_INIT_DRING_DATA_TAG(dmsg); 30533af08d82Slm66018 VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp); 30543af08d82Slm66018 dmsg.dring_ident = vdcp->dring_ident; 30551ae08745Sheppo dmsg.start_idx = idx; 30561ae08745Sheppo dmsg.end_idx = idx; 30573af08d82Slm66018 vdcp->seq_num++; 30581ae08745Sheppo 3059366a92acSlm66018 DTRACE_PROBE2(populate, int, vdcp->instance, 3060366a92acSlm66018 vdc_local_desc_t *, local_dep); 30613af08d82Slm66018 DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n", 30623af08d82Slm66018 vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num); 30631ae08745Sheppo 30643af08d82Slm66018 /* 30653af08d82Slm66018 * note we're still holding the lock here to 30663af08d82Slm66018 * make sure the message goes out in order !!!... 30673af08d82Slm66018 */ 30683af08d82Slm66018 msglen = sizeof (dmsg); 30693af08d82Slm66018 rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen); 30703af08d82Slm66018 switch (rv) { 30713af08d82Slm66018 case ECONNRESET: 30723af08d82Slm66018 /* 30733af08d82Slm66018 * vdc_send initiates the reset on failure. 30743af08d82Slm66018 * Since the transaction has already been put 30753af08d82Slm66018 * on the local dring, it will automatically get 30763af08d82Slm66018 * retried when the channel is reset. Given that, 30773af08d82Slm66018 * it is ok to just return success even though the 30783af08d82Slm66018 * send failed. 30793af08d82Slm66018 */ 30803af08d82Slm66018 rv = 0; 30813af08d82Slm66018 break; 3082d10e4ef2Snarayan 30833af08d82Slm66018 case 0: /* EOK */ 30843af08d82Slm66018 DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv); 30853af08d82Slm66018 break; 3086d10e4ef2Snarayan 30873af08d82Slm66018 default: 30883af08d82Slm66018 goto cleanup_and_exit; 30893af08d82Slm66018 } 3090e1ebb9ecSlm66018 30913af08d82Slm66018 vdcp->threads_pending--; 30923af08d82Slm66018 return (rv); 30933af08d82Slm66018 30943af08d82Slm66018 cleanup_and_exit: 30953af08d82Slm66018 DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv); 30963af08d82Slm66018 return (ENXIO); 30971ae08745Sheppo } 30981ae08745Sheppo 30991ae08745Sheppo /* 31003af08d82Slm66018 * Function: 31013af08d82Slm66018 * vdc_do_sync_op 31023af08d82Slm66018 * 31033af08d82Slm66018 * Description: 31043af08d82Slm66018 * Wrapper around vdc_populate_descriptor that blocks until the 31053af08d82Slm66018 * response to the message is available. 31063af08d82Slm66018 * 31073af08d82Slm66018 * Arguments: 31083af08d82Slm66018 * vdcp - the soft state pointer 31093af08d82Slm66018 * operation - operation we want vds to perform (VD_OP_XXX) 31103af08d82Slm66018 * addr - address of data buf to be read/written. 31113af08d82Slm66018 * nbytes - number of bytes to read/write 31123af08d82Slm66018 * slice - the disk slice this request is for 31133af08d82Slm66018 * offset - relative disk offset 31143af08d82Slm66018 * cb_type - type of call - STRATEGY or SYNC 31153af08d82Slm66018 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 31163af08d82Slm66018 * . mode for ioctl(9e) 31173af08d82Slm66018 * . LP64 diskaddr_t (block I/O) 31183af08d82Slm66018 * dir - direction of operation (READ/WRITE/BOTH) 31192f5224aeSachartre * rconflict - check for reservation conflict in case of failure 31202f5224aeSachartre * 31212f5224aeSachartre * rconflict should be set to B_TRUE by most callers. Callers invoking the 31222f5224aeSachartre * VD_OP_SCSICMD operation can set rconflict to B_FALSE if they check the 31232f5224aeSachartre * result of a successful operation with vd_scsi_status(). 31243af08d82Slm66018 * 31253af08d82Slm66018 * Return Codes: 31263af08d82Slm66018 * 0 31273af08d82Slm66018 * EAGAIN 31283af08d82Slm66018 * EFAULT 31293af08d82Slm66018 * ENXIO 31303af08d82Slm66018 * EIO 31310a55fbb7Slm66018 */ 31323af08d82Slm66018 static int 31333af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes, 31343af08d82Slm66018 int slice, diskaddr_t offset, int cb_type, void *cb_arg, 31352f5224aeSachartre vio_desc_direction_t dir, boolean_t rconflict) 31363af08d82Slm66018 { 31373af08d82Slm66018 int status; 31382f5224aeSachartre vdc_io_t *vio; 31392f5224aeSachartre boolean_t check_resv_conflict = B_FALSE; 31403af08d82Slm66018 31413af08d82Slm66018 ASSERT(cb_type == CB_SYNC); 31421ae08745Sheppo 31431ae08745Sheppo /* 31443af08d82Slm66018 * Grab the lock, if blocked wait until the server 31453af08d82Slm66018 * response causes us to wake up again. 31463af08d82Slm66018 */ 31473af08d82Slm66018 mutex_enter(&vdcp->lock); 31483af08d82Slm66018 vdcp->sync_op_cnt++; 3149*11f54b6eSAlexandre Chartre while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH) { 3150*11f54b6eSAlexandre Chartre if (ddi_in_panic()) { 3151*11f54b6eSAlexandre Chartre /* don't block if we are panicking */ 3152*11f54b6eSAlexandre Chartre vdcp->sync_op_cnt--; 3153*11f54b6eSAlexandre Chartre mutex_exit(&vdcp->lock); 3154*11f54b6eSAlexandre Chartre return (EIO); 3155*11f54b6eSAlexandre Chartre } else { 31563af08d82Slm66018 cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock); 3157*11f54b6eSAlexandre Chartre } 3158*11f54b6eSAlexandre Chartre } 31593af08d82Slm66018 31603af08d82Slm66018 if (vdcp->state == VDC_STATE_DETACH) { 31613af08d82Slm66018 cv_broadcast(&vdcp->sync_blocked_cv); 31623af08d82Slm66018 vdcp->sync_op_cnt--; 31633af08d82Slm66018 mutex_exit(&vdcp->lock); 31643af08d82Slm66018 return (ENXIO); 31653af08d82Slm66018 } 31663af08d82Slm66018 31673af08d82Slm66018 /* now block anyone other thread entering after us */ 31683af08d82Slm66018 vdcp->sync_op_blocked = B_TRUE; 31693af08d82Slm66018 vdcp->sync_op_pending = B_TRUE; 31703af08d82Slm66018 mutex_exit(&vdcp->lock); 31713af08d82Slm66018 3172655fd6a9Sachartre status = vdc_send_request(vdcp, operation, addr, 31733af08d82Slm66018 nbytes, slice, offset, cb_type, cb_arg, dir); 31743af08d82Slm66018 3175655fd6a9Sachartre mutex_enter(&vdcp->lock); 3176655fd6a9Sachartre 3177655fd6a9Sachartre if (status != 0) { 3178655fd6a9Sachartre vdcp->sync_op_pending = B_FALSE; 3179*11f54b6eSAlexandre Chartre } else if (ddi_in_panic()) { 3180*11f54b6eSAlexandre Chartre if (vdc_drain_response(vdcp, CB_SYNC, NULL) == 0) { 3181*11f54b6eSAlexandre Chartre status = vdcp->sync_op_status; 3182*11f54b6eSAlexandre Chartre } else { 3183*11f54b6eSAlexandre Chartre vdcp->sync_op_pending = B_FALSE; 3184*11f54b6eSAlexandre Chartre status = EIO; 3185*11f54b6eSAlexandre Chartre } 3186655fd6a9Sachartre } else { 31873af08d82Slm66018 /* 31883af08d82Slm66018 * block until our transaction completes. 31893af08d82Slm66018 * Also anyone else waiting also gets to go next. 31903af08d82Slm66018 */ 31913af08d82Slm66018 while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH) 31923af08d82Slm66018 cv_wait(&vdcp->sync_pending_cv, &vdcp->lock); 31933af08d82Slm66018 3194655fd6a9Sachartre DMSG(vdcp, 2, ": operation returned %d\n", 3195655fd6a9Sachartre vdcp->sync_op_status); 31963c96341aSnarayan if (vdcp->state == VDC_STATE_DETACH) { 31973c96341aSnarayan vdcp->sync_op_pending = B_FALSE; 31983af08d82Slm66018 status = ENXIO; 31993c96341aSnarayan } else { 32003af08d82Slm66018 status = vdcp->sync_op_status; 32012f5224aeSachartre if (status != 0 && vdcp->failfast_interval != 0) { 32022f5224aeSachartre /* 32032f5224aeSachartre * Operation has failed and failfast is enabled. 32042f5224aeSachartre * We need to check if the failure is due to a 32052f5224aeSachartre * reservation conflict if this was requested. 32062f5224aeSachartre */ 32072f5224aeSachartre check_resv_conflict = rconflict; 32082f5224aeSachartre } 32092f5224aeSachartre 32103c96341aSnarayan } 3211655fd6a9Sachartre } 32123c96341aSnarayan 32133af08d82Slm66018 vdcp->sync_op_status = 0; 32143af08d82Slm66018 vdcp->sync_op_blocked = B_FALSE; 32153af08d82Slm66018 vdcp->sync_op_cnt--; 32163af08d82Slm66018 32173af08d82Slm66018 /* signal the next waiting thread */ 32183af08d82Slm66018 cv_signal(&vdcp->sync_blocked_cv); 32192f5224aeSachartre 32202f5224aeSachartre /* 32212f5224aeSachartre * We have to check for reservation conflict after unblocking sync 32222f5224aeSachartre * operations because some sync operations will be used to do this 32232f5224aeSachartre * check. 32242f5224aeSachartre */ 32252f5224aeSachartre if (check_resv_conflict) { 32262f5224aeSachartre vio = vdc_failfast_io_queue(vdcp, NULL); 32272f5224aeSachartre while (vio->vio_qtime != 0) 32282f5224aeSachartre cv_wait(&vdcp->failfast_io_cv, &vdcp->lock); 32292f5224aeSachartre kmem_free(vio, sizeof (vdc_io_t)); 32302f5224aeSachartre } 32312f5224aeSachartre 32323af08d82Slm66018 mutex_exit(&vdcp->lock); 32333af08d82Slm66018 32343af08d82Slm66018 return (status); 32353af08d82Slm66018 } 32363af08d82Slm66018 32373af08d82Slm66018 32383af08d82Slm66018 /* 32393af08d82Slm66018 * Function: 32403af08d82Slm66018 * vdc_drain_response() 32413af08d82Slm66018 * 32423af08d82Slm66018 * Description: 32431ae08745Sheppo * When a guest is panicking, the completion of requests needs to be 32441ae08745Sheppo * handled differently because interrupts are disabled and vdc 32451ae08745Sheppo * will not get messages. We have to poll for the messages instead. 32463af08d82Slm66018 * 32473c2ebf09Sachartre * Note: since we are panicking we don't implement the io:::done 32483c2ebf09Sachartre * DTrace probe or update the I/O statistics kstats. 3249366a92acSlm66018 * 32503af08d82Slm66018 * Arguments: 32513af08d82Slm66018 * vdc - soft state pointer for this instance of the device driver. 3252*11f54b6eSAlexandre Chartre * cb_type - the type of request we want to drain. If type is CB_SYNC 3253*11f54b6eSAlexandre Chartre * then we drain all responses until we find a CB_SYNC request. 3254*11f54b6eSAlexandre Chartre * If the type is CB_STRATEGY then the behavior depends on the 3255*11f54b6eSAlexandre Chartre * value of the buf argument. 3256*11f54b6eSAlexandre Chartre * buf - if the cb_type argument is CB_SYNC then the buf argument 3257*11f54b6eSAlexandre Chartre * must be NULL. If the cb_type argument is CB_STRATEGY and 3258*11f54b6eSAlexandre Chartre * if buf is NULL then we drain all responses, otherwise we 32593c2ebf09Sachartre * poll until we receive a ACK/NACK for the specific I/O 32603c2ebf09Sachartre * described by buf. 32613af08d82Slm66018 * 32623af08d82Slm66018 * Return Code: 3263*11f54b6eSAlexandre Chartre * 0 - Success. If we were expecting a response to a particular 3264*11f54b6eSAlexandre Chartre * CB_SYNC or CB_STRATEGY request then this means that a 3265*11f54b6eSAlexandre Chartre * response has been received. 32661ae08745Sheppo */ 32673af08d82Slm66018 static int 3268*11f54b6eSAlexandre Chartre vdc_drain_response(vdc_t *vdc, vio_cb_type_t cb_type, struct buf *buf) 32693af08d82Slm66018 { 32703af08d82Slm66018 int rv, idx, retries; 32713af08d82Slm66018 size_t msglen; 32723af08d82Slm66018 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 32733af08d82Slm66018 vio_dring_msg_t dmsg; 32743c2ebf09Sachartre struct buf *mbuf; 3275*11f54b6eSAlexandre Chartre boolean_t ack; 3276*11f54b6eSAlexandre Chartre 3277*11f54b6eSAlexandre Chartre ASSERT(cb_type == CB_STRATEGY || cb_type == CB_SYNC); 32783af08d82Slm66018 32793af08d82Slm66018 mutex_enter(&vdc->lock); 32803af08d82Slm66018 32811ae08745Sheppo retries = 0; 32821ae08745Sheppo for (;;) { 32831ae08745Sheppo msglen = sizeof (dmsg); 32848cd10891Snarayan rv = ldc_read(vdc->curr_server->ldc_handle, (caddr_t)&dmsg, 32858cd10891Snarayan &msglen); 32868e6a2a04Slm66018 if (rv) { 32878e6a2a04Slm66018 rv = EINVAL; 32881ae08745Sheppo break; 32891ae08745Sheppo } 32901ae08745Sheppo 32911ae08745Sheppo /* 32921ae08745Sheppo * if there are no packets wait and check again 32931ae08745Sheppo */ 32948e6a2a04Slm66018 if ((rv == 0) && (msglen == 0)) { 32951ae08745Sheppo if (retries++ > vdc_dump_retries) { 32968e6a2a04Slm66018 rv = EAGAIN; 32971ae08745Sheppo break; 32981ae08745Sheppo } 32991ae08745Sheppo 3300d10e4ef2Snarayan drv_usecwait(vdc_usec_timeout_dump); 33011ae08745Sheppo continue; 33021ae08745Sheppo } 33031ae08745Sheppo 33041ae08745Sheppo /* 33051ae08745Sheppo * Ignore all messages that are not ACKs/NACKs to 33061ae08745Sheppo * DRing requests. 33071ae08745Sheppo */ 33081ae08745Sheppo if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) || 33091ae08745Sheppo (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) { 33103af08d82Slm66018 DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n", 33111ae08745Sheppo dmsg.tag.vio_msgtype, 33121ae08745Sheppo dmsg.tag.vio_subtype, 33131ae08745Sheppo dmsg.tag.vio_subtype_env); 33141ae08745Sheppo continue; 33151ae08745Sheppo } 33161ae08745Sheppo 33171ae08745Sheppo /* 3318*11f54b6eSAlexandre Chartre * Record if the packet was ACK'ed or not. If the packet was not 3319*11f54b6eSAlexandre Chartre * ACK'ed then we will just mark the request as failed; we don't 3320*11f54b6eSAlexandre Chartre * want to reset the connection at this point. 33211ae08745Sheppo */ 33221ae08745Sheppo switch (dmsg.tag.vio_subtype) { 33231ae08745Sheppo case VIO_SUBTYPE_ACK: 3324*11f54b6eSAlexandre Chartre ack = B_TRUE; 33251ae08745Sheppo break; 33261ae08745Sheppo case VIO_SUBTYPE_NACK: 3327*11f54b6eSAlexandre Chartre ack = B_FALSE; 33281ae08745Sheppo break; 33291ae08745Sheppo default: 33301ae08745Sheppo continue; 33311ae08745Sheppo } 33321ae08745Sheppo 33333af08d82Slm66018 idx = dmsg.start_idx; 33343af08d82Slm66018 if (idx >= vdc->dring_len) { 33353af08d82Slm66018 DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n", 3336e1ebb9ecSlm66018 vdc->instance, idx); 33373af08d82Slm66018 continue; 33381ae08745Sheppo } 33393af08d82Slm66018 ldep = &vdc->local_dring[idx]; 33403af08d82Slm66018 if (ldep->dep->hdr.dstate != VIO_DESC_DONE) { 33413af08d82Slm66018 DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n", 33423af08d82Slm66018 vdc->instance, idx, ldep->dep->hdr.dstate); 33431ae08745Sheppo continue; 33441ae08745Sheppo } 33451ae08745Sheppo 3346*11f54b6eSAlexandre Chartre switch (ldep->cb_type) { 3347*11f54b6eSAlexandre Chartre 3348*11f54b6eSAlexandre Chartre case CB_STRATEGY: 33493c2ebf09Sachartre mbuf = ldep->cb_arg; 3350*11f54b6eSAlexandre Chartre if (mbuf != NULL) { 33513c2ebf09Sachartre mbuf->b_resid = mbuf->b_bcount - 33523c2ebf09Sachartre ldep->dep->payload.nbytes; 3353*11f54b6eSAlexandre Chartre bioerror(mbuf, 3354*11f54b6eSAlexandre Chartre ack ? ldep->dep->payload.status : EIO); 33553c2ebf09Sachartre biodone(mbuf); 33563c2ebf09Sachartre } 33573af08d82Slm66018 rv = vdc_depopulate_descriptor(vdc, idx); 33583c2ebf09Sachartre if (buf != NULL && buf == mbuf) { 33593c2ebf09Sachartre rv = 0; 3360*11f54b6eSAlexandre Chartre goto done; 3361*11f54b6eSAlexandre Chartre } 3362*11f54b6eSAlexandre Chartre break; 3363*11f54b6eSAlexandre Chartre 3364*11f54b6eSAlexandre Chartre case CB_SYNC: 3365*11f54b6eSAlexandre Chartre rv = vdc_depopulate_descriptor(vdc, idx); 3366*11f54b6eSAlexandre Chartre vdc->sync_op_status = ack ? rv : EIO; 3367*11f54b6eSAlexandre Chartre vdc->sync_op_pending = B_FALSE; 3368*11f54b6eSAlexandre Chartre cv_signal(&vdc->sync_pending_cv); 3369*11f54b6eSAlexandre Chartre if (cb_type == CB_SYNC) { 3370*11f54b6eSAlexandre Chartre rv = 0; 3371*11f54b6eSAlexandre Chartre goto done; 3372*11f54b6eSAlexandre Chartre } 33733af08d82Slm66018 break; 33743af08d82Slm66018 } 33753af08d82Slm66018 33763c2ebf09Sachartre /* if this is the last descriptor - break out of loop */ 33773c2ebf09Sachartre if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx) { 33783c2ebf09Sachartre /* 3379*11f54b6eSAlexandre Chartre * If we were expecting a response for a particular 3380*11f54b6eSAlexandre Chartre * request then we return with an error otherwise we 3381*11f54b6eSAlexandre Chartre * have successfully completed the drain. 33823c2ebf09Sachartre */ 3383*11f54b6eSAlexandre Chartre rv = (buf != NULL || cb_type == CB_SYNC)? ESRCH: 0; 33843c2ebf09Sachartre break; 33853c2ebf09Sachartre } 33863c2ebf09Sachartre } 33873c2ebf09Sachartre 3388*11f54b6eSAlexandre Chartre done: 33893af08d82Slm66018 mutex_exit(&vdc->lock); 33903af08d82Slm66018 DMSG(vdc, 0, "End idx=%d\n", idx); 33913af08d82Slm66018 33923af08d82Slm66018 return (rv); 33931ae08745Sheppo } 33941ae08745Sheppo 33951ae08745Sheppo 33960a55fbb7Slm66018 /* 33970a55fbb7Slm66018 * Function: 33980a55fbb7Slm66018 * vdc_depopulate_descriptor() 33990a55fbb7Slm66018 * 34000a55fbb7Slm66018 * Description: 34010a55fbb7Slm66018 * 34020a55fbb7Slm66018 * Arguments: 34030a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 34040a55fbb7Slm66018 * idx - Index of the Descriptor Ring entry being modified 34050a55fbb7Slm66018 * 34060a55fbb7Slm66018 * Return Code: 34070a55fbb7Slm66018 * 0 - Success 34080a55fbb7Slm66018 */ 34091ae08745Sheppo static int 34101ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx) 34111ae08745Sheppo { 34121ae08745Sheppo vd_dring_entry_t *dep = NULL; /* Dring Entry Pointer */ 34131ae08745Sheppo vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 34141ae08745Sheppo int status = ENXIO; 34158e6a2a04Slm66018 int rv = 0; 34161ae08745Sheppo 34171ae08745Sheppo ASSERT(vdc != NULL); 3418e1ebb9ecSlm66018 ASSERT(idx < vdc->dring_len); 34191ae08745Sheppo ldep = &vdc->local_dring[idx]; 34201ae08745Sheppo ASSERT(ldep != NULL); 34213af08d82Slm66018 ASSERT(MUTEX_HELD(&vdc->lock)); 34223af08d82Slm66018 3423366a92acSlm66018 DTRACE_PROBE2(depopulate, int, vdc->instance, vdc_local_desc_t *, ldep); 34243af08d82Slm66018 DMSG(vdc, 2, ": idx = %d\n", idx); 3425366a92acSlm66018 34261ae08745Sheppo dep = ldep->dep; 34271ae08745Sheppo ASSERT(dep != NULL); 3428e1ebb9ecSlm66018 ASSERT((dep->hdr.dstate == VIO_DESC_DONE) || 3429e1ebb9ecSlm66018 (dep->payload.status == ECANCELED)); 34301ae08745Sheppo 3431e1ebb9ecSlm66018 VDC_MARK_DRING_ENTRY_FREE(vdc, idx); 34323af08d82Slm66018 34333af08d82Slm66018 ldep->is_free = B_TRUE; 34341ae08745Sheppo status = dep->payload.status; 3435205eeb1aSlm66018 DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status); 34361ae08745Sheppo 3437eff7243fSlm66018 /* 3438eff7243fSlm66018 * If no buffers were used to transfer information to the server when 3439eff7243fSlm66018 * populating the descriptor then no memory handles need to be unbound 3440eff7243fSlm66018 * and we can return now. 3441eff7243fSlm66018 */ 3442eff7243fSlm66018 if (ldep->nbytes == 0) { 3443eff7243fSlm66018 cv_signal(&vdc->dring_free_cv); 34448e6a2a04Slm66018 return (status); 3445eff7243fSlm66018 } 34468e6a2a04Slm66018 34471ae08745Sheppo /* 34481ae08745Sheppo * If the upper layer passed in a misaligned address we copied the 34491ae08745Sheppo * data into an aligned buffer before sending it to LDC - we now 34501ae08745Sheppo * copy it back to the original buffer. 34511ae08745Sheppo */ 34521ae08745Sheppo if (ldep->align_addr) { 34531ae08745Sheppo ASSERT(ldep->addr != NULL); 34541ae08745Sheppo 34553c96341aSnarayan if (dep->payload.nbytes > 0) 34563c96341aSnarayan bcopy(ldep->align_addr, ldep->addr, 34573c96341aSnarayan dep->payload.nbytes); 34581ae08745Sheppo kmem_free(ldep->align_addr, 34593c96341aSnarayan sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8)); 34601ae08745Sheppo ldep->align_addr = NULL; 34611ae08745Sheppo } 34621ae08745Sheppo 34638e6a2a04Slm66018 rv = ldc_mem_unbind_handle(ldep->desc_mhdl); 34648e6a2a04Slm66018 if (rv != 0) { 34653af08d82Slm66018 DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)", 34668e6a2a04Slm66018 vdc->instance, ldep->desc_mhdl, idx, rv); 34678e6a2a04Slm66018 /* 34688e6a2a04Slm66018 * The error returned by the vDisk server is more informative 34698e6a2a04Slm66018 * and thus has a higher priority but if it isn't set we ensure 34708e6a2a04Slm66018 * that this function returns an error. 34718e6a2a04Slm66018 */ 34728e6a2a04Slm66018 if (status == 0) 34738e6a2a04Slm66018 status = EINVAL; 34741ae08745Sheppo } 34751ae08745Sheppo 34763af08d82Slm66018 cv_signal(&vdc->membind_cv); 34773af08d82Slm66018 cv_signal(&vdc->dring_free_cv); 34783af08d82Slm66018 34791ae08745Sheppo return (status); 34801ae08745Sheppo } 34811ae08745Sheppo 34820a55fbb7Slm66018 /* 34830a55fbb7Slm66018 * Function: 34840a55fbb7Slm66018 * vdc_populate_mem_hdl() 34850a55fbb7Slm66018 * 34860a55fbb7Slm66018 * Description: 34870a55fbb7Slm66018 * 34880a55fbb7Slm66018 * Arguments: 34890a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 34900a55fbb7Slm66018 * idx - Index of the Descriptor Ring entry being modified 34910a55fbb7Slm66018 * addr - virtual address being mapped in 34920a55fbb7Slm66018 * nybtes - number of bytes in 'addr' 34930a55fbb7Slm66018 * operation - the vDisk operation being performed (VD_OP_xxx) 34940a55fbb7Slm66018 * 34950a55fbb7Slm66018 * Return Code: 34960a55fbb7Slm66018 * 0 - Success 34970a55fbb7Slm66018 */ 34981ae08745Sheppo static int 34993af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep) 35001ae08745Sheppo { 35011ae08745Sheppo vd_dring_entry_t *dep = NULL; 35021ae08745Sheppo ldc_mem_handle_t mhdl; 35031ae08745Sheppo caddr_t vaddr; 35043af08d82Slm66018 size_t nbytes; 35054bac2208Snarayan uint8_t perm = LDC_MEM_RW; 35064bac2208Snarayan uint8_t maptype; 35071ae08745Sheppo int rv = 0; 35081ae08745Sheppo int i; 35091ae08745Sheppo 35103af08d82Slm66018 ASSERT(vdcp != NULL); 35111ae08745Sheppo 35123af08d82Slm66018 dep = ldep->dep; 35131ae08745Sheppo mhdl = ldep->desc_mhdl; 35141ae08745Sheppo 35153af08d82Slm66018 switch (ldep->dir) { 35163af08d82Slm66018 case VIO_read_dir: 35171ae08745Sheppo perm = LDC_MEM_W; 35181ae08745Sheppo break; 35191ae08745Sheppo 35203af08d82Slm66018 case VIO_write_dir: 35211ae08745Sheppo perm = LDC_MEM_R; 35221ae08745Sheppo break; 35231ae08745Sheppo 35243af08d82Slm66018 case VIO_both_dir: 35251ae08745Sheppo perm = LDC_MEM_RW; 35261ae08745Sheppo break; 35271ae08745Sheppo 35281ae08745Sheppo default: 35291ae08745Sheppo ASSERT(0); /* catch bad programming in vdc */ 35301ae08745Sheppo } 35311ae08745Sheppo 35321ae08745Sheppo /* 35331ae08745Sheppo * LDC expects any addresses passed in to be 8-byte aligned. We need 35341ae08745Sheppo * to copy the contents of any misaligned buffers to a newly allocated 35351ae08745Sheppo * buffer and bind it instead (and copy the the contents back to the 35361ae08745Sheppo * original buffer passed in when depopulating the descriptor) 35371ae08745Sheppo */ 35383af08d82Slm66018 vaddr = ldep->addr; 35393af08d82Slm66018 nbytes = ldep->nbytes; 35403af08d82Slm66018 if (((uint64_t)vaddr & 0x7) != 0) { 3541d10e4ef2Snarayan ASSERT(ldep->align_addr == NULL); 35421ae08745Sheppo ldep->align_addr = 35433af08d82Slm66018 kmem_alloc(sizeof (caddr_t) * 35443af08d82Slm66018 P2ROUNDUP(nbytes, 8), KM_SLEEP); 35453af08d82Slm66018 DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating " 35463af08d82Slm66018 "(buf=%p nb=%ld op=%d)\n", 35473af08d82Slm66018 vdcp->instance, (void *)vaddr, (void *)ldep->align_addr, 35483af08d82Slm66018 nbytes, ldep->operation); 35493af08d82Slm66018 if (perm != LDC_MEM_W) 35503af08d82Slm66018 bcopy(vaddr, ldep->align_addr, nbytes); 35511ae08745Sheppo vaddr = ldep->align_addr; 35521ae08745Sheppo } 35531ae08745Sheppo 35544bac2208Snarayan maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP; 35551ae08745Sheppo rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8), 355687a7269eSachartre maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies); 35573af08d82Slm66018 DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n", 35583af08d82Slm66018 vdcp->instance, dep->payload.ncookies); 35591ae08745Sheppo if (rv != 0) { 35603af08d82Slm66018 DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle " 35613af08d82Slm66018 "(mhdl=%p, buf=%p, err=%d)\n", 35623af08d82Slm66018 vdcp->instance, (void *)mhdl, (void *)vaddr, rv); 35631ae08745Sheppo if (ldep->align_addr) { 35641ae08745Sheppo kmem_free(ldep->align_addr, 3565d10e4ef2Snarayan sizeof (caddr_t) * P2ROUNDUP(nbytes, 8)); 35661ae08745Sheppo ldep->align_addr = NULL; 35671ae08745Sheppo } 35681ae08745Sheppo return (EAGAIN); 35691ae08745Sheppo } 35701ae08745Sheppo 35711ae08745Sheppo /* 35721ae08745Sheppo * Get the other cookies (if any). 35731ae08745Sheppo */ 35741ae08745Sheppo for (i = 1; i < dep->payload.ncookies; i++) { 35751ae08745Sheppo rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]); 35761ae08745Sheppo if (rv != 0) { 35771ae08745Sheppo (void) ldc_mem_unbind_handle(mhdl); 35783af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Failed to get next cookie " 3579e1ebb9ecSlm66018 "(mhdl=%lx cnum=%d), err=%d", 35803af08d82Slm66018 vdcp->instance, mhdl, i, rv); 35811ae08745Sheppo if (ldep->align_addr) { 35821ae08745Sheppo kmem_free(ldep->align_addr, 35833c96341aSnarayan sizeof (caddr_t) * ldep->nbytes); 35841ae08745Sheppo ldep->align_addr = NULL; 35851ae08745Sheppo } 35861ae08745Sheppo return (EAGAIN); 35871ae08745Sheppo } 35881ae08745Sheppo } 35891ae08745Sheppo 35901ae08745Sheppo return (rv); 35911ae08745Sheppo } 35921ae08745Sheppo 35931ae08745Sheppo /* 35941ae08745Sheppo * Interrupt handlers for messages from LDC 35951ae08745Sheppo */ 35961ae08745Sheppo 35970a55fbb7Slm66018 /* 35980a55fbb7Slm66018 * Function: 35990a55fbb7Slm66018 * vdc_handle_cb() 36000a55fbb7Slm66018 * 36010a55fbb7Slm66018 * Description: 36020a55fbb7Slm66018 * 36030a55fbb7Slm66018 * Arguments: 36040a55fbb7Slm66018 * event - Type of event (LDC_EVT_xxx) that triggered the callback 36050a55fbb7Slm66018 * arg - soft state pointer for this instance of the device driver. 36060a55fbb7Slm66018 * 36070a55fbb7Slm66018 * Return Code: 36080a55fbb7Slm66018 * 0 - Success 36090a55fbb7Slm66018 */ 36101ae08745Sheppo static uint_t 36111ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg) 36121ae08745Sheppo { 36131ae08745Sheppo ldc_status_t ldc_state; 36141ae08745Sheppo int rv = 0; 36158cd10891Snarayan vdc_server_t *srvr = (vdc_server_t *)(void *)arg; 36168cd10891Snarayan vdc_t *vdc = srvr->vdcp; 36171ae08745Sheppo 36181ae08745Sheppo ASSERT(vdc != NULL); 36191ae08745Sheppo 36203af08d82Slm66018 DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num); 36211ae08745Sheppo 36228cd10891Snarayan /* If callback is not for the current server, ignore it */ 36238cd10891Snarayan mutex_enter(&vdc->lock); 36248cd10891Snarayan 36258cd10891Snarayan if (vdc->curr_server != srvr) { 36268cd10891Snarayan DMSG(vdc, 0, "[%d] Ignoring event 0x%lx for port@%ld\n", 36278cd10891Snarayan vdc->instance, event, srvr->id); 36288cd10891Snarayan mutex_exit(&vdc->lock); 36298cd10891Snarayan return (LDC_SUCCESS); 36308cd10891Snarayan } 36318cd10891Snarayan 36321ae08745Sheppo /* 36331ae08745Sheppo * Depending on the type of event that triggered this callback, 36343af08d82Slm66018 * we modify the handshake state or read the data. 36351ae08745Sheppo * 36361ae08745Sheppo * NOTE: not done as a switch() as event could be triggered by 36371ae08745Sheppo * a state change and a read request. Also the ordering of the 36381ae08745Sheppo * check for the event types is deliberate. 36391ae08745Sheppo */ 36401ae08745Sheppo if (event & LDC_EVT_UP) { 36413af08d82Slm66018 DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance); 36423af08d82Slm66018 36431ae08745Sheppo /* get LDC state */ 36448cd10891Snarayan rv = ldc_status(srvr->ldc_handle, &ldc_state); 36451ae08745Sheppo if (rv != 0) { 36463af08d82Slm66018 DMSG(vdc, 0, "[%d] Couldn't get LDC status %d", 36471ae08745Sheppo vdc->instance, rv); 36488cd10891Snarayan mutex_exit(&vdc->lock); 36491ae08745Sheppo return (LDC_SUCCESS); 36501ae08745Sheppo } 36518cd10891Snarayan if (srvr->ldc_state != LDC_UP && 36528cd10891Snarayan ldc_state == LDC_UP) { 36531ae08745Sheppo /* 36543af08d82Slm66018 * Reset the transaction sequence numbers when 36553af08d82Slm66018 * LDC comes up. We then kick off the handshake 36563af08d82Slm66018 * negotiation with the vDisk server. 36571ae08745Sheppo */ 36580a55fbb7Slm66018 vdc->seq_num = 1; 36591ae08745Sheppo vdc->seq_num_reply = 0; 36608cd10891Snarayan srvr->ldc_state = ldc_state; 36613af08d82Slm66018 cv_signal(&vdc->initwait_cv); 36623af08d82Slm66018 } 36631ae08745Sheppo } 36641ae08745Sheppo 36651ae08745Sheppo if (event & LDC_EVT_READ) { 366617cadca8Slm66018 DMSG(vdc, 1, "[%d] Received LDC_EVT_READ\n", vdc->instance); 36673af08d82Slm66018 mutex_enter(&vdc->read_lock); 36683af08d82Slm66018 cv_signal(&vdc->read_cv); 36693af08d82Slm66018 vdc->read_state = VDC_READ_PENDING; 36703af08d82Slm66018 mutex_exit(&vdc->read_lock); 36718cd10891Snarayan mutex_exit(&vdc->lock); 36721ae08745Sheppo 36731ae08745Sheppo /* that's all we have to do - no need to handle DOWN/RESET */ 36741ae08745Sheppo return (LDC_SUCCESS); 36751ae08745Sheppo } 36761ae08745Sheppo 36773af08d82Slm66018 if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) { 36780a55fbb7Slm66018 36793af08d82Slm66018 DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance); 36803af08d82Slm66018 36813af08d82Slm66018 /* 36823af08d82Slm66018 * Need to wake up any readers so they will 36833af08d82Slm66018 * detect that a reset has occurred. 36843af08d82Slm66018 */ 36853af08d82Slm66018 mutex_enter(&vdc->read_lock); 36863af08d82Slm66018 if ((vdc->read_state == VDC_READ_WAITING) || 36873af08d82Slm66018 (vdc->read_state == VDC_READ_RESET)) 36883af08d82Slm66018 cv_signal(&vdc->read_cv); 36893af08d82Slm66018 vdc->read_state = VDC_READ_RESET; 36903af08d82Slm66018 mutex_exit(&vdc->read_lock); 36910a55fbb7Slm66018 36923af08d82Slm66018 /* wake up any threads waiting for connection to come up */ 36933af08d82Slm66018 if (vdc->state == VDC_STATE_INIT_WAITING) { 36943af08d82Slm66018 vdc->state = VDC_STATE_RESETTING; 36953af08d82Slm66018 cv_signal(&vdc->initwait_cv); 36961ae08745Sheppo } 36971ae08745Sheppo 36981ae08745Sheppo } 36991ae08745Sheppo 37008cd10891Snarayan mutex_exit(&vdc->lock); 37018cd10891Snarayan 37021ae08745Sheppo if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ)) 37033af08d82Slm66018 DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received", 37041ae08745Sheppo vdc->instance, event); 37051ae08745Sheppo 37061ae08745Sheppo return (LDC_SUCCESS); 37071ae08745Sheppo } 37081ae08745Sheppo 37093af08d82Slm66018 /* 37103af08d82Slm66018 * Function: 37113af08d82Slm66018 * vdc_wait_for_response() 37123af08d82Slm66018 * 37133af08d82Slm66018 * Description: 37143af08d82Slm66018 * Block waiting for a response from the server. If there is 37153af08d82Slm66018 * no data the thread block on the read_cv that is signalled 37163af08d82Slm66018 * by the callback when an EVT_READ occurs. 37173af08d82Slm66018 * 37183af08d82Slm66018 * Arguments: 37193af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 37203af08d82Slm66018 * 37213af08d82Slm66018 * Return Code: 37223af08d82Slm66018 * 0 - Success 37233af08d82Slm66018 */ 37243af08d82Slm66018 static int 37253af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp) 37263af08d82Slm66018 { 37273af08d82Slm66018 size_t nbytes = sizeof (*msgp); 37283af08d82Slm66018 int status; 37293af08d82Slm66018 37303af08d82Slm66018 ASSERT(vdcp != NULL); 37313af08d82Slm66018 37323af08d82Slm66018 DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance); 37333af08d82Slm66018 37343af08d82Slm66018 status = vdc_recv(vdcp, msgp, &nbytes); 37353af08d82Slm66018 DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n", 37363af08d82Slm66018 status, (int)nbytes); 37373af08d82Slm66018 if (status) { 37383af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n", 37393af08d82Slm66018 vdcp->instance, status); 37403af08d82Slm66018 return (status); 37413af08d82Slm66018 } 37423af08d82Slm66018 37433af08d82Slm66018 if (nbytes < sizeof (vio_msg_tag_t)) { 37443af08d82Slm66018 DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n", 37453af08d82Slm66018 vdcp->instance, sizeof (vio_msg_tag_t), nbytes); 37463af08d82Slm66018 return (ENOMSG); 37473af08d82Slm66018 } 37483af08d82Slm66018 37493af08d82Slm66018 DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance, 37503af08d82Slm66018 msgp->tag.vio_msgtype, 37513af08d82Slm66018 msgp->tag.vio_subtype, 37523af08d82Slm66018 msgp->tag.vio_subtype_env); 37533af08d82Slm66018 37543af08d82Slm66018 /* 37553af08d82Slm66018 * Verify the Session ID of the message 37563af08d82Slm66018 * 37573af08d82Slm66018 * Every message after the Version has been negotiated should 37583af08d82Slm66018 * have the correct session ID set. 37593af08d82Slm66018 */ 37603af08d82Slm66018 if ((msgp->tag.vio_sid != vdcp->session_id) && 37613af08d82Slm66018 (msgp->tag.vio_subtype_env != VIO_VER_INFO)) { 37623af08d82Slm66018 DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, " 37633af08d82Slm66018 "expected 0x%lx [seq num %lx @ %d]", 37643af08d82Slm66018 vdcp->instance, msgp->tag.vio_sid, 37653af08d82Slm66018 vdcp->session_id, 37663af08d82Slm66018 ((vio_dring_msg_t *)msgp)->seq_num, 37673af08d82Slm66018 ((vio_dring_msg_t *)msgp)->start_idx); 37683af08d82Slm66018 return (ENOMSG); 37693af08d82Slm66018 } 37703af08d82Slm66018 return (0); 37713af08d82Slm66018 } 37723af08d82Slm66018 37733af08d82Slm66018 37743af08d82Slm66018 /* 37753af08d82Slm66018 * Function: 37763af08d82Slm66018 * vdc_resubmit_backup_dring() 37773af08d82Slm66018 * 37783af08d82Slm66018 * Description: 37793af08d82Slm66018 * Resubmit each descriptor in the backed up dring to 37803af08d82Slm66018 * vDisk server. The Dring was backed up during connection 37813af08d82Slm66018 * reset. 37823af08d82Slm66018 * 37833af08d82Slm66018 * Arguments: 37843af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 37853af08d82Slm66018 * 37863af08d82Slm66018 * Return Code: 37873af08d82Slm66018 * 0 - Success 37883af08d82Slm66018 */ 37893af08d82Slm66018 static int 37903af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp) 37913af08d82Slm66018 { 379290e2f9dcSlm66018 int processed = 0; 37933af08d82Slm66018 int count; 37943af08d82Slm66018 int b_idx; 379590e2f9dcSlm66018 int rv = 0; 37963af08d82Slm66018 int dring_size; 379790e2f9dcSlm66018 int op; 37983af08d82Slm66018 vio_msg_t vio_msg; 37993af08d82Slm66018 vdc_local_desc_t *curr_ldep; 38003af08d82Slm66018 38013af08d82Slm66018 ASSERT(MUTEX_NOT_HELD(&vdcp->lock)); 38023af08d82Slm66018 ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING); 38033af08d82Slm66018 3804655fd6a9Sachartre if (vdcp->local_dring_backup == NULL) { 3805655fd6a9Sachartre /* the pending requests have already been processed */ 3806655fd6a9Sachartre return (0); 3807655fd6a9Sachartre } 3808655fd6a9Sachartre 38093af08d82Slm66018 DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n", 38103af08d82Slm66018 vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail); 38113af08d82Slm66018 38123af08d82Slm66018 /* 38133af08d82Slm66018 * Walk the backup copy of the local descriptor ring and 38143af08d82Slm66018 * resubmit all the outstanding transactions. 38153af08d82Slm66018 */ 38163af08d82Slm66018 b_idx = vdcp->local_dring_backup_tail; 38173af08d82Slm66018 for (count = 0; count < vdcp->local_dring_backup_len; count++) { 38183af08d82Slm66018 38193af08d82Slm66018 curr_ldep = &(vdcp->local_dring_backup[b_idx]); 38203af08d82Slm66018 3821eff7243fSlm66018 /* only resubmit outstanding transactions */ 38223af08d82Slm66018 if (!curr_ldep->is_free) { 382390e2f9dcSlm66018 /* 382490e2f9dcSlm66018 * If we are retrying a block read/write operation we 382590e2f9dcSlm66018 * need to update the I/O statistics to indicate that 382690e2f9dcSlm66018 * the request is being put back on the waitq to be 382790e2f9dcSlm66018 * serviced (it will have been taken off after the 382890e2f9dcSlm66018 * error was reported). 382990e2f9dcSlm66018 */ 383090e2f9dcSlm66018 mutex_enter(&vdcp->lock); 383190e2f9dcSlm66018 op = curr_ldep->operation; 383290e2f9dcSlm66018 if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) { 383390e2f9dcSlm66018 DTRACE_IO1(start, buf_t *, curr_ldep->cb_arg); 383490e2f9dcSlm66018 VD_KSTAT_WAITQ_ENTER(vdcp); 383590e2f9dcSlm66018 } 38363af08d82Slm66018 38373af08d82Slm66018 DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx); 383890e2f9dcSlm66018 rv = vdc_populate_descriptor(vdcp, op, 38393af08d82Slm66018 curr_ldep->addr, curr_ldep->nbytes, 38403af08d82Slm66018 curr_ldep->slice, curr_ldep->offset, 38413af08d82Slm66018 curr_ldep->cb_type, curr_ldep->cb_arg, 38423af08d82Slm66018 curr_ldep->dir); 384390e2f9dcSlm66018 38443af08d82Slm66018 if (rv) { 384590e2f9dcSlm66018 if (op == VD_OP_BREAD || op == VD_OP_BWRITE) { 384690e2f9dcSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_transerrs); 384790e2f9dcSlm66018 VD_KSTAT_WAITQ_EXIT(vdcp); 384890e2f9dcSlm66018 DTRACE_IO1(done, buf_t *, 384990e2f9dcSlm66018 curr_ldep->cb_arg); 385090e2f9dcSlm66018 } 38513af08d82Slm66018 DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n", 38523af08d82Slm66018 vdcp->instance, b_idx); 385390e2f9dcSlm66018 mutex_exit(&vdcp->lock); 385490e2f9dcSlm66018 goto done; 38553af08d82Slm66018 } 38563af08d82Slm66018 385790e2f9dcSlm66018 /* 385890e2f9dcSlm66018 * If this is a block read/write we update the I/O 385990e2f9dcSlm66018 * statistics kstat to indicate that the request 386090e2f9dcSlm66018 * has been sent back to the vDisk server and should 386190e2f9dcSlm66018 * now be put on the run queue. 386290e2f9dcSlm66018 */ 386390e2f9dcSlm66018 if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) { 386490e2f9dcSlm66018 DTRACE_PROBE1(send, buf_t *, curr_ldep->cb_arg); 386590e2f9dcSlm66018 VD_KSTAT_WAITQ_TO_RUNQ(vdcp); 386690e2f9dcSlm66018 } 386790e2f9dcSlm66018 mutex_exit(&vdcp->lock); 386890e2f9dcSlm66018 38693af08d82Slm66018 /* Wait for the response message. */ 38703af08d82Slm66018 DMSG(vdcp, 1, "waiting for response to idx=%x\n", 38713af08d82Slm66018 b_idx); 387290e2f9dcSlm66018 rv = vdc_wait_for_response(vdcp, &vio_msg); 387390e2f9dcSlm66018 if (rv) { 387490e2f9dcSlm66018 /* 387590e2f9dcSlm66018 * If this is a block read/write we update 387690e2f9dcSlm66018 * the I/O statistics kstat to take it 387790e2f9dcSlm66018 * off the run queue. 387890e2f9dcSlm66018 */ 387990e2f9dcSlm66018 mutex_enter(&vdcp->lock); 388090e2f9dcSlm66018 if (op == VD_OP_BREAD || op == VD_OP_BWRITE) { 388190e2f9dcSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_transerrs); 388290e2f9dcSlm66018 VD_KSTAT_RUNQ_EXIT(vdcp); 388390e2f9dcSlm66018 DTRACE_IO1(done, buf_t *, 388490e2f9dcSlm66018 curr_ldep->cb_arg); 388590e2f9dcSlm66018 } 38863af08d82Slm66018 DMSG(vdcp, 1, "[%d] wait_for_response " 38873af08d82Slm66018 "returned err=%d\n", vdcp->instance, 388890e2f9dcSlm66018 rv); 388990e2f9dcSlm66018 mutex_exit(&vdcp->lock); 389090e2f9dcSlm66018 goto done; 38913af08d82Slm66018 } 38923af08d82Slm66018 38933af08d82Slm66018 DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx); 389490e2f9dcSlm66018 rv = vdc_process_data_msg(vdcp, &vio_msg); 389590e2f9dcSlm66018 if (rv) { 38963af08d82Slm66018 DMSG(vdcp, 1, "[%d] process_data_msg " 38973af08d82Slm66018 "returned err=%d\n", vdcp->instance, 389890e2f9dcSlm66018 rv); 389990e2f9dcSlm66018 goto done; 39003af08d82Slm66018 } 3901630f014dSrameshc /* 3902630f014dSrameshc * Mark this entry as free so that we will not resubmit 3903630f014dSrameshc * this "done" request again, if we were to use the same 3904630f014dSrameshc * backup_dring again in future. This could happen when 3905630f014dSrameshc * a reset happens while processing the backup_dring. 3906630f014dSrameshc */ 3907630f014dSrameshc curr_ldep->is_free = B_TRUE; 390890e2f9dcSlm66018 processed++; 39093af08d82Slm66018 } 39103af08d82Slm66018 39113af08d82Slm66018 /* get the next element to submit */ 39123af08d82Slm66018 if (++b_idx >= vdcp->local_dring_backup_len) 39133af08d82Slm66018 b_idx = 0; 39143af08d82Slm66018 } 39153af08d82Slm66018 39163af08d82Slm66018 /* all done - now clear up pending dring copy */ 39173af08d82Slm66018 dring_size = vdcp->local_dring_backup_len * 39183af08d82Slm66018 sizeof (vdcp->local_dring_backup[0]); 39193af08d82Slm66018 39203af08d82Slm66018 (void) kmem_free(vdcp->local_dring_backup, dring_size); 39213af08d82Slm66018 39223af08d82Slm66018 vdcp->local_dring_backup = NULL; 39233af08d82Slm66018 392490e2f9dcSlm66018 done: 392590e2f9dcSlm66018 DTRACE_PROBE2(processed, int, processed, vdc_t *, vdcp); 392690e2f9dcSlm66018 392790e2f9dcSlm66018 return (rv); 39283af08d82Slm66018 } 39293af08d82Slm66018 39303af08d82Slm66018 /* 39313af08d82Slm66018 * Function: 3932655fd6a9Sachartre * vdc_cancel_backup_dring 3933655fd6a9Sachartre * 3934655fd6a9Sachartre * Description: 3935655fd6a9Sachartre * Cancel each descriptor in the backed up dring to vDisk server. 3936655fd6a9Sachartre * The Dring was backed up during connection reset. 3937655fd6a9Sachartre * 3938655fd6a9Sachartre * Arguments: 3939655fd6a9Sachartre * vdcp - soft state pointer for this instance of the device driver. 3940655fd6a9Sachartre * 3941655fd6a9Sachartre * Return Code: 3942655fd6a9Sachartre * None 3943655fd6a9Sachartre */ 3944655fd6a9Sachartre void 394590e2f9dcSlm66018 vdc_cancel_backup_dring(vdc_t *vdcp) 3946655fd6a9Sachartre { 3947655fd6a9Sachartre vdc_local_desc_t *ldep; 3948655fd6a9Sachartre struct buf *bufp; 3949655fd6a9Sachartre int count; 3950655fd6a9Sachartre int b_idx; 3951655fd6a9Sachartre int dring_size; 395290e2f9dcSlm66018 int cancelled = 0; 3953655fd6a9Sachartre 3954655fd6a9Sachartre ASSERT(MUTEX_HELD(&vdcp->lock)); 3955655fd6a9Sachartre ASSERT(vdcp->state == VDC_STATE_INIT || 3956655fd6a9Sachartre vdcp->state == VDC_STATE_INIT_WAITING || 3957655fd6a9Sachartre vdcp->state == VDC_STATE_NEGOTIATE || 3958655fd6a9Sachartre vdcp->state == VDC_STATE_RESETTING); 3959655fd6a9Sachartre 3960655fd6a9Sachartre if (vdcp->local_dring_backup == NULL) { 3961655fd6a9Sachartre /* the pending requests have already been processed */ 3962655fd6a9Sachartre return; 3963655fd6a9Sachartre } 3964655fd6a9Sachartre 3965655fd6a9Sachartre DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n", 3966655fd6a9Sachartre vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail); 3967655fd6a9Sachartre 3968655fd6a9Sachartre /* 3969655fd6a9Sachartre * Walk the backup copy of the local descriptor ring and 3970655fd6a9Sachartre * cancel all the outstanding transactions. 3971655fd6a9Sachartre */ 3972655fd6a9Sachartre b_idx = vdcp->local_dring_backup_tail; 3973655fd6a9Sachartre for (count = 0; count < vdcp->local_dring_backup_len; count++) { 3974655fd6a9Sachartre 3975655fd6a9Sachartre ldep = &(vdcp->local_dring_backup[b_idx]); 3976655fd6a9Sachartre 3977655fd6a9Sachartre /* only cancel outstanding transactions */ 3978655fd6a9Sachartre if (!ldep->is_free) { 3979655fd6a9Sachartre 3980655fd6a9Sachartre DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx); 398190e2f9dcSlm66018 cancelled++; 3982655fd6a9Sachartre 3983655fd6a9Sachartre /* 3984655fd6a9Sachartre * All requests have already been cleared from the 3985655fd6a9Sachartre * local descriptor ring and the LDC channel has been 3986655fd6a9Sachartre * reset so we will never get any reply for these 3987655fd6a9Sachartre * requests. Now we just have to notify threads waiting 3988655fd6a9Sachartre * for replies that the request has failed. 3989655fd6a9Sachartre */ 3990655fd6a9Sachartre switch (ldep->cb_type) { 3991655fd6a9Sachartre case CB_SYNC: 3992655fd6a9Sachartre ASSERT(vdcp->sync_op_pending); 3993655fd6a9Sachartre vdcp->sync_op_status = EIO; 3994655fd6a9Sachartre vdcp->sync_op_pending = B_FALSE; 3995655fd6a9Sachartre cv_signal(&vdcp->sync_pending_cv); 3996655fd6a9Sachartre break; 3997655fd6a9Sachartre 3998655fd6a9Sachartre case CB_STRATEGY: 3999655fd6a9Sachartre bufp = ldep->cb_arg; 4000655fd6a9Sachartre ASSERT(bufp != NULL); 4001655fd6a9Sachartre bufp->b_resid = bufp->b_bcount; 4002366a92acSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs); 400390e2f9dcSlm66018 VD_KSTAT_RUNQ_EXIT(vdcp); 4004366a92acSlm66018 DTRACE_IO1(done, buf_t *, bufp); 4005655fd6a9Sachartre bioerror(bufp, EIO); 4006655fd6a9Sachartre biodone(bufp); 4007655fd6a9Sachartre break; 4008655fd6a9Sachartre 4009655fd6a9Sachartre default: 4010655fd6a9Sachartre ASSERT(0); 4011655fd6a9Sachartre } 4012655fd6a9Sachartre 4013655fd6a9Sachartre } 4014655fd6a9Sachartre 4015655fd6a9Sachartre /* get the next element to cancel */ 4016655fd6a9Sachartre if (++b_idx >= vdcp->local_dring_backup_len) 4017655fd6a9Sachartre b_idx = 0; 4018655fd6a9Sachartre } 4019655fd6a9Sachartre 4020655fd6a9Sachartre /* all done - now clear up pending dring copy */ 4021655fd6a9Sachartre dring_size = vdcp->local_dring_backup_len * 4022655fd6a9Sachartre sizeof (vdcp->local_dring_backup[0]); 4023655fd6a9Sachartre 4024655fd6a9Sachartre (void) kmem_free(vdcp->local_dring_backup, dring_size); 4025655fd6a9Sachartre 4026655fd6a9Sachartre vdcp->local_dring_backup = NULL; 4027655fd6a9Sachartre 402890e2f9dcSlm66018 DTRACE_PROBE2(cancelled, int, cancelled, vdc_t *, vdcp); 4029655fd6a9Sachartre } 4030655fd6a9Sachartre 4031655fd6a9Sachartre /* 4032655fd6a9Sachartre * Function: 4033655fd6a9Sachartre * vdc_connection_timeout 4034655fd6a9Sachartre * 4035655fd6a9Sachartre * Description: 4036655fd6a9Sachartre * This function is invoked if the timeout set to establish the connection 4037655fd6a9Sachartre * with vds expires. This will happen if we spend too much time in the 4038655fd6a9Sachartre * VDC_STATE_INIT_WAITING or VDC_STATE_NEGOTIATE states. Then we will 4039655fd6a9Sachartre * cancel any pending request and mark them as failed. 4040655fd6a9Sachartre * 4041655fd6a9Sachartre * If the timeout does not expire, it will be cancelled when we reach the 4042655fd6a9Sachartre * VDC_STATE_HANDLE_PENDING or VDC_STATE_RESETTING state. This function can 4043655fd6a9Sachartre * be invoked while we are in the VDC_STATE_HANDLE_PENDING or 4044655fd6a9Sachartre * VDC_STATE_RESETTING state in which case we do nothing because the 4045655fd6a9Sachartre * timeout is being cancelled. 4046655fd6a9Sachartre * 4047655fd6a9Sachartre * Arguments: 4048655fd6a9Sachartre * arg - argument of the timeout function actually a soft state 4049655fd6a9Sachartre * pointer for the instance of the device driver. 4050655fd6a9Sachartre * 4051655fd6a9Sachartre * Return Code: 4052655fd6a9Sachartre * None 4053655fd6a9Sachartre */ 4054655fd6a9Sachartre void 4055655fd6a9Sachartre vdc_connection_timeout(void *arg) 4056655fd6a9Sachartre { 4057655fd6a9Sachartre vdc_t *vdcp = (vdc_t *)arg; 4058655fd6a9Sachartre 4059655fd6a9Sachartre mutex_enter(&vdcp->lock); 4060655fd6a9Sachartre 4061655fd6a9Sachartre if (vdcp->state == VDC_STATE_HANDLE_PENDING || 4062655fd6a9Sachartre vdcp->state == VDC_STATE_DETACH) { 4063655fd6a9Sachartre /* 4064655fd6a9Sachartre * The connection has just been re-established or 4065655fd6a9Sachartre * we are detaching. 4066655fd6a9Sachartre */ 4067655fd6a9Sachartre vdcp->ctimeout_reached = B_FALSE; 4068655fd6a9Sachartre mutex_exit(&vdcp->lock); 4069655fd6a9Sachartre return; 4070655fd6a9Sachartre } 4071655fd6a9Sachartre 4072655fd6a9Sachartre vdcp->ctimeout_reached = B_TRUE; 4073655fd6a9Sachartre 4074655fd6a9Sachartre /* notify requests waiting for sending */ 4075655fd6a9Sachartre cv_broadcast(&vdcp->running_cv); 4076655fd6a9Sachartre 4077655fd6a9Sachartre /* cancel requests waiting for a result */ 407890e2f9dcSlm66018 vdc_cancel_backup_dring(vdcp); 4079655fd6a9Sachartre 4080655fd6a9Sachartre mutex_exit(&vdcp->lock); 4081655fd6a9Sachartre 4082655fd6a9Sachartre cmn_err(CE_NOTE, "[%d] connection to service domain timeout", 4083655fd6a9Sachartre vdcp->instance); 4084655fd6a9Sachartre } 4085655fd6a9Sachartre 4086655fd6a9Sachartre /* 4087655fd6a9Sachartre * Function: 40883af08d82Slm66018 * vdc_backup_local_dring() 40893af08d82Slm66018 * 40903af08d82Slm66018 * Description: 40913af08d82Slm66018 * Backup the current dring in the event of a reset. The Dring 40923af08d82Slm66018 * transactions will be resubmitted to the server when the 40933af08d82Slm66018 * connection is restored. 40943af08d82Slm66018 * 40953af08d82Slm66018 * Arguments: 40963af08d82Slm66018 * vdcp - soft state pointer for this instance of the device driver. 40973af08d82Slm66018 * 40983af08d82Slm66018 * Return Code: 40993af08d82Slm66018 * NONE 41003af08d82Slm66018 */ 41013af08d82Slm66018 static void 41023af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp) 41033af08d82Slm66018 { 41043af08d82Slm66018 int dring_size; 41053af08d82Slm66018 4106655fd6a9Sachartre ASSERT(MUTEX_HELD(&vdcp->lock)); 41073af08d82Slm66018 ASSERT(vdcp->state == VDC_STATE_RESETTING); 41083af08d82Slm66018 41093af08d82Slm66018 /* 41103af08d82Slm66018 * If the backup dring is stil around, it means 41113af08d82Slm66018 * that the last restore did not complete. However, 41123af08d82Slm66018 * since we never got back into the running state, 41133af08d82Slm66018 * the backup copy we have is still valid. 41143af08d82Slm66018 */ 41153af08d82Slm66018 if (vdcp->local_dring_backup != NULL) { 41163af08d82Slm66018 DMSG(vdcp, 1, "reusing local descriptor ring backup " 41173af08d82Slm66018 "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len, 41183af08d82Slm66018 vdcp->local_dring_backup_tail); 41193af08d82Slm66018 return; 41203af08d82Slm66018 } 41213af08d82Slm66018 4122655fd6a9Sachartre /* 4123655fd6a9Sachartre * The backup dring can be NULL and the local dring may not be 4124655fd6a9Sachartre * initialized. This can happen if we had a reset while establishing 4125655fd6a9Sachartre * a new connection but after the connection has timed out. In that 4126655fd6a9Sachartre * case the backup dring is NULL because the requests have been 4127655fd6a9Sachartre * cancelled and the request occured before the local dring is 4128655fd6a9Sachartre * initialized. 4129655fd6a9Sachartre */ 4130655fd6a9Sachartre if (!(vdcp->initialized & VDC_DRING_LOCAL)) 4131655fd6a9Sachartre return; 4132655fd6a9Sachartre 41333af08d82Slm66018 DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, " 41343af08d82Slm66018 "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx); 41353af08d82Slm66018 41363af08d82Slm66018 dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]); 41373af08d82Slm66018 41383af08d82Slm66018 vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP); 41393af08d82Slm66018 bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size); 41403af08d82Slm66018 41413af08d82Slm66018 vdcp->local_dring_backup_tail = vdcp->dring_curr_idx; 41423af08d82Slm66018 vdcp->local_dring_backup_len = vdcp->dring_len; 41433af08d82Slm66018 } 41443af08d82Slm66018 41458cd10891Snarayan static void 41468cd10891Snarayan vdc_switch_server(vdc_t *vdcp) 41478cd10891Snarayan { 41488cd10891Snarayan int rv; 41498cd10891Snarayan vdc_server_t *curr_server, *new_server; 41508cd10891Snarayan 41518cd10891Snarayan ASSERT(MUTEX_HELD(&vdcp->lock)); 41528cd10891Snarayan 41538cd10891Snarayan /* if there is only one server return back */ 41548cd10891Snarayan if (vdcp->num_servers == 1) { 41558cd10891Snarayan return; 41568cd10891Snarayan } 41578cd10891Snarayan 41588cd10891Snarayan /* Get current and next server */ 41598cd10891Snarayan curr_server = vdcp->curr_server; 41608cd10891Snarayan new_server = 41618cd10891Snarayan (curr_server->next) ? curr_server->next : vdcp->server_list; 41628cd10891Snarayan ASSERT(curr_server != new_server); 41638cd10891Snarayan 41648cd10891Snarayan /* bring current server's channel down */ 41658cd10891Snarayan rv = ldc_down(curr_server->ldc_handle); 41668cd10891Snarayan if (rv) { 41678cd10891Snarayan DMSG(vdcp, 0, "[%d] Cannot bring channel down, port %ld\n", 41688cd10891Snarayan vdcp->instance, curr_server->id); 41698cd10891Snarayan return; 41708cd10891Snarayan } 41718cd10891Snarayan 41728cd10891Snarayan /* switch the server */ 41738cd10891Snarayan vdcp->curr_server = new_server; 41748cd10891Snarayan 41758cd10891Snarayan DMSG(vdcp, 0, "[%d] Switched to next vdisk server, port@%ld, ldc@%ld\n", 41768cd10891Snarayan vdcp->instance, vdcp->curr_server->id, vdcp->curr_server->ldc_id); 41778cd10891Snarayan } 41788cd10891Snarayan 41791ae08745Sheppo /* -------------------------------------------------------------------------- */ 41801ae08745Sheppo 41811ae08745Sheppo /* 41821ae08745Sheppo * The following functions process the incoming messages from vds 41831ae08745Sheppo */ 41841ae08745Sheppo 41850a55fbb7Slm66018 /* 41860a55fbb7Slm66018 * Function: 41870a55fbb7Slm66018 * vdc_process_msg_thread() 41880a55fbb7Slm66018 * 41890a55fbb7Slm66018 * Description: 41900a55fbb7Slm66018 * 41913af08d82Slm66018 * Main VDC message processing thread. Each vDisk instance 41923af08d82Slm66018 * consists of a copy of this thread. This thread triggers 41933af08d82Slm66018 * all the handshakes and data exchange with the server. It 41943af08d82Slm66018 * also handles all channel resets 41953af08d82Slm66018 * 41960a55fbb7Slm66018 * Arguments: 41970a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 41980a55fbb7Slm66018 * 41990a55fbb7Slm66018 * Return Code: 42000a55fbb7Slm66018 * None 42010a55fbb7Slm66018 */ 42021ae08745Sheppo static void 42033af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp) 42041ae08745Sheppo { 42051ae08745Sheppo int status; 4206655fd6a9Sachartre int ctimeout; 4207655fd6a9Sachartre timeout_id_t tmid = 0; 42088cd10891Snarayan clock_t ldcup_timeout = 0; 42091ae08745Sheppo 42103af08d82Slm66018 mutex_enter(&vdcp->lock); 42111ae08745Sheppo 42121ae08745Sheppo for (;;) { 42131ae08745Sheppo 42143af08d82Slm66018 #define Q(_s) (vdcp->state == _s) ? #_s : 42153af08d82Slm66018 DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state, 42163af08d82Slm66018 Q(VDC_STATE_INIT) 42173af08d82Slm66018 Q(VDC_STATE_INIT_WAITING) 42183af08d82Slm66018 Q(VDC_STATE_NEGOTIATE) 42193af08d82Slm66018 Q(VDC_STATE_HANDLE_PENDING) 42203af08d82Slm66018 Q(VDC_STATE_RUNNING) 42213af08d82Slm66018 Q(VDC_STATE_RESETTING) 42223af08d82Slm66018 Q(VDC_STATE_DETACH) 42233af08d82Slm66018 "UNKNOWN"); 42241ae08745Sheppo 42253af08d82Slm66018 switch (vdcp->state) { 42263af08d82Slm66018 case VDC_STATE_INIT: 42273af08d82Slm66018 4228655fd6a9Sachartre /* 4229655fd6a9Sachartre * If requested, start a timeout to check if the 4230655fd6a9Sachartre * connection with vds is established in the 4231655fd6a9Sachartre * specified delay. If the timeout expires, we 4232655fd6a9Sachartre * will cancel any pending request. 4233655fd6a9Sachartre * 4234655fd6a9Sachartre * If some reset have occurred while establishing 4235655fd6a9Sachartre * the connection, we already have a timeout armed 4236655fd6a9Sachartre * and in that case we don't need to arm a new one. 42378cd10891Snarayan * 42388cd10891Snarayan * The same rule applies when there are multiple vds'. 42398cd10891Snarayan * If either a connection cannot be established or 42408cd10891Snarayan * the handshake times out, the connection thread will 42418cd10891Snarayan * try another server. The 'ctimeout' will report 42428cd10891Snarayan * back an error after it expires irrespective of 42438cd10891Snarayan * whether the vdisk is trying to connect to just 42448cd10891Snarayan * one or multiple servers. 4245655fd6a9Sachartre */ 4246655fd6a9Sachartre ctimeout = (vdc_timeout != 0)? 42478cd10891Snarayan vdc_timeout : vdcp->curr_server->ctimeout; 4248655fd6a9Sachartre 4249655fd6a9Sachartre if (ctimeout != 0 && tmid == 0) { 4250655fd6a9Sachartre tmid = timeout(vdc_connection_timeout, vdcp, 42518cd10891Snarayan ctimeout * drv_usectohz(MICROSEC)); 4252655fd6a9Sachartre } 4253655fd6a9Sachartre 42548cd10891Snarayan /* Check if we are re-initializing repeatedly */ 42558cd10891Snarayan if (vdcp->hshake_cnt > vdc_hshake_retries && 4256655fd6a9Sachartre vdcp->lifecycle != VDC_LC_ONLINE) { 42578cd10891Snarayan 42588cd10891Snarayan DMSG(vdcp, 0, "[%d] too many handshakes,cnt=%d", 42598cd10891Snarayan vdcp->instance, vdcp->hshake_cnt); 42603c96341aSnarayan cmn_err(CE_NOTE, "[%d] disk access failed.\n", 42613c96341aSnarayan vdcp->instance); 42623af08d82Slm66018 vdcp->state = VDC_STATE_DETACH; 42633af08d82Slm66018 break; 42643af08d82Slm66018 } 42653af08d82Slm66018 42668cd10891Snarayan /* Switch to STATE_DETACH if drv is detaching */ 42678cd10891Snarayan if (vdcp->lifecycle == VDC_LC_DETACHING) { 42688cd10891Snarayan vdcp->state = VDC_STATE_DETACH; 42698cd10891Snarayan break; 42708cd10891Snarayan } 42718cd10891Snarayan 42728cd10891Snarayan /* Switch server */ 42738cd10891Snarayan if (vdcp->hshake_cnt > 0) 42748cd10891Snarayan vdc_switch_server(vdcp); 42758cd10891Snarayan vdcp->hshake_cnt++; 42768cd10891Snarayan 42773af08d82Slm66018 /* Bring up connection with vds via LDC */ 42783af08d82Slm66018 status = vdc_start_ldc_connection(vdcp); 42798cd10891Snarayan if (status != EINVAL) { 42803af08d82Slm66018 vdcp->state = VDC_STATE_INIT_WAITING; 42813af08d82Slm66018 } 42823af08d82Slm66018 break; 42833af08d82Slm66018 42843af08d82Slm66018 case VDC_STATE_INIT_WAITING: 42853af08d82Slm66018 42868cd10891Snarayan /* if channel is UP, start negotiation */ 42878cd10891Snarayan if (vdcp->curr_server->ldc_state == LDC_UP) { 42888cd10891Snarayan vdcp->state = VDC_STATE_NEGOTIATE; 42898cd10891Snarayan break; 42908cd10891Snarayan } 42918cd10891Snarayan 42928cd10891Snarayan /* check if only one server exists */ 42938cd10891Snarayan if (vdcp->num_servers == 1) { 42943af08d82Slm66018 cv_wait(&vdcp->initwait_cv, &vdcp->lock); 42958cd10891Snarayan } else { 42968cd10891Snarayan /* 42978cd10891Snarayan * wait for LDC_UP, if it times out, switch 42988cd10891Snarayan * to another server. 42998cd10891Snarayan */ 43008cd10891Snarayan ldcup_timeout = ddi_get_lbolt() + 43018cd10891Snarayan (vdc_ldcup_timeout * 43028cd10891Snarayan drv_usectohz(MICROSEC)); 43038cd10891Snarayan status = cv_timedwait(&vdcp->initwait_cv, 43048cd10891Snarayan &vdcp->lock, ldcup_timeout); 43058cd10891Snarayan if (status == -1 && 43068cd10891Snarayan vdcp->state == VDC_STATE_INIT_WAITING && 43078cd10891Snarayan vdcp->curr_server->ldc_state != LDC_UP) { 43088cd10891Snarayan /* timed out & still waiting */ 43098cd10891Snarayan vdcp->state = VDC_STATE_INIT; 43108cd10891Snarayan break; 43118cd10891Snarayan } 43128cd10891Snarayan } 43138cd10891Snarayan 43143af08d82Slm66018 if (vdcp->state != VDC_STATE_INIT_WAITING) { 43153af08d82Slm66018 DMSG(vdcp, 0, 43163af08d82Slm66018 "state moved to %d out from under us...\n", 43173af08d82Slm66018 vdcp->state); 43183af08d82Slm66018 } 43193af08d82Slm66018 break; 43203af08d82Slm66018 43213af08d82Slm66018 case VDC_STATE_NEGOTIATE: 43223af08d82Slm66018 switch (status = vdc_ver_negotiation(vdcp)) { 43233af08d82Slm66018 case 0: 43243af08d82Slm66018 break; 43253af08d82Slm66018 default: 43263af08d82Slm66018 DMSG(vdcp, 0, "ver negotiate failed (%d)..\n", 43273af08d82Slm66018 status); 43283af08d82Slm66018 goto reset; 43293af08d82Slm66018 } 43303af08d82Slm66018 43313af08d82Slm66018 switch (status = vdc_attr_negotiation(vdcp)) { 43323af08d82Slm66018 case 0: 43333af08d82Slm66018 break; 43343af08d82Slm66018 default: 43353af08d82Slm66018 DMSG(vdcp, 0, "attr negotiate failed (%d)..\n", 43363af08d82Slm66018 status); 43373af08d82Slm66018 goto reset; 43383af08d82Slm66018 } 43393af08d82Slm66018 43403af08d82Slm66018 switch (status = vdc_dring_negotiation(vdcp)) { 43413af08d82Slm66018 case 0: 43423af08d82Slm66018 break; 43433af08d82Slm66018 default: 43443af08d82Slm66018 DMSG(vdcp, 0, "dring negotiate failed (%d)..\n", 43453af08d82Slm66018 status); 43463af08d82Slm66018 goto reset; 43473af08d82Slm66018 } 43483af08d82Slm66018 43493af08d82Slm66018 switch (status = vdc_rdx_exchange(vdcp)) { 43503af08d82Slm66018 case 0: 43513af08d82Slm66018 vdcp->state = VDC_STATE_HANDLE_PENDING; 43523af08d82Slm66018 goto done; 43533af08d82Slm66018 default: 43543af08d82Slm66018 DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n", 43553af08d82Slm66018 status); 43563af08d82Slm66018 goto reset; 43573af08d82Slm66018 } 43583af08d82Slm66018 reset: 43593af08d82Slm66018 DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n", 43603af08d82Slm66018 status); 43613af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 4362655fd6a9Sachartre vdcp->self_reset = B_TRUE; 43633af08d82Slm66018 done: 43643af08d82Slm66018 DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n", 43653af08d82Slm66018 vdcp->state); 43663af08d82Slm66018 break; 43673af08d82Slm66018 43683af08d82Slm66018 case VDC_STATE_HANDLE_PENDING: 43693af08d82Slm66018 4370655fd6a9Sachartre if (vdcp->ctimeout_reached) { 4371655fd6a9Sachartre /* 4372655fd6a9Sachartre * The connection timeout had been reached so 4373655fd6a9Sachartre * pending requests have been cancelled. Now 4374655fd6a9Sachartre * that the connection is back we can reset 4375655fd6a9Sachartre * the timeout. 4376655fd6a9Sachartre */ 4377655fd6a9Sachartre ASSERT(vdcp->local_dring_backup == NULL); 4378655fd6a9Sachartre ASSERT(tmid != 0); 4379655fd6a9Sachartre tmid = 0; 4380655fd6a9Sachartre vdcp->ctimeout_reached = B_FALSE; 4381655fd6a9Sachartre vdcp->state = VDC_STATE_RUNNING; 4382655fd6a9Sachartre DMSG(vdcp, 0, "[%d] connection to service " 4383655fd6a9Sachartre "domain is up", vdcp->instance); 4384655fd6a9Sachartre break; 4385655fd6a9Sachartre } 4386655fd6a9Sachartre 43873af08d82Slm66018 mutex_exit(&vdcp->lock); 4388655fd6a9Sachartre if (tmid != 0) { 4389655fd6a9Sachartre (void) untimeout(tmid); 4390655fd6a9Sachartre tmid = 0; 4391655fd6a9Sachartre } 43923af08d82Slm66018 status = vdc_resubmit_backup_dring(vdcp); 43933af08d82Slm66018 mutex_enter(&vdcp->lock); 43943af08d82Slm66018 43953af08d82Slm66018 if (status) 43963af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 43973af08d82Slm66018 else 43983af08d82Slm66018 vdcp->state = VDC_STATE_RUNNING; 43993af08d82Slm66018 44003af08d82Slm66018 break; 44013af08d82Slm66018 44023af08d82Slm66018 /* enter running state */ 44033af08d82Slm66018 case VDC_STATE_RUNNING: 44043af08d82Slm66018 /* 44053af08d82Slm66018 * Signal anyone waiting for the connection 44063af08d82Slm66018 * to come on line. 44073af08d82Slm66018 */ 44083af08d82Slm66018 vdcp->hshake_cnt = 0; 44093af08d82Slm66018 cv_broadcast(&vdcp->running_cv); 44102f5224aeSachartre 44112f5224aeSachartre /* failfast has to been checked after reset */ 44122f5224aeSachartre cv_signal(&vdcp->failfast_cv); 44132f5224aeSachartre 44142f5224aeSachartre /* ownership is lost during reset */ 44152f5224aeSachartre if (vdcp->ownership & VDC_OWNERSHIP_WANTED) 44162f5224aeSachartre vdcp->ownership |= VDC_OWNERSHIP_RESET; 44172f5224aeSachartre cv_signal(&vdcp->ownership_cv); 44182f5224aeSachartre 4419d7400d00Sachartre cmn_err(CE_CONT, "?vdisk@%d is online using " 4420d7400d00Sachartre "ldc@%ld,%ld\n", vdcp->instance, 4421d7400d00Sachartre vdcp->curr_server->ldc_id, vdcp->curr_server->id); 4422d7400d00Sachartre 44233af08d82Slm66018 mutex_exit(&vdcp->lock); 44243af08d82Slm66018 44253af08d82Slm66018 for (;;) { 44263af08d82Slm66018 vio_msg_t msg; 44273af08d82Slm66018 status = vdc_wait_for_response(vdcp, &msg); 44283af08d82Slm66018 if (status) break; 44293af08d82Slm66018 44303af08d82Slm66018 DMSG(vdcp, 1, "[%d] new pkt(s) available\n", 44313af08d82Slm66018 vdcp->instance); 44323af08d82Slm66018 status = vdc_process_data_msg(vdcp, &msg); 44331ae08745Sheppo if (status) { 44343af08d82Slm66018 DMSG(vdcp, 1, "[%d] process_data_msg " 44353af08d82Slm66018 "returned err=%d\n", vdcp->instance, 44363af08d82Slm66018 status); 44371ae08745Sheppo break; 44381ae08745Sheppo } 44391ae08745Sheppo 44403af08d82Slm66018 } 4441e1ebb9ecSlm66018 44423af08d82Slm66018 mutex_enter(&vdcp->lock); 44433af08d82Slm66018 4444d7400d00Sachartre cmn_err(CE_CONT, "?vdisk@%d is offline\n", 4445d7400d00Sachartre vdcp->instance); 4446d7400d00Sachartre 44473af08d82Slm66018 vdcp->state = VDC_STATE_RESETTING; 4448690555a1Sachartre vdcp->self_reset = B_TRUE; 44493af08d82Slm66018 break; 44503af08d82Slm66018 44513af08d82Slm66018 case VDC_STATE_RESETTING: 4452655fd6a9Sachartre /* 4453655fd6a9Sachartre * When we reach this state, we either come from the 4454655fd6a9Sachartre * VDC_STATE_RUNNING state and we can have pending 4455655fd6a9Sachartre * request but no timeout is armed; or we come from 4456655fd6a9Sachartre * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or 4457655fd6a9Sachartre * VDC_HANDLE_PENDING state and there is no pending 4458655fd6a9Sachartre * request or pending requests have already been copied 4459655fd6a9Sachartre * into the backup dring. So we can safely keep the 4460655fd6a9Sachartre * connection timeout armed while we are in this state. 4461655fd6a9Sachartre */ 4462655fd6a9Sachartre 44633af08d82Slm66018 DMSG(vdcp, 0, "Initiating channel reset " 44643af08d82Slm66018 "(pending = %d)\n", (int)vdcp->threads_pending); 44653af08d82Slm66018 44663af08d82Slm66018 if (vdcp->self_reset) { 44673af08d82Slm66018 DMSG(vdcp, 0, 44683af08d82Slm66018 "[%d] calling stop_ldc_connection.\n", 44693af08d82Slm66018 vdcp->instance); 44703af08d82Slm66018 status = vdc_stop_ldc_connection(vdcp); 44713af08d82Slm66018 vdcp->self_reset = B_FALSE; 44721ae08745Sheppo } 44731ae08745Sheppo 44741ae08745Sheppo /* 44753af08d82Slm66018 * Wait for all threads currently waiting 44763af08d82Slm66018 * for a free dring entry to use. 44771ae08745Sheppo */ 44783af08d82Slm66018 while (vdcp->threads_pending) { 44793af08d82Slm66018 cv_broadcast(&vdcp->membind_cv); 44803af08d82Slm66018 cv_broadcast(&vdcp->dring_free_cv); 44813af08d82Slm66018 mutex_exit(&vdcp->lock); 4482205eeb1aSlm66018 /* give the waiters enough time to wake up */ 4483205eeb1aSlm66018 delay(vdc_hz_min_ldc_delay); 44843af08d82Slm66018 mutex_enter(&vdcp->lock); 44851ae08745Sheppo } 44861ae08745Sheppo 44873af08d82Slm66018 ASSERT(vdcp->threads_pending == 0); 44881ae08745Sheppo 44893af08d82Slm66018 /* Sanity check that no thread is receiving */ 44903af08d82Slm66018 ASSERT(vdcp->read_state != VDC_READ_WAITING); 44910a55fbb7Slm66018 44923af08d82Slm66018 vdcp->read_state = VDC_READ_IDLE; 44933af08d82Slm66018 44943af08d82Slm66018 vdc_backup_local_dring(vdcp); 44953af08d82Slm66018 44963af08d82Slm66018 /* cleanup the old d-ring */ 44973af08d82Slm66018 vdc_destroy_descriptor_ring(vdcp); 44983af08d82Slm66018 44993af08d82Slm66018 /* go and start again */ 45003af08d82Slm66018 vdcp->state = VDC_STATE_INIT; 45013af08d82Slm66018 45020a55fbb7Slm66018 break; 45030a55fbb7Slm66018 45043af08d82Slm66018 case VDC_STATE_DETACH: 45053af08d82Slm66018 DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n", 45063af08d82Slm66018 vdcp->instance); 45073af08d82Slm66018 4508655fd6a9Sachartre /* cancel any pending timeout */ 4509655fd6a9Sachartre mutex_exit(&vdcp->lock); 4510655fd6a9Sachartre if (tmid != 0) { 4511655fd6a9Sachartre (void) untimeout(tmid); 4512655fd6a9Sachartre tmid = 0; 4513655fd6a9Sachartre } 4514655fd6a9Sachartre mutex_enter(&vdcp->lock); 4515655fd6a9Sachartre 45163c96341aSnarayan /* 45173c96341aSnarayan * Signal anyone waiting for connection 45183c96341aSnarayan * to come online 45193c96341aSnarayan */ 45203c96341aSnarayan cv_broadcast(&vdcp->running_cv); 45213c96341aSnarayan 45223af08d82Slm66018 while (vdcp->sync_op_pending) { 45233af08d82Slm66018 cv_signal(&vdcp->sync_pending_cv); 45243af08d82Slm66018 cv_signal(&vdcp->sync_blocked_cv); 45253af08d82Slm66018 mutex_exit(&vdcp->lock); 4526205eeb1aSlm66018 /* give the waiters enough time to wake up */ 4527205eeb1aSlm66018 delay(vdc_hz_min_ldc_delay); 45283af08d82Slm66018 mutex_enter(&vdcp->lock); 45290a55fbb7Slm66018 } 45301ae08745Sheppo 45313af08d82Slm66018 mutex_exit(&vdcp->lock); 45323af08d82Slm66018 45333af08d82Slm66018 DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n", 45343af08d82Slm66018 vdcp->instance); 45353af08d82Slm66018 thread_exit(); 45363af08d82Slm66018 break; 45373af08d82Slm66018 } 45383af08d82Slm66018 } 45390a55fbb7Slm66018 } 45400a55fbb7Slm66018 45410a55fbb7Slm66018 45420a55fbb7Slm66018 /* 45430a55fbb7Slm66018 * Function: 45440a55fbb7Slm66018 * vdc_process_data_msg() 45450a55fbb7Slm66018 * 45460a55fbb7Slm66018 * Description: 45470a55fbb7Slm66018 * This function is called by the message processing thread each time 45480a55fbb7Slm66018 * a message with a msgtype of VIO_TYPE_DATA is received. It will either 45490a55fbb7Slm66018 * be an ACK or NACK from vds[1] which vdc handles as follows. 45500a55fbb7Slm66018 * ACK - wake up the waiting thread 45510a55fbb7Slm66018 * NACK - resend any messages necessary 45520a55fbb7Slm66018 * 45530a55fbb7Slm66018 * [1] Although the message format allows it, vds should not send a 45540a55fbb7Slm66018 * VIO_SUBTYPE_INFO message to vdc asking it to read data; if for 45550a55fbb7Slm66018 * some bizarre reason it does, vdc will reset the connection. 45560a55fbb7Slm66018 * 45570a55fbb7Slm66018 * Arguments: 45580a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 45590a55fbb7Slm66018 * msg - the LDC message sent by vds 45600a55fbb7Slm66018 * 45610a55fbb7Slm66018 * Return Code: 45620a55fbb7Slm66018 * 0 - Success. 45630a55fbb7Slm66018 * > 0 - error value returned by LDC 45640a55fbb7Slm66018 */ 45650a55fbb7Slm66018 static int 45663af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg) 45670a55fbb7Slm66018 { 45680a55fbb7Slm66018 int status = 0; 45693af08d82Slm66018 vio_dring_msg_t *dring_msg; 4570d10e4ef2Snarayan vdc_local_desc_t *ldep = NULL; 45713af08d82Slm66018 int start, end; 45723af08d82Slm66018 int idx; 457390e2f9dcSlm66018 int op; 45740a55fbb7Slm66018 45753af08d82Slm66018 dring_msg = (vio_dring_msg_t *)msg; 45760a55fbb7Slm66018 45773af08d82Slm66018 ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA); 45783af08d82Slm66018 ASSERT(vdcp != NULL); 45793af08d82Slm66018 45803af08d82Slm66018 mutex_enter(&vdcp->lock); 45810a55fbb7Slm66018 45820a55fbb7Slm66018 /* 45830a55fbb7Slm66018 * Check to see if the message has bogus data 45840a55fbb7Slm66018 */ 4585e1ebb9ecSlm66018 idx = start = dring_msg->start_idx; 45860a55fbb7Slm66018 end = dring_msg->end_idx; 45873af08d82Slm66018 if ((start >= vdcp->dring_len) || 45883af08d82Slm66018 (end >= vdcp->dring_len) || (end < -1)) { 458990e2f9dcSlm66018 /* 459090e2f9dcSlm66018 * Update the I/O statistics to indicate that an error ocurred. 459190e2f9dcSlm66018 * No need to update the wait/run queues as no specific read or 459290e2f9dcSlm66018 * write request is being completed in response to this 'msg'. 459390e2f9dcSlm66018 */ 459490e2f9dcSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs); 45953af08d82Slm66018 DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n", 45963af08d82Slm66018 vdcp->instance, start, end); 45973af08d82Slm66018 mutex_exit(&vdcp->lock); 4598e1ebb9ecSlm66018 return (EINVAL); 45990a55fbb7Slm66018 } 46000a55fbb7Slm66018 46010a55fbb7Slm66018 /* 46020a55fbb7Slm66018 * Verify that the sequence number is what vdc expects. 46030a55fbb7Slm66018 */ 46043af08d82Slm66018 switch (vdc_verify_seq_num(vdcp, dring_msg)) { 4605e1ebb9ecSlm66018 case VDC_SEQ_NUM_TODO: 4606e1ebb9ecSlm66018 break; /* keep processing this message */ 4607e1ebb9ecSlm66018 case VDC_SEQ_NUM_SKIP: 46083af08d82Slm66018 mutex_exit(&vdcp->lock); 4609e1ebb9ecSlm66018 return (0); 4610e1ebb9ecSlm66018 case VDC_SEQ_NUM_INVALID: 461190e2f9dcSlm66018 /* 461290e2f9dcSlm66018 * Update the I/O statistics to indicate that an error ocurred. 461390e2f9dcSlm66018 * No need to update the wait/run queues as no specific read or 461490e2f9dcSlm66018 * write request is being completed in response to this 'msg'. 461590e2f9dcSlm66018 */ 4616366a92acSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs); 461790e2f9dcSlm66018 DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance); 4618366a92acSlm66018 mutex_exit(&vdcp->lock); 46190a55fbb7Slm66018 return (ENXIO); 46200a55fbb7Slm66018 } 46210a55fbb7Slm66018 46223af08d82Slm66018 if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) { 462390e2f9dcSlm66018 /* 462490e2f9dcSlm66018 * Update the I/O statistics to indicate that an error ocurred. 462590e2f9dcSlm66018 * 462690e2f9dcSlm66018 * We need to update the run queue if a read or write request 462790e2f9dcSlm66018 * is being NACKed - otherwise there will appear to be an 462890e2f9dcSlm66018 * indefinite outstanding request and statistics reported by 462990e2f9dcSlm66018 * iostat(1M) will be incorrect. The transaction will be 463090e2f9dcSlm66018 * resubmitted from the backup DRing following the reset 463190e2f9dcSlm66018 * and the wait/run queues will be entered again. 463290e2f9dcSlm66018 */ 463390e2f9dcSlm66018 ldep = &vdcp->local_dring[idx]; 463490e2f9dcSlm66018 op = ldep->operation; 463590e2f9dcSlm66018 if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) { 463690e2f9dcSlm66018 DTRACE_IO1(done, buf_t *, ldep->cb_arg); 463790e2f9dcSlm66018 VD_KSTAT_RUNQ_EXIT(vdcp); 463890e2f9dcSlm66018 } 4639366a92acSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs); 464090e2f9dcSlm66018 VDC_DUMP_DRING_MSG(dring_msg); 464190e2f9dcSlm66018 DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance); 46423af08d82Slm66018 mutex_exit(&vdcp->lock); 4643e1ebb9ecSlm66018 return (EIO); 46440a55fbb7Slm66018 46453af08d82Slm66018 } else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) { 464690e2f9dcSlm66018 /* 464790e2f9dcSlm66018 * Update the I/O statistics to indicate that an error occurred. 464890e2f9dcSlm66018 * No need to update the wait/run queues as no specific read or 464990e2f9dcSlm66018 * write request is being completed in response to this 'msg'. 465090e2f9dcSlm66018 */ 4651366a92acSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_protoerrs); 46523af08d82Slm66018 mutex_exit(&vdcp->lock); 4653e1ebb9ecSlm66018 return (EPROTO); 4654e1ebb9ecSlm66018 } 4655e1ebb9ecSlm66018 46563af08d82Slm66018 DMSG(vdcp, 1, ": start %d end %d\n", start, end); 46573af08d82Slm66018 ASSERT(start == end); 46583af08d82Slm66018 46593af08d82Slm66018 ldep = &vdcp->local_dring[idx]; 46603af08d82Slm66018 46613af08d82Slm66018 DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n", 46623af08d82Slm66018 ldep->dep->hdr.dstate, ldep->cb_type); 46633af08d82Slm66018 4664e1ebb9ecSlm66018 if (ldep->dep->hdr.dstate == VIO_DESC_DONE) { 46653af08d82Slm66018 struct buf *bufp; 4666e1ebb9ecSlm66018 46673af08d82Slm66018 switch (ldep->cb_type) { 46683af08d82Slm66018 case CB_SYNC: 46693af08d82Slm66018 ASSERT(vdcp->sync_op_pending); 4670d10e4ef2Snarayan 46713af08d82Slm66018 status = vdc_depopulate_descriptor(vdcp, idx); 46723af08d82Slm66018 vdcp->sync_op_status = status; 46733af08d82Slm66018 vdcp->sync_op_pending = B_FALSE; 46743af08d82Slm66018 cv_signal(&vdcp->sync_pending_cv); 46753af08d82Slm66018 break; 46764bac2208Snarayan 46773af08d82Slm66018 case CB_STRATEGY: 46783af08d82Slm66018 bufp = ldep->cb_arg; 46793af08d82Slm66018 ASSERT(bufp != NULL); 46803c96341aSnarayan bufp->b_resid = 46813c96341aSnarayan bufp->b_bcount - ldep->dep->payload.nbytes; 46823af08d82Slm66018 status = ldep->dep->payload.status; /* Future:ntoh */ 46833af08d82Slm66018 if (status != 0) { 46843af08d82Slm66018 DMSG(vdcp, 1, "strategy status=%d\n", status); 4685366a92acSlm66018 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs); 46863af08d82Slm66018 bioerror(bufp, status); 4687d10e4ef2Snarayan } 46882f5224aeSachartre 46892f5224aeSachartre (void) vdc_depopulate_descriptor(vdcp, idx); 46903c96341aSnarayan 46913c96341aSnarayan DMSG(vdcp, 1, 46923c96341aSnarayan "strategy complete req=%ld bytes resp=%ld bytes\n", 46933c96341aSnarayan bufp->b_bcount, ldep->dep->payload.nbytes); 46942f5224aeSachartre 46952f5224aeSachartre if (status != 0 && vdcp->failfast_interval != 0) { 46962f5224aeSachartre /* 46972f5224aeSachartre * The I/O has failed and failfast is enabled. 46982f5224aeSachartre * We need the failfast thread to check if the 46992f5224aeSachartre * failure is due to a reservation conflict. 47002f5224aeSachartre */ 47012f5224aeSachartre (void) vdc_failfast_io_queue(vdcp, bufp); 47022f5224aeSachartre } else { 4703366a92acSlm66018 if (status == 0) { 470490e2f9dcSlm66018 op = (bufp->b_flags & B_READ) ? 4705366a92acSlm66018 VD_OP_BREAD : VD_OP_BWRITE; 4706366a92acSlm66018 VD_UPDATE_IO_STATS(vdcp, op, 4707366a92acSlm66018 ldep->dep->payload.nbytes); 4708366a92acSlm66018 } 470990e2f9dcSlm66018 VD_KSTAT_RUNQ_EXIT(vdcp); 4710366a92acSlm66018 DTRACE_IO1(done, buf_t *, bufp); 47112f5224aeSachartre biodone(bufp); 47122f5224aeSachartre } 47133af08d82Slm66018 break; 47143af08d82Slm66018 47153af08d82Slm66018 default: 47163af08d82Slm66018 ASSERT(0); 47170a55fbb7Slm66018 } 47183af08d82Slm66018 } 47193af08d82Slm66018 47203af08d82Slm66018 /* let the arrival signal propogate */ 47213af08d82Slm66018 mutex_exit(&vdcp->lock); 47220a55fbb7Slm66018 4723e1ebb9ecSlm66018 /* probe gives the count of how many entries were processed */ 4724366a92acSlm66018 DTRACE_PROBE2(processed, int, 1, vdc_t *, vdcp); 47250a55fbb7Slm66018 47263af08d82Slm66018 return (0); 47270a55fbb7Slm66018 } 47280a55fbb7Slm66018 47290a55fbb7Slm66018 47300a55fbb7Slm66018 /* 47310a55fbb7Slm66018 * Function: 47320a55fbb7Slm66018 * vdc_handle_ver_msg() 47330a55fbb7Slm66018 * 47340a55fbb7Slm66018 * Description: 47350a55fbb7Slm66018 * 47360a55fbb7Slm66018 * Arguments: 47370a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 47380a55fbb7Slm66018 * ver_msg - LDC message sent by vDisk server 47390a55fbb7Slm66018 * 47400a55fbb7Slm66018 * Return Code: 47410a55fbb7Slm66018 * 0 - Success 47420a55fbb7Slm66018 */ 47430a55fbb7Slm66018 static int 47440a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg) 47450a55fbb7Slm66018 { 47460a55fbb7Slm66018 int status = 0; 47470a55fbb7Slm66018 47480a55fbb7Slm66018 ASSERT(vdc != NULL); 47490a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 47500a55fbb7Slm66018 47510a55fbb7Slm66018 if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) { 47520a55fbb7Slm66018 return (EPROTO); 47530a55fbb7Slm66018 } 47540a55fbb7Slm66018 47550a55fbb7Slm66018 if (ver_msg->dev_class != VDEV_DISK_SERVER) { 47560a55fbb7Slm66018 return (EINVAL); 47570a55fbb7Slm66018 } 47580a55fbb7Slm66018 47590a55fbb7Slm66018 switch (ver_msg->tag.vio_subtype) { 47600a55fbb7Slm66018 case VIO_SUBTYPE_ACK: 47610a55fbb7Slm66018 /* 47620a55fbb7Slm66018 * We check to see if the version returned is indeed supported 47630a55fbb7Slm66018 * (The server may have also adjusted the minor number downwards 47640a55fbb7Slm66018 * and if so 'ver_msg' will contain the actual version agreed) 47650a55fbb7Slm66018 */ 47660a55fbb7Slm66018 if (vdc_is_supported_version(ver_msg)) { 47670a55fbb7Slm66018 vdc->ver.major = ver_msg->ver_major; 47680a55fbb7Slm66018 vdc->ver.minor = ver_msg->ver_minor; 47690a55fbb7Slm66018 ASSERT(vdc->ver.major > 0); 47700a55fbb7Slm66018 } else { 47710a55fbb7Slm66018 status = EPROTO; 47720a55fbb7Slm66018 } 47730a55fbb7Slm66018 break; 47740a55fbb7Slm66018 47750a55fbb7Slm66018 case VIO_SUBTYPE_NACK: 47760a55fbb7Slm66018 /* 47770a55fbb7Slm66018 * call vdc_is_supported_version() which will return the next 47780a55fbb7Slm66018 * supported version (if any) in 'ver_msg' 47790a55fbb7Slm66018 */ 47800a55fbb7Slm66018 (void) vdc_is_supported_version(ver_msg); 47810a55fbb7Slm66018 if (ver_msg->ver_major > 0) { 47820a55fbb7Slm66018 size_t len = sizeof (*ver_msg); 47830a55fbb7Slm66018 47840a55fbb7Slm66018 ASSERT(vdc->ver.major > 0); 47850a55fbb7Slm66018 47860a55fbb7Slm66018 /* reset the necessary fields and resend */ 47870a55fbb7Slm66018 ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO; 47880a55fbb7Slm66018 ver_msg->dev_class = VDEV_DISK; 47890a55fbb7Slm66018 47900a55fbb7Slm66018 status = vdc_send(vdc, (caddr_t)ver_msg, &len); 47913af08d82Slm66018 DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n", 47920a55fbb7Slm66018 vdc->instance, status); 47930a55fbb7Slm66018 if (len != sizeof (*ver_msg)) 47940a55fbb7Slm66018 status = EBADMSG; 47950a55fbb7Slm66018 } else { 479687a7269eSachartre DMSG(vdc, 0, "[%d] No common version with vDisk server", 479787a7269eSachartre vdc->instance); 47980a55fbb7Slm66018 status = ENOTSUP; 47990a55fbb7Slm66018 } 48000a55fbb7Slm66018 48010a55fbb7Slm66018 break; 48021ae08745Sheppo case VIO_SUBTYPE_INFO: 48031ae08745Sheppo /* 48041ae08745Sheppo * Handle the case where vds starts handshake 4805eff7243fSlm66018 * (for now only vdc is the instigator) 48061ae08745Sheppo */ 48071ae08745Sheppo status = ENOTSUP; 48081ae08745Sheppo break; 48091ae08745Sheppo 48101ae08745Sheppo default: 48110a55fbb7Slm66018 status = EINVAL; 48121ae08745Sheppo break; 48131ae08745Sheppo } 48141ae08745Sheppo 48150a55fbb7Slm66018 return (status); 48160a55fbb7Slm66018 } 48170a55fbb7Slm66018 48180a55fbb7Slm66018 /* 48190a55fbb7Slm66018 * Function: 48200a55fbb7Slm66018 * vdc_handle_attr_msg() 48210a55fbb7Slm66018 * 48220a55fbb7Slm66018 * Description: 48230a55fbb7Slm66018 * 48240a55fbb7Slm66018 * Arguments: 48250a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 48260a55fbb7Slm66018 * attr_msg - LDC message sent by vDisk server 48270a55fbb7Slm66018 * 48280a55fbb7Slm66018 * Return Code: 48290a55fbb7Slm66018 * 0 - Success 48300a55fbb7Slm66018 */ 48310a55fbb7Slm66018 static int 48320a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg) 48330a55fbb7Slm66018 { 48340a55fbb7Slm66018 int status = 0; 48350a55fbb7Slm66018 48360a55fbb7Slm66018 ASSERT(vdc != NULL); 48370a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 48380a55fbb7Slm66018 48390a55fbb7Slm66018 if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) { 48400a55fbb7Slm66018 return (EPROTO); 48410a55fbb7Slm66018 } 48420a55fbb7Slm66018 48430a55fbb7Slm66018 switch (attr_msg->tag.vio_subtype) { 48441ae08745Sheppo case VIO_SUBTYPE_ACK: 48451ae08745Sheppo /* 48461ae08745Sheppo * We now verify the attributes sent by vds. 48471ae08745Sheppo */ 484878fcd0a1Sachartre if (attr_msg->vdisk_size == 0) { 484978fcd0a1Sachartre DMSG(vdc, 0, "[%d] Invalid disk size from vds", 485078fcd0a1Sachartre vdc->instance); 485178fcd0a1Sachartre status = EINVAL; 485278fcd0a1Sachartre break; 485378fcd0a1Sachartre } 485478fcd0a1Sachartre 485578fcd0a1Sachartre if (attr_msg->max_xfer_sz == 0) { 485678fcd0a1Sachartre DMSG(vdc, 0, "[%d] Invalid transfer size from vds", 485778fcd0a1Sachartre vdc->instance); 485878fcd0a1Sachartre status = EINVAL; 485978fcd0a1Sachartre break; 486078fcd0a1Sachartre } 486178fcd0a1Sachartre 48622f5224aeSachartre if (attr_msg->vdisk_size == VD_SIZE_UNKNOWN) { 48632f5224aeSachartre DMSG(vdc, 0, "[%d] Unknown disk size from vds", 48642f5224aeSachartre vdc->instance); 48652f5224aeSachartre attr_msg->vdisk_size = 0; 48662f5224aeSachartre } 4867de3a5331SRamesh Chitrothu /* update disk, block and transfer sizes */ 4868de3a5331SRamesh Chitrothu vdc_update_size(vdc, attr_msg->vdisk_size, 4869de3a5331SRamesh Chitrothu attr_msg->vdisk_block_size, attr_msg->max_xfer_sz); 48701ae08745Sheppo vdc->vdisk_type = attr_msg->vdisk_type; 487117cadca8Slm66018 vdc->operations = attr_msg->operations; 487217cadca8Slm66018 if (vio_ver_is_supported(vdc->ver, 1, 1)) 487317cadca8Slm66018 vdc->vdisk_media = attr_msg->vdisk_media; 487417cadca8Slm66018 else 487517cadca8Slm66018 vdc->vdisk_media = 0; 48761ae08745Sheppo 48773af08d82Slm66018 DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n", 4878e1ebb9ecSlm66018 vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz); 48793af08d82Slm66018 DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n", 4880e1ebb9ecSlm66018 vdc->instance, vdc->block_size, 4881e1ebb9ecSlm66018 attr_msg->vdisk_block_size); 4882e1ebb9ecSlm66018 4883f0ca1d9aSsb155480 if ((attr_msg->xfer_mode != VIO_DRING_MODE_V1_0) || 48841ae08745Sheppo (attr_msg->vdisk_size > INT64_MAX) || 488517cadca8Slm66018 (attr_msg->operations == 0) || 48861ae08745Sheppo (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) { 48873af08d82Slm66018 DMSG(vdc, 0, "[%d] Invalid attributes from vds", 4888e1ebb9ecSlm66018 vdc->instance); 48891ae08745Sheppo status = EINVAL; 48901ae08745Sheppo break; 48911ae08745Sheppo } 48921ae08745Sheppo 489378fcd0a1Sachartre /* 489478fcd0a1Sachartre * Now that we have received all attributes we can create a 489578fcd0a1Sachartre * fake geometry for the disk. 489678fcd0a1Sachartre */ 489778fcd0a1Sachartre vdc_create_fake_geometry(vdc); 48981ae08745Sheppo break; 48991ae08745Sheppo 49001ae08745Sheppo case VIO_SUBTYPE_NACK: 49011ae08745Sheppo /* 49021ae08745Sheppo * vds could not handle the attributes we sent so we 49031ae08745Sheppo * stop negotiating. 49041ae08745Sheppo */ 49051ae08745Sheppo status = EPROTO; 49061ae08745Sheppo break; 49071ae08745Sheppo 49081ae08745Sheppo case VIO_SUBTYPE_INFO: 49091ae08745Sheppo /* 49101ae08745Sheppo * Handle the case where vds starts the handshake 49111ae08745Sheppo * (for now; vdc is the only supported instigatior) 49121ae08745Sheppo */ 49131ae08745Sheppo status = ENOTSUP; 49141ae08745Sheppo break; 49151ae08745Sheppo 49161ae08745Sheppo default: 49171ae08745Sheppo status = ENOTSUP; 49181ae08745Sheppo break; 49191ae08745Sheppo } 49201ae08745Sheppo 49210a55fbb7Slm66018 return (status); 49221ae08745Sheppo } 49231ae08745Sheppo 49240a55fbb7Slm66018 /* 49250a55fbb7Slm66018 * Function: 49260a55fbb7Slm66018 * vdc_handle_dring_reg_msg() 49270a55fbb7Slm66018 * 49280a55fbb7Slm66018 * Description: 49290a55fbb7Slm66018 * 49300a55fbb7Slm66018 * Arguments: 49310a55fbb7Slm66018 * vdc - soft state pointer for this instance of the driver. 49320a55fbb7Slm66018 * dring_msg - LDC message sent by vDisk server 49330a55fbb7Slm66018 * 49340a55fbb7Slm66018 * Return Code: 49350a55fbb7Slm66018 * 0 - Success 49360a55fbb7Slm66018 */ 49370a55fbb7Slm66018 static int 49380a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg) 49390a55fbb7Slm66018 { 49400a55fbb7Slm66018 int status = 0; 49411ae08745Sheppo 49420a55fbb7Slm66018 ASSERT(vdc != NULL); 49430a55fbb7Slm66018 ASSERT(mutex_owned(&vdc->lock)); 49440a55fbb7Slm66018 49450a55fbb7Slm66018 if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) { 49460a55fbb7Slm66018 return (EPROTO); 49470a55fbb7Slm66018 } 49480a55fbb7Slm66018 49490a55fbb7Slm66018 switch (dring_msg->tag.vio_subtype) { 49500a55fbb7Slm66018 case VIO_SUBTYPE_ACK: 49511ae08745Sheppo /* save the received dring_ident */ 49521ae08745Sheppo vdc->dring_ident = dring_msg->dring_ident; 49533af08d82Slm66018 DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n", 4954e1ebb9ecSlm66018 vdc->instance, vdc->dring_ident); 49551ae08745Sheppo break; 49561ae08745Sheppo 49571ae08745Sheppo case VIO_SUBTYPE_NACK: 49581ae08745Sheppo /* 49591ae08745Sheppo * vds could not handle the DRing info we sent so we 49601ae08745Sheppo * stop negotiating. 49611ae08745Sheppo */ 49623af08d82Slm66018 DMSG(vdc, 0, "[%d] server could not register DRing\n", 49633af08d82Slm66018 vdc->instance); 49641ae08745Sheppo status = EPROTO; 49651ae08745Sheppo break; 49661ae08745Sheppo 49671ae08745Sheppo case VIO_SUBTYPE_INFO: 49681ae08745Sheppo /* 49691ae08745Sheppo * Handle the case where vds starts handshake 49701ae08745Sheppo * (for now only vdc is the instigatior) 49711ae08745Sheppo */ 49721ae08745Sheppo status = ENOTSUP; 49731ae08745Sheppo break; 49741ae08745Sheppo default: 49751ae08745Sheppo status = ENOTSUP; 49761ae08745Sheppo } 49771ae08745Sheppo 49781ae08745Sheppo return (status); 49791ae08745Sheppo } 49801ae08745Sheppo 49811ae08745Sheppo /* 49821ae08745Sheppo * Function: 49831ae08745Sheppo * vdc_verify_seq_num() 49841ae08745Sheppo * 49851ae08745Sheppo * Description: 4986e1ebb9ecSlm66018 * This functions verifies that the sequence number sent back by the vDisk 4987e1ebb9ecSlm66018 * server with the latest message is what is expected (i.e. it is greater 4988e1ebb9ecSlm66018 * than the last seq num sent by the vDisk server and less than or equal 4989e1ebb9ecSlm66018 * to the last seq num generated by vdc). 4990e1ebb9ecSlm66018 * 4991e1ebb9ecSlm66018 * It then checks the request ID to see if any requests need processing 4992e1ebb9ecSlm66018 * in the DRing. 49931ae08745Sheppo * 49941ae08745Sheppo * Arguments: 49951ae08745Sheppo * vdc - soft state pointer for this instance of the driver. 49961ae08745Sheppo * dring_msg - pointer to the LDC message sent by vds 49971ae08745Sheppo * 49981ae08745Sheppo * Return Code: 4999e1ebb9ecSlm66018 * VDC_SEQ_NUM_TODO - Message needs to be processed 5000e1ebb9ecSlm66018 * VDC_SEQ_NUM_SKIP - Message has already been processed 5001e1ebb9ecSlm66018 * VDC_SEQ_NUM_INVALID - The seq numbers are so out of sync, 5002e1ebb9ecSlm66018 * vdc cannot deal with them 50031ae08745Sheppo */ 5004e1ebb9ecSlm66018 static int 5005e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg) 50061ae08745Sheppo { 50071ae08745Sheppo ASSERT(vdc != NULL); 50081ae08745Sheppo ASSERT(dring_msg != NULL); 5009d10e4ef2Snarayan ASSERT(mutex_owned(&vdc->lock)); 50101ae08745Sheppo 50111ae08745Sheppo /* 50121ae08745Sheppo * Check to see if the messages were responded to in the correct 5013e1ebb9ecSlm66018 * order by vds. 50141ae08745Sheppo */ 5015e1ebb9ecSlm66018 if ((dring_msg->seq_num <= vdc->seq_num_reply) || 5016e1ebb9ecSlm66018 (dring_msg->seq_num > vdc->seq_num)) { 50173af08d82Slm66018 DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: " 5018e1ebb9ecSlm66018 "%lu > expected <= %lu (last proc req %lu sent %lu)\n", 5019e1ebb9ecSlm66018 vdc->instance, dring_msg->seq_num, 5020e1ebb9ecSlm66018 vdc->seq_num_reply, vdc->seq_num, 5021e1ebb9ecSlm66018 vdc->req_id_proc, vdc->req_id); 5022e1ebb9ecSlm66018 return (VDC_SEQ_NUM_INVALID); 50231ae08745Sheppo } 5024e1ebb9ecSlm66018 vdc->seq_num_reply = dring_msg->seq_num; 50251ae08745Sheppo 5026e1ebb9ecSlm66018 if (vdc->req_id_proc < vdc->req_id) 5027e1ebb9ecSlm66018 return (VDC_SEQ_NUM_TODO); 5028e1ebb9ecSlm66018 else 5029e1ebb9ecSlm66018 return (VDC_SEQ_NUM_SKIP); 50301ae08745Sheppo } 50311ae08745Sheppo 50320a55fbb7Slm66018 50330a55fbb7Slm66018 /* 50340a55fbb7Slm66018 * Function: 50350a55fbb7Slm66018 * vdc_is_supported_version() 50360a55fbb7Slm66018 * 50370a55fbb7Slm66018 * Description: 50380a55fbb7Slm66018 * This routine checks if the major/minor version numbers specified in 50390a55fbb7Slm66018 * 'ver_msg' are supported. If not it finds the next version that is 50400a55fbb7Slm66018 * in the supported version list 'vdc_version[]' and sets the fields in 50410a55fbb7Slm66018 * 'ver_msg' to those values 50420a55fbb7Slm66018 * 50430a55fbb7Slm66018 * Arguments: 50440a55fbb7Slm66018 * ver_msg - LDC message sent by vDisk server 50450a55fbb7Slm66018 * 50460a55fbb7Slm66018 * Return Code: 50470a55fbb7Slm66018 * B_TRUE - Success 50480a55fbb7Slm66018 * B_FALSE - Version not supported 50490a55fbb7Slm66018 */ 50500a55fbb7Slm66018 static boolean_t 50510a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg) 50520a55fbb7Slm66018 { 50530a55fbb7Slm66018 int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]); 50540a55fbb7Slm66018 50550a55fbb7Slm66018 for (int i = 0; i < vdc_num_versions; i++) { 50560a55fbb7Slm66018 ASSERT(vdc_version[i].major > 0); 50570a55fbb7Slm66018 ASSERT((i == 0) || 50580a55fbb7Slm66018 (vdc_version[i].major < vdc_version[i-1].major)); 50590a55fbb7Slm66018 50600a55fbb7Slm66018 /* 50610a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 50620a55fbb7Slm66018 * necessary, down to the highest value supported by this 50630a55fbb7Slm66018 * client. The server should support all minor versions lower 50640a55fbb7Slm66018 * than the value it sent 50650a55fbb7Slm66018 */ 50660a55fbb7Slm66018 if (ver_msg->ver_major == vdc_version[i].major) { 50670a55fbb7Slm66018 if (ver_msg->ver_minor > vdc_version[i].minor) { 50683af08d82Slm66018 DMSGX(0, 50693af08d82Slm66018 "Adjusting minor version from %u to %u", 50700a55fbb7Slm66018 ver_msg->ver_minor, vdc_version[i].minor); 50710a55fbb7Slm66018 ver_msg->ver_minor = vdc_version[i].minor; 50720a55fbb7Slm66018 } 50730a55fbb7Slm66018 return (B_TRUE); 50740a55fbb7Slm66018 } 50750a55fbb7Slm66018 50760a55fbb7Slm66018 /* 50770a55fbb7Slm66018 * If the message contains a higher major version number, set 50780a55fbb7Slm66018 * the message's major/minor versions to the current values 50790a55fbb7Slm66018 * and return false, so this message will get resent with 50800a55fbb7Slm66018 * these values, and the server will potentially try again 50810a55fbb7Slm66018 * with the same or a lower version 50820a55fbb7Slm66018 */ 50830a55fbb7Slm66018 if (ver_msg->ver_major > vdc_version[i].major) { 50840a55fbb7Slm66018 ver_msg->ver_major = vdc_version[i].major; 50850a55fbb7Slm66018 ver_msg->ver_minor = vdc_version[i].minor; 50863af08d82Slm66018 DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n", 50870a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 50880a55fbb7Slm66018 50890a55fbb7Slm66018 return (B_FALSE); 50900a55fbb7Slm66018 } 50910a55fbb7Slm66018 50920a55fbb7Slm66018 /* 50930a55fbb7Slm66018 * Otherwise, the message's major version is less than the 50940a55fbb7Slm66018 * current major version, so continue the loop to the next 50950a55fbb7Slm66018 * (lower) supported version 50960a55fbb7Slm66018 */ 50970a55fbb7Slm66018 } 50980a55fbb7Slm66018 50990a55fbb7Slm66018 /* 51000a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 51010a55fbb7Slm66018 * message to terminate negotiation 51020a55fbb7Slm66018 */ 51030a55fbb7Slm66018 ver_msg->ver_major = 0; 51040a55fbb7Slm66018 ver_msg->ver_minor = 0; 51050a55fbb7Slm66018 51060a55fbb7Slm66018 return (B_FALSE); 51070a55fbb7Slm66018 } 51081ae08745Sheppo /* -------------------------------------------------------------------------- */ 51091ae08745Sheppo 51101ae08745Sheppo /* 51111ae08745Sheppo * DKIO(7) support 51121ae08745Sheppo */ 51131ae08745Sheppo 51141ae08745Sheppo typedef struct vdc_dk_arg { 51151ae08745Sheppo struct dk_callback dkc; 51161ae08745Sheppo int mode; 51171ae08745Sheppo dev_t dev; 51181ae08745Sheppo vdc_t *vdc; 51191ae08745Sheppo } vdc_dk_arg_t; 51201ae08745Sheppo 51211ae08745Sheppo /* 51221ae08745Sheppo * Function: 51231ae08745Sheppo * vdc_dkio_flush_cb() 51241ae08745Sheppo * 51251ae08745Sheppo * Description: 51261ae08745Sheppo * This routine is a callback for DKIOCFLUSHWRITECACHE which can be called 51271ae08745Sheppo * by kernel code. 51281ae08745Sheppo * 51291ae08745Sheppo * Arguments: 51301ae08745Sheppo * arg - a pointer to a vdc_dk_arg_t structure. 51311ae08745Sheppo */ 51321ae08745Sheppo void 51331ae08745Sheppo vdc_dkio_flush_cb(void *arg) 51341ae08745Sheppo { 51351ae08745Sheppo struct vdc_dk_arg *dk_arg = (struct vdc_dk_arg *)arg; 51361ae08745Sheppo struct dk_callback *dkc = NULL; 51371ae08745Sheppo vdc_t *vdc = NULL; 51381ae08745Sheppo int rv; 51391ae08745Sheppo 51401ae08745Sheppo if (dk_arg == NULL) { 51413af08d82Slm66018 cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n"); 51421ae08745Sheppo return; 51431ae08745Sheppo } 51441ae08745Sheppo dkc = &dk_arg->dkc; 51451ae08745Sheppo vdc = dk_arg->vdc; 51461ae08745Sheppo ASSERT(vdc != NULL); 51471ae08745Sheppo 51483af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0, 51492f5224aeSachartre VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir, B_TRUE); 51501ae08745Sheppo if (rv != 0) { 51513af08d82Slm66018 DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n", 5152e1ebb9ecSlm66018 vdc->instance, rv, 51531ae08745Sheppo ddi_model_convert_from(dk_arg->mode & FMODELS)); 51541ae08745Sheppo } 51551ae08745Sheppo 51561ae08745Sheppo /* 51571ae08745Sheppo * Trigger the call back to notify the caller the the ioctl call has 51581ae08745Sheppo * been completed. 51591ae08745Sheppo */ 51601ae08745Sheppo if ((dk_arg->mode & FKIOCTL) && 51611ae08745Sheppo (dkc != NULL) && 51621ae08745Sheppo (dkc->dkc_callback != NULL)) { 51631ae08745Sheppo ASSERT(dkc->dkc_cookie != NULL); 51648e6a2a04Slm66018 (*dkc->dkc_callback)(dkc->dkc_cookie, rv); 51651ae08745Sheppo } 51661ae08745Sheppo 51671ae08745Sheppo /* Indicate that one less DKIO write flush is outstanding */ 51681ae08745Sheppo mutex_enter(&vdc->lock); 51691ae08745Sheppo vdc->dkio_flush_pending--; 51701ae08745Sheppo ASSERT(vdc->dkio_flush_pending >= 0); 51711ae08745Sheppo mutex_exit(&vdc->lock); 51728e6a2a04Slm66018 51738e6a2a04Slm66018 /* free the mem that was allocated when the callback was dispatched */ 51748e6a2a04Slm66018 kmem_free(arg, sizeof (vdc_dk_arg_t)); 51751ae08745Sheppo } 51761ae08745Sheppo 51771ae08745Sheppo /* 517887a7269eSachartre * Function: 51799642afceSachartre * vdc_dkio_gapart() 518087a7269eSachartre * 518187a7269eSachartre * Description: 518287a7269eSachartre * This function implements the DKIOCGAPART ioctl. 518387a7269eSachartre * 518487a7269eSachartre * Arguments: 518578fcd0a1Sachartre * vdc - soft state pointer 518687a7269eSachartre * arg - a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure 518787a7269eSachartre * flag - ioctl flags 518887a7269eSachartre */ 518987a7269eSachartre static int 51909642afceSachartre vdc_dkio_gapart(vdc_t *vdc, caddr_t arg, int flag) 519187a7269eSachartre { 519278fcd0a1Sachartre struct dk_geom *geom; 5193342440ecSPrasad Singamsetty struct extvtoc *vtoc; 519487a7269eSachartre union { 519587a7269eSachartre struct dk_map map[NDKMAP]; 519687a7269eSachartre struct dk_map32 map32[NDKMAP]; 519787a7269eSachartre } data; 519887a7269eSachartre int i, rv, size; 519987a7269eSachartre 520078fcd0a1Sachartre mutex_enter(&vdc->lock); 520187a7269eSachartre 520278fcd0a1Sachartre if ((rv = vdc_validate_geometry(vdc)) != 0) { 520378fcd0a1Sachartre mutex_exit(&vdc->lock); 520487a7269eSachartre return (rv); 520578fcd0a1Sachartre } 520687a7269eSachartre 5207342440ecSPrasad Singamsetty if (vdc->vdisk_size > VD_OLDVTOC_LIMIT) { 5208342440ecSPrasad Singamsetty mutex_exit(&vdc->lock); 5209342440ecSPrasad Singamsetty return (EOVERFLOW); 5210342440ecSPrasad Singamsetty } 5211342440ecSPrasad Singamsetty 521278fcd0a1Sachartre vtoc = vdc->vtoc; 521378fcd0a1Sachartre geom = vdc->geom; 521487a7269eSachartre 521587a7269eSachartre if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) { 521687a7269eSachartre 521778fcd0a1Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 521878fcd0a1Sachartre data.map32[i].dkl_cylno = vtoc->v_part[i].p_start / 521978fcd0a1Sachartre (geom->dkg_nhead * geom->dkg_nsect); 522078fcd0a1Sachartre data.map32[i].dkl_nblk = vtoc->v_part[i].p_size; 522187a7269eSachartre } 522287a7269eSachartre size = NDKMAP * sizeof (struct dk_map32); 522387a7269eSachartre 522487a7269eSachartre } else { 522587a7269eSachartre 522678fcd0a1Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 522778fcd0a1Sachartre data.map[i].dkl_cylno = vtoc->v_part[i].p_start / 522878fcd0a1Sachartre (geom->dkg_nhead * geom->dkg_nsect); 522978fcd0a1Sachartre data.map[i].dkl_nblk = vtoc->v_part[i].p_size; 523087a7269eSachartre } 523187a7269eSachartre size = NDKMAP * sizeof (struct dk_map); 523287a7269eSachartre 523387a7269eSachartre } 523487a7269eSachartre 523578fcd0a1Sachartre mutex_exit(&vdc->lock); 523678fcd0a1Sachartre 523787a7269eSachartre if (ddi_copyout(&data, arg, size, flag) != 0) 523887a7269eSachartre return (EFAULT); 523987a7269eSachartre 524087a7269eSachartre return (0); 524187a7269eSachartre } 524287a7269eSachartre 524387a7269eSachartre /* 524487a7269eSachartre * Function: 52459642afceSachartre * vdc_dkio_partition() 52469642afceSachartre * 52479642afceSachartre * Description: 52489642afceSachartre * This function implements the DKIOCPARTITION ioctl. 52499642afceSachartre * 52509642afceSachartre * Arguments: 52519642afceSachartre * vdc - soft state pointer 52529642afceSachartre * arg - a pointer to a struct partition64 structure 52539642afceSachartre * flag - ioctl flags 52549642afceSachartre */ 52559642afceSachartre static int 52569642afceSachartre vdc_dkio_partition(vdc_t *vdc, caddr_t arg, int flag) 52579642afceSachartre { 52589642afceSachartre struct partition64 p64; 52599642afceSachartre efi_gpt_t *gpt; 52609642afceSachartre efi_gpe_t *gpe; 52619642afceSachartre vd_efi_dev_t edev; 52629642afceSachartre uint_t partno; 52639642afceSachartre int rv; 52649642afceSachartre 52659642afceSachartre if (ddi_copyin(arg, &p64, sizeof (struct partition64), flag)) { 52669642afceSachartre return (EFAULT); 52679642afceSachartre } 52689642afceSachartre 52699642afceSachartre VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl); 52709642afceSachartre 52719642afceSachartre if ((rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe)) != 0) { 52729642afceSachartre return (rv); 52739642afceSachartre } 52749642afceSachartre 52759642afceSachartre partno = p64.p_partno; 52769642afceSachartre 52779642afceSachartre if (partno >= gpt->efi_gpt_NumberOfPartitionEntries) { 52789642afceSachartre vd_efi_free(&edev, gpt, gpe); 52799642afceSachartre return (ESRCH); 52809642afceSachartre } 52819642afceSachartre 52829642afceSachartre bcopy(&gpe[partno].efi_gpe_PartitionTypeGUID, &p64.p_type, 52839642afceSachartre sizeof (struct uuid)); 52849642afceSachartre p64.p_start = gpe[partno].efi_gpe_StartingLBA; 52859642afceSachartre p64.p_size = gpe[partno].efi_gpe_EndingLBA - p64.p_start + 1; 52869642afceSachartre 52879642afceSachartre if (ddi_copyout(&p64, arg, sizeof (struct partition64), flag)) { 52889642afceSachartre vd_efi_free(&edev, gpt, gpe); 52899642afceSachartre return (EFAULT); 52909642afceSachartre } 52919642afceSachartre 52929642afceSachartre vd_efi_free(&edev, gpt, gpe); 52939642afceSachartre return (0); 52949642afceSachartre } 52959642afceSachartre 52969642afceSachartre /* 52979642afceSachartre * Function: 529887a7269eSachartre * vdc_dioctl_rwcmd() 529987a7269eSachartre * 530087a7269eSachartre * Description: 530187a7269eSachartre * This function implements the DIOCTL_RWCMD ioctl. This ioctl is used 530287a7269eSachartre * for DKC_DIRECT disks to read or write at an absolute disk offset. 530387a7269eSachartre * 530487a7269eSachartre * Arguments: 530587a7269eSachartre * dev - device 530687a7269eSachartre * arg - a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure 530787a7269eSachartre * flag - ioctl flags 530887a7269eSachartre */ 530987a7269eSachartre static int 531087a7269eSachartre vdc_dioctl_rwcmd(dev_t dev, caddr_t arg, int flag) 531187a7269eSachartre { 531287a7269eSachartre struct dadkio_rwcmd32 rwcmd32; 531387a7269eSachartre struct dadkio_rwcmd rwcmd; 531487a7269eSachartre struct iovec aiov; 531587a7269eSachartre struct uio auio; 531687a7269eSachartre int rw, status; 531787a7269eSachartre struct buf *buf; 531887a7269eSachartre 531987a7269eSachartre if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) { 532087a7269eSachartre if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32, 532187a7269eSachartre sizeof (struct dadkio_rwcmd32), flag)) { 532287a7269eSachartre return (EFAULT); 532387a7269eSachartre } 532487a7269eSachartre rwcmd.cmd = rwcmd32.cmd; 532587a7269eSachartre rwcmd.flags = rwcmd32.flags; 532687a7269eSachartre rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr; 532787a7269eSachartre rwcmd.buflen = rwcmd32.buflen; 532887a7269eSachartre rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr; 532987a7269eSachartre } else { 533087a7269eSachartre if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd, 533187a7269eSachartre sizeof (struct dadkio_rwcmd), flag)) { 533287a7269eSachartre return (EFAULT); 533387a7269eSachartre } 533487a7269eSachartre } 533587a7269eSachartre 533687a7269eSachartre switch (rwcmd.cmd) { 533787a7269eSachartre case DADKIO_RWCMD_READ: 533887a7269eSachartre rw = B_READ; 533987a7269eSachartre break; 534087a7269eSachartre case DADKIO_RWCMD_WRITE: 534187a7269eSachartre rw = B_WRITE; 534287a7269eSachartre break; 534387a7269eSachartre default: 534487a7269eSachartre return (EINVAL); 534587a7269eSachartre } 534687a7269eSachartre 534787a7269eSachartre bzero((caddr_t)&aiov, sizeof (struct iovec)); 534887a7269eSachartre aiov.iov_base = rwcmd.bufaddr; 534987a7269eSachartre aiov.iov_len = rwcmd.buflen; 535087a7269eSachartre 535187a7269eSachartre bzero((caddr_t)&auio, sizeof (struct uio)); 535287a7269eSachartre auio.uio_iov = &aiov; 535387a7269eSachartre auio.uio_iovcnt = 1; 535487a7269eSachartre auio.uio_loffset = rwcmd.blkaddr * DEV_BSIZE; 535587a7269eSachartre auio.uio_resid = rwcmd.buflen; 535687a7269eSachartre auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE; 535787a7269eSachartre 535887a7269eSachartre buf = kmem_alloc(sizeof (buf_t), KM_SLEEP); 535987a7269eSachartre bioinit(buf); 536087a7269eSachartre /* 536187a7269eSachartre * We use the private field of buf to specify that this is an 536287a7269eSachartre * I/O using an absolute offset. 536387a7269eSachartre */ 536487a7269eSachartre buf->b_private = (void *)VD_SLICE_NONE; 536587a7269eSachartre 536687a7269eSachartre status = physio(vdc_strategy, buf, dev, rw, vdc_min, &auio); 536787a7269eSachartre 536887a7269eSachartre biofini(buf); 536987a7269eSachartre kmem_free(buf, sizeof (buf_t)); 537087a7269eSachartre 537187a7269eSachartre return (status); 537287a7269eSachartre } 537387a7269eSachartre 537487a7269eSachartre /* 53752f5224aeSachartre * Allocate a buffer for a VD_OP_SCSICMD operation. The size of the allocated 53762f5224aeSachartre * buffer is returned in alloc_len. 53772f5224aeSachartre */ 53782f5224aeSachartre static vd_scsi_t * 53792f5224aeSachartre vdc_scsi_alloc(int cdb_len, int sense_len, int datain_len, int dataout_len, 53802f5224aeSachartre int *alloc_len) 53812f5224aeSachartre { 53822f5224aeSachartre vd_scsi_t *vd_scsi; 53832f5224aeSachartre int vd_scsi_len = VD_SCSI_SIZE; 53842f5224aeSachartre 53852f5224aeSachartre vd_scsi_len += P2ROUNDUP(cdb_len, sizeof (uint64_t)); 53862f5224aeSachartre vd_scsi_len += P2ROUNDUP(sense_len, sizeof (uint64_t)); 53872f5224aeSachartre vd_scsi_len += P2ROUNDUP(datain_len, sizeof (uint64_t)); 53882f5224aeSachartre vd_scsi_len += P2ROUNDUP(dataout_len, sizeof (uint64_t)); 53892f5224aeSachartre 53902f5224aeSachartre ASSERT(vd_scsi_len % sizeof (uint64_t) == 0); 53912f5224aeSachartre 53922f5224aeSachartre vd_scsi = kmem_zalloc(vd_scsi_len, KM_SLEEP); 53932f5224aeSachartre 53942f5224aeSachartre vd_scsi->cdb_len = cdb_len; 53952f5224aeSachartre vd_scsi->sense_len = sense_len; 53962f5224aeSachartre vd_scsi->datain_len = datain_len; 53972f5224aeSachartre vd_scsi->dataout_len = dataout_len; 53982f5224aeSachartre 53992f5224aeSachartre *alloc_len = vd_scsi_len; 54002f5224aeSachartre 54012f5224aeSachartre return (vd_scsi); 54022f5224aeSachartre } 54032f5224aeSachartre 54042f5224aeSachartre /* 54052f5224aeSachartre * Convert the status of a SCSI command to a Solaris return code. 54062f5224aeSachartre * 54072f5224aeSachartre * Arguments: 54082f5224aeSachartre * vd_scsi - The SCSI operation buffer. 54092f5224aeSachartre * log_error - indicate if an error message should be logged. 54102f5224aeSachartre * 54112f5224aeSachartre * Note that our SCSI error messages are rather primitive for the moment 54122f5224aeSachartre * and could be improved by decoding some data like the SCSI command and 54132f5224aeSachartre * the sense key. 54142f5224aeSachartre * 54152f5224aeSachartre * Return value: 54162f5224aeSachartre * 0 - Status is good. 54172f5224aeSachartre * EACCES - Status reports a reservation conflict. 54182f5224aeSachartre * ENOTSUP - Status reports a check condition and sense key 54192f5224aeSachartre * reports an illegal request. 54202f5224aeSachartre * EIO - Any other status. 54212f5224aeSachartre */ 54222f5224aeSachartre static int 54232f5224aeSachartre vdc_scsi_status(vdc_t *vdc, vd_scsi_t *vd_scsi, boolean_t log_error) 54242f5224aeSachartre { 54252f5224aeSachartre int rv; 54262f5224aeSachartre char path_str[MAXPATHLEN]; 54272f5224aeSachartre char panic_str[VDC_RESV_CONFLICT_FMT_LEN + MAXPATHLEN]; 54282f5224aeSachartre union scsi_cdb *cdb; 54292f5224aeSachartre struct scsi_extended_sense *sense; 54302f5224aeSachartre 54312f5224aeSachartre if (vd_scsi->cmd_status == STATUS_GOOD) 54322f5224aeSachartre /* no error */ 54332f5224aeSachartre return (0); 54342f5224aeSachartre 54352f5224aeSachartre /* when the tunable vdc_scsi_log_error is true we log all errors */ 54362f5224aeSachartre if (vdc_scsi_log_error) 54372f5224aeSachartre log_error = B_TRUE; 54382f5224aeSachartre 54392f5224aeSachartre if (log_error) { 54402f5224aeSachartre cmn_err(CE_WARN, "%s (vdc%d):\tError for Command: 0x%x)\n", 54412f5224aeSachartre ddi_pathname(vdc->dip, path_str), vdc->instance, 54422f5224aeSachartre GETCMD(VD_SCSI_DATA_CDB(vd_scsi))); 54432f5224aeSachartre } 54442f5224aeSachartre 54452f5224aeSachartre /* default returned value */ 54462f5224aeSachartre rv = EIO; 54472f5224aeSachartre 54482f5224aeSachartre switch (vd_scsi->cmd_status) { 54492f5224aeSachartre 54502f5224aeSachartre case STATUS_CHECK: 54512f5224aeSachartre case STATUS_TERMINATED: 54522f5224aeSachartre if (log_error) 54532f5224aeSachartre cmn_err(CE_CONT, "\tCheck Condition Error\n"); 54542f5224aeSachartre 54552f5224aeSachartre /* check sense buffer */ 54562f5224aeSachartre if (vd_scsi->sense_len == 0 || 54572f5224aeSachartre vd_scsi->sense_status != STATUS_GOOD) { 54582f5224aeSachartre if (log_error) 54592f5224aeSachartre cmn_err(CE_CONT, "\tNo Sense Data Available\n"); 54602f5224aeSachartre break; 54612f5224aeSachartre } 54622f5224aeSachartre 54632f5224aeSachartre sense = VD_SCSI_DATA_SENSE(vd_scsi); 54642f5224aeSachartre 54652f5224aeSachartre if (log_error) { 54662f5224aeSachartre cmn_err(CE_CONT, "\tSense Key: 0x%x\n" 54672f5224aeSachartre "\tASC: 0x%x, ASCQ: 0x%x\n", 54682f5224aeSachartre scsi_sense_key((uint8_t *)sense), 54692f5224aeSachartre scsi_sense_asc((uint8_t *)sense), 54702f5224aeSachartre scsi_sense_ascq((uint8_t *)sense)); 54712f5224aeSachartre } 54722f5224aeSachartre 54732f5224aeSachartre if (scsi_sense_key((uint8_t *)sense) == KEY_ILLEGAL_REQUEST) 54742f5224aeSachartre rv = ENOTSUP; 54752f5224aeSachartre break; 54762f5224aeSachartre 54772f5224aeSachartre case STATUS_BUSY: 54782f5224aeSachartre if (log_error) 54792f5224aeSachartre cmn_err(CE_NOTE, "\tDevice Busy\n"); 54802f5224aeSachartre break; 54812f5224aeSachartre 54822f5224aeSachartre case STATUS_RESERVATION_CONFLICT: 54832f5224aeSachartre /* 54842f5224aeSachartre * If the command was PERSISTENT_RESERVATION_[IN|OUT] then 54852f5224aeSachartre * reservation conflict could be due to various reasons like 54862f5224aeSachartre * incorrect keys, not registered or not reserved etc. So, 54872f5224aeSachartre * we should not panic in that case. 54882f5224aeSachartre */ 54892f5224aeSachartre cdb = VD_SCSI_DATA_CDB(vd_scsi); 54902f5224aeSachartre if (vdc->failfast_interval != 0 && 54912f5224aeSachartre cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_IN && 54922f5224aeSachartre cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_OUT) { 54932f5224aeSachartre /* failfast is enabled so we have to panic */ 54942f5224aeSachartre (void) snprintf(panic_str, sizeof (panic_str), 54952f5224aeSachartre VDC_RESV_CONFLICT_FMT_STR "%s", 54962f5224aeSachartre ddi_pathname(vdc->dip, path_str)); 54972f5224aeSachartre panic(panic_str); 54982f5224aeSachartre } 54992f5224aeSachartre if (log_error) 55002f5224aeSachartre cmn_err(CE_NOTE, "\tReservation Conflict\n"); 55012f5224aeSachartre rv = EACCES; 55022f5224aeSachartre break; 55032f5224aeSachartre 55042f5224aeSachartre case STATUS_QFULL: 55052f5224aeSachartre if (log_error) 55062f5224aeSachartre cmn_err(CE_NOTE, "\tQueue Full\n"); 55072f5224aeSachartre break; 55082f5224aeSachartre 55092f5224aeSachartre case STATUS_MET: 55102f5224aeSachartre case STATUS_INTERMEDIATE: 55112f5224aeSachartre case STATUS_SCSI2: 55122f5224aeSachartre case STATUS_INTERMEDIATE_MET: 55132f5224aeSachartre case STATUS_ACA_ACTIVE: 55142f5224aeSachartre if (log_error) 55152f5224aeSachartre cmn_err(CE_CONT, 55162f5224aeSachartre "\tUnexpected SCSI status received: 0x%x\n", 55172f5224aeSachartre vd_scsi->cmd_status); 55182f5224aeSachartre break; 55192f5224aeSachartre 55202f5224aeSachartre default: 55212f5224aeSachartre if (log_error) 55222f5224aeSachartre cmn_err(CE_CONT, 55232f5224aeSachartre "\tInvalid SCSI status received: 0x%x\n", 55242f5224aeSachartre vd_scsi->cmd_status); 55252f5224aeSachartre break; 55262f5224aeSachartre } 55272f5224aeSachartre 55282f5224aeSachartre return (rv); 55292f5224aeSachartre } 55302f5224aeSachartre 55312f5224aeSachartre /* 55322f5224aeSachartre * Implemented the USCSICMD uscsi(7I) ioctl. This ioctl is converted to 55332f5224aeSachartre * a VD_OP_SCSICMD operation which is sent to the vdisk server. If a SCSI 55342f5224aeSachartre * reset is requested (i.e. a flag USCSI_RESET* is set) then the ioctl is 55352f5224aeSachartre * converted to a VD_OP_RESET operation. 55362f5224aeSachartre */ 55372f5224aeSachartre static int 55382f5224aeSachartre vdc_uscsi_cmd(vdc_t *vdc, caddr_t arg, int mode) 55392f5224aeSachartre { 55402f5224aeSachartre struct uscsi_cmd uscsi; 55412f5224aeSachartre struct uscsi_cmd32 uscsi32; 55422f5224aeSachartre vd_scsi_t *vd_scsi; 55432f5224aeSachartre int vd_scsi_len; 55442f5224aeSachartre union scsi_cdb *cdb; 55452f5224aeSachartre struct scsi_extended_sense *sense; 55462f5224aeSachartre char *datain, *dataout; 55472f5224aeSachartre size_t cdb_len, datain_len, dataout_len, sense_len; 55482f5224aeSachartre int rv; 55492f5224aeSachartre 55502f5224aeSachartre if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 55512f5224aeSachartre if (ddi_copyin(arg, &uscsi32, sizeof (struct uscsi_cmd32), 55522f5224aeSachartre mode) != 0) 55532f5224aeSachartre return (EFAULT); 55542f5224aeSachartre uscsi_cmd32touscsi_cmd((&uscsi32), (&uscsi)); 55552f5224aeSachartre } else { 55562f5224aeSachartre if (ddi_copyin(arg, &uscsi, sizeof (struct uscsi_cmd), 55572f5224aeSachartre mode) != 0) 55582f5224aeSachartre return (EFAULT); 55592f5224aeSachartre } 55602f5224aeSachartre 55612f5224aeSachartre /* a uscsi reset is converted to a VD_OP_RESET operation */ 55622f5224aeSachartre if (uscsi.uscsi_flags & (USCSI_RESET | USCSI_RESET_LUN | 55632f5224aeSachartre USCSI_RESET_ALL)) { 55642f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_RESET, NULL, 0, 0, 0, CB_SYNC, 55652f5224aeSachartre (void *)(uint64_t)mode, VIO_both_dir, B_TRUE); 55662f5224aeSachartre return (rv); 55672f5224aeSachartre } 55682f5224aeSachartre 55692f5224aeSachartre /* cdb buffer length */ 55702f5224aeSachartre cdb_len = uscsi.uscsi_cdblen; 55712f5224aeSachartre 55722f5224aeSachartre /* data in and out buffers length */ 55732f5224aeSachartre if (uscsi.uscsi_flags & USCSI_READ) { 55742f5224aeSachartre datain_len = uscsi.uscsi_buflen; 55752f5224aeSachartre dataout_len = 0; 55762f5224aeSachartre } else { 55772f5224aeSachartre datain_len = 0; 55782f5224aeSachartre dataout_len = uscsi.uscsi_buflen; 55792f5224aeSachartre } 55802f5224aeSachartre 55812f5224aeSachartre /* sense buffer length */ 55822f5224aeSachartre if (uscsi.uscsi_flags & USCSI_RQENABLE) 55832f5224aeSachartre sense_len = uscsi.uscsi_rqlen; 55842f5224aeSachartre else 55852f5224aeSachartre sense_len = 0; 55862f5224aeSachartre 55872f5224aeSachartre /* allocate buffer for the VD_SCSICMD_OP operation */ 55882f5224aeSachartre vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len, 55892f5224aeSachartre &vd_scsi_len); 55902f5224aeSachartre 55912f5224aeSachartre /* 55922f5224aeSachartre * The documentation of USCSI_ISOLATE and USCSI_DIAGNOSE is very vague, 55932f5224aeSachartre * but basically they prevent a SCSI command from being retried in case 55942f5224aeSachartre * of an error. 55952f5224aeSachartre */ 55962f5224aeSachartre if ((uscsi.uscsi_flags & USCSI_ISOLATE) || 55972f5224aeSachartre (uscsi.uscsi_flags & USCSI_DIAGNOSE)) 55982f5224aeSachartre vd_scsi->options |= VD_SCSI_OPT_NORETRY; 55992f5224aeSachartre 56002f5224aeSachartre /* set task attribute */ 56012f5224aeSachartre if (uscsi.uscsi_flags & USCSI_NOTAG) { 56022f5224aeSachartre vd_scsi->task_attribute = 0; 56032f5224aeSachartre } else { 56042f5224aeSachartre if (uscsi.uscsi_flags & USCSI_HEAD) 56052f5224aeSachartre vd_scsi->task_attribute = VD_SCSI_TASK_ACA; 56062f5224aeSachartre else if (uscsi.uscsi_flags & USCSI_HTAG) 56072f5224aeSachartre vd_scsi->task_attribute = VD_SCSI_TASK_HQUEUE; 56082f5224aeSachartre else if (uscsi.uscsi_flags & USCSI_OTAG) 56092f5224aeSachartre vd_scsi->task_attribute = VD_SCSI_TASK_ORDERED; 56102f5224aeSachartre else 56112f5224aeSachartre vd_scsi->task_attribute = 0; 56122f5224aeSachartre } 56132f5224aeSachartre 56142f5224aeSachartre /* set timeout */ 56152f5224aeSachartre vd_scsi->timeout = uscsi.uscsi_timeout; 56162f5224aeSachartre 56172f5224aeSachartre /* copy-in cdb data */ 56182f5224aeSachartre cdb = VD_SCSI_DATA_CDB(vd_scsi); 56192f5224aeSachartre if (ddi_copyin(uscsi.uscsi_cdb, cdb, cdb_len, mode) != 0) { 56202f5224aeSachartre rv = EFAULT; 56212f5224aeSachartre goto done; 56222f5224aeSachartre } 56232f5224aeSachartre 56242f5224aeSachartre /* keep a pointer to the sense buffer */ 56252f5224aeSachartre sense = VD_SCSI_DATA_SENSE(vd_scsi); 56262f5224aeSachartre 56272f5224aeSachartre /* keep a pointer to the data-in buffer */ 56282f5224aeSachartre datain = (char *)VD_SCSI_DATA_IN(vd_scsi); 56292f5224aeSachartre 56302f5224aeSachartre /* copy-in request data to the data-out buffer */ 56312f5224aeSachartre dataout = (char *)VD_SCSI_DATA_OUT(vd_scsi); 56322f5224aeSachartre if (!(uscsi.uscsi_flags & USCSI_READ)) { 56332f5224aeSachartre if (ddi_copyin(uscsi.uscsi_bufaddr, dataout, dataout_len, 56342f5224aeSachartre mode)) { 56352f5224aeSachartre rv = EFAULT; 56362f5224aeSachartre goto done; 56372f5224aeSachartre } 56382f5224aeSachartre } 56392f5224aeSachartre 56402f5224aeSachartre /* submit the request */ 56412f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len, 56422f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE); 56432f5224aeSachartre 56442f5224aeSachartre if (rv != 0) 56452f5224aeSachartre goto done; 56462f5224aeSachartre 56472f5224aeSachartre /* update scsi status */ 56482f5224aeSachartre uscsi.uscsi_status = vd_scsi->cmd_status; 56492f5224aeSachartre 56502f5224aeSachartre /* update sense data */ 56512f5224aeSachartre if ((uscsi.uscsi_flags & USCSI_RQENABLE) && 56522f5224aeSachartre (uscsi.uscsi_status == STATUS_CHECK || 56532f5224aeSachartre uscsi.uscsi_status == STATUS_TERMINATED)) { 56542f5224aeSachartre 56552f5224aeSachartre uscsi.uscsi_rqstatus = vd_scsi->sense_status; 56562f5224aeSachartre 56572f5224aeSachartre if (uscsi.uscsi_rqstatus == STATUS_GOOD) { 56582f5224aeSachartre uscsi.uscsi_rqresid = uscsi.uscsi_rqlen - 56592f5224aeSachartre vd_scsi->sense_len; 56602f5224aeSachartre if (ddi_copyout(sense, uscsi.uscsi_rqbuf, 56612f5224aeSachartre vd_scsi->sense_len, mode) != 0) { 56622f5224aeSachartre rv = EFAULT; 56632f5224aeSachartre goto done; 56642f5224aeSachartre } 56652f5224aeSachartre } 56662f5224aeSachartre } 56672f5224aeSachartre 56682f5224aeSachartre /* update request data */ 56692f5224aeSachartre if (uscsi.uscsi_status == STATUS_GOOD) { 56702f5224aeSachartre if (uscsi.uscsi_flags & USCSI_READ) { 56712f5224aeSachartre uscsi.uscsi_resid = uscsi.uscsi_buflen - 56722f5224aeSachartre vd_scsi->datain_len; 56732f5224aeSachartre if (ddi_copyout(datain, uscsi.uscsi_bufaddr, 56742f5224aeSachartre vd_scsi->datain_len, mode) != 0) { 56752f5224aeSachartre rv = EFAULT; 56762f5224aeSachartre goto done; 56772f5224aeSachartre } 56782f5224aeSachartre } else { 56792f5224aeSachartre uscsi.uscsi_resid = uscsi.uscsi_buflen - 56802f5224aeSachartre vd_scsi->dataout_len; 56812f5224aeSachartre } 56822f5224aeSachartre } 56832f5224aeSachartre 56842f5224aeSachartre /* copy-out result */ 56852f5224aeSachartre if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 56862f5224aeSachartre uscsi_cmdtouscsi_cmd32((&uscsi), (&uscsi32)); 56872f5224aeSachartre if (ddi_copyout(&uscsi32, arg, sizeof (struct uscsi_cmd32), 56882f5224aeSachartre mode) != 0) { 56892f5224aeSachartre rv = EFAULT; 56902f5224aeSachartre goto done; 56912f5224aeSachartre } 56922f5224aeSachartre } else { 56932f5224aeSachartre if (ddi_copyout(&uscsi, arg, sizeof (struct uscsi_cmd), 56942f5224aeSachartre mode) != 0) { 56952f5224aeSachartre rv = EFAULT; 56962f5224aeSachartre goto done; 56972f5224aeSachartre } 56982f5224aeSachartre } 56992f5224aeSachartre 57002f5224aeSachartre /* get the return code from the SCSI command status */ 57012f5224aeSachartre rv = vdc_scsi_status(vdc, vd_scsi, 57022f5224aeSachartre !(uscsi.uscsi_flags & USCSI_SILENT)); 57032f5224aeSachartre 57042f5224aeSachartre done: 57052f5224aeSachartre kmem_free(vd_scsi, vd_scsi_len); 57062f5224aeSachartre return (rv); 57072f5224aeSachartre } 57082f5224aeSachartre 57092f5224aeSachartre /* 57102f5224aeSachartre * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT IN command. 57112f5224aeSachartre * 57122f5224aeSachartre * Arguments: 57132f5224aeSachartre * cmd - SCSI PERSISTENT IN command 57142f5224aeSachartre * len - length of the SCSI input buffer 57152f5224aeSachartre * vd_scsi_len - return the length of the allocated buffer 57162f5224aeSachartre * 57172f5224aeSachartre * Returned Value: 57182f5224aeSachartre * a pointer to the allocated VD_OP_SCSICMD buffer. 57192f5224aeSachartre */ 57202f5224aeSachartre static vd_scsi_t * 57212f5224aeSachartre vdc_scsi_alloc_persistent_in(uchar_t cmd, int len, int *vd_scsi_len) 57222f5224aeSachartre { 57232f5224aeSachartre int cdb_len, sense_len, datain_len, dataout_len; 57242f5224aeSachartre vd_scsi_t *vd_scsi; 57252f5224aeSachartre union scsi_cdb *cdb; 57262f5224aeSachartre 57272f5224aeSachartre cdb_len = CDB_GROUP1; 57282f5224aeSachartre sense_len = sizeof (struct scsi_extended_sense); 57292f5224aeSachartre datain_len = len; 57302f5224aeSachartre dataout_len = 0; 57312f5224aeSachartre 57322f5224aeSachartre vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len, 57332f5224aeSachartre vd_scsi_len); 57342f5224aeSachartre 57352f5224aeSachartre cdb = VD_SCSI_DATA_CDB(vd_scsi); 57362f5224aeSachartre 57372f5224aeSachartre /* set cdb */ 57382f5224aeSachartre cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 57392f5224aeSachartre cdb->cdb_opaque[1] = cmd; 57402f5224aeSachartre FORMG1COUNT(cdb, datain_len); 57412f5224aeSachartre 57422f5224aeSachartre vd_scsi->timeout = vdc_scsi_timeout; 57432f5224aeSachartre 57442f5224aeSachartre return (vd_scsi); 57452f5224aeSachartre } 57462f5224aeSachartre 57472f5224aeSachartre /* 57482f5224aeSachartre * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT OUT command. 57492f5224aeSachartre * 57502f5224aeSachartre * Arguments: 57512f5224aeSachartre * cmd - SCSI PERSISTENT OUT command 57522f5224aeSachartre * len - length of the SCSI output buffer 57532f5224aeSachartre * vd_scsi_len - return the length of the allocated buffer 57542f5224aeSachartre * 57552f5224aeSachartre * Returned Code: 57562f5224aeSachartre * a pointer to the allocated VD_OP_SCSICMD buffer. 57572f5224aeSachartre */ 57582f5224aeSachartre static vd_scsi_t * 57592f5224aeSachartre vdc_scsi_alloc_persistent_out(uchar_t cmd, int len, int *vd_scsi_len) 57602f5224aeSachartre { 57612f5224aeSachartre int cdb_len, sense_len, datain_len, dataout_len; 57622f5224aeSachartre vd_scsi_t *vd_scsi; 57632f5224aeSachartre union scsi_cdb *cdb; 57642f5224aeSachartre 57652f5224aeSachartre cdb_len = CDB_GROUP1; 57662f5224aeSachartre sense_len = sizeof (struct scsi_extended_sense); 57672f5224aeSachartre datain_len = 0; 57682f5224aeSachartre dataout_len = len; 57692f5224aeSachartre 57702f5224aeSachartre vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len, 57712f5224aeSachartre vd_scsi_len); 57722f5224aeSachartre 57732f5224aeSachartre cdb = VD_SCSI_DATA_CDB(vd_scsi); 57742f5224aeSachartre 57752f5224aeSachartre /* set cdb */ 57762f5224aeSachartre cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 57772f5224aeSachartre cdb->cdb_opaque[1] = cmd; 57782f5224aeSachartre FORMG1COUNT(cdb, dataout_len); 57792f5224aeSachartre 57802f5224aeSachartre vd_scsi->timeout = vdc_scsi_timeout; 57812f5224aeSachartre 57822f5224aeSachartre return (vd_scsi); 57832f5224aeSachartre } 57842f5224aeSachartre 57852f5224aeSachartre /* 57862f5224aeSachartre * Implement the MHIOCGRP_INKEYS mhd(7i) ioctl. The ioctl is converted 57872f5224aeSachartre * to a SCSI PERSISTENT IN READ KEYS command which is sent to the vdisk 57882f5224aeSachartre * server with a VD_OP_SCSICMD operation. 57892f5224aeSachartre */ 57902f5224aeSachartre static int 57912f5224aeSachartre vdc_mhd_inkeys(vdc_t *vdc, caddr_t arg, int mode) 57922f5224aeSachartre { 57932f5224aeSachartre vd_scsi_t *vd_scsi; 57942f5224aeSachartre mhioc_inkeys_t inkeys; 57952f5224aeSachartre mhioc_key_list_t klist; 57962f5224aeSachartre struct mhioc_inkeys32 inkeys32; 57972f5224aeSachartre struct mhioc_key_list32 klist32; 57982f5224aeSachartre sd_prin_readkeys_t *scsi_keys; 57992f5224aeSachartre void *user_keys; 58002f5224aeSachartre int vd_scsi_len; 58012f5224aeSachartre int listsize, listlen, rv; 58022f5224aeSachartre 58032f5224aeSachartre /* copyin arguments */ 58042f5224aeSachartre if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 58052f5224aeSachartre rv = ddi_copyin(arg, &inkeys32, sizeof (inkeys32), mode); 58062f5224aeSachartre if (rv != 0) 58072f5224aeSachartre return (EFAULT); 58082f5224aeSachartre 58092f5224aeSachartre rv = ddi_copyin((caddr_t)(uintptr_t)inkeys32.li, &klist32, 58102f5224aeSachartre sizeof (klist32), mode); 58112f5224aeSachartre if (rv != 0) 58122f5224aeSachartre return (EFAULT); 58132f5224aeSachartre 58142f5224aeSachartre listsize = klist32.listsize; 58152f5224aeSachartre } else { 58162f5224aeSachartre rv = ddi_copyin(arg, &inkeys, sizeof (inkeys), mode); 58172f5224aeSachartre if (rv != 0) 58182f5224aeSachartre return (EFAULT); 58192f5224aeSachartre 58202f5224aeSachartre rv = ddi_copyin(inkeys.li, &klist, sizeof (klist), mode); 58212f5224aeSachartre if (rv != 0) 58222f5224aeSachartre return (EFAULT); 58232f5224aeSachartre 58242f5224aeSachartre listsize = klist.listsize; 58252f5224aeSachartre } 58262f5224aeSachartre 58272f5224aeSachartre /* build SCSI VD_OP request */ 58282f5224aeSachartre vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_KEYS, 58292f5224aeSachartre sizeof (sd_prin_readkeys_t) - sizeof (caddr_t) + 58302f5224aeSachartre (sizeof (mhioc_resv_key_t) * listsize), &vd_scsi_len); 58312f5224aeSachartre 58322f5224aeSachartre scsi_keys = (sd_prin_readkeys_t *)VD_SCSI_DATA_IN(vd_scsi); 58332f5224aeSachartre 58342f5224aeSachartre /* submit the request */ 58352f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len, 58362f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE); 58372f5224aeSachartre 58382f5224aeSachartre if (rv != 0) 58392f5224aeSachartre goto done; 58402f5224aeSachartre 58412f5224aeSachartre listlen = scsi_keys->len / MHIOC_RESV_KEY_SIZE; 58422f5224aeSachartre 58432f5224aeSachartre if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 58442f5224aeSachartre inkeys32.generation = scsi_keys->generation; 58452f5224aeSachartre rv = ddi_copyout(&inkeys32, arg, sizeof (inkeys32), mode); 58462f5224aeSachartre if (rv != 0) { 58472f5224aeSachartre rv = EFAULT; 58482f5224aeSachartre goto done; 58492f5224aeSachartre } 58502f5224aeSachartre 58512f5224aeSachartre klist32.listlen = listlen; 58522f5224aeSachartre rv = ddi_copyout(&klist32, (caddr_t)(uintptr_t)inkeys32.li, 58532f5224aeSachartre sizeof (klist32), mode); 58542f5224aeSachartre if (rv != 0) { 58552f5224aeSachartre rv = EFAULT; 58562f5224aeSachartre goto done; 58572f5224aeSachartre } 58582f5224aeSachartre 58592f5224aeSachartre user_keys = (caddr_t)(uintptr_t)klist32.list; 58602f5224aeSachartre } else { 58612f5224aeSachartre inkeys.generation = scsi_keys->generation; 58622f5224aeSachartre rv = ddi_copyout(&inkeys, arg, sizeof (inkeys), mode); 58632f5224aeSachartre if (rv != 0) { 58642f5224aeSachartre rv = EFAULT; 58652f5224aeSachartre goto done; 58662f5224aeSachartre } 58672f5224aeSachartre 58682f5224aeSachartre klist.listlen = listlen; 58692f5224aeSachartre rv = ddi_copyout(&klist, inkeys.li, sizeof (klist), mode); 58702f5224aeSachartre if (rv != 0) { 58712f5224aeSachartre rv = EFAULT; 58722f5224aeSachartre goto done; 58732f5224aeSachartre } 58742f5224aeSachartre 58752f5224aeSachartre user_keys = klist.list; 58762f5224aeSachartre } 58772f5224aeSachartre 58782f5224aeSachartre /* copy out keys */ 58792f5224aeSachartre if (listlen > 0 && listsize > 0) { 58802f5224aeSachartre if (listsize < listlen) 58812f5224aeSachartre listlen = listsize; 58822f5224aeSachartre rv = ddi_copyout(&scsi_keys->keylist, user_keys, 58832f5224aeSachartre listlen * MHIOC_RESV_KEY_SIZE, mode); 58842f5224aeSachartre if (rv != 0) 58852f5224aeSachartre rv = EFAULT; 58862f5224aeSachartre } 58872f5224aeSachartre 58882f5224aeSachartre if (rv == 0) 58892f5224aeSachartre rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE); 58902f5224aeSachartre 58912f5224aeSachartre done: 58922f5224aeSachartre kmem_free(vd_scsi, vd_scsi_len); 58932f5224aeSachartre 58942f5224aeSachartre return (rv); 58952f5224aeSachartre } 58962f5224aeSachartre 58972f5224aeSachartre /* 58982f5224aeSachartre * Implement the MHIOCGRP_INRESV mhd(7i) ioctl. The ioctl is converted 58992f5224aeSachartre * to a SCSI PERSISTENT IN READ RESERVATION command which is sent to 59002f5224aeSachartre * the vdisk server with a VD_OP_SCSICMD operation. 59012f5224aeSachartre */ 59022f5224aeSachartre static int 59032f5224aeSachartre vdc_mhd_inresv(vdc_t *vdc, caddr_t arg, int mode) 59042f5224aeSachartre { 59052f5224aeSachartre vd_scsi_t *vd_scsi; 59062f5224aeSachartre mhioc_inresvs_t inresv; 59072f5224aeSachartre mhioc_resv_desc_list_t rlist; 59082f5224aeSachartre struct mhioc_inresvs32 inresv32; 59092f5224aeSachartre struct mhioc_resv_desc_list32 rlist32; 59102f5224aeSachartre mhioc_resv_desc_t mhd_resv; 59112f5224aeSachartre sd_prin_readresv_t *scsi_resv; 59122f5224aeSachartre sd_readresv_desc_t *resv; 59132f5224aeSachartre mhioc_resv_desc_t *user_resv; 59142f5224aeSachartre int vd_scsi_len; 59152f5224aeSachartre int listsize, listlen, i, rv; 59162f5224aeSachartre 59172f5224aeSachartre /* copyin arguments */ 59182f5224aeSachartre if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 59192f5224aeSachartre rv = ddi_copyin(arg, &inresv32, sizeof (inresv32), mode); 59202f5224aeSachartre if (rv != 0) 59212f5224aeSachartre return (EFAULT); 59222f5224aeSachartre 59232f5224aeSachartre rv = ddi_copyin((caddr_t)(uintptr_t)inresv32.li, &rlist32, 59242f5224aeSachartre sizeof (rlist32), mode); 59252f5224aeSachartre if (rv != 0) 59262f5224aeSachartre return (EFAULT); 59272f5224aeSachartre 59282f5224aeSachartre listsize = rlist32.listsize; 59292f5224aeSachartre } else { 59302f5224aeSachartre rv = ddi_copyin(arg, &inresv, sizeof (inresv), mode); 59312f5224aeSachartre if (rv != 0) 59322f5224aeSachartre return (EFAULT); 59332f5224aeSachartre 59342f5224aeSachartre rv = ddi_copyin(inresv.li, &rlist, sizeof (rlist), mode); 59352f5224aeSachartre if (rv != 0) 59362f5224aeSachartre return (EFAULT); 59372f5224aeSachartre 59382f5224aeSachartre listsize = rlist.listsize; 59392f5224aeSachartre } 59402f5224aeSachartre 59412f5224aeSachartre /* build SCSI VD_OP request */ 59422f5224aeSachartre vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_RESV, 59432f5224aeSachartre sizeof (sd_prin_readresv_t) - sizeof (caddr_t) + 59442f5224aeSachartre (SCSI3_RESV_DESC_LEN * listsize), &vd_scsi_len); 59452f5224aeSachartre 59462f5224aeSachartre scsi_resv = (sd_prin_readresv_t *)VD_SCSI_DATA_IN(vd_scsi); 59472f5224aeSachartre 59482f5224aeSachartre /* submit the request */ 59492f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len, 59502f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE); 59512f5224aeSachartre 59522f5224aeSachartre if (rv != 0) 59532f5224aeSachartre goto done; 59542f5224aeSachartre 59552f5224aeSachartre listlen = scsi_resv->len / SCSI3_RESV_DESC_LEN; 59562f5224aeSachartre 59572f5224aeSachartre if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 59582f5224aeSachartre inresv32.generation = scsi_resv->generation; 59592f5224aeSachartre rv = ddi_copyout(&inresv32, arg, sizeof (inresv32), mode); 59602f5224aeSachartre if (rv != 0) { 59612f5224aeSachartre rv = EFAULT; 59622f5224aeSachartre goto done; 59632f5224aeSachartre } 59642f5224aeSachartre 59652f5224aeSachartre rlist32.listlen = listlen; 59662f5224aeSachartre rv = ddi_copyout(&rlist32, (caddr_t)(uintptr_t)inresv32.li, 59672f5224aeSachartre sizeof (rlist32), mode); 59682f5224aeSachartre if (rv != 0) { 59692f5224aeSachartre rv = EFAULT; 59702f5224aeSachartre goto done; 59712f5224aeSachartre } 59722f5224aeSachartre 59732f5224aeSachartre user_resv = (mhioc_resv_desc_t *)(uintptr_t)rlist32.list; 59742f5224aeSachartre } else { 59752f5224aeSachartre inresv.generation = scsi_resv->generation; 59762f5224aeSachartre rv = ddi_copyout(&inresv, arg, sizeof (inresv), mode); 59772f5224aeSachartre if (rv != 0) { 59782f5224aeSachartre rv = EFAULT; 59792f5224aeSachartre goto done; 59802f5224aeSachartre } 59812f5224aeSachartre 59822f5224aeSachartre rlist.listlen = listlen; 59832f5224aeSachartre rv = ddi_copyout(&rlist, inresv.li, sizeof (rlist), mode); 59842f5224aeSachartre if (rv != 0) { 59852f5224aeSachartre rv = EFAULT; 59862f5224aeSachartre goto done; 59872f5224aeSachartre } 59882f5224aeSachartre 59892f5224aeSachartre user_resv = rlist.list; 59902f5224aeSachartre } 59912f5224aeSachartre 59922f5224aeSachartre /* copy out reservations */ 59932f5224aeSachartre if (listsize > 0 && listlen > 0) { 59942f5224aeSachartre if (listsize < listlen) 59952f5224aeSachartre listlen = listsize; 59962f5224aeSachartre resv = (sd_readresv_desc_t *)&scsi_resv->readresv_desc; 59972f5224aeSachartre 59982f5224aeSachartre for (i = 0; i < listlen; i++) { 59992f5224aeSachartre mhd_resv.type = resv->type; 60002f5224aeSachartre mhd_resv.scope = resv->scope; 60012f5224aeSachartre mhd_resv.scope_specific_addr = 60022f5224aeSachartre BE_32(resv->scope_specific_addr); 60032f5224aeSachartre bcopy(&resv->resvkey, &mhd_resv.key, 60042f5224aeSachartre MHIOC_RESV_KEY_SIZE); 60052f5224aeSachartre 60062f5224aeSachartre rv = ddi_copyout(&mhd_resv, user_resv, 60072f5224aeSachartre sizeof (mhd_resv), mode); 60082f5224aeSachartre if (rv != 0) { 60092f5224aeSachartre rv = EFAULT; 60102f5224aeSachartre goto done; 60112f5224aeSachartre } 60122f5224aeSachartre resv++; 60132f5224aeSachartre user_resv++; 60142f5224aeSachartre } 60152f5224aeSachartre } 60162f5224aeSachartre 60172f5224aeSachartre if (rv == 0) 60182f5224aeSachartre rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE); 60192f5224aeSachartre 60202f5224aeSachartre done: 60212f5224aeSachartre kmem_free(vd_scsi, vd_scsi_len); 60222f5224aeSachartre return (rv); 60232f5224aeSachartre } 60242f5224aeSachartre 60252f5224aeSachartre /* 60262f5224aeSachartre * Implement the MHIOCGRP_REGISTER mhd(7i) ioctl. The ioctl is converted 60272f5224aeSachartre * to a SCSI PERSISTENT OUT REGISTER command which is sent to the vdisk 60282f5224aeSachartre * server with a VD_OP_SCSICMD operation. 60292f5224aeSachartre */ 60302f5224aeSachartre static int 60312f5224aeSachartre vdc_mhd_register(vdc_t *vdc, caddr_t arg, int mode) 60322f5224aeSachartre { 60332f5224aeSachartre vd_scsi_t *vd_scsi; 60342f5224aeSachartre sd_prout_t *scsi_prout; 60352f5224aeSachartre mhioc_register_t mhd_reg; 60362f5224aeSachartre int vd_scsi_len, rv; 60372f5224aeSachartre 60382f5224aeSachartre /* copyin arguments */ 60392f5224aeSachartre rv = ddi_copyin(arg, &mhd_reg, sizeof (mhd_reg), mode); 60402f5224aeSachartre if (rv != 0) 60412f5224aeSachartre return (EFAULT); 60422f5224aeSachartre 60432f5224aeSachartre /* build SCSI VD_OP request */ 60442f5224aeSachartre vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTER, 60452f5224aeSachartre sizeof (sd_prout_t), &vd_scsi_len); 60462f5224aeSachartre 60472f5224aeSachartre /* set parameters */ 60482f5224aeSachartre scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi); 60492f5224aeSachartre bcopy(mhd_reg.oldkey.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE); 60502f5224aeSachartre bcopy(mhd_reg.newkey.key, scsi_prout->service_key, MHIOC_RESV_KEY_SIZE); 60512f5224aeSachartre scsi_prout->aptpl = (uchar_t)mhd_reg.aptpl; 60522f5224aeSachartre 60532f5224aeSachartre /* submit the request */ 60542f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len, 60552f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE); 60562f5224aeSachartre 60572f5224aeSachartre if (rv == 0) 60582f5224aeSachartre rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE); 60592f5224aeSachartre 60602f5224aeSachartre kmem_free(vd_scsi, vd_scsi_len); 60612f5224aeSachartre return (rv); 60622f5224aeSachartre } 60632f5224aeSachartre 60642f5224aeSachartre /* 60652f5224aeSachartre * Implement the MHIOCGRP_RESERVE mhd(7i) ioctl. The ioctl is converted 60662f5224aeSachartre * to a SCSI PERSISTENT OUT RESERVE command which is sent to the vdisk 60672f5224aeSachartre * server with a VD_OP_SCSICMD operation. 60682f5224aeSachartre */ 60692f5224aeSachartre static int 60702f5224aeSachartre vdc_mhd_reserve(vdc_t *vdc, caddr_t arg, int mode) 60712f5224aeSachartre { 60722f5224aeSachartre union scsi_cdb *cdb; 60732f5224aeSachartre vd_scsi_t *vd_scsi; 60742f5224aeSachartre sd_prout_t *scsi_prout; 60752f5224aeSachartre mhioc_resv_desc_t mhd_resv; 60762f5224aeSachartre int vd_scsi_len, rv; 60772f5224aeSachartre 60782f5224aeSachartre /* copyin arguments */ 60792f5224aeSachartre rv = ddi_copyin(arg, &mhd_resv, sizeof (mhd_resv), mode); 60802f5224aeSachartre if (rv != 0) 60812f5224aeSachartre return (EFAULT); 60822f5224aeSachartre 60832f5224aeSachartre /* build SCSI VD_OP request */ 60842f5224aeSachartre vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_RESERVE, 60852f5224aeSachartre sizeof (sd_prout_t), &vd_scsi_len); 60862f5224aeSachartre 60872f5224aeSachartre /* set parameters */ 60882f5224aeSachartre cdb = VD_SCSI_DATA_CDB(vd_scsi); 60892f5224aeSachartre scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi); 60902f5224aeSachartre bcopy(mhd_resv.key.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE); 60912f5224aeSachartre scsi_prout->scope_address = mhd_resv.scope_specific_addr; 60922f5224aeSachartre cdb->cdb_opaque[2] = mhd_resv.type; 60932f5224aeSachartre 60942f5224aeSachartre /* submit the request */ 60952f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len, 60962f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE); 60972f5224aeSachartre 60982f5224aeSachartre if (rv == 0) 60992f5224aeSachartre rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE); 61002f5224aeSachartre 61012f5224aeSachartre kmem_free(vd_scsi, vd_scsi_len); 61022f5224aeSachartre return (rv); 61032f5224aeSachartre } 61042f5224aeSachartre 61052f5224aeSachartre /* 61062f5224aeSachartre * Implement the MHIOCGRP_PREEMPTANDABORT mhd(7i) ioctl. The ioctl is 61072f5224aeSachartre * converted to a SCSI PERSISTENT OUT PREEMPT AND ABORT command which 61082f5224aeSachartre * is sent to the vdisk server with a VD_OP_SCSICMD operation. 61092f5224aeSachartre */ 61102f5224aeSachartre static int 61112f5224aeSachartre vdc_mhd_preemptabort(vdc_t *vdc, caddr_t arg, int mode) 61122f5224aeSachartre { 61132f5224aeSachartre union scsi_cdb *cdb; 61142f5224aeSachartre vd_scsi_t *vd_scsi; 61152f5224aeSachartre sd_prout_t *scsi_prout; 61162f5224aeSachartre mhioc_preemptandabort_t mhd_preempt; 61172f5224aeSachartre int vd_scsi_len, rv; 61182f5224aeSachartre 61192f5224aeSachartre /* copyin arguments */ 61202f5224aeSachartre rv = ddi_copyin(arg, &mhd_preempt, sizeof (mhd_preempt), mode); 61212f5224aeSachartre if (rv != 0) 61222f5224aeSachartre return (EFAULT); 61232f5224aeSachartre 61242f5224aeSachartre /* build SCSI VD_OP request */ 61252f5224aeSachartre vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_PREEMPTANDABORT, 61262f5224aeSachartre sizeof (sd_prout_t), &vd_scsi_len); 61272f5224aeSachartre 61282f5224aeSachartre /* set parameters */ 61292f5224aeSachartre vd_scsi->task_attribute = VD_SCSI_TASK_ACA; 61302f5224aeSachartre cdb = VD_SCSI_DATA_CDB(vd_scsi); 61312f5224aeSachartre scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi); 61322f5224aeSachartre bcopy(mhd_preempt.resvdesc.key.key, scsi_prout->res_key, 61332f5224aeSachartre MHIOC_RESV_KEY_SIZE); 61342f5224aeSachartre bcopy(mhd_preempt.victim_key.key, scsi_prout->service_key, 61352f5224aeSachartre MHIOC_RESV_KEY_SIZE); 61362f5224aeSachartre scsi_prout->scope_address = mhd_preempt.resvdesc.scope_specific_addr; 61372f5224aeSachartre cdb->cdb_opaque[2] = mhd_preempt.resvdesc.type; 61382f5224aeSachartre 61392f5224aeSachartre /* submit the request */ 61402f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len, 61412f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE); 61422f5224aeSachartre 61432f5224aeSachartre if (rv == 0) 61442f5224aeSachartre rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE); 61452f5224aeSachartre 61462f5224aeSachartre kmem_free(vd_scsi, vd_scsi_len); 61472f5224aeSachartre return (rv); 61482f5224aeSachartre } 61492f5224aeSachartre 61502f5224aeSachartre /* 61512f5224aeSachartre * Implement the MHIOCGRP_REGISTERANDIGNOREKEY mhd(7i) ioctl. The ioctl 61522f5224aeSachartre * is converted to a SCSI PERSISTENT OUT REGISTER AND IGNORE EXISTING KEY 61532f5224aeSachartre * command which is sent to the vdisk server with a VD_OP_SCSICMD operation. 61542f5224aeSachartre */ 61552f5224aeSachartre static int 61562f5224aeSachartre vdc_mhd_registerignore(vdc_t *vdc, caddr_t arg, int mode) 61572f5224aeSachartre { 61582f5224aeSachartre vd_scsi_t *vd_scsi; 61592f5224aeSachartre sd_prout_t *scsi_prout; 61602f5224aeSachartre mhioc_registerandignorekey_t mhd_regi; 61612f5224aeSachartre int vd_scsi_len, rv; 61622f5224aeSachartre 61632f5224aeSachartre /* copyin arguments */ 61642f5224aeSachartre rv = ddi_copyin(arg, &mhd_regi, sizeof (mhd_regi), mode); 61652f5224aeSachartre if (rv != 0) 61662f5224aeSachartre return (EFAULT); 61672f5224aeSachartre 61682f5224aeSachartre /* build SCSI VD_OP request */ 61692f5224aeSachartre vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTERANDIGNOREKEY, 61702f5224aeSachartre sizeof (sd_prout_t), &vd_scsi_len); 61712f5224aeSachartre 61722f5224aeSachartre /* set parameters */ 61732f5224aeSachartre scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi); 61742f5224aeSachartre bcopy(mhd_regi.newkey.key, scsi_prout->service_key, 61752f5224aeSachartre MHIOC_RESV_KEY_SIZE); 61762f5224aeSachartre scsi_prout->aptpl = (uchar_t)mhd_regi.aptpl; 61772f5224aeSachartre 61782f5224aeSachartre /* submit the request */ 61792f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len, 61802f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE); 61812f5224aeSachartre 61822f5224aeSachartre if (rv == 0) 61832f5224aeSachartre rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE); 61842f5224aeSachartre 61852f5224aeSachartre kmem_free(vd_scsi, vd_scsi_len); 61862f5224aeSachartre return (rv); 61872f5224aeSachartre } 61882f5224aeSachartre 61892f5224aeSachartre /* 61902f5224aeSachartre * This function is used by the failfast mechanism to send a SCSI command 61912f5224aeSachartre * to check for reservation conflict. 61922f5224aeSachartre */ 61932f5224aeSachartre static int 61942f5224aeSachartre vdc_failfast_scsi_cmd(vdc_t *vdc, uchar_t scmd) 61952f5224aeSachartre { 61962f5224aeSachartre int cdb_len, sense_len, vd_scsi_len; 61972f5224aeSachartre vd_scsi_t *vd_scsi; 61982f5224aeSachartre union scsi_cdb *cdb; 61992f5224aeSachartre int rv; 62002f5224aeSachartre 62012f5224aeSachartre ASSERT(scmd == SCMD_TEST_UNIT_READY || scmd == SCMD_WRITE_G1); 62022f5224aeSachartre 62032f5224aeSachartre if (scmd == SCMD_WRITE_G1) 62042f5224aeSachartre cdb_len = CDB_GROUP1; 62052f5224aeSachartre else 62062f5224aeSachartre cdb_len = CDB_GROUP0; 62072f5224aeSachartre 62082f5224aeSachartre sense_len = sizeof (struct scsi_extended_sense); 62092f5224aeSachartre 62102f5224aeSachartre vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, 0, 0, &vd_scsi_len); 62112f5224aeSachartre 62122f5224aeSachartre /* set cdb */ 62132f5224aeSachartre cdb = VD_SCSI_DATA_CDB(vd_scsi); 62142f5224aeSachartre cdb->scc_cmd = scmd; 62152f5224aeSachartre 62162f5224aeSachartre vd_scsi->timeout = vdc_scsi_timeout; 62172f5224aeSachartre 62182f5224aeSachartre /* 62192f5224aeSachartre * Submit the request. The last argument has to be B_FALSE so that 62202f5224aeSachartre * vdc_do_sync_op does not loop checking for reservation conflict if 62212f5224aeSachartre * the operation returns an error. 62222f5224aeSachartre */ 62232f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len, 62242f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_FALSE); 62252f5224aeSachartre 62262f5224aeSachartre if (rv == 0) 62272f5224aeSachartre (void) vdc_scsi_status(vdc, vd_scsi, B_FALSE); 62282f5224aeSachartre 62292f5224aeSachartre kmem_free(vd_scsi, vd_scsi_len); 62302f5224aeSachartre return (rv); 62312f5224aeSachartre } 62322f5224aeSachartre 62332f5224aeSachartre /* 62342f5224aeSachartre * This function is used by the failfast mechanism to check for reservation 62352f5224aeSachartre * conflict. It sends some SCSI commands which will fail with a reservation 62362f5224aeSachartre * conflict error if the system does not have access to the disk and this 62372f5224aeSachartre * will panic the system. 62382f5224aeSachartre * 62392f5224aeSachartre * Returned Code: 62402f5224aeSachartre * 0 - disk is accessible without reservation conflict error 62412f5224aeSachartre * != 0 - unable to check if disk is accessible 62422f5224aeSachartre */ 62432f5224aeSachartre int 62442f5224aeSachartre vdc_failfast_check_resv(vdc_t *vdc) 62452f5224aeSachartre { 62462f5224aeSachartre int failure = 0; 62472f5224aeSachartre 62482f5224aeSachartre /* 62492f5224aeSachartre * Send a TEST UNIT READY command. The command will panic 62502f5224aeSachartre * the system if it fails with a reservation conflict. 62512f5224aeSachartre */ 62522f5224aeSachartre if (vdc_failfast_scsi_cmd(vdc, SCMD_TEST_UNIT_READY) != 0) 62532f5224aeSachartre failure++; 62542f5224aeSachartre 62552f5224aeSachartre /* 62562f5224aeSachartre * With SPC-3 compliant devices TEST UNIT READY will succeed on 62572f5224aeSachartre * a reserved device, so we also do a WRITE(10) of zero byte in 62582f5224aeSachartre * order to provoke a Reservation Conflict status on those newer 62592f5224aeSachartre * devices. 62602f5224aeSachartre */ 62612f5224aeSachartre if (vdc_failfast_scsi_cmd(vdc, SCMD_WRITE_G1) != 0) 62622f5224aeSachartre failure++; 62632f5224aeSachartre 62642f5224aeSachartre return (failure); 62652f5224aeSachartre } 62662f5224aeSachartre 62672f5224aeSachartre /* 62682f5224aeSachartre * Add a pending I/O to the failfast I/O queue. An I/O is added to this 62692f5224aeSachartre * queue when it has failed and failfast is enabled. Then we have to check 62702f5224aeSachartre * if it has failed because of a reservation conflict in which case we have 62712f5224aeSachartre * to panic the system. 62722f5224aeSachartre * 62732f5224aeSachartre * Async I/O should be queued with their block I/O data transfer structure 62742f5224aeSachartre * (buf). Sync I/O should be queued with buf = NULL. 62752f5224aeSachartre */ 62762f5224aeSachartre static vdc_io_t * 62772f5224aeSachartre vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf) 62782f5224aeSachartre { 62792f5224aeSachartre vdc_io_t *vio; 62802f5224aeSachartre 62812f5224aeSachartre ASSERT(MUTEX_HELD(&vdc->lock)); 62822f5224aeSachartre 62832f5224aeSachartre vio = kmem_alloc(sizeof (vdc_io_t), KM_SLEEP); 62842f5224aeSachartre vio->vio_next = vdc->failfast_io_queue; 62852f5224aeSachartre vio->vio_buf = buf; 62862f5224aeSachartre vio->vio_qtime = ddi_get_lbolt(); 62872f5224aeSachartre 62882f5224aeSachartre vdc->failfast_io_queue = vio; 62892f5224aeSachartre 62902f5224aeSachartre /* notify the failfast thread that a new I/O is queued */ 62912f5224aeSachartre cv_signal(&vdc->failfast_cv); 62922f5224aeSachartre 62932f5224aeSachartre return (vio); 62942f5224aeSachartre } 62952f5224aeSachartre 62962f5224aeSachartre /* 62972f5224aeSachartre * Remove and complete I/O in the failfast I/O queue which have been 62982f5224aeSachartre * added after the indicated deadline. A deadline of 0 means that all 62992f5224aeSachartre * I/O have to be unqueued and marked as completed. 63002f5224aeSachartre */ 63012f5224aeSachartre static void 63022f5224aeSachartre vdc_failfast_io_unqueue(vdc_t *vdc, clock_t deadline) 63032f5224aeSachartre { 63042f5224aeSachartre vdc_io_t *vio, *vio_tmp; 63052f5224aeSachartre 63062f5224aeSachartre ASSERT(MUTEX_HELD(&vdc->lock)); 63072f5224aeSachartre 63082f5224aeSachartre vio_tmp = NULL; 63092f5224aeSachartre vio = vdc->failfast_io_queue; 63102f5224aeSachartre 63112f5224aeSachartre if (deadline != 0) { 63122f5224aeSachartre /* 63132f5224aeSachartre * Skip any io queued after the deadline. The failfast 63142f5224aeSachartre * I/O queue is ordered starting with the last I/O added 63152f5224aeSachartre * to the queue. 63162f5224aeSachartre */ 63172f5224aeSachartre while (vio != NULL && vio->vio_qtime > deadline) { 63182f5224aeSachartre vio_tmp = vio; 63192f5224aeSachartre vio = vio->vio_next; 63202f5224aeSachartre } 63212f5224aeSachartre } 63222f5224aeSachartre 63232f5224aeSachartre if (vio == NULL) 63242f5224aeSachartre /* nothing to unqueue */ 63252f5224aeSachartre return; 63262f5224aeSachartre 63272f5224aeSachartre /* update the queue */ 63282f5224aeSachartre if (vio_tmp == NULL) 63292f5224aeSachartre vdc->failfast_io_queue = NULL; 63302f5224aeSachartre else 63312f5224aeSachartre vio_tmp->vio_next = NULL; 63322f5224aeSachartre 63332f5224aeSachartre /* 63342f5224aeSachartre * Complete unqueued I/O. Async I/O have a block I/O data transfer 63352f5224aeSachartre * structure (buf) and they are completed by calling biodone(). Sync 63362f5224aeSachartre * I/O do not have a buf and they are completed by setting the 63372f5224aeSachartre * vio_qtime to zero and signaling failfast_io_cv. In that case, the 63382f5224aeSachartre * thread waiting for the I/O to complete is responsible for freeing 63392f5224aeSachartre * the vio structure. 63402f5224aeSachartre */ 63412f5224aeSachartre while (vio != NULL) { 63422f5224aeSachartre vio_tmp = vio->vio_next; 63432f5224aeSachartre if (vio->vio_buf != NULL) { 634490e2f9dcSlm66018 VD_KSTAT_RUNQ_EXIT(vdc); 6345366a92acSlm66018 DTRACE_IO1(done, buf_t *, vio->vio_buf); 63462f5224aeSachartre biodone(vio->vio_buf); 63472f5224aeSachartre kmem_free(vio, sizeof (vdc_io_t)); 63482f5224aeSachartre } else { 63492f5224aeSachartre vio->vio_qtime = 0; 63502f5224aeSachartre } 63512f5224aeSachartre vio = vio_tmp; 63522f5224aeSachartre } 63532f5224aeSachartre 63542f5224aeSachartre cv_broadcast(&vdc->failfast_io_cv); 63552f5224aeSachartre } 63562f5224aeSachartre 63572f5224aeSachartre /* 63582f5224aeSachartre * Failfast Thread. 63592f5224aeSachartre * 63602f5224aeSachartre * While failfast is enabled, the failfast thread sends a TEST UNIT READY 63612f5224aeSachartre * and a zero size WRITE(10) SCSI commands on a regular basis to check that 63622f5224aeSachartre * we still have access to the disk. If a command fails with a RESERVATION 63632f5224aeSachartre * CONFLICT error then the system will immediatly panic. 63642f5224aeSachartre * 63652f5224aeSachartre * The failfast thread is also woken up when an I/O has failed. It then check 63662f5224aeSachartre * the access to the disk to ensure that the I/O failure was not due to a 63672f5224aeSachartre * reservation conflict. 63682f5224aeSachartre * 63692f5224aeSachartre * There is one failfast thread for each virtual disk for which failfast is 63702f5224aeSachartre * enabled. We could have only one thread sending requests for all disks but 63712f5224aeSachartre * this would need vdc to send asynchronous requests and to have callbacks to 63722f5224aeSachartre * process replies. 63732f5224aeSachartre */ 63742f5224aeSachartre static void 63752f5224aeSachartre vdc_failfast_thread(void *arg) 63762f5224aeSachartre { 63772f5224aeSachartre int status; 63782f5224aeSachartre vdc_t *vdc = (vdc_t *)arg; 63792f5224aeSachartre clock_t timeout, starttime; 63802f5224aeSachartre 63812f5224aeSachartre mutex_enter(&vdc->lock); 63822f5224aeSachartre 63832f5224aeSachartre while (vdc->failfast_interval != 0) { 63842f5224aeSachartre 63852f5224aeSachartre starttime = ddi_get_lbolt(); 63862f5224aeSachartre 63872f5224aeSachartre mutex_exit(&vdc->lock); 63882f5224aeSachartre 63892f5224aeSachartre /* check for reservation conflict */ 63902f5224aeSachartre status = vdc_failfast_check_resv(vdc); 63912f5224aeSachartre 63922f5224aeSachartre mutex_enter(&vdc->lock); 63932f5224aeSachartre /* 63942f5224aeSachartre * We have dropped the lock to send the SCSI command so we have 63952f5224aeSachartre * to check that failfast is still enabled. 63962f5224aeSachartre */ 63972f5224aeSachartre if (vdc->failfast_interval == 0) 63982f5224aeSachartre break; 63992f5224aeSachartre 64002f5224aeSachartre /* 64012f5224aeSachartre * If we have successfully check the disk access and there was 64022f5224aeSachartre * no reservation conflict then we can complete any I/O queued 64032f5224aeSachartre * before the last check. 64042f5224aeSachartre */ 64052f5224aeSachartre if (status == 0) 64062f5224aeSachartre vdc_failfast_io_unqueue(vdc, starttime); 64072f5224aeSachartre 64082f5224aeSachartre /* proceed again if some I/O are still in the queue */ 64092f5224aeSachartre if (vdc->failfast_io_queue != NULL) 64102f5224aeSachartre continue; 64112f5224aeSachartre 64122f5224aeSachartre timeout = ddi_get_lbolt() + 64132f5224aeSachartre drv_usectohz(vdc->failfast_interval); 64142f5224aeSachartre (void) cv_timedwait(&vdc->failfast_cv, &vdc->lock, timeout); 64152f5224aeSachartre } 64162f5224aeSachartre 64172f5224aeSachartre /* 64182f5224aeSachartre * Failfast is being stop so we can complete any queued I/O. 64192f5224aeSachartre */ 64202f5224aeSachartre vdc_failfast_io_unqueue(vdc, 0); 64212f5224aeSachartre vdc->failfast_thread = NULL; 64222f5224aeSachartre mutex_exit(&vdc->lock); 64232f5224aeSachartre thread_exit(); 64242f5224aeSachartre } 64252f5224aeSachartre 64262f5224aeSachartre /* 64272f5224aeSachartre * Implement the MHIOCENFAILFAST mhd(7i) ioctl. 64282f5224aeSachartre */ 64292f5224aeSachartre static int 64302f5224aeSachartre vdc_failfast(vdc_t *vdc, caddr_t arg, int mode) 64312f5224aeSachartre { 64322f5224aeSachartre unsigned int mh_time; 64332f5224aeSachartre 64342f5224aeSachartre if (ddi_copyin((void *)arg, &mh_time, sizeof (int), mode)) 64352f5224aeSachartre return (EFAULT); 64362f5224aeSachartre 64372f5224aeSachartre mutex_enter(&vdc->lock); 64382f5224aeSachartre if (mh_time != 0 && vdc->failfast_thread == NULL) { 64392f5224aeSachartre vdc->failfast_thread = thread_create(NULL, 0, 64402f5224aeSachartre vdc_failfast_thread, vdc, 0, &p0, TS_RUN, 64412f5224aeSachartre v.v_maxsyspri - 2); 64422f5224aeSachartre } 64432f5224aeSachartre 64442f5224aeSachartre vdc->failfast_interval = mh_time * 1000; 64452f5224aeSachartre cv_signal(&vdc->failfast_cv); 64462f5224aeSachartre mutex_exit(&vdc->lock); 64472f5224aeSachartre 64482f5224aeSachartre return (0); 64492f5224aeSachartre } 64502f5224aeSachartre 64512f5224aeSachartre /* 64522f5224aeSachartre * Implement the MHIOCTKOWN and MHIOCRELEASE mhd(7i) ioctls. These ioctls are 64532f5224aeSachartre * converted to VD_OP_SET_ACCESS operations. 64542f5224aeSachartre */ 64552f5224aeSachartre static int 64562f5224aeSachartre vdc_access_set(vdc_t *vdc, uint64_t flags, int mode) 64572f5224aeSachartre { 64582f5224aeSachartre int rv; 64592f5224aeSachartre 64602f5224aeSachartre /* submit owership command request */ 64612f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_SET_ACCESS, (caddr_t)&flags, 64622f5224aeSachartre sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode, 64632f5224aeSachartre VIO_both_dir, B_TRUE); 64642f5224aeSachartre 64652f5224aeSachartre return (rv); 64662f5224aeSachartre } 64672f5224aeSachartre 64682f5224aeSachartre /* 64692f5224aeSachartre * Implement the MHIOCSTATUS mhd(7i) ioctl. This ioctl is converted to a 64702f5224aeSachartre * VD_OP_GET_ACCESS operation. 64712f5224aeSachartre */ 64722f5224aeSachartre static int 64732f5224aeSachartre vdc_access_get(vdc_t *vdc, uint64_t *status, int mode) 64742f5224aeSachartre { 64752f5224aeSachartre int rv; 64762f5224aeSachartre 64772f5224aeSachartre /* submit owership command request */ 64782f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_GET_ACCESS, (caddr_t)status, 64792f5224aeSachartre sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode, 64802f5224aeSachartre VIO_both_dir, B_TRUE); 64812f5224aeSachartre 64822f5224aeSachartre return (rv); 64832f5224aeSachartre } 64842f5224aeSachartre 64852f5224aeSachartre /* 64862f5224aeSachartre * Disk Ownership Thread. 64872f5224aeSachartre * 64882f5224aeSachartre * When we have taken the ownership of a disk, this thread waits to be 64892f5224aeSachartre * notified when the LDC channel is reset so that it can recover the 64902f5224aeSachartre * ownership. 64912f5224aeSachartre * 64922f5224aeSachartre * Note that the thread handling the LDC reset (vdc_process_msg_thread()) 64932f5224aeSachartre * can not be used to do the ownership recovery because it has to be 64942f5224aeSachartre * running to handle the reply message to the ownership operation. 64952f5224aeSachartre */ 64962f5224aeSachartre static void 64972f5224aeSachartre vdc_ownership_thread(void *arg) 64982f5224aeSachartre { 64992f5224aeSachartre vdc_t *vdc = (vdc_t *)arg; 65002f5224aeSachartre clock_t timeout; 65012f5224aeSachartre uint64_t status; 65022f5224aeSachartre 65032f5224aeSachartre mutex_enter(&vdc->ownership_lock); 65042f5224aeSachartre mutex_enter(&vdc->lock); 65052f5224aeSachartre 65062f5224aeSachartre while (vdc->ownership & VDC_OWNERSHIP_WANTED) { 65072f5224aeSachartre 65082f5224aeSachartre if ((vdc->ownership & VDC_OWNERSHIP_RESET) || 65092f5224aeSachartre !(vdc->ownership & VDC_OWNERSHIP_GRANTED)) { 65102f5224aeSachartre /* 65112f5224aeSachartre * There was a reset so the ownership has been lost, 65122f5224aeSachartre * try to recover. We do this without using the preempt 65132f5224aeSachartre * option so that we don't steal the ownership from 65142f5224aeSachartre * someone who has preempted us. 65152f5224aeSachartre */ 65162f5224aeSachartre DMSG(vdc, 0, "[%d] Ownership lost, recovering", 65172f5224aeSachartre vdc->instance); 65182f5224aeSachartre 65192f5224aeSachartre vdc->ownership &= ~(VDC_OWNERSHIP_RESET | 65202f5224aeSachartre VDC_OWNERSHIP_GRANTED); 65212f5224aeSachartre 65222f5224aeSachartre mutex_exit(&vdc->lock); 65232f5224aeSachartre 65242f5224aeSachartre status = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE | 65252f5224aeSachartre VD_ACCESS_SET_PRESERVE, FKIOCTL); 65262f5224aeSachartre 65272f5224aeSachartre mutex_enter(&vdc->lock); 65282f5224aeSachartre 65292f5224aeSachartre if (status == 0) { 65302f5224aeSachartre DMSG(vdc, 0, "[%d] Ownership recovered", 65312f5224aeSachartre vdc->instance); 65322f5224aeSachartre vdc->ownership |= VDC_OWNERSHIP_GRANTED; 65332f5224aeSachartre } else { 65342f5224aeSachartre DMSG(vdc, 0, "[%d] Fail to recover ownership", 65352f5224aeSachartre vdc->instance); 65362f5224aeSachartre } 65372f5224aeSachartre 65382f5224aeSachartre } 65392f5224aeSachartre 65402f5224aeSachartre /* 65412f5224aeSachartre * If we have the ownership then we just wait for an event 65422f5224aeSachartre * to happen (LDC reset), otherwise we will retry to recover 65432f5224aeSachartre * after a delay. 65442f5224aeSachartre */ 65452f5224aeSachartre if (vdc->ownership & VDC_OWNERSHIP_GRANTED) 65462f5224aeSachartre timeout = 0; 65472f5224aeSachartre else 65482f5224aeSachartre timeout = ddi_get_lbolt() + 65492f5224aeSachartre drv_usectohz(vdc_ownership_delay); 65502f5224aeSachartre 65512f5224aeSachartre /* Release the ownership_lock and wait on the vdc lock */ 65522f5224aeSachartre mutex_exit(&vdc->ownership_lock); 65532f5224aeSachartre 65542f5224aeSachartre if (timeout == 0) 65552f5224aeSachartre (void) cv_wait(&vdc->ownership_cv, &vdc->lock); 65562f5224aeSachartre else 65572f5224aeSachartre (void) cv_timedwait(&vdc->ownership_cv, 65582f5224aeSachartre &vdc->lock, timeout); 65592f5224aeSachartre 65602f5224aeSachartre mutex_exit(&vdc->lock); 65612f5224aeSachartre 65622f5224aeSachartre mutex_enter(&vdc->ownership_lock); 65632f5224aeSachartre mutex_enter(&vdc->lock); 65642f5224aeSachartre } 65652f5224aeSachartre 65662f5224aeSachartre vdc->ownership_thread = NULL; 65672f5224aeSachartre mutex_exit(&vdc->lock); 65682f5224aeSachartre mutex_exit(&vdc->ownership_lock); 65692f5224aeSachartre 65702f5224aeSachartre thread_exit(); 65712f5224aeSachartre } 65722f5224aeSachartre 65732f5224aeSachartre static void 65742f5224aeSachartre vdc_ownership_update(vdc_t *vdc, int ownership_flags) 65752f5224aeSachartre { 65762f5224aeSachartre ASSERT(MUTEX_HELD(&vdc->ownership_lock)); 65772f5224aeSachartre 65782f5224aeSachartre mutex_enter(&vdc->lock); 65792f5224aeSachartre vdc->ownership = ownership_flags; 65802f5224aeSachartre if ((vdc->ownership & VDC_OWNERSHIP_WANTED) && 65812f5224aeSachartre vdc->ownership_thread == NULL) { 65822f5224aeSachartre /* start ownership thread */ 65832f5224aeSachartre vdc->ownership_thread = thread_create(NULL, 0, 65842f5224aeSachartre vdc_ownership_thread, vdc, 0, &p0, TS_RUN, 65852f5224aeSachartre v.v_maxsyspri - 2); 65862f5224aeSachartre } else { 65872f5224aeSachartre /* notify the ownership thread */ 65882f5224aeSachartre cv_signal(&vdc->ownership_cv); 65892f5224aeSachartre } 65902f5224aeSachartre mutex_exit(&vdc->lock); 65912f5224aeSachartre } 65922f5224aeSachartre 65932f5224aeSachartre /* 65942f5224aeSachartre * Get the size and the block size of a virtual disk from the vdisk server. 65952f5224aeSachartre */ 65962f5224aeSachartre static int 6597de3a5331SRamesh Chitrothu vdc_get_capacity(vdc_t *vdc, size_t *dsk_size, size_t *blk_size) 65982f5224aeSachartre { 65992f5224aeSachartre int rv = 0; 66002f5224aeSachartre size_t alloc_len; 66012f5224aeSachartre vd_capacity_t *vd_cap; 66022f5224aeSachartre 6603de3a5331SRamesh Chitrothu ASSERT(MUTEX_NOT_HELD(&vdc->lock)); 66042f5224aeSachartre 66052f5224aeSachartre alloc_len = P2ROUNDUP(sizeof (vd_capacity_t), sizeof (uint64_t)); 66062f5224aeSachartre 66072f5224aeSachartre vd_cap = kmem_zalloc(alloc_len, KM_SLEEP); 66082f5224aeSachartre 66092f5224aeSachartre rv = vdc_do_sync_op(vdc, VD_OP_GET_CAPACITY, (caddr_t)vd_cap, alloc_len, 66102f5224aeSachartre 0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_TRUE); 66112f5224aeSachartre 6612de3a5331SRamesh Chitrothu *dsk_size = vd_cap->vdisk_size; 6613de3a5331SRamesh Chitrothu *blk_size = vd_cap->vdisk_block_size; 66142f5224aeSachartre 66152f5224aeSachartre kmem_free(vd_cap, alloc_len); 66162f5224aeSachartre return (rv); 66172f5224aeSachartre } 66182f5224aeSachartre 66192f5224aeSachartre /* 6620de3a5331SRamesh Chitrothu * Check the disk capacity. Disk size information is updated if size has 6621de3a5331SRamesh Chitrothu * changed. 6622de3a5331SRamesh Chitrothu * 6623de3a5331SRamesh Chitrothu * Return 0 if the disk capacity is available, or non-zero if it is not. 6624de3a5331SRamesh Chitrothu */ 6625de3a5331SRamesh Chitrothu static int 6626de3a5331SRamesh Chitrothu vdc_check_capacity(vdc_t *vdc) 6627de3a5331SRamesh Chitrothu { 6628de3a5331SRamesh Chitrothu size_t dsk_size, blk_size; 6629de3a5331SRamesh Chitrothu int rv; 6630de3a5331SRamesh Chitrothu 66313f4df6d3SAlexandre Chartre /* 66323f4df6d3SAlexandre Chartre * If the vdisk does not support the VD_OP_GET_CAPACITY operation 66333f4df6d3SAlexandre Chartre * then the disk capacity has been retrieved during the handshake 66343f4df6d3SAlexandre Chartre * and there's nothing more to do here. 66353f4df6d3SAlexandre Chartre */ 66363f4df6d3SAlexandre Chartre if (!VD_OP_SUPPORTED(vdc->operations, VD_OP_GET_CAPACITY)) 66373f4df6d3SAlexandre Chartre return (0); 66383f4df6d3SAlexandre Chartre 6639de3a5331SRamesh Chitrothu if ((rv = vdc_get_capacity(vdc, &dsk_size, &blk_size)) != 0) 6640de3a5331SRamesh Chitrothu return (rv); 6641de3a5331SRamesh Chitrothu 6642de3a5331SRamesh Chitrothu if (dsk_size == VD_SIZE_UNKNOWN || dsk_size == 0) 6643de3a5331SRamesh Chitrothu return (EINVAL); 6644de3a5331SRamesh Chitrothu 6645de3a5331SRamesh Chitrothu mutex_enter(&vdc->lock); 6646de3a5331SRamesh Chitrothu vdc_update_size(vdc, dsk_size, blk_size, vdc->max_xfer_sz); 6647de3a5331SRamesh Chitrothu mutex_exit(&vdc->lock); 6648de3a5331SRamesh Chitrothu 6649de3a5331SRamesh Chitrothu return (0); 6650de3a5331SRamesh Chitrothu } 6651de3a5331SRamesh Chitrothu 6652de3a5331SRamesh Chitrothu /* 66531ae08745Sheppo * This structure is used in the DKIO(7I) array below. 66541ae08745Sheppo */ 66551ae08745Sheppo typedef struct vdc_dk_ioctl { 66561ae08745Sheppo uint8_t op; /* VD_OP_XXX value */ 66571ae08745Sheppo int cmd; /* Solaris ioctl operation number */ 66581ae08745Sheppo size_t nbytes; /* size of structure to be copied */ 66590a55fbb7Slm66018 66600a55fbb7Slm66018 /* function to convert between vDisk and Solaris structure formats */ 6661d10e4ef2Snarayan int (*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg, 6662d10e4ef2Snarayan int mode, int dir); 66631ae08745Sheppo } vdc_dk_ioctl_t; 66641ae08745Sheppo 66651ae08745Sheppo /* 66661ae08745Sheppo * Subset of DKIO(7I) operations currently supported 66671ae08745Sheppo */ 66681ae08745Sheppo static vdc_dk_ioctl_t dk_ioctl[] = { 6669eff7243fSlm66018 {VD_OP_FLUSH, DKIOCFLUSHWRITECACHE, 0, 66700a55fbb7Slm66018 vdc_null_copy_func}, 66710a55fbb7Slm66018 {VD_OP_GET_WCE, DKIOCGETWCE, sizeof (int), 66724bac2208Snarayan vdc_get_wce_convert}, 66730a55fbb7Slm66018 {VD_OP_SET_WCE, DKIOCSETWCE, sizeof (int), 66744bac2208Snarayan vdc_set_wce_convert}, 66750a55fbb7Slm66018 {VD_OP_GET_VTOC, DKIOCGVTOC, sizeof (vd_vtoc_t), 66760a55fbb7Slm66018 vdc_get_vtoc_convert}, 66770a55fbb7Slm66018 {VD_OP_SET_VTOC, DKIOCSVTOC, sizeof (vd_vtoc_t), 66780a55fbb7Slm66018 vdc_set_vtoc_convert}, 6679342440ecSPrasad Singamsetty {VD_OP_GET_VTOC, DKIOCGEXTVTOC, sizeof (vd_vtoc_t), 6680342440ecSPrasad Singamsetty vdc_get_extvtoc_convert}, 6681342440ecSPrasad Singamsetty {VD_OP_SET_VTOC, DKIOCSEXTVTOC, sizeof (vd_vtoc_t), 6682342440ecSPrasad Singamsetty vdc_set_extvtoc_convert}, 66830a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCGGEOM, sizeof (vd_geom_t), 66840a55fbb7Slm66018 vdc_get_geom_convert}, 66850a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCG_PHYGEOM, sizeof (vd_geom_t), 66860a55fbb7Slm66018 vdc_get_geom_convert}, 66870a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, DKIOCG_VIRTGEOM, sizeof (vd_geom_t), 66880a55fbb7Slm66018 vdc_get_geom_convert}, 66890a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, DKIOCSGEOM, sizeof (vd_geom_t), 66900a55fbb7Slm66018 vdc_set_geom_convert}, 66914bac2208Snarayan {VD_OP_GET_EFI, DKIOCGETEFI, 0, 66924bac2208Snarayan vdc_get_efi_convert}, 66934bac2208Snarayan {VD_OP_SET_EFI, DKIOCSETEFI, 0, 66944bac2208Snarayan vdc_set_efi_convert}, 66950a55fbb7Slm66018 669687a7269eSachartre /* DIOCTL_RWCMD is converted to a read or a write */ 669787a7269eSachartre {0, DIOCTL_RWCMD, sizeof (struct dadkio_rwcmd), NULL}, 669887a7269eSachartre 66992f5224aeSachartre /* mhd(7I) non-shared multihost disks ioctls */ 67002f5224aeSachartre {0, MHIOCTKOWN, 0, vdc_null_copy_func}, 67012f5224aeSachartre {0, MHIOCRELEASE, 0, vdc_null_copy_func}, 67022f5224aeSachartre {0, MHIOCSTATUS, 0, vdc_null_copy_func}, 67032f5224aeSachartre {0, MHIOCQRESERVE, 0, vdc_null_copy_func}, 67042f5224aeSachartre 67052f5224aeSachartre /* mhd(7I) shared multihost disks ioctls */ 67062f5224aeSachartre {0, MHIOCGRP_INKEYS, 0, vdc_null_copy_func}, 67072f5224aeSachartre {0, MHIOCGRP_INRESV, 0, vdc_null_copy_func}, 67082f5224aeSachartre {0, MHIOCGRP_REGISTER, 0, vdc_null_copy_func}, 67092f5224aeSachartre {0, MHIOCGRP_RESERVE, 0, vdc_null_copy_func}, 67102f5224aeSachartre {0, MHIOCGRP_PREEMPTANDABORT, 0, vdc_null_copy_func}, 67112f5224aeSachartre {0, MHIOCGRP_REGISTERANDIGNOREKEY, 0, vdc_null_copy_func}, 67122f5224aeSachartre 67132f5224aeSachartre /* mhd(7I) failfast ioctl */ 67142f5224aeSachartre {0, MHIOCENFAILFAST, 0, vdc_null_copy_func}, 67152f5224aeSachartre 67160a55fbb7Slm66018 /* 67170a55fbb7Slm66018 * These particular ioctls are not sent to the server - vdc fakes up 67180a55fbb7Slm66018 * the necessary info. 67190a55fbb7Slm66018 */ 67200a55fbb7Slm66018 {0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func}, 67210a55fbb7Slm66018 {0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func}, 67220a55fbb7Slm66018 {0, USCSICMD, sizeof (struct uscsi_cmd), vdc_null_copy_func}, 67239642afceSachartre {0, DKIOCPARTITION, 0, vdc_null_copy_func }, 672487a7269eSachartre {0, DKIOCGAPART, 0, vdc_null_copy_func }, 67250a55fbb7Slm66018 {0, DKIOCREMOVABLE, 0, vdc_null_copy_func}, 67260a55fbb7Slm66018 {0, CDROMREADOFFSET, 0, vdc_null_copy_func} 67271ae08745Sheppo }; 67281ae08745Sheppo 67291ae08745Sheppo /* 6730edcc0754Sachartre * This function handles ioctl requests from the vd_efi_alloc_and_read() 6731edcc0754Sachartre * function and forward them to the vdisk. 67322f5224aeSachartre */ 67332f5224aeSachartre static int 6734edcc0754Sachartre vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg) 67352f5224aeSachartre { 6736edcc0754Sachartre vdc_t *vdc = (vdc_t *)vdisk; 6737edcc0754Sachartre dev_t dev; 67382f5224aeSachartre int rval; 6739edcc0754Sachartre 6740edcc0754Sachartre dev = makedevice(ddi_driver_major(vdc->dip), 6741edcc0754Sachartre VD_MAKE_DEV(vdc->instance, 0)); 6742edcc0754Sachartre 6743edcc0754Sachartre return (vd_process_ioctl(dev, cmd, (caddr_t)arg, FKIOCTL, &rval)); 67442f5224aeSachartre } 67452f5224aeSachartre 67462f5224aeSachartre /* 67471ae08745Sheppo * Function: 67481ae08745Sheppo * vd_process_ioctl() 67491ae08745Sheppo * 67501ae08745Sheppo * Description: 67510a55fbb7Slm66018 * This routine processes disk specific ioctl calls 67521ae08745Sheppo * 67531ae08745Sheppo * Arguments: 67541ae08745Sheppo * dev - the device number 67551ae08745Sheppo * cmd - the operation [dkio(7I)] to be processed 67561ae08745Sheppo * arg - pointer to user provided structure 67571ae08745Sheppo * (contains data to be set or reference parameter for get) 67581ae08745Sheppo * mode - bit flag, indicating open settings, 32/64 bit type, etc 67592f5224aeSachartre * rvalp - pointer to return value for calling process. 67601ae08745Sheppo * 67611ae08745Sheppo * Return Code: 67621ae08745Sheppo * 0 67631ae08745Sheppo * EFAULT 67641ae08745Sheppo * ENXIO 67651ae08745Sheppo * EIO 67661ae08745Sheppo * ENOTSUP 67671ae08745Sheppo */ 67681ae08745Sheppo static int 67692f5224aeSachartre vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, int *rvalp) 67701ae08745Sheppo { 67710d0c8d4bSnarayan int instance = VDCUNIT(dev); 67721ae08745Sheppo vdc_t *vdc = NULL; 67731ae08745Sheppo int rv = -1; 67741ae08745Sheppo int idx = 0; /* index into dk_ioctl[] */ 67751ae08745Sheppo size_t len = 0; /* #bytes to send to vds */ 67761ae08745Sheppo size_t alloc_len = 0; /* #bytes to allocate mem for */ 67771ae08745Sheppo caddr_t mem_p = NULL; 67781ae08745Sheppo size_t nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0])); 67793af08d82Slm66018 vdc_dk_ioctl_t *iop; 67801ae08745Sheppo 67811ae08745Sheppo vdc = ddi_get_soft_state(vdc_state, instance); 67821ae08745Sheppo if (vdc == NULL) { 67831ae08745Sheppo cmn_err(CE_NOTE, "![%d] Could not get soft state structure", 67841ae08745Sheppo instance); 67851ae08745Sheppo return (ENXIO); 67861ae08745Sheppo } 67871ae08745Sheppo 67883af08d82Slm66018 DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n", 67893af08d82Slm66018 instance, cmd, dev, ddi_model_convert_from(mode & FMODELS)); 67901ae08745Sheppo 67912f5224aeSachartre if (rvalp != NULL) { 67922f5224aeSachartre /* the return value of the ioctl is 0 by default */ 67932f5224aeSachartre *rvalp = 0; 67942f5224aeSachartre } 67952f5224aeSachartre 67961ae08745Sheppo /* 67971ae08745Sheppo * Validate the ioctl operation to be performed. 67981ae08745Sheppo * 67991ae08745Sheppo * If we have looped through the array without finding a match then we 68001ae08745Sheppo * don't support this ioctl. 68011ae08745Sheppo */ 68021ae08745Sheppo for (idx = 0; idx < nioctls; idx++) { 68031ae08745Sheppo if (cmd == dk_ioctl[idx].cmd) 68041ae08745Sheppo break; 68051ae08745Sheppo } 68061ae08745Sheppo 68071ae08745Sheppo if (idx >= nioctls) { 68083af08d82Slm66018 DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n", 6809e1ebb9ecSlm66018 vdc->instance, cmd); 68101ae08745Sheppo return (ENOTSUP); 68111ae08745Sheppo } 68121ae08745Sheppo 68133af08d82Slm66018 iop = &(dk_ioctl[idx]); 68143af08d82Slm66018 68154bac2208Snarayan if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) { 68164bac2208Snarayan /* size is not fixed for EFI ioctls, it depends on ioctl arg */ 68174bac2208Snarayan dk_efi_t dk_efi; 68184bac2208Snarayan 68194bac2208Snarayan rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode); 68204bac2208Snarayan if (rv != 0) 68214bac2208Snarayan return (EFAULT); 68224bac2208Snarayan 68234bac2208Snarayan len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length; 68244bac2208Snarayan } else { 68253af08d82Slm66018 len = iop->nbytes; 68264bac2208Snarayan } 68271ae08745Sheppo 68282f5224aeSachartre /* check if the ioctl is applicable */ 68291ae08745Sheppo switch (cmd) { 68301ae08745Sheppo case CDROMREADOFFSET: 68311ae08745Sheppo case DKIOCREMOVABLE: 68321ae08745Sheppo return (ENOTTY); 68331ae08745Sheppo 68342f5224aeSachartre case USCSICMD: 68352f5224aeSachartre case MHIOCTKOWN: 68362f5224aeSachartre case MHIOCSTATUS: 68372f5224aeSachartre case MHIOCQRESERVE: 68382f5224aeSachartre case MHIOCRELEASE: 68392f5224aeSachartre case MHIOCGRP_INKEYS: 68402f5224aeSachartre case MHIOCGRP_INRESV: 68412f5224aeSachartre case MHIOCGRP_REGISTER: 68422f5224aeSachartre case MHIOCGRP_RESERVE: 68432f5224aeSachartre case MHIOCGRP_PREEMPTANDABORT: 68442f5224aeSachartre case MHIOCGRP_REGISTERANDIGNOREKEY: 68452f5224aeSachartre case MHIOCENFAILFAST: 68462f5224aeSachartre if (vdc->cinfo == NULL) 68472f5224aeSachartre return (ENXIO); 68482f5224aeSachartre if (vdc->cinfo->dki_ctype != DKC_SCSI_CCS) 68492f5224aeSachartre return (ENOTTY); 68502f5224aeSachartre break; 68512f5224aeSachartre 68522f5224aeSachartre case DIOCTL_RWCMD: 68532f5224aeSachartre if (vdc->cinfo == NULL) 68542f5224aeSachartre return (ENXIO); 68552f5224aeSachartre if (vdc->cinfo->dki_ctype != DKC_DIRECT) 68562f5224aeSachartre return (ENOTTY); 68572f5224aeSachartre break; 68582f5224aeSachartre 68592f5224aeSachartre case DKIOCINFO: 68602f5224aeSachartre if (vdc->cinfo == NULL) 68612f5224aeSachartre return (ENXIO); 68622f5224aeSachartre break; 68632f5224aeSachartre 68642f5224aeSachartre case DKIOCGMEDIAINFO: 68652f5224aeSachartre if (vdc->minfo == NULL) 68662f5224aeSachartre return (ENXIO); 68672f5224aeSachartre if (vdc_check_capacity(vdc) != 0) 68682f5224aeSachartre /* disk capacity is not available */ 68692f5224aeSachartre return (EIO); 68702f5224aeSachartre break; 68712f5224aeSachartre } 68722f5224aeSachartre 68732f5224aeSachartre /* 68742f5224aeSachartre * Deal with ioctls which require a processing different than 68752f5224aeSachartre * converting ioctl arguments and sending a corresponding 68762f5224aeSachartre * VD operation. 68772f5224aeSachartre */ 68782f5224aeSachartre switch (cmd) { 68792f5224aeSachartre 68802f5224aeSachartre case USCSICMD: 68812f5224aeSachartre { 68822f5224aeSachartre return (vdc_uscsi_cmd(vdc, arg, mode)); 68832f5224aeSachartre } 68842f5224aeSachartre 68852f5224aeSachartre case MHIOCTKOWN: 68862f5224aeSachartre { 68872f5224aeSachartre mutex_enter(&vdc->ownership_lock); 68882f5224aeSachartre /* 68892f5224aeSachartre * We have to set VDC_OWNERSHIP_WANTED now so that the ownership 68902f5224aeSachartre * can be flagged with VDC_OWNERSHIP_RESET if the LDC is reset 68912f5224aeSachartre * while we are processing the ioctl. 68922f5224aeSachartre */ 68932f5224aeSachartre vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED); 68942f5224aeSachartre 68952f5224aeSachartre rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE | 68962f5224aeSachartre VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE, mode); 68972f5224aeSachartre if (rv == 0) { 68982f5224aeSachartre vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED | 68992f5224aeSachartre VDC_OWNERSHIP_GRANTED); 69002f5224aeSachartre } else { 69012f5224aeSachartre vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE); 69022f5224aeSachartre } 69032f5224aeSachartre mutex_exit(&vdc->ownership_lock); 69042f5224aeSachartre return (rv); 69052f5224aeSachartre } 69062f5224aeSachartre 69072f5224aeSachartre case MHIOCRELEASE: 69082f5224aeSachartre { 69092f5224aeSachartre mutex_enter(&vdc->ownership_lock); 69102f5224aeSachartre rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, mode); 69112f5224aeSachartre if (rv == 0) { 69122f5224aeSachartre vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE); 69132f5224aeSachartre } 69142f5224aeSachartre mutex_exit(&vdc->ownership_lock); 69152f5224aeSachartre return (rv); 69162f5224aeSachartre } 69172f5224aeSachartre 69182f5224aeSachartre case MHIOCSTATUS: 69192f5224aeSachartre { 69202f5224aeSachartre uint64_t status; 69212f5224aeSachartre 69222f5224aeSachartre rv = vdc_access_get(vdc, &status, mode); 69232f5224aeSachartre if (rv == 0 && rvalp != NULL) 69242f5224aeSachartre *rvalp = (status & VD_ACCESS_ALLOWED)? 0 : 1; 69252f5224aeSachartre return (rv); 69262f5224aeSachartre } 69272f5224aeSachartre 69282f5224aeSachartre case MHIOCQRESERVE: 69292f5224aeSachartre { 69302f5224aeSachartre rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE, mode); 69312f5224aeSachartre return (rv); 69322f5224aeSachartre } 69332f5224aeSachartre 69342f5224aeSachartre case MHIOCGRP_INKEYS: 69352f5224aeSachartre { 69362f5224aeSachartre return (vdc_mhd_inkeys(vdc, arg, mode)); 69372f5224aeSachartre } 69382f5224aeSachartre 69392f5224aeSachartre case MHIOCGRP_INRESV: 69402f5224aeSachartre { 69412f5224aeSachartre return (vdc_mhd_inresv(vdc, arg, mode)); 69422f5224aeSachartre } 69432f5224aeSachartre 69442f5224aeSachartre case MHIOCGRP_REGISTER: 69452f5224aeSachartre { 69462f5224aeSachartre return (vdc_mhd_register(vdc, arg, mode)); 69472f5224aeSachartre } 69482f5224aeSachartre 69492f5224aeSachartre case MHIOCGRP_RESERVE: 69502f5224aeSachartre { 69512f5224aeSachartre return (vdc_mhd_reserve(vdc, arg, mode)); 69522f5224aeSachartre } 69532f5224aeSachartre 69542f5224aeSachartre case MHIOCGRP_PREEMPTANDABORT: 69552f5224aeSachartre { 69562f5224aeSachartre return (vdc_mhd_preemptabort(vdc, arg, mode)); 69572f5224aeSachartre } 69582f5224aeSachartre 69592f5224aeSachartre case MHIOCGRP_REGISTERANDIGNOREKEY: 69602f5224aeSachartre { 69612f5224aeSachartre return (vdc_mhd_registerignore(vdc, arg, mode)); 69622f5224aeSachartre } 69632f5224aeSachartre 69642f5224aeSachartre case MHIOCENFAILFAST: 69652f5224aeSachartre { 69662f5224aeSachartre rv = vdc_failfast(vdc, arg, mode); 69672f5224aeSachartre return (rv); 69682f5224aeSachartre } 69692f5224aeSachartre 697087a7269eSachartre case DIOCTL_RWCMD: 697187a7269eSachartre { 697287a7269eSachartre return (vdc_dioctl_rwcmd(dev, arg, mode)); 697387a7269eSachartre } 697487a7269eSachartre 697587a7269eSachartre case DKIOCGAPART: 697687a7269eSachartre { 69779642afceSachartre return (vdc_dkio_gapart(vdc, arg, mode)); 69789642afceSachartre } 69799642afceSachartre 69809642afceSachartre case DKIOCPARTITION: 69819642afceSachartre { 69829642afceSachartre return (vdc_dkio_partition(vdc, arg, mode)); 698387a7269eSachartre } 698487a7269eSachartre 69851ae08745Sheppo case DKIOCINFO: 69861ae08745Sheppo { 69871ae08745Sheppo struct dk_cinfo cinfo; 69881ae08745Sheppo 69891ae08745Sheppo bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo)); 69900d0c8d4bSnarayan cinfo.dki_partition = VDCPART(dev); 69911ae08745Sheppo 69921ae08745Sheppo rv = ddi_copyout(&cinfo, (void *)arg, 69931ae08745Sheppo sizeof (struct dk_cinfo), mode); 69941ae08745Sheppo if (rv != 0) 69951ae08745Sheppo return (EFAULT); 69961ae08745Sheppo 69971ae08745Sheppo return (0); 69981ae08745Sheppo } 69991ae08745Sheppo 70001ae08745Sheppo case DKIOCGMEDIAINFO: 70018e6a2a04Slm66018 { 70022f5224aeSachartre ASSERT(vdc->vdisk_size != 0); 7003de3a5331SRamesh Chitrothu ASSERT(vdc->minfo->dki_capacity != 0); 70041ae08745Sheppo rv = ddi_copyout(vdc->minfo, (void *)arg, 70051ae08745Sheppo sizeof (struct dk_minfo), mode); 70061ae08745Sheppo if (rv != 0) 70071ae08745Sheppo return (EFAULT); 70081ae08745Sheppo 70091ae08745Sheppo return (0); 70101ae08745Sheppo } 70111ae08745Sheppo 70128e6a2a04Slm66018 case DKIOCFLUSHWRITECACHE: 70138e6a2a04Slm66018 { 701417cadca8Slm66018 struct dk_callback *dkc = 701517cadca8Slm66018 (struct dk_callback *)(uintptr_t)arg; 70168e6a2a04Slm66018 vdc_dk_arg_t *dkarg = NULL; 70178e6a2a04Slm66018 70183af08d82Slm66018 DMSG(vdc, 1, "[%d] Flush W$: mode %x\n", 70193af08d82Slm66018 instance, mode); 70208e6a2a04Slm66018 70218e6a2a04Slm66018 /* 70228e6a2a04Slm66018 * If arg is NULL, then there is no callback function 70238e6a2a04Slm66018 * registered and the call operates synchronously; we 70248e6a2a04Slm66018 * break and continue with the rest of the function and 70258e6a2a04Slm66018 * wait for vds to return (i.e. after the request to 70268e6a2a04Slm66018 * vds returns successfully, all writes completed prior 70278e6a2a04Slm66018 * to the ioctl will have been flushed from the disk 70288e6a2a04Slm66018 * write cache to persistent media. 70298e6a2a04Slm66018 * 70308e6a2a04Slm66018 * If a callback function is registered, we dispatch 70318e6a2a04Slm66018 * the request on a task queue and return immediately. 70328e6a2a04Slm66018 * The callback will deal with informing the calling 70338e6a2a04Slm66018 * thread that the flush request is completed. 70348e6a2a04Slm66018 */ 70358e6a2a04Slm66018 if (dkc == NULL) 70368e6a2a04Slm66018 break; 70378e6a2a04Slm66018 7038eff7243fSlm66018 /* 7039eff7243fSlm66018 * the asynchronous callback is only supported if 7040eff7243fSlm66018 * invoked from within the kernel 7041eff7243fSlm66018 */ 7042eff7243fSlm66018 if ((mode & FKIOCTL) == 0) 7043eff7243fSlm66018 return (ENOTSUP); 7044eff7243fSlm66018 70458e6a2a04Slm66018 dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP); 70468e6a2a04Slm66018 70478e6a2a04Slm66018 dkarg->mode = mode; 70488e6a2a04Slm66018 dkarg->dev = dev; 70498e6a2a04Slm66018 bcopy(dkc, &dkarg->dkc, sizeof (*dkc)); 70508e6a2a04Slm66018 70518e6a2a04Slm66018 mutex_enter(&vdc->lock); 70528e6a2a04Slm66018 vdc->dkio_flush_pending++; 70538e6a2a04Slm66018 dkarg->vdc = vdc; 70548e6a2a04Slm66018 mutex_exit(&vdc->lock); 70558e6a2a04Slm66018 70568e6a2a04Slm66018 /* put the request on a task queue */ 70578e6a2a04Slm66018 rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb, 70588e6a2a04Slm66018 (void *)dkarg, DDI_SLEEP); 70593af08d82Slm66018 if (rv == NULL) { 70603af08d82Slm66018 /* clean up if dispatch fails */ 70613af08d82Slm66018 mutex_enter(&vdc->lock); 70623af08d82Slm66018 vdc->dkio_flush_pending--; 706378fcd0a1Sachartre mutex_exit(&vdc->lock); 70643af08d82Slm66018 kmem_free(dkarg, sizeof (vdc_dk_arg_t)); 70653af08d82Slm66018 } 70668e6a2a04Slm66018 70678e6a2a04Slm66018 return (rv == NULL ? ENOMEM : 0); 70688e6a2a04Slm66018 } 70698e6a2a04Slm66018 } 70708e6a2a04Slm66018 70711ae08745Sheppo /* catch programming error in vdc - should be a VD_OP_XXX ioctl */ 70723af08d82Slm66018 ASSERT(iop->op != 0); 70731ae08745Sheppo 707417cadca8Slm66018 /* check if the vDisk server handles the operation for this vDisk */ 707517cadca8Slm66018 if (VD_OP_SUPPORTED(vdc->operations, iop->op) == B_FALSE) { 707617cadca8Slm66018 DMSG(vdc, 0, "[%d] Unsupported VD_OP operation (0x%x)\n", 707717cadca8Slm66018 vdc->instance, iop->op); 707817cadca8Slm66018 return (ENOTSUP); 707917cadca8Slm66018 } 708017cadca8Slm66018 70811ae08745Sheppo /* LDC requires that the memory being mapped is 8-byte aligned */ 70821ae08745Sheppo alloc_len = P2ROUNDUP(len, sizeof (uint64_t)); 70833af08d82Slm66018 DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n", 70843af08d82Slm66018 instance, len, alloc_len); 70851ae08745Sheppo 7086eff7243fSlm66018 if (alloc_len > 0) 70871ae08745Sheppo mem_p = kmem_zalloc(alloc_len, KM_SLEEP); 70881ae08745Sheppo 70890a55fbb7Slm66018 /* 7090eff7243fSlm66018 * Call the conversion function for this ioctl which, if necessary, 70910a55fbb7Slm66018 * converts from the Solaris format to the format ARC'ed 70920a55fbb7Slm66018 * as part of the vDisk protocol (FWARC 2006/195) 70930a55fbb7Slm66018 */ 70943af08d82Slm66018 ASSERT(iop->convert != NULL); 70953af08d82Slm66018 rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN); 70961ae08745Sheppo if (rv != 0) { 70973af08d82Slm66018 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n", 7098e1ebb9ecSlm66018 instance, rv, cmd); 70991ae08745Sheppo if (mem_p != NULL) 71001ae08745Sheppo kmem_free(mem_p, alloc_len); 71010a55fbb7Slm66018 return (rv); 71021ae08745Sheppo } 71031ae08745Sheppo 71041ae08745Sheppo /* 71051ae08745Sheppo * send request to vds to service the ioctl. 71061ae08745Sheppo */ 71073af08d82Slm66018 rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len, 71080d0c8d4bSnarayan VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode, 71092f5224aeSachartre VIO_both_dir, B_TRUE); 711078fcd0a1Sachartre 71111ae08745Sheppo if (rv != 0) { 71121ae08745Sheppo /* 71131ae08745Sheppo * This is not necessarily an error. The ioctl could 71141ae08745Sheppo * be returning a value such as ENOTTY to indicate 71151ae08745Sheppo * that the ioctl is not applicable. 71161ae08745Sheppo */ 71173af08d82Slm66018 DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n", 7118e1ebb9ecSlm66018 instance, rv, cmd); 71191ae08745Sheppo if (mem_p != NULL) 71201ae08745Sheppo kmem_free(mem_p, alloc_len); 7121d10e4ef2Snarayan 71221ae08745Sheppo return (rv); 71231ae08745Sheppo } 71241ae08745Sheppo 71251ae08745Sheppo /* 71260a55fbb7Slm66018 * Call the conversion function (if it exists) for this ioctl 71270a55fbb7Slm66018 * which converts from the format ARC'ed as part of the vDisk 71280a55fbb7Slm66018 * protocol (FWARC 2006/195) back to a format understood by 71290a55fbb7Slm66018 * the rest of Solaris. 71301ae08745Sheppo */ 71313af08d82Slm66018 rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT); 71320a55fbb7Slm66018 if (rv != 0) { 71333af08d82Slm66018 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n", 7134e1ebb9ecSlm66018 instance, rv, cmd); 71351ae08745Sheppo if (mem_p != NULL) 71361ae08745Sheppo kmem_free(mem_p, alloc_len); 71370a55fbb7Slm66018 return (rv); 71381ae08745Sheppo } 71391ae08745Sheppo 71401ae08745Sheppo if (mem_p != NULL) 71411ae08745Sheppo kmem_free(mem_p, alloc_len); 71421ae08745Sheppo 71431ae08745Sheppo return (rv); 71441ae08745Sheppo } 71451ae08745Sheppo 71461ae08745Sheppo /* 71471ae08745Sheppo * Function: 71480a55fbb7Slm66018 * 71490a55fbb7Slm66018 * Description: 71500a55fbb7Slm66018 * This is an empty conversion function used by ioctl calls which 71510a55fbb7Slm66018 * do not need to convert the data being passed in/out to userland 71520a55fbb7Slm66018 */ 71530a55fbb7Slm66018 static int 7154d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir) 71550a55fbb7Slm66018 { 7156d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 71570a55fbb7Slm66018 _NOTE(ARGUNUSED(from)) 71580a55fbb7Slm66018 _NOTE(ARGUNUSED(to)) 71590a55fbb7Slm66018 _NOTE(ARGUNUSED(mode)) 71600a55fbb7Slm66018 _NOTE(ARGUNUSED(dir)) 71610a55fbb7Slm66018 71620a55fbb7Slm66018 return (0); 71630a55fbb7Slm66018 } 71640a55fbb7Slm66018 71654bac2208Snarayan static int 71664bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to, 71674bac2208Snarayan int mode, int dir) 71684bac2208Snarayan { 71694bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 71704bac2208Snarayan 71714bac2208Snarayan if (dir == VD_COPYIN) 71724bac2208Snarayan return (0); /* nothing to do */ 71734bac2208Snarayan 71744bac2208Snarayan if (ddi_copyout(from, to, sizeof (int), mode) != 0) 71754bac2208Snarayan return (EFAULT); 71764bac2208Snarayan 71774bac2208Snarayan return (0); 71784bac2208Snarayan } 71794bac2208Snarayan 71804bac2208Snarayan static int 71814bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to, 71824bac2208Snarayan int mode, int dir) 71834bac2208Snarayan { 71844bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 71854bac2208Snarayan 71864bac2208Snarayan if (dir == VD_COPYOUT) 71874bac2208Snarayan return (0); /* nothing to do */ 71884bac2208Snarayan 71894bac2208Snarayan if (ddi_copyin(from, to, sizeof (int), mode) != 0) 71904bac2208Snarayan return (EFAULT); 71914bac2208Snarayan 71924bac2208Snarayan return (0); 71934bac2208Snarayan } 71944bac2208Snarayan 71950a55fbb7Slm66018 /* 71960a55fbb7Slm66018 * Function: 71970a55fbb7Slm66018 * vdc_get_vtoc_convert() 71980a55fbb7Slm66018 * 71990a55fbb7Slm66018 * Description: 7200d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCGVTOC 7201d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 7202d10e4ef2Snarayan * 7203d10e4ef2Snarayan * In the struct vtoc definition, the timestamp field is marked as not 7204d10e4ef2Snarayan * supported so it is not part of vDisk protocol (FWARC 2006/195). 7205d10e4ef2Snarayan * However SVM uses that field to check it can write into the VTOC, 7206d10e4ef2Snarayan * so we fake up the info of that field. 72070a55fbb7Slm66018 * 72080a55fbb7Slm66018 * Arguments: 7209d10e4ef2Snarayan * vdc - the vDisk client 72100a55fbb7Slm66018 * from - the buffer containing the data to be copied from 72110a55fbb7Slm66018 * to - the buffer to be copied to 72120a55fbb7Slm66018 * mode - flags passed to ioctl() call 72130a55fbb7Slm66018 * dir - the "direction" of the copy - VD_COPYIN or VD_COPYOUT 72140a55fbb7Slm66018 * 72150a55fbb7Slm66018 * Return Code: 72160a55fbb7Slm66018 * 0 - Success 72170a55fbb7Slm66018 * ENXIO - incorrect buffer passed in. 7218d10e4ef2Snarayan * EFAULT - ddi_copyout routine encountered an error. 72190a55fbb7Slm66018 */ 72200a55fbb7Slm66018 static int 7221d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 72220a55fbb7Slm66018 { 7223d10e4ef2Snarayan int i; 7224342440ecSPrasad Singamsetty struct vtoc vtoc; 7225342440ecSPrasad Singamsetty struct vtoc32 vtoc32; 7226342440ecSPrasad Singamsetty struct extvtoc evtoc; 7227342440ecSPrasad Singamsetty int rv; 72280a55fbb7Slm66018 72290a55fbb7Slm66018 if (dir != VD_COPYOUT) 72300a55fbb7Slm66018 return (0); /* nothing to do */ 72310a55fbb7Slm66018 72320a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 72330a55fbb7Slm66018 return (ENXIO); 72340a55fbb7Slm66018 7235342440ecSPrasad Singamsetty if (vdc->vdisk_size > VD_OLDVTOC_LIMIT) 7236342440ecSPrasad Singamsetty return (EOVERFLOW); 72370a55fbb7Slm66018 7238342440ecSPrasad Singamsetty VD_VTOC2VTOC((vd_vtoc_t *)from, &evtoc); 7239d10e4ef2Snarayan 7240d10e4ef2Snarayan /* fake the VTOC timestamp field */ 7241d10e4ef2Snarayan for (i = 0; i < V_NUMPAR; i++) { 7242342440ecSPrasad Singamsetty evtoc.timestamp[i] = vdc->vtoc->timestamp[i]; 7243d10e4ef2Snarayan } 7244d10e4ef2Snarayan 72450a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 724617cadca8Slm66018 /* LINTED E_ASSIGN_NARROW_CONV */ 7247342440ecSPrasad Singamsetty extvtoctovtoc32(evtoc, vtoc32); 7248342440ecSPrasad Singamsetty rv = ddi_copyout(&vtoc32, to, sizeof (vtoc32), mode); 72490a55fbb7Slm66018 if (rv != 0) 72500a55fbb7Slm66018 rv = EFAULT; 7251342440ecSPrasad Singamsetty } else { 7252342440ecSPrasad Singamsetty extvtoctovtoc(evtoc, vtoc); 7253342440ecSPrasad Singamsetty rv = ddi_copyout(&vtoc, to, sizeof (vtoc), mode); 7254342440ecSPrasad Singamsetty if (rv != 0) 7255342440ecSPrasad Singamsetty rv = EFAULT; 7256342440ecSPrasad Singamsetty } 72570a55fbb7Slm66018 72580a55fbb7Slm66018 return (rv); 72590a55fbb7Slm66018 } 72600a55fbb7Slm66018 72610a55fbb7Slm66018 /* 72620a55fbb7Slm66018 * Function: 72630a55fbb7Slm66018 * vdc_set_vtoc_convert() 72640a55fbb7Slm66018 * 72650a55fbb7Slm66018 * Description: 7266d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCSVTOC 7267d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 72680a55fbb7Slm66018 * 72690a55fbb7Slm66018 * Arguments: 7270d10e4ef2Snarayan * vdc - the vDisk client 72710a55fbb7Slm66018 * from - Buffer with data 72720a55fbb7Slm66018 * to - Buffer where data is to be copied to 72730a55fbb7Slm66018 * mode - flags passed to ioctl 72740a55fbb7Slm66018 * dir - direction of copy (in or out) 72750a55fbb7Slm66018 * 72760a55fbb7Slm66018 * Return Code: 72770a55fbb7Slm66018 * 0 - Success 72780a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 72790a55fbb7Slm66018 * EFAULT - ddi_copyin of data failed 72800a55fbb7Slm66018 */ 72810a55fbb7Slm66018 static int 7282d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 72830a55fbb7Slm66018 { 7284342440ecSPrasad Singamsetty void *uvtoc; 7285342440ecSPrasad Singamsetty struct vtoc vtoc; 7286342440ecSPrasad Singamsetty struct vtoc32 vtoc32; 7287342440ecSPrasad Singamsetty struct extvtoc evtoc; 7288342440ecSPrasad Singamsetty int i, rv; 72890a55fbb7Slm66018 72900a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 72910a55fbb7Slm66018 return (ENXIO); 72920a55fbb7Slm66018 7293342440ecSPrasad Singamsetty if (vdc->vdisk_size > VD_OLDVTOC_LIMIT) 7294342440ecSPrasad Singamsetty return (EOVERFLOW); 72952f5224aeSachartre 7296342440ecSPrasad Singamsetty uvtoc = (dir == VD_COPYIN)? from : to; 72970a55fbb7Slm66018 72980a55fbb7Slm66018 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 7299342440ecSPrasad Singamsetty rv = ddi_copyin(uvtoc, &vtoc32, sizeof (vtoc32), mode); 7300342440ecSPrasad Singamsetty if (rv != 0) 7301342440ecSPrasad Singamsetty return (EFAULT); 7302342440ecSPrasad Singamsetty vtoc32toextvtoc(vtoc32, evtoc); 73030a55fbb7Slm66018 } else { 7304342440ecSPrasad Singamsetty rv = ddi_copyin(uvtoc, &vtoc, sizeof (vtoc), mode); 7305342440ecSPrasad Singamsetty if (rv != 0) 7306342440ecSPrasad Singamsetty return (EFAULT); 7307342440ecSPrasad Singamsetty vtoctoextvtoc(vtoc, evtoc); 73080a55fbb7Slm66018 } 73090a55fbb7Slm66018 73102f5224aeSachartre if (dir == VD_COPYOUT) { 73112f5224aeSachartre /* 73122f5224aeSachartre * The disk label may have changed. Revalidate the disk 73135b98b509Sachartre * geometry. This will also update the device nodes. 73142f5224aeSachartre */ 73152f5224aeSachartre vdc_validate(vdc); 73162f5224aeSachartre 73172f5224aeSachartre /* 73182f5224aeSachartre * We also need to keep track of the timestamp fields. 73192f5224aeSachartre */ 73202f5224aeSachartre for (i = 0; i < V_NUMPAR; i++) { 7321342440ecSPrasad Singamsetty vdc->vtoc->timestamp[i] = evtoc.timestamp[i]; 7322342440ecSPrasad Singamsetty } 7323342440ecSPrasad Singamsetty 7324342440ecSPrasad Singamsetty } else { 7325342440ecSPrasad Singamsetty VTOC2VD_VTOC(&evtoc, (vd_vtoc_t *)to); 73262f5224aeSachartre } 73272f5224aeSachartre 73282f5224aeSachartre return (0); 73292f5224aeSachartre } 73302f5224aeSachartre 7331342440ecSPrasad Singamsetty static int 7332342440ecSPrasad Singamsetty vdc_get_extvtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 7333342440ecSPrasad Singamsetty { 7334342440ecSPrasad Singamsetty int i, rv; 7335342440ecSPrasad Singamsetty struct extvtoc evtoc; 7336342440ecSPrasad Singamsetty 7337342440ecSPrasad Singamsetty if (dir != VD_COPYOUT) 7338342440ecSPrasad Singamsetty return (0); /* nothing to do */ 7339342440ecSPrasad Singamsetty 7340342440ecSPrasad Singamsetty if ((from == NULL) || (to == NULL)) 7341342440ecSPrasad Singamsetty return (ENXIO); 7342342440ecSPrasad Singamsetty 7343342440ecSPrasad Singamsetty VD_VTOC2VTOC((vd_vtoc_t *)from, &evtoc); 7344342440ecSPrasad Singamsetty 7345342440ecSPrasad Singamsetty /* fake the VTOC timestamp field */ 7346342440ecSPrasad Singamsetty for (i = 0; i < V_NUMPAR; i++) { 7347342440ecSPrasad Singamsetty evtoc.timestamp[i] = vdc->vtoc->timestamp[i]; 7348342440ecSPrasad Singamsetty } 7349342440ecSPrasad Singamsetty 7350342440ecSPrasad Singamsetty rv = ddi_copyout(&evtoc, to, sizeof (struct extvtoc), mode); 7351342440ecSPrasad Singamsetty if (rv != 0) 7352342440ecSPrasad Singamsetty rv = EFAULT; 7353342440ecSPrasad Singamsetty 7354342440ecSPrasad Singamsetty return (rv); 7355342440ecSPrasad Singamsetty } 7356342440ecSPrasad Singamsetty 7357342440ecSPrasad Singamsetty static int 7358342440ecSPrasad Singamsetty vdc_set_extvtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 7359342440ecSPrasad Singamsetty { 7360342440ecSPrasad Singamsetty void *uvtoc; 7361342440ecSPrasad Singamsetty struct extvtoc evtoc; 7362342440ecSPrasad Singamsetty int i, rv; 7363342440ecSPrasad Singamsetty 7364342440ecSPrasad Singamsetty if ((from == NULL) || (to == NULL)) 7365342440ecSPrasad Singamsetty return (ENXIO); 7366342440ecSPrasad Singamsetty 7367342440ecSPrasad Singamsetty uvtoc = (dir == VD_COPYIN)? from : to; 7368342440ecSPrasad Singamsetty 7369342440ecSPrasad Singamsetty rv = ddi_copyin(uvtoc, &evtoc, sizeof (struct extvtoc), mode); 7370342440ecSPrasad Singamsetty if (rv != 0) 7371342440ecSPrasad Singamsetty return (EFAULT); 7372342440ecSPrasad Singamsetty 7373342440ecSPrasad Singamsetty if (dir == VD_COPYOUT) { 7374342440ecSPrasad Singamsetty /* 7375342440ecSPrasad Singamsetty * The disk label may have changed. Revalidate the disk 7376342440ecSPrasad Singamsetty * geometry. This will also update the device nodes. 7377342440ecSPrasad Singamsetty */ 7378342440ecSPrasad Singamsetty vdc_validate(vdc); 7379342440ecSPrasad Singamsetty 7380342440ecSPrasad Singamsetty /* 7381342440ecSPrasad Singamsetty * We also need to keep track of the timestamp fields. 7382342440ecSPrasad Singamsetty */ 7383342440ecSPrasad Singamsetty for (i = 0; i < V_NUMPAR; i++) { 7384342440ecSPrasad Singamsetty vdc->vtoc->timestamp[i] = evtoc.timestamp[i]; 7385342440ecSPrasad Singamsetty } 7386342440ecSPrasad Singamsetty 7387342440ecSPrasad Singamsetty } else { 7388342440ecSPrasad Singamsetty VTOC2VD_VTOC(&evtoc, (vd_vtoc_t *)to); 7389342440ecSPrasad Singamsetty } 73900a55fbb7Slm66018 73910a55fbb7Slm66018 return (0); 73920a55fbb7Slm66018 } 73930a55fbb7Slm66018 73940a55fbb7Slm66018 /* 73950a55fbb7Slm66018 * Function: 73960a55fbb7Slm66018 * vdc_get_geom_convert() 73970a55fbb7Slm66018 * 73980a55fbb7Slm66018 * Description: 7399d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCGGEOM, 7400d10e4ef2Snarayan * DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format 7401d10e4ef2Snarayan * defined in FWARC 2006/195 74020a55fbb7Slm66018 * 74030a55fbb7Slm66018 * Arguments: 7404d10e4ef2Snarayan * vdc - the vDisk client 74050a55fbb7Slm66018 * from - Buffer with data 74060a55fbb7Slm66018 * to - Buffer where data is to be copied to 74070a55fbb7Slm66018 * mode - flags passed to ioctl 74080a55fbb7Slm66018 * dir - direction of copy (in or out) 74090a55fbb7Slm66018 * 74100a55fbb7Slm66018 * Return Code: 74110a55fbb7Slm66018 * 0 - Success 74120a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 7413d10e4ef2Snarayan * EFAULT - ddi_copyout of data failed 74140a55fbb7Slm66018 */ 74150a55fbb7Slm66018 static int 7416d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 74170a55fbb7Slm66018 { 7418d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 7419d10e4ef2Snarayan 74200a55fbb7Slm66018 struct dk_geom geom; 74210a55fbb7Slm66018 int copy_len = sizeof (struct dk_geom); 74220a55fbb7Slm66018 int rv = 0; 74230a55fbb7Slm66018 74240a55fbb7Slm66018 if (dir != VD_COPYOUT) 74250a55fbb7Slm66018 return (0); /* nothing to do */ 74260a55fbb7Slm66018 74270a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 74280a55fbb7Slm66018 return (ENXIO); 74290a55fbb7Slm66018 74300a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom); 74310a55fbb7Slm66018 rv = ddi_copyout(&geom, to, copy_len, mode); 74320a55fbb7Slm66018 if (rv != 0) 74330a55fbb7Slm66018 rv = EFAULT; 74340a55fbb7Slm66018 74350a55fbb7Slm66018 return (rv); 74360a55fbb7Slm66018 } 74370a55fbb7Slm66018 74380a55fbb7Slm66018 /* 74390a55fbb7Slm66018 * Function: 74400a55fbb7Slm66018 * vdc_set_geom_convert() 74410a55fbb7Slm66018 * 74420a55fbb7Slm66018 * Description: 7443d10e4ef2Snarayan * This routine performs the necessary convertions from the DKIOCSGEOM 7444d10e4ef2Snarayan * Solaris structure to the format defined in FWARC 2006/195. 74450a55fbb7Slm66018 * 74460a55fbb7Slm66018 * Arguments: 7447d10e4ef2Snarayan * vdc - the vDisk client 74480a55fbb7Slm66018 * from - Buffer with data 74490a55fbb7Slm66018 * to - Buffer where data is to be copied to 74500a55fbb7Slm66018 * mode - flags passed to ioctl 74510a55fbb7Slm66018 * dir - direction of copy (in or out) 74520a55fbb7Slm66018 * 74530a55fbb7Slm66018 * Return Code: 74540a55fbb7Slm66018 * 0 - Success 74550a55fbb7Slm66018 * ENXIO - Invalid buffer passed in 74560a55fbb7Slm66018 * EFAULT - ddi_copyin of data failed 74570a55fbb7Slm66018 */ 74580a55fbb7Slm66018 static int 7459d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 74600a55fbb7Slm66018 { 7461d10e4ef2Snarayan _NOTE(ARGUNUSED(vdc)) 7462d10e4ef2Snarayan 74630a55fbb7Slm66018 vd_geom_t vdgeom; 74640a55fbb7Slm66018 void *tmp_mem = NULL; 74650a55fbb7Slm66018 int copy_len = sizeof (struct dk_geom); 74660a55fbb7Slm66018 int rv = 0; 74670a55fbb7Slm66018 74680a55fbb7Slm66018 if (dir != VD_COPYIN) 74690a55fbb7Slm66018 return (0); /* nothing to do */ 74700a55fbb7Slm66018 74710a55fbb7Slm66018 if ((from == NULL) || (to == NULL)) 74720a55fbb7Slm66018 return (ENXIO); 74730a55fbb7Slm66018 74740a55fbb7Slm66018 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 74750a55fbb7Slm66018 74760a55fbb7Slm66018 rv = ddi_copyin(from, tmp_mem, copy_len, mode); 74770a55fbb7Slm66018 if (rv != 0) { 74780a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 74790a55fbb7Slm66018 return (EFAULT); 74800a55fbb7Slm66018 } 74810a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom); 74820a55fbb7Slm66018 bcopy(&vdgeom, to, sizeof (vdgeom)); 74830a55fbb7Slm66018 kmem_free(tmp_mem, copy_len); 74840a55fbb7Slm66018 74850a55fbb7Slm66018 return (0); 74860a55fbb7Slm66018 } 74870a55fbb7Slm66018 74884bac2208Snarayan static int 74894bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 74904bac2208Snarayan { 74914bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 74924bac2208Snarayan 74934bac2208Snarayan vd_efi_t *vd_efi; 74944bac2208Snarayan dk_efi_t dk_efi; 74954bac2208Snarayan int rv = 0; 74964bac2208Snarayan void *uaddr; 74974bac2208Snarayan 74984bac2208Snarayan if ((from == NULL) || (to == NULL)) 74994bac2208Snarayan return (ENXIO); 75004bac2208Snarayan 75014bac2208Snarayan if (dir == VD_COPYIN) { 75024bac2208Snarayan 75034bac2208Snarayan vd_efi = (vd_efi_t *)to; 75044bac2208Snarayan 75054bac2208Snarayan rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode); 75064bac2208Snarayan if (rv != 0) 75074bac2208Snarayan return (EFAULT); 75084bac2208Snarayan 75094bac2208Snarayan vd_efi->lba = dk_efi.dki_lba; 75104bac2208Snarayan vd_efi->length = dk_efi.dki_length; 75114bac2208Snarayan bzero(vd_efi->data, vd_efi->length); 75124bac2208Snarayan 75134bac2208Snarayan } else { 75144bac2208Snarayan 75154bac2208Snarayan rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode); 75164bac2208Snarayan if (rv != 0) 75174bac2208Snarayan return (EFAULT); 75184bac2208Snarayan 75194bac2208Snarayan uaddr = dk_efi.dki_data; 75204bac2208Snarayan 75214bac2208Snarayan dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP); 75224bac2208Snarayan 75234bac2208Snarayan VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi); 75244bac2208Snarayan 75254bac2208Snarayan rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length, 75264bac2208Snarayan mode); 75274bac2208Snarayan if (rv != 0) 75284bac2208Snarayan return (EFAULT); 75294bac2208Snarayan 75304bac2208Snarayan kmem_free(dk_efi.dki_data, dk_efi.dki_length); 75314bac2208Snarayan } 75324bac2208Snarayan 75334bac2208Snarayan return (0); 75344bac2208Snarayan } 75354bac2208Snarayan 75364bac2208Snarayan static int 75374bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 75384bac2208Snarayan { 75394bac2208Snarayan _NOTE(ARGUNUSED(vdc)) 75404bac2208Snarayan 75414bac2208Snarayan dk_efi_t dk_efi; 75424bac2208Snarayan void *uaddr; 75434bac2208Snarayan 75442f5224aeSachartre if (dir == VD_COPYOUT) { 75452f5224aeSachartre /* 75462f5224aeSachartre * The disk label may have changed. Revalidate the disk 75475b98b509Sachartre * geometry. This will also update the device nodes. 75482f5224aeSachartre */ 75492f5224aeSachartre vdc_validate(vdc); 75502f5224aeSachartre return (0); 75512f5224aeSachartre } 75524bac2208Snarayan 75534bac2208Snarayan if ((from == NULL) || (to == NULL)) 75544bac2208Snarayan return (ENXIO); 75554bac2208Snarayan 75564bac2208Snarayan if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0) 75574bac2208Snarayan return (EFAULT); 75584bac2208Snarayan 75594bac2208Snarayan uaddr = dk_efi.dki_data; 75604bac2208Snarayan 75614bac2208Snarayan dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP); 75624bac2208Snarayan 75634bac2208Snarayan if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0) 75644bac2208Snarayan return (EFAULT); 75654bac2208Snarayan 75664bac2208Snarayan DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to); 75674bac2208Snarayan 75684bac2208Snarayan kmem_free(dk_efi.dki_data, dk_efi.dki_length); 75694bac2208Snarayan 75704bac2208Snarayan return (0); 75714bac2208Snarayan } 75724bac2208Snarayan 757317cadca8Slm66018 757417cadca8Slm66018 /* -------------------------------------------------------------------------- */ 757517cadca8Slm66018 75760a55fbb7Slm66018 /* 75770a55fbb7Slm66018 * Function: 75781ae08745Sheppo * vdc_create_fake_geometry() 75791ae08745Sheppo * 75801ae08745Sheppo * Description: 758117cadca8Slm66018 * This routine fakes up the disk info needed for some DKIO ioctls such 758217cadca8Slm66018 * as DKIOCINFO and DKIOCGMEDIAINFO [just like lofi(7D) and ramdisk(7D) do] 75831ae08745Sheppo * 758417cadca8Slm66018 * Note: This function must not be called until the vDisk attributes have 758517cadca8Slm66018 * been exchanged as part of the handshake with the vDisk server. 75861ae08745Sheppo * 75871ae08745Sheppo * Arguments: 75881ae08745Sheppo * vdc - soft state pointer for this instance of the device driver. 75891ae08745Sheppo * 75901ae08745Sheppo * Return Code: 759178fcd0a1Sachartre * none. 75921ae08745Sheppo */ 759378fcd0a1Sachartre static void 75941ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc) 75951ae08745Sheppo { 75961ae08745Sheppo ASSERT(vdc != NULL); 759778fcd0a1Sachartre ASSERT(vdc->max_xfer_sz != 0); 75980d0c8d4bSnarayan 75990d0c8d4bSnarayan /* 76001ae08745Sheppo * DKIOCINFO support 76011ae08745Sheppo */ 760278fcd0a1Sachartre if (vdc->cinfo == NULL) 76031ae08745Sheppo vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 76041ae08745Sheppo 76051ae08745Sheppo (void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME); 76061ae08745Sheppo (void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME); 76078e6a2a04Slm66018 /* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */ 76088e6a2a04Slm66018 vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz; 76092f5224aeSachartre 761087a7269eSachartre /* 76112f5224aeSachartre * We set the controller type to DKC_SCSI_CCS only if the VD_OP_SCSICMD 76122f5224aeSachartre * operation is supported, otherwise the controller type is DKC_DIRECT. 76132f5224aeSachartre * Version 1.0 does not support the VD_OP_SCSICMD operation, so the 76142f5224aeSachartre * controller type is always DKC_DIRECT in that case. 76152f5224aeSachartre * 761617cadca8Slm66018 * If the virtual disk is backed by a physical CD/DVD device or 761717cadca8Slm66018 * an ISO image, modify the controller type to indicate this 761887a7269eSachartre */ 761917cadca8Slm66018 switch (vdc->vdisk_media) { 762017cadca8Slm66018 case VD_MEDIA_CD: 762117cadca8Slm66018 case VD_MEDIA_DVD: 762217cadca8Slm66018 vdc->cinfo->dki_ctype = DKC_CDROM; 762317cadca8Slm66018 break; 762417cadca8Slm66018 case VD_MEDIA_FIXED: 76252f5224aeSachartre if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD)) 76262f5224aeSachartre vdc->cinfo->dki_ctype = DKC_SCSI_CCS; 76272f5224aeSachartre else 762887a7269eSachartre vdc->cinfo->dki_ctype = DKC_DIRECT; 762917cadca8Slm66018 break; 763017cadca8Slm66018 default: 763117cadca8Slm66018 /* in the case of v1.0 we default to a fixed disk */ 763217cadca8Slm66018 vdc->cinfo->dki_ctype = DKC_DIRECT; 763317cadca8Slm66018 break; 763417cadca8Slm66018 } 76351ae08745Sheppo vdc->cinfo->dki_flags = DKI_FMTVOL; 76361ae08745Sheppo vdc->cinfo->dki_cnum = 0; 76371ae08745Sheppo vdc->cinfo->dki_addr = 0; 76381ae08745Sheppo vdc->cinfo->dki_space = 0; 76391ae08745Sheppo vdc->cinfo->dki_prio = 0; 76401ae08745Sheppo vdc->cinfo->dki_vec = 0; 76411ae08745Sheppo vdc->cinfo->dki_unit = vdc->instance; 76421ae08745Sheppo vdc->cinfo->dki_slave = 0; 76431ae08745Sheppo /* 76441ae08745Sheppo * The partition number will be created on the fly depending on the 76451ae08745Sheppo * actual slice (i.e. minor node) that is used to request the data. 76461ae08745Sheppo */ 76471ae08745Sheppo vdc->cinfo->dki_partition = 0; 76481ae08745Sheppo 76491ae08745Sheppo /* 76501ae08745Sheppo * DKIOCGMEDIAINFO support 76511ae08745Sheppo */ 76520a55fbb7Slm66018 if (vdc->minfo == NULL) 76531ae08745Sheppo vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP); 765417cadca8Slm66018 765517cadca8Slm66018 if (vio_ver_is_supported(vdc->ver, 1, 1)) { 765617cadca8Slm66018 vdc->minfo->dki_media_type = 765717cadca8Slm66018 VD_MEDIATYPE2DK_MEDIATYPE(vdc->vdisk_media); 765817cadca8Slm66018 } else { 76591ae08745Sheppo vdc->minfo->dki_media_type = DK_FIXED_DISK; 766017cadca8Slm66018 } 766117cadca8Slm66018 76624bac2208Snarayan vdc->minfo->dki_capacity = vdc->vdisk_size; 766317cadca8Slm66018 vdc->minfo->dki_lbsize = vdc->block_size; 766478fcd0a1Sachartre } 76651ae08745Sheppo 766678fcd0a1Sachartre static ushort_t 766778fcd0a1Sachartre vdc_lbl2cksum(struct dk_label *label) 766878fcd0a1Sachartre { 766978fcd0a1Sachartre int count; 767078fcd0a1Sachartre ushort_t sum, *sp; 767178fcd0a1Sachartre 767278fcd0a1Sachartre count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 767378fcd0a1Sachartre sp = (ushort_t *)label; 767478fcd0a1Sachartre sum = 0; 767578fcd0a1Sachartre while (count--) { 767678fcd0a1Sachartre sum ^= *sp++; 767778fcd0a1Sachartre } 767878fcd0a1Sachartre 767978fcd0a1Sachartre return (sum); 76800a55fbb7Slm66018 } 76810a55fbb7Slm66018 7682de3a5331SRamesh Chitrothu static void 7683de3a5331SRamesh Chitrothu vdc_update_size(vdc_t *vdc, size_t dsk_size, size_t blk_size, size_t xfr_size) 7684de3a5331SRamesh Chitrothu { 7685de3a5331SRamesh Chitrothu vd_err_stats_t *stp; 7686de3a5331SRamesh Chitrothu 7687de3a5331SRamesh Chitrothu ASSERT(MUTEX_HELD(&vdc->lock)); 7688de3a5331SRamesh Chitrothu ASSERT(xfr_size != 0); 7689de3a5331SRamesh Chitrothu 7690de3a5331SRamesh Chitrothu /* 7691de3a5331SRamesh Chitrothu * If the disk size is unknown or sizes are unchanged then don't 7692de3a5331SRamesh Chitrothu * update anything. 7693de3a5331SRamesh Chitrothu */ 7694de3a5331SRamesh Chitrothu if (dsk_size == VD_SIZE_UNKNOWN || dsk_size == 0 || 7695de3a5331SRamesh Chitrothu (blk_size == vdc->block_size && dsk_size == vdc->vdisk_size && 7696de3a5331SRamesh Chitrothu xfr_size == vdc->max_xfer_sz)) 7697de3a5331SRamesh Chitrothu return; 7698de3a5331SRamesh Chitrothu 7699de3a5331SRamesh Chitrothu /* 7700de3a5331SRamesh Chitrothu * We don't know at compile time what the vDisk server will think 7701de3a5331SRamesh Chitrothu * are good values but we apply a large (arbitrary) upper bound to 7702de3a5331SRamesh Chitrothu * prevent memory exhaustion in vdc if it was allocating a DRing 7703de3a5331SRamesh Chitrothu * based of huge values sent by the server. We probably will never 7704de3a5331SRamesh Chitrothu * exceed this except if the message was garbage. 7705de3a5331SRamesh Chitrothu */ 7706de3a5331SRamesh Chitrothu if ((xfr_size * blk_size) > (PAGESIZE * DEV_BSIZE)) { 7707de3a5331SRamesh Chitrothu DMSG(vdc, 0, "[%d] vds block transfer size too big;" 7708de3a5331SRamesh Chitrothu " using max supported by vdc", vdc->instance); 7709de3a5331SRamesh Chitrothu xfr_size = maxphys / DEV_BSIZE; 7710de3a5331SRamesh Chitrothu dsk_size = (dsk_size * blk_size) / DEV_BSIZE; 7711de3a5331SRamesh Chitrothu blk_size = DEV_BSIZE; 7712de3a5331SRamesh Chitrothu } 7713de3a5331SRamesh Chitrothu 7714de3a5331SRamesh Chitrothu vdc->max_xfer_sz = xfr_size; 7715de3a5331SRamesh Chitrothu vdc->block_size = blk_size; 7716de3a5331SRamesh Chitrothu vdc->vdisk_size = dsk_size; 7717de3a5331SRamesh Chitrothu 7718de3a5331SRamesh Chitrothu stp = (vd_err_stats_t *)vdc->err_stats->ks_data; 7719de3a5331SRamesh Chitrothu stp->vd_capacity.value.ui64 = dsk_size * blk_size; 7720de3a5331SRamesh Chitrothu 7721de3a5331SRamesh Chitrothu vdc->minfo->dki_capacity = dsk_size; 7722de3a5331SRamesh Chitrothu vdc->minfo->dki_lbsize = (uint_t)blk_size; 7723de3a5331SRamesh Chitrothu } 7724de3a5331SRamesh Chitrothu 77250a55fbb7Slm66018 /* 77260a55fbb7Slm66018 * Function: 772778fcd0a1Sachartre * vdc_validate_geometry 77280a55fbb7Slm66018 * 77290a55fbb7Slm66018 * Description: 773078fcd0a1Sachartre * This routine discovers the label and geometry of the disk. It stores 773178fcd0a1Sachartre * the disk label and related information in the vdc structure. If it 773278fcd0a1Sachartre * fails to validate the geometry or to discover the disk label then 773378fcd0a1Sachartre * the label is marked as unknown (VD_DISK_LABEL_UNK). 77340a55fbb7Slm66018 * 77350a55fbb7Slm66018 * Arguments: 77360a55fbb7Slm66018 * vdc - soft state pointer for this instance of the device driver. 77370a55fbb7Slm66018 * 77380a55fbb7Slm66018 * Return Code: 773978fcd0a1Sachartre * 0 - success. 774078fcd0a1Sachartre * EINVAL - unknown disk label. 774178fcd0a1Sachartre * ENOTSUP - geometry not applicable (EFI label). 774278fcd0a1Sachartre * EIO - error accessing the disk. 77430a55fbb7Slm66018 */ 77440a55fbb7Slm66018 static int 774578fcd0a1Sachartre vdc_validate_geometry(vdc_t *vdc) 77460a55fbb7Slm66018 { 7747d10e4ef2Snarayan buf_t *buf; /* BREAD requests need to be in a buf_t structure */ 77480a55fbb7Slm66018 dev_t dev; 77492f5224aeSachartre int rv, rval; 775078fcd0a1Sachartre struct dk_label label; 775178fcd0a1Sachartre struct dk_geom geom; 7752342440ecSPrasad Singamsetty struct extvtoc vtoc; 7753edcc0754Sachartre efi_gpt_t *gpt; 7754edcc0754Sachartre efi_gpe_t *gpe; 7755edcc0754Sachartre vd_efi_dev_t edev; 77560a55fbb7Slm66018 77570a55fbb7Slm66018 ASSERT(vdc != NULL); 775878fcd0a1Sachartre ASSERT(vdc->vtoc != NULL && vdc->geom != NULL); 775978fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 77600a55fbb7Slm66018 776178fcd0a1Sachartre mutex_exit(&vdc->lock); 7762de3a5331SRamesh Chitrothu /* 7763de3a5331SRamesh Chitrothu * Check the disk capacity in case it has changed. If that fails then 7764de3a5331SRamesh Chitrothu * we proceed and we will be using the disk size we currently have. 7765de3a5331SRamesh Chitrothu */ 7766de3a5331SRamesh Chitrothu (void) vdc_check_capacity(vdc); 77670a55fbb7Slm66018 dev = makedevice(ddi_driver_major(vdc->dip), 77680a55fbb7Slm66018 VD_MAKE_DEV(vdc->instance, 0)); 77694bac2208Snarayan 77702f5224aeSachartre rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL, &rval); 777178fcd0a1Sachartre if (rv == 0) 7772342440ecSPrasad Singamsetty rv = vd_process_ioctl(dev, DKIOCGEXTVTOC, (caddr_t)&vtoc, 77732f5224aeSachartre FKIOCTL, &rval); 77740d0c8d4bSnarayan 77754bac2208Snarayan if (rv == ENOTSUP) { 77764bac2208Snarayan /* 77774bac2208Snarayan * If the device does not support VTOC then we try 77784bac2208Snarayan * to read an EFI label. 7779edcc0754Sachartre * 7780edcc0754Sachartre * We need to know the block size and the disk size to 7781edcc0754Sachartre * be able to read an EFI label. 77824bac2208Snarayan */ 7783edcc0754Sachartre if (vdc->vdisk_size == 0) { 7784edcc0754Sachartre mutex_enter(&vdc->lock); 7785edcc0754Sachartre vdc_store_label_unk(vdc); 7786de3a5331SRamesh Chitrothu return (EIO); 7787edcc0754Sachartre } 77884bac2208Snarayan 7789edcc0754Sachartre VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl); 7790edcc0754Sachartre 7791edcc0754Sachartre rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe); 77924bac2208Snarayan 77934bac2208Snarayan if (rv) { 77943af08d82Slm66018 DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)", 77954bac2208Snarayan vdc->instance, rv); 779678fcd0a1Sachartre mutex_enter(&vdc->lock); 779778fcd0a1Sachartre vdc_store_label_unk(vdc); 779878fcd0a1Sachartre return (EIO); 779978fcd0a1Sachartre } 780078fcd0a1Sachartre 780178fcd0a1Sachartre mutex_enter(&vdc->lock); 7802edcc0754Sachartre vdc_store_label_efi(vdc, gpt, gpe); 7803edcc0754Sachartre vd_efi_free(&edev, gpt, gpe); 780478fcd0a1Sachartre return (ENOTSUP); 780578fcd0a1Sachartre } 780678fcd0a1Sachartre 780778fcd0a1Sachartre if (rv != 0) { 780878fcd0a1Sachartre DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)", 780978fcd0a1Sachartre vdc->instance, rv); 781078fcd0a1Sachartre mutex_enter(&vdc->lock); 781178fcd0a1Sachartre vdc_store_label_unk(vdc); 781278fcd0a1Sachartre if (rv != EINVAL) 781378fcd0a1Sachartre rv = EIO; 78144bac2208Snarayan return (rv); 78154bac2208Snarayan } 78164bac2208Snarayan 781778fcd0a1Sachartre /* check that geometry and vtoc are valid */ 781878fcd0a1Sachartre if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 || 781978fcd0a1Sachartre vtoc.v_sanity != VTOC_SANE) { 782078fcd0a1Sachartre mutex_enter(&vdc->lock); 782178fcd0a1Sachartre vdc_store_label_unk(vdc); 782278fcd0a1Sachartre return (EINVAL); 782378fcd0a1Sachartre } 78244bac2208Snarayan 782578fcd0a1Sachartre /* 782678fcd0a1Sachartre * We have a disk and a valid VTOC. However this does not mean 782778fcd0a1Sachartre * that the disk currently have a VTOC label. The returned VTOC may 782878fcd0a1Sachartre * be a default VTOC to be used for configuring the disk (this is 782978fcd0a1Sachartre * what is done for disk image). So we read the label from the 783078fcd0a1Sachartre * beginning of the disk to ensure we really have a VTOC label. 783178fcd0a1Sachartre * 783278fcd0a1Sachartre * FUTURE: This could be the default way for reading the VTOC 783378fcd0a1Sachartre * from the disk as opposed to sending the VD_OP_GET_VTOC 783478fcd0a1Sachartre * to the server. This will be the default if vdc is implemented 783578fcd0a1Sachartre * ontop of cmlb. 783678fcd0a1Sachartre */ 783778fcd0a1Sachartre 783878fcd0a1Sachartre /* 783978fcd0a1Sachartre * Single slice disk does not support read using an absolute disk 784078fcd0a1Sachartre * offset so we just rely on the DKIOCGVTOC ioctl in that case. 784178fcd0a1Sachartre */ 784278fcd0a1Sachartre if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) { 784378fcd0a1Sachartre mutex_enter(&vdc->lock); 784478fcd0a1Sachartre if (vtoc.v_nparts != 1) { 784578fcd0a1Sachartre vdc_store_label_unk(vdc); 784678fcd0a1Sachartre return (EINVAL); 784778fcd0a1Sachartre } 784878fcd0a1Sachartre vdc_store_label_vtoc(vdc, &geom, &vtoc); 78494bac2208Snarayan return (0); 78504bac2208Snarayan } 78514bac2208Snarayan 785278fcd0a1Sachartre if (vtoc.v_nparts != V_NUMPAR) { 785378fcd0a1Sachartre mutex_enter(&vdc->lock); 785478fcd0a1Sachartre vdc_store_label_unk(vdc); 785578fcd0a1Sachartre return (EINVAL); 78560a55fbb7Slm66018 } 7857d10e4ef2Snarayan 7858d10e4ef2Snarayan /* 7859d10e4ef2Snarayan * Read disk label from start of disk 7860d10e4ef2Snarayan */ 7861d10e4ef2Snarayan buf = kmem_alloc(sizeof (buf_t), KM_SLEEP); 7862d10e4ef2Snarayan bioinit(buf); 786378fcd0a1Sachartre buf->b_un.b_addr = (caddr_t)&label; 7864d10e4ef2Snarayan buf->b_bcount = DK_LABEL_SIZE; 7865d10e4ef2Snarayan buf->b_flags = B_BUSY | B_READ; 786617cadca8Slm66018 buf->b_dev = cmpdev(dev); 786778fcd0a1Sachartre rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)&label, 786878fcd0a1Sachartre DK_LABEL_SIZE, VD_SLICE_NONE, 0, CB_STRATEGY, buf, VIO_read_dir); 78693af08d82Slm66018 if (rv) { 78703af08d82Slm66018 DMSG(vdc, 1, "[%d] Failed to read disk block 0\n", 78713af08d82Slm66018 vdc->instance); 7872*11f54b6eSAlexandre Chartre } else if (ddi_in_panic()) { 7873*11f54b6eSAlexandre Chartre rv = vdc_drain_response(vdc, CB_STRATEGY, buf); 7874*11f54b6eSAlexandre Chartre if (rv == 0) { 7875*11f54b6eSAlexandre Chartre rv = geterror(buf); 7876*11f54b6eSAlexandre Chartre } 787778fcd0a1Sachartre } else { 7878d10e4ef2Snarayan rv = biowait(buf); 787978fcd0a1Sachartre } 7880*11f54b6eSAlexandre Chartre biofini(buf); 7881d10e4ef2Snarayan kmem_free(buf, sizeof (buf_t)); 78820a55fbb7Slm66018 788378fcd0a1Sachartre if (rv != 0 || label.dkl_magic != DKL_MAGIC || 788478fcd0a1Sachartre label.dkl_cksum != vdc_lbl2cksum(&label)) { 788578fcd0a1Sachartre DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n", 788678fcd0a1Sachartre vdc->instance); 788778fcd0a1Sachartre mutex_enter(&vdc->lock); 788878fcd0a1Sachartre vdc_store_label_unk(vdc); 788978fcd0a1Sachartre return (EINVAL); 789078fcd0a1Sachartre } 789178fcd0a1Sachartre 789278fcd0a1Sachartre mutex_enter(&vdc->lock); 789378fcd0a1Sachartre vdc_store_label_vtoc(vdc, &geom, &vtoc); 789478fcd0a1Sachartre return (0); 789578fcd0a1Sachartre } 789678fcd0a1Sachartre 789778fcd0a1Sachartre /* 789878fcd0a1Sachartre * Function: 789978fcd0a1Sachartre * vdc_validate 790078fcd0a1Sachartre * 790178fcd0a1Sachartre * Description: 790278fcd0a1Sachartre * This routine discovers the label of the disk and create the 790378fcd0a1Sachartre * appropriate device nodes if the label has changed. 790478fcd0a1Sachartre * 790578fcd0a1Sachartre * Arguments: 790678fcd0a1Sachartre * vdc - soft state pointer for this instance of the device driver. 790778fcd0a1Sachartre * 790878fcd0a1Sachartre * Return Code: 790978fcd0a1Sachartre * none. 791078fcd0a1Sachartre */ 791178fcd0a1Sachartre static void 791278fcd0a1Sachartre vdc_validate(vdc_t *vdc) 791378fcd0a1Sachartre { 791478fcd0a1Sachartre vd_disk_label_t old_label; 7915edcc0754Sachartre vd_slice_t old_slice[V_NUMPAR]; 791678fcd0a1Sachartre int rv; 791778fcd0a1Sachartre 791878fcd0a1Sachartre ASSERT(!MUTEX_HELD(&vdc->lock)); 791978fcd0a1Sachartre 792078fcd0a1Sachartre mutex_enter(&vdc->lock); 792178fcd0a1Sachartre 792278fcd0a1Sachartre /* save the current label and vtoc */ 792378fcd0a1Sachartre old_label = vdc->vdisk_label; 7924edcc0754Sachartre bcopy(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR); 792578fcd0a1Sachartre 792678fcd0a1Sachartre /* check the geometry */ 792778fcd0a1Sachartre (void) vdc_validate_geometry(vdc); 792878fcd0a1Sachartre 792978fcd0a1Sachartre /* if the disk label has changed, update device nodes */ 793078fcd0a1Sachartre if (vdc->vdisk_label != old_label) { 793178fcd0a1Sachartre 793278fcd0a1Sachartre if (vdc->vdisk_label == VD_DISK_LABEL_EFI) 793378fcd0a1Sachartre rv = vdc_create_device_nodes_efi(vdc); 793478fcd0a1Sachartre else 793578fcd0a1Sachartre rv = vdc_create_device_nodes_vtoc(vdc); 793678fcd0a1Sachartre 793778fcd0a1Sachartre if (rv != 0) { 793878fcd0a1Sachartre DMSG(vdc, 0, "![%d] Failed to update device nodes", 793978fcd0a1Sachartre vdc->instance); 794078fcd0a1Sachartre } 794178fcd0a1Sachartre } 794278fcd0a1Sachartre 794378fcd0a1Sachartre mutex_exit(&vdc->lock); 794478fcd0a1Sachartre } 794578fcd0a1Sachartre 794678fcd0a1Sachartre static void 794778fcd0a1Sachartre vdc_validate_task(void *arg) 794878fcd0a1Sachartre { 794978fcd0a1Sachartre vdc_t *vdc = (vdc_t *)arg; 795078fcd0a1Sachartre 795178fcd0a1Sachartre vdc_validate(vdc); 795278fcd0a1Sachartre 795378fcd0a1Sachartre mutex_enter(&vdc->lock); 795478fcd0a1Sachartre ASSERT(vdc->validate_pending > 0); 795578fcd0a1Sachartre vdc->validate_pending--; 795678fcd0a1Sachartre mutex_exit(&vdc->lock); 79571ae08745Sheppo } 79584bac2208Snarayan 79594bac2208Snarayan /* 79604bac2208Snarayan * Function: 79614bac2208Snarayan * vdc_setup_devid() 79624bac2208Snarayan * 79634bac2208Snarayan * Description: 79644bac2208Snarayan * This routine discovers the devid of a vDisk. It requests the devid of 79654bac2208Snarayan * the underlying device from the vDisk server, builds an encapsulated 79664bac2208Snarayan * devid based on the retrieved devid and registers that new devid to 79674bac2208Snarayan * the vDisk. 79684bac2208Snarayan * 79694bac2208Snarayan * Arguments: 79704bac2208Snarayan * vdc - soft state pointer for this instance of the device driver. 79714bac2208Snarayan * 79724bac2208Snarayan * Return Code: 79734bac2208Snarayan * 0 - A devid was succesfully registered for the vDisk 79744bac2208Snarayan */ 79754bac2208Snarayan static int 79764bac2208Snarayan vdc_setup_devid(vdc_t *vdc) 79774bac2208Snarayan { 79784bac2208Snarayan int rv; 79794bac2208Snarayan vd_devid_t *vd_devid; 79804bac2208Snarayan size_t bufsize, bufid_len; 79814bac2208Snarayan 79824bac2208Snarayan /* 79834bac2208Snarayan * At first sight, we don't know the size of the devid that the 79844bac2208Snarayan * server will return but this size will be encoded into the 79854bac2208Snarayan * reply. So we do a first request using a default size then we 79864bac2208Snarayan * check if this size was large enough. If not then we do a second 79874bac2208Snarayan * request with the correct size returned by the server. Note that 79884bac2208Snarayan * ldc requires size to be 8-byte aligned. 79894bac2208Snarayan */ 79904bac2208Snarayan bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN), 79914bac2208Snarayan sizeof (uint64_t)); 79924bac2208Snarayan vd_devid = kmem_zalloc(bufsize, KM_SLEEP); 79934bac2208Snarayan bufid_len = bufsize - sizeof (vd_efi_t) - 1; 79944bac2208Snarayan 79953af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid, 79962f5224aeSachartre bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir, B_TRUE); 79973af08d82Slm66018 79983af08d82Slm66018 DMSG(vdc, 2, "sync_op returned %d\n", rv); 79993af08d82Slm66018 80004bac2208Snarayan if (rv) { 80014bac2208Snarayan kmem_free(vd_devid, bufsize); 80024bac2208Snarayan return (rv); 80034bac2208Snarayan } 80044bac2208Snarayan 80054bac2208Snarayan if (vd_devid->length > bufid_len) { 80064bac2208Snarayan /* 80074bac2208Snarayan * The returned devid is larger than the buffer used. Try again 80084bac2208Snarayan * with a buffer with the right size. 80094bac2208Snarayan */ 80104bac2208Snarayan kmem_free(vd_devid, bufsize); 80114bac2208Snarayan bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length), 80124bac2208Snarayan sizeof (uint64_t)); 80134bac2208Snarayan vd_devid = kmem_zalloc(bufsize, KM_SLEEP); 80144bac2208Snarayan bufid_len = bufsize - sizeof (vd_efi_t) - 1; 80154bac2208Snarayan 80163af08d82Slm66018 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, 80173af08d82Slm66018 (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0, 80182f5224aeSachartre VIO_both_dir, B_TRUE); 80193af08d82Slm66018 80204bac2208Snarayan if (rv) { 80214bac2208Snarayan kmem_free(vd_devid, bufsize); 80224bac2208Snarayan return (rv); 80234bac2208Snarayan } 80244bac2208Snarayan } 80254bac2208Snarayan 80264bac2208Snarayan /* 80274bac2208Snarayan * The virtual disk should have the same device id as the one associated 80284bac2208Snarayan * with the physical disk it is mapped on, otherwise sharing a disk 80294bac2208Snarayan * between a LDom and a non-LDom may not work (for example for a shared 80304bac2208Snarayan * SVM disk set). 80314bac2208Snarayan * 80324bac2208Snarayan * The DDI framework does not allow creating a device id with any 80334bac2208Snarayan * type so we first create a device id of type DEVID_ENCAP and then 80344bac2208Snarayan * we restore the orignal type of the physical device. 80354bac2208Snarayan */ 80364bac2208Snarayan 80373af08d82Slm66018 DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length); 80383af08d82Slm66018 80394bac2208Snarayan /* build an encapsulated devid based on the returned devid */ 80404bac2208Snarayan if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length, 80414bac2208Snarayan vd_devid->id, &vdc->devid) != DDI_SUCCESS) { 80423af08d82Slm66018 DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance); 80434bac2208Snarayan kmem_free(vd_devid, bufsize); 80444bac2208Snarayan return (1); 80454bac2208Snarayan } 80464bac2208Snarayan 80474bac2208Snarayan DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type); 80484bac2208Snarayan 80494bac2208Snarayan ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS); 80504bac2208Snarayan 80514bac2208Snarayan kmem_free(vd_devid, bufsize); 80524bac2208Snarayan 80534bac2208Snarayan if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) { 80543af08d82Slm66018 DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance); 80554bac2208Snarayan return (1); 80564bac2208Snarayan } 80574bac2208Snarayan 80584bac2208Snarayan return (0); 80594bac2208Snarayan } 80604bac2208Snarayan 80614bac2208Snarayan static void 8062edcc0754Sachartre vdc_store_label_efi(vdc_t *vdc, efi_gpt_t *gpt, efi_gpe_t *gpe) 80634bac2208Snarayan { 8064edcc0754Sachartre int i, nparts; 80654bac2208Snarayan 806678fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 806778fcd0a1Sachartre 806878fcd0a1Sachartre vdc->vdisk_label = VD_DISK_LABEL_EFI; 8069342440ecSPrasad Singamsetty bzero(vdc->vtoc, sizeof (struct extvtoc)); 807078fcd0a1Sachartre bzero(vdc->geom, sizeof (struct dk_geom)); 8071edcc0754Sachartre bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR); 8072edcc0754Sachartre 8073edcc0754Sachartre nparts = gpt->efi_gpt_NumberOfPartitionEntries; 8074edcc0754Sachartre 8075edcc0754Sachartre for (i = 0; i < nparts && i < VD_EFI_WD_SLICE; i++) { 8076edcc0754Sachartre 8077edcc0754Sachartre if (gpe[i].efi_gpe_StartingLBA == 0 || 8078edcc0754Sachartre gpe[i].efi_gpe_EndingLBA == 0) { 8079edcc0754Sachartre continue; 80804bac2208Snarayan } 8081edcc0754Sachartre 8082edcc0754Sachartre vdc->slice[i].start = gpe[i].efi_gpe_StartingLBA; 8083edcc0754Sachartre vdc->slice[i].nblocks = gpe[i].efi_gpe_EndingLBA - 8084edcc0754Sachartre gpe[i].efi_gpe_StartingLBA + 1; 8085edcc0754Sachartre } 8086edcc0754Sachartre 8087edcc0754Sachartre ASSERT(vdc->vdisk_size != 0); 8088edcc0754Sachartre vdc->slice[VD_EFI_WD_SLICE].start = 0; 8089edcc0754Sachartre vdc->slice[VD_EFI_WD_SLICE].nblocks = vdc->vdisk_size; 8090edcc0754Sachartre 80914bac2208Snarayan } 809278fcd0a1Sachartre 809378fcd0a1Sachartre static void 8094342440ecSPrasad Singamsetty vdc_store_label_vtoc(vdc_t *vdc, struct dk_geom *geom, struct extvtoc *vtoc) 809578fcd0a1Sachartre { 8096edcc0754Sachartre int i; 8097edcc0754Sachartre 809878fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 8099edcc0754Sachartre ASSERT(vdc->block_size == vtoc->v_sectorsz); 810078fcd0a1Sachartre 810178fcd0a1Sachartre vdc->vdisk_label = VD_DISK_LABEL_VTOC; 8102342440ecSPrasad Singamsetty bcopy(vtoc, vdc->vtoc, sizeof (struct extvtoc)); 810378fcd0a1Sachartre bcopy(geom, vdc->geom, sizeof (struct dk_geom)); 8104edcc0754Sachartre bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR); 8105edcc0754Sachartre 8106edcc0754Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 8107edcc0754Sachartre vdc->slice[i].start = vtoc->v_part[i].p_start; 8108edcc0754Sachartre vdc->slice[i].nblocks = vtoc->v_part[i].p_size; 8109edcc0754Sachartre } 811078fcd0a1Sachartre } 811178fcd0a1Sachartre 811278fcd0a1Sachartre static void 811378fcd0a1Sachartre vdc_store_label_unk(vdc_t *vdc) 811478fcd0a1Sachartre { 811578fcd0a1Sachartre ASSERT(MUTEX_HELD(&vdc->lock)); 811678fcd0a1Sachartre 811778fcd0a1Sachartre vdc->vdisk_label = VD_DISK_LABEL_UNK; 8118342440ecSPrasad Singamsetty bzero(vdc->vtoc, sizeof (struct extvtoc)); 811978fcd0a1Sachartre bzero(vdc->geom, sizeof (struct dk_geom)); 8120edcc0754Sachartre bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR); 812178fcd0a1Sachartre } 8122