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