rd.c revision 1.3 1 /* $NetBSD: rd.c,v 1.3 2003/11/14 16:52:40 tsutsui Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1990, 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 * the Systems Programming Group of the University of Utah Computer
9 * Science Department.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * from: Utah Hdr: rd.c 1.20 92/12/21
36 *
37 * @(#)rd.c 8.1 (Berkeley) 7/15/93
38 */
39 /*
40 * Copyright (c) 1988 University of Utah.
41 *
42 * This code is derived from software contributed to Berkeley by
43 * the Systems Programming Group of the University of Utah Computer
44 * Science Department.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * from: Utah Hdr: rd.c 1.20 92/12/21
75 *
76 * @(#)rd.c 8.1 (Berkeley) 7/15/93
77 */
78
79 /*
80 * CS80/SS80 disk driver
81 */
82 #include <sys/param.h>
83 #include <sys/disklabel.h>
84
85 #include <machine/stdarg.h>
86
87 #include <lib/libsa/stand.h>
88
89 #include <hp300/dev/rdreg.h>
90
91 #include <hp300/stand/common/conf.h>
92 #include <hp300/stand/common/hpibvar.h>
93 #include <hp300/stand/common/samachdep.h>
94
95 struct rd_iocmd rd_ioc;
96 struct rd_rscmd rd_rsc;
97 struct rd_stat rd_stat;
98 struct rd_ssmcmd rd_ssmc;
99
100 struct disklabel rdlabel;
101
102 struct rdminilabel {
103 u_short npart;
104 u_long offset[MAXPARTITIONS];
105 };
106
107 struct rd_softc {
108 int sc_ctlr;
109 int sc_unit;
110 int sc_part;
111 char sc_retry;
112 char sc_alive;
113 short sc_type;
114 struct rdminilabel sc_pinfo;
115 };
116
117 #define RDRETRY 5
118
119 struct rdidentinfo {
120 short ri_hwid;
121 short ri_maxunum;
122 int ri_nblocks;
123 };
124
125 static int rdinit(int, int);
126 static int rdident(int, int);
127 static void rdreset(int, int);
128 static int rdgetinfo(struct rd_softc *);
129 static int rderror(int, int, int);
130
131 struct rd_softc rd_softc[NHPIB][NRD];
132
133 struct rdidentinfo rdidentinfo[] = {
134 { RD7946AID, 0, 108416 },
135 { RD9134DID, 1, 29088 },
136 { RD9134LID, 1, 1232 },
137 { RD7912PID, 0, 128128 },
138 { RD7914PID, 0, 258048 },
139 { RD7958AID, 0, 255276 },
140 { RD7957AID, 0, 159544 },
141 { RD7933HID, 0, 789958 },
142 { RD9134LID, 1, 77840 },
143 { RD7936HID, 0, 600978 },
144 { RD7937HID, 0, 1116102 },
145 { RD7914CTID, 0, 258048 },
146 { RD7946AID, 0, 108416 },
147 { RD9134LID, 1, 1232 },
148 { RD7957BID, 0, 159894 },
149 { RD7958BID, 0, 297108 },
150 { RD7959BID, 0, 594216 },
151 { RD2200AID, 0, 654948 },
152 { RD2203AID, 0, 1309896 }
153 };
154 int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
155
156 int
157 rdinit(ctlr, unit)
158 int ctlr, unit;
159 {
160 struct rd_softc *rs = &rd_softc[ctlr][unit];
161
162 rs->sc_type = rdident(ctlr, unit);
163 if (rs->sc_type < 0)
164 return 0;
165 rs->sc_alive = 1;
166 return 1;
167 }
168
169 static void
170 rdreset(ctlr, unit)
171 int ctlr, unit;
172 {
173 u_char stat;
174
175 rd_ssmc.c_unit = C_SUNIT(0);
176 rd_ssmc.c_cmd = C_SSM;
177 rd_ssmc.c_refm = REF_MASK;
178 rd_ssmc.c_fefm = FEF_MASK;
179 rd_ssmc.c_aefm = AEF_MASK;
180 rd_ssmc.c_iefm = IEF_MASK;
181 hpibsend(ctlr, unit, C_CMD, (char *)&rd_ssmc, sizeof(rd_ssmc));
182 hpibswait(ctlr, unit);
183 hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
184 }
185
186 static int
187 rdident(ctlr, unit)
188 int ctlr, unit;
189 {
190 struct rd_describe desc;
191 u_char stat, cmd[3];
192 char name[7];
193 int id, i;
194
195 id = hpibid(ctlr, unit);
196 if ((id & 0x200) == 0)
197 return -1;
198 for (i = 0; i < numrdidentinfo; i++)
199 if (id == rdidentinfo[i].ri_hwid)
200 break;
201 if (i == numrdidentinfo)
202 return -1;
203 id = i;
204 rdreset(ctlr, unit);
205 cmd[0] = C_SUNIT(0);
206 cmd[1] = C_SVOL(0);
207 cmd[2] = C_DESC;
208 hpibsend(ctlr, unit, C_CMD, cmd, sizeof(cmd));
209 hpibrecv(ctlr, unit, C_EXEC, (char *)&desc, 37);
210 hpibrecv(ctlr, unit, C_QSTAT, &stat, sizeof(stat));
211 memset(name, 0, sizeof(name));
212 if (!stat) {
213 int n = desc.d_name;
214 for (i = 5; i >= 0; i--) {
215 name[i] = (n & 0xf) + '0';
216 n >>= 4;
217 }
218 }
219 /*
220 * Take care of a couple of anomolies:
221 * 1. 7945A and 7946A both return same HW id
222 * 2. 9122S and 9134D both return same HW id
223 * 3. 9122D and 9134L both return same HW id
224 */
225 switch (rdidentinfo[id].ri_hwid) {
226 case RD7946AID:
227 if (memcmp(name, "079450", 6) == 0)
228 id = RD7945A;
229 else
230 id = RD7946A;
231 break;
232
233 case RD9134LID:
234 if (memcmp(name, "091340", 6) == 0)
235 id = RD9134L;
236 else
237 id = RD9122D;
238 break;
239
240 case RD9134DID:
241 if (memcmp(name, "091220", 6) == 0)
242 id = RD9122S;
243 else
244 id = RD9134D;
245 break;
246 }
247 return id;
248 }
249
250 #ifdef COMPAT_NOLABEL
251 int rdcyloff[][8] = {
252 { 1, 143, 0, 143, 0, 0, 323, 503, }, /* 7945A */
253 { 1, 167, 0, 0, 0, 0, 0, 0, }, /* 9134D */
254 { 0, 0, 0, 0, 0, 0, 0, 0, }, /* 9122S */
255 { 0, 71, 0, 221, 292, 542, 221, 0, }, /* 7912P */
256 { 1, 72, 0, 72, 362, 802, 252, 362, }, /* 7914P */
257 { 1, 28, 0, 140, 167, 444, 140, 721, }, /* 7933H */
258 { 1, 200, 0, 200, 0, 0, 450, 600, }, /* 9134L */
259 { 1, 105, 0, 105, 380, 736, 265, 380, }, /* 7957A */
260 { 1, 65, 0, 65, 257, 657, 193, 257, }, /* 7958A */
261 { 1, 128, 0, 128, 518, 918, 388, 518, }, /* 7957B */
262 { 1, 44, 0, 44, 174, 496, 131, 174, }, /* 7958B */
263 { 1, 44, 0, 44, 218, 1022,174, 218, }, /* 7959B */
264 { 1, 37, 0, 37, 183, 857, 147, 183, }, /* 2200A */
265 { 1, 19, 0, 94, 112, 450, 94, 788, }, /* 2203A */
266 { 1, 20, 0, 98, 117, 256, 98, 397, }, /* 7936H */
267 { 1, 11, 0, 53, 63, 217, 53, 371, }, /* 7937H */
268 };
269
270 struct rdcompatinfo {
271 int nbpc;
272 int *cyloff;
273 } rdcompatinfo[] = {
274 { NRD7945ABPT * NRD7945ATRK, rdcyloff[0] },
275 { NRD9134DBPT * NRD9134DTRK, rdcyloff[1] },
276 { NRD9122SBPT * NRD9122STRK, rdcyloff[2] },
277 { NRD7912PBPT * NRD7912PTRK, rdcyloff[3] },
278 { NRD7914PBPT * NRD7914PTRK, rdcyloff[4] },
279 { NRD7958ABPT * NRD7958ATRK, rdcyloff[8] },
280 { NRD7957ABPT * NRD7957ATRK, rdcyloff[7] },
281 { NRD7933HBPT * NRD7933HTRK, rdcyloff[5] },
282 { NRD9134LBPT * NRD9134LTRK, rdcyloff[6] },
283 { NRD7936HBPT * NRD7936HTRK, rdcyloff[14] },
284 { NRD7937HBPT * NRD7937HTRK, rdcyloff[15] },
285 { NRD7914PBPT * NRD7914PTRK, rdcyloff[4] },
286 { NRD7945ABPT * NRD7945ATRK, rdcyloff[0] },
287 { NRD9122SBPT * NRD9122STRK, rdcyloff[2] },
288 { NRD7957BBPT * NRD7957BTRK, rdcyloff[9] },
289 { NRD7958BBPT * NRD7958BTRK, rdcyloff[10] },
290 { NRD7959BBPT * NRD7959BTRK, rdcyloff[11] },
291 { NRD2200ABPT * NRD2200ATRK, rdcyloff[12] },
292 { NRD2203ABPT * NRD2203ATRK, rdcyloff[13] },
293 };
294 int nrdcompatinfo = sizeof(rdcompatinfo) / sizeof(rdcompatinfo[0]);
295 #endif
296
297 char io_buf[MAXBSIZE];
298
299 static int
300 rdgetinfo(rs)
301 struct rd_softc *rs;
302 {
303 struct rdminilabel *pi = &rs->sc_pinfo;
304 struct disklabel *lp = &rdlabel;
305 char *msg;
306 int err, savepart;
307 size_t i;
308
309 memset((caddr_t)lp, 0, sizeof *lp);
310 lp->d_secsize = DEV_BSIZE;
311
312 /* Disklabel is always from RAW_PART. */
313 savepart = rs->sc_part;
314 rs->sc_part = RAW_PART;
315 err = rdstrategy(rs, F_READ, LABELSECTOR,
316 lp->d_secsize ? lp->d_secsize : DEV_BSIZE, io_buf, &i);
317 rs->sc_part = savepart;
318
319 if (err) {
320 printf("rdgetinfo: rdstrategy error %d\n", err);
321 return 0;
322 }
323
324 msg = getdisklabel(io_buf, lp);
325 if (msg) {
326 printf("rd(%d,%d,%d): WARNING: %s, ",
327 rs->sc_ctlr, rs->sc_unit, rs->sc_part, msg);
328 #ifdef COMPAT_NOLABEL
329 {
330 struct rdcompatinfo *ci;
331
332 printf("using old default partitioning\n");
333 ci = &rdcompatinfo[rs->sc_type];
334 pi->npart = 8;
335 for (i = 0; i < pi->npart; i++)
336 pi->offset[i] = ci->cyloff[i] * ci->nbpc;
337 }
338 #else
339 printf("defining `c' partition as entire disk\n");
340 pi->npart = 3;
341 pi->offset[0] = pi->offset[1] = -1;
342 pi->offset[2] = 0;
343 #endif
344 } else {
345 pi->npart = lp->d_npartitions;
346 for (i = 0; i < pi->npart; i++)
347 pi->offset[i] = lp->d_partitions[i].p_size == 0 ?
348 -1 : lp->d_partitions[i].p_offset;
349 }
350 return 1;
351 }
352
353 int
354 rdopen(struct open_file *f, ...)
355 {
356 va_list ap;
357 int ctlr, unit, part;
358 struct rd_softc *rs;
359
360 va_start(ap, f);
361 ctlr = va_arg(ap, int);
362 unit = va_arg(ap, int);
363 part = va_arg(ap, int);
364 va_end(ap);
365
366 if (ctlr >= NHPIB || hpibalive(ctlr) == 0)
367 return EADAPT;
368 if (unit >= NRD)
369 return ECTLR;
370 rs = &rd_softc[ctlr][unit];
371 rs->sc_part = part;
372 rs->sc_unit = unit;
373 rs->sc_ctlr = ctlr;
374 if (rs->sc_alive == 0) {
375 if (rdinit(ctlr, unit) == 0)
376 return ENXIO;
377 if (rdgetinfo(rs) == 0)
378 return ERDLAB;
379 }
380 if (part != RAW_PART && /* always allow RAW_PART to be opened */
381 (part >= rs->sc_pinfo.npart || rs->sc_pinfo.offset[part] == -1))
382 return EPART;
383 f->f_devdata = (void *)rs;
384 return 0;
385 }
386
387 int
388 rdclose(f)
389 struct open_file *f;
390 {
391 struct rd_softc *rs = f->f_devdata;
392
393 /*
394 * Mark the disk `not alive' so that the disklabel
395 * will be re-loaded at next open.
396 */
397 memset(rs, 0, sizeof(struct rd_softc));
398 f->f_devdata = NULL;
399
400 return 0;
401 }
402
403 int
404 rdstrategy(devdata, func, dblk, size, v_buf, rsize)
405 void *devdata;
406 int func;
407 daddr_t dblk;
408 size_t size;
409 void *v_buf;
410 size_t *rsize;
411 {
412 char *buf = v_buf;
413 struct rd_softc *rs = devdata;
414 int ctlr = rs->sc_ctlr;
415 int unit = rs->sc_unit;
416 daddr_t blk;
417 char stat;
418
419 if (size == 0)
420 return 0;
421
422 /*
423 * Don't do partition translation on the `raw partition'.
424 */
425 blk = (dblk + ((rs->sc_part == RAW_PART) ? 0 :
426 rs->sc_pinfo.offset[rs->sc_part]));
427
428 rs->sc_retry = 0;
429 rd_ioc.c_unit = C_SUNIT(0);
430 rd_ioc.c_volume = C_SVOL(0);
431 rd_ioc.c_saddr = C_SADDR;
432 rd_ioc.c_hiaddr = 0;
433 rd_ioc.c_addr = RDBTOS(blk);
434 rd_ioc.c_nop2 = C_NOP;
435 rd_ioc.c_slen = C_SLEN;
436 rd_ioc.c_len = size;
437 rd_ioc.c_cmd = func == F_READ ? C_READ : C_WRITE;
438 retry:
439 hpibsend(ctlr, unit, C_CMD, &rd_ioc.c_unit, sizeof(rd_ioc)-2);
440 hpibswait(ctlr, unit);
441 hpibgo(ctlr, unit, C_EXEC, buf, size, func);
442 hpibswait(ctlr, unit);
443 hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
444 if (stat) {
445 if (rderror(ctlr, unit, rs->sc_part) == 0)
446 return EIO;
447 if (++rs->sc_retry > RDRETRY)
448 return EIO;
449 goto retry;
450 }
451 *rsize = size;
452
453 return 0;
454 }
455
456 static int
457 rderror(ctlr, unit, part)
458 int ctlr, unit;
459 int part;
460 {
461 char stat;
462
463 rd_rsc.c_unit = C_SUNIT(0);
464 rd_rsc.c_sram = C_SRAM;
465 rd_rsc.c_ram = C_RAM;
466 rd_rsc.c_cmd = C_STATUS;
467 hpibsend(ctlr, unit, C_CMD, (char *)&rd_rsc, sizeof(rd_rsc));
468 hpibrecv(ctlr, unit, C_EXEC, (char *)&rd_stat, sizeof(rd_stat));
469 hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
470 if (stat) {
471 printf("rd(%d,%d,0,%d): request status fail %d\n",
472 ctlr, unit, part, stat);
473 return 0;
474 }
475 printf("rd(%d,%d,0,%d) err: vu 0x%x",
476 ctlr, unit, part, rd_stat.c_vu);
477 if ((rd_stat.c_aef & AEF_UD) || (rd_stat.c_ief & (IEF_MD|IEF_RD)))
478 printf(", block %ld", rd_stat.c_blk);
479 printf(", R0x%x F0x%x A0x%x I0x%x\n",
480 rd_stat.c_ref, rd_stat.c_fef, rd_stat.c_aef, rd_stat.c_ief);
481 return 1;
482 }
483