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 #ifndef _VDC_H 281ae08745Sheppo #define _VDC_H 291ae08745Sheppo 301ae08745Sheppo #pragma ident "%Z%%M% %I% %E% SMI" 311ae08745Sheppo 321ae08745Sheppo /* 331ae08745Sheppo * Virtual disk client implementation definitions 341ae08745Sheppo */ 351ae08745Sheppo 361ae08745Sheppo #include <sys/sysmacros.h> 371ae08745Sheppo #include <sys/note.h> 381ae08745Sheppo 391ae08745Sheppo #include <sys/ldc.h> 401ae08745Sheppo #include <sys/vio_mailbox.h> 411ae08745Sheppo #include <sys/vdsk_mailbox.h> 421ae08745Sheppo #include <sys/vdsk_common.h> 431ae08745Sheppo 441ae08745Sheppo #ifdef __cplusplus 451ae08745Sheppo extern "C" { 461ae08745Sheppo #endif 471ae08745Sheppo 481ae08745Sheppo #define VDC_DRIVER_NAME "vdc" 491ae08745Sheppo 501ae08745Sheppo /* 511ae08745Sheppo * Bit-field values to indicate if parts of the vdc driver are initialised. 521ae08745Sheppo */ 531ae08745Sheppo #define VDC_SOFT_STATE 0x0001 541ae08745Sheppo #define VDC_LOCKS 0x0002 551ae08745Sheppo #define VDC_MINOR 0x0004 561ae08745Sheppo #define VDC_THREAD 0x0008 571ae08745Sheppo #define VDC_LDC 0x0010 581ae08745Sheppo #define VDC_LDC_INIT 0x0020 591ae08745Sheppo #define VDC_LDC_CB 0x0040 601ae08745Sheppo #define VDC_LDC_OPEN 0x0080 611ae08745Sheppo #define VDC_DRING_INIT 0x0100 /* The DRing was created */ 621ae08745Sheppo #define VDC_DRING_BOUND 0x0200 /* The DRing was bound to an LDC channel */ 631ae08745Sheppo #define VDC_DRING_LOCAL 0x0400 /* The local private DRing was allocated */ 641ae08745Sheppo #define VDC_DRING_ENTRY 0x0800 /* At least one DRing entry was initialised */ 651ae08745Sheppo #define VDC_DRING (VDC_DRING_INIT | VDC_DRING_BOUND | \ 661ae08745Sheppo VDC_DRING_LOCAL | VDC_DRING_ENTRY) 671ae08745Sheppo #define VDC_HANDSHAKE 0x1000 /* Indicates if a handshake is in progress */ 681ae08745Sheppo #define VDC_HANDSHAKE_STOP 0x2000 /* stop further handshakes */ 691ae08745Sheppo 701ae08745Sheppo /* 711ae08745Sheppo * Definitions of strings to be used to create device node properties. 721ae08745Sheppo * (vdc uses the capitalised versions of these properties as they are 64-bit) 731ae08745Sheppo */ 741ae08745Sheppo #define VDC_NBLOCKS_PROP_NAME "Nblocks" 751ae08745Sheppo #define VDC_SIZE_PROP_NAME "Size" 761ae08745Sheppo 771ae08745Sheppo /* 781ae08745Sheppo * Definitions of MD nodes/properties. 791ae08745Sheppo */ 801ae08745Sheppo #define VDC_MD_CHAN_NAME "channel-endpoint" 811ae08745Sheppo #define VDC_MD_VDEV_NAME "virtual-device" 821ae08745Sheppo #define VDC_MD_DISK_NAME "disk" 831ae08745Sheppo #define VDC_MD_CFG_HDL "cfg-handle" 841ae08745Sheppo #define VDC_ID_PROP "id" 851ae08745Sheppo 861ae08745Sheppo /* 87e1ebb9ecSlm66018 * Definition of actions to be carried out when processing the sequence ID 88e1ebb9ecSlm66018 * of a message received from the vDisk server. The function verifying the 89e1ebb9ecSlm66018 * sequence number checks the 'seq_num_xxx' fields in the soft state and 90e1ebb9ecSlm66018 * returns whether the message should be processed (VDC_SEQ_NUM_TODO) or 91e1ebb9ecSlm66018 * whether it was it was previously processed (VDC_SEQ_NUM_SKIP). 92e1ebb9ecSlm66018 */ 93e1ebb9ecSlm66018 #define VDC_SEQ_NUM_INVALID -1 /* Error */ 94e1ebb9ecSlm66018 #define VDC_SEQ_NUM_SKIP 0 /* Request already processed */ 95e1ebb9ecSlm66018 #define VDC_SEQ_NUM_TODO 1 /* Request needs processing */ 96e1ebb9ecSlm66018 97e1ebb9ecSlm66018 /* 981ae08745Sheppo * Scheme to store the instance number and the slice number in the minor number. 991ae08745Sheppo * (Uses the same format and definitions as the sd(7D) driver) 1001ae08745Sheppo */ 1011ae08745Sheppo #define VD_MAKE_DEV(instance, minor) ((instance << SDUNIT_SHIFT) | minor) 1021ae08745Sheppo 1031ae08745Sheppo /* 1041ae08745Sheppo * variables controlling how long to wait before timing out and how many 1051ae08745Sheppo * retries to attempt before giving up when communicating with vds. 106e1ebb9ecSlm66018 * 107e1ebb9ecSlm66018 * These values need to be sufficiently large so that a guest can survive 108e1ebb9ecSlm66018 * the reboot of the service domain. 1091ae08745Sheppo */ 110e1ebb9ecSlm66018 #define VDC_RETRIES 10 1111ae08745Sheppo 1121ae08745Sheppo #define VDC_USEC_TIMEOUT_MIN (30 * MICROSEC) /* 30 sec */ 1131ae08745Sheppo 114e1ebb9ecSlm66018 /* 115e1ebb9ecSlm66018 * This macro returns the number of Hz that the vdc driver should wait before 116e1ebb9ecSlm66018 * a timeout is triggered. The 'timeout' parameter specifiecs the wait 117e1ebb9ecSlm66018 * time in Hz. The 'mul' parameter allows for a multiplier to be 118e1ebb9ecSlm66018 * specified allowing for a backoff to be implemented (e.g. using the 119e1ebb9ecSlm66018 * retry number as a multiplier) where the wait time will get longer if 120e1ebb9ecSlm66018 * there is no response on the previous retry. 121e1ebb9ecSlm66018 */ 122e1ebb9ecSlm66018 #define VD_GET_TIMEOUT_HZ(timeout, mul) \ 123e1ebb9ecSlm66018 (ddi_get_lbolt() + ((timeout) * MAX(1, (mul)))) 1241ae08745Sheppo 1251ae08745Sheppo /* 1261ae08745Sheppo * Macros to manipulate Descriptor Ring variables in the soft state 1271ae08745Sheppo * structure. 1281ae08745Sheppo */ 129e1ebb9ecSlm66018 #define VDC_GET_NEXT_REQ_ID(vdc) ((vdc)->req_id++) 1301ae08745Sheppo 1311ae08745Sheppo #define VDC_GET_DRING_ENTRY_PTR(vdc, idx) \ 132e1ebb9ecSlm66018 (vd_dring_entry_t *)((vdc)->dring_mem_info.vaddr + \ 133e1ebb9ecSlm66018 (idx * (vdc)->dring_entry_size)) 1341ae08745Sheppo 1351ae08745Sheppo #define VDC_MARK_DRING_ENTRY_FREE(vdc, idx) \ 1361ae08745Sheppo { \ 1371ae08745Sheppo vd_dring_entry_t *dep = NULL; \ 1381ae08745Sheppo ASSERT(vdc != NULL); \ 139e1ebb9ecSlm66018 ASSERT((idx >= 0) && (idx < vdc->dring_len)); \ 1401ae08745Sheppo ASSERT(vdc->dring_mem_info.vaddr != NULL); \ 1411ae08745Sheppo dep = (vd_dring_entry_t *)(vdc->dring_mem_info.vaddr + \ 1421ae08745Sheppo (idx * vdc->dring_entry_size)); \ 1431ae08745Sheppo ASSERT(dep != NULL); \ 1441ae08745Sheppo dep->hdr.dstate = VIO_DESC_FREE; \ 1451ae08745Sheppo } 1461ae08745Sheppo 1471ae08745Sheppo /* Initialise the Session ID and Sequence Num in the DRing msg */ 1481ae08745Sheppo #define VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdc) \ 1491ae08745Sheppo ASSERT(vdc != NULL); \ 1501ae08745Sheppo dmsg.tag.vio_sid = vdc->session_id; \ 1510a55fbb7Slm66018 dmsg.seq_num = vdc->seq_num; 1521ae08745Sheppo 1531ae08745Sheppo /* 154*3af08d82Slm66018 * The states that the read thread can be in. 1551ae08745Sheppo */ 156*3af08d82Slm66018 typedef enum vdc_rd_state { 157*3af08d82Slm66018 VDC_READ_IDLE, /* idling - conn is not up */ 158*3af08d82Slm66018 VDC_READ_WAITING, /* waiting for data */ 159*3af08d82Slm66018 VDC_READ_PENDING, /* pending data avail for read */ 160*3af08d82Slm66018 VDC_READ_RESET /* channel was reset - stop reads */ 161*3af08d82Slm66018 } vdc_rd_state_t; 162*3af08d82Slm66018 163*3af08d82Slm66018 /* 164*3af08d82Slm66018 * The states that the vdc-vds connection can be in. 165*3af08d82Slm66018 */ 166*3af08d82Slm66018 typedef enum vdc_state { 167*3af08d82Slm66018 VDC_STATE_INIT, /* device is initialized */ 168*3af08d82Slm66018 VDC_STATE_INIT_WAITING, /* waiting for ldc connection */ 169*3af08d82Slm66018 VDC_STATE_NEGOTIATE, /* doing handshake negotiation */ 170*3af08d82Slm66018 VDC_STATE_HANDLE_PENDING, /* handle requests in backup dring */ 171*3af08d82Slm66018 VDC_STATE_RUNNING, /* running and accepting requests */ 172*3af08d82Slm66018 VDC_STATE_DETACH, /* detaching */ 173*3af08d82Slm66018 VDC_STATE_RESETTING /* resetting connection with vds */ 174*3af08d82Slm66018 } vdc_state_t; 175*3af08d82Slm66018 176*3af08d82Slm66018 /* 177*3af08d82Slm66018 * The states that the vdc instance can be in. 178*3af08d82Slm66018 */ 179*3af08d82Slm66018 typedef enum vdc_lc_state { 180*3af08d82Slm66018 VDC_LC_ATTACHING, /* driver is attaching */ 181*3af08d82Slm66018 VDC_LC_ONLINE, /* driver is attached and online */ 182*3af08d82Slm66018 VDC_LC_DETACHING /* driver is detaching */ 183*3af08d82Slm66018 } vdc_lc_state_t; 1841ae08745Sheppo 1851ae08745Sheppo /* 1861ae08745Sheppo * Local Descriptor Ring entry 1871ae08745Sheppo * 1881ae08745Sheppo * vdc creates a Local (private) descriptor ring the same size as the 1891ae08745Sheppo * public descriptor ring it exports to vds. 1901ae08745Sheppo */ 191*3af08d82Slm66018 192*3af08d82Slm66018 typedef enum { 193*3af08d82Slm66018 VIO_read_dir, /* read data from server */ 194*3af08d82Slm66018 VIO_write_dir, /* write data to server */ 195*3af08d82Slm66018 VIO_both_dir /* transfer both in and out in same buffer */ 196*3af08d82Slm66018 } vio_desc_direction_t; 197*3af08d82Slm66018 198*3af08d82Slm66018 typedef enum { 199*3af08d82Slm66018 CB_STRATEGY, /* non-blocking strategy call */ 200*3af08d82Slm66018 CB_SYNC /* synchronous operation */ 201*3af08d82Slm66018 } vio_cb_type_t; 202*3af08d82Slm66018 2031ae08745Sheppo typedef struct vdc_local_desc { 204*3af08d82Slm66018 boolean_t is_free; /* local state - inuse or not */ 205*3af08d82Slm66018 2061ae08745Sheppo int operation; /* VD_OP_xxx to be performed */ 2071ae08745Sheppo caddr_t addr; /* addr passed in by consumer */ 208*3af08d82Slm66018 int slice; 209*3af08d82Slm66018 diskaddr_t offset; /* disk offset */ 210*3af08d82Slm66018 size_t nbytes; 211*3af08d82Slm66018 vio_cb_type_t cb_type; /* operation type blk/nonblk */ 212*3af08d82Slm66018 void *cb_arg; /* buf passed to strategy() */ 213*3af08d82Slm66018 vio_desc_direction_t dir; /* direction of transfer */ 214*3af08d82Slm66018 2151ae08745Sheppo caddr_t align_addr; /* used if addr non-aligned */ 2161ae08745Sheppo ldc_mem_handle_t desc_mhdl; /* Mem handle of buf */ 2171ae08745Sheppo vd_dring_entry_t *dep; /* public Dring Entry Pointer */ 218*3af08d82Slm66018 2191ae08745Sheppo } vdc_local_desc_t; 2201ae08745Sheppo 2211ae08745Sheppo /* 2221ae08745Sheppo * vdc soft state structure 2231ae08745Sheppo */ 2241ae08745Sheppo typedef struct vdc { 2251ae08745Sheppo 2261ae08745Sheppo kmutex_t lock; /* protects next 2 sections of vars */ 227*3af08d82Slm66018 kcondvar_t running_cv; /* signal when upper layers can send */ 228*3af08d82Slm66018 kcondvar_t initwait_cv; /* signal when ldc conn is up */ 229*3af08d82Slm66018 kcondvar_t dring_free_cv; /* signal when desc is avail */ 230*3af08d82Slm66018 kcondvar_t membind_cv; /* signal when mem can be bound */ 231*3af08d82Slm66018 boolean_t self_reset; 2321ae08745Sheppo 2331ae08745Sheppo int initialized; /* keeps track of what's init'ed */ 234*3af08d82Slm66018 vdc_lc_state_t lifecycle; /* Current state of the vdc instance */ 235*3af08d82Slm66018 2360a55fbb7Slm66018 int hshake_cnt; /* number of failed handshakes */ 237*3af08d82Slm66018 int open_count; /* count of outstanding opens */ 2381ae08745Sheppo int dkio_flush_pending; /* # outstanding DKIO flushes */ 2391ae08745Sheppo 240*3af08d82Slm66018 kthread_t *msg_proc_thr; /* main msg processing thread */ 241*3af08d82Slm66018 242*3af08d82Slm66018 kmutex_t read_lock; /* lock to protect read */ 243*3af08d82Slm66018 kcondvar_t read_cv; /* cv to wait for READ events */ 244*3af08d82Slm66018 vdc_rd_state_t read_state; /* current read state */ 245*3af08d82Slm66018 246*3af08d82Slm66018 uint32_t sync_op_cnt; /* num of active sync operations */ 247*3af08d82Slm66018 boolean_t sync_op_pending; /* sync operation is pending */ 248*3af08d82Slm66018 boolean_t sync_op_blocked; /* blocked waiting to do sync op */ 249*3af08d82Slm66018 uint32_t sync_op_status; /* status of sync operation */ 250*3af08d82Slm66018 kcondvar_t sync_pending_cv; /* cv wait for sync op to finish */ 251*3af08d82Slm66018 kcondvar_t sync_blocked_cv; /* cv wait for other syncs to finish */ 252*3af08d82Slm66018 2531ae08745Sheppo uint64_t session_id; /* common ID sent with all messages */ 2541ae08745Sheppo uint64_t seq_num; /* most recent sequence num generated */ 2551ae08745Sheppo uint64_t seq_num_reply; /* Last seq num ACK/NACK'ed by vds */ 2561ae08745Sheppo uint64_t req_id; /* Most recent Request ID generated */ 257e1ebb9ecSlm66018 uint64_t req_id_proc; /* Last request ID processed by vdc */ 258*3af08d82Slm66018 vdc_state_t state; /* Current disk client-server state */ 259e1ebb9ecSlm66018 260e1ebb9ecSlm66018 dev_info_t *dip; /* device info pointer */ 261e1ebb9ecSlm66018 int instance; /* driver instance number */ 262*3af08d82Slm66018 263e1ebb9ecSlm66018 vio_ver_t ver; /* version number agreed with server */ 2641ae08745Sheppo vd_disk_type_t vdisk_type; /* type of device/disk being imported */ 2654bac2208Snarayan vd_disk_label_t vdisk_label; /* label type of device/disk imported */ 2664bac2208Snarayan uint64_t vdisk_size; /* device size in blocks */ 2671ae08745Sheppo uint64_t max_xfer_sz; /* maximum block size of a descriptor */ 2681ae08745Sheppo uint64_t block_size; /* device block size used */ 2690a55fbb7Slm66018 struct dk_label *label; /* structure to store disk label */ 2701ae08745Sheppo struct dk_cinfo *cinfo; /* structure to store DKIOCINFO data */ 2711ae08745Sheppo struct dk_minfo *minfo; /* structure for DKIOCGMEDIAINFO data */ 2721ae08745Sheppo struct vtoc *vtoc; /* structure to store VTOC data */ 2734bac2208Snarayan ddi_devid_t devid; /* device id */ 2741ae08745Sheppo 275*3af08d82Slm66018 ldc_mem_info_t dring_mem_info; /* dring information */ 276*3af08d82Slm66018 uint_t dring_curr_idx; /* current index */ 277*3af08d82Slm66018 uint32_t dring_len; /* dring length */ 278*3af08d82Slm66018 uint32_t dring_max_cookies; /* dring max cookies */ 279*3af08d82Slm66018 uint32_t dring_cookie_count; /* num cookies */ 280*3af08d82Slm66018 uint32_t dring_entry_size; /* descriptor size */ 281*3af08d82Slm66018 ldc_mem_cookie_t *dring_cookie; /* dring cookies */ 282*3af08d82Slm66018 uint64_t dring_ident; /* dring ident */ 2831ae08745Sheppo 284*3af08d82Slm66018 uint64_t threads_pending; /* num of threads */ 2851ae08745Sheppo 286*3af08d82Slm66018 vdc_local_desc_t *local_dring; /* local dring */ 287*3af08d82Slm66018 vdc_local_desc_t *local_dring_backup; /* local dring backup */ 288*3af08d82Slm66018 int local_dring_backup_tail; /* backup dring tail */ 289*3af08d82Slm66018 int local_dring_backup_len; /* backup dring len */ 2901ae08745Sheppo 291*3af08d82Slm66018 uint64_t ldc_id; /* LDC channel id */ 292*3af08d82Slm66018 ldc_status_t ldc_state; /* LDC channel state */ 293*3af08d82Slm66018 ldc_handle_t ldc_handle; /* LDC handle */ 294*3af08d82Slm66018 ldc_dring_handle_t ldc_dring_hdl; /* LDC dring handle */ 2951ae08745Sheppo } vdc_t; 2961ae08745Sheppo 2971ae08745Sheppo /* 2981ae08745Sheppo * Debugging macros 2991ae08745Sheppo */ 3001ae08745Sheppo #ifdef DEBUG 3011ae08745Sheppo extern int vdc_msglevel; 302*3af08d82Slm66018 extern uint64_t vdc_matchinst; 3031ae08745Sheppo 304*3af08d82Slm66018 #define DMSG(_vdc, err_level, format, ...) \ 305*3af08d82Slm66018 do { \ 306*3af08d82Slm66018 if (vdc_msglevel > err_level && \ 307*3af08d82Slm66018 (vdc_matchinst & (1ull << (_vdc)->instance))) \ 308*3af08d82Slm66018 cmn_err(CE_CONT, "?[%d,t@%p] %s: "format, \ 309*3af08d82Slm66018 (_vdc)->instance, (void *)curthread, \ 310*3af08d82Slm66018 __func__, __VA_ARGS__); \ 311*3af08d82Slm66018 _NOTE(CONSTANTCONDITION) \ 312*3af08d82Slm66018 } while (0); 313*3af08d82Slm66018 314*3af08d82Slm66018 #define DMSGX(err_level, format, ...) \ 315e1ebb9ecSlm66018 do { \ 316e1ebb9ecSlm66018 if (vdc_msglevel > err_level) \ 317*3af08d82Slm66018 cmn_err(CE_CONT, "?%s: "format, __func__, __VA_ARGS__);\ 318e1ebb9ecSlm66018 _NOTE(CONSTANTCONDITION) \ 319e1ebb9ecSlm66018 } while (0); 3201ae08745Sheppo 3211ae08745Sheppo #define VDC_DUMP_DRING_MSG(dmsgp) \ 322*3af08d82Slm66018 DMSGX(0, "sq:%lu start:%d end:%d ident:%lu\n", \ 3231ae08745Sheppo dmsgp->seq_num, dmsgp->start_idx, \ 3241ae08745Sheppo dmsgp->end_idx, dmsgp->dring_ident); 3251ae08745Sheppo 3261ae08745Sheppo #else /* !DEBUG */ 327e1ebb9ecSlm66018 #define DMSG(err_level, ...) 328*3af08d82Slm66018 #define DMSGX(err_level, format, ...) 3291ae08745Sheppo #define VDC_DUMP_DRING_MSG(dmsgp) 3301ae08745Sheppo 3311ae08745Sheppo #endif /* !DEBUG */ 3321ae08745Sheppo 3331ae08745Sheppo #ifdef __cplusplus 3341ae08745Sheppo } 3351ae08745Sheppo #endif 3361ae08745Sheppo 3371ae08745Sheppo #endif /* _VDC_H */ 338