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