xref: /netbsd-src/sys/arch/pmax/stand/common/rz.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: rz.c,v 1.20 2003/08/07 16:29:15 agc 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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)rz.c	8.1 (Berkeley) 6/10/93
35  */
36 
37 #include <lib/libsa/stand.h>
38 #include <lib/libkern/libkern.h>
39 #include <machine/dec_prom.h>
40 #include <machine/stdarg.h>
41 
42 #include <sys/param.h>
43 #include <sys/disklabel.h>
44 
45 #include "common.h"
46 #include "rz.h"
47 
48 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
49 
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;
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;
85 
86 	if (pp->p_fstype == FS_RAID)
87 		offset += RF_PROTECTED_SECTORS;
88 
89 	/*
90 	 * Convert from blocks to bytes.
91 	 */
92 	offset *= DEV_BSIZE;
93 
94 	if (callv == &callvec) {
95 		/* No REX on this machine */
96 		if (prom_lseek(sc->sc_fd, offset, 0) < 0)
97 			return (EIO);
98 		s = prom_read(sc->sc_fd, addr, reqcnt);
99 	} else
100 		s = bootread (offset / 512, addr, reqcnt);
101 	if (s < 0)
102 		return (EIO);
103 
104 	*cnt = s;
105 	return (0);
106 }
107 
108 int
109 rzopen(struct open_file *f, ...)
110 {
111 	int ctlr, unit, part;
112 
113 	struct rz_softc *sc;
114 	struct disklabel *lp;
115 	int i;
116 	char *msg;
117 	char buf[DEV_BSIZE];
118 	int cnt;
119 	static char device[] = "rz(0,0,0)";
120 	va_list ap;
121 
122 	va_start(ap, f);
123 
124 	ctlr = va_arg(ap, int);
125 	unit = va_arg(ap, int);
126 	part = va_arg(ap, int);
127 	va_end(ap);
128 	if (unit >= 8 || part >= 8)
129 		return (ENXIO);
130 	device[5] = '0' + unit;
131 	/* NOTE: only support reads for now */
132 	/* Another NOTE: bootinit on the TurboChannel doesn't look at
133 	   the device string - it's provided for compatibility with
134 	   the DS3100 PROMs.   As a consequence, it may be possible to
135 	   boot from some other drive with these bootblocks on the 3100,
136 	   but will not be possible on any TurboChannel machine. */
137 
138 	if (callv == &callvec)
139 		i = prom_open(device, 0);
140 	else
141 		i = bootinit (device);
142 	if (i < 0) {
143 		printf("open failed\n");
144 		return (ENXIO);
145 	}
146 
147 	sc = alloc(sizeof(struct rz_softc));
148 	memset(sc, 0, sizeof(struct rz_softc));
149 	f->f_devdata = (void *)sc;
150 
151 	sc->sc_fd = i;
152 	sc->sc_ctlr = ctlr;
153 	sc->sc_unit = unit;
154 	sc->sc_part = part;
155 
156 	/* try to read disk label and partition table information */
157 	lp = &sc->sc_label;
158 	lp->d_secsize = DEV_BSIZE;
159 	lp->d_secpercyl = 1;
160 	lp->d_npartitions = MAXPARTITIONS;
161 	lp->d_partitions[part].p_offset = 0;
162 	lp->d_partitions[part].p_size = 0x7fffffff;
163 
164 	i = rzstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
165 	if (i || cnt != DEV_BSIZE) {
166 		/* printf("rz%d: error reading disk label\n", unit); */
167 		goto bad;
168 	}
169 	msg = getdisklabel(buf, lp);
170 	if (msg) {
171 		/* If no label, just assume 0 and return */
172 		return (0);
173 	}
174 
175 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
176 	bad:
177 		free(sc, sizeof(struct rz_softc));
178 		return (ENXIO);
179 	}
180 	return (0);
181 }
182 
183 #ifndef LIBSA_NO_DEV_CLOSE
184 int
185 rzclose(f)
186 	struct open_file *f;
187 {
188 	if (callv == &callvec)
189 		prom_close(((struct rz_softc *)f->f_devdata)->sc_fd);
190 
191 	free(f->f_devdata, sizeof(struct rz_softc));
192 	f->f_devdata = (void *)0;
193 	return (0);
194 }
195 #endif
196