xref: /netbsd-src/sys/dev/mscp/mscp_disk.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: mscp_disk.c,v 1.58 2007/10/19 12:00:36 ad 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.58 2007/10/19 12:00:36 ad 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 <sys/bus.h>
104 #include <sys/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(struct device *, struct device *, void *);
130 int	rx_putonline(struct rx_softc *);
131 void	rrmakelabel(struct disklabel *, long);
132 
133 #if NRA
134 
135 int	ramatch(struct device *, struct cfdata *, void *);
136 void	raattach(struct device *, struct device *, void *);
137 int	ra_putonline(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, device_unit(&ra->ra_dev),
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, l)
230 	dev_t dev;
231 	int flag, fmt;
232 	struct	lwp *l;
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 	mutex_enter(&ra->ra_disk.dk_openlock);
249 
250 	/*
251 	 * If there are wedges, and this is not RAW_PART, then we
252 	 * need to fail.
253 	 */
254 	if (ra->ra_disk.dk_nwedges != 0 && part != RAW_PART) {
255 		error = EBUSY;
256 		goto bad1;
257 	}
258 
259 	/*
260 	 * If this is the first open; we must first try to put
261 	 * the disk online (and read the label).
262 	 */
263 	if (ra->ra_state == DK_CLOSED) {
264 		if (ra_putonline(ra) == MSCP_FAILED) {
265 			error = ENXIO;
266 			goto bad1;
267 		}
268 	}
269 
270 	/* If the disk has no label; allow writing everywhere */
271 	if (ra->ra_havelabel == 0)
272 		ra->ra_wlabel = 1;
273 
274 	if (part >= ra->ra_disk.dk_label->d_npartitions) {
275 		error = ENXIO;
276 		goto bad1;
277 	}
278 
279 	/*
280 	 * Wait for the state to settle
281 	 */
282 #if notyet
283 	while (ra->ra_state != DK_OPEN)
284 		if ((error = tsleep((void *)ra, (PZERO + 1) | PCATCH,
285 		    devopn, 0))) {
286 			splx(s);
287 			return (error);
288 		}
289 #endif
290 
291 	mask = 1 << part;
292 
293 	switch (fmt) {
294 	case S_IFCHR:
295 		ra->ra_disk.dk_copenmask |= mask;
296 		break;
297 	case S_IFBLK:
298 		ra->ra_disk.dk_bopenmask |= mask;
299 		break;
300 	}
301 	ra->ra_disk.dk_openmask |= mask;
302 	error = 0;
303  bad1:
304 	mutex_exit(&ra->ra_disk.dk_openlock);
305 	return (error);
306 }
307 
308 /* ARGSUSED */
309 int
310 raclose(dev, flags, fmt, l)
311 	dev_t dev;
312 	int flags, fmt;
313 	struct	lwp *l;
314 {
315 	int unit = DISKUNIT(dev);
316 	struct ra_softc *ra = ra_cd.cd_devs[unit];
317 	int mask = (1 << DISKPART(dev));
318 
319 	mutex_enter(&ra->ra_disk.dk_openlock);
320 
321 	switch (fmt) {
322 	case S_IFCHR:
323 		ra->ra_disk.dk_copenmask &= ~mask;
324 		break;
325 	case S_IFBLK:
326 		ra->ra_disk.dk_bopenmask &= ~mask;
327 		break;
328 	}
329 	ra->ra_disk.dk_openmask =
330 	    ra->ra_disk.dk_copenmask | ra->ra_disk.dk_bopenmask;
331 
332 	/*
333 	 * Should wait for I/O to complete on this partition even if
334 	 * others are open, but wait for work on blkflush().
335 	 */
336 #if notyet
337 	if (ra->ra_openpart == 0) {
338 		s = spluba();
339 		while (BUFQ_PEEK(udautab[unit]) != NULL)
340 			(void) tsleep(&udautab[unit], PZERO - 1,
341 			    "raclose", 0);
342 		splx(s);
343 		ra->ra_state = CLOSED;
344 		ra->ra_wlabel = 0;
345 	}
346 #endif
347 	mutex_exit(&ra->ra_disk.dk_openlock);
348 	return (0);
349 }
350 
351 /*
352  * Queue a transfer request, and if possible, hand it to the controller.
353  */
354 void
355 rastrategy(bp)
356 	struct buf *bp;
357 {
358 	int unit;
359 	struct ra_softc *ra;
360 	int b;
361 
362 	/*
363 	 * Make sure this is a reasonable drive to use.
364 	 */
365 	unit = DISKUNIT(bp->b_dev);
366 	if (unit > ra_cd.cd_ndevs || (ra = ra_cd.cd_devs[unit]) == NULL) {
367 		bp->b_error = ENXIO;
368 		goto done;
369 	}
370 	/*
371 	 * If drive is open `raw' or reading label, let it at it.
372 	 */
373 	if (ra->ra_state == DK_RDLABEL) {
374 	        /* Make some statistics... /bqt */
375 	        b = splbio();
376 	        disk_busy(&ra->ra_disk);
377 		splx(b);
378 		mscp_strategy(bp, device_parent(&ra->ra_dev));
379 		return;
380 	}
381 
382 	/* If disk is not online, try to put it online */
383 	if (ra->ra_state == DK_CLOSED)
384 		if (ra_putonline(ra) == MSCP_FAILED) {
385 			bp->b_error = EIO;
386 			goto done;
387 		}
388 
389 	/*
390 	 * Determine the size of the transfer, and make sure it is
391 	 * within the boundaries of the partition.
392 	 */
393 	if (bounds_check_with_label(&ra->ra_disk, bp, ra->ra_wlabel) <= 0)
394 		goto done;
395 
396 	/* Make some statistics... /bqt */
397 	b = splbio();
398 	disk_busy(&ra->ra_disk);
399 	splx(b);
400 	mscp_strategy(bp, device_parent(&ra->ra_dev));
401 	return;
402 
403 done:
404 	biodone(bp);
405 }
406 
407 int
408 raread(dev, uio, flags)
409 	dev_t dev;
410 	struct uio *uio;
411 	int flags;
412 {
413 
414 	return (physio(rastrategy, NULL, dev, B_READ, minphys, uio));
415 }
416 
417 int
418 rawrite(dev, uio, flags)
419 	dev_t dev;
420 	struct uio *uio;
421 	int flags;
422 {
423 
424 	return (physio(rastrategy, NULL, dev, B_WRITE, minphys, uio));
425 }
426 
427 /*
428  * I/O controls.
429  */
430 int
431 raioctl(dev, cmd, data, flag, l)
432 	dev_t dev;
433 	u_long cmd;
434 	void *data;
435 	int flag;
436 	struct lwp *l;
437 {
438 	int unit = DISKUNIT(dev);
439 	struct disklabel *lp, *tp;
440 	struct ra_softc *ra = ra_cd.cd_devs[unit];
441 	int error = 0;
442 #ifdef __HAVE_OLD_DISKLABEL
443 	struct disklabel newlabel;
444 #endif
445 
446 	lp = ra->ra_disk.dk_label;
447 
448 	switch (cmd) {
449 
450 	case DIOCGDINFO:
451 		bcopy(lp, data, sizeof (struct disklabel));
452 		break;
453 #ifdef __HAVE_OLD_DISKLABEL
454 	case ODIOCGDINFO:
455 		bcopy(lp, &newlabel, sizeof disklabel);
456 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
457 			return ENOTTY;
458 		bcopy(&newlabel, data, sizeof (struct olddisklabel));
459 		break;
460 #endif
461 
462 	case DIOCGPART:
463 		((struct partinfo *)data)->disklab = lp;
464 		((struct partinfo *)data)->part =
465 		    &lp->d_partitions[DISKPART(dev)];
466 		break;
467 
468 	case DIOCWDINFO:
469 	case DIOCSDINFO:
470 #ifdef __HAVE_OLD_DISKLABEL
471 	case ODIOCWDINFO:
472 	case ODIOCSDINFO:
473 		if (cmd == ODIOCSDINFO || xfer == ODIOCWDINFO) {
474 			memset(&newlabel, 0, sizeof newlabel);
475 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
476 			tp = &newlabel;
477 		} else
478 #endif
479 		tp = (struct disklabel *)data;
480 
481 		if ((flag & FWRITE) == 0)
482 			error = EBADF;
483 		else {
484 			mutex_enter(&ra->ra_disk.dk_openlock);
485 			error = setdisklabel(lp, tp, 0, 0);
486 			if ((error == 0) && (cmd == DIOCWDINFO
487 #ifdef __HAVE_OLD_DISKLABEL
488 			    || cmd == ODIOCWDINFO
489 #else
490 			    )) {
491 #endif
492 				ra->ra_wlabel = 1;
493 				error = writedisklabel(dev, rastrategy, lp,0);
494 				ra->ra_wlabel = 0;
495 			}
496 			mutex_exit(&ra->ra_disk.dk_openlock);
497 		}
498 		break;
499 
500 	case DIOCWLABEL:
501 		if ((flag & FWRITE) == 0)
502 			error = EBADF;
503 		else
504 			ra->ra_wlabel = 1;
505 		break;
506 
507 	case DIOCGDEFLABEL:
508 #ifdef __HAVE_OLD_DISKLABEL
509 	case ODIOCGDEFLABEL:
510 		if (cmd == ODIOCGDEFLABEL)
511 			tp = &newlabel;
512 		else
513 #else
514 		tp = (struct disklabel *)data;
515 #endif
516 		bzero(tp, sizeof(struct disklabel));
517 		tp->d_secsize = lp->d_secsize;
518 		tp->d_nsectors = lp->d_nsectors;
519 		tp->d_ntracks = lp->d_ntracks;
520 		tp->d_ncylinders = lp->d_ncylinders;
521 		tp->d_secpercyl = lp->d_secpercyl;
522 		tp->d_secperunit = lp->d_secperunit;
523 		tp->d_type = DTYPE_MSCP;
524 		tp->d_rpm = 3600;
525 		rrmakelabel(tp, ra->ra_mediaid);
526 #ifdef __HAVE_OLD_DISKLABEL
527 		if (cmd == ODIOCGDEFLABEL) {
528 			if (tp->d_npartitions > OLDMAXPARTITIONS)
529 				return ENOTTY;
530 			memcpy(data, tp, sizeof (struct olddisklabel));
531 		}
532 #endif
533 		break;
534 
535 	case DIOCAWEDGE:
536 	    {
537 	    	struct dkwedge_info *dkw = (void *) data;
538 
539 		if ((flag & FWRITE) == 0)
540 			return (EBADF);
541 
542 		/* If the ioctl happens here, the parent is us. */
543 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
544 		return (dkwedge_add(dkw));
545 	    }
546 
547 	case DIOCDWEDGE:
548 	    {
549 	    	struct dkwedge_info *dkw = (void *) data;
550 
551 		if ((flag & FWRITE) == 0)
552 			return (EBADF);
553 
554 		/* If the ioctl happens here, the parent is us. */
555 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
556 		return (dkwedge_del(dkw));
557 	    }
558 
559 	case DIOCLWEDGES:
560 	    {
561 	    	struct dkwedge_list *dkwl = (void *) data;
562 
563 		return (dkwedge_list(&ra->ra_disk, dkwl, l));
564 	    }
565 
566 	default:
567 		error = ENOTTY;
568 		break;
569 	}
570 	return (error);
571 }
572 
573 
574 int
575 radump(dev, blkno, va, size)
576 	dev_t	dev;
577 	daddr_t blkno;
578 	void *va;
579 	size_t	size;
580 {
581 	return ENXIO;
582 }
583 
584 /*
585  * Return the size of a partition, if known, or -1 if not.
586  */
587 int
588 rasize(dev)
589 	dev_t dev;
590 {
591 	int unit = DISKUNIT(dev);
592 	struct ra_softc *ra;
593 
594 	if (unit >= ra_cd.cd_ndevs || ra_cd.cd_devs[unit] == 0)
595 		return -1;
596 
597 	ra = ra_cd.cd_devs[unit];
598 
599 	if (ra->ra_state == DK_CLOSED)
600 		if (ra_putonline(ra) == MSCP_FAILED)
601 			return -1;
602 
603 	return ra->ra_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
604 	    (ra->ra_disk.dk_label->d_secsize / DEV_BSIZE);
605 }
606 
607 #endif /* NRA */
608 
609 #if NRX
610 
611 int	rxmatch(struct device *, struct cfdata *, void *);
612 
613 CFATTACH_DECL(rx, sizeof(struct rx_softc),
614     rxmatch, rxattach, NULL, NULL);
615 
616 dev_type_open(rxopen);
617 dev_type_read(rxread);
618 dev_type_write(rxwrite);
619 dev_type_ioctl(rxioctl);
620 dev_type_strategy(rxstrategy);
621 dev_type_dump(rxdump);
622 dev_type_size(rxsize);
623 
624 const struct bdevsw rx_bdevsw = {
625 	rxopen, nullclose, rxstrategy, rxioctl, rxdump, rxsize, D_DISK
626 };
627 
628 const struct cdevsw rx_cdevsw = {
629 	rxopen, nullclose, rxread, rxwrite, rxioctl,
630 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
631 };
632 
633 static struct dkdriver rxdkdriver = {
634 	rxstrategy, minphys
635 };
636 
637 /*
638  * More driver definitions, for generic MSCP code.
639  */
640 
641 int
642 rxmatch(parent, cf, aux)
643 	struct	device *parent;
644 	struct	cfdata *cf;
645 	void	*aux;
646 {
647 	struct	drive_attach_args *da = aux;
648 	struct	mscp *mp = da->da_mp;
649 
650 	if ((da->da_typ & MSCPBUS_DISK) == 0)
651 		return 0;
652 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
653 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
654 		return 0;
655 	/*
656 	 * Check if this disk is a floppy; then configure it.
657 	 * Seems to be a safe way to test it per Chris Torek.
658 	 */
659 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
660 		return 1;
661 	return 0;
662 }
663 
664 #endif /* NRX */
665 
666 /*
667  * The attach routine only checks and prints drive type.
668  * Bringing the disk online is done when the disk is accessed
669  * the first time.
670  */
671 void
672 rxattach(parent, self, aux)
673 	struct	device *parent, *self;
674 	void	*aux;
675 {
676 	struct	rx_softc *rx = device_private(self);
677 	struct	drive_attach_args *da = aux;
678 	struct	mscp *mp = da->da_mp;
679 	struct	mscp_softc *mi = (void *)parent;
680 	struct	disklabel *dl;
681 
682 	rx->ra_mediaid = mp->mscp_guse.guse_mediaid;
683 	rx->ra_state = DK_CLOSED;
684 	rx->ra_hwunit = mp->mscp_unit;
685 	mi->mi_dp[mp->mscp_unit] = self;
686 
687 #if NRX
688 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
689 		disk_init((struct disk *)&rx->ra_disk, rx->ra_dev.dv_xname,
690 		    &rxdkdriver);
691 #endif
692 #if NRA
693 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@')
694 		disk_init((struct disk *)&rx->ra_disk, rx->ra_dev.dv_xname,
695 		    &radkdriver);
696 #endif
697 	disk_attach((struct disk *)&rx->ra_disk);
698 
699 	/* Fill in what we know. The actual size is gotten later */
700 	dl = rx->ra_disk.dk_label;
701 
702 	dl->d_secsize = DEV_BSIZE;
703 	dl->d_nsectors = mp->mscp_guse.guse_nspt;
704 	dl->d_ntracks = mp->mscp_guse.guse_ngpc * mp->mscp_guse.guse_group;
705 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
706 	disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
707 #ifdef DEBUG
708 	printf("%s: nspt %d group %d ngpc %d rct %d nrpt %d nrct %d\n",
709 	    self->dv_xname, mp->mscp_guse.guse_nspt, mp->mscp_guse.guse_group,
710 	    mp->mscp_guse.guse_ngpc, mp->mscp_guse.guse_rctsize,
711 	    mp->mscp_guse.guse_nrpt, mp->mscp_guse.guse_nrct);
712 #endif
713 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@') {
714 		/*
715 		 * XXX We should try to discover wedges here, but
716 		 * XXX that would mean being able to do I/O.  Should
717 		 * XXX use config_defer() here.
718 		 */
719 	}
720 }
721 
722 /*
723  * (Try to) put the drive online. This is done the first time the
724  * drive is opened, or if it har fallen offline.
725  */
726 int
727 rx_putonline(rx)
728 	struct rx_softc *rx;
729 {
730 	struct	mscp *mp;
731 	struct	mscp_softc *mi =
732 	    (struct mscp_softc *)device_parent(&rx->ra_dev);
733 	volatile int i;
734 
735 	rx->ra_state = DK_CLOSED;
736 	mp = mscp_getcp(mi, MSCP_WAIT);
737 	mp->mscp_opcode = M_OP_ONLINE;
738 	mp->mscp_unit = rx->ra_hwunit;
739 	mp->mscp_cmdref = 1;
740 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
741 
742 	/* Poll away */
743 	i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
744 	if (tsleep(&rx->ra_state, PRIBIO, "rxonline", 100*100))
745 		rx->ra_state = DK_CLOSED;
746 
747 	if (rx->ra_state == DK_CLOSED)
748 		return MSCP_FAILED;
749 
750 	return MSCP_DONE;
751 }
752 
753 #if NRX
754 
755 /*
756  * Open a drive.
757  */
758 /*ARGSUSED*/
759 int
760 rxopen(dev, flag, fmt, l)
761 	dev_t dev;
762 	int flag, fmt;
763 	struct	lwp *l;
764 {
765 	struct rx_softc *rx;
766 	int unit;
767 
768 	/*
769 	 * Make sure this is a reasonable open request.
770 	 */
771 	unit = DISKUNIT(dev);
772 	if (unit >= rx_cd.cd_ndevs)
773 		return ENXIO;
774 	rx = rx_cd.cd_devs[unit];
775 	if (rx == 0)
776 		return ENXIO;
777 
778 	/*
779 	 * If this is the first open; we must first try to put
780 	 * the disk online (and read the label).
781 	 */
782 	if (rx->ra_state == DK_CLOSED)
783 		if (rx_putonline(rx) == MSCP_FAILED)
784 			return ENXIO;
785 
786 	return 0;
787 }
788 
789 /*
790  * Queue a transfer request, and if possible, hand it to the controller.
791  *
792  * This routine is broken into two so that the internal version
793  * udastrat1() can be called by the (nonexistent, as yet) bad block
794  * revectoring routine.
795  */
796 void
797 rxstrategy(bp)
798 	struct buf *bp;
799 {
800 	int unit;
801 	struct rx_softc *rx;
802 	int b;
803 
804 	/*
805 	 * Make sure this is a reasonable drive to use.
806 	 */
807 	unit = DISKUNIT(bp->b_dev);
808 	if (unit > rx_cd.cd_ndevs || (rx = rx_cd.cd_devs[unit]) == NULL) {
809 		bp->b_error = ENXIO;
810 		goto done;
811 	}
812 
813 	/* If disk is not online, try to put it online */
814 	if (rx->ra_state == DK_CLOSED)
815 		if (rx_putonline(rx) == MSCP_FAILED) {
816 			bp->b_error = EIO;
817 			goto done;
818 		}
819 
820 	/*
821 	 * Determine the size of the transfer, and make sure it is
822 	 * within the boundaries of the partition.
823 	 */
824 	if (bp->b_blkno >= rx->ra_disk.dk_label->d_secperunit) {
825 		bp->b_resid = bp->b_bcount;
826 		goto done;
827 	}
828 
829 	/* Make some statistics... /bqt */
830 	b = splbio();
831 	disk_busy(&rx->ra_disk);
832 	splx(b);
833 	mscp_strategy(bp, device_parent(&rx->ra_dev));
834 	return;
835 
836 done:
837 	biodone(bp);
838 }
839 
840 int
841 rxread(dev, uio, flag)
842 	dev_t dev;
843 	struct uio *uio;
844 	int flag;
845 {
846 
847 	return (physio(rxstrategy, NULL, dev, B_READ, minphys, uio));
848 }
849 
850 int
851 rxwrite(dev, uio, flag)
852 	dev_t dev;
853 	struct uio *uio;
854 	int flag;
855 {
856 
857 	return (physio(rxstrategy, NULL, dev, B_WRITE, minphys, uio));
858 }
859 
860 /*
861  * I/O controls.
862  */
863 int
864 rxioctl(dev, cmd, data, flag, l)
865 	dev_t dev;
866 	u_long cmd;
867 	void *data;
868 	int flag;
869 	struct lwp *l;
870 {
871 	int unit = DISKUNIT(dev);
872 	struct disklabel *lp;
873 	struct rx_softc *rx = rx_cd.cd_devs[unit];
874 	int error = 0;
875 
876 	lp = rx->ra_disk.dk_label;
877 
878 	switch (cmd) {
879 
880 	case DIOCGDINFO:
881 		bcopy(lp, data, sizeof (struct disklabel));
882 		break;
883 
884 	case DIOCGPART:
885 		((struct partinfo *)data)->disklab = lp;
886 		((struct partinfo *)data)->part =
887 		    &lp->d_partitions[DISKPART(dev)];
888 		break;
889 
890 
891 	case DIOCWDINFO:
892 	case DIOCSDINFO:
893 	case DIOCWLABEL:
894 		break;
895 
896 	default:
897 		error = ENOTTY;
898 		break;
899 	}
900 	return (error);
901 }
902 
903 int
904 rxdump(dev, blkno, va, size)
905 	dev_t dev;
906 	daddr_t blkno;
907 	void *va;
908 	size_t size;
909 {
910 
911 	/* Not likely. */
912 	return ENXIO;
913 }
914 
915 int
916 rxsize(dev)
917 	dev_t dev;
918 {
919 
920 	return -1;
921 }
922 
923 #endif /* NRX */
924 
925 void	rrdgram(struct device *, struct mscp *, struct mscp_softc *);
926 void	rriodone(struct device *, struct buf *);
927 int	rronline(struct device *, struct mscp *);
928 int	rrgotstatus(struct device *, struct mscp *);
929 void	rrreplace(struct device *, struct mscp *);
930 int	rrioerror(struct device *, struct mscp *, struct buf *);
931 void	rrfillin(struct buf *, struct mscp *);
932 void	rrbb(struct device *, struct mscp *, struct buf *);
933 
934 
935 struct	mscp_device ra_device = {
936 	rrdgram,
937 	rriodone,
938 	rronline,
939 	rrgotstatus,
940 	rrreplace,
941 	rrioerror,
942 	rrbb,
943 	rrfillin,
944 };
945 
946 /*
947  * Handle an error datagram.
948  * This can come from an unconfigured drive as well.
949  */
950 void
951 rrdgram(usc, mp, mi)
952 	struct device *usc;
953 	struct mscp *mp;
954 	struct mscp_softc *mi;
955 {
956 	if (mscp_decodeerror(usc == NULL?"unconf disk" : usc->dv_xname, mp, mi))
957 		return;
958 	/*
959 	 * SDI status information bytes 10 and 11 are the microprocessor
960 	 * error code and front panel code respectively.  These vary per
961 	 * drive type and are printed purely for field service information.
962 	 */
963 	if (mp->mscp_format == M_FM_SDI)
964 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
965 			mp->mscp_erd.erd_sdistat[10],
966 			mp->mscp_erd.erd_sdistat[11]);
967 }
968 
969 void
970 rriodone(usc, bp)
971 	struct device *usc;
972 	struct buf *bp;
973 {
974 	struct ra_softc *ra;
975 	int unit;
976 
977 	/* We assume that this is a reasonable drive. ra_strategy should
978 	   already have verified it. Thus, no checks here... /bqt */
979 	unit = DISKUNIT(bp->b_dev);
980 #if NRA
981 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
982 		ra = ra_cd.cd_devs[unit];
983 	else
984 #endif
985 #if NRX
986 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
987 		ra = rx_cd.cd_devs[unit];
988 	else
989 #endif
990 		panic("rriodone: unexpected major %d unit %d",
991 		    major(bp->b_dev), 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((void *)&rx->ra_state);
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_error = EIO;
1121 		ra->ra_state = DK_CLOSED;
1122 		if (code & M_OFFLINE_UNMOUNTED)
1123 			printf("%s: not mounted/spun down\n", usc->dv_xname);
1124 		if (code & M_OFFLINE_DUPLICATE)
1125 			printf("%s: duplicate unit number!!!\n", usc->dv_xname);
1126 		return MSCP_DONE;
1127 
1128 	case M_ST_AVAILABLE:
1129 		ra->ra_state = DK_CLOSED; /* Force another online */
1130 		return MSCP_DONE;
1131 
1132 	default:
1133 		printf("%s:", usc->dv_xname);
1134 		break;
1135 	}
1136 	return (MSCP_FAILED);
1137 }
1138 
1139 /*
1140  * Fill in disk addresses in a mscp packet waiting for transfer.
1141  */
1142 void
1143 rrfillin(bp, mp)
1144 	struct buf *bp;
1145 	struct mscp *mp;
1146 {
1147 	struct rx_softc *rx = 0; /* Wall */
1148 	struct disklabel *lp;
1149 	int unit = DISKUNIT(bp->b_dev);
1150 	int part = DISKPART(bp->b_dev);
1151 
1152 #if NRA
1153 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
1154 		rx = ra_cd.cd_devs[unit];
1155 #endif
1156 #if NRX
1157 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
1158 		rx = rx_cd.cd_devs[unit];
1159 #endif
1160 	lp = rx->ra_disk.dk_label;
1161 
1162 	mp->mscp_seq.seq_lbn = lp->d_partitions[part].p_offset + bp->b_blkno;
1163 	mp->mscp_unit = rx->ra_hwunit;
1164 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
1165 }
1166 
1167 /*
1168  * A bad block related operation finished.
1169  */
1170 /*ARGSUSED*/
1171 void
1172 rrbb(usc, mp, bp)
1173 	struct device *usc;
1174 	struct mscp *mp;
1175 	struct buf *bp;
1176 {
1177 
1178 	panic("udabb");
1179 }
1180