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> 53690555a1Sachartre #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 88*eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */ 89*eba0cb4eSachartre #define VD_FILE_WRITE_FLAGS SM_ASYNC 90*eba0cb4eSachartre 911ae08745Sheppo /* 921ae08745Sheppo * By Solaris convention, slice/partition 2 represents the entire disk; 931ae08745Sheppo * unfortunately, this convention does not appear to be codified. 941ae08745Sheppo */ 951ae08745Sheppo #define VD_ENTIRE_DISK_SLICE 2 961ae08745Sheppo 971ae08745Sheppo /* Return a cpp token as a string */ 981ae08745Sheppo #define STRINGIZE(token) #token 991ae08745Sheppo 1001ae08745Sheppo /* 1011ae08745Sheppo * Print a message prefixed with the current function name to the message log 1021ae08745Sheppo * (and optionally to the console for verbose boots); these macros use cpp's 1031ae08745Sheppo * concatenation of string literals and C99 variable-length-argument-list 1041ae08745Sheppo * macros 1051ae08745Sheppo */ 1061ae08745Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 1071ae08745Sheppo #define _PRN(format, ...) \ 1081ae08745Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 1091ae08745Sheppo 1101ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */ 1111ae08745Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 1121ae08745Sheppo (vd->dring + (i)*vd->descriptor_size)) 1131ae08745Sheppo 1141ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */ 1151ae08745Sheppo #define VD_CLIENT(vd) \ 1161ae08745Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 1171ae08745Sheppo (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" : \ 1181ae08745Sheppo (((vd)->xfer_mode == 0) ? "null client" : \ 1191ae08745Sheppo "unsupported client"))) 1201ae08745Sheppo 121690555a1Sachartre /* For IO to raw disk on file */ 122690555a1Sachartre #define VD_FILE_SLICE_NONE -1 123690555a1Sachartre 124690555a1Sachartre /* Read disk label from a disk on file */ 125690555a1Sachartre #define VD_FILE_LABEL_READ(vd, labelp) \ 126690555a1Sachartre vd_file_rw(vd, VD_FILE_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \ 127690555a1Sachartre 0, sizeof (struct dk_label)) 128690555a1Sachartre 129690555a1Sachartre /* Write disk label to a disk on file */ 130690555a1Sachartre #define VD_FILE_LABEL_WRITE(vd, labelp) \ 131690555a1Sachartre vd_file_rw(vd, VD_FILE_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \ 132690555a1Sachartre 0, sizeof (struct dk_label)) 133690555a1Sachartre 134445b4c2eSsb155480 /* 135445b4c2eSsb155480 * Specification of an MD node passed to the MDEG to filter any 136445b4c2eSsb155480 * 'vport' nodes that do not belong to the specified node. This 137445b4c2eSsb155480 * template is copied for each vds instance and filled in with 138445b4c2eSsb155480 * the appropriate 'cfg-handle' value before being passed to the MDEG. 139445b4c2eSsb155480 */ 140445b4c2eSsb155480 static mdeg_prop_spec_t vds_prop_template[] = { 141445b4c2eSsb155480 { MDET_PROP_STR, "name", VDS_NAME }, 142445b4c2eSsb155480 { MDET_PROP_VAL, "cfg-handle", NULL }, 143445b4c2eSsb155480 { MDET_LIST_END, NULL, NULL } 144445b4c2eSsb155480 }; 145445b4c2eSsb155480 146445b4c2eSsb155480 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val); 147445b4c2eSsb155480 148445b4c2eSsb155480 /* 149445b4c2eSsb155480 * Matching criteria passed to the MDEG to register interest 150445b4c2eSsb155480 * in changes to 'virtual-device-port' nodes identified by their 151445b4c2eSsb155480 * 'id' property. 152445b4c2eSsb155480 */ 153445b4c2eSsb155480 static md_prop_match_t vd_prop_match[] = { 154445b4c2eSsb155480 { MDET_PROP_VAL, VD_ID_PROP }, 155445b4c2eSsb155480 { MDET_LIST_END, NULL } 156445b4c2eSsb155480 }; 157445b4c2eSsb155480 158445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port", 159445b4c2eSsb155480 vd_prop_match}; 160445b4c2eSsb155480 1611ae08745Sheppo /* Debugging macros */ 1621ae08745Sheppo #ifdef DEBUG 1633af08d82Slm66018 1643af08d82Slm66018 static int vd_msglevel = 0; 1653af08d82Slm66018 1661ae08745Sheppo #define PR0 if (vd_msglevel > 0) PRN 1671ae08745Sheppo #define PR1 if (vd_msglevel > 1) PRN 1681ae08745Sheppo #define PR2 if (vd_msglevel > 2) PRN 1691ae08745Sheppo 1701ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) \ 1713c96341aSnarayan PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 1721ae08745Sheppo elem->hdr.dstate, \ 1731ae08745Sheppo elem->payload.operation, \ 1741ae08745Sheppo elem->payload.status, \ 1751ae08745Sheppo elem->payload.nbytes, \ 1761ae08745Sheppo elem->payload.addr, \ 1771ae08745Sheppo elem->payload.ncookies); 1781ae08745Sheppo 1793af08d82Slm66018 char * 1803af08d82Slm66018 vd_decode_state(int state) 1813af08d82Slm66018 { 1823af08d82Slm66018 char *str; 1833af08d82Slm66018 1843af08d82Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break; 1853af08d82Slm66018 1863af08d82Slm66018 switch (state) { 1873af08d82Slm66018 CASE_STATE(VD_STATE_INIT) 1883af08d82Slm66018 CASE_STATE(VD_STATE_VER) 1893af08d82Slm66018 CASE_STATE(VD_STATE_ATTR) 1903af08d82Slm66018 CASE_STATE(VD_STATE_DRING) 1913af08d82Slm66018 CASE_STATE(VD_STATE_RDX) 1923af08d82Slm66018 CASE_STATE(VD_STATE_DATA) 1933af08d82Slm66018 default: str = "unknown"; break; 1943af08d82Slm66018 } 1953af08d82Slm66018 1963af08d82Slm66018 #undef CASE_STATE 1973af08d82Slm66018 1983af08d82Slm66018 return (str); 1993af08d82Slm66018 } 2003af08d82Slm66018 2013af08d82Slm66018 void 2023af08d82Slm66018 vd_decode_tag(vio_msg_t *msg) 2033af08d82Slm66018 { 2043af08d82Slm66018 char *tstr, *sstr, *estr; 2053af08d82Slm66018 2063af08d82Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 2073af08d82Slm66018 2083af08d82Slm66018 switch (msg->tag.vio_msgtype) { 2093af08d82Slm66018 CASE_TYPE(VIO_TYPE_CTRL) 2103af08d82Slm66018 CASE_TYPE(VIO_TYPE_DATA) 2113af08d82Slm66018 CASE_TYPE(VIO_TYPE_ERR) 2123af08d82Slm66018 default: tstr = "unknown"; break; 2133af08d82Slm66018 } 2143af08d82Slm66018 2153af08d82Slm66018 #undef CASE_TYPE 2163af08d82Slm66018 2173af08d82Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 2183af08d82Slm66018 2193af08d82Slm66018 switch (msg->tag.vio_subtype) { 2203af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 2213af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 2223af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 2233af08d82Slm66018 default: sstr = "unknown"; break; 2243af08d82Slm66018 } 2253af08d82Slm66018 2263af08d82Slm66018 #undef CASE_SUBTYPE 2273af08d82Slm66018 2283af08d82Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break; 2293af08d82Slm66018 2303af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 2313af08d82Slm66018 CASE_ENV(VIO_VER_INFO) 2323af08d82Slm66018 CASE_ENV(VIO_ATTR_INFO) 2333af08d82Slm66018 CASE_ENV(VIO_DRING_REG) 2343af08d82Slm66018 CASE_ENV(VIO_DRING_UNREG) 2353af08d82Slm66018 CASE_ENV(VIO_RDX) 2363af08d82Slm66018 CASE_ENV(VIO_PKT_DATA) 2373af08d82Slm66018 CASE_ENV(VIO_DESC_DATA) 2383af08d82Slm66018 CASE_ENV(VIO_DRING_DATA) 2393af08d82Slm66018 default: estr = "unknown"; break; 2403af08d82Slm66018 } 2413af08d82Slm66018 2423af08d82Slm66018 #undef CASE_ENV 2433af08d82Slm66018 2443af08d82Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)", 2453af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 2463af08d82Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr); 2473af08d82Slm66018 } 2483af08d82Slm66018 2491ae08745Sheppo #else /* !DEBUG */ 2503af08d82Slm66018 2511ae08745Sheppo #define PR0(...) 2521ae08745Sheppo #define PR1(...) 2531ae08745Sheppo #define PR2(...) 2541ae08745Sheppo 2551ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) 2561ae08745Sheppo 2573af08d82Slm66018 #define vd_decode_state(_s) (NULL) 2583af08d82Slm66018 #define vd_decode_tag(_s) (NULL) 2593af08d82Slm66018 2601ae08745Sheppo #endif /* DEBUG */ 2611ae08745Sheppo 2621ae08745Sheppo 263d10e4ef2Snarayan /* 264d10e4ef2Snarayan * Soft state structure for a vds instance 265d10e4ef2Snarayan */ 2661ae08745Sheppo typedef struct vds { 2671ae08745Sheppo uint_t initialized; /* driver inst initialization flags */ 2681ae08745Sheppo dev_info_t *dip; /* driver inst devinfo pointer */ 2691ae08745Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 2701ae08745Sheppo mod_hash_t *vd_table; /* table of virtual disks served */ 271445b4c2eSsb155480 mdeg_node_spec_t *ispecp; /* mdeg node specification */ 2721ae08745Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */ 2731ae08745Sheppo } vds_t; 2741ae08745Sheppo 275d10e4ef2Snarayan /* 276d10e4ef2Snarayan * Types of descriptor-processing tasks 277d10e4ef2Snarayan */ 278d10e4ef2Snarayan typedef enum vd_task_type { 279d10e4ef2Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 280d10e4ef2Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 281d10e4ef2Snarayan } vd_task_type_t; 282d10e4ef2Snarayan 283d10e4ef2Snarayan /* 284d10e4ef2Snarayan * Structure describing the task for processing a descriptor 285d10e4ef2Snarayan */ 286d10e4ef2Snarayan typedef struct vd_task { 287d10e4ef2Snarayan struct vd *vd; /* vd instance task is for */ 288d10e4ef2Snarayan vd_task_type_t type; /* type of descriptor task */ 289d10e4ef2Snarayan int index; /* dring elem index for task */ 290d10e4ef2Snarayan vio_msg_t *msg; /* VIO message task is for */ 291d10e4ef2Snarayan size_t msglen; /* length of message content */ 292d10e4ef2Snarayan vd_dring_payload_t *request; /* request task will perform */ 293d10e4ef2Snarayan struct buf buf; /* buf(9s) for I/O request */ 2944bac2208Snarayan ldc_mem_handle_t mhdl; /* task memory handle */ 295d10e4ef2Snarayan } vd_task_t; 296d10e4ef2Snarayan 297d10e4ef2Snarayan /* 298d10e4ef2Snarayan * Soft state structure for a virtual disk instance 299d10e4ef2Snarayan */ 3001ae08745Sheppo typedef struct vd { 3011ae08745Sheppo uint_t initialized; /* vdisk initialization flags */ 3021ae08745Sheppo vds_t *vds; /* server for this vdisk */ 303d10e4ef2Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */ 304d10e4ef2Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */ 3051ae08745Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 3063c96341aSnarayan char device_path[MAXPATHLEN + 1]; /* vdisk device */ 3071ae08745Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 308e1ebb9ecSlm66018 uint_t nslices; /* number of slices */ 3091ae08745Sheppo size_t vdisk_size; /* number of blocks in vdisk */ 3101ae08745Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */ 3114bac2208Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 312e1ebb9ecSlm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 3131ae08745Sheppo boolean_t pseudo; /* underlying pseudo dev */ 3143c96341aSnarayan boolean_t file; /* underlying file */ 3153c96341aSnarayan vnode_t *file_vnode; /* file vnode */ 3163c96341aSnarayan size_t file_size; /* file size */ 3174bac2208Snarayan struct dk_efi dk_efi; /* synthetic for slice type */ 3181ae08745Sheppo struct dk_geom dk_geom; /* synthetic for slice type */ 3191ae08745Sheppo struct vtoc vtoc; /* synthetic for slice type */ 3201ae08745Sheppo ldc_status_t ldc_state; /* LDC connection state */ 3211ae08745Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */ 3221ae08745Sheppo size_t max_msglen; /* largest LDC message len */ 3231ae08745Sheppo vd_state_t state; /* client handshake state */ 3241ae08745Sheppo uint8_t xfer_mode; /* transfer mode with client */ 3251ae08745Sheppo uint32_t sid; /* client's session ID */ 3261ae08745Sheppo uint64_t seq_num; /* message sequence number */ 3271ae08745Sheppo uint64_t dring_ident; /* identifier of dring */ 3281ae08745Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */ 3291ae08745Sheppo uint32_t descriptor_size; /* num bytes in desc */ 3301ae08745Sheppo uint32_t dring_len; /* number of dring elements */ 3311ae08745Sheppo caddr_t dring; /* address of dring */ 3323af08d82Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */ 333d10e4ef2Snarayan vd_task_t inband_task; /* task for inband descriptor */ 334d10e4ef2Snarayan vd_task_t *dring_task; /* tasks dring elements */ 335d10e4ef2Snarayan 336d10e4ef2Snarayan kmutex_t lock; /* protects variables below */ 337d10e4ef2Snarayan boolean_t enabled; /* is vdisk enabled? */ 338d10e4ef2Snarayan boolean_t reset_state; /* reset connection state? */ 339d10e4ef2Snarayan boolean_t reset_ldc; /* reset LDC channel? */ 3401ae08745Sheppo } vd_t; 3411ae08745Sheppo 3421ae08745Sheppo typedef struct vds_operation { 3433af08d82Slm66018 char *namep; 3441ae08745Sheppo uint8_t operation; 345d10e4ef2Snarayan int (*start)(vd_task_t *task); 346d10e4ef2Snarayan void (*complete)(void *arg); 3471ae08745Sheppo } vds_operation_t; 3481ae08745Sheppo 3490a55fbb7Slm66018 typedef struct vd_ioctl { 3500a55fbb7Slm66018 uint8_t operation; /* vdisk operation */ 3510a55fbb7Slm66018 const char *operation_name; /* vdisk operation name */ 3520a55fbb7Slm66018 size_t nbytes; /* size of operation buffer */ 3530a55fbb7Slm66018 int cmd; /* corresponding ioctl cmd */ 3540a55fbb7Slm66018 const char *cmd_name; /* ioctl cmd name */ 3550a55fbb7Slm66018 void *arg; /* ioctl cmd argument */ 3560a55fbb7Slm66018 /* convert input vd_buf to output ioctl_arg */ 3570a55fbb7Slm66018 void (*copyin)(void *vd_buf, void *ioctl_arg); 3580a55fbb7Slm66018 /* convert input ioctl_arg to output vd_buf */ 3590a55fbb7Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf); 3600a55fbb7Slm66018 } vd_ioctl_t; 3610a55fbb7Slm66018 3620a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */ 3630a55fbb7Slm66018 #define VD_IDENTITY ((void (*)(void *, void *))-1) 3641ae08745Sheppo 3651ae08745Sheppo 3663c96341aSnarayan static int vds_ldc_retries = VDS_RETRIES; 3673af08d82Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY; 3683c96341aSnarayan static int vds_dev_retries = VDS_RETRIES; 3693c96341aSnarayan static int vds_dev_delay = VDS_DEV_DELAY; 3701ae08745Sheppo static void *vds_state; 3711ae08745Sheppo static uint64_t vds_operations; /* see vds_operation[] definition below */ 3721ae08745Sheppo 3731ae08745Sheppo static int vd_open_flags = VD_OPEN_FLAGS; 3741ae08745Sheppo 375*eba0cb4eSachartre static uint_t vd_file_write_flags = VD_FILE_WRITE_FLAGS; 376*eba0cb4eSachartre 3770a55fbb7Slm66018 /* 3780a55fbb7Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 3790a55fbb7Slm66018 * 3800a55fbb7Slm66018 * Each supported major version should appear only once, paired with (and only 3810a55fbb7Slm66018 * with) its highest supported minor version number (as the protocol requires 3820a55fbb7Slm66018 * supporting all lower minor version numbers as well) 3830a55fbb7Slm66018 */ 3840a55fbb7Slm66018 static const vio_ver_t vds_version[] = {{1, 0}}; 3850a55fbb7Slm66018 static const size_t vds_num_versions = 3860a55fbb7Slm66018 sizeof (vds_version)/sizeof (vds_version[0]); 3870a55fbb7Slm66018 3883af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp); 3893c96341aSnarayan static int vd_setup_vd(vd_t *vd); 3903c96341aSnarayan static boolean_t vd_enabled(vd_t *vd); 3911ae08745Sheppo 392690555a1Sachartre /* 393690555a1Sachartre * Function: 394690555a1Sachartre * vd_file_rw 395690555a1Sachartre * 396690555a1Sachartre * Description: 397690555a1Sachartre * Read or write to a disk on file. 398690555a1Sachartre * 399690555a1Sachartre * Parameters: 400690555a1Sachartre * vd - disk on which the operation is performed. 401690555a1Sachartre * slice - slice on which the operation is performed, 402690555a1Sachartre * VD_FILE_SLICE_NONE indicates that the operation 403690555a1Sachartre * is done on the raw disk. 404690555a1Sachartre * operation - operation to execute: read (VD_OP_BREAD) or 405690555a1Sachartre * write (VD_OP_BWRITE). 406690555a1Sachartre * data - buffer where data are read to or written from. 407690555a1Sachartre * blk - starting block for the operation. 408690555a1Sachartre * len - number of bytes to read or write. 409690555a1Sachartre * 410690555a1Sachartre * Return Code: 411690555a1Sachartre * n >= 0 - success, n indicates the number of bytes read 412690555a1Sachartre * or written. 413690555a1Sachartre * -1 - error. 414690555a1Sachartre */ 415690555a1Sachartre static ssize_t 416690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk, 417690555a1Sachartre size_t len) 418690555a1Sachartre { 419690555a1Sachartre caddr_t maddr; 420690555a1Sachartre size_t offset, maxlen, moffset, mlen, n; 421690555a1Sachartre uint_t smflags; 422690555a1Sachartre enum seg_rw srw; 423690555a1Sachartre 424690555a1Sachartre ASSERT(vd->file); 425690555a1Sachartre ASSERT(len > 0); 426690555a1Sachartre 427690555a1Sachartre if (slice == VD_FILE_SLICE_NONE) { 428690555a1Sachartre /* raw disk access */ 429690555a1Sachartre offset = blk * DEV_BSIZE; 430690555a1Sachartre } else { 431690555a1Sachartre ASSERT(slice >= 0 && slice < V_NUMPAR); 432690555a1Sachartre if (blk >= vd->vtoc.v_part[slice].p_size) { 433690555a1Sachartre /* address past the end of the slice */ 434690555a1Sachartre PR0("req_addr (0x%lx) > psize (0x%lx)", 435690555a1Sachartre blk, vd->vtoc.v_part[slice].p_size); 436690555a1Sachartre return (0); 437690555a1Sachartre } 438690555a1Sachartre 439690555a1Sachartre offset = (vd->vtoc.v_part[slice].p_start + blk) * DEV_BSIZE; 440690555a1Sachartre 441690555a1Sachartre /* 442690555a1Sachartre * If the requested size is greater than the size 443690555a1Sachartre * of the partition, truncate the read/write. 444690555a1Sachartre */ 445690555a1Sachartre maxlen = (vd->vtoc.v_part[slice].p_size - blk) * DEV_BSIZE; 446690555a1Sachartre 447690555a1Sachartre if (len > maxlen) { 448690555a1Sachartre PR0("I/O size truncated to %lu bytes from %lu bytes", 449690555a1Sachartre maxlen, len); 450690555a1Sachartre len = maxlen; 451690555a1Sachartre } 452690555a1Sachartre } 453690555a1Sachartre 454690555a1Sachartre /* 455690555a1Sachartre * We have to ensure that we are reading/writing into the mmap 456690555a1Sachartre * range. If we have a partial disk image (e.g. an image of 457690555a1Sachartre * s0 instead s2) the system can try to access slices that 458690555a1Sachartre * are not included into the disk image. 459690555a1Sachartre */ 460690555a1Sachartre if ((offset + len) >= vd->file_size) { 461690555a1Sachartre PR0("offset + nbytes (0x%lx + 0x%lx) >= " 462690555a1Sachartre "file_size (0x%lx)", offset, len, vd->file_size); 463690555a1Sachartre return (-1); 464690555a1Sachartre } 465690555a1Sachartre 466690555a1Sachartre srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE; 467*eba0cb4eSachartre smflags = (operation == VD_OP_BREAD)? 0 : 468*eba0cb4eSachartre (SM_WRITE | vd_file_write_flags); 469690555a1Sachartre n = len; 470690555a1Sachartre 471690555a1Sachartre do { 472690555a1Sachartre /* 473690555a1Sachartre * segmap_getmapflt() returns a MAXBSIZE chunk which is 474690555a1Sachartre * MAXBSIZE aligned. 475690555a1Sachartre */ 476690555a1Sachartre moffset = offset & MAXBOFFSET; 477690555a1Sachartre mlen = MIN(MAXBSIZE - moffset, n); 478690555a1Sachartre maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset, 479690555a1Sachartre mlen, 1, srw); 480690555a1Sachartre /* 481690555a1Sachartre * Fault in the pages so we can check for error and ensure 482690555a1Sachartre * that we can safely used the mapped address. 483690555a1Sachartre */ 484690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 485690555a1Sachartre F_SOFTLOCK, srw) != 0) { 486690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 487690555a1Sachartre return (-1); 488690555a1Sachartre } 489690555a1Sachartre 490690555a1Sachartre if (operation == VD_OP_BREAD) 491690555a1Sachartre bcopy(maddr + moffset, data, mlen); 492690555a1Sachartre else 493690555a1Sachartre bcopy(data, maddr + moffset, mlen); 494690555a1Sachartre 495690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 496690555a1Sachartre F_SOFTUNLOCK, srw) != 0) { 497690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 498690555a1Sachartre return (-1); 499690555a1Sachartre } 500690555a1Sachartre if (segmap_release(segkmap, maddr, smflags) != 0) 501690555a1Sachartre return (-1); 502690555a1Sachartre n -= mlen; 503690555a1Sachartre offset += mlen; 504690555a1Sachartre data += mlen; 505690555a1Sachartre 506690555a1Sachartre } while (n > 0); 507690555a1Sachartre 508690555a1Sachartre return (len); 509690555a1Sachartre } 510690555a1Sachartre 5111ae08745Sheppo static int 512d10e4ef2Snarayan vd_start_bio(vd_task_t *task) 5131ae08745Sheppo { 5144bac2208Snarayan int rv, status = 0; 515d10e4ef2Snarayan vd_t *vd = task->vd; 516d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 517d10e4ef2Snarayan struct buf *buf = &task->buf; 5184bac2208Snarayan uint8_t mtype; 5193c96341aSnarayan int slice; 520d10e4ef2Snarayan 521d10e4ef2Snarayan ASSERT(vd != NULL); 522d10e4ef2Snarayan ASSERT(request != NULL); 5233c96341aSnarayan 5243c96341aSnarayan slice = request->slice; 5253c96341aSnarayan 5263c96341aSnarayan ASSERT(slice < vd->nslices); 527d10e4ef2Snarayan ASSERT((request->operation == VD_OP_BREAD) || 528d10e4ef2Snarayan (request->operation == VD_OP_BWRITE)); 529d10e4ef2Snarayan 5301ae08745Sheppo if (request->nbytes == 0) 5311ae08745Sheppo return (EINVAL); /* no service for trivial requests */ 5321ae08745Sheppo 533d10e4ef2Snarayan PR1("%s %lu bytes at block %lu", 534d10e4ef2Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write", 535d10e4ef2Snarayan request->nbytes, request->addr); 5361ae08745Sheppo 537d10e4ef2Snarayan bioinit(buf); 538d10e4ef2Snarayan buf->b_flags = B_BUSY; 539d10e4ef2Snarayan buf->b_bcount = request->nbytes; 540d10e4ef2Snarayan buf->b_lblkno = request->addr; 5413c96341aSnarayan buf->b_edev = vd->dev[slice]; 542d10e4ef2Snarayan 5434bac2208Snarayan mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 5444bac2208Snarayan 5454bac2208Snarayan /* Map memory exported by client */ 5464bac2208Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 5474bac2208Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 5484bac2208Snarayan &(buf->b_un.b_addr), NULL); 5494bac2208Snarayan if (status != 0) { 5503af08d82Slm66018 PR0("ldc_mem_map() returned err %d ", status); 5514bac2208Snarayan biofini(buf); 5524bac2208Snarayan return (status); 553d10e4ef2Snarayan } 554d10e4ef2Snarayan 5554bac2208Snarayan status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount); 5564bac2208Snarayan if (status != 0) { 5574bac2208Snarayan (void) ldc_mem_unmap(task->mhdl); 5583af08d82Slm66018 PR0("ldc_mem_acquire() returned err %d ", status); 5594bac2208Snarayan biofini(buf); 5604bac2208Snarayan return (status); 5614bac2208Snarayan } 5624bac2208Snarayan 5634bac2208Snarayan buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE; 5644bac2208Snarayan 565d10e4ef2Snarayan /* Start the block I/O */ 5663c96341aSnarayan if (vd->file) { 567690555a1Sachartre rv = vd_file_rw(vd, slice, request->operation, buf->b_un.b_addr, 568690555a1Sachartre request->addr, request->nbytes); 569690555a1Sachartre if (rv < 0) { 5703c96341aSnarayan request->nbytes = 0; 5713c96341aSnarayan status = EIO; 572690555a1Sachartre } else { 573690555a1Sachartre request->nbytes = rv; 574690555a1Sachartre status = 0; 5753c96341aSnarayan } 5763c96341aSnarayan } else { 5773c96341aSnarayan status = ldi_strategy(vd->ldi_handle[slice], buf); 5783c96341aSnarayan if (status == 0) 5793c96341aSnarayan return (EINPROGRESS); /* will complete on completionq */ 5803c96341aSnarayan } 5813c96341aSnarayan 582d10e4ef2Snarayan /* Clean up after error */ 5834bac2208Snarayan rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount); 5844bac2208Snarayan if (rv) { 5853af08d82Slm66018 PR0("ldc_mem_release() returned err %d ", rv); 5864bac2208Snarayan } 5874bac2208Snarayan rv = ldc_mem_unmap(task->mhdl); 5884bac2208Snarayan if (rv) { 5893af08d82Slm66018 PR0("ldc_mem_unmap() returned err %d ", status); 5904bac2208Snarayan } 5914bac2208Snarayan 592d10e4ef2Snarayan biofini(buf); 593d10e4ef2Snarayan return (status); 594d10e4ef2Snarayan } 595d10e4ef2Snarayan 596d10e4ef2Snarayan static int 597d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 598d10e4ef2Snarayan { 5993af08d82Slm66018 int status; 600d10e4ef2Snarayan size_t nbytes; 601d10e4ef2Snarayan 6023af08d82Slm66018 do { 603d10e4ef2Snarayan nbytes = msglen; 604d10e4ef2Snarayan status = ldc_write(ldc_handle, msg, &nbytes); 6053af08d82Slm66018 if (status != EWOULDBLOCK) 6063af08d82Slm66018 break; 6073af08d82Slm66018 drv_usecwait(vds_ldc_delay); 6083af08d82Slm66018 } while (status == EWOULDBLOCK); 609d10e4ef2Snarayan 610d10e4ef2Snarayan if (status != 0) { 6113af08d82Slm66018 if (status != ECONNRESET) 6123af08d82Slm66018 PR0("ldc_write() returned errno %d", status); 613d10e4ef2Snarayan return (status); 614d10e4ef2Snarayan } else if (nbytes != msglen) { 6153af08d82Slm66018 PR0("ldc_write() performed only partial write"); 616d10e4ef2Snarayan return (EIO); 617d10e4ef2Snarayan } 618d10e4ef2Snarayan 619d10e4ef2Snarayan PR1("SENT %lu bytes", msglen); 620d10e4ef2Snarayan return (0); 621d10e4ef2Snarayan } 622d10e4ef2Snarayan 623d10e4ef2Snarayan static void 624d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc) 625d10e4ef2Snarayan { 626d10e4ef2Snarayan mutex_enter(&vd->lock); 627d10e4ef2Snarayan vd->reset_state = B_TRUE; 628d10e4ef2Snarayan vd->reset_ldc = reset_ldc; 629d10e4ef2Snarayan mutex_exit(&vd->lock); 630d10e4ef2Snarayan } 631d10e4ef2Snarayan 632d10e4ef2Snarayan /* 633d10e4ef2Snarayan * Reset the state of the connection with a client, if needed; reset the LDC 634d10e4ef2Snarayan * transport as well, if needed. This function should only be called from the 6353af08d82Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 636d10e4ef2Snarayan */ 637d10e4ef2Snarayan static void 638d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd) 639d10e4ef2Snarayan { 640d10e4ef2Snarayan int status = 0; 641d10e4ef2Snarayan 642d10e4ef2Snarayan mutex_enter(&vd->lock); 643d10e4ef2Snarayan if (!vd->reset_state) { 644d10e4ef2Snarayan ASSERT(!vd->reset_ldc); 645d10e4ef2Snarayan mutex_exit(&vd->lock); 646d10e4ef2Snarayan return; 647d10e4ef2Snarayan } 648d10e4ef2Snarayan mutex_exit(&vd->lock); 649d10e4ef2Snarayan 650d10e4ef2Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd)); 651d10e4ef2Snarayan 652d10e4ef2Snarayan /* 653d10e4ef2Snarayan * Let any asynchronous I/O complete before possibly pulling the rug 654d10e4ef2Snarayan * out from under it; defer checking vd->reset_ldc, as one of the 655d10e4ef2Snarayan * asynchronous tasks might set it 656d10e4ef2Snarayan */ 657d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 658d10e4ef2Snarayan 6593c96341aSnarayan if (vd->file) { 6603c96341aSnarayan status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred); 6613c96341aSnarayan if (status) { 6623c96341aSnarayan PR0("VOP_FSYNC returned errno %d", status); 6633c96341aSnarayan } 6643c96341aSnarayan } 6653c96341aSnarayan 666d10e4ef2Snarayan if ((vd->initialized & VD_DRING) && 667d10e4ef2Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 6683af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 669d10e4ef2Snarayan 6703af08d82Slm66018 vd_free_dring_task(vd); 6713af08d82Slm66018 6723af08d82Slm66018 /* Free the staging buffer for msgs */ 6733af08d82Slm66018 if (vd->vio_msgp != NULL) { 6743af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 6753af08d82Slm66018 vd->vio_msgp = NULL; 676d10e4ef2Snarayan } 677d10e4ef2Snarayan 6783af08d82Slm66018 /* Free the inband message buffer */ 6793af08d82Slm66018 if (vd->inband_task.msg != NULL) { 6803af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 6813af08d82Slm66018 vd->inband_task.msg = NULL; 6823af08d82Slm66018 } 683d10e4ef2Snarayan 684d10e4ef2Snarayan mutex_enter(&vd->lock); 6853af08d82Slm66018 6863af08d82Slm66018 if (vd->reset_ldc) 6873af08d82Slm66018 PR0("taking down LDC channel"); 688e1ebb9ecSlm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 6893af08d82Slm66018 PR0("ldc_down() returned errno %d", status); 690d10e4ef2Snarayan 691d10e4ef2Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 692d10e4ef2Snarayan vd->state = VD_STATE_INIT; 693d10e4ef2Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 694d10e4ef2Snarayan 6953af08d82Slm66018 /* Allocate the staging buffer */ 6963af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 6973af08d82Slm66018 6983af08d82Slm66018 PR0("calling ldc_up\n"); 6993af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 7003af08d82Slm66018 701d10e4ef2Snarayan vd->reset_state = B_FALSE; 702d10e4ef2Snarayan vd->reset_ldc = B_FALSE; 7033af08d82Slm66018 704d10e4ef2Snarayan mutex_exit(&vd->lock); 705d10e4ef2Snarayan } 706d10e4ef2Snarayan 7073af08d82Slm66018 static void vd_recv_msg(void *arg); 7083af08d82Slm66018 7093af08d82Slm66018 static void 7103af08d82Slm66018 vd_mark_in_reset(vd_t *vd) 7113af08d82Slm66018 { 7123af08d82Slm66018 int status; 7133af08d82Slm66018 7143af08d82Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n"); 7153af08d82Slm66018 7163af08d82Slm66018 vd_need_reset(vd, B_FALSE); 7173af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 7183af08d82Slm66018 if (status == DDI_FAILURE) { 7193af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 7203af08d82Slm66018 vd_need_reset(vd, B_TRUE); 7213af08d82Slm66018 return; 7223af08d82Slm66018 } 7233af08d82Slm66018 } 7243af08d82Slm66018 725d10e4ef2Snarayan static int 7263c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes) 727d10e4ef2Snarayan { 728d10e4ef2Snarayan boolean_t accepted; 729d10e4ef2Snarayan int status; 730d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 731d10e4ef2Snarayan 7323af08d82Slm66018 if (vd->reset_state) 7333af08d82Slm66018 return (0); 734d10e4ef2Snarayan 735d10e4ef2Snarayan /* Acquire the element */ 7363af08d82Slm66018 if (!vd->reset_state && 7373af08d82Slm66018 (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 7383af08d82Slm66018 if (status == ECONNRESET) { 7393af08d82Slm66018 vd_mark_in_reset(vd); 7403af08d82Slm66018 return (0); 7413af08d82Slm66018 } else { 7423af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", 7433af08d82Slm66018 status); 744d10e4ef2Snarayan return (status); 745d10e4ef2Snarayan } 7463af08d82Slm66018 } 747d10e4ef2Snarayan 748d10e4ef2Snarayan /* Set the element's status and mark it done */ 749d10e4ef2Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 750d10e4ef2Snarayan if (accepted) { 7513c96341aSnarayan elem->payload.nbytes = elem_nbytes; 752d10e4ef2Snarayan elem->payload.status = elem_status; 753d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_DONE; 754d10e4ef2Snarayan } else { 755d10e4ef2Snarayan /* Perhaps client timed out waiting for I/O... */ 7563af08d82Slm66018 PR0("element %u no longer \"accepted\"", idx); 757d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 758d10e4ef2Snarayan } 759d10e4ef2Snarayan /* Release the element */ 7603af08d82Slm66018 if (!vd->reset_state && 7613af08d82Slm66018 (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 7623af08d82Slm66018 if (status == ECONNRESET) { 7633af08d82Slm66018 vd_mark_in_reset(vd); 7643af08d82Slm66018 return (0); 7653af08d82Slm66018 } else { 7663af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", 7673af08d82Slm66018 status); 768d10e4ef2Snarayan return (status); 769d10e4ef2Snarayan } 7703af08d82Slm66018 } 771d10e4ef2Snarayan 772d10e4ef2Snarayan return (accepted ? 0 : EINVAL); 773d10e4ef2Snarayan } 774d10e4ef2Snarayan 775d10e4ef2Snarayan static void 776d10e4ef2Snarayan vd_complete_bio(void *arg) 777d10e4ef2Snarayan { 778d10e4ef2Snarayan int status = 0; 779d10e4ef2Snarayan vd_task_t *task = (vd_task_t *)arg; 780d10e4ef2Snarayan vd_t *vd = task->vd; 781d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 782d10e4ef2Snarayan struct buf *buf = &task->buf; 783d10e4ef2Snarayan 784d10e4ef2Snarayan 785d10e4ef2Snarayan ASSERT(vd != NULL); 786d10e4ef2Snarayan ASSERT(request != NULL); 787d10e4ef2Snarayan ASSERT(task->msg != NULL); 788d10e4ef2Snarayan ASSERT(task->msglen >= sizeof (*task->msg)); 7893c96341aSnarayan ASSERT(!vd->file); 790d10e4ef2Snarayan 791d10e4ef2Snarayan /* Wait for the I/O to complete */ 792d10e4ef2Snarayan request->status = biowait(buf); 793d10e4ef2Snarayan 7943c96341aSnarayan /* return back the number of bytes read/written */ 7953c96341aSnarayan request->nbytes = buf->b_bcount - buf->b_resid; 7963c96341aSnarayan 7974bac2208Snarayan /* Release the buffer */ 7983af08d82Slm66018 if (!vd->reset_state) 7994bac2208Snarayan status = ldc_mem_release(task->mhdl, 0, buf->b_bcount); 8004bac2208Snarayan if (status) { 8013af08d82Slm66018 PR0("ldc_mem_release() returned errno %d copying to " 8023af08d82Slm66018 "client", status); 8033af08d82Slm66018 if (status == ECONNRESET) { 8043af08d82Slm66018 vd_mark_in_reset(vd); 8053af08d82Slm66018 } 8061ae08745Sheppo } 8071ae08745Sheppo 8083af08d82Slm66018 /* Unmap the memory, even if in reset */ 8094bac2208Snarayan status = ldc_mem_unmap(task->mhdl); 8104bac2208Snarayan if (status) { 8113af08d82Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client", 8124bac2208Snarayan status); 8133af08d82Slm66018 if (status == ECONNRESET) { 8143af08d82Slm66018 vd_mark_in_reset(vd); 8153af08d82Slm66018 } 8164bac2208Snarayan } 8174bac2208Snarayan 818d10e4ef2Snarayan biofini(buf); 8191ae08745Sheppo 820d10e4ef2Snarayan /* Update the dring element for a dring client */ 8213af08d82Slm66018 if (!vd->reset_state && (status == 0) && 8223af08d82Slm66018 (vd->xfer_mode == VIO_DRING_MODE)) { 8233c96341aSnarayan status = vd_mark_elem_done(vd, task->index, 8243c96341aSnarayan request->status, request->nbytes); 8253af08d82Slm66018 if (status == ECONNRESET) 8263af08d82Slm66018 vd_mark_in_reset(vd); 8273af08d82Slm66018 } 8281ae08745Sheppo 829d10e4ef2Snarayan /* 830d10e4ef2Snarayan * If a transport error occurred, arrange to "nack" the message when 831d10e4ef2Snarayan * the final task in the descriptor element range completes 832d10e4ef2Snarayan */ 833d10e4ef2Snarayan if (status != 0) 834d10e4ef2Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 8351ae08745Sheppo 836d10e4ef2Snarayan /* 837d10e4ef2Snarayan * Only the final task for a range of elements will respond to and 838d10e4ef2Snarayan * free the message 839d10e4ef2Snarayan */ 8403af08d82Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) { 841d10e4ef2Snarayan return; 8423af08d82Slm66018 } 8431ae08745Sheppo 844d10e4ef2Snarayan /* 845d10e4ef2Snarayan * Send the "ack" or "nack" back to the client; if sending the message 846d10e4ef2Snarayan * via LDC fails, arrange to reset both the connection state and LDC 847d10e4ef2Snarayan * itself 848d10e4ef2Snarayan */ 849d10e4ef2Snarayan PR1("Sending %s", 850d10e4ef2Snarayan (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 8513af08d82Slm66018 if (!vd->reset_state) { 8523af08d82Slm66018 status = send_msg(vd->ldc_handle, task->msg, task->msglen); 8533af08d82Slm66018 switch (status) { 8543af08d82Slm66018 case 0: 8553af08d82Slm66018 break; 8563af08d82Slm66018 case ECONNRESET: 8573af08d82Slm66018 vd_mark_in_reset(vd); 8583af08d82Slm66018 break; 8593af08d82Slm66018 default: 8603af08d82Slm66018 PR0("initiating full reset"); 861d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 8623af08d82Slm66018 break; 8633af08d82Slm66018 } 8643af08d82Slm66018 } 8651ae08745Sheppo } 8661ae08745Sheppo 8670a55fbb7Slm66018 static void 8680a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg) 8690a55fbb7Slm66018 { 8700a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 8710a55fbb7Slm66018 } 8720a55fbb7Slm66018 8730a55fbb7Slm66018 static void 8740a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg) 8750a55fbb7Slm66018 { 8760a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg); 8770a55fbb7Slm66018 } 8780a55fbb7Slm66018 8790a55fbb7Slm66018 static void 8800a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 8810a55fbb7Slm66018 { 8820a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 8830a55fbb7Slm66018 } 8840a55fbb7Slm66018 8850a55fbb7Slm66018 static void 8860a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 8870a55fbb7Slm66018 { 8880a55fbb7Slm66018 VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 8890a55fbb7Slm66018 } 8900a55fbb7Slm66018 8914bac2208Snarayan static void 8924bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg) 8934bac2208Snarayan { 8944bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 8954bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 8964bac2208Snarayan 8974bac2208Snarayan dk_efi->dki_lba = vd_efi->lba; 8984bac2208Snarayan dk_efi->dki_length = vd_efi->length; 8994bac2208Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 9004bac2208Snarayan } 9014bac2208Snarayan 9024bac2208Snarayan static void 9034bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf) 9044bac2208Snarayan { 9054bac2208Snarayan int len; 9064bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 9074bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 9084bac2208Snarayan 9094bac2208Snarayan len = vd_efi->length; 9104bac2208Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi); 9114bac2208Snarayan kmem_free(dk_efi->dki_data, len); 9124bac2208Snarayan } 9134bac2208Snarayan 9144bac2208Snarayan static void 9154bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg) 9164bac2208Snarayan { 9174bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 9184bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 9194bac2208Snarayan 9204bac2208Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 9214bac2208Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi); 9224bac2208Snarayan } 9234bac2208Snarayan 9244bac2208Snarayan static void 9254bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf) 9264bac2208Snarayan { 9274bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 9284bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 9294bac2208Snarayan 9304bac2208Snarayan kmem_free(dk_efi->dki_data, vd_efi->length); 9314bac2208Snarayan } 9324bac2208Snarayan 9334bac2208Snarayan static int 9344bac2208Snarayan vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc, vd_disk_label_t *label) 9354bac2208Snarayan { 9364bac2208Snarayan int status, rval; 9374bac2208Snarayan struct dk_gpt *efi; 9384bac2208Snarayan size_t efi_len; 9394bac2208Snarayan 9404bac2208Snarayan *label = VD_DISK_LABEL_UNK; 9414bac2208Snarayan 9424bac2208Snarayan status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc, 9434bac2208Snarayan (vd_open_flags | FKIOCTL), kcred, &rval); 9444bac2208Snarayan 9454bac2208Snarayan if (status == 0) { 9464bac2208Snarayan *label = VD_DISK_LABEL_VTOC; 9474bac2208Snarayan return (0); 9484bac2208Snarayan } else if (status != ENOTSUP) { 9493af08d82Slm66018 PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status); 9504bac2208Snarayan return (status); 9514bac2208Snarayan } 9524bac2208Snarayan 9534bac2208Snarayan status = vds_efi_alloc_and_read(handle, &efi, &efi_len); 9544bac2208Snarayan 9554bac2208Snarayan if (status) { 9563af08d82Slm66018 PR0("vds_efi_alloc_and_read returned error %d", status); 9574bac2208Snarayan return (status); 9584bac2208Snarayan } 9594bac2208Snarayan 9604bac2208Snarayan *label = VD_DISK_LABEL_EFI; 9614bac2208Snarayan vd_efi_to_vtoc(efi, vtoc); 9624bac2208Snarayan vd_efi_free(efi, efi_len); 9634bac2208Snarayan 9644bac2208Snarayan return (0); 9654bac2208Snarayan } 9664bac2208Snarayan 967690555a1Sachartre static ushort_t 9683c96341aSnarayan vd_lbl2cksum(struct dk_label *label) 9693c96341aSnarayan { 9703c96341aSnarayan int count; 971690555a1Sachartre ushort_t sum, *sp; 9723c96341aSnarayan 9733c96341aSnarayan count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 974690555a1Sachartre sp = (ushort_t *)label; 9753c96341aSnarayan sum = 0; 9763c96341aSnarayan while (count--) { 9773c96341aSnarayan sum ^= *sp++; 9783c96341aSnarayan } 9793c96341aSnarayan 9803c96341aSnarayan return (sum); 9813c96341aSnarayan } 9823c96341aSnarayan 9831ae08745Sheppo static int 9840a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 9851ae08745Sheppo { 9864bac2208Snarayan dk_efi_t *dk_ioc; 987690555a1Sachartre struct dk_label label; 988690555a1Sachartre struct vtoc *vtoc; 9893c96341aSnarayan int i; 9904bac2208Snarayan 9914bac2208Snarayan switch (vd->vdisk_label) { 9924bac2208Snarayan 9934bac2208Snarayan case VD_DISK_LABEL_VTOC: 9944bac2208Snarayan 9951ae08745Sheppo switch (cmd) { 9961ae08745Sheppo case DKIOCGGEOM: 9970a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 9980a55fbb7Slm66018 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 9991ae08745Sheppo return (0); 10001ae08745Sheppo case DKIOCGVTOC: 10010a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 10020a55fbb7Slm66018 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 10031ae08745Sheppo return (0); 10043c96341aSnarayan case DKIOCSVTOC: 10053c96341aSnarayan if (!vd->file) 10063c96341aSnarayan return (ENOTSUP); 10073c96341aSnarayan ASSERT(ioctl_arg != NULL); 1008690555a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 1009690555a1Sachartre 1010690555a1Sachartre if (vtoc->v_sanity != VTOC_SANE || 1011690555a1Sachartre vtoc->v_sectorsz != DEV_BSIZE || 1012690555a1Sachartre vtoc->v_nparts != V_NUMPAR) 1013690555a1Sachartre return (EINVAL); 1014690555a1Sachartre 1015690555a1Sachartre bzero(&label, sizeof (label)); 1016690555a1Sachartre label.dkl_ncyl = vd->dk_geom.dkg_ncyl; 1017690555a1Sachartre label.dkl_acyl = vd->dk_geom.dkg_acyl; 1018690555a1Sachartre label.dkl_pcyl = vd->dk_geom.dkg_pcyl; 1019690555a1Sachartre label.dkl_nhead = vd->dk_geom.dkg_nhead; 1020690555a1Sachartre label.dkl_nsect = vd->dk_geom.dkg_nsect; 1021690555a1Sachartre label.dkl_intrlv = vd->dk_geom.dkg_intrlv; 1022690555a1Sachartre label.dkl_apc = vd->dk_geom.dkg_apc; 1023690555a1Sachartre label.dkl_rpm = vd->dk_geom.dkg_rpm; 1024690555a1Sachartre label.dkl_write_reinstruct = 1025690555a1Sachartre vd->dk_geom.dkg_write_reinstruct; 1026690555a1Sachartre label.dkl_read_reinstruct = 1027690555a1Sachartre vd->dk_geom.dkg_read_reinstruct; 1028690555a1Sachartre 1029690555a1Sachartre label.dkl_vtoc.v_nparts = vtoc->v_nparts; 1030690555a1Sachartre label.dkl_vtoc.v_sanity = vtoc->v_sanity; 1031690555a1Sachartre label.dkl_vtoc.v_version = vtoc->v_version; 1032690555a1Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 1033690555a1Sachartre label.dkl_vtoc.v_timestamp[i] = 1034690555a1Sachartre vtoc->timestamp[i]; 1035690555a1Sachartre label.dkl_vtoc.v_part[i].p_tag = 1036690555a1Sachartre vtoc->v_part[i].p_tag; 1037690555a1Sachartre label.dkl_vtoc.v_part[i].p_flag = 1038690555a1Sachartre vtoc->v_part[i].p_flag; 1039690555a1Sachartre label.dkl_map[i].dkl_cylno = 1040690555a1Sachartre vtoc->v_part[i].p_start / 1041690555a1Sachartre (label.dkl_nhead * label.dkl_nsect); 1042690555a1Sachartre label.dkl_map[i].dkl_nblk = 1043690555a1Sachartre vtoc->v_part[i].p_size; 10443c96341aSnarayan } 1045690555a1Sachartre bcopy(vtoc->v_asciilabel, label.dkl_asciilabel, 1046690555a1Sachartre LEN_DKL_ASCII); 1047690555a1Sachartre bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume, 1048690555a1Sachartre LEN_DKL_VVOL); 1049690555a1Sachartre bcopy(vtoc->v_bootinfo, label.dkl_vtoc.v_bootinfo, 1050690555a1Sachartre sizeof (vtoc->v_bootinfo)); 10513c96341aSnarayan 10523c96341aSnarayan /* re-compute checksum */ 1053690555a1Sachartre label.dkl_magic = DKL_MAGIC; 1054690555a1Sachartre label.dkl_cksum = vd_lbl2cksum(&label); 1055690555a1Sachartre 1056690555a1Sachartre /* write label to file */ 1057690555a1Sachartre if (VD_FILE_LABEL_WRITE(vd, &label) < 0) 1058690555a1Sachartre return (EIO); 1059690555a1Sachartre 1060690555a1Sachartre /* update the cached vdisk VTOC */ 1061690555a1Sachartre bcopy(vtoc, &vd->vtoc, sizeof (vd->vtoc)); 10623c96341aSnarayan 10633c96341aSnarayan return (0); 10641ae08745Sheppo default: 10651ae08745Sheppo return (ENOTSUP); 10661ae08745Sheppo } 10674bac2208Snarayan 10684bac2208Snarayan case VD_DISK_LABEL_EFI: 10694bac2208Snarayan 10704bac2208Snarayan switch (cmd) { 10714bac2208Snarayan case DKIOCGETEFI: 10724bac2208Snarayan ASSERT(ioctl_arg != NULL); 10734bac2208Snarayan dk_ioc = (dk_efi_t *)ioctl_arg; 10744bac2208Snarayan if (dk_ioc->dki_length < vd->dk_efi.dki_length) 10754bac2208Snarayan return (EINVAL); 10764bac2208Snarayan bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data, 10774bac2208Snarayan vd->dk_efi.dki_length); 10784bac2208Snarayan return (0); 10794bac2208Snarayan default: 10804bac2208Snarayan return (ENOTSUP); 10814bac2208Snarayan } 10824bac2208Snarayan 10834bac2208Snarayan default: 10844bac2208Snarayan return (ENOTSUP); 10854bac2208Snarayan } 10861ae08745Sheppo } 10871ae08745Sheppo 10881ae08745Sheppo static int 10890a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 10901ae08745Sheppo { 10911ae08745Sheppo int rval = 0, status; 10921ae08745Sheppo size_t nbytes = request->nbytes; /* modifiable copy */ 10931ae08745Sheppo 10941ae08745Sheppo 10951ae08745Sheppo ASSERT(request->slice < vd->nslices); 10961ae08745Sheppo PR0("Performing %s", ioctl->operation_name); 10971ae08745Sheppo 10980a55fbb7Slm66018 /* Get data from client and convert, if necessary */ 10990a55fbb7Slm66018 if (ioctl->copyin != NULL) { 11001ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 11011ae08745Sheppo PR1("Getting \"arg\" data from client"); 11021ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 11031ae08745Sheppo request->cookie, request->ncookies, 11041ae08745Sheppo LDC_COPY_IN)) != 0) { 11053af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 11061ae08745Sheppo "copying from client", status); 11071ae08745Sheppo return (status); 11081ae08745Sheppo } 11090a55fbb7Slm66018 11100a55fbb7Slm66018 /* Convert client's data, if necessary */ 11110a55fbb7Slm66018 if (ioctl->copyin == VD_IDENTITY) /* use client buffer */ 11120a55fbb7Slm66018 ioctl->arg = buf; 11130a55fbb7Slm66018 else /* convert client vdisk operation data to ioctl data */ 11140a55fbb7Slm66018 (ioctl->copyin)(buf, (void *)ioctl->arg); 11151ae08745Sheppo } 11161ae08745Sheppo 11171ae08745Sheppo /* 11181ae08745Sheppo * Handle single-slice block devices internally; otherwise, have the 11191ae08745Sheppo * real driver perform the ioctl() 11201ae08745Sheppo */ 11213c96341aSnarayan if (vd->file || (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo)) { 11220a55fbb7Slm66018 if ((status = vd_do_slice_ioctl(vd, ioctl->cmd, 11230a55fbb7Slm66018 (void *)ioctl->arg)) != 0) 11241ae08745Sheppo return (status); 11251ae08745Sheppo } else if ((status = ldi_ioctl(vd->ldi_handle[request->slice], 1126d10e4ef2Snarayan ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL), 1127d10e4ef2Snarayan kcred, &rval)) != 0) { 11281ae08745Sheppo PR0("ldi_ioctl(%s) = errno %d", ioctl->cmd_name, status); 11291ae08745Sheppo return (status); 11301ae08745Sheppo } 11311ae08745Sheppo #ifdef DEBUG 11321ae08745Sheppo if (rval != 0) { 11333af08d82Slm66018 PR0("%s set rval = %d, which is not being returned to client", 11341ae08745Sheppo ioctl->cmd_name, rval); 11351ae08745Sheppo } 11361ae08745Sheppo #endif /* DEBUG */ 11371ae08745Sheppo 11380a55fbb7Slm66018 /* Convert data and send to client, if necessary */ 11390a55fbb7Slm66018 if (ioctl->copyout != NULL) { 11401ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 11411ae08745Sheppo PR1("Sending \"arg\" data to client"); 11420a55fbb7Slm66018 11430a55fbb7Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */ 11440a55fbb7Slm66018 if (ioctl->copyout != VD_IDENTITY) 11450a55fbb7Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf); 11460a55fbb7Slm66018 11471ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 11481ae08745Sheppo request->cookie, request->ncookies, 11491ae08745Sheppo LDC_COPY_OUT)) != 0) { 11503af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 11511ae08745Sheppo "copying to client", status); 11521ae08745Sheppo return (status); 11531ae08745Sheppo } 11541ae08745Sheppo } 11551ae08745Sheppo 11561ae08745Sheppo return (status); 11571ae08745Sheppo } 11581ae08745Sheppo 11591ae08745Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 11601ae08745Sheppo static int 1161d10e4ef2Snarayan vd_ioctl(vd_task_t *task) 11621ae08745Sheppo { 116334683adeSsg70180 int i, status, rc; 11641ae08745Sheppo void *buf = NULL; 11650a55fbb7Slm66018 struct dk_geom dk_geom = {0}; 11660a55fbb7Slm66018 struct vtoc vtoc = {0}; 11674bac2208Snarayan struct dk_efi dk_efi = {0}; 1168d10e4ef2Snarayan vd_t *vd = task->vd; 1169d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 11700a55fbb7Slm66018 vd_ioctl_t ioctl[] = { 11710a55fbb7Slm66018 /* Command (no-copy) operations */ 11720a55fbb7Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 11730a55fbb7Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 11740a55fbb7Slm66018 NULL, NULL, NULL}, 11750a55fbb7Slm66018 11760a55fbb7Slm66018 /* "Get" (copy-out) operations */ 11770a55fbb7Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 11780a55fbb7Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 11794bac2208Snarayan NULL, VD_IDENTITY, VD_IDENTITY}, 11800a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 11810a55fbb7Slm66018 RNDSIZE(vd_geom_t), 11820a55fbb7Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 11830a55fbb7Slm66018 &dk_geom, NULL, dk_geom2vd_geom}, 11840a55fbb7Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 11850a55fbb7Slm66018 DKIOCGVTOC, STRINGIZE(DKIOCGVTOC), 11860a55fbb7Slm66018 &vtoc, NULL, vtoc2vd_vtoc}, 11874bac2208Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 11884bac2208Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 11894bac2208Snarayan &dk_efi, vd_get_efi_in, vd_get_efi_out}, 11900a55fbb7Slm66018 11910a55fbb7Slm66018 /* "Set" (copy-in) operations */ 11920a55fbb7Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 11930a55fbb7Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 11944bac2208Snarayan NULL, VD_IDENTITY, VD_IDENTITY}, 11950a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 11960a55fbb7Slm66018 RNDSIZE(vd_geom_t), 11970a55fbb7Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 11980a55fbb7Slm66018 &dk_geom, vd_geom2dk_geom, NULL}, 11990a55fbb7Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 12000a55fbb7Slm66018 DKIOCSVTOC, STRINGIZE(DKIOCSVTOC), 12010a55fbb7Slm66018 &vtoc, vd_vtoc2vtoc, NULL}, 12024bac2208Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 12034bac2208Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 12044bac2208Snarayan &dk_efi, vd_set_efi_in, vd_set_efi_out}, 12050a55fbb7Slm66018 }; 12061ae08745Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 12071ae08745Sheppo 12081ae08745Sheppo 1209d10e4ef2Snarayan ASSERT(vd != NULL); 1210d10e4ef2Snarayan ASSERT(request != NULL); 12111ae08745Sheppo ASSERT(request->slice < vd->nslices); 12121ae08745Sheppo 12131ae08745Sheppo /* 12141ae08745Sheppo * Determine ioctl corresponding to caller's "operation" and 12151ae08745Sheppo * validate caller's "nbytes" 12161ae08745Sheppo */ 12171ae08745Sheppo for (i = 0; i < nioctls; i++) { 12181ae08745Sheppo if (request->operation == ioctl[i].operation) { 12190a55fbb7Slm66018 /* LDC memory operations require 8-byte multiples */ 12200a55fbb7Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 12210a55fbb7Slm66018 12224bac2208Snarayan if (request->operation == VD_OP_GET_EFI || 12234bac2208Snarayan request->operation == VD_OP_SET_EFI) { 12244bac2208Snarayan if (request->nbytes >= ioctl[i].nbytes) 12254bac2208Snarayan break; 12263af08d82Slm66018 PR0("%s: Expected at least nbytes = %lu, " 12274bac2208Snarayan "got %lu", ioctl[i].operation_name, 12284bac2208Snarayan ioctl[i].nbytes, request->nbytes); 12294bac2208Snarayan return (EINVAL); 12304bac2208Snarayan } 12314bac2208Snarayan 12320a55fbb7Slm66018 if (request->nbytes != ioctl[i].nbytes) { 12333af08d82Slm66018 PR0("%s: Expected nbytes = %lu, got %lu", 12340a55fbb7Slm66018 ioctl[i].operation_name, ioctl[i].nbytes, 12350a55fbb7Slm66018 request->nbytes); 12361ae08745Sheppo return (EINVAL); 12371ae08745Sheppo } 12381ae08745Sheppo 12391ae08745Sheppo break; 12401ae08745Sheppo } 12411ae08745Sheppo } 12421ae08745Sheppo ASSERT(i < nioctls); /* because "operation" already validated */ 12431ae08745Sheppo 12441ae08745Sheppo if (request->nbytes) 12451ae08745Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP); 12461ae08745Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 12471ae08745Sheppo if (request->nbytes) 12481ae08745Sheppo kmem_free(buf, request->nbytes); 12493c96341aSnarayan if (!vd->file && vd->vdisk_type == VD_DISK_TYPE_DISK && 12504bac2208Snarayan (request->operation == VD_OP_SET_VTOC || 125134683adeSsg70180 request->operation == VD_OP_SET_EFI)) { 125234683adeSsg70180 /* update disk information */ 125334683adeSsg70180 rc = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, 125434683adeSsg70180 &vd->vdisk_label); 125534683adeSsg70180 if (rc != 0) 125634683adeSsg70180 PR0("vd_read_vtoc return error %d", rc); 125734683adeSsg70180 } 1258d10e4ef2Snarayan PR0("Returning %d", status); 12591ae08745Sheppo return (status); 12601ae08745Sheppo } 12611ae08745Sheppo 12624bac2208Snarayan static int 12634bac2208Snarayan vd_get_devid(vd_task_t *task) 12644bac2208Snarayan { 12654bac2208Snarayan vd_t *vd = task->vd; 12664bac2208Snarayan vd_dring_payload_t *request = task->request; 12674bac2208Snarayan vd_devid_t *vd_devid; 12684bac2208Snarayan impl_devid_t *devid; 12694bac2208Snarayan int status, bufid_len, devid_len, len; 12703af08d82Slm66018 int bufbytes; 12714bac2208Snarayan 12723af08d82Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes); 12734bac2208Snarayan 12743c96341aSnarayan if (vd->file) { 12753c96341aSnarayan /* no devid for disk on file */ 12763c96341aSnarayan return (ENOENT); 12773c96341aSnarayan } 12783c96341aSnarayan 12794bac2208Snarayan if (ddi_lyr_get_devid(vd->dev[request->slice], 12804bac2208Snarayan (ddi_devid_t *)&devid) != DDI_SUCCESS) { 12814bac2208Snarayan /* the most common failure is that no devid is available */ 12823af08d82Slm66018 PR2("No Device ID"); 12834bac2208Snarayan return (ENOENT); 12844bac2208Snarayan } 12854bac2208Snarayan 12864bac2208Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 12874bac2208Snarayan devid_len = DEVID_GETLEN(devid); 12884bac2208Snarayan 12893af08d82Slm66018 /* 12903af08d82Slm66018 * Save the buffer size here for use in deallocation. 12913af08d82Slm66018 * The actual number of bytes copied is returned in 12923af08d82Slm66018 * the 'nbytes' field of the request structure. 12933af08d82Slm66018 */ 12943af08d82Slm66018 bufbytes = request->nbytes; 12953af08d82Slm66018 12963af08d82Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 12974bac2208Snarayan vd_devid->length = devid_len; 12984bac2208Snarayan vd_devid->type = DEVID_GETTYPE(devid); 12994bac2208Snarayan 13004bac2208Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len; 13014bac2208Snarayan 13024bac2208Snarayan bcopy(devid->did_id, vd_devid->id, len); 13034bac2208Snarayan 13044bac2208Snarayan /* LDC memory operations require 8-byte multiples */ 13054bac2208Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0); 13064bac2208Snarayan 13074bac2208Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 13084bac2208Snarayan &request->nbytes, request->cookie, request->ncookies, 13094bac2208Snarayan LDC_COPY_OUT)) != 0) { 13103af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client", 13114bac2208Snarayan status); 13124bac2208Snarayan } 13133af08d82Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes); 13144bac2208Snarayan 13153af08d82Slm66018 kmem_free(vd_devid, bufbytes); 13164bac2208Snarayan ddi_devid_free((ddi_devid_t)devid); 13174bac2208Snarayan 13184bac2208Snarayan return (status); 13194bac2208Snarayan } 13204bac2208Snarayan 13211ae08745Sheppo /* 13221ae08745Sheppo * Define the supported operations once the functions for performing them have 13231ae08745Sheppo * been defined 13241ae08745Sheppo */ 13251ae08745Sheppo static const vds_operation_t vds_operation[] = { 13263af08d82Slm66018 #define X(_s) #_s, _s 13273af08d82Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 13283af08d82Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 13293af08d82Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 13303af08d82Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 13313af08d82Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 13323af08d82Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 13333af08d82Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 13343af08d82Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 13353af08d82Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 13363af08d82Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 13373af08d82Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 13383af08d82Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 13393af08d82Slm66018 #undef X 13401ae08745Sheppo }; 13411ae08745Sheppo 13421ae08745Sheppo static const size_t vds_noperations = 13431ae08745Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0])); 13441ae08745Sheppo 13451ae08745Sheppo /* 1346d10e4ef2Snarayan * Process a task specifying a client I/O request 13471ae08745Sheppo */ 13481ae08745Sheppo static int 1349d10e4ef2Snarayan vd_process_task(vd_task_t *task) 13501ae08745Sheppo { 1351d10e4ef2Snarayan int i, status; 1352d10e4ef2Snarayan vd_t *vd = task->vd; 1353d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 13541ae08745Sheppo 13551ae08745Sheppo 1356d10e4ef2Snarayan ASSERT(vd != NULL); 1357d10e4ef2Snarayan ASSERT(request != NULL); 13581ae08745Sheppo 1359d10e4ef2Snarayan /* Find the requested operation */ 13601ae08745Sheppo for (i = 0; i < vds_noperations; i++) 13611ae08745Sheppo if (request->operation == vds_operation[i].operation) 1362d10e4ef2Snarayan break; 1363d10e4ef2Snarayan if (i == vds_noperations) { 13643af08d82Slm66018 PR0("Unsupported operation %u", request->operation); 13651ae08745Sheppo return (ENOTSUP); 13661ae08745Sheppo } 13671ae08745Sheppo 13687636cb21Slm66018 /* Handle client using absolute disk offsets */ 13697636cb21Slm66018 if ((vd->vdisk_type == VD_DISK_TYPE_DISK) && 13707636cb21Slm66018 (request->slice == UINT8_MAX)) 13717636cb21Slm66018 request->slice = VD_ENTIRE_DISK_SLICE; 13727636cb21Slm66018 13737636cb21Slm66018 /* Range-check slice */ 13747636cb21Slm66018 if (request->slice >= vd->nslices) { 13753af08d82Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 13767636cb21Slm66018 request->slice, (vd->nslices - 1)); 13777636cb21Slm66018 return (EINVAL); 13787636cb21Slm66018 } 13797636cb21Slm66018 13803af08d82Slm66018 PR1("operation : %s", vds_operation[i].namep); 13813af08d82Slm66018 1382d10e4ef2Snarayan /* Start the operation */ 1383d10e4ef2Snarayan if ((status = vds_operation[i].start(task)) != EINPROGRESS) { 13843af08d82Slm66018 PR0("operation : %s returned status %d", 13853af08d82Slm66018 vds_operation[i].namep, status); 1386d10e4ef2Snarayan request->status = status; /* op succeeded or failed */ 1387d10e4ef2Snarayan return (0); /* but request completed */ 13881ae08745Sheppo } 13891ae08745Sheppo 1390d10e4ef2Snarayan ASSERT(vds_operation[i].complete != NULL); /* debug case */ 1391d10e4ef2Snarayan if (vds_operation[i].complete == NULL) { /* non-debug case */ 13923af08d82Slm66018 PR0("Unexpected return of EINPROGRESS " 1393d10e4ef2Snarayan "with no I/O completion handler"); 1394d10e4ef2Snarayan request->status = EIO; /* operation failed */ 1395d10e4ef2Snarayan return (0); /* but request completed */ 13961ae08745Sheppo } 13971ae08745Sheppo 13983af08d82Slm66018 PR1("operation : kick off taskq entry for %s", vds_operation[i].namep); 13993af08d82Slm66018 1400d10e4ef2Snarayan /* Queue a task to complete the operation */ 1401d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->completionq, vds_operation[i].complete, 1402d10e4ef2Snarayan task, DDI_SLEEP); 1403d10e4ef2Snarayan /* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */ 1404d10e4ef2Snarayan ASSERT(status == DDI_SUCCESS); 1405d10e4ef2Snarayan 1406d10e4ef2Snarayan PR1("Operation in progress"); 1407d10e4ef2Snarayan return (EINPROGRESS); /* completion handler will finish request */ 14081ae08745Sheppo } 14091ae08745Sheppo 14101ae08745Sheppo /* 14110a55fbb7Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first 14120a55fbb7Slm66018 * argument match the corresponding remaining arguments; otherwise, return false 14131ae08745Sheppo */ 14140a55fbb7Slm66018 boolean_t 14151ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 14161ae08745Sheppo { 14171ae08745Sheppo return ((tag->vio_msgtype == type) && 14181ae08745Sheppo (tag->vio_subtype == subtype) && 14190a55fbb7Slm66018 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 14201ae08745Sheppo } 14211ae08745Sheppo 14220a55fbb7Slm66018 /* 14230a55fbb7Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported 14240a55fbb7Slm66018 * by this server. 14250a55fbb7Slm66018 */ 14260a55fbb7Slm66018 static boolean_t 14270a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg) 14280a55fbb7Slm66018 { 14290a55fbb7Slm66018 for (int i = 0; i < vds_num_versions; i++) { 14300a55fbb7Slm66018 ASSERT(vds_version[i].major > 0); 14310a55fbb7Slm66018 ASSERT((i == 0) || 14320a55fbb7Slm66018 (vds_version[i].major < vds_version[i-1].major)); 14330a55fbb7Slm66018 14340a55fbb7Slm66018 /* 14350a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 14360a55fbb7Slm66018 * necessary, down to the highest value supported by this 14370a55fbb7Slm66018 * server and return true so this message will get "ack"ed; 14380a55fbb7Slm66018 * the client should also support all minor versions lower 14390a55fbb7Slm66018 * than the value it sent 14400a55fbb7Slm66018 */ 14410a55fbb7Slm66018 if (ver_msg->ver_major == vds_version[i].major) { 14420a55fbb7Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) { 14430a55fbb7Slm66018 PR0("Adjusting minor version from %u to %u", 14440a55fbb7Slm66018 ver_msg->ver_minor, vds_version[i].minor); 14450a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 14460a55fbb7Slm66018 } 14470a55fbb7Slm66018 return (B_TRUE); 14480a55fbb7Slm66018 } 14490a55fbb7Slm66018 14500a55fbb7Slm66018 /* 14510a55fbb7Slm66018 * If the message contains a higher major version number, set 14520a55fbb7Slm66018 * the message's major/minor versions to the current values 14530a55fbb7Slm66018 * and return false, so this message will get "nack"ed with 14540a55fbb7Slm66018 * these values, and the client will potentially try again 14550a55fbb7Slm66018 * with the same or a lower version 14560a55fbb7Slm66018 */ 14570a55fbb7Slm66018 if (ver_msg->ver_major > vds_version[i].major) { 14580a55fbb7Slm66018 ver_msg->ver_major = vds_version[i].major; 14590a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 14600a55fbb7Slm66018 return (B_FALSE); 14610a55fbb7Slm66018 } 14620a55fbb7Slm66018 14630a55fbb7Slm66018 /* 14640a55fbb7Slm66018 * Otherwise, the message's major version is less than the 14650a55fbb7Slm66018 * current major version, so continue the loop to the next 14660a55fbb7Slm66018 * (lower) supported version 14670a55fbb7Slm66018 */ 14680a55fbb7Slm66018 } 14690a55fbb7Slm66018 14700a55fbb7Slm66018 /* 14710a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 14720a55fbb7Slm66018 * message to terminate negotiation 14730a55fbb7Slm66018 */ 14740a55fbb7Slm66018 ver_msg->ver_major = 0; 14750a55fbb7Slm66018 ver_msg->ver_minor = 0; 14760a55fbb7Slm66018 return (B_FALSE); 14770a55fbb7Slm66018 } 14780a55fbb7Slm66018 14790a55fbb7Slm66018 /* 14800a55fbb7Slm66018 * Process a version message from a client. vds expects to receive version 14810a55fbb7Slm66018 * messages from clients seeking service, but never issues version messages 14820a55fbb7Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does 14830a55fbb7Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such 14840a55fbb7Slm66018 * messages as invalid). 14850a55fbb7Slm66018 */ 14861ae08745Sheppo static int 14870a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 14881ae08745Sheppo { 14891ae08745Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 14901ae08745Sheppo 14911ae08745Sheppo 14921ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 14931ae08745Sheppo 14941ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 14951ae08745Sheppo VIO_VER_INFO)) { 14961ae08745Sheppo return (ENOMSG); /* not a version message */ 14971ae08745Sheppo } 14981ae08745Sheppo 14991ae08745Sheppo if (msglen != sizeof (*ver_msg)) { 15003af08d82Slm66018 PR0("Expected %lu-byte version message; " 15011ae08745Sheppo "received %lu bytes", sizeof (*ver_msg), msglen); 15021ae08745Sheppo return (EBADMSG); 15031ae08745Sheppo } 15041ae08745Sheppo 15051ae08745Sheppo if (ver_msg->dev_class != VDEV_DISK) { 15063af08d82Slm66018 PR0("Expected device class %u (disk); received %u", 15071ae08745Sheppo VDEV_DISK, ver_msg->dev_class); 15081ae08745Sheppo return (EBADMSG); 15091ae08745Sheppo } 15101ae08745Sheppo 15110a55fbb7Slm66018 /* 15120a55fbb7Slm66018 * We're talking to the expected kind of client; set our device class 15130a55fbb7Slm66018 * for "ack/nack" back to the client 15140a55fbb7Slm66018 */ 15151ae08745Sheppo ver_msg->dev_class = VDEV_DISK_SERVER; 15160a55fbb7Slm66018 15170a55fbb7Slm66018 /* 15180a55fbb7Slm66018 * Check whether the (valid) version message specifies a version 15190a55fbb7Slm66018 * supported by this server. If the version is not supported, return 15200a55fbb7Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version() 15210a55fbb7Slm66018 * will have updated the message with a supported version for the 15220a55fbb7Slm66018 * client to consider 15230a55fbb7Slm66018 */ 15240a55fbb7Slm66018 if (!vds_supported_version(ver_msg)) 15250a55fbb7Slm66018 return (EBADMSG); 15260a55fbb7Slm66018 15270a55fbb7Slm66018 15280a55fbb7Slm66018 /* 15290a55fbb7Slm66018 * A version has been agreed upon; use the client's SID for 15300a55fbb7Slm66018 * communication on this channel now 15310a55fbb7Slm66018 */ 15320a55fbb7Slm66018 ASSERT(!(vd->initialized & VD_SID)); 15330a55fbb7Slm66018 vd->sid = ver_msg->tag.vio_sid; 15340a55fbb7Slm66018 vd->initialized |= VD_SID; 15350a55fbb7Slm66018 15360a55fbb7Slm66018 /* 15370a55fbb7Slm66018 * When multiple versions are supported, this function should store 15380a55fbb7Slm66018 * the negotiated major and minor version values in the "vd" data 15390a55fbb7Slm66018 * structure to govern further communication; in particular, note that 15400a55fbb7Slm66018 * the client might have specified a lower minor version for the 15410a55fbb7Slm66018 * agreed major version than specifed in the vds_version[] array. The 15420a55fbb7Slm66018 * following assertions should help remind future maintainers to make 15430a55fbb7Slm66018 * the appropriate changes to support multiple versions. 15440a55fbb7Slm66018 */ 15450a55fbb7Slm66018 ASSERT(vds_num_versions == 1); 15460a55fbb7Slm66018 ASSERT(ver_msg->ver_major == vds_version[0].major); 15470a55fbb7Slm66018 ASSERT(ver_msg->ver_minor == vds_version[0].minor); 15480a55fbb7Slm66018 15490a55fbb7Slm66018 PR0("Using major version %u, minor version %u", 15500a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 15511ae08745Sheppo return (0); 15521ae08745Sheppo } 15531ae08745Sheppo 15541ae08745Sheppo static int 15551ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 15561ae08745Sheppo { 15571ae08745Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 15583c96341aSnarayan int status, retry = 0; 15591ae08745Sheppo 15601ae08745Sheppo 15611ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 15621ae08745Sheppo 15631ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 15641ae08745Sheppo VIO_ATTR_INFO)) { 1565d10e4ef2Snarayan PR0("Message is not an attribute message"); 1566d10e4ef2Snarayan return (ENOMSG); 15671ae08745Sheppo } 15681ae08745Sheppo 15691ae08745Sheppo if (msglen != sizeof (*attr_msg)) { 15703af08d82Slm66018 PR0("Expected %lu-byte attribute message; " 15711ae08745Sheppo "received %lu bytes", sizeof (*attr_msg), msglen); 15721ae08745Sheppo return (EBADMSG); 15731ae08745Sheppo } 15741ae08745Sheppo 15751ae08745Sheppo if (attr_msg->max_xfer_sz == 0) { 15763af08d82Slm66018 PR0("Received maximum transfer size of 0 from client"); 15771ae08745Sheppo return (EBADMSG); 15781ae08745Sheppo } 15791ae08745Sheppo 15801ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 15811ae08745Sheppo (attr_msg->xfer_mode != VIO_DRING_MODE)) { 15823af08d82Slm66018 PR0("Client requested unsupported transfer mode"); 15831ae08745Sheppo return (EBADMSG); 15841ae08745Sheppo } 15851ae08745Sheppo 15863c96341aSnarayan /* 15873c96341aSnarayan * check if the underlying disk is ready, if not try accessing 15883c96341aSnarayan * the device again. Open the vdisk device and extract info 15893c96341aSnarayan * about it, as this is needed to respond to the attr info msg 15903c96341aSnarayan */ 15913c96341aSnarayan if ((vd->initialized & VD_DISK_READY) == 0) { 15923c96341aSnarayan PR0("Retry setting up disk (%s)", vd->device_path); 15933c96341aSnarayan do { 15943c96341aSnarayan status = vd_setup_vd(vd); 15953c96341aSnarayan if (status != EAGAIN || ++retry > vds_dev_retries) 15963c96341aSnarayan break; 15973c96341aSnarayan 15983c96341aSnarayan /* incremental delay */ 15993c96341aSnarayan delay(drv_usectohz(vds_dev_delay)); 16003c96341aSnarayan 16013c96341aSnarayan /* if vdisk is no longer enabled - return error */ 16023c96341aSnarayan if (!vd_enabled(vd)) 16033c96341aSnarayan return (ENXIO); 16043c96341aSnarayan 16053c96341aSnarayan } while (status == EAGAIN); 16063c96341aSnarayan 16073c96341aSnarayan if (status) 16083c96341aSnarayan return (ENXIO); 16093c96341aSnarayan 16103c96341aSnarayan vd->initialized |= VD_DISK_READY; 16113c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 16123c96341aSnarayan PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u", 16133c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 16143c96341aSnarayan (vd->pseudo ? "yes" : "no"), 16153c96341aSnarayan (vd->file ? "yes" : "no"), 16163c96341aSnarayan vd->nslices); 16173c96341aSnarayan } 16183c96341aSnarayan 16191ae08745Sheppo /* Success: valid message and transfer mode */ 16201ae08745Sheppo vd->xfer_mode = attr_msg->xfer_mode; 16213af08d82Slm66018 16221ae08745Sheppo if (vd->xfer_mode == VIO_DESC_MODE) { 16233af08d82Slm66018 16241ae08745Sheppo /* 16251ae08745Sheppo * The vd_dring_inband_msg_t contains one cookie; need room 16261ae08745Sheppo * for up to n-1 more cookies, where "n" is the number of full 16271ae08745Sheppo * pages plus possibly one partial page required to cover 16281ae08745Sheppo * "max_xfer_sz". Add room for one more cookie if 16291ae08745Sheppo * "max_xfer_sz" isn't an integral multiple of the page size. 16301ae08745Sheppo * Must first get the maximum transfer size in bytes. 16311ae08745Sheppo */ 16321ae08745Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 16331ae08745Sheppo attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 16341ae08745Sheppo attr_msg->max_xfer_sz; 16351ae08745Sheppo size_t max_inband_msglen = 16361ae08745Sheppo sizeof (vd_dring_inband_msg_t) + 16371ae08745Sheppo ((max_xfer_bytes/PAGESIZE + 16381ae08745Sheppo ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 16391ae08745Sheppo (sizeof (ldc_mem_cookie_t))); 16401ae08745Sheppo 16411ae08745Sheppo /* 16421ae08745Sheppo * Set the maximum expected message length to 16431ae08745Sheppo * accommodate in-band-descriptor messages with all 16441ae08745Sheppo * their cookies 16451ae08745Sheppo */ 16461ae08745Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 1647d10e4ef2Snarayan 1648d10e4ef2Snarayan /* 1649d10e4ef2Snarayan * Initialize the data structure for processing in-band I/O 1650d10e4ef2Snarayan * request descriptors 1651d10e4ef2Snarayan */ 1652d10e4ef2Snarayan vd->inband_task.vd = vd; 16533af08d82Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 1654d10e4ef2Snarayan vd->inband_task.index = 0; 1655d10e4ef2Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 16561ae08745Sheppo } 16571ae08745Sheppo 1658e1ebb9ecSlm66018 /* Return the device's block size and max transfer size to the client */ 1659e1ebb9ecSlm66018 attr_msg->vdisk_block_size = DEV_BSIZE; 1660e1ebb9ecSlm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz; 1661e1ebb9ecSlm66018 16621ae08745Sheppo attr_msg->vdisk_size = vd->vdisk_size; 16631ae08745Sheppo attr_msg->vdisk_type = vd->vdisk_type; 16641ae08745Sheppo attr_msg->operations = vds_operations; 16651ae08745Sheppo PR0("%s", VD_CLIENT(vd)); 16663af08d82Slm66018 16673af08d82Slm66018 ASSERT(vd->dring_task == NULL); 16683af08d82Slm66018 16691ae08745Sheppo return (0); 16701ae08745Sheppo } 16711ae08745Sheppo 16721ae08745Sheppo static int 16731ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 16741ae08745Sheppo { 16751ae08745Sheppo int status; 16761ae08745Sheppo size_t expected; 16771ae08745Sheppo ldc_mem_info_t dring_minfo; 16781ae08745Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 16791ae08745Sheppo 16801ae08745Sheppo 16811ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 16821ae08745Sheppo 16831ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 16841ae08745Sheppo VIO_DRING_REG)) { 1685d10e4ef2Snarayan PR0("Message is not a register-dring message"); 1686d10e4ef2Snarayan return (ENOMSG); 16871ae08745Sheppo } 16881ae08745Sheppo 16891ae08745Sheppo if (msglen < sizeof (*reg_msg)) { 16903af08d82Slm66018 PR0("Expected at least %lu-byte register-dring message; " 16911ae08745Sheppo "received %lu bytes", sizeof (*reg_msg), msglen); 16921ae08745Sheppo return (EBADMSG); 16931ae08745Sheppo } 16941ae08745Sheppo 16951ae08745Sheppo expected = sizeof (*reg_msg) + 16961ae08745Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 16971ae08745Sheppo if (msglen != expected) { 16983af08d82Slm66018 PR0("Expected %lu-byte register-dring message; " 16991ae08745Sheppo "received %lu bytes", expected, msglen); 17001ae08745Sheppo return (EBADMSG); 17011ae08745Sheppo } 17021ae08745Sheppo 17031ae08745Sheppo if (vd->initialized & VD_DRING) { 17043af08d82Slm66018 PR0("A dring was previously registered; only support one"); 17051ae08745Sheppo return (EBADMSG); 17061ae08745Sheppo } 17071ae08745Sheppo 1708d10e4ef2Snarayan if (reg_msg->num_descriptors > INT32_MAX) { 17093af08d82Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 1710d10e4ef2Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 1711d10e4ef2Snarayan return (EBADMSG); 1712d10e4ef2Snarayan } 1713d10e4ef2Snarayan 17141ae08745Sheppo if (reg_msg->ncookies != 1) { 17151ae08745Sheppo /* 17161ae08745Sheppo * In addition to fixing the assertion in the success case 17171ae08745Sheppo * below, supporting drings which require more than one 17181ae08745Sheppo * "cookie" requires increasing the value of vd->max_msglen 17191ae08745Sheppo * somewhere in the code path prior to receiving the message 17201ae08745Sheppo * which results in calling this function. Note that without 17211ae08745Sheppo * making this change, the larger message size required to 17221ae08745Sheppo * accommodate multiple cookies cannot be successfully 17231ae08745Sheppo * received, so this function will not even get called. 17241ae08745Sheppo * Gracefully accommodating more dring cookies might 17251ae08745Sheppo * reasonably demand exchanging an additional attribute or 17261ae08745Sheppo * making a minor protocol adjustment 17271ae08745Sheppo */ 17283af08d82Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 17291ae08745Sheppo return (EBADMSG); 17301ae08745Sheppo } 17311ae08745Sheppo 17321ae08745Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 17331ae08745Sheppo reg_msg->ncookies, reg_msg->num_descriptors, 17344bac2208Snarayan reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle); 17351ae08745Sheppo if (status != 0) { 17363af08d82Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status); 17371ae08745Sheppo return (status); 17381ae08745Sheppo } 17391ae08745Sheppo 17401ae08745Sheppo /* 17411ae08745Sheppo * To remove the need for this assertion, must call 17421ae08745Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 17431ae08745Sheppo * successful call to ldc_mem_dring_map() 17441ae08745Sheppo */ 17451ae08745Sheppo ASSERT(reg_msg->ncookies == 1); 17461ae08745Sheppo 17471ae08745Sheppo if ((status = 17481ae08745Sheppo ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 17493af08d82Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status); 17501ae08745Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 17513af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 17521ae08745Sheppo return (status); 17531ae08745Sheppo } 17541ae08745Sheppo 17551ae08745Sheppo if (dring_minfo.vaddr == NULL) { 17563af08d82Slm66018 PR0("Descriptor ring virtual address is NULL"); 17570a55fbb7Slm66018 return (ENXIO); 17581ae08745Sheppo } 17591ae08745Sheppo 17601ae08745Sheppo 1761d10e4ef2Snarayan /* Initialize for valid message and mapped dring */ 17621ae08745Sheppo PR1("descriptor size = %u, dring length = %u", 17631ae08745Sheppo vd->descriptor_size, vd->dring_len); 17641ae08745Sheppo vd->initialized |= VD_DRING; 17651ae08745Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */ 17661ae08745Sheppo vd->dring = dring_minfo.vaddr; 17671ae08745Sheppo vd->descriptor_size = reg_msg->descriptor_size; 17681ae08745Sheppo vd->dring_len = reg_msg->num_descriptors; 17691ae08745Sheppo reg_msg->dring_ident = vd->dring_ident; 1770d10e4ef2Snarayan 1771d10e4ef2Snarayan /* 1772d10e4ef2Snarayan * Allocate and initialize a "shadow" array of data structures for 1773d10e4ef2Snarayan * tasks to process I/O requests in dring elements 1774d10e4ef2Snarayan */ 1775d10e4ef2Snarayan vd->dring_task = 1776d10e4ef2Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 1777d10e4ef2Snarayan for (int i = 0; i < vd->dring_len; i++) { 1778d10e4ef2Snarayan vd->dring_task[i].vd = vd; 1779d10e4ef2Snarayan vd->dring_task[i].index = i; 1780d10e4ef2Snarayan vd->dring_task[i].request = &VD_DRING_ELEM(i)->payload; 17814bac2208Snarayan 17824bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, 17834bac2208Snarayan &(vd->dring_task[i].mhdl)); 17844bac2208Snarayan if (status) { 17853af08d82Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status); 17864bac2208Snarayan return (ENXIO); 17874bac2208Snarayan } 17883af08d82Slm66018 17893af08d82Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 1790d10e4ef2Snarayan } 1791d10e4ef2Snarayan 17921ae08745Sheppo return (0); 17931ae08745Sheppo } 17941ae08745Sheppo 17951ae08745Sheppo static int 17961ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 17971ae08745Sheppo { 17981ae08745Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 17991ae08745Sheppo 18001ae08745Sheppo 18011ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 18021ae08745Sheppo 18031ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 18041ae08745Sheppo VIO_DRING_UNREG)) { 1805d10e4ef2Snarayan PR0("Message is not an unregister-dring message"); 1806d10e4ef2Snarayan return (ENOMSG); 18071ae08745Sheppo } 18081ae08745Sheppo 18091ae08745Sheppo if (msglen != sizeof (*unreg_msg)) { 18103af08d82Slm66018 PR0("Expected %lu-byte unregister-dring message; " 18111ae08745Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen); 18121ae08745Sheppo return (EBADMSG); 18131ae08745Sheppo } 18141ae08745Sheppo 18151ae08745Sheppo if (unreg_msg->dring_ident != vd->dring_ident) { 18163af08d82Slm66018 PR0("Expected dring ident %lu; received %lu", 18171ae08745Sheppo vd->dring_ident, unreg_msg->dring_ident); 18181ae08745Sheppo return (EBADMSG); 18191ae08745Sheppo } 18201ae08745Sheppo 18211ae08745Sheppo return (0); 18221ae08745Sheppo } 18231ae08745Sheppo 18241ae08745Sheppo static int 18251ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen) 18261ae08745Sheppo { 18271ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 18281ae08745Sheppo 1829d10e4ef2Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 1830d10e4ef2Snarayan PR0("Message is not an RDX message"); 1831d10e4ef2Snarayan return (ENOMSG); 1832d10e4ef2Snarayan } 18331ae08745Sheppo 18341ae08745Sheppo if (msglen != sizeof (vio_rdx_msg_t)) { 18353af08d82Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes", 18361ae08745Sheppo sizeof (vio_rdx_msg_t), msglen); 18371ae08745Sheppo return (EBADMSG); 18381ae08745Sheppo } 18391ae08745Sheppo 1840d10e4ef2Snarayan PR0("Valid RDX message"); 18411ae08745Sheppo return (0); 18421ae08745Sheppo } 18431ae08745Sheppo 18441ae08745Sheppo static int 18451ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num) 18461ae08745Sheppo { 18471ae08745Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 18483af08d82Slm66018 PR0("Received seq_num %lu; expected %lu", 18491ae08745Sheppo seq_num, (vd->seq_num + 1)); 18503af08d82Slm66018 PR0("initiating soft reset"); 1851d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 18521ae08745Sheppo return (1); 18531ae08745Sheppo } 18541ae08745Sheppo 18551ae08745Sheppo vd->seq_num = seq_num; 18561ae08745Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 18571ae08745Sheppo return (0); 18581ae08745Sheppo } 18591ae08745Sheppo 18601ae08745Sheppo /* 18611ae08745Sheppo * Return the expected size of an inband-descriptor message with all the 18621ae08745Sheppo * cookies it claims to include 18631ae08745Sheppo */ 18641ae08745Sheppo static size_t 18651ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg) 18661ae08745Sheppo { 18671ae08745Sheppo return ((sizeof (*msg)) + 18681ae08745Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 18691ae08745Sheppo } 18701ae08745Sheppo 18711ae08745Sheppo /* 18721ae08745Sheppo * Process an in-band descriptor message: used with clients like OBP, with 18731ae08745Sheppo * which vds exchanges descriptors within VIO message payloads, rather than 18741ae08745Sheppo * operating on them within a descriptor ring 18751ae08745Sheppo */ 18761ae08745Sheppo static int 18773af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 18781ae08745Sheppo { 18791ae08745Sheppo size_t expected; 18801ae08745Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 18811ae08745Sheppo 18821ae08745Sheppo 18831ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 18841ae08745Sheppo 18851ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 1886d10e4ef2Snarayan VIO_DESC_DATA)) { 1887d10e4ef2Snarayan PR1("Message is not an in-band-descriptor message"); 1888d10e4ef2Snarayan return (ENOMSG); 1889d10e4ef2Snarayan } 18901ae08745Sheppo 18911ae08745Sheppo if (msglen < sizeof (*desc_msg)) { 18923af08d82Slm66018 PR0("Expected at least %lu-byte descriptor message; " 18931ae08745Sheppo "received %lu bytes", sizeof (*desc_msg), msglen); 18941ae08745Sheppo return (EBADMSG); 18951ae08745Sheppo } 18961ae08745Sheppo 18971ae08745Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) { 18983af08d82Slm66018 PR0("Expected %lu-byte descriptor message; " 18991ae08745Sheppo "received %lu bytes", expected, msglen); 19001ae08745Sheppo return (EBADMSG); 19011ae08745Sheppo } 19021ae08745Sheppo 1903d10e4ef2Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 19041ae08745Sheppo return (EBADMSG); 19051ae08745Sheppo 1906d10e4ef2Snarayan /* 1907d10e4ef2Snarayan * Valid message: Set up the in-band descriptor task and process the 1908d10e4ef2Snarayan * request. Arrange to acknowledge the client's message, unless an 1909d10e4ef2Snarayan * error processing the descriptor task results in setting 1910d10e4ef2Snarayan * VIO_SUBTYPE_NACK 1911d10e4ef2Snarayan */ 1912d10e4ef2Snarayan PR1("Valid in-band-descriptor message"); 1913d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 19143af08d82Slm66018 19153af08d82Slm66018 ASSERT(vd->inband_task.msg != NULL); 19163af08d82Slm66018 19173af08d82Slm66018 bcopy(msg, vd->inband_task.msg, msglen); 1918d10e4ef2Snarayan vd->inband_task.msglen = msglen; 19193af08d82Slm66018 19203af08d82Slm66018 /* 19213af08d82Slm66018 * The task request is now the payload of the message 19223af08d82Slm66018 * that was just copied into the body of the task. 19233af08d82Slm66018 */ 19243af08d82Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 1925d10e4ef2Snarayan vd->inband_task.request = &desc_msg->payload; 19263af08d82Slm66018 1927d10e4ef2Snarayan return (vd_process_task(&vd->inband_task)); 19281ae08745Sheppo } 19291ae08745Sheppo 19301ae08745Sheppo static int 1931d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 19323af08d82Slm66018 vio_msg_t *msg, size_t msglen) 19331ae08745Sheppo { 19341ae08745Sheppo int status; 1935d10e4ef2Snarayan boolean_t ready; 1936d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 19371ae08745Sheppo 19381ae08745Sheppo 1939d10e4ef2Snarayan /* Accept the updated dring element */ 1940d10e4ef2Snarayan if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 19413af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", status); 19421ae08745Sheppo return (status); 19431ae08745Sheppo } 1944d10e4ef2Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY); 1945d10e4ef2Snarayan if (ready) { 1946d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED; 1947d10e4ef2Snarayan } else { 19483af08d82Slm66018 PR0("descriptor %u not ready", idx); 1949d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 1950d10e4ef2Snarayan } 1951d10e4ef2Snarayan if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 19523af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", status); 19531ae08745Sheppo return (status); 19541ae08745Sheppo } 1955d10e4ef2Snarayan if (!ready) 1956d10e4ef2Snarayan return (EBUSY); 19571ae08745Sheppo 19581ae08745Sheppo 1959d10e4ef2Snarayan /* Initialize a task and process the accepted element */ 1960d10e4ef2Snarayan PR1("Processing dring element %u", idx); 1961d10e4ef2Snarayan vd->dring_task[idx].type = type; 19623af08d82Slm66018 19633af08d82Slm66018 /* duplicate msg buf for cookies etc. */ 19643af08d82Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen); 19653af08d82Slm66018 1966d10e4ef2Snarayan vd->dring_task[idx].msglen = msglen; 1967d10e4ef2Snarayan if ((status = vd_process_task(&vd->dring_task[idx])) != EINPROGRESS) 19683c96341aSnarayan status = vd_mark_elem_done(vd, idx, 19693c96341aSnarayan vd->dring_task[idx].request->status, 19703c96341aSnarayan vd->dring_task[idx].request->nbytes); 19711ae08745Sheppo 19721ae08745Sheppo return (status); 19731ae08745Sheppo } 19741ae08745Sheppo 19751ae08745Sheppo static int 1976d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end, 19773af08d82Slm66018 vio_msg_t *msg, size_t msglen) 1978d10e4ef2Snarayan { 1979d10e4ef2Snarayan int i, n, nelem, status = 0; 1980d10e4ef2Snarayan boolean_t inprogress = B_FALSE; 1981d10e4ef2Snarayan vd_task_type_t type; 1982d10e4ef2Snarayan 1983d10e4ef2Snarayan 1984d10e4ef2Snarayan ASSERT(start >= 0); 1985d10e4ef2Snarayan ASSERT(end >= 0); 1986d10e4ef2Snarayan 1987d10e4ef2Snarayan /* 1988d10e4ef2Snarayan * Arrange to acknowledge the client's message, unless an error 1989d10e4ef2Snarayan * processing one of the dring elements results in setting 1990d10e4ef2Snarayan * VIO_SUBTYPE_NACK 1991d10e4ef2Snarayan */ 1992d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 1993d10e4ef2Snarayan 1994d10e4ef2Snarayan /* 1995d10e4ef2Snarayan * Process the dring elements in the range 1996d10e4ef2Snarayan */ 1997d10e4ef2Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 1998d10e4ef2Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 1999d10e4ef2Snarayan ((vio_dring_msg_t *)msg)->end_idx = i; 2000d10e4ef2Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 20013af08d82Slm66018 status = vd_process_element(vd, type, i, msg, msglen); 2002d10e4ef2Snarayan if (status == EINPROGRESS) 2003d10e4ef2Snarayan inprogress = B_TRUE; 2004d10e4ef2Snarayan else if (status != 0) 2005d10e4ef2Snarayan break; 2006d10e4ef2Snarayan } 2007d10e4ef2Snarayan 2008d10e4ef2Snarayan /* 2009d10e4ef2Snarayan * If some, but not all, operations of a multi-element range are in 2010d10e4ef2Snarayan * progress, wait for other operations to complete before returning 2011d10e4ef2Snarayan * (which will result in "ack" or "nack" of the message). Note that 2012d10e4ef2Snarayan * all outstanding operations will need to complete, not just the ones 2013d10e4ef2Snarayan * corresponding to the current range of dring elements; howevever, as 2014d10e4ef2Snarayan * this situation is an error case, performance is less critical. 2015d10e4ef2Snarayan */ 2016d10e4ef2Snarayan if ((nelem > 1) && (status != EINPROGRESS) && inprogress) 2017d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 2018d10e4ef2Snarayan 2019d10e4ef2Snarayan return (status); 2020d10e4ef2Snarayan } 2021d10e4ef2Snarayan 2022d10e4ef2Snarayan static int 20233af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 20241ae08745Sheppo { 20251ae08745Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 20261ae08745Sheppo 20271ae08745Sheppo 20281ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 20291ae08745Sheppo 20301ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 20311ae08745Sheppo VIO_DRING_DATA)) { 2032d10e4ef2Snarayan PR1("Message is not a dring-data message"); 2033d10e4ef2Snarayan return (ENOMSG); 20341ae08745Sheppo } 20351ae08745Sheppo 20361ae08745Sheppo if (msglen != sizeof (*dring_msg)) { 20373af08d82Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes", 20381ae08745Sheppo sizeof (*dring_msg), msglen); 20391ae08745Sheppo return (EBADMSG); 20401ae08745Sheppo } 20411ae08745Sheppo 2042d10e4ef2Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 20431ae08745Sheppo return (EBADMSG); 20441ae08745Sheppo 20451ae08745Sheppo if (dring_msg->dring_ident != vd->dring_ident) { 20463af08d82Slm66018 PR0("Expected dring ident %lu; received ident %lu", 20471ae08745Sheppo vd->dring_ident, dring_msg->dring_ident); 20481ae08745Sheppo return (EBADMSG); 20491ae08745Sheppo } 20501ae08745Sheppo 2051d10e4ef2Snarayan if (dring_msg->start_idx >= vd->dring_len) { 20523af08d82Slm66018 PR0("\"start_idx\" = %u; must be less than %u", 2053d10e4ef2Snarayan dring_msg->start_idx, vd->dring_len); 2054d10e4ef2Snarayan return (EBADMSG); 2055d10e4ef2Snarayan } 20561ae08745Sheppo 2057d10e4ef2Snarayan if ((dring_msg->end_idx < 0) || 2058d10e4ef2Snarayan (dring_msg->end_idx >= vd->dring_len)) { 20593af08d82Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 2060d10e4ef2Snarayan dring_msg->end_idx, vd->dring_len); 2061d10e4ef2Snarayan return (EBADMSG); 2062d10e4ef2Snarayan } 2063d10e4ef2Snarayan 2064d10e4ef2Snarayan /* Valid message; process range of updated dring elements */ 2065d10e4ef2Snarayan PR1("Processing descriptor range, start = %u, end = %u", 2066d10e4ef2Snarayan dring_msg->start_idx, dring_msg->end_idx); 2067d10e4ef2Snarayan return (vd_process_element_range(vd, dring_msg->start_idx, 20683af08d82Slm66018 dring_msg->end_idx, msg, msglen)); 20691ae08745Sheppo } 20701ae08745Sheppo 20711ae08745Sheppo static int 20721ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 20731ae08745Sheppo { 20741ae08745Sheppo int retry, status; 20751ae08745Sheppo size_t size = *nbytes; 20761ae08745Sheppo 20771ae08745Sheppo 20781ae08745Sheppo for (retry = 0, status = ETIMEDOUT; 20791ae08745Sheppo retry < vds_ldc_retries && status == ETIMEDOUT; 20801ae08745Sheppo retry++) { 20811ae08745Sheppo PR1("ldc_read() attempt %d", (retry + 1)); 20821ae08745Sheppo *nbytes = size; 20831ae08745Sheppo status = ldc_read(ldc_handle, msg, nbytes); 20841ae08745Sheppo } 20851ae08745Sheppo 20863af08d82Slm66018 if (status) { 20873af08d82Slm66018 PR0("ldc_read() returned errno %d", status); 20883af08d82Slm66018 if (status != ECONNRESET) 20893af08d82Slm66018 return (ENOMSG); 20901ae08745Sheppo return (status); 20911ae08745Sheppo } else if (*nbytes == 0) { 20921ae08745Sheppo PR1("ldc_read() returned 0 and no message read"); 20931ae08745Sheppo return (ENOMSG); 20941ae08745Sheppo } 20951ae08745Sheppo 20961ae08745Sheppo PR1("RCVD %lu-byte message", *nbytes); 20971ae08745Sheppo return (0); 20981ae08745Sheppo } 20991ae08745Sheppo 21001ae08745Sheppo static int 21013af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 21021ae08745Sheppo { 21031ae08745Sheppo int status; 21041ae08745Sheppo 21051ae08745Sheppo 21061ae08745Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 21071ae08745Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env); 21083af08d82Slm66018 #ifdef DEBUG 21093af08d82Slm66018 vd_decode_tag(msg); 21103af08d82Slm66018 #endif 21111ae08745Sheppo 21121ae08745Sheppo /* 21131ae08745Sheppo * Validate session ID up front, since it applies to all messages 21141ae08745Sheppo * once set 21151ae08745Sheppo */ 21161ae08745Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 21173af08d82Slm66018 PR0("Expected SID %u, received %u", vd->sid, 21181ae08745Sheppo msg->tag.vio_sid); 21191ae08745Sheppo return (EBADMSG); 21201ae08745Sheppo } 21211ae08745Sheppo 21223af08d82Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 21231ae08745Sheppo 21241ae08745Sheppo /* 21251ae08745Sheppo * Process the received message based on connection state 21261ae08745Sheppo */ 21271ae08745Sheppo switch (vd->state) { 21281ae08745Sheppo case VD_STATE_INIT: /* expect version message */ 21290a55fbb7Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 21301ae08745Sheppo return (status); 21311ae08745Sheppo 21321ae08745Sheppo /* Version negotiated, move to that state */ 21331ae08745Sheppo vd->state = VD_STATE_VER; 21341ae08745Sheppo return (0); 21351ae08745Sheppo 21361ae08745Sheppo case VD_STATE_VER: /* expect attribute message */ 21371ae08745Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 21381ae08745Sheppo return (status); 21391ae08745Sheppo 21401ae08745Sheppo /* Attributes exchanged, move to that state */ 21411ae08745Sheppo vd->state = VD_STATE_ATTR; 21421ae08745Sheppo return (0); 21431ae08745Sheppo 21441ae08745Sheppo case VD_STATE_ATTR: 21451ae08745Sheppo switch (vd->xfer_mode) { 21461ae08745Sheppo case VIO_DESC_MODE: /* expect RDX message */ 21471ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0) 21481ae08745Sheppo return (status); 21491ae08745Sheppo 21501ae08745Sheppo /* Ready to receive in-band descriptors */ 21511ae08745Sheppo vd->state = VD_STATE_DATA; 21521ae08745Sheppo return (0); 21531ae08745Sheppo 21541ae08745Sheppo case VIO_DRING_MODE: /* expect register-dring message */ 21551ae08745Sheppo if ((status = 21561ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 21571ae08745Sheppo return (status); 21581ae08745Sheppo 21591ae08745Sheppo /* One dring negotiated, move to that state */ 21601ae08745Sheppo vd->state = VD_STATE_DRING; 21611ae08745Sheppo return (0); 21621ae08745Sheppo 21631ae08745Sheppo default: 21641ae08745Sheppo ASSERT("Unsupported transfer mode"); 21653af08d82Slm66018 PR0("Unsupported transfer mode"); 21661ae08745Sheppo return (ENOTSUP); 21671ae08745Sheppo } 21681ae08745Sheppo 21691ae08745Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 21701ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) { 21711ae08745Sheppo /* Ready to receive data */ 21721ae08745Sheppo vd->state = VD_STATE_DATA; 21731ae08745Sheppo return (0); 21741ae08745Sheppo } else if (status != ENOMSG) { 21751ae08745Sheppo return (status); 21761ae08745Sheppo } 21771ae08745Sheppo 21781ae08745Sheppo 21791ae08745Sheppo /* 21801ae08745Sheppo * If another register-dring message is received, stay in 21811ae08745Sheppo * dring state in case the client sends RDX; although the 21821ae08745Sheppo * protocol allows multiple drings, this server does not 21831ae08745Sheppo * support using more than one 21841ae08745Sheppo */ 21851ae08745Sheppo if ((status = 21861ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 21871ae08745Sheppo return (status); 21881ae08745Sheppo 21891ae08745Sheppo /* 21901ae08745Sheppo * Acknowledge an unregister-dring message, but reset the 21911ae08745Sheppo * connection anyway: Although the protocol allows 21921ae08745Sheppo * unregistering drings, this server cannot serve a vdisk 21931ae08745Sheppo * without its only dring 21941ae08745Sheppo */ 21951ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 21961ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 21971ae08745Sheppo 21981ae08745Sheppo case VD_STATE_DATA: 21991ae08745Sheppo switch (vd->xfer_mode) { 22001ae08745Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */ 22013af08d82Slm66018 return (vd_process_desc_msg(vd, msg, msglen)); 22021ae08745Sheppo 22031ae08745Sheppo case VIO_DRING_MODE: /* expect dring-data or unreg-dring */ 22041ae08745Sheppo /* 22051ae08745Sheppo * Typically expect dring-data messages, so handle 22061ae08745Sheppo * them first 22071ae08745Sheppo */ 22081ae08745Sheppo if ((status = vd_process_dring_msg(vd, msg, 22093af08d82Slm66018 msglen)) != ENOMSG) 22101ae08745Sheppo return (status); 22111ae08745Sheppo 22121ae08745Sheppo /* 22131ae08745Sheppo * Acknowledge an unregister-dring message, but reset 22141ae08745Sheppo * the connection anyway: Although the protocol 22151ae08745Sheppo * allows unregistering drings, this server cannot 22161ae08745Sheppo * serve a vdisk without its only dring 22171ae08745Sheppo */ 22181ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 22191ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 22201ae08745Sheppo 22211ae08745Sheppo default: 22221ae08745Sheppo ASSERT("Unsupported transfer mode"); 22233af08d82Slm66018 PR0("Unsupported transfer mode"); 22241ae08745Sheppo return (ENOTSUP); 22251ae08745Sheppo } 22261ae08745Sheppo 22271ae08745Sheppo default: 22281ae08745Sheppo ASSERT("Invalid client connection state"); 22293af08d82Slm66018 PR0("Invalid client connection state"); 22301ae08745Sheppo return (ENOTSUP); 22311ae08745Sheppo } 22321ae08745Sheppo } 22331ae08745Sheppo 2234d10e4ef2Snarayan static int 22353af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 22361ae08745Sheppo { 22371ae08745Sheppo int status; 22381ae08745Sheppo boolean_t reset_ldc = B_FALSE; 22391ae08745Sheppo 22401ae08745Sheppo 22411ae08745Sheppo /* 22421ae08745Sheppo * Check that the message is at least big enough for a "tag", so that 22431ae08745Sheppo * message processing can proceed based on tag-specified message type 22441ae08745Sheppo */ 22451ae08745Sheppo if (msglen < sizeof (vio_msg_tag_t)) { 22463af08d82Slm66018 PR0("Received short (%lu-byte) message", msglen); 22471ae08745Sheppo /* Can't "nack" short message, so drop the big hammer */ 22483af08d82Slm66018 PR0("initiating full reset"); 2249d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 2250d10e4ef2Snarayan return (EBADMSG); 22511ae08745Sheppo } 22521ae08745Sheppo 22531ae08745Sheppo /* 22541ae08745Sheppo * Process the message 22551ae08745Sheppo */ 22563af08d82Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) { 22571ae08745Sheppo case 0: 22581ae08745Sheppo /* "ack" valid, successfully-processed messages */ 22591ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 22601ae08745Sheppo break; 22611ae08745Sheppo 2262d10e4ef2Snarayan case EINPROGRESS: 2263d10e4ef2Snarayan /* The completion handler will "ack" or "nack" the message */ 2264d10e4ef2Snarayan return (EINPROGRESS); 22651ae08745Sheppo case ENOMSG: 22663af08d82Slm66018 PR0("Received unexpected message"); 22671ae08745Sheppo _NOTE(FALLTHROUGH); 22681ae08745Sheppo case EBADMSG: 22691ae08745Sheppo case ENOTSUP: 22701ae08745Sheppo /* "nack" invalid messages */ 22711ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 22721ae08745Sheppo break; 22731ae08745Sheppo 22741ae08745Sheppo default: 22751ae08745Sheppo /* "nack" failed messages */ 22761ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 22771ae08745Sheppo /* An LDC error probably occurred, so try resetting it */ 22781ae08745Sheppo reset_ldc = B_TRUE; 22791ae08745Sheppo break; 22801ae08745Sheppo } 22811ae08745Sheppo 22823af08d82Slm66018 PR1("\tResulting in state %d (%s)", vd->state, 22833af08d82Slm66018 vd_decode_state(vd->state)); 22843af08d82Slm66018 2285d10e4ef2Snarayan /* Send the "ack" or "nack" to the client */ 22861ae08745Sheppo PR1("Sending %s", 22871ae08745Sheppo (msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 22881ae08745Sheppo if (send_msg(vd->ldc_handle, msg, msglen) != 0) 22891ae08745Sheppo reset_ldc = B_TRUE; 22901ae08745Sheppo 2291d10e4ef2Snarayan /* Arrange to reset the connection for nack'ed or failed messages */ 22923af08d82Slm66018 if ((status != 0) || reset_ldc) { 22933af08d82Slm66018 PR0("initiating %s reset", 22943af08d82Slm66018 (reset_ldc) ? "full" : "soft"); 2295d10e4ef2Snarayan vd_need_reset(vd, reset_ldc); 22963af08d82Slm66018 } 2297d10e4ef2Snarayan 2298d10e4ef2Snarayan return (status); 2299d10e4ef2Snarayan } 2300d10e4ef2Snarayan 2301d10e4ef2Snarayan static boolean_t 2302d10e4ef2Snarayan vd_enabled(vd_t *vd) 2303d10e4ef2Snarayan { 2304d10e4ef2Snarayan boolean_t enabled; 2305d10e4ef2Snarayan 2306d10e4ef2Snarayan 2307d10e4ef2Snarayan mutex_enter(&vd->lock); 2308d10e4ef2Snarayan enabled = vd->enabled; 2309d10e4ef2Snarayan mutex_exit(&vd->lock); 2310d10e4ef2Snarayan return (enabled); 23111ae08745Sheppo } 23121ae08745Sheppo 23131ae08745Sheppo static void 23140a55fbb7Slm66018 vd_recv_msg(void *arg) 23151ae08745Sheppo { 23161ae08745Sheppo vd_t *vd = (vd_t *)arg; 23173af08d82Slm66018 int rv = 0, status = 0; 23181ae08745Sheppo 23191ae08745Sheppo ASSERT(vd != NULL); 23203af08d82Slm66018 2321d10e4ef2Snarayan PR2("New task to receive incoming message(s)"); 23223af08d82Slm66018 23233af08d82Slm66018 2324d10e4ef2Snarayan while (vd_enabled(vd) && status == 0) { 2325d10e4ef2Snarayan size_t msglen, msgsize; 23263af08d82Slm66018 ldc_status_t lstatus; 2327d10e4ef2Snarayan 23280a55fbb7Slm66018 /* 2329d10e4ef2Snarayan * Receive and process a message 23300a55fbb7Slm66018 */ 2331d10e4ef2Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */ 23323af08d82Slm66018 23333af08d82Slm66018 /* 23343af08d82Slm66018 * check if channel is UP - else break out of loop 23353af08d82Slm66018 */ 23363af08d82Slm66018 status = ldc_status(vd->ldc_handle, &lstatus); 23373af08d82Slm66018 if (lstatus != LDC_UP) { 23383af08d82Slm66018 PR0("channel not up (status=%d), exiting recv loop\n", 23393af08d82Slm66018 lstatus); 23403af08d82Slm66018 break; 23413af08d82Slm66018 } 23423af08d82Slm66018 23433af08d82Slm66018 ASSERT(vd->max_msglen != 0); 23443af08d82Slm66018 2345d10e4ef2Snarayan msgsize = vd->max_msglen; /* stable copy for alloc/free */ 23463af08d82Slm66018 msglen = msgsize; /* actual len after recv_msg() */ 23473af08d82Slm66018 23483af08d82Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 23493af08d82Slm66018 switch (status) { 23503af08d82Slm66018 case 0: 23513af08d82Slm66018 rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp, 23523af08d82Slm66018 msglen); 23533af08d82Slm66018 /* check if max_msglen changed */ 23543af08d82Slm66018 if (msgsize != vd->max_msglen) { 23553af08d82Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 23563af08d82Slm66018 msgsize, vd->max_msglen); 23573af08d82Slm66018 kmem_free(vd->vio_msgp, msgsize); 23583af08d82Slm66018 vd->vio_msgp = 23593af08d82Slm66018 kmem_alloc(vd->max_msglen, KM_SLEEP); 23603af08d82Slm66018 } 23613af08d82Slm66018 if (rv == EINPROGRESS) 23623af08d82Slm66018 continue; 23633af08d82Slm66018 break; 23643af08d82Slm66018 23653af08d82Slm66018 case ENOMSG: 23663af08d82Slm66018 break; 23673af08d82Slm66018 23683af08d82Slm66018 case ECONNRESET: 23693af08d82Slm66018 PR0("initiating soft reset (ECONNRESET)\n"); 23703af08d82Slm66018 vd_need_reset(vd, B_FALSE); 23713af08d82Slm66018 status = 0; 23723af08d82Slm66018 break; 23733af08d82Slm66018 23743af08d82Slm66018 default: 2375d10e4ef2Snarayan /* Probably an LDC failure; arrange to reset it */ 23763af08d82Slm66018 PR0("initiating full reset (status=0x%x)", status); 2377d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 23783af08d82Slm66018 break; 23790a55fbb7Slm66018 } 23801ae08745Sheppo } 23813af08d82Slm66018 2382d10e4ef2Snarayan PR2("Task finished"); 23830a55fbb7Slm66018 } 23840a55fbb7Slm66018 23850a55fbb7Slm66018 static uint_t 23861ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg) 23871ae08745Sheppo { 23881ae08745Sheppo vd_t *vd = (vd_t *)(void *)arg; 23893af08d82Slm66018 int status; 23901ae08745Sheppo 23911ae08745Sheppo ASSERT(vd != NULL); 2392d10e4ef2Snarayan 2393d10e4ef2Snarayan if (!vd_enabled(vd)) 2394d10e4ef2Snarayan return (LDC_SUCCESS); 2395d10e4ef2Snarayan 23963af08d82Slm66018 if (event & LDC_EVT_DOWN) { 239734683adeSsg70180 PR0("LDC_EVT_DOWN: LDC channel went down"); 23983af08d82Slm66018 23993af08d82Slm66018 vd_need_reset(vd, B_TRUE); 24003af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 24013af08d82Slm66018 DDI_SLEEP); 24023af08d82Slm66018 if (status == DDI_FAILURE) { 24033af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 24043af08d82Slm66018 vd_need_reset(vd, B_TRUE); 24053af08d82Slm66018 } 24063af08d82Slm66018 } 24073af08d82Slm66018 2408d10e4ef2Snarayan if (event & LDC_EVT_RESET) { 24093af08d82Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset"); 24103af08d82Slm66018 24113af08d82Slm66018 if (vd->state != VD_STATE_INIT) { 24123af08d82Slm66018 PR0("scheduling full reset"); 24133af08d82Slm66018 vd_need_reset(vd, B_FALSE); 24143af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 24153af08d82Slm66018 vd, DDI_SLEEP); 24163af08d82Slm66018 if (status == DDI_FAILURE) { 24173af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 24183af08d82Slm66018 vd_need_reset(vd, B_TRUE); 24193af08d82Slm66018 } 24203af08d82Slm66018 24213af08d82Slm66018 } else { 24223af08d82Slm66018 PR0("channel already reset, ignoring...\n"); 24233af08d82Slm66018 PR0("doing ldc up...\n"); 24243af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 24253af08d82Slm66018 } 24263af08d82Slm66018 2427d10e4ef2Snarayan return (LDC_SUCCESS); 2428d10e4ef2Snarayan } 2429d10e4ef2Snarayan 2430d10e4ef2Snarayan if (event & LDC_EVT_UP) { 24313af08d82Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state"); 24323af08d82Slm66018 PR0("initiating soft reset"); 2433d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 24343af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 24353af08d82Slm66018 vd, DDI_SLEEP); 24363af08d82Slm66018 if (status == DDI_FAILURE) { 24373af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 24383af08d82Slm66018 vd_need_reset(vd, B_TRUE); 24393af08d82Slm66018 return (LDC_SUCCESS); 24403af08d82Slm66018 } 2441d10e4ef2Snarayan } 2442d10e4ef2Snarayan 2443d10e4ef2Snarayan if (event & LDC_EVT_READ) { 2444d10e4ef2Snarayan int status; 2445d10e4ef2Snarayan 2446d10e4ef2Snarayan PR1("New data available"); 2447d10e4ef2Snarayan /* Queue a task to receive the new data */ 2448d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 2449d10e4ef2Snarayan DDI_SLEEP); 24503af08d82Slm66018 24513af08d82Slm66018 if (status == DDI_FAILURE) { 24523af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 24533af08d82Slm66018 vd_need_reset(vd, B_TRUE); 24543af08d82Slm66018 } 2455d10e4ef2Snarayan } 2456d10e4ef2Snarayan 2457d10e4ef2Snarayan return (LDC_SUCCESS); 24581ae08745Sheppo } 24591ae08745Sheppo 24601ae08745Sheppo static uint_t 24611ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 24621ae08745Sheppo { 24631ae08745Sheppo _NOTE(ARGUNUSED(key, val)) 24641ae08745Sheppo (*((uint_t *)arg))++; 24651ae08745Sheppo return (MH_WALK_TERMINATE); 24661ae08745Sheppo } 24671ae08745Sheppo 24681ae08745Sheppo 24691ae08745Sheppo static int 24701ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 24711ae08745Sheppo { 24721ae08745Sheppo uint_t vd_present = 0; 24731ae08745Sheppo minor_t instance; 24741ae08745Sheppo vds_t *vds; 24751ae08745Sheppo 24761ae08745Sheppo 24771ae08745Sheppo switch (cmd) { 24781ae08745Sheppo case DDI_DETACH: 24791ae08745Sheppo /* the real work happens below */ 24801ae08745Sheppo break; 24811ae08745Sheppo case DDI_SUSPEND: 2482d10e4ef2Snarayan PR0("No action required for DDI_SUSPEND"); 24831ae08745Sheppo return (DDI_SUCCESS); 24841ae08745Sheppo default: 24853af08d82Slm66018 PR0("Unrecognized \"cmd\""); 24861ae08745Sheppo return (DDI_FAILURE); 24871ae08745Sheppo } 24881ae08745Sheppo 24891ae08745Sheppo ASSERT(cmd == DDI_DETACH); 24901ae08745Sheppo instance = ddi_get_instance(dip); 24911ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 24923af08d82Slm66018 PR0("Could not get state for instance %u", instance); 24931ae08745Sheppo ddi_soft_state_free(vds_state, instance); 24941ae08745Sheppo return (DDI_FAILURE); 24951ae08745Sheppo } 24961ae08745Sheppo 24971ae08745Sheppo /* Do no detach when serving any vdisks */ 24981ae08745Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 24991ae08745Sheppo if (vd_present) { 25001ae08745Sheppo PR0("Not detaching because serving vdisks"); 25011ae08745Sheppo return (DDI_FAILURE); 25021ae08745Sheppo } 25031ae08745Sheppo 25041ae08745Sheppo PR0("Detaching"); 2505445b4c2eSsb155480 if (vds->initialized & VDS_MDEG) { 25061ae08745Sheppo (void) mdeg_unregister(vds->mdeg); 2507445b4c2eSsb155480 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template)); 2508445b4c2eSsb155480 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t)); 2509445b4c2eSsb155480 vds->ispecp = NULL; 2510445b4c2eSsb155480 vds->mdeg = NULL; 2511445b4c2eSsb155480 } 2512445b4c2eSsb155480 25131ae08745Sheppo if (vds->initialized & VDS_LDI) 25141ae08745Sheppo (void) ldi_ident_release(vds->ldi_ident); 25151ae08745Sheppo mod_hash_destroy_hash(vds->vd_table); 25161ae08745Sheppo ddi_soft_state_free(vds_state, instance); 25171ae08745Sheppo return (DDI_SUCCESS); 25181ae08745Sheppo } 25191ae08745Sheppo 25201ae08745Sheppo static boolean_t 25211ae08745Sheppo is_pseudo_device(dev_info_t *dip) 25221ae08745Sheppo { 25231ae08745Sheppo dev_info_t *parent, *root = ddi_root_node(); 25241ae08745Sheppo 25251ae08745Sheppo 25261ae08745Sheppo for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root); 25271ae08745Sheppo parent = ddi_get_parent(parent)) { 25281ae08745Sheppo if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0) 25291ae08745Sheppo return (B_TRUE); 25301ae08745Sheppo } 25311ae08745Sheppo 25321ae08745Sheppo return (B_FALSE); 25331ae08745Sheppo } 25341ae08745Sheppo 25351ae08745Sheppo static int 25360a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd) 25370a55fbb7Slm66018 { 25380a55fbb7Slm66018 int rval, status; 25390a55fbb7Slm66018 major_t major = getmajor(vd->dev[0]); 25400a55fbb7Slm66018 minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 25414bac2208Snarayan struct dk_minfo dk_minfo; 25420a55fbb7Slm66018 25434bac2208Snarayan /* 25444bac2208Snarayan * At this point, vdisk_size is set to the size of partition 2 but 25454bac2208Snarayan * this does not represent the size of the disk because partition 2 25464bac2208Snarayan * may not cover the entire disk and its size does not include reserved 25474bac2208Snarayan * blocks. So we update vdisk_size to be the size of the entire disk. 25484bac2208Snarayan */ 25494bac2208Snarayan if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 25504bac2208Snarayan (intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL), 25514bac2208Snarayan kcred, &rval)) != 0) { 2552690555a1Sachartre PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 25534bac2208Snarayan status); 25540a55fbb7Slm66018 return (status); 25550a55fbb7Slm66018 } 25564bac2208Snarayan vd->vdisk_size = dk_minfo.dki_capacity; 25570a55fbb7Slm66018 25580a55fbb7Slm66018 /* Set full-disk parameters */ 25590a55fbb7Slm66018 vd->vdisk_type = VD_DISK_TYPE_DISK; 25600a55fbb7Slm66018 vd->nslices = (sizeof (vd->dev))/(sizeof (vd->dev[0])); 25610a55fbb7Slm66018 25620a55fbb7Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */ 25630a55fbb7Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 25640a55fbb7Slm66018 vd->dev[0] = 0; 25650a55fbb7Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 25660a55fbb7Slm66018 vd->ldi_handle[0] = NULL; 25670a55fbb7Slm66018 25680a55fbb7Slm66018 /* Initialize device numbers for remaining slices and open them */ 25690a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 25700a55fbb7Slm66018 /* 25710a55fbb7Slm66018 * Skip the entire-disk slice, as it's already open and its 25720a55fbb7Slm66018 * device known 25730a55fbb7Slm66018 */ 25740a55fbb7Slm66018 if (slice == VD_ENTIRE_DISK_SLICE) 25750a55fbb7Slm66018 continue; 25760a55fbb7Slm66018 ASSERT(vd->dev[slice] == 0); 25770a55fbb7Slm66018 ASSERT(vd->ldi_handle[slice] == NULL); 25780a55fbb7Slm66018 25790a55fbb7Slm66018 /* 25800a55fbb7Slm66018 * Construct the device number for the current slice 25810a55fbb7Slm66018 */ 25820a55fbb7Slm66018 vd->dev[slice] = makedevice(major, (minor + slice)); 25830a55fbb7Slm66018 25840a55fbb7Slm66018 /* 258534683adeSsg70180 * Open all slices of the disk to serve them to the client. 258634683adeSsg70180 * Slices are opened exclusively to prevent other threads or 258734683adeSsg70180 * processes in the service domain from performing I/O to 258834683adeSsg70180 * slices being accessed by a client. Failure to open a slice 258934683adeSsg70180 * results in vds not serving this disk, as the client could 259034683adeSsg70180 * attempt (and should be able) to access any slice immediately. 259134683adeSsg70180 * Any slices successfully opened before a failure will get 259234683adeSsg70180 * closed by vds_destroy_vd() as a result of the error returned 259334683adeSsg70180 * by this function. 259434683adeSsg70180 * 259534683adeSsg70180 * We need to do the open with FNDELAY so that opening an empty 259634683adeSsg70180 * slice does not fail. 25970a55fbb7Slm66018 */ 25980a55fbb7Slm66018 PR0("Opening device major %u, minor %u = slice %u", 25990a55fbb7Slm66018 major, minor, slice); 26000a55fbb7Slm66018 if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 260134683adeSsg70180 vd_open_flags | FNDELAY, kcred, &vd->ldi_handle[slice], 26020a55fbb7Slm66018 vd->vds->ldi_ident)) != 0) { 2603690555a1Sachartre PRN("ldi_open_by_dev() returned errno %d " 26040a55fbb7Slm66018 "for slice %u", status, slice); 26050a55fbb7Slm66018 /* vds_destroy_vd() will close any open slices */ 2606690555a1Sachartre vd->ldi_handle[slice] = NULL; 26070a55fbb7Slm66018 return (status); 26080a55fbb7Slm66018 } 26090a55fbb7Slm66018 } 26100a55fbb7Slm66018 26110a55fbb7Slm66018 return (0); 26120a55fbb7Slm66018 } 26130a55fbb7Slm66018 26140a55fbb7Slm66018 static int 26154bac2208Snarayan vd_setup_partition_efi(vd_t *vd) 26164bac2208Snarayan { 26174bac2208Snarayan efi_gpt_t *gpt; 26184bac2208Snarayan efi_gpe_t *gpe; 26194bac2208Snarayan struct uuid uuid = EFI_RESERVED; 26204bac2208Snarayan uint32_t crc; 26214bac2208Snarayan int length; 26224bac2208Snarayan 26234bac2208Snarayan length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t); 26244bac2208Snarayan 26254bac2208Snarayan gpt = kmem_zalloc(length, KM_SLEEP); 26264bac2208Snarayan gpe = (efi_gpe_t *)(gpt + 1); 26274bac2208Snarayan 26284bac2208Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 26294bac2208Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 26304bac2208Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 26314bac2208Snarayan gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL); 26324bac2208Snarayan gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 26334bac2208Snarayan gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 26344bac2208Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 26354bac2208Snarayan 26364bac2208Snarayan UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid); 26374bac2208Snarayan gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA; 26384bac2208Snarayan gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA; 26394bac2208Snarayan 26404bac2208Snarayan CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table); 26414bac2208Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 26424bac2208Snarayan 26434bac2208Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 26444bac2208Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 26454bac2208Snarayan 26464bac2208Snarayan vd->dk_efi.dki_lba = 0; 26474bac2208Snarayan vd->dk_efi.dki_length = length; 26484bac2208Snarayan vd->dk_efi.dki_data = gpt; 26494bac2208Snarayan 26504bac2208Snarayan return (0); 26514bac2208Snarayan } 26524bac2208Snarayan 26534bac2208Snarayan static int 26543c96341aSnarayan vd_setup_file(vd_t *vd) 26553c96341aSnarayan { 26563c96341aSnarayan int i, rval, status; 2657690555a1Sachartre ushort_t sum; 26583c96341aSnarayan vattr_t vattr; 26593c96341aSnarayan dev_t dev; 26603c96341aSnarayan char *file_path = vd->device_path; 26613c96341aSnarayan char dev_path[MAXPATHLEN + 1]; 26623c96341aSnarayan ldi_handle_t lhandle; 26633c96341aSnarayan struct dk_cinfo dk_cinfo; 2664690555a1Sachartre struct dk_label label; 26653c96341aSnarayan 26663c96341aSnarayan /* make sure the file is valid */ 26673c96341aSnarayan if ((status = lookupname(file_path, UIO_SYSSPACE, FOLLOW, 26683c96341aSnarayan NULLVPP, &vd->file_vnode)) != 0) { 2669690555a1Sachartre PRN("Cannot lookup file(%s) errno %d", file_path, status); 26703c96341aSnarayan return (status); 26713c96341aSnarayan } 26723c96341aSnarayan 26733c96341aSnarayan if (vd->file_vnode->v_type != VREG) { 2674690555a1Sachartre PRN("Invalid file type (%s)\n", file_path); 26753c96341aSnarayan VN_RELE(vd->file_vnode); 26763c96341aSnarayan return (EBADF); 26773c96341aSnarayan } 26783c96341aSnarayan VN_RELE(vd->file_vnode); 26793c96341aSnarayan 26803c96341aSnarayan if ((status = vn_open(file_path, UIO_SYSSPACE, vd_open_flags | FOFFMAX, 26813c96341aSnarayan 0, &vd->file_vnode, 0, 0)) != 0) { 2682690555a1Sachartre PRN("vn_open(%s) = errno %d", file_path, status); 26833c96341aSnarayan return (status); 26843c96341aSnarayan } 26853c96341aSnarayan 2686690555a1Sachartre /* 2687690555a1Sachartre * We set vd->file now so that vds_destroy_vd will take care of 2688690555a1Sachartre * closing the file and releasing the vnode in case of an error. 2689690555a1Sachartre */ 2690690555a1Sachartre vd->file = B_TRUE; 2691690555a1Sachartre vd->pseudo = B_FALSE; 2692690555a1Sachartre 26933c96341aSnarayan vattr.va_mask = AT_SIZE; 26943c96341aSnarayan if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred)) != 0) { 2695690555a1Sachartre PRN("VOP_GETATTR(%s) = errno %d", file_path, status); 26963c96341aSnarayan return (EIO); 26973c96341aSnarayan } 26983c96341aSnarayan 26993c96341aSnarayan vd->file_size = vattr.va_size; 27003c96341aSnarayan /* size should be at least sizeof(dk_label) */ 27013c96341aSnarayan if (vd->file_size < sizeof (struct dk_label)) { 27023c96341aSnarayan PRN("Size of file has to be at least %ld bytes", 27033c96341aSnarayan sizeof (struct dk_label)); 27043c96341aSnarayan return (EIO); 27053c96341aSnarayan } 27063c96341aSnarayan 2707690555a1Sachartre if (vd->file_vnode->v_flag & VNOMAP) { 2708690555a1Sachartre PRN("File %s cannot be mapped", file_path); 27093c96341aSnarayan return (EIO); 27103c96341aSnarayan } 27113c96341aSnarayan 2712690555a1Sachartre /* read label from file */ 2713690555a1Sachartre if (VD_FILE_LABEL_READ(vd, &label) < 0) { 2714690555a1Sachartre PRN("Can't read label from %s", file_path); 2715690555a1Sachartre return (EIO); 2716690555a1Sachartre } 27173c96341aSnarayan 27183c96341aSnarayan /* label checksum */ 2719690555a1Sachartre sum = vd_lbl2cksum(&label); 27203c96341aSnarayan 2721690555a1Sachartre if (label.dkl_magic != DKL_MAGIC || label.dkl_cksum != sum) { 27223c96341aSnarayan PR0("%s has an invalid disk label " 27233c96341aSnarayan "(magic=%x cksum=%x (expect %x))", 2724690555a1Sachartre file_path, label.dkl_magic, label.dkl_cksum, sum); 27253c96341aSnarayan 27263c96341aSnarayan /* default label */ 2727690555a1Sachartre bzero(&label, sizeof (struct dk_label)); 27283c96341aSnarayan 27293c96341aSnarayan /* 27303c96341aSnarayan * We must have a resonable number of cylinders and sectors so 27313c96341aSnarayan * that newfs can run using default values. 27323c96341aSnarayan * 27333c96341aSnarayan * if (disk_size < 2MB) 27343c96341aSnarayan * phys_cylinders = disk_size / 100K 27353c96341aSnarayan * else 27363c96341aSnarayan * phys_cylinders = disk_size / 300K 27373c96341aSnarayan * 27383c96341aSnarayan * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders 27393c96341aSnarayan * alt_cylinders = (phys_cylinders > 2) ? 2 : 0; 27403c96341aSnarayan * data_cylinders = phys_cylinders - alt_cylinders 27413c96341aSnarayan * 27423c96341aSnarayan * sectors = disk_size / (phys_cylinders * blk_size) 27433c96341aSnarayan */ 27443c96341aSnarayan if (vd->file_size < (2 * 1024 * 1024)) 2745690555a1Sachartre label.dkl_pcyl = vd->file_size / (100 * 1024); 27463c96341aSnarayan else 2747690555a1Sachartre label.dkl_pcyl = vd->file_size / (300 * 1024); 27483c96341aSnarayan 2749690555a1Sachartre if (label.dkl_pcyl == 0) 2750690555a1Sachartre label.dkl_pcyl = 1; 27513c96341aSnarayan 2752690555a1Sachartre if (label.dkl_pcyl > 2) 2753690555a1Sachartre label.dkl_acyl = 2; 27543c96341aSnarayan else 2755690555a1Sachartre label.dkl_acyl = 0; 27563c96341aSnarayan 2757690555a1Sachartre label.dkl_nsect = vd->file_size / 2758690555a1Sachartre (DEV_BSIZE * label.dkl_pcyl); 2759690555a1Sachartre label.dkl_ncyl = label.dkl_pcyl - label.dkl_acyl; 2760690555a1Sachartre label.dkl_nhead = 1; 2761690555a1Sachartre label.dkl_write_reinstruct = 0; 2762690555a1Sachartre label.dkl_read_reinstruct = 0; 2763690555a1Sachartre label.dkl_rpm = 7200; 2764690555a1Sachartre label.dkl_apc = 0; 2765690555a1Sachartre label.dkl_intrlv = 0; 2766690555a1Sachartre label.dkl_magic = DKL_MAGIC; 27673c96341aSnarayan 27683c96341aSnarayan PR0("requested disk size: %ld bytes\n", vd->file_size); 2769690555a1Sachartre PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label.dkl_pcyl, 2770690555a1Sachartre label.dkl_nhead, label.dkl_nsect); 27713c96341aSnarayan PR0("provided disk size: %ld bytes\n", (uint64_t) 2772690555a1Sachartre (label.dkl_pcyl * 2773690555a1Sachartre label.dkl_nhead * label.dkl_nsect * DEV_BSIZE)); 27743c96341aSnarayan 27753c96341aSnarayan /* 27763c96341aSnarayan * We must have a correct label name otherwise format(1m) will 27773c96341aSnarayan * not recognized the disk as labeled. 27783c96341aSnarayan */ 2779690555a1Sachartre (void) snprintf(label.dkl_asciilabel, LEN_DKL_ASCII, 27803c96341aSnarayan "SUNVDSK cyl %d alt %d hd %d sec %d", 2781690555a1Sachartre label.dkl_ncyl, label.dkl_acyl, label.dkl_nhead, 2782690555a1Sachartre label.dkl_nsect); 27833c96341aSnarayan 27843c96341aSnarayan /* default VTOC */ 2785690555a1Sachartre label.dkl_vtoc.v_version = V_VERSION; 2786690555a1Sachartre label.dkl_vtoc.v_nparts = V_NUMPAR; 2787690555a1Sachartre label.dkl_vtoc.v_sanity = VTOC_SANE; 2788690555a1Sachartre label.dkl_vtoc.v_part[2].p_tag = V_BACKUP; 2789690555a1Sachartre label.dkl_map[2].dkl_cylno = 0; 2790690555a1Sachartre label.dkl_map[2].dkl_nblk = label.dkl_ncyl * 2791690555a1Sachartre label.dkl_nhead * label.dkl_nsect; 2792690555a1Sachartre label.dkl_map[0] = label.dkl_map[2]; 2793690555a1Sachartre label.dkl_map[0] = label.dkl_map[2]; 2794690555a1Sachartre label.dkl_cksum = vd_lbl2cksum(&label); 2795690555a1Sachartre 2796690555a1Sachartre /* write default label to file */ 2797690555a1Sachartre if (VD_FILE_LABEL_WRITE(vd, &label) < 0) { 2798690555a1Sachartre PRN("Can't write label to %s", file_path); 2799690555a1Sachartre return (EIO); 2800690555a1Sachartre } 28013c96341aSnarayan } 28023c96341aSnarayan 2803690555a1Sachartre vd->nslices = label.dkl_vtoc.v_nparts; 28043c96341aSnarayan 28053c96341aSnarayan /* sector size = block size = DEV_BSIZE */ 2806690555a1Sachartre vd->vdisk_size = (label.dkl_pcyl * 2807690555a1Sachartre label.dkl_nhead * label.dkl_nsect) / DEV_BSIZE; 28083c96341aSnarayan vd->vdisk_type = VD_DISK_TYPE_DISK; 28093c96341aSnarayan vd->vdisk_label = VD_DISK_LABEL_VTOC; 28103c96341aSnarayan vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */ 28113c96341aSnarayan 28123c96341aSnarayan /* Get max_xfer_sz from the device where the file is */ 28133c96341aSnarayan dev = vd->file_vnode->v_vfsp->vfs_dev; 28143c96341aSnarayan dev_path[0] = NULL; 28153c96341aSnarayan if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) { 28163c96341aSnarayan PR0("underlying device = %s\n", dev_path); 28173c96341aSnarayan } 28183c96341aSnarayan 28193c96341aSnarayan if ((status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, 28203c96341aSnarayan kcred, &lhandle, vd->vds->ldi_ident)) != 0) { 28213c96341aSnarayan PR0("ldi_open_by_dev() returned errno %d for device %s", 28223c96341aSnarayan status, dev_path); 28233c96341aSnarayan } else { 28243c96341aSnarayan if ((status = ldi_ioctl(lhandle, DKIOCINFO, 28253c96341aSnarayan (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred, 28263c96341aSnarayan &rval)) != 0) { 28273c96341aSnarayan PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 28283c96341aSnarayan status, dev_path); 28293c96341aSnarayan } else { 28303c96341aSnarayan /* 28313c96341aSnarayan * Store the device's max transfer size for 28323c96341aSnarayan * return to the client 28333c96341aSnarayan */ 28343c96341aSnarayan vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 28353c96341aSnarayan } 28363c96341aSnarayan 28373c96341aSnarayan PR0("close the device %s", dev_path); 28383c96341aSnarayan (void) ldi_close(lhandle, FREAD, kcred); 28393c96341aSnarayan } 28403c96341aSnarayan 28413c96341aSnarayan PR0("using for file %s, dev %s, max_xfer = %u blks", 28423c96341aSnarayan file_path, dev_path, vd->max_xfer_sz); 28433c96341aSnarayan 2844690555a1Sachartre vd->dk_geom.dkg_ncyl = label.dkl_ncyl; 2845690555a1Sachartre vd->dk_geom.dkg_acyl = label.dkl_acyl; 2846690555a1Sachartre vd->dk_geom.dkg_pcyl = label.dkl_pcyl; 2847690555a1Sachartre vd->dk_geom.dkg_nhead = label.dkl_nhead; 2848690555a1Sachartre vd->dk_geom.dkg_nsect = label.dkl_nsect; 2849690555a1Sachartre vd->dk_geom.dkg_intrlv = label.dkl_intrlv; 2850690555a1Sachartre vd->dk_geom.dkg_apc = label.dkl_apc; 2851690555a1Sachartre vd->dk_geom.dkg_rpm = label.dkl_rpm; 2852690555a1Sachartre vd->dk_geom.dkg_write_reinstruct = label.dkl_write_reinstruct; 2853690555a1Sachartre vd->dk_geom.dkg_read_reinstruct = label.dkl_read_reinstruct; 28543c96341aSnarayan 2855690555a1Sachartre vd->vtoc.v_sanity = label.dkl_vtoc.v_sanity; 2856690555a1Sachartre vd->vtoc.v_version = label.dkl_vtoc.v_version; 28573c96341aSnarayan vd->vtoc.v_sectorsz = DEV_BSIZE; 2858690555a1Sachartre vd->vtoc.v_nparts = label.dkl_vtoc.v_nparts; 28593c96341aSnarayan 2860690555a1Sachartre bcopy(label.dkl_vtoc.v_volume, vd->vtoc.v_volume, 28613c96341aSnarayan LEN_DKL_VVOL); 2862690555a1Sachartre bcopy(label.dkl_asciilabel, vd->vtoc.v_asciilabel, 28633c96341aSnarayan LEN_DKL_ASCII); 28643c96341aSnarayan 28653c96341aSnarayan for (i = 0; i < vd->nslices; i++) { 2866690555a1Sachartre vd->vtoc.timestamp[i] = label.dkl_vtoc.v_timestamp[i]; 2867690555a1Sachartre vd->vtoc.v_part[i].p_tag = label.dkl_vtoc.v_part[i].p_tag; 2868690555a1Sachartre vd->vtoc.v_part[i].p_flag = label.dkl_vtoc.v_part[i].p_flag; 2869690555a1Sachartre vd->vtoc.v_part[i].p_start = label.dkl_map[i].dkl_cylno * 2870690555a1Sachartre label.dkl_nhead * label.dkl_nsect; 2871690555a1Sachartre vd->vtoc.v_part[i].p_size = label.dkl_map[i].dkl_nblk; 28723c96341aSnarayan vd->ldi_handle[i] = NULL; 28733c96341aSnarayan vd->dev[i] = NULL; 28743c96341aSnarayan } 28753c96341aSnarayan 28763c96341aSnarayan return (0); 28773c96341aSnarayan } 28783c96341aSnarayan 28793c96341aSnarayan static int 28803c96341aSnarayan vd_setup_vd(vd_t *vd) 28811ae08745Sheppo { 2882e1ebb9ecSlm66018 int rval, status; 28831ae08745Sheppo dev_info_t *dip; 28841ae08745Sheppo struct dk_cinfo dk_cinfo; 28853c96341aSnarayan char *device_path = vd->device_path; 28861ae08745Sheppo 28874bac2208Snarayan /* 28884bac2208Snarayan * We need to open with FNDELAY so that opening an empty partition 28894bac2208Snarayan * does not fail. 28904bac2208Snarayan */ 28914bac2208Snarayan if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY, 28924bac2208Snarayan kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) { 28933c96341aSnarayan PR0("ldi_open_by_name(%s) = errno %d", device_path, status); 2894690555a1Sachartre vd->ldi_handle[0] = NULL; 28953c96341aSnarayan 28963c96341aSnarayan /* this may not be a device try opening as a file */ 28973c96341aSnarayan if (status == ENXIO || status == ENODEV) 28983c96341aSnarayan status = vd_setup_file(vd); 28993c96341aSnarayan if (status) { 2900690555a1Sachartre PRN("Cannot use device/file (%s), errno=%d\n", 29013c96341aSnarayan device_path, status); 29023c96341aSnarayan if (status == ENXIO || status == ENODEV || 29033c96341aSnarayan status == ENOENT) { 29043c96341aSnarayan return (EAGAIN); 29053c96341aSnarayan } 29063c96341aSnarayan } 29070a55fbb7Slm66018 return (status); 29080a55fbb7Slm66018 } 29090a55fbb7Slm66018 29104bac2208Snarayan /* 29114bac2208Snarayan * nslices must be updated now so that vds_destroy_vd() will close 29124bac2208Snarayan * the slice we have just opened in case of an error. 29134bac2208Snarayan */ 29144bac2208Snarayan vd->nslices = 1; 29153c96341aSnarayan vd->file = B_FALSE; 29164bac2208Snarayan 2917e1ebb9ecSlm66018 /* Get device number and size of backing device */ 29180a55fbb7Slm66018 if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 29191ae08745Sheppo PRN("ldi_get_dev() returned errno %d for %s", 2920e1ebb9ecSlm66018 status, device_path); 29211ae08745Sheppo return (status); 29221ae08745Sheppo } 29230a55fbb7Slm66018 if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 2924e1ebb9ecSlm66018 PRN("ldi_get_size() failed for %s", device_path); 29251ae08745Sheppo return (EIO); 29261ae08745Sheppo } 2927e1ebb9ecSlm66018 vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 29281ae08745Sheppo 2929e1ebb9ecSlm66018 /* Verify backing device supports dk_cinfo, dk_geom, and vtoc */ 2930e1ebb9ecSlm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 2931e1ebb9ecSlm66018 (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred, 2932e1ebb9ecSlm66018 &rval)) != 0) { 2933e1ebb9ecSlm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 2934e1ebb9ecSlm66018 status, device_path); 2935e1ebb9ecSlm66018 return (status); 2936e1ebb9ecSlm66018 } 2937e1ebb9ecSlm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) { 2938e1ebb9ecSlm66018 PRN("slice %u >= maximum slice %u for %s", 2939e1ebb9ecSlm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path); 2940e1ebb9ecSlm66018 return (EIO); 2941e1ebb9ecSlm66018 } 29424bac2208Snarayan 29434bac2208Snarayan status = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, &vd->vdisk_label); 29444bac2208Snarayan 29454bac2208Snarayan if (status != 0) { 29464bac2208Snarayan PRN("vd_read_vtoc returned errno %d for %s", 2947e1ebb9ecSlm66018 status, device_path); 2948e1ebb9ecSlm66018 return (status); 2949e1ebb9ecSlm66018 } 29504bac2208Snarayan 29514bac2208Snarayan if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 29524bac2208Snarayan (status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 29534bac2208Snarayan (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL), 29544bac2208Snarayan kcred, &rval)) != 0) { 29554bac2208Snarayan PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s", 2956e1ebb9ecSlm66018 status, device_path); 2957e1ebb9ecSlm66018 return (status); 2958e1ebb9ecSlm66018 } 2959e1ebb9ecSlm66018 2960e1ebb9ecSlm66018 /* Store the device's max transfer size for return to the client */ 2961e1ebb9ecSlm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 2962e1ebb9ecSlm66018 2963e1ebb9ecSlm66018 /* Determine if backing device is a pseudo device */ 29641ae08745Sheppo if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]), 29651ae08745Sheppo dev_to_instance(vd->dev[0]), 0)) == NULL) { 2966e1ebb9ecSlm66018 PRN("%s is no longer accessible", device_path); 29671ae08745Sheppo return (EIO); 29681ae08745Sheppo } 29691ae08745Sheppo vd->pseudo = is_pseudo_device(dip); 29701ae08745Sheppo ddi_release_devi(dip); 29711ae08745Sheppo if (vd->pseudo) { 29721ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 29731ae08745Sheppo vd->nslices = 1; 29741ae08745Sheppo return (0); /* ...and we're done */ 29751ae08745Sheppo } 29761ae08745Sheppo 29770a55fbb7Slm66018 /* If slice is entire-disk slice, initialize for full disk */ 29780a55fbb7Slm66018 if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE) 29790a55fbb7Slm66018 return (vd_setup_full_disk(vd)); 29801ae08745Sheppo 29810a55fbb7Slm66018 2982e1ebb9ecSlm66018 /* Otherwise, we have a non-entire slice of a device */ 29831ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 29841ae08745Sheppo vd->nslices = 1; 29851ae08745Sheppo 29864bac2208Snarayan if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 29874bac2208Snarayan status = vd_setup_partition_efi(vd); 29884bac2208Snarayan return (status); 29894bac2208Snarayan } 29901ae08745Sheppo 2991e1ebb9ecSlm66018 /* Initialize dk_geom structure for single-slice device */ 29921ae08745Sheppo if (vd->dk_geom.dkg_nsect == 0) { 2993690555a1Sachartre PRN("%s geometry claims 0 sectors per track", device_path); 29941ae08745Sheppo return (EIO); 29951ae08745Sheppo } 29961ae08745Sheppo if (vd->dk_geom.dkg_nhead == 0) { 2997690555a1Sachartre PRN("%s geometry claims 0 heads", device_path); 29981ae08745Sheppo return (EIO); 29991ae08745Sheppo } 30001ae08745Sheppo vd->dk_geom.dkg_ncyl = 3001e1ebb9ecSlm66018 vd->vdisk_size/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead; 30021ae08745Sheppo vd->dk_geom.dkg_acyl = 0; 30031ae08745Sheppo vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 30041ae08745Sheppo 30051ae08745Sheppo 3006e1ebb9ecSlm66018 /* Initialize vtoc structure for single-slice device */ 30071ae08745Sheppo bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 30081ae08745Sheppo MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 30091ae08745Sheppo bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 30101ae08745Sheppo vd->vtoc.v_nparts = 1; 30111ae08745Sheppo vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 30121ae08745Sheppo vd->vtoc.v_part[0].p_flag = 0; 30131ae08745Sheppo vd->vtoc.v_part[0].p_start = 0; 3014e1ebb9ecSlm66018 vd->vtoc.v_part[0].p_size = vd->vdisk_size; 30151ae08745Sheppo bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 30161ae08745Sheppo MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel))); 30171ae08745Sheppo 30181ae08745Sheppo 30191ae08745Sheppo return (0); 30201ae08745Sheppo } 30211ae08745Sheppo 30221ae08745Sheppo static int 3023e1ebb9ecSlm66018 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id, 30241ae08745Sheppo vd_t **vdp) 30251ae08745Sheppo { 30261ae08745Sheppo char tq_name[TASKQ_NAMELEN]; 30270a55fbb7Slm66018 int status; 30281ae08745Sheppo ddi_iblock_cookie_t iblock = NULL; 30291ae08745Sheppo ldc_attr_t ldc_attr; 30301ae08745Sheppo vd_t *vd; 30311ae08745Sheppo 30321ae08745Sheppo 30331ae08745Sheppo ASSERT(vds != NULL); 3034e1ebb9ecSlm66018 ASSERT(device_path != NULL); 30351ae08745Sheppo ASSERT(vdp != NULL); 3036e1ebb9ecSlm66018 PR0("Adding vdisk for %s", device_path); 30371ae08745Sheppo 30381ae08745Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 30391ae08745Sheppo PRN("No memory for virtual disk"); 30401ae08745Sheppo return (EAGAIN); 30411ae08745Sheppo } 30421ae08745Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 30431ae08745Sheppo vd->vds = vds; 30443c96341aSnarayan (void) strncpy(vd->device_path, device_path, MAXPATHLEN); 30451ae08745Sheppo 30460a55fbb7Slm66018 /* Open vdisk and initialize parameters */ 30473c96341aSnarayan if ((status = vd_setup_vd(vd)) == 0) { 30483c96341aSnarayan vd->initialized |= VD_DISK_READY; 30491ae08745Sheppo 30503c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 30513c96341aSnarayan PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u", 30523c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 30533c96341aSnarayan (vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"), 30543c96341aSnarayan vd->nslices); 30553c96341aSnarayan } else { 30563c96341aSnarayan if (status != EAGAIN) 30573c96341aSnarayan return (status); 30583c96341aSnarayan } 30591ae08745Sheppo 30601ae08745Sheppo /* Initialize locking */ 30611ae08745Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 30621ae08745Sheppo &iblock) != DDI_SUCCESS) { 30631ae08745Sheppo PRN("Could not get iblock cookie."); 30641ae08745Sheppo return (EIO); 30651ae08745Sheppo } 30661ae08745Sheppo 30671ae08745Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 30681ae08745Sheppo vd->initialized |= VD_LOCKING; 30691ae08745Sheppo 30701ae08745Sheppo 3071d10e4ef2Snarayan /* Create start and completion task queues for the vdisk */ 3072d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 30731ae08745Sheppo PR1("tq_name = %s", tq_name); 3074d10e4ef2Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 30751ae08745Sheppo TASKQ_DEFAULTPRI, 0)) == NULL) { 30761ae08745Sheppo PRN("Could not create task queue"); 30771ae08745Sheppo return (EIO); 30781ae08745Sheppo } 3079d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 3080d10e4ef2Snarayan PR1("tq_name = %s", tq_name); 3081d10e4ef2Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 3082d10e4ef2Snarayan TASKQ_DEFAULTPRI, 0)) == NULL) { 3083d10e4ef2Snarayan PRN("Could not create task queue"); 3084d10e4ef2Snarayan return (EIO); 3085d10e4ef2Snarayan } 3086d10e4ef2Snarayan vd->enabled = 1; /* before callback can dispatch to startq */ 30871ae08745Sheppo 30881ae08745Sheppo 30891ae08745Sheppo /* Bring up LDC */ 30901ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC; 30911ae08745Sheppo ldc_attr.instance = ddi_get_instance(vds->dip); 30921ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; 3093e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 30941ae08745Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 3095690555a1Sachartre PRN("Could not initialize LDC channel %lu, " 3096690555a1Sachartre "init failed with error %d", ldc_id, status); 30971ae08745Sheppo return (status); 30981ae08745Sheppo } 30991ae08745Sheppo vd->initialized |= VD_LDC; 31001ae08745Sheppo 31011ae08745Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 31021ae08745Sheppo (caddr_t)vd)) != 0) { 3103690555a1Sachartre PRN("Could not initialize LDC channel %lu," 3104690555a1Sachartre "reg_callback failed with error %d", ldc_id, status); 31051ae08745Sheppo return (status); 31061ae08745Sheppo } 31071ae08745Sheppo 31081ae08745Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) { 3109690555a1Sachartre PRN("Could not initialize LDC channel %lu," 3110690555a1Sachartre "open failed with error %d", ldc_id, status); 31111ae08745Sheppo return (status); 31121ae08745Sheppo } 31131ae08745Sheppo 31143af08d82Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) { 311534683adeSsg70180 PR0("ldc_up() returned errno %d", status); 31163af08d82Slm66018 } 31173af08d82Slm66018 31184bac2208Snarayan /* Allocate the inband task memory handle */ 31194bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 31204bac2208Snarayan if (status) { 3121690555a1Sachartre PRN("Could not initialize LDC channel %lu," 3122690555a1Sachartre "alloc_handle failed with error %d", ldc_id, status); 31234bac2208Snarayan return (ENXIO); 31244bac2208Snarayan } 31251ae08745Sheppo 31261ae08745Sheppo /* Add the successfully-initialized vdisk to the server's table */ 31271ae08745Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 31281ae08745Sheppo PRN("Error adding vdisk ID %lu to table", id); 31291ae08745Sheppo return (EIO); 31301ae08745Sheppo } 31311ae08745Sheppo 31323af08d82Slm66018 /* Allocate the staging buffer */ 31333af08d82Slm66018 vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 31343af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 31353af08d82Slm66018 31363af08d82Slm66018 /* store initial state */ 31373af08d82Slm66018 vd->state = VD_STATE_INIT; 31383af08d82Slm66018 31391ae08745Sheppo return (0); 31401ae08745Sheppo } 31411ae08745Sheppo 31423af08d82Slm66018 static void 31433af08d82Slm66018 vd_free_dring_task(vd_t *vdp) 31443af08d82Slm66018 { 31453af08d82Slm66018 if (vdp->dring_task != NULL) { 31463af08d82Slm66018 ASSERT(vdp->dring_len != 0); 31473af08d82Slm66018 /* Free all dring_task memory handles */ 31483af08d82Slm66018 for (int i = 0; i < vdp->dring_len; i++) { 31493af08d82Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 31503af08d82Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 31513af08d82Slm66018 vdp->dring_task[i].msg = NULL; 31523af08d82Slm66018 } 31533af08d82Slm66018 kmem_free(vdp->dring_task, 31543af08d82Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len); 31553af08d82Slm66018 vdp->dring_task = NULL; 31563af08d82Slm66018 } 31573af08d82Slm66018 } 31583af08d82Slm66018 31591ae08745Sheppo /* 31601ae08745Sheppo * Destroy the state associated with a virtual disk 31611ae08745Sheppo */ 31621ae08745Sheppo static void 31631ae08745Sheppo vds_destroy_vd(void *arg) 31641ae08745Sheppo { 31651ae08745Sheppo vd_t *vd = (vd_t *)arg; 316634683adeSsg70180 int retry = 0, rv; 31671ae08745Sheppo 31681ae08745Sheppo if (vd == NULL) 31691ae08745Sheppo return; 31701ae08745Sheppo 3171d10e4ef2Snarayan PR0("Destroying vdisk state"); 3172d10e4ef2Snarayan 31734bac2208Snarayan if (vd->dk_efi.dki_data != NULL) 31744bac2208Snarayan kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length); 31754bac2208Snarayan 31761ae08745Sheppo /* Disable queuing requests for the vdisk */ 31771ae08745Sheppo if (vd->initialized & VD_LOCKING) { 31781ae08745Sheppo mutex_enter(&vd->lock); 31791ae08745Sheppo vd->enabled = 0; 31801ae08745Sheppo mutex_exit(&vd->lock); 31811ae08745Sheppo } 31821ae08745Sheppo 3183d10e4ef2Snarayan /* Drain and destroy start queue (*before* destroying completionq) */ 3184d10e4ef2Snarayan if (vd->startq != NULL) 3185d10e4ef2Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 3186d10e4ef2Snarayan 3187d10e4ef2Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */ 3188d10e4ef2Snarayan if (vd->completionq != NULL) 3189d10e4ef2Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 3190d10e4ef2Snarayan 31913af08d82Slm66018 vd_free_dring_task(vd); 31923af08d82Slm66018 319334683adeSsg70180 /* Free the inband task memory handle */ 319434683adeSsg70180 (void) ldc_mem_free_handle(vd->inband_task.mhdl); 319534683adeSsg70180 319634683adeSsg70180 /* Shut down LDC */ 319734683adeSsg70180 if (vd->initialized & VD_LDC) { 319834683adeSsg70180 /* unmap the dring */ 319934683adeSsg70180 if (vd->initialized & VD_DRING) 320034683adeSsg70180 (void) ldc_mem_dring_unmap(vd->dring_handle); 320134683adeSsg70180 320234683adeSsg70180 /* close LDC channel - retry on EAGAIN */ 320334683adeSsg70180 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) { 320434683adeSsg70180 if (++retry > vds_ldc_retries) { 320534683adeSsg70180 PR0("Timed out closing channel"); 320634683adeSsg70180 break; 320734683adeSsg70180 } 320834683adeSsg70180 drv_usecwait(vds_ldc_delay); 320934683adeSsg70180 } 321034683adeSsg70180 if (rv == 0) { 321134683adeSsg70180 (void) ldc_unreg_callback(vd->ldc_handle); 321234683adeSsg70180 (void) ldc_fini(vd->ldc_handle); 321334683adeSsg70180 } else { 321434683adeSsg70180 /* 321534683adeSsg70180 * Closing the LDC channel has failed. Ideally we should 321634683adeSsg70180 * fail here but there is no Zeus level infrastructure 321734683adeSsg70180 * to handle this. The MD has already been changed and 321834683adeSsg70180 * we have to do the close. So we try to do as much 321934683adeSsg70180 * clean up as we can. 322034683adeSsg70180 */ 322134683adeSsg70180 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE); 322234683adeSsg70180 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN) 322334683adeSsg70180 drv_usecwait(vds_ldc_delay); 322434683adeSsg70180 } 322534683adeSsg70180 } 322634683adeSsg70180 32273af08d82Slm66018 /* Free the staging buffer for msgs */ 32283af08d82Slm66018 if (vd->vio_msgp != NULL) { 32293af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 32303af08d82Slm66018 vd->vio_msgp = NULL; 32313af08d82Slm66018 } 32323af08d82Slm66018 32333af08d82Slm66018 /* Free the inband message buffer */ 32343af08d82Slm66018 if (vd->inband_task.msg != NULL) { 32353af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 32363af08d82Slm66018 vd->inband_task.msg = NULL; 3237d10e4ef2Snarayan } 32383c96341aSnarayan if (vd->file) { 3239690555a1Sachartre /* Close file */ 32403c96341aSnarayan (void) VOP_CLOSE(vd->file_vnode, vd_open_flags, 1, 32413c96341aSnarayan 0, kcred); 32423c96341aSnarayan VN_RELE(vd->file_vnode); 32433c96341aSnarayan } else { 32441ae08745Sheppo /* Close any open backing-device slices */ 32451ae08745Sheppo for (uint_t slice = 0; slice < vd->nslices; slice++) { 32461ae08745Sheppo if (vd->ldi_handle[slice] != NULL) { 32471ae08745Sheppo PR0("Closing slice %u", slice); 32481ae08745Sheppo (void) ldi_close(vd->ldi_handle[slice], 32494bac2208Snarayan vd_open_flags | FNDELAY, kcred); 32501ae08745Sheppo } 32511ae08745Sheppo } 32523c96341aSnarayan } 32531ae08745Sheppo 32541ae08745Sheppo /* Free lock */ 32551ae08745Sheppo if (vd->initialized & VD_LOCKING) 32561ae08745Sheppo mutex_destroy(&vd->lock); 32571ae08745Sheppo 32581ae08745Sheppo /* Finally, free the vdisk structure itself */ 32591ae08745Sheppo kmem_free(vd, sizeof (*vd)); 32601ae08745Sheppo } 32611ae08745Sheppo 32621ae08745Sheppo static int 3263e1ebb9ecSlm66018 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id) 32641ae08745Sheppo { 32651ae08745Sheppo int status; 32661ae08745Sheppo vd_t *vd = NULL; 32671ae08745Sheppo 32681ae08745Sheppo 3269e1ebb9ecSlm66018 if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0) 32701ae08745Sheppo vds_destroy_vd(vd); 32711ae08745Sheppo 32721ae08745Sheppo return (status); 32731ae08745Sheppo } 32741ae08745Sheppo 32751ae08745Sheppo static int 32761ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 32771ae08745Sheppo uint64_t *ldc_id) 32781ae08745Sheppo { 32791ae08745Sheppo int num_channels; 32801ae08745Sheppo 32811ae08745Sheppo 32821ae08745Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */ 32831ae08745Sheppo if ((num_channels = md_scan_dag(md, vd_node, 32841ae08745Sheppo md_find_name(md, VD_CHANNEL_ENDPOINT), 32851ae08745Sheppo md_find_name(md, "fwd"), channel)) <= 0) { 32861ae08745Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 32871ae08745Sheppo return (-1); 32881ae08745Sheppo } 32891ae08745Sheppo 32901ae08745Sheppo /* Get the "id" value for the first channel endpoint node */ 32911ae08745Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 32921ae08745Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk", 32931ae08745Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT); 32941ae08745Sheppo return (-1); 32951ae08745Sheppo } 32961ae08745Sheppo 32971ae08745Sheppo if (num_channels > 1) { 32981ae08745Sheppo PRN("Using ID of first of multiple channels for this vdisk"); 32991ae08745Sheppo } 33001ae08745Sheppo 33011ae08745Sheppo return (0); 33021ae08745Sheppo } 33031ae08745Sheppo 33041ae08745Sheppo static int 33051ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 33061ae08745Sheppo { 33071ae08745Sheppo int num_nodes, status; 33081ae08745Sheppo size_t size; 33091ae08745Sheppo mde_cookie_t *channel; 33101ae08745Sheppo 33111ae08745Sheppo 33121ae08745Sheppo if ((num_nodes = md_node_count(md)) <= 0) { 33131ae08745Sheppo PRN("Invalid node count in Machine Description subtree"); 33141ae08745Sheppo return (-1); 33151ae08745Sheppo } 33161ae08745Sheppo size = num_nodes*(sizeof (*channel)); 33171ae08745Sheppo channel = kmem_zalloc(size, KM_SLEEP); 33181ae08745Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 33191ae08745Sheppo kmem_free(channel, size); 33201ae08745Sheppo 33211ae08745Sheppo return (status); 33221ae08745Sheppo } 33231ae08745Sheppo 33241ae08745Sheppo static void 33251ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 33261ae08745Sheppo { 3327e1ebb9ecSlm66018 char *device_path = NULL; 33281ae08745Sheppo uint64_t id = 0, ldc_id = 0; 33291ae08745Sheppo 33301ae08745Sheppo 33311ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 33321ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 33331ae08745Sheppo return; 33341ae08745Sheppo } 33351ae08745Sheppo PR0("Adding vdisk ID %lu", id); 33361ae08745Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 3337e1ebb9ecSlm66018 &device_path) != 0) { 33381ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 33391ae08745Sheppo return; 33401ae08745Sheppo } 33411ae08745Sheppo 33421ae08745Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 33431ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", id); 33441ae08745Sheppo return; 33451ae08745Sheppo } 33461ae08745Sheppo 3347e1ebb9ecSlm66018 if (vds_init_vd(vds, id, device_path, ldc_id) != 0) { 33481ae08745Sheppo PRN("Failed to add vdisk ID %lu", id); 33491ae08745Sheppo return; 33501ae08745Sheppo } 33511ae08745Sheppo } 33521ae08745Sheppo 33531ae08745Sheppo static void 33541ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 33551ae08745Sheppo { 33561ae08745Sheppo uint64_t id = 0; 33571ae08745Sheppo 33581ae08745Sheppo 33591ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 33601ae08745Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node", 33611ae08745Sheppo VD_ID_PROP); 33621ae08745Sheppo return; 33631ae08745Sheppo } 33641ae08745Sheppo PR0("Removing vdisk ID %lu", id); 33651ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 33661ae08745Sheppo PRN("No vdisk entry found for vdisk ID %lu", id); 33671ae08745Sheppo } 33681ae08745Sheppo 33691ae08745Sheppo static void 33701ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 33711ae08745Sheppo md_t *curr_md, mde_cookie_t curr_vd_node) 33721ae08745Sheppo { 33731ae08745Sheppo char *curr_dev, *prev_dev; 33741ae08745Sheppo uint64_t curr_id = 0, curr_ldc_id = 0; 33751ae08745Sheppo uint64_t prev_id = 0, prev_ldc_id = 0; 33761ae08745Sheppo size_t len; 33771ae08745Sheppo 33781ae08745Sheppo 33791ae08745Sheppo /* Validate that vdisk ID has not changed */ 33801ae08745Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 33811ae08745Sheppo PRN("Error getting previous vdisk \"%s\" property", 33821ae08745Sheppo VD_ID_PROP); 33831ae08745Sheppo return; 33841ae08745Sheppo } 33851ae08745Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 33861ae08745Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 33871ae08745Sheppo return; 33881ae08745Sheppo } 33891ae08745Sheppo if (curr_id != prev_id) { 33901ae08745Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu", 33911ae08745Sheppo prev_id, curr_id); 33921ae08745Sheppo return; 33931ae08745Sheppo } 33941ae08745Sheppo 33951ae08745Sheppo /* Validate that LDC ID has not changed */ 33961ae08745Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 33971ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id); 33981ae08745Sheppo return; 33991ae08745Sheppo } 34001ae08745Sheppo 34011ae08745Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 34021ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id); 34031ae08745Sheppo return; 34041ae08745Sheppo } 34051ae08745Sheppo if (curr_ldc_id != prev_ldc_id) { 34060a55fbb7Slm66018 _NOTE(NOTREACHED); /* lint is confused */ 34071ae08745Sheppo PRN("Not changing vdisk: " 34081ae08745Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 34091ae08745Sheppo return; 34101ae08745Sheppo } 34111ae08745Sheppo 34121ae08745Sheppo /* Determine whether device path has changed */ 34131ae08745Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 34141ae08745Sheppo &prev_dev) != 0) { 34151ae08745Sheppo PRN("Error getting previous vdisk \"%s\"", 34161ae08745Sheppo VD_BLOCK_DEVICE_PROP); 34171ae08745Sheppo return; 34181ae08745Sheppo } 34191ae08745Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 34201ae08745Sheppo &curr_dev) != 0) { 34211ae08745Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 34221ae08745Sheppo return; 34231ae08745Sheppo } 34241ae08745Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 34251ae08745Sheppo (strncmp(curr_dev, prev_dev, len) == 0)) 34261ae08745Sheppo return; /* no relevant (supported) change */ 34271ae08745Sheppo 34281ae08745Sheppo PR0("Changing vdisk ID %lu", prev_id); 34293af08d82Slm66018 34301ae08745Sheppo /* Remove old state, which will close vdisk and reset */ 34311ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 34321ae08745Sheppo PRN("No entry found for vdisk ID %lu", prev_id); 34333af08d82Slm66018 34341ae08745Sheppo /* Re-initialize vdisk with new state */ 34351ae08745Sheppo if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) { 34361ae08745Sheppo PRN("Failed to change vdisk ID %lu", curr_id); 34371ae08745Sheppo return; 34381ae08745Sheppo } 34391ae08745Sheppo } 34401ae08745Sheppo 34411ae08745Sheppo static int 34421ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md) 34431ae08745Sheppo { 34441ae08745Sheppo int i; 34451ae08745Sheppo vds_t *vds = arg; 34461ae08745Sheppo 34471ae08745Sheppo 34481ae08745Sheppo if (md == NULL) 34491ae08745Sheppo return (MDEG_FAILURE); 34501ae08745Sheppo ASSERT(vds != NULL); 34511ae08745Sheppo 34521ae08745Sheppo for (i = 0; i < md->removed.nelem; i++) 34531ae08745Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 34541ae08745Sheppo for (i = 0; i < md->match_curr.nelem; i++) 34551ae08745Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 34561ae08745Sheppo md->match_curr.mdp, md->match_curr.mdep[i]); 34571ae08745Sheppo for (i = 0; i < md->added.nelem; i++) 34581ae08745Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 34591ae08745Sheppo 34601ae08745Sheppo return (MDEG_SUCCESS); 34611ae08745Sheppo } 34621ae08745Sheppo 34633c96341aSnarayan 34641ae08745Sheppo static int 34651ae08745Sheppo vds_do_attach(dev_info_t *dip) 34661ae08745Sheppo { 3467445b4c2eSsb155480 int status, sz; 3468445b4c2eSsb155480 int cfg_handle; 34691ae08745Sheppo minor_t instance = ddi_get_instance(dip); 34701ae08745Sheppo vds_t *vds; 3471445b4c2eSsb155480 mdeg_prop_spec_t *pspecp; 3472445b4c2eSsb155480 mdeg_node_spec_t *ispecp; 34731ae08745Sheppo 34741ae08745Sheppo /* 34751ae08745Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's 34761ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 34771ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 34781ae08745Sheppo * the "reg" property on the node in the device tree it builds from 34791ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 34801ae08745Sheppo * "reg" property value to uniquely identify this device instance when 34811ae08745Sheppo * registering with the MD event-generation framework. If the "reg" 34821ae08745Sheppo * property cannot be found, the device tree state is presumably so 34831ae08745Sheppo * broken that there is no point in continuing. 34841ae08745Sheppo */ 3485445b4c2eSsb155480 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 3486445b4c2eSsb155480 VD_REG_PROP)) { 3487445b4c2eSsb155480 PRN("vds \"%s\" property does not exist", VD_REG_PROP); 34881ae08745Sheppo return (DDI_FAILURE); 34891ae08745Sheppo } 34901ae08745Sheppo 34911ae08745Sheppo /* Get the MD instance for later MDEG registration */ 34921ae08745Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 3493445b4c2eSsb155480 VD_REG_PROP, -1); 34941ae08745Sheppo 34951ae08745Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 34961ae08745Sheppo PRN("Could not allocate state for instance %u", instance); 34971ae08745Sheppo return (DDI_FAILURE); 34981ae08745Sheppo } 34991ae08745Sheppo 35001ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 35011ae08745Sheppo PRN("Could not get state for instance %u", instance); 35021ae08745Sheppo ddi_soft_state_free(vds_state, instance); 35031ae08745Sheppo return (DDI_FAILURE); 35041ae08745Sheppo } 35051ae08745Sheppo 35063c96341aSnarayan 35071ae08745Sheppo vds->dip = dip; 35081ae08745Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 35091ae08745Sheppo vds_destroy_vd, 35101ae08745Sheppo sizeof (void *)); 35111ae08745Sheppo ASSERT(vds->vd_table != NULL); 35121ae08745Sheppo 35131ae08745Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 35141ae08745Sheppo PRN("ldi_ident_from_dip() returned errno %d", status); 35151ae08745Sheppo return (DDI_FAILURE); 35161ae08745Sheppo } 35171ae08745Sheppo vds->initialized |= VDS_LDI; 35181ae08745Sheppo 35191ae08745Sheppo /* Register for MD updates */ 3520445b4c2eSsb155480 sz = sizeof (vds_prop_template); 3521445b4c2eSsb155480 pspecp = kmem_alloc(sz, KM_SLEEP); 3522445b4c2eSsb155480 bcopy(vds_prop_template, pspecp, sz); 3523445b4c2eSsb155480 3524445b4c2eSsb155480 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle); 3525445b4c2eSsb155480 3526445b4c2eSsb155480 /* initialize the complete prop spec structure */ 3527445b4c2eSsb155480 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP); 3528445b4c2eSsb155480 ispecp->namep = "virtual-device"; 3529445b4c2eSsb155480 ispecp->specp = pspecp; 3530445b4c2eSsb155480 3531445b4c2eSsb155480 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds, 35321ae08745Sheppo &vds->mdeg) != MDEG_SUCCESS) { 35331ae08745Sheppo PRN("Unable to register for MD updates"); 3534445b4c2eSsb155480 kmem_free(ispecp, sizeof (mdeg_node_spec_t)); 3535445b4c2eSsb155480 kmem_free(pspecp, sz); 35361ae08745Sheppo return (DDI_FAILURE); 35371ae08745Sheppo } 3538445b4c2eSsb155480 3539445b4c2eSsb155480 vds->ispecp = ispecp; 35401ae08745Sheppo vds->initialized |= VDS_MDEG; 35411ae08745Sheppo 35420a55fbb7Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */ 35430a55fbb7Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 35440a55fbb7Slm66018 DDI_PROP_SUCCESS) { 35450a55fbb7Slm66018 PRN("failed to set \"%s\" property for instance %u", 35460a55fbb7Slm66018 DDI_NO_AUTODETACH, instance); 35470a55fbb7Slm66018 } 35480a55fbb7Slm66018 35491ae08745Sheppo ddi_report_dev(dip); 35501ae08745Sheppo return (DDI_SUCCESS); 35511ae08745Sheppo } 35521ae08745Sheppo 35531ae08745Sheppo static int 35541ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 35551ae08745Sheppo { 35561ae08745Sheppo int status; 35571ae08745Sheppo 35581ae08745Sheppo switch (cmd) { 35591ae08745Sheppo case DDI_ATTACH: 3560d10e4ef2Snarayan PR0("Attaching"); 35611ae08745Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 35621ae08745Sheppo (void) vds_detach(dip, DDI_DETACH); 35631ae08745Sheppo return (status); 35641ae08745Sheppo case DDI_RESUME: 3565d10e4ef2Snarayan PR0("No action required for DDI_RESUME"); 35661ae08745Sheppo return (DDI_SUCCESS); 35671ae08745Sheppo default: 35681ae08745Sheppo return (DDI_FAILURE); 35691ae08745Sheppo } 35701ae08745Sheppo } 35711ae08745Sheppo 35721ae08745Sheppo static struct dev_ops vds_ops = { 35731ae08745Sheppo DEVO_REV, /* devo_rev */ 35741ae08745Sheppo 0, /* devo_refcnt */ 35751ae08745Sheppo ddi_no_info, /* devo_getinfo */ 35761ae08745Sheppo nulldev, /* devo_identify */ 35771ae08745Sheppo nulldev, /* devo_probe */ 35781ae08745Sheppo vds_attach, /* devo_attach */ 35791ae08745Sheppo vds_detach, /* devo_detach */ 35801ae08745Sheppo nodev, /* devo_reset */ 35811ae08745Sheppo NULL, /* devo_cb_ops */ 35821ae08745Sheppo NULL, /* devo_bus_ops */ 35831ae08745Sheppo nulldev /* devo_power */ 35841ae08745Sheppo }; 35851ae08745Sheppo 35861ae08745Sheppo static struct modldrv modldrv = { 35871ae08745Sheppo &mod_driverops, 35881ae08745Sheppo "virtual disk server v%I%", 35891ae08745Sheppo &vds_ops, 35901ae08745Sheppo }; 35911ae08745Sheppo 35921ae08745Sheppo static struct modlinkage modlinkage = { 35931ae08745Sheppo MODREV_1, 35941ae08745Sheppo &modldrv, 35951ae08745Sheppo NULL 35961ae08745Sheppo }; 35971ae08745Sheppo 35981ae08745Sheppo 35991ae08745Sheppo int 36001ae08745Sheppo _init(void) 36011ae08745Sheppo { 36021ae08745Sheppo int i, status; 36031ae08745Sheppo 3604d10e4ef2Snarayan 36051ae08745Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 36061ae08745Sheppo return (status); 36071ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) { 36081ae08745Sheppo ddi_soft_state_fini(&vds_state); 36091ae08745Sheppo return (status); 36101ae08745Sheppo } 36111ae08745Sheppo 36121ae08745Sheppo /* Fill in the bit-mask of server-supported operations */ 36131ae08745Sheppo for (i = 0; i < vds_noperations; i++) 36141ae08745Sheppo vds_operations |= 1 << (vds_operation[i].operation - 1); 36151ae08745Sheppo 36161ae08745Sheppo return (0); 36171ae08745Sheppo } 36181ae08745Sheppo 36191ae08745Sheppo int 36201ae08745Sheppo _info(struct modinfo *modinfop) 36211ae08745Sheppo { 36221ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 36231ae08745Sheppo } 36241ae08745Sheppo 36251ae08745Sheppo int 36261ae08745Sheppo _fini(void) 36271ae08745Sheppo { 36281ae08745Sheppo int status; 36291ae08745Sheppo 3630d10e4ef2Snarayan 36311ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 36321ae08745Sheppo return (status); 36331ae08745Sheppo ddi_soft_state_fini(&vds_state); 36341ae08745Sheppo return (0); 36351ae08745Sheppo } 3636