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