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