rz.c revision 1.25
11.25Sdsl/* $NetBSD: rz.c,v 1.25 2009/03/14 21:04:14 dsl 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.20Sagc * 3. Neither the name of the University nor the names of its contributors 191.1Sderaadt * may be used to endorse or promote products derived from this software 201.1Sderaadt * without specific prior written permission. 211.1Sderaadt * 221.1Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231.1Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241.1Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251.1Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261.1Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271.1Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281.1Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291.1Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301.1Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311.1Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321.1Sderaadt * SUCH DAMAGE. 331.1Sderaadt * 341.4Scgd * @(#)rz.c 8.1 (Berkeley) 6/10/93 351.1Sderaadt */ 361.1Sderaadt 371.15Ssimonb#include <lib/libsa/stand.h> 381.19Sjdolecek#include <lib/libkern/libkern.h> 391.15Ssimonb#include <machine/dec_prom.h> 401.15Ssimonb#include <machine/stdarg.h> 411.15Ssimonb 421.1Sderaadt#include <sys/param.h> 431.1Sderaadt#include <sys/disklabel.h> 441.15Ssimonb 451.15Ssimonb#include "common.h" 461.15Ssimonb#include "rz.h" 471.1Sderaadt 481.17Ssimonb#define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */ 491.17Ssimonb 501.17Ssimonb 511.1Sderaadtstruct rz_softc { 521.1Sderaadt int sc_fd; /* PROM file id */ 531.1Sderaadt int sc_ctlr; /* controller number */ 541.1Sderaadt int sc_unit; /* disk unit number */ 551.1Sderaadt int sc_part; /* disk partition number */ 561.1Sderaadt struct disklabel sc_label; /* disk label for this disk */ 571.1Sderaadt}; 581.1Sderaadt 591.1Sderaadtint 601.25Sdslrzstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addr, size_t *cnt) 611.25Sdsl /* cnt: out: number of bytes transfered */ 621.1Sderaadt{ 631.16Ssimonb struct rz_softc *sc = (struct rz_softc *)devdata; 641.16Ssimonb int part = sc->sc_part; 651.16Ssimonb struct partition *pp = &sc->sc_label.d_partitions[part]; 661.16Ssimonb int s; 671.1Sderaadt long offset; 681.1Sderaadt 691.17Ssimonb offset = bn; 701.5Smellon 711.1Sderaadt /* 721.1Sderaadt * Partial-block transfers not handled. 731.1Sderaadt */ 741.1Sderaadt if (reqcnt & (DEV_BSIZE - 1)) { 751.1Sderaadt *cnt = 0; 761.1Sderaadt return (EINVAL); 771.1Sderaadt } 781.1Sderaadt 791.17Ssimonb offset += pp->p_offset; 801.17Ssimonb 811.17Ssimonb if (pp->p_fstype == FS_RAID) 821.17Ssimonb offset += RF_PROTECTED_SECTORS; 831.17Ssimonb 841.17Ssimonb /* 851.17Ssimonb * Convert from blocks to bytes. 861.17Ssimonb */ 871.17Ssimonb offset *= DEV_BSIZE; 881.6Sjonathan 891.6Sjonathan if (callv == &callvec) { 901.6Sjonathan /* No REX on this machine */ 911.6Sjonathan if (prom_lseek(sc->sc_fd, offset, 0) < 0) 921.6Sjonathan return (EIO); 931.6Sjonathan s = prom_read(sc->sc_fd, addr, reqcnt); 941.6Sjonathan } else 951.6Sjonathan s = bootread (offset / 512, addr, reqcnt); 961.1Sderaadt if (s < 0) 971.1Sderaadt return (EIO); 981.1Sderaadt 991.1Sderaadt *cnt = s; 1001.1Sderaadt return (0); 1011.1Sderaadt} 1021.1Sderaadt 1031.1Sderaadtint 1041.6Sjonathanrzopen(struct open_file *f, ...) 1051.1Sderaadt{ 1061.16Ssimonb int ctlr, unit, part; 1071.6Sjonathan 1081.16Ssimonb struct rz_softc *sc; 1091.16Ssimonb struct disklabel *lp; 1101.16Ssimonb int i; 1111.1Sderaadt char *msg; 1121.1Sderaadt char buf[DEV_BSIZE]; 1131.23Stsutsui size_t cnt; 1141.1Sderaadt static char device[] = "rz(0,0,0)"; 1151.6Sjonathan va_list ap; 1161.6Sjonathan 1171.6Sjonathan va_start(ap, f); 1181.1Sderaadt 1191.6Sjonathan ctlr = va_arg(ap, int); 1201.6Sjonathan unit = va_arg(ap, int); 1211.6Sjonathan part = va_arg(ap, int); 1221.18Swiz va_end(ap); 1231.1Sderaadt if (unit >= 8 || part >= 8) 1241.1Sderaadt return (ENXIO); 1251.1Sderaadt device[5] = '0' + unit; 1261.1Sderaadt /* NOTE: only support reads for now */ 1271.5Smellon /* Another NOTE: bootinit on the TurboChannel doesn't look at 1281.5Smellon the device string - it's provided for compatibility with 1291.5Smellon the DS3100 PROMs. As a consequence, it may be possible to 1301.5Smellon boot from some other drive with these bootblocks on the 3100, 1311.5Smellon but will not be possible on any TurboChannel machine. */ 1321.6Sjonathan 1331.6Sjonathan if (callv == &callvec) 1341.6Sjonathan i = prom_open(device, 0); 1351.6Sjonathan else 1361.6Sjonathan i = bootinit (device); 1371.6Sjonathan if (i < 0) { 1381.12Ssimonb printf("open failed\n"); 1391.1Sderaadt return (ENXIO); 1401.5Smellon } 1411.1Sderaadt 1421.1Sderaadt sc = alloc(sizeof(struct rz_softc)); 1431.10Ssimonb memset(sc, 0, sizeof(struct rz_softc)); 1441.1Sderaadt f->f_devdata = (void *)sc; 1451.1Sderaadt 1461.1Sderaadt sc->sc_fd = i; 1471.1Sderaadt sc->sc_ctlr = ctlr; 1481.1Sderaadt sc->sc_unit = unit; 1491.1Sderaadt sc->sc_part = part; 1501.1Sderaadt 1511.1Sderaadt /* try to read disk label and partition table information */ 1521.1Sderaadt lp = &sc->sc_label; 1531.1Sderaadt lp->d_secsize = DEV_BSIZE; 1541.1Sderaadt lp->d_secpercyl = 1; 1551.1Sderaadt lp->d_npartitions = MAXPARTITIONS; 1561.1Sderaadt lp->d_partitions[part].p_offset = 0; 1571.1Sderaadt lp->d_partitions[part].p_size = 0x7fffffff; 1581.15Ssimonb 1591.1Sderaadt i = rzstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt); 1601.1Sderaadt if (i || cnt != DEV_BSIZE) { 1611.15Ssimonb /* printf("rz%d: error reading disk label\n", unit); */ 1621.1Sderaadt goto bad; 1631.15Ssimonb } 1641.15Ssimonb msg = getdisklabel(buf, lp); 1651.15Ssimonb if (msg) { 1661.15Ssimonb /* If no label, just assume 0 and return */ 1671.15Ssimonb return (0); 1681.1Sderaadt } 1691.1Sderaadt 1701.1Sderaadt if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) { 1711.1Sderaadt bad: 1721.22Schristos dealloc(sc, sizeof(struct rz_softc)); 1731.1Sderaadt return (ENXIO); 1741.1Sderaadt } 1751.1Sderaadt return (0); 1761.1Sderaadt} 1771.1Sderaadt 1781.13Ssimonb#ifndef LIBSA_NO_DEV_CLOSE 1791.13Ssimonbint 1801.24Sdslrzclose(struct open_file *f) 1811.1Sderaadt{ 1821.13Ssimonb if (callv == &callvec) 1831.13Ssimonb prom_close(((struct rz_softc *)f->f_devdata)->sc_fd); 1841.13Ssimonb 1851.22Schristos dealloc(f->f_devdata, sizeof(struct rz_softc)); 1861.1Sderaadt f->f_devdata = (void *)0; 1871.1Sderaadt return (0); 1881.1Sderaadt} 1891.5Smellon#endif 190