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