wd.c revision 1.201 1 /* $NetBSD: wd.c,v 1.201 2000/01/24 14:51:07 enami 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_queue 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], pbuf[9], c, *p, *q;
262 WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
263
264 BUFQ_INIT(&wd->sc_q);
265
266 wd->openings = aa_link->aa_openings;
267 wd->drvp = aa_link->aa_drv_data;;
268 wd->wdc_softc = parent;
269 /* give back our softc to our caller */
270 wd->drvp->drv_softc = &wd->sc_dev;
271
272 /* read our drive info */
273 if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
274 printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
275 return;
276 }
277
278 for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
279 i < sizeof(wd->sc_params.atap_model); i++) {
280 c = *p++;
281 if (c == '\0')
282 break;
283 if (c != ' ') {
284 if (blank) {
285 *q++ = ' ';
286 blank = 0;
287 }
288 *q++ = c;
289 } else
290 blank = 1;
291 }
292 *q++ = '\0';
293
294 printf(": <%s>\n", buf);
295
296 if ((wd->sc_params.atap_multi & 0xff) > 1) {
297 wd->sc_multi = wd->sc_params.atap_multi & 0xff;
298 } else {
299 wd->sc_multi = 1;
300 }
301
302 printf("%s: drive supports %d-sector pio transfers,",
303 wd->sc_dev.dv_xname, wd->sc_multi);
304
305 /* Prior to ATA-4, LBA was optional. */
306 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
307 wd->sc_flags |= WDF_LBA;
308 #if 0
309 /* ATA-4 requires LBA. */
310 if (wd->sc_params.atap_ataversion != 0xffff &&
311 wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
312 wd->sc_flags |= WDF_LBA;
313 #endif
314
315 if ((wd->sc_flags & WDF_LBA) != 0) {
316 printf(" lba addressing\n");
317 wd->sc_capacity =
318 (wd->sc_params.atap_capacity[1] << 16) |
319 wd->sc_params.atap_capacity[0];
320 } else {
321 printf(" chs addressing\n");
322 wd->sc_capacity =
323 wd->sc_params.atap_cylinders *
324 wd->sc_params.atap_heads *
325 wd->sc_params.atap_sectors;
326 }
327 format_bytes(pbuf, sizeof(pbuf),
328 (u_int64_t)wd->sc_capacity * DEV_BSIZE);
329 printf("%s: %s, %d cyl, %d head, %d sec, "
330 "%d bytes/sect x %d sectors\n",
331 self->dv_xname, pbuf, wd->sc_params.atap_cylinders,
332 wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
333 DEV_BSIZE, wd->sc_capacity);
334
335 WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
336 self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
337 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
338 /*
339 * Initialize and attach the disk structure.
340 */
341 wd->sc_dk.dk_driver = &wddkdriver;
342 wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
343 disk_attach(&wd->sc_dk);
344 wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
345 wd->sc_sdhook = shutdownhook_establish(wd_shutdown, wd);
346 if (wd->sc_sdhook == NULL)
347 printf("%s: WARNING: unable to establish shutdown hook\n",
348 wd->sc_dev.dv_xname);
349 #if NRND > 0
350 rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname,
351 RND_TYPE_DISK, 0);
352 #endif
353 }
354
355 int
356 wdactivate(self, act)
357 struct device *self;
358 enum devact act;
359 {
360 int rv = 0;
361
362 switch (act) {
363 case DVACT_ACTIVATE:
364 rv = EOPNOTSUPP;
365 break;
366
367 case DVACT_DEACTIVATE:
368 /*
369 * Nothing to do; we key off the device's DVF_ACTIVATE.
370 */
371 break;
372 }
373 return (rv);
374 }
375
376 int
377 wddetach(self, flags)
378 struct device *self;
379 int flags;
380 {
381 struct wd_softc *sc = (struct wd_softc *)self;
382 struct buf *bp;
383 int s, bmaj, cmaj, mn;
384
385 /* locate the major number */
386 for (bmaj = 0; bmaj < nblkdev; bmaj++)
387 if (bdevsw[bmaj].d_open == wdopen)
388 break;
389 for (cmaj = 0; cmaj < nchrdev; cmaj++)
390 if (cdevsw[cmaj].d_open == wdopen)
391 break;
392
393 s = splbio();
394
395 /* Kill off any queued buffers. */
396 while ((bp = BUFQ_FIRST(&sc->sc_q)) != NULL) {
397 BUFQ_REMOVE(&sc->sc_q, bp);
398 bp->b_error = EIO;
399 bp->b_flags |= B_ERROR;
400 bp->b_resid = bp->b_bcount;
401 biodone(bp);
402 }
403
404 splx(s);
405
406 /* Nuke the vnodes for any open instances. */
407 mn = WDMINOR(self->dv_unit, 0);
408 vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
409 vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
410
411 /* Detach disk. */
412 disk_detach(&sc->sc_dk);
413
414 /* Get rid of the shutdown hook. */
415 if (sc->sc_sdhook != NULL)
416 shutdownhook_disestablish(sc->sc_sdhook);
417
418 #if NRND > 0
419 /* Unhook the entropy source. */
420 rnd_detach_source(&sc->rnd_source);
421 #endif
422
423 return (0);
424 }
425
426 /*
427 * Read/write routine for a buffer. Validates the arguments and schedules the
428 * transfer. Does not wait for the transfer to complete.
429 */
430 void
431 wdstrategy(bp)
432 struct buf *bp;
433 {
434 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
435 int s;
436 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
437 DEBUG_XFERS);
438
439 /* Valid request? */
440 if (bp->b_blkno < 0 ||
441 (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
442 (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
443 bp->b_error = EINVAL;
444 goto bad;
445 }
446
447 /* If device invalidated (e.g. media change, door open), error. */
448 if ((wd->sc_flags & WDF_LOADED) == 0) {
449 bp->b_error = EIO;
450 goto bad;
451 }
452
453 /* If it's a null transfer, return immediately. */
454 if (bp->b_bcount == 0)
455 goto done;
456
457 /*
458 * Do bounds checking, adjust transfer. if error, process.
459 * If end of partition, just return.
460 */
461 if (WDPART(bp->b_dev) != RAW_PART &&
462 bounds_check_with_label(bp, wd->sc_dk.dk_label,
463 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
464 goto done;
465 /* Queue transfer on drive, activate drive and controller if idle. */
466 s = splbio();
467 disksort_blkno(&wd->sc_q, bp);
468 wdstart(wd);
469 splx(s);
470 return;
471 bad:
472 bp->b_flags |= B_ERROR;
473 done:
474 /* Toss transfer; we're done early. */
475 bp->b_resid = bp->b_bcount;
476 biodone(bp);
477 }
478
479 /*
480 * Queue a drive for I/O.
481 */
482 void
483 wdstart(arg)
484 void *arg;
485 {
486 struct wd_softc *wd = arg;
487 struct buf *bp = NULL;
488
489 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
490 DEBUG_XFERS);
491 while (wd->openings > 0) {
492
493 /* Is there a buf for us ? */
494 if ((bp = BUFQ_FIRST(&wd->sc_q)) == NULL)
495 return;
496 BUFQ_REMOVE(&wd->sc_q, bp);
497
498 /*
499 * Make the command. First lock the device
500 */
501 wd->openings--;
502
503 wd->retries = 0;
504 __wdstart(wd, bp);
505 }
506 }
507
508 void
509 __wdstart(wd, bp)
510 struct wd_softc *wd;
511 struct buf *bp;
512 {
513 daddr_t p_offset;
514 if (WDPART(bp->b_dev) != RAW_PART)
515 p_offset =
516 wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
517 else
518 p_offset = 0;
519 wd->sc_wdc_bio.blkno = bp->b_blkno + p_offset;
520 wd->sc_wdc_bio.blkno /= (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
521 wd->sc_wdc_bio.blkdone =0;
522 wd->sc_bp = bp;
523 /*
524 * If we're retrying, retry in single-sector mode. This will give us
525 * the sector number of the problem, and will eventually allow the
526 * transfer to succeed.
527 */
528 if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
529 wd->sc_wdc_bio.flags = ATA_SINGLE;
530 else
531 wd->sc_wdc_bio.flags = 0;
532 if (wd->sc_flags & WDF_LBA)
533 wd->sc_wdc_bio.flags |= ATA_LBA;
534 if (bp->b_flags & B_READ)
535 wd->sc_wdc_bio.flags |= ATA_READ;
536 wd->sc_wdc_bio.bcount = bp->b_bcount;
537 wd->sc_wdc_bio.databuf = bp->b_data;
538 /* Instrumentation. */
539 disk_busy(&wd->sc_dk);
540 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
541 case WDC_TRY_AGAIN:
542 timeout(wdrestart, wd, hz);
543 break;
544 case WDC_QUEUED:
545 case WDC_COMPLETE:
546 break;
547 default:
548 panic("__wdstart: bad return code from wdc_ata_bio()");
549 }
550 }
551
552 void
553 wddone(v)
554 void *v;
555 {
556 struct wd_softc *wd = v;
557 struct buf *bp = wd->sc_bp;
558 char buf[256], *errbuf = buf;
559 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
560 DEBUG_XFERS);
561
562 bp->b_resid = wd->sc_wdc_bio.bcount;
563 errbuf[0] = '\0';
564 switch (wd->sc_wdc_bio.error) {
565 case ERR_DMA:
566 errbuf = "DMA error";
567 goto retry;
568 case ERR_DF:
569 errbuf = "device fault";
570 goto retry;
571 case TIMEOUT:
572 errbuf = "device timeout";
573 goto retry;
574 case ERROR:
575 /* Don't care about media change bits */
576 if (wd->sc_wdc_bio.r_error != 0 &&
577 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
578 goto noerror;
579 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
580 retry: /* Just reset and retry. Can we do more ? */
581 wdc_reset_channel(wd->drvp);
582 diskerr(bp, "wd", errbuf, LOG_PRINTF,
583 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
584 if (wd->retries++ < WDIORETRIES) {
585 printf(", retrying\n");
586 timeout(wdrestart, wd, RECOVERYTIME);
587 return;
588 }
589 printf("\n");
590 bp->b_flags |= B_ERROR;
591 bp->b_error = EIO;
592 break;
593 case NOERROR:
594 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
595 printf("%s: soft error (corrected)\n",
596 wd->sc_dev.dv_xname);
597 break;
598 case ERR_NODEV:
599 bp->b_flags |= B_ERROR;
600 bp->b_error = EIO;
601 break;
602 }
603 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
604 #if NRND > 0
605 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
606 #endif
607 biodone(bp);
608 wd->openings++;
609 wdstart(wd);
610 }
611
612 void
613 wdrestart(v)
614 void *v;
615 {
616 struct wd_softc *wd = v;
617 struct buf *bp = wd->sc_bp;
618 int s;
619 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
620 DEBUG_XFERS);
621
622 s = splbio();
623 __wdstart(v, bp);
624 splx(s);
625 }
626
627 int
628 wdread(dev, uio, flags)
629 dev_t dev;
630 struct uio *uio;
631 int flags;
632 {
633
634 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
635 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
636 }
637
638 int
639 wdwrite(dev, uio, flags)
640 dev_t dev;
641 struct uio *uio;
642 int flags;
643 {
644
645 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
646 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
647 }
648
649 /*
650 * Wait interruptibly for an exclusive lock.
651 *
652 * XXX
653 * Several drivers do this; it should be abstracted and made MP-safe.
654 */
655 int
656 wdlock(wd)
657 struct wd_softc *wd;
658 {
659 int error;
660 int s;
661
662 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
663
664 s = splbio();
665
666 while ((wd->sc_flags & WDF_LOCKED) != 0) {
667 wd->sc_flags |= WDF_WANTED;
668 if ((error = tsleep(wd, PRIBIO | PCATCH,
669 "wdlck", 0)) != 0) {
670 splx(s);
671 return error;
672 }
673 }
674 wd->sc_flags |= WDF_LOCKED;
675 splx(s);
676 return 0;
677 }
678
679 /*
680 * Unlock and wake up any waiters.
681 */
682 void
683 wdunlock(wd)
684 struct wd_softc *wd;
685 {
686
687 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
688
689 wd->sc_flags &= ~WDF_LOCKED;
690 if ((wd->sc_flags & WDF_WANTED) != 0) {
691 wd->sc_flags &= ~WDF_WANTED;
692 wakeup(wd);
693 }
694 }
695
696 int
697 wdopen(dev, flag, fmt, p)
698 dev_t dev;
699 int flag, fmt;
700 struct proc *p;
701 {
702 struct wd_softc *wd;
703 int unit, part;
704 int error;
705
706 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
707 unit = WDUNIT(dev);
708 if (unit >= wd_cd.cd_ndevs)
709 return ENXIO;
710 wd = wd_cd.cd_devs[unit];
711 if (wd == NULL)
712 return ENXIO;
713
714 /*
715 * If this is the first open of this device, add a reference
716 * to the adapter.
717 */
718 if (wd->sc_dk.dk_openmask == 0 &&
719 (error = wdc_ata_addref(wd->drvp)) != 0)
720 return (error);
721
722 if ((error = wdlock(wd)) != 0)
723 goto bad4;
724
725 if (wd->sc_dk.dk_openmask != 0) {
726 /*
727 * If any partition is open, but the disk has been invalidated,
728 * disallow further opens.
729 */
730 if ((wd->sc_flags & WDF_LOADED) == 0) {
731 error = EIO;
732 goto bad3;
733 }
734 } else {
735 if ((wd->sc_flags & WDF_LOADED) == 0) {
736 wd->sc_flags |= WDF_LOADED;
737
738 /* Load the physical device parameters. */
739 wd_get_params(wd, AT_WAIT, &wd->sc_params);
740
741 /* Load the partition info if not already loaded. */
742 wdgetdisklabel(wd);
743 }
744 }
745
746 part = WDPART(dev);
747
748 /* Check that the partition exists. */
749 if (part != RAW_PART &&
750 (part >= wd->sc_dk.dk_label->d_npartitions ||
751 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
752 error = ENXIO;
753 goto bad;
754 }
755
756 /* Insure only one open at a time. */
757 switch (fmt) {
758 case S_IFCHR:
759 wd->sc_dk.dk_copenmask |= (1 << part);
760 break;
761 case S_IFBLK:
762 wd->sc_dk.dk_bopenmask |= (1 << part);
763 break;
764 }
765 wd->sc_dk.dk_openmask =
766 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
767
768 wdunlock(wd);
769 return 0;
770
771 bad:
772 if (wd->sc_dk.dk_openmask == 0) {
773 }
774
775 bad3:
776 wdunlock(wd);
777 bad4:
778 if (wd->sc_dk.dk_openmask == 0)
779 wdc_ata_delref(wd->drvp);
780 return error;
781 }
782
783 int
784 wdclose(dev, flag, fmt, p)
785 dev_t dev;
786 int flag, fmt;
787 struct proc *p;
788 {
789 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
790 int part = WDPART(dev);
791 int error;
792
793 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
794 if ((error = wdlock(wd)) != 0)
795 return error;
796
797 switch (fmt) {
798 case S_IFCHR:
799 wd->sc_dk.dk_copenmask &= ~(1 << part);
800 break;
801 case S_IFBLK:
802 wd->sc_dk.dk_bopenmask &= ~(1 << part);
803 break;
804 }
805 wd->sc_dk.dk_openmask =
806 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
807
808 if (wd->sc_dk.dk_openmask == 0) {
809 wd_flushcache(wd, AT_WAIT);
810 /* XXXX Must wait for I/O to complete! */
811
812 if (! (wd->sc_flags & WDF_KLABEL))
813 wd->sc_flags &= ~WDF_LOADED;
814
815 wdc_ata_delref(wd->drvp);
816 }
817
818 wdunlock(wd);
819 return 0;
820 }
821
822 void
823 wdgetdefaultlabel(wd, lp)
824 struct wd_softc *wd;
825 struct disklabel *lp;
826 {
827
828 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
829 memset(lp, 0, sizeof(struct disklabel));
830
831 lp->d_secsize = DEV_BSIZE;
832 lp->d_ntracks = wd->sc_params.atap_heads;
833 lp->d_nsectors = wd->sc_params.atap_sectors;
834 lp->d_ncylinders = wd->sc_params.atap_cylinders;
835 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
836
837 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
838 lp->d_type = DTYPE_ST506;
839 else
840 lp->d_type = DTYPE_ESDI;
841
842 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
843 strncpy(lp->d_packname, "fictitious", 16);
844 lp->d_secperunit = wd->sc_capacity;
845 lp->d_rpm = 3600;
846 lp->d_interleave = 1;
847 lp->d_flags = 0;
848
849 lp->d_partitions[RAW_PART].p_offset = 0;
850 lp->d_partitions[RAW_PART].p_size =
851 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
852 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
853 lp->d_npartitions = RAW_PART + 1;
854
855 lp->d_magic = DISKMAGIC;
856 lp->d_magic2 = DISKMAGIC;
857 lp->d_checksum = dkcksum(lp);
858 }
859
860 /*
861 * Fabricate a default disk label, and try to read the correct one.
862 */
863 void
864 wdgetdisklabel(wd)
865 struct wd_softc *wd;
866 {
867 struct disklabel *lp = wd->sc_dk.dk_label;
868 char *errstring;
869
870 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
871
872 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
873
874 wdgetdefaultlabel(wd, lp);
875
876 wd->sc_badsect[0] = -1;
877
878 if (wd->drvp->state > RECAL)
879 wd->drvp->drive_flags |= DRIVE_RESET;
880 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
881 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
882 if (errstring) {
883 /*
884 * This probably happened because the drive's default
885 * geometry doesn't match the DOS geometry. We
886 * assume the DOS geometry is now in the label and try
887 * again. XXX This is a kluge.
888 */
889 if (wd->drvp->state > RECAL)
890 wd->drvp->drive_flags |= DRIVE_RESET;
891 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
892 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
893 }
894 if (errstring) {
895 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
896 return;
897 }
898
899 if (wd->drvp->state > RECAL)
900 wd->drvp->drive_flags |= DRIVE_RESET;
901 #ifdef HAS_BAD144_HANDLING
902 if ((lp->d_flags & D_BADSECT) != 0)
903 bad144intern(wd);
904 #endif
905 }
906
907 int
908 wdioctl(dev, xfer, addr, flag, p)
909 dev_t dev;
910 u_long xfer;
911 caddr_t addr;
912 int flag;
913 struct proc *p;
914 {
915 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
916 int error;
917
918 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
919
920 if ((wd->sc_flags & WDF_LOADED) == 0)
921 return EIO;
922
923 switch (xfer) {
924 #ifdef HAS_BAD144_HANDLING
925 case DIOCSBAD:
926 if ((flag & FWRITE) == 0)
927 return EBADF;
928 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
929 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
930 bad144intern(wd);
931 return 0;
932 #endif
933
934 case DIOCGDINFO:
935 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
936 return 0;
937
938 case DIOCGPART:
939 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
940 ((struct partinfo *)addr)->part =
941 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
942 return 0;
943
944 case DIOCWDINFO:
945 case DIOCSDINFO:
946 if ((flag & FWRITE) == 0)
947 return EBADF;
948
949 if ((error = wdlock(wd)) != 0)
950 return error;
951 wd->sc_flags |= WDF_LABELLING;
952
953 error = setdisklabel(wd->sc_dk.dk_label,
954 (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
955 wd->sc_dk.dk_cpulabel);
956 if (error == 0) {
957 if (wd->drvp->state > RECAL)
958 wd->drvp->drive_flags |= DRIVE_RESET;
959 if (xfer == DIOCWDINFO)
960 error = writedisklabel(WDLABELDEV(dev),
961 wdstrategy, wd->sc_dk.dk_label,
962 wd->sc_dk.dk_cpulabel);
963 }
964
965 wd->sc_flags &= ~WDF_LABELLING;
966 wdunlock(wd);
967 return error;
968
969 case DIOCKLABEL:
970 if (*(int *)addr)
971 wd->sc_flags |= WDF_KLABEL;
972 else
973 wd->sc_flags &= ~WDF_KLABEL;
974 return 0;
975
976 case DIOCWLABEL:
977 if ((flag & FWRITE) == 0)
978 return EBADF;
979 if (*(int *)addr)
980 wd->sc_flags |= WDF_WLABEL;
981 else
982 wd->sc_flags &= ~WDF_WLABEL;
983 return 0;
984
985 case DIOCGDEFLABEL:
986 wdgetdefaultlabel(wd, (struct disklabel *)addr);
987 return 0;
988
989 #ifdef notyet
990 case DIOCWFORMAT:
991 if ((flag & FWRITE) == 0)
992 return EBADF;
993 {
994 register struct format_op *fop;
995 struct iovec aiov;
996 struct uio auio;
997
998 fop = (struct format_op *)addr;
999 aiov.iov_base = fop->df_buf;
1000 aiov.iov_len = fop->df_count;
1001 auio.uio_iov = &aiov;
1002 auio.uio_iovcnt = 1;
1003 auio.uio_resid = fop->df_count;
1004 auio.uio_segflg = 0;
1005 auio.uio_offset =
1006 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1007 auio.uio_procp = p;
1008 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1009 &auio);
1010 fop->df_count -= auio.uio_resid;
1011 fop->df_reg[0] = wdc->sc_status;
1012 fop->df_reg[1] = wdc->sc_error;
1013 return error;
1014 }
1015 #endif
1016
1017 case ATAIOCCOMMAND:
1018 /*
1019 * Make sure this command is (relatively) safe first
1020 */
1021 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1022 (flag & FWRITE) == 0)
1023 return (EBADF);
1024 {
1025 struct wd_ioctl *wi;
1026 atareq_t *atareq = (atareq_t *) addr;
1027 int error;
1028
1029 wi = wi_get();
1030 wi->wi_softc = wd;
1031 wi->wi_atareq = *atareq;
1032
1033 if (atareq->datalen && atareq->flags &
1034 (ATACMD_READ | ATACMD_WRITE)) {
1035 wi->wi_iov.iov_base = atareq->databuf;
1036 wi->wi_iov.iov_len = atareq->datalen;
1037 wi->wi_uio.uio_iov = &wi->wi_iov;
1038 wi->wi_uio.uio_iovcnt = 1;
1039 wi->wi_uio.uio_resid = atareq->datalen;
1040 wi->wi_uio.uio_offset = 0;
1041 wi->wi_uio.uio_segflg = UIO_USERSPACE;
1042 wi->wi_uio.uio_rw =
1043 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1044 wi->wi_uio.uio_procp = p;
1045 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
1046 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1047 minphys, &wi->wi_uio);
1048 } else {
1049 /* No need to call physio if we don't have any
1050 user data */
1051 wi->wi_bp.b_flags = 0;
1052 wi->wi_bp.b_data = 0;
1053 wi->wi_bp.b_bcount = 0;
1054 wi->wi_bp.b_dev = 0;
1055 wi->wi_bp.b_proc = p;
1056 wdioctlstrategy(&wi->wi_bp);
1057 error = wi->wi_bp.b_error;
1058 }
1059 *atareq = wi->wi_atareq;
1060 wi_free(wi);
1061 return(error);
1062 }
1063
1064 default:
1065 return ENOTTY;
1066 }
1067
1068 #ifdef DIAGNOSTIC
1069 panic("wdioctl: impossible");
1070 #endif
1071 }
1072
1073 #ifdef B_FORMAT
1074 int
1075 wdformat(struct buf *bp)
1076 {
1077
1078 bp->b_flags |= B_FORMAT;
1079 return wdstrategy(bp);
1080 }
1081 #endif
1082
1083 int
1084 wdsize(dev)
1085 dev_t dev;
1086 {
1087 struct wd_softc *wd;
1088 int part, unit, omask;
1089 int size;
1090
1091 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1092
1093 unit = WDUNIT(dev);
1094 if (unit >= wd_cd.cd_ndevs)
1095 return (-1);
1096 wd = wd_cd.cd_devs[unit];
1097 if (wd == NULL)
1098 return (-1);
1099
1100 part = WDPART(dev);
1101 omask = wd->sc_dk.dk_openmask & (1 << part);
1102
1103 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1104 return (-1);
1105 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1106 size = -1;
1107 else
1108 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1109 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1110 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1111 return (-1);
1112 return (size);
1113 }
1114
1115 #ifndef __BDEVSW_DUMP_OLD_TYPE
1116 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1117 static int wddoingadump = 0;
1118 static int wddumprecalibrated = 0;
1119 static int wddumpmulti = 1;
1120
1121 /*
1122 * Dump core after a system crash.
1123 */
1124 int
1125 wddump(dev, blkno, va, size)
1126 dev_t dev;
1127 daddr_t blkno;
1128 caddr_t va;
1129 size_t size;
1130 {
1131 struct wd_softc *wd; /* disk unit to do the I/O */
1132 struct disklabel *lp; /* disk's disklabel */
1133 int unit, part;
1134 int nblks; /* total number of sectors left to write */
1135 int err;
1136 char errbuf[256];
1137
1138 /* Check if recursive dump; if so, punt. */
1139 if (wddoingadump)
1140 return EFAULT;
1141 wddoingadump = 1;
1142
1143 unit = WDUNIT(dev);
1144 if (unit >= wd_cd.cd_ndevs)
1145 return ENXIO;
1146 wd = wd_cd.cd_devs[unit];
1147 if (wd == (struct wd_softc *)0)
1148 return ENXIO;
1149
1150 part = WDPART(dev);
1151
1152 /* Make sure it was initialized. */
1153 if (wd->drvp->state < READY)
1154 return ENXIO;
1155
1156 /* Convert to disk sectors. Request must be a multiple of size. */
1157 lp = wd->sc_dk.dk_label;
1158 if ((size % lp->d_secsize) != 0)
1159 return EFAULT;
1160 nblks = size / lp->d_secsize;
1161 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1162
1163 /* Check transfer bounds against partition size. */
1164 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1165 return EINVAL;
1166
1167 /* Offset block number to start of partition. */
1168 blkno += lp->d_partitions[part].p_offset;
1169
1170 /* Recalibrate, if first dump transfer. */
1171 if (wddumprecalibrated == 0) {
1172 wddumpmulti = wd->sc_multi;
1173 wddumprecalibrated = 1;
1174 wd->drvp->state = RECAL;
1175 }
1176
1177 while (nblks > 0) {
1178 again:
1179 wd->sc_wdc_bio.blkno = blkno;
1180 wd->sc_wdc_bio.flags = ATA_POLL;
1181 if (wddumpmulti == 1)
1182 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1183 if (wd->sc_flags & WDF_LBA)
1184 wd->sc_wdc_bio.flags |= ATA_LBA;
1185 wd->sc_wdc_bio.bcount =
1186 min(nblks, wddumpmulti) * lp->d_secsize;
1187 wd->sc_wdc_bio.databuf = va;
1188 #ifndef WD_DUMP_NOT_TRUSTED
1189 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1190 case WDC_TRY_AGAIN:
1191 panic("wddump: try again");
1192 break;
1193 case WDC_QUEUED:
1194 panic("wddump: polled command has been queued");
1195 break;
1196 case WDC_COMPLETE:
1197 break;
1198 }
1199 switch(wd->sc_wdc_bio.error) {
1200 case TIMEOUT:
1201 printf("wddump: device timed out");
1202 err = EIO;
1203 break;
1204 case ERR_DF:
1205 printf("wddump: drive fault");
1206 err = EIO;
1207 break;
1208 case ERR_DMA:
1209 printf("wddump: DMA error");
1210 err = EIO;
1211 break;
1212 case ERROR:
1213 errbuf[0] = '\0';
1214 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
1215 printf("wddump: %s", errbuf);
1216 err = EIO;
1217 break;
1218 case NOERROR:
1219 err = 0;
1220 break;
1221 default:
1222 panic("wddump: unknown error type");
1223 }
1224 if (err != 0) {
1225 if (wddumpmulti != 1) {
1226 wddumpmulti = 1; /* retry in single-sector */
1227 printf(", retrying\n");
1228 goto again;
1229 }
1230 printf("\n");
1231 return err;
1232 }
1233 #else /* WD_DUMP_NOT_TRUSTED */
1234 /* Let's just talk about this first... */
1235 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1236 unit, va, cylin, head, sector);
1237 delay(500 * 1000); /* half a second */
1238 #endif
1239
1240 /* update block count */
1241 nblks -= min(nblks, wddumpmulti);
1242 blkno += min(nblks, wddumpmulti);
1243 va += min(nblks, wddumpmulti) * lp->d_secsize;
1244 }
1245
1246 wddoingadump = 0;
1247 return 0;
1248 }
1249 #else /* __BDEVSW_DUMP_NEW_TYPE */
1250
1251
1252 int
1253 wddump(dev, blkno, va, size)
1254 dev_t dev;
1255 daddr_t blkno;
1256 caddr_t va;
1257 size_t size;
1258 {
1259
1260 /* Not implemented. */
1261 return ENXIO;
1262 }
1263 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1264
1265 #ifdef HAS_BAD144_HANDLING
1266 /*
1267 * Internalize the bad sector table.
1268 */
1269 void
1270 bad144intern(wd)
1271 struct wd_softc *wd;
1272 {
1273 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1274 struct disklabel *lp = wd->sc_dk.dk_label;
1275 int i = 0;
1276
1277 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1278
1279 for (; i < NBT_BAD; i++) {
1280 if (bt->bt_bad[i].bt_cyl == 0xffff)
1281 break;
1282 wd->sc_badsect[i] =
1283 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1284 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1285 (bt->bt_bad[i].bt_trksec & 0xff);
1286 }
1287 for (; i < NBT_BAD+1; i++)
1288 wd->sc_badsect[i] = -1;
1289 }
1290 #endif
1291
1292 int
1293 wd_get_params(wd, flags, params)
1294 struct wd_softc *wd;
1295 u_int8_t flags;
1296 struct ataparams *params;
1297 {
1298 switch (ata_get_params(wd->drvp, flags, params)) {
1299 case CMD_AGAIN:
1300 return 1;
1301 case CMD_ERR:
1302 /*
1303 * We `know' there's a drive here; just assume it's old.
1304 * This geometry is only used to read the MBR and print a
1305 * (false) attach message.
1306 */
1307 strncpy(params->atap_model, "ST506",
1308 sizeof params->atap_model);
1309 params->atap_config = ATA_CFG_FIXED;
1310 params->atap_cylinders = 1024;
1311 params->atap_heads = 8;
1312 params->atap_sectors = 17;
1313 params->atap_multi = 1;
1314 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1315 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1316 return 0;
1317 case CMD_OK:
1318 return 0;
1319 default:
1320 panic("wd_get_params: bad return code from ata_get_params");
1321 /* NOTREACHED */
1322 }
1323 }
1324
1325 void
1326 wd_flushcache(wd, flags)
1327 struct wd_softc *wd;
1328 int flags;
1329 {
1330 struct wdc_command wdc_c;
1331
1332 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1333 return;
1334 memset(&wdc_c, 0, sizeof(struct wdc_command));
1335 wdc_c.r_command = WDCC_FLUSHCACHE;
1336 wdc_c.r_st_bmask = WDCS_DRDY;
1337 wdc_c.r_st_pmask = WDCS_DRDY;
1338 wdc_c.flags = flags;
1339 wdc_c.timeout = 30000; /* 30s timeout */
1340 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1341 printf("%s: flush cache command didn't complete\n",
1342 wd->sc_dev.dv_xname);
1343 }
1344 if (wdc_c.flags & AT_TIMEOU) {
1345 printf("%s: flush cache command timeout\n",
1346 wd->sc_dev.dv_xname);
1347 }
1348 if (wdc_c.flags & AT_DF) {
1349 printf("%s: flush cache command: drive fault\n",
1350 wd->sc_dev.dv_xname);
1351 }
1352 /*
1353 * Ignore error register, it shouldn't report anything else
1354 * than COMMAND ABORTED, which means the device doesn't support
1355 * flush cache
1356 */
1357 }
1358
1359 void
1360 wd_shutdown(arg)
1361 void *arg;
1362 {
1363 struct wd_softc *wd = arg;
1364 wd_flushcache(wd, AT_POLL);
1365 }
1366
1367 /*
1368 * Allocate space for a ioctl queue structure. Mostly taken from
1369 * scsipi_ioctl.c
1370 */
1371 struct wd_ioctl *
1372 wi_get()
1373 {
1374 struct wd_ioctl *wi;
1375 int s;
1376
1377 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK);
1378 memset(wi, 0, sizeof (struct wd_ioctl));
1379 s = splbio();
1380 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1381 splx(s);
1382 return (wi);
1383 }
1384
1385 /*
1386 * Free an ioctl structure and remove it from our list
1387 */
1388
1389 void
1390 wi_free(wi)
1391 struct wd_ioctl *wi;
1392 {
1393 int s;
1394
1395 s = splbio();
1396 LIST_REMOVE(wi, wi_list);
1397 splx(s);
1398 free(wi, M_TEMP);
1399 }
1400
1401 /*
1402 * Find a wd_ioctl structure based on the struct buf.
1403 */
1404
1405 struct wd_ioctl *
1406 wi_find(bp)
1407 struct buf *bp;
1408 {
1409 struct wd_ioctl *wi;
1410 int s;
1411
1412 s = splbio();
1413 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1414 if (bp == &wi->wi_bp)
1415 break;
1416 splx(s);
1417 return (wi);
1418 }
1419
1420 /*
1421 * Ioctl pseudo strategy routine
1422 *
1423 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1424 * happens here is:
1425 *
1426 * - wdioctl() queues a wd_ioctl structure.
1427 *
1428 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1429 * user space I/O is required. If physio() is called, physio() eventually
1430 * calls wdioctlstrategy().
1431 *
1432 * - In either case, wdioctlstrategy() calls wdc_exec_command()
1433 * to perform the actual command
1434 *
1435 * The reason for the use of the pseudo strategy routine is because
1436 * when doing I/O to/from user space, physio _really_ wants to be in
1437 * the loop. We could put the entire buffer into the ioctl request
1438 * structure, but that won't scale if we want to do things like download
1439 * microcode.
1440 */
1441
1442 void
1443 wdioctlstrategy(bp)
1444 struct buf *bp;
1445 {
1446 struct wd_ioctl *wi;
1447 struct wdc_command wdc_c;
1448 int error = 0;
1449
1450 wi = wi_find(bp);
1451 if (wi == NULL) {
1452 printf("user_strat: No ioctl\n");
1453 error = EINVAL;
1454 goto bad;
1455 }
1456
1457 memset(&wdc_c, 0, sizeof(wdc_c));
1458
1459 /*
1460 * Abort if physio broke up the transfer
1461 */
1462
1463 if (bp->b_bcount != wi->wi_atareq.datalen) {
1464 printf("physio split wd ioctl request... cannot proceed\n");
1465 error = EIO;
1466 goto bad;
1467 }
1468
1469 /*
1470 * Abort if we didn't get a buffer size that was a multiple of
1471 * our sector size (or was larger than NBBY)
1472 */
1473
1474 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1475 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1476 (1 << NBBY)) {
1477 error = EINVAL;
1478 goto bad;
1479 }
1480
1481 /*
1482 * Make sure a timeout was supplied in the ioctl request
1483 */
1484
1485 if (wi->wi_atareq.timeout == 0) {
1486 error = EINVAL;
1487 goto bad;
1488 }
1489
1490 if (wi->wi_atareq.flags & ATACMD_READ)
1491 wdc_c.flags |= AT_READ;
1492 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1493 wdc_c.flags |= AT_WRITE;
1494
1495 if (wi->wi_atareq.flags & ATACMD_READREG)
1496 wdc_c.flags |= AT_READREG;
1497
1498 wdc_c.flags |= AT_WAIT;
1499
1500 wdc_c.timeout = wi->wi_atareq.timeout;
1501 wdc_c.r_command = wi->wi_atareq.command;
1502 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1503 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1504 wdc_c.r_sector = wi->wi_atareq.sec_num;
1505 wdc_c.r_count = wi->wi_atareq.sec_count;
1506 wdc_c.r_precomp = wi->wi_atareq.features;
1507 wdc_c.r_st_bmask = WDCS_DRDY;
1508 wdc_c.r_st_pmask = WDCS_DRDY;
1509 wdc_c.data = wi->wi_bp.b_data;
1510 wdc_c.bcount = wi->wi_bp.b_bcount;
1511
1512 if (wdc_exec_command(wi->wi_softc->drvp, &wdc_c) != WDC_COMPLETE) {
1513 wi->wi_atareq.retsts = ATACMD_ERROR;
1514 goto bad;
1515 }
1516
1517 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1518 if (wdc_c.flags & AT_ERROR) {
1519 wi->wi_atareq.retsts = ATACMD_ERROR;
1520 wi->wi_atareq.error = wdc_c.r_error;
1521 } else if (wdc_c.flags & AT_DF)
1522 wi->wi_atareq.retsts = ATACMD_DF;
1523 else
1524 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1525 } else {
1526 wi->wi_atareq.retsts = ATACMD_OK;
1527 if (wi->wi_atareq.flags & ATACMD_READREG) {
1528 wi->wi_atareq.head = wdc_c.r_head ;
1529 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1530 wi->wi_atareq.sec_num = wdc_c.r_sector;
1531 wi->wi_atareq.sec_count = wdc_c.r_count;
1532 wi->wi_atareq.features = wdc_c.r_precomp;
1533 wi->wi_atareq.error = wdc_c.r_error;
1534 }
1535 }
1536
1537 bp->b_error = 0;
1538 biodone(bp);
1539 return;
1540 bad:
1541 bp->b_flags |= B_ERROR;
1542 bp->b_error = error;
1543 biodone(bp);
1544 }
1545