wd.c revision 1.279 1 /* $NetBSD: wd.c,v 1.279 2004/06/22 19:20:14 mycroft 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.279 2004/06/22 19:20:14 mycroft 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 lockmgr(&sc->sc_lock, LK_DRAIN, NULL);
419
420 /* Clean out the bad sector list */
421 while (!SLIST_EMPTY(&sc->sc_bslist)) {
422 void *head = SLIST_FIRST(&sc->sc_bslist);
423 SLIST_REMOVE_HEAD(&sc->sc_bslist, dbs_next);
424 free(head, M_TEMP);
425 }
426 sc->sc_bscount = 0;
427
428 /* locate the major number */
429 bmaj = bdevsw_lookup_major(&wd_bdevsw);
430 cmaj = cdevsw_lookup_major(&wd_cdevsw);
431
432 s = splbio();
433
434 /* Kill off any queued buffers. */
435 while ((bp = BUFQ_GET(&sc->sc_q)) != NULL) {
436 bp->b_error = EIO;
437 bp->b_flags |= B_ERROR;
438 bp->b_resid = bp->b_bcount;
439 biodone(bp);
440 }
441
442 bufq_free(&sc->sc_q);
443
444 splx(s);
445
446 /* Nuke the vnodes for any open instances. */
447 for (i = 0; i < MAXPARTITIONS; i++) {
448 mn = WDMINOR(self->dv_unit, i);
449 vdevgone(bmaj, mn, mn, VBLK);
450 vdevgone(cmaj, mn, mn, VCHR);
451 }
452
453 /* Detach disk. */
454 disk_detach(&sc->sc_dk);
455
456 /* Get rid of the shutdown hook. */
457 if (sc->sc_sdhook != NULL)
458 shutdownhook_disestablish(sc->sc_sdhook);
459
460 #if NRND > 0
461 /* Unhook the entropy source. */
462 rnd_detach_source(&sc->rnd_source);
463 #endif
464
465 return (0);
466 }
467
468 /*
469 * Read/write routine for a buffer. Validates the arguments and schedules the
470 * transfer. Does not wait for the transfer to complete.
471 */
472 void
473 wdstrategy(struct buf *bp)
474 {
475 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
476 struct disklabel *lp = wd->sc_dk.dk_label;
477 daddr_t blkno;
478 int s;
479
480 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
481 DEBUG_XFERS);
482
483 /* Valid request? */
484 if (bp->b_blkno < 0 ||
485 (bp->b_bcount % lp->d_secsize) != 0 ||
486 (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
487 bp->b_error = EINVAL;
488 goto bad;
489 }
490
491 /* If device invalidated (e.g. media change, door open), error. */
492 if ((wd->sc_flags & WDF_LOADED) == 0) {
493 bp->b_error = EIO;
494 goto bad;
495 }
496
497 /* If it's a null transfer, return immediately. */
498 if (bp->b_bcount == 0)
499 goto done;
500
501 /*
502 * Do bounds checking, adjust transfer. if error, process.
503 * If end of partition, just return.
504 */
505 if (WDPART(bp->b_dev) == RAW_PART) {
506 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
507 wd->sc_capacity) <= 0)
508 goto done;
509 } else {
510 if (bounds_check_with_label(&wd->sc_dk, bp,
511 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
512 goto done;
513 }
514
515 /*
516 * Now convert the block number to absolute and put it in
517 * terms of the device's logical block size.
518 */
519 if (lp->d_secsize >= DEV_BSIZE)
520 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
521 else
522 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
523
524 if (WDPART(bp->b_dev) != RAW_PART)
525 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
526
527 bp->b_rawblkno = blkno;
528
529 /*
530 * If the transfer about to be attempted contains only a block that
531 * is known to be bad then return an error for the transfer without
532 * even attempting to start a transfer up under the premis that we
533 * will just end up doing more retries for a transfer that will end
534 * up failing again.
535 * XXX:SMP - mutex required to protect with DIOCBSFLUSH
536 */
537 if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) {
538 struct disk_badsectors *dbs;
539 daddr_t maxblk = blkno + (bp->b_bcount >> DEV_BSHIFT) - 1;
540
541 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next)
542 if ((dbs->dbs_min <= blkno && blkno <= dbs->dbs_max) ||
543 (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){
544 bp->b_error = EIO;
545 goto bad;
546 }
547 }
548
549 /* Queue transfer on drive, activate drive and controller if idle. */
550 s = splbio();
551 BUFQ_PUT(&wd->sc_q, bp);
552 wdstart(wd);
553 splx(s);
554 return;
555 bad:
556 bp->b_flags |= B_ERROR;
557 done:
558 /* Toss transfer; we're done early. */
559 bp->b_resid = bp->b_bcount;
560 biodone(bp);
561 }
562
563 /*
564 * Queue a drive for I/O.
565 */
566 void
567 wdstart(void *arg)
568 {
569 struct wd_softc *wd = arg;
570 struct buf *bp = NULL;
571
572 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
573 DEBUG_XFERS);
574 while (wd->openings > 0) {
575
576 /* Is there a buf for us ? */
577 if ((bp = BUFQ_GET(&wd->sc_q)) == NULL)
578 return;
579
580 /*
581 * Make the command. First lock the device
582 */
583 wd->openings--;
584
585 wd->retries = 0;
586 __wdstart(wd, bp);
587 }
588 }
589
590 static void
591 wd_split_mod15_write(struct buf *bp)
592 {
593 struct buf *obp = bp->b_private;
594 struct wd_softc *sc = wd_cd.cd_devs[DISKUNIT(obp->b_dev)];
595
596 if (__predict_false(bp->b_flags & B_ERROR) != 0) {
597 /*
598 * Propagate the error. If this was the first half of
599 * the original transfer, make sure to account for that
600 * in the residual.
601 */
602 if (bp->b_data == obp->b_data)
603 bp->b_resid += bp->b_bcount;
604 goto done;
605 }
606
607 /*
608 * If this was the second half of the transfer, we're all done!
609 */
610 if (bp->b_data != obp->b_data)
611 goto done;
612
613 /*
614 * Advance the pointer to the second half and issue that command
615 * using the same opening.
616 */
617 bp->b_flags = obp->b_flags | B_CALL;
618 bp->b_data += bp->b_bcount;
619 bp->b_blkno += (bp->b_bcount / 512);
620 bp->b_rawblkno += (bp->b_bcount / 512);
621 __wdstart(sc, bp);
622 return;
623
624 done:
625 obp->b_flags |= (bp->b_flags & (B_EINTR|B_ERROR));
626 obp->b_error = bp->b_error;
627 obp->b_resid = bp->b_resid;
628 pool_put(&bufpool, bp);
629 biodone(obp);
630 sc->openings++;
631 /* wddone() will call wdstart() */
632 }
633
634 void
635 __wdstart(struct wd_softc *wd, struct buf *bp)
636 {
637
638 /*
639 * Deal with the "split mod15 write" quirk. We just divide the
640 * transfer in two, doing the first half and then then second half
641 * with the same command opening.
642 *
643 * Note we MUST do this here, because we can't let insertion
644 * into the bufq cause the transfers to be re-merged.
645 */
646 if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 &&
647 (bp->b_flags & B_READ) == 0 &&
648 bp->b_bcount > 512 &&
649 ((bp->b_bcount / 512) % 15) == 1)) {
650 struct buf *nbp;
651
652 /* already at splbio */
653 nbp = pool_get(&bufpool, PR_NOWAIT);
654 if (__predict_false(nbp == NULL)) {
655 /* No memory -- fail the iop. */
656 bp->b_error = ENOMEM;
657 bp->b_flags |= B_ERROR;
658 bp->b_resid = bp->b_bcount;
659 biodone(bp);
660 wd->openings++;
661 return;
662 }
663
664 BUF_INIT(nbp);
665 nbp->b_error = 0;
666 nbp->b_proc = bp->b_proc;
667 nbp->b_vp = NULLVP;
668 nbp->b_dev = bp->b_dev;
669
670 nbp->b_bcount = bp->b_bcount / 2;
671 nbp->b_bufsize = bp->b_bcount / 2;
672 nbp->b_data = bp->b_data;
673
674 nbp->b_blkno = bp->b_blkno;
675 nbp->b_rawblkno = bp->b_rawblkno;
676
677 nbp->b_flags = bp->b_flags | B_CALL;
678 nbp->b_iodone = wd_split_mod15_write;
679
680 /* Put ptr to orig buf in b_private and use new buf */
681 nbp->b_private = bp;
682
683 BIO_COPYPRIO(nbp, bp);
684
685 bp = nbp;
686 }
687
688 wd->sc_wdc_bio.blkno = bp->b_rawblkno;
689 wd->sc_wdc_bio.blkdone =0;
690 wd->sc_bp = bp;
691 /*
692 * If we're retrying, retry in single-sector mode. This will give us
693 * the sector number of the problem, and will eventually allow the
694 * transfer to succeed.
695 */
696 if (wd->retries >= WDIORETRIES_SINGLE)
697 wd->sc_wdc_bio.flags = ATA_SINGLE;
698 else
699 wd->sc_wdc_bio.flags = 0;
700 if (wd->sc_flags & WDF_LBA48 && wd->sc_wdc_bio.blkno > LBA48_THRESHOLD)
701 wd->sc_wdc_bio.flags |= ATA_LBA48;
702 if (wd->sc_flags & WDF_LBA)
703 wd->sc_wdc_bio.flags |= ATA_LBA;
704 if (bp->b_flags & B_READ)
705 wd->sc_wdc_bio.flags |= ATA_READ;
706 wd->sc_wdc_bio.bcount = bp->b_bcount;
707 wd->sc_wdc_bio.databuf = bp->b_data;
708 /* Instrumentation. */
709 disk_busy(&wd->sc_dk);
710 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
711 case WDC_TRY_AGAIN:
712 callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
713 break;
714 case WDC_QUEUED:
715 case WDC_COMPLETE:
716 break;
717 default:
718 panic("__wdstart: bad return code from ata_bio()");
719 }
720 }
721
722 void
723 wddone(void *v)
724 {
725 struct wd_softc *wd = v;
726 struct buf *bp = wd->sc_bp;
727 const char *errmsg;
728 int do_perror = 0;
729 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
730 DEBUG_XFERS);
731
732 if (bp == NULL)
733 return;
734 bp->b_resid = wd->sc_wdc_bio.bcount;
735 switch (wd->sc_wdc_bio.error) {
736 case ERR_DMA:
737 errmsg = "DMA error";
738 goto retry;
739 case ERR_DF:
740 errmsg = "device fault";
741 goto retry;
742 case TIMEOUT:
743 errmsg = "device timeout";
744 goto retry;
745 case ERROR:
746 /* Don't care about media change bits */
747 if (wd->sc_wdc_bio.r_error != 0 &&
748 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
749 goto noerror;
750 errmsg = "error";
751 do_perror = 1;
752 retry: /* Just reset and retry. Can we do more ? */
753 wd->atabus->ata_reset_channel(wd->drvp, 0);
754 diskerr(bp, "wd", errmsg, LOG_PRINTF,
755 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
756 if (wd->retries < WDIORETRIES)
757 printf(", retrying\n");
758 if (do_perror)
759 wdperror(wd);
760 if (wd->retries < WDIORETRIES) {
761 wd->retries++;
762 callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
763 wdrestart, wd);
764 return;
765 }
766 printf("\n");
767
768 /*
769 * Not all errors indicate a failed block but those that do,
770 * put the block on the bad-block list for the device. Only
771 * do this for reads because the drive should do it for writes,
772 * itself, according to Manuel.
773 */
774 if ((bp->b_flags & B_READ) &&
775 ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) ||
776 (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) {
777 struct disk_badsectors *dbs;
778
779 dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK);
780 dbs->dbs_min = bp->b_rawblkno;
781 dbs->dbs_max = dbs->dbs_min + (bp->b_bcount >> DEV_BSHIFT) - 1;
782 microtime(&dbs->dbs_failedat);
783 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
784 wd->sc_bscount++;
785 }
786
787 bp->b_flags |= B_ERROR;
788 bp->b_error = EIO;
789 break;
790 case NOERROR:
791 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
792 printf("%s: soft error (corrected)\n",
793 wd->sc_dev.dv_xname);
794 break;
795 case ERR_NODEV:
796 bp->b_flags |= B_ERROR;
797 bp->b_error = EIO;
798 break;
799 }
800 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
801 (bp->b_flags & B_READ));
802 #if NRND > 0
803 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
804 #endif
805 /* XXX Yuck, but we don't want to increment openings in this case */
806 if (__predict_false((bp->b_flags & B_CALL) != 0 &&
807 bp->b_iodone == wd_split_mod15_write))
808 biodone(bp);
809 else {
810 biodone(bp);
811 wd->openings++;
812 }
813 wdstart(wd);
814 }
815
816 void
817 wdrestart(void *v)
818 {
819 struct wd_softc *wd = v;
820 struct buf *bp = wd->sc_bp;
821 int s;
822 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
823 DEBUG_XFERS);
824
825 s = splbio();
826 __wdstart(v, bp);
827 splx(s);
828 }
829
830 int
831 wdread(dev_t dev, struct uio *uio, int flags)
832 {
833
834 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
835 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
836 }
837
838 int
839 wdwrite(dev_t dev, struct uio *uio, int flags)
840 {
841
842 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
843 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
844 }
845
846 int
847 wdopen(dev_t dev, int flag, int fmt, struct proc *p)
848 {
849 struct wd_softc *wd;
850 int part, error;
851
852 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
853 wd = device_lookup(&wd_cd, WDUNIT(dev));
854 if (wd == NULL)
855 return (ENXIO);
856
857 /*
858 * If this is the first open of this device, add a reference
859 * to the adapter.
860 */
861 if (wd->sc_dk.dk_openmask == 0 &&
862 (error = wd->atabus->ata_addref(wd->drvp)) != 0)
863 return (error);
864
865 if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
866 goto bad4;
867
868 if (wd->sc_dk.dk_openmask != 0) {
869 /*
870 * If any partition is open, but the disk has been invalidated,
871 * disallow further opens.
872 */
873 if ((wd->sc_flags & WDF_LOADED) == 0) {
874 error = EIO;
875 goto bad3;
876 }
877 } else {
878 if ((wd->sc_flags & WDF_LOADED) == 0) {
879 wd->sc_flags |= WDF_LOADED;
880
881 /* Load the physical device parameters. */
882 wd_get_params(wd, AT_WAIT, &wd->sc_params);
883
884 /* Load the partition info if not already loaded. */
885 wdgetdisklabel(wd);
886 }
887 }
888
889 part = WDPART(dev);
890
891 /* Check that the partition exists. */
892 if (part != RAW_PART &&
893 (part >= wd->sc_dk.dk_label->d_npartitions ||
894 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
895 error = ENXIO;
896 goto bad;
897 }
898
899 /* Insure only one open at a time. */
900 switch (fmt) {
901 case S_IFCHR:
902 wd->sc_dk.dk_copenmask |= (1 << part);
903 break;
904 case S_IFBLK:
905 wd->sc_dk.dk_bopenmask |= (1 << part);
906 break;
907 }
908 wd->sc_dk.dk_openmask =
909 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
910
911 lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
912 return 0;
913
914 bad:
915 if (wd->sc_dk.dk_openmask == 0) {
916 }
917
918 bad3:
919 lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
920 bad4:
921 if (wd->sc_dk.dk_openmask == 0)
922 wd->atabus->ata_delref(wd->drvp);
923 return error;
924 }
925
926 int
927 wdclose(dev_t dev, int flag, int fmt, struct proc *p)
928 {
929 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
930 int part = WDPART(dev);
931 int error;
932
933 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
934 if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
935 return error;
936
937 switch (fmt) {
938 case S_IFCHR:
939 wd->sc_dk.dk_copenmask &= ~(1 << part);
940 break;
941 case S_IFBLK:
942 wd->sc_dk.dk_bopenmask &= ~(1 << part);
943 break;
944 }
945 wd->sc_dk.dk_openmask =
946 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
947
948 if (wd->sc_dk.dk_openmask == 0) {
949 wd_flushcache(wd, AT_WAIT);
950 /* XXXX Must wait for I/O to complete! */
951
952 if (! (wd->sc_flags & WDF_KLABEL))
953 wd->sc_flags &= ~WDF_LOADED;
954
955 wd->atabus->ata_delref(wd->drvp);
956 }
957
958 lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
959 return 0;
960 }
961
962 void
963 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
964 {
965
966 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
967 memset(lp, 0, sizeof(struct disklabel));
968
969 lp->d_secsize = DEV_BSIZE;
970 lp->d_ntracks = wd->sc_params.atap_heads;
971 lp->d_nsectors = wd->sc_params.atap_sectors;
972 lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity /
973 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors) :
974 wd->sc_params.atap_cylinders;
975 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
976
977 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
978 lp->d_type = DTYPE_ST506;
979 else
980 lp->d_type = DTYPE_ESDI;
981
982 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
983 strncpy(lp->d_packname, "fictitious", 16);
984 if (wd->sc_capacity > UINT32_MAX)
985 lp->d_secperunit = UINT32_MAX;
986 else
987 lp->d_secperunit = wd->sc_capacity;
988 lp->d_rpm = 3600;
989 lp->d_interleave = 1;
990 lp->d_flags = 0;
991
992 lp->d_partitions[RAW_PART].p_offset = 0;
993 lp->d_partitions[RAW_PART].p_size =
994 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
995 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
996 lp->d_npartitions = RAW_PART + 1;
997
998 lp->d_magic = DISKMAGIC;
999 lp->d_magic2 = DISKMAGIC;
1000 lp->d_checksum = dkcksum(lp);
1001 }
1002
1003 /*
1004 * Fabricate a default disk label, and try to read the correct one.
1005 */
1006 void
1007 wdgetdisklabel(struct wd_softc *wd)
1008 {
1009 struct disklabel *lp = wd->sc_dk.dk_label;
1010 const char *errstring;
1011
1012 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1013
1014 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1015
1016 wdgetdefaultlabel(wd, lp);
1017
1018 wd->sc_badsect[0] = -1;
1019
1020 if (wd->drvp->state > RESET)
1021 wd->drvp->drive_flags |= DRIVE_RESET;
1022 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
1023 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1024 if (errstring) {
1025 /*
1026 * This probably happened because the drive's default
1027 * geometry doesn't match the DOS geometry. We
1028 * assume the DOS geometry is now in the label and try
1029 * again. XXX This is a kluge.
1030 */
1031 if (wd->drvp->state > RESET)
1032 wd->drvp->drive_flags |= DRIVE_RESET;
1033 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
1034 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1035 }
1036 if (errstring) {
1037 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
1038 return;
1039 }
1040
1041 if (wd->drvp->state > RESET)
1042 wd->drvp->drive_flags |= DRIVE_RESET;
1043 #ifdef HAS_BAD144_HANDLING
1044 if ((lp->d_flags & D_BADSECT) != 0)
1045 bad144intern(wd);
1046 #endif
1047 }
1048
1049 void
1050 wdperror(const struct wd_softc *wd)
1051 {
1052 static const char *const errstr0_3[] = {"address mark not found",
1053 "track 0 not found", "aborted command", "media change requested",
1054 "id not found", "media changed", "uncorrectable data error",
1055 "bad block detected"};
1056 static const char *const errstr4_5[] = {
1057 "obsolete (address mark not found)",
1058 "no media/write protected", "aborted command",
1059 "media change requested", "id not found", "media changed",
1060 "uncorrectable data error", "interface CRC error"};
1061 const char *const *errstr;
1062 int i;
1063 char *sep = "";
1064
1065 const char *devname = wd->sc_dev.dv_xname;
1066 struct ata_drive_datas *drvp = wd->drvp;
1067 int errno = wd->sc_wdc_bio.r_error;
1068
1069 if (drvp->ata_vers >= 4)
1070 errstr = errstr4_5;
1071 else
1072 errstr = errstr0_3;
1073
1074 printf("%s: (", devname);
1075
1076 if (errno == 0)
1077 printf("error not notified");
1078
1079 for (i = 0; i < 8; i++) {
1080 if (errno & (1 << i)) {
1081 printf("%s%s", sep, errstr[i]);
1082 sep = ", ";
1083 }
1084 }
1085 printf(")\n");
1086 }
1087
1088 int
1089 wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct proc *p)
1090 {
1091 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
1092 int error = 0;
1093 #ifdef __HAVE_OLD_DISKLABEL
1094 struct disklabel *newlabel = NULL;
1095 #endif
1096
1097 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1098
1099 if ((wd->sc_flags & WDF_LOADED) == 0)
1100 return EIO;
1101
1102 switch (xfer) {
1103 #ifdef HAS_BAD144_HANDLING
1104 case DIOCSBAD:
1105 if ((flag & FWRITE) == 0)
1106 return EBADF;
1107 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1108 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1109 bad144intern(wd);
1110 return 0;
1111 #endif
1112
1113 case DIOCBSLIST :
1114 {
1115 u_int32_t count, missing, skip;
1116 struct disk_badsecinfo dbsi;
1117 struct disk_badsectors *dbs;
1118 size_t available;
1119 caddr_t laddr;
1120
1121 dbsi = *(struct disk_badsecinfo *)addr;
1122 missing = wd->sc_bscount;
1123 count = 0;
1124 available = dbsi.dbsi_bufsize;
1125 skip = dbsi.dbsi_skip;
1126 laddr = dbsi.dbsi_buffer;
1127
1128 /*
1129 * We start this loop with the expectation that all of the
1130 * entries will be missed and decrement this counter each
1131 * time we either skip over one (already copied out) or
1132 * we actually copy it back to user space. The structs
1133 * holding the bad sector information are copied directly
1134 * back to user space whilst the summary is returned via
1135 * the struct passed in via the ioctl.
1136 */
1137 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1138 if (skip > 0) {
1139 missing--;
1140 skip--;
1141 continue;
1142 }
1143 if (available < sizeof(*dbs))
1144 break;
1145 available -= sizeof(*dbs);
1146 copyout(dbs, laddr, sizeof(*dbs));
1147 laddr += sizeof(*dbs);
1148 missing--;
1149 count++;
1150 }
1151 dbsi.dbsi_left = missing;
1152 dbsi.dbsi_copied = count;
1153 *(struct disk_badsecinfo *)addr = dbsi;
1154 return 0;
1155 }
1156
1157 case DIOCBSFLUSH :
1158 /* Clean out the bad sector list */
1159 while (!SLIST_EMPTY(&wd->sc_bslist)) {
1160 void *head = SLIST_FIRST(&wd->sc_bslist);
1161 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1162 free(head, M_TEMP);
1163 }
1164 wd->sc_bscount = 0;
1165 return 0;
1166
1167 case DIOCGDINFO:
1168 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1169 return 0;
1170 #ifdef __HAVE_OLD_DISKLABEL
1171 case ODIOCGDINFO:
1172 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1173 if (newlabel == NULL)
1174 return EIO;
1175 *newlabel = *(wd->sc_dk.dk_label);
1176 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1177 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1178 else
1179 error = ENOTTY;
1180 free(newlabel, M_TEMP);
1181 return error;
1182 #endif
1183
1184 case DIOCGPART:
1185 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1186 ((struct partinfo *)addr)->part =
1187 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1188 return 0;
1189
1190 case DIOCWDINFO:
1191 case DIOCSDINFO:
1192 #ifdef __HAVE_OLD_DISKLABEL
1193 case ODIOCWDINFO:
1194 case ODIOCSDINFO:
1195 #endif
1196 {
1197 struct disklabel *lp;
1198
1199 if ((flag & FWRITE) == 0)
1200 return EBADF;
1201
1202 #ifdef __HAVE_OLD_DISKLABEL
1203 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1204 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1205 if (newlabel == NULL)
1206 return EIO;
1207 memset(newlabel, 0, sizeof newlabel);
1208 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1209 lp = newlabel;
1210 } else
1211 #endif
1212 lp = (struct disklabel *)addr;
1213
1214 if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
1215 goto bad;
1216 wd->sc_flags |= WDF_LABELLING;
1217
1218 error = setdisklabel(wd->sc_dk.dk_label,
1219 lp, /*wd->sc_dk.dk_openmask : */0,
1220 wd->sc_dk.dk_cpulabel);
1221 if (error == 0) {
1222 if (wd->drvp->state > RESET)
1223 wd->drvp->drive_flags |= DRIVE_RESET;
1224 if (xfer == DIOCWDINFO
1225 #ifdef __HAVE_OLD_DISKLABEL
1226 || xfer == ODIOCWDINFO
1227 #endif
1228 )
1229 error = writedisklabel(WDLABELDEV(dev),
1230 wdstrategy, wd->sc_dk.dk_label,
1231 wd->sc_dk.dk_cpulabel);
1232 }
1233
1234 wd->sc_flags &= ~WDF_LABELLING;
1235 lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
1236 bad:
1237 #ifdef __HAVE_OLD_DISKLABEL
1238 if (newlabel != NULL)
1239 free(newlabel, M_TEMP);
1240 #endif
1241 return error;
1242 }
1243
1244 case DIOCKLABEL:
1245 if (*(int *)addr)
1246 wd->sc_flags |= WDF_KLABEL;
1247 else
1248 wd->sc_flags &= ~WDF_KLABEL;
1249 return 0;
1250
1251 case DIOCWLABEL:
1252 if ((flag & FWRITE) == 0)
1253 return EBADF;
1254 if (*(int *)addr)
1255 wd->sc_flags |= WDF_WLABEL;
1256 else
1257 wd->sc_flags &= ~WDF_WLABEL;
1258 return 0;
1259
1260 case DIOCGDEFLABEL:
1261 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1262 return 0;
1263 #ifdef __HAVE_OLD_DISKLABEL
1264 case ODIOCGDEFLABEL:
1265 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1266 if (newlabel == NULL)
1267 return EIO;
1268 wdgetdefaultlabel(wd, newlabel);
1269 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1270 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1271 else
1272 error = ENOTTY;
1273 free(newlabel, M_TEMP);
1274 return error;
1275 #endif
1276
1277 #ifdef notyet
1278 case DIOCWFORMAT:
1279 if ((flag & FWRITE) == 0)
1280 return EBADF;
1281 {
1282 register struct format_op *fop;
1283 struct iovec aiov;
1284 struct uio auio;
1285
1286 fop = (struct format_op *)addr;
1287 aiov.iov_base = fop->df_buf;
1288 aiov.iov_len = fop->df_count;
1289 auio.uio_iov = &aiov;
1290 auio.uio_iovcnt = 1;
1291 auio.uio_resid = fop->df_count;
1292 auio.uio_segflg = 0;
1293 auio.uio_offset =
1294 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1295 auio.uio_procp = p;
1296 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1297 &auio);
1298 fop->df_count -= auio.uio_resid;
1299 fop->df_reg[0] = wdc->sc_status;
1300 fop->df_reg[1] = wdc->sc_error;
1301 return error;
1302 }
1303 #endif
1304 case DIOCGCACHE:
1305 return wd_getcache(wd, (int *)addr);
1306
1307 case DIOCSCACHE:
1308 return wd_setcache(wd, *(int *)addr);
1309
1310 case DIOCCACHESYNC:
1311 return wd_flushcache(wd, AT_WAIT);
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 wd->sc_bp = NULL;
1457 wd->sc_wdc_bio.blkno = blkno;
1458 wd->sc_wdc_bio.flags = ATA_POLL;
1459 if (wd->sc_flags & WDF_LBA48 && blkno > LBA48_THRESHOLD)
1460 wd->sc_wdc_bio.flags |= ATA_LBA48;
1461 if (wd->sc_flags & WDF_LBA)
1462 wd->sc_wdc_bio.flags |= ATA_LBA;
1463 wd->sc_wdc_bio.bcount =
1464 min(nblks, wddumpmulti) * lp->d_secsize;
1465 wd->sc_wdc_bio.databuf = va;
1466 #ifndef WD_DUMP_NOT_TRUSTED
1467 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1468 case WDC_TRY_AGAIN:
1469 panic("wddump: try again");
1470 break;
1471 case WDC_QUEUED:
1472 panic("wddump: polled command has been queued");
1473 break;
1474 case WDC_COMPLETE:
1475 break;
1476 }
1477 switch(wd->sc_wdc_bio.error) {
1478 case TIMEOUT:
1479 printf("wddump: device timed out");
1480 err = EIO;
1481 break;
1482 case ERR_DF:
1483 printf("wddump: drive fault");
1484 err = EIO;
1485 break;
1486 case ERR_DMA:
1487 printf("wddump: DMA error");
1488 err = EIO;
1489 break;
1490 case ERROR:
1491 printf("wddump: ");
1492 wdperror(wd);
1493 err = EIO;
1494 break;
1495 case NOERROR:
1496 err = 0;
1497 break;
1498 default:
1499 panic("wddump: unknown error type");
1500 }
1501 if (err != 0) {
1502 printf("\n");
1503 return err;
1504 }
1505 #else /* WD_DUMP_NOT_TRUSTED */
1506 /* Let's just talk about this first... */
1507 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1508 unit, va, cylin, head, sector);
1509 delay(500 * 1000); /* half a second */
1510 #endif
1511
1512 /* update block count */
1513 nblks -= min(nblks, wddumpmulti);
1514 blkno += min(nblks, wddumpmulti);
1515 va += min(nblks, wddumpmulti) * lp->d_secsize;
1516 }
1517
1518 wddoingadump = 0;
1519 return 0;
1520 }
1521
1522 #ifdef HAS_BAD144_HANDLING
1523 /*
1524 * Internalize the bad sector table.
1525 */
1526 void
1527 bad144intern(struct wd_softc *wd)
1528 {
1529 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1530 struct disklabel *lp = wd->sc_dk.dk_label;
1531 int i = 0;
1532
1533 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1534
1535 for (; i < NBT_BAD; i++) {
1536 if (bt->bt_bad[i].bt_cyl == 0xffff)
1537 break;
1538 wd->sc_badsect[i] =
1539 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1540 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1541 (bt->bt_bad[i].bt_trksec & 0xff);
1542 }
1543 for (; i < NBT_BAD+1; i++)
1544 wd->sc_badsect[i] = -1;
1545 }
1546 #endif
1547
1548 int
1549 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params)
1550 {
1551 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1552 case CMD_AGAIN:
1553 return 1;
1554 case CMD_ERR:
1555 /*
1556 * We `know' there's a drive here; just assume it's old.
1557 * This geometry is only used to read the MBR and print a
1558 * (false) attach message.
1559 */
1560 strncpy(params->atap_model, "ST506",
1561 sizeof params->atap_model);
1562 params->atap_config = ATA_CFG_FIXED;
1563 params->atap_cylinders = 1024;
1564 params->atap_heads = 8;
1565 params->atap_sectors = 17;
1566 params->atap_multi = 1;
1567 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1568 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1569 return 0;
1570 case CMD_OK:
1571 return 0;
1572 default:
1573 panic("wd_get_params: bad return code from ata_get_params");
1574 /* NOTREACHED */
1575 }
1576 }
1577
1578 int
1579 wd_getcache(struct wd_softc *wd, int *bitsp)
1580 {
1581 struct ataparams params;
1582
1583 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1584 return EIO;
1585 if (params.atap_cmd_set1 == 0x0000 ||
1586 params.atap_cmd_set1 == 0xffff ||
1587 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
1588 *bitsp = 0;
1589 return 0;
1590 }
1591 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
1592 if (params.atap_cmd1_en & WDC_CMD1_CACHE)
1593 *bitsp |= DKCACHE_WRITE;
1594
1595 return 0;
1596 }
1597
1598 int
1599 wd_setcache(struct wd_softc *wd, int bits)
1600 {
1601 struct ataparams params;
1602 struct wdc_command wdc_c;
1603
1604 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1605 return EIO;
1606
1607 if (params.atap_cmd_set1 == 0x0000 ||
1608 params.atap_cmd_set1 == 0xffff ||
1609 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
1610 return EOPNOTSUPP;
1611
1612 if ((bits & DKCACHE_READ) == 0 ||
1613 (bits & DKCACHE_SAVE) != 0)
1614 return EOPNOTSUPP;
1615
1616 memset(&wdc_c, 0, sizeof(struct wdc_command));
1617 wdc_c.r_command = SET_FEATURES;
1618 wdc_c.r_st_bmask = 0;
1619 wdc_c.r_st_pmask = 0;
1620 wdc_c.timeout = 30000; /* 30s timeout */
1621 wdc_c.flags = AT_WAIT;
1622 if (bits & DKCACHE_WRITE)
1623 wdc_c.r_features = WDSF_WRITE_CACHE_EN;
1624 else
1625 wdc_c.r_features = WDSF_WRITE_CACHE_DS;
1626 if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1627 printf("%s: wd_setcache command not complete\n",
1628 wd->sc_dev.dv_xname);
1629 return EIO;
1630 }
1631 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1632 printf("%s: wd_setcache command error 0x%x\n",
1633 wd->sc_dev.dv_xname, wdc_c.flags);
1634 return EIO;
1635 }
1636 if (wdc_c.flags & ERR_NODEV)
1637 return ENODEV;
1638 return 0;
1639 }
1640
1641 int
1642 wd_flushcache(struct wd_softc *wd, int flags)
1643 {
1644 struct wdc_command wdc_c;
1645
1646 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1647 return ENODEV;
1648 memset(&wdc_c, 0, sizeof(struct wdc_command));
1649 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
1650 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0)
1651 wdc_c.r_command = WDCC_FLUSHCACHE_EXT;
1652 else
1653 wdc_c.r_command = WDCC_FLUSHCACHE;
1654 wdc_c.r_st_bmask = WDCS_DRDY;
1655 wdc_c.r_st_pmask = WDCS_DRDY;
1656 wdc_c.flags = flags;
1657 wdc_c.timeout = 30000; /* 30s timeout */
1658 if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1659 printf("%s: flush cache command didn't complete\n",
1660 wd->sc_dev.dv_xname);
1661 return EIO;
1662 }
1663 if (wdc_c.flags & ERR_NODEV)
1664 return ENODEV;
1665 if (wdc_c.flags & AT_TIMEOU) {
1666 printf("%s: flush cache command timeout\n",
1667 wd->sc_dev.dv_xname);
1668 return EIO;
1669 }
1670 if (wdc_c.flags & AT_ERROR) {
1671 if (wdc_c.r_error == WDCE_ABRT) /* command not supported */
1672 return ENODEV;
1673 printf("%s: flush cache command: error 0x%x\n",
1674 wd->sc_dev.dv_xname, wdc_c.r_error);
1675 return EIO;
1676 }
1677 if (wdc_c.flags & AT_DF) {
1678 printf("%s: flush cache command: drive fault\n",
1679 wd->sc_dev.dv_xname);
1680 return EIO;
1681 }
1682 return 0;
1683 }
1684
1685 void
1686 wd_shutdown(void *arg)
1687 {
1688 struct wd_softc *wd = arg;
1689 wd_flushcache(wd, AT_POLL);
1690 }
1691
1692 /*
1693 * Allocate space for a ioctl queue structure. Mostly taken from
1694 * scsipi_ioctl.c
1695 */
1696 struct wd_ioctl *
1697 wi_get(void)
1698 {
1699 struct wd_ioctl *wi;
1700 int s;
1701
1702 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
1703 simple_lock_init(&wi->wi_bp.b_interlock);
1704 s = splbio();
1705 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1706 splx(s);
1707 return (wi);
1708 }
1709
1710 /*
1711 * Free an ioctl structure and remove it from our list
1712 */
1713
1714 void
1715 wi_free(struct wd_ioctl *wi)
1716 {
1717 int s;
1718
1719 s = splbio();
1720 LIST_REMOVE(wi, wi_list);
1721 splx(s);
1722 free(wi, M_TEMP);
1723 }
1724
1725 /*
1726 * Find a wd_ioctl structure based on the struct buf.
1727 */
1728
1729 struct wd_ioctl *
1730 wi_find(struct buf *bp)
1731 {
1732 struct wd_ioctl *wi;
1733 int s;
1734
1735 s = splbio();
1736 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1737 if (bp == &wi->wi_bp)
1738 break;
1739 splx(s);
1740 return (wi);
1741 }
1742
1743 /*
1744 * Ioctl pseudo strategy routine
1745 *
1746 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1747 * happens here is:
1748 *
1749 * - wdioctl() queues a wd_ioctl structure.
1750 *
1751 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1752 * user space I/O is required. If physio() is called, physio() eventually
1753 * calls wdioctlstrategy().
1754 *
1755 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
1756 * to perform the actual command
1757 *
1758 * The reason for the use of the pseudo strategy routine is because
1759 * when doing I/O to/from user space, physio _really_ wants to be in
1760 * the loop. We could put the entire buffer into the ioctl request
1761 * structure, but that won't scale if we want to do things like download
1762 * microcode.
1763 */
1764
1765 void
1766 wdioctlstrategy(struct buf *bp)
1767 {
1768 struct wd_ioctl *wi;
1769 struct wdc_command wdc_c;
1770 int error = 0;
1771
1772 wi = wi_find(bp);
1773 if (wi == NULL) {
1774 printf("user_strat: No ioctl\n");
1775 error = EINVAL;
1776 goto bad;
1777 }
1778
1779 memset(&wdc_c, 0, sizeof(wdc_c));
1780
1781 /*
1782 * Abort if physio broke up the transfer
1783 */
1784
1785 if (bp->b_bcount != wi->wi_atareq.datalen) {
1786 printf("physio split wd ioctl request... cannot proceed\n");
1787 error = EIO;
1788 goto bad;
1789 }
1790
1791 /*
1792 * Abort if we didn't get a buffer size that was a multiple of
1793 * our sector size (or was larger than NBBY)
1794 */
1795
1796 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1797 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1798 (1 << NBBY)) {
1799 error = EINVAL;
1800 goto bad;
1801 }
1802
1803 /*
1804 * Make sure a timeout was supplied in the ioctl request
1805 */
1806
1807 if (wi->wi_atareq.timeout == 0) {
1808 error = EINVAL;
1809 goto bad;
1810 }
1811
1812 if (wi->wi_atareq.flags & ATACMD_READ)
1813 wdc_c.flags |= AT_READ;
1814 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1815 wdc_c.flags |= AT_WRITE;
1816
1817 if (wi->wi_atareq.flags & ATACMD_READREG)
1818 wdc_c.flags |= AT_READREG;
1819
1820 wdc_c.flags |= AT_WAIT;
1821
1822 wdc_c.timeout = wi->wi_atareq.timeout;
1823 wdc_c.r_command = wi->wi_atareq.command;
1824 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1825 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1826 wdc_c.r_sector = wi->wi_atareq.sec_num;
1827 wdc_c.r_count = wi->wi_atareq.sec_count;
1828 wdc_c.r_features = wi->wi_atareq.features;
1829 wdc_c.r_st_bmask = WDCS_DRDY;
1830 wdc_c.r_st_pmask = WDCS_DRDY;
1831 wdc_c.data = wi->wi_bp.b_data;
1832 wdc_c.bcount = wi->wi_bp.b_bcount;
1833
1834 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &wdc_c)
1835 != WDC_COMPLETE) {
1836 wi->wi_atareq.retsts = ATACMD_ERROR;
1837 goto bad;
1838 }
1839
1840 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1841 if (wdc_c.flags & AT_ERROR) {
1842 wi->wi_atareq.retsts = ATACMD_ERROR;
1843 wi->wi_atareq.error = wdc_c.r_error;
1844 } else if (wdc_c.flags & AT_DF)
1845 wi->wi_atareq.retsts = ATACMD_DF;
1846 else
1847 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1848 } else {
1849 wi->wi_atareq.retsts = ATACMD_OK;
1850 if (wi->wi_atareq.flags & ATACMD_READREG) {
1851 wi->wi_atareq.head = wdc_c.r_head ;
1852 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1853 wi->wi_atareq.sec_num = wdc_c.r_sector;
1854 wi->wi_atareq.sec_count = wdc_c.r_count;
1855 wi->wi_atareq.features = wdc_c.r_features;
1856 wi->wi_atareq.error = wdc_c.r_error;
1857 }
1858 }
1859
1860 bp->b_error = 0;
1861 biodone(bp);
1862 return;
1863 bad:
1864 bp->b_flags |= B_ERROR;
1865 bp->b_error = error;
1866 biodone(bp);
1867 }
1868