xref: /netbsd-src/sys/arch/pmax/stand/common/rz.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: rz.c,v 1.25 2009/03/14 21:04:14 dsl 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(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addr, size_t *cnt)
61 	/* cnt:	 out: number of bytes transfered */
62 {
63 	struct rz_softc *sc = (struct rz_softc *)devdata;
64 	int part = sc->sc_part;
65 	struct partition *pp = &sc->sc_label.d_partitions[part];
66 	int s;
67 	long offset;
68 
69 	offset = bn;
70 
71 	/*
72 	 * Partial-block transfers not handled.
73 	 */
74 	if (reqcnt & (DEV_BSIZE - 1)) {
75 		*cnt = 0;
76 		return (EINVAL);
77 	}
78 
79 	offset += pp->p_offset;
80 
81 	if (pp->p_fstype == FS_RAID)
82 		offset += RF_PROTECTED_SECTORS;
83 
84 	/*
85 	 * Convert from blocks to bytes.
86 	 */
87 	offset *= DEV_BSIZE;
88 
89 	if (callv == &callvec) {
90 		/* No REX on this machine */
91 		if (prom_lseek(sc->sc_fd, offset, 0) < 0)
92 			return (EIO);
93 		s = prom_read(sc->sc_fd, addr, reqcnt);
94 	} else
95 		s = bootread (offset / 512, addr, reqcnt);
96 	if (s < 0)
97 		return (EIO);
98 
99 	*cnt = s;
100 	return (0);
101 }
102 
103 int
104 rzopen(struct open_file *f, ...)
105 {
106 	int ctlr, unit, part;
107 
108 	struct rz_softc *sc;
109 	struct disklabel *lp;
110 	int i;
111 	char *msg;
112 	char buf[DEV_BSIZE];
113 	size_t cnt;
114 	static char device[] = "rz(0,0,0)";
115 	va_list ap;
116 
117 	va_start(ap, f);
118 
119 	ctlr = va_arg(ap, int);
120 	unit = va_arg(ap, int);
121 	part = va_arg(ap, int);
122 	va_end(ap);
123 	if (unit >= 8 || part >= 8)
124 		return (ENXIO);
125 	device[5] = '0' + unit;
126 	/* NOTE: only support reads for now */
127 	/* Another NOTE: bootinit on the TurboChannel doesn't look at
128 	   the device string - it's provided for compatibility with
129 	   the DS3100 PROMs.   As a consequence, it may be possible to
130 	   boot from some other drive with these bootblocks on the 3100,
131 	   but will not be possible on any TurboChannel machine. */
132 
133 	if (callv == &callvec)
134 		i = prom_open(device, 0);
135 	else
136 		i = bootinit (device);
137 	if (i < 0) {
138 		printf("open failed\n");
139 		return (ENXIO);
140 	}
141 
142 	sc = alloc(sizeof(struct rz_softc));
143 	memset(sc, 0, sizeof(struct rz_softc));
144 	f->f_devdata = (void *)sc;
145 
146 	sc->sc_fd = i;
147 	sc->sc_ctlr = ctlr;
148 	sc->sc_unit = unit;
149 	sc->sc_part = part;
150 
151 	/* try to read disk label and partition table information */
152 	lp = &sc->sc_label;
153 	lp->d_secsize = DEV_BSIZE;
154 	lp->d_secpercyl = 1;
155 	lp->d_npartitions = MAXPARTITIONS;
156 	lp->d_partitions[part].p_offset = 0;
157 	lp->d_partitions[part].p_size = 0x7fffffff;
158 
159 	i = rzstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
160 	if (i || cnt != DEV_BSIZE) {
161 		/* printf("rz%d: error reading disk label\n", unit); */
162 		goto bad;
163 	}
164 	msg = getdisklabel(buf, lp);
165 	if (msg) {
166 		/* If no label, just assume 0 and return */
167 		return (0);
168 	}
169 
170 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
171 	bad:
172 		dealloc(sc, sizeof(struct rz_softc));
173 		return (ENXIO);
174 	}
175 	return (0);
176 }
177 
178 #ifndef LIBSA_NO_DEV_CLOSE
179 int
180 rzclose(struct open_file *f)
181 {
182 	if (callv == &callvec)
183 		prom_close(((struct rz_softc *)f->f_devdata)->sc_fd);
184 
185 	dealloc(f->f_devdata, sizeof(struct rz_softc));
186 	f->f_devdata = (void *)0;
187 	return (0);
188 }
189 #endif
190