drctl.c revision 8fea755a86ff6c596183a4366bfbd59f1bfdfe55
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * See the License for the specific language governing permissions
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * and limitations under the License.
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 * CDDL HEADER END
8fea755a86ff6c596183a4366bfbd59f1bfdfe55jm * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Use is subject to license terms.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda#pragma ident "%Z%%M% %I% %E% SMI"
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * DR control module for LDoms
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 **);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int drctl_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic void *pack_message(int, int, int, void *, size_t *);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic int send_message(void *, size_t, void **, size_t *);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Configuration data structures
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda 0, /* refcnt */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda "DR Control pseudo driver v%I%",
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Locking strategy
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 * 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 * 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 * Driver private data
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic struct drctl_unit {
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda drctl_cookie_t drc_busy; /* NULL if free else a unique */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /* identifier for caller */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda mutex_init(&drctlp->drc_lock, NULL, MUTEX_DRIVER, NULL);
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm return (rv);
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm return (rv);
e1ebb9ec908bc2d0a8810f137ebd6566cc8a8061lm return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Do the attach work
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_do_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda cmn_err(CE_WARN, "%s: an instance is already attached!", str);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda retval = ddi_create_minor_node(dip, "drctl", S_IFCHR,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda cmn_err(CE_WARN, "%s: can't create minor node", str);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda/* ARGSUSED */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda return (0);
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.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_config_common(int cmd, int flags, drctl_rsrc_t *res,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (rv != 0) {
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 return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda bufp = pack_message(cmd, flags, count, (void *)res, &size);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda DR_DBG_CTL("%s: from pack_message, bufp = %p size %ld\n",
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = send_message(bufp, size, (void **)rbuf, rsize);
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 * 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.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedaverify_response(int count, drctl_rsrc_t *resp, size_t size)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda if (resp == NULL || size < count * sizeof (*resp)) {
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 return (0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Prepare for a reconfig operation.
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 /* Mark the link busy. Below we will fill in the actual cookie. */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * If there wasn't a valid response msg passed back,
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * create a response with each resource op denied.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Complete a reconfig operation.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedadrctl_config_fini(drctl_cookie_t ck, drctl_rsrc_t *res, int count)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * Flip the saved _REQUEST command to its corresponding
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda * _NOTIFY command.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda /* none of the above should have been accepted in _init */
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda "drctl_config_fini: bad cmd %d\n", drctlp->drc_cmd);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda rv = drctl_config_common(notify_cmd, flags, res, count, NULL, 0);
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaeda int cmd, intptr_t arg, int mode, cred_t *cred_p, int *rval_p)
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.
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedasend_message(void *msg, size_t size, void **obufp, size_t *osize)
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedastatic void *
1d4b38e0077763e7c9b20768eacb841957e787bcrsmaedapack_message(int cmd, int flags, int count, void *data, size_t *osize)