xref: /netbsd-src/sys/dev/mscp/mscp_disk.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: mscp_disk.c,v 1.44 2004/10/31 12:52:55 he Exp $	*/
2 /*
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Chris Torek.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)uda.c	7.32 (Berkeley) 2/13/91
34  */
35 
36 /*
37  * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Chris Torek.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *	This product includes software developed by the University of
53  *	California, Berkeley and its contributors.
54  * 4. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	@(#)uda.c	7.32 (Berkeley) 2/13/91
71  */
72 
73 /*
74  * RA disk device driver
75  * RX MSCP floppy disk device driver
76  */
77 
78 /*
79  * TODO
80  *	write bad block forwarding code
81  */
82 
83 #include <sys/cdefs.h>
84 __KERNEL_RCSID(0, "$NetBSD: mscp_disk.c,v 1.44 2004/10/31 12:52:55 he Exp $");
85 
86 #include <sys/param.h>
87 #include <sys/buf.h>
88 #include <sys/bufq.h>
89 #include <sys/device.h>
90 #include <sys/disk.h>
91 #include <sys/disklabel.h>
92 #include <sys/ioctl.h>
93 #include <sys/stat.h>
94 #include <sys/fcntl.h>
95 #include <sys/reboot.h>
96 #include <sys/proc.h>
97 #include <sys/systm.h>
98 #include <sys/conf.h>
99 
100 #include <ufs/ufs/dinode.h>
101 #include <ufs/ffs/fs.h>
102 
103 #include <machine/bus.h>
104 #include <machine/cpu.h>
105 
106 #include <dev/mscp/mscp.h>
107 #include <dev/mscp/mscpreg.h>
108 #include <dev/mscp/mscpvar.h>
109 
110 #include "locators.h"
111 #include "ioconf.h"
112 #include "ra.h"
113 
114 /*
115  * Drive status, per drive
116  */
117 struct ra_softc {
118 	struct	device ra_dev;	/* Autoconf struct */
119 	struct	disk ra_disk;
120 	int	ra_state;	/* open/closed state */
121 	u_long	ra_mediaid;	/* media id */
122 	int	ra_hwunit;	/* Hardware unit number */
123 	int	ra_havelabel;	/* true if we have a label */
124 	int	ra_wlabel;	/* label sector is currently writable */
125 };
126 
127 #define rx_softc ra_softc
128 
129 void	rxattach __P((struct device *, struct device *, void *));
130 int	rx_putonline __P((struct rx_softc *));
131 void	rrmakelabel __P((struct disklabel *, long));
132 
133 #if NRA
134 
135 int	ramatch __P((struct device *, struct cfdata *, void *));
136 void	raattach __P((struct device *, struct device *, void *));
137 int	ra_putonline __P((struct ra_softc *));
138 
139 CFATTACH_DECL(ra, sizeof(struct ra_softc),
140     ramatch, rxattach, NULL, NULL);
141 
142 dev_type_open(raopen);
143 dev_type_close(raclose);
144 dev_type_read(raread);
145 dev_type_write(rawrite);
146 dev_type_ioctl(raioctl);
147 dev_type_strategy(rastrategy);
148 dev_type_dump(radump);
149 dev_type_size(rasize);
150 
151 const struct bdevsw ra_bdevsw = {
152 	raopen, raclose, rastrategy, raioctl, radump, rasize, D_DISK
153 };
154 
155 const struct cdevsw ra_cdevsw = {
156 	raopen, raclose, raread, rawrite, raioctl,
157 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
158 };
159 
160 static struct dkdriver radkdriver = {
161 	rastrategy, minphys
162 };
163 
164 /*
165  * More driver definitions, for generic MSCP code.
166  */
167 
168 int
169 ramatch(parent, cf, aux)
170 	struct	device *parent;
171 	struct	cfdata *cf;
172 	void	*aux;
173 {
174 	struct	drive_attach_args *da = aux;
175 	struct	mscp *mp = da->da_mp;
176 
177 	if ((da->da_typ & MSCPBUS_DISK) == 0)
178 		return 0;
179 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
180 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
181 		return 0;
182 	/*
183 	 * Check if this disk is a floppy; then don't configure it.
184 	 * Seems to be a safe way to test it per Chris Torek.
185 	 */
186 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
187 		return 0;
188 	return 1;
189 }
190 
191 /*
192  * (Try to) put the drive online. This is done the first time the
193  * drive is opened, or if it har fallen offline.
194  */
195 int
196 ra_putonline(ra)
197 	struct ra_softc *ra;
198 {
199 	struct	disklabel *dl;
200 	const char *msg;
201 	int maj;
202 
203 	if (rx_putonline(ra) != MSCP_DONE)
204 		return MSCP_FAILED;
205 
206 	dl = ra->ra_disk.dk_label;
207 
208 	ra->ra_state = DK_RDLABEL;
209 	printf("%s", ra->ra_dev.dv_xname);
210 	maj = cdevsw_lookup_major(&ra_cdevsw);
211 	if ((msg = readdisklabel(MAKEDISKDEV(maj, ra->ra_dev.dv_unit,
212 	    RAW_PART), rastrategy, dl, NULL)) != NULL)
213 		printf(": %s", msg);
214 	else {
215 		ra->ra_havelabel = 1;
216 		ra->ra_state = DK_OPEN;
217 	}
218 
219 	printf(": size %d sectors\n", dl->d_secperunit);
220 
221 	return MSCP_DONE;
222 }
223 
224 /*
225  * Open a drive.
226  */
227 /*ARGSUSED*/
228 int
229 raopen(dev, flag, fmt, p)
230 	dev_t dev;
231 	int flag, fmt;
232 	struct	proc *p;
233 {
234 	struct ra_softc *ra;
235 	int error, part, unit, mask;
236 	/*
237 	 * Make sure this is a reasonable open request.
238 	 */
239 	unit = DISKUNIT(dev);
240 	if (unit >= ra_cd.cd_ndevs)
241 		return ENXIO;
242 	ra = ra_cd.cd_devs[unit];
243 	if (ra == 0)
244 		return ENXIO;
245 
246 	part = DISKPART(dev);
247 
248 	if ((error = lockmgr(&ra->ra_disk.dk_openlock, LK_EXCLUSIVE,
249 			     NULL)) != 0)
250 		return (error);
251 
252 	/*
253 	 * If there are wedges, and this is not RAW_PART, then we
254 	 * need to fail.
255 	 */
256 	if (ra->ra_disk.dk_nwedges != 0 && part != RAW_PART) {
257 		error = EBUSY;
258 		goto bad1;
259 	}
260 
261 	/*
262 	 * If this is the first open; we must first try to put
263 	 * the disk online (and read the label).
264 	 */
265 	if (ra->ra_state == DK_CLOSED) {
266 		if (ra_putonline(ra) == MSCP_FAILED) {
267 			error = ENXIO;
268 			goto bad1;
269 		}
270 	}
271 
272 	/* If the disk has no label; allow writing everywhere */
273 	if (ra->ra_havelabel == 0)
274 		ra->ra_wlabel = 1;
275 
276 	if (part >= ra->ra_disk.dk_label->d_npartitions) {
277 		error = ENXIO;
278 		goto bad1;
279 	}
280 
281 	/*
282 	 * Wait for the state to settle
283 	 */
284 #if notyet
285 	while (ra->ra_state != DK_OPEN)
286 		if ((error = tsleep((caddr_t)ra, (PZERO + 1) | PCATCH,
287 		    devopn, 0))) {
288 			splx(s);
289 			return (error);
290 		}
291 #endif
292 
293 	mask = 1 << part;
294 
295 	switch (fmt) {
296 	case S_IFCHR:
297 		ra->ra_disk.dk_copenmask |= mask;
298 		break;
299 	case S_IFBLK:
300 		ra->ra_disk.dk_bopenmask |= mask;
301 		break;
302 	}
303 	ra->ra_disk.dk_openmask |= mask;
304 	(void) lockmgr(&ra->ra_disk.dk_openlock, LK_RELEASE, NULL);
305 	return 0;
306 
307  bad1:
308 	(void) lockmgr(&ra->ra_disk.dk_openlock, LK_RELEASE, NULL);
309 	return (error);
310 }
311 
312 /* ARGSUSED */
313 int
314 raclose(dev, flags, fmt, p)
315 	dev_t dev;
316 	int flags, fmt;
317 	struct	proc *p;
318 {
319 	int unit = DISKUNIT(dev);
320 	struct ra_softc *ra = ra_cd.cd_devs[unit];
321 	int error, mask = (1 << DISKPART(dev));
322 
323 	if ((error = lockmgr(&ra->ra_disk.dk_openlock, LK_EXCLUSIVE,
324 			     NULL)) != 0)
325 		return (error);
326 
327 	switch (fmt) {
328 	case S_IFCHR:
329 		ra->ra_disk.dk_copenmask &= ~mask;
330 		break;
331 	case S_IFBLK:
332 		ra->ra_disk.dk_bopenmask &= ~mask;
333 		break;
334 	}
335 	ra->ra_disk.dk_openmask =
336 	    ra->ra_disk.dk_copenmask | ra->ra_disk.dk_bopenmask;
337 
338 	/*
339 	 * Should wait for I/O to complete on this partition even if
340 	 * others are open, but wait for work on blkflush().
341 	 */
342 #if notyet
343 	if (ra->ra_openpart == 0) {
344 		s = spluba();
345 		while (BUFQ_PEEK(&udautab[unit]) != NULL)
346 			(void) tsleep(&udautab[unit], PZERO - 1,
347 			    "raclose", 0);
348 		splx(s);
349 		ra->ra_state = CLOSED;
350 		ra->ra_wlabel = 0;
351 	}
352 #endif
353 	(void) lockmgr(&ra->ra_disk.dk_openlock, LK_RELEASE, NULL);
354 	return (0);
355 }
356 
357 /*
358  * Queue a transfer request, and if possible, hand it to the controller.
359  */
360 void
361 rastrategy(bp)
362 	struct buf *bp;
363 {
364 	int unit;
365 	struct ra_softc *ra;
366 	int b;
367 
368 	/*
369 	 * Make sure this is a reasonable drive to use.
370 	 */
371 	unit = DISKUNIT(bp->b_dev);
372 	if (unit > ra_cd.cd_ndevs || (ra = ra_cd.cd_devs[unit]) == NULL) {
373 		bp->b_error = ENXIO;
374 		bp->b_flags |= B_ERROR;
375 		goto done;
376 	}
377 	/*
378 	 * If drive is open `raw' or reading label, let it at it.
379 	 */
380 	if (ra->ra_state == DK_RDLABEL) {
381 	        /* Make some statistics... /bqt */
382 	        b = splbio();
383 	        disk_busy(&ra->ra_disk);
384 		splx(b);
385 		mscp_strategy(bp, ra->ra_dev.dv_parent);
386 		return;
387 	}
388 
389 	/* If disk is not online, try to put it online */
390 	if (ra->ra_state == DK_CLOSED)
391 		if (ra_putonline(ra) == MSCP_FAILED) {
392 			bp->b_flags |= B_ERROR;
393 			bp->b_error = EIO;
394 			goto done;
395 		}
396 
397 	/*
398 	 * Determine the size of the transfer, and make sure it is
399 	 * within the boundaries of the partition.
400 	 */
401 	if (bounds_check_with_label(&ra->ra_disk, bp, ra->ra_wlabel) <= 0)
402 		goto done;
403 
404 	/* Make some statistics... /bqt */
405 	b = splbio();
406 	disk_busy(&ra->ra_disk);
407 	splx(b);
408 	mscp_strategy(bp, ra->ra_dev.dv_parent);
409 	return;
410 
411 done:
412 	biodone(bp);
413 }
414 
415 int
416 raread(dev, uio, flags)
417 	dev_t dev;
418 	struct uio *uio;
419 	int flags;
420 {
421 
422 	return (physio(rastrategy, NULL, dev, B_READ, minphys, uio));
423 }
424 
425 int
426 rawrite(dev, uio, flags)
427 	dev_t dev;
428 	struct uio *uio;
429 	int flags;
430 {
431 
432 	return (physio(rastrategy, NULL, dev, B_WRITE, minphys, uio));
433 }
434 
435 /*
436  * I/O controls.
437  */
438 int
439 raioctl(dev, cmd, data, flag, p)
440 	dev_t dev;
441 	u_long cmd;
442 	caddr_t data;
443 	int flag;
444 	struct proc *p;
445 {
446 	int unit = DISKUNIT(dev);
447 	struct disklabel *lp, *tp;
448 	struct ra_softc *ra = ra_cd.cd_devs[unit];
449 	int error = 0;
450 #ifdef __HAVE_OLD_DISKLABEL
451 	struct disklabel newlabel;
452 #endif
453 
454 	lp = ra->ra_disk.dk_label;
455 
456 	switch (cmd) {
457 
458 	case DIOCGDINFO:
459 		bcopy(lp, data, sizeof (struct disklabel));
460 		break;
461 #ifdef __HAVE_OLD_DISKLABEL
462 	case ODIOCGDINFO:
463 		bcopy(lp, &newlabel, sizeof disklabel);
464 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
465 			return ENOTTY;
466 		bcopy(&newlabel, data, sizeof (struct olddisklabel));
467 		break;
468 #endif
469 
470 	case DIOCGPART:
471 		((struct partinfo *)data)->disklab = lp;
472 		((struct partinfo *)data)->part =
473 		    &lp->d_partitions[DISKPART(dev)];
474 		break;
475 
476 	case DIOCWDINFO:
477 	case DIOCSDINFO:
478 #ifdef __HAVE_OLD_DISKLABEL
479 	case ODIOCWDINFO:
480 	case ODIOCSDINFO:
481 		if (cmd == ODIOCSDINFO || xfer == ODIOCWDINFO) {
482 			memset(&newlabel, 0, sizeof newlabel);
483 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
484 			tp = &newlabel;
485 		} else
486 #endif
487 		tp = (struct disklabel *)data;
488 
489 		if ((flag & FWRITE) == 0)
490 			error = EBADF;
491 		else {
492 			if ((error = lockmgr(&ra->ra_disk.dk_openlock,
493 					     LK_EXCLUSIVE, NULL)) != 0)
494 				break;
495 			error = setdisklabel(lp, tp, 0, 0);
496 			if ((error == 0) && (cmd == DIOCWDINFO
497 #ifdef __HAVE_OLD_DISKLABEL
498 			    || cmd == ODIOCWDINFO
499 #else
500 			    )) {
501 #endif
502 				ra->ra_wlabel = 1;
503 				error = writedisklabel(dev, rastrategy, lp,0);
504 				ra->ra_wlabel = 0;
505 			}
506 			(void) lockmgr(&ra->ra_disk.dk_openlock,
507 				       LK_RELEASE, NULL);
508 		}
509 		break;
510 
511 	case DIOCWLABEL:
512 		if ((flag & FWRITE) == 0)
513 			error = EBADF;
514 		else
515 			ra->ra_wlabel = 1;
516 		break;
517 
518 	case DIOCGDEFLABEL:
519 #ifdef __HAVE_OLD_DISKLABEL
520 	case ODIOCGDEFLABEL:
521 		if (cmd == ODIOCGDEFLABEL)
522 			tp = &newlabel;
523 		else
524 #else
525 		tp = (struct disklabel *)data;
526 #endif
527 		bzero(tp, sizeof(struct disklabel));
528 		tp->d_secsize = lp->d_secsize;
529 		tp->d_nsectors = lp->d_nsectors;
530 		tp->d_ntracks = lp->d_ntracks;
531 		tp->d_ncylinders = lp->d_ncylinders;
532 		tp->d_secpercyl = lp->d_secpercyl;
533 		tp->d_secperunit = lp->d_secperunit;
534 		tp->d_type = DTYPE_MSCP;
535 		tp->d_rpm = 3600;
536 		rrmakelabel(tp, ra->ra_mediaid);
537 #ifdef __HAVE_OLD_DISKLABEL
538 		if (cmd == ODIOCGDEFLABEL) {
539 			if (tp->d_npartitions > OLDMAXPARTITIONS)
540 				return ENOTTY;
541 			memcpy(data, tp, sizeof (struct olddisklabel));
542 		}
543 #endif
544 		break;
545 
546 	case DIOCAWEDGE:
547 	    {
548 	    	struct dkwedge_info *dkw = (void *) data;
549 
550 		if ((flag & FWRITE) == 0)
551 			return (EBADF);
552 
553 		/* If the ioctl happens here, the parent is us. */
554 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
555 		return (dkwedge_add(dkw));
556 	    }
557 
558 	case DIOCDWEDGE:
559 	    {
560 	    	struct dkwedge_info *dkw = (void *) data;
561 
562 		if ((flag & FWRITE) == 0)
563 			return (EBADF);
564 
565 		/* If the ioctl happens here, the parent is us. */
566 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
567 		return (dkwedge_del(dkw));
568 	    }
569 
570 	case DIOCLWEDGES:
571 	    {
572 	    	struct dkwedge_list *dkwl = (void *) data;
573 
574 		return (dkwedge_list(&ra->ra_disk, dkwl, p));
575 	    }
576 
577 	default:
578 		error = ENOTTY;
579 		break;
580 	}
581 	return (error);
582 }
583 
584 
585 int
586 radump(dev, blkno, va, size)
587 	dev_t	dev;
588 	daddr_t blkno;
589 	caddr_t va;
590 	size_t	size;
591 {
592 	return ENXIO;
593 }
594 
595 /*
596  * Return the size of a partition, if known, or -1 if not.
597  */
598 int
599 rasize(dev)
600 	dev_t dev;
601 {
602 	int unit = DISKUNIT(dev);
603 	struct ra_softc *ra;
604 
605 	if (unit >= ra_cd.cd_ndevs || ra_cd.cd_devs[unit] == 0)
606 		return -1;
607 
608 	ra = ra_cd.cd_devs[unit];
609 
610 	if (ra->ra_state == DK_CLOSED)
611 		if (ra_putonline(ra) == MSCP_FAILED)
612 			return -1;
613 
614 	return ra->ra_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
615 	    (ra->ra_disk.dk_label->d_secsize / DEV_BSIZE);
616 }
617 
618 #endif /* NRA */
619 
620 #if NRX
621 
622 int	rxmatch __P((struct device *, struct cfdata *, void *));
623 
624 CFATTACH_DECL(rx, sizeof(struct rx_softc),
625     rxmatch, rxattach, NULL, NULL);
626 
627 dev_type_open(rxopen);
628 dev_type_read(rxread);
629 dev_type_write(rxwrite);
630 dev_type_ioctl(rxioctl);
631 dev_type_strategy(rxstrategy);
632 dev_type_dump(rxdump);
633 dev_type_size(rxsize);
634 
635 const struct bdevsw rx_bdevsw = {
636 	rxopen, nullclose, rxstrategy, rxioctl, rxdump, rxsize, D_DISK
637 };
638 
639 const struct cdevsw rx_cdevsw = {
640 	rxopen, nullclose, rxread, rxwrite, rxioctl,
641 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
642 };
643 
644 static struct dkdriver rxdkdriver = {
645 	rxstrategy, minphys
646 };
647 
648 /*
649  * More driver definitions, for generic MSCP code.
650  */
651 
652 int
653 rxmatch(parent, cf, aux)
654 	struct	device *parent;
655 	struct	cfdata *cf;
656 	void	*aux;
657 {
658 	struct	drive_attach_args *da = aux;
659 	struct	mscp *mp = da->da_mp;
660 
661 	if ((da->da_typ & MSCPBUS_DISK) == 0)
662 		return 0;
663 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
664 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
665 		return 0;
666 	/*
667 	 * Check if this disk is a floppy; then configure it.
668 	 * Seems to be a safe way to test it per Chris Torek.
669 	 */
670 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
671 		return 1;
672 	return 0;
673 }
674 
675 #endif /* NRX */
676 
677 /*
678  * The attach routine only checks and prints drive type.
679  * Bringing the disk online is done when the disk is accessed
680  * the first time.
681  */
682 void
683 rxattach(parent, self, aux)
684 	struct	device *parent, *self;
685 	void	*aux;
686 {
687 	struct	rx_softc *rx = (void *)self;
688 	struct	drive_attach_args *da = aux;
689 	struct	mscp *mp = da->da_mp;
690 	struct	mscp_softc *mi = (void *)parent;
691 	struct	disklabel *dl;
692 
693 	rx->ra_mediaid = mp->mscp_guse.guse_mediaid;
694 	rx->ra_state = DK_CLOSED;
695 	rx->ra_hwunit = mp->mscp_unit;
696 	mi->mi_dp[mp->mscp_unit] = self;
697 
698 	rx->ra_disk.dk_name = rx->ra_dev.dv_xname;
699 #if NRX
700 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
701 		rx->ra_disk.dk_driver = &rxdkdriver;
702 #endif
703 #if NRA
704 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@')
705 		rx->ra_disk.dk_driver = &radkdriver;
706 #endif
707 	disk_attach((struct disk *)&rx->ra_disk);
708 
709 	/* Fill in what we know. The actual size is gotten later */
710 	dl = rx->ra_disk.dk_label;
711 
712 	dl->d_secsize = DEV_BSIZE;
713 	dl->d_nsectors = mp->mscp_guse.guse_nspt;
714 	dl->d_ntracks = mp->mscp_guse.guse_ngpc * mp->mscp_guse.guse_group;
715 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
716 	disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
717 #ifdef DEBUG
718 	printf("%s: nspt %d group %d ngpc %d rct %d nrpt %d nrct %d\n",
719 	    self->dv_xname, mp->mscp_guse.guse_nspt, mp->mscp_guse.guse_group,
720 	    mp->mscp_guse.guse_ngpc, mp->mscp_guse.guse_rctsize,
721 	    mp->mscp_guse.guse_nrpt, mp->mscp_guse.guse_nrct);
722 #endif
723 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@') {
724 		/*
725 		 * XXX We should try to discover wedges here, but
726 		 * XXX that would mean being able to do I/O.  Should
727 		 * XXX use config_defer() here.
728 		 */
729 	}
730 }
731 
732 /*
733  * (Try to) put the drive online. This is done the first time the
734  * drive is opened, or if it har fallen offline.
735  */
736 int
737 rx_putonline(rx)
738 	struct rx_softc *rx;
739 {
740 	struct	mscp *mp;
741 	struct	mscp_softc *mi = (struct mscp_softc *)rx->ra_dev.dv_parent;
742 	volatile int i;
743 
744 	rx->ra_state = DK_CLOSED;
745 	mp = mscp_getcp(mi, MSCP_WAIT);
746 	mp->mscp_opcode = M_OP_ONLINE;
747 	mp->mscp_unit = rx->ra_hwunit;
748 	mp->mscp_cmdref = 1;
749 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
750 
751 	/* Poll away */
752 	i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
753 	if (tsleep(&rx->ra_dev.dv_unit, PRIBIO, "rxonline", 100*100))
754 		rx->ra_state = DK_CLOSED;
755 
756 	if (rx->ra_state == DK_CLOSED)
757 		return MSCP_FAILED;
758 
759 	return MSCP_DONE;
760 }
761 
762 #if NRX
763 
764 /*
765  * Open a drive.
766  */
767 /*ARGSUSED*/
768 int
769 rxopen(dev, flag, fmt, p)
770 	dev_t dev;
771 	int flag, fmt;
772 	struct	proc *p;
773 {
774 	struct rx_softc *rx;
775 	int unit;
776 
777 	/*
778 	 * Make sure this is a reasonable open request.
779 	 */
780 	unit = DISKUNIT(dev);
781 	if (unit >= rx_cd.cd_ndevs)
782 		return ENXIO;
783 	rx = rx_cd.cd_devs[unit];
784 	if (rx == 0)
785 		return ENXIO;
786 
787 	/*
788 	 * If this is the first open; we must first try to put
789 	 * the disk online (and read the label).
790 	 */
791 	if (rx->ra_state == DK_CLOSED)
792 		if (rx_putonline(rx) == MSCP_FAILED)
793 			return ENXIO;
794 
795 	return 0;
796 }
797 
798 /*
799  * Queue a transfer request, and if possible, hand it to the controller.
800  *
801  * This routine is broken into two so that the internal version
802  * udastrat1() can be called by the (nonexistent, as yet) bad block
803  * revectoring routine.
804  */
805 void
806 rxstrategy(bp)
807 	struct buf *bp;
808 {
809 	int unit;
810 	struct rx_softc *rx;
811 	int b;
812 
813 	/*
814 	 * Make sure this is a reasonable drive to use.
815 	 */
816 	unit = DISKUNIT(bp->b_dev);
817 	if (unit > rx_cd.cd_ndevs || (rx = rx_cd.cd_devs[unit]) == NULL) {
818 		bp->b_error = ENXIO;
819 		bp->b_flags |= B_ERROR;
820 		goto done;
821 	}
822 
823 	/* If disk is not online, try to put it online */
824 	if (rx->ra_state == DK_CLOSED)
825 		if (rx_putonline(rx) == MSCP_FAILED) {
826 			bp->b_flags |= B_ERROR;
827 			bp->b_error = EIO;
828 			goto done;
829 		}
830 
831 	/*
832 	 * Determine the size of the transfer, and make sure it is
833 	 * within the boundaries of the partition.
834 	 */
835 	if (bp->b_blkno >= rx->ra_disk.dk_label->d_secperunit) {
836 		bp->b_resid = bp->b_bcount;
837 		goto done;
838 	}
839 
840 	/* Make some statistics... /bqt */
841 	b = splbio();
842 	disk_busy(&rx->ra_disk);
843 	splx(b);
844 	mscp_strategy(bp, rx->ra_dev.dv_parent);
845 	return;
846 
847 done:
848 	biodone(bp);
849 }
850 
851 int
852 rxread(dev, uio, flag)
853 	dev_t dev;
854 	struct uio *uio;
855 	int flag;
856 {
857 
858 	return (physio(rxstrategy, NULL, dev, B_READ, minphys, uio));
859 }
860 
861 int
862 rxwrite(dev, uio, flag)
863 	dev_t dev;
864 	struct uio *uio;
865 	int flag;
866 {
867 
868 	return (physio(rxstrategy, NULL, dev, B_WRITE, minphys, uio));
869 }
870 
871 /*
872  * I/O controls.
873  */
874 int
875 rxioctl(dev, cmd, data, flag, p)
876 	dev_t dev;
877 	u_long cmd;
878 	caddr_t data;
879 	int flag;
880 	struct proc *p;
881 {
882 	int unit = DISKUNIT(dev);
883 	struct disklabel *lp;
884 	struct rx_softc *rx = rx_cd.cd_devs[unit];
885 	int error = 0;
886 
887 	lp = rx->ra_disk.dk_label;
888 
889 	switch (cmd) {
890 
891 	case DIOCGDINFO:
892 		bcopy(lp, data, sizeof (struct disklabel));
893 		break;
894 
895 	case DIOCGPART:
896 		((struct partinfo *)data)->disklab = lp;
897 		((struct partinfo *)data)->part =
898 		    &lp->d_partitions[DISKPART(dev)];
899 		break;
900 
901 
902 	case DIOCWDINFO:
903 	case DIOCSDINFO:
904 	case DIOCWLABEL:
905 		break;
906 
907 	default:
908 		error = ENOTTY;
909 		break;
910 	}
911 	return (error);
912 }
913 
914 int
915 rxdump(dev, blkno, va, size)
916 	dev_t dev;
917 	daddr_t blkno;
918 	caddr_t va;
919 	size_t size;
920 {
921 
922 	/* Not likely. */
923 	return ENXIO;
924 }
925 
926 int
927 rxsize(dev)
928 	dev_t dev;
929 {
930 
931 	return -1;
932 }
933 
934 #endif /* NRX */
935 
936 void	rrdgram __P((struct device *, struct mscp *, struct mscp_softc *));
937 void	rriodone __P((struct device *, struct buf *));
938 int	rronline __P((struct device *, struct mscp *));
939 int	rrgotstatus __P((struct device *, struct mscp *));
940 void	rrreplace __P((struct device *, struct mscp *));
941 int	rrioerror __P((struct device *, struct mscp *, struct buf *));
942 void	rrfillin __P((struct buf *, struct mscp *));
943 void	rrbb __P((struct device *, struct mscp *, struct buf *));
944 
945 
946 struct	mscp_device ra_device = {
947 	rrdgram,
948 	rriodone,
949 	rronline,
950 	rrgotstatus,
951 	rrreplace,
952 	rrioerror,
953 	rrbb,
954 	rrfillin,
955 };
956 
957 /*
958  * Handle an error datagram.
959  * This can come from an unconfigured drive as well.
960  */
961 void
962 rrdgram(usc, mp, mi)
963 	struct device *usc;
964 	struct mscp *mp;
965 	struct mscp_softc *mi;
966 {
967 	if (mscp_decodeerror(usc == NULL?"unconf disk" : usc->dv_xname, mp, mi))
968 		return;
969 	/*
970 	 * SDI status information bytes 10 and 11 are the microprocessor
971 	 * error code and front panel code respectively.  These vary per
972 	 * drive type and are printed purely for field service information.
973 	 */
974 	if (mp->mscp_format == M_FM_SDI)
975 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
976 			mp->mscp_erd.erd_sdistat[10],
977 			mp->mscp_erd.erd_sdistat[11]);
978 }
979 
980 void
981 rriodone(usc, bp)
982 	struct device *usc;
983 	struct buf *bp;
984 {
985 	struct ra_softc *ra;
986 	int unit;
987 
988 	/* We assume that this is a reasonable drive. ra_strategy should
989 	   already have verified it. Thus, no checks here... /bqt */
990 	unit = DISKUNIT(bp->b_dev);
991 	ra = ra_cd.cd_devs[unit];
992 	disk_unbusy(&ra->ra_disk, bp->b_bcount, (bp->b_flags & B_READ));
993 
994 	biodone(bp);
995 }
996 
997 /*
998  * A drive came on line.  Check its type and size.  Return DONE if
999  * we think the drive is truly on line.	 In any case, awaken anyone
1000  * sleeping on the drive on-line-ness.
1001  */
1002 int
1003 rronline(usc, mp)
1004 	struct device *usc;
1005 	struct mscp *mp;
1006 {
1007 	struct rx_softc *rx = (struct rx_softc *)usc;
1008 	struct disklabel *dl;
1009 
1010 	wakeup((caddr_t)&usc->dv_unit);
1011 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1012 		printf("%s: attempt to bring on line failed: ", usc->dv_xname);
1013 		mscp_printevent(mp);
1014 		return (MSCP_FAILED);
1015 	}
1016 
1017 	rx->ra_state = DK_OPEN;
1018 
1019 	dl = rx->ra_disk.dk_label;
1020 	dl->d_secperunit = (daddr_t)mp->mscp_onle.onle_unitsize;
1021 
1022 	if (dl->d_secpercyl) {
1023 		dl->d_ncylinders = dl->d_secperunit/dl->d_secpercyl;
1024 		dl->d_type = DTYPE_MSCP;
1025 		dl->d_rpm = 3600;
1026 	} else {
1027 		dl->d_type = DTYPE_FLOPPY;
1028 		dl->d_rpm = 300;
1029 	}
1030 	rrmakelabel(dl, rx->ra_mediaid);
1031 
1032 	return (MSCP_DONE);
1033 }
1034 
1035 void
1036 rrmakelabel(dl, type)
1037 	struct disklabel *dl;
1038 	long type;
1039 {
1040 	int n, p = 0;
1041 
1042 	dl->d_bbsize = BBSIZE;
1043 	dl->d_sbsize = SBLOCKSIZE;
1044 
1045 	/* Create the disk name for disklabel. Phew... */
1046 	dl->d_typename[p++] = MSCP_MID_CHAR(2, type);
1047 	dl->d_typename[p++] = MSCP_MID_CHAR(1, type);
1048 	if (MSCP_MID_ECH(0, type))
1049 		dl->d_typename[p++] = MSCP_MID_CHAR(0, type);
1050 	n = MSCP_MID_NUM(type);
1051 	if (n > 99) {
1052 		dl->d_typename[p++] = '1';
1053 		n -= 100;
1054 	}
1055 	if (n > 9) {
1056 		dl->d_typename[p++] = (n / 10) + '0';
1057 		n %= 10;
1058 	}
1059 	dl->d_typename[p++] = n + '0';
1060 	dl->d_typename[p] = 0;
1061 	dl->d_npartitions = MAXPARTITIONS;
1062 	dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
1063 	    dl->d_secperunit;
1064 	dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
1065 	dl->d_interleave = dl->d_headswitch = 1;
1066 	dl->d_magic = dl->d_magic2 = DISKMAGIC;
1067 	dl->d_checksum = dkcksum(dl);
1068 }
1069 
1070 /*
1071  * We got some (configured) unit's status.  Return DONE if it succeeded.
1072  */
1073 int
1074 rrgotstatus(usc, mp)
1075 	struct device *usc;
1076 	struct mscp *mp;
1077 {
1078 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1079 		printf("%s: attempt to get status failed: ", usc->dv_xname);
1080 		mscp_printevent(mp);
1081 		return (MSCP_FAILED);
1082 	}
1083 	/* record for (future) bad block forwarding and whatever else */
1084 #ifdef notyet
1085 	uda_rasave(ui->ui_unit, mp, 1);
1086 #endif
1087 	return (MSCP_DONE);
1088 }
1089 
1090 /*
1091  * A replace operation finished.
1092  */
1093 /*ARGSUSED*/
1094 void
1095 rrreplace(usc, mp)
1096 	struct device *usc;
1097 	struct mscp *mp;
1098 {
1099 
1100 	panic("udareplace");
1101 }
1102 
1103 /*
1104  * A transfer failed.  We get a chance to fix or restart it.
1105  * Need to write the bad block forwaring code first....
1106  */
1107 /*ARGSUSED*/
1108 int
1109 rrioerror(usc, mp, bp)
1110 	struct device *usc;
1111 	struct mscp *mp;
1112 	struct buf *bp;
1113 {
1114 	struct ra_softc *ra = (void *)usc;
1115 	int code = mp->mscp_event;
1116 
1117 	switch (code & M_ST_MASK) {
1118 	/* The unit has fallen offline. Try to figure out why. */
1119 	case M_ST_OFFLINE:
1120 		bp->b_flags |= B_ERROR;
1121 		bp->b_error = EIO;
1122 		ra->ra_state = DK_CLOSED;
1123 		if (code & M_OFFLINE_UNMOUNTED)
1124 			printf("%s: not mounted/spun down\n", usc->dv_xname);
1125 		if (code & M_OFFLINE_DUPLICATE)
1126 			printf("%s: duplicate unit number!!!\n", usc->dv_xname);
1127 		return MSCP_DONE;
1128 
1129 	case M_ST_AVAILABLE:
1130 		ra->ra_state = DK_CLOSED; /* Force another online */
1131 		return MSCP_DONE;
1132 
1133 	default:
1134 		printf("%s:", usc->dv_xname);
1135 		break;
1136 	}
1137 	return (MSCP_FAILED);
1138 }
1139 
1140 /*
1141  * Fill in disk addresses in a mscp packet waiting for transfer.
1142  */
1143 void
1144 rrfillin(bp, mp)
1145 	struct buf *bp;
1146 	struct mscp *mp;
1147 {
1148 	struct rx_softc *rx = 0; /* Wall */
1149 	struct disklabel *lp;
1150 	int unit = DISKUNIT(bp->b_dev);
1151 	int part = DISKPART(bp->b_dev);
1152 
1153 #if NRA
1154 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
1155 		rx = ra_cd.cd_devs[unit];
1156 #endif
1157 #if NRX
1158 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
1159 		rx = rx_cd.cd_devs[unit];
1160 #endif
1161 	lp = rx->ra_disk.dk_label;
1162 
1163 	mp->mscp_seq.seq_lbn = lp->d_partitions[part].p_offset + bp->b_blkno;
1164 	mp->mscp_unit = rx->ra_hwunit;
1165 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
1166 }
1167 
1168 /*
1169  * A bad block related operation finished.
1170  */
1171 /*ARGSUSED*/
1172 void
1173 rrbb(usc, mp, bp)
1174 	struct device *usc;
1175 	struct mscp *mp;
1176 	struct buf *bp;
1177 {
1178 
1179 	panic("udabb");
1180 }
1181