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 * Note: This is the backend part of the split PV disk driver. This driver 29 * is not a nexus driver, nor is it a leaf driver(block/char/stream driver). 30 * Currently, it does not create any minor node. So, although, it runs in 31 * backend domain, it will not be used directly from within dom0. 32 * It simply gets block I/O requests issued by frontend from a shared page 33 * (blkif ring buffer - defined by Xen) between backend and frontend domain, 34 * generates a buf, and push it down to underlying disk target driver via 35 * ldi interface. When buf is done, this driver will generate a response 36 * and put it into ring buffer to inform frontend of the status of the I/O 37 * request issued by it. When a new virtual device entry is added in xenstore, 38 * there will be an watch event sent from Xen to xvdi framework, who will, 39 * in turn, create the devinfo node and try to attach this driver 40 * (see xvdi_create_dev). When frontend peer changes its state to 41 * XenbusStateClose, an event will also be sent from Xen to xvdi framework, 42 * who will detach and remove this devinfo node (see i_xvdi_oestate_handler). 43 * I/O requests get from ring buffer and event coming from xenstore cannot be 44 * trusted. We verify them in xdb_get_buf() and xdb_check_state_transition(). 45 * 46 * Virtual device configuration is read/written from/to the database via 47 * xenbus_* interfaces. Driver also use xvdi_* to interact with hypervisor. 48 * There is an on-going effort to make xvdi_* cover all xenbus_*. 49 */ 50 51 #include <sys/types.h> 52 #include <sys/conf.h> 53 #include <sys/ddi.h> 54 #include <sys/dditypes.h> 55 #include <sys/sunddi.h> 56 #include <sys/list.h> 57 #include <sys/dkio.h> 58 #include <sys/cmlb.h> 59 #include <sys/vtoc.h> 60 #include <sys/modctl.h> 61 #include <sys/bootconf.h> 62 #include <sys/promif.h> 63 #include <sys/sysmacros.h> 64 #include <public/io/xenbus.h> 65 #include <public/io/xs_wire.h> 66 #include <xen/sys/xenbus_impl.h> 67 #include <xen/sys/xendev.h> 68 #include <sys/gnttab.h> 69 #include <sys/scsi/generic/inquiry.h> 70 #include <vm/seg_kmem.h> 71 #include <vm/hat_i86.h> 72 #include <sys/gnttab.h> 73 #include <sys/lofi.h> 74 #include <io/xdf.h> 75 #include <xen/io/blkif_impl.h> 76 #include <io/xdb.h> 77 78 static xdb_t *xdb_statep; 79 static int xdb_debug = 0; 80 81 static void xdb_close(dev_info_t *); 82 static int xdb_push_response(xdb_t *, uint64_t, uint8_t, uint16_t); 83 static int xdb_get_request(xdb_t *, blkif_request_t *); 84 static void blkif_get_x86_32_req(blkif_request_t *, blkif_x86_32_request_t *); 85 static void blkif_get_x86_64_req(blkif_request_t *, blkif_x86_64_request_t *); 86 static int xdb_biodone(buf_t *); 87 88 89 #ifdef DEBUG 90 /* 91 * debug aid functions 92 */ 93 94 static void 95 logva(xdb_t *vdp, uint64_t va) 96 { 97 uint64_t *page_addrs; 98 int i; 99 100 page_addrs = vdp->page_addrs; 101 for (i = 0; i < XDB_MAX_IO_PAGES(vdp); i++) { 102 if (page_addrs[i] == va) 103 debug_enter("VA remapping found!"); 104 } 105 106 for (i = 0; i < XDB_MAX_IO_PAGES(vdp); i++) { 107 if (page_addrs[i] == 0) { 108 page_addrs[i] = va; 109 break; 110 } 111 } 112 ASSERT(i < XDB_MAX_IO_PAGES(vdp)); 113 } 114 115 static void 116 unlogva(xdb_t *vdp, uint64_t va) 117 { 118 uint64_t *page_addrs; 119 int i; 120 121 page_addrs = vdp->page_addrs; 122 for (i = 0; i < XDB_MAX_IO_PAGES(vdp); i++) { 123 if (page_addrs[i] == va) { 124 page_addrs[i] = 0; 125 break; 126 } 127 } 128 ASSERT(i < XDB_MAX_IO_PAGES(vdp)); 129 } 130 131 static void 132 xdb_dump_request_oe(blkif_request_t *req) 133 { 134 int i; 135 136 /* 137 * Exploit the public interface definitions for BLKIF_OP_READ 138 * etc.. 139 */ 140 char *op_name[] = { "read", "write", "barrier", "flush" }; 141 142 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, "op=%s", op_name[req->operation])); 143 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, "num of segments=%d", 144 req->nr_segments)); 145 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, "handle=%d", req->handle)); 146 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, "id=%llu", 147 (unsigned long long)req->id)); 148 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, "start sector=%llu", 149 (unsigned long long)req->sector_number)); 150 for (i = 0; i < req->nr_segments; i++) { 151 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, "gref=%d, first sec=%d," 152 "last sec=%d", req->seg[i].gref, req->seg[i].first_sect, 153 req->seg[i].last_sect)); 154 } 155 } 156 #endif /* DEBUG */ 157 158 /* 159 * Statistics. 160 */ 161 static char *xdb_stats[] = { 162 "rd_reqs", 163 "wr_reqs", 164 "br_reqs", 165 "fl_reqs", 166 "oo_reqs" 167 }; 168 169 static int 170 xdb_kstat_update(kstat_t *ksp, int flag) 171 { 172 xdb_t *vdp; 173 kstat_named_t *knp; 174 175 if (flag != KSTAT_READ) 176 return (EACCES); 177 178 vdp = ksp->ks_private; 179 knp = ksp->ks_data; 180 181 /* 182 * Assignment order should match that of the names in 183 * xdb_stats. 184 */ 185 (knp++)->value.ui64 = vdp->xs_stat_req_reads; 186 (knp++)->value.ui64 = vdp->xs_stat_req_writes; 187 (knp++)->value.ui64 = vdp->xs_stat_req_barriers; 188 (knp++)->value.ui64 = vdp->xs_stat_req_flushes; 189 (knp++)->value.ui64 = 0; /* oo_req */ 190 191 return (0); 192 } 193 194 static boolean_t 195 xdb_kstat_init(xdb_t *vdp) 196 { 197 int nstat = sizeof (xdb_stats) / sizeof (xdb_stats[0]); 198 char **cp = xdb_stats; 199 kstat_named_t *knp; 200 201 if ((vdp->xs_kstats = kstat_create("xdb", 202 ddi_get_instance(vdp->xs_dip), 203 "req_statistics", "block", KSTAT_TYPE_NAMED, 204 nstat, 0)) == NULL) 205 return (B_FALSE); 206 207 vdp->xs_kstats->ks_private = vdp; 208 vdp->xs_kstats->ks_update = xdb_kstat_update; 209 210 knp = vdp->xs_kstats->ks_data; 211 while (nstat > 0) { 212 kstat_named_init(knp, *cp, KSTAT_DATA_UINT64); 213 knp++; 214 cp++; 215 nstat--; 216 } 217 218 kstat_install(vdp->xs_kstats); 219 220 return (B_TRUE); 221 } 222 223 static char * 224 i_pathname(dev_info_t *dip) 225 { 226 char *path, *rv; 227 228 path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 229 (void) ddi_pathname(dip, path); 230 rv = strdup(path); 231 kmem_free(path, MAXPATHLEN); 232 233 return (rv); 234 } 235 236 static buf_t * 237 xdb_get_buf(xdb_t *vdp, blkif_request_t *req, xdb_request_t *xreq) 238 { 239 buf_t *bp; 240 uint8_t segs, curseg; 241 int sectors; 242 int i, err; 243 gnttab_map_grant_ref_t mapops[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 244 ddi_acc_handle_t acchdl; 245 246 acchdl = vdp->xs_ring_hdl; 247 bp = XDB_XREQ2BP(xreq); 248 curseg = xreq->xr_curseg; 249 /* init a new xdb request */ 250 if (req != NULL) { 251 ASSERT(MUTEX_HELD(&vdp->xs_iomutex)); 252 boolean_t pagemapok = B_TRUE; 253 uint8_t op = ddi_get8(acchdl, &req->operation); 254 255 xreq->xr_vdp = vdp; 256 xreq->xr_op = op; 257 xreq->xr_id = ddi_get64(acchdl, &req->id); 258 segs = xreq->xr_buf_pages = ddi_get8(acchdl, &req->nr_segments); 259 if (segs == 0) { 260 if (op != BLKIF_OP_FLUSH_DISKCACHE) 261 cmn_err(CE_WARN, "!non-BLKIF_OP_FLUSH_DISKCACHE" 262 " is seen from domain %d with zero " 263 "length data buffer!", vdp->xs_peer); 264 bioinit(bp); 265 bp->b_bcount = 0; 266 bp->b_lblkno = 0; 267 bp->b_un.b_addr = NULL; 268 return (bp); 269 } else if (op == BLKIF_OP_FLUSH_DISKCACHE) { 270 cmn_err(CE_WARN, "!BLKIF_OP_FLUSH_DISKCACHE" 271 " is seen from domain %d with non-zero " 272 "length data buffer!", vdp->xs_peer); 273 } 274 275 /* 276 * segs should be no bigger than BLKIF_MAX_SEGMENTS_PER_REQUEST 277 * according to the definition of blk interface by Xen 278 * we do sanity check here 279 */ 280 if (segs > BLKIF_MAX_SEGMENTS_PER_REQUEST) 281 segs = xreq->xr_buf_pages = 282 BLKIF_MAX_SEGMENTS_PER_REQUEST; 283 284 for (i = 0; i < segs; i++) { 285 uint8_t fs, ls; 286 287 mapops[i].host_addr = 288 (uint64_t)(uintptr_t)XDB_IOPAGE_VA( 289 vdp->xs_iopage_va, xreq->xr_idx, i); 290 mapops[i].dom = vdp->xs_peer; 291 mapops[i].ref = ddi_get32(acchdl, &req->seg[i].gref); 292 mapops[i].flags = GNTMAP_host_map; 293 if (op != BLKIF_OP_READ) 294 mapops[i].flags |= GNTMAP_readonly; 295 296 fs = ddi_get8(acchdl, &req->seg[i].first_sect); 297 ls = ddi_get8(acchdl, &req->seg[i].last_sect); 298 299 /* 300 * first_sect should be no bigger than last_sect and 301 * both of them should be no bigger than 302 * (PAGESIZE / XB_BSIZE - 1) according to definition 303 * of blk interface by Xen, so sanity check again 304 */ 305 if (fs > (PAGESIZE / XB_BSIZE - 1)) 306 fs = PAGESIZE / XB_BSIZE - 1; 307 if (ls > (PAGESIZE / XB_BSIZE - 1)) 308 ls = PAGESIZE / XB_BSIZE - 1; 309 if (fs > ls) 310 fs = ls; 311 312 xreq->xr_segs[i].fs = fs; 313 xreq->xr_segs[i].ls = ls; 314 } 315 316 /* map in io pages */ 317 err = xen_map_gref(GNTTABOP_map_grant_ref, mapops, i, B_FALSE); 318 if (err != 0) 319 return (NULL); 320 for (i = 0; i < segs; i++) { 321 /* 322 * Although HYPERVISOR_grant_table_op() returned no 323 * error, mapping of each single page can fail. So, 324 * we have to do the check here and handle the error 325 * if needed 326 */ 327 if (mapops[i].status != GNTST_okay) { 328 int j; 329 for (j = 0; j < i; j++) { 330 #ifdef DEBUG 331 unlogva(vdp, mapops[j].host_addr); 332 #endif 333 xen_release_pfn( 334 xreq->xr_plist[j].p_pagenum); 335 } 336 pagemapok = B_FALSE; 337 break; 338 } 339 /* record page mapping handle for unmapping later */ 340 xreq->xr_page_hdls[i] = mapops[i].handle; 341 #ifdef DEBUG 342 logva(vdp, mapops[i].host_addr); 343 #endif 344 /* 345 * Pass the MFNs down using the shadow list (xr_pplist) 346 * 347 * This is pretty ugly since we have implict knowledge 348 * of how the rootnex binds buffers. 349 * The GNTTABOP_map_grant_ref op makes us do some ugly 350 * stuff since we're not allowed to touch these PTEs 351 * from the VM. 352 * 353 * Obviously, these aren't real page_t's. The rootnex 354 * only needs p_pagenum. 355 * Also, don't use btop() here or 32 bit PAE breaks. 356 */ 357 xreq->xr_pplist[i] = &xreq->xr_plist[i]; 358 xreq->xr_plist[i].p_pagenum = 359 xen_assign_pfn(mapops[i].dev_bus_addr >> PAGESHIFT); 360 } 361 362 /* 363 * not all pages mapped in successfully, unmap those mapped-in 364 * page and return failure 365 */ 366 if (!pagemapok) { 367 gnttab_unmap_grant_ref_t unmapop; 368 369 for (i = 0; i < segs; i++) { 370 if (mapops[i].status != GNTST_okay) 371 continue; 372 unmapop.host_addr = 373 (uint64_t)(uintptr_t)XDB_IOPAGE_VA( 374 vdp->xs_iopage_va, xreq->xr_idx, i); 375 unmapop.dev_bus_addr = NULL; 376 unmapop.handle = mapops[i].handle; 377 (void) HYPERVISOR_grant_table_op( 378 GNTTABOP_unmap_grant_ref, &unmapop, 1); 379 } 380 381 return (NULL); 382 } 383 bioinit(bp); 384 bp->b_lblkno = ddi_get64(acchdl, &req->sector_number); 385 bp->b_flags = B_BUSY | B_SHADOW | B_PHYS; 386 bp->b_flags |= (ddi_get8(acchdl, &req->operation) == 387 BLKIF_OP_READ) ? B_READ : (B_WRITE | B_ASYNC); 388 } else { 389 uint64_t blkst; 390 int isread; 391 392 /* reuse this buf */ 393 blkst = bp->b_lblkno + bp->b_bcount / DEV_BSIZE; 394 isread = bp->b_flags & B_READ; 395 bioreset(bp); 396 bp->b_lblkno = blkst; 397 bp->b_flags = B_BUSY | B_SHADOW | B_PHYS; 398 bp->b_flags |= isread ? B_READ : (B_WRITE | B_ASYNC); 399 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, "reuse buf, xreq is %d!!", 400 xreq->xr_idx)); 401 } 402 403 /* form a buf */ 404 bp->b_un.b_addr = XDB_IOPAGE_VA(vdp->xs_iopage_va, xreq->xr_idx, 405 curseg) + xreq->xr_segs[curseg].fs * DEV_BSIZE; 406 bp->b_shadow = &xreq->xr_pplist[curseg]; 407 bp->b_iodone = xdb_biodone; 408 sectors = 0; 409 for (i = curseg; i < xreq->xr_buf_pages; i++) { 410 /* 411 * The xreq->xr_segs[i].fs of the first seg can be non-zero 412 * otherwise, we'll break it into multiple bufs 413 */ 414 if ((i != curseg) && (xreq->xr_segs[i].fs != 0)) { 415 break; 416 } 417 sectors += (xreq->xr_segs[i].ls - xreq->xr_segs[i].fs + 1); 418 } 419 xreq->xr_curseg = i; 420 bp->b_bcount = sectors * DEV_BSIZE; 421 bp->b_bufsize = bp->b_bcount; 422 423 return (bp); 424 } 425 426 static xdb_request_t * 427 xdb_get_req(xdb_t *vdp) 428 { 429 xdb_request_t *req; 430 int idx; 431 432 ASSERT(MUTEX_HELD(&vdp->xs_iomutex)); 433 ASSERT(vdp->xs_free_req != -1); 434 req = &vdp->xs_req[vdp->xs_free_req]; 435 vdp->xs_free_req = req->xr_next; 436 idx = req->xr_idx; 437 bzero(req, sizeof (xdb_request_t)); 438 req->xr_idx = idx; 439 return (req); 440 } 441 442 static void 443 xdb_free_req(xdb_request_t *req) 444 { 445 xdb_t *vdp = req->xr_vdp; 446 447 ASSERT(MUTEX_HELD(&vdp->xs_iomutex)); 448 req->xr_next = vdp->xs_free_req; 449 vdp->xs_free_req = req->xr_idx; 450 } 451 452 static void 453 xdb_response(xdb_t *vdp, blkif_request_t *req, boolean_t ok) 454 { 455 ddi_acc_handle_t acchdl = vdp->xs_ring_hdl; 456 457 if (xdb_push_response(vdp, ddi_get64(acchdl, &req->id), 458 ddi_get8(acchdl, &req->operation), ok)) 459 xvdi_notify_oe(vdp->xs_dip); 460 } 461 462 static void 463 xdb_init_ioreqs(xdb_t *vdp) 464 { 465 int i; 466 467 ASSERT(vdp->xs_nentry); 468 469 if (vdp->xs_req == NULL) 470 vdp->xs_req = kmem_alloc(vdp->xs_nentry * 471 sizeof (xdb_request_t), KM_SLEEP); 472 #ifdef DEBUG 473 if (vdp->page_addrs == NULL) 474 vdp->page_addrs = kmem_zalloc(XDB_MAX_IO_PAGES(vdp) * 475 sizeof (uint64_t), KM_SLEEP); 476 #endif 477 for (i = 0; i < vdp->xs_nentry; i++) { 478 vdp->xs_req[i].xr_idx = i; 479 vdp->xs_req[i].xr_next = i + 1; 480 } 481 vdp->xs_req[vdp->xs_nentry - 1].xr_next = -1; 482 vdp->xs_free_req = 0; 483 484 /* alloc va in host dom for io page mapping */ 485 vdp->xs_iopage_va = vmem_xalloc(heap_arena, 486 XDB_MAX_IO_PAGES(vdp) * PAGESIZE, PAGESIZE, 0, 0, 0, 0, 487 VM_SLEEP); 488 for (i = 0; i < XDB_MAX_IO_PAGES(vdp); i++) 489 hat_prepare_mapping(kas.a_hat, 490 vdp->xs_iopage_va + i * PAGESIZE, NULL); 491 } 492 493 static void 494 xdb_uninit_ioreqs(xdb_t *vdp) 495 { 496 int i; 497 498 for (i = 0; i < XDB_MAX_IO_PAGES(vdp); i++) 499 hat_release_mapping(kas.a_hat, 500 vdp->xs_iopage_va + i * PAGESIZE); 501 vmem_xfree(heap_arena, vdp->xs_iopage_va, 502 XDB_MAX_IO_PAGES(vdp) * PAGESIZE); 503 if (vdp->xs_req != NULL) { 504 kmem_free(vdp->xs_req, vdp->xs_nentry * sizeof (xdb_request_t)); 505 vdp->xs_req = NULL; 506 } 507 #ifdef DEBUG 508 if (vdp->page_addrs != NULL) { 509 kmem_free(vdp->page_addrs, XDB_MAX_IO_PAGES(vdp) * 510 sizeof (uint64_t)); 511 vdp->page_addrs = NULL; 512 } 513 #endif 514 } 515 516 static uint_t 517 xdb_intr(caddr_t arg) 518 { 519 xdb_t *vdp = (xdb_t *)arg; 520 dev_info_t *dip = vdp->xs_dip; 521 blkif_request_t req, *reqp = &req; 522 xdb_request_t *xreq; 523 buf_t *bp; 524 uint8_t op; 525 int ret = DDI_INTR_UNCLAIMED; 526 527 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, 528 "xdb@%s: I/O request received from dom %d", 529 ddi_get_name_addr(dip), vdp->xs_peer)); 530 531 mutex_enter(&vdp->xs_iomutex); 532 533 /* shouldn't touch ring buffer if not in connected state */ 534 if (!vdp->xs_if_connected) { 535 mutex_exit(&vdp->xs_iomutex); 536 return (DDI_INTR_UNCLAIMED); 537 } 538 ASSERT(vdp->xs_hp_connected && vdp->xs_fe_initialised); 539 540 /* 541 * We'll loop till there is no more request in the ring 542 * We won't stuck in this loop for ever since the size of ring buffer 543 * is limited, and frontend will stop pushing requests into it when 544 * the ring buffer is full 545 */ 546 547 /* req_event will be increased in xvdi_ring_get_request() */ 548 while (xdb_get_request(vdp, reqp)) { 549 ret = DDI_INTR_CLAIMED; 550 551 op = ddi_get8(vdp->xs_ring_hdl, &reqp->operation); 552 if (op == BLKIF_OP_READ || 553 op == BLKIF_OP_WRITE || 554 op == BLKIF_OP_WRITE_BARRIER || 555 op == BLKIF_OP_FLUSH_DISKCACHE) { 556 #ifdef DEBUG 557 xdb_dump_request_oe(reqp); 558 #endif 559 xreq = xdb_get_req(vdp); 560 ASSERT(xreq); 561 switch (op) { 562 case BLKIF_OP_READ: 563 vdp->xs_stat_req_reads++; 564 break; 565 case BLKIF_OP_WRITE_BARRIER: 566 vdp->xs_stat_req_barriers++; 567 /* FALLTHRU */ 568 case BLKIF_OP_WRITE: 569 vdp->xs_stat_req_writes++; 570 break; 571 case BLKIF_OP_FLUSH_DISKCACHE: 572 vdp->xs_stat_req_flushes++; 573 break; 574 } 575 576 xreq->xr_curseg = 0; /* start from first segment */ 577 bp = xdb_get_buf(vdp, reqp, xreq); 578 if (bp == NULL) { 579 /* failed to form a buf */ 580 xdb_free_req(xreq); 581 xdb_response(vdp, reqp, B_FALSE); 582 continue; 583 } 584 bp->av_forw = NULL; 585 586 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, 587 " buf %p, blkno %lld, size %lu, addr %p", 588 (void *)bp, (longlong_t)bp->b_blkno, 589 (ulong_t)bp->b_bcount, (void *)bp->b_un.b_addr)); 590 591 /* send bp to underlying blk driver */ 592 if (vdp->xs_f_iobuf == NULL) { 593 vdp->xs_f_iobuf = vdp->xs_l_iobuf = bp; 594 } else { 595 vdp->xs_l_iobuf->av_forw = bp; 596 vdp->xs_l_iobuf = bp; 597 } 598 } else { 599 xdb_response(vdp, reqp, B_FALSE); 600 XDB_DBPRINT(XDB_DBG_IO, (CE_WARN, "xdb@%s: " 601 "Unsupported cmd received from dom %d", 602 ddi_get_name_addr(dip), vdp->xs_peer)); 603 } 604 } 605 /* notify our taskq to push buf to underlying blk driver */ 606 if (ret == DDI_INTR_CLAIMED) 607 cv_broadcast(&vdp->xs_iocv); 608 609 mutex_exit(&vdp->xs_iomutex); 610 611 return (ret); 612 } 613 614 static int 615 xdb_biodone(buf_t *bp) 616 { 617 int i, err, bioerr; 618 uint8_t segs; 619 gnttab_unmap_grant_ref_t unmapops[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 620 xdb_request_t *xreq = XDB_BP2XREQ(bp); 621 xdb_t *vdp = xreq->xr_vdp; 622 buf_t *nbp; 623 624 bioerr = geterror(bp); 625 if (bioerr) 626 XDB_DBPRINT(XDB_DBG_IO, (CE_WARN, "xdb@%s: I/O error %d", 627 ddi_get_name_addr(vdp->xs_dip), bioerr)); 628 629 /* check if we are done w/ this I/O request */ 630 if ((bioerr == 0) && (xreq->xr_curseg < xreq->xr_buf_pages)) { 631 nbp = xdb_get_buf(vdp, NULL, xreq); 632 if (nbp) { 633 err = ldi_strategy(vdp->xs_ldi_hdl, nbp); 634 if (err == 0) { 635 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, 636 "sent buf to backend ok")); 637 return (DDI_SUCCESS); 638 } 639 bioerr = EIO; 640 XDB_DBPRINT(XDB_DBG_IO, (CE_WARN, "xdb@%s: " 641 "sent buf to backend dev failed, err=%d", 642 ddi_get_name_addr(vdp->xs_dip), err)); 643 } else { 644 bioerr = EIO; 645 } 646 } 647 648 /* unmap io pages */ 649 segs = xreq->xr_buf_pages; 650 /* 651 * segs should be no bigger than BLKIF_MAX_SEGMENTS_PER_REQUEST 652 * according to the definition of blk interface by Xen 653 */ 654 ASSERT(segs <= BLKIF_MAX_SEGMENTS_PER_REQUEST); 655 for (i = 0; i < segs; i++) { 656 unmapops[i].host_addr = (uint64_t)(uintptr_t)XDB_IOPAGE_VA( 657 vdp->xs_iopage_va, xreq->xr_idx, i); 658 #ifdef DEBUG 659 mutex_enter(&vdp->xs_iomutex); 660 unlogva(vdp, unmapops[i].host_addr); 661 mutex_exit(&vdp->xs_iomutex); 662 #endif 663 unmapops[i].dev_bus_addr = NULL; 664 unmapops[i].handle = xreq->xr_page_hdls[i]; 665 } 666 err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, 667 unmapops, segs); 668 ASSERT(!err); 669 670 /* 671 * If we have reached a barrier write or a cache flush , then we must 672 * flush all our I/Os. 673 */ 674 if (xreq->xr_op == BLKIF_OP_WRITE_BARRIER || 675 xreq->xr_op == BLKIF_OP_FLUSH_DISKCACHE) { 676 /* 677 * XXX At this point the write did succeed, so I don't 678 * believe we should report an error because the flush 679 * failed. However, this is a debatable point, so 680 * maybe we need to think more carefully about this. 681 * For now, just cast to void. 682 */ 683 (void) ldi_ioctl(vdp->xs_ldi_hdl, 684 DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, kcred, NULL); 685 } 686 687 mutex_enter(&vdp->xs_iomutex); 688 689 /* send response back to frontend */ 690 if (vdp->xs_if_connected) { 691 ASSERT(vdp->xs_hp_connected && vdp->xs_fe_initialised); 692 if (xdb_push_response(vdp, xreq->xr_id, xreq->xr_op, bioerr)) 693 xvdi_notify_oe(vdp->xs_dip); 694 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, 695 "sent resp back to frontend, id=%llu", 696 (unsigned long long)xreq->xr_id)); 697 } 698 /* free io resources */ 699 biofini(bp); 700 xdb_free_req(xreq); 701 702 vdp->xs_ionum--; 703 if (!vdp->xs_if_connected && (vdp->xs_ionum == 0)) { 704 /* we're closing, someone is waiting for I/O clean-up */ 705 cv_signal(&vdp->xs_ionumcv); 706 } 707 708 mutex_exit(&vdp->xs_iomutex); 709 710 return (DDI_SUCCESS); 711 } 712 713 static int 714 xdb_bindto_frontend(xdb_t *vdp) 715 { 716 int err; 717 char *oename; 718 grant_ref_t gref; 719 evtchn_port_t evtchn; 720 dev_info_t *dip = vdp->xs_dip; 721 char protocol[64] = ""; 722 723 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 724 725 /* 726 * Switch to the XenbusStateInitialised state. This let's the 727 * frontend know that we're about to negotiate a connection. 728 */ 729 (void) xvdi_switch_state(dip, XBT_NULL, XenbusStateInitialised); 730 731 /* 732 * Gather info from frontend 733 */ 734 oename = xvdi_get_oename(dip); 735 if (oename == NULL) 736 return (DDI_FAILURE); 737 738 err = xenbus_gather(XBT_NULL, oename, 739 XBP_RING_REF, "%lu", &gref, 740 XBP_EVENT_CHAN, "%u", &evtchn, 741 NULL); 742 if (err != 0) { 743 xvdi_dev_error(dip, err, 744 "Getting ring-ref and evtchn from frontend"); 745 return (DDI_FAILURE); 746 } 747 748 vdp->xs_blk_protocol = BLKIF_PROTOCOL_NATIVE; 749 vdp->xs_nentry = BLKIF_RING_SIZE; 750 vdp->xs_entrysize = sizeof (union blkif_sring_entry); 751 752 err = xenbus_gather(XBT_NULL, oename, 753 XBP_PROTOCOL, "%63s", protocol, NULL); 754 if (err) 755 (void) strcpy(protocol, "unspecified, assuming native"); 756 else { 757 /* 758 * We must check for NATIVE first, so that the fast path 759 * is taken for copying data from the guest to the host. 760 */ 761 if (strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE) != 0) { 762 if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_32) == 0) { 763 vdp->xs_blk_protocol = BLKIF_PROTOCOL_X86_32; 764 vdp->xs_nentry = BLKIF_X86_32_RING_SIZE; 765 vdp->xs_entrysize = 766 sizeof (union blkif_x86_32_sring_entry); 767 } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_64) == 768 0) { 769 vdp->xs_blk_protocol = BLKIF_PROTOCOL_X86_64; 770 vdp->xs_nentry = BLKIF_X86_64_RING_SIZE; 771 vdp->xs_entrysize = 772 sizeof (union blkif_x86_64_sring_entry); 773 } else { 774 xvdi_fatal_error(dip, err, "unknown protocol"); 775 return (DDI_FAILURE); 776 } 777 } 778 } 779 #ifdef DEBUG 780 cmn_err(CE_NOTE, "!xdb@%s: blkif protocol '%s' ", 781 ddi_get_name_addr(dip), protocol); 782 #endif 783 784 /* 785 * Map and init ring. The ring parameters must match those which 786 * have been allocated in the front end. 787 */ 788 if (xvdi_map_ring(dip, vdp->xs_nentry, vdp->xs_entrysize, 789 gref, &vdp->xs_ring) != DDI_SUCCESS) 790 return (DDI_FAILURE); 791 792 /* 793 * This will be removed after we use shadow I/O ring request since 794 * we don't need to access the ring itself directly, thus the access 795 * handle is not needed 796 */ 797 vdp->xs_ring_hdl = vdp->xs_ring->xr_acc_hdl; 798 799 /* bind event channel */ 800 err = xvdi_bind_evtchn(dip, evtchn); 801 if (err != DDI_SUCCESS) { 802 xvdi_unmap_ring(vdp->xs_ring); 803 return (DDI_FAILURE); 804 } 805 806 return (DDI_SUCCESS); 807 } 808 809 static void 810 xdb_unbindfrom_frontend(xdb_t *vdp) 811 { 812 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 813 814 xvdi_free_evtchn(vdp->xs_dip); 815 xvdi_unmap_ring(vdp->xs_ring); 816 } 817 818 /* 819 * xdb_params_change() initiates a allows change to the underlying device/file 820 * that the backend is accessing. It does this by disconnecting from the 821 * frontend, closing the old device, clearing a bunch of xenbus parameters, 822 * and switching back to the XenbusStateInitialising state. The frontend 823 * should notice this transition to the XenbusStateInitialising state and 824 * should attempt to reconnect to us (the backend). 825 */ 826 static void 827 xdb_params_change(xdb_t *vdp, char *params, boolean_t update_xs) 828 { 829 xenbus_transaction_t xbt; 830 dev_info_t *dip = vdp->xs_dip; 831 char *xsname; 832 int err; 833 834 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 835 ASSERT(vdp->xs_params_path != NULL); 836 837 if ((xsname = xvdi_get_xsname(dip)) == NULL) 838 return; 839 if (strcmp(vdp->xs_params_path, params) == 0) 840 return; 841 842 /* 843 * Close the device we're currently accessing and update the 844 * path which points to our backend device/file. 845 */ 846 xdb_close(dip); 847 vdp->xs_fe_initialised = B_FALSE; 848 849 trans_retry: 850 if ((err = xenbus_transaction_start(&xbt)) != 0) { 851 xvdi_dev_error(dip, err, "params change transaction init"); 852 goto errout; 853 } 854 855 /* 856 * Delete all the xenbus properties that are connection dependant 857 * and go back to the initializing state so that the frontend 858 * driver can re-negotiate a connection. 859 */ 860 if (((err = xenbus_rm(xbt, xsname, XBP_FB)) != 0) || 861 ((err = xenbus_rm(xbt, xsname, XBP_INFO)) != 0) || 862 ((err = xenbus_rm(xbt, xsname, "sector-size")) != 0) || 863 ((err = xenbus_rm(xbt, xsname, XBP_SECTORS)) != 0) || 864 ((err = xenbus_rm(xbt, xsname, "instance")) != 0) || 865 ((err = xenbus_rm(xbt, xsname, "node")) != 0) || 866 (update_xs && ((err = xenbus_printf(xbt, xsname, 867 "params", "%s", params)) != 0)) || 868 ((err = xvdi_switch_state(dip, 869 xbt, XenbusStateInitialising) > 0))) { 870 (void) xenbus_transaction_end(xbt, 1); 871 xvdi_dev_error(dip, err, "params change transaction setup"); 872 goto errout; 873 } 874 875 if ((err = xenbus_transaction_end(xbt, 0)) != 0) { 876 if (err == EAGAIN) { 877 /* transaction is ended, don't need to abort it */ 878 goto trans_retry; 879 } 880 xvdi_dev_error(dip, err, "params change transaction commit"); 881 goto errout; 882 } 883 884 /* Change the device that we plan to access */ 885 strfree(vdp->xs_params_path); 886 vdp->xs_params_path = strdup(params); 887 return; 888 889 errout: 890 (void) xvdi_switch_state(dip, xbt, XenbusStateInitialising); 891 } 892 893 /* 894 * xdb_watch_params_cb() - This callback is invoked whenever there 895 * is an update to the following xenbus parameter: 896 * /local/domain/0/backend/vbd/<domU_id>/<domU_dev>/params 897 * 898 * This normally happens during xm block-configure operations, which 899 * are used to change CD device images for HVM domUs. 900 */ 901 /*ARGSUSED*/ 902 static void 903 xdb_watch_params_cb(dev_info_t *dip, const char *path, void *arg) 904 { 905 xdb_t *vdp = (xdb_t *)ddi_get_driver_private(dip); 906 char *xsname, *oename, *str, *str2; 907 908 if (((xsname = xvdi_get_xsname(dip)) == NULL) || 909 ((oename = xvdi_get_oename(dip)) == NULL)) { 910 return; 911 } 912 913 mutex_enter(&vdp->xs_cbmutex); 914 915 if (xenbus_read_str(xsname, "params", &str) != 0) { 916 mutex_exit(&vdp->xs_cbmutex); 917 return; 918 } 919 920 if (strcmp(vdp->xs_params_path, str) == 0) { 921 /* Nothing todo */ 922 mutex_exit(&vdp->xs_cbmutex); 923 strfree(str); 924 return; 925 } 926 927 /* 928 * If the frontend isn't a cd device, doesn't support media 929 * requests, or has locked the media, then we can't change 930 * the params value. restore the current value. 931 */ 932 str2 = NULL; 933 if (!XDB_IS_FE_CD(vdp) || 934 (xenbus_read_str(oename, XBP_MEDIA_REQ, &str2) != 0) || 935 (strcmp(str2, XBV_MEDIA_REQ_LOCK) == 0)) { 936 if (str2 != NULL) 937 strfree(str2); 938 strfree(str); 939 940 str = i_pathname(dip); 941 cmn_err(CE_NOTE, 942 "!%s: media locked, ignoring params update", str); 943 strfree(str); 944 945 mutex_exit(&vdp->xs_cbmutex); 946 return; 947 } 948 949 XDB_DBPRINT(XDB_DBG_INFO, (CE_NOTE, 950 "block-configure params request: \"%s\"", str)); 951 952 xdb_params_change(vdp, str, B_FALSE); 953 mutex_exit(&vdp->xs_cbmutex); 954 strfree(str); 955 } 956 957 /* 958 * xdb_watch_media_req_cb() - This callback is invoked whenever there 959 * is an update to the following xenbus parameter: 960 * /local/domain/<domU_id>/device/vbd/<domU_dev>/media-req 961 * 962 * Media requests are only supported on CD devices and are issued by 963 * the frontend. Currently the only supported media request operaions 964 * are "lock" and "eject". A "lock" prevents the backend from changing 965 * the backing device/file (via xm block-configure). An "eject" requests 966 * tells the backend device that it should disconnect from the frontend 967 * and closing the backing device/file that is currently in use. 968 */ 969 /*ARGSUSED*/ 970 static void 971 xdb_watch_media_req_cb(dev_info_t *dip, const char *path, void *arg) 972 { 973 xdb_t *vdp = (xdb_t *)ddi_get_driver_private(dip); 974 char *oename, *str; 975 976 mutex_enter(&vdp->xs_cbmutex); 977 978 if ((oename = xvdi_get_oename(dip)) == NULL) { 979 mutex_exit(&vdp->xs_cbmutex); 980 return; 981 } 982 983 if (xenbus_read_str(oename, XBP_MEDIA_REQ, &str) != 0) { 984 mutex_exit(&vdp->xs_cbmutex); 985 return; 986 } 987 988 if (!XDB_IS_FE_CD(vdp)) { 989 xvdi_dev_error(dip, EINVAL, 990 "media-req only supported for cdrom devices"); 991 mutex_exit(&vdp->xs_cbmutex); 992 return; 993 } 994 995 if (strcmp(str, XBV_MEDIA_REQ_EJECT) != 0) { 996 mutex_exit(&vdp->xs_cbmutex); 997 strfree(str); 998 return; 999 } 1000 strfree(str); 1001 1002 XDB_DBPRINT(XDB_DBG_INFO, (CE_NOTE, "media eject request")); 1003 1004 xdb_params_change(vdp, "", B_TRUE); 1005 (void) xenbus_printf(XBT_NULL, oename, 1006 XBP_MEDIA_REQ, "%s", XBV_MEDIA_REQ_NONE); 1007 mutex_exit(&vdp->xs_cbmutex); 1008 } 1009 1010 /* 1011 * If we're dealing with a cdrom device, let the frontend know that 1012 * we support media requests via XBP_MEDIA_REQ_SUP, and setup a watch 1013 * to handle those frontend media request changes, which modify the 1014 * following xenstore parameter: 1015 * /local/domain/<domU_id>/device/vbd/<domU_dev>/media-req 1016 */ 1017 static boolean_t 1018 xdb_media_req_init(xdb_t *vdp) 1019 { 1020 dev_info_t *dip = vdp->xs_dip; 1021 char *xsname, *oename; 1022 1023 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 1024 1025 if (((xsname = xvdi_get_xsname(dip)) == NULL) || 1026 ((oename = xvdi_get_oename(dip)) == NULL)) 1027 return (B_FALSE); 1028 1029 if (!XDB_IS_FE_CD(vdp)) 1030 return (B_TRUE); 1031 1032 if (xenbus_printf(XBT_NULL, xsname, XBP_MEDIA_REQ_SUP, "%d", 1) != 0) 1033 return (B_FALSE); 1034 1035 if (xvdi_add_xb_watch_handler(dip, oename, 1036 XBP_MEDIA_REQ, xdb_watch_media_req_cb, NULL) != DDI_SUCCESS) { 1037 xvdi_dev_error(dip, EAGAIN, 1038 "Failed to register watch for cdrom media requests"); 1039 return (B_FALSE); 1040 } 1041 1042 return (B_TRUE); 1043 } 1044 1045 /* 1046 * Get our params value. Also, if we're using "params" then setup a 1047 * watch to handle xm block-configure operations which modify the 1048 * following xenstore parameter: 1049 * /local/domain/0/backend/vbd/<domU_id>/<domU_dev>/params 1050 */ 1051 static boolean_t 1052 xdb_params_init(xdb_t *vdp) 1053 { 1054 dev_info_t *dip = vdp->xs_dip; 1055 char *str, *xsname; 1056 int err, watch_params = B_FALSE; 1057 1058 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 1059 ASSERT(vdp->xs_params_path == NULL); 1060 1061 if ((xsname = xvdi_get_xsname(dip)) == NULL) 1062 return (B_FALSE); 1063 1064 if ((err = xenbus_read_str(xsname, 1065 "dynamic-device-path", &str)) == ENOENT) { 1066 err = xenbus_read_str(xsname, "params", &str); 1067 watch_params = B_TRUE; 1068 } 1069 if (err != 0) 1070 return (B_FALSE); 1071 vdp->xs_params_path = str; 1072 1073 /* 1074 * If we got our backing store path from "dynamic-device-path" then 1075 * there's no reason to watch "params" 1076 */ 1077 if (!watch_params) 1078 return (B_TRUE); 1079 1080 if (xvdi_add_xb_watch_handler(dip, xsname, "params", 1081 xdb_watch_params_cb, NULL) != DDI_SUCCESS) { 1082 strfree(vdp->xs_params_path); 1083 vdp->xs_params_path = NULL; 1084 return (B_FALSE); 1085 } 1086 1087 return (B_TRUE); 1088 } 1089 1090 #define LOFI_CTRL_NODE "/dev/lofictl" 1091 #define LOFI_DEV_NODE "/devices/pseudo/lofi@0:" 1092 #define LOFI_MODE (FREAD | FWRITE | FEXCL) 1093 1094 static int 1095 xdb_setup_node(xdb_t *vdp, char *path) 1096 { 1097 dev_info_t *dip = vdp->xs_dip; 1098 char *xsname, *str; 1099 ldi_handle_t ldi_hdl; 1100 struct lofi_ioctl *li; 1101 int minor, err; 1102 1103 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 1104 1105 if ((xsname = xvdi_get_xsname(dip)) == NULL) 1106 return (DDI_FAILURE); 1107 1108 if ((err = xenbus_read_str(xsname, "type", &str)) != 0) { 1109 xvdi_dev_error(dip, err, "Getting type from backend device"); 1110 return (DDI_FAILURE); 1111 } 1112 if (strcmp(str, "file") == 0) 1113 vdp->xs_type |= XDB_DEV_BE_LOFI; 1114 strfree(str); 1115 1116 if (!XDB_IS_BE_LOFI(vdp)) { 1117 (void) strlcpy(path, vdp->xs_params_path, MAXPATHLEN); 1118 ASSERT(vdp->xs_lofi_path == NULL); 1119 return (DDI_SUCCESS); 1120 } 1121 1122 do { 1123 err = ldi_open_by_name(LOFI_CTRL_NODE, LOFI_MODE, kcred, 1124 &ldi_hdl, vdp->xs_ldi_li); 1125 } while (err == EBUSY); 1126 if (err != 0) { 1127 return (DDI_FAILURE); 1128 } 1129 1130 li = kmem_zalloc(sizeof (*li), KM_SLEEP); 1131 (void) strlcpy(li->li_filename, vdp->xs_params_path, 1132 sizeof (li->li_filename)); 1133 err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li, 1134 LOFI_MODE | FKIOCTL, kcred, &minor); 1135 (void) ldi_close(ldi_hdl, LOFI_MODE, kcred); 1136 kmem_free(li, sizeof (*li)); 1137 1138 if (err != 0) { 1139 cmn_err(CE_WARN, "xdb@%s: Failed to create lofi dev for %s", 1140 ddi_get_name_addr(dip), vdp->xs_params_path); 1141 return (DDI_FAILURE); 1142 } 1143 1144 /* 1145 * return '/devices/...' instead of '/dev/lofi/...' since the 1146 * former is available immediately after calling ldi_ioctl 1147 */ 1148 (void) snprintf(path, MAXPATHLEN, LOFI_DEV_NODE "%d", minor); 1149 (void) xenbus_printf(XBT_NULL, xsname, "node", "%s", path); 1150 1151 ASSERT(vdp->xs_lofi_path == NULL); 1152 vdp->xs_lofi_path = strdup(path); 1153 1154 return (DDI_SUCCESS); 1155 } 1156 1157 static void 1158 xdb_teardown_node(xdb_t *vdp) 1159 { 1160 dev_info_t *dip = vdp->xs_dip; 1161 ldi_handle_t ldi_hdl; 1162 struct lofi_ioctl *li; 1163 int err; 1164 1165 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 1166 1167 if (!XDB_IS_BE_LOFI(vdp)) 1168 return; 1169 1170 vdp->xs_type &= ~XDB_DEV_BE_LOFI; 1171 ASSERT(vdp->xs_lofi_path != NULL); 1172 1173 li = kmem_zalloc(sizeof (*li), KM_SLEEP); 1174 (void) strlcpy(li->li_filename, vdp->xs_params_path, 1175 sizeof (li->li_filename)); 1176 1177 do { 1178 err = ldi_open_by_name(LOFI_CTRL_NODE, LOFI_MODE, kcred, 1179 &ldi_hdl, vdp->xs_ldi_li); 1180 } while (err == EBUSY); 1181 1182 if (err != 0) { 1183 kmem_free(li, sizeof (*li)); 1184 return; 1185 } 1186 1187 if (ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE, (intptr_t)li, 1188 LOFI_MODE | FKIOCTL, kcred, NULL) != 0) { 1189 cmn_err(CE_WARN, "xdb@%s: Failed to delete lofi dev for %s", 1190 ddi_get_name_addr(dip), li->li_filename); 1191 } 1192 1193 (void) ldi_close(ldi_hdl, LOFI_MODE, kcred); 1194 kmem_free(li, sizeof (*li)); 1195 1196 strfree(vdp->xs_lofi_path); 1197 vdp->xs_lofi_path = NULL; 1198 } 1199 1200 static int 1201 xdb_open_device(xdb_t *vdp) 1202 { 1203 dev_info_t *dip = vdp->xs_dip; 1204 uint64_t devsize; 1205 int blksize; 1206 char *nodepath; 1207 1208 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 1209 1210 if (strlen(vdp->xs_params_path) == 0) { 1211 /* 1212 * it's possible to have no backing device when dealing 1213 * with a pv cdrom drive that has no virtual cd associated 1214 * with it. 1215 */ 1216 ASSERT(XDB_IS_FE_CD(vdp)); 1217 ASSERT(vdp->xs_sectors == 0); 1218 ASSERT(vdp->xs_ldi_li == NULL); 1219 ASSERT(vdp->xs_ldi_hdl == NULL); 1220 return (DDI_SUCCESS); 1221 } 1222 1223 if (ldi_ident_from_dip(dip, &vdp->xs_ldi_li) != 0) 1224 return (DDI_FAILURE); 1225 1226 nodepath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 1227 1228 /* try to open backend device */ 1229 if (xdb_setup_node(vdp, nodepath) != DDI_SUCCESS) { 1230 xvdi_dev_error(dip, ENXIO, 1231 "Getting device path of backend device"); 1232 ldi_ident_release(vdp->xs_ldi_li); 1233 kmem_free(nodepath, MAXPATHLEN); 1234 return (DDI_FAILURE); 1235 } 1236 1237 if (ldi_open_by_name(nodepath, 1238 FREAD | (XDB_IS_RO(vdp) ? 0 : FWRITE), 1239 kcred, &vdp->xs_ldi_hdl, vdp->xs_ldi_li) != 0) { 1240 xdb_teardown_node(vdp); 1241 ldi_ident_release(vdp->xs_ldi_li); 1242 cmn_err(CE_WARN, "xdb@%s: Failed to open: %s", 1243 ddi_get_name_addr(dip), nodepath); 1244 kmem_free(nodepath, MAXPATHLEN); 1245 return (DDI_FAILURE); 1246 } 1247 1248 if (ldi_get_size(vdp->xs_ldi_hdl, &devsize) != DDI_SUCCESS) { 1249 (void) ldi_close(vdp->xs_ldi_hdl, 1250 FREAD | (XDB_IS_RO(vdp) ? 0 : FWRITE), kcred); 1251 xdb_teardown_node(vdp); 1252 ldi_ident_release(vdp->xs_ldi_li); 1253 kmem_free(nodepath, MAXPATHLEN); 1254 return (DDI_FAILURE); 1255 } 1256 1257 blksize = ldi_prop_get_int64(vdp->xs_ldi_hdl, 1258 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 1259 "blksize", DEV_BSIZE); 1260 if (blksize == DEV_BSIZE) 1261 blksize = ldi_prop_get_int(vdp->xs_ldi_hdl, 1262 LDI_DEV_T_ANY | DDI_PROP_DONTPASS | 1263 DDI_PROP_NOTPROM, "device-blksize", DEV_BSIZE); 1264 1265 vdp->xs_sec_size = blksize; 1266 vdp->xs_sectors = devsize / blksize; 1267 1268 /* check if the underlying device is a CD/DVD disc */ 1269 if (ldi_prop_get_int(vdp->xs_ldi_hdl, LDI_DEV_T_ANY | DDI_PROP_DONTPASS, 1270 INQUIRY_DEVICE_TYPE, DTYPE_DIRECT) == DTYPE_RODIRECT) 1271 vdp->xs_type |= XDB_DEV_BE_CD; 1272 1273 /* check if the underlying device is a removable disk */ 1274 if (ldi_prop_exists(vdp->xs_ldi_hdl, 1275 LDI_DEV_T_ANY | DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 1276 "removable-media")) 1277 vdp->xs_type |= XDB_DEV_BE_RMB; 1278 1279 kmem_free(nodepath, MAXPATHLEN); 1280 return (DDI_SUCCESS); 1281 } 1282 1283 static void 1284 xdb_close_device(xdb_t *vdp) 1285 { 1286 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 1287 1288 if (strlen(vdp->xs_params_path) == 0) { 1289 ASSERT(XDB_IS_FE_CD(vdp)); 1290 ASSERT(vdp->xs_sectors == 0); 1291 ASSERT(vdp->xs_ldi_li == NULL); 1292 ASSERT(vdp->xs_ldi_hdl == NULL); 1293 return; 1294 } 1295 1296 (void) ldi_close(vdp->xs_ldi_hdl, 1297 FREAD | (XDB_IS_RO(vdp) ? 0 : FWRITE), kcred); 1298 xdb_teardown_node(vdp); 1299 ldi_ident_release(vdp->xs_ldi_li); 1300 vdp->xs_type &= ~(XDB_DEV_BE_CD | XDB_DEV_BE_RMB); 1301 vdp->xs_sectors = 0; 1302 vdp->xs_ldi_li = NULL; 1303 vdp->xs_ldi_hdl = NULL; 1304 } 1305 1306 /* 1307 * Kick-off connect process 1308 * If xs_fe_initialised == B_TRUE and xs_hp_connected == B_TRUE 1309 * the xs_if_connected will be changed to B_TRUE on success, 1310 */ 1311 static void 1312 xdb_start_connect(xdb_t *vdp) 1313 { 1314 xenbus_transaction_t xbt; 1315 dev_info_t *dip = vdp->xs_dip; 1316 boolean_t fb_exists; 1317 int err, instance = ddi_get_instance(dip); 1318 uint64_t sectors; 1319 uint_t dinfo, ssize; 1320 char *xsname; 1321 1322 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 1323 1324 if (((xsname = xvdi_get_xsname(dip)) == NULL) || 1325 ((vdp->xs_peer = xvdi_get_oeid(dip)) == (domid_t)-1)) 1326 return; 1327 1328 mutex_enter(&vdp->xs_iomutex); 1329 /* 1330 * if the hotplug scripts haven't run or if the frontend is not 1331 * initialized, then we can't try to connect. 1332 */ 1333 if (!vdp->xs_hp_connected || !vdp->xs_fe_initialised) { 1334 ASSERT(!vdp->xs_if_connected); 1335 mutex_exit(&vdp->xs_iomutex); 1336 return; 1337 } 1338 1339 /* If we're already connected then there's nothing todo */ 1340 if (vdp->xs_if_connected) { 1341 mutex_exit(&vdp->xs_iomutex); 1342 return; 1343 } 1344 mutex_exit(&vdp->xs_iomutex); 1345 1346 /* 1347 * Start connect to frontend only when backend device are ready 1348 * and frontend has moved to XenbusStateInitialised, which means 1349 * ready to connect. 1350 */ 1351 XDB_DBPRINT(XDB_DBG_INFO, (CE_NOTE, 1352 "xdb@%s: starting connection process", ddi_get_name_addr(dip))); 1353 1354 if (xdb_open_device(vdp) != DDI_SUCCESS) 1355 return; 1356 1357 if (xdb_bindto_frontend(vdp) != DDI_SUCCESS) { 1358 xdb_close_device(vdp); 1359 return; 1360 } 1361 1362 /* init i/o requests */ 1363 xdb_init_ioreqs(vdp); 1364 1365 if (ddi_add_intr(dip, 0, NULL, NULL, xdb_intr, (caddr_t)vdp) 1366 != DDI_SUCCESS) { 1367 xdb_uninit_ioreqs(vdp); 1368 xdb_unbindfrom_frontend(vdp); 1369 xdb_close_device(vdp); 1370 return; 1371 } 1372 1373 dinfo = 0; 1374 if (XDB_IS_RO(vdp)) 1375 dinfo |= VDISK_READONLY; 1376 if (XDB_IS_BE_RMB(vdp)) 1377 dinfo |= VDISK_REMOVABLE; 1378 if (XDB_IS_BE_CD(vdp)) 1379 dinfo |= VDISK_CDROM; 1380 if (XDB_IS_FE_CD(vdp)) 1381 dinfo |= VDISK_REMOVABLE | VDISK_CDROM; 1382 1383 /* 1384 * we can recieve intr any time from now on 1385 * mark that we're ready to take intr 1386 */ 1387 mutex_enter(&vdp->xs_iomutex); 1388 ASSERT(vdp->xs_fe_initialised); 1389 vdp->xs_if_connected = B_TRUE; 1390 mutex_exit(&vdp->xs_iomutex); 1391 1392 trans_retry: 1393 /* write into xenstore the info needed by frontend */ 1394 if ((err = xenbus_transaction_start(&xbt)) != 0) { 1395 xvdi_dev_error(dip, err, "connect transaction init"); 1396 goto errout; 1397 } 1398 1399 /* If feature-barrier isn't present in xenstore, add it. */ 1400 fb_exists = xenbus_exists(xsname, XBP_FB); 1401 1402 ssize = (vdp->xs_sec_size == 0) ? DEV_BSIZE : vdp->xs_sec_size; 1403 sectors = vdp->xs_sectors; 1404 if (((!fb_exists && 1405 (err = xenbus_printf(xbt, xsname, XBP_FB, "%d", 1)))) || 1406 (err = xenbus_printf(xbt, xsname, XBP_INFO, "%u", dinfo)) || 1407 (err = xenbus_printf(xbt, xsname, XBP_SECTOR_SIZE, "%u", ssize)) || 1408 (err = xenbus_printf(xbt, xsname, 1409 XBP_SECTORS, "%"PRIu64, sectors)) || 1410 (err = xenbus_printf(xbt, xsname, "instance", "%d", instance)) || 1411 ((err = xvdi_switch_state(dip, xbt, XenbusStateConnected)) > 0)) { 1412 (void) xenbus_transaction_end(xbt, 1); 1413 xvdi_dev_error(dip, err, "connect transaction setup"); 1414 goto errout; 1415 } 1416 1417 if ((err = xenbus_transaction_end(xbt, 0)) != 0) { 1418 if (err == EAGAIN) { 1419 /* transaction is ended, don't need to abort it */ 1420 goto trans_retry; 1421 } 1422 xvdi_dev_error(dip, err, "connect transaction commit"); 1423 goto errout; 1424 } 1425 1426 return; 1427 1428 errout: 1429 xdb_close(dip); 1430 } 1431 1432 /* 1433 * Disconnect from frontend and close backend device 1434 */ 1435 static void 1436 xdb_close(dev_info_t *dip) 1437 { 1438 xdb_t *vdp = (xdb_t *)ddi_get_driver_private(dip); 1439 1440 ASSERT(MUTEX_HELD(&vdp->xs_cbmutex)); 1441 mutex_enter(&vdp->xs_iomutex); 1442 1443 /* 1444 * if the hotplug scripts haven't run or if the frontend is not 1445 * initialized, then we can't be connected, so there's no 1446 * connection to close. 1447 */ 1448 if (!vdp->xs_hp_connected || !vdp->xs_fe_initialised) { 1449 ASSERT(!vdp->xs_if_connected); 1450 mutex_exit(&vdp->xs_iomutex); 1451 return; 1452 } 1453 1454 /* if we're not connected, there's nothing to do */ 1455 if (!vdp->xs_if_connected) { 1456 cv_broadcast(&vdp->xs_iocv); 1457 mutex_exit(&vdp->xs_iomutex); 1458 return; 1459 } 1460 1461 XDB_DBPRINT(XDB_DBG_INFO, (CE_NOTE, "closing while connected")); 1462 1463 vdp->xs_if_connected = B_FALSE; 1464 cv_broadcast(&vdp->xs_iocv); 1465 1466 mutex_exit(&vdp->xs_iomutex); 1467 1468 /* stop accepting I/O request from frontend */ 1469 ddi_remove_intr(dip, 0, NULL); 1470 1471 /* clear all on-going I/Os, if any */ 1472 mutex_enter(&vdp->xs_iomutex); 1473 while (vdp->xs_ionum > 0) 1474 cv_wait(&vdp->xs_ionumcv, &vdp->xs_iomutex); 1475 mutex_exit(&vdp->xs_iomutex); 1476 1477 /* clean up resources and close this interface */ 1478 xdb_uninit_ioreqs(vdp); 1479 xdb_unbindfrom_frontend(vdp); 1480 xdb_close_device(vdp); 1481 vdp->xs_peer = (domid_t)-1; 1482 } 1483 1484 static void 1485 xdb_send_buf(void *arg) 1486 { 1487 xdb_t *vdp = (xdb_t *)arg; 1488 buf_t *bp; 1489 int err; 1490 1491 mutex_enter(&vdp->xs_iomutex); 1492 while (vdp->xs_send_buf) { 1493 if ((bp = vdp->xs_f_iobuf) == NULL) { 1494 /* wait for some io to send */ 1495 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, 1496 "send buf waiting for io")); 1497 cv_wait(&vdp->xs_iocv, &vdp->xs_iomutex); 1498 continue; 1499 } 1500 1501 vdp->xs_f_iobuf = bp->av_forw; 1502 bp->av_forw = NULL; 1503 vdp->xs_ionum++; 1504 1505 mutex_exit(&vdp->xs_iomutex); 1506 if (bp->b_bcount == 0) { 1507 /* no I/O needs to be done */ 1508 (void) xdb_biodone(bp); 1509 mutex_enter(&vdp->xs_iomutex); 1510 continue; 1511 } 1512 1513 err = EIO; 1514 if (vdp->xs_ldi_hdl != NULL) 1515 err = ldi_strategy(vdp->xs_ldi_hdl, bp); 1516 if (err != 0) { 1517 bp->b_flags |= B_ERROR; 1518 (void) xdb_biodone(bp); 1519 XDB_DBPRINT(XDB_DBG_IO, (CE_WARN, 1520 "xdb@%s: sent buf to backend devfailed, err=%d", 1521 ddi_get_name_addr(vdp->xs_dip), err)); 1522 } else { 1523 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, 1524 "sent buf to backend ok")); 1525 } 1526 mutex_enter(&vdp->xs_iomutex); 1527 } 1528 XDB_DBPRINT(XDB_DBG_IO, (CE_NOTE, "send buf finishing")); 1529 mutex_exit(&vdp->xs_iomutex); 1530 } 1531 1532 /*ARGSUSED*/ 1533 static void 1534 xdb_hp_state_change(dev_info_t *dip, ddi_eventcookie_t id, void *arg, 1535 void *impl_data) 1536 { 1537 xendev_hotplug_state_t state = *(xendev_hotplug_state_t *)impl_data; 1538 xdb_t *vdp = (xdb_t *)ddi_get_driver_private(dip); 1539 1540 XDB_DBPRINT(XDB_DBG_INFO, (CE_NOTE, "xdb@%s: " 1541 "hotplug status change to %d!", ddi_get_name_addr(dip), state)); 1542 1543 if (state != Connected) 1544 return; 1545 1546 mutex_enter(&vdp->xs_cbmutex); 1547 1548 /* If hotplug script have already run, there's nothing todo */ 1549 if (vdp->xs_hp_connected) { 1550 mutex_exit(&vdp->xs_cbmutex); 1551 return; 1552 } 1553 1554 vdp->xs_hp_connected = B_TRUE; 1555 xdb_start_connect(vdp); 1556 mutex_exit(&vdp->xs_cbmutex); 1557 } 1558 1559 /*ARGSUSED*/ 1560 static void 1561 xdb_oe_state_change(dev_info_t *dip, ddi_eventcookie_t id, void *arg, 1562 void *impl_data) 1563 { 1564 XenbusState new_state = *(XenbusState *)impl_data; 1565 xdb_t *vdp = (xdb_t *)ddi_get_driver_private(dip); 1566 1567 XDB_DBPRINT(XDB_DBG_INFO, (CE_NOTE, "xdb@%s: " 1568 "otherend state change to %d!", ddi_get_name_addr(dip), new_state)); 1569 1570 mutex_enter(&vdp->xs_cbmutex); 1571 1572 /* 1573 * Now it'd really be nice if there was a well defined state 1574 * transition model for xen frontend drivers, but unfortunatly 1575 * there isn't. So we're stuck with assuming that all state 1576 * transitions are possible, and we'll just have to deal with 1577 * them regardless of what state we're in. 1578 */ 1579 switch (new_state) { 1580 case XenbusStateUnknown: 1581 case XenbusStateInitialising: 1582 case XenbusStateInitWait: 1583 /* tear down our connection to the frontend */ 1584 xdb_close(dip); 1585 vdp->xs_fe_initialised = B_FALSE; 1586 break; 1587 1588 case XenbusStateInitialised: 1589 /* 1590 * If we were conected, then we need to drop the connection 1591 * and re-negotiate it. 1592 */ 1593 xdb_close(dip); 1594 vdp->xs_fe_initialised = B_TRUE; 1595 xdb_start_connect(vdp); 1596 break; 1597 1598 case XenbusStateConnected: 1599 /* nothing todo here other than congratulate the frontend */ 1600 break; 1601 1602 case XenbusStateClosing: 1603 /* monkey see monkey do */ 1604 (void) xvdi_switch_state(dip, XBT_NULL, XenbusStateClosing); 1605 break; 1606 1607 case XenbusStateClosed: 1608 /* tear down our connection to the frontend */ 1609 xdb_close(dip); 1610 vdp->xs_fe_initialised = B_FALSE; 1611 (void) xvdi_switch_state(dip, XBT_NULL, new_state); 1612 break; 1613 } 1614 1615 mutex_exit(&vdp->xs_cbmutex); 1616 } 1617 1618 static int 1619 xdb_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1620 { 1621 ddi_iblock_cookie_t ibc; 1622 xdb_t *vdp; 1623 int instance = ddi_get_instance(dip); 1624 char *xsname, *oename; 1625 char *str; 1626 1627 switch (cmd) { 1628 case DDI_RESUME: 1629 return (DDI_FAILURE); 1630 case DDI_ATTACH: 1631 break; 1632 default: 1633 return (DDI_FAILURE); 1634 } 1635 /* DDI_ATTACH */ 1636 1637 if (((xsname = xvdi_get_xsname(dip)) == NULL) || 1638 ((oename = xvdi_get_oename(dip)) == NULL)) 1639 return (DDI_FAILURE); 1640 1641 /* 1642 * Disable auto-detach. This is necessary so that we don't get 1643 * detached while we're disconnected from the front end. 1644 */ 1645 (void) ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1); 1646 1647 if (ddi_get_iblock_cookie(dip, 0, &ibc) != DDI_SUCCESS) 1648 return (DDI_FAILURE); 1649 1650 if (ddi_soft_state_zalloc(xdb_statep, instance) != DDI_SUCCESS) 1651 return (DDI_FAILURE); 1652 1653 vdp = ddi_get_soft_state(xdb_statep, instance); 1654 vdp->xs_dip = dip; 1655 mutex_init(&vdp->xs_iomutex, NULL, MUTEX_DRIVER, (void *)ibc); 1656 mutex_init(&vdp->xs_cbmutex, NULL, MUTEX_DRIVER, (void *)ibc); 1657 cv_init(&vdp->xs_iocv, NULL, CV_DRIVER, NULL); 1658 cv_init(&vdp->xs_ionumcv, NULL, CV_DRIVER, NULL); 1659 ddi_set_driver_private(dip, vdp); 1660 1661 if (!xdb_kstat_init(vdp)) 1662 goto errout1; 1663 1664 /* Check if the frontend device is supposed to be a cdrom */ 1665 if (xenbus_read_str(oename, XBP_DEV_TYPE, &str) != 0) 1666 return (DDI_FAILURE); 1667 if (strcmp(str, XBV_DEV_TYPE_CD) == 0) 1668 vdp->xs_type |= XDB_DEV_FE_CD; 1669 strfree(str); 1670 1671 /* Check if the frontend device is supposed to be read only */ 1672 if (xenbus_read_str(xsname, "mode", &str) != 0) 1673 return (DDI_FAILURE); 1674 if ((strcmp(str, "r") == NULL) || (strcmp(str, "ro") == NULL)) 1675 vdp->xs_type |= XDB_DEV_RO; 1676 strfree(str); 1677 1678 mutex_enter(&vdp->xs_cbmutex); 1679 if (!xdb_media_req_init(vdp) || !xdb_params_init(vdp)) { 1680 xvdi_remove_xb_watch_handlers(dip); 1681 mutex_exit(&vdp->xs_cbmutex); 1682 goto errout2; 1683 } 1684 mutex_exit(&vdp->xs_cbmutex); 1685 1686 vdp->xs_send_buf = B_TRUE; 1687 vdp->xs_iotaskq = ddi_taskq_create(dip, "xdb_iotask", 1, 1688 TASKQ_DEFAULTPRI, 0); 1689 (void) ddi_taskq_dispatch(vdp->xs_iotaskq, xdb_send_buf, vdp, 1690 DDI_SLEEP); 1691 1692 /* Watch frontend and hotplug state change */ 1693 if ((xvdi_add_event_handler(dip, XS_OE_STATE, xdb_oe_state_change, 1694 NULL) != DDI_SUCCESS) || 1695 (xvdi_add_event_handler(dip, XS_HP_STATE, xdb_hp_state_change, 1696 NULL) != DDI_SUCCESS)) 1697 goto errout3; 1698 1699 /* 1700 * Kick-off hotplug script 1701 */ 1702 if (xvdi_post_event(dip, XEN_HP_ADD) != DDI_SUCCESS) { 1703 cmn_err(CE_WARN, "xdb@%s: failed to start hotplug script", 1704 ddi_get_name_addr(dip)); 1705 goto errout3; 1706 } 1707 1708 /* 1709 * start waiting for hotplug event and otherend state event 1710 * mainly for debugging, frontend will not take any op seeing this 1711 */ 1712 (void) xvdi_switch_state(dip, XBT_NULL, XenbusStateInitWait); 1713 1714 XDB_DBPRINT(XDB_DBG_INFO, (CE_NOTE, "xdb@%s: attached!", 1715 ddi_get_name_addr(dip))); 1716 return (DDI_SUCCESS); 1717 1718 errout3: 1719 ASSERT(vdp->xs_hp_connected && vdp->xs_if_connected); 1720 1721 xvdi_remove_event_handler(dip, NULL); 1722 1723 /* Disconnect from the backend */ 1724 mutex_enter(&vdp->xs_cbmutex); 1725 mutex_enter(&vdp->xs_iomutex); 1726 vdp->xs_send_buf = B_FALSE; 1727 cv_broadcast(&vdp->xs_iocv); 1728 mutex_exit(&vdp->xs_iomutex); 1729 mutex_exit(&vdp->xs_cbmutex); 1730 1731 /* wait for all io to dtrain and destroy io taskq */ 1732 ddi_taskq_destroy(vdp->xs_iotaskq); 1733 1734 /* tear down block-configure watch */ 1735 mutex_enter(&vdp->xs_cbmutex); 1736 xvdi_remove_xb_watch_handlers(dip); 1737 mutex_exit(&vdp->xs_cbmutex); 1738 1739 errout2: 1740 /* remove kstats */ 1741 kstat_delete(vdp->xs_kstats); 1742 1743 errout1: 1744 /* free up driver state */ 1745 ddi_set_driver_private(dip, NULL); 1746 cv_destroy(&vdp->xs_iocv); 1747 cv_destroy(&vdp->xs_ionumcv); 1748 mutex_destroy(&vdp->xs_cbmutex); 1749 mutex_destroy(&vdp->xs_iomutex); 1750 ddi_soft_state_free(xdb_statep, instance); 1751 1752 return (DDI_FAILURE); 1753 } 1754 1755 /*ARGSUSED*/ 1756 static int 1757 xdb_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 1758 { 1759 int instance = ddi_get_instance(dip); 1760 xdb_t *vdp = XDB_INST2SOFTS(instance); 1761 1762 switch (cmd) { 1763 case DDI_SUSPEND: 1764 return (DDI_FAILURE); 1765 case DDI_DETACH: 1766 break; 1767 default: 1768 return (DDI_FAILURE); 1769 } 1770 1771 /* DDI_DETACH handling */ 1772 1773 /* refuse to detach if we're still in use by the frontend */ 1774 mutex_enter(&vdp->xs_iomutex); 1775 if (vdp->xs_if_connected) { 1776 mutex_exit(&vdp->xs_iomutex); 1777 return (DDI_FAILURE); 1778 } 1779 vdp->xs_send_buf = B_FALSE; 1780 cv_broadcast(&vdp->xs_iocv); 1781 mutex_exit(&vdp->xs_iomutex); 1782 1783 xvdi_remove_event_handler(dip, NULL); 1784 (void) xvdi_post_event(dip, XEN_HP_REMOVE); 1785 1786 ddi_taskq_destroy(vdp->xs_iotaskq); 1787 1788 mutex_enter(&vdp->xs_cbmutex); 1789 xvdi_remove_xb_watch_handlers(dip); 1790 mutex_exit(&vdp->xs_cbmutex); 1791 1792 cv_destroy(&vdp->xs_iocv); 1793 cv_destroy(&vdp->xs_ionumcv); 1794 mutex_destroy(&vdp->xs_cbmutex); 1795 mutex_destroy(&vdp->xs_iomutex); 1796 kstat_delete(vdp->xs_kstats); 1797 ddi_set_driver_private(dip, NULL); 1798 ddi_soft_state_free(xdb_statep, instance); 1799 1800 XDB_DBPRINT(XDB_DBG_INFO, (CE_NOTE, "xdb@%s: detached!", 1801 ddi_get_name_addr(dip))); 1802 return (DDI_SUCCESS); 1803 } 1804 1805 static struct dev_ops xdb_dev_ops = { 1806 DEVO_REV, /* devo_rev */ 1807 0, /* devo_refcnt */ 1808 ddi_getinfo_1to1, /* devo_getinfo */ 1809 nulldev, /* devo_identify */ 1810 nulldev, /* devo_probe */ 1811 xdb_attach, /* devo_attach */ 1812 xdb_detach, /* devo_detach */ 1813 nodev, /* devo_reset */ 1814 NULL, /* devo_cb_ops */ 1815 NULL, /* devo_bus_ops */ 1816 NULL, /* power */ 1817 ddi_quiesce_not_needed, /* quiesce */ 1818 }; 1819 1820 /* 1821 * Module linkage information for the kernel. 1822 */ 1823 static struct modldrv modldrv = { 1824 &mod_driverops, /* Type of module. */ 1825 "vbd backend driver", /* Name of the module */ 1826 &xdb_dev_ops /* driver ops */ 1827 }; 1828 1829 static struct modlinkage xdb_modlinkage = { 1830 MODREV_1, 1831 &modldrv, 1832 NULL 1833 }; 1834 1835 int 1836 _init(void) 1837 { 1838 int rv; 1839 1840 if ((rv = ddi_soft_state_init((void **)&xdb_statep, 1841 sizeof (xdb_t), 0)) == 0) 1842 if ((rv = mod_install(&xdb_modlinkage)) != 0) 1843 ddi_soft_state_fini((void **)&xdb_statep); 1844 return (rv); 1845 } 1846 1847 int 1848 _fini(void) 1849 { 1850 int rv; 1851 1852 if ((rv = mod_remove(&xdb_modlinkage)) != 0) 1853 return (rv); 1854 ddi_soft_state_fini((void **)&xdb_statep); 1855 return (rv); 1856 } 1857 1858 int 1859 _info(struct modinfo *modinfop) 1860 { 1861 return (mod_info(&xdb_modlinkage, modinfop)); 1862 } 1863 1864 static int 1865 xdb_get_request(xdb_t *vdp, blkif_request_t *req) 1866 { 1867 void *src = xvdi_ring_get_request(vdp->xs_ring); 1868 1869 if (src == NULL) 1870 return (0); 1871 1872 switch (vdp->xs_blk_protocol) { 1873 case BLKIF_PROTOCOL_NATIVE: 1874 (void) memcpy(req, src, sizeof (*req)); 1875 break; 1876 case BLKIF_PROTOCOL_X86_32: 1877 blkif_get_x86_32_req(req, src); 1878 break; 1879 case BLKIF_PROTOCOL_X86_64: 1880 blkif_get_x86_64_req(req, src); 1881 break; 1882 default: 1883 cmn_err(CE_PANIC, "xdb@%s: unrecognised protocol: %d", 1884 ddi_get_name_addr(vdp->xs_dip), 1885 vdp->xs_blk_protocol); 1886 } 1887 return (1); 1888 } 1889 1890 static int 1891 xdb_push_response(xdb_t *vdp, uint64_t id, uint8_t op, uint16_t status) 1892 { 1893 ddi_acc_handle_t acchdl = vdp->xs_ring_hdl; 1894 blkif_response_t *rsp = xvdi_ring_get_response(vdp->xs_ring); 1895 blkif_x86_32_response_t *rsp_32 = (blkif_x86_32_response_t *)rsp; 1896 blkif_x86_64_response_t *rsp_64 = (blkif_x86_64_response_t *)rsp; 1897 1898 ASSERT(rsp); 1899 1900 switch (vdp->xs_blk_protocol) { 1901 case BLKIF_PROTOCOL_NATIVE: 1902 ddi_put64(acchdl, &rsp->id, id); 1903 ddi_put8(acchdl, &rsp->operation, op); 1904 ddi_put16(acchdl, (uint16_t *)&rsp->status, 1905 status == 0 ? BLKIF_RSP_OKAY : BLKIF_RSP_ERROR); 1906 break; 1907 case BLKIF_PROTOCOL_X86_32: 1908 ddi_put64(acchdl, &rsp_32->id, id); 1909 ddi_put8(acchdl, &rsp_32->operation, op); 1910 ddi_put16(acchdl, (uint16_t *)&rsp_32->status, 1911 status == 0 ? BLKIF_RSP_OKAY : BLKIF_RSP_ERROR); 1912 break; 1913 case BLKIF_PROTOCOL_X86_64: 1914 ddi_put64(acchdl, &rsp_64->id, id); 1915 ddi_put8(acchdl, &rsp_64->operation, op); 1916 ddi_put16(acchdl, (uint16_t *)&rsp_64->status, 1917 status == 0 ? BLKIF_RSP_OKAY : BLKIF_RSP_ERROR); 1918 break; 1919 default: 1920 cmn_err(CE_PANIC, "xdb@%s: unrecognised protocol: %d", 1921 ddi_get_name_addr(vdp->xs_dip), 1922 vdp->xs_blk_protocol); 1923 } 1924 1925 return (xvdi_ring_push_response(vdp->xs_ring)); 1926 } 1927 1928 static void 1929 blkif_get_x86_32_req(blkif_request_t *dst, blkif_x86_32_request_t *src) 1930 { 1931 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; 1932 dst->operation = src->operation; 1933 dst->nr_segments = src->nr_segments; 1934 dst->handle = src->handle; 1935 dst->id = src->id; 1936 dst->sector_number = src->sector_number; 1937 if (n > src->nr_segments) 1938 n = src->nr_segments; 1939 for (i = 0; i < n; i++) 1940 dst->seg[i] = src->seg[i]; 1941 } 1942 1943 static void 1944 blkif_get_x86_64_req(blkif_request_t *dst, blkif_x86_64_request_t *src) 1945 { 1946 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; 1947 dst->operation = src->operation; 1948 dst->nr_segments = src->nr_segments; 1949 dst->handle = src->handle; 1950 dst->id = src->id; 1951 dst->sector_number = src->sector_number; 1952 if (n > src->nr_segments) 1953 n = src->nr_segments; 1954 for (i = 0; i < n; i++) 1955 dst->seg[i] = src->seg[i]; 1956 } 1957