1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * DR control module for LDoms 29 */ 30 31 #include <sys/sysmacros.h> 32 #include <sys/modctl.h> 33 #include <sys/conf.h> 34 #include <sys/ddi.h> 35 #include <sys/sunddi.h> 36 #include <sys/ddi_impldefs.h> 37 #include <sys/stat.h> 38 #include <sys/door.h> 39 #include <sys/open.h> 40 #include <sys/note.h> 41 #include <sys/ldoms.h> 42 #include <sys/dr_util.h> 43 #include <sys/drctl.h> 44 #include <sys/drctl_impl.h> 45 46 47 static int drctl_attach(dev_info_t *, ddi_attach_cmd_t); 48 static int drctl_detach(dev_info_t *, ddi_detach_cmd_t); 49 static int drctl_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 50 51 static int drctl_open(dev_t *, int, int, cred_t *); 52 static int drctl_close(dev_t, int, int, cred_t *); 53 static int drctl_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 54 55 static void *pack_message(int, int, int, void *, size_t *, size_t *); 56 static int send_message(void *, size_t, drctl_resp_t **, size_t *); 57 58 59 /* 60 * Configuration data structures 61 */ 62 static struct cb_ops drctl_cb_ops = { 63 drctl_open, /* open */ 64 drctl_close, /* close */ 65 nodev, /* strategy */ 66 nodev, /* print */ 67 nodev, /* dump */ 68 nodev, /* read */ 69 nodev, /* write */ 70 drctl_ioctl, /* ioctl */ 71 nodev, /* devmap */ 72 nodev, /* mmap */ 73 nodev, /* segmap */ 74 nochpoll, /* poll */ 75 ddi_prop_op, /* prop_op */ 76 NULL, /* streamtab */ 77 D_MP | D_NEW, /* driver compatibility flag */ 78 CB_REV, /* cb_ops revision */ 79 nodev, /* async read */ 80 nodev /* async write */ 81 }; 82 83 84 static struct dev_ops drctl_ops = { 85 DEVO_REV, /* devo_rev */ 86 0, /* refcnt */ 87 drctl_getinfo, /* info */ 88 nulldev, /* identify */ 89 nulldev, /* probe */ 90 drctl_attach, /* attach */ 91 drctl_detach, /* detach */ 92 nodev, /* reset */ 93 &drctl_cb_ops, /* driver operations */ 94 NULL, /* bus operations */ 95 NULL, /* power */ 96 ddi_quiesce_not_needed, /* quiesce */ 97 }; 98 99 static struct modldrv modldrv = { 100 &mod_driverops, /* type of module - driver */ 101 "DR Control pseudo driver", 102 &drctl_ops 103 }; 104 105 static struct modlinkage modlinkage = { 106 MODREV_1, 107 &modldrv, 108 NULL 109 }; 110 111 112 /* 113 * Locking strategy 114 * 115 * One of the reasons for this module's existence is to serialize 116 * DR requests which might be coming from different sources. Only 117 * one operation is allowed to be in progress at any given time. 118 * 119 * A single lock word (the 'drc_busy' element below) is NULL 120 * when there is no operation in progress. When a client of this 121 * module initiates an operation it grabs the mutex 'drc_lock' in 122 * order to examine the lock word ('drc_busy'). If no other 123 * operation is in progress, the lock word will be NULL. If so, 124 * a cookie which uniquely identifies the requestor is stored in 125 * the lock word, and the mutex is released. Attempts by other 126 * clients to initiate an operation will fail. 127 * 128 * When the lock-holding client's operation is completed, the 129 * client will call a "finalize" function in this module, providing 130 * the cookie passed with the original request. Since the cookie 131 * matches, the operation will succeed and the lock word will be 132 * cleared. At this point, an new operation may be initiated. 133 */ 134 135 /* 136 * Driver private data 137 */ 138 static struct drctl_unit { 139 kmutex_t drc_lock; /* global driver lock */ 140 dev_info_t *drc_dip; /* dev_info pointer */ 141 kcondvar_t drc_busy_cv; /* block for !busy */ 142 drctl_cookie_t drc_busy; /* NULL if free else a unique */ 143 /* identifier for caller */ 144 int drc_cmd; /* the cmd underway (or -1) */ 145 int drc_flags; /* saved flag from above cmd */ 146 int drc_inst; /* our single instance */ 147 uint_t drc_state; /* driver state */ 148 } drctl_state; 149 150 static struct drctl_unit *drctlp = &drctl_state; 151 152 int 153 _init(void) 154 { 155 int rv; 156 157 drctlp->drc_inst = -1; 158 mutex_init(&drctlp->drc_lock, NULL, MUTEX_DRIVER, NULL); 159 160 if ((rv = mod_install(&modlinkage)) != 0) 161 mutex_destroy(&drctlp->drc_lock); 162 163 return (rv); 164 } 165 166 167 int 168 _fini(void) 169 { 170 int rv; 171 172 if ((rv = mod_remove(&modlinkage)) != 0) 173 return (rv); 174 175 mutex_destroy(&drctlp->drc_lock); 176 return (0); 177 } 178 179 180 int 181 _info(struct modinfo *modinfop) 182 { 183 return (mod_info(&modlinkage, modinfop)); 184 } 185 186 187 /* 188 * Do the attach work 189 */ 190 static int 191 drctl_do_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 192 { 193 _NOTE(ARGUNUSED(cmd)) 194 195 char *str = "drctl_do_attach"; 196 int retval = DDI_SUCCESS; 197 198 if (drctlp->drc_inst != -1) { 199 cmn_err(CE_WARN, "%s: an instance is already attached!", str); 200 return (DDI_FAILURE); 201 } 202 drctlp->drc_inst = ddi_get_instance(dip); 203 204 retval = ddi_create_minor_node(dip, "drctl", S_IFCHR, 205 drctlp->drc_inst, DDI_PSEUDO, 0); 206 if (retval != DDI_SUCCESS) { 207 cmn_err(CE_WARN, "%s: can't create minor node", str); 208 drctlp->drc_inst = -1; 209 return (retval); 210 } 211 212 drctlp->drc_dip = dip; 213 ddi_report_dev(dip); 214 215 return (retval); 216 } 217 218 219 static int 220 drctl_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 221 { 222 switch (cmd) { 223 case DDI_ATTACH: 224 return (drctl_do_attach(dip, cmd)); 225 226 default: 227 return (DDI_FAILURE); 228 } 229 } 230 231 232 /* ARGSUSED */ 233 static int 234 drctl_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 235 { 236 switch (cmd) { 237 case DDI_DETACH: 238 drctlp->drc_inst = -1; 239 ddi_remove_minor_node(dip, "drctl"); 240 return (DDI_SUCCESS); 241 242 default: 243 return (DDI_FAILURE); 244 } 245 } 246 247 static int 248 drctl_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 249 { 250 _NOTE(ARGUNUSED(dip, cmd, arg, resultp)) 251 252 return (0); 253 } 254 255 static int 256 drctl_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 257 { 258 _NOTE(ARGUNUSED(devp, flag, cred_p)) 259 260 if (otyp != OTYP_CHR) 261 return (EINVAL); 262 263 return (0); 264 } 265 266 static int 267 drctl_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 268 { 269 _NOTE(ARGUNUSED(dev, flag, otyp, cred_p)) 270 271 return (0); 272 } 273 274 /* 275 * Create a reponse structure which includes an array of drctl_rsrc_t 276 * structures in which each status element is set to the 'status' 277 * arg. There is no error text, so set the 'offset' elements to 0. 278 */ 279 static drctl_resp_t * 280 drctl_generate_resp(drctl_rsrc_t *res, 281 int count, size_t *rsize, drctl_status_t status) 282 { 283 int i; 284 size_t size; 285 drctl_rsrc_t *rsrc; 286 drctl_resp_t *resp; 287 288 size = offsetof(drctl_resp_t, resp_resources) + (count * sizeof (*res)); 289 resp = kmem_alloc(size, KM_SLEEP); 290 DR_DBG_KMEM("%s: alloc addr %p size %ld\n", 291 __func__, (void *)resp, size); 292 293 resp->resp_type = DRCTL_RESP_OK; 294 rsrc = resp->resp_resources; 295 296 bcopy(res, rsrc, count * sizeof (*res)); 297 298 for (i = 0; i < count; i++) { 299 rsrc[i].status = status; 300 rsrc[i].offset = 0; 301 } 302 303 *rsize = size; 304 305 return (resp); 306 } 307 308 /* 309 * Generate an error response message. 310 */ 311 static drctl_resp_t * 312 drctl_generate_err_resp(char *msg, size_t *size) 313 { 314 drctl_resp_t *resp; 315 316 ASSERT(msg != NULL); 317 ASSERT(size != NULL); 318 319 *size = offsetof(drctl_resp_t, resp_err_msg) + strlen(msg) + 1; 320 resp = kmem_alloc(*size, KM_SLEEP); 321 DR_DBG_KMEM("%s: alloc addr %p size %ld\n", 322 __func__, (void *)resp, *size); 323 324 resp->resp_type = DRCTL_RESP_ERR; 325 (void) strcpy(resp->resp_err_msg, msg); 326 327 return (resp); 328 } 329 330 /* 331 * Since response comes from userland, verify that it is at least the 332 * minimum size based on the size of the original request. Verify 333 * that any offsets to error strings are within the string area of 334 * the response and, force the string area to be null-terminated. 335 */ 336 static int 337 verify_response(int cmd, 338 int count, drctl_resp_t *resp, size_t sent_len, size_t resp_len) 339 { 340 drctl_rsrc_t *rsrc = resp->resp_resources; 341 size_t rcvd_len = resp_len - (offsetof(drctl_resp_t, resp_resources)); 342 int is_cpu = 0; 343 int i; 344 345 switch (cmd) { 346 case DRCTL_CPU_CONFIG_REQUEST: 347 case DRCTL_CPU_UNCONFIG_REQUEST: 348 if (rcvd_len < sent_len) 349 return (EIO); 350 is_cpu = 1; 351 break; 352 case DRCTL_IO_UNCONFIG_REQUEST: 353 case DRCTL_IO_CONFIG_REQUEST: 354 if (count != 1) 355 return (EIO); 356 break; 357 case DRCTL_MEM_CONFIG_REQUEST: 358 case DRCTL_MEM_UNCONFIG_REQUEST: 359 break; 360 default: 361 return (EIO); 362 } 363 364 for (i = 0; i < count; i++) 365 if ((rsrc[i].offset > 0) && 366 /* string can't be inside the bounds of original request */ 367 (((rsrc[i].offset < sent_len) && is_cpu) || 368 /* string must start inside the message */ 369 (rsrc[i].offset >= rcvd_len))) 370 return (EIO); 371 372 /* If there are any strings, terminate the string area. */ 373 if (rcvd_len > sent_len) 374 *((char *)rsrc + rcvd_len - 1) = '\0'; 375 376 return (0); 377 } 378 379 380 static int 381 drctl_config_common(int cmd, int flags, drctl_rsrc_t *res, 382 int count, drctl_resp_t **rbuf, size_t *rsize, size_t *rq_size) 383 { 384 int rv = 0; 385 size_t size; 386 char *bufp; 387 388 switch (cmd) { 389 case DRCTL_CPU_CONFIG_REQUEST: 390 case DRCTL_CPU_CONFIG_NOTIFY: 391 case DRCTL_CPU_UNCONFIG_REQUEST: 392 case DRCTL_CPU_UNCONFIG_NOTIFY: 393 case DRCTL_IO_UNCONFIG_REQUEST: 394 case DRCTL_IO_UNCONFIG_NOTIFY: 395 case DRCTL_IO_CONFIG_REQUEST: 396 case DRCTL_IO_CONFIG_NOTIFY: 397 case DRCTL_MEM_CONFIG_REQUEST: 398 case DRCTL_MEM_CONFIG_NOTIFY: 399 case DRCTL_MEM_UNCONFIG_REQUEST: 400 case DRCTL_MEM_UNCONFIG_NOTIFY: 401 rv = 0; 402 break; 403 default: 404 rv = ENOTSUP; 405 break; 406 } 407 408 if (rv != 0) { 409 DR_DBG_CTL("%s: invalid cmd %d\n", __func__, cmd); 410 return (rv); 411 } 412 413 /* 414 * If the operation is a FORCE, we don't send a message to 415 * the daemon. But, the upstream clients still expect a 416 * response, so generate a response with all ops 'allowed'. 417 */ 418 if (flags == DRCTL_FLAG_FORCE) { 419 if (rbuf != NULL) 420 *rbuf = drctl_generate_resp(res, 421 count, rsize, DRCTL_STATUS_ALLOW); 422 return (0); 423 } 424 425 bufp = pack_message(cmd, flags, count, (void *)res, &size, rq_size); 426 DR_DBG_CTL("%s: from pack_message, bufp = %p size %ld\n", 427 __func__, (void *)bufp, size); 428 429 if (bufp == NULL || size == 0) 430 return (EINVAL); 431 432 return (send_message(bufp, size, rbuf, rsize)); 433 } 434 435 /* 436 * Prepare for a reconfig operation. 437 */ 438 int 439 drctl_config_init(int cmd, int flags, drctl_rsrc_t *res, 440 int count, drctl_resp_t **rbuf, size_t *rsize, drctl_cookie_t ck) 441 { 442 static char inval_msg[] = "Invalid command format received.\n"; 443 static char unsup_msg[] = "Unsuppported command received.\n"; 444 static char unk_msg [] = "Failure reason unknown.\n"; 445 static char rsp_msg [] = "Invalid response from " 446 "reconfiguration daemon.\n"; 447 static char drd_msg [] = "Cannot communicate with reconfiguration " 448 "daemon (drd) in target domain.\n" 449 "drd(1M) SMF service may not be enabled.\n"; 450 static char busy_msg [] = "Busy executing earlier command; " 451 "please try again later.\n"; 452 size_t rq_size; 453 char *ermsg; 454 int rv; 455 456 if (ck == 0) { 457 *rbuf = drctl_generate_err_resp(inval_msg, rsize); 458 459 return (EINVAL); 460 } 461 462 mutex_enter(&drctlp->drc_lock); 463 464 if (drctlp->drc_busy != NULL) { 465 mutex_exit(&drctlp->drc_lock); 466 *rbuf = drctl_generate_err_resp(busy_msg, rsize); 467 468 return (EBUSY); 469 } 470 471 DR_DBG_CTL("%s: cmd %d flags %d res %p count %d\n", 472 __func__, cmd, flags, (void *)res, count); 473 474 /* Mark the link busy. Below we will fill in the actual cookie. */ 475 drctlp->drc_busy = (drctl_cookie_t)-1; 476 mutex_exit(&drctlp->drc_lock); 477 478 rv = drctl_config_common(cmd, flags, res, count, rbuf, rsize, &rq_size); 479 if (rv == 0) { 480 /* 481 * If the upcall to the daemon returned successfully, we 482 * still need to validate the format of the returned msg. 483 */ 484 if ((rv = verify_response(cmd, 485 count, *rbuf, rq_size, *rsize)) != 0) { 486 DR_DBG_KMEM("%s: free addr %p size %ld\n", 487 __func__, (void *)*rbuf, *rsize); 488 kmem_free(*rbuf, *rsize); 489 *rbuf = drctl_generate_err_resp(rsp_msg, rsize); 490 drctlp->drc_busy = NULL; 491 } else { /* message format is valid */ 492 drctlp->drc_busy = ck; 493 drctlp->drc_cmd = cmd; 494 drctlp->drc_flags = flags; 495 } 496 } else { 497 switch (rv) { 498 case ENOTSUP: 499 ermsg = unsup_msg; 500 break; 501 case EIO: 502 ermsg = drd_msg; 503 break; 504 default: 505 ermsg = unk_msg; 506 break; 507 } 508 509 *rbuf = drctl_generate_err_resp(ermsg, rsize); 510 511 drctlp->drc_cmd = -1; 512 drctlp->drc_flags = 0; 513 drctlp->drc_busy = NULL; 514 } 515 516 return (rv); 517 } 518 519 /* 520 * Complete a reconfig operation. 521 */ 522 int 523 drctl_config_fini(drctl_cookie_t ck, drctl_rsrc_t *res, int count) 524 { 525 int rv; 526 int notify_cmd; 527 int flags; 528 size_t rq_size; 529 530 mutex_enter(&drctlp->drc_lock); 531 532 if (drctlp->drc_busy != ck) { 533 mutex_exit(&drctlp->drc_lock); 534 return (EBUSY); 535 } 536 537 mutex_exit(&drctlp->drc_lock); 538 539 flags = drctlp->drc_flags; 540 /* 541 * Flip the saved _REQUEST command to its corresponding 542 * _NOTIFY command. 543 */ 544 switch (drctlp->drc_cmd) { 545 case DRCTL_CPU_CONFIG_REQUEST: 546 notify_cmd = DRCTL_CPU_CONFIG_NOTIFY; 547 break; 548 549 case DRCTL_CPU_UNCONFIG_REQUEST: 550 notify_cmd = DRCTL_CPU_UNCONFIG_NOTIFY; 551 break; 552 553 case DRCTL_IO_UNCONFIG_REQUEST: 554 notify_cmd = DRCTL_IO_UNCONFIG_NOTIFY; 555 break; 556 557 case DRCTL_IO_CONFIG_REQUEST: 558 notify_cmd = DRCTL_IO_CONFIG_NOTIFY; 559 break; 560 561 case DRCTL_MEM_CONFIG_REQUEST: 562 notify_cmd = DRCTL_MEM_CONFIG_NOTIFY; 563 break; 564 565 case DRCTL_MEM_UNCONFIG_REQUEST: 566 notify_cmd = DRCTL_MEM_UNCONFIG_NOTIFY; 567 break; 568 569 default: 570 /* none of the above should have been accepted in _init */ 571 ASSERT(0); 572 cmn_err(CE_CONT, 573 "drctl_config_fini: bad cmd %d\n", drctlp->drc_cmd); 574 rv = EINVAL; 575 goto done; 576 } 577 578 rv = drctl_config_common(notify_cmd, 579 flags, res, count, NULL, 0, &rq_size); 580 581 done: 582 drctlp->drc_cmd = -1; 583 drctlp->drc_flags = 0; 584 drctlp->drc_busy = NULL; 585 586 return (rv); 587 } 588 589 static int 590 drctl_ioctl(dev_t dev, 591 int cmd, intptr_t arg, int mode, cred_t *cred_p, int *rval_p) 592 { 593 _NOTE(ARGUNUSED(dev, mode, cred_p, rval_p)) 594 595 int rv; 596 597 switch (cmd) { 598 case DRCTL_IOCTL_CONNECT_SERVER: 599 rv = i_drctl_ioctl(cmd, arg); 600 break; 601 default: 602 rv = ENOTSUP; 603 } 604 605 *rval_p = (rv == 0) ? 0 : -1; 606 607 return (rv); 608 } 609 610 /* 611 * Accept a preformatted request from caller and send a message to 612 * the daemon. A pointer to the daemon's response buffer is passed 613 * back in obufp, its size in osize. 614 */ 615 static int 616 send_message(void *msg, size_t size, drctl_resp_t **obufp, size_t *osize) 617 { 618 drctl_resp_t *bufp; 619 drctl_rsrc_t *rsrcs; 620 size_t rsrcs_size; 621 int rv; 622 623 rv = i_drctl_send(msg, size, (void **)&rsrcs, &rsrcs_size); 624 625 if ((rv == 0) && ((rsrcs == NULL) ||(rsrcs_size == 0))) 626 rv = EINVAL; 627 628 if (rv == 0) { 629 if (obufp != NULL) { 630 ASSERT(osize != NULL); 631 632 *osize = 633 offsetof(drctl_resp_t, resp_resources) + rsrcs_size; 634 bufp = 635 kmem_alloc(*osize, KM_SLEEP); 636 DR_DBG_KMEM("%s: alloc addr %p size %ld\n", 637 __func__, (void *)bufp, *osize); 638 bufp->resp_type = DRCTL_RESP_OK; 639 bcopy(rsrcs, bufp->resp_resources, rsrcs_size); 640 *obufp = bufp; 641 } 642 643 DR_DBG_KMEM("%s: free addr %p size %ld\n", 644 __func__, (void *)rsrcs, rsrcs_size); 645 kmem_free(rsrcs, rsrcs_size); 646 } 647 648 DR_DBG_KMEM("%s:free addr %p size %ld\n", __func__, msg, size); 649 kmem_free(msg, size); 650 651 return (rv); 652 } 653 654 static void * 655 pack_message(int cmd, 656 int flags, int count, void *data, size_t *osize, size_t *data_size) 657 { 658 drd_msg_t *msgp = NULL; 659 size_t hdr_size = offsetof(drd_msg_t, data); 660 661 switch (cmd) { 662 case DRCTL_CPU_CONFIG_REQUEST: 663 case DRCTL_CPU_CONFIG_NOTIFY: 664 case DRCTL_CPU_UNCONFIG_REQUEST: 665 case DRCTL_CPU_UNCONFIG_NOTIFY: 666 *data_size = count * sizeof (drctl_rsrc_t); 667 break; 668 case DRCTL_MEM_CONFIG_REQUEST: 669 case DRCTL_MEM_CONFIG_NOTIFY: 670 case DRCTL_MEM_UNCONFIG_REQUEST: 671 case DRCTL_MEM_UNCONFIG_NOTIFY: 672 *data_size = count * sizeof (drctl_rsrc_t); 673 break; 674 case DRCTL_IO_CONFIG_REQUEST: 675 case DRCTL_IO_CONFIG_NOTIFY: 676 case DRCTL_IO_UNCONFIG_REQUEST: 677 case DRCTL_IO_UNCONFIG_NOTIFY: 678 *data_size = sizeof (drctl_rsrc_t) + 679 strlen(((drctl_rsrc_t *)data)->res_dev_path); 680 break; 681 default: 682 cmn_err(CE_WARN, 683 "drctl: pack_message received invalid cmd %d", cmd); 684 break; 685 } 686 687 if (data_size) { 688 *osize = hdr_size + *data_size; 689 msgp = kmem_alloc(*osize, KM_SLEEP); 690 DR_DBG_KMEM("%s: alloc addr %p size %ld\n", 691 __func__, (void *)msgp, *osize); 692 msgp->cmd = cmd; 693 msgp->count = count; 694 msgp->flags = flags; 695 bcopy(data, msgp->data, *data_size); 696 } 697 698 return (msgp); 699 } 700