Home | History | Annotate | Line # | Download | only in common
saio.c revision 1.1
      1  1.1  wdk /*	$NetBSD: saio.c,v 1.1 2000/09/18 11:40:48 wdk Exp $	*/
      2  1.1  wdk 
      3  1.1  wdk /*
      4  1.1  wdk  * Copyright (c) 1992, 1993
      5  1.1  wdk  *	The Regents of the University of California.  All rights reserved.
      6  1.1  wdk  *
      7  1.1  wdk  * This code is derived from software contributed to Berkeley by
      8  1.1  wdk  * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
      9  1.1  wdk  *
     10  1.1  wdk  * Redistribution and use in source and binary forms, with or without
     11  1.1  wdk  * modification, are permitted provided that the following conditions
     12  1.1  wdk  * are met:
     13  1.1  wdk  * 1. Redistributions of source code must retain the above copyright
     14  1.1  wdk  *    notice, this list of conditions and the following disclaimer.
     15  1.1  wdk  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  wdk  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  wdk  *    documentation and/or other materials provided with the distribution.
     18  1.1  wdk  * 3. All advertising materials mentioning features or use of this software
     19  1.1  wdk  *    must display the following acknowledgement:
     20  1.1  wdk  *	This product includes software developed by the University of
     21  1.1  wdk  *	California, Berkeley and its contributors.
     22  1.1  wdk  * 4. Neither the name of the University nor the names of its contributors
     23  1.1  wdk  *    may be used to endorse or promote products derived from this software
     24  1.1  wdk  *    without specific prior written permission.
     25  1.1  wdk  *
     26  1.1  wdk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1  wdk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1  wdk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1  wdk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1  wdk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1  wdk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1  wdk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1  wdk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1  wdk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1  wdk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1  wdk  * SUCH DAMAGE.
     37  1.1  wdk  *
     38  1.1  wdk  *	@(#)rz.c	8.1 (Berkeley) 6/10/93
     39  1.1  wdk  */
     40  1.1  wdk 
     41  1.1  wdk #include <lib/libsa/stand.h>
     42  1.1  wdk #include <machine/prom.h>
     43  1.1  wdk #include <machine/stdarg.h>
     44  1.1  wdk 
     45  1.1  wdk #include <sys/param.h>
     46  1.1  wdk #include <sys/disklabel.h>
     47  1.1  wdk 
     48  1.1  wdk #include "common.h"
     49  1.1  wdk #include "saio.h"
     50  1.1  wdk 
     51  1.1  wdk struct	saio_softc {
     52  1.1  wdk 	int	sc_fd;			/* PROM file id */
     53  1.1  wdk 	int	sc_ctlr;		/* controller number */
     54  1.1  wdk 	int	sc_unit;		/* disk unit number */
     55  1.1  wdk 	int	sc_part;		/* disk partition number */
     56  1.1  wdk 	struct	disklabel sc_label;	/* disk label for this disk */
     57  1.1  wdk };
     58  1.1  wdk 
     59  1.1  wdk struct io_arg {
     60  1.1  wdk 	int retval;
     61  1.1  wdk 	unsigned long sectst;
     62  1.1  wdk 	unsigned long memaddr;
     63  1.1  wdk 	unsigned long datasz;
     64  1.1  wdk };
     65  1.1  wdk 
     66  1.1  wdk #define IOB_BUFSZ	512
     67  1.1  wdk #define IOB_INODESZ	312
     68  1.1  wdk 
     69  1.1  wdk struct device_table {
     70  1.1  wdk 	char *dt_string;	/* device name */
     71  1.1  wdk 	int (*dt_init) (int);	/* device init routine */
     72  1.1  wdk 	int (*dt_open) __P((int));	/* device open routine */
     73  1.1  wdk 	int (*dt_strategy) __P((int));	/* device strategy routine, returns cnt */
     74  1.1  wdk 	int (*dt_close) __P((int));	/* device close routine */
     75  1.1  wdk 	int (*dt_ioctl) __P((int));	/* device ioctl routine */
     76  1.1  wdk 	int dt_type;		/* device "type" */
     77  1.1  wdk 	int dt_fs;		/* file system type */
     78  1.1  wdk 	char *dt_desc;		/* device description */
     79  1.1  wdk };
     80  1.1  wdk 
     81  1.1  wdk struct sa_iob {
     82  1.1  wdk 	char	i_buf[IOB_BUFSZ];	/* file system or tape header */
     83  1.1  wdk 	char	i_ino_dir[IOB_INODESZ];	/* inode or disk/tape directory */
     84  1.1  wdk 
     85  1.1  wdk 	int	i_flgs;		/* see F_ below */
     86  1.1  wdk 	int	i_ctlr;		/* controller board */
     87  1.1  wdk 	int	i_unit;		/* pseudo device unit */
     88  1.1  wdk 	int	i_part;		/* disk partition */
     89  1.1  wdk 	char	*i_ma;		/* memory address of i/o buffer */
     90  1.1  wdk 	int	i_cc;		/* character count of transfer */
     91  1.1  wdk 	off_t	i_offset;	/* seek offset in file */
     92  1.1  wdk 	daddr_t	i_bn;		/* 1st block # of next read */
     93  1.1  wdk 	int	i_fstype;	/* file system type */
     94  1.1  wdk 	int	i_errno;	/* error # return */
     95  1.1  wdk 	unsigned int	i_devaddr;	/* csr address */
     96  1.1  wdk 	struct device_table *i_dp;	/* pointer into device_table */
     97  1.1  wdk 	char	*i_bufp;			/* i/o buffer for blk devs */
     98  1.1  wdk }__attribute__((__packed__));
     99  1.1  wdk 
    100  1.1  wdk extern struct sa_iob *saiob[];
    101  1.1  wdk 
    102  1.1  wdk int
    103  1.1  wdk saiostrategy(devdata, rw, bn, reqcnt, addr, cnt)
    104  1.1  wdk 	void *devdata;
    105  1.1  wdk 	int rw;
    106  1.1  wdk 	daddr_t bn;
    107  1.1  wdk 	size_t reqcnt;
    108  1.1  wdk 	void *addr;
    109  1.1  wdk 	size_t *cnt;	/* out: number of bytes transfered */
    110  1.1  wdk {
    111  1.1  wdk 	struct saio_softc *sc = (struct saio_softc *)devdata;
    112  1.1  wdk 	int part = sc->sc_part;
    113  1.1  wdk 	struct partition *pp = &sc->sc_label.d_partitions[part];
    114  1.1  wdk 	int s;
    115  1.1  wdk 	long offset;
    116  1.1  wdk 	struct sa_iob *iob;
    117  1.1  wdk 
    118  1.1  wdk 	offset = bn * DEV_BSIZE;
    119  1.1  wdk 	*cnt = 0;
    120  1.1  wdk 
    121  1.1  wdk 	/*
    122  1.1  wdk 	 * Partial-block transfers not handled.
    123  1.1  wdk 	 */
    124  1.1  wdk 	if (reqcnt & (DEV_BSIZE - 1)) {
    125  1.1  wdk 		return (EINVAL);
    126  1.1  wdk 	}
    127  1.1  wdk 	offset += pp->p_offset * DEV_BSIZE;
    128  1.1  wdk 
    129  1.1  wdk 	iob = saiob[sc->sc_fd];
    130  1.1  wdk 	iob->i_offset = offset;
    131  1.1  wdk 
    132  1.1  wdk #if notyet
    133  1.1  wdk 	if (prom_lseek(sc->sc_fd, offset, 0) < 0)
    134  1.1  wdk 		return (EIO);
    135  1.1  wdk #endif
    136  1.1  wdk 
    137  1.1  wdk 	while (*cnt < reqcnt) {
    138  1.1  wdk 		s = prom_read(sc->sc_fd, iob->i_buf, 512);
    139  1.1  wdk 		if (s < 0) {
    140  1.1  wdk 			return (EIO);
    141  1.1  wdk 		}
    142  1.1  wdk 		memcpy(addr, iob->i_buf, s);
    143  1.1  wdk 		*cnt += s;
    144  1.1  wdk 		(char *)addr += s;
    145  1.1  wdk 	}
    146  1.1  wdk 
    147  1.1  wdk 	return (0);
    148  1.1  wdk }
    149  1.1  wdk 
    150  1.1  wdk int
    151  1.1  wdk saioopen(struct open_file *f, ...)
    152  1.1  wdk {
    153  1.1  wdk 	int ctlr, unit, part;
    154  1.1  wdk 
    155  1.1  wdk 	struct saio_softc *sc;
    156  1.1  wdk 	struct disklabel *lp;
    157  1.1  wdk 	int i;
    158  1.1  wdk 	char *msg;
    159  1.1  wdk 	char buf[DEV_BSIZE];
    160  1.1  wdk 	int cnt;
    161  1.1  wdk 	static char device[] = "dksd(0,0,10)";
    162  1.1  wdk 
    163  1.1  wdk 	va_list ap;
    164  1.1  wdk 
    165  1.1  wdk 	va_start(ap, f);
    166  1.1  wdk 
    167  1.1  wdk 	ctlr = va_arg(ap, int);
    168  1.1  wdk 	unit = va_arg(ap, int);
    169  1.1  wdk 	part = va_arg(ap, int);
    170  1.1  wdk 	if (unit >= 8 || part >= 8)
    171  1.1  wdk 		return (ENXIO);
    172  1.1  wdk 	device[7] = '0' + unit;
    173  1.1  wdk 
    174  1.1  wdk 	i = MIPS_PROM(open)(device, 0);
    175  1.1  wdk 	if (i < 0) {
    176  1.1  wdk 		printf("open failed\n");
    177  1.1  wdk 		return (ENXIO);
    178  1.1  wdk 	}
    179  1.1  wdk 
    180  1.1  wdk 	sc = alloc(sizeof(struct saio_softc));
    181  1.1  wdk 	memset(sc, 0, sizeof(struct saio_softc));
    182  1.1  wdk 	f->f_devdata = (void *)sc;
    183  1.1  wdk 
    184  1.1  wdk 	sc->sc_fd = i;
    185  1.1  wdk 	sc->sc_ctlr = ctlr;
    186  1.1  wdk 	sc->sc_unit = unit;
    187  1.1  wdk 	sc->sc_part = part;
    188  1.1  wdk 
    189  1.1  wdk 	/* try to read disk label and partition table information */
    190  1.1  wdk 	lp = &sc->sc_label;
    191  1.1  wdk 	lp->d_secsize = DEV_BSIZE;
    192  1.1  wdk 	lp->d_secpercyl = 1;
    193  1.1  wdk 	lp->d_npartitions = MAXPARTITIONS;
    194  1.1  wdk 	lp->d_partitions[part].p_offset = 0;
    195  1.1  wdk 	lp->d_partitions[part].p_size = 0x7fffffff;
    196  1.1  wdk 
    197  1.1  wdk 	i = saiostrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
    198  1.1  wdk 	if (i || cnt != DEV_BSIZE) {
    199  1.1  wdk 		printf("%s: error reading disk label\n", device);
    200  1.1  wdk 		goto bad;
    201  1.1  wdk 	}
    202  1.1  wdk 
    203  1.1  wdk 	msg = getdisklabel(buf, lp);
    204  1.1  wdk 	if (msg) {
    205  1.1  wdk 		printf("getlabel: %s\n", msg);
    206  1.1  wdk 		return (0);
    207  1.1  wdk 	}
    208  1.1  wdk 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
    209  1.1  wdk 	bad:
    210  1.1  wdk 		free(sc, sizeof(struct saio_softc));
    211  1.1  wdk 		return (ENXIO);
    212  1.1  wdk 	}
    213  1.1  wdk 	return (0);
    214  1.1  wdk }
    215  1.1  wdk 
    216  1.1  wdk #ifndef LIBSA_NO_DEV_CLOSE
    217  1.1  wdk int
    218  1.1  wdk saioclose(f)
    219  1.1  wdk 	struct open_file *f;
    220  1.1  wdk {
    221  1.1  wdk 
    222  1.1  wdk 	prom_close(((struct saio_softc *)f->f_devdata)->sc_fd);
    223  1.1  wdk 	free(f->f_devdata, sizeof(struct saio_softc));
    224  1.1  wdk 	f->f_devdata = (void *)0;
    225  1.1  wdk 	return (0);
    226  1.1  wdk }
    227  1.1  wdk #endif
    228