rd.c revision 1.14 1 1.14 tsutsui /* $NetBSD: rd.c,v 1.14 2022/11/21 16:22:37 tsutsui Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.9 rmind * Copyright (c) 1988 University of Utah.
5 1.1 thorpej * Copyright (c) 1982, 1990, 1993
6 1.1 thorpej * The Regents of the University of California. All rights reserved.
7 1.2 agc *
8 1.2 agc * This code is derived from software contributed to Berkeley by
9 1.2 agc * the Systems Programming Group of the University of Utah Computer
10 1.2 agc * Science Department.
11 1.2 agc *
12 1.2 agc * Redistribution and use in source and binary forms, with or without
13 1.2 agc * modification, are permitted provided that the following conditions
14 1.2 agc * are met:
15 1.2 agc * 1. Redistributions of source code must retain the above copyright
16 1.2 agc * notice, this list of conditions and the following disclaimer.
17 1.2 agc * 2. Redistributions in binary form must reproduce the above copyright
18 1.2 agc * notice, this list of conditions and the following disclaimer in the
19 1.2 agc * documentation and/or other materials provided with the distribution.
20 1.2 agc * 3. Neither the name of the University nor the names of its contributors
21 1.2 agc * may be used to endorse or promote products derived from this software
22 1.2 agc * without specific prior written permission.
23 1.2 agc *
24 1.2 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.2 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.2 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.2 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.2 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.2 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.2 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.2 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.2 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.2 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.2 agc * SUCH DAMAGE.
35 1.2 agc *
36 1.2 agc * from: Utah Hdr: rd.c 1.20 92/12/21
37 1.2 agc *
38 1.2 agc * @(#)rd.c 8.1 (Berkeley) 7/15/93
39 1.2 agc */
40 1.1 thorpej
41 1.1 thorpej /*
42 1.1 thorpej * CS80/SS80 disk driver
43 1.1 thorpej */
44 1.1 thorpej #include <sys/param.h>
45 1.1 thorpej #include <sys/disklabel.h>
46 1.1 thorpej
47 1.1 thorpej #include <lib/libsa/stand.h>
48 1.1 thorpej
49 1.1 thorpej #include <hp300/dev/rdreg.h>
50 1.1 thorpej
51 1.3 tsutsui #include <hp300/stand/common/conf.h>
52 1.3 tsutsui #include <hp300/stand/common/hpibvar.h>
53 1.1 thorpej #include <hp300/stand/common/samachdep.h>
54 1.1 thorpej
55 1.13 tsutsui static struct rd_iocmd rd_ioc;
56 1.13 tsutsui static struct rd_rscmd rd_rsc;
57 1.13 tsutsui static struct rd_stat rd_stat;
58 1.13 tsutsui static struct rd_ssmcmd rd_ssmc;
59 1.1 thorpej
60 1.13 tsutsui static struct disklabel rdlabel;
61 1.1 thorpej
62 1.1 thorpej struct rdminilabel {
63 1.1 thorpej u_short npart;
64 1.1 thorpej u_long offset[MAXPARTITIONS];
65 1.1 thorpej };
66 1.1 thorpej
67 1.1 thorpej struct rd_softc {
68 1.1 thorpej int sc_ctlr;
69 1.1 thorpej int sc_unit;
70 1.1 thorpej int sc_part;
71 1.1 thorpej char sc_retry;
72 1.1 thorpej char sc_alive;
73 1.1 thorpej short sc_type;
74 1.1 thorpej struct rdminilabel sc_pinfo;
75 1.3 tsutsui };
76 1.1 thorpej
77 1.1 thorpej #define RDRETRY 5
78 1.1 thorpej
79 1.1 thorpej struct rdidentinfo {
80 1.1 thorpej short ri_hwid;
81 1.1 thorpej short ri_maxunum;
82 1.1 thorpej int ri_nblocks;
83 1.3 tsutsui };
84 1.3 tsutsui
85 1.3 tsutsui static int rdinit(int, int);
86 1.3 tsutsui static int rdident(int, int);
87 1.3 tsutsui static void rdreset(int, int);
88 1.3 tsutsui static int rdgetinfo(struct rd_softc *);
89 1.3 tsutsui static int rderror(int, int, int);
90 1.3 tsutsui
91 1.13 tsutsui static struct rd_softc rd_softc[NHPIB][NRD];
92 1.3 tsutsui
93 1.13 tsutsui static const struct rdidentinfo rdidentinfo[] = {
94 1.14 tsutsui [RD7945A] = { RD7946AID, 0, NRD7945ABLK },
95 1.14 tsutsui [RD9134D] = { RD9134DID, 1, NRD9134DBLK },
96 1.14 tsutsui [RD9122S] = { RD9134LID, 1, NRD9122SBLK },
97 1.14 tsutsui [RD7912P] = { RD7912PID, 0, NRD7912PBLK },
98 1.14 tsutsui [RD7914P] = { RD7914PID, 0, NRD7914PBLK },
99 1.14 tsutsui [RD7958A] = { RD7958AID, 0, NRD7958ABLK },
100 1.14 tsutsui [RD7957A] = { RD7957AID, 0, NRD7957ABLK },
101 1.14 tsutsui [RD7933H] = { RD7933HID, 0, NRD7933HBLK },
102 1.14 tsutsui [RD9134L] = { RD9134LID, 1, NRD9134LBLK },
103 1.14 tsutsui [RD7936H] = { RD7936HID, 0, NRD7936HBLK },
104 1.14 tsutsui [RD7937H] = { RD7937HID, 0, NRD7937HBLK },
105 1.14 tsutsui [RD7914CT] = { RD7914CTID, 0, NRD7914PBLK },
106 1.14 tsutsui [RD7946A] = { RD7946AID, 0, NRD7945ABLK },
107 1.14 tsutsui [RD9122D] = { RD9134LID, 1, NRD9122SBLK },
108 1.14 tsutsui [RD7957B] = { RD7957BID, 0, NRD7957BBLK },
109 1.14 tsutsui [RD7958B] = { RD7958BID, 0, NRD7958BBLK },
110 1.14 tsutsui [RD7959B] = { RD7959BID, 0, NRD7959BBLK },
111 1.14 tsutsui [RD2200A] = { RD2200AID, 0, NRD2200ABLK },
112 1.14 tsutsui [RD2203A] = { RD2203AID, 0, NRD2203ABLK },
113 1.14 tsutsui [RD2202A] = { RD2202AID, 0, NRD2202ABLK },
114 1.14 tsutsui [RD7908A] = { RD7908AID, 0, NRD7908ABLK },
115 1.14 tsutsui [RD7911A] = { RD7911AID, 0, NRD7911ABLK },
116 1.14 tsutsui [RD7941A] = { RD7946AID, 0, NRD7941ABLK }
117 1.1 thorpej };
118 1.13 tsutsui static const int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
119 1.1 thorpej
120 1.13 tsutsui static int
121 1.5 tsutsui rdinit(int ctlr, int unit)
122 1.1 thorpej {
123 1.3 tsutsui struct rd_softc *rs = &rd_softc[ctlr][unit];
124 1.1 thorpej
125 1.1 thorpej rs->sc_type = rdident(ctlr, unit);
126 1.1 thorpej if (rs->sc_type < 0)
127 1.3 tsutsui return 0;
128 1.1 thorpej rs->sc_alive = 1;
129 1.3 tsutsui return 1;
130 1.1 thorpej }
131 1.1 thorpej
132 1.3 tsutsui static void
133 1.5 tsutsui rdreset(int ctlr, int unit)
134 1.1 thorpej {
135 1.7 tsutsui uint8_t stat;
136 1.1 thorpej
137 1.1 thorpej rd_ssmc.c_unit = C_SUNIT(0);
138 1.1 thorpej rd_ssmc.c_cmd = C_SSM;
139 1.1 thorpej rd_ssmc.c_refm = REF_MASK;
140 1.1 thorpej rd_ssmc.c_fefm = FEF_MASK;
141 1.1 thorpej rd_ssmc.c_aefm = AEF_MASK;
142 1.1 thorpej rd_ssmc.c_iefm = IEF_MASK;
143 1.7 tsutsui hpibsend(ctlr, unit, C_CMD, (uint8_t *)&rd_ssmc, sizeof(rd_ssmc));
144 1.1 thorpej hpibswait(ctlr, unit);
145 1.1 thorpej hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
146 1.1 thorpej }
147 1.1 thorpej
148 1.3 tsutsui static int
149 1.5 tsutsui rdident(int ctlr, int unit)
150 1.1 thorpej {
151 1.11 tsutsui struct cs80_describe desc;
152 1.7 tsutsui uint8_t stat, cmd[3];
153 1.1 thorpej char name[7];
154 1.3 tsutsui int id, i;
155 1.1 thorpej
156 1.1 thorpej id = hpibid(ctlr, unit);
157 1.1 thorpej if ((id & 0x200) == 0)
158 1.3 tsutsui return -1;
159 1.1 thorpej for (i = 0; i < numrdidentinfo; i++)
160 1.1 thorpej if (id == rdidentinfo[i].ri_hwid)
161 1.1 thorpej break;
162 1.1 thorpej if (i == numrdidentinfo)
163 1.3 tsutsui return -1;
164 1.1 thorpej id = i;
165 1.1 thorpej rdreset(ctlr, unit);
166 1.1 thorpej cmd[0] = C_SUNIT(0);
167 1.1 thorpej cmd[1] = C_SVOL(0);
168 1.1 thorpej cmd[2] = C_DESC;
169 1.1 thorpej hpibsend(ctlr, unit, C_CMD, cmd, sizeof(cmd));
170 1.11 tsutsui hpibrecv(ctlr, unit, C_EXEC, (uint8_t *)&desc, sizeof(desc));
171 1.1 thorpej hpibrecv(ctlr, unit, C_QSTAT, &stat, sizeof(stat));
172 1.3 tsutsui memset(name, 0, sizeof(name));
173 1.1 thorpej if (!stat) {
174 1.3 tsutsui int n = desc.d_name;
175 1.1 thorpej for (i = 5; i >= 0; i--) {
176 1.1 thorpej name[i] = (n & 0xf) + '0';
177 1.1 thorpej n >>= 4;
178 1.1 thorpej }
179 1.1 thorpej }
180 1.1 thorpej /*
181 1.1 thorpej * Take care of a couple of anomolies:
182 1.1 thorpej * 1. 7945A and 7946A both return same HW id
183 1.1 thorpej * 2. 9122S and 9134D both return same HW id
184 1.1 thorpej * 3. 9122D and 9134L both return same HW id
185 1.1 thorpej */
186 1.1 thorpej switch (rdidentinfo[id].ri_hwid) {
187 1.1 thorpej case RD7946AID:
188 1.3 tsutsui if (memcmp(name, "079450", 6) == 0)
189 1.1 thorpej id = RD7945A;
190 1.12 tsutsui else if (memcmp(name, "079410", 6) == 0)
191 1.12 tsutsui id = RD7941A;
192 1.1 thorpej else
193 1.1 thorpej id = RD7946A;
194 1.1 thorpej break;
195 1.1 thorpej
196 1.1 thorpej case RD9134LID:
197 1.3 tsutsui if (memcmp(name, "091340", 6) == 0)
198 1.1 thorpej id = RD9134L;
199 1.1 thorpej else
200 1.1 thorpej id = RD9122D;
201 1.1 thorpej break;
202 1.1 thorpej
203 1.1 thorpej case RD9134DID:
204 1.3 tsutsui if (memcmp(name, "091220", 6) == 0)
205 1.1 thorpej id = RD9122S;
206 1.1 thorpej else
207 1.1 thorpej id = RD9134D;
208 1.1 thorpej break;
209 1.1 thorpej }
210 1.3 tsutsui return id;
211 1.1 thorpej }
212 1.1 thorpej
213 1.13 tsutsui static char io_buf[MAXBSIZE];
214 1.1 thorpej
215 1.3 tsutsui static int
216 1.5 tsutsui rdgetinfo(struct rd_softc *rs)
217 1.1 thorpej {
218 1.3 tsutsui struct rdminilabel *pi = &rs->sc_pinfo;
219 1.3 tsutsui struct disklabel *lp = &rdlabel;
220 1.3 tsutsui char *msg;
221 1.3 tsutsui int err, savepart;
222 1.1 thorpej size_t i;
223 1.1 thorpej
224 1.8 christos memset((void *)lp, 0, sizeof *lp);
225 1.1 thorpej lp->d_secsize = DEV_BSIZE;
226 1.1 thorpej
227 1.1 thorpej /* Disklabel is always from RAW_PART. */
228 1.1 thorpej savepart = rs->sc_part;
229 1.1 thorpej rs->sc_part = RAW_PART;
230 1.1 thorpej err = rdstrategy(rs, F_READ, LABELSECTOR,
231 1.1 thorpej lp->d_secsize ? lp->d_secsize : DEV_BSIZE, io_buf, &i);
232 1.1 thorpej rs->sc_part = savepart;
233 1.1 thorpej
234 1.1 thorpej if (err) {
235 1.1 thorpej printf("rdgetinfo: rdstrategy error %d\n", err);
236 1.3 tsutsui return 0;
237 1.1 thorpej }
238 1.1 thorpej
239 1.1 thorpej msg = getdisklabel(io_buf, lp);
240 1.1 thorpej if (msg) {
241 1.4 thorpej printf("rd(%d,%d,%d): WARNING: %s\n",
242 1.1 thorpej rs->sc_ctlr, rs->sc_unit, rs->sc_part, msg);
243 1.1 thorpej pi->npart = 3;
244 1.1 thorpej pi->offset[0] = pi->offset[1] = -1;
245 1.1 thorpej pi->offset[2] = 0;
246 1.1 thorpej } else {
247 1.1 thorpej pi->npart = lp->d_npartitions;
248 1.1 thorpej for (i = 0; i < pi->npart; i++)
249 1.1 thorpej pi->offset[i] = lp->d_partitions[i].p_size == 0 ?
250 1.1 thorpej -1 : lp->d_partitions[i].p_offset;
251 1.1 thorpej }
252 1.3 tsutsui return 1;
253 1.1 thorpej }
254 1.1 thorpej
255 1.3 tsutsui int
256 1.3 tsutsui rdopen(struct open_file *f, ...)
257 1.3 tsutsui {
258 1.3 tsutsui va_list ap;
259 1.1 thorpej int ctlr, unit, part;
260 1.3 tsutsui struct rd_softc *rs;
261 1.3 tsutsui
262 1.3 tsutsui va_start(ap, f);
263 1.3 tsutsui ctlr = va_arg(ap, int);
264 1.3 tsutsui unit = va_arg(ap, int);
265 1.3 tsutsui part = va_arg(ap, int);
266 1.3 tsutsui va_end(ap);
267 1.1 thorpej
268 1.1 thorpej if (ctlr >= NHPIB || hpibalive(ctlr) == 0)
269 1.3 tsutsui return EADAPT;
270 1.1 thorpej if (unit >= NRD)
271 1.3 tsutsui return ECTLR;
272 1.1 thorpej rs = &rd_softc[ctlr][unit];
273 1.1 thorpej rs->sc_part = part;
274 1.1 thorpej rs->sc_unit = unit;
275 1.1 thorpej rs->sc_ctlr = ctlr;
276 1.1 thorpej if (rs->sc_alive == 0) {
277 1.1 thorpej if (rdinit(ctlr, unit) == 0)
278 1.3 tsutsui return ENXIO;
279 1.1 thorpej if (rdgetinfo(rs) == 0)
280 1.3 tsutsui return ERDLAB;
281 1.1 thorpej }
282 1.1 thorpej if (part != RAW_PART && /* always allow RAW_PART to be opened */
283 1.1 thorpej (part >= rs->sc_pinfo.npart || rs->sc_pinfo.offset[part] == -1))
284 1.3 tsutsui return EPART;
285 1.1 thorpej f->f_devdata = (void *)rs;
286 1.3 tsutsui return 0;
287 1.1 thorpej }
288 1.1 thorpej
289 1.3 tsutsui int
290 1.5 tsutsui rdclose(struct open_file *f)
291 1.1 thorpej {
292 1.1 thorpej struct rd_softc *rs = f->f_devdata;
293 1.1 thorpej
294 1.1 thorpej /*
295 1.1 thorpej * Mark the disk `not alive' so that the disklabel
296 1.1 thorpej * will be re-loaded at next open.
297 1.1 thorpej */
298 1.3 tsutsui memset(rs, 0, sizeof(struct rd_softc));
299 1.1 thorpej f->f_devdata = NULL;
300 1.1 thorpej
301 1.3 tsutsui return 0;
302 1.1 thorpej }
303 1.1 thorpej
304 1.3 tsutsui int
305 1.5 tsutsui rdstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *v_buf,
306 1.5 tsutsui size_t *rsize)
307 1.1 thorpej {
308 1.7 tsutsui uint8_t *buf = v_buf;
309 1.1 thorpej struct rd_softc *rs = devdata;
310 1.3 tsutsui int ctlr = rs->sc_ctlr;
311 1.3 tsutsui int unit = rs->sc_unit;
312 1.1 thorpej daddr_t blk;
313 1.7 tsutsui uint8_t stat;
314 1.1 thorpej
315 1.1 thorpej if (size == 0)
316 1.3 tsutsui return 0;
317 1.1 thorpej
318 1.1 thorpej /*
319 1.1 thorpej * Don't do partition translation on the `raw partition'.
320 1.1 thorpej */
321 1.1 thorpej blk = (dblk + ((rs->sc_part == RAW_PART) ? 0 :
322 1.1 thorpej rs->sc_pinfo.offset[rs->sc_part]));
323 1.1 thorpej
324 1.1 thorpej rs->sc_retry = 0;
325 1.1 thorpej rd_ioc.c_unit = C_SUNIT(0);
326 1.1 thorpej rd_ioc.c_volume = C_SVOL(0);
327 1.1 thorpej rd_ioc.c_saddr = C_SADDR;
328 1.1 thorpej rd_ioc.c_hiaddr = 0;
329 1.1 thorpej rd_ioc.c_addr = RDBTOS(blk);
330 1.1 thorpej rd_ioc.c_nop2 = C_NOP;
331 1.1 thorpej rd_ioc.c_slen = C_SLEN;
332 1.1 thorpej rd_ioc.c_len = size;
333 1.1 thorpej rd_ioc.c_cmd = func == F_READ ? C_READ : C_WRITE;
334 1.1 thorpej retry:
335 1.7 tsutsui hpibsend(ctlr, unit, C_CMD, (uint8_t *)&rd_ioc.c_unit,
336 1.7 tsutsui sizeof(rd_ioc) - 2);
337 1.1 thorpej hpibswait(ctlr, unit);
338 1.1 thorpej hpibgo(ctlr, unit, C_EXEC, buf, size, func);
339 1.1 thorpej hpibswait(ctlr, unit);
340 1.1 thorpej hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
341 1.1 thorpej if (stat) {
342 1.1 thorpej if (rderror(ctlr, unit, rs->sc_part) == 0)
343 1.3 tsutsui return EIO;
344 1.1 thorpej if (++rs->sc_retry > RDRETRY)
345 1.3 tsutsui return EIO;
346 1.1 thorpej goto retry;
347 1.1 thorpej }
348 1.1 thorpej *rsize = size;
349 1.1 thorpej
350 1.3 tsutsui return 0;
351 1.1 thorpej }
352 1.1 thorpej
353 1.3 tsutsui static int
354 1.5 tsutsui rderror(int ctlr, int unit, int part)
355 1.1 thorpej {
356 1.7 tsutsui uint8_t stat;
357 1.1 thorpej
358 1.1 thorpej rd_rsc.c_unit = C_SUNIT(0);
359 1.1 thorpej rd_rsc.c_sram = C_SRAM;
360 1.1 thorpej rd_rsc.c_ram = C_RAM;
361 1.1 thorpej rd_rsc.c_cmd = C_STATUS;
362 1.7 tsutsui hpibsend(ctlr, unit, C_CMD, (uint8_t *)&rd_rsc, sizeof(rd_rsc));
363 1.7 tsutsui hpibrecv(ctlr, unit, C_EXEC, (uint8_t *)&rd_stat, sizeof(rd_stat));
364 1.1 thorpej hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
365 1.1 thorpej if (stat) {
366 1.1 thorpej printf("rd(%d,%d,0,%d): request status fail %d\n",
367 1.1 thorpej ctlr, unit, part, stat);
368 1.3 tsutsui return 0;
369 1.1 thorpej }
370 1.1 thorpej printf("rd(%d,%d,0,%d) err: vu 0x%x",
371 1.1 thorpej ctlr, unit, part, rd_stat.c_vu);
372 1.1 thorpej if ((rd_stat.c_aef & AEF_UD) || (rd_stat.c_ief & (IEF_MD|IEF_RD)))
373 1.3 tsutsui printf(", block %ld", rd_stat.c_blk);
374 1.1 thorpej printf(", R0x%x F0x%x A0x%x I0x%x\n",
375 1.1 thorpej rd_stat.c_ref, rd_stat.c_fef, rd_stat.c_aef, rd_stat.c_ief);
376 1.3 tsutsui return 1;
377 1.1 thorpej }
378