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