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 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/ksynch.h> 29 #include <sys/kmem.h> 30 #include <sys/file.h> 31 #include <sys/errno.h> 32 #include <sys/open.h> 33 #include <sys/buf.h> 34 #include <sys/uio.h> 35 #include <sys/aio_req.h> 36 #include <sys/cred.h> 37 #include <sys/modctl.h> 38 #include <sys/cmlb.h> 39 #include <sys/conf.h> 40 #include <sys/devops.h> 41 #include <sys/list.h> 42 #include <sys/sysmacros.h> 43 #include <sys/dkio.h> 44 #include <sys/vtoc.h> 45 #include <sys/scsi/scsi.h> /* for DTYPE_DIRECT */ 46 #include <sys/kstat.h> 47 #include <sys/fs/dv_node.h> 48 #include <sys/ddi.h> 49 #include <sys/sunddi.h> 50 #include <sys/note.h> 51 #include <sys/blkdev.h> 52 53 #define BD_MAXPART 64 54 #define BDINST(dev) (getminor(dev) / BD_MAXPART) 55 #define BDPART(dev) (getminor(dev) % BD_MAXPART) 56 57 typedef struct bd bd_t; 58 typedef struct bd_xfer_impl bd_xfer_impl_t; 59 60 struct bd { 61 void *d_private; 62 dev_info_t *d_dip; 63 kmutex_t d_ocmutex; 64 kmutex_t d_iomutex; 65 kmutex_t d_statemutex; 66 kcondvar_t d_statecv; 67 enum dkio_state d_state; 68 cmlb_handle_t d_cmlbh; 69 unsigned d_open_lyr[BD_MAXPART]; /* open count */ 70 uint64_t d_open_excl; /* bit mask indexed by partition */ 71 uint64_t d_open_reg[OTYPCNT]; /* bit mask */ 72 73 uint32_t d_qsize; 74 uint32_t d_qactive; 75 uint32_t d_maxxfer; 76 uint32_t d_blkshift; 77 uint64_t d_numblks; 78 ddi_devid_t d_devid; 79 80 kmem_cache_t *d_cache; 81 list_t d_runq; 82 list_t d_waitq; 83 kstat_t *d_ksp; 84 kstat_io_t *d_kiop; 85 86 boolean_t d_rdonly; 87 boolean_t d_removable; 88 boolean_t d_hotpluggable; 89 boolean_t d_use_dma; 90 91 ddi_dma_attr_t d_dma; 92 bd_ops_t d_ops; 93 bd_handle_t d_handle; 94 }; 95 96 struct bd_handle { 97 bd_ops_t h_ops; 98 ddi_dma_attr_t *h_dma; 99 dev_info_t *h_parent; 100 dev_info_t *h_child; 101 void *h_private; 102 bd_t *h_bd; 103 char *h_name; 104 char h_addr[20]; /* enough for %X,%X */ 105 }; 106 107 struct bd_xfer_impl { 108 bd_xfer_t i_public; 109 list_node_t i_linkage; 110 bd_t *i_bd; 111 buf_t *i_bp; 112 uint_t i_num_win; 113 uint_t i_cur_win; 114 off_t i_offset; 115 int (*i_func)(void *, bd_xfer_t *); 116 uint32_t i_blkshift; 117 size_t i_len; 118 size_t i_resid; 119 }; 120 121 #define i_dmah i_public.x_dmah 122 #define i_dmac i_public.x_dmac 123 #define i_ndmac i_public.x_ndmac 124 #define i_kaddr i_public.x_kaddr 125 #define i_nblks i_public.x_nblks 126 #define i_blkno i_public.x_blkno 127 #define i_flags i_public.x_flags 128 129 130 /* 131 * Private prototypes. 132 */ 133 134 static int bd_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 135 static int bd_attach(dev_info_t *, ddi_attach_cmd_t); 136 static int bd_detach(dev_info_t *, ddi_detach_cmd_t); 137 138 static int bd_open(dev_t *, int, int, cred_t *); 139 static int bd_close(dev_t, int, int, cred_t *); 140 static int bd_strategy(struct buf *); 141 static int bd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 142 static int bd_dump(dev_t, caddr_t, daddr_t, int); 143 static int bd_read(dev_t, struct uio *, cred_t *); 144 static int bd_write(dev_t, struct uio *, cred_t *); 145 static int bd_aread(dev_t, struct aio_req *, cred_t *); 146 static int bd_awrite(dev_t, struct aio_req *, cred_t *); 147 static int bd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *, 148 caddr_t, int *); 149 150 static int bd_tg_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t, size_t, 151 void *); 152 static int bd_tg_getinfo(dev_info_t *, int, void *, void *); 153 static int bd_xfer_ctor(void *, void *, int); 154 static void bd_xfer_dtor(void *, void *); 155 static void bd_sched(bd_t *); 156 static void bd_submit(bd_t *, bd_xfer_impl_t *); 157 static void bd_runq_exit(bd_xfer_impl_t *, int); 158 static void bd_update_state(bd_t *); 159 static int bd_check_state(bd_t *, enum dkio_state *); 160 static int bd_flush_write_cache(bd_t *, struct dk_callback *); 161 162 struct cmlb_tg_ops bd_tg_ops = { 163 TG_DK_OPS_VERSION_1, 164 bd_tg_rdwr, 165 bd_tg_getinfo, 166 }; 167 168 static struct cb_ops bd_cb_ops = { 169 bd_open, /* open */ 170 bd_close, /* close */ 171 bd_strategy, /* strategy */ 172 nodev, /* print */ 173 bd_dump, /* dump */ 174 bd_read, /* read */ 175 bd_write, /* write */ 176 bd_ioctl, /* ioctl */ 177 nodev, /* devmap */ 178 nodev, /* mmap */ 179 nodev, /* segmap */ 180 nochpoll, /* poll */ 181 bd_prop_op, /* cb_prop_op */ 182 0, /* streamtab */ 183 D_64BIT | D_MP, /* Driver comaptibility flag */ 184 CB_REV, /* cb_rev */ 185 bd_aread, /* async read */ 186 bd_awrite /* async write */ 187 }; 188 189 struct dev_ops bd_dev_ops = { 190 DEVO_REV, /* devo_rev, */ 191 0, /* refcnt */ 192 bd_getinfo, /* getinfo */ 193 nulldev, /* identify */ 194 nulldev, /* probe */ 195 bd_attach, /* attach */ 196 bd_detach, /* detach */ 197 nodev, /* reset */ 198 &bd_cb_ops, /* driver operations */ 199 NULL, /* bus operations */ 200 NULL, /* power */ 201 ddi_quiesce_not_needed, /* quiesce */ 202 }; 203 204 static struct modldrv modldrv = { 205 &mod_driverops, 206 "Generic Block Device", 207 &bd_dev_ops, 208 }; 209 210 static struct modlinkage modlinkage = { 211 MODREV_1, { &modldrv, NULL } 212 }; 213 214 static void *bd_state; 215 static krwlock_t bd_lock; 216 217 int 218 _init(void) 219 { 220 int rv; 221 222 rv = ddi_soft_state_init(&bd_state, sizeof (struct bd), 2); 223 if (rv != DDI_SUCCESS) { 224 return (rv); 225 } 226 rw_init(&bd_lock, NULL, RW_DRIVER, NULL); 227 rv = mod_install(&modlinkage); 228 if (rv != DDI_SUCCESS) { 229 rw_destroy(&bd_lock); 230 ddi_soft_state_fini(&bd_state); 231 } 232 return (rv); 233 } 234 235 int 236 _fini(void) 237 { 238 int rv; 239 240 rv = mod_remove(&modlinkage); 241 if (rv == DDI_SUCCESS) { 242 rw_destroy(&bd_lock); 243 ddi_soft_state_fini(&bd_state); 244 } 245 return (rv); 246 } 247 248 int 249 _info(struct modinfo *modinfop) 250 { 251 return (mod_info(&modlinkage, modinfop)); 252 } 253 254 static int 255 bd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 256 { 257 bd_t *bd; 258 minor_t inst; 259 260 _NOTE(ARGUNUSED(dip)); 261 262 inst = BDINST((dev_t)arg); 263 264 switch (cmd) { 265 case DDI_INFO_DEVT2DEVINFO: 266 bd = ddi_get_soft_state(bd_state, inst); 267 if (bd == NULL) { 268 return (DDI_FAILURE); 269 } 270 *resultp = (void *)bd->d_dip; 271 break; 272 273 case DDI_INFO_DEVT2INSTANCE: 274 *resultp = (void *)(intptr_t)inst; 275 break; 276 277 default: 278 return (DDI_FAILURE); 279 } 280 return (DDI_SUCCESS); 281 } 282 283 static int 284 bd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 285 { 286 int inst; 287 bd_handle_t hdl; 288 bd_t *bd; 289 bd_drive_t drive; 290 int rv; 291 char name[16]; 292 char kcache[32]; 293 294 switch (cmd) { 295 case DDI_ATTACH: 296 break; 297 case DDI_RESUME: 298 /* We don't do anything native for suspend/resume */ 299 return (DDI_SUCCESS); 300 default: 301 return (DDI_FAILURE); 302 } 303 304 inst = ddi_get_instance(dip); 305 hdl = ddi_get_parent_data(dip); 306 307 (void) snprintf(name, sizeof (name), "%s%d", 308 ddi_driver_name(dip), ddi_get_instance(dip)); 309 (void) snprintf(kcache, sizeof (kcache), "%s_xfer", name); 310 311 if (hdl == NULL) { 312 cmn_err(CE_WARN, "%s: missing parent data!", name); 313 return (DDI_FAILURE); 314 } 315 316 if (ddi_soft_state_zalloc(bd_state, inst) != DDI_SUCCESS) { 317 cmn_err(CE_WARN, "%s: unable to zalloc soft state!", name); 318 return (DDI_FAILURE); 319 } 320 bd = ddi_get_soft_state(bd_state, inst); 321 322 if (hdl->h_dma) { 323 bd->d_dma = *(hdl->h_dma); 324 bd->d_dma.dma_attr_granular = 325 max(DEV_BSIZE, bd->d_dma.dma_attr_granular); 326 bd->d_use_dma = B_TRUE; 327 328 if (bd->d_maxxfer && 329 (bd->d_maxxfer != bd->d_dma.dma_attr_maxxfer)) { 330 cmn_err(CE_WARN, 331 "%s: inconsistent maximum transfer size!", 332 name); 333 /* We force it */ 334 bd->d_maxxfer = bd->d_dma.dma_attr_maxxfer; 335 } else { 336 bd->d_maxxfer = bd->d_dma.dma_attr_maxxfer; 337 } 338 } else { 339 bd->d_use_dma = B_FALSE; 340 if (bd->d_maxxfer == 0) { 341 bd->d_maxxfer = 1024 * 1024; 342 } 343 } 344 bd->d_ops = hdl->h_ops; 345 bd->d_private = hdl->h_private; 346 bd->d_blkshift = 9; /* 512 bytes, to start */ 347 348 if (bd->d_maxxfer % DEV_BSIZE) { 349 cmn_err(CE_WARN, "%s: maximum transfer misaligned!", name); 350 bd->d_maxxfer &= ~(DEV_BSIZE - 1); 351 } 352 if (bd->d_maxxfer < DEV_BSIZE) { 353 cmn_err(CE_WARN, "%s: maximum transfer size too small!", name); 354 ddi_soft_state_free(bd_state, inst); 355 return (DDI_FAILURE); 356 } 357 358 bd->d_dip = dip; 359 bd->d_handle = hdl; 360 hdl->h_bd = bd; 361 ddi_set_driver_private(dip, bd); 362 363 mutex_init(&bd->d_iomutex, NULL, MUTEX_DRIVER, NULL); 364 mutex_init(&bd->d_ocmutex, NULL, MUTEX_DRIVER, NULL); 365 mutex_init(&bd->d_statemutex, NULL, MUTEX_DRIVER, NULL); 366 cv_init(&bd->d_statecv, NULL, CV_DRIVER, NULL); 367 368 list_create(&bd->d_waitq, sizeof (bd_xfer_impl_t), 369 offsetof(struct bd_xfer_impl, i_linkage)); 370 list_create(&bd->d_runq, sizeof (bd_xfer_impl_t), 371 offsetof(struct bd_xfer_impl, i_linkage)); 372 373 bd->d_cache = kmem_cache_create(kcache, sizeof (bd_xfer_impl_t), 8, 374 bd_xfer_ctor, bd_xfer_dtor, NULL, bd, NULL, 0); 375 376 bd->d_ksp = kstat_create(ddi_driver_name(dip), inst, NULL, "disk", 377 KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 378 if (bd->d_ksp != NULL) { 379 bd->d_ksp->ks_lock = &bd->d_iomutex; 380 kstat_install(bd->d_ksp); 381 bd->d_kiop = bd->d_ksp->ks_data; 382 } else { 383 /* 384 * Even if we cannot create the kstat, we create a 385 * scratch kstat. The reason for this is to ensure 386 * that we can update the kstat all of the time, 387 * without adding an extra branch instruction. 388 */ 389 bd->d_kiop = kmem_zalloc(sizeof (kstat_io_t), KM_SLEEP); 390 } 391 392 cmlb_alloc_handle(&bd->d_cmlbh); 393 394 bd->d_state = DKIO_NONE; 395 396 bzero(&drive, sizeof (drive)); 397 bd->d_ops.o_drive_info(bd->d_private, &drive); 398 bd->d_qsize = drive.d_qsize; 399 bd->d_maxxfer = drive.d_maxxfer; 400 bd->d_removable = drive.d_removable; 401 bd->d_hotpluggable = drive.d_hotpluggable; 402 403 rv = cmlb_attach(dip, &bd_tg_ops, DTYPE_DIRECT, 404 bd->d_removable, bd->d_hotpluggable, 405 drive.d_lun >= 0 ? DDI_NT_BLOCK_CHAN : DDI_NT_BLOCK, 406 CMLB_FAKE_LABEL_ONE_PARTITION, bd->d_cmlbh, 0); 407 if (rv != 0) { 408 cmlb_free_handle(&bd->d_cmlbh); 409 kmem_cache_destroy(bd->d_cache); 410 mutex_destroy(&bd->d_iomutex); 411 mutex_destroy(&bd->d_ocmutex); 412 mutex_destroy(&bd->d_statemutex); 413 cv_destroy(&bd->d_statecv); 414 list_destroy(&bd->d_waitq); 415 list_destroy(&bd->d_runq); 416 if (bd->d_ksp != NULL) { 417 kstat_delete(bd->d_ksp); 418 bd->d_ksp = NULL; 419 } else { 420 kmem_free(bd->d_kiop, sizeof (kstat_io_t)); 421 } 422 ddi_soft_state_free(bd_state, inst); 423 return (DDI_FAILURE); 424 } 425 426 if (bd->d_ops.o_devid_init != NULL) { 427 rv = bd->d_ops.o_devid_init(bd->d_private, dip, &bd->d_devid); 428 if (rv == DDI_SUCCESS) { 429 if (ddi_devid_register(dip, bd->d_devid) != 430 DDI_SUCCESS) { 431 cmn_err(CE_WARN, 432 "%s: unable to register devid", name); 433 } 434 } 435 } 436 437 /* 438 * Add a zero-length attribute to tell the world we support 439 * kernel ioctls (for layered drivers). Also set up properties 440 * used by HAL to identify removable media. 441 */ 442 (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 443 DDI_KERNEL_IOCTL, NULL, 0); 444 if (bd->d_removable) { 445 (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 446 "removable-media", NULL, 0); 447 } 448 if (bd->d_hotpluggable) { 449 (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 450 "hotpluggable", NULL, 0); 451 } 452 453 ddi_report_dev(dip); 454 455 return (DDI_SUCCESS); 456 } 457 458 static int 459 bd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 460 { 461 bd_t *bd; 462 463 bd = ddi_get_driver_private(dip); 464 465 switch (cmd) { 466 case DDI_DETACH: 467 break; 468 case DDI_SUSPEND: 469 /* We don't suspend, but our parent does */ 470 return (DDI_SUCCESS); 471 default: 472 return (DDI_FAILURE); 473 } 474 if (bd->d_ksp != NULL) { 475 kstat_delete(bd->d_ksp); 476 bd->d_ksp = NULL; 477 } else { 478 kmem_free(bd->d_kiop, sizeof (kstat_io_t)); 479 } 480 cmlb_detach(bd->d_cmlbh, 0); 481 cmlb_free_handle(&bd->d_cmlbh); 482 if (bd->d_devid) 483 ddi_devid_free(bd->d_devid); 484 kmem_cache_destroy(bd->d_cache); 485 mutex_destroy(&bd->d_iomutex); 486 mutex_destroy(&bd->d_ocmutex); 487 mutex_destroy(&bd->d_statemutex); 488 cv_destroy(&bd->d_statecv); 489 list_destroy(&bd->d_waitq); 490 list_destroy(&bd->d_runq); 491 ddi_soft_state_free(bd_state, ddi_get_instance(dip)); 492 return (DDI_SUCCESS); 493 } 494 495 static int 496 bd_xfer_ctor(void *buf, void *arg, int kmflag) 497 { 498 bd_xfer_impl_t *xi; 499 bd_t *bd = arg; 500 int (*dcb)(caddr_t); 501 502 if (kmflag == KM_SLEEP) { 503 dcb = DDI_DMA_SLEEP; 504 } else { 505 dcb = DDI_DMA_DONTWAIT; 506 } 507 508 xi = buf; 509 bzero(xi, sizeof (*xi)); 510 xi->i_bd = bd; 511 512 if (bd->d_use_dma) { 513 if (ddi_dma_alloc_handle(bd->d_dip, &bd->d_dma, dcb, NULL, 514 &xi->i_dmah) != DDI_SUCCESS) { 515 return (-1); 516 } 517 } 518 519 return (0); 520 } 521 522 static void 523 bd_xfer_dtor(void *buf, void *arg) 524 { 525 bd_xfer_impl_t *xi = buf; 526 527 _NOTE(ARGUNUSED(arg)); 528 529 if (xi->i_dmah) 530 ddi_dma_free_handle(&xi->i_dmah); 531 xi->i_dmah = NULL; 532 } 533 534 static bd_xfer_impl_t * 535 bd_xfer_alloc(bd_t *bd, struct buf *bp, int (*func)(void *, bd_xfer_t *), 536 int kmflag) 537 { 538 bd_xfer_impl_t *xi; 539 int rv; 540 int status; 541 unsigned dir; 542 int (*cb)(caddr_t); 543 size_t len; 544 uint32_t shift; 545 546 if (kmflag == KM_SLEEP) { 547 cb = DDI_DMA_SLEEP; 548 } else { 549 cb = DDI_DMA_DONTWAIT; 550 } 551 552 xi = kmem_cache_alloc(bd->d_cache, kmflag); 553 if (xi == NULL) { 554 bioerror(bp, ENOMEM); 555 return (NULL); 556 } 557 558 ASSERT(bp); 559 ASSERT(bp->b_bcount); 560 561 xi->i_bp = bp; 562 xi->i_func = func; 563 xi->i_blkno = bp->b_lblkno; 564 565 if (bp->b_bcount == 0) { 566 xi->i_len = 0; 567 xi->i_nblks = 0; 568 xi->i_kaddr = NULL; 569 xi->i_resid = 0; 570 xi->i_num_win = 0; 571 goto done; 572 } 573 574 if (bp->b_flags & B_READ) { 575 dir = DDI_DMA_READ; 576 xi->i_func = bd->d_ops.o_read; 577 } else { 578 dir = DDI_DMA_WRITE; 579 xi->i_func = bd->d_ops.o_write; 580 } 581 582 shift = bd->d_blkshift; 583 xi->i_blkshift = shift; 584 585 if (!bd->d_use_dma) { 586 bp_mapin(bp); 587 rv = 0; 588 xi->i_offset = 0; 589 xi->i_num_win = 590 (bp->b_bcount + (bd->d_maxxfer - 1)) / bd->d_maxxfer; 591 xi->i_cur_win = 0; 592 xi->i_len = min(bp->b_bcount, bd->d_maxxfer); 593 xi->i_nblks = xi->i_len >> shift; 594 xi->i_kaddr = bp->b_un.b_addr; 595 xi->i_resid = bp->b_bcount; 596 } else { 597 598 /* 599 * We have to use consistent DMA if the address is misaligned. 600 */ 601 if (((bp->b_flags & (B_PAGEIO | B_REMAPPED)) != B_PAGEIO) && 602 ((uintptr_t)bp->b_un.b_addr & 0x7)) { 603 dir |= DDI_DMA_CONSISTENT | DDI_DMA_PARTIAL; 604 } else { 605 dir |= DDI_DMA_STREAMING | DDI_DMA_PARTIAL; 606 } 607 608 status = ddi_dma_buf_bind_handle(xi->i_dmah, bp, dir, cb, 609 NULL, &xi->i_dmac, &xi->i_ndmac); 610 switch (status) { 611 case DDI_DMA_MAPPED: 612 xi->i_num_win = 1; 613 xi->i_cur_win = 0; 614 xi->i_offset = 0; 615 xi->i_len = bp->b_bcount; 616 xi->i_nblks = xi->i_len >> shift; 617 xi->i_resid = bp->b_bcount; 618 rv = 0; 619 break; 620 case DDI_DMA_PARTIAL_MAP: 621 xi->i_cur_win = 0; 622 623 if ((ddi_dma_numwin(xi->i_dmah, &xi->i_num_win) != 624 DDI_SUCCESS) || 625 (ddi_dma_getwin(xi->i_dmah, 0, &xi->i_offset, 626 &len, &xi->i_dmac, &xi->i_ndmac) != 627 DDI_SUCCESS) || 628 (P2PHASE(len, shift) != 0)) { 629 (void) ddi_dma_unbind_handle(xi->i_dmah); 630 rv = EFAULT; 631 goto done; 632 } 633 xi->i_len = len; 634 xi->i_nblks = xi->i_len >> shift; 635 xi->i_resid = bp->b_bcount; 636 rv = 0; 637 break; 638 case DDI_DMA_NORESOURCES: 639 rv = EAGAIN; 640 goto done; 641 case DDI_DMA_TOOBIG: 642 rv = EINVAL; 643 goto done; 644 case DDI_DMA_NOMAPPING: 645 case DDI_DMA_INUSE: 646 default: 647 rv = EFAULT; 648 goto done; 649 } 650 } 651 652 done: 653 if (rv != 0) { 654 kmem_cache_free(bd->d_cache, xi); 655 bioerror(bp, rv); 656 return (NULL); 657 } 658 659 return (xi); 660 } 661 662 static void 663 bd_xfer_free(bd_xfer_impl_t *xi) 664 { 665 if (xi->i_dmah) { 666 (void) ddi_dma_unbind_handle(xi->i_dmah); 667 } 668 kmem_cache_free(xi->i_bd->d_cache, xi); 669 } 670 671 static int 672 bd_open(dev_t *devp, int flag, int otyp, cred_t *credp) 673 { 674 dev_t dev = *devp; 675 bd_t *bd; 676 minor_t part; 677 minor_t inst; 678 uint64_t mask; 679 boolean_t ndelay; 680 int rv; 681 diskaddr_t nblks; 682 diskaddr_t lba; 683 684 _NOTE(ARGUNUSED(credp)); 685 686 part = BDPART(dev); 687 inst = BDINST(dev); 688 689 if (otyp >= OTYPCNT) 690 return (EINVAL); 691 692 ndelay = (flag & (FNDELAY | FNONBLOCK)) ? B_TRUE : B_FALSE; 693 694 /* 695 * Block any DR events from changing the set of registered 696 * devices while we function. 697 */ 698 rw_enter(&bd_lock, RW_READER); 699 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) { 700 rw_exit(&bd_lock); 701 return (ENXIO); 702 } 703 704 mutex_enter(&bd->d_ocmutex); 705 706 ASSERT(part < 64); 707 mask = (1U << part); 708 709 bd_update_state(bd); 710 711 if (cmlb_validate(bd->d_cmlbh, 0, 0) != 0) { 712 713 /* non-blocking opens are allowed to succeed */ 714 if (!ndelay) { 715 rv = ENXIO; 716 goto done; 717 } 718 } else if (cmlb_partinfo(bd->d_cmlbh, part, &nblks, &lba, 719 NULL, NULL, 0) == 0) { 720 721 /* 722 * We read the partinfo, verify valid ranges. If the 723 * partition is invalid, and we aren't blocking or 724 * doing a raw access, then fail. (Non-blocking and 725 * raw accesses can still succeed to allow a disk with 726 * bad partition data to opened by format and fdisk.) 727 */ 728 if ((!nblks) && ((!ndelay) || (otyp != OTYP_CHR))) { 729 rv = ENXIO; 730 goto done; 731 } 732 } else if (!ndelay) { 733 /* 734 * cmlb_partinfo failed -- invalid partition or no 735 * disk label. 736 */ 737 rv = ENXIO; 738 goto done; 739 } 740 741 if ((flag & FWRITE) && bd->d_rdonly) { 742 rv = EROFS; 743 goto done; 744 } 745 746 if ((bd->d_open_excl) & (mask)) { 747 rv = EBUSY; 748 goto done; 749 } 750 if (flag & FEXCL) { 751 if (bd->d_open_lyr[part]) { 752 rv = EBUSY; 753 goto done; 754 } 755 for (int i = 0; i < OTYP_LYR; i++) { 756 if (bd->d_open_reg[i] & mask) { 757 rv = EBUSY; 758 goto done; 759 } 760 } 761 } 762 763 if (otyp == OTYP_LYR) { 764 bd->d_open_lyr[part]++; 765 } else { 766 bd->d_open_reg[otyp] |= mask; 767 } 768 if (flag & FEXCL) { 769 bd->d_open_excl |= mask; 770 } 771 772 rv = 0; 773 done: 774 mutex_exit(&bd->d_ocmutex); 775 rw_exit(&bd_lock); 776 777 return (rv); 778 } 779 780 static int 781 bd_close(dev_t dev, int flag, int otyp, cred_t *credp) 782 { 783 bd_t *bd; 784 minor_t inst; 785 minor_t part; 786 uint64_t mask; 787 boolean_t last = B_TRUE; 788 789 _NOTE(ARGUNUSED(flag)); 790 _NOTE(ARGUNUSED(credp)); 791 792 part = BDPART(dev); 793 inst = BDINST(dev); 794 795 ASSERT(part < 64); 796 mask = (1U << part); 797 798 rw_enter(&bd_lock, RW_READER); 799 800 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) { 801 rw_exit(&bd_lock); 802 return (ENXIO); 803 } 804 805 mutex_enter(&bd->d_ocmutex); 806 if (bd->d_open_excl & mask) { 807 bd->d_open_excl &= ~mask; 808 } 809 if (otyp == OTYP_LYR) { 810 bd->d_open_lyr[part]--; 811 } else { 812 bd->d_open_reg[otyp] &= ~mask; 813 } 814 for (int i = 0; i < 64; i++) { 815 if (bd->d_open_lyr[part]) { 816 last = B_FALSE; 817 } 818 } 819 for (int i = 0; last && (i < OTYP_LYR); i++) { 820 if (bd->d_open_reg[i]) { 821 last = B_FALSE; 822 } 823 } 824 mutex_exit(&bd->d_ocmutex); 825 826 if (last) { 827 cmlb_invalidate(bd->d_cmlbh, 0); 828 } 829 rw_exit(&bd_lock); 830 831 return (0); 832 } 833 834 static int 835 bd_dump(dev_t dev, caddr_t caddr, daddr_t blkno, int nblk) 836 { 837 minor_t inst; 838 minor_t part; 839 diskaddr_t pstart; 840 diskaddr_t psize; 841 bd_t *bd; 842 bd_xfer_impl_t *xi; 843 buf_t *bp; 844 int rv; 845 846 rw_enter(&bd_lock, RW_READER); 847 848 part = BDPART(dev); 849 inst = BDINST(dev); 850 851 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) { 852 rw_exit(&bd_lock); 853 return (ENXIO); 854 } 855 /* 856 * do cmlb, but do it synchronously unless we already have the 857 * partition (which we probably should.) 858 */ 859 if (cmlb_partinfo(bd->d_cmlbh, part, &psize, &pstart, NULL, NULL, 860 (void *)1)) { 861 rw_exit(&bd_lock); 862 return (ENXIO); 863 } 864 865 if ((blkno + nblk) > psize) { 866 rw_exit(&bd_lock); 867 return (EINVAL); 868 } 869 bp = getrbuf(KM_NOSLEEP); 870 if (bp == NULL) { 871 rw_exit(&bd_lock); 872 return (ENOMEM); 873 } 874 875 bp->b_bcount = nblk << bd->d_blkshift; 876 bp->b_resid = bp->b_bcount; 877 bp->b_lblkno = blkno; 878 bp->b_un.b_addr = caddr; 879 880 xi = bd_xfer_alloc(bd, bp, bd->d_ops.o_write, KM_NOSLEEP); 881 if (xi == NULL) { 882 rw_exit(&bd_lock); 883 freerbuf(bp); 884 return (ENOMEM); 885 } 886 xi->i_blkno = blkno + pstart; 887 xi->i_flags = BD_XFER_POLL; 888 bd_submit(bd, xi); 889 rw_exit(&bd_lock); 890 891 /* 892 * Generally, we should have run this entirely synchronously 893 * at this point and the biowait call should be a no-op. If 894 * it didn't happen this way, it's a bug in the underlying 895 * driver not honoring BD_XFER_POLL. 896 */ 897 (void) biowait(bp); 898 rv = geterror(bp); 899 freerbuf(bp); 900 return (rv); 901 } 902 903 static int 904 bd_read(dev_t dev, struct uio *uio, cred_t *credp) 905 { 906 _NOTE(ARGUNUSED(credp)); 907 return (physio(bd_strategy, NULL, dev, B_READ, minphys, uio)); 908 } 909 910 static int 911 bd_write(dev_t dev, struct uio *uio, cred_t *credp) 912 { 913 _NOTE(ARGUNUSED(credp)); 914 return (physio(bd_strategy, NULL, dev, B_WRITE, minphys, uio)); 915 } 916 917 static int 918 bd_aread(dev_t dev, struct aio_req *aio, cred_t *credp) 919 { 920 _NOTE(ARGUNUSED(credp)); 921 return (aphysio(bd_strategy, anocancel, dev, B_READ, minphys, aio)); 922 } 923 924 static int 925 bd_awrite(dev_t dev, struct aio_req *aio, cred_t *credp) 926 { 927 _NOTE(ARGUNUSED(credp)); 928 return (aphysio(bd_strategy, anocancel, dev, B_WRITE, minphys, aio)); 929 } 930 931 static int 932 bd_strategy(struct buf *bp) 933 { 934 minor_t inst; 935 minor_t part; 936 bd_t *bd; 937 diskaddr_t p_lba; 938 diskaddr_t p_nblks; 939 diskaddr_t b_nblks; 940 bd_xfer_impl_t *xi; 941 uint32_t shift; 942 int (*func)(void *, bd_xfer_t *); 943 944 part = BDPART(bp->b_edev); 945 inst = BDINST(bp->b_edev); 946 947 ASSERT(bp); 948 949 bp->b_resid = bp->b_bcount; 950 951 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) { 952 bioerror(bp, ENXIO); 953 biodone(bp); 954 return (0); 955 } 956 957 if (cmlb_partinfo(bd->d_cmlbh, part, &p_nblks, &p_lba, 958 NULL, NULL, 0)) { 959 bioerror(bp, ENXIO); 960 biodone(bp); 961 return (0); 962 } 963 964 shift = bd->d_blkshift; 965 966 if ((P2PHASE(bp->b_bcount, (1U << shift)) != 0) || 967 (bp->b_lblkno > p_nblks)) { 968 bioerror(bp, ENXIO); 969 biodone(bp); 970 return (0); 971 } 972 b_nblks = bp->b_bcount >> shift; 973 if ((bp->b_lblkno == p_nblks) || (bp->b_bcount == 0)) { 974 biodone(bp); 975 return (0); 976 } 977 978 if ((b_nblks + bp->b_lblkno) > p_nblks) { 979 bp->b_resid = ((bp->b_lblkno + b_nblks - p_nblks) << shift); 980 bp->b_bcount -= bp->b_resid; 981 } else { 982 bp->b_resid = 0; 983 } 984 func = (bp->b_flags & B_READ) ? bd->d_ops.o_read : bd->d_ops.o_write; 985 986 xi = bd_xfer_alloc(bd, bp, func, KM_NOSLEEP); 987 if (xi == NULL) { 988 xi = bd_xfer_alloc(bd, bp, func, KM_PUSHPAGE); 989 } 990 if (xi == NULL) { 991 /* bd_request_alloc will have done bioerror */ 992 biodone(bp); 993 return (0); 994 } 995 xi->i_blkno = bp->b_lblkno + p_lba; 996 997 bd_submit(bd, xi); 998 999 return (0); 1000 } 1001 1002 static int 1003 bd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, int *rvalp) 1004 { 1005 minor_t inst; 1006 uint16_t part; 1007 bd_t *bd; 1008 void *ptr = (void *)arg; 1009 int rv; 1010 1011 part = BDPART(dev); 1012 inst = BDINST(dev); 1013 1014 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) { 1015 return (ENXIO); 1016 } 1017 1018 rv = cmlb_ioctl(bd->d_cmlbh, dev, cmd, arg, flag, credp, rvalp, 0); 1019 if (rv != ENOTTY) 1020 return (rv); 1021 1022 switch (cmd) { 1023 case DKIOCGMEDIAINFO: { 1024 struct dk_minfo minfo; 1025 1026 /* make sure our state information is current */ 1027 bd_update_state(bd); 1028 bzero(&minfo, sizeof (minfo)); 1029 minfo.dki_media_type = DK_FIXED_DISK; 1030 minfo.dki_lbsize = (1U << bd->d_blkshift); 1031 minfo.dki_capacity = bd->d_numblks; 1032 if (ddi_copyout(&minfo, ptr, sizeof (minfo), flag)) { 1033 return (EFAULT); 1034 } 1035 return (0); 1036 } 1037 case DKIOCINFO: { 1038 struct dk_cinfo cinfo; 1039 bzero(&cinfo, sizeof (cinfo)); 1040 cinfo.dki_ctype = DKC_BLKDEV; 1041 cinfo.dki_cnum = ddi_get_instance(ddi_get_parent(bd->d_dip)); 1042 (void) snprintf(cinfo.dki_cname, sizeof (cinfo.dki_cname), 1043 "%s", ddi_driver_name(ddi_get_parent(bd->d_dip))); 1044 (void) snprintf(cinfo.dki_dname, sizeof (cinfo.dki_dname), 1045 "%s", ddi_driver_name(bd->d_dip)); 1046 cinfo.dki_unit = inst; 1047 cinfo.dki_flags = DKI_FMTVOL; 1048 cinfo.dki_partition = part; 1049 cinfo.dki_maxtransfer = bd->d_maxxfer / DEV_BSIZE; 1050 cinfo.dki_addr = 0; 1051 cinfo.dki_slave = 0; 1052 cinfo.dki_space = 0; 1053 cinfo.dki_prio = 0; 1054 cinfo.dki_vec = 0; 1055 if (ddi_copyout(&cinfo, ptr, sizeof (cinfo), flag)) { 1056 return (EFAULT); 1057 } 1058 return (0); 1059 } 1060 case DKIOCREMOVABLE: { 1061 int i; 1062 i = bd->d_removable ? 1 : 0; 1063 if (ddi_copyout(&i, ptr, sizeof (i), flag)) { 1064 return (EFAULT); 1065 } 1066 return (0); 1067 } 1068 case DKIOCHOTPLUGGABLE: { 1069 int i; 1070 i = bd->d_hotpluggable ? 1 : 0; 1071 if (ddi_copyout(&i, ptr, sizeof (i), flag)) { 1072 return (EFAULT); 1073 } 1074 return (0); 1075 } 1076 case DKIOCREADONLY: { 1077 int i; 1078 i = bd->d_rdonly ? 1 : 0; 1079 if (ddi_copyout(&i, ptr, sizeof (i), flag)) { 1080 return (EFAULT); 1081 } 1082 return (0); 1083 } 1084 case DKIOCSTATE: { 1085 enum dkio_state state; 1086 if (ddi_copyin(ptr, &state, sizeof (state), flag)) { 1087 return (EFAULT); 1088 } 1089 if ((rv = bd_check_state(bd, &state)) != 0) { 1090 return (rv); 1091 } 1092 if (ddi_copyout(&state, ptr, sizeof (state), flag)) { 1093 return (EFAULT); 1094 } 1095 return (0); 1096 } 1097 case DKIOCFLUSHWRITECACHE: { 1098 struct dk_callback *dkc; 1099 1100 dkc = flag & FKIOCTL ? (void *)arg : NULL; 1101 rv = bd_flush_write_cache(bd, dkc); 1102 return (rv); 1103 } 1104 1105 default: 1106 break; 1107 1108 } 1109 return (ENOTTY); 1110 } 1111 1112 static int 1113 bd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 1114 char *name, caddr_t valuep, int *lengthp) 1115 { 1116 bd_t *bd; 1117 1118 bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip)); 1119 if (bd == NULL) 1120 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 1121 name, valuep, lengthp)); 1122 1123 return (cmlb_prop_op(bd->d_cmlbh, dev, dip, prop_op, mod_flags, name, 1124 valuep, lengthp, BDPART(dev), 0)); 1125 } 1126 1127 1128 static int 1129 bd_tg_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, diskaddr_t start, 1130 size_t length, void *tg_cookie) 1131 { 1132 bd_t *bd; 1133 buf_t *bp; 1134 bd_xfer_impl_t *xi; 1135 int rv; 1136 int (*func)(void *, bd_xfer_t *); 1137 int kmflag; 1138 1139 /* 1140 * If we are running in polled mode (such as during dump(9e) 1141 * execution), then we cannot sleep for kernel allocations. 1142 */ 1143 kmflag = tg_cookie ? KM_NOSLEEP : KM_SLEEP; 1144 1145 bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip)); 1146 1147 if (P2PHASE(length, (1U << bd->d_blkshift)) != 0) { 1148 /* We can only transfer whole blocks at a time! */ 1149 return (EINVAL); 1150 } 1151 1152 if ((bp = getrbuf(kmflag)) == NULL) { 1153 return (ENOMEM); 1154 } 1155 1156 switch (cmd) { 1157 case TG_READ: 1158 bp->b_flags = B_READ; 1159 func = bd->d_ops.o_read; 1160 break; 1161 case TG_WRITE: 1162 bp->b_flags = B_WRITE; 1163 func = bd->d_ops.o_write; 1164 break; 1165 default: 1166 freerbuf(bp); 1167 return (EINVAL); 1168 } 1169 1170 bp->b_un.b_addr = bufaddr; 1171 bp->b_bcount = length; 1172 xi = bd_xfer_alloc(bd, bp, func, kmflag); 1173 if (xi == NULL) { 1174 rv = geterror(bp); 1175 freerbuf(bp); 1176 return (rv); 1177 } 1178 xi->i_flags = tg_cookie ? BD_XFER_POLL : 0; 1179 xi->i_blkno = start; 1180 bd_submit(bd, xi); 1181 (void) biowait(bp); 1182 rv = geterror(bp); 1183 freerbuf(bp); 1184 1185 return (rv); 1186 } 1187 1188 static int 1189 bd_tg_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie) 1190 { 1191 bd_t *bd; 1192 1193 _NOTE(ARGUNUSED(tg_cookie)); 1194 bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip)); 1195 1196 switch (cmd) { 1197 case TG_GETPHYGEOM: 1198 case TG_GETVIRTGEOM: 1199 /* 1200 * We don't have any "geometry" as such, let cmlb 1201 * fabricate something. 1202 */ 1203 return (ENOTTY); 1204 1205 case TG_GETCAPACITY: 1206 bd_update_state(bd); 1207 *(diskaddr_t *)arg = bd->d_numblks; 1208 return (0); 1209 1210 case TG_GETBLOCKSIZE: 1211 *(uint32_t *)arg = (1U << bd->d_blkshift); 1212 return (0); 1213 1214 case TG_GETATTR: 1215 /* 1216 * It turns out that cmlb really doesn't do much for 1217 * non-writable media, but lets make the information 1218 * available for it in case it does more in the 1219 * future. (The value is currently used for 1220 * triggering special behavior for CD-ROMs.) 1221 */ 1222 bd_update_state(bd); 1223 ((tg_attribute_t *)arg)->media_is_writable = 1224 bd->d_rdonly ? B_FALSE : B_TRUE; 1225 return (0); 1226 1227 default: 1228 return (EINVAL); 1229 } 1230 } 1231 1232 1233 static void 1234 bd_sched(bd_t *bd) 1235 { 1236 bd_xfer_impl_t *xi; 1237 struct buf *bp; 1238 int rv; 1239 1240 mutex_enter(&bd->d_iomutex); 1241 1242 while ((bd->d_qactive < bd->d_qsize) && 1243 ((xi = list_remove_head(&bd->d_waitq)) != NULL)) { 1244 bd->d_qactive++; 1245 kstat_waitq_to_runq(bd->d_kiop); 1246 list_insert_tail(&bd->d_runq, xi); 1247 1248 /* 1249 * Submit the job to the driver. We drop the I/O mutex 1250 * so that we can deal with the case where the driver 1251 * completion routine calls back into us synchronously. 1252 */ 1253 1254 mutex_exit(&bd->d_iomutex); 1255 1256 rv = xi->i_func(bd->d_private, &xi->i_public); 1257 if (rv != 0) { 1258 bp = xi->i_bp; 1259 bd_xfer_free(xi); 1260 bioerror(bp, rv); 1261 biodone(bp); 1262 1263 mutex_enter(&bd->d_iomutex); 1264 bd->d_qactive--; 1265 kstat_runq_exit(bd->d_kiop); 1266 list_remove(&bd->d_runq, xi); 1267 } else { 1268 mutex_enter(&bd->d_iomutex); 1269 } 1270 } 1271 1272 mutex_exit(&bd->d_iomutex); 1273 } 1274 1275 static void 1276 bd_submit(bd_t *bd, bd_xfer_impl_t *xi) 1277 { 1278 mutex_enter(&bd->d_iomutex); 1279 list_insert_tail(&bd->d_waitq, xi); 1280 kstat_waitq_enter(bd->d_kiop); 1281 mutex_exit(&bd->d_iomutex); 1282 1283 bd_sched(bd); 1284 } 1285 1286 static void 1287 bd_runq_exit(bd_xfer_impl_t *xi, int err) 1288 { 1289 bd_t *bd = xi->i_bd; 1290 buf_t *bp = xi->i_bp; 1291 1292 mutex_enter(&bd->d_iomutex); 1293 bd->d_qactive--; 1294 kstat_runq_exit(bd->d_kiop); 1295 list_remove(&bd->d_runq, xi); 1296 mutex_exit(&bd->d_iomutex); 1297 1298 if (err == 0) { 1299 if (bp->b_flags & B_READ) { 1300 bd->d_kiop->reads++; 1301 bd->d_kiop->nread += (bp->b_bcount - xi->i_resid); 1302 } else { 1303 bd->d_kiop->writes++; 1304 bd->d_kiop->nwritten += (bp->b_bcount - xi->i_resid); 1305 } 1306 } 1307 bd_sched(bd); 1308 } 1309 1310 static void 1311 bd_update_state(bd_t *bd) 1312 { 1313 enum dkio_state state; 1314 bd_media_t media; 1315 boolean_t docmlb = B_FALSE; 1316 1317 bzero(&media, sizeof (media)); 1318 1319 mutex_enter(&bd->d_statemutex); 1320 if (bd->d_ops.o_media_info(bd->d_private, &media) == 0) { 1321 if ((1U << bd->d_blkshift) != media.m_blksize) { 1322 if ((media.m_blksize < 512) || 1323 (!ISP2(media.m_blksize)) || 1324 (P2PHASE(bd->d_maxxfer, media.m_blksize))) { 1325 cmn_err(CE_WARN, 1326 "%s%d: Invalid media block size (%d)", 1327 ddi_driver_name(bd->d_dip), 1328 ddi_get_instance(bd->d_dip), 1329 media.m_blksize); 1330 /* 1331 * We can't use the media, treat it as 1332 * not present. 1333 */ 1334 state = DKIO_EJECTED; 1335 bd->d_numblks = 0; 1336 } else { 1337 bd->d_blkshift = ddi_ffs(media.m_blksize) - 1; 1338 bd->d_numblks = media.m_nblks; 1339 bd->d_rdonly = media.m_readonly; 1340 state = DKIO_INSERTED; 1341 } 1342 1343 /* Device size changed */ 1344 docmlb = B_TRUE; 1345 1346 } else { 1347 if (bd->d_numblks != media.m_nblks) { 1348 /* Device size changed */ 1349 docmlb = B_TRUE; 1350 } 1351 bd->d_numblks = media.m_nblks; 1352 bd->d_rdonly = media.m_readonly; 1353 state = DKIO_INSERTED; 1354 } 1355 1356 } else { 1357 bd->d_numblks = 0; 1358 state = DKIO_EJECTED; 1359 } 1360 if (state != bd->d_state) { 1361 bd->d_state = state; 1362 cv_broadcast(&bd->d_statecv); 1363 docmlb = B_TRUE; 1364 } 1365 mutex_exit(&bd->d_statemutex); 1366 1367 if (docmlb) { 1368 if (state == DKIO_INSERTED) { 1369 (void) cmlb_validate(bd->d_cmlbh, 0, 0); 1370 } else { 1371 cmlb_invalidate(bd->d_cmlbh, 0); 1372 } 1373 } 1374 } 1375 1376 static int 1377 bd_check_state(bd_t *bd, enum dkio_state *state) 1378 { 1379 clock_t when; 1380 1381 for (;;) { 1382 1383 bd_update_state(bd); 1384 1385 mutex_enter(&bd->d_statemutex); 1386 1387 if (bd->d_state != *state) { 1388 *state = bd->d_state; 1389 mutex_exit(&bd->d_statemutex); 1390 break; 1391 } 1392 1393 when = drv_usectohz(1000000); 1394 if (cv_reltimedwait_sig(&bd->d_statecv, &bd->d_statemutex, 1395 when, TR_CLOCK_TICK) == 0) { 1396 mutex_exit(&bd->d_statemutex); 1397 return (EINTR); 1398 } 1399 1400 mutex_exit(&bd->d_statemutex); 1401 } 1402 1403 return (0); 1404 } 1405 1406 static int 1407 bd_flush_write_cache_done(struct buf *bp) 1408 { 1409 struct dk_callback *dc = (void *)bp->b_private; 1410 1411 (*dc->dkc_callback)(dc->dkc_cookie, geterror(bp)); 1412 kmem_free(dc, sizeof (*dc)); 1413 freerbuf(bp); 1414 return (0); 1415 } 1416 1417 static int 1418 bd_flush_write_cache(bd_t *bd, struct dk_callback *dkc) 1419 { 1420 buf_t *bp; 1421 struct dk_callback *dc; 1422 bd_xfer_impl_t *xi; 1423 int rv; 1424 1425 if (bd->d_ops.o_sync_cache == NULL) { 1426 return (ENOTSUP); 1427 } 1428 if ((bp = getrbuf(KM_SLEEP)) == NULL) { 1429 return (ENOMEM); 1430 } 1431 bp->b_resid = 0; 1432 bp->b_bcount = 0; 1433 1434 xi = bd_xfer_alloc(bd, bp, bd->d_ops.o_sync_cache, KM_SLEEP); 1435 if (xi == NULL) { 1436 rv = geterror(bp); 1437 freerbuf(bp); 1438 return (rv); 1439 } 1440 1441 if (dkc != NULL) { 1442 /* Make a private copy of the callback structure */ 1443 dc = kmem_alloc(sizeof (*dc), KM_SLEEP); 1444 *dc = *dkc; 1445 bp->b_private = dc; 1446 bp->b_iodone = bd_flush_write_cache_done; 1447 } 1448 1449 bd_submit(bd, xi); 1450 if (dkc == NULL) { 1451 /* wait synchronously */ 1452 (void) biowait(bp); 1453 rv = geterror(bp); 1454 freerbuf(bp); 1455 } else { 1456 /* deferred via callback */ 1457 rv = 0; 1458 } 1459 return (rv); 1460 } 1461 1462 /* 1463 * Nexus support. 1464 */ 1465 int 1466 bd_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop, 1467 void *arg, void *result) 1468 { 1469 bd_handle_t hdl; 1470 1471 switch (ctlop) { 1472 case DDI_CTLOPS_REPORTDEV: 1473 cmn_err(CE_CONT, "?Block device: %s@%s, %s%d\n", 1474 ddi_node_name(rdip), ddi_get_name_addr(rdip), 1475 ddi_driver_name(rdip), ddi_get_instance(rdip)); 1476 return (DDI_SUCCESS); 1477 1478 case DDI_CTLOPS_INITCHILD: 1479 hdl = ddi_get_parent_data((dev_info_t *)arg); 1480 if (hdl == NULL) { 1481 return (DDI_NOT_WELL_FORMED); 1482 } 1483 ddi_set_name_addr((dev_info_t *)arg, hdl->h_addr); 1484 return (DDI_SUCCESS); 1485 1486 case DDI_CTLOPS_UNINITCHILD: 1487 ddi_set_name_addr((dev_info_t *)arg, NULL); 1488 ndi_prop_remove_all((dev_info_t *)arg); 1489 return (DDI_SUCCESS); 1490 1491 default: 1492 return (ddi_ctlops(dip, rdip, ctlop, arg, result)); 1493 } 1494 } 1495 1496 /* 1497 * Functions for device drivers. 1498 */ 1499 bd_handle_t 1500 bd_alloc_handle(void *private, bd_ops_t *ops, ddi_dma_attr_t *dma, int kmflag) 1501 { 1502 bd_handle_t hdl; 1503 1504 hdl = kmem_zalloc(sizeof (*hdl), kmflag); 1505 if (hdl != NULL) { 1506 hdl->h_ops = *ops; 1507 hdl->h_dma = dma; 1508 hdl->h_private = private; 1509 } 1510 1511 return (hdl); 1512 } 1513 1514 void 1515 bd_free_handle(bd_handle_t hdl) 1516 { 1517 kmem_free(hdl, sizeof (*hdl)); 1518 } 1519 1520 int 1521 bd_attach_handle(dev_info_t *dip, bd_handle_t hdl) 1522 { 1523 dev_info_t *child; 1524 bd_drive_t drive; 1525 1526 /* if drivers don't override this, make it assume none */ 1527 drive.d_lun = -1; 1528 hdl->h_ops.o_drive_info(hdl->h_private, &drive); 1529 1530 hdl->h_parent = dip; 1531 hdl->h_name = "blkdev"; 1532 1533 if (drive.d_lun >= 0) { 1534 (void) snprintf(hdl->h_addr, sizeof (hdl->h_addr), "%X,%X", 1535 drive.d_target, drive.d_lun); 1536 } else { 1537 (void) snprintf(hdl->h_addr, sizeof (hdl->h_addr), "%X", 1538 drive.d_target); 1539 } 1540 if (ndi_devi_alloc(dip, hdl->h_name, (pnode_t)DEVI_SID_NODEID, 1541 &child) != NDI_SUCCESS) { 1542 cmn_err(CE_WARN, "%s%d: unable to allocate node %s@%s", 1543 ddi_driver_name(dip), ddi_get_instance(dip), 1544 "blkdev", hdl->h_addr); 1545 return (DDI_FAILURE); 1546 } 1547 1548 ddi_set_parent_data(child, hdl); 1549 hdl->h_child = child; 1550 1551 if (ndi_devi_online(child, 0) == NDI_FAILURE) { 1552 cmn_err(CE_WARN, "%s%d: failed bringing node %s@%s online", 1553 ddi_driver_name(dip), ddi_get_instance(dip), 1554 hdl->h_name, hdl->h_addr); 1555 (void) ndi_devi_free(child); 1556 return (DDI_FAILURE); 1557 } 1558 1559 return (DDI_SUCCESS); 1560 } 1561 1562 int 1563 bd_detach_handle(bd_handle_t hdl) 1564 { 1565 int circ; 1566 int rv; 1567 char *devnm; 1568 1569 if (hdl->h_child == NULL) { 1570 return (DDI_SUCCESS); 1571 } 1572 ndi_devi_enter(hdl->h_parent, &circ); 1573 if (i_ddi_node_state(hdl->h_child) < DS_INITIALIZED) { 1574 rv = ddi_remove_child(hdl->h_child, 0); 1575 } else { 1576 devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP); 1577 (void) ddi_deviname(hdl->h_child, devnm); 1578 (void) devfs_clean(hdl->h_parent, devnm + 1, DV_CLEAN_FORCE); 1579 rv = ndi_devi_unconfig_one(hdl->h_parent, devnm + 1, NULL, 1580 NDI_DEVI_REMOVE | NDI_UNCONFIG); 1581 kmem_free(devnm, MAXNAMELEN + 1); 1582 } 1583 if (rv == 0) { 1584 hdl->h_child = NULL; 1585 } 1586 1587 ndi_devi_exit(hdl->h_parent, circ); 1588 return (rv = NDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE); 1589 } 1590 1591 void 1592 bd_xfer_done(bd_xfer_t *xfer, int err) 1593 { 1594 bd_xfer_impl_t *xi = (void *)xfer; 1595 buf_t *bp = xi->i_bp; 1596 int rv; 1597 bd_t *bd = xi->i_bd; 1598 size_t len; 1599 1600 if (err != 0) { 1601 bd_runq_exit(xi, err); 1602 1603 bp->b_resid += xi->i_resid; 1604 bd_xfer_free(xi); 1605 bioerror(bp, err); 1606 biodone(bp); 1607 return; 1608 } 1609 1610 xi->i_cur_win++; 1611 xi->i_resid -= xi->i_len; 1612 1613 if (xi->i_resid == 0) { 1614 /* Job completed succcessfully! */ 1615 bd_runq_exit(xi, 0); 1616 1617 bd_xfer_free(xi); 1618 biodone(bp); 1619 return; 1620 } 1621 1622 xi->i_blkno += xi->i_nblks; 1623 1624 if (bd->d_use_dma) { 1625 /* More transfer still pending... advance to next DMA window. */ 1626 rv = ddi_dma_getwin(xi->i_dmah, xi->i_cur_win, 1627 &xi->i_offset, &len, &xi->i_dmac, &xi->i_ndmac); 1628 } else { 1629 /* Advance memory window. */ 1630 xi->i_kaddr += xi->i_len; 1631 xi->i_offset += xi->i_len; 1632 len = min(bp->b_bcount - xi->i_offset, bd->d_maxxfer); 1633 } 1634 1635 1636 if ((rv != DDI_SUCCESS) || 1637 (P2PHASE(len, (1U << xi->i_blkshift) != 0))) { 1638 bd_runq_exit(xi, EFAULT); 1639 1640 bp->b_resid += xi->i_resid; 1641 bd_xfer_free(xi); 1642 bioerror(bp, EFAULT); 1643 biodone(bp); 1644 return; 1645 } 1646 xi->i_len = len; 1647 xi->i_nblks = len >> xi->i_blkshift; 1648 1649 /* Submit next window to hardware. */ 1650 rv = xi->i_func(bd->d_private, &xi->i_public); 1651 if (rv != 0) { 1652 bd_runq_exit(xi, rv); 1653 1654 bp->b_resid += xi->i_resid; 1655 bd_xfer_free(xi); 1656 bioerror(bp, rv); 1657 biodone(bp); 1658 } 1659 } 1660 1661 void 1662 bd_state_change(bd_handle_t hdl) 1663 { 1664 bd_t *bd; 1665 1666 if ((bd = hdl->h_bd) != NULL) { 1667 bd_update_state(bd); 1668 } 1669 } 1670 1671 void 1672 bd_mod_init(struct dev_ops *devops) 1673 { 1674 static struct bus_ops bd_bus_ops = { 1675 BUSO_REV, /* busops_rev */ 1676 nullbusmap, /* bus_map */ 1677 NULL, /* bus_get_intrspec (OBSOLETE) */ 1678 NULL, /* bus_add_intrspec (OBSOLETE) */ 1679 NULL, /* bus_remove_intrspec (OBSOLETE) */ 1680 i_ddi_map_fault, /* bus_map_fault */ 1681 NULL, /* bus_dma_map (OBSOLETE) */ 1682 ddi_dma_allochdl, /* bus_dma_allochdl */ 1683 ddi_dma_freehdl, /* bus_dma_freehdl */ 1684 ddi_dma_bindhdl, /* bus_dma_bindhdl */ 1685 ddi_dma_unbindhdl, /* bus_dma_unbindhdl */ 1686 ddi_dma_flush, /* bus_dma_flush */ 1687 ddi_dma_win, /* bus_dma_win */ 1688 ddi_dma_mctl, /* bus_dma_ctl */ 1689 bd_bus_ctl, /* bus_ctl */ 1690 ddi_bus_prop_op, /* bus_prop_op */ 1691 NULL, /* bus_get_eventcookie */ 1692 NULL, /* bus_add_eventcall */ 1693 NULL, /* bus_remove_eventcall */ 1694 NULL, /* bus_post_event */ 1695 NULL, /* bus_intr_ctl (OBSOLETE) */ 1696 NULL, /* bus_config */ 1697 NULL, /* bus_unconfig */ 1698 NULL, /* bus_fm_init */ 1699 NULL, /* bus_fm_fini */ 1700 NULL, /* bus_fm_access_enter */ 1701 NULL, /* bus_fm_access_exit */ 1702 NULL, /* bus_power */ 1703 NULL, /* bus_intr_op */ 1704 }; 1705 1706 devops->devo_bus_ops = &bd_bus_ops; 1707 1708 /* 1709 * NB: The device driver is free to supply its own 1710 * character entry device support. 1711 */ 1712 } 1713 1714 void 1715 bd_mod_fini(struct dev_ops *devops) 1716 { 1717 devops->devo_bus_ops = NULL; 1718 } 1719