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