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