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
519397407SSherry Moore * Common Development and Distribution License (the "License").
619397407SSherry Moore * 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 /*
2219397407SSherry 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 #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/conf.h>
297c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/open.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /* #define SBUSMEM_DEBUG */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
417c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate int sbusmem_debug_flag;
447c478bd9Sstevel@tonic-gate #define sbusmem_debug if (sbusmem_debug_flag) printf
457c478bd9Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate static void *sbusmem_state_head;
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate struct sbusmem_unit {
507c478bd9Sstevel@tonic-gate uint_t size;
517c478bd9Sstevel@tonic-gate uint_t pagesize;
527c478bd9Sstevel@tonic-gate dev_info_t *dip;
537c478bd9Sstevel@tonic-gate };
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static int sbmem_open(dev_t *, int, int, cred_t *);
567c478bd9Sstevel@tonic-gate static int sbmem_close(dev_t, int, int, struct cred *);
577c478bd9Sstevel@tonic-gate static int sbmem_read(dev_t, struct uio *, cred_t *);
587c478bd9Sstevel@tonic-gate static int sbmem_write(dev_t, struct uio *, cred_t *);
597c478bd9Sstevel@tonic-gate static int sbmem_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
607c478bd9Sstevel@tonic-gate size_t *, uint_t);
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static struct cb_ops sbmem_cb_ops = {
637c478bd9Sstevel@tonic-gate sbmem_open, /* open */
647c478bd9Sstevel@tonic-gate sbmem_close, /* close */
657c478bd9Sstevel@tonic-gate nodev, /* strategy */
667c478bd9Sstevel@tonic-gate nodev, /* print */
677c478bd9Sstevel@tonic-gate nodev, /* dump */
687c478bd9Sstevel@tonic-gate sbmem_read, /* read */
697c478bd9Sstevel@tonic-gate sbmem_write, /* write */
707c478bd9Sstevel@tonic-gate nodev, /* ioctl */
717c478bd9Sstevel@tonic-gate sbmem_devmap, /* devmap */
727c478bd9Sstevel@tonic-gate nodev, /* mmap */
737c478bd9Sstevel@tonic-gate ddi_devmap_segmap, /* segmap */
747c478bd9Sstevel@tonic-gate nochpoll, /* poll */
757c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
767c478bd9Sstevel@tonic-gate 0, /* streamtab */
777c478bd9Sstevel@tonic-gate D_NEW|D_MP|D_DEVMAP|D_HOTPLUG, /* Driver compatibility flag */
787c478bd9Sstevel@tonic-gate CB_REV, /* rev */
797c478bd9Sstevel@tonic-gate nodev, /* int (*cb_aread)() */
807c478bd9Sstevel@tonic-gate nodev /* int (*cb_awrite)() */
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate static int sbmem_attach(dev_info_t *, ddi_attach_cmd_t);
847c478bd9Sstevel@tonic-gate static int sbmem_detach(dev_info_t *, ddi_detach_cmd_t);
857c478bd9Sstevel@tonic-gate static int sbmem_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate static struct dev_ops sbmem_ops = {
887c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
897c478bd9Sstevel@tonic-gate 0, /* refcnt */
907c478bd9Sstevel@tonic-gate sbmem_info, /* get_dev_info */
917c478bd9Sstevel@tonic-gate nulldev, /* identify */
927c478bd9Sstevel@tonic-gate nulldev, /* probe */
937c478bd9Sstevel@tonic-gate sbmem_attach, /* attach */
947c478bd9Sstevel@tonic-gate sbmem_detach, /* detach */
957c478bd9Sstevel@tonic-gate nodev, /* reset */
967c478bd9Sstevel@tonic-gate &sbmem_cb_ops, /* driver operations */
977c478bd9Sstevel@tonic-gate (struct bus_ops *)0, /* bus operations */
9819397407SSherry Moore nulldev, /* power */
9919397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1037c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */
10419397407SSherry Moore "SBus memory driver", /* Name of module. */
1057c478bd9Sstevel@tonic-gate &sbmem_ops, /* driver ops */
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1097c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate static int sbmem_rw(dev_t, struct uio *, enum uio_rw, cred_t *);
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate int
_init(void)1157c478bd9Sstevel@tonic-gate _init(void)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate int error;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate if ((error = ddi_soft_state_init(&sbusmem_state_head,
1207c478bd9Sstevel@tonic-gate sizeof (struct sbusmem_unit), 1)) != 0) {
1217c478bd9Sstevel@tonic-gate return (error);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate if ((error = mod_install(&modlinkage)) != 0) {
1247c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&sbusmem_state_head);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate return (error);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate int
_fini(void)1307c478bd9Sstevel@tonic-gate _fini(void)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate int error;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate if ((error = mod_remove(&modlinkage)) == 0) {
1357c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&sbusmem_state_head);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate return (error);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1417c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate static int
sbmem_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1477c478bd9Sstevel@tonic-gate sbmem_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate struct sbusmem_unit *un;
1507c478bd9Sstevel@tonic-gate int error = DDI_FAILURE;
1517c478bd9Sstevel@tonic-gate int instance, ilen;
1527c478bd9Sstevel@tonic-gate uint_t size;
1537c478bd9Sstevel@tonic-gate char *ident;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate switch (cmd) {
1567c478bd9Sstevel@tonic-gate case DDI_ATTACH:
1577c478bd9Sstevel@tonic-gate instance = ddi_get_instance(devi);
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate size = ddi_getprop(DDI_DEV_T_NONE, devi,
1607c478bd9Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "size", -1);
1617c478bd9Sstevel@tonic-gate if (size == (uint_t)-1) {
1627c478bd9Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
1637c478bd9Sstevel@tonic-gate sbusmem_debug(
1647c478bd9Sstevel@tonic-gate "sbmem_attach%d: No size property\n", instance);
1657c478bd9Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
1667c478bd9Sstevel@tonic-gate break;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate struct regspec *rp = ddi_rnumber_to_regspec(devi, 0);
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate if (rp == NULL) {
1747c478bd9Sstevel@tonic-gate sbusmem_debug(
1757c478bd9Sstevel@tonic-gate "sbmem_attach%d: No reg property\n", instance);
1767c478bd9Sstevel@tonic-gate } else {
1777c478bd9Sstevel@tonic-gate sbusmem_debug(
1787c478bd9Sstevel@tonic-gate "sbmem_attach%d: slot 0x%x size 0x%x\n", instance,
1797c478bd9Sstevel@tonic-gate rp->regspec_bustype, rp->regspec_size);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
1837c478bd9Sstevel@tonic-gate
184a3282898Scth if (ddi_getlongprop(DDI_DEV_T_ANY, devi,
1857c478bd9Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "ident",
1867c478bd9Sstevel@tonic-gate (caddr_t)&ident, &ilen) != DDI_PROP_SUCCESS) {
1877c478bd9Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
1887c478bd9Sstevel@tonic-gate sbusmem_debug(
1897c478bd9Sstevel@tonic-gate "sbmem_attach%d: No ident property\n", instance);
1907c478bd9Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
1917c478bd9Sstevel@tonic-gate break;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(sbusmem_state_head,
1957c478bd9Sstevel@tonic-gate instance) != DDI_SUCCESS)
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate if ((un = ddi_get_soft_state(sbusmem_state_head,
1997c478bd9Sstevel@tonic-gate instance)) == NULL) {
2007c478bd9Sstevel@tonic-gate ddi_soft_state_free(sbusmem_state_head, instance);
2017c478bd9Sstevel@tonic-gate break;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, ident, S_IFCHR, instance,
205*78d71c09SToomas Soome DDI_PSEUDO, 0) == DDI_FAILURE) {
2067c478bd9Sstevel@tonic-gate kmem_free(ident, ilen);
2077c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2087c478bd9Sstevel@tonic-gate ddi_soft_state_free(sbusmem_state_head, instance);
2097c478bd9Sstevel@tonic-gate break;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate kmem_free(ident, ilen);
2127c478bd9Sstevel@tonic-gate un->dip = devi;
2137c478bd9Sstevel@tonic-gate un->size = size;
2147c478bd9Sstevel@tonic-gate un->pagesize = ddi_ptob(devi, 1);
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
2177c478bd9Sstevel@tonic-gate sbusmem_debug("sbmem_attach%d: dip 0x%p size 0x%x\n",
2187c478bd9Sstevel@tonic-gate instance, devi, size);
2197c478bd9Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate ddi_report_dev(devi);
2227c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
2237c478bd9Sstevel@tonic-gate break;
2247c478bd9Sstevel@tonic-gate case DDI_RESUME:
2257c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
2267c478bd9Sstevel@tonic-gate break;
2277c478bd9Sstevel@tonic-gate default:
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate return (error);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate static int
sbmem_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2347c478bd9Sstevel@tonic-gate sbmem_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate int instance;
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate switch (cmd) {
2397c478bd9Sstevel@tonic-gate case DDI_DETACH:
2407c478bd9Sstevel@tonic-gate instance = ddi_get_instance(devi);
2417c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2427c478bd9Sstevel@tonic-gate ddi_soft_state_free(sbusmem_state_head, instance);
2437c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate case DDI_SUSPEND:
2467c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
2527c478bd9Sstevel@tonic-gate static int
sbmem_open(dev_t * devp,int flag,int typ,cred_t * cred)2537c478bd9Sstevel@tonic-gate sbmem_open(dev_t *devp, int flag, int typ, cred_t *cred)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate int instance;
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate if (typ != OTYP_CHR)
2587c478bd9Sstevel@tonic-gate return (EINVAL);
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate instance = getminor(*devp);
2617c478bd9Sstevel@tonic-gate if (ddi_get_soft_state(sbusmem_state_head, instance) == NULL) {
2627c478bd9Sstevel@tonic-gate return (ENXIO);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate return (0);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2687c478bd9Sstevel@tonic-gate static int
sbmem_close(dev_t dev,int flag,int otyp,struct cred * cred)2697c478bd9Sstevel@tonic-gate sbmem_close(dev_t dev, int flag, int otyp, struct cred *cred)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR)
2727c478bd9Sstevel@tonic-gate return (EINVAL);
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate return (0);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate static int
sbmem_info(dev_info_t * dip __unused,ddi_info_cmd_t infocmd,void * arg,void ** result)278*78d71c09SToomas Soome sbmem_info(dev_info_t *dip __unused, ddi_info_cmd_t infocmd, void *arg,
279*78d71c09SToomas Soome void **result)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate int instance, error = DDI_FAILURE;
2827c478bd9Sstevel@tonic-gate struct sbusmem_unit *un;
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate switch (infocmd) {
2857c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
2867c478bd9Sstevel@tonic-gate instance = getminor((dev_t)arg);
2877c478bd9Sstevel@tonic-gate if ((un = ddi_get_soft_state(sbusmem_state_head,
2887c478bd9Sstevel@tonic-gate instance)) != NULL) {
2897c478bd9Sstevel@tonic-gate *result = (void *)un->dip;
2907c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
2917c478bd9Sstevel@tonic-gate #ifdef SBUSMEM_DEBUG
2927c478bd9Sstevel@tonic-gate sbusmem_debug(
2937c478bd9Sstevel@tonic-gate "sbmem_info%d: returning dip 0x%p\n", instance, un->dip);
2947c478bd9Sstevel@tonic-gate #endif /* SBUSMEM_DEBUG */
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate break;
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
3007c478bd9Sstevel@tonic-gate instance = getminor((dev_t)arg);
3011a70ae91Smathue *result = (void *)(uintptr_t)instance;
3027c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
3037c478bd9Sstevel@tonic-gate break;
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate default:
3067c478bd9Sstevel@tonic-gate break;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate return (error);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate static int
sbmem_read(dev_t dev,struct uio * uio,cred_t * cred)3127c478bd9Sstevel@tonic-gate sbmem_read(dev_t dev, struct uio *uio, cred_t *cred)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate return (sbmem_rw(dev, uio, UIO_READ, cred));
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate static int
sbmem_write(dev_t dev,struct uio * uio,cred_t * cred)3187c478bd9Sstevel@tonic-gate sbmem_write(dev_t dev, struct uio *uio, cred_t *cred)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate return (sbmem_rw(dev, uio, UIO_WRITE, cred));
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate static int
sbmem_rw(dev_t dev,struct uio * uio,enum uio_rw rw,cred_t * cred __unused)324*78d71c09SToomas Soome sbmem_rw(dev_t dev, struct uio *uio, enum uio_rw rw, cred_t *cred __unused)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate uint_t c;
3277c478bd9Sstevel@tonic-gate struct iovec *iov;
3287c478bd9Sstevel@tonic-gate struct sbusmem_unit *un;
3297c478bd9Sstevel@tonic-gate uint_t pagesize, msize;
3307c478bd9Sstevel@tonic-gate int instance, error = 0;
3317c478bd9Sstevel@tonic-gate dev_info_t *dip;
3327c478bd9Sstevel@tonic-gate caddr_t reg;
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate instance = getminor(dev);
3357c478bd9Sstevel@tonic-gate if ((un = ddi_get_soft_state(sbusmem_state_head, instance)) == NULL) {
3367c478bd9Sstevel@tonic-gate return (ENXIO);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate dip = un->dip;
3397c478bd9Sstevel@tonic-gate pagesize = un->pagesize;
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate while (uio->uio_resid > 0 && error == 0) {
3427c478bd9Sstevel@tonic-gate iov = uio->uio_iov;
3437c478bd9Sstevel@tonic-gate if (iov->iov_len == 0) {
3447c478bd9Sstevel@tonic-gate uio->uio_iov++;
3457c478bd9Sstevel@tonic-gate uio->uio_iovcnt--;
3467c478bd9Sstevel@tonic-gate if (uio->uio_iovcnt < 0)
3477c478bd9Sstevel@tonic-gate cmn_err(CE_PANIC, "sbmem_rw");
3487c478bd9Sstevel@tonic-gate continue;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate if (uio->uio_offset > un->size) {
3527c478bd9Sstevel@tonic-gate return (EFAULT);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate if (uio->uio_offset == un->size) {
3567c478bd9Sstevel@tonic-gate return (0); /* EOF */
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate msize = pagesize - (uio->uio_offset & (pagesize - 1));
3597c478bd9Sstevel@tonic-gate if (ddi_map_regs(dip, 0, ®, uio->uio_offset,
3607c478bd9Sstevel@tonic-gate (off_t)msize) != DDI_SUCCESS) {
3617c478bd9Sstevel@tonic-gate return (EFAULT);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate c = min(msize, (uint_t)iov->iov_len);
3647c478bd9Sstevel@tonic-gate if (ddi_peekpokeio(dip, uio, rw, reg, (int)c,
3657c478bd9Sstevel@tonic-gate sizeof (int)) != DDI_SUCCESS)
3667c478bd9Sstevel@tonic-gate error = EFAULT;
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate ddi_unmap_regs(dip, 0, ®, uio->uio_offset, (off_t)msize);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate return (error);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate static int
sbmem_devmap(dev_t dev,devmap_cookie_t dhp,offset_t off,size_t len,size_t * maplen,uint_t model __unused)3747c478bd9Sstevel@tonic-gate sbmem_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
375*78d71c09SToomas Soome size_t *maplen, uint_t model __unused)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate struct sbusmem_unit *un;
3787c478bd9Sstevel@tonic-gate int instance, error;
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate instance = getminor(dev);
3817c478bd9Sstevel@tonic-gate if ((un = ddi_get_soft_state(sbusmem_state_head, instance)) == NULL) {
3827c478bd9Sstevel@tonic-gate return (ENXIO);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate if (off + len > un->size) {
3857c478bd9Sstevel@tonic-gate return (ENXIO);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate if ((error = devmap_devmem_setup(dhp, un->dip, NULL, 0,
38819397407SSherry Moore off, len, PROT_ALL, DEVMAP_DEFAULTS, NULL)) < 0) {
3897c478bd9Sstevel@tonic-gate return (error);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate *maplen = ptob(btopr(len));
3927c478bd9Sstevel@tonic-gate return (0);
3937c478bd9Sstevel@tonic-gate }
394