Home | History | Annotate | Line # | Download | only in common
promdev.c revision 1.28
      1 /*	$NetBSD: promdev.c,v 1.28 2017/03/25 09:21:21 martin Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1993 Paul Kranenburg
      5  * Copyright (c) 1995 Gordon W. Ross
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Paul Kranenburg.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Note: the `#ifndef BOOTXX' in here serve to queeze the code size
     36  * of the 1st-stage boot program.
     37  */
     38 #include <sys/param.h>
     39 #include <sys/reboot.h>
     40 #include <sys/systm.h>
     41 #include <machine/oldmon.h>
     42 #include <machine/promlib.h>
     43 #include <machine/ctlreg.h>
     44 #include <sparc/sparc/asm.h>
     45 #include <machine/pte.h>
     46 
     47 #include <lib/libsa/stand.h>
     48 #include <lib/libsa/net.h>
     49 #include <lib/libkern/libkern.h>
     50 #include <sparc/stand/common/promdev.h>
     51 #include <sparc/stand/common/isfloppy.h>
     52 
     53 #ifndef BOOTXX
     54 #include <sys/disklabel.h>
     55 #include <dev/sun/disklabel.h>
     56 #include <dev/raidframe/raidframevar.h>
     57 #endif
     58 
     59 /* OBP V0-3 PROM vector */
     60 #define obpvec	((struct promvec *)romp)
     61 
     62 int	obp_close(struct open_file *);
     63 int	obp_strategy(void *, int, daddr_t, size_t, void *, size_t *);
     64 int	obp_v0_strategy(void *, int, daddr_t, size_t, void *, size_t *);
     65 ssize_t	obp_v0_xmit(struct promdata *, void *, size_t);
     66 ssize_t	obp_v0_recv(struct promdata *, void *, size_t);
     67 int	obp_v2_strategy(void *, int, daddr_t, size_t, void *, size_t *);
     68 ssize_t	obp_v2_xmit(struct promdata *, void *, size_t);
     69 ssize_t	obp_v2_recv(struct promdata *, void *, size_t);
     70 int	oldmon_close(struct open_file *);
     71 int	oldmon_strategy(void *, int, daddr_t, size_t, void *, size_t *);
     72 void	oldmon_iclose(struct saioreq *);
     73 int	oldmon_iopen(struct promdata *);
     74 ssize_t	oldmon_xmit(struct promdata *, void *, size_t);
     75 ssize_t	oldmon_recv(struct promdata *, void *, size_t);
     76 
     77 static char	*oldmon_mapin(u_long, int, int);
     78 #ifndef BOOTXX
     79 static char	*mygetpropstring(int, char *);
     80 static int	getdevtype(int, char *);
     81 #endif
     82 
     83 extern struct fs_ops file_system_nfs[];
     84 extern struct fs_ops file_system_ufs[];
     85 
     86 #define null_devopen	(void *)sparc_noop
     87 #define null_devioctl	(void *)sparc_noop
     88 
     89 #if 0
     90 struct devsw devsw[];
     91 int	ndevs = (sizeof(devsw)/sizeof(devsw[0]));
     92 #endif
     93 
     94 struct devsw oldmon_devsw =
     95 	{ "oldmon", oldmon_strategy, null_devopen, oldmon_close, null_devioctl };
     96 struct devsw obp_v0_devsw =
     97 	{ "obp v0", obp_v0_strategy, null_devopen, obp_close, null_devioctl };
     98 struct devsw obp_v2_devsw =
     99 	{ "obp v2", obp_v2_strategy, null_devopen, obp_close, null_devioctl };
    100 
    101 
    102 char	prom_bootdevice[MAX_PROM_PATH];
    103 static int	saveecho;
    104 
    105 #ifndef BOOTXX
    106 static daddr_t doffset = 0;
    107 #endif
    108 
    109 void
    110 putchar(int c)
    111 {
    112 
    113 	if (c == '\n')
    114 		prom_putchar('\r');
    115 	prom_putchar(c);
    116 }
    117 
    118 void
    119 _rtt(void)
    120 {
    121 
    122 	prom_halt();
    123 }
    124 
    125 int
    126 devopen(struct open_file *f, const char *fname, char **file)
    127 {
    128 	int	error = 0, fd = 0;
    129 	struct	promdata *pd;
    130 #ifndef BOOTXX
    131 	char *partition;
    132 	int part = 0;
    133 	char rawpart[MAX_PROM_PATH];
    134 	struct promdata *disk_pd;
    135 	char buf[DEV_BSIZE];
    136 	struct disklabel *dlp;
    137 	size_t read;
    138 #endif
    139 
    140 	pd = (struct promdata *)alloc(sizeof *pd);
    141 	f->f_devdata = (void *)pd;
    142 
    143 	switch (prom_version()) {
    144 	case PROM_OLDMON:
    145 		error = oldmon_iopen(pd);
    146 #ifndef BOOTXX
    147 		pd->xmit = oldmon_xmit;
    148 		pd->recv = oldmon_recv;
    149 #endif
    150 		f->f_dev = &oldmon_devsw;
    151 		saveecho = *romVectorPtr->echo;
    152 		*romVectorPtr->echo = 0;
    153 		break;
    154 
    155 	case PROM_OBP_V0:
    156 	case PROM_OBP_V2:
    157 	case PROM_OBP_V3:
    158 	case PROM_OPENFIRM:
    159 		if (*prom_bootdevice == '\0') {
    160 			error = ENXIO;
    161 			break;
    162 		}
    163 		fd = prom_open(prom_bootdevice);
    164 		if (fd == 0) {
    165 			error = ENXIO;
    166 			break;
    167 		}
    168 		pd->fd = fd;
    169 		switch (prom_version()) {
    170 		case PROM_OBP_V0:
    171 #ifndef BOOTXX
    172 			pd->xmit = obp_v0_xmit;
    173 			pd->recv = obp_v0_recv;
    174 #endif
    175 			f->f_dev = &obp_v0_devsw;
    176 			break;
    177 		case PROM_OBP_V2:
    178 		case PROM_OBP_V3:
    179 		case PROM_OPENFIRM:
    180 #ifndef BOOTXX
    181 			pd->xmit = obp_v2_xmit;
    182 			pd->recv = obp_v2_recv;
    183 #endif
    184 			f->f_dev = &obp_v2_devsw;
    185 		}
    186 	}
    187 
    188 	if (error) {
    189 		printf("Can't open device `%s'\n", prom_bootdevice);
    190 		return (error);
    191 	}
    192 
    193 #ifdef BOOTXX
    194 	pd->devtype = DT_BLOCK;
    195 #else /* BOOTXX */
    196 	pd->devtype = getdevtype(fd, prom_bootdevice);
    197 	/* Assume type BYTE is a raw device */
    198 	if (pd->devtype != DT_BYTE)
    199 		*file = (char *)fname;
    200 
    201 	if (pd->devtype == DT_NET) {
    202 		nfsys = 1;
    203 		memcpy(file_system, file_system_nfs,
    204 		    sizeof(struct fs_ops) * nfsys);
    205 		if ((error = net_open(pd)) != 0) {
    206 			printf("Can't open NFS network connection on `%s'\n",
    207 				prom_bootdevice);
    208 			return (error);
    209 		}
    210 	} else {
    211 		memcpy(file_system, file_system_ufs,
    212 		    sizeof(struct fs_ops) * nfsys);
    213 
    214 #ifdef NOTDEF_DEBUG
    215 	printf("devopen: Checking disklabel for RAID partition\n");
    216 #endif
    217 
    218 		/*
    219 		 * Don't check disklabel on floppy boot since
    220 		 * reopening it could cause Data Access Exception later.
    221 		 */
    222 		if (bootdev_isfloppy(prom_bootdevice))
    223 			return 0;
    224 
    225 		/*
    226 		 * We need to read from the raw partition (i.e. the
    227 		 * beginning of the disk in order to check the NetBSD
    228 		 * disklabel to see if the boot partition is type RAID.
    229 		 *
    230 		 * For machines with prom_version() == PROM_OLDMON, we
    231 		 * only handle boot from RAID for the first disk partition.
    232 		 */
    233 		disk_pd = (struct promdata *)alloc(sizeof *disk_pd);
    234 		memcpy(disk_pd, pd, sizeof(struct promdata));
    235 		if (prom_version() != PROM_OLDMON) {
    236 			strcpy(rawpart, prom_bootdevice);
    237 			if ((partition = strchr(rawpart, ':')) != '\0' &&
    238 		    	    *++partition >= 'a' &&
    239 			    *partition <= 'a' +  MAXPARTITIONS) {
    240 				part = *partition - 'a';
    241 				*partition = RAW_PART + 'a';
    242 			} else
    243 				strcat(rawpart, ":c");
    244 			if ((disk_pd->fd = prom_open(rawpart)) == 0)
    245 				return 0;
    246 		}
    247 		error = f->f_dev->dv_strategy(disk_pd, F_READ, LABELSECTOR,
    248 		    DEV_BSIZE, &buf, &read);
    249 		if (prom_version() != PROM_OLDMON)
    250 			prom_close(disk_pd->fd);
    251 		if (error || (read != DEV_BSIZE))
    252 			return 0;
    253 #ifdef NOTDEF_DEBUG
    254 		{
    255 			int x = 0;
    256 			char *p = (char *) buf;
    257 
    258 			printf("  Sector %d:\n", LABELSECTOR);
    259 			printf("00000000  ");
    260 			while (x < DEV_BSIZE) {
    261 				if (*p >= 0x00 && *p < 0x10)
    262 					printf("0%x ", *p & 0xff);
    263 				else
    264 					printf("%x ", *p & 0xff);
    265 				x++;
    266 				if (x && !(x % 8))
    267 					printf(" ");
    268 				if (x && !(x % 16)) {
    269 					if(x < 0x100)
    270 						printf("\n000000%x  ", x);
    271 					else
    272 						printf("\n00000%x  ", x);
    273 				}
    274 				p++;
    275 			}
    276 			printf("\n");
    277 		}
    278 #endif
    279 		/* Check for NetBSD disk label. */
    280 		dlp = (struct disklabel *) (buf + LABELOFFSET);
    281 		if (dlp->d_magic == DISKMAGIC && !dkcksum(dlp) &&
    282 		    dlp->d_partitions[part].p_fstype == FS_RAID) {
    283 #ifdef NOTDEF_DEBUG
    284 			printf("devopen: found RAID partition, "
    285 			    "adjusting offset to %d\n", RF_PROTECTED_SECTORS);
    286 #endif
    287 			doffset = RF_PROTECTED_SECTORS;
    288 		}
    289 	}
    290 #endif /* BOOTXX */
    291 	return (0);
    292 }
    293 
    294 
    295 int
    296 obp_v0_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
    297 		void *buf, size_t *rsize)
    298 {
    299 	int	n, error = 0;
    300 	struct	promdata *pd = (struct promdata *)devdata;
    301 	int	fd = pd->fd;
    302 
    303 #ifndef BOOTXX
    304 	dblk += doffset;
    305 #endif
    306 #ifdef DEBUG_PROM
    307 	printf("promstrategy: size=%zd dblk=%d\n", size, (int)dblk);
    308 #endif
    309 
    310 #define prom_bread(fd, nblk, dblk, buf) \
    311 		(*obpvec->pv_v0devops.v0_rbdev)(fd, nblk, dblk, buf)
    312 #define prom_bwrite(fd, nblk, dblk, buf) \
    313 		(*obpvec->pv_v0devops.v0_wbdev)(fd, nblk, dblk, buf)
    314 
    315 #ifndef BOOTXX	/* We know it's a block device, so save some space */
    316 	if (pd->devtype != DT_BLOCK) {
    317 		printf("promstrategy: non-block device not supported\n");
    318 		error = EINVAL;
    319 	}
    320 #endif
    321 
    322 	n = (flag == F_READ)
    323 		? prom_bread(fd, btodb(size), dblk, buf)
    324 		: prom_bwrite(fd, btodb(size), dblk, buf);
    325 
    326 	*rsize = dbtob(n);
    327 
    328 #ifdef DEBUG_PROM
    329 	printf("rsize = %zx\n", *rsize);
    330 #endif
    331 	return (error);
    332 }
    333 
    334 int
    335 obp_v2_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
    336 		void *buf, size_t *rsize)
    337 {
    338 	int	error = 0;
    339 	struct	promdata *pd = (struct promdata *)devdata;
    340 	int	fd = pd->fd;
    341 
    342 #ifndef BOOTXX
    343 	dblk += doffset;
    344 #endif
    345 #ifdef DEBUG_PROM
    346 	printf("promstrategy: size=%zd dblk=%d\n", size, (int)dblk);
    347 #endif
    348 
    349 #ifndef BOOTXX	/* We know it's a block device, so save some space */
    350 	if (pd->devtype == DT_BLOCK)
    351 #endif
    352 		prom_seek(fd, dbtob(dblk));
    353 
    354 	*rsize = (flag == F_READ)
    355 		? prom_read(fd, buf, size)
    356 		: prom_write(fd, buf, size);
    357 
    358 #ifdef DEBUG_PROM
    359 	printf("rsize = %zx\n", *rsize);
    360 #endif
    361 	return (error);
    362 }
    363 
    364 /*
    365  * On old-monitor machines, things work differently.
    366  */
    367 int
    368 oldmon_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
    369 		void *buf, size_t *rsize)
    370 {
    371 	struct promdata	*pd = devdata;
    372 	struct saioreq	*si;
    373 	struct om_boottable *ops;
    374 	char	*dmabuf;
    375 	int	si_flag;
    376 	size_t	xcnt;
    377 
    378 	si = pd->si;
    379 	ops = si->si_boottab;
    380 
    381 #ifndef BOOTXX
    382 	dblk += doffset;
    383 #endif
    384 #ifdef DEBUG_PROM
    385 	printf("prom_strategy: size=%zd dblk=%d\n", size, (int)dblk);
    386 #endif
    387 
    388 	dmabuf = dvma_mapin(buf, size);
    389 
    390 	si->si_bn = dblk;
    391 	si->si_ma = dmabuf;
    392 	si->si_cc = size;
    393 
    394 	si_flag = (flag == F_READ) ? SAIO_F_READ : SAIO_F_WRITE;
    395 	xcnt = (*ops->b_strategy)(si, si_flag);
    396 	dvma_mapout(dmabuf, size);
    397 
    398 #ifdef DEBUG_PROM
    399 	printf("disk_strategy: xcnt = %zx\n", xcnt);
    400 #endif
    401 
    402 	if (xcnt <= 0)
    403 		return (EIO);
    404 
    405 	*rsize = xcnt;
    406 	return (0);
    407 }
    408 
    409 int
    410 obp_close(struct open_file *f)
    411 {
    412 	struct promdata *pd = f->f_devdata;
    413 	register int fd = pd->fd;
    414 
    415 #ifndef BOOTXX
    416 	if (pd->devtype == DT_NET)
    417 		net_close(pd);
    418 #endif
    419 	prom_close(fd);
    420 	return 0;
    421 }
    422 
    423 int
    424 oldmon_close(struct open_file *f)
    425 {
    426 	struct promdata *pd = f->f_devdata;
    427 
    428 #ifndef BOOTXX
    429 	if (pd->devtype == DT_NET)
    430 		net_close(pd);
    431 #endif
    432 	oldmon_iclose(pd->si);
    433 	pd->si = NULL;
    434 	*romVectorPtr->echo = saveecho; /* Hmm, probably must go somewhere else */
    435 	return 0;
    436 }
    437 
    438 #ifndef BOOTXX
    439 ssize_t
    440 obp_v0_xmit(struct promdata *pd, void *buf, size_t len)
    441 {
    442 
    443 	return ((*obpvec->pv_v0devops.v0_wnet)(pd->fd, len, buf));
    444 }
    445 
    446 ssize_t
    447 obp_v2_xmit(struct promdata *pd, void *buf, size_t len)
    448 {
    449 
    450 	return (prom_write(pd->fd, buf, len));
    451 }
    452 
    453 ssize_t
    454 obp_v0_recv(struct promdata *pd, void *buf, size_t len)
    455 {
    456 
    457 	return ((*obpvec->pv_v0devops.v0_rnet)(pd->fd, len, buf));
    458 }
    459 
    460 ssize_t
    461 obp_v2_recv(struct promdata *pd, void *buf, size_t len)
    462 {
    463 	int	n;
    464 
    465 	n = prom_read(pd->fd, buf, len);
    466 
    467 	/* OBP V2 & V3 may return -2 */
    468 	return (n == -2 ? 0 : n);
    469 }
    470 
    471 ssize_t
    472 oldmon_xmit(struct promdata *pd, void *buf, size_t len)
    473 {
    474 	struct saioreq	*si;
    475 	struct saif	*sif;
    476 	char		*dmabuf;
    477 	int		rv;
    478 
    479 	si = pd->si;
    480 	sif = si->si_sif;
    481 	if (sif == NULL) {
    482 		printf("xmit: not a network device\n");
    483 		return (-1);
    484 	}
    485 	dmabuf = dvma_mapin(buf, len);
    486 	rv = sif->sif_xmit(si->si_devdata, dmabuf, len);
    487 	dvma_mapout(dmabuf, len);
    488 
    489 	return (ssize_t)(rv ? -1 : len);
    490 }
    491 
    492 ssize_t
    493 oldmon_recv(struct promdata *pd, void *buf, size_t len)
    494 {
    495 	struct saioreq	*si;
    496 	struct saif	*sif;
    497 	char		*dmabuf;
    498 	int		rv;
    499 
    500 	si = pd->si;
    501 	sif = si->si_sif;
    502 	dmabuf = dvma_mapin(buf, len);
    503 	rv = sif->sif_poll(si->si_devdata, dmabuf);
    504 	dvma_mapout(dmabuf, len);
    505 
    506 	return (ssize_t)rv;
    507 }
    508 
    509 int
    510 getchar(void)
    511 {
    512 
    513 	return (prom_getchar());
    514 }
    515 
    516 satime_t
    517 getsecs(void)
    518 {
    519 
    520 	(void)prom_peekchar();
    521 	return (prom_ticks() / 1000);
    522 }
    523 
    524 /*
    525  * A number of well-known devices on sun4s.
    526  */
    527 static struct dtab {
    528 	char	*name;
    529 	int	type;
    530 } dtab[] = {
    531 	{ "sd",	DT_BLOCK },
    532 	{ "st",	DT_BLOCK },
    533 	{ "xd",	DT_BLOCK },
    534 	{ "xy",	DT_BLOCK },
    535 	{ "fd",	DT_BLOCK },
    536 	{ "le",	DT_NET },
    537 	{ "ie",	DT_NET },
    538 	{ NULL, 0 }
    539 };
    540 
    541 int
    542 getdevtype(int fd, char *name)
    543 {
    544 	struct dtab *dp;
    545 	int node;
    546 	char *cp;
    547 
    548 	switch (prom_version()) {
    549 	case PROM_OLDMON:
    550 	case PROM_OBP_V0:
    551 		for (dp = dtab; dp->name; dp++) {
    552 			if (name[0] == dp->name[0] &&
    553 			    name[1] == dp->name[1])
    554 				return (dp->type);
    555 		}
    556 		break;
    557 
    558 	case PROM_OBP_V2:
    559 	case PROM_OBP_V3:
    560 	case PROM_OPENFIRM:
    561 		node = prom_instance_to_package(fd);
    562 		cp = mygetpropstring(node, "device_type");
    563 		if (strcmp(cp, "block") == 0)
    564 			return (DT_BLOCK);
    565 		if (strcmp(cp, "scsi") == 0)
    566 			return (DT_BLOCK);
    567 		else if (strcmp(cp, "network") == 0)
    568 			return (DT_NET);
    569 		else if (strcmp(cp, "byte") == 0)
    570 			return (DT_BYTE);
    571 		break;
    572 	}
    573 	return (0);
    574 }
    575 
    576 /*
    577  * Return a string property.  There is a (small) limit on the length;
    578  * the string is fetched into a static buffer which is overwritten on
    579  * subsequent calls.
    580  */
    581 char *
    582 mygetpropstring(int node, char *name)
    583 {
    584 	int len;
    585 static	char buf[64];
    586 
    587 	len = prom_proplen(node, name);
    588 	if (len > sizeof(buf))
    589 		len = sizeof(buf)-1;
    590 	if (len > 0)
    591 		_prom_getprop(node, name, buf, len);
    592 	else
    593 		len = 0;
    594 
    595 	buf[len] = '\0';	/* usually unnecessary */
    596 	return (buf);
    597 }
    598 #endif /* BOOTXX */
    599 
    600 /*
    601  * Old monitor routines
    602  */
    603 
    604 struct saioreq prom_si;
    605 static int promdev_inuse;
    606 
    607 int
    608 oldmon_iopen(struct promdata *pd)
    609 {
    610 	struct om_bootparam *bp;
    611 	struct om_boottable *ops;
    612 	struct devinfo *dip;
    613 	struct saioreq *si;
    614 	int	error;
    615 
    616 	if (promdev_inuse)
    617 		return (EMFILE);
    618 
    619 	bp = *romVectorPtr->bootParam;
    620 	ops = bp->bootTable;
    621 	dip = ops->b_devinfo;
    622 
    623 #ifdef DEBUG_PROM
    624 	printf("Boot device type: %s\n", ops->b_desc);
    625 	printf("d_devbytes=%d\n", dip->d_devbytes);
    626 	printf("d_dmabytes=%d\n", dip->d_dmabytes);
    627 	printf("d_localbytes=%d\n", dip->d_localbytes);
    628 	printf("d_stdcount=%d\n", dip->d_stdcount);
    629 	printf("d_stdaddrs[%d]=%lx\n", bp->ctlrNum, dip->d_stdaddrs[bp->ctlrNum]);
    630 	printf("d_devtype=%d\n", dip->d_devtype);
    631 	printf("d_maxiobytes=%d\n", dip->d_maxiobytes);
    632 #endif
    633 
    634 	dvma_init();
    635 
    636 	si = &prom_si;
    637 	memset(si, 0, sizeof(*si));
    638 	si->si_boottab = ops;
    639 	si->si_ctlr = bp->ctlrNum;
    640 	si->si_unit = bp->unitNum;
    641 	si->si_boff = bp->partNum;
    642 
    643 	if (si->si_ctlr > dip->d_stdcount)
    644 		return (ECTLR);
    645 
    646 	if (dip->d_devbytes) {
    647 		si->si_devaddr = oldmon_mapin(dip->d_stdaddrs[si->si_ctlr],
    648 			dip->d_devbytes, dip->d_devtype);
    649 #ifdef	DEBUG_PROM
    650 		printf("prom_iopen: devaddr=%p pte=0x%x\n",
    651 			si->si_devaddr,
    652 			getpte4((u_long)si->si_devaddr & ~PGOFSET));
    653 #endif
    654 	}
    655 
    656 	if (dip->d_dmabytes) {
    657 		si->si_dmaaddr = dvma_alloc(dip->d_dmabytes);
    658 #ifdef	DEBUG_PROM
    659 		printf("prom_iopen: dmaaddr=%p\n", si->si_dmaaddr);
    660 #endif
    661 	}
    662 
    663 	if (dip->d_localbytes) {
    664 		si->si_devdata = alloc(dip->d_localbytes);
    665 #ifdef	DEBUG_PROM
    666 		printf("prom_iopen: devdata=%p\n", si->si_devdata);
    667 #endif
    668 	}
    669 
    670 	/* OK, call the PROM device open routine. */
    671 	error = (*ops->b_open)(si);
    672 	if (error != 0) {
    673 		printf("prom_iopen: \"%s\" error=%d\n", ops->b_desc, error);
    674 		return (ENXIO);
    675 	}
    676 #ifdef	DEBUG_PROM
    677 	printf("prom_iopen: succeeded, error=%d\n", error);
    678 #endif
    679 
    680 	pd->si = si;
    681 	promdev_inuse++;
    682 	return (0);
    683 }
    684 
    685 void
    686 oldmon_iclose(struct saioreq *si)
    687 {
    688 	struct om_boottable *ops;
    689 	struct devinfo *dip;
    690 
    691 	if (promdev_inuse == 0)
    692 		return;
    693 
    694 	ops = si->si_boottab;
    695 	dip = ops->b_devinfo;
    696 
    697 	(*ops->b_close)(si);
    698 
    699 	if (si->si_dmaaddr) {
    700 		dvma_free(si->si_dmaaddr, dip->d_dmabytes);
    701 		si->si_dmaaddr = NULL;
    702 	}
    703 
    704 	promdev_inuse = 0;
    705 }
    706 
    707 static struct mapinfo {
    708 	int maptype;
    709 	int pgtype;
    710 	int base;
    711 } oldmon_mapinfo[] = {
    712 #define PG_COMMON	(PG_V|PG_W|PG_S|PG_NC)
    713 	{ MAP_MAINMEM,   PG_OBMEM | PG_COMMON, 0 },
    714 	{ MAP_OBIO,      PG_OBIO  | PG_COMMON, 0 },
    715 	{ MAP_MBMEM,     PG_VME16 | PG_COMMON, 0xFF000000 },
    716 	{ MAP_MBIO,      PG_VME16 | PG_COMMON, 0xFFFF0000 },
    717 	{ MAP_VME16A16D, PG_VME16 | PG_COMMON, 0xFFFF0000 },
    718 	{ MAP_VME16A32D, PG_VME32 | PG_COMMON, 0xFFFF0000 },
    719 	{ MAP_VME24A16D, PG_VME16 | PG_COMMON, 0xFF000000 },
    720 	{ MAP_VME24A32D, PG_VME32 | PG_COMMON, 0xFF000000 },
    721 	{ MAP_VME32A16D, PG_VME16 | PG_COMMON, 0 },
    722 	{ MAP_VME32A32D, PG_VME32 | PG_COMMON, 0 },
    723 };
    724 static int oldmon_mapinfo_cnt =
    725 	sizeof(oldmon_mapinfo) / sizeof(oldmon_mapinfo[0]);
    726 
    727 /* The virtual address we will use for PROM device mappings. */
    728 static u_long prom_devmap = MONSHORTSEG;
    729 
    730 static char *
    731 oldmon_mapin(u_long physaddr, int length, int maptype)
    732 {
    733 	int i, pa, pte, va;
    734 
    735 	if (length > (4*NBPG))
    736 		panic("oldmon_mapin: length=%d", length);
    737 
    738 	for (i = 0; i < oldmon_mapinfo_cnt; i++)
    739 		if (oldmon_mapinfo[i].maptype == maptype)
    740 			goto found;
    741 	panic("oldmon_mapin: invalid maptype %d", maptype);
    742 
    743 found:
    744 	pte = oldmon_mapinfo[i].pgtype;
    745 	pa = oldmon_mapinfo[i].base;
    746 	pa += physaddr;
    747 	pte |= ((pa >> SUN4_PGSHIFT) & PG_PFNUM);
    748 
    749 	va = prom_devmap;
    750 	do {
    751 		setpte4(va, pte);
    752 		va += NBPG;
    753 		pte += 1;
    754 		length -= NBPG;
    755 	} while (length > 0);
    756 	return ((char*)(prom_devmap | (pa & PGOFSET)));
    757 }
    758