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