1*1ae08745Sheppo /* 2*1ae08745Sheppo * CDDL HEADER START 3*1ae08745Sheppo * 4*1ae08745Sheppo * The contents of this file are subject to the terms of the 5*1ae08745Sheppo * Common Development and Distribution License (the "License"). 6*1ae08745Sheppo * You may not use this file except in compliance with the License. 7*1ae08745Sheppo * 8*1ae08745Sheppo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*1ae08745Sheppo * or http://www.opensolaris.org/os/licensing. 10*1ae08745Sheppo * See the License for the specific language governing permissions 11*1ae08745Sheppo * and limitations under the License. 12*1ae08745Sheppo * 13*1ae08745Sheppo * When distributing Covered Code, include this CDDL HEADER in each 14*1ae08745Sheppo * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*1ae08745Sheppo * If applicable, add the following below this CDDL HEADER, with the 16*1ae08745Sheppo * fields enclosed by brackets "[]" replaced with your own identifying 17*1ae08745Sheppo * information: Portions Copyright [yyyy] [name of copyright owner] 18*1ae08745Sheppo * 19*1ae08745Sheppo * CDDL HEADER END 20*1ae08745Sheppo */ 21*1ae08745Sheppo 22*1ae08745Sheppo /* 23*1ae08745Sheppo * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24*1ae08745Sheppo * Use is subject to license terms. 25*1ae08745Sheppo */ 26*1ae08745Sheppo 27*1ae08745Sheppo #ifndef _VDC_H 28*1ae08745Sheppo #define _VDC_H 29*1ae08745Sheppo 30*1ae08745Sheppo #pragma ident "%Z%%M% %I% %E% SMI" 31*1ae08745Sheppo 32*1ae08745Sheppo /* 33*1ae08745Sheppo * Virtual disk client implementation definitions 34*1ae08745Sheppo */ 35*1ae08745Sheppo 36*1ae08745Sheppo #include <sys/sysmacros.h> 37*1ae08745Sheppo #include <sys/note.h> 38*1ae08745Sheppo 39*1ae08745Sheppo #include <sys/ldc.h> 40*1ae08745Sheppo #include <sys/vio_mailbox.h> 41*1ae08745Sheppo #include <sys/vdsk_mailbox.h> 42*1ae08745Sheppo #include <sys/vdsk_common.h> 43*1ae08745Sheppo 44*1ae08745Sheppo #ifdef __cplusplus 45*1ae08745Sheppo extern "C" { 46*1ae08745Sheppo #endif 47*1ae08745Sheppo 48*1ae08745Sheppo #define VDC_DRIVER_NAME "vdc" 49*1ae08745Sheppo 50*1ae08745Sheppo /* 51*1ae08745Sheppo * Bit-field values to indicate if parts of the vdc driver are initialised. 52*1ae08745Sheppo */ 53*1ae08745Sheppo #define VDC_SOFT_STATE 0x0001 54*1ae08745Sheppo #define VDC_LOCKS 0x0002 55*1ae08745Sheppo #define VDC_MINOR 0x0004 56*1ae08745Sheppo #define VDC_THREAD 0x0008 57*1ae08745Sheppo #define VDC_LDC 0x0010 58*1ae08745Sheppo #define VDC_LDC_INIT 0x0020 59*1ae08745Sheppo #define VDC_LDC_CB 0x0040 60*1ae08745Sheppo #define VDC_LDC_OPEN 0x0080 61*1ae08745Sheppo #define VDC_DRING_INIT 0x0100 /* The DRing was created */ 62*1ae08745Sheppo #define VDC_DRING_BOUND 0x0200 /* The DRing was bound to an LDC channel */ 63*1ae08745Sheppo #define VDC_DRING_LOCAL 0x0400 /* The local private DRing was allocated */ 64*1ae08745Sheppo #define VDC_DRING_ENTRY 0x0800 /* At least one DRing entry was initialised */ 65*1ae08745Sheppo #define VDC_DRING (VDC_DRING_INIT | VDC_DRING_BOUND | \ 66*1ae08745Sheppo VDC_DRING_LOCAL | VDC_DRING_ENTRY) 67*1ae08745Sheppo #define VDC_HANDSHAKE 0x1000 /* Indicates if a handshake is in progress */ 68*1ae08745Sheppo #define VDC_HANDSHAKE_STOP 0x2000 /* stop further handshakes */ 69*1ae08745Sheppo 70*1ae08745Sheppo /* 71*1ae08745Sheppo * Bit-field values to indicate status of local DRing entry 72*1ae08745Sheppo * 73*1ae08745Sheppo * The lowest 8 bits are reserved for the DRing state. 74*1ae08745Sheppo */ 75*1ae08745Sheppo #define VDC_ALLOC_HANDLE 0x10 76*1ae08745Sheppo 77*1ae08745Sheppo /* 78*1ae08745Sheppo * Definitions of strings to be used to create device node properties. 79*1ae08745Sheppo * (vdc uses the capitalised versions of these properties as they are 64-bit) 80*1ae08745Sheppo */ 81*1ae08745Sheppo #define VDC_NBLOCKS_PROP_NAME "Nblocks" 82*1ae08745Sheppo #define VDC_SIZE_PROP_NAME "Size" 83*1ae08745Sheppo 84*1ae08745Sheppo /* 85*1ae08745Sheppo * Definitions of MD nodes/properties. 86*1ae08745Sheppo */ 87*1ae08745Sheppo #define VDC_MD_CHAN_NAME "channel-endpoint" 88*1ae08745Sheppo #define VDC_MD_VDEV_NAME "virtual-device" 89*1ae08745Sheppo #define VDC_MD_DISK_NAME "disk" 90*1ae08745Sheppo #define VDC_MD_CFG_HDL "cfg-handle" 91*1ae08745Sheppo #define VDC_ID_PROP "id" 92*1ae08745Sheppo 93*1ae08745Sheppo /* 94*1ae08745Sheppo * Scheme to store the instance number and the slice number in the minor number. 95*1ae08745Sheppo * (Uses the same format and definitions as the sd(7D) driver) 96*1ae08745Sheppo */ 97*1ae08745Sheppo #define VD_MAKE_DEV(instance, minor) ((instance << SDUNIT_SHIFT) | minor) 98*1ae08745Sheppo 99*1ae08745Sheppo /* 100*1ae08745Sheppo * variables controlling how long to wait before timing out and how many 101*1ae08745Sheppo * retries to attempt before giving up when communicating with vds. 102*1ae08745Sheppo */ 103*1ae08745Sheppo #define VDC_RETRIES 10 104*1ae08745Sheppo 105*1ae08745Sheppo #define VDC_USEC_TIMEOUT_MIN (30 * MICROSEC) /* 30 sec */ 106*1ae08745Sheppo 107*1ae08745Sheppo #define VD_GET_TIMEOUT_HZ(mul) \ 108*1ae08745Sheppo (ddi_get_lbolt() + (vdc_hz_timeout * MAX(1, mul))) 109*1ae08745Sheppo 110*1ae08745Sheppo /* 111*1ae08745Sheppo * Macros to manipulate Descriptor Ring variables in the soft state 112*1ae08745Sheppo * structure. 113*1ae08745Sheppo */ 114*1ae08745Sheppo #define VDC_GET_NEXT_REQ_ID(vdc) ((vdc->req_id)++) 115*1ae08745Sheppo 116*1ae08745Sheppo #define VDC_GET_DRING_ENTRY_PTR(vdc, idx) \ 117*1ae08745Sheppo (vd_dring_entry_t *)(vdc->dring_mem_info.vaddr + \ 118*1ae08745Sheppo (idx * vdc->dring_entry_size)) 119*1ae08745Sheppo 120*1ae08745Sheppo #define VDC_MARK_DRING_ENTRY_FREE(vdc, idx) \ 121*1ae08745Sheppo { \ 122*1ae08745Sheppo vd_dring_entry_t *dep = NULL; \ 123*1ae08745Sheppo ASSERT(vdc != NULL); \ 124*1ae08745Sheppo ASSERT((idx >= 0) && (idx < VD_DRING_LEN)); \ 125*1ae08745Sheppo ASSERT(vdc->dring_mem_info.vaddr != NULL); \ 126*1ae08745Sheppo dep = (vd_dring_entry_t *)(vdc->dring_mem_info.vaddr + \ 127*1ae08745Sheppo (idx * vdc->dring_entry_size)); \ 128*1ae08745Sheppo ASSERT(dep != NULL); \ 129*1ae08745Sheppo dep->hdr.dstate = VIO_DESC_FREE; \ 130*1ae08745Sheppo } 131*1ae08745Sheppo 132*1ae08745Sheppo /* Initialise the Session ID and Sequence Num in the DRing msg */ 133*1ae08745Sheppo #define VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdc) \ 134*1ae08745Sheppo ASSERT(vdc != NULL); \ 135*1ae08745Sheppo dmsg.tag.vio_sid = vdc->session_id; \ 136*1ae08745Sheppo dmsg.seq_num = ++(vdc->seq_num); 137*1ae08745Sheppo 138*1ae08745Sheppo /* 139*1ae08745Sheppo * The states the message processing thread can be in. 140*1ae08745Sheppo */ 141*1ae08745Sheppo typedef enum vdc_thr_state { 142*1ae08745Sheppo VDC_THR_RUNNING, /* thread is running & ready to process */ 143*1ae08745Sheppo VDC_THR_STOP, /* The detach func signals the thread to stop */ 144*1ae08745Sheppo VDC_THR_DONE /* Thread has exited */ 145*1ae08745Sheppo } vdc_thr_state_t; 146*1ae08745Sheppo 147*1ae08745Sheppo /* 148*1ae08745Sheppo * Local Descriptor Ring entry 149*1ae08745Sheppo * 150*1ae08745Sheppo * vdc creates a Local (private) descriptor ring the same size as the 151*1ae08745Sheppo * public descriptor ring it exports to vds. 152*1ae08745Sheppo */ 153*1ae08745Sheppo typedef struct vdc_local_desc { 154*1ae08745Sheppo kmutex_t lock; /* protects all fields */ 155*1ae08745Sheppo kcondvar_t cv; /* indicate processing done */ 156*1ae08745Sheppo int flags; /* Dring entry state, etc */ 157*1ae08745Sheppo int operation; /* VD_OP_xxx to be performed */ 158*1ae08745Sheppo caddr_t addr; /* addr passed in by consumer */ 159*1ae08745Sheppo caddr_t align_addr; /* used if addr non-aligned */ 160*1ae08745Sheppo struct buf *buf; /* buf passed to strategy() */ 161*1ae08745Sheppo ldc_mem_handle_t desc_mhdl; /* Mem handle of buf */ 162*1ae08745Sheppo vd_dring_entry_t *dep; /* public Dring Entry Pointer */ 163*1ae08745Sheppo } vdc_local_desc_t; 164*1ae08745Sheppo 165*1ae08745Sheppo /* 166*1ae08745Sheppo * vdc soft state structure 167*1ae08745Sheppo */ 168*1ae08745Sheppo typedef struct vdc { 169*1ae08745Sheppo kmutex_t attach_lock; /* used by CV which waits in attach */ 170*1ae08745Sheppo kcondvar_t attach_cv; /* signal when attach can finish */ 171*1ae08745Sheppo 172*1ae08745Sheppo kmutex_t lock; /* protects next 2 sections of vars */ 173*1ae08745Sheppo kcondvar_t cv; /* signal when upper layers can send */ 174*1ae08745Sheppo 175*1ae08745Sheppo dev_info_t *dip; /* device info pointer */ 176*1ae08745Sheppo int instance; /* driver instance number */ 177*1ae08745Sheppo int initialized; /* keeps track of what's init'ed */ 178*1ae08745Sheppo int open; /* count of outstanding opens */ 179*1ae08745Sheppo int dkio_flush_pending; /* # outstanding DKIO flushes */ 180*1ae08745Sheppo 181*1ae08745Sheppo uint64_t session_id; /* common ID sent with all messages */ 182*1ae08745Sheppo uint64_t seq_num; /* most recent sequence num generated */ 183*1ae08745Sheppo uint64_t seq_num_reply; /* Last seq num ACK/NACK'ed by vds */ 184*1ae08745Sheppo uint64_t req_id; /* Most recent Request ID generated */ 185*1ae08745Sheppo vd_state_t state; /* Current handshake state */ 186*1ae08745Sheppo vd_disk_type_t vdisk_type; /* type of device/disk being imported */ 187*1ae08745Sheppo uint64_t vdisk_size; /* device size in bytes */ 188*1ae08745Sheppo uint64_t max_xfer_sz; /* maximum block size of a descriptor */ 189*1ae08745Sheppo uint64_t block_size; /* device block size used */ 190*1ae08745Sheppo struct dk_cinfo *cinfo; /* structure to store DKIOCINFO data */ 191*1ae08745Sheppo struct dk_minfo *minfo; /* structure for DKIOCGMEDIAINFO data */ 192*1ae08745Sheppo struct vtoc *vtoc; /* structure to store VTOC data */ 193*1ae08745Sheppo 194*1ae08745Sheppo /* 195*1ae08745Sheppo * The mutex 'msg_proc_lock' protects the following group of fields. 196*1ae08745Sheppo * 197*1ae08745Sheppo * The callback function checks to see if LDC triggered it due to 198*1ae08745Sheppo * there being data available and the callback will signal to 199*1ae08745Sheppo * the message processing thread waiting on 'msg_proc_cv'. 200*1ae08745Sheppo */ 201*1ae08745Sheppo kmutex_t msg_proc_lock; 202*1ae08745Sheppo kcondvar_t msg_proc_cv; 203*1ae08745Sheppo boolean_t msg_pending; 204*1ae08745Sheppo vdc_thr_state_t msg_proc_thr_state; 205*1ae08745Sheppo kthread_t *msg_proc_thr_id; 206*1ae08745Sheppo 207*1ae08745Sheppo /* 208*1ae08745Sheppo * The mutex 'dring_lock' protects the following group of fields. 209*1ae08745Sheppo */ 210*1ae08745Sheppo kmutex_t dring_lock; 211*1ae08745Sheppo ldc_mem_info_t dring_mem_info; 212*1ae08745Sheppo uint_t dring_curr_idx; 213*1ae08745Sheppo uint32_t dring_len; 214*1ae08745Sheppo uint32_t dring_cookie_count; 215*1ae08745Sheppo uint32_t dring_entry_size; 216*1ae08745Sheppo ldc_mem_cookie_t *dring_cookie; 217*1ae08745Sheppo uint64_t dring_ident; 218*1ae08745Sheppo 219*1ae08745Sheppo vdc_local_desc_t *local_dring; 220*1ae08745Sheppo 221*1ae08745Sheppo uint64_t ldc_id; 222*1ae08745Sheppo ldc_status_t ldc_state; 223*1ae08745Sheppo ldc_handle_t ldc_handle; 224*1ae08745Sheppo ldc_dring_handle_t ldc_dring_hdl; 225*1ae08745Sheppo } vdc_t; 226*1ae08745Sheppo 227*1ae08745Sheppo /* 228*1ae08745Sheppo * Debugging macros 229*1ae08745Sheppo */ 230*1ae08745Sheppo #ifdef DEBUG 231*1ae08745Sheppo extern int vdc_msglevel; 232*1ae08745Sheppo 233*1ae08745Sheppo #define PR0 if (vdc_msglevel > 0) \ 234*1ae08745Sheppo vdc_msg 235*1ae08745Sheppo 236*1ae08745Sheppo #define PR1 if (vdc_msglevel > 1) \ 237*1ae08745Sheppo vdc_msg 238*1ae08745Sheppo 239*1ae08745Sheppo #define PR2 if (vdc_msglevel > 2) \ 240*1ae08745Sheppo vdc_msg 241*1ae08745Sheppo 242*1ae08745Sheppo #define VDC_DUMP_DRING_MSG(dmsgp) \ 243*1ae08745Sheppo vdc_msg("sq:%d start:%d end:%d ident:%x\n", \ 244*1ae08745Sheppo dmsgp->seq_num, dmsgp->start_idx, \ 245*1ae08745Sheppo dmsgp->end_idx, dmsgp->dring_ident); 246*1ae08745Sheppo 247*1ae08745Sheppo #else /* !DEBUG */ 248*1ae08745Sheppo #define PR0(...) 249*1ae08745Sheppo #define PR1(...) 250*1ae08745Sheppo #define PR2(...) 251*1ae08745Sheppo 252*1ae08745Sheppo #define VDC_DUMP_DRING_MSG(dmsgp) 253*1ae08745Sheppo 254*1ae08745Sheppo #endif /* !DEBUG */ 255*1ae08745Sheppo 256*1ae08745Sheppo #ifdef __cplusplus 257*1ae08745Sheppo } 258*1ae08745Sheppo #endif 259*1ae08745Sheppo 260*1ae08745Sheppo #endif /* _VDC_H */ 261