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