hp.c revision 1.5 1 /* $NetBSD: hp.c,v 1.5 1996/02/23 17:29:01 ragge 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/device.h>
45 #include <sys/disklabel.h>
46 #include <sys/disk.h>
47 #include <sys/dkio.h>
48 #include <sys/buf.h>
49 #include <sys/stat.h>
50 #include <sys/ioccom.h>
51 #include <sys/fcntl.h>
52 #include <sys/syslog.h>
53
54 #include <machine/trap.h>
55 #include <machine/pte.h>
56 #include <machine/mtpr.h>
57
58 #include <vax/mba/mbavar.h>
59 #include <vax/mba/mbareg.h>
60 #include <vax/mba/hpreg.h>
61
62 #define HPMASK 0xffff
63
64 struct hp_softc {
65 struct device sc_dev;
66 struct disk sc_disk;
67 struct mba_device sc_md; /* Common struct used by mbaqueue. */
68 int sc_wlabel;
69 };
70
71 int hpmatch __P((struct device *, void *, void *));
72 void hpattach __P((struct device *, struct device *, void *));
73 void hpstrategy __P((struct buf *));
74 void hpstart __P((struct mba_device *));
75 int hpattn __P((struct mba_device *));
76 enum xfer_action hpfinish __P((struct mba_device *, int, int *));
77 int hpopen __P((dev_t, int, int));
78 int hpclose __P((dev_t, int, int));
79 int hpioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
80 int hpdump __P((dev_t, caddr_t, caddr_t, size_t));
81 int hpread __P((dev_t, struct uio *));
82 int hpwrite __P((dev_t, struct uio *));
83 int hpsize __P((dev_t));
84
85 struct cfdriver hpcd = {
86 NULL, "hp", hpmatch, hpattach, DV_DISK, sizeof(struct hp_softc)
87 };
88
89 /*
90 * Check if this is a disk drive; done by checking type from mbaattach.
91 */
92 int
93 hpmatch(parent, match, aux)
94 struct device *parent;
95 void *match, *aux;
96 {
97 struct cfdata *cf = match;
98 struct mba_attach_args *ma = aux;
99
100 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != ma->unit)
101 return 0;
102
103 if (ma->devtyp != MB_RP)
104 return 0;
105
106 return 1;
107 }
108
109 /*
110 * Disk drive found; fake a disklabel and try to read the real one.
111 * If the on-disk label can't be read; we lose.
112 */
113 void
114 hpattach(parent, self, aux)
115 struct device *parent, *self;
116 void *aux;
117 {
118 struct hp_softc *sc = (void *)self;
119 struct mba_softc *ms = (void *)parent;
120 struct disklabel *dl;
121 struct mba_attach_args *ma = aux;
122 char *msg;
123
124 /*
125 * Init the common struct for both the adapter and its slaves.
126 */
127 sc->sc_md.md_softc = (void *)sc; /* Pointer to this softc */
128 sc->sc_md.md_mba = (void *)parent; /* Pointer to parent softc */
129 sc->sc_md.md_start = hpstart; /* Disk start routine */
130 sc->sc_md.md_attn = hpattn; /* Disk attention routine */
131 sc->sc_md.md_finish = hpfinish; /* Disk xfer finish routine */
132
133 ms->sc_md[ma->unit] = &sc->sc_md; /* Per-unit backpointer */
134
135 /*
136 * Init and attach the disk structure.
137 */
138 sc->sc_disk.dk_name = sc->sc_dev.dv_xname;
139 disk_attach(&sc->sc_disk);
140
141 /*
142 * Fake a disklabel to be able to read in the real label.
143 */
144 dl = sc->sc_disk.dk_label;
145
146 dl->d_secsize = DEV_BSIZE;
147 dl->d_ntracks = 1;
148 dl->d_nsectors = 32;
149 dl->d_secpercyl = 32;
150
151 /*
152 * Read in label.
153 */
154 if ((msg = readdisklabel(makedev(0, self->dv_unit * 8), hpstrategy,
155 dl, NULL)) != NULL)
156 printf(": %s", msg);
157 printf(": %s, size = %d sectors\n", dl->d_typename, dl->d_secperunit);
158 }
159
160
161 void
162 hpstrategy(bp)
163 struct buf *bp;
164 {
165 struct hp_softc *sc;
166 struct buf *gp;
167 int unit, s;
168
169 unit = DISKUNIT(bp->b_dev);
170 sc = hpcd.cd_devs[unit];
171
172 if (bounds_check_with_label(bp, sc->sc_disk.dk_label, sc->sc_wlabel)
173 <= 0)
174 goto done;
175 s = splbio();
176
177 gp = sc->sc_md.md_q.b_actf;
178 disksort(&sc->sc_md.md_q, bp);
179 if (gp == 0)
180 mbaqueue(&sc->sc_md);
181
182 splx(s);
183 return;
184
185 done:
186 bp->b_resid = bp->b_bcount;
187 err:
188 biodone(bp);
189 }
190
191 /*
192 * Start transfer on given disk. Called from mbastart().
193 */
194 void
195 hpstart(md)
196 struct mba_device *md;
197 {
198 struct hp_softc *sc = md->md_softc;
199 struct mba_regs *mr = md->md_mba->sc_mbareg;
200 volatile struct hp_regs *hr;
201 struct disklabel *lp = sc->sc_disk.dk_label;
202 struct buf *bp = md->md_q.b_actf;
203 unsigned bn, cn, sn, tn;
204 int part = DISKPART(bp->b_dev);
205
206 /*
207 * Collect statistics.
208 */
209 disk_busy(&sc->sc_disk);
210 sc->sc_disk.dk_seek++;
211
212 hr = (void *)&mr->mba_md[DISKUNIT(bp->b_dev)];
213
214 bn = bp->b_blkno + lp->d_partitions[part].p_offset;
215 if (bn) {
216 cn = bn / lp->d_secpercyl;
217 sn = bn % lp->d_secpercyl;
218 tn = sn / lp->d_nsectors;
219 sn = sn % lp->d_nsectors;
220 } else
221 cn = sn = tn = 0;
222
223 hr->hp_dc = cn;
224 hr->hp_da = (tn << 8) | sn;
225 if (bp->b_flags & B_READ)
226 hr->hp_cs1 = HPCS_READ; /* GO */
227 else
228 hr->hp_cs1 = HPCS_WRITE;
229 }
230
231 int
232 hpopen(dev, flag, fmt)
233 dev_t dev;
234 int flag, fmt;
235 {
236 struct hp_softc *sc;
237 int unit, part;
238
239 unit = DISKUNIT(dev);
240 if (unit >= hpcd.cd_ndevs)
241 return ENXIO;
242 sc = hpcd.cd_devs[unit];
243 if (sc == 0)
244 return ENXIO;
245
246 part = DISKPART(dev);
247
248 if (part >= sc->sc_disk.dk_label->d_npartitions)
249 return ENXIO;
250
251 switch (fmt) {
252 case S_IFCHR:
253 sc->sc_disk.dk_copenmask |= (1 << part);
254 break;
255
256 case S_IFBLK:
257 sc->sc_disk.dk_bopenmask |= (1 << part);
258 break;
259 }
260 sc->sc_disk.dk_openmask =
261 sc->sc_disk.dk_copenmask | sc->sc_disk.dk_bopenmask;
262
263 return 0;
264 }
265
266 int
267 hpclose(dev, flag, fmt)
268 dev_t dev;
269 int flag, fmt;
270 {
271 struct hp_softc *sc;
272 int unit, part;
273
274 unit = DISKUNIT(dev);
275 sc = hpcd.cd_devs[unit];
276
277 part = DISKPART(dev);
278
279 switch (fmt) {
280 case S_IFCHR:
281 sc->sc_disk.dk_copenmask &= ~(1 << part);
282 break;
283
284 case S_IFBLK:
285 sc->sc_disk.dk_bopenmask &= ~(1 << part);
286 break;
287 }
288 sc->sc_disk.dk_openmask =
289 sc->sc_disk.dk_copenmask | sc->sc_disk.dk_bopenmask;
290
291 return 0;
292 }
293
294 int
295 hpioctl(dev, cmd, addr, flag, p)
296 dev_t dev;
297 u_long cmd;
298 caddr_t addr;
299 int flag;
300 struct proc *p;
301 {
302 struct hp_softc *sc = hpcd.cd_devs[DISKUNIT(dev)];
303 struct disklabel *lp = sc->sc_disk.dk_label;
304 int error;
305
306 switch (cmd) {
307 case DIOCGDINFO:
308 bcopy(lp, addr, sizeof (struct disklabel));
309 return 0;
310
311 case DIOCGPART:
312 ((struct partinfo *)addr)->disklab = lp;
313 ((struct partinfo *)addr)->part =
314 &lp->d_partitions[DISKPART(dev)];
315 break;
316
317 case DIOCSDINFO:
318 if ((flag & FWRITE) == 0)
319 return EBADF;
320
321 return setdisklabel(lp, (struct disklabel *)addr, 0, 0);
322
323 case DIOCWDINFO:
324 if ((flag & FWRITE) == 0)
325 error = EBADF;
326 else {
327 sc->sc_wlabel = 1;
328 error = writedisklabel(dev, hpstrategy, lp, 0);
329 sc->sc_wlabel = 0;
330 }
331 return error;
332
333 default:
334 printf("hpioctl: command %d\n", cmd);
335 return ENOTTY;
336 }
337 }
338
339 /*
340 * Called when a transfer is finished. Check if transfer went OK,
341 * Return info about what-to-do-now.
342 */
343 enum xfer_action
344 hpfinish(md, mbasr, attn)
345 struct mba_device *md;
346 int mbasr, *attn;
347 {
348 struct hp_softc *sc = md->md_softc;
349 struct buf *bp = md->md_q.b_actf;
350 volatile struct mba_regs *mr = md->md_mba->sc_mbareg;
351 volatile struct hp_regs *hr = (void *)&mr->mba_md[DISKUNIT(bp->b_dev)];
352 int er1, er2;
353 volatile int bc; /* to get GCC read whole longword */
354 unsigned byte;
355
356 er1 = hr->hp_er1 & HPMASK;
357 er2 = hr->hp_er2 & HPMASK;
358 hr->hp_er1 = hr->hp_er2 = 0;
359 hper1:
360 switch (ffs(er1) - 1) {
361 case -1:
362 hr->hp_er1 = 0;
363 goto hper2;
364
365 case HPER1_DCK: /* Corrected? data read. Just notice. */
366 bc = mr->mba_bc;
367 byte = ~(bc >> 16);
368 diskerr(buf, hpcd.cd_name, "soft ecc", LOG_PRINTF,
369 btodb(bp->b_bcount - byte), sc->sc_disk.dk_label);
370 er1 &= ~(1<<HPER1_DCK);
371 er1 &= HPMASK;
372 break;
373
374 default:
375 printf("drive error :%s er1 %x er2 %x\n",
376 sc->sc_dev.dv_xname, er1, er2);
377 hr->hp_er1 = hr->hp_er2 = 0;
378 goto hper2;
379 }
380 goto hper1;
381
382 hper2:
383 mbasr &= ~(MBASR_DTBUSY|MBASR_DTCMP|MBASR_ATTN);
384 if (mbasr)
385 printf("massbuss error :%s %x\n",
386 sc->sc_dev.dv_xname, mbasr);
387
388 md->md_q.b_actf->b_resid = 0;
389 disk_unbusy(&sc->sc_disk, md->md_q.b_actf->b_bcount);
390 return XFER_FINISH;
391 }
392
393 /*
394 * Non-data transfer interrupt; like volume change.
395 */
396 int
397 hpattn(md)
398 struct mba_device *md;
399 {
400 struct hp_softc *sc = md->md_softc;
401 struct mba_softc *ms = (void *)sc->sc_dev.dv_parent;
402 struct mba_regs *mr = ms->sc_mbareg;
403 struct hp_regs *hr = (void *)&mr->mba_md[sc->sc_dev.dv_unit];
404 int er1, er2;
405
406 er1 = hr->hp_er1 & HPMASK;
407 er2 = hr->hp_er2 & HPMASK;
408
409 printf("%s: Attention! er1 %x er2 %x\n",
410 sc->sc_dev.dv_xname, er1, er2);
411 return 0;
412 }
413
414
415 int
416 hpsize(dev)
417 dev_t dev;
418 {
419 int size, unit = DISKUNIT(dev);
420 struct hp_softc *sc;
421
422 if (unit >= hpcd.cd_ndevs || hpcd.cd_devs[unit] == 0)
423 return -1;
424
425 sc = hpcd.cd_devs[unit];
426 size = sc->sc_disk.dk_label->d_partitions[DISKPART(dev)].p_size;
427
428 return size;
429 }
430
431 int
432 hpdump(dev, a1, a2, size)
433 dev_t dev;
434 caddr_t a1, a2;
435 size_t size;
436 {
437 printf("hpdump: Not implemented yet.\n");
438 }
439
440 int
441 hpread(dev, uio)
442 dev_t dev;
443 struct uio *uio;
444 {
445 return (physio(hpstrategy, NULL, dev, B_READ, minphys, uio));
446 }
447
448 int
449 hpwrite(dev, uio)
450 dev_t dev;
451 struct uio *uio;
452 {
453 return (physio(hpstrategy, NULL, dev, B_WRITE, minphys, uio));
454 }
455
456