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