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