xref: /netbsd-src/sys/arch/mipsco/stand/common/saio.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: saio.c,v 1.5 2003/01/24 21:55:13 fvdl 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/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 "saio.h"
51 
52 struct	saio_softc {
53 	int	sc_fd;			/* PROM file id */
54 	int	sc_ctlr;		/* controller number */
55 	int	sc_unit;		/* disk unit number */
56 	int	sc_part;		/* disk partition number */
57 	struct	disklabel sc_label;	/* disk label for this disk */
58 };
59 
60 struct io_arg {
61 	int retval;
62 	unsigned long sectst;
63 	unsigned long memaddr;
64 	unsigned long datasz;
65 };
66 
67 #define IOB_BUFSZ	512
68 #define IOB_INODESZ	316
69 
70 struct device_table {
71 	char *dt_string;	/* device name */
72 	int (*dt_init) (int);	/* device init routine */
73 	int (*dt_open) __P((int));	/* device open routine */
74 	int (*dt_strategy) __P((int));	/* device strategy routine, returns cnt */
75 	int (*dt_close) __P((int));	/* device close routine */
76 	int (*dt_ioctl) __P((int));	/* device ioctl routine */
77 	int dt_type;		/* device "type" */
78 	int dt_fs;		/* file system type */
79 	char *dt_desc;		/* device description */
80 };
81 
82 struct sa_iob {
83 	char	i_buf[IOB_BUFSZ];	/* file system or tape header */
84 	char	i_ino_dir[IOB_INODESZ];	/* inode or disk/tape directory */
85 
86 	int	i_flgs;		/* see F_ below */
87 	int	i_ctlr;		/* controller board */
88 	int	i_unit;		/* pseudo device unit */
89 	int	i_part;		/* disk partition */
90 	char	*i_ma;		/* memory address of i/o buffer */
91 	int	i_cc;		/* character count of transfer */
92 	int32_t	i_offset;	/* seek offset in file */
93 	/* XXX ondisk32 */
94 	int32_t	i_bn;		/* 1st block # of next read */
95 	int	i_fstype;	/* file system type */
96 	int	i_errno;	/* error # return */
97 	unsigned int	i_devaddr;	/* csr address */
98 	struct device_table *i_dp;	/* pointer into device_table */
99 	char	*i_bufp;			/* i/o buffer for blk devs */
100 }__attribute__((__packed__));
101 
102 extern struct sa_iob *saiob[];
103 
104 int
105 saiostrategy(devdata, rw, bn, reqcnt, addr, cnt)
106 	void *devdata;
107 	int rw;
108 	daddr_t bn;
109 	size_t reqcnt;
110 	void *addr;
111 	size_t *cnt;	/* out: number of bytes transfered */
112 {
113 	struct saio_softc *sc = (struct saio_softc *)devdata;
114 	int part = sc->sc_part;
115 	struct partition *pp = &sc->sc_label.d_partitions[part];
116 	int s;
117 	long offset;
118 	struct sa_iob *iob;
119 
120 	offset = bn * DEV_BSIZE;
121 	*cnt = 0;
122 
123 	/*
124 	 * Partial-block transfers not handled.
125 	 */
126 	if (reqcnt & (DEV_BSIZE - 1)) {
127 		return (EINVAL);
128 	}
129 	offset += pp->p_offset * DEV_BSIZE;
130 
131 	iob = saiob[sc->sc_fd];
132 	iob->i_offset = offset;
133 
134 #if notyet
135 	if (prom_lseek(sc->sc_fd, offset, 0) < 0)
136 		return (EIO);
137 #endif
138 
139 	while (*cnt < reqcnt) {
140 		s = prom_read(sc->sc_fd, iob->i_buf, 512);
141 		if (s < 0) {
142 			return (EIO);
143 		}
144 		memcpy(addr, iob->i_buf, s);
145 		*cnt += s;
146 		(char *)addr += s;
147 	}
148 	return (0);
149 }
150 
151 int
152 saioopen(struct open_file *f, ...)
153 {
154 	int ctlr, unit, part;
155 
156 	struct saio_softc *sc;
157 	struct disklabel *lp;
158 	int i;
159 	char *msg;
160 	char buf[DEV_BSIZE];
161 	int cnt;
162 	static char device[] = "dksd(0,0,10)";
163 
164 	va_list ap;
165 
166 	va_start(ap, f);
167 
168 	ctlr = va_arg(ap, int);
169 	unit = va_arg(ap, int);
170 	part = va_arg(ap, int);
171 
172 	va_end(ap);
173 
174 	device[5] = '0' + ctlr;
175 	device[7] = '0' + unit;
176 
177 	i = prom_open(device, 0);
178 	if (i < 0) {
179 		printf("open failed\n");
180 		return (ENXIO);
181 	}
182 
183 	sc = alloc(sizeof(struct saio_softc));
184 	memset(sc, 0, sizeof(struct saio_softc));
185 	f->f_devdata = (void *)sc;
186 
187 	sc->sc_fd = i;
188 	sc->sc_ctlr = ctlr;
189 	sc->sc_unit = unit;
190 	sc->sc_part = part;
191 
192 	/* try to read disk label and partition table information */
193 	lp = &sc->sc_label;
194 	lp->d_secsize = DEV_BSIZE;
195 	lp->d_secpercyl = 1;
196 	lp->d_npartitions = MAXPARTITIONS;
197 	lp->d_partitions[part].p_offset = 0;
198 	lp->d_partitions[part].p_size = 0x7fffffff;
199 
200 	i = saiostrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
201 	if (i || cnt != DEV_BSIZE) {
202 		printf("%s: error reading disk label\n", device);
203 		goto bad;
204 	}
205 
206 	msg = getdisklabel(buf, lp);
207 	if (msg) {
208 #ifdef LIBSA_NO_DISKLABEL_MSGS
209 		printf("%s: no disklabel\n", device);
210 #else
211 		printf("getlabel: %s\n", msg);
212 #endif
213 		return (0);
214 	}
215 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
216 	bad:
217 		free(sc, sizeof(struct saio_softc));
218 		return (ENXIO);
219 	}
220 	return (0);
221 }
222 
223 #ifndef LIBSA_NO_DEV_CLOSE
224 int
225 saioclose(f)
226 	struct open_file *f;
227 {
228 
229 	prom_close(((struct saio_softc *)f->f_devdata)->sc_fd);
230 	free(f->f_devdata, sizeof(struct saio_softc));
231 	f->f_devdata = (void *)0;
232 	return (0);
233 }
234 #endif
235