wd.c revision 1.171 1 1.171 bouyer /* $NetBSD: wd.c,v 1.171 1998/04/23 13:30:39 bouyer 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.168 cgd * Bus_space-ified by Christopher G. Demetriou.
10 1.168 cgd *
11 1.113 mycroft * Redistribution and use in source and binary forms, with or without
12 1.113 mycroft * modification, are permitted provided that the following conditions
13 1.113 mycroft * are met:
14 1.113 mycroft * 1. Redistributions of source code must retain the above copyright
15 1.113 mycroft * notice, this list of conditions and the following disclaimer.
16 1.113 mycroft * 2. Redistributions in binary form must reproduce the above copyright
17 1.113 mycroft * notice, this list of conditions and the following disclaimer in the
18 1.113 mycroft * documentation and/or other materials provided with the distribution.
19 1.113 mycroft * 3. All advertising materials mentioning features or use of this software
20 1.113 mycroft * must display the following acknowledgement:
21 1.135 mycroft * This product includes software developed by Charles M. Hannum.
22 1.113 mycroft * 4. The name of the author may not be used to endorse or promote products
23 1.113 mycroft * derived from this software without specific prior written permission.
24 1.113 mycroft *
25 1.135 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 1.135 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 1.135 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 1.135 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 1.135 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 1.135 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 1.135 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 1.135 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 1.135 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 1.135 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.166 explorer #include "rnd.h"
38 1.166 explorer
39 1.29 mycroft #include <sys/param.h>
40 1.29 mycroft #include <sys/systm.h>
41 1.43 mycroft #include <sys/kernel.h>
42 1.29 mycroft #include <sys/conf.h>
43 1.29 mycroft #include <sys/file.h>
44 1.29 mycroft #include <sys/stat.h>
45 1.29 mycroft #include <sys/ioctl.h>
46 1.29 mycroft #include <sys/buf.h>
47 1.29 mycroft #include <sys/uio.h>
48 1.29 mycroft #include <sys/malloc.h>
49 1.64 mycroft #include <sys/device.h>
50 1.94 mycroft #include <sys/disklabel.h>
51 1.94 mycroft #include <sys/disk.h>
52 1.29 mycroft #include <sys/syslog.h>
53 1.149 christos #include <sys/proc.h>
54 1.166 explorer #if NRND > 0
55 1.165 explorer #include <sys/rnd.h>
56 1.166 explorer #endif
57 1.29 mycroft
58 1.29 mycroft #include <vm/vm.h>
59 1.29 mycroft
60 1.150 mycroft #include <machine/intr.h>
61 1.168 cgd #include <machine/bus.h>
62 1.29 mycroft
63 1.168 cgd #include <dev/ic/wdcreg.h> /* XXX */
64 1.168 cgd #include <dev/ic/wdcvar.h> /* XXX */
65 1.168 cgd #include <dev/ata/wdlink.h>
66 1.163 bouyer #include "locators.h"
67 1.1 cgd
68 1.98 mycroft #define WAITTIME (4 * hz) /* time to wait for a completion */
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.163 bouyer #ifdef WDDEBUG
79 1.163 bouyer #define WDDEBUG_PRINT(args) printf args
80 1.163 bouyer #else
81 1.163 bouyer #define WDDEBUG_PRINT(args)
82 1.163 bouyer #endif
83 1.163 bouyer
84 1.64 mycroft struct wd_softc {
85 1.64 mycroft struct device sc_dev;
86 1.144 thorpej struct disk sc_dk;
87 1.163 bouyer struct wd_link *d_link;
88 1.64 mycroft struct buf sc_q;
89 1.166 explorer #if NRND > 0
90 1.165 explorer rndsource_element_t rnd_source;
91 1.166 explorer #endif
92 1.74 mycroft };
93 1.64 mycroft
94 1.168 cgd #ifdef __BROKEN_INDIRECT_CONFIG
95 1.163 bouyer int wdprobe __P((struct device *, void *, void *));
96 1.168 cgd #else
97 1.168 cgd int wdprobe __P((struct device *, struct cfdata *, void *));
98 1.168 cgd #endif
99 1.163 bouyer void wdattach __P((struct device *, struct device *, void *));
100 1.163 bouyer int wdprint __P((void *, char *));
101 1.102 mycroft
102 1.147 thorpej struct cfattach wd_ca = {
103 1.147 thorpej sizeof(struct wd_softc), wdprobe, wdattach
104 1.147 thorpej };
105 1.147 thorpej
106 1.167 thorpej extern struct cfdriver wd_cd;
107 1.108 mycroft
108 1.164 thorpej void wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
109 1.149 christos void wdgetdisklabel __P((struct wd_softc *));
110 1.149 christos int wd_get_parms __P((struct wd_softc *));
111 1.149 christos void wdstrategy __P((struct buf *));
112 1.163 bouyer void wdstart __P((void *));
113 1.108 mycroft
114 1.102 mycroft struct dkdriver wddkdriver = { wdstrategy };
115 1.65 mycroft
116 1.149 christos /* XXX: these should go elsewhere */
117 1.149 christos cdev_decl(wd);
118 1.149 christos bdev_decl(wd);
119 1.149 christos
120 1.149 christos void wdfinish __P((struct wd_softc *, struct buf *));
121 1.163 bouyer int wdsetctlr __P((struct wd_link *));
122 1.170 leo #ifdef HAS_BAD144_HANDLING
123 1.64 mycroft static void bad144intern __P((struct wd_softc *));
124 1.170 leo #endif
125 1.163 bouyer int wdlock __P((struct wd_link *));
126 1.163 bouyer void wdunlock __P((struct wd_link *));
127 1.1 cgd
128 1.1 cgd int
129 1.168 cgd #ifdef __BROKEN_INDIRECT_CONFIG
130 1.168 cgd wdprobe(parent, matchv, aux)
131 1.168 cgd #else
132 1.163 bouyer wdprobe(parent, match, aux)
133 1.168 cgd #endif
134 1.102 mycroft struct device *parent;
135 1.168 cgd #ifdef __BROKEN_INDIRECT_CONFIG
136 1.168 cgd void *matchv;
137 1.168 cgd #else
138 1.168 cgd struct cfdata *match;
139 1.168 cgd #endif
140 1.168 cgd
141 1.168 cgd void *aux;
142 1.1 cgd {
143 1.168 cgd #ifdef __BROKEN_INDIRECT_CONFIG
144 1.168 cgd struct cfdata *match = matchv;
145 1.168 cgd #endif
146 1.163 bouyer struct wd_link *d_link = aux;
147 1.163 bouyer int drive;
148 1.167 thorpej
149 1.163 bouyer if (d_link == NULL)
150 1.64 mycroft return 0;
151 1.163 bouyer if (d_link->type != ATA)
152 1.105 mycroft return 0;
153 1.105 mycroft
154 1.163 bouyer drive = d_link->drive;
155 1.168 cgd if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
156 1.168 cgd match->cf_loc[ATACF_DRIVE] != drive)
157 1.74 mycroft return 0;
158 1.64 mycroft
159 1.74 mycroft return 1;
160 1.74 mycroft }
161 1.64 mycroft
162 1.74 mycroft void
163 1.74 mycroft wdattach(parent, self, aux)
164 1.74 mycroft struct device *parent, *self;
165 1.74 mycroft void *aux;
166 1.74 mycroft {
167 1.74 mycroft struct wd_softc *wd = (void *)self;
168 1.163 bouyer struct wd_link *d_link= aux;
169 1.74 mycroft int i, blank;
170 1.135 mycroft char buf[41], c, *p, *q;
171 1.56 mycroft
172 1.163 bouyer wd->d_link = d_link;
173 1.163 bouyer d_link->openings = 1;
174 1.163 bouyer d_link->wd_softc = (caddr_t)wd;
175 1.106 mycroft
176 1.144 thorpej /*
177 1.144 thorpej * Initialize and attach the disk structure.
178 1.144 thorpej */
179 1.144 thorpej wd->sc_dk.dk_driver = &wddkdriver;
180 1.144 thorpej wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
181 1.144 thorpej disk_attach(&wd->sc_dk);
182 1.144 thorpej
183 1.163 bouyer d_link->sc_lp = wd->sc_dk.dk_label;
184 1.163 bouyer
185 1.163 bouyer wdc_get_parms((struct wdc_softc *)d_link->wdc_softc, d_link);
186 1.163 bouyer for (blank = 0, p = d_link->sc_params.wdp_model, q = buf, i = 0;
187 1.163 bouyer i < sizeof(d_link->sc_params.wdp_model); i++) {
188 1.135 mycroft c = *p++;
189 1.135 mycroft if (c == '\0')
190 1.135 mycroft break;
191 1.135 mycroft if (c != ' ') {
192 1.135 mycroft if (blank) {
193 1.135 mycroft *q++ = ' ';
194 1.135 mycroft blank = 0;
195 1.135 mycroft }
196 1.135 mycroft *q++ = c;
197 1.135 mycroft } else
198 1.135 mycroft blank = 1;
199 1.135 mycroft }
200 1.135 mycroft *q++ = '\0';
201 1.135 mycroft
202 1.155 thorpej printf(": <%s>\n%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
203 1.163 bouyer buf, self->dv_xname,
204 1.163 bouyer d_link->sc_params.wdp_cylinders *
205 1.163 bouyer (d_link->sc_params.wdp_heads * d_link->sc_params.wdp_sectors) /
206 1.163 bouyer (1048576 / DEV_BSIZE),
207 1.163 bouyer d_link->sc_params.wdp_cylinders,
208 1.163 bouyer d_link->sc_params.wdp_heads,
209 1.163 bouyer d_link->sc_params.wdp_sectors,
210 1.155 thorpej DEV_BSIZE);
211 1.113 mycroft
212 1.163 bouyer if ((d_link->sc_params.wdp_capabilities & WD_CAP_DMA) != 0 &&
213 1.163 bouyer d_link->sc_mode == WDM_DMA) {
214 1.163 bouyer d_link->sc_mode = WDM_DMA;
215 1.163 bouyer } else if (d_link->sc_params.wdp_maxmulti > 1) {
216 1.163 bouyer d_link->sc_mode = WDM_PIOMULTI;
217 1.163 bouyer d_link->sc_multiple = min(d_link->sc_params.wdp_maxmulti, 16);
218 1.113 mycroft } else {
219 1.163 bouyer d_link->sc_mode = WDM_PIOSINGLE;
220 1.163 bouyer d_link->sc_multiple = 1;
221 1.113 mycroft }
222 1.113 mycroft
223 1.153 christos printf("%s: using", wd->sc_dev.dv_xname);
224 1.163 bouyer if (d_link->sc_mode == WDM_DMA)
225 1.153 christos printf(" dma transfers,");
226 1.113 mycroft else
227 1.153 christos printf(" %d-sector %d-bit pio transfers,",
228 1.163 bouyer d_link->sc_multiple,
229 1.168 cgd (((struct wdc_softc *)d_link->wdc_softc)->sc_cap &
230 1.168 cgd WDC_CAPABILITY_DATA32) == 0 ? 16 : 32);
231 1.163 bouyer if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
232 1.153 christos printf(" lba addressing\n");
233 1.116 mycroft else
234 1.153 christos printf(" chs addressing\n");
235 1.165 explorer
236 1.166 explorer #if NRND > 0
237 1.165 explorer rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname, RND_TYPE_DISK);
238 1.166 explorer #endif
239 1.1 cgd }
240 1.1 cgd
241 1.64 mycroft /*
242 1.135 mycroft * Read/write routine for a buffer. Validates the arguments and schedules the
243 1.135 mycroft * transfer. Does not wait for the transfer to complete.
244 1.1 cgd */
245 1.64 mycroft void
246 1.55 mycroft wdstrategy(bp)
247 1.55 mycroft struct buf *bp;
248 1.1 cgd {
249 1.147 thorpej struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
250 1.163 bouyer struct wd_link *d_link= wd->d_link;
251 1.37 mycroft int s;
252 1.3 deraadt
253 1.135 mycroft /* Valid request? */
254 1.135 mycroft if (bp->b_blkno < 0 ||
255 1.144 thorpej (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
256 1.144 thorpej (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
257 1.1 cgd bp->b_error = EINVAL;
258 1.93 mycroft goto bad;
259 1.1 cgd }
260 1.3 deraadt
261 1.135 mycroft /* If device invalidated (e.g. media change, door open), error. */
262 1.163 bouyer if ((d_link->sc_flags & WDF_LOADED) == 0) {
263 1.135 mycroft bp->b_error = EIO;
264 1.135 mycroft goto bad;
265 1.135 mycroft }
266 1.135 mycroft
267 1.93 mycroft /* If it's a null transfer, return immediately. */
268 1.93 mycroft if (bp->b_bcount == 0)
269 1.93 mycroft goto done;
270 1.93 mycroft
271 1.128 mycroft /*
272 1.128 mycroft * Do bounds checking, adjust transfer. if error, process.
273 1.128 mycroft * If end of partition, just return.
274 1.128 mycroft */
275 1.138 mycroft if (WDPART(bp->b_dev) != RAW_PART &&
276 1.144 thorpej bounds_check_with_label(bp, wd->sc_dk.dk_label,
277 1.163 bouyer (d_link->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
278 1.128 mycroft goto done;
279 1.3 deraadt
280 1.64 mycroft /* Queue transfer on drive, activate drive and controller if idle. */
281 1.1 cgd s = splbio();
282 1.113 mycroft disksort(&wd->sc_q, bp);
283 1.163 bouyer wdstart(wd);
284 1.1 cgd splx(s);
285 1.64 mycroft return;
286 1.3 deraadt
287 1.93 mycroft bad:
288 1.93 mycroft bp->b_flags |= B_ERROR;
289 1.1 cgd done:
290 1.64 mycroft /* Toss transfer; we're done early. */
291 1.135 mycroft bp->b_resid = bp->b_bcount;
292 1.1 cgd biodone(bp);
293 1.1 cgd }
294 1.1 cgd
295 1.1 cgd /*
296 1.135 mycroft * Queue a drive for I/O.
297 1.1 cgd */
298 1.108 mycroft void
299 1.163 bouyer wdstart(arg)
300 1.163 bouyer void *arg;
301 1.1 cgd {
302 1.163 bouyer struct wd_softc *wd = arg;
303 1.163 bouyer struct buf *dp, *bp=0;
304 1.163 bouyer struct wd_link *d_link = wd->d_link;
305 1.163 bouyer struct wdc_xfer *xfer;
306 1.171 bouyer daddr_t blkno, p_offset;
307 1.163 bouyer
308 1.163 bouyer while (d_link->openings > 0) {
309 1.163 bouyer
310 1.163 bouyer /* Is there a buf for us ? */
311 1.163 bouyer dp = &wd->sc_q;
312 1.163 bouyer if ((bp = dp->b_actf) == NULL) /* yes, an assign */
313 1.163 bouyer return;
314 1.163 bouyer dp->b_actf = bp->b_actf;
315 1.163 bouyer
316 1.163 bouyer /*
317 1.163 bouyer * Make the command. First lock the device
318 1.163 bouyer */
319 1.163 bouyer d_link->openings--;
320 1.163 bouyer if (WDPART(bp->b_dev) != RAW_PART)
321 1.163 bouyer p_offset =
322 1.163 bouyer wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
323 1.163 bouyer else
324 1.163 bouyer p_offset = 0;
325 1.171 bouyer blkno = bp->b_blkno + p_offset;
326 1.171 bouyer blkno /= (wd->d_link->sc_lp->d_secsize / DEV_BSIZE);
327 1.64 mycroft
328 1.163 bouyer xfer = wdc_get_xfer(0);
329 1.163 bouyer if (xfer == NULL)
330 1.163 bouyer panic("wdc_xfer");
331 1.163 bouyer
332 1.163 bouyer xfer->d_link = d_link;
333 1.163 bouyer xfer->c_bp = bp;
334 1.163 bouyer xfer->databuf = bp->b_data;
335 1.163 bouyer xfer->c_bcount = bp->b_bcount;
336 1.163 bouyer xfer->c_flags |= bp->b_flags & (B_READ|B_WRITE);
337 1.171 bouyer xfer->c_blkno = blkno;
338 1.144 thorpej
339 1.163 bouyer /* Instrumentation. */
340 1.163 bouyer disk_busy(&wd->sc_dk);
341 1.163 bouyer wdc_exec_xfer((struct wdc_softc *)wd->d_link->wdc_softc,
342 1.163 bouyer wd->d_link, xfer);
343 1.64 mycroft }
344 1.141 mycroft }
345 1.141 mycroft
346 1.141 mycroft int
347 1.149 christos wdread(dev, uio, flags)
348 1.141 mycroft dev_t dev;
349 1.141 mycroft struct uio *uio;
350 1.149 christos int flags;
351 1.141 mycroft {
352 1.141 mycroft
353 1.163 bouyer WDDEBUG_PRINT(("wdread\n"));
354 1.141 mycroft return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
355 1.141 mycroft }
356 1.141 mycroft
357 1.141 mycroft int
358 1.149 christos wdwrite(dev, uio, flags)
359 1.141 mycroft dev_t dev;
360 1.141 mycroft struct uio *uio;
361 1.149 christos int flags;
362 1.141 mycroft {
363 1.141 mycroft
364 1.163 bouyer WDDEBUG_PRINT(("wdwrite\n"));
365 1.141 mycroft return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
366 1.64 mycroft }
367 1.64 mycroft
368 1.1 cgd /*
369 1.135 mycroft * Wait interruptibly for an exclusive lock.
370 1.135 mycroft *
371 1.135 mycroft * XXX
372 1.135 mycroft * Several drivers do this; it should be abstracted and made MP-safe.
373 1.135 mycroft */
374 1.132 mycroft int
375 1.163 bouyer wdlock(d_link)
376 1.163 bouyer struct wd_link *d_link;
377 1.132 mycroft {
378 1.132 mycroft int error;
379 1.163 bouyer int s;
380 1.163 bouyer
381 1.163 bouyer WDDEBUG_PRINT(("wdlock\n"));
382 1.132 mycroft
383 1.163 bouyer s = splbio();
384 1.163 bouyer
385 1.163 bouyer while ((d_link->sc_flags & WDF_LOCKED) != 0) {
386 1.163 bouyer d_link->sc_flags |= WDF_WANTED;
387 1.163 bouyer if ((error = tsleep(d_link, PRIBIO | PCATCH,
388 1.163 bouyer "wdlck", 0)) != 0) {
389 1.163 bouyer splx(s);
390 1.132 mycroft return error;
391 1.163 bouyer }
392 1.132 mycroft }
393 1.163 bouyer d_link->sc_flags |= WDF_LOCKED;
394 1.163 bouyer splx(s);
395 1.132 mycroft return 0;
396 1.132 mycroft }
397 1.132 mycroft
398 1.135 mycroft /*
399 1.135 mycroft * Unlock and wake up any waiters.
400 1.135 mycroft */
401 1.132 mycroft void
402 1.163 bouyer wdunlock(d_link)
403 1.163 bouyer struct wd_link *d_link;
404 1.132 mycroft {
405 1.132 mycroft
406 1.163 bouyer WDDEBUG_PRINT(("wdunlock"));
407 1.163 bouyer
408 1.163 bouyer d_link->sc_flags &= ~WDF_LOCKED;
409 1.163 bouyer if ((d_link->sc_flags & WDF_WANTED) != 0) {
410 1.163 bouyer d_link->sc_flags &= ~WDF_WANTED;
411 1.163 bouyer wakeup(d_link);
412 1.132 mycroft }
413 1.132 mycroft }
414 1.132 mycroft
415 1.1 cgd int
416 1.149 christos wdopen(dev, flag, fmt, p)
417 1.55 mycroft dev_t dev;
418 1.93 mycroft int flag, fmt;
419 1.149 christos struct proc *p;
420 1.1 cgd {
421 1.135 mycroft struct wd_softc *wd;
422 1.163 bouyer struct wd_link *d_link;
423 1.135 mycroft int unit, part;
424 1.109 mycroft int error;
425 1.163 bouyer
426 1.163 bouyer WDDEBUG_PRINT(("wdopen\n"));
427 1.163 bouyer
428 1.108 mycroft unit = WDUNIT(dev);
429 1.147 thorpej if (unit >= wd_cd.cd_ndevs)
430 1.37 mycroft return ENXIO;
431 1.147 thorpej wd = wd_cd.cd_devs[unit];
432 1.158 pk if (wd == NULL)
433 1.37 mycroft return ENXIO;
434 1.3 deraadt
435 1.163 bouyer d_link = wd->d_link;
436 1.163 bouyer if ((error = wdlock(d_link)) != 0)
437 1.132 mycroft return error;
438 1.3 deraadt
439 1.109 mycroft if (wd->sc_dk.dk_openmask != 0) {
440 1.109 mycroft /*
441 1.109 mycroft * If any partition is open, but the disk has been invalidated,
442 1.109 mycroft * disallow further opens.
443 1.109 mycroft */
444 1.163 bouyer if ((d_link->sc_flags & WDF_LOADED) == 0) {
445 1.136 mycroft error = EIO;
446 1.136 mycroft goto bad3;
447 1.136 mycroft }
448 1.109 mycroft } else {
449 1.163 bouyer if ((d_link->sc_flags & WDF_LOADED) == 0) {
450 1.163 bouyer d_link->sc_flags |= WDF_LOADED;
451 1.108 mycroft
452 1.108 mycroft /* Load the physical device parameters. */
453 1.163 bouyer if (wdc_get_parms((struct wdc_softc *)d_link->wdc_softc,
454 1.163 bouyer d_link) != 0) {
455 1.108 mycroft error = ENXIO;
456 1.108 mycroft goto bad2;
457 1.108 mycroft }
458 1.108 mycroft
459 1.108 mycroft /* Load the partition info if not already loaded. */
460 1.108 mycroft wdgetdisklabel(wd);
461 1.108 mycroft }
462 1.135 mycroft }
463 1.108 mycroft
464 1.135 mycroft part = WDPART(dev);
465 1.108 mycroft
466 1.109 mycroft /* Check that the partition exists. */
467 1.93 mycroft if (part != RAW_PART &&
468 1.144 thorpej (part >= wd->sc_dk.dk_label->d_npartitions ||
469 1.144 thorpej wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
470 1.93 mycroft error = ENXIO;
471 1.93 mycroft goto bad;
472 1.93 mycroft }
473 1.3 deraadt
474 1.64 mycroft /* Insure only one open at a time. */
475 1.3 deraadt switch (fmt) {
476 1.3 deraadt case S_IFCHR:
477 1.94 mycroft wd->sc_dk.dk_copenmask |= (1 << part);
478 1.3 deraadt break;
479 1.3 deraadt case S_IFBLK:
480 1.94 mycroft wd->sc_dk.dk_bopenmask |= (1 << part);
481 1.3 deraadt break;
482 1.3 deraadt }
483 1.94 mycroft wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
484 1.60 mycroft
485 1.163 bouyer wdunlock(d_link);
486 1.37 mycroft return 0;
487 1.93 mycroft
488 1.108 mycroft bad2:
489 1.163 bouyer d_link->sc_flags &= ~WDF_LOADED;
490 1.108 mycroft
491 1.93 mycroft bad:
492 1.118 mycroft if (wd->sc_dk.dk_openmask == 0) {
493 1.108 mycroft }
494 1.108 mycroft
495 1.136 mycroft bad3:
496 1.163 bouyer wdunlock(d_link);
497 1.93 mycroft return error;
498 1.1 cgd }
499 1.1 cgd
500 1.135 mycroft int
501 1.149 christos wdclose(dev, flag, fmt, p)
502 1.135 mycroft dev_t dev;
503 1.135 mycroft int flag, fmt;
504 1.149 christos struct proc *p;
505 1.135 mycroft {
506 1.147 thorpej struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
507 1.135 mycroft int part = WDPART(dev);
508 1.135 mycroft int error;
509 1.135 mycroft
510 1.163 bouyer if ((error = wdlock(wd->d_link)) != 0)
511 1.135 mycroft return error;
512 1.135 mycroft
513 1.135 mycroft switch (fmt) {
514 1.135 mycroft case S_IFCHR:
515 1.135 mycroft wd->sc_dk.dk_copenmask &= ~(1 << part);
516 1.135 mycroft break;
517 1.135 mycroft case S_IFBLK:
518 1.135 mycroft wd->sc_dk.dk_bopenmask &= ~(1 << part);
519 1.135 mycroft break;
520 1.135 mycroft }
521 1.135 mycroft wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
522 1.135 mycroft
523 1.135 mycroft if (wd->sc_dk.dk_openmask == 0) {
524 1.135 mycroft /* XXXX Must wait for I/O to complete! */
525 1.135 mycroft }
526 1.135 mycroft
527 1.163 bouyer wdunlock(wd->d_link);
528 1.135 mycroft return 0;
529 1.135 mycroft }
530 1.135 mycroft
531 1.108 mycroft void
532 1.164 thorpej wdgetdefaultlabel(wd, lp)
533 1.108 mycroft struct wd_softc *wd;
534 1.164 thorpej struct disklabel *lp;
535 1.108 mycroft {
536 1.163 bouyer struct wd_link *d_link = wd->d_link;
537 1.163 bouyer
538 1.142 mycroft bzero(lp, sizeof(struct disklabel));
539 1.108 mycroft
540 1.142 mycroft lp->d_secsize = DEV_BSIZE;
541 1.163 bouyer lp->d_ntracks = d_link->sc_params.wdp_heads;
542 1.163 bouyer lp->d_nsectors = d_link->sc_params.wdp_sectors;
543 1.163 bouyer lp->d_ncylinders = d_link->sc_params.wdp_cylinders;
544 1.142 mycroft lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
545 1.108 mycroft
546 1.108 mycroft #if 0
547 1.142 mycroft strncpy(lp->d_typename, "ST506 disk", 16);
548 1.142 mycroft lp->d_type = DTYPE_ST506;
549 1.108 mycroft #endif
550 1.163 bouyer strncpy(lp->d_packname, d_link->sc_params.wdp_model, 16);
551 1.142 mycroft lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
552 1.142 mycroft lp->d_rpm = 3600;
553 1.142 mycroft lp->d_interleave = 1;
554 1.142 mycroft lp->d_flags = 0;
555 1.142 mycroft
556 1.142 mycroft lp->d_partitions[RAW_PART].p_offset = 0;
557 1.142 mycroft lp->d_partitions[RAW_PART].p_size =
558 1.142 mycroft lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
559 1.142 mycroft lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
560 1.142 mycroft lp->d_npartitions = RAW_PART + 1;
561 1.142 mycroft
562 1.142 mycroft lp->d_magic = DISKMAGIC;
563 1.142 mycroft lp->d_magic2 = DISKMAGIC;
564 1.142 mycroft lp->d_checksum = dkcksum(lp);
565 1.164 thorpej }
566 1.164 thorpej
567 1.164 thorpej /*
568 1.164 thorpej * Fabricate a default disk label, and try to read the correct one.
569 1.164 thorpej */
570 1.164 thorpej void
571 1.164 thorpej wdgetdisklabel(wd)
572 1.164 thorpej struct wd_softc *wd;
573 1.164 thorpej {
574 1.164 thorpej struct disklabel *lp = wd->sc_dk.dk_label;
575 1.164 thorpej struct wd_link *d_link = wd->d_link;
576 1.164 thorpej char *errstring;
577 1.164 thorpej
578 1.164 thorpej WDDEBUG_PRINT(("wdgetdisklabel\n"));
579 1.164 thorpej
580 1.164 thorpej bzero(wd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
581 1.164 thorpej
582 1.164 thorpej wdgetdefaultlabel(wd, lp);
583 1.108 mycroft
584 1.163 bouyer d_link->sc_badsect[0] = -1;
585 1.113 mycroft
586 1.163 bouyer if (d_link->sc_state > RECAL)
587 1.163 bouyer d_link->sc_state = RECAL;
588 1.108 mycroft errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
589 1.144 thorpej wdstrategy, lp, wd->sc_dk.dk_cpulabel);
590 1.108 mycroft if (errstring) {
591 1.108 mycroft /*
592 1.108 mycroft * This probably happened because the drive's default
593 1.108 mycroft * geometry doesn't match the DOS geometry. We
594 1.108 mycroft * assume the DOS geometry is now in the label and try
595 1.108 mycroft * again. XXX This is a kluge.
596 1.108 mycroft */
597 1.163 bouyer if (d_link->sc_state > GEOMETRY)
598 1.163 bouyer d_link->sc_state = GEOMETRY;
599 1.108 mycroft errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
600 1.144 thorpej wdstrategy, lp, wd->sc_dk.dk_cpulabel);
601 1.108 mycroft }
602 1.108 mycroft if (errstring) {
603 1.153 christos printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
604 1.108 mycroft return;
605 1.108 mycroft }
606 1.108 mycroft
607 1.163 bouyer if (d_link->sc_state > GEOMETRY)
608 1.163 bouyer d_link->sc_state = GEOMETRY;
609 1.170 leo #ifdef HAS_BAD144_HANDLING
610 1.142 mycroft if ((lp->d_flags & D_BADSECT) != 0)
611 1.108 mycroft bad144intern(wd);
612 1.170 leo #endif
613 1.108 mycroft }
614 1.108 mycroft
615 1.1 cgd
616 1.1 cgd /*
617 1.135 mycroft * Tell the drive what geometry to use.
618 1.1 cgd */
619 1.135 mycroft int
620 1.163 bouyer wdsetctlr(d_link)
621 1.163 bouyer struct wd_link *d_link;
622 1.3 deraadt {
623 1.163 bouyer struct wd_softc *wd=(struct wd_softc *)d_link->wd_softc;
624 1.63 mycroft
625 1.163 bouyer WDDEBUG_PRINT(("wd(%d,%d) C%dH%dS%d\n", wd->sc_dev.dv_unit,
626 1.163 bouyer d_link->drive, wd->sc_dk.dk_label->d_ncylinders,
627 1.163 bouyer wd->sc_dk.dk_label->d_ntracks, wd->sc_dk.dk_label->d_nsectors));
628 1.163 bouyer
629 1.163 bouyer if (wdccommand((struct wdc_softc *)d_link->wdc_softc,
630 1.163 bouyer d_link, WDCC_IDP, d_link->drive,
631 1.163 bouyer wd->sc_dk.dk_label->d_ncylinders,
632 1.144 thorpej wd->sc_dk.dk_label->d_ntracks - 1, 0,
633 1.144 thorpej wd->sc_dk.dk_label->d_nsectors) != 0) {
634 1.163 bouyer wderror(d_link, NULL, "wdsetctlr: geometry upload failed");
635 1.51 mycroft return -1;
636 1.57 mycroft }
637 1.69 mycroft
638 1.60 mycroft return 0;
639 1.1 cgd }
640 1.1 cgd
641 1.1 cgd int
642 1.163 bouyer wdioctl(dev, xfer, addr, flag, p)
643 1.55 mycroft dev_t dev;
644 1.163 bouyer u_long xfer;
645 1.55 mycroft caddr_t addr;
646 1.55 mycroft int flag;
647 1.55 mycroft struct proc *p;
648 1.1 cgd {
649 1.147 thorpej struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
650 1.163 bouyer struct wd_link *d_link = wd->d_link;
651 1.54 mycroft int error;
652 1.163 bouyer
653 1.163 bouyer WDDEBUG_PRINT(("wdioctl\n"));
654 1.163 bouyer
655 1.163 bouyer if ((d_link->sc_flags & WDF_LOADED) == 0)
656 1.108 mycroft return EIO;
657 1.108 mycroft
658 1.163 bouyer switch (xfer) {
659 1.170 leo #ifdef HAS_BAD144_HANDLING
660 1.1 cgd case DIOCSBAD:
661 1.3 deraadt if ((flag & FWRITE) == 0)
662 1.54 mycroft return EBADF;
663 1.144 thorpej wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
664 1.144 thorpej wd->sc_dk.dk_label->d_flags |= D_BADSECT;
665 1.64 mycroft bad144intern(wd);
666 1.54 mycroft return 0;
667 1.170 leo #endif
668 1.1 cgd
669 1.1 cgd case DIOCGDINFO:
670 1.144 thorpej *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
671 1.54 mycroft return 0;
672 1.3 deraadt
673 1.3 deraadt case DIOCGPART:
674 1.144 thorpej ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
675 1.3 deraadt ((struct partinfo *)addr)->part =
676 1.144 thorpej &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
677 1.54 mycroft return 0;
678 1.3 deraadt
679 1.132 mycroft case DIOCWDINFO:
680 1.3 deraadt case DIOCSDINFO:
681 1.3 deraadt if ((flag & FWRITE) == 0)
682 1.54 mycroft return EBADF;
683 1.132 mycroft
684 1.163 bouyer if ((error = wdlock(wd->d_link)) != 0)
685 1.132 mycroft return error;
686 1.163 bouyer d_link->sc_flags |= WDF_LABELLING;
687 1.132 mycroft
688 1.144 thorpej error = setdisklabel(wd->sc_dk.dk_label,
689 1.128 mycroft (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
690 1.144 thorpej wd->sc_dk.dk_cpulabel);
691 1.3 deraadt if (error == 0) {
692 1.163 bouyer if (d_link->sc_state > GEOMETRY)
693 1.163 bouyer d_link->sc_state = GEOMETRY;
694 1.163 bouyer if (xfer == DIOCWDINFO)
695 1.132 mycroft error = writedisklabel(WDLABELDEV(dev),
696 1.144 thorpej wdstrategy, wd->sc_dk.dk_label,
697 1.144 thorpej wd->sc_dk.dk_cpulabel);
698 1.1 cgd }
699 1.132 mycroft
700 1.163 bouyer d_link->sc_flags &= ~WDF_LABELLING;
701 1.163 bouyer wdunlock(d_link);
702 1.54 mycroft return error;
703 1.3 deraadt
704 1.44 mycroft case DIOCWLABEL:
705 1.3 deraadt if ((flag & FWRITE) == 0)
706 1.54 mycroft return EBADF;
707 1.93 mycroft if (*(int *)addr)
708 1.163 bouyer d_link->sc_flags |= WDF_WLABEL;
709 1.93 mycroft else
710 1.163 bouyer d_link->sc_flags &= ~WDF_WLABEL;
711 1.54 mycroft return 0;
712 1.164 thorpej
713 1.164 thorpej case DIOCGDEFLABEL:
714 1.164 thorpej wdgetdefaultlabel(wd, (struct disklabel *)addr);
715 1.164 thorpej return 0;
716 1.164 thorpej
717 1.1 cgd #ifdef notyet
718 1.1 cgd case DIOCWFORMAT:
719 1.1 cgd if ((flag & FWRITE) == 0)
720 1.54 mycroft return EBADF;
721 1.54 mycroft {
722 1.54 mycroft register struct format_op *fop;
723 1.54 mycroft struct iovec aiov;
724 1.54 mycroft struct uio auio;
725 1.3 deraadt
726 1.54 mycroft fop = (struct format_op *)addr;
727 1.54 mycroft aiov.iov_base = fop->df_buf;
728 1.54 mycroft aiov.iov_len = fop->df_count;
729 1.54 mycroft auio.uio_iov = &aiov;
730 1.54 mycroft auio.uio_iovcnt = 1;
731 1.54 mycroft auio.uio_resid = fop->df_count;
732 1.54 mycroft auio.uio_segflg = 0;
733 1.54 mycroft auio.uio_offset =
734 1.144 thorpej fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
735 1.83 pk auio.uio_procp = p;
736 1.64 mycroft error = physio(wdformat, NULL, dev, B_WRITE, minphys,
737 1.64 mycroft &auio);
738 1.54 mycroft fop->df_count -= auio.uio_resid;
739 1.64 mycroft fop->df_reg[0] = wdc->sc_status;
740 1.64 mycroft fop->df_reg[1] = wdc->sc_error;
741 1.54 mycroft return error;
742 1.54 mycroft }
743 1.1 cgd #endif
744 1.163 bouyer
745 1.1 cgd default:
746 1.54 mycroft return ENOTTY;
747 1.1 cgd }
748 1.54 mycroft
749 1.54 mycroft #ifdef DIAGNOSTIC
750 1.54 mycroft panic("wdioctl: impossible");
751 1.54 mycroft #endif
752 1.1 cgd }
753 1.1 cgd
754 1.44 mycroft #ifdef B_FORMAT
755 1.1 cgd int
756 1.1 cgd wdformat(struct buf *bp)
757 1.1 cgd {
758 1.44 mycroft
759 1.1 cgd bp->b_flags |= B_FORMAT;
760 1.37 mycroft return wdstrategy(bp);
761 1.1 cgd }
762 1.1 cgd #endif
763 1.1 cgd
764 1.1 cgd int
765 1.55 mycroft wdsize(dev)
766 1.55 mycroft dev_t dev;
767 1.1 cgd {
768 1.64 mycroft struct wd_softc *wd;
769 1.158 pk int part, unit, omask;
770 1.108 mycroft int size;
771 1.163 bouyer
772 1.163 bouyer WDDEBUG_PRINT(("wdsize\n"));
773 1.163 bouyer
774 1.158 pk unit = WDUNIT(dev);
775 1.158 pk if (unit >= wd_cd.cd_ndevs)
776 1.158 pk return (-1);
777 1.158 pk wd = wd_cd.cd_devs[unit];
778 1.158 pk if (wd == NULL)
779 1.158 pk return (-1);
780 1.158 pk
781 1.158 pk part = WDPART(dev);
782 1.158 pk omask = wd->sc_dk.dk_openmask & (1 << part);
783 1.158 pk
784 1.158 pk if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
785 1.158 pk return (-1);
786 1.144 thorpej if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
787 1.108 mycroft size = -1;
788 1.108 mycroft else
789 1.160 thorpej size = wd->sc_dk.dk_label->d_partitions[part].p_size *
790 1.163 bouyer (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
791 1.158 pk if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
792 1.158 pk return (-1);
793 1.158 pk return (size);
794 1.1 cgd }
795 1.1 cgd
796 1.140 cgd
797 1.140 cgd #ifndef __BDEVSW_DUMP_OLD_TYPE
798 1.140 cgd /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
799 1.140 cgd static int wddoingadump;
800 1.140 cgd static int wddumprecalibrated;
801 1.140 cgd
802 1.64 mycroft /*
803 1.64 mycroft * Dump core after a system crash.
804 1.163 bouyer *
805 1.163 bouyer * XXX: This needs work! Currently, it's a major hack: the
806 1.163 bouyer * use of wdc_softc is very bad and should go away.
807 1.64 mycroft */
808 1.1 cgd int
809 1.140 cgd wddump(dev, blkno, va, size)
810 1.140 cgd dev_t dev;
811 1.140 cgd daddr_t blkno;
812 1.140 cgd caddr_t va;
813 1.140 cgd size_t size;
814 1.140 cgd {
815 1.140 cgd struct wd_softc *wd; /* disk unit to do the I/O */
816 1.140 cgd struct wdc_softc *wdc; /* disk controller to do the I/O */
817 1.140 cgd struct disklabel *lp; /* disk's disklabel */
818 1.163 bouyer struct wd_link *d_link;
819 1.140 cgd int unit, part;
820 1.142 mycroft int nblks; /* total number of sectors left to write */
821 1.140 cgd
822 1.140 cgd /* Check if recursive dump; if so, punt. */
823 1.116 mycroft if (wddoingadump)
824 1.116 mycroft return EFAULT;
825 1.142 mycroft wddoingadump = 1;
826 1.140 cgd
827 1.142 mycroft unit = WDUNIT(dev);
828 1.147 thorpej if (unit >= wd_cd.cd_ndevs)
829 1.142 mycroft return ENXIO;
830 1.147 thorpej wd = wd_cd.cd_devs[unit];
831 1.163 bouyer if (wd == (struct wd_softc *)0)
832 1.142 mycroft return ENXIO;
833 1.163 bouyer d_link = wd->d_link;
834 1.60 mycroft
835 1.140 cgd part = WDPART(dev);
836 1.140 cgd
837 1.140 cgd /* Make sure it was initialized. */
838 1.163 bouyer if (d_link->sc_state < READY)
839 1.37 mycroft return ENXIO;
840 1.140 cgd
841 1.142 mycroft wdc = (void *)wd->sc_dev.dv_parent;
842 1.46 mycroft
843 1.140 cgd /* Convert to disk sectors. Request must be a multiple of size. */
844 1.144 thorpej lp = wd->sc_dk.dk_label;
845 1.142 mycroft if ((size % lp->d_secsize) != 0)
846 1.140 cgd return EFAULT;
847 1.142 mycroft nblks = size / lp->d_secsize;
848 1.142 mycroft blkno = blkno / (lp->d_secsize / DEV_BSIZE);
849 1.116 mycroft
850 1.64 mycroft /* Check transfer bounds against partition size. */
851 1.142 mycroft if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
852 1.140 cgd return EINVAL;
853 1.140 cgd
854 1.140 cgd /* Offset block number to start of partition. */
855 1.142 mycroft blkno += lp->d_partitions[part].p_offset;
856 1.60 mycroft
857 1.140 cgd /* Recalibrate, if first dump transfer. */
858 1.140 cgd if (wddumprecalibrated == 0) {
859 1.140 cgd wddumprecalibrated = 1;
860 1.163 bouyer if (wdccommandshort(wdc, d_link->drive, WDCC_RECAL) != 0 ||
861 1.163 bouyer wait_for_ready(wdc) != 0 || wdsetctlr(d_link) != 0 ||
862 1.140 cgd wait_for_ready(wdc) != 0) {
863 1.163 bouyer wderror(d_link, NULL, "wddump: recal failed");
864 1.140 cgd return EIO;
865 1.140 cgd }
866 1.63 mycroft }
867 1.140 cgd
868 1.142 mycroft while (nblks > 0) {
869 1.142 mycroft daddr_t xlt_blkno = blkno;
870 1.116 mycroft long cylin, head, sector;
871 1.116 mycroft
872 1.116 mycroft if ((lp->d_flags & D_BADSECT) != 0) {
873 1.116 mycroft long blkdiff;
874 1.3 deraadt int i;
875 1.3 deraadt
876 1.163 bouyer for (i = 0; (blkdiff = d_link->sc_badsect[i]) != -1; i++) {
877 1.140 cgd blkdiff -= xlt_blkno;
878 1.116 mycroft if (blkdiff < 0)
879 1.116 mycroft continue;
880 1.116 mycroft if (blkdiff == 0) {
881 1.116 mycroft /* Replace current block of transfer. */
882 1.140 cgd xlt_blkno = lp->d_secperunit -
883 1.140 cgd lp->d_nsectors - i - 1;
884 1.3 deraadt }
885 1.116 mycroft break;
886 1.1 cgd }
887 1.116 mycroft /* Tranfer is okay now. */
888 1.116 mycroft }
889 1.116 mycroft
890 1.163 bouyer if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
891 1.140 cgd sector = (xlt_blkno >> 0) & 0xff;
892 1.140 cgd cylin = (xlt_blkno >> 8) & 0xffff;
893 1.140 cgd head = (xlt_blkno >> 24) & 0xf;
894 1.116 mycroft head |= WDSD_LBA;
895 1.116 mycroft } else {
896 1.140 cgd sector = xlt_blkno % lp->d_nsectors;
897 1.116 mycroft sector++; /* Sectors begin with 1, not 0. */
898 1.140 cgd xlt_blkno /= lp->d_nsectors;
899 1.140 cgd head = xlt_blkno % lp->d_ntracks;
900 1.140 cgd xlt_blkno /= lp->d_ntracks;
901 1.140 cgd cylin = xlt_blkno;
902 1.116 mycroft head |= WDSD_CHS;
903 1.3 deraadt }
904 1.140 cgd
905 1.140 cgd #ifndef WD_DUMP_NOT_TRUSTED
906 1.163 bouyer if (wdccommand((struct wdc_softc *)d_link->wdc_softc, d_link,
907 1.163 bouyer WDCC_WRITE, d_link->drive, cylin, head, sector, 1) != 0 ||
908 1.105 mycroft wait_for_drq(wdc) != 0) {
909 1.163 bouyer wderror(d_link, NULL, "wddump: write failed");
910 1.51 mycroft return EIO;
911 1.63 mycroft }
912 1.3 deraadt
913 1.163 bouyer /* XXX XXX XXX */
914 1.168 cgd bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
915 1.168 cgd (u_int16_t *)va, lp->d_secsize >> 1);
916 1.3 deraadt
917 1.13 deraadt /* Check data request (should be done). */
918 1.64 mycroft if (wait_for_ready(wdc) != 0) {
919 1.163 bouyer wderror(d_link, NULL,
920 1.163 bouyer "wddump: timeout waiting for ready");
921 1.63 mycroft return EIO;
922 1.63 mycroft }
923 1.140 cgd #else /* WD_DUMP_NOT_TRUSTED */
924 1.140 cgd /* Let's just talk about this first... */
925 1.153 christos printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
926 1.140 cgd unit, va, cylin, head, sector);
927 1.140 cgd delay(500 * 1000); /* half a second */
928 1.140 cgd #endif
929 1.1 cgd
930 1.140 cgd /* update block count */
931 1.142 mycroft nblks -= 1;
932 1.142 mycroft blkno += 1;
933 1.142 mycroft va += lp->d_secsize;
934 1.1 cgd }
935 1.142 mycroft
936 1.140 cgd wddoingadump = 0;
937 1.140 cgd return 0;
938 1.140 cgd }
939 1.140 cgd #else /* __BDEVSW_DUMP_NEW_TYPE */
940 1.163 bouyer
941 1.163 bouyer
942 1.140 cgd int
943 1.140 cgd wddump(dev, blkno, va, size)
944 1.140 cgd dev_t dev;
945 1.140 cgd daddr_t blkno;
946 1.140 cgd caddr_t va;
947 1.140 cgd size_t size;
948 1.140 cgd {
949 1.116 mycroft
950 1.140 cgd /* Not implemented. */
951 1.140 cgd return ENXIO;
952 1.1 cgd }
953 1.140 cgd #endif /* __BDEVSW_DUMP_NEW_TYPE */
954 1.3 deraadt
955 1.170 leo #ifdef HAS_BAD144_HANDLING
956 1.3 deraadt /*
957 1.3 deraadt * Internalize the bad sector table.
958 1.3 deraadt */
959 1.3 deraadt void
960 1.64 mycroft bad144intern(wd)
961 1.64 mycroft struct wd_softc *wd;
962 1.3 deraadt {
963 1.144 thorpej struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
964 1.144 thorpej struct disklabel *lp = wd->sc_dk.dk_label;
965 1.163 bouyer struct wd_link *d_link = wd->d_link;
966 1.98 mycroft int i = 0;
967 1.55 mycroft
968 1.163 bouyer WDDEBUG_PRINT(("bad144intern\n"));
969 1.163 bouyer
970 1.170 leo for (; i < NBT_BAD; i++) {
971 1.98 mycroft if (bt->bt_bad[i].bt_cyl == 0xffff)
972 1.55 mycroft break;
973 1.163 bouyer d_link->sc_badsect[i] =
974 1.98 mycroft bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
975 1.98 mycroft (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
976 1.98 mycroft (bt->bt_bad[i].bt_trksec & 0xff);
977 1.3 deraadt }
978 1.170 leo for (; i < NBT_BAD+1; i++)
979 1.163 bouyer d_link->sc_badsect[i] = -1;
980 1.64 mycroft }
981 1.170 leo #endif
982 1.64 mycroft
983 1.135 mycroft void
984 1.163 bouyer wderror(d_link, bp, msg)
985 1.163 bouyer struct wd_link *d_link;
986 1.163 bouyer struct buf *bp;
987 1.163 bouyer char *msg;
988 1.64 mycroft {
989 1.163 bouyer struct wd_softc *wd = (struct wd_softc *)d_link->wd_softc;
990 1.64 mycroft
991 1.163 bouyer if (bp) {
992 1.163 bouyer diskerr(bp, "wd", msg, LOG_PRINTF, bp->b_bcount,
993 1.163 bouyer wd->sc_dk.dk_label);
994 1.163 bouyer printf("\n");
995 1.120 mycroft } else
996 1.163 bouyer printf("%s: %s\n", wd->sc_dev.dv_xname, msg);
997 1.63 mycroft }
998 1.63 mycroft
999 1.135 mycroft void
1000 1.163 bouyer wddone(d_link, bp)
1001 1.163 bouyer struct wd_link *d_link;
1002 1.63 mycroft struct buf *bp;
1003 1.63 mycroft {
1004 1.163 bouyer struct wd_softc *wd = (void *)d_link->wd_softc;
1005 1.64 mycroft
1006 1.163 bouyer disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
1007 1.166 explorer #if NRND > 0
1008 1.165 explorer rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
1009 1.166 explorer #endif
1010 1.21 deraadt }
1011