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 /* 233c96341aSnarayan * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 241ae08745Sheppo * Use is subject to license terms. 251ae08745Sheppo */ 261ae08745Sheppo 271ae08745Sheppo #pragma ident "%Z%%M% %I% %E% SMI" 281ae08745Sheppo 291ae08745Sheppo /* 301ae08745Sheppo * Virtual disk server 311ae08745Sheppo */ 321ae08745Sheppo 331ae08745Sheppo 341ae08745Sheppo #include <sys/types.h> 351ae08745Sheppo #include <sys/conf.h> 364bac2208Snarayan #include <sys/crc32.h> 371ae08745Sheppo #include <sys/ddi.h> 381ae08745Sheppo #include <sys/dkio.h> 391ae08745Sheppo #include <sys/file.h> 401ae08745Sheppo #include <sys/mdeg.h> 411ae08745Sheppo #include <sys/modhash.h> 421ae08745Sheppo #include <sys/note.h> 431ae08745Sheppo #include <sys/pathname.h> 441ae08745Sheppo #include <sys/sunddi.h> 451ae08745Sheppo #include <sys/sunldi.h> 461ae08745Sheppo #include <sys/sysmacros.h> 471ae08745Sheppo #include <sys/vio_common.h> 481ae08745Sheppo #include <sys/vdsk_mailbox.h> 491ae08745Sheppo #include <sys/vdsk_common.h> 501ae08745Sheppo #include <sys/vtoc.h> 513c96341aSnarayan #include <sys/vfs.h> 523c96341aSnarayan #include <sys/stat.h> 53*690555a1Sachartre #include <vm/seg_map.h> 541ae08745Sheppo 551ae08745Sheppo /* Virtual disk server initialization flags */ 56d10e4ef2Snarayan #define VDS_LDI 0x01 57d10e4ef2Snarayan #define VDS_MDEG 0x02 581ae08745Sheppo 591ae08745Sheppo /* Virtual disk server tunable parameters */ 603c96341aSnarayan #define VDS_RETRIES 5 613c96341aSnarayan #define VDS_LDC_DELAY 1000 /* 1 msecs */ 623c96341aSnarayan #define VDS_DEV_DELAY 10000000 /* 10 secs */ 631ae08745Sheppo #define VDS_NCHAINS 32 641ae08745Sheppo 651ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */ 661ae08745Sheppo #define VDS_NAME "virtual-disk-server" 671ae08745Sheppo 681ae08745Sheppo #define VD_NAME "vd" 691ae08745Sheppo #define VD_VOLUME_NAME "vdisk" 701ae08745Sheppo #define VD_ASCIILABEL "Virtual Disk" 711ae08745Sheppo 721ae08745Sheppo #define VD_CHANNEL_ENDPOINT "channel-endpoint" 731ae08745Sheppo #define VD_ID_PROP "id" 741ae08745Sheppo #define VD_BLOCK_DEVICE_PROP "vds-block-device" 75445b4c2eSsb155480 #define VD_REG_PROP "reg" 761ae08745Sheppo 771ae08745Sheppo /* Virtual disk initialization flags */ 783c96341aSnarayan #define VD_DISK_READY 0x01 793c96341aSnarayan #define VD_LOCKING 0x02 803c96341aSnarayan #define VD_LDC 0x04 813c96341aSnarayan #define VD_DRING 0x08 823c96341aSnarayan #define VD_SID 0x10 833c96341aSnarayan #define VD_SEQ_NUM 0x20 841ae08745Sheppo 851ae08745Sheppo /* Flags for opening/closing backing devices via LDI */ 861ae08745Sheppo #define VD_OPEN_FLAGS (FEXCL | FREAD | FWRITE) 871ae08745Sheppo 881ae08745Sheppo /* 891ae08745Sheppo * By Solaris convention, slice/partition 2 represents the entire disk; 901ae08745Sheppo * unfortunately, this convention does not appear to be codified. 911ae08745Sheppo */ 921ae08745Sheppo #define VD_ENTIRE_DISK_SLICE 2 931ae08745Sheppo 941ae08745Sheppo /* Return a cpp token as a string */ 951ae08745Sheppo #define STRINGIZE(token) #token 961ae08745Sheppo 971ae08745Sheppo /* 981ae08745Sheppo * Print a message prefixed with the current function name to the message log 991ae08745Sheppo * (and optionally to the console for verbose boots); these macros use cpp's 1001ae08745Sheppo * concatenation of string literals and C99 variable-length-argument-list 1011ae08745Sheppo * macros 1021ae08745Sheppo */ 1031ae08745Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 1041ae08745Sheppo #define _PRN(format, ...) \ 1051ae08745Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 1061ae08745Sheppo 1071ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */ 1081ae08745Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 1091ae08745Sheppo (vd->dring + (i)*vd->descriptor_size)) 1101ae08745Sheppo 1111ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */ 1121ae08745Sheppo #define VD_CLIENT(vd) \ 1131ae08745Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 1141ae08745Sheppo (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" : \ 1151ae08745Sheppo (((vd)->xfer_mode == 0) ? "null client" : \ 1161ae08745Sheppo "unsupported client"))) 1171ae08745Sheppo 118*690555a1Sachartre /* For IO to raw disk on file */ 119*690555a1Sachartre #define VD_FILE_SLICE_NONE -1 120*690555a1Sachartre 121*690555a1Sachartre /* Read disk label from a disk on file */ 122*690555a1Sachartre #define VD_FILE_LABEL_READ(vd, labelp) \ 123*690555a1Sachartre vd_file_rw(vd, VD_FILE_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \ 124*690555a1Sachartre 0, sizeof (struct dk_label)) 125*690555a1Sachartre 126*690555a1Sachartre /* Write disk label to a disk on file */ 127*690555a1Sachartre #define VD_FILE_LABEL_WRITE(vd, labelp) \ 128*690555a1Sachartre vd_file_rw(vd, VD_FILE_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \ 129*690555a1Sachartre 0, sizeof (struct dk_label)) 130*690555a1Sachartre 131445b4c2eSsb155480 /* 132445b4c2eSsb155480 * Specification of an MD node passed to the MDEG to filter any 133445b4c2eSsb155480 * 'vport' nodes that do not belong to the specified node. This 134445b4c2eSsb155480 * template is copied for each vds instance and filled in with 135445b4c2eSsb155480 * the appropriate 'cfg-handle' value before being passed to the MDEG. 136445b4c2eSsb155480 */ 137445b4c2eSsb155480 static mdeg_prop_spec_t vds_prop_template[] = { 138445b4c2eSsb155480 { MDET_PROP_STR, "name", VDS_NAME }, 139445b4c2eSsb155480 { MDET_PROP_VAL, "cfg-handle", NULL }, 140445b4c2eSsb155480 { MDET_LIST_END, NULL, NULL } 141445b4c2eSsb155480 }; 142445b4c2eSsb155480 143445b4c2eSsb155480 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val); 144445b4c2eSsb155480 145445b4c2eSsb155480 /* 146445b4c2eSsb155480 * Matching criteria passed to the MDEG to register interest 147445b4c2eSsb155480 * in changes to 'virtual-device-port' nodes identified by their 148445b4c2eSsb155480 * 'id' property. 149445b4c2eSsb155480 */ 150445b4c2eSsb155480 static md_prop_match_t vd_prop_match[] = { 151445b4c2eSsb155480 { MDET_PROP_VAL, VD_ID_PROP }, 152445b4c2eSsb155480 { MDET_LIST_END, NULL } 153445b4c2eSsb155480 }; 154445b4c2eSsb155480 155445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port", 156445b4c2eSsb155480 vd_prop_match}; 157445b4c2eSsb155480 1581ae08745Sheppo /* Debugging macros */ 1591ae08745Sheppo #ifdef DEBUG 1603af08d82Slm66018 1613af08d82Slm66018 static int vd_msglevel = 0; 1623af08d82Slm66018 1631ae08745Sheppo #define PR0 if (vd_msglevel > 0) PRN 1641ae08745Sheppo #define PR1 if (vd_msglevel > 1) PRN 1651ae08745Sheppo #define PR2 if (vd_msglevel > 2) PRN 1661ae08745Sheppo 1671ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) \ 1683c96341aSnarayan PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 1691ae08745Sheppo elem->hdr.dstate, \ 1701ae08745Sheppo elem->payload.operation, \ 1711ae08745Sheppo elem->payload.status, \ 1721ae08745Sheppo elem->payload.nbytes, \ 1731ae08745Sheppo elem->payload.addr, \ 1741ae08745Sheppo elem->payload.ncookies); 1751ae08745Sheppo 1763af08d82Slm66018 char * 1773af08d82Slm66018 vd_decode_state(int state) 1783af08d82Slm66018 { 1793af08d82Slm66018 char *str; 1803af08d82Slm66018 1813af08d82Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break; 1823af08d82Slm66018 1833af08d82Slm66018 switch (state) { 1843af08d82Slm66018 CASE_STATE(VD_STATE_INIT) 1853af08d82Slm66018 CASE_STATE(VD_STATE_VER) 1863af08d82Slm66018 CASE_STATE(VD_STATE_ATTR) 1873af08d82Slm66018 CASE_STATE(VD_STATE_DRING) 1883af08d82Slm66018 CASE_STATE(VD_STATE_RDX) 1893af08d82Slm66018 CASE_STATE(VD_STATE_DATA) 1903af08d82Slm66018 default: str = "unknown"; break; 1913af08d82Slm66018 } 1923af08d82Slm66018 1933af08d82Slm66018 #undef CASE_STATE 1943af08d82Slm66018 1953af08d82Slm66018 return (str); 1963af08d82Slm66018 } 1973af08d82Slm66018 1983af08d82Slm66018 void 1993af08d82Slm66018 vd_decode_tag(vio_msg_t *msg) 2003af08d82Slm66018 { 2013af08d82Slm66018 char *tstr, *sstr, *estr; 2023af08d82Slm66018 2033af08d82Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 2043af08d82Slm66018 2053af08d82Slm66018 switch (msg->tag.vio_msgtype) { 2063af08d82Slm66018 CASE_TYPE(VIO_TYPE_CTRL) 2073af08d82Slm66018 CASE_TYPE(VIO_TYPE_DATA) 2083af08d82Slm66018 CASE_TYPE(VIO_TYPE_ERR) 2093af08d82Slm66018 default: tstr = "unknown"; break; 2103af08d82Slm66018 } 2113af08d82Slm66018 2123af08d82Slm66018 #undef CASE_TYPE 2133af08d82Slm66018 2143af08d82Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 2153af08d82Slm66018 2163af08d82Slm66018 switch (msg->tag.vio_subtype) { 2173af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 2183af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 2193af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 2203af08d82Slm66018 default: sstr = "unknown"; break; 2213af08d82Slm66018 } 2223af08d82Slm66018 2233af08d82Slm66018 #undef CASE_SUBTYPE 2243af08d82Slm66018 2253af08d82Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break; 2263af08d82Slm66018 2273af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 2283af08d82Slm66018 CASE_ENV(VIO_VER_INFO) 2293af08d82Slm66018 CASE_ENV(VIO_ATTR_INFO) 2303af08d82Slm66018 CASE_ENV(VIO_DRING_REG) 2313af08d82Slm66018 CASE_ENV(VIO_DRING_UNREG) 2323af08d82Slm66018 CASE_ENV(VIO_RDX) 2333af08d82Slm66018 CASE_ENV(VIO_PKT_DATA) 2343af08d82Slm66018 CASE_ENV(VIO_DESC_DATA) 2353af08d82Slm66018 CASE_ENV(VIO_DRING_DATA) 2363af08d82Slm66018 default: estr = "unknown"; break; 2373af08d82Slm66018 } 2383af08d82Slm66018 2393af08d82Slm66018 #undef CASE_ENV 2403af08d82Slm66018 2413af08d82Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)", 2423af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 2433af08d82Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr); 2443af08d82Slm66018 } 2453af08d82Slm66018 2461ae08745Sheppo #else /* !DEBUG */ 2473af08d82Slm66018 2481ae08745Sheppo #define PR0(...) 2491ae08745Sheppo #define PR1(...) 2501ae08745Sheppo #define PR2(...) 2511ae08745Sheppo 2521ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) 2531ae08745Sheppo 2543af08d82Slm66018 #define vd_decode_state(_s) (NULL) 2553af08d82Slm66018 #define vd_decode_tag(_s) (NULL) 2563af08d82Slm66018 2571ae08745Sheppo #endif /* DEBUG */ 2581ae08745Sheppo 2591ae08745Sheppo 260d10e4ef2Snarayan /* 261d10e4ef2Snarayan * Soft state structure for a vds instance 262d10e4ef2Snarayan */ 2631ae08745Sheppo typedef struct vds { 2641ae08745Sheppo uint_t initialized; /* driver inst initialization flags */ 2651ae08745Sheppo dev_info_t *dip; /* driver inst devinfo pointer */ 2661ae08745Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 2671ae08745Sheppo mod_hash_t *vd_table; /* table of virtual disks served */ 268445b4c2eSsb155480 mdeg_node_spec_t *ispecp; /* mdeg node specification */ 2691ae08745Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */ 2701ae08745Sheppo } vds_t; 2711ae08745Sheppo 272d10e4ef2Snarayan /* 273d10e4ef2Snarayan * Types of descriptor-processing tasks 274d10e4ef2Snarayan */ 275d10e4ef2Snarayan typedef enum vd_task_type { 276d10e4ef2Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 277d10e4ef2Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 278d10e4ef2Snarayan } vd_task_type_t; 279d10e4ef2Snarayan 280d10e4ef2Snarayan /* 281d10e4ef2Snarayan * Structure describing the task for processing a descriptor 282d10e4ef2Snarayan */ 283d10e4ef2Snarayan typedef struct vd_task { 284d10e4ef2Snarayan struct vd *vd; /* vd instance task is for */ 285d10e4ef2Snarayan vd_task_type_t type; /* type of descriptor task */ 286d10e4ef2Snarayan int index; /* dring elem index for task */ 287d10e4ef2Snarayan vio_msg_t *msg; /* VIO message task is for */ 288d10e4ef2Snarayan size_t msglen; /* length of message content */ 289d10e4ef2Snarayan vd_dring_payload_t *request; /* request task will perform */ 290d10e4ef2Snarayan struct buf buf; /* buf(9s) for I/O request */ 2914bac2208Snarayan ldc_mem_handle_t mhdl; /* task memory handle */ 292d10e4ef2Snarayan } vd_task_t; 293d10e4ef2Snarayan 294d10e4ef2Snarayan /* 295d10e4ef2Snarayan * Soft state structure for a virtual disk instance 296d10e4ef2Snarayan */ 2971ae08745Sheppo typedef struct vd { 2981ae08745Sheppo uint_t initialized; /* vdisk initialization flags */ 2991ae08745Sheppo vds_t *vds; /* server for this vdisk */ 300d10e4ef2Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */ 301d10e4ef2Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */ 3021ae08745Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 3033c96341aSnarayan char device_path[MAXPATHLEN + 1]; /* vdisk device */ 3041ae08745Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 305e1ebb9ecSlm66018 uint_t nslices; /* number of slices */ 3061ae08745Sheppo size_t vdisk_size; /* number of blocks in vdisk */ 3071ae08745Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */ 3084bac2208Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 309e1ebb9ecSlm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 3101ae08745Sheppo boolean_t pseudo; /* underlying pseudo dev */ 3113c96341aSnarayan boolean_t file; /* underlying file */ 3123c96341aSnarayan vnode_t *file_vnode; /* file vnode */ 3133c96341aSnarayan size_t file_size; /* file size */ 3144bac2208Snarayan struct dk_efi dk_efi; /* synthetic for slice type */ 3151ae08745Sheppo struct dk_geom dk_geom; /* synthetic for slice type */ 3161ae08745Sheppo struct vtoc vtoc; /* synthetic for slice type */ 3171ae08745Sheppo ldc_status_t ldc_state; /* LDC connection state */ 3181ae08745Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */ 3191ae08745Sheppo size_t max_msglen; /* largest LDC message len */ 3201ae08745Sheppo vd_state_t state; /* client handshake state */ 3211ae08745Sheppo uint8_t xfer_mode; /* transfer mode with client */ 3221ae08745Sheppo uint32_t sid; /* client's session ID */ 3231ae08745Sheppo uint64_t seq_num; /* message sequence number */ 3241ae08745Sheppo uint64_t dring_ident; /* identifier of dring */ 3251ae08745Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */ 3261ae08745Sheppo uint32_t descriptor_size; /* num bytes in desc */ 3271ae08745Sheppo uint32_t dring_len; /* number of dring elements */ 3281ae08745Sheppo caddr_t dring; /* address of dring */ 3293af08d82Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */ 330d10e4ef2Snarayan vd_task_t inband_task; /* task for inband descriptor */ 331d10e4ef2Snarayan vd_task_t *dring_task; /* tasks dring elements */ 332d10e4ef2Snarayan 333d10e4ef2Snarayan kmutex_t lock; /* protects variables below */ 334d10e4ef2Snarayan boolean_t enabled; /* is vdisk enabled? */ 335d10e4ef2Snarayan boolean_t reset_state; /* reset connection state? */ 336d10e4ef2Snarayan boolean_t reset_ldc; /* reset LDC channel? */ 3371ae08745Sheppo } vd_t; 3381ae08745Sheppo 3391ae08745Sheppo typedef struct vds_operation { 3403af08d82Slm66018 char *namep; 3411ae08745Sheppo uint8_t operation; 342d10e4ef2Snarayan int (*start)(vd_task_t *task); 343d10e4ef2Snarayan void (*complete)(void *arg); 3441ae08745Sheppo } vds_operation_t; 3451ae08745Sheppo 3460a55fbb7Slm66018 typedef struct vd_ioctl { 3470a55fbb7Slm66018 uint8_t operation; /* vdisk operation */ 3480a55fbb7Slm66018 const char *operation_name; /* vdisk operation name */ 3490a55fbb7Slm66018 size_t nbytes; /* size of operation buffer */ 3500a55fbb7Slm66018 int cmd; /* corresponding ioctl cmd */ 3510a55fbb7Slm66018 const char *cmd_name; /* ioctl cmd name */ 3520a55fbb7Slm66018 void *arg; /* ioctl cmd argument */ 3530a55fbb7Slm66018 /* convert input vd_buf to output ioctl_arg */ 3540a55fbb7Slm66018 void (*copyin)(void *vd_buf, void *ioctl_arg); 3550a55fbb7Slm66018 /* convert input ioctl_arg to output vd_buf */ 3560a55fbb7Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf); 3570a55fbb7Slm66018 } vd_ioctl_t; 3580a55fbb7Slm66018 3590a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */ 3600a55fbb7Slm66018 #define VD_IDENTITY ((void (*)(void *, void *))-1) 3611ae08745Sheppo 3621ae08745Sheppo 3633c96341aSnarayan static int vds_ldc_retries = VDS_RETRIES; 3643af08d82Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY; 3653c96341aSnarayan static int vds_dev_retries = VDS_RETRIES; 3663c96341aSnarayan static int vds_dev_delay = VDS_DEV_DELAY; 3671ae08745Sheppo static void *vds_state; 3681ae08745Sheppo static uint64_t vds_operations; /* see vds_operation[] definition below */ 3691ae08745Sheppo 3701ae08745Sheppo static int vd_open_flags = VD_OPEN_FLAGS; 3711ae08745Sheppo 3720a55fbb7Slm66018 /* 3730a55fbb7Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 3740a55fbb7Slm66018 * 3750a55fbb7Slm66018 * Each supported major version should appear only once, paired with (and only 3760a55fbb7Slm66018 * with) its highest supported minor version number (as the protocol requires 3770a55fbb7Slm66018 * supporting all lower minor version numbers as well) 3780a55fbb7Slm66018 */ 3790a55fbb7Slm66018 static const vio_ver_t vds_version[] = {{1, 0}}; 3800a55fbb7Slm66018 static const size_t vds_num_versions = 3810a55fbb7Slm66018 sizeof (vds_version)/sizeof (vds_version[0]); 3820a55fbb7Slm66018 3833af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp); 3843c96341aSnarayan static int vd_setup_vd(vd_t *vd); 3853c96341aSnarayan static boolean_t vd_enabled(vd_t *vd); 3861ae08745Sheppo 387*690555a1Sachartre /* 388*690555a1Sachartre * Function: 389*690555a1Sachartre * vd_file_rw 390*690555a1Sachartre * 391*690555a1Sachartre * Description: 392*690555a1Sachartre * Read or write to a disk on file. 393*690555a1Sachartre * 394*690555a1Sachartre * Parameters: 395*690555a1Sachartre * vd - disk on which the operation is performed. 396*690555a1Sachartre * slice - slice on which the operation is performed, 397*690555a1Sachartre * VD_FILE_SLICE_NONE indicates that the operation 398*690555a1Sachartre * is done on the raw disk. 399*690555a1Sachartre * operation - operation to execute: read (VD_OP_BREAD) or 400*690555a1Sachartre * write (VD_OP_BWRITE). 401*690555a1Sachartre * data - buffer where data are read to or written from. 402*690555a1Sachartre * blk - starting block for the operation. 403*690555a1Sachartre * len - number of bytes to read or write. 404*690555a1Sachartre * 405*690555a1Sachartre * Return Code: 406*690555a1Sachartre * n >= 0 - success, n indicates the number of bytes read 407*690555a1Sachartre * or written. 408*690555a1Sachartre * -1 - error. 409*690555a1Sachartre */ 410*690555a1Sachartre static ssize_t 411*690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk, 412*690555a1Sachartre size_t len) 413*690555a1Sachartre { 414*690555a1Sachartre caddr_t maddr; 415*690555a1Sachartre size_t offset, maxlen, moffset, mlen, n; 416*690555a1Sachartre uint_t smflags; 417*690555a1Sachartre enum seg_rw srw; 418*690555a1Sachartre 419*690555a1Sachartre ASSERT(vd->file); 420*690555a1Sachartre ASSERT(len > 0); 421*690555a1Sachartre 422*690555a1Sachartre if (slice == VD_FILE_SLICE_NONE) { 423*690555a1Sachartre /* raw disk access */ 424*690555a1Sachartre offset = blk * DEV_BSIZE; 425*690555a1Sachartre } else { 426*690555a1Sachartre ASSERT(slice >= 0 && slice < V_NUMPAR); 427*690555a1Sachartre if (blk >= vd->vtoc.v_part[slice].p_size) { 428*690555a1Sachartre /* address past the end of the slice */ 429*690555a1Sachartre PR0("req_addr (0x%lx) > psize (0x%lx)", 430*690555a1Sachartre blk, vd->vtoc.v_part[slice].p_size); 431*690555a1Sachartre return (0); 432*690555a1Sachartre } 433*690555a1Sachartre 434*690555a1Sachartre offset = (vd->vtoc.v_part[slice].p_start + blk) * DEV_BSIZE; 435*690555a1Sachartre 436*690555a1Sachartre /* 437*690555a1Sachartre * If the requested size is greater than the size 438*690555a1Sachartre * of the partition, truncate the read/write. 439*690555a1Sachartre */ 440*690555a1Sachartre maxlen = (vd->vtoc.v_part[slice].p_size - blk) * DEV_BSIZE; 441*690555a1Sachartre 442*690555a1Sachartre if (len > maxlen) { 443*690555a1Sachartre PR0("I/O size truncated to %lu bytes from %lu bytes", 444*690555a1Sachartre maxlen, len); 445*690555a1Sachartre len = maxlen; 446*690555a1Sachartre } 447*690555a1Sachartre } 448*690555a1Sachartre 449*690555a1Sachartre /* 450*690555a1Sachartre * We have to ensure that we are reading/writing into the mmap 451*690555a1Sachartre * range. If we have a partial disk image (e.g. an image of 452*690555a1Sachartre * s0 instead s2) the system can try to access slices that 453*690555a1Sachartre * are not included into the disk image. 454*690555a1Sachartre */ 455*690555a1Sachartre if ((offset + len) >= vd->file_size) { 456*690555a1Sachartre PR0("offset + nbytes (0x%lx + 0x%lx) >= " 457*690555a1Sachartre "file_size (0x%lx)", offset, len, vd->file_size); 458*690555a1Sachartre return (-1); 459*690555a1Sachartre } 460*690555a1Sachartre 461*690555a1Sachartre srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE; 462*690555a1Sachartre smflags = (operation == VD_OP_BREAD)? 0 : SM_WRITE; 463*690555a1Sachartre n = len; 464*690555a1Sachartre 465*690555a1Sachartre do { 466*690555a1Sachartre /* 467*690555a1Sachartre * segmap_getmapflt() returns a MAXBSIZE chunk which is 468*690555a1Sachartre * MAXBSIZE aligned. 469*690555a1Sachartre */ 470*690555a1Sachartre moffset = offset & MAXBOFFSET; 471*690555a1Sachartre mlen = MIN(MAXBSIZE - moffset, n); 472*690555a1Sachartre maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset, 473*690555a1Sachartre mlen, 1, srw); 474*690555a1Sachartre /* 475*690555a1Sachartre * Fault in the pages so we can check for error and ensure 476*690555a1Sachartre * that we can safely used the mapped address. 477*690555a1Sachartre */ 478*690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 479*690555a1Sachartre F_SOFTLOCK, srw) != 0) { 480*690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 481*690555a1Sachartre return (-1); 482*690555a1Sachartre } 483*690555a1Sachartre 484*690555a1Sachartre if (operation == VD_OP_BREAD) 485*690555a1Sachartre bcopy(maddr + moffset, data, mlen); 486*690555a1Sachartre else 487*690555a1Sachartre bcopy(data, maddr + moffset, mlen); 488*690555a1Sachartre 489*690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 490*690555a1Sachartre F_SOFTUNLOCK, srw) != 0) { 491*690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 492*690555a1Sachartre return (-1); 493*690555a1Sachartre } 494*690555a1Sachartre if (segmap_release(segkmap, maddr, smflags) != 0) 495*690555a1Sachartre return (-1); 496*690555a1Sachartre n -= mlen; 497*690555a1Sachartre offset += mlen; 498*690555a1Sachartre data += mlen; 499*690555a1Sachartre 500*690555a1Sachartre } while (n > 0); 501*690555a1Sachartre 502*690555a1Sachartre return (len); 503*690555a1Sachartre } 504*690555a1Sachartre 5051ae08745Sheppo static int 506d10e4ef2Snarayan vd_start_bio(vd_task_t *task) 5071ae08745Sheppo { 5084bac2208Snarayan int rv, status = 0; 509d10e4ef2Snarayan vd_t *vd = task->vd; 510d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 511d10e4ef2Snarayan struct buf *buf = &task->buf; 5124bac2208Snarayan uint8_t mtype; 5133c96341aSnarayan int slice; 514d10e4ef2Snarayan 515d10e4ef2Snarayan ASSERT(vd != NULL); 516d10e4ef2Snarayan ASSERT(request != NULL); 5173c96341aSnarayan 5183c96341aSnarayan slice = request->slice; 5193c96341aSnarayan 5203c96341aSnarayan ASSERT(slice < vd->nslices); 521d10e4ef2Snarayan ASSERT((request->operation == VD_OP_BREAD) || 522d10e4ef2Snarayan (request->operation == VD_OP_BWRITE)); 523d10e4ef2Snarayan 5241ae08745Sheppo if (request->nbytes == 0) 5251ae08745Sheppo return (EINVAL); /* no service for trivial requests */ 5261ae08745Sheppo 527d10e4ef2Snarayan PR1("%s %lu bytes at block %lu", 528d10e4ef2Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write", 529d10e4ef2Snarayan request->nbytes, request->addr); 5301ae08745Sheppo 531d10e4ef2Snarayan bioinit(buf); 532d10e4ef2Snarayan buf->b_flags = B_BUSY; 533d10e4ef2Snarayan buf->b_bcount = request->nbytes; 534d10e4ef2Snarayan buf->b_lblkno = request->addr; 5353c96341aSnarayan buf->b_edev = vd->dev[slice]; 536d10e4ef2Snarayan 5374bac2208Snarayan mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 5384bac2208Snarayan 5394bac2208Snarayan /* Map memory exported by client */ 5404bac2208Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 5414bac2208Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 5424bac2208Snarayan &(buf->b_un.b_addr), NULL); 5434bac2208Snarayan if (status != 0) { 5443af08d82Slm66018 PR0("ldc_mem_map() returned err %d ", status); 5454bac2208Snarayan biofini(buf); 5464bac2208Snarayan return (status); 547d10e4ef2Snarayan } 548d10e4ef2Snarayan 5494bac2208Snarayan status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount); 5504bac2208Snarayan if (status != 0) { 5514bac2208Snarayan (void) ldc_mem_unmap(task->mhdl); 5523af08d82Slm66018 PR0("ldc_mem_acquire() returned err %d ", status); 5534bac2208Snarayan biofini(buf); 5544bac2208Snarayan return (status); 5554bac2208Snarayan } 5564bac2208Snarayan 5574bac2208Snarayan buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE; 5584bac2208Snarayan 559d10e4ef2Snarayan /* Start the block I/O */ 5603c96341aSnarayan if (vd->file) { 561*690555a1Sachartre rv = vd_file_rw(vd, slice, request->operation, buf->b_un.b_addr, 562*690555a1Sachartre request->addr, request->nbytes); 563*690555a1Sachartre if (rv < 0) { 5643c96341aSnarayan request->nbytes = 0; 5653c96341aSnarayan status = EIO; 566*690555a1Sachartre } else { 567*690555a1Sachartre request->nbytes = rv; 568*690555a1Sachartre status = 0; 5693c96341aSnarayan } 5703c96341aSnarayan } else { 5713c96341aSnarayan status = ldi_strategy(vd->ldi_handle[slice], buf); 5723c96341aSnarayan if (status == 0) 5733c96341aSnarayan return (EINPROGRESS); /* will complete on completionq */ 5743c96341aSnarayan } 5753c96341aSnarayan 576d10e4ef2Snarayan /* Clean up after error */ 5774bac2208Snarayan rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount); 5784bac2208Snarayan if (rv) { 5793af08d82Slm66018 PR0("ldc_mem_release() returned err %d ", rv); 5804bac2208Snarayan } 5814bac2208Snarayan rv = ldc_mem_unmap(task->mhdl); 5824bac2208Snarayan if (rv) { 5833af08d82Slm66018 PR0("ldc_mem_unmap() returned err %d ", status); 5844bac2208Snarayan } 5854bac2208Snarayan 586d10e4ef2Snarayan biofini(buf); 587d10e4ef2Snarayan return (status); 588d10e4ef2Snarayan } 589d10e4ef2Snarayan 590d10e4ef2Snarayan static int 591d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 592d10e4ef2Snarayan { 5933af08d82Slm66018 int status; 594d10e4ef2Snarayan size_t nbytes; 595d10e4ef2Snarayan 5963af08d82Slm66018 do { 597d10e4ef2Snarayan nbytes = msglen; 598d10e4ef2Snarayan status = ldc_write(ldc_handle, msg, &nbytes); 5993af08d82Slm66018 if (status != EWOULDBLOCK) 6003af08d82Slm66018 break; 6013af08d82Slm66018 drv_usecwait(vds_ldc_delay); 6023af08d82Slm66018 } while (status == EWOULDBLOCK); 603d10e4ef2Snarayan 604d10e4ef2Snarayan if (status != 0) { 6053af08d82Slm66018 if (status != ECONNRESET) 6063af08d82Slm66018 PR0("ldc_write() returned errno %d", status); 607d10e4ef2Snarayan return (status); 608d10e4ef2Snarayan } else if (nbytes != msglen) { 6093af08d82Slm66018 PR0("ldc_write() performed only partial write"); 610d10e4ef2Snarayan return (EIO); 611d10e4ef2Snarayan } 612d10e4ef2Snarayan 613d10e4ef2Snarayan PR1("SENT %lu bytes", msglen); 614d10e4ef2Snarayan return (0); 615d10e4ef2Snarayan } 616d10e4ef2Snarayan 617d10e4ef2Snarayan static void 618d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc) 619d10e4ef2Snarayan { 620d10e4ef2Snarayan mutex_enter(&vd->lock); 621d10e4ef2Snarayan vd->reset_state = B_TRUE; 622d10e4ef2Snarayan vd->reset_ldc = reset_ldc; 623d10e4ef2Snarayan mutex_exit(&vd->lock); 624d10e4ef2Snarayan } 625d10e4ef2Snarayan 626d10e4ef2Snarayan /* 627d10e4ef2Snarayan * Reset the state of the connection with a client, if needed; reset the LDC 628d10e4ef2Snarayan * transport as well, if needed. This function should only be called from the 6293af08d82Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 630d10e4ef2Snarayan */ 631d10e4ef2Snarayan static void 632d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd) 633d10e4ef2Snarayan { 634d10e4ef2Snarayan int status = 0; 635d10e4ef2Snarayan 636d10e4ef2Snarayan mutex_enter(&vd->lock); 637d10e4ef2Snarayan if (!vd->reset_state) { 638d10e4ef2Snarayan ASSERT(!vd->reset_ldc); 639d10e4ef2Snarayan mutex_exit(&vd->lock); 640d10e4ef2Snarayan return; 641d10e4ef2Snarayan } 642d10e4ef2Snarayan mutex_exit(&vd->lock); 643d10e4ef2Snarayan 644d10e4ef2Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd)); 645d10e4ef2Snarayan 646d10e4ef2Snarayan /* 647d10e4ef2Snarayan * Let any asynchronous I/O complete before possibly pulling the rug 648d10e4ef2Snarayan * out from under it; defer checking vd->reset_ldc, as one of the 649d10e4ef2Snarayan * asynchronous tasks might set it 650d10e4ef2Snarayan */ 651d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 652d10e4ef2Snarayan 6533c96341aSnarayan if (vd->file) { 6543c96341aSnarayan status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred); 6553c96341aSnarayan if (status) { 6563c96341aSnarayan PR0("VOP_FSYNC returned errno %d", status); 6573c96341aSnarayan } 6583c96341aSnarayan } 6593c96341aSnarayan 660d10e4ef2Snarayan if ((vd->initialized & VD_DRING) && 661d10e4ef2Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 6623af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 663d10e4ef2Snarayan 6643af08d82Slm66018 vd_free_dring_task(vd); 6653af08d82Slm66018 6663af08d82Slm66018 /* Free the staging buffer for msgs */ 6673af08d82Slm66018 if (vd->vio_msgp != NULL) { 6683af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 6693af08d82Slm66018 vd->vio_msgp = NULL; 670d10e4ef2Snarayan } 671d10e4ef2Snarayan 6723af08d82Slm66018 /* Free the inband message buffer */ 6733af08d82Slm66018 if (vd->inband_task.msg != NULL) { 6743af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 6753af08d82Slm66018 vd->inband_task.msg = NULL; 6763af08d82Slm66018 } 677d10e4ef2Snarayan 678d10e4ef2Snarayan mutex_enter(&vd->lock); 6793af08d82Slm66018 6803af08d82Slm66018 if (vd->reset_ldc) 6813af08d82Slm66018 PR0("taking down LDC channel"); 682e1ebb9ecSlm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 6833af08d82Slm66018 PR0("ldc_down() returned errno %d", status); 684d10e4ef2Snarayan 685d10e4ef2Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 686d10e4ef2Snarayan vd->state = VD_STATE_INIT; 687d10e4ef2Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 688d10e4ef2Snarayan 6893af08d82Slm66018 /* Allocate the staging buffer */ 6903af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 6913af08d82Slm66018 6923af08d82Slm66018 PR0("calling ldc_up\n"); 6933af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 6943af08d82Slm66018 695d10e4ef2Snarayan vd->reset_state = B_FALSE; 696d10e4ef2Snarayan vd->reset_ldc = B_FALSE; 6973af08d82Slm66018 698d10e4ef2Snarayan mutex_exit(&vd->lock); 699d10e4ef2Snarayan } 700d10e4ef2Snarayan 7013af08d82Slm66018 static void vd_recv_msg(void *arg); 7023af08d82Slm66018 7033af08d82Slm66018 static void 7043af08d82Slm66018 vd_mark_in_reset(vd_t *vd) 7053af08d82Slm66018 { 7063af08d82Slm66018 int status; 7073af08d82Slm66018 7083af08d82Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n"); 7093af08d82Slm66018 7103af08d82Slm66018 vd_need_reset(vd, B_FALSE); 7113af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 7123af08d82Slm66018 if (status == DDI_FAILURE) { 7133af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 7143af08d82Slm66018 vd_need_reset(vd, B_TRUE); 7153af08d82Slm66018 return; 7163af08d82Slm66018 } 7173af08d82Slm66018 } 7183af08d82Slm66018 719d10e4ef2Snarayan static int 7203c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes) 721d10e4ef2Snarayan { 722d10e4ef2Snarayan boolean_t accepted; 723d10e4ef2Snarayan int status; 724d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 725d10e4ef2Snarayan 7263af08d82Slm66018 if (vd->reset_state) 7273af08d82Slm66018 return (0); 728d10e4ef2Snarayan 729d10e4ef2Snarayan /* Acquire the element */ 7303af08d82Slm66018 if (!vd->reset_state && 7313af08d82Slm66018 (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 7323af08d82Slm66018 if (status == ECONNRESET) { 7333af08d82Slm66018 vd_mark_in_reset(vd); 7343af08d82Slm66018 return (0); 7353af08d82Slm66018 } else { 7363af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", 7373af08d82Slm66018 status); 738d10e4ef2Snarayan return (status); 739d10e4ef2Snarayan } 7403af08d82Slm66018 } 741d10e4ef2Snarayan 742d10e4ef2Snarayan /* Set the element's status and mark it done */ 743d10e4ef2Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 744d10e4ef2Snarayan if (accepted) { 7453c96341aSnarayan elem->payload.nbytes = elem_nbytes; 746d10e4ef2Snarayan elem->payload.status = elem_status; 747d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_DONE; 748d10e4ef2Snarayan } else { 749d10e4ef2Snarayan /* Perhaps client timed out waiting for I/O... */ 7503af08d82Slm66018 PR0("element %u no longer \"accepted\"", idx); 751d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 752d10e4ef2Snarayan } 753d10e4ef2Snarayan /* Release the element */ 7543af08d82Slm66018 if (!vd->reset_state && 7553af08d82Slm66018 (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 7563af08d82Slm66018 if (status == ECONNRESET) { 7573af08d82Slm66018 vd_mark_in_reset(vd); 7583af08d82Slm66018 return (0); 7593af08d82Slm66018 } else { 7603af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", 7613af08d82Slm66018 status); 762d10e4ef2Snarayan return (status); 763d10e4ef2Snarayan } 7643af08d82Slm66018 } 765d10e4ef2Snarayan 766d10e4ef2Snarayan return (accepted ? 0 : EINVAL); 767d10e4ef2Snarayan } 768d10e4ef2Snarayan 769d10e4ef2Snarayan static void 770d10e4ef2Snarayan vd_complete_bio(void *arg) 771d10e4ef2Snarayan { 772d10e4ef2Snarayan int status = 0; 773d10e4ef2Snarayan vd_task_t *task = (vd_task_t *)arg; 774d10e4ef2Snarayan vd_t *vd = task->vd; 775d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 776d10e4ef2Snarayan struct buf *buf = &task->buf; 777d10e4ef2Snarayan 778d10e4ef2Snarayan 779d10e4ef2Snarayan ASSERT(vd != NULL); 780d10e4ef2Snarayan ASSERT(request != NULL); 781d10e4ef2Snarayan ASSERT(task->msg != NULL); 782d10e4ef2Snarayan ASSERT(task->msglen >= sizeof (*task->msg)); 7833c96341aSnarayan ASSERT(!vd->file); 784d10e4ef2Snarayan 785d10e4ef2Snarayan /* Wait for the I/O to complete */ 786d10e4ef2Snarayan request->status = biowait(buf); 787d10e4ef2Snarayan 7883c96341aSnarayan /* return back the number of bytes read/written */ 7893c96341aSnarayan request->nbytes = buf->b_bcount - buf->b_resid; 7903c96341aSnarayan 7914bac2208Snarayan /* Release the buffer */ 7923af08d82Slm66018 if (!vd->reset_state) 7934bac2208Snarayan status = ldc_mem_release(task->mhdl, 0, buf->b_bcount); 7944bac2208Snarayan if (status) { 7953af08d82Slm66018 PR0("ldc_mem_release() returned errno %d copying to " 7963af08d82Slm66018 "client", status); 7973af08d82Slm66018 if (status == ECONNRESET) { 7983af08d82Slm66018 vd_mark_in_reset(vd); 7993af08d82Slm66018 } 8001ae08745Sheppo } 8011ae08745Sheppo 8023af08d82Slm66018 /* Unmap the memory, even if in reset */ 8034bac2208Snarayan status = ldc_mem_unmap(task->mhdl); 8044bac2208Snarayan if (status) { 8053af08d82Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client", 8064bac2208Snarayan status); 8073af08d82Slm66018 if (status == ECONNRESET) { 8083af08d82Slm66018 vd_mark_in_reset(vd); 8093af08d82Slm66018 } 8104bac2208Snarayan } 8114bac2208Snarayan 812d10e4ef2Snarayan biofini(buf); 8131ae08745Sheppo 814d10e4ef2Snarayan /* Update the dring element for a dring client */ 8153af08d82Slm66018 if (!vd->reset_state && (status == 0) && 8163af08d82Slm66018 (vd->xfer_mode == VIO_DRING_MODE)) { 8173c96341aSnarayan status = vd_mark_elem_done(vd, task->index, 8183c96341aSnarayan request->status, request->nbytes); 8193af08d82Slm66018 if (status == ECONNRESET) 8203af08d82Slm66018 vd_mark_in_reset(vd); 8213af08d82Slm66018 } 8221ae08745Sheppo 823d10e4ef2Snarayan /* 824d10e4ef2Snarayan * If a transport error occurred, arrange to "nack" the message when 825d10e4ef2Snarayan * the final task in the descriptor element range completes 826d10e4ef2Snarayan */ 827d10e4ef2Snarayan if (status != 0) 828d10e4ef2Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 8291ae08745Sheppo 830d10e4ef2Snarayan /* 831d10e4ef2Snarayan * Only the final task for a range of elements will respond to and 832d10e4ef2Snarayan * free the message 833d10e4ef2Snarayan */ 8343af08d82Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) { 835d10e4ef2Snarayan return; 8363af08d82Slm66018 } 8371ae08745Sheppo 838d10e4ef2Snarayan /* 839d10e4ef2Snarayan * Send the "ack" or "nack" back to the client; if sending the message 840d10e4ef2Snarayan * via LDC fails, arrange to reset both the connection state and LDC 841d10e4ef2Snarayan * itself 842d10e4ef2Snarayan */ 843d10e4ef2Snarayan PR1("Sending %s", 844d10e4ef2Snarayan (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 8453af08d82Slm66018 if (!vd->reset_state) { 8463af08d82Slm66018 status = send_msg(vd->ldc_handle, task->msg, task->msglen); 8473af08d82Slm66018 switch (status) { 8483af08d82Slm66018 case 0: 8493af08d82Slm66018 break; 8503af08d82Slm66018 case ECONNRESET: 8513af08d82Slm66018 vd_mark_in_reset(vd); 8523af08d82Slm66018 break; 8533af08d82Slm66018 default: 8543af08d82Slm66018 PR0("initiating full reset"); 855d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 8563af08d82Slm66018 break; 8573af08d82Slm66018 } 8583af08d82Slm66018 } 8591ae08745Sheppo } 8601ae08745Sheppo 8610a55fbb7Slm66018 static void 8620a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg) 8630a55fbb7Slm66018 { 8640a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 8650a55fbb7Slm66018 } 8660a55fbb7Slm66018 8670a55fbb7Slm66018 static void 8680a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg) 8690a55fbb7Slm66018 { 8700a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg); 8710a55fbb7Slm66018 } 8720a55fbb7Slm66018 8730a55fbb7Slm66018 static void 8740a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 8750a55fbb7Slm66018 { 8760a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 8770a55fbb7Slm66018 } 8780a55fbb7Slm66018 8790a55fbb7Slm66018 static void 8800a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 8810a55fbb7Slm66018 { 8820a55fbb7Slm66018 VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 8830a55fbb7Slm66018 } 8840a55fbb7Slm66018 8854bac2208Snarayan static void 8864bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg) 8874bac2208Snarayan { 8884bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 8894bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 8904bac2208Snarayan 8914bac2208Snarayan dk_efi->dki_lba = vd_efi->lba; 8924bac2208Snarayan dk_efi->dki_length = vd_efi->length; 8934bac2208Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 8944bac2208Snarayan } 8954bac2208Snarayan 8964bac2208Snarayan static void 8974bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf) 8984bac2208Snarayan { 8994bac2208Snarayan int len; 9004bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 9014bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 9024bac2208Snarayan 9034bac2208Snarayan len = vd_efi->length; 9044bac2208Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi); 9054bac2208Snarayan kmem_free(dk_efi->dki_data, len); 9064bac2208Snarayan } 9074bac2208Snarayan 9084bac2208Snarayan static void 9094bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg) 9104bac2208Snarayan { 9114bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 9124bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 9134bac2208Snarayan 9144bac2208Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 9154bac2208Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi); 9164bac2208Snarayan } 9174bac2208Snarayan 9184bac2208Snarayan static void 9194bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf) 9204bac2208Snarayan { 9214bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 9224bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 9234bac2208Snarayan 9244bac2208Snarayan kmem_free(dk_efi->dki_data, vd_efi->length); 9254bac2208Snarayan } 9264bac2208Snarayan 9274bac2208Snarayan static int 9284bac2208Snarayan vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc, vd_disk_label_t *label) 9294bac2208Snarayan { 9304bac2208Snarayan int status, rval; 9314bac2208Snarayan struct dk_gpt *efi; 9324bac2208Snarayan size_t efi_len; 9334bac2208Snarayan 9344bac2208Snarayan *label = VD_DISK_LABEL_UNK; 9354bac2208Snarayan 9364bac2208Snarayan status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc, 9374bac2208Snarayan (vd_open_flags | FKIOCTL), kcred, &rval); 9384bac2208Snarayan 9394bac2208Snarayan if (status == 0) { 9404bac2208Snarayan *label = VD_DISK_LABEL_VTOC; 9414bac2208Snarayan return (0); 9424bac2208Snarayan } else if (status != ENOTSUP) { 9433af08d82Slm66018 PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status); 9444bac2208Snarayan return (status); 9454bac2208Snarayan } 9464bac2208Snarayan 9474bac2208Snarayan status = vds_efi_alloc_and_read(handle, &efi, &efi_len); 9484bac2208Snarayan 9494bac2208Snarayan if (status) { 9503af08d82Slm66018 PR0("vds_efi_alloc_and_read returned error %d", status); 9514bac2208Snarayan return (status); 9524bac2208Snarayan } 9534bac2208Snarayan 9544bac2208Snarayan *label = VD_DISK_LABEL_EFI; 9554bac2208Snarayan vd_efi_to_vtoc(efi, vtoc); 9564bac2208Snarayan vd_efi_free(efi, efi_len); 9574bac2208Snarayan 9584bac2208Snarayan return (0); 9594bac2208Snarayan } 9604bac2208Snarayan 961*690555a1Sachartre static ushort_t 9623c96341aSnarayan vd_lbl2cksum(struct dk_label *label) 9633c96341aSnarayan { 9643c96341aSnarayan int count; 965*690555a1Sachartre ushort_t sum, *sp; 9663c96341aSnarayan 9673c96341aSnarayan count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 968*690555a1Sachartre sp = (ushort_t *)label; 9693c96341aSnarayan sum = 0; 9703c96341aSnarayan while (count--) { 9713c96341aSnarayan sum ^= *sp++; 9723c96341aSnarayan } 9733c96341aSnarayan 9743c96341aSnarayan return (sum); 9753c96341aSnarayan } 9763c96341aSnarayan 9771ae08745Sheppo static int 9780a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 9791ae08745Sheppo { 9804bac2208Snarayan dk_efi_t *dk_ioc; 981*690555a1Sachartre struct dk_label label; 982*690555a1Sachartre struct vtoc *vtoc; 9833c96341aSnarayan int i; 9844bac2208Snarayan 9854bac2208Snarayan switch (vd->vdisk_label) { 9864bac2208Snarayan 9874bac2208Snarayan case VD_DISK_LABEL_VTOC: 9884bac2208Snarayan 9891ae08745Sheppo switch (cmd) { 9901ae08745Sheppo case DKIOCGGEOM: 9910a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 9920a55fbb7Slm66018 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 9931ae08745Sheppo return (0); 9941ae08745Sheppo case DKIOCGVTOC: 9950a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 9960a55fbb7Slm66018 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 9971ae08745Sheppo return (0); 9983c96341aSnarayan case DKIOCSVTOC: 9993c96341aSnarayan if (!vd->file) 10003c96341aSnarayan return (ENOTSUP); 10013c96341aSnarayan ASSERT(ioctl_arg != NULL); 1002*690555a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 1003*690555a1Sachartre 1004*690555a1Sachartre if (vtoc->v_sanity != VTOC_SANE || 1005*690555a1Sachartre vtoc->v_sectorsz != DEV_BSIZE || 1006*690555a1Sachartre vtoc->v_nparts != V_NUMPAR) 1007*690555a1Sachartre return (EINVAL); 1008*690555a1Sachartre 1009*690555a1Sachartre bzero(&label, sizeof (label)); 1010*690555a1Sachartre label.dkl_ncyl = vd->dk_geom.dkg_ncyl; 1011*690555a1Sachartre label.dkl_acyl = vd->dk_geom.dkg_acyl; 1012*690555a1Sachartre label.dkl_pcyl = vd->dk_geom.dkg_pcyl; 1013*690555a1Sachartre label.dkl_nhead = vd->dk_geom.dkg_nhead; 1014*690555a1Sachartre label.dkl_nsect = vd->dk_geom.dkg_nsect; 1015*690555a1Sachartre label.dkl_intrlv = vd->dk_geom.dkg_intrlv; 1016*690555a1Sachartre label.dkl_apc = vd->dk_geom.dkg_apc; 1017*690555a1Sachartre label.dkl_rpm = vd->dk_geom.dkg_rpm; 1018*690555a1Sachartre label.dkl_write_reinstruct = 1019*690555a1Sachartre vd->dk_geom.dkg_write_reinstruct; 1020*690555a1Sachartre label.dkl_read_reinstruct = 1021*690555a1Sachartre vd->dk_geom.dkg_read_reinstruct; 1022*690555a1Sachartre 1023*690555a1Sachartre label.dkl_vtoc.v_nparts = vtoc->v_nparts; 1024*690555a1Sachartre label.dkl_vtoc.v_sanity = vtoc->v_sanity; 1025*690555a1Sachartre label.dkl_vtoc.v_version = vtoc->v_version; 1026*690555a1Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 1027*690555a1Sachartre label.dkl_vtoc.v_timestamp[i] = 1028*690555a1Sachartre vtoc->timestamp[i]; 1029*690555a1Sachartre label.dkl_vtoc.v_part[i].p_tag = 1030*690555a1Sachartre vtoc->v_part[i].p_tag; 1031*690555a1Sachartre label.dkl_vtoc.v_part[i].p_flag = 1032*690555a1Sachartre vtoc->v_part[i].p_flag; 1033*690555a1Sachartre label.dkl_map[i].dkl_cylno = 1034*690555a1Sachartre vtoc->v_part[i].p_start / 1035*690555a1Sachartre (label.dkl_nhead * label.dkl_nsect); 1036*690555a1Sachartre label.dkl_map[i].dkl_nblk = 1037*690555a1Sachartre vtoc->v_part[i].p_size; 10383c96341aSnarayan } 1039*690555a1Sachartre bcopy(vtoc->v_asciilabel, label.dkl_asciilabel, 1040*690555a1Sachartre LEN_DKL_ASCII); 1041*690555a1Sachartre bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume, 1042*690555a1Sachartre LEN_DKL_VVOL); 1043*690555a1Sachartre bcopy(vtoc->v_bootinfo, label.dkl_vtoc.v_bootinfo, 1044*690555a1Sachartre sizeof (vtoc->v_bootinfo)); 10453c96341aSnarayan 10463c96341aSnarayan /* re-compute checksum */ 1047*690555a1Sachartre label.dkl_magic = DKL_MAGIC; 1048*690555a1Sachartre label.dkl_cksum = vd_lbl2cksum(&label); 1049*690555a1Sachartre 1050*690555a1Sachartre /* write label to file */ 1051*690555a1Sachartre if (VD_FILE_LABEL_WRITE(vd, &label) < 0) 1052*690555a1Sachartre return (EIO); 1053*690555a1Sachartre 1054*690555a1Sachartre /* update the cached vdisk VTOC */ 1055*690555a1Sachartre bcopy(vtoc, &vd->vtoc, sizeof (vd->vtoc)); 10563c96341aSnarayan 10573c96341aSnarayan return (0); 10581ae08745Sheppo default: 10591ae08745Sheppo return (ENOTSUP); 10601ae08745Sheppo } 10614bac2208Snarayan 10624bac2208Snarayan case VD_DISK_LABEL_EFI: 10634bac2208Snarayan 10644bac2208Snarayan switch (cmd) { 10654bac2208Snarayan case DKIOCGETEFI: 10664bac2208Snarayan ASSERT(ioctl_arg != NULL); 10674bac2208Snarayan dk_ioc = (dk_efi_t *)ioctl_arg; 10684bac2208Snarayan if (dk_ioc->dki_length < vd->dk_efi.dki_length) 10694bac2208Snarayan return (EINVAL); 10704bac2208Snarayan bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data, 10714bac2208Snarayan vd->dk_efi.dki_length); 10724bac2208Snarayan return (0); 10734bac2208Snarayan default: 10744bac2208Snarayan return (ENOTSUP); 10754bac2208Snarayan } 10764bac2208Snarayan 10774bac2208Snarayan default: 10784bac2208Snarayan return (ENOTSUP); 10794bac2208Snarayan } 10801ae08745Sheppo } 10811ae08745Sheppo 10821ae08745Sheppo static int 10830a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 10841ae08745Sheppo { 10851ae08745Sheppo int rval = 0, status; 10861ae08745Sheppo size_t nbytes = request->nbytes; /* modifiable copy */ 10871ae08745Sheppo 10881ae08745Sheppo 10891ae08745Sheppo ASSERT(request->slice < vd->nslices); 10901ae08745Sheppo PR0("Performing %s", ioctl->operation_name); 10911ae08745Sheppo 10920a55fbb7Slm66018 /* Get data from client and convert, if necessary */ 10930a55fbb7Slm66018 if (ioctl->copyin != NULL) { 10941ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 10951ae08745Sheppo PR1("Getting \"arg\" data from client"); 10961ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 10971ae08745Sheppo request->cookie, request->ncookies, 10981ae08745Sheppo LDC_COPY_IN)) != 0) { 10993af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 11001ae08745Sheppo "copying from client", status); 11011ae08745Sheppo return (status); 11021ae08745Sheppo } 11030a55fbb7Slm66018 11040a55fbb7Slm66018 /* Convert client's data, if necessary */ 11050a55fbb7Slm66018 if (ioctl->copyin == VD_IDENTITY) /* use client buffer */ 11060a55fbb7Slm66018 ioctl->arg = buf; 11070a55fbb7Slm66018 else /* convert client vdisk operation data to ioctl data */ 11080a55fbb7Slm66018 (ioctl->copyin)(buf, (void *)ioctl->arg); 11091ae08745Sheppo } 11101ae08745Sheppo 11111ae08745Sheppo /* 11121ae08745Sheppo * Handle single-slice block devices internally; otherwise, have the 11131ae08745Sheppo * real driver perform the ioctl() 11141ae08745Sheppo */ 11153c96341aSnarayan if (vd->file || (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo)) { 11160a55fbb7Slm66018 if ((status = vd_do_slice_ioctl(vd, ioctl->cmd, 11170a55fbb7Slm66018 (void *)ioctl->arg)) != 0) 11181ae08745Sheppo return (status); 11191ae08745Sheppo } else if ((status = ldi_ioctl(vd->ldi_handle[request->slice], 1120d10e4ef2Snarayan ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL), 1121d10e4ef2Snarayan kcred, &rval)) != 0) { 11221ae08745Sheppo PR0("ldi_ioctl(%s) = errno %d", ioctl->cmd_name, status); 11231ae08745Sheppo return (status); 11241ae08745Sheppo } 11251ae08745Sheppo #ifdef DEBUG 11261ae08745Sheppo if (rval != 0) { 11273af08d82Slm66018 PR0("%s set rval = %d, which is not being returned to client", 11281ae08745Sheppo ioctl->cmd_name, rval); 11291ae08745Sheppo } 11301ae08745Sheppo #endif /* DEBUG */ 11311ae08745Sheppo 11320a55fbb7Slm66018 /* Convert data and send to client, if necessary */ 11330a55fbb7Slm66018 if (ioctl->copyout != NULL) { 11341ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 11351ae08745Sheppo PR1("Sending \"arg\" data to client"); 11360a55fbb7Slm66018 11370a55fbb7Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */ 11380a55fbb7Slm66018 if (ioctl->copyout != VD_IDENTITY) 11390a55fbb7Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf); 11400a55fbb7Slm66018 11411ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 11421ae08745Sheppo request->cookie, request->ncookies, 11431ae08745Sheppo LDC_COPY_OUT)) != 0) { 11443af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 11451ae08745Sheppo "copying to client", status); 11461ae08745Sheppo return (status); 11471ae08745Sheppo } 11481ae08745Sheppo } 11491ae08745Sheppo 11501ae08745Sheppo return (status); 11511ae08745Sheppo } 11521ae08745Sheppo 11531ae08745Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 11541ae08745Sheppo static int 1155d10e4ef2Snarayan vd_ioctl(vd_task_t *task) 11561ae08745Sheppo { 115734683adeSsg70180 int i, status, rc; 11581ae08745Sheppo void *buf = NULL; 11590a55fbb7Slm66018 struct dk_geom dk_geom = {0}; 11600a55fbb7Slm66018 struct vtoc vtoc = {0}; 11614bac2208Snarayan struct dk_efi dk_efi = {0}; 1162d10e4ef2Snarayan vd_t *vd = task->vd; 1163d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 11640a55fbb7Slm66018 vd_ioctl_t ioctl[] = { 11650a55fbb7Slm66018 /* Command (no-copy) operations */ 11660a55fbb7Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 11670a55fbb7Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 11680a55fbb7Slm66018 NULL, NULL, NULL}, 11690a55fbb7Slm66018 11700a55fbb7Slm66018 /* "Get" (copy-out) operations */ 11710a55fbb7Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 11720a55fbb7Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 11734bac2208Snarayan NULL, VD_IDENTITY, VD_IDENTITY}, 11740a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 11750a55fbb7Slm66018 RNDSIZE(vd_geom_t), 11760a55fbb7Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 11770a55fbb7Slm66018 &dk_geom, NULL, dk_geom2vd_geom}, 11780a55fbb7Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 11790a55fbb7Slm66018 DKIOCGVTOC, STRINGIZE(DKIOCGVTOC), 11800a55fbb7Slm66018 &vtoc, NULL, vtoc2vd_vtoc}, 11814bac2208Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 11824bac2208Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 11834bac2208Snarayan &dk_efi, vd_get_efi_in, vd_get_efi_out}, 11840a55fbb7Slm66018 11850a55fbb7Slm66018 /* "Set" (copy-in) operations */ 11860a55fbb7Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 11870a55fbb7Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 11884bac2208Snarayan NULL, VD_IDENTITY, VD_IDENTITY}, 11890a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 11900a55fbb7Slm66018 RNDSIZE(vd_geom_t), 11910a55fbb7Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 11920a55fbb7Slm66018 &dk_geom, vd_geom2dk_geom, NULL}, 11930a55fbb7Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 11940a55fbb7Slm66018 DKIOCSVTOC, STRINGIZE(DKIOCSVTOC), 11950a55fbb7Slm66018 &vtoc, vd_vtoc2vtoc, NULL}, 11964bac2208Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 11974bac2208Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 11984bac2208Snarayan &dk_efi, vd_set_efi_in, vd_set_efi_out}, 11990a55fbb7Slm66018 }; 12001ae08745Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 12011ae08745Sheppo 12021ae08745Sheppo 1203d10e4ef2Snarayan ASSERT(vd != NULL); 1204d10e4ef2Snarayan ASSERT(request != NULL); 12051ae08745Sheppo ASSERT(request->slice < vd->nslices); 12061ae08745Sheppo 12071ae08745Sheppo /* 12081ae08745Sheppo * Determine ioctl corresponding to caller's "operation" and 12091ae08745Sheppo * validate caller's "nbytes" 12101ae08745Sheppo */ 12111ae08745Sheppo for (i = 0; i < nioctls; i++) { 12121ae08745Sheppo if (request->operation == ioctl[i].operation) { 12130a55fbb7Slm66018 /* LDC memory operations require 8-byte multiples */ 12140a55fbb7Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 12150a55fbb7Slm66018 12164bac2208Snarayan if (request->operation == VD_OP_GET_EFI || 12174bac2208Snarayan request->operation == VD_OP_SET_EFI) { 12184bac2208Snarayan if (request->nbytes >= ioctl[i].nbytes) 12194bac2208Snarayan break; 12203af08d82Slm66018 PR0("%s: Expected at least nbytes = %lu, " 12214bac2208Snarayan "got %lu", ioctl[i].operation_name, 12224bac2208Snarayan ioctl[i].nbytes, request->nbytes); 12234bac2208Snarayan return (EINVAL); 12244bac2208Snarayan } 12254bac2208Snarayan 12260a55fbb7Slm66018 if (request->nbytes != ioctl[i].nbytes) { 12273af08d82Slm66018 PR0("%s: Expected nbytes = %lu, got %lu", 12280a55fbb7Slm66018 ioctl[i].operation_name, ioctl[i].nbytes, 12290a55fbb7Slm66018 request->nbytes); 12301ae08745Sheppo return (EINVAL); 12311ae08745Sheppo } 12321ae08745Sheppo 12331ae08745Sheppo break; 12341ae08745Sheppo } 12351ae08745Sheppo } 12361ae08745Sheppo ASSERT(i < nioctls); /* because "operation" already validated */ 12371ae08745Sheppo 12381ae08745Sheppo if (request->nbytes) 12391ae08745Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP); 12401ae08745Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 12411ae08745Sheppo if (request->nbytes) 12421ae08745Sheppo kmem_free(buf, request->nbytes); 12433c96341aSnarayan if (!vd->file && vd->vdisk_type == VD_DISK_TYPE_DISK && 12444bac2208Snarayan (request->operation == VD_OP_SET_VTOC || 124534683adeSsg70180 request->operation == VD_OP_SET_EFI)) { 124634683adeSsg70180 /* update disk information */ 124734683adeSsg70180 rc = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, 124834683adeSsg70180 &vd->vdisk_label); 124934683adeSsg70180 if (rc != 0) 125034683adeSsg70180 PR0("vd_read_vtoc return error %d", rc); 125134683adeSsg70180 } 1252d10e4ef2Snarayan PR0("Returning %d", status); 12531ae08745Sheppo return (status); 12541ae08745Sheppo } 12551ae08745Sheppo 12564bac2208Snarayan static int 12574bac2208Snarayan vd_get_devid(vd_task_t *task) 12584bac2208Snarayan { 12594bac2208Snarayan vd_t *vd = task->vd; 12604bac2208Snarayan vd_dring_payload_t *request = task->request; 12614bac2208Snarayan vd_devid_t *vd_devid; 12624bac2208Snarayan impl_devid_t *devid; 12634bac2208Snarayan int status, bufid_len, devid_len, len; 12643af08d82Slm66018 int bufbytes; 12654bac2208Snarayan 12663af08d82Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes); 12674bac2208Snarayan 12683c96341aSnarayan if (vd->file) { 12693c96341aSnarayan /* no devid for disk on file */ 12703c96341aSnarayan return (ENOENT); 12713c96341aSnarayan } 12723c96341aSnarayan 12734bac2208Snarayan if (ddi_lyr_get_devid(vd->dev[request->slice], 12744bac2208Snarayan (ddi_devid_t *)&devid) != DDI_SUCCESS) { 12754bac2208Snarayan /* the most common failure is that no devid is available */ 12763af08d82Slm66018 PR2("No Device ID"); 12774bac2208Snarayan return (ENOENT); 12784bac2208Snarayan } 12794bac2208Snarayan 12804bac2208Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 12814bac2208Snarayan devid_len = DEVID_GETLEN(devid); 12824bac2208Snarayan 12833af08d82Slm66018 /* 12843af08d82Slm66018 * Save the buffer size here for use in deallocation. 12853af08d82Slm66018 * The actual number of bytes copied is returned in 12863af08d82Slm66018 * the 'nbytes' field of the request structure. 12873af08d82Slm66018 */ 12883af08d82Slm66018 bufbytes = request->nbytes; 12893af08d82Slm66018 12903af08d82Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 12914bac2208Snarayan vd_devid->length = devid_len; 12924bac2208Snarayan vd_devid->type = DEVID_GETTYPE(devid); 12934bac2208Snarayan 12944bac2208Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len; 12954bac2208Snarayan 12964bac2208Snarayan bcopy(devid->did_id, vd_devid->id, len); 12974bac2208Snarayan 12984bac2208Snarayan /* LDC memory operations require 8-byte multiples */ 12994bac2208Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0); 13004bac2208Snarayan 13014bac2208Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 13024bac2208Snarayan &request->nbytes, request->cookie, request->ncookies, 13034bac2208Snarayan LDC_COPY_OUT)) != 0) { 13043af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client", 13054bac2208Snarayan status); 13064bac2208Snarayan } 13073af08d82Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes); 13084bac2208Snarayan 13093af08d82Slm66018 kmem_free(vd_devid, bufbytes); 13104bac2208Snarayan ddi_devid_free((ddi_devid_t)devid); 13114bac2208Snarayan 13124bac2208Snarayan return (status); 13134bac2208Snarayan } 13144bac2208Snarayan 13151ae08745Sheppo /* 13161ae08745Sheppo * Define the supported operations once the functions for performing them have 13171ae08745Sheppo * been defined 13181ae08745Sheppo */ 13191ae08745Sheppo static const vds_operation_t vds_operation[] = { 13203af08d82Slm66018 #define X(_s) #_s, _s 13213af08d82Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 13223af08d82Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 13233af08d82Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 13243af08d82Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 13253af08d82Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 13263af08d82Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 13273af08d82Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 13283af08d82Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 13293af08d82Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 13303af08d82Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 13313af08d82Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 13323af08d82Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 13333af08d82Slm66018 #undef X 13341ae08745Sheppo }; 13351ae08745Sheppo 13361ae08745Sheppo static const size_t vds_noperations = 13371ae08745Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0])); 13381ae08745Sheppo 13391ae08745Sheppo /* 1340d10e4ef2Snarayan * Process a task specifying a client I/O request 13411ae08745Sheppo */ 13421ae08745Sheppo static int 1343d10e4ef2Snarayan vd_process_task(vd_task_t *task) 13441ae08745Sheppo { 1345d10e4ef2Snarayan int i, status; 1346d10e4ef2Snarayan vd_t *vd = task->vd; 1347d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 13481ae08745Sheppo 13491ae08745Sheppo 1350d10e4ef2Snarayan ASSERT(vd != NULL); 1351d10e4ef2Snarayan ASSERT(request != NULL); 13521ae08745Sheppo 1353d10e4ef2Snarayan /* Find the requested operation */ 13541ae08745Sheppo for (i = 0; i < vds_noperations; i++) 13551ae08745Sheppo if (request->operation == vds_operation[i].operation) 1356d10e4ef2Snarayan break; 1357d10e4ef2Snarayan if (i == vds_noperations) { 13583af08d82Slm66018 PR0("Unsupported operation %u", request->operation); 13591ae08745Sheppo return (ENOTSUP); 13601ae08745Sheppo } 13611ae08745Sheppo 13627636cb21Slm66018 /* Handle client using absolute disk offsets */ 13637636cb21Slm66018 if ((vd->vdisk_type == VD_DISK_TYPE_DISK) && 13647636cb21Slm66018 (request->slice == UINT8_MAX)) 13657636cb21Slm66018 request->slice = VD_ENTIRE_DISK_SLICE; 13667636cb21Slm66018 13677636cb21Slm66018 /* Range-check slice */ 13687636cb21Slm66018 if (request->slice >= vd->nslices) { 13693af08d82Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 13707636cb21Slm66018 request->slice, (vd->nslices - 1)); 13717636cb21Slm66018 return (EINVAL); 13727636cb21Slm66018 } 13737636cb21Slm66018 13743af08d82Slm66018 PR1("operation : %s", vds_operation[i].namep); 13753af08d82Slm66018 1376d10e4ef2Snarayan /* Start the operation */ 1377d10e4ef2Snarayan if ((status = vds_operation[i].start(task)) != EINPROGRESS) { 13783af08d82Slm66018 PR0("operation : %s returned status %d", 13793af08d82Slm66018 vds_operation[i].namep, status); 1380d10e4ef2Snarayan request->status = status; /* op succeeded or failed */ 1381d10e4ef2Snarayan return (0); /* but request completed */ 13821ae08745Sheppo } 13831ae08745Sheppo 1384d10e4ef2Snarayan ASSERT(vds_operation[i].complete != NULL); /* debug case */ 1385d10e4ef2Snarayan if (vds_operation[i].complete == NULL) { /* non-debug case */ 13863af08d82Slm66018 PR0("Unexpected return of EINPROGRESS " 1387d10e4ef2Snarayan "with no I/O completion handler"); 1388d10e4ef2Snarayan request->status = EIO; /* operation failed */ 1389d10e4ef2Snarayan return (0); /* but request completed */ 13901ae08745Sheppo } 13911ae08745Sheppo 13923af08d82Slm66018 PR1("operation : kick off taskq entry for %s", vds_operation[i].namep); 13933af08d82Slm66018 1394d10e4ef2Snarayan /* Queue a task to complete the operation */ 1395d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->completionq, vds_operation[i].complete, 1396d10e4ef2Snarayan task, DDI_SLEEP); 1397d10e4ef2Snarayan /* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */ 1398d10e4ef2Snarayan ASSERT(status == DDI_SUCCESS); 1399d10e4ef2Snarayan 1400d10e4ef2Snarayan PR1("Operation in progress"); 1401d10e4ef2Snarayan return (EINPROGRESS); /* completion handler will finish request */ 14021ae08745Sheppo } 14031ae08745Sheppo 14041ae08745Sheppo /* 14050a55fbb7Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first 14060a55fbb7Slm66018 * argument match the corresponding remaining arguments; otherwise, return false 14071ae08745Sheppo */ 14080a55fbb7Slm66018 boolean_t 14091ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 14101ae08745Sheppo { 14111ae08745Sheppo return ((tag->vio_msgtype == type) && 14121ae08745Sheppo (tag->vio_subtype == subtype) && 14130a55fbb7Slm66018 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 14141ae08745Sheppo } 14151ae08745Sheppo 14160a55fbb7Slm66018 /* 14170a55fbb7Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported 14180a55fbb7Slm66018 * by this server. 14190a55fbb7Slm66018 */ 14200a55fbb7Slm66018 static boolean_t 14210a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg) 14220a55fbb7Slm66018 { 14230a55fbb7Slm66018 for (int i = 0; i < vds_num_versions; i++) { 14240a55fbb7Slm66018 ASSERT(vds_version[i].major > 0); 14250a55fbb7Slm66018 ASSERT((i == 0) || 14260a55fbb7Slm66018 (vds_version[i].major < vds_version[i-1].major)); 14270a55fbb7Slm66018 14280a55fbb7Slm66018 /* 14290a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 14300a55fbb7Slm66018 * necessary, down to the highest value supported by this 14310a55fbb7Slm66018 * server and return true so this message will get "ack"ed; 14320a55fbb7Slm66018 * the client should also support all minor versions lower 14330a55fbb7Slm66018 * than the value it sent 14340a55fbb7Slm66018 */ 14350a55fbb7Slm66018 if (ver_msg->ver_major == vds_version[i].major) { 14360a55fbb7Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) { 14370a55fbb7Slm66018 PR0("Adjusting minor version from %u to %u", 14380a55fbb7Slm66018 ver_msg->ver_minor, vds_version[i].minor); 14390a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 14400a55fbb7Slm66018 } 14410a55fbb7Slm66018 return (B_TRUE); 14420a55fbb7Slm66018 } 14430a55fbb7Slm66018 14440a55fbb7Slm66018 /* 14450a55fbb7Slm66018 * If the message contains a higher major version number, set 14460a55fbb7Slm66018 * the message's major/minor versions to the current values 14470a55fbb7Slm66018 * and return false, so this message will get "nack"ed with 14480a55fbb7Slm66018 * these values, and the client will potentially try again 14490a55fbb7Slm66018 * with the same or a lower version 14500a55fbb7Slm66018 */ 14510a55fbb7Slm66018 if (ver_msg->ver_major > vds_version[i].major) { 14520a55fbb7Slm66018 ver_msg->ver_major = vds_version[i].major; 14530a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 14540a55fbb7Slm66018 return (B_FALSE); 14550a55fbb7Slm66018 } 14560a55fbb7Slm66018 14570a55fbb7Slm66018 /* 14580a55fbb7Slm66018 * Otherwise, the message's major version is less than the 14590a55fbb7Slm66018 * current major version, so continue the loop to the next 14600a55fbb7Slm66018 * (lower) supported version 14610a55fbb7Slm66018 */ 14620a55fbb7Slm66018 } 14630a55fbb7Slm66018 14640a55fbb7Slm66018 /* 14650a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 14660a55fbb7Slm66018 * message to terminate negotiation 14670a55fbb7Slm66018 */ 14680a55fbb7Slm66018 ver_msg->ver_major = 0; 14690a55fbb7Slm66018 ver_msg->ver_minor = 0; 14700a55fbb7Slm66018 return (B_FALSE); 14710a55fbb7Slm66018 } 14720a55fbb7Slm66018 14730a55fbb7Slm66018 /* 14740a55fbb7Slm66018 * Process a version message from a client. vds expects to receive version 14750a55fbb7Slm66018 * messages from clients seeking service, but never issues version messages 14760a55fbb7Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does 14770a55fbb7Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such 14780a55fbb7Slm66018 * messages as invalid). 14790a55fbb7Slm66018 */ 14801ae08745Sheppo static int 14810a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 14821ae08745Sheppo { 14831ae08745Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 14841ae08745Sheppo 14851ae08745Sheppo 14861ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 14871ae08745Sheppo 14881ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 14891ae08745Sheppo VIO_VER_INFO)) { 14901ae08745Sheppo return (ENOMSG); /* not a version message */ 14911ae08745Sheppo } 14921ae08745Sheppo 14931ae08745Sheppo if (msglen != sizeof (*ver_msg)) { 14943af08d82Slm66018 PR0("Expected %lu-byte version message; " 14951ae08745Sheppo "received %lu bytes", sizeof (*ver_msg), msglen); 14961ae08745Sheppo return (EBADMSG); 14971ae08745Sheppo } 14981ae08745Sheppo 14991ae08745Sheppo if (ver_msg->dev_class != VDEV_DISK) { 15003af08d82Slm66018 PR0("Expected device class %u (disk); received %u", 15011ae08745Sheppo VDEV_DISK, ver_msg->dev_class); 15021ae08745Sheppo return (EBADMSG); 15031ae08745Sheppo } 15041ae08745Sheppo 15050a55fbb7Slm66018 /* 15060a55fbb7Slm66018 * We're talking to the expected kind of client; set our device class 15070a55fbb7Slm66018 * for "ack/nack" back to the client 15080a55fbb7Slm66018 */ 15091ae08745Sheppo ver_msg->dev_class = VDEV_DISK_SERVER; 15100a55fbb7Slm66018 15110a55fbb7Slm66018 /* 15120a55fbb7Slm66018 * Check whether the (valid) version message specifies a version 15130a55fbb7Slm66018 * supported by this server. If the version is not supported, return 15140a55fbb7Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version() 15150a55fbb7Slm66018 * will have updated the message with a supported version for the 15160a55fbb7Slm66018 * client to consider 15170a55fbb7Slm66018 */ 15180a55fbb7Slm66018 if (!vds_supported_version(ver_msg)) 15190a55fbb7Slm66018 return (EBADMSG); 15200a55fbb7Slm66018 15210a55fbb7Slm66018 15220a55fbb7Slm66018 /* 15230a55fbb7Slm66018 * A version has been agreed upon; use the client's SID for 15240a55fbb7Slm66018 * communication on this channel now 15250a55fbb7Slm66018 */ 15260a55fbb7Slm66018 ASSERT(!(vd->initialized & VD_SID)); 15270a55fbb7Slm66018 vd->sid = ver_msg->tag.vio_sid; 15280a55fbb7Slm66018 vd->initialized |= VD_SID; 15290a55fbb7Slm66018 15300a55fbb7Slm66018 /* 15310a55fbb7Slm66018 * When multiple versions are supported, this function should store 15320a55fbb7Slm66018 * the negotiated major and minor version values in the "vd" data 15330a55fbb7Slm66018 * structure to govern further communication; in particular, note that 15340a55fbb7Slm66018 * the client might have specified a lower minor version for the 15350a55fbb7Slm66018 * agreed major version than specifed in the vds_version[] array. The 15360a55fbb7Slm66018 * following assertions should help remind future maintainers to make 15370a55fbb7Slm66018 * the appropriate changes to support multiple versions. 15380a55fbb7Slm66018 */ 15390a55fbb7Slm66018 ASSERT(vds_num_versions == 1); 15400a55fbb7Slm66018 ASSERT(ver_msg->ver_major == vds_version[0].major); 15410a55fbb7Slm66018 ASSERT(ver_msg->ver_minor == vds_version[0].minor); 15420a55fbb7Slm66018 15430a55fbb7Slm66018 PR0("Using major version %u, minor version %u", 15440a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 15451ae08745Sheppo return (0); 15461ae08745Sheppo } 15471ae08745Sheppo 15481ae08745Sheppo static int 15491ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 15501ae08745Sheppo { 15511ae08745Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 15523c96341aSnarayan int status, retry = 0; 15531ae08745Sheppo 15541ae08745Sheppo 15551ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 15561ae08745Sheppo 15571ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 15581ae08745Sheppo VIO_ATTR_INFO)) { 1559d10e4ef2Snarayan PR0("Message is not an attribute message"); 1560d10e4ef2Snarayan return (ENOMSG); 15611ae08745Sheppo } 15621ae08745Sheppo 15631ae08745Sheppo if (msglen != sizeof (*attr_msg)) { 15643af08d82Slm66018 PR0("Expected %lu-byte attribute message; " 15651ae08745Sheppo "received %lu bytes", sizeof (*attr_msg), msglen); 15661ae08745Sheppo return (EBADMSG); 15671ae08745Sheppo } 15681ae08745Sheppo 15691ae08745Sheppo if (attr_msg->max_xfer_sz == 0) { 15703af08d82Slm66018 PR0("Received maximum transfer size of 0 from client"); 15711ae08745Sheppo return (EBADMSG); 15721ae08745Sheppo } 15731ae08745Sheppo 15741ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 15751ae08745Sheppo (attr_msg->xfer_mode != VIO_DRING_MODE)) { 15763af08d82Slm66018 PR0("Client requested unsupported transfer mode"); 15771ae08745Sheppo return (EBADMSG); 15781ae08745Sheppo } 15791ae08745Sheppo 15803c96341aSnarayan /* 15813c96341aSnarayan * check if the underlying disk is ready, if not try accessing 15823c96341aSnarayan * the device again. Open the vdisk device and extract info 15833c96341aSnarayan * about it, as this is needed to respond to the attr info msg 15843c96341aSnarayan */ 15853c96341aSnarayan if ((vd->initialized & VD_DISK_READY) == 0) { 15863c96341aSnarayan PR0("Retry setting up disk (%s)", vd->device_path); 15873c96341aSnarayan do { 15883c96341aSnarayan status = vd_setup_vd(vd); 15893c96341aSnarayan if (status != EAGAIN || ++retry > vds_dev_retries) 15903c96341aSnarayan break; 15913c96341aSnarayan 15923c96341aSnarayan /* incremental delay */ 15933c96341aSnarayan delay(drv_usectohz(vds_dev_delay)); 15943c96341aSnarayan 15953c96341aSnarayan /* if vdisk is no longer enabled - return error */ 15963c96341aSnarayan if (!vd_enabled(vd)) 15973c96341aSnarayan return (ENXIO); 15983c96341aSnarayan 15993c96341aSnarayan } while (status == EAGAIN); 16003c96341aSnarayan 16013c96341aSnarayan if (status) 16023c96341aSnarayan return (ENXIO); 16033c96341aSnarayan 16043c96341aSnarayan vd->initialized |= VD_DISK_READY; 16053c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 16063c96341aSnarayan PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u", 16073c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 16083c96341aSnarayan (vd->pseudo ? "yes" : "no"), 16093c96341aSnarayan (vd->file ? "yes" : "no"), 16103c96341aSnarayan vd->nslices); 16113c96341aSnarayan } 16123c96341aSnarayan 16131ae08745Sheppo /* Success: valid message and transfer mode */ 16141ae08745Sheppo vd->xfer_mode = attr_msg->xfer_mode; 16153af08d82Slm66018 16161ae08745Sheppo if (vd->xfer_mode == VIO_DESC_MODE) { 16173af08d82Slm66018 16181ae08745Sheppo /* 16191ae08745Sheppo * The vd_dring_inband_msg_t contains one cookie; need room 16201ae08745Sheppo * for up to n-1 more cookies, where "n" is the number of full 16211ae08745Sheppo * pages plus possibly one partial page required to cover 16221ae08745Sheppo * "max_xfer_sz". Add room for one more cookie if 16231ae08745Sheppo * "max_xfer_sz" isn't an integral multiple of the page size. 16241ae08745Sheppo * Must first get the maximum transfer size in bytes. 16251ae08745Sheppo */ 16261ae08745Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 16271ae08745Sheppo attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 16281ae08745Sheppo attr_msg->max_xfer_sz; 16291ae08745Sheppo size_t max_inband_msglen = 16301ae08745Sheppo sizeof (vd_dring_inband_msg_t) + 16311ae08745Sheppo ((max_xfer_bytes/PAGESIZE + 16321ae08745Sheppo ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 16331ae08745Sheppo (sizeof (ldc_mem_cookie_t))); 16341ae08745Sheppo 16351ae08745Sheppo /* 16361ae08745Sheppo * Set the maximum expected message length to 16371ae08745Sheppo * accommodate in-band-descriptor messages with all 16381ae08745Sheppo * their cookies 16391ae08745Sheppo */ 16401ae08745Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 1641d10e4ef2Snarayan 1642d10e4ef2Snarayan /* 1643d10e4ef2Snarayan * Initialize the data structure for processing in-band I/O 1644d10e4ef2Snarayan * request descriptors 1645d10e4ef2Snarayan */ 1646d10e4ef2Snarayan vd->inband_task.vd = vd; 16473af08d82Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 1648d10e4ef2Snarayan vd->inband_task.index = 0; 1649d10e4ef2Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 16501ae08745Sheppo } 16511ae08745Sheppo 1652e1ebb9ecSlm66018 /* Return the device's block size and max transfer size to the client */ 1653e1ebb9ecSlm66018 attr_msg->vdisk_block_size = DEV_BSIZE; 1654e1ebb9ecSlm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz; 1655e1ebb9ecSlm66018 16561ae08745Sheppo attr_msg->vdisk_size = vd->vdisk_size; 16571ae08745Sheppo attr_msg->vdisk_type = vd->vdisk_type; 16581ae08745Sheppo attr_msg->operations = vds_operations; 16591ae08745Sheppo PR0("%s", VD_CLIENT(vd)); 16603af08d82Slm66018 16613af08d82Slm66018 ASSERT(vd->dring_task == NULL); 16623af08d82Slm66018 16631ae08745Sheppo return (0); 16641ae08745Sheppo } 16651ae08745Sheppo 16661ae08745Sheppo static int 16671ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 16681ae08745Sheppo { 16691ae08745Sheppo int status; 16701ae08745Sheppo size_t expected; 16711ae08745Sheppo ldc_mem_info_t dring_minfo; 16721ae08745Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 16731ae08745Sheppo 16741ae08745Sheppo 16751ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 16761ae08745Sheppo 16771ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 16781ae08745Sheppo VIO_DRING_REG)) { 1679d10e4ef2Snarayan PR0("Message is not a register-dring message"); 1680d10e4ef2Snarayan return (ENOMSG); 16811ae08745Sheppo } 16821ae08745Sheppo 16831ae08745Sheppo if (msglen < sizeof (*reg_msg)) { 16843af08d82Slm66018 PR0("Expected at least %lu-byte register-dring message; " 16851ae08745Sheppo "received %lu bytes", sizeof (*reg_msg), msglen); 16861ae08745Sheppo return (EBADMSG); 16871ae08745Sheppo } 16881ae08745Sheppo 16891ae08745Sheppo expected = sizeof (*reg_msg) + 16901ae08745Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 16911ae08745Sheppo if (msglen != expected) { 16923af08d82Slm66018 PR0("Expected %lu-byte register-dring message; " 16931ae08745Sheppo "received %lu bytes", expected, msglen); 16941ae08745Sheppo return (EBADMSG); 16951ae08745Sheppo } 16961ae08745Sheppo 16971ae08745Sheppo if (vd->initialized & VD_DRING) { 16983af08d82Slm66018 PR0("A dring was previously registered; only support one"); 16991ae08745Sheppo return (EBADMSG); 17001ae08745Sheppo } 17011ae08745Sheppo 1702d10e4ef2Snarayan if (reg_msg->num_descriptors > INT32_MAX) { 17033af08d82Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 1704d10e4ef2Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 1705d10e4ef2Snarayan return (EBADMSG); 1706d10e4ef2Snarayan } 1707d10e4ef2Snarayan 17081ae08745Sheppo if (reg_msg->ncookies != 1) { 17091ae08745Sheppo /* 17101ae08745Sheppo * In addition to fixing the assertion in the success case 17111ae08745Sheppo * below, supporting drings which require more than one 17121ae08745Sheppo * "cookie" requires increasing the value of vd->max_msglen 17131ae08745Sheppo * somewhere in the code path prior to receiving the message 17141ae08745Sheppo * which results in calling this function. Note that without 17151ae08745Sheppo * making this change, the larger message size required to 17161ae08745Sheppo * accommodate multiple cookies cannot be successfully 17171ae08745Sheppo * received, so this function will not even get called. 17181ae08745Sheppo * Gracefully accommodating more dring cookies might 17191ae08745Sheppo * reasonably demand exchanging an additional attribute or 17201ae08745Sheppo * making a minor protocol adjustment 17211ae08745Sheppo */ 17223af08d82Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 17231ae08745Sheppo return (EBADMSG); 17241ae08745Sheppo } 17251ae08745Sheppo 17261ae08745Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 17271ae08745Sheppo reg_msg->ncookies, reg_msg->num_descriptors, 17284bac2208Snarayan reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle); 17291ae08745Sheppo if (status != 0) { 17303af08d82Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status); 17311ae08745Sheppo return (status); 17321ae08745Sheppo } 17331ae08745Sheppo 17341ae08745Sheppo /* 17351ae08745Sheppo * To remove the need for this assertion, must call 17361ae08745Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 17371ae08745Sheppo * successful call to ldc_mem_dring_map() 17381ae08745Sheppo */ 17391ae08745Sheppo ASSERT(reg_msg->ncookies == 1); 17401ae08745Sheppo 17411ae08745Sheppo if ((status = 17421ae08745Sheppo ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 17433af08d82Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status); 17441ae08745Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 17453af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 17461ae08745Sheppo return (status); 17471ae08745Sheppo } 17481ae08745Sheppo 17491ae08745Sheppo if (dring_minfo.vaddr == NULL) { 17503af08d82Slm66018 PR0("Descriptor ring virtual address is NULL"); 17510a55fbb7Slm66018 return (ENXIO); 17521ae08745Sheppo } 17531ae08745Sheppo 17541ae08745Sheppo 1755d10e4ef2Snarayan /* Initialize for valid message and mapped dring */ 17561ae08745Sheppo PR1("descriptor size = %u, dring length = %u", 17571ae08745Sheppo vd->descriptor_size, vd->dring_len); 17581ae08745Sheppo vd->initialized |= VD_DRING; 17591ae08745Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */ 17601ae08745Sheppo vd->dring = dring_minfo.vaddr; 17611ae08745Sheppo vd->descriptor_size = reg_msg->descriptor_size; 17621ae08745Sheppo vd->dring_len = reg_msg->num_descriptors; 17631ae08745Sheppo reg_msg->dring_ident = vd->dring_ident; 1764d10e4ef2Snarayan 1765d10e4ef2Snarayan /* 1766d10e4ef2Snarayan * Allocate and initialize a "shadow" array of data structures for 1767d10e4ef2Snarayan * tasks to process I/O requests in dring elements 1768d10e4ef2Snarayan */ 1769d10e4ef2Snarayan vd->dring_task = 1770d10e4ef2Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 1771d10e4ef2Snarayan for (int i = 0; i < vd->dring_len; i++) { 1772d10e4ef2Snarayan vd->dring_task[i].vd = vd; 1773d10e4ef2Snarayan vd->dring_task[i].index = i; 1774d10e4ef2Snarayan vd->dring_task[i].request = &VD_DRING_ELEM(i)->payload; 17754bac2208Snarayan 17764bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, 17774bac2208Snarayan &(vd->dring_task[i].mhdl)); 17784bac2208Snarayan if (status) { 17793af08d82Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status); 17804bac2208Snarayan return (ENXIO); 17814bac2208Snarayan } 17823af08d82Slm66018 17833af08d82Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 1784d10e4ef2Snarayan } 1785d10e4ef2Snarayan 17861ae08745Sheppo return (0); 17871ae08745Sheppo } 17881ae08745Sheppo 17891ae08745Sheppo static int 17901ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 17911ae08745Sheppo { 17921ae08745Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 17931ae08745Sheppo 17941ae08745Sheppo 17951ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 17961ae08745Sheppo 17971ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 17981ae08745Sheppo VIO_DRING_UNREG)) { 1799d10e4ef2Snarayan PR0("Message is not an unregister-dring message"); 1800d10e4ef2Snarayan return (ENOMSG); 18011ae08745Sheppo } 18021ae08745Sheppo 18031ae08745Sheppo if (msglen != sizeof (*unreg_msg)) { 18043af08d82Slm66018 PR0("Expected %lu-byte unregister-dring message; " 18051ae08745Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen); 18061ae08745Sheppo return (EBADMSG); 18071ae08745Sheppo } 18081ae08745Sheppo 18091ae08745Sheppo if (unreg_msg->dring_ident != vd->dring_ident) { 18103af08d82Slm66018 PR0("Expected dring ident %lu; received %lu", 18111ae08745Sheppo vd->dring_ident, unreg_msg->dring_ident); 18121ae08745Sheppo return (EBADMSG); 18131ae08745Sheppo } 18141ae08745Sheppo 18151ae08745Sheppo return (0); 18161ae08745Sheppo } 18171ae08745Sheppo 18181ae08745Sheppo static int 18191ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen) 18201ae08745Sheppo { 18211ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 18221ae08745Sheppo 1823d10e4ef2Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 1824d10e4ef2Snarayan PR0("Message is not an RDX message"); 1825d10e4ef2Snarayan return (ENOMSG); 1826d10e4ef2Snarayan } 18271ae08745Sheppo 18281ae08745Sheppo if (msglen != sizeof (vio_rdx_msg_t)) { 18293af08d82Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes", 18301ae08745Sheppo sizeof (vio_rdx_msg_t), msglen); 18311ae08745Sheppo return (EBADMSG); 18321ae08745Sheppo } 18331ae08745Sheppo 1834d10e4ef2Snarayan PR0("Valid RDX message"); 18351ae08745Sheppo return (0); 18361ae08745Sheppo } 18371ae08745Sheppo 18381ae08745Sheppo static int 18391ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num) 18401ae08745Sheppo { 18411ae08745Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 18423af08d82Slm66018 PR0("Received seq_num %lu; expected %lu", 18431ae08745Sheppo seq_num, (vd->seq_num + 1)); 18443af08d82Slm66018 PR0("initiating soft reset"); 1845d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 18461ae08745Sheppo return (1); 18471ae08745Sheppo } 18481ae08745Sheppo 18491ae08745Sheppo vd->seq_num = seq_num; 18501ae08745Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 18511ae08745Sheppo return (0); 18521ae08745Sheppo } 18531ae08745Sheppo 18541ae08745Sheppo /* 18551ae08745Sheppo * Return the expected size of an inband-descriptor message with all the 18561ae08745Sheppo * cookies it claims to include 18571ae08745Sheppo */ 18581ae08745Sheppo static size_t 18591ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg) 18601ae08745Sheppo { 18611ae08745Sheppo return ((sizeof (*msg)) + 18621ae08745Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 18631ae08745Sheppo } 18641ae08745Sheppo 18651ae08745Sheppo /* 18661ae08745Sheppo * Process an in-band descriptor message: used with clients like OBP, with 18671ae08745Sheppo * which vds exchanges descriptors within VIO message payloads, rather than 18681ae08745Sheppo * operating on them within a descriptor ring 18691ae08745Sheppo */ 18701ae08745Sheppo static int 18713af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 18721ae08745Sheppo { 18731ae08745Sheppo size_t expected; 18741ae08745Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 18751ae08745Sheppo 18761ae08745Sheppo 18771ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 18781ae08745Sheppo 18791ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 1880d10e4ef2Snarayan VIO_DESC_DATA)) { 1881d10e4ef2Snarayan PR1("Message is not an in-band-descriptor message"); 1882d10e4ef2Snarayan return (ENOMSG); 1883d10e4ef2Snarayan } 18841ae08745Sheppo 18851ae08745Sheppo if (msglen < sizeof (*desc_msg)) { 18863af08d82Slm66018 PR0("Expected at least %lu-byte descriptor message; " 18871ae08745Sheppo "received %lu bytes", sizeof (*desc_msg), msglen); 18881ae08745Sheppo return (EBADMSG); 18891ae08745Sheppo } 18901ae08745Sheppo 18911ae08745Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) { 18923af08d82Slm66018 PR0("Expected %lu-byte descriptor message; " 18931ae08745Sheppo "received %lu bytes", expected, msglen); 18941ae08745Sheppo return (EBADMSG); 18951ae08745Sheppo } 18961ae08745Sheppo 1897d10e4ef2Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 18981ae08745Sheppo return (EBADMSG); 18991ae08745Sheppo 1900d10e4ef2Snarayan /* 1901d10e4ef2Snarayan * Valid message: Set up the in-band descriptor task and process the 1902d10e4ef2Snarayan * request. Arrange to acknowledge the client's message, unless an 1903d10e4ef2Snarayan * error processing the descriptor task results in setting 1904d10e4ef2Snarayan * VIO_SUBTYPE_NACK 1905d10e4ef2Snarayan */ 1906d10e4ef2Snarayan PR1("Valid in-band-descriptor message"); 1907d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 19083af08d82Slm66018 19093af08d82Slm66018 ASSERT(vd->inband_task.msg != NULL); 19103af08d82Slm66018 19113af08d82Slm66018 bcopy(msg, vd->inband_task.msg, msglen); 1912d10e4ef2Snarayan vd->inband_task.msglen = msglen; 19133af08d82Slm66018 19143af08d82Slm66018 /* 19153af08d82Slm66018 * The task request is now the payload of the message 19163af08d82Slm66018 * that was just copied into the body of the task. 19173af08d82Slm66018 */ 19183af08d82Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 1919d10e4ef2Snarayan vd->inband_task.request = &desc_msg->payload; 19203af08d82Slm66018 1921d10e4ef2Snarayan return (vd_process_task(&vd->inband_task)); 19221ae08745Sheppo } 19231ae08745Sheppo 19241ae08745Sheppo static int 1925d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 19263af08d82Slm66018 vio_msg_t *msg, size_t msglen) 19271ae08745Sheppo { 19281ae08745Sheppo int status; 1929d10e4ef2Snarayan boolean_t ready; 1930d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 19311ae08745Sheppo 19321ae08745Sheppo 1933d10e4ef2Snarayan /* Accept the updated dring element */ 1934d10e4ef2Snarayan if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 19353af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", status); 19361ae08745Sheppo return (status); 19371ae08745Sheppo } 1938d10e4ef2Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY); 1939d10e4ef2Snarayan if (ready) { 1940d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED; 1941d10e4ef2Snarayan } else { 19423af08d82Slm66018 PR0("descriptor %u not ready", idx); 1943d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 1944d10e4ef2Snarayan } 1945d10e4ef2Snarayan if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 19463af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", status); 19471ae08745Sheppo return (status); 19481ae08745Sheppo } 1949d10e4ef2Snarayan if (!ready) 1950d10e4ef2Snarayan return (EBUSY); 19511ae08745Sheppo 19521ae08745Sheppo 1953d10e4ef2Snarayan /* Initialize a task and process the accepted element */ 1954d10e4ef2Snarayan PR1("Processing dring element %u", idx); 1955d10e4ef2Snarayan vd->dring_task[idx].type = type; 19563af08d82Slm66018 19573af08d82Slm66018 /* duplicate msg buf for cookies etc. */ 19583af08d82Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen); 19593af08d82Slm66018 1960d10e4ef2Snarayan vd->dring_task[idx].msglen = msglen; 1961d10e4ef2Snarayan if ((status = vd_process_task(&vd->dring_task[idx])) != EINPROGRESS) 19623c96341aSnarayan status = vd_mark_elem_done(vd, idx, 19633c96341aSnarayan vd->dring_task[idx].request->status, 19643c96341aSnarayan vd->dring_task[idx].request->nbytes); 19651ae08745Sheppo 19661ae08745Sheppo return (status); 19671ae08745Sheppo } 19681ae08745Sheppo 19691ae08745Sheppo static int 1970d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end, 19713af08d82Slm66018 vio_msg_t *msg, size_t msglen) 1972d10e4ef2Snarayan { 1973d10e4ef2Snarayan int i, n, nelem, status = 0; 1974d10e4ef2Snarayan boolean_t inprogress = B_FALSE; 1975d10e4ef2Snarayan vd_task_type_t type; 1976d10e4ef2Snarayan 1977d10e4ef2Snarayan 1978d10e4ef2Snarayan ASSERT(start >= 0); 1979d10e4ef2Snarayan ASSERT(end >= 0); 1980d10e4ef2Snarayan 1981d10e4ef2Snarayan /* 1982d10e4ef2Snarayan * Arrange to acknowledge the client's message, unless an error 1983d10e4ef2Snarayan * processing one of the dring elements results in setting 1984d10e4ef2Snarayan * VIO_SUBTYPE_NACK 1985d10e4ef2Snarayan */ 1986d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 1987d10e4ef2Snarayan 1988d10e4ef2Snarayan /* 1989d10e4ef2Snarayan * Process the dring elements in the range 1990d10e4ef2Snarayan */ 1991d10e4ef2Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 1992d10e4ef2Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 1993d10e4ef2Snarayan ((vio_dring_msg_t *)msg)->end_idx = i; 1994d10e4ef2Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 19953af08d82Slm66018 status = vd_process_element(vd, type, i, msg, msglen); 1996d10e4ef2Snarayan if (status == EINPROGRESS) 1997d10e4ef2Snarayan inprogress = B_TRUE; 1998d10e4ef2Snarayan else if (status != 0) 1999d10e4ef2Snarayan break; 2000d10e4ef2Snarayan } 2001d10e4ef2Snarayan 2002d10e4ef2Snarayan /* 2003d10e4ef2Snarayan * If some, but not all, operations of a multi-element range are in 2004d10e4ef2Snarayan * progress, wait for other operations to complete before returning 2005d10e4ef2Snarayan * (which will result in "ack" or "nack" of the message). Note that 2006d10e4ef2Snarayan * all outstanding operations will need to complete, not just the ones 2007d10e4ef2Snarayan * corresponding to the current range of dring elements; howevever, as 2008d10e4ef2Snarayan * this situation is an error case, performance is less critical. 2009d10e4ef2Snarayan */ 2010d10e4ef2Snarayan if ((nelem > 1) && (status != EINPROGRESS) && inprogress) 2011d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 2012d10e4ef2Snarayan 2013d10e4ef2Snarayan return (status); 2014d10e4ef2Snarayan } 2015d10e4ef2Snarayan 2016d10e4ef2Snarayan static int 20173af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 20181ae08745Sheppo { 20191ae08745Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 20201ae08745Sheppo 20211ae08745Sheppo 20221ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 20231ae08745Sheppo 20241ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 20251ae08745Sheppo VIO_DRING_DATA)) { 2026d10e4ef2Snarayan PR1("Message is not a dring-data message"); 2027d10e4ef2Snarayan return (ENOMSG); 20281ae08745Sheppo } 20291ae08745Sheppo 20301ae08745Sheppo if (msglen != sizeof (*dring_msg)) { 20313af08d82Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes", 20321ae08745Sheppo sizeof (*dring_msg), msglen); 20331ae08745Sheppo return (EBADMSG); 20341ae08745Sheppo } 20351ae08745Sheppo 2036d10e4ef2Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 20371ae08745Sheppo return (EBADMSG); 20381ae08745Sheppo 20391ae08745Sheppo if (dring_msg->dring_ident != vd->dring_ident) { 20403af08d82Slm66018 PR0("Expected dring ident %lu; received ident %lu", 20411ae08745Sheppo vd->dring_ident, dring_msg->dring_ident); 20421ae08745Sheppo return (EBADMSG); 20431ae08745Sheppo } 20441ae08745Sheppo 2045d10e4ef2Snarayan if (dring_msg->start_idx >= vd->dring_len) { 20463af08d82Slm66018 PR0("\"start_idx\" = %u; must be less than %u", 2047d10e4ef2Snarayan dring_msg->start_idx, vd->dring_len); 2048d10e4ef2Snarayan return (EBADMSG); 2049d10e4ef2Snarayan } 20501ae08745Sheppo 2051d10e4ef2Snarayan if ((dring_msg->end_idx < 0) || 2052d10e4ef2Snarayan (dring_msg->end_idx >= vd->dring_len)) { 20533af08d82Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 2054d10e4ef2Snarayan dring_msg->end_idx, vd->dring_len); 2055d10e4ef2Snarayan return (EBADMSG); 2056d10e4ef2Snarayan } 2057d10e4ef2Snarayan 2058d10e4ef2Snarayan /* Valid message; process range of updated dring elements */ 2059d10e4ef2Snarayan PR1("Processing descriptor range, start = %u, end = %u", 2060d10e4ef2Snarayan dring_msg->start_idx, dring_msg->end_idx); 2061d10e4ef2Snarayan return (vd_process_element_range(vd, dring_msg->start_idx, 20623af08d82Slm66018 dring_msg->end_idx, msg, msglen)); 20631ae08745Sheppo } 20641ae08745Sheppo 20651ae08745Sheppo static int 20661ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 20671ae08745Sheppo { 20681ae08745Sheppo int retry, status; 20691ae08745Sheppo size_t size = *nbytes; 20701ae08745Sheppo 20711ae08745Sheppo 20721ae08745Sheppo for (retry = 0, status = ETIMEDOUT; 20731ae08745Sheppo retry < vds_ldc_retries && status == ETIMEDOUT; 20741ae08745Sheppo retry++) { 20751ae08745Sheppo PR1("ldc_read() attempt %d", (retry + 1)); 20761ae08745Sheppo *nbytes = size; 20771ae08745Sheppo status = ldc_read(ldc_handle, msg, nbytes); 20781ae08745Sheppo } 20791ae08745Sheppo 20803af08d82Slm66018 if (status) { 20813af08d82Slm66018 PR0("ldc_read() returned errno %d", status); 20823af08d82Slm66018 if (status != ECONNRESET) 20833af08d82Slm66018 return (ENOMSG); 20841ae08745Sheppo return (status); 20851ae08745Sheppo } else if (*nbytes == 0) { 20861ae08745Sheppo PR1("ldc_read() returned 0 and no message read"); 20871ae08745Sheppo return (ENOMSG); 20881ae08745Sheppo } 20891ae08745Sheppo 20901ae08745Sheppo PR1("RCVD %lu-byte message", *nbytes); 20911ae08745Sheppo return (0); 20921ae08745Sheppo } 20931ae08745Sheppo 20941ae08745Sheppo static int 20953af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 20961ae08745Sheppo { 20971ae08745Sheppo int status; 20981ae08745Sheppo 20991ae08745Sheppo 21001ae08745Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 21011ae08745Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env); 21023af08d82Slm66018 #ifdef DEBUG 21033af08d82Slm66018 vd_decode_tag(msg); 21043af08d82Slm66018 #endif 21051ae08745Sheppo 21061ae08745Sheppo /* 21071ae08745Sheppo * Validate session ID up front, since it applies to all messages 21081ae08745Sheppo * once set 21091ae08745Sheppo */ 21101ae08745Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 21113af08d82Slm66018 PR0("Expected SID %u, received %u", vd->sid, 21121ae08745Sheppo msg->tag.vio_sid); 21131ae08745Sheppo return (EBADMSG); 21141ae08745Sheppo } 21151ae08745Sheppo 21163af08d82Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 21171ae08745Sheppo 21181ae08745Sheppo /* 21191ae08745Sheppo * Process the received message based on connection state 21201ae08745Sheppo */ 21211ae08745Sheppo switch (vd->state) { 21221ae08745Sheppo case VD_STATE_INIT: /* expect version message */ 21230a55fbb7Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 21241ae08745Sheppo return (status); 21251ae08745Sheppo 21261ae08745Sheppo /* Version negotiated, move to that state */ 21271ae08745Sheppo vd->state = VD_STATE_VER; 21281ae08745Sheppo return (0); 21291ae08745Sheppo 21301ae08745Sheppo case VD_STATE_VER: /* expect attribute message */ 21311ae08745Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 21321ae08745Sheppo return (status); 21331ae08745Sheppo 21341ae08745Sheppo /* Attributes exchanged, move to that state */ 21351ae08745Sheppo vd->state = VD_STATE_ATTR; 21361ae08745Sheppo return (0); 21371ae08745Sheppo 21381ae08745Sheppo case VD_STATE_ATTR: 21391ae08745Sheppo switch (vd->xfer_mode) { 21401ae08745Sheppo case VIO_DESC_MODE: /* expect RDX message */ 21411ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0) 21421ae08745Sheppo return (status); 21431ae08745Sheppo 21441ae08745Sheppo /* Ready to receive in-band descriptors */ 21451ae08745Sheppo vd->state = VD_STATE_DATA; 21461ae08745Sheppo return (0); 21471ae08745Sheppo 21481ae08745Sheppo case VIO_DRING_MODE: /* expect register-dring message */ 21491ae08745Sheppo if ((status = 21501ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 21511ae08745Sheppo return (status); 21521ae08745Sheppo 21531ae08745Sheppo /* One dring negotiated, move to that state */ 21541ae08745Sheppo vd->state = VD_STATE_DRING; 21551ae08745Sheppo return (0); 21561ae08745Sheppo 21571ae08745Sheppo default: 21581ae08745Sheppo ASSERT("Unsupported transfer mode"); 21593af08d82Slm66018 PR0("Unsupported transfer mode"); 21601ae08745Sheppo return (ENOTSUP); 21611ae08745Sheppo } 21621ae08745Sheppo 21631ae08745Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 21641ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) { 21651ae08745Sheppo /* Ready to receive data */ 21661ae08745Sheppo vd->state = VD_STATE_DATA; 21671ae08745Sheppo return (0); 21681ae08745Sheppo } else if (status != ENOMSG) { 21691ae08745Sheppo return (status); 21701ae08745Sheppo } 21711ae08745Sheppo 21721ae08745Sheppo 21731ae08745Sheppo /* 21741ae08745Sheppo * If another register-dring message is received, stay in 21751ae08745Sheppo * dring state in case the client sends RDX; although the 21761ae08745Sheppo * protocol allows multiple drings, this server does not 21771ae08745Sheppo * support using more than one 21781ae08745Sheppo */ 21791ae08745Sheppo if ((status = 21801ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 21811ae08745Sheppo return (status); 21821ae08745Sheppo 21831ae08745Sheppo /* 21841ae08745Sheppo * Acknowledge an unregister-dring message, but reset the 21851ae08745Sheppo * connection anyway: Although the protocol allows 21861ae08745Sheppo * unregistering drings, this server cannot serve a vdisk 21871ae08745Sheppo * without its only dring 21881ae08745Sheppo */ 21891ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 21901ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 21911ae08745Sheppo 21921ae08745Sheppo case VD_STATE_DATA: 21931ae08745Sheppo switch (vd->xfer_mode) { 21941ae08745Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */ 21953af08d82Slm66018 return (vd_process_desc_msg(vd, msg, msglen)); 21961ae08745Sheppo 21971ae08745Sheppo case VIO_DRING_MODE: /* expect dring-data or unreg-dring */ 21981ae08745Sheppo /* 21991ae08745Sheppo * Typically expect dring-data messages, so handle 22001ae08745Sheppo * them first 22011ae08745Sheppo */ 22021ae08745Sheppo if ((status = vd_process_dring_msg(vd, msg, 22033af08d82Slm66018 msglen)) != ENOMSG) 22041ae08745Sheppo return (status); 22051ae08745Sheppo 22061ae08745Sheppo /* 22071ae08745Sheppo * Acknowledge an unregister-dring message, but reset 22081ae08745Sheppo * the connection anyway: Although the protocol 22091ae08745Sheppo * allows unregistering drings, this server cannot 22101ae08745Sheppo * serve a vdisk without its only dring 22111ae08745Sheppo */ 22121ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 22131ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 22141ae08745Sheppo 22151ae08745Sheppo default: 22161ae08745Sheppo ASSERT("Unsupported transfer mode"); 22173af08d82Slm66018 PR0("Unsupported transfer mode"); 22181ae08745Sheppo return (ENOTSUP); 22191ae08745Sheppo } 22201ae08745Sheppo 22211ae08745Sheppo default: 22221ae08745Sheppo ASSERT("Invalid client connection state"); 22233af08d82Slm66018 PR0("Invalid client connection state"); 22241ae08745Sheppo return (ENOTSUP); 22251ae08745Sheppo } 22261ae08745Sheppo } 22271ae08745Sheppo 2228d10e4ef2Snarayan static int 22293af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 22301ae08745Sheppo { 22311ae08745Sheppo int status; 22321ae08745Sheppo boolean_t reset_ldc = B_FALSE; 22331ae08745Sheppo 22341ae08745Sheppo 22351ae08745Sheppo /* 22361ae08745Sheppo * Check that the message is at least big enough for a "tag", so that 22371ae08745Sheppo * message processing can proceed based on tag-specified message type 22381ae08745Sheppo */ 22391ae08745Sheppo if (msglen < sizeof (vio_msg_tag_t)) { 22403af08d82Slm66018 PR0("Received short (%lu-byte) message", msglen); 22411ae08745Sheppo /* Can't "nack" short message, so drop the big hammer */ 22423af08d82Slm66018 PR0("initiating full reset"); 2243d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 2244d10e4ef2Snarayan return (EBADMSG); 22451ae08745Sheppo } 22461ae08745Sheppo 22471ae08745Sheppo /* 22481ae08745Sheppo * Process the message 22491ae08745Sheppo */ 22503af08d82Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) { 22511ae08745Sheppo case 0: 22521ae08745Sheppo /* "ack" valid, successfully-processed messages */ 22531ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 22541ae08745Sheppo break; 22551ae08745Sheppo 2256d10e4ef2Snarayan case EINPROGRESS: 2257d10e4ef2Snarayan /* The completion handler will "ack" or "nack" the message */ 2258d10e4ef2Snarayan return (EINPROGRESS); 22591ae08745Sheppo case ENOMSG: 22603af08d82Slm66018 PR0("Received unexpected message"); 22611ae08745Sheppo _NOTE(FALLTHROUGH); 22621ae08745Sheppo case EBADMSG: 22631ae08745Sheppo case ENOTSUP: 22641ae08745Sheppo /* "nack" invalid messages */ 22651ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 22661ae08745Sheppo break; 22671ae08745Sheppo 22681ae08745Sheppo default: 22691ae08745Sheppo /* "nack" failed messages */ 22701ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 22711ae08745Sheppo /* An LDC error probably occurred, so try resetting it */ 22721ae08745Sheppo reset_ldc = B_TRUE; 22731ae08745Sheppo break; 22741ae08745Sheppo } 22751ae08745Sheppo 22763af08d82Slm66018 PR1("\tResulting in state %d (%s)", vd->state, 22773af08d82Slm66018 vd_decode_state(vd->state)); 22783af08d82Slm66018 2279d10e4ef2Snarayan /* Send the "ack" or "nack" to the client */ 22801ae08745Sheppo PR1("Sending %s", 22811ae08745Sheppo (msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 22821ae08745Sheppo if (send_msg(vd->ldc_handle, msg, msglen) != 0) 22831ae08745Sheppo reset_ldc = B_TRUE; 22841ae08745Sheppo 2285d10e4ef2Snarayan /* Arrange to reset the connection for nack'ed or failed messages */ 22863af08d82Slm66018 if ((status != 0) || reset_ldc) { 22873af08d82Slm66018 PR0("initiating %s reset", 22883af08d82Slm66018 (reset_ldc) ? "full" : "soft"); 2289d10e4ef2Snarayan vd_need_reset(vd, reset_ldc); 22903af08d82Slm66018 } 2291d10e4ef2Snarayan 2292d10e4ef2Snarayan return (status); 2293d10e4ef2Snarayan } 2294d10e4ef2Snarayan 2295d10e4ef2Snarayan static boolean_t 2296d10e4ef2Snarayan vd_enabled(vd_t *vd) 2297d10e4ef2Snarayan { 2298d10e4ef2Snarayan boolean_t enabled; 2299d10e4ef2Snarayan 2300d10e4ef2Snarayan 2301d10e4ef2Snarayan mutex_enter(&vd->lock); 2302d10e4ef2Snarayan enabled = vd->enabled; 2303d10e4ef2Snarayan mutex_exit(&vd->lock); 2304d10e4ef2Snarayan return (enabled); 23051ae08745Sheppo } 23061ae08745Sheppo 23071ae08745Sheppo static void 23080a55fbb7Slm66018 vd_recv_msg(void *arg) 23091ae08745Sheppo { 23101ae08745Sheppo vd_t *vd = (vd_t *)arg; 23113af08d82Slm66018 int rv = 0, status = 0; 23121ae08745Sheppo 23131ae08745Sheppo ASSERT(vd != NULL); 23143af08d82Slm66018 2315d10e4ef2Snarayan PR2("New task to receive incoming message(s)"); 23163af08d82Slm66018 23173af08d82Slm66018 2318d10e4ef2Snarayan while (vd_enabled(vd) && status == 0) { 2319d10e4ef2Snarayan size_t msglen, msgsize; 23203af08d82Slm66018 ldc_status_t lstatus; 2321d10e4ef2Snarayan 23220a55fbb7Slm66018 /* 2323d10e4ef2Snarayan * Receive and process a message 23240a55fbb7Slm66018 */ 2325d10e4ef2Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */ 23263af08d82Slm66018 23273af08d82Slm66018 /* 23283af08d82Slm66018 * check if channel is UP - else break out of loop 23293af08d82Slm66018 */ 23303af08d82Slm66018 status = ldc_status(vd->ldc_handle, &lstatus); 23313af08d82Slm66018 if (lstatus != LDC_UP) { 23323af08d82Slm66018 PR0("channel not up (status=%d), exiting recv loop\n", 23333af08d82Slm66018 lstatus); 23343af08d82Slm66018 break; 23353af08d82Slm66018 } 23363af08d82Slm66018 23373af08d82Slm66018 ASSERT(vd->max_msglen != 0); 23383af08d82Slm66018 2339d10e4ef2Snarayan msgsize = vd->max_msglen; /* stable copy for alloc/free */ 23403af08d82Slm66018 msglen = msgsize; /* actual len after recv_msg() */ 23413af08d82Slm66018 23423af08d82Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 23433af08d82Slm66018 switch (status) { 23443af08d82Slm66018 case 0: 23453af08d82Slm66018 rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp, 23463af08d82Slm66018 msglen); 23473af08d82Slm66018 /* check if max_msglen changed */ 23483af08d82Slm66018 if (msgsize != vd->max_msglen) { 23493af08d82Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 23503af08d82Slm66018 msgsize, vd->max_msglen); 23513af08d82Slm66018 kmem_free(vd->vio_msgp, msgsize); 23523af08d82Slm66018 vd->vio_msgp = 23533af08d82Slm66018 kmem_alloc(vd->max_msglen, KM_SLEEP); 23543af08d82Slm66018 } 23553af08d82Slm66018 if (rv == EINPROGRESS) 23563af08d82Slm66018 continue; 23573af08d82Slm66018 break; 23583af08d82Slm66018 23593af08d82Slm66018 case ENOMSG: 23603af08d82Slm66018 break; 23613af08d82Slm66018 23623af08d82Slm66018 case ECONNRESET: 23633af08d82Slm66018 PR0("initiating soft reset (ECONNRESET)\n"); 23643af08d82Slm66018 vd_need_reset(vd, B_FALSE); 23653af08d82Slm66018 status = 0; 23663af08d82Slm66018 break; 23673af08d82Slm66018 23683af08d82Slm66018 default: 2369d10e4ef2Snarayan /* Probably an LDC failure; arrange to reset it */ 23703af08d82Slm66018 PR0("initiating full reset (status=0x%x)", status); 2371d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 23723af08d82Slm66018 break; 23730a55fbb7Slm66018 } 23741ae08745Sheppo } 23753af08d82Slm66018 2376d10e4ef2Snarayan PR2("Task finished"); 23770a55fbb7Slm66018 } 23780a55fbb7Slm66018 23790a55fbb7Slm66018 static uint_t 23801ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg) 23811ae08745Sheppo { 23821ae08745Sheppo vd_t *vd = (vd_t *)(void *)arg; 23833af08d82Slm66018 int status; 23841ae08745Sheppo 23851ae08745Sheppo ASSERT(vd != NULL); 2386d10e4ef2Snarayan 2387d10e4ef2Snarayan if (!vd_enabled(vd)) 2388d10e4ef2Snarayan return (LDC_SUCCESS); 2389d10e4ef2Snarayan 23903af08d82Slm66018 if (event & LDC_EVT_DOWN) { 239134683adeSsg70180 PR0("LDC_EVT_DOWN: LDC channel went down"); 23923af08d82Slm66018 23933af08d82Slm66018 vd_need_reset(vd, B_TRUE); 23943af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 23953af08d82Slm66018 DDI_SLEEP); 23963af08d82Slm66018 if (status == DDI_FAILURE) { 23973af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 23983af08d82Slm66018 vd_need_reset(vd, B_TRUE); 23993af08d82Slm66018 } 24003af08d82Slm66018 } 24013af08d82Slm66018 2402d10e4ef2Snarayan if (event & LDC_EVT_RESET) { 24033af08d82Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset"); 24043af08d82Slm66018 24053af08d82Slm66018 if (vd->state != VD_STATE_INIT) { 24063af08d82Slm66018 PR0("scheduling full reset"); 24073af08d82Slm66018 vd_need_reset(vd, B_FALSE); 24083af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 24093af08d82Slm66018 vd, DDI_SLEEP); 24103af08d82Slm66018 if (status == DDI_FAILURE) { 24113af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 24123af08d82Slm66018 vd_need_reset(vd, B_TRUE); 24133af08d82Slm66018 } 24143af08d82Slm66018 24153af08d82Slm66018 } else { 24163af08d82Slm66018 PR0("channel already reset, ignoring...\n"); 24173af08d82Slm66018 PR0("doing ldc up...\n"); 24183af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 24193af08d82Slm66018 } 24203af08d82Slm66018 2421d10e4ef2Snarayan return (LDC_SUCCESS); 2422d10e4ef2Snarayan } 2423d10e4ef2Snarayan 2424d10e4ef2Snarayan if (event & LDC_EVT_UP) { 24253af08d82Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state"); 24263af08d82Slm66018 PR0("initiating soft reset"); 2427d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 24283af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 24293af08d82Slm66018 vd, DDI_SLEEP); 24303af08d82Slm66018 if (status == DDI_FAILURE) { 24313af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 24323af08d82Slm66018 vd_need_reset(vd, B_TRUE); 24333af08d82Slm66018 return (LDC_SUCCESS); 24343af08d82Slm66018 } 2435d10e4ef2Snarayan } 2436d10e4ef2Snarayan 2437d10e4ef2Snarayan if (event & LDC_EVT_READ) { 2438d10e4ef2Snarayan int status; 2439d10e4ef2Snarayan 2440d10e4ef2Snarayan PR1("New data available"); 2441d10e4ef2Snarayan /* Queue a task to receive the new data */ 2442d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 2443d10e4ef2Snarayan DDI_SLEEP); 24443af08d82Slm66018 24453af08d82Slm66018 if (status == DDI_FAILURE) { 24463af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 24473af08d82Slm66018 vd_need_reset(vd, B_TRUE); 24483af08d82Slm66018 } 2449d10e4ef2Snarayan } 2450d10e4ef2Snarayan 2451d10e4ef2Snarayan return (LDC_SUCCESS); 24521ae08745Sheppo } 24531ae08745Sheppo 24541ae08745Sheppo static uint_t 24551ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 24561ae08745Sheppo { 24571ae08745Sheppo _NOTE(ARGUNUSED(key, val)) 24581ae08745Sheppo (*((uint_t *)arg))++; 24591ae08745Sheppo return (MH_WALK_TERMINATE); 24601ae08745Sheppo } 24611ae08745Sheppo 24621ae08745Sheppo 24631ae08745Sheppo static int 24641ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 24651ae08745Sheppo { 24661ae08745Sheppo uint_t vd_present = 0; 24671ae08745Sheppo minor_t instance; 24681ae08745Sheppo vds_t *vds; 24691ae08745Sheppo 24701ae08745Sheppo 24711ae08745Sheppo switch (cmd) { 24721ae08745Sheppo case DDI_DETACH: 24731ae08745Sheppo /* the real work happens below */ 24741ae08745Sheppo break; 24751ae08745Sheppo case DDI_SUSPEND: 2476d10e4ef2Snarayan PR0("No action required for DDI_SUSPEND"); 24771ae08745Sheppo return (DDI_SUCCESS); 24781ae08745Sheppo default: 24793af08d82Slm66018 PR0("Unrecognized \"cmd\""); 24801ae08745Sheppo return (DDI_FAILURE); 24811ae08745Sheppo } 24821ae08745Sheppo 24831ae08745Sheppo ASSERT(cmd == DDI_DETACH); 24841ae08745Sheppo instance = ddi_get_instance(dip); 24851ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 24863af08d82Slm66018 PR0("Could not get state for instance %u", instance); 24871ae08745Sheppo ddi_soft_state_free(vds_state, instance); 24881ae08745Sheppo return (DDI_FAILURE); 24891ae08745Sheppo } 24901ae08745Sheppo 24911ae08745Sheppo /* Do no detach when serving any vdisks */ 24921ae08745Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 24931ae08745Sheppo if (vd_present) { 24941ae08745Sheppo PR0("Not detaching because serving vdisks"); 24951ae08745Sheppo return (DDI_FAILURE); 24961ae08745Sheppo } 24971ae08745Sheppo 24981ae08745Sheppo PR0("Detaching"); 2499445b4c2eSsb155480 if (vds->initialized & VDS_MDEG) { 25001ae08745Sheppo (void) mdeg_unregister(vds->mdeg); 2501445b4c2eSsb155480 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template)); 2502445b4c2eSsb155480 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t)); 2503445b4c2eSsb155480 vds->ispecp = NULL; 2504445b4c2eSsb155480 vds->mdeg = NULL; 2505445b4c2eSsb155480 } 2506445b4c2eSsb155480 25071ae08745Sheppo if (vds->initialized & VDS_LDI) 25081ae08745Sheppo (void) ldi_ident_release(vds->ldi_ident); 25091ae08745Sheppo mod_hash_destroy_hash(vds->vd_table); 25101ae08745Sheppo ddi_soft_state_free(vds_state, instance); 25111ae08745Sheppo return (DDI_SUCCESS); 25121ae08745Sheppo } 25131ae08745Sheppo 25141ae08745Sheppo static boolean_t 25151ae08745Sheppo is_pseudo_device(dev_info_t *dip) 25161ae08745Sheppo { 25171ae08745Sheppo dev_info_t *parent, *root = ddi_root_node(); 25181ae08745Sheppo 25191ae08745Sheppo 25201ae08745Sheppo for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root); 25211ae08745Sheppo parent = ddi_get_parent(parent)) { 25221ae08745Sheppo if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0) 25231ae08745Sheppo return (B_TRUE); 25241ae08745Sheppo } 25251ae08745Sheppo 25261ae08745Sheppo return (B_FALSE); 25271ae08745Sheppo } 25281ae08745Sheppo 25291ae08745Sheppo static int 25300a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd) 25310a55fbb7Slm66018 { 25320a55fbb7Slm66018 int rval, status; 25330a55fbb7Slm66018 major_t major = getmajor(vd->dev[0]); 25340a55fbb7Slm66018 minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 25354bac2208Snarayan struct dk_minfo dk_minfo; 25360a55fbb7Slm66018 25374bac2208Snarayan /* 25384bac2208Snarayan * At this point, vdisk_size is set to the size of partition 2 but 25394bac2208Snarayan * this does not represent the size of the disk because partition 2 25404bac2208Snarayan * may not cover the entire disk and its size does not include reserved 25414bac2208Snarayan * blocks. So we update vdisk_size to be the size of the entire disk. 25424bac2208Snarayan */ 25434bac2208Snarayan if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 25444bac2208Snarayan (intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL), 25454bac2208Snarayan kcred, &rval)) != 0) { 2546*690555a1Sachartre PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 25474bac2208Snarayan status); 25480a55fbb7Slm66018 return (status); 25490a55fbb7Slm66018 } 25504bac2208Snarayan vd->vdisk_size = dk_minfo.dki_capacity; 25510a55fbb7Slm66018 25520a55fbb7Slm66018 /* Set full-disk parameters */ 25530a55fbb7Slm66018 vd->vdisk_type = VD_DISK_TYPE_DISK; 25540a55fbb7Slm66018 vd->nslices = (sizeof (vd->dev))/(sizeof (vd->dev[0])); 25550a55fbb7Slm66018 25560a55fbb7Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */ 25570a55fbb7Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 25580a55fbb7Slm66018 vd->dev[0] = 0; 25590a55fbb7Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 25600a55fbb7Slm66018 vd->ldi_handle[0] = NULL; 25610a55fbb7Slm66018 25620a55fbb7Slm66018 /* Initialize device numbers for remaining slices and open them */ 25630a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 25640a55fbb7Slm66018 /* 25650a55fbb7Slm66018 * Skip the entire-disk slice, as it's already open and its 25660a55fbb7Slm66018 * device known 25670a55fbb7Slm66018 */ 25680a55fbb7Slm66018 if (slice == VD_ENTIRE_DISK_SLICE) 25690a55fbb7Slm66018 continue; 25700a55fbb7Slm66018 ASSERT(vd->dev[slice] == 0); 25710a55fbb7Slm66018 ASSERT(vd->ldi_handle[slice] == NULL); 25720a55fbb7Slm66018 25730a55fbb7Slm66018 /* 25740a55fbb7Slm66018 * Construct the device number for the current slice 25750a55fbb7Slm66018 */ 25760a55fbb7Slm66018 vd->dev[slice] = makedevice(major, (minor + slice)); 25770a55fbb7Slm66018 25780a55fbb7Slm66018 /* 257934683adeSsg70180 * Open all slices of the disk to serve them to the client. 258034683adeSsg70180 * Slices are opened exclusively to prevent other threads or 258134683adeSsg70180 * processes in the service domain from performing I/O to 258234683adeSsg70180 * slices being accessed by a client. Failure to open a slice 258334683adeSsg70180 * results in vds not serving this disk, as the client could 258434683adeSsg70180 * attempt (and should be able) to access any slice immediately. 258534683adeSsg70180 * Any slices successfully opened before a failure will get 258634683adeSsg70180 * closed by vds_destroy_vd() as a result of the error returned 258734683adeSsg70180 * by this function. 258834683adeSsg70180 * 258934683adeSsg70180 * We need to do the open with FNDELAY so that opening an empty 259034683adeSsg70180 * slice does not fail. 25910a55fbb7Slm66018 */ 25920a55fbb7Slm66018 PR0("Opening device major %u, minor %u = slice %u", 25930a55fbb7Slm66018 major, minor, slice); 25940a55fbb7Slm66018 if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 259534683adeSsg70180 vd_open_flags | FNDELAY, kcred, &vd->ldi_handle[slice], 25960a55fbb7Slm66018 vd->vds->ldi_ident)) != 0) { 2597*690555a1Sachartre PRN("ldi_open_by_dev() returned errno %d " 25980a55fbb7Slm66018 "for slice %u", status, slice); 25990a55fbb7Slm66018 /* vds_destroy_vd() will close any open slices */ 2600*690555a1Sachartre vd->ldi_handle[slice] = NULL; 26010a55fbb7Slm66018 return (status); 26020a55fbb7Slm66018 } 26030a55fbb7Slm66018 } 26040a55fbb7Slm66018 26050a55fbb7Slm66018 return (0); 26060a55fbb7Slm66018 } 26070a55fbb7Slm66018 26080a55fbb7Slm66018 static int 26094bac2208Snarayan vd_setup_partition_efi(vd_t *vd) 26104bac2208Snarayan { 26114bac2208Snarayan efi_gpt_t *gpt; 26124bac2208Snarayan efi_gpe_t *gpe; 26134bac2208Snarayan struct uuid uuid = EFI_RESERVED; 26144bac2208Snarayan uint32_t crc; 26154bac2208Snarayan int length; 26164bac2208Snarayan 26174bac2208Snarayan length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t); 26184bac2208Snarayan 26194bac2208Snarayan gpt = kmem_zalloc(length, KM_SLEEP); 26204bac2208Snarayan gpe = (efi_gpe_t *)(gpt + 1); 26214bac2208Snarayan 26224bac2208Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 26234bac2208Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 26244bac2208Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 26254bac2208Snarayan gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL); 26264bac2208Snarayan gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 26274bac2208Snarayan gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 26284bac2208Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 26294bac2208Snarayan 26304bac2208Snarayan UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid); 26314bac2208Snarayan gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA; 26324bac2208Snarayan gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA; 26334bac2208Snarayan 26344bac2208Snarayan CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table); 26354bac2208Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 26364bac2208Snarayan 26374bac2208Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 26384bac2208Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 26394bac2208Snarayan 26404bac2208Snarayan vd->dk_efi.dki_lba = 0; 26414bac2208Snarayan vd->dk_efi.dki_length = length; 26424bac2208Snarayan vd->dk_efi.dki_data = gpt; 26434bac2208Snarayan 26444bac2208Snarayan return (0); 26454bac2208Snarayan } 26464bac2208Snarayan 26474bac2208Snarayan static int 26483c96341aSnarayan vd_setup_file(vd_t *vd) 26493c96341aSnarayan { 26503c96341aSnarayan int i, rval, status; 2651*690555a1Sachartre ushort_t sum; 26523c96341aSnarayan vattr_t vattr; 26533c96341aSnarayan dev_t dev; 26543c96341aSnarayan char *file_path = vd->device_path; 26553c96341aSnarayan char dev_path[MAXPATHLEN + 1]; 26563c96341aSnarayan ldi_handle_t lhandle; 26573c96341aSnarayan struct dk_cinfo dk_cinfo; 2658*690555a1Sachartre struct dk_label label; 26593c96341aSnarayan 26603c96341aSnarayan /* make sure the file is valid */ 26613c96341aSnarayan if ((status = lookupname(file_path, UIO_SYSSPACE, FOLLOW, 26623c96341aSnarayan NULLVPP, &vd->file_vnode)) != 0) { 2663*690555a1Sachartre PRN("Cannot lookup file(%s) errno %d", file_path, status); 26643c96341aSnarayan return (status); 26653c96341aSnarayan } 26663c96341aSnarayan 26673c96341aSnarayan if (vd->file_vnode->v_type != VREG) { 2668*690555a1Sachartre PRN("Invalid file type (%s)\n", file_path); 26693c96341aSnarayan VN_RELE(vd->file_vnode); 26703c96341aSnarayan return (EBADF); 26713c96341aSnarayan } 26723c96341aSnarayan VN_RELE(vd->file_vnode); 26733c96341aSnarayan 26743c96341aSnarayan if ((status = vn_open(file_path, UIO_SYSSPACE, vd_open_flags | FOFFMAX, 26753c96341aSnarayan 0, &vd->file_vnode, 0, 0)) != 0) { 2676*690555a1Sachartre PRN("vn_open(%s) = errno %d", file_path, status); 26773c96341aSnarayan return (status); 26783c96341aSnarayan } 26793c96341aSnarayan 2680*690555a1Sachartre /* 2681*690555a1Sachartre * We set vd->file now so that vds_destroy_vd will take care of 2682*690555a1Sachartre * closing the file and releasing the vnode in case of an error. 2683*690555a1Sachartre */ 2684*690555a1Sachartre vd->file = B_TRUE; 2685*690555a1Sachartre vd->pseudo = B_FALSE; 2686*690555a1Sachartre 26873c96341aSnarayan vattr.va_mask = AT_SIZE; 26883c96341aSnarayan if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred)) != 0) { 2689*690555a1Sachartre PRN("VOP_GETATTR(%s) = errno %d", file_path, status); 26903c96341aSnarayan return (EIO); 26913c96341aSnarayan } 26923c96341aSnarayan 26933c96341aSnarayan vd->file_size = vattr.va_size; 26943c96341aSnarayan /* size should be at least sizeof(dk_label) */ 26953c96341aSnarayan if (vd->file_size < sizeof (struct dk_label)) { 26963c96341aSnarayan PRN("Size of file has to be at least %ld bytes", 26973c96341aSnarayan sizeof (struct dk_label)); 26983c96341aSnarayan return (EIO); 26993c96341aSnarayan } 27003c96341aSnarayan 2701*690555a1Sachartre if (vd->file_vnode->v_flag & VNOMAP) { 2702*690555a1Sachartre PRN("File %s cannot be mapped", file_path); 27033c96341aSnarayan return (EIO); 27043c96341aSnarayan } 27053c96341aSnarayan 2706*690555a1Sachartre /* read label from file */ 2707*690555a1Sachartre if (VD_FILE_LABEL_READ(vd, &label) < 0) { 2708*690555a1Sachartre PRN("Can't read label from %s", file_path); 2709*690555a1Sachartre return (EIO); 2710*690555a1Sachartre } 27113c96341aSnarayan 27123c96341aSnarayan /* label checksum */ 2713*690555a1Sachartre sum = vd_lbl2cksum(&label); 27143c96341aSnarayan 2715*690555a1Sachartre if (label.dkl_magic != DKL_MAGIC || label.dkl_cksum != sum) { 27163c96341aSnarayan PR0("%s has an invalid disk label " 27173c96341aSnarayan "(magic=%x cksum=%x (expect %x))", 2718*690555a1Sachartre file_path, label.dkl_magic, label.dkl_cksum, sum); 27193c96341aSnarayan 27203c96341aSnarayan /* default label */ 2721*690555a1Sachartre bzero(&label, sizeof (struct dk_label)); 27223c96341aSnarayan 27233c96341aSnarayan /* 27243c96341aSnarayan * We must have a resonable number of cylinders and sectors so 27253c96341aSnarayan * that newfs can run using default values. 27263c96341aSnarayan * 27273c96341aSnarayan * if (disk_size < 2MB) 27283c96341aSnarayan * phys_cylinders = disk_size / 100K 27293c96341aSnarayan * else 27303c96341aSnarayan * phys_cylinders = disk_size / 300K 27313c96341aSnarayan * 27323c96341aSnarayan * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders 27333c96341aSnarayan * alt_cylinders = (phys_cylinders > 2) ? 2 : 0; 27343c96341aSnarayan * data_cylinders = phys_cylinders - alt_cylinders 27353c96341aSnarayan * 27363c96341aSnarayan * sectors = disk_size / (phys_cylinders * blk_size) 27373c96341aSnarayan */ 27383c96341aSnarayan if (vd->file_size < (2 * 1024 * 1024)) 2739*690555a1Sachartre label.dkl_pcyl = vd->file_size / (100 * 1024); 27403c96341aSnarayan else 2741*690555a1Sachartre label.dkl_pcyl = vd->file_size / (300 * 1024); 27423c96341aSnarayan 2743*690555a1Sachartre if (label.dkl_pcyl == 0) 2744*690555a1Sachartre label.dkl_pcyl = 1; 27453c96341aSnarayan 2746*690555a1Sachartre if (label.dkl_pcyl > 2) 2747*690555a1Sachartre label.dkl_acyl = 2; 27483c96341aSnarayan else 2749*690555a1Sachartre label.dkl_acyl = 0; 27503c96341aSnarayan 2751*690555a1Sachartre label.dkl_nsect = vd->file_size / 2752*690555a1Sachartre (DEV_BSIZE * label.dkl_pcyl); 2753*690555a1Sachartre label.dkl_ncyl = label.dkl_pcyl - label.dkl_acyl; 2754*690555a1Sachartre label.dkl_nhead = 1; 2755*690555a1Sachartre label.dkl_write_reinstruct = 0; 2756*690555a1Sachartre label.dkl_read_reinstruct = 0; 2757*690555a1Sachartre label.dkl_rpm = 7200; 2758*690555a1Sachartre label.dkl_apc = 0; 2759*690555a1Sachartre label.dkl_intrlv = 0; 2760*690555a1Sachartre label.dkl_magic = DKL_MAGIC; 27613c96341aSnarayan 27623c96341aSnarayan PR0("requested disk size: %ld bytes\n", vd->file_size); 2763*690555a1Sachartre PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label.dkl_pcyl, 2764*690555a1Sachartre label.dkl_nhead, label.dkl_nsect); 27653c96341aSnarayan PR0("provided disk size: %ld bytes\n", (uint64_t) 2766*690555a1Sachartre (label.dkl_pcyl * 2767*690555a1Sachartre label.dkl_nhead * label.dkl_nsect * DEV_BSIZE)); 27683c96341aSnarayan 27693c96341aSnarayan /* 27703c96341aSnarayan * We must have a correct label name otherwise format(1m) will 27713c96341aSnarayan * not recognized the disk as labeled. 27723c96341aSnarayan */ 2773*690555a1Sachartre (void) snprintf(label.dkl_asciilabel, LEN_DKL_ASCII, 27743c96341aSnarayan "SUNVDSK cyl %d alt %d hd %d sec %d", 2775*690555a1Sachartre label.dkl_ncyl, label.dkl_acyl, label.dkl_nhead, 2776*690555a1Sachartre label.dkl_nsect); 27773c96341aSnarayan 27783c96341aSnarayan /* default VTOC */ 2779*690555a1Sachartre label.dkl_vtoc.v_version = V_VERSION; 2780*690555a1Sachartre label.dkl_vtoc.v_nparts = V_NUMPAR; 2781*690555a1Sachartre label.dkl_vtoc.v_sanity = VTOC_SANE; 2782*690555a1Sachartre label.dkl_vtoc.v_part[2].p_tag = V_BACKUP; 2783*690555a1Sachartre label.dkl_map[2].dkl_cylno = 0; 2784*690555a1Sachartre label.dkl_map[2].dkl_nblk = label.dkl_ncyl * 2785*690555a1Sachartre label.dkl_nhead * label.dkl_nsect; 2786*690555a1Sachartre label.dkl_map[0] = label.dkl_map[2]; 2787*690555a1Sachartre label.dkl_map[0] = label.dkl_map[2]; 2788*690555a1Sachartre label.dkl_cksum = vd_lbl2cksum(&label); 2789*690555a1Sachartre 2790*690555a1Sachartre /* write default label to file */ 2791*690555a1Sachartre if (VD_FILE_LABEL_WRITE(vd, &label) < 0) { 2792*690555a1Sachartre PRN("Can't write label to %s", file_path); 2793*690555a1Sachartre return (EIO); 2794*690555a1Sachartre } 27953c96341aSnarayan } 27963c96341aSnarayan 2797*690555a1Sachartre vd->nslices = label.dkl_vtoc.v_nparts; 27983c96341aSnarayan 27993c96341aSnarayan /* sector size = block size = DEV_BSIZE */ 2800*690555a1Sachartre vd->vdisk_size = (label.dkl_pcyl * 2801*690555a1Sachartre label.dkl_nhead * label.dkl_nsect) / DEV_BSIZE; 28023c96341aSnarayan vd->vdisk_type = VD_DISK_TYPE_DISK; 28033c96341aSnarayan vd->vdisk_label = VD_DISK_LABEL_VTOC; 28043c96341aSnarayan vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */ 28053c96341aSnarayan 28063c96341aSnarayan /* Get max_xfer_sz from the device where the file is */ 28073c96341aSnarayan dev = vd->file_vnode->v_vfsp->vfs_dev; 28083c96341aSnarayan dev_path[0] = NULL; 28093c96341aSnarayan if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) { 28103c96341aSnarayan PR0("underlying device = %s\n", dev_path); 28113c96341aSnarayan } 28123c96341aSnarayan 28133c96341aSnarayan if ((status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, 28143c96341aSnarayan kcred, &lhandle, vd->vds->ldi_ident)) != 0) { 28153c96341aSnarayan PR0("ldi_open_by_dev() returned errno %d for device %s", 28163c96341aSnarayan status, dev_path); 28173c96341aSnarayan } else { 28183c96341aSnarayan if ((status = ldi_ioctl(lhandle, DKIOCINFO, 28193c96341aSnarayan (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred, 28203c96341aSnarayan &rval)) != 0) { 28213c96341aSnarayan PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 28223c96341aSnarayan status, dev_path); 28233c96341aSnarayan } else { 28243c96341aSnarayan /* 28253c96341aSnarayan * Store the device's max transfer size for 28263c96341aSnarayan * return to the client 28273c96341aSnarayan */ 28283c96341aSnarayan vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 28293c96341aSnarayan } 28303c96341aSnarayan 28313c96341aSnarayan PR0("close the device %s", dev_path); 28323c96341aSnarayan (void) ldi_close(lhandle, FREAD, kcred); 28333c96341aSnarayan } 28343c96341aSnarayan 28353c96341aSnarayan PR0("using for file %s, dev %s, max_xfer = %u blks", 28363c96341aSnarayan file_path, dev_path, vd->max_xfer_sz); 28373c96341aSnarayan 2838*690555a1Sachartre vd->dk_geom.dkg_ncyl = label.dkl_ncyl; 2839*690555a1Sachartre vd->dk_geom.dkg_acyl = label.dkl_acyl; 2840*690555a1Sachartre vd->dk_geom.dkg_pcyl = label.dkl_pcyl; 2841*690555a1Sachartre vd->dk_geom.dkg_nhead = label.dkl_nhead; 2842*690555a1Sachartre vd->dk_geom.dkg_nsect = label.dkl_nsect; 2843*690555a1Sachartre vd->dk_geom.dkg_intrlv = label.dkl_intrlv; 2844*690555a1Sachartre vd->dk_geom.dkg_apc = label.dkl_apc; 2845*690555a1Sachartre vd->dk_geom.dkg_rpm = label.dkl_rpm; 2846*690555a1Sachartre vd->dk_geom.dkg_write_reinstruct = label.dkl_write_reinstruct; 2847*690555a1Sachartre vd->dk_geom.dkg_read_reinstruct = label.dkl_read_reinstruct; 28483c96341aSnarayan 2849*690555a1Sachartre vd->vtoc.v_sanity = label.dkl_vtoc.v_sanity; 2850*690555a1Sachartre vd->vtoc.v_version = label.dkl_vtoc.v_version; 28513c96341aSnarayan vd->vtoc.v_sectorsz = DEV_BSIZE; 2852*690555a1Sachartre vd->vtoc.v_nparts = label.dkl_vtoc.v_nparts; 28533c96341aSnarayan 2854*690555a1Sachartre bcopy(label.dkl_vtoc.v_volume, vd->vtoc.v_volume, 28553c96341aSnarayan LEN_DKL_VVOL); 2856*690555a1Sachartre bcopy(label.dkl_asciilabel, vd->vtoc.v_asciilabel, 28573c96341aSnarayan LEN_DKL_ASCII); 28583c96341aSnarayan 28593c96341aSnarayan for (i = 0; i < vd->nslices; i++) { 2860*690555a1Sachartre vd->vtoc.timestamp[i] = label.dkl_vtoc.v_timestamp[i]; 2861*690555a1Sachartre vd->vtoc.v_part[i].p_tag = label.dkl_vtoc.v_part[i].p_tag; 2862*690555a1Sachartre vd->vtoc.v_part[i].p_flag = label.dkl_vtoc.v_part[i].p_flag; 2863*690555a1Sachartre vd->vtoc.v_part[i].p_start = label.dkl_map[i].dkl_cylno * 2864*690555a1Sachartre label.dkl_nhead * label.dkl_nsect; 2865*690555a1Sachartre vd->vtoc.v_part[i].p_size = label.dkl_map[i].dkl_nblk; 28663c96341aSnarayan vd->ldi_handle[i] = NULL; 28673c96341aSnarayan vd->dev[i] = NULL; 28683c96341aSnarayan } 28693c96341aSnarayan 28703c96341aSnarayan return (0); 28713c96341aSnarayan } 28723c96341aSnarayan 28733c96341aSnarayan static int 28743c96341aSnarayan vd_setup_vd(vd_t *vd) 28751ae08745Sheppo { 2876e1ebb9ecSlm66018 int rval, status; 28771ae08745Sheppo dev_info_t *dip; 28781ae08745Sheppo struct dk_cinfo dk_cinfo; 28793c96341aSnarayan char *device_path = vd->device_path; 28801ae08745Sheppo 28814bac2208Snarayan /* 28824bac2208Snarayan * We need to open with FNDELAY so that opening an empty partition 28834bac2208Snarayan * does not fail. 28844bac2208Snarayan */ 28854bac2208Snarayan if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY, 28864bac2208Snarayan kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) { 28873c96341aSnarayan PR0("ldi_open_by_name(%s) = errno %d", device_path, status); 2888*690555a1Sachartre vd->ldi_handle[0] = NULL; 28893c96341aSnarayan 28903c96341aSnarayan /* this may not be a device try opening as a file */ 28913c96341aSnarayan if (status == ENXIO || status == ENODEV) 28923c96341aSnarayan status = vd_setup_file(vd); 28933c96341aSnarayan if (status) { 2894*690555a1Sachartre PRN("Cannot use device/file (%s), errno=%d\n", 28953c96341aSnarayan device_path, status); 28963c96341aSnarayan if (status == ENXIO || status == ENODEV || 28973c96341aSnarayan status == ENOENT) { 28983c96341aSnarayan return (EAGAIN); 28993c96341aSnarayan } 29003c96341aSnarayan } 29010a55fbb7Slm66018 return (status); 29020a55fbb7Slm66018 } 29030a55fbb7Slm66018 29044bac2208Snarayan /* 29054bac2208Snarayan * nslices must be updated now so that vds_destroy_vd() will close 29064bac2208Snarayan * the slice we have just opened in case of an error. 29074bac2208Snarayan */ 29084bac2208Snarayan vd->nslices = 1; 29093c96341aSnarayan vd->file = B_FALSE; 29104bac2208Snarayan 2911e1ebb9ecSlm66018 /* Get device number and size of backing device */ 29120a55fbb7Slm66018 if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 29131ae08745Sheppo PRN("ldi_get_dev() returned errno %d for %s", 2914e1ebb9ecSlm66018 status, device_path); 29151ae08745Sheppo return (status); 29161ae08745Sheppo } 29170a55fbb7Slm66018 if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 2918e1ebb9ecSlm66018 PRN("ldi_get_size() failed for %s", device_path); 29191ae08745Sheppo return (EIO); 29201ae08745Sheppo } 2921e1ebb9ecSlm66018 vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 29221ae08745Sheppo 2923e1ebb9ecSlm66018 /* Verify backing device supports dk_cinfo, dk_geom, and vtoc */ 2924e1ebb9ecSlm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 2925e1ebb9ecSlm66018 (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred, 2926e1ebb9ecSlm66018 &rval)) != 0) { 2927e1ebb9ecSlm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 2928e1ebb9ecSlm66018 status, device_path); 2929e1ebb9ecSlm66018 return (status); 2930e1ebb9ecSlm66018 } 2931e1ebb9ecSlm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) { 2932e1ebb9ecSlm66018 PRN("slice %u >= maximum slice %u for %s", 2933e1ebb9ecSlm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path); 2934e1ebb9ecSlm66018 return (EIO); 2935e1ebb9ecSlm66018 } 29364bac2208Snarayan 29374bac2208Snarayan status = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, &vd->vdisk_label); 29384bac2208Snarayan 29394bac2208Snarayan if (status != 0) { 29404bac2208Snarayan PRN("vd_read_vtoc returned errno %d for %s", 2941e1ebb9ecSlm66018 status, device_path); 2942e1ebb9ecSlm66018 return (status); 2943e1ebb9ecSlm66018 } 29444bac2208Snarayan 29454bac2208Snarayan if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 29464bac2208Snarayan (status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 29474bac2208Snarayan (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL), 29484bac2208Snarayan kcred, &rval)) != 0) { 29494bac2208Snarayan PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s", 2950e1ebb9ecSlm66018 status, device_path); 2951e1ebb9ecSlm66018 return (status); 2952e1ebb9ecSlm66018 } 2953e1ebb9ecSlm66018 2954e1ebb9ecSlm66018 /* Store the device's max transfer size for return to the client */ 2955e1ebb9ecSlm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 2956e1ebb9ecSlm66018 2957e1ebb9ecSlm66018 /* Determine if backing device is a pseudo device */ 29581ae08745Sheppo if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]), 29591ae08745Sheppo dev_to_instance(vd->dev[0]), 0)) == NULL) { 2960e1ebb9ecSlm66018 PRN("%s is no longer accessible", device_path); 29611ae08745Sheppo return (EIO); 29621ae08745Sheppo } 29631ae08745Sheppo vd->pseudo = is_pseudo_device(dip); 29641ae08745Sheppo ddi_release_devi(dip); 29651ae08745Sheppo if (vd->pseudo) { 29661ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 29671ae08745Sheppo vd->nslices = 1; 29681ae08745Sheppo return (0); /* ...and we're done */ 29691ae08745Sheppo } 29701ae08745Sheppo 29710a55fbb7Slm66018 /* If slice is entire-disk slice, initialize for full disk */ 29720a55fbb7Slm66018 if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE) 29730a55fbb7Slm66018 return (vd_setup_full_disk(vd)); 29741ae08745Sheppo 29750a55fbb7Slm66018 2976e1ebb9ecSlm66018 /* Otherwise, we have a non-entire slice of a device */ 29771ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 29781ae08745Sheppo vd->nslices = 1; 29791ae08745Sheppo 29804bac2208Snarayan if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 29814bac2208Snarayan status = vd_setup_partition_efi(vd); 29824bac2208Snarayan return (status); 29834bac2208Snarayan } 29841ae08745Sheppo 2985e1ebb9ecSlm66018 /* Initialize dk_geom structure for single-slice device */ 29861ae08745Sheppo if (vd->dk_geom.dkg_nsect == 0) { 2987*690555a1Sachartre PRN("%s geometry claims 0 sectors per track", device_path); 29881ae08745Sheppo return (EIO); 29891ae08745Sheppo } 29901ae08745Sheppo if (vd->dk_geom.dkg_nhead == 0) { 2991*690555a1Sachartre PRN("%s geometry claims 0 heads", device_path); 29921ae08745Sheppo return (EIO); 29931ae08745Sheppo } 29941ae08745Sheppo vd->dk_geom.dkg_ncyl = 2995e1ebb9ecSlm66018 vd->vdisk_size/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead; 29961ae08745Sheppo vd->dk_geom.dkg_acyl = 0; 29971ae08745Sheppo vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 29981ae08745Sheppo 29991ae08745Sheppo 3000e1ebb9ecSlm66018 /* Initialize vtoc structure for single-slice device */ 30011ae08745Sheppo bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 30021ae08745Sheppo MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 30031ae08745Sheppo bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 30041ae08745Sheppo vd->vtoc.v_nparts = 1; 30051ae08745Sheppo vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 30061ae08745Sheppo vd->vtoc.v_part[0].p_flag = 0; 30071ae08745Sheppo vd->vtoc.v_part[0].p_start = 0; 3008e1ebb9ecSlm66018 vd->vtoc.v_part[0].p_size = vd->vdisk_size; 30091ae08745Sheppo bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 30101ae08745Sheppo MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel))); 30111ae08745Sheppo 30121ae08745Sheppo 30131ae08745Sheppo return (0); 30141ae08745Sheppo } 30151ae08745Sheppo 30161ae08745Sheppo static int 3017e1ebb9ecSlm66018 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id, 30181ae08745Sheppo vd_t **vdp) 30191ae08745Sheppo { 30201ae08745Sheppo char tq_name[TASKQ_NAMELEN]; 30210a55fbb7Slm66018 int status; 30221ae08745Sheppo ddi_iblock_cookie_t iblock = NULL; 30231ae08745Sheppo ldc_attr_t ldc_attr; 30241ae08745Sheppo vd_t *vd; 30251ae08745Sheppo 30261ae08745Sheppo 30271ae08745Sheppo ASSERT(vds != NULL); 3028e1ebb9ecSlm66018 ASSERT(device_path != NULL); 30291ae08745Sheppo ASSERT(vdp != NULL); 3030e1ebb9ecSlm66018 PR0("Adding vdisk for %s", device_path); 30311ae08745Sheppo 30321ae08745Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 30331ae08745Sheppo PRN("No memory for virtual disk"); 30341ae08745Sheppo return (EAGAIN); 30351ae08745Sheppo } 30361ae08745Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 30371ae08745Sheppo vd->vds = vds; 30383c96341aSnarayan (void) strncpy(vd->device_path, device_path, MAXPATHLEN); 30391ae08745Sheppo 30400a55fbb7Slm66018 /* Open vdisk and initialize parameters */ 30413c96341aSnarayan if ((status = vd_setup_vd(vd)) == 0) { 30423c96341aSnarayan vd->initialized |= VD_DISK_READY; 30431ae08745Sheppo 30443c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 30453c96341aSnarayan PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u", 30463c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 30473c96341aSnarayan (vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"), 30483c96341aSnarayan vd->nslices); 30493c96341aSnarayan } else { 30503c96341aSnarayan if (status != EAGAIN) 30513c96341aSnarayan return (status); 30523c96341aSnarayan } 30531ae08745Sheppo 30541ae08745Sheppo /* Initialize locking */ 30551ae08745Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 30561ae08745Sheppo &iblock) != DDI_SUCCESS) { 30571ae08745Sheppo PRN("Could not get iblock cookie."); 30581ae08745Sheppo return (EIO); 30591ae08745Sheppo } 30601ae08745Sheppo 30611ae08745Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 30621ae08745Sheppo vd->initialized |= VD_LOCKING; 30631ae08745Sheppo 30641ae08745Sheppo 3065d10e4ef2Snarayan /* Create start and completion task queues for the vdisk */ 3066d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 30671ae08745Sheppo PR1("tq_name = %s", tq_name); 3068d10e4ef2Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 30691ae08745Sheppo TASKQ_DEFAULTPRI, 0)) == NULL) { 30701ae08745Sheppo PRN("Could not create task queue"); 30711ae08745Sheppo return (EIO); 30721ae08745Sheppo } 3073d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 3074d10e4ef2Snarayan PR1("tq_name = %s", tq_name); 3075d10e4ef2Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 3076d10e4ef2Snarayan TASKQ_DEFAULTPRI, 0)) == NULL) { 3077d10e4ef2Snarayan PRN("Could not create task queue"); 3078d10e4ef2Snarayan return (EIO); 3079d10e4ef2Snarayan } 3080d10e4ef2Snarayan vd->enabled = 1; /* before callback can dispatch to startq */ 30811ae08745Sheppo 30821ae08745Sheppo 30831ae08745Sheppo /* Bring up LDC */ 30841ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC; 30851ae08745Sheppo ldc_attr.instance = ddi_get_instance(vds->dip); 30861ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; 3087e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 30881ae08745Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 3089*690555a1Sachartre PRN("Could not initialize LDC channel %lu, " 3090*690555a1Sachartre "init failed with error %d", ldc_id, status); 30911ae08745Sheppo return (status); 30921ae08745Sheppo } 30931ae08745Sheppo vd->initialized |= VD_LDC; 30941ae08745Sheppo 30951ae08745Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 30961ae08745Sheppo (caddr_t)vd)) != 0) { 3097*690555a1Sachartre PRN("Could not initialize LDC channel %lu," 3098*690555a1Sachartre "reg_callback failed with error %d", ldc_id, status); 30991ae08745Sheppo return (status); 31001ae08745Sheppo } 31011ae08745Sheppo 31021ae08745Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) { 3103*690555a1Sachartre PRN("Could not initialize LDC channel %lu," 3104*690555a1Sachartre "open failed with error %d", ldc_id, status); 31051ae08745Sheppo return (status); 31061ae08745Sheppo } 31071ae08745Sheppo 31083af08d82Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) { 310934683adeSsg70180 PR0("ldc_up() returned errno %d", status); 31103af08d82Slm66018 } 31113af08d82Slm66018 31124bac2208Snarayan /* Allocate the inband task memory handle */ 31134bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 31144bac2208Snarayan if (status) { 3115*690555a1Sachartre PRN("Could not initialize LDC channel %lu," 3116*690555a1Sachartre "alloc_handle failed with error %d", ldc_id, status); 31174bac2208Snarayan return (ENXIO); 31184bac2208Snarayan } 31191ae08745Sheppo 31201ae08745Sheppo /* Add the successfully-initialized vdisk to the server's table */ 31211ae08745Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 31221ae08745Sheppo PRN("Error adding vdisk ID %lu to table", id); 31231ae08745Sheppo return (EIO); 31241ae08745Sheppo } 31251ae08745Sheppo 31263af08d82Slm66018 /* Allocate the staging buffer */ 31273af08d82Slm66018 vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 31283af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 31293af08d82Slm66018 31303af08d82Slm66018 /* store initial state */ 31313af08d82Slm66018 vd->state = VD_STATE_INIT; 31323af08d82Slm66018 31331ae08745Sheppo return (0); 31341ae08745Sheppo } 31351ae08745Sheppo 31363af08d82Slm66018 static void 31373af08d82Slm66018 vd_free_dring_task(vd_t *vdp) 31383af08d82Slm66018 { 31393af08d82Slm66018 if (vdp->dring_task != NULL) { 31403af08d82Slm66018 ASSERT(vdp->dring_len != 0); 31413af08d82Slm66018 /* Free all dring_task memory handles */ 31423af08d82Slm66018 for (int i = 0; i < vdp->dring_len; i++) { 31433af08d82Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 31443af08d82Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 31453af08d82Slm66018 vdp->dring_task[i].msg = NULL; 31463af08d82Slm66018 } 31473af08d82Slm66018 kmem_free(vdp->dring_task, 31483af08d82Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len); 31493af08d82Slm66018 vdp->dring_task = NULL; 31503af08d82Slm66018 } 31513af08d82Slm66018 } 31523af08d82Slm66018 31531ae08745Sheppo /* 31541ae08745Sheppo * Destroy the state associated with a virtual disk 31551ae08745Sheppo */ 31561ae08745Sheppo static void 31571ae08745Sheppo vds_destroy_vd(void *arg) 31581ae08745Sheppo { 31591ae08745Sheppo vd_t *vd = (vd_t *)arg; 316034683adeSsg70180 int retry = 0, rv; 31611ae08745Sheppo 31621ae08745Sheppo if (vd == NULL) 31631ae08745Sheppo return; 31641ae08745Sheppo 3165d10e4ef2Snarayan PR0("Destroying vdisk state"); 3166d10e4ef2Snarayan 31674bac2208Snarayan if (vd->dk_efi.dki_data != NULL) 31684bac2208Snarayan kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length); 31694bac2208Snarayan 31701ae08745Sheppo /* Disable queuing requests for the vdisk */ 31711ae08745Sheppo if (vd->initialized & VD_LOCKING) { 31721ae08745Sheppo mutex_enter(&vd->lock); 31731ae08745Sheppo vd->enabled = 0; 31741ae08745Sheppo mutex_exit(&vd->lock); 31751ae08745Sheppo } 31761ae08745Sheppo 3177d10e4ef2Snarayan /* Drain and destroy start queue (*before* destroying completionq) */ 3178d10e4ef2Snarayan if (vd->startq != NULL) 3179d10e4ef2Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 3180d10e4ef2Snarayan 3181d10e4ef2Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */ 3182d10e4ef2Snarayan if (vd->completionq != NULL) 3183d10e4ef2Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 3184d10e4ef2Snarayan 31853af08d82Slm66018 vd_free_dring_task(vd); 31863af08d82Slm66018 318734683adeSsg70180 /* Free the inband task memory handle */ 318834683adeSsg70180 (void) ldc_mem_free_handle(vd->inband_task.mhdl); 318934683adeSsg70180 319034683adeSsg70180 /* Shut down LDC */ 319134683adeSsg70180 if (vd->initialized & VD_LDC) { 319234683adeSsg70180 /* unmap the dring */ 319334683adeSsg70180 if (vd->initialized & VD_DRING) 319434683adeSsg70180 (void) ldc_mem_dring_unmap(vd->dring_handle); 319534683adeSsg70180 319634683adeSsg70180 /* close LDC channel - retry on EAGAIN */ 319734683adeSsg70180 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) { 319834683adeSsg70180 if (++retry > vds_ldc_retries) { 319934683adeSsg70180 PR0("Timed out closing channel"); 320034683adeSsg70180 break; 320134683adeSsg70180 } 320234683adeSsg70180 drv_usecwait(vds_ldc_delay); 320334683adeSsg70180 } 320434683adeSsg70180 if (rv == 0) { 320534683adeSsg70180 (void) ldc_unreg_callback(vd->ldc_handle); 320634683adeSsg70180 (void) ldc_fini(vd->ldc_handle); 320734683adeSsg70180 } else { 320834683adeSsg70180 /* 320934683adeSsg70180 * Closing the LDC channel has failed. Ideally we should 321034683adeSsg70180 * fail here but there is no Zeus level infrastructure 321134683adeSsg70180 * to handle this. The MD has already been changed and 321234683adeSsg70180 * we have to do the close. So we try to do as much 321334683adeSsg70180 * clean up as we can. 321434683adeSsg70180 */ 321534683adeSsg70180 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE); 321634683adeSsg70180 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN) 321734683adeSsg70180 drv_usecwait(vds_ldc_delay); 321834683adeSsg70180 } 321934683adeSsg70180 } 322034683adeSsg70180 32213af08d82Slm66018 /* Free the staging buffer for msgs */ 32223af08d82Slm66018 if (vd->vio_msgp != NULL) { 32233af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 32243af08d82Slm66018 vd->vio_msgp = NULL; 32253af08d82Slm66018 } 32263af08d82Slm66018 32273af08d82Slm66018 /* Free the inband message buffer */ 32283af08d82Slm66018 if (vd->inband_task.msg != NULL) { 32293af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 32303af08d82Slm66018 vd->inband_task.msg = NULL; 3231d10e4ef2Snarayan } 32323c96341aSnarayan if (vd->file) { 3233*690555a1Sachartre /* Close file */ 32343c96341aSnarayan (void) VOP_CLOSE(vd->file_vnode, vd_open_flags, 1, 32353c96341aSnarayan 0, kcred); 32363c96341aSnarayan VN_RELE(vd->file_vnode); 32373c96341aSnarayan } else { 32381ae08745Sheppo /* Close any open backing-device slices */ 32391ae08745Sheppo for (uint_t slice = 0; slice < vd->nslices; slice++) { 32401ae08745Sheppo if (vd->ldi_handle[slice] != NULL) { 32411ae08745Sheppo PR0("Closing slice %u", slice); 32421ae08745Sheppo (void) ldi_close(vd->ldi_handle[slice], 32434bac2208Snarayan vd_open_flags | FNDELAY, kcred); 32441ae08745Sheppo } 32451ae08745Sheppo } 32463c96341aSnarayan } 32471ae08745Sheppo 32481ae08745Sheppo /* Free lock */ 32491ae08745Sheppo if (vd->initialized & VD_LOCKING) 32501ae08745Sheppo mutex_destroy(&vd->lock); 32511ae08745Sheppo 32521ae08745Sheppo /* Finally, free the vdisk structure itself */ 32531ae08745Sheppo kmem_free(vd, sizeof (*vd)); 32541ae08745Sheppo } 32551ae08745Sheppo 32561ae08745Sheppo static int 3257e1ebb9ecSlm66018 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id) 32581ae08745Sheppo { 32591ae08745Sheppo int status; 32601ae08745Sheppo vd_t *vd = NULL; 32611ae08745Sheppo 32621ae08745Sheppo 3263e1ebb9ecSlm66018 if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0) 32641ae08745Sheppo vds_destroy_vd(vd); 32651ae08745Sheppo 32661ae08745Sheppo return (status); 32671ae08745Sheppo } 32681ae08745Sheppo 32691ae08745Sheppo static int 32701ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 32711ae08745Sheppo uint64_t *ldc_id) 32721ae08745Sheppo { 32731ae08745Sheppo int num_channels; 32741ae08745Sheppo 32751ae08745Sheppo 32761ae08745Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */ 32771ae08745Sheppo if ((num_channels = md_scan_dag(md, vd_node, 32781ae08745Sheppo md_find_name(md, VD_CHANNEL_ENDPOINT), 32791ae08745Sheppo md_find_name(md, "fwd"), channel)) <= 0) { 32801ae08745Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 32811ae08745Sheppo return (-1); 32821ae08745Sheppo } 32831ae08745Sheppo 32841ae08745Sheppo /* Get the "id" value for the first channel endpoint node */ 32851ae08745Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 32861ae08745Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk", 32871ae08745Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT); 32881ae08745Sheppo return (-1); 32891ae08745Sheppo } 32901ae08745Sheppo 32911ae08745Sheppo if (num_channels > 1) { 32921ae08745Sheppo PRN("Using ID of first of multiple channels for this vdisk"); 32931ae08745Sheppo } 32941ae08745Sheppo 32951ae08745Sheppo return (0); 32961ae08745Sheppo } 32971ae08745Sheppo 32981ae08745Sheppo static int 32991ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 33001ae08745Sheppo { 33011ae08745Sheppo int num_nodes, status; 33021ae08745Sheppo size_t size; 33031ae08745Sheppo mde_cookie_t *channel; 33041ae08745Sheppo 33051ae08745Sheppo 33061ae08745Sheppo if ((num_nodes = md_node_count(md)) <= 0) { 33071ae08745Sheppo PRN("Invalid node count in Machine Description subtree"); 33081ae08745Sheppo return (-1); 33091ae08745Sheppo } 33101ae08745Sheppo size = num_nodes*(sizeof (*channel)); 33111ae08745Sheppo channel = kmem_zalloc(size, KM_SLEEP); 33121ae08745Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 33131ae08745Sheppo kmem_free(channel, size); 33141ae08745Sheppo 33151ae08745Sheppo return (status); 33161ae08745Sheppo } 33171ae08745Sheppo 33181ae08745Sheppo static void 33191ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 33201ae08745Sheppo { 3321e1ebb9ecSlm66018 char *device_path = NULL; 33221ae08745Sheppo uint64_t id = 0, ldc_id = 0; 33231ae08745Sheppo 33241ae08745Sheppo 33251ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 33261ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 33271ae08745Sheppo return; 33281ae08745Sheppo } 33291ae08745Sheppo PR0("Adding vdisk ID %lu", id); 33301ae08745Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 3331e1ebb9ecSlm66018 &device_path) != 0) { 33321ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 33331ae08745Sheppo return; 33341ae08745Sheppo } 33351ae08745Sheppo 33361ae08745Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 33371ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", id); 33381ae08745Sheppo return; 33391ae08745Sheppo } 33401ae08745Sheppo 3341e1ebb9ecSlm66018 if (vds_init_vd(vds, id, device_path, ldc_id) != 0) { 33421ae08745Sheppo PRN("Failed to add vdisk ID %lu", id); 33431ae08745Sheppo return; 33441ae08745Sheppo } 33451ae08745Sheppo } 33461ae08745Sheppo 33471ae08745Sheppo static void 33481ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 33491ae08745Sheppo { 33501ae08745Sheppo uint64_t id = 0; 33511ae08745Sheppo 33521ae08745Sheppo 33531ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 33541ae08745Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node", 33551ae08745Sheppo VD_ID_PROP); 33561ae08745Sheppo return; 33571ae08745Sheppo } 33581ae08745Sheppo PR0("Removing vdisk ID %lu", id); 33591ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 33601ae08745Sheppo PRN("No vdisk entry found for vdisk ID %lu", id); 33611ae08745Sheppo } 33621ae08745Sheppo 33631ae08745Sheppo static void 33641ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 33651ae08745Sheppo md_t *curr_md, mde_cookie_t curr_vd_node) 33661ae08745Sheppo { 33671ae08745Sheppo char *curr_dev, *prev_dev; 33681ae08745Sheppo uint64_t curr_id = 0, curr_ldc_id = 0; 33691ae08745Sheppo uint64_t prev_id = 0, prev_ldc_id = 0; 33701ae08745Sheppo size_t len; 33711ae08745Sheppo 33721ae08745Sheppo 33731ae08745Sheppo /* Validate that vdisk ID has not changed */ 33741ae08745Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 33751ae08745Sheppo PRN("Error getting previous vdisk \"%s\" property", 33761ae08745Sheppo VD_ID_PROP); 33771ae08745Sheppo return; 33781ae08745Sheppo } 33791ae08745Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 33801ae08745Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 33811ae08745Sheppo return; 33821ae08745Sheppo } 33831ae08745Sheppo if (curr_id != prev_id) { 33841ae08745Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu", 33851ae08745Sheppo prev_id, curr_id); 33861ae08745Sheppo return; 33871ae08745Sheppo } 33881ae08745Sheppo 33891ae08745Sheppo /* Validate that LDC ID has not changed */ 33901ae08745Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 33911ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id); 33921ae08745Sheppo return; 33931ae08745Sheppo } 33941ae08745Sheppo 33951ae08745Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 33961ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id); 33971ae08745Sheppo return; 33981ae08745Sheppo } 33991ae08745Sheppo if (curr_ldc_id != prev_ldc_id) { 34000a55fbb7Slm66018 _NOTE(NOTREACHED); /* lint is confused */ 34011ae08745Sheppo PRN("Not changing vdisk: " 34021ae08745Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 34031ae08745Sheppo return; 34041ae08745Sheppo } 34051ae08745Sheppo 34061ae08745Sheppo /* Determine whether device path has changed */ 34071ae08745Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 34081ae08745Sheppo &prev_dev) != 0) { 34091ae08745Sheppo PRN("Error getting previous vdisk \"%s\"", 34101ae08745Sheppo VD_BLOCK_DEVICE_PROP); 34111ae08745Sheppo return; 34121ae08745Sheppo } 34131ae08745Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 34141ae08745Sheppo &curr_dev) != 0) { 34151ae08745Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 34161ae08745Sheppo return; 34171ae08745Sheppo } 34181ae08745Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 34191ae08745Sheppo (strncmp(curr_dev, prev_dev, len) == 0)) 34201ae08745Sheppo return; /* no relevant (supported) change */ 34211ae08745Sheppo 34221ae08745Sheppo PR0("Changing vdisk ID %lu", prev_id); 34233af08d82Slm66018 34241ae08745Sheppo /* Remove old state, which will close vdisk and reset */ 34251ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 34261ae08745Sheppo PRN("No entry found for vdisk ID %lu", prev_id); 34273af08d82Slm66018 34281ae08745Sheppo /* Re-initialize vdisk with new state */ 34291ae08745Sheppo if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) { 34301ae08745Sheppo PRN("Failed to change vdisk ID %lu", curr_id); 34311ae08745Sheppo return; 34321ae08745Sheppo } 34331ae08745Sheppo } 34341ae08745Sheppo 34351ae08745Sheppo static int 34361ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md) 34371ae08745Sheppo { 34381ae08745Sheppo int i; 34391ae08745Sheppo vds_t *vds = arg; 34401ae08745Sheppo 34411ae08745Sheppo 34421ae08745Sheppo if (md == NULL) 34431ae08745Sheppo return (MDEG_FAILURE); 34441ae08745Sheppo ASSERT(vds != NULL); 34451ae08745Sheppo 34461ae08745Sheppo for (i = 0; i < md->removed.nelem; i++) 34471ae08745Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 34481ae08745Sheppo for (i = 0; i < md->match_curr.nelem; i++) 34491ae08745Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 34501ae08745Sheppo md->match_curr.mdp, md->match_curr.mdep[i]); 34511ae08745Sheppo for (i = 0; i < md->added.nelem; i++) 34521ae08745Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 34531ae08745Sheppo 34541ae08745Sheppo return (MDEG_SUCCESS); 34551ae08745Sheppo } 34561ae08745Sheppo 34573c96341aSnarayan 34581ae08745Sheppo static int 34591ae08745Sheppo vds_do_attach(dev_info_t *dip) 34601ae08745Sheppo { 3461445b4c2eSsb155480 int status, sz; 3462445b4c2eSsb155480 int cfg_handle; 34631ae08745Sheppo minor_t instance = ddi_get_instance(dip); 34641ae08745Sheppo vds_t *vds; 3465445b4c2eSsb155480 mdeg_prop_spec_t *pspecp; 3466445b4c2eSsb155480 mdeg_node_spec_t *ispecp; 34671ae08745Sheppo 34681ae08745Sheppo /* 34691ae08745Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's 34701ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 34711ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 34721ae08745Sheppo * the "reg" property on the node in the device tree it builds from 34731ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 34741ae08745Sheppo * "reg" property value to uniquely identify this device instance when 34751ae08745Sheppo * registering with the MD event-generation framework. If the "reg" 34761ae08745Sheppo * property cannot be found, the device tree state is presumably so 34771ae08745Sheppo * broken that there is no point in continuing. 34781ae08745Sheppo */ 3479445b4c2eSsb155480 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 3480445b4c2eSsb155480 VD_REG_PROP)) { 3481445b4c2eSsb155480 PRN("vds \"%s\" property does not exist", VD_REG_PROP); 34821ae08745Sheppo return (DDI_FAILURE); 34831ae08745Sheppo } 34841ae08745Sheppo 34851ae08745Sheppo /* Get the MD instance for later MDEG registration */ 34861ae08745Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 3487445b4c2eSsb155480 VD_REG_PROP, -1); 34881ae08745Sheppo 34891ae08745Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 34901ae08745Sheppo PRN("Could not allocate state for instance %u", instance); 34911ae08745Sheppo return (DDI_FAILURE); 34921ae08745Sheppo } 34931ae08745Sheppo 34941ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 34951ae08745Sheppo PRN("Could not get state for instance %u", instance); 34961ae08745Sheppo ddi_soft_state_free(vds_state, instance); 34971ae08745Sheppo return (DDI_FAILURE); 34981ae08745Sheppo } 34991ae08745Sheppo 35003c96341aSnarayan 35011ae08745Sheppo vds->dip = dip; 35021ae08745Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 35031ae08745Sheppo vds_destroy_vd, 35041ae08745Sheppo sizeof (void *)); 35051ae08745Sheppo ASSERT(vds->vd_table != NULL); 35061ae08745Sheppo 35071ae08745Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 35081ae08745Sheppo PRN("ldi_ident_from_dip() returned errno %d", status); 35091ae08745Sheppo return (DDI_FAILURE); 35101ae08745Sheppo } 35111ae08745Sheppo vds->initialized |= VDS_LDI; 35121ae08745Sheppo 35131ae08745Sheppo /* Register for MD updates */ 3514445b4c2eSsb155480 sz = sizeof (vds_prop_template); 3515445b4c2eSsb155480 pspecp = kmem_alloc(sz, KM_SLEEP); 3516445b4c2eSsb155480 bcopy(vds_prop_template, pspecp, sz); 3517445b4c2eSsb155480 3518445b4c2eSsb155480 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle); 3519445b4c2eSsb155480 3520445b4c2eSsb155480 /* initialize the complete prop spec structure */ 3521445b4c2eSsb155480 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP); 3522445b4c2eSsb155480 ispecp->namep = "virtual-device"; 3523445b4c2eSsb155480 ispecp->specp = pspecp; 3524445b4c2eSsb155480 3525445b4c2eSsb155480 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds, 35261ae08745Sheppo &vds->mdeg) != MDEG_SUCCESS) { 35271ae08745Sheppo PRN("Unable to register for MD updates"); 3528445b4c2eSsb155480 kmem_free(ispecp, sizeof (mdeg_node_spec_t)); 3529445b4c2eSsb155480 kmem_free(pspecp, sz); 35301ae08745Sheppo return (DDI_FAILURE); 35311ae08745Sheppo } 3532445b4c2eSsb155480 3533445b4c2eSsb155480 vds->ispecp = ispecp; 35341ae08745Sheppo vds->initialized |= VDS_MDEG; 35351ae08745Sheppo 35360a55fbb7Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */ 35370a55fbb7Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 35380a55fbb7Slm66018 DDI_PROP_SUCCESS) { 35390a55fbb7Slm66018 PRN("failed to set \"%s\" property for instance %u", 35400a55fbb7Slm66018 DDI_NO_AUTODETACH, instance); 35410a55fbb7Slm66018 } 35420a55fbb7Slm66018 35431ae08745Sheppo ddi_report_dev(dip); 35441ae08745Sheppo return (DDI_SUCCESS); 35451ae08745Sheppo } 35461ae08745Sheppo 35471ae08745Sheppo static int 35481ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 35491ae08745Sheppo { 35501ae08745Sheppo int status; 35511ae08745Sheppo 35521ae08745Sheppo switch (cmd) { 35531ae08745Sheppo case DDI_ATTACH: 3554d10e4ef2Snarayan PR0("Attaching"); 35551ae08745Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 35561ae08745Sheppo (void) vds_detach(dip, DDI_DETACH); 35571ae08745Sheppo return (status); 35581ae08745Sheppo case DDI_RESUME: 3559d10e4ef2Snarayan PR0("No action required for DDI_RESUME"); 35601ae08745Sheppo return (DDI_SUCCESS); 35611ae08745Sheppo default: 35621ae08745Sheppo return (DDI_FAILURE); 35631ae08745Sheppo } 35641ae08745Sheppo } 35651ae08745Sheppo 35661ae08745Sheppo static struct dev_ops vds_ops = { 35671ae08745Sheppo DEVO_REV, /* devo_rev */ 35681ae08745Sheppo 0, /* devo_refcnt */ 35691ae08745Sheppo ddi_no_info, /* devo_getinfo */ 35701ae08745Sheppo nulldev, /* devo_identify */ 35711ae08745Sheppo nulldev, /* devo_probe */ 35721ae08745Sheppo vds_attach, /* devo_attach */ 35731ae08745Sheppo vds_detach, /* devo_detach */ 35741ae08745Sheppo nodev, /* devo_reset */ 35751ae08745Sheppo NULL, /* devo_cb_ops */ 35761ae08745Sheppo NULL, /* devo_bus_ops */ 35771ae08745Sheppo nulldev /* devo_power */ 35781ae08745Sheppo }; 35791ae08745Sheppo 35801ae08745Sheppo static struct modldrv modldrv = { 35811ae08745Sheppo &mod_driverops, 35821ae08745Sheppo "virtual disk server v%I%", 35831ae08745Sheppo &vds_ops, 35841ae08745Sheppo }; 35851ae08745Sheppo 35861ae08745Sheppo static struct modlinkage modlinkage = { 35871ae08745Sheppo MODREV_1, 35881ae08745Sheppo &modldrv, 35891ae08745Sheppo NULL 35901ae08745Sheppo }; 35911ae08745Sheppo 35921ae08745Sheppo 35931ae08745Sheppo int 35941ae08745Sheppo _init(void) 35951ae08745Sheppo { 35961ae08745Sheppo int i, status; 35971ae08745Sheppo 3598d10e4ef2Snarayan 35991ae08745Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 36001ae08745Sheppo return (status); 36011ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) { 36021ae08745Sheppo ddi_soft_state_fini(&vds_state); 36031ae08745Sheppo return (status); 36041ae08745Sheppo } 36051ae08745Sheppo 36061ae08745Sheppo /* Fill in the bit-mask of server-supported operations */ 36071ae08745Sheppo for (i = 0; i < vds_noperations; i++) 36081ae08745Sheppo vds_operations |= 1 << (vds_operation[i].operation - 1); 36091ae08745Sheppo 36101ae08745Sheppo return (0); 36111ae08745Sheppo } 36121ae08745Sheppo 36131ae08745Sheppo int 36141ae08745Sheppo _info(struct modinfo *modinfop) 36151ae08745Sheppo { 36161ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 36171ae08745Sheppo } 36181ae08745Sheppo 36191ae08745Sheppo int 36201ae08745Sheppo _fini(void) 36211ae08745Sheppo { 36221ae08745Sheppo int status; 36231ae08745Sheppo 3624d10e4ef2Snarayan 36251ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 36261ae08745Sheppo return (status); 36271ae08745Sheppo ddi_soft_state_fini(&vds_state); 36281ae08745Sheppo return (0); 36291ae08745Sheppo } 3630