1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 2014 by Delphix. All rights reserved. 29 */ 30 31 #ifndef _SYS_XDF_H 32 #define _SYS_XDF_H 33 34 #include <sys/ddi.h> 35 #include <sys/sunddi.h> 36 #include <sys/cmlb.h> 37 #include <sys/dkio.h> 38 39 #include <sys/gnttab.h> 40 #include <xen/sys/xendev.h> 41 42 #ifdef __cplusplus 43 extern "C" { 44 #endif 45 46 47 /* 48 * VBDs have standard 512 byte blocks 49 * A single blkif_request can transfer up to 11 pages of data, 1 page/segment 50 */ 51 #define XB_BSIZE DEV_BSIZE 52 #define XB_BMASK (XB_BSIZE - 1) 53 #define XB_BSHIFT 9 54 #define XB_DTOB(bn, vdp) ((bn) * (vdp)->xdf_xdev_secsize) 55 56 #define XB_MAX_SEGLEN (8 * XB_BSIZE) 57 #define XB_SEGOFFSET (XB_MAX_SEGLEN - 1) 58 #define XB_MAX_XFER (XB_MAX_SEGLEN * BLKIF_MAX_SEGMENTS_PER_REQUEST) 59 #define XB_MAXPHYS (XB_MAX_XFER * BLKIF_RING_SIZE) 60 61 /* Number of sectors per segement */ 62 #define XB_NUM_SECTORS_PER_SEG (PAGESIZE / XB_BSIZE) 63 /* sectors are number 0 through XB_NUM_SECTORS_PER_SEG - 1 */ 64 #define XB_LAST_SECTOR_IN_SEG (XB_NUM_SECTORS_PER_SEG - 1) 65 66 67 /* 68 * Slice for absolute disk transaction. 69 * 70 * Hack Alert. XB_SLICE_NONE is a magic value that can be written into the 71 * b_private field of buf structures passed to xdf_strategy(). When present 72 * it indicates that the I/O is using an absolute offset. (ie, the I/O is 73 * not bound to any one partition.) This magic value is currently used by 74 * the pv_cmdk driver. This hack is shamelessly stolen from the sun4v vdc 75 * driver, another virtual disk device driver. (Although in the case of 76 * vdc the hack is less egregious since it is self contained within the 77 * vdc driver, where as here it is used as an interface between the pv_cmdk 78 * driver and the xdf driver.) 79 */ 80 #define XB_SLICE_NONE 0xFF 81 82 /* 83 * blkif status 84 */ 85 typedef enum xdf_state { 86 /* 87 * initial state 88 */ 89 XD_UNKNOWN = 0, 90 /* 91 * ring and evtchn alloced, xenbus state changed to 92 * XenbusStateInitialised, wait for backend to connect 93 */ 94 XD_INIT = 1, 95 /* 96 * backend and frontend xenbus state has changed to 97 * XenbusStateConnected. IO is now allowed, but we are not still 98 * fully initialized. 99 */ 100 XD_CONNECTED = 2, 101 /* 102 * We're fully initialized and allowing regular IO. 103 */ 104 XD_READY = 3, 105 /* 106 * vbd interface close request received from backend, no more I/O 107 * requestis allowed to be put into ring buffer, while interrupt handler 108 * is allowed to run to finish any outstanding I/O request, disconnect 109 * process is kicked off by changing xenbus state to XenbusStateClosed 110 */ 111 XD_CLOSING = 4, 112 /* 113 * disconnection process finished, both backend and frontend's 114 * xenbus state has been changed to XenbusStateClosed, can be detached 115 */ 116 XD_CLOSED = 5, 117 /* 118 * We're either being suspended or resuming from a suspend. If we're 119 * in the process of suspending, we block all new IO, but but allow 120 * existing IO to drain. 121 */ 122 XD_SUSPEND = 6 123 } xdf_state_t; 124 125 /* 126 * 16 partitions + fdisk 127 */ 128 #define XDF_PSHIFT 6 129 #define XDF_PMASK ((1 << XDF_PSHIFT) - 1) 130 #define XDF_PEXT (1 << XDF_PSHIFT) 131 #define XDF_MINOR(i, m) (((i) << XDF_PSHIFT) | (m)) 132 #define XDF_INST(m) ((m) >> XDF_PSHIFT) 133 #define XDF_PART(m) ((m) & XDF_PMASK) 134 135 /* 136 * one blkif_request_t will have one corresponding ge_slot_t 137 * where we save those grant table refs used in this blkif_request_t 138 * 139 * the id of this ge_slot_t will also be put into 'id' field in 140 * each blkif_request_t when sent out to the ring buffer. 141 */ 142 typedef struct ge_slot { 143 list_node_t gs_vreq_link; 144 struct v_req *gs_vreq; 145 domid_t gs_oeid; 146 int gs_isread; 147 grant_ref_t gs_ghead; 148 int gs_ngrefs; 149 grant_ref_t gs_ge[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 150 } ge_slot_t; 151 152 /* 153 * vbd I/O request 154 * 155 * An instance of this structure is bound to each buf passed to 156 * the driver's strategy by setting the pointer into bp->av_back. 157 * The id of this vreq will also be put into 'id' field in each 158 * blkif_request_t when sent out to the ring buffer for one DMA 159 * window of this buf. 160 * 161 * Vreq mainly contains DMA information for this buf. In one vreq/buf, 162 * there could be more than one DMA window, each of which will be 163 * mapped to one blkif_request_t/ge_slot_t. Ge_slot_t contains all grant 164 * table entry information for this buf. The ge_slot_t for current DMA 165 * window is pointed to by v_gs in vreq. 166 * 167 * So, grant table entries will only be alloc'ed when the DMA window is 168 * about to be transferred via blkif_request_t to the ring buffer. And 169 * they will be freed right after the blkif_response_t is seen. By this 170 * means, we can make use of grant table entries more efficiently. 171 */ 172 typedef struct v_req { 173 list_node_t v_link; 174 list_t v_gs; 175 int v_status; 176 buf_t *v_buf; 177 uint_t v_ndmacs; 178 uint_t v_dmaw; 179 uint_t v_ndmaws; 180 uint_t v_nslots; 181 uint64_t v_blkno; 182 ddi_dma_handle_t v_memdmahdl; 183 ddi_acc_handle_t v_align; 184 ddi_dma_handle_t v_dmahdl; 185 ddi_dma_cookie_t v_dmac; 186 caddr_t v_abuf; 187 uint8_t v_flush_diskcache; 188 boolean_t v_runq; 189 } v_req_t; 190 191 /* 192 * Status set and checked in vreq->v_status by vreq_setup() 193 * 194 * These flags will help us to continue the vreq setup work from last failure 195 * point, instead of starting from scratch after each failure. 196 */ 197 #define VREQ_INIT 0x0 198 #define VREQ_INIT_DONE 0x1 199 #define VREQ_DMAHDL_ALLOCED 0x2 200 #define VREQ_MEMDMAHDL_ALLOCED 0x3 201 #define VREQ_DMAMEM_ALLOCED 0x4 202 #define VREQ_DMABUF_BOUND 0x5 203 #define VREQ_GS_ALLOCED 0x6 204 #define VREQ_DMAWIN_DONE 0x7 205 206 /* 207 * virtual block device per-instance softstate 208 */ 209 typedef struct xdf { 210 dev_info_t *xdf_dip; 211 char *xdf_addr; 212 ddi_iblock_cookie_t xdf_ibc; /* mutex iblock cookie */ 213 domid_t xdf_peer; /* otherend's dom ID */ 214 xendev_ring_t *xdf_xb_ring; /* I/O ring buffer */ 215 ddi_acc_handle_t xdf_xb_ring_hdl; /* access handler for ring buffer */ 216 list_t xdf_vreq_act; /* active vreq list */ 217 buf_t *xdf_f_act; /* active buf list head */ 218 buf_t *xdf_l_act; /* active buf list tail */ 219 buf_t *xdf_i_act; /* active buf list index */ 220 xdf_state_t xdf_state; /* status of this virtual disk */ 221 boolean_t xdf_suspending; 222 ulong_t xdf_vd_open[OTYPCNT]; 223 ulong_t xdf_vd_lyropen[XDF_PEXT]; 224 ulong_t xdf_connect_req; 225 kthread_t *xdf_connect_thread; 226 ulong_t xdf_vd_exclopen; 227 kmutex_t xdf_iostat_lk; /* muxes lock for the iostat ptr */ 228 kmutex_t xdf_dev_lk; /* mutex lock for I/O path */ 229 kmutex_t xdf_cb_lk; /* mutex lock for event handling path */ 230 kcondvar_t xdf_dev_cv; /* cv used in I/O path */ 231 uint_t xdf_dinfo; /* disk info from backend xenstore */ 232 diskaddr_t xdf_xdev_nblocks; /* total size in block */ 233 uint_t xdf_xdev_secsize; /* disk blksize from backend */ 234 cmlb_geom_t xdf_pgeom; 235 boolean_t xdf_pgeom_set; 236 boolean_t xdf_pgeom_fixed; 237 kstat_t *xdf_xdev_iostat; 238 cmlb_handle_t xdf_vd_lbl; 239 ddi_softintr_t xdf_softintr_id; 240 timeout_id_t xdf_timeout_id; 241 struct gnttab_free_callback xdf_gnt_callback; 242 boolean_t xdf_feature_barrier; 243 boolean_t xdf_flush_supported; 244 boolean_t xdf_media_req_supported; 245 boolean_t xdf_wce; 246 boolean_t xdf_cmbl_reattach; 247 char *xdf_flush_mem; 248 char *xdf_cache_flush_block; 249 int xdf_evtchn; 250 enum dkio_state xdf_mstate; 251 kcondvar_t xdf_mstate_cv; 252 kcondvar_t xdf_hp_status_cv; 253 struct buf *xdf_ready_bp; 254 ddi_taskq_t *xdf_ready_tq; 255 kthread_t *xdf_ready_tq_thread; 256 struct buf *xdf_ready_tq_bp; 257 ddi_devid_t xdf_tgt_devid; 258 #ifdef DEBUG 259 int xdf_dmacallback_num; 260 kthread_t *xdf_oe_change_thread; 261 #endif 262 } xdf_t; 263 264 /* 265 * VBD I/O requests must be aligned on a 512-byte boundary and specify 266 * a transfer size which is a mutiple of 512-bytes 267 */ 268 #define ALIGNED_XFER(bp) \ 269 ((((uintptr_t)((bp)->b_un.b_addr) & XB_BMASK) == 0) && \ 270 (((bp)->b_bcount & XB_BMASK) == 0)) 271 272 #define U_INVAL(u) (((u)->uio_loffset & (offset_t)(XB_BMASK)) || \ 273 ((u)->uio_iov->iov_len & (offset_t)(XB_BMASK))) 274 275 /* wrap pa_to_ma() for xdf to run in dom0 */ 276 #define PATOMA(addr) (DOMAIN_IS_INITDOMAIN(xen_info) ? addr : pa_to_ma(addr)) 277 278 #define XD_IS_RO(vbd) VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_READONLY) 279 #define XD_IS_CD(vbd) VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_CDROM) 280 #define XD_IS_RM(vbd) VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_REMOVABLE) 281 #define IS_READ(bp) VOID2BOOLEAN((bp)->b_flags & B_READ) 282 #define IS_ERROR(bp) VOID2BOOLEAN((bp)->b_flags & B_ERROR) 283 284 #define XDF_UPDATE_IO_STAT(vdp, bp) \ 285 { \ 286 kstat_io_t *kip = KSTAT_IO_PTR((vdp)->xdf_xdev_iostat); \ 287 size_t n_done = (bp)->b_bcount - (bp)->b_resid; \ 288 if ((bp)->b_flags & B_READ) { \ 289 kip->reads++; \ 290 kip->nread += n_done; \ 291 } else { \ 292 kip->writes++; \ 293 kip->nwritten += n_done; \ 294 } \ 295 } 296 297 #ifdef DEBUG 298 #define DPRINTF(flag, args) {if (xdf_debug & (flag)) prom_printf args; } 299 #define SETDMACBON(vbd) {(vbd)->xdf_dmacallback_num++; } 300 #define SETDMACBOFF(vbd) {(vbd)->xdf_dmacallback_num--; } 301 #define ISDMACBON(vbd) ((vbd)->xdf_dmacallback_num > 0) 302 #else 303 #define DPRINTF(flag, args) 304 #define SETDMACBON(vbd) 305 #define SETDMACBOFF(vbd) 306 #define ISDMACBON(vbd) 307 #endif /* DEBUG */ 308 309 #define DDI_DBG 0x1 310 #define DMA_DBG 0x2 311 #define INTR_DBG 0x8 312 #define IO_DBG 0x10 313 #define IOCTL_DBG 0x20 314 #define SUSRES_DBG 0x40 315 #define LBL_DBG 0x80 316 317 #ifdef XPV_HVM_DRIVER 318 extern int xdf_lb_getinfo(dev_info_t *, int, void *, void *); 319 extern int xdf_lb_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t, size_t, 320 void *); 321 extern void xdfmin(struct buf *bp); 322 extern dev_info_t *xdf_hvm_hold(const char *); 323 extern boolean_t xdf_hvm_connect(dev_info_t *); 324 extern int xdf_hvm_setpgeom(dev_info_t *, cmlb_geom_t *); 325 extern boolean_t xdf_is_cd(dev_info_t *); 326 extern boolean_t xdf_is_rm(dev_info_t *); 327 extern boolean_t xdf_media_req_supported(dev_info_t *); 328 #endif /* XPV_HVM_DRIVER */ 329 330 #ifdef __cplusplus 331 } 332 #endif 333 334 #endif /* _SYS_XDF_H */ 335