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 /* 231ae08745Sheppo * Copyright 2006 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> 511ae08745Sheppo 521ae08745Sheppo 531ae08745Sheppo /* Virtual disk server initialization flags */ 54d10e4ef2Snarayan #define VDS_LDI 0x01 55d10e4ef2Snarayan #define VDS_MDEG 0x02 561ae08745Sheppo 571ae08745Sheppo /* Virtual disk server tunable parameters */ 581ae08745Sheppo #define VDS_LDC_RETRIES 3 59*3af08d82Slm66018 #define VDS_LDC_DELAY 1000 /* usec */ 601ae08745Sheppo #define VDS_NCHAINS 32 611ae08745Sheppo 621ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */ 631ae08745Sheppo #define VDS_NAME "virtual-disk-server" 641ae08745Sheppo 651ae08745Sheppo #define VD_NAME "vd" 661ae08745Sheppo #define VD_VOLUME_NAME "vdisk" 671ae08745Sheppo #define VD_ASCIILABEL "Virtual Disk" 681ae08745Sheppo 691ae08745Sheppo #define VD_CHANNEL_ENDPOINT "channel-endpoint" 701ae08745Sheppo #define VD_ID_PROP "id" 711ae08745Sheppo #define VD_BLOCK_DEVICE_PROP "vds-block-device" 721ae08745Sheppo 731ae08745Sheppo /* Virtual disk initialization flags */ 741ae08745Sheppo #define VD_LOCKING 0x01 75d10e4ef2Snarayan #define VD_LDC 0x02 76d10e4ef2Snarayan #define VD_DRING 0x04 77d10e4ef2Snarayan #define VD_SID 0x08 78d10e4ef2Snarayan #define VD_SEQ_NUM 0x10 791ae08745Sheppo 801ae08745Sheppo /* Flags for opening/closing backing devices via LDI */ 811ae08745Sheppo #define VD_OPEN_FLAGS (FEXCL | FREAD | FWRITE) 821ae08745Sheppo 831ae08745Sheppo /* 841ae08745Sheppo * By Solaris convention, slice/partition 2 represents the entire disk; 851ae08745Sheppo * unfortunately, this convention does not appear to be codified. 861ae08745Sheppo */ 871ae08745Sheppo #define VD_ENTIRE_DISK_SLICE 2 881ae08745Sheppo 891ae08745Sheppo /* Return a cpp token as a string */ 901ae08745Sheppo #define STRINGIZE(token) #token 911ae08745Sheppo 921ae08745Sheppo /* 931ae08745Sheppo * Print a message prefixed with the current function name to the message log 941ae08745Sheppo * (and optionally to the console for verbose boots); these macros use cpp's 951ae08745Sheppo * concatenation of string literals and C99 variable-length-argument-list 961ae08745Sheppo * macros 971ae08745Sheppo */ 981ae08745Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 991ae08745Sheppo #define _PRN(format, ...) \ 1001ae08745Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 1011ae08745Sheppo 1021ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */ 1031ae08745Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 1041ae08745Sheppo (vd->dring + (i)*vd->descriptor_size)) 1051ae08745Sheppo 1061ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */ 1071ae08745Sheppo #define VD_CLIENT(vd) \ 1081ae08745Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 1091ae08745Sheppo (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" : \ 1101ae08745Sheppo (((vd)->xfer_mode == 0) ? "null client" : \ 1111ae08745Sheppo "unsupported client"))) 1121ae08745Sheppo 1131ae08745Sheppo /* Debugging macros */ 1141ae08745Sheppo #ifdef DEBUG 115*3af08d82Slm66018 116*3af08d82Slm66018 static int vd_msglevel = 0; 117*3af08d82Slm66018 118*3af08d82Slm66018 1191ae08745Sheppo #define PR0 if (vd_msglevel > 0) PRN 1201ae08745Sheppo #define PR1 if (vd_msglevel > 1) PRN 1211ae08745Sheppo #define PR2 if (vd_msglevel > 2) PRN 1221ae08745Sheppo 1231ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) \ 1241ae08745Sheppo PRN("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 1251ae08745Sheppo elem->hdr.dstate, \ 1261ae08745Sheppo elem->payload.operation, \ 1271ae08745Sheppo elem->payload.status, \ 1281ae08745Sheppo elem->payload.nbytes, \ 1291ae08745Sheppo elem->payload.addr, \ 1301ae08745Sheppo elem->payload.ncookies); 1311ae08745Sheppo 132*3af08d82Slm66018 char * 133*3af08d82Slm66018 vd_decode_state(int state) 134*3af08d82Slm66018 { 135*3af08d82Slm66018 char *str; 136*3af08d82Slm66018 137*3af08d82Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break; 138*3af08d82Slm66018 139*3af08d82Slm66018 switch (state) { 140*3af08d82Slm66018 CASE_STATE(VD_STATE_INIT) 141*3af08d82Slm66018 CASE_STATE(VD_STATE_VER) 142*3af08d82Slm66018 CASE_STATE(VD_STATE_ATTR) 143*3af08d82Slm66018 CASE_STATE(VD_STATE_DRING) 144*3af08d82Slm66018 CASE_STATE(VD_STATE_RDX) 145*3af08d82Slm66018 CASE_STATE(VD_STATE_DATA) 146*3af08d82Slm66018 default: str = "unknown"; break; 147*3af08d82Slm66018 } 148*3af08d82Slm66018 149*3af08d82Slm66018 #undef CASE_STATE 150*3af08d82Slm66018 151*3af08d82Slm66018 return (str); 152*3af08d82Slm66018 } 153*3af08d82Slm66018 154*3af08d82Slm66018 void 155*3af08d82Slm66018 vd_decode_tag(vio_msg_t *msg) 156*3af08d82Slm66018 { 157*3af08d82Slm66018 char *tstr, *sstr, *estr; 158*3af08d82Slm66018 159*3af08d82Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 160*3af08d82Slm66018 161*3af08d82Slm66018 switch (msg->tag.vio_msgtype) { 162*3af08d82Slm66018 CASE_TYPE(VIO_TYPE_CTRL) 163*3af08d82Slm66018 CASE_TYPE(VIO_TYPE_DATA) 164*3af08d82Slm66018 CASE_TYPE(VIO_TYPE_ERR) 165*3af08d82Slm66018 default: tstr = "unknown"; break; 166*3af08d82Slm66018 } 167*3af08d82Slm66018 168*3af08d82Slm66018 #undef CASE_TYPE 169*3af08d82Slm66018 170*3af08d82Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 171*3af08d82Slm66018 172*3af08d82Slm66018 switch (msg->tag.vio_subtype) { 173*3af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 174*3af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 175*3af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 176*3af08d82Slm66018 default: sstr = "unknown"; break; 177*3af08d82Slm66018 } 178*3af08d82Slm66018 179*3af08d82Slm66018 #undef CASE_SUBTYPE 180*3af08d82Slm66018 181*3af08d82Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break; 182*3af08d82Slm66018 183*3af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 184*3af08d82Slm66018 CASE_ENV(VIO_VER_INFO) 185*3af08d82Slm66018 CASE_ENV(VIO_ATTR_INFO) 186*3af08d82Slm66018 CASE_ENV(VIO_DRING_REG) 187*3af08d82Slm66018 CASE_ENV(VIO_DRING_UNREG) 188*3af08d82Slm66018 CASE_ENV(VIO_RDX) 189*3af08d82Slm66018 CASE_ENV(VIO_PKT_DATA) 190*3af08d82Slm66018 CASE_ENV(VIO_DESC_DATA) 191*3af08d82Slm66018 CASE_ENV(VIO_DRING_DATA) 192*3af08d82Slm66018 default: estr = "unknown"; break; 193*3af08d82Slm66018 } 194*3af08d82Slm66018 195*3af08d82Slm66018 #undef CASE_ENV 196*3af08d82Slm66018 197*3af08d82Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)", 198*3af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 199*3af08d82Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr); 200*3af08d82Slm66018 } 201*3af08d82Slm66018 2021ae08745Sheppo #else /* !DEBUG */ 203*3af08d82Slm66018 2041ae08745Sheppo #define PR0(...) 2051ae08745Sheppo #define PR1(...) 2061ae08745Sheppo #define PR2(...) 2071ae08745Sheppo 2081ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) 2091ae08745Sheppo 210*3af08d82Slm66018 #define vd_decode_state(_s) (NULL) 211*3af08d82Slm66018 #define vd_decode_tag(_s) (NULL) 212*3af08d82Slm66018 2131ae08745Sheppo #endif /* DEBUG */ 2141ae08745Sheppo 2151ae08745Sheppo 216d10e4ef2Snarayan /* 217d10e4ef2Snarayan * Soft state structure for a vds instance 218d10e4ef2Snarayan */ 2191ae08745Sheppo typedef struct vds { 2201ae08745Sheppo uint_t initialized; /* driver inst initialization flags */ 2211ae08745Sheppo dev_info_t *dip; /* driver inst devinfo pointer */ 2221ae08745Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 2231ae08745Sheppo mod_hash_t *vd_table; /* table of virtual disks served */ 2241ae08745Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */ 2251ae08745Sheppo } vds_t; 2261ae08745Sheppo 227d10e4ef2Snarayan /* 228d10e4ef2Snarayan * Types of descriptor-processing tasks 229d10e4ef2Snarayan */ 230d10e4ef2Snarayan typedef enum vd_task_type { 231d10e4ef2Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 232d10e4ef2Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 233d10e4ef2Snarayan } vd_task_type_t; 234d10e4ef2Snarayan 235d10e4ef2Snarayan /* 236d10e4ef2Snarayan * Structure describing the task for processing a descriptor 237d10e4ef2Snarayan */ 238d10e4ef2Snarayan typedef struct vd_task { 239d10e4ef2Snarayan struct vd *vd; /* vd instance task is for */ 240d10e4ef2Snarayan vd_task_type_t type; /* type of descriptor task */ 241d10e4ef2Snarayan int index; /* dring elem index for task */ 242d10e4ef2Snarayan vio_msg_t *msg; /* VIO message task is for */ 243d10e4ef2Snarayan size_t msglen; /* length of message content */ 244d10e4ef2Snarayan vd_dring_payload_t *request; /* request task will perform */ 245d10e4ef2Snarayan struct buf buf; /* buf(9s) for I/O request */ 2464bac2208Snarayan ldc_mem_handle_t mhdl; /* task memory handle */ 247d10e4ef2Snarayan } vd_task_t; 248d10e4ef2Snarayan 249d10e4ef2Snarayan /* 250d10e4ef2Snarayan * Soft state structure for a virtual disk instance 251d10e4ef2Snarayan */ 2521ae08745Sheppo typedef struct vd { 2531ae08745Sheppo uint_t initialized; /* vdisk initialization flags */ 2541ae08745Sheppo vds_t *vds; /* server for this vdisk */ 255d10e4ef2Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */ 256d10e4ef2Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */ 2571ae08745Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 2581ae08745Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 259e1ebb9ecSlm66018 uint_t nslices; /* number of slices */ 2601ae08745Sheppo size_t vdisk_size; /* number of blocks in vdisk */ 2611ae08745Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */ 2624bac2208Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 263e1ebb9ecSlm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 2641ae08745Sheppo boolean_t pseudo; /* underlying pseudo dev */ 2654bac2208Snarayan struct dk_efi dk_efi; /* synthetic for slice type */ 2661ae08745Sheppo struct dk_geom dk_geom; /* synthetic for slice type */ 2671ae08745Sheppo struct vtoc vtoc; /* synthetic for slice type */ 2681ae08745Sheppo ldc_status_t ldc_state; /* LDC connection state */ 2691ae08745Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */ 2701ae08745Sheppo size_t max_msglen; /* largest LDC message len */ 2711ae08745Sheppo vd_state_t state; /* client handshake state */ 2721ae08745Sheppo uint8_t xfer_mode; /* transfer mode with client */ 2731ae08745Sheppo uint32_t sid; /* client's session ID */ 2741ae08745Sheppo uint64_t seq_num; /* message sequence number */ 2751ae08745Sheppo uint64_t dring_ident; /* identifier of dring */ 2761ae08745Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */ 2771ae08745Sheppo uint32_t descriptor_size; /* num bytes in desc */ 2781ae08745Sheppo uint32_t dring_len; /* number of dring elements */ 2791ae08745Sheppo caddr_t dring; /* address of dring */ 280*3af08d82Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */ 281d10e4ef2Snarayan vd_task_t inband_task; /* task for inband descriptor */ 282d10e4ef2Snarayan vd_task_t *dring_task; /* tasks dring elements */ 283d10e4ef2Snarayan 284d10e4ef2Snarayan kmutex_t lock; /* protects variables below */ 285d10e4ef2Snarayan boolean_t enabled; /* is vdisk enabled? */ 286d10e4ef2Snarayan boolean_t reset_state; /* reset connection state? */ 287d10e4ef2Snarayan boolean_t reset_ldc; /* reset LDC channel? */ 2881ae08745Sheppo } vd_t; 2891ae08745Sheppo 2901ae08745Sheppo typedef struct vds_operation { 291*3af08d82Slm66018 char *namep; 2921ae08745Sheppo uint8_t operation; 293d10e4ef2Snarayan int (*start)(vd_task_t *task); 294d10e4ef2Snarayan void (*complete)(void *arg); 2951ae08745Sheppo } vds_operation_t; 2961ae08745Sheppo 2970a55fbb7Slm66018 typedef struct vd_ioctl { 2980a55fbb7Slm66018 uint8_t operation; /* vdisk operation */ 2990a55fbb7Slm66018 const char *operation_name; /* vdisk operation name */ 3000a55fbb7Slm66018 size_t nbytes; /* size of operation buffer */ 3010a55fbb7Slm66018 int cmd; /* corresponding ioctl cmd */ 3020a55fbb7Slm66018 const char *cmd_name; /* ioctl cmd name */ 3030a55fbb7Slm66018 void *arg; /* ioctl cmd argument */ 3040a55fbb7Slm66018 /* convert input vd_buf to output ioctl_arg */ 3050a55fbb7Slm66018 void (*copyin)(void *vd_buf, void *ioctl_arg); 3060a55fbb7Slm66018 /* convert input ioctl_arg to output vd_buf */ 3070a55fbb7Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf); 3080a55fbb7Slm66018 } vd_ioctl_t; 3090a55fbb7Slm66018 3100a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */ 3110a55fbb7Slm66018 #define VD_IDENTITY ((void (*)(void *, void *))-1) 3121ae08745Sheppo 3131ae08745Sheppo 3141ae08745Sheppo static int vds_ldc_retries = VDS_LDC_RETRIES; 315*3af08d82Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY; 3161ae08745Sheppo static void *vds_state; 3171ae08745Sheppo static uint64_t vds_operations; /* see vds_operation[] definition below */ 3181ae08745Sheppo 3191ae08745Sheppo static int vd_open_flags = VD_OPEN_FLAGS; 3201ae08745Sheppo 3210a55fbb7Slm66018 /* 3220a55fbb7Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 3230a55fbb7Slm66018 * 3240a55fbb7Slm66018 * Each supported major version should appear only once, paired with (and only 3250a55fbb7Slm66018 * with) its highest supported minor version number (as the protocol requires 3260a55fbb7Slm66018 * supporting all lower minor version numbers as well) 3270a55fbb7Slm66018 */ 3280a55fbb7Slm66018 static const vio_ver_t vds_version[] = {{1, 0}}; 3290a55fbb7Slm66018 static const size_t vds_num_versions = 3300a55fbb7Slm66018 sizeof (vds_version)/sizeof (vds_version[0]); 3310a55fbb7Slm66018 332*3af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp); 3331ae08745Sheppo 3341ae08745Sheppo static int 335d10e4ef2Snarayan vd_start_bio(vd_task_t *task) 3361ae08745Sheppo { 3374bac2208Snarayan int rv, status = 0; 338d10e4ef2Snarayan vd_t *vd = task->vd; 339d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 340d10e4ef2Snarayan struct buf *buf = &task->buf; 3414bac2208Snarayan uint8_t mtype; 3421ae08745Sheppo 343d10e4ef2Snarayan 344d10e4ef2Snarayan ASSERT(vd != NULL); 345d10e4ef2Snarayan ASSERT(request != NULL); 346d10e4ef2Snarayan ASSERT(request->slice < vd->nslices); 347d10e4ef2Snarayan ASSERT((request->operation == VD_OP_BREAD) || 348d10e4ef2Snarayan (request->operation == VD_OP_BWRITE)); 349d10e4ef2Snarayan 3501ae08745Sheppo if (request->nbytes == 0) 3511ae08745Sheppo return (EINVAL); /* no service for trivial requests */ 3521ae08745Sheppo 353d10e4ef2Snarayan PR1("%s %lu bytes at block %lu", 354d10e4ef2Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write", 355d10e4ef2Snarayan request->nbytes, request->addr); 3561ae08745Sheppo 357d10e4ef2Snarayan bioinit(buf); 358d10e4ef2Snarayan buf->b_flags = B_BUSY; 359d10e4ef2Snarayan buf->b_bcount = request->nbytes; 360d10e4ef2Snarayan buf->b_lblkno = request->addr; 361d10e4ef2Snarayan buf->b_edev = vd->dev[request->slice]; 362d10e4ef2Snarayan 3634bac2208Snarayan mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 3644bac2208Snarayan 3654bac2208Snarayan /* Map memory exported by client */ 3664bac2208Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 3674bac2208Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 3684bac2208Snarayan &(buf->b_un.b_addr), NULL); 3694bac2208Snarayan if (status != 0) { 370*3af08d82Slm66018 PR0("ldc_mem_map() returned err %d ", status); 3714bac2208Snarayan biofini(buf); 3724bac2208Snarayan return (status); 373d10e4ef2Snarayan } 374d10e4ef2Snarayan 3754bac2208Snarayan status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount); 3764bac2208Snarayan if (status != 0) { 3774bac2208Snarayan (void) ldc_mem_unmap(task->mhdl); 378*3af08d82Slm66018 PR0("ldc_mem_acquire() returned err %d ", status); 3794bac2208Snarayan biofini(buf); 3804bac2208Snarayan return (status); 3814bac2208Snarayan } 3824bac2208Snarayan 3834bac2208Snarayan buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE; 3844bac2208Snarayan 385d10e4ef2Snarayan /* Start the block I/O */ 3864bac2208Snarayan if ((status = ldi_strategy(vd->ldi_handle[request->slice], buf)) == 0) 387d10e4ef2Snarayan return (EINPROGRESS); /* will complete on completionq */ 388d10e4ef2Snarayan 389d10e4ef2Snarayan /* Clean up after error */ 3904bac2208Snarayan rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount); 3914bac2208Snarayan if (rv) { 392*3af08d82Slm66018 PR0("ldc_mem_release() returned err %d ", rv); 3934bac2208Snarayan } 3944bac2208Snarayan rv = ldc_mem_unmap(task->mhdl); 3954bac2208Snarayan if (rv) { 396*3af08d82Slm66018 PR0("ldc_mem_unmap() returned err %d ", status); 3974bac2208Snarayan } 3984bac2208Snarayan 399d10e4ef2Snarayan biofini(buf); 400d10e4ef2Snarayan return (status); 401d10e4ef2Snarayan } 402d10e4ef2Snarayan 403d10e4ef2Snarayan static int 404d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 405d10e4ef2Snarayan { 406*3af08d82Slm66018 int status; 407d10e4ef2Snarayan size_t nbytes; 408d10e4ef2Snarayan 409*3af08d82Slm66018 do { 410d10e4ef2Snarayan nbytes = msglen; 411d10e4ef2Snarayan status = ldc_write(ldc_handle, msg, &nbytes); 412*3af08d82Slm66018 if (status != EWOULDBLOCK) 413*3af08d82Slm66018 break; 414*3af08d82Slm66018 drv_usecwait(vds_ldc_delay); 415*3af08d82Slm66018 } while (status == EWOULDBLOCK); 416d10e4ef2Snarayan 417d10e4ef2Snarayan if (status != 0) { 418*3af08d82Slm66018 if (status != ECONNRESET) 419*3af08d82Slm66018 PR0("ldc_write() returned errno %d", status); 420d10e4ef2Snarayan return (status); 421d10e4ef2Snarayan } else if (nbytes != msglen) { 422*3af08d82Slm66018 PR0("ldc_write() performed only partial write"); 423d10e4ef2Snarayan return (EIO); 424d10e4ef2Snarayan } 425d10e4ef2Snarayan 426d10e4ef2Snarayan PR1("SENT %lu bytes", msglen); 427d10e4ef2Snarayan return (0); 428d10e4ef2Snarayan } 429d10e4ef2Snarayan 430d10e4ef2Snarayan static void 431d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc) 432d10e4ef2Snarayan { 433d10e4ef2Snarayan mutex_enter(&vd->lock); 434d10e4ef2Snarayan vd->reset_state = B_TRUE; 435d10e4ef2Snarayan vd->reset_ldc = reset_ldc; 436d10e4ef2Snarayan mutex_exit(&vd->lock); 437d10e4ef2Snarayan } 438d10e4ef2Snarayan 439d10e4ef2Snarayan /* 440d10e4ef2Snarayan * Reset the state of the connection with a client, if needed; reset the LDC 441d10e4ef2Snarayan * transport as well, if needed. This function should only be called from the 442*3af08d82Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 443d10e4ef2Snarayan */ 444d10e4ef2Snarayan static void 445d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd) 446d10e4ef2Snarayan { 447d10e4ef2Snarayan int status = 0; 448d10e4ef2Snarayan 449d10e4ef2Snarayan mutex_enter(&vd->lock); 450d10e4ef2Snarayan if (!vd->reset_state) { 451d10e4ef2Snarayan ASSERT(!vd->reset_ldc); 452d10e4ef2Snarayan mutex_exit(&vd->lock); 453d10e4ef2Snarayan return; 454d10e4ef2Snarayan } 455d10e4ef2Snarayan mutex_exit(&vd->lock); 456d10e4ef2Snarayan 457d10e4ef2Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd)); 458d10e4ef2Snarayan 459d10e4ef2Snarayan /* 460d10e4ef2Snarayan * Let any asynchronous I/O complete before possibly pulling the rug 461d10e4ef2Snarayan * out from under it; defer checking vd->reset_ldc, as one of the 462d10e4ef2Snarayan * asynchronous tasks might set it 463d10e4ef2Snarayan */ 464d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 465d10e4ef2Snarayan 466d10e4ef2Snarayan if ((vd->initialized & VD_DRING) && 467d10e4ef2Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 468*3af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 469d10e4ef2Snarayan 470*3af08d82Slm66018 vd_free_dring_task(vd); 471*3af08d82Slm66018 472*3af08d82Slm66018 /* Free the staging buffer for msgs */ 473*3af08d82Slm66018 if (vd->vio_msgp != NULL) { 474*3af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 475*3af08d82Slm66018 vd->vio_msgp = NULL; 476d10e4ef2Snarayan } 477d10e4ef2Snarayan 478*3af08d82Slm66018 /* Free the inband message buffer */ 479*3af08d82Slm66018 if (vd->inband_task.msg != NULL) { 480*3af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 481*3af08d82Slm66018 vd->inband_task.msg = NULL; 482*3af08d82Slm66018 } 483d10e4ef2Snarayan 484d10e4ef2Snarayan mutex_enter(&vd->lock); 485*3af08d82Slm66018 486*3af08d82Slm66018 if (vd->reset_ldc) 487*3af08d82Slm66018 PR0("taking down LDC channel"); 488e1ebb9ecSlm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 489*3af08d82Slm66018 PR0("ldc_down() returned errno %d", status); 490d10e4ef2Snarayan 491d10e4ef2Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 492d10e4ef2Snarayan vd->state = VD_STATE_INIT; 493d10e4ef2Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 494d10e4ef2Snarayan 495*3af08d82Slm66018 /* Allocate the staging buffer */ 496*3af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 497*3af08d82Slm66018 498*3af08d82Slm66018 status = ldc_status(vd->ldc_handle, &vd->ldc_state); 499*3af08d82Slm66018 if (vd->reset_ldc && vd->ldc_state != LDC_UP) { 500*3af08d82Slm66018 PR0("calling ldc_up\n"); 501*3af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 502*3af08d82Slm66018 } 503*3af08d82Slm66018 504d10e4ef2Snarayan vd->reset_state = B_FALSE; 505d10e4ef2Snarayan vd->reset_ldc = B_FALSE; 506*3af08d82Slm66018 507d10e4ef2Snarayan mutex_exit(&vd->lock); 508d10e4ef2Snarayan } 509d10e4ef2Snarayan 510*3af08d82Slm66018 static void vd_recv_msg(void *arg); 511*3af08d82Slm66018 512*3af08d82Slm66018 static void 513*3af08d82Slm66018 vd_mark_in_reset(vd_t *vd) 514*3af08d82Slm66018 { 515*3af08d82Slm66018 int status; 516*3af08d82Slm66018 517*3af08d82Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n"); 518*3af08d82Slm66018 519*3af08d82Slm66018 vd_need_reset(vd, B_FALSE); 520*3af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 521*3af08d82Slm66018 if (status == DDI_FAILURE) { 522*3af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 523*3af08d82Slm66018 vd_need_reset(vd, B_TRUE); 524*3af08d82Slm66018 return; 525*3af08d82Slm66018 } 526*3af08d82Slm66018 } 527*3af08d82Slm66018 528d10e4ef2Snarayan static int 529d10e4ef2Snarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status) 530d10e4ef2Snarayan { 531d10e4ef2Snarayan boolean_t accepted; 532d10e4ef2Snarayan int status; 533d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 534d10e4ef2Snarayan 535*3af08d82Slm66018 if (vd->reset_state) 536*3af08d82Slm66018 return (0); 537d10e4ef2Snarayan 538d10e4ef2Snarayan /* Acquire the element */ 539*3af08d82Slm66018 if (!vd->reset_state && 540*3af08d82Slm66018 (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 541*3af08d82Slm66018 if (status == ECONNRESET) { 542*3af08d82Slm66018 vd_mark_in_reset(vd); 543*3af08d82Slm66018 return (0); 544*3af08d82Slm66018 } else { 545*3af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", 546*3af08d82Slm66018 status); 547d10e4ef2Snarayan return (status); 548d10e4ef2Snarayan } 549*3af08d82Slm66018 } 550d10e4ef2Snarayan 551d10e4ef2Snarayan /* Set the element's status and mark it done */ 552d10e4ef2Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 553d10e4ef2Snarayan if (accepted) { 554d10e4ef2Snarayan elem->payload.status = elem_status; 555d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_DONE; 556d10e4ef2Snarayan } else { 557d10e4ef2Snarayan /* Perhaps client timed out waiting for I/O... */ 558*3af08d82Slm66018 PR0("element %u no longer \"accepted\"", idx); 559d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 560d10e4ef2Snarayan } 561d10e4ef2Snarayan /* Release the element */ 562*3af08d82Slm66018 if (!vd->reset_state && 563*3af08d82Slm66018 (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 564*3af08d82Slm66018 if (status == ECONNRESET) { 565*3af08d82Slm66018 vd_mark_in_reset(vd); 566*3af08d82Slm66018 return (0); 567*3af08d82Slm66018 } else { 568*3af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", 569*3af08d82Slm66018 status); 570d10e4ef2Snarayan return (status); 571d10e4ef2Snarayan } 572*3af08d82Slm66018 } 573d10e4ef2Snarayan 574d10e4ef2Snarayan return (accepted ? 0 : EINVAL); 575d10e4ef2Snarayan } 576d10e4ef2Snarayan 577d10e4ef2Snarayan static void 578d10e4ef2Snarayan vd_complete_bio(void *arg) 579d10e4ef2Snarayan { 580d10e4ef2Snarayan int status = 0; 581d10e4ef2Snarayan vd_task_t *task = (vd_task_t *)arg; 582d10e4ef2Snarayan vd_t *vd = task->vd; 583d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 584d10e4ef2Snarayan struct buf *buf = &task->buf; 585d10e4ef2Snarayan 586d10e4ef2Snarayan 587d10e4ef2Snarayan ASSERT(vd != NULL); 588d10e4ef2Snarayan ASSERT(request != NULL); 589d10e4ef2Snarayan ASSERT(task->msg != NULL); 590d10e4ef2Snarayan ASSERT(task->msglen >= sizeof (*task->msg)); 591d10e4ef2Snarayan 592d10e4ef2Snarayan /* Wait for the I/O to complete */ 593d10e4ef2Snarayan request->status = biowait(buf); 594d10e4ef2Snarayan 5954bac2208Snarayan /* Release the buffer */ 596*3af08d82Slm66018 if (!vd->reset_state) 5974bac2208Snarayan status = ldc_mem_release(task->mhdl, 0, buf->b_bcount); 5984bac2208Snarayan if (status) { 599*3af08d82Slm66018 PR0("ldc_mem_release() returned errno %d copying to " 600*3af08d82Slm66018 "client", status); 601*3af08d82Slm66018 if (status == ECONNRESET) { 602*3af08d82Slm66018 vd_mark_in_reset(vd); 603*3af08d82Slm66018 } 6041ae08745Sheppo } 6051ae08745Sheppo 606*3af08d82Slm66018 /* Unmap the memory, even if in reset */ 6074bac2208Snarayan status = ldc_mem_unmap(task->mhdl); 6084bac2208Snarayan if (status) { 609*3af08d82Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client", 6104bac2208Snarayan status); 611*3af08d82Slm66018 if (status == ECONNRESET) { 612*3af08d82Slm66018 vd_mark_in_reset(vd); 613*3af08d82Slm66018 } 6144bac2208Snarayan } 6154bac2208Snarayan 616d10e4ef2Snarayan biofini(buf); 6171ae08745Sheppo 618d10e4ef2Snarayan /* Update the dring element for a dring client */ 619*3af08d82Slm66018 if (!vd->reset_state && (status == 0) && 620*3af08d82Slm66018 (vd->xfer_mode == VIO_DRING_MODE)) { 621d10e4ef2Snarayan status = vd_mark_elem_done(vd, task->index, request->status); 622*3af08d82Slm66018 if (status == ECONNRESET) 623*3af08d82Slm66018 vd_mark_in_reset(vd); 624*3af08d82Slm66018 } 6251ae08745Sheppo 626d10e4ef2Snarayan /* 627d10e4ef2Snarayan * If a transport error occurred, arrange to "nack" the message when 628d10e4ef2Snarayan * the final task in the descriptor element range completes 629d10e4ef2Snarayan */ 630d10e4ef2Snarayan if (status != 0) 631d10e4ef2Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 6321ae08745Sheppo 633d10e4ef2Snarayan /* 634d10e4ef2Snarayan * Only the final task for a range of elements will respond to and 635d10e4ef2Snarayan * free the message 636d10e4ef2Snarayan */ 637*3af08d82Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) { 638d10e4ef2Snarayan return; 639*3af08d82Slm66018 } 6401ae08745Sheppo 641d10e4ef2Snarayan /* 642d10e4ef2Snarayan * Send the "ack" or "nack" back to the client; if sending the message 643d10e4ef2Snarayan * via LDC fails, arrange to reset both the connection state and LDC 644d10e4ef2Snarayan * itself 645d10e4ef2Snarayan */ 646d10e4ef2Snarayan PR1("Sending %s", 647d10e4ef2Snarayan (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 648*3af08d82Slm66018 if (!vd->reset_state) { 649*3af08d82Slm66018 status = send_msg(vd->ldc_handle, task->msg, task->msglen); 650*3af08d82Slm66018 switch (status) { 651*3af08d82Slm66018 case 0: 652*3af08d82Slm66018 break; 653*3af08d82Slm66018 case ECONNRESET: 654*3af08d82Slm66018 vd_mark_in_reset(vd); 655*3af08d82Slm66018 break; 656*3af08d82Slm66018 default: 657*3af08d82Slm66018 PR0("initiating full reset"); 658d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 659*3af08d82Slm66018 break; 660*3af08d82Slm66018 } 661*3af08d82Slm66018 } 6621ae08745Sheppo } 6631ae08745Sheppo 6640a55fbb7Slm66018 static void 6650a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg) 6660a55fbb7Slm66018 { 6670a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 6680a55fbb7Slm66018 } 6690a55fbb7Slm66018 6700a55fbb7Slm66018 static void 6710a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg) 6720a55fbb7Slm66018 { 6730a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg); 6740a55fbb7Slm66018 } 6750a55fbb7Slm66018 6760a55fbb7Slm66018 static void 6770a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 6780a55fbb7Slm66018 { 6790a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 6800a55fbb7Slm66018 } 6810a55fbb7Slm66018 6820a55fbb7Slm66018 static void 6830a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 6840a55fbb7Slm66018 { 6850a55fbb7Slm66018 VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 6860a55fbb7Slm66018 } 6870a55fbb7Slm66018 6884bac2208Snarayan static void 6894bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg) 6904bac2208Snarayan { 6914bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 6924bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 6934bac2208Snarayan 6944bac2208Snarayan dk_efi->dki_lba = vd_efi->lba; 6954bac2208Snarayan dk_efi->dki_length = vd_efi->length; 6964bac2208Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 6974bac2208Snarayan } 6984bac2208Snarayan 6994bac2208Snarayan static void 7004bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf) 7014bac2208Snarayan { 7024bac2208Snarayan int len; 7034bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 7044bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 7054bac2208Snarayan 7064bac2208Snarayan len = vd_efi->length; 7074bac2208Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi); 7084bac2208Snarayan kmem_free(dk_efi->dki_data, len); 7094bac2208Snarayan } 7104bac2208Snarayan 7114bac2208Snarayan static void 7124bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg) 7134bac2208Snarayan { 7144bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 7154bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 7164bac2208Snarayan 7174bac2208Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 7184bac2208Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi); 7194bac2208Snarayan } 7204bac2208Snarayan 7214bac2208Snarayan static void 7224bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf) 7234bac2208Snarayan { 7244bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 7254bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 7264bac2208Snarayan 7274bac2208Snarayan kmem_free(dk_efi->dki_data, vd_efi->length); 7284bac2208Snarayan } 7294bac2208Snarayan 7304bac2208Snarayan static int 7314bac2208Snarayan vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc, vd_disk_label_t *label) 7324bac2208Snarayan { 7334bac2208Snarayan int status, rval; 7344bac2208Snarayan struct dk_gpt *efi; 7354bac2208Snarayan size_t efi_len; 7364bac2208Snarayan 7374bac2208Snarayan *label = VD_DISK_LABEL_UNK; 7384bac2208Snarayan 7394bac2208Snarayan status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc, 7404bac2208Snarayan (vd_open_flags | FKIOCTL), kcred, &rval); 7414bac2208Snarayan 7424bac2208Snarayan if (status == 0) { 7434bac2208Snarayan *label = VD_DISK_LABEL_VTOC; 7444bac2208Snarayan return (0); 7454bac2208Snarayan } else if (status != ENOTSUP) { 746*3af08d82Slm66018 PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status); 7474bac2208Snarayan return (status); 7484bac2208Snarayan } 7494bac2208Snarayan 7504bac2208Snarayan status = vds_efi_alloc_and_read(handle, &efi, &efi_len); 7514bac2208Snarayan 7524bac2208Snarayan if (status) { 753*3af08d82Slm66018 PR0("vds_efi_alloc_and_read returned error %d", status); 7544bac2208Snarayan return (status); 7554bac2208Snarayan } 7564bac2208Snarayan 7574bac2208Snarayan *label = VD_DISK_LABEL_EFI; 7584bac2208Snarayan vd_efi_to_vtoc(efi, vtoc); 7594bac2208Snarayan vd_efi_free(efi, efi_len); 7604bac2208Snarayan 7614bac2208Snarayan return (0); 7624bac2208Snarayan } 7634bac2208Snarayan 7641ae08745Sheppo static int 7650a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 7661ae08745Sheppo { 7674bac2208Snarayan dk_efi_t *dk_ioc; 7684bac2208Snarayan 7694bac2208Snarayan switch (vd->vdisk_label) { 7704bac2208Snarayan 7714bac2208Snarayan case VD_DISK_LABEL_VTOC: 7724bac2208Snarayan 7731ae08745Sheppo switch (cmd) { 7741ae08745Sheppo case DKIOCGGEOM: 7750a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 7760a55fbb7Slm66018 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 7771ae08745Sheppo return (0); 7781ae08745Sheppo case DKIOCGVTOC: 7790a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 7800a55fbb7Slm66018 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 7811ae08745Sheppo return (0); 7821ae08745Sheppo default: 7831ae08745Sheppo return (ENOTSUP); 7841ae08745Sheppo } 7854bac2208Snarayan 7864bac2208Snarayan case VD_DISK_LABEL_EFI: 7874bac2208Snarayan 7884bac2208Snarayan switch (cmd) { 7894bac2208Snarayan case DKIOCGETEFI: 7904bac2208Snarayan ASSERT(ioctl_arg != NULL); 7914bac2208Snarayan dk_ioc = (dk_efi_t *)ioctl_arg; 7924bac2208Snarayan if (dk_ioc->dki_length < vd->dk_efi.dki_length) 7934bac2208Snarayan return (EINVAL); 7944bac2208Snarayan bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data, 7954bac2208Snarayan vd->dk_efi.dki_length); 7964bac2208Snarayan return (0); 7974bac2208Snarayan default: 7984bac2208Snarayan return (ENOTSUP); 7994bac2208Snarayan } 8004bac2208Snarayan 8014bac2208Snarayan default: 8024bac2208Snarayan return (ENOTSUP); 8034bac2208Snarayan } 8041ae08745Sheppo } 8051ae08745Sheppo 8061ae08745Sheppo static int 8070a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 8081ae08745Sheppo { 8091ae08745Sheppo int rval = 0, status; 8101ae08745Sheppo size_t nbytes = request->nbytes; /* modifiable copy */ 8111ae08745Sheppo 8121ae08745Sheppo 8131ae08745Sheppo ASSERT(request->slice < vd->nslices); 8141ae08745Sheppo PR0("Performing %s", ioctl->operation_name); 8151ae08745Sheppo 8160a55fbb7Slm66018 /* Get data from client and convert, if necessary */ 8170a55fbb7Slm66018 if (ioctl->copyin != NULL) { 8181ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 8191ae08745Sheppo PR1("Getting \"arg\" data from client"); 8201ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 8211ae08745Sheppo request->cookie, request->ncookies, 8221ae08745Sheppo LDC_COPY_IN)) != 0) { 823*3af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 8241ae08745Sheppo "copying from client", status); 8251ae08745Sheppo return (status); 8261ae08745Sheppo } 8270a55fbb7Slm66018 8280a55fbb7Slm66018 /* Convert client's data, if necessary */ 8290a55fbb7Slm66018 if (ioctl->copyin == VD_IDENTITY) /* use client buffer */ 8300a55fbb7Slm66018 ioctl->arg = buf; 8310a55fbb7Slm66018 else /* convert client vdisk operation data to ioctl data */ 8320a55fbb7Slm66018 (ioctl->copyin)(buf, (void *)ioctl->arg); 8331ae08745Sheppo } 8341ae08745Sheppo 8351ae08745Sheppo /* 8361ae08745Sheppo * Handle single-slice block devices internally; otherwise, have the 8371ae08745Sheppo * real driver perform the ioctl() 8381ae08745Sheppo */ 8391ae08745Sheppo if (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo) { 8400a55fbb7Slm66018 if ((status = vd_do_slice_ioctl(vd, ioctl->cmd, 8410a55fbb7Slm66018 (void *)ioctl->arg)) != 0) 8421ae08745Sheppo return (status); 8431ae08745Sheppo } else if ((status = ldi_ioctl(vd->ldi_handle[request->slice], 844d10e4ef2Snarayan ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL), 845d10e4ef2Snarayan kcred, &rval)) != 0) { 8461ae08745Sheppo PR0("ldi_ioctl(%s) = errno %d", ioctl->cmd_name, status); 8471ae08745Sheppo return (status); 8481ae08745Sheppo } 8491ae08745Sheppo #ifdef DEBUG 8501ae08745Sheppo if (rval != 0) { 851*3af08d82Slm66018 PR0("%s set rval = %d, which is not being returned to client", 8521ae08745Sheppo ioctl->cmd_name, rval); 8531ae08745Sheppo } 8541ae08745Sheppo #endif /* DEBUG */ 8551ae08745Sheppo 8560a55fbb7Slm66018 /* Convert data and send to client, if necessary */ 8570a55fbb7Slm66018 if (ioctl->copyout != NULL) { 8581ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 8591ae08745Sheppo PR1("Sending \"arg\" data to client"); 8600a55fbb7Slm66018 8610a55fbb7Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */ 8620a55fbb7Slm66018 if (ioctl->copyout != VD_IDENTITY) 8630a55fbb7Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf); 8640a55fbb7Slm66018 8651ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 8661ae08745Sheppo request->cookie, request->ncookies, 8671ae08745Sheppo LDC_COPY_OUT)) != 0) { 868*3af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 8691ae08745Sheppo "copying to client", status); 8701ae08745Sheppo return (status); 8711ae08745Sheppo } 8721ae08745Sheppo } 8731ae08745Sheppo 8741ae08745Sheppo return (status); 8751ae08745Sheppo } 8761ae08745Sheppo 8770a55fbb7Slm66018 /* 8780a55fbb7Slm66018 * Open any slices which have become non-empty as a result of performing a 8790a55fbb7Slm66018 * set-VTOC operation for the client. 8800a55fbb7Slm66018 * 8810a55fbb7Slm66018 * When serving a full disk, vds attempts to exclusively open all of the 8820a55fbb7Slm66018 * disk's slices to prevent another thread or process in the service domain 8830a55fbb7Slm66018 * from "stealing" a slice or from performing I/O to a slice while a vds 8840a55fbb7Slm66018 * client is accessing it. Unfortunately, underlying drivers, such as sd(7d) 8850a55fbb7Slm66018 * and cmdk(7d), return an error when attempting to open the device file for a 8860a55fbb7Slm66018 * slice which is currently empty according to the VTOC. This driver behavior 8870a55fbb7Slm66018 * means that vds must skip opening empty slices when initializing a vdisk for 8880a55fbb7Slm66018 * full-disk service and try to open slices that become non-empty (via a 8890a55fbb7Slm66018 * set-VTOC operation) during use of the full disk in order to begin serving 8900a55fbb7Slm66018 * such slices to the client. This approach has an inherent (and therefore 8910a55fbb7Slm66018 * unavoidable) race condition; it also means that failure to open a 8920a55fbb7Slm66018 * newly-non-empty slice has different semantics than failure to open an 8930a55fbb7Slm66018 * initially-non-empty slice: Due to driver bahavior, opening a 8940a55fbb7Slm66018 * newly-non-empty slice is a necessary side effect of vds performing a 8950a55fbb7Slm66018 * (successful) set-VTOC operation for a client on an in-service (and in-use) 8960a55fbb7Slm66018 * disk in order to begin serving the slice; failure of this side-effect 8970a55fbb7Slm66018 * operation does not mean that the client's set-VTOC operation failed or that 8980a55fbb7Slm66018 * operations on other slices must fail. Therefore, this function prints an 8990a55fbb7Slm66018 * error message on failure to open a slice, but does not return an error to 9000a55fbb7Slm66018 * its caller--unlike failure to open a slice initially, which results in an 9010a55fbb7Slm66018 * error that prevents serving the vdisk (and thereby requires an 9020a55fbb7Slm66018 * administrator to resolve the problem). Note that, apart from another 9030a55fbb7Slm66018 * thread or process opening a new slice during the race-condition window, 9040a55fbb7Slm66018 * failure to open a slice in this function will likely indicate an underlying 9050a55fbb7Slm66018 * drive problem, which will also likely become evident in errors returned by 9060a55fbb7Slm66018 * operations on other slices, and which will require administrative 9070a55fbb7Slm66018 * intervention and possibly servicing the drive. 9080a55fbb7Slm66018 */ 9090a55fbb7Slm66018 static void 9100a55fbb7Slm66018 vd_open_new_slices(vd_t *vd) 9110a55fbb7Slm66018 { 9124bac2208Snarayan int status; 9130a55fbb7Slm66018 struct vtoc vtoc; 9140a55fbb7Slm66018 9154bac2208Snarayan /* Get the (new) partitions for updated slice sizes */ 9164bac2208Snarayan if ((status = vd_read_vtoc(vd->ldi_handle[0], &vtoc, 9174bac2208Snarayan &vd->vdisk_label)) != 0) { 918*3af08d82Slm66018 PR0("vd_read_vtoc returned error %d", status); 9190a55fbb7Slm66018 return; 9200a55fbb7Slm66018 } 9210a55fbb7Slm66018 9220a55fbb7Slm66018 /* Open any newly-non-empty slices */ 9230a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 9240a55fbb7Slm66018 /* Skip zero-length slices */ 9250a55fbb7Slm66018 if (vtoc.v_part[slice].p_size == 0) { 9260a55fbb7Slm66018 if (vd->ldi_handle[slice] != NULL) 9270a55fbb7Slm66018 PR0("Open slice %u now has zero length", slice); 9280a55fbb7Slm66018 continue; 9290a55fbb7Slm66018 } 9300a55fbb7Slm66018 9310a55fbb7Slm66018 /* Skip already-open slices */ 9320a55fbb7Slm66018 if (vd->ldi_handle[slice] != NULL) 9330a55fbb7Slm66018 continue; 9340a55fbb7Slm66018 9350a55fbb7Slm66018 PR0("Opening newly-non-empty slice %u", slice); 9360a55fbb7Slm66018 if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 9370a55fbb7Slm66018 vd_open_flags, kcred, &vd->ldi_handle[slice], 9380a55fbb7Slm66018 vd->vds->ldi_ident)) != 0) { 939*3af08d82Slm66018 PR0("ldi_open_by_dev() returned errno %d " 9400a55fbb7Slm66018 "for slice %u", status, slice); 9410a55fbb7Slm66018 } 9420a55fbb7Slm66018 } 9430a55fbb7Slm66018 } 9440a55fbb7Slm66018 9451ae08745Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 9461ae08745Sheppo static int 947d10e4ef2Snarayan vd_ioctl(vd_task_t *task) 9481ae08745Sheppo { 9491ae08745Sheppo int i, status; 9501ae08745Sheppo void *buf = NULL; 9510a55fbb7Slm66018 struct dk_geom dk_geom = {0}; 9520a55fbb7Slm66018 struct vtoc vtoc = {0}; 9534bac2208Snarayan struct dk_efi dk_efi = {0}; 954d10e4ef2Snarayan vd_t *vd = task->vd; 955d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 9560a55fbb7Slm66018 vd_ioctl_t ioctl[] = { 9570a55fbb7Slm66018 /* Command (no-copy) operations */ 9580a55fbb7Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 9590a55fbb7Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 9600a55fbb7Slm66018 NULL, NULL, NULL}, 9610a55fbb7Slm66018 9620a55fbb7Slm66018 /* "Get" (copy-out) operations */ 9630a55fbb7Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 9640a55fbb7Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 9654bac2208Snarayan NULL, VD_IDENTITY, VD_IDENTITY}, 9660a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 9670a55fbb7Slm66018 RNDSIZE(vd_geom_t), 9680a55fbb7Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 9690a55fbb7Slm66018 &dk_geom, NULL, dk_geom2vd_geom}, 9700a55fbb7Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 9710a55fbb7Slm66018 DKIOCGVTOC, STRINGIZE(DKIOCGVTOC), 9720a55fbb7Slm66018 &vtoc, NULL, vtoc2vd_vtoc}, 9734bac2208Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 9744bac2208Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 9754bac2208Snarayan &dk_efi, vd_get_efi_in, vd_get_efi_out}, 9760a55fbb7Slm66018 9770a55fbb7Slm66018 /* "Set" (copy-in) operations */ 9780a55fbb7Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 9790a55fbb7Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 9804bac2208Snarayan NULL, VD_IDENTITY, VD_IDENTITY}, 9810a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 9820a55fbb7Slm66018 RNDSIZE(vd_geom_t), 9830a55fbb7Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 9840a55fbb7Slm66018 &dk_geom, vd_geom2dk_geom, NULL}, 9850a55fbb7Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 9860a55fbb7Slm66018 DKIOCSVTOC, STRINGIZE(DKIOCSVTOC), 9870a55fbb7Slm66018 &vtoc, vd_vtoc2vtoc, NULL}, 9884bac2208Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 9894bac2208Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 9904bac2208Snarayan &dk_efi, vd_set_efi_in, vd_set_efi_out}, 9910a55fbb7Slm66018 }; 9921ae08745Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 9931ae08745Sheppo 9941ae08745Sheppo 995d10e4ef2Snarayan ASSERT(vd != NULL); 996d10e4ef2Snarayan ASSERT(request != NULL); 9971ae08745Sheppo ASSERT(request->slice < vd->nslices); 9981ae08745Sheppo 9991ae08745Sheppo /* 10001ae08745Sheppo * Determine ioctl corresponding to caller's "operation" and 10011ae08745Sheppo * validate caller's "nbytes" 10021ae08745Sheppo */ 10031ae08745Sheppo for (i = 0; i < nioctls; i++) { 10041ae08745Sheppo if (request->operation == ioctl[i].operation) { 10050a55fbb7Slm66018 /* LDC memory operations require 8-byte multiples */ 10060a55fbb7Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 10070a55fbb7Slm66018 10084bac2208Snarayan if (request->operation == VD_OP_GET_EFI || 10094bac2208Snarayan request->operation == VD_OP_SET_EFI) { 10104bac2208Snarayan if (request->nbytes >= ioctl[i].nbytes) 10114bac2208Snarayan break; 1012*3af08d82Slm66018 PR0("%s: Expected at least nbytes = %lu, " 10134bac2208Snarayan "got %lu", ioctl[i].operation_name, 10144bac2208Snarayan ioctl[i].nbytes, request->nbytes); 10154bac2208Snarayan return (EINVAL); 10164bac2208Snarayan } 10174bac2208Snarayan 10180a55fbb7Slm66018 if (request->nbytes != ioctl[i].nbytes) { 1019*3af08d82Slm66018 PR0("%s: Expected nbytes = %lu, got %lu", 10200a55fbb7Slm66018 ioctl[i].operation_name, ioctl[i].nbytes, 10210a55fbb7Slm66018 request->nbytes); 10221ae08745Sheppo return (EINVAL); 10231ae08745Sheppo } 10241ae08745Sheppo 10251ae08745Sheppo break; 10261ae08745Sheppo } 10271ae08745Sheppo } 10281ae08745Sheppo ASSERT(i < nioctls); /* because "operation" already validated */ 10291ae08745Sheppo 10301ae08745Sheppo if (request->nbytes) 10311ae08745Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP); 10321ae08745Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 10331ae08745Sheppo if (request->nbytes) 10341ae08745Sheppo kmem_free(buf, request->nbytes); 10354bac2208Snarayan if (vd->vdisk_type == VD_DISK_TYPE_DISK && 10364bac2208Snarayan (request->operation == VD_OP_SET_VTOC || 10374bac2208Snarayan request->operation == VD_OP_SET_EFI)) 10380a55fbb7Slm66018 vd_open_new_slices(vd); 1039d10e4ef2Snarayan PR0("Returning %d", status); 10401ae08745Sheppo return (status); 10411ae08745Sheppo } 10421ae08745Sheppo 10434bac2208Snarayan static int 10444bac2208Snarayan vd_get_devid(vd_task_t *task) 10454bac2208Snarayan { 10464bac2208Snarayan vd_t *vd = task->vd; 10474bac2208Snarayan vd_dring_payload_t *request = task->request; 10484bac2208Snarayan vd_devid_t *vd_devid; 10494bac2208Snarayan impl_devid_t *devid; 10504bac2208Snarayan int status, bufid_len, devid_len, len; 1051*3af08d82Slm66018 int bufbytes; 10524bac2208Snarayan 1053*3af08d82Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes); 10544bac2208Snarayan 10554bac2208Snarayan if (ddi_lyr_get_devid(vd->dev[request->slice], 10564bac2208Snarayan (ddi_devid_t *)&devid) != DDI_SUCCESS) { 10574bac2208Snarayan /* the most common failure is that no devid is available */ 1058*3af08d82Slm66018 PR2("No Device ID"); 10594bac2208Snarayan return (ENOENT); 10604bac2208Snarayan } 10614bac2208Snarayan 10624bac2208Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 10634bac2208Snarayan devid_len = DEVID_GETLEN(devid); 10644bac2208Snarayan 1065*3af08d82Slm66018 /* 1066*3af08d82Slm66018 * Save the buffer size here for use in deallocation. 1067*3af08d82Slm66018 * The actual number of bytes copied is returned in 1068*3af08d82Slm66018 * the 'nbytes' field of the request structure. 1069*3af08d82Slm66018 */ 1070*3af08d82Slm66018 bufbytes = request->nbytes; 1071*3af08d82Slm66018 1072*3af08d82Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 10734bac2208Snarayan vd_devid->length = devid_len; 10744bac2208Snarayan vd_devid->type = DEVID_GETTYPE(devid); 10754bac2208Snarayan 10764bac2208Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len; 10774bac2208Snarayan 10784bac2208Snarayan bcopy(devid->did_id, vd_devid->id, len); 10794bac2208Snarayan 10804bac2208Snarayan /* LDC memory operations require 8-byte multiples */ 10814bac2208Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0); 10824bac2208Snarayan 10834bac2208Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 10844bac2208Snarayan &request->nbytes, request->cookie, request->ncookies, 10854bac2208Snarayan LDC_COPY_OUT)) != 0) { 1086*3af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client", 10874bac2208Snarayan status); 10884bac2208Snarayan } 1089*3af08d82Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes); 10904bac2208Snarayan 1091*3af08d82Slm66018 kmem_free(vd_devid, bufbytes); 10924bac2208Snarayan ddi_devid_free((ddi_devid_t)devid); 10934bac2208Snarayan 10944bac2208Snarayan return (status); 10954bac2208Snarayan } 10964bac2208Snarayan 10971ae08745Sheppo /* 10981ae08745Sheppo * Define the supported operations once the functions for performing them have 10991ae08745Sheppo * been defined 11001ae08745Sheppo */ 11011ae08745Sheppo static const vds_operation_t vds_operation[] = { 1102*3af08d82Slm66018 #define X(_s) #_s, _s 1103*3af08d82Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 1104*3af08d82Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 1105*3af08d82Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 1106*3af08d82Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 1107*3af08d82Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 1108*3af08d82Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 1109*3af08d82Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 1110*3af08d82Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 1111*3af08d82Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 1112*3af08d82Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 1113*3af08d82Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 1114*3af08d82Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 1115*3af08d82Slm66018 #undef X 11161ae08745Sheppo }; 11171ae08745Sheppo 11181ae08745Sheppo static const size_t vds_noperations = 11191ae08745Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0])); 11201ae08745Sheppo 11211ae08745Sheppo /* 1122d10e4ef2Snarayan * Process a task specifying a client I/O request 11231ae08745Sheppo */ 11241ae08745Sheppo static int 1125d10e4ef2Snarayan vd_process_task(vd_task_t *task) 11261ae08745Sheppo { 1127d10e4ef2Snarayan int i, status; 1128d10e4ef2Snarayan vd_t *vd = task->vd; 1129d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 11301ae08745Sheppo 11311ae08745Sheppo 1132d10e4ef2Snarayan ASSERT(vd != NULL); 1133d10e4ef2Snarayan ASSERT(request != NULL); 11341ae08745Sheppo 1135d10e4ef2Snarayan /* Find the requested operation */ 11361ae08745Sheppo for (i = 0; i < vds_noperations; i++) 11371ae08745Sheppo if (request->operation == vds_operation[i].operation) 1138d10e4ef2Snarayan break; 1139d10e4ef2Snarayan if (i == vds_noperations) { 1140*3af08d82Slm66018 PR0("Unsupported operation %u", request->operation); 11411ae08745Sheppo return (ENOTSUP); 11421ae08745Sheppo } 11431ae08745Sheppo 11447636cb21Slm66018 /* Handle client using absolute disk offsets */ 11457636cb21Slm66018 if ((vd->vdisk_type == VD_DISK_TYPE_DISK) && 11467636cb21Slm66018 (request->slice == UINT8_MAX)) 11477636cb21Slm66018 request->slice = VD_ENTIRE_DISK_SLICE; 11487636cb21Slm66018 11497636cb21Slm66018 /* Range-check slice */ 11507636cb21Slm66018 if (request->slice >= vd->nslices) { 1151*3af08d82Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 11527636cb21Slm66018 request->slice, (vd->nslices - 1)); 11537636cb21Slm66018 return (EINVAL); 11547636cb21Slm66018 } 11557636cb21Slm66018 1156*3af08d82Slm66018 PR1("operation : %s", vds_operation[i].namep); 1157*3af08d82Slm66018 1158d10e4ef2Snarayan /* Start the operation */ 1159d10e4ef2Snarayan if ((status = vds_operation[i].start(task)) != EINPROGRESS) { 1160*3af08d82Slm66018 PR0("operation : %s returned status %d", 1161*3af08d82Slm66018 vds_operation[i].namep, status); 1162d10e4ef2Snarayan request->status = status; /* op succeeded or failed */ 1163d10e4ef2Snarayan return (0); /* but request completed */ 11641ae08745Sheppo } 11651ae08745Sheppo 1166d10e4ef2Snarayan ASSERT(vds_operation[i].complete != NULL); /* debug case */ 1167d10e4ef2Snarayan if (vds_operation[i].complete == NULL) { /* non-debug case */ 1168*3af08d82Slm66018 PR0("Unexpected return of EINPROGRESS " 1169d10e4ef2Snarayan "with no I/O completion handler"); 1170d10e4ef2Snarayan request->status = EIO; /* operation failed */ 1171d10e4ef2Snarayan return (0); /* but request completed */ 11721ae08745Sheppo } 11731ae08745Sheppo 1174*3af08d82Slm66018 PR1("operation : kick off taskq entry for %s", vds_operation[i].namep); 1175*3af08d82Slm66018 1176d10e4ef2Snarayan /* Queue a task to complete the operation */ 1177d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->completionq, vds_operation[i].complete, 1178d10e4ef2Snarayan task, DDI_SLEEP); 1179d10e4ef2Snarayan /* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */ 1180d10e4ef2Snarayan ASSERT(status == DDI_SUCCESS); 1181d10e4ef2Snarayan 1182d10e4ef2Snarayan PR1("Operation in progress"); 1183d10e4ef2Snarayan return (EINPROGRESS); /* completion handler will finish request */ 11841ae08745Sheppo } 11851ae08745Sheppo 11861ae08745Sheppo /* 11870a55fbb7Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first 11880a55fbb7Slm66018 * argument match the corresponding remaining arguments; otherwise, return false 11891ae08745Sheppo */ 11900a55fbb7Slm66018 boolean_t 11911ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 11921ae08745Sheppo { 11931ae08745Sheppo return ((tag->vio_msgtype == type) && 11941ae08745Sheppo (tag->vio_subtype == subtype) && 11950a55fbb7Slm66018 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 11961ae08745Sheppo } 11971ae08745Sheppo 11980a55fbb7Slm66018 /* 11990a55fbb7Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported 12000a55fbb7Slm66018 * by this server. 12010a55fbb7Slm66018 */ 12020a55fbb7Slm66018 static boolean_t 12030a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg) 12040a55fbb7Slm66018 { 12050a55fbb7Slm66018 for (int i = 0; i < vds_num_versions; i++) { 12060a55fbb7Slm66018 ASSERT(vds_version[i].major > 0); 12070a55fbb7Slm66018 ASSERT((i == 0) || 12080a55fbb7Slm66018 (vds_version[i].major < vds_version[i-1].major)); 12090a55fbb7Slm66018 12100a55fbb7Slm66018 /* 12110a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 12120a55fbb7Slm66018 * necessary, down to the highest value supported by this 12130a55fbb7Slm66018 * server and return true so this message will get "ack"ed; 12140a55fbb7Slm66018 * the client should also support all minor versions lower 12150a55fbb7Slm66018 * than the value it sent 12160a55fbb7Slm66018 */ 12170a55fbb7Slm66018 if (ver_msg->ver_major == vds_version[i].major) { 12180a55fbb7Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) { 12190a55fbb7Slm66018 PR0("Adjusting minor version from %u to %u", 12200a55fbb7Slm66018 ver_msg->ver_minor, vds_version[i].minor); 12210a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 12220a55fbb7Slm66018 } 12230a55fbb7Slm66018 return (B_TRUE); 12240a55fbb7Slm66018 } 12250a55fbb7Slm66018 12260a55fbb7Slm66018 /* 12270a55fbb7Slm66018 * If the message contains a higher major version number, set 12280a55fbb7Slm66018 * the message's major/minor versions to the current values 12290a55fbb7Slm66018 * and return false, so this message will get "nack"ed with 12300a55fbb7Slm66018 * these values, and the client will potentially try again 12310a55fbb7Slm66018 * with the same or a lower version 12320a55fbb7Slm66018 */ 12330a55fbb7Slm66018 if (ver_msg->ver_major > vds_version[i].major) { 12340a55fbb7Slm66018 ver_msg->ver_major = vds_version[i].major; 12350a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 12360a55fbb7Slm66018 return (B_FALSE); 12370a55fbb7Slm66018 } 12380a55fbb7Slm66018 12390a55fbb7Slm66018 /* 12400a55fbb7Slm66018 * Otherwise, the message's major version is less than the 12410a55fbb7Slm66018 * current major version, so continue the loop to the next 12420a55fbb7Slm66018 * (lower) supported version 12430a55fbb7Slm66018 */ 12440a55fbb7Slm66018 } 12450a55fbb7Slm66018 12460a55fbb7Slm66018 /* 12470a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 12480a55fbb7Slm66018 * message to terminate negotiation 12490a55fbb7Slm66018 */ 12500a55fbb7Slm66018 ver_msg->ver_major = 0; 12510a55fbb7Slm66018 ver_msg->ver_minor = 0; 12520a55fbb7Slm66018 return (B_FALSE); 12530a55fbb7Slm66018 } 12540a55fbb7Slm66018 12550a55fbb7Slm66018 /* 12560a55fbb7Slm66018 * Process a version message from a client. vds expects to receive version 12570a55fbb7Slm66018 * messages from clients seeking service, but never issues version messages 12580a55fbb7Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does 12590a55fbb7Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such 12600a55fbb7Slm66018 * messages as invalid). 12610a55fbb7Slm66018 */ 12621ae08745Sheppo static int 12630a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 12641ae08745Sheppo { 12651ae08745Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 12661ae08745Sheppo 12671ae08745Sheppo 12681ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 12691ae08745Sheppo 12701ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 12711ae08745Sheppo VIO_VER_INFO)) { 12721ae08745Sheppo return (ENOMSG); /* not a version message */ 12731ae08745Sheppo } 12741ae08745Sheppo 12751ae08745Sheppo if (msglen != sizeof (*ver_msg)) { 1276*3af08d82Slm66018 PR0("Expected %lu-byte version message; " 12771ae08745Sheppo "received %lu bytes", sizeof (*ver_msg), msglen); 12781ae08745Sheppo return (EBADMSG); 12791ae08745Sheppo } 12801ae08745Sheppo 12811ae08745Sheppo if (ver_msg->dev_class != VDEV_DISK) { 1282*3af08d82Slm66018 PR0("Expected device class %u (disk); received %u", 12831ae08745Sheppo VDEV_DISK, ver_msg->dev_class); 12841ae08745Sheppo return (EBADMSG); 12851ae08745Sheppo } 12861ae08745Sheppo 12870a55fbb7Slm66018 /* 12880a55fbb7Slm66018 * We're talking to the expected kind of client; set our device class 12890a55fbb7Slm66018 * for "ack/nack" back to the client 12900a55fbb7Slm66018 */ 12911ae08745Sheppo ver_msg->dev_class = VDEV_DISK_SERVER; 12920a55fbb7Slm66018 12930a55fbb7Slm66018 /* 12940a55fbb7Slm66018 * Check whether the (valid) version message specifies a version 12950a55fbb7Slm66018 * supported by this server. If the version is not supported, return 12960a55fbb7Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version() 12970a55fbb7Slm66018 * will have updated the message with a supported version for the 12980a55fbb7Slm66018 * client to consider 12990a55fbb7Slm66018 */ 13000a55fbb7Slm66018 if (!vds_supported_version(ver_msg)) 13010a55fbb7Slm66018 return (EBADMSG); 13020a55fbb7Slm66018 13030a55fbb7Slm66018 13040a55fbb7Slm66018 /* 13050a55fbb7Slm66018 * A version has been agreed upon; use the client's SID for 13060a55fbb7Slm66018 * communication on this channel now 13070a55fbb7Slm66018 */ 13080a55fbb7Slm66018 ASSERT(!(vd->initialized & VD_SID)); 13090a55fbb7Slm66018 vd->sid = ver_msg->tag.vio_sid; 13100a55fbb7Slm66018 vd->initialized |= VD_SID; 13110a55fbb7Slm66018 13120a55fbb7Slm66018 /* 13130a55fbb7Slm66018 * When multiple versions are supported, this function should store 13140a55fbb7Slm66018 * the negotiated major and minor version values in the "vd" data 13150a55fbb7Slm66018 * structure to govern further communication; in particular, note that 13160a55fbb7Slm66018 * the client might have specified a lower minor version for the 13170a55fbb7Slm66018 * agreed major version than specifed in the vds_version[] array. The 13180a55fbb7Slm66018 * following assertions should help remind future maintainers to make 13190a55fbb7Slm66018 * the appropriate changes to support multiple versions. 13200a55fbb7Slm66018 */ 13210a55fbb7Slm66018 ASSERT(vds_num_versions == 1); 13220a55fbb7Slm66018 ASSERT(ver_msg->ver_major == vds_version[0].major); 13230a55fbb7Slm66018 ASSERT(ver_msg->ver_minor == vds_version[0].minor); 13240a55fbb7Slm66018 13250a55fbb7Slm66018 PR0("Using major version %u, minor version %u", 13260a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 13271ae08745Sheppo return (0); 13281ae08745Sheppo } 13291ae08745Sheppo 13301ae08745Sheppo static int 13311ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 13321ae08745Sheppo { 13331ae08745Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 13341ae08745Sheppo 13351ae08745Sheppo 13361ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 13371ae08745Sheppo 13381ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 13391ae08745Sheppo VIO_ATTR_INFO)) { 1340d10e4ef2Snarayan PR0("Message is not an attribute message"); 1341d10e4ef2Snarayan return (ENOMSG); 13421ae08745Sheppo } 13431ae08745Sheppo 13441ae08745Sheppo if (msglen != sizeof (*attr_msg)) { 1345*3af08d82Slm66018 PR0("Expected %lu-byte attribute message; " 13461ae08745Sheppo "received %lu bytes", sizeof (*attr_msg), msglen); 13471ae08745Sheppo return (EBADMSG); 13481ae08745Sheppo } 13491ae08745Sheppo 13501ae08745Sheppo if (attr_msg->max_xfer_sz == 0) { 1351*3af08d82Slm66018 PR0("Received maximum transfer size of 0 from client"); 13521ae08745Sheppo return (EBADMSG); 13531ae08745Sheppo } 13541ae08745Sheppo 13551ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 13561ae08745Sheppo (attr_msg->xfer_mode != VIO_DRING_MODE)) { 1357*3af08d82Slm66018 PR0("Client requested unsupported transfer mode"); 13581ae08745Sheppo return (EBADMSG); 13591ae08745Sheppo } 13601ae08745Sheppo 13611ae08745Sheppo /* Success: valid message and transfer mode */ 13621ae08745Sheppo vd->xfer_mode = attr_msg->xfer_mode; 1363*3af08d82Slm66018 13641ae08745Sheppo if (vd->xfer_mode == VIO_DESC_MODE) { 1365*3af08d82Slm66018 13661ae08745Sheppo /* 13671ae08745Sheppo * The vd_dring_inband_msg_t contains one cookie; need room 13681ae08745Sheppo * for up to n-1 more cookies, where "n" is the number of full 13691ae08745Sheppo * pages plus possibly one partial page required to cover 13701ae08745Sheppo * "max_xfer_sz". Add room for one more cookie if 13711ae08745Sheppo * "max_xfer_sz" isn't an integral multiple of the page size. 13721ae08745Sheppo * Must first get the maximum transfer size in bytes. 13731ae08745Sheppo */ 13741ae08745Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 13751ae08745Sheppo attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 13761ae08745Sheppo attr_msg->max_xfer_sz; 13771ae08745Sheppo size_t max_inband_msglen = 13781ae08745Sheppo sizeof (vd_dring_inband_msg_t) + 13791ae08745Sheppo ((max_xfer_bytes/PAGESIZE + 13801ae08745Sheppo ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 13811ae08745Sheppo (sizeof (ldc_mem_cookie_t))); 13821ae08745Sheppo 13831ae08745Sheppo /* 13841ae08745Sheppo * Set the maximum expected message length to 13851ae08745Sheppo * accommodate in-band-descriptor messages with all 13861ae08745Sheppo * their cookies 13871ae08745Sheppo */ 13881ae08745Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 1389d10e4ef2Snarayan 1390d10e4ef2Snarayan /* 1391d10e4ef2Snarayan * Initialize the data structure for processing in-band I/O 1392d10e4ef2Snarayan * request descriptors 1393d10e4ef2Snarayan */ 1394d10e4ef2Snarayan vd->inband_task.vd = vd; 1395*3af08d82Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 1396d10e4ef2Snarayan vd->inband_task.index = 0; 1397d10e4ef2Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 13981ae08745Sheppo } 13991ae08745Sheppo 1400e1ebb9ecSlm66018 /* Return the device's block size and max transfer size to the client */ 1401e1ebb9ecSlm66018 attr_msg->vdisk_block_size = DEV_BSIZE; 1402e1ebb9ecSlm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz; 1403e1ebb9ecSlm66018 14041ae08745Sheppo attr_msg->vdisk_size = vd->vdisk_size; 14051ae08745Sheppo attr_msg->vdisk_type = vd->vdisk_type; 14061ae08745Sheppo attr_msg->operations = vds_operations; 14071ae08745Sheppo PR0("%s", VD_CLIENT(vd)); 1408*3af08d82Slm66018 1409*3af08d82Slm66018 ASSERT(vd->dring_task == NULL); 1410*3af08d82Slm66018 14111ae08745Sheppo return (0); 14121ae08745Sheppo } 14131ae08745Sheppo 14141ae08745Sheppo static int 14151ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 14161ae08745Sheppo { 14171ae08745Sheppo int status; 14181ae08745Sheppo size_t expected; 14191ae08745Sheppo ldc_mem_info_t dring_minfo; 14201ae08745Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 14211ae08745Sheppo 14221ae08745Sheppo 14231ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 14241ae08745Sheppo 14251ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 14261ae08745Sheppo VIO_DRING_REG)) { 1427d10e4ef2Snarayan PR0("Message is not a register-dring message"); 1428d10e4ef2Snarayan return (ENOMSG); 14291ae08745Sheppo } 14301ae08745Sheppo 14311ae08745Sheppo if (msglen < sizeof (*reg_msg)) { 1432*3af08d82Slm66018 PR0("Expected at least %lu-byte register-dring message; " 14331ae08745Sheppo "received %lu bytes", sizeof (*reg_msg), msglen); 14341ae08745Sheppo return (EBADMSG); 14351ae08745Sheppo } 14361ae08745Sheppo 14371ae08745Sheppo expected = sizeof (*reg_msg) + 14381ae08745Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 14391ae08745Sheppo if (msglen != expected) { 1440*3af08d82Slm66018 PR0("Expected %lu-byte register-dring message; " 14411ae08745Sheppo "received %lu bytes", expected, msglen); 14421ae08745Sheppo return (EBADMSG); 14431ae08745Sheppo } 14441ae08745Sheppo 14451ae08745Sheppo if (vd->initialized & VD_DRING) { 1446*3af08d82Slm66018 PR0("A dring was previously registered; only support one"); 14471ae08745Sheppo return (EBADMSG); 14481ae08745Sheppo } 14491ae08745Sheppo 1450d10e4ef2Snarayan if (reg_msg->num_descriptors > INT32_MAX) { 1451*3af08d82Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 1452d10e4ef2Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 1453d10e4ef2Snarayan return (EBADMSG); 1454d10e4ef2Snarayan } 1455d10e4ef2Snarayan 14561ae08745Sheppo if (reg_msg->ncookies != 1) { 14571ae08745Sheppo /* 14581ae08745Sheppo * In addition to fixing the assertion in the success case 14591ae08745Sheppo * below, supporting drings which require more than one 14601ae08745Sheppo * "cookie" requires increasing the value of vd->max_msglen 14611ae08745Sheppo * somewhere in the code path prior to receiving the message 14621ae08745Sheppo * which results in calling this function. Note that without 14631ae08745Sheppo * making this change, the larger message size required to 14641ae08745Sheppo * accommodate multiple cookies cannot be successfully 14651ae08745Sheppo * received, so this function will not even get called. 14661ae08745Sheppo * Gracefully accommodating more dring cookies might 14671ae08745Sheppo * reasonably demand exchanging an additional attribute or 14681ae08745Sheppo * making a minor protocol adjustment 14691ae08745Sheppo */ 1470*3af08d82Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 14711ae08745Sheppo return (EBADMSG); 14721ae08745Sheppo } 14731ae08745Sheppo 14741ae08745Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 14751ae08745Sheppo reg_msg->ncookies, reg_msg->num_descriptors, 14764bac2208Snarayan reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle); 14771ae08745Sheppo if (status != 0) { 1478*3af08d82Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status); 14791ae08745Sheppo return (status); 14801ae08745Sheppo } 14811ae08745Sheppo 14821ae08745Sheppo /* 14831ae08745Sheppo * To remove the need for this assertion, must call 14841ae08745Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 14851ae08745Sheppo * successful call to ldc_mem_dring_map() 14861ae08745Sheppo */ 14871ae08745Sheppo ASSERT(reg_msg->ncookies == 1); 14881ae08745Sheppo 14891ae08745Sheppo if ((status = 14901ae08745Sheppo ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 1491*3af08d82Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status); 14921ae08745Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 1493*3af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 14941ae08745Sheppo return (status); 14951ae08745Sheppo } 14961ae08745Sheppo 14971ae08745Sheppo if (dring_minfo.vaddr == NULL) { 1498*3af08d82Slm66018 PR0("Descriptor ring virtual address is NULL"); 14990a55fbb7Slm66018 return (ENXIO); 15001ae08745Sheppo } 15011ae08745Sheppo 15021ae08745Sheppo 1503d10e4ef2Snarayan /* Initialize for valid message and mapped dring */ 15041ae08745Sheppo PR1("descriptor size = %u, dring length = %u", 15051ae08745Sheppo vd->descriptor_size, vd->dring_len); 15061ae08745Sheppo vd->initialized |= VD_DRING; 15071ae08745Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */ 15081ae08745Sheppo vd->dring = dring_minfo.vaddr; 15091ae08745Sheppo vd->descriptor_size = reg_msg->descriptor_size; 15101ae08745Sheppo vd->dring_len = reg_msg->num_descriptors; 15111ae08745Sheppo reg_msg->dring_ident = vd->dring_ident; 1512d10e4ef2Snarayan 1513d10e4ef2Snarayan /* 1514d10e4ef2Snarayan * Allocate and initialize a "shadow" array of data structures for 1515d10e4ef2Snarayan * tasks to process I/O requests in dring elements 1516d10e4ef2Snarayan */ 1517d10e4ef2Snarayan vd->dring_task = 1518d10e4ef2Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 1519d10e4ef2Snarayan for (int i = 0; i < vd->dring_len; i++) { 1520d10e4ef2Snarayan vd->dring_task[i].vd = vd; 1521d10e4ef2Snarayan vd->dring_task[i].index = i; 1522d10e4ef2Snarayan vd->dring_task[i].request = &VD_DRING_ELEM(i)->payload; 15234bac2208Snarayan 15244bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, 15254bac2208Snarayan &(vd->dring_task[i].mhdl)); 15264bac2208Snarayan if (status) { 1527*3af08d82Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status); 15284bac2208Snarayan return (ENXIO); 15294bac2208Snarayan } 1530*3af08d82Slm66018 1531*3af08d82Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 1532d10e4ef2Snarayan } 1533d10e4ef2Snarayan 15341ae08745Sheppo return (0); 15351ae08745Sheppo } 15361ae08745Sheppo 15371ae08745Sheppo static int 15381ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 15391ae08745Sheppo { 15401ae08745Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 15411ae08745Sheppo 15421ae08745Sheppo 15431ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 15441ae08745Sheppo 15451ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 15461ae08745Sheppo VIO_DRING_UNREG)) { 1547d10e4ef2Snarayan PR0("Message is not an unregister-dring message"); 1548d10e4ef2Snarayan return (ENOMSG); 15491ae08745Sheppo } 15501ae08745Sheppo 15511ae08745Sheppo if (msglen != sizeof (*unreg_msg)) { 1552*3af08d82Slm66018 PR0("Expected %lu-byte unregister-dring message; " 15531ae08745Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen); 15541ae08745Sheppo return (EBADMSG); 15551ae08745Sheppo } 15561ae08745Sheppo 15571ae08745Sheppo if (unreg_msg->dring_ident != vd->dring_ident) { 1558*3af08d82Slm66018 PR0("Expected dring ident %lu; received %lu", 15591ae08745Sheppo vd->dring_ident, unreg_msg->dring_ident); 15601ae08745Sheppo return (EBADMSG); 15611ae08745Sheppo } 15621ae08745Sheppo 15631ae08745Sheppo return (0); 15641ae08745Sheppo } 15651ae08745Sheppo 15661ae08745Sheppo static int 15671ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen) 15681ae08745Sheppo { 15691ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 15701ae08745Sheppo 1571d10e4ef2Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 1572d10e4ef2Snarayan PR0("Message is not an RDX message"); 1573d10e4ef2Snarayan return (ENOMSG); 1574d10e4ef2Snarayan } 15751ae08745Sheppo 15761ae08745Sheppo if (msglen != sizeof (vio_rdx_msg_t)) { 1577*3af08d82Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes", 15781ae08745Sheppo sizeof (vio_rdx_msg_t), msglen); 15791ae08745Sheppo return (EBADMSG); 15801ae08745Sheppo } 15811ae08745Sheppo 1582d10e4ef2Snarayan PR0("Valid RDX message"); 15831ae08745Sheppo return (0); 15841ae08745Sheppo } 15851ae08745Sheppo 15861ae08745Sheppo static int 15871ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num) 15881ae08745Sheppo { 15891ae08745Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 1590*3af08d82Slm66018 PR0("Received seq_num %lu; expected %lu", 15911ae08745Sheppo seq_num, (vd->seq_num + 1)); 1592*3af08d82Slm66018 PR0("initiating soft reset"); 1593d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 15941ae08745Sheppo return (1); 15951ae08745Sheppo } 15961ae08745Sheppo 15971ae08745Sheppo vd->seq_num = seq_num; 15981ae08745Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 15991ae08745Sheppo return (0); 16001ae08745Sheppo } 16011ae08745Sheppo 16021ae08745Sheppo /* 16031ae08745Sheppo * Return the expected size of an inband-descriptor message with all the 16041ae08745Sheppo * cookies it claims to include 16051ae08745Sheppo */ 16061ae08745Sheppo static size_t 16071ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg) 16081ae08745Sheppo { 16091ae08745Sheppo return ((sizeof (*msg)) + 16101ae08745Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 16111ae08745Sheppo } 16121ae08745Sheppo 16131ae08745Sheppo /* 16141ae08745Sheppo * Process an in-band descriptor message: used with clients like OBP, with 16151ae08745Sheppo * which vds exchanges descriptors within VIO message payloads, rather than 16161ae08745Sheppo * operating on them within a descriptor ring 16171ae08745Sheppo */ 16181ae08745Sheppo static int 1619*3af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 16201ae08745Sheppo { 16211ae08745Sheppo size_t expected; 16221ae08745Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 16231ae08745Sheppo 16241ae08745Sheppo 16251ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 16261ae08745Sheppo 16271ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 1628d10e4ef2Snarayan VIO_DESC_DATA)) { 1629d10e4ef2Snarayan PR1("Message is not an in-band-descriptor message"); 1630d10e4ef2Snarayan return (ENOMSG); 1631d10e4ef2Snarayan } 16321ae08745Sheppo 16331ae08745Sheppo if (msglen < sizeof (*desc_msg)) { 1634*3af08d82Slm66018 PR0("Expected at least %lu-byte descriptor message; " 16351ae08745Sheppo "received %lu bytes", sizeof (*desc_msg), msglen); 16361ae08745Sheppo return (EBADMSG); 16371ae08745Sheppo } 16381ae08745Sheppo 16391ae08745Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) { 1640*3af08d82Slm66018 PR0("Expected %lu-byte descriptor message; " 16411ae08745Sheppo "received %lu bytes", expected, msglen); 16421ae08745Sheppo return (EBADMSG); 16431ae08745Sheppo } 16441ae08745Sheppo 1645d10e4ef2Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 16461ae08745Sheppo return (EBADMSG); 16471ae08745Sheppo 1648d10e4ef2Snarayan /* 1649d10e4ef2Snarayan * Valid message: Set up the in-band descriptor task and process the 1650d10e4ef2Snarayan * request. Arrange to acknowledge the client's message, unless an 1651d10e4ef2Snarayan * error processing the descriptor task results in setting 1652d10e4ef2Snarayan * VIO_SUBTYPE_NACK 1653d10e4ef2Snarayan */ 1654d10e4ef2Snarayan PR1("Valid in-band-descriptor message"); 1655d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 1656*3af08d82Slm66018 1657*3af08d82Slm66018 ASSERT(vd->inband_task.msg != NULL); 1658*3af08d82Slm66018 1659*3af08d82Slm66018 bcopy(msg, vd->inband_task.msg, msglen); 1660d10e4ef2Snarayan vd->inband_task.msglen = msglen; 1661*3af08d82Slm66018 1662*3af08d82Slm66018 /* 1663*3af08d82Slm66018 * The task request is now the payload of the message 1664*3af08d82Slm66018 * that was just copied into the body of the task. 1665*3af08d82Slm66018 */ 1666*3af08d82Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 1667d10e4ef2Snarayan vd->inband_task.request = &desc_msg->payload; 1668*3af08d82Slm66018 1669d10e4ef2Snarayan return (vd_process_task(&vd->inband_task)); 16701ae08745Sheppo } 16711ae08745Sheppo 16721ae08745Sheppo static int 1673d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 1674*3af08d82Slm66018 vio_msg_t *msg, size_t msglen) 16751ae08745Sheppo { 16761ae08745Sheppo int status; 1677d10e4ef2Snarayan boolean_t ready; 1678d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 16791ae08745Sheppo 16801ae08745Sheppo 1681d10e4ef2Snarayan /* Accept the updated dring element */ 1682d10e4ef2Snarayan if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 1683*3af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", status); 16841ae08745Sheppo return (status); 16851ae08745Sheppo } 1686d10e4ef2Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY); 1687d10e4ef2Snarayan if (ready) { 1688d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED; 1689d10e4ef2Snarayan } else { 1690*3af08d82Slm66018 PR0("descriptor %u not ready", idx); 1691d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 1692d10e4ef2Snarayan } 1693d10e4ef2Snarayan if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 1694*3af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", status); 16951ae08745Sheppo return (status); 16961ae08745Sheppo } 1697d10e4ef2Snarayan if (!ready) 1698d10e4ef2Snarayan return (EBUSY); 16991ae08745Sheppo 17001ae08745Sheppo 1701d10e4ef2Snarayan /* Initialize a task and process the accepted element */ 1702d10e4ef2Snarayan PR1("Processing dring element %u", idx); 1703d10e4ef2Snarayan vd->dring_task[idx].type = type; 1704*3af08d82Slm66018 1705*3af08d82Slm66018 /* duplicate msg buf for cookies etc. */ 1706*3af08d82Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen); 1707*3af08d82Slm66018 1708d10e4ef2Snarayan vd->dring_task[idx].msglen = msglen; 1709d10e4ef2Snarayan if ((status = vd_process_task(&vd->dring_task[idx])) != EINPROGRESS) 1710d10e4ef2Snarayan status = vd_mark_elem_done(vd, idx, elem->payload.status); 17111ae08745Sheppo 17121ae08745Sheppo return (status); 17131ae08745Sheppo } 17141ae08745Sheppo 17151ae08745Sheppo static int 1716d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end, 1717*3af08d82Slm66018 vio_msg_t *msg, size_t msglen) 1718d10e4ef2Snarayan { 1719d10e4ef2Snarayan int i, n, nelem, status = 0; 1720d10e4ef2Snarayan boolean_t inprogress = B_FALSE; 1721d10e4ef2Snarayan vd_task_type_t type; 1722d10e4ef2Snarayan 1723d10e4ef2Snarayan 1724d10e4ef2Snarayan ASSERT(start >= 0); 1725d10e4ef2Snarayan ASSERT(end >= 0); 1726d10e4ef2Snarayan 1727d10e4ef2Snarayan /* 1728d10e4ef2Snarayan * Arrange to acknowledge the client's message, unless an error 1729d10e4ef2Snarayan * processing one of the dring elements results in setting 1730d10e4ef2Snarayan * VIO_SUBTYPE_NACK 1731d10e4ef2Snarayan */ 1732d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 1733d10e4ef2Snarayan 1734d10e4ef2Snarayan /* 1735d10e4ef2Snarayan * Process the dring elements in the range 1736d10e4ef2Snarayan */ 1737d10e4ef2Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 1738d10e4ef2Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 1739d10e4ef2Snarayan ((vio_dring_msg_t *)msg)->end_idx = i; 1740d10e4ef2Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 1741*3af08d82Slm66018 status = vd_process_element(vd, type, i, msg, msglen); 1742d10e4ef2Snarayan if (status == EINPROGRESS) 1743d10e4ef2Snarayan inprogress = B_TRUE; 1744d10e4ef2Snarayan else if (status != 0) 1745d10e4ef2Snarayan break; 1746d10e4ef2Snarayan } 1747d10e4ef2Snarayan 1748d10e4ef2Snarayan /* 1749d10e4ef2Snarayan * If some, but not all, operations of a multi-element range are in 1750d10e4ef2Snarayan * progress, wait for other operations to complete before returning 1751d10e4ef2Snarayan * (which will result in "ack" or "nack" of the message). Note that 1752d10e4ef2Snarayan * all outstanding operations will need to complete, not just the ones 1753d10e4ef2Snarayan * corresponding to the current range of dring elements; howevever, as 1754d10e4ef2Snarayan * this situation is an error case, performance is less critical. 1755d10e4ef2Snarayan */ 1756d10e4ef2Snarayan if ((nelem > 1) && (status != EINPROGRESS) && inprogress) 1757d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 1758d10e4ef2Snarayan 1759d10e4ef2Snarayan return (status); 1760d10e4ef2Snarayan } 1761d10e4ef2Snarayan 1762d10e4ef2Snarayan static int 1763*3af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 17641ae08745Sheppo { 17651ae08745Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 17661ae08745Sheppo 17671ae08745Sheppo 17681ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 17691ae08745Sheppo 17701ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 17711ae08745Sheppo VIO_DRING_DATA)) { 1772d10e4ef2Snarayan PR1("Message is not a dring-data message"); 1773d10e4ef2Snarayan return (ENOMSG); 17741ae08745Sheppo } 17751ae08745Sheppo 17761ae08745Sheppo if (msglen != sizeof (*dring_msg)) { 1777*3af08d82Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes", 17781ae08745Sheppo sizeof (*dring_msg), msglen); 17791ae08745Sheppo return (EBADMSG); 17801ae08745Sheppo } 17811ae08745Sheppo 1782d10e4ef2Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 17831ae08745Sheppo return (EBADMSG); 17841ae08745Sheppo 17851ae08745Sheppo if (dring_msg->dring_ident != vd->dring_ident) { 1786*3af08d82Slm66018 PR0("Expected dring ident %lu; received ident %lu", 17871ae08745Sheppo vd->dring_ident, dring_msg->dring_ident); 17881ae08745Sheppo return (EBADMSG); 17891ae08745Sheppo } 17901ae08745Sheppo 1791d10e4ef2Snarayan if (dring_msg->start_idx >= vd->dring_len) { 1792*3af08d82Slm66018 PR0("\"start_idx\" = %u; must be less than %u", 1793d10e4ef2Snarayan dring_msg->start_idx, vd->dring_len); 1794d10e4ef2Snarayan return (EBADMSG); 1795d10e4ef2Snarayan } 17961ae08745Sheppo 1797d10e4ef2Snarayan if ((dring_msg->end_idx < 0) || 1798d10e4ef2Snarayan (dring_msg->end_idx >= vd->dring_len)) { 1799*3af08d82Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 1800d10e4ef2Snarayan dring_msg->end_idx, vd->dring_len); 1801d10e4ef2Snarayan return (EBADMSG); 1802d10e4ef2Snarayan } 1803d10e4ef2Snarayan 1804d10e4ef2Snarayan /* Valid message; process range of updated dring elements */ 1805d10e4ef2Snarayan PR1("Processing descriptor range, start = %u, end = %u", 1806d10e4ef2Snarayan dring_msg->start_idx, dring_msg->end_idx); 1807d10e4ef2Snarayan return (vd_process_element_range(vd, dring_msg->start_idx, 1808*3af08d82Slm66018 dring_msg->end_idx, msg, msglen)); 18091ae08745Sheppo } 18101ae08745Sheppo 18111ae08745Sheppo static int 18121ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 18131ae08745Sheppo { 18141ae08745Sheppo int retry, status; 18151ae08745Sheppo size_t size = *nbytes; 18161ae08745Sheppo 18171ae08745Sheppo 18181ae08745Sheppo for (retry = 0, status = ETIMEDOUT; 18191ae08745Sheppo retry < vds_ldc_retries && status == ETIMEDOUT; 18201ae08745Sheppo retry++) { 18211ae08745Sheppo PR1("ldc_read() attempt %d", (retry + 1)); 18221ae08745Sheppo *nbytes = size; 18231ae08745Sheppo status = ldc_read(ldc_handle, msg, nbytes); 18241ae08745Sheppo } 18251ae08745Sheppo 1826*3af08d82Slm66018 if (status) { 1827*3af08d82Slm66018 PR0("ldc_read() returned errno %d", status); 1828*3af08d82Slm66018 if (status != ECONNRESET) 1829*3af08d82Slm66018 return (ENOMSG); 18301ae08745Sheppo return (status); 18311ae08745Sheppo } else if (*nbytes == 0) { 18321ae08745Sheppo PR1("ldc_read() returned 0 and no message read"); 18331ae08745Sheppo return (ENOMSG); 18341ae08745Sheppo } 18351ae08745Sheppo 18361ae08745Sheppo PR1("RCVD %lu-byte message", *nbytes); 18371ae08745Sheppo return (0); 18381ae08745Sheppo } 18391ae08745Sheppo 18401ae08745Sheppo static int 1841*3af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 18421ae08745Sheppo { 18431ae08745Sheppo int status; 18441ae08745Sheppo 18451ae08745Sheppo 18461ae08745Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 18471ae08745Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env); 1848*3af08d82Slm66018 #ifdef DEBUG 1849*3af08d82Slm66018 vd_decode_tag(msg); 1850*3af08d82Slm66018 #endif 18511ae08745Sheppo 18521ae08745Sheppo /* 18531ae08745Sheppo * Validate session ID up front, since it applies to all messages 18541ae08745Sheppo * once set 18551ae08745Sheppo */ 18561ae08745Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 1857*3af08d82Slm66018 PR0("Expected SID %u, received %u", vd->sid, 18581ae08745Sheppo msg->tag.vio_sid); 18591ae08745Sheppo return (EBADMSG); 18601ae08745Sheppo } 18611ae08745Sheppo 1862*3af08d82Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 18631ae08745Sheppo 18641ae08745Sheppo /* 18651ae08745Sheppo * Process the received message based on connection state 18661ae08745Sheppo */ 18671ae08745Sheppo switch (vd->state) { 18681ae08745Sheppo case VD_STATE_INIT: /* expect version message */ 18690a55fbb7Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 18701ae08745Sheppo return (status); 18711ae08745Sheppo 18721ae08745Sheppo /* Version negotiated, move to that state */ 18731ae08745Sheppo vd->state = VD_STATE_VER; 18741ae08745Sheppo return (0); 18751ae08745Sheppo 18761ae08745Sheppo case VD_STATE_VER: /* expect attribute message */ 18771ae08745Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 18781ae08745Sheppo return (status); 18791ae08745Sheppo 18801ae08745Sheppo /* Attributes exchanged, move to that state */ 18811ae08745Sheppo vd->state = VD_STATE_ATTR; 18821ae08745Sheppo return (0); 18831ae08745Sheppo 18841ae08745Sheppo case VD_STATE_ATTR: 18851ae08745Sheppo switch (vd->xfer_mode) { 18861ae08745Sheppo case VIO_DESC_MODE: /* expect RDX message */ 18871ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0) 18881ae08745Sheppo return (status); 18891ae08745Sheppo 18901ae08745Sheppo /* Ready to receive in-band descriptors */ 18911ae08745Sheppo vd->state = VD_STATE_DATA; 18921ae08745Sheppo return (0); 18931ae08745Sheppo 18941ae08745Sheppo case VIO_DRING_MODE: /* expect register-dring message */ 18951ae08745Sheppo if ((status = 18961ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 18971ae08745Sheppo return (status); 18981ae08745Sheppo 18991ae08745Sheppo /* One dring negotiated, move to that state */ 19001ae08745Sheppo vd->state = VD_STATE_DRING; 19011ae08745Sheppo return (0); 19021ae08745Sheppo 19031ae08745Sheppo default: 19041ae08745Sheppo ASSERT("Unsupported transfer mode"); 1905*3af08d82Slm66018 PR0("Unsupported transfer mode"); 19061ae08745Sheppo return (ENOTSUP); 19071ae08745Sheppo } 19081ae08745Sheppo 19091ae08745Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 19101ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) { 19111ae08745Sheppo /* Ready to receive data */ 19121ae08745Sheppo vd->state = VD_STATE_DATA; 19131ae08745Sheppo return (0); 19141ae08745Sheppo } else if (status != ENOMSG) { 19151ae08745Sheppo return (status); 19161ae08745Sheppo } 19171ae08745Sheppo 19181ae08745Sheppo 19191ae08745Sheppo /* 19201ae08745Sheppo * If another register-dring message is received, stay in 19211ae08745Sheppo * dring state in case the client sends RDX; although the 19221ae08745Sheppo * protocol allows multiple drings, this server does not 19231ae08745Sheppo * support using more than one 19241ae08745Sheppo */ 19251ae08745Sheppo if ((status = 19261ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 19271ae08745Sheppo return (status); 19281ae08745Sheppo 19291ae08745Sheppo /* 19301ae08745Sheppo * Acknowledge an unregister-dring message, but reset the 19311ae08745Sheppo * connection anyway: Although the protocol allows 19321ae08745Sheppo * unregistering drings, this server cannot serve a vdisk 19331ae08745Sheppo * without its only dring 19341ae08745Sheppo */ 19351ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 19361ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 19371ae08745Sheppo 19381ae08745Sheppo case VD_STATE_DATA: 19391ae08745Sheppo switch (vd->xfer_mode) { 19401ae08745Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */ 1941*3af08d82Slm66018 return (vd_process_desc_msg(vd, msg, msglen)); 19421ae08745Sheppo 19431ae08745Sheppo case VIO_DRING_MODE: /* expect dring-data or unreg-dring */ 19441ae08745Sheppo /* 19451ae08745Sheppo * Typically expect dring-data messages, so handle 19461ae08745Sheppo * them first 19471ae08745Sheppo */ 19481ae08745Sheppo if ((status = vd_process_dring_msg(vd, msg, 1949*3af08d82Slm66018 msglen)) != ENOMSG) 19501ae08745Sheppo return (status); 19511ae08745Sheppo 19521ae08745Sheppo /* 19531ae08745Sheppo * Acknowledge an unregister-dring message, but reset 19541ae08745Sheppo * the connection anyway: Although the protocol 19551ae08745Sheppo * allows unregistering drings, this server cannot 19561ae08745Sheppo * serve a vdisk without its only dring 19571ae08745Sheppo */ 19581ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 19591ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 19601ae08745Sheppo 19611ae08745Sheppo default: 19621ae08745Sheppo ASSERT("Unsupported transfer mode"); 1963*3af08d82Slm66018 PR0("Unsupported transfer mode"); 19641ae08745Sheppo return (ENOTSUP); 19651ae08745Sheppo } 19661ae08745Sheppo 19671ae08745Sheppo default: 19681ae08745Sheppo ASSERT("Invalid client connection state"); 1969*3af08d82Slm66018 PR0("Invalid client connection state"); 19701ae08745Sheppo return (ENOTSUP); 19711ae08745Sheppo } 19721ae08745Sheppo } 19731ae08745Sheppo 1974d10e4ef2Snarayan static int 1975*3af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 19761ae08745Sheppo { 19771ae08745Sheppo int status; 19781ae08745Sheppo boolean_t reset_ldc = B_FALSE; 19791ae08745Sheppo 19801ae08745Sheppo 19811ae08745Sheppo /* 19821ae08745Sheppo * Check that the message is at least big enough for a "tag", so that 19831ae08745Sheppo * message processing can proceed based on tag-specified message type 19841ae08745Sheppo */ 19851ae08745Sheppo if (msglen < sizeof (vio_msg_tag_t)) { 1986*3af08d82Slm66018 PR0("Received short (%lu-byte) message", msglen); 19871ae08745Sheppo /* Can't "nack" short message, so drop the big hammer */ 1988*3af08d82Slm66018 PR0("initiating full reset"); 1989d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 1990d10e4ef2Snarayan return (EBADMSG); 19911ae08745Sheppo } 19921ae08745Sheppo 19931ae08745Sheppo /* 19941ae08745Sheppo * Process the message 19951ae08745Sheppo */ 1996*3af08d82Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) { 19971ae08745Sheppo case 0: 19981ae08745Sheppo /* "ack" valid, successfully-processed messages */ 19991ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 20001ae08745Sheppo break; 20011ae08745Sheppo 2002d10e4ef2Snarayan case EINPROGRESS: 2003d10e4ef2Snarayan /* The completion handler will "ack" or "nack" the message */ 2004d10e4ef2Snarayan return (EINPROGRESS); 20051ae08745Sheppo case ENOMSG: 2006*3af08d82Slm66018 PR0("Received unexpected message"); 20071ae08745Sheppo _NOTE(FALLTHROUGH); 20081ae08745Sheppo case EBADMSG: 20091ae08745Sheppo case ENOTSUP: 20101ae08745Sheppo /* "nack" invalid messages */ 20111ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 20121ae08745Sheppo break; 20131ae08745Sheppo 20141ae08745Sheppo default: 20151ae08745Sheppo /* "nack" failed messages */ 20161ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 20171ae08745Sheppo /* An LDC error probably occurred, so try resetting it */ 20181ae08745Sheppo reset_ldc = B_TRUE; 20191ae08745Sheppo break; 20201ae08745Sheppo } 20211ae08745Sheppo 2022*3af08d82Slm66018 PR1("\tResulting in state %d (%s)", vd->state, 2023*3af08d82Slm66018 vd_decode_state(vd->state)); 2024*3af08d82Slm66018 2025d10e4ef2Snarayan /* Send the "ack" or "nack" to the client */ 20261ae08745Sheppo PR1("Sending %s", 20271ae08745Sheppo (msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 20281ae08745Sheppo if (send_msg(vd->ldc_handle, msg, msglen) != 0) 20291ae08745Sheppo reset_ldc = B_TRUE; 20301ae08745Sheppo 2031d10e4ef2Snarayan /* Arrange to reset the connection for nack'ed or failed messages */ 2032*3af08d82Slm66018 if ((status != 0) || reset_ldc) { 2033*3af08d82Slm66018 PR0("initiating %s reset", 2034*3af08d82Slm66018 (reset_ldc) ? "full" : "soft"); 2035d10e4ef2Snarayan vd_need_reset(vd, reset_ldc); 2036*3af08d82Slm66018 } 2037d10e4ef2Snarayan 2038d10e4ef2Snarayan return (status); 2039d10e4ef2Snarayan } 2040d10e4ef2Snarayan 2041d10e4ef2Snarayan static boolean_t 2042d10e4ef2Snarayan vd_enabled(vd_t *vd) 2043d10e4ef2Snarayan { 2044d10e4ef2Snarayan boolean_t enabled; 2045d10e4ef2Snarayan 2046d10e4ef2Snarayan 2047d10e4ef2Snarayan mutex_enter(&vd->lock); 2048d10e4ef2Snarayan enabled = vd->enabled; 2049d10e4ef2Snarayan mutex_exit(&vd->lock); 2050d10e4ef2Snarayan return (enabled); 20511ae08745Sheppo } 20521ae08745Sheppo 20531ae08745Sheppo static void 20540a55fbb7Slm66018 vd_recv_msg(void *arg) 20551ae08745Sheppo { 20561ae08745Sheppo vd_t *vd = (vd_t *)arg; 2057*3af08d82Slm66018 int rv = 0, status = 0; 20581ae08745Sheppo 20591ae08745Sheppo ASSERT(vd != NULL); 2060*3af08d82Slm66018 2061d10e4ef2Snarayan PR2("New task to receive incoming message(s)"); 2062*3af08d82Slm66018 2063*3af08d82Slm66018 2064d10e4ef2Snarayan while (vd_enabled(vd) && status == 0) { 2065d10e4ef2Snarayan size_t msglen, msgsize; 2066*3af08d82Slm66018 ldc_status_t lstatus; 2067d10e4ef2Snarayan 20680a55fbb7Slm66018 /* 2069d10e4ef2Snarayan * Receive and process a message 20700a55fbb7Slm66018 */ 2071d10e4ef2Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */ 2072*3af08d82Slm66018 2073*3af08d82Slm66018 /* 2074*3af08d82Slm66018 * check if channel is UP - else break out of loop 2075*3af08d82Slm66018 */ 2076*3af08d82Slm66018 status = ldc_status(vd->ldc_handle, &lstatus); 2077*3af08d82Slm66018 if (lstatus != LDC_UP) { 2078*3af08d82Slm66018 PR0("channel not up (status=%d), exiting recv loop\n", 2079*3af08d82Slm66018 lstatus); 2080*3af08d82Slm66018 break; 2081*3af08d82Slm66018 } 2082*3af08d82Slm66018 2083*3af08d82Slm66018 ASSERT(vd->max_msglen != 0); 2084*3af08d82Slm66018 2085d10e4ef2Snarayan msgsize = vd->max_msglen; /* stable copy for alloc/free */ 2086*3af08d82Slm66018 msglen = msgsize; /* actual len after recv_msg() */ 2087*3af08d82Slm66018 2088*3af08d82Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 2089*3af08d82Slm66018 switch (status) { 2090*3af08d82Slm66018 case 0: 2091*3af08d82Slm66018 rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp, 2092*3af08d82Slm66018 msglen); 2093*3af08d82Slm66018 /* check if max_msglen changed */ 2094*3af08d82Slm66018 if (msgsize != vd->max_msglen) { 2095*3af08d82Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 2096*3af08d82Slm66018 msgsize, vd->max_msglen); 2097*3af08d82Slm66018 kmem_free(vd->vio_msgp, msgsize); 2098*3af08d82Slm66018 vd->vio_msgp = 2099*3af08d82Slm66018 kmem_alloc(vd->max_msglen, KM_SLEEP); 2100*3af08d82Slm66018 } 2101*3af08d82Slm66018 if (rv == EINPROGRESS) 2102*3af08d82Slm66018 continue; 2103*3af08d82Slm66018 break; 2104*3af08d82Slm66018 2105*3af08d82Slm66018 case ENOMSG: 2106*3af08d82Slm66018 break; 2107*3af08d82Slm66018 2108*3af08d82Slm66018 case ECONNRESET: 2109*3af08d82Slm66018 PR0("initiating soft reset (ECONNRESET)\n"); 2110*3af08d82Slm66018 vd_need_reset(vd, B_FALSE); 2111*3af08d82Slm66018 status = 0; 2112*3af08d82Slm66018 break; 2113*3af08d82Slm66018 2114*3af08d82Slm66018 default: 2115d10e4ef2Snarayan /* Probably an LDC failure; arrange to reset it */ 2116*3af08d82Slm66018 PR0("initiating full reset (status=0x%x)", status); 2117d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 2118*3af08d82Slm66018 break; 21190a55fbb7Slm66018 } 21201ae08745Sheppo } 2121*3af08d82Slm66018 2122d10e4ef2Snarayan PR2("Task finished"); 21230a55fbb7Slm66018 } 21240a55fbb7Slm66018 21250a55fbb7Slm66018 static uint_t 21261ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg) 21271ae08745Sheppo { 21281ae08745Sheppo vd_t *vd = (vd_t *)(void *)arg; 2129*3af08d82Slm66018 int status; 21301ae08745Sheppo 21311ae08745Sheppo 21321ae08745Sheppo ASSERT(vd != NULL); 2133d10e4ef2Snarayan 2134d10e4ef2Snarayan if (!vd_enabled(vd)) 2135d10e4ef2Snarayan return (LDC_SUCCESS); 2136d10e4ef2Snarayan 2137*3af08d82Slm66018 if (event & LDC_EVT_DOWN) { 2138*3af08d82Slm66018 PRN("LDC_EVT_DOWN: LDC channel went down"); 2139*3af08d82Slm66018 2140*3af08d82Slm66018 vd_need_reset(vd, B_TRUE); 2141*3af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 2142*3af08d82Slm66018 DDI_SLEEP); 2143*3af08d82Slm66018 if (status == DDI_FAILURE) { 2144*3af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 2145*3af08d82Slm66018 vd_need_reset(vd, B_TRUE); 2146*3af08d82Slm66018 } 2147*3af08d82Slm66018 } 2148*3af08d82Slm66018 2149d10e4ef2Snarayan if (event & LDC_EVT_RESET) { 2150*3af08d82Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset"); 2151*3af08d82Slm66018 2152*3af08d82Slm66018 if (vd->state != VD_STATE_INIT) { 2153*3af08d82Slm66018 PR0("scheduling full reset"); 2154*3af08d82Slm66018 vd_need_reset(vd, B_FALSE); 2155*3af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 2156*3af08d82Slm66018 vd, DDI_SLEEP); 2157*3af08d82Slm66018 if (status == DDI_FAILURE) { 2158*3af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 2159*3af08d82Slm66018 vd_need_reset(vd, B_TRUE); 2160*3af08d82Slm66018 } 2161*3af08d82Slm66018 2162*3af08d82Slm66018 } else { 2163*3af08d82Slm66018 PR0("channel already reset, ignoring...\n"); 2164*3af08d82Slm66018 PR0("doing ldc up...\n"); 2165*3af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 2166*3af08d82Slm66018 } 2167*3af08d82Slm66018 2168d10e4ef2Snarayan return (LDC_SUCCESS); 2169d10e4ef2Snarayan } 2170d10e4ef2Snarayan 2171d10e4ef2Snarayan if (event & LDC_EVT_UP) { 2172*3af08d82Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state"); 2173*3af08d82Slm66018 PR0("initiating soft reset"); 2174d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 2175*3af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 2176*3af08d82Slm66018 vd, DDI_SLEEP); 2177*3af08d82Slm66018 if (status == DDI_FAILURE) { 2178*3af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 2179*3af08d82Slm66018 vd_need_reset(vd, B_TRUE); 2180*3af08d82Slm66018 return (LDC_SUCCESS); 2181*3af08d82Slm66018 } 2182d10e4ef2Snarayan } 2183d10e4ef2Snarayan 2184d10e4ef2Snarayan if (event & LDC_EVT_READ) { 2185d10e4ef2Snarayan int status; 2186d10e4ef2Snarayan 2187d10e4ef2Snarayan PR1("New data available"); 2188d10e4ef2Snarayan /* Queue a task to receive the new data */ 2189d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 2190d10e4ef2Snarayan DDI_SLEEP); 2191*3af08d82Slm66018 2192*3af08d82Slm66018 if (status == DDI_FAILURE) { 2193*3af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 2194*3af08d82Slm66018 vd_need_reset(vd, B_TRUE); 2195*3af08d82Slm66018 } 2196d10e4ef2Snarayan } 2197d10e4ef2Snarayan 2198d10e4ef2Snarayan return (LDC_SUCCESS); 21991ae08745Sheppo } 22001ae08745Sheppo 22011ae08745Sheppo static uint_t 22021ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 22031ae08745Sheppo { 22041ae08745Sheppo _NOTE(ARGUNUSED(key, val)) 22051ae08745Sheppo (*((uint_t *)arg))++; 22061ae08745Sheppo return (MH_WALK_TERMINATE); 22071ae08745Sheppo } 22081ae08745Sheppo 22091ae08745Sheppo 22101ae08745Sheppo static int 22111ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 22121ae08745Sheppo { 22131ae08745Sheppo uint_t vd_present = 0; 22141ae08745Sheppo minor_t instance; 22151ae08745Sheppo vds_t *vds; 22161ae08745Sheppo 22171ae08745Sheppo 22181ae08745Sheppo switch (cmd) { 22191ae08745Sheppo case DDI_DETACH: 22201ae08745Sheppo /* the real work happens below */ 22211ae08745Sheppo break; 22221ae08745Sheppo case DDI_SUSPEND: 2223d10e4ef2Snarayan PR0("No action required for DDI_SUSPEND"); 22241ae08745Sheppo return (DDI_SUCCESS); 22251ae08745Sheppo default: 2226*3af08d82Slm66018 PR0("Unrecognized \"cmd\""); 22271ae08745Sheppo return (DDI_FAILURE); 22281ae08745Sheppo } 22291ae08745Sheppo 22301ae08745Sheppo ASSERT(cmd == DDI_DETACH); 22311ae08745Sheppo instance = ddi_get_instance(dip); 22321ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 2233*3af08d82Slm66018 PR0("Could not get state for instance %u", instance); 22341ae08745Sheppo ddi_soft_state_free(vds_state, instance); 22351ae08745Sheppo return (DDI_FAILURE); 22361ae08745Sheppo } 22371ae08745Sheppo 22381ae08745Sheppo /* Do no detach when serving any vdisks */ 22391ae08745Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 22401ae08745Sheppo if (vd_present) { 22411ae08745Sheppo PR0("Not detaching because serving vdisks"); 22421ae08745Sheppo return (DDI_FAILURE); 22431ae08745Sheppo } 22441ae08745Sheppo 22451ae08745Sheppo PR0("Detaching"); 22461ae08745Sheppo if (vds->initialized & VDS_MDEG) 22471ae08745Sheppo (void) mdeg_unregister(vds->mdeg); 22481ae08745Sheppo if (vds->initialized & VDS_LDI) 22491ae08745Sheppo (void) ldi_ident_release(vds->ldi_ident); 22501ae08745Sheppo mod_hash_destroy_hash(vds->vd_table); 22511ae08745Sheppo ddi_soft_state_free(vds_state, instance); 22521ae08745Sheppo return (DDI_SUCCESS); 22531ae08745Sheppo } 22541ae08745Sheppo 22551ae08745Sheppo static boolean_t 22561ae08745Sheppo is_pseudo_device(dev_info_t *dip) 22571ae08745Sheppo { 22581ae08745Sheppo dev_info_t *parent, *root = ddi_root_node(); 22591ae08745Sheppo 22601ae08745Sheppo 22611ae08745Sheppo for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root); 22621ae08745Sheppo parent = ddi_get_parent(parent)) { 22631ae08745Sheppo if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0) 22641ae08745Sheppo return (B_TRUE); 22651ae08745Sheppo } 22661ae08745Sheppo 22671ae08745Sheppo return (B_FALSE); 22681ae08745Sheppo } 22691ae08745Sheppo 22701ae08745Sheppo static int 22710a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd) 22720a55fbb7Slm66018 { 22730a55fbb7Slm66018 int rval, status; 22740a55fbb7Slm66018 major_t major = getmajor(vd->dev[0]); 22750a55fbb7Slm66018 minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 22764bac2208Snarayan struct dk_minfo dk_minfo; 22770a55fbb7Slm66018 22784bac2208Snarayan /* 22794bac2208Snarayan * At this point, vdisk_size is set to the size of partition 2 but 22804bac2208Snarayan * this does not represent the size of the disk because partition 2 22814bac2208Snarayan * may not cover the entire disk and its size does not include reserved 22824bac2208Snarayan * blocks. So we update vdisk_size to be the size of the entire disk. 22834bac2208Snarayan */ 22844bac2208Snarayan if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 22854bac2208Snarayan (intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL), 22864bac2208Snarayan kcred, &rval)) != 0) { 22874bac2208Snarayan PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 22884bac2208Snarayan status); 22890a55fbb7Slm66018 return (status); 22900a55fbb7Slm66018 } 22914bac2208Snarayan vd->vdisk_size = dk_minfo.dki_capacity; 22920a55fbb7Slm66018 22930a55fbb7Slm66018 /* Set full-disk parameters */ 22940a55fbb7Slm66018 vd->vdisk_type = VD_DISK_TYPE_DISK; 22950a55fbb7Slm66018 vd->nslices = (sizeof (vd->dev))/(sizeof (vd->dev[0])); 22960a55fbb7Slm66018 22970a55fbb7Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */ 22980a55fbb7Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 22990a55fbb7Slm66018 vd->dev[0] = 0; 23000a55fbb7Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 23010a55fbb7Slm66018 vd->ldi_handle[0] = NULL; 23020a55fbb7Slm66018 23030a55fbb7Slm66018 /* Initialize device numbers for remaining slices and open them */ 23040a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 23050a55fbb7Slm66018 /* 23060a55fbb7Slm66018 * Skip the entire-disk slice, as it's already open and its 23070a55fbb7Slm66018 * device known 23080a55fbb7Slm66018 */ 23090a55fbb7Slm66018 if (slice == VD_ENTIRE_DISK_SLICE) 23100a55fbb7Slm66018 continue; 23110a55fbb7Slm66018 ASSERT(vd->dev[slice] == 0); 23120a55fbb7Slm66018 ASSERT(vd->ldi_handle[slice] == NULL); 23130a55fbb7Slm66018 23140a55fbb7Slm66018 /* 23150a55fbb7Slm66018 * Construct the device number for the current slice 23160a55fbb7Slm66018 */ 23170a55fbb7Slm66018 vd->dev[slice] = makedevice(major, (minor + slice)); 23180a55fbb7Slm66018 23190a55fbb7Slm66018 /* 23200a55fbb7Slm66018 * At least some underlying drivers refuse to open 23210a55fbb7Slm66018 * devices for (currently) zero-length slices, so skip 23220a55fbb7Slm66018 * them for now 23230a55fbb7Slm66018 */ 23244bac2208Snarayan if (vd->vtoc.v_part[slice].p_size == 0) { 23250a55fbb7Slm66018 PR0("Skipping zero-length slice %u", slice); 23260a55fbb7Slm66018 continue; 23270a55fbb7Slm66018 } 23280a55fbb7Slm66018 23290a55fbb7Slm66018 /* 23300a55fbb7Slm66018 * Open all non-empty slices of the disk to serve them to the 23310a55fbb7Slm66018 * client. Slices are opened exclusively to prevent other 23320a55fbb7Slm66018 * threads or processes in the service domain from performing 23330a55fbb7Slm66018 * I/O to slices being accessed by a client. Failure to open 23340a55fbb7Slm66018 * a slice results in vds not serving this disk, as the client 23350a55fbb7Slm66018 * could attempt (and should be able) to access any non-empty 23360a55fbb7Slm66018 * slice immediately. Any slices successfully opened before a 23370a55fbb7Slm66018 * failure will get closed by vds_destroy_vd() as a result of 23380a55fbb7Slm66018 * the error returned by this function. 23390a55fbb7Slm66018 */ 23400a55fbb7Slm66018 PR0("Opening device major %u, minor %u = slice %u", 23410a55fbb7Slm66018 major, minor, slice); 23420a55fbb7Slm66018 if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 23430a55fbb7Slm66018 vd_open_flags, kcred, &vd->ldi_handle[slice], 23440a55fbb7Slm66018 vd->vds->ldi_ident)) != 0) { 23450a55fbb7Slm66018 PRN("ldi_open_by_dev() returned errno %d " 23460a55fbb7Slm66018 "for slice %u", status, slice); 23470a55fbb7Slm66018 /* vds_destroy_vd() will close any open slices */ 23480a55fbb7Slm66018 return (status); 23490a55fbb7Slm66018 } 23500a55fbb7Slm66018 } 23510a55fbb7Slm66018 23520a55fbb7Slm66018 return (0); 23530a55fbb7Slm66018 } 23540a55fbb7Slm66018 23550a55fbb7Slm66018 static int 23564bac2208Snarayan vd_setup_partition_efi(vd_t *vd) 23574bac2208Snarayan { 23584bac2208Snarayan efi_gpt_t *gpt; 23594bac2208Snarayan efi_gpe_t *gpe; 23604bac2208Snarayan struct uuid uuid = EFI_RESERVED; 23614bac2208Snarayan uint32_t crc; 23624bac2208Snarayan int length; 23634bac2208Snarayan 23644bac2208Snarayan length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t); 23654bac2208Snarayan 23664bac2208Snarayan gpt = kmem_zalloc(length, KM_SLEEP); 23674bac2208Snarayan gpe = (efi_gpe_t *)(gpt + 1); 23684bac2208Snarayan 23694bac2208Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 23704bac2208Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 23714bac2208Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 23724bac2208Snarayan gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL); 23734bac2208Snarayan gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 23744bac2208Snarayan gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 23754bac2208Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 23764bac2208Snarayan 23774bac2208Snarayan UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid); 23784bac2208Snarayan gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA; 23794bac2208Snarayan gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA; 23804bac2208Snarayan 23814bac2208Snarayan CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table); 23824bac2208Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 23834bac2208Snarayan 23844bac2208Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 23854bac2208Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 23864bac2208Snarayan 23874bac2208Snarayan vd->dk_efi.dki_lba = 0; 23884bac2208Snarayan vd->dk_efi.dki_length = length; 23894bac2208Snarayan vd->dk_efi.dki_data = gpt; 23904bac2208Snarayan 23914bac2208Snarayan return (0); 23924bac2208Snarayan } 23934bac2208Snarayan 23944bac2208Snarayan static int 2395e1ebb9ecSlm66018 vd_setup_vd(char *device_path, vd_t *vd) 23961ae08745Sheppo { 2397e1ebb9ecSlm66018 int rval, status; 23981ae08745Sheppo dev_info_t *dip; 23991ae08745Sheppo struct dk_cinfo dk_cinfo; 24001ae08745Sheppo 24014bac2208Snarayan /* 24024bac2208Snarayan * We need to open with FNDELAY so that opening an empty partition 24034bac2208Snarayan * does not fail. 24044bac2208Snarayan */ 24054bac2208Snarayan if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY, 24064bac2208Snarayan kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) { 2407e1ebb9ecSlm66018 PRN("ldi_open_by_name(%s) = errno %d", device_path, status); 24080a55fbb7Slm66018 return (status); 24090a55fbb7Slm66018 } 24100a55fbb7Slm66018 24114bac2208Snarayan /* 24124bac2208Snarayan * nslices must be updated now so that vds_destroy_vd() will close 24134bac2208Snarayan * the slice we have just opened in case of an error. 24144bac2208Snarayan */ 24154bac2208Snarayan vd->nslices = 1; 24164bac2208Snarayan 2417e1ebb9ecSlm66018 /* Get device number and size of backing device */ 24180a55fbb7Slm66018 if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 24191ae08745Sheppo PRN("ldi_get_dev() returned errno %d for %s", 2420e1ebb9ecSlm66018 status, device_path); 24211ae08745Sheppo return (status); 24221ae08745Sheppo } 24230a55fbb7Slm66018 if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 2424e1ebb9ecSlm66018 PRN("ldi_get_size() failed for %s", device_path); 24251ae08745Sheppo return (EIO); 24261ae08745Sheppo } 2427e1ebb9ecSlm66018 vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 24281ae08745Sheppo 2429e1ebb9ecSlm66018 /* Verify backing device supports dk_cinfo, dk_geom, and vtoc */ 2430e1ebb9ecSlm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 2431e1ebb9ecSlm66018 (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred, 2432e1ebb9ecSlm66018 &rval)) != 0) { 2433e1ebb9ecSlm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 2434e1ebb9ecSlm66018 status, device_path); 2435e1ebb9ecSlm66018 return (status); 2436e1ebb9ecSlm66018 } 2437e1ebb9ecSlm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) { 2438e1ebb9ecSlm66018 PRN("slice %u >= maximum slice %u for %s", 2439e1ebb9ecSlm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path); 2440e1ebb9ecSlm66018 return (EIO); 2441e1ebb9ecSlm66018 } 24424bac2208Snarayan 24434bac2208Snarayan status = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, &vd->vdisk_label); 24444bac2208Snarayan 24454bac2208Snarayan if (status != 0) { 24464bac2208Snarayan PRN("vd_read_vtoc returned errno %d for %s", 2447e1ebb9ecSlm66018 status, device_path); 2448e1ebb9ecSlm66018 return (status); 2449e1ebb9ecSlm66018 } 24504bac2208Snarayan 24514bac2208Snarayan if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 24524bac2208Snarayan (status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 24534bac2208Snarayan (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL), 24544bac2208Snarayan kcred, &rval)) != 0) { 24554bac2208Snarayan PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s", 2456e1ebb9ecSlm66018 status, device_path); 2457e1ebb9ecSlm66018 return (status); 2458e1ebb9ecSlm66018 } 2459e1ebb9ecSlm66018 2460e1ebb9ecSlm66018 /* Store the device's max transfer size for return to the client */ 2461e1ebb9ecSlm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 2462e1ebb9ecSlm66018 2463e1ebb9ecSlm66018 2464e1ebb9ecSlm66018 /* Determine if backing device is a pseudo device */ 24651ae08745Sheppo if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]), 24661ae08745Sheppo dev_to_instance(vd->dev[0]), 0)) == NULL) { 2467e1ebb9ecSlm66018 PRN("%s is no longer accessible", device_path); 24681ae08745Sheppo return (EIO); 24691ae08745Sheppo } 24701ae08745Sheppo vd->pseudo = is_pseudo_device(dip); 24711ae08745Sheppo ddi_release_devi(dip); 24721ae08745Sheppo if (vd->pseudo) { 24731ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 24741ae08745Sheppo vd->nslices = 1; 24751ae08745Sheppo return (0); /* ...and we're done */ 24761ae08745Sheppo } 24771ae08745Sheppo 24781ae08745Sheppo 24790a55fbb7Slm66018 /* If slice is entire-disk slice, initialize for full disk */ 24800a55fbb7Slm66018 if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE) 24810a55fbb7Slm66018 return (vd_setup_full_disk(vd)); 24821ae08745Sheppo 24830a55fbb7Slm66018 2484e1ebb9ecSlm66018 /* Otherwise, we have a non-entire slice of a device */ 24851ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 24861ae08745Sheppo vd->nslices = 1; 24871ae08745Sheppo 24884bac2208Snarayan if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 24894bac2208Snarayan status = vd_setup_partition_efi(vd); 24904bac2208Snarayan return (status); 24914bac2208Snarayan } 24921ae08745Sheppo 2493e1ebb9ecSlm66018 /* Initialize dk_geom structure for single-slice device */ 24941ae08745Sheppo if (vd->dk_geom.dkg_nsect == 0) { 2495*3af08d82Slm66018 PR0("%s geometry claims 0 sectors per track", device_path); 24961ae08745Sheppo return (EIO); 24971ae08745Sheppo } 24981ae08745Sheppo if (vd->dk_geom.dkg_nhead == 0) { 2499*3af08d82Slm66018 PR0("%s geometry claims 0 heads", device_path); 25001ae08745Sheppo return (EIO); 25011ae08745Sheppo } 25021ae08745Sheppo vd->dk_geom.dkg_ncyl = 2503e1ebb9ecSlm66018 vd->vdisk_size/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead; 25041ae08745Sheppo vd->dk_geom.dkg_acyl = 0; 25051ae08745Sheppo vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 25061ae08745Sheppo 25071ae08745Sheppo 2508e1ebb9ecSlm66018 /* Initialize vtoc structure for single-slice device */ 25091ae08745Sheppo bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 25101ae08745Sheppo MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 25111ae08745Sheppo bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 25121ae08745Sheppo vd->vtoc.v_nparts = 1; 25131ae08745Sheppo vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 25141ae08745Sheppo vd->vtoc.v_part[0].p_flag = 0; 25151ae08745Sheppo vd->vtoc.v_part[0].p_start = 0; 2516e1ebb9ecSlm66018 vd->vtoc.v_part[0].p_size = vd->vdisk_size; 25171ae08745Sheppo bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 25181ae08745Sheppo MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel))); 25191ae08745Sheppo 25201ae08745Sheppo 25211ae08745Sheppo return (0); 25221ae08745Sheppo } 25231ae08745Sheppo 25241ae08745Sheppo static int 2525e1ebb9ecSlm66018 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id, 25261ae08745Sheppo vd_t **vdp) 25271ae08745Sheppo { 25281ae08745Sheppo char tq_name[TASKQ_NAMELEN]; 25290a55fbb7Slm66018 int status; 25301ae08745Sheppo ddi_iblock_cookie_t iblock = NULL; 25311ae08745Sheppo ldc_attr_t ldc_attr; 25321ae08745Sheppo vd_t *vd; 25331ae08745Sheppo 25341ae08745Sheppo 25351ae08745Sheppo ASSERT(vds != NULL); 2536e1ebb9ecSlm66018 ASSERT(device_path != NULL); 25371ae08745Sheppo ASSERT(vdp != NULL); 2538e1ebb9ecSlm66018 PR0("Adding vdisk for %s", device_path); 25391ae08745Sheppo 25401ae08745Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 25411ae08745Sheppo PRN("No memory for virtual disk"); 25421ae08745Sheppo return (EAGAIN); 25431ae08745Sheppo } 25441ae08745Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 25451ae08745Sheppo vd->vds = vds; 25461ae08745Sheppo 25471ae08745Sheppo 25480a55fbb7Slm66018 /* Open vdisk and initialize parameters */ 2549e1ebb9ecSlm66018 if ((status = vd_setup_vd(device_path, vd)) != 0) 25501ae08745Sheppo return (status); 25511ae08745Sheppo ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 25521ae08745Sheppo PR0("vdisk_type = %s, pseudo = %s, nslices = %u", 25531ae08745Sheppo ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 25541ae08745Sheppo (vd->pseudo ? "yes" : "no"), vd->nslices); 25551ae08745Sheppo 25561ae08745Sheppo 25571ae08745Sheppo /* Initialize locking */ 25581ae08745Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 25591ae08745Sheppo &iblock) != DDI_SUCCESS) { 25601ae08745Sheppo PRN("Could not get iblock cookie."); 25611ae08745Sheppo return (EIO); 25621ae08745Sheppo } 25631ae08745Sheppo 25641ae08745Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 25651ae08745Sheppo vd->initialized |= VD_LOCKING; 25661ae08745Sheppo 25671ae08745Sheppo 2568d10e4ef2Snarayan /* Create start and completion task queues for the vdisk */ 2569d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 25701ae08745Sheppo PR1("tq_name = %s", tq_name); 2571d10e4ef2Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 25721ae08745Sheppo TASKQ_DEFAULTPRI, 0)) == NULL) { 25731ae08745Sheppo PRN("Could not create task queue"); 25741ae08745Sheppo return (EIO); 25751ae08745Sheppo } 2576d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 2577d10e4ef2Snarayan PR1("tq_name = %s", tq_name); 2578d10e4ef2Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 2579d10e4ef2Snarayan TASKQ_DEFAULTPRI, 0)) == NULL) { 2580d10e4ef2Snarayan PRN("Could not create task queue"); 2581d10e4ef2Snarayan return (EIO); 2582d10e4ef2Snarayan } 2583d10e4ef2Snarayan vd->enabled = 1; /* before callback can dispatch to startq */ 25841ae08745Sheppo 25851ae08745Sheppo 25861ae08745Sheppo /* Bring up LDC */ 25871ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC; 25881ae08745Sheppo ldc_attr.instance = ddi_get_instance(vds->dip); 25891ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; 2590e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 25911ae08745Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 2592*3af08d82Slm66018 PR0("ldc_init(%lu) = errno %d", ldc_id, status); 25931ae08745Sheppo return (status); 25941ae08745Sheppo } 25951ae08745Sheppo vd->initialized |= VD_LDC; 25961ae08745Sheppo 25971ae08745Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 25981ae08745Sheppo (caddr_t)vd)) != 0) { 2599*3af08d82Slm66018 PR0("ldc_reg_callback() returned errno %d", status); 26001ae08745Sheppo return (status); 26011ae08745Sheppo } 26021ae08745Sheppo 26031ae08745Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) { 2604*3af08d82Slm66018 PR0("ldc_open() returned errno %d", status); 26051ae08745Sheppo return (status); 26061ae08745Sheppo } 26071ae08745Sheppo 2608*3af08d82Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) { 2609*3af08d82Slm66018 PRN("ldc_up() returned errno %d", status); 2610*3af08d82Slm66018 } 2611*3af08d82Slm66018 26124bac2208Snarayan /* Allocate the inband task memory handle */ 26134bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 26144bac2208Snarayan if (status) { 26154bac2208Snarayan PRN("ldc_mem_alloc_handle() returned err %d ", status); 26164bac2208Snarayan return (ENXIO); 26174bac2208Snarayan } 26181ae08745Sheppo 26191ae08745Sheppo /* Add the successfully-initialized vdisk to the server's table */ 26201ae08745Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 26211ae08745Sheppo PRN("Error adding vdisk ID %lu to table", id); 26221ae08745Sheppo return (EIO); 26231ae08745Sheppo } 26241ae08745Sheppo 2625*3af08d82Slm66018 /* Allocate the staging buffer */ 2626*3af08d82Slm66018 vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 2627*3af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 2628*3af08d82Slm66018 2629*3af08d82Slm66018 /* store initial state */ 2630*3af08d82Slm66018 vd->state = VD_STATE_INIT; 2631*3af08d82Slm66018 26321ae08745Sheppo return (0); 26331ae08745Sheppo } 26341ae08745Sheppo 2635*3af08d82Slm66018 static void 2636*3af08d82Slm66018 vd_free_dring_task(vd_t *vdp) 2637*3af08d82Slm66018 { 2638*3af08d82Slm66018 if (vdp->dring_task != NULL) { 2639*3af08d82Slm66018 ASSERT(vdp->dring_len != 0); 2640*3af08d82Slm66018 /* Free all dring_task memory handles */ 2641*3af08d82Slm66018 for (int i = 0; i < vdp->dring_len; i++) { 2642*3af08d82Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 2643*3af08d82Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 2644*3af08d82Slm66018 vdp->dring_task[i].msg = NULL; 2645*3af08d82Slm66018 } 2646*3af08d82Slm66018 kmem_free(vdp->dring_task, 2647*3af08d82Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len); 2648*3af08d82Slm66018 vdp->dring_task = NULL; 2649*3af08d82Slm66018 } 2650*3af08d82Slm66018 } 2651*3af08d82Slm66018 26521ae08745Sheppo /* 26531ae08745Sheppo * Destroy the state associated with a virtual disk 26541ae08745Sheppo */ 26551ae08745Sheppo static void 26561ae08745Sheppo vds_destroy_vd(void *arg) 26571ae08745Sheppo { 26581ae08745Sheppo vd_t *vd = (vd_t *)arg; 26591ae08745Sheppo 26601ae08745Sheppo 26611ae08745Sheppo if (vd == NULL) 26621ae08745Sheppo return; 26631ae08745Sheppo 2664d10e4ef2Snarayan PR0("Destroying vdisk state"); 2665d10e4ef2Snarayan 26664bac2208Snarayan if (vd->dk_efi.dki_data != NULL) 26674bac2208Snarayan kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length); 26684bac2208Snarayan 26691ae08745Sheppo /* Disable queuing requests for the vdisk */ 26701ae08745Sheppo if (vd->initialized & VD_LOCKING) { 26711ae08745Sheppo mutex_enter(&vd->lock); 26721ae08745Sheppo vd->enabled = 0; 26731ae08745Sheppo mutex_exit(&vd->lock); 26741ae08745Sheppo } 26751ae08745Sheppo 2676d10e4ef2Snarayan /* Drain and destroy start queue (*before* destroying completionq) */ 2677d10e4ef2Snarayan if (vd->startq != NULL) 2678d10e4ef2Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 2679d10e4ef2Snarayan 2680d10e4ef2Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */ 2681d10e4ef2Snarayan if (vd->completionq != NULL) 2682d10e4ef2Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 2683d10e4ef2Snarayan 2684*3af08d82Slm66018 vd_free_dring_task(vd); 2685*3af08d82Slm66018 2686*3af08d82Slm66018 /* Free the staging buffer for msgs */ 2687*3af08d82Slm66018 if (vd->vio_msgp != NULL) { 2688*3af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 2689*3af08d82Slm66018 vd->vio_msgp = NULL; 2690*3af08d82Slm66018 } 2691*3af08d82Slm66018 2692*3af08d82Slm66018 /* Free the inband message buffer */ 2693*3af08d82Slm66018 if (vd->inband_task.msg != NULL) { 2694*3af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 2695*3af08d82Slm66018 vd->inband_task.msg = NULL; 2696d10e4ef2Snarayan } 26971ae08745Sheppo 26984bac2208Snarayan /* Free the inband task memory handle */ 26994bac2208Snarayan (void) ldc_mem_free_handle(vd->inband_task.mhdl); 27004bac2208Snarayan 27011ae08745Sheppo /* Shut down LDC */ 27021ae08745Sheppo if (vd->initialized & VD_LDC) { 27031ae08745Sheppo if (vd->initialized & VD_DRING) 27041ae08745Sheppo (void) ldc_mem_dring_unmap(vd->dring_handle); 27051ae08745Sheppo (void) ldc_unreg_callback(vd->ldc_handle); 27061ae08745Sheppo (void) ldc_close(vd->ldc_handle); 27071ae08745Sheppo (void) ldc_fini(vd->ldc_handle); 27081ae08745Sheppo } 27091ae08745Sheppo 27101ae08745Sheppo /* Close any open backing-device slices */ 27111ae08745Sheppo for (uint_t slice = 0; slice < vd->nslices; slice++) { 27121ae08745Sheppo if (vd->ldi_handle[slice] != NULL) { 27131ae08745Sheppo PR0("Closing slice %u", slice); 27141ae08745Sheppo (void) ldi_close(vd->ldi_handle[slice], 27154bac2208Snarayan vd_open_flags | FNDELAY, kcred); 27161ae08745Sheppo } 27171ae08745Sheppo } 27181ae08745Sheppo 27191ae08745Sheppo /* Free lock */ 27201ae08745Sheppo if (vd->initialized & VD_LOCKING) 27211ae08745Sheppo mutex_destroy(&vd->lock); 27221ae08745Sheppo 27231ae08745Sheppo /* Finally, free the vdisk structure itself */ 27241ae08745Sheppo kmem_free(vd, sizeof (*vd)); 27251ae08745Sheppo } 27261ae08745Sheppo 27271ae08745Sheppo static int 2728e1ebb9ecSlm66018 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id) 27291ae08745Sheppo { 27301ae08745Sheppo int status; 27311ae08745Sheppo vd_t *vd = NULL; 27321ae08745Sheppo 27331ae08745Sheppo 2734e1ebb9ecSlm66018 if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0) 27351ae08745Sheppo vds_destroy_vd(vd); 27361ae08745Sheppo 27371ae08745Sheppo return (status); 27381ae08745Sheppo } 27391ae08745Sheppo 27401ae08745Sheppo static int 27411ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 27421ae08745Sheppo uint64_t *ldc_id) 27431ae08745Sheppo { 27441ae08745Sheppo int num_channels; 27451ae08745Sheppo 27461ae08745Sheppo 27471ae08745Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */ 27481ae08745Sheppo if ((num_channels = md_scan_dag(md, vd_node, 27491ae08745Sheppo md_find_name(md, VD_CHANNEL_ENDPOINT), 27501ae08745Sheppo md_find_name(md, "fwd"), channel)) <= 0) { 27511ae08745Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 27521ae08745Sheppo return (-1); 27531ae08745Sheppo } 27541ae08745Sheppo 27551ae08745Sheppo /* Get the "id" value for the first channel endpoint node */ 27561ae08745Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 27571ae08745Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk", 27581ae08745Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT); 27591ae08745Sheppo return (-1); 27601ae08745Sheppo } 27611ae08745Sheppo 27621ae08745Sheppo if (num_channels > 1) { 27631ae08745Sheppo PRN("Using ID of first of multiple channels for this vdisk"); 27641ae08745Sheppo } 27651ae08745Sheppo 27661ae08745Sheppo return (0); 27671ae08745Sheppo } 27681ae08745Sheppo 27691ae08745Sheppo static int 27701ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 27711ae08745Sheppo { 27721ae08745Sheppo int num_nodes, status; 27731ae08745Sheppo size_t size; 27741ae08745Sheppo mde_cookie_t *channel; 27751ae08745Sheppo 27761ae08745Sheppo 27771ae08745Sheppo if ((num_nodes = md_node_count(md)) <= 0) { 27781ae08745Sheppo PRN("Invalid node count in Machine Description subtree"); 27791ae08745Sheppo return (-1); 27801ae08745Sheppo } 27811ae08745Sheppo size = num_nodes*(sizeof (*channel)); 27821ae08745Sheppo channel = kmem_zalloc(size, KM_SLEEP); 27831ae08745Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 27841ae08745Sheppo kmem_free(channel, size); 27851ae08745Sheppo 27861ae08745Sheppo return (status); 27871ae08745Sheppo } 27881ae08745Sheppo 27891ae08745Sheppo static void 27901ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 27911ae08745Sheppo { 2792e1ebb9ecSlm66018 char *device_path = NULL; 27931ae08745Sheppo uint64_t id = 0, ldc_id = 0; 27941ae08745Sheppo 27951ae08745Sheppo 27961ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 27971ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 27981ae08745Sheppo return; 27991ae08745Sheppo } 28001ae08745Sheppo PR0("Adding vdisk ID %lu", id); 28011ae08745Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 2802e1ebb9ecSlm66018 &device_path) != 0) { 28031ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 28041ae08745Sheppo return; 28051ae08745Sheppo } 28061ae08745Sheppo 28071ae08745Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 28081ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", id); 28091ae08745Sheppo return; 28101ae08745Sheppo } 28111ae08745Sheppo 2812e1ebb9ecSlm66018 if (vds_init_vd(vds, id, device_path, ldc_id) != 0) { 28131ae08745Sheppo PRN("Failed to add vdisk ID %lu", id); 28141ae08745Sheppo return; 28151ae08745Sheppo } 28161ae08745Sheppo } 28171ae08745Sheppo 28181ae08745Sheppo static void 28191ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 28201ae08745Sheppo { 28211ae08745Sheppo uint64_t id = 0; 28221ae08745Sheppo 28231ae08745Sheppo 28241ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 28251ae08745Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node", 28261ae08745Sheppo VD_ID_PROP); 28271ae08745Sheppo return; 28281ae08745Sheppo } 28291ae08745Sheppo PR0("Removing vdisk ID %lu", id); 28301ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 28311ae08745Sheppo PRN("No vdisk entry found for vdisk ID %lu", id); 28321ae08745Sheppo } 28331ae08745Sheppo 28341ae08745Sheppo static void 28351ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 28361ae08745Sheppo md_t *curr_md, mde_cookie_t curr_vd_node) 28371ae08745Sheppo { 28381ae08745Sheppo char *curr_dev, *prev_dev; 28391ae08745Sheppo uint64_t curr_id = 0, curr_ldc_id = 0; 28401ae08745Sheppo uint64_t prev_id = 0, prev_ldc_id = 0; 28411ae08745Sheppo size_t len; 28421ae08745Sheppo 28431ae08745Sheppo 28441ae08745Sheppo /* Validate that vdisk ID has not changed */ 28451ae08745Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 28461ae08745Sheppo PRN("Error getting previous vdisk \"%s\" property", 28471ae08745Sheppo VD_ID_PROP); 28481ae08745Sheppo return; 28491ae08745Sheppo } 28501ae08745Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 28511ae08745Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 28521ae08745Sheppo return; 28531ae08745Sheppo } 28541ae08745Sheppo if (curr_id != prev_id) { 28551ae08745Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu", 28561ae08745Sheppo prev_id, curr_id); 28571ae08745Sheppo return; 28581ae08745Sheppo } 28591ae08745Sheppo 28601ae08745Sheppo /* Validate that LDC ID has not changed */ 28611ae08745Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 28621ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id); 28631ae08745Sheppo return; 28641ae08745Sheppo } 28651ae08745Sheppo 28661ae08745Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 28671ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id); 28681ae08745Sheppo return; 28691ae08745Sheppo } 28701ae08745Sheppo if (curr_ldc_id != prev_ldc_id) { 28710a55fbb7Slm66018 _NOTE(NOTREACHED); /* lint is confused */ 28721ae08745Sheppo PRN("Not changing vdisk: " 28731ae08745Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 28741ae08745Sheppo return; 28751ae08745Sheppo } 28761ae08745Sheppo 28771ae08745Sheppo /* Determine whether device path has changed */ 28781ae08745Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 28791ae08745Sheppo &prev_dev) != 0) { 28801ae08745Sheppo PRN("Error getting previous vdisk \"%s\"", 28811ae08745Sheppo VD_BLOCK_DEVICE_PROP); 28821ae08745Sheppo return; 28831ae08745Sheppo } 28841ae08745Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 28851ae08745Sheppo &curr_dev) != 0) { 28861ae08745Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 28871ae08745Sheppo return; 28881ae08745Sheppo } 28891ae08745Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 28901ae08745Sheppo (strncmp(curr_dev, prev_dev, len) == 0)) 28911ae08745Sheppo return; /* no relevant (supported) change */ 28921ae08745Sheppo 28931ae08745Sheppo PR0("Changing vdisk ID %lu", prev_id); 2894*3af08d82Slm66018 28951ae08745Sheppo /* Remove old state, which will close vdisk and reset */ 28961ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 28971ae08745Sheppo PRN("No entry found for vdisk ID %lu", prev_id); 2898*3af08d82Slm66018 28991ae08745Sheppo /* Re-initialize vdisk with new state */ 29001ae08745Sheppo if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) { 29011ae08745Sheppo PRN("Failed to change vdisk ID %lu", curr_id); 29021ae08745Sheppo return; 29031ae08745Sheppo } 29041ae08745Sheppo } 29051ae08745Sheppo 29061ae08745Sheppo static int 29071ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md) 29081ae08745Sheppo { 29091ae08745Sheppo int i; 29101ae08745Sheppo vds_t *vds = arg; 29111ae08745Sheppo 29121ae08745Sheppo 29131ae08745Sheppo if (md == NULL) 29141ae08745Sheppo return (MDEG_FAILURE); 29151ae08745Sheppo ASSERT(vds != NULL); 29161ae08745Sheppo 29171ae08745Sheppo for (i = 0; i < md->removed.nelem; i++) 29181ae08745Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 29191ae08745Sheppo for (i = 0; i < md->match_curr.nelem; i++) 29201ae08745Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 29211ae08745Sheppo md->match_curr.mdp, md->match_curr.mdep[i]); 29221ae08745Sheppo for (i = 0; i < md->added.nelem; i++) 29231ae08745Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 29241ae08745Sheppo 29251ae08745Sheppo return (MDEG_SUCCESS); 29261ae08745Sheppo } 29271ae08745Sheppo 29281ae08745Sheppo static int 29291ae08745Sheppo vds_do_attach(dev_info_t *dip) 29301ae08745Sheppo { 29311ae08745Sheppo static char reg_prop[] = "reg"; /* devinfo ID prop */ 29321ae08745Sheppo 29331ae08745Sheppo /* MDEG specification for a (particular) vds node */ 29341ae08745Sheppo static mdeg_prop_spec_t vds_prop_spec[] = { 29351ae08745Sheppo {MDET_PROP_STR, "name", {VDS_NAME}}, 29361ae08745Sheppo {MDET_PROP_VAL, "cfg-handle", {0}}, 29371ae08745Sheppo {MDET_LIST_END, NULL, {0}}}; 29381ae08745Sheppo static mdeg_node_spec_t vds_spec = {"virtual-device", vds_prop_spec}; 29391ae08745Sheppo 29401ae08745Sheppo /* MDEG specification for matching a vd node */ 29411ae08745Sheppo static md_prop_match_t vd_prop_spec[] = { 29421ae08745Sheppo {MDET_PROP_VAL, VD_ID_PROP}, 29431ae08745Sheppo {MDET_LIST_END, NULL}}; 29441ae08745Sheppo static mdeg_node_match_t vd_spec = {"virtual-device-port", 29451ae08745Sheppo vd_prop_spec}; 29461ae08745Sheppo 29471ae08745Sheppo int status; 29481ae08745Sheppo uint64_t cfg_handle; 29491ae08745Sheppo minor_t instance = ddi_get_instance(dip); 29501ae08745Sheppo vds_t *vds; 29511ae08745Sheppo 29521ae08745Sheppo 29531ae08745Sheppo /* 29541ae08745Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's 29551ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 29561ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 29571ae08745Sheppo * the "reg" property on the node in the device tree it builds from 29581ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 29591ae08745Sheppo * "reg" property value to uniquely identify this device instance when 29601ae08745Sheppo * registering with the MD event-generation framework. If the "reg" 29611ae08745Sheppo * property cannot be found, the device tree state is presumably so 29621ae08745Sheppo * broken that there is no point in continuing. 29631ae08745Sheppo */ 29641ae08745Sheppo if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, reg_prop)) { 29651ae08745Sheppo PRN("vds \"%s\" property does not exist", reg_prop); 29661ae08745Sheppo return (DDI_FAILURE); 29671ae08745Sheppo } 29681ae08745Sheppo 29691ae08745Sheppo /* Get the MD instance for later MDEG registration */ 29701ae08745Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 29711ae08745Sheppo reg_prop, -1); 29721ae08745Sheppo 29731ae08745Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 29741ae08745Sheppo PRN("Could not allocate state for instance %u", instance); 29751ae08745Sheppo return (DDI_FAILURE); 29761ae08745Sheppo } 29771ae08745Sheppo 29781ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 29791ae08745Sheppo PRN("Could not get state for instance %u", instance); 29801ae08745Sheppo ddi_soft_state_free(vds_state, instance); 29811ae08745Sheppo return (DDI_FAILURE); 29821ae08745Sheppo } 29831ae08745Sheppo 29841ae08745Sheppo 29851ae08745Sheppo vds->dip = dip; 29861ae08745Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 29871ae08745Sheppo vds_destroy_vd, 29881ae08745Sheppo sizeof (void *)); 29891ae08745Sheppo ASSERT(vds->vd_table != NULL); 29901ae08745Sheppo 29911ae08745Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 29921ae08745Sheppo PRN("ldi_ident_from_dip() returned errno %d", status); 29931ae08745Sheppo return (DDI_FAILURE); 29941ae08745Sheppo } 29951ae08745Sheppo vds->initialized |= VDS_LDI; 29961ae08745Sheppo 29971ae08745Sheppo /* Register for MD updates */ 29981ae08745Sheppo vds_prop_spec[1].ps_val = cfg_handle; 29991ae08745Sheppo if (mdeg_register(&vds_spec, &vd_spec, vds_process_md, vds, 30001ae08745Sheppo &vds->mdeg) != MDEG_SUCCESS) { 30011ae08745Sheppo PRN("Unable to register for MD updates"); 30021ae08745Sheppo return (DDI_FAILURE); 30031ae08745Sheppo } 30041ae08745Sheppo vds->initialized |= VDS_MDEG; 30051ae08745Sheppo 30060a55fbb7Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */ 30070a55fbb7Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 30080a55fbb7Slm66018 DDI_PROP_SUCCESS) { 30090a55fbb7Slm66018 PRN("failed to set \"%s\" property for instance %u", 30100a55fbb7Slm66018 DDI_NO_AUTODETACH, instance); 30110a55fbb7Slm66018 } 30120a55fbb7Slm66018 30131ae08745Sheppo ddi_report_dev(dip); 30141ae08745Sheppo return (DDI_SUCCESS); 30151ae08745Sheppo } 30161ae08745Sheppo 30171ae08745Sheppo static int 30181ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 30191ae08745Sheppo { 30201ae08745Sheppo int status; 30211ae08745Sheppo 30221ae08745Sheppo switch (cmd) { 30231ae08745Sheppo case DDI_ATTACH: 3024d10e4ef2Snarayan PR0("Attaching"); 30251ae08745Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 30261ae08745Sheppo (void) vds_detach(dip, DDI_DETACH); 30271ae08745Sheppo return (status); 30281ae08745Sheppo case DDI_RESUME: 3029d10e4ef2Snarayan PR0("No action required for DDI_RESUME"); 30301ae08745Sheppo return (DDI_SUCCESS); 30311ae08745Sheppo default: 30321ae08745Sheppo return (DDI_FAILURE); 30331ae08745Sheppo } 30341ae08745Sheppo } 30351ae08745Sheppo 30361ae08745Sheppo static struct dev_ops vds_ops = { 30371ae08745Sheppo DEVO_REV, /* devo_rev */ 30381ae08745Sheppo 0, /* devo_refcnt */ 30391ae08745Sheppo ddi_no_info, /* devo_getinfo */ 30401ae08745Sheppo nulldev, /* devo_identify */ 30411ae08745Sheppo nulldev, /* devo_probe */ 30421ae08745Sheppo vds_attach, /* devo_attach */ 30431ae08745Sheppo vds_detach, /* devo_detach */ 30441ae08745Sheppo nodev, /* devo_reset */ 30451ae08745Sheppo NULL, /* devo_cb_ops */ 30461ae08745Sheppo NULL, /* devo_bus_ops */ 30471ae08745Sheppo nulldev /* devo_power */ 30481ae08745Sheppo }; 30491ae08745Sheppo 30501ae08745Sheppo static struct modldrv modldrv = { 30511ae08745Sheppo &mod_driverops, 30521ae08745Sheppo "virtual disk server v%I%", 30531ae08745Sheppo &vds_ops, 30541ae08745Sheppo }; 30551ae08745Sheppo 30561ae08745Sheppo static struct modlinkage modlinkage = { 30571ae08745Sheppo MODREV_1, 30581ae08745Sheppo &modldrv, 30591ae08745Sheppo NULL 30601ae08745Sheppo }; 30611ae08745Sheppo 30621ae08745Sheppo 30631ae08745Sheppo int 30641ae08745Sheppo _init(void) 30651ae08745Sheppo { 30661ae08745Sheppo int i, status; 30671ae08745Sheppo 3068d10e4ef2Snarayan 30691ae08745Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 30701ae08745Sheppo return (status); 30711ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) { 30721ae08745Sheppo ddi_soft_state_fini(&vds_state); 30731ae08745Sheppo return (status); 30741ae08745Sheppo } 30751ae08745Sheppo 30761ae08745Sheppo /* Fill in the bit-mask of server-supported operations */ 30771ae08745Sheppo for (i = 0; i < vds_noperations; i++) 30781ae08745Sheppo vds_operations |= 1 << (vds_operation[i].operation - 1); 30791ae08745Sheppo 30801ae08745Sheppo return (0); 30811ae08745Sheppo } 30821ae08745Sheppo 30831ae08745Sheppo int 30841ae08745Sheppo _info(struct modinfo *modinfop) 30851ae08745Sheppo { 30861ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 30871ae08745Sheppo } 30881ae08745Sheppo 30891ae08745Sheppo int 30901ae08745Sheppo _fini(void) 30911ae08745Sheppo { 30921ae08745Sheppo int status; 30931ae08745Sheppo 3094d10e4ef2Snarayan 30951ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 30961ae08745Sheppo return (status); 30971ae08745Sheppo ddi_soft_state_fini(&vds_state); 30981ae08745Sheppo return (0); 30991ae08745Sheppo } 3100