wd.c revision 1.149 1 1.149 christos /* $NetBSD: wd.c,v 1.149 1996/04/29 19:50:47 christos Exp $ */
2 1.100 cgd
3 1.11 deraadt /*
4 1.135 mycroft * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
5 1.119 mycroft *
6 1.119 mycroft * DMA and multi-sector PIO handling are derived from code contributed by
7 1.119 mycroft * Onno van der Linden.
8 1.113 mycroft *
9 1.113 mycroft * Redistribution and use in source and binary forms, with or without
10 1.113 mycroft * modification, are permitted provided that the following conditions
11 1.113 mycroft * are met:
12 1.113 mycroft * 1. Redistributions of source code must retain the above copyright
13 1.113 mycroft * notice, this list of conditions and the following disclaimer.
14 1.113 mycroft * 2. Redistributions in binary form must reproduce the above copyright
15 1.113 mycroft * notice, this list of conditions and the following disclaimer in the
16 1.113 mycroft * documentation and/or other materials provided with the distribution.
17 1.113 mycroft * 3. All advertising materials mentioning features or use of this software
18 1.113 mycroft * must display the following acknowledgement:
19 1.135 mycroft * This product includes software developed by Charles M. Hannum.
20 1.113 mycroft * 4. The name of the author may not be used to endorse or promote products
21 1.113 mycroft * derived from this software without specific prior written permission.
22 1.113 mycroft *
23 1.135 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.135 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.135 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.135 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.135 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.135 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.135 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.135 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.135 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.135 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.29 mycroft #include <sys/param.h>
36 1.29 mycroft #include <sys/systm.h>
37 1.43 mycroft #include <sys/kernel.h>
38 1.29 mycroft #include <sys/conf.h>
39 1.29 mycroft #include <sys/file.h>
40 1.29 mycroft #include <sys/stat.h>
41 1.29 mycroft #include <sys/ioctl.h>
42 1.29 mycroft #include <sys/buf.h>
43 1.29 mycroft #include <sys/uio.h>
44 1.29 mycroft #include <sys/malloc.h>
45 1.64 mycroft #include <sys/device.h>
46 1.94 mycroft #include <sys/disklabel.h>
47 1.94 mycroft #include <sys/disk.h>
48 1.29 mycroft #include <sys/syslog.h>
49 1.149 christos #include <sys/proc.h>
50 1.29 mycroft
51 1.29 mycroft #include <vm/vm.h>
52 1.29 mycroft
53 1.29 mycroft #include <machine/cpu.h>
54 1.29 mycroft #include <machine/pio.h>
55 1.29 mycroft
56 1.139 cgd #include <dev/isa/isavar.h>
57 1.146 mycroft #include <dev/isa/isadmavar.h>
58 1.130 cgd #include <dev/isa/wdreg.h>
59 1.1 cgd
60 1.98 mycroft #define WAITTIME (4 * hz) /* time to wait for a completion */
61 1.98 mycroft #define RECOVERYTIME (hz / 2) /* time to recover from an error */
62 1.19 deraadt
63 1.135 mycroft #define WDCDELAY 100
64 1.135 mycroft #define WDCNDELAY 100000 /* delay = 100us; so 10s for a controller state change */
65 1.89 mycroft #if 0
66 1.64 mycroft /* If you enable this, it will report any delays more than 100us * N long. */
67 1.69 mycroft #define WDCNDELAY_DEBUG 10
68 1.51 mycroft #endif
69 1.23 deraadt
70 1.135 mycroft #define WDIORETRIES 5 /* number of retries before giving up */
71 1.1 cgd
72 1.92 cgd #define WDUNIT(dev) DISKUNIT(dev)
73 1.92 cgd #define WDPART(dev) DISKPART(dev)
74 1.92 cgd #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
75 1.92 cgd
76 1.92 cgd #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
77 1.1 cgd
78 1.64 mycroft struct wd_softc {
79 1.64 mycroft struct device sc_dev;
80 1.144 thorpej struct disk sc_dk;
81 1.9 deraadt
82 1.135 mycroft /* Information about the current transfer: */
83 1.126 mycroft daddr_t sc_blkno; /* starting block number */
84 1.126 mycroft int sc_bcount; /* byte count left */
85 1.126 mycroft int sc_skip; /* bytes already transferred */
86 1.126 mycroft int sc_nblks; /* number of blocks currently transferring */
87 1.129 mycroft int sc_nbytes; /* number of bytes currently transferring */
88 1.126 mycroft
89 1.135 mycroft /* Long-term state: */
90 1.135 mycroft int sc_drive; /* physical unit number */
91 1.135 mycroft int sc_state; /* control state */
92 1.113 mycroft #define RECAL 0 /* recalibrate */
93 1.113 mycroft #define RECAL_WAIT 1 /* done recalibrating */
94 1.113 mycroft #define GEOMETRY 2 /* upload geometry */
95 1.113 mycroft #define GEOMETRY_WAIT 3 /* done uploading geometry */
96 1.113 mycroft #define MULTIMODE 4 /* set multiple mode */
97 1.113 mycroft #define MULTIMODE_WAIT 5 /* done setting multiple mode */
98 1.113 mycroft #define OPEN 6 /* done with open */
99 1.135 mycroft int sc_mode; /* transfer mode */
100 1.113 mycroft #define WDM_PIOSINGLE 0 /* single-sector PIO */
101 1.113 mycroft #define WDM_PIOMULTI 1 /* multi-sector PIO */
102 1.113 mycroft #define WDM_DMA 2 /* DMA */
103 1.135 mycroft int sc_multiple; /* multiple for WDM_PIOMULTI */
104 1.135 mycroft int sc_flags; /* drive characteistics found */
105 1.108 mycroft #define WDF_LOCKED 0x01
106 1.108 mycroft #define WDF_WANTED 0x02
107 1.132 mycroft #define WDF_WLABEL 0x04 /* label is writable */
108 1.132 mycroft #define WDF_LABELLING 0x08 /* writing label */
109 1.135 mycroft /* XXX Nothing resets this yet, but disk change sensing will when ATAPI is
110 1.135 mycroft implemented. */
111 1.135 mycroft #define WDF_LOADED 0x10 /* parameters loaded */
112 1.132 mycroft #define WDF_32BIT 0x20 /* can do 32-bit transfer */
113 1.126 mycroft
114 1.135 mycroft struct wdparams sc_params; /* ESDI/ATA drive parameters */
115 1.126 mycroft daddr_t sc_badsect[127]; /* 126 plus trailing -1 marker */
116 1.126 mycroft
117 1.78 mycroft TAILQ_ENTRY(wd_softc) sc_drivechain;
118 1.64 mycroft struct buf sc_q;
119 1.74 mycroft };
120 1.64 mycroft
121 1.64 mycroft struct wdc_softc {
122 1.64 mycroft struct device sc_dev;
123 1.139 cgd void *sc_ih;
124 1.64 mycroft
125 1.135 mycroft int sc_iobase; /* I/O port base */
126 1.135 mycroft int sc_drq; /* DMA channel */
127 1.125 mycroft
128 1.126 mycroft TAILQ_HEAD(drivehead, wd_softc) sc_drives;
129 1.126 mycroft int sc_flags;
130 1.135 mycroft #define WDCF_ACTIVE 0x01 /* controller is active */
131 1.135 mycroft #define WDCF_SINGLE 0x02 /* sector at a time mode */
132 1.135 mycroft #define WDCF_ERROR 0x04 /* processing a disk error */
133 1.135 mycroft #define WDCF_WANTED 0x08 /* XXX locking for wd_get_parms() */
134 1.135 mycroft int sc_errors; /* errors during current transfer */
135 1.135 mycroft u_char sc_status; /* copy of status register */
136 1.135 mycroft u_char sc_error; /* copy of error register */
137 1.74 mycroft };
138 1.3 deraadt
139 1.149 christos int wdcprobe __P((struct device *, void *, void *));
140 1.149 christos void wdcattach __P((struct device *, struct device *, void *));
141 1.149 christos int wdcintr __P((void *));
142 1.3 deraadt
143 1.147 thorpej struct cfattach wdc_ca = {
144 1.147 thorpej sizeof(struct wdc_softc), wdcprobe, wdcattach
145 1.147 thorpej };
146 1.147 thorpej
147 1.147 thorpej struct cfdriver wdc_cd = {
148 1.147 thorpej NULL, "wdc", DV_DULL
149 1.1 cgd };
150 1.1 cgd
151 1.102 mycroft int wdprobe __P((struct device *, void *, void *));
152 1.102 mycroft void wdattach __P((struct device *, struct device *, void *));
153 1.149 christos int wdprint __P((void *, char *));
154 1.102 mycroft
155 1.147 thorpej struct cfattach wd_ca = {
156 1.147 thorpej sizeof(struct wd_softc), wdprobe, wdattach
157 1.147 thorpej };
158 1.147 thorpej
159 1.147 thorpej struct cfdriver wd_cd = {
160 1.147 thorpej NULL, "wd", DV_DISK
161 1.65 mycroft };
162 1.108 mycroft
163 1.149 christos void wdgetdisklabel __P((struct wd_softc *));
164 1.149 christos int wd_get_parms __P((struct wd_softc *));
165 1.149 christos void wdstrategy __P((struct buf *));
166 1.149 christos void wdstart __P((struct wd_softc *));
167 1.108 mycroft
168 1.102 mycroft struct dkdriver wddkdriver = { wdstrategy };
169 1.65 mycroft
170 1.149 christos /* XXX: these should go elsewhere */
171 1.149 christos cdev_decl(wd);
172 1.149 christos bdev_decl(wd);
173 1.149 christos
174 1.149 christos void wdfinish __P((struct wd_softc *, struct buf *));
175 1.149 christos int dcintr __P((void *));
176 1.149 christos void wdcstart __P((struct wdc_softc *));
177 1.149 christos int wdcommand __P((struct wd_softc *, int, int, int, int, int));
178 1.149 christos int wdcommandshort __P((struct wdc_softc *, int, int));
179 1.149 christos int wdcontrol __P((struct wd_softc *));
180 1.149 christos int wdsetctlr __P((struct wd_softc *));
181 1.64 mycroft static void bad144intern __P((struct wd_softc *));
182 1.149 christos int wdcreset __P((struct wdc_softc *));
183 1.149 christos void wdcrestart __P((void *arg));
184 1.149 christos void wdcunwedge __P((struct wdc_softc *));
185 1.149 christos void wdctimeout __P((void *arg));
186 1.149 christos void wderror __P((void *, struct buf *, char *));
187 1.149 christos int wdcwait __P((struct wdc_softc *, int));
188 1.149 christos int wdlock __P((struct wd_softc *));
189 1.149 christos void wdunlock __P((struct wd_softc *));
190 1.149 christos
191 1.87 mycroft /* ST506 spec says that if READY or SEEKCMPLT go off, then the read or write
192 1.87 mycroft command is aborted. */
193 1.110 mycroft #define wait_for_drq(d) wdcwait(d, WDCS_DRDY | WDCS_DSC | WDCS_DRQ)
194 1.110 mycroft #define wait_for_ready(d) wdcwait(d, WDCS_DRDY | WDCS_DSC)
195 1.64 mycroft #define wait_for_unbusy(d) wdcwait(d, 0)
196 1.1 cgd
197 1.1 cgd int
198 1.102 mycroft wdcprobe(parent, match, aux)
199 1.102 mycroft struct device *parent;
200 1.102 mycroft void *match, *aux;
201 1.1 cgd {
202 1.102 mycroft struct wdc_softc *wdc = match;
203 1.74 mycroft struct isa_attach_args *ia = aux;
204 1.107 mycroft int iobase;
205 1.1 cgd
206 1.74 mycroft wdc->sc_iobase = iobase = ia->ia_iobase;
207 1.64 mycroft
208 1.64 mycroft /* Check if we have registers that work. */
209 1.135 mycroft outb(iobase+wd_error, 0x5a); /* Error register not writable, */
210 1.135 mycroft outb(iobase+wd_cyl_lo, 0xa5); /* but all of cyllo are. */
211 1.64 mycroft if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5)
212 1.64 mycroft return 0;
213 1.3 deraadt
214 1.64 mycroft if (wdcreset(wdc) != 0) {
215 1.64 mycroft delay(500000);
216 1.64 mycroft if (wdcreset(wdc) != 0)
217 1.64 mycroft return 0;
218 1.64 mycroft }
219 1.1 cgd
220 1.135 mycroft /* Select drive 0. */
221 1.105 mycroft outb(iobase+wd_sdh, WDSD_IBM | 0);
222 1.105 mycroft
223 1.105 mycroft /* Wait for controller to become ready. */
224 1.105 mycroft if (wait_for_unbusy(wdc) < 0)
225 1.105 mycroft return 0;
226 1.105 mycroft
227 1.135 mycroft /* Start drive diagnostics. */
228 1.105 mycroft outb(iobase+wd_command, WDCC_DIAGNOSE);
229 1.105 mycroft
230 1.105 mycroft /* Wait for command to complete. */
231 1.121 mycroft if (wait_for_unbusy(wdc) < 0)
232 1.105 mycroft return 0;
233 1.3 deraadt
234 1.74 mycroft ia->ia_iosize = 8;
235 1.74 mycroft ia->ia_msize = 0;
236 1.74 mycroft return 1;
237 1.66 mycroft }
238 1.66 mycroft
239 1.74 mycroft struct wdc_attach_args {
240 1.74 mycroft int wa_drive;
241 1.74 mycroft };
242 1.74 mycroft
243 1.66 mycroft int
244 1.74 mycroft wdprint(aux, wdc)
245 1.74 mycroft void *aux;
246 1.74 mycroft char *wdc;
247 1.74 mycroft {
248 1.74 mycroft struct wdc_attach_args *wa = aux;
249 1.74 mycroft
250 1.74 mycroft if (!wdc)
251 1.74 mycroft printf(" drive %d", wa->wa_drive);
252 1.74 mycroft return QUIET;
253 1.66 mycroft }
254 1.66 mycroft
255 1.74 mycroft void
256 1.74 mycroft wdcattach(parent, self, aux)
257 1.74 mycroft struct device *parent, *self;
258 1.74 mycroft void *aux;
259 1.66 mycroft {
260 1.74 mycroft struct wdc_softc *wdc = (void *)self;
261 1.76 mycroft struct isa_attach_args *ia = aux;
262 1.74 mycroft struct wdc_attach_args wa;
263 1.67 mycroft
264 1.102 mycroft TAILQ_INIT(&wdc->sc_drives);
265 1.113 mycroft wdc->sc_drq = ia->ia_drq;
266 1.102 mycroft
267 1.74 mycroft printf("\n");
268 1.74 mycroft
269 1.148 cgd wdc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
270 1.148 cgd IPL_BIO, wdcintr, wdc);
271 1.102 mycroft
272 1.102 mycroft for (wa.wa_drive = 0; wa.wa_drive < 2; wa.wa_drive++)
273 1.102 mycroft (void)config_found(self, (void *)&wa, wdprint);
274 1.1 cgd }
275 1.1 cgd
276 1.1 cgd int
277 1.102 mycroft wdprobe(parent, match, aux)
278 1.102 mycroft struct device *parent;
279 1.102 mycroft void *match, *aux;
280 1.74 mycroft {
281 1.105 mycroft struct wdc_softc *wdc = (void *)parent;
282 1.105 mycroft struct cfdata *cf = match;
283 1.74 mycroft struct wdc_attach_args *wa = aux;
284 1.74 mycroft int drive = wa->wa_drive;
285 1.3 deraadt
286 1.102 mycroft if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
287 1.11 deraadt return 0;
288 1.3 deraadt
289 1.110 mycroft if (wdcommandshort(wdc, drive, WDCC_RECAL) != 0 ||
290 1.105 mycroft wait_for_ready(wdc) != 0)
291 1.74 mycroft return 0;
292 1.64 mycroft
293 1.74 mycroft return 1;
294 1.74 mycroft }
295 1.64 mycroft
296 1.74 mycroft void
297 1.74 mycroft wdattach(parent, self, aux)
298 1.74 mycroft struct device *parent, *self;
299 1.74 mycroft void *aux;
300 1.74 mycroft {
301 1.74 mycroft struct wd_softc *wd = (void *)self;
302 1.113 mycroft struct wdc_softc *wdc = (void *)parent;
303 1.106 mycroft struct wdc_attach_args *wa = aux;
304 1.74 mycroft int i, blank;
305 1.135 mycroft char buf[41], c, *p, *q;
306 1.56 mycroft
307 1.106 mycroft wd->sc_drive = wa->wa_drive;
308 1.106 mycroft
309 1.144 thorpej /*
310 1.144 thorpej * Initialize and attach the disk structure.
311 1.144 thorpej */
312 1.144 thorpej wd->sc_dk.dk_driver = &wddkdriver;
313 1.144 thorpej wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
314 1.144 thorpej disk_attach(&wd->sc_dk);
315 1.144 thorpej
316 1.108 mycroft wd_get_parms(wd);
317 1.135 mycroft for (blank = 0, p = wd->sc_params.wdp_model, q = buf, i = 0;
318 1.135 mycroft i < sizeof(wd->sc_params.wdp_model); i++) {
319 1.135 mycroft c = *p++;
320 1.135 mycroft if (c == '\0')
321 1.135 mycroft break;
322 1.135 mycroft if (c != ' ') {
323 1.135 mycroft if (blank) {
324 1.135 mycroft *q++ = ' ';
325 1.135 mycroft blank = 0;
326 1.135 mycroft }
327 1.135 mycroft *q++ = c;
328 1.135 mycroft } else
329 1.135 mycroft blank = 1;
330 1.135 mycroft }
331 1.135 mycroft *q++ = '\0';
332 1.135 mycroft
333 1.135 mycroft printf(": %dMB, %d cyl, %d head, %d sec, %d bytes/sec <%s>\n",
334 1.110 mycroft wd->sc_params.wdp_cylinders *
335 1.108 mycroft (wd->sc_params.wdp_heads * wd->sc_params.wdp_sectors) /
336 1.108 mycroft (1048576 / DEV_BSIZE),
337 1.110 mycroft wd->sc_params.wdp_cylinders,
338 1.108 mycroft wd->sc_params.wdp_heads,
339 1.108 mycroft wd->sc_params.wdp_sectors,
340 1.135 mycroft DEV_BSIZE,
341 1.135 mycroft buf);
342 1.113 mycroft
343 1.113 mycroft if ((wd->sc_params.wdp_capabilities & WD_CAP_DMA) != 0 &&
344 1.113 mycroft wdc->sc_drq != DRQUNK) {
345 1.113 mycroft wd->sc_mode = WDM_DMA;
346 1.113 mycroft } else if (wd->sc_params.wdp_maxmulti > 1) {
347 1.113 mycroft wd->sc_mode = WDM_PIOMULTI;
348 1.115 mycroft wd->sc_multiple = min(wd->sc_params.wdp_maxmulti, 16);
349 1.113 mycroft } else {
350 1.113 mycroft wd->sc_mode = WDM_PIOSINGLE;
351 1.113 mycroft wd->sc_multiple = 1;
352 1.113 mycroft }
353 1.113 mycroft
354 1.116 mycroft printf("%s: using", wd->sc_dev.dv_xname);
355 1.113 mycroft if (wd->sc_mode == WDM_DMA)
356 1.116 mycroft printf(" dma transfers,");
357 1.113 mycroft else
358 1.116 mycroft printf(" %d-sector %d-bit pio transfers,",
359 1.115 mycroft wd->sc_multiple, (wd->sc_flags & WDF_32BIT) == 0 ? 16 : 32);
360 1.116 mycroft if ((wd->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
361 1.116 mycroft printf(" lba addressing\n");
362 1.116 mycroft else
363 1.116 mycroft printf(" chs addressing\n");
364 1.1 cgd }
365 1.1 cgd
366 1.64 mycroft /*
367 1.135 mycroft * Read/write routine for a buffer. Validates the arguments and schedules the
368 1.135 mycroft * transfer. Does not wait for the transfer to complete.
369 1.1 cgd */
370 1.64 mycroft void
371 1.55 mycroft wdstrategy(bp)
372 1.55 mycroft struct buf *bp;
373 1.1 cgd {
374 1.147 thorpej struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
375 1.37 mycroft int s;
376 1.3 deraadt
377 1.135 mycroft /* Valid request? */
378 1.135 mycroft if (bp->b_blkno < 0 ||
379 1.144 thorpej (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
380 1.144 thorpej (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
381 1.1 cgd bp->b_error = EINVAL;
382 1.93 mycroft goto bad;
383 1.1 cgd }
384 1.3 deraadt
385 1.135 mycroft /* If device invalidated (e.g. media change, door open), error. */
386 1.135 mycroft if ((wd->sc_flags & WDF_LOADED) == 0) {
387 1.135 mycroft bp->b_error = EIO;
388 1.135 mycroft goto bad;
389 1.135 mycroft }
390 1.135 mycroft
391 1.93 mycroft /* If it's a null transfer, return immediately. */
392 1.93 mycroft if (bp->b_bcount == 0)
393 1.93 mycroft goto done;
394 1.93 mycroft
395 1.128 mycroft /*
396 1.128 mycroft * Do bounds checking, adjust transfer. if error, process.
397 1.128 mycroft * If end of partition, just return.
398 1.128 mycroft */
399 1.138 mycroft if (WDPART(bp->b_dev) != RAW_PART &&
400 1.144 thorpej bounds_check_with_label(bp, wd->sc_dk.dk_label,
401 1.132 mycroft (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
402 1.128 mycroft goto done;
403 1.3 deraadt
404 1.64 mycroft /* Queue transfer on drive, activate drive and controller if idle. */
405 1.1 cgd s = splbio();
406 1.113 mycroft disksort(&wd->sc_q, bp);
407 1.78 mycroft if (!wd->sc_q.b_active)
408 1.135 mycroft wdstart(wd);
409 1.120 mycroft #if 0
410 1.80 mycroft else {
411 1.80 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
412 1.120 mycroft if ((wdc->sc_flags & (WDCF_ACTIVE|WDCF_ERROR)) == 0) {
413 1.80 mycroft printf("wdstrategy: controller inactive\n");
414 1.80 mycroft wdcstart(wdc);
415 1.80 mycroft }
416 1.78 mycroft }
417 1.78 mycroft #endif
418 1.1 cgd splx(s);
419 1.64 mycroft return;
420 1.3 deraadt
421 1.93 mycroft bad:
422 1.93 mycroft bp->b_flags |= B_ERROR;
423 1.1 cgd done:
424 1.64 mycroft /* Toss transfer; we're done early. */
425 1.135 mycroft bp->b_resid = bp->b_bcount;
426 1.1 cgd biodone(bp);
427 1.1 cgd }
428 1.1 cgd
429 1.1 cgd /*
430 1.135 mycroft * Queue a drive for I/O.
431 1.1 cgd */
432 1.108 mycroft void
433 1.64 mycroft wdstart(wd)
434 1.64 mycroft struct wd_softc *wd;
435 1.1 cgd {
436 1.78 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
437 1.78 mycroft int active = wdc->sc_drives.tqh_first != 0;
438 1.64 mycroft
439 1.64 mycroft /* Link onto controller queue. */
440 1.78 mycroft wd->sc_q.b_active = 1;
441 1.78 mycroft TAILQ_INSERT_TAIL(&wdc->sc_drives, wd, sc_drivechain);
442 1.144 thorpej
443 1.144 thorpej disk_busy(&wd->sc_dk);
444 1.3 deraadt
445 1.78 mycroft /* If controller not already active, start it. */
446 1.78 mycroft if (!active)
447 1.78 mycroft wdcstart(wdc);
448 1.1 cgd }
449 1.1 cgd
450 1.135 mycroft /*
451 1.135 mycroft * Finish an I/O operation. Clean up the drive and controller state, set the
452 1.135 mycroft * residual count, and inform the upper layers that the operation is complete.
453 1.135 mycroft */
454 1.64 mycroft void
455 1.64 mycroft wdfinish(wd, bp)
456 1.64 mycroft struct wd_softc *wd;
457 1.64 mycroft struct buf *bp;
458 1.64 mycroft {
459 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
460 1.64 mycroft
461 1.69 mycroft wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR);
462 1.78 mycroft wdc->sc_errors = 0;
463 1.64 mycroft /*
464 1.113 mycroft * Move this drive to the end of the queue to give others a `fair'
465 1.113 mycroft * chance.
466 1.64 mycroft */
467 1.113 mycroft if (wd->sc_drivechain.tqe_next) {
468 1.113 mycroft TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
469 1.113 mycroft if (bp->b_actf) {
470 1.127 mycroft TAILQ_INSERT_TAIL(&wdc->sc_drives, wd, sc_drivechain);
471 1.113 mycroft } else
472 1.113 mycroft wd->sc_q.b_active = 0;
473 1.64 mycroft }
474 1.64 mycroft bp->b_resid = wd->sc_bcount;
475 1.64 mycroft wd->sc_skip = 0;
476 1.64 mycroft wd->sc_q.b_actf = bp->b_actf;
477 1.144 thorpej
478 1.144 thorpej disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
479 1.144 thorpej
480 1.145 mycroft if (!wd->sc_q.b_actf) {
481 1.145 mycroft TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
482 1.145 mycroft wd->sc_q.b_active = 0;
483 1.145 mycroft } else
484 1.145 mycroft disk_busy(&wd->sc_dk);
485 1.145 mycroft
486 1.64 mycroft biodone(bp);
487 1.141 mycroft }
488 1.141 mycroft
489 1.141 mycroft int
490 1.149 christos wdread(dev, uio, flags)
491 1.141 mycroft dev_t dev;
492 1.141 mycroft struct uio *uio;
493 1.149 christos int flags;
494 1.141 mycroft {
495 1.141 mycroft
496 1.141 mycroft return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
497 1.141 mycroft }
498 1.141 mycroft
499 1.141 mycroft int
500 1.149 christos wdwrite(dev, uio, flags)
501 1.141 mycroft dev_t dev;
502 1.141 mycroft struct uio *uio;
503 1.149 christos int flags;
504 1.141 mycroft {
505 1.141 mycroft
506 1.141 mycroft return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
507 1.64 mycroft }
508 1.64 mycroft
509 1.1 cgd /*
510 1.135 mycroft * Start I/O on a controller. This does the calculation, and starts a read or
511 1.135 mycroft * write operation. Called to from wdstart() to start a transfer, from
512 1.135 mycroft * wdcintr() to continue a multi-sector transfer or start the next transfer, or
513 1.135 mycroft * wdcrestart() after recovering from an error.
514 1.1 cgd */
515 1.135 mycroft void
516 1.64 mycroft wdcstart(wdc)
517 1.64 mycroft struct wdc_softc *wdc;
518 1.1 cgd {
519 1.135 mycroft struct wd_softc *wd;
520 1.64 mycroft struct buf *bp;
521 1.129 mycroft struct disklabel *lp;
522 1.113 mycroft int nblks;
523 1.64 mycroft
524 1.135 mycroft #ifdef DIAGNOSTIC
525 1.135 mycroft if ((wdc->sc_flags & WDCF_ACTIVE) != 0)
526 1.135 mycroft panic("wdcstart: controller still active");
527 1.135 mycroft #endif
528 1.135 mycroft
529 1.124 mycroft /*
530 1.124 mycroft * XXX
531 1.124 mycroft * This is a kluge. See comments in wd_get_parms().
532 1.124 mycroft */
533 1.124 mycroft if ((wdc->sc_flags & WDCF_WANTED) != 0) {
534 1.124 mycroft wdc->sc_flags &= ~WDCF_WANTED;
535 1.124 mycroft wakeup(wdc);
536 1.124 mycroft return;
537 1.124 mycroft }
538 1.124 mycroft
539 1.1 cgd loop:
540 1.64 mycroft /* Is there a drive for the controller to do a transfer with? */
541 1.78 mycroft wd = wdc->sc_drives.tqh_first;
542 1.78 mycroft if (wd == NULL)
543 1.1 cgd return;
544 1.3 deraadt
545 1.64 mycroft /* Is there a transfer to this drive? If not, deactivate drive. */
546 1.78 mycroft bp = wd->sc_q.b_actf;
547 1.3 deraadt
548 1.78 mycroft if (wdc->sc_errors >= WDIORETRIES) {
549 1.64 mycroft wderror(wd, bp, "hard error");
550 1.64 mycroft bp->b_error = EIO;
551 1.64 mycroft bp->b_flags |= B_ERROR;
552 1.64 mycroft wdfinish(wd, bp);
553 1.64 mycroft goto loop;
554 1.64 mycroft }
555 1.78 mycroft
556 1.69 mycroft /* Do control operations specially. */
557 1.64 mycroft if (wd->sc_state < OPEN) {
558 1.69 mycroft /*
559 1.69 mycroft * Actually, we want to be careful not to mess with the control
560 1.69 mycroft * state if the device is currently busy, but we can assume
561 1.69 mycroft * that we never get to this point if that's the case.
562 1.69 mycroft */
563 1.116 mycroft if (wdcontrol(wd) == 0) {
564 1.116 mycroft /* The drive is busy. Wait. */
565 1.116 mycroft return;
566 1.116 mycroft }
567 1.1 cgd }
568 1.64 mycroft
569 1.64 mycroft /*
570 1.76 mycroft * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
571 1.69 mycroft * encountered. If we are in multi-sector mode, then we switch to
572 1.69 mycroft * single-sector mode and retry the operation from the start.
573 1.64 mycroft */
574 1.69 mycroft if (wdc->sc_flags & WDCF_ERROR) {
575 1.69 mycroft wdc->sc_flags &= ~WDCF_ERROR;
576 1.69 mycroft if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
577 1.69 mycroft wdc->sc_flags |= WDCF_SINGLE;
578 1.113 mycroft wd->sc_skip = 0;
579 1.69 mycroft }
580 1.64 mycroft }
581 1.64 mycroft
582 1.144 thorpej lp = wd->sc_dk.dk_label;
583 1.129 mycroft
584 1.135 mycroft /* When starting a transfer... */
585 1.95 mycroft if (wd->sc_skip == 0) {
586 1.125 mycroft int part = WDPART(bp->b_dev);
587 1.129 mycroft daddr_t blkno;
588 1.125 mycroft
589 1.44 mycroft #ifdef WDDEBUG
590 1.95 mycroft printf("\n%s: wdcstart %s %d@%d; map ", wd->sc_dev.dv_xname,
591 1.37 mycroft (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
592 1.95 mycroft bp->b_blkno);
593 1.95 mycroft #endif
594 1.95 mycroft wd->sc_bcount = bp->b_bcount;
595 1.129 mycroft blkno = bp->b_blkno;
596 1.125 mycroft if (part != RAW_PART)
597 1.129 mycroft blkno += lp->d_partitions[part].p_offset;
598 1.129 mycroft wd->sc_blkno = blkno / (lp->d_secsize / DEV_BSIZE);
599 1.95 mycroft } else {
600 1.95 mycroft #ifdef WDDEBUG
601 1.64 mycroft printf(" %d)%x", wd->sc_skip, inb(wd->sc_iobase+wd_altsts));
602 1.1 cgd #endif
603 1.95 mycroft }
604 1.95 mycroft
605 1.135 mycroft /* When starting a multi-sector transfer, or doing single-sector
606 1.135 mycroft transfers... */
607 1.129 mycroft if (wd->sc_skip == 0 || (wdc->sc_flags & WDCF_SINGLE) != 0 ||
608 1.129 mycroft wd->sc_mode == WDM_DMA) {
609 1.125 mycroft daddr_t blkno = wd->sc_blkno;
610 1.95 mycroft long cylin, head, sector;
611 1.95 mycroft int command;
612 1.95 mycroft
613 1.117 mycroft if ((wdc->sc_flags & WDCF_SINGLE) != 0)
614 1.95 mycroft nblks = 1;
615 1.117 mycroft else if (wd->sc_mode != WDM_DMA)
616 1.129 mycroft nblks = wd->sc_bcount / lp->d_secsize;
617 1.95 mycroft else
618 1.129 mycroft nblks = min(wd->sc_bcount / lp->d_secsize, 8);
619 1.95 mycroft
620 1.117 mycroft /* Check for bad sectors and adjust transfer, if necessary. */
621 1.116 mycroft if ((lp->d_flags & D_BADSECT) != 0
622 1.44 mycroft #ifdef B_FORMAT
623 1.95 mycroft && (bp->b_flags & B_FORMAT) == 0
624 1.3 deraadt #endif
625 1.95 mycroft ) {
626 1.95 mycroft long blkdiff;
627 1.95 mycroft int i;
628 1.95 mycroft
629 1.95 mycroft for (i = 0; (blkdiff = wd->sc_badsect[i]) != -1; i++) {
630 1.95 mycroft blkdiff -= blkno;
631 1.95 mycroft if (blkdiff < 0)
632 1.95 mycroft continue;
633 1.95 mycroft if (blkdiff == 0) {
634 1.95 mycroft /* Replace current block of transfer. */
635 1.95 mycroft blkno =
636 1.95 mycroft lp->d_secperunit - lp->d_nsectors - i - 1;
637 1.113 mycroft }
638 1.113 mycroft if (blkdiff < nblks) {
639 1.95 mycroft /* Bad block inside transfer. */
640 1.113 mycroft wdc->sc_flags |= WDCF_SINGLE;
641 1.113 mycroft nblks = 1;
642 1.95 mycroft }
643 1.113 mycroft break;
644 1.3 deraadt }
645 1.95 mycroft /* Tranfer is okay now. */
646 1.3 deraadt }
647 1.1 cgd
648 1.116 mycroft if ((wd->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
649 1.116 mycroft sector = (blkno >> 0) & 0xff;
650 1.116 mycroft cylin = (blkno >> 8) & 0xffff;
651 1.117 mycroft head = (blkno >> 24) & 0xf;
652 1.116 mycroft head |= WDSD_LBA;
653 1.116 mycroft } else {
654 1.116 mycroft sector = blkno % lp->d_nsectors;
655 1.116 mycroft sector++; /* Sectors begin with 1, not 0. */
656 1.116 mycroft blkno /= lp->d_nsectors;
657 1.116 mycroft head = blkno % lp->d_ntracks;
658 1.116 mycroft blkno /= lp->d_ntracks;
659 1.116 mycroft cylin = blkno;
660 1.116 mycroft head |= WDSD_CHS;
661 1.116 mycroft }
662 1.51 mycroft
663 1.129 mycroft if (wd->sc_mode == WDM_PIOSINGLE ||
664 1.129 mycroft (wdc->sc_flags & WDCF_SINGLE) != 0)
665 1.129 mycroft wd->sc_nblks = 1;
666 1.129 mycroft else if (wd->sc_mode == WDM_PIOMULTI)
667 1.129 mycroft wd->sc_nblks = min(nblks, wd->sc_multiple);
668 1.129 mycroft else
669 1.129 mycroft wd->sc_nblks = nblks;
670 1.129 mycroft wd->sc_nbytes = wd->sc_nblks * lp->d_secsize;
671 1.129 mycroft
672 1.37 mycroft #ifdef B_FORMAT
673 1.1 cgd if (bp->b_flags & B_FORMAT) {
674 1.60 mycroft sector = lp->d_gap3;
675 1.95 mycroft nblks = lp->d_nsectors;
676 1.60 mycroft command = WDCC_FORMAT;
677 1.95 mycroft } else
678 1.95 mycroft #endif
679 1.113 mycroft switch (wd->sc_mode) {
680 1.113 mycroft case WDM_DMA:
681 1.113 mycroft command = (bp->b_flags & B_READ) ?
682 1.113 mycroft WDCC_READDMA : WDCC_WRITEDMA;
683 1.135 mycroft /* Start the DMA channel and bounce the buffer if
684 1.135 mycroft necessary. */
685 1.146 mycroft isa_dmastart(
686 1.146 mycroft bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE,
687 1.125 mycroft bp->b_data + wd->sc_skip,
688 1.129 mycroft wd->sc_nbytes, wdc->sc_drq);
689 1.113 mycroft break;
690 1.113 mycroft case WDM_PIOMULTI:
691 1.113 mycroft command = (bp->b_flags & B_READ) ?
692 1.113 mycroft WDCC_READMULTI : WDCC_WRITEMULTI;
693 1.113 mycroft break;
694 1.113 mycroft case WDM_PIOSINGLE:
695 1.113 mycroft command = (bp->b_flags & B_READ) ?
696 1.113 mycroft WDCC_READ : WDCC_WRITE;
697 1.113 mycroft break;
698 1.149 christos default:
699 1.149 christos #ifdef DIAGNOSTIC
700 1.149 christos panic("bad wd mode");
701 1.149 christos #endif
702 1.149 christos return;
703 1.113 mycroft }
704 1.3 deraadt
705 1.64 mycroft /* Initiate command! */
706 1.105 mycroft if (wdcommand(wd, command, cylin, head, sector, nblks) != 0) {
707 1.64 mycroft wderror(wd, NULL,
708 1.64 mycroft "wdcstart: timeout waiting for unbusy");
709 1.64 mycroft wdcunwedge(wdc);
710 1.64 mycroft return;
711 1.51 mycroft }
712 1.135 mycroft
713 1.44 mycroft #ifdef WDDEBUG
714 1.37 mycroft printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
715 1.84 mycroft cylin, head, bp->b_data, inb(wd->sc_iobase+wd_altsts));
716 1.1 cgd #endif
717 1.129 mycroft } else if (wd->sc_nblks > 1) {
718 1.135 mycroft /* The number of blocks in the last stretch may be smaller. */
719 1.129 mycroft nblks = wd->sc_bcount / lp->d_secsize;
720 1.129 mycroft if (wd->sc_nblks > nblks) {
721 1.129 mycroft wd->sc_nblks = nblks;
722 1.129 mycroft wd->sc_nbytes = wd->sc_bcount;
723 1.129 mycroft }
724 1.1 cgd }
725 1.78 mycroft
726 1.113 mycroft /* If this was a write and not using DMA, push the data. */
727 1.113 mycroft if (wd->sc_mode != WDM_DMA &&
728 1.135 mycroft (bp->b_flags & (B_READ|B_WRITE)) == B_WRITE) {
729 1.113 mycroft if (wait_for_drq(wdc) < 0) {
730 1.113 mycroft wderror(wd, NULL, "wdcstart: timeout waiting for drq");
731 1.113 mycroft wdcunwedge(wdc);
732 1.113 mycroft return;
733 1.113 mycroft }
734 1.113 mycroft
735 1.135 mycroft /* Push out data. */
736 1.115 mycroft if ((wd->sc_flags & WDF_32BIT) == 0)
737 1.125 mycroft outsw(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip,
738 1.129 mycroft wd->sc_nbytes >> 1);
739 1.115 mycroft else
740 1.125 mycroft outsl(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip,
741 1.129 mycroft wd->sc_nbytes >> 2);
742 1.19 deraadt }
743 1.116 mycroft
744 1.116 mycroft wdc->sc_flags |= WDCF_ACTIVE;
745 1.116 mycroft timeout(wdctimeout, wdc, WAITTIME);
746 1.1 cgd }
747 1.1 cgd
748 1.64 mycroft /*
749 1.64 mycroft * Interrupt routine for the controller. Acknowledge the interrupt, check for
750 1.64 mycroft * errors on the current operation, mark it done if necessary, and start the
751 1.64 mycroft * next request. Also check for a partially done transfer, and continue with
752 1.64 mycroft * the next chunk if so.
753 1.1 cgd */
754 1.64 mycroft int
755 1.139 cgd wdcintr(arg)
756 1.139 cgd void *arg;
757 1.1 cgd {
758 1.139 cgd struct wdc_softc *wdc = arg;
759 1.64 mycroft struct wd_softc *wd;
760 1.64 mycroft struct buf *bp;
761 1.59 mycroft
762 1.87 mycroft if ((wdc->sc_flags & WDCF_ACTIVE) == 0) {
763 1.135 mycroft /* Clear the pending interrupt and abort. */
764 1.87 mycroft (void) inb(wdc->sc_iobase+wd_status);
765 1.64 mycroft return 0;
766 1.87 mycroft }
767 1.78 mycroft
768 1.116 mycroft wdc->sc_flags &= ~WDCF_ACTIVE;
769 1.116 mycroft untimeout(wdctimeout, wdc);
770 1.116 mycroft
771 1.78 mycroft wd = wdc->sc_drives.tqh_first;
772 1.78 mycroft bp = wd->sc_q.b_actf;
773 1.78 mycroft
774 1.44 mycroft #ifdef WDDEBUG
775 1.3 deraadt printf("I%d ", ctrlr);
776 1.1 cgd #endif
777 1.13 deraadt
778 1.64 mycroft if (wait_for_unbusy(wdc) < 0) {
779 1.76 mycroft wderror(wd, NULL, "wdcintr: timeout waiting for unbusy");
780 1.64 mycroft wdc->sc_status |= WDCS_ERR; /* XXX */
781 1.27 cgd }
782 1.3 deraadt
783 1.64 mycroft /* Is it not a transfer, but a control operation? */
784 1.64 mycroft if (wd->sc_state < OPEN) {
785 1.116 mycroft if (wdcontrol(wd) == 0) {
786 1.116 mycroft /* The drive is busy. Wait. */
787 1.116 mycroft return 1;
788 1.116 mycroft }
789 1.116 mycroft wdcstart(wdc);
790 1.64 mycroft return 1;
791 1.1 cgd }
792 1.78 mycroft
793 1.135 mycroft /* Turn off the DMA channel and unbounce the buffer. */
794 1.113 mycroft if (wd->sc_mode == WDM_DMA)
795 1.146 mycroft isa_dmadone(bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE,
796 1.146 mycroft bp->b_data + wd->sc_skip, wd->sc_nbytes, wdc->sc_drq);
797 1.113 mycroft
798 1.64 mycroft /* Have we an error? */
799 1.96 mycroft if (wdc->sc_status & WDCS_ERR) {
800 1.44 mycroft #ifdef WDDEBUG
801 1.76 mycroft wderror(wd, NULL, "wdcintr");
802 1.1 cgd #endif
803 1.69 mycroft if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
804 1.69 mycroft wdc->sc_flags |= WDCF_ERROR;
805 1.64 mycroft goto restart;
806 1.1 cgd }
807 1.96 mycroft
808 1.1 cgd #ifdef B_FORMAT
809 1.96 mycroft if (bp->b_flags & B_FORMAT)
810 1.96 mycroft goto bad;
811 1.1 cgd #endif
812 1.3 deraadt
813 1.96 mycroft if (++wdc->sc_errors < WDIORETRIES)
814 1.96 mycroft goto restart;
815 1.96 mycroft wderror(wd, bp, "hard error");
816 1.96 mycroft
817 1.149 christos #ifdef B_FORMAT
818 1.96 mycroft bad:
819 1.149 christos #endif
820 1.96 mycroft bp->b_error = EIO;
821 1.96 mycroft bp->b_flags |= B_ERROR;
822 1.96 mycroft goto done;
823 1.1 cgd }
824 1.96 mycroft
825 1.113 mycroft /* If this was a read and not using DMA, fetch the data. */
826 1.113 mycroft if (wd->sc_mode != WDM_DMA &&
827 1.135 mycroft (bp->b_flags & (B_READ|B_WRITE)) == B_READ) {
828 1.110 mycroft if ((wdc->sc_status & (WDCS_DRDY | WDCS_DSC | WDCS_DRQ))
829 1.110 mycroft != (WDCS_DRDY | WDCS_DSC | WDCS_DRQ)) {
830 1.87 mycroft wderror(wd, NULL, "wdcintr: read intr before drq");
831 1.64 mycroft wdcunwedge(wdc);
832 1.64 mycroft return 1;
833 1.27 cgd }
834 1.27 cgd
835 1.135 mycroft /* Pull in data. */
836 1.115 mycroft if ((wd->sc_flags & WDF_32BIT) == 0)
837 1.125 mycroft insw(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip,
838 1.129 mycroft wd->sc_nbytes >> 1);
839 1.115 mycroft else
840 1.125 mycroft insl(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip,
841 1.129 mycroft wd->sc_nbytes >> 2);
842 1.64 mycroft }
843 1.64 mycroft
844 1.64 mycroft /* If we encountered any abnormalities, flag it as a soft error. */
845 1.135 mycroft if (wdc->sc_errors > 0 ||
846 1.135 mycroft (wdc->sc_status & WDCS_CORR) != 0) {
847 1.135 mycroft wderror(wd, bp, "soft error (corrected)");
848 1.78 mycroft wdc->sc_errors = 0;
849 1.1 cgd }
850 1.3 deraadt
851 1.135 mycroft /* Adjust pointers for the next block, if any. */
852 1.129 mycroft wd->sc_blkno += wd->sc_nblks;
853 1.129 mycroft wd->sc_skip += wd->sc_nbytes;
854 1.129 mycroft wd->sc_bcount -= wd->sc_nbytes;
855 1.64 mycroft
856 1.135 mycroft /* See if this transfer is complete. */
857 1.64 mycroft if (wd->sc_bcount > 0)
858 1.64 mycroft goto restart;
859 1.64 mycroft
860 1.64 mycroft done:
861 1.64 mycroft /* Done with this transfer, with or without error. */
862 1.64 mycroft wdfinish(wd, bp);
863 1.64 mycroft
864 1.64 mycroft restart:
865 1.135 mycroft /* Start the next operation, if any. */
866 1.64 mycroft wdcstart(wdc);
867 1.1 cgd
868 1.64 mycroft return 1;
869 1.1 cgd }
870 1.1 cgd
871 1.135 mycroft /*
872 1.135 mycroft * Wait interruptibly for an exclusive lock.
873 1.135 mycroft *
874 1.135 mycroft * XXX
875 1.135 mycroft * Several drivers do this; it should be abstracted and made MP-safe.
876 1.135 mycroft */
877 1.132 mycroft int
878 1.135 mycroft wdlock(wd)
879 1.132 mycroft struct wd_softc *wd;
880 1.132 mycroft {
881 1.132 mycroft int error;
882 1.132 mycroft
883 1.132 mycroft while ((wd->sc_flags & WDF_LOCKED) != 0) {
884 1.132 mycroft wd->sc_flags |= WDF_WANTED;
885 1.132 mycroft if ((error = tsleep(wd, PRIBIO | PCATCH, "wdlck", 0)) != 0)
886 1.132 mycroft return error;
887 1.132 mycroft }
888 1.135 mycroft wd->sc_flags |= WDF_LOCKED;
889 1.132 mycroft return 0;
890 1.132 mycroft }
891 1.132 mycroft
892 1.135 mycroft /*
893 1.135 mycroft * Unlock and wake up any waiters.
894 1.135 mycroft */
895 1.132 mycroft void
896 1.132 mycroft wdunlock(wd)
897 1.132 mycroft struct wd_softc *wd;
898 1.132 mycroft {
899 1.132 mycroft
900 1.132 mycroft wd->sc_flags &= ~WDF_LOCKED;
901 1.132 mycroft if ((wd->sc_flags & WDF_WANTED) != 0) {
902 1.132 mycroft wd->sc_flags &= ~WDF_WANTED;
903 1.132 mycroft wakeup(wd);
904 1.132 mycroft }
905 1.132 mycroft }
906 1.132 mycroft
907 1.1 cgd int
908 1.149 christos wdopen(dev, flag, fmt, p)
909 1.55 mycroft dev_t dev;
910 1.93 mycroft int flag, fmt;
911 1.149 christos struct proc *p;
912 1.1 cgd {
913 1.135 mycroft struct wd_softc *wd;
914 1.135 mycroft int unit, part;
915 1.109 mycroft int error;
916 1.3 deraadt
917 1.108 mycroft unit = WDUNIT(dev);
918 1.147 thorpej if (unit >= wd_cd.cd_ndevs)
919 1.37 mycroft return ENXIO;
920 1.147 thorpej wd = wd_cd.cd_devs[unit];
921 1.64 mycroft if (wd == 0)
922 1.37 mycroft return ENXIO;
923 1.3 deraadt
924 1.149 christos if ((error = wdlock(wd)) != 0)
925 1.132 mycroft return error;
926 1.3 deraadt
927 1.109 mycroft if (wd->sc_dk.dk_openmask != 0) {
928 1.109 mycroft /*
929 1.109 mycroft * If any partition is open, but the disk has been invalidated,
930 1.109 mycroft * disallow further opens.
931 1.109 mycroft */
932 1.136 mycroft if ((wd->sc_flags & WDF_LOADED) == 0) {
933 1.136 mycroft error = EIO;
934 1.136 mycroft goto bad3;
935 1.136 mycroft }
936 1.109 mycroft } else {
937 1.108 mycroft if ((wd->sc_flags & WDF_LOADED) == 0) {
938 1.108 mycroft wd->sc_flags |= WDF_LOADED;
939 1.108 mycroft
940 1.108 mycroft /* Load the physical device parameters. */
941 1.108 mycroft if (wd_get_parms(wd) != 0) {
942 1.108 mycroft error = ENXIO;
943 1.108 mycroft goto bad2;
944 1.108 mycroft }
945 1.108 mycroft
946 1.108 mycroft /* Load the partition info if not already loaded. */
947 1.108 mycroft wdgetdisklabel(wd);
948 1.108 mycroft }
949 1.135 mycroft }
950 1.108 mycroft
951 1.135 mycroft part = WDPART(dev);
952 1.108 mycroft
953 1.109 mycroft /* Check that the partition exists. */
954 1.93 mycroft if (part != RAW_PART &&
955 1.144 thorpej (part >= wd->sc_dk.dk_label->d_npartitions ||
956 1.144 thorpej wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
957 1.93 mycroft error = ENXIO;
958 1.93 mycroft goto bad;
959 1.93 mycroft }
960 1.3 deraadt
961 1.64 mycroft /* Insure only one open at a time. */
962 1.3 deraadt switch (fmt) {
963 1.3 deraadt case S_IFCHR:
964 1.94 mycroft wd->sc_dk.dk_copenmask |= (1 << part);
965 1.3 deraadt break;
966 1.3 deraadt case S_IFBLK:
967 1.94 mycroft wd->sc_dk.dk_bopenmask |= (1 << part);
968 1.3 deraadt break;
969 1.3 deraadt }
970 1.94 mycroft wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
971 1.60 mycroft
972 1.135 mycroft wdunlock(wd);
973 1.37 mycroft return 0;
974 1.93 mycroft
975 1.108 mycroft bad2:
976 1.108 mycroft wd->sc_flags &= ~WDF_LOADED;
977 1.108 mycroft
978 1.93 mycroft bad:
979 1.118 mycroft if (wd->sc_dk.dk_openmask == 0) {
980 1.108 mycroft }
981 1.108 mycroft
982 1.136 mycroft bad3:
983 1.135 mycroft wdunlock(wd);
984 1.93 mycroft return error;
985 1.1 cgd }
986 1.1 cgd
987 1.135 mycroft int
988 1.149 christos wdclose(dev, flag, fmt, p)
989 1.135 mycroft dev_t dev;
990 1.135 mycroft int flag, fmt;
991 1.149 christos struct proc *p;
992 1.135 mycroft {
993 1.147 thorpej struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
994 1.135 mycroft int part = WDPART(dev);
995 1.135 mycroft int error;
996 1.135 mycroft
997 1.149 christos if ((error = wdlock(wd)) != 0)
998 1.135 mycroft return error;
999 1.135 mycroft
1000 1.135 mycroft switch (fmt) {
1001 1.135 mycroft case S_IFCHR:
1002 1.135 mycroft wd->sc_dk.dk_copenmask &= ~(1 << part);
1003 1.135 mycroft break;
1004 1.135 mycroft case S_IFBLK:
1005 1.135 mycroft wd->sc_dk.dk_bopenmask &= ~(1 << part);
1006 1.135 mycroft break;
1007 1.135 mycroft }
1008 1.135 mycroft wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1009 1.135 mycroft
1010 1.135 mycroft if (wd->sc_dk.dk_openmask == 0) {
1011 1.135 mycroft /* XXXX Must wait for I/O to complete! */
1012 1.135 mycroft }
1013 1.135 mycroft
1014 1.135 mycroft wdunlock(wd);
1015 1.135 mycroft return 0;
1016 1.135 mycroft }
1017 1.135 mycroft
1018 1.135 mycroft /*
1019 1.135 mycroft * Fabricate a default disk label, and try to read the correct one.
1020 1.135 mycroft */
1021 1.108 mycroft void
1022 1.108 mycroft wdgetdisklabel(wd)
1023 1.108 mycroft struct wd_softc *wd;
1024 1.108 mycroft {
1025 1.144 thorpej struct disklabel *lp = wd->sc_dk.dk_label;
1026 1.108 mycroft char *errstring;
1027 1.108 mycroft
1028 1.142 mycroft bzero(lp, sizeof(struct disklabel));
1029 1.144 thorpej bzero(wd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
1030 1.108 mycroft
1031 1.142 mycroft lp->d_secsize = DEV_BSIZE;
1032 1.142 mycroft lp->d_ntracks = wd->sc_params.wdp_heads;
1033 1.142 mycroft lp->d_nsectors = wd->sc_params.wdp_sectors;
1034 1.142 mycroft lp->d_ncylinders = wd->sc_params.wdp_cylinders;
1035 1.142 mycroft lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1036 1.108 mycroft
1037 1.108 mycroft #if 0
1038 1.142 mycroft strncpy(lp->d_typename, "ST506 disk", 16);
1039 1.142 mycroft lp->d_type = DTYPE_ST506;
1040 1.108 mycroft #endif
1041 1.142 mycroft strncpy(lp->d_packname, wd->sc_params.wdp_model, 16);
1042 1.142 mycroft lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1043 1.142 mycroft lp->d_rpm = 3600;
1044 1.142 mycroft lp->d_interleave = 1;
1045 1.142 mycroft lp->d_flags = 0;
1046 1.142 mycroft
1047 1.142 mycroft lp->d_partitions[RAW_PART].p_offset = 0;
1048 1.142 mycroft lp->d_partitions[RAW_PART].p_size =
1049 1.142 mycroft lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1050 1.142 mycroft lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1051 1.142 mycroft lp->d_npartitions = RAW_PART + 1;
1052 1.142 mycroft
1053 1.142 mycroft lp->d_magic = DISKMAGIC;
1054 1.142 mycroft lp->d_magic2 = DISKMAGIC;
1055 1.142 mycroft lp->d_checksum = dkcksum(lp);
1056 1.108 mycroft
1057 1.113 mycroft wd->sc_badsect[0] = -1;
1058 1.113 mycroft
1059 1.108 mycroft if (wd->sc_state > RECAL)
1060 1.108 mycroft wd->sc_state = RECAL;
1061 1.108 mycroft errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
1062 1.144 thorpej wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1063 1.108 mycroft if (errstring) {
1064 1.108 mycroft /*
1065 1.108 mycroft * This probably happened because the drive's default
1066 1.108 mycroft * geometry doesn't match the DOS geometry. We
1067 1.108 mycroft * assume the DOS geometry is now in the label and try
1068 1.108 mycroft * again. XXX This is a kluge.
1069 1.108 mycroft */
1070 1.108 mycroft if (wd->sc_state > GEOMETRY)
1071 1.108 mycroft wd->sc_state = GEOMETRY;
1072 1.108 mycroft errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
1073 1.144 thorpej wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1074 1.108 mycroft }
1075 1.108 mycroft if (errstring) {
1076 1.108 mycroft printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
1077 1.108 mycroft return;
1078 1.108 mycroft }
1079 1.108 mycroft
1080 1.108 mycroft if (wd->sc_state > GEOMETRY)
1081 1.108 mycroft wd->sc_state = GEOMETRY;
1082 1.142 mycroft if ((lp->d_flags & D_BADSECT) != 0)
1083 1.108 mycroft bad144intern(wd);
1084 1.108 mycroft }
1085 1.108 mycroft
1086 1.1 cgd /*
1087 1.135 mycroft * Implement operations needed before read/write.
1088 1.1 cgd * Returns 0 if operation still in progress, 1 if completed.
1089 1.1 cgd */
1090 1.135 mycroft int
1091 1.69 mycroft wdcontrol(wd)
1092 1.69 mycroft struct wd_softc *wd;
1093 1.1 cgd {
1094 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1095 1.3 deraadt
1096 1.64 mycroft switch (wd->sc_state) {
1097 1.110 mycroft case RECAL: /* Set SDH, step rate, do recal. */
1098 1.110 mycroft if (wdcommandshort(wdc, wd->sc_drive, WDCC_RECAL) != 0) {
1099 1.113 mycroft wderror(wd, NULL, "wdcontrol: recal failed (1)");
1100 1.113 mycroft goto bad;
1101 1.21 deraadt }
1102 1.69 mycroft wd->sc_state = RECAL_WAIT;
1103 1.116 mycroft break;
1104 1.55 mycroft
1105 1.69 mycroft case RECAL_WAIT:
1106 1.69 mycroft if (wdc->sc_status & WDCS_ERR) {
1107 1.113 mycroft wderror(wd, NULL, "wdcontrol: recal failed (2)");
1108 1.113 mycroft goto bad;
1109 1.1 cgd }
1110 1.69 mycroft /* fall through */
1111 1.69 mycroft case GEOMETRY:
1112 1.116 mycroft if ((wd->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
1113 1.116 mycroft goto multimode;
1114 1.69 mycroft if (wdsetctlr(wd) != 0) {
1115 1.69 mycroft /* Already printed a message. */
1116 1.113 mycroft goto bad;
1117 1.69 mycroft }
1118 1.69 mycroft wd->sc_state = GEOMETRY_WAIT;
1119 1.116 mycroft break;
1120 1.69 mycroft
1121 1.69 mycroft case GEOMETRY_WAIT:
1122 1.69 mycroft if (wdc->sc_status & WDCS_ERR) {
1123 1.69 mycroft wderror(wd, NULL, "wdcontrol: geometry failed");
1124 1.113 mycroft goto bad;
1125 1.113 mycroft }
1126 1.113 mycroft /* fall through */
1127 1.113 mycroft case MULTIMODE:
1128 1.116 mycroft multimode:
1129 1.113 mycroft if (wd->sc_mode != WDM_PIOMULTI)
1130 1.113 mycroft goto open;
1131 1.113 mycroft outb(wdc->sc_iobase+wd_seccnt, wd->sc_multiple);
1132 1.113 mycroft if (wdcommandshort(wdc, wd->sc_drive, WDCC_SETMULTI) != 0) {
1133 1.113 mycroft wderror(wd, NULL, "wdcontrol: setmulti failed (1)");
1134 1.113 mycroft goto bad;
1135 1.69 mycroft }
1136 1.113 mycroft wd->sc_state = MULTIMODE_WAIT;
1137 1.116 mycroft break;
1138 1.69 mycroft
1139 1.113 mycroft case MULTIMODE_WAIT:
1140 1.113 mycroft if (wdc->sc_status & WDCS_ERR) {
1141 1.113 mycroft wderror(wd, NULL, "wdcontrol: setmulti failed (2)");
1142 1.113 mycroft goto bad;
1143 1.113 mycroft }
1144 1.113 mycroft /* fall through */
1145 1.116 mycroft case OPEN:
1146 1.113 mycroft open:
1147 1.78 mycroft wdc->sc_errors = 0;
1148 1.64 mycroft wd->sc_state = OPEN;
1149 1.1 cgd /*
1150 1.63 mycroft * The rest of the initialization can be done by normal means.
1151 1.1 cgd */
1152 1.37 mycroft return 1;
1153 1.113 mycroft
1154 1.113 mycroft bad:
1155 1.113 mycroft wdcunwedge(wdc);
1156 1.113 mycroft return 0;
1157 1.1 cgd }
1158 1.1 cgd
1159 1.116 mycroft wdc->sc_flags |= WDCF_ACTIVE;
1160 1.116 mycroft timeout(wdctimeout, wdc, WAITTIME);
1161 1.116 mycroft return 0;
1162 1.1 cgd }
1163 1.1 cgd
1164 1.1 cgd /*
1165 1.135 mycroft * Wait for the drive to become ready and send a command.
1166 1.135 mycroft * Return -1 if busy for too long or 0 otherwise.
1167 1.64 mycroft * Assumes interrupts are blocked.
1168 1.1 cgd */
1169 1.135 mycroft int
1170 1.105 mycroft wdcommand(wd, command, cylin, head, sector, count)
1171 1.64 mycroft struct wd_softc *wd;
1172 1.105 mycroft int command;
1173 1.60 mycroft int cylin, head, sector, count;
1174 1.3 deraadt {
1175 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1176 1.107 mycroft int iobase = wdc->sc_iobase;
1177 1.60 mycroft int stat;
1178 1.3 deraadt
1179 1.116 mycroft /* Select drive, head, and addressing mode. */
1180 1.74 mycroft outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_drive << 4) | head);
1181 1.90 mycroft
1182 1.90 mycroft /* Wait for it to become ready to accept a command. */
1183 1.110 mycroft if (command == WDCC_IDP)
1184 1.64 mycroft stat = wait_for_unbusy(wdc);
1185 1.60 mycroft else
1186 1.110 mycroft stat = wdcwait(wdc, WDCS_DRDY);
1187 1.60 mycroft if (stat < 0)
1188 1.60 mycroft return -1;
1189 1.3 deraadt
1190 1.64 mycroft /* Load parameters. */
1191 1.144 thorpej if (wd->sc_dk.dk_label->d_type == DTYPE_ST506)
1192 1.144 thorpej outb(iobase+wd_precomp, wd->sc_dk.dk_label->d_precompcyl / 4);
1193 1.110 mycroft else
1194 1.110 mycroft outb(iobase+wd_features, 0);
1195 1.64 mycroft outb(iobase+wd_cyl_lo, cylin);
1196 1.64 mycroft outb(iobase+wd_cyl_hi, cylin >> 8);
1197 1.64 mycroft outb(iobase+wd_sector, sector);
1198 1.64 mycroft outb(iobase+wd_seccnt, count);
1199 1.60 mycroft
1200 1.105 mycroft /* Send command. */
1201 1.105 mycroft outb(iobase+wd_command, command);
1202 1.105 mycroft
1203 1.105 mycroft return 0;
1204 1.105 mycroft }
1205 1.105 mycroft
1206 1.135 mycroft /*
1207 1.135 mycroft * Simplified version of wdcommand().
1208 1.135 mycroft */
1209 1.105 mycroft int
1210 1.105 mycroft wdcommandshort(wdc, drive, command)
1211 1.105 mycroft struct wdc_softc *wdc;
1212 1.105 mycroft int drive;
1213 1.105 mycroft int command;
1214 1.105 mycroft {
1215 1.107 mycroft int iobase = wdc->sc_iobase;
1216 1.105 mycroft
1217 1.105 mycroft /* Select drive. */
1218 1.105 mycroft outb(iobase+wd_sdh, WDSD_IBM | (drive << 4));
1219 1.105 mycroft
1220 1.110 mycroft if (wdcwait(wdc, WDCS_DRDY) < 0)
1221 1.105 mycroft return -1;
1222 1.105 mycroft
1223 1.105 mycroft outb(iobase+wd_command, command);
1224 1.40 mycroft
1225 1.64 mycroft return 0;
1226 1.1 cgd }
1227 1.1 cgd
1228 1.1 cgd /*
1229 1.135 mycroft * Tell the drive what geometry to use.
1230 1.1 cgd */
1231 1.135 mycroft int
1232 1.64 mycroft wdsetctlr(wd)
1233 1.64 mycroft struct wd_softc *wd;
1234 1.3 deraadt {
1235 1.63 mycroft
1236 1.44 mycroft #ifdef WDDEBUG
1237 1.112 mycroft printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_dev.dv_unit, wd->sc_drive,
1238 1.144 thorpej wd->sc_dk.dk_label->d_ncylinders, wd->sc_dk.dk_label->d_ntracks,
1239 1.144 thorpej wd->sc_dk.dk_label->d_nsectors);
1240 1.44 mycroft #endif
1241 1.3 deraadt
1242 1.144 thorpej if (wdcommand(wd, WDCC_IDP, wd->sc_dk.dk_label->d_ncylinders,
1243 1.144 thorpej wd->sc_dk.dk_label->d_ntracks - 1, 0,
1244 1.144 thorpej wd->sc_dk.dk_label->d_nsectors) != 0) {
1245 1.105 mycroft wderror(wd, NULL, "wdsetctlr: geometry upload failed");
1246 1.51 mycroft return -1;
1247 1.57 mycroft }
1248 1.69 mycroft
1249 1.60 mycroft return 0;
1250 1.1 cgd }
1251 1.1 cgd
1252 1.1 cgd /*
1253 1.135 mycroft * Get the drive parameters, if ESDI or ATA, or create fake ones for ST506.
1254 1.1 cgd */
1255 1.108 mycroft int
1256 1.108 mycroft wd_get_parms(wd)
1257 1.64 mycroft struct wd_softc *wd;
1258 1.3 deraadt {
1259 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1260 1.63 mycroft int i;
1261 1.1 cgd char tb[DEV_BSIZE];
1262 1.124 mycroft int s, error;
1263 1.124 mycroft
1264 1.124 mycroft /*
1265 1.124 mycroft * XXX
1266 1.135 mycroft * The locking done here, and the length of time this may keep the rest
1267 1.135 mycroft * of the system suspended, is a kluge. This should be rewritten to
1268 1.135 mycroft * set up a transfer and queue it through wdstart(), but it's called
1269 1.135 mycroft * infrequently enough that this isn't a pressing matter.
1270 1.124 mycroft */
1271 1.124 mycroft
1272 1.124 mycroft s = splbio();
1273 1.124 mycroft
1274 1.124 mycroft while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
1275 1.124 mycroft wdc->sc_flags |= WDCF_WANTED;
1276 1.124 mycroft if ((error = tsleep(wdc, PRIBIO | PCATCH, "wdprm", 0)) != 0) {
1277 1.124 mycroft splx(s);
1278 1.124 mycroft return error;
1279 1.124 mycroft }
1280 1.124 mycroft }
1281 1.124 mycroft
1282 1.110 mycroft if (wdcommandshort(wdc, wd->sc_drive, WDCC_IDENTIFY) != 0 ||
1283 1.64 mycroft wait_for_drq(wdc) != 0) {
1284 1.60 mycroft /*
1285 1.105 mycroft * We `know' there's a drive here; just assume it's old.
1286 1.135 mycroft * This geometry is only used to read the MBR and print a
1287 1.135 mycroft * (false) attach message.
1288 1.60 mycroft */
1289 1.144 thorpej strncpy(wd->sc_dk.dk_label->d_typename, "ST506",
1290 1.144 thorpej sizeof wd->sc_dk.dk_label->d_typename);
1291 1.144 thorpej wd->sc_dk.dk_label->d_type = DTYPE_ST506;
1292 1.108 mycroft
1293 1.108 mycroft strncpy(wd->sc_params.wdp_model, "unknown",
1294 1.64 mycroft sizeof wd->sc_params.wdp_model);
1295 1.110 mycroft wd->sc_params.wdp_config = WD_CFG_FIXED;
1296 1.110 mycroft wd->sc_params.wdp_cylinders = 1024;
1297 1.108 mycroft wd->sc_params.wdp_heads = 8;
1298 1.108 mycroft wd->sc_params.wdp_sectors = 17;
1299 1.111 mycroft wd->sc_params.wdp_maxmulti = 0;
1300 1.111 mycroft wd->sc_params.wdp_usedmovsd = 0;
1301 1.110 mycroft wd->sc_params.wdp_capabilities = 0;
1302 1.60 mycroft } else {
1303 1.144 thorpej strncpy(wd->sc_dk.dk_label->d_typename, "ESDI/IDE",
1304 1.144 thorpej sizeof wd->sc_dk.dk_label->d_typename);
1305 1.144 thorpej wd->sc_dk.dk_label->d_type = DTYPE_ESDI;
1306 1.124 mycroft
1307 1.135 mycroft /* Read in parameter block. */
1308 1.64 mycroft insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
1309 1.108 mycroft bcopy(tb, &wd->sc_params, sizeof(struct wdparams));
1310 1.13 deraadt
1311 1.64 mycroft /* Shuffle string byte order. */
1312 1.108 mycroft for (i = 0; i < sizeof(wd->sc_params.wdp_model); i += 2) {
1313 1.13 deraadt u_short *p;
1314 1.108 mycroft p = (u_short *)(wd->sc_params.wdp_model + i);
1315 1.13 deraadt *p = ntohs(*p);
1316 1.13 deraadt }
1317 1.8 deraadt }
1318 1.13 deraadt
1319 1.124 mycroft /* Clear any leftover interrupt. */
1320 1.64 mycroft (void) inb(wdc->sc_iobase+wd_status);
1321 1.108 mycroft
1322 1.135 mycroft /* Restart the queue. */
1323 1.124 mycroft wdcstart(wdc);
1324 1.124 mycroft
1325 1.124 mycroft splx(s);
1326 1.108 mycroft return 0;
1327 1.1 cgd }
1328 1.1 cgd
1329 1.1 cgd int
1330 1.132 mycroft wdioctl(dev, cmd, addr, flag, p)
1331 1.55 mycroft dev_t dev;
1332 1.132 mycroft u_long cmd;
1333 1.55 mycroft caddr_t addr;
1334 1.55 mycroft int flag;
1335 1.55 mycroft struct proc *p;
1336 1.1 cgd {
1337 1.147 thorpej struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
1338 1.54 mycroft int error;
1339 1.3 deraadt
1340 1.108 mycroft if ((wd->sc_flags & WDF_LOADED) == 0)
1341 1.108 mycroft return EIO;
1342 1.108 mycroft
1343 1.132 mycroft switch (cmd) {
1344 1.1 cgd case DIOCSBAD:
1345 1.3 deraadt if ((flag & FWRITE) == 0)
1346 1.54 mycroft return EBADF;
1347 1.144 thorpej wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1348 1.144 thorpej wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1349 1.64 mycroft bad144intern(wd);
1350 1.54 mycroft return 0;
1351 1.1 cgd
1352 1.1 cgd case DIOCGDINFO:
1353 1.144 thorpej *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1354 1.54 mycroft return 0;
1355 1.3 deraadt
1356 1.3 deraadt case DIOCGPART:
1357 1.144 thorpej ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1358 1.3 deraadt ((struct partinfo *)addr)->part =
1359 1.144 thorpej &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1360 1.54 mycroft return 0;
1361 1.3 deraadt
1362 1.132 mycroft case DIOCWDINFO:
1363 1.3 deraadt case DIOCSDINFO:
1364 1.3 deraadt if ((flag & FWRITE) == 0)
1365 1.54 mycroft return EBADF;
1366 1.132 mycroft
1367 1.149 christos if ((error = wdlock(wd)) != 0)
1368 1.132 mycroft return error;
1369 1.135 mycroft wd->sc_flags |= WDF_LABELLING;
1370 1.132 mycroft
1371 1.144 thorpej error = setdisklabel(wd->sc_dk.dk_label,
1372 1.128 mycroft (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
1373 1.144 thorpej wd->sc_dk.dk_cpulabel);
1374 1.3 deraadt if (error == 0) {
1375 1.69 mycroft if (wd->sc_state > GEOMETRY)
1376 1.69 mycroft wd->sc_state = GEOMETRY;
1377 1.132 mycroft if (cmd == DIOCWDINFO)
1378 1.132 mycroft error = writedisklabel(WDLABELDEV(dev),
1379 1.144 thorpej wdstrategy, wd->sc_dk.dk_label,
1380 1.144 thorpej wd->sc_dk.dk_cpulabel);
1381 1.1 cgd }
1382 1.132 mycroft
1383 1.132 mycroft wd->sc_flags &= ~WDF_LABELLING;
1384 1.132 mycroft wdunlock(wd);
1385 1.54 mycroft return error;
1386 1.3 deraadt
1387 1.44 mycroft case DIOCWLABEL:
1388 1.3 deraadt if ((flag & FWRITE) == 0)
1389 1.54 mycroft return EBADF;
1390 1.93 mycroft if (*(int *)addr)
1391 1.93 mycroft wd->sc_flags |= WDF_WLABEL;
1392 1.93 mycroft else
1393 1.93 mycroft wd->sc_flags &= ~WDF_WLABEL;
1394 1.54 mycroft return 0;
1395 1.3 deraadt
1396 1.1 cgd #ifdef notyet
1397 1.1 cgd case DIOCWFORMAT:
1398 1.1 cgd if ((flag & FWRITE) == 0)
1399 1.54 mycroft return EBADF;
1400 1.54 mycroft {
1401 1.54 mycroft register struct format_op *fop;
1402 1.54 mycroft struct iovec aiov;
1403 1.54 mycroft struct uio auio;
1404 1.3 deraadt
1405 1.54 mycroft fop = (struct format_op *)addr;
1406 1.54 mycroft aiov.iov_base = fop->df_buf;
1407 1.54 mycroft aiov.iov_len = fop->df_count;
1408 1.54 mycroft auio.uio_iov = &aiov;
1409 1.54 mycroft auio.uio_iovcnt = 1;
1410 1.54 mycroft auio.uio_resid = fop->df_count;
1411 1.54 mycroft auio.uio_segflg = 0;
1412 1.54 mycroft auio.uio_offset =
1413 1.144 thorpej fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1414 1.83 pk auio.uio_procp = p;
1415 1.64 mycroft error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1416 1.64 mycroft &auio);
1417 1.54 mycroft fop->df_count -= auio.uio_resid;
1418 1.64 mycroft fop->df_reg[0] = wdc->sc_status;
1419 1.64 mycroft fop->df_reg[1] = wdc->sc_error;
1420 1.54 mycroft return error;
1421 1.54 mycroft }
1422 1.1 cgd #endif
1423 1.144 thorpej
1424 1.1 cgd default:
1425 1.54 mycroft return ENOTTY;
1426 1.1 cgd }
1427 1.54 mycroft
1428 1.54 mycroft #ifdef DIAGNOSTIC
1429 1.54 mycroft panic("wdioctl: impossible");
1430 1.54 mycroft #endif
1431 1.1 cgd }
1432 1.1 cgd
1433 1.44 mycroft #ifdef B_FORMAT
1434 1.1 cgd int
1435 1.1 cgd wdformat(struct buf *bp)
1436 1.1 cgd {
1437 1.44 mycroft
1438 1.1 cgd bp->b_flags |= B_FORMAT;
1439 1.37 mycroft return wdstrategy(bp);
1440 1.1 cgd }
1441 1.1 cgd #endif
1442 1.1 cgd
1443 1.1 cgd int
1444 1.55 mycroft wdsize(dev)
1445 1.55 mycroft dev_t dev;
1446 1.1 cgd {
1447 1.64 mycroft struct wd_softc *wd;
1448 1.108 mycroft int part;
1449 1.108 mycroft int size;
1450 1.3 deraadt
1451 1.149 christos if (wdopen(dev, 0, S_IFBLK, NULL) != 0)
1452 1.37 mycroft return -1;
1453 1.147 thorpej wd = wd_cd.cd_devs[WDUNIT(dev)];
1454 1.108 mycroft part = WDPART(dev);
1455 1.144 thorpej if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1456 1.108 mycroft size = -1;
1457 1.108 mycroft else
1458 1.144 thorpej size = wd->sc_dk.dk_label->d_partitions[part].p_size;
1459 1.149 christos if (wdclose(dev, 0, S_IFBLK, NULL) != 0)
1460 1.37 mycroft return -1;
1461 1.108 mycroft return size;
1462 1.1 cgd }
1463 1.1 cgd
1464 1.140 cgd
1465 1.140 cgd #ifndef __BDEVSW_DUMP_OLD_TYPE
1466 1.140 cgd /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1467 1.140 cgd static int wddoingadump;
1468 1.140 cgd static int wddumprecalibrated;
1469 1.140 cgd
1470 1.64 mycroft /*
1471 1.64 mycroft * Dump core after a system crash.
1472 1.64 mycroft */
1473 1.1 cgd int
1474 1.140 cgd wddump(dev, blkno, va, size)
1475 1.140 cgd dev_t dev;
1476 1.140 cgd daddr_t blkno;
1477 1.140 cgd caddr_t va;
1478 1.140 cgd size_t size;
1479 1.140 cgd {
1480 1.140 cgd struct wd_softc *wd; /* disk unit to do the I/O */
1481 1.140 cgd struct wdc_softc *wdc; /* disk controller to do the I/O */
1482 1.140 cgd struct disklabel *lp; /* disk's disklabel */
1483 1.140 cgd int unit, part;
1484 1.142 mycroft int nblks; /* total number of sectors left to write */
1485 1.140 cgd
1486 1.140 cgd /* Check if recursive dump; if so, punt. */
1487 1.116 mycroft if (wddoingadump)
1488 1.116 mycroft return EFAULT;
1489 1.142 mycroft wddoingadump = 1;
1490 1.140 cgd
1491 1.142 mycroft unit = WDUNIT(dev);
1492 1.147 thorpej if (unit >= wd_cd.cd_ndevs)
1493 1.142 mycroft return ENXIO;
1494 1.147 thorpej wd = wd_cd.cd_devs[unit];
1495 1.142 mycroft if (wd == 0)
1496 1.142 mycroft return ENXIO;
1497 1.60 mycroft
1498 1.140 cgd part = WDPART(dev);
1499 1.140 cgd
1500 1.140 cgd /* Make sure it was initialized. */
1501 1.140 cgd if (wd->sc_state < OPEN)
1502 1.37 mycroft return ENXIO;
1503 1.140 cgd
1504 1.142 mycroft wdc = (void *)wd->sc_dev.dv_parent;
1505 1.46 mycroft
1506 1.140 cgd /* Convert to disk sectors. Request must be a multiple of size. */
1507 1.144 thorpej lp = wd->sc_dk.dk_label;
1508 1.142 mycroft if ((size % lp->d_secsize) != 0)
1509 1.140 cgd return EFAULT;
1510 1.142 mycroft nblks = size / lp->d_secsize;
1511 1.142 mycroft blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1512 1.116 mycroft
1513 1.64 mycroft /* Check transfer bounds against partition size. */
1514 1.142 mycroft if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1515 1.140 cgd return EINVAL;
1516 1.140 cgd
1517 1.140 cgd /* Offset block number to start of partition. */
1518 1.142 mycroft blkno += lp->d_partitions[part].p_offset;
1519 1.60 mycroft
1520 1.140 cgd /* Recalibrate, if first dump transfer. */
1521 1.140 cgd if (wddumprecalibrated == 0) {
1522 1.140 cgd wddumprecalibrated = 1;
1523 1.140 cgd if (wdcommandshort(wdc, wd->sc_drive, WDCC_RECAL) != 0 ||
1524 1.140 cgd wait_for_ready(wdc) != 0 || wdsetctlr(wd) != 0 ||
1525 1.140 cgd wait_for_ready(wdc) != 0) {
1526 1.140 cgd wderror(wd, NULL, "wddump: recal failed");
1527 1.140 cgd return EIO;
1528 1.140 cgd }
1529 1.63 mycroft }
1530 1.140 cgd
1531 1.142 mycroft while (nblks > 0) {
1532 1.142 mycroft daddr_t xlt_blkno = blkno;
1533 1.116 mycroft long cylin, head, sector;
1534 1.116 mycroft
1535 1.116 mycroft if ((lp->d_flags & D_BADSECT) != 0) {
1536 1.116 mycroft long blkdiff;
1537 1.3 deraadt int i;
1538 1.3 deraadt
1539 1.116 mycroft for (i = 0; (blkdiff = wd->sc_badsect[i]) != -1; i++) {
1540 1.140 cgd blkdiff -= xlt_blkno;
1541 1.116 mycroft if (blkdiff < 0)
1542 1.116 mycroft continue;
1543 1.116 mycroft if (blkdiff == 0) {
1544 1.116 mycroft /* Replace current block of transfer. */
1545 1.140 cgd xlt_blkno = lp->d_secperunit -
1546 1.140 cgd lp->d_nsectors - i - 1;
1547 1.3 deraadt }
1548 1.116 mycroft break;
1549 1.1 cgd }
1550 1.116 mycroft /* Tranfer is okay now. */
1551 1.116 mycroft }
1552 1.116 mycroft
1553 1.116 mycroft if ((wd->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
1554 1.140 cgd sector = (xlt_blkno >> 0) & 0xff;
1555 1.140 cgd cylin = (xlt_blkno >> 8) & 0xffff;
1556 1.140 cgd head = (xlt_blkno >> 24) & 0xf;
1557 1.116 mycroft head |= WDSD_LBA;
1558 1.116 mycroft } else {
1559 1.140 cgd sector = xlt_blkno % lp->d_nsectors;
1560 1.116 mycroft sector++; /* Sectors begin with 1, not 0. */
1561 1.140 cgd xlt_blkno /= lp->d_nsectors;
1562 1.140 cgd head = xlt_blkno % lp->d_ntracks;
1563 1.140 cgd xlt_blkno /= lp->d_ntracks;
1564 1.140 cgd cylin = xlt_blkno;
1565 1.116 mycroft head |= WDSD_CHS;
1566 1.3 deraadt }
1567 1.140 cgd
1568 1.140 cgd #ifndef WD_DUMP_NOT_TRUSTED
1569 1.105 mycroft if (wdcommand(wd, WDCC_WRITE, cylin, head, sector, 1) != 0 ||
1570 1.105 mycroft wait_for_drq(wdc) != 0) {
1571 1.105 mycroft wderror(wd, NULL, "wddump: write failed");
1572 1.51 mycroft return EIO;
1573 1.63 mycroft }
1574 1.3 deraadt
1575 1.142 mycroft outsw(wdc->sc_iobase+wd_data, va, lp->d_secsize >> 1);
1576 1.3 deraadt
1577 1.13 deraadt /* Check data request (should be done). */
1578 1.64 mycroft if (wait_for_ready(wdc) != 0) {
1579 1.64 mycroft wderror(wd, NULL, "wddump: timeout waiting for ready");
1580 1.63 mycroft return EIO;
1581 1.63 mycroft }
1582 1.140 cgd #else /* WD_DUMP_NOT_TRUSTED */
1583 1.140 cgd /* Let's just talk about this first... */
1584 1.140 cgd printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1585 1.140 cgd unit, va, cylin, head, sector);
1586 1.140 cgd delay(500 * 1000); /* half a second */
1587 1.140 cgd #endif
1588 1.1 cgd
1589 1.140 cgd /* update block count */
1590 1.142 mycroft nblks -= 1;
1591 1.142 mycroft blkno += 1;
1592 1.142 mycroft va += lp->d_secsize;
1593 1.1 cgd }
1594 1.142 mycroft
1595 1.140 cgd wddoingadump = 0;
1596 1.140 cgd return 0;
1597 1.140 cgd }
1598 1.140 cgd #else /* __BDEVSW_DUMP_NEW_TYPE */
1599 1.140 cgd int
1600 1.140 cgd wddump(dev, blkno, va, size)
1601 1.140 cgd dev_t dev;
1602 1.140 cgd daddr_t blkno;
1603 1.140 cgd caddr_t va;
1604 1.140 cgd size_t size;
1605 1.140 cgd {
1606 1.116 mycroft
1607 1.140 cgd /* Not implemented. */
1608 1.140 cgd return ENXIO;
1609 1.1 cgd }
1610 1.140 cgd #endif /* __BDEVSW_DUMP_NEW_TYPE */
1611 1.3 deraadt
1612 1.3 deraadt /*
1613 1.3 deraadt * Internalize the bad sector table.
1614 1.3 deraadt */
1615 1.3 deraadt void
1616 1.64 mycroft bad144intern(wd)
1617 1.64 mycroft struct wd_softc *wd;
1618 1.3 deraadt {
1619 1.144 thorpej struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1620 1.144 thorpej struct disklabel *lp = wd->sc_dk.dk_label;
1621 1.98 mycroft int i = 0;
1622 1.55 mycroft
1623 1.98 mycroft for (; i < 126; i++) {
1624 1.98 mycroft if (bt->bt_bad[i].bt_cyl == 0xffff)
1625 1.55 mycroft break;
1626 1.64 mycroft wd->sc_badsect[i] =
1627 1.98 mycroft bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1628 1.98 mycroft (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1629 1.98 mycroft (bt->bt_bad[i].bt_trksec & 0xff);
1630 1.3 deraadt }
1631 1.98 mycroft for (; i < 127; i++)
1632 1.98 mycroft wd->sc_badsect[i] = -1;
1633 1.3 deraadt }
1634 1.3 deraadt
1635 1.135 mycroft int
1636 1.64 mycroft wdcreset(wdc)
1637 1.64 mycroft struct wdc_softc *wdc;
1638 1.21 deraadt {
1639 1.107 mycroft int iobase = wdc->sc_iobase;
1640 1.21 deraadt
1641 1.64 mycroft /* Reset the device. */
1642 1.64 mycroft outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1643 1.61 mycroft delay(1000);
1644 1.64 mycroft outb(iobase+wd_ctlr, WDCTL_IDS);
1645 1.61 mycroft delay(1000);
1646 1.64 mycroft (void) inb(iobase+wd_error);
1647 1.64 mycroft outb(iobase+wd_ctlr, WDCTL_4BIT);
1648 1.64 mycroft
1649 1.64 mycroft if (wait_for_unbusy(wdc) < 0) {
1650 1.64 mycroft printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1651 1.64 mycroft return 1;
1652 1.64 mycroft }
1653 1.64 mycroft
1654 1.64 mycroft return 0;
1655 1.64 mycroft }
1656 1.64 mycroft
1657 1.135 mycroft void
1658 1.81 cgd wdcrestart(arg)
1659 1.81 cgd void *arg;
1660 1.64 mycroft {
1661 1.135 mycroft struct wdc_softc *wdc = arg;
1662 1.98 mycroft int s;
1663 1.64 mycroft
1664 1.98 mycroft s = splbio();
1665 1.64 mycroft wdcstart(wdc);
1666 1.64 mycroft splx(s);
1667 1.64 mycroft }
1668 1.64 mycroft
1669 1.64 mycroft /*
1670 1.64 mycroft * Unwedge the controller after an unexpected error. We do this by resetting
1671 1.64 mycroft * it, marking all drives for recalibration, and stalling the queue for a short
1672 1.64 mycroft * period to give the reset time to finish.
1673 1.64 mycroft * NOTE: We use a timeout here, so this routine must not be called during
1674 1.64 mycroft * autoconfig or dump.
1675 1.64 mycroft */
1676 1.135 mycroft void
1677 1.64 mycroft wdcunwedge(wdc)
1678 1.64 mycroft struct wdc_softc *wdc;
1679 1.64 mycroft {
1680 1.108 mycroft int unit;
1681 1.64 mycroft
1682 1.98 mycroft untimeout(wdctimeout, wdc);
1683 1.64 mycroft (void) wdcreset(wdc);
1684 1.21 deraadt
1685 1.64 mycroft /* Schedule recalibrate for all drives on this controller. */
1686 1.147 thorpej for (unit = 0; unit < wd_cd.cd_ndevs; unit++) {
1687 1.147 thorpej struct wd_softc *wd = wd_cd.cd_devs[unit];
1688 1.64 mycroft if (!wd || (void *)wd->sc_dev.dv_parent != wdc)
1689 1.64 mycroft continue;
1690 1.69 mycroft if (wd->sc_state > RECAL)
1691 1.69 mycroft wd->sc_state = RECAL;
1692 1.64 mycroft }
1693 1.64 mycroft
1694 1.69 mycroft wdc->sc_flags |= WDCF_ERROR;
1695 1.78 mycroft ++wdc->sc_errors;
1696 1.64 mycroft
1697 1.64 mycroft /* Wake up in a little bit and restart the operation. */
1698 1.82 mycroft timeout(wdcrestart, wdc, RECOVERYTIME);
1699 1.40 mycroft }
1700 1.40 mycroft
1701 1.40 mycroft int
1702 1.64 mycroft wdcwait(wdc, mask)
1703 1.64 mycroft struct wdc_softc *wdc;
1704 1.49 mycroft int mask;
1705 1.40 mycroft {
1706 1.107 mycroft int iobase = wdc->sc_iobase;
1707 1.40 mycroft int timeout = 0;
1708 1.63 mycroft u_char status;
1709 1.149 christos #ifdef WDCNDELAY_DEBUG
1710 1.87 mycroft extern int cold;
1711 1.149 christos #endif
1712 1.40 mycroft
1713 1.40 mycroft for (;;) {
1714 1.87 mycroft wdc->sc_status = status = inb(iobase+wd_status);
1715 1.110 mycroft if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
1716 1.54 mycroft break;
1717 1.40 mycroft if (++timeout > WDCNDELAY)
1718 1.40 mycroft return -1;
1719 1.61 mycroft delay(WDCDELAY);
1720 1.21 deraadt }
1721 1.87 mycroft if (status & WDCS_ERR) {
1722 1.64 mycroft wdc->sc_error = inb(iobase+wd_error);
1723 1.87 mycroft return WDCS_ERR;
1724 1.87 mycroft }
1725 1.23 deraadt #ifdef WDCNDELAY_DEBUG
1726 1.87 mycroft /* After autoconfig, there should be no long delays. */
1727 1.87 mycroft if (!cold && timeout > WDCNDELAY_DEBUG)
1728 1.87 mycroft printf("%s: warning: busy-wait took %dus\n",
1729 1.73 mycroft wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1730 1.23 deraadt #endif
1731 1.87 mycroft return 0;
1732 1.27 cgd }
1733 1.27 cgd
1734 1.135 mycroft void
1735 1.81 cgd wdctimeout(arg)
1736 1.81 cgd void *arg;
1737 1.27 cgd {
1738 1.81 cgd struct wdc_softc *wdc = (struct wdc_softc *)arg;
1739 1.98 mycroft int s;
1740 1.27 cgd
1741 1.98 mycroft s = splbio();
1742 1.120 mycroft if ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
1743 1.145 mycroft struct wd_softc *wd = wdc->sc_drives.tqh_first;
1744 1.145 mycroft struct buf *bp = wd->sc_q.b_actf;
1745 1.145 mycroft
1746 1.120 mycroft wdc->sc_flags &= ~WDCF_ACTIVE;
1747 1.120 mycroft wderror(wdc, NULL, "lost interrupt");
1748 1.145 mycroft printf("%s: lost interrupt: %sing %d@%s:%d\n",
1749 1.145 mycroft wdc->sc_dev.dv_xname,
1750 1.145 mycroft (bp->b_flags & B_READ) ? "read" : "writ",
1751 1.145 mycroft wd->sc_nblks, wd->sc_dev.dv_xname, wd->sc_blkno);
1752 1.120 mycroft wdcunwedge(wdc);
1753 1.120 mycroft } else
1754 1.120 mycroft wderror(wdc, NULL, "missing untimeout");
1755 1.55 mycroft splx(s);
1756 1.63 mycroft }
1757 1.63 mycroft
1758 1.135 mycroft void
1759 1.64 mycroft wderror(dev, bp, msg)
1760 1.64 mycroft void *dev;
1761 1.63 mycroft struct buf *bp;
1762 1.63 mycroft char *msg;
1763 1.63 mycroft {
1764 1.64 mycroft struct wd_softc *wd = dev;
1765 1.64 mycroft struct wdc_softc *wdc = dev;
1766 1.64 mycroft
1767 1.114 mycroft if (bp) {
1768 1.125 mycroft diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip / DEV_BSIZE,
1769 1.144 thorpej wd->sc_dk.dk_label);
1770 1.114 mycroft printf("\n");
1771 1.114 mycroft } else
1772 1.64 mycroft printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
1773 1.64 mycroft msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
1774 1.21 deraadt }
1775