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