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