wd.c revision 1.245 1 /* $NetBSD: wd.c,v 1.245 2003/04/16 00:07:17 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.245 2003/04/16 00:07:17 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 bp->b_error = EIO;
553 goto bad;
554 }
555 }
556
557 /* Queue transfer on drive, activate drive and controller if idle. */
558 s = splbio();
559 BUFQ_PUT(&wd->sc_q, bp);
560 wdstart(wd);
561 splx(s);
562 return;
563 bad:
564 bp->b_flags |= B_ERROR;
565 done:
566 /* Toss transfer; we're done early. */
567 bp->b_resid = bp->b_bcount;
568 biodone(bp);
569 }
570
571 /*
572 * Queue a drive for I/O.
573 */
574 void
575 wdstart(arg)
576 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(wd, bp)
645 struct wd_softc *wd;
646 struct buf *bp;
647 {
648
649 /*
650 * Deal with the "split mod15 write" quirk. We just divide the
651 * transfer in two, doing the first half and then then second half
652 * with the same command opening.
653 *
654 * Note we MUST do this here, because we can't let insertion
655 * into the bufq cause the transfers to be re-merged.
656 */
657 if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 &&
658 (bp->b_flags & B_READ) == 0 &&
659 bp->b_bcount > 512 &&
660 ((bp->b_bcount / 512) % 15) == 1)) {
661 struct buf *nbp;
662
663 /* already at splbio */
664 nbp = pool_get(&bufpool, PR_NOWAIT);
665 if (__predict_false(nbp == NULL)) {
666 /* No memory -- fail the iop. */
667 bp->b_error = ENOMEM;
668 bp->b_flags |= B_ERROR;
669 bp->b_resid = bp->b_bcount;
670 biodone(bp);
671 wd->openings++;
672 return;
673 }
674
675 BUF_INIT(nbp);
676 nbp->b_error = 0;
677 nbp->b_proc = bp->b_proc;
678 nbp->b_vp = NULLVP;
679 nbp->b_dev = bp->b_dev;
680
681 nbp->b_bcount = bp->b_bcount / 2;
682 nbp->b_bufsize = bp->b_bcount / 2;
683 nbp->b_data = bp->b_data;
684
685 nbp->b_blkno = bp->b_blkno;
686 nbp->b_rawblkno = bp->b_rawblkno;
687
688 nbp->b_flags = bp->b_flags | B_CALL;
689 nbp->b_iodone = wd_split_mod15_write;
690
691 /* Put ptr to orig buf in b_private and use new buf */
692 nbp->b_private = bp;
693 bp = nbp;
694 }
695
696 wd->sc_wdc_bio.blkno = bp->b_rawblkno;
697 wd->sc_wdc_bio.blkdone =0;
698 wd->sc_bp = bp;
699 /*
700 * If we're retrying, retry in single-sector mode. This will give us
701 * the sector number of the problem, and will eventually allow the
702 * transfer to succeed.
703 */
704 if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
705 wd->sc_wdc_bio.flags = ATA_SINGLE;
706 else
707 wd->sc_wdc_bio.flags = 0;
708 if (wd->sc_flags & WDF_LBA48)
709 wd->sc_wdc_bio.flags |= ATA_LBA48;
710 if (wd->sc_flags & WDF_LBA)
711 wd->sc_wdc_bio.flags |= ATA_LBA;
712 if (bp->b_flags & B_READ)
713 wd->sc_wdc_bio.flags |= ATA_READ;
714 wd->sc_wdc_bio.bcount = bp->b_bcount;
715 wd->sc_wdc_bio.databuf = bp->b_data;
716 /* Instrumentation. */
717 disk_busy(&wd->sc_dk);
718 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
719 case WDC_TRY_AGAIN:
720 callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
721 break;
722 case WDC_QUEUED:
723 case WDC_COMPLETE:
724 break;
725 default:
726 panic("__wdstart: bad return code from ata_bio()");
727 }
728 }
729
730 void
731 wddone(v)
732 void *v;
733 {
734 struct wd_softc *wd = v;
735 struct buf *bp = wd->sc_bp;
736 const char *errmsg;
737 int do_perror = 0;
738 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
739 DEBUG_XFERS);
740
741 if (bp == NULL)
742 return;
743 bp->b_resid = wd->sc_wdc_bio.bcount;
744 switch (wd->sc_wdc_bio.error) {
745 case ERR_DMA:
746 errmsg = "DMA error";
747 goto retry;
748 case ERR_DF:
749 errmsg = "device fault";
750 goto retry;
751 case TIMEOUT:
752 errmsg = "device timeout";
753 goto retry;
754 case ERROR:
755 /* Don't care about media change bits */
756 if (wd->sc_wdc_bio.r_error != 0 &&
757 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
758 goto noerror;
759 errmsg = "error";
760 do_perror = 1;
761 retry: /* Just reset and retry. Can we do more ? */
762 wd->atabus->ata_reset_channel(wd->drvp);
763 diskerr(bp, "wd", errmsg, LOG_PRINTF,
764 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
765 if (wd->retries < WDIORETRIES)
766 printf(", retrying\n");
767 if (do_perror)
768 wdperror(wd);
769 if (wd->retries < WDIORETRIES) {
770 wd->retries++;
771 callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
772 wdrestart, wd);
773 return;
774 }
775 printf("\n");
776
777 /*
778 * Not all errors indicate a failed block but those that do,
779 * put the block on the bad-block list for the device. Only
780 * do this for reads because the drive should do it for writes,
781 * itself, according to Manuel.
782 */
783 if ((bp->b_flags & B_READ) &&
784 ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) ||
785 (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) {
786 struct disk_badsectors *dbs;
787
788 dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK);
789 dbs->dbs_min = bp->b_rawblkno;
790 dbs->dbs_max = dbs->dbs_min + bp->b_bcount - 1;
791 microtime(&dbs->dbs_failedat);
792 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
793 wd->sc_bscount++;
794 }
795
796 bp->b_flags |= B_ERROR;
797 bp->b_error = EIO;
798 break;
799 case NOERROR:
800 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
801 printf("%s: soft error (corrected)\n",
802 wd->sc_dev.dv_xname);
803 break;
804 case ERR_NODEV:
805 bp->b_flags |= B_ERROR;
806 bp->b_error = EIO;
807 break;
808 }
809 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
810 (bp->b_flags & B_READ));
811 #if NRND > 0
812 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
813 #endif
814 /* XXX Yuck, but we don't want to increment openings in this case */
815 if (__predict_false((bp->b_flags & B_CALL) != 0 &&
816 bp->b_iodone == wd_split_mod15_write))
817 biodone(bp);
818 else {
819 biodone(bp);
820 wd->openings++;
821 }
822 wdstart(wd);
823 }
824
825 void
826 wdrestart(v)
827 void *v;
828 {
829 struct wd_softc *wd = v;
830 struct buf *bp = wd->sc_bp;
831 int s;
832 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
833 DEBUG_XFERS);
834
835 s = splbio();
836 __wdstart(v, bp);
837 splx(s);
838 }
839
840 int
841 wdread(dev, uio, flags)
842 dev_t dev;
843 struct uio *uio;
844 int flags;
845 {
846
847 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
848 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
849 }
850
851 int
852 wdwrite(dev, uio, flags)
853 dev_t dev;
854 struct uio *uio;
855 int flags;
856 {
857
858 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
859 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
860 }
861
862 /*
863 * Wait interruptibly for an exclusive lock.
864 *
865 * XXX
866 * Several drivers do this; it should be abstracted and made MP-safe.
867 */
868 int
869 wdlock(wd)
870 struct wd_softc *wd;
871 {
872 int error;
873 int s;
874
875 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
876
877 s = splbio();
878
879 while ((wd->sc_flags & WDF_LOCKED) != 0) {
880 wd->sc_flags |= WDF_WANTED;
881 if ((error = tsleep(wd, PRIBIO | PCATCH,
882 "wdlck", 0)) != 0) {
883 splx(s);
884 return error;
885 }
886 }
887 wd->sc_flags |= WDF_LOCKED;
888 splx(s);
889 return 0;
890 }
891
892 /*
893 * Unlock and wake up any waiters.
894 */
895 void
896 wdunlock(wd)
897 struct wd_softc *wd;
898 {
899
900 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
901
902 wd->sc_flags &= ~WDF_LOCKED;
903 if ((wd->sc_flags & WDF_WANTED) != 0) {
904 wd->sc_flags &= ~WDF_WANTED;
905 wakeup(wd);
906 }
907 }
908
909 int
910 wdopen(dev, flag, fmt, p)
911 dev_t dev;
912 int flag, fmt;
913 struct proc *p;
914 {
915 struct wd_softc *wd;
916 int part, error;
917
918 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
919 wd = device_lookup(&wd_cd, WDUNIT(dev));
920 if (wd == NULL)
921 return (ENXIO);
922
923 /*
924 * If this is the first open of this device, add a reference
925 * to the adapter.
926 */
927 if (wd->sc_dk.dk_openmask == 0 &&
928 (error = wd->atabus->ata_addref(wd->drvp)) != 0)
929 return (error);
930
931 if ((error = wdlock(wd)) != 0)
932 goto bad4;
933
934 if (wd->sc_dk.dk_openmask != 0) {
935 /*
936 * If any partition is open, but the disk has been invalidated,
937 * disallow further opens.
938 */
939 if ((wd->sc_flags & WDF_LOADED) == 0) {
940 error = EIO;
941 goto bad3;
942 }
943 } else {
944 if ((wd->sc_flags & WDF_LOADED) == 0) {
945 wd->sc_flags |= WDF_LOADED;
946
947 /* Load the physical device parameters. */
948 wd_get_params(wd, AT_WAIT, &wd->sc_params);
949
950 /* Load the partition info if not already loaded. */
951 wdgetdisklabel(wd);
952 }
953 }
954
955 part = WDPART(dev);
956
957 /* Check that the partition exists. */
958 if (part != RAW_PART &&
959 (part >= wd->sc_dk.dk_label->d_npartitions ||
960 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
961 error = ENXIO;
962 goto bad;
963 }
964
965 /* Insure only one open at a time. */
966 switch (fmt) {
967 case S_IFCHR:
968 wd->sc_dk.dk_copenmask |= (1 << part);
969 break;
970 case S_IFBLK:
971 wd->sc_dk.dk_bopenmask |= (1 << part);
972 break;
973 }
974 wd->sc_dk.dk_openmask =
975 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
976
977 wdunlock(wd);
978 return 0;
979
980 bad:
981 if (wd->sc_dk.dk_openmask == 0) {
982 }
983
984 bad3:
985 wdunlock(wd);
986 bad4:
987 if (wd->sc_dk.dk_openmask == 0)
988 wd->atabus->ata_delref(wd->drvp);
989 return error;
990 }
991
992 int
993 wdclose(dev, flag, fmt, p)
994 dev_t dev;
995 int flag, fmt;
996 struct proc *p;
997 {
998 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
999 int part = WDPART(dev);
1000 int error;
1001
1002 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
1003 if ((error = wdlock(wd)) != 0)
1004 return error;
1005
1006 switch (fmt) {
1007 case S_IFCHR:
1008 wd->sc_dk.dk_copenmask &= ~(1 << part);
1009 break;
1010 case S_IFBLK:
1011 wd->sc_dk.dk_bopenmask &= ~(1 << part);
1012 break;
1013 }
1014 wd->sc_dk.dk_openmask =
1015 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1016
1017 if (wd->sc_dk.dk_openmask == 0) {
1018 wd_flushcache(wd, AT_WAIT);
1019 /* XXXX Must wait for I/O to complete! */
1020
1021 if (! (wd->sc_flags & WDF_KLABEL))
1022 wd->sc_flags &= ~WDF_LOADED;
1023
1024 wd->atabus->ata_delref(wd->drvp);
1025 }
1026
1027 wdunlock(wd);
1028 return 0;
1029 }
1030
1031 void
1032 wdgetdefaultlabel(wd, lp)
1033 struct wd_softc *wd;
1034 struct disklabel *lp;
1035 {
1036
1037 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
1038 memset(lp, 0, sizeof(struct disklabel));
1039
1040 lp->d_secsize = DEV_BSIZE;
1041 lp->d_ntracks = wd->sc_params.atap_heads;
1042 lp->d_nsectors = wd->sc_params.atap_sectors;
1043 lp->d_ncylinders = wd->sc_params.atap_cylinders;
1044 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1045
1046 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
1047 lp->d_type = DTYPE_ST506;
1048 else
1049 lp->d_type = DTYPE_ESDI;
1050
1051 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
1052 strncpy(lp->d_packname, "fictitious", 16);
1053 if (wd->sc_capacity > UINT32_MAX)
1054 lp->d_secperunit = UINT32_MAX;
1055 else
1056 lp->d_secperunit = wd->sc_capacity;
1057 lp->d_rpm = 3600;
1058 lp->d_interleave = 1;
1059 lp->d_flags = 0;
1060
1061 lp->d_partitions[RAW_PART].p_offset = 0;
1062 lp->d_partitions[RAW_PART].p_size =
1063 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1064 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1065 lp->d_npartitions = RAW_PART + 1;
1066
1067 lp->d_magic = DISKMAGIC;
1068 lp->d_magic2 = DISKMAGIC;
1069 lp->d_checksum = dkcksum(lp);
1070 }
1071
1072 /*
1073 * Fabricate a default disk label, and try to read the correct one.
1074 */
1075 void
1076 wdgetdisklabel(wd)
1077 struct wd_softc *wd;
1078 {
1079 struct disklabel *lp = wd->sc_dk.dk_label;
1080 char *errstring;
1081
1082 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1083
1084 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1085
1086 wdgetdefaultlabel(wd, lp);
1087
1088 wd->sc_badsect[0] = -1;
1089
1090 if (wd->drvp->state > RECAL)
1091 wd->drvp->drive_flags |= DRIVE_RESET;
1092 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
1093 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1094 if (errstring) {
1095 /*
1096 * This probably happened because the drive's default
1097 * geometry doesn't match the DOS geometry. We
1098 * assume the DOS geometry is now in the label and try
1099 * again. XXX This is a kluge.
1100 */
1101 if (wd->drvp->state > RECAL)
1102 wd->drvp->drive_flags |= DRIVE_RESET;
1103 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
1104 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1105 }
1106 if (errstring) {
1107 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
1108 return;
1109 }
1110
1111 if (wd->drvp->state > RECAL)
1112 wd->drvp->drive_flags |= DRIVE_RESET;
1113 #ifdef HAS_BAD144_HANDLING
1114 if ((lp->d_flags & D_BADSECT) != 0)
1115 bad144intern(wd);
1116 #endif
1117 }
1118
1119 void
1120 wdperror(wd)
1121 const struct wd_softc *wd;
1122 {
1123 static const char *const errstr0_3[] = {"address mark not found",
1124 "track 0 not found", "aborted command", "media change requested",
1125 "id not found", "media changed", "uncorrectable data error",
1126 "bad block detected"};
1127 static const char *const errstr4_5[] = {
1128 "obsolete (address mark not found)",
1129 "no media/write protected", "aborted command",
1130 "media change requested", "id not found", "media changed",
1131 "uncorrectable data error", "interface CRC error"};
1132 const char *const *errstr;
1133 int i;
1134 char *sep = "";
1135
1136 const char *devname = wd->sc_dev.dv_xname;
1137 struct ata_drive_datas *drvp = wd->drvp;
1138 int errno = wd->sc_wdc_bio.r_error;
1139
1140 if (drvp->ata_vers >= 4)
1141 errstr = errstr4_5;
1142 else
1143 errstr = errstr0_3;
1144
1145 printf("%s: (", devname);
1146
1147 if (errno == 0)
1148 printf("error not notified");
1149
1150 for (i = 0; i < 8; i++) {
1151 if (errno & (1 << i)) {
1152 printf("%s%s", sep, errstr[i]);
1153 sep = ", ";
1154 }
1155 }
1156 printf(")\n");
1157 }
1158
1159 int
1160 wdioctl(dev, xfer, addr, flag, p)
1161 dev_t dev;
1162 u_long xfer;
1163 caddr_t addr;
1164 int flag;
1165 struct proc *p;
1166 {
1167 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
1168 int error = 0;
1169 #ifdef __HAVE_OLD_DISKLABEL
1170 struct disklabel *newlabel = NULL;
1171 #endif
1172
1173 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1174
1175 if ((wd->sc_flags & WDF_LOADED) == 0)
1176 return EIO;
1177
1178 switch (xfer) {
1179 #ifdef HAS_BAD144_HANDLING
1180 case DIOCSBAD:
1181 if ((flag & FWRITE) == 0)
1182 return EBADF;
1183 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1184 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1185 bad144intern(wd);
1186 return 0;
1187 #endif
1188
1189 case DIOCBSLIST :
1190 {
1191 u_int32_t count, missing, skip;
1192 struct disk_badsecinfo dbsi;
1193 struct disk_badsectors *dbs;
1194 size_t available;
1195 caddr_t laddr;
1196
1197 dbsi = *(struct disk_badsecinfo *)addr;
1198 missing = wd->sc_bscount;
1199 count = 0;
1200 available = dbsi.dbsi_bufsize;
1201 skip = dbsi.dbsi_skip;
1202 laddr = dbsi.dbsi_buffer;
1203
1204 /*
1205 * We start this loop with the expectation that all of the
1206 * entries will be missed and decrement this counter each
1207 * time we either skip over one (already copied out) or
1208 * we actually copy it back to user space. The structs
1209 * holding the bad sector information are copied directly
1210 * back to user space whilst the summary is returned via
1211 * the struct passed in via the ioctl.
1212 */
1213 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1214 if (skip > 0) {
1215 missing--;
1216 skip--;
1217 continue;
1218 }
1219 if (available < sizeof(*dbs))
1220 break;
1221 available -= sizeof(*dbs);
1222 copyout(dbs, laddr, sizeof(*dbs));
1223 laddr += sizeof(*dbs);
1224 missing--;
1225 count++;
1226 }
1227 dbsi.dbsi_left = missing;
1228 dbsi.dbsi_copied = count;
1229 *(struct disk_badsecinfo *)addr = dbsi;
1230 return 0;
1231 }
1232
1233 case DIOCBSFLUSH :
1234 /* Clean out the bad sector list */
1235 while (!SLIST_EMPTY(&wd->sc_bslist)) {
1236 void *head = SLIST_FIRST(&wd->sc_bslist);
1237 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1238 free(head, M_TEMP);
1239 }
1240 wd->sc_bscount = 0;
1241 return 0;
1242
1243 case DIOCGDINFO:
1244 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1245 return 0;
1246 #ifdef __HAVE_OLD_DISKLABEL
1247 case ODIOCGDINFO:
1248 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1249 if (newlabel == NULL)
1250 return EIO;
1251 *newlabel = *(wd->sc_dk.dk_label);
1252 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1253 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1254 else
1255 error = ENOTTY;
1256 free(newlabel, M_TEMP);
1257 return error;
1258 #endif
1259
1260 case DIOCGPART:
1261 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1262 ((struct partinfo *)addr)->part =
1263 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1264 return 0;
1265
1266 case DIOCWDINFO:
1267 case DIOCSDINFO:
1268 #ifdef __HAVE_OLD_DISKLABEL
1269 case ODIOCWDINFO:
1270 case ODIOCSDINFO:
1271 #endif
1272 {
1273 struct disklabel *lp;
1274
1275 if ((flag & FWRITE) == 0)
1276 return EBADF;
1277
1278 #ifdef __HAVE_OLD_DISKLABEL
1279 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1280 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1281 if (newlabel == NULL)
1282 return EIO;
1283 memset(newlabel, 0, sizeof newlabel);
1284 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1285 lp = newlabel;
1286 } else
1287 #endif
1288 lp = (struct disklabel *)addr;
1289
1290 if ((error = wdlock(wd)) != 0)
1291 goto bad;
1292 wd->sc_flags |= WDF_LABELLING;
1293
1294 error = setdisklabel(wd->sc_dk.dk_label,
1295 lp, /*wd->sc_dk.dk_openmask : */0,
1296 wd->sc_dk.dk_cpulabel);
1297 if (error == 0) {
1298 if (wd->drvp->state > RECAL)
1299 wd->drvp->drive_flags |= DRIVE_RESET;
1300 if (xfer == DIOCWDINFO
1301 #ifdef __HAVE_OLD_DISKLABEL
1302 || xfer == ODIOCWDINFO
1303 #endif
1304 )
1305 error = writedisklabel(WDLABELDEV(dev),
1306 wdstrategy, wd->sc_dk.dk_label,
1307 wd->sc_dk.dk_cpulabel);
1308 }
1309
1310 wd->sc_flags &= ~WDF_LABELLING;
1311 wdunlock(wd);
1312 bad:
1313 #ifdef __HAVE_OLD_DISKLABEL
1314 if (newlabel != NULL)
1315 free(newlabel, M_TEMP);
1316 #endif
1317 return error;
1318 }
1319
1320 case DIOCKLABEL:
1321 if (*(int *)addr)
1322 wd->sc_flags |= WDF_KLABEL;
1323 else
1324 wd->sc_flags &= ~WDF_KLABEL;
1325 return 0;
1326
1327 case DIOCWLABEL:
1328 if ((flag & FWRITE) == 0)
1329 return EBADF;
1330 if (*(int *)addr)
1331 wd->sc_flags |= WDF_WLABEL;
1332 else
1333 wd->sc_flags &= ~WDF_WLABEL;
1334 return 0;
1335
1336 case DIOCGDEFLABEL:
1337 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1338 return 0;
1339 #ifdef __HAVE_OLD_DISKLABEL
1340 case ODIOCGDEFLABEL:
1341 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1342 if (newlabel == NULL)
1343 return EIO;
1344 wdgetdefaultlabel(wd, newlabel);
1345 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1346 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1347 else
1348 error = ENOTTY;
1349 free(newlabel, M_TEMP);
1350 return error;
1351 #endif
1352
1353 #ifdef notyet
1354 case DIOCWFORMAT:
1355 if ((flag & FWRITE) == 0)
1356 return EBADF;
1357 {
1358 register struct format_op *fop;
1359 struct iovec aiov;
1360 struct uio auio;
1361
1362 fop = (struct format_op *)addr;
1363 aiov.iov_base = fop->df_buf;
1364 aiov.iov_len = fop->df_count;
1365 auio.uio_iov = &aiov;
1366 auio.uio_iovcnt = 1;
1367 auio.uio_resid = fop->df_count;
1368 auio.uio_segflg = 0;
1369 auio.uio_offset =
1370 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1371 auio.uio_procp = p;
1372 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1373 &auio);
1374 fop->df_count -= auio.uio_resid;
1375 fop->df_reg[0] = wdc->sc_status;
1376 fop->df_reg[1] = wdc->sc_error;
1377 return error;
1378 }
1379 #endif
1380
1381 case ATAIOCCOMMAND:
1382 /*
1383 * Make sure this command is (relatively) safe first
1384 */
1385 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1386 (flag & FWRITE) == 0)
1387 return (EBADF);
1388 {
1389 struct wd_ioctl *wi;
1390 atareq_t *atareq = (atareq_t *) addr;
1391 int error;
1392
1393 wi = wi_get();
1394 wi->wi_softc = wd;
1395 wi->wi_atareq = *atareq;
1396
1397 if (atareq->datalen && atareq->flags &
1398 (ATACMD_READ | ATACMD_WRITE)) {
1399 wi->wi_iov.iov_base = atareq->databuf;
1400 wi->wi_iov.iov_len = atareq->datalen;
1401 wi->wi_uio.uio_iov = &wi->wi_iov;
1402 wi->wi_uio.uio_iovcnt = 1;
1403 wi->wi_uio.uio_resid = atareq->datalen;
1404 wi->wi_uio.uio_offset = 0;
1405 wi->wi_uio.uio_segflg = UIO_USERSPACE;
1406 wi->wi_uio.uio_rw =
1407 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1408 wi->wi_uio.uio_procp = p;
1409 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
1410 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1411 minphys, &wi->wi_uio);
1412 } else {
1413 /* No need to call physio if we don't have any
1414 user data */
1415 wi->wi_bp.b_flags = 0;
1416 wi->wi_bp.b_data = 0;
1417 wi->wi_bp.b_bcount = 0;
1418 wi->wi_bp.b_dev = 0;
1419 wi->wi_bp.b_proc = p;
1420 wdioctlstrategy(&wi->wi_bp);
1421 error = wi->wi_bp.b_error;
1422 }
1423 *atareq = wi->wi_atareq;
1424 wi_free(wi);
1425 return(error);
1426 }
1427
1428 default:
1429 return ENOTTY;
1430 }
1431
1432 #ifdef DIAGNOSTIC
1433 panic("wdioctl: impossible");
1434 #endif
1435 }
1436
1437 #ifdef B_FORMAT
1438 int
1439 wdformat(struct buf *bp)
1440 {
1441
1442 bp->b_flags |= B_FORMAT;
1443 return wdstrategy(bp);
1444 }
1445 #endif
1446
1447 int
1448 wdsize(dev)
1449 dev_t dev;
1450 {
1451 struct wd_softc *wd;
1452 int part, omask;
1453 int size;
1454
1455 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1456
1457 wd = device_lookup(&wd_cd, WDUNIT(dev));
1458 if (wd == NULL)
1459 return (-1);
1460
1461 part = WDPART(dev);
1462 omask = wd->sc_dk.dk_openmask & (1 << part);
1463
1464 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1465 return (-1);
1466 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1467 size = -1;
1468 else
1469 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1470 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1471 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1472 return (-1);
1473 return (size);
1474 }
1475
1476 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1477 static int wddoingadump = 0;
1478 static int wddumprecalibrated = 0;
1479 static int wddumpmulti = 1;
1480
1481 /*
1482 * Dump core after a system crash.
1483 */
1484 int
1485 wddump(dev, blkno, va, size)
1486 dev_t dev;
1487 daddr_t blkno;
1488 caddr_t va;
1489 size_t size;
1490 {
1491 struct wd_softc *wd; /* disk unit to do the I/O */
1492 struct disklabel *lp; /* disk's disklabel */
1493 int part, err;
1494 int nblks; /* total number of sectors left to write */
1495
1496 /* Check if recursive dump; if so, punt. */
1497 if (wddoingadump)
1498 return EFAULT;
1499 wddoingadump = 1;
1500
1501 wd = device_lookup(&wd_cd, WDUNIT(dev));
1502 if (wd == NULL)
1503 return (ENXIO);
1504
1505 part = WDPART(dev);
1506
1507 /* Convert to disk sectors. Request must be a multiple of size. */
1508 lp = wd->sc_dk.dk_label;
1509 if ((size % lp->d_secsize) != 0)
1510 return EFAULT;
1511 nblks = size / lp->d_secsize;
1512 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1513
1514 /* Check transfer bounds against partition size. */
1515 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1516 return EINVAL;
1517
1518 /* Offset block number to start of partition. */
1519 blkno += lp->d_partitions[part].p_offset;
1520
1521 /* Recalibrate, if first dump transfer. */
1522 if (wddumprecalibrated == 0) {
1523 wddumpmulti = wd->sc_multi;
1524 wddumprecalibrated = 1;
1525 wd->drvp->state = RESET;
1526 }
1527
1528 while (nblks > 0) {
1529 again:
1530 wd->sc_bp = NULL;
1531 wd->sc_wdc_bio.blkno = blkno;
1532 wd->sc_wdc_bio.flags = ATA_POLL;
1533 if (wddumpmulti == 1)
1534 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1535 if (wd->sc_flags & WDF_LBA48)
1536 wd->sc_wdc_bio.flags |= ATA_LBA48;
1537 if (wd->sc_flags & WDF_LBA)
1538 wd->sc_wdc_bio.flags |= ATA_LBA;
1539 wd->sc_wdc_bio.bcount =
1540 min(nblks, wddumpmulti) * lp->d_secsize;
1541 wd->sc_wdc_bio.databuf = va;
1542 #ifndef WD_DUMP_NOT_TRUSTED
1543 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1544 case WDC_TRY_AGAIN:
1545 panic("wddump: try again");
1546 break;
1547 case WDC_QUEUED:
1548 panic("wddump: polled command has been queued");
1549 break;
1550 case WDC_COMPLETE:
1551 break;
1552 }
1553 switch(wd->sc_wdc_bio.error) {
1554 case TIMEOUT:
1555 printf("wddump: device timed out");
1556 err = EIO;
1557 break;
1558 case ERR_DF:
1559 printf("wddump: drive fault");
1560 err = EIO;
1561 break;
1562 case ERR_DMA:
1563 printf("wddump: DMA error");
1564 err = EIO;
1565 break;
1566 case ERROR:
1567 printf("wddump: ");
1568 wdperror(wd);
1569 err = EIO;
1570 break;
1571 case NOERROR:
1572 err = 0;
1573 break;
1574 default:
1575 panic("wddump: unknown error type");
1576 }
1577 if (err != 0) {
1578 if (wddumpmulti != 1) {
1579 wddumpmulti = 1; /* retry in single-sector */
1580 printf(", retrying\n");
1581 goto again;
1582 }
1583 printf("\n");
1584 return err;
1585 }
1586 #else /* WD_DUMP_NOT_TRUSTED */
1587 /* Let's just talk about this first... */
1588 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1589 unit, va, cylin, head, sector);
1590 delay(500 * 1000); /* half a second */
1591 #endif
1592
1593 /* update block count */
1594 nblks -= min(nblks, wddumpmulti);
1595 blkno += min(nblks, wddumpmulti);
1596 va += min(nblks, wddumpmulti) * lp->d_secsize;
1597 }
1598
1599 wddoingadump = 0;
1600 return 0;
1601 }
1602
1603 #ifdef HAS_BAD144_HANDLING
1604 /*
1605 * Internalize the bad sector table.
1606 */
1607 void
1608 bad144intern(wd)
1609 struct wd_softc *wd;
1610 {
1611 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1612 struct disklabel *lp = wd->sc_dk.dk_label;
1613 int i = 0;
1614
1615 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1616
1617 for (; i < NBT_BAD; i++) {
1618 if (bt->bt_bad[i].bt_cyl == 0xffff)
1619 break;
1620 wd->sc_badsect[i] =
1621 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1622 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1623 (bt->bt_bad[i].bt_trksec & 0xff);
1624 }
1625 for (; i < NBT_BAD+1; i++)
1626 wd->sc_badsect[i] = -1;
1627 }
1628 #endif
1629
1630 int
1631 wd_get_params(wd, flags, params)
1632 struct wd_softc *wd;
1633 u_int8_t flags;
1634 struct ataparams *params;
1635 {
1636 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1637 case CMD_AGAIN:
1638 return 1;
1639 case CMD_ERR:
1640 /*
1641 * We `know' there's a drive here; just assume it's old.
1642 * This geometry is only used to read the MBR and print a
1643 * (false) attach message.
1644 */
1645 strncpy(params->atap_model, "ST506",
1646 sizeof params->atap_model);
1647 params->atap_config = ATA_CFG_FIXED;
1648 params->atap_cylinders = 1024;
1649 params->atap_heads = 8;
1650 params->atap_sectors = 17;
1651 params->atap_multi = 1;
1652 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1653 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1654 return 0;
1655 case CMD_OK:
1656 return 0;
1657 default:
1658 panic("wd_get_params: bad return code from ata_get_params");
1659 /* NOTREACHED */
1660 }
1661 }
1662
1663 void
1664 wd_flushcache(wd, flags)
1665 struct wd_softc *wd;
1666 int flags;
1667 {
1668 struct wdc_command wdc_c;
1669
1670 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1671 return;
1672 memset(&wdc_c, 0, sizeof(struct wdc_command));
1673 wdc_c.r_command = WDCC_FLUSHCACHE;
1674 wdc_c.r_st_bmask = WDCS_DRDY;
1675 wdc_c.r_st_pmask = WDCS_DRDY;
1676 wdc_c.flags = flags;
1677 wdc_c.timeout = 30000; /* 30s timeout */
1678 if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1679 printf("%s: flush cache command didn't complete\n",
1680 wd->sc_dev.dv_xname);
1681 }
1682 if (wdc_c.flags & AT_TIMEOU) {
1683 printf("%s: flush cache command timeout\n",
1684 wd->sc_dev.dv_xname);
1685 }
1686 if (wdc_c.flags & AT_DF) {
1687 printf("%s: flush cache command: drive fault\n",
1688 wd->sc_dev.dv_xname);
1689 }
1690 /*
1691 * Ignore error register, it shouldn't report anything else
1692 * than COMMAND ABORTED, which means the device doesn't support
1693 * flush cache
1694 */
1695 }
1696
1697 void
1698 wd_shutdown(arg)
1699 void *arg;
1700 {
1701 struct wd_softc *wd = arg;
1702 wd_flushcache(wd, AT_POLL);
1703 }
1704
1705 /*
1706 * Allocate space for a ioctl queue structure. Mostly taken from
1707 * scsipi_ioctl.c
1708 */
1709 struct wd_ioctl *
1710 wi_get()
1711 {
1712 struct wd_ioctl *wi;
1713 int s;
1714
1715 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
1716 simple_lock_init(&wi->wi_bp.b_interlock);
1717 s = splbio();
1718 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1719 splx(s);
1720 return (wi);
1721 }
1722
1723 /*
1724 * Free an ioctl structure and remove it from our list
1725 */
1726
1727 void
1728 wi_free(wi)
1729 struct wd_ioctl *wi;
1730 {
1731 int s;
1732
1733 s = splbio();
1734 LIST_REMOVE(wi, wi_list);
1735 splx(s);
1736 free(wi, M_TEMP);
1737 }
1738
1739 /*
1740 * Find a wd_ioctl structure based on the struct buf.
1741 */
1742
1743 struct wd_ioctl *
1744 wi_find(bp)
1745 struct buf *bp;
1746 {
1747 struct wd_ioctl *wi;
1748 int s;
1749
1750 s = splbio();
1751 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1752 if (bp == &wi->wi_bp)
1753 break;
1754 splx(s);
1755 return (wi);
1756 }
1757
1758 /*
1759 * Ioctl pseudo strategy routine
1760 *
1761 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1762 * happens here is:
1763 *
1764 * - wdioctl() queues a wd_ioctl structure.
1765 *
1766 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1767 * user space I/O is required. If physio() is called, physio() eventually
1768 * calls wdioctlstrategy().
1769 *
1770 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
1771 * to perform the actual command
1772 *
1773 * The reason for the use of the pseudo strategy routine is because
1774 * when doing I/O to/from user space, physio _really_ wants to be in
1775 * the loop. We could put the entire buffer into the ioctl request
1776 * structure, but that won't scale if we want to do things like download
1777 * microcode.
1778 */
1779
1780 void
1781 wdioctlstrategy(bp)
1782 struct buf *bp;
1783 {
1784 struct wd_ioctl *wi;
1785 struct wdc_command wdc_c;
1786 int error = 0;
1787
1788 wi = wi_find(bp);
1789 if (wi == NULL) {
1790 printf("user_strat: No ioctl\n");
1791 error = EINVAL;
1792 goto bad;
1793 }
1794
1795 memset(&wdc_c, 0, sizeof(wdc_c));
1796
1797 /*
1798 * Abort if physio broke up the transfer
1799 */
1800
1801 if (bp->b_bcount != wi->wi_atareq.datalen) {
1802 printf("physio split wd ioctl request... cannot proceed\n");
1803 error = EIO;
1804 goto bad;
1805 }
1806
1807 /*
1808 * Abort if we didn't get a buffer size that was a multiple of
1809 * our sector size (or was larger than NBBY)
1810 */
1811
1812 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1813 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1814 (1 << NBBY)) {
1815 error = EINVAL;
1816 goto bad;
1817 }
1818
1819 /*
1820 * Make sure a timeout was supplied in the ioctl request
1821 */
1822
1823 if (wi->wi_atareq.timeout == 0) {
1824 error = EINVAL;
1825 goto bad;
1826 }
1827
1828 if (wi->wi_atareq.flags & ATACMD_READ)
1829 wdc_c.flags |= AT_READ;
1830 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1831 wdc_c.flags |= AT_WRITE;
1832
1833 if (wi->wi_atareq.flags & ATACMD_READREG)
1834 wdc_c.flags |= AT_READREG;
1835
1836 wdc_c.flags |= AT_WAIT;
1837
1838 wdc_c.timeout = wi->wi_atareq.timeout;
1839 wdc_c.r_command = wi->wi_atareq.command;
1840 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1841 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1842 wdc_c.r_sector = wi->wi_atareq.sec_num;
1843 wdc_c.r_count = wi->wi_atareq.sec_count;
1844 wdc_c.r_precomp = wi->wi_atareq.features;
1845 wdc_c.r_st_bmask = WDCS_DRDY;
1846 wdc_c.r_st_pmask = WDCS_DRDY;
1847 wdc_c.data = wi->wi_bp.b_data;
1848 wdc_c.bcount = wi->wi_bp.b_bcount;
1849
1850 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &wdc_c)
1851 != WDC_COMPLETE) {
1852 wi->wi_atareq.retsts = ATACMD_ERROR;
1853 goto bad;
1854 }
1855
1856 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1857 if (wdc_c.flags & AT_ERROR) {
1858 wi->wi_atareq.retsts = ATACMD_ERROR;
1859 wi->wi_atareq.error = wdc_c.r_error;
1860 } else if (wdc_c.flags & AT_DF)
1861 wi->wi_atareq.retsts = ATACMD_DF;
1862 else
1863 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1864 } else {
1865 wi->wi_atareq.retsts = ATACMD_OK;
1866 if (wi->wi_atareq.flags & ATACMD_READREG) {
1867 wi->wi_atareq.head = wdc_c.r_head ;
1868 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1869 wi->wi_atareq.sec_num = wdc_c.r_sector;
1870 wi->wi_atareq.sec_count = wdc_c.r_count;
1871 wi->wi_atareq.features = wdc_c.r_precomp;
1872 wi->wi_atareq.error = wdc_c.r_error;
1873 }
1874 }
1875
1876 bp->b_error = 0;
1877 biodone(bp);
1878 return;
1879 bad:
1880 bp->b_flags |= B_ERROR;
1881 bp->b_error = error;
1882 biodone(bp);
1883 }
1884