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