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