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