xref: /illumos-gate/usr/src/uts/sun4/io/efcode/fcode.c (revision 193974072f41a843678abf5f61979c748687e66b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5447e4a63Spetede  * Common Development and Distribution License (the "License").
6447e4a63Spetede  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * fcode helper driver -- provide priv. access and kernel communication
297c478bd9Sstevel@tonic-gate  * to the userland fcode interpreter.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/cred.h>
337c478bd9Sstevel@tonic-gate #include <sys/mman.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
417c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/fcode.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static int fc_max_opens = 32;	/* Up to this many simultaneous opens */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Soft state associated with each instance of driver open.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate static struct fc_state {
517c478bd9Sstevel@tonic-gate 	int	state;		/* available flag or active state */
527c478bd9Sstevel@tonic-gate 	struct fc_request *req;	/* Active Request */
537c478bd9Sstevel@tonic-gate } *fc_states;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	FC_STATE_INACTIVE	0	/* Unopen, available for use */
567c478bd9Sstevel@tonic-gate #define	FC_STATE_OPEN		1	/* Inital open */
577c478bd9Sstevel@tonic-gate #define	FC_STATE_READ_DONE	2	/* blocking read done */
587c478bd9Sstevel@tonic-gate #define	FC_STATE_IN_PROGRESS	3	/* FC_GET_PARAMETERS done, active */
597c478bd9Sstevel@tonic-gate #define	FC_STATE_VALIDATED	4	/* FC_VALIDATE done, active */
606d22b733Sdhain #define	FC_STATE_ERROR_SET	5	/* FC_SET_FCODE_ERROR done, active */
617c478bd9Sstevel@tonic-gate #define	FC_STATE_ACTIVE(s)	((s) != 0)
627c478bd9Sstevel@tonic-gate #define	FC_STATE_AVAILABLE(s)	((s) == FC_STATE_INACTIVE)
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static kmutex_t fc_open_lock;	/* serialize instance assignment */
657c478bd9Sstevel@tonic-gate static kcondvar_t fc_open_cv;	/* wait for available open */
667c478bd9Sstevel@tonic-gate static int fc_open_count;	/* number of current open instance */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static int fc_open(dev_t *, int, int, cred_t *);
697c478bd9Sstevel@tonic-gate static int fc_close(dev_t, int, int, cred_t *);
707c478bd9Sstevel@tonic-gate static int fc_read(dev_t, struct uio *, cred_t *);
717c478bd9Sstevel@tonic-gate static int fc_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
727c478bd9Sstevel@tonic-gate static int fc_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
737c478bd9Sstevel@tonic-gate static int fc_attach(dev_info_t *, ddi_attach_cmd_t cmd);
747c478bd9Sstevel@tonic-gate static int fc_detach(dev_info_t *, ddi_detach_cmd_t cmd);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static int fc_get_parameters(dev_t, intptr_t, int, cred_t *, int *);
777c478bd9Sstevel@tonic-gate static int fc_get_my_args(dev_t, intptr_t, int, cred_t *, int *);
787c478bd9Sstevel@tonic-gate static int fc_run_priv(dev_t, intptr_t, int, cred_t *, int *);
797c478bd9Sstevel@tonic-gate static int fc_validate(dev_t, intptr_t, int, cred_t *, int *);
807c478bd9Sstevel@tonic-gate static int fc_get_fcode(dev_t, intptr_t, int, cred_t *, int *);
816d22b733Sdhain static int fc_set_fcode_error(dev_t, intptr_t, int, cred_t *, int *);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static struct cb_ops fc_cb_ops = {
847c478bd9Sstevel@tonic-gate 	fc_open,		/* open */
857c478bd9Sstevel@tonic-gate 	fc_close,		/* close */
867c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
877c478bd9Sstevel@tonic-gate 	nodev,			/* print */
887c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
897c478bd9Sstevel@tonic-gate 	fc_read,		/* read */
907c478bd9Sstevel@tonic-gate 	nodev,			/* write */
917c478bd9Sstevel@tonic-gate 	fc_ioctl,		/* ioctl */
927c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
937c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
947c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
957c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
967c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
977c478bd9Sstevel@tonic-gate 	NULL,			/* streamtab  */
987c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static struct dev_ops fcode_ops = {
1027c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1037c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
1047c478bd9Sstevel@tonic-gate 	fc_info,		/* info */
1057c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1067c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1077c478bd9Sstevel@tonic-gate 	fc_attach,		/* attach */
1087c478bd9Sstevel@tonic-gate 	fc_detach,		/* detach */
1097c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1107c478bd9Sstevel@tonic-gate 	&fc_cb_ops,		/* driver operations */
111*19397407SSherry Moore 	NULL,			/* bus operations */
112*19397407SSherry Moore 	NULL,			/* power */
113*19397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1207c478bd9Sstevel@tonic-gate 	&mod_driverops,
121*19397407SSherry Moore 	"FCode driver",
1227c478bd9Sstevel@tonic-gate 	&fcode_ops
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1267c478bd9Sstevel@tonic-gate 	MODREV_1,
1277c478bd9Sstevel@tonic-gate 	&modldrv,
1287c478bd9Sstevel@tonic-gate 	NULL
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #ifndef	lint
132447e4a63Spetede char _depends_on[] = "misc/fcodem";
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
1367c478bd9Sstevel@tonic-gate _init(void)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	int	error;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	mutex_init(&fc_open_lock, NULL, MUTEX_DRIVER, NULL);
1417c478bd9Sstevel@tonic-gate 	cv_init(&fc_open_cv, NULL, CV_DRIVER, NULL);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
1447c478bd9Sstevel@tonic-gate 	if (error != 0) {
1457c478bd9Sstevel@tonic-gate 		mutex_destroy(&fc_open_lock);
1467c478bd9Sstevel@tonic-gate 		cv_destroy(&fc_open_cv);
1477c478bd9Sstevel@tonic-gate 		return (error);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	return (0);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate int
1547c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
1607c478bd9Sstevel@tonic-gate _fini(void)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int	error;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1657c478bd9Sstevel@tonic-gate 	if (error != 0) {
1667c478bd9Sstevel@tonic-gate 		return (error);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	mutex_destroy(&fc_open_lock);
1707c478bd9Sstevel@tonic-gate 	cv_destroy(&fc_open_cv);
1717c478bd9Sstevel@tonic-gate 	return (0);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static dev_info_t *fc_dip;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1777c478bd9Sstevel@tonic-gate static int
1787c478bd9Sstevel@tonic-gate fc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	switch (infocmd) {
1837c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1847c478bd9Sstevel@tonic-gate 		*result = (void *)fc_dip;
1857c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
1867c478bd9Sstevel@tonic-gate 		break;
1877c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1887c478bd9Sstevel@tonic-gate 		/* All dev_t's map to the same, single instance */
1897c478bd9Sstevel@tonic-gate 		*result = (void *)0;
1907c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
1917c478bd9Sstevel@tonic-gate 		break;
1927c478bd9Sstevel@tonic-gate 	default:
1937c478bd9Sstevel@tonic-gate 		break;
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return (error);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static int
2007c478bd9Sstevel@tonic-gate fc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	switch (cmd) {
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
2077c478bd9Sstevel@tonic-gate 		fc_open_count = 0;
2087c478bd9Sstevel@tonic-gate 		fc_states = kmem_zalloc(
2097c478bd9Sstevel@tonic-gate 		    fc_max_opens * sizeof (struct fc_state), KM_SLEEP);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "fcode", S_IFCHR,
2127c478bd9Sstevel@tonic-gate 		    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2137c478bd9Sstevel@tonic-gate 			kmem_free(fc_states,
2147c478bd9Sstevel@tonic-gate 			    fc_max_opens * sizeof (struct fc_state));
2157c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
2167c478bd9Sstevel@tonic-gate 		} else {
2177c478bd9Sstevel@tonic-gate 			fc_dip = dip;
2187c478bd9Sstevel@tonic-gate 			ddi_report_dev(dip);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 		break;
2237c478bd9Sstevel@tonic-gate 	default:
2247c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
2257c478bd9Sstevel@tonic-gate 		break;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	return (error);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static int
2327c478bd9Sstevel@tonic-gate fc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	switch (cmd) {
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
2397c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2407c478bd9Sstevel@tonic-gate 		fc_dip = NULL;
2417c478bd9Sstevel@tonic-gate 		kmem_free(fc_states, fc_max_opens * sizeof (struct fc_state));
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
2447c478bd9Sstevel@tonic-gate 		break;
2457c478bd9Sstevel@tonic-gate 	default:
2467c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
2477c478bd9Sstevel@tonic-gate 		break;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	return (error);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * Allow multiple opens by tweaking the dev_t such that it looks like each
2557c478bd9Sstevel@tonic-gate  * open is getting a different minor device.  Each minor gets a separate
2567c478bd9Sstevel@tonic-gate  * entry in the fc_states[] table.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2597c478bd9Sstevel@tonic-gate static int
2607c478bd9Sstevel@tonic-gate fc_open(dev_t *devp, int flag, int otyp, cred_t *credp)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	int m;
2637c478bd9Sstevel@tonic-gate 	struct fc_state *st;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (getminor(*devp) != 0)
2667c478bd9Sstevel@tonic-gate 		return (EINVAL);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	mutex_enter(&fc_open_lock);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	while (fc_open_count >= fc_max_opens)  {
2717c478bd9Sstevel@tonic-gate 		/*
2727c478bd9Sstevel@tonic-gate 		 * maximum open instance reached, wait for a close
2737c478bd9Sstevel@tonic-gate 		 */
2747c478bd9Sstevel@tonic-gate 		FC_DEBUG0(1, CE_WARN,
2757c478bd9Sstevel@tonic-gate 		"fcode: Maximum fcode open reached, waiting for exit\n");
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&fc_open_cv, &fc_open_lock) == 0) {
2787c478bd9Sstevel@tonic-gate 			mutex_exit(&fc_open_lock);
2797c478bd9Sstevel@tonic-gate 			return (EINTR);
2807c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	fc_open_count++;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	for (m = 0, st = fc_states; m < fc_max_opens; m++, st++) {
2867c478bd9Sstevel@tonic-gate 		if (FC_STATE_ACTIVE(st->state))
2877c478bd9Sstevel@tonic-gate 			continue;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		st->state = FC_STATE_OPEN;
2907c478bd9Sstevel@tonic-gate 		st->req = 0;
2917c478bd9Sstevel@tonic-gate 		break;	/* It's ours. */
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	mutex_exit(&fc_open_lock);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	ASSERT(m < fc_max_opens);
2967c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), (minor_t)(m + 1));
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	FC_DEBUG2(9, CE_CONT, "fc_open: open count = %d (%d)\n",
2997c478bd9Sstevel@tonic-gate 	    fc_open_count, m + 1);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	return (0);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3057c478bd9Sstevel@tonic-gate static int
3067c478bd9Sstevel@tonic-gate fc_close(dev_t dev, int flag, int otype, cred_t *cred_p)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	struct fc_state *st;
3097c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - 1;
3107c478bd9Sstevel@tonic-gate 	struct fc_request *fp;
3117c478bd9Sstevel@tonic-gate 	struct fc_client_interface *cp;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	st = fc_states + m;
3147c478bd9Sstevel@tonic-gate 	ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/*
3177c478bd9Sstevel@tonic-gate 	 * The close indicates we're done with this request.
3187c478bd9Sstevel@tonic-gate 	 * If we haven't validated this request, then something
3197c478bd9Sstevel@tonic-gate 	 * bad may have happened (ie: perhaps the user program was
3207c478bd9Sstevel@tonic-gate 	 * killed), so we should invalidate it, then close the session.
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (st->state == FC_STATE_READ_DONE) {
3247c478bd9Sstevel@tonic-gate 		fp = st->req;
3257c478bd9Sstevel@tonic-gate 		fp->error = FC_ERROR;
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (st->state > FC_STATE_READ_DONE) {
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		cp = kmem_zalloc(sizeof (struct fc_client_interface), KM_SLEEP);
3317c478bd9Sstevel@tonic-gate 		fp = st->req;
3327c478bd9Sstevel@tonic-gate 		ASSERT(fp);
3337c478bd9Sstevel@tonic-gate 		ASSERT(fp->ap_ops);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		if (st->state != FC_STATE_VALIDATED) {
3367c478bd9Sstevel@tonic-gate 			FC_DEBUG0(1, CE_CONT,
3377c478bd9Sstevel@tonic-gate 			    "fc_close: Send invalidate cmd\n");
3387c478bd9Sstevel@tonic-gate 			cp->svc_name = fc_ptr2cell(FC_SVC_INVALIDATE);
3397c478bd9Sstevel@tonic-gate 			(void) fp->ap_ops(fp->ap_dip, fp->handle, cp);
3406d22b733Sdhain 			if ((st->state != FC_STATE_ERROR_SET) ||
3416d22b733Sdhain 			    (fp->error == FC_SUCCESS)) {
3427c478bd9Sstevel@tonic-gate 				fp->error = FC_ERROR;
3437c478bd9Sstevel@tonic-gate 			}
3446d22b733Sdhain 			/*
3456d22b733Sdhain 			 * else - fp->error already set by userland interpreter
3466d22b733Sdhain 			 */
3476d22b733Sdhain 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		bzero(cp, sizeof (struct fc_client_interface));
3507c478bd9Sstevel@tonic-gate 		FC_DEBUG0(9, CE_CONT, "fc_close: Sending exit cmd\n");
3517c478bd9Sstevel@tonic-gate 		cp->svc_name = fc_ptr2cell(FC_SVC_EXIT);
3527c478bd9Sstevel@tonic-gate 		(void) fp->ap_ops(fp->ap_dip, fp->handle, cp);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		kmem_free(cp, sizeof (struct fc_client_interface));
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * Mark the request as done ...
3597c478bd9Sstevel@tonic-gate 	 */
3607c478bd9Sstevel@tonic-gate 	if ((fp = st->req) != NULL)
3617c478bd9Sstevel@tonic-gate 		fc_finish_request(fp);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
3647c478bd9Sstevel@tonic-gate 	 * rectify count and signal any waiters
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	mutex_enter(&fc_open_lock);
3677c478bd9Sstevel@tonic-gate 	st->state = FC_STATE_INACTIVE;
3687c478bd9Sstevel@tonic-gate 	st->req = 0;
3697c478bd9Sstevel@tonic-gate 	FC_DEBUG2(9, CE_CONT, "fc_close: open count = %d (%d)\n",
3707c478bd9Sstevel@tonic-gate 	    fc_open_count, m + 1);
3717c478bd9Sstevel@tonic-gate 	if (fc_open_count >= fc_max_opens) {
3727c478bd9Sstevel@tonic-gate 		cv_broadcast(&fc_open_cv);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	fc_open_count--;
3757c478bd9Sstevel@tonic-gate 	mutex_exit(&fc_open_lock);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	return (0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3817c478bd9Sstevel@tonic-gate static int
3827c478bd9Sstevel@tonic-gate fc_read(dev_t dev, struct uio *uio, cred_t *cred)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	struct fc_state *st;
3857c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - 1;
3867c478bd9Sstevel@tonic-gate 	struct fc_request *fp;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	st = fc_states + m;
3897c478bd9Sstevel@tonic-gate 	ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * Wait for a internal request for the interpreter
3937c478bd9Sstevel@tonic-gate 	 * and sleep till one arrives.  When one arrives,
3947c478bd9Sstevel@tonic-gate 	 * return from the read. (No data is actually returned).
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (st->state != FC_STATE_OPEN)  {
3987c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "fc_read: Wrong state (%d) for read\n",
3997c478bd9Sstevel@tonic-gate 		    st->state);
4007c478bd9Sstevel@tonic-gate 		return (EINVAL);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * Wait for a request, allowing the wait to be interrupted.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	if ((fp = fc_get_request()) == NULL)
4077c478bd9Sstevel@tonic-gate 		return (EINTR);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	FC_DEBUG1(3, CE_CONT, "fc_read: request fp: %p\n", fp);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * Update our state and store the request pointer.
4137c478bd9Sstevel@tonic-gate 	 */
4147c478bd9Sstevel@tonic-gate 	mutex_enter(&fc_open_lock);
4157c478bd9Sstevel@tonic-gate 	st->req = fp;
4167c478bd9Sstevel@tonic-gate 	st->state = FC_STATE_READ_DONE;
4177c478bd9Sstevel@tonic-gate 	mutex_exit(&fc_open_lock);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	return (0);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4237c478bd9Sstevel@tonic-gate static int
4247c478bd9Sstevel@tonic-gate fc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	struct fc_state *st;
4277c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - 1;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (m >= fc_max_opens) {
4307c478bd9Sstevel@tonic-gate 		return (EINVAL);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	st = fc_states + m;
4347c478bd9Sstevel@tonic-gate 	ASSERT(FC_STATE_ACTIVE(st->state));
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	switch (cmd) {
4377c478bd9Sstevel@tonic-gate 	case FC_GET_PARAMETERS:
4387c478bd9Sstevel@tonic-gate 		/*
4397c478bd9Sstevel@tonic-gate 		 * This should be the first command and is used to
4407c478bd9Sstevel@tonic-gate 		 * return data about the request, including the
4417c478bd9Sstevel@tonic-gate 		 * the fcode address and size and the unit address
4427c478bd9Sstevel@tonic-gate 		 * of the new child.  The fcode offset,size can later
4437c478bd9Sstevel@tonic-gate 		 * be used as an offset in an mmap request to allow
4447c478bd9Sstevel@tonic-gate 		 * the fcode to be mapped in.
4457c478bd9Sstevel@tonic-gate 		 */
4467c478bd9Sstevel@tonic-gate 		return (fc_get_parameters(dev, arg, mode, credp, rvalp));
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	case FC_GET_MY_ARGS:
4497c478bd9Sstevel@tonic-gate 		/*
4507c478bd9Sstevel@tonic-gate 		 * Get the inital setting of my-args.  This should be done
4517c478bd9Sstevel@tonic-gate 		 * after FC_GET_PARAMETERS.
4527c478bd9Sstevel@tonic-gate 		 */
4537c478bd9Sstevel@tonic-gate 		return (fc_get_my_args(dev, arg, mode, credp, rvalp));
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	case FC_RUN_PRIV:
4567c478bd9Sstevel@tonic-gate 		/*
4577c478bd9Sstevel@tonic-gate 		 * Run a priveledged op on behalf of the interpreter,
4587c478bd9Sstevel@tonic-gate 		 * or download device tree data from the interpreter.
4597c478bd9Sstevel@tonic-gate 		 */
4607c478bd9Sstevel@tonic-gate 		return (fc_run_priv(dev, arg, mode, credp, rvalp));
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	case FC_VALIDATE:
4637c478bd9Sstevel@tonic-gate 		/*
4647c478bd9Sstevel@tonic-gate 		 * The interpreter is done, mark state as done, validating
4657c478bd9Sstevel@tonic-gate 		 * the data downloaded into the kernel.
4667c478bd9Sstevel@tonic-gate 		 */
4677c478bd9Sstevel@tonic-gate 		return (fc_validate(dev, arg, mode, credp, rvalp));
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	case FC_GET_FCODE_DATA:
4707c478bd9Sstevel@tonic-gate 		/*
4717c478bd9Sstevel@tonic-gate 		 * Copy out device fcode to user buffer.
4727c478bd9Sstevel@tonic-gate 		 */
4737c478bd9Sstevel@tonic-gate 		return (fc_get_fcode(dev, arg, mode, credp, rvalp));
4747c478bd9Sstevel@tonic-gate 
4756d22b733Sdhain 
4766d22b733Sdhain 	case FC_SET_FCODE_ERROR:
4776d22b733Sdhain 		/*
4786d22b733Sdhain 		 * Copy in interpreter error status
4796d22b733Sdhain 		 */
4806d22b733Sdhain 		return (fc_set_fcode_error(dev, arg, mode, credp, rvalp));
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	/*
4837c478bd9Sstevel@tonic-gate 	 * Invalid ioctl command
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 	return (ENOTTY);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * fc_get_parameters:  Get information about the current request.
4907c478bd9Sstevel@tonic-gate  * The input 'arg' is a pointer to 'struct fc_parameters' which
4917c478bd9Sstevel@tonic-gate  * we write back to the caller with the information from the req
4927c478bd9Sstevel@tonic-gate  * structure.
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4967c478bd9Sstevel@tonic-gate static int
4977c478bd9Sstevel@tonic-gate fc_get_parameters(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	struct fc_state *st;
5007c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - 1;
5017c478bd9Sstevel@tonic-gate 	fco_handle_t rp;
5027c478bd9Sstevel@tonic-gate 	struct fc_parameters *fcp;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	st = fc_states + m;
5057c478bd9Sstevel@tonic-gate 	ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/*
5087c478bd9Sstevel@tonic-gate 	 * It's an error if we're not in state FC_STATE_READ_DONE
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (st->state != FC_STATE_READ_DONE) {
5127c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "fc_ioctl: fc_get_parameters: "
5137c478bd9Sstevel@tonic-gate 		    "wrong state (%d)\n", st->state);
5147c478bd9Sstevel@tonic-gate 		return (EINVAL);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	ASSERT(st->req != NULL);
5187c478bd9Sstevel@tonic-gate 	rp = st->req->handle;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_get_parameters fp: %p\n", st->req);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * Create and copyout the attachment point ihandle,
5247c478bd9Sstevel@tonic-gate 	 * the fcode kaddr,len and the unit address.
5257c478bd9Sstevel@tonic-gate 	 * Note how we treat ihandles and phandles (they are the same thing
5267c478bd9Sstevel@tonic-gate 	 * only accross this interface ... a dev_info_t *.)
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	fcp = kmem_zalloc(sizeof (struct fc_parameters), KM_SLEEP);
5297c478bd9Sstevel@tonic-gate 	fcp->fcode_size = rp->fcode_size;
5307c478bd9Sstevel@tonic-gate 	(void) strncpy(fcp->unit_address, rp->unit_address,
5317c478bd9Sstevel@tonic-gate 	    sizeof (fcp->unit_address) - 1);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/*
5347c478bd9Sstevel@tonic-gate 	 * XXX - APA This needs to be made more bus independant.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	if (rp->bus_args) {
5377c478bd9Sstevel@tonic-gate 		bcopy(rp->bus_args, &fcp->config_address, sizeof (int));
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		FC_DEBUG1(3, CE_CONT, "fc_ioctl: config_address=%x\n",
5407c478bd9Sstevel@tonic-gate 		    fcp->config_address);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	} else {
5437c478bd9Sstevel@tonic-gate 		FC_DEBUG0(3, CE_CONT, "fc_ioctl: fc_get_parameters "
5447c478bd9Sstevel@tonic-gate 		    "There are no bus specific arguments\n");
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	if (copyout(fcp, (void *)arg, sizeof (struct fc_parameters)) == -1) {
5477c478bd9Sstevel@tonic-gate 		kmem_free(fcp, sizeof (struct fc_parameters));
5487c478bd9Sstevel@tonic-gate 		return (EFAULT);
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 	kmem_free(fcp, sizeof (struct fc_parameters));
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * Update our state
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	mutex_enter(&fc_open_lock);
5567c478bd9Sstevel@tonic-gate 	st->state = FC_STATE_IN_PROGRESS;
5577c478bd9Sstevel@tonic-gate 	mutex_exit(&fc_open_lock);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	return (0);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate  * fc_get_my_args:  Get the initial setting for my-args.
5647c478bd9Sstevel@tonic-gate  * The input 'arg' is a pointer where the my-arg string is written
5657c478bd9Sstevel@tonic-gate  * to. The string is NULL terminated.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5697c478bd9Sstevel@tonic-gate static int
5707c478bd9Sstevel@tonic-gate fc_get_my_args(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	struct fc_state *st;
5737c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - 1;
5747c478bd9Sstevel@tonic-gate 	fco_handle_t rp;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	st = fc_states + m;
5777c478bd9Sstevel@tonic-gate 	ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * It's an error if we're not in state FC_STATE_READ_DONE
5817c478bd9Sstevel@tonic-gate 	 */
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (st->state != FC_STATE_IN_PROGRESS) {
5847c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "fc_ioctl: fc_get_my_args: "
5857c478bd9Sstevel@tonic-gate 		    "wrong state (%d)\n", st->state);
5867c478bd9Sstevel@tonic-gate 		return (EINVAL);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	ASSERT(st->req != NULL);
5907c478bd9Sstevel@tonic-gate 	rp = st->req->handle;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_get_my_args fp: %p\n", st->req);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (rp->my_args == NULL) {
5957c478bd9Sstevel@tonic-gate 		FC_DEBUG0(3, CE_CONT, "fc_ioctl: fc_get_my_args "
5967c478bd9Sstevel@tonic-gate 		    "There are no bus specific my-args\n");
5977c478bd9Sstevel@tonic-gate 		return (EINVAL);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (strlen(rp->my_args) > FC_GET_MY_ARGS_BUFLEN) {
6017c478bd9Sstevel@tonic-gate 		FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_get_my_args "
6027c478bd9Sstevel@tonic-gate 		    "my-args is larger than %d\n", FC_GET_MY_ARGS_BUFLEN);
6037c478bd9Sstevel@tonic-gate 		return (EINVAL);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (copyout(rp->my_args, (void *)arg, strlen(rp->my_args) + 1) == -1) {
6087c478bd9Sstevel@tonic-gate 		return (EFAULT);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	return (0);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6157c478bd9Sstevel@tonic-gate static int
6167c478bd9Sstevel@tonic-gate fc_run_priv(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	struct fc_state *st;
6197c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - 1;
6207c478bd9Sstevel@tonic-gate 	struct fc_request *fp;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	struct fc_client_interface tc, *cp, *ap;
6237c478bd9Sstevel@tonic-gate 	size_t csize;
6247c478bd9Sstevel@tonic-gate 	int nresults, nargs, error;
6257c478bd9Sstevel@tonic-gate 	char *name;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	ap = (struct fc_client_interface *)arg;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	st = fc_states + m;
6307c478bd9Sstevel@tonic-gate 	ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * It's an error if we're not in state FC_STATE_IN_PROGRESS
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (st->state != FC_STATE_IN_PROGRESS) {
6377c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "fc_ioctl: fc_run_priv: wrong state (%d)\n",
6387c478bd9Sstevel@tonic-gate 		    st->state);
6397c478bd9Sstevel@tonic-gate 		return (EINVAL);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * Get the first three cells to figure out how large the buffer
6447c478bd9Sstevel@tonic-gate 	 * needs to be; allocate it and copy it in. The array is variable
6457c478bd9Sstevel@tonic-gate 	 * sized based on the fixed portion plus the given number of arg.
6467c478bd9Sstevel@tonic-gate 	 * cells and given number of result cells.
6477c478bd9Sstevel@tonic-gate 	 */
6487c478bd9Sstevel@tonic-gate 	if (copyin((void *)arg, &tc, 3 * sizeof (fc_cell_t))) {
6497c478bd9Sstevel@tonic-gate 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv "
6507c478bd9Sstevel@tonic-gate 		    "fault copying in first 2 cells from %p\n", arg);
6517c478bd9Sstevel@tonic-gate 		return (EFAULT);
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * XXX We should probably limit #args and #results to something
6567c478bd9Sstevel@tonic-gate 	 * reasonable without blindly copying it in.
6577c478bd9Sstevel@tonic-gate 	 */
6587c478bd9Sstevel@tonic-gate 	nresults = fc_cell2int(tc.nresults); /* save me for later */
6597c478bd9Sstevel@tonic-gate 	nargs = fc_cell2int(tc.nargs);
6607c478bd9Sstevel@tonic-gate 	csize = (FCC_FIXED_CELLS + nargs + nresults) * sizeof (fc_cell_t);
6617c478bd9Sstevel@tonic-gate 	cp = kmem_zalloc(csize, KM_SLEEP);
6627c478bd9Sstevel@tonic-gate 	/*
6637c478bd9Sstevel@tonic-gate 	 * Don't bother copying in the result cells
6647c478bd9Sstevel@tonic-gate 	 */
6657c478bd9Sstevel@tonic-gate 	if (copyin((void *)arg, cp, csize - (nresults * sizeof (fc_cell_t)))) {
6667c478bd9Sstevel@tonic-gate 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv "
6677c478bd9Sstevel@tonic-gate 		    "fault copying in argument array from %p\n", arg);
6687c478bd9Sstevel@tonic-gate 		kmem_free(cp, csize);
6697c478bd9Sstevel@tonic-gate 		return (EFAULT);
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 	/*
6727c478bd9Sstevel@tonic-gate 	 * reset the error fields.
6737c478bd9Sstevel@tonic-gate 	 */
6747c478bd9Sstevel@tonic-gate 	cp->error = fc_int2cell(0);
6757c478bd9Sstevel@tonic-gate 	cp->priv_error = fc_int2cell(0);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/*
6787c478bd9Sstevel@tonic-gate 	 * Copy in the service name into our copy of the array.
6797c478bd9Sstevel@tonic-gate 	 * Later, be careful not to copy out the svc name pointer.
6807c478bd9Sstevel@tonic-gate 	 */
6817c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(FC_SVC_NAME_LEN, KM_SLEEP);
6827c478bd9Sstevel@tonic-gate 	if (copyinstr(fc_cell2ptr(cp->svc_name), name,
6837c478bd9Sstevel@tonic-gate 	    FC_SVC_NAME_LEN - 1, NULL))  {
6847c478bd9Sstevel@tonic-gate 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv "
6857c478bd9Sstevel@tonic-gate 		    "fault copying in service name from %p\n",
6867c478bd9Sstevel@tonic-gate 		    fc_cell2ptr(cp->svc_name));
6877c478bd9Sstevel@tonic-gate 		kmem_free(cp, csize);
6887c478bd9Sstevel@tonic-gate 		kmem_free(name, FC_SVC_NAME_LEN);
6897c478bd9Sstevel@tonic-gate 		return (EFAULT);
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	cp->svc_name = fc_ptr2cell(name);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	FC_DEBUG3(7, CE_CONT, "fc_ioctl: fc_run_priv: "
6947c478bd9Sstevel@tonic-gate 	    "service name <%s> nargs %d nresults %d\n",
6957c478bd9Sstevel@tonic-gate 	    name, fc_cell2int(cp->nargs), fc_cell2int(cp->nresults));
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/*
6987c478bd9Sstevel@tonic-gate 	 * Call the driver's ops function to provide the service
6997c478bd9Sstevel@tonic-gate 	 */
7007c478bd9Sstevel@tonic-gate 	fp = st->req;
7017c478bd9Sstevel@tonic-gate 	ASSERT(fp->ap_ops);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	error = fp->ap_ops(fp->ap_dip, fp->handle, cp);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * If error is non-zero, we need to log the error and
7077c478bd9Sstevel@tonic-gate 	 * the service name, and write back the error to the
7087c478bd9Sstevel@tonic-gate 	 * callers argument array.
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (error || cp->error) {
7127c478bd9Sstevel@tonic-gate 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv: "
7137c478bd9Sstevel@tonic-gate 		    "service name <%s> was unserviced\n", name);
7147c478bd9Sstevel@tonic-gate 		cp->error = FC_ERR_SVC_NAME;
7157c478bd9Sstevel@tonic-gate 		cp->nresults = fc_int2cell(0);
7167c478bd9Sstevel@tonic-gate 		error = copyout(&cp->error, &ap->error, sizeof (fc_cell_t));
7177c478bd9Sstevel@tonic-gate 		error |= copyout(&cp->nresults, &ap->nresults,
7187c478bd9Sstevel@tonic-gate 		    sizeof (fc_cell_t));
7197c478bd9Sstevel@tonic-gate 		kmem_free(cp, csize);
7207c478bd9Sstevel@tonic-gate 		kmem_free(name, FC_SVC_NAME_LEN);
7217c478bd9Sstevel@tonic-gate 		if (error) {
7227c478bd9Sstevel@tonic-gate 			FC_DEBUG0(1, CE_CONT, "fc_ioctl: fc_run_priv "
7237c478bd9Sstevel@tonic-gate 			    "fault copying out error result\n");
7247c478bd9Sstevel@tonic-gate 			return (EFAULT);
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 		return (0);
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (cp->priv_error) {
7307c478bd9Sstevel@tonic-gate 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv: "
7317c478bd9Sstevel@tonic-gate 		    "service name <%s> caused a priv violation\n", name);
7327c478bd9Sstevel@tonic-gate 		cp->priv_error = FC_PRIV_ERROR;
7337c478bd9Sstevel@tonic-gate 		cp->nresults = fc_int2cell(0);
7347c478bd9Sstevel@tonic-gate 		error = copyout(&cp->error, &ap->error, sizeof (fc_cell_t));
7357c478bd9Sstevel@tonic-gate 		error |= copyout(&cp->priv_error, &ap->priv_error,
7367c478bd9Sstevel@tonic-gate 		    sizeof (fc_cell_t));
7377c478bd9Sstevel@tonic-gate 		error |= copyout(&cp->nresults, &ap->nresults,
7387c478bd9Sstevel@tonic-gate 		    sizeof (fc_cell_t));
7397c478bd9Sstevel@tonic-gate 		kmem_free(cp, csize);
7407c478bd9Sstevel@tonic-gate 		kmem_free(name, FC_SVC_NAME_LEN);
7417c478bd9Sstevel@tonic-gate 		if (error) {
7427c478bd9Sstevel@tonic-gate 			FC_DEBUG0(1, CE_CONT, "fc_ioctl: fc_run_priv "
7437c478bd9Sstevel@tonic-gate 			    "fault copying out priv error result\n");
7447c478bd9Sstevel@tonic-gate 			return (EFAULT);
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 		return (0);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * We believe we have a successful result at this point, thus we
7517c478bd9Sstevel@tonic-gate 	 * have to copy out the actual number of result cells to be
7527c478bd9Sstevel@tonic-gate 	 * returned, the two error fields and each of the results.
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) > nresults)
7567c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "fc_ioctl: fc_run_priv: "
7577c478bd9Sstevel@tonic-gate 		    "results (from ops function) overflow\n");
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	error = copyout(&cp->nresults, &ap->nresults, sizeof (fc_cell_t));
7607c478bd9Sstevel@tonic-gate 	error |= copyout(&cp->error, &ap->error, sizeof (fc_cell_t));
7617c478bd9Sstevel@tonic-gate 	error |= copyout(&cp->priv_error, &ap->priv_error, sizeof (fc_cell_t));
7627c478bd9Sstevel@tonic-gate 	if ((error == 0) && cp->nresults)
7637c478bd9Sstevel@tonic-gate 		error |= copyout(&fc_result(cp, 0), &(ap->v[nargs]),
7647c478bd9Sstevel@tonic-gate 		    cp->nresults * sizeof (fc_cell_t));
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	kmem_free(cp, csize);
7677c478bd9Sstevel@tonic-gate 	kmem_free(name, FC_SVC_NAME_LEN);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if (error) {
7707c478bd9Sstevel@tonic-gate 		FC_DEBUG0(1, CE_CONT, "fc_ioctl: fc_run_priv "
7717c478bd9Sstevel@tonic-gate 		    "fault copying out (good) results\n");
7727c478bd9Sstevel@tonic-gate 		return (EFAULT);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 	return (0);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7787c478bd9Sstevel@tonic-gate static int
7797c478bd9Sstevel@tonic-gate fc_validate(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	struct fc_state *st;
7827c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - 1;
7837c478bd9Sstevel@tonic-gate 	struct fc_request *fp;
7847c478bd9Sstevel@tonic-gate 	struct fc_client_interface *cp;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	st = fc_states + m;
7877c478bd9Sstevel@tonic-gate 	ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * It's an error if we're not in state FC_STATE_IN_PROGRESS
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	if (st->state != FC_STATE_IN_PROGRESS) {
7937c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "fc_ioctl: fc_validate: wrong state (%d)\n",
7947c478bd9Sstevel@tonic-gate 		    st->state);
7957c478bd9Sstevel@tonic-gate 		return (EINVAL);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	FC_DEBUG0(2, CE_CONT, "fc_ioctl: fc_validate: Sending validate cmd\n");
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/*
8017c478bd9Sstevel@tonic-gate 	 * Send a "validate" command down the line.
8027c478bd9Sstevel@tonic-gate 	 * The command has no arguments and no results.
8037c478bd9Sstevel@tonic-gate 	 */
8047c478bd9Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (struct fc_client_interface), KM_SLEEP);
8057c478bd9Sstevel@tonic-gate 	cp->svc_name = fc_ptr2cell(FC_SVC_VALIDATE);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	fp = st->req;
8087c478bd9Sstevel@tonic-gate 	ASSERT(fp->ap_ops);
8097c478bd9Sstevel@tonic-gate 	(void) fp->ap_ops(fp->ap_dip, fp->handle, cp);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	kmem_free(cp, sizeof (struct fc_client_interface));
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * Update our state.
8157c478bd9Sstevel@tonic-gate 	 */
8167c478bd9Sstevel@tonic-gate 	mutex_enter(&fc_open_lock);
8177c478bd9Sstevel@tonic-gate 	st->state = FC_STATE_VALIDATED;
8187c478bd9Sstevel@tonic-gate 	mutex_exit(&fc_open_lock);
8197c478bd9Sstevel@tonic-gate 	return (0);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate  * fc_get_fcode:  Copy out device fcode to user buffer.
8247c478bd9Sstevel@tonic-gate  * The input 'arg' is a pointer to 'fc_fcode_info_t' which
8257c478bd9Sstevel@tonic-gate  * should have fcode_size field set.  The fcode_ptr field is a
8267c478bd9Sstevel@tonic-gate  * pointer to a user buffer of fcode_size.
8277c478bd9Sstevel@tonic-gate  */
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8307c478bd9Sstevel@tonic-gate static int
8317c478bd9Sstevel@tonic-gate fc_get_fcode(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	struct fc_state *st;
8347c478bd9Sstevel@tonic-gate 	int m = (int)getminor(dev) - 1;
8357c478bd9Sstevel@tonic-gate 	fco_handle_t rp;
8367c478bd9Sstevel@tonic-gate 	struct fc_fcode_info fcode_info;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	st = fc_states + m;
8397c478bd9Sstevel@tonic-gate 	ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	ASSERT(st->req != NULL);
8427c478bd9Sstevel@tonic-gate 	rp = st->req->handle;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_get_fcode fp: %p\n", st->req);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * Get the fc_fcode_info structure from userland.
8487c478bd9Sstevel@tonic-gate 	 */
8497c478bd9Sstevel@tonic-gate 	if (copyin((void *)arg, &fcode_info, sizeof (fc_fcode_info_t))) {
8507c478bd9Sstevel@tonic-gate 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_get_fcode "
8517c478bd9Sstevel@tonic-gate 		    "fault copying in fcode_info from %p\n", arg);
8527c478bd9Sstevel@tonic-gate 		return (EFAULT);
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	/*
8567c478bd9Sstevel@tonic-gate 	 * Validate that buffer size is what we expect.
8577c478bd9Sstevel@tonic-gate 	 */
8587c478bd9Sstevel@tonic-gate 	if (fcode_info.fcode_size != rp->fcode_size) {
8597c478bd9Sstevel@tonic-gate 		FC_DEBUG2(1, CE_CONT, "fc_ioctl: fc_get_fcode "
8607c478bd9Sstevel@tonic-gate 		    "requested size (0x%x) doesn't match real size (0x%x)\n",
8617c478bd9Sstevel@tonic-gate 		    fcode_info.fcode_size, rp->fcode_size);
8627c478bd9Sstevel@tonic-gate 		return (EINVAL);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/*
8667c478bd9Sstevel@tonic-gate 	 * Copyout the fcode.
8677c478bd9Sstevel@tonic-gate 	 */
8687c478bd9Sstevel@tonic-gate 	if (copyout(rp->fcode, fcode_info.fcode_ptr, rp->fcode_size) == -1) {
8697c478bd9Sstevel@tonic-gate 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_get_fcode "
8707c478bd9Sstevel@tonic-gate 		    "fault copying out fcode to %p\n", fcode_info.fcode_ptr);
8717c478bd9Sstevel@tonic-gate 		return (EFAULT);
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	return (0);
8757c478bd9Sstevel@tonic-gate }
8766d22b733Sdhain 
8776d22b733Sdhain /*
8786d22b733Sdhain  * fc_set_fcode_error:  Copy in	fcode error.
8796d22b733Sdhain  * The input 'arg' is a pointer to int which
8806d22b733Sdhain  * should have the appropriate error code set.
8816d22b733Sdhain  */
8826d22b733Sdhain 
8836d22b733Sdhain /*ARGSUSED*/
8846d22b733Sdhain static int
8856d22b733Sdhain fc_set_fcode_error(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
8866d22b733Sdhain {
8876d22b733Sdhain 	struct fc_state *st;
8886d22b733Sdhain 	struct fc_request *fp;
8896d22b733Sdhain 	int m = (int)getminor(dev) - 1;
8906d22b733Sdhain 	int status;
8916d22b733Sdhain 
8926d22b733Sdhain 	st = fc_states + m;
8936d22b733Sdhain 	ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
8946d22b733Sdhain 
8956d22b733Sdhain 	ASSERT(st->req != NULL);
8966d22b733Sdhain 	fp = st->req;
8976d22b733Sdhain 
8986d22b733Sdhain 	FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_set_fcode_error fp: %p\n", fp);
8996d22b733Sdhain 
9006d22b733Sdhain 	/*
9016d22b733Sdhain 	 * Get the error code from userland.
9026d22b733Sdhain 	 * We expect these to be negative values to denote
9036d22b733Sdhain 	 * interpreter errors.
9046d22b733Sdhain 	 */
9056d22b733Sdhain 	if (copyin((void *)arg, &status, sizeof (int))) {
9066d22b733Sdhain 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_set_fcode_error "
9076d22b733Sdhain 		    "fault copying in status from %p\n", arg);
9086d22b733Sdhain 		return (EFAULT);
9096d22b733Sdhain 	}
9106d22b733Sdhain 
9116d22b733Sdhain 	if (!FC_ERROR_VALID(status)) {
9126d22b733Sdhain 		FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_set_fcode_error "
9136d22b733Sdhain 		    "invalid error code specified %i\n", status);
9146d22b733Sdhain 		return (EINVAL);
9156d22b733Sdhain 	}
9166d22b733Sdhain 	fp->error = status;
9176d22b733Sdhain 	mutex_enter(&fc_open_lock);
9186d22b733Sdhain 	st->state = FC_STATE_ERROR_SET;
9196d22b733Sdhain 	mutex_exit(&fc_open_lock);
9206d22b733Sdhain 
9216d22b733Sdhain 	return (0);
9226d22b733Sdhain }
923