1 /* $NetBSD: hdc9224.c,v 1.62 2021/08/07 16:19:07 thorpej 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.62 2021/08/07 16:19:07 thorpej 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 /*
228 * new-config's hdcmatch() is similar to old-config's hdcprobe(),
229 * thus we probe for the existence of the controller and reset it.
230 * NB: we can't initialize the controller yet, since space for hdcsoftc
231 * is not yet allocated. Thus we do this in hdcattach()...
232 */
233 int
hdcmatch(device_t parent,cfdata_t cf,void * aux)234 hdcmatch(device_t parent, cfdata_t cf, void *aux)
235 {
236 struct vsbus_attach_args * const va = aux;
237 volatile char * const hdc_csr = (volatile char *)va->va_addr;
238 int i;
239
240 u = 8; /* !!! - GCC */
241
242 if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_46
243 || vax_boardtype == VAX_BTYP_48 || vax_boardtype == VAX_BTYP_53)
244 return 0;
245
246 hdc_csr[4] = DKC_CMD_RESET; /* reset chip */
247 for (i = 0; i < 1000; i++) {
248 DELAY(1000);
249 if (hdc_csr[4] & DKC_ST_DONE)
250 break;
251 }
252 if (i == 100)
253 return 0; /* No response to reset */
254
255 hdc_csr[4] = DKC_CMD_SETREGPTR|UDC_TERM;
256 WAIT;
257 hdc_csr[0] = UDC_TC_CRCPRE|UDC_TC_INTDONE;
258 WAIT;
259 hdc_csr[4] = DKC_CMD_DRDESELECT; /* Should be harmless */
260 DELAY(1000);
261 return (1);
262 }
263
264 int
hdcprint(void * aux,const char * name)265 hdcprint(void *aux, const char *name)
266 {
267 struct hdc_attach_args * const ha = aux;
268
269 if (name)
270 aprint_normal ("RD?? at %s drive %d", name, ha->ha_drive);
271 return UNCONF;
272 }
273
274 /*
275 * hdc_attach() probes for all possible devices
276 */
277 void
hdcattach(device_t parent,device_t self,void * aux)278 hdcattach(device_t parent, device_t self, void *aux)
279 {
280 struct vsbus_attach_args * const va = aux;
281 struct hdcsoftc * const sc = device_private(self);
282 struct hdc_attach_args ha;
283 int status, i;
284
285 aprint_normal("\n");
286
287 sc->sc_dev = self;
288
289 /*
290 * Get interrupt vector, enable instrumentation.
291 */
292 scb_vecalloc(va->va_cvec, hdcintr, sc, SCB_ISTACK, &sc->sc_intrcnt);
293 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
294 device_xname(self), "intr");
295
296 sc->sc_regs = vax_map_physmem(va->va_paddr, 1);
297 sc->sc_dmabase = (void *)va->va_dmaaddr;
298 sc->sc_dmasize = va->va_dmasize;
299 sc->sc_intbit = va->va_maskno;
300 rd_dmasize = uimin(MAXPHYS, sc->sc_dmasize); /* Used in rd_minphys */
301
302 sc->sc_vd.vd_go = hdc_qstart;
303 sc->sc_vd.vd_arg = sc;
304 /*
305 * Reset controller.
306 */
307 HDC_WCMD(DKC_CMD_RESET);
308 DELAY(1000);
309 status = HDC_RSTAT;
310 if (status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
311 aprint_error_dev(self, "RESET failed, status 0x%x\n", status);
312 return;
313 }
314 bufq_alloc(&sc->sc_q, "disksort", BUFQ_SORT_CYLINDER);
315
316 /*
317 * now probe for all possible hard drives
318 */
319 for (i = 0; i < 4; i++) {
320 if (i == 2) /* Floppy, needs special handling */
321 continue;
322 HDC_WCMD(DKC_CMD_DRSELECT | i);
323 DELAY(1000);
324 status = HDC_RSTAT;
325 ha.ha_drive = i;
326 if ((status & DKC_ST_TERMCOD) == DKC_TC_SUCCESS)
327 config_found(self, (void *)&ha, hdcprint, CFARGS_NONE);
328 }
329 }
330
331 /*
332 * rdmatch() probes for the existence of a RD-type disk/floppy
333 */
334 int
rdmatch(device_t parent,cfdata_t cf,void * aux)335 rdmatch(device_t parent, cfdata_t cf, void *aux)
336 {
337 struct hdc_attach_args * const ha = aux;
338
339 if (cf->cf_loc[HDCCF_DRIVE] != HDCCF_DRIVE_DEFAULT &&
340 cf->cf_loc[HDCCF_DRIVE] != ha->ha_drive)
341 return 0;
342
343 if (ha->ha_drive == 2) /* Always floppy, not supported */
344 return 0;
345
346 return 1;
347 }
348
349 void
rdattach(device_t parent,device_t self,void * aux)350 rdattach(device_t parent, device_t self, void *aux)
351 {
352 struct hdcsoftc * const sc = device_private(parent);
353 struct rdsoftc * const rd = device_private(self);
354 struct hdc_attach_args * const ha = aux;
355 struct disklabel *dl;
356 const char *msg;
357
358 rd->sc_dev = self;
359 rd->sc_drive = ha->ha_drive;
360 rd->sc_hdc = sc;
361 /*
362 * Initialize and attach the disk structure.
363 */
364 disk_init(&rd->sc_disk, device_xname(rd->sc_dev), NULL);
365 disk_attach(&rd->sc_disk);
366
367 /*
368 * if it's not a floppy then evaluate the on-disk geometry.
369 * if necessary correct the label...
370 */
371 rd_readgeom(sc, rd);
372 disk_printtype(rd->sc_drive, rd->sc_xbn.media_id);
373 dl = rd->sc_disk.dk_label;
374 rdmakelabel(dl, &rd->sc_xbn);
375 msg = readdisklabel(MAKEDISKDEV(cdevsw_lookup_major(&rd_cdevsw),
376 device_unit(rd->sc_dev), RAW_PART),
377 rdstrategy, dl, NULL);
378 if (msg)
379 aprint_normal_dev(self, "%s: size %u sectors",
380 msg, dl->d_secperunit);
381 else
382 aprint_normal_dev(self, "size %u sectors\n", dl->d_secperunit);
383 #ifdef RDDEBUG
384 hdc_printgeom(&rd->sc_xbn);
385 #endif
386 }
387
388 void
hdcintr(void * arg)389 hdcintr(void *arg)
390 {
391 struct hdcsoftc * const sc = arg;
392 struct buf *bp;
393
394 sc->sc_status = HDC_RSTAT;
395 if (sc->sc_active == 0)
396 return; /* Complain? */
397
398 if ((sc->sc_status & (DKC_ST_INTPEND|DKC_ST_DONE)) !=
399 (DKC_ST_INTPEND|DKC_ST_DONE))
400 return; /* Why spurious ints sometimes??? */
401
402 bp = sc->sc_active;
403 sc->sc_active = 0;
404 if ((sc->sc_status & DKC_ST_TERMCOD) != DKC_TC_SUCCESS) {
405 int i;
406 u_char *g = (u_char *)&sc->sc_sreg;
407
408 if (sc->sc_retries++ < 3) { /* Allow 3 retries */
409 hdcstart(sc, bp);
410 return;
411 }
412 aprint_error_dev(sc->sc_dev, "failed, status 0x%x\n",
413 sc->sc_status);
414 hdc_readregs(sc);
415 for (i = 0; i < 10; i++)
416 aprint_error("%i: %x\n", i, g[i]);
417 bp->b_error = ENXIO;
418 bp->b_resid = bp->b_bcount;
419 biodone(bp);
420 vsbus_dma_intr();
421 return;
422 }
423
424 if (bp->b_flags & B_READ) {
425 vsbus_copytoproc(bp->b_proc, sc->sc_dmabase, sc->sc_bufaddr,
426 sc->sc_xfer);
427 }
428 sc->sc_diskblk += (sc->sc_xfer/DEV_BSIZE);
429 sc->sc_bytecnt -= sc->sc_xfer;
430 sc->sc_bufaddr = (char *)sc->sc_bufaddr + sc->sc_xfer;
431
432 if (sc->sc_bytecnt == 0) { /* Finished transfer */
433 biodone(bp);
434 vsbus_dma_intr();
435 } else
436 hdcstart(sc, bp);
437 }
438
439 /*
440 *
441 */
442 void
rdstrategy(struct buf * bp)443 rdstrategy(struct buf *bp)
444 {
445 struct rdsoftc *rd;
446 struct hdcsoftc *sc;
447 struct disklabel *lp;
448 int s;
449
450 if ((rd = device_lookup_private(&rd_cd, DISKUNIT(bp->b_dev))) == NULL) {
451 bp->b_error = ENXIO;
452 goto done;
453 }
454 sc = rd->sc_hdc;
455
456 lp = rd->sc_disk.dk_label;
457 if ((bounds_check_with_label(&rd->sc_disk, bp, 1)) <= 0)
458 goto done;
459
460 if (bp->b_bcount == 0)
461 goto done;
462
463 bp->b_rawblkno =
464 bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
465 bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl;
466
467 s = splbio();
468 bufq_put(sc->sc_q, bp);
469 if (inq == 0) {
470 inq = 1;
471 vsbus_dma_start(&sc->sc_vd);
472 }
473 splx(s);
474 return;
475
476 done: biodone(bp);
477 }
478
479 void
hdc_qstart(void * arg)480 hdc_qstart(void *arg)
481 {
482 struct hdcsoftc * const sc = arg;
483
484 inq = 0;
485
486 hdcstart(sc, 0);
487 if (bufq_peek(sc->sc_q)) {
488 vsbus_dma_start(&sc->sc_vd); /* More to go */
489 inq = 1;
490 }
491 }
492
493 void
hdcstart(struct hdcsoftc * sc,struct buf * ob)494 hdcstart(struct hdcsoftc *sc, struct buf *ob)
495 {
496 struct hdc9224_UDCreg * const p = &sc->sc_creg;
497 struct disklabel *lp;
498 struct rdsoftc *rd;
499 struct buf *bp;
500 int cn, sn, tn, bn, blks;
501
502 if (sc->sc_active)
503 return; /* Already doing something */
504
505 if (ob == 0) {
506 bp = bufq_get(sc->sc_q);
507 if (bp == NULL)
508 return; /* Nothing to do */
509 sc->sc_bufaddr = bp->b_data;
510 sc->sc_diskblk = bp->b_rawblkno;
511 sc->sc_bytecnt = bp->b_bcount;
512 sc->sc_retries = 0;
513 bp->b_resid = 0;
514 } else
515 bp = ob;
516
517 rd = device_lookup_private(&rd_cd, DISKUNIT(bp->b_dev));
518 hdc_rdselect(sc, rd->sc_drive);
519 sc->sc_active = bp;
520
521 bn = sc->sc_diskblk;
522 lp = rd->sc_disk.dk_label;
523 if (bn) {
524 cn = bn / lp->d_secpercyl;
525 sn = bn % lp->d_secpercyl;
526 tn = sn / lp->d_nsectors;
527 sn = sn % lp->d_nsectors;
528 } else
529 cn = sn = tn = 0;
530
531 cn++; /* first cylinder is reserved */
532
533 memset(p, 0, sizeof(struct hdc9224_UDCreg));
534
535 /*
536 * Tricky thing: the controller do itself only increase the sector
537 * number, not the track or cylinder number. Therefore the driver
538 * is not allowed to have transfers that crosses track boundaries.
539 */
540 blks = sc->sc_bytecnt/DEV_BSIZE;
541 if ((sn + blks) > lp->d_nsectors)
542 blks = lp->d_nsectors - sn;
543
544 p->udc_dsect = sn;
545 p->udc_dcyl = cn & 0xff;
546 p->udc_dhead = ((cn >> 4) & 0x70) | tn;
547 p->udc_scnt = blks;
548
549 p->udc_rtcnt = UDC_RC_RTRYCNT;
550 p->udc_mode = UDC_MD_HDD;
551 p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWRFLT;
552 hdc_writeregs(sc);
553
554 /* Count up vars */
555 sc->sc_xfer = blks * DEV_BSIZE;
556
557 (void)HDC_RSTAT; /* Avoid pending interrupts */
558 WAIT;
559 vsbus_clrintr(sc->sc_intbit); /* Clear pending int's */
560
561 if (bp->b_flags & B_READ) {
562 HDC_WCMD(DKC_CMD_READ_HDD);
563 } else {
564 vsbus_copyfromproc(bp->b_proc, sc->sc_bufaddr, sc->sc_dmabase,
565 sc->sc_xfer);
566 HDC_WCMD(DKC_CMD_WRITE_HDD);
567 }
568 }
569
570 void
rd_readgeom(struct hdcsoftc * sc,struct rdsoftc * rd)571 rd_readgeom(struct hdcsoftc *sc, struct rdsoftc *rd)
572 {
573 struct hdc9224_UDCreg * const p = &sc->sc_creg;
574
575 hdc_rdselect(sc, rd->sc_drive); /* select drive right now */
576
577 memset(p, 0, sizeof(*p));
578
579 p->udc_scnt = 1;
580 p->udc_rtcnt = UDC_RC_RTRYCNT;
581 p->udc_mode = UDC_MD_HDD;
582 p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWPROT;
583 hdc_writeregs(sc);
584 sc->sc_status = 0;
585 HDC_WCMD(DKC_CMD_READ_HDD|2);
586 while ((sc->sc_status & DKC_ST_INTPEND) == 0)
587 ;
588 memcpy(&rd->sc_xbn, sc->sc_dmabase, sizeof(struct rdgeom));
589 }
590
591 #ifdef RDDEBUG
592 /*
593 * display the contents of the on-disk geometry structure
594 */
595 void
hdc_printgeom(struct rdgeom * p)596 hdc_printgeom(struct rdgeom *p)
597 {
598 printf ("**DiskData** XBNs: %ld, DBNs: %ld, LBNs: %ld, RBNs: %ld\n",
599 p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
600 printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
601 p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
602 printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
603 p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
604 printf ("media-ID: %lx, interleave: %d, headskew: %d, cylskew: %d\n",
605 p->media_id, p->interleave, p->headskew, p->cylskew);
606 printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
607 p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
608 p->sync_value);
609 }
610 #endif
611
612 /*
613 * Return the size of a partition, if known, or -1 if not.
614 */
615 int
rdpsize(dev_t dev)616 rdpsize(dev_t dev)
617 {
618 struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
619 const int part = DISKPART(dev);
620
621 if (rd == NULL || part >= rd->sc_disk.dk_label->d_npartitions)
622 return -1;
623
624 return rd->sc_disk.dk_label->d_partitions[part].p_size *
625 (rd->sc_disk.dk_label->d_secsize / DEV_BSIZE);
626 }
627
628 /*
629 *
630 */
631 int
rdopen(dev_t dev,int flag,int fmt,struct lwp * l)632 rdopen(dev_t dev, int flag, int fmt, struct lwp *l)
633 {
634 struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
635 const int part = DISKPART(dev);
636
637 if (rd == NULL || part >= rd->sc_disk.dk_label->d_npartitions)
638 return ENXIO;
639
640 switch (fmt) {
641 case S_IFCHR:
642 rd->sc_disk.dk_copenmask |= (1 << part);
643 break;
644 case S_IFBLK:
645 rd->sc_disk.dk_bopenmask |= (1 << part);
646 break;
647 }
648 rd->sc_disk.dk_openmask =
649 rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
650
651 return 0;
652 }
653
654 /*
655 *
656 */
657 int
rdclose(dev_t dev,int flag,int fmt,struct lwp * l)658 rdclose(dev_t dev, int flag, int fmt, struct lwp *l)
659 {
660 struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
661 const int part = DISKPART(dev);
662
663 switch (fmt) {
664 case S_IFCHR:
665 rd->sc_disk.dk_copenmask &= ~(1 << part);
666 break;
667 case S_IFBLK:
668 rd->sc_disk.dk_bopenmask &= ~(1 << part);
669 break;
670 }
671 rd->sc_disk.dk_openmask =
672 rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
673
674 return (0);
675 }
676
677 /*
678 *
679 */
680 int
rdioctl(dev_t dev,u_long cmd,void * addr,int flag,struct lwp * l)681 rdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
682 {
683 struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
684 struct disklabel * const lp = rd->sc_disk.dk_label;
685 int error;
686
687 error = disk_ioctl(&rd->sc_disk, dev, cmd, addr, flag, l);
688 if (error != EPASSTHROUGH)
689 return error;
690 else
691 error = 0;
692
693 switch (cmd) {
694 case DIOCWDINFO:
695 case DIOCSDINFO:
696 if ((flag & FWRITE) == 0)
697 return EBADF;
698 error = (cmd == DIOCSDINFO ?
699 setdisklabel(lp, (struct disklabel *)addr, 0, 0) :
700 writedisklabel(dev, rdstrategy, lp, 0));
701 break;
702
703 case DIOCGDEFLABEL:
704 memset(lp, 0, sizeof(*lp));
705 rdmakelabel(lp, &rd->sc_xbn);
706 break;
707
708 case DIOCWLABEL:
709 if ((flag & FWRITE) == 0)
710 error = EBADF;
711 break;
712
713 default:
714 error = ENOTTY;
715 }
716 return error;
717 }
718
719 /*
720 *
721 */
722 int
rdread(dev_t dev,struct uio * uio,int flag)723 rdread(dev_t dev, struct uio *uio, int flag)
724 {
725 return (physio (rdstrategy, NULL, dev, B_READ, minphys, uio));
726 }
727
728 /*
729 *
730 */
731 int
rdwrite(dev_t dev,struct uio * uio,int flag)732 rdwrite(dev_t dev, struct uio *uio, int flag)
733 {
734 return (physio (rdstrategy, NULL, dev, B_WRITE, minphys, uio));
735 }
736
737 /*
738 * we have to wait 0.7 usec between two accesses to any of the
739 * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
740 * instruction. Thus the loop-overhead will be enough...
741 */
742 static void
hdc_readregs(struct hdcsoftc * sc)743 hdc_readregs(struct hdcsoftc *sc)
744 {
745 int i;
746 char *p;
747
748 HDC_WCMD(DKC_CMD_SETREGPTR);
749 WAIT;
750 p = (void*)&sc->sc_sreg;
751 for (i=0; i<10; i++) {
752 *p++ = HDC_RREG; /* dkc_reg auto-increments */
753 WAIT;
754 }
755 }
756
757 static void
hdc_writeregs(struct hdcsoftc * sc)758 hdc_writeregs(struct hdcsoftc *sc)
759 {
760 int i;
761 char *p;
762
763 HDC_WCMD(DKC_CMD_SETREGPTR);
764 p = (void*)&sc->sc_creg;
765 for (i=0; i<10; i++) {
766 HDC_WREG(*p++); /* dkc_reg auto-increments */
767 WAIT;
768 }
769 }
770
771 /*
772 * hdc_command() issues a command and polls the intreq-register
773 * to find when command has completed
774 */
775 int
hdc_command(struct hdcsoftc * sc,int cmd)776 hdc_command(struct hdcsoftc *sc, int cmd)
777 {
778 hdc_writeregs(sc); /* write the prepared registers */
779 HDC_WCMD(cmd);
780 WAIT;
781 return (0);
782 }
783
784 int
hdc_rdselect(struct hdcsoftc * sc,int unit)785 hdc_rdselect(struct hdcsoftc *sc, int unit)
786 {
787 struct hdc9224_UDCreg * const p = &sc->sc_creg;
788 int error;
789
790 /*
791 * bring "creg" in some known-to-work state and
792 * select the drive with the DRIVE SELECT command.
793 */
794 memset(p, 0, sizeof(*p));
795
796 p->udc_rtcnt = UDC_RC_HDD_READ;
797 p->udc_mode = UDC_MD_HDD;
798 p->udc_term = UDC_TC_HDD;
799
800 error = hdc_command(sc, DKC_CMD_DRSEL_HDD | unit);
801
802 return error;
803 }
804
805 void
rdmakelabel(struct disklabel * dl,struct rdgeom * g)806 rdmakelabel(struct disklabel *dl, struct rdgeom *g)
807 {
808 int n, p = 0;
809
810 dl->d_bbsize = BBSIZE;
811 dl->d_sbsize = SBLOCKSIZE;
812 dl->d_typename[p++] = MSCP_MID_CHAR(2, g->media_id);
813 dl->d_typename[p++] = MSCP_MID_CHAR(1, g->media_id);
814 if (MSCP_MID_ECH(0, g->media_id))
815 dl->d_typename[p++] = MSCP_MID_CHAR(0, g->media_id);
816 n = MSCP_MID_NUM(g->media_id);
817 if (n > 99) {
818 dl->d_typename[p++] = '1';
819 n -= 100;
820 }
821 if (n > 9) {
822 dl->d_typename[p++] = (n / 10) + '0';
823 n %= 10;
824 }
825 dl->d_typename[p++] = n + '0';
826 dl->d_typename[p] = 0;
827 dl->d_type = DKTYPE_MSCP; /* XXX - what to use here??? */
828 dl->d_rpm = 3600;
829 dl->d_secsize = DEV_BSIZE;
830
831 dl->d_secperunit = g->lbn_count;
832 dl->d_nsectors = g->nspt;
833 dl->d_ntracks = g->ntracks;
834 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
835 dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
836
837 dl->d_npartitions = MAXPARTITIONS;
838 dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
839 dl->d_secperunit;
840 dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
841 dl->d_interleave = dl->d_headswitch = 1;
842 dl->d_magic = dl->d_magic2 = DISKMAGIC;
843 dl->d_checksum = dkcksum(dl);
844 }
845