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