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