Home | History | Annotate | Line # | Download | only in common
disk.c revision 1.1
      1 /*	$NetBSD: disk.c,v 1.1 2001/11/21 19:09:10 thorpej 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  *	@(#)disk.c	8.1 (Berkeley) 6/10/93
     39  */
     40 
     41 #include <lib/libsa/stand.h>
     42 #include <machine/stdarg.h>
     43 
     44 #include <sys/param.h>
     45 #include <sys/disklabel.h>
     46 
     47 #include <dev/arcbios/arcbios.h>
     48 
     49 #include "common.h"
     50 #include "disk.h"
     51 
     52 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
     53 
     54 extern const struct arcbios_fv *ARCBIOS;
     55 
     56 struct	disk_softc {
     57 	int	sc_fd;			/* PROM file id */
     58 	int	sc_ctlr;		/* controller number */
     59 	int	sc_unit;		/* disk unit number */
     60 	int	sc_part;		/* disk partition number */
     61 	struct	disklabel sc_label;	/* disk label for this disk */
     62 };
     63 
     64 int
     65 diskstrategy(devdata, rw, bn, reqcnt, addr, cnt)
     66 	void *devdata;
     67 	int rw;
     68 	daddr_t bn;
     69 	size_t reqcnt;
     70 	void *addr;
     71 	size_t *cnt;	/* out: number of bytes transfered */
     72 {
     73 	struct disk_softc *sc = (struct disk_softc *)devdata;
     74 	int part = sc->sc_part;
     75 	struct partition *pp = &sc->sc_label.d_partitions[part];
     76 	int s;
     77 	int64_t offset;
     78 	int count;
     79 
     80 	offset = bn;
     81 
     82 	/*
     83 	 * Partial-block transfers not handled.
     84 	 */
     85 	if (reqcnt & (DEV_BSIZE - 1)) {
     86 		*cnt = 0;
     87 		return (EINVAL);
     88 	}
     89 
     90 	offset += pp->p_offset;
     91 
     92 	if (pp->p_fstype == FS_RAID)
     93 		offset += RF_PROTECTED_SECTORS;
     94 
     95 	/*
     96 	 * Convert from blocks to bytes.
     97 	 */
     98 	offset *= DEV_BSIZE;
     99 
    100 	s = ARCBIOS->Seek(sc->sc_fd, &offset, 0);
    101 	s = ARCBIOS->Read(sc->sc_fd, addr, reqcnt, &count);
    102 
    103 	if (s < 0)
    104 		return (EIO);
    105 
    106 	*cnt = count;
    107 	return (0);
    108 }
    109 
    110 int
    111 diskopen(struct open_file *f, ...)
    112 {
    113 	int ctlr, unit, part;
    114 
    115 	struct disk_softc *sc;
    116 	struct disklabel *lp;
    117 	int i;
    118 	char *msg;
    119 	char buf[DEV_BSIZE];
    120 	int cnt;
    121 	char *device;
    122 	va_list ap;
    123 
    124 	va_start(ap, f);
    125 
    126 	device = va_arg(ap, char *);
    127 	ctlr = 0;		/* XXX extract from filename */
    128 	unit = 1;
    129 	part = 0;
    130 	if (unit >= 16 || part >= 16)
    131 		return (ENXIO);
    132 	printf("diskopen: %d,%d,%d %s\n", ctlr, unit, part, device);
    133 	/* NOTE: only support reads for now */
    134 
    135 	if (ARCBIOS->Open(device, 0, &i)) {
    136 		printf("open failed\n");
    137 		return (ENXIO);
    138 	}
    139 
    140 	sc = alloc(sizeof(struct disk_softc));
    141 	memset(sc, 0, sizeof(struct disk_softc));
    142 	f->f_devdata = (void *)sc;
    143 
    144 	sc->sc_fd = i;
    145 	sc->sc_ctlr = ctlr;
    146 	sc->sc_unit = unit;
    147 	sc->sc_part = part;
    148 
    149 	/* try to read disk label and partition table information */
    150 	lp = &sc->sc_label;
    151 	lp->d_secsize = DEV_BSIZE;
    152 	lp->d_secpercyl = 1;
    153 	lp->d_npartitions = MAXPARTITIONS;
    154 	lp->d_partitions[part].p_offset = 0;
    155 	lp->d_partitions[part].p_size = 0x7fffffff;
    156 
    157 	i = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
    158 	if (i || cnt != DEV_BSIZE) {
    159 		/* printf("disk%d: error reading disk label\n", unit); */
    160 		goto bad;
    161 	}
    162 	msg = getdisklabel(buf, lp);
    163 	if (msg) {
    164 		/* If no label, just assume 0 and return */
    165 		return (0);
    166 	}
    167 
    168 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
    169 	bad:
    170 		free(sc, sizeof(struct disk_softc));
    171 		return (ENXIO);
    172 	}
    173 	return (0);
    174 }
    175 
    176 #ifndef LIBSA_NO_DEV_CLOSE
    177 int
    178 diskclose(f)
    179 	struct open_file *f;
    180 {
    181 	ARCBIOS->Close(((struct disk_softc *)(f->f_devdata))->sc_fd);
    182 	free(f->f_devdata, sizeof(struct disk_softc));
    183 	f->f_devdata = (void *)0;
    184 	return (0);
    185 }
    186 #endif
    187