wd.c revision 1.211 1 /* $NetBSD: wd.c,v 1.211 2001/01/07 18:09:01 fvdl 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 <machine/intr.h>
95 #include <machine/bus.h>
96
97 #include <dev/ata/atareg.h>
98 #include <dev/ata/atavar.h>
99 #include <dev/ata/wdvar.h>
100 #include <dev/ic/wdcreg.h>
101 #include <sys/ataio.h>
102 #include "locators.h"
103
104 #define WAITTIME (4 * hz) /* time to wait for a completion */
105 #define WDIORETRIES_SINGLE 4 /* number of retries before single-sector */
106 #define WDIORETRIES 5 /* number of retries before giving up */
107 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
108
109 #define WDUNIT(dev) DISKUNIT(dev)
110 #define WDPART(dev) DISKPART(dev)
111 #define WDMINOR(unit, part) DISKMINOR(unit, part)
112 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
113
114 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
115
116 #define DEBUG_INTR 0x01
117 #define DEBUG_XFERS 0x02
118 #define DEBUG_STATUS 0x04
119 #define DEBUG_FUNCS 0x08
120 #define DEBUG_PROBE 0x10
121 #ifdef WDCDEBUG
122 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
123 #define WDCDEBUG_PRINT(args, level) \
124 if (wdcdebug_wd_mask & (level)) \
125 printf args
126 #else
127 #define WDCDEBUG_PRINT(args, level)
128 #endif
129
130 struct wd_softc {
131 /* General disk infos */
132 struct device sc_dev;
133 struct disk sc_dk;
134 struct buf_queue sc_q;
135 struct callout sc_restart_ch;
136 /* IDE disk soft states */
137 struct ata_bio sc_wdc_bio; /* current transfer */
138 struct buf *sc_bp; /* buf being transfered */
139 void *wdc_softc; /* pointer to our parent */
140 struct ata_drive_datas *drvp; /* Our controller's infos */
141 int openings;
142 struct ataparams sc_params;/* drive characteistics found */
143 int sc_flags;
144 #define WDF_LOCKED 0x01
145 #define WDF_WANTED 0x02
146 #define WDF_WLABEL 0x04 /* label is writable */
147 #define WDF_LABELLING 0x08 /* writing label */
148 /*
149 * XXX Nothing resets this yet, but disk change sensing will when ATA-4 is
150 * more fully implemented.
151 */
152 #define WDF_LOADED 0x10 /* parameters loaded */
153 #define WDF_WAIT 0x20 /* waiting for resources */
154 #define WDF_LBA 0x40 /* using LBA mode */
155 #define WDF_KLABEL 0x80 /* retain label after 'full' close */
156 int sc_capacity;
157 int cyl; /* actual drive parameters */
158 int heads;
159 int sectors;
160 int retries; /* number of xfer retry */
161
162 void *sc_sdhook; /* our shutdown hook */
163
164 #if NRND > 0
165 rndsource_element_t rnd_source;
166 #endif
167 };
168
169 #define sc_drive sc_wdc_bio.drive
170 #define sc_mode sc_wdc_bio.mode
171 #define sc_multi sc_wdc_bio.multi
172 #define sc_badsect sc_wdc_bio.badsect
173
174 int wdprobe __P((struct device *, struct cfdata *, void *));
175 void wdattach __P((struct device *, struct device *, void *));
176 int wddetach __P((struct device *, int));
177 int wdactivate __P((struct device *, enum devact));
178 int wdprint __P((void *, char *));
179
180 struct cfattach wd_ca = {
181 sizeof(struct wd_softc), wdprobe, wdattach, wddetach, wdactivate
182 };
183
184 extern struct cfdriver wd_cd;
185
186 /*
187 * Glue necessary to hook WDCIOCCOMMAND into physio
188 */
189
190 struct wd_ioctl {
191 LIST_ENTRY(wd_ioctl) wi_list;
192 struct buf wi_bp;
193 struct uio wi_uio;
194 struct iovec wi_iov;
195 atareq_t wi_atareq;
196 struct wd_softc *wi_softc;
197 };
198
199 LIST_HEAD(, wd_ioctl) wi_head;
200
201 struct wd_ioctl *wi_find __P((struct buf *));
202 void wi_free __P((struct wd_ioctl *));
203 struct wd_ioctl *wi_get __P((void));
204 void wdioctlstrategy __P((struct buf *));
205
206 void wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
207 void wdgetdisklabel __P((struct wd_softc *));
208 void wdstrategy __P((struct buf *));
209 void wdstart __P((void *));
210 void __wdstart __P((struct wd_softc*, struct buf *));
211 void wdrestart __P((void*));
212 int wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
213 void wd_flushcache __P((struct wd_softc *, int));
214 void wd_shutdown __P((void*));
215
216 struct dkdriver wddkdriver = { wdstrategy };
217
218 /* XXX: these should go elsewhere */
219 cdev_decl(wd);
220 bdev_decl(wd);
221
222 #ifdef HAS_BAD144_HANDLING
223 static void bad144intern __P((struct wd_softc *));
224 #endif
225 int wdlock __P((struct wd_softc *));
226 void wdunlock __P((struct wd_softc *));
227
228 int
229 wdprobe(parent, match, aux)
230 struct device *parent;
231 struct cfdata *match;
232
233 void *aux;
234 {
235 struct ata_atapi_attach *aa_link = aux;
236
237 if (aa_link == NULL)
238 return 0;
239 if (aa_link->aa_type != T_ATA)
240 return 0;
241
242 if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
243 match->cf_loc[ATACF_CHANNEL] != aa_link->aa_channel)
244 return 0;
245
246 if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
247 match->cf_loc[ATACF_DRIVE] != aa_link->aa_drv_data->drive)
248 return 0;
249 return 1;
250 }
251
252 void
253 wdattach(parent, self, aux)
254 struct device *parent, *self;
255 void *aux;
256 {
257 struct wd_softc *wd = (void *)self;
258 struct ata_atapi_attach *aa_link= aux;
259 int i, blank;
260 char buf[41], pbuf[9], c, *p, *q;
261 WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
262
263 callout_init(&wd->sc_restart_ch);
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 = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
435 struct disklabel *lp = wd->sc_dk.dk_label;
436 daddr_t blkno;
437 int s;
438
439 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
440 DEBUG_XFERS);
441
442 /* Valid request? */
443 if (bp->b_blkno < 0 ||
444 (bp->b_bcount % lp->d_secsize) != 0 ||
445 (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
446 bp->b_error = EINVAL;
447 goto bad;
448 }
449
450 /* If device invalidated (e.g. media change, door open), error. */
451 if ((wd->sc_flags & WDF_LOADED) == 0) {
452 bp->b_error = EIO;
453 goto bad;
454 }
455
456 /* If it's a null transfer, return immediately. */
457 if (bp->b_bcount == 0)
458 goto done;
459
460 /*
461 * Do bounds checking, adjust transfer. if error, process.
462 * If end of partition, just return.
463 */
464 if (WDPART(bp->b_dev) != RAW_PART &&
465 bounds_check_with_label(bp, wd->sc_dk.dk_label,
466 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
467 goto done;
468
469 /*
470 * Now convert the block number to absolute and put it in
471 * terms of the device's logical block size.
472 */
473 if (lp->d_secsize >= DEV_BSIZE)
474 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
475 else
476 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
477
478 if (WDPART(bp->b_dev) != RAW_PART)
479 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
480
481 bp->b_rawblkno = blkno;
482
483 /* Queue transfer on drive, activate drive and controller if idle. */
484 s = splbio();
485 disksort_blkno(&wd->sc_q, bp);
486 wdstart(wd);
487 splx(s);
488 return;
489 bad:
490 bp->b_flags |= B_ERROR;
491 done:
492 /* Toss transfer; we're done early. */
493 bp->b_resid = bp->b_bcount;
494 biodone(bp);
495 }
496
497 /*
498 * Queue a drive for I/O.
499 */
500 void
501 wdstart(arg)
502 void *arg;
503 {
504 struct wd_softc *wd = arg;
505 struct buf *bp = NULL;
506
507 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
508 DEBUG_XFERS);
509 while (wd->openings > 0) {
510
511 /* Is there a buf for us ? */
512 if ((bp = BUFQ_FIRST(&wd->sc_q)) == NULL)
513 return;
514 BUFQ_REMOVE(&wd->sc_q, bp);
515
516 /*
517 * Make the command. First lock the device
518 */
519 wd->openings--;
520
521 wd->retries = 0;
522 __wdstart(wd, bp);
523 }
524 }
525
526 void
527 __wdstart(wd, bp)
528 struct wd_softc *wd;
529 struct buf *bp;
530 {
531
532 wd->sc_wdc_bio.blkno = bp->b_rawblkno;
533 wd->sc_wdc_bio.blkdone =0;
534 wd->sc_bp = bp;
535 /*
536 * If we're retrying, retry in single-sector mode. This will give us
537 * the sector number of the problem, and will eventually allow the
538 * transfer to succeed.
539 */
540 if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
541 wd->sc_wdc_bio.flags = ATA_SINGLE;
542 else
543 wd->sc_wdc_bio.flags = 0;
544 if (wd->sc_flags & WDF_LBA)
545 wd->sc_wdc_bio.flags |= ATA_LBA;
546 if (bp->b_flags & B_READ)
547 wd->sc_wdc_bio.flags |= ATA_READ;
548 wd->sc_wdc_bio.bcount = bp->b_bcount;
549 wd->sc_wdc_bio.databuf = bp->b_data;
550 /* Instrumentation. */
551 disk_busy(&wd->sc_dk);
552 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
553 case WDC_TRY_AGAIN:
554 callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
555 break;
556 case WDC_QUEUED:
557 case WDC_COMPLETE:
558 break;
559 default:
560 panic("__wdstart: bad return code from wdc_ata_bio()");
561 }
562 }
563
564 void
565 wddone(v)
566 void *v;
567 {
568 struct wd_softc *wd = v;
569 struct buf *bp = wd->sc_bp;
570 char buf[256], *errbuf = buf;
571 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
572 DEBUG_XFERS);
573
574 bp->b_resid = wd->sc_wdc_bio.bcount;
575 errbuf[0] = '\0';
576 switch (wd->sc_wdc_bio.error) {
577 case ERR_DMA:
578 errbuf = "DMA error";
579 goto retry;
580 case ERR_DF:
581 errbuf = "device fault";
582 goto retry;
583 case TIMEOUT:
584 errbuf = "device timeout";
585 goto retry;
586 case ERROR:
587 /* Don't care about media change bits */
588 if (wd->sc_wdc_bio.r_error != 0 &&
589 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
590 goto noerror;
591 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
592 retry: /* Just reset and retry. Can we do more ? */
593 wdc_reset_channel(wd->drvp);
594 diskerr(bp, "wd", errbuf, LOG_PRINTF,
595 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
596 if (wd->retries++ < WDIORETRIES) {
597 printf(", retrying\n");
598 callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
599 wdrestart, wd);
600 return;
601 }
602 printf("\n");
603 bp->b_flags |= B_ERROR;
604 bp->b_error = EIO;
605 break;
606 case NOERROR:
607 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
608 printf("%s: soft error (corrected)\n",
609 wd->sc_dev.dv_xname);
610 break;
611 case ERR_NODEV:
612 bp->b_flags |= B_ERROR;
613 bp->b_error = EIO;
614 break;
615 }
616 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
617 #if NRND > 0
618 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
619 #endif
620 biodone(bp);
621 wd->openings++;
622 wdstart(wd);
623 }
624
625 void
626 wdrestart(v)
627 void *v;
628 {
629 struct wd_softc *wd = v;
630 struct buf *bp = wd->sc_bp;
631 int s;
632 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
633 DEBUG_XFERS);
634
635 s = splbio();
636 __wdstart(v, bp);
637 splx(s);
638 }
639
640 int
641 wdread(dev, uio, flags)
642 dev_t dev;
643 struct uio *uio;
644 int flags;
645 {
646
647 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
648 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
649 }
650
651 int
652 wdwrite(dev, uio, flags)
653 dev_t dev;
654 struct uio *uio;
655 int flags;
656 {
657
658 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
659 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
660 }
661
662 /*
663 * Wait interruptibly for an exclusive lock.
664 *
665 * XXX
666 * Several drivers do this; it should be abstracted and made MP-safe.
667 */
668 int
669 wdlock(wd)
670 struct wd_softc *wd;
671 {
672 int error;
673 int s;
674
675 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
676
677 s = splbio();
678
679 while ((wd->sc_flags & WDF_LOCKED) != 0) {
680 wd->sc_flags |= WDF_WANTED;
681 if ((error = tsleep(wd, PRIBIO | PCATCH,
682 "wdlck", 0)) != 0) {
683 splx(s);
684 return error;
685 }
686 }
687 wd->sc_flags |= WDF_LOCKED;
688 splx(s);
689 return 0;
690 }
691
692 /*
693 * Unlock and wake up any waiters.
694 */
695 void
696 wdunlock(wd)
697 struct wd_softc *wd;
698 {
699
700 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
701
702 wd->sc_flags &= ~WDF_LOCKED;
703 if ((wd->sc_flags & WDF_WANTED) != 0) {
704 wd->sc_flags &= ~WDF_WANTED;
705 wakeup(wd);
706 }
707 }
708
709 int
710 wdopen(dev, flag, fmt, p)
711 dev_t dev;
712 int flag, fmt;
713 struct proc *p;
714 {
715 struct wd_softc *wd;
716 int part, error;
717
718 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
719 wd = device_lookup(&wd_cd, WDUNIT(dev));
720 if (wd == NULL)
721 return (ENXIO);
722
723 /*
724 * If this is the first open of this device, add a reference
725 * to the adapter.
726 */
727 if (wd->sc_dk.dk_openmask == 0 &&
728 (error = wdc_ata_addref(wd->drvp)) != 0)
729 return (error);
730
731 if ((error = wdlock(wd)) != 0)
732 goto bad4;
733
734 if (wd->sc_dk.dk_openmask != 0) {
735 /*
736 * If any partition is open, but the disk has been invalidated,
737 * disallow further opens.
738 */
739 if ((wd->sc_flags & WDF_LOADED) == 0) {
740 error = EIO;
741 goto bad3;
742 }
743 } else {
744 if ((wd->sc_flags & WDF_LOADED) == 0) {
745 wd->sc_flags |= WDF_LOADED;
746
747 /* Load the physical device parameters. */
748 wd_get_params(wd, AT_WAIT, &wd->sc_params);
749
750 /* Load the partition info if not already loaded. */
751 wdgetdisklabel(wd);
752 }
753 }
754
755 part = WDPART(dev);
756
757 /* Check that the partition exists. */
758 if (part != RAW_PART &&
759 (part >= wd->sc_dk.dk_label->d_npartitions ||
760 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
761 error = ENXIO;
762 goto bad;
763 }
764
765 /* Insure only one open at a time. */
766 switch (fmt) {
767 case S_IFCHR:
768 wd->sc_dk.dk_copenmask |= (1 << part);
769 break;
770 case S_IFBLK:
771 wd->sc_dk.dk_bopenmask |= (1 << part);
772 break;
773 }
774 wd->sc_dk.dk_openmask =
775 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
776
777 wdunlock(wd);
778 return 0;
779
780 bad:
781 if (wd->sc_dk.dk_openmask == 0) {
782 }
783
784 bad3:
785 wdunlock(wd);
786 bad4:
787 if (wd->sc_dk.dk_openmask == 0)
788 wdc_ata_delref(wd->drvp);
789 return error;
790 }
791
792 int
793 wdclose(dev, flag, fmt, p)
794 dev_t dev;
795 int flag, fmt;
796 struct proc *p;
797 {
798 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
799 int part = WDPART(dev);
800 int error;
801
802 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
803 if ((error = wdlock(wd)) != 0)
804 return error;
805
806 switch (fmt) {
807 case S_IFCHR:
808 wd->sc_dk.dk_copenmask &= ~(1 << part);
809 break;
810 case S_IFBLK:
811 wd->sc_dk.dk_bopenmask &= ~(1 << part);
812 break;
813 }
814 wd->sc_dk.dk_openmask =
815 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
816
817 if (wd->sc_dk.dk_openmask == 0) {
818 wd_flushcache(wd, AT_WAIT);
819 /* XXXX Must wait for I/O to complete! */
820
821 if (! (wd->sc_flags & WDF_KLABEL))
822 wd->sc_flags &= ~WDF_LOADED;
823
824 wdc_ata_delref(wd->drvp);
825 }
826
827 wdunlock(wd);
828 return 0;
829 }
830
831 void
832 wdgetdefaultlabel(wd, lp)
833 struct wd_softc *wd;
834 struct disklabel *lp;
835 {
836
837 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
838 memset(lp, 0, sizeof(struct disklabel));
839
840 lp->d_secsize = DEV_BSIZE;
841 lp->d_ntracks = wd->sc_params.atap_heads;
842 lp->d_nsectors = wd->sc_params.atap_sectors;
843 lp->d_ncylinders = wd->sc_params.atap_cylinders;
844 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
845
846 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
847 lp->d_type = DTYPE_ST506;
848 else
849 lp->d_type = DTYPE_ESDI;
850
851 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
852 strncpy(lp->d_packname, "fictitious", 16);
853 lp->d_secperunit = wd->sc_capacity;
854 lp->d_rpm = 3600;
855 lp->d_interleave = 1;
856 lp->d_flags = 0;
857
858 lp->d_partitions[RAW_PART].p_offset = 0;
859 lp->d_partitions[RAW_PART].p_size =
860 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
861 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
862 lp->d_npartitions = RAW_PART + 1;
863
864 lp->d_magic = DISKMAGIC;
865 lp->d_magic2 = DISKMAGIC;
866 lp->d_checksum = dkcksum(lp);
867 }
868
869 /*
870 * Fabricate a default disk label, and try to read the correct one.
871 */
872 void
873 wdgetdisklabel(wd)
874 struct wd_softc *wd;
875 {
876 struct disklabel *lp = wd->sc_dk.dk_label;
877 char *errstring;
878
879 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
880
881 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
882
883 wdgetdefaultlabel(wd, lp);
884
885 wd->sc_badsect[0] = -1;
886
887 if (wd->drvp->state > RECAL)
888 wd->drvp->drive_flags |= DRIVE_RESET;
889 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
890 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
891 if (errstring) {
892 /*
893 * This probably happened because the drive's default
894 * geometry doesn't match the DOS geometry. We
895 * assume the DOS geometry is now in the label and try
896 * again. XXX This is a kluge.
897 */
898 if (wd->drvp->state > RECAL)
899 wd->drvp->drive_flags |= DRIVE_RESET;
900 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
901 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
902 }
903 if (errstring) {
904 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
905 return;
906 }
907
908 if (wd->drvp->state > RECAL)
909 wd->drvp->drive_flags |= DRIVE_RESET;
910 #ifdef HAS_BAD144_HANDLING
911 if ((lp->d_flags & D_BADSECT) != 0)
912 bad144intern(wd);
913 #endif
914 }
915
916 int
917 wdioctl(dev, xfer, addr, flag, p)
918 dev_t dev;
919 u_long xfer;
920 caddr_t addr;
921 int flag;
922 struct proc *p;
923 {
924 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
925 int error;
926 #ifdef __HAVE_OLD_DISKLABEL
927 struct disklabel newlabel;
928 #endif
929
930 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
931
932 if ((wd->sc_flags & WDF_LOADED) == 0)
933 return EIO;
934
935 switch (xfer) {
936 #ifdef HAS_BAD144_HANDLING
937 case DIOCSBAD:
938 if ((flag & FWRITE) == 0)
939 return EBADF;
940 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
941 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
942 bad144intern(wd);
943 return 0;
944 #endif
945
946 case DIOCGDINFO:
947 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
948 return 0;
949 #ifdef __HAVE_OLD_DISKLABEL
950 case ODIOCGDINFO:
951 newlabel = *(wd->sc_dk.dk_label);
952 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
953 newlabel.d_npartitions = OLDMAXPARTITIONS;
954 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
955 return 0;
956 #endif
957
958 case DIOCGPART:
959 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
960 ((struct partinfo *)addr)->part =
961 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
962 return 0;
963
964 case DIOCWDINFO:
965 case DIOCSDINFO:
966 #ifdef __HAVE_OLD_DISKLABEL
967 case ODIOCWDINFO:
968 case ODIOCSDINFO:
969 #endif
970 {
971 struct disklabel *lp;
972
973 #ifdef __HAVE_OLD_DISKLABEL
974 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
975 memset(&newlabel, 0, sizeof newlabel);
976 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
977 lp = &newlabel;
978 } else
979 #endif
980 lp = (struct disklabel *)addr;
981
982 if ((flag & FWRITE) == 0)
983 return EBADF;
984
985 if ((error = wdlock(wd)) != 0)
986 return error;
987 wd->sc_flags |= WDF_LABELLING;
988
989 error = setdisklabel(wd->sc_dk.dk_label,
990 lp, /*wd->sc_dk.dk_openmask : */0,
991 wd->sc_dk.dk_cpulabel);
992 if (error == 0) {
993 if (wd->drvp->state > RECAL)
994 wd->drvp->drive_flags |= DRIVE_RESET;
995 if (xfer == DIOCWDINFO
996 #ifdef __HAVE_OLD_DISKLABEL
997 || xfer == ODIOCWDINFO
998 #endif
999 )
1000 error = writedisklabel(WDLABELDEV(dev),
1001 wdstrategy, wd->sc_dk.dk_label,
1002 wd->sc_dk.dk_cpulabel);
1003 }
1004
1005 wd->sc_flags &= ~WDF_LABELLING;
1006 wdunlock(wd);
1007 return error;
1008 }
1009
1010 case DIOCKLABEL:
1011 if (*(int *)addr)
1012 wd->sc_flags |= WDF_KLABEL;
1013 else
1014 wd->sc_flags &= ~WDF_KLABEL;
1015 return 0;
1016
1017 case DIOCWLABEL:
1018 if ((flag & FWRITE) == 0)
1019 return EBADF;
1020 if (*(int *)addr)
1021 wd->sc_flags |= WDF_WLABEL;
1022 else
1023 wd->sc_flags &= ~WDF_WLABEL;
1024 return 0;
1025
1026 case DIOCGDEFLABEL:
1027 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1028 return 0;
1029 #ifdef __HAVE_OLD_DISKLABEL
1030 case ODIOCGDEFLABEL:
1031 wdgetdefaultlabel(wd, &newlabel);
1032 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1033 newlabel.d_npartitions = OLDMAXPARTITIONS;
1034 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1035 return 0;
1036 #endif
1037
1038 #ifdef notyet
1039 case DIOCWFORMAT:
1040 if ((flag & FWRITE) == 0)
1041 return EBADF;
1042 {
1043 register struct format_op *fop;
1044 struct iovec aiov;
1045 struct uio auio;
1046
1047 fop = (struct format_op *)addr;
1048 aiov.iov_base = fop->df_buf;
1049 aiov.iov_len = fop->df_count;
1050 auio.uio_iov = &aiov;
1051 auio.uio_iovcnt = 1;
1052 auio.uio_resid = fop->df_count;
1053 auio.uio_segflg = 0;
1054 auio.uio_offset =
1055 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1056 auio.uio_procp = p;
1057 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1058 &auio);
1059 fop->df_count -= auio.uio_resid;
1060 fop->df_reg[0] = wdc->sc_status;
1061 fop->df_reg[1] = wdc->sc_error;
1062 return error;
1063 }
1064 #endif
1065
1066 case ATAIOCCOMMAND:
1067 /*
1068 * Make sure this command is (relatively) safe first
1069 */
1070 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1071 (flag & FWRITE) == 0)
1072 return (EBADF);
1073 {
1074 struct wd_ioctl *wi;
1075 atareq_t *atareq = (atareq_t *) addr;
1076 int error;
1077
1078 wi = wi_get();
1079 wi->wi_softc = wd;
1080 wi->wi_atareq = *atareq;
1081
1082 if (atareq->datalen && atareq->flags &
1083 (ATACMD_READ | ATACMD_WRITE)) {
1084 wi->wi_iov.iov_base = atareq->databuf;
1085 wi->wi_iov.iov_len = atareq->datalen;
1086 wi->wi_uio.uio_iov = &wi->wi_iov;
1087 wi->wi_uio.uio_iovcnt = 1;
1088 wi->wi_uio.uio_resid = atareq->datalen;
1089 wi->wi_uio.uio_offset = 0;
1090 wi->wi_uio.uio_segflg = UIO_USERSPACE;
1091 wi->wi_uio.uio_rw =
1092 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1093 wi->wi_uio.uio_procp = p;
1094 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
1095 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1096 minphys, &wi->wi_uio);
1097 } else {
1098 /* No need to call physio if we don't have any
1099 user data */
1100 wi->wi_bp.b_flags = 0;
1101 wi->wi_bp.b_data = 0;
1102 wi->wi_bp.b_bcount = 0;
1103 wi->wi_bp.b_dev = 0;
1104 wi->wi_bp.b_proc = p;
1105 wdioctlstrategy(&wi->wi_bp);
1106 error = wi->wi_bp.b_error;
1107 }
1108 *atareq = wi->wi_atareq;
1109 wi_free(wi);
1110 return(error);
1111 }
1112
1113 default:
1114 return ENOTTY;
1115 }
1116
1117 #ifdef DIAGNOSTIC
1118 panic("wdioctl: impossible");
1119 #endif
1120 }
1121
1122 #ifdef B_FORMAT
1123 int
1124 wdformat(struct buf *bp)
1125 {
1126
1127 bp->b_flags |= B_FORMAT;
1128 return wdstrategy(bp);
1129 }
1130 #endif
1131
1132 int
1133 wdsize(dev)
1134 dev_t dev;
1135 {
1136 struct wd_softc *wd;
1137 int part, omask;
1138 int size;
1139
1140 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1141
1142 wd = device_lookup(&wd_cd, WDUNIT(dev));
1143 if (wd == NULL)
1144 return (-1);
1145
1146 part = WDPART(dev);
1147 omask = wd->sc_dk.dk_openmask & (1 << part);
1148
1149 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1150 return (-1);
1151 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1152 size = -1;
1153 else
1154 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1155 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1156 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1157 return (-1);
1158 return (size);
1159 }
1160
1161 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1162 static int wddoingadump = 0;
1163 static int wddumprecalibrated = 0;
1164 static int wddumpmulti = 1;
1165
1166 /*
1167 * Dump core after a system crash.
1168 */
1169 int
1170 wddump(dev, blkno, va, size)
1171 dev_t dev;
1172 daddr_t blkno;
1173 caddr_t va;
1174 size_t size;
1175 {
1176 struct wd_softc *wd; /* disk unit to do the I/O */
1177 struct disklabel *lp; /* disk's disklabel */
1178 int part, err;
1179 int nblks; /* total number of sectors left to write */
1180 char errbuf[256];
1181
1182 /* Check if recursive dump; if so, punt. */
1183 if (wddoingadump)
1184 return EFAULT;
1185 wddoingadump = 1;
1186
1187 wd = device_lookup(&wd_cd, WDUNIT(dev));
1188 if (wd == NULL)
1189 return (ENXIO);
1190
1191 part = WDPART(dev);
1192
1193 /* Make sure it was initialized. */
1194 if (wd->drvp->state < READY)
1195 return ENXIO;
1196
1197 /* Convert to disk sectors. Request must be a multiple of size. */
1198 lp = wd->sc_dk.dk_label;
1199 if ((size % lp->d_secsize) != 0)
1200 return EFAULT;
1201 nblks = size / lp->d_secsize;
1202 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1203
1204 /* Check transfer bounds against partition size. */
1205 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1206 return EINVAL;
1207
1208 /* Offset block number to start of partition. */
1209 blkno += lp->d_partitions[part].p_offset;
1210
1211 /* Recalibrate, if first dump transfer. */
1212 if (wddumprecalibrated == 0) {
1213 wddumpmulti = wd->sc_multi;
1214 wddumprecalibrated = 1;
1215 wd->drvp->state = RESET;
1216 }
1217
1218 while (nblks > 0) {
1219 again:
1220 wd->sc_wdc_bio.blkno = blkno;
1221 wd->sc_wdc_bio.flags = ATA_POLL;
1222 if (wddumpmulti == 1)
1223 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1224 if (wd->sc_flags & WDF_LBA)
1225 wd->sc_wdc_bio.flags |= ATA_LBA;
1226 wd->sc_wdc_bio.bcount =
1227 min(nblks, wddumpmulti) * lp->d_secsize;
1228 wd->sc_wdc_bio.databuf = va;
1229 #ifndef WD_DUMP_NOT_TRUSTED
1230 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1231 case WDC_TRY_AGAIN:
1232 panic("wddump: try again");
1233 break;
1234 case WDC_QUEUED:
1235 panic("wddump: polled command has been queued");
1236 break;
1237 case WDC_COMPLETE:
1238 break;
1239 }
1240 switch(wd->sc_wdc_bio.error) {
1241 case TIMEOUT:
1242 printf("wddump: device timed out");
1243 err = EIO;
1244 break;
1245 case ERR_DF:
1246 printf("wddump: drive fault");
1247 err = EIO;
1248 break;
1249 case ERR_DMA:
1250 printf("wddump: DMA error");
1251 err = EIO;
1252 break;
1253 case ERROR:
1254 errbuf[0] = '\0';
1255 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
1256 printf("wddump: %s", errbuf);
1257 err = EIO;
1258 break;
1259 case NOERROR:
1260 err = 0;
1261 break;
1262 default:
1263 panic("wddump: unknown error type");
1264 }
1265 if (err != 0) {
1266 if (wddumpmulti != 1) {
1267 wddumpmulti = 1; /* retry in single-sector */
1268 printf(", retrying\n");
1269 goto again;
1270 }
1271 printf("\n");
1272 return err;
1273 }
1274 #else /* WD_DUMP_NOT_TRUSTED */
1275 /* Let's just talk about this first... */
1276 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1277 unit, va, cylin, head, sector);
1278 delay(500 * 1000); /* half a second */
1279 #endif
1280
1281 /* update block count */
1282 nblks -= min(nblks, wddumpmulti);
1283 blkno += min(nblks, wddumpmulti);
1284 va += min(nblks, wddumpmulti) * lp->d_secsize;
1285 }
1286
1287 wddoingadump = 0;
1288 return 0;
1289 }
1290
1291 #ifdef HAS_BAD144_HANDLING
1292 /*
1293 * Internalize the bad sector table.
1294 */
1295 void
1296 bad144intern(wd)
1297 struct wd_softc *wd;
1298 {
1299 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1300 struct disklabel *lp = wd->sc_dk.dk_label;
1301 int i = 0;
1302
1303 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1304
1305 for (; i < NBT_BAD; i++) {
1306 if (bt->bt_bad[i].bt_cyl == 0xffff)
1307 break;
1308 wd->sc_badsect[i] =
1309 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1310 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1311 (bt->bt_bad[i].bt_trksec & 0xff);
1312 }
1313 for (; i < NBT_BAD+1; i++)
1314 wd->sc_badsect[i] = -1;
1315 }
1316 #endif
1317
1318 int
1319 wd_get_params(wd, flags, params)
1320 struct wd_softc *wd;
1321 u_int8_t flags;
1322 struct ataparams *params;
1323 {
1324 switch (ata_get_params(wd->drvp, flags, params)) {
1325 case CMD_AGAIN:
1326 return 1;
1327 case CMD_ERR:
1328 /*
1329 * We `know' there's a drive here; just assume it's old.
1330 * This geometry is only used to read the MBR and print a
1331 * (false) attach message.
1332 */
1333 strncpy(params->atap_model, "ST506",
1334 sizeof params->atap_model);
1335 params->atap_config = ATA_CFG_FIXED;
1336 params->atap_cylinders = 1024;
1337 params->atap_heads = 8;
1338 params->atap_sectors = 17;
1339 params->atap_multi = 1;
1340 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1341 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1342 return 0;
1343 case CMD_OK:
1344 return 0;
1345 default:
1346 panic("wd_get_params: bad return code from ata_get_params");
1347 /* NOTREACHED */
1348 }
1349 }
1350
1351 void
1352 wd_flushcache(wd, flags)
1353 struct wd_softc *wd;
1354 int flags;
1355 {
1356 struct wdc_command wdc_c;
1357
1358 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1359 return;
1360 memset(&wdc_c, 0, sizeof(struct wdc_command));
1361 wdc_c.r_command = WDCC_FLUSHCACHE;
1362 wdc_c.r_st_bmask = WDCS_DRDY;
1363 wdc_c.r_st_pmask = WDCS_DRDY;
1364 wdc_c.flags = flags;
1365 wdc_c.timeout = 30000; /* 30s timeout */
1366 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1367 printf("%s: flush cache command didn't complete\n",
1368 wd->sc_dev.dv_xname);
1369 }
1370 if (wdc_c.flags & AT_TIMEOU) {
1371 printf("%s: flush cache command timeout\n",
1372 wd->sc_dev.dv_xname);
1373 }
1374 if (wdc_c.flags & AT_DF) {
1375 printf("%s: flush cache command: drive fault\n",
1376 wd->sc_dev.dv_xname);
1377 }
1378 /*
1379 * Ignore error register, it shouldn't report anything else
1380 * than COMMAND ABORTED, which means the device doesn't support
1381 * flush cache
1382 */
1383 }
1384
1385 void
1386 wd_shutdown(arg)
1387 void *arg;
1388 {
1389 struct wd_softc *wd = arg;
1390 wd_flushcache(wd, AT_POLL);
1391 }
1392
1393 /*
1394 * Allocate space for a ioctl queue structure. Mostly taken from
1395 * scsipi_ioctl.c
1396 */
1397 struct wd_ioctl *
1398 wi_get()
1399 {
1400 struct wd_ioctl *wi;
1401 int s;
1402
1403 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK);
1404 memset(wi, 0, sizeof (struct wd_ioctl));
1405 s = splbio();
1406 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1407 splx(s);
1408 return (wi);
1409 }
1410
1411 /*
1412 * Free an ioctl structure and remove it from our list
1413 */
1414
1415 void
1416 wi_free(wi)
1417 struct wd_ioctl *wi;
1418 {
1419 int s;
1420
1421 s = splbio();
1422 LIST_REMOVE(wi, wi_list);
1423 splx(s);
1424 free(wi, M_TEMP);
1425 }
1426
1427 /*
1428 * Find a wd_ioctl structure based on the struct buf.
1429 */
1430
1431 struct wd_ioctl *
1432 wi_find(bp)
1433 struct buf *bp;
1434 {
1435 struct wd_ioctl *wi;
1436 int s;
1437
1438 s = splbio();
1439 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1440 if (bp == &wi->wi_bp)
1441 break;
1442 splx(s);
1443 return (wi);
1444 }
1445
1446 /*
1447 * Ioctl pseudo strategy routine
1448 *
1449 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1450 * happens here is:
1451 *
1452 * - wdioctl() queues a wd_ioctl structure.
1453 *
1454 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1455 * user space I/O is required. If physio() is called, physio() eventually
1456 * calls wdioctlstrategy().
1457 *
1458 * - In either case, wdioctlstrategy() calls wdc_exec_command()
1459 * to perform the actual command
1460 *
1461 * The reason for the use of the pseudo strategy routine is because
1462 * when doing I/O to/from user space, physio _really_ wants to be in
1463 * the loop. We could put the entire buffer into the ioctl request
1464 * structure, but that won't scale if we want to do things like download
1465 * microcode.
1466 */
1467
1468 void
1469 wdioctlstrategy(bp)
1470 struct buf *bp;
1471 {
1472 struct wd_ioctl *wi;
1473 struct wdc_command wdc_c;
1474 int error = 0;
1475
1476 wi = wi_find(bp);
1477 if (wi == NULL) {
1478 printf("user_strat: No ioctl\n");
1479 error = EINVAL;
1480 goto bad;
1481 }
1482
1483 memset(&wdc_c, 0, sizeof(wdc_c));
1484
1485 /*
1486 * Abort if physio broke up the transfer
1487 */
1488
1489 if (bp->b_bcount != wi->wi_atareq.datalen) {
1490 printf("physio split wd ioctl request... cannot proceed\n");
1491 error = EIO;
1492 goto bad;
1493 }
1494
1495 /*
1496 * Abort if we didn't get a buffer size that was a multiple of
1497 * our sector size (or was larger than NBBY)
1498 */
1499
1500 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1501 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1502 (1 << NBBY)) {
1503 error = EINVAL;
1504 goto bad;
1505 }
1506
1507 /*
1508 * Make sure a timeout was supplied in the ioctl request
1509 */
1510
1511 if (wi->wi_atareq.timeout == 0) {
1512 error = EINVAL;
1513 goto bad;
1514 }
1515
1516 if (wi->wi_atareq.flags & ATACMD_READ)
1517 wdc_c.flags |= AT_READ;
1518 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1519 wdc_c.flags |= AT_WRITE;
1520
1521 if (wi->wi_atareq.flags & ATACMD_READREG)
1522 wdc_c.flags |= AT_READREG;
1523
1524 wdc_c.flags |= AT_WAIT;
1525
1526 wdc_c.timeout = wi->wi_atareq.timeout;
1527 wdc_c.r_command = wi->wi_atareq.command;
1528 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1529 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1530 wdc_c.r_sector = wi->wi_atareq.sec_num;
1531 wdc_c.r_count = wi->wi_atareq.sec_count;
1532 wdc_c.r_precomp = wi->wi_atareq.features;
1533 wdc_c.r_st_bmask = WDCS_DRDY;
1534 wdc_c.r_st_pmask = WDCS_DRDY;
1535 wdc_c.data = wi->wi_bp.b_data;
1536 wdc_c.bcount = wi->wi_bp.b_bcount;
1537
1538 if (wdc_exec_command(wi->wi_softc->drvp, &wdc_c) != WDC_COMPLETE) {
1539 wi->wi_atareq.retsts = ATACMD_ERROR;
1540 goto bad;
1541 }
1542
1543 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1544 if (wdc_c.flags & AT_ERROR) {
1545 wi->wi_atareq.retsts = ATACMD_ERROR;
1546 wi->wi_atareq.error = wdc_c.r_error;
1547 } else if (wdc_c.flags & AT_DF)
1548 wi->wi_atareq.retsts = ATACMD_DF;
1549 else
1550 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1551 } else {
1552 wi->wi_atareq.retsts = ATACMD_OK;
1553 if (wi->wi_atareq.flags & ATACMD_READREG) {
1554 wi->wi_atareq.head = wdc_c.r_head ;
1555 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1556 wi->wi_atareq.sec_num = wdc_c.r_sector;
1557 wi->wi_atareq.sec_count = wdc_c.r_count;
1558 wi->wi_atareq.features = wdc_c.r_precomp;
1559 wi->wi_atareq.error = wdc_c.r_error;
1560 }
1561 }
1562
1563 bp->b_error = 0;
1564 biodone(bp);
1565 return;
1566 bad:
1567 bp->b_flags |= B_ERROR;
1568 bp->b_error = error;
1569 biodone(bp);
1570 }
1571