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