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