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