wd.c revision 1.199 1 /* $NetBSD: wd.c,v 1.199 1999/12/23 21:23:19 leo Exp $ */
2
3 /*
4 * Copyright (c) 1998 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Manuel Bouyer.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Charles M. Hannum and by Onno van der Linden.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the NetBSD
50 * Foundation, Inc. and its contributors.
51 * 4. Neither the name of The NetBSD Foundation nor the names of its
52 * contributors may be used to endorse or promote products derived
53 * from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
66 */
67
68 #ifndef WDCDEBUG
69 #define WDCDEBUG
70 #endif /* WDCDEBUG */
71
72 #include "rnd.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/conf.h>
78 #include <sys/file.h>
79 #include <sys/stat.h>
80 #include <sys/ioctl.h>
81 #include <sys/buf.h>
82 #include <sys/uio.h>
83 #include <sys/malloc.h>
84 #include <sys/device.h>
85 #include <sys/disklabel.h>
86 #include <sys/disk.h>
87 #include <sys/syslog.h>
88 #include <sys/proc.h>
89 #include <sys/vnode.h>
90 #if NRND > 0
91 #include <sys/rnd.h>
92 #endif
93
94 #include <vm/vm.h>
95
96 #include <machine/intr.h>
97 #include <machine/bus.h>
98
99 #include <dev/ata/atareg.h>
100 #include <dev/ata/atavar.h>
101 #include <dev/ata/wdvar.h>
102 #include <dev/ic/wdcreg.h>
103 #include <sys/ataio.h>
104 #include "locators.h"
105
106 #define WAITTIME (4 * hz) /* time to wait for a completion */
107 #define WDIORETRIES_SINGLE 4 /* number of retries before single-sector */
108 #define WDIORETRIES 5 /* number of retries before giving up */
109 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
110
111 #define WDUNIT(dev) DISKUNIT(dev)
112 #define WDPART(dev) DISKPART(dev)
113 #define WDMINOR(unit, part) DISKMINOR(unit, part)
114 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
115
116 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
117
118 #define DEBUG_INTR 0x01
119 #define DEBUG_XFERS 0x02
120 #define DEBUG_STATUS 0x04
121 #define DEBUG_FUNCS 0x08
122 #define DEBUG_PROBE 0x10
123 #ifdef WDCDEBUG
124 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
125 #define WDCDEBUG_PRINT(args, level) \
126 if (wdcdebug_wd_mask & (level)) \
127 printf args
128 #else
129 #define WDCDEBUG_PRINT(args, level)
130 #endif
131
132 struct wd_softc {
133 /* General disk infos */
134 struct device sc_dev;
135 struct disk sc_dk;
136 struct buf sc_q;
137 /* IDE disk soft states */
138 struct ata_bio sc_wdc_bio; /* current transfer */
139 struct buf *sc_bp; /* buf being transfered */
140 void *wdc_softc; /* pointer to our parent */
141 struct ata_drive_datas *drvp; /* Our controller's infos */
142 int openings;
143 struct ataparams sc_params;/* drive characteistics found */
144 int sc_flags;
145 #define WDF_LOCKED 0x01
146 #define WDF_WANTED 0x02
147 #define WDF_WLABEL 0x04 /* label is writable */
148 #define WDF_LABELLING 0x08 /* writing label */
149 /*
150 * XXX Nothing resets this yet, but disk change sensing will when ATA-4 is
151 * more fully implemented.
152 */
153 #define WDF_LOADED 0x10 /* parameters loaded */
154 #define WDF_WAIT 0x20 /* waiting for resources */
155 #define WDF_LBA 0x40 /* using LBA mode */
156 #define WDF_KLABEL 0x80 /* retain label after 'full' close */
157 int sc_capacity;
158 int cyl; /* actual drive parameters */
159 int heads;
160 int sectors;
161 int retries; /* number of xfer retry */
162
163 void *sc_sdhook; /* our shutdown hook */
164
165 #if NRND > 0
166 rndsource_element_t rnd_source;
167 #endif
168 };
169
170 #define sc_drive sc_wdc_bio.drive
171 #define sc_mode sc_wdc_bio.mode
172 #define sc_multi sc_wdc_bio.multi
173 #define sc_badsect sc_wdc_bio.badsect
174
175 int wdprobe __P((struct device *, struct cfdata *, void *));
176 void wdattach __P((struct device *, struct device *, void *));
177 int wddetach __P((struct device *, int));
178 int wdactivate __P((struct device *, enum devact));
179 int wdprint __P((void *, char *));
180
181 struct cfattach wd_ca = {
182 sizeof(struct wd_softc), wdprobe, wdattach, wddetach, wdactivate
183 };
184
185 extern struct cfdriver wd_cd;
186
187 /*
188 * Glue necessary to hook WDCIOCCOMMAND into physio
189 */
190
191 struct wd_ioctl {
192 LIST_ENTRY(wd_ioctl) wi_list;
193 struct buf wi_bp;
194 struct uio wi_uio;
195 struct iovec wi_iov;
196 atareq_t wi_atareq;
197 struct wd_softc *wi_softc;
198 };
199
200 LIST_HEAD(, wd_ioctl) wi_head;
201
202 struct wd_ioctl *wi_find __P((struct buf *));
203 void wi_free __P((struct wd_ioctl *));
204 struct wd_ioctl *wi_get __P((void));
205 void wdioctlstrategy __P((struct buf *));
206
207 void wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
208 void wdgetdisklabel __P((struct wd_softc *));
209 void wdstrategy __P((struct buf *));
210 void wdstart __P((void *));
211 void __wdstart __P((struct wd_softc*, struct buf *));
212 void wdrestart __P((void*));
213 int wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
214 void wd_flushcache __P((struct wd_softc *, int));
215 void wd_shutdown __P((void*));
216
217 struct dkdriver wddkdriver = { wdstrategy };
218
219 /* XXX: these should go elsewhere */
220 cdev_decl(wd);
221 bdev_decl(wd);
222
223 #ifdef HAS_BAD144_HANDLING
224 static void bad144intern __P((struct wd_softc *));
225 #endif
226 int wdlock __P((struct wd_softc *));
227 void wdunlock __P((struct wd_softc *));
228
229 int
230 wdprobe(parent, match, aux)
231 struct device *parent;
232 struct cfdata *match;
233
234 void *aux;
235 {
236 struct ata_atapi_attach *aa_link = aux;
237
238 if (aa_link == NULL)
239 return 0;
240 if (aa_link->aa_type != T_ATA)
241 return 0;
242
243 if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
244 match->cf_loc[ATACF_CHANNEL] != aa_link->aa_channel)
245 return 0;
246
247 if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
248 match->cf_loc[ATACF_DRIVE] != aa_link->aa_drv_data->drive)
249 return 0;
250 return 1;
251 }
252
253 void
254 wdattach(parent, self, aux)
255 struct device *parent, *self;
256 void *aux;
257 {
258 struct wd_softc *wd = (void *)self;
259 struct ata_atapi_attach *aa_link= aux;
260 int i, blank;
261 char buf[41], c, *p, *q;
262 WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
263
264 wd->openings = aa_link->aa_openings;
265 wd->drvp = aa_link->aa_drv_data;;
266 wd->wdc_softc = parent;
267 /* give back our softc to our caller */
268 wd->drvp->drv_softc = &wd->sc_dev;
269
270 /* read our drive info */
271 if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
272 printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
273 return;
274 }
275
276 for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
277 i < sizeof(wd->sc_params.atap_model); i++) {
278 c = *p++;
279 if (c == '\0')
280 break;
281 if (c != ' ') {
282 if (blank) {
283 *q++ = ' ';
284 blank = 0;
285 }
286 *q++ = c;
287 } else
288 blank = 1;
289 }
290 *q++ = '\0';
291
292 printf(": <%s>\n", buf);
293
294 if ((wd->sc_params.atap_multi & 0xff) > 1) {
295 wd->sc_multi = wd->sc_params.atap_multi & 0xff;
296 } else {
297 wd->sc_multi = 1;
298 }
299
300 printf("%s: drive supports %d-sector pio transfers,",
301 wd->sc_dev.dv_xname, wd->sc_multi);
302
303 /* Prior to ATA-4, LBA was optional. */
304 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
305 wd->sc_flags |= WDF_LBA;
306 #if 0
307 /* ATA-4 requires LBA. */
308 if (wd->sc_params.atap_ataversion != 0xffff &&
309 wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
310 wd->sc_flags |= WDF_LBA;
311 #endif
312
313 if ((wd->sc_flags & WDF_LBA) != 0) {
314 printf(" lba addressing\n");
315 wd->sc_capacity =
316 (wd->sc_params.atap_capacity[1] << 16) |
317 wd->sc_params.atap_capacity[0];
318 printf("%s: %dMB, %d cyl, %d head, %d sec, "
319 "%d bytes/sect x %d sectors\n",
320 self->dv_xname,
321 wd->sc_capacity / (1048576 / DEV_BSIZE),
322 wd->sc_params.atap_cylinders,
323 wd->sc_params.atap_heads,
324 wd->sc_params.atap_sectors,
325 DEV_BSIZE,
326 wd->sc_capacity);
327 } else {
328 printf(" chs addressing\n");
329 wd->sc_capacity =
330 wd->sc_params.atap_cylinders *
331 wd->sc_params.atap_heads *
332 wd->sc_params.atap_sectors;
333 printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d "
334 "sectors\n", self->dv_xname,
335 wd->sc_capacity / (1048576 / DEV_BSIZE),
336 wd->sc_params.atap_cylinders,
337 wd->sc_params.atap_heads,
338 wd->sc_params.atap_sectors,
339 DEV_BSIZE,
340 wd->sc_capacity);
341 }
342 WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
343 self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
344 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
345 /*
346 * Initialize and attach the disk structure.
347 */
348 wd->sc_dk.dk_driver = &wddkdriver;
349 wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
350 disk_attach(&wd->sc_dk);
351 wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
352 wd->sc_sdhook = shutdownhook_establish(wd_shutdown, wd);
353 if (wd->sc_sdhook == NULL)
354 printf("%s: WARNING: unable to establish shutdown hook\n",
355 wd->sc_dev.dv_xname);
356 #if NRND > 0
357 rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname,
358 RND_TYPE_DISK, 0);
359 #endif
360 }
361
362 int
363 wdactivate(self, act)
364 struct device *self;
365 enum devact act;
366 {
367 int rv = 0;
368
369 switch (act) {
370 case DVACT_ACTIVATE:
371 rv = EOPNOTSUPP;
372 break;
373
374 case DVACT_DEACTIVATE:
375 /*
376 * Nothing to do; we key off the device's DVF_ACTIVATE.
377 */
378 break;
379 }
380 return (rv);
381 }
382
383 int
384 wddetach(self, flags)
385 struct device *self;
386 int flags;
387 {
388 struct wd_softc *sc = (struct wd_softc *)self;
389 struct buf *bp;
390 int s, bmaj, cmaj, mn;
391
392 /* locate the major number */
393 for (bmaj = 0; bmaj < nblkdev; bmaj++)
394 if (bdevsw[bmaj].d_open == wdopen)
395 break;
396 for (cmaj = 0; cmaj < nchrdev; cmaj++)
397 if (cdevsw[cmaj].d_open == wdopen)
398 break;
399
400 s = splbio();
401
402 /* Kill off any queued buffers. */
403 while ((bp = sc->sc_q.b_actf) != NULL) {
404 sc->sc_q.b_actf = bp->b_actf;
405 bp->b_error = EIO;
406 bp->b_flags |= B_ERROR;
407 bp->b_resid = bp->b_bcount;
408 biodone(bp);
409 }
410
411 splx(s);
412
413 /* Nuke the vnodes for any open instances. */
414 mn = WDMINOR(self->dv_unit, 0);
415 vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
416 vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
417
418 /* Detach disk. */
419 disk_detach(&sc->sc_dk);
420
421 /* Get rid of the shutdown hook. */
422 if (sc->sc_sdhook != NULL)
423 shutdownhook_disestablish(sc->sc_sdhook);
424
425 #if NRND > 0
426 /* Unhook the entropy source. */
427 rnd_detach_source(&sc->rnd_source);
428 #endif
429
430 return (0);
431 }
432
433 /*
434 * Read/write routine for a buffer. Validates the arguments and schedules the
435 * transfer. Does not wait for the transfer to complete.
436 */
437 void
438 wdstrategy(bp)
439 struct buf *bp;
440 {
441 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
442 int s;
443 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
444 DEBUG_XFERS);
445
446 /* Valid request? */
447 if (bp->b_blkno < 0 ||
448 (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
449 (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
450 bp->b_error = EINVAL;
451 goto bad;
452 }
453
454 /* If device invalidated (e.g. media change, door open), error. */
455 if ((wd->sc_flags & WDF_LOADED) == 0) {
456 bp->b_error = EIO;
457 goto bad;
458 }
459
460 /* If it's a null transfer, return immediately. */
461 if (bp->b_bcount == 0)
462 goto done;
463
464 /*
465 * Do bounds checking, adjust transfer. if error, process.
466 * If end of partition, just return.
467 */
468 if (WDPART(bp->b_dev) != RAW_PART &&
469 bounds_check_with_label(bp, wd->sc_dk.dk_label,
470 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
471 goto done;
472 /* Queue transfer on drive, activate drive and controller if idle. */
473 s = splbio();
474 disksort(&wd->sc_q, bp);
475 wdstart(wd);
476 splx(s);
477 return;
478 bad:
479 bp->b_flags |= B_ERROR;
480 done:
481 /* Toss transfer; we're done early. */
482 bp->b_resid = bp->b_bcount;
483 biodone(bp);
484 }
485
486 /*
487 * Queue a drive for I/O.
488 */
489 void
490 wdstart(arg)
491 void *arg;
492 {
493 struct wd_softc *wd = arg;
494 struct buf *dp, *bp=0;
495
496 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
497 DEBUG_XFERS);
498 while (wd->openings > 0) {
499
500 /* Is there a buf for us ? */
501 dp = &wd->sc_q;
502 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
503 return;
504 dp->b_actf = bp->b_actf;
505
506 /*
507 * Make the command. First lock the device
508 */
509 wd->openings--;
510
511 wd->retries = 0;
512 __wdstart(wd, bp);
513 }
514 }
515
516 void
517 __wdstart(wd, bp)
518 struct wd_softc *wd;
519 struct buf *bp;
520 {
521 daddr_t p_offset;
522 if (WDPART(bp->b_dev) != RAW_PART)
523 p_offset =
524 wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
525 else
526 p_offset = 0;
527 wd->sc_wdc_bio.blkno = bp->b_blkno + p_offset;
528 wd->sc_wdc_bio.blkno /= (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
529 wd->sc_wdc_bio.blkdone =0;
530 wd->sc_bp = bp;
531 /*
532 * If we're retrying, retry in single-sector mode. This will give us
533 * the sector number of the problem, and will eventually allow the
534 * transfer to succeed.
535 */
536 if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
537 wd->sc_wdc_bio.flags = ATA_SINGLE;
538 else
539 wd->sc_wdc_bio.flags = 0;
540 if (wd->sc_flags & WDF_LBA)
541 wd->sc_wdc_bio.flags |= ATA_LBA;
542 if (bp->b_flags & B_READ)
543 wd->sc_wdc_bio.flags |= ATA_READ;
544 wd->sc_wdc_bio.bcount = bp->b_bcount;
545 wd->sc_wdc_bio.databuf = bp->b_data;
546 /* Instrumentation. */
547 disk_busy(&wd->sc_dk);
548 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
549 case WDC_TRY_AGAIN:
550 timeout(wdrestart, wd, hz);
551 break;
552 case WDC_QUEUED:
553 case WDC_COMPLETE:
554 break;
555 default:
556 panic("__wdstart: bad return code from wdc_ata_bio()");
557 }
558 }
559
560 void
561 wddone(v)
562 void *v;
563 {
564 struct wd_softc *wd = v;
565 struct buf *bp = wd->sc_bp;
566 char buf[256], *errbuf = buf;
567 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
568 DEBUG_XFERS);
569
570 bp->b_resid = wd->sc_wdc_bio.bcount;
571 errbuf[0] = '\0';
572 switch (wd->sc_wdc_bio.error) {
573 case ERR_DMA:
574 errbuf = "DMA error";
575 goto retry;
576 case ERR_DF:
577 errbuf = "device fault";
578 goto retry;
579 case TIMEOUT:
580 errbuf = "device timeout";
581 goto retry;
582 case ERROR:
583 /* Don't care about media change bits */
584 if (wd->sc_wdc_bio.r_error != 0 &&
585 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
586 goto noerror;
587 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
588 retry: /* Just reset and retry. Can we do more ? */
589 wdc_reset_channel(wd->drvp);
590 diskerr(bp, "wd", errbuf, LOG_PRINTF,
591 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
592 if (wd->retries++ < WDIORETRIES) {
593 printf(", retrying\n");
594 timeout(wdrestart, wd, RECOVERYTIME);
595 return;
596 }
597 printf("\n");
598 bp->b_flags |= B_ERROR;
599 bp->b_error = EIO;
600 break;
601 case NOERROR:
602 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
603 printf("%s: soft error (corrected)\n",
604 wd->sc_dev.dv_xname);
605 break;
606 case ERR_NODEV:
607 bp->b_flags |= B_ERROR;
608 bp->b_error = EIO;
609 break;
610 }
611 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
612 #if NRND > 0
613 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
614 #endif
615 biodone(bp);
616 wd->openings++;
617 wdstart(wd);
618 }
619
620 void
621 wdrestart(v)
622 void *v;
623 {
624 struct wd_softc *wd = v;
625 struct buf *bp = wd->sc_bp;
626 int s;
627 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
628 DEBUG_XFERS);
629
630 s = splbio();
631 __wdstart(v, bp);
632 splx(s);
633 }
634
635 int
636 wdread(dev, uio, flags)
637 dev_t dev;
638 struct uio *uio;
639 int flags;
640 {
641
642 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
643 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
644 }
645
646 int
647 wdwrite(dev, uio, flags)
648 dev_t dev;
649 struct uio *uio;
650 int flags;
651 {
652
653 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
654 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
655 }
656
657 /*
658 * Wait interruptibly for an exclusive lock.
659 *
660 * XXX
661 * Several drivers do this; it should be abstracted and made MP-safe.
662 */
663 int
664 wdlock(wd)
665 struct wd_softc *wd;
666 {
667 int error;
668 int s;
669
670 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
671
672 s = splbio();
673
674 while ((wd->sc_flags & WDF_LOCKED) != 0) {
675 wd->sc_flags |= WDF_WANTED;
676 if ((error = tsleep(wd, PRIBIO | PCATCH,
677 "wdlck", 0)) != 0) {
678 splx(s);
679 return error;
680 }
681 }
682 wd->sc_flags |= WDF_LOCKED;
683 splx(s);
684 return 0;
685 }
686
687 /*
688 * Unlock and wake up any waiters.
689 */
690 void
691 wdunlock(wd)
692 struct wd_softc *wd;
693 {
694
695 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
696
697 wd->sc_flags &= ~WDF_LOCKED;
698 if ((wd->sc_flags & WDF_WANTED) != 0) {
699 wd->sc_flags &= ~WDF_WANTED;
700 wakeup(wd);
701 }
702 }
703
704 int
705 wdopen(dev, flag, fmt, p)
706 dev_t dev;
707 int flag, fmt;
708 struct proc *p;
709 {
710 struct wd_softc *wd;
711 int unit, part;
712 int error;
713
714 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
715 unit = WDUNIT(dev);
716 if (unit >= wd_cd.cd_ndevs)
717 return ENXIO;
718 wd = wd_cd.cd_devs[unit];
719 if (wd == NULL)
720 return ENXIO;
721
722 /*
723 * If this is the first open of this device, add a reference
724 * to the adapter.
725 */
726 if (wd->sc_dk.dk_openmask == 0 &&
727 (error = wdc_ata_addref(wd->drvp)) != 0)
728 return (error);
729
730 if ((error = wdlock(wd)) != 0)
731 goto bad4;
732
733 if (wd->sc_dk.dk_openmask != 0) {
734 /*
735 * If any partition is open, but the disk has been invalidated,
736 * disallow further opens.
737 */
738 if ((wd->sc_flags & WDF_LOADED) == 0) {
739 error = EIO;
740 goto bad3;
741 }
742 } else {
743 if ((wd->sc_flags & WDF_LOADED) == 0) {
744 wd->sc_flags |= WDF_LOADED;
745
746 /* Load the physical device parameters. */
747 wd_get_params(wd, AT_WAIT, &wd->sc_params);
748
749 /* Load the partition info if not already loaded. */
750 wdgetdisklabel(wd);
751 }
752 }
753
754 part = WDPART(dev);
755
756 /* Check that the partition exists. */
757 if (part != RAW_PART &&
758 (part >= wd->sc_dk.dk_label->d_npartitions ||
759 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
760 error = ENXIO;
761 goto bad;
762 }
763
764 /* Insure only one open at a time. */
765 switch (fmt) {
766 case S_IFCHR:
767 wd->sc_dk.dk_copenmask |= (1 << part);
768 break;
769 case S_IFBLK:
770 wd->sc_dk.dk_bopenmask |= (1 << part);
771 break;
772 }
773 wd->sc_dk.dk_openmask =
774 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
775
776 wdunlock(wd);
777 return 0;
778
779 bad:
780 if (wd->sc_dk.dk_openmask == 0) {
781 }
782
783 bad3:
784 wdunlock(wd);
785 bad4:
786 if (wd->sc_dk.dk_openmask == 0)
787 wdc_ata_delref(wd->drvp);
788 return error;
789 }
790
791 int
792 wdclose(dev, flag, fmt, p)
793 dev_t dev;
794 int flag, fmt;
795 struct proc *p;
796 {
797 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
798 int part = WDPART(dev);
799 int error;
800
801 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
802 if ((error = wdlock(wd)) != 0)
803 return error;
804
805 switch (fmt) {
806 case S_IFCHR:
807 wd->sc_dk.dk_copenmask &= ~(1 << part);
808 break;
809 case S_IFBLK:
810 wd->sc_dk.dk_bopenmask &= ~(1 << part);
811 break;
812 }
813 wd->sc_dk.dk_openmask =
814 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
815
816 if (wd->sc_dk.dk_openmask == 0) {
817 wd_flushcache(wd, AT_WAIT);
818 /* XXXX Must wait for I/O to complete! */
819
820 if (! (wd->sc_flags & WDF_KLABEL))
821 wd->sc_flags &= ~WDF_LOADED;
822
823 wdc_ata_delref(wd->drvp);
824 }
825
826 wdunlock(wd);
827 return 0;
828 }
829
830 void
831 wdgetdefaultlabel(wd, lp)
832 struct wd_softc *wd;
833 struct disklabel *lp;
834 {
835
836 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
837 memset(lp, 0, sizeof(struct disklabel));
838
839 lp->d_secsize = DEV_BSIZE;
840 lp->d_ntracks = wd->sc_params.atap_heads;
841 lp->d_nsectors = wd->sc_params.atap_sectors;
842 lp->d_ncylinders = wd->sc_params.atap_cylinders;
843 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
844
845 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
846 lp->d_type = DTYPE_ST506;
847 else
848 lp->d_type = DTYPE_ESDI;
849
850 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
851 strncpy(lp->d_packname, "fictitious", 16);
852 lp->d_secperunit = wd->sc_capacity;
853 lp->d_rpm = 3600;
854 lp->d_interleave = 1;
855 lp->d_flags = 0;
856
857 lp->d_partitions[RAW_PART].p_offset = 0;
858 lp->d_partitions[RAW_PART].p_size =
859 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
860 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
861 lp->d_npartitions = RAW_PART + 1;
862
863 lp->d_magic = DISKMAGIC;
864 lp->d_magic2 = DISKMAGIC;
865 lp->d_checksum = dkcksum(lp);
866 }
867
868 /*
869 * Fabricate a default disk label, and try to read the correct one.
870 */
871 void
872 wdgetdisklabel(wd)
873 struct wd_softc *wd;
874 {
875 struct disklabel *lp = wd->sc_dk.dk_label;
876 char *errstring;
877
878 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
879
880 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
881
882 wdgetdefaultlabel(wd, lp);
883
884 wd->sc_badsect[0] = -1;
885
886 if (wd->drvp->state > RECAL)
887 wd->drvp->drive_flags |= DRIVE_RESET;
888 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
889 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
890 if (errstring) {
891 /*
892 * This probably happened because the drive's default
893 * geometry doesn't match the DOS geometry. We
894 * assume the DOS geometry is now in the label and try
895 * again. XXX This is a kluge.
896 */
897 if (wd->drvp->state > RECAL)
898 wd->drvp->drive_flags |= DRIVE_RESET;
899 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
900 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
901 }
902 if (errstring) {
903 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
904 return;
905 }
906
907 if (wd->drvp->state > RECAL)
908 wd->drvp->drive_flags |= DRIVE_RESET;
909 #ifdef HAS_BAD144_HANDLING
910 if ((lp->d_flags & D_BADSECT) != 0)
911 bad144intern(wd);
912 #endif
913 }
914
915 int
916 wdioctl(dev, xfer, addr, flag, p)
917 dev_t dev;
918 u_long xfer;
919 caddr_t addr;
920 int flag;
921 struct proc *p;
922 {
923 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
924 int error;
925
926 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
927
928 if ((wd->sc_flags & WDF_LOADED) == 0)
929 return EIO;
930
931 switch (xfer) {
932 #ifdef HAS_BAD144_HANDLING
933 case DIOCSBAD:
934 if ((flag & FWRITE) == 0)
935 return EBADF;
936 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
937 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
938 bad144intern(wd);
939 return 0;
940 #endif
941
942 case DIOCGDINFO:
943 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
944 return 0;
945
946 case DIOCGPART:
947 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
948 ((struct partinfo *)addr)->part =
949 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
950 return 0;
951
952 case DIOCWDINFO:
953 case DIOCSDINFO:
954 if ((flag & FWRITE) == 0)
955 return EBADF;
956
957 if ((error = wdlock(wd)) != 0)
958 return error;
959 wd->sc_flags |= WDF_LABELLING;
960
961 error = setdisklabel(wd->sc_dk.dk_label,
962 (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
963 wd->sc_dk.dk_cpulabel);
964 if (error == 0) {
965 if (wd->drvp->state > RECAL)
966 wd->drvp->drive_flags |= DRIVE_RESET;
967 if (xfer == DIOCWDINFO)
968 error = writedisklabel(WDLABELDEV(dev),
969 wdstrategy, wd->sc_dk.dk_label,
970 wd->sc_dk.dk_cpulabel);
971 }
972
973 wd->sc_flags &= ~WDF_LABELLING;
974 wdunlock(wd);
975 return error;
976
977 case DIOCKLABEL:
978 if (*(int *)addr)
979 wd->sc_flags |= WDF_KLABEL;
980 else
981 wd->sc_flags &= ~WDF_KLABEL;
982 return 0;
983
984 case DIOCWLABEL:
985 if ((flag & FWRITE) == 0)
986 return EBADF;
987 if (*(int *)addr)
988 wd->sc_flags |= WDF_WLABEL;
989 else
990 wd->sc_flags &= ~WDF_WLABEL;
991 return 0;
992
993 case DIOCGDEFLABEL:
994 wdgetdefaultlabel(wd, (struct disklabel *)addr);
995 return 0;
996
997 #ifdef notyet
998 case DIOCWFORMAT:
999 if ((flag & FWRITE) == 0)
1000 return EBADF;
1001 {
1002 register struct format_op *fop;
1003 struct iovec aiov;
1004 struct uio auio;
1005
1006 fop = (struct format_op *)addr;
1007 aiov.iov_base = fop->df_buf;
1008 aiov.iov_len = fop->df_count;
1009 auio.uio_iov = &aiov;
1010 auio.uio_iovcnt = 1;
1011 auio.uio_resid = fop->df_count;
1012 auio.uio_segflg = 0;
1013 auio.uio_offset =
1014 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1015 auio.uio_procp = p;
1016 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1017 &auio);
1018 fop->df_count -= auio.uio_resid;
1019 fop->df_reg[0] = wdc->sc_status;
1020 fop->df_reg[1] = wdc->sc_error;
1021 return error;
1022 }
1023 #endif
1024
1025 case ATAIOCCOMMAND:
1026 /*
1027 * Make sure this command is (relatively) safe first
1028 */
1029 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1030 (flag & FWRITE) == 0)
1031 return (EBADF);
1032 {
1033 struct wd_ioctl *wi;
1034 atareq_t *atareq = (atareq_t *) addr;
1035 int error;
1036
1037 wi = wi_get();
1038 wi->wi_softc = wd;
1039 wi->wi_atareq = *atareq;
1040
1041 if (atareq->datalen && atareq->flags &
1042 (ATACMD_READ | ATACMD_WRITE)) {
1043 wi->wi_iov.iov_base = atareq->databuf;
1044 wi->wi_iov.iov_len = atareq->datalen;
1045 wi->wi_uio.uio_iov = &wi->wi_iov;
1046 wi->wi_uio.uio_iovcnt = 1;
1047 wi->wi_uio.uio_resid = atareq->datalen;
1048 wi->wi_uio.uio_offset = 0;
1049 wi->wi_uio.uio_segflg = UIO_USERSPACE;
1050 wi->wi_uio.uio_rw =
1051 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1052 wi->wi_uio.uio_procp = p;
1053 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
1054 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1055 minphys, &wi->wi_uio);
1056 } else {
1057 /* No need to call physio if we don't have any
1058 user data */
1059 wi->wi_bp.b_flags = 0;
1060 wi->wi_bp.b_data = 0;
1061 wi->wi_bp.b_bcount = 0;
1062 wi->wi_bp.b_dev = 0;
1063 wi->wi_bp.b_proc = p;
1064 wdioctlstrategy(&wi->wi_bp);
1065 error = wi->wi_bp.b_error;
1066 }
1067 *atareq = wi->wi_atareq;
1068 wi_free(wi);
1069 return(error);
1070 }
1071
1072 default:
1073 return ENOTTY;
1074 }
1075
1076 #ifdef DIAGNOSTIC
1077 panic("wdioctl: impossible");
1078 #endif
1079 }
1080
1081 #ifdef B_FORMAT
1082 int
1083 wdformat(struct buf *bp)
1084 {
1085
1086 bp->b_flags |= B_FORMAT;
1087 return wdstrategy(bp);
1088 }
1089 #endif
1090
1091 int
1092 wdsize(dev)
1093 dev_t dev;
1094 {
1095 struct wd_softc *wd;
1096 int part, unit, omask;
1097 int size;
1098
1099 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1100
1101 unit = WDUNIT(dev);
1102 if (unit >= wd_cd.cd_ndevs)
1103 return (-1);
1104 wd = wd_cd.cd_devs[unit];
1105 if (wd == NULL)
1106 return (-1);
1107
1108 part = WDPART(dev);
1109 omask = wd->sc_dk.dk_openmask & (1 << part);
1110
1111 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1112 return (-1);
1113 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1114 size = -1;
1115 else
1116 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1117 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1118 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1119 return (-1);
1120 return (size);
1121 }
1122
1123 #ifndef __BDEVSW_DUMP_OLD_TYPE
1124 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1125 static int wddoingadump = 0;
1126 static int wddumprecalibrated = 0;
1127 static int wddumpmulti = 1;
1128
1129 /*
1130 * Dump core after a system crash.
1131 */
1132 int
1133 wddump(dev, blkno, va, size)
1134 dev_t dev;
1135 daddr_t blkno;
1136 caddr_t va;
1137 size_t size;
1138 {
1139 struct wd_softc *wd; /* disk unit to do the I/O */
1140 struct disklabel *lp; /* disk's disklabel */
1141 int unit, part;
1142 int nblks; /* total number of sectors left to write */
1143 int err;
1144 char errbuf[256];
1145
1146 /* Check if recursive dump; if so, punt. */
1147 if (wddoingadump)
1148 return EFAULT;
1149 wddoingadump = 1;
1150
1151 unit = WDUNIT(dev);
1152 if (unit >= wd_cd.cd_ndevs)
1153 return ENXIO;
1154 wd = wd_cd.cd_devs[unit];
1155 if (wd == (struct wd_softc *)0)
1156 return ENXIO;
1157
1158 part = WDPART(dev);
1159
1160 /* Make sure it was initialized. */
1161 if (wd->drvp->state < READY)
1162 return ENXIO;
1163
1164 /* Convert to disk sectors. Request must be a multiple of size. */
1165 lp = wd->sc_dk.dk_label;
1166 if ((size % lp->d_secsize) != 0)
1167 return EFAULT;
1168 nblks = size / lp->d_secsize;
1169 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1170
1171 /* Check transfer bounds against partition size. */
1172 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1173 return EINVAL;
1174
1175 /* Offset block number to start of partition. */
1176 blkno += lp->d_partitions[part].p_offset;
1177
1178 /* Recalibrate, if first dump transfer. */
1179 if (wddumprecalibrated == 0) {
1180 wddumpmulti = wd->sc_multi;
1181 wddumprecalibrated = 1;
1182 wd->drvp->state = RECAL;
1183 }
1184
1185 while (nblks > 0) {
1186 again:
1187 wd->sc_wdc_bio.blkno = blkno;
1188 wd->sc_wdc_bio.flags = ATA_POLL;
1189 if (wddumpmulti == 1)
1190 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1191 if (wd->sc_flags & WDF_LBA)
1192 wd->sc_wdc_bio.flags |= ATA_LBA;
1193 wd->sc_wdc_bio.bcount =
1194 min(nblks, wddumpmulti) * lp->d_secsize;
1195 wd->sc_wdc_bio.databuf = va;
1196 #ifndef WD_DUMP_NOT_TRUSTED
1197 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1198 case WDC_TRY_AGAIN:
1199 panic("wddump: try again");
1200 break;
1201 case WDC_QUEUED:
1202 panic("wddump: polled command has been queued");
1203 break;
1204 case WDC_COMPLETE:
1205 break;
1206 }
1207 switch(wd->sc_wdc_bio.error) {
1208 case TIMEOUT:
1209 printf("wddump: device timed out");
1210 err = EIO;
1211 break;
1212 case ERR_DF:
1213 printf("wddump: drive fault");
1214 err = EIO;
1215 break;
1216 case ERR_DMA:
1217 printf("wddump: DMA error");
1218 err = EIO;
1219 break;
1220 case ERROR:
1221 errbuf[0] = '\0';
1222 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
1223 printf("wddump: %s", errbuf);
1224 err = EIO;
1225 break;
1226 case NOERROR:
1227 err = 0;
1228 break;
1229 default:
1230 panic("wddump: unknown error type");
1231 }
1232 if (err != 0) {
1233 if (wddumpmulti != 1) {
1234 wddumpmulti = 1; /* retry in single-sector */
1235 printf(", retrying\n");
1236 goto again;
1237 }
1238 printf("\n");
1239 return err;
1240 }
1241 #else /* WD_DUMP_NOT_TRUSTED */
1242 /* Let's just talk about this first... */
1243 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1244 unit, va, cylin, head, sector);
1245 delay(500 * 1000); /* half a second */
1246 #endif
1247
1248 /* update block count */
1249 nblks -= min(nblks, wddumpmulti);
1250 blkno += min(nblks, wddumpmulti);
1251 va += min(nblks, wddumpmulti) * lp->d_secsize;
1252 }
1253
1254 wddoingadump = 0;
1255 return 0;
1256 }
1257 #else /* __BDEVSW_DUMP_NEW_TYPE */
1258
1259
1260 int
1261 wddump(dev, blkno, va, size)
1262 dev_t dev;
1263 daddr_t blkno;
1264 caddr_t va;
1265 size_t size;
1266 {
1267
1268 /* Not implemented. */
1269 return ENXIO;
1270 }
1271 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1272
1273 #ifdef HAS_BAD144_HANDLING
1274 /*
1275 * Internalize the bad sector table.
1276 */
1277 void
1278 bad144intern(wd)
1279 struct wd_softc *wd;
1280 {
1281 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1282 struct disklabel *lp = wd->sc_dk.dk_label;
1283 int i = 0;
1284
1285 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1286
1287 for (; i < NBT_BAD; i++) {
1288 if (bt->bt_bad[i].bt_cyl == 0xffff)
1289 break;
1290 wd->sc_badsect[i] =
1291 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1292 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1293 (bt->bt_bad[i].bt_trksec & 0xff);
1294 }
1295 for (; i < NBT_BAD+1; i++)
1296 wd->sc_badsect[i] = -1;
1297 }
1298 #endif
1299
1300 int
1301 wd_get_params(wd, flags, params)
1302 struct wd_softc *wd;
1303 u_int8_t flags;
1304 struct ataparams *params;
1305 {
1306 switch (ata_get_params(wd->drvp, flags, params)) {
1307 case CMD_AGAIN:
1308 return 1;
1309 case CMD_ERR:
1310 /*
1311 * We `know' there's a drive here; just assume it's old.
1312 * This geometry is only used to read the MBR and print a
1313 * (false) attach message.
1314 */
1315 strncpy(params->atap_model, "ST506",
1316 sizeof params->atap_model);
1317 params->atap_config = ATA_CFG_FIXED;
1318 params->atap_cylinders = 1024;
1319 params->atap_heads = 8;
1320 params->atap_sectors = 17;
1321 params->atap_multi = 1;
1322 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1323 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1324 return 0;
1325 case CMD_OK:
1326 return 0;
1327 default:
1328 panic("wd_get_params: bad return code from ata_get_params");
1329 /* NOTREACHED */
1330 }
1331 }
1332
1333 void
1334 wd_flushcache(wd, flags)
1335 struct wd_softc *wd;
1336 int flags;
1337 {
1338 struct wdc_command wdc_c;
1339
1340 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1341 return;
1342 memset(&wdc_c, 0, sizeof(struct wdc_command));
1343 wdc_c.r_command = WDCC_FLUSHCACHE;
1344 wdc_c.r_st_bmask = WDCS_DRDY;
1345 wdc_c.r_st_pmask = WDCS_DRDY;
1346 wdc_c.flags = flags;
1347 wdc_c.timeout = 30000; /* 30s timeout */
1348 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1349 printf("%s: flush cache command didn't complete\n",
1350 wd->sc_dev.dv_xname);
1351 }
1352 if (wdc_c.flags & AT_TIMEOU) {
1353 printf("%s: flush cache command timeout\n",
1354 wd->sc_dev.dv_xname);
1355 }
1356 if (wdc_c.flags & AT_DF) {
1357 printf("%s: flush cache command: drive fault\n",
1358 wd->sc_dev.dv_xname);
1359 }
1360 /*
1361 * Ignore error register, it shouldn't report anything else
1362 * than COMMAND ABORTED, which means the device doesn't support
1363 * flush cache
1364 */
1365 }
1366
1367 void
1368 wd_shutdown(arg)
1369 void *arg;
1370 {
1371 struct wd_softc *wd = arg;
1372 wd_flushcache(wd, AT_POLL);
1373 }
1374
1375 /*
1376 * Allocate space for a ioctl queue structure. Mostly taken from
1377 * scsipi_ioctl.c
1378 */
1379 struct wd_ioctl *
1380 wi_get()
1381 {
1382 struct wd_ioctl *wi;
1383 int s;
1384
1385 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK);
1386 memset(wi, 0, sizeof (struct wd_ioctl));
1387 s = splbio();
1388 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1389 splx(s);
1390 return (wi);
1391 }
1392
1393 /*
1394 * Free an ioctl structure and remove it from our list
1395 */
1396
1397 void
1398 wi_free(wi)
1399 struct wd_ioctl *wi;
1400 {
1401 int s;
1402
1403 s = splbio();
1404 LIST_REMOVE(wi, wi_list);
1405 splx(s);
1406 free(wi, M_TEMP);
1407 }
1408
1409 /*
1410 * Find a wd_ioctl structure based on the struct buf.
1411 */
1412
1413 struct wd_ioctl *
1414 wi_find(bp)
1415 struct buf *bp;
1416 {
1417 struct wd_ioctl *wi;
1418 int s;
1419
1420 s = splbio();
1421 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1422 if (bp == &wi->wi_bp)
1423 break;
1424 splx(s);
1425 return (wi);
1426 }
1427
1428 /*
1429 * Ioctl pseudo strategy routine
1430 *
1431 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1432 * happens here is:
1433 *
1434 * - wdioctl() queues a wd_ioctl structure.
1435 *
1436 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1437 * user space I/O is required. If physio() is called, physio() eventually
1438 * calls wdioctlstrategy().
1439 *
1440 * - In either case, wdioctlstrategy() calls wdc_exec_command()
1441 * to perform the actual command
1442 *
1443 * The reason for the use of the pseudo strategy routine is because
1444 * when doing I/O to/from user space, physio _really_ wants to be in
1445 * the loop. We could put the entire buffer into the ioctl request
1446 * structure, but that won't scale if we want to do things like download
1447 * microcode.
1448 */
1449
1450 void
1451 wdioctlstrategy(bp)
1452 struct buf *bp;
1453 {
1454 struct wd_ioctl *wi;
1455 struct wdc_command wdc_c;
1456 int error = 0;
1457
1458 wi = wi_find(bp);
1459 if (wi == NULL) {
1460 printf("user_strat: No ioctl\n");
1461 error = EINVAL;
1462 goto bad;
1463 }
1464
1465 memset(&wdc_c, 0, sizeof(wdc_c));
1466
1467 /*
1468 * Abort if physio broke up the transfer
1469 */
1470
1471 if (bp->b_bcount != wi->wi_atareq.datalen) {
1472 printf("physio split wd ioctl request... cannot proceed\n");
1473 error = EIO;
1474 goto bad;
1475 }
1476
1477 /*
1478 * Abort if we didn't get a buffer size that was a multiple of
1479 * our sector size (or was larger than NBBY)
1480 */
1481
1482 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1483 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1484 (1 << NBBY)) {
1485 error = EINVAL;
1486 goto bad;
1487 }
1488
1489 /*
1490 * Make sure a timeout was supplied in the ioctl request
1491 */
1492
1493 if (wi->wi_atareq.timeout == 0) {
1494 error = EINVAL;
1495 goto bad;
1496 }
1497
1498 if (wi->wi_atareq.flags & ATACMD_READ)
1499 wdc_c.flags |= AT_READ;
1500 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1501 wdc_c.flags |= AT_WRITE;
1502
1503 if (wi->wi_atareq.flags & ATACMD_READREG)
1504 wdc_c.flags |= AT_READREG;
1505
1506 wdc_c.flags |= AT_WAIT;
1507
1508 wdc_c.timeout = wi->wi_atareq.timeout;
1509 wdc_c.r_command = wi->wi_atareq.command;
1510 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1511 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1512 wdc_c.r_sector = wi->wi_atareq.sec_num;
1513 wdc_c.r_count = wi->wi_atareq.sec_count;
1514 wdc_c.r_precomp = wi->wi_atareq.features;
1515 wdc_c.r_st_bmask = WDCS_DRDY;
1516 wdc_c.r_st_pmask = WDCS_DRDY;
1517 wdc_c.data = wi->wi_bp.b_data;
1518 wdc_c.bcount = wi->wi_bp.b_bcount;
1519
1520 if (wdc_exec_command(wi->wi_softc->drvp, &wdc_c) != WDC_COMPLETE) {
1521 wi->wi_atareq.retsts = ATACMD_ERROR;
1522 goto bad;
1523 }
1524
1525 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1526 if (wdc_c.flags & AT_ERROR) {
1527 wi->wi_atareq.retsts = ATACMD_ERROR;
1528 wi->wi_atareq.error = wdc_c.r_error;
1529 } else if (wdc_c.flags & AT_DF)
1530 wi->wi_atareq.retsts = ATACMD_DF;
1531 else
1532 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1533 } else {
1534 wi->wi_atareq.retsts = ATACMD_OK;
1535 if (wi->wi_atareq.flags & ATACMD_READREG) {
1536 wi->wi_atareq.head = wdc_c.r_head ;
1537 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1538 wi->wi_atareq.sec_num = wdc_c.r_sector;
1539 wi->wi_atareq.sec_count = wdc_c.r_count;
1540 wi->wi_atareq.features = wdc_c.r_precomp;
1541 wi->wi_atareq.error = wdc_c.r_error;
1542 }
1543 }
1544
1545 bp->b_error = 0;
1546 biodone(bp);
1547 return;
1548 bad:
1549 bp->b_flags |= B_ERROR;
1550 bp->b_error = error;
1551 biodone(bp);
1552 }
1553