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