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