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