rz.c revision 1.19
11.19Sjdolecek/*	$NetBSD: rz.c,v 1.19 2002/12/09 12:14:10 jdolecek Exp $	*/
21.4Scgd
31.1Sderaadt/*
41.2Sglass * Copyright (c) 1992, 1993
51.2Sglass *	The Regents of the University of California.  All rights reserved.
61.1Sderaadt *
71.1Sderaadt * This code is derived from software contributed to Berkeley by
81.1Sderaadt * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
91.1Sderaadt *
101.1Sderaadt * Redistribution and use in source and binary forms, with or without
111.1Sderaadt * modification, are permitted provided that the following conditions
121.1Sderaadt * are met:
131.1Sderaadt * 1. Redistributions of source code must retain the above copyright
141.1Sderaadt *    notice, this list of conditions and the following disclaimer.
151.1Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
161.1Sderaadt *    notice, this list of conditions and the following disclaimer in the
171.1Sderaadt *    documentation and/or other materials provided with the distribution.
181.1Sderaadt * 3. All advertising materials mentioning features or use of this software
191.1Sderaadt *    must display the following acknowledgement:
201.1Sderaadt *	This product includes software developed by the University of
211.1Sderaadt *	California, Berkeley and its contributors.
221.1Sderaadt * 4. Neither the name of the University nor the names of its contributors
231.1Sderaadt *    may be used to endorse or promote products derived from this software
241.1Sderaadt *    without specific prior written permission.
251.1Sderaadt *
261.1Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271.1Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281.1Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291.1Sderaadt * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301.1Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311.1Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321.1Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331.1Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341.1Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351.1Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361.1Sderaadt * SUCH DAMAGE.
371.1Sderaadt *
381.4Scgd *	@(#)rz.c	8.1 (Berkeley) 6/10/93
391.1Sderaadt */
401.1Sderaadt
411.15Ssimonb#include <lib/libsa/stand.h>
421.19Sjdolecek#include <lib/libkern/libkern.h>
431.15Ssimonb#include <machine/dec_prom.h>
441.15Ssimonb#include <machine/stdarg.h>
451.15Ssimonb
461.1Sderaadt#include <sys/param.h>
471.1Sderaadt#include <sys/disklabel.h>
481.15Ssimonb
491.15Ssimonb#include "common.h"
501.15Ssimonb#include "rz.h"
511.1Sderaadt
521.17Ssimonb#define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
531.17Ssimonb
541.17Ssimonb
551.1Sderaadtstruct	rz_softc {
561.1Sderaadt	int	sc_fd;			/* PROM file id */
571.1Sderaadt	int	sc_ctlr;		/* controller number */
581.1Sderaadt	int	sc_unit;		/* disk unit number */
591.1Sderaadt	int	sc_part;		/* disk partition number */
601.1Sderaadt	struct	disklabel sc_label;	/* disk label for this disk */
611.1Sderaadt};
621.1Sderaadt
631.1Sderaadtint
641.1Sderaadtrzstrategy(devdata, rw, bn, reqcnt, addr, cnt)
651.1Sderaadt	void *devdata;
661.1Sderaadt	int rw;
671.1Sderaadt	daddr_t bn;
681.7Ssimonb	size_t reqcnt;
691.7Ssimonb	void *addr;
701.7Ssimonb	size_t *cnt;	/* out: number of bytes transfered */
711.1Sderaadt{
721.16Ssimonb	struct rz_softc *sc = (struct rz_softc *)devdata;
731.16Ssimonb	int part = sc->sc_part;
741.16Ssimonb	struct partition *pp = &sc->sc_label.d_partitions[part];
751.16Ssimonb	int s;
761.1Sderaadt	long offset;
771.1Sderaadt
781.17Ssimonb	offset = bn;
791.5Smellon
801.1Sderaadt	/*
811.1Sderaadt	 * Partial-block transfers not handled.
821.1Sderaadt	 */
831.1Sderaadt	if (reqcnt & (DEV_BSIZE - 1)) {
841.1Sderaadt		*cnt = 0;
851.1Sderaadt		return (EINVAL);
861.1Sderaadt	}
871.1Sderaadt
881.17Ssimonb	offset += pp->p_offset;
891.17Ssimonb
901.17Ssimonb	if (pp->p_fstype == FS_RAID)
911.17Ssimonb		offset += RF_PROTECTED_SECTORS;
921.17Ssimonb
931.17Ssimonb	/*
941.17Ssimonb	 * Convert from blocks to bytes.
951.17Ssimonb	 */
961.17Ssimonb	offset *= DEV_BSIZE;
971.6Sjonathan
981.6Sjonathan	if (callv == &callvec) {
991.6Sjonathan		/* No REX on this machine */
1001.6Sjonathan		if (prom_lseek(sc->sc_fd, offset, 0) < 0)
1011.6Sjonathan			return (EIO);
1021.6Sjonathan		s = prom_read(sc->sc_fd, addr, reqcnt);
1031.6Sjonathan	} else
1041.6Sjonathan		s = bootread (offset / 512, addr, reqcnt);
1051.1Sderaadt	if (s < 0)
1061.1Sderaadt		return (EIO);
1071.1Sderaadt
1081.1Sderaadt	*cnt = s;
1091.1Sderaadt	return (0);
1101.1Sderaadt}
1111.1Sderaadt
1121.1Sderaadtint
1131.6Sjonathanrzopen(struct open_file *f, ...)
1141.1Sderaadt{
1151.16Ssimonb	int ctlr, unit, part;
1161.6Sjonathan
1171.16Ssimonb	struct rz_softc *sc;
1181.16Ssimonb	struct disklabel *lp;
1191.16Ssimonb	int i;
1201.1Sderaadt	char *msg;
1211.1Sderaadt	char buf[DEV_BSIZE];
1221.1Sderaadt	int cnt;
1231.1Sderaadt	static char device[] = "rz(0,0,0)";
1241.6Sjonathan	va_list ap;
1251.6Sjonathan
1261.6Sjonathan	va_start(ap, f);
1271.1Sderaadt
1281.6Sjonathan	ctlr = va_arg(ap, int);
1291.6Sjonathan	unit = va_arg(ap, int);
1301.6Sjonathan	part = va_arg(ap, int);
1311.18Swiz	va_end(ap);
1321.1Sderaadt	if (unit >= 8 || part >= 8)
1331.1Sderaadt		return (ENXIO);
1341.1Sderaadt	device[5] = '0' + unit;
1351.1Sderaadt	/* NOTE: only support reads for now */
1361.5Smellon	/* Another NOTE: bootinit on the TurboChannel doesn't look at
1371.5Smellon	   the device string - it's provided for compatibility with
1381.5Smellon	   the DS3100 PROMs.   As a consequence, it may be possible to
1391.5Smellon	   boot from some other drive with these bootblocks on the 3100,
1401.5Smellon	   but will not be possible on any TurboChannel machine. */
1411.6Sjonathan
1421.6Sjonathan	if (callv == &callvec)
1431.6Sjonathan		i = prom_open(device, 0);
1441.6Sjonathan	else
1451.6Sjonathan		i = bootinit (device);
1461.6Sjonathan	if (i < 0) {
1471.12Ssimonb		printf("open failed\n");
1481.1Sderaadt		return (ENXIO);
1491.5Smellon	}
1501.1Sderaadt
1511.1Sderaadt	sc = alloc(sizeof(struct rz_softc));
1521.10Ssimonb	memset(sc, 0, sizeof(struct rz_softc));
1531.1Sderaadt	f->f_devdata = (void *)sc;
1541.1Sderaadt
1551.1Sderaadt	sc->sc_fd = i;
1561.1Sderaadt	sc->sc_ctlr = ctlr;
1571.1Sderaadt	sc->sc_unit = unit;
1581.1Sderaadt	sc->sc_part = part;
1591.1Sderaadt
1601.1Sderaadt	/* try to read disk label and partition table information */
1611.1Sderaadt	lp = &sc->sc_label;
1621.1Sderaadt	lp->d_secsize = DEV_BSIZE;
1631.1Sderaadt	lp->d_secpercyl = 1;
1641.1Sderaadt	lp->d_npartitions = MAXPARTITIONS;
1651.1Sderaadt	lp->d_partitions[part].p_offset = 0;
1661.1Sderaadt	lp->d_partitions[part].p_size = 0x7fffffff;
1671.15Ssimonb
1681.1Sderaadt	i = rzstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
1691.1Sderaadt	if (i || cnt != DEV_BSIZE) {
1701.15Ssimonb		/* printf("rz%d: error reading disk label\n", unit); */
1711.1Sderaadt		goto bad;
1721.15Ssimonb	}
1731.15Ssimonb	msg = getdisklabel(buf, lp);
1741.15Ssimonb	if (msg) {
1751.15Ssimonb		/* If no label, just assume 0 and return */
1761.15Ssimonb		return (0);
1771.1Sderaadt	}
1781.1Sderaadt
1791.1Sderaadt	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
1801.1Sderaadt	bad:
1811.1Sderaadt		free(sc, sizeof(struct rz_softc));
1821.1Sderaadt		return (ENXIO);
1831.1Sderaadt	}
1841.1Sderaadt	return (0);
1851.1Sderaadt}
1861.1Sderaadt
1871.13Ssimonb#ifndef LIBSA_NO_DEV_CLOSE
1881.13Ssimonbint
1891.1Sderaadtrzclose(f)
1901.1Sderaadt	struct open_file *f;
1911.1Sderaadt{
1921.13Ssimonb	if (callv == &callvec)
1931.13Ssimonb		prom_close(((struct rz_softc *)f->f_devdata)->sc_fd);
1941.13Ssimonb
1951.1Sderaadt	free(f->f_devdata, sizeof(struct rz_softc));
1961.1Sderaadt	f->f_devdata = (void *)0;
1971.1Sderaadt	return (0);
1981.1Sderaadt}
1991.5Smellon#endif
200