hdc9224.c revision 1.18 1 /* $NetBSD: hdc9224.c,v 1.18 2002/07/22 21:48:38 hannken Exp $ */
2 /*
3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Ludd by Bertram Barth.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed at Ludd, University of
19 * Lule}, Sweden and its contributors.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * with much help from (in alphabetical order):
37 * Jeremy
38 * Roger Ivie
39 * Rick Macklem
40 * Mike Young
41 *
42 * Rewritten by Ragge 25 Jun 2000. New features:
43 * - Uses interrupts instead of polling to signal ready.
44 * - Can cooperate with the SCSI routines WRT. the DMA area.
45 *
46 * TODO:
47 * - Floppy support missing.
48 * - Bad block forwarding missing.
49 * - Statistics collection.
50 */
51 #undef RDDEBUG
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/conf.h>
57 #include <sys/file.h>
58 #include <sys/stat.h>
59 #include <sys/ioctl.h>
60 #include <sys/buf.h>
61 #include <sys/proc.h>
62 #include <sys/user.h>
63 #include <sys/map.h>
64 #include <sys/device.h>
65 #include <sys/dkstat.h>
66 #include <sys/disklabel.h>
67 #include <sys/disk.h>
68 #include <sys/syslog.h>
69 #include <sys/reboot.h>
70
71 #include <uvm/uvm_extern.h>
72
73 #include <ufs/ufs/dinode.h> /* For BBSIZE */
74 #include <ufs/ffs/fs.h>
75
76 #include <machine/pte.h>
77 #include <machine/sid.h>
78 #include <machine/cpu.h>
79 #include <machine/uvax.h>
80 #include <machine/ka410.h>
81 #include <machine/vsbus.h>
82 #include <machine/rpb.h>
83 #include <machine/scb.h>
84
85 #include <dev/mscp/mscp.h> /* For DEC disk encoding */
86
87 #include <vax/vsa/hdc9224.h>
88
89 #include "ioconf.h"
90 #include "locators.h"
91
92
93 /*
94 * on-disk geometry block
95 */
96 #define _aP __attribute__ ((packed)) /* force byte-alignment */
97 struct rdgeom {
98 char mbz[10]; /* 10 bytes of zero */
99 long xbn_count _aP; /* number of XBNs */
100 long dbn_count _aP; /* number of DBNs */
101 long lbn_count _aP; /* number of LBNs (Logical-Block-Numbers) */
102 long rbn_count _aP; /* number of RBNs (Replacement-Block-Numbers) */
103 short nspt; /* number of sectors per track */
104 short ntracks; /* number of tracks */
105 short ncylinders; /* number of cylinders */
106 short precomp; /* first cylinder for write precompensation */
107 short reduced; /* first cylinder for reduced write current */
108 short seek_rate; /* seek rate or zero for buffered seeks */
109 short crc_eec; /* 0 if CRC, 1 if ECC is being used */
110 short rct; /* "replacement control table" (RCT) */
111 short rct_ncopies; /* number of copies of the RCT */
112 long media_id _aP; /* media identifier */
113 short interleave; /* sector-to-sector interleave */
114 short headskew; /* head-to-head skew */
115 short cylskew; /* cylinder-to-cylinder skew */
116 short gap0_size; /* size of GAP 0 in the MFM format */
117 short gap1_size; /* size of GAP 1 in the MFM format */
118 short gap2_size; /* size of GAP 2 in the MFM format */
119 short gap3_size; /* size of GAP 3 in the MFM format */
120 short sync_value; /* sync value used when formatting */
121 char reserved[32]; /* reserved for use by the RQDX formatter */
122 short serial_number; /* serial number */
123 #if 0 /* we don't need these 412 useless bytes ... */
124 char fill[412-2]; /* Filler bytes to the end of the block */
125 short checksum; /* checksum over the XBN */
126 #endif
127 };
128
129 /*
130 * Software status
131 */
132 struct rdsoftc {
133 struct device sc_dev; /* must be here! (pseudo-OOP:) */
134 struct disk sc_disk; /* disklabel etc. */
135 struct rdgeom sc_xbn; /* on-disk geometry information */
136 int sc_drive; /* physical unit number */
137 };
138
139 struct hdcsoftc {
140 struct device sc_dev; /* must be here (pseudo-OOP:) */
141 struct evcnt sc_intrcnt;
142 struct vsbus_dma sc_vd;
143 vaddr_t sc_regs; /* register addresses */
144 struct bufq_state sc_q;
145 struct buf *sc_active;
146 struct hdc9224_UDCreg sc_creg; /* (command) registers to be written */
147 struct hdc9224_UDCreg sc_sreg; /* (status) registers being read */
148 caddr_t sc_dmabase; /* */
149 int sc_dmasize;
150 caddr_t sc_bufaddr; /* Current in-core address */
151 int sc_diskblk; /* Current block on disk */
152 int sc_bytecnt; /* How much left to transfer */
153 int sc_xfer; /* Current transfer size */
154 int sc_retries;
155 volatile u_char sc_status; /* last status from interrupt */
156 char sc_intbit;
157 };
158
159 struct hdc_attach_args {
160 int ha_drive;
161 };
162
163 /*
164 * prototypes for (almost) all the internal routines
165 */
166 static int hdcmatch(struct device *, struct cfdata *, void *);
167 static void hdcattach(struct device *, struct device *, void *);
168 static int hdcprint(void *, const char *);
169 static int rdmatch(struct device *, struct cfdata *, void *);
170 static void rdattach(struct device *, struct device *, void *);
171 static void hdcintr(void *);
172 static int hdc_command(struct hdcsoftc *, int);
173 static void rd_readgeom(struct hdcsoftc *, struct rdsoftc *);
174 #ifdef RDDEBUG
175 static void hdc_printgeom( struct rdgeom *);
176 #endif
177 static void hdc_writeregs(struct hdcsoftc *);
178 static void hdcstart(struct hdcsoftc *, struct buf *);
179 static int hdc_rdselect(struct hdcsoftc *, int);
180 static void rdmakelabel(struct disklabel *, struct rdgeom *);
181 static void hdc_writeregs(struct hdcsoftc *);
182 static void hdc_readregs(struct hdcsoftc *);
183 static void hdc_qstart(void *);
184
185 bdev_decl(rd);
186 cdev_decl(rd);
187
188 struct cfattach hdc_ca = {
189 sizeof(struct hdcsoftc), hdcmatch, hdcattach
190 };
191
192 struct cfattach rd_ca = {
193 sizeof(struct rdsoftc), rdmatch, rdattach
194 };
195
196
197 /* At least 0.7 uS between register accesses */
198 static int rd_dmasize, inq = 0;
199 static int u;
200 #define WAIT asm("movl %0,%0;movl %0,%0;movl %0,%0; movl %0,%0" :: "m"(u))
201
202 #define HDC_WREG(x) *(volatile char *)(sc->sc_regs) = (x)
203 #define HDC_RREG *(volatile char *)(sc->sc_regs)
204 #define HDC_WCMD(x) *(volatile char *)(sc->sc_regs + 4) = (x)
205 #define HDC_RSTAT *(volatile char *)(sc->sc_regs + 4)
206
207 /*
208 * new-config's hdcmatch() is similiar to old-config's hdcprobe(),
209 * thus we probe for the existence of the controller and reset it.
210 * NB: we can't initialize the controller yet, since space for hdcsoftc
211 * is not yet allocated. Thus we do this in hdcattach()...
212 */
213 int
214 hdcmatch(struct device *parent, struct cfdata *cf, void *aux)
215 {
216 struct vsbus_attach_args *va = aux;
217 volatile char *hdc_csr = (char *)va->va_addr;
218 int i;
219
220 u = 8; /* !!! - GCC */
221
222 if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_46
223 || vax_boardtype == VAX_BTYP_48 || vax_boardtype == VAX_BTYP_53)
224 return 0;
225
226 hdc_csr[4] = DKC_CMD_RESET; /* reset chip */
227 for (i = 0; i < 1000; i++) {
228 DELAY(1000);
229 if (hdc_csr[4] & DKC_ST_DONE)
230 break;
231 }
232 if (i == 100)
233 return 0; /* No response to reset */
234
235 hdc_csr[4] = DKC_CMD_SETREGPTR|UDC_TERM;
236 WAIT;
237 hdc_csr[0] = UDC_TC_CRCPRE|UDC_TC_INTDONE;
238 WAIT;
239 hdc_csr[4] = DKC_CMD_DRDESELECT; /* Should be harmless */
240 DELAY(1000);
241 return (1);
242 }
243
244 int
245 hdcprint(void *aux, const char *name)
246 {
247 struct hdc_attach_args *ha = aux;
248
249 if (name)
250 printf ("RD?? at %s drive %d", name, ha->ha_drive);
251 return UNCONF;
252 }
253
254 /*
255 * hdc_attach() probes for all possible devices
256 */
257 void
258 hdcattach(struct device *parent, struct device *self, void *aux)
259 {
260 struct vsbus_attach_args *va = aux;
261 struct hdcsoftc *sc = (void *)self;
262 struct hdc_attach_args ha;
263 int status, i;
264
265 printf ("\n");
266 /*
267 * Get interrupt vector, enable instrumentation.
268 */
269 scb_vecalloc(va->va_cvec, hdcintr, sc, SCB_ISTACK, &sc->sc_intrcnt);
270 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
271 self->dv_xname, "intr");
272
273 sc->sc_regs = vax_map_physmem(va->va_paddr, 1);
274 sc->sc_dmabase = (caddr_t)va->va_dmaaddr;
275 sc->sc_dmasize = va->va_dmasize;
276 sc->sc_intbit = va->va_maskno;
277 rd_dmasize = min(MAXPHYS, sc->sc_dmasize); /* Used in rd_minphys */
278
279 sc->sc_vd.vd_go = hdc_qstart;
280 sc->sc_vd.vd_arg = sc;
281 /*
282 * Reset controller.
283 */
284 HDC_WCMD(DKC_CMD_RESET);
285 DELAY(1000);
286 status = HDC_RSTAT;
287 if (status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
288 printf("%s: RESET failed, status 0x%x\n",
289 sc->sc_dev.dv_xname, status);
290 return;
291 }
292 bufq_alloc(&sc->sc_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
293
294 /*
295 * now probe for all possible hard drives
296 */
297 for (i = 0; i < 4; i++) {
298 if (i == 2) /* Floppy, needs special handling */
299 continue;
300 HDC_WCMD(DKC_CMD_DRSELECT | i);
301 DELAY(1000);
302 status = HDC_RSTAT;
303 ha.ha_drive = i;
304 if ((status & DKC_ST_TERMCOD) == DKC_TC_SUCCESS)
305 config_found(self, (void *)&ha, hdcprint);
306 }
307 }
308
309 /*
310 * rdmatch() probes for the existence of a RD-type disk/floppy
311 */
312 int
313 rdmatch(parent, cf, aux)
314 struct device *parent;
315 struct cfdata *cf;
316 void *aux;
317 {
318 struct hdc_attach_args *ha = aux;
319
320 if (cf->cf_loc[HDCCF_DRIVE] != HDCCF_DRIVE_DEFAULT &&
321 cf->cf_loc[HDCCF_DRIVE] != ha->ha_drive)
322 return 0;
323
324 if (ha->ha_drive == 2) /* Always floppy, not supported */
325 return 0;
326
327 return 1;
328 }
329
330 #define RDMAJOR 19
331
332 void
333 rdattach(struct device *parent, struct device *self, void *aux)
334 {
335 struct hdcsoftc *sc = (void*)parent;
336 struct rdsoftc *rd = (void*)self;
337 struct hdc_attach_args *ha = aux;
338 struct disklabel *dl;
339 char *msg;
340
341 rd->sc_drive = ha->ha_drive;
342 /*
343 * Initialize and attach the disk structure.
344 */
345 rd->sc_disk.dk_name = rd->sc_dev.dv_xname;
346 disk_attach(&rd->sc_disk);
347
348 /*
349 * if it's not a floppy then evaluate the on-disk geometry.
350 * if necessary correct the label...
351 */
352 rd_readgeom(sc, rd);
353 disk_printtype(rd->sc_drive, rd->sc_xbn.media_id);
354 dl = rd->sc_disk.dk_label;
355 rdmakelabel(dl, &rd->sc_xbn);
356 printf("%s", rd->sc_dev.dv_xname);
357 msg = readdisklabel(MAKEDISKDEV(RDMAJOR, rd->sc_dev.dv_unit, RAW_PART),
358 rdstrategy, dl, NULL);
359 if (msg)
360 printf(": %s", msg);
361 printf(": size %d sectors\n", dl->d_secperunit);
362 #ifdef RDDEBUG
363 hdc_printgeom(&rd->sc_xbn);
364 #endif
365 }
366
367 void
368 hdcintr(void *arg)
369 {
370 struct hdcsoftc *sc = arg;
371 struct buf *bp;
372
373 sc->sc_status = HDC_RSTAT;
374 if (sc->sc_active == 0)
375 return; /* Complain? */
376
377 if ((sc->sc_status & (DKC_ST_INTPEND|DKC_ST_DONE)) !=
378 (DKC_ST_INTPEND|DKC_ST_DONE))
379 return; /* Why spurious ints sometimes??? */
380
381 bp = sc->sc_active;
382 sc->sc_active = 0;
383 if ((sc->sc_status & DKC_ST_TERMCOD) != DKC_TC_SUCCESS) {
384 int i;
385 u_char *g = (u_char *)&sc->sc_sreg;
386
387 if (sc->sc_retries++ < 3) { /* Allow 3 retries */
388 hdcstart(sc, bp);
389 return;
390 }
391 printf("%s: failed, status 0x%x\n",
392 sc->sc_dev.dv_xname, sc->sc_status);
393 hdc_readregs(sc);
394 for (i = 0; i < 10; i++)
395 printf("%i: %x\n", i, g[i]);
396 bp->b_flags |= B_ERROR;
397 bp->b_error = ENXIO;
398 bp->b_resid = bp->b_bcount;
399 biodone(bp);
400 vsbus_dma_intr();
401 return;
402 }
403
404 if (bp->b_flags & B_READ) {
405 vsbus_copytoproc(bp->b_proc, sc->sc_dmabase, sc->sc_bufaddr,
406 sc->sc_xfer);
407 }
408 sc->sc_diskblk += (sc->sc_xfer/DEV_BSIZE);
409 sc->sc_bytecnt -= sc->sc_xfer;
410 sc->sc_bufaddr += sc->sc_xfer;
411
412 if (sc->sc_bytecnt == 0) { /* Finished transfer */
413 biodone(bp);
414 vsbus_dma_intr();
415 } else
416 hdcstart(sc, bp);
417 }
418
419 /*
420 *
421 */
422 void
423 rdstrategy(struct buf *bp)
424 {
425 struct rdsoftc *rd;
426 struct hdcsoftc *sc;
427 struct disklabel *lp;
428 int unit, s;
429
430 unit = DISKUNIT(bp->b_dev);
431 if (unit > rd_cd.cd_ndevs || (rd = rd_cd.cd_devs[unit]) == NULL) {
432 bp->b_error = ENXIO;
433 bp->b_flags |= B_ERROR;
434 goto done;
435 }
436 sc = (void *)rd->sc_dev.dv_parent;
437
438 lp = rd->sc_disk.dk_label;
439 if ((bounds_check_with_label(bp, lp, 1)) <= 0)
440 goto done;
441
442 if (bp->b_bcount == 0)
443 goto done;
444
445 bp->b_rawblkno =
446 bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
447 bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl;
448
449 s = splbio();
450 BUFQ_PUT(&sc->sc_q, bp);
451 if (inq == 0) {
452 inq = 1;
453 vsbus_dma_start(&sc->sc_vd);
454 }
455 splx(s);
456 return;
457
458 done: biodone(bp);
459 }
460
461 void
462 hdc_qstart(void *arg)
463 {
464 struct hdcsoftc *sc = arg;
465
466 inq = 0;
467
468 hdcstart(sc, 0);
469 if (BUFQ_PEEK(&sc->sc_q)) {
470 vsbus_dma_start(&sc->sc_vd); /* More to go */
471 inq = 1;
472 }
473 }
474
475 void
476 hdcstart(struct hdcsoftc *sc, struct buf *ob)
477 {
478 struct hdc9224_UDCreg *p = &sc->sc_creg;
479 struct disklabel *lp;
480 struct rdsoftc *rd;
481 struct buf *bp;
482 int cn, sn, tn, bn, blks;
483 volatile char ch;
484
485 if (sc->sc_active)
486 return; /* Already doing something */
487
488
489 if (ob == 0) {
490 bp = BUFQ_GET(&sc->sc_q);
491 if (bp == NULL)
492 return; /* Nothing to do */
493 sc->sc_bufaddr = bp->b_data;
494 sc->sc_diskblk = bp->b_rawblkno;
495 sc->sc_bytecnt = bp->b_bcount;
496 sc->sc_retries = 0;
497 bp->b_resid = 0;
498 } else
499 bp = ob;
500
501 rd = rd_cd.cd_devs[DISKUNIT(bp->b_dev)];
502 hdc_rdselect(sc, rd->sc_drive);
503 sc->sc_active = bp;
504
505 bn = sc->sc_diskblk;
506 lp = rd->sc_disk.dk_label;
507 if (bn) {
508 cn = bn / lp->d_secpercyl;
509 sn = bn % lp->d_secpercyl;
510 tn = sn / lp->d_nsectors;
511 sn = sn % lp->d_nsectors;
512 } else
513 cn = sn = tn = 0;
514
515 cn++; /* first cylinder is reserved */
516
517 bzero(p, sizeof(struct hdc9224_UDCreg));
518
519 /*
520 * Tricky thing: the controller do itself only increase the sector
521 * number, not the track or cylinder number. Therefore the driver
522 * is not allowed to have transfers that crosses track boundaries.
523 */
524 blks = sc->sc_bytecnt/DEV_BSIZE;
525 if ((sn + blks) > lp->d_nsectors)
526 blks = lp->d_nsectors - sn;
527
528 p->udc_dsect = sn;
529 p->udc_dcyl = cn & 0xff;
530 p->udc_dhead = ((cn >> 4) & 0x70) | tn;
531 p->udc_scnt = blks;
532
533 p->udc_rtcnt = UDC_RC_RTRYCNT;
534 p->udc_mode = UDC_MD_HDD;
535 p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWRFLT;
536 hdc_writeregs(sc);
537
538 /* Count up vars */
539 sc->sc_xfer = blks * DEV_BSIZE;
540
541 ch = HDC_RSTAT; /* Avoid pending interrupts */
542 WAIT;
543 vsbus_clrintr(sc->sc_intbit); /* Clear pending int's */
544
545 if (bp->b_flags & B_READ) {
546 HDC_WCMD(DKC_CMD_READ_HDD);
547 } else {
548 vsbus_copyfromproc(bp->b_proc, sc->sc_bufaddr, sc->sc_dmabase,
549 sc->sc_xfer);
550 HDC_WCMD(DKC_CMD_WRITE_HDD);
551 }
552 }
553
554 void
555 rd_readgeom(struct hdcsoftc *sc, struct rdsoftc *rd)
556 {
557 struct hdc9224_UDCreg *p = &sc->sc_creg;
558
559 hdc_rdselect(sc, rd->sc_drive); /* select drive right now */
560
561 bzero(p, sizeof(struct hdc9224_UDCreg));
562
563 p->udc_scnt = 1;
564 p->udc_rtcnt = UDC_RC_RTRYCNT;
565 p->udc_mode = UDC_MD_HDD;
566 p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWPROT;
567 hdc_writeregs(sc);
568 sc->sc_status = 0;
569 HDC_WCMD(DKC_CMD_READ_HDD|2);
570 while ((sc->sc_status & DKC_ST_INTPEND) == 0)
571 ;
572 bcopy(sc->sc_dmabase, &rd->sc_xbn, sizeof(struct rdgeom));
573 }
574
575 #ifdef RDDEBUG
576 /*
577 * display the contents of the on-disk geometry structure
578 */
579 void
580 hdc_printgeom(p)
581 struct rdgeom *p;
582 {
583 printf ("**DiskData** XBNs: %ld, DBNs: %ld, LBNs: %ld, RBNs: %ld\n",
584 p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
585 printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
586 p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
587 printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
588 p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
589 printf ("media-ID: %lx, interleave: %d, headskew: %d, cylskew: %d\n",
590 p->media_id, p->interleave, p->headskew, p->cylskew);
591 printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
592 p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
593 p->sync_value);
594 }
595 #endif
596
597 /*
598 * Return the size of a partition, if known, or -1 if not.
599 */
600 int
601 rdsize(dev_t dev)
602 {
603 struct rdsoftc *rd;
604 int unit = DISKUNIT(dev);
605 int size;
606
607 if (unit >= rd_cd.cd_ndevs || rd_cd.cd_devs[unit] == 0)
608 return -1;
609 rd = rd_cd.cd_devs[unit];
610 size = rd->sc_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
611 (rd->sc_disk.dk_label->d_secsize / DEV_BSIZE);
612
613 return (size);
614 }
615
616 /*
617 *
618 */
619 int
620 rdopen(dev_t dev, int flag, int fmt, struct proc *p)
621 {
622 struct rdsoftc *rd;
623 int unit, part;
624
625 unit = DISKUNIT(dev);
626 if (unit >= rd_cd.cd_ndevs)
627 return ENXIO;
628 rd = rd_cd.cd_devs[unit];
629 if (rd == 0)
630 return ENXIO;
631
632 part = DISKPART(dev);
633 if (part >= rd->sc_disk.dk_label->d_npartitions)
634 return ENXIO;
635
636 switch (fmt) {
637 case S_IFCHR:
638 rd->sc_disk.dk_copenmask |= (1 << part);
639 break;
640 case S_IFBLK:
641 rd->sc_disk.dk_bopenmask |= (1 << part);
642 break;
643 }
644 rd->sc_disk.dk_openmask =
645 rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
646
647 return 0;
648 }
649
650 /*
651 *
652 */
653 int
654 rdclose(dev_t dev, int flag, int fmt, struct proc *p)
655 {
656 struct rdsoftc *rd;
657 int part;
658
659 rd = rd_cd.cd_devs[DISKUNIT(dev)];
660 part = DISKPART(dev);
661
662 switch (fmt) {
663 case S_IFCHR:
664 rd->sc_disk.dk_copenmask &= ~(1 << part);
665 break;
666 case S_IFBLK:
667 rd->sc_disk.dk_bopenmask &= ~(1 << part);
668 break;
669 }
670 rd->sc_disk.dk_openmask =
671 rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
672
673 return (0);
674 }
675
676 /*
677 *
678 */
679 int
680 rdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
681 {
682 struct rdsoftc *rd = rd_cd.cd_devs[DISKUNIT(dev)];
683 struct disklabel *lp = rd->sc_disk.dk_label;
684 int err = 0;
685
686 switch (cmd) {
687 case DIOCGDINFO:
688 bcopy(lp, addr, sizeof (struct disklabel));
689 break;
690
691 case DIOCGPART:
692 ((struct partinfo *)addr)->disklab = lp;
693 ((struct partinfo *)addr)->part =
694 &lp->d_partitions[DISKPART(dev)];
695 break;
696
697 case DIOCWDINFO:
698 case DIOCSDINFO:
699 if ((flag & FWRITE) == 0)
700 return EBADF;
701 else
702 err = (cmd == DIOCSDINFO ?
703 setdisklabel(lp, (struct disklabel *)addr, 0, 0) :
704 writedisklabel(dev, rdstrategy, lp, 0));
705 break;
706
707 case DIOCGDEFLABEL:
708 bzero(lp, sizeof(struct disklabel));
709 rdmakelabel(lp, &rd->sc_xbn);
710 break;
711
712 case DIOCWLABEL:
713 if ((flag & FWRITE) == 0)
714 err = EBADF;
715 break;
716
717 default:
718 err = ENOTTY;
719 }
720 return err;
721 }
722
723 /*
724 *
725 */
726 int
727 rdread(dev_t dev, struct uio *uio, int flag)
728 {
729 return (physio (rdstrategy, NULL, dev, B_READ, minphys, uio));
730 }
731
732 /*
733 *
734 */
735 int
736 rdwrite(dev_t dev, struct uio *uio, int flag)
737 {
738 return (physio (rdstrategy, NULL, dev, B_WRITE, minphys, uio));
739 }
740
741 /*
742 *
743 */
744 int
745 rddump(dev_t dev, daddr_t daddr, caddr_t addr, size_t size)
746 {
747 return 0;
748 }
749
750 /*
751 * we have to wait 0.7 usec between two accesses to any of the
752 * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
753 * instruction. Thus the loop-overhead will be enough...
754 */
755 static void
756 hdc_readregs(struct hdcsoftc *sc)
757 {
758 int i;
759 char *p;
760
761 HDC_WCMD(DKC_CMD_SETREGPTR);
762 WAIT;
763 p = (void*)&sc->sc_sreg;
764 for (i=0; i<10; i++) {
765 *p++ = HDC_RREG; /* dkc_reg auto-increments */
766 WAIT;
767 }
768 }
769
770 static void
771 hdc_writeregs(struct hdcsoftc *sc)
772 {
773 int i;
774 char *p;
775
776 HDC_WCMD(DKC_CMD_SETREGPTR);
777 p = (void*)&sc->sc_creg;
778 for (i=0; i<10; i++) {
779 HDC_WREG(*p++); /* dkc_reg auto-increments */
780 WAIT;
781 }
782 }
783
784 /*
785 * hdc_command() issues a command and polls the intreq-register
786 * to find when command has completed
787 */
788 int
789 hdc_command(struct hdcsoftc *sc, int cmd)
790 {
791 hdc_writeregs(sc); /* write the prepared registers */
792 HDC_WCMD(cmd);
793 WAIT;
794 return (0);
795 }
796
797 int
798 hdc_rdselect(struct hdcsoftc *sc, int unit)
799 {
800 struct hdc9224_UDCreg *p = &sc->sc_creg;
801 int error;
802
803 /*
804 * bring "creg" in some known-to-work state and
805 * select the drive with the DRIVE SELECT command.
806 */
807 bzero(p, sizeof(struct hdc9224_UDCreg));
808
809 p->udc_rtcnt = UDC_RC_HDD_READ;
810 p->udc_mode = UDC_MD_HDD;
811 p->udc_term = UDC_TC_HDD;
812
813 error = hdc_command(sc, DKC_CMD_DRSEL_HDD | unit);
814
815 return (error);
816 }
817
818 void
819 rdmakelabel(struct disklabel *dl, struct rdgeom *g)
820 {
821 int n, p = 0;
822
823 dl->d_bbsize = BBSIZE;
824 dl->d_sbsize = SBSIZE;
825 dl->d_typename[p++] = MSCP_MID_CHAR(2, g->media_id);
826 dl->d_typename[p++] = MSCP_MID_CHAR(1, g->media_id);
827 if (MSCP_MID_ECH(0, g->media_id))
828 dl->d_typename[p++] = MSCP_MID_CHAR(0, g->media_id);
829 n = MSCP_MID_NUM(g->media_id);
830 if (n > 99) {
831 dl->d_typename[p++] = '1';
832 n -= 100;
833 }
834 if (n > 9) {
835 dl->d_typename[p++] = (n / 10) + '0';
836 n %= 10;
837 }
838 dl->d_typename[p++] = n + '0';
839 dl->d_typename[p] = 0;
840 dl->d_type = DTYPE_MSCP; /* XXX - what to use here??? */
841 dl->d_rpm = 3600;
842 dl->d_secsize = DEV_BSIZE;
843
844 dl->d_secperunit = g->lbn_count;
845 dl->d_nsectors = g->nspt;
846 dl->d_ntracks = g->ntracks;
847 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
848 dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
849
850 dl->d_npartitions = MAXPARTITIONS;
851 dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
852 dl->d_secperunit;
853 dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
854 dl->d_interleave = dl->d_headswitch = 1;
855 dl->d_magic = dl->d_magic2 = DISKMAGIC;
856 dl->d_checksum = dkcksum(dl);
857 }
858