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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * LDoms virtual disk client (vdc) device driver 31 * 32 * This driver runs on a guest logical domain and communicates with the virtual 33 * disk server (vds) driver running on the service domain which is exporting 34 * virtualized "disks" to the guest logical domain. 35 * 36 * The driver can be divided into four sections: 37 * 38 * 1) generic device driver housekeeping 39 * _init, _fini, attach, detach, ops structures, etc. 40 * 41 * 2) communication channel setup 42 * Setup the communications link over the LDC channel that vdc uses to 43 * talk to the vDisk server. Initialise the descriptor ring which 44 * allows the LDC clients to transfer data via memory mappings. 45 * 46 * 3) Support exported to upper layers (filesystems, etc) 47 * The upper layers call into vdc via strategy(9E) and DKIO(7I) 48 * ioctl calls. vdc will copy the data to be written to the descriptor 49 * ring or maps the buffer to store the data read by the vDisk 50 * server into the descriptor ring. It then sends a message to the 51 * vDisk server requesting it to complete the operation. 52 * 53 * 4) Handling responses from vDisk server. 54 * The vDisk server will ACK some or all of the messages vdc sends to it 55 * (this is configured during the handshake). Upon receipt of an ACK 56 * vdc will check the descriptor ring and signal to the upper layer 57 * code waiting on the IO. 58 */ 59 60 #include <sys/atomic.h> 61 #include <sys/conf.h> 62 #include <sys/disp.h> 63 #include <sys/ddi.h> 64 #include <sys/dkio.h> 65 #include <sys/efi_partition.h> 66 #include <sys/fcntl.h> 67 #include <sys/file.h> 68 #include <sys/mach_descrip.h> 69 #include <sys/modctl.h> 70 #include <sys/mdeg.h> 71 #include <sys/note.h> 72 #include <sys/open.h> 73 #include <sys/sdt.h> 74 #include <sys/stat.h> 75 #include <sys/sunddi.h> 76 #include <sys/types.h> 77 #include <sys/promif.h> 78 #include <sys/vtoc.h> 79 #include <sys/archsystm.h> 80 #include <sys/sysmacros.h> 81 82 #include <sys/cdio.h> 83 #include <sys/dktp/fdisk.h> 84 #include <sys/scsi/generic/sense.h> 85 #include <sys/scsi/impl/uscsi.h> /* Needed for defn of USCSICMD ioctl */ 86 87 #include <sys/ldoms.h> 88 #include <sys/ldc.h> 89 #include <sys/vio_common.h> 90 #include <sys/vio_mailbox.h> 91 #include <sys/vdsk_common.h> 92 #include <sys/vdsk_mailbox.h> 93 #include <sys/vdc.h> 94 95 /* 96 * function prototypes 97 */ 98 99 /* standard driver functions */ 100 static int vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred); 101 static int vdc_close(dev_t dev, int flag, int otyp, cred_t *cred); 102 static int vdc_strategy(struct buf *buf); 103 static int vdc_print(dev_t dev, char *str); 104 static int vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 105 static int vdc_read(dev_t dev, struct uio *uio, cred_t *cred); 106 static int vdc_write(dev_t dev, struct uio *uio, cred_t *cred); 107 static int vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 108 cred_t *credp, int *rvalp); 109 static int vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred); 110 static int vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred); 111 112 static int vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, 113 void *arg, void **resultp); 114 static int vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 115 static int vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 116 117 /* setup */ 118 static void vdc_min(struct buf *bufp); 119 static int vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen); 120 static int vdc_do_ldc_init(vdc_t *vdc); 121 static int vdc_start_ldc_connection(vdc_t *vdc); 122 static int vdc_create_device_nodes(vdc_t *vdc); 123 static int vdc_create_device_nodes_efi(vdc_t *vdc); 124 static int vdc_create_device_nodes_vtoc(vdc_t *vdc); 125 static int vdc_create_device_nodes_props(vdc_t *vdc); 126 static int vdc_get_ldc_id(dev_info_t *dip, uint64_t *ldc_id); 127 static int vdc_do_ldc_up(vdc_t *vdc); 128 static void vdc_terminate_ldc(vdc_t *vdc); 129 static int vdc_init_descriptor_ring(vdc_t *vdc); 130 static void vdc_destroy_descriptor_ring(vdc_t *vdc); 131 static int vdc_setup_devid(vdc_t *vdc); 132 static void vdc_store_efi(vdc_t *vdc, struct dk_gpt *efi); 133 134 /* handshake with vds */ 135 static int vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver); 136 static int vdc_ver_negotiation(vdc_t *vdcp); 137 static int vdc_init_attr_negotiation(vdc_t *vdc); 138 static int vdc_attr_negotiation(vdc_t *vdcp); 139 static int vdc_init_dring_negotiate(vdc_t *vdc); 140 static int vdc_dring_negotiation(vdc_t *vdcp); 141 static int vdc_send_rdx(vdc_t *vdcp); 142 static int vdc_rdx_exchange(vdc_t *vdcp); 143 static boolean_t vdc_is_supported_version(vio_ver_msg_t *ver_msg); 144 145 /* processing incoming messages from vDisk server */ 146 static void vdc_process_msg_thread(vdc_t *vdc); 147 static int vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp); 148 149 static uint_t vdc_handle_cb(uint64_t event, caddr_t arg); 150 static int vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg); 151 static int vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg); 152 static int vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg); 153 static int vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg); 154 static int vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg); 155 static int vdc_send_request(vdc_t *vdcp, int operation, 156 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 157 int cb_type, void *cb_arg, vio_desc_direction_t dir); 158 static int vdc_map_to_shared_dring(vdc_t *vdcp, int idx); 159 static int vdc_populate_descriptor(vdc_t *vdcp, int operation, 160 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 161 int cb_type, void *cb_arg, vio_desc_direction_t dir); 162 static int vdc_do_sync_op(vdc_t *vdcp, int operation, 163 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset, 164 int cb_type, void *cb_arg, vio_desc_direction_t dir); 165 166 static int vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp); 167 static int vdc_drain_response(vdc_t *vdcp); 168 static int vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx); 169 static int vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep); 170 static int vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg); 171 172 /* dkio */ 173 static int vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode); 174 static int vdc_create_fake_geometry(vdc_t *vdc); 175 static int vdc_setup_disk_layout(vdc_t *vdc); 176 static int vdc_null_copy_func(vdc_t *vdc, void *from, void *to, 177 int mode, int dir); 178 static int vdc_get_wce_convert(vdc_t *vdc, void *from, void *to, 179 int mode, int dir); 180 static int vdc_set_wce_convert(vdc_t *vdc, void *from, void *to, 181 int mode, int dir); 182 static int vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, 183 int mode, int dir); 184 static int vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, 185 int mode, int dir); 186 static int vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, 187 int mode, int dir); 188 static int vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, 189 int mode, int dir); 190 static int vdc_uscsicmd_convert(vdc_t *vdc, void *from, void *to, 191 int mode, int dir); 192 static int vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, 193 int mode, int dir); 194 static int vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, 195 int mode, int dir); 196 197 /* 198 * Module variables 199 */ 200 201 /* 202 * Tunable variables to control how long vdc waits before timing out on 203 * various operations 204 */ 205 static int vdc_retries = 10; 206 static int vdc_hshake_retries = 3; 207 208 /* calculated from 'vdc_usec_timeout' during attach */ 209 static uint64_t vdc_hz_timeout; /* units: Hz */ 210 static uint64_t vdc_usec_timeout = 30 * MICROSEC; /* 30s units: ns */ 211 212 static uint64_t vdc_hz_min_ldc_delay; 213 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC; 214 static uint64_t vdc_hz_max_ldc_delay; 215 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC; 216 217 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC; 218 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC; 219 220 /* values for dumping - need to run in a tighter loop */ 221 static uint64_t vdc_usec_timeout_dump = 100 * MILLISEC; /* 0.1s units: ns */ 222 static int vdc_dump_retries = 100; 223 224 /* Count of the number of vdc instances attached */ 225 static volatile uint32_t vdc_instance_count = 0; 226 227 /* Soft state pointer */ 228 static void *vdc_state; 229 230 /* 231 * Controlling the verbosity of the error/debug messages 232 * 233 * vdc_msglevel - controls level of messages 234 * vdc_matchinst - 64-bit variable where each bit corresponds 235 * to the vdc instance the vdc_msglevel applies. 236 */ 237 int vdc_msglevel = 0x0; 238 uint64_t vdc_matchinst = 0ull; 239 240 /* 241 * Supported vDisk protocol version pairs. 242 * 243 * The first array entry is the latest and preferred version. 244 */ 245 static const vio_ver_t vdc_version[] = {{1, 0}}; 246 247 static struct cb_ops vdc_cb_ops = { 248 vdc_open, /* cb_open */ 249 vdc_close, /* cb_close */ 250 vdc_strategy, /* cb_strategy */ 251 vdc_print, /* cb_print */ 252 vdc_dump, /* cb_dump */ 253 vdc_read, /* cb_read */ 254 vdc_write, /* cb_write */ 255 vdc_ioctl, /* cb_ioctl */ 256 nodev, /* cb_devmap */ 257 nodev, /* cb_mmap */ 258 nodev, /* cb_segmap */ 259 nochpoll, /* cb_chpoll */ 260 ddi_prop_op, /* cb_prop_op */ 261 NULL, /* cb_str */ 262 D_MP | D_64BIT, /* cb_flag */ 263 CB_REV, /* cb_rev */ 264 vdc_aread, /* cb_aread */ 265 vdc_awrite /* cb_awrite */ 266 }; 267 268 static struct dev_ops vdc_ops = { 269 DEVO_REV, /* devo_rev */ 270 0, /* devo_refcnt */ 271 vdc_getinfo, /* devo_getinfo */ 272 nulldev, /* devo_identify */ 273 nulldev, /* devo_probe */ 274 vdc_attach, /* devo_attach */ 275 vdc_detach, /* devo_detach */ 276 nodev, /* devo_reset */ 277 &vdc_cb_ops, /* devo_cb_ops */ 278 NULL, /* devo_bus_ops */ 279 nulldev /* devo_power */ 280 }; 281 282 static struct modldrv modldrv = { 283 &mod_driverops, 284 "virtual disk client %I%", 285 &vdc_ops, 286 }; 287 288 static struct modlinkage modlinkage = { 289 MODREV_1, 290 &modldrv, 291 NULL 292 }; 293 294 /* -------------------------------------------------------------------------- */ 295 296 /* 297 * Device Driver housekeeping and setup 298 */ 299 300 int 301 _init(void) 302 { 303 int status; 304 305 if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0) 306 return (status); 307 if ((status = mod_install(&modlinkage)) != 0) 308 ddi_soft_state_fini(&vdc_state); 309 vdc_efi_init(vd_process_ioctl); 310 return (status); 311 } 312 313 int 314 _info(struct modinfo *modinfop) 315 { 316 return (mod_info(&modlinkage, modinfop)); 317 } 318 319 int 320 _fini(void) 321 { 322 int status; 323 324 if ((status = mod_remove(&modlinkage)) != 0) 325 return (status); 326 vdc_efi_fini(); 327 ddi_soft_state_fini(&vdc_state); 328 return (0); 329 } 330 331 static int 332 vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 333 { 334 _NOTE(ARGUNUSED(dip)) 335 336 int instance = VDCUNIT((dev_t)arg); 337 vdc_t *vdc = NULL; 338 339 switch (cmd) { 340 case DDI_INFO_DEVT2DEVINFO: 341 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 342 *resultp = NULL; 343 return (DDI_FAILURE); 344 } 345 *resultp = vdc->dip; 346 return (DDI_SUCCESS); 347 case DDI_INFO_DEVT2INSTANCE: 348 *resultp = (void *)(uintptr_t)instance; 349 return (DDI_SUCCESS); 350 default: 351 *resultp = NULL; 352 return (DDI_FAILURE); 353 } 354 } 355 356 static int 357 vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 358 { 359 int instance; 360 int rv; 361 vdc_t *vdc = NULL; 362 363 switch (cmd) { 364 case DDI_DETACH: 365 /* the real work happens below */ 366 break; 367 case DDI_SUSPEND: 368 /* nothing to do for this non-device */ 369 return (DDI_SUCCESS); 370 default: 371 return (DDI_FAILURE); 372 } 373 374 ASSERT(cmd == DDI_DETACH); 375 instance = ddi_get_instance(dip); 376 DMSGX(1, "[%d] Entered\n", instance); 377 378 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 379 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 380 return (DDI_FAILURE); 381 } 382 383 if (vdc->open_count) { 384 DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance); 385 return (DDI_FAILURE); 386 } 387 388 DMSG(vdc, 0, "[%d] proceeding...\n", instance); 389 390 /* mark instance as detaching */ 391 vdc->lifecycle = VDC_LC_DETACHING; 392 393 /* 394 * try and disable callbacks to prevent another handshake 395 */ 396 rv = ldc_set_cb_mode(vdc->ldc_handle, LDC_CB_DISABLE); 397 DMSG(vdc, 0, "callback disabled (rv=%d)\n", rv); 398 399 if (vdc->initialized & VDC_THREAD) { 400 mutex_enter(&vdc->read_lock); 401 if ((vdc->read_state == VDC_READ_WAITING) || 402 (vdc->read_state == VDC_READ_RESET)) { 403 vdc->read_state = VDC_READ_RESET; 404 cv_signal(&vdc->read_cv); 405 } 406 407 mutex_exit(&vdc->read_lock); 408 409 /* wake up any thread waiting for connection to come online */ 410 mutex_enter(&vdc->lock); 411 if (vdc->state == VDC_STATE_INIT_WAITING) { 412 DMSG(vdc, 0, 413 "[%d] write reset - move to resetting state...\n", 414 instance); 415 vdc->state = VDC_STATE_RESETTING; 416 cv_signal(&vdc->initwait_cv); 417 } 418 mutex_exit(&vdc->lock); 419 420 /* now wait until state transitions to VDC_STATE_DETACH */ 421 thread_join(vdc->msg_proc_thr->t_did); 422 ASSERT(vdc->state == VDC_STATE_DETACH); 423 DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n", 424 vdc->instance); 425 } 426 427 mutex_enter(&vdc->lock); 428 429 if (vdc->initialized & VDC_DRING) 430 vdc_destroy_descriptor_ring(vdc); 431 432 if (vdc->initialized & VDC_LDC) 433 vdc_terminate_ldc(vdc); 434 435 mutex_exit(&vdc->lock); 436 437 if (vdc->initialized & VDC_MINOR) { 438 ddi_prop_remove_all(dip); 439 ddi_remove_minor_node(dip, NULL); 440 } 441 442 if (vdc->initialized & VDC_LOCKS) { 443 mutex_destroy(&vdc->lock); 444 mutex_destroy(&vdc->read_lock); 445 cv_destroy(&vdc->initwait_cv); 446 cv_destroy(&vdc->dring_free_cv); 447 cv_destroy(&vdc->membind_cv); 448 cv_destroy(&vdc->sync_pending_cv); 449 cv_destroy(&vdc->sync_blocked_cv); 450 cv_destroy(&vdc->read_cv); 451 cv_destroy(&vdc->running_cv); 452 } 453 454 if (vdc->minfo) 455 kmem_free(vdc->minfo, sizeof (struct dk_minfo)); 456 457 if (vdc->cinfo) 458 kmem_free(vdc->cinfo, sizeof (struct dk_cinfo)); 459 460 if (vdc->vtoc) 461 kmem_free(vdc->vtoc, sizeof (struct vtoc)); 462 463 if (vdc->label) 464 kmem_free(vdc->label, DK_LABEL_SIZE); 465 466 if (vdc->devid) { 467 ddi_devid_unregister(dip); 468 ddi_devid_free(vdc->devid); 469 } 470 471 if (vdc->initialized & VDC_SOFT_STATE) 472 ddi_soft_state_free(vdc_state, instance); 473 474 DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc); 475 476 return (DDI_SUCCESS); 477 } 478 479 480 static int 481 vdc_do_attach(dev_info_t *dip) 482 { 483 int instance; 484 vdc_t *vdc = NULL; 485 int status; 486 487 ASSERT(dip != NULL); 488 489 instance = ddi_get_instance(dip); 490 if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) { 491 cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure", 492 instance); 493 return (DDI_FAILURE); 494 } 495 496 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 497 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 498 return (DDI_FAILURE); 499 } 500 501 /* 502 * We assign the value to initialized in this case to zero out the 503 * variable and then set bits in it to indicate what has been done 504 */ 505 vdc->initialized = VDC_SOFT_STATE; 506 507 vdc_hz_timeout = drv_usectohz(vdc_usec_timeout); 508 509 vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc); 510 vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc); 511 512 vdc->dip = dip; 513 vdc->instance = instance; 514 vdc->open_count = 0; 515 vdc->vdisk_type = VD_DISK_TYPE_UNK; 516 vdc->vdisk_label = VD_DISK_LABEL_UNK; 517 vdc->state = VDC_STATE_INIT; 518 vdc->lifecycle = VDC_LC_ATTACHING; 519 vdc->ldc_state = 0; 520 vdc->session_id = 0; 521 vdc->block_size = DEV_BSIZE; 522 vdc->max_xfer_sz = maxphys / DEV_BSIZE; 523 524 vdc->vtoc = NULL; 525 vdc->cinfo = NULL; 526 vdc->minfo = NULL; 527 528 mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL); 529 cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL); 530 cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL); 531 cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL); 532 cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL); 533 534 vdc->threads_pending = 0; 535 vdc->sync_op_pending = B_FALSE; 536 vdc->sync_op_blocked = B_FALSE; 537 cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL); 538 cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL); 539 540 /* init blocking msg read functionality */ 541 mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL); 542 cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL); 543 vdc->read_state = VDC_READ_IDLE; 544 545 vdc->initialized |= VDC_LOCKS; 546 547 /* initialise LDC channel which will be used to communicate with vds */ 548 if ((status = vdc_do_ldc_init(vdc)) != 0) { 549 cmn_err(CE_NOTE, "[%d] Couldn't initialize LDC", instance); 550 goto return_status; 551 } 552 553 /* initialize the thread responsible for managing state with server */ 554 vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread, 555 vdc, 0, &p0, TS_RUN, minclsyspri); 556 if (vdc->msg_proc_thr == NULL) { 557 cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread", 558 instance); 559 return (DDI_FAILURE); 560 } 561 562 vdc->initialized |= VDC_THREAD; 563 564 atomic_inc_32(&vdc_instance_count); 565 566 /* 567 * Once the handshake is complete, we can use the DRing to send 568 * requests to the vDisk server to calculate the geometry and 569 * VTOC of the "disk" 570 */ 571 status = vdc_setup_disk_layout(vdc); 572 if (status != 0) { 573 DMSG(vdc, 0, "[%d] Failed to discover disk layout (err%d)", 574 vdc->instance, status); 575 goto return_status; 576 } 577 578 /* 579 * Now that we have the device info we can create the 580 * device nodes and properties 581 */ 582 status = vdc_create_device_nodes(vdc); 583 if (status) { 584 DMSG(vdc, 0, "[%d] Failed to create device nodes", 585 instance); 586 goto return_status; 587 } 588 status = vdc_create_device_nodes_props(vdc); 589 if (status) { 590 DMSG(vdc, 0, "[%d] Failed to create device nodes" 591 " properties (%d)", instance, status); 592 goto return_status; 593 } 594 595 /* 596 * Setup devid 597 */ 598 if (vdc_setup_devid(vdc)) { 599 DMSG(vdc, 0, "[%d] No device id available\n", instance); 600 } 601 602 ddi_report_dev(dip); 603 vdc->lifecycle = VDC_LC_ONLINE; 604 DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance); 605 606 return_status: 607 DMSG(vdc, 0, "[%d] Attach completed\n", instance); 608 return (status); 609 } 610 611 static int 612 vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 613 { 614 int status; 615 616 switch (cmd) { 617 case DDI_ATTACH: 618 if ((status = vdc_do_attach(dip)) != 0) 619 (void) vdc_detach(dip, DDI_DETACH); 620 return (status); 621 case DDI_RESUME: 622 /* nothing to do for this non-device */ 623 return (DDI_SUCCESS); 624 default: 625 return (DDI_FAILURE); 626 } 627 } 628 629 static int 630 vdc_do_ldc_init(vdc_t *vdc) 631 { 632 int status = 0; 633 ldc_status_t ldc_state; 634 ldc_attr_t ldc_attr; 635 uint64_t ldc_id = 0; 636 dev_info_t *dip = NULL; 637 638 ASSERT(vdc != NULL); 639 640 dip = vdc->dip; 641 vdc->initialized |= VDC_LDC; 642 643 if ((status = vdc_get_ldc_id(dip, &ldc_id)) != 0) { 644 DMSG(vdc, 0, "[%d] Failed to get LDC channel ID property", 645 vdc->instance); 646 return (EIO); 647 } 648 vdc->ldc_id = ldc_id; 649 650 ldc_attr.devclass = LDC_DEV_BLK; 651 ldc_attr.instance = vdc->instance; 652 ldc_attr.mode = LDC_MODE_UNRELIABLE; /* unreliable transport */ 653 ldc_attr.mtu = VD_LDC_MTU; 654 655 if ((vdc->initialized & VDC_LDC_INIT) == 0) { 656 status = ldc_init(ldc_id, &ldc_attr, &vdc->ldc_handle); 657 if (status != 0) { 658 DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d", 659 vdc->instance, ldc_id, status); 660 return (status); 661 } 662 vdc->initialized |= VDC_LDC_INIT; 663 } 664 status = ldc_status(vdc->ldc_handle, &ldc_state); 665 if (status != 0) { 666 DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]", 667 vdc->instance, status); 668 return (status); 669 } 670 vdc->ldc_state = ldc_state; 671 672 if ((vdc->initialized & VDC_LDC_CB) == 0) { 673 status = ldc_reg_callback(vdc->ldc_handle, vdc_handle_cb, 674 (caddr_t)vdc); 675 if (status != 0) { 676 DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)", 677 vdc->instance, status); 678 return (status); 679 } 680 vdc->initialized |= VDC_LDC_CB; 681 } 682 683 vdc->initialized |= VDC_LDC; 684 685 /* 686 * At this stage we have initialised LDC, we will now try and open 687 * the connection. 688 */ 689 if (vdc->ldc_state == LDC_INIT) { 690 status = ldc_open(vdc->ldc_handle); 691 if (status != 0) { 692 DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d", 693 vdc->instance, vdc->ldc_id, status); 694 return (status); 695 } 696 vdc->initialized |= VDC_LDC_OPEN; 697 } 698 699 return (status); 700 } 701 702 static int 703 vdc_start_ldc_connection(vdc_t *vdc) 704 { 705 int status = 0; 706 707 ASSERT(vdc != NULL); 708 709 ASSERT(MUTEX_HELD(&vdc->lock)); 710 711 status = vdc_do_ldc_up(vdc); 712 713 DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance); 714 715 return (status); 716 } 717 718 static int 719 vdc_stop_ldc_connection(vdc_t *vdcp) 720 { 721 int status; 722 723 DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n", 724 vdcp->state); 725 726 status = ldc_down(vdcp->ldc_handle); 727 DMSG(vdcp, 0, "ldc_down() = %d\n", status); 728 729 vdcp->initialized &= ~VDC_HANDSHAKE; 730 DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized); 731 732 return (status); 733 } 734 735 static int 736 vdc_create_device_nodes_efi(vdc_t *vdc) 737 { 738 ddi_remove_minor_node(vdc->dip, "h"); 739 ddi_remove_minor_node(vdc->dip, "h,raw"); 740 741 if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK, 742 VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 743 DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 744 cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'", 745 vdc->instance); 746 return (EIO); 747 } 748 749 /* if any device node is created we set this flag */ 750 vdc->initialized |= VDC_MINOR; 751 752 if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR, 753 VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 754 DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 755 cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'", 756 vdc->instance); 757 return (EIO); 758 } 759 760 return (0); 761 } 762 763 static int 764 vdc_create_device_nodes_vtoc(vdc_t *vdc) 765 { 766 ddi_remove_minor_node(vdc->dip, "wd"); 767 ddi_remove_minor_node(vdc->dip, "wd,raw"); 768 769 if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK, 770 VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 771 DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 772 cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'", 773 vdc->instance); 774 return (EIO); 775 } 776 777 /* if any device node is created we set this flag */ 778 vdc->initialized |= VDC_MINOR; 779 780 if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR, 781 VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE), 782 DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 783 cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'", 784 vdc->instance); 785 return (EIO); 786 } 787 788 return (0); 789 } 790 791 /* 792 * Function: 793 * vdc_create_device_nodes 794 * 795 * Description: 796 * This function creates the block and character device nodes under 797 * /devices along with the node properties. It is called as part of 798 * the attach(9E) of the instance during the handshake with vds after 799 * vds has sent the attributes to vdc. 800 * 801 * If the device is of type VD_DISK_TYPE_SLICE then the minor node 802 * of 2 is used in keeping with the Solaris convention that slice 2 803 * refers to a whole disk. Slices start at 'a' 804 * 805 * Parameters: 806 * vdc - soft state pointer 807 * 808 * Return Values 809 * 0 - Success 810 * EIO - Failed to create node 811 * EINVAL - Unknown type of disk exported 812 */ 813 static int 814 vdc_create_device_nodes(vdc_t *vdc) 815 { 816 char name[sizeof ("s,raw")]; 817 dev_info_t *dip = NULL; 818 int instance, status; 819 int num_slices = 1; 820 int i; 821 822 ASSERT(vdc != NULL); 823 824 instance = vdc->instance; 825 dip = vdc->dip; 826 827 switch (vdc->vdisk_type) { 828 case VD_DISK_TYPE_DISK: 829 num_slices = V_NUMPAR; 830 break; 831 case VD_DISK_TYPE_SLICE: 832 num_slices = 1; 833 break; 834 case VD_DISK_TYPE_UNK: 835 default: 836 return (EINVAL); 837 } 838 839 /* 840 * Minor nodes are different for EFI disks: EFI disks do not have 841 * a minor node 'g' for the minor number corresponding to slice 842 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd' 843 * representing the whole disk. 844 */ 845 for (i = 0; i < num_slices; i++) { 846 847 if (i == VD_EFI_WD_SLICE) { 848 if (vdc->vdisk_label == VD_DISK_LABEL_EFI) 849 status = vdc_create_device_nodes_efi(vdc); 850 else 851 status = vdc_create_device_nodes_vtoc(vdc); 852 if (status != 0) 853 return (status); 854 continue; 855 } 856 857 (void) snprintf(name, sizeof (name), "%c", 'a' + i); 858 if (ddi_create_minor_node(dip, name, S_IFBLK, 859 VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 860 cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'", 861 instance, name); 862 return (EIO); 863 } 864 865 /* if any device node is created we set this flag */ 866 vdc->initialized |= VDC_MINOR; 867 868 (void) snprintf(name, sizeof (name), "%c%s", 869 'a' + i, ",raw"); 870 if (ddi_create_minor_node(dip, name, S_IFCHR, 871 VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) { 872 cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'", 873 instance, name); 874 return (EIO); 875 } 876 } 877 878 return (0); 879 } 880 881 /* 882 * Function: 883 * vdc_create_device_nodes_props 884 * 885 * Description: 886 * This function creates the block and character device nodes under 887 * /devices along with the node properties. It is called as part of 888 * the attach(9E) of the instance during the handshake with vds after 889 * vds has sent the attributes to vdc. 890 * 891 * Parameters: 892 * vdc - soft state pointer 893 * 894 * Return Values 895 * 0 - Success 896 * EIO - Failed to create device node property 897 * EINVAL - Unknown type of disk exported 898 */ 899 static int 900 vdc_create_device_nodes_props(vdc_t *vdc) 901 { 902 dev_info_t *dip = NULL; 903 int instance; 904 int num_slices = 1; 905 int64_t size = 0; 906 dev_t dev; 907 int rv; 908 int i; 909 910 ASSERT(vdc != NULL); 911 912 instance = vdc->instance; 913 dip = vdc->dip; 914 915 if ((vdc->vtoc == NULL) || (vdc->vtoc->v_sanity != VTOC_SANE)) { 916 DMSG(vdc, 0, "![%d] Could not create device node property." 917 " No VTOC available", instance); 918 return (ENXIO); 919 } 920 921 switch (vdc->vdisk_type) { 922 case VD_DISK_TYPE_DISK: 923 num_slices = V_NUMPAR; 924 break; 925 case VD_DISK_TYPE_SLICE: 926 num_slices = 1; 927 break; 928 case VD_DISK_TYPE_UNK: 929 default: 930 return (EINVAL); 931 } 932 933 for (i = 0; i < num_slices; i++) { 934 dev = makedevice(ddi_driver_major(dip), 935 VD_MAKE_DEV(instance, i)); 936 937 size = vdc->vtoc->v_part[i].p_size * vdc->vtoc->v_sectorsz; 938 DMSG(vdc, 0, "[%d] sz %ld (%ld Mb) p_size %lx\n", 939 instance, size, size / (1024 * 1024), 940 vdc->vtoc->v_part[i].p_size); 941 942 rv = ddi_prop_update_int64(dev, dip, VDC_SIZE_PROP_NAME, size); 943 if (rv != DDI_PROP_SUCCESS) { 944 cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop of [%ld]", 945 instance, VDC_SIZE_PROP_NAME, size); 946 return (EIO); 947 } 948 949 rv = ddi_prop_update_int64(dev, dip, VDC_NBLOCKS_PROP_NAME, 950 lbtodb(size)); 951 if (rv != DDI_PROP_SUCCESS) { 952 cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop [%llu]", 953 instance, VDC_NBLOCKS_PROP_NAME, lbtodb(size)); 954 return (EIO); 955 } 956 } 957 958 return (0); 959 } 960 961 static int 962 vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred) 963 { 964 _NOTE(ARGUNUSED(cred)) 965 966 int instance; 967 vdc_t *vdc; 968 969 ASSERT(dev != NULL); 970 instance = VDCUNIT(*dev); 971 972 if ((otyp != OTYP_CHR) && (otyp != OTYP_BLK)) 973 return (EINVAL); 974 975 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 976 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 977 return (ENXIO); 978 } 979 980 DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n", 981 getminor(*dev), flag, otyp); 982 983 mutex_enter(&vdc->lock); 984 vdc->open_count++; 985 mutex_exit(&vdc->lock); 986 987 return (0); 988 } 989 990 static int 991 vdc_close(dev_t dev, int flag, int otyp, cred_t *cred) 992 { 993 _NOTE(ARGUNUSED(cred)) 994 995 int instance; 996 vdc_t *vdc; 997 998 instance = VDCUNIT(dev); 999 1000 if ((otyp != OTYP_CHR) && (otyp != OTYP_BLK)) 1001 return (EINVAL); 1002 1003 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1004 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 1005 return (ENXIO); 1006 } 1007 1008 DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp); 1009 if (vdc->dkio_flush_pending) { 1010 DMSG(vdc, 0, 1011 "[%d] Cannot detach: %d outstanding DKIO flushes\n", 1012 instance, vdc->dkio_flush_pending); 1013 return (EBUSY); 1014 } 1015 1016 /* 1017 * Should not need the mutex here, since the framework should protect 1018 * against more opens on this device, but just in case. 1019 */ 1020 mutex_enter(&vdc->lock); 1021 vdc->open_count--; 1022 mutex_exit(&vdc->lock); 1023 1024 return (0); 1025 } 1026 1027 static int 1028 vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 1029 { 1030 _NOTE(ARGUNUSED(credp)) 1031 _NOTE(ARGUNUSED(rvalp)) 1032 1033 return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode)); 1034 } 1035 1036 static int 1037 vdc_print(dev_t dev, char *str) 1038 { 1039 cmn_err(CE_NOTE, "vdc%d: %s", VDCUNIT(dev), str); 1040 return (0); 1041 } 1042 1043 static int 1044 vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 1045 { 1046 int rv; 1047 size_t nbytes = nblk * DEV_BSIZE; 1048 int instance = VDCUNIT(dev); 1049 vdc_t *vdc = NULL; 1050 1051 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1052 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 1053 return (ENXIO); 1054 } 1055 1056 DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n", 1057 instance, nbytes, blkno, (void *)addr); 1058 rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes, 1059 VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir); 1060 if (rv) { 1061 DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv); 1062 return (rv); 1063 } 1064 1065 if (ddi_in_panic()) 1066 (void) vdc_drain_response(vdc); 1067 1068 DMSG(vdc, 0, "[%d] End\n", instance); 1069 1070 return (0); 1071 } 1072 1073 /* -------------------------------------------------------------------------- */ 1074 1075 /* 1076 * Disk access routines 1077 * 1078 */ 1079 1080 /* 1081 * vdc_strategy() 1082 * 1083 * Return Value: 1084 * 0: As per strategy(9E), the strategy() function must return 0 1085 * [ bioerror(9f) sets b_flags to the proper error code ] 1086 */ 1087 static int 1088 vdc_strategy(struct buf *buf) 1089 { 1090 int rv = -1; 1091 vdc_t *vdc = NULL; 1092 int instance = VDCUNIT(buf->b_edev); 1093 int op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE; 1094 1095 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) { 1096 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance); 1097 bioerror(buf, ENXIO); 1098 biodone(buf); 1099 return (0); 1100 } 1101 1102 DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n", 1103 instance, (buf->b_flags & B_READ) ? "Read" : "Write", 1104 buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr); 1105 DTRACE_IO2(vstart, buf_t *, buf, vdc_t *, vdc); 1106 1107 bp_mapin(buf); 1108 1109 rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr, 1110 buf->b_bcount, VDCPART(buf->b_edev), buf->b_lblkno, 1111 CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir : 1112 VIO_write_dir); 1113 1114 /* 1115 * If the request was successfully sent, the strategy call returns and 1116 * the ACK handler calls the bioxxx functions when the vDisk server is 1117 * done. 1118 */ 1119 if (rv) { 1120 DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv); 1121 bioerror(buf, rv); 1122 biodone(buf); 1123 } 1124 1125 return (0); 1126 } 1127 1128 /* 1129 * Function: 1130 * vdc_min 1131 * 1132 * Description: 1133 * Routine to limit the size of a data transfer. Used in 1134 * conjunction with physio(9F). 1135 * 1136 * Arguments: 1137 * bp - pointer to the indicated buf(9S) struct. 1138 * 1139 */ 1140 static void 1141 vdc_min(struct buf *bufp) 1142 { 1143 vdc_t *vdc = NULL; 1144 int instance = VDCUNIT(bufp->b_edev); 1145 1146 vdc = ddi_get_soft_state(vdc_state, instance); 1147 VERIFY(vdc != NULL); 1148 1149 if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) { 1150 bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size; 1151 } 1152 } 1153 1154 static int 1155 vdc_read(dev_t dev, struct uio *uio, cred_t *cred) 1156 { 1157 _NOTE(ARGUNUSED(cred)) 1158 1159 DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 1160 return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio)); 1161 } 1162 1163 static int 1164 vdc_write(dev_t dev, struct uio *uio, cred_t *cred) 1165 { 1166 _NOTE(ARGUNUSED(cred)) 1167 1168 DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 1169 return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio)); 1170 } 1171 1172 static int 1173 vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred) 1174 { 1175 _NOTE(ARGUNUSED(cred)) 1176 1177 DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 1178 return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio)); 1179 } 1180 1181 static int 1182 vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred) 1183 { 1184 _NOTE(ARGUNUSED(cred)) 1185 1186 DMSGX(1, "[%d] Entered", VDCUNIT(dev)); 1187 return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio)); 1188 } 1189 1190 1191 /* -------------------------------------------------------------------------- */ 1192 1193 /* 1194 * Handshake support 1195 */ 1196 1197 1198 /* 1199 * Function: 1200 * vdc_init_ver_negotiation() 1201 * 1202 * Description: 1203 * 1204 * Arguments: 1205 * vdc - soft state pointer for this instance of the device driver. 1206 * 1207 * Return Code: 1208 * 0 - Success 1209 */ 1210 static int 1211 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver) 1212 { 1213 vio_ver_msg_t pkt; 1214 size_t msglen = sizeof (pkt); 1215 int status = -1; 1216 1217 ASSERT(vdc != NULL); 1218 ASSERT(mutex_owned(&vdc->lock)); 1219 1220 DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance); 1221 1222 /* 1223 * set the Session ID to a unique value 1224 * (the lower 32 bits of the clock tick) 1225 */ 1226 vdc->session_id = ((uint32_t)gettick() & 0xffffffff); 1227 DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id); 1228 1229 pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 1230 pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 1231 pkt.tag.vio_subtype_env = VIO_VER_INFO; 1232 pkt.tag.vio_sid = vdc->session_id; 1233 pkt.dev_class = VDEV_DISK; 1234 pkt.ver_major = ver.major; 1235 pkt.ver_minor = ver.minor; 1236 1237 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 1238 DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n", 1239 vdc->instance, status); 1240 if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) { 1241 DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: " 1242 "id(%lx) rv(%d) size(%ld)", 1243 vdc->instance, vdc->ldc_handle, 1244 status, msglen); 1245 if (msglen != sizeof (vio_ver_msg_t)) 1246 status = ENOMSG; 1247 } 1248 1249 return (status); 1250 } 1251 1252 /* 1253 * Function: 1254 * vdc_ver_negotiation() 1255 * 1256 * Description: 1257 * 1258 * Arguments: 1259 * vdcp - soft state pointer for this instance of the device driver. 1260 * 1261 * Return Code: 1262 * 0 - Success 1263 */ 1264 static int 1265 vdc_ver_negotiation(vdc_t *vdcp) 1266 { 1267 vio_msg_t vio_msg; 1268 int status; 1269 1270 if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0])) 1271 return (status); 1272 1273 /* release lock and wait for response */ 1274 mutex_exit(&vdcp->lock); 1275 status = vdc_wait_for_response(vdcp, &vio_msg); 1276 mutex_enter(&vdcp->lock); 1277 if (status) { 1278 DMSG(vdcp, 0, 1279 "[%d] Failed waiting for Ver negotiation response, rv(%d)", 1280 vdcp->instance, status); 1281 return (status); 1282 } 1283 1284 /* check type and sub_type ... */ 1285 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 1286 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 1287 DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n", 1288 vdcp->instance); 1289 return (EPROTO); 1290 } 1291 1292 return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg)); 1293 } 1294 1295 /* 1296 * Function: 1297 * vdc_init_attr_negotiation() 1298 * 1299 * Description: 1300 * 1301 * Arguments: 1302 * vdc - soft state pointer for this instance of the device driver. 1303 * 1304 * Return Code: 1305 * 0 - Success 1306 */ 1307 static int 1308 vdc_init_attr_negotiation(vdc_t *vdc) 1309 { 1310 vd_attr_msg_t pkt; 1311 size_t msglen = sizeof (pkt); 1312 int status; 1313 1314 ASSERT(vdc != NULL); 1315 ASSERT(mutex_owned(&vdc->lock)); 1316 1317 DMSG(vdc, 0, "[%d] entered\n", vdc->instance); 1318 1319 /* fill in tag */ 1320 pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 1321 pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 1322 pkt.tag.vio_subtype_env = VIO_ATTR_INFO; 1323 pkt.tag.vio_sid = vdc->session_id; 1324 /* fill in payload */ 1325 pkt.max_xfer_sz = vdc->max_xfer_sz; 1326 pkt.vdisk_block_size = vdc->block_size; 1327 pkt.xfer_mode = VIO_DRING_MODE; 1328 pkt.operations = 0; /* server will set bits of valid operations */ 1329 pkt.vdisk_type = 0; /* server will set to valid device type */ 1330 pkt.vdisk_size = 0; /* server will set to valid size */ 1331 1332 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 1333 DMSG(vdc, 0, "Attr info sent (status = %d)\n", status); 1334 1335 if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) { 1336 DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: " 1337 "id(%lx) rv(%d) size(%ld)", 1338 vdc->instance, vdc->ldc_handle, 1339 status, msglen); 1340 if (msglen != sizeof (vio_ver_msg_t)) 1341 status = ENOMSG; 1342 } 1343 1344 return (status); 1345 } 1346 1347 /* 1348 * Function: 1349 * vdc_attr_negotiation() 1350 * 1351 * Description: 1352 * 1353 * Arguments: 1354 * vdc - soft state pointer for this instance of the device driver. 1355 * 1356 * Return Code: 1357 * 0 - Success 1358 */ 1359 static int 1360 vdc_attr_negotiation(vdc_t *vdcp) 1361 { 1362 int status; 1363 vio_msg_t vio_msg; 1364 1365 if (status = vdc_init_attr_negotiation(vdcp)) 1366 return (status); 1367 1368 /* release lock and wait for response */ 1369 mutex_exit(&vdcp->lock); 1370 status = vdc_wait_for_response(vdcp, &vio_msg); 1371 mutex_enter(&vdcp->lock); 1372 if (status) { 1373 DMSG(vdcp, 0, 1374 "[%d] Failed waiting for Attr negotiation response, rv(%d)", 1375 vdcp->instance, status); 1376 return (status); 1377 } 1378 1379 /* check type and sub_type ... */ 1380 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 1381 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 1382 DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n", 1383 vdcp->instance); 1384 return (EPROTO); 1385 } 1386 1387 return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg)); 1388 } 1389 1390 1391 /* 1392 * Function: 1393 * vdc_init_dring_negotiate() 1394 * 1395 * Description: 1396 * 1397 * Arguments: 1398 * vdc - soft state pointer for this instance of the device driver. 1399 * 1400 * Return Code: 1401 * 0 - Success 1402 */ 1403 static int 1404 vdc_init_dring_negotiate(vdc_t *vdc) 1405 { 1406 vio_dring_reg_msg_t pkt; 1407 size_t msglen = sizeof (pkt); 1408 int status = -1; 1409 int retry; 1410 int nretries = 10; 1411 1412 ASSERT(vdc != NULL); 1413 ASSERT(mutex_owned(&vdc->lock)); 1414 1415 for (retry = 0; retry < nretries; retry++) { 1416 status = vdc_init_descriptor_ring(vdc); 1417 if (status != EAGAIN) 1418 break; 1419 drv_usecwait(vdc_min_timeout_ldc); 1420 } 1421 1422 if (status != 0) { 1423 DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n", 1424 vdc->instance, status); 1425 return (status); 1426 } 1427 1428 DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n", 1429 vdc->instance, status); 1430 1431 /* fill in tag */ 1432 pkt.tag.vio_msgtype = VIO_TYPE_CTRL; 1433 pkt.tag.vio_subtype = VIO_SUBTYPE_INFO; 1434 pkt.tag.vio_subtype_env = VIO_DRING_REG; 1435 pkt.tag.vio_sid = vdc->session_id; 1436 /* fill in payload */ 1437 pkt.dring_ident = 0; 1438 pkt.num_descriptors = vdc->dring_len; 1439 pkt.descriptor_size = vdc->dring_entry_size; 1440 pkt.options = (VIO_TX_DRING | VIO_RX_DRING); 1441 pkt.ncookies = vdc->dring_cookie_count; 1442 pkt.cookie[0] = vdc->dring_cookie[0]; /* for now just one cookie */ 1443 1444 status = vdc_send(vdc, (caddr_t)&pkt, &msglen); 1445 if (status != 0) { 1446 DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)", 1447 vdc->instance, status); 1448 } 1449 1450 return (status); 1451 } 1452 1453 1454 /* 1455 * Function: 1456 * vdc_dring_negotiation() 1457 * 1458 * Description: 1459 * 1460 * Arguments: 1461 * vdc - soft state pointer for this instance of the device driver. 1462 * 1463 * Return Code: 1464 * 0 - Success 1465 */ 1466 static int 1467 vdc_dring_negotiation(vdc_t *vdcp) 1468 { 1469 int status; 1470 vio_msg_t vio_msg; 1471 1472 if (status = vdc_init_dring_negotiate(vdcp)) 1473 return (status); 1474 1475 /* release lock and wait for response */ 1476 mutex_exit(&vdcp->lock); 1477 status = vdc_wait_for_response(vdcp, &vio_msg); 1478 mutex_enter(&vdcp->lock); 1479 if (status) { 1480 DMSG(vdcp, 0, 1481 "[%d] Failed waiting for Dring negotiation response," 1482 " rv(%d)", vdcp->instance, status); 1483 return (status); 1484 } 1485 1486 /* check type and sub_type ... */ 1487 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 1488 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) { 1489 DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n", 1490 vdcp->instance); 1491 return (EPROTO); 1492 } 1493 1494 return (vdc_handle_dring_reg_msg(vdcp, 1495 (vio_dring_reg_msg_t *)&vio_msg)); 1496 } 1497 1498 1499 /* 1500 * Function: 1501 * vdc_send_rdx() 1502 * 1503 * Description: 1504 * 1505 * Arguments: 1506 * vdc - soft state pointer for this instance of the device driver. 1507 * 1508 * Return Code: 1509 * 0 - Success 1510 */ 1511 static int 1512 vdc_send_rdx(vdc_t *vdcp) 1513 { 1514 vio_msg_t msg; 1515 size_t msglen = sizeof (vio_msg_t); 1516 int status; 1517 1518 /* 1519 * Send an RDX message to vds to indicate we are ready 1520 * to send data 1521 */ 1522 msg.tag.vio_msgtype = VIO_TYPE_CTRL; 1523 msg.tag.vio_subtype = VIO_SUBTYPE_INFO; 1524 msg.tag.vio_subtype_env = VIO_RDX; 1525 msg.tag.vio_sid = vdcp->session_id; 1526 status = vdc_send(vdcp, (caddr_t)&msg, &msglen); 1527 if (status != 0) { 1528 DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)", 1529 vdcp->instance, status); 1530 } 1531 1532 return (status); 1533 } 1534 1535 /* 1536 * Function: 1537 * vdc_handle_rdx() 1538 * 1539 * Description: 1540 * 1541 * Arguments: 1542 * vdc - soft state pointer for this instance of the device driver. 1543 * msgp - received msg 1544 * 1545 * Return Code: 1546 * 0 - Success 1547 */ 1548 static int 1549 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp) 1550 { 1551 _NOTE(ARGUNUSED(vdcp)) 1552 _NOTE(ARGUNUSED(msgp)) 1553 1554 ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL); 1555 ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK); 1556 ASSERT(msgp->tag.vio_subtype_env == VIO_RDX); 1557 1558 DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance); 1559 1560 return (0); 1561 } 1562 1563 /* 1564 * Function: 1565 * vdc_rdx_exchange() 1566 * 1567 * Description: 1568 * 1569 * Arguments: 1570 * vdc - soft state pointer for this instance of the device driver. 1571 * 1572 * Return Code: 1573 * 0 - Success 1574 */ 1575 static int 1576 vdc_rdx_exchange(vdc_t *vdcp) 1577 { 1578 int status; 1579 vio_msg_t vio_msg; 1580 1581 if (status = vdc_send_rdx(vdcp)) 1582 return (status); 1583 1584 /* release lock and wait for response */ 1585 mutex_exit(&vdcp->lock); 1586 status = vdc_wait_for_response(vdcp, &vio_msg); 1587 mutex_enter(&vdcp->lock); 1588 if (status) { 1589 DMSG(vdcp, 0, 1590 "[%d] Failed waiting for RDX response," 1591 " rv(%d)", vdcp->instance, status); 1592 return (status); 1593 } 1594 1595 /* check type and sub_type ... */ 1596 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL || 1597 vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) { 1598 DMSG(vdcp, 0, "[%d] Invalid RDX response\n", 1599 vdcp->instance); 1600 return (EPROTO); 1601 } 1602 1603 return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg)); 1604 } 1605 1606 1607 /* -------------------------------------------------------------------------- */ 1608 1609 /* 1610 * LDC helper routines 1611 */ 1612 1613 static int 1614 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp) 1615 { 1616 int status; 1617 boolean_t q_has_pkts = B_FALSE; 1618 int delay_time; 1619 size_t len; 1620 1621 mutex_enter(&vdc->read_lock); 1622 1623 if (vdc->read_state == VDC_READ_IDLE) 1624 vdc->read_state = VDC_READ_WAITING; 1625 1626 while (vdc->read_state != VDC_READ_PENDING) { 1627 1628 /* detect if the connection has been reset */ 1629 if (vdc->read_state == VDC_READ_RESET) { 1630 status = ECONNRESET; 1631 goto done; 1632 } 1633 1634 cv_wait(&vdc->read_cv, &vdc->read_lock); 1635 } 1636 1637 /* 1638 * Until we get a blocking ldc read we have to retry 1639 * until the entire LDC message has arrived before 1640 * ldc_read() will succeed. Note we also bail out if 1641 * the chanel is reset or goes away. 1642 */ 1643 delay_time = vdc_ldc_read_init_delay; 1644 loop: 1645 len = *nbytesp; 1646 status = ldc_read(vdc->ldc_handle, (caddr_t)msgp, &len); 1647 switch (status) { 1648 case EAGAIN: 1649 delay_time *= 2; 1650 if (delay_time >= vdc_ldc_read_max_delay) 1651 delay_time = vdc_ldc_read_max_delay; 1652 delay(delay_time); 1653 goto loop; 1654 1655 case 0: 1656 if (len == 0) { 1657 DMSG(vdc, 0, "[%d] ldc_read returned 0 bytes with " 1658 "no error!\n", vdc->instance); 1659 goto loop; 1660 } 1661 1662 *nbytesp = len; 1663 1664 /* 1665 * If there are pending messages, leave the 1666 * read state as pending. Otherwise, set the state 1667 * back to idle. 1668 */ 1669 status = ldc_chkq(vdc->ldc_handle, &q_has_pkts); 1670 if (status == 0 && !q_has_pkts) 1671 vdc->read_state = VDC_READ_IDLE; 1672 1673 break; 1674 default: 1675 DMSG(vdc, 0, "ldc_read returned %d\n", status); 1676 break; 1677 } 1678 1679 done: 1680 mutex_exit(&vdc->read_lock); 1681 1682 return (status); 1683 } 1684 1685 1686 1687 #ifdef DEBUG 1688 void 1689 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg) 1690 { 1691 char *ms, *ss, *ses; 1692 switch (msg->tag.vio_msgtype) { 1693 #define Q(_s) case _s : ms = #_s; break; 1694 Q(VIO_TYPE_CTRL) 1695 Q(VIO_TYPE_DATA) 1696 Q(VIO_TYPE_ERR) 1697 #undef Q 1698 default: ms = "unknown"; break; 1699 } 1700 1701 switch (msg->tag.vio_subtype) { 1702 #define Q(_s) case _s : ss = #_s; break; 1703 Q(VIO_SUBTYPE_INFO) 1704 Q(VIO_SUBTYPE_ACK) 1705 Q(VIO_SUBTYPE_NACK) 1706 #undef Q 1707 default: ss = "unknown"; break; 1708 } 1709 1710 switch (msg->tag.vio_subtype_env) { 1711 #define Q(_s) case _s : ses = #_s; break; 1712 Q(VIO_VER_INFO) 1713 Q(VIO_ATTR_INFO) 1714 Q(VIO_DRING_REG) 1715 Q(VIO_DRING_UNREG) 1716 Q(VIO_RDX) 1717 Q(VIO_PKT_DATA) 1718 Q(VIO_DESC_DATA) 1719 Q(VIO_DRING_DATA) 1720 #undef Q 1721 default: ses = "unknown"; break; 1722 } 1723 1724 DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n", 1725 msg->tag.vio_msgtype, msg->tag.vio_subtype, 1726 msg->tag.vio_subtype_env, ms, ss, ses); 1727 } 1728 #endif 1729 1730 /* 1731 * Function: 1732 * vdc_send() 1733 * 1734 * Description: 1735 * The function encapsulates the call to write a message using LDC. 1736 * If LDC indicates that the call failed due to the queue being full, 1737 * we retry the ldc_write() [ up to 'vdc_retries' time ], otherwise 1738 * we return the error returned by LDC. 1739 * 1740 * Arguments: 1741 * ldc_handle - LDC handle for the channel this instance of vdc uses 1742 * pkt - address of LDC message to be sent 1743 * msglen - the size of the message being sent. When the function 1744 * returns, this contains the number of bytes written. 1745 * 1746 * Return Code: 1747 * 0 - Success. 1748 * EINVAL - pkt or msglen were NULL 1749 * ECONNRESET - The connection was not up. 1750 * EWOULDBLOCK - LDC queue is full 1751 * xxx - other error codes returned by ldc_write 1752 */ 1753 static int 1754 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen) 1755 { 1756 size_t size = 0; 1757 int status = 0; 1758 clock_t delay_ticks; 1759 1760 ASSERT(vdc != NULL); 1761 ASSERT(mutex_owned(&vdc->lock)); 1762 ASSERT(msglen != NULL); 1763 ASSERT(*msglen != 0); 1764 1765 #ifdef DEBUG 1766 vdc_decode_tag(vdc, (vio_msg_t *)pkt); 1767 #endif 1768 /* 1769 * Wait indefinitely to send if channel 1770 * is busy, but bail out if we succeed or 1771 * if the channel closes or is reset. 1772 */ 1773 delay_ticks = vdc_hz_min_ldc_delay; 1774 do { 1775 size = *msglen; 1776 status = ldc_write(vdc->ldc_handle, pkt, &size); 1777 if (status == EWOULDBLOCK) { 1778 delay(delay_ticks); 1779 /* geometric backoff */ 1780 delay_ticks *= 2; 1781 if (delay_ticks > vdc_hz_max_ldc_delay) 1782 delay_ticks = vdc_hz_max_ldc_delay; 1783 } 1784 } while (status == EWOULDBLOCK); 1785 1786 /* if LDC had serious issues --- reset vdc state */ 1787 if (status == EIO || status == ECONNRESET) { 1788 /* LDC had serious issues --- reset vdc state */ 1789 mutex_enter(&vdc->read_lock); 1790 if ((vdc->read_state == VDC_READ_WAITING) || 1791 (vdc->read_state == VDC_READ_RESET)) 1792 cv_signal(&vdc->read_cv); 1793 vdc->read_state = VDC_READ_RESET; 1794 mutex_exit(&vdc->read_lock); 1795 1796 /* wake up any waiters in the reset thread */ 1797 if (vdc->state == VDC_STATE_INIT_WAITING) { 1798 DMSG(vdc, 0, "[%d] write reset - " 1799 "vdc is resetting ..\n", vdc->instance); 1800 vdc->state = VDC_STATE_RESETTING; 1801 cv_signal(&vdc->initwait_cv); 1802 } 1803 1804 return (ECONNRESET); 1805 } 1806 1807 /* return the last size written */ 1808 *msglen = size; 1809 1810 return (status); 1811 } 1812 1813 /* 1814 * Function: 1815 * vdc_get_ldc_id() 1816 * 1817 * Description: 1818 * This function gets the 'ldc-id' for this particular instance of vdc. 1819 * The id returned is the guest domain channel endpoint LDC uses for 1820 * communication with vds. 1821 * 1822 * Arguments: 1823 * dip - dev info pointer for this instance of the device driver. 1824 * ldc_id - pointer to variable used to return the 'ldc-id' found. 1825 * 1826 * Return Code: 1827 * 0 - Success. 1828 * ENOENT - Expected node or property did not exist. 1829 * ENXIO - Unexpected error communicating with MD framework 1830 */ 1831 static int 1832 vdc_get_ldc_id(dev_info_t *dip, uint64_t *ldc_id) 1833 { 1834 int status = ENOENT; 1835 char *node_name = NULL; 1836 md_t *mdp = NULL; 1837 int num_nodes; 1838 int num_vdevs; 1839 int num_chans; 1840 mde_cookie_t rootnode; 1841 mde_cookie_t *listp = NULL; 1842 mde_cookie_t *chanp = NULL; 1843 boolean_t found_inst = B_FALSE; 1844 int listsz; 1845 int idx; 1846 uint64_t md_inst; 1847 int obp_inst; 1848 int instance = ddi_get_instance(dip); 1849 1850 ASSERT(ldc_id != NULL); 1851 *ldc_id = 0; 1852 1853 /* 1854 * Get the OBP instance number for comparison with the MD instance 1855 * 1856 * The "cfg-handle" property of a vdc node in an MD contains the MD's 1857 * notion of "instance", or unique identifier, for that node; OBP 1858 * stores the value of the "cfg-handle" MD property as the value of 1859 * the "reg" property on the node in the device tree it builds from 1860 * the MD and passes to Solaris. Thus, we look up the devinfo node's 1861 * "reg" property value to uniquely identify this device instance. 1862 * If the "reg" property cannot be found, the device tree state is 1863 * presumably so broken that there is no point in continuing. 1864 */ 1865 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) { 1866 cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG); 1867 return (ENOENT); 1868 } 1869 obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1870 OBP_REG, -1); 1871 DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst); 1872 1873 /* 1874 * We now walk the MD nodes and if an instance of a vdc node matches 1875 * the instance got from OBP we get the ldc-id property. 1876 */ 1877 if ((mdp = md_get_handle()) == NULL) { 1878 cmn_err(CE_WARN, "unable to init machine description"); 1879 return (ENXIO); 1880 } 1881 1882 num_nodes = md_node_count(mdp); 1883 ASSERT(num_nodes > 0); 1884 1885 listsz = num_nodes * sizeof (mde_cookie_t); 1886 1887 /* allocate memory for nodes */ 1888 listp = kmem_zalloc(listsz, KM_SLEEP); 1889 chanp = kmem_zalloc(listsz, KM_SLEEP); 1890 1891 rootnode = md_root_node(mdp); 1892 ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE); 1893 1894 /* 1895 * Search for all the virtual devices, we will then check to see which 1896 * ones are disk nodes. 1897 */ 1898 num_vdevs = md_scan_dag(mdp, rootnode, 1899 md_find_name(mdp, VDC_MD_VDEV_NAME), 1900 md_find_name(mdp, "fwd"), listp); 1901 1902 if (num_vdevs <= 0) { 1903 cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME); 1904 status = ENOENT; 1905 goto done; 1906 } 1907 1908 DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs); 1909 for (idx = 0; idx < num_vdevs; idx++) { 1910 status = md_get_prop_str(mdp, listp[idx], "name", &node_name); 1911 if ((status != 0) || (node_name == NULL)) { 1912 cmn_err(CE_NOTE, "Unable to get name of node type '%s'" 1913 ": err %d", VDC_MD_VDEV_NAME, status); 1914 continue; 1915 } 1916 1917 DMSGX(1, "[%d] Found node '%s'\n", instance, node_name); 1918 if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) { 1919 status = md_get_prop_val(mdp, listp[idx], 1920 VDC_MD_CFG_HDL, &md_inst); 1921 DMSGX(1, "[%d] vdc inst in MD=%lx\n", 1922 instance, md_inst); 1923 if ((status == 0) && (md_inst == obp_inst)) { 1924 found_inst = B_TRUE; 1925 break; 1926 } 1927 } 1928 } 1929 1930 if (!found_inst) { 1931 DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME); 1932 status = ENOENT; 1933 goto done; 1934 } 1935 DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst); 1936 1937 /* get the channels for this node */ 1938 num_chans = md_scan_dag(mdp, listp[idx], 1939 md_find_name(mdp, VDC_MD_CHAN_NAME), 1940 md_find_name(mdp, "fwd"), chanp); 1941 1942 /* expecting at least one channel */ 1943 if (num_chans <= 0) { 1944 cmn_err(CE_NOTE, "No '%s' node for '%s' port", 1945 VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME); 1946 status = ENOENT; 1947 goto done; 1948 1949 } else if (num_chans != 1) { 1950 DMSGX(0, "[%d] Expected 1 '%s' node for '%s' port, found %d\n", 1951 instance, VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME, 1952 num_chans); 1953 } 1954 1955 /* 1956 * We use the first channel found (index 0), irrespective of how 1957 * many are there in total. 1958 */ 1959 if (md_get_prop_val(mdp, chanp[0], VDC_ID_PROP, ldc_id) != 0) { 1960 cmn_err(CE_NOTE, "Channel '%s' property not found", 1961 VDC_ID_PROP); 1962 status = ENOENT; 1963 } 1964 1965 DMSGX(0, "[%d] LDC id is 0x%lx\n", instance, *ldc_id); 1966 1967 done: 1968 if (chanp) 1969 kmem_free(chanp, listsz); 1970 if (listp) 1971 kmem_free(listp, listsz); 1972 1973 (void) md_fini_handle(mdp); 1974 1975 return (status); 1976 } 1977 1978 static int 1979 vdc_do_ldc_up(vdc_t *vdc) 1980 { 1981 int status; 1982 ldc_status_t ldc_state; 1983 1984 DMSG(vdc, 0, "[%d] Bringing up channel %lx\n", 1985 vdc->instance, vdc->ldc_id); 1986 1987 if (vdc->lifecycle == VDC_LC_DETACHING) 1988 return (EINVAL); 1989 1990 if ((status = ldc_up(vdc->ldc_handle)) != 0) { 1991 switch (status) { 1992 case ECONNREFUSED: /* listener not ready at other end */ 1993 DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n", 1994 vdc->instance, vdc->ldc_id, status); 1995 status = 0; 1996 break; 1997 default: 1998 DMSG(vdc, 0, "[%d] Failed to bring up LDC: " 1999 "channel=%ld, err=%d", vdc->instance, vdc->ldc_id, 2000 status); 2001 break; 2002 } 2003 } 2004 2005 if (ldc_status(vdc->ldc_handle, &ldc_state) == 0) { 2006 vdc->ldc_state = ldc_state; 2007 if (ldc_state == LDC_UP) { 2008 DMSG(vdc, 0, "[%d] LDC channel already up\n", 2009 vdc->instance); 2010 vdc->seq_num = 1; 2011 vdc->seq_num_reply = 0; 2012 } 2013 } 2014 2015 return (status); 2016 } 2017 2018 /* 2019 * Function: 2020 * vdc_terminate_ldc() 2021 * 2022 * Description: 2023 * 2024 * Arguments: 2025 * vdc - soft state pointer for this instance of the device driver. 2026 * 2027 * Return Code: 2028 * None 2029 */ 2030 static void 2031 vdc_terminate_ldc(vdc_t *vdc) 2032 { 2033 int instance = ddi_get_instance(vdc->dip); 2034 2035 ASSERT(vdc != NULL); 2036 ASSERT(mutex_owned(&vdc->lock)); 2037 2038 DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized); 2039 2040 if (vdc->initialized & VDC_LDC_OPEN) { 2041 DMSG(vdc, 0, "[%d] ldc_close()\n", instance); 2042 (void) ldc_close(vdc->ldc_handle); 2043 } 2044 if (vdc->initialized & VDC_LDC_CB) { 2045 DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance); 2046 (void) ldc_unreg_callback(vdc->ldc_handle); 2047 } 2048 if (vdc->initialized & VDC_LDC) { 2049 DMSG(vdc, 0, "[%d] ldc_fini()\n", instance); 2050 (void) ldc_fini(vdc->ldc_handle); 2051 vdc->ldc_handle = NULL; 2052 } 2053 2054 vdc->initialized &= ~(VDC_LDC | VDC_LDC_CB | VDC_LDC_OPEN); 2055 } 2056 2057 /* -------------------------------------------------------------------------- */ 2058 2059 /* 2060 * Descriptor Ring helper routines 2061 */ 2062 2063 /* 2064 * Function: 2065 * vdc_init_descriptor_ring() 2066 * 2067 * Description: 2068 * 2069 * Arguments: 2070 * vdc - soft state pointer for this instance of the device driver. 2071 * 2072 * Return Code: 2073 * 0 - Success 2074 */ 2075 static int 2076 vdc_init_descriptor_ring(vdc_t *vdc) 2077 { 2078 vd_dring_entry_t *dep = NULL; /* DRing Entry pointer */ 2079 int status = 0; 2080 int i; 2081 2082 DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized); 2083 2084 ASSERT(vdc != NULL); 2085 ASSERT(mutex_owned(&vdc->lock)); 2086 ASSERT(vdc->ldc_handle != NULL); 2087 2088 /* ensure we have enough room to store max sized block */ 2089 ASSERT(maxphys <= VD_MAX_BLOCK_SIZE); 2090 2091 if ((vdc->initialized & VDC_DRING_INIT) == 0) { 2092 DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance); 2093 /* 2094 * Calculate the maximum block size we can transmit using one 2095 * Descriptor Ring entry from the attributes returned by the 2096 * vDisk server. This is subject to a minimum of 'maxphys' 2097 * as we do not have the capability to split requests over 2098 * multiple DRing entries. 2099 */ 2100 if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) { 2101 DMSG(vdc, 0, "[%d] using minimum DRing size\n", 2102 vdc->instance); 2103 vdc->dring_max_cookies = maxphys / PAGESIZE; 2104 } else { 2105 vdc->dring_max_cookies = 2106 (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE; 2107 } 2108 vdc->dring_entry_size = (sizeof (vd_dring_entry_t) + 2109 (sizeof (ldc_mem_cookie_t) * 2110 (vdc->dring_max_cookies - 1))); 2111 vdc->dring_len = VD_DRING_LEN; 2112 2113 status = ldc_mem_dring_create(vdc->dring_len, 2114 vdc->dring_entry_size, &vdc->ldc_dring_hdl); 2115 if ((vdc->ldc_dring_hdl == NULL) || (status != 0)) { 2116 DMSG(vdc, 0, "[%d] Descriptor ring creation failed", 2117 vdc->instance); 2118 return (status); 2119 } 2120 vdc->initialized |= VDC_DRING_INIT; 2121 } 2122 2123 if ((vdc->initialized & VDC_DRING_BOUND) == 0) { 2124 DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance); 2125 vdc->dring_cookie = 2126 kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP); 2127 2128 status = ldc_mem_dring_bind(vdc->ldc_handle, vdc->ldc_dring_hdl, 2129 LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW, 2130 &vdc->dring_cookie[0], 2131 &vdc->dring_cookie_count); 2132 if (status != 0) { 2133 DMSG(vdc, 0, "[%d] Failed to bind descriptor ring " 2134 "(%lx) to channel (%lx) status=%d\n", 2135 vdc->instance, vdc->ldc_dring_hdl, 2136 vdc->ldc_handle, status); 2137 return (status); 2138 } 2139 ASSERT(vdc->dring_cookie_count == 1); 2140 vdc->initialized |= VDC_DRING_BOUND; 2141 } 2142 2143 status = ldc_mem_dring_info(vdc->ldc_dring_hdl, &vdc->dring_mem_info); 2144 if (status != 0) { 2145 DMSG(vdc, 0, 2146 "[%d] Failed to get info for descriptor ring (%lx)\n", 2147 vdc->instance, vdc->ldc_dring_hdl); 2148 return (status); 2149 } 2150 2151 if ((vdc->initialized & VDC_DRING_LOCAL) == 0) { 2152 DMSG(vdc, 0, "[%d] local dring\n", vdc->instance); 2153 2154 /* Allocate the local copy of this dring */ 2155 vdc->local_dring = 2156 kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t), 2157 KM_SLEEP); 2158 vdc->initialized |= VDC_DRING_LOCAL; 2159 } 2160 2161 /* 2162 * Mark all DRing entries as free and initialize the private 2163 * descriptor's memory handles. If any entry is initialized, 2164 * we need to free it later so we set the bit in 'initialized' 2165 * at the start. 2166 */ 2167 vdc->initialized |= VDC_DRING_ENTRY; 2168 for (i = 0; i < vdc->dring_len; i++) { 2169 dep = VDC_GET_DRING_ENTRY_PTR(vdc, i); 2170 dep->hdr.dstate = VIO_DESC_FREE; 2171 2172 status = ldc_mem_alloc_handle(vdc->ldc_handle, 2173 &vdc->local_dring[i].desc_mhdl); 2174 if (status != 0) { 2175 DMSG(vdc, 0, "![%d] Failed to alloc mem handle for" 2176 " descriptor %d", vdc->instance, i); 2177 return (status); 2178 } 2179 vdc->local_dring[i].is_free = B_TRUE; 2180 vdc->local_dring[i].dep = dep; 2181 } 2182 2183 /* Initialize the starting index */ 2184 vdc->dring_curr_idx = 0; 2185 2186 return (status); 2187 } 2188 2189 /* 2190 * Function: 2191 * vdc_destroy_descriptor_ring() 2192 * 2193 * Description: 2194 * 2195 * Arguments: 2196 * vdc - soft state pointer for this instance of the device driver. 2197 * 2198 * Return Code: 2199 * None 2200 */ 2201 static void 2202 vdc_destroy_descriptor_ring(vdc_t *vdc) 2203 { 2204 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 2205 ldc_mem_handle_t mhdl = NULL; 2206 ldc_mem_info_t minfo; 2207 int status = -1; 2208 int i; /* loop */ 2209 2210 ASSERT(vdc != NULL); 2211 ASSERT(mutex_owned(&vdc->lock)); 2212 2213 DMSG(vdc, 0, "[%d] Entered\n", vdc->instance); 2214 2215 if (vdc->initialized & VDC_DRING_ENTRY) { 2216 DMSG(vdc, 0, 2217 "[%d] Removing Local DRing entries\n", vdc->instance); 2218 for (i = 0; i < vdc->dring_len; i++) { 2219 ldep = &vdc->local_dring[i]; 2220 mhdl = ldep->desc_mhdl; 2221 2222 if (mhdl == NULL) 2223 continue; 2224 2225 if ((status = ldc_mem_info(mhdl, &minfo)) != 0) { 2226 DMSG(vdc, 0, 2227 "ldc_mem_info returned an error: %d\n", 2228 status); 2229 2230 /* 2231 * This must mean that the mem handle 2232 * is not valid. Clear it out so that 2233 * no one tries to use it. 2234 */ 2235 ldep->desc_mhdl = NULL; 2236 continue; 2237 } 2238 2239 if (minfo.status == LDC_BOUND) { 2240 (void) ldc_mem_unbind_handle(mhdl); 2241 } 2242 2243 (void) ldc_mem_free_handle(mhdl); 2244 2245 ldep->desc_mhdl = NULL; 2246 } 2247 vdc->initialized &= ~VDC_DRING_ENTRY; 2248 } 2249 2250 if (vdc->initialized & VDC_DRING_LOCAL) { 2251 DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance); 2252 kmem_free(vdc->local_dring, 2253 vdc->dring_len * sizeof (vdc_local_desc_t)); 2254 vdc->initialized &= ~VDC_DRING_LOCAL; 2255 } 2256 2257 if (vdc->initialized & VDC_DRING_BOUND) { 2258 DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance); 2259 status = ldc_mem_dring_unbind(vdc->ldc_dring_hdl); 2260 if (status == 0) { 2261 vdc->initialized &= ~VDC_DRING_BOUND; 2262 } else { 2263 DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx", 2264 vdc->instance, status, vdc->ldc_dring_hdl); 2265 } 2266 kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t)); 2267 } 2268 2269 if (vdc->initialized & VDC_DRING_INIT) { 2270 DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance); 2271 status = ldc_mem_dring_destroy(vdc->ldc_dring_hdl); 2272 if (status == 0) { 2273 vdc->ldc_dring_hdl = NULL; 2274 bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t)); 2275 vdc->initialized &= ~VDC_DRING_INIT; 2276 } else { 2277 DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)", 2278 vdc->instance, status, vdc->ldc_dring_hdl); 2279 } 2280 } 2281 } 2282 2283 /* 2284 * Function: 2285 * vdc_map_to_shared_ring() 2286 * 2287 * Description: 2288 * Copy contents of the local descriptor to the shared 2289 * memory descriptor. 2290 * 2291 * Arguments: 2292 * vdcp - soft state pointer for this instance of the device driver. 2293 * idx - descriptor ring index 2294 * 2295 * Return Code: 2296 * None 2297 */ 2298 static int 2299 vdc_map_to_shared_dring(vdc_t *vdcp, int idx) 2300 { 2301 vdc_local_desc_t *ldep; 2302 vd_dring_entry_t *dep; 2303 int rv; 2304 2305 ldep = &(vdcp->local_dring[idx]); 2306 2307 /* for now leave in the old pop_mem_hdl stuff */ 2308 if (ldep->nbytes > 0) { 2309 rv = vdc_populate_mem_hdl(vdcp, ldep); 2310 if (rv) { 2311 DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n", 2312 vdcp->instance); 2313 return (rv); 2314 } 2315 } 2316 2317 /* 2318 * fill in the data details into the DRing 2319 */ 2320 dep = ldep->dep; 2321 ASSERT(dep != NULL); 2322 2323 dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp); 2324 dep->payload.operation = ldep->operation; 2325 dep->payload.addr = ldep->offset; 2326 dep->payload.nbytes = ldep->nbytes; 2327 dep->payload.status = (uint32_t)-1; /* vds will set valid value */ 2328 dep->payload.slice = ldep->slice; 2329 dep->hdr.dstate = VIO_DESC_READY; 2330 dep->hdr.ack = 1; /* request an ACK for every message */ 2331 2332 return (0); 2333 } 2334 2335 /* 2336 * Function: 2337 * vdc_send_request 2338 * 2339 * Description: 2340 * This routine writes the data to be transmitted to vds into the 2341 * descriptor, notifies vds that the ring has been updated and 2342 * then waits for the request to be processed. 2343 * 2344 * Arguments: 2345 * vdcp - the soft state pointer 2346 * operation - operation we want vds to perform (VD_OP_XXX) 2347 * addr - address of data buf to be read/written. 2348 * nbytes - number of bytes to read/write 2349 * slice - the disk slice this request is for 2350 * offset - relative disk offset 2351 * cb_type - type of call - STRATEGY or SYNC 2352 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 2353 * . mode for ioctl(9e) 2354 * . LP64 diskaddr_t (block I/O) 2355 * dir - direction of operation (READ/WRITE/BOTH) 2356 * 2357 * Return Codes: 2358 * 0 2359 * ENXIO 2360 */ 2361 static int 2362 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr, 2363 size_t nbytes, int slice, diskaddr_t offset, int cb_type, 2364 void *cb_arg, vio_desc_direction_t dir) 2365 { 2366 ASSERT(vdcp != NULL); 2367 ASSERT(slice < V_NUMPAR); 2368 2369 mutex_enter(&vdcp->lock); 2370 2371 do { 2372 while (vdcp->state != VDC_STATE_RUNNING) { 2373 cv_wait(&vdcp->running_cv, &vdcp->lock); 2374 2375 /* return error if detaching */ 2376 if (vdcp->state == VDC_STATE_DETACH) { 2377 mutex_exit(&vdcp->lock); 2378 return (ENXIO); 2379 } 2380 } 2381 2382 } while (vdc_populate_descriptor(vdcp, operation, addr, 2383 nbytes, slice, offset, cb_type, cb_arg, dir)); 2384 2385 mutex_exit(&vdcp->lock); 2386 return (0); 2387 } 2388 2389 2390 /* 2391 * Function: 2392 * vdc_populate_descriptor 2393 * 2394 * Description: 2395 * This routine writes the data to be transmitted to vds into the 2396 * descriptor, notifies vds that the ring has been updated and 2397 * then waits for the request to be processed. 2398 * 2399 * Arguments: 2400 * vdcp - the soft state pointer 2401 * operation - operation we want vds to perform (VD_OP_XXX) 2402 * addr - address of data buf to be read/written. 2403 * nbytes - number of bytes to read/write 2404 * slice - the disk slice this request is for 2405 * offset - relative disk offset 2406 * cb_type - type of call - STRATEGY or SYNC 2407 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 2408 * . mode for ioctl(9e) 2409 * . LP64 diskaddr_t (block I/O) 2410 * dir - direction of operation (READ/WRITE/BOTH) 2411 * 2412 * Return Codes: 2413 * 0 2414 * EAGAIN 2415 * EFAULT 2416 * ENXIO 2417 * EIO 2418 */ 2419 static int 2420 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr, 2421 size_t nbytes, int slice, diskaddr_t offset, int cb_type, 2422 void *cb_arg, vio_desc_direction_t dir) 2423 { 2424 vdc_local_desc_t *local_dep = NULL; /* Local Dring Pointer */ 2425 int idx; /* Index of DRing entry used */ 2426 int next_idx; 2427 vio_dring_msg_t dmsg; 2428 size_t msglen; 2429 int rv; 2430 2431 ASSERT(MUTEX_HELD(&vdcp->lock)); 2432 vdcp->threads_pending++; 2433 loop: 2434 DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx); 2435 2436 /* Get next available D-Ring entry */ 2437 idx = vdcp->dring_curr_idx; 2438 local_dep = &(vdcp->local_dring[idx]); 2439 2440 if (!local_dep->is_free) { 2441 DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n", 2442 vdcp->instance); 2443 cv_wait(&vdcp->dring_free_cv, &vdcp->lock); 2444 if (vdcp->state == VDC_STATE_RUNNING || 2445 vdcp->state == VDC_STATE_HANDLE_PENDING) { 2446 goto loop; 2447 } 2448 vdcp->threads_pending--; 2449 return (ECONNRESET); 2450 } 2451 2452 next_idx = idx + 1; 2453 if (next_idx >= vdcp->dring_len) 2454 next_idx = 0; 2455 vdcp->dring_curr_idx = next_idx; 2456 2457 ASSERT(local_dep->is_free); 2458 2459 local_dep->operation = operation; 2460 local_dep->addr = addr; 2461 local_dep->nbytes = nbytes; 2462 local_dep->slice = slice; 2463 local_dep->offset = offset; 2464 local_dep->cb_type = cb_type; 2465 local_dep->cb_arg = cb_arg; 2466 local_dep->dir = dir; 2467 2468 local_dep->is_free = B_FALSE; 2469 2470 rv = vdc_map_to_shared_dring(vdcp, idx); 2471 if (rv) { 2472 DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n", 2473 vdcp->instance); 2474 /* free the descriptor */ 2475 local_dep->is_free = B_TRUE; 2476 vdcp->dring_curr_idx = idx; 2477 cv_wait(&vdcp->membind_cv, &vdcp->lock); 2478 if (vdcp->state == VDC_STATE_RUNNING || 2479 vdcp->state == VDC_STATE_HANDLE_PENDING) { 2480 goto loop; 2481 } 2482 vdcp->threads_pending--; 2483 return (ECONNRESET); 2484 } 2485 2486 /* 2487 * Send a msg with the DRing details to vds 2488 */ 2489 VIO_INIT_DRING_DATA_TAG(dmsg); 2490 VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp); 2491 dmsg.dring_ident = vdcp->dring_ident; 2492 dmsg.start_idx = idx; 2493 dmsg.end_idx = idx; 2494 vdcp->seq_num++; 2495 2496 DTRACE_IO2(send, vio_dring_msg_t *, &dmsg, vdc_t *, vdcp); 2497 2498 DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n", 2499 vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num); 2500 2501 /* 2502 * note we're still holding the lock here to 2503 * make sure the message goes out in order !!!... 2504 */ 2505 msglen = sizeof (dmsg); 2506 rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen); 2507 switch (rv) { 2508 case ECONNRESET: 2509 /* 2510 * vdc_send initiates the reset on failure. 2511 * Since the transaction has already been put 2512 * on the local dring, it will automatically get 2513 * retried when the channel is reset. Given that, 2514 * it is ok to just return success even though the 2515 * send failed. 2516 */ 2517 rv = 0; 2518 break; 2519 2520 case 0: /* EOK */ 2521 DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv); 2522 break; 2523 2524 default: 2525 goto cleanup_and_exit; 2526 } 2527 2528 vdcp->threads_pending--; 2529 return (rv); 2530 2531 cleanup_and_exit: 2532 DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv); 2533 return (ENXIO); 2534 } 2535 2536 /* 2537 * Function: 2538 * vdc_do_sync_op 2539 * 2540 * Description: 2541 * Wrapper around vdc_populate_descriptor that blocks until the 2542 * response to the message is available. 2543 * 2544 * Arguments: 2545 * vdcp - the soft state pointer 2546 * operation - operation we want vds to perform (VD_OP_XXX) 2547 * addr - address of data buf to be read/written. 2548 * nbytes - number of bytes to read/write 2549 * slice - the disk slice this request is for 2550 * offset - relative disk offset 2551 * cb_type - type of call - STRATEGY or SYNC 2552 * cb_arg - parameter to be sent to server (depends on VD_OP_XXX type) 2553 * . mode for ioctl(9e) 2554 * . LP64 diskaddr_t (block I/O) 2555 * dir - direction of operation (READ/WRITE/BOTH) 2556 * 2557 * Return Codes: 2558 * 0 2559 * EAGAIN 2560 * EFAULT 2561 * ENXIO 2562 * EIO 2563 */ 2564 static int 2565 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes, 2566 int slice, diskaddr_t offset, int cb_type, void *cb_arg, 2567 vio_desc_direction_t dir) 2568 { 2569 int status; 2570 2571 ASSERT(cb_type == CB_SYNC); 2572 2573 /* 2574 * Grab the lock, if blocked wait until the server 2575 * response causes us to wake up again. 2576 */ 2577 mutex_enter(&vdcp->lock); 2578 vdcp->sync_op_cnt++; 2579 while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH) 2580 cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock); 2581 2582 if (vdcp->state == VDC_STATE_DETACH) { 2583 cv_broadcast(&vdcp->sync_blocked_cv); 2584 vdcp->sync_op_cnt--; 2585 mutex_exit(&vdcp->lock); 2586 return (ENXIO); 2587 } 2588 2589 /* now block anyone other thread entering after us */ 2590 vdcp->sync_op_blocked = B_TRUE; 2591 vdcp->sync_op_pending = B_TRUE; 2592 mutex_exit(&vdcp->lock); 2593 2594 /* 2595 * No need to check return value - will return error only 2596 * in the DETACH case and we can fall through 2597 */ 2598 (void) vdc_send_request(vdcp, operation, addr, 2599 nbytes, slice, offset, cb_type, cb_arg, dir); 2600 2601 /* 2602 * block until our transaction completes. 2603 * Also anyone else waiting also gets to go next. 2604 */ 2605 mutex_enter(&vdcp->lock); 2606 while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH) 2607 cv_wait(&vdcp->sync_pending_cv, &vdcp->lock); 2608 2609 DMSG(vdcp, 2, ": operation returned %d\n", vdcp->sync_op_status); 2610 if (vdcp->state == VDC_STATE_DETACH) { 2611 vdcp->sync_op_pending = B_FALSE; 2612 status = ENXIO; 2613 } else { 2614 status = vdcp->sync_op_status; 2615 } 2616 2617 vdcp->sync_op_status = 0; 2618 vdcp->sync_op_blocked = B_FALSE; 2619 vdcp->sync_op_cnt--; 2620 2621 /* signal the next waiting thread */ 2622 cv_signal(&vdcp->sync_blocked_cv); 2623 mutex_exit(&vdcp->lock); 2624 2625 return (status); 2626 } 2627 2628 2629 /* 2630 * Function: 2631 * vdc_drain_response() 2632 * 2633 * Description: 2634 * When a guest is panicking, the completion of requests needs to be 2635 * handled differently because interrupts are disabled and vdc 2636 * will not get messages. We have to poll for the messages instead. 2637 * 2638 * Arguments: 2639 * vdc - soft state pointer for this instance of the device driver. 2640 * 2641 * Return Code: 2642 * 0 - Success 2643 */ 2644 static int 2645 vdc_drain_response(vdc_t *vdc) 2646 { 2647 int rv, idx, retries; 2648 size_t msglen; 2649 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 2650 vio_dring_msg_t dmsg; 2651 2652 mutex_enter(&vdc->lock); 2653 2654 retries = 0; 2655 for (;;) { 2656 msglen = sizeof (dmsg); 2657 rv = ldc_read(vdc->ldc_handle, (caddr_t)&dmsg, &msglen); 2658 if (rv) { 2659 rv = EINVAL; 2660 break; 2661 } 2662 2663 /* 2664 * if there are no packets wait and check again 2665 */ 2666 if ((rv == 0) && (msglen == 0)) { 2667 if (retries++ > vdc_dump_retries) { 2668 rv = EAGAIN; 2669 break; 2670 } 2671 2672 drv_usecwait(vdc_usec_timeout_dump); 2673 continue; 2674 } 2675 2676 /* 2677 * Ignore all messages that are not ACKs/NACKs to 2678 * DRing requests. 2679 */ 2680 if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) || 2681 (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) { 2682 DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n", 2683 dmsg.tag.vio_msgtype, 2684 dmsg.tag.vio_subtype, 2685 dmsg.tag.vio_subtype_env); 2686 continue; 2687 } 2688 2689 /* 2690 * set the appropriate return value for the current request. 2691 */ 2692 switch (dmsg.tag.vio_subtype) { 2693 case VIO_SUBTYPE_ACK: 2694 rv = 0; 2695 break; 2696 case VIO_SUBTYPE_NACK: 2697 rv = EAGAIN; 2698 break; 2699 default: 2700 continue; 2701 } 2702 2703 idx = dmsg.start_idx; 2704 if (idx >= vdc->dring_len) { 2705 DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n", 2706 vdc->instance, idx); 2707 continue; 2708 } 2709 ldep = &vdc->local_dring[idx]; 2710 if (ldep->dep->hdr.dstate != VIO_DESC_DONE) { 2711 DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n", 2712 vdc->instance, idx, ldep->dep->hdr.dstate); 2713 continue; 2714 } 2715 2716 DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n", 2717 vdc->instance, idx, ldep->dep->hdr.dstate); 2718 rv = vdc_depopulate_descriptor(vdc, idx); 2719 if (rv) { 2720 DMSG(vdc, 0, 2721 "[%d] Entry @ %d - depopulate failed ..\n", 2722 vdc->instance, idx); 2723 } 2724 2725 /* if this is the last descriptor - break out of loop */ 2726 if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx) 2727 break; 2728 } 2729 2730 mutex_exit(&vdc->lock); 2731 DMSG(vdc, 0, "End idx=%d\n", idx); 2732 2733 return (rv); 2734 } 2735 2736 2737 /* 2738 * Function: 2739 * vdc_depopulate_descriptor() 2740 * 2741 * Description: 2742 * 2743 * Arguments: 2744 * vdc - soft state pointer for this instance of the device driver. 2745 * idx - Index of the Descriptor Ring entry being modified 2746 * 2747 * Return Code: 2748 * 0 - Success 2749 */ 2750 static int 2751 vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx) 2752 { 2753 vd_dring_entry_t *dep = NULL; /* Dring Entry Pointer */ 2754 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */ 2755 int status = ENXIO; 2756 int operation; 2757 int rv = 0; 2758 2759 ASSERT(vdc != NULL); 2760 ASSERT(idx < vdc->dring_len); 2761 ldep = &vdc->local_dring[idx]; 2762 ASSERT(ldep != NULL); 2763 ASSERT(MUTEX_HELD(&vdc->lock)); 2764 2765 DMSG(vdc, 2, ": idx = %d\n", idx); 2766 dep = ldep->dep; 2767 ASSERT(dep != NULL); 2768 ASSERT((dep->hdr.dstate == VIO_DESC_DONE) || 2769 (dep->payload.status == ECANCELED)); 2770 2771 VDC_MARK_DRING_ENTRY_FREE(vdc, idx); 2772 2773 ldep->is_free = B_TRUE; 2774 DMSG(vdc, 2, ": is_free = %d\n", ldep->is_free); 2775 status = dep->payload.status; 2776 operation = dep->payload.operation; 2777 2778 /* the DKIO FLUSH operation never bind handles so we can return now */ 2779 if (operation == VD_OP_FLUSH) 2780 return (status); 2781 2782 /* 2783 * If the upper layer passed in a misaligned address we copied the 2784 * data into an aligned buffer before sending it to LDC - we now 2785 * copy it back to the original buffer. 2786 */ 2787 if (ldep->align_addr) { 2788 ASSERT(ldep->addr != NULL); 2789 2790 if (dep->payload.nbytes > 0) 2791 bcopy(ldep->align_addr, ldep->addr, 2792 dep->payload.nbytes); 2793 kmem_free(ldep->align_addr, 2794 sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8)); 2795 ldep->align_addr = NULL; 2796 } 2797 2798 rv = ldc_mem_unbind_handle(ldep->desc_mhdl); 2799 if (rv != 0) { 2800 DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)", 2801 vdc->instance, ldep->desc_mhdl, idx, rv); 2802 /* 2803 * The error returned by the vDisk server is more informative 2804 * and thus has a higher priority but if it isn't set we ensure 2805 * that this function returns an error. 2806 */ 2807 if (status == 0) 2808 status = EINVAL; 2809 } 2810 2811 cv_signal(&vdc->membind_cv); 2812 cv_signal(&vdc->dring_free_cv); 2813 2814 return (status); 2815 } 2816 2817 /* 2818 * Function: 2819 * vdc_populate_mem_hdl() 2820 * 2821 * Description: 2822 * 2823 * Arguments: 2824 * vdc - soft state pointer for this instance of the device driver. 2825 * idx - Index of the Descriptor Ring entry being modified 2826 * addr - virtual address being mapped in 2827 * nybtes - number of bytes in 'addr' 2828 * operation - the vDisk operation being performed (VD_OP_xxx) 2829 * 2830 * Return Code: 2831 * 0 - Success 2832 */ 2833 static int 2834 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep) 2835 { 2836 vd_dring_entry_t *dep = NULL; 2837 ldc_mem_handle_t mhdl; 2838 caddr_t vaddr; 2839 size_t nbytes; 2840 uint8_t perm = LDC_MEM_RW; 2841 uint8_t maptype; 2842 int rv = 0; 2843 int i; 2844 2845 ASSERT(vdcp != NULL); 2846 2847 dep = ldep->dep; 2848 mhdl = ldep->desc_mhdl; 2849 2850 switch (ldep->dir) { 2851 case VIO_read_dir: 2852 perm = LDC_MEM_W; 2853 break; 2854 2855 case VIO_write_dir: 2856 perm = LDC_MEM_R; 2857 break; 2858 2859 case VIO_both_dir: 2860 perm = LDC_MEM_RW; 2861 break; 2862 2863 default: 2864 ASSERT(0); /* catch bad programming in vdc */ 2865 } 2866 2867 /* 2868 * LDC expects any addresses passed in to be 8-byte aligned. We need 2869 * to copy the contents of any misaligned buffers to a newly allocated 2870 * buffer and bind it instead (and copy the the contents back to the 2871 * original buffer passed in when depopulating the descriptor) 2872 */ 2873 vaddr = ldep->addr; 2874 nbytes = ldep->nbytes; 2875 if (((uint64_t)vaddr & 0x7) != 0) { 2876 ASSERT(ldep->align_addr == NULL); 2877 ldep->align_addr = 2878 kmem_alloc(sizeof (caddr_t) * 2879 P2ROUNDUP(nbytes, 8), KM_SLEEP); 2880 DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating " 2881 "(buf=%p nb=%ld op=%d)\n", 2882 vdcp->instance, (void *)vaddr, (void *)ldep->align_addr, 2883 nbytes, ldep->operation); 2884 if (perm != LDC_MEM_W) 2885 bcopy(vaddr, ldep->align_addr, nbytes); 2886 vaddr = ldep->align_addr; 2887 } 2888 2889 maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP; 2890 rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8), 2891 maptype, perm, &dep->payload.cookie[0], 2892 &dep->payload.ncookies); 2893 DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n", 2894 vdcp->instance, dep->payload.ncookies); 2895 if (rv != 0) { 2896 DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle " 2897 "(mhdl=%p, buf=%p, err=%d)\n", 2898 vdcp->instance, (void *)mhdl, (void *)vaddr, rv); 2899 if (ldep->align_addr) { 2900 kmem_free(ldep->align_addr, 2901 sizeof (caddr_t) * P2ROUNDUP(nbytes, 8)); 2902 ldep->align_addr = NULL; 2903 } 2904 return (EAGAIN); 2905 } 2906 2907 /* 2908 * Get the other cookies (if any). 2909 */ 2910 for (i = 1; i < dep->payload.ncookies; i++) { 2911 rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]); 2912 if (rv != 0) { 2913 (void) ldc_mem_unbind_handle(mhdl); 2914 DMSG(vdcp, 0, "?[%d] Failed to get next cookie " 2915 "(mhdl=%lx cnum=%d), err=%d", 2916 vdcp->instance, mhdl, i, rv); 2917 if (ldep->align_addr) { 2918 kmem_free(ldep->align_addr, 2919 sizeof (caddr_t) * ldep->nbytes); 2920 ldep->align_addr = NULL; 2921 } 2922 return (EAGAIN); 2923 } 2924 } 2925 2926 return (rv); 2927 } 2928 2929 /* 2930 * Interrupt handlers for messages from LDC 2931 */ 2932 2933 /* 2934 * Function: 2935 * vdc_handle_cb() 2936 * 2937 * Description: 2938 * 2939 * Arguments: 2940 * event - Type of event (LDC_EVT_xxx) that triggered the callback 2941 * arg - soft state pointer for this instance of the device driver. 2942 * 2943 * Return Code: 2944 * 0 - Success 2945 */ 2946 static uint_t 2947 vdc_handle_cb(uint64_t event, caddr_t arg) 2948 { 2949 ldc_status_t ldc_state; 2950 int rv = 0; 2951 2952 vdc_t *vdc = (vdc_t *)(void *)arg; 2953 2954 ASSERT(vdc != NULL); 2955 2956 DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num); 2957 2958 /* 2959 * Depending on the type of event that triggered this callback, 2960 * we modify the handshake state or read the data. 2961 * 2962 * NOTE: not done as a switch() as event could be triggered by 2963 * a state change and a read request. Also the ordering of the 2964 * check for the event types is deliberate. 2965 */ 2966 if (event & LDC_EVT_UP) { 2967 DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance); 2968 2969 mutex_enter(&vdc->lock); 2970 2971 /* get LDC state */ 2972 rv = ldc_status(vdc->ldc_handle, &ldc_state); 2973 if (rv != 0) { 2974 DMSG(vdc, 0, "[%d] Couldn't get LDC status %d", 2975 vdc->instance, rv); 2976 return (LDC_SUCCESS); 2977 } 2978 if (vdc->ldc_state != LDC_UP && ldc_state == LDC_UP) { 2979 /* 2980 * Reset the transaction sequence numbers when 2981 * LDC comes up. We then kick off the handshake 2982 * negotiation with the vDisk server. 2983 */ 2984 vdc->seq_num = 1; 2985 vdc->seq_num_reply = 0; 2986 vdc->ldc_state = ldc_state; 2987 cv_signal(&vdc->initwait_cv); 2988 } 2989 2990 mutex_exit(&vdc->lock); 2991 } 2992 2993 if (event & LDC_EVT_READ) { 2994 DMSG(vdc, 0, "[%d] Received LDC_EVT_READ\n", vdc->instance); 2995 mutex_enter(&vdc->read_lock); 2996 cv_signal(&vdc->read_cv); 2997 vdc->read_state = VDC_READ_PENDING; 2998 mutex_exit(&vdc->read_lock); 2999 3000 /* that's all we have to do - no need to handle DOWN/RESET */ 3001 return (LDC_SUCCESS); 3002 } 3003 3004 if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) { 3005 3006 DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance); 3007 3008 mutex_enter(&vdc->lock); 3009 /* 3010 * Need to wake up any readers so they will 3011 * detect that a reset has occurred. 3012 */ 3013 mutex_enter(&vdc->read_lock); 3014 if ((vdc->read_state == VDC_READ_WAITING) || 3015 (vdc->read_state == VDC_READ_RESET)) 3016 cv_signal(&vdc->read_cv); 3017 vdc->read_state = VDC_READ_RESET; 3018 mutex_exit(&vdc->read_lock); 3019 3020 /* wake up any threads waiting for connection to come up */ 3021 if (vdc->state == VDC_STATE_INIT_WAITING) { 3022 vdc->state = VDC_STATE_RESETTING; 3023 cv_signal(&vdc->initwait_cv); 3024 } 3025 3026 mutex_exit(&vdc->lock); 3027 } 3028 3029 if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ)) 3030 DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received", 3031 vdc->instance, event); 3032 3033 return (LDC_SUCCESS); 3034 } 3035 3036 /* 3037 * Function: 3038 * vdc_wait_for_response() 3039 * 3040 * Description: 3041 * Block waiting for a response from the server. If there is 3042 * no data the thread block on the read_cv that is signalled 3043 * by the callback when an EVT_READ occurs. 3044 * 3045 * Arguments: 3046 * vdcp - soft state pointer for this instance of the device driver. 3047 * 3048 * Return Code: 3049 * 0 - Success 3050 */ 3051 static int 3052 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp) 3053 { 3054 size_t nbytes = sizeof (*msgp); 3055 int status; 3056 3057 ASSERT(vdcp != NULL); 3058 3059 DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance); 3060 3061 status = vdc_recv(vdcp, msgp, &nbytes); 3062 DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n", 3063 status, (int)nbytes); 3064 if (status) { 3065 DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n", 3066 vdcp->instance, status); 3067 return (status); 3068 } 3069 3070 if (nbytes < sizeof (vio_msg_tag_t)) { 3071 DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n", 3072 vdcp->instance, sizeof (vio_msg_tag_t), nbytes); 3073 return (ENOMSG); 3074 } 3075 3076 DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance, 3077 msgp->tag.vio_msgtype, 3078 msgp->tag.vio_subtype, 3079 msgp->tag.vio_subtype_env); 3080 3081 /* 3082 * Verify the Session ID of the message 3083 * 3084 * Every message after the Version has been negotiated should 3085 * have the correct session ID set. 3086 */ 3087 if ((msgp->tag.vio_sid != vdcp->session_id) && 3088 (msgp->tag.vio_subtype_env != VIO_VER_INFO)) { 3089 DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, " 3090 "expected 0x%lx [seq num %lx @ %d]", 3091 vdcp->instance, msgp->tag.vio_sid, 3092 vdcp->session_id, 3093 ((vio_dring_msg_t *)msgp)->seq_num, 3094 ((vio_dring_msg_t *)msgp)->start_idx); 3095 return (ENOMSG); 3096 } 3097 return (0); 3098 } 3099 3100 3101 /* 3102 * Function: 3103 * vdc_resubmit_backup_dring() 3104 * 3105 * Description: 3106 * Resubmit each descriptor in the backed up dring to 3107 * vDisk server. The Dring was backed up during connection 3108 * reset. 3109 * 3110 * Arguments: 3111 * vdcp - soft state pointer for this instance of the device driver. 3112 * 3113 * Return Code: 3114 * 0 - Success 3115 */ 3116 static int 3117 vdc_resubmit_backup_dring(vdc_t *vdcp) 3118 { 3119 int count; 3120 int b_idx; 3121 int rv; 3122 int dring_size; 3123 int status; 3124 vio_msg_t vio_msg; 3125 vdc_local_desc_t *curr_ldep; 3126 3127 ASSERT(MUTEX_NOT_HELD(&vdcp->lock)); 3128 ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING); 3129 3130 DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n", 3131 vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail); 3132 3133 /* 3134 * Walk the backup copy of the local descriptor ring and 3135 * resubmit all the outstanding transactions. 3136 */ 3137 b_idx = vdcp->local_dring_backup_tail; 3138 for (count = 0; count < vdcp->local_dring_backup_len; count++) { 3139 3140 curr_ldep = &(vdcp->local_dring_backup[b_idx]); 3141 3142 /* only resubmit oustanding transactions */ 3143 if (!curr_ldep->is_free) { 3144 3145 DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx); 3146 mutex_enter(&vdcp->lock); 3147 rv = vdc_populate_descriptor(vdcp, curr_ldep->operation, 3148 curr_ldep->addr, curr_ldep->nbytes, 3149 curr_ldep->slice, curr_ldep->offset, 3150 curr_ldep->cb_type, curr_ldep->cb_arg, 3151 curr_ldep->dir); 3152 mutex_exit(&vdcp->lock); 3153 if (rv) { 3154 DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n", 3155 vdcp->instance, b_idx); 3156 return (rv); 3157 } 3158 3159 /* Wait for the response message. */ 3160 DMSG(vdcp, 1, "waiting for response to idx=%x\n", 3161 b_idx); 3162 status = vdc_wait_for_response(vdcp, &vio_msg); 3163 if (status) { 3164 DMSG(vdcp, 1, "[%d] wait_for_response " 3165 "returned err=%d\n", vdcp->instance, 3166 status); 3167 return (status); 3168 } 3169 3170 DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx); 3171 status = vdc_process_data_msg(vdcp, &vio_msg); 3172 if (status) { 3173 DMSG(vdcp, 1, "[%d] process_data_msg " 3174 "returned err=%d\n", vdcp->instance, 3175 status); 3176 return (status); 3177 } 3178 } 3179 3180 /* get the next element to submit */ 3181 if (++b_idx >= vdcp->local_dring_backup_len) 3182 b_idx = 0; 3183 } 3184 3185 /* all done - now clear up pending dring copy */ 3186 dring_size = vdcp->local_dring_backup_len * 3187 sizeof (vdcp->local_dring_backup[0]); 3188 3189 (void) kmem_free(vdcp->local_dring_backup, dring_size); 3190 3191 vdcp->local_dring_backup = NULL; 3192 3193 return (0); 3194 } 3195 3196 /* 3197 * Function: 3198 * vdc_backup_local_dring() 3199 * 3200 * Description: 3201 * Backup the current dring in the event of a reset. The Dring 3202 * transactions will be resubmitted to the server when the 3203 * connection is restored. 3204 * 3205 * Arguments: 3206 * vdcp - soft state pointer for this instance of the device driver. 3207 * 3208 * Return Code: 3209 * NONE 3210 */ 3211 static void 3212 vdc_backup_local_dring(vdc_t *vdcp) 3213 { 3214 int dring_size; 3215 3216 ASSERT(vdcp->state == VDC_STATE_RESETTING); 3217 3218 /* 3219 * If the backup dring is stil around, it means 3220 * that the last restore did not complete. However, 3221 * since we never got back into the running state, 3222 * the backup copy we have is still valid. 3223 */ 3224 if (vdcp->local_dring_backup != NULL) { 3225 DMSG(vdcp, 1, "reusing local descriptor ring backup " 3226 "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len, 3227 vdcp->local_dring_backup_tail); 3228 return; 3229 } 3230 3231 DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, " 3232 "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx); 3233 3234 dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]); 3235 3236 vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP); 3237 bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size); 3238 3239 vdcp->local_dring_backup_tail = vdcp->dring_curr_idx; 3240 vdcp->local_dring_backup_len = vdcp->dring_len; 3241 } 3242 3243 /* -------------------------------------------------------------------------- */ 3244 3245 /* 3246 * The following functions process the incoming messages from vds 3247 */ 3248 3249 /* 3250 * Function: 3251 * vdc_process_msg_thread() 3252 * 3253 * Description: 3254 * 3255 * Main VDC message processing thread. Each vDisk instance 3256 * consists of a copy of this thread. This thread triggers 3257 * all the handshakes and data exchange with the server. It 3258 * also handles all channel resets 3259 * 3260 * Arguments: 3261 * vdc - soft state pointer for this instance of the device driver. 3262 * 3263 * Return Code: 3264 * None 3265 */ 3266 static void 3267 vdc_process_msg_thread(vdc_t *vdcp) 3268 { 3269 int status; 3270 3271 mutex_enter(&vdcp->lock); 3272 3273 for (;;) { 3274 3275 #define Q(_s) (vdcp->state == _s) ? #_s : 3276 DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state, 3277 Q(VDC_STATE_INIT) 3278 Q(VDC_STATE_INIT_WAITING) 3279 Q(VDC_STATE_NEGOTIATE) 3280 Q(VDC_STATE_HANDLE_PENDING) 3281 Q(VDC_STATE_RUNNING) 3282 Q(VDC_STATE_RESETTING) 3283 Q(VDC_STATE_DETACH) 3284 "UNKNOWN"); 3285 3286 switch (vdcp->state) { 3287 case VDC_STATE_INIT: 3288 3289 /* Check if have re-initializing repeatedly */ 3290 if (vdcp->hshake_cnt++ > vdc_hshake_retries) { 3291 cmn_err(CE_NOTE, "[%d] disk access failed.\n", 3292 vdcp->instance); 3293 vdcp->state = VDC_STATE_DETACH; 3294 break; 3295 } 3296 3297 /* Bring up connection with vds via LDC */ 3298 status = vdc_start_ldc_connection(vdcp); 3299 switch (status) { 3300 case EINVAL: 3301 DMSG(vdcp, 0, "[%d] Could not start LDC", 3302 vdcp->instance); 3303 vdcp->state = VDC_STATE_DETACH; 3304 break; 3305 case 0: 3306 vdcp->state = VDC_STATE_INIT_WAITING; 3307 break; 3308 default: 3309 vdcp->state = VDC_STATE_INIT_WAITING; 3310 break; 3311 } 3312 break; 3313 3314 case VDC_STATE_INIT_WAITING: 3315 3316 /* 3317 * Let the callback event move us on 3318 * when channel is open to server 3319 */ 3320 while (vdcp->ldc_state != LDC_UP) { 3321 cv_wait(&vdcp->initwait_cv, &vdcp->lock); 3322 if (vdcp->state != VDC_STATE_INIT_WAITING) { 3323 DMSG(vdcp, 0, 3324 "state moved to %d out from under us...\n", 3325 vdcp->state); 3326 3327 break; 3328 } 3329 } 3330 if (vdcp->state == VDC_STATE_INIT_WAITING && 3331 vdcp->ldc_state == LDC_UP) { 3332 vdcp->state = VDC_STATE_NEGOTIATE; 3333 } 3334 break; 3335 3336 case VDC_STATE_NEGOTIATE: 3337 switch (status = vdc_ver_negotiation(vdcp)) { 3338 case 0: 3339 break; 3340 default: 3341 DMSG(vdcp, 0, "ver negotiate failed (%d)..\n", 3342 status); 3343 goto reset; 3344 } 3345 3346 switch (status = vdc_attr_negotiation(vdcp)) { 3347 case 0: 3348 break; 3349 default: 3350 DMSG(vdcp, 0, "attr negotiate failed (%d)..\n", 3351 status); 3352 goto reset; 3353 } 3354 3355 switch (status = vdc_dring_negotiation(vdcp)) { 3356 case 0: 3357 break; 3358 default: 3359 DMSG(vdcp, 0, "dring negotiate failed (%d)..\n", 3360 status); 3361 goto reset; 3362 } 3363 3364 switch (status = vdc_rdx_exchange(vdcp)) { 3365 case 0: 3366 vdcp->state = VDC_STATE_HANDLE_PENDING; 3367 goto done; 3368 default: 3369 DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n", 3370 status); 3371 goto reset; 3372 } 3373 reset: 3374 DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n", 3375 status); 3376 vdcp->state = VDC_STATE_RESETTING; 3377 done: 3378 DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n", 3379 vdcp->state); 3380 break; 3381 3382 case VDC_STATE_HANDLE_PENDING: 3383 3384 mutex_exit(&vdcp->lock); 3385 status = vdc_resubmit_backup_dring(vdcp); 3386 mutex_enter(&vdcp->lock); 3387 3388 if (status) 3389 vdcp->state = VDC_STATE_RESETTING; 3390 else 3391 vdcp->state = VDC_STATE_RUNNING; 3392 3393 break; 3394 3395 /* enter running state */ 3396 case VDC_STATE_RUNNING: 3397 /* 3398 * Signal anyone waiting for the connection 3399 * to come on line. 3400 */ 3401 vdcp->hshake_cnt = 0; 3402 cv_broadcast(&vdcp->running_cv); 3403 mutex_exit(&vdcp->lock); 3404 3405 for (;;) { 3406 vio_msg_t msg; 3407 status = vdc_wait_for_response(vdcp, &msg); 3408 if (status) break; 3409 3410 DMSG(vdcp, 1, "[%d] new pkt(s) available\n", 3411 vdcp->instance); 3412 status = vdc_process_data_msg(vdcp, &msg); 3413 if (status) { 3414 DMSG(vdcp, 1, "[%d] process_data_msg " 3415 "returned err=%d\n", vdcp->instance, 3416 status); 3417 break; 3418 } 3419 3420 } 3421 3422 mutex_enter(&vdcp->lock); 3423 3424 vdcp->state = VDC_STATE_RESETTING; 3425 vdcp->self_reset = B_TRUE; 3426 break; 3427 3428 case VDC_STATE_RESETTING: 3429 DMSG(vdcp, 0, "Initiating channel reset " 3430 "(pending = %d)\n", (int)vdcp->threads_pending); 3431 3432 if (vdcp->self_reset) { 3433 DMSG(vdcp, 0, 3434 "[%d] calling stop_ldc_connection.\n", 3435 vdcp->instance); 3436 status = vdc_stop_ldc_connection(vdcp); 3437 vdcp->self_reset = B_FALSE; 3438 } 3439 3440 /* 3441 * Wait for all threads currently waiting 3442 * for a free dring entry to use. 3443 */ 3444 while (vdcp->threads_pending) { 3445 cv_broadcast(&vdcp->membind_cv); 3446 cv_broadcast(&vdcp->dring_free_cv); 3447 mutex_exit(&vdcp->lock); 3448 /* let them wake up */ 3449 drv_usecwait(vdc_min_timeout_ldc); 3450 mutex_enter(&vdcp->lock); 3451 } 3452 3453 ASSERT(vdcp->threads_pending == 0); 3454 3455 /* Sanity check that no thread is receiving */ 3456 ASSERT(vdcp->read_state != VDC_READ_WAITING); 3457 3458 vdcp->read_state = VDC_READ_IDLE; 3459 3460 vdc_backup_local_dring(vdcp); 3461 3462 /* cleanup the old d-ring */ 3463 vdc_destroy_descriptor_ring(vdcp); 3464 3465 /* go and start again */ 3466 vdcp->state = VDC_STATE_INIT; 3467 3468 break; 3469 3470 case VDC_STATE_DETACH: 3471 DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n", 3472 vdcp->instance); 3473 3474 /* 3475 * Signal anyone waiting for connection 3476 * to come online 3477 */ 3478 cv_broadcast(&vdcp->running_cv); 3479 3480 while (vdcp->sync_op_pending) { 3481 cv_signal(&vdcp->sync_pending_cv); 3482 cv_signal(&vdcp->sync_blocked_cv); 3483 mutex_exit(&vdcp->lock); 3484 drv_usecwait(vdc_min_timeout_ldc); 3485 mutex_enter(&vdcp->lock); 3486 } 3487 3488 mutex_exit(&vdcp->lock); 3489 3490 DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n", 3491 vdcp->instance); 3492 thread_exit(); 3493 break; 3494 } 3495 } 3496 } 3497 3498 3499 /* 3500 * Function: 3501 * vdc_process_data_msg() 3502 * 3503 * Description: 3504 * This function is called by the message processing thread each time 3505 * a message with a msgtype of VIO_TYPE_DATA is received. It will either 3506 * be an ACK or NACK from vds[1] which vdc handles as follows. 3507 * ACK - wake up the waiting thread 3508 * NACK - resend any messages necessary 3509 * 3510 * [1] Although the message format allows it, vds should not send a 3511 * VIO_SUBTYPE_INFO message to vdc asking it to read data; if for 3512 * some bizarre reason it does, vdc will reset the connection. 3513 * 3514 * Arguments: 3515 * vdc - soft state pointer for this instance of the device driver. 3516 * msg - the LDC message sent by vds 3517 * 3518 * Return Code: 3519 * 0 - Success. 3520 * > 0 - error value returned by LDC 3521 */ 3522 static int 3523 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg) 3524 { 3525 int status = 0; 3526 vio_dring_msg_t *dring_msg; 3527 vdc_local_desc_t *ldep = NULL; 3528 int start, end; 3529 int idx; 3530 3531 dring_msg = (vio_dring_msg_t *)msg; 3532 3533 ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA); 3534 ASSERT(vdcp != NULL); 3535 3536 mutex_enter(&vdcp->lock); 3537 3538 /* 3539 * Check to see if the message has bogus data 3540 */ 3541 idx = start = dring_msg->start_idx; 3542 end = dring_msg->end_idx; 3543 if ((start >= vdcp->dring_len) || 3544 (end >= vdcp->dring_len) || (end < -1)) { 3545 DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n", 3546 vdcp->instance, start, end); 3547 mutex_exit(&vdcp->lock); 3548 return (EINVAL); 3549 } 3550 3551 /* 3552 * Verify that the sequence number is what vdc expects. 3553 */ 3554 switch (vdc_verify_seq_num(vdcp, dring_msg)) { 3555 case VDC_SEQ_NUM_TODO: 3556 break; /* keep processing this message */ 3557 case VDC_SEQ_NUM_SKIP: 3558 mutex_exit(&vdcp->lock); 3559 return (0); 3560 case VDC_SEQ_NUM_INVALID: 3561 mutex_exit(&vdcp->lock); 3562 DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance); 3563 return (ENXIO); 3564 } 3565 3566 if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) { 3567 DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance); 3568 VDC_DUMP_DRING_MSG(dring_msg); 3569 mutex_exit(&vdcp->lock); 3570 return (EIO); 3571 3572 } else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) { 3573 mutex_exit(&vdcp->lock); 3574 return (EPROTO); 3575 } 3576 3577 DTRACE_IO2(recv, vio_dring_msg_t, dring_msg, vdc_t *, vdcp); 3578 DMSG(vdcp, 1, ": start %d end %d\n", start, end); 3579 ASSERT(start == end); 3580 3581 ldep = &vdcp->local_dring[idx]; 3582 3583 DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n", 3584 ldep->dep->hdr.dstate, ldep->cb_type); 3585 3586 if (ldep->dep->hdr.dstate == VIO_DESC_DONE) { 3587 struct buf *bufp; 3588 3589 switch (ldep->cb_type) { 3590 case CB_SYNC: 3591 ASSERT(vdcp->sync_op_pending); 3592 3593 status = vdc_depopulate_descriptor(vdcp, idx); 3594 vdcp->sync_op_status = status; 3595 vdcp->sync_op_pending = B_FALSE; 3596 cv_signal(&vdcp->sync_pending_cv); 3597 break; 3598 3599 case CB_STRATEGY: 3600 bufp = ldep->cb_arg; 3601 ASSERT(bufp != NULL); 3602 bufp->b_resid = 3603 bufp->b_bcount - ldep->dep->payload.nbytes; 3604 status = ldep->dep->payload.status; /* Future:ntoh */ 3605 if (status != 0) { 3606 DMSG(vdcp, 1, "strategy status=%d\n", status); 3607 bioerror(bufp, status); 3608 } 3609 status = vdc_depopulate_descriptor(vdcp, idx); 3610 biodone(bufp); 3611 3612 DMSG(vdcp, 1, 3613 "strategy complete req=%ld bytes resp=%ld bytes\n", 3614 bufp->b_bcount, ldep->dep->payload.nbytes); 3615 break; 3616 3617 default: 3618 ASSERT(0); 3619 } 3620 } 3621 3622 /* let the arrival signal propogate */ 3623 mutex_exit(&vdcp->lock); 3624 3625 /* probe gives the count of how many entries were processed */ 3626 DTRACE_IO2(processed, int, 1, vdc_t *, vdcp); 3627 3628 return (0); 3629 } 3630 3631 /* 3632 * Function: 3633 * vdc_process_err_msg() 3634 * 3635 * NOTE: No error messages are used as part of the vDisk protocol 3636 */ 3637 static int 3638 vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg) 3639 { 3640 _NOTE(ARGUNUSED(vdc)) 3641 _NOTE(ARGUNUSED(msg)) 3642 3643 ASSERT(msg.tag.vio_msgtype == VIO_TYPE_ERR); 3644 DMSG(vdc, 1, "[%d] Got an ERR msg", vdc->instance); 3645 3646 return (ENOTSUP); 3647 } 3648 3649 /* 3650 * Function: 3651 * vdc_handle_ver_msg() 3652 * 3653 * Description: 3654 * 3655 * Arguments: 3656 * vdc - soft state pointer for this instance of the device driver. 3657 * ver_msg - LDC message sent by vDisk server 3658 * 3659 * Return Code: 3660 * 0 - Success 3661 */ 3662 static int 3663 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg) 3664 { 3665 int status = 0; 3666 3667 ASSERT(vdc != NULL); 3668 ASSERT(mutex_owned(&vdc->lock)); 3669 3670 if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) { 3671 return (EPROTO); 3672 } 3673 3674 if (ver_msg->dev_class != VDEV_DISK_SERVER) { 3675 return (EINVAL); 3676 } 3677 3678 switch (ver_msg->tag.vio_subtype) { 3679 case VIO_SUBTYPE_ACK: 3680 /* 3681 * We check to see if the version returned is indeed supported 3682 * (The server may have also adjusted the minor number downwards 3683 * and if so 'ver_msg' will contain the actual version agreed) 3684 */ 3685 if (vdc_is_supported_version(ver_msg)) { 3686 vdc->ver.major = ver_msg->ver_major; 3687 vdc->ver.minor = ver_msg->ver_minor; 3688 ASSERT(vdc->ver.major > 0); 3689 } else { 3690 status = EPROTO; 3691 } 3692 break; 3693 3694 case VIO_SUBTYPE_NACK: 3695 /* 3696 * call vdc_is_supported_version() which will return the next 3697 * supported version (if any) in 'ver_msg' 3698 */ 3699 (void) vdc_is_supported_version(ver_msg); 3700 if (ver_msg->ver_major > 0) { 3701 size_t len = sizeof (*ver_msg); 3702 3703 ASSERT(vdc->ver.major > 0); 3704 3705 /* reset the necessary fields and resend */ 3706 ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO; 3707 ver_msg->dev_class = VDEV_DISK; 3708 3709 status = vdc_send(vdc, (caddr_t)ver_msg, &len); 3710 DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n", 3711 vdc->instance, status); 3712 if (len != sizeof (*ver_msg)) 3713 status = EBADMSG; 3714 } else { 3715 DMSG(vdc, 0, "[%d] No common version with " 3716 "vDisk server", vdc->instance); 3717 status = ENOTSUP; 3718 } 3719 3720 break; 3721 case VIO_SUBTYPE_INFO: 3722 /* 3723 * Handle the case where vds starts handshake 3724 * (for now only vdc is the instigatior) 3725 */ 3726 status = ENOTSUP; 3727 break; 3728 3729 default: 3730 status = EINVAL; 3731 break; 3732 } 3733 3734 return (status); 3735 } 3736 3737 /* 3738 * Function: 3739 * vdc_handle_attr_msg() 3740 * 3741 * Description: 3742 * 3743 * Arguments: 3744 * vdc - soft state pointer for this instance of the device driver. 3745 * attr_msg - LDC message sent by vDisk server 3746 * 3747 * Return Code: 3748 * 0 - Success 3749 */ 3750 static int 3751 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg) 3752 { 3753 int status = 0; 3754 3755 ASSERT(vdc != NULL); 3756 ASSERT(mutex_owned(&vdc->lock)); 3757 3758 if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) { 3759 return (EPROTO); 3760 } 3761 3762 switch (attr_msg->tag.vio_subtype) { 3763 case VIO_SUBTYPE_ACK: 3764 /* 3765 * We now verify the attributes sent by vds. 3766 */ 3767 vdc->vdisk_size = attr_msg->vdisk_size; 3768 vdc->vdisk_type = attr_msg->vdisk_type; 3769 3770 DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n", 3771 vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz); 3772 DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n", 3773 vdc->instance, vdc->block_size, 3774 attr_msg->vdisk_block_size); 3775 3776 /* 3777 * We don't know at compile time what the vDisk server will 3778 * think are good values but we apply an large (arbitrary) 3779 * upper bound to prevent memory exhaustion in vdc if it was 3780 * allocating a DRing based of huge values sent by the server. 3781 * We probably will never exceed this except if the message 3782 * was garbage. 3783 */ 3784 if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <= 3785 (PAGESIZE * DEV_BSIZE)) { 3786 vdc->max_xfer_sz = attr_msg->max_xfer_sz; 3787 vdc->block_size = attr_msg->vdisk_block_size; 3788 } else { 3789 DMSG(vdc, 0, "[%d] vds block transfer size too big;" 3790 " using max supported by vdc", vdc->instance); 3791 } 3792 3793 if ((attr_msg->xfer_mode != VIO_DRING_MODE) || 3794 (attr_msg->vdisk_size > INT64_MAX) || 3795 (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) { 3796 DMSG(vdc, 0, "[%d] Invalid attributes from vds", 3797 vdc->instance); 3798 status = EINVAL; 3799 break; 3800 } 3801 3802 break; 3803 3804 case VIO_SUBTYPE_NACK: 3805 /* 3806 * vds could not handle the attributes we sent so we 3807 * stop negotiating. 3808 */ 3809 status = EPROTO; 3810 break; 3811 3812 case VIO_SUBTYPE_INFO: 3813 /* 3814 * Handle the case where vds starts the handshake 3815 * (for now; vdc is the only supported instigatior) 3816 */ 3817 status = ENOTSUP; 3818 break; 3819 3820 default: 3821 status = ENOTSUP; 3822 break; 3823 } 3824 3825 return (status); 3826 } 3827 3828 /* 3829 * Function: 3830 * vdc_handle_dring_reg_msg() 3831 * 3832 * Description: 3833 * 3834 * Arguments: 3835 * vdc - soft state pointer for this instance of the driver. 3836 * dring_msg - LDC message sent by vDisk server 3837 * 3838 * Return Code: 3839 * 0 - Success 3840 */ 3841 static int 3842 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg) 3843 { 3844 int status = 0; 3845 3846 ASSERT(vdc != NULL); 3847 ASSERT(mutex_owned(&vdc->lock)); 3848 3849 if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) { 3850 return (EPROTO); 3851 } 3852 3853 switch (dring_msg->tag.vio_subtype) { 3854 case VIO_SUBTYPE_ACK: 3855 /* save the received dring_ident */ 3856 vdc->dring_ident = dring_msg->dring_ident; 3857 DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n", 3858 vdc->instance, vdc->dring_ident); 3859 break; 3860 3861 case VIO_SUBTYPE_NACK: 3862 /* 3863 * vds could not handle the DRing info we sent so we 3864 * stop negotiating. 3865 */ 3866 DMSG(vdc, 0, "[%d] server could not register DRing\n", 3867 vdc->instance); 3868 status = EPROTO; 3869 break; 3870 3871 case VIO_SUBTYPE_INFO: 3872 /* 3873 * Handle the case where vds starts handshake 3874 * (for now only vdc is the instigatior) 3875 */ 3876 status = ENOTSUP; 3877 break; 3878 default: 3879 status = ENOTSUP; 3880 } 3881 3882 return (status); 3883 } 3884 3885 /* 3886 * Function: 3887 * vdc_verify_seq_num() 3888 * 3889 * Description: 3890 * This functions verifies that the sequence number sent back by the vDisk 3891 * server with the latest message is what is expected (i.e. it is greater 3892 * than the last seq num sent by the vDisk server and less than or equal 3893 * to the last seq num generated by vdc). 3894 * 3895 * It then checks the request ID to see if any requests need processing 3896 * in the DRing. 3897 * 3898 * Arguments: 3899 * vdc - soft state pointer for this instance of the driver. 3900 * dring_msg - pointer to the LDC message sent by vds 3901 * 3902 * Return Code: 3903 * VDC_SEQ_NUM_TODO - Message needs to be processed 3904 * VDC_SEQ_NUM_SKIP - Message has already been processed 3905 * VDC_SEQ_NUM_INVALID - The seq numbers are so out of sync, 3906 * vdc cannot deal with them 3907 */ 3908 static int 3909 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg) 3910 { 3911 ASSERT(vdc != NULL); 3912 ASSERT(dring_msg != NULL); 3913 ASSERT(mutex_owned(&vdc->lock)); 3914 3915 /* 3916 * Check to see if the messages were responded to in the correct 3917 * order by vds. 3918 */ 3919 if ((dring_msg->seq_num <= vdc->seq_num_reply) || 3920 (dring_msg->seq_num > vdc->seq_num)) { 3921 DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: " 3922 "%lu > expected <= %lu (last proc req %lu sent %lu)\n", 3923 vdc->instance, dring_msg->seq_num, 3924 vdc->seq_num_reply, vdc->seq_num, 3925 vdc->req_id_proc, vdc->req_id); 3926 return (VDC_SEQ_NUM_INVALID); 3927 } 3928 vdc->seq_num_reply = dring_msg->seq_num; 3929 3930 if (vdc->req_id_proc < vdc->req_id) 3931 return (VDC_SEQ_NUM_TODO); 3932 else 3933 return (VDC_SEQ_NUM_SKIP); 3934 } 3935 3936 3937 /* 3938 * Function: 3939 * vdc_is_supported_version() 3940 * 3941 * Description: 3942 * This routine checks if the major/minor version numbers specified in 3943 * 'ver_msg' are supported. If not it finds the next version that is 3944 * in the supported version list 'vdc_version[]' and sets the fields in 3945 * 'ver_msg' to those values 3946 * 3947 * Arguments: 3948 * ver_msg - LDC message sent by vDisk server 3949 * 3950 * Return Code: 3951 * B_TRUE - Success 3952 * B_FALSE - Version not supported 3953 */ 3954 static boolean_t 3955 vdc_is_supported_version(vio_ver_msg_t *ver_msg) 3956 { 3957 int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]); 3958 3959 for (int i = 0; i < vdc_num_versions; i++) { 3960 ASSERT(vdc_version[i].major > 0); 3961 ASSERT((i == 0) || 3962 (vdc_version[i].major < vdc_version[i-1].major)); 3963 3964 /* 3965 * If the major versions match, adjust the minor version, if 3966 * necessary, down to the highest value supported by this 3967 * client. The server should support all minor versions lower 3968 * than the value it sent 3969 */ 3970 if (ver_msg->ver_major == vdc_version[i].major) { 3971 if (ver_msg->ver_minor > vdc_version[i].minor) { 3972 DMSGX(0, 3973 "Adjusting minor version from %u to %u", 3974 ver_msg->ver_minor, vdc_version[i].minor); 3975 ver_msg->ver_minor = vdc_version[i].minor; 3976 } 3977 return (B_TRUE); 3978 } 3979 3980 /* 3981 * If the message contains a higher major version number, set 3982 * the message's major/minor versions to the current values 3983 * and return false, so this message will get resent with 3984 * these values, and the server will potentially try again 3985 * with the same or a lower version 3986 */ 3987 if (ver_msg->ver_major > vdc_version[i].major) { 3988 ver_msg->ver_major = vdc_version[i].major; 3989 ver_msg->ver_minor = vdc_version[i].minor; 3990 DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n", 3991 ver_msg->ver_major, ver_msg->ver_minor); 3992 3993 return (B_FALSE); 3994 } 3995 3996 /* 3997 * Otherwise, the message's major version is less than the 3998 * current major version, so continue the loop to the next 3999 * (lower) supported version 4000 */ 4001 } 4002 4003 /* 4004 * No common version was found; "ground" the version pair in the 4005 * message to terminate negotiation 4006 */ 4007 ver_msg->ver_major = 0; 4008 ver_msg->ver_minor = 0; 4009 4010 return (B_FALSE); 4011 } 4012 /* -------------------------------------------------------------------------- */ 4013 4014 /* 4015 * DKIO(7) support 4016 */ 4017 4018 typedef struct vdc_dk_arg { 4019 struct dk_callback dkc; 4020 int mode; 4021 dev_t dev; 4022 vdc_t *vdc; 4023 } vdc_dk_arg_t; 4024 4025 /* 4026 * Function: 4027 * vdc_dkio_flush_cb() 4028 * 4029 * Description: 4030 * This routine is a callback for DKIOCFLUSHWRITECACHE which can be called 4031 * by kernel code. 4032 * 4033 * Arguments: 4034 * arg - a pointer to a vdc_dk_arg_t structure. 4035 */ 4036 void 4037 vdc_dkio_flush_cb(void *arg) 4038 { 4039 struct vdc_dk_arg *dk_arg = (struct vdc_dk_arg *)arg; 4040 struct dk_callback *dkc = NULL; 4041 vdc_t *vdc = NULL; 4042 int rv; 4043 4044 if (dk_arg == NULL) { 4045 cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n"); 4046 return; 4047 } 4048 dkc = &dk_arg->dkc; 4049 vdc = dk_arg->vdc; 4050 ASSERT(vdc != NULL); 4051 4052 rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0, 4053 VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir); 4054 if (rv != 0) { 4055 DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n", 4056 vdc->instance, rv, 4057 ddi_model_convert_from(dk_arg->mode & FMODELS)); 4058 } 4059 4060 /* 4061 * Trigger the call back to notify the caller the the ioctl call has 4062 * been completed. 4063 */ 4064 if ((dk_arg->mode & FKIOCTL) && 4065 (dkc != NULL) && 4066 (dkc->dkc_callback != NULL)) { 4067 ASSERT(dkc->dkc_cookie != NULL); 4068 (*dkc->dkc_callback)(dkc->dkc_cookie, rv); 4069 } 4070 4071 /* Indicate that one less DKIO write flush is outstanding */ 4072 mutex_enter(&vdc->lock); 4073 vdc->dkio_flush_pending--; 4074 ASSERT(vdc->dkio_flush_pending >= 0); 4075 mutex_exit(&vdc->lock); 4076 4077 /* free the mem that was allocated when the callback was dispatched */ 4078 kmem_free(arg, sizeof (vdc_dk_arg_t)); 4079 } 4080 4081 /* 4082 * This structure is used in the DKIO(7I) array below. 4083 */ 4084 typedef struct vdc_dk_ioctl { 4085 uint8_t op; /* VD_OP_XXX value */ 4086 int cmd; /* Solaris ioctl operation number */ 4087 size_t nbytes; /* size of structure to be copied */ 4088 4089 /* function to convert between vDisk and Solaris structure formats */ 4090 int (*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg, 4091 int mode, int dir); 4092 } vdc_dk_ioctl_t; 4093 4094 /* 4095 * Subset of DKIO(7I) operations currently supported 4096 */ 4097 static vdc_dk_ioctl_t dk_ioctl[] = { 4098 {VD_OP_FLUSH, DKIOCFLUSHWRITECACHE, sizeof (int), 4099 vdc_null_copy_func}, 4100 {VD_OP_GET_WCE, DKIOCGETWCE, sizeof (int), 4101 vdc_get_wce_convert}, 4102 {VD_OP_SET_WCE, DKIOCSETWCE, sizeof (int), 4103 vdc_set_wce_convert}, 4104 {VD_OP_GET_VTOC, DKIOCGVTOC, sizeof (vd_vtoc_t), 4105 vdc_get_vtoc_convert}, 4106 {VD_OP_SET_VTOC, DKIOCSVTOC, sizeof (vd_vtoc_t), 4107 vdc_set_vtoc_convert}, 4108 {VD_OP_GET_DISKGEOM, DKIOCGGEOM, sizeof (vd_geom_t), 4109 vdc_get_geom_convert}, 4110 {VD_OP_GET_DISKGEOM, DKIOCG_PHYGEOM, sizeof (vd_geom_t), 4111 vdc_get_geom_convert}, 4112 {VD_OP_GET_DISKGEOM, DKIOCG_VIRTGEOM, sizeof (vd_geom_t), 4113 vdc_get_geom_convert}, 4114 {VD_OP_SET_DISKGEOM, DKIOCSGEOM, sizeof (vd_geom_t), 4115 vdc_set_geom_convert}, 4116 {VD_OP_GET_EFI, DKIOCGETEFI, 0, 4117 vdc_get_efi_convert}, 4118 {VD_OP_SET_EFI, DKIOCSETEFI, 0, 4119 vdc_set_efi_convert}, 4120 4121 /* 4122 * These particular ioctls are not sent to the server - vdc fakes up 4123 * the necessary info. 4124 */ 4125 {0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func}, 4126 {0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func}, 4127 {0, USCSICMD, sizeof (struct uscsi_cmd), vdc_null_copy_func}, 4128 {0, DKIOCREMOVABLE, 0, vdc_null_copy_func}, 4129 {0, CDROMREADOFFSET, 0, vdc_null_copy_func} 4130 }; 4131 4132 /* 4133 * Function: 4134 * vd_process_ioctl() 4135 * 4136 * Description: 4137 * This routine processes disk specific ioctl calls 4138 * 4139 * Arguments: 4140 * dev - the device number 4141 * cmd - the operation [dkio(7I)] to be processed 4142 * arg - pointer to user provided structure 4143 * (contains data to be set or reference parameter for get) 4144 * mode - bit flag, indicating open settings, 32/64 bit type, etc 4145 * 4146 * Return Code: 4147 * 0 4148 * EFAULT 4149 * ENXIO 4150 * EIO 4151 * ENOTSUP 4152 */ 4153 static int 4154 vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode) 4155 { 4156 int instance = VDCUNIT(dev); 4157 vdc_t *vdc = NULL; 4158 int rv = -1; 4159 int idx = 0; /* index into dk_ioctl[] */ 4160 size_t len = 0; /* #bytes to send to vds */ 4161 size_t alloc_len = 0; /* #bytes to allocate mem for */ 4162 caddr_t mem_p = NULL; 4163 size_t nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0])); 4164 struct vtoc vtoc_saved; 4165 vdc_dk_ioctl_t *iop; 4166 4167 vdc = ddi_get_soft_state(vdc_state, instance); 4168 if (vdc == NULL) { 4169 cmn_err(CE_NOTE, "![%d] Could not get soft state structure", 4170 instance); 4171 return (ENXIO); 4172 } 4173 4174 DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n", 4175 instance, cmd, dev, ddi_model_convert_from(mode & FMODELS)); 4176 4177 /* 4178 * Validate the ioctl operation to be performed. 4179 * 4180 * If we have looped through the array without finding a match then we 4181 * don't support this ioctl. 4182 */ 4183 for (idx = 0; idx < nioctls; idx++) { 4184 if (cmd == dk_ioctl[idx].cmd) 4185 break; 4186 } 4187 4188 if (idx >= nioctls) { 4189 DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n", 4190 vdc->instance, cmd); 4191 return (ENOTSUP); 4192 } 4193 4194 iop = &(dk_ioctl[idx]); 4195 4196 if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) { 4197 /* size is not fixed for EFI ioctls, it depends on ioctl arg */ 4198 dk_efi_t dk_efi; 4199 4200 rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode); 4201 if (rv != 0) 4202 return (EFAULT); 4203 4204 len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length; 4205 } else { 4206 len = iop->nbytes; 4207 } 4208 4209 /* 4210 * Deal with the ioctls which the server does not provide. vdc can 4211 * fake these up and return immediately 4212 */ 4213 switch (cmd) { 4214 case CDROMREADOFFSET: 4215 case DKIOCREMOVABLE: 4216 case USCSICMD: 4217 return (ENOTTY); 4218 4219 case DKIOCINFO: 4220 { 4221 struct dk_cinfo cinfo; 4222 if (vdc->cinfo == NULL) 4223 return (ENXIO); 4224 4225 bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo)); 4226 cinfo.dki_partition = VDCPART(dev); 4227 4228 rv = ddi_copyout(&cinfo, (void *)arg, 4229 sizeof (struct dk_cinfo), mode); 4230 if (rv != 0) 4231 return (EFAULT); 4232 4233 return (0); 4234 } 4235 4236 case DKIOCGMEDIAINFO: 4237 { 4238 if (vdc->minfo == NULL) 4239 return (ENXIO); 4240 4241 rv = ddi_copyout(vdc->minfo, (void *)arg, 4242 sizeof (struct dk_minfo), mode); 4243 if (rv != 0) 4244 return (EFAULT); 4245 4246 return (0); 4247 } 4248 4249 case DKIOCFLUSHWRITECACHE: 4250 { 4251 struct dk_callback *dkc = (struct dk_callback *)arg; 4252 vdc_dk_arg_t *dkarg = NULL; 4253 4254 DMSG(vdc, 1, "[%d] Flush W$: mode %x\n", 4255 instance, mode); 4256 4257 /* 4258 * If the backing device is not a 'real' disk then the 4259 * W$ operation request to the vDisk server will fail 4260 * so we might as well save the cycles and return now. 4261 */ 4262 if (vdc->vdisk_type != VD_DISK_TYPE_DISK) 4263 return (ENOTTY); 4264 4265 /* 4266 * If arg is NULL, then there is no callback function 4267 * registered and the call operates synchronously; we 4268 * break and continue with the rest of the function and 4269 * wait for vds to return (i.e. after the request to 4270 * vds returns successfully, all writes completed prior 4271 * to the ioctl will have been flushed from the disk 4272 * write cache to persistent media. 4273 * 4274 * If a callback function is registered, we dispatch 4275 * the request on a task queue and return immediately. 4276 * The callback will deal with informing the calling 4277 * thread that the flush request is completed. 4278 */ 4279 if (dkc == NULL) 4280 break; 4281 4282 dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP); 4283 4284 dkarg->mode = mode; 4285 dkarg->dev = dev; 4286 bcopy(dkc, &dkarg->dkc, sizeof (*dkc)); 4287 4288 mutex_enter(&vdc->lock); 4289 vdc->dkio_flush_pending++; 4290 dkarg->vdc = vdc; 4291 mutex_exit(&vdc->lock); 4292 4293 /* put the request on a task queue */ 4294 rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb, 4295 (void *)dkarg, DDI_SLEEP); 4296 if (rv == NULL) { 4297 /* clean up if dispatch fails */ 4298 mutex_enter(&vdc->lock); 4299 vdc->dkio_flush_pending--; 4300 kmem_free(dkarg, sizeof (vdc_dk_arg_t)); 4301 } 4302 4303 return (rv == NULL ? ENOMEM : 0); 4304 } 4305 } 4306 4307 /* catch programming error in vdc - should be a VD_OP_XXX ioctl */ 4308 ASSERT(iop->op != 0); 4309 4310 /* LDC requires that the memory being mapped is 8-byte aligned */ 4311 alloc_len = P2ROUNDUP(len, sizeof (uint64_t)); 4312 DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n", 4313 instance, len, alloc_len); 4314 4315 ASSERT(alloc_len != 0); /* sanity check */ 4316 mem_p = kmem_zalloc(alloc_len, KM_SLEEP); 4317 4318 if (cmd == DKIOCSVTOC) { 4319 /* 4320 * Save a copy of the current VTOC so that we can roll back 4321 * if the setting of the new VTOC fails. 4322 */ 4323 bcopy(vdc->vtoc, &vtoc_saved, sizeof (struct vtoc)); 4324 } 4325 4326 /* 4327 * Call the conversion function for this ioctl whhich if necessary 4328 * converts from the Solaris format to the format ARC'ed 4329 * as part of the vDisk protocol (FWARC 2006/195) 4330 */ 4331 ASSERT(iop->convert != NULL); 4332 rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN); 4333 if (rv != 0) { 4334 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n", 4335 instance, rv, cmd); 4336 if (mem_p != NULL) 4337 kmem_free(mem_p, alloc_len); 4338 return (rv); 4339 } 4340 4341 /* 4342 * send request to vds to service the ioctl. 4343 */ 4344 rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len, 4345 VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode, 4346 VIO_both_dir); 4347 4348 if (rv != 0) { 4349 /* 4350 * This is not necessarily an error. The ioctl could 4351 * be returning a value such as ENOTTY to indicate 4352 * that the ioctl is not applicable. 4353 */ 4354 DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n", 4355 instance, rv, cmd); 4356 if (mem_p != NULL) 4357 kmem_free(mem_p, alloc_len); 4358 4359 if (cmd == DKIOCSVTOC) { 4360 /* update of the VTOC has failed, roll back */ 4361 bcopy(&vtoc_saved, vdc->vtoc, sizeof (struct vtoc)); 4362 } 4363 4364 return (rv); 4365 } 4366 4367 if (cmd == DKIOCSVTOC) { 4368 /* 4369 * The VTOC has been changed. We need to update the device 4370 * nodes to handle the case where an EFI label has been 4371 * changed to a VTOC label. We also try and update the device 4372 * node properties. Failing to set the properties should 4373 * not cause an error to be return the caller though. 4374 */ 4375 vdc->vdisk_label = VD_DISK_LABEL_VTOC; 4376 (void) vdc_create_device_nodes_vtoc(vdc); 4377 4378 if (vdc_create_device_nodes_props(vdc)) { 4379 DMSG(vdc, 0, "![%d] Failed to update device nodes" 4380 " properties", vdc->instance); 4381 } 4382 4383 } else if (cmd == DKIOCSETEFI) { 4384 /* 4385 * The EFI has been changed. We need to update the device 4386 * nodes to handle the case where a VTOC label has been 4387 * changed to an EFI label. We also try and update the device 4388 * node properties. Failing to set the properties should 4389 * not cause an error to be return the caller though. 4390 */ 4391 struct dk_gpt *efi; 4392 size_t efi_len; 4393 4394 vdc->vdisk_label = VD_DISK_LABEL_EFI; 4395 (void) vdc_create_device_nodes_efi(vdc); 4396 4397 rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len); 4398 4399 if (rv == 0) { 4400 vdc_store_efi(vdc, efi); 4401 rv = vdc_create_device_nodes_props(vdc); 4402 vd_efi_free(efi, efi_len); 4403 } 4404 4405 if (rv) { 4406 DMSG(vdc, 0, "![%d] Failed to update device nodes" 4407 " properties", vdc->instance); 4408 } 4409 } 4410 4411 /* 4412 * Call the conversion function (if it exists) for this ioctl 4413 * which converts from the format ARC'ed as part of the vDisk 4414 * protocol (FWARC 2006/195) back to a format understood by 4415 * the rest of Solaris. 4416 */ 4417 rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT); 4418 if (rv != 0) { 4419 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n", 4420 instance, rv, cmd); 4421 if (mem_p != NULL) 4422 kmem_free(mem_p, alloc_len); 4423 return (rv); 4424 } 4425 4426 if (mem_p != NULL) 4427 kmem_free(mem_p, alloc_len); 4428 4429 return (rv); 4430 } 4431 4432 /* 4433 * Function: 4434 * 4435 * Description: 4436 * This is an empty conversion function used by ioctl calls which 4437 * do not need to convert the data being passed in/out to userland 4438 */ 4439 static int 4440 vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir) 4441 { 4442 _NOTE(ARGUNUSED(vdc)) 4443 _NOTE(ARGUNUSED(from)) 4444 _NOTE(ARGUNUSED(to)) 4445 _NOTE(ARGUNUSED(mode)) 4446 _NOTE(ARGUNUSED(dir)) 4447 4448 return (0); 4449 } 4450 4451 static int 4452 vdc_get_wce_convert(vdc_t *vdc, void *from, void *to, 4453 int mode, int dir) 4454 { 4455 _NOTE(ARGUNUSED(vdc)) 4456 4457 if (dir == VD_COPYIN) 4458 return (0); /* nothing to do */ 4459 4460 if (ddi_copyout(from, to, sizeof (int), mode) != 0) 4461 return (EFAULT); 4462 4463 return (0); 4464 } 4465 4466 static int 4467 vdc_set_wce_convert(vdc_t *vdc, void *from, void *to, 4468 int mode, int dir) 4469 { 4470 _NOTE(ARGUNUSED(vdc)) 4471 4472 if (dir == VD_COPYOUT) 4473 return (0); /* nothing to do */ 4474 4475 if (ddi_copyin(from, to, sizeof (int), mode) != 0) 4476 return (EFAULT); 4477 4478 return (0); 4479 } 4480 4481 /* 4482 * Function: 4483 * vdc_get_vtoc_convert() 4484 * 4485 * Description: 4486 * This routine performs the necessary convertions from the DKIOCGVTOC 4487 * Solaris structure to the format defined in FWARC 2006/195. 4488 * 4489 * In the struct vtoc definition, the timestamp field is marked as not 4490 * supported so it is not part of vDisk protocol (FWARC 2006/195). 4491 * However SVM uses that field to check it can write into the VTOC, 4492 * so we fake up the info of that field. 4493 * 4494 * Arguments: 4495 * vdc - the vDisk client 4496 * from - the buffer containing the data to be copied from 4497 * to - the buffer to be copied to 4498 * mode - flags passed to ioctl() call 4499 * dir - the "direction" of the copy - VD_COPYIN or VD_COPYOUT 4500 * 4501 * Return Code: 4502 * 0 - Success 4503 * ENXIO - incorrect buffer passed in. 4504 * EFAULT - ddi_copyout routine encountered an error. 4505 */ 4506 static int 4507 vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 4508 { 4509 int i; 4510 void *tmp_mem = NULL; 4511 void *tmp_memp; 4512 struct vtoc vt; 4513 struct vtoc32 vt32; 4514 int copy_len = 0; 4515 int rv = 0; 4516 4517 if (dir != VD_COPYOUT) 4518 return (0); /* nothing to do */ 4519 4520 if ((from == NULL) || (to == NULL)) 4521 return (ENXIO); 4522 4523 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) 4524 copy_len = sizeof (struct vtoc32); 4525 else 4526 copy_len = sizeof (struct vtoc); 4527 4528 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 4529 4530 VD_VTOC2VTOC((vd_vtoc_t *)from, &vt); 4531 4532 /* fake the VTOC timestamp field */ 4533 for (i = 0; i < V_NUMPAR; i++) { 4534 vt.timestamp[i] = vdc->vtoc->timestamp[i]; 4535 } 4536 4537 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 4538 vtoctovtoc32(vt, vt32); 4539 tmp_memp = &vt32; 4540 } else { 4541 tmp_memp = &vt; 4542 } 4543 rv = ddi_copyout(tmp_memp, to, copy_len, mode); 4544 if (rv != 0) 4545 rv = EFAULT; 4546 4547 kmem_free(tmp_mem, copy_len); 4548 return (rv); 4549 } 4550 4551 /* 4552 * Function: 4553 * vdc_set_vtoc_convert() 4554 * 4555 * Description: 4556 * This routine performs the necessary convertions from the DKIOCSVTOC 4557 * Solaris structure to the format defined in FWARC 2006/195. 4558 * 4559 * Arguments: 4560 * vdc - the vDisk client 4561 * from - Buffer with data 4562 * to - Buffer where data is to be copied to 4563 * mode - flags passed to ioctl 4564 * dir - direction of copy (in or out) 4565 * 4566 * Return Code: 4567 * 0 - Success 4568 * ENXIO - Invalid buffer passed in 4569 * EFAULT - ddi_copyin of data failed 4570 */ 4571 static int 4572 vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 4573 { 4574 void *tmp_mem = NULL; 4575 struct vtoc vt; 4576 struct vtoc *vtp = &vt; 4577 vd_vtoc_t vtvd; 4578 int copy_len = 0; 4579 int rv = 0; 4580 4581 if (dir != VD_COPYIN) 4582 return (0); /* nothing to do */ 4583 4584 if ((from == NULL) || (to == NULL)) 4585 return (ENXIO); 4586 4587 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) 4588 copy_len = sizeof (struct vtoc32); 4589 else 4590 copy_len = sizeof (struct vtoc); 4591 4592 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 4593 4594 rv = ddi_copyin(from, tmp_mem, copy_len, mode); 4595 if (rv != 0) { 4596 kmem_free(tmp_mem, copy_len); 4597 return (EFAULT); 4598 } 4599 4600 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 4601 vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt); 4602 } else { 4603 vtp = tmp_mem; 4604 } 4605 4606 /* 4607 * The VTOC is being changed, then vdc needs to update the copy 4608 * it saved in the soft state structure. 4609 */ 4610 bcopy(vtp, vdc->vtoc, sizeof (struct vtoc)); 4611 4612 VTOC2VD_VTOC(vtp, &vtvd); 4613 bcopy(&vtvd, to, sizeof (vd_vtoc_t)); 4614 kmem_free(tmp_mem, copy_len); 4615 4616 return (0); 4617 } 4618 4619 /* 4620 * Function: 4621 * vdc_get_geom_convert() 4622 * 4623 * Description: 4624 * This routine performs the necessary convertions from the DKIOCGGEOM, 4625 * DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format 4626 * defined in FWARC 2006/195 4627 * 4628 * Arguments: 4629 * vdc - the vDisk client 4630 * from - Buffer with data 4631 * to - Buffer where data is to be copied to 4632 * mode - flags passed to ioctl 4633 * dir - direction of copy (in or out) 4634 * 4635 * Return Code: 4636 * 0 - Success 4637 * ENXIO - Invalid buffer passed in 4638 * EFAULT - ddi_copyout of data failed 4639 */ 4640 static int 4641 vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 4642 { 4643 _NOTE(ARGUNUSED(vdc)) 4644 4645 struct dk_geom geom; 4646 int copy_len = sizeof (struct dk_geom); 4647 int rv = 0; 4648 4649 if (dir != VD_COPYOUT) 4650 return (0); /* nothing to do */ 4651 4652 if ((from == NULL) || (to == NULL)) 4653 return (ENXIO); 4654 4655 VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom); 4656 rv = ddi_copyout(&geom, to, copy_len, mode); 4657 if (rv != 0) 4658 rv = EFAULT; 4659 4660 return (rv); 4661 } 4662 4663 /* 4664 * Function: 4665 * vdc_set_geom_convert() 4666 * 4667 * Description: 4668 * This routine performs the necessary convertions from the DKIOCSGEOM 4669 * Solaris structure to the format defined in FWARC 2006/195. 4670 * 4671 * Arguments: 4672 * vdc - the vDisk client 4673 * from - Buffer with data 4674 * to - Buffer where data is to be copied to 4675 * mode - flags passed to ioctl 4676 * dir - direction of copy (in or out) 4677 * 4678 * Return Code: 4679 * 0 - Success 4680 * ENXIO - Invalid buffer passed in 4681 * EFAULT - ddi_copyin of data failed 4682 */ 4683 static int 4684 vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 4685 { 4686 _NOTE(ARGUNUSED(vdc)) 4687 4688 vd_geom_t vdgeom; 4689 void *tmp_mem = NULL; 4690 int copy_len = sizeof (struct dk_geom); 4691 int rv = 0; 4692 4693 if (dir != VD_COPYIN) 4694 return (0); /* nothing to do */ 4695 4696 if ((from == NULL) || (to == NULL)) 4697 return (ENXIO); 4698 4699 tmp_mem = kmem_alloc(copy_len, KM_SLEEP); 4700 4701 rv = ddi_copyin(from, tmp_mem, copy_len, mode); 4702 if (rv != 0) { 4703 kmem_free(tmp_mem, copy_len); 4704 return (EFAULT); 4705 } 4706 DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom); 4707 bcopy(&vdgeom, to, sizeof (vdgeom)); 4708 kmem_free(tmp_mem, copy_len); 4709 4710 return (0); 4711 } 4712 4713 static int 4714 vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 4715 { 4716 _NOTE(ARGUNUSED(vdc)) 4717 4718 vd_efi_t *vd_efi; 4719 dk_efi_t dk_efi; 4720 int rv = 0; 4721 void *uaddr; 4722 4723 if ((from == NULL) || (to == NULL)) 4724 return (ENXIO); 4725 4726 if (dir == VD_COPYIN) { 4727 4728 vd_efi = (vd_efi_t *)to; 4729 4730 rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode); 4731 if (rv != 0) 4732 return (EFAULT); 4733 4734 vd_efi->lba = dk_efi.dki_lba; 4735 vd_efi->length = dk_efi.dki_length; 4736 bzero(vd_efi->data, vd_efi->length); 4737 4738 } else { 4739 4740 rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode); 4741 if (rv != 0) 4742 return (EFAULT); 4743 4744 uaddr = dk_efi.dki_data; 4745 4746 dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP); 4747 4748 VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi); 4749 4750 rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length, 4751 mode); 4752 if (rv != 0) 4753 return (EFAULT); 4754 4755 kmem_free(dk_efi.dki_data, dk_efi.dki_length); 4756 } 4757 4758 return (0); 4759 } 4760 4761 static int 4762 vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir) 4763 { 4764 _NOTE(ARGUNUSED(vdc)) 4765 4766 dk_efi_t dk_efi; 4767 void *uaddr; 4768 4769 if (dir == VD_COPYOUT) 4770 return (0); /* nothing to do */ 4771 4772 if ((from == NULL) || (to == NULL)) 4773 return (ENXIO); 4774 4775 if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0) 4776 return (EFAULT); 4777 4778 uaddr = dk_efi.dki_data; 4779 4780 dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP); 4781 4782 if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0) 4783 return (EFAULT); 4784 4785 DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to); 4786 4787 kmem_free(dk_efi.dki_data, dk_efi.dki_length); 4788 4789 return (0); 4790 } 4791 4792 /* 4793 * Function: 4794 * vdc_create_fake_geometry() 4795 * 4796 * Description: 4797 * This routine fakes up the disk info needed for some DKIO ioctls. 4798 * - DKIOCINFO 4799 * - DKIOCGMEDIAINFO 4800 * 4801 * [ just like lofi(7D) and ramdisk(7D) ] 4802 * 4803 * Arguments: 4804 * vdc - soft state pointer for this instance of the device driver. 4805 * 4806 * Return Code: 4807 * 0 - Success 4808 */ 4809 static int 4810 vdc_create_fake_geometry(vdc_t *vdc) 4811 { 4812 ASSERT(vdc != NULL); 4813 4814 /* 4815 * Check if max_xfer_sz and vdisk_size are valid 4816 */ 4817 if (vdc->vdisk_size == 0 || vdc->max_xfer_sz == 0) 4818 return (EIO); 4819 4820 /* 4821 * DKIOCINFO support 4822 */ 4823 vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 4824 4825 (void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME); 4826 (void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME); 4827 /* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */ 4828 vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz; 4829 vdc->cinfo->dki_ctype = DKC_SCSI_CCS; 4830 vdc->cinfo->dki_flags = DKI_FMTVOL; 4831 vdc->cinfo->dki_cnum = 0; 4832 vdc->cinfo->dki_addr = 0; 4833 vdc->cinfo->dki_space = 0; 4834 vdc->cinfo->dki_prio = 0; 4835 vdc->cinfo->dki_vec = 0; 4836 vdc->cinfo->dki_unit = vdc->instance; 4837 vdc->cinfo->dki_slave = 0; 4838 /* 4839 * The partition number will be created on the fly depending on the 4840 * actual slice (i.e. minor node) that is used to request the data. 4841 */ 4842 vdc->cinfo->dki_partition = 0; 4843 4844 /* 4845 * DKIOCGMEDIAINFO support 4846 */ 4847 if (vdc->minfo == NULL) 4848 vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP); 4849 vdc->minfo->dki_media_type = DK_FIXED_DISK; 4850 vdc->minfo->dki_capacity = vdc->vdisk_size; 4851 vdc->minfo->dki_lbsize = DEV_BSIZE; 4852 4853 return (0); 4854 } 4855 4856 /* 4857 * Function: 4858 * vdc_setup_disk_layout() 4859 * 4860 * Description: 4861 * This routine discovers all the necessary details about the "disk" 4862 * by requesting the data that is available from the vDisk server and by 4863 * faking up the rest of the data. 4864 * 4865 * Arguments: 4866 * vdc - soft state pointer for this instance of the device driver. 4867 * 4868 * Return Code: 4869 * 0 - Success 4870 */ 4871 static int 4872 vdc_setup_disk_layout(vdc_t *vdc) 4873 { 4874 buf_t *buf; /* BREAD requests need to be in a buf_t structure */ 4875 dev_t dev; 4876 int slice = 0; 4877 int rv, error; 4878 4879 ASSERT(vdc != NULL); 4880 4881 if (vdc->vtoc == NULL) 4882 vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP); 4883 4884 dev = makedevice(ddi_driver_major(vdc->dip), 4885 VD_MAKE_DEV(vdc->instance, 0)); 4886 rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)vdc->vtoc, FKIOCTL); 4887 4888 if (rv && rv != ENOTSUP) { 4889 DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)", 4890 vdc->instance, rv); 4891 return (rv); 4892 } 4893 4894 /* 4895 * The process of attempting to read VTOC will initiate 4896 * the handshake and establish a connection. Following 4897 * handshake, go ahead and create geometry. 4898 */ 4899 error = vdc_create_fake_geometry(vdc); 4900 if (error != 0) { 4901 DMSG(vdc, 0, "[%d] Failed to create disk geometry (err%d)", 4902 vdc->instance, error); 4903 return (error); 4904 } 4905 4906 if (rv == ENOTSUP) { 4907 /* 4908 * If the device does not support VTOC then we try 4909 * to read an EFI label. 4910 */ 4911 struct dk_gpt *efi; 4912 size_t efi_len; 4913 4914 rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len); 4915 4916 if (rv) { 4917 DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)", 4918 vdc->instance, rv); 4919 return (rv); 4920 } 4921 4922 vdc->vdisk_label = VD_DISK_LABEL_EFI; 4923 vdc_store_efi(vdc, efi); 4924 vd_efi_free(efi, efi_len); 4925 4926 return (0); 4927 } 4928 4929 vdc->vdisk_label = VD_DISK_LABEL_VTOC; 4930 4931 /* 4932 * FUTURE: This could be default way for reading the VTOC 4933 * from the disk as supposed to sending the VD_OP_GET_VTOC 4934 * to the server. Currently this is a sanity check. 4935 * 4936 * find the slice that represents the entire "disk" and use that to 4937 * read the disk label. The convention in Solaris is that slice 2 4938 * represents the whole disk so we check that it is, otherwise we 4939 * default to slice 0 4940 */ 4941 if ((vdc->vdisk_type == VD_DISK_TYPE_DISK) && 4942 (vdc->vtoc->v_part[2].p_tag == V_BACKUP)) { 4943 slice = 2; 4944 } else { 4945 slice = 0; 4946 } 4947 4948 /* 4949 * Read disk label from start of disk 4950 */ 4951 vdc->label = kmem_zalloc(DK_LABEL_SIZE, KM_SLEEP); 4952 buf = kmem_alloc(sizeof (buf_t), KM_SLEEP); 4953 bioinit(buf); 4954 buf->b_un.b_addr = (caddr_t)vdc->label; 4955 buf->b_bcount = DK_LABEL_SIZE; 4956 buf->b_flags = B_BUSY | B_READ; 4957 buf->b_dev = dev; 4958 rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)vdc->label, 4959 DK_LABEL_SIZE, slice, 0, CB_STRATEGY, buf, VIO_read_dir); 4960 if (rv) { 4961 DMSG(vdc, 1, "[%d] Failed to read disk block 0\n", 4962 vdc->instance); 4963 kmem_free(buf, sizeof (buf_t)); 4964 return (rv); 4965 } 4966 rv = biowait(buf); 4967 biofini(buf); 4968 kmem_free(buf, sizeof (buf_t)); 4969 4970 return (rv); 4971 } 4972 4973 /* 4974 * Function: 4975 * vdc_setup_devid() 4976 * 4977 * Description: 4978 * This routine discovers the devid of a vDisk. It requests the devid of 4979 * the underlying device from the vDisk server, builds an encapsulated 4980 * devid based on the retrieved devid and registers that new devid to 4981 * the vDisk. 4982 * 4983 * Arguments: 4984 * vdc - soft state pointer for this instance of the device driver. 4985 * 4986 * Return Code: 4987 * 0 - A devid was succesfully registered for the vDisk 4988 */ 4989 static int 4990 vdc_setup_devid(vdc_t *vdc) 4991 { 4992 int rv; 4993 vd_devid_t *vd_devid; 4994 size_t bufsize, bufid_len; 4995 4996 /* 4997 * At first sight, we don't know the size of the devid that the 4998 * server will return but this size will be encoded into the 4999 * reply. So we do a first request using a default size then we 5000 * check if this size was large enough. If not then we do a second 5001 * request with the correct size returned by the server. Note that 5002 * ldc requires size to be 8-byte aligned. 5003 */ 5004 bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN), 5005 sizeof (uint64_t)); 5006 vd_devid = kmem_zalloc(bufsize, KM_SLEEP); 5007 bufid_len = bufsize - sizeof (vd_efi_t) - 1; 5008 5009 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid, 5010 bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir); 5011 5012 DMSG(vdc, 2, "sync_op returned %d\n", rv); 5013 5014 if (rv) { 5015 kmem_free(vd_devid, bufsize); 5016 return (rv); 5017 } 5018 5019 if (vd_devid->length > bufid_len) { 5020 /* 5021 * The returned devid is larger than the buffer used. Try again 5022 * with a buffer with the right size. 5023 */ 5024 kmem_free(vd_devid, bufsize); 5025 bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length), 5026 sizeof (uint64_t)); 5027 vd_devid = kmem_zalloc(bufsize, KM_SLEEP); 5028 bufid_len = bufsize - sizeof (vd_efi_t) - 1; 5029 5030 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, 5031 (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0, 5032 VIO_both_dir); 5033 5034 if (rv) { 5035 kmem_free(vd_devid, bufsize); 5036 return (rv); 5037 } 5038 } 5039 5040 /* 5041 * The virtual disk should have the same device id as the one associated 5042 * with the physical disk it is mapped on, otherwise sharing a disk 5043 * between a LDom and a non-LDom may not work (for example for a shared 5044 * SVM disk set). 5045 * 5046 * The DDI framework does not allow creating a device id with any 5047 * type so we first create a device id of type DEVID_ENCAP and then 5048 * we restore the orignal type of the physical device. 5049 */ 5050 5051 DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length); 5052 5053 /* build an encapsulated devid based on the returned devid */ 5054 if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length, 5055 vd_devid->id, &vdc->devid) != DDI_SUCCESS) { 5056 DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance); 5057 kmem_free(vd_devid, bufsize); 5058 return (1); 5059 } 5060 5061 DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type); 5062 5063 ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS); 5064 5065 kmem_free(vd_devid, bufsize); 5066 5067 if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) { 5068 DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance); 5069 return (1); 5070 } 5071 5072 return (0); 5073 } 5074 5075 static void 5076 vdc_store_efi(vdc_t *vdc, struct dk_gpt *efi) 5077 { 5078 struct vtoc *vtoc = vdc->vtoc; 5079 5080 vd_efi_to_vtoc(efi, vtoc); 5081 if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) { 5082 /* 5083 * vd_efi_to_vtoc() will store information about the EFI Sun 5084 * reserved partition (representing the entire disk) into 5085 * partition 7. However single-slice device will only have 5086 * that single partition and the vdc driver expects to find 5087 * information about that partition in slice 0. So we need 5088 * to copy information from slice 7 to slice 0. 5089 */ 5090 vtoc->v_part[0].p_tag = vtoc->v_part[VD_EFI_WD_SLICE].p_tag; 5091 vtoc->v_part[0].p_flag = vtoc->v_part[VD_EFI_WD_SLICE].p_flag; 5092 vtoc->v_part[0].p_start = vtoc->v_part[VD_EFI_WD_SLICE].p_start; 5093 vtoc->v_part[0].p_size = vtoc->v_part[VD_EFI_WD_SLICE].p_size; 5094 } 5095 } 5096