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