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