wd.c revision 1.274.2.13 1 /* $NetBSD: wd.c,v 1.274.2.13 2006/11/20 21:47:05 tron 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, 2003, 2004 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.274.2.13 2006/11/20 21:47:05 tron 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 LBA48_THRESHOLD (0xfffffff) /* 128GB / DEV_BSIZE */
108
109 #define WDIORETRIES_SINGLE 4 /* number of retries before single-sector */
110 #define WDIORETRIES 5 /* number of retries before giving up */
111 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
112
113 #define WDUNIT(dev) DISKUNIT(dev)
114 #define WDPART(dev) DISKPART(dev)
115 #define WDMINOR(unit, part) DISKMINOR(unit, part)
116 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
117
118 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
119
120 #define DEBUG_INTR 0x01
121 #define DEBUG_XFERS 0x02
122 #define DEBUG_STATUS 0x04
123 #define DEBUG_FUNCS 0x08
124 #define DEBUG_PROBE 0x10
125 #ifdef WDCDEBUG
126 int wdcdebug_wd_mask = 0x0;
127 #define WDCDEBUG_PRINT(args, level) \
128 if (wdcdebug_wd_mask & (level)) \
129 printf args
130 #else
131 #define WDCDEBUG_PRINT(args, level)
132 #endif
133
134 int wdprobe(struct device *, struct cfdata *, void *);
135 void wdattach(struct device *, struct device *, void *);
136 int wddetach(struct device *, int);
137 int wdactivate(struct device *, enum devact);
138 int wdprint(void *, char *);
139 void wdperror(const struct wd_softc *);
140
141 CFATTACH_DECL(wd, sizeof(struct wd_softc),
142 wdprobe, wdattach, wddetach, wdactivate);
143
144 extern struct cfdriver wd_cd;
145
146 dev_type_open(wdopen);
147 dev_type_close(wdclose);
148 dev_type_read(wdread);
149 dev_type_write(wdwrite);
150 dev_type_ioctl(wdioctl);
151 dev_type_strategy(wdstrategy);
152 dev_type_dump(wddump);
153 dev_type_size(wdsize);
154
155 const struct bdevsw wd_bdevsw = {
156 wdopen, wdclose, wdstrategy, wdioctl, wddump, wdsize, D_DISK
157 };
158
159 const struct cdevsw wd_cdevsw = {
160 wdopen, wdclose, wdread, wdwrite, wdioctl,
161 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
162 };
163
164 /*
165 * Glue necessary to hook WDCIOCCOMMAND into physio
166 */
167
168 struct wd_ioctl {
169 LIST_ENTRY(wd_ioctl) wi_list;
170 struct buf wi_bp;
171 struct uio wi_uio;
172 struct iovec wi_iov;
173 atareq_t wi_atareq;
174 struct wd_softc *wi_softc;
175 };
176
177 LIST_HEAD(, wd_ioctl) wi_head;
178
179 struct wd_ioctl *wi_find(struct buf *);
180 void wi_free(struct wd_ioctl *);
181 struct wd_ioctl *wi_get(void);
182 void wdioctlstrategy(struct buf *);
183
184 void wdgetdefaultlabel(struct wd_softc *, struct disklabel *);
185 void wdgetdisklabel(struct wd_softc *);
186 void wdstart(void *);
187 void __wdstart(struct wd_softc*, struct buf *);
188 void wdrestart(void *);
189 void wddone(void *);
190 int wd_get_params(struct wd_softc *, u_int8_t, struct ataparams *);
191 int wd_flushcache(struct wd_softc *, int);
192 void wd_shutdown(void *);
193
194 int wd_getcache(struct wd_softc *, int *);
195 int wd_setcache(struct wd_softc *, int);
196
197 struct dkdriver wddkdriver = { wdstrategy };
198
199 #ifdef HAS_BAD144_HANDLING
200 static void bad144intern(struct wd_softc *);
201 #endif
202
203 #define WD_QUIRK_SPLIT_MOD15_WRITE 0x0001 /* must split certain writes */
204 #define WD_QUIRK_FORCE_LBA48 0x0002 /* must use LBA48 commands */
205
206 /*
207 * Quirk table for IDE drives. Put more-specific matches first, since
208 * a simple globbing routine is used for matching.
209 */
210 static const struct wd_quirk {
211 const char *wdq_match; /* inquiry pattern to match */
212 int wdq_quirks; /* drive quirks */
213 } wd_quirk_table[] = {
214 /*
215 * Some Seagate S-ATA drives have a PHY which can get confused
216 * with the way data is packetized by some S-ATA controllers.
217 *
218 * The work-around is to split in two any write transfer whose
219 * sector count % 15 == 1 (assuming 512 byte sectors).
220 *
221 * XXX This is an incomplete list. There are at least a couple
222 * XXX more model numbers. If you have trouble with such transfers
223 * XXX (8K is the most common) on Seagate S-ATA drives, please
224 * XXX notify thorpej (at) NetBSD.org.
225 */
226 { "ST3120023AS",
227 WD_QUIRK_SPLIT_MOD15_WRITE },
228 { "ST380023AS",
229 WD_QUIRK_SPLIT_MOD15_WRITE },
230
231 /*
232 * This seagate drive seems to have issue addressing sector 0xfffffff
233 * (aka LBA48_THRESHOLD) in LBA mode. The workaround is to force
234 * LBA48
235 */
236 { "ST3160021A*",
237 WD_QUIRK_FORCE_LBA48 },
238 { "ST3160811A*",
239 WD_QUIRK_FORCE_LBA48 },
240 { "ST3160812A*",
241 WD_QUIRK_FORCE_LBA48 },
242 { "ST3160023A*",
243 WD_QUIRK_FORCE_LBA48 },
244 { "ST3160827A*",
245 WD_QUIRK_FORCE_LBA48 },
246 /* Attempt to catch all seagate drives larger than 200GB */
247 { "ST3[2-9][0-9][0-9][0-9][0-9][0-9][A-Z]*",
248 WD_QUIRK_FORCE_LBA48 },
249 { NULL,
250 0 }
251 };
252
253 static const struct wd_quirk *
254 wd_lookup_quirks(const char *name)
255 {
256 const struct wd_quirk *wdq;
257 const char *estr;
258
259 for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) {
260 /*
261 * We only want exact matches (which include matches
262 * against globbing characters).
263 */
264 if (pmatch(name, wdq->wdq_match, &estr) == 2)
265 return (wdq);
266 }
267 return (NULL);
268 }
269
270 int
271 wdprobe(struct device *parent, struct cfdata *match, void *aux)
272 {
273 struct ata_device *adev = aux;
274
275 if (adev == NULL)
276 return 0;
277 if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA)
278 return 0;
279
280 if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT &&
281 match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive)
282 return 0;
283 return 1;
284 }
285
286 void
287 wdattach(struct device *parent, struct device *self, void *aux)
288 {
289 struct wd_softc *wd = (void *)self;
290 struct ata_device *adev= aux;
291 int i, blank;
292 char buf[41], pbuf[9], c, *p, *q;
293 const struct wd_quirk *wdq;
294 WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
295
296 lockinit(&wd->sc_lock, PRIBIO | PCATCH, "wdlock", 0, 0);
297
298 callout_init(&wd->sc_restart_ch);
299 bufq_alloc(&wd->sc_q, BUFQ_DISK_DEFAULT_STRAT()|BUFQ_SORT_RAWBLOCK);
300 SLIST_INIT(&wd->sc_bslist);
301
302 wd->atabus = adev->adev_bustype;
303 wd->openings = adev->adev_openings;
304 wd->drvp = adev->adev_drv_data;
305
306 wd->drvp->drv_done = wddone;
307 wd->drvp->drv_softc = &wd->sc_dev;
308
309 aprint_naive("\n");
310
311 /* read our drive info */
312 if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) {
313 aprint_error("\n%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
314 return;
315 }
316
317 for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
318 i < sizeof(wd->sc_params.atap_model); i++) {
319 c = *p++;
320 if (c == '\0')
321 break;
322 if (c != ' ') {
323 if (blank) {
324 *q++ = ' ';
325 blank = 0;
326 }
327 *q++ = c;
328 } else
329 blank = 1;
330 }
331 *q++ = '\0';
332
333 aprint_normal(": <%s>\n", buf);
334
335 wdq = wd_lookup_quirks(buf);
336 if (wdq != NULL)
337 wd->sc_quirks = wdq->wdq_quirks;
338
339 if ((wd->sc_params.atap_multi & 0xff) > 1) {
340 wd->sc_multi = wd->sc_params.atap_multi & 0xff;
341 } else {
342 wd->sc_multi = 1;
343 }
344
345 aprint_normal("%s: drive supports %d-sector PIO transfers,",
346 wd->sc_dev.dv_xname, wd->sc_multi);
347
348 /* 48-bit LBA addressing */
349 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0)
350 wd->sc_flags |= WDF_LBA48;
351
352 /* Prior to ATA-4, LBA was optional. */
353 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
354 wd->sc_flags |= WDF_LBA;
355 #if 0
356 /* ATA-4 requires LBA. */
357 if (wd->sc_params.atap_ataversion != 0xffff &&
358 wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
359 wd->sc_flags |= WDF_LBA;
360 #endif
361
362 if ((wd->sc_flags & WDF_LBA48) != 0) {
363 aprint_normal(" LBA48 addressing\n");
364 wd->sc_capacity =
365 ((u_int64_t) wd->sc_params.__reserved6[11] << 48) |
366 ((u_int64_t) wd->sc_params.__reserved6[10] << 32) |
367 ((u_int64_t) wd->sc_params.__reserved6[9] << 16) |
368 ((u_int64_t) wd->sc_params.__reserved6[8] << 0);
369 } else if ((wd->sc_flags & WDF_LBA) != 0) {
370 aprint_normal(" LBA addressing\n");
371 wd->sc_capacity =
372 (wd->sc_params.atap_capacity[1] << 16) |
373 wd->sc_params.atap_capacity[0];
374 } else {
375 aprint_normal(" chs addressing\n");
376 wd->sc_capacity =
377 wd->sc_params.atap_cylinders *
378 wd->sc_params.atap_heads *
379 wd->sc_params.atap_sectors;
380 }
381 format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * DEV_BSIZE);
382 aprint_normal("%s: %s, %d cyl, %d head, %d sec, "
383 "%d bytes/sect x %llu sectors\n",
384 self->dv_xname, pbuf,
385 (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity /
386 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) :
387 wd->sc_params.atap_cylinders,
388 wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
389 DEV_BSIZE, (unsigned long long)wd->sc_capacity);
390
391 WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
392 self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
393 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
394 /*
395 * Initialize and attach the disk structure.
396 */
397 wd->sc_dk.dk_driver = &wddkdriver;
398 wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
399 disk_attach(&wd->sc_dk);
400 wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
401 wd->sc_sdhook = shutdownhook_establish(wd_shutdown, wd);
402 if (wd->sc_sdhook == NULL)
403 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
404 wd->sc_dev.dv_xname);
405 #if NRND > 0
406 rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname,
407 RND_TYPE_DISK, 0);
408 #endif
409 }
410
411 int
412 wdactivate(struct device *self, enum devact act)
413 {
414 int rv = 0;
415
416 switch (act) {
417 case DVACT_ACTIVATE:
418 rv = EOPNOTSUPP;
419 break;
420
421 case DVACT_DEACTIVATE:
422 /*
423 * Nothing to do; we key off the device's DVF_ACTIVATE.
424 */
425 break;
426 }
427 return (rv);
428 }
429
430 int
431 wddetach(struct device *self, int flags)
432 {
433 struct wd_softc *sc = (struct wd_softc *)self;
434 struct buf *bp;
435 int s, bmaj, cmaj, i, mn;
436
437 /* locate the major number */
438 bmaj = bdevsw_lookup_major(&wd_bdevsw);
439 cmaj = cdevsw_lookup_major(&wd_cdevsw);
440
441 s = splbio();
442
443 /* Kill off any queued buffers. */
444 while ((bp = BUFQ_GET(&sc->sc_q)) != NULL) {
445 bp->b_error = EIO;
446 bp->b_flags |= B_ERROR;
447 bp->b_resid = bp->b_bcount;
448 biodone(bp);
449 }
450
451 bufq_free(&sc->sc_q);
452
453 splx(s);
454
455 /* Nuke the vnodes for any open instances. */
456 for (i = 0; i < MAXPARTITIONS; i++) {
457 mn = WDMINOR(self->dv_unit, i);
458 vdevgone(bmaj, mn, mn, VBLK);
459 vdevgone(cmaj, mn, mn, VCHR);
460 }
461
462 /* Detach disk. */
463 disk_detach(&sc->sc_dk);
464
465 /* Clean out the bad sector list */
466 while (!SLIST_EMPTY(&sc->sc_bslist)) {
467 void *head = SLIST_FIRST(&sc->sc_bslist);
468 SLIST_REMOVE_HEAD(&sc->sc_bslist, dbs_next);
469 free(head, M_TEMP);
470 }
471 sc->sc_bscount = 0;
472
473 /* Get rid of the shutdown hook. */
474 if (sc->sc_sdhook != NULL)
475 shutdownhook_disestablish(sc->sc_sdhook);
476
477 #if NRND > 0
478 /* Unhook the entropy source. */
479 rnd_detach_source(&sc->rnd_source);
480 #endif
481
482 lockmgr(&sc->sc_lock, LK_DRAIN, NULL);
483
484 return (0);
485 }
486
487 /*
488 * Read/write routine for a buffer. Validates the arguments and schedules the
489 * transfer. Does not wait for the transfer to complete.
490 */
491 void
492 wdstrategy(struct buf *bp)
493 {
494 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
495 struct disklabel *lp = wd->sc_dk.dk_label;
496 daddr_t blkno;
497 int s;
498
499 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
500 DEBUG_XFERS);
501
502 /* Valid request? */
503 if (bp->b_blkno < 0 ||
504 (bp->b_bcount % lp->d_secsize) != 0 ||
505 (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
506 bp->b_error = EINVAL;
507 goto bad;
508 }
509
510 /* If device invalidated (e.g. media change, door open), error. */
511 if ((wd->sc_flags & WDF_LOADED) == 0) {
512 bp->b_error = EIO;
513 goto bad;
514 }
515
516 /* If it's a null transfer, return immediately. */
517 if (bp->b_bcount == 0)
518 goto done;
519
520 /*
521 * Do bounds checking, adjust transfer. if error, process.
522 * If end of partition, just return.
523 */
524 if (WDPART(bp->b_dev) == RAW_PART) {
525 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
526 wd->sc_capacity) <= 0)
527 goto done;
528 } else {
529 if (bounds_check_with_label(&wd->sc_dk, bp,
530 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
531 goto done;
532 }
533
534 /*
535 * Now convert the block number to absolute and put it in
536 * terms of the device's logical block size.
537 */
538 if (lp->d_secsize >= DEV_BSIZE)
539 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
540 else
541 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
542
543 if (WDPART(bp->b_dev) != RAW_PART)
544 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
545
546 bp->b_rawblkno = blkno;
547
548 /*
549 * If the transfer about to be attempted contains only a block that
550 * is known to be bad then return an error for the transfer without
551 * even attempting to start a transfer up under the premis that we
552 * will just end up doing more retries for a transfer that will end
553 * up failing again.
554 * XXX:SMP - mutex required to protect with DIOCBSFLUSH
555 */
556 if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) {
557 struct disk_badsectors *dbs;
558 daddr_t maxblk = blkno + (bp->b_bcount >> DEV_BSHIFT) - 1;
559
560 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next)
561 if ((dbs->dbs_min <= blkno && blkno <= dbs->dbs_max) ||
562 (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){
563 bp->b_error = EIO;
564 goto bad;
565 }
566 }
567
568 /* Queue transfer on drive, activate drive and controller if idle. */
569 s = splbio();
570 BUFQ_PUT(&wd->sc_q, bp);
571 wdstart(wd);
572 splx(s);
573 return;
574 bad:
575 bp->b_flags |= B_ERROR;
576 done:
577 /* Toss transfer; we're done early. */
578 bp->b_resid = bp->b_bcount;
579 biodone(bp);
580 }
581
582 /*
583 * Queue a drive for I/O.
584 */
585 void
586 wdstart(void *arg)
587 {
588 struct wd_softc *wd = arg;
589 struct buf *bp = NULL;
590
591 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
592 DEBUG_XFERS);
593 while (wd->openings > 0) {
594
595 /* Is there a buf for us ? */
596 if ((bp = BUFQ_GET(&wd->sc_q)) == NULL)
597 return;
598
599 /*
600 * Make the command. First lock the device
601 */
602 wd->openings--;
603
604 wd->retries = 0;
605 __wdstart(wd, bp);
606 }
607 }
608
609 static void
610 wd_split_mod15_write(struct buf *bp)
611 {
612 struct buf *obp = bp->b_private;
613 struct wd_softc *sc = wd_cd.cd_devs[DISKUNIT(obp->b_dev)];
614
615 if (__predict_false(bp->b_flags & B_ERROR) != 0) {
616 /*
617 * Propagate the error. If this was the first half of
618 * the original transfer, make sure to account for that
619 * in the residual.
620 */
621 if (bp->b_data == obp->b_data)
622 bp->b_resid += bp->b_bcount;
623 goto done;
624 }
625
626 /*
627 * If this was the second half of the transfer, we're all done!
628 */
629 if (bp->b_data != obp->b_data)
630 goto done;
631
632 /*
633 * Advance the pointer to the second half and issue that command
634 * using the same opening.
635 */
636 bp->b_flags = obp->b_flags | B_CALL;
637 bp->b_data += bp->b_bcount;
638 bp->b_blkno += (bp->b_bcount / 512);
639 bp->b_rawblkno += (bp->b_bcount / 512);
640 __wdstart(sc, bp);
641 return;
642
643 done:
644 obp->b_flags |= (bp->b_flags & (B_EINTR|B_ERROR));
645 obp->b_error = bp->b_error;
646 obp->b_resid = bp->b_resid;
647 pool_put(&bufpool, bp);
648 biodone(obp);
649 sc->openings++;
650 /* wddone() will call wdstart() */
651 }
652
653 void
654 __wdstart(struct wd_softc *wd, struct buf *bp)
655 {
656
657 /*
658 * Deal with the "split mod15 write" quirk. We just divide the
659 * transfer in two, doing the first half and then then second half
660 * with the same command opening.
661 *
662 * Note we MUST do this here, because we can't let insertion
663 * into the bufq cause the transfers to be re-merged.
664 */
665 if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 &&
666 (bp->b_flags & B_READ) == 0 &&
667 bp->b_bcount > 512 &&
668 ((bp->b_bcount / 512) % 15) == 1)) {
669 struct buf *nbp;
670
671 /* already at splbio */
672 nbp = pool_get(&bufpool, PR_NOWAIT);
673 if (__predict_false(nbp == NULL)) {
674 /* No memory -- fail the iop. */
675 bp->b_error = ENOMEM;
676 bp->b_flags |= B_ERROR;
677 bp->b_resid = bp->b_bcount;
678 biodone(bp);
679 wd->openings++;
680 return;
681 }
682
683 BUF_INIT(nbp);
684 nbp->b_error = 0;
685 nbp->b_proc = bp->b_proc;
686 nbp->b_vp = NULLVP;
687 nbp->b_dev = bp->b_dev;
688
689 nbp->b_bcount = bp->b_bcount / 2;
690 nbp->b_bufsize = bp->b_bcount / 2;
691 nbp->b_data = bp->b_data;
692
693 nbp->b_blkno = bp->b_blkno;
694 nbp->b_rawblkno = bp->b_rawblkno;
695
696 nbp->b_flags = bp->b_flags | B_CALL;
697 nbp->b_iodone = wd_split_mod15_write;
698
699 /* Put ptr to orig buf in b_private and use new buf */
700 nbp->b_private = bp;
701
702 BIO_COPYPRIO(nbp, bp);
703
704 bp = nbp;
705 }
706
707 wd->sc_wdc_bio.blkno = bp->b_rawblkno;
708 wd->sc_wdc_bio.blkdone =0;
709 wd->sc_bp = bp;
710 /*
711 * If we're retrying, retry in single-sector mode. This will give us
712 * the sector number of the problem, and will eventually allow the
713 * transfer to succeed.
714 */
715 if (wd->retries >= WDIORETRIES_SINGLE)
716 wd->sc_wdc_bio.flags = ATA_SINGLE;
717 else
718 wd->sc_wdc_bio.flags = 0;
719 if (wd->sc_flags & WDF_LBA48 &&
720 (wd->sc_wdc_bio.blkno > LBA48_THRESHOLD ||
721 (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
722 wd->sc_wdc_bio.flags |= ATA_LBA48;
723 if (wd->sc_flags & WDF_LBA)
724 wd->sc_wdc_bio.flags |= ATA_LBA;
725 if (bp->b_flags & B_READ)
726 wd->sc_wdc_bio.flags |= ATA_READ;
727 wd->sc_wdc_bio.bcount = bp->b_bcount;
728 wd->sc_wdc_bio.databuf = bp->b_data;
729 /* Instrumentation. */
730 disk_busy(&wd->sc_dk);
731 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
732 case WDC_TRY_AGAIN:
733 callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
734 break;
735 case WDC_QUEUED:
736 case WDC_COMPLETE:
737 break;
738 default:
739 panic("__wdstart: bad return code from ata_bio()");
740 }
741 }
742
743 void
744 wddone(void *v)
745 {
746 struct wd_softc *wd = v;
747 struct buf *bp = wd->sc_bp;
748 const char *errmsg;
749 int do_perror = 0;
750 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
751 DEBUG_XFERS);
752
753 if (bp == NULL)
754 return;
755 bp->b_resid = wd->sc_wdc_bio.bcount;
756 switch (wd->sc_wdc_bio.error) {
757 case ERR_DMA:
758 errmsg = "DMA error";
759 goto retry;
760 case ERR_DF:
761 errmsg = "device fault";
762 goto retry;
763 case TIMEOUT:
764 errmsg = "device timeout";
765 goto retry;
766 case ERR_RESET:
767 errmsg = "channel reset";
768 goto retry2;
769 case ERROR:
770 /* Don't care about media change bits */
771 if (wd->sc_wdc_bio.r_error != 0 &&
772 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
773 goto noerror;
774 errmsg = "error";
775 do_perror = 1;
776 retry: /* Just reset and retry. Can we do more ? */
777 wd->atabus->ata_reset_channel(wd->drvp, AT_RST_NOCMD);
778 retry2:
779 diskerr(bp, "wd", errmsg, LOG_PRINTF,
780 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
781 if (wd->retries < WDIORETRIES)
782 printf(", retrying\n");
783 if (do_perror)
784 wdperror(wd);
785 if (wd->retries < WDIORETRIES) {
786 wd->retries++;
787 callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
788 wdrestart, wd);
789 return;
790 }
791 printf("\n");
792
793 /*
794 * Not all errors indicate a failed block but those that do,
795 * put the block on the bad-block list for the device. Only
796 * do this for reads because the drive should do it for writes,
797 * itself, according to Manuel.
798 */
799 if ((bp->b_flags & B_READ) &&
800 ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) ||
801 (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) {
802 struct disk_badsectors *dbs;
803
804 dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK);
805 dbs->dbs_min = bp->b_rawblkno;
806 dbs->dbs_max = dbs->dbs_min + (bp->b_bcount >> DEV_BSHIFT) - 1;
807 microtime(&dbs->dbs_failedat);
808 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
809 wd->sc_bscount++;
810 }
811
812 bp->b_flags |= B_ERROR;
813 bp->b_error = EIO;
814 break;
815 case NOERROR:
816 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
817 printf("%s: soft error (corrected)\n",
818 wd->sc_dev.dv_xname);
819 break;
820 case ERR_NODEV:
821 bp->b_flags |= B_ERROR;
822 bp->b_error = EIO;
823 break;
824 }
825 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
826 (bp->b_flags & B_READ));
827 #if NRND > 0
828 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
829 #endif
830 /* XXX Yuck, but we don't want to increment openings in this case */
831 if (__predict_false((bp->b_flags & B_CALL) != 0 &&
832 bp->b_iodone == wd_split_mod15_write))
833 biodone(bp);
834 else {
835 biodone(bp);
836 wd->openings++;
837 }
838 wdstart(wd);
839 }
840
841 void
842 wdrestart(void *v)
843 {
844 struct wd_softc *wd = v;
845 struct buf *bp = wd->sc_bp;
846 int s;
847 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
848 DEBUG_XFERS);
849
850 s = splbio();
851 __wdstart(v, bp);
852 splx(s);
853 }
854
855 int
856 wdread(dev_t dev, struct uio *uio, int flags)
857 {
858
859 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
860 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
861 }
862
863 int
864 wdwrite(dev_t dev, struct uio *uio, int flags)
865 {
866
867 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
868 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
869 }
870
871 int
872 wdopen(dev_t dev, int flag, int fmt, struct proc *p)
873 {
874 struct wd_softc *wd;
875 int part, error;
876
877 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
878 wd = device_lookup(&wd_cd, WDUNIT(dev));
879 if (wd == NULL)
880 return (ENXIO);
881
882 if ((wd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
883 return (ENODEV);
884
885 /*
886 * If this is the first open of this device, add a reference
887 * to the adapter.
888 */
889 if (wd->sc_dk.dk_openmask == 0 &&
890 (error = wd->atabus->ata_addref(wd->drvp)) != 0)
891 return (error);
892
893 if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
894 goto bad4;
895
896 if (wd->sc_dk.dk_openmask != 0) {
897 /*
898 * If any partition is open, but the disk has been invalidated,
899 * disallow further opens.
900 */
901 if ((wd->sc_flags & WDF_LOADED) == 0) {
902 error = EIO;
903 goto bad3;
904 }
905 } else {
906 if ((wd->sc_flags & WDF_LOADED) == 0) {
907 wd->sc_flags |= WDF_LOADED;
908
909 /* Load the physical device parameters. */
910 wd_get_params(wd, AT_WAIT, &wd->sc_params);
911
912 /* Load the partition info if not already loaded. */
913 wdgetdisklabel(wd);
914 }
915 }
916
917 part = WDPART(dev);
918
919 /* Check that the partition exists. */
920 if (part != RAW_PART &&
921 (part >= wd->sc_dk.dk_label->d_npartitions ||
922 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
923 error = ENXIO;
924 goto bad;
925 }
926
927 /* Insure only one open at a time. */
928 switch (fmt) {
929 case S_IFCHR:
930 wd->sc_dk.dk_copenmask |= (1 << part);
931 break;
932 case S_IFBLK:
933 wd->sc_dk.dk_bopenmask |= (1 << part);
934 break;
935 }
936 wd->sc_dk.dk_openmask =
937 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
938
939 lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
940 return 0;
941
942 bad:
943 if (wd->sc_dk.dk_openmask == 0) {
944 }
945
946 bad3:
947 lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
948 bad4:
949 if (wd->sc_dk.dk_openmask == 0)
950 wd->atabus->ata_delref(wd->drvp);
951 return error;
952 }
953
954 int
955 wdclose(dev_t dev, int flag, int fmt, struct proc *p)
956 {
957 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
958 int part = WDPART(dev);
959 int error;
960
961 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
962 if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
963 return error;
964
965 switch (fmt) {
966 case S_IFCHR:
967 wd->sc_dk.dk_copenmask &= ~(1 << part);
968 break;
969 case S_IFBLK:
970 wd->sc_dk.dk_bopenmask &= ~(1 << part);
971 break;
972 }
973 wd->sc_dk.dk_openmask =
974 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
975
976 if (wd->sc_dk.dk_openmask == 0) {
977 wd_flushcache(wd, AT_WAIT);
978 /* XXXX Must wait for I/O to complete! */
979
980 if (! (wd->sc_flags & WDF_KLABEL))
981 wd->sc_flags &= ~WDF_LOADED;
982
983 wd->atabus->ata_delref(wd->drvp);
984 }
985
986 lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
987 return 0;
988 }
989
990 void
991 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
992 {
993
994 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
995 memset(lp, 0, sizeof(struct disklabel));
996
997 lp->d_secsize = DEV_BSIZE;
998 lp->d_ntracks = wd->sc_params.atap_heads;
999 lp->d_nsectors = wd->sc_params.atap_sectors;
1000 lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity /
1001 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors) :
1002 wd->sc_params.atap_cylinders;
1003 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1004
1005 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
1006 lp->d_type = DTYPE_ST506;
1007 else
1008 lp->d_type = DTYPE_ESDI;
1009
1010 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
1011 strncpy(lp->d_packname, "fictitious", 16);
1012 if (wd->sc_capacity > UINT32_MAX)
1013 lp->d_secperunit = UINT32_MAX;
1014 else
1015 lp->d_secperunit = wd->sc_capacity;
1016 lp->d_rpm = 3600;
1017 lp->d_interleave = 1;
1018 lp->d_flags = 0;
1019
1020 lp->d_partitions[RAW_PART].p_offset = 0;
1021 lp->d_partitions[RAW_PART].p_size =
1022 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1023 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1024 lp->d_npartitions = RAW_PART + 1;
1025
1026 lp->d_magic = DISKMAGIC;
1027 lp->d_magic2 = DISKMAGIC;
1028 lp->d_checksum = dkcksum(lp);
1029 }
1030
1031 /*
1032 * Fabricate a default disk label, and try to read the correct one.
1033 */
1034 void
1035 wdgetdisklabel(struct wd_softc *wd)
1036 {
1037 struct disklabel *lp = wd->sc_dk.dk_label;
1038 const char *errstring;
1039
1040 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1041
1042 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1043
1044 wdgetdefaultlabel(wd, lp);
1045
1046 wd->sc_badsect[0] = -1;
1047
1048 if (wd->drvp->state > RESET)
1049 wd->drvp->drive_flags |= DRIVE_RESET;
1050 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
1051 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1052 if (errstring) {
1053 /*
1054 * This probably happened because the drive's default
1055 * geometry doesn't match the DOS geometry. We
1056 * assume the DOS geometry is now in the label and try
1057 * again. XXX This is a kluge.
1058 */
1059 if (wd->drvp->state > RESET)
1060 wd->drvp->drive_flags |= DRIVE_RESET;
1061 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
1062 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1063 }
1064 if (errstring) {
1065 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
1066 return;
1067 }
1068
1069 if (wd->drvp->state > RESET)
1070 wd->drvp->drive_flags |= DRIVE_RESET;
1071 #ifdef HAS_BAD144_HANDLING
1072 if ((lp->d_flags & D_BADSECT) != 0)
1073 bad144intern(wd);
1074 #endif
1075 }
1076
1077 void
1078 wdperror(const struct wd_softc *wd)
1079 {
1080 static const char *const errstr0_3[] = {"address mark not found",
1081 "track 0 not found", "aborted command", "media change requested",
1082 "id not found", "media changed", "uncorrectable data error",
1083 "bad block detected"};
1084 static const char *const errstr4_5[] = {
1085 "obsolete (address mark not found)",
1086 "no media/write protected", "aborted command",
1087 "media change requested", "id not found", "media changed",
1088 "uncorrectable data error", "interface CRC error"};
1089 const char *const *errstr;
1090 int i;
1091 char *sep = "";
1092
1093 const char *devname = wd->sc_dev.dv_xname;
1094 struct ata_drive_datas *drvp = wd->drvp;
1095 int errno = wd->sc_wdc_bio.r_error;
1096
1097 if (drvp->ata_vers >= 4)
1098 errstr = errstr4_5;
1099 else
1100 errstr = errstr0_3;
1101
1102 printf("%s: (", devname);
1103
1104 if (errno == 0)
1105 printf("error not notified");
1106
1107 for (i = 0; i < 8; i++) {
1108 if (errno & (1 << i)) {
1109 printf("%s%s", sep, errstr[i]);
1110 sep = ", ";
1111 }
1112 }
1113 printf(")\n");
1114 }
1115
1116 int
1117 wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct proc *p)
1118 {
1119 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
1120 int error = 0;
1121 #ifdef __HAVE_OLD_DISKLABEL
1122 struct disklabel *newlabel = NULL;
1123 #endif
1124
1125 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1126
1127 if ((wd->sc_flags & WDF_LOADED) == 0)
1128 return EIO;
1129
1130 switch (xfer) {
1131 #ifdef HAS_BAD144_HANDLING
1132 case DIOCSBAD:
1133 if ((flag & FWRITE) == 0)
1134 return EBADF;
1135 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1136 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1137 bad144intern(wd);
1138 return 0;
1139 #endif
1140
1141 case DIOCBSLIST :
1142 {
1143 u_int32_t count, missing, skip;
1144 struct disk_badsecinfo dbsi;
1145 struct disk_badsectors *dbs;
1146 size_t available;
1147 caddr_t laddr;
1148
1149 dbsi = *(struct disk_badsecinfo *)addr;
1150 missing = wd->sc_bscount;
1151 count = 0;
1152 available = dbsi.dbsi_bufsize;
1153 skip = dbsi.dbsi_skip;
1154 laddr = dbsi.dbsi_buffer;
1155
1156 /*
1157 * We start this loop with the expectation that all of the
1158 * entries will be missed and decrement this counter each
1159 * time we either skip over one (already copied out) or
1160 * we actually copy it back to user space. The structs
1161 * holding the bad sector information are copied directly
1162 * back to user space whilst the summary is returned via
1163 * the struct passed in via the ioctl.
1164 */
1165 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1166 if (skip > 0) {
1167 missing--;
1168 skip--;
1169 continue;
1170 }
1171 if (available < sizeof(*dbs))
1172 break;
1173 available -= sizeof(*dbs);
1174 copyout(dbs, laddr, sizeof(*dbs));
1175 laddr += sizeof(*dbs);
1176 missing--;
1177 count++;
1178 }
1179 dbsi.dbsi_left = missing;
1180 dbsi.dbsi_copied = count;
1181 *(struct disk_badsecinfo *)addr = dbsi;
1182 return 0;
1183 }
1184
1185 case DIOCBSFLUSH :
1186 /* Clean out the bad sector list */
1187 while (!SLIST_EMPTY(&wd->sc_bslist)) {
1188 void *head = SLIST_FIRST(&wd->sc_bslist);
1189 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1190 free(head, M_TEMP);
1191 }
1192 wd->sc_bscount = 0;
1193 return 0;
1194
1195 case DIOCGDINFO:
1196 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1197 return 0;
1198 #ifdef __HAVE_OLD_DISKLABEL
1199 case ODIOCGDINFO:
1200 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1201 if (newlabel == NULL)
1202 return EIO;
1203 *newlabel = *(wd->sc_dk.dk_label);
1204 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1205 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1206 else
1207 error = ENOTTY;
1208 free(newlabel, M_TEMP);
1209 return error;
1210 #endif
1211
1212 case DIOCGPART:
1213 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1214 ((struct partinfo *)addr)->part =
1215 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1216 return 0;
1217
1218 case DIOCWDINFO:
1219 case DIOCSDINFO:
1220 #ifdef __HAVE_OLD_DISKLABEL
1221 case ODIOCWDINFO:
1222 case ODIOCSDINFO:
1223 #endif
1224 {
1225 struct disklabel *lp;
1226
1227 if ((flag & FWRITE) == 0)
1228 return EBADF;
1229
1230 #ifdef __HAVE_OLD_DISKLABEL
1231 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1232 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1233 if (newlabel == NULL)
1234 return EIO;
1235 memset(newlabel, 0, sizeof newlabel);
1236 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1237 lp = newlabel;
1238 } else
1239 #endif
1240 lp = (struct disklabel *)addr;
1241
1242 if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
1243 goto bad;
1244 wd->sc_flags |= WDF_LABELLING;
1245
1246 error = setdisklabel(wd->sc_dk.dk_label,
1247 lp, /*wd->sc_dk.dk_openmask : */0,
1248 wd->sc_dk.dk_cpulabel);
1249 if (error == 0) {
1250 if (wd->drvp->state > RESET)
1251 wd->drvp->drive_flags |= DRIVE_RESET;
1252 if (xfer == DIOCWDINFO
1253 #ifdef __HAVE_OLD_DISKLABEL
1254 || xfer == ODIOCWDINFO
1255 #endif
1256 )
1257 error = writedisklabel(WDLABELDEV(dev),
1258 wdstrategy, wd->sc_dk.dk_label,
1259 wd->sc_dk.dk_cpulabel);
1260 }
1261
1262 wd->sc_flags &= ~WDF_LABELLING;
1263 lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
1264 bad:
1265 #ifdef __HAVE_OLD_DISKLABEL
1266 if (newlabel != NULL)
1267 free(newlabel, M_TEMP);
1268 #endif
1269 return error;
1270 }
1271
1272 case DIOCKLABEL:
1273 if (*(int *)addr)
1274 wd->sc_flags |= WDF_KLABEL;
1275 else
1276 wd->sc_flags &= ~WDF_KLABEL;
1277 return 0;
1278
1279 case DIOCWLABEL:
1280 if ((flag & FWRITE) == 0)
1281 return EBADF;
1282 if (*(int *)addr)
1283 wd->sc_flags |= WDF_WLABEL;
1284 else
1285 wd->sc_flags &= ~WDF_WLABEL;
1286 return 0;
1287
1288 case DIOCGDEFLABEL:
1289 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1290 return 0;
1291 #ifdef __HAVE_OLD_DISKLABEL
1292 case ODIOCGDEFLABEL:
1293 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1294 if (newlabel == NULL)
1295 return EIO;
1296 wdgetdefaultlabel(wd, newlabel);
1297 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1298 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1299 else
1300 error = ENOTTY;
1301 free(newlabel, M_TEMP);
1302 return error;
1303 #endif
1304
1305 #ifdef notyet
1306 case DIOCWFORMAT:
1307 if ((flag & FWRITE) == 0)
1308 return EBADF;
1309 {
1310 register struct format_op *fop;
1311 struct iovec aiov;
1312 struct uio auio;
1313
1314 fop = (struct format_op *)addr;
1315 aiov.iov_base = fop->df_buf;
1316 aiov.iov_len = fop->df_count;
1317 auio.uio_iov = &aiov;
1318 auio.uio_iovcnt = 1;
1319 auio.uio_resid = fop->df_count;
1320 auio.uio_segflg = 0;
1321 auio.uio_offset =
1322 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1323 auio.uio_procp = p;
1324 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1325 &auio);
1326 fop->df_count -= auio.uio_resid;
1327 fop->df_reg[0] = wdc->sc_status;
1328 fop->df_reg[1] = wdc->sc_error;
1329 return error;
1330 }
1331 #endif
1332 case DIOCGCACHE:
1333 return wd_getcache(wd, (int *)addr);
1334
1335 case DIOCSCACHE:
1336 return wd_setcache(wd, *(int *)addr);
1337
1338 case DIOCCACHESYNC:
1339 return wd_flushcache(wd, AT_WAIT);
1340
1341 case ATAIOCCOMMAND:
1342 /*
1343 * Make sure this command is (relatively) safe first
1344 */
1345 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1346 (flag & FWRITE) == 0)
1347 return (EBADF);
1348 {
1349 struct wd_ioctl *wi;
1350 atareq_t *atareq = (atareq_t *) addr;
1351 int error;
1352
1353 wi = wi_get();
1354 wi->wi_softc = wd;
1355 wi->wi_atareq = *atareq;
1356
1357 if (atareq->datalen && atareq->flags &
1358 (ATACMD_READ | ATACMD_WRITE)) {
1359 wi->wi_iov.iov_base = atareq->databuf;
1360 wi->wi_iov.iov_len = atareq->datalen;
1361 wi->wi_uio.uio_iov = &wi->wi_iov;
1362 wi->wi_uio.uio_iovcnt = 1;
1363 wi->wi_uio.uio_resid = atareq->datalen;
1364 wi->wi_uio.uio_offset = 0;
1365 wi->wi_uio.uio_segflg = UIO_USERSPACE;
1366 wi->wi_uio.uio_rw =
1367 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1368 wi->wi_uio.uio_procp = p;
1369 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
1370 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1371 minphys, &wi->wi_uio);
1372 } else {
1373 /* No need to call physio if we don't have any
1374 user data */
1375 wi->wi_bp.b_flags = 0;
1376 wi->wi_bp.b_data = 0;
1377 wi->wi_bp.b_bcount = 0;
1378 wi->wi_bp.b_dev = 0;
1379 wi->wi_bp.b_proc = p;
1380 wdioctlstrategy(&wi->wi_bp);
1381 error = wi->wi_bp.b_error;
1382 }
1383 *atareq = wi->wi_atareq;
1384 wi_free(wi);
1385 return(error);
1386 }
1387
1388 default:
1389 return ENOTTY;
1390 }
1391
1392 #ifdef DIAGNOSTIC
1393 panic("wdioctl: impossible");
1394 #endif
1395 }
1396
1397 #ifdef B_FORMAT
1398 int
1399 wdformat(struct buf *bp)
1400 {
1401
1402 bp->b_flags |= B_FORMAT;
1403 return wdstrategy(bp);
1404 }
1405 #endif
1406
1407 int
1408 wdsize(dev_t dev)
1409 {
1410 struct wd_softc *wd;
1411 int part, omask;
1412 int size;
1413
1414 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1415
1416 wd = device_lookup(&wd_cd, WDUNIT(dev));
1417 if (wd == NULL)
1418 return (-1);
1419
1420 part = WDPART(dev);
1421 omask = wd->sc_dk.dk_openmask & (1 << part);
1422
1423 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1424 return (-1);
1425 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1426 size = -1;
1427 else
1428 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1429 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1430 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1431 return (-1);
1432 return (size);
1433 }
1434
1435 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1436 static int wddoingadump = 0;
1437 static int wddumprecalibrated = 0;
1438 static int wddumpmulti = 1;
1439
1440 /*
1441 * Dump core after a system crash.
1442 */
1443 int
1444 wddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1445 {
1446 struct wd_softc *wd; /* disk unit to do the I/O */
1447 struct disklabel *lp; /* disk's disklabel */
1448 int part, err;
1449 int nblks; /* total number of sectors left to write */
1450
1451 /* Check if recursive dump; if so, punt. */
1452 if (wddoingadump)
1453 return EFAULT;
1454 wddoingadump = 1;
1455
1456 wd = device_lookup(&wd_cd, WDUNIT(dev));
1457 if (wd == NULL)
1458 return (ENXIO);
1459
1460 part = WDPART(dev);
1461
1462 /* Convert to disk sectors. Request must be a multiple of size. */
1463 lp = wd->sc_dk.dk_label;
1464 if ((size % lp->d_secsize) != 0)
1465 return EFAULT;
1466 nblks = size / lp->d_secsize;
1467 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1468
1469 /* Check transfer bounds against partition size. */
1470 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1471 return EINVAL;
1472
1473 /* Offset block number to start of partition. */
1474 blkno += lp->d_partitions[part].p_offset;
1475
1476 /* Recalibrate, if first dump transfer. */
1477 if (wddumprecalibrated == 0) {
1478 wddumpmulti = wd->sc_multi;
1479 wddumprecalibrated = 1;
1480 wd->atabus->ata_reset_channel(wd->drvp, AT_POLL | AT_RST_EMERG);
1481 wd->drvp->state = RESET;
1482 }
1483
1484 while (nblks > 0) {
1485 wd->sc_bp = NULL;
1486 wd->sc_wdc_bio.blkno = blkno;
1487 wd->sc_wdc_bio.flags = ATA_POLL;
1488 if (wd->sc_flags & WDF_LBA48 &&
1489 (blkno > LBA48_THRESHOLD ||
1490 (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
1491 wd->sc_wdc_bio.flags |= ATA_LBA48;
1492 if (wd->sc_flags & WDF_LBA)
1493 wd->sc_wdc_bio.flags |= ATA_LBA;
1494 wd->sc_wdc_bio.bcount =
1495 min(nblks, wddumpmulti) * lp->d_secsize;
1496 wd->sc_wdc_bio.databuf = va;
1497 #ifndef WD_DUMP_NOT_TRUSTED
1498 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1499 case WDC_TRY_AGAIN:
1500 panic("wddump: try again");
1501 break;
1502 case WDC_QUEUED:
1503 panic("wddump: polled command has been queued");
1504 break;
1505 case WDC_COMPLETE:
1506 break;
1507 }
1508 switch(wd->sc_wdc_bio.error) {
1509 case TIMEOUT:
1510 printf("wddump: device timed out");
1511 err = EIO;
1512 break;
1513 case ERR_DF:
1514 printf("wddump: drive fault");
1515 err = EIO;
1516 break;
1517 case ERR_DMA:
1518 printf("wddump: DMA error");
1519 err = EIO;
1520 break;
1521 case ERROR:
1522 printf("wddump: ");
1523 wdperror(wd);
1524 err = EIO;
1525 break;
1526 case NOERROR:
1527 err = 0;
1528 break;
1529 default:
1530 panic("wddump: unknown error type");
1531 }
1532 if (err != 0) {
1533 printf("\n");
1534 return err;
1535 }
1536 #else /* WD_DUMP_NOT_TRUSTED */
1537 /* Let's just talk about this first... */
1538 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1539 unit, va, cylin, head, sector);
1540 delay(500 * 1000); /* half a second */
1541 #endif
1542
1543 /* update block count */
1544 nblks -= min(nblks, wddumpmulti);
1545 blkno += min(nblks, wddumpmulti);
1546 va += min(nblks, wddumpmulti) * lp->d_secsize;
1547 }
1548
1549 wddoingadump = 0;
1550 return 0;
1551 }
1552
1553 #ifdef HAS_BAD144_HANDLING
1554 /*
1555 * Internalize the bad sector table.
1556 */
1557 void
1558 bad144intern(struct wd_softc *wd)
1559 {
1560 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1561 struct disklabel *lp = wd->sc_dk.dk_label;
1562 int i = 0;
1563
1564 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1565
1566 for (; i < NBT_BAD; i++) {
1567 if (bt->bt_bad[i].bt_cyl == 0xffff)
1568 break;
1569 wd->sc_badsect[i] =
1570 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1571 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1572 (bt->bt_bad[i].bt_trksec & 0xff);
1573 }
1574 for (; i < NBT_BAD+1; i++)
1575 wd->sc_badsect[i] = -1;
1576 }
1577 #endif
1578
1579 int
1580 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params)
1581 {
1582 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1583 case CMD_AGAIN:
1584 return 1;
1585 case CMD_ERR:
1586 /*
1587 * We `know' there's a drive here; just assume it's old.
1588 * This geometry is only used to read the MBR and print a
1589 * (false) attach message.
1590 */
1591 strncpy(params->atap_model, "ST506",
1592 sizeof params->atap_model);
1593 params->atap_config = ATA_CFG_FIXED;
1594 params->atap_cylinders = 1024;
1595 params->atap_heads = 8;
1596 params->atap_sectors = 17;
1597 params->atap_multi = 1;
1598 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1599 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1600 return 0;
1601 case CMD_OK:
1602 return 0;
1603 default:
1604 panic("wd_get_params: bad return code from ata_get_params");
1605 /* NOTREACHED */
1606 }
1607 }
1608
1609 int
1610 wd_getcache(struct wd_softc *wd, int *bitsp)
1611 {
1612 struct ataparams params;
1613
1614 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1615 return EIO;
1616 if (params.atap_cmd_set1 == 0x0000 ||
1617 params.atap_cmd_set1 == 0xffff ||
1618 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
1619 *bitsp = 0;
1620 return 0;
1621 }
1622 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
1623 if (params.atap_cmd1_en & WDC_CMD1_CACHE)
1624 *bitsp |= DKCACHE_WRITE;
1625
1626 return 0;
1627 }
1628
1629 int
1630 wd_setcache(struct wd_softc *wd, int bits)
1631 {
1632 struct ataparams params;
1633 struct wdc_command wdc_c;
1634
1635 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1636 return EIO;
1637
1638 if (params.atap_cmd_set1 == 0x0000 ||
1639 params.atap_cmd_set1 == 0xffff ||
1640 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
1641 return EOPNOTSUPP;
1642
1643 if ((bits & DKCACHE_READ) == 0 ||
1644 (bits & DKCACHE_SAVE) != 0)
1645 return EOPNOTSUPP;
1646
1647 memset(&wdc_c, 0, sizeof(struct wdc_command));
1648 wdc_c.r_command = SET_FEATURES;
1649 wdc_c.r_st_bmask = 0;
1650 wdc_c.r_st_pmask = 0;
1651 wdc_c.timeout = 30000; /* 30s timeout */
1652 wdc_c.flags = AT_WAIT;
1653 if (bits & DKCACHE_WRITE)
1654 wdc_c.r_precomp = WDSF_WRITE_CACHE_EN;
1655 else
1656 wdc_c.r_precomp = WDSF_WRITE_CACHE_DS;
1657 if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1658 printf("%s: wd_setcache command not complete\n",
1659 wd->sc_dev.dv_xname);
1660 return EIO;
1661 }
1662 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1663 printf("%s: wd_setcache command error 0x%x\n",
1664 wd->sc_dev.dv_xname, wdc_c.flags);
1665 return EIO;
1666 }
1667 if (wdc_c.flags & ERR_NODEV)
1668 return ENODEV;
1669 return 0;
1670 }
1671
1672 int
1673 wd_flushcache(struct wd_softc *wd, int flags)
1674 {
1675 struct wdc_command wdc_c;
1676
1677 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1678 return ENODEV;
1679 memset(&wdc_c, 0, sizeof(struct wdc_command));
1680 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
1681 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0)
1682 wdc_c.r_command = WDCC_FLUSHCACHE_EXT;
1683 else
1684 wdc_c.r_command = WDCC_FLUSHCACHE;
1685 wdc_c.r_st_bmask = WDCS_DRDY;
1686 wdc_c.r_st_pmask = WDCS_DRDY;
1687 wdc_c.flags = flags;
1688 wdc_c.timeout = 30000; /* 30s timeout */
1689 if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1690 printf("%s: flush cache command didn't complete\n",
1691 wd->sc_dev.dv_xname);
1692 return EIO;
1693 }
1694 if (wdc_c.flags & ERR_NODEV)
1695 return ENODEV;
1696 if (wdc_c.flags & AT_TIMEOU) {
1697 printf("%s: flush cache command timeout\n",
1698 wd->sc_dev.dv_xname);
1699 return EIO;
1700 }
1701 if (wdc_c.flags & AT_ERROR) {
1702 if (wdc_c.r_error == WDCE_ABRT) /* command not supported */
1703 return ENODEV;
1704 printf("%s: flush cache command: error 0x%x\n",
1705 wd->sc_dev.dv_xname, wdc_c.r_error);
1706 return EIO;
1707 }
1708 if (wdc_c.flags & AT_DF) {
1709 printf("%s: flush cache command: drive fault\n",
1710 wd->sc_dev.dv_xname);
1711 return EIO;
1712 }
1713 return 0;
1714 }
1715
1716 void
1717 wd_shutdown(void *arg)
1718 {
1719 struct wd_softc *wd = arg;
1720 wd_flushcache(wd, AT_POLL);
1721 }
1722
1723 /*
1724 * Allocate space for a ioctl queue structure. Mostly taken from
1725 * scsipi_ioctl.c
1726 */
1727 struct wd_ioctl *
1728 wi_get(void)
1729 {
1730 struct wd_ioctl *wi;
1731 int s;
1732
1733 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
1734 simple_lock_init(&wi->wi_bp.b_interlock);
1735 s = splbio();
1736 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1737 splx(s);
1738 return (wi);
1739 }
1740
1741 /*
1742 * Free an ioctl structure and remove it from our list
1743 */
1744
1745 void
1746 wi_free(struct wd_ioctl *wi)
1747 {
1748 int s;
1749
1750 s = splbio();
1751 LIST_REMOVE(wi, wi_list);
1752 splx(s);
1753 free(wi, M_TEMP);
1754 }
1755
1756 /*
1757 * Find a wd_ioctl structure based on the struct buf.
1758 */
1759
1760 struct wd_ioctl *
1761 wi_find(struct buf *bp)
1762 {
1763 struct wd_ioctl *wi;
1764 int s;
1765
1766 s = splbio();
1767 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1768 if (bp == &wi->wi_bp)
1769 break;
1770 splx(s);
1771 return (wi);
1772 }
1773
1774 /*
1775 * Ioctl pseudo strategy routine
1776 *
1777 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1778 * happens here is:
1779 *
1780 * - wdioctl() queues a wd_ioctl structure.
1781 *
1782 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1783 * user space I/O is required. If physio() is called, physio() eventually
1784 * calls wdioctlstrategy().
1785 *
1786 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
1787 * to perform the actual command
1788 *
1789 * The reason for the use of the pseudo strategy routine is because
1790 * when doing I/O to/from user space, physio _really_ wants to be in
1791 * the loop. We could put the entire buffer into the ioctl request
1792 * structure, but that won't scale if we want to do things like download
1793 * microcode.
1794 */
1795
1796 void
1797 wdioctlstrategy(struct buf *bp)
1798 {
1799 struct wd_ioctl *wi;
1800 struct wdc_command wdc_c;
1801 int error = 0;
1802
1803 wi = wi_find(bp);
1804 if (wi == NULL) {
1805 printf("user_strat: No ioctl\n");
1806 error = EINVAL;
1807 goto bad;
1808 }
1809
1810 memset(&wdc_c, 0, sizeof(wdc_c));
1811
1812 /*
1813 * Abort if physio broke up the transfer
1814 */
1815
1816 if (bp->b_bcount != wi->wi_atareq.datalen) {
1817 printf("physio split wd ioctl request... cannot proceed\n");
1818 error = EIO;
1819 goto bad;
1820 }
1821
1822 /*
1823 * Abort if we didn't get a buffer size that was a multiple of
1824 * our sector size (or was larger than NBBY)
1825 */
1826
1827 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1828 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1829 (1 << NBBY)) {
1830 error = EINVAL;
1831 goto bad;
1832 }
1833
1834 /*
1835 * Make sure a timeout was supplied in the ioctl request
1836 */
1837
1838 if (wi->wi_atareq.timeout == 0) {
1839 error = EINVAL;
1840 goto bad;
1841 }
1842
1843 if (wi->wi_atareq.flags & ATACMD_READ)
1844 wdc_c.flags |= AT_READ;
1845 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1846 wdc_c.flags |= AT_WRITE;
1847
1848 if (wi->wi_atareq.flags & ATACMD_READREG)
1849 wdc_c.flags |= AT_READREG;
1850
1851 wdc_c.flags |= AT_WAIT;
1852
1853 wdc_c.timeout = wi->wi_atareq.timeout;
1854 wdc_c.r_command = wi->wi_atareq.command;
1855 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1856 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1857 wdc_c.r_sector = wi->wi_atareq.sec_num;
1858 wdc_c.r_count = wi->wi_atareq.sec_count;
1859 wdc_c.r_precomp = wi->wi_atareq.features;
1860 wdc_c.r_st_bmask = WDCS_DRDY;
1861 wdc_c.r_st_pmask = WDCS_DRDY;
1862 wdc_c.data = wi->wi_bp.b_data;
1863 wdc_c.bcount = wi->wi_bp.b_bcount;
1864
1865 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &wdc_c)
1866 != WDC_COMPLETE) {
1867 wi->wi_atareq.retsts = ATACMD_ERROR;
1868 goto bad;
1869 }
1870
1871 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1872 if (wdc_c.flags & AT_ERROR) {
1873 wi->wi_atareq.retsts = ATACMD_ERROR;
1874 wi->wi_atareq.error = wdc_c.r_error;
1875 } else if (wdc_c.flags & AT_DF)
1876 wi->wi_atareq.retsts = ATACMD_DF;
1877 else
1878 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1879 } else {
1880 wi->wi_atareq.retsts = ATACMD_OK;
1881 if (wi->wi_atareq.flags & ATACMD_READREG) {
1882 wi->wi_atareq.head = wdc_c.r_head ;
1883 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1884 wi->wi_atareq.sec_num = wdc_c.r_sector;
1885 wi->wi_atareq.sec_count = wdc_c.r_count;
1886 wi->wi_atareq.features = wdc_c.r_precomp;
1887 wi->wi_atareq.error = wdc_c.r_error;
1888 }
1889 }
1890
1891 bp->b_error = 0;
1892 biodone(bp);
1893 return;
1894 bad:
1895 bp->b_flags |= B_ERROR;
1896 bp->b_error = error;
1897 biodone(bp);
1898 }
1899