xref: /netbsd-src/sys/arch/pmax/stand/common/rz.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: rz.c,v 1.16 2000/03/30 14:45:11 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)rz.c	8.1 (Berkeley) 6/10/93
39  */
40 
41 #include <lib/libsa/stand.h>
42 #include <machine/dec_prom.h>
43 #include <machine/stdarg.h>
44 
45 #include <sys/param.h>
46 #include <sys/disklabel.h>
47 
48 #include "common.h"
49 #include "rz.h"
50 
51 struct	rz_softc {
52 	int	sc_fd;			/* PROM file id */
53 	int	sc_ctlr;		/* controller number */
54 	int	sc_unit;		/* disk unit number */
55 	int	sc_part;		/* disk partition number */
56 	struct	disklabel sc_label;	/* disk label for this disk */
57 };
58 
59 int
60 rzstrategy(devdata, rw, bn, reqcnt, addr, cnt)
61 	void *devdata;
62 	int rw;
63 	daddr_t bn;
64 	size_t reqcnt;
65 	void *addr;
66 	size_t *cnt;	/* out: number of bytes transfered */
67 {
68 	struct rz_softc *sc = (struct rz_softc *)devdata;
69 	int part = sc->sc_part;
70 	struct partition *pp = &sc->sc_label.d_partitions[part];
71 	int s;
72 	long offset;
73 
74 	offset = bn * DEV_BSIZE;
75 
76 	/*
77 	 * Partial-block transfers not handled.
78 	 */
79 	if (reqcnt & (DEV_BSIZE - 1)) {
80 		*cnt = 0;
81 		return (EINVAL);
82 	}
83 
84 	offset += pp->p_offset * DEV_BSIZE;
85 
86 	if (callv == &callvec) {
87 		/* No REX on this machine */
88 		if (prom_lseek(sc->sc_fd, offset, 0) < 0)
89 			return (EIO);
90 		s = prom_read(sc->sc_fd, addr, reqcnt);
91 	} else
92 		s = bootread (offset / 512, addr, reqcnt);
93 	if (s < 0)
94 		return (EIO);
95 
96 	*cnt = s;
97 	return (0);
98 }
99 
100 int
101 rzopen(struct open_file *f, ...)
102 {
103 	int ctlr, unit, part;
104 
105 	struct rz_softc *sc;
106 	struct disklabel *lp;
107 	int i;
108 	char *msg;
109 	char buf[DEV_BSIZE];
110 	int cnt;
111 	static char device[] = "rz(0,0,0)";
112 	va_list ap;
113 
114 	va_start(ap, f);
115 
116 	ctlr = va_arg(ap, int);
117 	unit = va_arg(ap, int);
118 	part = va_arg(ap, int);
119 	if (unit >= 8 || part >= 8)
120 		return (ENXIO);
121 	device[5] = '0' + unit;
122 	/* NOTE: only support reads for now */
123 	/* Another NOTE: bootinit on the TurboChannel doesn't look at
124 	   the device string - it's provided for compatibility with
125 	   the DS3100 PROMs.   As a consequence, it may be possible to
126 	   boot from some other drive with these bootblocks on the 3100,
127 	   but will not be possible on any TurboChannel machine. */
128 
129 	if (callv == &callvec)
130 		i = prom_open(device, 0);
131 	else
132 		i = bootinit (device);
133 	if (i < 0) {
134 		printf("open failed\n");
135 		return (ENXIO);
136 	}
137 
138 	sc = alloc(sizeof(struct rz_softc));
139 	memset(sc, 0, sizeof(struct rz_softc));
140 	f->f_devdata = (void *)sc;
141 
142 	sc->sc_fd = i;
143 	sc->sc_ctlr = ctlr;
144 	sc->sc_unit = unit;
145 	sc->sc_part = part;
146 
147 	/* try to read disk label and partition table information */
148 	lp = &sc->sc_label;
149 	lp->d_secsize = DEV_BSIZE;
150 	lp->d_secpercyl = 1;
151 	lp->d_npartitions = MAXPARTITIONS;
152 	lp->d_partitions[part].p_offset = 0;
153 	lp->d_partitions[part].p_size = 0x7fffffff;
154 
155 	i = rzstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
156 	if (i || cnt != DEV_BSIZE) {
157 		/* printf("rz%d: error reading disk label\n", unit); */
158 		goto bad;
159 	}
160 	msg = getdisklabel(buf, lp);
161 	if (msg) {
162 		/* If no label, just assume 0 and return */
163 		return (0);
164 	}
165 
166 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
167 	bad:
168 		free(sc, sizeof(struct rz_softc));
169 		return (ENXIO);
170 	}
171 	return (0);
172 }
173 
174 #ifndef LIBSA_NO_DEV_CLOSE
175 int
176 rzclose(f)
177 	struct open_file *f;
178 {
179 	if (callv == &callvec)
180 		prom_close(((struct rz_softc *)f->f_devdata)->sc_fd);
181 
182 	free(f->f_devdata, sizeof(struct rz_softc));
183 	f->f_devdata = (void *)0;
184 	return (0);
185 }
186 #endif
187