xref: /netbsd-src/sys/dev/mscp/mscp_disk.c (revision 2de962bd804263c16657f586aa00f1704045df8e)
1 /*	$NetBSD: mscp_disk.c,v 1.59 2008/04/08 20:10:44 cegger 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.59 2008/04/08 20:10:44 cegger 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", device_xname(&ra->ra_dev));
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 		strlcpy(dkw->dkw_parent, device_xname(&ra->ra_dev),
544 			sizeof(dkw->dkw_parent));
545 		return (dkwedge_add(dkw));
546 	    }
547 
548 	case DIOCDWEDGE:
549 	    {
550 	    	struct dkwedge_info *dkw = (void *) data;
551 
552 		if ((flag & FWRITE) == 0)
553 			return (EBADF);
554 
555 		/* If the ioctl happens here, the parent is us. */
556 		strlcpy(dkw->dkw_parent, device_xname(&ra->ra_dev),
557 			sizeof(dkw->dkw_parent));
558 		return (dkwedge_del(dkw));
559 	    }
560 
561 	case DIOCLWEDGES:
562 	    {
563 	    	struct dkwedge_list *dkwl = (void *) data;
564 
565 		return (dkwedge_list(&ra->ra_disk, dkwl, l));
566 	    }
567 
568 	default:
569 		error = ENOTTY;
570 		break;
571 	}
572 	return (error);
573 }
574 
575 
576 int
577 radump(dev, blkno, va, size)
578 	dev_t	dev;
579 	daddr_t blkno;
580 	void *va;
581 	size_t	size;
582 {
583 	return ENXIO;
584 }
585 
586 /*
587  * Return the size of a partition, if known, or -1 if not.
588  */
589 int
590 rasize(dev)
591 	dev_t dev;
592 {
593 	int unit = DISKUNIT(dev);
594 	struct ra_softc *ra;
595 
596 	if (unit >= ra_cd.cd_ndevs || ra_cd.cd_devs[unit] == 0)
597 		return -1;
598 
599 	ra = ra_cd.cd_devs[unit];
600 
601 	if (ra->ra_state == DK_CLOSED)
602 		if (ra_putonline(ra) == MSCP_FAILED)
603 			return -1;
604 
605 	return ra->ra_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
606 	    (ra->ra_disk.dk_label->d_secsize / DEV_BSIZE);
607 }
608 
609 #endif /* NRA */
610 
611 #if NRX
612 
613 int	rxmatch(struct device *, struct cfdata *, void *);
614 
615 CFATTACH_DECL(rx, sizeof(struct rx_softc),
616     rxmatch, rxattach, NULL, NULL);
617 
618 dev_type_open(rxopen);
619 dev_type_read(rxread);
620 dev_type_write(rxwrite);
621 dev_type_ioctl(rxioctl);
622 dev_type_strategy(rxstrategy);
623 dev_type_dump(rxdump);
624 dev_type_size(rxsize);
625 
626 const struct bdevsw rx_bdevsw = {
627 	rxopen, nullclose, rxstrategy, rxioctl, rxdump, rxsize, D_DISK
628 };
629 
630 const struct cdevsw rx_cdevsw = {
631 	rxopen, nullclose, rxread, rxwrite, rxioctl,
632 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
633 };
634 
635 static struct dkdriver rxdkdriver = {
636 	rxstrategy, minphys
637 };
638 
639 /*
640  * More driver definitions, for generic MSCP code.
641  */
642 
643 int
644 rxmatch(parent, cf, aux)
645 	struct	device *parent;
646 	struct	cfdata *cf;
647 	void	*aux;
648 {
649 	struct	drive_attach_args *da = aux;
650 	struct	mscp *mp = da->da_mp;
651 
652 	if ((da->da_typ & MSCPBUS_DISK) == 0)
653 		return 0;
654 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
655 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
656 		return 0;
657 	/*
658 	 * Check if this disk is a floppy; then configure it.
659 	 * Seems to be a safe way to test it per Chris Torek.
660 	 */
661 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
662 		return 1;
663 	return 0;
664 }
665 
666 #endif /* NRX */
667 
668 /*
669  * The attach routine only checks and prints drive type.
670  * Bringing the disk online is done when the disk is accessed
671  * the first time.
672  */
673 void
674 rxattach(parent, self, aux)
675 	struct	device *parent, *self;
676 	void	*aux;
677 {
678 	struct	rx_softc *rx = device_private(self);
679 	struct	drive_attach_args *da = aux;
680 	struct	mscp *mp = da->da_mp;
681 	struct	mscp_softc *mi = (void *)parent;
682 	struct	disklabel *dl;
683 
684 	rx->ra_mediaid = mp->mscp_guse.guse_mediaid;
685 	rx->ra_state = DK_CLOSED;
686 	rx->ra_hwunit = mp->mscp_unit;
687 	mi->mi_dp[mp->mscp_unit] = self;
688 
689 #if NRX
690 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
691 		disk_init((struct disk *)&rx->ra_disk, device_xname(&rx->ra_dev),
692 		    &rxdkdriver);
693 #endif
694 #if NRA
695 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@')
696 		disk_init((struct disk *)&rx->ra_disk, device_xname(&rx->ra_dev),
697 		    &radkdriver);
698 #endif
699 	disk_attach((struct disk *)&rx->ra_disk);
700 
701 	/* Fill in what we know. The actual size is gotten later */
702 	dl = rx->ra_disk.dk_label;
703 
704 	dl->d_secsize = DEV_BSIZE;
705 	dl->d_nsectors = mp->mscp_guse.guse_nspt;
706 	dl->d_ntracks = mp->mscp_guse.guse_ngpc * mp->mscp_guse.guse_group;
707 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
708 	disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
709 #ifdef DEBUG
710 	printf("%s: nspt %d group %d ngpc %d rct %d nrpt %d nrct %d\n",
711 	    device_xname(self), mp->mscp_guse.guse_nspt, mp->mscp_guse.guse_group,
712 	    mp->mscp_guse.guse_ngpc, mp->mscp_guse.guse_rctsize,
713 	    mp->mscp_guse.guse_nrpt, mp->mscp_guse.guse_nrct);
714 #endif
715 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@') {
716 		/*
717 		 * XXX We should try to discover wedges here, but
718 		 * XXX that would mean being able to do I/O.  Should
719 		 * XXX use config_defer() here.
720 		 */
721 	}
722 }
723 
724 /*
725  * (Try to) put the drive online. This is done the first time the
726  * drive is opened, or if it har fallen offline.
727  */
728 int
729 rx_putonline(rx)
730 	struct rx_softc *rx;
731 {
732 	struct	mscp *mp;
733 	struct	mscp_softc *mi =
734 	    (struct mscp_softc *)device_parent(&rx->ra_dev);
735 	volatile int i;
736 
737 	rx->ra_state = DK_CLOSED;
738 	mp = mscp_getcp(mi, MSCP_WAIT);
739 	mp->mscp_opcode = M_OP_ONLINE;
740 	mp->mscp_unit = rx->ra_hwunit;
741 	mp->mscp_cmdref = 1;
742 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
743 
744 	/* Poll away */
745 	i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
746 	if (tsleep(&rx->ra_state, PRIBIO, "rxonline", 100*100))
747 		rx->ra_state = DK_CLOSED;
748 
749 	if (rx->ra_state == DK_CLOSED)
750 		return MSCP_FAILED;
751 
752 	return MSCP_DONE;
753 }
754 
755 #if NRX
756 
757 /*
758  * Open a drive.
759  */
760 /*ARGSUSED*/
761 int
762 rxopen(dev, flag, fmt, l)
763 	dev_t dev;
764 	int flag, fmt;
765 	struct	lwp *l;
766 {
767 	struct rx_softc *rx;
768 	int unit;
769 
770 	/*
771 	 * Make sure this is a reasonable open request.
772 	 */
773 	unit = DISKUNIT(dev);
774 	if (unit >= rx_cd.cd_ndevs)
775 		return ENXIO;
776 	rx = rx_cd.cd_devs[unit];
777 	if (rx == 0)
778 		return ENXIO;
779 
780 	/*
781 	 * If this is the first open; we must first try to put
782 	 * the disk online (and read the label).
783 	 */
784 	if (rx->ra_state == DK_CLOSED)
785 		if (rx_putonline(rx) == MSCP_FAILED)
786 			return ENXIO;
787 
788 	return 0;
789 }
790 
791 /*
792  * Queue a transfer request, and if possible, hand it to the controller.
793  *
794  * This routine is broken into two so that the internal version
795  * udastrat1() can be called by the (nonexistent, as yet) bad block
796  * revectoring routine.
797  */
798 void
799 rxstrategy(bp)
800 	struct buf *bp;
801 {
802 	int unit;
803 	struct rx_softc *rx;
804 	int b;
805 
806 	/*
807 	 * Make sure this is a reasonable drive to use.
808 	 */
809 	unit = DISKUNIT(bp->b_dev);
810 	if (unit > rx_cd.cd_ndevs || (rx = rx_cd.cd_devs[unit]) == NULL) {
811 		bp->b_error = ENXIO;
812 		goto done;
813 	}
814 
815 	/* If disk is not online, try to put it online */
816 	if (rx->ra_state == DK_CLOSED)
817 		if (rx_putonline(rx) == MSCP_FAILED) {
818 			bp->b_error = EIO;
819 			goto done;
820 		}
821 
822 	/*
823 	 * Determine the size of the transfer, and make sure it is
824 	 * within the boundaries of the partition.
825 	 */
826 	if (bp->b_blkno >= rx->ra_disk.dk_label->d_secperunit) {
827 		bp->b_resid = bp->b_bcount;
828 		goto done;
829 	}
830 
831 	/* Make some statistics... /bqt */
832 	b = splbio();
833 	disk_busy(&rx->ra_disk);
834 	splx(b);
835 	mscp_strategy(bp, device_parent(&rx->ra_dev));
836 	return;
837 
838 done:
839 	biodone(bp);
840 }
841 
842 int
843 rxread(dev, uio, flag)
844 	dev_t dev;
845 	struct uio *uio;
846 	int flag;
847 {
848 
849 	return (physio(rxstrategy, NULL, dev, B_READ, minphys, uio));
850 }
851 
852 int
853 rxwrite(dev, uio, flag)
854 	dev_t dev;
855 	struct uio *uio;
856 	int flag;
857 {
858 
859 	return (physio(rxstrategy, NULL, dev, B_WRITE, minphys, uio));
860 }
861 
862 /*
863  * I/O controls.
864  */
865 int
866 rxioctl(dev, cmd, data, flag, l)
867 	dev_t dev;
868 	u_long cmd;
869 	void *data;
870 	int flag;
871 	struct lwp *l;
872 {
873 	int unit = DISKUNIT(dev);
874 	struct disklabel *lp;
875 	struct rx_softc *rx = rx_cd.cd_devs[unit];
876 	int error = 0;
877 
878 	lp = rx->ra_disk.dk_label;
879 
880 	switch (cmd) {
881 
882 	case DIOCGDINFO:
883 		bcopy(lp, data, sizeof (struct disklabel));
884 		break;
885 
886 	case DIOCGPART:
887 		((struct partinfo *)data)->disklab = lp;
888 		((struct partinfo *)data)->part =
889 		    &lp->d_partitions[DISKPART(dev)];
890 		break;
891 
892 
893 	case DIOCWDINFO:
894 	case DIOCSDINFO:
895 	case DIOCWLABEL:
896 		break;
897 
898 	default:
899 		error = ENOTTY;
900 		break;
901 	}
902 	return (error);
903 }
904 
905 int
906 rxdump(dev, blkno, va, size)
907 	dev_t dev;
908 	daddr_t blkno;
909 	void *va;
910 	size_t size;
911 {
912 
913 	/* Not likely. */
914 	return ENXIO;
915 }
916 
917 int
918 rxsize(dev)
919 	dev_t dev;
920 {
921 
922 	return -1;
923 }
924 
925 #endif /* NRX */
926 
927 void	rrdgram(struct device *, struct mscp *, struct mscp_softc *);
928 void	rriodone(struct device *, struct buf *);
929 int	rronline(struct device *, struct mscp *);
930 int	rrgotstatus(struct device *, struct mscp *);
931 void	rrreplace(struct device *, struct mscp *);
932 int	rrioerror(struct device *, struct mscp *, struct buf *);
933 void	rrfillin(struct buf *, struct mscp *);
934 void	rrbb(struct device *, struct mscp *, struct buf *);
935 
936 
937 struct	mscp_device ra_device = {
938 	rrdgram,
939 	rriodone,
940 	rronline,
941 	rrgotstatus,
942 	rrreplace,
943 	rrioerror,
944 	rrbb,
945 	rrfillin,
946 };
947 
948 /*
949  * Handle an error datagram.
950  * This can come from an unconfigured drive as well.
951  */
952 void
953 rrdgram(usc, mp, mi)
954 	struct device *usc;
955 	struct mscp *mp;
956 	struct mscp_softc *mi;
957 {
958 	if (mscp_decodeerror(usc == NULL?"unconf disk" : device_xname(usc), mp, mi))
959 		return;
960 	/*
961 	 * SDI status information bytes 10 and 11 are the microprocessor
962 	 * error code and front panel code respectively.  These vary per
963 	 * drive type and are printed purely for field service information.
964 	 */
965 	if (mp->mscp_format == M_FM_SDI)
966 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
967 			mp->mscp_erd.erd_sdistat[10],
968 			mp->mscp_erd.erd_sdistat[11]);
969 }
970 
971 void
972 rriodone(usc, bp)
973 	struct device *usc;
974 	struct buf *bp;
975 {
976 	struct ra_softc *ra;
977 	int unit;
978 
979 	/* We assume that this is a reasonable drive. ra_strategy should
980 	   already have verified it. Thus, no checks here... /bqt */
981 	unit = DISKUNIT(bp->b_dev);
982 #if NRA
983 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
984 		ra = ra_cd.cd_devs[unit];
985 	else
986 #endif
987 #if NRX
988 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
989 		ra = rx_cd.cd_devs[unit];
990 	else
991 #endif
992 		panic("rriodone: unexpected major %d unit %d",
993 		    major(bp->b_dev), unit);
994 	disk_unbusy(&ra->ra_disk, bp->b_bcount, (bp->b_flags & B_READ));
995 
996 	biodone(bp);
997 }
998 
999 /*
1000  * A drive came on line.  Check its type and size.  Return DONE if
1001  * we think the drive is truly on line.	 In any case, awaken anyone
1002  * sleeping on the drive on-line-ness.
1003  */
1004 int
1005 rronline(usc, mp)
1006 	struct device *usc;
1007 	struct mscp *mp;
1008 {
1009 	struct rx_softc *rx = (struct rx_softc *)usc;
1010 	struct disklabel *dl;
1011 
1012 	wakeup((void *)&rx->ra_state);
1013 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1014 		aprint_error_dev(usc, "attempt to bring on line failed: ");
1015 		mscp_printevent(mp);
1016 		return (MSCP_FAILED);
1017 	}
1018 
1019 	rx->ra_state = DK_OPEN;
1020 
1021 	dl = rx->ra_disk.dk_label;
1022 	dl->d_secperunit = (daddr_t)mp->mscp_onle.onle_unitsize;
1023 
1024 	if (dl->d_secpercyl) {
1025 		dl->d_ncylinders = dl->d_secperunit/dl->d_secpercyl;
1026 		dl->d_type = DTYPE_MSCP;
1027 		dl->d_rpm = 3600;
1028 	} else {
1029 		dl->d_type = DTYPE_FLOPPY;
1030 		dl->d_rpm = 300;
1031 	}
1032 	rrmakelabel(dl, rx->ra_mediaid);
1033 
1034 	return (MSCP_DONE);
1035 }
1036 
1037 void
1038 rrmakelabel(dl, type)
1039 	struct disklabel *dl;
1040 	long type;
1041 {
1042 	int n, p = 0;
1043 
1044 	dl->d_bbsize = BBSIZE;
1045 	dl->d_sbsize = SBLOCKSIZE;
1046 
1047 	/* Create the disk name for disklabel. Phew... */
1048 	dl->d_typename[p++] = MSCP_MID_CHAR(2, type);
1049 	dl->d_typename[p++] = MSCP_MID_CHAR(1, type);
1050 	if (MSCP_MID_ECH(0, type))
1051 		dl->d_typename[p++] = MSCP_MID_CHAR(0, type);
1052 	n = MSCP_MID_NUM(type);
1053 	if (n > 99) {
1054 		dl->d_typename[p++] = '1';
1055 		n -= 100;
1056 	}
1057 	if (n > 9) {
1058 		dl->d_typename[p++] = (n / 10) + '0';
1059 		n %= 10;
1060 	}
1061 	dl->d_typename[p++] = n + '0';
1062 	dl->d_typename[p] = 0;
1063 	dl->d_npartitions = MAXPARTITIONS;
1064 	dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
1065 	    dl->d_secperunit;
1066 	dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
1067 	dl->d_interleave = dl->d_headswitch = 1;
1068 	dl->d_magic = dl->d_magic2 = DISKMAGIC;
1069 	dl->d_checksum = dkcksum(dl);
1070 }
1071 
1072 /*
1073  * We got some (configured) unit's status.  Return DONE if it succeeded.
1074  */
1075 int
1076 rrgotstatus(usc, mp)
1077 	struct device *usc;
1078 	struct mscp *mp;
1079 {
1080 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
1081 		aprint_error_dev(usc, "attempt to get status failed: ");
1082 		mscp_printevent(mp);
1083 		return (MSCP_FAILED);
1084 	}
1085 	/* record for (future) bad block forwarding and whatever else */
1086 #ifdef notyet
1087 	uda_rasave(ui->ui_unit, mp, 1);
1088 #endif
1089 	return (MSCP_DONE);
1090 }
1091 
1092 /*
1093  * A replace operation finished.
1094  */
1095 /*ARGSUSED*/
1096 void
1097 rrreplace(usc, mp)
1098 	struct device *usc;
1099 	struct mscp *mp;
1100 {
1101 
1102 	panic("udareplace");
1103 }
1104 
1105 /*
1106  * A transfer failed.  We get a chance to fix or restart it.
1107  * Need to write the bad block forwaring code first....
1108  */
1109 /*ARGSUSED*/
1110 int
1111 rrioerror(usc, mp, bp)
1112 	struct device *usc;
1113 	struct mscp *mp;
1114 	struct buf *bp;
1115 {
1116 	struct ra_softc *ra = (void *)usc;
1117 	int code = mp->mscp_event;
1118 
1119 	switch (code & M_ST_MASK) {
1120 	/* The unit has fallen offline. Try to figure out why. */
1121 	case M_ST_OFFLINE:
1122 		bp->b_error = EIO;
1123 		ra->ra_state = DK_CLOSED;
1124 		if (code & M_OFFLINE_UNMOUNTED)
1125 			aprint_error_dev(usc, "not mounted/spun down\n");
1126 		if (code & M_OFFLINE_DUPLICATE)
1127 			aprint_error_dev(usc, "duplicate unit number!!!\n");
1128 		return MSCP_DONE;
1129 
1130 	case M_ST_AVAILABLE:
1131 		ra->ra_state = DK_CLOSED; /* Force another online */
1132 		return MSCP_DONE;
1133 
1134 	default:
1135 		printf("%s:", device_xname(usc));
1136 		break;
1137 	}
1138 	return (MSCP_FAILED);
1139 }
1140 
1141 /*
1142  * Fill in disk addresses in a mscp packet waiting for transfer.
1143  */
1144 void
1145 rrfillin(bp, mp)
1146 	struct buf *bp;
1147 	struct mscp *mp;
1148 {
1149 	struct rx_softc *rx = 0; /* Wall */
1150 	struct disklabel *lp;
1151 	int unit = DISKUNIT(bp->b_dev);
1152 	int part = DISKPART(bp->b_dev);
1153 
1154 #if NRA
1155 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
1156 		rx = ra_cd.cd_devs[unit];
1157 #endif
1158 #if NRX
1159 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
1160 		rx = rx_cd.cd_devs[unit];
1161 #endif
1162 	lp = rx->ra_disk.dk_label;
1163 
1164 	mp->mscp_seq.seq_lbn = lp->d_partitions[part].p_offset + bp->b_blkno;
1165 	mp->mscp_unit = rx->ra_hwunit;
1166 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
1167 }
1168 
1169 /*
1170  * A bad block related operation finished.
1171  */
1172 /*ARGSUSED*/
1173 void
1174 rrbb(usc, mp, bp)
1175 	struct device *usc;
1176 	struct mscp *mp;
1177 	struct buf *bp;
1178 {
1179 
1180 	panic("udabb");
1181 }
1182