drctl.c revision 8fea755a86ff6c596183a4366bfbd59f1bfdfe55
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * CDDL HEADER START
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * The contents of this file are subject to the terms of the
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Common Development and Distribution License (the "License").
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * You may not use this file except in compliance with the License.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * or http://www.opensolaris.org/os/licensing.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * See the License for the specific language governing permissions
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * and limitations under the License.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * When distributing Covered Code, include this CDDL HEADER in each
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * If applicable, add the following below this CDDL HEADER, with the
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * fields enclosed by brackets "[]" replaced with your own identifying
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * information: Portions Copyright [yyyy] [name of copyright owner]
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * CDDL HEADER END
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Use is subject to license terms.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#pragma ident "%Z%%M% %I% %E% SMI"
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * DR control module for LDoms
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/sysmacros.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/modctl.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/conf.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/ddi.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/sunddi.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/ddi_impldefs.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/stat.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/door.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/open.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/note.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/ldoms.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/dr_util.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/drctl.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#include <sys/drctl_impl.h>
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int drctl_attach(dev_info_t *, ddi_attach_cmd_t);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int drctl_detach(dev_info_t *, ddi_detach_cmd_t);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int drctl_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int drctl_open(dev_t *, int, int, cred_t *);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int drctl_close(dev_t, int, int, cred_t *);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int drctl_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic void *pack_message(int, int, int, void *, size_t *);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int send_message(void *, size_t, void **, size_t *);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Configuration data structures
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic struct cb_ops drctl_cb_ops = {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_open, /* open */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_close, /* close */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* strategy */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* print */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* dump */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* read */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* write */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_ioctl, /* ioctl */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* devmap */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* mmap */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* segmap */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nochpoll, /* poll */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda ddi_prop_op, /* prop_op */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda NULL, /* streamtab */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda D_MP | D_NEW, /* driver compatibility flag */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda CB_REV, /* cb_ops revision */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* async read */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev /* async write */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda};
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic struct dev_ops drctl_ops = {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DEVO_REV, /* devo_rev */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda 0, /* refcnt */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_getinfo, /* info */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nulldev, /* identify */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nulldev, /* probe */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_attach, /* attach */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_detach, /* detach */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda nodev, /* reset */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda &drctl_cb_ops, /* driver operations */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda NULL, /* bus operations */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda NULL, /* power */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda};
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic struct modldrv modldrv = {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda &mod_driverops, /* type of module - driver */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda "DR Control pseudo driver v%I%",
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda &drctl_ops
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda};
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic struct modlinkage modlinkage = {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda MODREV_1,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda &modldrv,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda NULL
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda};
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Locking strategy
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * One of the reasons for this module's existence is to serialize
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * DR requests which might be coming from different sources. Only
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * one operation is allowed to be in progress at any given time.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * A single lock word (the 'drc_busy' element below) is NULL
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * when there is no operation in progress. When a client of this
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * module initiates an operation it grabs the mutex 'drc_lock' in
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * order to examine the lock word ('drc_busy'). If no other
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * operation is in progress, the lock word will be NULL. If so,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * a cookie which uniquely identifies the requestor is stored in
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * the lock word, and the mutex is released. Attempts by other
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * clients to initiate an operation will fail.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * When the lock-holding client's operation is completed, the
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * client will call a "finalize" function in this module, providing
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * the cookie passed with the original request. Since the cookie
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * matches, the operation will succeed and the lock word will be
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * cleared. At this point, an new operation may be initiated.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Driver private data
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic struct drctl_unit {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda kmutex_t drc_lock; /* global driver lock */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda dev_info_t *drc_dip; /* dev_info pointer */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda kcondvar_t drc_busy_cv; /* block for !busy */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_cookie_t drc_busy; /* NULL if free else a unique */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /* identifier for caller */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int drc_cmd; /* the cmd underway (or -1) */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int drc_flags; /* saved flag from above cmd */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int drc_inst; /* our single instance */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda uint_t drc_state; /* driver state */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda} drctl_state;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic struct drctl_unit *drctlp = &drctl_state;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedaint
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda_init(void)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm int rv;
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_inst = -1;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_init(&drctlp->drc_lock, NULL, MUTEX_DRIVER, NULL);
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm if ((rv = mod_install(&modlinkage)) != 0)
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm mutex_destroy(&drctlp->drc_lock);
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm return (rv);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedaint
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda_fini(void)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm int rv;
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm if ((rv = mod_remove(&modlinkage)) != 0)
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm return (rv);
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_destroy(&drctlp->drc_lock);
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedaint
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda_info(struct modinfo *modinfop)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (mod_info(&modlinkage, modinfop));
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Do the attach work
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_do_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda _NOTE(ARGUNUSED(cmd))
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda char *str = "drctl_do_attach";
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int retval = DDI_SUCCESS;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (drctlp->drc_inst != -1) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda cmn_err(CE_WARN, "%s: an instance is already attached!", str);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (DDI_FAILURE);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_inst = ddi_get_instance(dip);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda retval = ddi_create_minor_node(dip, "drctl", S_IFCHR,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_inst, DDI_PSEUDO, 0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (retval != DDI_SUCCESS) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda cmn_err(CE_WARN, "%s: can't create minor node", str);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_inst = -1;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (retval);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_dip = dip;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda ddi_report_dev(dip);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (retval);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda switch (cmd) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DDI_ATTACH:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (drctl_do_attach(dip, cmd));
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda default:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (DDI_FAILURE);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/* ARGSUSED */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda switch (cmd) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DDI_DETACH:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_inst = -1;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda ddi_remove_minor_node(dip, "drctl");
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (DDI_SUCCESS);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda default:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (DDI_FAILURE);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda _NOTE(ARGUNUSED(dip, cmd, arg, resultp))
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda _NOTE(ARGUNUSED(devp, flag, cred_p))
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (otyp != OTYP_CHR)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (EINVAL);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda _NOTE(ARGUNUSED(dev, flag, otyp, cred_p))
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * This driver guarantees that if drctl_config_init returns 0,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * a valid response buffer will be passed back to the caller. This
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * routine can be used to generate that response in cases where the
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * upcall has not resulted in a response message from userland.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic drctl_rsrc_t *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_generate_resp(drctl_rsrc_t *res,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int count, size_t *rsize, drctl_status_t status)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int idx;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda size_t size;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_rsrc_t *rbuf;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda size = count * sizeof (*res);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rbuf = kmem_alloc(size, KM_SLEEP);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda bcopy(res, rbuf, size);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda for (idx = 0; idx < count; idx++) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rbuf[idx].status = status;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rbuf[idx].offset = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *rsize = size;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (rbuf);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_config_common(int cmd, int flags, drctl_rsrc_t *res,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int count, drctl_rsrc_t **rbuf, size_t *rsize)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int rv = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda size_t size;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda char *bufp;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda static const char me[] = "drctl_config_common";
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda switch (cmd) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_CONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_CONFIG_NOTIFY:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_UNCONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_UNCONFIG_NOTIFY:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_UNCONFIG_REQUEST:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_UNCONFIG_NOTIFY:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_CONFIG_REQUEST:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_CONFIG_NOTIFY:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda break;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_MEM_CONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_MEM_CONFIG_NOTIFY:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_MEM_UNCONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_MEM_UNCONFIG_NOTIFY:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = ENOTSUP;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda break;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (rv != 0) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DR_DBG_CTL("%s: invalid cmd %d\n", me, cmd);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (rv);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * If the operation is a FORCE, we don't send a message to
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * the daemon. But, the upstream clients still expect a
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * response, so generate a response with all ops 'allowed'.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (flags == DRCTL_FLAG_FORCE) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (rbuf != NULL) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *rbuf = drctl_generate_resp(res, count, &size,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DRCTL_STATUS_ALLOW);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *rsize = size;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda bufp = pack_message(cmd, flags, count, (void *)res, &size);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DR_DBG_CTL("%s: from pack_message, bufp = %p size %ld\n",
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda me, (void *)bufp, size);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (bufp == NULL || size == 0)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (EIO);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = send_message(bufp, size, (void **)rbuf, rsize);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * For failure, as part of our contract with the caller,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * generate a response message, but mark all proposed
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * changes as 'denied'.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
20046e4c1cecc35fec6e8881900a6a4fc194ac2ajm if (rv != 0 && rbuf != NULL) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *rbuf = drctl_generate_resp(res, count, &size,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DRCTL_STATUS_DENY);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *rsize = size;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (rv);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Since the response comes from userland, make sure it is
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * at least the minimum size and, if it contains error
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * strings, that the string area is null-terminated.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedaverify_response(int count, drctl_rsrc_t *resp, size_t size)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int idx;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int need_terminator = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda static const char me[] = "verify_response";
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (resp == NULL || size < count * sizeof (*resp)) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DR_DBG_CTL("%s: BAD size - count %d size %ld\n",
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda me, count, size);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (EIO);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda for (idx = 0; idx < count; idx++) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (resp[idx].offset != 0)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda need_terminator++;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (need_terminator && *((caddr_t)(resp) + size - 1) != '\0') {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DR_DBG_CTL("%s: unterm. strings: resp %p size %ld char %d\n",
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda me, (void *)resp, size, *((caddr_t)(resp) + size - 1));
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /* Don't fail the transaction, but don't advertise strings */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda for (idx = 0; idx < count; idx++)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda resp[idx].offset = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Prepare for a reconfig operation.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedaint
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_config_init(int cmd, int flags, drctl_rsrc_t *res,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int count, drctl_rsrc_t **rbuf, size_t *rsize, drctl_cookie_t ck)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda static char me[] = "drctl_config_init";
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int idx;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int rv;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (ck == 0)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (EINVAL);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_enter(&drctlp->drc_lock);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (drctlp->drc_busy != NULL) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_exit(&drctlp->drc_lock);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (EBUSY);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DR_DBG_CTL("%s: cmd %d flags %d res %p count %d\n",
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda me, cmd, flags, (void *)res, count);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /* Mark the link busy. Below we will fill in the actual cookie. */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_busy = (drctl_cookie_t)-1;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_exit(&drctlp->drc_lock);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if ((rv = drctl_config_common(cmd,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda flags, res, count, rbuf, rsize)) == 0 &&
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda verify_response(count, *rbuf, *rsize) == 0) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_busy = ck;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_cmd = cmd;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_flags = flags;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * If there wasn't a valid response msg passed back,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * create a response with each resource op denied.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (*rbuf == NULL || *rsize == 0) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_rsrc_t *bp = *rbuf;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *rsize = count * sizeof (*bp);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda bp = kmem_zalloc(*rsize, KM_SLEEP);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda bcopy(res, bp, *rsize);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda for (idx = 0; idx < count; idx++) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda bp[idx].status = DRCTL_STATUS_DENY;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda bp[idx].offset = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda } else {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_cmd = -1;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_flags = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_busy = NULL;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (rv);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Complete a reconfig operation.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedaint
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_config_fini(drctl_cookie_t ck, drctl_rsrc_t *res, int count)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int rv;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int notify_cmd;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int flags;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_enter(&drctlp->drc_lock);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (drctlp->drc_busy != ck) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_exit(&drctlp->drc_lock);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (EBUSY);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_exit(&drctlp->drc_lock);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda flags = drctlp->drc_flags;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Flip the saved _REQUEST command to its corresponding
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * _NOTIFY command.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda switch (drctlp->drc_cmd) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_CONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda notify_cmd = DRCTL_CPU_CONFIG_NOTIFY;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda break;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_UNCONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda notify_cmd = DRCTL_CPU_UNCONFIG_NOTIFY;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda break;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_UNCONFIG_REQUEST:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm notify_cmd = DRCTL_IO_UNCONFIG_NOTIFY;
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm break;
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_CONFIG_REQUEST:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm notify_cmd = DRCTL_IO_CONFIG_NOTIFY;
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm break;
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_MEM_CONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_MEM_CONFIG_NOTIFY:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_MEM_UNCONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_MEM_UNCONFIG_NOTIFY:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda default:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /* none of the above should have been accepted in _init */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda ASSERT(0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda cmn_err(CE_CONT,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda "drctl_config_fini: bad cmd %d\n", drctlp->drc_cmd);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = EINVAL;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda goto done;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = drctl_config_common(notify_cmd, flags, res, count, NULL, 0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadone:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_cmd = -1;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_flags = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctlp->drc_busy = NULL;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (rv);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_ioctl(dev_t dev,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int cmd, intptr_t arg, int mode, cred_t *cred_p, int *rval_p)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda _NOTE(ARGUNUSED(dev, mode, cred_p, rval_p))
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int rv;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda switch (cmd) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_IOCTL_CONNECT_SERVER:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = i_drctl_ioctl(cmd, arg);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda break;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda default:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = ENOTSUP;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda *rval_p = (rv == 0) ? 0 : -1;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (rv);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/*
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Accept a preformatted request from caller and send a message to
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * the daemon. A pointer to the daemon's response buffer is passed
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * back in obufp, its size in osize.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedasend_message(void *msg, size_t size, void **obufp, size_t *osize)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int rv;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = i_drctl_send(msg, size, obufp, osize);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda kmem_free(msg, size);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (rv);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic void *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedapack_message(int cmd, int flags, int count, void *data, size_t *osize)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda{
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm drd_msg_t *msgp = NULL;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda size_t hdr_size = offsetof(drd_msg_t, data);
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm size_t data_size = 0;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda switch (cmd) {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_CONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_CONFIG_NOTIFY:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_UNCONFIG_REQUEST:
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda case DRCTL_CPU_UNCONFIG_NOTIFY:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm data_size = count * sizeof (drctl_rsrc_t);
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm break;
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_CONFIG_REQUEST:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_CONFIG_NOTIFY:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_UNCONFIG_REQUEST:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm case DRCTL_IO_UNCONFIG_NOTIFY:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm data_size = sizeof (drctl_rsrc_t) +
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm strlen(((drctl_rsrc_t *)data)->res_dev_path);
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm break;
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm default:
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm cmn_err(CE_WARN,
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm "drctl: pack_message received invalid cmd %d", cmd);
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm break;
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm if (data_size) {
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm *osize = hdr_size + data_size;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda msgp = kmem_alloc(*osize, KM_SLEEP);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda msgp->cmd = cmd;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda msgp->count = count;
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda msgp->flags = flags;
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm bcopy(data, msgp->data, data_size);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda }
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (msgp);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda}