rz.c revision 1.17
1/*	$NetBSD: rz.c,v 1.17 2000/09/13 04:06:47 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#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	if (unit >= 8 || part >= 8)
131		return (ENXIO);
132	device[5] = '0' + unit;
133	/* NOTE: only support reads for now */
134	/* Another NOTE: bootinit on the TurboChannel doesn't look at
135	   the device string - it's provided for compatibility with
136	   the DS3100 PROMs.   As a consequence, it may be possible to
137	   boot from some other drive with these bootblocks on the 3100,
138	   but will not be possible on any TurboChannel machine. */
139
140	if (callv == &callvec)
141		i = prom_open(device, 0);
142	else
143		i = bootinit (device);
144	if (i < 0) {
145		printf("open failed\n");
146		return (ENXIO);
147	}
148
149	sc = alloc(sizeof(struct rz_softc));
150	memset(sc, 0, sizeof(struct rz_softc));
151	f->f_devdata = (void *)sc;
152
153	sc->sc_fd = i;
154	sc->sc_ctlr = ctlr;
155	sc->sc_unit = unit;
156	sc->sc_part = part;
157
158	/* try to read disk label and partition table information */
159	lp = &sc->sc_label;
160	lp->d_secsize = DEV_BSIZE;
161	lp->d_secpercyl = 1;
162	lp->d_npartitions = MAXPARTITIONS;
163	lp->d_partitions[part].p_offset = 0;
164	lp->d_partitions[part].p_size = 0x7fffffff;
165
166	i = rzstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
167	if (i || cnt != DEV_BSIZE) {
168		/* printf("rz%d: error reading disk label\n", unit); */
169		goto bad;
170	}
171	msg = getdisklabel(buf, lp);
172	if (msg) {
173		/* If no label, just assume 0 and return */
174		return (0);
175	}
176
177	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
178	bad:
179		free(sc, sizeof(struct rz_softc));
180		return (ENXIO);
181	}
182	return (0);
183}
184
185#ifndef LIBSA_NO_DEV_CLOSE
186int
187rzclose(f)
188	struct open_file *f;
189{
190	if (callv == &callvec)
191		prom_close(((struct rz_softc *)f->f_devdata)->sc_fd);
192
193	free(f->f_devdata, sizeof(struct rz_softc));
194	f->f_devdata = (void *)0;
195	return (0);
196}
197#endif
198