hp.c revision 1.21 1 /* $NetBSD: hp.c,v 1.21 2000/02/07 20:16:54 thorpej Exp $ */
2 /*
3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed at Ludd, University of
17 * Lule}, Sweden and its contributors.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Simple device driver routine for massbuss disks.
35 * TODO:
36 * Fix support for Standard DEC BAD144 bad block forwarding.
37 * Be able to to handle soft/hard transfer errors.
38 * Handle non-data transfer interrupts.
39 * Autoconfiguration of disk drives 'on the fly'.
40 * Handle disk media changes.
41 * Dual-port operations should be supported.
42 */
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/disklabel.h>
47 #include <sys/disk.h>
48 #include <sys/dkio.h>
49 #include <sys/buf.h>
50 #include <sys/stat.h>
51 #include <sys/ioccom.h>
52 #include <sys/fcntl.h>
53 #include <sys/syslog.h>
54 #include <sys/reboot.h>
55
56 #include <machine/trap.h>
57 #include <machine/pte.h>
58 #include <machine/mtpr.h>
59 #include <machine/cpu.h>
60 #include <machine/rpb.h>
61
62 #include <vax/mba/mbavar.h>
63 #include <vax/mba/mbareg.h>
64 #include <vax/mba/hpreg.h>
65
66 #include "locators.h"
67
68 #define HPMASK 0xffff
69
70 struct hp_softc {
71 struct device sc_dev;
72 struct disk sc_disk;
73 struct mba_device sc_md; /* Common struct used by mbaqueue. */
74 int sc_wlabel; /* Disklabel area is writable */
75 int sc_physnr; /* Physical disk number */
76 };
77
78 int hpmatch __P((struct device *, struct cfdata *, void *));
79 void hpattach __P((struct device *, struct device *, void *));
80 void hpstrategy __P((struct buf *));
81 void hpstart __P((struct mba_device *));
82 int hpattn __P((struct mba_device *));
83 enum xfer_action hpfinish __P((struct mba_device *, int, int *));
84 int hpopen __P((dev_t, int, int));
85 int hpclose __P((dev_t, int, int));
86 int hpioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
87 int hpdump __P((dev_t, caddr_t, caddr_t, size_t));
88 int hpread __P((dev_t, struct uio *));
89 int hpwrite __P((dev_t, struct uio *));
90 int hpsize __P((dev_t));
91
92 struct cfattach hp_ca = {
93 sizeof(struct hp_softc), hpmatch, hpattach
94 };
95
96 extern struct cfdriver hp_cd;
97
98 /*
99 * Check if this is a disk drive; done by checking type from mbaattach.
100 */
101 int
102 hpmatch(parent, cf, aux)
103 struct device *parent;
104 struct cfdata *cf;
105 void *aux;
106 {
107 struct mba_attach_args *ma = aux;
108
109 if (cf->cf_loc[MBACF_DRIVE] != MBACF_DRIVE_DEFAULT &&
110 cf->cf_loc[MBACF_DRIVE] != ma->unit)
111 return 0;
112
113 if (ma->devtyp != MB_RP)
114 return 0;
115
116 return 1;
117 }
118
119 /*
120 * Disk drive found; fake a disklabel and try to read the real one.
121 * If the on-disk label can't be read; we lose.
122 */
123 void
124 hpattach(parent, self, aux)
125 struct device *parent, *self;
126 void *aux;
127 {
128 struct hp_softc *sc = (void *)self;
129 struct mba_softc *ms = (void *)parent;
130 struct disklabel *dl;
131 struct mba_attach_args *ma = aux;
132 char *msg;
133
134 /*
135 * Init the common struct for both the adapter and its slaves.
136 */
137 BUFQ_INIT(&sc->sc_md.md_q);
138 sc->sc_md.md_softc = (void *)sc; /* Pointer to this softc */
139 sc->sc_md.md_mba = (void *)parent; /* Pointer to parent softc */
140 sc->sc_md.md_start = hpstart; /* Disk start routine */
141 sc->sc_md.md_attn = hpattn; /* Disk attention routine */
142 sc->sc_md.md_finish = hpfinish; /* Disk xfer finish routine */
143
144 ms->sc_md[ma->unit] = &sc->sc_md; /* Per-unit backpointer */
145
146 sc->sc_physnr = ma->unit;
147 /*
148 * Init and attach the disk structure.
149 */
150 sc->sc_disk.dk_name = sc->sc_dev.dv_xname;
151 disk_attach(&sc->sc_disk);
152
153 /*
154 * Fake a disklabel to be able to read in the real label.
155 */
156 dl = sc->sc_disk.dk_label;
157
158 dl->d_secsize = DEV_BSIZE;
159 dl->d_ntracks = 1;
160 dl->d_nsectors = 32;
161 dl->d_secpercyl = 32;
162
163 /*
164 * Read in label.
165 */
166 if ((msg = readdisklabel(makedev(0, self->dv_unit * 8), hpstrategy,
167 dl, NULL)) != NULL)
168 printf(": %s", msg);
169 printf(": %s, size = %d sectors\n", dl->d_typename, dl->d_secperunit);
170 /*
171 * check if this was what we booted from.
172 */
173 if ((B_TYPE(bootdev) == BDEV_HP) && (ma->unit == B_UNIT(bootdev)) &&
174 (ms->sc_physnr == B_ADAPTOR(bootdev)))
175 booted_from = self;
176 }
177
178
179 void
180 hpstrategy(bp)
181 struct buf *bp;
182 {
183 struct hp_softc *sc;
184 struct buf *gp;
185 int unit, s, err;
186 struct disklabel *lp;
187
188 unit = DISKUNIT(bp->b_dev);
189 sc = hp_cd.cd_devs[unit];
190 lp = sc->sc_disk.dk_label;
191
192 err = bounds_check_with_label(bp, lp, sc->sc_wlabel);
193 if (err < 0)
194 goto done;
195
196 bp->b_rawblkno =
197 bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
198 bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl;
199
200 s = splbio();
201
202 gp = BUFQ_FIRST(&sc->sc_md.md_q);
203 disksort_cylinder(&sc->sc_md.md_q, bp);
204 if (gp == 0)
205 mbaqueue(&sc->sc_md);
206
207 splx(s);
208 return;
209
210 done:
211 bp->b_resid = bp->b_bcount;
212 biodone(bp);
213 }
214
215 /*
216 * Start transfer on given disk. Called from mbastart().
217 */
218 void
219 hpstart(md)
220 struct mba_device *md;
221 {
222 struct hp_softc *sc = md->md_softc;
223 struct mba_regs *mr = md->md_mba->sc_mbareg;
224 volatile struct hp_regs *hr;
225 struct disklabel *lp = sc->sc_disk.dk_label;
226 struct buf *bp = BUFQ_FIRST(&md->md_q);
227 unsigned bn, cn, sn, tn;
228 int part = DISKPART(bp->b_dev);
229
230 /*
231 * Collect statistics.
232 */
233 disk_busy(&sc->sc_disk);
234 sc->sc_disk.dk_seek++;
235
236 hr = (void *)&mr->mba_md[DISKUNIT(bp->b_dev)];
237
238 bn = bp->b_rawblkno;
239 if (bn) {
240 cn = bn / lp->d_secpercyl;
241 sn = bn % lp->d_secpercyl;
242 tn = sn / lp->d_nsectors;
243 sn = sn % lp->d_nsectors;
244 } else
245 cn = sn = tn = 0;
246
247 hr->hp_dc = cn;
248 hr->hp_da = (tn << 8) | sn;
249 if (bp->b_flags & B_READ)
250 hr->hp_cs1 = HPCS_READ; /* GO */
251 else
252 hr->hp_cs1 = HPCS_WRITE;
253 }
254
255 int
256 hpopen(dev, flag, fmt)
257 dev_t dev;
258 int flag, fmt;
259 {
260 struct hp_softc *sc;
261 int unit, part;
262
263 unit = DISKUNIT(dev);
264 if (unit >= hp_cd.cd_ndevs)
265 return ENXIO;
266 sc = hp_cd.cd_devs[unit];
267 if (sc == 0)
268 return ENXIO;
269
270 part = DISKPART(dev);
271
272 if (part >= sc->sc_disk.dk_label->d_npartitions)
273 return ENXIO;
274
275 switch (fmt) {
276 case S_IFCHR:
277 sc->sc_disk.dk_copenmask |= (1 << part);
278 break;
279
280 case S_IFBLK:
281 sc->sc_disk.dk_bopenmask |= (1 << part);
282 break;
283 }
284 sc->sc_disk.dk_openmask =
285 sc->sc_disk.dk_copenmask | sc->sc_disk.dk_bopenmask;
286
287 return 0;
288 }
289
290 int
291 hpclose(dev, flag, fmt)
292 dev_t dev;
293 int flag, fmt;
294 {
295 struct hp_softc *sc;
296 int unit, part;
297
298 unit = DISKUNIT(dev);
299 sc = hp_cd.cd_devs[unit];
300
301 part = DISKPART(dev);
302
303 switch (fmt) {
304 case S_IFCHR:
305 sc->sc_disk.dk_copenmask &= ~(1 << part);
306 break;
307
308 case S_IFBLK:
309 sc->sc_disk.dk_bopenmask &= ~(1 << part);
310 break;
311 }
312 sc->sc_disk.dk_openmask =
313 sc->sc_disk.dk_copenmask | sc->sc_disk.dk_bopenmask;
314
315 return 0;
316 }
317
318 int
319 hpioctl(dev, cmd, addr, flag, p)
320 dev_t dev;
321 u_long cmd;
322 caddr_t addr;
323 int flag;
324 struct proc *p;
325 {
326 struct hp_softc *sc = hp_cd.cd_devs[DISKUNIT(dev)];
327 struct disklabel *lp = sc->sc_disk.dk_label;
328 int error;
329
330 switch (cmd) {
331 case DIOCGDINFO:
332 bcopy(lp, addr, sizeof (struct disklabel));
333 return 0;
334
335 case DIOCGPART:
336 ((struct partinfo *)addr)->disklab = lp;
337 ((struct partinfo *)addr)->part =
338 &lp->d_partitions[DISKPART(dev)];
339 break;
340
341 case DIOCSDINFO:
342 if ((flag & FWRITE) == 0)
343 return EBADF;
344
345 return setdisklabel(lp, (struct disklabel *)addr, 0, 0);
346
347 case DIOCWDINFO:
348 if ((flag & FWRITE) == 0)
349 error = EBADF;
350 else {
351 sc->sc_wlabel = 1;
352 error = writedisklabel(dev, hpstrategy, lp, 0);
353 sc->sc_wlabel = 0;
354 }
355 return error;
356 case DIOCWLABEL:
357 if ((flag & FWRITE) == 0)
358 return EBADF;
359 sc->sc_wlabel = 1;
360 break;
361
362 default:
363 printf("hpioctl: command %x\n", (unsigned int)cmd);
364 return ENOTTY;
365 }
366 return 0;
367 }
368
369 /*
370 * Called when a transfer is finished. Check if transfer went OK,
371 * Return info about what-to-do-now.
372 */
373 enum xfer_action
374 hpfinish(md, mbasr, attn)
375 struct mba_device *md;
376 int mbasr, *attn;
377 {
378 struct hp_softc *sc = md->md_softc;
379 struct buf *bp = BUFQ_FIRST(&md->md_q);
380 volatile struct mba_regs *mr = md->md_mba->sc_mbareg;
381 volatile struct hp_regs *hr = (void *)&mr->mba_md[DISKUNIT(bp->b_dev)];
382 int er1, er2;
383 volatile int bc; /* to get GCC read whole longword */
384 unsigned byte;
385
386 er1 = hr->hp_er1 & HPMASK;
387 er2 = hr->hp_er2 & HPMASK;
388 hr->hp_er1 = hr->hp_er2 = 0;
389 hper1:
390 switch (ffs(er1) - 1) {
391 case -1:
392 hr->hp_er1 = 0;
393 goto hper2;
394
395 case HPER1_DCK: /* Corrected? data read. Just notice. */
396 bc = mr->mba_bc;
397 byte = ~(bc >> 16);
398 diskerr(buf, hp_cd.cd_name, "soft ecc", LOG_PRINTF,
399 btodb(bp->b_bcount - byte), sc->sc_disk.dk_label);
400 er1 &= ~(1<<HPER1_DCK);
401 er1 &= HPMASK;
402 break;
403
404 default:
405 printf("drive error :%s er1 %x er2 %x\n",
406 sc->sc_dev.dv_xname, er1, er2);
407 hr->hp_er1 = hr->hp_er2 = 0;
408 goto hper2;
409 }
410 goto hper1;
411
412 hper2:
413 mbasr &= ~(MBASR_DTBUSY|MBASR_DTCMP|MBASR_ATTN);
414 if (mbasr)
415 printf("massbuss error :%s %x\n",
416 sc->sc_dev.dv_xname, mbasr);
417
418 BUFQ_FIRST(&md->md_q)->b_resid = 0;
419 disk_unbusy(&sc->sc_disk, BUFQ_FIRST(&md->md_q)->b_bcount);
420 return XFER_FINISH;
421 }
422
423 /*
424 * Non-data transfer interrupt; like volume change.
425 */
426 int
427 hpattn(md)
428 struct mba_device *md;
429 {
430 struct hp_softc *sc = md->md_softc;
431 struct mba_softc *ms = (void *)sc->sc_dev.dv_parent;
432 struct mba_regs *mr = ms->sc_mbareg;
433 struct hp_regs *hr = (void *)&mr->mba_md[sc->sc_dev.dv_unit];
434 int er1, er2;
435
436 er1 = hr->hp_er1 & HPMASK;
437 er2 = hr->hp_er2 & HPMASK;
438
439 printf("%s: Attention! er1 %x er2 %x\n",
440 sc->sc_dev.dv_xname, er1, er2);
441 return 0;
442 }
443
444
445 int
446 hpsize(dev)
447 dev_t dev;
448 {
449 int size, unit = DISKUNIT(dev);
450 struct hp_softc *sc;
451
452 if (unit >= hp_cd.cd_ndevs || hp_cd.cd_devs[unit] == 0)
453 return -1;
454
455 sc = hp_cd.cd_devs[unit];
456 size = sc->sc_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
457 (sc->sc_disk.dk_label->d_secsize / DEV_BSIZE);
458
459 return size;
460 }
461
462 int
463 hpdump(dev, a1, a2, size)
464 dev_t dev;
465 caddr_t a1, a2;
466 size_t size;
467 {
468 printf("hpdump: Not implemented yet.\n");
469 return 0;
470 }
471
472 int
473 hpread(dev, uio)
474 dev_t dev;
475 struct uio *uio;
476 {
477 return (physio(hpstrategy, NULL, dev, B_READ, minphys, uio));
478 }
479
480 int
481 hpwrite(dev, uio)
482 dev_t dev;
483 struct uio *uio;
484 {
485 return (physio(hpstrategy, NULL, dev, B_WRITE, minphys, uio));
486 }
487