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